xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision d12abe7ce2663ac39e686a14960eb4febf560195)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * SCSI disk target driver.
30  */
31 #include <sys/scsi/scsi.h>
32 #include <sys/dkbad.h>
33 #include <sys/dklabel.h>
34 #include <sys/dkio.h>
35 #include <sys/fdio.h>
36 #include <sys/cdio.h>
37 #include <sys/mhd.h>
38 #include <sys/vtoc.h>
39 #include <sys/dktp/fdisk.h>
40 #include <sys/file.h>
41 #include <sys/stat.h>
42 #include <sys/kstat.h>
43 #include <sys/vtrace.h>
44 #include <sys/note.h>
45 #include <sys/thread.h>
46 #include <sys/proc.h>
47 #include <sys/efi_partition.h>
48 #include <sys/var.h>
49 #include <sys/aio_req.h>
50 
51 #ifdef __lock_lint
52 #define	_LP64
53 #define	__amd64
54 #endif
55 
56 #if (defined(__fibre))
57 /* Note: is there a leadville version of the following? */
58 #include <sys/fc4/fcal_linkapp.h>
59 #endif
60 #include <sys/taskq.h>
61 #include <sys/uuid.h>
62 #include <sys/byteorder.h>
63 #include <sys/sdt.h>
64 
65 #include "sd_xbuf.h"
66 
67 #include <sys/scsi/targets/sddef.h>
68 
69 
70 /*
71  * Loadable module info.
72  */
73 #if (defined(__fibre))
74 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver %I%"
75 char _depends_on[]	= "misc/scsi drv/fcp";
76 #else
77 #define	SD_MODULE_NAME	"SCSI Disk Driver %I%"
78 char _depends_on[]	= "misc/scsi";
79 #endif
80 
81 /*
82  * Define the interconnect type, to allow the driver to distinguish
83  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
84  *
85  * This is really for backward compatability. In the future, the driver
86  * should actually check the "interconnect-type" property as reported by
87  * the HBA; however at present this property is not defined by all HBAs,
88  * so we will use this #define (1) to permit the driver to run in
89  * backward-compatability mode; and (2) to print a notification message
90  * if an FC HBA does not support the "interconnect-type" property.  The
91  * behavior of the driver will be to assume parallel SCSI behaviors unless
92  * the "interconnect-type" property is defined by the HBA **AND** has a
93  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
94  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
95  * Channel behaviors (as per the old ssd).  (Note that the
96  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
97  * will result in the driver assuming parallel SCSI behaviors.)
98  *
99  * (see common/sys/scsi/impl/services.h)
100  *
101  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
102  * since some FC HBAs may already support that, and there is some code in
103  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
104  * default would confuse that code, and besides things should work fine
105  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
106  * "interconnect_type" property.
107  */
108 #if (defined(__fibre))
109 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
110 #else
111 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
112 #endif
113 
114 /*
115  * The name of the driver, established from the module name in _init.
116  */
117 static	char *sd_label			= NULL;
118 
119 /*
120  * Driver name is unfortunately prefixed on some driver.conf properties.
121  */
122 #if (defined(__fibre))
123 #define	sd_max_xfer_size		ssd_max_xfer_size
124 #define	sd_config_list			ssd_config_list
125 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
126 static	char *sd_config_list		= "ssd-config-list";
127 #else
128 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
129 static	char *sd_config_list		= "sd-config-list";
130 #endif
131 
132 /*
133  * Driver global variables
134  */
135 
136 #if (defined(__fibre))
137 /*
138  * These #defines are to avoid namespace collisions that occur because this
139  * code is currently used to compile two seperate driver modules: sd and ssd.
140  * All global variables need to be treated this way (even if declared static)
141  * in order to allow the debugger to resolve the names properly.
142  * It is anticipated that in the near future the ssd module will be obsoleted,
143  * at which time this namespace issue should go away.
144  */
145 #define	sd_state			ssd_state
146 #define	sd_io_time			ssd_io_time
147 #define	sd_failfast_enable		ssd_failfast_enable
148 #define	sd_ua_retry_count		ssd_ua_retry_count
149 #define	sd_report_pfa			ssd_report_pfa
150 #define	sd_max_throttle			ssd_max_throttle
151 #define	sd_min_throttle			ssd_min_throttle
152 #define	sd_rot_delay			ssd_rot_delay
153 
154 #define	sd_retry_on_reservation_conflict	\
155 					ssd_retry_on_reservation_conflict
156 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
157 #define	sd_resv_conflict_name		ssd_resv_conflict_name
158 
159 #define	sd_component_mask		ssd_component_mask
160 #define	sd_level_mask			ssd_level_mask
161 #define	sd_debug_un			ssd_debug_un
162 #define	sd_error_level			ssd_error_level
163 
164 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
165 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
166 
167 #define	sd_tr				ssd_tr
168 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
169 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
170 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
171 #define	sd_check_media_time		ssd_check_media_time
172 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
173 #define	sd_label_mutex			ssd_label_mutex
174 #define	sd_detach_mutex			ssd_detach_mutex
175 #define	sd_log_buf			ssd_log_buf
176 #define	sd_log_mutex			ssd_log_mutex
177 
178 #define	sd_disk_table			ssd_disk_table
179 #define	sd_disk_table_size		ssd_disk_table_size
180 #define	sd_sense_mutex			ssd_sense_mutex
181 #define	sd_cdbtab			ssd_cdbtab
182 
183 #define	sd_cb_ops			ssd_cb_ops
184 #define	sd_ops				ssd_ops
185 #define	sd_additional_codes		ssd_additional_codes
186 
187 #define	sd_minor_data			ssd_minor_data
188 #define	sd_minor_data_efi		ssd_minor_data_efi
189 
190 #define	sd_tq				ssd_tq
191 #define	sd_wmr_tq			ssd_wmr_tq
192 #define	sd_taskq_name			ssd_taskq_name
193 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
194 #define	sd_taskq_minalloc		ssd_taskq_minalloc
195 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
196 
197 #define	sd_dump_format_string		ssd_dump_format_string
198 
199 #define	sd_iostart_chain		ssd_iostart_chain
200 #define	sd_iodone_chain			ssd_iodone_chain
201 
202 #define	sd_pm_idletime			ssd_pm_idletime
203 
204 #define	sd_force_pm_supported		ssd_force_pm_supported
205 
206 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
207 
208 #endif
209 
210 
211 #ifdef	SDDEBUG
212 int	sd_force_pm_supported		= 0;
213 #endif	/* SDDEBUG */
214 
215 void *sd_state				= NULL;
216 int sd_io_time				= SD_IO_TIME;
217 int sd_failfast_enable			= 1;
218 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
219 int sd_report_pfa			= 1;
220 int sd_max_throttle			= SD_MAX_THROTTLE;
221 int sd_min_throttle			= SD_MIN_THROTTLE;
222 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
223 int sd_qfull_throttle_enable		= TRUE;
224 
225 int sd_retry_on_reservation_conflict	= 1;
226 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
227 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
228 
229 static int sd_dtype_optical_bind	= -1;
230 
231 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
232 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
233 
234 /*
235  * Global data for debug logging. To enable debug printing, sd_component_mask
236  * and sd_level_mask should be set to the desired bit patterns as outlined in
237  * sddef.h.
238  */
239 uint_t	sd_component_mask		= 0x0;
240 uint_t	sd_level_mask			= 0x0;
241 struct	sd_lun *sd_debug_un		= NULL;
242 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
243 
244 /* Note: these may go away in the future... */
245 static uint32_t	sd_xbuf_active_limit	= 512;
246 static uint32_t sd_xbuf_reserve_limit	= 16;
247 
248 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
249 
250 /*
251  * Timer value used to reset the throttle after it has been reduced
252  * (typically in response to TRAN_BUSY or STATUS_QFULL)
253  */
254 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
255 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
256 
257 /*
258  * Interval value associated with the media change scsi watch.
259  */
260 static int sd_check_media_time		= 3000000;
261 
262 /*
263  * Wait value used for in progress operations during a DDI_SUSPEND
264  */
265 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
266 
267 /*
268  * sd_label_mutex protects a static buffer used in the disk label
269  * component of the driver
270  */
271 static kmutex_t sd_label_mutex;
272 
273 /*
274  * sd_detach_mutex protects un_layer_count, un_detach_count, and
275  * un_opens_in_progress in the sd_lun structure.
276  */
277 static kmutex_t sd_detach_mutex;
278 
279 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
280 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
281 
282 /*
283  * Global buffer and mutex for debug logging
284  */
285 static char	sd_log_buf[1024];
286 static kmutex_t	sd_log_mutex;
287 
288 
289 /*
290  * "Smart" Probe Caching structs, globals, #defines, etc.
291  * For parallel scsi and non-self-identify device only.
292  */
293 
294 /*
295  * The following resources and routines are implemented to support
296  * "smart" probing, which caches the scsi_probe() results in an array,
297  * in order to help avoid long probe times.
298  */
299 struct sd_scsi_probe_cache {
300 	struct	sd_scsi_probe_cache	*next;
301 	dev_info_t	*pdip;
302 	int		cache[NTARGETS_WIDE];
303 };
304 
305 static kmutex_t	sd_scsi_probe_cache_mutex;
306 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
307 
308 /*
309  * Really we only need protection on the head of the linked list, but
310  * better safe than sorry.
311  */
312 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
313     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
314 
315 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
316     sd_scsi_probe_cache_head))
317 
318 
319 /*
320  * Vendor specific data name property declarations
321  */
322 
323 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
324 
325 static sd_tunables seagate_properties = {
326 	SEAGATE_THROTTLE_VALUE,
327 	0,
328 	0,
329 	0,
330 	0,
331 	0,
332 	0,
333 	0,
334 	0
335 };
336 
337 
338 static sd_tunables fujitsu_properties = {
339 	FUJITSU_THROTTLE_VALUE,
340 	0,
341 	0,
342 	0,
343 	0,
344 	0,
345 	0,
346 	0,
347 	0
348 };
349 
350 static sd_tunables ibm_properties = {
351 	IBM_THROTTLE_VALUE,
352 	0,
353 	0,
354 	0,
355 	0,
356 	0,
357 	0,
358 	0,
359 	0
360 };
361 
362 static sd_tunables purple_properties = {
363 	PURPLE_THROTTLE_VALUE,
364 	0,
365 	0,
366 	PURPLE_BUSY_RETRIES,
367 	PURPLE_RESET_RETRY_COUNT,
368 	PURPLE_RESERVE_RELEASE_TIME,
369 	0,
370 	0,
371 	0
372 };
373 
374 static sd_tunables sve_properties = {
375 	SVE_THROTTLE_VALUE,
376 	0,
377 	0,
378 	SVE_BUSY_RETRIES,
379 	SVE_RESET_RETRY_COUNT,
380 	SVE_RESERVE_RELEASE_TIME,
381 	SVE_MIN_THROTTLE_VALUE,
382 	SVE_DISKSORT_DISABLED_FLAG,
383 	0
384 };
385 
386 static sd_tunables maserati_properties = {
387 	0,
388 	0,
389 	0,
390 	0,
391 	0,
392 	0,
393 	0,
394 	MASERATI_DISKSORT_DISABLED_FLAG,
395 	MASERATI_LUN_RESET_ENABLED_FLAG
396 };
397 
398 static sd_tunables pirus_properties = {
399 	PIRUS_THROTTLE_VALUE,
400 	0,
401 	PIRUS_NRR_COUNT,
402 	PIRUS_BUSY_RETRIES,
403 	PIRUS_RESET_RETRY_COUNT,
404 	0,
405 	PIRUS_MIN_THROTTLE_VALUE,
406 	PIRUS_DISKSORT_DISABLED_FLAG,
407 	PIRUS_LUN_RESET_ENABLED_FLAG
408 };
409 
410 #endif
411 
412 #if (defined(__sparc) && !defined(__fibre)) || \
413 	(defined(__i386) || defined(__amd64))
414 
415 
416 static sd_tunables elite_properties = {
417 	ELITE_THROTTLE_VALUE,
418 	0,
419 	0,
420 	0,
421 	0,
422 	0,
423 	0,
424 	0,
425 	0
426 };
427 
428 static sd_tunables st31200n_properties = {
429 	ST31200N_THROTTLE_VALUE,
430 	0,
431 	0,
432 	0,
433 	0,
434 	0,
435 	0,
436 	0,
437 	0
438 };
439 
440 #endif /* Fibre or not */
441 
442 static sd_tunables lsi_properties_scsi = {
443 	LSI_THROTTLE_VALUE,
444 	0,
445 	LSI_NOTREADY_RETRIES,
446 	0,
447 	0,
448 	0,
449 	0,
450 	0,
451 	0
452 };
453 
454 static sd_tunables symbios_properties = {
455 	SYMBIOS_THROTTLE_VALUE,
456 	0,
457 	SYMBIOS_NOTREADY_RETRIES,
458 	0,
459 	0,
460 	0,
461 	0,
462 	0,
463 	0
464 };
465 
466 static sd_tunables lsi_properties = {
467 	0,
468 	0,
469 	LSI_NOTREADY_RETRIES,
470 	0,
471 	0,
472 	0,
473 	0,
474 	0,
475 	0
476 };
477 
478 static sd_tunables lsi_oem_properties = {
479 	0,
480 	0,
481 	LSI_OEM_NOTREADY_RETRIES,
482 	0,
483 	0,
484 	0,
485 	0,
486 	0,
487 	0
488 };
489 
490 
491 
492 #if (defined(SD_PROP_TST))
493 
494 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
495 #define	SD_TST_THROTTLE_VAL	16
496 #define	SD_TST_NOTREADY_VAL	12
497 #define	SD_TST_BUSY_VAL		60
498 #define	SD_TST_RST_RETRY_VAL	36
499 #define	SD_TST_RSV_REL_TIME	60
500 
501 static sd_tunables tst_properties = {
502 	SD_TST_THROTTLE_VAL,
503 	SD_TST_CTYPE_VAL,
504 	SD_TST_NOTREADY_VAL,
505 	SD_TST_BUSY_VAL,
506 	SD_TST_RST_RETRY_VAL,
507 	SD_TST_RSV_REL_TIME,
508 	0,
509 	0,
510 	0
511 };
512 #endif
513 
514 /* This is similiar to the ANSI toupper implementation */
515 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
516 
517 /*
518  * Static Driver Configuration Table
519  *
520  * This is the table of disks which need throttle adjustment (or, perhaps
521  * something else as defined by the flags at a future time.)  device_id
522  * is a string consisting of concatenated vid (vendor), pid (product/model)
523  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
524  * the parts of the string are as defined by the sizes in the scsi_inquiry
525  * structure.  Device type is searched as far as the device_id string is
526  * defined.  Flags defines which values are to be set in the driver from the
527  * properties list.
528  *
529  * Entries below which begin and end with a "*" are a special case.
530  * These do not have a specific vendor, and the string which follows
531  * can appear anywhere in the 16 byte PID portion of the inquiry data.
532  *
533  * Entries below which begin and end with a " " (blank) are a special
534  * case. The comparison function will treat multiple consecutive blanks
535  * as equivalent to a single blank. For example, this causes a
536  * sd_disk_table entry of " NEC CDROM " to match a device's id string
537  * of  "NEC       CDROM".
538  *
539  * Note: The MD21 controller type has been obsoleted.
540  *	 ST318202F is a Legacy device
541  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
542  *	 made with an FC connection. The entries here are a legacy.
543  */
544 static sd_disk_config_t sd_disk_table[] = {
545 #if defined(__fibre) || defined(__i386) || defined(__amd64)
546 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
547 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
548 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
549 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
550 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
551 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
552 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
553 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
554 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
555 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
556 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
557 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
558 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
559 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
560 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
561 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
562 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
563 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
564 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
565 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
566 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
567 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
568 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
569 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
570 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
571 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
572 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
573 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
574 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
575 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
576 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
577 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
578 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
579 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
580 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
581 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
582 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
583 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
584 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
585 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
586 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
587 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
588 			SD_CONF_BSET_BSY_RETRY_COUNT|
589 			SD_CONF_BSET_RST_RETRIES|
590 			SD_CONF_BSET_RSV_REL_TIME,
591 		&purple_properties },
592 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
593 		SD_CONF_BSET_BSY_RETRY_COUNT|
594 		SD_CONF_BSET_RST_RETRIES|
595 		SD_CONF_BSET_RSV_REL_TIME|
596 		SD_CONF_BSET_MIN_THROTTLE|
597 		SD_CONF_BSET_DISKSORT_DISABLED,
598 		&sve_properties },
599 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
600 			SD_CONF_BSET_BSY_RETRY_COUNT|
601 			SD_CONF_BSET_RST_RETRIES|
602 			SD_CONF_BSET_RSV_REL_TIME,
603 		&purple_properties },
604 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
605 		SD_CONF_BSET_LUN_RESET_ENABLED,
606 		&maserati_properties },
607 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
608 		SD_CONF_BSET_NRR_COUNT|
609 		SD_CONF_BSET_BSY_RETRY_COUNT|
610 		SD_CONF_BSET_RST_RETRIES|
611 		SD_CONF_BSET_MIN_THROTTLE|
612 		SD_CONF_BSET_DISKSORT_DISABLED|
613 		SD_CONF_BSET_LUN_RESET_ENABLED,
614 		&pirus_properties },
615 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
616 		SD_CONF_BSET_NRR_COUNT|
617 		SD_CONF_BSET_BSY_RETRY_COUNT|
618 		SD_CONF_BSET_RST_RETRIES|
619 		SD_CONF_BSET_MIN_THROTTLE|
620 		SD_CONF_BSET_DISKSORT_DISABLED|
621 		SD_CONF_BSET_LUN_RESET_ENABLED,
622 		&pirus_properties },
623 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
624 		SD_CONF_BSET_NRR_COUNT|
625 		SD_CONF_BSET_BSY_RETRY_COUNT|
626 		SD_CONF_BSET_RST_RETRIES|
627 		SD_CONF_BSET_MIN_THROTTLE|
628 		SD_CONF_BSET_DISKSORT_DISABLED|
629 		SD_CONF_BSET_LUN_RESET_ENABLED,
630 		&pirus_properties },
631 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
632 		SD_CONF_BSET_NRR_COUNT|
633 		SD_CONF_BSET_BSY_RETRY_COUNT|
634 		SD_CONF_BSET_RST_RETRIES|
635 		SD_CONF_BSET_MIN_THROTTLE|
636 		SD_CONF_BSET_DISKSORT_DISABLED|
637 		SD_CONF_BSET_LUN_RESET_ENABLED,
638 		&pirus_properties },
639 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
640 		SD_CONF_BSET_NRR_COUNT|
641 		SD_CONF_BSET_BSY_RETRY_COUNT|
642 		SD_CONF_BSET_RST_RETRIES|
643 		SD_CONF_BSET_MIN_THROTTLE|
644 		SD_CONF_BSET_DISKSORT_DISABLED|
645 		SD_CONF_BSET_LUN_RESET_ENABLED,
646 		&pirus_properties },
647 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
648 		SD_CONF_BSET_NRR_COUNT|
649 		SD_CONF_BSET_BSY_RETRY_COUNT|
650 		SD_CONF_BSET_RST_RETRIES|
651 		SD_CONF_BSET_MIN_THROTTLE|
652 		SD_CONF_BSET_DISKSORT_DISABLED|
653 		SD_CONF_BSET_LUN_RESET_ENABLED,
654 		&pirus_properties },
655 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
660 #endif /* fibre or NON-sparc platforms */
661 #if ((defined(__sparc) && !defined(__fibre)) ||\
662 	(defined(__i386) || defined(__amd64)))
663 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
664 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
665 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
666 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
667 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
668 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
669 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
670 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
671 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
672 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
673 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
674 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
675 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
676 	    &symbios_properties },
677 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
678 	    &lsi_properties_scsi },
679 #if defined(__i386) || defined(__amd64)
680 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
681 				    | SD_CONF_BSET_READSUB_BCD
682 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
683 				    | SD_CONF_BSET_NO_READ_HEADER
684 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
685 
686 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
687 				    | SD_CONF_BSET_READSUB_BCD
688 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
689 				    | SD_CONF_BSET_NO_READ_HEADER
690 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
691 #endif /* __i386 || __amd64 */
692 #endif /* sparc NON-fibre or NON-sparc platforms */
693 
694 #if (defined(SD_PROP_TST))
695 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
696 				| SD_CONF_BSET_CTYPE
697 				| SD_CONF_BSET_NRR_COUNT
698 				| SD_CONF_BSET_FAB_DEVID
699 				| SD_CONF_BSET_NOCACHE
700 				| SD_CONF_BSET_BSY_RETRY_COUNT
701 				| SD_CONF_BSET_PLAYMSF_BCD
702 				| SD_CONF_BSET_READSUB_BCD
703 				| SD_CONF_BSET_READ_TOC_TRK_BCD
704 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
705 				| SD_CONF_BSET_NO_READ_HEADER
706 				| SD_CONF_BSET_READ_CD_XD4
707 				| SD_CONF_BSET_RST_RETRIES
708 				| SD_CONF_BSET_RSV_REL_TIME
709 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
710 #endif
711 };
712 
713 static const int sd_disk_table_size =
714 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
715 
716 
717 /*
718  * Return codes of sd_uselabel().
719  */
720 #define	SD_LABEL_IS_VALID		0
721 #define	SD_LABEL_IS_INVALID		1
722 
723 #define	SD_INTERCONNECT_PARALLEL	0
724 #define	SD_INTERCONNECT_FABRIC		1
725 #define	SD_INTERCONNECT_FIBRE		2
726 #define	SD_INTERCONNECT_SSA		3
727 #define	SD_IS_PARALLEL_SCSI(un)		\
728 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
729 
730 /*
731  * Definitions used by device id registration routines
732  */
733 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
734 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
735 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
736 #define	WD_NODE			7	/* the whole disk minor */
737 
738 static kmutex_t sd_sense_mutex = {0};
739 
740 /*
741  * Macros for updates of the driver state
742  */
743 #define	New_state(un, s)        \
744 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
745 #define	Restore_state(un)	\
746 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
747 
748 static struct sd_cdbinfo sd_cdbtab[] = {
749 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
750 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
751 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
752 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
753 };
754 
755 /*
756  * Specifies the number of seconds that must have elapsed since the last
757  * cmd. has completed for a device to be declared idle to the PM framework.
758  */
759 static int sd_pm_idletime = 1;
760 
761 /*
762  * Internal function prototypes
763  */
764 
765 #if (defined(__fibre))
766 /*
767  * These #defines are to avoid namespace collisions that occur because this
768  * code is currently used to compile two seperate driver modules: sd and ssd.
769  * All function names need to be treated this way (even if declared static)
770  * in order to allow the debugger to resolve the names properly.
771  * It is anticipated that in the near future the ssd module will be obsoleted,
772  * at which time this ugliness should go away.
773  */
774 #define	sd_log_trace			ssd_log_trace
775 #define	sd_log_info			ssd_log_info
776 #define	sd_log_err			ssd_log_err
777 #define	sdprobe				ssdprobe
778 #define	sdinfo				ssdinfo
779 #define	sd_prop_op			ssd_prop_op
780 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
781 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
782 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
783 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
784 #define	sd_spin_up_unit			ssd_spin_up_unit
785 #define	sd_enable_descr_sense		ssd_enable_descr_sense
786 #define	sd_set_mmc_caps			ssd_set_mmc_caps
787 #define	sd_read_unit_properties		ssd_read_unit_properties
788 #define	sd_process_sdconf_file		ssd_process_sdconf_file
789 #define	sd_process_sdconf_table		ssd_process_sdconf_table
790 #define	sd_sdconf_id_match		ssd_sdconf_id_match
791 #define	sd_blank_cmp			ssd_blank_cmp
792 #define	sd_chk_vers1_data		ssd_chk_vers1_data
793 #define	sd_set_vers1_properties		ssd_set_vers1_properties
794 #define	sd_validate_geometry		ssd_validate_geometry
795 
796 #if defined(_SUNOS_VTOC_16)
797 #define	sd_convert_geometry		ssd_convert_geometry
798 #endif
799 
800 #define	sd_resync_geom_caches		ssd_resync_geom_caches
801 #define	sd_read_fdisk			ssd_read_fdisk
802 #define	sd_get_physical_geometry	ssd_get_physical_geometry
803 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
804 #define	sd_update_block_info		ssd_update_block_info
805 #define	sd_swap_efi_gpt			ssd_swap_efi_gpt
806 #define	sd_swap_efi_gpe			ssd_swap_efi_gpe
807 #define	sd_validate_efi			ssd_validate_efi
808 #define	sd_use_efi			ssd_use_efi
809 #define	sd_uselabel			ssd_uselabel
810 #define	sd_build_default_label		ssd_build_default_label
811 #define	sd_has_max_chs_vals		ssd_has_max_chs_vals
812 #define	sd_inq_fill			ssd_inq_fill
813 #define	sd_register_devid		ssd_register_devid
814 #define	sd_get_devid_block		ssd_get_devid_block
815 #define	sd_get_devid			ssd_get_devid
816 #define	sd_create_devid			ssd_create_devid
817 #define	sd_write_deviceid		ssd_write_deviceid
818 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
819 #define	sd_setup_pm			ssd_setup_pm
820 #define	sd_create_pm_components		ssd_create_pm_components
821 #define	sd_ddi_suspend			ssd_ddi_suspend
822 #define	sd_ddi_pm_suspend		ssd_ddi_pm_suspend
823 #define	sd_ddi_resume			ssd_ddi_resume
824 #define	sd_ddi_pm_resume		ssd_ddi_pm_resume
825 #define	sdpower				ssdpower
826 #define	sdattach			ssdattach
827 #define	sddetach			ssddetach
828 #define	sd_unit_attach			ssd_unit_attach
829 #define	sd_unit_detach			ssd_unit_detach
830 #define	sd_set_unit_attributes		ssd_set_unit_attributes
831 #define	sd_create_minor_nodes		ssd_create_minor_nodes
832 #define	sd_create_errstats		ssd_create_errstats
833 #define	sd_set_errstats			ssd_set_errstats
834 #define	sd_set_pstats			ssd_set_pstats
835 #define	sddump				ssddump
836 #define	sd_scsi_poll			ssd_scsi_poll
837 #define	sd_send_polled_RQS		ssd_send_polled_RQS
838 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
839 #define	sd_init_event_callbacks		ssd_init_event_callbacks
840 #define	sd_event_callback		ssd_event_callback
841 #define	sd_cache_control		ssd_cache_control
842 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
843 #define	sd_make_device			ssd_make_device
844 #define	sdopen				ssdopen
845 #define	sdclose				ssdclose
846 #define	sd_ready_and_valid		ssd_ready_and_valid
847 #define	sdmin				ssdmin
848 #define	sdread				ssdread
849 #define	sdwrite				ssdwrite
850 #define	sdaread				ssdaread
851 #define	sdawrite			ssdawrite
852 #define	sdstrategy			ssdstrategy
853 #define	sdioctl				ssdioctl
854 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
855 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
856 #define	sd_checksum_iostart		ssd_checksum_iostart
857 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
858 #define	sd_pm_iostart			ssd_pm_iostart
859 #define	sd_core_iostart			ssd_core_iostart
860 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
861 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
862 #define	sd_checksum_iodone		ssd_checksum_iodone
863 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
864 #define	sd_pm_iodone			ssd_pm_iodone
865 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
866 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
867 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
868 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
869 #define	sd_buf_iodone			ssd_buf_iodone
870 #define	sd_uscsi_strategy		ssd_uscsi_strategy
871 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
872 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
873 #define	sd_uscsi_iodone			ssd_uscsi_iodone
874 #define	sd_xbuf_strategy		ssd_xbuf_strategy
875 #define	sd_xbuf_init			ssd_xbuf_init
876 #define	sd_pm_entry			ssd_pm_entry
877 #define	sd_pm_exit			ssd_pm_exit
878 
879 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
880 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
881 
882 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
883 #define	sdintr				ssdintr
884 #define	sd_start_cmds			ssd_start_cmds
885 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
886 #define	sd_bioclone_alloc		ssd_bioclone_alloc
887 #define	sd_bioclone_free		ssd_bioclone_free
888 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
889 #define	sd_shadow_buf_free		ssd_shadow_buf_free
890 #define	sd_print_transport_rejected_message	\
891 					ssd_print_transport_rejected_message
892 #define	sd_retry_command		ssd_retry_command
893 #define	sd_set_retry_bp			ssd_set_retry_bp
894 #define	sd_send_request_sense_command	ssd_send_request_sense_command
895 #define	sd_start_retry_command		ssd_start_retry_command
896 #define	sd_start_direct_priority_command	\
897 					ssd_start_direct_priority_command
898 #define	sd_return_failed_command	ssd_return_failed_command
899 #define	sd_return_failed_command_no_restart	\
900 					ssd_return_failed_command_no_restart
901 #define	sd_return_command		ssd_return_command
902 #define	sd_sync_with_callback		ssd_sync_with_callback
903 #define	sdrunout			ssdrunout
904 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
905 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
906 #define	sd_reduce_throttle		ssd_reduce_throttle
907 #define	sd_restore_throttle		ssd_restore_throttle
908 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
909 #define	sd_init_cdb_limits		ssd_init_cdb_limits
910 #define	sd_pkt_status_good		ssd_pkt_status_good
911 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
912 #define	sd_pkt_status_busy		ssd_pkt_status_busy
913 #define	sd_pkt_status_reservation_conflict	\
914 					ssd_pkt_status_reservation_conflict
915 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
916 #define	sd_handle_request_sense		ssd_handle_request_sense
917 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
918 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
919 #define	sd_validate_sense_data		ssd_validate_sense_data
920 #define	sd_decode_sense			ssd_decode_sense
921 #define	sd_print_sense_msg		ssd_print_sense_msg
922 #define	sd_extract_sense_info_descr	ssd_extract_sense_info_descr
923 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
924 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
925 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
926 #define	sd_sense_key_medium_or_hardware_error	\
927 					ssd_sense_key_medium_or_hardware_error
928 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
929 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
930 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
931 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
932 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
933 #define	sd_sense_key_default		ssd_sense_key_default
934 #define	sd_print_retry_msg		ssd_print_retry_msg
935 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
936 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
937 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
938 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
939 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
940 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
941 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
942 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
943 #define	sd_pkt_reason_default		ssd_pkt_reason_default
944 #define	sd_reset_target			ssd_reset_target
945 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
946 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
947 #define	sd_taskq_create			ssd_taskq_create
948 #define	sd_taskq_delete			ssd_taskq_delete
949 #define	sd_media_change_task		ssd_media_change_task
950 #define	sd_handle_mchange		ssd_handle_mchange
951 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
952 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
953 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
954 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
955 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
956 					sd_send_scsi_feature_GET_CONFIGURATION
957 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
958 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
959 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
960 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
961 					ssd_send_scsi_PERSISTENT_RESERVE_IN
962 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
963 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
964 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
965 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
966 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
967 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
968 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
969 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
970 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
971 #define	sd_alloc_rqs			ssd_alloc_rqs
972 #define	sd_free_rqs			ssd_free_rqs
973 #define	sd_dump_memory			ssd_dump_memory
974 #define	sd_uscsi_ioctl			ssd_uscsi_ioctl
975 #define	sd_get_media_info		ssd_get_media_info
976 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
977 #define	sd_dkio_get_geometry		ssd_dkio_get_geometry
978 #define	sd_dkio_set_geometry		ssd_dkio_set_geometry
979 #define	sd_dkio_get_partition		ssd_dkio_get_partition
980 #define	sd_dkio_set_partition		ssd_dkio_set_partition
981 #define	sd_dkio_partition		ssd_dkio_partition
982 #define	sd_dkio_get_vtoc		ssd_dkio_get_vtoc
983 #define	sd_dkio_get_efi			ssd_dkio_get_efi
984 #define	sd_build_user_vtoc		ssd_build_user_vtoc
985 #define	sd_dkio_set_vtoc		ssd_dkio_set_vtoc
986 #define	sd_dkio_set_efi			ssd_dkio_set_efi
987 #define	sd_build_label_vtoc		ssd_build_label_vtoc
988 #define	sd_write_label			ssd_write_label
989 #define	sd_clear_vtoc			ssd_clear_vtoc
990 #define	sd_clear_efi			ssd_clear_efi
991 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
992 #define	sd_setup_next_xfer		ssd_setup_next_xfer
993 #define	sd_dkio_get_temp		ssd_dkio_get_temp
994 #define	sd_dkio_get_mboot		ssd_dkio_get_mboot
995 #define	sd_dkio_set_mboot		ssd_dkio_set_mboot
996 #define	sd_setup_default_geometry	ssd_setup_default_geometry
997 #define	sd_update_fdisk_and_vtoc	ssd_update_fdisk_and_vtoc
998 #define	sd_check_mhd			ssd_check_mhd
999 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1000 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1001 #define	sd_sname			ssd_sname
1002 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1003 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1004 #define	sd_take_ownership		ssd_take_ownership
1005 #define	sd_reserve_release		ssd_reserve_release
1006 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1007 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1008 #define	sd_persistent_reservation_in_read_keys	\
1009 					ssd_persistent_reservation_in_read_keys
1010 #define	sd_persistent_reservation_in_read_resv	\
1011 					ssd_persistent_reservation_in_read_resv
1012 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1013 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1014 #define	sd_mhdioc_release		ssd_mhdioc_release
1015 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1016 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1017 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1018 #define	sr_change_blkmode		ssr_change_blkmode
1019 #define	sr_change_speed			ssr_change_speed
1020 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1021 #define	sr_pause_resume			ssr_pause_resume
1022 #define	sr_play_msf			ssr_play_msf
1023 #define	sr_play_trkind			ssr_play_trkind
1024 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1025 #define	sr_read_subchannel		ssr_read_subchannel
1026 #define	sr_read_tocentry		ssr_read_tocentry
1027 #define	sr_read_tochdr			ssr_read_tochdr
1028 #define	sr_read_cdda			ssr_read_cdda
1029 #define	sr_read_cdxa			ssr_read_cdxa
1030 #define	sr_read_mode1			ssr_read_mode1
1031 #define	sr_read_mode2			ssr_read_mode2
1032 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1033 #define	sr_sector_mode			ssr_sector_mode
1034 #define	sr_eject			ssr_eject
1035 #define	sr_ejected			ssr_ejected
1036 #define	sr_check_wp			ssr_check_wp
1037 #define	sd_check_media			ssd_check_media
1038 #define	sd_media_watch_cb		ssd_media_watch_cb
1039 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1040 #define	sr_volume_ctrl			ssr_volume_ctrl
1041 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1042 #define	sd_log_page_supported		ssd_log_page_supported
1043 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1044 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1045 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1046 #define	sd_range_lock			ssd_range_lock
1047 #define	sd_get_range			ssd_get_range
1048 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1049 #define	sd_range_unlock			ssd_range_unlock
1050 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1051 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1052 
1053 #define	sd_iostart_chain		ssd_iostart_chain
1054 #define	sd_iodone_chain			ssd_iodone_chain
1055 #define	sd_initpkt_map			ssd_initpkt_map
1056 #define	sd_destroypkt_map		ssd_destroypkt_map
1057 #define	sd_chain_type_map		ssd_chain_type_map
1058 #define	sd_chain_index_map		ssd_chain_index_map
1059 
1060 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1061 #define	sd_failfast_flushq		ssd_failfast_flushq
1062 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1063 
1064 #define	sd_is_lsi			ssd_is_lsi
1065 
1066 #endif	/* #if (defined(__fibre)) */
1067 
1068 
1069 int _init(void);
1070 int _fini(void);
1071 int _info(struct modinfo *modinfop);
1072 
1073 /*PRINTFLIKE3*/
1074 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1075 /*PRINTFLIKE3*/
1076 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1077 /*PRINTFLIKE3*/
1078 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1079 
1080 static int sdprobe(dev_info_t *devi);
1081 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1082     void **result);
1083 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1084     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1085 
1086 /*
1087  * Smart probe for parallel scsi
1088  */
1089 static void sd_scsi_probe_cache_init(void);
1090 static void sd_scsi_probe_cache_fini(void);
1091 static void sd_scsi_clear_probe_cache(void);
1092 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1093 
1094 static int	sd_spin_up_unit(struct sd_lun *un);
1095 #ifdef _LP64
1096 static void	sd_enable_descr_sense(struct sd_lun *un);
1097 #endif /* _LP64 */
1098 static void	sd_set_mmc_caps(struct sd_lun *un);
1099 
1100 static void sd_read_unit_properties(struct sd_lun *un);
1101 static int  sd_process_sdconf_file(struct sd_lun *un);
1102 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1103     int *data_list, sd_tunables *values);
1104 static void sd_process_sdconf_table(struct sd_lun *un);
1105 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1106 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1107 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1108 	int list_len, char *dataname_ptr);
1109 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1110     sd_tunables *prop_list);
1111 static int  sd_validate_geometry(struct sd_lun *un, int path_flag);
1112 
1113 #if defined(_SUNOS_VTOC_16)
1114 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g);
1115 #endif
1116 
1117 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize,
1118 	int path_flag);
1119 static int  sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize,
1120 	int path_flag);
1121 static void sd_get_physical_geometry(struct sd_lun *un,
1122 	struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag);
1123 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity,
1124 	int lbasize);
1125 static int  sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag);
1126 static void sd_swap_efi_gpt(efi_gpt_t *);
1127 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *);
1128 static int sd_validate_efi(efi_gpt_t *);
1129 static int sd_use_efi(struct sd_lun *, int);
1130 static void sd_build_default_label(struct sd_lun *un);
1131 
1132 #if defined(_FIRMWARE_NEEDS_FDISK)
1133 static int  sd_has_max_chs_vals(struct ipart *fdp);
1134 #endif
1135 static void sd_inq_fill(char *p, int l, char *s);
1136 
1137 
1138 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi,
1139     int reservation_flag);
1140 static daddr_t  sd_get_devid_block(struct sd_lun *un);
1141 static int  sd_get_devid(struct sd_lun *un);
1142 static int  sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len);
1143 static ddi_devid_t sd_create_devid(struct sd_lun *un);
1144 static int  sd_write_deviceid(struct sd_lun *un);
1145 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1146 static int  sd_check_vpd_page_support(struct sd_lun *un);
1147 
1148 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi);
1149 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1150 
1151 static int  sd_ddi_suspend(dev_info_t *devi);
1152 static int  sd_ddi_pm_suspend(struct sd_lun *un);
1153 static int  sd_ddi_resume(dev_info_t *devi);
1154 static int  sd_ddi_pm_resume(struct sd_lun *un);
1155 static int  sdpower(dev_info_t *devi, int component, int level);
1156 
1157 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1158 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1159 static int  sd_unit_attach(dev_info_t *devi);
1160 static int  sd_unit_detach(dev_info_t *devi);
1161 
1162 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1163 static int  sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi);
1164 static void sd_create_errstats(struct sd_lun *un, int instance);
1165 static void sd_set_errstats(struct sd_lun *un);
1166 static void sd_set_pstats(struct sd_lun *un);
1167 
1168 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1169 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1170 static int  sd_send_polled_RQS(struct sd_lun *un);
1171 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1172 
1173 #if (defined(__fibre))
1174 /*
1175  * Event callbacks (photon)
1176  */
1177 static void sd_init_event_callbacks(struct sd_lun *un);
1178 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1179 #endif
1180 
1181 /*
1182  * Defines for sd_cache_control
1183  */
1184 
1185 #define	SD_CACHE_ENABLE		1
1186 #define	SD_CACHE_DISABLE	0
1187 #define	SD_CACHE_NOCHANGE	-1
1188 
1189 static int   sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag);
1190 static int   sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled);
1191 static dev_t sd_make_device(dev_info_t *devi);
1192 
1193 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1194 	uint64_t capacity);
1195 
1196 /*
1197  * Driver entry point functions.
1198  */
1199 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1200 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1201 static int  sd_ready_and_valid(struct sd_lun *un);
1202 
1203 static void sdmin(struct buf *bp);
1204 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1205 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1206 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1207 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1208 
1209 static int sdstrategy(struct buf *bp);
1210 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1211 
1212 /*
1213  * Function prototypes for layering functions in the iostart chain.
1214  */
1215 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1216 	struct buf *bp);
1217 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1218 	struct buf *bp);
1219 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1220 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1221 	struct buf *bp);
1222 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1223 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1224 
1225 /*
1226  * Function prototypes for layering functions in the iodone chain.
1227  */
1228 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1229 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1230 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1231 	struct buf *bp);
1232 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1233 	struct buf *bp);
1234 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1235 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1236 	struct buf *bp);
1237 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1238 
1239 /*
1240  * Prototypes for functions to support buf(9S) based IO.
1241  */
1242 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1243 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1244 static void sd_destroypkt_for_buf(struct buf *);
1245 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1246 	struct buf *bp, int flags,
1247 	int (*callback)(caddr_t), caddr_t callback_arg,
1248 	diskaddr_t lba, uint32_t blockcount);
1249 #if defined(__i386) || defined(__amd64)
1250 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1251 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1252 #endif /* defined(__i386) || defined(__amd64) */
1253 
1254 /*
1255  * Prototypes for functions to support USCSI IO.
1256  */
1257 static int sd_uscsi_strategy(struct buf *bp);
1258 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1259 static void sd_destroypkt_for_uscsi(struct buf *);
1260 
1261 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1262 	uchar_t chain_type, void *pktinfop);
1263 
1264 static int  sd_pm_entry(struct sd_lun *un);
1265 static void sd_pm_exit(struct sd_lun *un);
1266 
1267 static void sd_pm_idletimeout_handler(void *arg);
1268 
1269 /*
1270  * sd_core internal functions (used at the sd_core_io layer).
1271  */
1272 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1273 static void sdintr(struct scsi_pkt *pktp);
1274 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1275 
1276 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd,
1277 	enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace,
1278 	int path_flag);
1279 
1280 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1281 	daddr_t blkno, int (*func)(struct buf *));
1282 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1283 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1284 static void sd_bioclone_free(struct buf *bp);
1285 static void sd_shadow_buf_free(struct buf *bp);
1286 
1287 static void sd_print_transport_rejected_message(struct sd_lun *un,
1288 	struct sd_xbuf *xp, int code);
1289 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1290     void *arg, int code);
1291 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1292     void *arg, int code);
1293 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1294     void *arg, int code);
1295 
1296 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1297 	int retry_check_flag,
1298 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1299 		int c),
1300 	void *user_arg, int failure_code,  clock_t retry_delay,
1301 	void (*statp)(kstat_io_t *));
1302 
1303 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1304 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1305 
1306 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1307 	struct scsi_pkt *pktp);
1308 static void sd_start_retry_command(void *arg);
1309 static void sd_start_direct_priority_command(void *arg);
1310 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1311 	int errcode);
1312 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1313 	struct buf *bp, int errcode);
1314 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1315 static void sd_sync_with_callback(struct sd_lun *un);
1316 static int sdrunout(caddr_t arg);
1317 
1318 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1319 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1320 
1321 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1322 static void sd_restore_throttle(void *arg);
1323 
1324 static void sd_init_cdb_limits(struct sd_lun *un);
1325 
1326 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1327 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1328 
1329 /*
1330  * Error handling functions
1331  */
1332 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1333 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1334 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1335 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1336 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1337 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1338 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1339 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1340 
1341 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1342 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1343 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1344 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1345 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1346 	struct sd_xbuf *xp);
1347 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1348 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1349 
1350 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1351 	void *arg, int code);
1352 static diskaddr_t sd_extract_sense_info_descr(
1353 	struct scsi_descr_sense_hdr *sdsp);
1354 
1355 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1356 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1357 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1358 	uint8_t asc,
1359 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1360 static void sd_sense_key_not_ready(struct sd_lun *un,
1361 	uint8_t asc, uint8_t ascq,
1362 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1363 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1364 	int sense_key, uint8_t asc,
1365 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1366 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1367 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1368 static void sd_sense_key_unit_attention(struct sd_lun *un,
1369 	uint8_t asc,
1370 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1371 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1372 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1373 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1374 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1375 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1376 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1377 static void sd_sense_key_default(struct sd_lun *un,
1378 	int sense_key,
1379 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1380 
1381 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1382 	void *arg, int flag);
1383 
1384 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1385 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1386 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1387 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1388 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1389 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1390 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1391 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1392 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1393 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1394 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1395 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1396 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1397 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1398 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1399 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1400 
1401 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1402 
1403 static void sd_start_stop_unit_callback(void *arg);
1404 static void sd_start_stop_unit_task(void *arg);
1405 
1406 static void sd_taskq_create(void);
1407 static void sd_taskq_delete(void);
1408 static void sd_media_change_task(void *arg);
1409 
1410 static int sd_handle_mchange(struct sd_lun *un);
1411 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag);
1412 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp,
1413 	uint32_t *lbap, int path_flag);
1414 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
1415 	uint32_t *lbap, int path_flag);
1416 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag,
1417 	int path_flag);
1418 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr,
1419 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1420 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag);
1421 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un,
1422 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1423 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un,
1424 	uchar_t usr_cmd, uchar_t *usr_bufp);
1425 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1426 	struct dk_callback *dkc);
1427 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1428 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un,
1429 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1430 	uchar_t *bufaddr, uint_t buflen);
1431 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
1432 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1433 	uchar_t *bufaddr, uint_t buflen, char feature);
1434 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize,
1435 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1436 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize,
1437 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1438 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
1439 	size_t buflen, daddr_t start_block, int path_flag);
1440 #define	sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag)	\
1441 	sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \
1442 	path_flag)
1443 #define	sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag)	\
1444 	sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\
1445 	path_flag)
1446 
1447 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr,
1448 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1449 	uint16_t param_ptr, int path_flag);
1450 
1451 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1452 static void sd_free_rqs(struct sd_lun *un);
1453 
1454 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1455 	uchar_t *data, int len, int fmt);
1456 static void sd_panic_for_res_conflict(struct sd_lun *un);
1457 
1458 /*
1459  * Disk Ioctl Function Prototypes
1460  */
1461 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag);
1462 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1463 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1464 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag,
1465 	int geom_validated);
1466 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag);
1467 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag,
1468 	int geom_validated);
1469 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag);
1470 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag,
1471 	int geom_validated);
1472 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag);
1473 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag);
1474 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1475 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag);
1476 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag);
1477 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1478 static int sd_write_label(dev_t dev);
1479 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl);
1480 static void sd_clear_vtoc(struct sd_lun *un);
1481 static void sd_clear_efi(struct sd_lun *un);
1482 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1483 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag);
1484 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag);
1485 static void sd_setup_default_geometry(struct sd_lun *un);
1486 #if defined(__i386) || defined(__amd64)
1487 static int sd_update_fdisk_and_vtoc(struct sd_lun *un);
1488 #endif
1489 
1490 /*
1491  * Multi-host Ioctl Prototypes
1492  */
1493 static int sd_check_mhd(dev_t dev, int interval);
1494 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1495 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1496 static char *sd_sname(uchar_t status);
1497 static void sd_mhd_resvd_recover(void *arg);
1498 static void sd_resv_reclaim_thread();
1499 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1500 static int sd_reserve_release(dev_t dev, int cmd);
1501 static void sd_rmv_resv_reclaim_req(dev_t dev);
1502 static void sd_mhd_reset_notify_cb(caddr_t arg);
1503 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1504 	mhioc_inkeys_t *usrp, int flag);
1505 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1506 	mhioc_inresvs_t *usrp, int flag);
1507 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1508 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1509 static int sd_mhdioc_release(dev_t dev);
1510 static int sd_mhdioc_register_devid(dev_t dev);
1511 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1512 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1513 
1514 /*
1515  * SCSI removable prototypes
1516  */
1517 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1518 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1519 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1520 static int sr_pause_resume(dev_t dev, int mode);
1521 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1522 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1523 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1524 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1525 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1526 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1527 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1528 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1529 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1530 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1531 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1532 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1533 static int sr_eject(dev_t dev);
1534 static void sr_ejected(register struct sd_lun *un);
1535 static int sr_check_wp(dev_t dev);
1536 static int sd_check_media(dev_t dev, enum dkio_state state);
1537 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1538 static void sd_delayed_cv_broadcast(void *arg);
1539 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1540 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1541 
1542 static int sd_log_page_supported(struct sd_lun *un, int log_page);
1543 
1544 /*
1545  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1546  */
1547 static void sd_check_for_writable_cd(struct sd_lun *un);
1548 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1549 static void sd_wm_cache_destructor(void *wm, void *un);
1550 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1551 	daddr_t endb, ushort_t typ);
1552 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1553 	daddr_t endb);
1554 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1555 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1556 static void sd_read_modify_write_task(void * arg);
1557 static int
1558 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1559 	struct buf **bpp);
1560 
1561 
1562 /*
1563  * Function prototypes for failfast support.
1564  */
1565 static void sd_failfast_flushq(struct sd_lun *un);
1566 static int sd_failfast_flushq_callback(struct buf *bp);
1567 
1568 /*
1569  * Function prototypes to check for lsi devices
1570  */
1571 static void sd_is_lsi(struct sd_lun *un);
1572 
1573 /*
1574  * Function prototypes for x86 support
1575  */
1576 #if defined(__i386) || defined(__amd64)
1577 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1578 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1579 #endif
1580 
1581 /*
1582  * Constants for failfast support:
1583  *
1584  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1585  * failfast processing being performed.
1586  *
1587  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1588  * failfast processing on all bufs with B_FAILFAST set.
1589  */
1590 
1591 #define	SD_FAILFAST_INACTIVE		0
1592 #define	SD_FAILFAST_ACTIVE		1
1593 
1594 /*
1595  * Bitmask to control behavior of buf(9S) flushes when a transition to
1596  * the failfast state occurs. Optional bits include:
1597  *
1598  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1599  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1600  * be flushed.
1601  *
1602  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1603  * driver, in addition to the regular wait queue. This includes the xbuf
1604  * queues. When clear, only the driver's wait queue will be flushed.
1605  */
1606 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1607 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1608 
1609 /*
1610  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1611  * to flush all queues within the driver.
1612  */
1613 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1614 
1615 
1616 /*
1617  * SD Testing Fault Injection
1618  */
1619 #ifdef SD_FAULT_INJECTION
1620 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1621 static void sd_faultinjection(struct scsi_pkt *pktp);
1622 static void sd_injection_log(char *buf, struct sd_lun *un);
1623 #endif
1624 
1625 /*
1626  * Device driver ops vector
1627  */
1628 static struct cb_ops sd_cb_ops = {
1629 	sdopen,			/* open */
1630 	sdclose,		/* close */
1631 	sdstrategy,		/* strategy */
1632 	nodev,			/* print */
1633 	sddump,			/* dump */
1634 	sdread,			/* read */
1635 	sdwrite,		/* write */
1636 	sdioctl,		/* ioctl */
1637 	nodev,			/* devmap */
1638 	nodev,			/* mmap */
1639 	nodev,			/* segmap */
1640 	nochpoll,		/* poll */
1641 	sd_prop_op,		/* cb_prop_op */
1642 	0,			/* streamtab  */
1643 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1644 	CB_REV,			/* cb_rev */
1645 	sdaread, 		/* async I/O read entry point */
1646 	sdawrite		/* async I/O write entry point */
1647 };
1648 
1649 static struct dev_ops sd_ops = {
1650 	DEVO_REV,		/* devo_rev, */
1651 	0,			/* refcnt  */
1652 	sdinfo,			/* info */
1653 	nulldev,		/* identify */
1654 	sdprobe,		/* probe */
1655 	sdattach,		/* attach */
1656 	sddetach,		/* detach */
1657 	nodev,			/* reset */
1658 	&sd_cb_ops,		/* driver operations */
1659 	NULL,			/* bus operations */
1660 	sdpower			/* power */
1661 };
1662 
1663 
1664 /*
1665  * This is the loadable module wrapper.
1666  */
1667 #include <sys/modctl.h>
1668 
1669 static struct modldrv modldrv = {
1670 	&mod_driverops,		/* Type of module. This one is a driver */
1671 	SD_MODULE_NAME,		/* Module name. */
1672 	&sd_ops			/* driver ops */
1673 };
1674 
1675 
1676 static struct modlinkage modlinkage = {
1677 	MODREV_1,
1678 	&modldrv,
1679 	NULL
1680 };
1681 
1682 
1683 static struct scsi_asq_key_strings sd_additional_codes[] = {
1684 	0x81, 0, "Logical Unit is Reserved",
1685 	0x85, 0, "Audio Address Not Valid",
1686 	0xb6, 0, "Media Load Mechanism Failed",
1687 	0xB9, 0, "Audio Play Operation Aborted",
1688 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1689 	0x53, 2, "Medium removal prevented",
1690 	0x6f, 0, "Authentication failed during key exchange",
1691 	0x6f, 1, "Key not present",
1692 	0x6f, 2, "Key not established",
1693 	0x6f, 3, "Read without proper authentication",
1694 	0x6f, 4, "Mismatched region to this logical unit",
1695 	0x6f, 5, "Region reset count error",
1696 	0xffff, 0x0, NULL
1697 };
1698 
1699 
1700 /*
1701  * Struct for passing printing information for sense data messages
1702  */
1703 struct sd_sense_info {
1704 	int	ssi_severity;
1705 	int	ssi_pfa_flag;
1706 };
1707 
1708 /*
1709  * Table of function pointers for iostart-side routines. Seperate "chains"
1710  * of layered function calls are formed by placing the function pointers
1711  * sequentially in the desired order. Functions are called according to an
1712  * incrementing table index ordering. The last function in each chain must
1713  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1714  * in the sd_iodone_chain[] array.
1715  *
1716  * Note: It may seem more natural to organize both the iostart and iodone
1717  * functions together, into an array of structures (or some similar
1718  * organization) with a common index, rather than two seperate arrays which
1719  * must be maintained in synchronization. The purpose of this division is
1720  * to achiece improved performance: individual arrays allows for more
1721  * effective cache line utilization on certain platforms.
1722  */
1723 
1724 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1725 
1726 
1727 static sd_chain_t sd_iostart_chain[] = {
1728 
1729 	/* Chain for buf IO for disk drive targets (PM enabled) */
1730 	sd_mapblockaddr_iostart,	/* Index: 0 */
1731 	sd_pm_iostart,			/* Index: 1 */
1732 	sd_core_iostart,		/* Index: 2 */
1733 
1734 	/* Chain for buf IO for disk drive targets (PM disabled) */
1735 	sd_mapblockaddr_iostart,	/* Index: 3 */
1736 	sd_core_iostart,		/* Index: 4 */
1737 
1738 	/* Chain for buf IO for removable-media targets (PM enabled) */
1739 	sd_mapblockaddr_iostart,	/* Index: 5 */
1740 	sd_mapblocksize_iostart,	/* Index: 6 */
1741 	sd_pm_iostart,			/* Index: 7 */
1742 	sd_core_iostart,		/* Index: 8 */
1743 
1744 	/* Chain for buf IO for removable-media targets (PM disabled) */
1745 	sd_mapblockaddr_iostart,	/* Index: 9 */
1746 	sd_mapblocksize_iostart,	/* Index: 10 */
1747 	sd_core_iostart,		/* Index: 11 */
1748 
1749 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1750 	sd_mapblockaddr_iostart,	/* Index: 12 */
1751 	sd_checksum_iostart,		/* Index: 13 */
1752 	sd_pm_iostart,			/* Index: 14 */
1753 	sd_core_iostart,		/* Index: 15 */
1754 
1755 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1756 	sd_mapblockaddr_iostart,	/* Index: 16 */
1757 	sd_checksum_iostart,		/* Index: 17 */
1758 	sd_core_iostart,		/* Index: 18 */
1759 
1760 	/* Chain for USCSI commands (all targets) */
1761 	sd_pm_iostart,			/* Index: 19 */
1762 	sd_core_iostart,		/* Index: 20 */
1763 
1764 	/* Chain for checksumming USCSI commands (all targets) */
1765 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1766 	sd_pm_iostart,			/* Index: 22 */
1767 	sd_core_iostart,		/* Index: 23 */
1768 
1769 	/* Chain for "direct" USCSI commands (all targets) */
1770 	sd_core_iostart,		/* Index: 24 */
1771 
1772 	/* Chain for "direct priority" USCSI commands (all targets) */
1773 	sd_core_iostart,		/* Index: 25 */
1774 };
1775 
1776 /*
1777  * Macros to locate the first function of each iostart chain in the
1778  * sd_iostart_chain[] array. These are located by the index in the array.
1779  */
1780 #define	SD_CHAIN_DISK_IOSTART			0
1781 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1782 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1783 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1784 #define	SD_CHAIN_CHKSUM_IOSTART			12
1785 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1786 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1787 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1788 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1789 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1790 
1791 
1792 /*
1793  * Table of function pointers for the iodone-side routines for the driver-
1794  * internal layering mechanism.  The calling sequence for iodone routines
1795  * uses a decrementing table index, so the last routine called in a chain
1796  * must be at the lowest array index location for that chain.  The last
1797  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1798  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1799  * of the functions in an iodone side chain must correspond to the ordering
1800  * of the iostart routines for that chain.  Note that there is no iodone
1801  * side routine that corresponds to sd_core_iostart(), so there is no
1802  * entry in the table for this.
1803  */
1804 
1805 static sd_chain_t sd_iodone_chain[] = {
1806 
1807 	/* Chain for buf IO for disk drive targets (PM enabled) */
1808 	sd_buf_iodone,			/* Index: 0 */
1809 	sd_mapblockaddr_iodone,		/* Index: 1 */
1810 	sd_pm_iodone,			/* Index: 2 */
1811 
1812 	/* Chain for buf IO for disk drive targets (PM disabled) */
1813 	sd_buf_iodone,			/* Index: 3 */
1814 	sd_mapblockaddr_iodone,		/* Index: 4 */
1815 
1816 	/* Chain for buf IO for removable-media targets (PM enabled) */
1817 	sd_buf_iodone,			/* Index: 5 */
1818 	sd_mapblockaddr_iodone,		/* Index: 6 */
1819 	sd_mapblocksize_iodone,		/* Index: 7 */
1820 	sd_pm_iodone,			/* Index: 8 */
1821 
1822 	/* Chain for buf IO for removable-media targets (PM disabled) */
1823 	sd_buf_iodone,			/* Index: 9 */
1824 	sd_mapblockaddr_iodone,		/* Index: 10 */
1825 	sd_mapblocksize_iodone,		/* Index: 11 */
1826 
1827 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1828 	sd_buf_iodone,			/* Index: 12 */
1829 	sd_mapblockaddr_iodone,		/* Index: 13 */
1830 	sd_checksum_iodone,		/* Index: 14 */
1831 	sd_pm_iodone,			/* Index: 15 */
1832 
1833 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1834 	sd_buf_iodone,			/* Index: 16 */
1835 	sd_mapblockaddr_iodone,		/* Index: 17 */
1836 	sd_checksum_iodone,		/* Index: 18 */
1837 
1838 	/* Chain for USCSI commands (non-checksum targets) */
1839 	sd_uscsi_iodone,		/* Index: 19 */
1840 	sd_pm_iodone,			/* Index: 20 */
1841 
1842 	/* Chain for USCSI commands (checksum targets) */
1843 	sd_uscsi_iodone,		/* Index: 21 */
1844 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1845 	sd_pm_iodone,			/* Index: 22 */
1846 
1847 	/* Chain for "direct" USCSI commands (all targets) */
1848 	sd_uscsi_iodone,		/* Index: 24 */
1849 
1850 	/* Chain for "direct priority" USCSI commands (all targets) */
1851 	sd_uscsi_iodone,		/* Index: 25 */
1852 };
1853 
1854 
1855 /*
1856  * Macros to locate the "first" function in the sd_iodone_chain[] array for
1857  * each iodone-side chain. These are located by the array index, but as the
1858  * iodone side functions are called in a decrementing-index order, the
1859  * highest index number in each chain must be specified (as these correspond
1860  * to the first function in the iodone chain that will be called by the core
1861  * at IO completion time).
1862  */
1863 
1864 #define	SD_CHAIN_DISK_IODONE			2
1865 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
1866 #define	SD_CHAIN_RMMEDIA_IODONE			8
1867 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
1868 #define	SD_CHAIN_CHKSUM_IODONE			15
1869 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
1870 #define	SD_CHAIN_USCSI_CMD_IODONE		20
1871 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
1872 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
1873 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
1874 
1875 
1876 
1877 
1878 /*
1879  * Array to map a layering chain index to the appropriate initpkt routine.
1880  * The redundant entries are present so that the index used for accessing
1881  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1882  * with this table as well.
1883  */
1884 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
1885 
1886 static sd_initpkt_t	sd_initpkt_map[] = {
1887 
1888 	/* Chain for buf IO for disk drive targets (PM enabled) */
1889 	sd_initpkt_for_buf,		/* Index: 0 */
1890 	sd_initpkt_for_buf,		/* Index: 1 */
1891 	sd_initpkt_for_buf,		/* Index: 2 */
1892 
1893 	/* Chain for buf IO for disk drive targets (PM disabled) */
1894 	sd_initpkt_for_buf,		/* Index: 3 */
1895 	sd_initpkt_for_buf,		/* Index: 4 */
1896 
1897 	/* Chain for buf IO for removable-media targets (PM enabled) */
1898 	sd_initpkt_for_buf,		/* Index: 5 */
1899 	sd_initpkt_for_buf,		/* Index: 6 */
1900 	sd_initpkt_for_buf,		/* Index: 7 */
1901 	sd_initpkt_for_buf,		/* Index: 8 */
1902 
1903 	/* Chain for buf IO for removable-media targets (PM disabled) */
1904 	sd_initpkt_for_buf,		/* Index: 9 */
1905 	sd_initpkt_for_buf,		/* Index: 10 */
1906 	sd_initpkt_for_buf,		/* Index: 11 */
1907 
1908 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1909 	sd_initpkt_for_buf,		/* Index: 12 */
1910 	sd_initpkt_for_buf,		/* Index: 13 */
1911 	sd_initpkt_for_buf,		/* Index: 14 */
1912 	sd_initpkt_for_buf,		/* Index: 15 */
1913 
1914 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1915 	sd_initpkt_for_buf,		/* Index: 16 */
1916 	sd_initpkt_for_buf,		/* Index: 17 */
1917 	sd_initpkt_for_buf,		/* Index: 18 */
1918 
1919 	/* Chain for USCSI commands (non-checksum targets) */
1920 	sd_initpkt_for_uscsi,		/* Index: 19 */
1921 	sd_initpkt_for_uscsi,		/* Index: 20 */
1922 
1923 	/* Chain for USCSI commands (checksum targets) */
1924 	sd_initpkt_for_uscsi,		/* Index: 21 */
1925 	sd_initpkt_for_uscsi,		/* Index: 22 */
1926 	sd_initpkt_for_uscsi,		/* Index: 22 */
1927 
1928 	/* Chain for "direct" USCSI commands (all targets) */
1929 	sd_initpkt_for_uscsi,		/* Index: 24 */
1930 
1931 	/* Chain for "direct priority" USCSI commands (all targets) */
1932 	sd_initpkt_for_uscsi,		/* Index: 25 */
1933 
1934 };
1935 
1936 
1937 /*
1938  * Array to map a layering chain index to the appropriate destroypktpkt routine.
1939  * The redundant entries are present so that the index used for accessing
1940  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1941  * with this table as well.
1942  */
1943 typedef void (*sd_destroypkt_t)(struct buf *);
1944 
1945 static sd_destroypkt_t	sd_destroypkt_map[] = {
1946 
1947 	/* Chain for buf IO for disk drive targets (PM enabled) */
1948 	sd_destroypkt_for_buf,		/* Index: 0 */
1949 	sd_destroypkt_for_buf,		/* Index: 1 */
1950 	sd_destroypkt_for_buf,		/* Index: 2 */
1951 
1952 	/* Chain for buf IO for disk drive targets (PM disabled) */
1953 	sd_destroypkt_for_buf,		/* Index: 3 */
1954 	sd_destroypkt_for_buf,		/* Index: 4 */
1955 
1956 	/* Chain for buf IO for removable-media targets (PM enabled) */
1957 	sd_destroypkt_for_buf,		/* Index: 5 */
1958 	sd_destroypkt_for_buf,		/* Index: 6 */
1959 	sd_destroypkt_for_buf,		/* Index: 7 */
1960 	sd_destroypkt_for_buf,		/* Index: 8 */
1961 
1962 	/* Chain for buf IO for removable-media targets (PM disabled) */
1963 	sd_destroypkt_for_buf,		/* Index: 9 */
1964 	sd_destroypkt_for_buf,		/* Index: 10 */
1965 	sd_destroypkt_for_buf,		/* Index: 11 */
1966 
1967 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1968 	sd_destroypkt_for_buf,		/* Index: 12 */
1969 	sd_destroypkt_for_buf,		/* Index: 13 */
1970 	sd_destroypkt_for_buf,		/* Index: 14 */
1971 	sd_destroypkt_for_buf,		/* Index: 15 */
1972 
1973 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1974 	sd_destroypkt_for_buf,		/* Index: 16 */
1975 	sd_destroypkt_for_buf,		/* Index: 17 */
1976 	sd_destroypkt_for_buf,		/* Index: 18 */
1977 
1978 	/* Chain for USCSI commands (non-checksum targets) */
1979 	sd_destroypkt_for_uscsi,	/* Index: 19 */
1980 	sd_destroypkt_for_uscsi,	/* Index: 20 */
1981 
1982 	/* Chain for USCSI commands (checksum targets) */
1983 	sd_destroypkt_for_uscsi,	/* Index: 21 */
1984 	sd_destroypkt_for_uscsi,	/* Index: 22 */
1985 	sd_destroypkt_for_uscsi,	/* Index: 22 */
1986 
1987 	/* Chain for "direct" USCSI commands (all targets) */
1988 	sd_destroypkt_for_uscsi,	/* Index: 24 */
1989 
1990 	/* Chain for "direct priority" USCSI commands (all targets) */
1991 	sd_destroypkt_for_uscsi,	/* Index: 25 */
1992 
1993 };
1994 
1995 
1996 
1997 /*
1998  * Array to map a layering chain index to the appropriate chain "type".
1999  * The chain type indicates a specific property/usage of the chain.
2000  * The redundant entries are present so that the index used for accessing
2001  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2002  * with this table as well.
2003  */
2004 
2005 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2006 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2007 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2008 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2009 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2010 						/* (for error recovery) */
2011 
2012 static int sd_chain_type_map[] = {
2013 
2014 	/* Chain for buf IO for disk drive targets (PM enabled) */
2015 	SD_CHAIN_BUFIO,			/* Index: 0 */
2016 	SD_CHAIN_BUFIO,			/* Index: 1 */
2017 	SD_CHAIN_BUFIO,			/* Index: 2 */
2018 
2019 	/* Chain for buf IO for disk drive targets (PM disabled) */
2020 	SD_CHAIN_BUFIO,			/* Index: 3 */
2021 	SD_CHAIN_BUFIO,			/* Index: 4 */
2022 
2023 	/* Chain for buf IO for removable-media targets (PM enabled) */
2024 	SD_CHAIN_BUFIO,			/* Index: 5 */
2025 	SD_CHAIN_BUFIO,			/* Index: 6 */
2026 	SD_CHAIN_BUFIO,			/* Index: 7 */
2027 	SD_CHAIN_BUFIO,			/* Index: 8 */
2028 
2029 	/* Chain for buf IO for removable-media targets (PM disabled) */
2030 	SD_CHAIN_BUFIO,			/* Index: 9 */
2031 	SD_CHAIN_BUFIO,			/* Index: 10 */
2032 	SD_CHAIN_BUFIO,			/* Index: 11 */
2033 
2034 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2035 	SD_CHAIN_BUFIO,			/* Index: 12 */
2036 	SD_CHAIN_BUFIO,			/* Index: 13 */
2037 	SD_CHAIN_BUFIO,			/* Index: 14 */
2038 	SD_CHAIN_BUFIO,			/* Index: 15 */
2039 
2040 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2041 	SD_CHAIN_BUFIO,			/* Index: 16 */
2042 	SD_CHAIN_BUFIO,			/* Index: 17 */
2043 	SD_CHAIN_BUFIO,			/* Index: 18 */
2044 
2045 	/* Chain for USCSI commands (non-checksum targets) */
2046 	SD_CHAIN_USCSI,			/* Index: 19 */
2047 	SD_CHAIN_USCSI,			/* Index: 20 */
2048 
2049 	/* Chain for USCSI commands (checksum targets) */
2050 	SD_CHAIN_USCSI,			/* Index: 21 */
2051 	SD_CHAIN_USCSI,			/* Index: 22 */
2052 	SD_CHAIN_USCSI,			/* Index: 22 */
2053 
2054 	/* Chain for "direct" USCSI commands (all targets) */
2055 	SD_CHAIN_DIRECT,		/* Index: 24 */
2056 
2057 	/* Chain for "direct priority" USCSI commands (all targets) */
2058 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2059 };
2060 
2061 
2062 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2063 #define	SD_IS_BUFIO(xp)			\
2064 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2065 
2066 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2067 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2068 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2069 
2070 
2071 
2072 /*
2073  * Struct, array, and macros to map a specific chain to the appropriate
2074  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2075  *
2076  * The sd_chain_index_map[] array is used at attach time to set the various
2077  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2078  * chain to be used with the instance. This allows different instances to use
2079  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2080  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2081  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2082  * dynamically & without the use of locking; and (2) a layer may update the
2083  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2084  * to allow for deferred processing of an IO within the same chain from a
2085  * different execution context.
2086  */
2087 
2088 struct sd_chain_index {
2089 	int	sci_iostart_index;
2090 	int	sci_iodone_index;
2091 };
2092 
2093 static struct sd_chain_index	sd_chain_index_map[] = {
2094 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2095 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2096 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2097 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2098 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2099 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2100 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2101 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2102 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2103 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2104 };
2105 
2106 
2107 /*
2108  * The following are indexes into the sd_chain_index_map[] array.
2109  */
2110 
2111 /* un->un_buf_chain_type must be set to one of these */
2112 #define	SD_CHAIN_INFO_DISK		0
2113 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2114 #define	SD_CHAIN_INFO_RMMEDIA		2
2115 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2116 #define	SD_CHAIN_INFO_CHKSUM		4
2117 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2118 
2119 /* un->un_uscsi_chain_type must be set to one of these */
2120 #define	SD_CHAIN_INFO_USCSI_CMD		6
2121 /* USCSI with PM disabled is the same as DIRECT */
2122 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2123 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2124 
2125 /* un->un_direct_chain_type must be set to one of these */
2126 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2127 
2128 /* un->un_priority_chain_type must be set to one of these */
2129 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2130 
2131 /* size for devid inquiries */
2132 #define	MAX_INQUIRY_SIZE		0xF0
2133 
2134 /*
2135  * Macros used by functions to pass a given buf(9S) struct along to the
2136  * next function in the layering chain for further processing.
2137  *
2138  * In the following macros, passing more than three arguments to the called
2139  * routines causes the optimizer for the SPARC compiler to stop doing tail
2140  * call elimination which results in significant performance degradation.
2141  */
2142 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2143 	((*(sd_iostart_chain[index]))(index, un, bp))
2144 
2145 #define	SD_BEGIN_IODONE(index, un, bp)	\
2146 	((*(sd_iodone_chain[index]))(index, un, bp))
2147 
2148 #define	SD_NEXT_IOSTART(index, un, bp)				\
2149 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2150 
2151 #define	SD_NEXT_IODONE(index, un, bp)				\
2152 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2153 
2154 /*
2155  *    Function: _init
2156  *
2157  * Description: This is the driver _init(9E) entry point.
2158  *
2159  * Return Code: Returns the value from mod_install(9F) or
2160  *		ddi_soft_state_init(9F) as appropriate.
2161  *
2162  *     Context: Called when driver module loaded.
2163  */
2164 
2165 int
2166 _init(void)
2167 {
2168 	int	err;
2169 
2170 	/* establish driver name from module name */
2171 	sd_label = mod_modname(&modlinkage);
2172 
2173 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2174 		SD_MAXUNIT);
2175 
2176 	if (err != 0) {
2177 		return (err);
2178 	}
2179 
2180 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2181 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2182 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2183 
2184 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2185 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2186 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2187 
2188 	/*
2189 	 * it's ok to init here even for fibre device
2190 	 */
2191 	sd_scsi_probe_cache_init();
2192 
2193 	/*
2194 	 * Creating taskq before mod_install ensures that all callers (threads)
2195 	 * that enter the module after a successfull mod_install encounter
2196 	 * a valid taskq.
2197 	 */
2198 	sd_taskq_create();
2199 
2200 	err = mod_install(&modlinkage);
2201 	if (err != 0) {
2202 		/* delete taskq if install fails */
2203 		sd_taskq_delete();
2204 
2205 		mutex_destroy(&sd_detach_mutex);
2206 		mutex_destroy(&sd_log_mutex);
2207 		mutex_destroy(&sd_label_mutex);
2208 
2209 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2210 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2211 		cv_destroy(&sd_tr.srq_inprocess_cv);
2212 
2213 		sd_scsi_probe_cache_fini();
2214 
2215 		ddi_soft_state_fini(&sd_state);
2216 		return (err);
2217 	}
2218 
2219 	return (err);
2220 }
2221 
2222 
2223 /*
2224  *    Function: _fini
2225  *
2226  * Description: This is the driver _fini(9E) entry point.
2227  *
2228  * Return Code: Returns the value from mod_remove(9F)
2229  *
2230  *     Context: Called when driver module is unloaded.
2231  */
2232 
2233 int
2234 _fini(void)
2235 {
2236 	int err;
2237 
2238 	if ((err = mod_remove(&modlinkage)) != 0) {
2239 		return (err);
2240 	}
2241 
2242 	sd_taskq_delete();
2243 
2244 	mutex_destroy(&sd_detach_mutex);
2245 	mutex_destroy(&sd_log_mutex);
2246 	mutex_destroy(&sd_label_mutex);
2247 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2248 
2249 	sd_scsi_probe_cache_fini();
2250 
2251 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2252 	cv_destroy(&sd_tr.srq_inprocess_cv);
2253 
2254 	ddi_soft_state_fini(&sd_state);
2255 
2256 	return (err);
2257 }
2258 
2259 
2260 /*
2261  *    Function: _info
2262  *
2263  * Description: This is the driver _info(9E) entry point.
2264  *
2265  *   Arguments: modinfop - pointer to the driver modinfo structure
2266  *
2267  * Return Code: Returns the value from mod_info(9F).
2268  *
2269  *     Context: Kernel thread context
2270  */
2271 
2272 int
2273 _info(struct modinfo *modinfop)
2274 {
2275 	return (mod_info(&modlinkage, modinfop));
2276 }
2277 
2278 
2279 /*
2280  * The following routines implement the driver message logging facility.
2281  * They provide component- and level- based debug output filtering.
2282  * Output may also be restricted to messages for a single instance by
2283  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2284  * to NULL, then messages for all instances are printed.
2285  *
2286  * These routines have been cloned from each other due to the language
2287  * constraints of macros and variable argument list processing.
2288  */
2289 
2290 
2291 /*
2292  *    Function: sd_log_err
2293  *
2294  * Description: This routine is called by the SD_ERROR macro for debug
2295  *		logging of error conditions.
2296  *
2297  *   Arguments: comp - driver component being logged
2298  *		dev  - pointer to driver info structure
2299  *		fmt  - error string and format to be logged
2300  */
2301 
2302 static void
2303 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2304 {
2305 	va_list		ap;
2306 	dev_info_t	*dev;
2307 
2308 	ASSERT(un != NULL);
2309 	dev = SD_DEVINFO(un);
2310 	ASSERT(dev != NULL);
2311 
2312 	/*
2313 	 * Filter messages based on the global component and level masks.
2314 	 * Also print if un matches the value of sd_debug_un, or if
2315 	 * sd_debug_un is set to NULL.
2316 	 */
2317 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2318 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2319 		mutex_enter(&sd_log_mutex);
2320 		va_start(ap, fmt);
2321 		(void) vsprintf(sd_log_buf, fmt, ap);
2322 		va_end(ap);
2323 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2324 		mutex_exit(&sd_log_mutex);
2325 	}
2326 #ifdef SD_FAULT_INJECTION
2327 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2328 	if (un->sd_injection_mask & comp) {
2329 		mutex_enter(&sd_log_mutex);
2330 		va_start(ap, fmt);
2331 		(void) vsprintf(sd_log_buf, fmt, ap);
2332 		va_end(ap);
2333 		sd_injection_log(sd_log_buf, un);
2334 		mutex_exit(&sd_log_mutex);
2335 	}
2336 #endif
2337 }
2338 
2339 
2340 /*
2341  *    Function: sd_log_info
2342  *
2343  * Description: This routine is called by the SD_INFO macro for debug
2344  *		logging of general purpose informational conditions.
2345  *
2346  *   Arguments: comp - driver component being logged
2347  *		dev  - pointer to driver info structure
2348  *		fmt  - info string and format to be logged
2349  */
2350 
2351 static void
2352 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2353 {
2354 	va_list		ap;
2355 	dev_info_t	*dev;
2356 
2357 	ASSERT(un != NULL);
2358 	dev = SD_DEVINFO(un);
2359 	ASSERT(dev != NULL);
2360 
2361 	/*
2362 	 * Filter messages based on the global component and level masks.
2363 	 * Also print if un matches the value of sd_debug_un, or if
2364 	 * sd_debug_un is set to NULL.
2365 	 */
2366 	if ((sd_component_mask & component) &&
2367 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2368 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2369 		mutex_enter(&sd_log_mutex);
2370 		va_start(ap, fmt);
2371 		(void) vsprintf(sd_log_buf, fmt, ap);
2372 		va_end(ap);
2373 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2374 		mutex_exit(&sd_log_mutex);
2375 	}
2376 #ifdef SD_FAULT_INJECTION
2377 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2378 	if (un->sd_injection_mask & component) {
2379 		mutex_enter(&sd_log_mutex);
2380 		va_start(ap, fmt);
2381 		(void) vsprintf(sd_log_buf, fmt, ap);
2382 		va_end(ap);
2383 		sd_injection_log(sd_log_buf, un);
2384 		mutex_exit(&sd_log_mutex);
2385 	}
2386 #endif
2387 }
2388 
2389 
2390 /*
2391  *    Function: sd_log_trace
2392  *
2393  * Description: This routine is called by the SD_TRACE macro for debug
2394  *		logging of trace conditions (i.e. function entry/exit).
2395  *
2396  *   Arguments: comp - driver component being logged
2397  *		dev  - pointer to driver info structure
2398  *		fmt  - trace string and format to be logged
2399  */
2400 
2401 static void
2402 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2403 {
2404 	va_list		ap;
2405 	dev_info_t	*dev;
2406 
2407 	ASSERT(un != NULL);
2408 	dev = SD_DEVINFO(un);
2409 	ASSERT(dev != NULL);
2410 
2411 	/*
2412 	 * Filter messages based on the global component and level masks.
2413 	 * Also print if un matches the value of sd_debug_un, or if
2414 	 * sd_debug_un is set to NULL.
2415 	 */
2416 	if ((sd_component_mask & component) &&
2417 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2418 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2419 		mutex_enter(&sd_log_mutex);
2420 		va_start(ap, fmt);
2421 		(void) vsprintf(sd_log_buf, fmt, ap);
2422 		va_end(ap);
2423 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2424 		mutex_exit(&sd_log_mutex);
2425 	}
2426 #ifdef SD_FAULT_INJECTION
2427 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2428 	if (un->sd_injection_mask & component) {
2429 		mutex_enter(&sd_log_mutex);
2430 		va_start(ap, fmt);
2431 		(void) vsprintf(sd_log_buf, fmt, ap);
2432 		va_end(ap);
2433 		sd_injection_log(sd_log_buf, un);
2434 		mutex_exit(&sd_log_mutex);
2435 	}
2436 #endif
2437 }
2438 
2439 
2440 /*
2441  *    Function: sdprobe
2442  *
2443  * Description: This is the driver probe(9e) entry point function.
2444  *
2445  *   Arguments: devi - opaque device info handle
2446  *
2447  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2448  *              DDI_PROBE_FAILURE: If the probe failed.
2449  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2450  *				   but may be present in the future.
2451  */
2452 
2453 static int
2454 sdprobe(dev_info_t *devi)
2455 {
2456 	struct scsi_device	*devp;
2457 	int			rval;
2458 	int			instance;
2459 
2460 	/*
2461 	 * if it wasn't for pln, sdprobe could actually be nulldev
2462 	 * in the "__fibre" case.
2463 	 */
2464 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2465 		return (DDI_PROBE_DONTCARE);
2466 	}
2467 
2468 	devp = ddi_get_driver_private(devi);
2469 
2470 	if (devp == NULL) {
2471 		/* Ooops... nexus driver is mis-configured... */
2472 		return (DDI_PROBE_FAILURE);
2473 	}
2474 
2475 	instance = ddi_get_instance(devi);
2476 
2477 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2478 		return (DDI_PROBE_PARTIAL);
2479 	}
2480 
2481 	/*
2482 	 * Call the SCSA utility probe routine to see if we actually
2483 	 * have a target at this SCSI nexus.
2484 	 */
2485 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2486 	case SCSIPROBE_EXISTS:
2487 		switch (devp->sd_inq->inq_dtype) {
2488 		case DTYPE_DIRECT:
2489 			rval = DDI_PROBE_SUCCESS;
2490 			break;
2491 		case DTYPE_RODIRECT:
2492 			/* CDs etc. Can be removable media */
2493 			rval = DDI_PROBE_SUCCESS;
2494 			break;
2495 		case DTYPE_OPTICAL:
2496 			/*
2497 			 * Rewritable optical driver HP115AA
2498 			 * Can also be removable media
2499 			 */
2500 
2501 			/*
2502 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2503 			 * pre solaris 9 sparc sd behavior is required
2504 			 *
2505 			 * If first time through and sd_dtype_optical_bind
2506 			 * has not been set in /etc/system check properties
2507 			 */
2508 
2509 			if (sd_dtype_optical_bind  < 0) {
2510 			    sd_dtype_optical_bind = ddi_prop_get_int
2511 				(DDI_DEV_T_ANY,	devi,	0,
2512 				"optical-device-bind",	1);
2513 			}
2514 
2515 			if (sd_dtype_optical_bind == 0) {
2516 				rval = DDI_PROBE_FAILURE;
2517 			} else {
2518 				rval = DDI_PROBE_SUCCESS;
2519 			}
2520 			break;
2521 
2522 		case DTYPE_NOTPRESENT:
2523 		default:
2524 			rval = DDI_PROBE_FAILURE;
2525 			break;
2526 		}
2527 		break;
2528 	default:
2529 		rval = DDI_PROBE_PARTIAL;
2530 		break;
2531 	}
2532 
2533 	/*
2534 	 * This routine checks for resource allocation prior to freeing,
2535 	 * so it will take care of the "smart probing" case where a
2536 	 * scsi_probe() may or may not have been issued and will *not*
2537 	 * free previously-freed resources.
2538 	 */
2539 	scsi_unprobe(devp);
2540 	return (rval);
2541 }
2542 
2543 
2544 /*
2545  *    Function: sdinfo
2546  *
2547  * Description: This is the driver getinfo(9e) entry point function.
2548  * 		Given the device number, return the devinfo pointer from
2549  *		the scsi_device structure or the instance number
2550  *		associated with the dev_t.
2551  *
2552  *   Arguments: dip     - pointer to device info structure
2553  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2554  *			  DDI_INFO_DEVT2INSTANCE)
2555  *		arg     - driver dev_t
2556  *		resultp - user buffer for request response
2557  *
2558  * Return Code: DDI_SUCCESS
2559  *              DDI_FAILURE
2560  */
2561 /* ARGSUSED */
2562 static int
2563 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2564 {
2565 	struct sd_lun	*un;
2566 	dev_t		dev;
2567 	int		instance;
2568 	int		error;
2569 
2570 	switch (infocmd) {
2571 	case DDI_INFO_DEVT2DEVINFO:
2572 		dev = (dev_t)arg;
2573 		instance = SDUNIT(dev);
2574 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2575 			return (DDI_FAILURE);
2576 		}
2577 		*result = (void *) SD_DEVINFO(un);
2578 		error = DDI_SUCCESS;
2579 		break;
2580 	case DDI_INFO_DEVT2INSTANCE:
2581 		dev = (dev_t)arg;
2582 		instance = SDUNIT(dev);
2583 		*result = (void *)(uintptr_t)instance;
2584 		error = DDI_SUCCESS;
2585 		break;
2586 	default:
2587 		error = DDI_FAILURE;
2588 	}
2589 	return (error);
2590 }
2591 
2592 /*
2593  *    Function: sd_prop_op
2594  *
2595  * Description: This is the driver prop_op(9e) entry point function.
2596  *		Return the number of blocks for the partition in question
2597  *		or forward the request to the property facilities.
2598  *
2599  *   Arguments: dev       - device number
2600  *		dip       - pointer to device info structure
2601  *		prop_op   - property operator
2602  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2603  *		name      - pointer to property name
2604  *		valuep    - pointer or address of the user buffer
2605  *		lengthp   - property length
2606  *
2607  * Return Code: DDI_PROP_SUCCESS
2608  *              DDI_PROP_NOT_FOUND
2609  *              DDI_PROP_UNDEFINED
2610  *              DDI_PROP_NO_MEMORY
2611  *              DDI_PROP_BUF_TOO_SMALL
2612  */
2613 
2614 static int
2615 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2616 	char *name, caddr_t valuep, int *lengthp)
2617 {
2618 	int		instance = ddi_get_instance(dip);
2619 	struct sd_lun	*un;
2620 	uint64_t	nblocks64;
2621 
2622 	/*
2623 	 * Our dynamic properties are all device specific and size oriented.
2624 	 * Requests issued under conditions where size is valid are passed
2625 	 * to ddi_prop_op_nblocks with the size information, otherwise the
2626 	 * request is passed to ddi_prop_op. Size depends on valid geometry.
2627 	 */
2628 	un = ddi_get_soft_state(sd_state, instance);
2629 	if ((dev == DDI_DEV_T_ANY) || (un == NULL) ||
2630 	    (un->un_f_geometry_is_valid == FALSE)) {
2631 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2632 		    name, valuep, lengthp));
2633 	} else {
2634 		/* get nblocks value */
2635 		ASSERT(!mutex_owned(SD_MUTEX(un)));
2636 		mutex_enter(SD_MUTEX(un));
2637 		nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk;
2638 		mutex_exit(SD_MUTEX(un));
2639 
2640 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
2641 		    name, valuep, lengthp, nblocks64));
2642 	}
2643 }
2644 
2645 /*
2646  * The following functions are for smart probing:
2647  * sd_scsi_probe_cache_init()
2648  * sd_scsi_probe_cache_fini()
2649  * sd_scsi_clear_probe_cache()
2650  * sd_scsi_probe_with_cache()
2651  */
2652 
2653 /*
2654  *    Function: sd_scsi_probe_cache_init
2655  *
2656  * Description: Initializes the probe response cache mutex and head pointer.
2657  *
2658  *     Context: Kernel thread context
2659  */
2660 
2661 static void
2662 sd_scsi_probe_cache_init(void)
2663 {
2664 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2665 	sd_scsi_probe_cache_head = NULL;
2666 }
2667 
2668 
2669 /*
2670  *    Function: sd_scsi_probe_cache_fini
2671  *
2672  * Description: Frees all resources associated with the probe response cache.
2673  *
2674  *     Context: Kernel thread context
2675  */
2676 
2677 static void
2678 sd_scsi_probe_cache_fini(void)
2679 {
2680 	struct sd_scsi_probe_cache *cp;
2681 	struct sd_scsi_probe_cache *ncp;
2682 
2683 	/* Clean up our smart probing linked list */
2684 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2685 		ncp = cp->next;
2686 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2687 	}
2688 	sd_scsi_probe_cache_head = NULL;
2689 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2690 }
2691 
2692 
2693 /*
2694  *    Function: sd_scsi_clear_probe_cache
2695  *
2696  * Description: This routine clears the probe response cache. This is
2697  *		done when open() returns ENXIO so that when deferred
2698  *		attach is attempted (possibly after a device has been
2699  *		turned on) we will retry the probe. Since we don't know
2700  *		which target we failed to open, we just clear the
2701  *		entire cache.
2702  *
2703  *     Context: Kernel thread context
2704  */
2705 
2706 static void
2707 sd_scsi_clear_probe_cache(void)
2708 {
2709 	struct sd_scsi_probe_cache	*cp;
2710 	int				i;
2711 
2712 	mutex_enter(&sd_scsi_probe_cache_mutex);
2713 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2714 		/*
2715 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2716 		 * force probing to be performed the next time
2717 		 * sd_scsi_probe_with_cache is called.
2718 		 */
2719 		for (i = 0; i < NTARGETS_WIDE; i++) {
2720 			cp->cache[i] = SCSIPROBE_EXISTS;
2721 		}
2722 	}
2723 	mutex_exit(&sd_scsi_probe_cache_mutex);
2724 }
2725 
2726 
2727 /*
2728  *    Function: sd_scsi_probe_with_cache
2729  *
2730  * Description: This routine implements support for a scsi device probe
2731  *		with cache. The driver maintains a cache of the target
2732  *		responses to scsi probes. If we get no response from a
2733  *		target during a probe inquiry, we remember that, and we
2734  *		avoid additional calls to scsi_probe on non-zero LUNs
2735  *		on the same target until the cache is cleared. By doing
2736  *		so we avoid the 1/4 sec selection timeout for nonzero
2737  *		LUNs. lun0 of a target is always probed.
2738  *
2739  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2740  *              waitfunc - indicates what the allocator routines should
2741  *			   do when resources are not available. This value
2742  *			   is passed on to scsi_probe() when that routine
2743  *			   is called.
2744  *
2745  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2746  *		otherwise the value returned by scsi_probe(9F).
2747  *
2748  *     Context: Kernel thread context
2749  */
2750 
2751 static int
2752 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2753 {
2754 	struct sd_scsi_probe_cache	*cp;
2755 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
2756 	int		lun, tgt;
2757 
2758 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2759 	    SCSI_ADDR_PROP_LUN, 0);
2760 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2761 	    SCSI_ADDR_PROP_TARGET, -1);
2762 
2763 	/* Make sure caching enabled and target in range */
2764 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2765 		/* do it the old way (no cache) */
2766 		return (scsi_probe(devp, waitfn));
2767 	}
2768 
2769 	mutex_enter(&sd_scsi_probe_cache_mutex);
2770 
2771 	/* Find the cache for this scsi bus instance */
2772 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2773 		if (cp->pdip == pdip) {
2774 			break;
2775 		}
2776 	}
2777 
2778 	/* If we can't find a cache for this pdip, create one */
2779 	if (cp == NULL) {
2780 		int i;
2781 
2782 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
2783 		    KM_SLEEP);
2784 		cp->pdip = pdip;
2785 		cp->next = sd_scsi_probe_cache_head;
2786 		sd_scsi_probe_cache_head = cp;
2787 		for (i = 0; i < NTARGETS_WIDE; i++) {
2788 			cp->cache[i] = SCSIPROBE_EXISTS;
2789 		}
2790 	}
2791 
2792 	mutex_exit(&sd_scsi_probe_cache_mutex);
2793 
2794 	/* Recompute the cache for this target if LUN zero */
2795 	if (lun == 0) {
2796 		cp->cache[tgt] = SCSIPROBE_EXISTS;
2797 	}
2798 
2799 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
2800 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
2801 		return (SCSIPROBE_NORESP);
2802 	}
2803 
2804 	/* Do the actual probe; save & return the result */
2805 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
2806 }
2807 
2808 
2809 /*
2810  *    Function: sd_spin_up_unit
2811  *
2812  * Description: Issues the following commands to spin-up the device:
2813  *		START STOP UNIT, and INQUIRY.
2814  *
2815  *   Arguments: un - driver soft state (unit) structure
2816  *
2817  * Return Code: 0 - success
2818  *		EIO - failure
2819  *		EACCES - reservation conflict
2820  *
2821  *     Context: Kernel thread context
2822  */
2823 
2824 static int
2825 sd_spin_up_unit(struct sd_lun *un)
2826 {
2827 	size_t	resid		= 0;
2828 	int	has_conflict	= FALSE;
2829 	uchar_t *bufaddr;
2830 
2831 	ASSERT(un != NULL);
2832 
2833 	/*
2834 	 * Send a throwaway START UNIT command.
2835 	 *
2836 	 * If we fail on this, we don't care presently what precisely
2837 	 * is wrong.  EMC's arrays will also fail this with a check
2838 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
2839 	 * we don't want to fail the attach because it may become
2840 	 * "active" later.
2841 	 */
2842 	if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT)
2843 	    == EACCES)
2844 		has_conflict = TRUE;
2845 
2846 	/*
2847 	 * Send another INQUIRY command to the target. This is necessary for
2848 	 * non-removable media direct access devices because their INQUIRY data
2849 	 * may not be fully qualified until they are spun up (perhaps via the
2850 	 * START command above).  Note: This seems to be needed for some
2851 	 * legacy devices only.) The INQUIRY command should succeed even if a
2852 	 * Reservation Conflict is present.
2853 	 */
2854 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
2855 	if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) {
2856 		kmem_free(bufaddr, SUN_INQSIZE);
2857 		return (EIO);
2858 	}
2859 
2860 	/*
2861 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
2862 	 * Note that this routine does not return a failure here even if the
2863 	 * INQUIRY command did not return any data.  This is a legacy behavior.
2864 	 */
2865 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
2866 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
2867 	}
2868 
2869 	kmem_free(bufaddr, SUN_INQSIZE);
2870 
2871 	/* If we hit a reservation conflict above, tell the caller. */
2872 	if (has_conflict == TRUE) {
2873 		return (EACCES);
2874 	}
2875 
2876 	return (0);
2877 }
2878 
2879 #ifdef _LP64
2880 /*
2881  *    Function: sd_enable_descr_sense
2882  *
2883  * Description: This routine attempts to select descriptor sense format
2884  *		using the Control mode page.  Devices that support 64 bit
2885  *		LBAs (for >2TB luns) should also implement descriptor
2886  *		sense data so we will call this function whenever we see
2887  *		a lun larger than 2TB.  If for some reason the device
2888  *		supports 64 bit LBAs but doesn't support descriptor sense
2889  *		presumably the mode select will fail.  Everything will
2890  *		continue to work normally except that we will not get
2891  *		complete sense data for commands that fail with an LBA
2892  *		larger than 32 bits.
2893  *
2894  *   Arguments: un - driver soft state (unit) structure
2895  *
2896  *     Context: Kernel thread context only
2897  */
2898 
2899 static void
2900 sd_enable_descr_sense(struct sd_lun *un)
2901 {
2902 	uchar_t			*header;
2903 	struct mode_control_scsi3 *ctrl_bufp;
2904 	size_t			buflen;
2905 	size_t			bd_len;
2906 
2907 	/*
2908 	 * Read MODE SENSE page 0xA, Control Mode Page
2909 	 */
2910 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
2911 	    sizeof (struct mode_control_scsi3);
2912 	header = kmem_zalloc(buflen, KM_SLEEP);
2913 	if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
2914 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) {
2915 		SD_ERROR(SD_LOG_COMMON, un,
2916 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
2917 		goto eds_exit;
2918 	}
2919 
2920 	/*
2921 	 * Determine size of Block Descriptors in order to locate
2922 	 * the mode page data. ATAPI devices return 0, SCSI devices
2923 	 * should return MODE_BLK_DESC_LENGTH.
2924 	 */
2925 	bd_len  = ((struct mode_header *)header)->bdesc_length;
2926 
2927 	ctrl_bufp = (struct mode_control_scsi3 *)
2928 	    (header + MODE_HEADER_LENGTH + bd_len);
2929 
2930 	/*
2931 	 * Clear PS bit for MODE SELECT
2932 	 */
2933 	ctrl_bufp->mode_page.ps = 0;
2934 
2935 	/*
2936 	 * Set D_SENSE to enable descriptor sense format.
2937 	 */
2938 	ctrl_bufp->d_sense = 1;
2939 
2940 	/*
2941 	 * Use MODE SELECT to commit the change to the D_SENSE bit
2942 	 */
2943 	if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
2944 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) {
2945 		SD_INFO(SD_LOG_COMMON, un,
2946 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
2947 		goto eds_exit;
2948 	}
2949 
2950 eds_exit:
2951 	kmem_free(header, buflen);
2952 }
2953 #endif /* _LP64 */
2954 
2955 
2956 /*
2957  *    Function: sd_set_mmc_caps
2958  *
2959  * Description: This routine determines if the device is MMC compliant and if
2960  *		the device supports CDDA via a mode sense of the CDVD
2961  *		capabilities mode page. Also checks if the device is a
2962  *		dvdram writable device.
2963  *
2964  *   Arguments: un - driver soft state (unit) structure
2965  *
2966  *     Context: Kernel thread context only
2967  */
2968 
2969 static void
2970 sd_set_mmc_caps(struct sd_lun *un)
2971 {
2972 	struct mode_header_grp2		*sense_mhp;
2973 	uchar_t				*sense_page;
2974 	caddr_t				buf;
2975 	int				bd_len;
2976 	int				status;
2977 	struct uscsi_cmd		com;
2978 	int				rtn;
2979 	uchar_t				*out_data_rw, *out_data_hd;
2980 	uchar_t				*rqbuf_rw, *rqbuf_hd;
2981 
2982 	ASSERT(un != NULL);
2983 
2984 	/*
2985 	 * The flags which will be set in this function are - mmc compliant,
2986 	 * dvdram writable device, cdda support. Initialize them to FALSE
2987 	 * and if a capability is detected - it will be set to TRUE.
2988 	 */
2989 	un->un_f_mmc_cap = FALSE;
2990 	un->un_f_dvdram_writable_device = FALSE;
2991 	un->un_f_cfg_cdda = FALSE;
2992 
2993 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
2994 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
2995 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
2996 
2997 	if (status != 0) {
2998 		/* command failed; just return */
2999 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3000 		return;
3001 	}
3002 	/*
3003 	 * If the mode sense request for the CDROM CAPABILITIES
3004 	 * page (0x2A) succeeds the device is assumed to be MMC.
3005 	 */
3006 	un->un_f_mmc_cap = TRUE;
3007 
3008 	/* Get to the page data */
3009 	sense_mhp = (struct mode_header_grp2 *)buf;
3010 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3011 	    sense_mhp->bdesc_length_lo;
3012 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3013 		/*
3014 		 * We did not get back the expected block descriptor
3015 		 * length so we cannot determine if the device supports
3016 		 * CDDA. However, we still indicate the device is MMC
3017 		 * according to the successful response to the page
3018 		 * 0x2A mode sense request.
3019 		 */
3020 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3021 		    "sd_set_mmc_caps: Mode Sense returned "
3022 		    "invalid block descriptor length\n");
3023 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3024 		return;
3025 	}
3026 
3027 	/* See if read CDDA is supported */
3028 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3029 	    bd_len);
3030 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3031 
3032 	/* See if writing DVD RAM is supported. */
3033 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3034 	if (un->un_f_dvdram_writable_device == TRUE) {
3035 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3036 		return;
3037 	}
3038 
3039 	/*
3040 	 * If the device presents DVD or CD capabilities in the mode
3041 	 * page, we can return here since a RRD will not have
3042 	 * these capabilities.
3043 	 */
3044 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3045 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3046 		return;
3047 	}
3048 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3049 
3050 	/*
3051 	 * If un->un_f_dvdram_writable_device is still FALSE,
3052 	 * check for a Removable Rigid Disk (RRD).  A RRD
3053 	 * device is identified by the features RANDOM_WRITABLE and
3054 	 * HARDWARE_DEFECT_MANAGEMENT.
3055 	 */
3056 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3057 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3058 
3059 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3060 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3061 	    RANDOM_WRITABLE);
3062 	if (rtn != 0) {
3063 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3064 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3065 		return;
3066 	}
3067 
3068 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3069 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3070 
3071 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3072 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3073 	    HARDWARE_DEFECT_MANAGEMENT);
3074 	if (rtn == 0) {
3075 		/*
3076 		 * We have good information, check for random writable
3077 		 * and hardware defect features.
3078 		 */
3079 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3080 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3081 			un->un_f_dvdram_writable_device = TRUE;
3082 		}
3083 	}
3084 
3085 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3086 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3087 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3088 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3089 }
3090 
3091 /*
3092  *    Function: sd_check_for_writable_cd
3093  *
3094  * Description: This routine determines if the media in the device is
3095  *		writable or not. It uses the get configuration command (0x46)
3096  *		to determine if the media is writable
3097  *
3098  *   Arguments: un - driver soft state (unit) structure
3099  *
3100  *     Context: Never called at interrupt context.
3101  */
3102 
3103 static void
3104 sd_check_for_writable_cd(struct sd_lun *un)
3105 {
3106 	struct uscsi_cmd		com;
3107 	uchar_t				*out_data;
3108 	uchar_t				*rqbuf;
3109 	int				rtn;
3110 	uchar_t				*out_data_rw, *out_data_hd;
3111 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3112 	struct mode_header_grp2		*sense_mhp;
3113 	uchar_t				*sense_page;
3114 	caddr_t				buf;
3115 	int				bd_len;
3116 	int				status;
3117 
3118 	ASSERT(un != NULL);
3119 	ASSERT(mutex_owned(SD_MUTEX(un)));
3120 
3121 	/*
3122 	 * Initialize the writable media to false, if configuration info.
3123 	 * tells us otherwise then only we will set it.
3124 	 */
3125 	un->un_f_mmc_writable_media = FALSE;
3126 	mutex_exit(SD_MUTEX(un));
3127 
3128 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3129 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3130 
3131 	rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH,
3132 	    out_data, SD_PROFILE_HEADER_LEN);
3133 
3134 	mutex_enter(SD_MUTEX(un));
3135 	if (rtn == 0) {
3136 		/*
3137 		 * We have good information, check for writable DVD.
3138 		 */
3139 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3140 			un->un_f_mmc_writable_media = TRUE;
3141 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3142 			kmem_free(rqbuf, SENSE_LENGTH);
3143 			return;
3144 		}
3145 	}
3146 
3147 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3148 	kmem_free(rqbuf, SENSE_LENGTH);
3149 
3150 	/*
3151 	 * Determine if this is a RRD type device.
3152 	 */
3153 	mutex_exit(SD_MUTEX(un));
3154 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3155 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
3156 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3157 	mutex_enter(SD_MUTEX(un));
3158 	if (status != 0) {
3159 		/* command failed; just return */
3160 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3161 		return;
3162 	}
3163 
3164 	/* Get to the page data */
3165 	sense_mhp = (struct mode_header_grp2 *)buf;
3166 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3167 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3168 		/*
3169 		 * We did not get back the expected block descriptor length so
3170 		 * we cannot check the mode page.
3171 		 */
3172 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3173 		    "sd_check_for_writable_cd: Mode Sense returned "
3174 		    "invalid block descriptor length\n");
3175 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3176 		return;
3177 	}
3178 
3179 	/*
3180 	 * If the device presents DVD or CD capabilities in the mode
3181 	 * page, we can return here since a RRD device will not have
3182 	 * these capabilities.
3183 	 */
3184 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3185 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3186 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3187 		return;
3188 	}
3189 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3190 
3191 	/*
3192 	 * If un->un_f_mmc_writable_media is still FALSE,
3193 	 * check for RRD type media.  A RRD device is identified
3194 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3195 	 */
3196 	mutex_exit(SD_MUTEX(un));
3197 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3198 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3199 
3200 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3201 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3202 	    RANDOM_WRITABLE);
3203 	if (rtn != 0) {
3204 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3205 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3206 		mutex_enter(SD_MUTEX(un));
3207 		return;
3208 	}
3209 
3210 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3211 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3212 
3213 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3214 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3215 	    HARDWARE_DEFECT_MANAGEMENT);
3216 	mutex_enter(SD_MUTEX(un));
3217 	if (rtn == 0) {
3218 		/*
3219 		 * We have good information, check for random writable
3220 		 * and hardware defect features as current.
3221 		 */
3222 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3223 		    (out_data_rw[10] & 0x1) &&
3224 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3225 		    (out_data_hd[10] & 0x1)) {
3226 			un->un_f_mmc_writable_media = TRUE;
3227 		}
3228 	}
3229 
3230 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3231 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3232 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3233 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3234 }
3235 
3236 /*
3237  *    Function: sd_read_unit_properties
3238  *
3239  * Description: The following implements a property lookup mechanism.
3240  *		Properties for particular disks (keyed on vendor, model
3241  *		and rev numbers) are sought in the sd.conf file via
3242  *		sd_process_sdconf_file(), and if not found there, are
3243  *		looked for in a list hardcoded in this driver via
3244  *		sd_process_sdconf_table() Once located the properties
3245  *		are used to update the driver unit structure.
3246  *
3247  *   Arguments: un - driver soft state (unit) structure
3248  */
3249 
3250 static void
3251 sd_read_unit_properties(struct sd_lun *un)
3252 {
3253 	/*
3254 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3255 	 * the "sd-config-list" property (from the sd.conf file) or if
3256 	 * there was not a match for the inquiry vid/pid. If this event
3257 	 * occurs the static driver configuration table is searched for
3258 	 * a match.
3259 	 */
3260 	ASSERT(un != NULL);
3261 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3262 		sd_process_sdconf_table(un);
3263 	}
3264 
3265 	/* check for LSI device */
3266 	sd_is_lsi(un);
3267 
3268 
3269 }
3270 
3271 
3272 /*
3273  *    Function: sd_process_sdconf_file
3274  *
3275  * Description: Use ddi_getlongprop to obtain the properties from the
3276  *		driver's config file (ie, sd.conf) and update the driver
3277  *		soft state structure accordingly.
3278  *
3279  *   Arguments: un - driver soft state (unit) structure
3280  *
3281  * Return Code: SD_SUCCESS - The properties were successfully set according
3282  *			     to the driver configuration file.
3283  *		SD_FAILURE - The driver config list was not obtained or
3284  *			     there was no vid/pid match. This indicates that
3285  *			     the static config table should be used.
3286  *
3287  * The config file has a property, "sd-config-list", which consists of
3288  * one or more duplets as follows:
3289  *
3290  *  sd-config-list=
3291  *	<duplet>,
3292  *	[<duplet>,]
3293  *	[<duplet>];
3294  *
3295  * The structure of each duplet is as follows:
3296  *
3297  *  <duplet>:= <vid+pid>,<data-property-name_list>
3298  *
3299  * The first entry of the duplet is the device ID string (the concatenated
3300  * vid & pid; not to be confused with a device_id).  This is defined in
3301  * the same way as in the sd_disk_table.
3302  *
3303  * The second part of the duplet is a string that identifies a
3304  * data-property-name-list. The data-property-name-list is defined as
3305  * follows:
3306  *
3307  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3308  *
3309  * The syntax of <data-property-name> depends on the <version> field.
3310  *
3311  * If version = SD_CONF_VERSION_1 we have the following syntax:
3312  *
3313  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3314  *
3315  * where the prop0 value will be used to set prop0 if bit0 set in the
3316  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3317  *
3318  */
3319 
3320 static int
3321 sd_process_sdconf_file(struct sd_lun *un)
3322 {
3323 	char	*config_list = NULL;
3324 	int	config_list_len;
3325 	int	len;
3326 	int	dupletlen = 0;
3327 	char	*vidptr;
3328 	int	vidlen;
3329 	char	*dnlist_ptr;
3330 	char	*dataname_ptr;
3331 	int	dnlist_len;
3332 	int	dataname_len;
3333 	int	*data_list;
3334 	int	data_list_len;
3335 	int	rval = SD_FAILURE;
3336 	int	i;
3337 
3338 	ASSERT(un != NULL);
3339 
3340 	/* Obtain the configuration list associated with the .conf file */
3341 	if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS,
3342 	    sd_config_list, (caddr_t)&config_list, &config_list_len)
3343 	    != DDI_PROP_SUCCESS) {
3344 		return (SD_FAILURE);
3345 	}
3346 
3347 	/*
3348 	 * Compare vids in each duplet to the inquiry vid - if a match is
3349 	 * made, get the data value and update the soft state structure
3350 	 * accordingly.
3351 	 *
3352 	 * Note: This algorithm is complex and difficult to maintain. It should
3353 	 * be replaced with a more robust implementation.
3354 	 */
3355 	for (len = config_list_len, vidptr = config_list; len > 0;
3356 	    vidptr += dupletlen, len -= dupletlen) {
3357 		/*
3358 		 * Note: The assumption here is that each vid entry is on
3359 		 * a unique line from its associated duplet.
3360 		 */
3361 		vidlen = dupletlen = (int)strlen(vidptr);
3362 		if ((vidlen == 0) ||
3363 		    (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3364 			dupletlen++;
3365 			continue;
3366 		}
3367 
3368 		/*
3369 		 * dnlist contains 1 or more blank separated
3370 		 * data-property-name entries
3371 		 */
3372 		dnlist_ptr = vidptr + vidlen + 1;
3373 		dnlist_len = (int)strlen(dnlist_ptr);
3374 		dupletlen += dnlist_len + 2;
3375 
3376 		/*
3377 		 * Set a pointer for the first data-property-name
3378 		 * entry in the list
3379 		 */
3380 		dataname_ptr = dnlist_ptr;
3381 		dataname_len = 0;
3382 
3383 		/*
3384 		 * Loop through all data-property-name entries in the
3385 		 * data-property-name-list setting the properties for each.
3386 		 */
3387 		while (dataname_len < dnlist_len) {
3388 			int version;
3389 
3390 			/*
3391 			 * Determine the length of the current
3392 			 * data-property-name entry by indexing until a
3393 			 * blank or NULL is encountered. When the space is
3394 			 * encountered reset it to a NULL for compliance
3395 			 * with ddi_getlongprop().
3396 			 */
3397 			for (i = 0; ((dataname_ptr[i] != ' ') &&
3398 			    (dataname_ptr[i] != '\0')); i++) {
3399 				;
3400 			}
3401 
3402 			dataname_len += i;
3403 			/* If not null terminated, Make it so */
3404 			if (dataname_ptr[i] == ' ') {
3405 				dataname_ptr[i] = '\0';
3406 			}
3407 			dataname_len++;
3408 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3409 			    "sd_process_sdconf_file: disk:%s, data:%s\n",
3410 			    vidptr, dataname_ptr);
3411 
3412 			/* Get the data list */
3413 			if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0,
3414 			    dataname_ptr, (caddr_t)&data_list, &data_list_len)
3415 			    != DDI_PROP_SUCCESS) {
3416 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3417 				    "sd_process_sdconf_file: data property (%s)"
3418 				    " has no value\n", dataname_ptr);
3419 				dataname_ptr = dnlist_ptr + dataname_len;
3420 				continue;
3421 			}
3422 
3423 			version = data_list[0];
3424 
3425 			if (version == SD_CONF_VERSION_1) {
3426 				sd_tunables values;
3427 
3428 				/* Set the properties */
3429 				if (sd_chk_vers1_data(un, data_list[1],
3430 				    &data_list[2], data_list_len, dataname_ptr)
3431 				    == SD_SUCCESS) {
3432 					sd_get_tunables_from_conf(un,
3433 					    data_list[1], &data_list[2],
3434 					    &values);
3435 					sd_set_vers1_properties(un,
3436 					    data_list[1], &values);
3437 					rval = SD_SUCCESS;
3438 				} else {
3439 					rval = SD_FAILURE;
3440 				}
3441 			} else {
3442 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3443 				    "data property %s version 0x%x is invalid.",
3444 				    dataname_ptr, version);
3445 				rval = SD_FAILURE;
3446 			}
3447 			kmem_free(data_list, data_list_len);
3448 			dataname_ptr = dnlist_ptr + dataname_len;
3449 		}
3450 	}
3451 
3452 	/* free up the memory allocated by ddi_getlongprop */
3453 	if (config_list) {
3454 		kmem_free(config_list, config_list_len);
3455 	}
3456 
3457 	return (rval);
3458 }
3459 
3460 /*
3461  *    Function: sd_get_tunables_from_conf()
3462  *
3463  *
3464  *    This function reads the data list from the sd.conf file and pulls
3465  *    the values that can have numeric values as arguments and places
3466  *    the values in the apropriate sd_tunables member.
3467  *    Since the order of the data list members varies across platforms
3468  *    This function reads them from the data list in a platform specific
3469  *    order and places them into the correct sd_tunable member that is
3470  *    a consistant across all platforms.
3471  */
3472 static void
3473 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
3474     sd_tunables *values)
3475 {
3476 	int i;
3477 	int mask;
3478 
3479 	bzero(values, sizeof (sd_tunables));
3480 
3481 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3482 
3483 		mask = 1 << i;
3484 		if (mask > flags) {
3485 			break;
3486 		}
3487 
3488 		switch (mask & flags) {
3489 		case 0:	/* This mask bit not set in flags */
3490 			continue;
3491 		case SD_CONF_BSET_THROTTLE:
3492 			values->sdt_throttle = data_list[i];
3493 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3494 			    "sd_get_tunables_from_conf: throttle = %d\n",
3495 			    values->sdt_throttle);
3496 			break;
3497 		case SD_CONF_BSET_CTYPE:
3498 			values->sdt_ctype = data_list[i];
3499 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3500 			    "sd_get_tunables_from_conf: ctype = %d\n",
3501 			    values->sdt_ctype);
3502 			break;
3503 		case SD_CONF_BSET_NRR_COUNT:
3504 			values->sdt_not_rdy_retries = data_list[i];
3505 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3506 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
3507 			    values->sdt_not_rdy_retries);
3508 			break;
3509 		case SD_CONF_BSET_BSY_RETRY_COUNT:
3510 			values->sdt_busy_retries = data_list[i];
3511 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3512 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
3513 			    values->sdt_busy_retries);
3514 			break;
3515 		case SD_CONF_BSET_RST_RETRIES:
3516 			values->sdt_reset_retries = data_list[i];
3517 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3518 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
3519 			    values->sdt_reset_retries);
3520 			break;
3521 		case SD_CONF_BSET_RSV_REL_TIME:
3522 			values->sdt_reserv_rel_time = data_list[i];
3523 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3524 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
3525 			    values->sdt_reserv_rel_time);
3526 			break;
3527 		case SD_CONF_BSET_MIN_THROTTLE:
3528 			values->sdt_min_throttle = data_list[i];
3529 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3530 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
3531 			    values->sdt_min_throttle);
3532 			break;
3533 		case SD_CONF_BSET_DISKSORT_DISABLED:
3534 			values->sdt_disk_sort_dis = data_list[i];
3535 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3536 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
3537 			    values->sdt_disk_sort_dis);
3538 			break;
3539 		case SD_CONF_BSET_LUN_RESET_ENABLED:
3540 			values->sdt_lun_reset_enable = data_list[i];
3541 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3542 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
3543 			    "\n", values->sdt_lun_reset_enable);
3544 			break;
3545 		}
3546 	}
3547 }
3548 
3549 /*
3550  *    Function: sd_process_sdconf_table
3551  *
3552  * Description: Search the static configuration table for a match on the
3553  *		inquiry vid/pid and update the driver soft state structure
3554  *		according to the table property values for the device.
3555  *
3556  *		The form of a configuration table entry is:
3557  *		  <vid+pid>,<flags>,<property-data>
3558  *		  "SEAGATE ST42400N",1,63,0,0			(Fibre)
3559  *		  "SEAGATE ST42400N",1,63,0,0,0,0		(Sparc)
3560  *		  "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0	(Intel)
3561  *
3562  *   Arguments: un - driver soft state (unit) structure
3563  */
3564 
3565 static void
3566 sd_process_sdconf_table(struct sd_lun *un)
3567 {
3568 	char	*id = NULL;
3569 	int	table_index;
3570 	int	idlen;
3571 
3572 	ASSERT(un != NULL);
3573 	for (table_index = 0; table_index < sd_disk_table_size;
3574 	    table_index++) {
3575 		id = sd_disk_table[table_index].device_id;
3576 		idlen = strlen(id);
3577 		if (idlen == 0) {
3578 			continue;
3579 		}
3580 
3581 		/*
3582 		 * The static configuration table currently does not
3583 		 * implement version 10 properties. Additionally,
3584 		 * multiple data-property-name entries are not
3585 		 * implemented in the static configuration table.
3586 		 */
3587 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
3588 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3589 			    "sd_process_sdconf_table: disk %s\n", id);
3590 			sd_set_vers1_properties(un,
3591 			    sd_disk_table[table_index].flags,
3592 			    sd_disk_table[table_index].properties);
3593 			break;
3594 		}
3595 	}
3596 }
3597 
3598 
3599 /*
3600  *    Function: sd_sdconf_id_match
3601  *
3602  * Description: This local function implements a case sensitive vid/pid
3603  *		comparison as well as the boundary cases of wild card and
3604  *		multiple blanks.
3605  *
3606  *		Note: An implicit assumption made here is that the scsi
3607  *		inquiry structure will always keep the vid, pid and
3608  *		revision strings in consecutive sequence, so they can be
3609  *		read as a single string. If this assumption is not the
3610  *		case, a separate string, to be used for the check, needs
3611  *		to be built with these strings concatenated.
3612  *
3613  *   Arguments: un - driver soft state (unit) structure
3614  *		id - table or config file vid/pid
3615  *		idlen  - length of the vid/pid (bytes)
3616  *
3617  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3618  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3619  */
3620 
3621 static int
3622 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
3623 {
3624 	struct scsi_inquiry	*sd_inq;
3625 	int 			rval = SD_SUCCESS;
3626 
3627 	ASSERT(un != NULL);
3628 	sd_inq = un->un_sd->sd_inq;
3629 	ASSERT(id != NULL);
3630 
3631 	/*
3632 	 * We use the inq_vid as a pointer to a buffer containing the
3633 	 * vid and pid and use the entire vid/pid length of the table
3634 	 * entry for the comparison. This works because the inq_pid
3635 	 * data member follows inq_vid in the scsi_inquiry structure.
3636 	 */
3637 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
3638 		/*
3639 		 * The user id string is compared to the inquiry vid/pid
3640 		 * using a case insensitive comparison and ignoring
3641 		 * multiple spaces.
3642 		 */
3643 		rval = sd_blank_cmp(un, id, idlen);
3644 		if (rval != SD_SUCCESS) {
3645 			/*
3646 			 * User id strings that start and end with a "*"
3647 			 * are a special case. These do not have a
3648 			 * specific vendor, and the product string can
3649 			 * appear anywhere in the 16 byte PID portion of
3650 			 * the inquiry data. This is a simple strstr()
3651 			 * type search for the user id in the inquiry data.
3652 			 */
3653 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
3654 				char	*pidptr = &id[1];
3655 				int	i;
3656 				int	j;
3657 				int	pidstrlen = idlen - 2;
3658 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
3659 				    pidstrlen;
3660 
3661 				if (j < 0) {
3662 					return (SD_FAILURE);
3663 				}
3664 				for (i = 0; i < j; i++) {
3665 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
3666 					    pidptr, pidstrlen) == 0) {
3667 						rval = SD_SUCCESS;
3668 						break;
3669 					}
3670 				}
3671 			}
3672 		}
3673 	}
3674 	return (rval);
3675 }
3676 
3677 
3678 /*
3679  *    Function: sd_blank_cmp
3680  *
3681  * Description: If the id string starts and ends with a space, treat
3682  *		multiple consecutive spaces as equivalent to a single
3683  *		space. For example, this causes a sd_disk_table entry
3684  *		of " NEC CDROM " to match a device's id string of
3685  *		"NEC       CDROM".
3686  *
3687  *		Note: The success exit condition for this routine is if
3688  *		the pointer to the table entry is '\0' and the cnt of
3689  *		the inquiry length is zero. This will happen if the inquiry
3690  *		string returned by the device is padded with spaces to be
3691  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
3692  *		SCSI spec states that the inquiry string is to be padded with
3693  *		spaces.
3694  *
3695  *   Arguments: un - driver soft state (unit) structure
3696  *		id - table or config file vid/pid
3697  *		idlen  - length of the vid/pid (bytes)
3698  *
3699  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3700  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3701  */
3702 
3703 static int
3704 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
3705 {
3706 	char		*p1;
3707 	char		*p2;
3708 	int		cnt;
3709 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
3710 	    sizeof (SD_INQUIRY(un)->inq_pid);
3711 
3712 	ASSERT(un != NULL);
3713 	p2 = un->un_sd->sd_inq->inq_vid;
3714 	ASSERT(id != NULL);
3715 	p1 = id;
3716 
3717 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
3718 		/*
3719 		 * Note: string p1 is terminated by a NUL but string p2
3720 		 * isn't.  The end of p2 is determined by cnt.
3721 		 */
3722 		for (;;) {
3723 			/* skip over any extra blanks in both strings */
3724 			while ((*p1 != '\0') && (*p1 == ' ')) {
3725 				p1++;
3726 			}
3727 			while ((cnt != 0) && (*p2 == ' ')) {
3728 				p2++;
3729 				cnt--;
3730 			}
3731 
3732 			/* compare the two strings */
3733 			if ((cnt == 0) ||
3734 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
3735 				break;
3736 			}
3737 			while ((cnt > 0) &&
3738 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
3739 				p1++;
3740 				p2++;
3741 				cnt--;
3742 			}
3743 		}
3744 	}
3745 
3746 	/* return SD_SUCCESS if both strings match */
3747 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
3748 }
3749 
3750 
3751 /*
3752  *    Function: sd_chk_vers1_data
3753  *
3754  * Description: Verify the version 1 device properties provided by the
3755  *		user via the configuration file
3756  *
3757  *   Arguments: un	     - driver soft state (unit) structure
3758  *		flags	     - integer mask indicating properties to be set
3759  *		prop_list    - integer list of property values
3760  *		list_len     - length of user provided data
3761  *
3762  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
3763  *		SD_FAILURE - Indicates the user provided data is invalid
3764  */
3765 
3766 static int
3767 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
3768     int list_len, char *dataname_ptr)
3769 {
3770 	int i;
3771 	int mask = 1;
3772 	int index = 0;
3773 
3774 	ASSERT(un != NULL);
3775 
3776 	/* Check for a NULL property name and list */
3777 	if (dataname_ptr == NULL) {
3778 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3779 		    "sd_chk_vers1_data: NULL data property name.");
3780 		return (SD_FAILURE);
3781 	}
3782 	if (prop_list == NULL) {
3783 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3784 		    "sd_chk_vers1_data: %s NULL data property list.",
3785 		    dataname_ptr);
3786 		return (SD_FAILURE);
3787 	}
3788 
3789 	/* Display a warning if undefined bits are set in the flags */
3790 	if (flags & ~SD_CONF_BIT_MASK) {
3791 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3792 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
3793 		    "Properties not set.",
3794 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
3795 		return (SD_FAILURE);
3796 	}
3797 
3798 	/*
3799 	 * Verify the length of the list by identifying the highest bit set
3800 	 * in the flags and validating that the property list has a length
3801 	 * up to the index of this bit.
3802 	 */
3803 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3804 		if (flags & mask) {
3805 			index++;
3806 		}
3807 		mask = 1 << i;
3808 	}
3809 	if ((list_len / sizeof (int)) < (index + 2)) {
3810 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3811 		    "sd_chk_vers1_data: "
3812 		    "Data property list %s size is incorrect. "
3813 		    "Properties not set.", dataname_ptr);
3814 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
3815 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
3816 		return (SD_FAILURE);
3817 	}
3818 	return (SD_SUCCESS);
3819 }
3820 
3821 
3822 /*
3823  *    Function: sd_set_vers1_properties
3824  *
3825  * Description: Set version 1 device properties based on a property list
3826  *		retrieved from the driver configuration file or static
3827  *		configuration table. Version 1 properties have the format:
3828  *
3829  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3830  *
3831  *		where the prop0 value will be used to set prop0 if bit0
3832  *		is set in the flags
3833  *
3834  *   Arguments: un	     - driver soft state (unit) structure
3835  *		flags	     - integer mask indicating properties to be set
3836  *		prop_list    - integer list of property values
3837  */
3838 
3839 static void
3840 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
3841 {
3842 	ASSERT(un != NULL);
3843 
3844 	/*
3845 	 * Set the flag to indicate cache is to be disabled. An attempt
3846 	 * to disable the cache via sd_cache_control() will be made
3847 	 * later during attach once the basic initialization is complete.
3848 	 */
3849 	if (flags & SD_CONF_BSET_NOCACHE) {
3850 		un->un_f_opt_disable_cache = TRUE;
3851 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3852 		    "sd_set_vers1_properties: caching disabled flag set\n");
3853 	}
3854 
3855 	/* CD-specific configuration parameters */
3856 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
3857 		un->un_f_cfg_playmsf_bcd = TRUE;
3858 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3859 		    "sd_set_vers1_properties: playmsf_bcd set\n");
3860 	}
3861 	if (flags & SD_CONF_BSET_READSUB_BCD) {
3862 		un->un_f_cfg_readsub_bcd = TRUE;
3863 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3864 		    "sd_set_vers1_properties: readsub_bcd set\n");
3865 	}
3866 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
3867 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
3868 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3869 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
3870 	}
3871 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
3872 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
3873 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3874 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
3875 	}
3876 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
3877 		un->un_f_cfg_no_read_header = TRUE;
3878 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3879 			    "sd_set_vers1_properties: no_read_header set\n");
3880 	}
3881 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
3882 		un->un_f_cfg_read_cd_xd4 = TRUE;
3883 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3884 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
3885 	}
3886 
3887 	/* Support for devices which do not have valid/unique serial numbers */
3888 	if (flags & SD_CONF_BSET_FAB_DEVID) {
3889 		un->un_f_opt_fab_devid = TRUE;
3890 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3891 		    "sd_set_vers1_properties: fab_devid bit set\n");
3892 	}
3893 
3894 	/* Support for user throttle configuration */
3895 	if (flags & SD_CONF_BSET_THROTTLE) {
3896 		ASSERT(prop_list != NULL);
3897 		un->un_saved_throttle = un->un_throttle =
3898 		    prop_list->sdt_throttle;
3899 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3900 		    "sd_set_vers1_properties: throttle set to %d\n",
3901 		    prop_list->sdt_throttle);
3902 	}
3903 
3904 	/* Set the per disk retry count according to the conf file or table. */
3905 	if (flags & SD_CONF_BSET_NRR_COUNT) {
3906 		ASSERT(prop_list != NULL);
3907 		if (prop_list->sdt_not_rdy_retries) {
3908 			un->un_notready_retry_count =
3909 				prop_list->sdt_not_rdy_retries;
3910 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3911 			    "sd_set_vers1_properties: not ready retry count"
3912 			    " set to %d\n", un->un_notready_retry_count);
3913 		}
3914 	}
3915 
3916 	/* The controller type is reported for generic disk driver ioctls */
3917 	if (flags & SD_CONF_BSET_CTYPE) {
3918 		ASSERT(prop_list != NULL);
3919 		switch (prop_list->sdt_ctype) {
3920 		case CTYPE_CDROM:
3921 			un->un_ctype = prop_list->sdt_ctype;
3922 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3923 			    "sd_set_vers1_properties: ctype set to "
3924 			    "CTYPE_CDROM\n");
3925 			break;
3926 		case CTYPE_CCS:
3927 			un->un_ctype = prop_list->sdt_ctype;
3928 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3929 				"sd_set_vers1_properties: ctype set to "
3930 				"CTYPE_CCS\n");
3931 			break;
3932 		case CTYPE_ROD:		/* RW optical */
3933 			un->un_ctype = prop_list->sdt_ctype;
3934 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3935 			    "sd_set_vers1_properties: ctype set to "
3936 			    "CTYPE_ROD\n");
3937 			break;
3938 		default:
3939 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3940 			    "sd_set_vers1_properties: Could not set "
3941 			    "invalid ctype value (%d)",
3942 			    prop_list->sdt_ctype);
3943 		}
3944 	}
3945 
3946 	/* Purple failover timeout */
3947 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
3948 		ASSERT(prop_list != NULL);
3949 		un->un_busy_retry_count =
3950 			prop_list->sdt_busy_retries;
3951 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3952 		    "sd_set_vers1_properties: "
3953 		    "busy retry count set to %d\n",
3954 		    un->un_busy_retry_count);
3955 	}
3956 
3957 	/* Purple reset retry count */
3958 	if (flags & SD_CONF_BSET_RST_RETRIES) {
3959 		ASSERT(prop_list != NULL);
3960 		un->un_reset_retry_count =
3961 			prop_list->sdt_reset_retries;
3962 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3963 		    "sd_set_vers1_properties: "
3964 		    "reset retry count set to %d\n",
3965 		    un->un_reset_retry_count);
3966 	}
3967 
3968 	/* Purple reservation release timeout */
3969 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
3970 		ASSERT(prop_list != NULL);
3971 		un->un_reserve_release_time =
3972 			prop_list->sdt_reserv_rel_time;
3973 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3974 		    "sd_set_vers1_properties: "
3975 		    "reservation release timeout set to %d\n",
3976 		    un->un_reserve_release_time);
3977 	}
3978 
3979 	/*
3980 	 * Driver flag telling the driver to verify that no commands are pending
3981 	 * for a device before issuing a Test Unit Ready. This is a workaround
3982 	 * for a firmware bug in some Seagate eliteI drives.
3983 	 */
3984 	if (flags & SD_CONF_BSET_TUR_CHECK) {
3985 		un->un_f_cfg_tur_check = TRUE;
3986 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3987 		    "sd_set_vers1_properties: tur queue check set\n");
3988 	}
3989 
3990 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
3991 		un->un_min_throttle = prop_list->sdt_min_throttle;
3992 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3993 		    "sd_set_vers1_properties: min throttle set to %d\n",
3994 		    un->un_min_throttle);
3995 	}
3996 
3997 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
3998 		un->un_f_disksort_disabled =
3999 		    (prop_list->sdt_disk_sort_dis != 0) ?
4000 		    TRUE : FALSE;
4001 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4002 		    "sd_set_vers1_properties: disksort disabled "
4003 		    "flag set to %d\n",
4004 		    prop_list->sdt_disk_sort_dis);
4005 	}
4006 
4007 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4008 		un->un_f_lun_reset_enabled =
4009 		    (prop_list->sdt_lun_reset_enable != 0) ?
4010 		    TRUE : FALSE;
4011 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4012 		    "sd_set_vers1_properties: lun reset enabled "
4013 		    "flag set to %d\n",
4014 		    prop_list->sdt_lun_reset_enable);
4015 	}
4016 
4017 	/*
4018 	 * Validate the throttle values.
4019 	 * If any of the numbers are invalid, set everything to defaults.
4020 	 */
4021 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4022 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4023 	    (un->un_min_throttle > un->un_throttle)) {
4024 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4025 		un->un_min_throttle = sd_min_throttle;
4026 	}
4027 }
4028 
4029 /*
4030  *   Function: sd_is_lsi()
4031  *
4032  *   Description: Check for lsi devices, step throught the static device
4033  *	table to match vid/pid.
4034  *
4035  *   Args: un - ptr to sd_lun
4036  *
4037  *   Notes:  When creating new LSI property, need to add the new LSI property
4038  *		to this function.
4039  */
4040 static void
4041 sd_is_lsi(struct sd_lun *un)
4042 {
4043 	char	*id = NULL;
4044 	int	table_index;
4045 	int	idlen;
4046 	void	*prop;
4047 
4048 	ASSERT(un != NULL);
4049 	for (table_index = 0; table_index < sd_disk_table_size;
4050 	    table_index++) {
4051 		id = sd_disk_table[table_index].device_id;
4052 		idlen = strlen(id);
4053 		if (idlen == 0) {
4054 			continue;
4055 		}
4056 
4057 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4058 			prop = sd_disk_table[table_index].properties;
4059 			if (prop == &lsi_properties ||
4060 			    prop == &lsi_oem_properties ||
4061 			    prop == &lsi_properties_scsi ||
4062 			    prop == &symbios_properties) {
4063 				un->un_f_cfg_is_lsi = TRUE;
4064 			}
4065 			break;
4066 		}
4067 	}
4068 }
4069 
4070 
4071 /*
4072  * The following routines support reading and interpretation of disk labels,
4073  * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and
4074  * fdisk tables.
4075  */
4076 
4077 /*
4078  *    Function: sd_validate_geometry
4079  *
4080  * Description: Read the label from the disk (if present). Update the unit's
4081  *		geometry and vtoc information from the data in the label.
4082  *		Verify that the label is valid.
4083  *
4084  *   Arguments: un - driver soft state (unit) structure
4085  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4086  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4087  *			to use the USCSI "direct" chain and bypass the normal
4088  *			command waitq.
4089  *
4090  * Return Code: 0 - Successful completion
4091  *		EINVAL  - Invalid value in un->un_tgt_blocksize or
4092  *			  un->un_blockcount; or label on disk is corrupted
4093  *			  or unreadable.
4094  *		EACCES  - Reservation conflict at the device.
4095  *		ENOMEM  - Resource allocation error
4096  *		ENOTSUP - geometry not applicable
4097  *
4098  *     Context: Kernel thread only (can sleep).
4099  */
4100 
4101 static int
4102 sd_validate_geometry(struct sd_lun *un, int path_flag)
4103 {
4104 	static	char		labelstring[128];
4105 	static	char		buf[256];
4106 	char	*label		= NULL;
4107 	int	label_error	= 0;
4108 	int	gvalid		= un->un_f_geometry_is_valid;
4109 	int	lbasize;
4110 	uint_t	capacity;
4111 	int	count;
4112 
4113 	ASSERT(un != NULL);
4114 	ASSERT(mutex_owned(SD_MUTEX(un)));
4115 
4116 	/*
4117 	 * If the required values are not valid, then try getting them
4118 	 * once via read capacity. If that fails, then fail this call.
4119 	 * This is necessary with the new mpxio failover behavior in
4120 	 * the T300 where we can get an attach for the inactive path
4121 	 * before the active path. The inactive path fails commands with
4122 	 * sense data of 02,04,88 which happens to the read capacity
4123 	 * before mpxio has had sufficient knowledge to know if it should
4124 	 * force a fail over or not. (Which it won't do at attach anyhow).
4125 	 * If the read capacity at attach time fails, un_tgt_blocksize and
4126 	 * un_blockcount won't be valid.
4127 	 */
4128 	if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
4129 	    (un->un_f_blockcount_is_valid != TRUE)) {
4130 		uint64_t	cap;
4131 		uint32_t	lbasz;
4132 		int		rval;
4133 
4134 		mutex_exit(SD_MUTEX(un));
4135 		rval = sd_send_scsi_READ_CAPACITY(un, &cap,
4136 		    &lbasz, SD_PATH_DIRECT);
4137 		mutex_enter(SD_MUTEX(un));
4138 		if (rval == 0) {
4139 			/*
4140 			 * The following relies on
4141 			 * sd_send_scsi_READ_CAPACITY never
4142 			 * returning 0 for capacity and/or lbasize.
4143 			 */
4144 			sd_update_block_info(un, lbasz, cap);
4145 		}
4146 
4147 		if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
4148 		    (un->un_f_blockcount_is_valid != TRUE)) {
4149 			return (EINVAL);
4150 		}
4151 	}
4152 
4153 	/*
4154 	 * Copy the lbasize and capacity so that if they're reset while we're
4155 	 * not holding the SD_MUTEX, we will continue to use valid values
4156 	 * after the SD_MUTEX is reacquired. (4119659)
4157 	 */
4158 	lbasize  = un->un_tgt_blocksize;
4159 	capacity = un->un_blockcount;
4160 
4161 #if defined(_SUNOS_VTOC_16)
4162 	/*
4163 	 * Set up the "whole disk" fdisk partition; this should always
4164 	 * exist, regardless of whether the disk contains an fdisk table
4165 	 * or vtoc.
4166 	 */
4167 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
4168 	un->un_map[P0_RAW_DISK].dkl_nblk  = capacity;
4169 #endif
4170 
4171 	/*
4172 	 * Refresh the logical and physical geometry caches.
4173 	 * (data from MODE SENSE format/rigid disk geometry pages,
4174 	 * and scsi_ifgetcap("geometry").
4175 	 */
4176 	sd_resync_geom_caches(un, capacity, lbasize, path_flag);
4177 
4178 	label_error = sd_use_efi(un, path_flag);
4179 	if (label_error == 0) {
4180 		/* found a valid EFI label */
4181 		SD_TRACE(SD_LOG_IO_PARTITION, un,
4182 			"sd_validate_geometry: found EFI label\n");
4183 		un->un_solaris_offset = 0;
4184 		un->un_solaris_size = capacity;
4185 		return (ENOTSUP);
4186 	}
4187 	if (un->un_blockcount > DK_MAX_BLOCKS) {
4188 		if (label_error == ESRCH) {
4189 			/*
4190 			 * they've configured a LUN over 1TB, but used
4191 			 * format.dat to restrict format's view of the
4192 			 * capacity to be under 1TB
4193 			 */
4194 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4195 "is >1TB and has a VTOC label: use format(1M) to either decrease the");
4196 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
4197 "size to be < 1TB or relabel the disk with an EFI label");
4198 		} else {
4199 			/* unlabeled disk over 1TB */
4200 			return (ENOTSUP);
4201 		}
4202 	}
4203 	label_error = 0;
4204 
4205 	/*
4206 	 * at this point it is either labeled with a VTOC or it is
4207 	 * under 1TB
4208 	 */
4209 	if (un->un_f_vtoc_label_supported) {
4210 		struct	dk_label *dkl;
4211 		offset_t dkl1;
4212 		offset_t label_addr, real_addr;
4213 		int	rval;
4214 		size_t	buffer_size;
4215 
4216 		/*
4217 		 * Note: This will set up un->un_solaris_size and
4218 		 * un->un_solaris_offset.
4219 		 */
4220 		switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) {
4221 		case SD_CMD_RESERVATION_CONFLICT:
4222 			ASSERT(mutex_owned(SD_MUTEX(un)));
4223 			return (EACCES);
4224 		case SD_CMD_FAILURE:
4225 			ASSERT(mutex_owned(SD_MUTEX(un)));
4226 			return (ENOMEM);
4227 		}
4228 
4229 		if (un->un_solaris_size <= DK_LABEL_LOC) {
4230 			/*
4231 			 * Found fdisk table but no Solaris partition entry,
4232 			 * so don't call sd_uselabel() and don't create
4233 			 * a default label.
4234 			 */
4235 			label_error = 0;
4236 			un->un_f_geometry_is_valid = TRUE;
4237 			goto no_solaris_partition;
4238 		}
4239 		label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC);
4240 
4241 		/*
4242 		 * sys_blocksize != tgt_blocksize, need to re-adjust
4243 		 * blkno and save the index to beginning of dk_label
4244 		 */
4245 		real_addr = SD_SYS2TGTBLOCK(un, label_addr);
4246 		buffer_size = SD_REQBYTES2TGTBYTES(un,
4247 		    sizeof (struct dk_label));
4248 
4249 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: "
4250 		    "label_addr: 0x%x allocation size: 0x%x\n",
4251 		    label_addr, buffer_size);
4252 		dkl = kmem_zalloc(buffer_size, KM_NOSLEEP);
4253 		if (dkl == NULL) {
4254 			return (ENOMEM);
4255 		}
4256 
4257 		mutex_exit(SD_MUTEX(un));
4258 		rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr,
4259 		    path_flag);
4260 		mutex_enter(SD_MUTEX(un));
4261 
4262 		switch (rval) {
4263 		case 0:
4264 			/*
4265 			 * sd_uselabel will establish that the geometry
4266 			 * is valid.
4267 			 * For sys_blocksize != tgt_blocksize, need
4268 			 * to index into the beginning of dk_label
4269 			 */
4270 			dkl1 = (daddr_t)dkl
4271 				+ SD_TGTBYTEOFFSET(un, label_addr, real_addr);
4272 			if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1,
4273 			    path_flag) != SD_LABEL_IS_VALID) {
4274 				label_error = EINVAL;
4275 			}
4276 			break;
4277 		case EACCES:
4278 			label_error = EACCES;
4279 			break;
4280 		default:
4281 			label_error = EINVAL;
4282 			break;
4283 		}
4284 
4285 		kmem_free(dkl, buffer_size);
4286 
4287 #if defined(_SUNOS_VTOC_8)
4288 		label = (char *)un->un_asciilabel;
4289 #elif defined(_SUNOS_VTOC_16)
4290 		label = (char *)un->un_vtoc.v_asciilabel;
4291 #else
4292 #error "No VTOC format defined."
4293 #endif
4294 	}
4295 
4296 	/*
4297 	 * If a valid label was not found, AND if no reservation conflict
4298 	 * was detected, then go ahead and create a default label (4069506).
4299 	 */
4300 
4301 	if (un->un_f_default_vtoc_supported && (label_error != EACCES)) {
4302 		if (un->un_f_geometry_is_valid == FALSE) {
4303 			sd_build_default_label(un);
4304 		}
4305 		label_error = 0;
4306 	}
4307 
4308 no_solaris_partition:
4309 	if ((!un->un_f_has_removable_media ||
4310 	    (un->un_f_has_removable_media &&
4311 		un->un_mediastate == DKIO_EJECTED)) &&
4312 		(un->un_state == SD_STATE_NORMAL && !gvalid)) {
4313 		/*
4314 		 * Print out a message indicating who and what we are.
4315 		 * We do this only when we happen to really validate the
4316 		 * geometry. We may call sd_validate_geometry() at other
4317 		 * times, e.g., ioctl()'s like Get VTOC in which case we
4318 		 * don't want to print the label.
4319 		 * If the geometry is valid, print the label string,
4320 		 * else print vendor and product info, if available
4321 		 */
4322 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
4323 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label);
4324 		} else {
4325 			mutex_enter(&sd_label_mutex);
4326 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
4327 			    labelstring);
4328 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
4329 			    &labelstring[64]);
4330 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
4331 			    labelstring, &labelstring[64]);
4332 			if (un->un_f_blockcount_is_valid == TRUE) {
4333 				(void) sprintf(&buf[strlen(buf)],
4334 				    ", %llu %u byte blocks\n",
4335 				    (longlong_t)un->un_blockcount,
4336 				    un->un_tgt_blocksize);
4337 			} else {
4338 				(void) sprintf(&buf[strlen(buf)],
4339 				    ", (unknown capacity)\n");
4340 			}
4341 			SD_INFO(SD_LOG_ATTACH_DETACH, un, buf);
4342 			mutex_exit(&sd_label_mutex);
4343 		}
4344 	}
4345 
4346 #if defined(_SUNOS_VTOC_16)
4347 	/*
4348 	 * If we have valid geometry, set up the remaining fdisk partitions.
4349 	 * Note that dkl_cylno is not used for the fdisk map entries, so
4350 	 * we set it to an entirely bogus value.
4351 	 */
4352 	for (count = 0; count < FD_NUMPART; count++) {
4353 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
4354 		un->un_map[FDISK_P1 + count].dkl_nblk =
4355 		    un->un_fmap[count].fmap_nblk;
4356 
4357 		un->un_offset[FDISK_P1 + count] =
4358 		    un->un_fmap[count].fmap_start;
4359 	}
4360 #endif
4361 
4362 	for (count = 0; count < NDKMAP; count++) {
4363 #if defined(_SUNOS_VTOC_8)
4364 		struct dk_map *lp  = &un->un_map[count];
4365 		un->un_offset[count] =
4366 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
4367 #elif defined(_SUNOS_VTOC_16)
4368 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
4369 
4370 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
4371 #else
4372 #error "No VTOC format defined."
4373 #endif
4374 	}
4375 
4376 	return (label_error);
4377 }
4378 
4379 
4380 #if defined(_SUNOS_VTOC_16)
4381 /*
4382  * Macro: MAX_BLKS
4383  *
4384  *	This macro is used for table entries where we need to have the largest
4385  *	possible sector value for that head & SPT (sectors per track)
4386  *	combination.  Other entries for some smaller disk sizes are set by
4387  *	convention to match those used by X86 BIOS usage.
4388  */
4389 #define	MAX_BLKS(heads, spt)	UINT16_MAX * heads * spt, heads, spt
4390 
4391 /*
4392  *    Function: sd_convert_geometry
4393  *
4394  * Description: Convert physical geometry into a dk_geom structure. In
4395  *		other words, make sure we don't wrap 16-bit values.
4396  *		e.g. converting from geom_cache to dk_geom
4397  *
4398  *     Context: Kernel thread only
4399  */
4400 static void
4401 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g)
4402 {
4403 	int i;
4404 	static const struct chs_values {
4405 		uint_t max_cap;		/* Max Capacity for this HS. */
4406 		uint_t nhead;		/* Heads to use. */
4407 		uint_t nsect;		/* SPT to use. */
4408 	} CHS_values[] = {
4409 		{0x00200000,  64, 32},		/* 1GB or smaller disk. */
4410 		{0x01000000, 128, 32},		/* 8GB or smaller disk. */
4411 		{MAX_BLKS(255,  63)},		/* 502.02GB or smaller disk. */
4412 		{MAX_BLKS(255, 126)},		/* .98TB or smaller disk. */
4413 		{DK_MAX_BLOCKS, 255, 189}	/* Max size is just under 1TB */
4414 	};
4415 
4416 	/* Unlabeled SCSI floppy device */
4417 	if (capacity <= 0x1000) {
4418 		un_g->dkg_nhead = 2;
4419 		un_g->dkg_ncyl = 80;
4420 		un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl);
4421 		return;
4422 	}
4423 
4424 	/*
4425 	 * For all devices we calculate cylinders using the
4426 	 * heads and sectors we assign based on capacity of the
4427 	 * device.  The table is designed to be compatible with the
4428 	 * way other operating systems lay out fdisk tables for X86
4429 	 * and to insure that the cylinders never exceed 65535 to
4430 	 * prevent problems with X86 ioctls that report geometry.
4431 	 * We use SPT that are multiples of 63, since other OSes that
4432 	 * are not limited to 16-bits for cylinders stop at 63 SPT
4433 	 * we make do by using multiples of 63 SPT.
4434 	 *
4435 	 * Note than capacities greater than or equal to 1TB will simply
4436 	 * get the largest geometry from the table. This should be okay
4437 	 * since disks this large shouldn't be using CHS values anyway.
4438 	 */
4439 	for (i = 0; CHS_values[i].max_cap < capacity &&
4440 	    CHS_values[i].max_cap != DK_MAX_BLOCKS; i++)
4441 		;
4442 
4443 	un_g->dkg_nhead = CHS_values[i].nhead;
4444 	un_g->dkg_nsect = CHS_values[i].nsect;
4445 }
4446 #endif
4447 
4448 
4449 /*
4450  *    Function: sd_resync_geom_caches
4451  *
4452  * Description: (Re)initialize both geometry caches: the virtual geometry
4453  *		information is extracted from the HBA (the "geometry"
4454  *		capability), and the physical geometry cache data is
4455  *		generated by issuing MODE SENSE commands.
4456  *
4457  *   Arguments: un - driver soft state (unit) structure
4458  *		capacity - disk capacity in #blocks
4459  *		lbasize - disk block size in bytes
4460  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4461  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4462  *			to use the USCSI "direct" chain and bypass the normal
4463  *			command waitq.
4464  *
4465  *     Context: Kernel thread only (can sleep).
4466  */
4467 
4468 static void
4469 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize,
4470 	int path_flag)
4471 {
4472 	struct 	geom_cache 	pgeom;
4473 	struct 	geom_cache	*pgeom_p = &pgeom;
4474 	int 	spc;
4475 	unsigned short nhead;
4476 	unsigned short nsect;
4477 
4478 	ASSERT(un != NULL);
4479 	ASSERT(mutex_owned(SD_MUTEX(un)));
4480 
4481 	/*
4482 	 * Ask the controller for its logical geometry.
4483 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
4484 	 * then the lgeom cache will be invalid.
4485 	 */
4486 	sd_get_virtual_geometry(un, capacity, lbasize);
4487 
4488 	/*
4489 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
4490 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
4491 	 */
4492 	if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) {
4493 		/*
4494 		 * Note: Perhaps this needs to be more adaptive? The rationale
4495 		 * is that, if there's no HBA geometry from the HBA driver, any
4496 		 * guess is good, since this is the physical geometry. If MODE
4497 		 * SENSE fails this gives a max cylinder size for non-LBA access
4498 		 */
4499 		nhead = 255;
4500 		nsect = 63;
4501 	} else {
4502 		nhead = un->un_lgeom.g_nhead;
4503 		nsect = un->un_lgeom.g_nsect;
4504 	}
4505 
4506 	if (ISCD(un)) {
4507 		pgeom_p->g_nhead = 1;
4508 		pgeom_p->g_nsect = nsect * nhead;
4509 	} else {
4510 		pgeom_p->g_nhead = nhead;
4511 		pgeom_p->g_nsect = nsect;
4512 	}
4513 
4514 	spc = pgeom_p->g_nhead * pgeom_p->g_nsect;
4515 	pgeom_p->g_capacity = capacity;
4516 	pgeom_p->g_ncyl = pgeom_p->g_capacity / spc;
4517 	pgeom_p->g_acyl = 0;
4518 
4519 	/*
4520 	 * Retrieve fresh geometry data from the hardware, stash it
4521 	 * here temporarily before we rebuild the incore label.
4522 	 *
4523 	 * We want to use the MODE SENSE commands to derive the
4524 	 * physical geometry of the device, but if either command
4525 	 * fails, the logical geometry is used as the fallback for
4526 	 * disk label geometry.
4527 	 */
4528 	mutex_exit(SD_MUTEX(un));
4529 	sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag);
4530 	mutex_enter(SD_MUTEX(un));
4531 
4532 	/*
4533 	 * Now update the real copy while holding the mutex. This
4534 	 * way the global copy is never in an inconsistent state.
4535 	 */
4536 	bcopy(pgeom_p, &un->un_pgeom,  sizeof (un->un_pgeom));
4537 
4538 	SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: "
4539 	    "(cached from lgeom)\n");
4540 	SD_INFO(SD_LOG_COMMON, un,
4541 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4542 	    un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl,
4543 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
4544 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
4545 	    "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize,
4546 	    un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv,
4547 	    un->un_pgeom.g_rpm);
4548 }
4549 
4550 
4551 /*
4552  *    Function: sd_read_fdisk
4553  *
4554  * Description: utility routine to read the fdisk table.
4555  *
4556  *   Arguments: un - driver soft state (unit) structure
4557  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4558  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4559  *			to use the USCSI "direct" chain and bypass the normal
4560  *			command waitq.
4561  *
4562  * Return Code: SD_CMD_SUCCESS
4563  *		SD_CMD_FAILURE
4564  *
4565  *     Context: Kernel thread only (can sleep).
4566  */
4567 /* ARGSUSED */
4568 static int
4569 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag)
4570 {
4571 #if defined(_NO_FDISK_PRESENT)
4572 
4573 	un->un_solaris_offset = 0;
4574 	un->un_solaris_size = capacity;
4575 	bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4576 	return (SD_CMD_SUCCESS);
4577 
4578 #elif defined(_FIRMWARE_NEEDS_FDISK)
4579 
4580 	struct ipart	*fdp;
4581 	struct mboot	*mbp;
4582 	struct ipart	fdisk[FD_NUMPART];
4583 	int		i;
4584 	char		sigbuf[2];
4585 	caddr_t		bufp;
4586 	int		uidx;
4587 	int		rval;
4588 	int		lba = 0;
4589 	uint_t		solaris_offset;	/* offset to solaris part. */
4590 	daddr_t		solaris_size;	/* size of solaris partition */
4591 	uint32_t	blocksize;
4592 
4593 	ASSERT(un != NULL);
4594 	ASSERT(mutex_owned(SD_MUTEX(un)));
4595 	ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE);
4596 
4597 	blocksize = un->un_tgt_blocksize;
4598 
4599 	/*
4600 	 * Start off assuming no fdisk table
4601 	 */
4602 	solaris_offset = 0;
4603 	solaris_size   = capacity;
4604 
4605 	mutex_exit(SD_MUTEX(un));
4606 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
4607 	rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag);
4608 	mutex_enter(SD_MUTEX(un));
4609 
4610 	if (rval != 0) {
4611 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4612 		    "sd_read_fdisk: fdisk read err\n");
4613 		kmem_free(bufp, blocksize);
4614 		return (SD_CMD_FAILURE);
4615 	}
4616 
4617 	mbp = (struct mboot *)bufp;
4618 
4619 	/*
4620 	 * The fdisk table does not begin on a 4-byte boundary within the
4621 	 * master boot record, so we copy it to an aligned structure to avoid
4622 	 * alignment exceptions on some processors.
4623 	 */
4624 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
4625 
4626 	/*
4627 	 * Check for lba support before verifying sig; sig might not be
4628 	 * there, say on a blank disk, but the max_chs mark may still
4629 	 * be present.
4630 	 *
4631 	 * Note: LBA support and BEFs are an x86-only concept but this
4632 	 * code should work OK on SPARC as well.
4633 	 */
4634 
4635 	/*
4636 	 * First, check for lba-access-ok on root node (or prom root node)
4637 	 * if present there, don't need to search fdisk table.
4638 	 */
4639 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
4640 	    "lba-access-ok", 0) != 0) {
4641 		/* All drives do LBA; don't search fdisk table */
4642 		lba = 1;
4643 	} else {
4644 		/* Okay, look for mark in fdisk table */
4645 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4646 			/* accumulate "lba" value from all partitions */
4647 			lba = (lba || sd_has_max_chs_vals(fdp));
4648 		}
4649 	}
4650 
4651 	if (lba != 0) {
4652 		dev_t dev = sd_make_device(SD_DEVINFO(un));
4653 
4654 		if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS,
4655 		    "lba-access-ok", 0) == 0) {
4656 			/* not found; create it */
4657 			if (ddi_prop_create(dev, SD_DEVINFO(un), 0,
4658 			    "lba-access-ok", (caddr_t)NULL, 0) !=
4659 			    DDI_PROP_SUCCESS) {
4660 				SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4661 				    "sd_read_fdisk: Can't create lba property "
4662 				    "for instance %d\n",
4663 				    ddi_get_instance(SD_DEVINFO(un)));
4664 			}
4665 		}
4666 	}
4667 
4668 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
4669 
4670 	/*
4671 	 * Endian-independent signature check
4672 	 */
4673 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
4674 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
4675 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4676 		    "sd_read_fdisk: no fdisk\n");
4677 		bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4678 		rval = SD_CMD_SUCCESS;
4679 		goto done;
4680 	}
4681 
4682 #ifdef SDDEBUG
4683 	if (sd_level_mask & SD_LOGMASK_INFO) {
4684 		fdp = fdisk;
4685 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n");
4686 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "         relsect    "
4687 		    "numsect         sysid       bootid\n");
4688 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
4689 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4690 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
4691 			    i, fdp->relsect, fdp->numsect,
4692 			    fdp->systid, fdp->bootid);
4693 		}
4694 	}
4695 #endif
4696 
4697 	/*
4698 	 * Try to find the unix partition
4699 	 */
4700 	uidx = -1;
4701 	solaris_offset = 0;
4702 	solaris_size   = 0;
4703 
4704 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4705 		int	relsect;
4706 		int	numsect;
4707 
4708 		if (fdp->numsect == 0) {
4709 			un->un_fmap[i].fmap_start = 0;
4710 			un->un_fmap[i].fmap_nblk  = 0;
4711 			continue;
4712 		}
4713 
4714 		/*
4715 		 * Data in the fdisk table is little-endian.
4716 		 */
4717 		relsect = LE_32(fdp->relsect);
4718 		numsect = LE_32(fdp->numsect);
4719 
4720 		un->un_fmap[i].fmap_start = relsect;
4721 		un->un_fmap[i].fmap_nblk  = numsect;
4722 
4723 		if (fdp->systid != SUNIXOS &&
4724 		    fdp->systid != SUNIXOS2 &&
4725 		    fdp->systid != EFI_PMBR) {
4726 			continue;
4727 		}
4728 
4729 		/*
4730 		 * use the last active solaris partition id found
4731 		 * (there should only be 1 active partition id)
4732 		 *
4733 		 * if there are no active solaris partition id
4734 		 * then use the first inactive solaris partition id
4735 		 */
4736 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
4737 			uidx = i;
4738 			solaris_offset = relsect;
4739 			solaris_size   = numsect;
4740 		}
4741 	}
4742 
4743 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx",
4744 	    un->un_solaris_offset, un->un_solaris_size);
4745 
4746 	rval = SD_CMD_SUCCESS;
4747 
4748 done:
4749 
4750 	/*
4751 	 * Clear the VTOC info, only if the Solaris partition entry
4752 	 * has moved, changed size, been deleted, or if the size of
4753 	 * the partition is too small to even fit the label sector.
4754 	 */
4755 	if ((un->un_solaris_offset != solaris_offset) ||
4756 	    (un->un_solaris_size != solaris_size) ||
4757 	    solaris_size <= DK_LABEL_LOC) {
4758 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx",
4759 			solaris_offset, solaris_size);
4760 		bzero(&un->un_g, sizeof (struct dk_geom));
4761 		bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
4762 		bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
4763 		un->un_f_geometry_is_valid = FALSE;
4764 	}
4765 	un->un_solaris_offset = solaris_offset;
4766 	un->un_solaris_size = solaris_size;
4767 	kmem_free(bufp, blocksize);
4768 	return (rval);
4769 
4770 #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
4771 #error "fdisk table presence undetermined for this platform."
4772 #endif	/* #if defined(_NO_FDISK_PRESENT) */
4773 }
4774 
4775 
4776 /*
4777  *    Function: sd_get_physical_geometry
4778  *
4779  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4780  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4781  *		target, and use this information to initialize the physical
4782  *		geometry cache specified by pgeom_p.
4783  *
4784  *		MODE SENSE is an optional command, so failure in this case
4785  *		does not necessarily denote an error. We want to use the
4786  *		MODE SENSE commands to derive the physical geometry of the
4787  *		device, but if either command fails, the logical geometry is
4788  *		used as the fallback for disk label geometry.
4789  *
4790  *		This requires that un->un_blockcount and un->un_tgt_blocksize
4791  *		have already been initialized for the current target and
4792  *		that the current values be passed as args so that we don't
4793  *		end up ever trying to use -1 as a valid value. This could
4794  *		happen if either value is reset while we're not holding
4795  *		the mutex.
4796  *
4797  *   Arguments: un - driver soft state (unit) structure
4798  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4799  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4800  *			to use the USCSI "direct" chain and bypass the normal
4801  *			command waitq.
4802  *
4803  *     Context: Kernel thread only (can sleep).
4804  */
4805 
4806 static void
4807 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p,
4808 	int capacity, int lbasize, int path_flag)
4809 {
4810 	struct	mode_format	*page3p;
4811 	struct	mode_geometry	*page4p;
4812 	struct	mode_header	*headerp;
4813 	int	sector_size;
4814 	int	nsect;
4815 	int	nhead;
4816 	int	ncyl;
4817 	int	intrlv;
4818 	int	spc;
4819 	int	modesense_capacity;
4820 	int	rpm;
4821 	int	bd_len;
4822 	int	mode_header_length;
4823 	uchar_t	*p3bufp;
4824 	uchar_t	*p4bufp;
4825 	int	cdbsize;
4826 
4827 	ASSERT(un != NULL);
4828 	ASSERT(!(mutex_owned(SD_MUTEX(un))));
4829 
4830 	if (un->un_f_blockcount_is_valid != TRUE) {
4831 		return;
4832 	}
4833 
4834 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
4835 		return;
4836 	}
4837 
4838 	if (lbasize == 0) {
4839 		if (ISCD(un)) {
4840 			lbasize = 2048;
4841 		} else {
4842 			lbasize = un->un_sys_blocksize;
4843 		}
4844 	}
4845 	pgeom_p->g_secsize = (unsigned short)lbasize;
4846 
4847 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4848 
4849 	/*
4850 	 * Retrieve MODE SENSE page 3 - Format Device Page
4851 	 */
4852 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4853 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp,
4854 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag)
4855 	    != 0) {
4856 		SD_ERROR(SD_LOG_COMMON, un,
4857 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
4858 		goto page3_exit;
4859 	}
4860 
4861 	/*
4862 	 * Determine size of Block Descriptors in order to locate the mode
4863 	 * page data.  ATAPI devices return 0, SCSI devices should return
4864 	 * MODE_BLK_DESC_LENGTH.
4865 	 */
4866 	headerp = (struct mode_header *)p3bufp;
4867 	if (un->un_f_cfg_is_atapi == TRUE) {
4868 		struct mode_header_grp2 *mhp =
4869 		    (struct mode_header_grp2 *)headerp;
4870 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
4871 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4872 	} else {
4873 		mode_header_length = MODE_HEADER_LENGTH;
4874 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
4875 	}
4876 
4877 	if (bd_len > MODE_BLK_DESC_LENGTH) {
4878 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4879 		    "received unexpected bd_len of %d, page3\n", bd_len);
4880 		goto page3_exit;
4881 	}
4882 
4883 	page3p = (struct mode_format *)
4884 	    ((caddr_t)headerp + mode_header_length + bd_len);
4885 
4886 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
4887 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4888 		    "mode sense pg3 code mismatch %d\n",
4889 		    page3p->mode_page.code);
4890 		goto page3_exit;
4891 	}
4892 
4893 	/*
4894 	 * Use this physical geometry data only if BOTH MODE SENSE commands
4895 	 * complete successfully; otherwise, revert to the logical geometry.
4896 	 * So, we need to save everything in temporary variables.
4897 	 */
4898 	sector_size = BE_16(page3p->data_bytes_sect);
4899 
4900 	/*
4901 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
4902 	 */
4903 	if (sector_size == 0) {
4904 		sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize;
4905 	} else {
4906 		sector_size &= ~(un->un_sys_blocksize - 1);
4907 	}
4908 
4909 	nsect  = BE_16(page3p->sect_track);
4910 	intrlv = BE_16(page3p->interleave);
4911 
4912 	SD_INFO(SD_LOG_COMMON, un,
4913 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
4914 	SD_INFO(SD_LOG_COMMON, un,
4915 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
4916 	    page3p->mode_page.code, nsect, sector_size);
4917 	SD_INFO(SD_LOG_COMMON, un,
4918 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
4919 	    BE_16(page3p->track_skew),
4920 	    BE_16(page3p->cylinder_skew));
4921 
4922 
4923 	/*
4924 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
4925 	 */
4926 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
4927 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp,
4928 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag)
4929 	    != 0) {
4930 		SD_ERROR(SD_LOG_COMMON, un,
4931 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
4932 		goto page4_exit;
4933 	}
4934 
4935 	/*
4936 	 * Determine size of Block Descriptors in order to locate the mode
4937 	 * page data.  ATAPI devices return 0, SCSI devices should return
4938 	 * MODE_BLK_DESC_LENGTH.
4939 	 */
4940 	headerp = (struct mode_header *)p4bufp;
4941 	if (un->un_f_cfg_is_atapi == TRUE) {
4942 		struct mode_header_grp2 *mhp =
4943 		    (struct mode_header_grp2 *)headerp;
4944 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4945 	} else {
4946 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
4947 	}
4948 
4949 	if (bd_len > MODE_BLK_DESC_LENGTH) {
4950 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4951 		    "received unexpected bd_len of %d, page4\n", bd_len);
4952 		goto page4_exit;
4953 	}
4954 
4955 	page4p = (struct mode_geometry *)
4956 	    ((caddr_t)headerp + mode_header_length + bd_len);
4957 
4958 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
4959 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4960 		    "mode sense pg4 code mismatch %d\n",
4961 		    page4p->mode_page.code);
4962 		goto page4_exit;
4963 	}
4964 
4965 	/*
4966 	 * Stash the data now, after we know that both commands completed.
4967 	 */
4968 
4969 	mutex_enter(SD_MUTEX(un));
4970 
4971 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
4972 	spc   = nhead * nsect;
4973 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
4974 	rpm   = BE_16(page4p->rpm);
4975 
4976 	modesense_capacity = spc * ncyl;
4977 
4978 	SD_INFO(SD_LOG_COMMON, un,
4979 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
4980 	SD_INFO(SD_LOG_COMMON, un,
4981 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
4982 	SD_INFO(SD_LOG_COMMON, un,
4983 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
4984 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
4985 	    (void *)pgeom_p, capacity);
4986 
4987 	/*
4988 	 * Compensate if the drive's geometry is not rectangular, i.e.,
4989 	 * the product of C * H * S returned by MODE SENSE >= that returned
4990 	 * by read capacity. This is an idiosyncrasy of the original x86
4991 	 * disk subsystem.
4992 	 */
4993 	if (modesense_capacity >= capacity) {
4994 		SD_INFO(SD_LOG_COMMON, un,
4995 		    "sd_get_physical_geometry: adjusting acyl; "
4996 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
4997 		    (modesense_capacity - capacity + spc - 1) / spc);
4998 		if (sector_size != 0) {
4999 			/* 1243403: NEC D38x7 drives don't support sec size */
5000 			pgeom_p->g_secsize = (unsigned short)sector_size;
5001 		}
5002 		pgeom_p->g_nsect    = (unsigned short)nsect;
5003 		pgeom_p->g_nhead    = (unsigned short)nhead;
5004 		pgeom_p->g_capacity = capacity;
5005 		pgeom_p->g_acyl	    =
5006 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5007 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5008 	}
5009 
5010 	pgeom_p->g_rpm    = (unsigned short)rpm;
5011 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5012 
5013 	SD_INFO(SD_LOG_COMMON, un,
5014 	    "sd_get_physical_geometry: mode sense geometry:\n");
5015 	SD_INFO(SD_LOG_COMMON, un,
5016 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5017 	    nsect, sector_size, intrlv);
5018 	SD_INFO(SD_LOG_COMMON, un,
5019 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5020 	    nhead, ncyl, rpm, modesense_capacity);
5021 	SD_INFO(SD_LOG_COMMON, un,
5022 	    "sd_get_physical_geometry: (cached)\n");
5023 	SD_INFO(SD_LOG_COMMON, un,
5024 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5025 	    un->un_pgeom.g_ncyl,  un->un_pgeom.g_acyl,
5026 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
5027 	SD_INFO(SD_LOG_COMMON, un,
5028 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5029 	    un->un_pgeom.g_secsize, un->un_pgeom.g_capacity,
5030 	    un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm);
5031 
5032 	mutex_exit(SD_MUTEX(un));
5033 
5034 page4_exit:
5035 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5036 page3_exit:
5037 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5038 }
5039 
5040 
5041 /*
5042  *    Function: sd_get_virtual_geometry
5043  *
5044  * Description: Ask the controller to tell us about the target device.
5045  *
5046  *   Arguments: un - pointer to softstate
5047  *		capacity - disk capacity in #blocks
5048  *		lbasize - disk block size in bytes
5049  *
5050  *     Context: Kernel thread only
5051  */
5052 
5053 static void
5054 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize)
5055 {
5056 	struct	geom_cache 	*lgeom_p = &un->un_lgeom;
5057 	uint_t	geombuf;
5058 	int	spc;
5059 
5060 	ASSERT(un != NULL);
5061 	ASSERT(mutex_owned(SD_MUTEX(un)));
5062 
5063 	mutex_exit(SD_MUTEX(un));
5064 
5065 	/* Set sector size, and total number of sectors */
5066 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5067 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5068 
5069 	/* Let the HBA tell us its geometry */
5070 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5071 
5072 	mutex_enter(SD_MUTEX(un));
5073 
5074 	/* A value of -1 indicates an undefined "geometry" property */
5075 	if (geombuf == (-1)) {
5076 		return;
5077 	}
5078 
5079 	/* Initialize the logical geometry cache. */
5080 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5081 	lgeom_p->g_nsect   = geombuf & 0xffff;
5082 	lgeom_p->g_secsize = un->un_sys_blocksize;
5083 
5084 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5085 
5086 	/*
5087 	 * Note: The driver originally converted the capacity value from
5088 	 * target blocks to system blocks. However, the capacity value passed
5089 	 * to this routine is already in terms of system blocks (this scaling
5090 	 * is done when the READ CAPACITY command is issued and processed).
5091 	 * This 'error' may have gone undetected because the usage of g_ncyl
5092 	 * (which is based upon g_capacity) is very limited within the driver
5093 	 */
5094 	lgeom_p->g_capacity = capacity;
5095 
5096 	/*
5097 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5098 	 * hba may return zero values if the device has been removed.
5099 	 */
5100 	if (spc == 0) {
5101 		lgeom_p->g_ncyl = 0;
5102 	} else {
5103 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5104 	}
5105 	lgeom_p->g_acyl = 0;
5106 
5107 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5108 	SD_INFO(SD_LOG_COMMON, un,
5109 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5110 	    un->un_lgeom.g_ncyl,  un->un_lgeom.g_acyl,
5111 	    un->un_lgeom.g_nhead, un->un_lgeom.g_nsect);
5112 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
5113 	    "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize,
5114 	    un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm);
5115 }
5116 
5117 
5118 /*
5119  *    Function: sd_update_block_info
5120  *
5121  * Description: Calculate a byte count to sector count bitshift value
5122  *		from sector size.
5123  *
5124  *   Arguments: un: unit struct.
5125  *		lbasize: new target sector size
5126  *		capacity: new target capacity, ie. block count
5127  *
5128  *     Context: Kernel thread context
5129  */
5130 
5131 static void
5132 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5133 {
5134 	if (lbasize != 0) {
5135 		un->un_tgt_blocksize = lbasize;
5136 		un->un_f_tgt_blocksize_is_valid	= TRUE;
5137 	}
5138 
5139 	if (capacity != 0) {
5140 		un->un_blockcount		= capacity;
5141 		un->un_f_blockcount_is_valid	= TRUE;
5142 	}
5143 }
5144 
5145 
5146 static void
5147 sd_swap_efi_gpt(efi_gpt_t *e)
5148 {
5149 	_NOTE(ASSUMING_PROTECTED(*e))
5150 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
5151 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
5152 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
5153 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
5154 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
5155 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
5156 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
5157 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
5158 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
5159 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
5160 	e->efi_gpt_NumberOfPartitionEntries =
5161 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
5162 	e->efi_gpt_SizeOfPartitionEntry =
5163 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
5164 	e->efi_gpt_PartitionEntryArrayCRC32 =
5165 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
5166 }
5167 
5168 static void
5169 sd_swap_efi_gpe(int nparts, efi_gpe_t *p)
5170 {
5171 	int i;
5172 
5173 	_NOTE(ASSUMING_PROTECTED(*p))
5174 	for (i = 0; i < nparts; i++) {
5175 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
5176 		    p[i].efi_gpe_PartitionTypeGUID);
5177 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
5178 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
5179 		/* PartitionAttrs */
5180 	}
5181 }
5182 
5183 static int
5184 sd_validate_efi(efi_gpt_t *labp)
5185 {
5186 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
5187 		return (EINVAL);
5188 	/* at least 96 bytes in this version of the spec. */
5189 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
5190 	    labp->efi_gpt_HeaderSize)
5191 		return (EINVAL);
5192 	/* this should be 128 bytes */
5193 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
5194 		return (EINVAL);
5195 	return (0);
5196 }
5197 
5198 static int
5199 sd_use_efi(struct sd_lun *un, int path_flag)
5200 {
5201 	int		i;
5202 	int		rval = 0;
5203 	efi_gpe_t	*partitions;
5204 	uchar_t		*buf;
5205 	uint_t		lbasize;
5206 	uint64_t	cap;
5207 	uint_t		nparts;
5208 	diskaddr_t	gpe_lba;
5209 
5210 	ASSERT(mutex_owned(SD_MUTEX(un)));
5211 	lbasize = un->un_tgt_blocksize;
5212 
5213 	mutex_exit(SD_MUTEX(un));
5214 
5215 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
5216 
5217 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
5218 		rval = EINVAL;
5219 		goto done_err;
5220 	}
5221 
5222 	rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag);
5223 	if (rval) {
5224 		goto done_err;
5225 	}
5226 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
5227 		/* not ours */
5228 		rval = ESRCH;
5229 		goto done_err;
5230 	}
5231 
5232 	rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag);
5233 	if (rval) {
5234 		goto done_err;
5235 	}
5236 	sd_swap_efi_gpt((efi_gpt_t *)buf);
5237 
5238 	if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) {
5239 		/*
5240 		 * Couldn't read the primary, try the backup.  Our
5241 		 * capacity at this point could be based on CHS, so
5242 		 * check what the device reports.
5243 		 */
5244 		rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
5245 		    path_flag);
5246 		if (rval) {
5247 			goto done_err;
5248 		}
5249 
5250 		/*
5251 		 * The MMC standard allows READ CAPACITY to be
5252 		 * inaccurate by a bounded amount (in the interest of
5253 		 * response latency).  As a result, failed READs are
5254 		 * commonplace (due to the reading of metadata and not
5255 		 * data). Depending on the per-Vendor/drive Sense data,
5256 		 * the failed READ can cause many (unnecessary) retries.
5257 		 */
5258 		if ((rval = sd_send_scsi_READ(un, buf, lbasize,
5259 		    cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY :
5260 			path_flag)) != 0) {
5261 				goto done_err;
5262 		}
5263 
5264 		sd_swap_efi_gpt((efi_gpt_t *)buf);
5265 		if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0)
5266 			goto done_err;
5267 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5268 		    "primary label corrupt; using backup\n");
5269 	}
5270 
5271 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
5272 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
5273 
5274 	rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba,
5275 	    path_flag);
5276 	if (rval) {
5277 		goto done_err;
5278 	}
5279 	partitions = (efi_gpe_t *)buf;
5280 
5281 	if (nparts > MAXPART) {
5282 		nparts = MAXPART;
5283 	}
5284 	sd_swap_efi_gpe(nparts, partitions);
5285 
5286 	mutex_enter(SD_MUTEX(un));
5287 
5288 	/* Fill in partition table. */
5289 	for (i = 0; i < nparts; i++) {
5290 		if (partitions->efi_gpe_StartingLBA != 0 ||
5291 		    partitions->efi_gpe_EndingLBA != 0) {
5292 			un->un_map[i].dkl_cylno =
5293 			    partitions->efi_gpe_StartingLBA;
5294 			un->un_map[i].dkl_nblk =
5295 			    partitions->efi_gpe_EndingLBA -
5296 			    partitions->efi_gpe_StartingLBA + 1;
5297 			un->un_offset[i] =
5298 			    partitions->efi_gpe_StartingLBA;
5299 		}
5300 		if (i == WD_NODE) {
5301 			/*
5302 			 * minor number 7 corresponds to the whole disk
5303 			 */
5304 			un->un_map[i].dkl_cylno = 0;
5305 			un->un_map[i].dkl_nblk = un->un_blockcount;
5306 			un->un_offset[i] = 0;
5307 		}
5308 		partitions++;
5309 	}
5310 	un->un_solaris_offset = 0;
5311 	un->un_solaris_size = cap;
5312 	un->un_f_geometry_is_valid = TRUE;
5313 
5314 	/* clear the vtoc label */
5315 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
5316 
5317 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
5318 	return (0);
5319 
5320 done_err:
5321 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
5322 	mutex_enter(SD_MUTEX(un));
5323 	/*
5324 	 * if we didn't find something that could look like a VTOC
5325 	 * and the disk is over 1TB, we know there isn't a valid label.
5326 	 * Otherwise let sd_uselabel decide what to do.  We only
5327 	 * want to invalidate this if we're certain the label isn't
5328 	 * valid because sd_prop_op will now fail, which in turn
5329 	 * causes things like opens and stats on the partition to fail.
5330 	 */
5331 	if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) {
5332 		un->un_f_geometry_is_valid = FALSE;
5333 	}
5334 	return (rval);
5335 }
5336 
5337 
5338 /*
5339  *    Function: sd_uselabel
5340  *
5341  * Description: Validate the disk label and update the relevant data (geometry,
5342  *		partition, vtoc, and capacity data) in the sd_lun struct.
5343  *		Marks the geometry of the unit as being valid.
5344  *
5345  *   Arguments: un: unit struct.
5346  *		dk_label: disk label
5347  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
5348  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
5349  *			to use the USCSI "direct" chain and bypass the normal
5350  *			command waitq.
5351  *
5352  * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry,
5353  *		partition, vtoc, and capacity data are good.
5354  *
5355  *		SD_LABEL_IS_INVALID: Magic number or checksum error in the
5356  *		label; or computed capacity does not jibe with capacity
5357  *		reported from the READ CAPACITY command.
5358  *
5359  *     Context: Kernel thread only (can sleep).
5360  */
5361 
5362 static int
5363 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag)
5364 {
5365 	short	*sp;
5366 	short	sum;
5367 	short	count;
5368 	int	label_error = SD_LABEL_IS_VALID;
5369 	int	i;
5370 	int	capacity;
5371 	int	part_end;
5372 	int	track_capacity;
5373 	int	err;
5374 #if defined(_SUNOS_VTOC_16)
5375 	struct	dkl_partition	*vpartp;
5376 #endif
5377 	ASSERT(un != NULL);
5378 	ASSERT(mutex_owned(SD_MUTEX(un)));
5379 
5380 	/* Validate the magic number of the label. */
5381 	if (labp->dkl_magic != DKL_MAGIC) {
5382 #if defined(__sparc)
5383 		if ((un->un_state == SD_STATE_NORMAL) &&
5384 			un->un_f_vtoc_errlog_supported) {
5385 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5386 			    "Corrupt label; wrong magic number\n");
5387 		}
5388 #endif
5389 		return (SD_LABEL_IS_INVALID);
5390 	}
5391 
5392 	/* Validate the checksum of the label. */
5393 	sp  = (short *)labp;
5394 	sum = 0;
5395 	count = sizeof (struct dk_label) / sizeof (short);
5396 	while (count--)	 {
5397 		sum ^= *sp++;
5398 	}
5399 
5400 	if (sum != 0) {
5401 #if	defined(_SUNOS_VTOC_16)
5402 		if ((un->un_state == SD_STATE_NORMAL) && !ISCD(un)) {
5403 #elif defined(_SUNOS_VTOC_8)
5404 		if ((un->un_state == SD_STATE_NORMAL) &&
5405 		    un->un_f_vtoc_errlog_supported) {
5406 #endif
5407 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5408 			    "Corrupt label - label checksum failed\n");
5409 		}
5410 		return (SD_LABEL_IS_INVALID);
5411 	}
5412 
5413 
5414 	/*
5415 	 * Fill in geometry structure with data from label.
5416 	 */
5417 	bzero(&un->un_g, sizeof (struct dk_geom));
5418 	un->un_g.dkg_ncyl   = labp->dkl_ncyl;
5419 	un->un_g.dkg_acyl   = labp->dkl_acyl;
5420 	un->un_g.dkg_bcyl   = 0;
5421 	un->un_g.dkg_nhead  = labp->dkl_nhead;
5422 	un->un_g.dkg_nsect  = labp->dkl_nsect;
5423 	un->un_g.dkg_intrlv = labp->dkl_intrlv;
5424 
5425 #if defined(_SUNOS_VTOC_8)
5426 	un->un_g.dkg_gap1   = labp->dkl_gap1;
5427 	un->un_g.dkg_gap2   = labp->dkl_gap2;
5428 	un->un_g.dkg_bhead  = labp->dkl_bhead;
5429 #endif
5430 #if defined(_SUNOS_VTOC_16)
5431 	un->un_dkg_skew = labp->dkl_skew;
5432 #endif
5433 
5434 #if defined(__i386) || defined(__amd64)
5435 	un->un_g.dkg_apc = labp->dkl_apc;
5436 #endif
5437 
5438 	/*
5439 	 * Currently we rely on the values in the label being accurate. If
5440 	 * dlk_rpm or dlk_pcly are zero in the label, use a default value.
5441 	 *
5442 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
5443 	 * although this command is optional in SCSI-2.
5444 	 */
5445 	un->un_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
5446 	un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
5447 	    (un->un_g.dkg_ncyl + un->un_g.dkg_acyl);
5448 
5449 	/*
5450 	 * The Read and Write reinstruct values may not be valid
5451 	 * for older disks.
5452 	 */
5453 	un->un_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
5454 	un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
5455 
5456 	/* Fill in partition table. */
5457 #if defined(_SUNOS_VTOC_8)
5458 	for (i = 0; i < NDKMAP; i++) {
5459 		un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
5460 		un->un_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
5461 	}
5462 #endif
5463 #if  defined(_SUNOS_VTOC_16)
5464 	vpartp		= labp->dkl_vtoc.v_part;
5465 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
5466 
5467 	/* Prevent divide by zero */
5468 	if (track_capacity == 0) {
5469 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5470 		    "Corrupt label - zero nhead or nsect value\n");
5471 
5472 		return (SD_LABEL_IS_INVALID);
5473 	}
5474 
5475 	for (i = 0; i < NDKMAP; i++, vpartp++) {
5476 		un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity;
5477 		un->un_map[i].dkl_nblk  = vpartp->p_size;
5478 	}
5479 #endif
5480 
5481 	/* Fill in VTOC Structure. */
5482 	bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc));
5483 #if defined(_SUNOS_VTOC_8)
5484 	/*
5485 	 * The 8-slice vtoc does not include the ascii label; save it into
5486 	 * the device's soft state structure here.
5487 	 */
5488 	bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
5489 #endif
5490 
5491 	/* Now look for a valid capacity. */
5492 	track_capacity	= (un->un_g.dkg_nhead * un->un_g.dkg_nsect);
5493 	capacity	= (un->un_g.dkg_ncyl  * track_capacity);
5494 
5495 	if (un->un_g.dkg_acyl) {
5496 #if defined(__i386) || defined(__amd64)
5497 		/* we may have > 1 alts cylinder */
5498 		capacity += (track_capacity * un->un_g.dkg_acyl);
5499 #else
5500 		capacity += track_capacity;
5501 #endif
5502 	}
5503 
5504 	/*
5505 	 * Force check here to ensure the computed capacity is valid.
5506 	 * If capacity is zero, it indicates an invalid label and
5507 	 * we should abort updating the relevant data then.
5508 	 */
5509 	if (capacity == 0) {
5510 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5511 		    "Corrupt label - no valid capacity could be retrieved\n");
5512 
5513 		return (SD_LABEL_IS_INVALID);
5514 	}
5515 
5516 	/* Mark the geometry as valid. */
5517 	un->un_f_geometry_is_valid = TRUE;
5518 
5519 	/*
5520 	 * At this point, un->un_blockcount should contain valid data from
5521 	 * the READ CAPACITY command.
5522 	 */
5523 	if (un->un_f_blockcount_is_valid != TRUE) {
5524 		/*
5525 		 * We have a situation where the target didn't give us a good
5526 		 * READ CAPACITY value, yet there appears to be a valid label.
5527 		 * In this case, we'll fake the capacity.
5528 		 */
5529 		un->un_blockcount = capacity;
5530 		un->un_f_blockcount_is_valid = TRUE;
5531 		goto done;
5532 	}
5533 
5534 
5535 	if ((capacity <= un->un_blockcount) ||
5536 	    (un->un_state != SD_STATE_NORMAL)) {
5537 #if defined(_SUNOS_VTOC_8)
5538 		/*
5539 		 * We can't let this happen on drives that are subdivided
5540 		 * into logical disks (i.e., that have an fdisk table).
5541 		 * The un_blockcount field should always hold the full media
5542 		 * size in sectors, period.  This code would overwrite
5543 		 * un_blockcount with the size of the Solaris fdisk partition.
5544 		 */
5545 		SD_ERROR(SD_LOG_COMMON, un,
5546 		    "sd_uselabel: Label %d blocks; Drive %d blocks\n",
5547 		    capacity, un->un_blockcount);
5548 		un->un_blockcount = capacity;
5549 		un->un_f_blockcount_is_valid = TRUE;
5550 #endif	/* defined(_SUNOS_VTOC_8) */
5551 		goto done;
5552 	}
5553 
5554 	if (ISCD(un)) {
5555 		/* For CDROMs, we trust that the data in the label is OK. */
5556 #if defined(_SUNOS_VTOC_8)
5557 		for (i = 0; i < NDKMAP; i++) {
5558 			part_end = labp->dkl_nhead * labp->dkl_nsect *
5559 			    labp->dkl_map[i].dkl_cylno +
5560 			    labp->dkl_map[i].dkl_nblk  - 1;
5561 
5562 			if ((labp->dkl_map[i].dkl_nblk) &&
5563 			    (part_end > un->un_blockcount)) {
5564 				un->un_f_geometry_is_valid = FALSE;
5565 				break;
5566 			}
5567 		}
5568 #endif
5569 #if defined(_SUNOS_VTOC_16)
5570 		vpartp = &(labp->dkl_vtoc.v_part[0]);
5571 		for (i = 0; i < NDKMAP; i++, vpartp++) {
5572 			part_end = vpartp->p_start + vpartp->p_size;
5573 			if ((vpartp->p_size > 0) &&
5574 			    (part_end > un->un_blockcount)) {
5575 				un->un_f_geometry_is_valid = FALSE;
5576 				break;
5577 			}
5578 		}
5579 #endif
5580 	} else {
5581 		uint64_t t_capacity;
5582 		uint32_t t_lbasize;
5583 
5584 		mutex_exit(SD_MUTEX(un));
5585 		err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize,
5586 		    path_flag);
5587 		ASSERT(t_capacity <= DK_MAX_BLOCKS);
5588 		mutex_enter(SD_MUTEX(un));
5589 
5590 		if (err == 0) {
5591 			sd_update_block_info(un, t_lbasize, t_capacity);
5592 		}
5593 
5594 		if (capacity > un->un_blockcount) {
5595 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5596 			    "Corrupt label - bad geometry\n");
5597 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
5598 			    "Label says %u blocks; Drive says %llu blocks\n",
5599 			    capacity, (unsigned long long)un->un_blockcount);
5600 			un->un_f_geometry_is_valid = FALSE;
5601 			label_error = SD_LABEL_IS_INVALID;
5602 		}
5603 	}
5604 
5605 done:
5606 
5607 	SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n");
5608 	SD_INFO(SD_LOG_COMMON, un,
5609 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
5610 	    un->un_g.dkg_ncyl,  un->un_g.dkg_acyl,
5611 	    un->un_g.dkg_nhead, un->un_g.dkg_nsect);
5612 	SD_INFO(SD_LOG_COMMON, un,
5613 	    "   lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n",
5614 	    un->un_tgt_blocksize, un->un_blockcount,
5615 	    un->un_g.dkg_intrlv, un->un_g.dkg_rpm);
5616 	SD_INFO(SD_LOG_COMMON, un, "   wrt_reinstr: %d; rd_reinstr: %d\n",
5617 	    un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct);
5618 
5619 	ASSERT(mutex_owned(SD_MUTEX(un)));
5620 
5621 	return (label_error);
5622 }
5623 
5624 
5625 /*
5626  *    Function: sd_build_default_label
5627  *
5628  * Description: Generate a default label for those devices that do not have
5629  *		one, e.g., new media, removable cartridges, etc..
5630  *
5631  *     Context: Kernel thread only
5632  */
5633 
5634 static void
5635 sd_build_default_label(struct sd_lun *un)
5636 {
5637 #if defined(_SUNOS_VTOC_16)
5638 	uint_t	phys_spc;
5639 	uint_t	disksize;
5640 	struct	dk_geom un_g;
5641 #endif
5642 
5643 	ASSERT(un != NULL);
5644 	ASSERT(mutex_owned(SD_MUTEX(un)));
5645 
5646 #if defined(_SUNOS_VTOC_8)
5647 	/*
5648 	 * Note: This is a legacy check for non-removable devices on VTOC_8
5649 	 * only. This may be a valid check for VTOC_16 as well.
5650 	 * Once we understand why there is this difference between SPARC and
5651 	 * x86 platform, we could remove this legacy check.
5652 	 */
5653 	ASSERT(un->un_f_default_vtoc_supported);
5654 #endif
5655 
5656 	bzero(&un->un_g, sizeof (struct dk_geom));
5657 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
5658 	bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
5659 
5660 #if defined(_SUNOS_VTOC_8)
5661 
5662 	/*
5663 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
5664 	 * But it is still necessary to set up various geometry information,
5665 	 * and we are doing this here.
5666 	 */
5667 
5668 	/*
5669 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
5670 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
5671 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
5672 	 * equal to C*H*S values.  This will cause some truncation of size due
5673 	 * to round off errors. For CD-ROMs, this truncation can have adverse
5674 	 * side effects, so returning ncyl and nhead as 1. The nsect will
5675 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
5676 	 */
5677 	if (ISCD(un)) {
5678 		/*
5679 		 * Preserve the old behavior for non-writable
5680 		 * medias. Since dkg_nsect is a ushort, it
5681 		 * will lose bits as cdroms have more than
5682 		 * 65536 sectors. So if we recalculate
5683 		 * capacity, it will become much shorter.
5684 		 * But the dkg_* information is not
5685 		 * used for CDROMs so it is OK. But for
5686 		 * Writable CDs we need this information
5687 		 * to be valid (for newfs say). So we
5688 		 * make nsect and nhead > 1 that way
5689 		 * nsect can still stay within ushort limit
5690 		 * without losing any bits.
5691 		 */
5692 		if (un->un_f_mmc_writable_media == TRUE) {
5693 			un->un_g.dkg_nhead = 64;
5694 			un->un_g.dkg_nsect = 32;
5695 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
5696 			un->un_blockcount = un->un_g.dkg_ncyl *
5697 			    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
5698 		} else {
5699 			un->un_g.dkg_ncyl  = 1;
5700 			un->un_g.dkg_nhead = 1;
5701 			un->un_g.dkg_nsect = un->un_blockcount;
5702 		}
5703 	} else {
5704 		if (un->un_blockcount <= 0x1000) {
5705 			/* unlabeled SCSI floppy device */
5706 			un->un_g.dkg_nhead = 2;
5707 			un->un_g.dkg_ncyl = 80;
5708 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
5709 		} else if (un->un_blockcount <= 0x200000) {
5710 			un->un_g.dkg_nhead = 64;
5711 			un->un_g.dkg_nsect = 32;
5712 			un->un_g.dkg_ncyl  = un->un_blockcount / (64 * 32);
5713 		} else {
5714 			un->un_g.dkg_nhead = 255;
5715 			un->un_g.dkg_nsect = 63;
5716 			un->un_g.dkg_ncyl  = un->un_blockcount / (255 * 63);
5717 		}
5718 		un->un_blockcount =
5719 		    un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect;
5720 	}
5721 
5722 	un->un_g.dkg_acyl	= 0;
5723 	un->un_g.dkg_bcyl	= 0;
5724 	un->un_g.dkg_rpm	= 200;
5725 	un->un_asciilabel[0]	= '\0';
5726 	un->un_g.dkg_pcyl	= un->un_g.dkg_ncyl;
5727 
5728 	un->un_map[0].dkl_cylno = 0;
5729 	un->un_map[0].dkl_nblk  = un->un_blockcount;
5730 	un->un_map[2].dkl_cylno = 0;
5731 	un->un_map[2].dkl_nblk  = un->un_blockcount;
5732 
5733 #elif defined(_SUNOS_VTOC_16)
5734 
5735 	if (un->un_solaris_size == 0) {
5736 		/*
5737 		 * Got fdisk table but no solaris entry therefore
5738 		 * don't create a default label
5739 		 */
5740 		un->un_f_geometry_is_valid = TRUE;
5741 		return;
5742 	}
5743 
5744 	/*
5745 	 * For CDs we continue to use the physical geometry to calculate
5746 	 * number of cylinders. All other devices must convert the
5747 	 * physical geometry (geom_cache) to values that will fit
5748 	 * in a dk_geom structure.
5749 	 */
5750 	if (ISCD(un)) {
5751 		phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect;
5752 	} else {
5753 		/* Convert physical geometry to disk geometry */
5754 		bzero(&un_g, sizeof (struct dk_geom));
5755 		sd_convert_geometry(un->un_blockcount, &un_g);
5756 		bcopy(&un_g, &un->un_g, sizeof (un->un_g));
5757 		phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect;
5758 	}
5759 
5760 	ASSERT(phys_spc != 0);
5761 	un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc;
5762 	un->un_g.dkg_acyl = DK_ACYL;
5763 	un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL;
5764 	disksize = un->un_g.dkg_ncyl * phys_spc;
5765 
5766 	if (ISCD(un)) {
5767 		/*
5768 		 * CD's don't use the "heads * sectors * cyls"-type of
5769 		 * geometry, but instead use the entire capacity of the media.
5770 		 */
5771 		disksize = un->un_solaris_size;
5772 		un->un_g.dkg_nhead = 1;
5773 		un->un_g.dkg_nsect = 1;
5774 		un->un_g.dkg_rpm =
5775 		    (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm;
5776 
5777 		un->un_vtoc.v_part[0].p_start = 0;
5778 		un->un_vtoc.v_part[0].p_size  = disksize;
5779 		un->un_vtoc.v_part[0].p_tag   = V_BACKUP;
5780 		un->un_vtoc.v_part[0].p_flag  = V_UNMNT;
5781 
5782 		un->un_map[0].dkl_cylno = 0;
5783 		un->un_map[0].dkl_nblk  = disksize;
5784 		un->un_offset[0] = 0;
5785 
5786 	} else {
5787 		/*
5788 		 * Hard disks and removable media cartridges
5789 		 */
5790 		un->un_g.dkg_rpm =
5791 		    (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm;
5792 		un->un_vtoc.v_sectorsz = un->un_sys_blocksize;
5793 
5794 		/* Add boot slice */
5795 		un->un_vtoc.v_part[8].p_start = 0;
5796 		un->un_vtoc.v_part[8].p_size  = phys_spc;
5797 		un->un_vtoc.v_part[8].p_tag   = V_BOOT;
5798 		un->un_vtoc.v_part[8].p_flag  = V_UNMNT;
5799 
5800 		un->un_map[8].dkl_cylno = 0;
5801 		un->un_map[8].dkl_nblk  = phys_spc;
5802 		un->un_offset[8] = 0;
5803 	}
5804 
5805 	un->un_g.dkg_apc = 0;
5806 	un->un_vtoc.v_nparts = V_NUMPAR;
5807 
5808 	/* Add backup slice */
5809 	un->un_vtoc.v_part[2].p_start = 0;
5810 	un->un_vtoc.v_part[2].p_size  = disksize;
5811 	un->un_vtoc.v_part[2].p_tag   = V_BACKUP;
5812 	un->un_vtoc.v_part[2].p_flag  = V_UNMNT;
5813 
5814 	un->un_map[2].dkl_cylno = 0;
5815 	un->un_map[2].dkl_nblk  = disksize;
5816 	un->un_offset[2] = 0;
5817 
5818 	(void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
5819 	    " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl,
5820 	    un->un_g.dkg_nhead, un->un_g.dkg_nsect);
5821 
5822 #else
5823 #error "No VTOC format defined."
5824 #endif
5825 
5826 	un->un_g.dkg_read_reinstruct  = 0;
5827 	un->un_g.dkg_write_reinstruct = 0;
5828 
5829 	un->un_g.dkg_intrlv = 1;
5830 
5831 	un->un_vtoc.v_version = V_VERSION;
5832 	un->un_vtoc.v_sanity  = VTOC_SANE;
5833 
5834 	un->un_f_geometry_is_valid = TRUE;
5835 
5836 	SD_INFO(SD_LOG_COMMON, un,
5837 	    "sd_build_default_label: Default label created: "
5838 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
5839 	    un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead,
5840 	    un->un_g.dkg_nsect, un->un_blockcount);
5841 }
5842 
5843 
5844 #if defined(_FIRMWARE_NEEDS_FDISK)
5845 /*
5846  * Max CHS values, as they are encoded into bytes, for 1022/254/63
5847  */
5848 #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
5849 #define	LBA_MAX_CYL	(1022 & 0xFF)
5850 #define	LBA_MAX_HEAD	(254)
5851 
5852 
5853 /*
5854  *    Function: sd_has_max_chs_vals
5855  *
5856  * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum.
5857  *
5858  *   Arguments: fdp - ptr to CHS info
5859  *
5860  * Return Code: True or false
5861  *
5862  *     Context: Any.
5863  */
5864 
5865 static int
5866 sd_has_max_chs_vals(struct ipart *fdp)
5867 {
5868 	return ((fdp->begcyl  == LBA_MAX_CYL)	&&
5869 	    (fdp->beghead == LBA_MAX_HEAD)	&&
5870 	    (fdp->begsect == LBA_MAX_SECT)	&&
5871 	    (fdp->endcyl  == LBA_MAX_CYL)	&&
5872 	    (fdp->endhead == LBA_MAX_HEAD)	&&
5873 	    (fdp->endsect == LBA_MAX_SECT));
5874 }
5875 #endif
5876 
5877 
5878 /*
5879  *    Function: sd_inq_fill
5880  *
5881  * Description: Print a piece of inquiry data, cleaned up for non-printable
5882  *		characters and stopping at the first space character after
5883  *		the beginning of the passed string;
5884  *
5885  *   Arguments: p - source string
5886  *		l - maximum length to copy
5887  *		s - destination string
5888  *
5889  *     Context: Any.
5890  */
5891 
5892 static void
5893 sd_inq_fill(char *p, int l, char *s)
5894 {
5895 	unsigned i = 0;
5896 	char c;
5897 
5898 	while (i++ < l) {
5899 		if ((c = *p++) < ' ' || c >= 0x7F) {
5900 			c = '*';
5901 		} else if (i != 1 && c == ' ') {
5902 			break;
5903 		}
5904 		*s++ = c;
5905 	}
5906 	*s++ = 0;
5907 }
5908 
5909 
5910 /*
5911  *    Function: sd_register_devid
5912  *
5913  * Description: This routine will obtain the device id information from the
5914  *		target, obtain the serial number, and register the device
5915  *		id with the ddi framework.
5916  *
5917  *   Arguments: devi - the system's dev_info_t for the device.
5918  *		un - driver soft state (unit) structure
5919  *		reservation_flag - indicates if a reservation conflict
5920  *		occurred during attach
5921  *
5922  *     Context: Kernel Thread
5923  */
5924 static void
5925 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag)
5926 {
5927 	int		rval		= 0;
5928 	uchar_t		*inq80		= NULL;
5929 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
5930 	size_t		inq80_resid	= 0;
5931 	uchar_t		*inq83		= NULL;
5932 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
5933 	size_t		inq83_resid	= 0;
5934 
5935 	ASSERT(un != NULL);
5936 	ASSERT(mutex_owned(SD_MUTEX(un)));
5937 	ASSERT((SD_DEVINFO(un)) == devi);
5938 
5939 	/*
5940 	 * This is the case of antiquated Sun disk drives that have the
5941 	 * FAB_DEVID property set in the disk_table.  These drives
5942 	 * manage the devid's by storing them in last 2 available sectors
5943 	 * on the drive and have them fabricated by the ddi layer by calling
5944 	 * ddi_devid_init and passing the DEVID_FAB flag.
5945 	 */
5946 	if (un->un_f_opt_fab_devid == TRUE) {
5947 		/*
5948 		 * Depending on EINVAL isn't reliable, since a reserved disk
5949 		 * may result in invalid geometry, so check to make sure a
5950 		 * reservation conflict did not occur during attach.
5951 		 */
5952 		if ((sd_get_devid(un) == EINVAL) &&
5953 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
5954 			/*
5955 			 * The devid is invalid AND there is no reservation
5956 			 * conflict.  Fabricate a new devid.
5957 			 */
5958 			(void) sd_create_devid(un);
5959 		}
5960 
5961 		/* Register the devid if it exists */
5962 		if (un->un_devid != NULL) {
5963 			(void) ddi_devid_register(SD_DEVINFO(un),
5964 			    un->un_devid);
5965 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5966 			    "sd_register_devid: Devid Fabricated\n");
5967 		}
5968 		return;
5969 	}
5970 
5971 	/*
5972 	 * We check the availibility of the World Wide Name (0x83) and Unit
5973 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5974 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5975 	 * 0x83 is availible, that is the best choice.  Our next choice is
5976 	 * 0x80.  If neither are availible, we munge the devid from the device
5977 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5978 	 * to fabricate a devid for non-Sun qualified disks.
5979 	 */
5980 	if (sd_check_vpd_page_support(un) == 0) {
5981 		/* collect page 80 data if available */
5982 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5983 
5984 			mutex_exit(SD_MUTEX(un));
5985 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5986 			rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len,
5987 			    0x01, 0x80, &inq80_resid);
5988 
5989 			if (rval != 0) {
5990 				kmem_free(inq80, inq80_len);
5991 				inq80 = NULL;
5992 				inq80_len = 0;
5993 			}
5994 			mutex_enter(SD_MUTEX(un));
5995 		}
5996 
5997 		/* collect page 83 data if available */
5998 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5999 			mutex_exit(SD_MUTEX(un));
6000 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
6001 			rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len,
6002 			    0x01, 0x83, &inq83_resid);
6003 
6004 			if (rval != 0) {
6005 				kmem_free(inq83, inq83_len);
6006 				inq83 = NULL;
6007 				inq83_len = 0;
6008 			}
6009 			mutex_enter(SD_MUTEX(un));
6010 		}
6011 	}
6012 
6013 	/* encode best devid possible based on data available */
6014 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
6015 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
6016 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
6017 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
6018 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
6019 
6020 		/* devid successfully encoded, register devid */
6021 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
6022 
6023 	} else {
6024 		/*
6025 		 * Unable to encode a devid based on data available.
6026 		 * This is not a Sun qualified disk.  Older Sun disk
6027 		 * drives that have the SD_FAB_DEVID property
6028 		 * set in the disk_table and non Sun qualified
6029 		 * disks are treated in the same manner.  These
6030 		 * drives manage the devid's by storing them in
6031 		 * last 2 available sectors on the drive and
6032 		 * have them fabricated by the ddi layer by
6033 		 * calling ddi_devid_init and passing the
6034 		 * DEVID_FAB flag.
6035 		 * Create a fabricate devid only if there's no
6036 		 * fabricate devid existed.
6037 		 */
6038 		if (sd_get_devid(un) == EINVAL) {
6039 			(void) sd_create_devid(un);
6040 			un->un_f_opt_fab_devid = TRUE;
6041 		}
6042 
6043 		/* Register the devid if it exists */
6044 		if (un->un_devid != NULL) {
6045 			(void) ddi_devid_register(SD_DEVINFO(un),
6046 			    un->un_devid);
6047 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
6048 			    "sd_register_devid: devid fabricated using "
6049 			    "ddi framework\n");
6050 		}
6051 	}
6052 
6053 	/* clean up resources */
6054 	if (inq80 != NULL) {
6055 		kmem_free(inq80, inq80_len);
6056 	}
6057 	if (inq83 != NULL) {
6058 		kmem_free(inq83, inq83_len);
6059 	}
6060 }
6061 
6062 static daddr_t
6063 sd_get_devid_block(struct sd_lun *un)
6064 {
6065 	daddr_t			spc, blk, head, cyl;
6066 
6067 	if (un->un_blockcount <= DK_MAX_BLOCKS) {
6068 		/* this geometry doesn't allow us to write a devid */
6069 		if (un->un_g.dkg_acyl < 2) {
6070 			return (-1);
6071 		}
6072 
6073 		/*
6074 		 * Subtract 2 guarantees that the next to last cylinder
6075 		 * is used
6076 		 */
6077 		cyl  = un->un_g.dkg_ncyl  + un->un_g.dkg_acyl - 2;
6078 		spc  = un->un_g.dkg_nhead * un->un_g.dkg_nsect;
6079 		head = un->un_g.dkg_nhead - 1;
6080 		blk  = (cyl * (spc - un->un_g.dkg_apc)) +
6081 		    (head * un->un_g.dkg_nsect) + 1;
6082 	} else {
6083 		if (un->un_reserved != -1) {
6084 			blk = un->un_map[un->un_reserved].dkl_cylno + 1;
6085 		} else {
6086 			return (-1);
6087 		}
6088 	}
6089 	return (blk);
6090 }
6091 
6092 /*
6093  *    Function: sd_get_devid
6094  *
6095  * Description: This routine will return 0 if a valid device id has been
6096  *		obtained from the target and stored in the soft state. If a
6097  *		valid device id has not been previously read and stored, a
6098  *		read attempt will be made.
6099  *
6100  *   Arguments: un - driver soft state (unit) structure
6101  *
6102  * Return Code: 0 if we successfully get the device id
6103  *
6104  *     Context: Kernel Thread
6105  */
6106 
6107 static int
6108 sd_get_devid(struct sd_lun *un)
6109 {
6110 	struct dk_devid		*dkdevid;
6111 	ddi_devid_t		tmpid;
6112 	uint_t			*ip;
6113 	size_t			sz;
6114 	daddr_t			blk;
6115 	int			status;
6116 	int			chksum;
6117 	int			i;
6118 	size_t			buffer_size;
6119 
6120 	ASSERT(un != NULL);
6121 	ASSERT(mutex_owned(SD_MUTEX(un)));
6122 
6123 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
6124 	    un);
6125 
6126 	if (un->un_devid != NULL) {
6127 		return (0);
6128 	}
6129 
6130 	blk = sd_get_devid_block(un);
6131 	if (blk < 0)
6132 		return (EINVAL);
6133 
6134 	/*
6135 	 * Read and verify device id, stored in the reserved cylinders at the
6136 	 * end of the disk. Backup label is on the odd sectors of the last
6137 	 * track of the last cylinder. Device id will be on track of the next
6138 	 * to last cylinder.
6139 	 */
6140 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
6141 	mutex_exit(SD_MUTEX(un));
6142 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
6143 	status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk,
6144 	    SD_PATH_DIRECT);
6145 	if (status != 0) {
6146 		goto error;
6147 	}
6148 
6149 	/* Validate the revision */
6150 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
6151 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
6152 		status = EINVAL;
6153 		goto error;
6154 	}
6155 
6156 	/* Calculate the checksum */
6157 	chksum = 0;
6158 	ip = (uint_t *)dkdevid;
6159 	for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int));
6160 	    i++) {
6161 		chksum ^= ip[i];
6162 	}
6163 
6164 	/* Compare the checksums */
6165 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
6166 		status = EINVAL;
6167 		goto error;
6168 	}
6169 
6170 	/* Validate the device id */
6171 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
6172 		status = EINVAL;
6173 		goto error;
6174 	}
6175 
6176 	/*
6177 	 * Store the device id in the driver soft state
6178 	 */
6179 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
6180 	tmpid = kmem_alloc(sz, KM_SLEEP);
6181 
6182 	mutex_enter(SD_MUTEX(un));
6183 
6184 	un->un_devid = tmpid;
6185 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
6186 
6187 	kmem_free(dkdevid, buffer_size);
6188 
6189 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
6190 
6191 	return (status);
6192 error:
6193 	mutex_enter(SD_MUTEX(un));
6194 	kmem_free(dkdevid, buffer_size);
6195 	return (status);
6196 }
6197 
6198 
6199 /*
6200  *    Function: sd_create_devid
6201  *
6202  * Description: This routine will fabricate the device id and write it
6203  *		to the disk.
6204  *
6205  *   Arguments: un - driver soft state (unit) structure
6206  *
6207  * Return Code: value of the fabricated device id
6208  *
6209  *     Context: Kernel Thread
6210  */
6211 
6212 static ddi_devid_t
6213 sd_create_devid(struct sd_lun *un)
6214 {
6215 	ASSERT(un != NULL);
6216 
6217 	/* Fabricate the devid */
6218 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
6219 	    == DDI_FAILURE) {
6220 		return (NULL);
6221 	}
6222 
6223 	/* Write the devid to disk */
6224 	if (sd_write_deviceid(un) != 0) {
6225 		ddi_devid_free(un->un_devid);
6226 		un->un_devid = NULL;
6227 	}
6228 
6229 	return (un->un_devid);
6230 }
6231 
6232 
6233 /*
6234  *    Function: sd_write_deviceid
6235  *
6236  * Description: This routine will write the device id to the disk
6237  *		reserved sector.
6238  *
6239  *   Arguments: un - driver soft state (unit) structure
6240  *
6241  * Return Code: EINVAL
6242  *		value returned by sd_send_scsi_cmd
6243  *
6244  *     Context: Kernel Thread
6245  */
6246 
6247 static int
6248 sd_write_deviceid(struct sd_lun *un)
6249 {
6250 	struct dk_devid		*dkdevid;
6251 	daddr_t			blk;
6252 	uint_t			*ip, chksum;
6253 	int			status;
6254 	int			i;
6255 
6256 	ASSERT(mutex_owned(SD_MUTEX(un)));
6257 
6258 	blk = sd_get_devid_block(un);
6259 	if (blk < 0)
6260 		return (-1);
6261 	mutex_exit(SD_MUTEX(un));
6262 
6263 	/* Allocate the buffer */
6264 	dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
6265 
6266 	/* Fill in the revision */
6267 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
6268 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
6269 
6270 	/* Copy in the device id */
6271 	mutex_enter(SD_MUTEX(un));
6272 	bcopy(un->un_devid, &dkdevid->dkd_devid,
6273 	    ddi_devid_sizeof(un->un_devid));
6274 	mutex_exit(SD_MUTEX(un));
6275 
6276 	/* Calculate the checksum */
6277 	chksum = 0;
6278 	ip = (uint_t *)dkdevid;
6279 	for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int));
6280 	    i++) {
6281 		chksum ^= ip[i];
6282 	}
6283 
6284 	/* Fill-in checksum */
6285 	DKD_FORMCHKSUM(chksum, dkdevid);
6286 
6287 	/* Write the reserved sector */
6288 	status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk,
6289 	    SD_PATH_DIRECT);
6290 
6291 	kmem_free(dkdevid, un->un_sys_blocksize);
6292 
6293 	mutex_enter(SD_MUTEX(un));
6294 	return (status);
6295 }
6296 
6297 
6298 /*
6299  *    Function: sd_check_vpd_page_support
6300  *
6301  * Description: This routine sends an inquiry command with the EVPD bit set and
6302  *		a page code of 0x00 to the device. It is used to determine which
6303  *		vital product pages are availible to find the devid. We are
6304  *		looking for pages 0x83 or 0x80.  If we return a negative 1, the
6305  *		device does not support that command.
6306  *
6307  *   Arguments: un  - driver soft state (unit) structure
6308  *
6309  * Return Code: 0 - success
6310  *		1 - check condition
6311  *
6312  *     Context: This routine can sleep.
6313  */
6314 
6315 static int
6316 sd_check_vpd_page_support(struct sd_lun *un)
6317 {
6318 	uchar_t	*page_list	= NULL;
6319 	uchar_t	page_length	= 0xff;	/* Use max possible length */
6320 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
6321 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
6322 	int    	rval		= 0;
6323 	int	counter;
6324 
6325 	ASSERT(un != NULL);
6326 	ASSERT(mutex_owned(SD_MUTEX(un)));
6327 
6328 	mutex_exit(SD_MUTEX(un));
6329 
6330 	/*
6331 	 * We'll set the page length to the maximum to save figuring it out
6332 	 * with an additional call.
6333 	 */
6334 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
6335 
6336 	rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd,
6337 	    page_code, NULL);
6338 
6339 	mutex_enter(SD_MUTEX(un));
6340 
6341 	/*
6342 	 * Now we must validate that the device accepted the command, as some
6343 	 * drives do not support it.  If the drive does support it, we will
6344 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
6345 	 * not, we return -1.
6346 	 */
6347 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
6348 		/* Loop to find one of the 2 pages we need */
6349 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
6350 
6351 		/*
6352 		 * Pages are returned in ascending order, and 0x83 is what we
6353 		 * are hoping for.
6354 		 */
6355 		while ((page_list[counter] <= 0x83) &&
6356 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
6357 		    VPD_HEAD_OFFSET))) {
6358 			/*
6359 			 * Add 3 because page_list[3] is the number of
6360 			 * pages minus 3
6361 			 */
6362 
6363 			switch (page_list[counter]) {
6364 			case 0x00:
6365 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
6366 				break;
6367 			case 0x80:
6368 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
6369 				break;
6370 			case 0x81:
6371 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
6372 				break;
6373 			case 0x82:
6374 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
6375 				break;
6376 			case 0x83:
6377 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
6378 				break;
6379 			}
6380 			counter++;
6381 		}
6382 
6383 	} else {
6384 		rval = -1;
6385 
6386 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
6387 		    "sd_check_vpd_page_support: This drive does not implement "
6388 		    "VPD pages.\n");
6389 	}
6390 
6391 	kmem_free(page_list, page_length);
6392 
6393 	return (rval);
6394 }
6395 
6396 
6397 /*
6398  *    Function: sd_setup_pm
6399  *
6400  * Description: Initialize Power Management on the device
6401  *
6402  *     Context: Kernel Thread
6403  */
6404 
6405 static void
6406 sd_setup_pm(struct sd_lun *un, dev_info_t *devi)
6407 {
6408 	uint_t	log_page_size;
6409 	uchar_t	*log_page_data;
6410 	int	rval;
6411 
6412 	/*
6413 	 * Since we are called from attach, holding a mutex for
6414 	 * un is unnecessary. Because some of the routines called
6415 	 * from here require SD_MUTEX to not be held, assert this
6416 	 * right up front.
6417 	 */
6418 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6419 	/*
6420 	 * Since the sd device does not have the 'reg' property,
6421 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
6422 	 * The following code is to tell cpr that this device
6423 	 * DOES need to be suspended and resumed.
6424 	 */
6425 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
6426 	    "pm-hardware-state", "needs-suspend-resume");
6427 
6428 	/*
6429 	 * This complies with the new power management framework
6430 	 * for certain desktop machines. Create the pm_components
6431 	 * property as a string array property.
6432 	 */
6433 	if (un->un_f_pm_supported) {
6434 		/*
6435 		 * not all devices have a motor, try it first.
6436 		 * some devices may return ILLEGAL REQUEST, some
6437 		 * will hang
6438 		 * The following START_STOP_UNIT is used to check if target
6439 		 * device has a motor.
6440 		 */
6441 		un->un_f_start_stop_supported = TRUE;
6442 		if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
6443 		    SD_PATH_DIRECT) != 0) {
6444 			un->un_f_start_stop_supported = FALSE;
6445 		}
6446 
6447 		/*
6448 		 * create pm properties anyways otherwise the parent can't
6449 		 * go to sleep
6450 		 */
6451 		(void) sd_create_pm_components(devi, un);
6452 		un->un_f_pm_is_enabled = TRUE;
6453 		return;
6454 	}
6455 
6456 	if (!un->un_f_log_sense_supported) {
6457 		un->un_power_level = SD_SPINDLE_ON;
6458 		un->un_f_pm_is_enabled = FALSE;
6459 		return;
6460 	}
6461 
6462 	rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE);
6463 
6464 #ifdef	SDDEBUG
6465 	if (sd_force_pm_supported) {
6466 		/* Force a successful result */
6467 		rval = 1;
6468 	}
6469 #endif
6470 
6471 	/*
6472 	 * If the start-stop cycle counter log page is not supported
6473 	 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0)
6474 	 * then we should not create the pm_components property.
6475 	 */
6476 	if (rval == -1) {
6477 		/*
6478 		 * Error.
6479 		 * Reading log sense failed, most likely this is
6480 		 * an older drive that does not support log sense.
6481 		 * If this fails auto-pm is not supported.
6482 		 */
6483 		un->un_power_level = SD_SPINDLE_ON;
6484 		un->un_f_pm_is_enabled = FALSE;
6485 
6486 	} else if (rval == 0) {
6487 		/*
6488 		 * Page not found.
6489 		 * The start stop cycle counter is implemented as page
6490 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6491 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6492 		 */
6493 		if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) {
6494 			/*
6495 			 * Page found, use this one.
6496 			 */
6497 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6498 			un->un_f_pm_is_enabled = TRUE;
6499 		} else {
6500 			/*
6501 			 * Error or page not found.
6502 			 * auto-pm is not supported for this device.
6503 			 */
6504 			un->un_power_level = SD_SPINDLE_ON;
6505 			un->un_f_pm_is_enabled = FALSE;
6506 		}
6507 	} else {
6508 		/*
6509 		 * Page found, use it.
6510 		 */
6511 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6512 		un->un_f_pm_is_enabled = TRUE;
6513 	}
6514 
6515 
6516 	if (un->un_f_pm_is_enabled == TRUE) {
6517 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6518 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6519 
6520 		rval = sd_send_scsi_LOG_SENSE(un, log_page_data,
6521 		    log_page_size, un->un_start_stop_cycle_page,
6522 		    0x01, 0, SD_PATH_DIRECT);
6523 #ifdef	SDDEBUG
6524 		if (sd_force_pm_supported) {
6525 			/* Force a successful result */
6526 			rval = 0;
6527 		}
6528 #endif
6529 
6530 		/*
6531 		 * If the Log sense for Page( Start/stop cycle counter page)
6532 		 * succeeds, then power managment is supported and we can
6533 		 * enable auto-pm.
6534 		 */
6535 		if (rval == 0)  {
6536 			(void) sd_create_pm_components(devi, un);
6537 		} else {
6538 			un->un_power_level = SD_SPINDLE_ON;
6539 			un->un_f_pm_is_enabled = FALSE;
6540 		}
6541 
6542 		kmem_free(log_page_data, log_page_size);
6543 	}
6544 }
6545 
6546 
6547 /*
6548  *    Function: sd_create_pm_components
6549  *
6550  * Description: Initialize PM property.
6551  *
6552  *     Context: Kernel thread context
6553  */
6554 
6555 static void
6556 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6557 {
6558 	char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL };
6559 
6560 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6561 
6562 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6563 	    "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) {
6564 		/*
6565 		 * When components are initially created they are idle,
6566 		 * power up any non-removables.
6567 		 * Note: the return value of pm_raise_power can't be used
6568 		 * for determining if PM should be enabled for this device.
6569 		 * Even if you check the return values and remove this
6570 		 * property created above, the PM framework will not honor the
6571 		 * change after the first call to pm_raise_power. Hence,
6572 		 * removal of that property does not help if pm_raise_power
6573 		 * fails. In the case of removable media, the start/stop
6574 		 * will fail if the media is not present.
6575 		 */
6576 		if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6577 		    SD_SPINDLE_ON) == DDI_SUCCESS)) {
6578 			mutex_enter(SD_MUTEX(un));
6579 			un->un_power_level = SD_SPINDLE_ON;
6580 			mutex_enter(&un->un_pm_mutex);
6581 			/* Set to on and not busy. */
6582 			un->un_pm_count = 0;
6583 		} else {
6584 			mutex_enter(SD_MUTEX(un));
6585 			un->un_power_level = SD_SPINDLE_OFF;
6586 			mutex_enter(&un->un_pm_mutex);
6587 			/* Set to off. */
6588 			un->un_pm_count = -1;
6589 		}
6590 		mutex_exit(&un->un_pm_mutex);
6591 		mutex_exit(SD_MUTEX(un));
6592 	} else {
6593 		un->un_power_level = SD_SPINDLE_ON;
6594 		un->un_f_pm_is_enabled = FALSE;
6595 	}
6596 }
6597 
6598 
6599 /*
6600  *    Function: sd_ddi_suspend
6601  *
6602  * Description: Performs system power-down operations. This includes
6603  *		setting the drive state to indicate its suspended so
6604  *		that no new commands will be accepted. Also, wait for
6605  *		all commands that are in transport or queued to a timer
6606  *		for retry to complete. All timeout threads are cancelled.
6607  *
6608  * Return Code: DDI_FAILURE or DDI_SUCCESS
6609  *
6610  *     Context: Kernel thread context
6611  */
6612 
6613 static int
6614 sd_ddi_suspend(dev_info_t *devi)
6615 {
6616 	struct	sd_lun	*un;
6617 	clock_t		wait_cmds_complete;
6618 
6619 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6620 	if (un == NULL) {
6621 		return (DDI_FAILURE);
6622 	}
6623 
6624 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6625 
6626 	mutex_enter(SD_MUTEX(un));
6627 
6628 	/* Return success if the device is already suspended. */
6629 	if (un->un_state == SD_STATE_SUSPENDED) {
6630 		mutex_exit(SD_MUTEX(un));
6631 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6632 		    "device already suspended, exiting\n");
6633 		return (DDI_SUCCESS);
6634 	}
6635 
6636 	/* Return failure if the device is being used by HA */
6637 	if (un->un_resvd_status &
6638 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6639 		mutex_exit(SD_MUTEX(un));
6640 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6641 		    "device in use by HA, exiting\n");
6642 		return (DDI_FAILURE);
6643 	}
6644 
6645 	/*
6646 	 * Return failure if the device is in a resource wait
6647 	 * or power changing state.
6648 	 */
6649 	if ((un->un_state == SD_STATE_RWAIT) ||
6650 	    (un->un_state == SD_STATE_PM_CHANGING)) {
6651 		mutex_exit(SD_MUTEX(un));
6652 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6653 		    "device in resource wait state, exiting\n");
6654 		return (DDI_FAILURE);
6655 	}
6656 
6657 
6658 	un->un_save_state = un->un_last_state;
6659 	New_state(un, SD_STATE_SUSPENDED);
6660 
6661 	/*
6662 	 * Wait for all commands that are in transport or queued to a timer
6663 	 * for retry to complete.
6664 	 *
6665 	 * While waiting, no new commands will be accepted or sent because of
6666 	 * the new state we set above.
6667 	 *
6668 	 * Wait till current operation has completed. If we are in the resource
6669 	 * wait state (with an intr outstanding) then we need to wait till the
6670 	 * intr completes and starts the next cmd. We want to wait for
6671 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6672 	 */
6673 	wait_cmds_complete = ddi_get_lbolt() +
6674 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
6675 
6676 	while (un->un_ncmds_in_transport != 0) {
6677 		/*
6678 		 * Fail if commands do not finish in the specified time.
6679 		 */
6680 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6681 		    wait_cmds_complete) == -1) {
6682 			/*
6683 			 * Undo the state changes made above. Everything
6684 			 * must go back to it's original value.
6685 			 */
6686 			Restore_state(un);
6687 			un->un_last_state = un->un_save_state;
6688 			/* Wake up any threads that might be waiting. */
6689 			cv_broadcast(&un->un_suspend_cv);
6690 			mutex_exit(SD_MUTEX(un));
6691 			SD_ERROR(SD_LOG_IO_PM, un,
6692 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
6693 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6694 			return (DDI_FAILURE);
6695 		}
6696 	}
6697 
6698 	/*
6699 	 * Cancel SCSI watch thread and timeouts, if any are active
6700 	 */
6701 
6702 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6703 		opaque_t temp_token = un->un_swr_token;
6704 		mutex_exit(SD_MUTEX(un));
6705 		scsi_watch_suspend(temp_token);
6706 		mutex_enter(SD_MUTEX(un));
6707 	}
6708 
6709 	if (un->un_reset_throttle_timeid != NULL) {
6710 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
6711 		un->un_reset_throttle_timeid = NULL;
6712 		mutex_exit(SD_MUTEX(un));
6713 		(void) untimeout(temp_id);
6714 		mutex_enter(SD_MUTEX(un));
6715 	}
6716 
6717 	if (un->un_dcvb_timeid != NULL) {
6718 		timeout_id_t temp_id = un->un_dcvb_timeid;
6719 		un->un_dcvb_timeid = NULL;
6720 		mutex_exit(SD_MUTEX(un));
6721 		(void) untimeout(temp_id);
6722 		mutex_enter(SD_MUTEX(un));
6723 	}
6724 
6725 	mutex_enter(&un->un_pm_mutex);
6726 	if (un->un_pm_timeid != NULL) {
6727 		timeout_id_t temp_id = un->un_pm_timeid;
6728 		un->un_pm_timeid = NULL;
6729 		mutex_exit(&un->un_pm_mutex);
6730 		mutex_exit(SD_MUTEX(un));
6731 		(void) untimeout(temp_id);
6732 		mutex_enter(SD_MUTEX(un));
6733 	} else {
6734 		mutex_exit(&un->un_pm_mutex);
6735 	}
6736 
6737 	if (un->un_retry_timeid != NULL) {
6738 		timeout_id_t temp_id = un->un_retry_timeid;
6739 		un->un_retry_timeid = NULL;
6740 		mutex_exit(SD_MUTEX(un));
6741 		(void) untimeout(temp_id);
6742 		mutex_enter(SD_MUTEX(un));
6743 	}
6744 
6745 	if (un->un_direct_priority_timeid != NULL) {
6746 		timeout_id_t temp_id = un->un_direct_priority_timeid;
6747 		un->un_direct_priority_timeid = NULL;
6748 		mutex_exit(SD_MUTEX(un));
6749 		(void) untimeout(temp_id);
6750 		mutex_enter(SD_MUTEX(un));
6751 	}
6752 
6753 	if (un->un_f_is_fibre == TRUE) {
6754 		/*
6755 		 * Remove callbacks for insert and remove events
6756 		 */
6757 		if (un->un_insert_event != NULL) {
6758 			mutex_exit(SD_MUTEX(un));
6759 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
6760 			mutex_enter(SD_MUTEX(un));
6761 			un->un_insert_event = NULL;
6762 		}
6763 
6764 		if (un->un_remove_event != NULL) {
6765 			mutex_exit(SD_MUTEX(un));
6766 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
6767 			mutex_enter(SD_MUTEX(un));
6768 			un->un_remove_event = NULL;
6769 		}
6770 	}
6771 
6772 	mutex_exit(SD_MUTEX(un));
6773 
6774 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6775 
6776 	return (DDI_SUCCESS);
6777 }
6778 
6779 
6780 /*
6781  *    Function: sd_ddi_pm_suspend
6782  *
6783  * Description: Set the drive state to low power.
6784  *		Someone else is required to actually change the drive
6785  *		power level.
6786  *
6787  *   Arguments: un - driver soft state (unit) structure
6788  *
6789  * Return Code: DDI_FAILURE or DDI_SUCCESS
6790  *
6791  *     Context: Kernel thread context
6792  */
6793 
6794 static int
6795 sd_ddi_pm_suspend(struct sd_lun *un)
6796 {
6797 	ASSERT(un != NULL);
6798 	SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n");
6799 
6800 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6801 	mutex_enter(SD_MUTEX(un));
6802 
6803 	/*
6804 	 * Exit if power management is not enabled for this device, or if
6805 	 * the device is being used by HA.
6806 	 */
6807 	if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6808 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6809 		mutex_exit(SD_MUTEX(un));
6810 		SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n");
6811 		return (DDI_SUCCESS);
6812 	}
6813 
6814 	SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n",
6815 	    un->un_ncmds_in_driver);
6816 
6817 	/*
6818 	 * See if the device is not busy, ie.:
6819 	 *    - we have no commands in the driver for this device
6820 	 *    - not waiting for resources
6821 	 */
6822 	if ((un->un_ncmds_in_driver == 0) &&
6823 	    (un->un_state != SD_STATE_RWAIT)) {
6824 		/*
6825 		 * The device is not busy, so it is OK to go to low power state.
6826 		 * Indicate low power, but rely on someone else to actually
6827 		 * change it.
6828 		 */
6829 		mutex_enter(&un->un_pm_mutex);
6830 		un->un_pm_count = -1;
6831 		mutex_exit(&un->un_pm_mutex);
6832 		un->un_power_level = SD_SPINDLE_OFF;
6833 	}
6834 
6835 	mutex_exit(SD_MUTEX(un));
6836 
6837 	SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n");
6838 
6839 	return (DDI_SUCCESS);
6840 }
6841 
6842 
6843 /*
6844  *    Function: sd_ddi_resume
6845  *
6846  * Description: Performs system power-up operations..
6847  *
6848  * Return Code: DDI_SUCCESS
6849  *		DDI_FAILURE
6850  *
6851  *     Context: Kernel thread context
6852  */
6853 
6854 static int
6855 sd_ddi_resume(dev_info_t *devi)
6856 {
6857 	struct	sd_lun	*un;
6858 
6859 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6860 	if (un == NULL) {
6861 		return (DDI_FAILURE);
6862 	}
6863 
6864 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6865 
6866 	mutex_enter(SD_MUTEX(un));
6867 	Restore_state(un);
6868 
6869 	/*
6870 	 * Restore the state which was saved to give the
6871 	 * the right state in un_last_state
6872 	 */
6873 	un->un_last_state = un->un_save_state;
6874 	/*
6875 	 * Note: throttle comes back at full.
6876 	 * Also note: this MUST be done before calling pm_raise_power
6877 	 * otherwise the system can get hung in biowait. The scenario where
6878 	 * this'll happen is under cpr suspend. Writing of the system
6879 	 * state goes through sddump, which writes 0 to un_throttle. If
6880 	 * writing the system state then fails, example if the partition is
6881 	 * too small, then cpr attempts a resume. If throttle isn't restored
6882 	 * from the saved value until after calling pm_raise_power then
6883 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6884 	 * in biowait.
6885 	 */
6886 	un->un_throttle = un->un_saved_throttle;
6887 
6888 	/*
6889 	 * The chance of failure is very rare as the only command done in power
6890 	 * entry point is START command when you transition from 0->1 or
6891 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6892 	 * which suspend was done. Ignore the return value as the resume should
6893 	 * not be failed. In the case of removable media the media need not be
6894 	 * inserted and hence there is a chance that raise power will fail with
6895 	 * media not present.
6896 	 */
6897 	if (un->un_f_attach_spinup) {
6898 		mutex_exit(SD_MUTEX(un));
6899 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
6900 		mutex_enter(SD_MUTEX(un));
6901 	}
6902 
6903 	/*
6904 	 * Don't broadcast to the suspend cv and therefore possibly
6905 	 * start I/O until after power has been restored.
6906 	 */
6907 	cv_broadcast(&un->un_suspend_cv);
6908 	cv_broadcast(&un->un_state_cv);
6909 
6910 	/* restart thread */
6911 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6912 		scsi_watch_resume(un->un_swr_token);
6913 	}
6914 
6915 #if (defined(__fibre))
6916 	if (un->un_f_is_fibre == TRUE) {
6917 		/*
6918 		 * Add callbacks for insert and remove events
6919 		 */
6920 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6921 			sd_init_event_callbacks(un);
6922 		}
6923 	}
6924 #endif
6925 
6926 	/*
6927 	 * Transport any pending commands to the target.
6928 	 *
6929 	 * If this is a low-activity device commands in queue will have to wait
6930 	 * until new commands come in, which may take awhile. Also, we
6931 	 * specifically don't check un_ncmds_in_transport because we know that
6932 	 * there really are no commands in progress after the unit was
6933 	 * suspended and we could have reached the throttle level, been
6934 	 * suspended, and have no new commands coming in for awhile. Highly
6935 	 * unlikely, but so is the low-activity disk scenario.
6936 	 */
6937 	ddi_xbuf_dispatch(un->un_xbuf_attr);
6938 
6939 	sd_start_cmds(un, NULL);
6940 	mutex_exit(SD_MUTEX(un));
6941 
6942 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6943 
6944 	return (DDI_SUCCESS);
6945 }
6946 
6947 
6948 /*
6949  *    Function: sd_ddi_pm_resume
6950  *
6951  * Description: Set the drive state to powered on.
6952  *		Someone else is required to actually change the drive
6953  *		power level.
6954  *
6955  *   Arguments: un - driver soft state (unit) structure
6956  *
6957  * Return Code: DDI_SUCCESS
6958  *
6959  *     Context: Kernel thread context
6960  */
6961 
6962 static int
6963 sd_ddi_pm_resume(struct sd_lun *un)
6964 {
6965 	ASSERT(un != NULL);
6966 
6967 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6968 	mutex_enter(SD_MUTEX(un));
6969 	un->un_power_level = SD_SPINDLE_ON;
6970 
6971 	ASSERT(!mutex_owned(&un->un_pm_mutex));
6972 	mutex_enter(&un->un_pm_mutex);
6973 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6974 		un->un_pm_count++;
6975 		ASSERT(un->un_pm_count == 0);
6976 		/*
6977 		 * Note: no longer do the cv_broadcast on un_suspend_cv. The
6978 		 * un_suspend_cv is for a system resume, not a power management
6979 		 * device resume. (4297749)
6980 		 *	 cv_broadcast(&un->un_suspend_cv);
6981 		 */
6982 	}
6983 	mutex_exit(&un->un_pm_mutex);
6984 	mutex_exit(SD_MUTEX(un));
6985 
6986 	return (DDI_SUCCESS);
6987 }
6988 
6989 
6990 /*
6991  *    Function: sd_pm_idletimeout_handler
6992  *
6993  * Description: A timer routine that's active only while a device is busy.
6994  *		The purpose is to extend slightly the pm framework's busy
6995  *		view of the device to prevent busy/idle thrashing for
6996  *		back-to-back commands. Do this by comparing the current time
6997  *		to the time at which the last command completed and when the
6998  *		difference is greater than sd_pm_idletime, call
6999  *		pm_idle_component. In addition to indicating idle to the pm
7000  *		framework, update the chain type to again use the internal pm
7001  *		layers of the driver.
7002  *
7003  *   Arguments: arg - driver soft state (unit) structure
7004  *
7005  *     Context: Executes in a timeout(9F) thread context
7006  */
7007 
7008 static void
7009 sd_pm_idletimeout_handler(void *arg)
7010 {
7011 	struct sd_lun *un = arg;
7012 
7013 	time_t	now;
7014 
7015 	mutex_enter(&sd_detach_mutex);
7016 	if (un->un_detach_count != 0) {
7017 		/* Abort if the instance is detaching */
7018 		mutex_exit(&sd_detach_mutex);
7019 		return;
7020 	}
7021 	mutex_exit(&sd_detach_mutex);
7022 
7023 	now = ddi_get_time();
7024 	/*
7025 	 * Grab both mutexes, in the proper order, since we're accessing
7026 	 * both PM and softstate variables.
7027 	 */
7028 	mutex_enter(SD_MUTEX(un));
7029 	mutex_enter(&un->un_pm_mutex);
7030 	if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
7031 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
7032 		/*
7033 		 * Update the chain types.
7034 		 * This takes affect on the next new command received.
7035 		 */
7036 		if (un->un_f_non_devbsize_supported) {
7037 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7038 		} else {
7039 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7040 		}
7041 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7042 
7043 		SD_TRACE(SD_LOG_IO_PM, un,
7044 		    "sd_pm_idletimeout_handler: idling device\n");
7045 		(void) pm_idle_component(SD_DEVINFO(un), 0);
7046 		un->un_pm_idle_timeid = NULL;
7047 	} else {
7048 		un->un_pm_idle_timeid =
7049 			timeout(sd_pm_idletimeout_handler, un,
7050 			(drv_usectohz((clock_t)300000))); /* 300 ms. */
7051 	}
7052 	mutex_exit(&un->un_pm_mutex);
7053 	mutex_exit(SD_MUTEX(un));
7054 }
7055 
7056 
7057 /*
7058  *    Function: sd_pm_timeout_handler
7059  *
7060  * Description: Callback to tell framework we are idle.
7061  *
7062  *     Context: timeout(9f) thread context.
7063  */
7064 
7065 static void
7066 sd_pm_timeout_handler(void *arg)
7067 {
7068 	struct sd_lun *un = arg;
7069 
7070 	(void) pm_idle_component(SD_DEVINFO(un), 0);
7071 	mutex_enter(&un->un_pm_mutex);
7072 	un->un_pm_timeid = NULL;
7073 	mutex_exit(&un->un_pm_mutex);
7074 }
7075 
7076 
7077 /*
7078  *    Function: sdpower
7079  *
7080  * Description: PM entry point.
7081  *
7082  * Return Code: DDI_SUCCESS
7083  *		DDI_FAILURE
7084  *
7085  *     Context: Kernel thread context
7086  */
7087 
7088 static int
7089 sdpower(dev_info_t *devi, int component, int level)
7090 {
7091 	struct sd_lun	*un;
7092 	int		instance;
7093 	int		rval = DDI_SUCCESS;
7094 	uint_t		i, log_page_size, maxcycles, ncycles;
7095 	uchar_t		*log_page_data;
7096 	int		log_sense_page;
7097 	int		medium_present;
7098 	time_t		intvlp;
7099 	dev_t		dev;
7100 	struct pm_trans_data	sd_pm_tran_data;
7101 	uchar_t		save_state;
7102 	int		sval;
7103 	uchar_t		state_before_pm;
7104 	int		got_semaphore_here;
7105 
7106 	instance = ddi_get_instance(devi);
7107 
7108 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
7109 	    (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) ||
7110 	    component != 0) {
7111 		return (DDI_FAILURE);
7112 	}
7113 
7114 	dev = sd_make_device(SD_DEVINFO(un));
7115 
7116 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
7117 
7118 	/*
7119 	 * Must synchronize power down with close.
7120 	 * Attempt to decrement/acquire the open/close semaphore,
7121 	 * but do NOT wait on it. If it's not greater than zero,
7122 	 * ie. it can't be decremented without waiting, then
7123 	 * someone else, either open or close, already has it
7124 	 * and the try returns 0. Use that knowledge here to determine
7125 	 * if it's OK to change the device power level.
7126 	 * Also, only increment it on exit if it was decremented, ie. gotten,
7127 	 * here.
7128 	 */
7129 	got_semaphore_here = sema_tryp(&un->un_semoclose);
7130 
7131 	mutex_enter(SD_MUTEX(un));
7132 
7133 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
7134 	    un->un_ncmds_in_driver);
7135 
7136 	/*
7137 	 * If un_ncmds_in_driver is non-zero it indicates commands are
7138 	 * already being processed in the driver, or if the semaphore was
7139 	 * not gotten here it indicates an open or close is being processed.
7140 	 * At the same time somebody is requesting to go low power which
7141 	 * can't happen, therefore we need to return failure.
7142 	 */
7143 	if ((level == SD_SPINDLE_OFF) &&
7144 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
7145 		mutex_exit(SD_MUTEX(un));
7146 
7147 		if (got_semaphore_here != 0) {
7148 			sema_v(&un->un_semoclose);
7149 		}
7150 		SD_TRACE(SD_LOG_IO_PM, un,
7151 		    "sdpower: exit, device has queued cmds.\n");
7152 		return (DDI_FAILURE);
7153 	}
7154 
7155 	/*
7156 	 * if it is OFFLINE that means the disk is completely dead
7157 	 * in our case we have to put the disk in on or off by sending commands
7158 	 * Of course that will fail anyway so return back here.
7159 	 *
7160 	 * Power changes to a device that's OFFLINE or SUSPENDED
7161 	 * are not allowed.
7162 	 */
7163 	if ((un->un_state == SD_STATE_OFFLINE) ||
7164 	    (un->un_state == SD_STATE_SUSPENDED)) {
7165 		mutex_exit(SD_MUTEX(un));
7166 
7167 		if (got_semaphore_here != 0) {
7168 			sema_v(&un->un_semoclose);
7169 		}
7170 		SD_TRACE(SD_LOG_IO_PM, un,
7171 		    "sdpower: exit, device is off-line.\n");
7172 		return (DDI_FAILURE);
7173 	}
7174 
7175 	/*
7176 	 * Change the device's state to indicate it's power level
7177 	 * is being changed. Do this to prevent a power off in the
7178 	 * middle of commands, which is especially bad on devices
7179 	 * that are really powered off instead of just spun down.
7180 	 */
7181 	state_before_pm = un->un_state;
7182 	un->un_state = SD_STATE_PM_CHANGING;
7183 
7184 	mutex_exit(SD_MUTEX(un));
7185 
7186 	/*
7187 	 * If "pm-capable" property is set to TRUE by HBA drivers,
7188 	 * bypass the following checking, otherwise, check the log
7189 	 * sense information for this device
7190 	 */
7191 	if ((level == SD_SPINDLE_OFF) && un->un_f_log_sense_supported) {
7192 		/*
7193 		 * Get the log sense information to understand whether the
7194 		 * the powercycle counts have gone beyond the threshhold.
7195 		 */
7196 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
7197 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
7198 
7199 		mutex_enter(SD_MUTEX(un));
7200 		log_sense_page = un->un_start_stop_cycle_page;
7201 		mutex_exit(SD_MUTEX(un));
7202 
7203 		rval = sd_send_scsi_LOG_SENSE(un, log_page_data,
7204 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
7205 #ifdef	SDDEBUG
7206 		if (sd_force_pm_supported) {
7207 			/* Force a successful result */
7208 			rval = 0;
7209 		}
7210 #endif
7211 		if (rval != 0) {
7212 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
7213 			    "Log Sense Failed\n");
7214 			kmem_free(log_page_data, log_page_size);
7215 			/* Cannot support power management on those drives */
7216 
7217 			if (got_semaphore_here != 0) {
7218 				sema_v(&un->un_semoclose);
7219 			}
7220 			/*
7221 			 * On exit put the state back to it's original value
7222 			 * and broadcast to anyone waiting for the power
7223 			 * change completion.
7224 			 */
7225 			mutex_enter(SD_MUTEX(un));
7226 			un->un_state = state_before_pm;
7227 			cv_broadcast(&un->un_suspend_cv);
7228 			mutex_exit(SD_MUTEX(un));
7229 			SD_TRACE(SD_LOG_IO_PM, un,
7230 			    "sdpower: exit, Log Sense Failed.\n");
7231 			return (DDI_FAILURE);
7232 		}
7233 
7234 		/*
7235 		 * From the page data - Convert the essential information to
7236 		 * pm_trans_data
7237 		 */
7238 		maxcycles =
7239 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
7240 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
7241 
7242 		sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
7243 
7244 		ncycles =
7245 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
7246 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
7247 
7248 		sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
7249 
7250 		for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
7251 			sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
7252 			    log_page_data[8+i];
7253 		}
7254 
7255 		kmem_free(log_page_data, log_page_size);
7256 
7257 		/*
7258 		 * Call pm_trans_check routine to get the Ok from
7259 		 * the global policy
7260 		 */
7261 
7262 		sd_pm_tran_data.format = DC_SCSI_FORMAT;
7263 		sd_pm_tran_data.un.scsi_cycles.flag = 0;
7264 
7265 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
7266 #ifdef	SDDEBUG
7267 		if (sd_force_pm_supported) {
7268 			/* Force a successful result */
7269 			rval = 1;
7270 		}
7271 #endif
7272 		switch (rval) {
7273 		case 0:
7274 			/*
7275 			 * Not Ok to Power cycle or error in parameters passed
7276 			 * Would have given the advised time to consider power
7277 			 * cycle. Based on the new intvlp parameter we are
7278 			 * supposed to pretend we are busy so that pm framework
7279 			 * will never call our power entry point. Because of
7280 			 * that install a timeout handler and wait for the
7281 			 * recommended time to elapse so that power management
7282 			 * can be effective again.
7283 			 *
7284 			 * To effect this behavior, call pm_busy_component to
7285 			 * indicate to the framework this device is busy.
7286 			 * By not adjusting un_pm_count the rest of PM in
7287 			 * the driver will function normally, and independant
7288 			 * of this but because the framework is told the device
7289 			 * is busy it won't attempt powering down until it gets
7290 			 * a matching idle. The timeout handler sends this.
7291 			 * Note: sd_pm_entry can't be called here to do this
7292 			 * because sdpower may have been called as a result
7293 			 * of a call to pm_raise_power from within sd_pm_entry.
7294 			 *
7295 			 * If a timeout handler is already active then
7296 			 * don't install another.
7297 			 */
7298 			mutex_enter(&un->un_pm_mutex);
7299 			if (un->un_pm_timeid == NULL) {
7300 				un->un_pm_timeid =
7301 				    timeout(sd_pm_timeout_handler,
7302 				    un, intvlp * drv_usectohz(1000000));
7303 				mutex_exit(&un->un_pm_mutex);
7304 				(void) pm_busy_component(SD_DEVINFO(un), 0);
7305 			} else {
7306 				mutex_exit(&un->un_pm_mutex);
7307 			}
7308 			if (got_semaphore_here != 0) {
7309 				sema_v(&un->un_semoclose);
7310 			}
7311 			/*
7312 			 * On exit put the state back to it's original value
7313 			 * and broadcast to anyone waiting for the power
7314 			 * change completion.
7315 			 */
7316 			mutex_enter(SD_MUTEX(un));
7317 			un->un_state = state_before_pm;
7318 			cv_broadcast(&un->un_suspend_cv);
7319 			mutex_exit(SD_MUTEX(un));
7320 
7321 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
7322 			    "trans check Failed, not ok to power cycle.\n");
7323 			return (DDI_FAILURE);
7324 
7325 		case -1:
7326 			if (got_semaphore_here != 0) {
7327 				sema_v(&un->un_semoclose);
7328 			}
7329 			/*
7330 			 * On exit put the state back to it's original value
7331 			 * and broadcast to anyone waiting for the power
7332 			 * change completion.
7333 			 */
7334 			mutex_enter(SD_MUTEX(un));
7335 			un->un_state = state_before_pm;
7336 			cv_broadcast(&un->un_suspend_cv);
7337 			mutex_exit(SD_MUTEX(un));
7338 			SD_TRACE(SD_LOG_IO_PM, un,
7339 			    "sdpower: exit, trans check command Failed.\n");
7340 			return (DDI_FAILURE);
7341 		}
7342 	}
7343 
7344 	if (level == SD_SPINDLE_OFF) {
7345 		/*
7346 		 * Save the last state... if the STOP FAILS we need it
7347 		 * for restoring
7348 		 */
7349 		mutex_enter(SD_MUTEX(un));
7350 		save_state = un->un_last_state;
7351 		/*
7352 		 * There must not be any cmds. getting processed
7353 		 * in the driver when we get here. Power to the
7354 		 * device is potentially going off.
7355 		 */
7356 		ASSERT(un->un_ncmds_in_driver == 0);
7357 		mutex_exit(SD_MUTEX(un));
7358 
7359 		/*
7360 		 * For now suspend the device completely before spindle is
7361 		 * turned off
7362 		 */
7363 		if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) {
7364 			if (got_semaphore_here != 0) {
7365 				sema_v(&un->un_semoclose);
7366 			}
7367 			/*
7368 			 * On exit put the state back to it's original value
7369 			 * and broadcast to anyone waiting for the power
7370 			 * change completion.
7371 			 */
7372 			mutex_enter(SD_MUTEX(un));
7373 			un->un_state = state_before_pm;
7374 			cv_broadcast(&un->un_suspend_cv);
7375 			mutex_exit(SD_MUTEX(un));
7376 			SD_TRACE(SD_LOG_IO_PM, un,
7377 			    "sdpower: exit, PM suspend Failed.\n");
7378 			return (DDI_FAILURE);
7379 		}
7380 	}
7381 
7382 	/*
7383 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
7384 	 * close, or strategy. Dump no long uses this routine, it uses it's
7385 	 * own code so it can be done in polled mode.
7386 	 */
7387 
7388 	medium_present = TRUE;
7389 
7390 	/*
7391 	 * When powering up, issue a TUR in case the device is at unit
7392 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
7393 	 * a deadlock on un_pm_busy_cv will occur.
7394 	 */
7395 	if (level == SD_SPINDLE_ON) {
7396 		(void) sd_send_scsi_TEST_UNIT_READY(un,
7397 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
7398 	}
7399 
7400 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
7401 	    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
7402 
7403 	sval = sd_send_scsi_START_STOP_UNIT(un,
7404 	    ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP),
7405 	    SD_PATH_DIRECT);
7406 	/* Command failed, check for media present. */
7407 	if ((sval == ENXIO) && un->un_f_has_removable_media) {
7408 		medium_present = FALSE;
7409 	}
7410 
7411 	/*
7412 	 * The conditions of interest here are:
7413 	 *   if a spindle off with media present fails,
7414 	 *	then restore the state and return an error.
7415 	 *   else if a spindle on fails,
7416 	 *	then return an error (there's no state to restore).
7417 	 * In all other cases we setup for the new state
7418 	 * and return success.
7419 	 */
7420 	switch (level) {
7421 	case SD_SPINDLE_OFF:
7422 		if ((medium_present == TRUE) && (sval != 0)) {
7423 			/* The stop command from above failed */
7424 			rval = DDI_FAILURE;
7425 			/*
7426 			 * The stop command failed, and we have media
7427 			 * present. Put the level back by calling the
7428 			 * sd_pm_resume() and set the state back to
7429 			 * it's previous value.
7430 			 */
7431 			(void) sd_ddi_pm_resume(un);
7432 			mutex_enter(SD_MUTEX(un));
7433 			un->un_last_state = save_state;
7434 			mutex_exit(SD_MUTEX(un));
7435 			break;
7436 		}
7437 		/*
7438 		 * The stop command from above succeeded.
7439 		 */
7440 		if (un->un_f_monitor_media_state) {
7441 			/*
7442 			 * Terminate watch thread in case of removable media
7443 			 * devices going into low power state. This is as per
7444 			 * the requirements of pm framework, otherwise commands
7445 			 * will be generated for the device (through watch
7446 			 * thread), even when the device is in low power state.
7447 			 */
7448 			mutex_enter(SD_MUTEX(un));
7449 			un->un_f_watcht_stopped = FALSE;
7450 			if (un->un_swr_token != NULL) {
7451 				opaque_t temp_token = un->un_swr_token;
7452 				un->un_f_watcht_stopped = TRUE;
7453 				un->un_swr_token = NULL;
7454 				mutex_exit(SD_MUTEX(un));
7455 				(void) scsi_watch_request_terminate(temp_token,
7456 				    SCSI_WATCH_TERMINATE_WAIT);
7457 			} else {
7458 				mutex_exit(SD_MUTEX(un));
7459 			}
7460 		}
7461 		break;
7462 
7463 	default:	/* The level requested is spindle on... */
7464 		/*
7465 		 * Legacy behavior: return success on a failed spinup
7466 		 * if there is no media in the drive.
7467 		 * Do this by looking at medium_present here.
7468 		 */
7469 		if ((sval != 0) && medium_present) {
7470 			/* The start command from above failed */
7471 			rval = DDI_FAILURE;
7472 			break;
7473 		}
7474 		/*
7475 		 * The start command from above succeeded
7476 		 * Resume the devices now that we have
7477 		 * started the disks
7478 		 */
7479 		(void) sd_ddi_pm_resume(un);
7480 
7481 		/*
7482 		 * Resume the watch thread since it was suspended
7483 		 * when the device went into low power mode.
7484 		 */
7485 		if (un->un_f_monitor_media_state) {
7486 			mutex_enter(SD_MUTEX(un));
7487 			if (un->un_f_watcht_stopped == TRUE) {
7488 				opaque_t temp_token;
7489 
7490 				un->un_f_watcht_stopped = FALSE;
7491 				mutex_exit(SD_MUTEX(un));
7492 				temp_token = scsi_watch_request_submit(
7493 				    SD_SCSI_DEVP(un),
7494 				    sd_check_media_time,
7495 				    SENSE_LENGTH, sd_media_watch_cb,
7496 				    (caddr_t)dev);
7497 				mutex_enter(SD_MUTEX(un));
7498 				un->un_swr_token = temp_token;
7499 			}
7500 			mutex_exit(SD_MUTEX(un));
7501 		}
7502 	}
7503 	if (got_semaphore_here != 0) {
7504 		sema_v(&un->un_semoclose);
7505 	}
7506 	/*
7507 	 * On exit put the state back to it's original value
7508 	 * and broadcast to anyone waiting for the power
7509 	 * change completion.
7510 	 */
7511 	mutex_enter(SD_MUTEX(un));
7512 	un->un_state = state_before_pm;
7513 	cv_broadcast(&un->un_suspend_cv);
7514 	mutex_exit(SD_MUTEX(un));
7515 
7516 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7517 
7518 	return (rval);
7519 }
7520 
7521 
7522 
7523 /*
7524  *    Function: sdattach
7525  *
7526  * Description: Driver's attach(9e) entry point function.
7527  *
7528  *   Arguments: devi - opaque device info handle
7529  *		cmd  - attach  type
7530  *
7531  * Return Code: DDI_SUCCESS
7532  *		DDI_FAILURE
7533  *
7534  *     Context: Kernel thread context
7535  */
7536 
7537 static int
7538 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7539 {
7540 	switch (cmd) {
7541 	case DDI_ATTACH:
7542 		return (sd_unit_attach(devi));
7543 	case DDI_RESUME:
7544 		return (sd_ddi_resume(devi));
7545 	default:
7546 		break;
7547 	}
7548 	return (DDI_FAILURE);
7549 }
7550 
7551 
7552 /*
7553  *    Function: sddetach
7554  *
7555  * Description: Driver's detach(9E) entry point function.
7556  *
7557  *   Arguments: devi - opaque device info handle
7558  *		cmd  - detach  type
7559  *
7560  * Return Code: DDI_SUCCESS
7561  *		DDI_FAILURE
7562  *
7563  *     Context: Kernel thread context
7564  */
7565 
7566 static int
7567 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7568 {
7569 	switch (cmd) {
7570 	case DDI_DETACH:
7571 		return (sd_unit_detach(devi));
7572 	case DDI_SUSPEND:
7573 		return (sd_ddi_suspend(devi));
7574 	default:
7575 		break;
7576 	}
7577 	return (DDI_FAILURE);
7578 }
7579 
7580 
7581 /*
7582  *     Function: sd_sync_with_callback
7583  *
7584  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7585  *		 state while the callback routine is active.
7586  *
7587  *    Arguments: un: softstate structure for the instance
7588  *
7589  *	Context: Kernel thread context
7590  */
7591 
7592 static void
7593 sd_sync_with_callback(struct sd_lun *un)
7594 {
7595 	ASSERT(un != NULL);
7596 
7597 	mutex_enter(SD_MUTEX(un));
7598 
7599 	ASSERT(un->un_in_callback >= 0);
7600 
7601 	while (un->un_in_callback > 0) {
7602 		mutex_exit(SD_MUTEX(un));
7603 		delay(2);
7604 		mutex_enter(SD_MUTEX(un));
7605 	}
7606 
7607 	mutex_exit(SD_MUTEX(un));
7608 }
7609 
7610 /*
7611  *    Function: sd_unit_attach
7612  *
7613  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7614  *		the soft state structure for the device and performs
7615  *		all necessary structure and device initializations.
7616  *
7617  *   Arguments: devi: the system's dev_info_t for the device.
7618  *
7619  * Return Code: DDI_SUCCESS if attach is successful.
7620  *		DDI_FAILURE if any part of the attach fails.
7621  *
7622  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7623  *		Kernel thread context only.  Can sleep.
7624  */
7625 
7626 static int
7627 sd_unit_attach(dev_info_t *devi)
7628 {
7629 	struct	scsi_device	*devp;
7630 	struct	sd_lun		*un;
7631 	char			*variantp;
7632 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
7633 	int	instance;
7634 	int	rval;
7635 	int	wc_enabled;
7636 	uint64_t	capacity;
7637 	uint_t		lbasize;
7638 
7639 	/*
7640 	 * Retrieve the target driver's private data area. This was set
7641 	 * up by the HBA.
7642 	 */
7643 	devp = ddi_get_driver_private(devi);
7644 
7645 	/*
7646 	 * Since we have no idea what state things were left in by the last
7647 	 * user of the device, set up some 'default' settings, ie. turn 'em
7648 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7649 	 * Do this before the scsi_probe, which sends an inquiry.
7650 	 * This is a fix for bug (4430280).
7651 	 * Of special importance is wide-xfer. The drive could have been left
7652 	 * in wide transfer mode by the last driver to communicate with it,
7653 	 * this includes us. If that's the case, and if the following is not
7654 	 * setup properly or we don't re-negotiate with the drive prior to
7655 	 * transferring data to/from the drive, it causes bus parity errors,
7656 	 * data overruns, and unexpected interrupts. This first occurred when
7657 	 * the fix for bug (4378686) was made.
7658 	 */
7659 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7660 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7661 	(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7662 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7663 
7664 	/*
7665 	 * Use scsi_probe() to issue an INQUIRY command to the device.
7666 	 * This call will allocate and fill in the scsi_inquiry structure
7667 	 * and point the sd_inq member of the scsi_device structure to it.
7668 	 * If the attach succeeds, then this memory will not be de-allocated
7669 	 * (via scsi_unprobe()) until the instance is detached.
7670 	 */
7671 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7672 		goto probe_failed;
7673 	}
7674 
7675 	/*
7676 	 * Check the device type as specified in the inquiry data and
7677 	 * claim it if it is of a type that we support.
7678 	 */
7679 	switch (devp->sd_inq->inq_dtype) {
7680 	case DTYPE_DIRECT:
7681 		break;
7682 	case DTYPE_RODIRECT:
7683 		break;
7684 	case DTYPE_OPTICAL:
7685 		break;
7686 	case DTYPE_NOTPRESENT:
7687 	default:
7688 		/* Unsupported device type; fail the attach. */
7689 		goto probe_failed;
7690 	}
7691 
7692 	/*
7693 	 * Allocate the soft state structure for this unit.
7694 	 *
7695 	 * We rely upon this memory being set to all zeroes by
7696 	 * ddi_soft_state_zalloc().  We assume that any member of the
7697 	 * soft state structure that is not explicitly initialized by
7698 	 * this routine will have a value of zero.
7699 	 */
7700 	instance = ddi_get_instance(devp->sd_dev);
7701 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7702 		goto probe_failed;
7703 	}
7704 
7705 	/*
7706 	 * Retrieve a pointer to the newly-allocated soft state.
7707 	 *
7708 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7709 	 * was successful, unless something has gone horribly wrong and the
7710 	 * ddi's soft state internals are corrupt (in which case it is
7711 	 * probably better to halt here than just fail the attach....)
7712 	 */
7713 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7714 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
7715 		    instance);
7716 		/*NOTREACHED*/
7717 	}
7718 
7719 	/*
7720 	 * Link the back ptr of the driver soft state to the scsi_device
7721 	 * struct for this lun.
7722 	 * Save a pointer to the softstate in the driver-private area of
7723 	 * the scsi_device struct.
7724 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7725 	 * we first set un->un_sd below.
7726 	 */
7727 	un->un_sd = devp;
7728 	devp->sd_private = (opaque_t)un;
7729 
7730 	/*
7731 	 * The following must be after devp is stored in the soft state struct.
7732 	 */
7733 #ifdef SDDEBUG
7734 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7735 	    "%s_unit_attach: un:0x%p instance:%d\n",
7736 	    ddi_driver_name(devi), un, instance);
7737 #endif
7738 
7739 	/*
7740 	 * Set up the device type and node type (for the minor nodes).
7741 	 * By default we assume that the device can at least support the
7742 	 * Common Command Set. Call it a CD-ROM if it reports itself
7743 	 * as a RODIRECT device.
7744 	 */
7745 	switch (devp->sd_inq->inq_dtype) {
7746 	case DTYPE_RODIRECT:
7747 		un->un_node_type = DDI_NT_CD_CHAN;
7748 		un->un_ctype	 = CTYPE_CDROM;
7749 		break;
7750 	case DTYPE_OPTICAL:
7751 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7752 		un->un_ctype	 = CTYPE_ROD;
7753 		break;
7754 	default:
7755 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7756 		un->un_ctype	 = CTYPE_CCS;
7757 		break;
7758 	}
7759 
7760 	/*
7761 	 * Try to read the interconnect type from the HBA.
7762 	 *
7763 	 * Note: This driver is currently compiled as two binaries, a parallel
7764 	 * scsi version (sd) and a fibre channel version (ssd). All functional
7765 	 * differences are determined at compile time. In the future a single
7766 	 * binary will be provided and the inteconnect type will be used to
7767 	 * differentiate between fibre and parallel scsi behaviors. At that time
7768 	 * it will be necessary for all fibre channel HBAs to support this
7769 	 * property.
7770 	 *
7771 	 * set un_f_is_fiber to TRUE ( default fiber )
7772 	 */
7773 	un->un_f_is_fibre = TRUE;
7774 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7775 	case INTERCONNECT_SSA:
7776 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
7777 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7778 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7779 		break;
7780 	case INTERCONNECT_PARALLEL:
7781 		un->un_f_is_fibre = FALSE;
7782 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7783 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7784 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7785 		break;
7786 	case INTERCONNECT_FIBRE:
7787 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7788 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7789 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7790 		break;
7791 	case INTERCONNECT_FABRIC:
7792 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7793 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
7794 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7795 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7796 		break;
7797 	default:
7798 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7799 		/*
7800 		 * The HBA does not support the "interconnect-type" property
7801 		 * (or did not provide a recognized type).
7802 		 *
7803 		 * Note: This will be obsoleted when a single fibre channel
7804 		 * and parallel scsi driver is delivered. In the meantime the
7805 		 * interconnect type will be set to the platform default.If that
7806 		 * type is not parallel SCSI, it means that we should be
7807 		 * assuming "ssd" semantics. However, here this also means that
7808 		 * the FC HBA is not supporting the "interconnect-type" property
7809 		 * like we expect it to, so log this occurrence.
7810 		 */
7811 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7812 		if (!SD_IS_PARALLEL_SCSI(un)) {
7813 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7814 			    "sd_unit_attach: un:0x%p Assuming "
7815 			    "INTERCONNECT_FIBRE\n", un);
7816 		} else {
7817 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7818 			    "sd_unit_attach: un:0x%p Assuming "
7819 			    "INTERCONNECT_PARALLEL\n", un);
7820 			un->un_f_is_fibre = FALSE;
7821 		}
7822 #else
7823 		/*
7824 		 * Note: This source will be implemented when a single fibre
7825 		 * channel and parallel scsi driver is delivered. The default
7826 		 * will be to assume that if a device does not support the
7827 		 * "interconnect-type" property it is a parallel SCSI HBA and
7828 		 * we will set the interconnect type for parallel scsi.
7829 		 */
7830 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7831 		un->un_f_is_fibre = FALSE;
7832 #endif
7833 		break;
7834 	}
7835 
7836 	if (un->un_f_is_fibre == TRUE) {
7837 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7838 			SCSI_VERSION_3) {
7839 			switch (un->un_interconnect_type) {
7840 			case SD_INTERCONNECT_FIBRE:
7841 			case SD_INTERCONNECT_SSA:
7842 				un->un_node_type = DDI_NT_BLOCK_WWN;
7843 				break;
7844 			default:
7845 				break;
7846 			}
7847 		}
7848 	}
7849 
7850 	/*
7851 	 * Initialize the Request Sense command for the target
7852 	 */
7853 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7854 		goto alloc_rqs_failed;
7855 	}
7856 
7857 	/*
7858 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7859 	 * with seperate binary for sd and ssd.
7860 	 *
7861 	 * x86 has 1 binary, un_retry_count is set base on connection type.
7862 	 * The hardcoded values will go away when Sparc uses 1 binary
7863 	 * for sd and ssd.  This hardcoded values need to match
7864 	 * SD_RETRY_COUNT in sddef.h
7865 	 * The value used is base on interconnect type.
7866 	 * fibre = 3, parallel = 5
7867 	 */
7868 #if defined(__i386) || defined(__amd64)
7869 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7870 #else
7871 	un->un_retry_count = SD_RETRY_COUNT;
7872 #endif
7873 
7874 	/*
7875 	 * Set the per disk retry count to the default number of retries
7876 	 * for disks and CDROMs. This value can be overridden by the
7877 	 * disk property list or an entry in sd.conf.
7878 	 */
7879 	un->un_notready_retry_count =
7880 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7881 			: DISK_NOT_READY_RETRY_COUNT(un);
7882 
7883 	/*
7884 	 * Set the busy retry count to the default value of un_retry_count.
7885 	 * This can be overridden by entries in sd.conf or the device
7886 	 * config table.
7887 	 */
7888 	un->un_busy_retry_count = un->un_retry_count;
7889 
7890 	/*
7891 	 * Init the reset threshold for retries.  This number determines
7892 	 * how many retries must be performed before a reset can be issued
7893 	 * (for certain error conditions). This can be overridden by entries
7894 	 * in sd.conf or the device config table.
7895 	 */
7896 	un->un_reset_retry_count = (un->un_retry_count / 2);
7897 
7898 	/*
7899 	 * Set the victim_retry_count to the default un_retry_count
7900 	 */
7901 	un->un_victim_retry_count = (2 * un->un_retry_count);
7902 
7903 	/*
7904 	 * Set the reservation release timeout to the default value of
7905 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
7906 	 * device config table.
7907 	 */
7908 	un->un_reserve_release_time = 5;
7909 
7910 	/*
7911 	 * Set up the default maximum transfer size. Note that this may
7912 	 * get updated later in the attach, when setting up default wide
7913 	 * operations for disks.
7914 	 */
7915 #if defined(__i386) || defined(__amd64)
7916 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7917 #else
7918 	un->un_max_xfer_size = (uint_t)maxphys;
7919 #endif
7920 
7921 	/*
7922 	 * Get "allow bus device reset" property (defaults to "enabled" if
7923 	 * the property was not defined). This is to disable bus resets for
7924 	 * certain kinds of error recovery. Note: In the future when a run-time
7925 	 * fibre check is available the soft state flag should default to
7926 	 * enabled.
7927 	 */
7928 	if (un->un_f_is_fibre == TRUE) {
7929 		un->un_f_allow_bus_device_reset = TRUE;
7930 	} else {
7931 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7932 			"allow-bus-device-reset", 1) != 0) {
7933 			un->un_f_allow_bus_device_reset = TRUE;
7934 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7935 			"sd_unit_attach: un:0x%p Bus device reset enabled\n",
7936 				un);
7937 		} else {
7938 			un->un_f_allow_bus_device_reset = FALSE;
7939 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7940 			"sd_unit_attach: un:0x%p Bus device reset disabled\n",
7941 				un);
7942 		}
7943 	}
7944 
7945 	/*
7946 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
7947 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
7948 	 *
7949 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
7950 	 * property. The new "variant" property with a value of "atapi" has been
7951 	 * introduced so that future 'variants' of standard SCSI behavior (like
7952 	 * atapi) could be specified by the underlying HBA drivers by supplying
7953 	 * a new value for the "variant" property, instead of having to define a
7954 	 * new property.
7955 	 */
7956 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7957 		un->un_f_cfg_is_atapi = TRUE;
7958 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7959 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
7960 	}
7961 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7962 	    &variantp) == DDI_PROP_SUCCESS) {
7963 		if (strcmp(variantp, "atapi") == 0) {
7964 			un->un_f_cfg_is_atapi = TRUE;
7965 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7966 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
7967 		}
7968 		ddi_prop_free(variantp);
7969 	}
7970 
7971 	un->un_cmd_timeout	= SD_IO_TIME;
7972 
7973 	/* Info on current states, statuses, etc. (Updated frequently) */
7974 	un->un_state		= SD_STATE_NORMAL;
7975 	un->un_last_state	= SD_STATE_NORMAL;
7976 
7977 	/* Control & status info for command throttling */
7978 	un->un_throttle		= sd_max_throttle;
7979 	un->un_saved_throttle	= sd_max_throttle;
7980 	un->un_min_throttle	= sd_min_throttle;
7981 
7982 	if (un->un_f_is_fibre == TRUE) {
7983 		un->un_f_use_adaptive_throttle = TRUE;
7984 	} else {
7985 		un->un_f_use_adaptive_throttle = FALSE;
7986 	}
7987 
7988 	/* Removable media support. */
7989 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7990 	un->un_mediastate		= DKIO_NONE;
7991 	un->un_specified_mediastate	= DKIO_NONE;
7992 
7993 	/* CVs for suspend/resume (PM or DR) */
7994 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7995 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7996 
7997 	/* Power management support. */
7998 	un->un_power_level = SD_SPINDLE_UNINIT;
7999 
8000 	cv_init(&un->un_wcc_cv,   NULL, CV_DRIVER, NULL);
8001 	un->un_f_wcc_inprog = 0;
8002 
8003 	/*
8004 	 * The open/close semaphore is used to serialize threads executing
8005 	 * in the driver's open & close entry point routines for a given
8006 	 * instance.
8007 	 */
8008 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
8009 
8010 	/*
8011 	 * The conf file entry and softstate variable is a forceful override,
8012 	 * meaning a non-zero value must be entered to change the default.
8013 	 */
8014 	un->un_f_disksort_disabled = FALSE;
8015 
8016 	/*
8017 	 * Retrieve the properties from the static driver table or the driver
8018 	 * configuration file (.conf) for this unit and update the soft state
8019 	 * for the device as needed for the indicated properties.
8020 	 * Note: the property configuration needs to occur here as some of the
8021 	 * following routines may have dependancies on soft state flags set
8022 	 * as part of the driver property configuration.
8023 	 */
8024 	sd_read_unit_properties(un);
8025 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8026 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
8027 
8028 	/*
8029 	 * Only if a device has "hotpluggable" property, it is
8030 	 * treated as hotpluggable device. Otherwise, it is
8031 	 * regarded as non-hotpluggable one.
8032 	 */
8033 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "hotpluggable",
8034 	    -1) != -1) {
8035 		un->un_f_is_hotpluggable = TRUE;
8036 	}
8037 
8038 	/*
8039 	 * set unit's attributes(flags) according to "hotpluggable" and
8040 	 * RMB bit in INQUIRY data.
8041 	 */
8042 	sd_set_unit_attributes(un, devi);
8043 
8044 	/*
8045 	 * By default, we mark the capacity, lbasize, and geometry
8046 	 * as invalid. Only if we successfully read a valid capacity
8047 	 * will we update the un_blockcount and un_tgt_blocksize with the
8048 	 * valid values (the geometry will be validated later).
8049 	 */
8050 	un->un_f_blockcount_is_valid	= FALSE;
8051 	un->un_f_tgt_blocksize_is_valid	= FALSE;
8052 	un->un_f_geometry_is_valid	= FALSE;
8053 
8054 	/*
8055 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
8056 	 * otherwise.
8057 	 */
8058 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
8059 	un->un_blockcount = 0;
8060 
8061 	/*
8062 	 * Set up the per-instance info needed to determine the correct
8063 	 * CDBs and other info for issuing commands to the target.
8064 	 */
8065 	sd_init_cdb_limits(un);
8066 
8067 	/*
8068 	 * Set up the IO chains to use, based upon the target type.
8069 	 */
8070 	if (un->un_f_non_devbsize_supported) {
8071 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
8072 	} else {
8073 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
8074 	}
8075 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
8076 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
8077 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
8078 
8079 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
8080 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
8081 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
8082 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
8083 
8084 
8085 	if (ISCD(un)) {
8086 		un->un_additional_codes = sd_additional_codes;
8087 	} else {
8088 		un->un_additional_codes = NULL;
8089 	}
8090 
8091 	/*
8092 	 * Create the kstats here so they can be available for attach-time
8093 	 * routines that send commands to the unit (either polled or via
8094 	 * sd_send_scsi_cmd).
8095 	 *
8096 	 * Note: This is a critical sequence that needs to be maintained:
8097 	 *	1) Instantiate the kstats here, before any routines using the
8098 	 *	   iopath (i.e. sd_send_scsi_cmd).
8099 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8100 	 *	   stats (sd_set_pstats), following sd_validate_geometry(),
8101 	 *	   sd_register_devid(), and sd_cache_control().
8102 	 */
8103 
8104 	un->un_stats = kstat_create(sd_label, instance,
8105 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
8106 	if (un->un_stats != NULL) {
8107 		un->un_stats->ks_lock = SD_MUTEX(un);
8108 		kstat_install(un->un_stats);
8109 	}
8110 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8111 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
8112 
8113 	sd_create_errstats(un, instance);
8114 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8115 	    "sd_unit_attach: un:0x%p errstats created\n", un);
8116 
8117 	/*
8118 	 * The following if/else code was relocated here from below as part
8119 	 * of the fix for bug (4430280). However with the default setup added
8120 	 * on entry to this routine, it's no longer absolutely necessary for
8121 	 * this to be before the call to sd_spin_up_unit.
8122 	 */
8123 	if (SD_IS_PARALLEL_SCSI(un)) {
8124 		/*
8125 		 * If SCSI-2 tagged queueing is supported by the target
8126 		 * and by the host adapter then we will enable it.
8127 		 */
8128 		un->un_tagflags = 0;
8129 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) &&
8130 		    (devp->sd_inq->inq_cmdque) &&
8131 		    (un->un_f_arq_enabled == TRUE)) {
8132 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
8133 			    1, 1) == 1) {
8134 				un->un_tagflags = FLAG_STAG;
8135 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8136 				    "sd_unit_attach: un:0x%p tag queueing "
8137 				    "enabled\n", un);
8138 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
8139 			    "untagged-qing", 0) == 1) {
8140 				un->un_f_opt_queueing = TRUE;
8141 				un->un_saved_throttle = un->un_throttle =
8142 				    min(un->un_throttle, 3);
8143 			} else {
8144 				un->un_f_opt_queueing = FALSE;
8145 				un->un_saved_throttle = un->un_throttle = 1;
8146 			}
8147 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
8148 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
8149 			/* The Host Adapter supports internal queueing. */
8150 			un->un_f_opt_queueing = TRUE;
8151 			un->un_saved_throttle = un->un_throttle =
8152 			    min(un->un_throttle, 3);
8153 		} else {
8154 			un->un_f_opt_queueing = FALSE;
8155 			un->un_saved_throttle = un->un_throttle = 1;
8156 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8157 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
8158 		}
8159 
8160 
8161 		/* Setup or tear down default wide operations for disks */
8162 
8163 		/*
8164 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
8165 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
8166 		 * system and be set to different values. In the future this
8167 		 * code may need to be updated when the ssd module is
8168 		 * obsoleted and removed from the system. (4299588)
8169 		 */
8170 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) &&
8171 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
8172 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
8173 			    1, 1) == 1) {
8174 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8175 				    "sd_unit_attach: un:0x%p Wide Transfer "
8176 				    "enabled\n", un);
8177 			}
8178 
8179 			/*
8180 			 * If tagged queuing has also been enabled, then
8181 			 * enable large xfers
8182 			 */
8183 			if (un->un_saved_throttle == sd_max_throttle) {
8184 				un->un_max_xfer_size =
8185 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8186 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
8187 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8188 				    "sd_unit_attach: un:0x%p max transfer "
8189 				    "size=0x%x\n", un, un->un_max_xfer_size);
8190 			}
8191 		} else {
8192 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
8193 			    0, 1) == 1) {
8194 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8195 				    "sd_unit_attach: un:0x%p "
8196 				    "Wide Transfer disabled\n", un);
8197 			}
8198 		}
8199 	} else {
8200 		un->un_tagflags = FLAG_STAG;
8201 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
8202 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
8203 	}
8204 
8205 	/*
8206 	 * If this target supports LUN reset, try to enable it.
8207 	 */
8208 	if (un->un_f_lun_reset_enabled) {
8209 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
8210 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8211 			    "un:0x%p lun_reset capability set\n", un);
8212 		} else {
8213 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8214 			    "un:0x%p lun-reset capability not set\n", un);
8215 		}
8216 	}
8217 
8218 	/*
8219 	 * At this point in the attach, we have enough info in the
8220 	 * soft state to be able to issue commands to the target.
8221 	 *
8222 	 * All command paths used below MUST issue their commands as
8223 	 * SD_PATH_DIRECT. This is important as intermediate layers
8224 	 * are not all initialized yet (such as PM).
8225 	 */
8226 
8227 	/*
8228 	 * Send a TEST UNIT READY command to the device. This should clear
8229 	 * any outstanding UNIT ATTENTION that may be present.
8230 	 *
8231 	 * Note: Don't check for success, just track if there is a reservation,
8232 	 * this is a throw away command to clear any unit attentions.
8233 	 *
8234 	 * Note: This MUST be the first command issued to the target during
8235 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
8236 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
8237 	 * with attempts at spinning up a device with no media.
8238 	 */
8239 	if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) {
8240 		reservation_flag = SD_TARGET_IS_RESERVED;
8241 	}
8242 
8243 	/*
8244 	 * If the device is NOT a removable media device, attempt to spin
8245 	 * it up (using the START_STOP_UNIT command) and read its capacity
8246 	 * (using the READ CAPACITY command).  Note, however, that either
8247 	 * of these could fail and in some cases we would continue with
8248 	 * the attach despite the failure (see below).
8249 	 */
8250 	if (un->un_f_descr_format_supported) {
8251 		switch (sd_spin_up_unit(un)) {
8252 		case 0:
8253 			/*
8254 			 * Spin-up was successful; now try to read the
8255 			 * capacity.  If successful then save the results
8256 			 * and mark the capacity & lbasize as valid.
8257 			 */
8258 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8259 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
8260 
8261 			switch (sd_send_scsi_READ_CAPACITY(un, &capacity,
8262 			    &lbasize, SD_PATH_DIRECT)) {
8263 			case 0: {
8264 				if (capacity > DK_MAX_BLOCKS) {
8265 #ifdef _LP64
8266 					/*
8267 					 * Enable descriptor format sense data
8268 					 * so that we can get 64 bit sense
8269 					 * data fields.
8270 					 */
8271 					sd_enable_descr_sense(un);
8272 #else
8273 					/* 32-bit kernels can't handle this */
8274 					scsi_log(SD_DEVINFO(un),
8275 					    sd_label, CE_WARN,
8276 					    "disk has %llu blocks, which "
8277 					    "is too large for a 32-bit "
8278 					    "kernel", capacity);
8279 					goto spinup_failed;
8280 #endif
8281 				}
8282 
8283 				/*
8284 				 * Here it's not necessary to check the case:
8285 				 * the capacity of the device is bigger than
8286 				 * what the max hba cdb can support. Because
8287 				 * sd_send_scsi_READ_CAPACITY will retrieve
8288 				 * the capacity by sending USCSI command, which
8289 				 * is constrained by the max hba cdb. Actually,
8290 				 * sd_send_scsi_READ_CAPACITY will return
8291 				 * EINVAL when using bigger cdb than required
8292 				 * cdb length. Will handle this case in
8293 				 * "case EINVAL".
8294 				 */
8295 
8296 				/*
8297 				 * The following relies on
8298 				 * sd_send_scsi_READ_CAPACITY never
8299 				 * returning 0 for capacity and/or lbasize.
8300 				 */
8301 				sd_update_block_info(un, lbasize, capacity);
8302 
8303 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8304 				    "sd_unit_attach: un:0x%p capacity = %ld "
8305 				    "blocks; lbasize= %ld.\n", un,
8306 				    un->un_blockcount, un->un_tgt_blocksize);
8307 
8308 				break;
8309 			}
8310 			case EINVAL:
8311 				/*
8312 				 * In the case where the max-cdb-length property
8313 				 * is smaller than the required CDB length for
8314 				 * a SCSI device, a target driver can fail to
8315 				 * attach to that device.
8316 				 */
8317 				scsi_log(SD_DEVINFO(un),
8318 				    sd_label, CE_WARN,
8319 				    "disk capacity is too large "
8320 				    "for current cdb length");
8321 				goto spinup_failed;
8322 			case EACCES:
8323 				/*
8324 				 * Should never get here if the spin-up
8325 				 * succeeded, but code it in anyway.
8326 				 * From here, just continue with the attach...
8327 				 */
8328 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8329 				    "sd_unit_attach: un:0x%p "
8330 				    "sd_send_scsi_READ_CAPACITY "
8331 				    "returned reservation conflict\n", un);
8332 				reservation_flag = SD_TARGET_IS_RESERVED;
8333 				break;
8334 			default:
8335 				/*
8336 				 * Likewise, should never get here if the
8337 				 * spin-up succeeded. Just continue with
8338 				 * the attach...
8339 				 */
8340 				break;
8341 			}
8342 			break;
8343 		case EACCES:
8344 			/*
8345 			 * Device is reserved by another host.  In this case
8346 			 * we could not spin it up or read the capacity, but
8347 			 * we continue with the attach anyway.
8348 			 */
8349 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8350 			    "sd_unit_attach: un:0x%p spin-up reservation "
8351 			    "conflict.\n", un);
8352 			reservation_flag = SD_TARGET_IS_RESERVED;
8353 			break;
8354 		default:
8355 			/* Fail the attach if the spin-up failed. */
8356 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8357 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8358 			goto spinup_failed;
8359 		}
8360 	}
8361 
8362 	/*
8363 	 * Check to see if this is a MMC drive
8364 	 */
8365 	if (ISCD(un)) {
8366 		sd_set_mmc_caps(un);
8367 	}
8368 
8369 	/*
8370 	 * Create the minor nodes for the device.
8371 	 * Note: If we want to support fdisk on both sparc and intel, this will
8372 	 * have to separate out the notion that VTOC8 is always sparc, and
8373 	 * VTOC16 is always intel (tho these can be the defaults).  The vtoc
8374 	 * type will have to be determined at run-time, and the fdisk
8375 	 * partitioning will have to have been read & set up before we
8376 	 * create the minor nodes. (any other inits (such as kstats) that
8377 	 * also ought to be done before creating the minor nodes?) (Doesn't
8378 	 * setting up the minor nodes kind of imply that we're ready to
8379 	 * handle an open from userland?)
8380 	 */
8381 	if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) {
8382 		goto create_minor_nodes_failed;
8383 	}
8384 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8385 	    "sd_unit_attach: un:0x%p minor nodes created\n", un);
8386 
8387 	/*
8388 	 * Add a zero-length attribute to tell the world we support
8389 	 * kernel ioctls (for layered drivers)
8390 	 */
8391 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8392 	    DDI_KERNEL_IOCTL, NULL, 0);
8393 
8394 	/*
8395 	 * Add a boolean property to tell the world we support
8396 	 * the B_FAILFAST flag (for layered drivers)
8397 	 */
8398 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8399 	    "ddi-failfast-supported", NULL, 0);
8400 
8401 	/*
8402 	 * Initialize power management
8403 	 */
8404 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8405 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8406 	sd_setup_pm(un, devi);
8407 	if (un->un_f_pm_is_enabled == FALSE) {
8408 		/*
8409 		 * For performance, point to a jump table that does
8410 		 * not include pm.
8411 		 * The direct and priority chains don't change with PM.
8412 		 *
8413 		 * Note: this is currently done based on individual device
8414 		 * capabilities. When an interface for determining system
8415 		 * power enabled state becomes available, or when additional
8416 		 * layers are added to the command chain, these values will
8417 		 * have to be re-evaluated for correctness.
8418 		 */
8419 		if (un->un_f_non_devbsize_supported) {
8420 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8421 		} else {
8422 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8423 		}
8424 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8425 	}
8426 
8427 	/*
8428 	 * This property is set to 0 by HA software to avoid retries
8429 	 * on a reserved disk. (The preferred property name is
8430 	 * "retry-on-reservation-conflict") (1189689)
8431 	 *
8432 	 * Note: The use of a global here can have unintended consequences. A
8433 	 * per instance variable is preferrable to match the capabilities of
8434 	 * different underlying hba's (4402600)
8435 	 */
8436 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8437 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8438 	    sd_retry_on_reservation_conflict);
8439 	if (sd_retry_on_reservation_conflict != 0) {
8440 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8441 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8442 		    sd_retry_on_reservation_conflict);
8443 	}
8444 
8445 	/* Set up options for QFULL handling. */
8446 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8447 	    "qfull-retries", -1)) != -1) {
8448 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8449 		    rval, 1);
8450 	}
8451 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8452 	    "qfull-retry-interval", -1)) != -1) {
8453 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8454 		    rval, 1);
8455 	}
8456 
8457 	/*
8458 	 * This just prints a message that announces the existence of the
8459 	 * device. The message is always printed in the system logfile, but
8460 	 * only appears on the console if the system is booted with the
8461 	 * -v (verbose) argument.
8462 	 */
8463 	ddi_report_dev(devi);
8464 
8465 	/*
8466 	 * The framework calls driver attach routines single-threaded
8467 	 * for a given instance.  However we still acquire SD_MUTEX here
8468 	 * because this required for calling the sd_validate_geometry()
8469 	 * and sd_register_devid() functions.
8470 	 */
8471 	mutex_enter(SD_MUTEX(un));
8472 	un->un_f_geometry_is_valid = FALSE;
8473 	un->un_mediastate = DKIO_NONE;
8474 	un->un_reserved = -1;
8475 
8476 	/*
8477 	 * Read and validate the device's geometry (ie, disk label)
8478 	 * A new unformatted drive will not have a valid geometry, but
8479 	 * the driver needs to successfully attach to this device so
8480 	 * the drive can be formatted via ioctls.
8481 	 */
8482 	if (((sd_validate_geometry(un, SD_PATH_DIRECT) ==
8483 	    ENOTSUP)) &&
8484 	    (un->un_blockcount < DK_MAX_BLOCKS)) {
8485 		/*
8486 		 * We found a small disk with an EFI label on it;
8487 		 * we need to fix up the minor nodes accordingly.
8488 		 */
8489 		ddi_remove_minor_node(devi, "h");
8490 		ddi_remove_minor_node(devi, "h,raw");
8491 		(void) ddi_create_minor_node(devi, "wd",
8492 		    S_IFBLK,
8493 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8494 		    un->un_node_type, NULL);
8495 		(void) ddi_create_minor_node(devi, "wd,raw",
8496 		    S_IFCHR,
8497 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8498 		    un->un_node_type, NULL);
8499 	}
8500 
8501 	/*
8502 	 * Read and initialize the devid for the unit.
8503 	 */
8504 	ASSERT(un->un_errstats != NULL);
8505 	if (un->un_f_devid_supported) {
8506 		sd_register_devid(un, devi, reservation_flag);
8507 	}
8508 	mutex_exit(SD_MUTEX(un));
8509 
8510 #if (defined(__fibre))
8511 	/*
8512 	 * Register callbacks for fibre only.  You can't do this soley
8513 	 * on the basis of the devid_type because this is hba specific.
8514 	 * We need to query our hba capabilities to find out whether to
8515 	 * register or not.
8516 	 */
8517 	if (un->un_f_is_fibre) {
8518 	    if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8519 		sd_init_event_callbacks(un);
8520 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8521 		    "sd_unit_attach: un:0x%p event callbacks inserted", un);
8522 	    }
8523 	}
8524 #endif
8525 
8526 	if (un->un_f_opt_disable_cache == TRUE) {
8527 		/*
8528 		 * Disable both read cache and write cache.  This is
8529 		 * the historic behavior of the keywords in the config file.
8530 		 */
8531 		if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8532 		    0) {
8533 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8534 			    "sd_unit_attach: un:0x%p Could not disable "
8535 			    "caching", un);
8536 			goto devid_failed;
8537 		}
8538 	}
8539 
8540 	/*
8541 	 * Check the value of the WCE bit now and
8542 	 * set un_f_write_cache_enabled accordingly.
8543 	 */
8544 	(void) sd_get_write_cache_enabled(un, &wc_enabled);
8545 	mutex_enter(SD_MUTEX(un));
8546 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8547 	mutex_exit(SD_MUTEX(un));
8548 
8549 	/*
8550 	 * Set the pstat and error stat values here, so data obtained during the
8551 	 * previous attach-time routines is available.
8552 	 *
8553 	 * Note: This is a critical sequence that needs to be maintained:
8554 	 *	1) Instantiate the kstats before any routines using the iopath
8555 	 *	   (i.e. sd_send_scsi_cmd).
8556 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8557 	 *	   stats (sd_set_pstats)here, following sd_validate_geometry(),
8558 	 *	   sd_register_devid(), and sd_cache_control().
8559 	 */
8560 	if (un->un_f_pkstats_enabled) {
8561 		sd_set_pstats(un);
8562 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8563 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8564 	}
8565 
8566 	sd_set_errstats(un);
8567 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8568 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8569 
8570 	/*
8571 	 * Find out what type of reservation this disk supports.
8572 	 */
8573 	switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) {
8574 	case 0:
8575 		/*
8576 		 * SCSI-3 reservations are supported.
8577 		 */
8578 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8579 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8580 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8581 		break;
8582 	case ENOTSUP:
8583 		/*
8584 		 * The PERSISTENT RESERVE IN command would not be recognized by
8585 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8586 		 */
8587 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8588 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8589 		un->un_reservation_type = SD_SCSI2_RESERVATION;
8590 		break;
8591 	default:
8592 		/*
8593 		 * default to SCSI-3 reservations
8594 		 */
8595 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8596 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8597 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8598 		break;
8599 	}
8600 
8601 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8602 	    "sd_unit_attach: un:0x%p exit success\n", un);
8603 
8604 	return (DDI_SUCCESS);
8605 
8606 	/*
8607 	 * An error occurred during the attach; clean up & return failure.
8608 	 */
8609 
8610 devid_failed:
8611 
8612 setup_pm_failed:
8613 	ddi_remove_minor_node(devi, NULL);
8614 
8615 create_minor_nodes_failed:
8616 	/*
8617 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8618 	 */
8619 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8620 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8621 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8622 
8623 	if (un->un_f_is_fibre == FALSE) {
8624 	    (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8625 	}
8626 
8627 spinup_failed:
8628 
8629 	mutex_enter(SD_MUTEX(un));
8630 
8631 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8632 	if (un->un_direct_priority_timeid != NULL) {
8633 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8634 		un->un_direct_priority_timeid = NULL;
8635 		mutex_exit(SD_MUTEX(un));
8636 		(void) untimeout(temp_id);
8637 		mutex_enter(SD_MUTEX(un));
8638 	}
8639 
8640 	/* Cancel any pending start/stop timeouts */
8641 	if (un->un_startstop_timeid != NULL) {
8642 		timeout_id_t temp_id = un->un_startstop_timeid;
8643 		un->un_startstop_timeid = NULL;
8644 		mutex_exit(SD_MUTEX(un));
8645 		(void) untimeout(temp_id);
8646 		mutex_enter(SD_MUTEX(un));
8647 	}
8648 
8649 	/* Cancel any pending reset-throttle timeouts */
8650 	if (un->un_reset_throttle_timeid != NULL) {
8651 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8652 		un->un_reset_throttle_timeid = NULL;
8653 		mutex_exit(SD_MUTEX(un));
8654 		(void) untimeout(temp_id);
8655 		mutex_enter(SD_MUTEX(un));
8656 	}
8657 
8658 	/* Cancel any pending retry timeouts */
8659 	if (un->un_retry_timeid != NULL) {
8660 		timeout_id_t temp_id = un->un_retry_timeid;
8661 		un->un_retry_timeid = NULL;
8662 		mutex_exit(SD_MUTEX(un));
8663 		(void) untimeout(temp_id);
8664 		mutex_enter(SD_MUTEX(un));
8665 	}
8666 
8667 	/* Cancel any pending delayed cv broadcast timeouts */
8668 	if (un->un_dcvb_timeid != NULL) {
8669 		timeout_id_t temp_id = un->un_dcvb_timeid;
8670 		un->un_dcvb_timeid = NULL;
8671 		mutex_exit(SD_MUTEX(un));
8672 		(void) untimeout(temp_id);
8673 		mutex_enter(SD_MUTEX(un));
8674 	}
8675 
8676 	mutex_exit(SD_MUTEX(un));
8677 
8678 	/* There should not be any in-progress I/O so ASSERT this check */
8679 	ASSERT(un->un_ncmds_in_transport == 0);
8680 	ASSERT(un->un_ncmds_in_driver == 0);
8681 
8682 	/* Do not free the softstate if the callback routine is active */
8683 	sd_sync_with_callback(un);
8684 
8685 	/*
8686 	 * Partition stats apparently are not used with removables. These would
8687 	 * not have been created during attach, so no need to clean them up...
8688 	 */
8689 	if (un->un_stats != NULL) {
8690 		kstat_delete(un->un_stats);
8691 		un->un_stats = NULL;
8692 	}
8693 	if (un->un_errstats != NULL) {
8694 		kstat_delete(un->un_errstats);
8695 		un->un_errstats = NULL;
8696 	}
8697 
8698 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8699 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8700 
8701 	ddi_prop_remove_all(devi);
8702 	sema_destroy(&un->un_semoclose);
8703 	cv_destroy(&un->un_state_cv);
8704 
8705 getrbuf_failed:
8706 
8707 	sd_free_rqs(un);
8708 
8709 alloc_rqs_failed:
8710 
8711 	devp->sd_private = NULL;
8712 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
8713 
8714 get_softstate_failed:
8715 	/*
8716 	 * Note: the man pages are unclear as to whether or not doing a
8717 	 * ddi_soft_state_free(sd_state, instance) is the right way to
8718 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
8719 	 * ddi_get_soft_state() fails.  The implication seems to be
8720 	 * that the get_soft_state cannot fail if the zalloc succeeds.
8721 	 */
8722 	ddi_soft_state_free(sd_state, instance);
8723 
8724 probe_failed:
8725 	scsi_unprobe(devp);
8726 #ifdef SDDEBUG
8727 	if ((sd_component_mask & SD_LOG_ATTACH_DETACH) &&
8728 	    (sd_level_mask & SD_LOGMASK_TRACE)) {
8729 		cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n",
8730 		    (void *)un);
8731 	}
8732 #endif
8733 	return (DDI_FAILURE);
8734 }
8735 
8736 
8737 /*
8738  *    Function: sd_unit_detach
8739  *
8740  * Description: Performs DDI_DETACH processing for sddetach().
8741  *
8742  * Return Code: DDI_SUCCESS
8743  *		DDI_FAILURE
8744  *
8745  *     Context: Kernel thread context
8746  */
8747 
8748 static int
8749 sd_unit_detach(dev_info_t *devi)
8750 {
8751 	struct scsi_device	*devp;
8752 	struct sd_lun		*un;
8753 	int			i;
8754 	dev_t			dev;
8755 	int			instance = ddi_get_instance(devi);
8756 
8757 	mutex_enter(&sd_detach_mutex);
8758 
8759 	/*
8760 	 * Fail the detach for any of the following:
8761 	 *  - Unable to get the sd_lun struct for the instance
8762 	 *  - A layered driver has an outstanding open on the instance
8763 	 *  - Another thread is already detaching this instance
8764 	 *  - Another thread is currently performing an open
8765 	 */
8766 	devp = ddi_get_driver_private(devi);
8767 	if ((devp == NULL) ||
8768 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8769 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8770 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8771 		mutex_exit(&sd_detach_mutex);
8772 		return (DDI_FAILURE);
8773 	}
8774 
8775 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8776 
8777 	/*
8778 	 * Mark this instance as currently in a detach, to inhibit any
8779 	 * opens from a layered driver.
8780 	 */
8781 	un->un_detach_count++;
8782 	mutex_exit(&sd_detach_mutex);
8783 
8784 	dev = sd_make_device(SD_DEVINFO(un));
8785 
8786 	_NOTE(COMPETING_THREADS_NOW);
8787 
8788 	mutex_enter(SD_MUTEX(un));
8789 
8790 	/*
8791 	 * Fail the detach if there are any outstanding layered
8792 	 * opens on this device.
8793 	 */
8794 	for (i = 0; i < NDKMAP; i++) {
8795 		if (un->un_ocmap.lyropen[i] != 0) {
8796 			goto err_notclosed;
8797 		}
8798 	}
8799 
8800 	/*
8801 	 * Verify there are NO outstanding commands issued to this device.
8802 	 * ie, un_ncmds_in_transport == 0.
8803 	 * It's possible to have outstanding commands through the physio
8804 	 * code path, even though everything's closed.
8805 	 */
8806 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8807 	    (un->un_direct_priority_timeid != NULL) ||
8808 	    (un->un_state == SD_STATE_RWAIT)) {
8809 		mutex_exit(SD_MUTEX(un));
8810 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8811 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
8812 		goto err_stillbusy;
8813 	}
8814 
8815 	/*
8816 	 * If we have the device reserved, release the reservation.
8817 	 */
8818 	if ((un->un_resvd_status & SD_RESERVE) &&
8819 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
8820 		mutex_exit(SD_MUTEX(un));
8821 		/*
8822 		 * Note: sd_reserve_release sends a command to the device
8823 		 * via the sd_ioctlcmd() path, and can sleep.
8824 		 */
8825 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8826 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8827 			    "sd_dr_detach: Cannot release reservation \n");
8828 		}
8829 	} else {
8830 		mutex_exit(SD_MUTEX(un));
8831 	}
8832 
8833 	/*
8834 	 * Untimeout any reserve recover, throttle reset, restart unit
8835 	 * and delayed broadcast timeout threads. Protect the timeout pointer
8836 	 * from getting nulled by their callback functions.
8837 	 */
8838 	mutex_enter(SD_MUTEX(un));
8839 	if (un->un_resvd_timeid != NULL) {
8840 		timeout_id_t temp_id = un->un_resvd_timeid;
8841 		un->un_resvd_timeid = NULL;
8842 		mutex_exit(SD_MUTEX(un));
8843 		(void) untimeout(temp_id);
8844 		mutex_enter(SD_MUTEX(un));
8845 	}
8846 
8847 	if (un->un_reset_throttle_timeid != NULL) {
8848 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8849 		un->un_reset_throttle_timeid = NULL;
8850 		mutex_exit(SD_MUTEX(un));
8851 		(void) untimeout(temp_id);
8852 		mutex_enter(SD_MUTEX(un));
8853 	}
8854 
8855 	if (un->un_startstop_timeid != NULL) {
8856 		timeout_id_t temp_id = un->un_startstop_timeid;
8857 		un->un_startstop_timeid = NULL;
8858 		mutex_exit(SD_MUTEX(un));
8859 		(void) untimeout(temp_id);
8860 		mutex_enter(SD_MUTEX(un));
8861 	}
8862 
8863 	if (un->un_dcvb_timeid != NULL) {
8864 		timeout_id_t temp_id = un->un_dcvb_timeid;
8865 		un->un_dcvb_timeid = NULL;
8866 		mutex_exit(SD_MUTEX(un));
8867 		(void) untimeout(temp_id);
8868 	} else {
8869 		mutex_exit(SD_MUTEX(un));
8870 	}
8871 
8872 	/* Remove any pending reservation reclaim requests for this device */
8873 	sd_rmv_resv_reclaim_req(dev);
8874 
8875 	mutex_enter(SD_MUTEX(un));
8876 
8877 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8878 	if (un->un_direct_priority_timeid != NULL) {
8879 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8880 		un->un_direct_priority_timeid = NULL;
8881 		mutex_exit(SD_MUTEX(un));
8882 		(void) untimeout(temp_id);
8883 		mutex_enter(SD_MUTEX(un));
8884 	}
8885 
8886 	/* Cancel any active multi-host disk watch thread requests */
8887 	if (un->un_mhd_token != NULL) {
8888 		mutex_exit(SD_MUTEX(un));
8889 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8890 		if (scsi_watch_request_terminate(un->un_mhd_token,
8891 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8892 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8893 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
8894 			/*
8895 			 * Note: We are returning here after having removed
8896 			 * some driver timeouts above. This is consistent with
8897 			 * the legacy implementation but perhaps the watch
8898 			 * terminate call should be made with the wait flag set.
8899 			 */
8900 			goto err_stillbusy;
8901 		}
8902 		mutex_enter(SD_MUTEX(un));
8903 		un->un_mhd_token = NULL;
8904 	}
8905 
8906 	if (un->un_swr_token != NULL) {
8907 		mutex_exit(SD_MUTEX(un));
8908 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8909 		if (scsi_watch_request_terminate(un->un_swr_token,
8910 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8911 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8912 			    "sd_dr_detach: Cannot cancel swr watch request\n");
8913 			/*
8914 			 * Note: We are returning here after having removed
8915 			 * some driver timeouts above. This is consistent with
8916 			 * the legacy implementation but perhaps the watch
8917 			 * terminate call should be made with the wait flag set.
8918 			 */
8919 			goto err_stillbusy;
8920 		}
8921 		mutex_enter(SD_MUTEX(un));
8922 		un->un_swr_token = NULL;
8923 	}
8924 
8925 	mutex_exit(SD_MUTEX(un));
8926 
8927 	/*
8928 	 * Clear any scsi_reset_notifies. We clear the reset notifies
8929 	 * if we have not registered one.
8930 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8931 	 */
8932 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8933 	    sd_mhd_reset_notify_cb, (caddr_t)un);
8934 
8935 	/*
8936 	 * protect the timeout pointers from getting nulled by
8937 	 * their callback functions during the cancellation process.
8938 	 * In such a scenario untimeout can be invoked with a null value.
8939 	 */
8940 	_NOTE(NO_COMPETING_THREADS_NOW);
8941 
8942 	mutex_enter(&un->un_pm_mutex);
8943 	if (un->un_pm_idle_timeid != NULL) {
8944 		timeout_id_t temp_id = un->un_pm_idle_timeid;
8945 		un->un_pm_idle_timeid = NULL;
8946 		mutex_exit(&un->un_pm_mutex);
8947 
8948 		/*
8949 		 * Timeout is active; cancel it.
8950 		 * Note that it'll never be active on a device
8951 		 * that does not support PM therefore we don't
8952 		 * have to check before calling pm_idle_component.
8953 		 */
8954 		(void) untimeout(temp_id);
8955 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8956 		mutex_enter(&un->un_pm_mutex);
8957 	}
8958 
8959 	/*
8960 	 * Check whether there is already a timeout scheduled for power
8961 	 * management. If yes then don't lower the power here, that's.
8962 	 * the timeout handler's job.
8963 	 */
8964 	if (un->un_pm_timeid != NULL) {
8965 		timeout_id_t temp_id = un->un_pm_timeid;
8966 		un->un_pm_timeid = NULL;
8967 		mutex_exit(&un->un_pm_mutex);
8968 		/*
8969 		 * Timeout is active; cancel it.
8970 		 * Note that it'll never be active on a device
8971 		 * that does not support PM therefore we don't
8972 		 * have to check before calling pm_idle_component.
8973 		 */
8974 		(void) untimeout(temp_id);
8975 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8976 
8977 	} else {
8978 		mutex_exit(&un->un_pm_mutex);
8979 		if ((un->un_f_pm_is_enabled == TRUE) &&
8980 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) !=
8981 		    DDI_SUCCESS)) {
8982 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8983 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
8984 			/*
8985 			 * Fix for bug: 4297749, item # 13
8986 			 * The above test now includes a check to see if PM is
8987 			 * supported by this device before call
8988 			 * pm_lower_power().
8989 			 * Note, the following is not dead code. The call to
8990 			 * pm_lower_power above will generate a call back into
8991 			 * our sdpower routine which might result in a timeout
8992 			 * handler getting activated. Therefore the following
8993 			 * code is valid and necessary.
8994 			 */
8995 			mutex_enter(&un->un_pm_mutex);
8996 			if (un->un_pm_timeid != NULL) {
8997 				timeout_id_t temp_id = un->un_pm_timeid;
8998 				un->un_pm_timeid = NULL;
8999 				mutex_exit(&un->un_pm_mutex);
9000 				(void) untimeout(temp_id);
9001 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9002 			} else {
9003 				mutex_exit(&un->un_pm_mutex);
9004 			}
9005 		}
9006 	}
9007 
9008 	/*
9009 	 * Cleanup from the scsi_ifsetcap() calls (437868)
9010 	 * Relocated here from above to be after the call to
9011 	 * pm_lower_power, which was getting errors.
9012 	 */
9013 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
9014 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
9015 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
9016 
9017 	if (un->un_f_is_fibre == FALSE) {
9018 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
9019 	}
9020 
9021 	/*
9022 	 * Remove any event callbacks, fibre only
9023 	 */
9024 	if (un->un_f_is_fibre == TRUE) {
9025 		if ((un->un_insert_event != NULL) &&
9026 			(ddi_remove_event_handler(un->un_insert_cb_id) !=
9027 				DDI_SUCCESS)) {
9028 			/*
9029 			 * Note: We are returning here after having done
9030 			 * substantial cleanup above. This is consistent
9031 			 * with the legacy implementation but this may not
9032 			 * be the right thing to do.
9033 			 */
9034 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9035 				"sd_dr_detach: Cannot cancel insert event\n");
9036 			goto err_remove_event;
9037 		}
9038 		un->un_insert_event = NULL;
9039 
9040 		if ((un->un_remove_event != NULL) &&
9041 			(ddi_remove_event_handler(un->un_remove_cb_id) !=
9042 				DDI_SUCCESS)) {
9043 			/*
9044 			 * Note: We are returning here after having done
9045 			 * substantial cleanup above. This is consistent
9046 			 * with the legacy implementation but this may not
9047 			 * be the right thing to do.
9048 			 */
9049 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9050 				"sd_dr_detach: Cannot cancel remove event\n");
9051 			goto err_remove_event;
9052 		}
9053 		un->un_remove_event = NULL;
9054 	}
9055 
9056 	/* Do not free the softstate if the callback routine is active */
9057 	sd_sync_with_callback(un);
9058 
9059 	/*
9060 	 * Hold the detach mutex here, to make sure that no other threads ever
9061 	 * can access a (partially) freed soft state structure.
9062 	 */
9063 	mutex_enter(&sd_detach_mutex);
9064 
9065 	/*
9066 	 * Clean up the soft state struct.
9067 	 * Cleanup is done in reverse order of allocs/inits.
9068 	 * At this point there should be no competing threads anymore.
9069 	 */
9070 
9071 	/* Unregister and free device id. */
9072 	ddi_devid_unregister(devi);
9073 	if (un->un_devid) {
9074 		ddi_devid_free(un->un_devid);
9075 		un->un_devid = NULL;
9076 	}
9077 
9078 	/*
9079 	 * Destroy wmap cache if it exists.
9080 	 */
9081 	if (un->un_wm_cache != NULL) {
9082 		kmem_cache_destroy(un->un_wm_cache);
9083 		un->un_wm_cache = NULL;
9084 	}
9085 
9086 	/* Remove minor nodes */
9087 	ddi_remove_minor_node(devi, NULL);
9088 
9089 	/*
9090 	 * kstat cleanup is done in detach for all device types (4363169).
9091 	 * We do not want to fail detach if the device kstats are not deleted
9092 	 * since there is a confusion about the devo_refcnt for the device.
9093 	 * We just delete the kstats and let detach complete successfully.
9094 	 */
9095 	if (un->un_stats != NULL) {
9096 		kstat_delete(un->un_stats);
9097 		un->un_stats = NULL;
9098 	}
9099 	if (un->un_errstats != NULL) {
9100 		kstat_delete(un->un_errstats);
9101 		un->un_errstats = NULL;
9102 	}
9103 
9104 	/* Remove partition stats */
9105 	if (un->un_f_pkstats_enabled) {
9106 		for (i = 0; i < NSDMAP; i++) {
9107 			if (un->un_pstats[i] != NULL) {
9108 				kstat_delete(un->un_pstats[i]);
9109 				un->un_pstats[i] = NULL;
9110 			}
9111 		}
9112 	}
9113 
9114 	/* Remove xbuf registration */
9115 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
9116 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
9117 
9118 	/* Remove driver properties */
9119 	ddi_prop_remove_all(devi);
9120 
9121 	mutex_destroy(&un->un_pm_mutex);
9122 	cv_destroy(&un->un_pm_busy_cv);
9123 
9124 	cv_destroy(&un->un_wcc_cv);
9125 
9126 	/* Open/close semaphore */
9127 	sema_destroy(&un->un_semoclose);
9128 
9129 	/* Removable media condvar. */
9130 	cv_destroy(&un->un_state_cv);
9131 
9132 	/* Suspend/resume condvar. */
9133 	cv_destroy(&un->un_suspend_cv);
9134 	cv_destroy(&un->un_disk_busy_cv);
9135 
9136 	sd_free_rqs(un);
9137 
9138 	/* Free up soft state */
9139 	devp->sd_private = NULL;
9140 	bzero(un, sizeof (struct sd_lun));
9141 	ddi_soft_state_free(sd_state, instance);
9142 
9143 	mutex_exit(&sd_detach_mutex);
9144 
9145 	/* This frees up the INQUIRY data associated with the device. */
9146 	scsi_unprobe(devp);
9147 
9148 	return (DDI_SUCCESS);
9149 
9150 err_notclosed:
9151 	mutex_exit(SD_MUTEX(un));
9152 
9153 err_stillbusy:
9154 	_NOTE(NO_COMPETING_THREADS_NOW);
9155 
9156 err_remove_event:
9157 	mutex_enter(&sd_detach_mutex);
9158 	un->un_detach_count--;
9159 	mutex_exit(&sd_detach_mutex);
9160 
9161 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9162 	return (DDI_FAILURE);
9163 }
9164 
9165 
9166 /*
9167  * Driver minor node structure and data table
9168  */
9169 struct driver_minor_data {
9170 	char	*name;
9171 	minor_t	minor;
9172 	int	type;
9173 };
9174 
9175 static struct driver_minor_data sd_minor_data[] = {
9176 	{"a", 0, S_IFBLK},
9177 	{"b", 1, S_IFBLK},
9178 	{"c", 2, S_IFBLK},
9179 	{"d", 3, S_IFBLK},
9180 	{"e", 4, S_IFBLK},
9181 	{"f", 5, S_IFBLK},
9182 	{"g", 6, S_IFBLK},
9183 	{"h", 7, S_IFBLK},
9184 #if defined(_SUNOS_VTOC_16)
9185 	{"i", 8, S_IFBLK},
9186 	{"j", 9, S_IFBLK},
9187 	{"k", 10, S_IFBLK},
9188 	{"l", 11, S_IFBLK},
9189 	{"m", 12, S_IFBLK},
9190 	{"n", 13, S_IFBLK},
9191 	{"o", 14, S_IFBLK},
9192 	{"p", 15, S_IFBLK},
9193 #endif			/* defined(_SUNOS_VTOC_16) */
9194 #if defined(_FIRMWARE_NEEDS_FDISK)
9195 	{"q", 16, S_IFBLK},
9196 	{"r", 17, S_IFBLK},
9197 	{"s", 18, S_IFBLK},
9198 	{"t", 19, S_IFBLK},
9199 	{"u", 20, S_IFBLK},
9200 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9201 	{"a,raw", 0, S_IFCHR},
9202 	{"b,raw", 1, S_IFCHR},
9203 	{"c,raw", 2, S_IFCHR},
9204 	{"d,raw", 3, S_IFCHR},
9205 	{"e,raw", 4, S_IFCHR},
9206 	{"f,raw", 5, S_IFCHR},
9207 	{"g,raw", 6, S_IFCHR},
9208 	{"h,raw", 7, S_IFCHR},
9209 #if defined(_SUNOS_VTOC_16)
9210 	{"i,raw", 8, S_IFCHR},
9211 	{"j,raw", 9, S_IFCHR},
9212 	{"k,raw", 10, S_IFCHR},
9213 	{"l,raw", 11, S_IFCHR},
9214 	{"m,raw", 12, S_IFCHR},
9215 	{"n,raw", 13, S_IFCHR},
9216 	{"o,raw", 14, S_IFCHR},
9217 	{"p,raw", 15, S_IFCHR},
9218 #endif			/* defined(_SUNOS_VTOC_16) */
9219 #if defined(_FIRMWARE_NEEDS_FDISK)
9220 	{"q,raw", 16, S_IFCHR},
9221 	{"r,raw", 17, S_IFCHR},
9222 	{"s,raw", 18, S_IFCHR},
9223 	{"t,raw", 19, S_IFCHR},
9224 	{"u,raw", 20, S_IFCHR},
9225 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9226 	{0}
9227 };
9228 
9229 static struct driver_minor_data sd_minor_data_efi[] = {
9230 	{"a", 0, S_IFBLK},
9231 	{"b", 1, S_IFBLK},
9232 	{"c", 2, S_IFBLK},
9233 	{"d", 3, S_IFBLK},
9234 	{"e", 4, S_IFBLK},
9235 	{"f", 5, S_IFBLK},
9236 	{"g", 6, S_IFBLK},
9237 	{"wd", 7, S_IFBLK},
9238 #if defined(_FIRMWARE_NEEDS_FDISK)
9239 	{"q", 16, S_IFBLK},
9240 	{"r", 17, S_IFBLK},
9241 	{"s", 18, S_IFBLK},
9242 	{"t", 19, S_IFBLK},
9243 	{"u", 20, S_IFBLK},
9244 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9245 	{"a,raw", 0, S_IFCHR},
9246 	{"b,raw", 1, S_IFCHR},
9247 	{"c,raw", 2, S_IFCHR},
9248 	{"d,raw", 3, S_IFCHR},
9249 	{"e,raw", 4, S_IFCHR},
9250 	{"f,raw", 5, S_IFCHR},
9251 	{"g,raw", 6, S_IFCHR},
9252 	{"wd,raw", 7, S_IFCHR},
9253 #if defined(_FIRMWARE_NEEDS_FDISK)
9254 	{"q,raw", 16, S_IFCHR},
9255 	{"r,raw", 17, S_IFCHR},
9256 	{"s,raw", 18, S_IFCHR},
9257 	{"t,raw", 19, S_IFCHR},
9258 	{"u,raw", 20, S_IFCHR},
9259 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9260 	{0}
9261 };
9262 
9263 
9264 /*
9265  *    Function: sd_create_minor_nodes
9266  *
9267  * Description: Create the minor device nodes for the instance.
9268  *
9269  *   Arguments: un - driver soft state (unit) structure
9270  *		devi - pointer to device info structure
9271  *
9272  * Return Code: DDI_SUCCESS
9273  *		DDI_FAILURE
9274  *
9275  *     Context: Kernel thread context
9276  */
9277 
9278 static int
9279 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi)
9280 {
9281 	struct driver_minor_data	*dmdp;
9282 	struct scsi_device		*devp;
9283 	int				instance;
9284 	char				name[48];
9285 
9286 	ASSERT(un != NULL);
9287 	devp = ddi_get_driver_private(devi);
9288 	instance = ddi_get_instance(devp->sd_dev);
9289 
9290 	/*
9291 	 * Create all the minor nodes for this target.
9292 	 */
9293 	if (un->un_blockcount > DK_MAX_BLOCKS)
9294 		dmdp = sd_minor_data_efi;
9295 	else
9296 		dmdp = sd_minor_data;
9297 	while (dmdp->name != NULL) {
9298 
9299 		(void) sprintf(name, "%s", dmdp->name);
9300 
9301 		if (ddi_create_minor_node(devi, name, dmdp->type,
9302 		    (instance << SDUNIT_SHIFT) | dmdp->minor,
9303 		    un->un_node_type, NULL) == DDI_FAILURE) {
9304 			/*
9305 			 * Clean up any nodes that may have been created, in
9306 			 * case this fails in the middle of the loop.
9307 			 */
9308 			ddi_remove_minor_node(devi, NULL);
9309 			return (DDI_FAILURE);
9310 		}
9311 		dmdp++;
9312 	}
9313 
9314 	return (DDI_SUCCESS);
9315 }
9316 
9317 
9318 /*
9319  *    Function: sd_create_errstats
9320  *
9321  * Description: This routine instantiates the device error stats.
9322  *
9323  *		Note: During attach the stats are instantiated first so they are
9324  *		available for attach-time routines that utilize the driver
9325  *		iopath to send commands to the device. The stats are initialized
9326  *		separately so data obtained during some attach-time routines is
9327  *		available. (4362483)
9328  *
9329  *   Arguments: un - driver soft state (unit) structure
9330  *		instance - driver instance
9331  *
9332  *     Context: Kernel thread context
9333  */
9334 
9335 static void
9336 sd_create_errstats(struct sd_lun *un, int instance)
9337 {
9338 	struct	sd_errstats	*stp;
9339 	char	kstatmodule_err[KSTAT_STRLEN];
9340 	char	kstatname[KSTAT_STRLEN];
9341 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9342 
9343 	ASSERT(un != NULL);
9344 
9345 	if (un->un_errstats != NULL) {
9346 		return;
9347 	}
9348 
9349 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9350 	    "%serr", sd_label);
9351 	(void) snprintf(kstatname, sizeof (kstatname),
9352 	    "%s%d,err", sd_label, instance);
9353 
9354 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9355 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9356 
9357 	if (un->un_errstats == NULL) {
9358 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9359 		    "sd_create_errstats: Failed kstat_create\n");
9360 		return;
9361 	}
9362 
9363 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9364 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9365 	    KSTAT_DATA_UINT32);
9366 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9367 	    KSTAT_DATA_UINT32);
9368 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9369 	    KSTAT_DATA_UINT32);
9370 	kstat_named_init(&stp->sd_vid,		"Vendor",
9371 	    KSTAT_DATA_CHAR);
9372 	kstat_named_init(&stp->sd_pid,		"Product",
9373 	    KSTAT_DATA_CHAR);
9374 	kstat_named_init(&stp->sd_revision,	"Revision",
9375 	    KSTAT_DATA_CHAR);
9376 	kstat_named_init(&stp->sd_serial,	"Serial No",
9377 	    KSTAT_DATA_CHAR);
9378 	kstat_named_init(&stp->sd_capacity,	"Size",
9379 	    KSTAT_DATA_ULONGLONG);
9380 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9381 	    KSTAT_DATA_UINT32);
9382 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9383 	    KSTAT_DATA_UINT32);
9384 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9385 	    KSTAT_DATA_UINT32);
9386 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9387 	    KSTAT_DATA_UINT32);
9388 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9389 	    KSTAT_DATA_UINT32);
9390 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9391 	    KSTAT_DATA_UINT32);
9392 
9393 	un->un_errstats->ks_private = un;
9394 	un->un_errstats->ks_update  = nulldev;
9395 
9396 	kstat_install(un->un_errstats);
9397 }
9398 
9399 
9400 /*
9401  *    Function: sd_set_errstats
9402  *
9403  * Description: This routine sets the value of the vendor id, product id,
9404  *		revision, serial number, and capacity device error stats.
9405  *
9406  *		Note: During attach the stats are instantiated first so they are
9407  *		available for attach-time routines that utilize the driver
9408  *		iopath to send commands to the device. The stats are initialized
9409  *		separately so data obtained during some attach-time routines is
9410  *		available. (4362483)
9411  *
9412  *   Arguments: un - driver soft state (unit) structure
9413  *
9414  *     Context: Kernel thread context
9415  */
9416 
9417 static void
9418 sd_set_errstats(struct sd_lun *un)
9419 {
9420 	struct	sd_errstats	*stp;
9421 
9422 	ASSERT(un != NULL);
9423 	ASSERT(un->un_errstats != NULL);
9424 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9425 	ASSERT(stp != NULL);
9426 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9427 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9428 	(void) strncpy(stp->sd_revision.value.c,
9429 	    un->un_sd->sd_inq->inq_revision, 4);
9430 
9431 	/*
9432 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9433 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9434 	 * (4376302))
9435 	 */
9436 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9437 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9438 		    sizeof (SD_INQUIRY(un)->inq_serial));
9439 	}
9440 
9441 	if (un->un_f_blockcount_is_valid != TRUE) {
9442 		/*
9443 		 * Set capacity error stat to 0 for no media. This ensures
9444 		 * a valid capacity is displayed in response to 'iostat -E'
9445 		 * when no media is present in the device.
9446 		 */
9447 		stp->sd_capacity.value.ui64 = 0;
9448 	} else {
9449 		/*
9450 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9451 		 * capacity.
9452 		 *
9453 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9454 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9455 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9456 		 */
9457 		stp->sd_capacity.value.ui64 = (uint64_t)
9458 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9459 	}
9460 }
9461 
9462 
9463 /*
9464  *    Function: sd_set_pstats
9465  *
9466  * Description: This routine instantiates and initializes the partition
9467  *              stats for each partition with more than zero blocks.
9468  *		(4363169)
9469  *
9470  *   Arguments: un - driver soft state (unit) structure
9471  *
9472  *     Context: Kernel thread context
9473  */
9474 
9475 static void
9476 sd_set_pstats(struct sd_lun *un)
9477 {
9478 	char	kstatname[KSTAT_STRLEN];
9479 	int	instance;
9480 	int	i;
9481 
9482 	ASSERT(un != NULL);
9483 
9484 	instance = ddi_get_instance(SD_DEVINFO(un));
9485 
9486 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9487 	for (i = 0; i < NSDMAP; i++) {
9488 		if ((un->un_pstats[i] == NULL) &&
9489 		    (un->un_map[i].dkl_nblk != 0)) {
9490 			(void) snprintf(kstatname, sizeof (kstatname),
9491 			    "%s%d,%s", sd_label, instance,
9492 			    sd_minor_data[i].name);
9493 			un->un_pstats[i] = kstat_create(sd_label,
9494 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9495 			    1, KSTAT_FLAG_PERSISTENT);
9496 			if (un->un_pstats[i] != NULL) {
9497 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9498 				kstat_install(un->un_pstats[i]);
9499 			}
9500 		}
9501 	}
9502 }
9503 
9504 
9505 #if (defined(__fibre))
9506 /*
9507  *    Function: sd_init_event_callbacks
9508  *
9509  * Description: This routine initializes the insertion and removal event
9510  *		callbacks. (fibre only)
9511  *
9512  *   Arguments: un - driver soft state (unit) structure
9513  *
9514  *     Context: Kernel thread context
9515  */
9516 
9517 static void
9518 sd_init_event_callbacks(struct sd_lun *un)
9519 {
9520 	ASSERT(un != NULL);
9521 
9522 	if ((un->un_insert_event == NULL) &&
9523 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9524 	    &un->un_insert_event) == DDI_SUCCESS)) {
9525 		/*
9526 		 * Add the callback for an insertion event
9527 		 */
9528 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9529 		    un->un_insert_event, sd_event_callback, (void *)un,
9530 		    &(un->un_insert_cb_id));
9531 	}
9532 
9533 	if ((un->un_remove_event == NULL) &&
9534 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9535 	    &un->un_remove_event) == DDI_SUCCESS)) {
9536 		/*
9537 		 * Add the callback for a removal event
9538 		 */
9539 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9540 		    un->un_remove_event, sd_event_callback, (void *)un,
9541 		    &(un->un_remove_cb_id));
9542 	}
9543 }
9544 
9545 
9546 /*
9547  *    Function: sd_event_callback
9548  *
9549  * Description: This routine handles insert/remove events (photon). The
9550  *		state is changed to OFFLINE which can be used to supress
9551  *		error msgs. (fibre only)
9552  *
9553  *   Arguments: un - driver soft state (unit) structure
9554  *
9555  *     Context: Callout thread context
9556  */
9557 /* ARGSUSED */
9558 static void
9559 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9560     void *bus_impldata)
9561 {
9562 	struct sd_lun *un = (struct sd_lun *)arg;
9563 
9564 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9565 	if (event == un->un_insert_event) {
9566 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9567 		mutex_enter(SD_MUTEX(un));
9568 		if (un->un_state == SD_STATE_OFFLINE) {
9569 			if (un->un_last_state != SD_STATE_SUSPENDED) {
9570 				un->un_state = un->un_last_state;
9571 			} else {
9572 				/*
9573 				 * We have gone through SUSPEND/RESUME while
9574 				 * we were offline. Restore the last state
9575 				 */
9576 				un->un_state = un->un_save_state;
9577 			}
9578 		}
9579 		mutex_exit(SD_MUTEX(un));
9580 
9581 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9582 	} else if (event == un->un_remove_event) {
9583 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9584 		mutex_enter(SD_MUTEX(un));
9585 		/*
9586 		 * We need to handle an event callback that occurs during
9587 		 * the suspend operation, since we don't prevent it.
9588 		 */
9589 		if (un->un_state != SD_STATE_OFFLINE) {
9590 			if (un->un_state != SD_STATE_SUSPENDED) {
9591 				New_state(un, SD_STATE_OFFLINE);
9592 			} else {
9593 				un->un_last_state = SD_STATE_OFFLINE;
9594 			}
9595 		}
9596 		mutex_exit(SD_MUTEX(un));
9597 	} else {
9598 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9599 		    "!Unknown event\n");
9600 	}
9601 
9602 }
9603 #endif
9604 
9605 /*
9606  *    Function: sd_cache_control()
9607  *
9608  * Description: This routine is the driver entry point for setting
9609  *		read and write caching by modifying the WCE (write cache
9610  *		enable) and RCD (read cache disable) bits of mode
9611  *		page 8 (MODEPAGE_CACHING).
9612  *
9613  *   Arguments: un - driver soft state (unit) structure
9614  *		rcd_flag - flag for controlling the read cache
9615  *		wce_flag - flag for controlling the write cache
9616  *
9617  * Return Code: EIO
9618  *		code returned by sd_send_scsi_MODE_SENSE and
9619  *		sd_send_scsi_MODE_SELECT
9620  *
9621  *     Context: Kernel Thread
9622  */
9623 
9624 static int
9625 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag)
9626 {
9627 	struct mode_caching	*mode_caching_page;
9628 	uchar_t			*header;
9629 	size_t			buflen;
9630 	int			hdrlen;
9631 	int			bd_len;
9632 	int			rval = 0;
9633 	struct mode_header_grp2	*mhp;
9634 
9635 	ASSERT(un != NULL);
9636 
9637 	/*
9638 	 * Do a test unit ready, otherwise a mode sense may not work if this
9639 	 * is the first command sent to the device after boot.
9640 	 */
9641 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
9642 
9643 	if (un->un_f_cfg_is_atapi == TRUE) {
9644 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9645 	} else {
9646 		hdrlen = MODE_HEADER_LENGTH;
9647 	}
9648 
9649 	/*
9650 	 * Allocate memory for the retrieved mode page and its headers.  Set
9651 	 * a pointer to the page itself.  Use mode_cache_scsi3 to insure
9652 	 * we get all of the mode sense data otherwise, the mode select
9653 	 * will fail.  mode_cache_scsi3 is a superset of mode_caching.
9654 	 */
9655 	buflen = hdrlen + MODE_BLK_DESC_LENGTH +
9656 		sizeof (struct mode_cache_scsi3);
9657 
9658 	header = kmem_zalloc(buflen, KM_SLEEP);
9659 
9660 	/* Get the information from the device. */
9661 	if (un->un_f_cfg_is_atapi == TRUE) {
9662 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
9663 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9664 	} else {
9665 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
9666 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9667 	}
9668 	if (rval != 0) {
9669 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9670 		    "sd_cache_control: Mode Sense Failed\n");
9671 		kmem_free(header, buflen);
9672 		return (rval);
9673 	}
9674 
9675 	/*
9676 	 * Determine size of Block Descriptors in order to locate
9677 	 * the mode page data. ATAPI devices return 0, SCSI devices
9678 	 * should return MODE_BLK_DESC_LENGTH.
9679 	 */
9680 	if (un->un_f_cfg_is_atapi == TRUE) {
9681 		mhp	= (struct mode_header_grp2 *)header;
9682 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9683 	} else {
9684 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9685 	}
9686 
9687 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9688 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
9689 		    "sd_cache_control: Mode Sense returned invalid "
9690 		    "block descriptor length\n");
9691 		kmem_free(header, buflen);
9692 		return (EIO);
9693 	}
9694 
9695 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9696 
9697 	/* Check the relevant bits on successful mode sense. */
9698 	if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9699 	    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9700 	    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9701 	    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9702 
9703 		size_t sbuflen;
9704 		uchar_t save_pg;
9705 
9706 		/*
9707 		 * Construct select buffer length based on the
9708 		 * length of the sense data returned.
9709 		 */
9710 		sbuflen =  hdrlen + MODE_BLK_DESC_LENGTH +
9711 				sizeof (struct mode_page) +
9712 				(int)mode_caching_page->mode_page.length;
9713 
9714 		/*
9715 		 * Set the caching bits as requested.
9716 		 */
9717 		if (rcd_flag == SD_CACHE_ENABLE)
9718 			mode_caching_page->rcd = 0;
9719 		else if (rcd_flag == SD_CACHE_DISABLE)
9720 			mode_caching_page->rcd = 1;
9721 
9722 		if (wce_flag == SD_CACHE_ENABLE)
9723 			mode_caching_page->wce = 1;
9724 		else if (wce_flag == SD_CACHE_DISABLE)
9725 			mode_caching_page->wce = 0;
9726 
9727 		/*
9728 		 * Save the page if the mode sense says the
9729 		 * drive supports it.
9730 		 */
9731 		save_pg = mode_caching_page->mode_page.ps ?
9732 				SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9733 
9734 		/* Clear reserved bits before mode select. */
9735 		mode_caching_page->mode_page.ps = 0;
9736 
9737 		/*
9738 		 * Clear out mode header for mode select.
9739 		 * The rest of the retrieved page will be reused.
9740 		 */
9741 		bzero(header, hdrlen);
9742 
9743 		if (un->un_f_cfg_is_atapi == TRUE) {
9744 			mhp = (struct mode_header_grp2 *)header;
9745 			mhp->bdesc_length_hi = bd_len >> 8;
9746 			mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
9747 		} else {
9748 			((struct mode_header *)header)->bdesc_length = bd_len;
9749 		}
9750 
9751 		/* Issue mode select to change the cache settings */
9752 		if (un->un_f_cfg_is_atapi == TRUE) {
9753 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header,
9754 			    sbuflen, save_pg, SD_PATH_DIRECT);
9755 		} else {
9756 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
9757 			    sbuflen, save_pg, SD_PATH_DIRECT);
9758 		}
9759 	}
9760 
9761 	kmem_free(header, buflen);
9762 	return (rval);
9763 }
9764 
9765 
9766 /*
9767  *    Function: sd_get_write_cache_enabled()
9768  *
9769  * Description: This routine is the driver entry point for determining if
9770  *		write caching is enabled.  It examines the WCE (write cache
9771  *		enable) bits of mode page 8 (MODEPAGE_CACHING).
9772  *
9773  *   Arguments: un - driver soft state (unit) structure
9774  *   		is_enabled - pointer to int where write cache enabled state
9775  *   			is returned (non-zero -> write cache enabled)
9776  *
9777  *
9778  * Return Code: EIO
9779  *		code returned by sd_send_scsi_MODE_SENSE
9780  *
9781  *     Context: Kernel Thread
9782  *
9783  * NOTE: If ioctl is added to disable write cache, this sequence should
9784  * be followed so that no locking is required for accesses to
9785  * un->un_f_write_cache_enabled:
9786  * 	do mode select to clear wce
9787  * 	do synchronize cache to flush cache
9788  * 	set un->un_f_write_cache_enabled = FALSE
9789  *
9790  * Conversely, an ioctl to enable the write cache should be done
9791  * in this order:
9792  * 	set un->un_f_write_cache_enabled = TRUE
9793  * 	do mode select to set wce
9794  */
9795 
9796 static int
9797 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled)
9798 {
9799 	struct mode_caching	*mode_caching_page;
9800 	uchar_t			*header;
9801 	size_t			buflen;
9802 	int			hdrlen;
9803 	int			bd_len;
9804 	int			rval = 0;
9805 
9806 	ASSERT(un != NULL);
9807 	ASSERT(is_enabled != NULL);
9808 
9809 	/* in case of error, flag as enabled */
9810 	*is_enabled = TRUE;
9811 
9812 	/*
9813 	 * Do a test unit ready, otherwise a mode sense may not work if this
9814 	 * is the first command sent to the device after boot.
9815 	 */
9816 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
9817 
9818 	if (un->un_f_cfg_is_atapi == TRUE) {
9819 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9820 	} else {
9821 		hdrlen = MODE_HEADER_LENGTH;
9822 	}
9823 
9824 	/*
9825 	 * Allocate memory for the retrieved mode page and its headers.  Set
9826 	 * a pointer to the page itself.
9827 	 */
9828 	buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
9829 	header = kmem_zalloc(buflen, KM_SLEEP);
9830 
9831 	/* Get the information from the device. */
9832 	if (un->un_f_cfg_is_atapi == TRUE) {
9833 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
9834 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9835 	} else {
9836 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
9837 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9838 	}
9839 	if (rval != 0) {
9840 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9841 		    "sd_get_write_cache_enabled: Mode Sense Failed\n");
9842 		kmem_free(header, buflen);
9843 		return (rval);
9844 	}
9845 
9846 	/*
9847 	 * Determine size of Block Descriptors in order to locate
9848 	 * the mode page data. ATAPI devices return 0, SCSI devices
9849 	 * should return MODE_BLK_DESC_LENGTH.
9850 	 */
9851 	if (un->un_f_cfg_is_atapi == TRUE) {
9852 		struct mode_header_grp2	*mhp;
9853 		mhp	= (struct mode_header_grp2 *)header;
9854 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9855 	} else {
9856 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9857 	}
9858 
9859 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9860 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
9861 		    "sd_get_write_cache_enabled: Mode Sense returned invalid "
9862 		    "block descriptor length\n");
9863 		kmem_free(header, buflen);
9864 		return (EIO);
9865 	}
9866 
9867 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9868 	*is_enabled = mode_caching_page->wce;
9869 
9870 	kmem_free(header, buflen);
9871 	return (0);
9872 }
9873 
9874 
9875 /*
9876  *    Function: sd_make_device
9877  *
9878  * Description: Utility routine to return the Solaris device number from
9879  *		the data in the device's dev_info structure.
9880  *
9881  * Return Code: The Solaris device number
9882  *
9883  *     Context: Any
9884  */
9885 
9886 static dev_t
9887 sd_make_device(dev_info_t *devi)
9888 {
9889 	return (makedevice(ddi_name_to_major(ddi_get_name(devi)),
9890 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
9891 }
9892 
9893 
9894 /*
9895  *    Function: sd_pm_entry
9896  *
9897  * Description: Called at the start of a new command to manage power
9898  *		and busy status of a device. This includes determining whether
9899  *		the current power state of the device is sufficient for
9900  *		performing the command or whether it must be changed.
9901  *		The PM framework is notified appropriately.
9902  *		Only with a return status of DDI_SUCCESS will the
9903  *		component be busy to the framework.
9904  *
9905  *		All callers of sd_pm_entry must check the return status
9906  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
9907  *		of DDI_FAILURE indicates the device failed to power up.
9908  *		In this case un_pm_count has been adjusted so the result
9909  *		on exit is still powered down, ie. count is less than 0.
9910  *		Calling sd_pm_exit with this count value hits an ASSERT.
9911  *
9912  * Return Code: DDI_SUCCESS or DDI_FAILURE
9913  *
9914  *     Context: Kernel thread context.
9915  */
9916 
9917 static int
9918 sd_pm_entry(struct sd_lun *un)
9919 {
9920 	int return_status = DDI_SUCCESS;
9921 
9922 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9923 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9924 
9925 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9926 
9927 	if (un->un_f_pm_is_enabled == FALSE) {
9928 		SD_TRACE(SD_LOG_IO_PM, un,
9929 		    "sd_pm_entry: exiting, PM not enabled\n");
9930 		return (return_status);
9931 	}
9932 
9933 	/*
9934 	 * Just increment a counter if PM is enabled. On the transition from
9935 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9936 	 * the count with each IO and mark the device as idle when the count
9937 	 * hits 0.
9938 	 *
9939 	 * If the count is less than 0 the device is powered down. If a powered
9940 	 * down device is successfully powered up then the count must be
9941 	 * incremented to reflect the power up. Note that it'll get incremented
9942 	 * a second time to become busy.
9943 	 *
9944 	 * Because the following has the potential to change the device state
9945 	 * and must release the un_pm_mutex to do so, only one thread can be
9946 	 * allowed through at a time.
9947 	 */
9948 
9949 	mutex_enter(&un->un_pm_mutex);
9950 	while (un->un_pm_busy == TRUE) {
9951 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9952 	}
9953 	un->un_pm_busy = TRUE;
9954 
9955 	if (un->un_pm_count < 1) {
9956 
9957 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9958 
9959 		/*
9960 		 * Indicate we are now busy so the framework won't attempt to
9961 		 * power down the device. This call will only fail if either
9962 		 * we passed a bad component number or the device has no
9963 		 * components. Neither of these should ever happen.
9964 		 */
9965 		mutex_exit(&un->un_pm_mutex);
9966 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
9967 		ASSERT(return_status == DDI_SUCCESS);
9968 
9969 		mutex_enter(&un->un_pm_mutex);
9970 
9971 		if (un->un_pm_count < 0) {
9972 			mutex_exit(&un->un_pm_mutex);
9973 
9974 			SD_TRACE(SD_LOG_IO_PM, un,
9975 			    "sd_pm_entry: power up component\n");
9976 
9977 			/*
9978 			 * pm_raise_power will cause sdpower to be called
9979 			 * which brings the device power level to the
9980 			 * desired state, ON in this case. If successful,
9981 			 * un_pm_count and un_power_level will be updated
9982 			 * appropriately.
9983 			 */
9984 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
9985 			    SD_SPINDLE_ON);
9986 
9987 			mutex_enter(&un->un_pm_mutex);
9988 
9989 			if (return_status != DDI_SUCCESS) {
9990 				/*
9991 				 * Power up failed.
9992 				 * Idle the device and adjust the count
9993 				 * so the result on exit is that we're
9994 				 * still powered down, ie. count is less than 0.
9995 				 */
9996 				SD_TRACE(SD_LOG_IO_PM, un,
9997 				    "sd_pm_entry: power up failed,"
9998 				    " idle the component\n");
9999 
10000 				(void) pm_idle_component(SD_DEVINFO(un), 0);
10001 				un->un_pm_count--;
10002 			} else {
10003 				/*
10004 				 * Device is powered up, verify the
10005 				 * count is non-negative.
10006 				 * This is debug only.
10007 				 */
10008 				ASSERT(un->un_pm_count == 0);
10009 			}
10010 		}
10011 
10012 		if (return_status == DDI_SUCCESS) {
10013 			/*
10014 			 * For performance, now that the device has been tagged
10015 			 * as busy, and it's known to be powered up, update the
10016 			 * chain types to use jump tables that do not include
10017 			 * pm. This significantly lowers the overhead and
10018 			 * therefore improves performance.
10019 			 */
10020 
10021 			mutex_exit(&un->un_pm_mutex);
10022 			mutex_enter(SD_MUTEX(un));
10023 			SD_TRACE(SD_LOG_IO_PM, un,
10024 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
10025 			    un->un_uscsi_chain_type);
10026 
10027 			if (un->un_f_non_devbsize_supported) {
10028 				un->un_buf_chain_type =
10029 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
10030 			} else {
10031 				un->un_buf_chain_type =
10032 				    SD_CHAIN_INFO_DISK_NO_PM;
10033 			}
10034 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
10035 
10036 			SD_TRACE(SD_LOG_IO_PM, un,
10037 			    "             changed  uscsi_chain_type to   %d\n",
10038 			    un->un_uscsi_chain_type);
10039 			mutex_exit(SD_MUTEX(un));
10040 			mutex_enter(&un->un_pm_mutex);
10041 
10042 			if (un->un_pm_idle_timeid == NULL) {
10043 				/* 300 ms. */
10044 				un->un_pm_idle_timeid =
10045 				    timeout(sd_pm_idletimeout_handler, un,
10046 				    (drv_usectohz((clock_t)300000)));
10047 				/*
10048 				 * Include an extra call to busy which keeps the
10049 				 * device busy with-respect-to the PM layer
10050 				 * until the timer fires, at which time it'll
10051 				 * get the extra idle call.
10052 				 */
10053 				(void) pm_busy_component(SD_DEVINFO(un), 0);
10054 			}
10055 		}
10056 	}
10057 	un->un_pm_busy = FALSE;
10058 	/* Next... */
10059 	cv_signal(&un->un_pm_busy_cv);
10060 
10061 	un->un_pm_count++;
10062 
10063 	SD_TRACE(SD_LOG_IO_PM, un,
10064 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10065 
10066 	mutex_exit(&un->un_pm_mutex);
10067 
10068 	return (return_status);
10069 }
10070 
10071 
10072 /*
10073  *    Function: sd_pm_exit
10074  *
10075  * Description: Called at the completion of a command to manage busy
10076  *		status for the device. If the device becomes idle the
10077  *		PM framework is notified.
10078  *
10079  *     Context: Kernel thread context
10080  */
10081 
10082 static void
10083 sd_pm_exit(struct sd_lun *un)
10084 {
10085 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10086 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10087 
10088 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10089 
10090 	/*
10091 	 * After attach the following flag is only read, so don't
10092 	 * take the penalty of acquiring a mutex for it.
10093 	 */
10094 	if (un->un_f_pm_is_enabled == TRUE) {
10095 
10096 		mutex_enter(&un->un_pm_mutex);
10097 		un->un_pm_count--;
10098 
10099 		SD_TRACE(SD_LOG_IO_PM, un,
10100 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10101 
10102 		ASSERT(un->un_pm_count >= 0);
10103 		if (un->un_pm_count == 0) {
10104 			mutex_exit(&un->un_pm_mutex);
10105 
10106 			SD_TRACE(SD_LOG_IO_PM, un,
10107 			    "sd_pm_exit: idle component\n");
10108 
10109 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10110 
10111 		} else {
10112 			mutex_exit(&un->un_pm_mutex);
10113 		}
10114 	}
10115 
10116 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10117 }
10118 
10119 
10120 /*
10121  *    Function: sdopen
10122  *
10123  * Description: Driver's open(9e) entry point function.
10124  *
10125  *   Arguments: dev_i   - pointer to device number
10126  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10127  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10128  *		cred_p  - user credential pointer
10129  *
10130  * Return Code: EINVAL
10131  *		ENXIO
10132  *		EIO
10133  *		EROFS
10134  *		EBUSY
10135  *
10136  *     Context: Kernel thread context
10137  */
10138 /* ARGSUSED */
10139 static int
10140 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10141 {
10142 	struct sd_lun	*un;
10143 	int		nodelay;
10144 	int		part;
10145 	uint64_t	partmask;
10146 	int		instance;
10147 	dev_t		dev;
10148 	int		rval = EIO;
10149 
10150 	/* Validate the open type */
10151 	if (otyp >= OTYPCNT) {
10152 		return (EINVAL);
10153 	}
10154 
10155 	dev = *dev_p;
10156 	instance = SDUNIT(dev);
10157 	mutex_enter(&sd_detach_mutex);
10158 
10159 	/*
10160 	 * Fail the open if there is no softstate for the instance, or
10161 	 * if another thread somewhere is trying to detach the instance.
10162 	 */
10163 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10164 	    (un->un_detach_count != 0)) {
10165 		mutex_exit(&sd_detach_mutex);
10166 		/*
10167 		 * The probe cache only needs to be cleared when open (9e) fails
10168 		 * with ENXIO (4238046).
10169 		 */
10170 		/*
10171 		 * un-conditionally clearing probe cache is ok with
10172 		 * separate sd/ssd binaries
10173 		 * x86 platform can be an issue with both parallel
10174 		 * and fibre in 1 binary
10175 		 */
10176 		sd_scsi_clear_probe_cache();
10177 		return (ENXIO);
10178 	}
10179 
10180 	/*
10181 	 * The un_layer_count is to prevent another thread in specfs from
10182 	 * trying to detach the instance, which can happen when we are
10183 	 * called from a higher-layer driver instead of thru specfs.
10184 	 * This will not be needed when DDI provides a layered driver
10185 	 * interface that allows specfs to know that an instance is in
10186 	 * use by a layered driver & should not be detached.
10187 	 *
10188 	 * Note: the semantics for layered driver opens are exactly one
10189 	 * close for every open.
10190 	 */
10191 	if (otyp == OTYP_LYR) {
10192 		un->un_layer_count++;
10193 	}
10194 
10195 	/*
10196 	 * Keep a count of the current # of opens in progress. This is because
10197 	 * some layered drivers try to call us as a regular open. This can
10198 	 * cause problems that we cannot prevent, however by keeping this count
10199 	 * we can at least keep our open and detach routines from racing against
10200 	 * each other under such conditions.
10201 	 */
10202 	un->un_opens_in_progress++;
10203 	mutex_exit(&sd_detach_mutex);
10204 
10205 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10206 	part	 = SDPART(dev);
10207 	partmask = 1 << part;
10208 
10209 	/*
10210 	 * We use a semaphore here in order to serialize
10211 	 * open and close requests on the device.
10212 	 */
10213 	sema_p(&un->un_semoclose);
10214 
10215 	mutex_enter(SD_MUTEX(un));
10216 
10217 	/*
10218 	 * All device accesses go thru sdstrategy() where we check
10219 	 * on suspend status but there could be a scsi_poll command,
10220 	 * which bypasses sdstrategy(), so we need to check pm
10221 	 * status.
10222 	 */
10223 
10224 	if (!nodelay) {
10225 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10226 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10227 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10228 		}
10229 
10230 		mutex_exit(SD_MUTEX(un));
10231 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10232 			rval = EIO;
10233 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10234 			    "sdopen: sd_pm_entry failed\n");
10235 			goto open_failed_with_pm;
10236 		}
10237 		mutex_enter(SD_MUTEX(un));
10238 	}
10239 
10240 	/* check for previous exclusive open */
10241 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10242 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10243 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10244 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10245 
10246 	if (un->un_exclopen & (partmask)) {
10247 		goto excl_open_fail;
10248 	}
10249 
10250 	if (flag & FEXCL) {
10251 		int i;
10252 		if (un->un_ocmap.lyropen[part]) {
10253 			goto excl_open_fail;
10254 		}
10255 		for (i = 0; i < (OTYPCNT - 1); i++) {
10256 			if (un->un_ocmap.regopen[i] & (partmask)) {
10257 				goto excl_open_fail;
10258 			}
10259 		}
10260 	}
10261 
10262 	/*
10263 	 * Check the write permission if this is a removable media device,
10264 	 * NDELAY has not been set, and writable permission is requested.
10265 	 *
10266 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10267 	 * attempt will fail with EIO as part of the I/O processing. This is a
10268 	 * more permissive implementation that allows the open to succeed and
10269 	 * WRITE attempts to fail when appropriate.
10270 	 */
10271 	if (un->un_f_chk_wp_open) {
10272 		if ((flag & FWRITE) && (!nodelay)) {
10273 			mutex_exit(SD_MUTEX(un));
10274 			/*
10275 			 * Defer the check for write permission on writable
10276 			 * DVD drive till sdstrategy and will not fail open even
10277 			 * if FWRITE is set as the device can be writable
10278 			 * depending upon the media and the media can change
10279 			 * after the call to open().
10280 			 */
10281 			if (un->un_f_dvdram_writable_device == FALSE) {
10282 				if (ISCD(un) || sr_check_wp(dev)) {
10283 				rval = EROFS;
10284 				mutex_enter(SD_MUTEX(un));
10285 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10286 				    "write to cd or write protected media\n");
10287 				goto open_fail;
10288 				}
10289 			}
10290 			mutex_enter(SD_MUTEX(un));
10291 		}
10292 	}
10293 
10294 	/*
10295 	 * If opening in NDELAY/NONBLOCK mode, just return.
10296 	 * Check if disk is ready and has a valid geometry later.
10297 	 */
10298 	if (!nodelay) {
10299 		mutex_exit(SD_MUTEX(un));
10300 		rval = sd_ready_and_valid(un);
10301 		mutex_enter(SD_MUTEX(un));
10302 		/*
10303 		 * Fail if device is not ready or if the number of disk
10304 		 * blocks is zero or negative for non CD devices.
10305 		 */
10306 		if ((rval != SD_READY_VALID) ||
10307 		    (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) {
10308 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10309 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10310 			    "device not ready or invalid disk block value\n");
10311 			goto open_fail;
10312 		}
10313 #if defined(__i386) || defined(__amd64)
10314 	} else {
10315 		uchar_t *cp;
10316 		/*
10317 		 * x86 requires special nodelay handling, so that p0 is
10318 		 * always defined and accessible.
10319 		 * Invalidate geometry only if device is not already open.
10320 		 */
10321 		cp = &un->un_ocmap.chkd[0];
10322 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10323 			if (*cp != (uchar_t)0) {
10324 			    break;
10325 			}
10326 			cp++;
10327 		}
10328 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10329 			un->un_f_geometry_is_valid = FALSE;
10330 		}
10331 
10332 #endif
10333 	}
10334 
10335 	if (otyp == OTYP_LYR) {
10336 		un->un_ocmap.lyropen[part]++;
10337 	} else {
10338 		un->un_ocmap.regopen[otyp] |= partmask;
10339 	}
10340 
10341 	/* Set up open and exclusive open flags */
10342 	if (flag & FEXCL) {
10343 		un->un_exclopen |= (partmask);
10344 	}
10345 
10346 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10347 	    "open of part %d type %d\n", part, otyp);
10348 
10349 	mutex_exit(SD_MUTEX(un));
10350 	if (!nodelay) {
10351 		sd_pm_exit(un);
10352 	}
10353 
10354 	sema_v(&un->un_semoclose);
10355 
10356 	mutex_enter(&sd_detach_mutex);
10357 	un->un_opens_in_progress--;
10358 	mutex_exit(&sd_detach_mutex);
10359 
10360 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10361 	return (DDI_SUCCESS);
10362 
10363 excl_open_fail:
10364 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10365 	rval = EBUSY;
10366 
10367 open_fail:
10368 	mutex_exit(SD_MUTEX(un));
10369 
10370 	/*
10371 	 * On a failed open we must exit the pm management.
10372 	 */
10373 	if (!nodelay) {
10374 		sd_pm_exit(un);
10375 	}
10376 open_failed_with_pm:
10377 	sema_v(&un->un_semoclose);
10378 
10379 	mutex_enter(&sd_detach_mutex);
10380 	un->un_opens_in_progress--;
10381 	if (otyp == OTYP_LYR) {
10382 		un->un_layer_count--;
10383 	}
10384 	mutex_exit(&sd_detach_mutex);
10385 
10386 	return (rval);
10387 }
10388 
10389 
10390 /*
10391  *    Function: sdclose
10392  *
10393  * Description: Driver's close(9e) entry point function.
10394  *
10395  *   Arguments: dev    - device number
10396  *		flag   - file status flag, informational only
10397  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10398  *		cred_p - user credential pointer
10399  *
10400  * Return Code: ENXIO
10401  *
10402  *     Context: Kernel thread context
10403  */
10404 /* ARGSUSED */
10405 static int
10406 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10407 {
10408 	struct sd_lun	*un;
10409 	uchar_t		*cp;
10410 	int		part;
10411 	int		nodelay;
10412 	int		rval = 0;
10413 
10414 	/* Validate the open type */
10415 	if (otyp >= OTYPCNT) {
10416 		return (ENXIO);
10417 	}
10418 
10419 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10420 		return (ENXIO);
10421 	}
10422 
10423 	part = SDPART(dev);
10424 	nodelay = flag & (FNDELAY | FNONBLOCK);
10425 
10426 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10427 	    "sdclose: close of part %d type %d\n", part, otyp);
10428 
10429 	/*
10430 	 * We use a semaphore here in order to serialize
10431 	 * open and close requests on the device.
10432 	 */
10433 	sema_p(&un->un_semoclose);
10434 
10435 	mutex_enter(SD_MUTEX(un));
10436 
10437 	/* Don't proceed if power is being changed. */
10438 	while (un->un_state == SD_STATE_PM_CHANGING) {
10439 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10440 	}
10441 
10442 	if (un->un_exclopen & (1 << part)) {
10443 		un->un_exclopen &= ~(1 << part);
10444 	}
10445 
10446 	/* Update the open partition map */
10447 	if (otyp == OTYP_LYR) {
10448 		un->un_ocmap.lyropen[part] -= 1;
10449 	} else {
10450 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10451 	}
10452 
10453 	cp = &un->un_ocmap.chkd[0];
10454 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10455 		if (*cp != NULL) {
10456 			break;
10457 		}
10458 		cp++;
10459 	}
10460 
10461 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10462 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10463 
10464 		/*
10465 		 * We avoid persistance upon the last close, and set
10466 		 * the throttle back to the maximum.
10467 		 */
10468 		un->un_throttle = un->un_saved_throttle;
10469 
10470 		if (un->un_state == SD_STATE_OFFLINE) {
10471 			if (un->un_f_is_fibre == FALSE) {
10472 				scsi_log(SD_DEVINFO(un), sd_label,
10473 					CE_WARN, "offline\n");
10474 			}
10475 			un->un_f_geometry_is_valid = FALSE;
10476 
10477 		} else {
10478 			/*
10479 			 * Flush any outstanding writes in NVRAM cache.
10480 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10481 			 * cmd, it may not work for non-Pluto devices.
10482 			 * SYNCHRONIZE CACHE is not required for removables,
10483 			 * except DVD-RAM drives.
10484 			 *
10485 			 * Also note: because SYNCHRONIZE CACHE is currently
10486 			 * the only command issued here that requires the
10487 			 * drive be powered up, only do the power up before
10488 			 * sending the Sync Cache command. If additional
10489 			 * commands are added which require a powered up
10490 			 * drive, the following sequence may have to change.
10491 			 *
10492 			 * And finally, note that parallel SCSI on SPARC
10493 			 * only issues a Sync Cache to DVD-RAM, a newly
10494 			 * supported device.
10495 			 */
10496 #if defined(__i386) || defined(__amd64)
10497 			if (un->un_f_sync_cache_supported ||
10498 			    un->un_f_dvdram_writable_device == TRUE) {
10499 #else
10500 			if (un->un_f_dvdram_writable_device == TRUE) {
10501 #endif
10502 				mutex_exit(SD_MUTEX(un));
10503 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10504 					rval =
10505 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
10506 					    NULL);
10507 					/* ignore error if not supported */
10508 					if (rval == ENOTSUP) {
10509 						rval = 0;
10510 					} else if (rval != 0) {
10511 						rval = EIO;
10512 					}
10513 					sd_pm_exit(un);
10514 				} else {
10515 					rval = EIO;
10516 				}
10517 				mutex_enter(SD_MUTEX(un));
10518 			}
10519 
10520 			/*
10521 			 * For devices which supports DOOR_LOCK, send an ALLOW
10522 			 * MEDIA REMOVAL command, but don't get upset if it
10523 			 * fails. We need to raise the power of the drive before
10524 			 * we can call sd_send_scsi_DOORLOCK()
10525 			 */
10526 			if (un->un_f_doorlock_supported) {
10527 				mutex_exit(SD_MUTEX(un));
10528 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10529 					rval = sd_send_scsi_DOORLOCK(un,
10530 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10531 
10532 					sd_pm_exit(un);
10533 					if (ISCD(un) && (rval != 0) &&
10534 					    (nodelay != 0)) {
10535 						rval = ENXIO;
10536 					}
10537 				} else {
10538 					rval = EIO;
10539 				}
10540 				mutex_enter(SD_MUTEX(un));
10541 			}
10542 
10543 			/*
10544 			 * If a device has removable media, invalidate all
10545 			 * parameters related to media, such as geometry,
10546 			 * blocksize, and blockcount.
10547 			 */
10548 			if (un->un_f_has_removable_media) {
10549 				sr_ejected(un);
10550 			}
10551 
10552 			/*
10553 			 * Destroy the cache (if it exists) which was
10554 			 * allocated for the write maps since this is
10555 			 * the last close for this media.
10556 			 */
10557 			if (un->un_wm_cache) {
10558 				/*
10559 				 * Check if there are pending commands.
10560 				 * and if there are give a warning and
10561 				 * do not destroy the cache.
10562 				 */
10563 				if (un->un_ncmds_in_driver > 0) {
10564 					scsi_log(SD_DEVINFO(un),
10565 					    sd_label, CE_WARN,
10566 					    "Unable to clean up memory "
10567 					    "because of pending I/O\n");
10568 				} else {
10569 					kmem_cache_destroy(
10570 					    un->un_wm_cache);
10571 					un->un_wm_cache = NULL;
10572 				}
10573 			}
10574 		}
10575 	}
10576 
10577 	mutex_exit(SD_MUTEX(un));
10578 	sema_v(&un->un_semoclose);
10579 
10580 	if (otyp == OTYP_LYR) {
10581 		mutex_enter(&sd_detach_mutex);
10582 		/*
10583 		 * The detach routine may run when the layer count
10584 		 * drops to zero.
10585 		 */
10586 		un->un_layer_count--;
10587 		mutex_exit(&sd_detach_mutex);
10588 	}
10589 
10590 	return (rval);
10591 }
10592 
10593 
10594 /*
10595  *    Function: sd_ready_and_valid
10596  *
10597  * Description: Test if device is ready and has a valid geometry.
10598  *
10599  *   Arguments: dev - device number
10600  *		un  - driver soft state (unit) structure
10601  *
10602  * Return Code: SD_READY_VALID		ready and valid label
10603  *		SD_READY_NOT_VALID	ready, geom ops never applicable
10604  *		SD_NOT_READY_VALID	not ready, no label
10605  *
10606  *     Context: Never called at interrupt context.
10607  */
10608 
10609 static int
10610 sd_ready_and_valid(struct sd_lun *un)
10611 {
10612 	struct sd_errstats	*stp;
10613 	uint64_t		capacity;
10614 	uint_t			lbasize;
10615 	int			rval = SD_READY_VALID;
10616 	char			name_str[48];
10617 
10618 	ASSERT(un != NULL);
10619 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10620 
10621 	mutex_enter(SD_MUTEX(un));
10622 	/*
10623 	 * If a device has removable media, we must check if media is
10624 	 * ready when checking if this device is ready and valid.
10625 	 */
10626 	if (un->un_f_has_removable_media) {
10627 		mutex_exit(SD_MUTEX(un));
10628 		if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) {
10629 			rval = SD_NOT_READY_VALID;
10630 			mutex_enter(SD_MUTEX(un));
10631 			goto done;
10632 		}
10633 
10634 		mutex_enter(SD_MUTEX(un));
10635 		if ((un->un_f_geometry_is_valid == FALSE) ||
10636 		    (un->un_f_blockcount_is_valid == FALSE) ||
10637 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10638 
10639 			/* capacity has to be read every open. */
10640 			mutex_exit(SD_MUTEX(un));
10641 			if (sd_send_scsi_READ_CAPACITY(un, &capacity,
10642 			    &lbasize, SD_PATH_DIRECT) != 0) {
10643 				mutex_enter(SD_MUTEX(un));
10644 				un->un_f_geometry_is_valid = FALSE;
10645 				rval = SD_NOT_READY_VALID;
10646 				goto done;
10647 			} else {
10648 				mutex_enter(SD_MUTEX(un));
10649 				sd_update_block_info(un, lbasize, capacity);
10650 			}
10651 		}
10652 
10653 		/*
10654 		 * Check if the media in the device is writable or not.
10655 		 */
10656 		if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) {
10657 			sd_check_for_writable_cd(un);
10658 		}
10659 
10660 	} else {
10661 		/*
10662 		 * Do a test unit ready to clear any unit attention from non-cd
10663 		 * devices.
10664 		 */
10665 		mutex_exit(SD_MUTEX(un));
10666 		(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
10667 		mutex_enter(SD_MUTEX(un));
10668 	}
10669 
10670 
10671 	/*
10672 	 * If this is a non 512 block device, allocate space for
10673 	 * the wmap cache. This is being done here since every time
10674 	 * a media is changed this routine will be called and the
10675 	 * block size is a function of media rather than device.
10676 	 */
10677 	if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) {
10678 		if (!(un->un_wm_cache)) {
10679 			(void) snprintf(name_str, sizeof (name_str),
10680 			    "%s%d_cache",
10681 			    ddi_driver_name(SD_DEVINFO(un)),
10682 			    ddi_get_instance(SD_DEVINFO(un)));
10683 			un->un_wm_cache = kmem_cache_create(
10684 			    name_str, sizeof (struct sd_w_map),
10685 			    8, sd_wm_cache_constructor,
10686 			    sd_wm_cache_destructor, NULL,
10687 			    (void *)un, NULL, 0);
10688 			if (!(un->un_wm_cache)) {
10689 					rval = ENOMEM;
10690 					goto done;
10691 			}
10692 		}
10693 	}
10694 
10695 	if (un->un_state == SD_STATE_NORMAL) {
10696 		/*
10697 		 * If the target is not yet ready here (defined by a TUR
10698 		 * failure), invalidate the geometry and print an 'offline'
10699 		 * message. This is a legacy message, as the state of the
10700 		 * target is not actually changed to SD_STATE_OFFLINE.
10701 		 *
10702 		 * If the TUR fails for EACCES (Reservation Conflict), it
10703 		 * means there actually is nothing wrong with the target that
10704 		 * would require invalidating the geometry, so continue in
10705 		 * that case as if the TUR was successful.
10706 		 */
10707 		int err;
10708 
10709 		mutex_exit(SD_MUTEX(un));
10710 		err = sd_send_scsi_TEST_UNIT_READY(un, 0);
10711 		mutex_enter(SD_MUTEX(un));
10712 
10713 		if ((err != 0) && (err != EACCES)) {
10714 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10715 			    "offline\n");
10716 			un->un_f_geometry_is_valid = FALSE;
10717 			rval = SD_NOT_READY_VALID;
10718 			goto done;
10719 		}
10720 	}
10721 
10722 	if (un->un_f_format_in_progress == FALSE) {
10723 		/*
10724 		 * Note: sd_validate_geometry may return TRUE, but that does
10725 		 * not necessarily mean un_f_geometry_is_valid == TRUE!
10726 		 */
10727 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
10728 		if (rval == ENOTSUP) {
10729 			if (un->un_f_geometry_is_valid == TRUE)
10730 				rval = 0;
10731 			else {
10732 				rval = SD_READY_NOT_VALID;
10733 				goto done;
10734 			}
10735 		}
10736 		if (rval != 0) {
10737 			/*
10738 			 * We don't check the validity of geometry for
10739 			 * CDROMs. Also we assume we have a good label
10740 			 * even if sd_validate_geometry returned ENOMEM.
10741 			 */
10742 			if (!ISCD(un) && rval != ENOMEM) {
10743 				rval = SD_NOT_READY_VALID;
10744 				goto done;
10745 			}
10746 		}
10747 	}
10748 
10749 	/*
10750 	 * If this device supports DOOR_LOCK command, try and send
10751 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10752 	 * if it fails. For a CD, however, it is an error
10753 	 */
10754 	if (un->un_f_doorlock_supported) {
10755 		mutex_exit(SD_MUTEX(un));
10756 		if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
10757 		    SD_PATH_DIRECT) != 0) && ISCD(un)) {
10758 			rval = SD_NOT_READY_VALID;
10759 			mutex_enter(SD_MUTEX(un));
10760 			goto done;
10761 		}
10762 		mutex_enter(SD_MUTEX(un));
10763 	}
10764 
10765 	/* The state has changed, inform the media watch routines */
10766 	un->un_mediastate = DKIO_INSERTED;
10767 	cv_broadcast(&un->un_state_cv);
10768 	rval = SD_READY_VALID;
10769 
10770 done:
10771 
10772 	/*
10773 	 * Initialize the capacity kstat value, if no media previously
10774 	 * (capacity kstat is 0) and a media has been inserted
10775 	 * (un_blockcount > 0).
10776 	 */
10777 	if (un->un_errstats != NULL) {
10778 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
10779 		if ((stp->sd_capacity.value.ui64 == 0) &&
10780 		    (un->un_f_blockcount_is_valid == TRUE)) {
10781 			stp->sd_capacity.value.ui64 =
10782 			    (uint64_t)((uint64_t)un->un_blockcount *
10783 			    un->un_sys_blocksize);
10784 		}
10785 	}
10786 
10787 	mutex_exit(SD_MUTEX(un));
10788 	return (rval);
10789 }
10790 
10791 
10792 /*
10793  *    Function: sdmin
10794  *
10795  * Description: Routine to limit the size of a data transfer. Used in
10796  *		conjunction with physio(9F).
10797  *
10798  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10799  *
10800  *     Context: Kernel thread context.
10801  */
10802 
10803 static void
10804 sdmin(struct buf *bp)
10805 {
10806 	struct sd_lun	*un;
10807 	int		instance;
10808 
10809 	instance = SDUNIT(bp->b_edev);
10810 
10811 	un = ddi_get_soft_state(sd_state, instance);
10812 	ASSERT(un != NULL);
10813 
10814 	if (bp->b_bcount > un->un_max_xfer_size) {
10815 		bp->b_bcount = un->un_max_xfer_size;
10816 	}
10817 }
10818 
10819 
10820 /*
10821  *    Function: sdread
10822  *
10823  * Description: Driver's read(9e) entry point function.
10824  *
10825  *   Arguments: dev   - device number
10826  *		uio   - structure pointer describing where data is to be stored
10827  *			in user's space
10828  *		cred_p  - user credential pointer
10829  *
10830  * Return Code: ENXIO
10831  *		EIO
10832  *		EINVAL
10833  *		value returned by physio
10834  *
10835  *     Context: Kernel thread context.
10836  */
10837 /* ARGSUSED */
10838 static int
10839 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10840 {
10841 	struct sd_lun	*un = NULL;
10842 	int		secmask;
10843 	int		err;
10844 
10845 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10846 		return (ENXIO);
10847 	}
10848 
10849 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10850 
10851 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10852 		mutex_enter(SD_MUTEX(un));
10853 		/*
10854 		 * Because the call to sd_ready_and_valid will issue I/O we
10855 		 * must wait here if either the device is suspended or
10856 		 * if it's power level is changing.
10857 		 */
10858 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10859 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10860 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10861 		}
10862 		un->un_ncmds_in_driver++;
10863 		mutex_exit(SD_MUTEX(un));
10864 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
10865 			mutex_enter(SD_MUTEX(un));
10866 			un->un_ncmds_in_driver--;
10867 			ASSERT(un->un_ncmds_in_driver >= 0);
10868 			mutex_exit(SD_MUTEX(un));
10869 			return (EIO);
10870 		}
10871 		mutex_enter(SD_MUTEX(un));
10872 		un->un_ncmds_in_driver--;
10873 		ASSERT(un->un_ncmds_in_driver >= 0);
10874 		mutex_exit(SD_MUTEX(un));
10875 	}
10876 
10877 	/*
10878 	 * Read requests are restricted to multiples of the system block size.
10879 	 */
10880 	secmask = un->un_sys_blocksize - 1;
10881 
10882 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10883 		SD_ERROR(SD_LOG_READ_WRITE, un,
10884 		    "sdread: file offset not modulo %d\n",
10885 		    un->un_sys_blocksize);
10886 		err = EINVAL;
10887 	} else if (uio->uio_iov->iov_len & (secmask)) {
10888 		SD_ERROR(SD_LOG_READ_WRITE, un,
10889 		    "sdread: transfer length not modulo %d\n",
10890 		    un->un_sys_blocksize);
10891 		err = EINVAL;
10892 	} else {
10893 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10894 	}
10895 	return (err);
10896 }
10897 
10898 
10899 /*
10900  *    Function: sdwrite
10901  *
10902  * Description: Driver's write(9e) entry point function.
10903  *
10904  *   Arguments: dev   - device number
10905  *		uio   - structure pointer describing where data is stored in
10906  *			user's space
10907  *		cred_p  - user credential pointer
10908  *
10909  * Return Code: ENXIO
10910  *		EIO
10911  *		EINVAL
10912  *		value returned by physio
10913  *
10914  *     Context: Kernel thread context.
10915  */
10916 /* ARGSUSED */
10917 static int
10918 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10919 {
10920 	struct sd_lun	*un = NULL;
10921 	int		secmask;
10922 	int		err;
10923 
10924 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10925 		return (ENXIO);
10926 	}
10927 
10928 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10929 
10930 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10931 		mutex_enter(SD_MUTEX(un));
10932 		/*
10933 		 * Because the call to sd_ready_and_valid will issue I/O we
10934 		 * must wait here if either the device is suspended or
10935 		 * if it's power level is changing.
10936 		 */
10937 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10938 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10939 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10940 		}
10941 		un->un_ncmds_in_driver++;
10942 		mutex_exit(SD_MUTEX(un));
10943 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
10944 			mutex_enter(SD_MUTEX(un));
10945 			un->un_ncmds_in_driver--;
10946 			ASSERT(un->un_ncmds_in_driver >= 0);
10947 			mutex_exit(SD_MUTEX(un));
10948 			return (EIO);
10949 		}
10950 		mutex_enter(SD_MUTEX(un));
10951 		un->un_ncmds_in_driver--;
10952 		ASSERT(un->un_ncmds_in_driver >= 0);
10953 		mutex_exit(SD_MUTEX(un));
10954 	}
10955 
10956 	/*
10957 	 * Write requests are restricted to multiples of the system block size.
10958 	 */
10959 	secmask = un->un_sys_blocksize - 1;
10960 
10961 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10962 		SD_ERROR(SD_LOG_READ_WRITE, un,
10963 		    "sdwrite: file offset not modulo %d\n",
10964 		    un->un_sys_blocksize);
10965 		err = EINVAL;
10966 	} else if (uio->uio_iov->iov_len & (secmask)) {
10967 		SD_ERROR(SD_LOG_READ_WRITE, un,
10968 		    "sdwrite: transfer length not modulo %d\n",
10969 		    un->un_sys_blocksize);
10970 		err = EINVAL;
10971 	} else {
10972 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
10973 	}
10974 	return (err);
10975 }
10976 
10977 
10978 /*
10979  *    Function: sdaread
10980  *
10981  * Description: Driver's aread(9e) entry point function.
10982  *
10983  *   Arguments: dev   - device number
10984  *		aio   - structure pointer describing where data is to be stored
10985  *		cred_p  - user credential pointer
10986  *
10987  * Return Code: ENXIO
10988  *		EIO
10989  *		EINVAL
10990  *		value returned by aphysio
10991  *
10992  *     Context: Kernel thread context.
10993  */
10994 /* ARGSUSED */
10995 static int
10996 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10997 {
10998 	struct sd_lun	*un = NULL;
10999 	struct uio	*uio = aio->aio_uio;
11000 	int		secmask;
11001 	int		err;
11002 
11003 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11004 		return (ENXIO);
11005 	}
11006 
11007 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11008 
11009 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11010 		mutex_enter(SD_MUTEX(un));
11011 		/*
11012 		 * Because the call to sd_ready_and_valid will issue I/O we
11013 		 * must wait here if either the device is suspended or
11014 		 * if it's power level is changing.
11015 		 */
11016 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11017 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11018 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11019 		}
11020 		un->un_ncmds_in_driver++;
11021 		mutex_exit(SD_MUTEX(un));
11022 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11023 			mutex_enter(SD_MUTEX(un));
11024 			un->un_ncmds_in_driver--;
11025 			ASSERT(un->un_ncmds_in_driver >= 0);
11026 			mutex_exit(SD_MUTEX(un));
11027 			return (EIO);
11028 		}
11029 		mutex_enter(SD_MUTEX(un));
11030 		un->un_ncmds_in_driver--;
11031 		ASSERT(un->un_ncmds_in_driver >= 0);
11032 		mutex_exit(SD_MUTEX(un));
11033 	}
11034 
11035 	/*
11036 	 * Read requests are restricted to multiples of the system block size.
11037 	 */
11038 	secmask = un->un_sys_blocksize - 1;
11039 
11040 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11041 		SD_ERROR(SD_LOG_READ_WRITE, un,
11042 		    "sdaread: file offset not modulo %d\n",
11043 		    un->un_sys_blocksize);
11044 		err = EINVAL;
11045 	} else if (uio->uio_iov->iov_len & (secmask)) {
11046 		SD_ERROR(SD_LOG_READ_WRITE, un,
11047 		    "sdaread: transfer length not modulo %d\n",
11048 		    un->un_sys_blocksize);
11049 		err = EINVAL;
11050 	} else {
11051 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11052 	}
11053 	return (err);
11054 }
11055 
11056 
11057 /*
11058  *    Function: sdawrite
11059  *
11060  * Description: Driver's awrite(9e) entry point function.
11061  *
11062  *   Arguments: dev   - device number
11063  *		aio   - structure pointer describing where data is stored
11064  *		cred_p  - user credential pointer
11065  *
11066  * Return Code: ENXIO
11067  *		EIO
11068  *		EINVAL
11069  *		value returned by aphysio
11070  *
11071  *     Context: Kernel thread context.
11072  */
11073 /* ARGSUSED */
11074 static int
11075 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11076 {
11077 	struct sd_lun	*un = NULL;
11078 	struct uio	*uio = aio->aio_uio;
11079 	int		secmask;
11080 	int		err;
11081 
11082 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11083 		return (ENXIO);
11084 	}
11085 
11086 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11087 
11088 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11089 		mutex_enter(SD_MUTEX(un));
11090 		/*
11091 		 * Because the call to sd_ready_and_valid will issue I/O we
11092 		 * must wait here if either the device is suspended or
11093 		 * if it's power level is changing.
11094 		 */
11095 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11096 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11097 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11098 		}
11099 		un->un_ncmds_in_driver++;
11100 		mutex_exit(SD_MUTEX(un));
11101 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11102 			mutex_enter(SD_MUTEX(un));
11103 			un->un_ncmds_in_driver--;
11104 			ASSERT(un->un_ncmds_in_driver >= 0);
11105 			mutex_exit(SD_MUTEX(un));
11106 			return (EIO);
11107 		}
11108 		mutex_enter(SD_MUTEX(un));
11109 		un->un_ncmds_in_driver--;
11110 		ASSERT(un->un_ncmds_in_driver >= 0);
11111 		mutex_exit(SD_MUTEX(un));
11112 	}
11113 
11114 	/*
11115 	 * Write requests are restricted to multiples of the system block size.
11116 	 */
11117 	secmask = un->un_sys_blocksize - 1;
11118 
11119 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11120 		SD_ERROR(SD_LOG_READ_WRITE, un,
11121 		    "sdawrite: file offset not modulo %d\n",
11122 		    un->un_sys_blocksize);
11123 		err = EINVAL;
11124 	} else if (uio->uio_iov->iov_len & (secmask)) {
11125 		SD_ERROR(SD_LOG_READ_WRITE, un,
11126 		    "sdawrite: transfer length not modulo %d\n",
11127 		    un->un_sys_blocksize);
11128 		err = EINVAL;
11129 	} else {
11130 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11131 	}
11132 	return (err);
11133 }
11134 
11135 
11136 
11137 
11138 
11139 /*
11140  * Driver IO processing follows the following sequence:
11141  *
11142  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11143  *         |                |                     ^
11144  *         v                v                     |
11145  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11146  *         |                |                     |                   |
11147  *         v                |                     |                   |
11148  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11149  *         |                |                     ^                   ^
11150  *         v                v                     |                   |
11151  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11152  *         |                |                     |                   |
11153  *     +---+                |                     +------------+      +-------+
11154  *     |                    |                                  |              |
11155  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11156  *     |                    v                                  |              |
11157  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11158  *     |                    |                                  ^              |
11159  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11160  *     |                    v                                  |              |
11161  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11162  *     |                    |                                  ^              |
11163  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11164  *     |                    v                                  |              |
11165  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11166  *     |                    |                                  ^              |
11167  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11168  *     |                    v                                  |              |
11169  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11170  *     |                    |                                  ^              |
11171  *     |                    |                                  |              |
11172  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11173  *                          |                           ^
11174  *                          v                           |
11175  *                   sd_core_iostart()                  |
11176  *                          |                           |
11177  *                          |                           +------>(*destroypkt)()
11178  *                          +-> sd_start_cmds() <-+     |           |
11179  *                          |                     |     |           v
11180  *                          |                     |     |  scsi_destroy_pkt(9F)
11181  *                          |                     |     |
11182  *                          +->(*initpkt)()       +- sdintr()
11183  *                          |  |                        |  |
11184  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11185  *                          |  +-> scsi_setup_cdb(9F)   |
11186  *                          |                           |
11187  *                          +--> scsi_transport(9F)     |
11188  *                                     |                |
11189  *                                     +----> SCSA ---->+
11190  *
11191  *
11192  * This code is based upon the following presumtions:
11193  *
11194  *   - iostart and iodone functions operate on buf(9S) structures. These
11195  *     functions perform the necessary operations on the buf(9S) and pass
11196  *     them along to the next function in the chain by using the macros
11197  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11198  *     (for iodone side functions).
11199  *
11200  *   - The iostart side functions may sleep. The iodone side functions
11201  *     are called under interrupt context and may NOT sleep. Therefore
11202  *     iodone side functions also may not call iostart side functions.
11203  *     (NOTE: iostart side functions should NOT sleep for memory, as
11204  *     this could result in deadlock.)
11205  *
11206  *   - An iostart side function may call its corresponding iodone side
11207  *     function directly (if necessary).
11208  *
11209  *   - In the event of an error, an iostart side function can return a buf(9S)
11210  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11211  *     b_error in the usual way of course).
11212  *
11213  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11214  *     requests to the iostart side functions.  The iostart side functions in
11215  *     this case would be called under the context of a taskq thread, so it's
11216  *     OK for them to block/sleep/spin in this case.
11217  *
11218  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11219  *     pass them along to the next function in the chain.  The corresponding
11220  *     iodone side functions must coalesce the "shadow" bufs and return
11221  *     the "original" buf to the next higher layer.
11222  *
11223  *   - The b_private field of the buf(9S) struct holds a pointer to
11224  *     an sd_xbuf struct, which contains information needed to
11225  *     construct the scsi_pkt for the command.
11226  *
11227  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11228  *     layer must acquire & release the SD_MUTEX(un) as needed.
11229  */
11230 
11231 
11232 /*
11233  * Create taskq for all targets in the system. This is created at
11234  * _init(9E) and destroyed at _fini(9E).
11235  *
11236  * Note: here we set the minalloc to a reasonably high number to ensure that
11237  * we will have an adequate supply of task entries available at interrupt time.
11238  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11239  * sd_create_taskq().  Since we do not want to sleep for allocations at
11240  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11241  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11242  * requests any one instant in time.
11243  */
11244 #define	SD_TASKQ_NUMTHREADS	8
11245 #define	SD_TASKQ_MINALLOC	256
11246 #define	SD_TASKQ_MAXALLOC	256
11247 
11248 static taskq_t	*sd_tq = NULL;
11249 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11250 
11251 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11252 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11253 
11254 /*
11255  * The following task queue is being created for the write part of
11256  * read-modify-write of non-512 block size devices.
11257  * Limit the number of threads to 1 for now. This number has been choosen
11258  * considering the fact that it applies only to dvd ram drives/MO drives
11259  * currently. Performance for which is not main criteria at this stage.
11260  * Note: It needs to be explored if we can use a single taskq in future
11261  */
11262 #define	SD_WMR_TASKQ_NUMTHREADS	1
11263 static taskq_t	*sd_wmr_tq = NULL;
11264 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11265 
11266 /*
11267  *    Function: sd_taskq_create
11268  *
11269  * Description: Create taskq thread(s) and preallocate task entries
11270  *
11271  * Return Code: Returns a pointer to the allocated taskq_t.
11272  *
11273  *     Context: Can sleep. Requires blockable context.
11274  *
11275  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11276  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11277  *		- taskq_create() will block for memory, also it will panic
11278  *		  if it cannot create the requested number of threads.
11279  *		- Currently taskq_create() creates threads that cannot be
11280  *		  swapped.
11281  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11282  *		  supply of taskq entries at interrupt time (ie, so that we
11283  *		  do not have to sleep for memory)
11284  */
11285 
11286 static void
11287 sd_taskq_create(void)
11288 {
11289 	char	taskq_name[TASKQ_NAMELEN];
11290 
11291 	ASSERT(sd_tq == NULL);
11292 	ASSERT(sd_wmr_tq == NULL);
11293 
11294 	(void) snprintf(taskq_name, sizeof (taskq_name),
11295 	    "%s_drv_taskq", sd_label);
11296 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11297 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11298 	    TASKQ_PREPOPULATE));
11299 
11300 	(void) snprintf(taskq_name, sizeof (taskq_name),
11301 	    "%s_rmw_taskq", sd_label);
11302 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11303 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11304 	    TASKQ_PREPOPULATE));
11305 }
11306 
11307 
11308 /*
11309  *    Function: sd_taskq_delete
11310  *
11311  * Description: Complementary cleanup routine for sd_taskq_create().
11312  *
11313  *     Context: Kernel thread context.
11314  */
11315 
11316 static void
11317 sd_taskq_delete(void)
11318 {
11319 	ASSERT(sd_tq != NULL);
11320 	ASSERT(sd_wmr_tq != NULL);
11321 	taskq_destroy(sd_tq);
11322 	taskq_destroy(sd_wmr_tq);
11323 	sd_tq = NULL;
11324 	sd_wmr_tq = NULL;
11325 }
11326 
11327 
11328 /*
11329  *    Function: sdstrategy
11330  *
11331  * Description: Driver's strategy (9E) entry point function.
11332  *
11333  *   Arguments: bp - pointer to buf(9S)
11334  *
11335  * Return Code: Always returns zero
11336  *
11337  *     Context: Kernel thread context.
11338  */
11339 
11340 static int
11341 sdstrategy(struct buf *bp)
11342 {
11343 	struct sd_lun *un;
11344 
11345 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11346 	if (un == NULL) {
11347 		bioerror(bp, EIO);
11348 		bp->b_resid = bp->b_bcount;
11349 		biodone(bp);
11350 		return (0);
11351 	}
11352 	/* As was done in the past, fail new cmds. if state is dumping. */
11353 	if (un->un_state == SD_STATE_DUMPING) {
11354 		bioerror(bp, ENXIO);
11355 		bp->b_resid = bp->b_bcount;
11356 		biodone(bp);
11357 		return (0);
11358 	}
11359 
11360 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11361 
11362 	/*
11363 	 * Commands may sneak in while we released the mutex in
11364 	 * DDI_SUSPEND, we should block new commands. However, old
11365 	 * commands that are still in the driver at this point should
11366 	 * still be allowed to drain.
11367 	 */
11368 	mutex_enter(SD_MUTEX(un));
11369 	/*
11370 	 * Must wait here if either the device is suspended or
11371 	 * if it's power level is changing.
11372 	 */
11373 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11374 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11375 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11376 	}
11377 
11378 	un->un_ncmds_in_driver++;
11379 
11380 	/*
11381 	 * atapi: Since we are running the CD for now in PIO mode we need to
11382 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11383 	 * the HBA's init_pkt routine.
11384 	 */
11385 	if (un->un_f_cfg_is_atapi == TRUE) {
11386 		mutex_exit(SD_MUTEX(un));
11387 		bp_mapin(bp);
11388 		mutex_enter(SD_MUTEX(un));
11389 	}
11390 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11391 	    un->un_ncmds_in_driver);
11392 
11393 	mutex_exit(SD_MUTEX(un));
11394 
11395 	/*
11396 	 * This will (eventually) allocate the sd_xbuf area and
11397 	 * call sd_xbuf_strategy().  We just want to return the
11398 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11399 	 * imized tail call which saves us a stack frame.
11400 	 */
11401 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11402 }
11403 
11404 
11405 /*
11406  *    Function: sd_xbuf_strategy
11407  *
11408  * Description: Function for initiating IO operations via the
11409  *		ddi_xbuf_qstrategy() mechanism.
11410  *
11411  *     Context: Kernel thread context.
11412  */
11413 
11414 static void
11415 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11416 {
11417 	struct sd_lun *un = arg;
11418 
11419 	ASSERT(bp != NULL);
11420 	ASSERT(xp != NULL);
11421 	ASSERT(un != NULL);
11422 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11423 
11424 	/*
11425 	 * Initialize the fields in the xbuf and save a pointer to the
11426 	 * xbuf in bp->b_private.
11427 	 */
11428 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11429 
11430 	/* Send the buf down the iostart chain */
11431 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11432 }
11433 
11434 
11435 /*
11436  *    Function: sd_xbuf_init
11437  *
11438  * Description: Prepare the given sd_xbuf struct for use.
11439  *
11440  *   Arguments: un - ptr to softstate
11441  *		bp - ptr to associated buf(9S)
11442  *		xp - ptr to associated sd_xbuf
11443  *		chain_type - IO chain type to use:
11444  *			SD_CHAIN_NULL
11445  *			SD_CHAIN_BUFIO
11446  *			SD_CHAIN_USCSI
11447  *			SD_CHAIN_DIRECT
11448  *			SD_CHAIN_DIRECT_PRIORITY
11449  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11450  *			initialization; may be NULL if none.
11451  *
11452  *     Context: Kernel thread context
11453  */
11454 
11455 static void
11456 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11457 	uchar_t chain_type, void *pktinfop)
11458 {
11459 	int index;
11460 
11461 	ASSERT(un != NULL);
11462 	ASSERT(bp != NULL);
11463 	ASSERT(xp != NULL);
11464 
11465 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11466 	    bp, chain_type);
11467 
11468 	xp->xb_un	= un;
11469 	xp->xb_pktp	= NULL;
11470 	xp->xb_pktinfo	= pktinfop;
11471 	xp->xb_private	= bp->b_private;
11472 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11473 
11474 	/*
11475 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11476 	 * upon the specified chain type to use.
11477 	 */
11478 	switch (chain_type) {
11479 	case SD_CHAIN_NULL:
11480 		/*
11481 		 * Fall thru to just use the values for the buf type, even
11482 		 * tho for the NULL chain these values will never be used.
11483 		 */
11484 		/* FALLTHRU */
11485 	case SD_CHAIN_BUFIO:
11486 		index = un->un_buf_chain_type;
11487 		break;
11488 	case SD_CHAIN_USCSI:
11489 		index = un->un_uscsi_chain_type;
11490 		break;
11491 	case SD_CHAIN_DIRECT:
11492 		index = un->un_direct_chain_type;
11493 		break;
11494 	case SD_CHAIN_DIRECT_PRIORITY:
11495 		index = un->un_priority_chain_type;
11496 		break;
11497 	default:
11498 		/* We're really broken if we ever get here... */
11499 		panic("sd_xbuf_init: illegal chain type!");
11500 		/*NOTREACHED*/
11501 	}
11502 
11503 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11504 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11505 
11506 	/*
11507 	 * It might be a bit easier to simply bzero the entire xbuf above,
11508 	 * but it turns out that since we init a fair number of members anyway,
11509 	 * we save a fair number cycles by doing explicit assignment of zero.
11510 	 */
11511 	xp->xb_pkt_flags	= 0;
11512 	xp->xb_dma_resid	= 0;
11513 	xp->xb_retry_count	= 0;
11514 	xp->xb_victim_retry_count = 0;
11515 	xp->xb_ua_retry_count	= 0;
11516 	xp->xb_sense_bp		= NULL;
11517 	xp->xb_sense_status	= 0;
11518 	xp->xb_sense_state	= 0;
11519 	xp->xb_sense_resid	= 0;
11520 
11521 	bp->b_private	= xp;
11522 	bp->b_flags	&= ~(B_DONE | B_ERROR);
11523 	bp->b_resid	= 0;
11524 	bp->av_forw	= NULL;
11525 	bp->av_back	= NULL;
11526 	bioerror(bp, 0);
11527 
11528 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11529 }
11530 
11531 
11532 /*
11533  *    Function: sd_uscsi_strategy
11534  *
11535  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11536  *
11537  *   Arguments: bp - buf struct ptr
11538  *
11539  * Return Code: Always returns 0
11540  *
11541  *     Context: Kernel thread context
11542  */
11543 
11544 static int
11545 sd_uscsi_strategy(struct buf *bp)
11546 {
11547 	struct sd_lun		*un;
11548 	struct sd_uscsi_info	*uip;
11549 	struct sd_xbuf		*xp;
11550 	uchar_t			chain_type;
11551 
11552 	ASSERT(bp != NULL);
11553 
11554 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11555 	if (un == NULL) {
11556 		bioerror(bp, EIO);
11557 		bp->b_resid = bp->b_bcount;
11558 		biodone(bp);
11559 		return (0);
11560 	}
11561 
11562 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11563 
11564 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11565 
11566 	mutex_enter(SD_MUTEX(un));
11567 	/*
11568 	 * atapi: Since we are running the CD for now in PIO mode we need to
11569 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11570 	 * the HBA's init_pkt routine.
11571 	 */
11572 	if (un->un_f_cfg_is_atapi == TRUE) {
11573 		mutex_exit(SD_MUTEX(un));
11574 		bp_mapin(bp);
11575 		mutex_enter(SD_MUTEX(un));
11576 	}
11577 	un->un_ncmds_in_driver++;
11578 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11579 	    un->un_ncmds_in_driver);
11580 	mutex_exit(SD_MUTEX(un));
11581 
11582 	/*
11583 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11584 	 */
11585 	ASSERT(bp->b_private != NULL);
11586 	uip = (struct sd_uscsi_info *)bp->b_private;
11587 
11588 	switch (uip->ui_flags) {
11589 	case SD_PATH_DIRECT:
11590 		chain_type = SD_CHAIN_DIRECT;
11591 		break;
11592 	case SD_PATH_DIRECT_PRIORITY:
11593 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
11594 		break;
11595 	default:
11596 		chain_type = SD_CHAIN_USCSI;
11597 		break;
11598 	}
11599 
11600 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
11601 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11602 
11603 	/* Use the index obtained within xbuf_init */
11604 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11605 
11606 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11607 
11608 	return (0);
11609 }
11610 
11611 
11612 /*
11613  * These routines perform raw i/o operations.
11614  */
11615 /*ARGSUSED*/
11616 static void
11617 sduscsimin(struct buf *bp)
11618 {
11619 	/*
11620 	 * do not break up because the CDB count would then
11621 	 * be incorrect and data underruns would result (incomplete
11622 	 * read/writes which would be retried and then failed, see
11623 	 * sdintr().
11624 	 */
11625 }
11626 
11627 
11628 
11629 /*
11630  *    Function: sd_send_scsi_cmd
11631  *
11632  * Description: Runs a USCSI command for user (when called thru sdioctl),
11633  *		or for the driver
11634  *
11635  *   Arguments: dev - the dev_t for the device
11636  *		incmd - ptr to a valid uscsi_cmd struct
11637  *		cdbspace - UIO_USERSPACE or UIO_SYSSPACE
11638  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11639  *		rqbufspace - UIO_USERSPACE or UIO_SYSSPACE
11640  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11641  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11642  *			to use the USCSI "direct" chain and bypass the normal
11643  *			command waitq.
11644  *
11645  * Return Code: 0 -  successful completion of the given command
11646  *		EIO - scsi_reset() failed, or see biowait()/physio() codes.
11647  *		ENXIO  - soft state not found for specified dev
11648  *		EINVAL
11649  *		EFAULT - copyin/copyout error
11650  *		return code of biowait(9F) or physio(9F):
11651  *			EIO - IO error, caller may check incmd->uscsi_status
11652  *			ENXIO
11653  *			EACCES - reservation conflict
11654  *
11655  *     Context: Waits for command to complete. Can sleep.
11656  */
11657 
11658 static int
11659 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd,
11660 	enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace,
11661 	int path_flag)
11662 {
11663 	struct sd_uscsi_info	*uip;
11664 	struct uscsi_cmd	*uscmd;
11665 	struct sd_lun	*un;
11666 	struct buf	*bp;
11667 	int	rval;
11668 	int	flags;
11669 
11670 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11671 	if (un == NULL) {
11672 		return (ENXIO);
11673 	}
11674 
11675 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11676 
11677 #ifdef SDDEBUG
11678 	switch (dataspace) {
11679 	case UIO_USERSPACE:
11680 		SD_TRACE(SD_LOG_IO, un,
11681 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un);
11682 		break;
11683 	case UIO_SYSSPACE:
11684 		SD_TRACE(SD_LOG_IO, un,
11685 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un);
11686 		break;
11687 	default:
11688 		SD_TRACE(SD_LOG_IO, un,
11689 		    "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un);
11690 		break;
11691 	}
11692 #endif
11693 
11694 	/*
11695 	 * Perform resets directly; no need to generate a command to do it.
11696 	 */
11697 	if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) {
11698 		flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ?
11699 		    RESET_ALL : RESET_TARGET;
11700 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n");
11701 		if (scsi_reset(SD_ADDRESS(un), flags) == 0) {
11702 			/* Reset attempt was unsuccessful */
11703 			SD_TRACE(SD_LOG_IO, un,
11704 			    "sd_send_scsi_cmd: reset: failure\n");
11705 			return (EIO);
11706 		}
11707 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n");
11708 		return (0);
11709 	}
11710 
11711 	/* Perfunctory sanity check... */
11712 	if (incmd->uscsi_cdblen <= 0) {
11713 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11714 		    "invalid uscsi_cdblen, returning EINVAL\n");
11715 		return (EINVAL);
11716 	} else if (incmd->uscsi_cdblen > un->un_max_hba_cdb) {
11717 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11718 		    "unsupported uscsi_cdblen, returning EINVAL\n");
11719 		return (EINVAL);
11720 	}
11721 
11722 	/*
11723 	 * In order to not worry about where the uscsi structure came from
11724 	 * (or where the cdb it points to came from) we're going to make
11725 	 * kmem_alloc'd copies of them here. This will also allow reference
11726 	 * to the data they contain long after this process has gone to
11727 	 * sleep and its kernel stack has been unmapped, etc.
11728 	 *
11729 	 * First get some memory for the uscsi_cmd struct and copy the
11730 	 * contents of the given uscsi_cmd struct into it.
11731 	 */
11732 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11733 	bcopy(incmd, uscmd, sizeof (struct uscsi_cmd));
11734 
11735 	SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd",
11736 	    (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX);
11737 
11738 	/*
11739 	 * Now get some space for the CDB, and copy the given CDB into
11740 	 * it. Use ddi_copyin() in case the data is in user space.
11741 	 */
11742 	uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP);
11743 	flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0;
11744 	if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb,
11745 	    (uint_t)incmd->uscsi_cdblen, flags) != 0) {
11746 		kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen);
11747 		kmem_free(uscmd, sizeof (struct uscsi_cmd));
11748 		return (EFAULT);
11749 	}
11750 
11751 	SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB",
11752 	    (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX);
11753 
11754 	bp = getrbuf(KM_SLEEP);
11755 
11756 	/*
11757 	 * Allocate an sd_uscsi_info struct and fill it with the info
11758 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
11759 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
11760 	 * since we allocate the buf here in this function, we do not
11761 	 * need to preserve the prior contents of b_private.
11762 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
11763 	 */
11764 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11765 	uip->ui_flags = path_flag;
11766 	uip->ui_cmdp  = uscmd;
11767 	bp->b_private = uip;
11768 
11769 	/*
11770 	 * Initialize Request Sense buffering, if requested.
11771 	 */
11772 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
11773 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
11774 		/*
11775 		 * Here uscmd->uscsi_rqbuf currently points to the caller's
11776 		 * buffer, but we replace this with a kernel buffer that
11777 		 * we allocate to use with the sense data. The sense data
11778 		 * (if present) gets copied into this new buffer before the
11779 		 * command is completed.  Then we copy the sense data from
11780 		 * our allocated buf into the caller's buffer below. Note
11781 		 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used
11782 		 * below to perform the copy back to the caller's buf.
11783 		 */
11784 		uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
11785 		if (rqbufspace == UIO_USERSPACE) {
11786 			uscmd->uscsi_rqlen   = SENSE_LENGTH;
11787 			uscmd->uscsi_rqresid = SENSE_LENGTH;
11788 		} else {
11789 			uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen);
11790 			uscmd->uscsi_rqlen   = rlen;
11791 			uscmd->uscsi_rqresid = rlen;
11792 		}
11793 	} else {
11794 		uscmd->uscsi_rqbuf = NULL;
11795 		uscmd->uscsi_rqlen   = 0;
11796 		uscmd->uscsi_rqresid = 0;
11797 	}
11798 
11799 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p  rqlen:%d\n",
11800 	    uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen);
11801 
11802 	if (un->un_f_is_fibre == FALSE) {
11803 		/*
11804 		 * Force asynchronous mode, if necessary.  Doing this here
11805 		 * has the unfortunate effect of running other queued
11806 		 * commands async also, but since the main purpose of this
11807 		 * capability is downloading new drive firmware, we can
11808 		 * probably live with it.
11809 		 */
11810 		if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) {
11811 			if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1)
11812 				== 1) {
11813 				if (scsi_ifsetcap(SD_ADDRESS(un),
11814 					    "synchronous", 0, 1) == 1) {
11815 					SD_TRACE(SD_LOG_IO, un,
11816 					"sd_send_scsi_cmd: forced async ok\n");
11817 				} else {
11818 					SD_TRACE(SD_LOG_IO, un,
11819 					"sd_send_scsi_cmd:\
11820 					forced async failed\n");
11821 					rval = EINVAL;
11822 					goto done;
11823 				}
11824 			}
11825 		}
11826 
11827 		/*
11828 		 * Re-enable synchronous mode, if requested
11829 		 */
11830 		if (uscmd->uscsi_flags & USCSI_SYNC) {
11831 			if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1)
11832 				== 0) {
11833 				int i = scsi_ifsetcap(SD_ADDRESS(un),
11834 						"synchronous", 1, 1);
11835 				SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11836 					"re-enabled sync %s\n",
11837 					(i == 1) ? "ok" : "failed");
11838 			}
11839 		}
11840 	}
11841 
11842 	/*
11843 	 * Commands sent with priority are intended for error recovery
11844 	 * situations, and do not have retries performed.
11845 	 */
11846 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
11847 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
11848 	}
11849 
11850 	/*
11851 	 * If we're going to do actual I/O, let physio do all the right things
11852 	 */
11853 	if (uscmd->uscsi_buflen != 0) {
11854 		struct iovec	aiov;
11855 		struct uio	auio;
11856 		struct uio	*uio = &auio;
11857 
11858 		bzero(&auio, sizeof (struct uio));
11859 		bzero(&aiov, sizeof (struct iovec));
11860 		aiov.iov_base = uscmd->uscsi_bufaddr;
11861 		aiov.iov_len  = uscmd->uscsi_buflen;
11862 		uio->uio_iov  = &aiov;
11863 
11864 		uio->uio_iovcnt  = 1;
11865 		uio->uio_resid   = uscmd->uscsi_buflen;
11866 		uio->uio_segflg  = dataspace;
11867 
11868 		/*
11869 		 * physio() will block here until the command completes....
11870 		 */
11871 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n");
11872 
11873 		rval = physio(sd_uscsi_strategy, bp, dev,
11874 		    ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE),
11875 		    sduscsimin, uio);
11876 
11877 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11878 		    "returned from physio with 0x%x\n", rval);
11879 
11880 	} else {
11881 		/*
11882 		 * We have to mimic what physio would do here! Argh!
11883 		 */
11884 		bp->b_flags  = B_BUSY |
11885 		    ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE);
11886 		bp->b_edev   = dev;
11887 		bp->b_dev    = cmpdev(dev);	/* maybe unnecessary? */
11888 		bp->b_bcount = 0;
11889 		bp->b_blkno  = 0;
11890 
11891 		SD_TRACE(SD_LOG_IO, un,
11892 		    "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n");
11893 
11894 		(void) sd_uscsi_strategy(bp);
11895 
11896 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n");
11897 
11898 		rval = biowait(bp);
11899 
11900 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11901 		    "returned from  biowait with 0x%x\n", rval);
11902 	}
11903 
11904 done:
11905 
11906 #ifdef SDDEBUG
11907 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11908 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
11909 	    uscmd->uscsi_status, uscmd->uscsi_resid);
11910 	if (uscmd->uscsi_bufaddr != NULL) {
11911 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11912 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
11913 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
11914 		if (dataspace == UIO_SYSSPACE) {
11915 			SD_DUMP_MEMORY(un, SD_LOG_IO,
11916 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
11917 			    uscmd->uscsi_buflen, SD_LOG_HEX);
11918 		}
11919 	}
11920 #endif
11921 
11922 	/*
11923 	 * Get the status and residual to return to the caller.
11924 	 */
11925 	incmd->uscsi_status = uscmd->uscsi_status;
11926 	incmd->uscsi_resid  = uscmd->uscsi_resid;
11927 
11928 	/*
11929 	 * If the caller wants sense data, copy back whatever sense data
11930 	 * we may have gotten, and update the relevant rqsense info.
11931 	 */
11932 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
11933 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
11934 
11935 		int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid;
11936 		rqlen = min(((int)incmd->uscsi_rqlen), rqlen);
11937 
11938 		/* Update the Request Sense status and resid */
11939 		incmd->uscsi_rqresid  = incmd->uscsi_rqlen - rqlen;
11940 		incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus;
11941 
11942 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11943 		    "uscsi_rqstatus: 0x%02x  uscsi_rqresid:0x%x\n",
11944 		    incmd->uscsi_rqstatus, incmd->uscsi_rqresid);
11945 
11946 		/* Copy out the sense data for user processes */
11947 		if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) {
11948 			int flags =
11949 			    (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL;
11950 			if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf,
11951 			    rqlen, flags) != 0) {
11952 				rval = EFAULT;
11953 			}
11954 			/*
11955 			 * Note: Can't touch incmd->uscsi_rqbuf so use
11956 			 * uscmd->uscsi_rqbuf instead. They're the same.
11957 			 */
11958 			SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11959 			    "incmd->uscsi_rqbuf: 0x%p  rqlen:%d\n",
11960 			    incmd->uscsi_rqbuf, rqlen);
11961 			SD_DUMP_MEMORY(un, SD_LOG_IO, "rq",
11962 			    (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX);
11963 		}
11964 	}
11965 
11966 	/*
11967 	 * Free allocated resources and return; mapout the buf in case it was
11968 	 * mapped in by a lower layer.
11969 	 */
11970 	bp_mapout(bp);
11971 	freerbuf(bp);
11972 	kmem_free(uip, sizeof (struct sd_uscsi_info));
11973 	if (uscmd->uscsi_rqbuf != NULL) {
11974 		kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
11975 	}
11976 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
11977 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
11978 
11979 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n");
11980 
11981 	return (rval);
11982 }
11983 
11984 
11985 /*
11986  *    Function: sd_buf_iodone
11987  *
11988  * Description: Frees the sd_xbuf & returns the buf to its originator.
11989  *
11990  *     Context: May be called from interrupt context.
11991  */
11992 /* ARGSUSED */
11993 static void
11994 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
11995 {
11996 	struct sd_xbuf *xp;
11997 
11998 	ASSERT(un != NULL);
11999 	ASSERT(bp != NULL);
12000 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12001 
12002 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
12003 
12004 	xp = SD_GET_XBUF(bp);
12005 	ASSERT(xp != NULL);
12006 
12007 	mutex_enter(SD_MUTEX(un));
12008 
12009 	/*
12010 	 * Grab time when the cmd completed.
12011 	 * This is used for determining if the system has been
12012 	 * idle long enough to make it idle to the PM framework.
12013 	 * This is for lowering the overhead, and therefore improving
12014 	 * performance per I/O operation.
12015 	 */
12016 	un->un_pm_idle_time = ddi_get_time();
12017 
12018 	un->un_ncmds_in_driver--;
12019 	ASSERT(un->un_ncmds_in_driver >= 0);
12020 	SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12021 	    un->un_ncmds_in_driver);
12022 
12023 	mutex_exit(SD_MUTEX(un));
12024 
12025 	ddi_xbuf_done(bp, un->un_xbuf_attr);	/* xbuf is gone after this */
12026 	biodone(bp);				/* bp is gone after this */
12027 
12028 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12029 }
12030 
12031 
12032 /*
12033  *    Function: sd_uscsi_iodone
12034  *
12035  * Description: Frees the sd_xbuf & returns the buf to its originator.
12036  *
12037  *     Context: May be called from interrupt context.
12038  */
12039 /* ARGSUSED */
12040 static void
12041 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12042 {
12043 	struct sd_xbuf *xp;
12044 
12045 	ASSERT(un != NULL);
12046 	ASSERT(bp != NULL);
12047 
12048 	xp = SD_GET_XBUF(bp);
12049 	ASSERT(xp != NULL);
12050 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12051 
12052 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12053 
12054 	bp->b_private = xp->xb_private;
12055 
12056 	mutex_enter(SD_MUTEX(un));
12057 
12058 	/*
12059 	 * Grab time when the cmd completed.
12060 	 * This is used for determining if the system has been
12061 	 * idle long enough to make it idle to the PM framework.
12062 	 * This is for lowering the overhead, and therefore improving
12063 	 * performance per I/O operation.
12064 	 */
12065 	un->un_pm_idle_time = ddi_get_time();
12066 
12067 	un->un_ncmds_in_driver--;
12068 	ASSERT(un->un_ncmds_in_driver >= 0);
12069 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12070 	    un->un_ncmds_in_driver);
12071 
12072 	mutex_exit(SD_MUTEX(un));
12073 
12074 	kmem_free(xp, sizeof (struct sd_xbuf));
12075 	biodone(bp);
12076 
12077 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12078 }
12079 
12080 
12081 /*
12082  *    Function: sd_mapblockaddr_iostart
12083  *
12084  * Description: Verify request lies withing the partition limits for
12085  *		the indicated minor device.  Issue "overrun" buf if
12086  *		request would exceed partition range.  Converts
12087  *		partition-relative block address to absolute.
12088  *
12089  *     Context: Can sleep
12090  *
12091  *      Issues: This follows what the old code did, in terms of accessing
12092  *		some of the partition info in the unit struct without holding
12093  *		the mutext.  This is a general issue, if the partition info
12094  *		can be altered while IO is in progress... as soon as we send
12095  *		a buf, its partitioning can be invalid before it gets to the
12096  *		device.  Probably the right fix is to move partitioning out
12097  *		of the driver entirely.
12098  */
12099 
12100 static void
12101 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12102 {
12103 	daddr_t	nblocks;	/* #blocks in the given partition */
12104 	daddr_t	blocknum;	/* Block number specified by the buf */
12105 	size_t	requested_nblocks;
12106 	size_t	available_nblocks;
12107 	int	partition;
12108 	diskaddr_t	partition_offset;
12109 	struct sd_xbuf *xp;
12110 
12111 
12112 	ASSERT(un != NULL);
12113 	ASSERT(bp != NULL);
12114 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12115 
12116 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12117 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12118 
12119 	xp = SD_GET_XBUF(bp);
12120 	ASSERT(xp != NULL);
12121 
12122 	/*
12123 	 * If the geometry is not indicated as valid, attempt to access
12124 	 * the unit & verify the geometry/label. This can be the case for
12125 	 * removable-media devices, of if the device was opened in
12126 	 * NDELAY/NONBLOCK mode.
12127 	 */
12128 	if ((un->un_f_geometry_is_valid != TRUE) &&
12129 	    (sd_ready_and_valid(un) != SD_READY_VALID)) {
12130 		/*
12131 		 * For removable devices it is possible to start an I/O
12132 		 * without a media by opening the device in nodelay mode.
12133 		 * Also for writable CDs there can be many scenarios where
12134 		 * there is no geometry yet but volume manager is trying to
12135 		 * issue a read() just because it can see TOC on the CD. So
12136 		 * do not print a message for removables.
12137 		 */
12138 		if (!un->un_f_has_removable_media) {
12139 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12140 			    "i/o to invalid geometry\n");
12141 		}
12142 		bioerror(bp, EIO);
12143 		bp->b_resid = bp->b_bcount;
12144 		SD_BEGIN_IODONE(index, un, bp);
12145 		return;
12146 	}
12147 
12148 	partition = SDPART(bp->b_edev);
12149 
12150 	/* #blocks in partition */
12151 	nblocks = un->un_map[partition].dkl_nblk;    /* #blocks in partition */
12152 
12153 	/* Use of a local variable potentially improves performance slightly */
12154 	partition_offset = un->un_offset[partition];
12155 
12156 	/*
12157 	 * blocknum is the starting block number of the request. At this
12158 	 * point it is still relative to the start of the minor device.
12159 	 */
12160 	blocknum = xp->xb_blkno;
12161 
12162 	/*
12163 	 * Legacy: If the starting block number is one past the last block
12164 	 * in the partition, do not set B_ERROR in the buf.
12165 	 */
12166 	if (blocknum == nblocks)  {
12167 		goto error_exit;
12168 	}
12169 
12170 	/*
12171 	 * Confirm that the first block of the request lies within the
12172 	 * partition limits. Also the requested number of bytes must be
12173 	 * a multiple of the system block size.
12174 	 */
12175 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12176 	    ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) {
12177 		bp->b_flags |= B_ERROR;
12178 		goto error_exit;
12179 	}
12180 
12181 	/*
12182 	 * If the requsted # blocks exceeds the available # blocks, that
12183 	 * is an overrun of the partition.
12184 	 */
12185 	requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount);
12186 	available_nblocks = (size_t)(nblocks - blocknum);
12187 	ASSERT(nblocks >= blocknum);
12188 
12189 	if (requested_nblocks > available_nblocks) {
12190 		/*
12191 		 * Allocate an "overrun" buf to allow the request to proceed
12192 		 * for the amount of space available in the partition. The
12193 		 * amount not transferred will be added into the b_resid
12194 		 * when the operation is complete. The overrun buf
12195 		 * replaces the original buf here, and the original buf
12196 		 * is saved inside the overrun buf, for later use.
12197 		 */
12198 		size_t resid = SD_SYSBLOCKS2BYTES(un,
12199 		    (offset_t)(requested_nblocks - available_nblocks));
12200 		size_t count = bp->b_bcount - resid;
12201 		/*
12202 		 * Note: count is an unsigned entity thus it'll NEVER
12203 		 * be less than 0 so ASSERT the original values are
12204 		 * correct.
12205 		 */
12206 		ASSERT(bp->b_bcount >= resid);
12207 
12208 		bp = sd_bioclone_alloc(bp, count, blocknum,
12209 			(int (*)(struct buf *)) sd_mapblockaddr_iodone);
12210 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12211 		ASSERT(xp != NULL);
12212 	}
12213 
12214 	/* At this point there should be no residual for this buf. */
12215 	ASSERT(bp->b_resid == 0);
12216 
12217 	/* Convert the block number to an absolute address. */
12218 	xp->xb_blkno += partition_offset;
12219 
12220 	SD_NEXT_IOSTART(index, un, bp);
12221 
12222 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12223 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12224 
12225 	return;
12226 
12227 error_exit:
12228 	bp->b_resid = bp->b_bcount;
12229 	SD_BEGIN_IODONE(index, un, bp);
12230 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12231 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12232 }
12233 
12234 
12235 /*
12236  *    Function: sd_mapblockaddr_iodone
12237  *
12238  * Description: Completion-side processing for partition management.
12239  *
12240  *     Context: May be called under interrupt context
12241  */
12242 
12243 static void
12244 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12245 {
12246 	/* int	partition; */	/* Not used, see below. */
12247 	ASSERT(un != NULL);
12248 	ASSERT(bp != NULL);
12249 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12250 
12251 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12252 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12253 
12254 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12255 		/*
12256 		 * We have an "overrun" buf to deal with...
12257 		 */
12258 		struct sd_xbuf	*xp;
12259 		struct buf	*obp;	/* ptr to the original buf */
12260 
12261 		xp = SD_GET_XBUF(bp);
12262 		ASSERT(xp != NULL);
12263 
12264 		/* Retrieve the pointer to the original buf */
12265 		obp = (struct buf *)xp->xb_private;
12266 		ASSERT(obp != NULL);
12267 
12268 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12269 		bioerror(obp, bp->b_error);
12270 
12271 		sd_bioclone_free(bp);
12272 
12273 		/*
12274 		 * Get back the original buf.
12275 		 * Note that since the restoration of xb_blkno below
12276 		 * was removed, the sd_xbuf is not needed.
12277 		 */
12278 		bp = obp;
12279 		/*
12280 		 * xp = SD_GET_XBUF(bp);
12281 		 * ASSERT(xp != NULL);
12282 		 */
12283 	}
12284 
12285 	/*
12286 	 * Convert sd->xb_blkno back to a minor-device relative value.
12287 	 * Note: this has been commented out, as it is not needed in the
12288 	 * current implementation of the driver (ie, since this function
12289 	 * is at the top of the layering chains, so the info will be
12290 	 * discarded) and it is in the "hot" IO path.
12291 	 *
12292 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12293 	 * xp->xb_blkno -= un->un_offset[partition];
12294 	 */
12295 
12296 	SD_NEXT_IODONE(index, un, bp);
12297 
12298 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12299 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12300 }
12301 
12302 
12303 /*
12304  *    Function: sd_mapblocksize_iostart
12305  *
12306  * Description: Convert between system block size (un->un_sys_blocksize)
12307  *		and target block size (un->un_tgt_blocksize).
12308  *
12309  *     Context: Can sleep to allocate resources.
12310  *
12311  * Assumptions: A higher layer has already performed any partition validation,
12312  *		and converted the xp->xb_blkno to an absolute value relative
12313  *		to the start of the device.
12314  *
12315  *		It is also assumed that the higher layer has implemented
12316  *		an "overrun" mechanism for the case where the request would
12317  *		read/write beyond the end of a partition.  In this case we
12318  *		assume (and ASSERT) that bp->b_resid == 0.
12319  *
12320  *		Note: The implementation for this routine assumes the target
12321  *		block size remains constant between allocation and transport.
12322  */
12323 
12324 static void
12325 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12326 {
12327 	struct sd_mapblocksize_info	*bsp;
12328 	struct sd_xbuf			*xp;
12329 	offset_t first_byte;
12330 	daddr_t	start_block, end_block;
12331 	daddr_t	request_bytes;
12332 	ushort_t is_aligned = FALSE;
12333 
12334 	ASSERT(un != NULL);
12335 	ASSERT(bp != NULL);
12336 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12337 	ASSERT(bp->b_resid == 0);
12338 
12339 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12340 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12341 
12342 	/*
12343 	 * For a non-writable CD, a write request is an error
12344 	 */
12345 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12346 	    (un->un_f_mmc_writable_media == FALSE)) {
12347 		bioerror(bp, EIO);
12348 		bp->b_resid = bp->b_bcount;
12349 		SD_BEGIN_IODONE(index, un, bp);
12350 		return;
12351 	}
12352 
12353 	/*
12354 	 * We do not need a shadow buf if the device is using
12355 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12356 	 * In this case there is no layer-private data block allocated.
12357 	 */
12358 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12359 	    (bp->b_bcount == 0)) {
12360 		goto done;
12361 	}
12362 
12363 #if defined(__i386) || defined(__amd64)
12364 	/* We do not support non-block-aligned transfers for ROD devices */
12365 	ASSERT(!ISROD(un));
12366 #endif
12367 
12368 	xp = SD_GET_XBUF(bp);
12369 	ASSERT(xp != NULL);
12370 
12371 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12372 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12373 	    un->un_tgt_blocksize, un->un_sys_blocksize);
12374 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12375 	    "request start block:0x%x\n", xp->xb_blkno);
12376 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12377 	    "request len:0x%x\n", bp->b_bcount);
12378 
12379 	/*
12380 	 * Allocate the layer-private data area for the mapblocksize layer.
12381 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12382 	 * struct to store the pointer to their layer-private data block, but
12383 	 * each layer also has the responsibility of restoring the prior
12384 	 * contents of xb_private before returning the buf/xbuf to the
12385 	 * higher layer that sent it.
12386 	 *
12387 	 * Here we save the prior contents of xp->xb_private into the
12388 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12389 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12390 	 * the layer-private area and returning the buf/xbuf to the layer
12391 	 * that sent it.
12392 	 *
12393 	 * Note that here we use kmem_zalloc for the allocation as there are
12394 	 * parts of the mapblocksize code that expect certain fields to be
12395 	 * zero unless explicitly set to a required value.
12396 	 */
12397 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12398 	bsp->mbs_oprivate = xp->xb_private;
12399 	xp->xb_private = bsp;
12400 
12401 	/*
12402 	 * This treats the data on the disk (target) as an array of bytes.
12403 	 * first_byte is the byte offset, from the beginning of the device,
12404 	 * to the location of the request. This is converted from a
12405 	 * un->un_sys_blocksize block address to a byte offset, and then back
12406 	 * to a block address based upon a un->un_tgt_blocksize block size.
12407 	 *
12408 	 * xp->xb_blkno should be absolute upon entry into this function,
12409 	 * but, but it is based upon partitions that use the "system"
12410 	 * block size. It must be adjusted to reflect the block size of
12411 	 * the target.
12412 	 *
12413 	 * Note that end_block is actually the block that follows the last
12414 	 * block of the request, but that's what is needed for the computation.
12415 	 */
12416 	first_byte  = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12417 	start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12418 	end_block   = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) /
12419 	    un->un_tgt_blocksize;
12420 
12421 	/* request_bytes is rounded up to a multiple of the target block size */
12422 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12423 
12424 	/*
12425 	 * See if the starting address of the request and the request
12426 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12427 	 * then we do not need to allocate a shadow buf to handle the request.
12428 	 */
12429 	if (((first_byte   % un->un_tgt_blocksize) == 0) &&
12430 	    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12431 		is_aligned = TRUE;
12432 	}
12433 
12434 	if ((bp->b_flags & B_READ) == 0) {
12435 		/*
12436 		 * Lock the range for a write operation. An aligned request is
12437 		 * considered a simple write; otherwise the request must be a
12438 		 * read-modify-write.
12439 		 */
12440 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12441 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12442 	}
12443 
12444 	/*
12445 	 * Alloc a shadow buf if the request is not aligned. Also, this is
12446 	 * where the READ command is generated for a read-modify-write. (The
12447 	 * write phase is deferred until after the read completes.)
12448 	 */
12449 	if (is_aligned == FALSE) {
12450 
12451 		struct sd_mapblocksize_info	*shadow_bsp;
12452 		struct sd_xbuf	*shadow_xp;
12453 		struct buf	*shadow_bp;
12454 
12455 		/*
12456 		 * Allocate the shadow buf and it associated xbuf. Note that
12457 		 * after this call the xb_blkno value in both the original
12458 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12459 		 * same: absolute relative to the start of the device, and
12460 		 * adjusted for the target block size. The b_blkno in the
12461 		 * shadow buf will also be set to this value. We should never
12462 		 * change b_blkno in the original bp however.
12463 		 *
12464 		 * Note also that the shadow buf will always need to be a
12465 		 * READ command, regardless of whether the incoming command
12466 		 * is a READ or a WRITE.
12467 		 */
12468 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12469 		    xp->xb_blkno,
12470 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
12471 
12472 		shadow_xp = SD_GET_XBUF(shadow_bp);
12473 
12474 		/*
12475 		 * Allocate the layer-private data for the shadow buf.
12476 		 * (No need to preserve xb_private in the shadow xbuf.)
12477 		 */
12478 		shadow_xp->xb_private = shadow_bsp =
12479 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12480 
12481 		/*
12482 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
12483 		 * to figure out where the start of the user data is (based upon
12484 		 * the system block size) in the data returned by the READ
12485 		 * command (which will be based upon the target blocksize). Note
12486 		 * that this is only really used if the request is unaligned.
12487 		 */
12488 		bsp->mbs_copy_offset = (ssize_t)(first_byte -
12489 		    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
12490 		ASSERT((bsp->mbs_copy_offset >= 0) &&
12491 		    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
12492 
12493 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
12494 
12495 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
12496 
12497 		/* Transfer the wmap (if any) to the shadow buf */
12498 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
12499 		bsp->mbs_wmp = NULL;
12500 
12501 		/*
12502 		 * The shadow buf goes on from here in place of the
12503 		 * original buf.
12504 		 */
12505 		shadow_bsp->mbs_orig_bp = bp;
12506 		bp = shadow_bp;
12507 	}
12508 
12509 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12510 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
12511 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12512 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
12513 	    request_bytes);
12514 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12515 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
12516 
12517 done:
12518 	SD_NEXT_IOSTART(index, un, bp);
12519 
12520 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12521 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
12522 }
12523 
12524 
12525 /*
12526  *    Function: sd_mapblocksize_iodone
12527  *
12528  * Description: Completion side processing for block-size mapping.
12529  *
12530  *     Context: May be called under interrupt context
12531  */
12532 
12533 static void
12534 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
12535 {
12536 	struct sd_mapblocksize_info	*bsp;
12537 	struct sd_xbuf	*xp;
12538 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
12539 	struct buf	*orig_bp;	/* ptr to the original buf */
12540 	offset_t	shadow_end;
12541 	offset_t	request_end;
12542 	offset_t	shadow_start;
12543 	ssize_t		copy_offset;
12544 	size_t		copy_length;
12545 	size_t		shortfall;
12546 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
12547 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
12548 
12549 	ASSERT(un != NULL);
12550 	ASSERT(bp != NULL);
12551 
12552 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12553 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
12554 
12555 	/*
12556 	 * There is no shadow buf or layer-private data if the target is
12557 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
12558 	 */
12559 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12560 	    (bp->b_bcount == 0)) {
12561 		goto exit;
12562 	}
12563 
12564 	xp = SD_GET_XBUF(bp);
12565 	ASSERT(xp != NULL);
12566 
12567 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
12568 	bsp = xp->xb_private;
12569 
12570 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
12571 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
12572 
12573 	if (is_write) {
12574 		/*
12575 		 * For a WRITE request we must free up the block range that
12576 		 * we have locked up.  This holds regardless of whether this is
12577 		 * an aligned write request or a read-modify-write request.
12578 		 */
12579 		sd_range_unlock(un, bsp->mbs_wmp);
12580 		bsp->mbs_wmp = NULL;
12581 	}
12582 
12583 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
12584 		/*
12585 		 * An aligned read or write command will have no shadow buf;
12586 		 * there is not much else to do with it.
12587 		 */
12588 		goto done;
12589 	}
12590 
12591 	orig_bp = bsp->mbs_orig_bp;
12592 	ASSERT(orig_bp != NULL);
12593 	orig_xp = SD_GET_XBUF(orig_bp);
12594 	ASSERT(orig_xp != NULL);
12595 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12596 
12597 	if (!is_write && has_wmap) {
12598 		/*
12599 		 * A READ with a wmap means this is the READ phase of a
12600 		 * read-modify-write. If an error occurred on the READ then
12601 		 * we do not proceed with the WRITE phase or copy any data.
12602 		 * Just release the write maps and return with an error.
12603 		 */
12604 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
12605 			orig_bp->b_resid = orig_bp->b_bcount;
12606 			bioerror(orig_bp, bp->b_error);
12607 			sd_range_unlock(un, bsp->mbs_wmp);
12608 			goto freebuf_done;
12609 		}
12610 	}
12611 
12612 	/*
12613 	 * Here is where we set up to copy the data from the shadow buf
12614 	 * into the space associated with the original buf.
12615 	 *
12616 	 * To deal with the conversion between block sizes, these
12617 	 * computations treat the data as an array of bytes, with the
12618 	 * first byte (byte 0) corresponding to the first byte in the
12619 	 * first block on the disk.
12620 	 */
12621 
12622 	/*
12623 	 * shadow_start and shadow_len indicate the location and size of
12624 	 * the data returned with the shadow IO request.
12625 	 */
12626 	shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12627 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
12628 
12629 	/*
12630 	 * copy_offset gives the offset (in bytes) from the start of the first
12631 	 * block of the READ request to the beginning of the data.  We retrieve
12632 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
12633 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
12634 	 * data to be copied (in bytes).
12635 	 */
12636 	copy_offset  = bsp->mbs_copy_offset;
12637 	ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize));
12638 	copy_length  = orig_bp->b_bcount;
12639 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
12640 
12641 	/*
12642 	 * Set up the resid and error fields of orig_bp as appropriate.
12643 	 */
12644 	if (shadow_end >= request_end) {
12645 		/* We got all the requested data; set resid to zero */
12646 		orig_bp->b_resid = 0;
12647 	} else {
12648 		/*
12649 		 * We failed to get enough data to fully satisfy the original
12650 		 * request. Just copy back whatever data we got and set
12651 		 * up the residual and error code as required.
12652 		 *
12653 		 * 'shortfall' is the amount by which the data received with the
12654 		 * shadow buf has "fallen short" of the requested amount.
12655 		 */
12656 		shortfall = (size_t)(request_end - shadow_end);
12657 
12658 		if (shortfall > orig_bp->b_bcount) {
12659 			/*
12660 			 * We did not get enough data to even partially
12661 			 * fulfill the original request.  The residual is
12662 			 * equal to the amount requested.
12663 			 */
12664 			orig_bp->b_resid = orig_bp->b_bcount;
12665 		} else {
12666 			/*
12667 			 * We did not get all the data that we requested
12668 			 * from the device, but we will try to return what
12669 			 * portion we did get.
12670 			 */
12671 			orig_bp->b_resid = shortfall;
12672 		}
12673 		ASSERT(copy_length >= orig_bp->b_resid);
12674 		copy_length  -= orig_bp->b_resid;
12675 	}
12676 
12677 	/* Propagate the error code from the shadow buf to the original buf */
12678 	bioerror(orig_bp, bp->b_error);
12679 
12680 	if (is_write) {
12681 		goto freebuf_done;	/* No data copying for a WRITE */
12682 	}
12683 
12684 	if (has_wmap) {
12685 		/*
12686 		 * This is a READ command from the READ phase of a
12687 		 * read-modify-write request. We have to copy the data given
12688 		 * by the user OVER the data returned by the READ command,
12689 		 * then convert the command from a READ to a WRITE and send
12690 		 * it back to the target.
12691 		 */
12692 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
12693 		    copy_length);
12694 
12695 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
12696 
12697 		/*
12698 		 * Dispatch the WRITE command to the taskq thread, which
12699 		 * will in turn send the command to the target. When the
12700 		 * WRITE command completes, we (sd_mapblocksize_iodone())
12701 		 * will get called again as part of the iodone chain
12702 		 * processing for it. Note that we will still be dealing
12703 		 * with the shadow buf at that point.
12704 		 */
12705 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
12706 		    KM_NOSLEEP) != 0) {
12707 			/*
12708 			 * Dispatch was successful so we are done. Return
12709 			 * without going any higher up the iodone chain. Do
12710 			 * not free up any layer-private data until after the
12711 			 * WRITE completes.
12712 			 */
12713 			return;
12714 		}
12715 
12716 		/*
12717 		 * Dispatch of the WRITE command failed; set up the error
12718 		 * condition and send this IO back up the iodone chain.
12719 		 */
12720 		bioerror(orig_bp, EIO);
12721 		orig_bp->b_resid = orig_bp->b_bcount;
12722 
12723 	} else {
12724 		/*
12725 		 * This is a regular READ request (ie, not a RMW). Copy the
12726 		 * data from the shadow buf into the original buf. The
12727 		 * copy_offset compensates for any "misalignment" between the
12728 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
12729 		 * original buf (with its un->un_sys_blocksize blocks).
12730 		 */
12731 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
12732 		    copy_length);
12733 	}
12734 
12735 freebuf_done:
12736 
12737 	/*
12738 	 * At this point we still have both the shadow buf AND the original
12739 	 * buf to deal with, as well as the layer-private data area in each.
12740 	 * Local variables are as follows:
12741 	 *
12742 	 * bp -- points to shadow buf
12743 	 * xp -- points to xbuf of shadow buf
12744 	 * bsp -- points to layer-private data area of shadow buf
12745 	 * orig_bp -- points to original buf
12746 	 *
12747 	 * First free the shadow buf and its associated xbuf, then free the
12748 	 * layer-private data area from the shadow buf. There is no need to
12749 	 * restore xb_private in the shadow xbuf.
12750 	 */
12751 	sd_shadow_buf_free(bp);
12752 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
12753 
12754 	/*
12755 	 * Now update the local variables to point to the original buf, xbuf,
12756 	 * and layer-private area.
12757 	 */
12758 	bp = orig_bp;
12759 	xp = SD_GET_XBUF(bp);
12760 	ASSERT(xp != NULL);
12761 	ASSERT(xp == orig_xp);
12762 	bsp = xp->xb_private;
12763 	ASSERT(bsp != NULL);
12764 
12765 done:
12766 	/*
12767 	 * Restore xb_private to whatever it was set to by the next higher
12768 	 * layer in the chain, then free the layer-private data area.
12769 	 */
12770 	xp->xb_private = bsp->mbs_oprivate;
12771 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
12772 
12773 exit:
12774 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
12775 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
12776 
12777 	SD_NEXT_IODONE(index, un, bp);
12778 }
12779 
12780 
12781 /*
12782  *    Function: sd_checksum_iostart
12783  *
12784  * Description: A stub function for a layer that's currently not used.
12785  *		For now just a placeholder.
12786  *
12787  *     Context: Kernel thread context
12788  */
12789 
12790 static void
12791 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
12792 {
12793 	ASSERT(un != NULL);
12794 	ASSERT(bp != NULL);
12795 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12796 	SD_NEXT_IOSTART(index, un, bp);
12797 }
12798 
12799 
12800 /*
12801  *    Function: sd_checksum_iodone
12802  *
12803  * Description: A stub function for a layer that's currently not used.
12804  *		For now just a placeholder.
12805  *
12806  *     Context: May be called under interrupt context
12807  */
12808 
12809 static void
12810 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
12811 {
12812 	ASSERT(un != NULL);
12813 	ASSERT(bp != NULL);
12814 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12815 	SD_NEXT_IODONE(index, un, bp);
12816 }
12817 
12818 
12819 /*
12820  *    Function: sd_checksum_uscsi_iostart
12821  *
12822  * Description: A stub function for a layer that's currently not used.
12823  *		For now just a placeholder.
12824  *
12825  *     Context: Kernel thread context
12826  */
12827 
12828 static void
12829 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
12830 {
12831 	ASSERT(un != NULL);
12832 	ASSERT(bp != NULL);
12833 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12834 	SD_NEXT_IOSTART(index, un, bp);
12835 }
12836 
12837 
12838 /*
12839  *    Function: sd_checksum_uscsi_iodone
12840  *
12841  * Description: A stub function for a layer that's currently not used.
12842  *		For now just a placeholder.
12843  *
12844  *     Context: May be called under interrupt context
12845  */
12846 
12847 static void
12848 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12849 {
12850 	ASSERT(un != NULL);
12851 	ASSERT(bp != NULL);
12852 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12853 	SD_NEXT_IODONE(index, un, bp);
12854 }
12855 
12856 
12857 /*
12858  *    Function: sd_pm_iostart
12859  *
12860  * Description: iostart-side routine for Power mangement.
12861  *
12862  *     Context: Kernel thread context
12863  */
12864 
12865 static void
12866 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
12867 {
12868 	ASSERT(un != NULL);
12869 	ASSERT(bp != NULL);
12870 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12871 	ASSERT(!mutex_owned(&un->un_pm_mutex));
12872 
12873 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
12874 
12875 	if (sd_pm_entry(un) != DDI_SUCCESS) {
12876 		/*
12877 		 * Set up to return the failed buf back up the 'iodone'
12878 		 * side of the calling chain.
12879 		 */
12880 		bioerror(bp, EIO);
12881 		bp->b_resid = bp->b_bcount;
12882 
12883 		SD_BEGIN_IODONE(index, un, bp);
12884 
12885 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
12886 		return;
12887 	}
12888 
12889 	SD_NEXT_IOSTART(index, un, bp);
12890 
12891 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
12892 }
12893 
12894 
12895 /*
12896  *    Function: sd_pm_iodone
12897  *
12898  * Description: iodone-side routine for power mangement.
12899  *
12900  *     Context: may be called from interrupt context
12901  */
12902 
12903 static void
12904 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
12905 {
12906 	ASSERT(un != NULL);
12907 	ASSERT(bp != NULL);
12908 	ASSERT(!mutex_owned(&un->un_pm_mutex));
12909 
12910 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
12911 
12912 	/*
12913 	 * After attach the following flag is only read, so don't
12914 	 * take the penalty of acquiring a mutex for it.
12915 	 */
12916 	if (un->un_f_pm_is_enabled == TRUE) {
12917 		sd_pm_exit(un);
12918 	}
12919 
12920 	SD_NEXT_IODONE(index, un, bp);
12921 
12922 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
12923 }
12924 
12925 
12926 /*
12927  *    Function: sd_core_iostart
12928  *
12929  * Description: Primary driver function for enqueuing buf(9S) structs from
12930  *		the system and initiating IO to the target device
12931  *
12932  *     Context: Kernel thread context. Can sleep.
12933  *
12934  * Assumptions:  - The given xp->xb_blkno is absolute
12935  *		   (ie, relative to the start of the device).
12936  *		 - The IO is to be done using the native blocksize of
12937  *		   the device, as specified in un->un_tgt_blocksize.
12938  */
12939 /* ARGSUSED */
12940 static void
12941 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
12942 {
12943 	struct sd_xbuf *xp;
12944 
12945 	ASSERT(un != NULL);
12946 	ASSERT(bp != NULL);
12947 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12948 	ASSERT(bp->b_resid == 0);
12949 
12950 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
12951 
12952 	xp = SD_GET_XBUF(bp);
12953 	ASSERT(xp != NULL);
12954 
12955 	mutex_enter(SD_MUTEX(un));
12956 
12957 	/*
12958 	 * If we are currently in the failfast state, fail any new IO
12959 	 * that has B_FAILFAST set, then return.
12960 	 */
12961 	if ((bp->b_flags & B_FAILFAST) &&
12962 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
12963 		mutex_exit(SD_MUTEX(un));
12964 		bioerror(bp, EIO);
12965 		bp->b_resid = bp->b_bcount;
12966 		SD_BEGIN_IODONE(index, un, bp);
12967 		return;
12968 	}
12969 
12970 	if (SD_IS_DIRECT_PRIORITY(xp)) {
12971 		/*
12972 		 * Priority command -- transport it immediately.
12973 		 *
12974 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
12975 		 * because all direct priority commands should be associated
12976 		 * with error recovery actions which we don't want to retry.
12977 		 */
12978 		sd_start_cmds(un, bp);
12979 	} else {
12980 		/*
12981 		 * Normal command -- add it to the wait queue, then start
12982 		 * transporting commands from the wait queue.
12983 		 */
12984 		sd_add_buf_to_waitq(un, bp);
12985 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
12986 		sd_start_cmds(un, NULL);
12987 	}
12988 
12989 	mutex_exit(SD_MUTEX(un));
12990 
12991 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
12992 }
12993 
12994 
12995 /*
12996  *    Function: sd_init_cdb_limits
12997  *
12998  * Description: This is to handle scsi_pkt initialization differences
12999  *		between the driver platforms.
13000  *
13001  *		Legacy behaviors:
13002  *
13003  *		If the block number or the sector count exceeds the
13004  *		capabilities of a Group 0 command, shift over to a
13005  *		Group 1 command. We don't blindly use Group 1
13006  *		commands because a) some drives (CDC Wren IVs) get a
13007  *		bit confused, and b) there is probably a fair amount
13008  *		of speed difference for a target to receive and decode
13009  *		a 10 byte command instead of a 6 byte command.
13010  *
13011  *		The xfer time difference of 6 vs 10 byte CDBs is
13012  *		still significant so this code is still worthwhile.
13013  *		10 byte CDBs are very inefficient with the fas HBA driver
13014  *		and older disks. Each CDB byte took 1 usec with some
13015  *		popular disks.
13016  *
13017  *     Context: Must be called at attach time
13018  */
13019 
13020 static void
13021 sd_init_cdb_limits(struct sd_lun *un)
13022 {
13023 	int hba_cdb_limit;
13024 
13025 	/*
13026 	 * Use CDB_GROUP1 commands for most devices except for
13027 	 * parallel SCSI fixed drives in which case we get better
13028 	 * performance using CDB_GROUP0 commands (where applicable).
13029 	 */
13030 	un->un_mincdb = SD_CDB_GROUP1;
13031 #if !defined(__fibre)
13032 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13033 	    !un->un_f_has_removable_media) {
13034 		un->un_mincdb = SD_CDB_GROUP0;
13035 	}
13036 #endif
13037 
13038 	/*
13039 	 * Try to read the max-cdb-length supported by HBA.
13040 	 */
13041 	un->un_max_hba_cdb = scsi_ifgetcap(SD_ADDRESS(un), "max-cdb-length", 1);
13042 	if (0 >= un->un_max_hba_cdb) {
13043 		un->un_max_hba_cdb = CDB_GROUP4;
13044 		hba_cdb_limit = SD_CDB_GROUP4;
13045 	} else if (0 < un->un_max_hba_cdb &&
13046 	    un->un_max_hba_cdb < CDB_GROUP1) {
13047 		hba_cdb_limit = SD_CDB_GROUP0;
13048 	} else if (CDB_GROUP1 <= un->un_max_hba_cdb &&
13049 	    un->un_max_hba_cdb < CDB_GROUP5) {
13050 		hba_cdb_limit = SD_CDB_GROUP1;
13051 	} else if (CDB_GROUP5 <= un->un_max_hba_cdb &&
13052 	    un->un_max_hba_cdb < CDB_GROUP4) {
13053 		hba_cdb_limit = SD_CDB_GROUP5;
13054 	} else {
13055 		hba_cdb_limit = SD_CDB_GROUP4;
13056 	}
13057 
13058 	/*
13059 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13060 	 * commands for fixed disks unless we are building for a 32 bit
13061 	 * kernel.
13062 	 */
13063 #ifdef _LP64
13064 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13065 	    min(hba_cdb_limit, SD_CDB_GROUP4);
13066 #else
13067 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13068 	    min(hba_cdb_limit, SD_CDB_GROUP1);
13069 #endif
13070 
13071 	/*
13072 	 * x86 systems require the PKT_DMA_PARTIAL flag
13073 	 */
13074 #if defined(__x86)
13075 	un->un_pkt_flags = PKT_DMA_PARTIAL;
13076 #else
13077 	un->un_pkt_flags = 0;
13078 #endif
13079 
13080 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13081 	    ? sizeof (struct scsi_arq_status) : 1);
13082 	un->un_cmd_timeout = (ushort_t)sd_io_time;
13083 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13084 }
13085 
13086 
13087 /*
13088  *    Function: sd_initpkt_for_buf
13089  *
13090  * Description: Allocate and initialize for transport a scsi_pkt struct,
13091  *		based upon the info specified in the given buf struct.
13092  *
13093  *		Assumes the xb_blkno in the request is absolute (ie,
13094  *		relative to the start of the device (NOT partition!).
13095  *		Also assumes that the request is using the native block
13096  *		size of the device (as returned by the READ CAPACITY
13097  *		command).
13098  *
13099  * Return Code: SD_PKT_ALLOC_SUCCESS
13100  *		SD_PKT_ALLOC_FAILURE
13101  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13102  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13103  *
13104  *     Context: Kernel thread and may be called from software interrupt context
13105  *		as part of a sdrunout callback. This function may not block or
13106  *		call routines that block
13107  */
13108 
13109 static int
13110 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13111 {
13112 	struct sd_xbuf	*xp;
13113 	struct scsi_pkt *pktp = NULL;
13114 	struct sd_lun	*un;
13115 	size_t		blockcount;
13116 	daddr_t		startblock;
13117 	int		rval;
13118 	int		cmd_flags;
13119 
13120 	ASSERT(bp != NULL);
13121 	ASSERT(pktpp != NULL);
13122 	xp = SD_GET_XBUF(bp);
13123 	ASSERT(xp != NULL);
13124 	un = SD_GET_UN(bp);
13125 	ASSERT(un != NULL);
13126 	ASSERT(mutex_owned(SD_MUTEX(un)));
13127 	ASSERT(bp->b_resid == 0);
13128 
13129 	SD_TRACE(SD_LOG_IO_CORE, un,
13130 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13131 
13132 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13133 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13134 		/*
13135 		 * Already have a scsi_pkt -- just need DMA resources.
13136 		 * We must recompute the CDB in case the mapping returns
13137 		 * a nonzero pkt_resid.
13138 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13139 		 * that is being retried, the unmap/remap of the DMA resouces
13140 		 * will result in the entire transfer starting over again
13141 		 * from the very first block.
13142 		 */
13143 		ASSERT(xp->xb_pktp != NULL);
13144 		pktp = xp->xb_pktp;
13145 	} else {
13146 		pktp = NULL;
13147 	}
13148 #endif /* __i386 || __amd64 */
13149 
13150 	startblock = xp->xb_blkno;	/* Absolute block num. */
13151 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13152 
13153 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13154 
13155 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13156 
13157 #else
13158 
13159 	cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags;
13160 
13161 #endif
13162 
13163 	/*
13164 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13165 	 * call scsi_init_pkt, and build the CDB.
13166 	 */
13167 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13168 	    cmd_flags, sdrunout, (caddr_t)un,
13169 	    startblock, blockcount);
13170 
13171 	if (rval == 0) {
13172 		/*
13173 		 * Success.
13174 		 *
13175 		 * If partial DMA is being used and required for this transfer.
13176 		 * set it up here.
13177 		 */
13178 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13179 		    (pktp->pkt_resid != 0)) {
13180 
13181 			/*
13182 			 * Save the CDB length and pkt_resid for the
13183 			 * next xfer
13184 			 */
13185 			xp->xb_dma_resid = pktp->pkt_resid;
13186 
13187 			/* rezero resid */
13188 			pktp->pkt_resid = 0;
13189 
13190 		} else {
13191 			xp->xb_dma_resid = 0;
13192 		}
13193 
13194 		pktp->pkt_flags = un->un_tagflags;
13195 		pktp->pkt_time  = un->un_cmd_timeout;
13196 		pktp->pkt_comp  = sdintr;
13197 
13198 		pktp->pkt_private = bp;
13199 		*pktpp = pktp;
13200 
13201 		SD_TRACE(SD_LOG_IO_CORE, un,
13202 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13203 
13204 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13205 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13206 #endif
13207 
13208 		return (SD_PKT_ALLOC_SUCCESS);
13209 
13210 	}
13211 
13212 	/*
13213 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13214 	 * from sd_setup_rw_pkt.
13215 	 */
13216 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13217 
13218 	if (rval == SD_PKT_ALLOC_FAILURE) {
13219 		*pktpp = NULL;
13220 		/*
13221 		 * Set the driver state to RWAIT to indicate the driver
13222 		 * is waiting on resource allocations. The driver will not
13223 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13224 		 */
13225 		New_state(un, SD_STATE_RWAIT);
13226 
13227 		SD_ERROR(SD_LOG_IO_CORE, un,
13228 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13229 
13230 		if ((bp->b_flags & B_ERROR) != 0) {
13231 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13232 		}
13233 		return (SD_PKT_ALLOC_FAILURE);
13234 	} else {
13235 		/*
13236 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13237 		 *
13238 		 * This should never happen.  Maybe someone messed with the
13239 		 * kernel's minphys?
13240 		 */
13241 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13242 		    "Request rejected: too large for CDB: "
13243 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13244 		SD_ERROR(SD_LOG_IO_CORE, un,
13245 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13246 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13247 
13248 	}
13249 }
13250 
13251 
13252 /*
13253  *    Function: sd_destroypkt_for_buf
13254  *
13255  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13256  *
13257  *     Context: Kernel thread or interrupt context
13258  */
13259 
13260 static void
13261 sd_destroypkt_for_buf(struct buf *bp)
13262 {
13263 	ASSERT(bp != NULL);
13264 	ASSERT(SD_GET_UN(bp) != NULL);
13265 
13266 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13267 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13268 
13269 	ASSERT(SD_GET_PKTP(bp) != NULL);
13270 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13271 
13272 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13273 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13274 }
13275 
13276 /*
13277  *    Function: sd_setup_rw_pkt
13278  *
13279  * Description: Determines appropriate CDB group for the requested LBA
13280  *		and transfer length, calls scsi_init_pkt, and builds
13281  *		the CDB.  Do not use for partial DMA transfers except
13282  *		for the initial transfer since the CDB size must
13283  *		remain constant.
13284  *
13285  *     Context: Kernel thread and may be called from software interrupt
13286  *		context as part of a sdrunout callback. This function may not
13287  *		block or call routines that block
13288  */
13289 
13290 
13291 int
13292 sd_setup_rw_pkt(struct sd_lun *un,
13293     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13294     int (*callback)(caddr_t), caddr_t callback_arg,
13295     diskaddr_t lba, uint32_t blockcount)
13296 {
13297 	struct scsi_pkt *return_pktp;
13298 	union scsi_cdb *cdbp;
13299 	struct sd_cdbinfo *cp = NULL;
13300 	int i;
13301 
13302 	/*
13303 	 * See which size CDB to use, based upon the request.
13304 	 */
13305 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13306 
13307 		/*
13308 		 * Check lba and block count against sd_cdbtab limits.
13309 		 * In the partial DMA case, we have to use the same size
13310 		 * CDB for all the transfers.  Check lba + blockcount
13311 		 * against the max LBA so we know that segment of the
13312 		 * transfer can use the CDB we select.
13313 		 */
13314 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13315 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13316 
13317 			/*
13318 			 * The command will fit into the CDB type
13319 			 * specified by sd_cdbtab[i].
13320 			 */
13321 			cp = sd_cdbtab + i;
13322 
13323 			/*
13324 			 * Call scsi_init_pkt so we can fill in the
13325 			 * CDB.
13326 			 */
13327 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13328 			    bp, cp->sc_grpcode, un->un_status_len, 0,
13329 			    flags, callback, callback_arg);
13330 
13331 			if (return_pktp != NULL) {
13332 
13333 				/*
13334 				 * Return new value of pkt
13335 				 */
13336 				*pktpp = return_pktp;
13337 
13338 				/*
13339 				 * To be safe, zero the CDB insuring there is
13340 				 * no leftover data from a previous command.
13341 				 */
13342 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13343 
13344 				/*
13345 				 * Handle partial DMA mapping
13346 				 */
13347 				if (return_pktp->pkt_resid != 0) {
13348 
13349 					/*
13350 					 * Not going to xfer as many blocks as
13351 					 * originally expected
13352 					 */
13353 					blockcount -=
13354 					    SD_BYTES2TGTBLOCKS(un,
13355 						return_pktp->pkt_resid);
13356 				}
13357 
13358 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13359 
13360 				/*
13361 				 * Set command byte based on the CDB
13362 				 * type we matched.
13363 				 */
13364 				cdbp->scc_cmd = cp->sc_grpmask |
13365 				    ((bp->b_flags & B_READ) ?
13366 					SCMD_READ : SCMD_WRITE);
13367 
13368 				SD_FILL_SCSI1_LUN(un, return_pktp);
13369 
13370 				/*
13371 				 * Fill in LBA and length
13372 				 */
13373 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13374 				    (cp->sc_grpcode == CDB_GROUP4) ||
13375 				    (cp->sc_grpcode == CDB_GROUP0) ||
13376 				    (cp->sc_grpcode == CDB_GROUP5));
13377 
13378 				if (cp->sc_grpcode == CDB_GROUP1) {
13379 					FORMG1ADDR(cdbp, lba);
13380 					FORMG1COUNT(cdbp, blockcount);
13381 					return (0);
13382 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13383 					FORMG4LONGADDR(cdbp, lba);
13384 					FORMG4COUNT(cdbp, blockcount);
13385 					return (0);
13386 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13387 					FORMG0ADDR(cdbp, lba);
13388 					FORMG0COUNT(cdbp, blockcount);
13389 					return (0);
13390 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13391 					FORMG5ADDR(cdbp, lba);
13392 					FORMG5COUNT(cdbp, blockcount);
13393 					return (0);
13394 				}
13395 
13396 				/*
13397 				 * It should be impossible to not match one
13398 				 * of the CDB types above, so we should never
13399 				 * reach this point.  Set the CDB command byte
13400 				 * to test-unit-ready to avoid writing
13401 				 * to somewhere we don't intend.
13402 				 */
13403 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13404 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13405 			} else {
13406 				/*
13407 				 * Couldn't get scsi_pkt
13408 				 */
13409 				return (SD_PKT_ALLOC_FAILURE);
13410 			}
13411 		}
13412 	}
13413 
13414 	/*
13415 	 * None of the available CDB types were suitable.  This really
13416 	 * should never happen:  on a 64 bit system we support
13417 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
13418 	 * and on a 32 bit system we will refuse to bind to a device
13419 	 * larger than 2TB so addresses will never be larger than 32 bits.
13420 	 */
13421 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13422 }
13423 
13424 #if defined(__i386) || defined(__amd64)
13425 /*
13426  *    Function: sd_setup_next_rw_pkt
13427  *
13428  * Description: Setup packet for partial DMA transfers, except for the
13429  * 		initial transfer.  sd_setup_rw_pkt should be used for
13430  *		the initial transfer.
13431  *
13432  *     Context: Kernel thread and may be called from interrupt context.
13433  */
13434 
13435 int
13436 sd_setup_next_rw_pkt(struct sd_lun *un,
13437     struct scsi_pkt *pktp, struct buf *bp,
13438     diskaddr_t lba, uint32_t blockcount)
13439 {
13440 	uchar_t com;
13441 	union scsi_cdb *cdbp;
13442 	uchar_t cdb_group_id;
13443 
13444 	ASSERT(pktp != NULL);
13445 	ASSERT(pktp->pkt_cdbp != NULL);
13446 
13447 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13448 	com = cdbp->scc_cmd;
13449 	cdb_group_id = CDB_GROUPID(com);
13450 
13451 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13452 	    (cdb_group_id == CDB_GROUPID_1) ||
13453 	    (cdb_group_id == CDB_GROUPID_4) ||
13454 	    (cdb_group_id == CDB_GROUPID_5));
13455 
13456 	/*
13457 	 * Move pkt to the next portion of the xfer.
13458 	 * func is NULL_FUNC so we do not have to release
13459 	 * the disk mutex here.
13460 	 */
13461 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13462 	    NULL_FUNC, NULL) == pktp) {
13463 		/* Success.  Handle partial DMA */
13464 		if (pktp->pkt_resid != 0) {
13465 			blockcount -=
13466 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13467 		}
13468 
13469 		cdbp->scc_cmd = com;
13470 		SD_FILL_SCSI1_LUN(un, pktp);
13471 		if (cdb_group_id == CDB_GROUPID_1) {
13472 			FORMG1ADDR(cdbp, lba);
13473 			FORMG1COUNT(cdbp, blockcount);
13474 			return (0);
13475 		} else if (cdb_group_id == CDB_GROUPID_4) {
13476 			FORMG4LONGADDR(cdbp, lba);
13477 			FORMG4COUNT(cdbp, blockcount);
13478 			return (0);
13479 		} else if (cdb_group_id == CDB_GROUPID_0) {
13480 			FORMG0ADDR(cdbp, lba);
13481 			FORMG0COUNT(cdbp, blockcount);
13482 			return (0);
13483 		} else if (cdb_group_id == CDB_GROUPID_5) {
13484 			FORMG5ADDR(cdbp, lba);
13485 			FORMG5COUNT(cdbp, blockcount);
13486 			return (0);
13487 		}
13488 
13489 		/* Unreachable */
13490 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13491 	}
13492 
13493 	/*
13494 	 * Error setting up next portion of cmd transfer.
13495 	 * Something is definitely very wrong and this
13496 	 * should not happen.
13497 	 */
13498 	return (SD_PKT_ALLOC_FAILURE);
13499 }
13500 #endif /* defined(__i386) || defined(__amd64) */
13501 
13502 /*
13503  *    Function: sd_initpkt_for_uscsi
13504  *
13505  * Description: Allocate and initialize for transport a scsi_pkt struct,
13506  *		based upon the info specified in the given uscsi_cmd struct.
13507  *
13508  * Return Code: SD_PKT_ALLOC_SUCCESS
13509  *		SD_PKT_ALLOC_FAILURE
13510  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13511  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13512  *
13513  *     Context: Kernel thread and may be called from software interrupt context
13514  *		as part of a sdrunout callback. This function may not block or
13515  *		call routines that block
13516  */
13517 
13518 static int
13519 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
13520 {
13521 	struct uscsi_cmd *uscmd;
13522 	struct sd_xbuf	*xp;
13523 	struct scsi_pkt	*pktp;
13524 	struct sd_lun	*un;
13525 	uint32_t	flags = 0;
13526 
13527 	ASSERT(bp != NULL);
13528 	ASSERT(pktpp != NULL);
13529 	xp = SD_GET_XBUF(bp);
13530 	ASSERT(xp != NULL);
13531 	un = SD_GET_UN(bp);
13532 	ASSERT(un != NULL);
13533 	ASSERT(mutex_owned(SD_MUTEX(un)));
13534 
13535 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13536 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13537 	ASSERT(uscmd != NULL);
13538 
13539 	SD_TRACE(SD_LOG_IO_CORE, un,
13540 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
13541 
13542 	/*
13543 	 * Allocate the scsi_pkt for the command.
13544 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
13545 	 *	 during scsi_init_pkt time and will continue to use the
13546 	 *	 same path as long as the same scsi_pkt is used without
13547 	 *	 intervening scsi_dma_free(). Since uscsi command does
13548 	 *	 not call scsi_dmafree() before retry failed command, it
13549 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
13550 	 *	 set such that scsi_vhci can use other available path for
13551 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
13552 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
13553 	 */
13554 	pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13555 	    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13556 	    sizeof (struct scsi_arq_status), 0,
13557 	    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
13558 	    sdrunout, (caddr_t)un);
13559 
13560 	if (pktp == NULL) {
13561 		*pktpp = NULL;
13562 		/*
13563 		 * Set the driver state to RWAIT to indicate the driver
13564 		 * is waiting on resource allocations. The driver will not
13565 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13566 		 */
13567 		New_state(un, SD_STATE_RWAIT);
13568 
13569 		SD_ERROR(SD_LOG_IO_CORE, un,
13570 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
13571 
13572 		if ((bp->b_flags & B_ERROR) != 0) {
13573 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13574 		}
13575 		return (SD_PKT_ALLOC_FAILURE);
13576 	}
13577 
13578 	/*
13579 	 * We do not do DMA breakup for USCSI commands, so return failure
13580 	 * here if all the needed DMA resources were not allocated.
13581 	 */
13582 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
13583 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
13584 		scsi_destroy_pkt(pktp);
13585 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
13586 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
13587 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
13588 	}
13589 
13590 	/* Init the cdb from the given uscsi struct */
13591 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
13592 	    uscmd->uscsi_cdb[0], 0, 0, 0);
13593 
13594 	SD_FILL_SCSI1_LUN(un, pktp);
13595 
13596 	/*
13597 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
13598 	 * for listing of the supported flags.
13599 	 */
13600 
13601 	if (uscmd->uscsi_flags & USCSI_SILENT) {
13602 		flags |= FLAG_SILENT;
13603 	}
13604 
13605 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
13606 		flags |= FLAG_DIAGNOSE;
13607 	}
13608 
13609 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
13610 		flags |= FLAG_ISOLATE;
13611 	}
13612 
13613 	if (un->un_f_is_fibre == FALSE) {
13614 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
13615 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
13616 		}
13617 	}
13618 
13619 	/*
13620 	 * Set the pkt flags here so we save time later.
13621 	 * Note: These flags are NOT in the uscsi man page!!!
13622 	 */
13623 	if (uscmd->uscsi_flags & USCSI_HEAD) {
13624 		flags |= FLAG_HEAD;
13625 	}
13626 
13627 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
13628 		flags |= FLAG_NOINTR;
13629 	}
13630 
13631 	/*
13632 	 * For tagged queueing, things get a bit complicated.
13633 	 * Check first for head of queue and last for ordered queue.
13634 	 * If neither head nor order, use the default driver tag flags.
13635 	 */
13636 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
13637 		if (uscmd->uscsi_flags & USCSI_HTAG) {
13638 			flags |= FLAG_HTAG;
13639 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
13640 			flags |= FLAG_OTAG;
13641 		} else {
13642 			flags |= un->un_tagflags & FLAG_TAGMASK;
13643 		}
13644 	}
13645 
13646 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
13647 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
13648 	}
13649 
13650 	pktp->pkt_flags = flags;
13651 
13652 	/* Copy the caller's CDB into the pkt... */
13653 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
13654 
13655 	if (uscmd->uscsi_timeout == 0) {
13656 		pktp->pkt_time = un->un_uscsi_timeout;
13657 	} else {
13658 		pktp->pkt_time = uscmd->uscsi_timeout;
13659 	}
13660 
13661 	/* need it later to identify USCSI request in sdintr */
13662 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
13663 
13664 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
13665 
13666 	pktp->pkt_private = bp;
13667 	pktp->pkt_comp = sdintr;
13668 	*pktpp = pktp;
13669 
13670 	SD_TRACE(SD_LOG_IO_CORE, un,
13671 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
13672 
13673 	return (SD_PKT_ALLOC_SUCCESS);
13674 }
13675 
13676 
13677 /*
13678  *    Function: sd_destroypkt_for_uscsi
13679  *
13680  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
13681  *		IOs.. Also saves relevant info into the associated uscsi_cmd
13682  *		struct.
13683  *
13684  *     Context: May be called under interrupt context
13685  */
13686 
13687 static void
13688 sd_destroypkt_for_uscsi(struct buf *bp)
13689 {
13690 	struct uscsi_cmd *uscmd;
13691 	struct sd_xbuf	*xp;
13692 	struct scsi_pkt	*pktp;
13693 	struct sd_lun	*un;
13694 
13695 	ASSERT(bp != NULL);
13696 	xp = SD_GET_XBUF(bp);
13697 	ASSERT(xp != NULL);
13698 	un = SD_GET_UN(bp);
13699 	ASSERT(un != NULL);
13700 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13701 	pktp = SD_GET_PKTP(bp);
13702 	ASSERT(pktp != NULL);
13703 
13704 	SD_TRACE(SD_LOG_IO_CORE, un,
13705 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
13706 
13707 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13708 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13709 	ASSERT(uscmd != NULL);
13710 
13711 	/* Save the status and the residual into the uscsi_cmd struct */
13712 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
13713 	uscmd->uscsi_resid  = bp->b_resid;
13714 
13715 	/*
13716 	 * If enabled, copy any saved sense data into the area specified
13717 	 * by the uscsi command.
13718 	 */
13719 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
13720 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
13721 		/*
13722 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
13723 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
13724 		 */
13725 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
13726 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
13727 		bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH);
13728 	}
13729 
13730 	/* We are done with the scsi_pkt; free it now */
13731 	ASSERT(SD_GET_PKTP(bp) != NULL);
13732 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13733 
13734 	SD_TRACE(SD_LOG_IO_CORE, un,
13735 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
13736 }
13737 
13738 
13739 /*
13740  *    Function: sd_bioclone_alloc
13741  *
13742  * Description: Allocate a buf(9S) and init it as per the given buf
13743  *		and the various arguments.  The associated sd_xbuf
13744  *		struct is (nearly) duplicated.  The struct buf *bp
13745  *		argument is saved in new_xp->xb_private.
13746  *
13747  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
13748  *		datalen - size of data area for the shadow bp
13749  *		blkno - starting LBA
13750  *		func - function pointer for b_iodone in the shadow buf. (May
13751  *			be NULL if none.)
13752  *
13753  * Return Code: Pointer to allocates buf(9S) struct
13754  *
13755  *     Context: Can sleep.
13756  */
13757 
13758 static struct buf *
13759 sd_bioclone_alloc(struct buf *bp, size_t datalen,
13760 	daddr_t blkno, int (*func)(struct buf *))
13761 {
13762 	struct	sd_lun	*un;
13763 	struct	sd_xbuf	*xp;
13764 	struct	sd_xbuf	*new_xp;
13765 	struct	buf	*new_bp;
13766 
13767 	ASSERT(bp != NULL);
13768 	xp = SD_GET_XBUF(bp);
13769 	ASSERT(xp != NULL);
13770 	un = SD_GET_UN(bp);
13771 	ASSERT(un != NULL);
13772 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13773 
13774 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
13775 	    NULL, KM_SLEEP);
13776 
13777 	new_bp->b_lblkno	= blkno;
13778 
13779 	/*
13780 	 * Allocate an xbuf for the shadow bp and copy the contents of the
13781 	 * original xbuf into it.
13782 	 */
13783 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
13784 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
13785 
13786 	/*
13787 	 * The given bp is automatically saved in the xb_private member
13788 	 * of the new xbuf.  Callers are allowed to depend on this.
13789 	 */
13790 	new_xp->xb_private = bp;
13791 
13792 	new_bp->b_private  = new_xp;
13793 
13794 	return (new_bp);
13795 }
13796 
13797 /*
13798  *    Function: sd_shadow_buf_alloc
13799  *
13800  * Description: Allocate a buf(9S) and init it as per the given buf
13801  *		and the various arguments.  The associated sd_xbuf
13802  *		struct is (nearly) duplicated.  The struct buf *bp
13803  *		argument is saved in new_xp->xb_private.
13804  *
13805  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
13806  *		datalen - size of data area for the shadow bp
13807  *		bflags - B_READ or B_WRITE (pseudo flag)
13808  *		blkno - starting LBA
13809  *		func - function pointer for b_iodone in the shadow buf. (May
13810  *			be NULL if none.)
13811  *
13812  * Return Code: Pointer to allocates buf(9S) struct
13813  *
13814  *     Context: Can sleep.
13815  */
13816 
13817 static struct buf *
13818 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
13819 	daddr_t blkno, int (*func)(struct buf *))
13820 {
13821 	struct	sd_lun	*un;
13822 	struct	sd_xbuf	*xp;
13823 	struct	sd_xbuf	*new_xp;
13824 	struct	buf	*new_bp;
13825 
13826 	ASSERT(bp != NULL);
13827 	xp = SD_GET_XBUF(bp);
13828 	ASSERT(xp != NULL);
13829 	un = SD_GET_UN(bp);
13830 	ASSERT(un != NULL);
13831 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13832 
13833 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
13834 		bp_mapin(bp);
13835 	}
13836 
13837 	bflags &= (B_READ | B_WRITE);
13838 #if defined(__i386) || defined(__amd64)
13839 	new_bp = getrbuf(KM_SLEEP);
13840 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
13841 	new_bp->b_bcount = datalen;
13842 	new_bp->b_flags = bflags |
13843 	    (bp->b_flags & ~(B_PAGEIO | B_PHYS | B_REMAPPED | B_SHADOW));
13844 #else
13845 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
13846 	    datalen, bflags, SLEEP_FUNC, NULL);
13847 #endif
13848 	new_bp->av_forw	= NULL;
13849 	new_bp->av_back	= NULL;
13850 	new_bp->b_dev	= bp->b_dev;
13851 	new_bp->b_blkno	= blkno;
13852 	new_bp->b_iodone = func;
13853 	new_bp->b_edev	= bp->b_edev;
13854 	new_bp->b_resid	= 0;
13855 
13856 	/* We need to preserve the B_FAILFAST flag */
13857 	if (bp->b_flags & B_FAILFAST) {
13858 		new_bp->b_flags |= B_FAILFAST;
13859 	}
13860 
13861 	/*
13862 	 * Allocate an xbuf for the shadow bp and copy the contents of the
13863 	 * original xbuf into it.
13864 	 */
13865 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
13866 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
13867 
13868 	/* Need later to copy data between the shadow buf & original buf! */
13869 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
13870 
13871 	/*
13872 	 * The given bp is automatically saved in the xb_private member
13873 	 * of the new xbuf.  Callers are allowed to depend on this.
13874 	 */
13875 	new_xp->xb_private = bp;
13876 
13877 	new_bp->b_private  = new_xp;
13878 
13879 	return (new_bp);
13880 }
13881 
13882 /*
13883  *    Function: sd_bioclone_free
13884  *
13885  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
13886  *		in the larger than partition operation.
13887  *
13888  *     Context: May be called under interrupt context
13889  */
13890 
13891 static void
13892 sd_bioclone_free(struct buf *bp)
13893 {
13894 	struct sd_xbuf	*xp;
13895 
13896 	ASSERT(bp != NULL);
13897 	xp = SD_GET_XBUF(bp);
13898 	ASSERT(xp != NULL);
13899 
13900 	/*
13901 	 * Call bp_mapout() before freeing the buf,  in case a lower
13902 	 * layer or HBA  had done a bp_mapin().  we must do this here
13903 	 * as we are the "originator" of the shadow buf.
13904 	 */
13905 	bp_mapout(bp);
13906 
13907 	/*
13908 	 * Null out b_iodone before freeing the bp, to ensure that the driver
13909 	 * never gets confused by a stale value in this field. (Just a little
13910 	 * extra defensiveness here.)
13911 	 */
13912 	bp->b_iodone = NULL;
13913 
13914 	freerbuf(bp);
13915 
13916 	kmem_free(xp, sizeof (struct sd_xbuf));
13917 }
13918 
13919 /*
13920  *    Function: sd_shadow_buf_free
13921  *
13922  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
13923  *
13924  *     Context: May be called under interrupt context
13925  */
13926 
13927 static void
13928 sd_shadow_buf_free(struct buf *bp)
13929 {
13930 	struct sd_xbuf	*xp;
13931 
13932 	ASSERT(bp != NULL);
13933 	xp = SD_GET_XBUF(bp);
13934 	ASSERT(xp != NULL);
13935 
13936 #if defined(__sparc)
13937 	/*
13938 	 * Call bp_mapout() before freeing the buf,  in case a lower
13939 	 * layer or HBA  had done a bp_mapin().  we must do this here
13940 	 * as we are the "originator" of the shadow buf.
13941 	 */
13942 	bp_mapout(bp);
13943 #endif
13944 
13945 	/*
13946 	 * Null out b_iodone before freeing the bp, to ensure that the driver
13947 	 * never gets confused by a stale value in this field. (Just a little
13948 	 * extra defensiveness here.)
13949 	 */
13950 	bp->b_iodone = NULL;
13951 
13952 #if defined(__i386) || defined(__amd64)
13953 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
13954 	freerbuf(bp);
13955 #else
13956 	scsi_free_consistent_buf(bp);
13957 #endif
13958 
13959 	kmem_free(xp, sizeof (struct sd_xbuf));
13960 }
13961 
13962 
13963 /*
13964  *    Function: sd_print_transport_rejected_message
13965  *
13966  * Description: This implements the ludicrously complex rules for printing
13967  *		a "transport rejected" message.  This is to address the
13968  *		specific problem of having a flood of this error message
13969  *		produced when a failover occurs.
13970  *
13971  *     Context: Any.
13972  */
13973 
13974 static void
13975 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
13976 	int code)
13977 {
13978 	ASSERT(un != NULL);
13979 	ASSERT(mutex_owned(SD_MUTEX(un)));
13980 	ASSERT(xp != NULL);
13981 
13982 	/*
13983 	 * Print the "transport rejected" message under the following
13984 	 * conditions:
13985 	 *
13986 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
13987 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
13988 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
13989 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
13990 	 *   scsi_transport(9F) (which indicates that the target might have
13991 	 *   gone off-line).  This uses the un->un_tran_fatal_count
13992 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
13993 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
13994 	 *   from scsi_transport().
13995 	 *
13996 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
13997 	 * the preceeding cases in order for the message to be printed.
13998 	 */
13999 	if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) {
14000 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
14001 		    (code != TRAN_FATAL_ERROR) ||
14002 		    (un->un_tran_fatal_count == 1)) {
14003 			switch (code) {
14004 			case TRAN_BADPKT:
14005 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14006 				    "transport rejected bad packet\n");
14007 				break;
14008 			case TRAN_FATAL_ERROR:
14009 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14010 				    "transport rejected fatal error\n");
14011 				break;
14012 			default:
14013 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14014 				    "transport rejected (%d)\n", code);
14015 				break;
14016 			}
14017 		}
14018 	}
14019 }
14020 
14021 
14022 /*
14023  *    Function: sd_add_buf_to_waitq
14024  *
14025  * Description: Add the given buf(9S) struct to the wait queue for the
14026  *		instance.  If sorting is enabled, then the buf is added
14027  *		to the queue via an elevator sort algorithm (a la
14028  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
14029  *		If sorting is not enabled, then the buf is just added
14030  *		to the end of the wait queue.
14031  *
14032  * Return Code: void
14033  *
14034  *     Context: Does not sleep/block, therefore technically can be called
14035  *		from any context.  However if sorting is enabled then the
14036  *		execution time is indeterminate, and may take long if
14037  *		the wait queue grows large.
14038  */
14039 
14040 static void
14041 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14042 {
14043 	struct buf *ap;
14044 
14045 	ASSERT(bp != NULL);
14046 	ASSERT(un != NULL);
14047 	ASSERT(mutex_owned(SD_MUTEX(un)));
14048 
14049 	/* If the queue is empty, add the buf as the only entry & return. */
14050 	if (un->un_waitq_headp == NULL) {
14051 		ASSERT(un->un_waitq_tailp == NULL);
14052 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14053 		bp->av_forw = NULL;
14054 		return;
14055 	}
14056 
14057 	ASSERT(un->un_waitq_tailp != NULL);
14058 
14059 	/*
14060 	 * If sorting is disabled, just add the buf to the tail end of
14061 	 * the wait queue and return.
14062 	 */
14063 	if (un->un_f_disksort_disabled) {
14064 		un->un_waitq_tailp->av_forw = bp;
14065 		un->un_waitq_tailp = bp;
14066 		bp->av_forw = NULL;
14067 		return;
14068 	}
14069 
14070 	/*
14071 	 * Sort thru the list of requests currently on the wait queue
14072 	 * and add the new buf request at the appropriate position.
14073 	 *
14074 	 * The un->un_waitq_headp is an activity chain pointer on which
14075 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14076 	 * first queue holds those requests which are positioned after
14077 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14078 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14079 	 * Thus we implement a one way scan, retracting after reaching
14080 	 * the end of the drive to the first request on the second
14081 	 * queue, at which time it becomes the first queue.
14082 	 * A one-way scan is natural because of the way UNIX read-ahead
14083 	 * blocks are allocated.
14084 	 *
14085 	 * If we lie after the first request, then we must locate the
14086 	 * second request list and add ourselves to it.
14087 	 */
14088 	ap = un->un_waitq_headp;
14089 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14090 		while (ap->av_forw != NULL) {
14091 			/*
14092 			 * Look for an "inversion" in the (normally
14093 			 * ascending) block numbers. This indicates
14094 			 * the start of the second request list.
14095 			 */
14096 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14097 				/*
14098 				 * Search the second request list for the
14099 				 * first request at a larger block number.
14100 				 * We go before that; however if there is
14101 				 * no such request, we go at the end.
14102 				 */
14103 				do {
14104 					if (SD_GET_BLKNO(bp) <
14105 					    SD_GET_BLKNO(ap->av_forw)) {
14106 						goto insert;
14107 					}
14108 					ap = ap->av_forw;
14109 				} while (ap->av_forw != NULL);
14110 				goto insert;		/* after last */
14111 			}
14112 			ap = ap->av_forw;
14113 		}
14114 
14115 		/*
14116 		 * No inversions... we will go after the last, and
14117 		 * be the first request in the second request list.
14118 		 */
14119 		goto insert;
14120 	}
14121 
14122 	/*
14123 	 * Request is at/after the current request...
14124 	 * sort in the first request list.
14125 	 */
14126 	while (ap->av_forw != NULL) {
14127 		/*
14128 		 * We want to go after the current request (1) if
14129 		 * there is an inversion after it (i.e. it is the end
14130 		 * of the first request list), or (2) if the next
14131 		 * request is a larger block no. than our request.
14132 		 */
14133 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14134 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14135 			goto insert;
14136 		}
14137 		ap = ap->av_forw;
14138 	}
14139 
14140 	/*
14141 	 * Neither a second list nor a larger request, therefore
14142 	 * we go at the end of the first list (which is the same
14143 	 * as the end of the whole schebang).
14144 	 */
14145 insert:
14146 	bp->av_forw = ap->av_forw;
14147 	ap->av_forw = bp;
14148 
14149 	/*
14150 	 * If we inserted onto the tail end of the waitq, make sure the
14151 	 * tail pointer is updated.
14152 	 */
14153 	if (ap == un->un_waitq_tailp) {
14154 		un->un_waitq_tailp = bp;
14155 	}
14156 }
14157 
14158 
14159 /*
14160  *    Function: sd_start_cmds
14161  *
14162  * Description: Remove and transport cmds from the driver queues.
14163  *
14164  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14165  *
14166  *		immed_bp - ptr to a buf to be transported immediately. Only
14167  *		the immed_bp is transported; bufs on the waitq are not
14168  *		processed and the un_retry_bp is not checked.  If immed_bp is
14169  *		NULL, then normal queue processing is performed.
14170  *
14171  *     Context: May be called from kernel thread context, interrupt context,
14172  *		or runout callback context. This function may not block or
14173  *		call routines that block.
14174  */
14175 
14176 static void
14177 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14178 {
14179 	struct	sd_xbuf	*xp;
14180 	struct	buf	*bp;
14181 	void	(*statp)(kstat_io_t *);
14182 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14183 	void	(*saved_statp)(kstat_io_t *);
14184 #endif
14185 	int	rval;
14186 
14187 	ASSERT(un != NULL);
14188 	ASSERT(mutex_owned(SD_MUTEX(un)));
14189 	ASSERT(un->un_ncmds_in_transport >= 0);
14190 	ASSERT(un->un_throttle >= 0);
14191 
14192 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14193 
14194 	do {
14195 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14196 		saved_statp = NULL;
14197 #endif
14198 
14199 		/*
14200 		 * If we are syncing or dumping, fail the command to
14201 		 * avoid recursively calling back into scsi_transport().
14202 		 * The dump I/O itself uses a separate code path so this
14203 		 * only prevents non-dump I/O from being sent while dumping.
14204 		 * File system sync takes place before dumping begins.
14205 		 * During panic, filesystem I/O is allowed provided
14206 		 * un_in_callback is <= 1.  This is to prevent recursion
14207 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14208 		 * sd_start_cmds and so on.  See panic.c for more information
14209 		 * about the states the system can be in during panic.
14210 		 */
14211 		if ((un->un_state == SD_STATE_DUMPING) ||
14212 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14213 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14214 			    "sd_start_cmds: panicking\n");
14215 			goto exit;
14216 		}
14217 
14218 		if ((bp = immed_bp) != NULL) {
14219 			/*
14220 			 * We have a bp that must be transported immediately.
14221 			 * It's OK to transport the immed_bp here without doing
14222 			 * the throttle limit check because the immed_bp is
14223 			 * always used in a retry/recovery case. This means
14224 			 * that we know we are not at the throttle limit by
14225 			 * virtue of the fact that to get here we must have
14226 			 * already gotten a command back via sdintr(). This also
14227 			 * relies on (1) the command on un_retry_bp preventing
14228 			 * further commands from the waitq from being issued;
14229 			 * and (2) the code in sd_retry_command checking the
14230 			 * throttle limit before issuing a delayed or immediate
14231 			 * retry. This holds even if the throttle limit is
14232 			 * currently ratcheted down from its maximum value.
14233 			 */
14234 			statp = kstat_runq_enter;
14235 			if (bp == un->un_retry_bp) {
14236 				ASSERT((un->un_retry_statp == NULL) ||
14237 				    (un->un_retry_statp == kstat_waitq_enter) ||
14238 				    (un->un_retry_statp ==
14239 				    kstat_runq_back_to_waitq));
14240 				/*
14241 				 * If the waitq kstat was incremented when
14242 				 * sd_set_retry_bp() queued this bp for a retry,
14243 				 * then we must set up statp so that the waitq
14244 				 * count will get decremented correctly below.
14245 				 * Also we must clear un->un_retry_statp to
14246 				 * ensure that we do not act on a stale value
14247 				 * in this field.
14248 				 */
14249 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14250 				    (un->un_retry_statp ==
14251 				    kstat_runq_back_to_waitq)) {
14252 					statp = kstat_waitq_to_runq;
14253 				}
14254 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14255 				saved_statp = un->un_retry_statp;
14256 #endif
14257 				un->un_retry_statp = NULL;
14258 
14259 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14260 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14261 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14262 				    un, un->un_retry_bp, un->un_throttle,
14263 				    un->un_ncmds_in_transport);
14264 			} else {
14265 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14266 				    "processing priority bp:0x%p\n", bp);
14267 			}
14268 
14269 		} else if ((bp = un->un_waitq_headp) != NULL) {
14270 			/*
14271 			 * A command on the waitq is ready to go, but do not
14272 			 * send it if:
14273 			 *
14274 			 * (1) the throttle limit has been reached, or
14275 			 * (2) a retry is pending, or
14276 			 * (3) a START_STOP_UNIT callback pending, or
14277 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14278 			 *	command is pending.
14279 			 *
14280 			 * For all of these conditions, IO processing will
14281 			 * restart after the condition is cleared.
14282 			 */
14283 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14284 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14285 				    "sd_start_cmds: exiting, "
14286 				    "throttle limit reached!\n");
14287 				goto exit;
14288 			}
14289 			if (un->un_retry_bp != NULL) {
14290 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14291 				    "sd_start_cmds: exiting, retry pending!\n");
14292 				goto exit;
14293 			}
14294 			if (un->un_startstop_timeid != NULL) {
14295 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14296 				    "sd_start_cmds: exiting, "
14297 				    "START_STOP pending!\n");
14298 				goto exit;
14299 			}
14300 			if (un->un_direct_priority_timeid != NULL) {
14301 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14302 				    "sd_start_cmds: exiting, "
14303 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14304 				goto exit;
14305 			}
14306 
14307 			/* Dequeue the command */
14308 			un->un_waitq_headp = bp->av_forw;
14309 			if (un->un_waitq_headp == NULL) {
14310 				un->un_waitq_tailp = NULL;
14311 			}
14312 			bp->av_forw = NULL;
14313 			statp = kstat_waitq_to_runq;
14314 			SD_TRACE(SD_LOG_IO_CORE, un,
14315 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14316 
14317 		} else {
14318 			/* No work to do so bail out now */
14319 			SD_TRACE(SD_LOG_IO_CORE, un,
14320 			    "sd_start_cmds: no more work, exiting!\n");
14321 			goto exit;
14322 		}
14323 
14324 		/*
14325 		 * Reset the state to normal. This is the mechanism by which
14326 		 * the state transitions from either SD_STATE_RWAIT or
14327 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14328 		 * If state is SD_STATE_PM_CHANGING then this command is
14329 		 * part of the device power control and the state must
14330 		 * not be put back to normal. Doing so would would
14331 		 * allow new commands to proceed when they shouldn't,
14332 		 * the device may be going off.
14333 		 */
14334 		if ((un->un_state != SD_STATE_SUSPENDED) &&
14335 		    (un->un_state != SD_STATE_PM_CHANGING)) {
14336 			New_state(un, SD_STATE_NORMAL);
14337 		    }
14338 
14339 		xp = SD_GET_XBUF(bp);
14340 		ASSERT(xp != NULL);
14341 
14342 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14343 		/*
14344 		 * Allocate the scsi_pkt if we need one, or attach DMA
14345 		 * resources if we have a scsi_pkt that needs them. The
14346 		 * latter should only occur for commands that are being
14347 		 * retried.
14348 		 */
14349 		if ((xp->xb_pktp == NULL) ||
14350 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14351 #else
14352 		if (xp->xb_pktp == NULL) {
14353 #endif
14354 			/*
14355 			 * There is no scsi_pkt allocated for this buf. Call
14356 			 * the initpkt function to allocate & init one.
14357 			 *
14358 			 * The scsi_init_pkt runout callback functionality is
14359 			 * implemented as follows:
14360 			 *
14361 			 * 1) The initpkt function always calls
14362 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14363 			 *    callback routine.
14364 			 * 2) A successful packet allocation is initialized and
14365 			 *    the I/O is transported.
14366 			 * 3) The I/O associated with an allocation resource
14367 			 *    failure is left on its queue to be retried via
14368 			 *    runout or the next I/O.
14369 			 * 4) The I/O associated with a DMA error is removed
14370 			 *    from the queue and failed with EIO. Processing of
14371 			 *    the transport queues is also halted to be
14372 			 *    restarted via runout or the next I/O.
14373 			 * 5) The I/O associated with a CDB size or packet
14374 			 *    size error is removed from the queue and failed
14375 			 *    with EIO. Processing of the transport queues is
14376 			 *    continued.
14377 			 *
14378 			 * Note: there is no interface for canceling a runout
14379 			 * callback. To prevent the driver from detaching or
14380 			 * suspending while a runout is pending the driver
14381 			 * state is set to SD_STATE_RWAIT
14382 			 *
14383 			 * Note: using the scsi_init_pkt callback facility can
14384 			 * result in an I/O request persisting at the head of
14385 			 * the list which cannot be satisfied even after
14386 			 * multiple retries. In the future the driver may
14387 			 * implement some kind of maximum runout count before
14388 			 * failing an I/O.
14389 			 *
14390 			 * Note: the use of funcp below may seem superfluous,
14391 			 * but it helps warlock figure out the correct
14392 			 * initpkt function calls (see [s]sd.wlcmd).
14393 			 */
14394 			struct scsi_pkt	*pktp;
14395 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14396 
14397 			ASSERT(bp != un->un_rqs_bp);
14398 
14399 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
14400 			switch ((*funcp)(bp, &pktp)) {
14401 			case  SD_PKT_ALLOC_SUCCESS:
14402 				xp->xb_pktp = pktp;
14403 				SD_TRACE(SD_LOG_IO_CORE, un,
14404 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14405 				    pktp);
14406 				goto got_pkt;
14407 
14408 			case SD_PKT_ALLOC_FAILURE:
14409 				/*
14410 				 * Temporary (hopefully) resource depletion.
14411 				 * Since retries and RQS commands always have a
14412 				 * scsi_pkt allocated, these cases should never
14413 				 * get here. So the only cases this needs to
14414 				 * handle is a bp from the waitq (which we put
14415 				 * back onto the waitq for sdrunout), or a bp
14416 				 * sent as an immed_bp (which we just fail).
14417 				 */
14418 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14419 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14420 
14421 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14422 
14423 				if (bp == immed_bp) {
14424 					/*
14425 					 * If SD_XB_DMA_FREED is clear, then
14426 					 * this is a failure to allocate a
14427 					 * scsi_pkt, and we must fail the
14428 					 * command.
14429 					 */
14430 					if ((xp->xb_pkt_flags &
14431 					    SD_XB_DMA_FREED) == 0) {
14432 						break;
14433 					}
14434 
14435 					/*
14436 					 * If this immediate command is NOT our
14437 					 * un_retry_bp, then we must fail it.
14438 					 */
14439 					if (bp != un->un_retry_bp) {
14440 						break;
14441 					}
14442 
14443 					/*
14444 					 * We get here if this cmd is our
14445 					 * un_retry_bp that was DMAFREED, but
14446 					 * scsi_init_pkt() failed to reallocate
14447 					 * DMA resources when we attempted to
14448 					 * retry it. This can happen when an
14449 					 * mpxio failover is in progress, but
14450 					 * we don't want to just fail the
14451 					 * command in this case.
14452 					 *
14453 					 * Use timeout(9F) to restart it after
14454 					 * a 100ms delay.  We don't want to
14455 					 * let sdrunout() restart it, because
14456 					 * sdrunout() is just supposed to start
14457 					 * commands that are sitting on the
14458 					 * wait queue.  The un_retry_bp stays
14459 					 * set until the command completes, but
14460 					 * sdrunout can be called many times
14461 					 * before that happens.  Since sdrunout
14462 					 * cannot tell if the un_retry_bp is
14463 					 * already in the transport, it could
14464 					 * end up calling scsi_transport() for
14465 					 * the un_retry_bp multiple times.
14466 					 *
14467 					 * Also: don't schedule the callback
14468 					 * if some other callback is already
14469 					 * pending.
14470 					 */
14471 					if (un->un_retry_statp == NULL) {
14472 						/*
14473 						 * restore the kstat pointer to
14474 						 * keep kstat counts coherent
14475 						 * when we do retry the command.
14476 						 */
14477 						un->un_retry_statp =
14478 						    saved_statp;
14479 					}
14480 
14481 					if ((un->un_startstop_timeid == NULL) &&
14482 					    (un->un_retry_timeid == NULL) &&
14483 					    (un->un_direct_priority_timeid ==
14484 					    NULL)) {
14485 
14486 						un->un_retry_timeid =
14487 						    timeout(
14488 						    sd_start_retry_command,
14489 						    un, SD_RESTART_TIMEOUT);
14490 					}
14491 					goto exit;
14492 				}
14493 
14494 #else
14495 				if (bp == immed_bp) {
14496 					break;	/* Just fail the command */
14497 				}
14498 #endif
14499 
14500 				/* Add the buf back to the head of the waitq */
14501 				bp->av_forw = un->un_waitq_headp;
14502 				un->un_waitq_headp = bp;
14503 				if (un->un_waitq_tailp == NULL) {
14504 					un->un_waitq_tailp = bp;
14505 				}
14506 				goto exit;
14507 
14508 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
14509 				/*
14510 				 * HBA DMA resource failure. Fail the command
14511 				 * and continue processing of the queues.
14512 				 */
14513 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14514 				    "sd_start_cmds: "
14515 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
14516 				break;
14517 
14518 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
14519 				/*
14520 				 * Note:x86: Partial DMA mapping not supported
14521 				 * for USCSI commands, and all the needed DMA
14522 				 * resources were not allocated.
14523 				 */
14524 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14525 				    "sd_start_cmds: "
14526 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
14527 				break;
14528 
14529 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
14530 				/*
14531 				 * Note:x86: Request cannot fit into CDB based
14532 				 * on lba and len.
14533 				 */
14534 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14535 				    "sd_start_cmds: "
14536 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
14537 				break;
14538 
14539 			default:
14540 				/* Should NEVER get here! */
14541 				panic("scsi_initpkt error");
14542 				/*NOTREACHED*/
14543 			}
14544 
14545 			/*
14546 			 * Fatal error in allocating a scsi_pkt for this buf.
14547 			 * Update kstats & return the buf with an error code.
14548 			 * We must use sd_return_failed_command_no_restart() to
14549 			 * avoid a recursive call back into sd_start_cmds().
14550 			 * However this also means that we must keep processing
14551 			 * the waitq here in order to avoid stalling.
14552 			 */
14553 			if (statp == kstat_waitq_to_runq) {
14554 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
14555 			}
14556 			sd_return_failed_command_no_restart(un, bp, EIO);
14557 			if (bp == immed_bp) {
14558 				/* immed_bp is gone by now, so clear this */
14559 				immed_bp = NULL;
14560 			}
14561 			continue;
14562 		}
14563 got_pkt:
14564 		if (bp == immed_bp) {
14565 			/* goto the head of the class.... */
14566 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
14567 		}
14568 
14569 		un->un_ncmds_in_transport++;
14570 		SD_UPDATE_KSTATS(un, statp, bp);
14571 
14572 		/*
14573 		 * Call scsi_transport() to send the command to the target.
14574 		 * According to SCSA architecture, we must drop the mutex here
14575 		 * before calling scsi_transport() in order to avoid deadlock.
14576 		 * Note that the scsi_pkt's completion routine can be executed
14577 		 * (from interrupt context) even before the call to
14578 		 * scsi_transport() returns.
14579 		 */
14580 		SD_TRACE(SD_LOG_IO_CORE, un,
14581 		    "sd_start_cmds: calling scsi_transport()\n");
14582 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
14583 
14584 		mutex_exit(SD_MUTEX(un));
14585 		rval = scsi_transport(xp->xb_pktp);
14586 		mutex_enter(SD_MUTEX(un));
14587 
14588 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14589 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
14590 
14591 		switch (rval) {
14592 		case TRAN_ACCEPT:
14593 			/* Clear this with every pkt accepted by the HBA */
14594 			un->un_tran_fatal_count = 0;
14595 			break;	/* Success; try the next cmd (if any) */
14596 
14597 		case TRAN_BUSY:
14598 			un->un_ncmds_in_transport--;
14599 			ASSERT(un->un_ncmds_in_transport >= 0);
14600 
14601 			/*
14602 			 * Don't retry request sense, the sense data
14603 			 * is lost when another request is sent.
14604 			 * Free up the rqs buf and retry
14605 			 * the original failed cmd.  Update kstat.
14606 			 */
14607 			if (bp == un->un_rqs_bp) {
14608 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14609 				bp = sd_mark_rqs_idle(un, xp);
14610 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
14611 					NULL, NULL, EIO, SD_BSY_TIMEOUT / 500,
14612 					kstat_waitq_enter);
14613 				goto exit;
14614 			}
14615 
14616 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14617 			/*
14618 			 * Free the DMA resources for the  scsi_pkt. This will
14619 			 * allow mpxio to select another path the next time
14620 			 * we call scsi_transport() with this scsi_pkt.
14621 			 * See sdintr() for the rationalization behind this.
14622 			 */
14623 			if ((un->un_f_is_fibre == TRUE) &&
14624 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
14625 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
14626 				scsi_dmafree(xp->xb_pktp);
14627 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
14628 			}
14629 #endif
14630 
14631 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
14632 				/*
14633 				 * Commands that are SD_PATH_DIRECT_PRIORITY
14634 				 * are for error recovery situations. These do
14635 				 * not use the normal command waitq, so if they
14636 				 * get a TRAN_BUSY we cannot put them back onto
14637 				 * the waitq for later retry. One possible
14638 				 * problem is that there could already be some
14639 				 * other command on un_retry_bp that is waiting
14640 				 * for this one to complete, so we would be
14641 				 * deadlocked if we put this command back onto
14642 				 * the waitq for later retry (since un_retry_bp
14643 				 * must complete before the driver gets back to
14644 				 * commands on the waitq).
14645 				 *
14646 				 * To avoid deadlock we must schedule a callback
14647 				 * that will restart this command after a set
14648 				 * interval.  This should keep retrying for as
14649 				 * long as the underlying transport keeps
14650 				 * returning TRAN_BUSY (just like for other
14651 				 * commands).  Use the same timeout interval as
14652 				 * for the ordinary TRAN_BUSY retry.
14653 				 */
14654 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14655 				    "sd_start_cmds: scsi_transport() returned "
14656 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
14657 
14658 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14659 				un->un_direct_priority_timeid =
14660 				    timeout(sd_start_direct_priority_command,
14661 				    bp, SD_BSY_TIMEOUT / 500);
14662 
14663 				goto exit;
14664 			}
14665 
14666 			/*
14667 			 * For TRAN_BUSY, we want to reduce the throttle value,
14668 			 * unless we are retrying a command.
14669 			 */
14670 			if (bp != un->un_retry_bp) {
14671 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
14672 			}
14673 
14674 			/*
14675 			 * Set up the bp to be tried again 10 ms later.
14676 			 * Note:x86: Is there a timeout value in the sd_lun
14677 			 * for this condition?
14678 			 */
14679 			sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500,
14680 				kstat_runq_back_to_waitq);
14681 			goto exit;
14682 
14683 		case TRAN_FATAL_ERROR:
14684 			un->un_tran_fatal_count++;
14685 			/* FALLTHRU */
14686 
14687 		case TRAN_BADPKT:
14688 		default:
14689 			un->un_ncmds_in_transport--;
14690 			ASSERT(un->un_ncmds_in_transport >= 0);
14691 
14692 			/*
14693 			 * If this is our REQUEST SENSE command with a
14694 			 * transport error, we must get back the pointers
14695 			 * to the original buf, and mark the REQUEST
14696 			 * SENSE command as "available".
14697 			 */
14698 			if (bp == un->un_rqs_bp) {
14699 				bp = sd_mark_rqs_idle(un, xp);
14700 				xp = SD_GET_XBUF(bp);
14701 			} else {
14702 				/*
14703 				 * Legacy behavior: do not update transport
14704 				 * error count for request sense commands.
14705 				 */
14706 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
14707 			}
14708 
14709 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14710 			sd_print_transport_rejected_message(un, xp, rval);
14711 
14712 			/*
14713 			 * We must use sd_return_failed_command_no_restart() to
14714 			 * avoid a recursive call back into sd_start_cmds().
14715 			 * However this also means that we must keep processing
14716 			 * the waitq here in order to avoid stalling.
14717 			 */
14718 			sd_return_failed_command_no_restart(un, bp, EIO);
14719 
14720 			/*
14721 			 * Notify any threads waiting in sd_ddi_suspend() that
14722 			 * a command completion has occurred.
14723 			 */
14724 			if (un->un_state == SD_STATE_SUSPENDED) {
14725 				cv_broadcast(&un->un_disk_busy_cv);
14726 			}
14727 
14728 			if (bp == immed_bp) {
14729 				/* immed_bp is gone by now, so clear this */
14730 				immed_bp = NULL;
14731 			}
14732 			break;
14733 		}
14734 
14735 	} while (immed_bp == NULL);
14736 
14737 exit:
14738 	ASSERT(mutex_owned(SD_MUTEX(un)));
14739 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
14740 }
14741 
14742 
14743 /*
14744  *    Function: sd_return_command
14745  *
14746  * Description: Returns a command to its originator (with or without an
14747  *		error).  Also starts commands waiting to be transported
14748  *		to the target.
14749  *
14750  *     Context: May be called from interrupt, kernel, or timeout context
14751  */
14752 
14753 static void
14754 sd_return_command(struct sd_lun *un, struct buf *bp)
14755 {
14756 	struct sd_xbuf *xp;
14757 #if defined(__i386) || defined(__amd64)
14758 	struct scsi_pkt *pktp;
14759 #endif
14760 
14761 	ASSERT(bp != NULL);
14762 	ASSERT(un != NULL);
14763 	ASSERT(mutex_owned(SD_MUTEX(un)));
14764 	ASSERT(bp != un->un_rqs_bp);
14765 	xp = SD_GET_XBUF(bp);
14766 	ASSERT(xp != NULL);
14767 
14768 #if defined(__i386) || defined(__amd64)
14769 	pktp = SD_GET_PKTP(bp);
14770 #endif
14771 
14772 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
14773 
14774 #if defined(__i386) || defined(__amd64)
14775 	/*
14776 	 * Note:x86: check for the "sdrestart failed" case.
14777 	 */
14778 	if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
14779 		(geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
14780 		(xp->xb_pktp->pkt_resid == 0)) {
14781 
14782 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
14783 			/*
14784 			 * Successfully set up next portion of cmd
14785 			 * transfer, try sending it
14786 			 */
14787 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
14788 			    NULL, NULL, 0, (clock_t)0, NULL);
14789 			sd_start_cmds(un, NULL);
14790 			return;	/* Note:x86: need a return here? */
14791 		}
14792 	}
14793 #endif
14794 
14795 	/*
14796 	 * If this is the failfast bp, clear it from un_failfast_bp. This
14797 	 * can happen if upon being re-tried the failfast bp either
14798 	 * succeeded or encountered another error (possibly even a different
14799 	 * error than the one that precipitated the failfast state, but in
14800 	 * that case it would have had to exhaust retries as well). Regardless,
14801 	 * this should not occur whenever the instance is in the active
14802 	 * failfast state.
14803 	 */
14804 	if (bp == un->un_failfast_bp) {
14805 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
14806 		un->un_failfast_bp = NULL;
14807 	}
14808 
14809 	/*
14810 	 * Clear the failfast state upon successful completion of ANY cmd.
14811 	 */
14812 	if (bp->b_error == 0) {
14813 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
14814 	}
14815 
14816 	/*
14817 	 * This is used if the command was retried one or more times. Show that
14818 	 * we are done with it, and allow processing of the waitq to resume.
14819 	 */
14820 	if (bp == un->un_retry_bp) {
14821 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14822 		    "sd_return_command: un:0x%p: "
14823 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
14824 		un->un_retry_bp = NULL;
14825 		un->un_retry_statp = NULL;
14826 	}
14827 
14828 	SD_UPDATE_RDWR_STATS(un, bp);
14829 	SD_UPDATE_PARTITION_STATS(un, bp);
14830 
14831 	switch (un->un_state) {
14832 	case SD_STATE_SUSPENDED:
14833 		/*
14834 		 * Notify any threads waiting in sd_ddi_suspend() that
14835 		 * a command completion has occurred.
14836 		 */
14837 		cv_broadcast(&un->un_disk_busy_cv);
14838 		break;
14839 	default:
14840 		sd_start_cmds(un, NULL);
14841 		break;
14842 	}
14843 
14844 	/* Return this command up the iodone chain to its originator. */
14845 	mutex_exit(SD_MUTEX(un));
14846 
14847 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
14848 	xp->xb_pktp = NULL;
14849 
14850 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
14851 
14852 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14853 	mutex_enter(SD_MUTEX(un));
14854 
14855 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
14856 }
14857 
14858 
14859 /*
14860  *    Function: sd_return_failed_command
14861  *
14862  * Description: Command completion when an error occurred.
14863  *
14864  *     Context: May be called from interrupt context
14865  */
14866 
14867 static void
14868 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
14869 {
14870 	ASSERT(bp != NULL);
14871 	ASSERT(un != NULL);
14872 	ASSERT(mutex_owned(SD_MUTEX(un)));
14873 
14874 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14875 	    "sd_return_failed_command: entry\n");
14876 
14877 	/*
14878 	 * b_resid could already be nonzero due to a partial data
14879 	 * transfer, so do not change it here.
14880 	 */
14881 	SD_BIOERROR(bp, errcode);
14882 
14883 	sd_return_command(un, bp);
14884 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14885 	    "sd_return_failed_command: exit\n");
14886 }
14887 
14888 
14889 /*
14890  *    Function: sd_return_failed_command_no_restart
14891  *
14892  * Description: Same as sd_return_failed_command, but ensures that no
14893  *		call back into sd_start_cmds will be issued.
14894  *
14895  *     Context: May be called from interrupt context
14896  */
14897 
14898 static void
14899 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
14900 	int errcode)
14901 {
14902 	struct sd_xbuf *xp;
14903 
14904 	ASSERT(bp != NULL);
14905 	ASSERT(un != NULL);
14906 	ASSERT(mutex_owned(SD_MUTEX(un)));
14907 	xp = SD_GET_XBUF(bp);
14908 	ASSERT(xp != NULL);
14909 	ASSERT(errcode != 0);
14910 
14911 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14912 	    "sd_return_failed_command_no_restart: entry\n");
14913 
14914 	/*
14915 	 * b_resid could already be nonzero due to a partial data
14916 	 * transfer, so do not change it here.
14917 	 */
14918 	SD_BIOERROR(bp, errcode);
14919 
14920 	/*
14921 	 * If this is the failfast bp, clear it. This can happen if the
14922 	 * failfast bp encounterd a fatal error when we attempted to
14923 	 * re-try it (such as a scsi_transport(9F) failure).  However
14924 	 * we should NOT be in an active failfast state if the failfast
14925 	 * bp is not NULL.
14926 	 */
14927 	if (bp == un->un_failfast_bp) {
14928 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
14929 		un->un_failfast_bp = NULL;
14930 	}
14931 
14932 	if (bp == un->un_retry_bp) {
14933 		/*
14934 		 * This command was retried one or more times. Show that we are
14935 		 * done with it, and allow processing of the waitq to resume.
14936 		 */
14937 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14938 		    "sd_return_failed_command_no_restart: "
14939 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
14940 		un->un_retry_bp = NULL;
14941 		un->un_retry_statp = NULL;
14942 	}
14943 
14944 	SD_UPDATE_RDWR_STATS(un, bp);
14945 	SD_UPDATE_PARTITION_STATS(un, bp);
14946 
14947 	mutex_exit(SD_MUTEX(un));
14948 
14949 	if (xp->xb_pktp != NULL) {
14950 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
14951 		xp->xb_pktp = NULL;
14952 	}
14953 
14954 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
14955 
14956 	mutex_enter(SD_MUTEX(un));
14957 
14958 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14959 	    "sd_return_failed_command_no_restart: exit\n");
14960 }
14961 
14962 
14963 /*
14964  *    Function: sd_retry_command
14965  *
14966  * Description: queue up a command for retry, or (optionally) fail it
14967  *		if retry counts are exhausted.
14968  *
14969  *   Arguments: un - Pointer to the sd_lun struct for the target.
14970  *
14971  *		bp - Pointer to the buf for the command to be retried.
14972  *
14973  *		retry_check_flag - Flag to see which (if any) of the retry
14974  *		   counts should be decremented/checked. If the indicated
14975  *		   retry count is exhausted, then the command will not be
14976  *		   retried; it will be failed instead. This should use a
14977  *		   value equal to one of the following:
14978  *
14979  *			SD_RETRIES_NOCHECK
14980  *			SD_RESD_RETRIES_STANDARD
14981  *			SD_RETRIES_VICTIM
14982  *
14983  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
14984  *		   if the check should be made to see of FLAG_ISOLATE is set
14985  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
14986  *		   not retried, it is simply failed.
14987  *
14988  *		user_funcp - Ptr to function to call before dispatching the
14989  *		   command. May be NULL if no action needs to be performed.
14990  *		   (Primarily intended for printing messages.)
14991  *
14992  *		user_arg - Optional argument to be passed along to
14993  *		   the user_funcp call.
14994  *
14995  *		failure_code - errno return code to set in the bp if the
14996  *		   command is going to be failed.
14997  *
14998  *		retry_delay - Retry delay interval in (clock_t) units. May
14999  *		   be zero which indicates that the retry should be retried
15000  *		   immediately (ie, without an intervening delay).
15001  *
15002  *		statp - Ptr to kstat function to be updated if the command
15003  *		   is queued for a delayed retry. May be NULL if no kstat
15004  *		   update is desired.
15005  *
15006  *     Context: May be called from interupt context.
15007  */
15008 
15009 static void
15010 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
15011 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
15012 	code), void *user_arg, int failure_code,  clock_t retry_delay,
15013 	void (*statp)(kstat_io_t *))
15014 {
15015 	struct sd_xbuf	*xp;
15016 	struct scsi_pkt	*pktp;
15017 
15018 	ASSERT(un != NULL);
15019 	ASSERT(mutex_owned(SD_MUTEX(un)));
15020 	ASSERT(bp != NULL);
15021 	xp = SD_GET_XBUF(bp);
15022 	ASSERT(xp != NULL);
15023 	pktp = SD_GET_PKTP(bp);
15024 	ASSERT(pktp != NULL);
15025 
15026 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15027 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
15028 
15029 	/*
15030 	 * If we are syncing or dumping, fail the command to avoid
15031 	 * recursively calling back into scsi_transport().
15032 	 */
15033 	if (ddi_in_panic()) {
15034 		goto fail_command_no_log;
15035 	}
15036 
15037 	/*
15038 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
15039 	 * log an error and fail the command.
15040 	 */
15041 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15042 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15043 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15044 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15045 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15046 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15047 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15048 		goto fail_command;
15049 	}
15050 
15051 	/*
15052 	 * If we are suspended, then put the command onto head of the
15053 	 * wait queue since we don't want to start more commands.
15054 	 */
15055 	switch (un->un_state) {
15056 	case SD_STATE_SUSPENDED:
15057 	case SD_STATE_DUMPING:
15058 		bp->av_forw = un->un_waitq_headp;
15059 		un->un_waitq_headp = bp;
15060 		if (un->un_waitq_tailp == NULL) {
15061 			un->un_waitq_tailp = bp;
15062 		}
15063 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15064 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15065 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15066 		return;
15067 	default:
15068 		break;
15069 	}
15070 
15071 	/*
15072 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15073 	 * is set; if it is then we do not want to retry the command.
15074 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15075 	 */
15076 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15077 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15078 			goto fail_command;
15079 		}
15080 	}
15081 
15082 
15083 	/*
15084 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15085 	 * command timeout or a selection timeout has occurred. This means
15086 	 * that we were unable to establish an kind of communication with
15087 	 * the target, and subsequent retries and/or commands are likely
15088 	 * to encounter similar results and take a long time to complete.
15089 	 *
15090 	 * If this is a failfast error condition, we need to update the
15091 	 * failfast state, even if this bp does not have B_FAILFAST set.
15092 	 */
15093 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15094 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15095 			ASSERT(un->un_failfast_bp == NULL);
15096 			/*
15097 			 * If we are already in the active failfast state, and
15098 			 * another failfast error condition has been detected,
15099 			 * then fail this command if it has B_FAILFAST set.
15100 			 * If B_FAILFAST is clear, then maintain the legacy
15101 			 * behavior of retrying heroically, even tho this will
15102 			 * take a lot more time to fail the command.
15103 			 */
15104 			if (bp->b_flags & B_FAILFAST) {
15105 				goto fail_command;
15106 			}
15107 		} else {
15108 			/*
15109 			 * We're not in the active failfast state, but we
15110 			 * have a failfast error condition, so we must begin
15111 			 * transition to the next state. We do this regardless
15112 			 * of whether or not this bp has B_FAILFAST set.
15113 			 */
15114 			if (un->un_failfast_bp == NULL) {
15115 				/*
15116 				 * This is the first bp to meet a failfast
15117 				 * condition so save it on un_failfast_bp &
15118 				 * do normal retry processing. Do not enter
15119 				 * active failfast state yet. This marks
15120 				 * entry into the "failfast pending" state.
15121 				 */
15122 				un->un_failfast_bp = bp;
15123 
15124 			} else if (un->un_failfast_bp == bp) {
15125 				/*
15126 				 * This is the second time *this* bp has
15127 				 * encountered a failfast error condition,
15128 				 * so enter active failfast state & flush
15129 				 * queues as appropriate.
15130 				 */
15131 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15132 				un->un_failfast_bp = NULL;
15133 				sd_failfast_flushq(un);
15134 
15135 				/*
15136 				 * Fail this bp now if B_FAILFAST set;
15137 				 * otherwise continue with retries. (It would
15138 				 * be pretty ironic if this bp succeeded on a
15139 				 * subsequent retry after we just flushed all
15140 				 * the queues).
15141 				 */
15142 				if (bp->b_flags & B_FAILFAST) {
15143 					goto fail_command;
15144 				}
15145 
15146 #if !defined(lint) && !defined(__lint)
15147 			} else {
15148 				/*
15149 				 * If neither of the preceeding conditionals
15150 				 * was true, it means that there is some
15151 				 * *other* bp that has met an inital failfast
15152 				 * condition and is currently either being
15153 				 * retried or is waiting to be retried. In
15154 				 * that case we should perform normal retry
15155 				 * processing on *this* bp, since there is a
15156 				 * chance that the current failfast condition
15157 				 * is transient and recoverable. If that does
15158 				 * not turn out to be the case, then retries
15159 				 * will be cleared when the wait queue is
15160 				 * flushed anyway.
15161 				 */
15162 #endif
15163 			}
15164 		}
15165 	} else {
15166 		/*
15167 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15168 		 * likely were able to at least establish some level of
15169 		 * communication with the target and subsequent commands
15170 		 * and/or retries are likely to get through to the target,
15171 		 * In this case we want to be aggressive about clearing
15172 		 * the failfast state. Note that this does not affect
15173 		 * the "failfast pending" condition.
15174 		 */
15175 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15176 	}
15177 
15178 
15179 	/*
15180 	 * Check the specified retry count to see if we can still do
15181 	 * any retries with this pkt before we should fail it.
15182 	 */
15183 	switch (retry_check_flag & SD_RETRIES_MASK) {
15184 	case SD_RETRIES_VICTIM:
15185 		/*
15186 		 * Check the victim retry count. If exhausted, then fall
15187 		 * thru & check against the standard retry count.
15188 		 */
15189 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15190 			/* Increment count & proceed with the retry */
15191 			xp->xb_victim_retry_count++;
15192 			break;
15193 		}
15194 		/* Victim retries exhausted, fall back to std. retries... */
15195 		/* FALLTHRU */
15196 
15197 	case SD_RETRIES_STANDARD:
15198 		if (xp->xb_retry_count >= un->un_retry_count) {
15199 			/* Retries exhausted, fail the command */
15200 			SD_TRACE(SD_LOG_IO_CORE, un,
15201 			    "sd_retry_command: retries exhausted!\n");
15202 			/*
15203 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15204 			 * commands with nonzero pkt_resid.
15205 			 */
15206 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15207 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15208 			    (pktp->pkt_resid != 0)) {
15209 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15210 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15211 					SD_UPDATE_B_RESID(bp, pktp);
15212 				}
15213 			}
15214 			goto fail_command;
15215 		}
15216 		xp->xb_retry_count++;
15217 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15218 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15219 		break;
15220 
15221 	case SD_RETRIES_UA:
15222 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15223 			/* Retries exhausted, fail the command */
15224 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15225 			    "Unit Attention retries exhausted. "
15226 			    "Check the target.\n");
15227 			goto fail_command;
15228 		}
15229 		xp->xb_ua_retry_count++;
15230 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15231 		    "sd_retry_command: retry count:%d\n",
15232 			xp->xb_ua_retry_count);
15233 		break;
15234 
15235 	case SD_RETRIES_BUSY:
15236 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15237 			/* Retries exhausted, fail the command */
15238 			SD_TRACE(SD_LOG_IO_CORE, un,
15239 			    "sd_retry_command: retries exhausted!\n");
15240 			goto fail_command;
15241 		}
15242 		xp->xb_retry_count++;
15243 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15244 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15245 		break;
15246 
15247 	case SD_RETRIES_NOCHECK:
15248 	default:
15249 		/* No retry count to check. Just proceed with the retry */
15250 		break;
15251 	}
15252 
15253 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15254 
15255 	/*
15256 	 * If we were given a zero timeout, we must attempt to retry the
15257 	 * command immediately (ie, without a delay).
15258 	 */
15259 	if (retry_delay == 0) {
15260 		/*
15261 		 * Check some limiting conditions to see if we can actually
15262 		 * do the immediate retry.  If we cannot, then we must
15263 		 * fall back to queueing up a delayed retry.
15264 		 */
15265 		if (un->un_ncmds_in_transport >= un->un_throttle) {
15266 			/*
15267 			 * We are at the throttle limit for the target,
15268 			 * fall back to delayed retry.
15269 			 */
15270 			retry_delay = SD_BSY_TIMEOUT;
15271 			statp = kstat_waitq_enter;
15272 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15273 			    "sd_retry_command: immed. retry hit "
15274 			    "throttle!\n");
15275 		} else {
15276 			/*
15277 			 * We're clear to proceed with the immediate retry.
15278 			 * First call the user-provided function (if any)
15279 			 */
15280 			if (user_funcp != NULL) {
15281 				(*user_funcp)(un, bp, user_arg,
15282 				    SD_IMMEDIATE_RETRY_ISSUED);
15283 #ifdef __lock_lint
15284 				sd_print_incomplete_msg(un, bp, user_arg,
15285 				    SD_IMMEDIATE_RETRY_ISSUED);
15286 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
15287 				    SD_IMMEDIATE_RETRY_ISSUED);
15288 				sd_print_sense_failed_msg(un, bp, user_arg,
15289 				    SD_IMMEDIATE_RETRY_ISSUED);
15290 #endif
15291 			}
15292 
15293 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15294 			    "sd_retry_command: issuing immediate retry\n");
15295 
15296 			/*
15297 			 * Call sd_start_cmds() to transport the command to
15298 			 * the target.
15299 			 */
15300 			sd_start_cmds(un, bp);
15301 
15302 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15303 			    "sd_retry_command exit\n");
15304 			return;
15305 		}
15306 	}
15307 
15308 	/*
15309 	 * Set up to retry the command after a delay.
15310 	 * First call the user-provided function (if any)
15311 	 */
15312 	if (user_funcp != NULL) {
15313 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15314 	}
15315 
15316 	sd_set_retry_bp(un, bp, retry_delay, statp);
15317 
15318 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15319 	return;
15320 
15321 fail_command:
15322 
15323 	if (user_funcp != NULL) {
15324 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15325 	}
15326 
15327 fail_command_no_log:
15328 
15329 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15330 	    "sd_retry_command: returning failed command\n");
15331 
15332 	sd_return_failed_command(un, bp, failure_code);
15333 
15334 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15335 }
15336 
15337 
15338 /*
15339  *    Function: sd_set_retry_bp
15340  *
15341  * Description: Set up the given bp for retry.
15342  *
15343  *   Arguments: un - ptr to associated softstate
15344  *		bp - ptr to buf(9S) for the command
15345  *		retry_delay - time interval before issuing retry (may be 0)
15346  *		statp - optional pointer to kstat function
15347  *
15348  *     Context: May be called under interrupt context
15349  */
15350 
15351 static void
15352 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15353 	void (*statp)(kstat_io_t *))
15354 {
15355 	ASSERT(un != NULL);
15356 	ASSERT(mutex_owned(SD_MUTEX(un)));
15357 	ASSERT(bp != NULL);
15358 
15359 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15360 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15361 
15362 	/*
15363 	 * Indicate that the command is being retried. This will not allow any
15364 	 * other commands on the wait queue to be transported to the target
15365 	 * until this command has been completed (success or failure). The
15366 	 * "retry command" is not transported to the target until the given
15367 	 * time delay expires, unless the user specified a 0 retry_delay.
15368 	 *
15369 	 * Note: the timeout(9F) callback routine is what actually calls
15370 	 * sd_start_cmds() to transport the command, with the exception of a
15371 	 * zero retry_delay. The only current implementor of a zero retry delay
15372 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
15373 	 */
15374 	if (un->un_retry_bp == NULL) {
15375 		ASSERT(un->un_retry_statp == NULL);
15376 		un->un_retry_bp = bp;
15377 
15378 		/*
15379 		 * If the user has not specified a delay the command should
15380 		 * be queued and no timeout should be scheduled.
15381 		 */
15382 		if (retry_delay == 0) {
15383 			/*
15384 			 * Save the kstat pointer that will be used in the
15385 			 * call to SD_UPDATE_KSTATS() below, so that
15386 			 * sd_start_cmds() can correctly decrement the waitq
15387 			 * count when it is time to transport this command.
15388 			 */
15389 			un->un_retry_statp = statp;
15390 			goto done;
15391 		}
15392 	}
15393 
15394 	if (un->un_retry_bp == bp) {
15395 		/*
15396 		 * Save the kstat pointer that will be used in the call to
15397 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
15398 		 * correctly decrement the waitq count when it is time to
15399 		 * transport this command.
15400 		 */
15401 		un->un_retry_statp = statp;
15402 
15403 		/*
15404 		 * Schedule a timeout if:
15405 		 *   1) The user has specified a delay.
15406 		 *   2) There is not a START_STOP_UNIT callback pending.
15407 		 *
15408 		 * If no delay has been specified, then it is up to the caller
15409 		 * to ensure that IO processing continues without stalling.
15410 		 * Effectively, this means that the caller will issue the
15411 		 * required call to sd_start_cmds(). The START_STOP_UNIT
15412 		 * callback does this after the START STOP UNIT command has
15413 		 * completed. In either of these cases we should not schedule
15414 		 * a timeout callback here.  Also don't schedule the timeout if
15415 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
15416 		 */
15417 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
15418 		    (un->un_direct_priority_timeid == NULL)) {
15419 			un->un_retry_timeid =
15420 			    timeout(sd_start_retry_command, un, retry_delay);
15421 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15422 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
15423 			    " bp:0x%p un_retry_timeid:0x%p\n",
15424 			    un, bp, un->un_retry_timeid);
15425 		}
15426 	} else {
15427 		/*
15428 		 * We only get in here if there is already another command
15429 		 * waiting to be retried.  In this case, we just put the
15430 		 * given command onto the wait queue, so it can be transported
15431 		 * after the current retry command has completed.
15432 		 *
15433 		 * Also we have to make sure that if the command at the head
15434 		 * of the wait queue is the un_failfast_bp, that we do not
15435 		 * put ahead of it any other commands that are to be retried.
15436 		 */
15437 		if ((un->un_failfast_bp != NULL) &&
15438 		    (un->un_failfast_bp == un->un_waitq_headp)) {
15439 			/*
15440 			 * Enqueue this command AFTER the first command on
15441 			 * the wait queue (which is also un_failfast_bp).
15442 			 */
15443 			bp->av_forw = un->un_waitq_headp->av_forw;
15444 			un->un_waitq_headp->av_forw = bp;
15445 			if (un->un_waitq_headp == un->un_waitq_tailp) {
15446 				un->un_waitq_tailp = bp;
15447 			}
15448 		} else {
15449 			/* Enqueue this command at the head of the waitq. */
15450 			bp->av_forw = un->un_waitq_headp;
15451 			un->un_waitq_headp = bp;
15452 			if (un->un_waitq_tailp == NULL) {
15453 				un->un_waitq_tailp = bp;
15454 			}
15455 		}
15456 
15457 		if (statp == NULL) {
15458 			statp = kstat_waitq_enter;
15459 		}
15460 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15461 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
15462 	}
15463 
15464 done:
15465 	if (statp != NULL) {
15466 		SD_UPDATE_KSTATS(un, statp, bp);
15467 	}
15468 
15469 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15470 	    "sd_set_retry_bp: exit un:0x%p\n", un);
15471 }
15472 
15473 
15474 /*
15475  *    Function: sd_start_retry_command
15476  *
15477  * Description: Start the command that has been waiting on the target's
15478  *		retry queue.  Called from timeout(9F) context after the
15479  *		retry delay interval has expired.
15480  *
15481  *   Arguments: arg - pointer to associated softstate for the device.
15482  *
15483  *     Context: timeout(9F) thread context.  May not sleep.
15484  */
15485 
15486 static void
15487 sd_start_retry_command(void *arg)
15488 {
15489 	struct sd_lun *un = arg;
15490 
15491 	ASSERT(un != NULL);
15492 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15493 
15494 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15495 	    "sd_start_retry_command: entry\n");
15496 
15497 	mutex_enter(SD_MUTEX(un));
15498 
15499 	un->un_retry_timeid = NULL;
15500 
15501 	if (un->un_retry_bp != NULL) {
15502 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15503 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
15504 		    un, un->un_retry_bp);
15505 		sd_start_cmds(un, un->un_retry_bp);
15506 	}
15507 
15508 	mutex_exit(SD_MUTEX(un));
15509 
15510 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15511 	    "sd_start_retry_command: exit\n");
15512 }
15513 
15514 
15515 /*
15516  *    Function: sd_start_direct_priority_command
15517  *
15518  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
15519  *		received TRAN_BUSY when we called scsi_transport() to send it
15520  *		to the underlying HBA. This function is called from timeout(9F)
15521  *		context after the delay interval has expired.
15522  *
15523  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
15524  *
15525  *     Context: timeout(9F) thread context.  May not sleep.
15526  */
15527 
15528 static void
15529 sd_start_direct_priority_command(void *arg)
15530 {
15531 	struct buf	*priority_bp = arg;
15532 	struct sd_lun	*un;
15533 
15534 	ASSERT(priority_bp != NULL);
15535 	un = SD_GET_UN(priority_bp);
15536 	ASSERT(un != NULL);
15537 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15538 
15539 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15540 	    "sd_start_direct_priority_command: entry\n");
15541 
15542 	mutex_enter(SD_MUTEX(un));
15543 	un->un_direct_priority_timeid = NULL;
15544 	sd_start_cmds(un, priority_bp);
15545 	mutex_exit(SD_MUTEX(un));
15546 
15547 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15548 	    "sd_start_direct_priority_command: exit\n");
15549 }
15550 
15551 
15552 /*
15553  *    Function: sd_send_request_sense_command
15554  *
15555  * Description: Sends a REQUEST SENSE command to the target
15556  *
15557  *     Context: May be called from interrupt context.
15558  */
15559 
15560 static void
15561 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
15562 	struct scsi_pkt *pktp)
15563 {
15564 	ASSERT(bp != NULL);
15565 	ASSERT(un != NULL);
15566 	ASSERT(mutex_owned(SD_MUTEX(un)));
15567 
15568 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
15569 	    "entry: buf:0x%p\n", bp);
15570 
15571 	/*
15572 	 * If we are syncing or dumping, then fail the command to avoid a
15573 	 * recursive callback into scsi_transport(). Also fail the command
15574 	 * if we are suspended (legacy behavior).
15575 	 */
15576 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
15577 	    (un->un_state == SD_STATE_DUMPING)) {
15578 		sd_return_failed_command(un, bp, EIO);
15579 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15580 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
15581 		return;
15582 	}
15583 
15584 	/*
15585 	 * Retry the failed command and don't issue the request sense if:
15586 	 *    1) the sense buf is busy
15587 	 *    2) we have 1 or more outstanding commands on the target
15588 	 *    (the sense data will be cleared or invalidated any way)
15589 	 *
15590 	 * Note: There could be an issue with not checking a retry limit here,
15591 	 * the problem is determining which retry limit to check.
15592 	 */
15593 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
15594 		/* Don't retry if the command is flagged as non-retryable */
15595 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
15596 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15597 			    NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter);
15598 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15599 			    "sd_send_request_sense_command: "
15600 			    "at full throttle, retrying exit\n");
15601 		} else {
15602 			sd_return_failed_command(un, bp, EIO);
15603 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15604 			    "sd_send_request_sense_command: "
15605 			    "at full throttle, non-retryable exit\n");
15606 		}
15607 		return;
15608 	}
15609 
15610 	sd_mark_rqs_busy(un, bp);
15611 	sd_start_cmds(un, un->un_rqs_bp);
15612 
15613 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15614 	    "sd_send_request_sense_command: exit\n");
15615 }
15616 
15617 
15618 /*
15619  *    Function: sd_mark_rqs_busy
15620  *
15621  * Description: Indicate that the request sense bp for this instance is
15622  *		in use.
15623  *
15624  *     Context: May be called under interrupt context
15625  */
15626 
15627 static void
15628 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
15629 {
15630 	struct sd_xbuf	*sense_xp;
15631 
15632 	ASSERT(un != NULL);
15633 	ASSERT(bp != NULL);
15634 	ASSERT(mutex_owned(SD_MUTEX(un)));
15635 	ASSERT(un->un_sense_isbusy == 0);
15636 
15637 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
15638 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
15639 
15640 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
15641 	ASSERT(sense_xp != NULL);
15642 
15643 	SD_INFO(SD_LOG_IO, un,
15644 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
15645 
15646 	ASSERT(sense_xp->xb_pktp != NULL);
15647 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
15648 	    == (FLAG_SENSING | FLAG_HEAD));
15649 
15650 	un->un_sense_isbusy = 1;
15651 	un->un_rqs_bp->b_resid = 0;
15652 	sense_xp->xb_pktp->pkt_resid  = 0;
15653 	sense_xp->xb_pktp->pkt_reason = 0;
15654 
15655 	/* So we can get back the bp at interrupt time! */
15656 	sense_xp->xb_sense_bp = bp;
15657 
15658 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
15659 
15660 	/*
15661 	 * Mark this buf as awaiting sense data. (This is already set in
15662 	 * the pkt_flags for the RQS packet.)
15663 	 */
15664 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
15665 
15666 	sense_xp->xb_retry_count	= 0;
15667 	sense_xp->xb_victim_retry_count = 0;
15668 	sense_xp->xb_ua_retry_count	= 0;
15669 	sense_xp->xb_dma_resid  = 0;
15670 
15671 	/* Clean up the fields for auto-request sense */
15672 	sense_xp->xb_sense_status = 0;
15673 	sense_xp->xb_sense_state  = 0;
15674 	sense_xp->xb_sense_resid  = 0;
15675 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
15676 
15677 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
15678 }
15679 
15680 
15681 /*
15682  *    Function: sd_mark_rqs_idle
15683  *
15684  * Description: SD_MUTEX must be held continuously through this routine
15685  *		to prevent reuse of the rqs struct before the caller can
15686  *		complete it's processing.
15687  *
15688  * Return Code: Pointer to the RQS buf
15689  *
15690  *     Context: May be called under interrupt context
15691  */
15692 
15693 static struct buf *
15694 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
15695 {
15696 	struct buf *bp;
15697 	ASSERT(un != NULL);
15698 	ASSERT(sense_xp != NULL);
15699 	ASSERT(mutex_owned(SD_MUTEX(un)));
15700 	ASSERT(un->un_sense_isbusy != 0);
15701 
15702 	un->un_sense_isbusy = 0;
15703 	bp = sense_xp->xb_sense_bp;
15704 	sense_xp->xb_sense_bp = NULL;
15705 
15706 	/* This pkt is no longer interested in getting sense data */
15707 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
15708 
15709 	return (bp);
15710 }
15711 
15712 
15713 
15714 /*
15715  *    Function: sd_alloc_rqs
15716  *
15717  * Description: Set up the unit to receive auto request sense data
15718  *
15719  * Return Code: DDI_SUCCESS or DDI_FAILURE
15720  *
15721  *     Context: Called under attach(9E) context
15722  */
15723 
15724 static int
15725 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
15726 {
15727 	struct sd_xbuf *xp;
15728 
15729 	ASSERT(un != NULL);
15730 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15731 	ASSERT(un->un_rqs_bp == NULL);
15732 	ASSERT(un->un_rqs_pktp == NULL);
15733 
15734 	/*
15735 	 * First allocate the required buf and scsi_pkt structs, then set up
15736 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
15737 	 */
15738 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
15739 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
15740 	if (un->un_rqs_bp == NULL) {
15741 		return (DDI_FAILURE);
15742 	}
15743 
15744 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
15745 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
15746 
15747 	if (un->un_rqs_pktp == NULL) {
15748 		sd_free_rqs(un);
15749 		return (DDI_FAILURE);
15750 	}
15751 
15752 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
15753 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
15754 	    SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
15755 
15756 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
15757 
15758 	/* Set up the other needed members in the ARQ scsi_pkt. */
15759 	un->un_rqs_pktp->pkt_comp   = sdintr;
15760 	un->un_rqs_pktp->pkt_time   = sd_io_time;
15761 	un->un_rqs_pktp->pkt_flags |=
15762 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
15763 
15764 	/*
15765 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
15766 	 * provide any intpkt, destroypkt routines as we take care of
15767 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
15768 	 */
15769 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
15770 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
15771 	xp->xb_pktp = un->un_rqs_pktp;
15772 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
15773 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
15774 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
15775 
15776 	/*
15777 	 * Save the pointer to the request sense private bp so it can
15778 	 * be retrieved in sdintr.
15779 	 */
15780 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
15781 	ASSERT(un->un_rqs_bp->b_private == xp);
15782 
15783 	/*
15784 	 * See if the HBA supports auto-request sense for the specified
15785 	 * target/lun. If it does, then try to enable it (if not already
15786 	 * enabled).
15787 	 *
15788 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
15789 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
15790 	 * return success.  However, in both of these cases ARQ is always
15791 	 * enabled and scsi_ifgetcap will always return true. The best approach
15792 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
15793 	 *
15794 	 * The 3rd case is the HBA (adp) always return enabled on
15795 	 * scsi_ifgetgetcap even when it's not enable, the best approach
15796 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
15797 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
15798 	 */
15799 
15800 	if (un->un_f_is_fibre == TRUE) {
15801 		un->un_f_arq_enabled = TRUE;
15802 	} else {
15803 #if defined(__i386) || defined(__amd64)
15804 		/*
15805 		 * Circumvent the Adaptec bug, remove this code when
15806 		 * the bug is fixed
15807 		 */
15808 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
15809 #endif
15810 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
15811 		case 0:
15812 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15813 				"sd_alloc_rqs: HBA supports ARQ\n");
15814 			/*
15815 			 * ARQ is supported by this HBA but currently is not
15816 			 * enabled. Attempt to enable it and if successful then
15817 			 * mark this instance as ARQ enabled.
15818 			 */
15819 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
15820 				== 1) {
15821 				/* Successfully enabled ARQ in the HBA */
15822 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
15823 					"sd_alloc_rqs: ARQ enabled\n");
15824 				un->un_f_arq_enabled = TRUE;
15825 			} else {
15826 				/* Could not enable ARQ in the HBA */
15827 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
15828 				"sd_alloc_rqs: failed ARQ enable\n");
15829 				un->un_f_arq_enabled = FALSE;
15830 			}
15831 			break;
15832 		case 1:
15833 			/*
15834 			 * ARQ is supported by this HBA and is already enabled.
15835 			 * Just mark ARQ as enabled for this instance.
15836 			 */
15837 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15838 				"sd_alloc_rqs: ARQ already enabled\n");
15839 			un->un_f_arq_enabled = TRUE;
15840 			break;
15841 		default:
15842 			/*
15843 			 * ARQ is not supported by this HBA; disable it for this
15844 			 * instance.
15845 			 */
15846 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15847 				"sd_alloc_rqs: HBA does not support ARQ\n");
15848 			un->un_f_arq_enabled = FALSE;
15849 			break;
15850 		}
15851 	}
15852 
15853 	return (DDI_SUCCESS);
15854 }
15855 
15856 
15857 /*
15858  *    Function: sd_free_rqs
15859  *
15860  * Description: Cleanup for the pre-instance RQS command.
15861  *
15862  *     Context: Kernel thread context
15863  */
15864 
15865 static void
15866 sd_free_rqs(struct sd_lun *un)
15867 {
15868 	ASSERT(un != NULL);
15869 
15870 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
15871 
15872 	/*
15873 	 * If consistent memory is bound to a scsi_pkt, the pkt
15874 	 * has to be destroyed *before* freeing the consistent memory.
15875 	 * Don't change the sequence of this operations.
15876 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
15877 	 * after it was freed in scsi_free_consistent_buf().
15878 	 */
15879 	if (un->un_rqs_pktp != NULL) {
15880 		scsi_destroy_pkt(un->un_rqs_pktp);
15881 		un->un_rqs_pktp = NULL;
15882 	}
15883 
15884 	if (un->un_rqs_bp != NULL) {
15885 		kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf));
15886 		scsi_free_consistent_buf(un->un_rqs_bp);
15887 		un->un_rqs_bp = NULL;
15888 	}
15889 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
15890 }
15891 
15892 
15893 
15894 /*
15895  *    Function: sd_reduce_throttle
15896  *
15897  * Description: Reduces the maximun # of outstanding commands on a
15898  *		target to the current number of outstanding commands.
15899  *		Queues a tiemout(9F) callback to restore the limit
15900  *		after a specified interval has elapsed.
15901  *		Typically used when we get a TRAN_BUSY return code
15902  *		back from scsi_transport().
15903  *
15904  *   Arguments: un - ptr to the sd_lun softstate struct
15905  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
15906  *
15907  *     Context: May be called from interrupt context
15908  */
15909 
15910 static void
15911 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
15912 {
15913 	ASSERT(un != NULL);
15914 	ASSERT(mutex_owned(SD_MUTEX(un)));
15915 	ASSERT(un->un_ncmds_in_transport >= 0);
15916 
15917 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
15918 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
15919 	    un, un->un_throttle, un->un_ncmds_in_transport);
15920 
15921 	if (un->un_throttle > 1) {
15922 		if (un->un_f_use_adaptive_throttle == TRUE) {
15923 			switch (throttle_type) {
15924 			case SD_THROTTLE_TRAN_BUSY:
15925 				if (un->un_busy_throttle == 0) {
15926 					un->un_busy_throttle = un->un_throttle;
15927 				}
15928 				break;
15929 			case SD_THROTTLE_QFULL:
15930 				un->un_busy_throttle = 0;
15931 				break;
15932 			default:
15933 				ASSERT(FALSE);
15934 			}
15935 
15936 			if (un->un_ncmds_in_transport > 0) {
15937 			    un->un_throttle = un->un_ncmds_in_transport;
15938 			}
15939 
15940 		} else {
15941 			if (un->un_ncmds_in_transport == 0) {
15942 				un->un_throttle = 1;
15943 			} else {
15944 				un->un_throttle = un->un_ncmds_in_transport;
15945 			}
15946 		}
15947 	}
15948 
15949 	/* Reschedule the timeout if none is currently active */
15950 	if (un->un_reset_throttle_timeid == NULL) {
15951 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
15952 		    un, SD_THROTTLE_RESET_INTERVAL);
15953 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15954 		    "sd_reduce_throttle: timeout scheduled!\n");
15955 	}
15956 
15957 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
15958 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
15959 }
15960 
15961 
15962 
15963 /*
15964  *    Function: sd_restore_throttle
15965  *
15966  * Description: Callback function for timeout(9F).  Resets the current
15967  *		value of un->un_throttle to its default.
15968  *
15969  *   Arguments: arg - pointer to associated softstate for the device.
15970  *
15971  *     Context: May be called from interrupt context
15972  */
15973 
15974 static void
15975 sd_restore_throttle(void *arg)
15976 {
15977 	struct sd_lun	*un = arg;
15978 
15979 	ASSERT(un != NULL);
15980 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15981 
15982 	mutex_enter(SD_MUTEX(un));
15983 
15984 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
15985 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
15986 
15987 	un->un_reset_throttle_timeid = NULL;
15988 
15989 	if (un->un_f_use_adaptive_throttle == TRUE) {
15990 		/*
15991 		 * If un_busy_throttle is nonzero, then it contains the
15992 		 * value that un_throttle was when we got a TRAN_BUSY back
15993 		 * from scsi_transport(). We want to revert back to this
15994 		 * value.
15995 		 *
15996 		 * In the QFULL case, the throttle limit will incrementally
15997 		 * increase until it reaches max throttle.
15998 		 */
15999 		if (un->un_busy_throttle > 0) {
16000 			un->un_throttle = un->un_busy_throttle;
16001 			un->un_busy_throttle = 0;
16002 		} else {
16003 			/*
16004 			 * increase throttle by 10% open gate slowly, schedule
16005 			 * another restore if saved throttle has not been
16006 			 * reached
16007 			 */
16008 			short throttle;
16009 			if (sd_qfull_throttle_enable) {
16010 				throttle = un->un_throttle +
16011 				    max((un->un_throttle / 10), 1);
16012 				un->un_throttle =
16013 				    (throttle < un->un_saved_throttle) ?
16014 				    throttle : un->un_saved_throttle;
16015 				if (un->un_throttle < un->un_saved_throttle) {
16016 				    un->un_reset_throttle_timeid =
16017 					timeout(sd_restore_throttle,
16018 					un, SD_QFULL_THROTTLE_RESET_INTERVAL);
16019 				}
16020 			}
16021 		}
16022 
16023 		/*
16024 		 * If un_throttle has fallen below the low-water mark, we
16025 		 * restore the maximum value here (and allow it to ratchet
16026 		 * down again if necessary).
16027 		 */
16028 		if (un->un_throttle < un->un_min_throttle) {
16029 			un->un_throttle = un->un_saved_throttle;
16030 		}
16031 	} else {
16032 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
16033 		    "restoring limit from 0x%x to 0x%x\n",
16034 		    un->un_throttle, un->un_saved_throttle);
16035 		un->un_throttle = un->un_saved_throttle;
16036 	}
16037 
16038 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16039 	    "sd_restore_throttle: calling sd_start_cmds!\n");
16040 
16041 	sd_start_cmds(un, NULL);
16042 
16043 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16044 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16045 	    un, un->un_throttle);
16046 
16047 	mutex_exit(SD_MUTEX(un));
16048 
16049 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16050 }
16051 
16052 /*
16053  *    Function: sdrunout
16054  *
16055  * Description: Callback routine for scsi_init_pkt when a resource allocation
16056  *		fails.
16057  *
16058  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16059  *		soft state instance.
16060  *
16061  * Return Code: The scsi_init_pkt routine allows for the callback function to
16062  *		return a 0 indicating the callback should be rescheduled or a 1
16063  *		indicating not to reschedule. This routine always returns 1
16064  *		because the driver always provides a callback function to
16065  *		scsi_init_pkt. This results in a callback always being scheduled
16066  *		(via the scsi_init_pkt callback implementation) if a resource
16067  *		failure occurs.
16068  *
16069  *     Context: This callback function may not block or call routines that block
16070  *
16071  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16072  *		request persisting at the head of the list which cannot be
16073  *		satisfied even after multiple retries. In the future the driver
16074  *		may implement some time of maximum runout count before failing
16075  *		an I/O.
16076  */
16077 
16078 static int
16079 sdrunout(caddr_t arg)
16080 {
16081 	struct sd_lun	*un = (struct sd_lun *)arg;
16082 
16083 	ASSERT(un != NULL);
16084 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16085 
16086 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16087 
16088 	mutex_enter(SD_MUTEX(un));
16089 	sd_start_cmds(un, NULL);
16090 	mutex_exit(SD_MUTEX(un));
16091 	/*
16092 	 * This callback routine always returns 1 (i.e. do not reschedule)
16093 	 * because we always specify sdrunout as the callback handler for
16094 	 * scsi_init_pkt inside the call to sd_start_cmds.
16095 	 */
16096 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16097 	return (1);
16098 }
16099 
16100 
16101 /*
16102  *    Function: sdintr
16103  *
16104  * Description: Completion callback routine for scsi_pkt(9S) structs
16105  *		sent to the HBA driver via scsi_transport(9F).
16106  *
16107  *     Context: Interrupt context
16108  */
16109 
16110 static void
16111 sdintr(struct scsi_pkt *pktp)
16112 {
16113 	struct buf	*bp;
16114 	struct sd_xbuf	*xp;
16115 	struct sd_lun	*un;
16116 
16117 	ASSERT(pktp != NULL);
16118 	bp = (struct buf *)pktp->pkt_private;
16119 	ASSERT(bp != NULL);
16120 	xp = SD_GET_XBUF(bp);
16121 	ASSERT(xp != NULL);
16122 	ASSERT(xp->xb_pktp != NULL);
16123 	un = SD_GET_UN(bp);
16124 	ASSERT(un != NULL);
16125 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16126 
16127 #ifdef SD_FAULT_INJECTION
16128 
16129 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16130 	/* SD FaultInjection */
16131 	sd_faultinjection(pktp);
16132 
16133 #endif /* SD_FAULT_INJECTION */
16134 
16135 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16136 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16137 
16138 	mutex_enter(SD_MUTEX(un));
16139 
16140 	/* Reduce the count of the #commands currently in transport */
16141 	un->un_ncmds_in_transport--;
16142 	ASSERT(un->un_ncmds_in_transport >= 0);
16143 
16144 	/* Increment counter to indicate that the callback routine is active */
16145 	un->un_in_callback++;
16146 
16147 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16148 
16149 #ifdef	SDDEBUG
16150 	if (bp == un->un_retry_bp) {
16151 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16152 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16153 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16154 	}
16155 #endif
16156 
16157 	/*
16158 	 * If pkt_reason is CMD_DEV_GONE, just fail the command
16159 	 */
16160 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16161 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16162 			    "Device is gone\n");
16163 		sd_return_failed_command(un, bp, EIO);
16164 		goto exit;
16165 	}
16166 
16167 	/*
16168 	 * First see if the pkt has auto-request sense data with it....
16169 	 * Look at the packet state first so we don't take a performance
16170 	 * hit looking at the arq enabled flag unless absolutely necessary.
16171 	 */
16172 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16173 	    (un->un_f_arq_enabled == TRUE)) {
16174 		/*
16175 		 * The HBA did an auto request sense for this command so check
16176 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16177 		 * driver command that should not be retried.
16178 		 */
16179 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16180 			/*
16181 			 * Save the relevant sense info into the xp for the
16182 			 * original cmd.
16183 			 */
16184 			struct scsi_arq_status *asp;
16185 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16186 			xp->xb_sense_status =
16187 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16188 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16189 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16190 			bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16191 			    min(sizeof (struct scsi_extended_sense),
16192 			    SENSE_LENGTH));
16193 
16194 			/* fail the command */
16195 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16196 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16197 			sd_return_failed_command(un, bp, EIO);
16198 			goto exit;
16199 		}
16200 
16201 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16202 		/*
16203 		 * We want to either retry or fail this command, so free
16204 		 * the DMA resources here.  If we retry the command then
16205 		 * the DMA resources will be reallocated in sd_start_cmds().
16206 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16207 		 * causes the *entire* transfer to start over again from the
16208 		 * beginning of the request, even for PARTIAL chunks that
16209 		 * have already transferred successfully.
16210 		 */
16211 		if ((un->un_f_is_fibre == TRUE) &&
16212 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16213 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16214 			scsi_dmafree(pktp);
16215 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16216 		}
16217 #endif
16218 
16219 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16220 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16221 
16222 		sd_handle_auto_request_sense(un, bp, xp, pktp);
16223 		goto exit;
16224 	}
16225 
16226 	/* Next see if this is the REQUEST SENSE pkt for the instance */
16227 	if (pktp->pkt_flags & FLAG_SENSING)  {
16228 		/* This pktp is from the unit's REQUEST_SENSE command */
16229 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16230 		    "sdintr: sd_handle_request_sense\n");
16231 		sd_handle_request_sense(un, bp, xp, pktp);
16232 		goto exit;
16233 	}
16234 
16235 	/*
16236 	 * Check to see if the command successfully completed as requested;
16237 	 * this is the most common case (and also the hot performance path).
16238 	 *
16239 	 * Requirements for successful completion are:
16240 	 * pkt_reason is CMD_CMPLT and packet status is status good.
16241 	 * In addition:
16242 	 * - A residual of zero indicates successful completion no matter what
16243 	 *   the command is.
16244 	 * - If the residual is not zero and the command is not a read or
16245 	 *   write, then it's still defined as successful completion. In other
16246 	 *   words, if the command is a read or write the residual must be
16247 	 *   zero for successful completion.
16248 	 * - If the residual is not zero and the command is a read or
16249 	 *   write, and it's a USCSICMD, then it's still defined as
16250 	 *   successful completion.
16251 	 */
16252 	if ((pktp->pkt_reason == CMD_CMPLT) &&
16253 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16254 
16255 		/*
16256 		 * Since this command is returned with a good status, we
16257 		 * can reset the count for Sonoma failover.
16258 		 */
16259 		un->un_sonoma_failure_count = 0;
16260 
16261 		/*
16262 		 * Return all USCSI commands on good status
16263 		 */
16264 		if (pktp->pkt_resid == 0) {
16265 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16266 			    "sdintr: returning command for resid == 0\n");
16267 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16268 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16269 			SD_UPDATE_B_RESID(bp, pktp);
16270 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16271 			    "sdintr: returning command for resid != 0\n");
16272 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16273 			SD_UPDATE_B_RESID(bp, pktp);
16274 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16275 				"sdintr: returning uscsi command\n");
16276 		} else {
16277 			goto not_successful;
16278 		}
16279 		sd_return_command(un, bp);
16280 
16281 		/*
16282 		 * Decrement counter to indicate that the callback routine
16283 		 * is done.
16284 		 */
16285 		un->un_in_callback--;
16286 		ASSERT(un->un_in_callback >= 0);
16287 		mutex_exit(SD_MUTEX(un));
16288 
16289 		return;
16290 	}
16291 
16292 not_successful:
16293 
16294 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16295 	/*
16296 	 * The following is based upon knowledge of the underlying transport
16297 	 * and its use of DMA resources.  This code should be removed when
16298 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16299 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16300 	 * and sd_start_cmds().
16301 	 *
16302 	 * Free any DMA resources associated with this command if there
16303 	 * is a chance it could be retried or enqueued for later retry.
16304 	 * If we keep the DMA binding then mpxio cannot reissue the
16305 	 * command on another path whenever a path failure occurs.
16306 	 *
16307 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16308 	 * causes the *entire* transfer to start over again from the
16309 	 * beginning of the request, even for PARTIAL chunks that
16310 	 * have already transferred successfully.
16311 	 *
16312 	 * This is only done for non-uscsi commands (and also skipped for the
16313 	 * driver's internal RQS command). Also just do this for Fibre Channel
16314 	 * devices as these are the only ones that support mpxio.
16315 	 */
16316 	if ((un->un_f_is_fibre == TRUE) &&
16317 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16318 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16319 		scsi_dmafree(pktp);
16320 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16321 	}
16322 #endif
16323 
16324 	/*
16325 	 * The command did not successfully complete as requested so check
16326 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16327 	 * driver command that should not be retried so just return. If
16328 	 * FLAG_DIAGNOSE is not set the error will be processed below.
16329 	 */
16330 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16331 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16332 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16333 		/*
16334 		 * Issue a request sense if a check condition caused the error
16335 		 * (we handle the auto request sense case above), otherwise
16336 		 * just fail the command.
16337 		 */
16338 		if ((pktp->pkt_reason == CMD_CMPLT) &&
16339 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16340 			sd_send_request_sense_command(un, bp, pktp);
16341 		} else {
16342 			sd_return_failed_command(un, bp, EIO);
16343 		}
16344 		goto exit;
16345 	}
16346 
16347 	/*
16348 	 * The command did not successfully complete as requested so process
16349 	 * the error, retry, and/or attempt recovery.
16350 	 */
16351 	switch (pktp->pkt_reason) {
16352 	case CMD_CMPLT:
16353 		switch (SD_GET_PKT_STATUS(pktp)) {
16354 		case STATUS_GOOD:
16355 			/*
16356 			 * The command completed successfully with a non-zero
16357 			 * residual
16358 			 */
16359 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16360 			    "sdintr: STATUS_GOOD \n");
16361 			sd_pkt_status_good(un, bp, xp, pktp);
16362 			break;
16363 
16364 		case STATUS_CHECK:
16365 		case STATUS_TERMINATED:
16366 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16367 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
16368 			sd_pkt_status_check_condition(un, bp, xp, pktp);
16369 			break;
16370 
16371 		case STATUS_BUSY:
16372 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16373 			    "sdintr: STATUS_BUSY\n");
16374 			sd_pkt_status_busy(un, bp, xp, pktp);
16375 			break;
16376 
16377 		case STATUS_RESERVATION_CONFLICT:
16378 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16379 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
16380 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16381 			break;
16382 
16383 		case STATUS_QFULL:
16384 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16385 			    "sdintr: STATUS_QFULL\n");
16386 			sd_pkt_status_qfull(un, bp, xp, pktp);
16387 			break;
16388 
16389 		case STATUS_MET:
16390 		case STATUS_INTERMEDIATE:
16391 		case STATUS_SCSI2:
16392 		case STATUS_INTERMEDIATE_MET:
16393 		case STATUS_ACA_ACTIVE:
16394 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16395 			    "Unexpected SCSI status received: 0x%x\n",
16396 			    SD_GET_PKT_STATUS(pktp));
16397 			sd_return_failed_command(un, bp, EIO);
16398 			break;
16399 
16400 		default:
16401 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16402 			    "Invalid SCSI status received: 0x%x\n",
16403 			    SD_GET_PKT_STATUS(pktp));
16404 			sd_return_failed_command(un, bp, EIO);
16405 			break;
16406 
16407 		}
16408 		break;
16409 
16410 	case CMD_INCOMPLETE:
16411 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16412 		    "sdintr:  CMD_INCOMPLETE\n");
16413 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
16414 		break;
16415 	case CMD_TRAN_ERR:
16416 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16417 		    "sdintr: CMD_TRAN_ERR\n");
16418 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
16419 		break;
16420 	case CMD_RESET:
16421 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16422 		    "sdintr: CMD_RESET \n");
16423 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
16424 		break;
16425 	case CMD_ABORTED:
16426 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16427 		    "sdintr: CMD_ABORTED \n");
16428 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
16429 		break;
16430 	case CMD_TIMEOUT:
16431 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16432 		    "sdintr: CMD_TIMEOUT\n");
16433 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
16434 		break;
16435 	case CMD_UNX_BUS_FREE:
16436 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16437 		    "sdintr: CMD_UNX_BUS_FREE \n");
16438 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
16439 		break;
16440 	case CMD_TAG_REJECT:
16441 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16442 		    "sdintr: CMD_TAG_REJECT\n");
16443 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
16444 		break;
16445 	default:
16446 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16447 		    "sdintr: default\n");
16448 		sd_pkt_reason_default(un, bp, xp, pktp);
16449 		break;
16450 	}
16451 
16452 exit:
16453 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
16454 
16455 	/* Decrement counter to indicate that the callback routine is done. */
16456 	un->un_in_callback--;
16457 	ASSERT(un->un_in_callback >= 0);
16458 
16459 	/*
16460 	 * At this point, the pkt has been dispatched, ie, it is either
16461 	 * being re-tried or has been returned to its caller and should
16462 	 * not be referenced.
16463 	 */
16464 
16465 	mutex_exit(SD_MUTEX(un));
16466 }
16467 
16468 
16469 /*
16470  *    Function: sd_print_incomplete_msg
16471  *
16472  * Description: Prints the error message for a CMD_INCOMPLETE error.
16473  *
16474  *   Arguments: un - ptr to associated softstate for the device.
16475  *		bp - ptr to the buf(9S) for the command.
16476  *		arg - message string ptr
16477  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
16478  *			or SD_NO_RETRY_ISSUED.
16479  *
16480  *     Context: May be called under interrupt context
16481  */
16482 
16483 static void
16484 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
16485 {
16486 	struct scsi_pkt	*pktp;
16487 	char	*msgp;
16488 	char	*cmdp = arg;
16489 
16490 	ASSERT(un != NULL);
16491 	ASSERT(mutex_owned(SD_MUTEX(un)));
16492 	ASSERT(bp != NULL);
16493 	ASSERT(arg != NULL);
16494 	pktp = SD_GET_PKTP(bp);
16495 	ASSERT(pktp != NULL);
16496 
16497 	switch (code) {
16498 	case SD_DELAYED_RETRY_ISSUED:
16499 	case SD_IMMEDIATE_RETRY_ISSUED:
16500 		msgp = "retrying";
16501 		break;
16502 	case SD_NO_RETRY_ISSUED:
16503 	default:
16504 		msgp = "giving up";
16505 		break;
16506 	}
16507 
16508 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
16509 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16510 		    "incomplete %s- %s\n", cmdp, msgp);
16511 	}
16512 }
16513 
16514 
16515 
16516 /*
16517  *    Function: sd_pkt_status_good
16518  *
16519  * Description: Processing for a STATUS_GOOD code in pkt_status.
16520  *
16521  *     Context: May be called under interrupt context
16522  */
16523 
16524 static void
16525 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
16526 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16527 {
16528 	char	*cmdp;
16529 
16530 	ASSERT(un != NULL);
16531 	ASSERT(mutex_owned(SD_MUTEX(un)));
16532 	ASSERT(bp != NULL);
16533 	ASSERT(xp != NULL);
16534 	ASSERT(pktp != NULL);
16535 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
16536 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
16537 	ASSERT(pktp->pkt_resid != 0);
16538 
16539 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
16540 
16541 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
16542 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
16543 	case SCMD_READ:
16544 		cmdp = "read";
16545 		break;
16546 	case SCMD_WRITE:
16547 		cmdp = "write";
16548 		break;
16549 	default:
16550 		SD_UPDATE_B_RESID(bp, pktp);
16551 		sd_return_command(un, bp);
16552 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16553 		return;
16554 	}
16555 
16556 	/*
16557 	 * See if we can retry the read/write, preferrably immediately.
16558 	 * If retries are exhaused, then sd_retry_command() will update
16559 	 * the b_resid count.
16560 	 */
16561 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
16562 	    cmdp, EIO, (clock_t)0, NULL);
16563 
16564 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16565 }
16566 
16567 
16568 
16569 
16570 
16571 /*
16572  *    Function: sd_handle_request_sense
16573  *
16574  * Description: Processing for non-auto Request Sense command.
16575  *
16576  *   Arguments: un - ptr to associated softstate
16577  *		sense_bp - ptr to buf(9S) for the RQS command
16578  *		sense_xp - ptr to the sd_xbuf for the RQS command
16579  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
16580  *
16581  *     Context: May be called under interrupt context
16582  */
16583 
16584 static void
16585 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
16586 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
16587 {
16588 	struct buf	*cmd_bp;	/* buf for the original command */
16589 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
16590 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
16591 
16592 	ASSERT(un != NULL);
16593 	ASSERT(mutex_owned(SD_MUTEX(un)));
16594 	ASSERT(sense_bp != NULL);
16595 	ASSERT(sense_xp != NULL);
16596 	ASSERT(sense_pktp != NULL);
16597 
16598 	/*
16599 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
16600 	 * RQS command and not the original command.
16601 	 */
16602 	ASSERT(sense_pktp == un->un_rqs_pktp);
16603 	ASSERT(sense_bp   == un->un_rqs_bp);
16604 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
16605 	    (FLAG_SENSING | FLAG_HEAD));
16606 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
16607 	    FLAG_SENSING) == FLAG_SENSING);
16608 
16609 	/* These are the bp, xp, and pktp for the original command */
16610 	cmd_bp = sense_xp->xb_sense_bp;
16611 	cmd_xp = SD_GET_XBUF(cmd_bp);
16612 	cmd_pktp = SD_GET_PKTP(cmd_bp);
16613 
16614 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
16615 		/*
16616 		 * The REQUEST SENSE command failed.  Release the REQUEST
16617 		 * SENSE command for re-use, get back the bp for the original
16618 		 * command, and attempt to re-try the original command if
16619 		 * FLAG_DIAGNOSE is not set in the original packet.
16620 		 */
16621 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
16622 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16623 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
16624 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
16625 			    NULL, NULL, EIO, (clock_t)0, NULL);
16626 			return;
16627 		}
16628 	}
16629 
16630 	/*
16631 	 * Save the relevant sense info into the xp for the original cmd.
16632 	 *
16633 	 * Note: if the request sense failed the state info will be zero
16634 	 * as set in sd_mark_rqs_busy()
16635 	 */
16636 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
16637 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
16638 	cmd_xp->xb_sense_resid  = sense_pktp->pkt_resid;
16639 	bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH);
16640 
16641 	/*
16642 	 *  Free up the RQS command....
16643 	 *  NOTE:
16644 	 *	Must do this BEFORE calling sd_validate_sense_data!
16645 	 *	sd_validate_sense_data may return the original command in
16646 	 *	which case the pkt will be freed and the flags can no
16647 	 *	longer be touched.
16648 	 *	SD_MUTEX is held through this process until the command
16649 	 *	is dispatched based upon the sense data, so there are
16650 	 *	no race conditions.
16651 	 */
16652 	(void) sd_mark_rqs_idle(un, sense_xp);
16653 
16654 	/*
16655 	 * For a retryable command see if we have valid sense data, if so then
16656 	 * turn it over to sd_decode_sense() to figure out the right course of
16657 	 * action. Just fail a non-retryable command.
16658 	 */
16659 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16660 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp) ==
16661 		    SD_SENSE_DATA_IS_VALID) {
16662 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
16663 		}
16664 	} else {
16665 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
16666 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
16667 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
16668 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
16669 		sd_return_failed_command(un, cmd_bp, EIO);
16670 	}
16671 }
16672 
16673 
16674 
16675 
16676 /*
16677  *    Function: sd_handle_auto_request_sense
16678  *
16679  * Description: Processing for auto-request sense information.
16680  *
16681  *   Arguments: un - ptr to associated softstate
16682  *		bp - ptr to buf(9S) for the command
16683  *		xp - ptr to the sd_xbuf for the command
16684  *		pktp - ptr to the scsi_pkt(9S) for the command
16685  *
16686  *     Context: May be called under interrupt context
16687  */
16688 
16689 static void
16690 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
16691 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16692 {
16693 	struct scsi_arq_status *asp;
16694 
16695 	ASSERT(un != NULL);
16696 	ASSERT(mutex_owned(SD_MUTEX(un)));
16697 	ASSERT(bp != NULL);
16698 	ASSERT(xp != NULL);
16699 	ASSERT(pktp != NULL);
16700 	ASSERT(pktp != un->un_rqs_pktp);
16701 	ASSERT(bp   != un->un_rqs_bp);
16702 
16703 	/*
16704 	 * For auto-request sense, we get a scsi_arq_status back from
16705 	 * the HBA, with the sense data in the sts_sensedata member.
16706 	 * The pkt_scbp of the packet points to this scsi_arq_status.
16707 	 */
16708 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16709 
16710 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
16711 		/*
16712 		 * The auto REQUEST SENSE failed; see if we can re-try
16713 		 * the original command.
16714 		 */
16715 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16716 		    "auto request sense failed (reason=%s)\n",
16717 		    scsi_rname(asp->sts_rqpkt_reason));
16718 
16719 		sd_reset_target(un, pktp);
16720 
16721 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16722 		    NULL, NULL, EIO, (clock_t)0, NULL);
16723 		return;
16724 	}
16725 
16726 	/* Save the relevant sense info into the xp for the original cmd. */
16727 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
16728 	xp->xb_sense_state  = asp->sts_rqpkt_state;
16729 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16730 	bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16731 	    min(sizeof (struct scsi_extended_sense), SENSE_LENGTH));
16732 
16733 	/*
16734 	 * See if we have valid sense data, if so then turn it over to
16735 	 * sd_decode_sense() to figure out the right course of action.
16736 	 */
16737 	if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) {
16738 		sd_decode_sense(un, bp, xp, pktp);
16739 	}
16740 }
16741 
16742 
16743 /*
16744  *    Function: sd_print_sense_failed_msg
16745  *
16746  * Description: Print log message when RQS has failed.
16747  *
16748  *   Arguments: un - ptr to associated softstate
16749  *		bp - ptr to buf(9S) for the command
16750  *		arg - generic message string ptr
16751  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
16752  *			or SD_NO_RETRY_ISSUED
16753  *
16754  *     Context: May be called from interrupt context
16755  */
16756 
16757 static void
16758 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
16759 	int code)
16760 {
16761 	char	*msgp = arg;
16762 
16763 	ASSERT(un != NULL);
16764 	ASSERT(mutex_owned(SD_MUTEX(un)));
16765 	ASSERT(bp != NULL);
16766 
16767 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
16768 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
16769 	}
16770 }
16771 
16772 
16773 /*
16774  *    Function: sd_validate_sense_data
16775  *
16776  * Description: Check the given sense data for validity.
16777  *		If the sense data is not valid, the command will
16778  *		be either failed or retried!
16779  *
16780  * Return Code: SD_SENSE_DATA_IS_INVALID
16781  *		SD_SENSE_DATA_IS_VALID
16782  *
16783  *     Context: May be called from interrupt context
16784  */
16785 
16786 static int
16787 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp)
16788 {
16789 	struct scsi_extended_sense *esp;
16790 	struct	scsi_pkt *pktp;
16791 	size_t	actual_len;
16792 	char	*msgp = NULL;
16793 
16794 	ASSERT(un != NULL);
16795 	ASSERT(mutex_owned(SD_MUTEX(un)));
16796 	ASSERT(bp != NULL);
16797 	ASSERT(bp != un->un_rqs_bp);
16798 	ASSERT(xp != NULL);
16799 
16800 	pktp = SD_GET_PKTP(bp);
16801 	ASSERT(pktp != NULL);
16802 
16803 	/*
16804 	 * Check the status of the RQS command (auto or manual).
16805 	 */
16806 	switch (xp->xb_sense_status & STATUS_MASK) {
16807 	case STATUS_GOOD:
16808 		break;
16809 
16810 	case STATUS_RESERVATION_CONFLICT:
16811 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16812 		return (SD_SENSE_DATA_IS_INVALID);
16813 
16814 	case STATUS_BUSY:
16815 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16816 		    "Busy Status on REQUEST SENSE\n");
16817 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
16818 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
16819 		return (SD_SENSE_DATA_IS_INVALID);
16820 
16821 	case STATUS_QFULL:
16822 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16823 		    "QFULL Status on REQUEST SENSE\n");
16824 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
16825 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
16826 		return (SD_SENSE_DATA_IS_INVALID);
16827 
16828 	case STATUS_CHECK:
16829 	case STATUS_TERMINATED:
16830 		msgp = "Check Condition on REQUEST SENSE\n";
16831 		goto sense_failed;
16832 
16833 	default:
16834 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
16835 		goto sense_failed;
16836 	}
16837 
16838 	/*
16839 	 * See if we got the minimum required amount of sense data.
16840 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
16841 	 * or less.
16842 	 */
16843 	actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid);
16844 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
16845 	    (actual_len == 0)) {
16846 		msgp = "Request Sense couldn't get sense data\n";
16847 		goto sense_failed;
16848 	}
16849 
16850 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
16851 		msgp = "Not enough sense information\n";
16852 		goto sense_failed;
16853 	}
16854 
16855 	/*
16856 	 * We require the extended sense data
16857 	 */
16858 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
16859 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
16860 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
16861 			static char tmp[8];
16862 			static char buf[148];
16863 			char *p = (char *)(xp->xb_sense_data);
16864 			int i;
16865 
16866 			mutex_enter(&sd_sense_mutex);
16867 			(void) strcpy(buf, "undecodable sense information:");
16868 			for (i = 0; i < actual_len; i++) {
16869 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
16870 				(void) strcpy(&buf[strlen(buf)], tmp);
16871 			}
16872 			i = strlen(buf);
16873 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
16874 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf);
16875 			mutex_exit(&sd_sense_mutex);
16876 		}
16877 		/* Note: Legacy behavior, fail the command with no retry */
16878 		sd_return_failed_command(un, bp, EIO);
16879 		return (SD_SENSE_DATA_IS_INVALID);
16880 	}
16881 
16882 	/*
16883 	 * Check that es_code is valid (es_class concatenated with es_code
16884 	 * make up the "response code" field.  es_class will always be 7, so
16885 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
16886 	 * format.
16887 	 */
16888 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
16889 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
16890 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
16891 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
16892 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
16893 		goto sense_failed;
16894 	}
16895 
16896 	return (SD_SENSE_DATA_IS_VALID);
16897 
16898 sense_failed:
16899 	/*
16900 	 * If the request sense failed (for whatever reason), attempt
16901 	 * to retry the original command.
16902 	 */
16903 #if defined(__i386) || defined(__amd64)
16904 	/*
16905 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
16906 	 * sddef.h for Sparc platform, and x86 uses 1 binary
16907 	 * for both SCSI/FC.
16908 	 * The SD_RETRY_DELAY value need to be adjusted here
16909 	 * when SD_RETRY_DELAY change in sddef.h
16910 	 */
16911 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16912 	    sd_print_sense_failed_msg, msgp, EIO,
16913 		un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
16914 #else
16915 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16916 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
16917 #endif
16918 
16919 	return (SD_SENSE_DATA_IS_INVALID);
16920 }
16921 
16922 
16923 
16924 /*
16925  *    Function: sd_decode_sense
16926  *
16927  * Description: Take recovery action(s) when SCSI Sense Data is received.
16928  *
16929  *     Context: Interrupt context.
16930  */
16931 
16932 static void
16933 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
16934 	struct scsi_pkt *pktp)
16935 {
16936 	struct scsi_extended_sense *esp;
16937 	struct scsi_descr_sense_hdr *sdsp;
16938 	uint8_t asc, ascq, sense_key;
16939 
16940 	ASSERT(un != NULL);
16941 	ASSERT(mutex_owned(SD_MUTEX(un)));
16942 	ASSERT(bp != NULL);
16943 	ASSERT(bp != un->un_rqs_bp);
16944 	ASSERT(xp != NULL);
16945 	ASSERT(pktp != NULL);
16946 
16947 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
16948 
16949 	switch (esp->es_code) {
16950 	case CODE_FMT_DESCR_CURRENT:
16951 	case CODE_FMT_DESCR_DEFERRED:
16952 		sdsp = (struct scsi_descr_sense_hdr *)xp->xb_sense_data;
16953 		sense_key = sdsp->ds_key;
16954 		asc = sdsp->ds_add_code;
16955 		ascq = sdsp->ds_qual_code;
16956 		break;
16957 	case CODE_FMT_VENDOR_SPECIFIC:
16958 	case CODE_FMT_FIXED_CURRENT:
16959 	case CODE_FMT_FIXED_DEFERRED:
16960 	default:
16961 		sense_key = esp->es_key;
16962 		asc = esp->es_add_code;
16963 		ascq = esp->es_qual_code;
16964 		break;
16965 	}
16966 
16967 	switch (sense_key) {
16968 	case KEY_NO_SENSE:
16969 		sd_sense_key_no_sense(un, bp, xp, pktp);
16970 		break;
16971 	case KEY_RECOVERABLE_ERROR:
16972 		sd_sense_key_recoverable_error(un, asc, bp, xp, pktp);
16973 		break;
16974 	case KEY_NOT_READY:
16975 		sd_sense_key_not_ready(un, asc, ascq, bp, xp, pktp);
16976 		break;
16977 	case KEY_MEDIUM_ERROR:
16978 	case KEY_HARDWARE_ERROR:
16979 		sd_sense_key_medium_or_hardware_error(un,
16980 		    sense_key, asc, bp, xp, pktp);
16981 		break;
16982 	case KEY_ILLEGAL_REQUEST:
16983 		sd_sense_key_illegal_request(un, bp, xp, pktp);
16984 		break;
16985 	case KEY_UNIT_ATTENTION:
16986 		sd_sense_key_unit_attention(un, asc, bp, xp, pktp);
16987 		break;
16988 	case KEY_WRITE_PROTECT:
16989 	case KEY_VOLUME_OVERFLOW:
16990 	case KEY_MISCOMPARE:
16991 		sd_sense_key_fail_command(un, bp, xp, pktp);
16992 		break;
16993 	case KEY_BLANK_CHECK:
16994 		sd_sense_key_blank_check(un, bp, xp, pktp);
16995 		break;
16996 	case KEY_ABORTED_COMMAND:
16997 		sd_sense_key_aborted_command(un, bp, xp, pktp);
16998 		break;
16999 	case KEY_VENDOR_UNIQUE:
17000 	case KEY_COPY_ABORTED:
17001 	case KEY_EQUAL:
17002 	case KEY_RESERVED:
17003 	default:
17004 		sd_sense_key_default(un, sense_key, bp, xp, pktp);
17005 		break;
17006 	}
17007 }
17008 
17009 
17010 /*
17011  *    Function: sd_dump_memory
17012  *
17013  * Description: Debug logging routine to print the contents of a user provided
17014  *		buffer. The output of the buffer is broken up into 256 byte
17015  *		segments due to a size constraint of the scsi_log.
17016  *		implementation.
17017  *
17018  *   Arguments: un - ptr to softstate
17019  *		comp - component mask
17020  *		title - "title" string to preceed data when printed
17021  *		data - ptr to data block to be printed
17022  *		len - size of data block to be printed
17023  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
17024  *
17025  *     Context: May be called from interrupt context
17026  */
17027 
17028 #define	SD_DUMP_MEMORY_BUF_SIZE	256
17029 
17030 static char *sd_dump_format_string[] = {
17031 		" 0x%02x",
17032 		" %c"
17033 };
17034 
17035 static void
17036 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
17037     int len, int fmt)
17038 {
17039 	int	i, j;
17040 	int	avail_count;
17041 	int	start_offset;
17042 	int	end_offset;
17043 	size_t	entry_len;
17044 	char	*bufp;
17045 	char	*local_buf;
17046 	char	*format_string;
17047 
17048 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17049 
17050 	/*
17051 	 * In the debug version of the driver, this function is called from a
17052 	 * number of places which are NOPs in the release driver.
17053 	 * The debug driver therefore has additional methods of filtering
17054 	 * debug output.
17055 	 */
17056 #ifdef SDDEBUG
17057 	/*
17058 	 * In the debug version of the driver we can reduce the amount of debug
17059 	 * messages by setting sd_error_level to something other than
17060 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17061 	 * sd_component_mask.
17062 	 */
17063 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17064 	    (sd_error_level != SCSI_ERR_ALL)) {
17065 		return;
17066 	}
17067 	if (((sd_component_mask & comp) == 0) ||
17068 	    (sd_error_level != SCSI_ERR_ALL)) {
17069 		return;
17070 	}
17071 #else
17072 	if (sd_error_level != SCSI_ERR_ALL) {
17073 		return;
17074 	}
17075 #endif
17076 
17077 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17078 	bufp = local_buf;
17079 	/*
17080 	 * Available length is the length of local_buf[], minus the
17081 	 * length of the title string, minus one for the ":", minus
17082 	 * one for the newline, minus one for the NULL terminator.
17083 	 * This gives the #bytes available for holding the printed
17084 	 * values from the given data buffer.
17085 	 */
17086 	if (fmt == SD_LOG_HEX) {
17087 		format_string = sd_dump_format_string[0];
17088 	} else /* SD_LOG_CHAR */ {
17089 		format_string = sd_dump_format_string[1];
17090 	}
17091 	/*
17092 	 * Available count is the number of elements from the given
17093 	 * data buffer that we can fit into the available length.
17094 	 * This is based upon the size of the format string used.
17095 	 * Make one entry and find it's size.
17096 	 */
17097 	(void) sprintf(bufp, format_string, data[0]);
17098 	entry_len = strlen(bufp);
17099 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17100 
17101 	j = 0;
17102 	while (j < len) {
17103 		bufp = local_buf;
17104 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17105 		start_offset = j;
17106 
17107 		end_offset = start_offset + avail_count;
17108 
17109 		(void) sprintf(bufp, "%s:", title);
17110 		bufp += strlen(bufp);
17111 		for (i = start_offset; ((i < end_offset) && (j < len));
17112 		    i++, j++) {
17113 			(void) sprintf(bufp, format_string, data[i]);
17114 			bufp += entry_len;
17115 		}
17116 		(void) sprintf(bufp, "\n");
17117 
17118 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17119 	}
17120 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17121 }
17122 
17123 /*
17124  *    Function: sd_print_sense_msg
17125  *
17126  * Description: Log a message based upon the given sense data.
17127  *
17128  *   Arguments: un - ptr to associated softstate
17129  *		bp - ptr to buf(9S) for the command
17130  *		arg - ptr to associate sd_sense_info struct
17131  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17132  *			or SD_NO_RETRY_ISSUED
17133  *
17134  *     Context: May be called from interrupt context
17135  */
17136 
17137 static void
17138 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17139 {
17140 	struct sd_xbuf	*xp;
17141 	struct scsi_pkt	*pktp;
17142 	struct scsi_extended_sense *sensep;
17143 	daddr_t request_blkno;
17144 	diskaddr_t err_blkno;
17145 	int severity;
17146 	int pfa_flag;
17147 	int fixed_format = TRUE;
17148 	extern struct scsi_key_strings scsi_cmds[];
17149 
17150 	ASSERT(un != NULL);
17151 	ASSERT(mutex_owned(SD_MUTEX(un)));
17152 	ASSERT(bp != NULL);
17153 	xp = SD_GET_XBUF(bp);
17154 	ASSERT(xp != NULL);
17155 	pktp = SD_GET_PKTP(bp);
17156 	ASSERT(pktp != NULL);
17157 	ASSERT(arg != NULL);
17158 
17159 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17160 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17161 
17162 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
17163 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17164 		severity = SCSI_ERR_RETRYABLE;
17165 	}
17166 
17167 	/* Use absolute block number for the request block number */
17168 	request_blkno = xp->xb_blkno;
17169 
17170 	/*
17171 	 * Now try to get the error block number from the sense data
17172 	 */
17173 	sensep = (struct scsi_extended_sense *)xp->xb_sense_data;
17174 	switch (sensep->es_code) {
17175 	case CODE_FMT_DESCR_CURRENT:
17176 	case CODE_FMT_DESCR_DEFERRED:
17177 		err_blkno =
17178 		    sd_extract_sense_info_descr(
17179 			(struct scsi_descr_sense_hdr *)sensep);
17180 		fixed_format = FALSE;
17181 		break;
17182 	case CODE_FMT_FIXED_CURRENT:
17183 	case CODE_FMT_FIXED_DEFERRED:
17184 	case CODE_FMT_VENDOR_SPECIFIC:
17185 	default:
17186 		/*
17187 		 * With the es_valid bit set, we assume that the error
17188 		 * blkno is in the sense data.  Also, if xp->xb_blkno is
17189 		 * greater than 0xffffffff then the target *should* have used
17190 		 * a descriptor sense format (or it shouldn't have set
17191 		 * the es_valid bit), and we may as well ignore the
17192 		 * 32-bit value.
17193 		 */
17194 		if ((sensep->es_valid != 0) && (xp->xb_blkno <= 0xffffffff)) {
17195 			err_blkno = (diskaddr_t)
17196 			    ((sensep->es_info_1 << 24) |
17197 			    (sensep->es_info_2 << 16) |
17198 			    (sensep->es_info_3 << 8)  |
17199 			    (sensep->es_info_4));
17200 		} else {
17201 			err_blkno = (diskaddr_t)-1;
17202 		}
17203 		break;
17204 	}
17205 
17206 	if (err_blkno == (diskaddr_t)-1) {
17207 		/*
17208 		 * Without the es_valid bit set (for fixed format) or an
17209 		 * information descriptor (for descriptor format) we cannot
17210 		 * be certain of the error blkno, so just use the
17211 		 * request_blkno.
17212 		 */
17213 		err_blkno = (diskaddr_t)request_blkno;
17214 	} else {
17215 		/*
17216 		 * We retrieved the error block number from the information
17217 		 * portion of the sense data.
17218 		 *
17219 		 * For USCSI commands we are better off using the error
17220 		 * block no. as the requested block no. (This is the best
17221 		 * we can estimate.)
17222 		 */
17223 		if ((SD_IS_BUFIO(xp) == FALSE) &&
17224 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17225 			request_blkno = err_blkno;
17226 		}
17227 	}
17228 
17229 	/*
17230 	 * The following will log the buffer contents for the release driver
17231 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17232 	 * level is set to verbose.
17233 	 */
17234 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17235 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17236 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17237 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17238 
17239 	if (pfa_flag == FALSE) {
17240 		/* This is normally only set for USCSI */
17241 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17242 			return;
17243 		}
17244 
17245 		if ((SD_IS_BUFIO(xp) == TRUE) &&
17246 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
17247 		    (severity < sd_error_level))) {
17248 			return;
17249 		}
17250 	}
17251 
17252 	/*
17253 	 * If the data is fixed format then check for Sonoma Failover,
17254 	 * and keep a count of how many failed I/O's.  We should not have
17255 	 * to worry about Sonoma returning descriptor format sense data,
17256 	 * and asc/ascq are in a different location in descriptor format.
17257 	 */
17258 	if (fixed_format &&
17259 	    (SD_IS_LSI(un)) && (sensep->es_key == KEY_ILLEGAL_REQUEST) &&
17260 	    (sensep->es_add_code == 0x94) && (sensep->es_qual_code == 0x01)) {
17261 		un->un_sonoma_failure_count++;
17262 		if (un->un_sonoma_failure_count > 1) {
17263 			return;
17264 		}
17265 	}
17266 
17267 	scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
17268 	    request_blkno, err_blkno, scsi_cmds, sensep,
17269 	    un->un_additional_codes, NULL);
17270 }
17271 
17272 /*
17273  *    Function: sd_extract_sense_info_descr
17274  *
17275  * Description: Retrieve "information" field from descriptor format
17276  *              sense data.  Iterates through each sense descriptor
17277  *              looking for the information descriptor and returns
17278  *              the information field from that descriptor.
17279  *
17280  *     Context: May be called from interrupt context
17281  */
17282 
17283 static diskaddr_t
17284 sd_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp)
17285 {
17286 	diskaddr_t result;
17287 	uint8_t *descr_offset;
17288 	int valid_sense_length;
17289 	struct scsi_information_sense_descr *isd;
17290 
17291 	/*
17292 	 * Initialize result to -1 indicating there is no information
17293 	 * descriptor
17294 	 */
17295 	result = (diskaddr_t)-1;
17296 
17297 	/*
17298 	 * The first descriptor will immediately follow the header
17299 	 */
17300 	descr_offset = (uint8_t *)(sdsp+1); /* Pointer arithmetic */
17301 
17302 	/*
17303 	 * Calculate the amount of valid sense data
17304 	 */
17305 	valid_sense_length =
17306 	    min((sizeof (struct scsi_descr_sense_hdr) +
17307 	    sdsp->ds_addl_sense_length),
17308 	    SENSE_LENGTH);
17309 
17310 	/*
17311 	 * Iterate through the list of descriptors, stopping when we
17312 	 * run out of sense data
17313 	 */
17314 	while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <=
17315 	    (uint8_t *)sdsp + valid_sense_length) {
17316 		/*
17317 		 * Check if this is an information descriptor.  We can
17318 		 * use the scsi_information_sense_descr structure as a
17319 		 * template sense the first two fields are always the
17320 		 * same
17321 		 */
17322 		isd = (struct scsi_information_sense_descr *)descr_offset;
17323 		if (isd->isd_descr_type == DESCR_INFORMATION) {
17324 			/*
17325 			 * Found an information descriptor.  Copy the
17326 			 * information field.  There will only be one
17327 			 * information descriptor so we can stop looking.
17328 			 */
17329 			result =
17330 			    (((diskaddr_t)isd->isd_information[0] << 56) |
17331 				((diskaddr_t)isd->isd_information[1] << 48) |
17332 				((diskaddr_t)isd->isd_information[2] << 40) |
17333 				((diskaddr_t)isd->isd_information[3] << 32) |
17334 				((diskaddr_t)isd->isd_information[4] << 24) |
17335 				((diskaddr_t)isd->isd_information[5] << 16) |
17336 				((diskaddr_t)isd->isd_information[6] << 8)  |
17337 				((diskaddr_t)isd->isd_information[7]));
17338 			break;
17339 		}
17340 
17341 		/*
17342 		 * Get pointer to the next descriptor.  The "additional
17343 		 * length" field holds the length of the descriptor except
17344 		 * for the "type" and "additional length" fields, so
17345 		 * we need to add 2 to get the total length.
17346 		 */
17347 		descr_offset += (isd->isd_addl_length + 2);
17348 	}
17349 
17350 	return (result);
17351 }
17352 
17353 /*
17354  *    Function: sd_sense_key_no_sense
17355  *
17356  * Description: Recovery action when sense data was not received.
17357  *
17358  *     Context: May be called from interrupt context
17359  */
17360 
17361 static void
17362 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
17363 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17364 {
17365 	struct sd_sense_info	si;
17366 
17367 	ASSERT(un != NULL);
17368 	ASSERT(mutex_owned(SD_MUTEX(un)));
17369 	ASSERT(bp != NULL);
17370 	ASSERT(xp != NULL);
17371 	ASSERT(pktp != NULL);
17372 
17373 	si.ssi_severity = SCSI_ERR_FATAL;
17374 	si.ssi_pfa_flag = FALSE;
17375 
17376 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17377 
17378 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17379 		&si, EIO, (clock_t)0, NULL);
17380 }
17381 
17382 
17383 /*
17384  *    Function: sd_sense_key_recoverable_error
17385  *
17386  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
17387  *
17388  *     Context: May be called from interrupt context
17389  */
17390 
17391 static void
17392 sd_sense_key_recoverable_error(struct sd_lun *un,
17393 	uint8_t asc,
17394 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17395 {
17396 	struct sd_sense_info	si;
17397 
17398 	ASSERT(un != NULL);
17399 	ASSERT(mutex_owned(SD_MUTEX(un)));
17400 	ASSERT(bp != NULL);
17401 	ASSERT(xp != NULL);
17402 	ASSERT(pktp != NULL);
17403 
17404 	/*
17405 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17406 	 */
17407 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
17408 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17409 		si.ssi_severity = SCSI_ERR_INFO;
17410 		si.ssi_pfa_flag = TRUE;
17411 	} else {
17412 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
17413 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
17414 		si.ssi_severity = SCSI_ERR_RECOVERED;
17415 		si.ssi_pfa_flag = FALSE;
17416 	}
17417 
17418 	if (pktp->pkt_resid == 0) {
17419 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17420 		sd_return_command(un, bp);
17421 		return;
17422 	}
17423 
17424 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17425 	    &si, EIO, (clock_t)0, NULL);
17426 }
17427 
17428 
17429 
17430 
17431 /*
17432  *    Function: sd_sense_key_not_ready
17433  *
17434  * Description: Recovery actions for a SCSI "Not Ready" sense key.
17435  *
17436  *     Context: May be called from interrupt context
17437  */
17438 
17439 static void
17440 sd_sense_key_not_ready(struct sd_lun *un,
17441 	uint8_t asc, uint8_t ascq,
17442 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17443 {
17444 	struct sd_sense_info	si;
17445 
17446 	ASSERT(un != NULL);
17447 	ASSERT(mutex_owned(SD_MUTEX(un)));
17448 	ASSERT(bp != NULL);
17449 	ASSERT(xp != NULL);
17450 	ASSERT(pktp != NULL);
17451 
17452 	si.ssi_severity = SCSI_ERR_FATAL;
17453 	si.ssi_pfa_flag = FALSE;
17454 
17455 	/*
17456 	 * Update error stats after first NOT READY error. Disks may have
17457 	 * been powered down and may need to be restarted.  For CDROMs,
17458 	 * report NOT READY errors only if media is present.
17459 	 */
17460 	if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) ||
17461 	    (xp->xb_retry_count > 0)) {
17462 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17463 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
17464 	}
17465 
17466 	/*
17467 	 * Just fail if the "not ready" retry limit has been reached.
17468 	 */
17469 	if (xp->xb_retry_count >= un->un_notready_retry_count) {
17470 		/* Special check for error message printing for removables. */
17471 		if (un->un_f_has_removable_media && (asc == 0x04) &&
17472 		    (ascq >= 0x04)) {
17473 			si.ssi_severity = SCSI_ERR_ALL;
17474 		}
17475 		goto fail_command;
17476 	}
17477 
17478 	/*
17479 	 * Check the ASC and ASCQ in the sense data as needed, to determine
17480 	 * what to do.
17481 	 */
17482 	switch (asc) {
17483 	case 0x04:	/* LOGICAL UNIT NOT READY */
17484 		/*
17485 		 * disk drives that don't spin up result in a very long delay
17486 		 * in format without warning messages. We will log a message
17487 		 * if the error level is set to verbose.
17488 		 */
17489 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17490 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17491 			    "logical unit not ready, resetting disk\n");
17492 		}
17493 
17494 		/*
17495 		 * There are different requirements for CDROMs and disks for
17496 		 * the number of retries.  If a CD-ROM is giving this, it is
17497 		 * probably reading TOC and is in the process of getting
17498 		 * ready, so we should keep on trying for a long time to make
17499 		 * sure that all types of media are taken in account (for
17500 		 * some media the drive takes a long time to read TOC).  For
17501 		 * disks we do not want to retry this too many times as this
17502 		 * can cause a long hang in format when the drive refuses to
17503 		 * spin up (a very common failure).
17504 		 */
17505 		switch (ascq) {
17506 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
17507 			/*
17508 			 * Disk drives frequently refuse to spin up which
17509 			 * results in a very long hang in format without
17510 			 * warning messages.
17511 			 *
17512 			 * Note: This code preserves the legacy behavior of
17513 			 * comparing xb_retry_count against zero for fibre
17514 			 * channel targets instead of comparing against the
17515 			 * un_reset_retry_count value.  The reason for this
17516 			 * discrepancy has been so utterly lost beneath the
17517 			 * Sands of Time that even Indiana Jones could not
17518 			 * find it.
17519 			 */
17520 			if (un->un_f_is_fibre == TRUE) {
17521 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17522 					(xp->xb_retry_count > 0)) &&
17523 					(un->un_startstop_timeid == NULL)) {
17524 					scsi_log(SD_DEVINFO(un), sd_label,
17525 					CE_WARN, "logical unit not ready, "
17526 					"resetting disk\n");
17527 					sd_reset_target(un, pktp);
17528 				}
17529 			} else {
17530 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17531 					(xp->xb_retry_count >
17532 					un->un_reset_retry_count)) &&
17533 					(un->un_startstop_timeid == NULL)) {
17534 					scsi_log(SD_DEVINFO(un), sd_label,
17535 					CE_WARN, "logical unit not ready, "
17536 					"resetting disk\n");
17537 					sd_reset_target(un, pktp);
17538 				}
17539 			}
17540 			break;
17541 
17542 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
17543 			/*
17544 			 * If the target is in the process of becoming
17545 			 * ready, just proceed with the retry. This can
17546 			 * happen with CD-ROMs that take a long time to
17547 			 * read TOC after a power cycle or reset.
17548 			 */
17549 			goto do_retry;
17550 
17551 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
17552 			break;
17553 
17554 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
17555 			/*
17556 			 * Retries cannot help here so just fail right away.
17557 			 */
17558 			goto fail_command;
17559 
17560 		case 0x88:
17561 			/*
17562 			 * Vendor-unique code for T3/T4: it indicates a
17563 			 * path problem in a mutipathed config, but as far as
17564 			 * the target driver is concerned it equates to a fatal
17565 			 * error, so we should just fail the command right away
17566 			 * (without printing anything to the console). If this
17567 			 * is not a T3/T4, fall thru to the default recovery
17568 			 * action.
17569 			 * T3/T4 is FC only, don't need to check is_fibre
17570 			 */
17571 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
17572 				sd_return_failed_command(un, bp, EIO);
17573 				return;
17574 			}
17575 			/* FALLTHRU */
17576 
17577 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
17578 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
17579 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
17580 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
17581 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
17582 		default:    /* Possible future codes in SCSI spec? */
17583 			/*
17584 			 * For removable-media devices, do not retry if
17585 			 * ASCQ > 2 as these result mostly from USCSI commands
17586 			 * on MMC devices issued to check status of an
17587 			 * operation initiated in immediate mode.  Also for
17588 			 * ASCQ >= 4 do not print console messages as these
17589 			 * mainly represent a user-initiated operation
17590 			 * instead of a system failure.
17591 			 */
17592 			if (un->un_f_has_removable_media) {
17593 				si.ssi_severity = SCSI_ERR_ALL;
17594 				goto fail_command;
17595 			}
17596 			break;
17597 		}
17598 
17599 		/*
17600 		 * As part of our recovery attempt for the NOT READY
17601 		 * condition, we issue a START STOP UNIT command. However
17602 		 * we want to wait for a short delay before attempting this
17603 		 * as there may still be more commands coming back from the
17604 		 * target with the check condition. To do this we use
17605 		 * timeout(9F) to call sd_start_stop_unit_callback() after
17606 		 * the delay interval expires. (sd_start_stop_unit_callback()
17607 		 * dispatches sd_start_stop_unit_task(), which will issue
17608 		 * the actual START STOP UNIT command. The delay interval
17609 		 * is one-half of the delay that we will use to retry the
17610 		 * command that generated the NOT READY condition.
17611 		 *
17612 		 * Note that we could just dispatch sd_start_stop_unit_task()
17613 		 * from here and allow it to sleep for the delay interval,
17614 		 * but then we would be tying up the taskq thread
17615 		 * uncesessarily for the duration of the delay.
17616 		 *
17617 		 * Do not issue the START STOP UNIT if the current command
17618 		 * is already a START STOP UNIT.
17619 		 */
17620 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
17621 			break;
17622 		}
17623 
17624 		/*
17625 		 * Do not schedule the timeout if one is already pending.
17626 		 */
17627 		if (un->un_startstop_timeid != NULL) {
17628 			SD_INFO(SD_LOG_ERROR, un,
17629 			    "sd_sense_key_not_ready: restart already issued to"
17630 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
17631 			    ddi_get_instance(SD_DEVINFO(un)));
17632 			break;
17633 		}
17634 
17635 		/*
17636 		 * Schedule the START STOP UNIT command, then queue the command
17637 		 * for a retry.
17638 		 *
17639 		 * Note: A timeout is not scheduled for this retry because we
17640 		 * want the retry to be serial with the START_STOP_UNIT. The
17641 		 * retry will be started when the START_STOP_UNIT is completed
17642 		 * in sd_start_stop_unit_task.
17643 		 */
17644 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
17645 		    un, SD_BSY_TIMEOUT / 2);
17646 		xp->xb_retry_count++;
17647 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
17648 		return;
17649 
17650 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
17651 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17652 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17653 			    "unit does not respond to selection\n");
17654 		}
17655 		break;
17656 
17657 	case 0x3A:	/* MEDIUM NOT PRESENT */
17658 		if (sd_error_level >= SCSI_ERR_FATAL) {
17659 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17660 			    "Caddy not inserted in drive\n");
17661 		}
17662 
17663 		sr_ejected(un);
17664 		un->un_mediastate = DKIO_EJECTED;
17665 		/* The state has changed, inform the media watch routines */
17666 		cv_broadcast(&un->un_state_cv);
17667 		/* Just fail if no media is present in the drive. */
17668 		goto fail_command;
17669 
17670 	default:
17671 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17672 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
17673 			    "Unit not Ready. Additional sense code 0x%x\n",
17674 			    asc);
17675 		}
17676 		break;
17677 	}
17678 
17679 do_retry:
17680 
17681 	/*
17682 	 * Retry the command, as some targets may report NOT READY for
17683 	 * several seconds after being reset.
17684 	 */
17685 	xp->xb_retry_count++;
17686 	si.ssi_severity = SCSI_ERR_RETRYABLE;
17687 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
17688 	    &si, EIO, SD_BSY_TIMEOUT, NULL);
17689 
17690 	return;
17691 
17692 fail_command:
17693 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17694 	sd_return_failed_command(un, bp, EIO);
17695 }
17696 
17697 
17698 
17699 /*
17700  *    Function: sd_sense_key_medium_or_hardware_error
17701  *
17702  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
17703  *		sense key.
17704  *
17705  *     Context: May be called from interrupt context
17706  */
17707 
17708 static void
17709 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
17710 	int sense_key, uint8_t asc,
17711 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17712 {
17713 	struct sd_sense_info	si;
17714 
17715 	ASSERT(un != NULL);
17716 	ASSERT(mutex_owned(SD_MUTEX(un)));
17717 	ASSERT(bp != NULL);
17718 	ASSERT(xp != NULL);
17719 	ASSERT(pktp != NULL);
17720 
17721 	si.ssi_severity = SCSI_ERR_FATAL;
17722 	si.ssi_pfa_flag = FALSE;
17723 
17724 	if (sense_key == KEY_MEDIUM_ERROR) {
17725 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
17726 	}
17727 
17728 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17729 
17730 	if ((un->un_reset_retry_count != 0) &&
17731 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
17732 		mutex_exit(SD_MUTEX(un));
17733 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
17734 		if (un->un_f_allow_bus_device_reset == TRUE) {
17735 
17736 			boolean_t try_resetting_target = B_TRUE;
17737 
17738 			/*
17739 			 * We need to be able to handle specific ASC when we are
17740 			 * handling a KEY_HARDWARE_ERROR. In particular
17741 			 * taking the default action of resetting the target may
17742 			 * not be the appropriate way to attempt recovery.
17743 			 * Resetting a target because of a single LUN failure
17744 			 * victimizes all LUNs on that target.
17745 			 *
17746 			 * This is true for the LSI arrays, if an LSI
17747 			 * array controller returns an ASC of 0x84 (LUN Dead) we
17748 			 * should trust it.
17749 			 */
17750 
17751 			if (sense_key == KEY_HARDWARE_ERROR) {
17752 				switch (asc) {
17753 				case 0x84:
17754 					if (SD_IS_LSI(un)) {
17755 						try_resetting_target = B_FALSE;
17756 					}
17757 					break;
17758 				default:
17759 					break;
17760 				}
17761 			}
17762 
17763 			if (try_resetting_target == B_TRUE) {
17764 				int reset_retval = 0;
17765 				if (un->un_f_lun_reset_enabled == TRUE) {
17766 					SD_TRACE(SD_LOG_IO_CORE, un,
17767 					    "sd_sense_key_medium_or_hardware_"
17768 					    "error: issuing RESET_LUN\n");
17769 					reset_retval =
17770 					    scsi_reset(SD_ADDRESS(un),
17771 					    RESET_LUN);
17772 				}
17773 				if (reset_retval == 0) {
17774 					SD_TRACE(SD_LOG_IO_CORE, un,
17775 					    "sd_sense_key_medium_or_hardware_"
17776 					    "error: issuing RESET_TARGET\n");
17777 					(void) scsi_reset(SD_ADDRESS(un),
17778 					    RESET_TARGET);
17779 				}
17780 			}
17781 		}
17782 		mutex_enter(SD_MUTEX(un));
17783 	}
17784 
17785 	/*
17786 	 * This really ought to be a fatal error, but we will retry anyway
17787 	 * as some drives report this as a spurious error.
17788 	 */
17789 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17790 	    &si, EIO, (clock_t)0, NULL);
17791 }
17792 
17793 
17794 
17795 /*
17796  *    Function: sd_sense_key_illegal_request
17797  *
17798  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
17799  *
17800  *     Context: May be called from interrupt context
17801  */
17802 
17803 static void
17804 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
17805 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17806 {
17807 	struct sd_sense_info	si;
17808 
17809 	ASSERT(un != NULL);
17810 	ASSERT(mutex_owned(SD_MUTEX(un)));
17811 	ASSERT(bp != NULL);
17812 	ASSERT(xp != NULL);
17813 	ASSERT(pktp != NULL);
17814 
17815 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17816 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
17817 
17818 	si.ssi_severity = SCSI_ERR_INFO;
17819 	si.ssi_pfa_flag = FALSE;
17820 
17821 	/* Pointless to retry if the target thinks it's an illegal request */
17822 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17823 	sd_return_failed_command(un, bp, EIO);
17824 }
17825 
17826 
17827 
17828 
17829 /*
17830  *    Function: sd_sense_key_unit_attention
17831  *
17832  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
17833  *
17834  *     Context: May be called from interrupt context
17835  */
17836 
17837 static void
17838 sd_sense_key_unit_attention(struct sd_lun *un,
17839 	uint8_t asc,
17840 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17841 {
17842 	/*
17843 	 * For UNIT ATTENTION we allow retries for one minute. Devices
17844 	 * like Sonoma can return UNIT ATTENTION close to a minute
17845 	 * under certain conditions.
17846 	 */
17847 	int	retry_check_flag = SD_RETRIES_UA;
17848 	boolean_t	kstat_updated = B_FALSE;
17849 	struct	sd_sense_info		si;
17850 
17851 	ASSERT(un != NULL);
17852 	ASSERT(mutex_owned(SD_MUTEX(un)));
17853 	ASSERT(bp != NULL);
17854 	ASSERT(xp != NULL);
17855 	ASSERT(pktp != NULL);
17856 
17857 	si.ssi_severity = SCSI_ERR_INFO;
17858 	si.ssi_pfa_flag = FALSE;
17859 
17860 
17861 	switch (asc) {
17862 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
17863 		if (sd_report_pfa != 0) {
17864 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17865 			si.ssi_pfa_flag = TRUE;
17866 			retry_check_flag = SD_RETRIES_STANDARD;
17867 			goto do_retry;
17868 		}
17869 		break;
17870 
17871 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
17872 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
17873 			un->un_resvd_status |=
17874 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
17875 		}
17876 		/* FALLTHRU */
17877 
17878 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
17879 		if (!un->un_f_has_removable_media) {
17880 			break;
17881 		}
17882 
17883 		/*
17884 		 * When we get a unit attention from a removable-media device,
17885 		 * it may be in a state that will take a long time to recover
17886 		 * (e.g., from a reset).  Since we are executing in interrupt
17887 		 * context here, we cannot wait around for the device to come
17888 		 * back. So hand this command off to sd_media_change_task()
17889 		 * for deferred processing under taskq thread context. (Note
17890 		 * that the command still may be failed if a problem is
17891 		 * encountered at a later time.)
17892 		 */
17893 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
17894 		    KM_NOSLEEP) == 0) {
17895 			/*
17896 			 * Cannot dispatch the request so fail the command.
17897 			 */
17898 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
17899 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
17900 			si.ssi_severity = SCSI_ERR_FATAL;
17901 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17902 			sd_return_failed_command(un, bp, EIO);
17903 		}
17904 
17905 		/*
17906 		 * If failed to dispatch sd_media_change_task(), we already
17907 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
17908 		 * we should update kstat later if it encounters an error. So,
17909 		 * we update kstat_updated flag here.
17910 		 */
17911 		kstat_updated = B_TRUE;
17912 
17913 		/*
17914 		 * Either the command has been successfully dispatched to a
17915 		 * task Q for retrying, or the dispatch failed. In either case
17916 		 * do NOT retry again by calling sd_retry_command. This sets up
17917 		 * two retries of the same command and when one completes and
17918 		 * frees the resources the other will access freed memory,
17919 		 * a bad thing.
17920 		 */
17921 		return;
17922 
17923 	default:
17924 		break;
17925 	}
17926 
17927 	/*
17928 	 * Update kstat if we haven't done that.
17929 	 */
17930 	if (!kstat_updated) {
17931 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17932 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
17933 	}
17934 
17935 do_retry:
17936 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
17937 	    EIO, SD_UA_RETRY_DELAY, NULL);
17938 }
17939 
17940 
17941 
17942 /*
17943  *    Function: sd_sense_key_fail_command
17944  *
17945  * Description: Use to fail a command when we don't like the sense key that
17946  *		was returned.
17947  *
17948  *     Context: May be called from interrupt context
17949  */
17950 
17951 static void
17952 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
17953 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17954 {
17955 	struct sd_sense_info	si;
17956 
17957 	ASSERT(un != NULL);
17958 	ASSERT(mutex_owned(SD_MUTEX(un)));
17959 	ASSERT(bp != NULL);
17960 	ASSERT(xp != NULL);
17961 	ASSERT(pktp != NULL);
17962 
17963 	si.ssi_severity = SCSI_ERR_FATAL;
17964 	si.ssi_pfa_flag = FALSE;
17965 
17966 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17967 	sd_return_failed_command(un, bp, EIO);
17968 }
17969 
17970 
17971 
17972 /*
17973  *    Function: sd_sense_key_blank_check
17974  *
17975  * Description: Recovery actions for a SCSI "Blank Check" sense key.
17976  *		Has no monetary connotation.
17977  *
17978  *     Context: May be called from interrupt context
17979  */
17980 
17981 static void
17982 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
17983 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17984 {
17985 	struct sd_sense_info	si;
17986 
17987 	ASSERT(un != NULL);
17988 	ASSERT(mutex_owned(SD_MUTEX(un)));
17989 	ASSERT(bp != NULL);
17990 	ASSERT(xp != NULL);
17991 	ASSERT(pktp != NULL);
17992 
17993 	/*
17994 	 * Blank check is not fatal for removable devices, therefore
17995 	 * it does not require a console message.
17996 	 */
17997 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
17998 	    SCSI_ERR_FATAL;
17999 	si.ssi_pfa_flag = FALSE;
18000 
18001 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18002 	sd_return_failed_command(un, bp, EIO);
18003 }
18004 
18005 
18006 
18007 
18008 /*
18009  *    Function: sd_sense_key_aborted_command
18010  *
18011  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
18012  *
18013  *     Context: May be called from interrupt context
18014  */
18015 
18016 static void
18017 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
18018 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18019 {
18020 	struct sd_sense_info	si;
18021 
18022 	ASSERT(un != NULL);
18023 	ASSERT(mutex_owned(SD_MUTEX(un)));
18024 	ASSERT(bp != NULL);
18025 	ASSERT(xp != NULL);
18026 	ASSERT(pktp != NULL);
18027 
18028 	si.ssi_severity = SCSI_ERR_FATAL;
18029 	si.ssi_pfa_flag = FALSE;
18030 
18031 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18032 
18033 	/*
18034 	 * This really ought to be a fatal error, but we will retry anyway
18035 	 * as some drives report this as a spurious error.
18036 	 */
18037 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18038 	    &si, EIO, (clock_t)0, NULL);
18039 }
18040 
18041 
18042 
18043 /*
18044  *    Function: sd_sense_key_default
18045  *
18046  * Description: Default recovery action for several SCSI sense keys (basically
18047  *		attempts a retry).
18048  *
18049  *     Context: May be called from interrupt context
18050  */
18051 
18052 static void
18053 sd_sense_key_default(struct sd_lun *un,
18054 	int sense_key,
18055 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18056 {
18057 	struct sd_sense_info	si;
18058 
18059 	ASSERT(un != NULL);
18060 	ASSERT(mutex_owned(SD_MUTEX(un)));
18061 	ASSERT(bp != NULL);
18062 	ASSERT(xp != NULL);
18063 	ASSERT(pktp != NULL);
18064 
18065 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18066 
18067 	/*
18068 	 * Undecoded sense key.	Attempt retries and hope that will fix
18069 	 * the problem.  Otherwise, we're dead.
18070 	 */
18071 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18072 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18073 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18074 	}
18075 
18076 	si.ssi_severity = SCSI_ERR_FATAL;
18077 	si.ssi_pfa_flag = FALSE;
18078 
18079 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
18080 	    &si, EIO, (clock_t)0, NULL);
18081 }
18082 
18083 
18084 
18085 /*
18086  *    Function: sd_print_retry_msg
18087  *
18088  * Description: Print a message indicating the retry action being taken.
18089  *
18090  *   Arguments: un - ptr to associated softstate
18091  *		bp - ptr to buf(9S) for the command
18092  *		arg - not used.
18093  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18094  *			or SD_NO_RETRY_ISSUED
18095  *
18096  *     Context: May be called from interrupt context
18097  */
18098 /* ARGSUSED */
18099 static void
18100 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18101 {
18102 	struct sd_xbuf	*xp;
18103 	struct scsi_pkt *pktp;
18104 	char *reasonp;
18105 	char *msgp;
18106 
18107 	ASSERT(un != NULL);
18108 	ASSERT(mutex_owned(SD_MUTEX(un)));
18109 	ASSERT(bp != NULL);
18110 	pktp = SD_GET_PKTP(bp);
18111 	ASSERT(pktp != NULL);
18112 	xp = SD_GET_XBUF(bp);
18113 	ASSERT(xp != NULL);
18114 
18115 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18116 	mutex_enter(&un->un_pm_mutex);
18117 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18118 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18119 	    (pktp->pkt_flags & FLAG_SILENT)) {
18120 		mutex_exit(&un->un_pm_mutex);
18121 		goto update_pkt_reason;
18122 	}
18123 	mutex_exit(&un->un_pm_mutex);
18124 
18125 	/*
18126 	 * Suppress messages if they are all the same pkt_reason; with
18127 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18128 	 * If we are in panic, then suppress the retry messages.
18129 	 */
18130 	switch (flag) {
18131 	case SD_NO_RETRY_ISSUED:
18132 		msgp = "giving up";
18133 		break;
18134 	case SD_IMMEDIATE_RETRY_ISSUED:
18135 	case SD_DELAYED_RETRY_ISSUED:
18136 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18137 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18138 		    (sd_error_level != SCSI_ERR_ALL))) {
18139 			return;
18140 		}
18141 		msgp = "retrying command";
18142 		break;
18143 	default:
18144 		goto update_pkt_reason;
18145 	}
18146 
18147 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18148 	    scsi_rname(pktp->pkt_reason));
18149 
18150 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18151 	    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18152 
18153 update_pkt_reason:
18154 	/*
18155 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18156 	 * This is to prevent multiple console messages for the same failure
18157 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18158 	 * when the command is retried successfully because there still may be
18159 	 * more commands coming back with the same value of pktp->pkt_reason.
18160 	 */
18161 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18162 		un->un_last_pkt_reason = pktp->pkt_reason;
18163 	}
18164 }
18165 
18166 
18167 /*
18168  *    Function: sd_print_cmd_incomplete_msg
18169  *
18170  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18171  *
18172  *   Arguments: un - ptr to associated softstate
18173  *		bp - ptr to buf(9S) for the command
18174  *		arg - passed to sd_print_retry_msg()
18175  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18176  *			or SD_NO_RETRY_ISSUED
18177  *
18178  *     Context: May be called from interrupt context
18179  */
18180 
18181 static void
18182 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18183 	int code)
18184 {
18185 	dev_info_t	*dip;
18186 
18187 	ASSERT(un != NULL);
18188 	ASSERT(mutex_owned(SD_MUTEX(un)));
18189 	ASSERT(bp != NULL);
18190 
18191 	switch (code) {
18192 	case SD_NO_RETRY_ISSUED:
18193 		/* Command was failed. Someone turned off this target? */
18194 		if (un->un_state != SD_STATE_OFFLINE) {
18195 			/*
18196 			 * Suppress message if we are detaching and
18197 			 * device has been disconnected
18198 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18199 			 * private interface and not part of the DDI
18200 			 */
18201 			dip = un->un_sd->sd_dev;
18202 			if (!(DEVI_IS_DETACHING(dip) &&
18203 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18204 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18205 				"disk not responding to selection\n");
18206 			}
18207 			New_state(un, SD_STATE_OFFLINE);
18208 		}
18209 		break;
18210 
18211 	case SD_DELAYED_RETRY_ISSUED:
18212 	case SD_IMMEDIATE_RETRY_ISSUED:
18213 	default:
18214 		/* Command was successfully queued for retry */
18215 		sd_print_retry_msg(un, bp, arg, code);
18216 		break;
18217 	}
18218 }
18219 
18220 
18221 /*
18222  *    Function: sd_pkt_reason_cmd_incomplete
18223  *
18224  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18225  *
18226  *     Context: May be called from interrupt context
18227  */
18228 
18229 static void
18230 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18231 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18232 {
18233 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18234 
18235 	ASSERT(un != NULL);
18236 	ASSERT(mutex_owned(SD_MUTEX(un)));
18237 	ASSERT(bp != NULL);
18238 	ASSERT(xp != NULL);
18239 	ASSERT(pktp != NULL);
18240 
18241 	/* Do not do a reset if selection did not complete */
18242 	/* Note: Should this not just check the bit? */
18243 	if (pktp->pkt_state != STATE_GOT_BUS) {
18244 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18245 		sd_reset_target(un, pktp);
18246 	}
18247 
18248 	/*
18249 	 * If the target was not successfully selected, then set
18250 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
18251 	 * with the target, and further retries and/or commands are
18252 	 * likely to take a long time.
18253 	 */
18254 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18255 		flag |= SD_RETRIES_FAILFAST;
18256 	}
18257 
18258 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18259 
18260 	sd_retry_command(un, bp, flag,
18261 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18262 }
18263 
18264 
18265 
18266 /*
18267  *    Function: sd_pkt_reason_cmd_tran_err
18268  *
18269  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18270  *
18271  *     Context: May be called from interrupt context
18272  */
18273 
18274 static void
18275 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
18276 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18277 {
18278 	ASSERT(un != NULL);
18279 	ASSERT(mutex_owned(SD_MUTEX(un)));
18280 	ASSERT(bp != NULL);
18281 	ASSERT(xp != NULL);
18282 	ASSERT(pktp != NULL);
18283 
18284 	/*
18285 	 * Do not reset if we got a parity error, or if
18286 	 * selection did not complete.
18287 	 */
18288 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18289 	/* Note: Should this not just check the bit for pkt_state? */
18290 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
18291 	    (pktp->pkt_state != STATE_GOT_BUS)) {
18292 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18293 		sd_reset_target(un, pktp);
18294 	}
18295 
18296 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18297 
18298 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18299 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18300 }
18301 
18302 
18303 
18304 /*
18305  *    Function: sd_pkt_reason_cmd_reset
18306  *
18307  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
18308  *
18309  *     Context: May be called from interrupt context
18310  */
18311 
18312 static void
18313 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
18314 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18315 {
18316 	ASSERT(un != NULL);
18317 	ASSERT(mutex_owned(SD_MUTEX(un)));
18318 	ASSERT(bp != NULL);
18319 	ASSERT(xp != NULL);
18320 	ASSERT(pktp != NULL);
18321 
18322 	/* The target may still be running the command, so try to reset. */
18323 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18324 	sd_reset_target(un, pktp);
18325 
18326 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18327 
18328 	/*
18329 	 * If pkt_reason is CMD_RESET chances are that this pkt got
18330 	 * reset because another target on this bus caused it. The target
18331 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18332 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18333 	 */
18334 
18335 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18336 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18337 }
18338 
18339 
18340 
18341 
18342 /*
18343  *    Function: sd_pkt_reason_cmd_aborted
18344  *
18345  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
18346  *
18347  *     Context: May be called from interrupt context
18348  */
18349 
18350 static void
18351 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
18352 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18353 {
18354 	ASSERT(un != NULL);
18355 	ASSERT(mutex_owned(SD_MUTEX(un)));
18356 	ASSERT(bp != NULL);
18357 	ASSERT(xp != NULL);
18358 	ASSERT(pktp != NULL);
18359 
18360 	/* The target may still be running the command, so try to reset. */
18361 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18362 	sd_reset_target(un, pktp);
18363 
18364 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18365 
18366 	/*
18367 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
18368 	 * aborted because another target on this bus caused it. The target
18369 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18370 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18371 	 */
18372 
18373 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18374 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18375 }
18376 
18377 
18378 
18379 /*
18380  *    Function: sd_pkt_reason_cmd_timeout
18381  *
18382  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
18383  *
18384  *     Context: May be called from interrupt context
18385  */
18386 
18387 static void
18388 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
18389 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18390 {
18391 	ASSERT(un != NULL);
18392 	ASSERT(mutex_owned(SD_MUTEX(un)));
18393 	ASSERT(bp != NULL);
18394 	ASSERT(xp != NULL);
18395 	ASSERT(pktp != NULL);
18396 
18397 
18398 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18399 	sd_reset_target(un, pktp);
18400 
18401 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18402 
18403 	/*
18404 	 * A command timeout indicates that we could not establish
18405 	 * communication with the target, so set SD_RETRIES_FAILFAST
18406 	 * as further retries/commands are likely to take a long time.
18407 	 */
18408 	sd_retry_command(un, bp,
18409 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
18410 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18411 }
18412 
18413 
18414 
18415 /*
18416  *    Function: sd_pkt_reason_cmd_unx_bus_free
18417  *
18418  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
18419  *
18420  *     Context: May be called from interrupt context
18421  */
18422 
18423 static void
18424 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
18425 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18426 {
18427 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
18428 
18429 	ASSERT(un != NULL);
18430 	ASSERT(mutex_owned(SD_MUTEX(un)));
18431 	ASSERT(bp != NULL);
18432 	ASSERT(xp != NULL);
18433 	ASSERT(pktp != NULL);
18434 
18435 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18436 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18437 
18438 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
18439 	    sd_print_retry_msg : NULL;
18440 
18441 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18442 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18443 }
18444 
18445 
18446 /*
18447  *    Function: sd_pkt_reason_cmd_tag_reject
18448  *
18449  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
18450  *
18451  *     Context: May be called from interrupt context
18452  */
18453 
18454 static void
18455 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
18456 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18457 {
18458 	ASSERT(un != NULL);
18459 	ASSERT(mutex_owned(SD_MUTEX(un)));
18460 	ASSERT(bp != NULL);
18461 	ASSERT(xp != NULL);
18462 	ASSERT(pktp != NULL);
18463 
18464 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18465 	pktp->pkt_flags = 0;
18466 	un->un_tagflags = 0;
18467 	if (un->un_f_opt_queueing == TRUE) {
18468 		un->un_throttle = min(un->un_throttle, 3);
18469 	} else {
18470 		un->un_throttle = 1;
18471 	}
18472 	mutex_exit(SD_MUTEX(un));
18473 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
18474 	mutex_enter(SD_MUTEX(un));
18475 
18476 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18477 
18478 	/* Legacy behavior not to check retry counts here. */
18479 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
18480 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18481 }
18482 
18483 
18484 /*
18485  *    Function: sd_pkt_reason_default
18486  *
18487  * Description: Default recovery actions for SCSA pkt_reason values that
18488  *		do not have more explicit recovery actions.
18489  *
18490  *     Context: May be called from interrupt context
18491  */
18492 
18493 static void
18494 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
18495 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18496 {
18497 	ASSERT(un != NULL);
18498 	ASSERT(mutex_owned(SD_MUTEX(un)));
18499 	ASSERT(bp != NULL);
18500 	ASSERT(xp != NULL);
18501 	ASSERT(pktp != NULL);
18502 
18503 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18504 	sd_reset_target(un, pktp);
18505 
18506 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18507 
18508 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18509 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18510 }
18511 
18512 
18513 
18514 /*
18515  *    Function: sd_pkt_status_check_condition
18516  *
18517  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
18518  *
18519  *     Context: May be called from interrupt context
18520  */
18521 
18522 static void
18523 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
18524 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18525 {
18526 	ASSERT(un != NULL);
18527 	ASSERT(mutex_owned(SD_MUTEX(un)));
18528 	ASSERT(bp != NULL);
18529 	ASSERT(xp != NULL);
18530 	ASSERT(pktp != NULL);
18531 
18532 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
18533 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
18534 
18535 	/*
18536 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
18537 	 * command will be retried after the request sense). Otherwise, retry
18538 	 * the command. Note: we are issuing the request sense even though the
18539 	 * retry limit may have been reached for the failed command.
18540 	 */
18541 	if (un->un_f_arq_enabled == FALSE) {
18542 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18543 		    "no ARQ, sending request sense command\n");
18544 		sd_send_request_sense_command(un, bp, pktp);
18545 	} else {
18546 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18547 		    "ARQ,retrying request sense command\n");
18548 #if defined(__i386) || defined(__amd64)
18549 		/*
18550 		 * The SD_RETRY_DELAY value need to be adjusted here
18551 		 * when SD_RETRY_DELAY change in sddef.h
18552 		 */
18553 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18554 			un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
18555 			NULL);
18556 #else
18557 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
18558 		    EIO, SD_RETRY_DELAY, NULL);
18559 #endif
18560 	}
18561 
18562 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
18563 }
18564 
18565 
18566 /*
18567  *    Function: sd_pkt_status_busy
18568  *
18569  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
18570  *
18571  *     Context: May be called from interrupt context
18572  */
18573 
18574 static void
18575 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18576 	struct scsi_pkt *pktp)
18577 {
18578 	ASSERT(un != NULL);
18579 	ASSERT(mutex_owned(SD_MUTEX(un)));
18580 	ASSERT(bp != NULL);
18581 	ASSERT(xp != NULL);
18582 	ASSERT(pktp != NULL);
18583 
18584 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18585 	    "sd_pkt_status_busy: entry\n");
18586 
18587 	/* If retries are exhausted, just fail the command. */
18588 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
18589 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18590 		    "device busy too long\n");
18591 		sd_return_failed_command(un, bp, EIO);
18592 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18593 		    "sd_pkt_status_busy: exit\n");
18594 		return;
18595 	}
18596 	xp->xb_retry_count++;
18597 
18598 	/*
18599 	 * Try to reset the target. However, we do not want to perform
18600 	 * more than one reset if the device continues to fail. The reset
18601 	 * will be performed when the retry count reaches the reset
18602 	 * threshold.  This threshold should be set such that at least
18603 	 * one retry is issued before the reset is performed.
18604 	 */
18605 	if (xp->xb_retry_count ==
18606 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
18607 		int rval = 0;
18608 		mutex_exit(SD_MUTEX(un));
18609 		if (un->un_f_allow_bus_device_reset == TRUE) {
18610 			/*
18611 			 * First try to reset the LUN; if we cannot then
18612 			 * try to reset the target.
18613 			 */
18614 			if (un->un_f_lun_reset_enabled == TRUE) {
18615 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18616 				    "sd_pkt_status_busy: RESET_LUN\n");
18617 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18618 			}
18619 			if (rval == 0) {
18620 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18621 				    "sd_pkt_status_busy: RESET_TARGET\n");
18622 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
18623 			}
18624 		}
18625 		if (rval == 0) {
18626 			/*
18627 			 * If the RESET_LUN and/or RESET_TARGET failed,
18628 			 * try RESET_ALL
18629 			 */
18630 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18631 			    "sd_pkt_status_busy: RESET_ALL\n");
18632 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
18633 		}
18634 		mutex_enter(SD_MUTEX(un));
18635 		if (rval == 0) {
18636 			/*
18637 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
18638 			 * At this point we give up & fail the command.
18639 			 */
18640 			sd_return_failed_command(un, bp, EIO);
18641 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18642 			    "sd_pkt_status_busy: exit (failed cmd)\n");
18643 			return;
18644 		}
18645 	}
18646 
18647 	/*
18648 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
18649 	 * we have already checked the retry counts above.
18650 	 */
18651 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
18652 	    EIO, SD_BSY_TIMEOUT, NULL);
18653 
18654 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18655 	    "sd_pkt_status_busy: exit\n");
18656 }
18657 
18658 
18659 /*
18660  *    Function: sd_pkt_status_reservation_conflict
18661  *
18662  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
18663  *		command status.
18664  *
18665  *     Context: May be called from interrupt context
18666  */
18667 
18668 static void
18669 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
18670 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18671 {
18672 	ASSERT(un != NULL);
18673 	ASSERT(mutex_owned(SD_MUTEX(un)));
18674 	ASSERT(bp != NULL);
18675 	ASSERT(xp != NULL);
18676 	ASSERT(pktp != NULL);
18677 
18678 	/*
18679 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
18680 	 * conflict could be due to various reasons like incorrect keys, not
18681 	 * registered or not reserved etc. So, we return EACCES to the caller.
18682 	 */
18683 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
18684 		int cmd = SD_GET_PKT_OPCODE(pktp);
18685 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
18686 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
18687 			sd_return_failed_command(un, bp, EACCES);
18688 			return;
18689 		}
18690 	}
18691 
18692 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
18693 
18694 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
18695 		if (sd_failfast_enable != 0) {
18696 			/* By definition, we must panic here.... */
18697 			sd_panic_for_res_conflict(un);
18698 			/*NOTREACHED*/
18699 		}
18700 		SD_ERROR(SD_LOG_IO, un,
18701 		    "sd_handle_resv_conflict: Disk Reserved\n");
18702 		sd_return_failed_command(un, bp, EACCES);
18703 		return;
18704 	}
18705 
18706 	/*
18707 	 * 1147670: retry only if sd_retry_on_reservation_conflict
18708 	 * property is set (default is 1). Retries will not succeed
18709 	 * on a disk reserved by another initiator. HA systems
18710 	 * may reset this via sd.conf to avoid these retries.
18711 	 *
18712 	 * Note: The legacy return code for this failure is EIO, however EACCES
18713 	 * seems more appropriate for a reservation conflict.
18714 	 */
18715 	if (sd_retry_on_reservation_conflict == 0) {
18716 		SD_ERROR(SD_LOG_IO, un,
18717 		    "sd_handle_resv_conflict: Device Reserved\n");
18718 		sd_return_failed_command(un, bp, EIO);
18719 		return;
18720 	}
18721 
18722 	/*
18723 	 * Retry the command if we can.
18724 	 *
18725 	 * Note: The legacy return code for this failure is EIO, however EACCES
18726 	 * seems more appropriate for a reservation conflict.
18727 	 */
18728 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18729 	    (clock_t)2, NULL);
18730 }
18731 
18732 
18733 
18734 /*
18735  *    Function: sd_pkt_status_qfull
18736  *
18737  * Description: Handle a QUEUE FULL condition from the target.  This can
18738  *		occur if the HBA does not handle the queue full condition.
18739  *		(Basically this means third-party HBAs as Sun HBAs will
18740  *		handle the queue full condition.)  Note that if there are
18741  *		some commands already in the transport, then the queue full
18742  *		has occurred because the queue for this nexus is actually
18743  *		full. If there are no commands in the transport, then the
18744  *		queue full is resulting from some other initiator or lun
18745  *		consuming all the resources at the target.
18746  *
18747  *     Context: May be called from interrupt context
18748  */
18749 
18750 static void
18751 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
18752 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18753 {
18754 	ASSERT(un != NULL);
18755 	ASSERT(mutex_owned(SD_MUTEX(un)));
18756 	ASSERT(bp != NULL);
18757 	ASSERT(xp != NULL);
18758 	ASSERT(pktp != NULL);
18759 
18760 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18761 	    "sd_pkt_status_qfull: entry\n");
18762 
18763 	/*
18764 	 * Just lower the QFULL throttle and retry the command.  Note that
18765 	 * we do not limit the number of retries here.
18766 	 */
18767 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
18768 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
18769 	    SD_RESTART_TIMEOUT, NULL);
18770 
18771 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18772 	    "sd_pkt_status_qfull: exit\n");
18773 }
18774 
18775 
18776 /*
18777  *    Function: sd_reset_target
18778  *
18779  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
18780  *		RESET_TARGET, or RESET_ALL.
18781  *
18782  *     Context: May be called under interrupt context.
18783  */
18784 
18785 static void
18786 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
18787 {
18788 	int rval = 0;
18789 
18790 	ASSERT(un != NULL);
18791 	ASSERT(mutex_owned(SD_MUTEX(un)));
18792 	ASSERT(pktp != NULL);
18793 
18794 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
18795 
18796 	/*
18797 	 * No need to reset if the transport layer has already done so.
18798 	 */
18799 	if ((pktp->pkt_statistics &
18800 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
18801 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18802 		    "sd_reset_target: no reset\n");
18803 		return;
18804 	}
18805 
18806 	mutex_exit(SD_MUTEX(un));
18807 
18808 	if (un->un_f_allow_bus_device_reset == TRUE) {
18809 		if (un->un_f_lun_reset_enabled == TRUE) {
18810 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18811 			    "sd_reset_target: RESET_LUN\n");
18812 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18813 		}
18814 		if (rval == 0) {
18815 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18816 			    "sd_reset_target: RESET_TARGET\n");
18817 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
18818 		}
18819 	}
18820 
18821 	if (rval == 0) {
18822 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18823 		    "sd_reset_target: RESET_ALL\n");
18824 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
18825 	}
18826 
18827 	mutex_enter(SD_MUTEX(un));
18828 
18829 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
18830 }
18831 
18832 
18833 /*
18834  *    Function: sd_media_change_task
18835  *
18836  * Description: Recovery action for CDROM to become available.
18837  *
18838  *     Context: Executes in a taskq() thread context
18839  */
18840 
18841 static void
18842 sd_media_change_task(void *arg)
18843 {
18844 	struct	scsi_pkt	*pktp = arg;
18845 	struct	sd_lun		*un;
18846 	struct	buf		*bp;
18847 	struct	sd_xbuf		*xp;
18848 	int	err		= 0;
18849 	int	retry_count	= 0;
18850 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
18851 	struct	sd_sense_info	si;
18852 
18853 	ASSERT(pktp != NULL);
18854 	bp = (struct buf *)pktp->pkt_private;
18855 	ASSERT(bp != NULL);
18856 	xp = SD_GET_XBUF(bp);
18857 	ASSERT(xp != NULL);
18858 	un = SD_GET_UN(bp);
18859 	ASSERT(un != NULL);
18860 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18861 	ASSERT(un->un_f_monitor_media_state);
18862 
18863 	si.ssi_severity = SCSI_ERR_INFO;
18864 	si.ssi_pfa_flag = FALSE;
18865 
18866 	/*
18867 	 * When a reset is issued on a CDROM, it takes a long time to
18868 	 * recover. First few attempts to read capacity and other things
18869 	 * related to handling unit attention fail (with a ASC 0x4 and
18870 	 * ASCQ 0x1). In that case we want to do enough retries and we want
18871 	 * to limit the retries in other cases of genuine failures like
18872 	 * no media in drive.
18873 	 */
18874 	while (retry_count++ < retry_limit) {
18875 		if ((err = sd_handle_mchange(un)) == 0) {
18876 			break;
18877 		}
18878 		if (err == EAGAIN) {
18879 			retry_limit = SD_UNIT_ATTENTION_RETRY;
18880 		}
18881 		/* Sleep for 0.5 sec. & try again */
18882 		delay(drv_usectohz(500000));
18883 	}
18884 
18885 	/*
18886 	 * Dispatch (retry or fail) the original command here,
18887 	 * along with appropriate console messages....
18888 	 *
18889 	 * Must grab the mutex before calling sd_retry_command,
18890 	 * sd_print_sense_msg and sd_return_failed_command.
18891 	 */
18892 	mutex_enter(SD_MUTEX(un));
18893 	if (err != SD_CMD_SUCCESS) {
18894 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18895 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18896 		si.ssi_severity = SCSI_ERR_FATAL;
18897 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18898 		sd_return_failed_command(un, bp, EIO);
18899 	} else {
18900 		sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18901 		    &si, EIO, (clock_t)0, NULL);
18902 	}
18903 	mutex_exit(SD_MUTEX(un));
18904 }
18905 
18906 
18907 
18908 /*
18909  *    Function: sd_handle_mchange
18910  *
18911  * Description: Perform geometry validation & other recovery when CDROM
18912  *		has been removed from drive.
18913  *
18914  * Return Code: 0 for success
18915  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
18916  *		sd_send_scsi_READ_CAPACITY()
18917  *
18918  *     Context: Executes in a taskq() thread context
18919  */
18920 
18921 static int
18922 sd_handle_mchange(struct sd_lun *un)
18923 {
18924 	uint64_t	capacity;
18925 	uint32_t	lbasize;
18926 	int		rval;
18927 
18928 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18929 	ASSERT(un->un_f_monitor_media_state);
18930 
18931 	if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
18932 	    SD_PATH_DIRECT_PRIORITY)) != 0) {
18933 		return (rval);
18934 	}
18935 
18936 	mutex_enter(SD_MUTEX(un));
18937 	sd_update_block_info(un, lbasize, capacity);
18938 
18939 	if (un->un_errstats != NULL) {
18940 		struct	sd_errstats *stp =
18941 		    (struct sd_errstats *)un->un_errstats->ks_data;
18942 		stp->sd_capacity.value.ui64 = (uint64_t)
18943 		    ((uint64_t)un->un_blockcount *
18944 		    (uint64_t)un->un_tgt_blocksize);
18945 	}
18946 
18947 	/*
18948 	 * Note: Maybe let the strategy/partitioning chain worry about getting
18949 	 * valid geometry.
18950 	 */
18951 	un->un_f_geometry_is_valid = FALSE;
18952 	(void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY);
18953 	if (un->un_f_geometry_is_valid == FALSE) {
18954 		mutex_exit(SD_MUTEX(un));
18955 		return (EIO);
18956 	}
18957 
18958 	mutex_exit(SD_MUTEX(un));
18959 
18960 	/*
18961 	 * Try to lock the door
18962 	 */
18963 	return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
18964 	    SD_PATH_DIRECT_PRIORITY));
18965 }
18966 
18967 
18968 /*
18969  *    Function: sd_send_scsi_DOORLOCK
18970  *
18971  * Description: Issue the scsi DOOR LOCK command
18972  *
18973  *   Arguments: un    - pointer to driver soft state (unit) structure for
18974  *			this target.
18975  *		flag  - SD_REMOVAL_ALLOW
18976  *			SD_REMOVAL_PREVENT
18977  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
18978  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
18979  *			to use the USCSI "direct" chain and bypass the normal
18980  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
18981  *			command is issued as part of an error recovery action.
18982  *
18983  * Return Code: 0   - Success
18984  *		errno return code from sd_send_scsi_cmd()
18985  *
18986  *     Context: Can sleep.
18987  */
18988 
18989 static int
18990 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag)
18991 {
18992 	union scsi_cdb		cdb;
18993 	struct uscsi_cmd	ucmd_buf;
18994 	struct scsi_extended_sense	sense_buf;
18995 	int			status;
18996 
18997 	ASSERT(un != NULL);
18998 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18999 
19000 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
19001 
19002 	/* already determined doorlock is not supported, fake success */
19003 	if (un->un_f_doorlock_supported == FALSE) {
19004 		return (0);
19005 	}
19006 
19007 	bzero(&cdb, sizeof (cdb));
19008 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19009 
19010 	cdb.scc_cmd = SCMD_DOORLOCK;
19011 	cdb.cdb_opaque[4] = (uchar_t)flag;
19012 
19013 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19014 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19015 	ucmd_buf.uscsi_bufaddr	= NULL;
19016 	ucmd_buf.uscsi_buflen	= 0;
19017 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19018 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19019 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19020 	ucmd_buf.uscsi_timeout	= 15;
19021 
19022 	SD_TRACE(SD_LOG_IO, un,
19023 	    "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n");
19024 
19025 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19026 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19027 
19028 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
19029 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19030 	    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
19031 		/* fake success and skip subsequent doorlock commands */
19032 		un->un_f_doorlock_supported = FALSE;
19033 		return (0);
19034 	}
19035 
19036 	return (status);
19037 }
19038 
19039 /*
19040  *    Function: sd_send_scsi_READ_CAPACITY
19041  *
19042  * Description: This routine uses the scsi READ CAPACITY command to determine
19043  *		the device capacity in number of blocks and the device native
19044  *		block size. If this function returns a failure, then the
19045  *		values in *capp and *lbap are undefined.  If the capacity
19046  *		returned is 0xffffffff then the lun is too large for a
19047  *		normal READ CAPACITY command and the results of a
19048  *		READ CAPACITY 16 will be used instead.
19049  *
19050  *   Arguments: un   - ptr to soft state struct for the target
19051  *		capp - ptr to unsigned 64-bit variable to receive the
19052  *			capacity value from the command.
19053  *		lbap - ptr to unsigned 32-bit varaible to receive the
19054  *			block size value from the command
19055  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19056  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19057  *			to use the USCSI "direct" chain and bypass the normal
19058  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19059  *			command is issued as part of an error recovery action.
19060  *
19061  * Return Code: 0   - Success
19062  *		EIO - IO error
19063  *		EACCES - Reservation conflict detected
19064  *		EAGAIN - Device is becoming ready
19065  *		errno return code from sd_send_scsi_cmd()
19066  *
19067  *     Context: Can sleep.  Blocks until command completes.
19068  */
19069 
19070 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
19071 
19072 static int
19073 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap,
19074 	int path_flag)
19075 {
19076 	struct	scsi_extended_sense	sense_buf;
19077 	struct	uscsi_cmd	ucmd_buf;
19078 	union	scsi_cdb	cdb;
19079 	uint32_t		*capacity_buf;
19080 	uint64_t		capacity;
19081 	uint32_t		lbasize;
19082 	int			status;
19083 
19084 	ASSERT(un != NULL);
19085 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19086 	ASSERT(capp != NULL);
19087 	ASSERT(lbap != NULL);
19088 
19089 	SD_TRACE(SD_LOG_IO, un,
19090 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19091 
19092 	/*
19093 	 * First send a READ_CAPACITY command to the target.
19094 	 * (This command is mandatory under SCSI-2.)
19095 	 *
19096 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
19097 	 * Medium Indicator bit is cleared.  The address field must be
19098 	 * zero if the PMI bit is zero.
19099 	 */
19100 	bzero(&cdb, sizeof (cdb));
19101 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19102 
19103 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19104 
19105 	cdb.scc_cmd = SCMD_READ_CAPACITY;
19106 
19107 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19108 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19109 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
19110 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
19111 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19112 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19113 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19114 	ucmd_buf.uscsi_timeout	= 60;
19115 
19116 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19117 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19118 
19119 	switch (status) {
19120 	case 0:
19121 		/* Return failure if we did not get valid capacity data. */
19122 		if (ucmd_buf.uscsi_resid != 0) {
19123 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19124 			return (EIO);
19125 		}
19126 
19127 		/*
19128 		 * Read capacity and block size from the READ CAPACITY 10 data.
19129 		 * This data may be adjusted later due to device specific
19130 		 * issues.
19131 		 *
19132 		 * According to the SCSI spec, the READ CAPACITY 10
19133 		 * command returns the following:
19134 		 *
19135 		 *  bytes 0-3: Maximum logical block address available.
19136 		 *		(MSB in byte:0 & LSB in byte:3)
19137 		 *
19138 		 *  bytes 4-7: Block length in bytes
19139 		 *		(MSB in byte:4 & LSB in byte:7)
19140 		 *
19141 		 */
19142 		capacity = BE_32(capacity_buf[0]);
19143 		lbasize = BE_32(capacity_buf[1]);
19144 
19145 		/*
19146 		 * Done with capacity_buf
19147 		 */
19148 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19149 
19150 		/*
19151 		 * if the reported capacity is set to all 0xf's, then
19152 		 * this disk is too large and requires SBC-2 commands.
19153 		 * Reissue the request using READ CAPACITY 16.
19154 		 */
19155 		if (capacity == 0xffffffff) {
19156 			status = sd_send_scsi_READ_CAPACITY_16(un, &capacity,
19157 			    &lbasize, path_flag);
19158 			if (status != 0) {
19159 				return (status);
19160 			}
19161 		}
19162 		break;	/* Success! */
19163 	case EIO:
19164 		switch (ucmd_buf.uscsi_status) {
19165 		case STATUS_RESERVATION_CONFLICT:
19166 			status = EACCES;
19167 			break;
19168 		case STATUS_CHECK:
19169 			/*
19170 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
19171 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19172 			 */
19173 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19174 			    (sense_buf.es_add_code  == 0x04) &&
19175 			    (sense_buf.es_qual_code == 0x01)) {
19176 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19177 				return (EAGAIN);
19178 			}
19179 			break;
19180 		default:
19181 			break;
19182 		}
19183 		/* FALLTHRU */
19184 	default:
19185 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19186 		return (status);
19187 	}
19188 
19189 	/*
19190 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
19191 	 * (2352 and 0 are common) so for these devices always force the value
19192 	 * to 2048 as required by the ATAPI specs.
19193 	 */
19194 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
19195 		lbasize = 2048;
19196 	}
19197 
19198 	/*
19199 	 * Get the maximum LBA value from the READ CAPACITY data.
19200 	 * Here we assume that the Partial Medium Indicator (PMI) bit
19201 	 * was cleared when issuing the command. This means that the LBA
19202 	 * returned from the device is the LBA of the last logical block
19203 	 * on the logical unit.  The actual logical block count will be
19204 	 * this value plus one.
19205 	 *
19206 	 * Currently the capacity is saved in terms of un->un_sys_blocksize,
19207 	 * so scale the capacity value to reflect this.
19208 	 */
19209 	capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize);
19210 
19211 #if defined(__i386) || defined(__amd64)
19212 	/*
19213 	 * On x86, compensate for off-by-1 error (number of sectors on
19214 	 * media)  (1175930)
19215 	 */
19216 	if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
19217 	    (lbasize == un->un_sys_blocksize)) {
19218 		capacity -= 1;
19219 	}
19220 #endif
19221 
19222 	/*
19223 	 * Copy the values from the READ CAPACITY command into the space
19224 	 * provided by the caller.
19225 	 */
19226 	*capp = capacity;
19227 	*lbap = lbasize;
19228 
19229 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
19230 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19231 
19232 	/*
19233 	 * Both the lbasize and capacity from the device must be nonzero,
19234 	 * otherwise we assume that the values are not valid and return
19235 	 * failure to the caller. (4203735)
19236 	 */
19237 	if ((capacity == 0) || (lbasize == 0)) {
19238 		return (EIO);
19239 	}
19240 
19241 	return (0);
19242 }
19243 
19244 /*
19245  *    Function: sd_send_scsi_READ_CAPACITY_16
19246  *
19247  * Description: This routine uses the scsi READ CAPACITY 16 command to
19248  *		determine the device capacity in number of blocks and the
19249  *		device native block size.  If this function returns a failure,
19250  *		then the values in *capp and *lbap are undefined.
19251  *		This routine should always be called by
19252  *		sd_send_scsi_READ_CAPACITY which will appy any device
19253  *		specific adjustments to capacity and lbasize.
19254  *
19255  *   Arguments: un   - ptr to soft state struct for the target
19256  *		capp - ptr to unsigned 64-bit variable to receive the
19257  *			capacity value from the command.
19258  *		lbap - ptr to unsigned 32-bit varaible to receive the
19259  *			block size value from the command
19260  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19261  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19262  *			to use the USCSI "direct" chain and bypass the normal
19263  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
19264  *			this command is issued as part of an error recovery
19265  *			action.
19266  *
19267  * Return Code: 0   - Success
19268  *		EIO - IO error
19269  *		EACCES - Reservation conflict detected
19270  *		EAGAIN - Device is becoming ready
19271  *		errno return code from sd_send_scsi_cmd()
19272  *
19273  *     Context: Can sleep.  Blocks until command completes.
19274  */
19275 
19276 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
19277 
19278 static int
19279 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
19280 	uint32_t *lbap, int path_flag)
19281 {
19282 	struct	scsi_extended_sense	sense_buf;
19283 	struct	uscsi_cmd	ucmd_buf;
19284 	union	scsi_cdb	cdb;
19285 	uint64_t		*capacity16_buf;
19286 	uint64_t		capacity;
19287 	uint32_t		lbasize;
19288 	int			status;
19289 
19290 	ASSERT(un != NULL);
19291 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19292 	ASSERT(capp != NULL);
19293 	ASSERT(lbap != NULL);
19294 
19295 	SD_TRACE(SD_LOG_IO, un,
19296 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19297 
19298 	/*
19299 	 * First send a READ_CAPACITY_16 command to the target.
19300 	 *
19301 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
19302 	 * Medium Indicator bit is cleared.  The address field must be
19303 	 * zero if the PMI bit is zero.
19304 	 */
19305 	bzero(&cdb, sizeof (cdb));
19306 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19307 
19308 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
19309 
19310 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19311 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
19312 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
19313 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
19314 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19315 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19316 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19317 	ucmd_buf.uscsi_timeout	= 60;
19318 
19319 	/*
19320 	 * Read Capacity (16) is a Service Action In command.  One
19321 	 * command byte (0x9E) is overloaded for multiple operations,
19322 	 * with the second CDB byte specifying the desired operation
19323 	 */
19324 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
19325 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
19326 
19327 	/*
19328 	 * Fill in allocation length field
19329 	 */
19330 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
19331 
19332 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19333 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19334 
19335 	switch (status) {
19336 	case 0:
19337 		/* Return failure if we did not get valid capacity data. */
19338 		if (ucmd_buf.uscsi_resid > 20) {
19339 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19340 			return (EIO);
19341 		}
19342 
19343 		/*
19344 		 * Read capacity and block size from the READ CAPACITY 10 data.
19345 		 * This data may be adjusted later due to device specific
19346 		 * issues.
19347 		 *
19348 		 * According to the SCSI spec, the READ CAPACITY 10
19349 		 * command returns the following:
19350 		 *
19351 		 *  bytes 0-7: Maximum logical block address available.
19352 		 *		(MSB in byte:0 & LSB in byte:7)
19353 		 *
19354 		 *  bytes 8-11: Block length in bytes
19355 		 *		(MSB in byte:8 & LSB in byte:11)
19356 		 *
19357 		 */
19358 		capacity = BE_64(capacity16_buf[0]);
19359 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
19360 
19361 		/*
19362 		 * Done with capacity16_buf
19363 		 */
19364 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19365 
19366 		/*
19367 		 * if the reported capacity is set to all 0xf's, then
19368 		 * this disk is too large.  This could only happen with
19369 		 * a device that supports LBAs larger than 64 bits which
19370 		 * are not defined by any current T10 standards.
19371 		 */
19372 		if (capacity == 0xffffffffffffffff) {
19373 			return (EIO);
19374 		}
19375 		break;	/* Success! */
19376 	case EIO:
19377 		switch (ucmd_buf.uscsi_status) {
19378 		case STATUS_RESERVATION_CONFLICT:
19379 			status = EACCES;
19380 			break;
19381 		case STATUS_CHECK:
19382 			/*
19383 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
19384 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19385 			 */
19386 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19387 			    (sense_buf.es_add_code  == 0x04) &&
19388 			    (sense_buf.es_qual_code == 0x01)) {
19389 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19390 				return (EAGAIN);
19391 			}
19392 			break;
19393 		default:
19394 			break;
19395 		}
19396 		/* FALLTHRU */
19397 	default:
19398 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19399 		return (status);
19400 	}
19401 
19402 	*capp = capacity;
19403 	*lbap = lbasize;
19404 
19405 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
19406 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19407 
19408 	return (0);
19409 }
19410 
19411 
19412 /*
19413  *    Function: sd_send_scsi_START_STOP_UNIT
19414  *
19415  * Description: Issue a scsi START STOP UNIT command to the target.
19416  *
19417  *   Arguments: un    - pointer to driver soft state (unit) structure for
19418  *			this target.
19419  *		flag  - SD_TARGET_START
19420  *			SD_TARGET_STOP
19421  *			SD_TARGET_EJECT
19422  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19423  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19424  *			to use the USCSI "direct" chain and bypass the normal
19425  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19426  *			command is issued as part of an error recovery action.
19427  *
19428  * Return Code: 0   - Success
19429  *		EIO - IO error
19430  *		EACCES - Reservation conflict detected
19431  *		ENXIO  - Not Ready, medium not present
19432  *		errno return code from sd_send_scsi_cmd()
19433  *
19434  *     Context: Can sleep.
19435  */
19436 
19437 static int
19438 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag)
19439 {
19440 	struct	scsi_extended_sense	sense_buf;
19441 	union scsi_cdb		cdb;
19442 	struct uscsi_cmd	ucmd_buf;
19443 	int			status;
19444 
19445 	ASSERT(un != NULL);
19446 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19447 
19448 	SD_TRACE(SD_LOG_IO, un,
19449 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
19450 
19451 	if (un->un_f_check_start_stop &&
19452 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
19453 	    (un->un_f_start_stop_supported != TRUE)) {
19454 		return (0);
19455 	}
19456 
19457 	bzero(&cdb, sizeof (cdb));
19458 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19459 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19460 
19461 	cdb.scc_cmd = SCMD_START_STOP;
19462 	cdb.cdb_opaque[4] = (uchar_t)flag;
19463 
19464 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19465 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19466 	ucmd_buf.uscsi_bufaddr	= NULL;
19467 	ucmd_buf.uscsi_buflen	= 0;
19468 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19469 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19470 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19471 	ucmd_buf.uscsi_timeout	= 200;
19472 
19473 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19474 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19475 
19476 	switch (status) {
19477 	case 0:
19478 		break;	/* Success! */
19479 	case EIO:
19480 		switch (ucmd_buf.uscsi_status) {
19481 		case STATUS_RESERVATION_CONFLICT:
19482 			status = EACCES;
19483 			break;
19484 		case STATUS_CHECK:
19485 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
19486 				switch (sense_buf.es_key) {
19487 				case KEY_ILLEGAL_REQUEST:
19488 					status = ENOTSUP;
19489 					break;
19490 				case KEY_NOT_READY:
19491 					if (sense_buf.es_add_code == 0x3A) {
19492 						status = ENXIO;
19493 					}
19494 					break;
19495 				default:
19496 					break;
19497 				}
19498 			}
19499 			break;
19500 		default:
19501 			break;
19502 		}
19503 		break;
19504 	default:
19505 		break;
19506 	}
19507 
19508 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
19509 
19510 	return (status);
19511 }
19512 
19513 
19514 /*
19515  *    Function: sd_start_stop_unit_callback
19516  *
19517  * Description: timeout(9F) callback to begin recovery process for a
19518  *		device that has spun down.
19519  *
19520  *   Arguments: arg - pointer to associated softstate struct.
19521  *
19522  *     Context: Executes in a timeout(9F) thread context
19523  */
19524 
19525 static void
19526 sd_start_stop_unit_callback(void *arg)
19527 {
19528 	struct sd_lun	*un = arg;
19529 	ASSERT(un != NULL);
19530 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19531 
19532 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
19533 
19534 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
19535 }
19536 
19537 
19538 /*
19539  *    Function: sd_start_stop_unit_task
19540  *
19541  * Description: Recovery procedure when a drive is spun down.
19542  *
19543  *   Arguments: arg - pointer to associated softstate struct.
19544  *
19545  *     Context: Executes in a taskq() thread context
19546  */
19547 
19548 static void
19549 sd_start_stop_unit_task(void *arg)
19550 {
19551 	struct sd_lun	*un = arg;
19552 
19553 	ASSERT(un != NULL);
19554 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19555 
19556 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
19557 
19558 	/*
19559 	 * Some unformatted drives report not ready error, no need to
19560 	 * restart if format has been initiated.
19561 	 */
19562 	mutex_enter(SD_MUTEX(un));
19563 	if (un->un_f_format_in_progress == TRUE) {
19564 		mutex_exit(SD_MUTEX(un));
19565 		return;
19566 	}
19567 	mutex_exit(SD_MUTEX(un));
19568 
19569 	/*
19570 	 * When a START STOP command is issued from here, it is part of a
19571 	 * failure recovery operation and must be issued before any other
19572 	 * commands, including any pending retries. Thus it must be sent
19573 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
19574 	 * succeeds or not, we will start I/O after the attempt.
19575 	 */
19576 	(void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
19577 	    SD_PATH_DIRECT_PRIORITY);
19578 
19579 	/*
19580 	 * The above call blocks until the START_STOP_UNIT command completes.
19581 	 * Now that it has completed, we must re-try the original IO that
19582 	 * received the NOT READY condition in the first place. There are
19583 	 * three possible conditions here:
19584 	 *
19585 	 *  (1) The original IO is on un_retry_bp.
19586 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
19587 	 *	is NULL.
19588 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
19589 	 *	points to some other, unrelated bp.
19590 	 *
19591 	 * For each case, we must call sd_start_cmds() with un_retry_bp
19592 	 * as the argument. If un_retry_bp is NULL, this will initiate
19593 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
19594 	 * then this will process the bp on un_retry_bp. That may or may not
19595 	 * be the original IO, but that does not matter: the important thing
19596 	 * is to keep the IO processing going at this point.
19597 	 *
19598 	 * Note: This is a very specific error recovery sequence associated
19599 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
19600 	 * serialize the I/O with completion of the spin-up.
19601 	 */
19602 	mutex_enter(SD_MUTEX(un));
19603 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19604 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
19605 	    un, un->un_retry_bp);
19606 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
19607 	sd_start_cmds(un, un->un_retry_bp);
19608 	mutex_exit(SD_MUTEX(un));
19609 
19610 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
19611 }
19612 
19613 
19614 /*
19615  *    Function: sd_send_scsi_INQUIRY
19616  *
19617  * Description: Issue the scsi INQUIRY command.
19618  *
19619  *   Arguments: un
19620  *		bufaddr
19621  *		buflen
19622  *		evpd
19623  *		page_code
19624  *		page_length
19625  *
19626  * Return Code: 0   - Success
19627  *		errno return code from sd_send_scsi_cmd()
19628  *
19629  *     Context: Can sleep. Does not return until command is completed.
19630  */
19631 
19632 static int
19633 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen,
19634 	uchar_t evpd, uchar_t page_code, size_t *residp)
19635 {
19636 	union scsi_cdb		cdb;
19637 	struct uscsi_cmd	ucmd_buf;
19638 	int			status;
19639 
19640 	ASSERT(un != NULL);
19641 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19642 	ASSERT(bufaddr != NULL);
19643 
19644 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
19645 
19646 	bzero(&cdb, sizeof (cdb));
19647 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19648 	bzero(bufaddr, buflen);
19649 
19650 	cdb.scc_cmd = SCMD_INQUIRY;
19651 	cdb.cdb_opaque[1] = evpd;
19652 	cdb.cdb_opaque[2] = page_code;
19653 	FORMG0COUNT(&cdb, buflen);
19654 
19655 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19656 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19657 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
19658 	ucmd_buf.uscsi_buflen	= buflen;
19659 	ucmd_buf.uscsi_rqbuf	= NULL;
19660 	ucmd_buf.uscsi_rqlen	= 0;
19661 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
19662 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
19663 
19664 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19665 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT);
19666 
19667 	if ((status == 0) && (residp != NULL)) {
19668 		*residp = ucmd_buf.uscsi_resid;
19669 	}
19670 
19671 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
19672 
19673 	return (status);
19674 }
19675 
19676 
19677 /*
19678  *    Function: sd_send_scsi_TEST_UNIT_READY
19679  *
19680  * Description: Issue the scsi TEST UNIT READY command.
19681  *		This routine can be told to set the flag USCSI_DIAGNOSE to
19682  *		prevent retrying failed commands. Use this when the intent
19683  *		is either to check for device readiness, to clear a Unit
19684  *		Attention, or to clear any outstanding sense data.
19685  *		However under specific conditions the expected behavior
19686  *		is for retries to bring a device ready, so use the flag
19687  *		with caution.
19688  *
19689  *   Arguments: un
19690  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
19691  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
19692  *			0: dont check for media present, do retries on cmd.
19693  *
19694  * Return Code: 0   - Success
19695  *		EIO - IO error
19696  *		EACCES - Reservation conflict detected
19697  *		ENXIO  - Not Ready, medium not present
19698  *		errno return code from sd_send_scsi_cmd()
19699  *
19700  *     Context: Can sleep. Does not return until command is completed.
19701  */
19702 
19703 static int
19704 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag)
19705 {
19706 	struct	scsi_extended_sense	sense_buf;
19707 	union scsi_cdb		cdb;
19708 	struct uscsi_cmd	ucmd_buf;
19709 	int			status;
19710 
19711 	ASSERT(un != NULL);
19712 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19713 
19714 	SD_TRACE(SD_LOG_IO, un,
19715 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
19716 
19717 	/*
19718 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
19719 	 * timeouts when they receive a TUR and the queue is not empty. Check
19720 	 * the configuration flag set during attach (indicating the drive has
19721 	 * this firmware bug) and un_ncmds_in_transport before issuing the
19722 	 * TUR. If there are
19723 	 * pending commands return success, this is a bit arbitrary but is ok
19724 	 * for non-removables (i.e. the eliteI disks) and non-clustering
19725 	 * configurations.
19726 	 */
19727 	if (un->un_f_cfg_tur_check == TRUE) {
19728 		mutex_enter(SD_MUTEX(un));
19729 		if (un->un_ncmds_in_transport != 0) {
19730 			mutex_exit(SD_MUTEX(un));
19731 			return (0);
19732 		}
19733 		mutex_exit(SD_MUTEX(un));
19734 	}
19735 
19736 	bzero(&cdb, sizeof (cdb));
19737 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19738 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19739 
19740 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
19741 
19742 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19743 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19744 	ucmd_buf.uscsi_bufaddr	= NULL;
19745 	ucmd_buf.uscsi_buflen	= 0;
19746 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19747 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19748 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19749 
19750 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
19751 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
19752 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
19753 	}
19754 	ucmd_buf.uscsi_timeout	= 60;
19755 
19756 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19757 	    UIO_SYSSPACE, UIO_SYSSPACE,
19758 	    ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD));
19759 
19760 	switch (status) {
19761 	case 0:
19762 		break;	/* Success! */
19763 	case EIO:
19764 		switch (ucmd_buf.uscsi_status) {
19765 		case STATUS_RESERVATION_CONFLICT:
19766 			status = EACCES;
19767 			break;
19768 		case STATUS_CHECK:
19769 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
19770 				break;
19771 			}
19772 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19773 			    (sense_buf.es_key == KEY_NOT_READY) &&
19774 			    (sense_buf.es_add_code == 0x3A)) {
19775 				status = ENXIO;
19776 			}
19777 			break;
19778 		default:
19779 			break;
19780 		}
19781 		break;
19782 	default:
19783 		break;
19784 	}
19785 
19786 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
19787 
19788 	return (status);
19789 }
19790 
19791 
19792 /*
19793  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
19794  *
19795  * Description: Issue the scsi PERSISTENT RESERVE IN command.
19796  *
19797  *   Arguments: un
19798  *
19799  * Return Code: 0   - Success
19800  *		EACCES
19801  *		ENOTSUP
19802  *		errno return code from sd_send_scsi_cmd()
19803  *
19804  *     Context: Can sleep. Does not return until command is completed.
19805  */
19806 
19807 static int
19808 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t  usr_cmd,
19809 	uint16_t data_len, uchar_t *data_bufp)
19810 {
19811 	struct scsi_extended_sense	sense_buf;
19812 	union scsi_cdb		cdb;
19813 	struct uscsi_cmd	ucmd_buf;
19814 	int			status;
19815 	int			no_caller_buf = FALSE;
19816 
19817 	ASSERT(un != NULL);
19818 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19819 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
19820 
19821 	SD_TRACE(SD_LOG_IO, un,
19822 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
19823 
19824 	bzero(&cdb, sizeof (cdb));
19825 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19826 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19827 	if (data_bufp == NULL) {
19828 		/* Allocate a default buf if the caller did not give one */
19829 		ASSERT(data_len == 0);
19830 		data_len  = MHIOC_RESV_KEY_SIZE;
19831 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
19832 		no_caller_buf = TRUE;
19833 	}
19834 
19835 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
19836 	cdb.cdb_opaque[1] = usr_cmd;
19837 	FORMG1COUNT(&cdb, data_len);
19838 
19839 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19840 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19841 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
19842 	ucmd_buf.uscsi_buflen	= data_len;
19843 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19844 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19845 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19846 	ucmd_buf.uscsi_timeout	= 60;
19847 
19848 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19849 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
19850 
19851 	switch (status) {
19852 	case 0:
19853 		break;	/* Success! */
19854 	case EIO:
19855 		switch (ucmd_buf.uscsi_status) {
19856 		case STATUS_RESERVATION_CONFLICT:
19857 			status = EACCES;
19858 			break;
19859 		case STATUS_CHECK:
19860 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19861 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
19862 				status = ENOTSUP;
19863 			}
19864 			break;
19865 		default:
19866 			break;
19867 		}
19868 		break;
19869 	default:
19870 		break;
19871 	}
19872 
19873 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
19874 
19875 	if (no_caller_buf == TRUE) {
19876 		kmem_free(data_bufp, data_len);
19877 	}
19878 
19879 	return (status);
19880 }
19881 
19882 
19883 /*
19884  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
19885  *
19886  * Description: This routine is the driver entry point for handling CD-ROM
19887  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
19888  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
19889  *		device.
19890  *
19891  *   Arguments: un  -   Pointer to soft state struct for the target.
19892  *		usr_cmd SCSI-3 reservation facility command (one of
19893  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
19894  *			SD_SCSI3_PREEMPTANDABORT)
19895  *		usr_bufp - user provided pointer register, reserve descriptor or
19896  *			preempt and abort structure (mhioc_register_t,
19897  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
19898  *
19899  * Return Code: 0   - Success
19900  *		EACCES
19901  *		ENOTSUP
19902  *		errno return code from sd_send_scsi_cmd()
19903  *
19904  *     Context: Can sleep. Does not return until command is completed.
19905  */
19906 
19907 static int
19908 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd,
19909 	uchar_t	*usr_bufp)
19910 {
19911 	struct scsi_extended_sense	sense_buf;
19912 	union scsi_cdb		cdb;
19913 	struct uscsi_cmd	ucmd_buf;
19914 	int			status;
19915 	uchar_t			data_len = sizeof (sd_prout_t);
19916 	sd_prout_t		*prp;
19917 
19918 	ASSERT(un != NULL);
19919 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19920 	ASSERT(data_len == 24);	/* required by scsi spec */
19921 
19922 	SD_TRACE(SD_LOG_IO, un,
19923 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
19924 
19925 	if (usr_bufp == NULL) {
19926 		return (EINVAL);
19927 	}
19928 
19929 	bzero(&cdb, sizeof (cdb));
19930 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19931 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19932 	prp = kmem_zalloc(data_len, KM_SLEEP);
19933 
19934 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
19935 	cdb.cdb_opaque[1] = usr_cmd;
19936 	FORMG1COUNT(&cdb, data_len);
19937 
19938 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19939 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19940 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
19941 	ucmd_buf.uscsi_buflen	= data_len;
19942 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19943 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19944 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
19945 	ucmd_buf.uscsi_timeout	= 60;
19946 
19947 	switch (usr_cmd) {
19948 	case SD_SCSI3_REGISTER: {
19949 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
19950 
19951 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19952 		bcopy(ptr->newkey.key, prp->service_key,
19953 		    MHIOC_RESV_KEY_SIZE);
19954 		prp->aptpl = ptr->aptpl;
19955 		break;
19956 	}
19957 	case SD_SCSI3_RESERVE:
19958 	case SD_SCSI3_RELEASE: {
19959 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
19960 
19961 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19962 		prp->scope_address = BE_32(ptr->scope_specific_addr);
19963 		cdb.cdb_opaque[2] = ptr->type;
19964 		break;
19965 	}
19966 	case SD_SCSI3_PREEMPTANDABORT: {
19967 		mhioc_preemptandabort_t *ptr =
19968 		    (mhioc_preemptandabort_t *)usr_bufp;
19969 
19970 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19971 		bcopy(ptr->victim_key.key, prp->service_key,
19972 		    MHIOC_RESV_KEY_SIZE);
19973 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
19974 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
19975 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
19976 		break;
19977 	}
19978 	case SD_SCSI3_REGISTERANDIGNOREKEY:
19979 	{
19980 		mhioc_registerandignorekey_t *ptr;
19981 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
19982 		bcopy(ptr->newkey.key,
19983 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
19984 		prp->aptpl = ptr->aptpl;
19985 		break;
19986 	}
19987 	default:
19988 		ASSERT(FALSE);
19989 		break;
19990 	}
19991 
19992 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19993 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
19994 
19995 	switch (status) {
19996 	case 0:
19997 		break;	/* Success! */
19998 	case EIO:
19999 		switch (ucmd_buf.uscsi_status) {
20000 		case STATUS_RESERVATION_CONFLICT:
20001 			status = EACCES;
20002 			break;
20003 		case STATUS_CHECK:
20004 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20005 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
20006 				status = ENOTSUP;
20007 			}
20008 			break;
20009 		default:
20010 			break;
20011 		}
20012 		break;
20013 	default:
20014 		break;
20015 	}
20016 
20017 	kmem_free(prp, data_len);
20018 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
20019 	return (status);
20020 }
20021 
20022 
20023 /*
20024  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
20025  *
20026  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
20027  *
20028  *   Arguments: un - pointer to the target's soft state struct
20029  *
20030  * Return Code: 0 - success
20031  *		errno-type error code
20032  *
20033  *     Context: kernel thread context only.
20034  */
20035 
20036 static int
20037 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
20038 {
20039 	struct sd_uscsi_info	*uip;
20040 	struct uscsi_cmd	*uscmd;
20041 	union scsi_cdb		*cdb;
20042 	struct buf		*bp;
20043 	int			rval = 0;
20044 
20045 	SD_TRACE(SD_LOG_IO, un,
20046 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
20047 
20048 	ASSERT(un != NULL);
20049 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20050 
20051 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
20052 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
20053 
20054 	/*
20055 	 * First get some memory for the uscsi_cmd struct and cdb
20056 	 * and initialize for SYNCHRONIZE_CACHE cmd.
20057 	 */
20058 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
20059 	uscmd->uscsi_cdblen = CDB_GROUP1;
20060 	uscmd->uscsi_cdb = (caddr_t)cdb;
20061 	uscmd->uscsi_bufaddr = NULL;
20062 	uscmd->uscsi_buflen = 0;
20063 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
20064 	uscmd->uscsi_rqlen = SENSE_LENGTH;
20065 	uscmd->uscsi_rqresid = SENSE_LENGTH;
20066 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20067 	uscmd->uscsi_timeout = sd_io_time;
20068 
20069 	/*
20070 	 * Allocate an sd_uscsi_info struct and fill it with the info
20071 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
20072 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
20073 	 * since we allocate the buf here in this function, we do not
20074 	 * need to preserve the prior contents of b_private.
20075 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
20076 	 */
20077 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
20078 	uip->ui_flags = SD_PATH_DIRECT;
20079 	uip->ui_cmdp  = uscmd;
20080 
20081 	bp = getrbuf(KM_SLEEP);
20082 	bp->b_private = uip;
20083 
20084 	/*
20085 	 * Setup buffer to carry uscsi request.
20086 	 */
20087 	bp->b_flags  = B_BUSY;
20088 	bp->b_bcount = 0;
20089 	bp->b_blkno  = 0;
20090 
20091 	if (dkc != NULL) {
20092 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
20093 		uip->ui_dkc = *dkc;
20094 	}
20095 
20096 	bp->b_edev = SD_GET_DEV(un);
20097 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
20098 
20099 	(void) sd_uscsi_strategy(bp);
20100 
20101 	/*
20102 	 * If synchronous request, wait for completion
20103 	 * If async just return and let b_iodone callback
20104 	 * cleanup.
20105 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
20106 	 * but it was also incremented in sd_uscsi_strategy(), so
20107 	 * we should be ok.
20108 	 */
20109 	if (dkc == NULL) {
20110 		(void) biowait(bp);
20111 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
20112 	}
20113 
20114 	return (rval);
20115 }
20116 
20117 
20118 static int
20119 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
20120 {
20121 	struct sd_uscsi_info *uip;
20122 	struct uscsi_cmd *uscmd;
20123 	struct scsi_extended_sense *sense_buf;
20124 	struct sd_lun *un;
20125 	int status;
20126 
20127 	uip = (struct sd_uscsi_info *)(bp->b_private);
20128 	ASSERT(uip != NULL);
20129 
20130 	uscmd = uip->ui_cmdp;
20131 	ASSERT(uscmd != NULL);
20132 
20133 	sense_buf = (struct scsi_extended_sense *)uscmd->uscsi_rqbuf;
20134 	ASSERT(sense_buf != NULL);
20135 
20136 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
20137 	ASSERT(un != NULL);
20138 
20139 	status = geterror(bp);
20140 	switch (status) {
20141 	case 0:
20142 		break;	/* Success! */
20143 	case EIO:
20144 		switch (uscmd->uscsi_status) {
20145 		case STATUS_RESERVATION_CONFLICT:
20146 			/* Ignore reservation conflict */
20147 			status = 0;
20148 			goto done;
20149 
20150 		case STATUS_CHECK:
20151 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
20152 			    (sense_buf->es_key == KEY_ILLEGAL_REQUEST)) {
20153 				/* Ignore Illegal Request error */
20154 				mutex_enter(SD_MUTEX(un));
20155 				un->un_f_sync_cache_supported = FALSE;
20156 				mutex_exit(SD_MUTEX(un));
20157 				status = ENOTSUP;
20158 				goto done;
20159 			}
20160 			break;
20161 		default:
20162 			break;
20163 		}
20164 		/* FALLTHRU */
20165 	default:
20166 		/* Ignore error if the media is not present */
20167 		if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) {
20168 			status = 0;
20169 			goto done;
20170 		}
20171 		/* If we reach this, we had an error */
20172 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
20173 		    "SYNCHRONIZE CACHE command failed (%d)\n", status);
20174 		break;
20175 	}
20176 
20177 done:
20178 	if (uip->ui_dkc.dkc_callback != NULL) {
20179 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
20180 	}
20181 
20182 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
20183 	freerbuf(bp);
20184 	kmem_free(uip, sizeof (struct sd_uscsi_info));
20185 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
20186 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
20187 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
20188 
20189 	return (status);
20190 }
20191 
20192 
20193 /*
20194  *    Function: sd_send_scsi_GET_CONFIGURATION
20195  *
20196  * Description: Issues the get configuration command to the device.
20197  *		Called from sd_check_for_writable_cd & sd_get_media_info
20198  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
20199  *   Arguments: un
20200  *		ucmdbuf
20201  *		rqbuf
20202  *		rqbuflen
20203  *		bufaddr
20204  *		buflen
20205  *
20206  * Return Code: 0   - Success
20207  *		errno return code from sd_send_scsi_cmd()
20208  *
20209  *     Context: Can sleep. Does not return until command is completed.
20210  *
20211  */
20212 
20213 static int
20214 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf,
20215 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen)
20216 {
20217 	char	cdb[CDB_GROUP1];
20218 	int	status;
20219 
20220 	ASSERT(un != NULL);
20221 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20222 	ASSERT(bufaddr != NULL);
20223 	ASSERT(ucmdbuf != NULL);
20224 	ASSERT(rqbuf != NULL);
20225 
20226 	SD_TRACE(SD_LOG_IO, un,
20227 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
20228 
20229 	bzero(cdb, sizeof (cdb));
20230 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
20231 	bzero(rqbuf, rqbuflen);
20232 	bzero(bufaddr, buflen);
20233 
20234 	/*
20235 	 * Set up cdb field for the get configuration command.
20236 	 */
20237 	cdb[0] = SCMD_GET_CONFIGURATION;
20238 	cdb[1] = 0x02;  /* Requested Type */
20239 	cdb[8] = SD_PROFILE_HEADER_LEN;
20240 	ucmdbuf->uscsi_cdb = cdb;
20241 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
20242 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
20243 	ucmdbuf->uscsi_buflen = buflen;
20244 	ucmdbuf->uscsi_timeout = sd_io_time;
20245 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
20246 	ucmdbuf->uscsi_rqlen = rqbuflen;
20247 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
20248 
20249 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE,
20250 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
20251 
20252 	switch (status) {
20253 	case 0:
20254 		break;  /* Success! */
20255 	case EIO:
20256 		switch (ucmdbuf->uscsi_status) {
20257 		case STATUS_RESERVATION_CONFLICT:
20258 			status = EACCES;
20259 			break;
20260 		default:
20261 			break;
20262 		}
20263 		break;
20264 	default:
20265 		break;
20266 	}
20267 
20268 	if (status == 0) {
20269 		SD_DUMP_MEMORY(un, SD_LOG_IO,
20270 		    "sd_send_scsi_GET_CONFIGURATION: data",
20271 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
20272 	}
20273 
20274 	SD_TRACE(SD_LOG_IO, un,
20275 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
20276 
20277 	return (status);
20278 }
20279 
20280 /*
20281  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
20282  *
20283  * Description: Issues the get configuration command to the device to
20284  *              retrieve a specfic feature. Called from
20285  *		sd_check_for_writable_cd & sd_set_mmc_caps.
20286  *   Arguments: un
20287  *              ucmdbuf
20288  *              rqbuf
20289  *              rqbuflen
20290  *              bufaddr
20291  *              buflen
20292  *		feature
20293  *
20294  * Return Code: 0   - Success
20295  *              errno return code from sd_send_scsi_cmd()
20296  *
20297  *     Context: Can sleep. Does not return until command is completed.
20298  *
20299  */
20300 static int
20301 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
20302 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
20303 	uchar_t *bufaddr, uint_t buflen, char feature)
20304 {
20305 	char    cdb[CDB_GROUP1];
20306 	int	status;
20307 
20308 	ASSERT(un != NULL);
20309 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20310 	ASSERT(bufaddr != NULL);
20311 	ASSERT(ucmdbuf != NULL);
20312 	ASSERT(rqbuf != NULL);
20313 
20314 	SD_TRACE(SD_LOG_IO, un,
20315 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
20316 
20317 	bzero(cdb, sizeof (cdb));
20318 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
20319 	bzero(rqbuf, rqbuflen);
20320 	bzero(bufaddr, buflen);
20321 
20322 	/*
20323 	 * Set up cdb field for the get configuration command.
20324 	 */
20325 	cdb[0] = SCMD_GET_CONFIGURATION;
20326 	cdb[1] = 0x02;  /* Requested Type */
20327 	cdb[3] = feature;
20328 	cdb[8] = buflen;
20329 	ucmdbuf->uscsi_cdb = cdb;
20330 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
20331 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
20332 	ucmdbuf->uscsi_buflen = buflen;
20333 	ucmdbuf->uscsi_timeout = sd_io_time;
20334 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
20335 	ucmdbuf->uscsi_rqlen = rqbuflen;
20336 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
20337 
20338 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE,
20339 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
20340 
20341 	switch (status) {
20342 	case 0:
20343 		break;  /* Success! */
20344 	case EIO:
20345 		switch (ucmdbuf->uscsi_status) {
20346 		case STATUS_RESERVATION_CONFLICT:
20347 			status = EACCES;
20348 			break;
20349 		default:
20350 			break;
20351 		}
20352 		break;
20353 	default:
20354 		break;
20355 	}
20356 
20357 	if (status == 0) {
20358 		SD_DUMP_MEMORY(un, SD_LOG_IO,
20359 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
20360 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
20361 	}
20362 
20363 	SD_TRACE(SD_LOG_IO, un,
20364 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
20365 
20366 	return (status);
20367 }
20368 
20369 
20370 /*
20371  *    Function: sd_send_scsi_MODE_SENSE
20372  *
20373  * Description: Utility function for issuing a scsi MODE SENSE command.
20374  *		Note: This routine uses a consistent implementation for Group0,
20375  *		Group1, and Group2 commands across all platforms. ATAPI devices
20376  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20377  *
20378  *   Arguments: un - pointer to the softstate struct for the target.
20379  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20380  *			  CDB_GROUP[1|2] (10 byte).
20381  *		bufaddr - buffer for page data retrieved from the target.
20382  *		buflen - size of page to be retrieved.
20383  *		page_code - page code of data to be retrieved from the target.
20384  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20385  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20386  *			to use the USCSI "direct" chain and bypass the normal
20387  *			command waitq.
20388  *
20389  * Return Code: 0   - Success
20390  *		errno return code from sd_send_scsi_cmd()
20391  *
20392  *     Context: Can sleep. Does not return until command is completed.
20393  */
20394 
20395 static int
20396 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20397 	size_t buflen,  uchar_t page_code, int path_flag)
20398 {
20399 	struct	scsi_extended_sense	sense_buf;
20400 	union scsi_cdb		cdb;
20401 	struct uscsi_cmd	ucmd_buf;
20402 	int			status;
20403 	int			headlen;
20404 
20405 	ASSERT(un != NULL);
20406 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20407 	ASSERT(bufaddr != NULL);
20408 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20409 	    (cdbsize == CDB_GROUP2));
20410 
20411 	SD_TRACE(SD_LOG_IO, un,
20412 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
20413 
20414 	bzero(&cdb, sizeof (cdb));
20415 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20416 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20417 	bzero(bufaddr, buflen);
20418 
20419 	if (cdbsize == CDB_GROUP0) {
20420 		cdb.scc_cmd = SCMD_MODE_SENSE;
20421 		cdb.cdb_opaque[2] = page_code;
20422 		FORMG0COUNT(&cdb, buflen);
20423 		headlen = MODE_HEADER_LENGTH;
20424 	} else {
20425 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
20426 		cdb.cdb_opaque[2] = page_code;
20427 		FORMG1COUNT(&cdb, buflen);
20428 		headlen = MODE_HEADER_LENGTH_GRP2;
20429 	}
20430 
20431 	ASSERT(headlen <= buflen);
20432 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20433 
20434 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20435 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20436 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20437 	ucmd_buf.uscsi_buflen	= buflen;
20438 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20439 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20440 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20441 	ucmd_buf.uscsi_timeout	= 60;
20442 
20443 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20444 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20445 
20446 	switch (status) {
20447 	case 0:
20448 		/*
20449 		 * sr_check_wp() uses 0x3f page code and check the header of
20450 		 * mode page to determine if target device is write-protected.
20451 		 * But some USB devices return 0 bytes for 0x3f page code. For
20452 		 * this case, make sure that mode page header is returned at
20453 		 * least.
20454 		 */
20455 		if (buflen - ucmd_buf.uscsi_resid <  headlen)
20456 			status = EIO;
20457 		break;	/* Success! */
20458 	case EIO:
20459 		switch (ucmd_buf.uscsi_status) {
20460 		case STATUS_RESERVATION_CONFLICT:
20461 			status = EACCES;
20462 			break;
20463 		default:
20464 			break;
20465 		}
20466 		break;
20467 	default:
20468 		break;
20469 	}
20470 
20471 	if (status == 0) {
20472 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
20473 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20474 	}
20475 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
20476 
20477 	return (status);
20478 }
20479 
20480 
20481 /*
20482  *    Function: sd_send_scsi_MODE_SELECT
20483  *
20484  * Description: Utility function for issuing a scsi MODE SELECT command.
20485  *		Note: This routine uses a consistent implementation for Group0,
20486  *		Group1, and Group2 commands across all platforms. ATAPI devices
20487  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20488  *
20489  *   Arguments: un - pointer to the softstate struct for the target.
20490  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20491  *			  CDB_GROUP[1|2] (10 byte).
20492  *		bufaddr - buffer for page data retrieved from the target.
20493  *		buflen - size of page to be retrieved.
20494  *		save_page - boolean to determin if SP bit should be set.
20495  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20496  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20497  *			to use the USCSI "direct" chain and bypass the normal
20498  *			command waitq.
20499  *
20500  * Return Code: 0   - Success
20501  *		errno return code from sd_send_scsi_cmd()
20502  *
20503  *     Context: Can sleep. Does not return until command is completed.
20504  */
20505 
20506 static int
20507 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20508 	size_t buflen,  uchar_t save_page, int path_flag)
20509 {
20510 	struct	scsi_extended_sense	sense_buf;
20511 	union scsi_cdb		cdb;
20512 	struct uscsi_cmd	ucmd_buf;
20513 	int			status;
20514 
20515 	ASSERT(un != NULL);
20516 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20517 	ASSERT(bufaddr != NULL);
20518 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20519 	    (cdbsize == CDB_GROUP2));
20520 
20521 	SD_TRACE(SD_LOG_IO, un,
20522 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
20523 
20524 	bzero(&cdb, sizeof (cdb));
20525 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20526 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20527 
20528 	/* Set the PF bit for many third party drives */
20529 	cdb.cdb_opaque[1] = 0x10;
20530 
20531 	/* Set the savepage(SP) bit if given */
20532 	if (save_page == SD_SAVE_PAGE) {
20533 		cdb.cdb_opaque[1] |= 0x01;
20534 	}
20535 
20536 	if (cdbsize == CDB_GROUP0) {
20537 		cdb.scc_cmd = SCMD_MODE_SELECT;
20538 		FORMG0COUNT(&cdb, buflen);
20539 	} else {
20540 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
20541 		FORMG1COUNT(&cdb, buflen);
20542 	}
20543 
20544 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20545 
20546 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20547 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20548 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20549 	ucmd_buf.uscsi_buflen	= buflen;
20550 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20551 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20552 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20553 	ucmd_buf.uscsi_timeout	= 60;
20554 
20555 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20556 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20557 
20558 	switch (status) {
20559 	case 0:
20560 		break;	/* Success! */
20561 	case EIO:
20562 		switch (ucmd_buf.uscsi_status) {
20563 		case STATUS_RESERVATION_CONFLICT:
20564 			status = EACCES;
20565 			break;
20566 		default:
20567 			break;
20568 		}
20569 		break;
20570 	default:
20571 		break;
20572 	}
20573 
20574 	if (status == 0) {
20575 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
20576 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20577 	}
20578 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
20579 
20580 	return (status);
20581 }
20582 
20583 
20584 /*
20585  *    Function: sd_send_scsi_RDWR
20586  *
20587  * Description: Issue a scsi READ or WRITE command with the given parameters.
20588  *
20589  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20590  *		cmd:	 SCMD_READ or SCMD_WRITE
20591  *		bufaddr: Address of caller's buffer to receive the RDWR data
20592  *		buflen:  Length of caller's buffer receive the RDWR data.
20593  *		start_block: Block number for the start of the RDWR operation.
20594  *			 (Assumes target-native block size.)
20595  *		residp:  Pointer to variable to receive the redisual of the
20596  *			 RDWR operation (may be NULL of no residual requested).
20597  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20598  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20599  *			to use the USCSI "direct" chain and bypass the normal
20600  *			command waitq.
20601  *
20602  * Return Code: 0   - Success
20603  *		errno return code from sd_send_scsi_cmd()
20604  *
20605  *     Context: Can sleep. Does not return until command is completed.
20606  */
20607 
20608 static int
20609 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
20610 	size_t buflen, daddr_t start_block, int path_flag)
20611 {
20612 	struct	scsi_extended_sense	sense_buf;
20613 	union scsi_cdb		cdb;
20614 	struct uscsi_cmd	ucmd_buf;
20615 	uint32_t		block_count;
20616 	int			status;
20617 	int			cdbsize;
20618 	uchar_t			flag;
20619 
20620 	ASSERT(un != NULL);
20621 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20622 	ASSERT(bufaddr != NULL);
20623 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
20624 
20625 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
20626 
20627 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
20628 		return (EINVAL);
20629 	}
20630 
20631 	mutex_enter(SD_MUTEX(un));
20632 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
20633 	mutex_exit(SD_MUTEX(un));
20634 
20635 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
20636 
20637 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
20638 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
20639 	    bufaddr, buflen, start_block, block_count);
20640 
20641 	bzero(&cdb, sizeof (cdb));
20642 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20643 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20644 
20645 	/* Compute CDB size to use */
20646 	if (start_block > 0xffffffff)
20647 		cdbsize = CDB_GROUP4;
20648 	else if ((start_block & 0xFFE00000) ||
20649 	    (un->un_f_cfg_is_atapi == TRUE))
20650 		cdbsize = CDB_GROUP1;
20651 	else
20652 		cdbsize = CDB_GROUP0;
20653 
20654 	switch (cdbsize) {
20655 	case CDB_GROUP0:	/* 6-byte CDBs */
20656 		cdb.scc_cmd = cmd;
20657 		FORMG0ADDR(&cdb, start_block);
20658 		FORMG0COUNT(&cdb, block_count);
20659 		break;
20660 	case CDB_GROUP1:	/* 10-byte CDBs */
20661 		cdb.scc_cmd = cmd | SCMD_GROUP1;
20662 		FORMG1ADDR(&cdb, start_block);
20663 		FORMG1COUNT(&cdb, block_count);
20664 		break;
20665 	case CDB_GROUP4:	/* 16-byte CDBs */
20666 		cdb.scc_cmd = cmd | SCMD_GROUP4;
20667 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
20668 		FORMG4COUNT(&cdb, block_count);
20669 		break;
20670 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
20671 	default:
20672 		/* All others reserved */
20673 		return (EINVAL);
20674 	}
20675 
20676 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
20677 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20678 
20679 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20680 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20681 	ucmd_buf.uscsi_bufaddr	= bufaddr;
20682 	ucmd_buf.uscsi_buflen	= buflen;
20683 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20684 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20685 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
20686 	ucmd_buf.uscsi_timeout	= 60;
20687 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20688 				UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20689 	switch (status) {
20690 	case 0:
20691 		break;	/* Success! */
20692 	case EIO:
20693 		switch (ucmd_buf.uscsi_status) {
20694 		case STATUS_RESERVATION_CONFLICT:
20695 			status = EACCES;
20696 			break;
20697 		default:
20698 			break;
20699 		}
20700 		break;
20701 	default:
20702 		break;
20703 	}
20704 
20705 	if (status == 0) {
20706 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
20707 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20708 	}
20709 
20710 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
20711 
20712 	return (status);
20713 }
20714 
20715 
20716 /*
20717  *    Function: sd_send_scsi_LOG_SENSE
20718  *
20719  * Description: Issue a scsi LOG_SENSE command with the given parameters.
20720  *
20721  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20722  *
20723  * Return Code: 0   - Success
20724  *		errno return code from sd_send_scsi_cmd()
20725  *
20726  *     Context: Can sleep. Does not return until command is completed.
20727  */
20728 
20729 static int
20730 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen,
20731 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
20732 	int path_flag)
20733 
20734 {
20735 	struct	scsi_extended_sense	sense_buf;
20736 	union scsi_cdb		cdb;
20737 	struct uscsi_cmd	ucmd_buf;
20738 	int			status;
20739 
20740 	ASSERT(un != NULL);
20741 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20742 
20743 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
20744 
20745 	bzero(&cdb, sizeof (cdb));
20746 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20747 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20748 
20749 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
20750 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
20751 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
20752 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
20753 	FORMG1COUNT(&cdb, buflen);
20754 
20755 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20756 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20757 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20758 	ucmd_buf.uscsi_buflen	= buflen;
20759 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20760 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20761 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20762 	ucmd_buf.uscsi_timeout	= 60;
20763 
20764 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20765 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20766 
20767 	switch (status) {
20768 	case 0:
20769 		break;
20770 	case EIO:
20771 		switch (ucmd_buf.uscsi_status) {
20772 		case STATUS_RESERVATION_CONFLICT:
20773 			status = EACCES;
20774 			break;
20775 		case STATUS_CHECK:
20776 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20777 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST) &&
20778 			    (sense_buf.es_add_code == 0x24)) {
20779 				/*
20780 				 * ASC 0x24: INVALID FIELD IN CDB
20781 				 */
20782 				switch (page_code) {
20783 				case START_STOP_CYCLE_PAGE:
20784 					/*
20785 					 * The start stop cycle counter is
20786 					 * implemented as page 0x31 in earlier
20787 					 * generation disks. In new generation
20788 					 * disks the start stop cycle counter is
20789 					 * implemented as page 0xE. To properly
20790 					 * handle this case if an attempt for
20791 					 * log page 0xE is made and fails we
20792 					 * will try again using page 0x31.
20793 					 *
20794 					 * Network storage BU committed to
20795 					 * maintain the page 0x31 for this
20796 					 * purpose and will not have any other
20797 					 * page implemented with page code 0x31
20798 					 * until all disks transition to the
20799 					 * standard page.
20800 					 */
20801 					mutex_enter(SD_MUTEX(un));
20802 					un->un_start_stop_cycle_page =
20803 					    START_STOP_CYCLE_VU_PAGE;
20804 					cdb.cdb_opaque[2] =
20805 					    (char)(page_control << 6) |
20806 					    un->un_start_stop_cycle_page;
20807 					mutex_exit(SD_MUTEX(un));
20808 					status = sd_send_scsi_cmd(
20809 					    SD_GET_DEV(un), &ucmd_buf,
20810 					    UIO_SYSSPACE, UIO_SYSSPACE,
20811 					    UIO_SYSSPACE, path_flag);
20812 
20813 					break;
20814 				case TEMPERATURE_PAGE:
20815 					status = ENOTTY;
20816 					break;
20817 				default:
20818 					break;
20819 				}
20820 			}
20821 			break;
20822 		default:
20823 			break;
20824 		}
20825 		break;
20826 	default:
20827 		break;
20828 	}
20829 
20830 	if (status == 0) {
20831 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
20832 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20833 	}
20834 
20835 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
20836 
20837 	return (status);
20838 }
20839 
20840 
20841 /*
20842  *    Function: sdioctl
20843  *
20844  * Description: Driver's ioctl(9e) entry point function.
20845  *
20846  *   Arguments: dev     - device number
20847  *		cmd     - ioctl operation to be performed
20848  *		arg     - user argument, contains data to be set or reference
20849  *			  parameter for get
20850  *		flag    - bit flag, indicating open settings, 32/64 bit type
20851  *		cred_p  - user credential pointer
20852  *		rval_p  - calling process return value (OPT)
20853  *
20854  * Return Code: EINVAL
20855  *		ENOTTY
20856  *		ENXIO
20857  *		EIO
20858  *		EFAULT
20859  *		ENOTSUP
20860  *		EPERM
20861  *
20862  *     Context: Called from the device switch at normal priority.
20863  */
20864 
20865 static int
20866 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
20867 {
20868 	struct sd_lun	*un = NULL;
20869 	int		geom_validated = FALSE;
20870 	int		err = 0;
20871 	int		i = 0;
20872 	cred_t		*cr;
20873 
20874 	/*
20875 	 * All device accesses go thru sdstrategy where we check on suspend
20876 	 * status
20877 	 */
20878 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
20879 		return (ENXIO);
20880 	}
20881 
20882 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20883 
20884 	/*
20885 	 * Moved this wait from sd_uscsi_strategy to here for
20886 	 * reasons of deadlock prevention. Internal driver commands,
20887 	 * specifically those to change a devices power level, result
20888 	 * in a call to sd_uscsi_strategy.
20889 	 */
20890 	mutex_enter(SD_MUTEX(un));
20891 	while ((un->un_state == SD_STATE_SUSPENDED) ||
20892 	    (un->un_state == SD_STATE_PM_CHANGING)) {
20893 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
20894 	}
20895 	/*
20896 	 * Twiddling the counter here protects commands from now
20897 	 * through to the top of sd_uscsi_strategy. Without the
20898 	 * counter inc. a power down, for example, could get in
20899 	 * after the above check for state is made and before
20900 	 * execution gets to the top of sd_uscsi_strategy.
20901 	 * That would cause problems.
20902 	 */
20903 	un->un_ncmds_in_driver++;
20904 
20905 	if ((un->un_f_geometry_is_valid == FALSE) &&
20906 	    (flag & (FNDELAY | FNONBLOCK))) {
20907 		switch (cmd) {
20908 		case CDROMPAUSE:
20909 		case CDROMRESUME:
20910 		case CDROMPLAYMSF:
20911 		case CDROMPLAYTRKIND:
20912 		case CDROMREADTOCHDR:
20913 		case CDROMREADTOCENTRY:
20914 		case CDROMSTOP:
20915 		case CDROMSTART:
20916 		case CDROMVOLCTRL:
20917 		case CDROMSUBCHNL:
20918 		case CDROMREADMODE2:
20919 		case CDROMREADMODE1:
20920 		case CDROMREADOFFSET:
20921 		case CDROMSBLKMODE:
20922 		case CDROMGBLKMODE:
20923 		case CDROMGDRVSPEED:
20924 		case CDROMSDRVSPEED:
20925 		case CDROMCDDA:
20926 		case CDROMCDXA:
20927 		case CDROMSUBCODE:
20928 			if (!ISCD(un)) {
20929 				un->un_ncmds_in_driver--;
20930 				ASSERT(un->un_ncmds_in_driver >= 0);
20931 				mutex_exit(SD_MUTEX(un));
20932 				return (ENOTTY);
20933 			}
20934 			break;
20935 		case FDEJECT:
20936 		case DKIOCEJECT:
20937 		case CDROMEJECT:
20938 			if (!un->un_f_eject_media_supported) {
20939 				un->un_ncmds_in_driver--;
20940 				ASSERT(un->un_ncmds_in_driver >= 0);
20941 				mutex_exit(SD_MUTEX(un));
20942 				return (ENOTTY);
20943 			}
20944 			break;
20945 		case DKIOCSVTOC:
20946 		case DKIOCSETEFI:
20947 		case DKIOCSMBOOT:
20948 		case DKIOCFLUSHWRITECACHE:
20949 			mutex_exit(SD_MUTEX(un));
20950 			err = sd_send_scsi_TEST_UNIT_READY(un, 0);
20951 			if (err != 0) {
20952 				mutex_enter(SD_MUTEX(un));
20953 				un->un_ncmds_in_driver--;
20954 				ASSERT(un->un_ncmds_in_driver >= 0);
20955 				mutex_exit(SD_MUTEX(un));
20956 				return (EIO);
20957 			}
20958 			mutex_enter(SD_MUTEX(un));
20959 			/* FALLTHROUGH */
20960 		case DKIOCREMOVABLE:
20961 		case DKIOCHOTPLUGGABLE:
20962 		case DKIOCINFO:
20963 		case DKIOCGMEDIAINFO:
20964 		case MHIOCENFAILFAST:
20965 		case MHIOCSTATUS:
20966 		case MHIOCTKOWN:
20967 		case MHIOCRELEASE:
20968 		case MHIOCGRP_INKEYS:
20969 		case MHIOCGRP_INRESV:
20970 		case MHIOCGRP_REGISTER:
20971 		case MHIOCGRP_RESERVE:
20972 		case MHIOCGRP_PREEMPTANDABORT:
20973 		case MHIOCGRP_REGISTERANDIGNOREKEY:
20974 		case CDROMCLOSETRAY:
20975 		case USCSICMD:
20976 			goto skip_ready_valid;
20977 		default:
20978 			break;
20979 		}
20980 
20981 		mutex_exit(SD_MUTEX(un));
20982 		err = sd_ready_and_valid(un);
20983 		mutex_enter(SD_MUTEX(un));
20984 		if (err == SD_READY_NOT_VALID) {
20985 			switch (cmd) {
20986 			case DKIOCGAPART:
20987 			case DKIOCGGEOM:
20988 			case DKIOCSGEOM:
20989 			case DKIOCGVTOC:
20990 			case DKIOCSVTOC:
20991 			case DKIOCSAPART:
20992 			case DKIOCG_PHYGEOM:
20993 			case DKIOCG_VIRTGEOM:
20994 				err = ENOTSUP;
20995 				un->un_ncmds_in_driver--;
20996 				ASSERT(un->un_ncmds_in_driver >= 0);
20997 				mutex_exit(SD_MUTEX(un));
20998 				return (err);
20999 			}
21000 		}
21001 		if (err != SD_READY_VALID) {
21002 			switch (cmd) {
21003 			case DKIOCSTATE:
21004 			case CDROMGDRVSPEED:
21005 			case CDROMSDRVSPEED:
21006 			case FDEJECT:	/* for eject command */
21007 			case DKIOCEJECT:
21008 			case CDROMEJECT:
21009 			case DKIOCGETEFI:
21010 			case DKIOCSGEOM:
21011 			case DKIOCREMOVABLE:
21012 			case DKIOCHOTPLUGGABLE:
21013 			case DKIOCSAPART:
21014 			case DKIOCSETEFI:
21015 				break;
21016 			default:
21017 				if (un->un_f_has_removable_media) {
21018 					err = ENXIO;
21019 				} else {
21020 					/* Do not map EACCES to EIO */
21021 					if (err != EACCES)
21022 						err = EIO;
21023 				}
21024 				un->un_ncmds_in_driver--;
21025 				ASSERT(un->un_ncmds_in_driver >= 0);
21026 				mutex_exit(SD_MUTEX(un));
21027 				return (err);
21028 			}
21029 		}
21030 		geom_validated = TRUE;
21031 	}
21032 	if ((un->un_f_geometry_is_valid == TRUE) &&
21033 	    (un->un_solaris_size > 0)) {
21034 		/*
21035 		 * the "geometry_is_valid" flag could be true if we
21036 		 * have an fdisk table but no Solaris partition
21037 		 */
21038 		if (un->un_vtoc.v_sanity != VTOC_SANE) {
21039 			/* it is EFI, so return ENOTSUP for these */
21040 			switch (cmd) {
21041 			case DKIOCGAPART:
21042 			case DKIOCGGEOM:
21043 			case DKIOCGVTOC:
21044 			case DKIOCSVTOC:
21045 			case DKIOCSAPART:
21046 				err = ENOTSUP;
21047 				un->un_ncmds_in_driver--;
21048 				ASSERT(un->un_ncmds_in_driver >= 0);
21049 				mutex_exit(SD_MUTEX(un));
21050 				return (err);
21051 			}
21052 		}
21053 	}
21054 
21055 skip_ready_valid:
21056 	mutex_exit(SD_MUTEX(un));
21057 
21058 	switch (cmd) {
21059 	case DKIOCINFO:
21060 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
21061 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
21062 		break;
21063 
21064 	case DKIOCGMEDIAINFO:
21065 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
21066 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
21067 		break;
21068 
21069 	case DKIOCGGEOM:
21070 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n");
21071 		err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag,
21072 		    geom_validated);
21073 		break;
21074 
21075 	case DKIOCSGEOM:
21076 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n");
21077 		err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag);
21078 		break;
21079 
21080 	case DKIOCGAPART:
21081 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n");
21082 		err = sd_dkio_get_partition(dev, (caddr_t)arg, flag,
21083 		    geom_validated);
21084 		break;
21085 
21086 	case DKIOCSAPART:
21087 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n");
21088 		err = sd_dkio_set_partition(dev, (caddr_t)arg, flag);
21089 		break;
21090 
21091 	case DKIOCGVTOC:
21092 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n");
21093 		err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag,
21094 		    geom_validated);
21095 		break;
21096 
21097 	case DKIOCGETEFI:
21098 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n");
21099 		err = sd_dkio_get_efi(dev, (caddr_t)arg, flag);
21100 		break;
21101 
21102 	case DKIOCPARTITION:
21103 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n");
21104 		err = sd_dkio_partition(dev, (caddr_t)arg, flag);
21105 		break;
21106 
21107 	case DKIOCSVTOC:
21108 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n");
21109 		err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag);
21110 		break;
21111 
21112 	case DKIOCSETEFI:
21113 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n");
21114 		err = sd_dkio_set_efi(dev, (caddr_t)arg, flag);
21115 		break;
21116 
21117 	case DKIOCGMBOOT:
21118 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n");
21119 		err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag);
21120 		break;
21121 
21122 	case DKIOCSMBOOT:
21123 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n");
21124 		err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag);
21125 		break;
21126 
21127 	case DKIOCLOCK:
21128 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
21129 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
21130 		    SD_PATH_STANDARD);
21131 		break;
21132 
21133 	case DKIOCUNLOCK:
21134 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
21135 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
21136 		    SD_PATH_STANDARD);
21137 		break;
21138 
21139 	case DKIOCSTATE: {
21140 		enum dkio_state		state;
21141 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
21142 
21143 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
21144 			err = EFAULT;
21145 		} else {
21146 			err = sd_check_media(dev, state);
21147 			if (err == 0) {
21148 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
21149 				    sizeof (int), flag) != 0)
21150 					err = EFAULT;
21151 			}
21152 		}
21153 		break;
21154 	}
21155 
21156 	case DKIOCREMOVABLE:
21157 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
21158 		/*
21159 		 * At present, vold only does automount for removable-media
21160 		 * devices, in order not to break current applications, we
21161 		 * still let hopluggable devices pretend to be removable media
21162 		 * devices for vold. In the near future, once vold is EOL'ed,
21163 		 * we should remove this workaround.
21164 		 */
21165 		if (un->un_f_has_removable_media || un->un_f_is_hotpluggable) {
21166 			i = 1;
21167 		} else {
21168 			i = 0;
21169 		}
21170 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
21171 			err = EFAULT;
21172 		} else {
21173 			err = 0;
21174 		}
21175 		break;
21176 
21177 	case DKIOCHOTPLUGGABLE:
21178 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
21179 		if (un->un_f_is_hotpluggable) {
21180 			i = 1;
21181 		} else {
21182 			i = 0;
21183 		}
21184 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
21185 			err = EFAULT;
21186 		} else {
21187 			err = 0;
21188 		}
21189 		break;
21190 
21191 	case DKIOCGTEMPERATURE:
21192 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
21193 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
21194 		break;
21195 
21196 	case MHIOCENFAILFAST:
21197 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
21198 		if ((err = drv_priv(cred_p)) == 0) {
21199 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
21200 		}
21201 		break;
21202 
21203 	case MHIOCTKOWN:
21204 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
21205 		if ((err = drv_priv(cred_p)) == 0) {
21206 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
21207 		}
21208 		break;
21209 
21210 	case MHIOCRELEASE:
21211 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
21212 		if ((err = drv_priv(cred_p)) == 0) {
21213 			err = sd_mhdioc_release(dev);
21214 		}
21215 		break;
21216 
21217 	case MHIOCSTATUS:
21218 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
21219 		if ((err = drv_priv(cred_p)) == 0) {
21220 			switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) {
21221 			case 0:
21222 				err = 0;
21223 				break;
21224 			case EACCES:
21225 				*rval_p = 1;
21226 				err = 0;
21227 				break;
21228 			default:
21229 				err = EIO;
21230 				break;
21231 			}
21232 		}
21233 		break;
21234 
21235 	case MHIOCQRESERVE:
21236 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
21237 		if ((err = drv_priv(cred_p)) == 0) {
21238 			err = sd_reserve_release(dev, SD_RESERVE);
21239 		}
21240 		break;
21241 
21242 	case MHIOCREREGISTERDEVID:
21243 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
21244 		if (drv_priv(cred_p) == EPERM) {
21245 			err = EPERM;
21246 		} else if (!un->un_f_devid_supported) {
21247 			err = ENOTTY;
21248 		} else {
21249 			err = sd_mhdioc_register_devid(dev);
21250 		}
21251 		break;
21252 
21253 	case MHIOCGRP_INKEYS:
21254 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
21255 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
21256 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21257 				err = ENOTSUP;
21258 			} else {
21259 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
21260 				    flag);
21261 			}
21262 		}
21263 		break;
21264 
21265 	case MHIOCGRP_INRESV:
21266 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
21267 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
21268 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21269 				err = ENOTSUP;
21270 			} else {
21271 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
21272 			}
21273 		}
21274 		break;
21275 
21276 	case MHIOCGRP_REGISTER:
21277 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
21278 		if ((err = drv_priv(cred_p)) != EPERM) {
21279 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21280 				err = ENOTSUP;
21281 			} else if (arg != NULL) {
21282 				mhioc_register_t reg;
21283 				if (ddi_copyin((void *)arg, &reg,
21284 				    sizeof (mhioc_register_t), flag) != 0) {
21285 					err = EFAULT;
21286 				} else {
21287 					err =
21288 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21289 					    un, SD_SCSI3_REGISTER,
21290 					    (uchar_t *)&reg);
21291 				}
21292 			}
21293 		}
21294 		break;
21295 
21296 	case MHIOCGRP_RESERVE:
21297 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
21298 		if ((err = drv_priv(cred_p)) != EPERM) {
21299 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21300 				err = ENOTSUP;
21301 			} else if (arg != NULL) {
21302 				mhioc_resv_desc_t resv_desc;
21303 				if (ddi_copyin((void *)arg, &resv_desc,
21304 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
21305 					err = EFAULT;
21306 				} else {
21307 					err =
21308 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21309 					    un, SD_SCSI3_RESERVE,
21310 					    (uchar_t *)&resv_desc);
21311 				}
21312 			}
21313 		}
21314 		break;
21315 
21316 	case MHIOCGRP_PREEMPTANDABORT:
21317 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
21318 		if ((err = drv_priv(cred_p)) != EPERM) {
21319 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21320 				err = ENOTSUP;
21321 			} else if (arg != NULL) {
21322 				mhioc_preemptandabort_t preempt_abort;
21323 				if (ddi_copyin((void *)arg, &preempt_abort,
21324 				    sizeof (mhioc_preemptandabort_t),
21325 				    flag) != 0) {
21326 					err = EFAULT;
21327 				} else {
21328 					err =
21329 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21330 					    un, SD_SCSI3_PREEMPTANDABORT,
21331 					    (uchar_t *)&preempt_abort);
21332 				}
21333 			}
21334 		}
21335 		break;
21336 
21337 	case MHIOCGRP_REGISTERANDIGNOREKEY:
21338 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
21339 		if ((err = drv_priv(cred_p)) != EPERM) {
21340 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21341 				err = ENOTSUP;
21342 			} else if (arg != NULL) {
21343 				mhioc_registerandignorekey_t r_and_i;
21344 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
21345 				    sizeof (mhioc_registerandignorekey_t),
21346 				    flag) != 0) {
21347 					err = EFAULT;
21348 				} else {
21349 					err =
21350 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21351 					    un, SD_SCSI3_REGISTERANDIGNOREKEY,
21352 					    (uchar_t *)&r_and_i);
21353 				}
21354 			}
21355 		}
21356 		break;
21357 
21358 	case USCSICMD:
21359 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
21360 		cr = ddi_get_cred();
21361 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
21362 			err = EPERM;
21363 		} else {
21364 			err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag);
21365 		}
21366 		break;
21367 
21368 	case CDROMPAUSE:
21369 	case CDROMRESUME:
21370 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
21371 		if (!ISCD(un)) {
21372 			err = ENOTTY;
21373 		} else {
21374 			err = sr_pause_resume(dev, cmd);
21375 		}
21376 		break;
21377 
21378 	case CDROMPLAYMSF:
21379 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
21380 		if (!ISCD(un)) {
21381 			err = ENOTTY;
21382 		} else {
21383 			err = sr_play_msf(dev, (caddr_t)arg, flag);
21384 		}
21385 		break;
21386 
21387 	case CDROMPLAYTRKIND:
21388 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
21389 #if defined(__i386) || defined(__amd64)
21390 		/*
21391 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
21392 		 */
21393 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21394 #else
21395 		if (!ISCD(un)) {
21396 #endif
21397 			err = ENOTTY;
21398 		} else {
21399 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
21400 		}
21401 		break;
21402 
21403 	case CDROMREADTOCHDR:
21404 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
21405 		if (!ISCD(un)) {
21406 			err = ENOTTY;
21407 		} else {
21408 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
21409 		}
21410 		break;
21411 
21412 	case CDROMREADTOCENTRY:
21413 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
21414 		if (!ISCD(un)) {
21415 			err = ENOTTY;
21416 		} else {
21417 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
21418 		}
21419 		break;
21420 
21421 	case CDROMSTOP:
21422 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
21423 		if (!ISCD(un)) {
21424 			err = ENOTTY;
21425 		} else {
21426 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP,
21427 			    SD_PATH_STANDARD);
21428 		}
21429 		break;
21430 
21431 	case CDROMSTART:
21432 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
21433 		if (!ISCD(un)) {
21434 			err = ENOTTY;
21435 		} else {
21436 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
21437 			    SD_PATH_STANDARD);
21438 		}
21439 		break;
21440 
21441 	case CDROMCLOSETRAY:
21442 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
21443 		if (!ISCD(un)) {
21444 			err = ENOTTY;
21445 		} else {
21446 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE,
21447 			    SD_PATH_STANDARD);
21448 		}
21449 		break;
21450 
21451 	case FDEJECT:	/* for eject command */
21452 	case DKIOCEJECT:
21453 	case CDROMEJECT:
21454 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
21455 		if (!un->un_f_eject_media_supported) {
21456 			err = ENOTTY;
21457 		} else {
21458 			err = sr_eject(dev);
21459 		}
21460 		break;
21461 
21462 	case CDROMVOLCTRL:
21463 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
21464 		if (!ISCD(un)) {
21465 			err = ENOTTY;
21466 		} else {
21467 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
21468 		}
21469 		break;
21470 
21471 	case CDROMSUBCHNL:
21472 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
21473 		if (!ISCD(un)) {
21474 			err = ENOTTY;
21475 		} else {
21476 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
21477 		}
21478 		break;
21479 
21480 	case CDROMREADMODE2:
21481 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
21482 		if (!ISCD(un)) {
21483 			err = ENOTTY;
21484 		} else if (un->un_f_cfg_is_atapi == TRUE) {
21485 			/*
21486 			 * If the drive supports READ CD, use that instead of
21487 			 * switching the LBA size via a MODE SELECT
21488 			 * Block Descriptor
21489 			 */
21490 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
21491 		} else {
21492 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
21493 		}
21494 		break;
21495 
21496 	case CDROMREADMODE1:
21497 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
21498 		if (!ISCD(un)) {
21499 			err = ENOTTY;
21500 		} else {
21501 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
21502 		}
21503 		break;
21504 
21505 	case CDROMREADOFFSET:
21506 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
21507 		if (!ISCD(un)) {
21508 			err = ENOTTY;
21509 		} else {
21510 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
21511 			    flag);
21512 		}
21513 		break;
21514 
21515 	case CDROMSBLKMODE:
21516 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
21517 		/*
21518 		 * There is no means of changing block size in case of atapi
21519 		 * drives, thus return ENOTTY if drive type is atapi
21520 		 */
21521 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21522 			err = ENOTTY;
21523 		} else if (un->un_f_mmc_cap == TRUE) {
21524 
21525 			/*
21526 			 * MMC Devices do not support changing the
21527 			 * logical block size
21528 			 *
21529 			 * Note: EINVAL is being returned instead of ENOTTY to
21530 			 * maintain consistancy with the original mmc
21531 			 * driver update.
21532 			 */
21533 			err = EINVAL;
21534 		} else {
21535 			mutex_enter(SD_MUTEX(un));
21536 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
21537 			    (un->un_ncmds_in_transport > 0)) {
21538 				mutex_exit(SD_MUTEX(un));
21539 				err = EINVAL;
21540 			} else {
21541 				mutex_exit(SD_MUTEX(un));
21542 				err = sr_change_blkmode(dev, cmd, arg, flag);
21543 			}
21544 		}
21545 		break;
21546 
21547 	case CDROMGBLKMODE:
21548 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
21549 		if (!ISCD(un)) {
21550 			err = ENOTTY;
21551 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
21552 		    (un->un_f_blockcount_is_valid != FALSE)) {
21553 			/*
21554 			 * Drive is an ATAPI drive so return target block
21555 			 * size for ATAPI drives since we cannot change the
21556 			 * blocksize on ATAPI drives. Used primarily to detect
21557 			 * if an ATAPI cdrom is present.
21558 			 */
21559 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
21560 			    sizeof (int), flag) != 0) {
21561 				err = EFAULT;
21562 			} else {
21563 				err = 0;
21564 			}
21565 
21566 		} else {
21567 			/*
21568 			 * Drive supports changing block sizes via a Mode
21569 			 * Select.
21570 			 */
21571 			err = sr_change_blkmode(dev, cmd, arg, flag);
21572 		}
21573 		break;
21574 
21575 	case CDROMGDRVSPEED:
21576 	case CDROMSDRVSPEED:
21577 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
21578 		if (!ISCD(un)) {
21579 			err = ENOTTY;
21580 		} else if (un->un_f_mmc_cap == TRUE) {
21581 			/*
21582 			 * Note: In the future the driver implementation
21583 			 * for getting and
21584 			 * setting cd speed should entail:
21585 			 * 1) If non-mmc try the Toshiba mode page
21586 			 *    (sr_change_speed)
21587 			 * 2) If mmc but no support for Real Time Streaming try
21588 			 *    the SET CD SPEED (0xBB) command
21589 			 *   (sr_atapi_change_speed)
21590 			 * 3) If mmc and support for Real Time Streaming
21591 			 *    try the GET PERFORMANCE and SET STREAMING
21592 			 *    commands (not yet implemented, 4380808)
21593 			 */
21594 			/*
21595 			 * As per recent MMC spec, CD-ROM speed is variable
21596 			 * and changes with LBA. Since there is no such
21597 			 * things as drive speed now, fail this ioctl.
21598 			 *
21599 			 * Note: EINVAL is returned for consistancy of original
21600 			 * implementation which included support for getting
21601 			 * the drive speed of mmc devices but not setting
21602 			 * the drive speed. Thus EINVAL would be returned
21603 			 * if a set request was made for an mmc device.
21604 			 * We no longer support get or set speed for
21605 			 * mmc but need to remain consistant with regard
21606 			 * to the error code returned.
21607 			 */
21608 			err = EINVAL;
21609 		} else if (un->un_f_cfg_is_atapi == TRUE) {
21610 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
21611 		} else {
21612 			err = sr_change_speed(dev, cmd, arg, flag);
21613 		}
21614 		break;
21615 
21616 	case CDROMCDDA:
21617 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
21618 		if (!ISCD(un)) {
21619 			err = ENOTTY;
21620 		} else {
21621 			err = sr_read_cdda(dev, (void *)arg, flag);
21622 		}
21623 		break;
21624 
21625 	case CDROMCDXA:
21626 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
21627 		if (!ISCD(un)) {
21628 			err = ENOTTY;
21629 		} else {
21630 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
21631 		}
21632 		break;
21633 
21634 	case CDROMSUBCODE:
21635 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
21636 		if (!ISCD(un)) {
21637 			err = ENOTTY;
21638 		} else {
21639 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
21640 		}
21641 		break;
21642 
21643 	case DKIOCPARTINFO: {
21644 		/*
21645 		 * Return parameters describing the selected disk slice.
21646 		 * Note: this ioctl is for the intel platform only
21647 		 */
21648 #if defined(__i386) || defined(__amd64)
21649 		int part;
21650 
21651 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
21652 		part = SDPART(dev);
21653 
21654 		/* don't check un_solaris_size for pN */
21655 		if (part < P0_RAW_DISK && un->un_solaris_size == 0) {
21656 			err = EIO;
21657 		} else {
21658 			struct part_info p;
21659 
21660 			p.p_start = (daddr_t)un->un_offset[part];
21661 			p.p_length = (int)un->un_map[part].dkl_nblk;
21662 #ifdef _MULTI_DATAMODEL
21663 			switch (ddi_model_convert_from(flag & FMODELS)) {
21664 			case DDI_MODEL_ILP32:
21665 			{
21666 				struct part_info32 p32;
21667 
21668 				p32.p_start = (daddr32_t)p.p_start;
21669 				p32.p_length = p.p_length;
21670 				if (ddi_copyout(&p32, (void *)arg,
21671 				    sizeof (p32), flag))
21672 					err = EFAULT;
21673 				break;
21674 			}
21675 
21676 			case DDI_MODEL_NONE:
21677 			{
21678 				if (ddi_copyout(&p, (void *)arg, sizeof (p),
21679 				    flag))
21680 					err = EFAULT;
21681 				break;
21682 			}
21683 			}
21684 #else /* ! _MULTI_DATAMODEL */
21685 			if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
21686 				err = EFAULT;
21687 #endif /* _MULTI_DATAMODEL */
21688 		}
21689 #else
21690 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
21691 		err = ENOTTY;
21692 #endif
21693 		break;
21694 	}
21695 
21696 	case DKIOCG_PHYGEOM: {
21697 		/* Return the driver's notion of the media physical geometry */
21698 #if defined(__i386) || defined(__amd64)
21699 		struct dk_geom	disk_geom;
21700 		struct dk_geom	*dkgp = &disk_geom;
21701 
21702 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
21703 		mutex_enter(SD_MUTEX(un));
21704 
21705 		if (un->un_g.dkg_nhead != 0 &&
21706 		    un->un_g.dkg_nsect != 0) {
21707 			/*
21708 			 * We succeeded in getting a geometry, but
21709 			 * right now it is being reported as just the
21710 			 * Solaris fdisk partition, just like for
21711 			 * DKIOCGGEOM. We need to change that to be
21712 			 * correct for the entire disk now.
21713 			 */
21714 			bcopy(&un->un_g, dkgp, sizeof (*dkgp));
21715 			dkgp->dkg_acyl = 0;
21716 			dkgp->dkg_ncyl = un->un_blockcount /
21717 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
21718 		} else {
21719 			bzero(dkgp, sizeof (struct dk_geom));
21720 			/*
21721 			 * This disk does not have a Solaris VTOC
21722 			 * so we must present a physical geometry
21723 			 * that will remain consistent regardless
21724 			 * of how the disk is used. This will ensure
21725 			 * that the geometry does not change regardless
21726 			 * of the fdisk partition type (ie. EFI, FAT32,
21727 			 * Solaris, etc).
21728 			 */
21729 			if (ISCD(un)) {
21730 				dkgp->dkg_nhead = un->un_pgeom.g_nhead;
21731 				dkgp->dkg_nsect = un->un_pgeom.g_nsect;
21732 				dkgp->dkg_ncyl = un->un_pgeom.g_ncyl;
21733 				dkgp->dkg_acyl = un->un_pgeom.g_acyl;
21734 			} else {
21735 				/*
21736 				 * Invalid un_blockcount can generate invalid
21737 				 * dk_geom and may result in division by zero
21738 				 * system failure. Should make sure blockcount
21739 				 * is valid before using it here.
21740 				 */
21741 				if (un->un_f_blockcount_is_valid == FALSE) {
21742 					mutex_exit(SD_MUTEX(un));
21743 					err = EIO;
21744 
21745 					break;
21746 				}
21747 				sd_convert_geometry(un->un_blockcount, dkgp);
21748 				dkgp->dkg_acyl = 0;
21749 				dkgp->dkg_ncyl = un->un_blockcount /
21750 				    (dkgp->dkg_nhead * dkgp->dkg_nsect);
21751 			}
21752 		}
21753 		dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
21754 
21755 		if (ddi_copyout(dkgp, (void *)arg,
21756 		    sizeof (struct dk_geom), flag)) {
21757 			mutex_exit(SD_MUTEX(un));
21758 			err = EFAULT;
21759 		} else {
21760 			mutex_exit(SD_MUTEX(un));
21761 			err = 0;
21762 		}
21763 #else
21764 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
21765 		err = ENOTTY;
21766 #endif
21767 		break;
21768 	}
21769 
21770 	case DKIOCG_VIRTGEOM: {
21771 		/* Return the driver's notion of the media's logical geometry */
21772 #if defined(__i386) || defined(__amd64)
21773 		struct dk_geom	disk_geom;
21774 		struct dk_geom	*dkgp = &disk_geom;
21775 
21776 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
21777 		mutex_enter(SD_MUTEX(un));
21778 		/*
21779 		 * If there is no HBA geometry available, or
21780 		 * if the HBA returned us something that doesn't
21781 		 * really fit into an Int 13/function 8 geometry
21782 		 * result, just fail the ioctl.  See PSARC 1998/313.
21783 		 */
21784 		if (un->un_lgeom.g_nhead == 0 ||
21785 		    un->un_lgeom.g_nsect == 0 ||
21786 		    un->un_lgeom.g_ncyl > 1024) {
21787 			mutex_exit(SD_MUTEX(un));
21788 			err = EINVAL;
21789 		} else {
21790 			dkgp->dkg_ncyl	= un->un_lgeom.g_ncyl;
21791 			dkgp->dkg_acyl	= un->un_lgeom.g_acyl;
21792 			dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
21793 			dkgp->dkg_nhead	= un->un_lgeom.g_nhead;
21794 			dkgp->dkg_nsect	= un->un_lgeom.g_nsect;
21795 
21796 			if (ddi_copyout(dkgp, (void *)arg,
21797 			    sizeof (struct dk_geom), flag)) {
21798 				mutex_exit(SD_MUTEX(un));
21799 				err = EFAULT;
21800 			} else {
21801 				mutex_exit(SD_MUTEX(un));
21802 				err = 0;
21803 			}
21804 		}
21805 #else
21806 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
21807 		err = ENOTTY;
21808 #endif
21809 		break;
21810 	}
21811 #ifdef SDDEBUG
21812 /* RESET/ABORTS testing ioctls */
21813 	case DKIOCRESET: {
21814 		int	reset_level;
21815 
21816 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
21817 			err = EFAULT;
21818 		} else {
21819 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
21820 			    "reset_level = 0x%lx\n", reset_level);
21821 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
21822 				err = 0;
21823 			} else {
21824 				err = EIO;
21825 			}
21826 		}
21827 		break;
21828 	}
21829 
21830 	case DKIOCABORT:
21831 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
21832 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
21833 			err = 0;
21834 		} else {
21835 			err = EIO;
21836 		}
21837 		break;
21838 #endif
21839 
21840 #ifdef SD_FAULT_INJECTION
21841 /* SDIOC FaultInjection testing ioctls */
21842 	case SDIOCSTART:
21843 	case SDIOCSTOP:
21844 	case SDIOCINSERTPKT:
21845 	case SDIOCINSERTXB:
21846 	case SDIOCINSERTUN:
21847 	case SDIOCINSERTARQ:
21848 	case SDIOCPUSH:
21849 	case SDIOCRETRIEVE:
21850 	case SDIOCRUN:
21851 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
21852 		    "SDIOC detected cmd:0x%X:\n", cmd);
21853 		/* call error generator */
21854 		sd_faultinjection_ioctl(cmd, arg, un);
21855 		err = 0;
21856 		break;
21857 
21858 #endif /* SD_FAULT_INJECTION */
21859 
21860 	case DKIOCFLUSHWRITECACHE:
21861 		{
21862 			struct dk_callback *dkc = (struct dk_callback *)arg;
21863 
21864 			mutex_enter(SD_MUTEX(un));
21865 			if (!un->un_f_sync_cache_supported ||
21866 			    !un->un_f_write_cache_enabled) {
21867 				err = un->un_f_sync_cache_supported ?
21868 					0 : ENOTSUP;
21869 				mutex_exit(SD_MUTEX(un));
21870 				if ((flag & FKIOCTL) && dkc != NULL &&
21871 				    dkc->dkc_callback != NULL) {
21872 					(*dkc->dkc_callback)(dkc->dkc_cookie,
21873 					    err);
21874 					/*
21875 					 * Did callback and reported error.
21876 					 * Since we did a callback, ioctl
21877 					 * should return 0.
21878 					 */
21879 					err = 0;
21880 				}
21881 				break;
21882 			}
21883 			mutex_exit(SD_MUTEX(un));
21884 
21885 			if ((flag & FKIOCTL) && dkc != NULL &&
21886 			    dkc->dkc_callback != NULL) {
21887 				/* async SYNC CACHE request */
21888 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
21889 			} else {
21890 				/* synchronous SYNC CACHE request */
21891 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
21892 			}
21893 		}
21894 		break;
21895 
21896 	case DKIOCGETWCE: {
21897 
21898 		int wce;
21899 
21900 		if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) {
21901 			break;
21902 		}
21903 
21904 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
21905 			err = EFAULT;
21906 		}
21907 		break;
21908 	}
21909 
21910 	case DKIOCSETWCE: {
21911 
21912 		int wce, sync_supported;
21913 
21914 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
21915 			err = EFAULT;
21916 			break;
21917 		}
21918 
21919 		/*
21920 		 * Synchronize multiple threads trying to enable
21921 		 * or disable the cache via the un_f_wcc_cv
21922 		 * condition variable.
21923 		 */
21924 		mutex_enter(SD_MUTEX(un));
21925 
21926 		/*
21927 		 * Don't allow the cache to be enabled if the
21928 		 * config file has it disabled.
21929 		 */
21930 		if (un->un_f_opt_disable_cache && wce) {
21931 			mutex_exit(SD_MUTEX(un));
21932 			err = EINVAL;
21933 			break;
21934 		}
21935 
21936 		/*
21937 		 * Wait for write cache change in progress
21938 		 * bit to be clear before proceeding.
21939 		 */
21940 		while (un->un_f_wcc_inprog)
21941 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
21942 
21943 		un->un_f_wcc_inprog = 1;
21944 
21945 		if (un->un_f_write_cache_enabled && wce == 0) {
21946 			/*
21947 			 * Disable the write cache.  Don't clear
21948 			 * un_f_write_cache_enabled until after
21949 			 * the mode select and flush are complete.
21950 			 */
21951 			sync_supported = un->un_f_sync_cache_supported;
21952 			mutex_exit(SD_MUTEX(un));
21953 			if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE,
21954 			    SD_CACHE_DISABLE)) == 0 && sync_supported) {
21955 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
21956 			}
21957 
21958 			mutex_enter(SD_MUTEX(un));
21959 			if (err == 0) {
21960 				un->un_f_write_cache_enabled = 0;
21961 			}
21962 
21963 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
21964 			/*
21965 			 * Set un_f_write_cache_enabled first, so there is
21966 			 * no window where the cache is enabled, but the
21967 			 * bit says it isn't.
21968 			 */
21969 			un->un_f_write_cache_enabled = 1;
21970 			mutex_exit(SD_MUTEX(un));
21971 
21972 			err = sd_cache_control(un, SD_CACHE_NOCHANGE,
21973 				SD_CACHE_ENABLE);
21974 
21975 			mutex_enter(SD_MUTEX(un));
21976 
21977 			if (err) {
21978 				un->un_f_write_cache_enabled = 0;
21979 			}
21980 		}
21981 
21982 		un->un_f_wcc_inprog = 0;
21983 		cv_broadcast(&un->un_wcc_cv);
21984 		mutex_exit(SD_MUTEX(un));
21985 		break;
21986 	}
21987 
21988 	default:
21989 		err = ENOTTY;
21990 		break;
21991 	}
21992 	mutex_enter(SD_MUTEX(un));
21993 	un->un_ncmds_in_driver--;
21994 	ASSERT(un->un_ncmds_in_driver >= 0);
21995 	mutex_exit(SD_MUTEX(un));
21996 
21997 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
21998 	return (err);
21999 }
22000 
22001 
22002 /*
22003  *    Function: sd_uscsi_ioctl
22004  *
22005  * Description: This routine is the driver entry point for handling USCSI ioctl
22006  *		requests (USCSICMD).
22007  *
22008  *   Arguments: dev	- the device number
22009  *		arg	- user provided scsi command
22010  *		flag	- this argument is a pass through to ddi_copyxxx()
22011  *			  directly from the mode argument of ioctl().
22012  *
22013  * Return Code: code returned by sd_send_scsi_cmd
22014  *		ENXIO
22015  *		EFAULT
22016  *		EAGAIN
22017  */
22018 
22019 static int
22020 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag)
22021 {
22022 #ifdef _MULTI_DATAMODEL
22023 	/*
22024 	 * For use when a 32 bit app makes a call into a
22025 	 * 64 bit ioctl
22026 	 */
22027 	struct uscsi_cmd32	uscsi_cmd_32_for_64;
22028 	struct uscsi_cmd32	*ucmd32 = &uscsi_cmd_32_for_64;
22029 	model_t			model;
22030 #endif /* _MULTI_DATAMODEL */
22031 	struct uscsi_cmd	*scmd = NULL;
22032 	struct sd_lun		*un = NULL;
22033 	enum uio_seg		uioseg;
22034 	char			cdb[CDB_GROUP0];
22035 	int			rval = 0;
22036 
22037 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22038 		return (ENXIO);
22039 	}
22040 
22041 	SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un);
22042 
22043 	scmd = (struct uscsi_cmd *)
22044 	    kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
22045 
22046 #ifdef _MULTI_DATAMODEL
22047 	switch (model = ddi_model_convert_from(flag & FMODELS)) {
22048 	case DDI_MODEL_ILP32:
22049 	{
22050 		if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) {
22051 			rval = EFAULT;
22052 			goto done;
22053 		}
22054 		/*
22055 		 * Convert the ILP32 uscsi data from the
22056 		 * application to LP64 for internal use.
22057 		 */
22058 		uscsi_cmd32touscsi_cmd(ucmd32, scmd);
22059 		break;
22060 	}
22061 	case DDI_MODEL_NONE:
22062 		if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) {
22063 			rval = EFAULT;
22064 			goto done;
22065 		}
22066 		break;
22067 	}
22068 #else /* ! _MULTI_DATAMODEL */
22069 	if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) {
22070 		rval = EFAULT;
22071 		goto done;
22072 	}
22073 #endif /* _MULTI_DATAMODEL */
22074 
22075 	scmd->uscsi_flags &= ~USCSI_NOINTR;
22076 	uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE;
22077 	if (un->un_f_format_in_progress == TRUE) {
22078 		rval = EAGAIN;
22079 		goto done;
22080 	}
22081 
22082 	/*
22083 	 * Gotta do the ddi_copyin() here on the uscsi_cdb so that
22084 	 * we will have a valid cdb[0] to test.
22085 	 */
22086 	if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) &&
22087 	    (cdb[0] == SCMD_FORMAT)) {
22088 		SD_TRACE(SD_LOG_IOCTL, un,
22089 		    "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]);
22090 		mutex_enter(SD_MUTEX(un));
22091 		un->un_f_format_in_progress = TRUE;
22092 		mutex_exit(SD_MUTEX(un));
22093 		rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg,
22094 		    SD_PATH_STANDARD);
22095 		mutex_enter(SD_MUTEX(un));
22096 		un->un_f_format_in_progress = FALSE;
22097 		mutex_exit(SD_MUTEX(un));
22098 	} else {
22099 		SD_TRACE(SD_LOG_IOCTL, un,
22100 		    "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]);
22101 		/*
22102 		 * It's OK to fall into here even if the ddi_copyin()
22103 		 * on the uscsi_cdb above fails, because sd_send_scsi_cmd()
22104 		 * does this same copyin and will return the EFAULT
22105 		 * if it fails.
22106 		 */
22107 		rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg,
22108 		    SD_PATH_STANDARD);
22109 	}
22110 #ifdef _MULTI_DATAMODEL
22111 	switch (model) {
22112 	case DDI_MODEL_ILP32:
22113 		/*
22114 		 * Convert back to ILP32 before copyout to the
22115 		 * application
22116 		 */
22117 		uscsi_cmdtouscsi_cmd32(scmd, ucmd32);
22118 		if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) {
22119 			if (rval != 0) {
22120 				rval = EFAULT;
22121 			}
22122 		}
22123 		break;
22124 	case DDI_MODEL_NONE:
22125 		if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) {
22126 			if (rval != 0) {
22127 				rval = EFAULT;
22128 			}
22129 		}
22130 		break;
22131 	}
22132 #else /* ! _MULTI_DATAMODE */
22133 	if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) {
22134 		if (rval != 0) {
22135 			rval = EFAULT;
22136 		}
22137 	}
22138 #endif /* _MULTI_DATAMODE */
22139 done:
22140 	kmem_free(scmd, sizeof (struct uscsi_cmd));
22141 
22142 	SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un);
22143 
22144 	return (rval);
22145 }
22146 
22147 
22148 /*
22149  *    Function: sd_dkio_ctrl_info
22150  *
22151  * Description: This routine is the driver entry point for handling controller
22152  *		information ioctl requests (DKIOCINFO).
22153  *
22154  *   Arguments: dev  - the device number
22155  *		arg  - pointer to user provided dk_cinfo structure
22156  *		       specifying the controller type and attributes.
22157  *		flag - this argument is a pass through to ddi_copyxxx()
22158  *		       directly from the mode argument of ioctl().
22159  *
22160  * Return Code: 0
22161  *		EFAULT
22162  *		ENXIO
22163  */
22164 
22165 static int
22166 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
22167 {
22168 	struct sd_lun	*un = NULL;
22169 	struct dk_cinfo	*info;
22170 	dev_info_t	*pdip;
22171 	int		lun, tgt;
22172 
22173 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22174 		return (ENXIO);
22175 	}
22176 
22177 	info = (struct dk_cinfo *)
22178 		kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
22179 
22180 	switch (un->un_ctype) {
22181 	case CTYPE_CDROM:
22182 		info->dki_ctype = DKC_CDROM;
22183 		break;
22184 	default:
22185 		info->dki_ctype = DKC_SCSI_CCS;
22186 		break;
22187 	}
22188 	pdip = ddi_get_parent(SD_DEVINFO(un));
22189 	info->dki_cnum = ddi_get_instance(pdip);
22190 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
22191 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
22192 	} else {
22193 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
22194 		    DK_DEVLEN - 1);
22195 	}
22196 
22197 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
22198 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
22199 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
22200 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
22201 
22202 	/* Unit Information */
22203 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
22204 	info->dki_slave = ((tgt << 3) | lun);
22205 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
22206 	    DK_DEVLEN - 1);
22207 	info->dki_flags = DKI_FMTVOL;
22208 	info->dki_partition = SDPART(dev);
22209 
22210 	/* Max Transfer size of this device in blocks */
22211 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
22212 	info->dki_addr = 0;
22213 	info->dki_space = 0;
22214 	info->dki_prio = 0;
22215 	info->dki_vec = 0;
22216 
22217 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
22218 		kmem_free(info, sizeof (struct dk_cinfo));
22219 		return (EFAULT);
22220 	} else {
22221 		kmem_free(info, sizeof (struct dk_cinfo));
22222 		return (0);
22223 	}
22224 }
22225 
22226 
22227 /*
22228  *    Function: sd_get_media_info
22229  *
22230  * Description: This routine is the driver entry point for handling ioctl
22231  *		requests for the media type or command set profile used by the
22232  *		drive to operate on the media (DKIOCGMEDIAINFO).
22233  *
22234  *   Arguments: dev	- the device number
22235  *		arg	- pointer to user provided dk_minfo structure
22236  *			  specifying the media type, logical block size and
22237  *			  drive capacity.
22238  *		flag	- this argument is a pass through to ddi_copyxxx()
22239  *			  directly from the mode argument of ioctl().
22240  *
22241  * Return Code: 0
22242  *		EACCESS
22243  *		EFAULT
22244  *		ENXIO
22245  *		EIO
22246  */
22247 
22248 static int
22249 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
22250 {
22251 	struct sd_lun		*un = NULL;
22252 	struct uscsi_cmd	com;
22253 	struct scsi_inquiry	*sinq;
22254 	struct dk_minfo		media_info;
22255 	u_longlong_t		media_capacity;
22256 	uint64_t		capacity;
22257 	uint_t			lbasize;
22258 	uchar_t			*out_data;
22259 	uchar_t			*rqbuf;
22260 	int			rval = 0;
22261 	int			rtn;
22262 
22263 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
22264 	    (un->un_state == SD_STATE_OFFLINE)) {
22265 		return (ENXIO);
22266 	}
22267 
22268 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n");
22269 
22270 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
22271 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
22272 
22273 	/* Issue a TUR to determine if the drive is ready with media present */
22274 	rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA);
22275 	if (rval == ENXIO) {
22276 		goto done;
22277 	}
22278 
22279 	/* Now get configuration data */
22280 	if (ISCD(un)) {
22281 		media_info.dki_media_type = DK_CDROM;
22282 
22283 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
22284 		if (un->un_f_mmc_cap == TRUE) {
22285 			rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf,
22286 				SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN);
22287 
22288 			if (rtn) {
22289 				/*
22290 				 * Failed for other than an illegal request
22291 				 * or command not supported
22292 				 */
22293 				if ((com.uscsi_status == STATUS_CHECK) &&
22294 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
22295 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
22296 					    (rqbuf[12] != 0x20)) {
22297 						rval = EIO;
22298 						goto done;
22299 					}
22300 				}
22301 			} else {
22302 				/*
22303 				 * The GET CONFIGURATION command succeeded
22304 				 * so set the media type according to the
22305 				 * returned data
22306 				 */
22307 				media_info.dki_media_type = out_data[6];
22308 				media_info.dki_media_type <<= 8;
22309 				media_info.dki_media_type |= out_data[7];
22310 			}
22311 		}
22312 	} else {
22313 		/*
22314 		 * The profile list is not available, so we attempt to identify
22315 		 * the media type based on the inquiry data
22316 		 */
22317 		sinq = un->un_sd->sd_inq;
22318 		if (sinq->inq_qual == 0) {
22319 			/* This is a direct access device */
22320 			media_info.dki_media_type = DK_FIXED_DISK;
22321 
22322 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
22323 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
22324 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
22325 					media_info.dki_media_type = DK_ZIP;
22326 				} else if (
22327 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
22328 					media_info.dki_media_type = DK_JAZ;
22329 				}
22330 			}
22331 		} else {
22332 			/* Not a CD or direct access so return unknown media */
22333 			media_info.dki_media_type = DK_UNKNOWN;
22334 		}
22335 	}
22336 
22337 	/* Now read the capacity so we can provide the lbasize and capacity */
22338 	switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
22339 	    SD_PATH_DIRECT)) {
22340 	case 0:
22341 		break;
22342 	case EACCES:
22343 		rval = EACCES;
22344 		goto done;
22345 	default:
22346 		rval = EIO;
22347 		goto done;
22348 	}
22349 
22350 	media_info.dki_lbsize = lbasize;
22351 	media_capacity = capacity;
22352 
22353 	/*
22354 	 * sd_send_scsi_READ_CAPACITY() reports capacity in
22355 	 * un->un_sys_blocksize chunks. So we need to convert it into
22356 	 * cap.lbasize chunks.
22357 	 */
22358 	media_capacity *= un->un_sys_blocksize;
22359 	media_capacity /= lbasize;
22360 	media_info.dki_capacity = media_capacity;
22361 
22362 	if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) {
22363 		rval = EFAULT;
22364 		/* Put goto. Anybody might add some code below in future */
22365 		goto done;
22366 	}
22367 done:
22368 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
22369 	kmem_free(rqbuf, SENSE_LENGTH);
22370 	return (rval);
22371 }
22372 
22373 
22374 /*
22375  *    Function: sd_dkio_get_geometry
22376  *
22377  * Description: This routine is the driver entry point for handling user
22378  *		requests to get the device geometry (DKIOCGGEOM).
22379  *
22380  *   Arguments: dev  - the device number
22381  *		arg  - pointer to user provided dk_geom structure specifying
22382  *			the controller's notion of the current geometry.
22383  *		flag - this argument is a pass through to ddi_copyxxx()
22384  *		       directly from the mode argument of ioctl().
22385  *		geom_validated - flag indicating if the device geometry has been
22386  *				 previously validated in the sdioctl routine.
22387  *
22388  * Return Code: 0
22389  *		EFAULT
22390  *		ENXIO
22391  *		EIO
22392  */
22393 
22394 static int
22395 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated)
22396 {
22397 	struct sd_lun	*un = NULL;
22398 	struct dk_geom	*tmp_geom = NULL;
22399 	int		rval = 0;
22400 
22401 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22402 		return (ENXIO);
22403 	}
22404 
22405 	if (geom_validated == FALSE) {
22406 		/*
22407 		 * sd_validate_geometry does not spin a disk up
22408 		 * if it was spun down. We need to make sure it
22409 		 * is ready.
22410 		 */
22411 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22412 			return (rval);
22413 		}
22414 		mutex_enter(SD_MUTEX(un));
22415 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
22416 		mutex_exit(SD_MUTEX(un));
22417 	}
22418 	if (rval)
22419 		return (rval);
22420 
22421 	/*
22422 	 * It is possible that un_solaris_size is 0(uninitialized)
22423 	 * after sd_unit_attach. Reservation conflict may cause the
22424 	 * above situation. Thus, the zero check of un_solaris_size
22425 	 * should occur after the sd_validate_geometry() call.
22426 	 */
22427 #if defined(__i386) || defined(__amd64)
22428 	if (un->un_solaris_size == 0) {
22429 		return (EIO);
22430 	}
22431 #endif
22432 
22433 	/*
22434 	 * Make a local copy of the soft state geometry to avoid some potential
22435 	 * race conditions associated with holding the mutex and updating the
22436 	 * write_reinstruct value
22437 	 */
22438 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
22439 	mutex_enter(SD_MUTEX(un));
22440 	bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom));
22441 	mutex_exit(SD_MUTEX(un));
22442 
22443 	if (tmp_geom->dkg_write_reinstruct == 0) {
22444 		tmp_geom->dkg_write_reinstruct =
22445 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
22446 		    sd_rot_delay) / (int)60000);
22447 	}
22448 
22449 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
22450 	    flag);
22451 	if (rval != 0) {
22452 		rval = EFAULT;
22453 	}
22454 
22455 	kmem_free(tmp_geom, sizeof (struct dk_geom));
22456 	return (rval);
22457 
22458 }
22459 
22460 
22461 /*
22462  *    Function: sd_dkio_set_geometry
22463  *
22464  * Description: This routine is the driver entry point for handling user
22465  *		requests to set the device geometry (DKIOCSGEOM). The actual
22466  *		device geometry is not updated, just the driver "notion" of it.
22467  *
22468  *   Arguments: dev  - the device number
22469  *		arg  - pointer to user provided dk_geom structure used to set
22470  *			the controller's notion of the current geometry.
22471  *		flag - this argument is a pass through to ddi_copyxxx()
22472  *		       directly from the mode argument of ioctl().
22473  *
22474  * Return Code: 0
22475  *		EFAULT
22476  *		ENXIO
22477  *		EIO
22478  */
22479 
22480 static int
22481 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag)
22482 {
22483 	struct sd_lun	*un = NULL;
22484 	struct dk_geom	*tmp_geom;
22485 	struct dk_map	*lp;
22486 	int		rval = 0;
22487 	int		i;
22488 
22489 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22490 		return (ENXIO);
22491 	}
22492 
22493 	/*
22494 	 * Make sure there is no reservation conflict on the lun.
22495 	 */
22496 	if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) {
22497 		return (EACCES);
22498 	}
22499 
22500 #if defined(__i386) || defined(__amd64)
22501 	if (un->un_solaris_size == 0) {
22502 		return (EIO);
22503 	}
22504 #endif
22505 
22506 	/*
22507 	 * We need to copy the user specified geometry into local
22508 	 * storage and then update the softstate. We don't want to hold
22509 	 * the mutex and copyin directly from the user to the soft state
22510 	 */
22511 	tmp_geom = (struct dk_geom *)
22512 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
22513 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
22514 	if (rval != 0) {
22515 		kmem_free(tmp_geom, sizeof (struct dk_geom));
22516 		return (EFAULT);
22517 	}
22518 
22519 	mutex_enter(SD_MUTEX(un));
22520 	bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom));
22521 	for (i = 0; i < NDKMAP; i++) {
22522 		lp  = &un->un_map[i];
22523 		un->un_offset[i] =
22524 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
22525 #if defined(__i386) || defined(__amd64)
22526 		un->un_offset[i] += un->un_solaris_offset;
22527 #endif
22528 	}
22529 	un->un_f_geometry_is_valid = FALSE;
22530 	mutex_exit(SD_MUTEX(un));
22531 	kmem_free(tmp_geom, sizeof (struct dk_geom));
22532 
22533 	return (rval);
22534 }
22535 
22536 
22537 /*
22538  *    Function: sd_dkio_get_partition
22539  *
22540  * Description: This routine is the driver entry point for handling user
22541  *		requests to get the partition table (DKIOCGAPART).
22542  *
22543  *   Arguments: dev  - the device number
22544  *		arg  - pointer to user provided dk_allmap structure specifying
22545  *			the controller's notion of the current partition table.
22546  *		flag - this argument is a pass through to ddi_copyxxx()
22547  *		       directly from the mode argument of ioctl().
22548  *		geom_validated - flag indicating if the device geometry has been
22549  *				 previously validated in the sdioctl routine.
22550  *
22551  * Return Code: 0
22552  *		EFAULT
22553  *		ENXIO
22554  *		EIO
22555  */
22556 
22557 static int
22558 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated)
22559 {
22560 	struct sd_lun	*un = NULL;
22561 	int		rval = 0;
22562 	int		size;
22563 
22564 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22565 		return (ENXIO);
22566 	}
22567 
22568 	/*
22569 	 * Make sure the geometry is valid before getting the partition
22570 	 * information.
22571 	 */
22572 	mutex_enter(SD_MUTEX(un));
22573 	if (geom_validated == FALSE) {
22574 		/*
22575 		 * sd_validate_geometry does not spin a disk up
22576 		 * if it was spun down. We need to make sure it
22577 		 * is ready before validating the geometry.
22578 		 */
22579 		mutex_exit(SD_MUTEX(un));
22580 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22581 			return (rval);
22582 		}
22583 		mutex_enter(SD_MUTEX(un));
22584 
22585 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
22586 			mutex_exit(SD_MUTEX(un));
22587 			return (rval);
22588 		}
22589 	}
22590 	mutex_exit(SD_MUTEX(un));
22591 
22592 	/*
22593 	 * It is possible that un_solaris_size is 0(uninitialized)
22594 	 * after sd_unit_attach. Reservation conflict may cause the
22595 	 * above situation. Thus, the zero check of un_solaris_size
22596 	 * should occur after the sd_validate_geometry() call.
22597 	 */
22598 #if defined(__i386) || defined(__amd64)
22599 	if (un->un_solaris_size == 0) {
22600 		return (EIO);
22601 	}
22602 #endif
22603 
22604 #ifdef _MULTI_DATAMODEL
22605 	switch (ddi_model_convert_from(flag & FMODELS)) {
22606 	case DDI_MODEL_ILP32: {
22607 		struct dk_map32 dk_map32[NDKMAP];
22608 		int		i;
22609 
22610 		for (i = 0; i < NDKMAP; i++) {
22611 			dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno;
22612 			dk_map32[i].dkl_nblk  = un->un_map[i].dkl_nblk;
22613 		}
22614 		size = NDKMAP * sizeof (struct dk_map32);
22615 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
22616 		if (rval != 0) {
22617 			rval = EFAULT;
22618 		}
22619 		break;
22620 	}
22621 	case DDI_MODEL_NONE:
22622 		size = NDKMAP * sizeof (struct dk_map);
22623 		rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
22624 		if (rval != 0) {
22625 			rval = EFAULT;
22626 		}
22627 		break;
22628 	}
22629 #else /* ! _MULTI_DATAMODEL */
22630 	size = NDKMAP * sizeof (struct dk_map);
22631 	rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
22632 	if (rval != 0) {
22633 		rval = EFAULT;
22634 	}
22635 #endif /* _MULTI_DATAMODEL */
22636 	return (rval);
22637 }
22638 
22639 
22640 /*
22641  *    Function: sd_dkio_set_partition
22642  *
22643  * Description: This routine is the driver entry point for handling user
22644  *		requests to set the partition table (DKIOCSAPART). The actual
22645  *		device partition is not updated.
22646  *
22647  *   Arguments: dev  - the device number
22648  *		arg  - pointer to user provided dk_allmap structure used to set
22649  *			the controller's notion of the partition table.
22650  *		flag - this argument is a pass through to ddi_copyxxx()
22651  *		       directly from the mode argument of ioctl().
22652  *
22653  * Return Code: 0
22654  *		EINVAL
22655  *		EFAULT
22656  *		ENXIO
22657  *		EIO
22658  */
22659 
22660 static int
22661 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag)
22662 {
22663 	struct sd_lun	*un = NULL;
22664 	struct dk_map	dk_map[NDKMAP];
22665 	struct dk_map	*lp;
22666 	int		rval = 0;
22667 	int		size;
22668 	int		i;
22669 #if defined(_SUNOS_VTOC_16)
22670 	struct dkl_partition	*vp;
22671 #endif
22672 
22673 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22674 		return (ENXIO);
22675 	}
22676 
22677 	/*
22678 	 * Set the map for all logical partitions.  We lock
22679 	 * the priority just to make sure an interrupt doesn't
22680 	 * come in while the map is half updated.
22681 	 */
22682 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size))
22683 	mutex_enter(SD_MUTEX(un));
22684 	if (un->un_blockcount > DK_MAX_BLOCKS) {
22685 		mutex_exit(SD_MUTEX(un));
22686 		return (ENOTSUP);
22687 	}
22688 	mutex_exit(SD_MUTEX(un));
22689 
22690 	/*
22691 	 * Make sure there is no reservation conflict on the lun.
22692 	 */
22693 	if (sd_send_scsi_TEST_UNIT_READY(un, 0) == EACCES) {
22694 		return (EACCES);
22695 	}
22696 
22697 #if defined(__i386) || defined(__amd64)
22698 	if (un->un_solaris_size == 0) {
22699 		return (EIO);
22700 	}
22701 #endif
22702 
22703 #ifdef _MULTI_DATAMODEL
22704 	switch (ddi_model_convert_from(flag & FMODELS)) {
22705 	case DDI_MODEL_ILP32: {
22706 		struct dk_map32 dk_map32[NDKMAP];
22707 
22708 		size = NDKMAP * sizeof (struct dk_map32);
22709 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
22710 		if (rval != 0) {
22711 			return (EFAULT);
22712 		}
22713 		for (i = 0; i < NDKMAP; i++) {
22714 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
22715 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
22716 		}
22717 		break;
22718 	}
22719 	case DDI_MODEL_NONE:
22720 		size = NDKMAP * sizeof (struct dk_map);
22721 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
22722 		if (rval != 0) {
22723 			return (EFAULT);
22724 		}
22725 		break;
22726 	}
22727 #else /* ! _MULTI_DATAMODEL */
22728 	size = NDKMAP * sizeof (struct dk_map);
22729 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
22730 	if (rval != 0) {
22731 		return (EFAULT);
22732 	}
22733 #endif /* _MULTI_DATAMODEL */
22734 
22735 	mutex_enter(SD_MUTEX(un));
22736 	/* Note: The size used in this bcopy is set based upon the data model */
22737 	bcopy(dk_map, un->un_map, size);
22738 #if defined(_SUNOS_VTOC_16)
22739 	vp = (struct dkl_partition *)&(un->un_vtoc);
22740 #endif	/* defined(_SUNOS_VTOC_16) */
22741 	for (i = 0; i < NDKMAP; i++) {
22742 		lp  = &un->un_map[i];
22743 		un->un_offset[i] =
22744 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
22745 #if defined(_SUNOS_VTOC_16)
22746 		vp->p_start = un->un_offset[i];
22747 		vp->p_size = lp->dkl_nblk;
22748 		vp++;
22749 #endif	/* defined(_SUNOS_VTOC_16) */
22750 #if defined(__i386) || defined(__amd64)
22751 		un->un_offset[i] += un->un_solaris_offset;
22752 #endif
22753 	}
22754 	mutex_exit(SD_MUTEX(un));
22755 	return (rval);
22756 }
22757 
22758 
22759 /*
22760  *    Function: sd_dkio_get_vtoc
22761  *
22762  * Description: This routine is the driver entry point for handling user
22763  *		requests to get the current volume table of contents
22764  *		(DKIOCGVTOC).
22765  *
22766  *   Arguments: dev  - the device number
22767  *		arg  - pointer to user provided vtoc structure specifying
22768  *			the current vtoc.
22769  *		flag - this argument is a pass through to ddi_copyxxx()
22770  *		       directly from the mode argument of ioctl().
22771  *		geom_validated - flag indicating if the device geometry has been
22772  *				 previously validated in the sdioctl routine.
22773  *
22774  * Return Code: 0
22775  *		EFAULT
22776  *		ENXIO
22777  *		EIO
22778  */
22779 
22780 static int
22781 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated)
22782 {
22783 	struct sd_lun	*un = NULL;
22784 #if defined(_SUNOS_VTOC_8)
22785 	struct vtoc	user_vtoc;
22786 #endif	/* defined(_SUNOS_VTOC_8) */
22787 	int		rval = 0;
22788 
22789 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22790 		return (ENXIO);
22791 	}
22792 
22793 	mutex_enter(SD_MUTEX(un));
22794 	if (geom_validated == FALSE) {
22795 		/*
22796 		 * sd_validate_geometry does not spin a disk up
22797 		 * if it was spun down. We need to make sure it
22798 		 * is ready.
22799 		 */
22800 		mutex_exit(SD_MUTEX(un));
22801 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22802 			return (rval);
22803 		}
22804 		mutex_enter(SD_MUTEX(un));
22805 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
22806 			mutex_exit(SD_MUTEX(un));
22807 			return (rval);
22808 		}
22809 	}
22810 
22811 #if defined(_SUNOS_VTOC_8)
22812 	sd_build_user_vtoc(un, &user_vtoc);
22813 	mutex_exit(SD_MUTEX(un));
22814 
22815 #ifdef _MULTI_DATAMODEL
22816 	switch (ddi_model_convert_from(flag & FMODELS)) {
22817 	case DDI_MODEL_ILP32: {
22818 		struct vtoc32 user_vtoc32;
22819 
22820 		vtoctovtoc32(user_vtoc, user_vtoc32);
22821 		if (ddi_copyout(&user_vtoc32, (void *)arg,
22822 		    sizeof (struct vtoc32), flag)) {
22823 			return (EFAULT);
22824 		}
22825 		break;
22826 	}
22827 
22828 	case DDI_MODEL_NONE:
22829 		if (ddi_copyout(&user_vtoc, (void *)arg,
22830 		    sizeof (struct vtoc), flag)) {
22831 			return (EFAULT);
22832 		}
22833 		break;
22834 	}
22835 #else /* ! _MULTI_DATAMODEL */
22836 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
22837 		return (EFAULT);
22838 	}
22839 #endif /* _MULTI_DATAMODEL */
22840 
22841 #elif defined(_SUNOS_VTOC_16)
22842 	mutex_exit(SD_MUTEX(un));
22843 
22844 #ifdef _MULTI_DATAMODEL
22845 	/*
22846 	 * The un_vtoc structure is a "struct dk_vtoc"  which is always
22847 	 * 32-bit to maintain compatibility with existing on-disk
22848 	 * structures.  Thus, we need to convert the structure when copying
22849 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
22850 	 * program.  If the target is a 32-bit program, then no conversion
22851 	 * is necessary.
22852 	 */
22853 	/* LINTED: logical expression always true: op "||" */
22854 	ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32));
22855 	switch (ddi_model_convert_from(flag & FMODELS)) {
22856 	case DDI_MODEL_ILP32:
22857 		if (ddi_copyout(&(un->un_vtoc), (void *)arg,
22858 		    sizeof (un->un_vtoc), flag)) {
22859 			return (EFAULT);
22860 		}
22861 		break;
22862 
22863 	case DDI_MODEL_NONE: {
22864 		struct vtoc user_vtoc;
22865 
22866 		vtoc32tovtoc(un->un_vtoc, user_vtoc);
22867 		if (ddi_copyout(&user_vtoc, (void *)arg,
22868 		    sizeof (struct vtoc), flag)) {
22869 			return (EFAULT);
22870 		}
22871 		break;
22872 	}
22873 	}
22874 #else /* ! _MULTI_DATAMODEL */
22875 	if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc),
22876 	    flag)) {
22877 		return (EFAULT);
22878 	}
22879 #endif /* _MULTI_DATAMODEL */
22880 #else
22881 #error "No VTOC format defined."
22882 #endif
22883 
22884 	return (rval);
22885 }
22886 
22887 static int
22888 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag)
22889 {
22890 	struct sd_lun	*un = NULL;
22891 	dk_efi_t	user_efi;
22892 	int		rval = 0;
22893 	void		*buffer;
22894 
22895 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
22896 		return (ENXIO);
22897 
22898 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
22899 		return (EFAULT);
22900 
22901 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
22902 
22903 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
22904 	    (user_efi.dki_length > un->un_max_xfer_size))
22905 		return (EINVAL);
22906 
22907 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
22908 	rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length,
22909 	    user_efi.dki_lba, SD_PATH_DIRECT);
22910 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
22911 	    user_efi.dki_length, flag) != 0)
22912 		rval = EFAULT;
22913 
22914 	kmem_free(buffer, user_efi.dki_length);
22915 	return (rval);
22916 }
22917 
22918 /*
22919  *    Function: sd_build_user_vtoc
22920  *
22921  * Description: This routine populates a pass by reference variable with the
22922  *		current volume table of contents.
22923  *
22924  *   Arguments: un - driver soft state (unit) structure
22925  *		user_vtoc - pointer to vtoc structure to be populated
22926  */
22927 
22928 static void
22929 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
22930 {
22931 	struct dk_map2		*lpart;
22932 	struct dk_map		*lmap;
22933 	struct partition	*vpart;
22934 	int			nblks;
22935 	int			i;
22936 
22937 	ASSERT(mutex_owned(SD_MUTEX(un)));
22938 
22939 	/*
22940 	 * Return vtoc structure fields in the provided VTOC area, addressed
22941 	 * by *vtoc.
22942 	 */
22943 	bzero(user_vtoc, sizeof (struct vtoc));
22944 	user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0];
22945 	user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1];
22946 	user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2];
22947 	user_vtoc->v_sanity	= VTOC_SANE;
22948 	user_vtoc->v_version	= un->un_vtoc.v_version;
22949 	bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
22950 	user_vtoc->v_sectorsz = un->un_sys_blocksize;
22951 	user_vtoc->v_nparts = un->un_vtoc.v_nparts;
22952 	bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved,
22953 	    sizeof (un->un_vtoc.v_reserved));
22954 	/*
22955 	 * Convert partitioning information.
22956 	 *
22957 	 * Note the conversion from starting cylinder number
22958 	 * to starting sector number.
22959 	 */
22960 	lmap = un->un_map;
22961 	lpart = (struct dk_map2 *)un->un_vtoc.v_part;
22962 	vpart = user_vtoc->v_part;
22963 
22964 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
22965 
22966 	for (i = 0; i < V_NUMPAR; i++) {
22967 		vpart->p_tag	= lpart->p_tag;
22968 		vpart->p_flag	= lpart->p_flag;
22969 		vpart->p_start	= lmap->dkl_cylno * nblks;
22970 		vpart->p_size	= lmap->dkl_nblk;
22971 		lmap++;
22972 		lpart++;
22973 		vpart++;
22974 
22975 		/* (4364927) */
22976 		user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i];
22977 	}
22978 
22979 	bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
22980 }
22981 
22982 static int
22983 sd_dkio_partition(dev_t dev, caddr_t arg, int flag)
22984 {
22985 	struct sd_lun		*un = NULL;
22986 	struct partition64	p64;
22987 	int			rval = 0;
22988 	uint_t			nparts;
22989 	efi_gpe_t		*partitions;
22990 	efi_gpt_t		*buffer;
22991 	diskaddr_t		gpe_lba;
22992 
22993 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22994 		return (ENXIO);
22995 	}
22996 
22997 	if (ddi_copyin((const void *)arg, &p64,
22998 	    sizeof (struct partition64), flag)) {
22999 		return (EFAULT);
23000 	}
23001 
23002 	buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
23003 	rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE,
23004 		1, SD_PATH_DIRECT);
23005 	if (rval != 0)
23006 		goto done_error;
23007 
23008 	sd_swap_efi_gpt(buffer);
23009 
23010 	if ((rval = sd_validate_efi(buffer)) != 0)
23011 		goto done_error;
23012 
23013 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
23014 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
23015 	if (p64.p_partno > nparts) {
23016 		/* couldn't find it */
23017 		rval = ESRCH;
23018 		goto done_error;
23019 	}
23020 	/*
23021 	 * if we're dealing with a partition that's out of the normal
23022 	 * 16K block, adjust accordingly
23023 	 */
23024 	gpe_lba += p64.p_partno / sizeof (efi_gpe_t);
23025 	rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE,
23026 			gpe_lba, SD_PATH_DIRECT);
23027 	if (rval) {
23028 		goto done_error;
23029 	}
23030 	partitions = (efi_gpe_t *)buffer;
23031 
23032 	sd_swap_efi_gpe(nparts, partitions);
23033 
23034 	partitions += p64.p_partno;
23035 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
23036 	    sizeof (struct uuid));
23037 	p64.p_start = partitions->efi_gpe_StartingLBA;
23038 	p64.p_size = partitions->efi_gpe_EndingLBA -
23039 			p64.p_start + 1;
23040 
23041 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
23042 		rval = EFAULT;
23043 
23044 done_error:
23045 	kmem_free(buffer, EFI_MIN_ARRAY_SIZE);
23046 	return (rval);
23047 }
23048 
23049 
23050 /*
23051  *    Function: sd_dkio_set_vtoc
23052  *
23053  * Description: This routine is the driver entry point for handling user
23054  *		requests to set the current volume table of contents
23055  *		(DKIOCSVTOC).
23056  *
23057  *   Arguments: dev  - the device number
23058  *		arg  - pointer to user provided vtoc structure used to set the
23059  *			current vtoc.
23060  *		flag - this argument is a pass through to ddi_copyxxx()
23061  *		       directly from the mode argument of ioctl().
23062  *
23063  * Return Code: 0
23064  *		EFAULT
23065  *		ENXIO
23066  *		EINVAL
23067  *		ENOTSUP
23068  */
23069 
23070 static int
23071 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag)
23072 {
23073 	struct sd_lun	*un = NULL;
23074 	struct vtoc	user_vtoc;
23075 	int		rval = 0;
23076 
23077 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23078 		return (ENXIO);
23079 	}
23080 
23081 #if defined(__i386) || defined(__amd64)
23082 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
23083 		return (EINVAL);
23084 	}
23085 #endif
23086 
23087 #ifdef _MULTI_DATAMODEL
23088 	switch (ddi_model_convert_from(flag & FMODELS)) {
23089 	case DDI_MODEL_ILP32: {
23090 		struct vtoc32 user_vtoc32;
23091 
23092 		if (ddi_copyin((const void *)arg, &user_vtoc32,
23093 		    sizeof (struct vtoc32), flag)) {
23094 			return (EFAULT);
23095 		}
23096 		vtoc32tovtoc(user_vtoc32, user_vtoc);
23097 		break;
23098 	}
23099 
23100 	case DDI_MODEL_NONE:
23101 		if (ddi_copyin((const void *)arg, &user_vtoc,
23102 		    sizeof (struct vtoc), flag)) {
23103 			return (EFAULT);
23104 		}
23105 		break;
23106 	}
23107 #else /* ! _MULTI_DATAMODEL */
23108 	if (ddi_copyin((const void *)arg, &user_vtoc,
23109 	    sizeof (struct vtoc), flag)) {
23110 		return (EFAULT);
23111 	}
23112 #endif /* _MULTI_DATAMODEL */
23113 
23114 	mutex_enter(SD_MUTEX(un));
23115 	if (un->un_blockcount > DK_MAX_BLOCKS) {
23116 		mutex_exit(SD_MUTEX(un));
23117 		return (ENOTSUP);
23118 	}
23119 	if (un->un_g.dkg_ncyl == 0) {
23120 		mutex_exit(SD_MUTEX(un));
23121 		return (EINVAL);
23122 	}
23123 
23124 	mutex_exit(SD_MUTEX(un));
23125 	sd_clear_efi(un);
23126 	ddi_remove_minor_node(SD_DEVINFO(un), "wd");
23127 	ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw");
23128 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h",
23129 	    S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23130 	    un->un_node_type, NULL);
23131 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw",
23132 	    S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23133 	    un->un_node_type, NULL);
23134 	mutex_enter(SD_MUTEX(un));
23135 
23136 	if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) {
23137 		if ((rval = sd_write_label(dev)) == 0) {
23138 			if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT))
23139 			    != 0) {
23140 				SD_ERROR(SD_LOG_IOCTL_DKIO, un,
23141 				    "sd_dkio_set_vtoc: "
23142 				    "Failed validate geometry\n");
23143 			}
23144 		}
23145 	}
23146 
23147 	/*
23148 	 * If sd_build_label_vtoc, or sd_write_label failed above write the
23149 	 * devid anyway, what can it hurt? Also preserve the device id by
23150 	 * writing to the disk acyl for the case where a devid has been
23151 	 * fabricated.
23152 	 */
23153 	if (un->un_f_devid_supported &&
23154 	    (un->un_f_opt_fab_devid == TRUE)) {
23155 		if (un->un_devid == NULL) {
23156 			sd_register_devid(un, SD_DEVINFO(un),
23157 			    SD_TARGET_IS_UNRESERVED);
23158 		} else {
23159 			/*
23160 			 * The device id for this disk has been
23161 			 * fabricated. Fabricated device id's are
23162 			 * managed by storing them in the last 2
23163 			 * available sectors on the drive. The device
23164 			 * id must be preserved by writing it back out
23165 			 * to this location.
23166 			 */
23167 			if (sd_write_deviceid(un) != 0) {
23168 				ddi_devid_free(un->un_devid);
23169 				un->un_devid = NULL;
23170 			}
23171 		}
23172 	}
23173 	mutex_exit(SD_MUTEX(un));
23174 	return (rval);
23175 }
23176 
23177 
23178 /*
23179  *    Function: sd_build_label_vtoc
23180  *
23181  * Description: This routine updates the driver soft state current volume table
23182  *		of contents based on a user specified vtoc.
23183  *
23184  *   Arguments: un - driver soft state (unit) structure
23185  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
23186  *			    to update the driver soft state.
23187  *
23188  * Return Code: 0
23189  *		EINVAL
23190  */
23191 
23192 static int
23193 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
23194 {
23195 	struct dk_map		*lmap;
23196 	struct partition	*vpart;
23197 	int			nblks;
23198 #if defined(_SUNOS_VTOC_8)
23199 	int			ncyl;
23200 	struct dk_map2		*lpart;
23201 #endif	/* defined(_SUNOS_VTOC_8) */
23202 	int			i;
23203 
23204 	ASSERT(mutex_owned(SD_MUTEX(un)));
23205 
23206 	/* Sanity-check the vtoc */
23207 	if (user_vtoc->v_sanity != VTOC_SANE ||
23208 	    user_vtoc->v_sectorsz != un->un_sys_blocksize ||
23209 	    user_vtoc->v_nparts != V_NUMPAR) {
23210 		return (EINVAL);
23211 	}
23212 
23213 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
23214 	if (nblks == 0) {
23215 		return (EINVAL);
23216 	}
23217 
23218 #if defined(_SUNOS_VTOC_8)
23219 	vpart = user_vtoc->v_part;
23220 	for (i = 0; i < V_NUMPAR; i++) {
23221 		if ((vpart->p_start % nblks) != 0) {
23222 			return (EINVAL);
23223 		}
23224 		ncyl = vpart->p_start / nblks;
23225 		ncyl += vpart->p_size / nblks;
23226 		if ((vpart->p_size % nblks) != 0) {
23227 			ncyl++;
23228 		}
23229 		if (ncyl > (int)un->un_g.dkg_ncyl) {
23230 			return (EINVAL);
23231 		}
23232 		vpart++;
23233 	}
23234 #endif	/* defined(_SUNOS_VTOC_8) */
23235 
23236 	/* Put appropriate vtoc structure fields into the disk label */
23237 #if defined(_SUNOS_VTOC_16)
23238 	/*
23239 	 * The vtoc is always a 32bit data structure to maintain the
23240 	 * on-disk format. Convert "in place" instead of bcopying it.
23241 	 */
23242 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc))));
23243 
23244 	/*
23245 	 * in the 16-slice vtoc, starting sectors are expressed in
23246 	 * numbers *relative* to the start of the Solaris fdisk partition.
23247 	 */
23248 	lmap = un->un_map;
23249 	vpart = user_vtoc->v_part;
23250 
23251 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
23252 		lmap->dkl_cylno = vpart->p_start / nblks;
23253 		lmap->dkl_nblk = vpart->p_size;
23254 	}
23255 
23256 #elif defined(_SUNOS_VTOC_8)
23257 
23258 	un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
23259 	un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
23260 	un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
23261 
23262 	un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
23263 	un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version;
23264 
23265 	bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL);
23266 
23267 	un->un_vtoc.v_nparts = user_vtoc->v_nparts;
23268 
23269 	bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved,
23270 	    sizeof (un->un_vtoc.v_reserved));
23271 
23272 	/*
23273 	 * Note the conversion from starting sector number
23274 	 * to starting cylinder number.
23275 	 * Return error if division results in a remainder.
23276 	 */
23277 	lmap = un->un_map;
23278 	lpart = un->un_vtoc.v_part;
23279 	vpart = user_vtoc->v_part;
23280 
23281 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
23282 		lpart->p_tag  = vpart->p_tag;
23283 		lpart->p_flag = vpart->p_flag;
23284 		lmap->dkl_cylno = vpart->p_start / nblks;
23285 		lmap->dkl_nblk = vpart->p_size;
23286 
23287 		lmap++;
23288 		lpart++;
23289 		vpart++;
23290 
23291 		/* (4387723) */
23292 #ifdef _LP64
23293 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
23294 			un->un_vtoc.v_timestamp[i] = TIME32_MAX;
23295 		} else {
23296 			un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
23297 		}
23298 #else
23299 		un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
23300 #endif
23301 	}
23302 
23303 	bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
23304 #else
23305 #error "No VTOC format defined."
23306 #endif
23307 	return (0);
23308 }
23309 
23310 /*
23311  *    Function: sd_clear_efi
23312  *
23313  * Description: This routine clears all EFI labels.
23314  *
23315  *   Arguments: un - driver soft state (unit) structure
23316  *
23317  * Return Code: void
23318  */
23319 
23320 static void
23321 sd_clear_efi(struct sd_lun *un)
23322 {
23323 	efi_gpt_t	*gpt;
23324 	uint_t		lbasize;
23325 	uint64_t	cap;
23326 	int rval;
23327 
23328 	ASSERT(!mutex_owned(SD_MUTEX(un)));
23329 
23330 	gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP);
23331 
23332 	if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) {
23333 		goto done;
23334 	}
23335 
23336 	sd_swap_efi_gpt(gpt);
23337 	rval = sd_validate_efi(gpt);
23338 	if (rval == 0) {
23339 		/* clear primary */
23340 		bzero(gpt, sizeof (efi_gpt_t));
23341 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1,
23342 			SD_PATH_DIRECT))) {
23343 			SD_INFO(SD_LOG_IO_PARTITION, un,
23344 				"sd_clear_efi: clear primary label failed\n");
23345 		}
23346 	}
23347 	/* the backup */
23348 	rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
23349 	    SD_PATH_DIRECT);
23350 	if (rval) {
23351 		goto done;
23352 	}
23353 	/*
23354 	 * The MMC standard allows READ CAPACITY to be
23355 	 * inaccurate by a bounded amount (in the interest of
23356 	 * response latency).  As a result, failed READs are
23357 	 * commonplace (due to the reading of metadata and not
23358 	 * data). Depending on the per-Vendor/drive Sense data,
23359 	 * the failed READ can cause many (unnecessary) retries.
23360 	 */
23361 	if ((rval = sd_send_scsi_READ(un, gpt, lbasize,
23362 	    cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY :
23363 		SD_PATH_DIRECT)) != 0) {
23364 		goto done;
23365 	}
23366 	sd_swap_efi_gpt(gpt);
23367 	rval = sd_validate_efi(gpt);
23368 	if (rval == 0) {
23369 		/* clear backup */
23370 		SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n",
23371 			cap-1);
23372 		bzero(gpt, sizeof (efi_gpt_t));
23373 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE,
23374 		    cap-1, SD_PATH_DIRECT))) {
23375 			SD_INFO(SD_LOG_IO_PARTITION, un,
23376 				"sd_clear_efi: clear backup label failed\n");
23377 		}
23378 	}
23379 
23380 done:
23381 	kmem_free(gpt, sizeof (efi_gpt_t));
23382 }
23383 
23384 /*
23385  *    Function: sd_set_vtoc
23386  *
23387  * Description: This routine writes data to the appropriate positions
23388  *
23389  *   Arguments: un - driver soft state (unit) structure
23390  *              dkl  - the data to be written
23391  *
23392  * Return: void
23393  */
23394 
23395 static int
23396 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl)
23397 {
23398 	void			*shadow_buf;
23399 	uint_t			label_addr;
23400 	int			sec;
23401 	int			blk;
23402 	int			head;
23403 	int			cyl;
23404 	int			rval;
23405 
23406 #if defined(__i386) || defined(__amd64)
23407 	label_addr = un->un_solaris_offset + DK_LABEL_LOC;
23408 #else
23409 	/* Write the primary label at block 0 of the solaris partition. */
23410 	label_addr = 0;
23411 #endif
23412 
23413 	if (NOT_DEVBSIZE(un)) {
23414 		shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP);
23415 		/*
23416 		 * Read the target's first block.
23417 		 */
23418 		if ((rval = sd_send_scsi_READ(un, shadow_buf,
23419 		    un->un_tgt_blocksize, label_addr,
23420 		    SD_PATH_STANDARD)) != 0) {
23421 			goto exit;
23422 		}
23423 		/*
23424 		 * Copy the contents of the label into the shadow buffer
23425 		 * which is of the size of target block size.
23426 		 */
23427 		bcopy(dkl, shadow_buf, sizeof (struct dk_label));
23428 	}
23429 
23430 	/* Write the primary label */
23431 	if (NOT_DEVBSIZE(un)) {
23432 		rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize,
23433 		    label_addr, SD_PATH_STANDARD);
23434 	} else {
23435 		rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
23436 		    label_addr, SD_PATH_STANDARD);
23437 	}
23438 	if (rval != 0) {
23439 		return (rval);
23440 	}
23441 
23442 	/*
23443 	 * Calculate where the backup labels go.  They are always on
23444 	 * the last alternate cylinder, but some older drives put them
23445 	 * on head 2 instead of the last head.	They are always on the
23446 	 * first 5 odd sectors of the appropriate track.
23447 	 *
23448 	 * We have no choice at this point, but to believe that the
23449 	 * disk label is valid.	 Use the geometry of the disk
23450 	 * as described in the label.
23451 	 */
23452 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
23453 	head = dkl->dkl_nhead - 1;
23454 
23455 	/*
23456 	 * Write and verify the backup labels. Make sure we don't try to
23457 	 * write past the last cylinder.
23458 	 */
23459 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
23460 		blk = (daddr_t)(
23461 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
23462 		    (head * dkl->dkl_nsect) + sec);
23463 #if defined(__i386) || defined(__amd64)
23464 		blk += un->un_solaris_offset;
23465 #endif
23466 		if (NOT_DEVBSIZE(un)) {
23467 			uint64_t	tblk;
23468 			/*
23469 			 * Need to read the block first for read modify write.
23470 			 */
23471 			tblk = (uint64_t)blk;
23472 			blk = (int)((tblk * un->un_sys_blocksize) /
23473 			    un->un_tgt_blocksize);
23474 			if ((rval = sd_send_scsi_READ(un, shadow_buf,
23475 			    un->un_tgt_blocksize, blk,
23476 			    SD_PATH_STANDARD)) != 0) {
23477 				goto exit;
23478 			}
23479 			/*
23480 			 * Modify the shadow buffer with the label.
23481 			 */
23482 			bcopy(dkl, shadow_buf, sizeof (struct dk_label));
23483 			rval = sd_send_scsi_WRITE(un, shadow_buf,
23484 			    un->un_tgt_blocksize, blk, SD_PATH_STANDARD);
23485 		} else {
23486 			rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
23487 			    blk, SD_PATH_STANDARD);
23488 			SD_INFO(SD_LOG_IO_PARTITION, un,
23489 			"sd_set_vtoc: wrote backup label %d\n", blk);
23490 		}
23491 		if (rval != 0) {
23492 			goto exit;
23493 		}
23494 	}
23495 exit:
23496 	if (NOT_DEVBSIZE(un)) {
23497 		kmem_free(shadow_buf, un->un_tgt_blocksize);
23498 	}
23499 	return (rval);
23500 }
23501 
23502 /*
23503  *    Function: sd_clear_vtoc
23504  *
23505  * Description: This routine clears out the VTOC labels.
23506  *
23507  *   Arguments: un - driver soft state (unit) structure
23508  *
23509  * Return: void
23510  */
23511 
23512 static void
23513 sd_clear_vtoc(struct sd_lun *un)
23514 {
23515 	struct dk_label		*dkl;
23516 
23517 	mutex_exit(SD_MUTEX(un));
23518 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
23519 	mutex_enter(SD_MUTEX(un));
23520 	/*
23521 	 * sd_set_vtoc uses these fields in order to figure out
23522 	 * where to overwrite the backup labels
23523 	 */
23524 	dkl->dkl_apc    = un->un_g.dkg_apc;
23525 	dkl->dkl_ncyl   = un->un_g.dkg_ncyl;
23526 	dkl->dkl_acyl   = un->un_g.dkg_acyl;
23527 	dkl->dkl_nhead  = un->un_g.dkg_nhead;
23528 	dkl->dkl_nsect  = un->un_g.dkg_nsect;
23529 	mutex_exit(SD_MUTEX(un));
23530 	(void) sd_set_vtoc(un, dkl);
23531 	kmem_free(dkl, sizeof (struct dk_label));
23532 
23533 	mutex_enter(SD_MUTEX(un));
23534 }
23535 
23536 /*
23537  *    Function: sd_write_label
23538  *
23539  * Description: This routine will validate and write the driver soft state vtoc
23540  *		contents to the device.
23541  *
23542  *   Arguments: dev - the device number
23543  *
23544  * Return Code: the code returned by sd_send_scsi_cmd()
23545  *		0
23546  *		EINVAL
23547  *		ENXIO
23548  *		ENOMEM
23549  */
23550 
23551 static int
23552 sd_write_label(dev_t dev)
23553 {
23554 	struct sd_lun		*un;
23555 	struct dk_label		*dkl;
23556 	short			sum;
23557 	short			*sp;
23558 	int			i;
23559 	int			rval;
23560 
23561 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
23562 	    (un->un_state == SD_STATE_OFFLINE)) {
23563 		return (ENXIO);
23564 	}
23565 	ASSERT(mutex_owned(SD_MUTEX(un)));
23566 	mutex_exit(SD_MUTEX(un));
23567 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
23568 	mutex_enter(SD_MUTEX(un));
23569 
23570 	bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
23571 	dkl->dkl_rpm	= un->un_g.dkg_rpm;
23572 	dkl->dkl_pcyl	= un->un_g.dkg_pcyl;
23573 	dkl->dkl_apc	= un->un_g.dkg_apc;
23574 	dkl->dkl_intrlv = un->un_g.dkg_intrlv;
23575 	dkl->dkl_ncyl	= un->un_g.dkg_ncyl;
23576 	dkl->dkl_acyl	= un->un_g.dkg_acyl;
23577 	dkl->dkl_nhead	= un->un_g.dkg_nhead;
23578 	dkl->dkl_nsect	= un->un_g.dkg_nsect;
23579 
23580 #if defined(_SUNOS_VTOC_8)
23581 	dkl->dkl_obs1	= un->un_g.dkg_obs1;
23582 	dkl->dkl_obs2	= un->un_g.dkg_obs2;
23583 	dkl->dkl_obs3	= un->un_g.dkg_obs3;
23584 	for (i = 0; i < NDKMAP; i++) {
23585 		dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno;
23586 		dkl->dkl_map[i].dkl_nblk  = un->un_map[i].dkl_nblk;
23587 	}
23588 	bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
23589 #elif defined(_SUNOS_VTOC_16)
23590 	dkl->dkl_skew	= un->un_dkg_skew;
23591 #else
23592 #error "No VTOC format defined."
23593 #endif
23594 
23595 	dkl->dkl_magic			= DKL_MAGIC;
23596 	dkl->dkl_write_reinstruct	= un->un_g.dkg_write_reinstruct;
23597 	dkl->dkl_read_reinstruct	= un->un_g.dkg_read_reinstruct;
23598 
23599 	/* Construct checksum for the new disk label */
23600 	sum = 0;
23601 	sp = (short *)dkl;
23602 	i = sizeof (struct dk_label) / sizeof (short);
23603 	while (i--) {
23604 		sum ^= *sp++;
23605 	}
23606 	dkl->dkl_cksum = sum;
23607 
23608 	mutex_exit(SD_MUTEX(un));
23609 
23610 	rval = sd_set_vtoc(un, dkl);
23611 exit:
23612 	kmem_free(dkl, sizeof (struct dk_label));
23613 	mutex_enter(SD_MUTEX(un));
23614 	return (rval);
23615 }
23616 
23617 static int
23618 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag)
23619 {
23620 	struct sd_lun	*un = NULL;
23621 	dk_efi_t	user_efi;
23622 	int		rval = 0;
23623 	void		*buffer;
23624 
23625 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
23626 		return (ENXIO);
23627 
23628 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
23629 		return (EFAULT);
23630 
23631 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
23632 
23633 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
23634 	    (user_efi.dki_length > un->un_max_xfer_size))
23635 		return (EINVAL);
23636 
23637 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
23638 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
23639 		rval = EFAULT;
23640 	} else {
23641 		/*
23642 		 * let's clear the vtoc labels and clear the softstate
23643 		 * vtoc.
23644 		 */
23645 		mutex_enter(SD_MUTEX(un));
23646 		if (un->un_vtoc.v_sanity == VTOC_SANE) {
23647 			SD_TRACE(SD_LOG_IO_PARTITION, un,
23648 				"sd_dkio_set_efi: CLEAR VTOC\n");
23649 			sd_clear_vtoc(un);
23650 			bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
23651 			mutex_exit(SD_MUTEX(un));
23652 			ddi_remove_minor_node(SD_DEVINFO(un), "h");
23653 			ddi_remove_minor_node(SD_DEVINFO(un), "h,raw");
23654 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd",
23655 			    S_IFBLK,
23656 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23657 			    un->un_node_type, NULL);
23658 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw",
23659 			    S_IFCHR,
23660 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23661 			    un->un_node_type, NULL);
23662 		} else
23663 			mutex_exit(SD_MUTEX(un));
23664 		rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length,
23665 		    user_efi.dki_lba, SD_PATH_DIRECT);
23666 		if (rval == 0) {
23667 			mutex_enter(SD_MUTEX(un));
23668 			un->un_f_geometry_is_valid = FALSE;
23669 			mutex_exit(SD_MUTEX(un));
23670 		}
23671 	}
23672 	kmem_free(buffer, user_efi.dki_length);
23673 	return (rval);
23674 }
23675 
23676 /*
23677  *    Function: sd_dkio_get_mboot
23678  *
23679  * Description: This routine is the driver entry point for handling user
23680  *		requests to get the current device mboot (DKIOCGMBOOT)
23681  *
23682  *   Arguments: dev  - the device number
23683  *		arg  - pointer to user provided mboot structure specifying
23684  *			the current mboot.
23685  *		flag - this argument is a pass through to ddi_copyxxx()
23686  *		       directly from the mode argument of ioctl().
23687  *
23688  * Return Code: 0
23689  *		EINVAL
23690  *		EFAULT
23691  *		ENXIO
23692  */
23693 
23694 static int
23695 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag)
23696 {
23697 	struct sd_lun	*un;
23698 	struct mboot	*mboot;
23699 	int		rval;
23700 	size_t		buffer_size;
23701 
23702 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
23703 	    (un->un_state == SD_STATE_OFFLINE)) {
23704 		return (ENXIO);
23705 	}
23706 
23707 	if (!un->un_f_mboot_supported || arg == NULL) {
23708 		return (EINVAL);
23709 	}
23710 
23711 	/*
23712 	 * Read the mboot block, located at absolute block 0 on the target.
23713 	 */
23714 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot));
23715 
23716 	SD_TRACE(SD_LOG_IO_PARTITION, un,
23717 	    "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
23718 
23719 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
23720 	if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0,
23721 	    SD_PATH_STANDARD)) == 0) {
23722 		if (ddi_copyout(mboot, (void *)arg,
23723 		    sizeof (struct mboot), flag) != 0) {
23724 			rval = EFAULT;
23725 		}
23726 	}
23727 	kmem_free(mboot, buffer_size);
23728 	return (rval);
23729 }
23730 
23731 
23732 /*
23733  *    Function: sd_dkio_set_mboot
23734  *
23735  * Description: This routine is the driver entry point for handling user
23736  *		requests to validate and set the device master boot
23737  *		(DKIOCSMBOOT).
23738  *
23739  *   Arguments: dev  - the device number
23740  *		arg  - pointer to user provided mboot structure used to set the
23741  *			master boot.
23742  *		flag - this argument is a pass through to ddi_copyxxx()
23743  *		       directly from the mode argument of ioctl().
23744  *
23745  * Return Code: 0
23746  *		EINVAL
23747  *		EFAULT
23748  *		ENXIO
23749  */
23750 
23751 static int
23752 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag)
23753 {
23754 	struct sd_lun	*un = NULL;
23755 	struct mboot	*mboot = NULL;
23756 	int		rval;
23757 	ushort_t	magic;
23758 
23759 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23760 		return (ENXIO);
23761 	}
23762 
23763 	ASSERT(!mutex_owned(SD_MUTEX(un)));
23764 
23765 	if (!un->un_f_mboot_supported) {
23766 		return (EINVAL);
23767 	}
23768 
23769 	if (arg == NULL) {
23770 		return (EINVAL);
23771 	}
23772 
23773 	mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP);
23774 
23775 	if (ddi_copyin((const void *)arg, mboot,
23776 	    sizeof (struct mboot), flag) != 0) {
23777 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23778 		return (EFAULT);
23779 	}
23780 
23781 	/* Is this really a master boot record? */
23782 	magic = LE_16(mboot->signature);
23783 	if (magic != MBB_MAGIC) {
23784 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23785 		return (EINVAL);
23786 	}
23787 
23788 	rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0,
23789 	    SD_PATH_STANDARD);
23790 
23791 	mutex_enter(SD_MUTEX(un));
23792 #if defined(__i386) || defined(__amd64)
23793 	if (rval == 0) {
23794 		/*
23795 		 * mboot has been written successfully.
23796 		 * update the fdisk and vtoc tables in memory
23797 		 */
23798 		rval = sd_update_fdisk_and_vtoc(un);
23799 		if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) {
23800 			mutex_exit(SD_MUTEX(un));
23801 			kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23802 			return (rval);
23803 		}
23804 	}
23805 
23806 	/*
23807 	 * If the mboot write fails, write the devid anyway, what can it hurt?
23808 	 * Also preserve the device id by writing to the disk acyl for the case
23809 	 * where a devid has been fabricated.
23810 	 */
23811 	if (un->un_f_devid_supported && un->un_f_opt_fab_devid) {
23812 		if (un->un_devid == NULL) {
23813 			sd_register_devid(un, SD_DEVINFO(un),
23814 			    SD_TARGET_IS_UNRESERVED);
23815 		} else {
23816 			/*
23817 			 * The device id for this disk has been
23818 			 * fabricated. Fabricated device id's are
23819 			 * managed by storing them in the last 2
23820 			 * available sectors on the drive. The device
23821 			 * id must be preserved by writing it back out
23822 			 * to this location.
23823 			 */
23824 			if (sd_write_deviceid(un) != 0) {
23825 				ddi_devid_free(un->un_devid);
23826 				un->un_devid = NULL;
23827 			}
23828 		}
23829 	}
23830 
23831 #ifdef __lock_lint
23832 	sd_setup_default_geometry(un);
23833 #endif
23834 
23835 #else
23836 	if (rval == 0) {
23837 		/*
23838 		 * mboot has been written successfully.
23839 		 * set up the default geometry and VTOC
23840 		 */
23841 		if (un->un_blockcount <= DK_MAX_BLOCKS)
23842 			sd_setup_default_geometry(un);
23843 	}
23844 #endif
23845 	mutex_exit(SD_MUTEX(un));
23846 	kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23847 	return (rval);
23848 }
23849 
23850 
23851 /*
23852  *    Function: sd_setup_default_geometry
23853  *
23854  * Description: This local utility routine sets the default geometry as part of
23855  *		setting the device mboot.
23856  *
23857  *   Arguments: un - driver soft state (unit) structure
23858  *
23859  * Note: This may be redundant with sd_build_default_label.
23860  */
23861 
23862 static void
23863 sd_setup_default_geometry(struct sd_lun *un)
23864 {
23865 	/* zero out the soft state geometry and partition table. */
23866 	bzero(&un->un_g, sizeof (struct dk_geom));
23867 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
23868 	bzero(un->un_map, NDKMAP * (sizeof (struct dk_map)));
23869 	un->un_asciilabel[0] = '\0';
23870 
23871 	/*
23872 	 * For the rpm, we use the minimum for the disk.
23873 	 * For the head, cyl and number of sector per track,
23874 	 * if the capacity <= 1GB, head = 64, sect = 32.
23875 	 * else head = 255, sect 63
23876 	 * Note: the capacity should be equal to C*H*S values.
23877 	 * This will cause some truncation of size due to
23878 	 * round off errors. For CD-ROMs, this truncation can
23879 	 * have adverse side effects, so returning ncyl and
23880 	 * nhead as 1. The nsect will overflow for most of
23881 	 * CD-ROMs as nsect is of type ushort.
23882 	 */
23883 	if (ISCD(un)) {
23884 		un->un_g.dkg_ncyl = 1;
23885 		un->un_g.dkg_nhead = 1;
23886 		un->un_g.dkg_nsect = un->un_blockcount;
23887 	} else {
23888 		if (un->un_blockcount <= 0x1000) {
23889 			/* Needed for unlabeled SCSI floppies. */
23890 			un->un_g.dkg_nhead = 2;
23891 			un->un_g.dkg_ncyl = 80;
23892 			un->un_g.dkg_pcyl = 80;
23893 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
23894 		} else if (un->un_blockcount <= 0x200000) {
23895 			un->un_g.dkg_nhead = 64;
23896 			un->un_g.dkg_nsect = 32;
23897 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
23898 		} else {
23899 			un->un_g.dkg_nhead = 255;
23900 			un->un_g.dkg_nsect = 63;
23901 			un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63);
23902 		}
23903 		un->un_blockcount = un->un_g.dkg_ncyl *
23904 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
23905 	}
23906 	un->un_g.dkg_acyl = 0;
23907 	un->un_g.dkg_bcyl = 0;
23908 	un->un_g.dkg_intrlv = 1;
23909 	un->un_g.dkg_rpm = 200;
23910 	un->un_g.dkg_read_reinstruct = 0;
23911 	un->un_g.dkg_write_reinstruct = 0;
23912 	if (un->un_g.dkg_pcyl == 0) {
23913 		un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl;
23914 	}
23915 
23916 	un->un_map['a'-'a'].dkl_cylno = 0;
23917 	un->un_map['a'-'a'].dkl_nblk = un->un_blockcount;
23918 	un->un_map['c'-'a'].dkl_cylno = 0;
23919 	un->un_map['c'-'a'].dkl_nblk = un->un_blockcount;
23920 	un->un_f_geometry_is_valid = FALSE;
23921 }
23922 
23923 
23924 #if defined(__i386) || defined(__amd64)
23925 /*
23926  *    Function: sd_update_fdisk_and_vtoc
23927  *
23928  * Description: This local utility routine updates the device fdisk and vtoc
23929  *		as part of setting the device mboot.
23930  *
23931  *   Arguments: un - driver soft state (unit) structure
23932  *
23933  * Return Code: 0 for success or errno-type return code.
23934  *
23935  *    Note:x86: This looks like a duplicate of sd_validate_geometry(), but
23936  *		these did exist seperately in x86 sd.c!!!
23937  */
23938 
23939 static int
23940 sd_update_fdisk_and_vtoc(struct sd_lun *un)
23941 {
23942 	static char	labelstring[128];
23943 	static char	buf[256];
23944 	char		*label = 0;
23945 	int		count;
23946 	int		label_rc = 0;
23947 	int		gvalid = un->un_f_geometry_is_valid;
23948 	int		fdisk_rval;
23949 	int		lbasize;
23950 	int		capacity;
23951 
23952 	ASSERT(mutex_owned(SD_MUTEX(un)));
23953 
23954 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
23955 		return (EINVAL);
23956 	}
23957 
23958 	if (un->un_f_blockcount_is_valid == FALSE) {
23959 		return (EINVAL);
23960 	}
23961 
23962 #if defined(_SUNOS_VTOC_16)
23963 	/*
23964 	 * Set up the "whole disk" fdisk partition; this should always
23965 	 * exist, regardless of whether the disk contains an fdisk table
23966 	 * or vtoc.
23967 	 */
23968 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
23969 	un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount;
23970 #endif	/* defined(_SUNOS_VTOC_16) */
23971 
23972 	/*
23973 	 * copy the lbasize and capacity so that if they're
23974 	 * reset while we're not holding the SD_MUTEX(un), we will
23975 	 * continue to use valid values after the SD_MUTEX(un) is
23976 	 * reacquired.
23977 	 */
23978 	lbasize  = un->un_tgt_blocksize;
23979 	capacity = un->un_blockcount;
23980 
23981 	/*
23982 	 * refresh the logical and physical geometry caches.
23983 	 * (data from mode sense format/rigid disk geometry pages,
23984 	 * and scsi_ifgetcap("geometry").
23985 	 */
23986 	sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT);
23987 
23988 	/*
23989 	 * Only DIRECT ACCESS devices will have Sun labels.
23990 	 * CD's supposedly have a Sun label, too
23991 	 */
23992 	if (un->un_f_vtoc_label_supported) {
23993 		fdisk_rval = sd_read_fdisk(un, capacity, lbasize,
23994 		    SD_PATH_DIRECT);
23995 		if (fdisk_rval == SD_CMD_FAILURE) {
23996 			ASSERT(mutex_owned(SD_MUTEX(un)));
23997 			return (EIO);
23998 		}
23999 
24000 		if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) {
24001 			ASSERT(mutex_owned(SD_MUTEX(un)));
24002 			return (EACCES);
24003 		}
24004 
24005 		if (un->un_solaris_size <= DK_LABEL_LOC) {
24006 			/*
24007 			 * Found fdisk table but no Solaris partition entry,
24008 			 * so don't call sd_uselabel() and don't create
24009 			 * a default label.
24010 			 */
24011 			label_rc = 0;
24012 			un->un_f_geometry_is_valid = TRUE;
24013 			goto no_solaris_partition;
24014 		}
24015 
24016 #if defined(_SUNOS_VTOC_8)
24017 		label = (char *)un->un_asciilabel;
24018 #elif defined(_SUNOS_VTOC_16)
24019 		label = (char *)un->un_vtoc.v_asciilabel;
24020 #else
24021 #error "No VTOC format defined."
24022 #endif
24023 	} else if (capacity < 0) {
24024 		ASSERT(mutex_owned(SD_MUTEX(un)));
24025 		return (EINVAL);
24026 	}
24027 
24028 	/*
24029 	 * For Removable media We reach here if we have found a
24030 	 * SOLARIS PARTITION.
24031 	 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS
24032 	 * PARTITION has changed from the previous one, hence we will setup a
24033 	 * default VTOC in this case.
24034 	 */
24035 	if (un->un_f_geometry_is_valid == FALSE) {
24036 		sd_build_default_label(un);
24037 		label_rc = 0;
24038 	}
24039 
24040 no_solaris_partition:
24041 	if ((!un->un_f_has_removable_media ||
24042 	    (un->un_f_has_removable_media &&
24043 	    un->un_mediastate == DKIO_EJECTED)) &&
24044 		(un->un_state == SD_STATE_NORMAL && !gvalid)) {
24045 		/*
24046 		 * Print out a message indicating who and what we are.
24047 		 * We do this only when we happen to really validate the
24048 		 * geometry. We may call sd_validate_geometry() at other
24049 		 * times, ioctl()'s like Get VTOC in which case we
24050 		 * don't want to print the label.
24051 		 * If the geometry is valid, print the label string,
24052 		 * else print vendor and product info, if available
24053 		 */
24054 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
24055 			SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label);
24056 		} else {
24057 			mutex_enter(&sd_label_mutex);
24058 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
24059 			    labelstring);
24060 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
24061 			    &labelstring[64]);
24062 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
24063 			    labelstring, &labelstring[64]);
24064 			if (un->un_f_blockcount_is_valid == TRUE) {
24065 				(void) sprintf(&buf[strlen(buf)],
24066 				    ", %" PRIu64 " %u byte blocks\n",
24067 				    un->un_blockcount,
24068 				    un->un_tgt_blocksize);
24069 			} else {
24070 				(void) sprintf(&buf[strlen(buf)],
24071 				    ", (unknown capacity)\n");
24072 			}
24073 			SD_INFO(SD_LOG_IOCTL_DKIO, un, buf);
24074 			mutex_exit(&sd_label_mutex);
24075 		}
24076 	}
24077 
24078 #if defined(_SUNOS_VTOC_16)
24079 	/*
24080 	 * If we have valid geometry, set up the remaining fdisk partitions.
24081 	 * Note that dkl_cylno is not used for the fdisk map entries, so
24082 	 * we set it to an entirely bogus value.
24083 	 */
24084 	for (count = 0; count < FD_NUMPART; count++) {
24085 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
24086 		un->un_map[FDISK_P1 + count].dkl_nblk =
24087 		    un->un_fmap[count].fmap_nblk;
24088 		un->un_offset[FDISK_P1 + count] =
24089 		    un->un_fmap[count].fmap_start;
24090 	}
24091 #endif
24092 
24093 	for (count = 0; count < NDKMAP; count++) {
24094 #if defined(_SUNOS_VTOC_8)
24095 		struct dk_map *lp  = &un->un_map[count];
24096 		un->un_offset[count] =
24097 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
24098 #elif defined(_SUNOS_VTOC_16)
24099 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
24100 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
24101 #else
24102 #error "No VTOC format defined."
24103 #endif
24104 	}
24105 
24106 	ASSERT(mutex_owned(SD_MUTEX(un)));
24107 	return (label_rc);
24108 }
24109 #endif
24110 
24111 
24112 /*
24113  *    Function: sd_check_media
24114  *
24115  * Description: This utility routine implements the functionality for the
24116  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
24117  *		driver state changes from that specified by the user
24118  *		(inserted or ejected). For example, if the user specifies
24119  *		DKIO_EJECTED and the current media state is inserted this
24120  *		routine will immediately return DKIO_INSERTED. However, if the
24121  *		current media state is not inserted the user thread will be
24122  *		blocked until the drive state changes. If DKIO_NONE is specified
24123  *		the user thread will block until a drive state change occurs.
24124  *
24125  *   Arguments: dev  - the device number
24126  *		state  - user pointer to a dkio_state, updated with the current
24127  *			drive state at return.
24128  *
24129  * Return Code: ENXIO
24130  *		EIO
24131  *		EAGAIN
24132  *		EINTR
24133  */
24134 
24135 static int
24136 sd_check_media(dev_t dev, enum dkio_state state)
24137 {
24138 	struct sd_lun		*un = NULL;
24139 	enum dkio_state		prev_state;
24140 	opaque_t		token = NULL;
24141 	int			rval = 0;
24142 
24143 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24144 		return (ENXIO);
24145 	}
24146 
24147 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
24148 
24149 	mutex_enter(SD_MUTEX(un));
24150 
24151 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
24152 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
24153 
24154 	prev_state = un->un_mediastate;
24155 
24156 	/* is there anything to do? */
24157 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
24158 		/*
24159 		 * submit the request to the scsi_watch service;
24160 		 * scsi_media_watch_cb() does the real work
24161 		 */
24162 		mutex_exit(SD_MUTEX(un));
24163 
24164 		/*
24165 		 * This change handles the case where a scsi watch request is
24166 		 * added to a device that is powered down. To accomplish this
24167 		 * we power up the device before adding the scsi watch request,
24168 		 * since the scsi watch sends a TUR directly to the device
24169 		 * which the device cannot handle if it is powered down.
24170 		 */
24171 		if (sd_pm_entry(un) != DDI_SUCCESS) {
24172 			mutex_enter(SD_MUTEX(un));
24173 			goto done;
24174 		}
24175 
24176 		token = scsi_watch_request_submit(SD_SCSI_DEVP(un),
24177 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
24178 		    (caddr_t)dev);
24179 
24180 		sd_pm_exit(un);
24181 
24182 		mutex_enter(SD_MUTEX(un));
24183 		if (token == NULL) {
24184 			rval = EAGAIN;
24185 			goto done;
24186 		}
24187 
24188 		/*
24189 		 * This is a special case IOCTL that doesn't return
24190 		 * until the media state changes. Routine sdpower
24191 		 * knows about and handles this so don't count it
24192 		 * as an active cmd in the driver, which would
24193 		 * keep the device busy to the pm framework.
24194 		 * If the count isn't decremented the device can't
24195 		 * be powered down.
24196 		 */
24197 		un->un_ncmds_in_driver--;
24198 		ASSERT(un->un_ncmds_in_driver >= 0);
24199 
24200 		/*
24201 		 * if a prior request had been made, this will be the same
24202 		 * token, as scsi_watch was designed that way.
24203 		 */
24204 		un->un_swr_token = token;
24205 		un->un_specified_mediastate = state;
24206 
24207 		/*
24208 		 * now wait for media change
24209 		 * we will not be signalled unless mediastate == state but it is
24210 		 * still better to test for this condition, since there is a
24211 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
24212 		 */
24213 		SD_TRACE(SD_LOG_COMMON, un,
24214 		    "sd_check_media: waiting for media state change\n");
24215 		while (un->un_mediastate == state) {
24216 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
24217 				SD_TRACE(SD_LOG_COMMON, un,
24218 				    "sd_check_media: waiting for media state "
24219 				    "was interrupted\n");
24220 				un->un_ncmds_in_driver++;
24221 				rval = EINTR;
24222 				goto done;
24223 			}
24224 			SD_TRACE(SD_LOG_COMMON, un,
24225 			    "sd_check_media: received signal, state=%x\n",
24226 			    un->un_mediastate);
24227 		}
24228 		/*
24229 		 * Inc the counter to indicate the device once again
24230 		 * has an active outstanding cmd.
24231 		 */
24232 		un->un_ncmds_in_driver++;
24233 	}
24234 
24235 	/* invalidate geometry */
24236 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
24237 		sr_ejected(un);
24238 	}
24239 
24240 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
24241 		uint64_t	capacity;
24242 		uint_t		lbasize;
24243 
24244 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
24245 		mutex_exit(SD_MUTEX(un));
24246 		/*
24247 		 * Since the following routines use SD_PATH_DIRECT, we must
24248 		 * call PM directly before the upcoming disk accesses. This
24249 		 * may cause the disk to be power/spin up.
24250 		 */
24251 
24252 		if (sd_pm_entry(un) == DDI_SUCCESS) {
24253 			rval = sd_send_scsi_READ_CAPACITY(un,
24254 			    &capacity,
24255 			    &lbasize, SD_PATH_DIRECT);
24256 			if (rval != 0) {
24257 				sd_pm_exit(un);
24258 				mutex_enter(SD_MUTEX(un));
24259 				goto done;
24260 			}
24261 		} else {
24262 			rval = EIO;
24263 			mutex_enter(SD_MUTEX(un));
24264 			goto done;
24265 		}
24266 		mutex_enter(SD_MUTEX(un));
24267 
24268 		sd_update_block_info(un, lbasize, capacity);
24269 
24270 		un->un_f_geometry_is_valid	= FALSE;
24271 		(void) sd_validate_geometry(un, SD_PATH_DIRECT);
24272 
24273 		mutex_exit(SD_MUTEX(un));
24274 		rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
24275 		    SD_PATH_DIRECT);
24276 		sd_pm_exit(un);
24277 
24278 		mutex_enter(SD_MUTEX(un));
24279 	}
24280 done:
24281 	un->un_f_watcht_stopped = FALSE;
24282 	if (un->un_swr_token) {
24283 		/*
24284 		 * Use of this local token and the mutex ensures that we avoid
24285 		 * some race conditions associated with terminating the
24286 		 * scsi watch.
24287 		 */
24288 		token = un->un_swr_token;
24289 		un->un_swr_token = (opaque_t)NULL;
24290 		mutex_exit(SD_MUTEX(un));
24291 		(void) scsi_watch_request_terminate(token,
24292 		    SCSI_WATCH_TERMINATE_WAIT);
24293 		mutex_enter(SD_MUTEX(un));
24294 	}
24295 
24296 	/*
24297 	 * Update the capacity kstat value, if no media previously
24298 	 * (capacity kstat is 0) and a media has been inserted
24299 	 * (un_f_blockcount_is_valid == TRUE)
24300 	 */
24301 	if (un->un_errstats) {
24302 		struct sd_errstats	*stp = NULL;
24303 
24304 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
24305 		if ((stp->sd_capacity.value.ui64 == 0) &&
24306 		    (un->un_f_blockcount_is_valid == TRUE)) {
24307 			stp->sd_capacity.value.ui64 =
24308 			    (uint64_t)((uint64_t)un->un_blockcount *
24309 			    un->un_sys_blocksize);
24310 		}
24311 	}
24312 	mutex_exit(SD_MUTEX(un));
24313 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
24314 	return (rval);
24315 }
24316 
24317 
24318 /*
24319  *    Function: sd_delayed_cv_broadcast
24320  *
24321  * Description: Delayed cv_broadcast to allow for target to recover from media
24322  *		insertion.
24323  *
24324  *   Arguments: arg - driver soft state (unit) structure
24325  */
24326 
24327 static void
24328 sd_delayed_cv_broadcast(void *arg)
24329 {
24330 	struct sd_lun *un = arg;
24331 
24332 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
24333 
24334 	mutex_enter(SD_MUTEX(un));
24335 	un->un_dcvb_timeid = NULL;
24336 	cv_broadcast(&un->un_state_cv);
24337 	mutex_exit(SD_MUTEX(un));
24338 }
24339 
24340 
24341 /*
24342  *    Function: sd_media_watch_cb
24343  *
24344  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
24345  *		routine processes the TUR sense data and updates the driver
24346  *		state if a transition has occurred. The user thread
24347  *		(sd_check_media) is then signalled.
24348  *
24349  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24350  *			among multiple watches that share this callback function
24351  *		resultp - scsi watch facility result packet containing scsi
24352  *			  packet, status byte and sense data
24353  *
24354  * Return Code: 0 for success, -1 for failure
24355  */
24356 
24357 static int
24358 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24359 {
24360 	struct sd_lun			*un;
24361 	struct scsi_status		*statusp = resultp->statusp;
24362 	struct scsi_extended_sense	*sensep = resultp->sensep;
24363 	enum dkio_state			state = DKIO_NONE;
24364 	dev_t				dev = (dev_t)arg;
24365 	uchar_t				actual_sense_length;
24366 
24367 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24368 		return (-1);
24369 	}
24370 	actual_sense_length = resultp->actual_sense_length;
24371 
24372 	mutex_enter(SD_MUTEX(un));
24373 	SD_TRACE(SD_LOG_COMMON, un,
24374 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
24375 	    *((char *)statusp), (void *)sensep, actual_sense_length);
24376 
24377 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
24378 		un->un_mediastate = DKIO_DEV_GONE;
24379 		cv_broadcast(&un->un_state_cv);
24380 		mutex_exit(SD_MUTEX(un));
24381 
24382 		return (0);
24383 	}
24384 
24385 	/*
24386 	 * If there was a check condition then sensep points to valid sense data
24387 	 * If status was not a check condition but a reservation or busy status
24388 	 * then the new state is DKIO_NONE
24389 	 */
24390 	if (sensep != NULL) {
24391 		SD_INFO(SD_LOG_COMMON, un,
24392 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24393 		    sensep->es_key, sensep->es_add_code, sensep->es_qual_code);
24394 		/* This routine only uses up to 13 bytes of sense data. */
24395 		if (actual_sense_length >= 13) {
24396 			if (sensep->es_key == KEY_UNIT_ATTENTION) {
24397 				if (sensep->es_add_code == 0x28) {
24398 					state = DKIO_INSERTED;
24399 				}
24400 			} else {
24401 				/*
24402 				 * if 02/04/02  means that the host
24403 				 * should send start command. Explicitly
24404 				 * leave the media state as is
24405 				 * (inserted) as the media is inserted
24406 				 * and host has stopped device for PM
24407 				 * reasons. Upon next true read/write
24408 				 * to this media will bring the
24409 				 * device to the right state good for
24410 				 * media access.
24411 				 */
24412 				if ((sensep->es_key == KEY_NOT_READY) &&
24413 				    (sensep->es_add_code == 0x3a)) {
24414 					state = DKIO_EJECTED;
24415 				}
24416 
24417 				/*
24418 				 * If the drivge is busy with an operation
24419 				 * or long write, keep the media in an
24420 				 * inserted state.
24421 				 */
24422 
24423 				if ((sensep->es_key == KEY_NOT_READY) &&
24424 				    (sensep->es_add_code == 0x04) &&
24425 				    ((sensep->es_qual_code == 0x02) ||
24426 				    (sensep->es_qual_code == 0x07) ||
24427 				    (sensep->es_qual_code == 0x08))) {
24428 					state = DKIO_INSERTED;
24429 				}
24430 			}
24431 		}
24432 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
24433 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24434 		state = DKIO_INSERTED;
24435 	}
24436 
24437 	SD_TRACE(SD_LOG_COMMON, un,
24438 	    "sd_media_watch_cb: state=%x, specified=%x\n",
24439 	    state, un->un_specified_mediastate);
24440 
24441 	/*
24442 	 * now signal the waiting thread if this is *not* the specified state;
24443 	 * delay the signal if the state is DKIO_INSERTED to allow the target
24444 	 * to recover
24445 	 */
24446 	if (state != un->un_specified_mediastate) {
24447 		un->un_mediastate = state;
24448 		if (state == DKIO_INSERTED) {
24449 			/*
24450 			 * delay the signal to give the drive a chance
24451 			 * to do what it apparently needs to do
24452 			 */
24453 			SD_TRACE(SD_LOG_COMMON, un,
24454 			    "sd_media_watch_cb: delayed cv_broadcast\n");
24455 			if (un->un_dcvb_timeid == NULL) {
24456 				un->un_dcvb_timeid =
24457 				    timeout(sd_delayed_cv_broadcast, un,
24458 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24459 			}
24460 		} else {
24461 			SD_TRACE(SD_LOG_COMMON, un,
24462 			    "sd_media_watch_cb: immediate cv_broadcast\n");
24463 			cv_broadcast(&un->un_state_cv);
24464 		}
24465 	}
24466 	mutex_exit(SD_MUTEX(un));
24467 	return (0);
24468 }
24469 
24470 
24471 /*
24472  *    Function: sd_dkio_get_temp
24473  *
24474  * Description: This routine is the driver entry point for handling ioctl
24475  *		requests to get the disk temperature.
24476  *
24477  *   Arguments: dev  - the device number
24478  *		arg  - pointer to user provided dk_temperature structure.
24479  *		flag - this argument is a pass through to ddi_copyxxx()
24480  *		       directly from the mode argument of ioctl().
24481  *
24482  * Return Code: 0
24483  *		EFAULT
24484  *		ENXIO
24485  *		EAGAIN
24486  */
24487 
24488 static int
24489 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24490 {
24491 	struct sd_lun		*un = NULL;
24492 	struct dk_temperature	*dktemp = NULL;
24493 	uchar_t			*temperature_page;
24494 	int			rval = 0;
24495 	int			path_flag = SD_PATH_STANDARD;
24496 
24497 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24498 		return (ENXIO);
24499 	}
24500 
24501 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24502 
24503 	/* copyin the disk temp argument to get the user flags */
24504 	if (ddi_copyin((void *)arg, dktemp,
24505 	    sizeof (struct dk_temperature), flag) != 0) {
24506 		rval = EFAULT;
24507 		goto done;
24508 	}
24509 
24510 	/* Initialize the temperature to invalid. */
24511 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24512 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24513 
24514 	/*
24515 	 * Note: Investigate removing the "bypass pm" semantic.
24516 	 * Can we just bypass PM always?
24517 	 */
24518 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24519 		path_flag = SD_PATH_DIRECT;
24520 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24521 		mutex_enter(&un->un_pm_mutex);
24522 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24523 			/*
24524 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24525 			 * in low power mode, we can not wake it up, Need to
24526 			 * return EAGAIN.
24527 			 */
24528 			mutex_exit(&un->un_pm_mutex);
24529 			rval = EAGAIN;
24530 			goto done;
24531 		} else {
24532 			/*
24533 			 * Indicate to PM the device is busy. This is required
24534 			 * to avoid a race - i.e. the ioctl is issuing a
24535 			 * command and the pm framework brings down the device
24536 			 * to low power mode (possible power cut-off on some
24537 			 * platforms).
24538 			 */
24539 			mutex_exit(&un->un_pm_mutex);
24540 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24541 				rval = EAGAIN;
24542 				goto done;
24543 			}
24544 		}
24545 	}
24546 
24547 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24548 
24549 	if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page,
24550 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) {
24551 		goto done2;
24552 	}
24553 
24554 	/*
24555 	 * For the current temperature verify that the parameter length is 0x02
24556 	 * and the parameter code is 0x00
24557 	 */
24558 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24559 	    (temperature_page[5] == 0x00)) {
24560 		if (temperature_page[9] == 0xFF) {
24561 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24562 		} else {
24563 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24564 		}
24565 	}
24566 
24567 	/*
24568 	 * For the reference temperature verify that the parameter
24569 	 * length is 0x02 and the parameter code is 0x01
24570 	 */
24571 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24572 	    (temperature_page[11] == 0x01)) {
24573 		if (temperature_page[15] == 0xFF) {
24574 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24575 		} else {
24576 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24577 		}
24578 	}
24579 
24580 	/* Do the copyout regardless of the temperature commands status. */
24581 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24582 	    flag) != 0) {
24583 		rval = EFAULT;
24584 	}
24585 
24586 done2:
24587 	if (path_flag == SD_PATH_DIRECT) {
24588 		sd_pm_exit(un);
24589 	}
24590 
24591 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24592 done:
24593 	if (dktemp != NULL) {
24594 		kmem_free(dktemp, sizeof (struct dk_temperature));
24595 	}
24596 
24597 	return (rval);
24598 }
24599 
24600 
24601 /*
24602  *    Function: sd_log_page_supported
24603  *
24604  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24605  *		supported log pages.
24606  *
24607  *   Arguments: un -
24608  *		log_page -
24609  *
24610  * Return Code: -1 - on error (log sense is optional and may not be supported).
24611  *		0  - log page not found.
24612  *  		1  - log page found.
24613  */
24614 
24615 static int
24616 sd_log_page_supported(struct sd_lun *un, int log_page)
24617 {
24618 	uchar_t *log_page_data;
24619 	int	i;
24620 	int	match = 0;
24621 	int	log_size;
24622 
24623 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24624 
24625 	if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0,
24626 	    SD_PATH_DIRECT) != 0) {
24627 		SD_ERROR(SD_LOG_COMMON, un,
24628 		    "sd_log_page_supported: failed log page retrieval\n");
24629 		kmem_free(log_page_data, 0xFF);
24630 		return (-1);
24631 	}
24632 	log_size = log_page_data[3];
24633 
24634 	/*
24635 	 * The list of supported log pages start from the fourth byte. Check
24636 	 * until we run out of log pages or a match is found.
24637 	 */
24638 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24639 		if (log_page_data[i] == log_page) {
24640 			match++;
24641 		}
24642 	}
24643 	kmem_free(log_page_data, 0xFF);
24644 	return (match);
24645 }
24646 
24647 
24648 /*
24649  *    Function: sd_mhdioc_failfast
24650  *
24651  * Description: This routine is the driver entry point for handling ioctl
24652  *		requests to enable/disable the multihost failfast option.
24653  *		(MHIOCENFAILFAST)
24654  *
24655  *   Arguments: dev	- the device number
24656  *		arg	- user specified probing interval.
24657  *		flag	- this argument is a pass through to ddi_copyxxx()
24658  *			  directly from the mode argument of ioctl().
24659  *
24660  * Return Code: 0
24661  *		EFAULT
24662  *		ENXIO
24663  */
24664 
24665 static int
24666 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24667 {
24668 	struct sd_lun	*un = NULL;
24669 	int		mh_time;
24670 	int		rval = 0;
24671 
24672 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24673 		return (ENXIO);
24674 	}
24675 
24676 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24677 		return (EFAULT);
24678 
24679 	if (mh_time) {
24680 		mutex_enter(SD_MUTEX(un));
24681 		un->un_resvd_status |= SD_FAILFAST;
24682 		mutex_exit(SD_MUTEX(un));
24683 		/*
24684 		 * If mh_time is INT_MAX, then this ioctl is being used for
24685 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24686 		 */
24687 		if (mh_time != INT_MAX) {
24688 			rval = sd_check_mhd(dev, mh_time);
24689 		}
24690 	} else {
24691 		(void) sd_check_mhd(dev, 0);
24692 		mutex_enter(SD_MUTEX(un));
24693 		un->un_resvd_status &= ~SD_FAILFAST;
24694 		mutex_exit(SD_MUTEX(un));
24695 	}
24696 	return (rval);
24697 }
24698 
24699 
24700 /*
24701  *    Function: sd_mhdioc_takeown
24702  *
24703  * Description: This routine is the driver entry point for handling ioctl
24704  *		requests to forcefully acquire exclusive access rights to the
24705  *		multihost disk (MHIOCTKOWN).
24706  *
24707  *   Arguments: dev	- the device number
24708  *		arg	- user provided structure specifying the delay
24709  *			  parameters in milliseconds
24710  *		flag	- this argument is a pass through to ddi_copyxxx()
24711  *			  directly from the mode argument of ioctl().
24712  *
24713  * Return Code: 0
24714  *		EFAULT
24715  *		ENXIO
24716  */
24717 
24718 static int
24719 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24720 {
24721 	struct sd_lun		*un = NULL;
24722 	struct mhioctkown	*tkown = NULL;
24723 	int			rval = 0;
24724 
24725 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24726 		return (ENXIO);
24727 	}
24728 
24729 	if (arg != NULL) {
24730 		tkown = (struct mhioctkown *)
24731 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24732 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24733 		if (rval != 0) {
24734 			rval = EFAULT;
24735 			goto error;
24736 		}
24737 	}
24738 
24739 	rval = sd_take_ownership(dev, tkown);
24740 	mutex_enter(SD_MUTEX(un));
24741 	if (rval == 0) {
24742 		un->un_resvd_status |= SD_RESERVE;
24743 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24744 			sd_reinstate_resv_delay =
24745 			    tkown->reinstate_resv_delay * 1000;
24746 		} else {
24747 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24748 		}
24749 		/*
24750 		 * Give the scsi_watch routine interval set by
24751 		 * the MHIOCENFAILFAST ioctl precedence here.
24752 		 */
24753 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24754 			mutex_exit(SD_MUTEX(un));
24755 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24756 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24757 			    "sd_mhdioc_takeown : %d\n",
24758 			    sd_reinstate_resv_delay);
24759 		} else {
24760 			mutex_exit(SD_MUTEX(un));
24761 		}
24762 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24763 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24764 	} else {
24765 		un->un_resvd_status &= ~SD_RESERVE;
24766 		mutex_exit(SD_MUTEX(un));
24767 	}
24768 
24769 error:
24770 	if (tkown != NULL) {
24771 		kmem_free(tkown, sizeof (struct mhioctkown));
24772 	}
24773 	return (rval);
24774 }
24775 
24776 
24777 /*
24778  *    Function: sd_mhdioc_release
24779  *
24780  * Description: This routine is the driver entry point for handling ioctl
24781  *		requests to release exclusive access rights to the multihost
24782  *		disk (MHIOCRELEASE).
24783  *
24784  *   Arguments: dev	- the device number
24785  *
24786  * Return Code: 0
24787  *		ENXIO
24788  */
24789 
24790 static int
24791 sd_mhdioc_release(dev_t dev)
24792 {
24793 	struct sd_lun		*un = NULL;
24794 	timeout_id_t		resvd_timeid_save;
24795 	int			resvd_status_save;
24796 	int			rval = 0;
24797 
24798 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24799 		return (ENXIO);
24800 	}
24801 
24802 	mutex_enter(SD_MUTEX(un));
24803 	resvd_status_save = un->un_resvd_status;
24804 	un->un_resvd_status &=
24805 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24806 	if (un->un_resvd_timeid) {
24807 		resvd_timeid_save = un->un_resvd_timeid;
24808 		un->un_resvd_timeid = NULL;
24809 		mutex_exit(SD_MUTEX(un));
24810 		(void) untimeout(resvd_timeid_save);
24811 	} else {
24812 		mutex_exit(SD_MUTEX(un));
24813 	}
24814 
24815 	/*
24816 	 * destroy any pending timeout thread that may be attempting to
24817 	 * reinstate reservation on this device.
24818 	 */
24819 	sd_rmv_resv_reclaim_req(dev);
24820 
24821 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24822 		mutex_enter(SD_MUTEX(un));
24823 		if ((un->un_mhd_token) &&
24824 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24825 			mutex_exit(SD_MUTEX(un));
24826 			(void) sd_check_mhd(dev, 0);
24827 		} else {
24828 			mutex_exit(SD_MUTEX(un));
24829 		}
24830 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24831 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24832 	} else {
24833 		/*
24834 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24835 		 */
24836 		mutex_enter(SD_MUTEX(un));
24837 		un->un_resvd_status = resvd_status_save;
24838 		mutex_exit(SD_MUTEX(un));
24839 	}
24840 	return (rval);
24841 }
24842 
24843 
24844 /*
24845  *    Function: sd_mhdioc_register_devid
24846  *
24847  * Description: This routine is the driver entry point for handling ioctl
24848  *		requests to register the device id (MHIOCREREGISTERDEVID).
24849  *
24850  *		Note: The implementation for this ioctl has been updated to
24851  *		be consistent with the original PSARC case (1999/357)
24852  *		(4375899, 4241671, 4220005)
24853  *
24854  *   Arguments: dev	- the device number
24855  *
24856  * Return Code: 0
24857  *		ENXIO
24858  */
24859 
24860 static int
24861 sd_mhdioc_register_devid(dev_t dev)
24862 {
24863 	struct sd_lun	*un = NULL;
24864 	int		rval = 0;
24865 
24866 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24867 		return (ENXIO);
24868 	}
24869 
24870 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24871 
24872 	mutex_enter(SD_MUTEX(un));
24873 
24874 	/* If a devid already exists, de-register it */
24875 	if (un->un_devid != NULL) {
24876 		ddi_devid_unregister(SD_DEVINFO(un));
24877 		/*
24878 		 * After unregister devid, needs to free devid memory
24879 		 */
24880 		ddi_devid_free(un->un_devid);
24881 		un->un_devid = NULL;
24882 	}
24883 
24884 	/* Check for reservation conflict */
24885 	mutex_exit(SD_MUTEX(un));
24886 	rval = sd_send_scsi_TEST_UNIT_READY(un, 0);
24887 	mutex_enter(SD_MUTEX(un));
24888 
24889 	switch (rval) {
24890 	case 0:
24891 		sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24892 		break;
24893 	case EACCES:
24894 		break;
24895 	default:
24896 		rval = EIO;
24897 	}
24898 
24899 	mutex_exit(SD_MUTEX(un));
24900 	return (rval);
24901 }
24902 
24903 
24904 /*
24905  *    Function: sd_mhdioc_inkeys
24906  *
24907  * Description: This routine is the driver entry point for handling ioctl
24908  *		requests to issue the SCSI-3 Persistent In Read Keys command
24909  *		to the device (MHIOCGRP_INKEYS).
24910  *
24911  *   Arguments: dev	- the device number
24912  *		arg	- user provided in_keys structure
24913  *		flag	- this argument is a pass through to ddi_copyxxx()
24914  *			  directly from the mode argument of ioctl().
24915  *
24916  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24917  *		ENXIO
24918  *		EFAULT
24919  */
24920 
24921 static int
24922 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24923 {
24924 	struct sd_lun		*un;
24925 	mhioc_inkeys_t		inkeys;
24926 	int			rval = 0;
24927 
24928 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24929 		return (ENXIO);
24930 	}
24931 
24932 #ifdef _MULTI_DATAMODEL
24933 	switch (ddi_model_convert_from(flag & FMODELS)) {
24934 	case DDI_MODEL_ILP32: {
24935 		struct mhioc_inkeys32	inkeys32;
24936 
24937 		if (ddi_copyin(arg, &inkeys32,
24938 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
24939 			return (EFAULT);
24940 		}
24941 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24942 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24943 		    &inkeys, flag)) != 0) {
24944 			return (rval);
24945 		}
24946 		inkeys32.generation = inkeys.generation;
24947 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24948 		    flag) != 0) {
24949 			return (EFAULT);
24950 		}
24951 		break;
24952 	}
24953 	case DDI_MODEL_NONE:
24954 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24955 		    flag) != 0) {
24956 			return (EFAULT);
24957 		}
24958 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24959 		    &inkeys, flag)) != 0) {
24960 			return (rval);
24961 		}
24962 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24963 		    flag) != 0) {
24964 			return (EFAULT);
24965 		}
24966 		break;
24967 	}
24968 
24969 #else /* ! _MULTI_DATAMODEL */
24970 
24971 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24972 		return (EFAULT);
24973 	}
24974 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24975 	if (rval != 0) {
24976 		return (rval);
24977 	}
24978 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24979 		return (EFAULT);
24980 	}
24981 
24982 #endif /* _MULTI_DATAMODEL */
24983 
24984 	return (rval);
24985 }
24986 
24987 
24988 /*
24989  *    Function: sd_mhdioc_inresv
24990  *
24991  * Description: This routine is the driver entry point for handling ioctl
24992  *		requests to issue the SCSI-3 Persistent In Read Reservations
24993  *		command to the device (MHIOCGRP_INKEYS).
24994  *
24995  *   Arguments: dev	- the device number
24996  *		arg	- user provided in_resv structure
24997  *		flag	- this argument is a pass through to ddi_copyxxx()
24998  *			  directly from the mode argument of ioctl().
24999  *
25000  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
25001  *		ENXIO
25002  *		EFAULT
25003  */
25004 
25005 static int
25006 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
25007 {
25008 	struct sd_lun		*un;
25009 	mhioc_inresvs_t		inresvs;
25010 	int			rval = 0;
25011 
25012 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25013 		return (ENXIO);
25014 	}
25015 
25016 #ifdef _MULTI_DATAMODEL
25017 
25018 	switch (ddi_model_convert_from(flag & FMODELS)) {
25019 	case DDI_MODEL_ILP32: {
25020 		struct mhioc_inresvs32	inresvs32;
25021 
25022 		if (ddi_copyin(arg, &inresvs32,
25023 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
25024 			return (EFAULT);
25025 		}
25026 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
25027 		if ((rval = sd_persistent_reservation_in_read_resv(un,
25028 		    &inresvs, flag)) != 0) {
25029 			return (rval);
25030 		}
25031 		inresvs32.generation = inresvs.generation;
25032 		if (ddi_copyout(&inresvs32, arg,
25033 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
25034 			return (EFAULT);
25035 		}
25036 		break;
25037 	}
25038 	case DDI_MODEL_NONE:
25039 		if (ddi_copyin(arg, &inresvs,
25040 		    sizeof (mhioc_inresvs_t), flag) != 0) {
25041 			return (EFAULT);
25042 		}
25043 		if ((rval = sd_persistent_reservation_in_read_resv(un,
25044 		    &inresvs, flag)) != 0) {
25045 			return (rval);
25046 		}
25047 		if (ddi_copyout(&inresvs, arg,
25048 		    sizeof (mhioc_inresvs_t), flag) != 0) {
25049 			return (EFAULT);
25050 		}
25051 		break;
25052 	}
25053 
25054 #else /* ! _MULTI_DATAMODEL */
25055 
25056 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
25057 		return (EFAULT);
25058 	}
25059 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
25060 	if (rval != 0) {
25061 		return (rval);
25062 	}
25063 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
25064 		return (EFAULT);
25065 	}
25066 
25067 #endif /* ! _MULTI_DATAMODEL */
25068 
25069 	return (rval);
25070 }
25071 
25072 
25073 /*
25074  * The following routines support the clustering functionality described below
25075  * and implement lost reservation reclaim functionality.
25076  *
25077  * Clustering
25078  * ----------
25079  * The clustering code uses two different, independent forms of SCSI
25080  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
25081  * Persistent Group Reservations. For any particular disk, it will use either
25082  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
25083  *
25084  * SCSI-2
25085  * The cluster software takes ownership of a multi-hosted disk by issuing the
25086  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
25087  * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster,
25088  * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues
25089  * the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the driver. The
25090  * meaning of failfast is that if the driver (on this host) ever encounters the
25091  * scsi error return code RESERVATION_CONFLICT from the device, it should
25092  * immediately panic the host. The motivation for this ioctl is that if this
25093  * host does encounter reservation conflict, the underlying cause is that some
25094  * other host of the cluster has decided that this host is no longer in the
25095  * cluster and has seized control of the disks for itself. Since this host is no
25096  * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl
25097  * does two things:
25098  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
25099  *      error to panic the host
25100  *      (b) it sets up a periodic timer to test whether this host still has
25101  *      "access" (in that no other host has reserved the device):  if the
25102  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
25103  *      purpose of that periodic timer is to handle scenarios where the host is
25104  *      otherwise temporarily quiescent, temporarily doing no real i/o.
25105  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
25106  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
25107  * the device itself.
25108  *
25109  * SCSI-3 PGR
25110  * A direct semantic implementation of the SCSI-3 Persistent Reservation
25111  * facility is supported through the shared multihost disk ioctls
25112  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
25113  * MHIOCGRP_PREEMPTANDABORT)
25114  *
25115  * Reservation Reclaim:
25116  * --------------------
25117  * To support the lost reservation reclaim operations this driver creates a
25118  * single thread to handle reinstating reservations on all devices that have
25119  * lost reservations sd_resv_reclaim_requests are logged for all devices that
25120  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
25121  * and the reservation reclaim thread loops through the requests to regain the
25122  * lost reservations.
25123  */
25124 
25125 /*
25126  *    Function: sd_check_mhd()
25127  *
25128  * Description: This function sets up and submits a scsi watch request or
25129  *		terminates an existing watch request. This routine is used in
25130  *		support of reservation reclaim.
25131  *
25132  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
25133  *			 among multiple watches that share the callback function
25134  *		interval - the number of microseconds specifying the watch
25135  *			   interval for issuing TEST UNIT READY commands. If
25136  *			   set to 0 the watch should be terminated. If the
25137  *			   interval is set to 0 and if the device is required
25138  *			   to hold reservation while disabling failfast, the
25139  *			   watch is restarted with an interval of
25140  *			   reinstate_resv_delay.
25141  *
25142  * Return Code: 0	   - Successful submit/terminate of scsi watch request
25143  *		ENXIO      - Indicates an invalid device was specified
25144  *		EAGAIN     - Unable to submit the scsi watch request
25145  */
25146 
25147 static int
25148 sd_check_mhd(dev_t dev, int interval)
25149 {
25150 	struct sd_lun	*un;
25151 	opaque_t	token;
25152 
25153 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25154 		return (ENXIO);
25155 	}
25156 
25157 	/* is this a watch termination request? */
25158 	if (interval == 0) {
25159 		mutex_enter(SD_MUTEX(un));
25160 		/* if there is an existing watch task then terminate it */
25161 		if (un->un_mhd_token) {
25162 			token = un->un_mhd_token;
25163 			un->un_mhd_token = NULL;
25164 			mutex_exit(SD_MUTEX(un));
25165 			(void) scsi_watch_request_terminate(token,
25166 			    SCSI_WATCH_TERMINATE_WAIT);
25167 			mutex_enter(SD_MUTEX(un));
25168 		} else {
25169 			mutex_exit(SD_MUTEX(un));
25170 			/*
25171 			 * Note: If we return here we don't check for the
25172 			 * failfast case. This is the original legacy
25173 			 * implementation but perhaps we should be checking
25174 			 * the failfast case.
25175 			 */
25176 			return (0);
25177 		}
25178 		/*
25179 		 * If the device is required to hold reservation while
25180 		 * disabling failfast, we need to restart the scsi_watch
25181 		 * routine with an interval of reinstate_resv_delay.
25182 		 */
25183 		if (un->un_resvd_status & SD_RESERVE) {
25184 			interval = sd_reinstate_resv_delay/1000;
25185 		} else {
25186 			/* no failfast so bail */
25187 			mutex_exit(SD_MUTEX(un));
25188 			return (0);
25189 		}
25190 		mutex_exit(SD_MUTEX(un));
25191 	}
25192 
25193 	/*
25194 	 * adjust minimum time interval to 1 second,
25195 	 * and convert from msecs to usecs
25196 	 */
25197 	if (interval > 0 && interval < 1000) {
25198 		interval = 1000;
25199 	}
25200 	interval *= 1000;
25201 
25202 	/*
25203 	 * submit the request to the scsi_watch service
25204 	 */
25205 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
25206 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
25207 	if (token == NULL) {
25208 		return (EAGAIN);
25209 	}
25210 
25211 	/*
25212 	 * save token for termination later on
25213 	 */
25214 	mutex_enter(SD_MUTEX(un));
25215 	un->un_mhd_token = token;
25216 	mutex_exit(SD_MUTEX(un));
25217 	return (0);
25218 }
25219 
25220 
25221 /*
25222  *    Function: sd_mhd_watch_cb()
25223  *
25224  * Description: This function is the call back function used by the scsi watch
25225  *		facility. The scsi watch facility sends the "Test Unit Ready"
25226  *		and processes the status. If applicable (i.e. a "Unit Attention"
25227  *		status and automatic "Request Sense" not used) the scsi watch
25228  *		facility will send a "Request Sense" and retrieve the sense data
25229  *		to be passed to this callback function. In either case the
25230  *		automatic "Request Sense" or the facility submitting one, this
25231  *		callback is passed the status and sense data.
25232  *
25233  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25234  *			among multiple watches that share this callback function
25235  *		resultp - scsi watch facility result packet containing scsi
25236  *			  packet, status byte and sense data
25237  *
25238  * Return Code: 0 - continue the watch task
25239  *		non-zero - terminate the watch task
25240  */
25241 
25242 static int
25243 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
25244 {
25245 	struct sd_lun			*un;
25246 	struct scsi_status		*statusp;
25247 	struct scsi_extended_sense	*sensep;
25248 	struct scsi_pkt			*pkt;
25249 	uchar_t				actual_sense_length;
25250 	dev_t  				dev = (dev_t)arg;
25251 
25252 	ASSERT(resultp != NULL);
25253 	statusp			= resultp->statusp;
25254 	sensep			= resultp->sensep;
25255 	pkt			= resultp->pkt;
25256 	actual_sense_length	= resultp->actual_sense_length;
25257 
25258 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25259 		return (ENXIO);
25260 	}
25261 
25262 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25263 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
25264 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
25265 
25266 	/* Begin processing of the status and/or sense data */
25267 	if (pkt->pkt_reason != CMD_CMPLT) {
25268 		/* Handle the incomplete packet */
25269 		sd_mhd_watch_incomplete(un, pkt);
25270 		return (0);
25271 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
25272 		if (*((unsigned char *)statusp)
25273 		    == STATUS_RESERVATION_CONFLICT) {
25274 			/*
25275 			 * Handle a reservation conflict by panicking if
25276 			 * configured for failfast or by logging the conflict
25277 			 * and updating the reservation status
25278 			 */
25279 			mutex_enter(SD_MUTEX(un));
25280 			if ((un->un_resvd_status & SD_FAILFAST) &&
25281 			    (sd_failfast_enable)) {
25282 				sd_panic_for_res_conflict(un);
25283 				/*NOTREACHED*/
25284 			}
25285 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25286 			    "sd_mhd_watch_cb: Reservation Conflict\n");
25287 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
25288 			mutex_exit(SD_MUTEX(un));
25289 		}
25290 	}
25291 
25292 	if (sensep != NULL) {
25293 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
25294 			mutex_enter(SD_MUTEX(un));
25295 			if ((sensep->es_add_code == SD_SCSI_RESET_SENSE_CODE) &&
25296 			    (un->un_resvd_status & SD_RESERVE)) {
25297 				/*
25298 				 * The additional sense code indicates a power
25299 				 * on or bus device reset has occurred; update
25300 				 * the reservation status.
25301 				 */
25302 				un->un_resvd_status |=
25303 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25304 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25305 				    "sd_mhd_watch_cb: Lost Reservation\n");
25306 			}
25307 		} else {
25308 			return (0);
25309 		}
25310 	} else {
25311 		mutex_enter(SD_MUTEX(un));
25312 	}
25313 
25314 	if ((un->un_resvd_status & SD_RESERVE) &&
25315 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
25316 		if (un->un_resvd_status & SD_WANT_RESERVE) {
25317 			/*
25318 			 * A reset occurred in between the last probe and this
25319 			 * one so if a timeout is pending cancel it.
25320 			 */
25321 			if (un->un_resvd_timeid) {
25322 				timeout_id_t temp_id = un->un_resvd_timeid;
25323 				un->un_resvd_timeid = NULL;
25324 				mutex_exit(SD_MUTEX(un));
25325 				(void) untimeout(temp_id);
25326 				mutex_enter(SD_MUTEX(un));
25327 			}
25328 			un->un_resvd_status &= ~SD_WANT_RESERVE;
25329 		}
25330 		if (un->un_resvd_timeid == 0) {
25331 			/* Schedule a timeout to handle the lost reservation */
25332 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25333 			    (void *)dev,
25334 			    drv_usectohz(sd_reinstate_resv_delay));
25335 		}
25336 	}
25337 	mutex_exit(SD_MUTEX(un));
25338 	return (0);
25339 }
25340 
25341 
25342 /*
25343  *    Function: sd_mhd_watch_incomplete()
25344  *
25345  * Description: This function is used to find out why a scsi pkt sent by the
25346  *		scsi watch facility was not completed. Under some scenarios this
25347  *		routine will return. Otherwise it will send a bus reset to see
25348  *		if the drive is still online.
25349  *
25350  *   Arguments: un  - driver soft state (unit) structure
25351  *		pkt - incomplete scsi pkt
25352  */
25353 
25354 static void
25355 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25356 {
25357 	int	be_chatty;
25358 	int	perr;
25359 
25360 	ASSERT(pkt != NULL);
25361 	ASSERT(un != NULL);
25362 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
25363 	perr		= (pkt->pkt_statistics & STAT_PERR);
25364 
25365 	mutex_enter(SD_MUTEX(un));
25366 	if (un->un_state == SD_STATE_DUMPING) {
25367 		mutex_exit(SD_MUTEX(un));
25368 		return;
25369 	}
25370 
25371 	switch (pkt->pkt_reason) {
25372 	case CMD_UNX_BUS_FREE:
25373 		/*
25374 		 * If we had a parity error that caused the target to drop BSY*,
25375 		 * don't be chatty about it.
25376 		 */
25377 		if (perr && be_chatty) {
25378 			be_chatty = 0;
25379 		}
25380 		break;
25381 	case CMD_TAG_REJECT:
25382 		/*
25383 		 * The SCSI-2 spec states that a tag reject will be sent by the
25384 		 * target if tagged queuing is not supported. A tag reject may
25385 		 * also be sent during certain initialization periods or to
25386 		 * control internal resources. For the latter case the target
25387 		 * may also return Queue Full.
25388 		 *
25389 		 * If this driver receives a tag reject from a target that is
25390 		 * going through an init period or controlling internal
25391 		 * resources tagged queuing will be disabled. This is a less
25392 		 * than optimal behavior but the driver is unable to determine
25393 		 * the target state and assumes tagged queueing is not supported
25394 		 */
25395 		pkt->pkt_flags = 0;
25396 		un->un_tagflags = 0;
25397 
25398 		if (un->un_f_opt_queueing == TRUE) {
25399 			un->un_throttle = min(un->un_throttle, 3);
25400 		} else {
25401 			un->un_throttle = 1;
25402 		}
25403 		mutex_exit(SD_MUTEX(un));
25404 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25405 		mutex_enter(SD_MUTEX(un));
25406 		break;
25407 	case CMD_INCOMPLETE:
25408 		/*
25409 		 * The transport stopped with an abnormal state, fallthrough and
25410 		 * reset the target and/or bus unless selection did not complete
25411 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
25412 		 * go through a target/bus reset
25413 		 */
25414 		if (pkt->pkt_state == STATE_GOT_BUS) {
25415 			break;
25416 		}
25417 		/*FALLTHROUGH*/
25418 
25419 	case CMD_TIMEOUT:
25420 	default:
25421 		/*
25422 		 * The lun may still be running the command, so a lun reset
25423 		 * should be attempted. If the lun reset fails or cannot be
25424 		 * issued, than try a target reset. Lastly try a bus reset.
25425 		 */
25426 		if ((pkt->pkt_statistics &
25427 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25428 			int reset_retval = 0;
25429 			mutex_exit(SD_MUTEX(un));
25430 			if (un->un_f_allow_bus_device_reset == TRUE) {
25431 				if (un->un_f_lun_reset_enabled == TRUE) {
25432 					reset_retval =
25433 					    scsi_reset(SD_ADDRESS(un),
25434 					    RESET_LUN);
25435 				}
25436 				if (reset_retval == 0) {
25437 					reset_retval =
25438 					    scsi_reset(SD_ADDRESS(un),
25439 					    RESET_TARGET);
25440 				}
25441 			}
25442 			if (reset_retval == 0) {
25443 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25444 			}
25445 			mutex_enter(SD_MUTEX(un));
25446 		}
25447 		break;
25448 	}
25449 
25450 	/* A device/bus reset has occurred; update the reservation status. */
25451 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25452 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25453 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25454 			un->un_resvd_status |=
25455 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25456 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25457 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25458 		}
25459 	}
25460 
25461 	/*
25462 	 * The disk has been turned off; Update the device state.
25463 	 *
25464 	 * Note: Should we be offlining the disk here?
25465 	 */
25466 	if (pkt->pkt_state == STATE_GOT_BUS) {
25467 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25468 		    "Disk not responding to selection\n");
25469 		if (un->un_state != SD_STATE_OFFLINE) {
25470 			New_state(un, SD_STATE_OFFLINE);
25471 		}
25472 	} else if (be_chatty) {
25473 		/*
25474 		 * suppress messages if they are all the same pkt reason;
25475 		 * with TQ, many (up to 256) are returned with the same
25476 		 * pkt_reason
25477 		 */
25478 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25479 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25480 			    "sd_mhd_watch_incomplete: "
25481 			    "SCSI transport failed: reason '%s'\n",
25482 			    scsi_rname(pkt->pkt_reason));
25483 		}
25484 	}
25485 	un->un_last_pkt_reason = pkt->pkt_reason;
25486 	mutex_exit(SD_MUTEX(un));
25487 }
25488 
25489 
25490 /*
25491  *    Function: sd_sname()
25492  *
25493  * Description: This is a simple little routine to return a string containing
25494  *		a printable description of command status byte for use in
25495  *		logging.
25496  *
25497  *   Arguments: status - pointer to a status byte
25498  *
25499  * Return Code: char * - string containing status description.
25500  */
25501 
25502 static char *
25503 sd_sname(uchar_t status)
25504 {
25505 	switch (status & STATUS_MASK) {
25506 	case STATUS_GOOD:
25507 		return ("good status");
25508 	case STATUS_CHECK:
25509 		return ("check condition");
25510 	case STATUS_MET:
25511 		return ("condition met");
25512 	case STATUS_BUSY:
25513 		return ("busy");
25514 	case STATUS_INTERMEDIATE:
25515 		return ("intermediate");
25516 	case STATUS_INTERMEDIATE_MET:
25517 		return ("intermediate - condition met");
25518 	case STATUS_RESERVATION_CONFLICT:
25519 		return ("reservation_conflict");
25520 	case STATUS_TERMINATED:
25521 		return ("command terminated");
25522 	case STATUS_QFULL:
25523 		return ("queue full");
25524 	default:
25525 		return ("<unknown status>");
25526 	}
25527 }
25528 
25529 
25530 /*
25531  *    Function: sd_mhd_resvd_recover()
25532  *
25533  * Description: This function adds a reservation entry to the
25534  *		sd_resv_reclaim_request list and signals the reservation
25535  *		reclaim thread that there is work pending. If the reservation
25536  *		reclaim thread has not been previously created this function
25537  *		will kick it off.
25538  *
25539  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25540  *			among multiple watches that share this callback function
25541  *
25542  *     Context: This routine is called by timeout() and is run in interrupt
25543  *		context. It must not sleep or call other functions which may
25544  *		sleep.
25545  */
25546 
25547 static void
25548 sd_mhd_resvd_recover(void *arg)
25549 {
25550 	dev_t			dev = (dev_t)arg;
25551 	struct sd_lun		*un;
25552 	struct sd_thr_request	*sd_treq = NULL;
25553 	struct sd_thr_request	*sd_cur = NULL;
25554 	struct sd_thr_request	*sd_prev = NULL;
25555 	int			already_there = 0;
25556 
25557 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25558 		return;
25559 	}
25560 
25561 	mutex_enter(SD_MUTEX(un));
25562 	un->un_resvd_timeid = NULL;
25563 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25564 		/*
25565 		 * There was a reset so don't issue the reserve, allow the
25566 		 * sd_mhd_watch_cb callback function to notice this and
25567 		 * reschedule the timeout for reservation.
25568 		 */
25569 		mutex_exit(SD_MUTEX(un));
25570 		return;
25571 	}
25572 	mutex_exit(SD_MUTEX(un));
25573 
25574 	/*
25575 	 * Add this device to the sd_resv_reclaim_request list and the
25576 	 * sd_resv_reclaim_thread should take care of the rest.
25577 	 *
25578 	 * Note: We can't sleep in this context so if the memory allocation
25579 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25580 	 * reschedule the timeout for reservation.  (4378460)
25581 	 */
25582 	sd_treq = (struct sd_thr_request *)
25583 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25584 	if (sd_treq == NULL) {
25585 		return;
25586 	}
25587 
25588 	sd_treq->sd_thr_req_next = NULL;
25589 	sd_treq->dev = dev;
25590 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25591 	if (sd_tr.srq_thr_req_head == NULL) {
25592 		sd_tr.srq_thr_req_head = sd_treq;
25593 	} else {
25594 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25595 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25596 			if (sd_cur->dev == dev) {
25597 				/*
25598 				 * already in Queue so don't log
25599 				 * another request for the device
25600 				 */
25601 				already_there = 1;
25602 				break;
25603 			}
25604 			sd_prev = sd_cur;
25605 		}
25606 		if (!already_there) {
25607 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25608 			    "logging request for %lx\n", dev);
25609 			sd_prev->sd_thr_req_next = sd_treq;
25610 		} else {
25611 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25612 		}
25613 	}
25614 
25615 	/*
25616 	 * Create a kernel thread to do the reservation reclaim and free up this
25617 	 * thread. We cannot block this thread while we go away to do the
25618 	 * reservation reclaim
25619 	 */
25620 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25621 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25622 		    sd_resv_reclaim_thread, NULL,
25623 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25624 
25625 	/* Tell the reservation reclaim thread that it has work to do */
25626 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25627 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25628 }
25629 
25630 /*
25631  *    Function: sd_resv_reclaim_thread()
25632  *
25633  * Description: This function implements the reservation reclaim operations
25634  *
25635  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25636  *		      among multiple watches that share this callback function
25637  */
25638 
25639 static void
25640 sd_resv_reclaim_thread()
25641 {
25642 	struct sd_lun		*un;
25643 	struct sd_thr_request	*sd_mhreq;
25644 
25645 	/* Wait for work */
25646 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25647 	if (sd_tr.srq_thr_req_head == NULL) {
25648 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25649 		    &sd_tr.srq_resv_reclaim_mutex);
25650 	}
25651 
25652 	/* Loop while we have work */
25653 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25654 		un = ddi_get_soft_state(sd_state,
25655 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25656 		if (un == NULL) {
25657 			/*
25658 			 * softstate structure is NULL so just
25659 			 * dequeue the request and continue
25660 			 */
25661 			sd_tr.srq_thr_req_head =
25662 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25663 			kmem_free(sd_tr.srq_thr_cur_req,
25664 			    sizeof (struct sd_thr_request));
25665 			continue;
25666 		}
25667 
25668 		/* dequeue the request */
25669 		sd_mhreq = sd_tr.srq_thr_cur_req;
25670 		sd_tr.srq_thr_req_head =
25671 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25672 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25673 
25674 		/*
25675 		 * Reclaim reservation only if SD_RESERVE is still set. There
25676 		 * may have been a call to MHIOCRELEASE before we got here.
25677 		 */
25678 		mutex_enter(SD_MUTEX(un));
25679 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25680 			/*
25681 			 * Note: The SD_LOST_RESERVE flag is cleared before
25682 			 * reclaiming the reservation. If this is done after the
25683 			 * call to sd_reserve_release a reservation loss in the
25684 			 * window between pkt completion of reserve cmd and
25685 			 * mutex_enter below may not be recognized
25686 			 */
25687 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25688 			mutex_exit(SD_MUTEX(un));
25689 
25690 			if (sd_reserve_release(sd_mhreq->dev,
25691 			    SD_RESERVE) == 0) {
25692 				mutex_enter(SD_MUTEX(un));
25693 				un->un_resvd_status |= SD_RESERVE;
25694 				mutex_exit(SD_MUTEX(un));
25695 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25696 				    "sd_resv_reclaim_thread: "
25697 				    "Reservation Recovered\n");
25698 			} else {
25699 				mutex_enter(SD_MUTEX(un));
25700 				un->un_resvd_status |= SD_LOST_RESERVE;
25701 				mutex_exit(SD_MUTEX(un));
25702 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25703 				    "sd_resv_reclaim_thread: Failed "
25704 				    "Reservation Recovery\n");
25705 			}
25706 		} else {
25707 			mutex_exit(SD_MUTEX(un));
25708 		}
25709 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25710 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25711 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25712 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25713 		/*
25714 		 * wakeup the destroy thread if anyone is waiting on
25715 		 * us to complete.
25716 		 */
25717 		cv_signal(&sd_tr.srq_inprocess_cv);
25718 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25719 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25720 	}
25721 
25722 	/*
25723 	 * cleanup the sd_tr structure now that this thread will not exist
25724 	 */
25725 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25726 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25727 	sd_tr.srq_resv_reclaim_thread = NULL;
25728 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25729 	thread_exit();
25730 }
25731 
25732 
25733 /*
25734  *    Function: sd_rmv_resv_reclaim_req()
25735  *
25736  * Description: This function removes any pending reservation reclaim requests
25737  *		for the specified device.
25738  *
25739  *   Arguments: dev - the device 'dev_t'
25740  */
25741 
25742 static void
25743 sd_rmv_resv_reclaim_req(dev_t dev)
25744 {
25745 	struct sd_thr_request *sd_mhreq;
25746 	struct sd_thr_request *sd_prev;
25747 
25748 	/* Remove a reservation reclaim request from the list */
25749 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25750 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25751 		/*
25752 		 * We are attempting to reinstate reservation for
25753 		 * this device. We wait for sd_reserve_release()
25754 		 * to return before we return.
25755 		 */
25756 		cv_wait(&sd_tr.srq_inprocess_cv,
25757 		    &sd_tr.srq_resv_reclaim_mutex);
25758 	} else {
25759 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25760 		if (sd_mhreq && sd_mhreq->dev == dev) {
25761 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25762 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25763 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25764 			return;
25765 		}
25766 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25767 			if (sd_mhreq && sd_mhreq->dev == dev) {
25768 				break;
25769 			}
25770 			sd_prev = sd_mhreq;
25771 		}
25772 		if (sd_mhreq != NULL) {
25773 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25774 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25775 		}
25776 	}
25777 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25778 }
25779 
25780 
25781 /*
25782  *    Function: sd_mhd_reset_notify_cb()
25783  *
25784  * Description: This is a call back function for scsi_reset_notify. This
25785  *		function updates the softstate reserved status and logs the
25786  *		reset. The driver scsi watch facility callback function
25787  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25788  *		will reclaim the reservation.
25789  *
25790  *   Arguments: arg  - driver soft state (unit) structure
25791  */
25792 
25793 static void
25794 sd_mhd_reset_notify_cb(caddr_t arg)
25795 {
25796 	struct sd_lun *un = (struct sd_lun *)arg;
25797 
25798 	mutex_enter(SD_MUTEX(un));
25799 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25800 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25801 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25802 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25803 	}
25804 	mutex_exit(SD_MUTEX(un));
25805 }
25806 
25807 
25808 /*
25809  *    Function: sd_take_ownership()
25810  *
25811  * Description: This routine implements an algorithm to achieve a stable
25812  *		reservation on disks which don't implement priority reserve,
25813  *		and makes sure that other host lose re-reservation attempts.
25814  *		This algorithm contains of a loop that keeps issuing the RESERVE
25815  *		for some period of time (min_ownership_delay, default 6 seconds)
25816  *		During that loop, it looks to see if there has been a bus device
25817  *		reset or bus reset (both of which cause an existing reservation
25818  *		to be lost). If the reservation is lost issue RESERVE until a
25819  *		period of min_ownership_delay with no resets has gone by, or
25820  *		until max_ownership_delay has expired. This loop ensures that
25821  *		the host really did manage to reserve the device, in spite of
25822  *		resets. The looping for min_ownership_delay (default six
25823  *		seconds) is important to early generation clustering products,
25824  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25825  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25826  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25827  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25828  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25829  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25830  *		no longer "owns" the disk and will have panicked itself.  Thus,
25831  *		the host issuing the MHIOCTKOWN is assured (with timing
25832  *		dependencies) that by the time it actually starts to use the
25833  *		disk for real work, the old owner is no longer accessing it.
25834  *
25835  *		min_ownership_delay is the minimum amount of time for which the
25836  *		disk must be reserved continuously devoid of resets before the
25837  *		MHIOCTKOWN ioctl will return success.
25838  *
25839  *		max_ownership_delay indicates the amount of time by which the
25840  *		take ownership should succeed or timeout with an error.
25841  *
25842  *   Arguments: dev - the device 'dev_t'
25843  *		*p  - struct containing timing info.
25844  *
25845  * Return Code: 0 for success or error code
25846  */
25847 
25848 static int
25849 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25850 {
25851 	struct sd_lun	*un;
25852 	int		rval;
25853 	int		err;
25854 	int		reservation_count   = 0;
25855 	int		min_ownership_delay =  6000000; /* in usec */
25856 	int		max_ownership_delay = 30000000; /* in usec */
25857 	clock_t		start_time;	/* starting time of this algorithm */
25858 	clock_t		end_time;	/* time limit for giving up */
25859 	clock_t		ownership_time;	/* time limit for stable ownership */
25860 	clock_t		current_time;
25861 	clock_t		previous_current_time;
25862 
25863 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25864 		return (ENXIO);
25865 	}
25866 
25867 	/*
25868 	 * Attempt a device reservation. A priority reservation is requested.
25869 	 */
25870 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25871 	    != SD_SUCCESS) {
25872 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25873 		    "sd_take_ownership: return(1)=%d\n", rval);
25874 		return (rval);
25875 	}
25876 
25877 	/* Update the softstate reserved status to indicate the reservation */
25878 	mutex_enter(SD_MUTEX(un));
25879 	un->un_resvd_status |= SD_RESERVE;
25880 	un->un_resvd_status &=
25881 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25882 	mutex_exit(SD_MUTEX(un));
25883 
25884 	if (p != NULL) {
25885 		if (p->min_ownership_delay != 0) {
25886 			min_ownership_delay = p->min_ownership_delay * 1000;
25887 		}
25888 		if (p->max_ownership_delay != 0) {
25889 			max_ownership_delay = p->max_ownership_delay * 1000;
25890 		}
25891 	}
25892 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25893 	    "sd_take_ownership: min, max delays: %d, %d\n",
25894 	    min_ownership_delay, max_ownership_delay);
25895 
25896 	start_time = ddi_get_lbolt();
25897 	current_time	= start_time;
25898 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25899 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25900 
25901 	while (current_time - end_time < 0) {
25902 		delay(drv_usectohz(500000));
25903 
25904 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25905 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25906 				mutex_enter(SD_MUTEX(un));
25907 				rval = (un->un_resvd_status &
25908 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25909 				mutex_exit(SD_MUTEX(un));
25910 				break;
25911 			}
25912 		}
25913 		previous_current_time = current_time;
25914 		current_time = ddi_get_lbolt();
25915 		mutex_enter(SD_MUTEX(un));
25916 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25917 			ownership_time = ddi_get_lbolt() +
25918 			    drv_usectohz(min_ownership_delay);
25919 			reservation_count = 0;
25920 		} else {
25921 			reservation_count++;
25922 		}
25923 		un->un_resvd_status |= SD_RESERVE;
25924 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25925 		mutex_exit(SD_MUTEX(un));
25926 
25927 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25928 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25929 		    "reservation=%s\n", (current_time - previous_current_time),
25930 		    reservation_count ? "ok" : "reclaimed");
25931 
25932 		if (current_time - ownership_time >= 0 &&
25933 		    reservation_count >= 4) {
25934 			rval = 0; /* Achieved a stable ownership */
25935 			break;
25936 		}
25937 		if (current_time - end_time >= 0) {
25938 			rval = EACCES; /* No ownership in max possible time */
25939 			break;
25940 		}
25941 	}
25942 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25943 	    "sd_take_ownership: return(2)=%d\n", rval);
25944 	return (rval);
25945 }
25946 
25947 
25948 /*
25949  *    Function: sd_reserve_release()
25950  *
25951  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25952  *		PRIORITY RESERVE commands based on a user specified command type
25953  *
25954  *   Arguments: dev - the device 'dev_t'
25955  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25956  *		      SD_RESERVE, SD_RELEASE
25957  *
25958  * Return Code: 0 or Error Code
25959  */
25960 
25961 static int
25962 sd_reserve_release(dev_t dev, int cmd)
25963 {
25964 	struct uscsi_cmd	*com = NULL;
25965 	struct sd_lun		*un = NULL;
25966 	char			cdb[CDB_GROUP0];
25967 	int			rval;
25968 
25969 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25970 	    (cmd == SD_PRIORITY_RESERVE));
25971 
25972 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25973 		return (ENXIO);
25974 	}
25975 
25976 	/* instantiate and initialize the command and cdb */
25977 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25978 	bzero(cdb, CDB_GROUP0);
25979 	com->uscsi_flags   = USCSI_SILENT;
25980 	com->uscsi_timeout = un->un_reserve_release_time;
25981 	com->uscsi_cdblen  = CDB_GROUP0;
25982 	com->uscsi_cdb	   = cdb;
25983 	if (cmd == SD_RELEASE) {
25984 		cdb[0] = SCMD_RELEASE;
25985 	} else {
25986 		cdb[0] = SCMD_RESERVE;
25987 	}
25988 
25989 	/* Send the command. */
25990 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
25991 	    UIO_SYSSPACE, SD_PATH_STANDARD);
25992 
25993 	/*
25994 	 * "break" a reservation that is held by another host, by issuing a
25995 	 * reset if priority reserve is desired, and we could not get the
25996 	 * device.
25997 	 */
25998 	if ((cmd == SD_PRIORITY_RESERVE) &&
25999 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
26000 		/*
26001 		 * First try to reset the LUN. If we cannot, then try a target
26002 		 * reset, followed by a bus reset if the target reset fails.
26003 		 */
26004 		int reset_retval = 0;
26005 		if (un->un_f_lun_reset_enabled == TRUE) {
26006 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
26007 		}
26008 		if (reset_retval == 0) {
26009 			/* The LUN reset either failed or was not issued */
26010 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26011 		}
26012 		if ((reset_retval == 0) &&
26013 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
26014 			rval = EIO;
26015 			kmem_free(com, sizeof (*com));
26016 			return (rval);
26017 		}
26018 
26019 		bzero(com, sizeof (struct uscsi_cmd));
26020 		com->uscsi_flags   = USCSI_SILENT;
26021 		com->uscsi_cdb	   = cdb;
26022 		com->uscsi_cdblen  = CDB_GROUP0;
26023 		com->uscsi_timeout = 5;
26024 
26025 		/*
26026 		 * Reissue the last reserve command, this time without request
26027 		 * sense.  Assume that it is just a regular reserve command.
26028 		 */
26029 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
26030 		    UIO_SYSSPACE, SD_PATH_STANDARD);
26031 	}
26032 
26033 	/* Return an error if still getting a reservation conflict. */
26034 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
26035 		rval = EACCES;
26036 	}
26037 
26038 	kmem_free(com, sizeof (*com));
26039 	return (rval);
26040 }
26041 
26042 
26043 #define	SD_NDUMP_RETRIES	12
26044 /*
26045  *	System Crash Dump routine
26046  */
26047 
26048 static int
26049 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
26050 {
26051 	int		instance;
26052 	int		partition;
26053 	int		i;
26054 	int		err;
26055 	struct sd_lun	*un;
26056 	struct dk_map	*lp;
26057 	struct scsi_pkt *wr_pktp;
26058 	struct buf	*wr_bp;
26059 	struct buf	wr_buf;
26060 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
26061 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
26062 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
26063 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
26064 	size_t		io_start_offset;
26065 	int		doing_rmw = FALSE;
26066 	int		rval;
26067 #if defined(__i386) || defined(__amd64)
26068 	ssize_t dma_resid;
26069 	daddr_t oblkno;
26070 #endif
26071 
26072 	instance = SDUNIT(dev);
26073 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
26074 	    (!un->un_f_geometry_is_valid) || ISCD(un)) {
26075 		return (ENXIO);
26076 	}
26077 
26078 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
26079 
26080 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
26081 
26082 	partition = SDPART(dev);
26083 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
26084 
26085 	/* Validate blocks to dump at against partition size. */
26086 	lp = &un->un_map[partition];
26087 	if ((blkno + nblk) > lp->dkl_nblk) {
26088 		SD_TRACE(SD_LOG_DUMP, un,
26089 		    "sddump: dump range larger than partition: "
26090 		    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
26091 		    blkno, nblk, lp->dkl_nblk);
26092 		return (EINVAL);
26093 	}
26094 
26095 	mutex_enter(&un->un_pm_mutex);
26096 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
26097 		struct scsi_pkt *start_pktp;
26098 
26099 		mutex_exit(&un->un_pm_mutex);
26100 
26101 		/*
26102 		 * use pm framework to power on HBA 1st
26103 		 */
26104 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
26105 
26106 		/*
26107 		 * Dump no long uses sdpower to power on a device, it's
26108 		 * in-line here so it can be done in polled mode.
26109 		 */
26110 
26111 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
26112 
26113 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
26114 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
26115 
26116 		if (start_pktp == NULL) {
26117 			/* We were not given a SCSI packet, fail. */
26118 			return (EIO);
26119 		}
26120 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
26121 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
26122 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
26123 		start_pktp->pkt_flags = FLAG_NOINTR;
26124 
26125 		mutex_enter(SD_MUTEX(un));
26126 		SD_FILL_SCSI1_LUN(un, start_pktp);
26127 		mutex_exit(SD_MUTEX(un));
26128 		/*
26129 		 * Scsi_poll returns 0 (success) if the command completes and
26130 		 * the status block is STATUS_GOOD.
26131 		 */
26132 		if (sd_scsi_poll(un, start_pktp) != 0) {
26133 			scsi_destroy_pkt(start_pktp);
26134 			return (EIO);
26135 		}
26136 		scsi_destroy_pkt(start_pktp);
26137 		(void) sd_ddi_pm_resume(un);
26138 	} else {
26139 		mutex_exit(&un->un_pm_mutex);
26140 	}
26141 
26142 	mutex_enter(SD_MUTEX(un));
26143 	un->un_throttle = 0;
26144 
26145 	/*
26146 	 * The first time through, reset the specific target device.
26147 	 * However, when cpr calls sddump we know that sd is in a
26148 	 * a good state so no bus reset is required.
26149 	 * Clear sense data via Request Sense cmd.
26150 	 * In sddump we don't care about allow_bus_device_reset anymore
26151 	 */
26152 
26153 	if ((un->un_state != SD_STATE_SUSPENDED) &&
26154 	    (un->un_state != SD_STATE_DUMPING)) {
26155 
26156 		New_state(un, SD_STATE_DUMPING);
26157 
26158 		if (un->un_f_is_fibre == FALSE) {
26159 			mutex_exit(SD_MUTEX(un));
26160 			/*
26161 			 * Attempt a bus reset for parallel scsi.
26162 			 *
26163 			 * Note: A bus reset is required because on some host
26164 			 * systems (i.e. E420R) a bus device reset is
26165 			 * insufficient to reset the state of the target.
26166 			 *
26167 			 * Note: Don't issue the reset for fibre-channel,
26168 			 * because this tends to hang the bus (loop) for
26169 			 * too long while everyone is logging out and in
26170 			 * and the deadman timer for dumping will fire
26171 			 * before the dump is complete.
26172 			 */
26173 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
26174 				mutex_enter(SD_MUTEX(un));
26175 				Restore_state(un);
26176 				mutex_exit(SD_MUTEX(un));
26177 				return (EIO);
26178 			}
26179 
26180 			/* Delay to give the device some recovery time. */
26181 			drv_usecwait(10000);
26182 
26183 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
26184 				SD_INFO(SD_LOG_DUMP, un,
26185 					"sddump: sd_send_polled_RQS failed\n");
26186 			}
26187 			mutex_enter(SD_MUTEX(un));
26188 		}
26189 	}
26190 
26191 	/*
26192 	 * Convert the partition-relative block number to a
26193 	 * disk physical block number.
26194 	 */
26195 	blkno += un->un_offset[partition];
26196 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
26197 
26198 
26199 	/*
26200 	 * Check if the device has a non-512 block size.
26201 	 */
26202 	wr_bp = NULL;
26203 	if (NOT_DEVBSIZE(un)) {
26204 		tgt_byte_offset = blkno * un->un_sys_blocksize;
26205 		tgt_byte_count = nblk * un->un_sys_blocksize;
26206 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
26207 		    (tgt_byte_count % un->un_tgt_blocksize)) {
26208 			doing_rmw = TRUE;
26209 			/*
26210 			 * Calculate the block number and number of block
26211 			 * in terms of the media block size.
26212 			 */
26213 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26214 			tgt_nblk =
26215 			    ((tgt_byte_offset + tgt_byte_count +
26216 				(un->un_tgt_blocksize - 1)) /
26217 				un->un_tgt_blocksize) - tgt_blkno;
26218 
26219 			/*
26220 			 * Invoke the routine which is going to do read part
26221 			 * of read-modify-write.
26222 			 * Note that this routine returns a pointer to
26223 			 * a valid bp in wr_bp.
26224 			 */
26225 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
26226 			    &wr_bp);
26227 			if (err) {
26228 				mutex_exit(SD_MUTEX(un));
26229 				return (err);
26230 			}
26231 			/*
26232 			 * Offset is being calculated as -
26233 			 * (original block # * system block size) -
26234 			 * (new block # * target block size)
26235 			 */
26236 			io_start_offset =
26237 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
26238 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
26239 
26240 			ASSERT((io_start_offset >= 0) &&
26241 			    (io_start_offset < un->un_tgt_blocksize));
26242 			/*
26243 			 * Do the modify portion of read modify write.
26244 			 */
26245 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
26246 			    (size_t)nblk * un->un_sys_blocksize);
26247 		} else {
26248 			doing_rmw = FALSE;
26249 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26250 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
26251 		}
26252 
26253 		/* Convert blkno and nblk to target blocks */
26254 		blkno = tgt_blkno;
26255 		nblk = tgt_nblk;
26256 	} else {
26257 		wr_bp = &wr_buf;
26258 		bzero(wr_bp, sizeof (struct buf));
26259 		wr_bp->b_flags		= B_BUSY;
26260 		wr_bp->b_un.b_addr	= addr;
26261 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
26262 		wr_bp->b_resid		= 0;
26263 	}
26264 
26265 	mutex_exit(SD_MUTEX(un));
26266 
26267 	/*
26268 	 * Obtain a SCSI packet for the write command.
26269 	 * It should be safe to call the allocator here without
26270 	 * worrying about being locked for DVMA mapping because
26271 	 * the address we're passed is already a DVMA mapping
26272 	 *
26273 	 * We are also not going to worry about semaphore ownership
26274 	 * in the dump buffer. Dumping is single threaded at present.
26275 	 */
26276 
26277 	wr_pktp = NULL;
26278 
26279 #if defined(__i386) || defined(__amd64)
26280 	dma_resid = wr_bp->b_bcount;
26281 	oblkno = blkno;
26282 	while (dma_resid != 0) {
26283 #endif
26284 
26285 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26286 		wr_bp->b_flags &= ~B_ERROR;
26287 
26288 #if defined(__i386) || defined(__amd64)
26289 		blkno = oblkno +
26290 			((wr_bp->b_bcount - dma_resid) /
26291 			    un->un_tgt_blocksize);
26292 		nblk = dma_resid / un->un_tgt_blocksize;
26293 
26294 		if (wr_pktp) {
26295 			/* Partial DMA transfers after initial transfer */
26296 			rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26297 			    blkno, nblk);
26298 		} else {
26299 			/* Initial transfer */
26300 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26301 			    un->un_pkt_flags, NULL_FUNC, NULL,
26302 			    blkno, nblk);
26303 		}
26304 #else
26305 		rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26306 		    0, NULL_FUNC, NULL, blkno, nblk);
26307 #endif
26308 
26309 		if (rval == 0) {
26310 			/* We were given a SCSI packet, continue. */
26311 			break;
26312 		}
26313 
26314 		if (i == 0) {
26315 			if (wr_bp->b_flags & B_ERROR) {
26316 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26317 				    "no resources for dumping; "
26318 				    "error code: 0x%x, retrying",
26319 				    geterror(wr_bp));
26320 			} else {
26321 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26322 				    "no resources for dumping; retrying");
26323 			}
26324 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
26325 			if (wr_bp->b_flags & B_ERROR) {
26326 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26327 				    "no resources for dumping; error code: "
26328 				    "0x%x, retrying\n", geterror(wr_bp));
26329 			}
26330 		} else {
26331 			if (wr_bp->b_flags & B_ERROR) {
26332 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26333 				    "no resources for dumping; "
26334 				    "error code: 0x%x, retries failed, "
26335 				    "giving up.\n", geterror(wr_bp));
26336 			} else {
26337 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26338 				    "no resources for dumping; "
26339 				    "retries failed, giving up.\n");
26340 			}
26341 			mutex_enter(SD_MUTEX(un));
26342 			Restore_state(un);
26343 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26344 				mutex_exit(SD_MUTEX(un));
26345 				scsi_free_consistent_buf(wr_bp);
26346 			} else {
26347 				mutex_exit(SD_MUTEX(un));
26348 			}
26349 			return (EIO);
26350 		}
26351 		drv_usecwait(10000);
26352 	}
26353 
26354 #if defined(__i386) || defined(__amd64)
26355 	/*
26356 	 * save the resid from PARTIAL_DMA
26357 	 */
26358 	dma_resid = wr_pktp->pkt_resid;
26359 	if (dma_resid != 0)
26360 		nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26361 	wr_pktp->pkt_resid = 0;
26362 #endif
26363 
26364 	/* SunBug 1222170 */
26365 	wr_pktp->pkt_flags = FLAG_NOINTR;
26366 
26367 	err = EIO;
26368 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26369 
26370 		/*
26371 		 * Scsi_poll returns 0 (success) if the command completes and
26372 		 * the status block is STATUS_GOOD.  We should only check
26373 		 * errors if this condition is not true.  Even then we should
26374 		 * send our own request sense packet only if we have a check
26375 		 * condition and auto request sense has not been performed by
26376 		 * the hba.
26377 		 */
26378 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26379 
26380 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26381 		    (wr_pktp->pkt_resid == 0)) {
26382 			err = SD_SUCCESS;
26383 			break;
26384 		}
26385 
26386 		/*
26387 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26388 		 */
26389 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26390 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26391 			    "Device is gone\n");
26392 			break;
26393 		}
26394 
26395 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26396 			SD_INFO(SD_LOG_DUMP, un,
26397 			    "sddump: write failed with CHECK, try # %d\n", i);
26398 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26399 				(void) sd_send_polled_RQS(un);
26400 			}
26401 
26402 			continue;
26403 		}
26404 
26405 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26406 			int reset_retval = 0;
26407 
26408 			SD_INFO(SD_LOG_DUMP, un,
26409 			    "sddump: write failed with BUSY, try # %d\n", i);
26410 
26411 			if (un->un_f_lun_reset_enabled == TRUE) {
26412 				reset_retval = scsi_reset(SD_ADDRESS(un),
26413 				    RESET_LUN);
26414 			}
26415 			if (reset_retval == 0) {
26416 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26417 			}
26418 			(void) sd_send_polled_RQS(un);
26419 
26420 		} else {
26421 			SD_INFO(SD_LOG_DUMP, un,
26422 			    "sddump: write failed with 0x%x, try # %d\n",
26423 			    SD_GET_PKT_STATUS(wr_pktp), i);
26424 			mutex_enter(SD_MUTEX(un));
26425 			sd_reset_target(un, wr_pktp);
26426 			mutex_exit(SD_MUTEX(un));
26427 		}
26428 
26429 		/*
26430 		 * If we are not getting anywhere with lun/target resets,
26431 		 * let's reset the bus.
26432 		 */
26433 		if (i == SD_NDUMP_RETRIES/2) {
26434 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26435 			(void) sd_send_polled_RQS(un);
26436 		}
26437 
26438 	}
26439 #if defined(__i386) || defined(__amd64)
26440 	}	/* dma_resid */
26441 #endif
26442 
26443 	scsi_destroy_pkt(wr_pktp);
26444 	mutex_enter(SD_MUTEX(un));
26445 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26446 		mutex_exit(SD_MUTEX(un));
26447 		scsi_free_consistent_buf(wr_bp);
26448 	} else {
26449 		mutex_exit(SD_MUTEX(un));
26450 	}
26451 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26452 	return (err);
26453 }
26454 
26455 /*
26456  *    Function: sd_scsi_poll()
26457  *
26458  * Description: This is a wrapper for the scsi_poll call.
26459  *
26460  *   Arguments: sd_lun - The unit structure
26461  *              scsi_pkt - The scsi packet being sent to the device.
26462  *
26463  * Return Code: 0 - Command completed successfully with good status
26464  *             -1 - Command failed.  This could indicate a check condition
26465  *                  or other status value requiring recovery action.
26466  *
26467  */
26468 
26469 static int
26470 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26471 {
26472 	int status;
26473 
26474 	ASSERT(un != NULL);
26475 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26476 	ASSERT(pktp != NULL);
26477 
26478 	status = SD_SUCCESS;
26479 
26480 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26481 		pktp->pkt_flags |= un->un_tagflags;
26482 		pktp->pkt_flags &= ~FLAG_NODISCON;
26483 	}
26484 
26485 	status = sd_ddi_scsi_poll(pktp);
26486 	/*
26487 	 * Scsi_poll returns 0 (success) if the command completes and the
26488 	 * status block is STATUS_GOOD.  We should only check errors if this
26489 	 * condition is not true.  Even then we should send our own request
26490 	 * sense packet only if we have a check condition and auto
26491 	 * request sense has not been performed by the hba.
26492 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26493 	 */
26494 	if ((status != SD_SUCCESS) &&
26495 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26496 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26497 	    (pktp->pkt_reason != CMD_DEV_GONE))
26498 		(void) sd_send_polled_RQS(un);
26499 
26500 	return (status);
26501 }
26502 
26503 /*
26504  *    Function: sd_send_polled_RQS()
26505  *
26506  * Description: This sends the request sense command to a device.
26507  *
26508  *   Arguments: sd_lun - The unit structure
26509  *
26510  * Return Code: 0 - Command completed successfully with good status
26511  *             -1 - Command failed.
26512  *
26513  */
26514 
26515 static int
26516 sd_send_polled_RQS(struct sd_lun *un)
26517 {
26518 	int	ret_val;
26519 	struct	scsi_pkt	*rqs_pktp;
26520 	struct	buf		*rqs_bp;
26521 
26522 	ASSERT(un != NULL);
26523 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26524 
26525 	ret_val = SD_SUCCESS;
26526 
26527 	rqs_pktp = un->un_rqs_pktp;
26528 	rqs_bp	 = un->un_rqs_bp;
26529 
26530 	mutex_enter(SD_MUTEX(un));
26531 
26532 	if (un->un_sense_isbusy) {
26533 		ret_val = SD_FAILURE;
26534 		mutex_exit(SD_MUTEX(un));
26535 		return (ret_val);
26536 	}
26537 
26538 	/*
26539 	 * If the request sense buffer (and packet) is not in use,
26540 	 * let's set the un_sense_isbusy and send our packet
26541 	 */
26542 	un->un_sense_isbusy 	= 1;
26543 	rqs_pktp->pkt_resid  	= 0;
26544 	rqs_pktp->pkt_reason 	= 0;
26545 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26546 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26547 
26548 	mutex_exit(SD_MUTEX(un));
26549 
26550 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26551 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26552 
26553 	/*
26554 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26555 	 * axle - it has a call into us!
26556 	 */
26557 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26558 		SD_INFO(SD_LOG_COMMON, un,
26559 		    "sd_send_polled_RQS: RQS failed\n");
26560 	}
26561 
26562 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26563 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26564 
26565 	mutex_enter(SD_MUTEX(un));
26566 	un->un_sense_isbusy = 0;
26567 	mutex_exit(SD_MUTEX(un));
26568 
26569 	return (ret_val);
26570 }
26571 
26572 /*
26573  * Defines needed for localized version of the scsi_poll routine.
26574  */
26575 #define	SD_CSEC		10000			/* usecs */
26576 #define	SD_SEC_TO_CSEC	(1000000/SD_CSEC)
26577 
26578 
26579 /*
26580  *    Function: sd_ddi_scsi_poll()
26581  *
26582  * Description: Localized version of the scsi_poll routine.  The purpose is to
26583  *		send a scsi_pkt to a device as a polled command.  This version
26584  *		is to ensure more robust handling of transport errors.
26585  *		Specifically this routine cures not ready, coming ready
26586  *		transition for power up and reset of sonoma's.  This can take
26587  *		up to 45 seconds for power-on and 20 seconds for reset of a
26588  * 		sonoma lun.
26589  *
26590  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26591  *
26592  * Return Code: 0 - Command completed successfully with good status
26593  *             -1 - Command failed.
26594  *
26595  */
26596 
26597 static int
26598 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26599 {
26600 	int busy_count;
26601 	int timeout;
26602 	int rval = SD_FAILURE;
26603 	int savef;
26604 	struct scsi_extended_sense *sensep;
26605 	long savet;
26606 	void (*savec)();
26607 	/*
26608 	 * The following is defined in machdep.c and is used in determining if
26609 	 * the scsi transport system will do polled I/O instead of interrupt
26610 	 * I/O when called from xx_dump().
26611 	 */
26612 	extern int do_polled_io;
26613 
26614 	/*
26615 	 * save old flags in pkt, to restore at end
26616 	 */
26617 	savef = pkt->pkt_flags;
26618 	savec = pkt->pkt_comp;
26619 	savet = pkt->pkt_time;
26620 
26621 	pkt->pkt_flags |= FLAG_NOINTR;
26622 
26623 	/*
26624 	 * XXX there is nothing in the SCSA spec that states that we should not
26625 	 * do a callback for polled cmds; however, removing this will break sd
26626 	 * and probably other target drivers
26627 	 */
26628 	pkt->pkt_comp = NULL;
26629 
26630 	/*
26631 	 * we don't like a polled command without timeout.
26632 	 * 60 seconds seems long enough.
26633 	 */
26634 	if (pkt->pkt_time == 0) {
26635 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26636 	}
26637 
26638 	/*
26639 	 * Send polled cmd.
26640 	 *
26641 	 * We do some error recovery for various errors.  Tran_busy,
26642 	 * queue full, and non-dispatched commands are retried every 10 msec.
26643 	 * as they are typically transient failures.  Busy status and Not
26644 	 * Ready are retried every second as this status takes a while to
26645 	 * change.  Unit attention is retried for pkt_time (60) times
26646 	 * with no delay.
26647 	 */
26648 	timeout = pkt->pkt_time * SD_SEC_TO_CSEC;
26649 
26650 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26651 		int rc;
26652 		int poll_delay;
26653 
26654 		/*
26655 		 * Initialize pkt status variables.
26656 		 */
26657 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26658 
26659 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26660 			if (rc != TRAN_BUSY) {
26661 				/* Transport failed - give up. */
26662 				break;
26663 			} else {
26664 				/* Transport busy - try again. */
26665 				poll_delay = 1 * SD_CSEC; /* 10 msec */
26666 			}
26667 		} else {
26668 			/*
26669 			 * Transport accepted - check pkt status.
26670 			 */
26671 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26672 			if (pkt->pkt_reason == CMD_CMPLT &&
26673 			    rc == STATUS_CHECK &&
26674 			    pkt->pkt_state & STATE_ARQ_DONE) {
26675 				struct scsi_arq_status *arqstat =
26676 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26677 
26678 				sensep = &arqstat->sts_sensedata;
26679 			} else {
26680 				sensep = NULL;
26681 			}
26682 
26683 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26684 			    (rc == STATUS_GOOD)) {
26685 				/* No error - we're done */
26686 				rval = SD_SUCCESS;
26687 				break;
26688 
26689 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26690 				/* Lost connection - give up */
26691 				break;
26692 
26693 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26694 			    (pkt->pkt_state == 0)) {
26695 				/* Pkt not dispatched - try again. */
26696 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
26697 
26698 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26699 			    (rc == STATUS_QFULL)) {
26700 				/* Queue full - try again. */
26701 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
26702 
26703 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26704 			    (rc == STATUS_BUSY)) {
26705 				/* Busy - try again. */
26706 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
26707 				busy_count += (SD_SEC_TO_CSEC - 1);
26708 
26709 			} else if ((sensep != NULL) &&
26710 			    (sensep->es_key == KEY_UNIT_ATTENTION)) {
26711 				/* Unit Attention - try again */
26712 				busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */
26713 				continue;
26714 
26715 			} else if ((sensep != NULL) &&
26716 			    (sensep->es_key == KEY_NOT_READY) &&
26717 			    (sensep->es_add_code == 0x04) &&
26718 			    (sensep->es_qual_code == 0x01)) {
26719 				/* Not ready -> ready - try again. */
26720 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
26721 				busy_count += (SD_SEC_TO_CSEC - 1);
26722 
26723 			} else {
26724 				/* BAD status - give up. */
26725 				break;
26726 			}
26727 		}
26728 
26729 		if ((curthread->t_flag & T_INTR_THREAD) == 0 &&
26730 		    !do_polled_io) {
26731 			delay(drv_usectohz(poll_delay));
26732 		} else {
26733 			/* we busy wait during cpr_dump or interrupt threads */
26734 			drv_usecwait(poll_delay);
26735 		}
26736 	}
26737 
26738 	pkt->pkt_flags = savef;
26739 	pkt->pkt_comp = savec;
26740 	pkt->pkt_time = savet;
26741 	return (rval);
26742 }
26743 
26744 
26745 /*
26746  *    Function: sd_persistent_reservation_in_read_keys
26747  *
26748  * Description: This routine is the driver entry point for handling CD-ROM
26749  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26750  *		by sending the SCSI-3 PRIN commands to the device.
26751  *		Processes the read keys command response by copying the
26752  *		reservation key information into the user provided buffer.
26753  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26754  *
26755  *   Arguments: un   -  Pointer to soft state struct for the target.
26756  *		usrp -	user provided pointer to multihost Persistent In Read
26757  *			Keys structure (mhioc_inkeys_t)
26758  *		flag -	this argument is a pass through to ddi_copyxxx()
26759  *			directly from the mode argument of ioctl().
26760  *
26761  * Return Code: 0   - Success
26762  *		EACCES
26763  *		ENOTSUP
26764  *		errno return code from sd_send_scsi_cmd()
26765  *
26766  *     Context: Can sleep. Does not return until command is completed.
26767  */
26768 
26769 static int
26770 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26771     mhioc_inkeys_t *usrp, int flag)
26772 {
26773 #ifdef _MULTI_DATAMODEL
26774 	struct mhioc_key_list32	li32;
26775 #endif
26776 	sd_prin_readkeys_t	*in;
26777 	mhioc_inkeys_t		*ptr;
26778 	mhioc_key_list_t	li;
26779 	uchar_t			*data_bufp;
26780 	int 			data_len;
26781 	int			rval;
26782 	size_t			copysz;
26783 
26784 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26785 		return (EINVAL);
26786 	}
26787 	bzero(&li, sizeof (mhioc_key_list_t));
26788 
26789 	/*
26790 	 * Get the listsize from user
26791 	 */
26792 #ifdef _MULTI_DATAMODEL
26793 
26794 	switch (ddi_model_convert_from(flag & FMODELS)) {
26795 	case DDI_MODEL_ILP32:
26796 		copysz = sizeof (struct mhioc_key_list32);
26797 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26798 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26799 			    "sd_persistent_reservation_in_read_keys: "
26800 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26801 			rval = EFAULT;
26802 			goto done;
26803 		}
26804 		li.listsize = li32.listsize;
26805 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26806 		break;
26807 
26808 	case DDI_MODEL_NONE:
26809 		copysz = sizeof (mhioc_key_list_t);
26810 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26811 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26812 			    "sd_persistent_reservation_in_read_keys: "
26813 			    "failed ddi_copyin: mhioc_key_list_t\n");
26814 			rval = EFAULT;
26815 			goto done;
26816 		}
26817 		break;
26818 	}
26819 
26820 #else /* ! _MULTI_DATAMODEL */
26821 	copysz = sizeof (mhioc_key_list_t);
26822 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26823 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26824 		    "sd_persistent_reservation_in_read_keys: "
26825 		    "failed ddi_copyin: mhioc_key_list_t\n");
26826 		rval = EFAULT;
26827 		goto done;
26828 	}
26829 #endif
26830 
26831 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26832 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26833 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26834 
26835 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS,
26836 	    data_len, data_bufp)) != 0) {
26837 		goto done;
26838 	}
26839 	in = (sd_prin_readkeys_t *)data_bufp;
26840 	ptr->generation = BE_32(in->generation);
26841 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26842 
26843 	/*
26844 	 * Return the min(listsize, listlen) keys
26845 	 */
26846 #ifdef _MULTI_DATAMODEL
26847 
26848 	switch (ddi_model_convert_from(flag & FMODELS)) {
26849 	case DDI_MODEL_ILP32:
26850 		li32.listlen = li.listlen;
26851 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26852 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26853 			    "sd_persistent_reservation_in_read_keys: "
26854 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26855 			rval = EFAULT;
26856 			goto done;
26857 		}
26858 		break;
26859 
26860 	case DDI_MODEL_NONE:
26861 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26862 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26863 			    "sd_persistent_reservation_in_read_keys: "
26864 			    "failed ddi_copyout: mhioc_key_list_t\n");
26865 			rval = EFAULT;
26866 			goto done;
26867 		}
26868 		break;
26869 	}
26870 
26871 #else /* ! _MULTI_DATAMODEL */
26872 
26873 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26874 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26875 		    "sd_persistent_reservation_in_read_keys: "
26876 		    "failed ddi_copyout: mhioc_key_list_t\n");
26877 		rval = EFAULT;
26878 		goto done;
26879 	}
26880 
26881 #endif /* _MULTI_DATAMODEL */
26882 
26883 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26884 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26885 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26886 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26887 		    "sd_persistent_reservation_in_read_keys: "
26888 		    "failed ddi_copyout: keylist\n");
26889 		rval = EFAULT;
26890 	}
26891 done:
26892 	kmem_free(data_bufp, data_len);
26893 	return (rval);
26894 }
26895 
26896 
26897 /*
26898  *    Function: sd_persistent_reservation_in_read_resv
26899  *
26900  * Description: This routine is the driver entry point for handling CD-ROM
26901  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
26902  *		by sending the SCSI-3 PRIN commands to the device.
26903  *		Process the read persistent reservations command response by
26904  *		copying the reservation information into the user provided
26905  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26906  *
26907  *   Arguments: un   -  Pointer to soft state struct for the target.
26908  *		usrp -	user provided pointer to multihost Persistent In Read
26909  *			Keys structure (mhioc_inkeys_t)
26910  *		flag -	this argument is a pass through to ddi_copyxxx()
26911  *			directly from the mode argument of ioctl().
26912  *
26913  * Return Code: 0   - Success
26914  *		EACCES
26915  *		ENOTSUP
26916  *		errno return code from sd_send_scsi_cmd()
26917  *
26918  *     Context: Can sleep. Does not return until command is completed.
26919  */
26920 
26921 static int
26922 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26923     mhioc_inresvs_t *usrp, int flag)
26924 {
26925 #ifdef _MULTI_DATAMODEL
26926 	struct mhioc_resv_desc_list32 resvlist32;
26927 #endif
26928 	sd_prin_readresv_t	*in;
26929 	mhioc_inresvs_t		*ptr;
26930 	sd_readresv_desc_t	*readresv_ptr;
26931 	mhioc_resv_desc_list_t	resvlist;
26932 	mhioc_resv_desc_t 	resvdesc;
26933 	uchar_t			*data_bufp;
26934 	int 			data_len;
26935 	int			rval;
26936 	int			i;
26937 	size_t			copysz;
26938 	mhioc_resv_desc_t	*bufp;
26939 
26940 	if ((ptr = usrp) == NULL) {
26941 		return (EINVAL);
26942 	}
26943 
26944 	/*
26945 	 * Get the listsize from user
26946 	 */
26947 #ifdef _MULTI_DATAMODEL
26948 	switch (ddi_model_convert_from(flag & FMODELS)) {
26949 	case DDI_MODEL_ILP32:
26950 		copysz = sizeof (struct mhioc_resv_desc_list32);
26951 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26952 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26953 			    "sd_persistent_reservation_in_read_resv: "
26954 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26955 			rval = EFAULT;
26956 			goto done;
26957 		}
26958 		resvlist.listsize = resvlist32.listsize;
26959 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26960 		break;
26961 
26962 	case DDI_MODEL_NONE:
26963 		copysz = sizeof (mhioc_resv_desc_list_t);
26964 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26965 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26966 			    "sd_persistent_reservation_in_read_resv: "
26967 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26968 			rval = EFAULT;
26969 			goto done;
26970 		}
26971 		break;
26972 	}
26973 #else /* ! _MULTI_DATAMODEL */
26974 	copysz = sizeof (mhioc_resv_desc_list_t);
26975 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26976 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26977 		    "sd_persistent_reservation_in_read_resv: "
26978 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26979 		rval = EFAULT;
26980 		goto done;
26981 	}
26982 #endif /* ! _MULTI_DATAMODEL */
26983 
26984 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26985 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26986 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26987 
26988 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV,
26989 	    data_len, data_bufp)) != 0) {
26990 		goto done;
26991 	}
26992 	in = (sd_prin_readresv_t *)data_bufp;
26993 	ptr->generation = BE_32(in->generation);
26994 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26995 
26996 	/*
26997 	 * Return the min(listsize, listlen( keys
26998 	 */
26999 #ifdef _MULTI_DATAMODEL
27000 
27001 	switch (ddi_model_convert_from(flag & FMODELS)) {
27002 	case DDI_MODEL_ILP32:
27003 		resvlist32.listlen = resvlist.listlen;
27004 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
27005 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27006 			    "sd_persistent_reservation_in_read_resv: "
27007 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27008 			rval = EFAULT;
27009 			goto done;
27010 		}
27011 		break;
27012 
27013 	case DDI_MODEL_NONE:
27014 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
27015 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27016 			    "sd_persistent_reservation_in_read_resv: "
27017 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27018 			rval = EFAULT;
27019 			goto done;
27020 		}
27021 		break;
27022 	}
27023 
27024 #else /* ! _MULTI_DATAMODEL */
27025 
27026 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
27027 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
27028 		    "sd_persistent_reservation_in_read_resv: "
27029 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
27030 		rval = EFAULT;
27031 		goto done;
27032 	}
27033 
27034 #endif /* ! _MULTI_DATAMODEL */
27035 
27036 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
27037 	bufp = resvlist.list;
27038 	copysz = sizeof (mhioc_resv_desc_t);
27039 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
27040 	    i++, readresv_ptr++, bufp++) {
27041 
27042 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
27043 		    MHIOC_RESV_KEY_SIZE);
27044 		resvdesc.type  = readresv_ptr->type;
27045 		resvdesc.scope = readresv_ptr->scope;
27046 		resvdesc.scope_specific_addr =
27047 		    BE_32(readresv_ptr->scope_specific_addr);
27048 
27049 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
27050 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
27051 			    "sd_persistent_reservation_in_read_resv: "
27052 			    "failed ddi_copyout: resvlist\n");
27053 			rval = EFAULT;
27054 			goto done;
27055 		}
27056 	}
27057 done:
27058 	kmem_free(data_bufp, data_len);
27059 	return (rval);
27060 }
27061 
27062 
27063 /*
27064  *    Function: sr_change_blkmode()
27065  *
27066  * Description: This routine is the driver entry point for handling CD-ROM
27067  *		block mode ioctl requests. Support for returning and changing
27068  *		the current block size in use by the device is implemented. The
27069  *		LBA size is changed via a MODE SELECT Block Descriptor.
27070  *
27071  *		This routine issues a mode sense with an allocation length of
27072  *		12 bytes for the mode page header and a single block descriptor.
27073  *
27074  *   Arguments: dev - the device 'dev_t'
27075  *		cmd - the request type; one of CDROMGBLKMODE (get) or
27076  *		      CDROMSBLKMODE (set)
27077  *		data - current block size or requested block size
27078  *		flag - this argument is a pass through to ddi_copyxxx() directly
27079  *		       from the mode argument of ioctl().
27080  *
27081  * Return Code: the code returned by sd_send_scsi_cmd()
27082  *		EINVAL if invalid arguments are provided
27083  *		EFAULT if ddi_copyxxx() fails
27084  *		ENXIO if fail ddi_get_soft_state
27085  *		EIO if invalid mode sense block descriptor length
27086  *
27087  */
27088 
27089 static int
27090 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
27091 {
27092 	struct sd_lun			*un = NULL;
27093 	struct mode_header		*sense_mhp, *select_mhp;
27094 	struct block_descriptor		*sense_desc, *select_desc;
27095 	int				current_bsize;
27096 	int				rval = EINVAL;
27097 	uchar_t				*sense = NULL;
27098 	uchar_t				*select = NULL;
27099 
27100 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
27101 
27102 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27103 		return (ENXIO);
27104 	}
27105 
27106 	/*
27107 	 * The block length is changed via the Mode Select block descriptor, the
27108 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
27109 	 * required as part of this routine. Therefore the mode sense allocation
27110 	 * length is specified to be the length of a mode page header and a
27111 	 * block descriptor.
27112 	 */
27113 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27114 
27115 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
27116 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) {
27117 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27118 		    "sr_change_blkmode: Mode Sense Failed\n");
27119 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27120 		return (rval);
27121 	}
27122 
27123 	/* Check the block descriptor len to handle only 1 block descriptor */
27124 	sense_mhp = (struct mode_header *)sense;
27125 	if ((sense_mhp->bdesc_length == 0) ||
27126 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
27127 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27128 		    "sr_change_blkmode: Mode Sense returned invalid block"
27129 		    " descriptor length\n");
27130 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27131 		return (EIO);
27132 	}
27133 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
27134 	current_bsize = ((sense_desc->blksize_hi << 16) |
27135 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
27136 
27137 	/* Process command */
27138 	switch (cmd) {
27139 	case CDROMGBLKMODE:
27140 		/* Return the block size obtained during the mode sense */
27141 		if (ddi_copyout(&current_bsize, (void *)data,
27142 		    sizeof (int), flag) != 0)
27143 			rval = EFAULT;
27144 		break;
27145 	case CDROMSBLKMODE:
27146 		/* Validate the requested block size */
27147 		switch (data) {
27148 		case CDROM_BLK_512:
27149 		case CDROM_BLK_1024:
27150 		case CDROM_BLK_2048:
27151 		case CDROM_BLK_2056:
27152 		case CDROM_BLK_2336:
27153 		case CDROM_BLK_2340:
27154 		case CDROM_BLK_2352:
27155 		case CDROM_BLK_2368:
27156 		case CDROM_BLK_2448:
27157 		case CDROM_BLK_2646:
27158 		case CDROM_BLK_2647:
27159 			break;
27160 		default:
27161 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27162 			    "sr_change_blkmode: "
27163 			    "Block Size '%ld' Not Supported\n", data);
27164 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27165 			return (EINVAL);
27166 		}
27167 
27168 		/*
27169 		 * The current block size matches the requested block size so
27170 		 * there is no need to send the mode select to change the size
27171 		 */
27172 		if (current_bsize == data) {
27173 			break;
27174 		}
27175 
27176 		/* Build the select data for the requested block size */
27177 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27178 		select_mhp = (struct mode_header *)select;
27179 		select_desc =
27180 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
27181 		/*
27182 		 * The LBA size is changed via the block descriptor, so the
27183 		 * descriptor is built according to the user data
27184 		 */
27185 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
27186 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
27187 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
27188 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
27189 
27190 		/* Send the mode select for the requested block size */
27191 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
27192 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27193 		    SD_PATH_STANDARD)) != 0) {
27194 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27195 			    "sr_change_blkmode: Mode Select Failed\n");
27196 			/*
27197 			 * The mode select failed for the requested block size,
27198 			 * so reset the data for the original block size and
27199 			 * send it to the target. The error is indicated by the
27200 			 * return value for the failed mode select.
27201 			 */
27202 			select_desc->blksize_hi  = sense_desc->blksize_hi;
27203 			select_desc->blksize_mid = sense_desc->blksize_mid;
27204 			select_desc->blksize_lo  = sense_desc->blksize_lo;
27205 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
27206 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27207 			    SD_PATH_STANDARD);
27208 		} else {
27209 			ASSERT(!mutex_owned(SD_MUTEX(un)));
27210 			mutex_enter(SD_MUTEX(un));
27211 			sd_update_block_info(un, (uint32_t)data, 0);
27212 
27213 			mutex_exit(SD_MUTEX(un));
27214 		}
27215 		break;
27216 	default:
27217 		/* should not reach here, but check anyway */
27218 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27219 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27220 		rval = EINVAL;
27221 		break;
27222 	}
27223 
27224 	if (select) {
27225 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
27226 	}
27227 	if (sense) {
27228 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27229 	}
27230 	return (rval);
27231 }
27232 
27233 
27234 /*
27235  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27236  * implement driver support for getting and setting the CD speed. The command
27237  * set used will be based on the device type. If the device has not been
27238  * identified as MMC the Toshiba vendor specific mode page will be used. If
27239  * the device is MMC but does not support the Real Time Streaming feature
27240  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27241  * be used to read the speed.
27242  */
27243 
27244 /*
27245  *    Function: sr_change_speed()
27246  *
27247  * Description: This routine is the driver entry point for handling CD-ROM
27248  *		drive speed ioctl requests for devices supporting the Toshiba
27249  *		vendor specific drive speed mode page. Support for returning
27250  *		and changing the current drive speed in use by the device is
27251  *		implemented.
27252  *
27253  *   Arguments: dev - the device 'dev_t'
27254  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
27255  *		      CDROMSDRVSPEED (set)
27256  *		data - current drive speed or requested drive speed
27257  *		flag - this argument is a pass through to ddi_copyxxx() directly
27258  *		       from the mode argument of ioctl().
27259  *
27260  * Return Code: the code returned by sd_send_scsi_cmd()
27261  *		EINVAL if invalid arguments are provided
27262  *		EFAULT if ddi_copyxxx() fails
27263  *		ENXIO if fail ddi_get_soft_state
27264  *		EIO if invalid mode sense block descriptor length
27265  */
27266 
27267 static int
27268 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27269 {
27270 	struct sd_lun			*un = NULL;
27271 	struct mode_header		*sense_mhp, *select_mhp;
27272 	struct mode_speed		*sense_page, *select_page;
27273 	int				current_speed;
27274 	int				rval = EINVAL;
27275 	int				bd_len;
27276 	uchar_t				*sense = NULL;
27277 	uchar_t				*select = NULL;
27278 
27279 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27280 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27281 		return (ENXIO);
27282 	}
27283 
27284 	/*
27285 	 * Note: The drive speed is being modified here according to a Toshiba
27286 	 * vendor specific mode page (0x31).
27287 	 */
27288 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27289 
27290 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
27291 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27292 		SD_PATH_STANDARD)) != 0) {
27293 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27294 		    "sr_change_speed: Mode Sense Failed\n");
27295 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27296 		return (rval);
27297 	}
27298 	sense_mhp  = (struct mode_header *)sense;
27299 
27300 	/* Check the block descriptor len to handle only 1 block descriptor */
27301 	bd_len = sense_mhp->bdesc_length;
27302 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27303 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27304 		    "sr_change_speed: Mode Sense returned invalid block "
27305 		    "descriptor length\n");
27306 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27307 		return (EIO);
27308 	}
27309 
27310 	sense_page = (struct mode_speed *)
27311 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27312 	current_speed = sense_page->speed;
27313 
27314 	/* Process command */
27315 	switch (cmd) {
27316 	case CDROMGDRVSPEED:
27317 		/* Return the drive speed obtained during the mode sense */
27318 		if (current_speed == 0x2) {
27319 			current_speed = CDROM_TWELVE_SPEED;
27320 		}
27321 		if (ddi_copyout(&current_speed, (void *)data,
27322 		    sizeof (int), flag) != 0) {
27323 			rval = EFAULT;
27324 		}
27325 		break;
27326 	case CDROMSDRVSPEED:
27327 		/* Validate the requested drive speed */
27328 		switch ((uchar_t)data) {
27329 		case CDROM_TWELVE_SPEED:
27330 			data = 0x2;
27331 			/*FALLTHROUGH*/
27332 		case CDROM_NORMAL_SPEED:
27333 		case CDROM_DOUBLE_SPEED:
27334 		case CDROM_QUAD_SPEED:
27335 		case CDROM_MAXIMUM_SPEED:
27336 			break;
27337 		default:
27338 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27339 			    "sr_change_speed: "
27340 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27341 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27342 			return (EINVAL);
27343 		}
27344 
27345 		/*
27346 		 * The current drive speed matches the requested drive speed so
27347 		 * there is no need to send the mode select to change the speed
27348 		 */
27349 		if (current_speed == data) {
27350 			break;
27351 		}
27352 
27353 		/* Build the select data for the requested drive speed */
27354 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27355 		select_mhp = (struct mode_header *)select;
27356 		select_mhp->bdesc_length = 0;
27357 		select_page =
27358 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27359 		select_page =
27360 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27361 		select_page->mode_page.code = CDROM_MODE_SPEED;
27362 		select_page->mode_page.length = 2;
27363 		select_page->speed = (uchar_t)data;
27364 
27365 		/* Send the mode select for the requested block size */
27366 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
27367 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27368 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
27369 			/*
27370 			 * The mode select failed for the requested drive speed,
27371 			 * so reset the data for the original drive speed and
27372 			 * send it to the target. The error is indicated by the
27373 			 * return value for the failed mode select.
27374 			 */
27375 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27376 			    "sr_drive_speed: Mode Select Failed\n");
27377 			select_page->speed = sense_page->speed;
27378 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
27379 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27380 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27381 		}
27382 		break;
27383 	default:
27384 		/* should not reach here, but check anyway */
27385 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27386 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27387 		rval = EINVAL;
27388 		break;
27389 	}
27390 
27391 	if (select) {
27392 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27393 	}
27394 	if (sense) {
27395 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27396 	}
27397 
27398 	return (rval);
27399 }
27400 
27401 
27402 /*
27403  *    Function: sr_atapi_change_speed()
27404  *
27405  * Description: This routine is the driver entry point for handling CD-ROM
27406  *		drive speed ioctl requests for MMC devices that do not support
27407  *		the Real Time Streaming feature (0x107).
27408  *
27409  *		Note: This routine will use the SET SPEED command which may not
27410  *		be supported by all devices.
27411  *
27412  *   Arguments: dev- the device 'dev_t'
27413  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27414  *		     CDROMSDRVSPEED (set)
27415  *		data- current drive speed or requested drive speed
27416  *		flag- this argument is a pass through to ddi_copyxxx() directly
27417  *		      from the mode argument of ioctl().
27418  *
27419  * Return Code: the code returned by sd_send_scsi_cmd()
27420  *		EINVAL if invalid arguments are provided
27421  *		EFAULT if ddi_copyxxx() fails
27422  *		ENXIO if fail ddi_get_soft_state
27423  *		EIO if invalid mode sense block descriptor length
27424  */
27425 
27426 static int
27427 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27428 {
27429 	struct sd_lun			*un;
27430 	struct uscsi_cmd		*com = NULL;
27431 	struct mode_header_grp2		*sense_mhp;
27432 	uchar_t				*sense_page;
27433 	uchar_t				*sense = NULL;
27434 	char				cdb[CDB_GROUP5];
27435 	int				bd_len;
27436 	int				current_speed = 0;
27437 	int				max_speed = 0;
27438 	int				rval;
27439 
27440 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27441 
27442 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27443 		return (ENXIO);
27444 	}
27445 
27446 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27447 
27448 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
27449 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27450 	    SD_PATH_STANDARD)) != 0) {
27451 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27452 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27453 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27454 		return (rval);
27455 	}
27456 
27457 	/* Check the block descriptor len to handle only 1 block descriptor */
27458 	sense_mhp = (struct mode_header_grp2 *)sense;
27459 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27460 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27461 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27462 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27463 		    "block descriptor length\n");
27464 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27465 		return (EIO);
27466 	}
27467 
27468 	/* Calculate the current and maximum drive speeds */
27469 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27470 	current_speed = (sense_page[14] << 8) | sense_page[15];
27471 	max_speed = (sense_page[8] << 8) | sense_page[9];
27472 
27473 	/* Process the command */
27474 	switch (cmd) {
27475 	case CDROMGDRVSPEED:
27476 		current_speed /= SD_SPEED_1X;
27477 		if (ddi_copyout(&current_speed, (void *)data,
27478 		    sizeof (int), flag) != 0)
27479 			rval = EFAULT;
27480 		break;
27481 	case CDROMSDRVSPEED:
27482 		/* Convert the speed code to KB/sec */
27483 		switch ((uchar_t)data) {
27484 		case CDROM_NORMAL_SPEED:
27485 			current_speed = SD_SPEED_1X;
27486 			break;
27487 		case CDROM_DOUBLE_SPEED:
27488 			current_speed = 2 * SD_SPEED_1X;
27489 			break;
27490 		case CDROM_QUAD_SPEED:
27491 			current_speed = 4 * SD_SPEED_1X;
27492 			break;
27493 		case CDROM_TWELVE_SPEED:
27494 			current_speed = 12 * SD_SPEED_1X;
27495 			break;
27496 		case CDROM_MAXIMUM_SPEED:
27497 			current_speed = 0xffff;
27498 			break;
27499 		default:
27500 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27501 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27502 			    (uchar_t)data);
27503 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27504 			return (EINVAL);
27505 		}
27506 
27507 		/* Check the request against the drive's max speed. */
27508 		if (current_speed != 0xffff) {
27509 			if (current_speed > max_speed) {
27510 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27511 				return (EINVAL);
27512 			}
27513 		}
27514 
27515 		/*
27516 		 * Build and send the SET SPEED command
27517 		 *
27518 		 * Note: The SET SPEED (0xBB) command used in this routine is
27519 		 * obsolete per the SCSI MMC spec but still supported in the
27520 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27521 		 * therefore the command is still implemented in this routine.
27522 		 */
27523 		bzero(cdb, sizeof (cdb));
27524 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27525 		cdb[2] = (uchar_t)(current_speed >> 8);
27526 		cdb[3] = (uchar_t)current_speed;
27527 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27528 		com->uscsi_cdb	   = (caddr_t)cdb;
27529 		com->uscsi_cdblen  = CDB_GROUP5;
27530 		com->uscsi_bufaddr = NULL;
27531 		com->uscsi_buflen  = 0;
27532 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27533 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0,
27534 		    UIO_SYSSPACE, SD_PATH_STANDARD);
27535 		break;
27536 	default:
27537 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27538 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27539 		rval = EINVAL;
27540 	}
27541 
27542 	if (sense) {
27543 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27544 	}
27545 	if (com) {
27546 		kmem_free(com, sizeof (*com));
27547 	}
27548 	return (rval);
27549 }
27550 
27551 
27552 /*
27553  *    Function: sr_pause_resume()
27554  *
27555  * Description: This routine is the driver entry point for handling CD-ROM
27556  *		pause/resume ioctl requests. This only affects the audio play
27557  *		operation.
27558  *
27559  *   Arguments: dev - the device 'dev_t'
27560  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27561  *		      for setting the resume bit of the cdb.
27562  *
27563  * Return Code: the code returned by sd_send_scsi_cmd()
27564  *		EINVAL if invalid mode specified
27565  *
27566  */
27567 
27568 static int
27569 sr_pause_resume(dev_t dev, int cmd)
27570 {
27571 	struct sd_lun		*un;
27572 	struct uscsi_cmd	*com;
27573 	char			cdb[CDB_GROUP1];
27574 	int			rval;
27575 
27576 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27577 		return (ENXIO);
27578 	}
27579 
27580 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27581 	bzero(cdb, CDB_GROUP1);
27582 	cdb[0] = SCMD_PAUSE_RESUME;
27583 	switch (cmd) {
27584 	case CDROMRESUME:
27585 		cdb[8] = 1;
27586 		break;
27587 	case CDROMPAUSE:
27588 		cdb[8] = 0;
27589 		break;
27590 	default:
27591 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27592 		    " Command '%x' Not Supported\n", cmd);
27593 		rval = EINVAL;
27594 		goto done;
27595 	}
27596 
27597 	com->uscsi_cdb    = cdb;
27598 	com->uscsi_cdblen = CDB_GROUP1;
27599 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27600 
27601 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27602 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27603 
27604 done:
27605 	kmem_free(com, sizeof (*com));
27606 	return (rval);
27607 }
27608 
27609 
27610 /*
27611  *    Function: sr_play_msf()
27612  *
27613  * Description: This routine is the driver entry point for handling CD-ROM
27614  *		ioctl requests to output the audio signals at the specified
27615  *		starting address and continue the audio play until the specified
27616  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27617  *		Frame (MSF) format.
27618  *
27619  *   Arguments: dev	- the device 'dev_t'
27620  *		data	- pointer to user provided audio msf structure,
27621  *		          specifying start/end addresses.
27622  *		flag	- this argument is a pass through to ddi_copyxxx()
27623  *		          directly from the mode argument of ioctl().
27624  *
27625  * Return Code: the code returned by sd_send_scsi_cmd()
27626  *		EFAULT if ddi_copyxxx() fails
27627  *		ENXIO if fail ddi_get_soft_state
27628  *		EINVAL if data pointer is NULL
27629  */
27630 
27631 static int
27632 sr_play_msf(dev_t dev, caddr_t data, int flag)
27633 {
27634 	struct sd_lun		*un;
27635 	struct uscsi_cmd	*com;
27636 	struct cdrom_msf	msf_struct;
27637 	struct cdrom_msf	*msf = &msf_struct;
27638 	char			cdb[CDB_GROUP1];
27639 	int			rval;
27640 
27641 	if (data == NULL) {
27642 		return (EINVAL);
27643 	}
27644 
27645 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27646 		return (ENXIO);
27647 	}
27648 
27649 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27650 		return (EFAULT);
27651 	}
27652 
27653 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27654 	bzero(cdb, CDB_GROUP1);
27655 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27656 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27657 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27658 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27659 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27660 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27661 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27662 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27663 	} else {
27664 		cdb[3] = msf->cdmsf_min0;
27665 		cdb[4] = msf->cdmsf_sec0;
27666 		cdb[5] = msf->cdmsf_frame0;
27667 		cdb[6] = msf->cdmsf_min1;
27668 		cdb[7] = msf->cdmsf_sec1;
27669 		cdb[8] = msf->cdmsf_frame1;
27670 	}
27671 	com->uscsi_cdb    = cdb;
27672 	com->uscsi_cdblen = CDB_GROUP1;
27673 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27674 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27675 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27676 	kmem_free(com, sizeof (*com));
27677 	return (rval);
27678 }
27679 
27680 
27681 /*
27682  *    Function: sr_play_trkind()
27683  *
27684  * Description: This routine is the driver entry point for handling CD-ROM
27685  *		ioctl requests to output the audio signals at the specified
27686  *		starting address and continue the audio play until the specified
27687  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27688  *		format.
27689  *
27690  *   Arguments: dev	- the device 'dev_t'
27691  *		data	- pointer to user provided audio track/index structure,
27692  *		          specifying start/end addresses.
27693  *		flag	- this argument is a pass through to ddi_copyxxx()
27694  *		          directly from the mode argument of ioctl().
27695  *
27696  * Return Code: the code returned by sd_send_scsi_cmd()
27697  *		EFAULT if ddi_copyxxx() fails
27698  *		ENXIO if fail ddi_get_soft_state
27699  *		EINVAL if data pointer is NULL
27700  */
27701 
27702 static int
27703 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27704 {
27705 	struct cdrom_ti		ti_struct;
27706 	struct cdrom_ti		*ti = &ti_struct;
27707 	struct uscsi_cmd	*com = NULL;
27708 	char			cdb[CDB_GROUP1];
27709 	int			rval;
27710 
27711 	if (data == NULL) {
27712 		return (EINVAL);
27713 	}
27714 
27715 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27716 		return (EFAULT);
27717 	}
27718 
27719 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27720 	bzero(cdb, CDB_GROUP1);
27721 	cdb[0] = SCMD_PLAYAUDIO_TI;
27722 	cdb[4] = ti->cdti_trk0;
27723 	cdb[5] = ti->cdti_ind0;
27724 	cdb[7] = ti->cdti_trk1;
27725 	cdb[8] = ti->cdti_ind1;
27726 	com->uscsi_cdb    = cdb;
27727 	com->uscsi_cdblen = CDB_GROUP1;
27728 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27729 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27730 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27731 	kmem_free(com, sizeof (*com));
27732 	return (rval);
27733 }
27734 
27735 
27736 /*
27737  *    Function: sr_read_all_subcodes()
27738  *
27739  * Description: This routine is the driver entry point for handling CD-ROM
27740  *		ioctl requests to return raw subcode data while the target is
27741  *		playing audio (CDROMSUBCODE).
27742  *
27743  *   Arguments: dev	- the device 'dev_t'
27744  *		data	- pointer to user provided cdrom subcode structure,
27745  *		          specifying the transfer length and address.
27746  *		flag	- this argument is a pass through to ddi_copyxxx()
27747  *		          directly from the mode argument of ioctl().
27748  *
27749  * Return Code: the code returned by sd_send_scsi_cmd()
27750  *		EFAULT if ddi_copyxxx() fails
27751  *		ENXIO if fail ddi_get_soft_state
27752  *		EINVAL if data pointer is NULL
27753  */
27754 
27755 static int
27756 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27757 {
27758 	struct sd_lun		*un = NULL;
27759 	struct uscsi_cmd	*com = NULL;
27760 	struct cdrom_subcode	*subcode = NULL;
27761 	int			rval;
27762 	size_t			buflen;
27763 	char			cdb[CDB_GROUP5];
27764 
27765 #ifdef _MULTI_DATAMODEL
27766 	/* To support ILP32 applications in an LP64 world */
27767 	struct cdrom_subcode32		cdrom_subcode32;
27768 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27769 #endif
27770 	if (data == NULL) {
27771 		return (EINVAL);
27772 	}
27773 
27774 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27775 		return (ENXIO);
27776 	}
27777 
27778 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27779 
27780 #ifdef _MULTI_DATAMODEL
27781 	switch (ddi_model_convert_from(flag & FMODELS)) {
27782 	case DDI_MODEL_ILP32:
27783 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27784 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27785 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27786 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27787 			return (EFAULT);
27788 		}
27789 		/* Convert the ILP32 uscsi data from the application to LP64 */
27790 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27791 		break;
27792 	case DDI_MODEL_NONE:
27793 		if (ddi_copyin(data, subcode,
27794 		    sizeof (struct cdrom_subcode), flag)) {
27795 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27796 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27797 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27798 			return (EFAULT);
27799 		}
27800 		break;
27801 	}
27802 #else /* ! _MULTI_DATAMODEL */
27803 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27804 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27805 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27806 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27807 		return (EFAULT);
27808 	}
27809 #endif /* _MULTI_DATAMODEL */
27810 
27811 	/*
27812 	 * Since MMC-2 expects max 3 bytes for length, check if the
27813 	 * length input is greater than 3 bytes
27814 	 */
27815 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27816 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27817 		    "sr_read_all_subcodes: "
27818 		    "cdrom transfer length too large: %d (limit %d)\n",
27819 		    subcode->cdsc_length, 0xFFFFFF);
27820 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27821 		return (EINVAL);
27822 	}
27823 
27824 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27825 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27826 	bzero(cdb, CDB_GROUP5);
27827 
27828 	if (un->un_f_mmc_cap == TRUE) {
27829 		cdb[0] = (char)SCMD_READ_CD;
27830 		cdb[2] = (char)0xff;
27831 		cdb[3] = (char)0xff;
27832 		cdb[4] = (char)0xff;
27833 		cdb[5] = (char)0xff;
27834 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27835 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27836 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27837 		cdb[10] = 1;
27838 	} else {
27839 		/*
27840 		 * Note: A vendor specific command (0xDF) is being used her to
27841 		 * request a read of all subcodes.
27842 		 */
27843 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27844 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27845 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27846 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27847 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27848 	}
27849 	com->uscsi_cdb	   = cdb;
27850 	com->uscsi_cdblen  = CDB_GROUP5;
27851 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27852 	com->uscsi_buflen  = buflen;
27853 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27854 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
27855 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27856 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27857 	kmem_free(com, sizeof (*com));
27858 	return (rval);
27859 }
27860 
27861 
27862 /*
27863  *    Function: sr_read_subchannel()
27864  *
27865  * Description: This routine is the driver entry point for handling CD-ROM
27866  *		ioctl requests to return the Q sub-channel data of the CD
27867  *		current position block. (CDROMSUBCHNL) The data includes the
27868  *		track number, index number, absolute CD-ROM address (LBA or MSF
27869  *		format per the user) , track relative CD-ROM address (LBA or MSF
27870  *		format per the user), control data and audio status.
27871  *
27872  *   Arguments: dev	- the device 'dev_t'
27873  *		data	- pointer to user provided cdrom sub-channel structure
27874  *		flag	- this argument is a pass through to ddi_copyxxx()
27875  *		          directly from the mode argument of ioctl().
27876  *
27877  * Return Code: the code returned by sd_send_scsi_cmd()
27878  *		EFAULT if ddi_copyxxx() fails
27879  *		ENXIO if fail ddi_get_soft_state
27880  *		EINVAL if data pointer is NULL
27881  */
27882 
27883 static int
27884 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27885 {
27886 	struct sd_lun		*un;
27887 	struct uscsi_cmd	*com;
27888 	struct cdrom_subchnl	subchanel;
27889 	struct cdrom_subchnl	*subchnl = &subchanel;
27890 	char			cdb[CDB_GROUP1];
27891 	caddr_t			buffer;
27892 	int			rval;
27893 
27894 	if (data == NULL) {
27895 		return (EINVAL);
27896 	}
27897 
27898 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27899 	    (un->un_state == SD_STATE_OFFLINE)) {
27900 		return (ENXIO);
27901 	}
27902 
27903 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27904 		return (EFAULT);
27905 	}
27906 
27907 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27908 	bzero(cdb, CDB_GROUP1);
27909 	cdb[0] = SCMD_READ_SUBCHANNEL;
27910 	/* Set the MSF bit based on the user requested address format */
27911 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27912 	/*
27913 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27914 	 * returned
27915 	 */
27916 	cdb[2] = 0x40;
27917 	/*
27918 	 * Set byte 3 to specify the return data format. A value of 0x01
27919 	 * indicates that the CD-ROM current position should be returned.
27920 	 */
27921 	cdb[3] = 0x01;
27922 	cdb[8] = 0x10;
27923 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27924 	com->uscsi_cdb	   = cdb;
27925 	com->uscsi_cdblen  = CDB_GROUP1;
27926 	com->uscsi_bufaddr = buffer;
27927 	com->uscsi_buflen  = 16;
27928 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27929 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27930 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27931 	if (rval != 0) {
27932 		kmem_free(buffer, 16);
27933 		kmem_free(com, sizeof (*com));
27934 		return (rval);
27935 	}
27936 
27937 	/* Process the returned Q sub-channel data */
27938 	subchnl->cdsc_audiostatus = buffer[1];
27939 	subchnl->cdsc_adr	= (buffer[5] & 0xF0);
27940 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
27941 	subchnl->cdsc_trk	= buffer[6];
27942 	subchnl->cdsc_ind	= buffer[7];
27943 	if (subchnl->cdsc_format & CDROM_LBA) {
27944 		subchnl->cdsc_absaddr.lba =
27945 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27946 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27947 		subchnl->cdsc_reladdr.lba =
27948 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27949 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27950 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
27951 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27952 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27953 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27954 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27955 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27956 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27957 	} else {
27958 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
27959 		subchnl->cdsc_absaddr.msf.second = buffer[10];
27960 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27961 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
27962 		subchnl->cdsc_reladdr.msf.second = buffer[14];
27963 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27964 	}
27965 	kmem_free(buffer, 16);
27966 	kmem_free(com, sizeof (*com));
27967 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27968 	    != 0) {
27969 		return (EFAULT);
27970 	}
27971 	return (rval);
27972 }
27973 
27974 
27975 /*
27976  *    Function: sr_read_tocentry()
27977  *
27978  * Description: This routine is the driver entry point for handling CD-ROM
27979  *		ioctl requests to read from the Table of Contents (TOC)
27980  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27981  *		fields, the starting address (LBA or MSF format per the user)
27982  *		and the data mode if the user specified track is a data track.
27983  *
27984  *		Note: The READ HEADER (0x44) command used in this routine is
27985  *		obsolete per the SCSI MMC spec but still supported in the
27986  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27987  *		therefore the command is still implemented in this routine.
27988  *
27989  *   Arguments: dev	- the device 'dev_t'
27990  *		data	- pointer to user provided toc entry structure,
27991  *			  specifying the track # and the address format
27992  *			  (LBA or MSF).
27993  *		flag	- this argument is a pass through to ddi_copyxxx()
27994  *		          directly from the mode argument of ioctl().
27995  *
27996  * Return Code: the code returned by sd_send_scsi_cmd()
27997  *		EFAULT if ddi_copyxxx() fails
27998  *		ENXIO if fail ddi_get_soft_state
27999  *		EINVAL if data pointer is NULL
28000  */
28001 
28002 static int
28003 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
28004 {
28005 	struct sd_lun		*un = NULL;
28006 	struct uscsi_cmd	*com;
28007 	struct cdrom_tocentry	toc_entry;
28008 	struct cdrom_tocentry	*entry = &toc_entry;
28009 	caddr_t			buffer;
28010 	int			rval;
28011 	char			cdb[CDB_GROUP1];
28012 
28013 	if (data == NULL) {
28014 		return (EINVAL);
28015 	}
28016 
28017 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28018 	    (un->un_state == SD_STATE_OFFLINE)) {
28019 		return (ENXIO);
28020 	}
28021 
28022 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
28023 		return (EFAULT);
28024 	}
28025 
28026 	/* Validate the requested track and address format */
28027 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
28028 		return (EINVAL);
28029 	}
28030 
28031 	if (entry->cdte_track == 0) {
28032 		return (EINVAL);
28033 	}
28034 
28035 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
28036 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28037 	bzero(cdb, CDB_GROUP1);
28038 
28039 	cdb[0] = SCMD_READ_TOC;
28040 	/* Set the MSF bit based on the user requested address format  */
28041 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
28042 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28043 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
28044 	} else {
28045 		cdb[6] = entry->cdte_track;
28046 	}
28047 
28048 	/*
28049 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
28050 	 * (4 byte TOC response header + 8 byte track descriptor)
28051 	 */
28052 	cdb[8] = 12;
28053 	com->uscsi_cdb	   = cdb;
28054 	com->uscsi_cdblen  = CDB_GROUP1;
28055 	com->uscsi_bufaddr = buffer;
28056 	com->uscsi_buflen  = 0x0C;
28057 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
28058 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28059 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28060 	if (rval != 0) {
28061 		kmem_free(buffer, 12);
28062 		kmem_free(com, sizeof (*com));
28063 		return (rval);
28064 	}
28065 
28066 	/* Process the toc entry */
28067 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
28068 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
28069 	if (entry->cdte_format & CDROM_LBA) {
28070 		entry->cdte_addr.lba =
28071 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
28072 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
28073 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
28074 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
28075 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
28076 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
28077 		/*
28078 		 * Send a READ TOC command using the LBA address format to get
28079 		 * the LBA for the track requested so it can be used in the
28080 		 * READ HEADER request
28081 		 *
28082 		 * Note: The MSF bit of the READ HEADER command specifies the
28083 		 * output format. The block address specified in that command
28084 		 * must be in LBA format.
28085 		 */
28086 		cdb[1] = 0;
28087 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28088 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28089 		if (rval != 0) {
28090 			kmem_free(buffer, 12);
28091 			kmem_free(com, sizeof (*com));
28092 			return (rval);
28093 		}
28094 	} else {
28095 		entry->cdte_addr.msf.minute	= buffer[9];
28096 		entry->cdte_addr.msf.second	= buffer[10];
28097 		entry->cdte_addr.msf.frame	= buffer[11];
28098 		/*
28099 		 * Send a READ TOC command using the LBA address format to get
28100 		 * the LBA for the track requested so it can be used in the
28101 		 * READ HEADER request
28102 		 *
28103 		 * Note: The MSF bit of the READ HEADER command specifies the
28104 		 * output format. The block address specified in that command
28105 		 * must be in LBA format.
28106 		 */
28107 		cdb[1] = 0;
28108 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28109 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28110 		if (rval != 0) {
28111 			kmem_free(buffer, 12);
28112 			kmem_free(com, sizeof (*com));
28113 			return (rval);
28114 		}
28115 	}
28116 
28117 	/*
28118 	 * Build and send the READ HEADER command to determine the data mode of
28119 	 * the user specified track.
28120 	 */
28121 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
28122 	    (entry->cdte_track != CDROM_LEADOUT)) {
28123 		bzero(cdb, CDB_GROUP1);
28124 		cdb[0] = SCMD_READ_HEADER;
28125 		cdb[2] = buffer[8];
28126 		cdb[3] = buffer[9];
28127 		cdb[4] = buffer[10];
28128 		cdb[5] = buffer[11];
28129 		cdb[8] = 0x08;
28130 		com->uscsi_buflen = 0x08;
28131 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28132 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28133 		if (rval == 0) {
28134 			entry->cdte_datamode = buffer[0];
28135 		} else {
28136 			/*
28137 			 * READ HEADER command failed, since this is
28138 			 * obsoleted in one spec, its better to return
28139 			 * -1 for an invlid track so that we can still
28140 			 * recieve the rest of the TOC data.
28141 			 */
28142 			entry->cdte_datamode = (uchar_t)-1;
28143 		}
28144 	} else {
28145 		entry->cdte_datamode = (uchar_t)-1;
28146 	}
28147 
28148 	kmem_free(buffer, 12);
28149 	kmem_free(com, sizeof (*com));
28150 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
28151 		return (EFAULT);
28152 
28153 	return (rval);
28154 }
28155 
28156 
28157 /*
28158  *    Function: sr_read_tochdr()
28159  *
28160  * Description: This routine is the driver entry point for handling CD-ROM
28161  * 		ioctl requests to read the Table of Contents (TOC) header
28162  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
28163  *		and ending track numbers
28164  *
28165  *   Arguments: dev	- the device 'dev_t'
28166  *		data	- pointer to user provided toc header structure,
28167  *			  specifying the starting and ending track numbers.
28168  *		flag	- this argument is a pass through to ddi_copyxxx()
28169  *			  directly from the mode argument of ioctl().
28170  *
28171  * Return Code: the code returned by sd_send_scsi_cmd()
28172  *		EFAULT if ddi_copyxxx() fails
28173  *		ENXIO if fail ddi_get_soft_state
28174  *		EINVAL if data pointer is NULL
28175  */
28176 
28177 static int
28178 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
28179 {
28180 	struct sd_lun		*un;
28181 	struct uscsi_cmd	*com;
28182 	struct cdrom_tochdr	toc_header;
28183 	struct cdrom_tochdr	*hdr = &toc_header;
28184 	char			cdb[CDB_GROUP1];
28185 	int			rval;
28186 	caddr_t			buffer;
28187 
28188 	if (data == NULL) {
28189 		return (EINVAL);
28190 	}
28191 
28192 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28193 	    (un->un_state == SD_STATE_OFFLINE)) {
28194 		return (ENXIO);
28195 	}
28196 
28197 	buffer = kmem_zalloc(4, KM_SLEEP);
28198 	bzero(cdb, CDB_GROUP1);
28199 	cdb[0] = SCMD_READ_TOC;
28200 	/*
28201 	 * Specifying a track number of 0x00 in the READ TOC command indicates
28202 	 * that the TOC header should be returned
28203 	 */
28204 	cdb[6] = 0x00;
28205 	/*
28206 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28207 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28208 	 */
28209 	cdb[8] = 0x04;
28210 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28211 	com->uscsi_cdb	   = cdb;
28212 	com->uscsi_cdblen  = CDB_GROUP1;
28213 	com->uscsi_bufaddr = buffer;
28214 	com->uscsi_buflen  = 0x04;
28215 	com->uscsi_timeout = 300;
28216 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28217 
28218 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28219 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28220 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28221 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28222 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28223 	} else {
28224 		hdr->cdth_trk0 = buffer[2];
28225 		hdr->cdth_trk1 = buffer[3];
28226 	}
28227 	kmem_free(buffer, 4);
28228 	kmem_free(com, sizeof (*com));
28229 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28230 		return (EFAULT);
28231 	}
28232 	return (rval);
28233 }
28234 
28235 
28236 /*
28237  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28238  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28239  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28240  * digital audio and extended architecture digital audio. These modes are
28241  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28242  * MMC specs.
28243  *
28244  * In addition to support for the various data formats these routines also
28245  * include support for devices that implement only the direct access READ
28246  * commands (0x08, 0x28), devices that implement the READ_CD commands
28247  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28248  * READ CDXA commands (0xD8, 0xDB)
28249  */
28250 
28251 /*
28252  *    Function: sr_read_mode1()
28253  *
28254  * Description: This routine is the driver entry point for handling CD-ROM
28255  *		ioctl read mode1 requests (CDROMREADMODE1).
28256  *
28257  *   Arguments: dev	- the device 'dev_t'
28258  *		data	- pointer to user provided cd read structure specifying
28259  *			  the lba buffer address and length.
28260  *		flag	- this argument is a pass through to ddi_copyxxx()
28261  *			  directly from the mode argument of ioctl().
28262  *
28263  * Return Code: the code returned by sd_send_scsi_cmd()
28264  *		EFAULT if ddi_copyxxx() fails
28265  *		ENXIO if fail ddi_get_soft_state
28266  *		EINVAL if data pointer is NULL
28267  */
28268 
28269 static int
28270 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28271 {
28272 	struct sd_lun		*un;
28273 	struct cdrom_read	mode1_struct;
28274 	struct cdrom_read	*mode1 = &mode1_struct;
28275 	int			rval;
28276 #ifdef _MULTI_DATAMODEL
28277 	/* To support ILP32 applications in an LP64 world */
28278 	struct cdrom_read32	cdrom_read32;
28279 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28280 #endif /* _MULTI_DATAMODEL */
28281 
28282 	if (data == NULL) {
28283 		return (EINVAL);
28284 	}
28285 
28286 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28287 	    (un->un_state == SD_STATE_OFFLINE)) {
28288 		return (ENXIO);
28289 	}
28290 
28291 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28292 	    "sd_read_mode1: entry: un:0x%p\n", un);
28293 
28294 #ifdef _MULTI_DATAMODEL
28295 	switch (ddi_model_convert_from(flag & FMODELS)) {
28296 	case DDI_MODEL_ILP32:
28297 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28298 			return (EFAULT);
28299 		}
28300 		/* Convert the ILP32 uscsi data from the application to LP64 */
28301 		cdrom_read32tocdrom_read(cdrd32, mode1);
28302 		break;
28303 	case DDI_MODEL_NONE:
28304 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28305 			return (EFAULT);
28306 		}
28307 	}
28308 #else /* ! _MULTI_DATAMODEL */
28309 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28310 		return (EFAULT);
28311 	}
28312 #endif /* _MULTI_DATAMODEL */
28313 
28314 	rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr,
28315 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28316 
28317 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28318 	    "sd_read_mode1: exit: un:0x%p\n", un);
28319 
28320 	return (rval);
28321 }
28322 
28323 
28324 /*
28325  *    Function: sr_read_cd_mode2()
28326  *
28327  * Description: This routine is the driver entry point for handling CD-ROM
28328  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28329  *		support the READ CD (0xBE) command or the 1st generation
28330  *		READ CD (0xD4) command.
28331  *
28332  *   Arguments: dev	- the device 'dev_t'
28333  *		data	- pointer to user provided cd read structure specifying
28334  *			  the lba buffer address and length.
28335  *		flag	- this argument is a pass through to ddi_copyxxx()
28336  *			  directly from the mode argument of ioctl().
28337  *
28338  * Return Code: the code returned by sd_send_scsi_cmd()
28339  *		EFAULT if ddi_copyxxx() fails
28340  *		ENXIO if fail ddi_get_soft_state
28341  *		EINVAL if data pointer is NULL
28342  */
28343 
28344 static int
28345 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28346 {
28347 	struct sd_lun		*un;
28348 	struct uscsi_cmd	*com;
28349 	struct cdrom_read	mode2_struct;
28350 	struct cdrom_read	*mode2 = &mode2_struct;
28351 	uchar_t			cdb[CDB_GROUP5];
28352 	int			nblocks;
28353 	int			rval;
28354 #ifdef _MULTI_DATAMODEL
28355 	/*  To support ILP32 applications in an LP64 world */
28356 	struct cdrom_read32	cdrom_read32;
28357 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28358 #endif /* _MULTI_DATAMODEL */
28359 
28360 	if (data == NULL) {
28361 		return (EINVAL);
28362 	}
28363 
28364 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28365 	    (un->un_state == SD_STATE_OFFLINE)) {
28366 		return (ENXIO);
28367 	}
28368 
28369 #ifdef _MULTI_DATAMODEL
28370 	switch (ddi_model_convert_from(flag & FMODELS)) {
28371 	case DDI_MODEL_ILP32:
28372 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28373 			return (EFAULT);
28374 		}
28375 		/* Convert the ILP32 uscsi data from the application to LP64 */
28376 		cdrom_read32tocdrom_read(cdrd32, mode2);
28377 		break;
28378 	case DDI_MODEL_NONE:
28379 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28380 			return (EFAULT);
28381 		}
28382 		break;
28383 	}
28384 
28385 #else /* ! _MULTI_DATAMODEL */
28386 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28387 		return (EFAULT);
28388 	}
28389 #endif /* _MULTI_DATAMODEL */
28390 
28391 	bzero(cdb, sizeof (cdb));
28392 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28393 		/* Read command supported by 1st generation atapi drives */
28394 		cdb[0] = SCMD_READ_CDD4;
28395 	} else {
28396 		/* Universal CD Access Command */
28397 		cdb[0] = SCMD_READ_CD;
28398 	}
28399 
28400 	/*
28401 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28402 	 */
28403 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28404 
28405 	/* set the start address */
28406 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28407 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28408 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28409 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28410 
28411 	/* set the transfer length */
28412 	nblocks = mode2->cdread_buflen / 2336;
28413 	cdb[6] = (uchar_t)(nblocks >> 16);
28414 	cdb[7] = (uchar_t)(nblocks >> 8);
28415 	cdb[8] = (uchar_t)nblocks;
28416 
28417 	/* set the filter bits */
28418 	cdb[9] = CDROM_READ_CD_USERDATA;
28419 
28420 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28421 	com->uscsi_cdb = (caddr_t)cdb;
28422 	com->uscsi_cdblen = sizeof (cdb);
28423 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28424 	com->uscsi_buflen = mode2->cdread_buflen;
28425 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28426 
28427 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28428 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28429 	kmem_free(com, sizeof (*com));
28430 	return (rval);
28431 }
28432 
28433 
28434 /*
28435  *    Function: sr_read_mode2()
28436  *
28437  * Description: This routine is the driver entry point for handling CD-ROM
28438  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28439  *		do not support the READ CD (0xBE) command.
28440  *
28441  *   Arguments: dev	- the device 'dev_t'
28442  *		data	- pointer to user provided cd read structure specifying
28443  *			  the lba buffer address and length.
28444  *		flag	- this argument is a pass through to ddi_copyxxx()
28445  *			  directly from the mode argument of ioctl().
28446  *
28447  * Return Code: the code returned by sd_send_scsi_cmd()
28448  *		EFAULT if ddi_copyxxx() fails
28449  *		ENXIO if fail ddi_get_soft_state
28450  *		EINVAL if data pointer is NULL
28451  *		EIO if fail to reset block size
28452  *		EAGAIN if commands are in progress in the driver
28453  */
28454 
28455 static int
28456 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28457 {
28458 	struct sd_lun		*un;
28459 	struct cdrom_read	mode2_struct;
28460 	struct cdrom_read	*mode2 = &mode2_struct;
28461 	int			rval;
28462 	uint32_t		restore_blksize;
28463 	struct uscsi_cmd	*com;
28464 	uchar_t			cdb[CDB_GROUP0];
28465 	int			nblocks;
28466 
28467 #ifdef _MULTI_DATAMODEL
28468 	/* To support ILP32 applications in an LP64 world */
28469 	struct cdrom_read32	cdrom_read32;
28470 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28471 #endif /* _MULTI_DATAMODEL */
28472 
28473 	if (data == NULL) {
28474 		return (EINVAL);
28475 	}
28476 
28477 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28478 	    (un->un_state == SD_STATE_OFFLINE)) {
28479 		return (ENXIO);
28480 	}
28481 
28482 	/*
28483 	 * Because this routine will update the device and driver block size
28484 	 * being used we want to make sure there are no commands in progress.
28485 	 * If commands are in progress the user will have to try again.
28486 	 *
28487 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28488 	 * in sdioctl to protect commands from sdioctl through to the top of
28489 	 * sd_uscsi_strategy. See sdioctl for details.
28490 	 */
28491 	mutex_enter(SD_MUTEX(un));
28492 	if (un->un_ncmds_in_driver != 1) {
28493 		mutex_exit(SD_MUTEX(un));
28494 		return (EAGAIN);
28495 	}
28496 	mutex_exit(SD_MUTEX(un));
28497 
28498 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28499 	    "sd_read_mode2: entry: un:0x%p\n", un);
28500 
28501 #ifdef _MULTI_DATAMODEL
28502 	switch (ddi_model_convert_from(flag & FMODELS)) {
28503 	case DDI_MODEL_ILP32:
28504 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28505 			return (EFAULT);
28506 		}
28507 		/* Convert the ILP32 uscsi data from the application to LP64 */
28508 		cdrom_read32tocdrom_read(cdrd32, mode2);
28509 		break;
28510 	case DDI_MODEL_NONE:
28511 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28512 			return (EFAULT);
28513 		}
28514 		break;
28515 	}
28516 #else /* ! _MULTI_DATAMODEL */
28517 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28518 		return (EFAULT);
28519 	}
28520 #endif /* _MULTI_DATAMODEL */
28521 
28522 	/* Store the current target block size for restoration later */
28523 	restore_blksize = un->un_tgt_blocksize;
28524 
28525 	/* Change the device and soft state target block size to 2336 */
28526 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28527 		rval = EIO;
28528 		goto done;
28529 	}
28530 
28531 
28532 	bzero(cdb, sizeof (cdb));
28533 
28534 	/* set READ operation */
28535 	cdb[0] = SCMD_READ;
28536 
28537 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28538 	mode2->cdread_lba >>= 2;
28539 
28540 	/* set the start address */
28541 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28542 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28543 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28544 
28545 	/* set the transfer length */
28546 	nblocks = mode2->cdread_buflen / 2336;
28547 	cdb[4] = (uchar_t)nblocks & 0xFF;
28548 
28549 	/* build command */
28550 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28551 	com->uscsi_cdb = (caddr_t)cdb;
28552 	com->uscsi_cdblen = sizeof (cdb);
28553 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28554 	com->uscsi_buflen = mode2->cdread_buflen;
28555 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28556 
28557 	/*
28558 	 * Issue SCSI command with user space address for read buffer.
28559 	 *
28560 	 * This sends the command through main channel in the driver.
28561 	 *
28562 	 * Since this is accessed via an IOCTL call, we go through the
28563 	 * standard path, so that if the device was powered down, then
28564 	 * it would be 'awakened' to handle the command.
28565 	 */
28566 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28567 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28568 
28569 	kmem_free(com, sizeof (*com));
28570 
28571 	/* Restore the device and soft state target block size */
28572 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28573 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28574 		    "can't do switch back to mode 1\n");
28575 		/*
28576 		 * If sd_send_scsi_READ succeeded we still need to report
28577 		 * an error because we failed to reset the block size
28578 		 */
28579 		if (rval == 0) {
28580 			rval = EIO;
28581 		}
28582 	}
28583 
28584 done:
28585 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28586 	    "sd_read_mode2: exit: un:0x%p\n", un);
28587 
28588 	return (rval);
28589 }
28590 
28591 
28592 /*
28593  *    Function: sr_sector_mode()
28594  *
28595  * Description: This utility function is used by sr_read_mode2 to set the target
28596  *		block size based on the user specified size. This is a legacy
28597  *		implementation based upon a vendor specific mode page
28598  *
28599  *   Arguments: dev	- the device 'dev_t'
28600  *		data	- flag indicating if block size is being set to 2336 or
28601  *			  512.
28602  *
28603  * Return Code: the code returned by sd_send_scsi_cmd()
28604  *		EFAULT if ddi_copyxxx() fails
28605  *		ENXIO if fail ddi_get_soft_state
28606  *		EINVAL if data pointer is NULL
28607  */
28608 
28609 static int
28610 sr_sector_mode(dev_t dev, uint32_t blksize)
28611 {
28612 	struct sd_lun	*un;
28613 	uchar_t		*sense;
28614 	uchar_t		*select;
28615 	int		rval;
28616 
28617 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28618 	    (un->un_state == SD_STATE_OFFLINE)) {
28619 		return (ENXIO);
28620 	}
28621 
28622 	sense = kmem_zalloc(20, KM_SLEEP);
28623 
28624 	/* Note: This is a vendor specific mode page (0x81) */
28625 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81,
28626 	    SD_PATH_STANDARD)) != 0) {
28627 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28628 		    "sr_sector_mode: Mode Sense failed\n");
28629 		kmem_free(sense, 20);
28630 		return (rval);
28631 	}
28632 	select = kmem_zalloc(20, KM_SLEEP);
28633 	select[3] = 0x08;
28634 	select[10] = ((blksize >> 8) & 0xff);
28635 	select[11] = (blksize & 0xff);
28636 	select[12] = 0x01;
28637 	select[13] = 0x06;
28638 	select[14] = sense[14];
28639 	select[15] = sense[15];
28640 	if (blksize == SD_MODE2_BLKSIZE) {
28641 		select[14] |= 0x01;
28642 	}
28643 
28644 	if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20,
28645 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
28646 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28647 		    "sr_sector_mode: Mode Select failed\n");
28648 	} else {
28649 		/*
28650 		 * Only update the softstate block size if we successfully
28651 		 * changed the device block mode.
28652 		 */
28653 		mutex_enter(SD_MUTEX(un));
28654 		sd_update_block_info(un, blksize, 0);
28655 		mutex_exit(SD_MUTEX(un));
28656 	}
28657 	kmem_free(sense, 20);
28658 	kmem_free(select, 20);
28659 	return (rval);
28660 }
28661 
28662 
28663 /*
28664  *    Function: sr_read_cdda()
28665  *
28666  * Description: This routine is the driver entry point for handling CD-ROM
28667  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28668  *		the target supports CDDA these requests are handled via a vendor
28669  *		specific command (0xD8) If the target does not support CDDA
28670  *		these requests are handled via the READ CD command (0xBE).
28671  *
28672  *   Arguments: dev	- the device 'dev_t'
28673  *		data	- pointer to user provided CD-DA structure specifying
28674  *			  the track starting address, transfer length, and
28675  *			  subcode options.
28676  *		flag	- this argument is a pass through to ddi_copyxxx()
28677  *			  directly from the mode argument of ioctl().
28678  *
28679  * Return Code: the code returned by sd_send_scsi_cmd()
28680  *		EFAULT if ddi_copyxxx() fails
28681  *		ENXIO if fail ddi_get_soft_state
28682  *		EINVAL if invalid arguments are provided
28683  *		ENOTTY
28684  */
28685 
28686 static int
28687 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28688 {
28689 	struct sd_lun			*un;
28690 	struct uscsi_cmd		*com;
28691 	struct cdrom_cdda		*cdda;
28692 	int				rval;
28693 	size_t				buflen;
28694 	char				cdb[CDB_GROUP5];
28695 
28696 #ifdef _MULTI_DATAMODEL
28697 	/* To support ILP32 applications in an LP64 world */
28698 	struct cdrom_cdda32	cdrom_cdda32;
28699 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28700 #endif /* _MULTI_DATAMODEL */
28701 
28702 	if (data == NULL) {
28703 		return (EINVAL);
28704 	}
28705 
28706 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28707 		return (ENXIO);
28708 	}
28709 
28710 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28711 
28712 #ifdef _MULTI_DATAMODEL
28713 	switch (ddi_model_convert_from(flag & FMODELS)) {
28714 	case DDI_MODEL_ILP32:
28715 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28716 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28717 			    "sr_read_cdda: ddi_copyin Failed\n");
28718 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28719 			return (EFAULT);
28720 		}
28721 		/* Convert the ILP32 uscsi data from the application to LP64 */
28722 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28723 		break;
28724 	case DDI_MODEL_NONE:
28725 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28726 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28727 			    "sr_read_cdda: ddi_copyin Failed\n");
28728 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28729 			return (EFAULT);
28730 		}
28731 		break;
28732 	}
28733 #else /* ! _MULTI_DATAMODEL */
28734 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28735 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28736 		    "sr_read_cdda: ddi_copyin Failed\n");
28737 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28738 		return (EFAULT);
28739 	}
28740 #endif /* _MULTI_DATAMODEL */
28741 
28742 	/*
28743 	 * Since MMC-2 expects max 3 bytes for length, check if the
28744 	 * length input is greater than 3 bytes
28745 	 */
28746 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28747 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28748 		    "cdrom transfer length too large: %d (limit %d)\n",
28749 		    cdda->cdda_length, 0xFFFFFF);
28750 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28751 		return (EINVAL);
28752 	}
28753 
28754 	switch (cdda->cdda_subcode) {
28755 	case CDROM_DA_NO_SUBCODE:
28756 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28757 		break;
28758 	case CDROM_DA_SUBQ:
28759 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28760 		break;
28761 	case CDROM_DA_ALL_SUBCODE:
28762 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28763 		break;
28764 	case CDROM_DA_SUBCODE_ONLY:
28765 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28766 		break;
28767 	default:
28768 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28769 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28770 		    cdda->cdda_subcode);
28771 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28772 		return (EINVAL);
28773 	}
28774 
28775 	/* Build and send the command */
28776 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28777 	bzero(cdb, CDB_GROUP5);
28778 
28779 	if (un->un_f_cfg_cdda == TRUE) {
28780 		cdb[0] = (char)SCMD_READ_CD;
28781 		cdb[1] = 0x04;
28782 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28783 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28784 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28785 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28786 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28787 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28788 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28789 		cdb[9] = 0x10;
28790 		switch (cdda->cdda_subcode) {
28791 		case CDROM_DA_NO_SUBCODE :
28792 			cdb[10] = 0x0;
28793 			break;
28794 		case CDROM_DA_SUBQ :
28795 			cdb[10] = 0x2;
28796 			break;
28797 		case CDROM_DA_ALL_SUBCODE :
28798 			cdb[10] = 0x1;
28799 			break;
28800 		case CDROM_DA_SUBCODE_ONLY :
28801 			/* FALLTHROUGH */
28802 		default :
28803 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28804 			kmem_free(com, sizeof (*com));
28805 			return (ENOTTY);
28806 		}
28807 	} else {
28808 		cdb[0] = (char)SCMD_READ_CDDA;
28809 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28810 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28811 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28812 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28813 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28814 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28815 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28816 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28817 		cdb[10] = cdda->cdda_subcode;
28818 	}
28819 
28820 	com->uscsi_cdb = cdb;
28821 	com->uscsi_cdblen = CDB_GROUP5;
28822 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28823 	com->uscsi_buflen = buflen;
28824 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28825 
28826 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28827 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28828 
28829 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28830 	kmem_free(com, sizeof (*com));
28831 	return (rval);
28832 }
28833 
28834 
28835 /*
28836  *    Function: sr_read_cdxa()
28837  *
28838  * Description: This routine is the driver entry point for handling CD-ROM
28839  *		ioctl requests to return CD-XA (Extended Architecture) data.
28840  *		(CDROMCDXA).
28841  *
28842  *   Arguments: dev	- the device 'dev_t'
28843  *		data	- pointer to user provided CD-XA structure specifying
28844  *			  the data starting address, transfer length, and format
28845  *		flag	- this argument is a pass through to ddi_copyxxx()
28846  *			  directly from the mode argument of ioctl().
28847  *
28848  * Return Code: the code returned by sd_send_scsi_cmd()
28849  *		EFAULT if ddi_copyxxx() fails
28850  *		ENXIO if fail ddi_get_soft_state
28851  *		EINVAL if data pointer is NULL
28852  */
28853 
28854 static int
28855 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28856 {
28857 	struct sd_lun		*un;
28858 	struct uscsi_cmd	*com;
28859 	struct cdrom_cdxa	*cdxa;
28860 	int			rval;
28861 	size_t			buflen;
28862 	char			cdb[CDB_GROUP5];
28863 	uchar_t			read_flags;
28864 
28865 #ifdef _MULTI_DATAMODEL
28866 	/* To support ILP32 applications in an LP64 world */
28867 	struct cdrom_cdxa32		cdrom_cdxa32;
28868 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28869 #endif /* _MULTI_DATAMODEL */
28870 
28871 	if (data == NULL) {
28872 		return (EINVAL);
28873 	}
28874 
28875 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28876 		return (ENXIO);
28877 	}
28878 
28879 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28880 
28881 #ifdef _MULTI_DATAMODEL
28882 	switch (ddi_model_convert_from(flag & FMODELS)) {
28883 	case DDI_MODEL_ILP32:
28884 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28885 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28886 			return (EFAULT);
28887 		}
28888 		/*
28889 		 * Convert the ILP32 uscsi data from the
28890 		 * application to LP64 for internal use.
28891 		 */
28892 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28893 		break;
28894 	case DDI_MODEL_NONE:
28895 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28896 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28897 			return (EFAULT);
28898 		}
28899 		break;
28900 	}
28901 #else /* ! _MULTI_DATAMODEL */
28902 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28903 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28904 		return (EFAULT);
28905 	}
28906 #endif /* _MULTI_DATAMODEL */
28907 
28908 	/*
28909 	 * Since MMC-2 expects max 3 bytes for length, check if the
28910 	 * length input is greater than 3 bytes
28911 	 */
28912 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28913 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28914 		    "cdrom transfer length too large: %d (limit %d)\n",
28915 		    cdxa->cdxa_length, 0xFFFFFF);
28916 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28917 		return (EINVAL);
28918 	}
28919 
28920 	switch (cdxa->cdxa_format) {
28921 	case CDROM_XA_DATA:
28922 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28923 		read_flags = 0x10;
28924 		break;
28925 	case CDROM_XA_SECTOR_DATA:
28926 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28927 		read_flags = 0xf8;
28928 		break;
28929 	case CDROM_XA_DATA_W_ERROR:
28930 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28931 		read_flags = 0xfc;
28932 		break;
28933 	default:
28934 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28935 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
28936 		    cdxa->cdxa_format);
28937 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28938 		return (EINVAL);
28939 	}
28940 
28941 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28942 	bzero(cdb, CDB_GROUP5);
28943 	if (un->un_f_mmc_cap == TRUE) {
28944 		cdb[0] = (char)SCMD_READ_CD;
28945 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28946 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28947 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28948 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28949 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28950 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28951 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28952 		cdb[9] = (char)read_flags;
28953 	} else {
28954 		/*
28955 		 * Note: A vendor specific command (0xDB) is being used her to
28956 		 * request a read of all subcodes.
28957 		 */
28958 		cdb[0] = (char)SCMD_READ_CDXA;
28959 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28960 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28961 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28962 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28963 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28964 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28965 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28966 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28967 		cdb[10] = cdxa->cdxa_format;
28968 	}
28969 	com->uscsi_cdb	   = cdb;
28970 	com->uscsi_cdblen  = CDB_GROUP5;
28971 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28972 	com->uscsi_buflen  = buflen;
28973 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28974 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28975 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28976 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28977 	kmem_free(com, sizeof (*com));
28978 	return (rval);
28979 }
28980 
28981 
28982 /*
28983  *    Function: sr_eject()
28984  *
28985  * Description: This routine is the driver entry point for handling CD-ROM
28986  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28987  *
28988  *   Arguments: dev	- the device 'dev_t'
28989  *
28990  * Return Code: the code returned by sd_send_scsi_cmd()
28991  */
28992 
28993 static int
28994 sr_eject(dev_t dev)
28995 {
28996 	struct sd_lun	*un;
28997 	int		rval;
28998 
28999 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29000 	    (un->un_state == SD_STATE_OFFLINE)) {
29001 		return (ENXIO);
29002 	}
29003 	if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
29004 	    SD_PATH_STANDARD)) != 0) {
29005 		return (rval);
29006 	}
29007 
29008 	rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT,
29009 	    SD_PATH_STANDARD);
29010 
29011 	if (rval == 0) {
29012 		mutex_enter(SD_MUTEX(un));
29013 		sr_ejected(un);
29014 		un->un_mediastate = DKIO_EJECTED;
29015 		cv_broadcast(&un->un_state_cv);
29016 		mutex_exit(SD_MUTEX(un));
29017 	}
29018 	return (rval);
29019 }
29020 
29021 
29022 /*
29023  *    Function: sr_ejected()
29024  *
29025  * Description: This routine updates the soft state structure to invalidate the
29026  *		geometry information after the media has been ejected or a
29027  *		media eject has been detected.
29028  *
29029  *   Arguments: un - driver soft state (unit) structure
29030  */
29031 
29032 static void
29033 sr_ejected(struct sd_lun *un)
29034 {
29035 	struct sd_errstats *stp;
29036 
29037 	ASSERT(un != NULL);
29038 	ASSERT(mutex_owned(SD_MUTEX(un)));
29039 
29040 	un->un_f_blockcount_is_valid	= FALSE;
29041 	un->un_f_tgt_blocksize_is_valid	= FALSE;
29042 	un->un_f_geometry_is_valid	= FALSE;
29043 
29044 	if (un->un_errstats != NULL) {
29045 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
29046 		stp->sd_capacity.value.ui64 = 0;
29047 	}
29048 }
29049 
29050 
29051 /*
29052  *    Function: sr_check_wp()
29053  *
29054  * Description: This routine checks the write protection of a removable
29055  *      media disk and hotpluggable devices via the write protect bit of
29056  *      the Mode Page Header device specific field. Some devices choke
29057  *      on unsupported mode page. In order to workaround this issue,
29058  *      this routine has been implemented to use 0x3f mode page(request
29059  *      for all pages) for all device types.
29060  *
29061  *   Arguments: dev		- the device 'dev_t'
29062  *
29063  * Return Code: int indicating if the device is write protected (1) or not (0)
29064  *
29065  *     Context: Kernel thread.
29066  *
29067  */
29068 
29069 static int
29070 sr_check_wp(dev_t dev)
29071 {
29072 	struct sd_lun	*un;
29073 	uchar_t		device_specific;
29074 	uchar_t		*sense;
29075 	int		hdrlen;
29076 	int		rval = FALSE;
29077 
29078 	/*
29079 	 * Note: The return codes for this routine should be reworked to
29080 	 * properly handle the case of a NULL softstate.
29081 	 */
29082 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
29083 		return (FALSE);
29084 	}
29085 
29086 	if (un->un_f_cfg_is_atapi == TRUE) {
29087 		/*
29088 		 * The mode page contents are not required; set the allocation
29089 		 * length for the mode page header only
29090 		 */
29091 		hdrlen = MODE_HEADER_LENGTH_GRP2;
29092 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29093 		if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen,
29094 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0)
29095 			goto err_exit;
29096 		device_specific =
29097 		    ((struct mode_header_grp2 *)sense)->device_specific;
29098 	} else {
29099 		hdrlen = MODE_HEADER_LENGTH;
29100 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29101 		if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen,
29102 		    MODEPAGE_ALLPAGES, SD_PATH_STANDARD) != 0)
29103 			goto err_exit;
29104 		device_specific =
29105 		    ((struct mode_header *)sense)->device_specific;
29106 	}
29107 
29108 	/*
29109 	 * Write protect mode sense failed; not all disks
29110 	 * understand this query. Return FALSE assuming that
29111 	 * these devices are not writable.
29112 	 */
29113 	if (device_specific & WRITE_PROTECT) {
29114 		rval = TRUE;
29115 	}
29116 
29117 err_exit:
29118 	kmem_free(sense, hdrlen);
29119 	return (rval);
29120 }
29121 
29122 /*
29123  *    Function: sr_volume_ctrl()
29124  *
29125  * Description: This routine is the driver entry point for handling CD-ROM
29126  *		audio output volume ioctl requests. (CDROMVOLCTRL)
29127  *
29128  *   Arguments: dev	- the device 'dev_t'
29129  *		data	- pointer to user audio volume control structure
29130  *		flag	- this argument is a pass through to ddi_copyxxx()
29131  *			  directly from the mode argument of ioctl().
29132  *
29133  * Return Code: the code returned by sd_send_scsi_cmd()
29134  *		EFAULT if ddi_copyxxx() fails
29135  *		ENXIO if fail ddi_get_soft_state
29136  *		EINVAL if data pointer is NULL
29137  *
29138  */
29139 
29140 static int
29141 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
29142 {
29143 	struct sd_lun		*un;
29144 	struct cdrom_volctrl    volume;
29145 	struct cdrom_volctrl    *vol = &volume;
29146 	uchar_t			*sense_page;
29147 	uchar_t			*select_page;
29148 	uchar_t			*sense;
29149 	uchar_t			*select;
29150 	int			sense_buflen;
29151 	int			select_buflen;
29152 	int			rval;
29153 
29154 	if (data == NULL) {
29155 		return (EINVAL);
29156 	}
29157 
29158 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29159 	    (un->un_state == SD_STATE_OFFLINE)) {
29160 		return (ENXIO);
29161 	}
29162 
29163 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29164 		return (EFAULT);
29165 	}
29166 
29167 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29168 		struct mode_header_grp2		*sense_mhp;
29169 		struct mode_header_grp2		*select_mhp;
29170 		int				bd_len;
29171 
29172 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29173 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
29174 		    MODEPAGE_AUDIO_CTRL_LEN;
29175 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29176 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29177 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
29178 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29179 		    SD_PATH_STANDARD)) != 0) {
29180 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29181 			    "sr_volume_ctrl: Mode Sense Failed\n");
29182 			kmem_free(sense, sense_buflen);
29183 			kmem_free(select, select_buflen);
29184 			return (rval);
29185 		}
29186 		sense_mhp = (struct mode_header_grp2 *)sense;
29187 		select_mhp = (struct mode_header_grp2 *)select;
29188 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
29189 		    sense_mhp->bdesc_length_lo;
29190 		if (bd_len > MODE_BLK_DESC_LENGTH) {
29191 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29192 			    "sr_volume_ctrl: Mode Sense returned invalid "
29193 			    "block descriptor length\n");
29194 			kmem_free(sense, sense_buflen);
29195 			kmem_free(select, select_buflen);
29196 			return (EIO);
29197 		}
29198 		sense_page = (uchar_t *)
29199 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29200 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29201 		select_mhp->length_msb = 0;
29202 		select_mhp->length_lsb = 0;
29203 		select_mhp->bdesc_length_hi = 0;
29204 		select_mhp->bdesc_length_lo = 0;
29205 	} else {
29206 		struct mode_header		*sense_mhp, *select_mhp;
29207 
29208 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29209 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29210 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29211 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29212 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
29213 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29214 		    SD_PATH_STANDARD)) != 0) {
29215 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29216 			    "sr_volume_ctrl: Mode Sense Failed\n");
29217 			kmem_free(sense, sense_buflen);
29218 			kmem_free(select, select_buflen);
29219 			return (rval);
29220 		}
29221 		sense_mhp  = (struct mode_header *)sense;
29222 		select_mhp = (struct mode_header *)select;
29223 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29224 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29225 			    "sr_volume_ctrl: Mode Sense returned invalid "
29226 			    "block descriptor length\n");
29227 			kmem_free(sense, sense_buflen);
29228 			kmem_free(select, select_buflen);
29229 			return (EIO);
29230 		}
29231 		sense_page = (uchar_t *)
29232 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29233 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29234 		select_mhp->length = 0;
29235 		select_mhp->bdesc_length = 0;
29236 	}
29237 	/*
29238 	 * Note: An audio control data structure could be created and overlayed
29239 	 * on the following in place of the array indexing method implemented.
29240 	 */
29241 
29242 	/* Build the select data for the user volume data */
29243 	select_page[0] = MODEPAGE_AUDIO_CTRL;
29244 	select_page[1] = 0xE;
29245 	/* Set the immediate bit */
29246 	select_page[2] = 0x04;
29247 	/* Zero out reserved fields */
29248 	select_page[3] = 0x00;
29249 	select_page[4] = 0x00;
29250 	/* Return sense data for fields not to be modified */
29251 	select_page[5] = sense_page[5];
29252 	select_page[6] = sense_page[6];
29253 	select_page[7] = sense_page[7];
29254 	/* Set the user specified volume levels for channel 0 and 1 */
29255 	select_page[8] = 0x01;
29256 	select_page[9] = vol->channel0;
29257 	select_page[10] = 0x02;
29258 	select_page[11] = vol->channel1;
29259 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29260 	select_page[12] = sense_page[12];
29261 	select_page[13] = sense_page[13];
29262 	select_page[14] = sense_page[14];
29263 	select_page[15] = sense_page[15];
29264 
29265 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29266 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select,
29267 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29268 	} else {
29269 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
29270 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29271 	}
29272 
29273 	kmem_free(sense, sense_buflen);
29274 	kmem_free(select, select_buflen);
29275 	return (rval);
29276 }
29277 
29278 
29279 /*
29280  *    Function: sr_read_sony_session_offset()
29281  *
29282  * Description: This routine is the driver entry point for handling CD-ROM
29283  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29284  *		The address of the first track in the last session of a
29285  *		multi-session CD-ROM is returned
29286  *
29287  *		Note: This routine uses a vendor specific key value in the
29288  *		command control field without implementing any vendor check here
29289  *		or in the ioctl routine.
29290  *
29291  *   Arguments: dev	- the device 'dev_t'
29292  *		data	- pointer to an int to hold the requested address
29293  *		flag	- this argument is a pass through to ddi_copyxxx()
29294  *			  directly from the mode argument of ioctl().
29295  *
29296  * Return Code: the code returned by sd_send_scsi_cmd()
29297  *		EFAULT if ddi_copyxxx() fails
29298  *		ENXIO if fail ddi_get_soft_state
29299  *		EINVAL if data pointer is NULL
29300  */
29301 
29302 static int
29303 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29304 {
29305 	struct sd_lun		*un;
29306 	struct uscsi_cmd	*com;
29307 	caddr_t			buffer;
29308 	char			cdb[CDB_GROUP1];
29309 	int			session_offset = 0;
29310 	int			rval;
29311 
29312 	if (data == NULL) {
29313 		return (EINVAL);
29314 	}
29315 
29316 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29317 	    (un->un_state == SD_STATE_OFFLINE)) {
29318 		return (ENXIO);
29319 	}
29320 
29321 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29322 	bzero(cdb, CDB_GROUP1);
29323 	cdb[0] = SCMD_READ_TOC;
29324 	/*
29325 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29326 	 * (4 byte TOC response header + 8 byte response data)
29327 	 */
29328 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29329 	/* Byte 9 is the control byte. A vendor specific value is used */
29330 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29331 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29332 	com->uscsi_cdb = cdb;
29333 	com->uscsi_cdblen = CDB_GROUP1;
29334 	com->uscsi_bufaddr = buffer;
29335 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29336 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29337 
29338 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
29339 	    UIO_SYSSPACE, SD_PATH_STANDARD);
29340 	if (rval != 0) {
29341 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29342 		kmem_free(com, sizeof (*com));
29343 		return (rval);
29344 	}
29345 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29346 		session_offset =
29347 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29348 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29349 		/*
29350 		 * Offset returned offset in current lbasize block's. Convert to
29351 		 * 2k block's to return to the user
29352 		 */
29353 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29354 			session_offset >>= 2;
29355 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29356 			session_offset >>= 1;
29357 		}
29358 	}
29359 
29360 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29361 		rval = EFAULT;
29362 	}
29363 
29364 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29365 	kmem_free(com, sizeof (*com));
29366 	return (rval);
29367 }
29368 
29369 
29370 /*
29371  *    Function: sd_wm_cache_constructor()
29372  *
29373  * Description: Cache Constructor for the wmap cache for the read/modify/write
29374  * 		devices.
29375  *
29376  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29377  *		un	- sd_lun structure for the device.
29378  *		flag	- the km flags passed to constructor
29379  *
29380  * Return Code: 0 on success.
29381  *		-1 on failure.
29382  */
29383 
29384 /*ARGSUSED*/
29385 static int
29386 sd_wm_cache_constructor(void *wm, void *un, int flags)
29387 {
29388 	bzero(wm, sizeof (struct sd_w_map));
29389 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29390 	return (0);
29391 }
29392 
29393 
29394 /*
29395  *    Function: sd_wm_cache_destructor()
29396  *
29397  * Description: Cache destructor for the wmap cache for the read/modify/write
29398  * 		devices.
29399  *
29400  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29401  *		un	- sd_lun structure for the device.
29402  */
29403 /*ARGSUSED*/
29404 static void
29405 sd_wm_cache_destructor(void *wm, void *un)
29406 {
29407 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29408 }
29409 
29410 
29411 /*
29412  *    Function: sd_range_lock()
29413  *
29414  * Description: Lock the range of blocks specified as parameter to ensure
29415  *		that read, modify write is atomic and no other i/o writes
29416  *		to the same location. The range is specified in terms
29417  *		of start and end blocks. Block numbers are the actual
29418  *		media block numbers and not system.
29419  *
29420  *   Arguments: un	- sd_lun structure for the device.
29421  *		startb - The starting block number
29422  *		endb - The end block number
29423  *		typ - type of i/o - simple/read_modify_write
29424  *
29425  * Return Code: wm  - pointer to the wmap structure.
29426  *
29427  *     Context: This routine can sleep.
29428  */
29429 
29430 static struct sd_w_map *
29431 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29432 {
29433 	struct sd_w_map *wmp = NULL;
29434 	struct sd_w_map *sl_wmp = NULL;
29435 	struct sd_w_map *tmp_wmp;
29436 	wm_state state = SD_WM_CHK_LIST;
29437 
29438 
29439 	ASSERT(un != NULL);
29440 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29441 
29442 	mutex_enter(SD_MUTEX(un));
29443 
29444 	while (state != SD_WM_DONE) {
29445 
29446 		switch (state) {
29447 		case SD_WM_CHK_LIST:
29448 			/*
29449 			 * This is the starting state. Check the wmap list
29450 			 * to see if the range is currently available.
29451 			 */
29452 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29453 				/*
29454 				 * If this is a simple write and no rmw
29455 				 * i/o is pending then try to lock the
29456 				 * range as the range should be available.
29457 				 */
29458 				state = SD_WM_LOCK_RANGE;
29459 			} else {
29460 				tmp_wmp = sd_get_range(un, startb, endb);
29461 				if (tmp_wmp != NULL) {
29462 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29463 						/*
29464 						 * Should not keep onlist wmps
29465 						 * while waiting this macro
29466 						 * will also do wmp = NULL;
29467 						 */
29468 						FREE_ONLIST_WMAP(un, wmp);
29469 					}
29470 					/*
29471 					 * sl_wmp is the wmap on which wait
29472 					 * is done, since the tmp_wmp points
29473 					 * to the inuse wmap, set sl_wmp to
29474 					 * tmp_wmp and change the state to sleep
29475 					 */
29476 					sl_wmp = tmp_wmp;
29477 					state = SD_WM_WAIT_MAP;
29478 				} else {
29479 					state = SD_WM_LOCK_RANGE;
29480 				}
29481 
29482 			}
29483 			break;
29484 
29485 		case SD_WM_LOCK_RANGE:
29486 			ASSERT(un->un_wm_cache);
29487 			/*
29488 			 * The range need to be locked, try to get a wmap.
29489 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29490 			 * if possible as we will have to release the sd mutex
29491 			 * if we have to sleep.
29492 			 */
29493 			if (wmp == NULL)
29494 				wmp = kmem_cache_alloc(un->un_wm_cache,
29495 				    KM_NOSLEEP);
29496 			if (wmp == NULL) {
29497 				mutex_exit(SD_MUTEX(un));
29498 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29499 				    (sd_lun::un_wm_cache))
29500 				wmp = kmem_cache_alloc(un->un_wm_cache,
29501 				    KM_SLEEP);
29502 				mutex_enter(SD_MUTEX(un));
29503 				/*
29504 				 * we released the mutex so recheck and go to
29505 				 * check list state.
29506 				 */
29507 				state = SD_WM_CHK_LIST;
29508 			} else {
29509 				/*
29510 				 * We exit out of state machine since we
29511 				 * have the wmap. Do the housekeeping first.
29512 				 * place the wmap on the wmap list if it is not
29513 				 * on it already and then set the state to done.
29514 				 */
29515 				wmp->wm_start = startb;
29516 				wmp->wm_end = endb;
29517 				wmp->wm_flags = typ | SD_WM_BUSY;
29518 				if (typ & SD_WTYPE_RMW) {
29519 					un->un_rmw_count++;
29520 				}
29521 				/*
29522 				 * If not already on the list then link
29523 				 */
29524 				if (!ONLIST(un, wmp)) {
29525 					wmp->wm_next = un->un_wm;
29526 					wmp->wm_prev = NULL;
29527 					if (wmp->wm_next)
29528 						wmp->wm_next->wm_prev = wmp;
29529 					un->un_wm = wmp;
29530 				}
29531 				state = SD_WM_DONE;
29532 			}
29533 			break;
29534 
29535 		case SD_WM_WAIT_MAP:
29536 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29537 			/*
29538 			 * Wait is done on sl_wmp, which is set in the
29539 			 * check_list state.
29540 			 */
29541 			sl_wmp->wm_wanted_count++;
29542 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29543 			sl_wmp->wm_wanted_count--;
29544 			/*
29545 			 * We can reuse the memory from the completed sl_wmp
29546 			 * lock range for our new lock, but only if noone is
29547 			 * waiting for it.
29548 			 */
29549 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29550 			if (sl_wmp->wm_wanted_count == 0) {
29551 				if (wmp != NULL)
29552 					CHK_N_FREEWMP(un, wmp);
29553 				wmp = sl_wmp;
29554 			}
29555 			sl_wmp = NULL;
29556 			/*
29557 			 * After waking up, need to recheck for availability of
29558 			 * range.
29559 			 */
29560 			state = SD_WM_CHK_LIST;
29561 			break;
29562 
29563 		default:
29564 			panic("sd_range_lock: "
29565 			    "Unknown state %d in sd_range_lock", state);
29566 			/*NOTREACHED*/
29567 		} /* switch(state) */
29568 
29569 	} /* while(state != SD_WM_DONE) */
29570 
29571 	mutex_exit(SD_MUTEX(un));
29572 
29573 	ASSERT(wmp != NULL);
29574 
29575 	return (wmp);
29576 }
29577 
29578 
29579 /*
29580  *    Function: sd_get_range()
29581  *
29582  * Description: Find if there any overlapping I/O to this one
29583  *		Returns the write-map of 1st such I/O, NULL otherwise.
29584  *
29585  *   Arguments: un	- sd_lun structure for the device.
29586  *		startb - The starting block number
29587  *		endb - The end block number
29588  *
29589  * Return Code: wm  - pointer to the wmap structure.
29590  */
29591 
29592 static struct sd_w_map *
29593 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29594 {
29595 	struct sd_w_map *wmp;
29596 
29597 	ASSERT(un != NULL);
29598 
29599 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29600 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29601 			continue;
29602 		}
29603 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29604 			break;
29605 		}
29606 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29607 			break;
29608 		}
29609 	}
29610 
29611 	return (wmp);
29612 }
29613 
29614 
29615 /*
29616  *    Function: sd_free_inlist_wmap()
29617  *
29618  * Description: Unlink and free a write map struct.
29619  *
29620  *   Arguments: un      - sd_lun structure for the device.
29621  *		wmp	- sd_w_map which needs to be unlinked.
29622  */
29623 
29624 static void
29625 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29626 {
29627 	ASSERT(un != NULL);
29628 
29629 	if (un->un_wm == wmp) {
29630 		un->un_wm = wmp->wm_next;
29631 	} else {
29632 		wmp->wm_prev->wm_next = wmp->wm_next;
29633 	}
29634 
29635 	if (wmp->wm_next) {
29636 		wmp->wm_next->wm_prev = wmp->wm_prev;
29637 	}
29638 
29639 	wmp->wm_next = wmp->wm_prev = NULL;
29640 
29641 	kmem_cache_free(un->un_wm_cache, wmp);
29642 }
29643 
29644 
29645 /*
29646  *    Function: sd_range_unlock()
29647  *
29648  * Description: Unlock the range locked by wm.
29649  *		Free write map if nobody else is waiting on it.
29650  *
29651  *   Arguments: un      - sd_lun structure for the device.
29652  *              wmp     - sd_w_map which needs to be unlinked.
29653  */
29654 
29655 static void
29656 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29657 {
29658 	ASSERT(un != NULL);
29659 	ASSERT(wm != NULL);
29660 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29661 
29662 	mutex_enter(SD_MUTEX(un));
29663 
29664 	if (wm->wm_flags & SD_WTYPE_RMW) {
29665 		un->un_rmw_count--;
29666 	}
29667 
29668 	if (wm->wm_wanted_count) {
29669 		wm->wm_flags = 0;
29670 		/*
29671 		 * Broadcast that the wmap is available now.
29672 		 */
29673 		cv_broadcast(&wm->wm_avail);
29674 	} else {
29675 		/*
29676 		 * If no one is waiting on the map, it should be free'ed.
29677 		 */
29678 		sd_free_inlist_wmap(un, wm);
29679 	}
29680 
29681 	mutex_exit(SD_MUTEX(un));
29682 }
29683 
29684 
29685 /*
29686  *    Function: sd_read_modify_write_task
29687  *
29688  * Description: Called from a taskq thread to initiate the write phase of
29689  *		a read-modify-write request.  This is used for targets where
29690  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29691  *
29692  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29693  *
29694  *     Context: Called under taskq thread context.
29695  */
29696 
29697 static void
29698 sd_read_modify_write_task(void *arg)
29699 {
29700 	struct sd_mapblocksize_info	*bsp;
29701 	struct buf	*bp;
29702 	struct sd_xbuf	*xp;
29703 	struct sd_lun	*un;
29704 
29705 	bp = arg;	/* The bp is given in arg */
29706 	ASSERT(bp != NULL);
29707 
29708 	/* Get the pointer to the layer-private data struct */
29709 	xp = SD_GET_XBUF(bp);
29710 	ASSERT(xp != NULL);
29711 	bsp = xp->xb_private;
29712 	ASSERT(bsp != NULL);
29713 
29714 	un = SD_GET_UN(bp);
29715 	ASSERT(un != NULL);
29716 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29717 
29718 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29719 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29720 
29721 	/*
29722 	 * This is the write phase of a read-modify-write request, called
29723 	 * under the context of a taskq thread in response to the completion
29724 	 * of the read portion of the rmw request completing under interrupt
29725 	 * context. The write request must be sent from here down the iostart
29726 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29727 	 * we use the layer index saved in the layer-private data area.
29728 	 */
29729 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29730 
29731 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29732 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29733 }
29734 
29735 
29736 /*
29737  *    Function: sddump_do_read_of_rmw()
29738  *
29739  * Description: This routine will be called from sddump, If sddump is called
29740  *		with an I/O which not aligned on device blocksize boundary
29741  *		then the write has to be converted to read-modify-write.
29742  *		Do the read part here in order to keep sddump simple.
29743  *		Note - That the sd_mutex is held across the call to this
29744  *		routine.
29745  *
29746  *   Arguments: un	- sd_lun
29747  *		blkno	- block number in terms of media block size.
29748  *		nblk	- number of blocks.
29749  *		bpp	- pointer to pointer to the buf structure. On return
29750  *			from this function, *bpp points to the valid buffer
29751  *			to which the write has to be done.
29752  *
29753  * Return Code: 0 for success or errno-type return code
29754  */
29755 
29756 static int
29757 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29758 	struct buf **bpp)
29759 {
29760 	int err;
29761 	int i;
29762 	int rval;
29763 	struct buf *bp;
29764 	struct scsi_pkt *pkt = NULL;
29765 	uint32_t target_blocksize;
29766 
29767 	ASSERT(un != NULL);
29768 	ASSERT(mutex_owned(SD_MUTEX(un)));
29769 
29770 	target_blocksize = un->un_tgt_blocksize;
29771 
29772 	mutex_exit(SD_MUTEX(un));
29773 
29774 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29775 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29776 	if (bp == NULL) {
29777 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29778 		    "no resources for dumping; giving up");
29779 		err = ENOMEM;
29780 		goto done;
29781 	}
29782 
29783 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29784 	    blkno, nblk);
29785 	if (rval != 0) {
29786 		scsi_free_consistent_buf(bp);
29787 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29788 		    "no resources for dumping; giving up");
29789 		err = ENOMEM;
29790 		goto done;
29791 	}
29792 
29793 	pkt->pkt_flags |= FLAG_NOINTR;
29794 
29795 	err = EIO;
29796 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29797 
29798 		/*
29799 		 * Scsi_poll returns 0 (success) if the command completes and
29800 		 * the status block is STATUS_GOOD.  We should only check
29801 		 * errors if this condition is not true.  Even then we should
29802 		 * send our own request sense packet only if we have a check
29803 		 * condition and auto request sense has not been performed by
29804 		 * the hba.
29805 		 */
29806 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29807 
29808 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29809 			err = 0;
29810 			break;
29811 		}
29812 
29813 		/*
29814 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29815 		 * no need to read RQS data.
29816 		 */
29817 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29818 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29819 			    "Device is gone\n");
29820 			break;
29821 		}
29822 
29823 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29824 			SD_INFO(SD_LOG_DUMP, un,
29825 			    "sddump: read failed with CHECK, try # %d\n", i);
29826 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29827 				(void) sd_send_polled_RQS(un);
29828 			}
29829 
29830 			continue;
29831 		}
29832 
29833 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29834 			int reset_retval = 0;
29835 
29836 			SD_INFO(SD_LOG_DUMP, un,
29837 			    "sddump: read failed with BUSY, try # %d\n", i);
29838 
29839 			if (un->un_f_lun_reset_enabled == TRUE) {
29840 				reset_retval = scsi_reset(SD_ADDRESS(un),
29841 				    RESET_LUN);
29842 			}
29843 			if (reset_retval == 0) {
29844 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29845 			}
29846 			(void) sd_send_polled_RQS(un);
29847 
29848 		} else {
29849 			SD_INFO(SD_LOG_DUMP, un,
29850 			    "sddump: read failed with 0x%x, try # %d\n",
29851 			    SD_GET_PKT_STATUS(pkt), i);
29852 			mutex_enter(SD_MUTEX(un));
29853 			sd_reset_target(un, pkt);
29854 			mutex_exit(SD_MUTEX(un));
29855 		}
29856 
29857 		/*
29858 		 * If we are not getting anywhere with lun/target resets,
29859 		 * let's reset the bus.
29860 		 */
29861 		if (i > SD_NDUMP_RETRIES/2) {
29862 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29863 			(void) sd_send_polled_RQS(un);
29864 		}
29865 
29866 	}
29867 	scsi_destroy_pkt(pkt);
29868 
29869 	if (err != 0) {
29870 		scsi_free_consistent_buf(bp);
29871 		*bpp = NULL;
29872 	} else {
29873 		*bpp = bp;
29874 	}
29875 
29876 done:
29877 	mutex_enter(SD_MUTEX(un));
29878 	return (err);
29879 }
29880 
29881 
29882 /*
29883  *    Function: sd_failfast_flushq
29884  *
29885  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29886  *		in b_flags and move them onto the failfast queue, then kick
29887  *		off a thread to return all bp's on the failfast queue to
29888  *		their owners with an error set.
29889  *
29890  *   Arguments: un - pointer to the soft state struct for the instance.
29891  *
29892  *     Context: may execute in interrupt context.
29893  */
29894 
29895 static void
29896 sd_failfast_flushq(struct sd_lun *un)
29897 {
29898 	struct buf *bp;
29899 	struct buf *next_waitq_bp;
29900 	struct buf *prev_waitq_bp = NULL;
29901 
29902 	ASSERT(un != NULL);
29903 	ASSERT(mutex_owned(SD_MUTEX(un)));
29904 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29905 	ASSERT(un->un_failfast_bp == NULL);
29906 
29907 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29908 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
29909 
29910 	/*
29911 	 * Check if we should flush all bufs when entering failfast state, or
29912 	 * just those with B_FAILFAST set.
29913 	 */
29914 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29915 		/*
29916 		 * Move *all* bp's on the wait queue to the failfast flush
29917 		 * queue, including those that do NOT have B_FAILFAST set.
29918 		 */
29919 		if (un->un_failfast_headp == NULL) {
29920 			ASSERT(un->un_failfast_tailp == NULL);
29921 			un->un_failfast_headp = un->un_waitq_headp;
29922 		} else {
29923 			ASSERT(un->un_failfast_tailp != NULL);
29924 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29925 		}
29926 
29927 		un->un_failfast_tailp = un->un_waitq_tailp;
29928 
29929 		/* update kstat for each bp moved out of the waitq */
29930 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29931 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29932 		}
29933 
29934 		/* empty the waitq */
29935 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
29936 
29937 	} else {
29938 		/*
29939 		 * Go thru the wait queue, pick off all entries with
29940 		 * B_FAILFAST set, and move these onto the failfast queue.
29941 		 */
29942 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29943 			/*
29944 			 * Save the pointer to the next bp on the wait queue,
29945 			 * so we get to it on the next iteration of this loop.
29946 			 */
29947 			next_waitq_bp = bp->av_forw;
29948 
29949 			/*
29950 			 * If this bp from the wait queue does NOT have
29951 			 * B_FAILFAST set, just move on to the next element
29952 			 * in the wait queue. Note, this is the only place
29953 			 * where it is correct to set prev_waitq_bp.
29954 			 */
29955 			if ((bp->b_flags & B_FAILFAST) == 0) {
29956 				prev_waitq_bp = bp;
29957 				continue;
29958 			}
29959 
29960 			/*
29961 			 * Remove the bp from the wait queue.
29962 			 */
29963 			if (bp == un->un_waitq_headp) {
29964 				/* The bp is the first element of the waitq. */
29965 				un->un_waitq_headp = next_waitq_bp;
29966 				if (un->un_waitq_headp == NULL) {
29967 					/* The wait queue is now empty */
29968 					un->un_waitq_tailp = NULL;
29969 				}
29970 			} else {
29971 				/*
29972 				 * The bp is either somewhere in the middle
29973 				 * or at the end of the wait queue.
29974 				 */
29975 				ASSERT(un->un_waitq_headp != NULL);
29976 				ASSERT(prev_waitq_bp != NULL);
29977 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29978 				    == 0);
29979 				if (bp == un->un_waitq_tailp) {
29980 					/* bp is the last entry on the waitq. */
29981 					ASSERT(next_waitq_bp == NULL);
29982 					un->un_waitq_tailp = prev_waitq_bp;
29983 				}
29984 				prev_waitq_bp->av_forw = next_waitq_bp;
29985 			}
29986 			bp->av_forw = NULL;
29987 
29988 			/*
29989 			 * update kstat since the bp is moved out of
29990 			 * the waitq
29991 			 */
29992 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29993 
29994 			/*
29995 			 * Now put the bp onto the failfast queue.
29996 			 */
29997 			if (un->un_failfast_headp == NULL) {
29998 				/* failfast queue is currently empty */
29999 				ASSERT(un->un_failfast_tailp == NULL);
30000 				un->un_failfast_headp =
30001 				    un->un_failfast_tailp = bp;
30002 			} else {
30003 				/* Add the bp to the end of the failfast q */
30004 				ASSERT(un->un_failfast_tailp != NULL);
30005 				ASSERT(un->un_failfast_tailp->b_flags &
30006 				    B_FAILFAST);
30007 				un->un_failfast_tailp->av_forw = bp;
30008 				un->un_failfast_tailp = bp;
30009 			}
30010 		}
30011 	}
30012 
30013 	/*
30014 	 * Now return all bp's on the failfast queue to their owners.
30015 	 */
30016 	while ((bp = un->un_failfast_headp) != NULL) {
30017 
30018 		un->un_failfast_headp = bp->av_forw;
30019 		if (un->un_failfast_headp == NULL) {
30020 			un->un_failfast_tailp = NULL;
30021 		}
30022 
30023 		/*
30024 		 * We want to return the bp with a failure error code, but
30025 		 * we do not want a call to sd_start_cmds() to occur here,
30026 		 * so use sd_return_failed_command_no_restart() instead of
30027 		 * sd_return_failed_command().
30028 		 */
30029 		sd_return_failed_command_no_restart(un, bp, EIO);
30030 	}
30031 
30032 	/* Flush the xbuf queues if required. */
30033 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
30034 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
30035 	}
30036 
30037 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
30038 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
30039 }
30040 
30041 
30042 /*
30043  *    Function: sd_failfast_flushq_callback
30044  *
30045  * Description: Return TRUE if the given bp meets the criteria for failfast
30046  *		flushing. Used with ddi_xbuf_flushq(9F).
30047  *
30048  *   Arguments: bp - ptr to buf struct to be examined.
30049  *
30050  *     Context: Any
30051  */
30052 
30053 static int
30054 sd_failfast_flushq_callback(struct buf *bp)
30055 {
30056 	/*
30057 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
30058 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
30059 	 */
30060 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
30061 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
30062 }
30063 
30064 
30065 
30066 #if defined(__i386) || defined(__amd64)
30067 /*
30068  * Function: sd_setup_next_xfer
30069  *
30070  * Description: Prepare next I/O operation using DMA_PARTIAL
30071  *
30072  */
30073 
30074 static int
30075 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
30076     struct scsi_pkt *pkt, struct sd_xbuf *xp)
30077 {
30078 	ssize_t	num_blks_not_xfered;
30079 	daddr_t	strt_blk_num;
30080 	ssize_t	bytes_not_xfered;
30081 	int	rval;
30082 
30083 	ASSERT(pkt->pkt_resid == 0);
30084 
30085 	/*
30086 	 * Calculate next block number and amount to be transferred.
30087 	 *
30088 	 * How much data NOT transfered to the HBA yet.
30089 	 */
30090 	bytes_not_xfered = xp->xb_dma_resid;
30091 
30092 	/*
30093 	 * figure how many blocks NOT transfered to the HBA yet.
30094 	 */
30095 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
30096 
30097 	/*
30098 	 * set starting block number to the end of what WAS transfered.
30099 	 */
30100 	strt_blk_num = xp->xb_blkno +
30101 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
30102 
30103 	/*
30104 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
30105 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
30106 	 * the disk mutex here.
30107 	 */
30108 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
30109 	    strt_blk_num, num_blks_not_xfered);
30110 
30111 	if (rval == 0) {
30112 
30113 		/*
30114 		 * Success.
30115 		 *
30116 		 * Adjust things if there are still more blocks to be
30117 		 * transfered.
30118 		 */
30119 		xp->xb_dma_resid = pkt->pkt_resid;
30120 		pkt->pkt_resid = 0;
30121 
30122 		return (1);
30123 	}
30124 
30125 	/*
30126 	 * There's really only one possible return value from
30127 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
30128 	 * returns NULL.
30129 	 */
30130 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
30131 
30132 	bp->b_resid = bp->b_bcount;
30133 	bp->b_flags |= B_ERROR;
30134 
30135 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30136 	    "Error setting up next portion of DMA transfer\n");
30137 
30138 	return (0);
30139 }
30140 #endif
30141 
30142 /*
30143  *    Function: sd_panic_for_res_conflict
30144  *
30145  * Description: Call panic with a string formated with "Reservation Conflict"
30146  *		and a human readable identifier indicating the SD instance
30147  *		that experienced the reservation conflict.
30148  *
30149  *   Arguments: un - pointer to the soft state struct for the instance.
30150  *
30151  *     Context: may execute in interrupt context.
30152  */
30153 
30154 #define	SD_RESV_CONFLICT_FMT_LEN 40
30155 void
30156 sd_panic_for_res_conflict(struct sd_lun *un)
30157 {
30158 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30159 	char path_str[MAXPATHLEN];
30160 
30161 	(void) snprintf(panic_str, sizeof (panic_str),
30162 	    "Reservation Conflict\nDisk: %s",
30163 	    ddi_pathname(SD_DEVINFO(un), path_str));
30164 
30165 	panic(panic_str);
30166 }
30167 
30168 /*
30169  * Note: The following sd_faultinjection_ioctl( ) routines implement
30170  * driver support for handling fault injection for error analysis
30171  * causing faults in multiple layers of the driver.
30172  *
30173  */
30174 
30175 #ifdef SD_FAULT_INJECTION
30176 static uint_t   sd_fault_injection_on = 0;
30177 
30178 /*
30179  *    Function: sd_faultinjection_ioctl()
30180  *
30181  * Description: This routine is the driver entry point for handling
30182  *              faultinjection ioctls to inject errors into the
30183  *              layer model
30184  *
30185  *   Arguments: cmd	- the ioctl cmd recieved
30186  *		arg	- the arguments from user and returns
30187  */
30188 
30189 static void
30190 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
30191 
30192 	uint_t i;
30193 	uint_t rval;
30194 
30195 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30196 
30197 	mutex_enter(SD_MUTEX(un));
30198 
30199 	switch (cmd) {
30200 	case SDIOCRUN:
30201 		/* Allow pushed faults to be injected */
30202 		SD_INFO(SD_LOG_SDTEST, un,
30203 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
30204 
30205 		sd_fault_injection_on = 1;
30206 
30207 		SD_INFO(SD_LOG_IOERR, un,
30208 		    "sd_faultinjection_ioctl: run finished\n");
30209 		break;
30210 
30211 	case SDIOCSTART:
30212 		/* Start Injection Session */
30213 		SD_INFO(SD_LOG_SDTEST, un,
30214 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
30215 
30216 		sd_fault_injection_on = 0;
30217 		un->sd_injection_mask = 0xFFFFFFFF;
30218 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30219 			un->sd_fi_fifo_pkt[i] = NULL;
30220 			un->sd_fi_fifo_xb[i] = NULL;
30221 			un->sd_fi_fifo_un[i] = NULL;
30222 			un->sd_fi_fifo_arq[i] = NULL;
30223 		}
30224 		un->sd_fi_fifo_start = 0;
30225 		un->sd_fi_fifo_end = 0;
30226 
30227 		mutex_enter(&(un->un_fi_mutex));
30228 		un->sd_fi_log[0] = '\0';
30229 		un->sd_fi_buf_len = 0;
30230 		mutex_exit(&(un->un_fi_mutex));
30231 
30232 		SD_INFO(SD_LOG_IOERR, un,
30233 		    "sd_faultinjection_ioctl: start finished\n");
30234 		break;
30235 
30236 	case SDIOCSTOP:
30237 		/* Stop Injection Session */
30238 		SD_INFO(SD_LOG_SDTEST, un,
30239 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30240 		sd_fault_injection_on = 0;
30241 		un->sd_injection_mask = 0x0;
30242 
30243 		/* Empty stray or unuseds structs from fifo */
30244 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30245 			if (un->sd_fi_fifo_pkt[i] != NULL) {
30246 				kmem_free(un->sd_fi_fifo_pkt[i],
30247 				    sizeof (struct sd_fi_pkt));
30248 			}
30249 			if (un->sd_fi_fifo_xb[i] != NULL) {
30250 				kmem_free(un->sd_fi_fifo_xb[i],
30251 				    sizeof (struct sd_fi_xb));
30252 			}
30253 			if (un->sd_fi_fifo_un[i] != NULL) {
30254 				kmem_free(un->sd_fi_fifo_un[i],
30255 				    sizeof (struct sd_fi_un));
30256 			}
30257 			if (un->sd_fi_fifo_arq[i] != NULL) {
30258 				kmem_free(un->sd_fi_fifo_arq[i],
30259 				    sizeof (struct sd_fi_arq));
30260 			}
30261 			un->sd_fi_fifo_pkt[i] = NULL;
30262 			un->sd_fi_fifo_un[i] = NULL;
30263 			un->sd_fi_fifo_xb[i] = NULL;
30264 			un->sd_fi_fifo_arq[i] = NULL;
30265 		}
30266 		un->sd_fi_fifo_start = 0;
30267 		un->sd_fi_fifo_end = 0;
30268 
30269 		SD_INFO(SD_LOG_IOERR, un,
30270 		    "sd_faultinjection_ioctl: stop finished\n");
30271 		break;
30272 
30273 	case SDIOCINSERTPKT:
30274 		/* Store a packet struct to be pushed onto fifo */
30275 		SD_INFO(SD_LOG_SDTEST, un,
30276 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30277 
30278 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30279 
30280 		sd_fault_injection_on = 0;
30281 
30282 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30283 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30284 			kmem_free(un->sd_fi_fifo_pkt[i],
30285 			    sizeof (struct sd_fi_pkt));
30286 		}
30287 		if (arg != NULL) {
30288 			un->sd_fi_fifo_pkt[i] =
30289 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30290 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30291 				/* Alloc failed don't store anything */
30292 				break;
30293 			}
30294 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30295 			    sizeof (struct sd_fi_pkt), 0);
30296 			if (rval == -1) {
30297 				kmem_free(un->sd_fi_fifo_pkt[i],
30298 				    sizeof (struct sd_fi_pkt));
30299 				un->sd_fi_fifo_pkt[i] = NULL;
30300 			}
30301 		} else {
30302 			SD_INFO(SD_LOG_IOERR, un,
30303 			    "sd_faultinjection_ioctl: pkt null\n");
30304 		}
30305 		break;
30306 
30307 	case SDIOCINSERTXB:
30308 		/* Store a xb struct to be pushed onto fifo */
30309 		SD_INFO(SD_LOG_SDTEST, un,
30310 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30311 
30312 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30313 
30314 		sd_fault_injection_on = 0;
30315 
30316 		if (un->sd_fi_fifo_xb[i] != NULL) {
30317 			kmem_free(un->sd_fi_fifo_xb[i],
30318 			    sizeof (struct sd_fi_xb));
30319 			un->sd_fi_fifo_xb[i] = NULL;
30320 		}
30321 		if (arg != NULL) {
30322 			un->sd_fi_fifo_xb[i] =
30323 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30324 			if (un->sd_fi_fifo_xb[i] == NULL) {
30325 				/* Alloc failed don't store anything */
30326 				break;
30327 			}
30328 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30329 			    sizeof (struct sd_fi_xb), 0);
30330 
30331 			if (rval == -1) {
30332 				kmem_free(un->sd_fi_fifo_xb[i],
30333 				    sizeof (struct sd_fi_xb));
30334 				un->sd_fi_fifo_xb[i] = NULL;
30335 			}
30336 		} else {
30337 			SD_INFO(SD_LOG_IOERR, un,
30338 			    "sd_faultinjection_ioctl: xb null\n");
30339 		}
30340 		break;
30341 
30342 	case SDIOCINSERTUN:
30343 		/* Store a un struct to be pushed onto fifo */
30344 		SD_INFO(SD_LOG_SDTEST, un,
30345 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30346 
30347 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30348 
30349 		sd_fault_injection_on = 0;
30350 
30351 		if (un->sd_fi_fifo_un[i] != NULL) {
30352 			kmem_free(un->sd_fi_fifo_un[i],
30353 			    sizeof (struct sd_fi_un));
30354 			un->sd_fi_fifo_un[i] = NULL;
30355 		}
30356 		if (arg != NULL) {
30357 			un->sd_fi_fifo_un[i] =
30358 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30359 			if (un->sd_fi_fifo_un[i] == NULL) {
30360 				/* Alloc failed don't store anything */
30361 				break;
30362 			}
30363 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30364 			    sizeof (struct sd_fi_un), 0);
30365 			if (rval == -1) {
30366 				kmem_free(un->sd_fi_fifo_un[i],
30367 				    sizeof (struct sd_fi_un));
30368 				un->sd_fi_fifo_un[i] = NULL;
30369 			}
30370 
30371 		} else {
30372 			SD_INFO(SD_LOG_IOERR, un,
30373 			    "sd_faultinjection_ioctl: un null\n");
30374 		}
30375 
30376 		break;
30377 
30378 	case SDIOCINSERTARQ:
30379 		/* Store a arq struct to be pushed onto fifo */
30380 		SD_INFO(SD_LOG_SDTEST, un,
30381 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30382 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30383 
30384 		sd_fault_injection_on = 0;
30385 
30386 		if (un->sd_fi_fifo_arq[i] != NULL) {
30387 			kmem_free(un->sd_fi_fifo_arq[i],
30388 			    sizeof (struct sd_fi_arq));
30389 			un->sd_fi_fifo_arq[i] = NULL;
30390 		}
30391 		if (arg != NULL) {
30392 			un->sd_fi_fifo_arq[i] =
30393 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30394 			if (un->sd_fi_fifo_arq[i] == NULL) {
30395 				/* Alloc failed don't store anything */
30396 				break;
30397 			}
30398 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30399 			    sizeof (struct sd_fi_arq), 0);
30400 			if (rval == -1) {
30401 				kmem_free(un->sd_fi_fifo_arq[i],
30402 				    sizeof (struct sd_fi_arq));
30403 				un->sd_fi_fifo_arq[i] = NULL;
30404 			}
30405 
30406 		} else {
30407 			SD_INFO(SD_LOG_IOERR, un,
30408 			    "sd_faultinjection_ioctl: arq null\n");
30409 		}
30410 
30411 		break;
30412 
30413 	case SDIOCPUSH:
30414 		/* Push stored xb, pkt, un, and arq onto fifo */
30415 		sd_fault_injection_on = 0;
30416 
30417 		if (arg != NULL) {
30418 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30419 			if (rval != -1 &&
30420 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30421 				un->sd_fi_fifo_end += i;
30422 			}
30423 		} else {
30424 			SD_INFO(SD_LOG_IOERR, un,
30425 			    "sd_faultinjection_ioctl: push arg null\n");
30426 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30427 				un->sd_fi_fifo_end++;
30428 			}
30429 		}
30430 		SD_INFO(SD_LOG_IOERR, un,
30431 		    "sd_faultinjection_ioctl: push to end=%d\n",
30432 		    un->sd_fi_fifo_end);
30433 		break;
30434 
30435 	case SDIOCRETRIEVE:
30436 		/* Return buffer of log from Injection session */
30437 		SD_INFO(SD_LOG_SDTEST, un,
30438 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30439 
30440 		sd_fault_injection_on = 0;
30441 
30442 		mutex_enter(&(un->un_fi_mutex));
30443 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30444 		    un->sd_fi_buf_len+1, 0);
30445 		mutex_exit(&(un->un_fi_mutex));
30446 
30447 		if (rval == -1) {
30448 			/*
30449 			 * arg is possibly invalid setting
30450 			 * it to NULL for return
30451 			 */
30452 			arg = NULL;
30453 		}
30454 		break;
30455 	}
30456 
30457 	mutex_exit(SD_MUTEX(un));
30458 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30459 			    " exit\n");
30460 }
30461 
30462 
30463 /*
30464  *    Function: sd_injection_log()
30465  *
30466  * Description: This routine adds buff to the already existing injection log
30467  *              for retrieval via faultinjection_ioctl for use in fault
30468  *              detection and recovery
30469  *
30470  *   Arguments: buf - the string to add to the log
30471  */
30472 
30473 static void
30474 sd_injection_log(char *buf, struct sd_lun *un)
30475 {
30476 	uint_t len;
30477 
30478 	ASSERT(un != NULL);
30479 	ASSERT(buf != NULL);
30480 
30481 	mutex_enter(&(un->un_fi_mutex));
30482 
30483 	len = min(strlen(buf), 255);
30484 	/* Add logged value to Injection log to be returned later */
30485 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30486 		uint_t	offset = strlen((char *)un->sd_fi_log);
30487 		char *destp = (char *)un->sd_fi_log + offset;
30488 		int i;
30489 		for (i = 0; i < len; i++) {
30490 			*destp++ = *buf++;
30491 		}
30492 		un->sd_fi_buf_len += len;
30493 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30494 	}
30495 
30496 	mutex_exit(&(un->un_fi_mutex));
30497 }
30498 
30499 
30500 /*
30501  *    Function: sd_faultinjection()
30502  *
30503  * Description: This routine takes the pkt and changes its
30504  *		content based on error injection scenerio.
30505  *
30506  *   Arguments: pktp	- packet to be changed
30507  */
30508 
30509 static void
30510 sd_faultinjection(struct scsi_pkt *pktp)
30511 {
30512 	uint_t i;
30513 	struct sd_fi_pkt *fi_pkt;
30514 	struct sd_fi_xb *fi_xb;
30515 	struct sd_fi_un *fi_un;
30516 	struct sd_fi_arq *fi_arq;
30517 	struct buf *bp;
30518 	struct sd_xbuf *xb;
30519 	struct sd_lun *un;
30520 
30521 	ASSERT(pktp != NULL);
30522 
30523 	/* pull bp xb and un from pktp */
30524 	bp = (struct buf *)pktp->pkt_private;
30525 	xb = SD_GET_XBUF(bp);
30526 	un = SD_GET_UN(bp);
30527 
30528 	ASSERT(un != NULL);
30529 
30530 	mutex_enter(SD_MUTEX(un));
30531 
30532 	SD_TRACE(SD_LOG_SDTEST, un,
30533 	    "sd_faultinjection: entry Injection from sdintr\n");
30534 
30535 	/* if injection is off return */
30536 	if (sd_fault_injection_on == 0 ||
30537 		un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30538 		mutex_exit(SD_MUTEX(un));
30539 		return;
30540 	}
30541 
30542 
30543 	/* take next set off fifo */
30544 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30545 
30546 	fi_pkt = un->sd_fi_fifo_pkt[i];
30547 	fi_xb = un->sd_fi_fifo_xb[i];
30548 	fi_un = un->sd_fi_fifo_un[i];
30549 	fi_arq = un->sd_fi_fifo_arq[i];
30550 
30551 
30552 	/* set variables accordingly */
30553 	/* set pkt if it was on fifo */
30554 	if (fi_pkt != NULL) {
30555 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30556 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30557 		SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30558 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30559 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30560 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30561 
30562 	}
30563 
30564 	/* set xb if it was on fifo */
30565 	if (fi_xb != NULL) {
30566 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30567 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30568 		SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30569 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30570 		    "xb_victim_retry_count");
30571 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30572 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30573 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30574 
30575 		/* copy in block data from sense */
30576 		if (fi_xb->xb_sense_data[0] != -1) {
30577 			bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30578 			    SENSE_LENGTH);
30579 		}
30580 
30581 		/* copy in extended sense codes */
30582 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code,
30583 		    "es_code");
30584 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key,
30585 		    "es_key");
30586 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code,
30587 		    "es_add_code");
30588 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb,
30589 		    es_qual_code, "es_qual_code");
30590 	}
30591 
30592 	/* set un if it was on fifo */
30593 	if (fi_un != NULL) {
30594 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30595 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30596 		SD_CONDSET(un, un, un_reset_retry_count,
30597 		    "un_reset_retry_count");
30598 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30599 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30600 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30601 		SD_CONDSET(un, un, un_f_geometry_is_valid,
30602 		    "un_f_geometry_is_valid");
30603 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30604 		    "un_f_allow_bus_device_reset");
30605 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30606 
30607 	}
30608 
30609 	/* copy in auto request sense if it was on fifo */
30610 	if (fi_arq != NULL) {
30611 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30612 	}
30613 
30614 	/* free structs */
30615 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30616 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30617 	}
30618 	if (un->sd_fi_fifo_xb[i] != NULL) {
30619 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30620 	}
30621 	if (un->sd_fi_fifo_un[i] != NULL) {
30622 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30623 	}
30624 	if (un->sd_fi_fifo_arq[i] != NULL) {
30625 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30626 	}
30627 
30628 	/*
30629 	 * kmem_free does not gurantee to set to NULL
30630 	 * since we uses these to determine if we set
30631 	 * values or not lets confirm they are always
30632 	 * NULL after free
30633 	 */
30634 	un->sd_fi_fifo_pkt[i] = NULL;
30635 	un->sd_fi_fifo_un[i] = NULL;
30636 	un->sd_fi_fifo_xb[i] = NULL;
30637 	un->sd_fi_fifo_arq[i] = NULL;
30638 
30639 	un->sd_fi_fifo_start++;
30640 
30641 	mutex_exit(SD_MUTEX(un));
30642 
30643 	SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30644 }
30645 
30646 #endif /* SD_FAULT_INJECTION */
30647 
30648 /*
30649  * This routine is invoked in sd_unit_attach(). Before calling it, the
30650  * properties in conf file should be processed already, and "hotpluggable"
30651  * property was processed also.
30652  *
30653  * The sd driver distinguishes 3 different type of devices: removable media,
30654  * non-removable media, and hotpluggable. Below the differences are defined:
30655  *
30656  * 1. Device ID
30657  *
30658  *     The device ID of a device is used to identify this device. Refer to
30659  *     ddi_devid_register(9F).
30660  *
30661  *     For a non-removable media disk device which can provide 0x80 or 0x83
30662  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30663  *     device ID is created to identify this device. For other non-removable
30664  *     media devices, a default device ID is created only if this device has
30665  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30666  *
30667  *     -------------------------------------------------------
30668  *     removable media   hotpluggable  | Can Have Device ID
30669  *     -------------------------------------------------------
30670  *         false             false     |     Yes
30671  *         false             true      |     Yes
30672  *         true                x       |     No
30673  *     ------------------------------------------------------
30674  *
30675  *
30676  * 2. SCSI group 4 commands
30677  *
30678  *     In SCSI specs, only some commands in group 4 command set can use
30679  *     8-byte addresses that can be used to access >2TB storage spaces.
30680  *     Other commands have no such capability. Without supporting group4,
30681  *     it is impossible to make full use of storage spaces of a disk with
30682  *     capacity larger than 2TB.
30683  *
30684  *     -----------------------------------------------
30685  *     removable media   hotpluggable   LP64  |  Group
30686  *     -----------------------------------------------
30687  *           false          false       false |   1
30688  *           false          false       true  |   4
30689  *           false          true        false |   1
30690  *           false          true        true  |   4
30691  *           true             x           x   |   5
30692  *     -----------------------------------------------
30693  *
30694  *
30695  * 3. Check for VTOC Label
30696  *
30697  *     If a direct-access disk has no EFI label, sd will check if it has a
30698  *     valid VTOC label. Now, sd also does that check for removable media
30699  *     and hotpluggable devices.
30700  *
30701  *     --------------------------------------------------------------
30702  *     Direct-Access   removable media    hotpluggable |  Check Label
30703  *     -------------------------------------------------------------
30704  *         false          false           false        |   No
30705  *         false          false           true         |   No
30706  *         false          true            false        |   Yes
30707  *         false          true            true         |   Yes
30708  *         true            x                x          |   Yes
30709  *     --------------------------------------------------------------
30710  *
30711  *
30712  * 4. Building default VTOC label
30713  *
30714  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30715  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30716  *     create default VTOC for them. Currently sd creates default VTOC label
30717  *     for all devices on x86 platform (VTOC_16), but only for removable
30718  *     media devices on SPARC (VTOC_8).
30719  *
30720  *     -----------------------------------------------------------
30721  *       removable media hotpluggable platform   |   Default Label
30722  *     -----------------------------------------------------------
30723  *             false          false    sparc     |     No
30724  *             false          true      x86      |     Yes
30725  *             false          true     sparc     |     Yes
30726  *             true             x        x       |     Yes
30727  *     ----------------------------------------------------------
30728  *
30729  *
30730  * 5. Supported blocksizes of target devices
30731  *
30732  *     Sd supports non-512-byte blocksize for removable media devices only.
30733  *     For other devices, only 512-byte blocksize is supported. This may be
30734  *     changed in near future because some RAID devices require non-512-byte
30735  *     blocksize
30736  *
30737  *     -----------------------------------------------------------
30738  *     removable media    hotpluggable    | non-512-byte blocksize
30739  *     -----------------------------------------------------------
30740  *           false          false         |   No
30741  *           false          true          |   No
30742  *           true             x           |   Yes
30743  *     -----------------------------------------------------------
30744  *
30745  *
30746  * 6. Automatic mount & unmount (i.e. vold)
30747  *
30748  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30749  *     if a device is removable media device. It return 1 for removable media
30750  *     devices, and 0 for others.
30751  *
30752  *     Vold treats a device as removable one only if DKIOREMOVABLE returns 1.
30753  *     And it does automounting only for removable media devices. In order to
30754  *     preserve users' experience and let vold continue to do automounting for
30755  *     USB disk devices, DKIOCREMOVABLE ioctl still returns 1 for USB/1394 disk
30756  *     devices.
30757  *
30758  *      ------------------------------------------------------
30759  *       removable media    hotpluggable   |  automatic mount
30760  *      ------------------------------------------------------
30761  *             false          false        |   No
30762  *             false          true         |   Yes
30763  *             true             x          |   Yes
30764  *      ------------------------------------------------------
30765  *
30766  *
30767  * 7. fdisk partition management
30768  *
30769  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30770  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30771  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30772  *     fdisk partitions on both x86 and SPARC platform.
30773  *
30774  *     -----------------------------------------------------------
30775  *       platform   removable media  USB/1394  |  fdisk supported
30776  *     -----------------------------------------------------------
30777  *        x86         X               X        |       true
30778  *     ------------------------------------------------------------
30779  *        sparc       X               X        |       false
30780  *     ------------------------------------------------------------
30781  *
30782  *
30783  * 8. MBOOT/MBR
30784  *
30785  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30786  *     read/write mboot for removable media devices on sparc platform.
30787  *
30788  *     -----------------------------------------------------------
30789  *       platform   removable media  USB/1394  |  mboot supported
30790  *     -----------------------------------------------------------
30791  *        x86         X               X        |       true
30792  *     ------------------------------------------------------------
30793  *        sparc      false           false     |       false
30794  *        sparc      false           true      |       true
30795  *        sparc      true            false     |       true
30796  *        sparc      true            true      |       true
30797  *     ------------------------------------------------------------
30798  *
30799  *
30800  * 9.  error handling during opening device
30801  *
30802  *     If failed to open a disk device, an errno is returned. For some kinds
30803  *     of errors, different errno is returned depending on if this device is
30804  *     a removable media device. This brings USB/1394 hard disks in line with
30805  *     expected hard disk behavior. It is not expected that this breaks any
30806  *     application.
30807  *
30808  *     ------------------------------------------------------
30809  *       removable media    hotpluggable   |  errno
30810  *     ------------------------------------------------------
30811  *             false          false        |   EIO
30812  *             false          true         |   EIO
30813  *             true             x          |   ENXIO
30814  *     ------------------------------------------------------
30815  *
30816  *
30817  * 10. off-by-1 workaround (bug 1175930, and 4996920) (x86 only)
30818  *
30819  *     [ this is a bit of very ugly history, soon to be removed ]
30820  *
30821  *     SCSI READ_CAPACITY command returns the last valid logical block number
30822  *     which starts from 0. So real capacity is larger than the returned
30823  *     value by 1. However, because scdk.c (which was EOL'ed) directly used
30824  *     the logical block number as capacity of disk devices, off-by-1 work-
30825  *     around was applied. This workaround causes fixed SCSI disk to loss a
30826  *     sector on x86 platform, and precludes exchanging fixed hard disks
30827  *     between sparc and x86.
30828  *
30829  *     ------------------------------------------------------
30830  *       removable media    hotplug        |   Off-by-1 works
30831  *     -------------------------------------------------------
30832  *             false          false        |     Yes
30833  *             false          true         |     No
30834  *             true           false        |     No
30835  *             true           true         |     No
30836  *     ------------------------------------------------------
30837  *
30838  *
30839  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30840  *
30841  *     These IOCTLs are applicable only to removable media devices.
30842  *
30843  *     -----------------------------------------------------------
30844  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30845  *     -----------------------------------------------------------
30846  *             false          false        |     No
30847  *             false          true         |     No
30848  *             true            x           |     Yes
30849  *     -----------------------------------------------------------
30850  *
30851  *
30852  * 12. Kstats for partitions
30853  *
30854  *     sd creates partition kstat for non-removable media devices. USB and
30855  *     Firewire hard disks now have partition kstats
30856  *
30857  *      ------------------------------------------------------
30858  *       removable media    hotplugable    |   kstat
30859  *      ------------------------------------------------------
30860  *             false          false        |    Yes
30861  *             false          true         |    Yes
30862  *             true             x          |    No
30863  *       ------------------------------------------------------
30864  *
30865  *
30866  * 13. Removable media & hotpluggable properties
30867  *
30868  *     Sd driver creates a "removable-media" property for removable media
30869  *     devices. Parent nexus drivers create a "hotpluggable" property if
30870  *     it supports hotplugging.
30871  *
30872  *     ---------------------------------------------------------------------
30873  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30874  *     ---------------------------------------------------------------------
30875  *       false            false       |    No                   No
30876  *       false            true        |    No                   Yes
30877  *       true             false       |    Yes                  No
30878  *       true             true        |    Yes                  Yes
30879  *     ---------------------------------------------------------------------
30880  *
30881  *
30882  * 14. Power Management
30883  *
30884  *     sd only power manages removable media devices or devices that support
30885  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30886  *
30887  *     A parent nexus that supports hotplugging can also set "pm-capable"
30888  *     if the disk can be power managed.
30889  *
30890  *     ------------------------------------------------------------
30891  *       removable media hotpluggable pm-capable  |   power manage
30892  *     ------------------------------------------------------------
30893  *             false          false     false     |     No
30894  *             false          false     true      |     Yes
30895  *             false          true      false     |     No
30896  *             false          true      true      |     Yes
30897  *             true             x        x        |     Yes
30898  *     ------------------------------------------------------------
30899  *
30900  *      USB and firewire hard disks can now be power managed independently
30901  *      of the framebuffer
30902  *
30903  *
30904  * 15. Support for USB disks with capacity larger than 1TB
30905  *
30906  *     Currently, sd doesn't permit a fixed disk device with capacity
30907  *     larger than 1TB to be used in a 32-bit operating system environment.
30908  *     However, sd doesn't do that for removable media devices. Instead, it
30909  *     assumes that removable media devices cannot have a capacity larger
30910  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30911  *     supported, which can cause some unexpected results.
30912  *
30913  *     ---------------------------------------------------------------------
30914  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30915  *     ---------------------------------------------------------------------
30916  *             false          false  |   true         |     no
30917  *             false          true   |   true         |     no
30918  *             true           false  |   true         |     Yes
30919  *             true           true   |   true         |     Yes
30920  *     ---------------------------------------------------------------------
30921  *
30922  *
30923  * 16. Check write-protection at open time
30924  *
30925  *     When a removable media device is being opened for writing without NDELAY
30926  *     flag, sd will check if this device is writable. If attempting to open
30927  *     without NDELAY flag a write-protected device, this operation will abort.
30928  *
30929  *     ------------------------------------------------------------
30930  *       removable media    USB/1394   |   WP Check
30931  *     ------------------------------------------------------------
30932  *             false          false    |     No
30933  *             false          true     |     No
30934  *             true           false    |     Yes
30935  *             true           true     |     Yes
30936  *     ------------------------------------------------------------
30937  *
30938  *
30939  * 17. syslog when corrupted VTOC is encountered
30940  *
30941  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30942  *      for fixed SCSI disks.
30943  *     ------------------------------------------------------------
30944  *       removable media    USB/1394   |   print syslog
30945  *     ------------------------------------------------------------
30946  *             false          false    |     Yes
30947  *             false          true     |     No
30948  *             true           false    |     No
30949  *             true           true     |     No
30950  *     ------------------------------------------------------------
30951  */
30952 static void
30953 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30954 {
30955 	int	pm_capable_prop;
30956 
30957 	ASSERT(un->un_sd);
30958 	ASSERT(un->un_sd->sd_inq);
30959 
30960 #if defined(_SUNOS_VTOC_16)
30961 	/*
30962 	 * For VTOC_16 devices, the default label will be created for all
30963 	 * devices. (see sd_build_default_label)
30964 	 */
30965 	un->un_f_default_vtoc_supported = TRUE;
30966 #endif
30967 
30968 	if (un->un_sd->sd_inq->inq_rmb) {
30969 		/*
30970 		 * The media of this device is removable. And for this kind
30971 		 * of devices, it is possible to change medium after openning
30972 		 * devices. Thus we should support this operation.
30973 		 */
30974 		un->un_f_has_removable_media = TRUE;
30975 
30976 #if defined(_SUNOS_VTOC_8)
30977 		/*
30978 		 * Note: currently, for VTOC_8 devices, default label is
30979 		 * created for removable and hotpluggable devices only.
30980 		 */
30981 		un->un_f_default_vtoc_supported = TRUE;
30982 #endif
30983 		/*
30984 		 * support non-512-byte blocksize of removable media devices
30985 		 */
30986 		un->un_f_non_devbsize_supported = TRUE;
30987 
30988 		/*
30989 		 * Assume that all removable media devices support DOOR_LOCK
30990 		 */
30991 		un->un_f_doorlock_supported = TRUE;
30992 
30993 		/*
30994 		 * For a removable media device, it is possible to be opened
30995 		 * with NDELAY flag when there is no media in drive, in this
30996 		 * case we don't care if device is writable. But if without
30997 		 * NDELAY flag, we need to check if media is write-protected.
30998 		 */
30999 		un->un_f_chk_wp_open = TRUE;
31000 
31001 		/*
31002 		 * need to start a SCSI watch thread to monitor media state,
31003 		 * when media is being inserted or ejected, notify syseventd.
31004 		 */
31005 		un->un_f_monitor_media_state = TRUE;
31006 
31007 		/*
31008 		 * Some devices don't support START_STOP_UNIT command.
31009 		 * Therefore, we'd better check if a device supports it
31010 		 * before sending it.
31011 		 */
31012 		un->un_f_check_start_stop = TRUE;
31013 
31014 		/*
31015 		 * support eject media ioctl:
31016 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
31017 		 */
31018 		un->un_f_eject_media_supported = TRUE;
31019 
31020 		/*
31021 		 * Because many removable-media devices don't support
31022 		 * LOG_SENSE, we couldn't use this command to check if
31023 		 * a removable media device support power-management.
31024 		 * We assume that they support power-management via
31025 		 * START_STOP_UNIT command and can be spun up and down
31026 		 * without limitations.
31027 		 */
31028 		un->un_f_pm_supported = TRUE;
31029 
31030 		/*
31031 		 * Need to create a zero length (Boolean) property
31032 		 * removable-media for the removable media devices.
31033 		 * Note that the return value of the property is not being
31034 		 * checked, since if unable to create the property
31035 		 * then do not want the attach to fail altogether. Consistent
31036 		 * with other property creation in attach.
31037 		 */
31038 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
31039 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
31040 
31041 	} else {
31042 		/*
31043 		 * create device ID for device
31044 		 */
31045 		un->un_f_devid_supported = TRUE;
31046 
31047 		/*
31048 		 * Spin up non-removable-media devices once it is attached
31049 		 */
31050 		un->un_f_attach_spinup = TRUE;
31051 
31052 		/*
31053 		 * According to SCSI specification, Sense data has two kinds of
31054 		 * format: fixed format, and descriptor format. At present, we
31055 		 * don't support descriptor format sense data for removable
31056 		 * media.
31057 		 */
31058 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31059 			un->un_f_descr_format_supported = TRUE;
31060 		}
31061 
31062 		/*
31063 		 * kstats are created only for non-removable media devices.
31064 		 *
31065 		 * Set this in sd.conf to 0 in order to disable kstats.  The
31066 		 * default is 1, so they are enabled by default.
31067 		 */
31068 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
31069 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
31070 			"enable-partition-kstats", 1));
31071 
31072 		/*
31073 		 * Check if HBA has set the "pm-capable" property.
31074 		 * If "pm-capable" exists and is non-zero then we can
31075 		 * power manage the device without checking the start/stop
31076 		 * cycle count log sense page.
31077 		 *
31078 		 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0)
31079 		 * then we should not power manage the device.
31080 		 *
31081 		 * If "pm-capable" doesn't exist then pm_capable_prop will
31082 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
31083 		 * sd will check the start/stop cycle count log sense page
31084 		 * and power manage the device if the cycle count limit has
31085 		 * not been exceeded.
31086 		 */
31087 		pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
31088 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
31089 		if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) {
31090 			un->un_f_log_sense_supported = TRUE;
31091 		} else {
31092 			/*
31093 			 * pm-capable property exists.
31094 			 *
31095 			 * Convert "TRUE" values for pm_capable_prop to
31096 			 * SD_PM_CAPABLE_TRUE (1) to make it easier to check
31097 			 * later. "TRUE" values are any values except
31098 			 * SD_PM_CAPABLE_FALSE (0) and
31099 			 * SD_PM_CAPABLE_UNDEFINED (-1)
31100 			 */
31101 			if (pm_capable_prop == SD_PM_CAPABLE_FALSE) {
31102 				un->un_f_log_sense_supported = FALSE;
31103 			} else {
31104 				un->un_f_pm_supported = TRUE;
31105 			}
31106 
31107 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
31108 			    "sd_unit_attach: un:0x%p pm-capable "
31109 			    "property set to %d.\n", un, un->un_f_pm_supported);
31110 		}
31111 	}
31112 
31113 	if (un->un_f_is_hotpluggable) {
31114 #if defined(_SUNOS_VTOC_8)
31115 		/*
31116 		 * Note: currently, for VTOC_8 devices, default label is
31117 		 * created for removable and hotpluggable devices only.
31118 		 */
31119 		un->un_f_default_vtoc_supported = TRUE;
31120 #endif
31121 
31122 		/*
31123 		 * Temporarily, let hotpluggable devices pretend to be
31124 		 * removable-media devices for vold.
31125 		 */
31126 		un->un_f_monitor_media_state = TRUE;
31127 
31128 		un->un_f_check_start_stop = TRUE;
31129 
31130 	}
31131 
31132 	/*
31133 	 * By default, only DIRECT ACCESS devices and CDs will have Sun
31134 	 * labels.
31135 	 */
31136 	if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) ||
31137 	    (un->un_sd->sd_inq->inq_rmb)) {
31138 		/*
31139 		 * Direct access devices have disk label
31140 		 */
31141 		un->un_f_vtoc_label_supported = TRUE;
31142 	}
31143 
31144 	/*
31145 	 * Fdisk partitions are supported for all direct access devices on
31146 	 * x86 platform, and just for removable media and hotpluggable
31147 	 * devices on SPARC platform. Later, we will set the following flag
31148 	 * to FALSE if current device is not removable media or hotpluggable
31149 	 * device and if sd works on SAPRC platform.
31150 	 */
31151 	if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31152 		un->un_f_mboot_supported = TRUE;
31153 	}
31154 
31155 	if (!un->un_f_is_hotpluggable &&
31156 	    !un->un_sd->sd_inq->inq_rmb) {
31157 
31158 #if defined(_SUNOS_VTOC_8)
31159 		/*
31160 		 * Don't support fdisk on fixed disk
31161 		 */
31162 		un->un_f_mboot_supported = FALSE;
31163 #endif
31164 
31165 		/*
31166 		 * Fixed disk support SYNC CACHE
31167 		 */
31168 		un->un_f_sync_cache_supported = TRUE;
31169 
31170 		/*
31171 		 * For fixed disk, if its VTOC is not valid, we will write
31172 		 * errlog into system log
31173 		 */
31174 		if (un->un_f_vtoc_label_supported)
31175 			un->un_f_vtoc_errlog_supported = TRUE;
31176 	}
31177 }
31178