xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/sd.c (revision 80ab886d233f514d54c2a6bdeb9fdfd951bd6881)
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 
32 
33 
34 
35 #include <sys/scsi/scsi.h>
36 #include <sys/dkbad.h>
37 #include <sys/dklabel.h>
38 #include <sys/dkio.h>
39 #include <sys/fdio.h>
40 #include <sys/cdio.h>
41 #include <sys/mhd.h>
42 #include <sys/vtoc.h>
43 #include <sys/dktp/fdisk.h>
44 #include <sys/file.h>
45 #include <sys/stat.h>
46 #include <sys/kstat.h>
47 #include <sys/vtrace.h>
48 #include <sys/note.h>
49 #include <sys/thread.h>
50 #include <sys/proc.h>
51 #include <sys/efi_partition.h>
52 #include <sys/var.h>
53 #include <sys/aio_req.h>
54 
55 #ifdef __lock_lint
56 #define	_LP64
57 #define	__amd64
58 #endif
59 
60 #if (defined(__fibre))
61 /* Note: is there a leadville version of the following? */
62 #include <sys/fc4/fcal_linkapp.h>
63 #endif
64 #include <sys/taskq.h>
65 #include <sys/uuid.h>
66 #include <sys/byteorder.h>
67 #include <sys/sdt.h>
68 
69 #include "sd_xbuf.h"
70 
71 #include <sys/scsi/targets/sddef.h>
72 
73 
74 /*
75  * Loadable module info.
76  */
77 #if (defined(__fibre))
78 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver %I%"
79 char _depends_on[]	= "misc/scsi drv/fcp";
80 #else
81 #define	SD_MODULE_NAME	"SCSI Disk Driver %I%"
82 char _depends_on[]	= "misc/scsi";
83 #endif
84 
85 /*
86  * Define the interconnect type, to allow the driver to distinguish
87  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
88  *
89  * This is really for backward compatability. In the future, the driver
90  * should actually check the "interconnect-type" property as reported by
91  * the HBA; however at present this property is not defined by all HBAs,
92  * so we will use this #define (1) to permit the driver to run in
93  * backward-compatability mode; and (2) to print a notification message
94  * if an FC HBA does not support the "interconnect-type" property.  The
95  * behavior of the driver will be to assume parallel SCSI behaviors unless
96  * the "interconnect-type" property is defined by the HBA **AND** has a
97  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
98  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
99  * Channel behaviors (as per the old ssd).  (Note that the
100  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
101  * will result in the driver assuming parallel SCSI behaviors.)
102  *
103  * (see common/sys/scsi/impl/services.h)
104  *
105  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
106  * since some FC HBAs may already support that, and there is some code in
107  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
108  * default would confuse that code, and besides things should work fine
109  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
110  * "interconnect_type" property.
111  */
112 #if (defined(__fibre))
113 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
114 #else
115 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
116 #endif
117 
118 /*
119  * The name of the driver, established from the module name in _init.
120  */
121 static	char *sd_label			= NULL;
122 
123 /*
124  * Driver name is unfortunately prefixed on some driver.conf properties.
125  */
126 #if (defined(__fibre))
127 #define	sd_max_xfer_size		ssd_max_xfer_size
128 #define	sd_config_list			ssd_config_list
129 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
130 static	char *sd_config_list		= "ssd-config-list";
131 #else
132 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
133 static	char *sd_config_list		= "sd-config-list";
134 #endif
135 
136 /*
137  * Driver global variables
138  */
139 
140 #if (defined(__fibre))
141 /*
142  * These #defines are to avoid namespace collisions that occur because this
143  * code is currently used to compile two seperate driver modules: sd and ssd.
144  * All global variables need to be treated this way (even if declared static)
145  * in order to allow the debugger to resolve the names properly.
146  * It is anticipated that in the near future the ssd module will be obsoleted,
147  * at which time this namespace issue should go away.
148  */
149 #define	sd_state			ssd_state
150 #define	sd_io_time			ssd_io_time
151 #define	sd_failfast_enable		ssd_failfast_enable
152 #define	sd_ua_retry_count		ssd_ua_retry_count
153 #define	sd_report_pfa			ssd_report_pfa
154 #define	sd_max_throttle			ssd_max_throttle
155 #define	sd_min_throttle			ssd_min_throttle
156 #define	sd_rot_delay			ssd_rot_delay
157 
158 #define	sd_retry_on_reservation_conflict	\
159 					ssd_retry_on_reservation_conflict
160 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
161 #define	sd_resv_conflict_name		ssd_resv_conflict_name
162 
163 #define	sd_component_mask		ssd_component_mask
164 #define	sd_level_mask			ssd_level_mask
165 #define	sd_debug_un			ssd_debug_un
166 #define	sd_error_level			ssd_error_level
167 
168 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
169 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
170 
171 #define	sd_tr				ssd_tr
172 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
173 #define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
174 #define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
175 #define	sd_check_media_time		ssd_check_media_time
176 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
177 #define	sd_label_mutex			ssd_label_mutex
178 #define	sd_detach_mutex			ssd_detach_mutex
179 #define	sd_log_buf			ssd_log_buf
180 #define	sd_log_mutex			ssd_log_mutex
181 
182 #define	sd_disk_table			ssd_disk_table
183 #define	sd_disk_table_size		ssd_disk_table_size
184 #define	sd_sense_mutex			ssd_sense_mutex
185 #define	sd_cdbtab			ssd_cdbtab
186 
187 #define	sd_cb_ops			ssd_cb_ops
188 #define	sd_ops				ssd_ops
189 #define	sd_additional_codes		ssd_additional_codes
190 
191 #define	sd_minor_data			ssd_minor_data
192 #define	sd_minor_data_efi		ssd_minor_data_efi
193 
194 #define	sd_tq				ssd_tq
195 #define	sd_wmr_tq			ssd_wmr_tq
196 #define	sd_taskq_name			ssd_taskq_name
197 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
198 #define	sd_taskq_minalloc		ssd_taskq_minalloc
199 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
200 
201 #define	sd_dump_format_string		ssd_dump_format_string
202 
203 #define	sd_iostart_chain		ssd_iostart_chain
204 #define	sd_iodone_chain			ssd_iodone_chain
205 
206 #define	sd_pm_idletime			ssd_pm_idletime
207 
208 #define	sd_force_pm_supported		ssd_force_pm_supported
209 
210 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
211 
212 #endif
213 
214 
215 #ifdef	SDDEBUG
216 int	sd_force_pm_supported		= 0;
217 #endif	/* SDDEBUG */
218 
219 void *sd_state				= NULL;
220 int sd_io_time				= SD_IO_TIME;
221 int sd_failfast_enable			= 1;
222 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
223 int sd_report_pfa			= 1;
224 int sd_max_throttle			= SD_MAX_THROTTLE;
225 int sd_min_throttle			= SD_MIN_THROTTLE;
226 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
227 int sd_qfull_throttle_enable		= TRUE;
228 
229 int sd_retry_on_reservation_conflict	= 1;
230 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
231 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
232 
233 static int sd_dtype_optical_bind	= -1;
234 
235 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
236 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
237 
238 /*
239  * Global data for debug logging. To enable debug printing, sd_component_mask
240  * and sd_level_mask should be set to the desired bit patterns as outlined in
241  * sddef.h.
242  */
243 uint_t	sd_component_mask		= 0x0;
244 uint_t	sd_level_mask			= 0x0;
245 struct	sd_lun *sd_debug_un		= NULL;
246 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
247 
248 /* Note: these may go away in the future... */
249 static uint32_t	sd_xbuf_active_limit	= 512;
250 static uint32_t sd_xbuf_reserve_limit	= 16;
251 
252 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
253 
254 /*
255  * Timer value used to reset the throttle after it has been reduced
256  * (typically in response to TRAN_BUSY or STATUS_QFULL)
257  */
258 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
259 static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
260 
261 /*
262  * Interval value associated with the media change scsi watch.
263  */
264 static int sd_check_media_time		= 3000000;
265 
266 /*
267  * Wait value used for in progress operations during a DDI_SUSPEND
268  */
269 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
270 
271 /*
272  * sd_label_mutex protects a static buffer used in the disk label
273  * component of the driver
274  */
275 static kmutex_t sd_label_mutex;
276 
277 /*
278  * sd_detach_mutex protects un_layer_count, un_detach_count, and
279  * un_opens_in_progress in the sd_lun structure.
280  */
281 static kmutex_t sd_detach_mutex;
282 
283 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
284 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
285 
286 /*
287  * Global buffer and mutex for debug logging
288  */
289 static char	sd_log_buf[1024];
290 static kmutex_t	sd_log_mutex;
291 
292 
293 /*
294  * "Smart" Probe Caching structs, globals, #defines, etc.
295  * For parallel scsi and non-self-identify device only.
296  */
297 
298 /*
299  * The following resources and routines are implemented to support
300  * "smart" probing, which caches the scsi_probe() results in an array,
301  * in order to help avoid long probe times.
302  */
303 struct sd_scsi_probe_cache {
304 	struct	sd_scsi_probe_cache	*next;
305 	dev_info_t	*pdip;
306 	int		cache[NTARGETS_WIDE];
307 };
308 
309 static kmutex_t	sd_scsi_probe_cache_mutex;
310 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
311 
312 /*
313  * Really we only need protection on the head of the linked list, but
314  * better safe than sorry.
315  */
316 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
317     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
318 
319 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
320     sd_scsi_probe_cache_head))
321 
322 
323 /*
324  * Vendor specific data name property declarations
325  */
326 
327 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
328 
329 static sd_tunables seagate_properties = {
330 	SEAGATE_THROTTLE_VALUE,
331 	0,
332 	0,
333 	0,
334 	0,
335 	0,
336 	0,
337 	0,
338 	0
339 };
340 
341 
342 static sd_tunables fujitsu_properties = {
343 	FUJITSU_THROTTLE_VALUE,
344 	0,
345 	0,
346 	0,
347 	0,
348 	0,
349 	0,
350 	0,
351 	0
352 };
353 
354 static sd_tunables ibm_properties = {
355 	IBM_THROTTLE_VALUE,
356 	0,
357 	0,
358 	0,
359 	0,
360 	0,
361 	0,
362 	0,
363 	0
364 };
365 
366 static sd_tunables purple_properties = {
367 	PURPLE_THROTTLE_VALUE,
368 	0,
369 	0,
370 	PURPLE_BUSY_RETRIES,
371 	PURPLE_RESET_RETRY_COUNT,
372 	PURPLE_RESERVE_RELEASE_TIME,
373 	0,
374 	0,
375 	0
376 };
377 
378 static sd_tunables sve_properties = {
379 	SVE_THROTTLE_VALUE,
380 	0,
381 	0,
382 	SVE_BUSY_RETRIES,
383 	SVE_RESET_RETRY_COUNT,
384 	SVE_RESERVE_RELEASE_TIME,
385 	SVE_MIN_THROTTLE_VALUE,
386 	SVE_DISKSORT_DISABLED_FLAG,
387 	0
388 };
389 
390 static sd_tunables maserati_properties = {
391 	0,
392 	0,
393 	0,
394 	0,
395 	0,
396 	0,
397 	0,
398 	MASERATI_DISKSORT_DISABLED_FLAG,
399 	MASERATI_LUN_RESET_ENABLED_FLAG
400 };
401 
402 static sd_tunables pirus_properties = {
403 	PIRUS_THROTTLE_VALUE,
404 	0,
405 	PIRUS_NRR_COUNT,
406 	PIRUS_BUSY_RETRIES,
407 	PIRUS_RESET_RETRY_COUNT,
408 	0,
409 	PIRUS_MIN_THROTTLE_VALUE,
410 	PIRUS_DISKSORT_DISABLED_FLAG,
411 	PIRUS_LUN_RESET_ENABLED_FLAG
412 };
413 
414 #endif
415 
416 #if (defined(__sparc) && !defined(__fibre)) || \
417 	(defined(__i386) || defined(__amd64))
418 
419 
420 static sd_tunables elite_properties = {
421 	ELITE_THROTTLE_VALUE,
422 	0,
423 	0,
424 	0,
425 	0,
426 	0,
427 	0,
428 	0,
429 	0
430 };
431 
432 static sd_tunables st31200n_properties = {
433 	ST31200N_THROTTLE_VALUE,
434 	0,
435 	0,
436 	0,
437 	0,
438 	0,
439 	0,
440 	0,
441 	0
442 };
443 
444 #endif /* Fibre or not */
445 
446 static sd_tunables lsi_properties_scsi = {
447 	LSI_THROTTLE_VALUE,
448 	0,
449 	LSI_NOTREADY_RETRIES,
450 	0,
451 	0,
452 	0,
453 	0,
454 	0,
455 	0
456 };
457 
458 static sd_tunables symbios_properties = {
459 	SYMBIOS_THROTTLE_VALUE,
460 	0,
461 	SYMBIOS_NOTREADY_RETRIES,
462 	0,
463 	0,
464 	0,
465 	0,
466 	0,
467 	0
468 };
469 
470 static sd_tunables lsi_properties = {
471 	0,
472 	0,
473 	LSI_NOTREADY_RETRIES,
474 	0,
475 	0,
476 	0,
477 	0,
478 	0,
479 	0
480 };
481 
482 static sd_tunables lsi_oem_properties = {
483 	0,
484 	0,
485 	LSI_OEM_NOTREADY_RETRIES,
486 	0,
487 	0,
488 	0,
489 	0,
490 	0,
491 	0
492 };
493 
494 
495 
496 #if (defined(SD_PROP_TST))
497 
498 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
499 #define	SD_TST_THROTTLE_VAL	16
500 #define	SD_TST_NOTREADY_VAL	12
501 #define	SD_TST_BUSY_VAL		60
502 #define	SD_TST_RST_RETRY_VAL	36
503 #define	SD_TST_RSV_REL_TIME	60
504 
505 static sd_tunables tst_properties = {
506 	SD_TST_THROTTLE_VAL,
507 	SD_TST_CTYPE_VAL,
508 	SD_TST_NOTREADY_VAL,
509 	SD_TST_BUSY_VAL,
510 	SD_TST_RST_RETRY_VAL,
511 	SD_TST_RSV_REL_TIME,
512 	0,
513 	0,
514 	0
515 };
516 #endif
517 
518 /* This is similiar to the ANSI toupper implementation */
519 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
520 
521 /*
522  * Static Driver Configuration Table
523  *
524  * This is the table of disks which need throttle adjustment (or, perhaps
525  * something else as defined by the flags at a future time.)  device_id
526  * is a string consisting of concatenated vid (vendor), pid (product/model)
527  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
528  * the parts of the string are as defined by the sizes in the scsi_inquiry
529  * structure.  Device type is searched as far as the device_id string is
530  * defined.  Flags defines which values are to be set in the driver from the
531  * properties list.
532  *
533  * Entries below which begin and end with a "*" are a special case.
534  * These do not have a specific vendor, and the string which follows
535  * can appear anywhere in the 16 byte PID portion of the inquiry data.
536  *
537  * Entries below which begin and end with a " " (blank) are a special
538  * case. The comparison function will treat multiple consecutive blanks
539  * as equivalent to a single blank. For example, this causes a
540  * sd_disk_table entry of " NEC CDROM " to match a device's id string
541  * of  "NEC       CDROM".
542  *
543  * Note: The MD21 controller type has been obsoleted.
544  *	 ST318202F is a Legacy device
545  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
546  *	 made with an FC connection. The entries here are a legacy.
547  */
548 static sd_disk_config_t sd_disk_table[] = {
549 #if defined(__fibre) || defined(__i386) || defined(__amd64)
550 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
551 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
552 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
553 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
554 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
555 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
556 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
557 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
558 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
559 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
560 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
561 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
562 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
563 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
564 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
565 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
566 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
567 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
568 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
569 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
570 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
571 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
572 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
573 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
574 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
575 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
576 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
577 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
578 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
579 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
580 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
581 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
582 	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
583 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
584 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
585 	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
586 	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
587 	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
588 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
589 	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
590 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
591 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
592 			SD_CONF_BSET_BSY_RETRY_COUNT|
593 			SD_CONF_BSET_RST_RETRIES|
594 			SD_CONF_BSET_RSV_REL_TIME,
595 		&purple_properties },
596 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
597 		SD_CONF_BSET_BSY_RETRY_COUNT|
598 		SD_CONF_BSET_RST_RETRIES|
599 		SD_CONF_BSET_RSV_REL_TIME|
600 		SD_CONF_BSET_MIN_THROTTLE|
601 		SD_CONF_BSET_DISKSORT_DISABLED,
602 		&sve_properties },
603 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
604 			SD_CONF_BSET_BSY_RETRY_COUNT|
605 			SD_CONF_BSET_RST_RETRIES|
606 			SD_CONF_BSET_RSV_REL_TIME,
607 		&purple_properties },
608 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
609 		SD_CONF_BSET_LUN_RESET_ENABLED,
610 		&maserati_properties },
611 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
612 		SD_CONF_BSET_NRR_COUNT|
613 		SD_CONF_BSET_BSY_RETRY_COUNT|
614 		SD_CONF_BSET_RST_RETRIES|
615 		SD_CONF_BSET_MIN_THROTTLE|
616 		SD_CONF_BSET_DISKSORT_DISABLED|
617 		SD_CONF_BSET_LUN_RESET_ENABLED,
618 		&pirus_properties },
619 	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
620 		SD_CONF_BSET_NRR_COUNT|
621 		SD_CONF_BSET_BSY_RETRY_COUNT|
622 		SD_CONF_BSET_RST_RETRIES|
623 		SD_CONF_BSET_MIN_THROTTLE|
624 		SD_CONF_BSET_DISKSORT_DISABLED|
625 		SD_CONF_BSET_LUN_RESET_ENABLED,
626 		&pirus_properties },
627 	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
628 		SD_CONF_BSET_NRR_COUNT|
629 		SD_CONF_BSET_BSY_RETRY_COUNT|
630 		SD_CONF_BSET_RST_RETRIES|
631 		SD_CONF_BSET_MIN_THROTTLE|
632 		SD_CONF_BSET_DISKSORT_DISABLED|
633 		SD_CONF_BSET_LUN_RESET_ENABLED,
634 		&pirus_properties },
635 	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
636 		SD_CONF_BSET_NRR_COUNT|
637 		SD_CONF_BSET_BSY_RETRY_COUNT|
638 		SD_CONF_BSET_RST_RETRIES|
639 		SD_CONF_BSET_MIN_THROTTLE|
640 		SD_CONF_BSET_DISKSORT_DISABLED|
641 		SD_CONF_BSET_LUN_RESET_ENABLED,
642 		&pirus_properties },
643 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
644 		SD_CONF_BSET_NRR_COUNT|
645 		SD_CONF_BSET_BSY_RETRY_COUNT|
646 		SD_CONF_BSET_RST_RETRIES|
647 		SD_CONF_BSET_MIN_THROTTLE|
648 		SD_CONF_BSET_DISKSORT_DISABLED|
649 		SD_CONF_BSET_LUN_RESET_ENABLED,
650 		&pirus_properties },
651 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
652 		SD_CONF_BSET_NRR_COUNT|
653 		SD_CONF_BSET_BSY_RETRY_COUNT|
654 		SD_CONF_BSET_RST_RETRIES|
655 		SD_CONF_BSET_MIN_THROTTLE|
656 		SD_CONF_BSET_DISKSORT_DISABLED|
657 		SD_CONF_BSET_LUN_RESET_ENABLED,
658 		&pirus_properties },
659 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662 	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
663 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
664 #endif /* fibre or NON-sparc platforms */
665 #if ((defined(__sparc) && !defined(__fibre)) ||\
666 	(defined(__i386) || defined(__amd64)))
667 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
668 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
669 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
670 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
671 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
672 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
673 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
674 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
675 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
676 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
677 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
678 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
679 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
680 	    &symbios_properties },
681 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
682 	    &lsi_properties_scsi },
683 #if defined(__i386) || defined(__amd64)
684 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
685 				    | SD_CONF_BSET_READSUB_BCD
686 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
687 				    | SD_CONF_BSET_NO_READ_HEADER
688 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
689 
690 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
691 				    | SD_CONF_BSET_READSUB_BCD
692 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
693 				    | SD_CONF_BSET_NO_READ_HEADER
694 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
695 #endif /* __i386 || __amd64 */
696 #endif /* sparc NON-fibre or NON-sparc platforms */
697 
698 #if (defined(SD_PROP_TST))
699 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
700 				| SD_CONF_BSET_CTYPE
701 				| SD_CONF_BSET_NRR_COUNT
702 				| SD_CONF_BSET_FAB_DEVID
703 				| SD_CONF_BSET_NOCACHE
704 				| SD_CONF_BSET_BSY_RETRY_COUNT
705 				| SD_CONF_BSET_PLAYMSF_BCD
706 				| SD_CONF_BSET_READSUB_BCD
707 				| SD_CONF_BSET_READ_TOC_TRK_BCD
708 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
709 				| SD_CONF_BSET_NO_READ_HEADER
710 				| SD_CONF_BSET_READ_CD_XD4
711 				| SD_CONF_BSET_RST_RETRIES
712 				| SD_CONF_BSET_RSV_REL_TIME
713 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
714 #endif
715 };
716 
717 static const int sd_disk_table_size =
718 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
719 
720 
721 /*
722  * Return codes of sd_uselabel().
723  */
724 #define	SD_LABEL_IS_VALID		0
725 #define	SD_LABEL_IS_INVALID		1
726 
727 #define	SD_INTERCONNECT_PARALLEL	0
728 #define	SD_INTERCONNECT_FABRIC		1
729 #define	SD_INTERCONNECT_FIBRE		2
730 #define	SD_INTERCONNECT_SSA		3
731 #define	SD_IS_PARALLEL_SCSI(un)		\
732 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
733 
734 /*
735  * Definitions used by device id registration routines
736  */
737 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
738 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
739 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
740 #define	WD_NODE			7	/* the whole disk minor */
741 
742 static kmutex_t sd_sense_mutex = {0};
743 
744 /*
745  * Macros for updates of the driver state
746  */
747 #define	New_state(un, s)        \
748 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
749 #define	Restore_state(un)	\
750 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
751 
752 static struct sd_cdbinfo sd_cdbtab[] = {
753 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
754 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
755 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
756 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
757 };
758 
759 /*
760  * Specifies the number of seconds that must have elapsed since the last
761  * cmd. has completed for a device to be declared idle to the PM framework.
762  */
763 static int sd_pm_idletime = 1;
764 
765 /*
766  * Internal function prototypes
767  */
768 
769 #if (defined(__fibre))
770 /*
771  * These #defines are to avoid namespace collisions that occur because this
772  * code is currently used to compile two seperate driver modules: sd and ssd.
773  * All function names need to be treated this way (even if declared static)
774  * in order to allow the debugger to resolve the names properly.
775  * It is anticipated that in the near future the ssd module will be obsoleted,
776  * at which time this ugliness should go away.
777  */
778 #define	sd_log_trace			ssd_log_trace
779 #define	sd_log_info			ssd_log_info
780 #define	sd_log_err			ssd_log_err
781 #define	sdprobe				ssdprobe
782 #define	sdinfo				ssdinfo
783 #define	sd_prop_op			ssd_prop_op
784 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
785 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
786 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
787 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
788 #define	sd_spin_up_unit			ssd_spin_up_unit
789 #define	sd_enable_descr_sense		ssd_enable_descr_sense
790 #define	sd_set_mmc_caps			ssd_set_mmc_caps
791 #define	sd_read_unit_properties		ssd_read_unit_properties
792 #define	sd_process_sdconf_file		ssd_process_sdconf_file
793 #define	sd_process_sdconf_table		ssd_process_sdconf_table
794 #define	sd_sdconf_id_match		ssd_sdconf_id_match
795 #define	sd_blank_cmp			ssd_blank_cmp
796 #define	sd_chk_vers1_data		ssd_chk_vers1_data
797 #define	sd_set_vers1_properties		ssd_set_vers1_properties
798 #define	sd_validate_geometry		ssd_validate_geometry
799 
800 #if defined(_SUNOS_VTOC_16)
801 #define	sd_convert_geometry		ssd_convert_geometry
802 #endif
803 
804 #define	sd_resync_geom_caches		ssd_resync_geom_caches
805 #define	sd_read_fdisk			ssd_read_fdisk
806 #define	sd_get_physical_geometry	ssd_get_physical_geometry
807 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
808 #define	sd_update_block_info		ssd_update_block_info
809 #define	sd_swap_efi_gpt			ssd_swap_efi_gpt
810 #define	sd_swap_efi_gpe			ssd_swap_efi_gpe
811 #define	sd_validate_efi			ssd_validate_efi
812 #define	sd_use_efi			ssd_use_efi
813 #define	sd_uselabel			ssd_uselabel
814 #define	sd_build_default_label		ssd_build_default_label
815 #define	sd_has_max_chs_vals		ssd_has_max_chs_vals
816 #define	sd_inq_fill			ssd_inq_fill
817 #define	sd_register_devid		ssd_register_devid
818 #define	sd_get_devid_block		ssd_get_devid_block
819 #define	sd_get_devid			ssd_get_devid
820 #define	sd_create_devid			ssd_create_devid
821 #define	sd_write_deviceid		ssd_write_deviceid
822 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
823 #define	sd_setup_pm			ssd_setup_pm
824 #define	sd_create_pm_components		ssd_create_pm_components
825 #define	sd_ddi_suspend			ssd_ddi_suspend
826 #define	sd_ddi_pm_suspend		ssd_ddi_pm_suspend
827 #define	sd_ddi_resume			ssd_ddi_resume
828 #define	sd_ddi_pm_resume		ssd_ddi_pm_resume
829 #define	sdpower				ssdpower
830 #define	sdattach			ssdattach
831 #define	sddetach			ssddetach
832 #define	sd_unit_attach			ssd_unit_attach
833 #define	sd_unit_detach			ssd_unit_detach
834 #define	sd_set_unit_attributes		ssd_set_unit_attributes
835 #define	sd_create_minor_nodes		ssd_create_minor_nodes
836 #define	sd_create_errstats		ssd_create_errstats
837 #define	sd_set_errstats			ssd_set_errstats
838 #define	sd_set_pstats			ssd_set_pstats
839 #define	sddump				ssddump
840 #define	sd_scsi_poll			ssd_scsi_poll
841 #define	sd_send_polled_RQS		ssd_send_polled_RQS
842 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
843 #define	sd_init_event_callbacks		ssd_init_event_callbacks
844 #define	sd_event_callback		ssd_event_callback
845 #define	sd_cache_control		ssd_cache_control
846 #define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
847 #define	sd_make_device			ssd_make_device
848 #define	sdopen				ssdopen
849 #define	sdclose				ssdclose
850 #define	sd_ready_and_valid		ssd_ready_and_valid
851 #define	sdmin				ssdmin
852 #define	sdread				ssdread
853 #define	sdwrite				ssdwrite
854 #define	sdaread				ssdaread
855 #define	sdawrite			ssdawrite
856 #define	sdstrategy			ssdstrategy
857 #define	sdioctl				ssdioctl
858 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
859 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
860 #define	sd_checksum_iostart		ssd_checksum_iostart
861 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
862 #define	sd_pm_iostart			ssd_pm_iostart
863 #define	sd_core_iostart			ssd_core_iostart
864 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
865 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
866 #define	sd_checksum_iodone		ssd_checksum_iodone
867 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
868 #define	sd_pm_iodone			ssd_pm_iodone
869 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
870 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
871 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
872 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
873 #define	sd_buf_iodone			ssd_buf_iodone
874 #define	sd_uscsi_strategy		ssd_uscsi_strategy
875 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
876 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
877 #define	sd_uscsi_iodone			ssd_uscsi_iodone
878 #define	sd_xbuf_strategy		ssd_xbuf_strategy
879 #define	sd_xbuf_init			ssd_xbuf_init
880 #define	sd_pm_entry			ssd_pm_entry
881 #define	sd_pm_exit			ssd_pm_exit
882 
883 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
884 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
885 
886 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
887 #define	sdintr				ssdintr
888 #define	sd_start_cmds			ssd_start_cmds
889 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
890 #define	sd_bioclone_alloc		ssd_bioclone_alloc
891 #define	sd_bioclone_free		ssd_bioclone_free
892 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
893 #define	sd_shadow_buf_free		ssd_shadow_buf_free
894 #define	sd_print_transport_rejected_message	\
895 					ssd_print_transport_rejected_message
896 #define	sd_retry_command		ssd_retry_command
897 #define	sd_set_retry_bp			ssd_set_retry_bp
898 #define	sd_send_request_sense_command	ssd_send_request_sense_command
899 #define	sd_start_retry_command		ssd_start_retry_command
900 #define	sd_start_direct_priority_command	\
901 					ssd_start_direct_priority_command
902 #define	sd_return_failed_command	ssd_return_failed_command
903 #define	sd_return_failed_command_no_restart	\
904 					ssd_return_failed_command_no_restart
905 #define	sd_return_command		ssd_return_command
906 #define	sd_sync_with_callback		ssd_sync_with_callback
907 #define	sdrunout			ssdrunout
908 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
909 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
910 #define	sd_reduce_throttle		ssd_reduce_throttle
911 #define	sd_restore_throttle		ssd_restore_throttle
912 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
913 #define	sd_init_cdb_limits		ssd_init_cdb_limits
914 #define	sd_pkt_status_good		ssd_pkt_status_good
915 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
916 #define	sd_pkt_status_busy		ssd_pkt_status_busy
917 #define	sd_pkt_status_reservation_conflict	\
918 					ssd_pkt_status_reservation_conflict
919 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
920 #define	sd_handle_request_sense		ssd_handle_request_sense
921 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
922 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
923 #define	sd_validate_sense_data		ssd_validate_sense_data
924 #define	sd_decode_sense			ssd_decode_sense
925 #define	sd_print_sense_msg		ssd_print_sense_msg
926 #define	sd_extract_sense_info_descr	ssd_extract_sense_info_descr
927 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
928 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
929 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
930 #define	sd_sense_key_medium_or_hardware_error	\
931 					ssd_sense_key_medium_or_hardware_error
932 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
933 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
934 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
935 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
936 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
937 #define	sd_sense_key_default		ssd_sense_key_default
938 #define	sd_print_retry_msg		ssd_print_retry_msg
939 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
940 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
941 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
942 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
943 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
944 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
945 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
946 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
947 #define	sd_pkt_reason_default		ssd_pkt_reason_default
948 #define	sd_reset_target			ssd_reset_target
949 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
950 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
951 #define	sd_taskq_create			ssd_taskq_create
952 #define	sd_taskq_delete			ssd_taskq_delete
953 #define	sd_media_change_task		ssd_media_change_task
954 #define	sd_handle_mchange		ssd_handle_mchange
955 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
956 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
957 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
958 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
959 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
960 					sd_send_scsi_feature_GET_CONFIGURATION
961 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
962 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
963 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
964 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
965 					ssd_send_scsi_PERSISTENT_RESERVE_IN
966 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
967 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
968 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
969 #define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
970 					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
971 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
972 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
973 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
974 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
975 #define	sd_alloc_rqs			ssd_alloc_rqs
976 #define	sd_free_rqs			ssd_free_rqs
977 #define	sd_dump_memory			ssd_dump_memory
978 #define	sd_uscsi_ioctl			ssd_uscsi_ioctl
979 #define	sd_get_media_info		ssd_get_media_info
980 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
981 #define	sd_dkio_get_geometry		ssd_dkio_get_geometry
982 #define	sd_dkio_set_geometry		ssd_dkio_set_geometry
983 #define	sd_dkio_get_partition		ssd_dkio_get_partition
984 #define	sd_dkio_set_partition		ssd_dkio_set_partition
985 #define	sd_dkio_partition		ssd_dkio_partition
986 #define	sd_dkio_get_vtoc		ssd_dkio_get_vtoc
987 #define	sd_dkio_get_efi			ssd_dkio_get_efi
988 #define	sd_build_user_vtoc		ssd_build_user_vtoc
989 #define	sd_dkio_set_vtoc		ssd_dkio_set_vtoc
990 #define	sd_dkio_set_efi			ssd_dkio_set_efi
991 #define	sd_build_label_vtoc		ssd_build_label_vtoc
992 #define	sd_write_label			ssd_write_label
993 #define	sd_clear_vtoc			ssd_clear_vtoc
994 #define	sd_clear_efi			ssd_clear_efi
995 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
996 #define	sd_setup_next_xfer		ssd_setup_next_xfer
997 #define	sd_dkio_get_temp		ssd_dkio_get_temp
998 #define	sd_dkio_get_mboot		ssd_dkio_get_mboot
999 #define	sd_dkio_set_mboot		ssd_dkio_set_mboot
1000 #define	sd_setup_default_geometry	ssd_setup_default_geometry
1001 #define	sd_update_fdisk_and_vtoc	ssd_update_fdisk_and_vtoc
1002 #define	sd_check_mhd			ssd_check_mhd
1003 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1004 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1005 #define	sd_sname			ssd_sname
1006 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1007 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1008 #define	sd_take_ownership		ssd_take_ownership
1009 #define	sd_reserve_release		ssd_reserve_release
1010 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1011 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1012 #define	sd_persistent_reservation_in_read_keys	\
1013 					ssd_persistent_reservation_in_read_keys
1014 #define	sd_persistent_reservation_in_read_resv	\
1015 					ssd_persistent_reservation_in_read_resv
1016 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1017 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1018 #define	sd_mhdioc_release		ssd_mhdioc_release
1019 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1020 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1021 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1022 #define	sr_change_blkmode		ssr_change_blkmode
1023 #define	sr_change_speed			ssr_change_speed
1024 #define	sr_atapi_change_speed		ssr_atapi_change_speed
1025 #define	sr_pause_resume			ssr_pause_resume
1026 #define	sr_play_msf			ssr_play_msf
1027 #define	sr_play_trkind			ssr_play_trkind
1028 #define	sr_read_all_subcodes		ssr_read_all_subcodes
1029 #define	sr_read_subchannel		ssr_read_subchannel
1030 #define	sr_read_tocentry		ssr_read_tocentry
1031 #define	sr_read_tochdr			ssr_read_tochdr
1032 #define	sr_read_cdda			ssr_read_cdda
1033 #define	sr_read_cdxa			ssr_read_cdxa
1034 #define	sr_read_mode1			ssr_read_mode1
1035 #define	sr_read_mode2			ssr_read_mode2
1036 #define	sr_read_cd_mode2		ssr_read_cd_mode2
1037 #define	sr_sector_mode			ssr_sector_mode
1038 #define	sr_eject			ssr_eject
1039 #define	sr_ejected			ssr_ejected
1040 #define	sr_check_wp			ssr_check_wp
1041 #define	sd_check_media			ssd_check_media
1042 #define	sd_media_watch_cb		ssd_media_watch_cb
1043 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1044 #define	sr_volume_ctrl			ssr_volume_ctrl
1045 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1046 #define	sd_log_page_supported		ssd_log_page_supported
1047 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1048 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1049 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1050 #define	sd_range_lock			ssd_range_lock
1051 #define	sd_get_range			ssd_get_range
1052 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1053 #define	sd_range_unlock			ssd_range_unlock
1054 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1055 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1056 
1057 #define	sd_iostart_chain		ssd_iostart_chain
1058 #define	sd_iodone_chain			ssd_iodone_chain
1059 #define	sd_initpkt_map			ssd_initpkt_map
1060 #define	sd_destroypkt_map		ssd_destroypkt_map
1061 #define	sd_chain_type_map		ssd_chain_type_map
1062 #define	sd_chain_index_map		ssd_chain_index_map
1063 
1064 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1065 #define	sd_failfast_flushq		ssd_failfast_flushq
1066 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1067 
1068 #define	sd_is_lsi			ssd_is_lsi
1069 
1070 #endif	/* #if (defined(__fibre)) */
1071 
1072 
1073 int _init(void);
1074 int _fini(void);
1075 int _info(struct modinfo *modinfop);
1076 
1077 /*PRINTFLIKE3*/
1078 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1079 /*PRINTFLIKE3*/
1080 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1081 /*PRINTFLIKE3*/
1082 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1083 
1084 static int sdprobe(dev_info_t *devi);
1085 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1086     void **result);
1087 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1088     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1089 
1090 /*
1091  * Smart probe for parallel scsi
1092  */
1093 static void sd_scsi_probe_cache_init(void);
1094 static void sd_scsi_probe_cache_fini(void);
1095 static void sd_scsi_clear_probe_cache(void);
1096 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1097 
1098 static int	sd_spin_up_unit(struct sd_lun *un);
1099 #ifdef _LP64
1100 static void	sd_enable_descr_sense(struct sd_lun *un);
1101 #endif /* _LP64 */
1102 static void	sd_set_mmc_caps(struct sd_lun *un);
1103 
1104 static void sd_read_unit_properties(struct sd_lun *un);
1105 static int  sd_process_sdconf_file(struct sd_lun *un);
1106 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1107     int *data_list, sd_tunables *values);
1108 static void sd_process_sdconf_table(struct sd_lun *un);
1109 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1110 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1111 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1112 	int list_len, char *dataname_ptr);
1113 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1114     sd_tunables *prop_list);
1115 static int  sd_validate_geometry(struct sd_lun *un, int path_flag);
1116 
1117 #if defined(_SUNOS_VTOC_16)
1118 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g);
1119 #endif
1120 
1121 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize,
1122 	int path_flag);
1123 static int  sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize,
1124 	int path_flag);
1125 static void sd_get_physical_geometry(struct sd_lun *un,
1126 	struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag);
1127 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity,
1128 	int lbasize);
1129 static int  sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag);
1130 static void sd_swap_efi_gpt(efi_gpt_t *);
1131 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *);
1132 static int sd_validate_efi(efi_gpt_t *);
1133 static int sd_use_efi(struct sd_lun *, int);
1134 static void sd_build_default_label(struct sd_lun *un);
1135 
1136 #if defined(_FIRMWARE_NEEDS_FDISK)
1137 static int  sd_has_max_chs_vals(struct ipart *fdp);
1138 #endif
1139 static void sd_inq_fill(char *p, int l, char *s);
1140 
1141 
1142 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi,
1143     int reservation_flag);
1144 static daddr_t  sd_get_devid_block(struct sd_lun *un);
1145 static int  sd_get_devid(struct sd_lun *un);
1146 static int  sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len);
1147 static ddi_devid_t sd_create_devid(struct sd_lun *un);
1148 static int  sd_write_deviceid(struct sd_lun *un);
1149 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1150 static int  sd_check_vpd_page_support(struct sd_lun *un);
1151 
1152 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi);
1153 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1154 
1155 static int  sd_ddi_suspend(dev_info_t *devi);
1156 static int  sd_ddi_pm_suspend(struct sd_lun *un);
1157 static int  sd_ddi_resume(dev_info_t *devi);
1158 static int  sd_ddi_pm_resume(struct sd_lun *un);
1159 static int  sdpower(dev_info_t *devi, int component, int level);
1160 
1161 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1162 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1163 static int  sd_unit_attach(dev_info_t *devi);
1164 static int  sd_unit_detach(dev_info_t *devi);
1165 
1166 static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1167 static int  sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi);
1168 static void sd_create_errstats(struct sd_lun *un, int instance);
1169 static void sd_set_errstats(struct sd_lun *un);
1170 static void sd_set_pstats(struct sd_lun *un);
1171 
1172 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1173 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1174 static int  sd_send_polled_RQS(struct sd_lun *un);
1175 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1176 
1177 #if (defined(__fibre))
1178 /*
1179  * Event callbacks (photon)
1180  */
1181 static void sd_init_event_callbacks(struct sd_lun *un);
1182 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1183 #endif
1184 
1185 /*
1186  * Defines for sd_cache_control
1187  */
1188 
1189 #define	SD_CACHE_ENABLE		1
1190 #define	SD_CACHE_DISABLE	0
1191 #define	SD_CACHE_NOCHANGE	-1
1192 
1193 static int   sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag);
1194 static int   sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled);
1195 static dev_t sd_make_device(dev_info_t *devi);
1196 
1197 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1198 	uint64_t capacity);
1199 
1200 /*
1201  * Driver entry point functions.
1202  */
1203 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1204 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1205 static int  sd_ready_and_valid(struct sd_lun *un);
1206 
1207 static void sdmin(struct buf *bp);
1208 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1209 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1210 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1211 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1212 
1213 static int sdstrategy(struct buf *bp);
1214 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1215 
1216 /*
1217  * Function prototypes for layering functions in the iostart chain.
1218  */
1219 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1220 	struct buf *bp);
1221 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1222 	struct buf *bp);
1223 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1224 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1225 	struct buf *bp);
1226 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1227 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1228 
1229 /*
1230  * Function prototypes for layering functions in the iodone chain.
1231  */
1232 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1233 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1234 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1235 	struct buf *bp);
1236 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1237 	struct buf *bp);
1238 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1239 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1240 	struct buf *bp);
1241 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1242 
1243 /*
1244  * Prototypes for functions to support buf(9S) based IO.
1245  */
1246 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1247 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1248 static void sd_destroypkt_for_buf(struct buf *);
1249 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1250 	struct buf *bp, int flags,
1251 	int (*callback)(caddr_t), caddr_t callback_arg,
1252 	diskaddr_t lba, uint32_t blockcount);
1253 #if defined(__i386) || defined(__amd64)
1254 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1255 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1256 #endif /* defined(__i386) || defined(__amd64) */
1257 
1258 /*
1259  * Prototypes for functions to support USCSI IO.
1260  */
1261 static int sd_uscsi_strategy(struct buf *bp);
1262 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1263 static void sd_destroypkt_for_uscsi(struct buf *);
1264 
1265 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1266 	uchar_t chain_type, void *pktinfop);
1267 
1268 static int  sd_pm_entry(struct sd_lun *un);
1269 static void sd_pm_exit(struct sd_lun *un);
1270 
1271 static void sd_pm_idletimeout_handler(void *arg);
1272 
1273 /*
1274  * sd_core internal functions (used at the sd_core_io layer).
1275  */
1276 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1277 static void sdintr(struct scsi_pkt *pktp);
1278 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1279 
1280 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd,
1281 	enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace,
1282 	int path_flag);
1283 
1284 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1285 	daddr_t blkno, int (*func)(struct buf *));
1286 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1287 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1288 static void sd_bioclone_free(struct buf *bp);
1289 static void sd_shadow_buf_free(struct buf *bp);
1290 
1291 static void sd_print_transport_rejected_message(struct sd_lun *un,
1292 	struct sd_xbuf *xp, int code);
1293 static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1294     void *arg, int code);
1295 static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1296     void *arg, int code);
1297 static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1298     void *arg, int code);
1299 
1300 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1301 	int retry_check_flag,
1302 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1303 		int c),
1304 	void *user_arg, int failure_code,  clock_t retry_delay,
1305 	void (*statp)(kstat_io_t *));
1306 
1307 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1308 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1309 
1310 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1311 	struct scsi_pkt *pktp);
1312 static void sd_start_retry_command(void *arg);
1313 static void sd_start_direct_priority_command(void *arg);
1314 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1315 	int errcode);
1316 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1317 	struct buf *bp, int errcode);
1318 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1319 static void sd_sync_with_callback(struct sd_lun *un);
1320 static int sdrunout(caddr_t arg);
1321 
1322 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1323 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1324 
1325 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1326 static void sd_restore_throttle(void *arg);
1327 
1328 static void sd_init_cdb_limits(struct sd_lun *un);
1329 
1330 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1331 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1332 
1333 /*
1334  * Error handling functions
1335  */
1336 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1337 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1338 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1339 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1340 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1341 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1342 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1343 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1344 
1345 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1346 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1347 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1348 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1349 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1350 	struct sd_xbuf *xp);
1351 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1352 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1353 
1354 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1355 	void *arg, int code);
1356 static diskaddr_t sd_extract_sense_info_descr(
1357 	struct scsi_descr_sense_hdr *sdsp);
1358 
1359 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1360 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1361 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1362 	uint8_t asc,
1363 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1364 static void sd_sense_key_not_ready(struct sd_lun *un,
1365 	uint8_t asc, uint8_t ascq,
1366 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1367 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1368 	int sense_key, uint8_t asc,
1369 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1370 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1371 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1372 static void sd_sense_key_unit_attention(struct sd_lun *un,
1373 	uint8_t asc,
1374 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1375 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1376 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1377 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1378 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1379 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1380 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1381 static void sd_sense_key_default(struct sd_lun *un,
1382 	int sense_key,
1383 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1384 
1385 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1386 	void *arg, int flag);
1387 
1388 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1389 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1390 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1391 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1392 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1393 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1394 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1395 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1396 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1397 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1398 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1399 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1400 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1401 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1402 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1403 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1404 
1405 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1406 
1407 static void sd_start_stop_unit_callback(void *arg);
1408 static void sd_start_stop_unit_task(void *arg);
1409 
1410 static void sd_taskq_create(void);
1411 static void sd_taskq_delete(void);
1412 static void sd_media_change_task(void *arg);
1413 
1414 static int sd_handle_mchange(struct sd_lun *un);
1415 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag);
1416 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp,
1417 	uint32_t *lbap, int path_flag);
1418 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
1419 	uint32_t *lbap, int path_flag);
1420 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag,
1421 	int path_flag);
1422 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr,
1423 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1424 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag);
1425 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un,
1426 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1427 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un,
1428 	uchar_t usr_cmd, uchar_t *usr_bufp);
1429 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1430 	struct dk_callback *dkc);
1431 static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1432 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un,
1433 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1434 	uchar_t *bufaddr, uint_t buflen);
1435 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
1436 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1437 	uchar_t *bufaddr, uint_t buflen, char feature);
1438 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize,
1439 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1440 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize,
1441 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1442 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
1443 	size_t buflen, daddr_t start_block, int path_flag);
1444 #define	sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag)	\
1445 	sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \
1446 	path_flag)
1447 #define	sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag)	\
1448 	sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\
1449 	path_flag)
1450 
1451 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr,
1452 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1453 	uint16_t param_ptr, int path_flag);
1454 
1455 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1456 static void sd_free_rqs(struct sd_lun *un);
1457 
1458 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1459 	uchar_t *data, int len, int fmt);
1460 static void sd_panic_for_res_conflict(struct sd_lun *un);
1461 
1462 /*
1463  * Disk Ioctl Function Prototypes
1464  */
1465 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag);
1466 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1467 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1468 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag,
1469 	int geom_validated);
1470 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag);
1471 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag,
1472 	int geom_validated);
1473 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag);
1474 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag,
1475 	int geom_validated);
1476 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag);
1477 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag);
1478 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1479 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag);
1480 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag);
1481 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1482 static int sd_write_label(dev_t dev);
1483 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl);
1484 static void sd_clear_vtoc(struct sd_lun *un);
1485 static void sd_clear_efi(struct sd_lun *un);
1486 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1487 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag);
1488 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag);
1489 static void sd_setup_default_geometry(struct sd_lun *un);
1490 #if defined(__i386) || defined(__amd64)
1491 static int sd_update_fdisk_and_vtoc(struct sd_lun *un);
1492 #endif
1493 
1494 /*
1495  * Multi-host Ioctl Prototypes
1496  */
1497 static int sd_check_mhd(dev_t dev, int interval);
1498 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1499 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1500 static char *sd_sname(uchar_t status);
1501 static void sd_mhd_resvd_recover(void *arg);
1502 static void sd_resv_reclaim_thread();
1503 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1504 static int sd_reserve_release(dev_t dev, int cmd);
1505 static void sd_rmv_resv_reclaim_req(dev_t dev);
1506 static void sd_mhd_reset_notify_cb(caddr_t arg);
1507 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1508 	mhioc_inkeys_t *usrp, int flag);
1509 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1510 	mhioc_inresvs_t *usrp, int flag);
1511 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1512 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1513 static int sd_mhdioc_release(dev_t dev);
1514 static int sd_mhdioc_register_devid(dev_t dev);
1515 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1516 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1517 
1518 /*
1519  * SCSI removable prototypes
1520  */
1521 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1522 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1523 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1524 static int sr_pause_resume(dev_t dev, int mode);
1525 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1526 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1527 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1528 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1529 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1530 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1531 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1532 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1533 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1534 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1535 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1536 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1537 static int sr_eject(dev_t dev);
1538 static void sr_ejected(register struct sd_lun *un);
1539 static int sr_check_wp(dev_t dev);
1540 static int sd_check_media(dev_t dev, enum dkio_state state);
1541 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1542 static void sd_delayed_cv_broadcast(void *arg);
1543 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1544 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1545 
1546 static int sd_log_page_supported(struct sd_lun *un, int log_page);
1547 
1548 /*
1549  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1550  */
1551 static void sd_check_for_writable_cd(struct sd_lun *un);
1552 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1553 static void sd_wm_cache_destructor(void *wm, void *un);
1554 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1555 	daddr_t endb, ushort_t typ);
1556 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1557 	daddr_t endb);
1558 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1559 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1560 static void sd_read_modify_write_task(void * arg);
1561 static int
1562 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1563 	struct buf **bpp);
1564 
1565 
1566 /*
1567  * Function prototypes for failfast support.
1568  */
1569 static void sd_failfast_flushq(struct sd_lun *un);
1570 static int sd_failfast_flushq_callback(struct buf *bp);
1571 
1572 /*
1573  * Function prototypes to check for lsi devices
1574  */
1575 static void sd_is_lsi(struct sd_lun *un);
1576 
1577 /*
1578  * Function prototypes for x86 support
1579  */
1580 #if defined(__i386) || defined(__amd64)
1581 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1582 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1583 #endif
1584 
1585 /*
1586  * Constants for failfast support:
1587  *
1588  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1589  * failfast processing being performed.
1590  *
1591  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1592  * failfast processing on all bufs with B_FAILFAST set.
1593  */
1594 
1595 #define	SD_FAILFAST_INACTIVE		0
1596 #define	SD_FAILFAST_ACTIVE		1
1597 
1598 /*
1599  * Bitmask to control behavior of buf(9S) flushes when a transition to
1600  * the failfast state occurs. Optional bits include:
1601  *
1602  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1603  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1604  * be flushed.
1605  *
1606  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1607  * driver, in addition to the regular wait queue. This includes the xbuf
1608  * queues. When clear, only the driver's wait queue will be flushed.
1609  */
1610 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1611 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1612 
1613 /*
1614  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1615  * to flush all queues within the driver.
1616  */
1617 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1618 
1619 
1620 /*
1621  * SD Testing Fault Injection
1622  */
1623 #ifdef SD_FAULT_INJECTION
1624 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1625 static void sd_faultinjection(struct scsi_pkt *pktp);
1626 static void sd_injection_log(char *buf, struct sd_lun *un);
1627 #endif
1628 
1629 /*
1630  * Device driver ops vector
1631  */
1632 static struct cb_ops sd_cb_ops = {
1633 	sdopen,			/* open */
1634 	sdclose,		/* close */
1635 	sdstrategy,		/* strategy */
1636 	nodev,			/* print */
1637 	sddump,			/* dump */
1638 	sdread,			/* read */
1639 	sdwrite,		/* write */
1640 	sdioctl,		/* ioctl */
1641 	nodev,			/* devmap */
1642 	nodev,			/* mmap */
1643 	nodev,			/* segmap */
1644 	nochpoll,		/* poll */
1645 	sd_prop_op,		/* cb_prop_op */
1646 	0,			/* streamtab  */
1647 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1648 	CB_REV,			/* cb_rev */
1649 	sdaread, 		/* async I/O read entry point */
1650 	sdawrite		/* async I/O write entry point */
1651 };
1652 
1653 static struct dev_ops sd_ops = {
1654 	DEVO_REV,		/* devo_rev, */
1655 	0,			/* refcnt  */
1656 	sdinfo,			/* info */
1657 	nulldev,		/* identify */
1658 	sdprobe,		/* probe */
1659 	sdattach,		/* attach */
1660 	sddetach,		/* detach */
1661 	nodev,			/* reset */
1662 	&sd_cb_ops,		/* driver operations */
1663 	NULL,			/* bus operations */
1664 	sdpower			/* power */
1665 };
1666 
1667 
1668 /*
1669  * This is the loadable module wrapper.
1670  */
1671 #include <sys/modctl.h>
1672 
1673 static struct modldrv modldrv = {
1674 	&mod_driverops,		/* Type of module. This one is a driver */
1675 	SD_MODULE_NAME,		/* Module name. */
1676 	&sd_ops			/* driver ops */
1677 };
1678 
1679 
1680 static struct modlinkage modlinkage = {
1681 	MODREV_1,
1682 	&modldrv,
1683 	NULL
1684 };
1685 
1686 
1687 static struct scsi_asq_key_strings sd_additional_codes[] = {
1688 	0x81, 0, "Logical Unit is Reserved",
1689 	0x85, 0, "Audio Address Not Valid",
1690 	0xb6, 0, "Media Load Mechanism Failed",
1691 	0xB9, 0, "Audio Play Operation Aborted",
1692 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1693 	0x53, 2, "Medium removal prevented",
1694 	0x6f, 0, "Authentication failed during key exchange",
1695 	0x6f, 1, "Key not present",
1696 	0x6f, 2, "Key not established",
1697 	0x6f, 3, "Read without proper authentication",
1698 	0x6f, 4, "Mismatched region to this logical unit",
1699 	0x6f, 5, "Region reset count error",
1700 	0xffff, 0x0, NULL
1701 };
1702 
1703 
1704 /*
1705  * Struct for passing printing information for sense data messages
1706  */
1707 struct sd_sense_info {
1708 	int	ssi_severity;
1709 	int	ssi_pfa_flag;
1710 };
1711 
1712 /*
1713  * Table of function pointers for iostart-side routines. Seperate "chains"
1714  * of layered function calls are formed by placing the function pointers
1715  * sequentially in the desired order. Functions are called according to an
1716  * incrementing table index ordering. The last function in each chain must
1717  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1718  * in the sd_iodone_chain[] array.
1719  *
1720  * Note: It may seem more natural to organize both the iostart and iodone
1721  * functions together, into an array of structures (or some similar
1722  * organization) with a common index, rather than two seperate arrays which
1723  * must be maintained in synchronization. The purpose of this division is
1724  * to achiece improved performance: individual arrays allows for more
1725  * effective cache line utilization on certain platforms.
1726  */
1727 
1728 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1729 
1730 
1731 static sd_chain_t sd_iostart_chain[] = {
1732 
1733 	/* Chain for buf IO for disk drive targets (PM enabled) */
1734 	sd_mapblockaddr_iostart,	/* Index: 0 */
1735 	sd_pm_iostart,			/* Index: 1 */
1736 	sd_core_iostart,		/* Index: 2 */
1737 
1738 	/* Chain for buf IO for disk drive targets (PM disabled) */
1739 	sd_mapblockaddr_iostart,	/* Index: 3 */
1740 	sd_core_iostart,		/* Index: 4 */
1741 
1742 	/* Chain for buf IO for removable-media targets (PM enabled) */
1743 	sd_mapblockaddr_iostart,	/* Index: 5 */
1744 	sd_mapblocksize_iostart,	/* Index: 6 */
1745 	sd_pm_iostart,			/* Index: 7 */
1746 	sd_core_iostart,		/* Index: 8 */
1747 
1748 	/* Chain for buf IO for removable-media targets (PM disabled) */
1749 	sd_mapblockaddr_iostart,	/* Index: 9 */
1750 	sd_mapblocksize_iostart,	/* Index: 10 */
1751 	sd_core_iostart,		/* Index: 11 */
1752 
1753 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1754 	sd_mapblockaddr_iostart,	/* Index: 12 */
1755 	sd_checksum_iostart,		/* Index: 13 */
1756 	sd_pm_iostart,			/* Index: 14 */
1757 	sd_core_iostart,		/* Index: 15 */
1758 
1759 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1760 	sd_mapblockaddr_iostart,	/* Index: 16 */
1761 	sd_checksum_iostart,		/* Index: 17 */
1762 	sd_core_iostart,		/* Index: 18 */
1763 
1764 	/* Chain for USCSI commands (all targets) */
1765 	sd_pm_iostart,			/* Index: 19 */
1766 	sd_core_iostart,		/* Index: 20 */
1767 
1768 	/* Chain for checksumming USCSI commands (all targets) */
1769 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1770 	sd_pm_iostart,			/* Index: 22 */
1771 	sd_core_iostart,		/* Index: 23 */
1772 
1773 	/* Chain for "direct" USCSI commands (all targets) */
1774 	sd_core_iostart,		/* Index: 24 */
1775 
1776 	/* Chain for "direct priority" USCSI commands (all targets) */
1777 	sd_core_iostart,		/* Index: 25 */
1778 };
1779 
1780 /*
1781  * Macros to locate the first function of each iostart chain in the
1782  * sd_iostart_chain[] array. These are located by the index in the array.
1783  */
1784 #define	SD_CHAIN_DISK_IOSTART			0
1785 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1786 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1787 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1788 #define	SD_CHAIN_CHKSUM_IOSTART			12
1789 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1790 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1791 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1792 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1793 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1794 
1795 
1796 /*
1797  * Table of function pointers for the iodone-side routines for the driver-
1798  * internal layering mechanism.  The calling sequence for iodone routines
1799  * uses a decrementing table index, so the last routine called in a chain
1800  * must be at the lowest array index location for that chain.  The last
1801  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1802  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1803  * of the functions in an iodone side chain must correspond to the ordering
1804  * of the iostart routines for that chain.  Note that there is no iodone
1805  * side routine that corresponds to sd_core_iostart(), so there is no
1806  * entry in the table for this.
1807  */
1808 
1809 static sd_chain_t sd_iodone_chain[] = {
1810 
1811 	/* Chain for buf IO for disk drive targets (PM enabled) */
1812 	sd_buf_iodone,			/* Index: 0 */
1813 	sd_mapblockaddr_iodone,		/* Index: 1 */
1814 	sd_pm_iodone,			/* Index: 2 */
1815 
1816 	/* Chain for buf IO for disk drive targets (PM disabled) */
1817 	sd_buf_iodone,			/* Index: 3 */
1818 	sd_mapblockaddr_iodone,		/* Index: 4 */
1819 
1820 	/* Chain for buf IO for removable-media targets (PM enabled) */
1821 	sd_buf_iodone,			/* Index: 5 */
1822 	sd_mapblockaddr_iodone,		/* Index: 6 */
1823 	sd_mapblocksize_iodone,		/* Index: 7 */
1824 	sd_pm_iodone,			/* Index: 8 */
1825 
1826 	/* Chain for buf IO for removable-media targets (PM disabled) */
1827 	sd_buf_iodone,			/* Index: 9 */
1828 	sd_mapblockaddr_iodone,		/* Index: 10 */
1829 	sd_mapblocksize_iodone,		/* Index: 11 */
1830 
1831 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1832 	sd_buf_iodone,			/* Index: 12 */
1833 	sd_mapblockaddr_iodone,		/* Index: 13 */
1834 	sd_checksum_iodone,		/* Index: 14 */
1835 	sd_pm_iodone,			/* Index: 15 */
1836 
1837 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1838 	sd_buf_iodone,			/* Index: 16 */
1839 	sd_mapblockaddr_iodone,		/* Index: 17 */
1840 	sd_checksum_iodone,		/* Index: 18 */
1841 
1842 	/* Chain for USCSI commands (non-checksum targets) */
1843 	sd_uscsi_iodone,		/* Index: 19 */
1844 	sd_pm_iodone,			/* Index: 20 */
1845 
1846 	/* Chain for USCSI commands (checksum targets) */
1847 	sd_uscsi_iodone,		/* Index: 21 */
1848 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1849 	sd_pm_iodone,			/* Index: 22 */
1850 
1851 	/* Chain for "direct" USCSI commands (all targets) */
1852 	sd_uscsi_iodone,		/* Index: 24 */
1853 
1854 	/* Chain for "direct priority" USCSI commands (all targets) */
1855 	sd_uscsi_iodone,		/* Index: 25 */
1856 };
1857 
1858 
1859 /*
1860  * Macros to locate the "first" function in the sd_iodone_chain[] array for
1861  * each iodone-side chain. These are located by the array index, but as the
1862  * iodone side functions are called in a decrementing-index order, the
1863  * highest index number in each chain must be specified (as these correspond
1864  * to the first function in the iodone chain that will be called by the core
1865  * at IO completion time).
1866  */
1867 
1868 #define	SD_CHAIN_DISK_IODONE			2
1869 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
1870 #define	SD_CHAIN_RMMEDIA_IODONE			8
1871 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
1872 #define	SD_CHAIN_CHKSUM_IODONE			15
1873 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
1874 #define	SD_CHAIN_USCSI_CMD_IODONE		20
1875 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
1876 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
1877 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
1878 
1879 
1880 
1881 
1882 /*
1883  * Array to map a layering chain index to the appropriate initpkt routine.
1884  * The redundant entries are present so that the index used for accessing
1885  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1886  * with this table as well.
1887  */
1888 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
1889 
1890 static sd_initpkt_t	sd_initpkt_map[] = {
1891 
1892 	/* Chain for buf IO for disk drive targets (PM enabled) */
1893 	sd_initpkt_for_buf,		/* Index: 0 */
1894 	sd_initpkt_for_buf,		/* Index: 1 */
1895 	sd_initpkt_for_buf,		/* Index: 2 */
1896 
1897 	/* Chain for buf IO for disk drive targets (PM disabled) */
1898 	sd_initpkt_for_buf,		/* Index: 3 */
1899 	sd_initpkt_for_buf,		/* Index: 4 */
1900 
1901 	/* Chain for buf IO for removable-media targets (PM enabled) */
1902 	sd_initpkt_for_buf,		/* Index: 5 */
1903 	sd_initpkt_for_buf,		/* Index: 6 */
1904 	sd_initpkt_for_buf,		/* Index: 7 */
1905 	sd_initpkt_for_buf,		/* Index: 8 */
1906 
1907 	/* Chain for buf IO for removable-media targets (PM disabled) */
1908 	sd_initpkt_for_buf,		/* Index: 9 */
1909 	sd_initpkt_for_buf,		/* Index: 10 */
1910 	sd_initpkt_for_buf,		/* Index: 11 */
1911 
1912 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1913 	sd_initpkt_for_buf,		/* Index: 12 */
1914 	sd_initpkt_for_buf,		/* Index: 13 */
1915 	sd_initpkt_for_buf,		/* Index: 14 */
1916 	sd_initpkt_for_buf,		/* Index: 15 */
1917 
1918 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1919 	sd_initpkt_for_buf,		/* Index: 16 */
1920 	sd_initpkt_for_buf,		/* Index: 17 */
1921 	sd_initpkt_for_buf,		/* Index: 18 */
1922 
1923 	/* Chain for USCSI commands (non-checksum targets) */
1924 	sd_initpkt_for_uscsi,		/* Index: 19 */
1925 	sd_initpkt_for_uscsi,		/* Index: 20 */
1926 
1927 	/* Chain for USCSI commands (checksum targets) */
1928 	sd_initpkt_for_uscsi,		/* Index: 21 */
1929 	sd_initpkt_for_uscsi,		/* Index: 22 */
1930 	sd_initpkt_for_uscsi,		/* Index: 22 */
1931 
1932 	/* Chain for "direct" USCSI commands (all targets) */
1933 	sd_initpkt_for_uscsi,		/* Index: 24 */
1934 
1935 	/* Chain for "direct priority" USCSI commands (all targets) */
1936 	sd_initpkt_for_uscsi,		/* Index: 25 */
1937 
1938 };
1939 
1940 
1941 /*
1942  * Array to map a layering chain index to the appropriate destroypktpkt routine.
1943  * The redundant entries are present so that the index used for accessing
1944  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1945  * with this table as well.
1946  */
1947 typedef void (*sd_destroypkt_t)(struct buf *);
1948 
1949 static sd_destroypkt_t	sd_destroypkt_map[] = {
1950 
1951 	/* Chain for buf IO for disk drive targets (PM enabled) */
1952 	sd_destroypkt_for_buf,		/* Index: 0 */
1953 	sd_destroypkt_for_buf,		/* Index: 1 */
1954 	sd_destroypkt_for_buf,		/* Index: 2 */
1955 
1956 	/* Chain for buf IO for disk drive targets (PM disabled) */
1957 	sd_destroypkt_for_buf,		/* Index: 3 */
1958 	sd_destroypkt_for_buf,		/* Index: 4 */
1959 
1960 	/* Chain for buf IO for removable-media targets (PM enabled) */
1961 	sd_destroypkt_for_buf,		/* Index: 5 */
1962 	sd_destroypkt_for_buf,		/* Index: 6 */
1963 	sd_destroypkt_for_buf,		/* Index: 7 */
1964 	sd_destroypkt_for_buf,		/* Index: 8 */
1965 
1966 	/* Chain for buf IO for removable-media targets (PM disabled) */
1967 	sd_destroypkt_for_buf,		/* Index: 9 */
1968 	sd_destroypkt_for_buf,		/* Index: 10 */
1969 	sd_destroypkt_for_buf,		/* Index: 11 */
1970 
1971 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1972 	sd_destroypkt_for_buf,		/* Index: 12 */
1973 	sd_destroypkt_for_buf,		/* Index: 13 */
1974 	sd_destroypkt_for_buf,		/* Index: 14 */
1975 	sd_destroypkt_for_buf,		/* Index: 15 */
1976 
1977 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1978 	sd_destroypkt_for_buf,		/* Index: 16 */
1979 	sd_destroypkt_for_buf,		/* Index: 17 */
1980 	sd_destroypkt_for_buf,		/* Index: 18 */
1981 
1982 	/* Chain for USCSI commands (non-checksum targets) */
1983 	sd_destroypkt_for_uscsi,	/* Index: 19 */
1984 	sd_destroypkt_for_uscsi,	/* Index: 20 */
1985 
1986 	/* Chain for USCSI commands (checksum targets) */
1987 	sd_destroypkt_for_uscsi,	/* Index: 21 */
1988 	sd_destroypkt_for_uscsi,	/* Index: 22 */
1989 	sd_destroypkt_for_uscsi,	/* Index: 22 */
1990 
1991 	/* Chain for "direct" USCSI commands (all targets) */
1992 	sd_destroypkt_for_uscsi,	/* Index: 24 */
1993 
1994 	/* Chain for "direct priority" USCSI commands (all targets) */
1995 	sd_destroypkt_for_uscsi,	/* Index: 25 */
1996 
1997 };
1998 
1999 
2000 
2001 /*
2002  * Array to map a layering chain index to the appropriate chain "type".
2003  * The chain type indicates a specific property/usage of the chain.
2004  * The redundant entries are present so that the index used for accessing
2005  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2006  * with this table as well.
2007  */
2008 
2009 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2010 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2011 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2012 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2013 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2014 						/* (for error recovery) */
2015 
2016 static int sd_chain_type_map[] = {
2017 
2018 	/* Chain for buf IO for disk drive targets (PM enabled) */
2019 	SD_CHAIN_BUFIO,			/* Index: 0 */
2020 	SD_CHAIN_BUFIO,			/* Index: 1 */
2021 	SD_CHAIN_BUFIO,			/* Index: 2 */
2022 
2023 	/* Chain for buf IO for disk drive targets (PM disabled) */
2024 	SD_CHAIN_BUFIO,			/* Index: 3 */
2025 	SD_CHAIN_BUFIO,			/* Index: 4 */
2026 
2027 	/* Chain for buf IO for removable-media targets (PM enabled) */
2028 	SD_CHAIN_BUFIO,			/* Index: 5 */
2029 	SD_CHAIN_BUFIO,			/* Index: 6 */
2030 	SD_CHAIN_BUFIO,			/* Index: 7 */
2031 	SD_CHAIN_BUFIO,			/* Index: 8 */
2032 
2033 	/* Chain for buf IO for removable-media targets (PM disabled) */
2034 	SD_CHAIN_BUFIO,			/* Index: 9 */
2035 	SD_CHAIN_BUFIO,			/* Index: 10 */
2036 	SD_CHAIN_BUFIO,			/* Index: 11 */
2037 
2038 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2039 	SD_CHAIN_BUFIO,			/* Index: 12 */
2040 	SD_CHAIN_BUFIO,			/* Index: 13 */
2041 	SD_CHAIN_BUFIO,			/* Index: 14 */
2042 	SD_CHAIN_BUFIO,			/* Index: 15 */
2043 
2044 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2045 	SD_CHAIN_BUFIO,			/* Index: 16 */
2046 	SD_CHAIN_BUFIO,			/* Index: 17 */
2047 	SD_CHAIN_BUFIO,			/* Index: 18 */
2048 
2049 	/* Chain for USCSI commands (non-checksum targets) */
2050 	SD_CHAIN_USCSI,			/* Index: 19 */
2051 	SD_CHAIN_USCSI,			/* Index: 20 */
2052 
2053 	/* Chain for USCSI commands (checksum targets) */
2054 	SD_CHAIN_USCSI,			/* Index: 21 */
2055 	SD_CHAIN_USCSI,			/* Index: 22 */
2056 	SD_CHAIN_USCSI,			/* Index: 22 */
2057 
2058 	/* Chain for "direct" USCSI commands (all targets) */
2059 	SD_CHAIN_DIRECT,		/* Index: 24 */
2060 
2061 	/* Chain for "direct priority" USCSI commands (all targets) */
2062 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2063 };
2064 
2065 
2066 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2067 #define	SD_IS_BUFIO(xp)			\
2068 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2069 
2070 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2071 #define	SD_IS_DIRECT_PRIORITY(xp)	\
2072 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2073 
2074 
2075 
2076 /*
2077  * Struct, array, and macros to map a specific chain to the appropriate
2078  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2079  *
2080  * The sd_chain_index_map[] array is used at attach time to set the various
2081  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2082  * chain to be used with the instance. This allows different instances to use
2083  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2084  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2085  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2086  * dynamically & without the use of locking; and (2) a layer may update the
2087  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2088  * to allow for deferred processing of an IO within the same chain from a
2089  * different execution context.
2090  */
2091 
2092 struct sd_chain_index {
2093 	int	sci_iostart_index;
2094 	int	sci_iodone_index;
2095 };
2096 
2097 static struct sd_chain_index	sd_chain_index_map[] = {
2098 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2099 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2100 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2101 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2102 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2103 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2104 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2105 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2106 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2107 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2108 };
2109 
2110 
2111 /*
2112  * The following are indexes into the sd_chain_index_map[] array.
2113  */
2114 
2115 /* un->un_buf_chain_type must be set to one of these */
2116 #define	SD_CHAIN_INFO_DISK		0
2117 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2118 #define	SD_CHAIN_INFO_RMMEDIA		2
2119 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2120 #define	SD_CHAIN_INFO_CHKSUM		4
2121 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2122 
2123 /* un->un_uscsi_chain_type must be set to one of these */
2124 #define	SD_CHAIN_INFO_USCSI_CMD		6
2125 /* USCSI with PM disabled is the same as DIRECT */
2126 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2127 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2128 
2129 /* un->un_direct_chain_type must be set to one of these */
2130 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2131 
2132 /* un->un_priority_chain_type must be set to one of these */
2133 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2134 
2135 /* size for devid inquiries */
2136 #define	MAX_INQUIRY_SIZE		0xF0
2137 
2138 /*
2139  * Macros used by functions to pass a given buf(9S) struct along to the
2140  * next function in the layering chain for further processing.
2141  *
2142  * In the following macros, passing more than three arguments to the called
2143  * routines causes the optimizer for the SPARC compiler to stop doing tail
2144  * call elimination which results in significant performance degradation.
2145  */
2146 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2147 	((*(sd_iostart_chain[index]))(index, un, bp))
2148 
2149 #define	SD_BEGIN_IODONE(index, un, bp)	\
2150 	((*(sd_iodone_chain[index]))(index, un, bp))
2151 
2152 #define	SD_NEXT_IOSTART(index, un, bp)				\
2153 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2154 
2155 #define	SD_NEXT_IODONE(index, un, bp)				\
2156 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2157 
2158 /*
2159  *    Function: _init
2160  *
2161  * Description: This is the driver _init(9E) entry point.
2162  *
2163  * Return Code: Returns the value from mod_install(9F) or
2164  *		ddi_soft_state_init(9F) as appropriate.
2165  *
2166  *     Context: Called when driver module loaded.
2167  */
2168 
2169 int
2170 _init(void)
2171 {
2172 	int	err;
2173 
2174 	/* establish driver name from module name */
2175 	sd_label = mod_modname(&modlinkage);
2176 
2177 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2178 		SD_MAXUNIT);
2179 
2180 	if (err != 0) {
2181 		return (err);
2182 	}
2183 
2184 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2185 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2186 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2187 
2188 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2189 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2190 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2191 
2192 	/*
2193 	 * it's ok to init here even for fibre device
2194 	 */
2195 	sd_scsi_probe_cache_init();
2196 
2197 	/*
2198 	 * Creating taskq before mod_install ensures that all callers (threads)
2199 	 * that enter the module after a successfull mod_install encounter
2200 	 * a valid taskq.
2201 	 */
2202 	sd_taskq_create();
2203 
2204 	err = mod_install(&modlinkage);
2205 	if (err != 0) {
2206 		/* delete taskq if install fails */
2207 		sd_taskq_delete();
2208 
2209 		mutex_destroy(&sd_detach_mutex);
2210 		mutex_destroy(&sd_log_mutex);
2211 		mutex_destroy(&sd_label_mutex);
2212 
2213 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2214 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2215 		cv_destroy(&sd_tr.srq_inprocess_cv);
2216 
2217 		sd_scsi_probe_cache_fini();
2218 
2219 		ddi_soft_state_fini(&sd_state);
2220 		return (err);
2221 	}
2222 
2223 	return (err);
2224 }
2225 
2226 
2227 /*
2228  *    Function: _fini
2229  *
2230  * Description: This is the driver _fini(9E) entry point.
2231  *
2232  * Return Code: Returns the value from mod_remove(9F)
2233  *
2234  *     Context: Called when driver module is unloaded.
2235  */
2236 
2237 int
2238 _fini(void)
2239 {
2240 	int err;
2241 
2242 	if ((err = mod_remove(&modlinkage)) != 0) {
2243 		return (err);
2244 	}
2245 
2246 	sd_taskq_delete();
2247 
2248 	mutex_destroy(&sd_detach_mutex);
2249 	mutex_destroy(&sd_log_mutex);
2250 	mutex_destroy(&sd_label_mutex);
2251 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2252 
2253 	sd_scsi_probe_cache_fini();
2254 
2255 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2256 	cv_destroy(&sd_tr.srq_inprocess_cv);
2257 
2258 	ddi_soft_state_fini(&sd_state);
2259 
2260 	return (err);
2261 }
2262 
2263 
2264 /*
2265  *    Function: _info
2266  *
2267  * Description: This is the driver _info(9E) entry point.
2268  *
2269  *   Arguments: modinfop - pointer to the driver modinfo structure
2270  *
2271  * Return Code: Returns the value from mod_info(9F).
2272  *
2273  *     Context: Kernel thread context
2274  */
2275 
2276 int
2277 _info(struct modinfo *modinfop)
2278 {
2279 	return (mod_info(&modlinkage, modinfop));
2280 }
2281 
2282 
2283 /*
2284  * The following routines implement the driver message logging facility.
2285  * They provide component- and level- based debug output filtering.
2286  * Output may also be restricted to messages for a single instance by
2287  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2288  * to NULL, then messages for all instances are printed.
2289  *
2290  * These routines have been cloned from each other due to the language
2291  * constraints of macros and variable argument list processing.
2292  */
2293 
2294 
2295 /*
2296  *    Function: sd_log_err
2297  *
2298  * Description: This routine is called by the SD_ERROR macro for debug
2299  *		logging of error conditions.
2300  *
2301  *   Arguments: comp - driver component being logged
2302  *		dev  - pointer to driver info structure
2303  *		fmt  - error string and format to be logged
2304  */
2305 
2306 static void
2307 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2308 {
2309 	va_list		ap;
2310 	dev_info_t	*dev;
2311 
2312 	ASSERT(un != NULL);
2313 	dev = SD_DEVINFO(un);
2314 	ASSERT(dev != NULL);
2315 
2316 	/*
2317 	 * Filter messages based on the global component and level masks.
2318 	 * Also print if un matches the value of sd_debug_un, or if
2319 	 * sd_debug_un is set to NULL.
2320 	 */
2321 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2322 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2323 		mutex_enter(&sd_log_mutex);
2324 		va_start(ap, fmt);
2325 		(void) vsprintf(sd_log_buf, fmt, ap);
2326 		va_end(ap);
2327 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2328 		mutex_exit(&sd_log_mutex);
2329 	}
2330 #ifdef SD_FAULT_INJECTION
2331 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2332 	if (un->sd_injection_mask & comp) {
2333 		mutex_enter(&sd_log_mutex);
2334 		va_start(ap, fmt);
2335 		(void) vsprintf(sd_log_buf, fmt, ap);
2336 		va_end(ap);
2337 		sd_injection_log(sd_log_buf, un);
2338 		mutex_exit(&sd_log_mutex);
2339 	}
2340 #endif
2341 }
2342 
2343 
2344 /*
2345  *    Function: sd_log_info
2346  *
2347  * Description: This routine is called by the SD_INFO macro for debug
2348  *		logging of general purpose informational conditions.
2349  *
2350  *   Arguments: comp - driver component being logged
2351  *		dev  - pointer to driver info structure
2352  *		fmt  - info string and format to be logged
2353  */
2354 
2355 static void
2356 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2357 {
2358 	va_list		ap;
2359 	dev_info_t	*dev;
2360 
2361 	ASSERT(un != NULL);
2362 	dev = SD_DEVINFO(un);
2363 	ASSERT(dev != NULL);
2364 
2365 	/*
2366 	 * Filter messages based on the global component and level masks.
2367 	 * Also print if un matches the value of sd_debug_un, or if
2368 	 * sd_debug_un is set to NULL.
2369 	 */
2370 	if ((sd_component_mask & component) &&
2371 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2372 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2373 		mutex_enter(&sd_log_mutex);
2374 		va_start(ap, fmt);
2375 		(void) vsprintf(sd_log_buf, fmt, ap);
2376 		va_end(ap);
2377 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2378 		mutex_exit(&sd_log_mutex);
2379 	}
2380 #ifdef SD_FAULT_INJECTION
2381 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2382 	if (un->sd_injection_mask & component) {
2383 		mutex_enter(&sd_log_mutex);
2384 		va_start(ap, fmt);
2385 		(void) vsprintf(sd_log_buf, fmt, ap);
2386 		va_end(ap);
2387 		sd_injection_log(sd_log_buf, un);
2388 		mutex_exit(&sd_log_mutex);
2389 	}
2390 #endif
2391 }
2392 
2393 
2394 /*
2395  *    Function: sd_log_trace
2396  *
2397  * Description: This routine is called by the SD_TRACE macro for debug
2398  *		logging of trace conditions (i.e. function entry/exit).
2399  *
2400  *   Arguments: comp - driver component being logged
2401  *		dev  - pointer to driver info structure
2402  *		fmt  - trace string and format to be logged
2403  */
2404 
2405 static void
2406 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2407 {
2408 	va_list		ap;
2409 	dev_info_t	*dev;
2410 
2411 	ASSERT(un != NULL);
2412 	dev = SD_DEVINFO(un);
2413 	ASSERT(dev != NULL);
2414 
2415 	/*
2416 	 * Filter messages based on the global component and level masks.
2417 	 * Also print if un matches the value of sd_debug_un, or if
2418 	 * sd_debug_un is set to NULL.
2419 	 */
2420 	if ((sd_component_mask & component) &&
2421 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2422 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2423 		mutex_enter(&sd_log_mutex);
2424 		va_start(ap, fmt);
2425 		(void) vsprintf(sd_log_buf, fmt, ap);
2426 		va_end(ap);
2427 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2428 		mutex_exit(&sd_log_mutex);
2429 	}
2430 #ifdef SD_FAULT_INJECTION
2431 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2432 	if (un->sd_injection_mask & component) {
2433 		mutex_enter(&sd_log_mutex);
2434 		va_start(ap, fmt);
2435 		(void) vsprintf(sd_log_buf, fmt, ap);
2436 		va_end(ap);
2437 		sd_injection_log(sd_log_buf, un);
2438 		mutex_exit(&sd_log_mutex);
2439 	}
2440 #endif
2441 }
2442 
2443 
2444 /*
2445  *    Function: sdprobe
2446  *
2447  * Description: This is the driver probe(9e) entry point function.
2448  *
2449  *   Arguments: devi - opaque device info handle
2450  *
2451  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2452  *              DDI_PROBE_FAILURE: If the probe failed.
2453  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2454  *				   but may be present in the future.
2455  */
2456 
2457 static int
2458 sdprobe(dev_info_t *devi)
2459 {
2460 	struct scsi_device	*devp;
2461 	int			rval;
2462 	int			instance;
2463 
2464 	/*
2465 	 * if it wasn't for pln, sdprobe could actually be nulldev
2466 	 * in the "__fibre" case.
2467 	 */
2468 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2469 		return (DDI_PROBE_DONTCARE);
2470 	}
2471 
2472 	devp = ddi_get_driver_private(devi);
2473 
2474 	if (devp == NULL) {
2475 		/* Ooops... nexus driver is mis-configured... */
2476 		return (DDI_PROBE_FAILURE);
2477 	}
2478 
2479 	instance = ddi_get_instance(devi);
2480 
2481 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2482 		return (DDI_PROBE_PARTIAL);
2483 	}
2484 
2485 	/*
2486 	 * Call the SCSA utility probe routine to see if we actually
2487 	 * have a target at this SCSI nexus.
2488 	 */
2489 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2490 	case SCSIPROBE_EXISTS:
2491 		switch (devp->sd_inq->inq_dtype) {
2492 		case DTYPE_DIRECT:
2493 			rval = DDI_PROBE_SUCCESS;
2494 			break;
2495 		case DTYPE_RODIRECT:
2496 			/* CDs etc. Can be removable media */
2497 			rval = DDI_PROBE_SUCCESS;
2498 			break;
2499 		case DTYPE_OPTICAL:
2500 			/*
2501 			 * Rewritable optical driver HP115AA
2502 			 * Can also be removable media
2503 			 */
2504 
2505 			/*
2506 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2507 			 * pre solaris 9 sparc sd behavior is required
2508 			 *
2509 			 * If first time through and sd_dtype_optical_bind
2510 			 * has not been set in /etc/system check properties
2511 			 */
2512 
2513 			if (sd_dtype_optical_bind  < 0) {
2514 			    sd_dtype_optical_bind = ddi_prop_get_int
2515 				(DDI_DEV_T_ANY,	devi,	0,
2516 				"optical-device-bind",	1);
2517 			}
2518 
2519 			if (sd_dtype_optical_bind == 0) {
2520 				rval = DDI_PROBE_FAILURE;
2521 			} else {
2522 				rval = DDI_PROBE_SUCCESS;
2523 			}
2524 			break;
2525 
2526 		case DTYPE_NOTPRESENT:
2527 		default:
2528 			rval = DDI_PROBE_FAILURE;
2529 			break;
2530 		}
2531 		break;
2532 	default:
2533 		rval = DDI_PROBE_PARTIAL;
2534 		break;
2535 	}
2536 
2537 	/*
2538 	 * This routine checks for resource allocation prior to freeing,
2539 	 * so it will take care of the "smart probing" case where a
2540 	 * scsi_probe() may or may not have been issued and will *not*
2541 	 * free previously-freed resources.
2542 	 */
2543 	scsi_unprobe(devp);
2544 	return (rval);
2545 }
2546 
2547 
2548 /*
2549  *    Function: sdinfo
2550  *
2551  * Description: This is the driver getinfo(9e) entry point function.
2552  * 		Given the device number, return the devinfo pointer from
2553  *		the scsi_device structure or the instance number
2554  *		associated with the dev_t.
2555  *
2556  *   Arguments: dip     - pointer to device info structure
2557  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2558  *			  DDI_INFO_DEVT2INSTANCE)
2559  *		arg     - driver dev_t
2560  *		resultp - user buffer for request response
2561  *
2562  * Return Code: DDI_SUCCESS
2563  *              DDI_FAILURE
2564  */
2565 /* ARGSUSED */
2566 static int
2567 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2568 {
2569 	struct sd_lun	*un;
2570 	dev_t		dev;
2571 	int		instance;
2572 	int		error;
2573 
2574 	switch (infocmd) {
2575 	case DDI_INFO_DEVT2DEVINFO:
2576 		dev = (dev_t)arg;
2577 		instance = SDUNIT(dev);
2578 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2579 			return (DDI_FAILURE);
2580 		}
2581 		*result = (void *) SD_DEVINFO(un);
2582 		error = DDI_SUCCESS;
2583 		break;
2584 	case DDI_INFO_DEVT2INSTANCE:
2585 		dev = (dev_t)arg;
2586 		instance = SDUNIT(dev);
2587 		*result = (void *)(uintptr_t)instance;
2588 		error = DDI_SUCCESS;
2589 		break;
2590 	default:
2591 		error = DDI_FAILURE;
2592 	}
2593 	return (error);
2594 }
2595 
2596 /*
2597  *    Function: sd_prop_op
2598  *
2599  * Description: This is the driver prop_op(9e) entry point function.
2600  *		Return the number of blocks for the partition in question
2601  *		or forward the request to the property facilities.
2602  *
2603  *   Arguments: dev       - device number
2604  *		dip       - pointer to device info structure
2605  *		prop_op   - property operator
2606  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2607  *		name      - pointer to property name
2608  *		valuep    - pointer or address of the user buffer
2609  *		lengthp   - property length
2610  *
2611  * Return Code: DDI_PROP_SUCCESS
2612  *              DDI_PROP_NOT_FOUND
2613  *              DDI_PROP_UNDEFINED
2614  *              DDI_PROP_NO_MEMORY
2615  *              DDI_PROP_BUF_TOO_SMALL
2616  */
2617 
2618 static int
2619 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2620 	char *name, caddr_t valuep, int *lengthp)
2621 {
2622 	int		instance = ddi_get_instance(dip);
2623 	struct sd_lun	*un;
2624 	uint64_t	nblocks64;
2625 
2626 	/*
2627 	 * Our dynamic properties are all device specific and size oriented.
2628 	 * Requests issued under conditions where size is valid are passed
2629 	 * to ddi_prop_op_nblocks with the size information, otherwise the
2630 	 * request is passed to ddi_prop_op. Size depends on valid geometry.
2631 	 */
2632 	un = ddi_get_soft_state(sd_state, instance);
2633 	if ((dev == DDI_DEV_T_ANY) || (un == NULL) ||
2634 	    (un->un_f_geometry_is_valid == FALSE)) {
2635 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2636 		    name, valuep, lengthp));
2637 	} else {
2638 		/* get nblocks value */
2639 		ASSERT(!mutex_owned(SD_MUTEX(un)));
2640 		mutex_enter(SD_MUTEX(un));
2641 		nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk;
2642 		mutex_exit(SD_MUTEX(un));
2643 
2644 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
2645 		    name, valuep, lengthp, nblocks64));
2646 	}
2647 }
2648 
2649 /*
2650  * The following functions are for smart probing:
2651  * sd_scsi_probe_cache_init()
2652  * sd_scsi_probe_cache_fini()
2653  * sd_scsi_clear_probe_cache()
2654  * sd_scsi_probe_with_cache()
2655  */
2656 
2657 /*
2658  *    Function: sd_scsi_probe_cache_init
2659  *
2660  * Description: Initializes the probe response cache mutex and head pointer.
2661  *
2662  *     Context: Kernel thread context
2663  */
2664 
2665 static void
2666 sd_scsi_probe_cache_init(void)
2667 {
2668 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2669 	sd_scsi_probe_cache_head = NULL;
2670 }
2671 
2672 
2673 /*
2674  *    Function: sd_scsi_probe_cache_fini
2675  *
2676  * Description: Frees all resources associated with the probe response cache.
2677  *
2678  *     Context: Kernel thread context
2679  */
2680 
2681 static void
2682 sd_scsi_probe_cache_fini(void)
2683 {
2684 	struct sd_scsi_probe_cache *cp;
2685 	struct sd_scsi_probe_cache *ncp;
2686 
2687 	/* Clean up our smart probing linked list */
2688 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2689 		ncp = cp->next;
2690 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2691 	}
2692 	sd_scsi_probe_cache_head = NULL;
2693 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2694 }
2695 
2696 
2697 /*
2698  *    Function: sd_scsi_clear_probe_cache
2699  *
2700  * Description: This routine clears the probe response cache. This is
2701  *		done when open() returns ENXIO so that when deferred
2702  *		attach is attempted (possibly after a device has been
2703  *		turned on) we will retry the probe. Since we don't know
2704  *		which target we failed to open, we just clear the
2705  *		entire cache.
2706  *
2707  *     Context: Kernel thread context
2708  */
2709 
2710 static void
2711 sd_scsi_clear_probe_cache(void)
2712 {
2713 	struct sd_scsi_probe_cache	*cp;
2714 	int				i;
2715 
2716 	mutex_enter(&sd_scsi_probe_cache_mutex);
2717 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2718 		/*
2719 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2720 		 * force probing to be performed the next time
2721 		 * sd_scsi_probe_with_cache is called.
2722 		 */
2723 		for (i = 0; i < NTARGETS_WIDE; i++) {
2724 			cp->cache[i] = SCSIPROBE_EXISTS;
2725 		}
2726 	}
2727 	mutex_exit(&sd_scsi_probe_cache_mutex);
2728 }
2729 
2730 
2731 /*
2732  *    Function: sd_scsi_probe_with_cache
2733  *
2734  * Description: This routine implements support for a scsi device probe
2735  *		with cache. The driver maintains a cache of the target
2736  *		responses to scsi probes. If we get no response from a
2737  *		target during a probe inquiry, we remember that, and we
2738  *		avoid additional calls to scsi_probe on non-zero LUNs
2739  *		on the same target until the cache is cleared. By doing
2740  *		so we avoid the 1/4 sec selection timeout for nonzero
2741  *		LUNs. lun0 of a target is always probed.
2742  *
2743  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2744  *              waitfunc - indicates what the allocator routines should
2745  *			   do when resources are not available. This value
2746  *			   is passed on to scsi_probe() when that routine
2747  *			   is called.
2748  *
2749  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2750  *		otherwise the value returned by scsi_probe(9F).
2751  *
2752  *     Context: Kernel thread context
2753  */
2754 
2755 static int
2756 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2757 {
2758 	struct sd_scsi_probe_cache	*cp;
2759 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
2760 	int		lun, tgt;
2761 
2762 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2763 	    SCSI_ADDR_PROP_LUN, 0);
2764 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
2765 	    SCSI_ADDR_PROP_TARGET, -1);
2766 
2767 	/* Make sure caching enabled and target in range */
2768 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2769 		/* do it the old way (no cache) */
2770 		return (scsi_probe(devp, waitfn));
2771 	}
2772 
2773 	mutex_enter(&sd_scsi_probe_cache_mutex);
2774 
2775 	/* Find the cache for this scsi bus instance */
2776 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2777 		if (cp->pdip == pdip) {
2778 			break;
2779 		}
2780 	}
2781 
2782 	/* If we can't find a cache for this pdip, create one */
2783 	if (cp == NULL) {
2784 		int i;
2785 
2786 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
2787 		    KM_SLEEP);
2788 		cp->pdip = pdip;
2789 		cp->next = sd_scsi_probe_cache_head;
2790 		sd_scsi_probe_cache_head = cp;
2791 		for (i = 0; i < NTARGETS_WIDE; i++) {
2792 			cp->cache[i] = SCSIPROBE_EXISTS;
2793 		}
2794 	}
2795 
2796 	mutex_exit(&sd_scsi_probe_cache_mutex);
2797 
2798 	/* Recompute the cache for this target if LUN zero */
2799 	if (lun == 0) {
2800 		cp->cache[tgt] = SCSIPROBE_EXISTS;
2801 	}
2802 
2803 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
2804 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
2805 		return (SCSIPROBE_NORESP);
2806 	}
2807 
2808 	/* Do the actual probe; save & return the result */
2809 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
2810 }
2811 
2812 
2813 /*
2814  *    Function: sd_spin_up_unit
2815  *
2816  * Description: Issues the following commands to spin-up the device:
2817  *		START STOP UNIT, and INQUIRY.
2818  *
2819  *   Arguments: un - driver soft state (unit) structure
2820  *
2821  * Return Code: 0 - success
2822  *		EIO - failure
2823  *		EACCES - reservation conflict
2824  *
2825  *     Context: Kernel thread context
2826  */
2827 
2828 static int
2829 sd_spin_up_unit(struct sd_lun *un)
2830 {
2831 	size_t	resid		= 0;
2832 	int	has_conflict	= FALSE;
2833 	uchar_t *bufaddr;
2834 
2835 	ASSERT(un != NULL);
2836 
2837 	/*
2838 	 * Send a throwaway START UNIT command.
2839 	 *
2840 	 * If we fail on this, we don't care presently what precisely
2841 	 * is wrong.  EMC's arrays will also fail this with a check
2842 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
2843 	 * we don't want to fail the attach because it may become
2844 	 * "active" later.
2845 	 */
2846 	if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT)
2847 	    == EACCES)
2848 		has_conflict = TRUE;
2849 
2850 	/*
2851 	 * Send another INQUIRY command to the target. This is necessary for
2852 	 * non-removable media direct access devices because their INQUIRY data
2853 	 * may not be fully qualified until they are spun up (perhaps via the
2854 	 * START command above).  Note: This seems to be needed for some
2855 	 * legacy devices only.) The INQUIRY command should succeed even if a
2856 	 * Reservation Conflict is present.
2857 	 */
2858 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
2859 	if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) {
2860 		kmem_free(bufaddr, SUN_INQSIZE);
2861 		return (EIO);
2862 	}
2863 
2864 	/*
2865 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
2866 	 * Note that this routine does not return a failure here even if the
2867 	 * INQUIRY command did not return any data.  This is a legacy behavior.
2868 	 */
2869 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
2870 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
2871 	}
2872 
2873 	kmem_free(bufaddr, SUN_INQSIZE);
2874 
2875 	/* If we hit a reservation conflict above, tell the caller. */
2876 	if (has_conflict == TRUE) {
2877 		return (EACCES);
2878 	}
2879 
2880 	return (0);
2881 }
2882 
2883 #ifdef _LP64
2884 /*
2885  *    Function: sd_enable_descr_sense
2886  *
2887  * Description: This routine attempts to select descriptor sense format
2888  *		using the Control mode page.  Devices that support 64 bit
2889  *		LBAs (for >2TB luns) should also implement descriptor
2890  *		sense data so we will call this function whenever we see
2891  *		a lun larger than 2TB.  If for some reason the device
2892  *		supports 64 bit LBAs but doesn't support descriptor sense
2893  *		presumably the mode select will fail.  Everything will
2894  *		continue to work normally except that we will not get
2895  *		complete sense data for commands that fail with an LBA
2896  *		larger than 32 bits.
2897  *
2898  *   Arguments: un - driver soft state (unit) structure
2899  *
2900  *     Context: Kernel thread context only
2901  */
2902 
2903 static void
2904 sd_enable_descr_sense(struct sd_lun *un)
2905 {
2906 	uchar_t			*header;
2907 	struct mode_control_scsi3 *ctrl_bufp;
2908 	size_t			buflen;
2909 	size_t			bd_len;
2910 
2911 	/*
2912 	 * Read MODE SENSE page 0xA, Control Mode Page
2913 	 */
2914 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
2915 	    sizeof (struct mode_control_scsi3);
2916 	header = kmem_zalloc(buflen, KM_SLEEP);
2917 	if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
2918 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) {
2919 		SD_ERROR(SD_LOG_COMMON, un,
2920 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
2921 		goto eds_exit;
2922 	}
2923 
2924 	/*
2925 	 * Determine size of Block Descriptors in order to locate
2926 	 * the mode page data. ATAPI devices return 0, SCSI devices
2927 	 * should return MODE_BLK_DESC_LENGTH.
2928 	 */
2929 	bd_len  = ((struct mode_header *)header)->bdesc_length;
2930 
2931 	ctrl_bufp = (struct mode_control_scsi3 *)
2932 	    (header + MODE_HEADER_LENGTH + bd_len);
2933 
2934 	/*
2935 	 * Clear PS bit for MODE SELECT
2936 	 */
2937 	ctrl_bufp->mode_page.ps = 0;
2938 
2939 	/*
2940 	 * Set D_SENSE to enable descriptor sense format.
2941 	 */
2942 	ctrl_bufp->d_sense = 1;
2943 
2944 	/*
2945 	 * Use MODE SELECT to commit the change to the D_SENSE bit
2946 	 */
2947 	if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
2948 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) {
2949 		SD_INFO(SD_LOG_COMMON, un,
2950 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
2951 		goto eds_exit;
2952 	}
2953 
2954 eds_exit:
2955 	kmem_free(header, buflen);
2956 }
2957 #endif /* _LP64 */
2958 
2959 
2960 /*
2961  *    Function: sd_set_mmc_caps
2962  *
2963  * Description: This routine determines if the device is MMC compliant and if
2964  *		the device supports CDDA via a mode sense of the CDVD
2965  *		capabilities mode page. Also checks if the device is a
2966  *		dvdram writable device.
2967  *
2968  *   Arguments: un - driver soft state (unit) structure
2969  *
2970  *     Context: Kernel thread context only
2971  */
2972 
2973 static void
2974 sd_set_mmc_caps(struct sd_lun *un)
2975 {
2976 	struct mode_header_grp2		*sense_mhp;
2977 	uchar_t				*sense_page;
2978 	caddr_t				buf;
2979 	int				bd_len;
2980 	int				status;
2981 	struct uscsi_cmd		com;
2982 	int				rtn;
2983 	uchar_t				*out_data_rw, *out_data_hd;
2984 	uchar_t				*rqbuf_rw, *rqbuf_hd;
2985 
2986 	ASSERT(un != NULL);
2987 
2988 	/*
2989 	 * The flags which will be set in this function are - mmc compliant,
2990 	 * dvdram writable device, cdda support. Initialize them to FALSE
2991 	 * and if a capability is detected - it will be set to TRUE.
2992 	 */
2993 	un->un_f_mmc_cap = FALSE;
2994 	un->un_f_dvdram_writable_device = FALSE;
2995 	un->un_f_cfg_cdda = FALSE;
2996 
2997 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
2998 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
2999 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3000 
3001 	if (status != 0) {
3002 		/* command failed; just return */
3003 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3004 		return;
3005 	}
3006 	/*
3007 	 * If the mode sense request for the CDROM CAPABILITIES
3008 	 * page (0x2A) succeeds the device is assumed to be MMC.
3009 	 */
3010 	un->un_f_mmc_cap = TRUE;
3011 
3012 	/* Get to the page data */
3013 	sense_mhp = (struct mode_header_grp2 *)buf;
3014 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3015 	    sense_mhp->bdesc_length_lo;
3016 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3017 		/*
3018 		 * We did not get back the expected block descriptor
3019 		 * length so we cannot determine if the device supports
3020 		 * CDDA. However, we still indicate the device is MMC
3021 		 * according to the successful response to the page
3022 		 * 0x2A mode sense request.
3023 		 */
3024 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3025 		    "sd_set_mmc_caps: Mode Sense returned "
3026 		    "invalid block descriptor length\n");
3027 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3028 		return;
3029 	}
3030 
3031 	/* See if read CDDA is supported */
3032 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3033 	    bd_len);
3034 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3035 
3036 	/* See if writing DVD RAM is supported. */
3037 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3038 	if (un->un_f_dvdram_writable_device == TRUE) {
3039 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3040 		return;
3041 	}
3042 
3043 	/*
3044 	 * If the device presents DVD or CD capabilities in the mode
3045 	 * page, we can return here since a RRD will not have
3046 	 * these capabilities.
3047 	 */
3048 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3049 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3050 		return;
3051 	}
3052 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3053 
3054 	/*
3055 	 * If un->un_f_dvdram_writable_device is still FALSE,
3056 	 * check for a Removable Rigid Disk (RRD).  A RRD
3057 	 * device is identified by the features RANDOM_WRITABLE and
3058 	 * HARDWARE_DEFECT_MANAGEMENT.
3059 	 */
3060 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3061 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3062 
3063 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3064 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3065 	    RANDOM_WRITABLE);
3066 	if (rtn != 0) {
3067 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3068 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3069 		return;
3070 	}
3071 
3072 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3073 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3074 
3075 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3076 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3077 	    HARDWARE_DEFECT_MANAGEMENT);
3078 	if (rtn == 0) {
3079 		/*
3080 		 * We have good information, check for random writable
3081 		 * and hardware defect features.
3082 		 */
3083 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3084 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3085 			un->un_f_dvdram_writable_device = TRUE;
3086 		}
3087 	}
3088 
3089 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3090 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3091 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3092 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3093 }
3094 
3095 /*
3096  *    Function: sd_check_for_writable_cd
3097  *
3098  * Description: This routine determines if the media in the device is
3099  *		writable or not. It uses the get configuration command (0x46)
3100  *		to determine if the media is writable
3101  *
3102  *   Arguments: un - driver soft state (unit) structure
3103  *
3104  *     Context: Never called at interrupt context.
3105  */
3106 
3107 static void
3108 sd_check_for_writable_cd(struct sd_lun *un)
3109 {
3110 	struct uscsi_cmd		com;
3111 	uchar_t				*out_data;
3112 	uchar_t				*rqbuf;
3113 	int				rtn;
3114 	uchar_t				*out_data_rw, *out_data_hd;
3115 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3116 	struct mode_header_grp2		*sense_mhp;
3117 	uchar_t				*sense_page;
3118 	caddr_t				buf;
3119 	int				bd_len;
3120 	int				status;
3121 
3122 	ASSERT(un != NULL);
3123 	ASSERT(mutex_owned(SD_MUTEX(un)));
3124 
3125 	/*
3126 	 * Initialize the writable media to false, if configuration info.
3127 	 * tells us otherwise then only we will set it.
3128 	 */
3129 	un->un_f_mmc_writable_media = FALSE;
3130 	mutex_exit(SD_MUTEX(un));
3131 
3132 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3133 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3134 
3135 	rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH,
3136 	    out_data, SD_PROFILE_HEADER_LEN);
3137 
3138 	mutex_enter(SD_MUTEX(un));
3139 	if (rtn == 0) {
3140 		/*
3141 		 * We have good information, check for writable DVD.
3142 		 */
3143 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3144 			un->un_f_mmc_writable_media = TRUE;
3145 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3146 			kmem_free(rqbuf, SENSE_LENGTH);
3147 			return;
3148 		}
3149 	}
3150 
3151 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3152 	kmem_free(rqbuf, SENSE_LENGTH);
3153 
3154 	/*
3155 	 * Determine if this is a RRD type device.
3156 	 */
3157 	mutex_exit(SD_MUTEX(un));
3158 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3159 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
3160 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3161 	mutex_enter(SD_MUTEX(un));
3162 	if (status != 0) {
3163 		/* command failed; just return */
3164 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3165 		return;
3166 	}
3167 
3168 	/* Get to the page data */
3169 	sense_mhp = (struct mode_header_grp2 *)buf;
3170 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3171 	if (bd_len > MODE_BLK_DESC_LENGTH) {
3172 		/*
3173 		 * We did not get back the expected block descriptor length so
3174 		 * we cannot check the mode page.
3175 		 */
3176 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3177 		    "sd_check_for_writable_cd: Mode Sense returned "
3178 		    "invalid block descriptor length\n");
3179 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3180 		return;
3181 	}
3182 
3183 	/*
3184 	 * If the device presents DVD or CD capabilities in the mode
3185 	 * page, we can return here since a RRD device will not have
3186 	 * these capabilities.
3187 	 */
3188 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3189 	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3190 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3191 		return;
3192 	}
3193 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3194 
3195 	/*
3196 	 * If un->un_f_mmc_writable_media is still FALSE,
3197 	 * check for RRD type media.  A RRD device is identified
3198 	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3199 	 */
3200 	mutex_exit(SD_MUTEX(un));
3201 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3202 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3203 
3204 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3205 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3206 	    RANDOM_WRITABLE);
3207 	if (rtn != 0) {
3208 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3209 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3210 		mutex_enter(SD_MUTEX(un));
3211 		return;
3212 	}
3213 
3214 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3215 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3216 
3217 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3218 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3219 	    HARDWARE_DEFECT_MANAGEMENT);
3220 	mutex_enter(SD_MUTEX(un));
3221 	if (rtn == 0) {
3222 		/*
3223 		 * We have good information, check for random writable
3224 		 * and hardware defect features as current.
3225 		 */
3226 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3227 		    (out_data_rw[10] & 0x1) &&
3228 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3229 		    (out_data_hd[10] & 0x1)) {
3230 			un->un_f_mmc_writable_media = TRUE;
3231 		}
3232 	}
3233 
3234 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3235 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3236 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3237 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3238 }
3239 
3240 /*
3241  *    Function: sd_read_unit_properties
3242  *
3243  * Description: The following implements a property lookup mechanism.
3244  *		Properties for particular disks (keyed on vendor, model
3245  *		and rev numbers) are sought in the sd.conf file via
3246  *		sd_process_sdconf_file(), and if not found there, are
3247  *		looked for in a list hardcoded in this driver via
3248  *		sd_process_sdconf_table() Once located the properties
3249  *		are used to update the driver unit structure.
3250  *
3251  *   Arguments: un - driver soft state (unit) structure
3252  */
3253 
3254 static void
3255 sd_read_unit_properties(struct sd_lun *un)
3256 {
3257 	/*
3258 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3259 	 * the "sd-config-list" property (from the sd.conf file) or if
3260 	 * there was not a match for the inquiry vid/pid. If this event
3261 	 * occurs the static driver configuration table is searched for
3262 	 * a match.
3263 	 */
3264 	ASSERT(un != NULL);
3265 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3266 		sd_process_sdconf_table(un);
3267 	}
3268 
3269 	/* check for LSI device */
3270 	sd_is_lsi(un);
3271 
3272 
3273 }
3274 
3275 
3276 /*
3277  *    Function: sd_process_sdconf_file
3278  *
3279  * Description: Use ddi_getlongprop to obtain the properties from the
3280  *		driver's config file (ie, sd.conf) and update the driver
3281  *		soft state structure accordingly.
3282  *
3283  *   Arguments: un - driver soft state (unit) structure
3284  *
3285  * Return Code: SD_SUCCESS - The properties were successfully set according
3286  *			     to the driver configuration file.
3287  *		SD_FAILURE - The driver config list was not obtained or
3288  *			     there was no vid/pid match. This indicates that
3289  *			     the static config table should be used.
3290  *
3291  * The config file has a property, "sd-config-list", which consists of
3292  * one or more duplets as follows:
3293  *
3294  *  sd-config-list=
3295  *	<duplet>,
3296  *	[<duplet>,]
3297  *	[<duplet>];
3298  *
3299  * The structure of each duplet is as follows:
3300  *
3301  *  <duplet>:= <vid+pid>,<data-property-name_list>
3302  *
3303  * The first entry of the duplet is the device ID string (the concatenated
3304  * vid & pid; not to be confused with a device_id).  This is defined in
3305  * the same way as in the sd_disk_table.
3306  *
3307  * The second part of the duplet is a string that identifies a
3308  * data-property-name-list. The data-property-name-list is defined as
3309  * follows:
3310  *
3311  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3312  *
3313  * The syntax of <data-property-name> depends on the <version> field.
3314  *
3315  * If version = SD_CONF_VERSION_1 we have the following syntax:
3316  *
3317  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3318  *
3319  * where the prop0 value will be used to set prop0 if bit0 set in the
3320  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3321  *
3322  */
3323 
3324 static int
3325 sd_process_sdconf_file(struct sd_lun *un)
3326 {
3327 	char	*config_list = NULL;
3328 	int	config_list_len;
3329 	int	len;
3330 	int	dupletlen = 0;
3331 	char	*vidptr;
3332 	int	vidlen;
3333 	char	*dnlist_ptr;
3334 	char	*dataname_ptr;
3335 	int	dnlist_len;
3336 	int	dataname_len;
3337 	int	*data_list;
3338 	int	data_list_len;
3339 	int	rval = SD_FAILURE;
3340 	int	i;
3341 
3342 	ASSERT(un != NULL);
3343 
3344 	/* Obtain the configuration list associated with the .conf file */
3345 	if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS,
3346 	    sd_config_list, (caddr_t)&config_list, &config_list_len)
3347 	    != DDI_PROP_SUCCESS) {
3348 		return (SD_FAILURE);
3349 	}
3350 
3351 	/*
3352 	 * Compare vids in each duplet to the inquiry vid - if a match is
3353 	 * made, get the data value and update the soft state structure
3354 	 * accordingly.
3355 	 *
3356 	 * Note: This algorithm is complex and difficult to maintain. It should
3357 	 * be replaced with a more robust implementation.
3358 	 */
3359 	for (len = config_list_len, vidptr = config_list; len > 0;
3360 	    vidptr += dupletlen, len -= dupletlen) {
3361 		/*
3362 		 * Note: The assumption here is that each vid entry is on
3363 		 * a unique line from its associated duplet.
3364 		 */
3365 		vidlen = dupletlen = (int)strlen(vidptr);
3366 		if ((vidlen == 0) ||
3367 		    (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3368 			dupletlen++;
3369 			continue;
3370 		}
3371 
3372 		/*
3373 		 * dnlist contains 1 or more blank separated
3374 		 * data-property-name entries
3375 		 */
3376 		dnlist_ptr = vidptr + vidlen + 1;
3377 		dnlist_len = (int)strlen(dnlist_ptr);
3378 		dupletlen += dnlist_len + 2;
3379 
3380 		/*
3381 		 * Set a pointer for the first data-property-name
3382 		 * entry in the list
3383 		 */
3384 		dataname_ptr = dnlist_ptr;
3385 		dataname_len = 0;
3386 
3387 		/*
3388 		 * Loop through all data-property-name entries in the
3389 		 * data-property-name-list setting the properties for each.
3390 		 */
3391 		while (dataname_len < dnlist_len) {
3392 			int version;
3393 
3394 			/*
3395 			 * Determine the length of the current
3396 			 * data-property-name entry by indexing until a
3397 			 * blank or NULL is encountered. When the space is
3398 			 * encountered reset it to a NULL for compliance
3399 			 * with ddi_getlongprop().
3400 			 */
3401 			for (i = 0; ((dataname_ptr[i] != ' ') &&
3402 			    (dataname_ptr[i] != '\0')); i++) {
3403 				;
3404 			}
3405 
3406 			dataname_len += i;
3407 			/* If not null terminated, Make it so */
3408 			if (dataname_ptr[i] == ' ') {
3409 				dataname_ptr[i] = '\0';
3410 			}
3411 			dataname_len++;
3412 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3413 			    "sd_process_sdconf_file: disk:%s, data:%s\n",
3414 			    vidptr, dataname_ptr);
3415 
3416 			/* Get the data list */
3417 			if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0,
3418 			    dataname_ptr, (caddr_t)&data_list, &data_list_len)
3419 			    != DDI_PROP_SUCCESS) {
3420 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3421 				    "sd_process_sdconf_file: data property (%s)"
3422 				    " has no value\n", dataname_ptr);
3423 				dataname_ptr = dnlist_ptr + dataname_len;
3424 				continue;
3425 			}
3426 
3427 			version = data_list[0];
3428 
3429 			if (version == SD_CONF_VERSION_1) {
3430 				sd_tunables values;
3431 
3432 				/* Set the properties */
3433 				if (sd_chk_vers1_data(un, data_list[1],
3434 				    &data_list[2], data_list_len, dataname_ptr)
3435 				    == SD_SUCCESS) {
3436 					sd_get_tunables_from_conf(un,
3437 					    data_list[1], &data_list[2],
3438 					    &values);
3439 					sd_set_vers1_properties(un,
3440 					    data_list[1], &values);
3441 					rval = SD_SUCCESS;
3442 				} else {
3443 					rval = SD_FAILURE;
3444 				}
3445 			} else {
3446 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3447 				    "data property %s version 0x%x is invalid.",
3448 				    dataname_ptr, version);
3449 				rval = SD_FAILURE;
3450 			}
3451 			kmem_free(data_list, data_list_len);
3452 			dataname_ptr = dnlist_ptr + dataname_len;
3453 		}
3454 	}
3455 
3456 	/* free up the memory allocated by ddi_getlongprop */
3457 	if (config_list) {
3458 		kmem_free(config_list, config_list_len);
3459 	}
3460 
3461 	return (rval);
3462 }
3463 
3464 /*
3465  *    Function: sd_get_tunables_from_conf()
3466  *
3467  *
3468  *    This function reads the data list from the sd.conf file and pulls
3469  *    the values that can have numeric values as arguments and places
3470  *    the values in the apropriate sd_tunables member.
3471  *    Since the order of the data list members varies across platforms
3472  *    This function reads them from the data list in a platform specific
3473  *    order and places them into the correct sd_tunable member that is
3474  *    a consistant across all platforms.
3475  */
3476 static void
3477 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
3478     sd_tunables *values)
3479 {
3480 	int i;
3481 	int mask;
3482 
3483 	bzero(values, sizeof (sd_tunables));
3484 
3485 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3486 
3487 		mask = 1 << i;
3488 		if (mask > flags) {
3489 			break;
3490 		}
3491 
3492 		switch (mask & flags) {
3493 		case 0:	/* This mask bit not set in flags */
3494 			continue;
3495 		case SD_CONF_BSET_THROTTLE:
3496 			values->sdt_throttle = data_list[i];
3497 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3498 			    "sd_get_tunables_from_conf: throttle = %d\n",
3499 			    values->sdt_throttle);
3500 			break;
3501 		case SD_CONF_BSET_CTYPE:
3502 			values->sdt_ctype = data_list[i];
3503 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3504 			    "sd_get_tunables_from_conf: ctype = %d\n",
3505 			    values->sdt_ctype);
3506 			break;
3507 		case SD_CONF_BSET_NRR_COUNT:
3508 			values->sdt_not_rdy_retries = data_list[i];
3509 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3510 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
3511 			    values->sdt_not_rdy_retries);
3512 			break;
3513 		case SD_CONF_BSET_BSY_RETRY_COUNT:
3514 			values->sdt_busy_retries = data_list[i];
3515 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3516 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
3517 			    values->sdt_busy_retries);
3518 			break;
3519 		case SD_CONF_BSET_RST_RETRIES:
3520 			values->sdt_reset_retries = data_list[i];
3521 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3522 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
3523 			    values->sdt_reset_retries);
3524 			break;
3525 		case SD_CONF_BSET_RSV_REL_TIME:
3526 			values->sdt_reserv_rel_time = data_list[i];
3527 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3528 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
3529 			    values->sdt_reserv_rel_time);
3530 			break;
3531 		case SD_CONF_BSET_MIN_THROTTLE:
3532 			values->sdt_min_throttle = data_list[i];
3533 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3534 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
3535 			    values->sdt_min_throttle);
3536 			break;
3537 		case SD_CONF_BSET_DISKSORT_DISABLED:
3538 			values->sdt_disk_sort_dis = data_list[i];
3539 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3540 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
3541 			    values->sdt_disk_sort_dis);
3542 			break;
3543 		case SD_CONF_BSET_LUN_RESET_ENABLED:
3544 			values->sdt_lun_reset_enable = data_list[i];
3545 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3546 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
3547 			    "\n", values->sdt_lun_reset_enable);
3548 			break;
3549 		}
3550 	}
3551 }
3552 
3553 /*
3554  *    Function: sd_process_sdconf_table
3555  *
3556  * Description: Search the static configuration table for a match on the
3557  *		inquiry vid/pid and update the driver soft state structure
3558  *		according to the table property values for the device.
3559  *
3560  *		The form of a configuration table entry is:
3561  *		  <vid+pid>,<flags>,<property-data>
3562  *		  "SEAGATE ST42400N",1,63,0,0			(Fibre)
3563  *		  "SEAGATE ST42400N",1,63,0,0,0,0		(Sparc)
3564  *		  "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0	(Intel)
3565  *
3566  *   Arguments: un - driver soft state (unit) structure
3567  */
3568 
3569 static void
3570 sd_process_sdconf_table(struct sd_lun *un)
3571 {
3572 	char	*id = NULL;
3573 	int	table_index;
3574 	int	idlen;
3575 
3576 	ASSERT(un != NULL);
3577 	for (table_index = 0; table_index < sd_disk_table_size;
3578 	    table_index++) {
3579 		id = sd_disk_table[table_index].device_id;
3580 		idlen = strlen(id);
3581 		if (idlen == 0) {
3582 			continue;
3583 		}
3584 
3585 		/*
3586 		 * The static configuration table currently does not
3587 		 * implement version 10 properties. Additionally,
3588 		 * multiple data-property-name entries are not
3589 		 * implemented in the static configuration table.
3590 		 */
3591 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
3592 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3593 			    "sd_process_sdconf_table: disk %s\n", id);
3594 			sd_set_vers1_properties(un,
3595 			    sd_disk_table[table_index].flags,
3596 			    sd_disk_table[table_index].properties);
3597 			break;
3598 		}
3599 	}
3600 }
3601 
3602 
3603 /*
3604  *    Function: sd_sdconf_id_match
3605  *
3606  * Description: This local function implements a case sensitive vid/pid
3607  *		comparison as well as the boundary cases of wild card and
3608  *		multiple blanks.
3609  *
3610  *		Note: An implicit assumption made here is that the scsi
3611  *		inquiry structure will always keep the vid, pid and
3612  *		revision strings in consecutive sequence, so they can be
3613  *		read as a single string. If this assumption is not the
3614  *		case, a separate string, to be used for the check, needs
3615  *		to be built with these strings concatenated.
3616  *
3617  *   Arguments: un - driver soft state (unit) structure
3618  *		id - table or config file vid/pid
3619  *		idlen  - length of the vid/pid (bytes)
3620  *
3621  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3622  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3623  */
3624 
3625 static int
3626 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
3627 {
3628 	struct scsi_inquiry	*sd_inq;
3629 	int 			rval = SD_SUCCESS;
3630 
3631 	ASSERT(un != NULL);
3632 	sd_inq = un->un_sd->sd_inq;
3633 	ASSERT(id != NULL);
3634 
3635 	/*
3636 	 * We use the inq_vid as a pointer to a buffer containing the
3637 	 * vid and pid and use the entire vid/pid length of the table
3638 	 * entry for the comparison. This works because the inq_pid
3639 	 * data member follows inq_vid in the scsi_inquiry structure.
3640 	 */
3641 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
3642 		/*
3643 		 * The user id string is compared to the inquiry vid/pid
3644 		 * using a case insensitive comparison and ignoring
3645 		 * multiple spaces.
3646 		 */
3647 		rval = sd_blank_cmp(un, id, idlen);
3648 		if (rval != SD_SUCCESS) {
3649 			/*
3650 			 * User id strings that start and end with a "*"
3651 			 * are a special case. These do not have a
3652 			 * specific vendor, and the product string can
3653 			 * appear anywhere in the 16 byte PID portion of
3654 			 * the inquiry data. This is a simple strstr()
3655 			 * type search for the user id in the inquiry data.
3656 			 */
3657 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
3658 				char	*pidptr = &id[1];
3659 				int	i;
3660 				int	j;
3661 				int	pidstrlen = idlen - 2;
3662 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
3663 				    pidstrlen;
3664 
3665 				if (j < 0) {
3666 					return (SD_FAILURE);
3667 				}
3668 				for (i = 0; i < j; i++) {
3669 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
3670 					    pidptr, pidstrlen) == 0) {
3671 						rval = SD_SUCCESS;
3672 						break;
3673 					}
3674 				}
3675 			}
3676 		}
3677 	}
3678 	return (rval);
3679 }
3680 
3681 
3682 /*
3683  *    Function: sd_blank_cmp
3684  *
3685  * Description: If the id string starts and ends with a space, treat
3686  *		multiple consecutive spaces as equivalent to a single
3687  *		space. For example, this causes a sd_disk_table entry
3688  *		of " NEC CDROM " to match a device's id string of
3689  *		"NEC       CDROM".
3690  *
3691  *		Note: The success exit condition for this routine is if
3692  *		the pointer to the table entry is '\0' and the cnt of
3693  *		the inquiry length is zero. This will happen if the inquiry
3694  *		string returned by the device is padded with spaces to be
3695  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
3696  *		SCSI spec states that the inquiry string is to be padded with
3697  *		spaces.
3698  *
3699  *   Arguments: un - driver soft state (unit) structure
3700  *		id - table or config file vid/pid
3701  *		idlen  - length of the vid/pid (bytes)
3702  *
3703  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3704  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3705  */
3706 
3707 static int
3708 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
3709 {
3710 	char		*p1;
3711 	char		*p2;
3712 	int		cnt;
3713 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
3714 	    sizeof (SD_INQUIRY(un)->inq_pid);
3715 
3716 	ASSERT(un != NULL);
3717 	p2 = un->un_sd->sd_inq->inq_vid;
3718 	ASSERT(id != NULL);
3719 	p1 = id;
3720 
3721 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
3722 		/*
3723 		 * Note: string p1 is terminated by a NUL but string p2
3724 		 * isn't.  The end of p2 is determined by cnt.
3725 		 */
3726 		for (;;) {
3727 			/* skip over any extra blanks in both strings */
3728 			while ((*p1 != '\0') && (*p1 == ' ')) {
3729 				p1++;
3730 			}
3731 			while ((cnt != 0) && (*p2 == ' ')) {
3732 				p2++;
3733 				cnt--;
3734 			}
3735 
3736 			/* compare the two strings */
3737 			if ((cnt == 0) ||
3738 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
3739 				break;
3740 			}
3741 			while ((cnt > 0) &&
3742 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
3743 				p1++;
3744 				p2++;
3745 				cnt--;
3746 			}
3747 		}
3748 	}
3749 
3750 	/* return SD_SUCCESS if both strings match */
3751 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
3752 }
3753 
3754 
3755 /*
3756  *    Function: sd_chk_vers1_data
3757  *
3758  * Description: Verify the version 1 device properties provided by the
3759  *		user via the configuration file
3760  *
3761  *   Arguments: un	     - driver soft state (unit) structure
3762  *		flags	     - integer mask indicating properties to be set
3763  *		prop_list    - integer list of property values
3764  *		list_len     - length of user provided data
3765  *
3766  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
3767  *		SD_FAILURE - Indicates the user provided data is invalid
3768  */
3769 
3770 static int
3771 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
3772     int list_len, char *dataname_ptr)
3773 {
3774 	int i;
3775 	int mask = 1;
3776 	int index = 0;
3777 
3778 	ASSERT(un != NULL);
3779 
3780 	/* Check for a NULL property name and list */
3781 	if (dataname_ptr == NULL) {
3782 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3783 		    "sd_chk_vers1_data: NULL data property name.");
3784 		return (SD_FAILURE);
3785 	}
3786 	if (prop_list == NULL) {
3787 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3788 		    "sd_chk_vers1_data: %s NULL data property list.",
3789 		    dataname_ptr);
3790 		return (SD_FAILURE);
3791 	}
3792 
3793 	/* Display a warning if undefined bits are set in the flags */
3794 	if (flags & ~SD_CONF_BIT_MASK) {
3795 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3796 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
3797 		    "Properties not set.",
3798 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
3799 		return (SD_FAILURE);
3800 	}
3801 
3802 	/*
3803 	 * Verify the length of the list by identifying the highest bit set
3804 	 * in the flags and validating that the property list has a length
3805 	 * up to the index of this bit.
3806 	 */
3807 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3808 		if (flags & mask) {
3809 			index++;
3810 		}
3811 		mask = 1 << i;
3812 	}
3813 	if ((list_len / sizeof (int)) < (index + 2)) {
3814 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3815 		    "sd_chk_vers1_data: "
3816 		    "Data property list %s size is incorrect. "
3817 		    "Properties not set.", dataname_ptr);
3818 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
3819 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
3820 		return (SD_FAILURE);
3821 	}
3822 	return (SD_SUCCESS);
3823 }
3824 
3825 
3826 /*
3827  *    Function: sd_set_vers1_properties
3828  *
3829  * Description: Set version 1 device properties based on a property list
3830  *		retrieved from the driver configuration file or static
3831  *		configuration table. Version 1 properties have the format:
3832  *
3833  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3834  *
3835  *		where the prop0 value will be used to set prop0 if bit0
3836  *		is set in the flags
3837  *
3838  *   Arguments: un	     - driver soft state (unit) structure
3839  *		flags	     - integer mask indicating properties to be set
3840  *		prop_list    - integer list of property values
3841  */
3842 
3843 static void
3844 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
3845 {
3846 	ASSERT(un != NULL);
3847 
3848 	/*
3849 	 * Set the flag to indicate cache is to be disabled. An attempt
3850 	 * to disable the cache via sd_cache_control() will be made
3851 	 * later during attach once the basic initialization is complete.
3852 	 */
3853 	if (flags & SD_CONF_BSET_NOCACHE) {
3854 		un->un_f_opt_disable_cache = TRUE;
3855 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3856 		    "sd_set_vers1_properties: caching disabled flag set\n");
3857 	}
3858 
3859 	/* CD-specific configuration parameters */
3860 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
3861 		un->un_f_cfg_playmsf_bcd = TRUE;
3862 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3863 		    "sd_set_vers1_properties: playmsf_bcd set\n");
3864 	}
3865 	if (flags & SD_CONF_BSET_READSUB_BCD) {
3866 		un->un_f_cfg_readsub_bcd = TRUE;
3867 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3868 		    "sd_set_vers1_properties: readsub_bcd set\n");
3869 	}
3870 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
3871 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
3872 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3873 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
3874 	}
3875 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
3876 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
3877 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3878 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
3879 	}
3880 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
3881 		un->un_f_cfg_no_read_header = TRUE;
3882 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3883 			    "sd_set_vers1_properties: no_read_header set\n");
3884 	}
3885 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
3886 		un->un_f_cfg_read_cd_xd4 = TRUE;
3887 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3888 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
3889 	}
3890 
3891 	/* Support for devices which do not have valid/unique serial numbers */
3892 	if (flags & SD_CONF_BSET_FAB_DEVID) {
3893 		un->un_f_opt_fab_devid = TRUE;
3894 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3895 		    "sd_set_vers1_properties: fab_devid bit set\n");
3896 	}
3897 
3898 	/* Support for user throttle configuration */
3899 	if (flags & SD_CONF_BSET_THROTTLE) {
3900 		ASSERT(prop_list != NULL);
3901 		un->un_saved_throttle = un->un_throttle =
3902 		    prop_list->sdt_throttle;
3903 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3904 		    "sd_set_vers1_properties: throttle set to %d\n",
3905 		    prop_list->sdt_throttle);
3906 	}
3907 
3908 	/* Set the per disk retry count according to the conf file or table. */
3909 	if (flags & SD_CONF_BSET_NRR_COUNT) {
3910 		ASSERT(prop_list != NULL);
3911 		if (prop_list->sdt_not_rdy_retries) {
3912 			un->un_notready_retry_count =
3913 				prop_list->sdt_not_rdy_retries;
3914 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3915 			    "sd_set_vers1_properties: not ready retry count"
3916 			    " set to %d\n", un->un_notready_retry_count);
3917 		}
3918 	}
3919 
3920 	/* The controller type is reported for generic disk driver ioctls */
3921 	if (flags & SD_CONF_BSET_CTYPE) {
3922 		ASSERT(prop_list != NULL);
3923 		switch (prop_list->sdt_ctype) {
3924 		case CTYPE_CDROM:
3925 			un->un_ctype = prop_list->sdt_ctype;
3926 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3927 			    "sd_set_vers1_properties: ctype set to "
3928 			    "CTYPE_CDROM\n");
3929 			break;
3930 		case CTYPE_CCS:
3931 			un->un_ctype = prop_list->sdt_ctype;
3932 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3933 				"sd_set_vers1_properties: ctype set to "
3934 				"CTYPE_CCS\n");
3935 			break;
3936 		case CTYPE_ROD:		/* RW optical */
3937 			un->un_ctype = prop_list->sdt_ctype;
3938 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3939 			    "sd_set_vers1_properties: ctype set to "
3940 			    "CTYPE_ROD\n");
3941 			break;
3942 		default:
3943 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3944 			    "sd_set_vers1_properties: Could not set "
3945 			    "invalid ctype value (%d)",
3946 			    prop_list->sdt_ctype);
3947 		}
3948 	}
3949 
3950 	/* Purple failover timeout */
3951 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
3952 		ASSERT(prop_list != NULL);
3953 		un->un_busy_retry_count =
3954 			prop_list->sdt_busy_retries;
3955 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3956 		    "sd_set_vers1_properties: "
3957 		    "busy retry count set to %d\n",
3958 		    un->un_busy_retry_count);
3959 	}
3960 
3961 	/* Purple reset retry count */
3962 	if (flags & SD_CONF_BSET_RST_RETRIES) {
3963 		ASSERT(prop_list != NULL);
3964 		un->un_reset_retry_count =
3965 			prop_list->sdt_reset_retries;
3966 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3967 		    "sd_set_vers1_properties: "
3968 		    "reset retry count set to %d\n",
3969 		    un->un_reset_retry_count);
3970 	}
3971 
3972 	/* Purple reservation release timeout */
3973 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
3974 		ASSERT(prop_list != NULL);
3975 		un->un_reserve_release_time =
3976 			prop_list->sdt_reserv_rel_time;
3977 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3978 		    "sd_set_vers1_properties: "
3979 		    "reservation release timeout set to %d\n",
3980 		    un->un_reserve_release_time);
3981 	}
3982 
3983 	/*
3984 	 * Driver flag telling the driver to verify that no commands are pending
3985 	 * for a device before issuing a Test Unit Ready. This is a workaround
3986 	 * for a firmware bug in some Seagate eliteI drives.
3987 	 */
3988 	if (flags & SD_CONF_BSET_TUR_CHECK) {
3989 		un->un_f_cfg_tur_check = TRUE;
3990 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3991 		    "sd_set_vers1_properties: tur queue check set\n");
3992 	}
3993 
3994 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
3995 		un->un_min_throttle = prop_list->sdt_min_throttle;
3996 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3997 		    "sd_set_vers1_properties: min throttle set to %d\n",
3998 		    un->un_min_throttle);
3999 	}
4000 
4001 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4002 		un->un_f_disksort_disabled =
4003 		    (prop_list->sdt_disk_sort_dis != 0) ?
4004 		    TRUE : FALSE;
4005 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4006 		    "sd_set_vers1_properties: disksort disabled "
4007 		    "flag set to %d\n",
4008 		    prop_list->sdt_disk_sort_dis);
4009 	}
4010 
4011 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4012 		un->un_f_lun_reset_enabled =
4013 		    (prop_list->sdt_lun_reset_enable != 0) ?
4014 		    TRUE : FALSE;
4015 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4016 		    "sd_set_vers1_properties: lun reset enabled "
4017 		    "flag set to %d\n",
4018 		    prop_list->sdt_lun_reset_enable);
4019 	}
4020 
4021 	/*
4022 	 * Validate the throttle values.
4023 	 * If any of the numbers are invalid, set everything to defaults.
4024 	 */
4025 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4026 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4027 	    (un->un_min_throttle > un->un_throttle)) {
4028 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4029 		un->un_min_throttle = sd_min_throttle;
4030 	}
4031 }
4032 
4033 /*
4034  *   Function: sd_is_lsi()
4035  *
4036  *   Description: Check for lsi devices, step throught the static device
4037  *	table to match vid/pid.
4038  *
4039  *   Args: un - ptr to sd_lun
4040  *
4041  *   Notes:  When creating new LSI property, need to add the new LSI property
4042  *		to this function.
4043  */
4044 static void
4045 sd_is_lsi(struct sd_lun *un)
4046 {
4047 	char	*id = NULL;
4048 	int	table_index;
4049 	int	idlen;
4050 	void	*prop;
4051 
4052 	ASSERT(un != NULL);
4053 	for (table_index = 0; table_index < sd_disk_table_size;
4054 	    table_index++) {
4055 		id = sd_disk_table[table_index].device_id;
4056 		idlen = strlen(id);
4057 		if (idlen == 0) {
4058 			continue;
4059 		}
4060 
4061 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4062 			prop = sd_disk_table[table_index].properties;
4063 			if (prop == &lsi_properties ||
4064 			    prop == &lsi_oem_properties ||
4065 			    prop == &lsi_properties_scsi ||
4066 			    prop == &symbios_properties) {
4067 				un->un_f_cfg_is_lsi = TRUE;
4068 			}
4069 			break;
4070 		}
4071 	}
4072 }
4073 
4074 
4075 /*
4076  * The following routines support reading and interpretation of disk labels,
4077  * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and
4078  * fdisk tables.
4079  */
4080 
4081 /*
4082  *    Function: sd_validate_geometry
4083  *
4084  * Description: Read the label from the disk (if present). Update the unit's
4085  *		geometry and vtoc information from the data in the label.
4086  *		Verify that the label is valid.
4087  *
4088  *   Arguments: un - driver soft state (unit) structure
4089  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4090  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4091  *			to use the USCSI "direct" chain and bypass the normal
4092  *			command waitq.
4093  *
4094  * Return Code: 0 - Successful completion
4095  *		EINVAL  - Invalid value in un->un_tgt_blocksize or
4096  *			  un->un_blockcount; or label on disk is corrupted
4097  *			  or unreadable.
4098  *		EACCES  - Reservation conflict at the device.
4099  *		ENOMEM  - Resource allocation error
4100  *		ENOTSUP - geometry not applicable
4101  *
4102  *     Context: Kernel thread only (can sleep).
4103  */
4104 
4105 static int
4106 sd_validate_geometry(struct sd_lun *un, int path_flag)
4107 {
4108 	static	char		labelstring[128];
4109 	static	char		buf[256];
4110 	char	*label		= NULL;
4111 	int	label_error	= 0;
4112 	int	gvalid		= un->un_f_geometry_is_valid;
4113 	int	lbasize;
4114 	uint_t	capacity;
4115 	int	count;
4116 
4117 	ASSERT(un != NULL);
4118 	ASSERT(mutex_owned(SD_MUTEX(un)));
4119 
4120 	/*
4121 	 * If the required values are not valid, then try getting them
4122 	 * once via read capacity. If that fails, then fail this call.
4123 	 * This is necessary with the new mpxio failover behavior in
4124 	 * the T300 where we can get an attach for the inactive path
4125 	 * before the active path. The inactive path fails commands with
4126 	 * sense data of 02,04,88 which happens to the read capacity
4127 	 * before mpxio has had sufficient knowledge to know if it should
4128 	 * force a fail over or not. (Which it won't do at attach anyhow).
4129 	 * If the read capacity at attach time fails, un_tgt_blocksize and
4130 	 * un_blockcount won't be valid.
4131 	 */
4132 	if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
4133 	    (un->un_f_blockcount_is_valid != TRUE)) {
4134 		uint64_t	cap;
4135 		uint32_t	lbasz;
4136 		int		rval;
4137 
4138 		mutex_exit(SD_MUTEX(un));
4139 		rval = sd_send_scsi_READ_CAPACITY(un, &cap,
4140 		    &lbasz, SD_PATH_DIRECT);
4141 		mutex_enter(SD_MUTEX(un));
4142 		if (rval == 0) {
4143 			/*
4144 			 * The following relies on
4145 			 * sd_send_scsi_READ_CAPACITY never
4146 			 * returning 0 for capacity and/or lbasize.
4147 			 */
4148 			sd_update_block_info(un, lbasz, cap);
4149 		}
4150 
4151 		if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
4152 		    (un->un_f_blockcount_is_valid != TRUE)) {
4153 			return (EINVAL);
4154 		}
4155 	}
4156 
4157 	/*
4158 	 * Copy the lbasize and capacity so that if they're reset while we're
4159 	 * not holding the SD_MUTEX, we will continue to use valid values
4160 	 * after the SD_MUTEX is reacquired. (4119659)
4161 	 */
4162 	lbasize  = un->un_tgt_blocksize;
4163 	capacity = un->un_blockcount;
4164 
4165 #if defined(_SUNOS_VTOC_16)
4166 	/*
4167 	 * Set up the "whole disk" fdisk partition; this should always
4168 	 * exist, regardless of whether the disk contains an fdisk table
4169 	 * or vtoc.
4170 	 */
4171 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
4172 	un->un_map[P0_RAW_DISK].dkl_nblk  = capacity;
4173 #endif
4174 
4175 	/*
4176 	 * Refresh the logical and physical geometry caches.
4177 	 * (data from MODE SENSE format/rigid disk geometry pages,
4178 	 * and scsi_ifgetcap("geometry").
4179 	 */
4180 	sd_resync_geom_caches(un, capacity, lbasize, path_flag);
4181 
4182 	label_error = sd_use_efi(un, path_flag);
4183 	if (label_error == 0) {
4184 		/* found a valid EFI label */
4185 		SD_TRACE(SD_LOG_IO_PARTITION, un,
4186 			"sd_validate_geometry: found EFI label\n");
4187 		un->un_solaris_offset = 0;
4188 		un->un_solaris_size = capacity;
4189 		return (ENOTSUP);
4190 	}
4191 	if (un->un_blockcount > DK_MAX_BLOCKS) {
4192 		if (label_error == ESRCH) {
4193 			/*
4194 			 * they've configured a LUN over 1TB, but used
4195 			 * format.dat to restrict format's view of the
4196 			 * capacity to be under 1TB
4197 			 */
4198 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4199 "is >1TB and has a VTOC label: use format(1M) to either decrease the");
4200 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
4201 "size to be < 1TB or relabel the disk with an EFI label");
4202 		} else {
4203 			/* unlabeled disk over 1TB */
4204 			return (ENOTSUP);
4205 		}
4206 	}
4207 	label_error = 0;
4208 
4209 	/*
4210 	 * at this point it is either labeled with a VTOC or it is
4211 	 * under 1TB
4212 	 */
4213 	if (un->un_f_vtoc_label_supported) {
4214 		struct	dk_label *dkl;
4215 		offset_t dkl1;
4216 		offset_t label_addr, real_addr;
4217 		int	rval;
4218 		size_t	buffer_size;
4219 
4220 		/*
4221 		 * Note: This will set up un->un_solaris_size and
4222 		 * un->un_solaris_offset.
4223 		 */
4224 		switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) {
4225 		case SD_CMD_RESERVATION_CONFLICT:
4226 			ASSERT(mutex_owned(SD_MUTEX(un)));
4227 			return (EACCES);
4228 		case SD_CMD_FAILURE:
4229 			ASSERT(mutex_owned(SD_MUTEX(un)));
4230 			return (ENOMEM);
4231 		}
4232 
4233 		if (un->un_solaris_size <= DK_LABEL_LOC) {
4234 			/*
4235 			 * Found fdisk table but no Solaris partition entry,
4236 			 * so don't call sd_uselabel() and don't create
4237 			 * a default label.
4238 			 */
4239 			label_error = 0;
4240 			un->un_f_geometry_is_valid = TRUE;
4241 			goto no_solaris_partition;
4242 		}
4243 		label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC);
4244 
4245 		/*
4246 		 * sys_blocksize != tgt_blocksize, need to re-adjust
4247 		 * blkno and save the index to beginning of dk_label
4248 		 */
4249 		real_addr = SD_SYS2TGTBLOCK(un, label_addr);
4250 		buffer_size = SD_REQBYTES2TGTBYTES(un,
4251 		    sizeof (struct dk_label));
4252 
4253 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: "
4254 		    "label_addr: 0x%x allocation size: 0x%x\n",
4255 		    label_addr, buffer_size);
4256 		dkl = kmem_zalloc(buffer_size, KM_NOSLEEP);
4257 		if (dkl == NULL) {
4258 			return (ENOMEM);
4259 		}
4260 
4261 		mutex_exit(SD_MUTEX(un));
4262 		rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr,
4263 		    path_flag);
4264 		mutex_enter(SD_MUTEX(un));
4265 
4266 		switch (rval) {
4267 		case 0:
4268 			/*
4269 			 * sd_uselabel will establish that the geometry
4270 			 * is valid.
4271 			 * For sys_blocksize != tgt_blocksize, need
4272 			 * to index into the beginning of dk_label
4273 			 */
4274 			dkl1 = (daddr_t)dkl
4275 				+ SD_TGTBYTEOFFSET(un, label_addr, real_addr);
4276 			if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1,
4277 			    path_flag) != SD_LABEL_IS_VALID) {
4278 				label_error = EINVAL;
4279 			}
4280 			break;
4281 		case EACCES:
4282 			label_error = EACCES;
4283 			break;
4284 		default:
4285 			label_error = EINVAL;
4286 			break;
4287 		}
4288 
4289 		kmem_free(dkl, buffer_size);
4290 
4291 #if defined(_SUNOS_VTOC_8)
4292 		label = (char *)un->un_asciilabel;
4293 #elif defined(_SUNOS_VTOC_16)
4294 		label = (char *)un->un_vtoc.v_asciilabel;
4295 #else
4296 #error "No VTOC format defined."
4297 #endif
4298 	}
4299 
4300 	/*
4301 	 * If a valid label was not found, AND if no reservation conflict
4302 	 * was detected, then go ahead and create a default label (4069506).
4303 	 */
4304 
4305 	if (un->un_f_default_vtoc_supported && (label_error != EACCES)) {
4306 		if (un->un_f_geometry_is_valid == FALSE) {
4307 			sd_build_default_label(un);
4308 		}
4309 		label_error = 0;
4310 	}
4311 
4312 no_solaris_partition:
4313 	if ((!un->un_f_has_removable_media ||
4314 	    (un->un_f_has_removable_media &&
4315 		un->un_mediastate == DKIO_EJECTED)) &&
4316 		(un->un_state == SD_STATE_NORMAL && !gvalid)) {
4317 		/*
4318 		 * Print out a message indicating who and what we are.
4319 		 * We do this only when we happen to really validate the
4320 		 * geometry. We may call sd_validate_geometry() at other
4321 		 * times, e.g., ioctl()'s like Get VTOC in which case we
4322 		 * don't want to print the label.
4323 		 * If the geometry is valid, print the label string,
4324 		 * else print vendor and product info, if available
4325 		 */
4326 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
4327 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label);
4328 		} else {
4329 			mutex_enter(&sd_label_mutex);
4330 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
4331 			    labelstring);
4332 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
4333 			    &labelstring[64]);
4334 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
4335 			    labelstring, &labelstring[64]);
4336 			if (un->un_f_blockcount_is_valid == TRUE) {
4337 				(void) sprintf(&buf[strlen(buf)],
4338 				    ", %llu %u byte blocks\n",
4339 				    (longlong_t)un->un_blockcount,
4340 				    un->un_tgt_blocksize);
4341 			} else {
4342 				(void) sprintf(&buf[strlen(buf)],
4343 				    ", (unknown capacity)\n");
4344 			}
4345 			SD_INFO(SD_LOG_ATTACH_DETACH, un, buf);
4346 			mutex_exit(&sd_label_mutex);
4347 		}
4348 	}
4349 
4350 #if defined(_SUNOS_VTOC_16)
4351 	/*
4352 	 * If we have valid geometry, set up the remaining fdisk partitions.
4353 	 * Note that dkl_cylno is not used for the fdisk map entries, so
4354 	 * we set it to an entirely bogus value.
4355 	 */
4356 	for (count = 0; count < FD_NUMPART; count++) {
4357 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
4358 		un->un_map[FDISK_P1 + count].dkl_nblk =
4359 		    un->un_fmap[count].fmap_nblk;
4360 
4361 		un->un_offset[FDISK_P1 + count] =
4362 		    un->un_fmap[count].fmap_start;
4363 	}
4364 #endif
4365 
4366 	for (count = 0; count < NDKMAP; count++) {
4367 #if defined(_SUNOS_VTOC_8)
4368 		struct dk_map *lp  = &un->un_map[count];
4369 		un->un_offset[count] =
4370 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
4371 #elif defined(_SUNOS_VTOC_16)
4372 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
4373 
4374 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
4375 #else
4376 #error "No VTOC format defined."
4377 #endif
4378 	}
4379 
4380 	return (label_error);
4381 }
4382 
4383 
4384 #if defined(_SUNOS_VTOC_16)
4385 /*
4386  * Macro: MAX_BLKS
4387  *
4388  *	This macro is used for table entries where we need to have the largest
4389  *	possible sector value for that head & SPT (sectors per track)
4390  *	combination.  Other entries for some smaller disk sizes are set by
4391  *	convention to match those used by X86 BIOS usage.
4392  */
4393 #define	MAX_BLKS(heads, spt)	UINT16_MAX * heads * spt, heads, spt
4394 
4395 /*
4396  *    Function: sd_convert_geometry
4397  *
4398  * Description: Convert physical geometry into a dk_geom structure. In
4399  *		other words, make sure we don't wrap 16-bit values.
4400  *		e.g. converting from geom_cache to dk_geom
4401  *
4402  *     Context: Kernel thread only
4403  */
4404 static void
4405 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g)
4406 {
4407 	int i;
4408 	static const struct chs_values {
4409 		uint_t max_cap;		/* Max Capacity for this HS. */
4410 		uint_t nhead;		/* Heads to use. */
4411 		uint_t nsect;		/* SPT to use. */
4412 	} CHS_values[] = {
4413 		{0x00200000,  64, 32},		/* 1GB or smaller disk. */
4414 		{0x01000000, 128, 32},		/* 8GB or smaller disk. */
4415 		{MAX_BLKS(255,  63)},		/* 502.02GB or smaller disk. */
4416 		{MAX_BLKS(255, 126)},		/* .98TB or smaller disk. */
4417 		{DK_MAX_BLOCKS, 255, 189}	/* Max size is just under 1TB */
4418 	};
4419 
4420 	/* Unlabeled SCSI floppy device */
4421 	if (capacity <= 0x1000) {
4422 		un_g->dkg_nhead = 2;
4423 		un_g->dkg_ncyl = 80;
4424 		un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl);
4425 		return;
4426 	}
4427 
4428 	/*
4429 	 * For all devices we calculate cylinders using the
4430 	 * heads and sectors we assign based on capacity of the
4431 	 * device.  The table is designed to be compatible with the
4432 	 * way other operating systems lay out fdisk tables for X86
4433 	 * and to insure that the cylinders never exceed 65535 to
4434 	 * prevent problems with X86 ioctls that report geometry.
4435 	 * We use SPT that are multiples of 63, since other OSes that
4436 	 * are not limited to 16-bits for cylinders stop at 63 SPT
4437 	 * we make do by using multiples of 63 SPT.
4438 	 *
4439 	 * Note than capacities greater than or equal to 1TB will simply
4440 	 * get the largest geometry from the table. This should be okay
4441 	 * since disks this large shouldn't be using CHS values anyway.
4442 	 */
4443 	for (i = 0; CHS_values[i].max_cap < capacity &&
4444 	    CHS_values[i].max_cap != DK_MAX_BLOCKS; i++)
4445 		;
4446 
4447 	un_g->dkg_nhead = CHS_values[i].nhead;
4448 	un_g->dkg_nsect = CHS_values[i].nsect;
4449 }
4450 #endif
4451 
4452 
4453 /*
4454  *    Function: sd_resync_geom_caches
4455  *
4456  * Description: (Re)initialize both geometry caches: the virtual geometry
4457  *		information is extracted from the HBA (the "geometry"
4458  *		capability), and the physical geometry cache data is
4459  *		generated by issuing MODE SENSE commands.
4460  *
4461  *   Arguments: un - driver soft state (unit) structure
4462  *		capacity - disk capacity in #blocks
4463  *		lbasize - disk block size in bytes
4464  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4465  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4466  *			to use the USCSI "direct" chain and bypass the normal
4467  *			command waitq.
4468  *
4469  *     Context: Kernel thread only (can sleep).
4470  */
4471 
4472 static void
4473 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize,
4474 	int path_flag)
4475 {
4476 	struct 	geom_cache 	pgeom;
4477 	struct 	geom_cache	*pgeom_p = &pgeom;
4478 	int 	spc;
4479 	unsigned short nhead;
4480 	unsigned short nsect;
4481 
4482 	ASSERT(un != NULL);
4483 	ASSERT(mutex_owned(SD_MUTEX(un)));
4484 
4485 	/*
4486 	 * Ask the controller for its logical geometry.
4487 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
4488 	 * then the lgeom cache will be invalid.
4489 	 */
4490 	sd_get_virtual_geometry(un, capacity, lbasize);
4491 
4492 	/*
4493 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
4494 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
4495 	 */
4496 	if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) {
4497 		/*
4498 		 * Note: Perhaps this needs to be more adaptive? The rationale
4499 		 * is that, if there's no HBA geometry from the HBA driver, any
4500 		 * guess is good, since this is the physical geometry. If MODE
4501 		 * SENSE fails this gives a max cylinder size for non-LBA access
4502 		 */
4503 		nhead = 255;
4504 		nsect = 63;
4505 	} else {
4506 		nhead = un->un_lgeom.g_nhead;
4507 		nsect = un->un_lgeom.g_nsect;
4508 	}
4509 
4510 	if (ISCD(un)) {
4511 		pgeom_p->g_nhead = 1;
4512 		pgeom_p->g_nsect = nsect * nhead;
4513 	} else {
4514 		pgeom_p->g_nhead = nhead;
4515 		pgeom_p->g_nsect = nsect;
4516 	}
4517 
4518 	spc = pgeom_p->g_nhead * pgeom_p->g_nsect;
4519 	pgeom_p->g_capacity = capacity;
4520 	pgeom_p->g_ncyl = pgeom_p->g_capacity / spc;
4521 	pgeom_p->g_acyl = 0;
4522 
4523 	/*
4524 	 * Retrieve fresh geometry data from the hardware, stash it
4525 	 * here temporarily before we rebuild the incore label.
4526 	 *
4527 	 * We want to use the MODE SENSE commands to derive the
4528 	 * physical geometry of the device, but if either command
4529 	 * fails, the logical geometry is used as the fallback for
4530 	 * disk label geometry.
4531 	 */
4532 	mutex_exit(SD_MUTEX(un));
4533 	sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag);
4534 	mutex_enter(SD_MUTEX(un));
4535 
4536 	/*
4537 	 * Now update the real copy while holding the mutex. This
4538 	 * way the global copy is never in an inconsistent state.
4539 	 */
4540 	bcopy(pgeom_p, &un->un_pgeom,  sizeof (un->un_pgeom));
4541 
4542 	SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: "
4543 	    "(cached from lgeom)\n");
4544 	SD_INFO(SD_LOG_COMMON, un,
4545 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4546 	    un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl,
4547 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
4548 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
4549 	    "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize,
4550 	    un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv,
4551 	    un->un_pgeom.g_rpm);
4552 }
4553 
4554 
4555 /*
4556  *    Function: sd_read_fdisk
4557  *
4558  * Description: utility routine to read the fdisk table.
4559  *
4560  *   Arguments: un - driver soft state (unit) structure
4561  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4562  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4563  *			to use the USCSI "direct" chain and bypass the normal
4564  *			command waitq.
4565  *
4566  * Return Code: SD_CMD_SUCCESS
4567  *		SD_CMD_FAILURE
4568  *
4569  *     Context: Kernel thread only (can sleep).
4570  */
4571 /* ARGSUSED */
4572 static int
4573 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag)
4574 {
4575 #if defined(_NO_FDISK_PRESENT)
4576 
4577 	un->un_solaris_offset = 0;
4578 	un->un_solaris_size = capacity;
4579 	bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4580 	return (SD_CMD_SUCCESS);
4581 
4582 #elif defined(_FIRMWARE_NEEDS_FDISK)
4583 
4584 	struct ipart	*fdp;
4585 	struct mboot	*mbp;
4586 	struct ipart	fdisk[FD_NUMPART];
4587 	int		i;
4588 	char		sigbuf[2];
4589 	caddr_t		bufp;
4590 	int		uidx;
4591 	int		rval;
4592 	int		lba = 0;
4593 	uint_t		solaris_offset;	/* offset to solaris part. */
4594 	daddr_t		solaris_size;	/* size of solaris partition */
4595 	uint32_t	blocksize;
4596 
4597 	ASSERT(un != NULL);
4598 	ASSERT(mutex_owned(SD_MUTEX(un)));
4599 	ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE);
4600 
4601 	blocksize = un->un_tgt_blocksize;
4602 
4603 	/*
4604 	 * Start off assuming no fdisk table
4605 	 */
4606 	solaris_offset = 0;
4607 	solaris_size   = capacity;
4608 
4609 	mutex_exit(SD_MUTEX(un));
4610 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
4611 	rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag);
4612 	mutex_enter(SD_MUTEX(un));
4613 
4614 	if (rval != 0) {
4615 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4616 		    "sd_read_fdisk: fdisk read err\n");
4617 		kmem_free(bufp, blocksize);
4618 		return (SD_CMD_FAILURE);
4619 	}
4620 
4621 	mbp = (struct mboot *)bufp;
4622 
4623 	/*
4624 	 * The fdisk table does not begin on a 4-byte boundary within the
4625 	 * master boot record, so we copy it to an aligned structure to avoid
4626 	 * alignment exceptions on some processors.
4627 	 */
4628 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
4629 
4630 	/*
4631 	 * Check for lba support before verifying sig; sig might not be
4632 	 * there, say on a blank disk, but the max_chs mark may still
4633 	 * be present.
4634 	 *
4635 	 * Note: LBA support and BEFs are an x86-only concept but this
4636 	 * code should work OK on SPARC as well.
4637 	 */
4638 
4639 	/*
4640 	 * First, check for lba-access-ok on root node (or prom root node)
4641 	 * if present there, don't need to search fdisk table.
4642 	 */
4643 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
4644 	    "lba-access-ok", 0) != 0) {
4645 		/* All drives do LBA; don't search fdisk table */
4646 		lba = 1;
4647 	} else {
4648 		/* Okay, look for mark in fdisk table */
4649 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4650 			/* accumulate "lba" value from all partitions */
4651 			lba = (lba || sd_has_max_chs_vals(fdp));
4652 		}
4653 	}
4654 
4655 	if (lba != 0) {
4656 		dev_t dev = sd_make_device(SD_DEVINFO(un));
4657 
4658 		if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS,
4659 		    "lba-access-ok", 0) == 0) {
4660 			/* not found; create it */
4661 			if (ddi_prop_create(dev, SD_DEVINFO(un), 0,
4662 			    "lba-access-ok", (caddr_t)NULL, 0) !=
4663 			    DDI_PROP_SUCCESS) {
4664 				SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4665 				    "sd_read_fdisk: Can't create lba property "
4666 				    "for instance %d\n",
4667 				    ddi_get_instance(SD_DEVINFO(un)));
4668 			}
4669 		}
4670 	}
4671 
4672 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
4673 
4674 	/*
4675 	 * Endian-independent signature check
4676 	 */
4677 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
4678 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
4679 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4680 		    "sd_read_fdisk: no fdisk\n");
4681 		bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4682 		rval = SD_CMD_SUCCESS;
4683 		goto done;
4684 	}
4685 
4686 #ifdef SDDEBUG
4687 	if (sd_level_mask & SD_LOGMASK_INFO) {
4688 		fdp = fdisk;
4689 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n");
4690 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "         relsect    "
4691 		    "numsect         sysid       bootid\n");
4692 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
4693 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4694 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
4695 			    i, fdp->relsect, fdp->numsect,
4696 			    fdp->systid, fdp->bootid);
4697 		}
4698 	}
4699 #endif
4700 
4701 	/*
4702 	 * Try to find the unix partition
4703 	 */
4704 	uidx = -1;
4705 	solaris_offset = 0;
4706 	solaris_size   = 0;
4707 
4708 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4709 		int	relsect;
4710 		int	numsect;
4711 
4712 		if (fdp->numsect == 0) {
4713 			un->un_fmap[i].fmap_start = 0;
4714 			un->un_fmap[i].fmap_nblk  = 0;
4715 			continue;
4716 		}
4717 
4718 		/*
4719 		 * Data in the fdisk table is little-endian.
4720 		 */
4721 		relsect = LE_32(fdp->relsect);
4722 		numsect = LE_32(fdp->numsect);
4723 
4724 		un->un_fmap[i].fmap_start = relsect;
4725 		un->un_fmap[i].fmap_nblk  = numsect;
4726 
4727 		if (fdp->systid != SUNIXOS &&
4728 		    fdp->systid != SUNIXOS2 &&
4729 		    fdp->systid != EFI_PMBR) {
4730 			continue;
4731 		}
4732 
4733 		/*
4734 		 * use the last active solaris partition id found
4735 		 * (there should only be 1 active partition id)
4736 		 *
4737 		 * if there are no active solaris partition id
4738 		 * then use the first inactive solaris partition id
4739 		 */
4740 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
4741 			uidx = i;
4742 			solaris_offset = relsect;
4743 			solaris_size   = numsect;
4744 		}
4745 	}
4746 
4747 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx",
4748 	    un->un_solaris_offset, un->un_solaris_size);
4749 
4750 	rval = SD_CMD_SUCCESS;
4751 
4752 done:
4753 
4754 	/*
4755 	 * Clear the VTOC info, only if the Solaris partition entry
4756 	 * has moved, changed size, been deleted, or if the size of
4757 	 * the partition is too small to even fit the label sector.
4758 	 */
4759 	if ((un->un_solaris_offset != solaris_offset) ||
4760 	    (un->un_solaris_size != solaris_size) ||
4761 	    solaris_size <= DK_LABEL_LOC) {
4762 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx",
4763 			solaris_offset, solaris_size);
4764 		bzero(&un->un_g, sizeof (struct dk_geom));
4765 		bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
4766 		bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
4767 		un->un_f_geometry_is_valid = FALSE;
4768 	}
4769 	un->un_solaris_offset = solaris_offset;
4770 	un->un_solaris_size = solaris_size;
4771 	kmem_free(bufp, blocksize);
4772 	return (rval);
4773 
4774 #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
4775 #error "fdisk table presence undetermined for this platform."
4776 #endif	/* #if defined(_NO_FDISK_PRESENT) */
4777 }
4778 
4779 
4780 /*
4781  *    Function: sd_get_physical_geometry
4782  *
4783  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4784  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4785  *		target, and use this information to initialize the physical
4786  *		geometry cache specified by pgeom_p.
4787  *
4788  *		MODE SENSE is an optional command, so failure in this case
4789  *		does not necessarily denote an error. We want to use the
4790  *		MODE SENSE commands to derive the physical geometry of the
4791  *		device, but if either command fails, the logical geometry is
4792  *		used as the fallback for disk label geometry.
4793  *
4794  *		This requires that un->un_blockcount and un->un_tgt_blocksize
4795  *		have already been initialized for the current target and
4796  *		that the current values be passed as args so that we don't
4797  *		end up ever trying to use -1 as a valid value. This could
4798  *		happen if either value is reset while we're not holding
4799  *		the mutex.
4800  *
4801  *   Arguments: un - driver soft state (unit) structure
4802  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4803  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4804  *			to use the USCSI "direct" chain and bypass the normal
4805  *			command waitq.
4806  *
4807  *     Context: Kernel thread only (can sleep).
4808  */
4809 
4810 static void
4811 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p,
4812 	int capacity, int lbasize, int path_flag)
4813 {
4814 	struct	mode_format	*page3p;
4815 	struct	mode_geometry	*page4p;
4816 	struct	mode_header	*headerp;
4817 	int	sector_size;
4818 	int	nsect;
4819 	int	nhead;
4820 	int	ncyl;
4821 	int	intrlv;
4822 	int	spc;
4823 	int	modesense_capacity;
4824 	int	rpm;
4825 	int	bd_len;
4826 	int	mode_header_length;
4827 	uchar_t	*p3bufp;
4828 	uchar_t	*p4bufp;
4829 	int	cdbsize;
4830 
4831 	ASSERT(un != NULL);
4832 	ASSERT(!(mutex_owned(SD_MUTEX(un))));
4833 
4834 	if (un->un_f_blockcount_is_valid != TRUE) {
4835 		return;
4836 	}
4837 
4838 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
4839 		return;
4840 	}
4841 
4842 	if (lbasize == 0) {
4843 		if (ISCD(un)) {
4844 			lbasize = 2048;
4845 		} else {
4846 			lbasize = un->un_sys_blocksize;
4847 		}
4848 	}
4849 	pgeom_p->g_secsize = (unsigned short)lbasize;
4850 
4851 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4852 
4853 	/*
4854 	 * Retrieve MODE SENSE page 3 - Format Device Page
4855 	 */
4856 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4857 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp,
4858 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag)
4859 	    != 0) {
4860 		SD_ERROR(SD_LOG_COMMON, un,
4861 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
4862 		goto page3_exit;
4863 	}
4864 
4865 	/*
4866 	 * Determine size of Block Descriptors in order to locate the mode
4867 	 * page data.  ATAPI devices return 0, SCSI devices should return
4868 	 * MODE_BLK_DESC_LENGTH.
4869 	 */
4870 	headerp = (struct mode_header *)p3bufp;
4871 	if (un->un_f_cfg_is_atapi == TRUE) {
4872 		struct mode_header_grp2 *mhp =
4873 		    (struct mode_header_grp2 *)headerp;
4874 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
4875 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4876 	} else {
4877 		mode_header_length = MODE_HEADER_LENGTH;
4878 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
4879 	}
4880 
4881 	if (bd_len > MODE_BLK_DESC_LENGTH) {
4882 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4883 		    "received unexpected bd_len of %d, page3\n", bd_len);
4884 		goto page3_exit;
4885 	}
4886 
4887 	page3p = (struct mode_format *)
4888 	    ((caddr_t)headerp + mode_header_length + bd_len);
4889 
4890 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
4891 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4892 		    "mode sense pg3 code mismatch %d\n",
4893 		    page3p->mode_page.code);
4894 		goto page3_exit;
4895 	}
4896 
4897 	/*
4898 	 * Use this physical geometry data only if BOTH MODE SENSE commands
4899 	 * complete successfully; otherwise, revert to the logical geometry.
4900 	 * So, we need to save everything in temporary variables.
4901 	 */
4902 	sector_size = BE_16(page3p->data_bytes_sect);
4903 
4904 	/*
4905 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
4906 	 */
4907 	if (sector_size == 0) {
4908 		sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize;
4909 	} else {
4910 		sector_size &= ~(un->un_sys_blocksize - 1);
4911 	}
4912 
4913 	nsect  = BE_16(page3p->sect_track);
4914 	intrlv = BE_16(page3p->interleave);
4915 
4916 	SD_INFO(SD_LOG_COMMON, un,
4917 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
4918 	SD_INFO(SD_LOG_COMMON, un,
4919 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
4920 	    page3p->mode_page.code, nsect, sector_size);
4921 	SD_INFO(SD_LOG_COMMON, un,
4922 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
4923 	    BE_16(page3p->track_skew),
4924 	    BE_16(page3p->cylinder_skew));
4925 
4926 
4927 	/*
4928 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
4929 	 */
4930 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
4931 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp,
4932 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag)
4933 	    != 0) {
4934 		SD_ERROR(SD_LOG_COMMON, un,
4935 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
4936 		goto page4_exit;
4937 	}
4938 
4939 	/*
4940 	 * Determine size of Block Descriptors in order to locate the mode
4941 	 * page data.  ATAPI devices return 0, SCSI devices should return
4942 	 * MODE_BLK_DESC_LENGTH.
4943 	 */
4944 	headerp = (struct mode_header *)p4bufp;
4945 	if (un->un_f_cfg_is_atapi == TRUE) {
4946 		struct mode_header_grp2 *mhp =
4947 		    (struct mode_header_grp2 *)headerp;
4948 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4949 	} else {
4950 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
4951 	}
4952 
4953 	if (bd_len > MODE_BLK_DESC_LENGTH) {
4954 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4955 		    "received unexpected bd_len of %d, page4\n", bd_len);
4956 		goto page4_exit;
4957 	}
4958 
4959 	page4p = (struct mode_geometry *)
4960 	    ((caddr_t)headerp + mode_header_length + bd_len);
4961 
4962 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
4963 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4964 		    "mode sense pg4 code mismatch %d\n",
4965 		    page4p->mode_page.code);
4966 		goto page4_exit;
4967 	}
4968 
4969 	/*
4970 	 * Stash the data now, after we know that both commands completed.
4971 	 */
4972 
4973 	mutex_enter(SD_MUTEX(un));
4974 
4975 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
4976 	spc   = nhead * nsect;
4977 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
4978 	rpm   = BE_16(page4p->rpm);
4979 
4980 	modesense_capacity = spc * ncyl;
4981 
4982 	SD_INFO(SD_LOG_COMMON, un,
4983 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
4984 	SD_INFO(SD_LOG_COMMON, un,
4985 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
4986 	SD_INFO(SD_LOG_COMMON, un,
4987 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
4988 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
4989 	    (void *)pgeom_p, capacity);
4990 
4991 	/*
4992 	 * Compensate if the drive's geometry is not rectangular, i.e.,
4993 	 * the product of C * H * S returned by MODE SENSE >= that returned
4994 	 * by read capacity. This is an idiosyncrasy of the original x86
4995 	 * disk subsystem.
4996 	 */
4997 	if (modesense_capacity >= capacity) {
4998 		SD_INFO(SD_LOG_COMMON, un,
4999 		    "sd_get_physical_geometry: adjusting acyl; "
5000 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5001 		    (modesense_capacity - capacity + spc - 1) / spc);
5002 		if (sector_size != 0) {
5003 			/* 1243403: NEC D38x7 drives don't support sec size */
5004 			pgeom_p->g_secsize = (unsigned short)sector_size;
5005 		}
5006 		pgeom_p->g_nsect    = (unsigned short)nsect;
5007 		pgeom_p->g_nhead    = (unsigned short)nhead;
5008 		pgeom_p->g_capacity = capacity;
5009 		pgeom_p->g_acyl	    =
5010 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5011 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5012 	}
5013 
5014 	pgeom_p->g_rpm    = (unsigned short)rpm;
5015 	pgeom_p->g_intrlv = (unsigned short)intrlv;
5016 
5017 	SD_INFO(SD_LOG_COMMON, un,
5018 	    "sd_get_physical_geometry: mode sense geometry:\n");
5019 	SD_INFO(SD_LOG_COMMON, un,
5020 	    "   nsect: %d; sector size: %d; interlv: %d\n",
5021 	    nsect, sector_size, intrlv);
5022 	SD_INFO(SD_LOG_COMMON, un,
5023 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5024 	    nhead, ncyl, rpm, modesense_capacity);
5025 	SD_INFO(SD_LOG_COMMON, un,
5026 	    "sd_get_physical_geometry: (cached)\n");
5027 	SD_INFO(SD_LOG_COMMON, un,
5028 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5029 	    un->un_pgeom.g_ncyl,  un->un_pgeom.g_acyl,
5030 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
5031 	SD_INFO(SD_LOG_COMMON, un,
5032 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5033 	    un->un_pgeom.g_secsize, un->un_pgeom.g_capacity,
5034 	    un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm);
5035 
5036 	mutex_exit(SD_MUTEX(un));
5037 
5038 page4_exit:
5039 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5040 page3_exit:
5041 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5042 }
5043 
5044 
5045 /*
5046  *    Function: sd_get_virtual_geometry
5047  *
5048  * Description: Ask the controller to tell us about the target device.
5049  *
5050  *   Arguments: un - pointer to softstate
5051  *		capacity - disk capacity in #blocks
5052  *		lbasize - disk block size in bytes
5053  *
5054  *     Context: Kernel thread only
5055  */
5056 
5057 static void
5058 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize)
5059 {
5060 	struct	geom_cache 	*lgeom_p = &un->un_lgeom;
5061 	uint_t	geombuf;
5062 	int	spc;
5063 
5064 	ASSERT(un != NULL);
5065 	ASSERT(mutex_owned(SD_MUTEX(un)));
5066 
5067 	mutex_exit(SD_MUTEX(un));
5068 
5069 	/* Set sector size, and total number of sectors */
5070 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5071 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5072 
5073 	/* Let the HBA tell us its geometry */
5074 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5075 
5076 	mutex_enter(SD_MUTEX(un));
5077 
5078 	/* A value of -1 indicates an undefined "geometry" property */
5079 	if (geombuf == (-1)) {
5080 		return;
5081 	}
5082 
5083 	/* Initialize the logical geometry cache. */
5084 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5085 	lgeom_p->g_nsect   = geombuf & 0xffff;
5086 	lgeom_p->g_secsize = un->un_sys_blocksize;
5087 
5088 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5089 
5090 	/*
5091 	 * Note: The driver originally converted the capacity value from
5092 	 * target blocks to system blocks. However, the capacity value passed
5093 	 * to this routine is already in terms of system blocks (this scaling
5094 	 * is done when the READ CAPACITY command is issued and processed).
5095 	 * This 'error' may have gone undetected because the usage of g_ncyl
5096 	 * (which is based upon g_capacity) is very limited within the driver
5097 	 */
5098 	lgeom_p->g_capacity = capacity;
5099 
5100 	/*
5101 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5102 	 * hba may return zero values if the device has been removed.
5103 	 */
5104 	if (spc == 0) {
5105 		lgeom_p->g_ncyl = 0;
5106 	} else {
5107 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5108 	}
5109 	lgeom_p->g_acyl = 0;
5110 
5111 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5112 	SD_INFO(SD_LOG_COMMON, un,
5113 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5114 	    un->un_lgeom.g_ncyl,  un->un_lgeom.g_acyl,
5115 	    un->un_lgeom.g_nhead, un->un_lgeom.g_nsect);
5116 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
5117 	    "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize,
5118 	    un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm);
5119 }
5120 
5121 
5122 /*
5123  *    Function: sd_update_block_info
5124  *
5125  * Description: Calculate a byte count to sector count bitshift value
5126  *		from sector size.
5127  *
5128  *   Arguments: un: unit struct.
5129  *		lbasize: new target sector size
5130  *		capacity: new target capacity, ie. block count
5131  *
5132  *     Context: Kernel thread context
5133  */
5134 
5135 static void
5136 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5137 {
5138 	if (lbasize != 0) {
5139 		un->un_tgt_blocksize = lbasize;
5140 		un->un_f_tgt_blocksize_is_valid	= TRUE;
5141 	}
5142 
5143 	if (capacity != 0) {
5144 		un->un_blockcount		= capacity;
5145 		un->un_f_blockcount_is_valid	= TRUE;
5146 	}
5147 }
5148 
5149 
5150 static void
5151 sd_swap_efi_gpt(efi_gpt_t *e)
5152 {
5153 	_NOTE(ASSUMING_PROTECTED(*e))
5154 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
5155 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
5156 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
5157 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
5158 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
5159 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
5160 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
5161 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
5162 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
5163 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
5164 	e->efi_gpt_NumberOfPartitionEntries =
5165 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
5166 	e->efi_gpt_SizeOfPartitionEntry =
5167 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
5168 	e->efi_gpt_PartitionEntryArrayCRC32 =
5169 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
5170 }
5171 
5172 static void
5173 sd_swap_efi_gpe(int nparts, efi_gpe_t *p)
5174 {
5175 	int i;
5176 
5177 	_NOTE(ASSUMING_PROTECTED(*p))
5178 	for (i = 0; i < nparts; i++) {
5179 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
5180 		    p[i].efi_gpe_PartitionTypeGUID);
5181 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
5182 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
5183 		/* PartitionAttrs */
5184 	}
5185 }
5186 
5187 static int
5188 sd_validate_efi(efi_gpt_t *labp)
5189 {
5190 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
5191 		return (EINVAL);
5192 	/* at least 96 bytes in this version of the spec. */
5193 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
5194 	    labp->efi_gpt_HeaderSize)
5195 		return (EINVAL);
5196 	/* this should be 128 bytes */
5197 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
5198 		return (EINVAL);
5199 	return (0);
5200 }
5201 
5202 static int
5203 sd_use_efi(struct sd_lun *un, int path_flag)
5204 {
5205 	int		i;
5206 	int		rval = 0;
5207 	efi_gpe_t	*partitions;
5208 	uchar_t		*buf;
5209 	uint_t		lbasize;
5210 	uint64_t	cap;
5211 	uint_t		nparts;
5212 	diskaddr_t	gpe_lba;
5213 
5214 	ASSERT(mutex_owned(SD_MUTEX(un)));
5215 	lbasize = un->un_tgt_blocksize;
5216 
5217 	mutex_exit(SD_MUTEX(un));
5218 
5219 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
5220 
5221 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
5222 		rval = EINVAL;
5223 		goto done_err;
5224 	}
5225 
5226 	rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag);
5227 	if (rval) {
5228 		goto done_err;
5229 	}
5230 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
5231 		/* not ours */
5232 		rval = ESRCH;
5233 		goto done_err;
5234 	}
5235 
5236 	rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag);
5237 	if (rval) {
5238 		goto done_err;
5239 	}
5240 	sd_swap_efi_gpt((efi_gpt_t *)buf);
5241 
5242 	if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) {
5243 		/*
5244 		 * Couldn't read the primary, try the backup.  Our
5245 		 * capacity at this point could be based on CHS, so
5246 		 * check what the device reports.
5247 		 */
5248 		rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
5249 		    path_flag);
5250 		if (rval) {
5251 			goto done_err;
5252 		}
5253 
5254 		/*
5255 		 * The MMC standard allows READ CAPACITY to be
5256 		 * inaccurate by a bounded amount (in the interest of
5257 		 * response latency).  As a result, failed READs are
5258 		 * commonplace (due to the reading of metadata and not
5259 		 * data). Depending on the per-Vendor/drive Sense data,
5260 		 * the failed READ can cause many (unnecessary) retries.
5261 		 */
5262 		if ((rval = sd_send_scsi_READ(un, buf, lbasize,
5263 		    cap - 1, (ISCD(un)) ? SD_PATH_DIRECT_PRIORITY :
5264 			path_flag)) != 0) {
5265 				goto done_err;
5266 		}
5267 
5268 		sd_swap_efi_gpt((efi_gpt_t *)buf);
5269 		if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0)
5270 			goto done_err;
5271 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5272 		    "primary label corrupt; using backup\n");
5273 	}
5274 
5275 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
5276 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
5277 
5278 	rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba,
5279 	    path_flag);
5280 	if (rval) {
5281 		goto done_err;
5282 	}
5283 	partitions = (efi_gpe_t *)buf;
5284 
5285 	if (nparts > MAXPART) {
5286 		nparts = MAXPART;
5287 	}
5288 	sd_swap_efi_gpe(nparts, partitions);
5289 
5290 	mutex_enter(SD_MUTEX(un));
5291 
5292 	/* Fill in partition table. */
5293 	for (i = 0; i < nparts; i++) {
5294 		if (partitions->efi_gpe_StartingLBA != 0 ||
5295 		    partitions->efi_gpe_EndingLBA != 0) {
5296 			un->un_map[i].dkl_cylno =
5297 			    partitions->efi_gpe_StartingLBA;
5298 			un->un_map[i].dkl_nblk =
5299 			    partitions->efi_gpe_EndingLBA -
5300 			    partitions->efi_gpe_StartingLBA + 1;
5301 			un->un_offset[i] =
5302 			    partitions->efi_gpe_StartingLBA;
5303 		}
5304 		if (i == WD_NODE) {
5305 			/*
5306 			 * minor number 7 corresponds to the whole disk
5307 			 */
5308 			un->un_map[i].dkl_cylno = 0;
5309 			un->un_map[i].dkl_nblk = un->un_blockcount;
5310 			un->un_offset[i] = 0;
5311 		}
5312 		partitions++;
5313 	}
5314 	un->un_solaris_offset = 0;
5315 	un->un_solaris_size = cap;
5316 	un->un_f_geometry_is_valid = TRUE;
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 				 * The following relies on
8284 				 * sd_send_scsi_READ_CAPACITY never
8285 				 * returning 0 for capacity and/or lbasize.
8286 				 */
8287 				sd_update_block_info(un, lbasize, capacity);
8288 
8289 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8290 				    "sd_unit_attach: un:0x%p capacity = %ld "
8291 				    "blocks; lbasize= %ld.\n", un,
8292 				    un->un_blockcount, un->un_tgt_blocksize);
8293 
8294 				break;
8295 			}
8296 			case EACCES:
8297 				/*
8298 				 * Should never get here if the spin-up
8299 				 * succeeded, but code it in anyway.
8300 				 * From here, just continue with the attach...
8301 				 */
8302 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8303 				    "sd_unit_attach: un:0x%p "
8304 				    "sd_send_scsi_READ_CAPACITY "
8305 				    "returned reservation conflict\n", un);
8306 				reservation_flag = SD_TARGET_IS_RESERVED;
8307 				break;
8308 			default:
8309 				/*
8310 				 * Likewise, should never get here if the
8311 				 * spin-up succeeded. Just continue with
8312 				 * the attach...
8313 				 */
8314 				break;
8315 			}
8316 			break;
8317 		case EACCES:
8318 			/*
8319 			 * Device is reserved by another host.  In this case
8320 			 * we could not spin it up or read the capacity, but
8321 			 * we continue with the attach anyway.
8322 			 */
8323 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8324 			    "sd_unit_attach: un:0x%p spin-up reservation "
8325 			    "conflict.\n", un);
8326 			reservation_flag = SD_TARGET_IS_RESERVED;
8327 			break;
8328 		default:
8329 			/* Fail the attach if the spin-up failed. */
8330 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8331 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8332 			goto spinup_failed;
8333 		}
8334 	}
8335 
8336 	/*
8337 	 * Check to see if this is a MMC drive
8338 	 */
8339 	if (ISCD(un)) {
8340 		sd_set_mmc_caps(un);
8341 	}
8342 
8343 	/*
8344 	 * Create the minor nodes for the device.
8345 	 * Note: If we want to support fdisk on both sparc and intel, this will
8346 	 * have to separate out the notion that VTOC8 is always sparc, and
8347 	 * VTOC16 is always intel (tho these can be the defaults).  The vtoc
8348 	 * type will have to be determined at run-time, and the fdisk
8349 	 * partitioning will have to have been read & set up before we
8350 	 * create the minor nodes. (any other inits (such as kstats) that
8351 	 * also ought to be done before creating the minor nodes?) (Doesn't
8352 	 * setting up the minor nodes kind of imply that we're ready to
8353 	 * handle an open from userland?)
8354 	 */
8355 	if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) {
8356 		goto create_minor_nodes_failed;
8357 	}
8358 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8359 	    "sd_unit_attach: un:0x%p minor nodes created\n", un);
8360 
8361 	/*
8362 	 * Add a zero-length attribute to tell the world we support
8363 	 * kernel ioctls (for layered drivers)
8364 	 */
8365 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8366 	    DDI_KERNEL_IOCTL, NULL, 0);
8367 
8368 	/*
8369 	 * Add a boolean property to tell the world we support
8370 	 * the B_FAILFAST flag (for layered drivers)
8371 	 */
8372 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8373 	    "ddi-failfast-supported", NULL, 0);
8374 
8375 	/*
8376 	 * Initialize power management
8377 	 */
8378 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8379 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8380 	sd_setup_pm(un, devi);
8381 	if (un->un_f_pm_is_enabled == FALSE) {
8382 		/*
8383 		 * For performance, point to a jump table that does
8384 		 * not include pm.
8385 		 * The direct and priority chains don't change with PM.
8386 		 *
8387 		 * Note: this is currently done based on individual device
8388 		 * capabilities. When an interface for determining system
8389 		 * power enabled state becomes available, or when additional
8390 		 * layers are added to the command chain, these values will
8391 		 * have to be re-evaluated for correctness.
8392 		 */
8393 		if (un->un_f_non_devbsize_supported) {
8394 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8395 		} else {
8396 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8397 		}
8398 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8399 	}
8400 
8401 	/*
8402 	 * This property is set to 0 by HA software to avoid retries
8403 	 * on a reserved disk. (The preferred property name is
8404 	 * "retry-on-reservation-conflict") (1189689)
8405 	 *
8406 	 * Note: The use of a global here can have unintended consequences. A
8407 	 * per instance variable is preferrable to match the capabilities of
8408 	 * different underlying hba's (4402600)
8409 	 */
8410 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8411 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8412 	    sd_retry_on_reservation_conflict);
8413 	if (sd_retry_on_reservation_conflict != 0) {
8414 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8415 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8416 		    sd_retry_on_reservation_conflict);
8417 	}
8418 
8419 	/* Set up options for QFULL handling. */
8420 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8421 	    "qfull-retries", -1)) != -1) {
8422 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8423 		    rval, 1);
8424 	}
8425 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8426 	    "qfull-retry-interval", -1)) != -1) {
8427 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8428 		    rval, 1);
8429 	}
8430 
8431 	/*
8432 	 * This just prints a message that announces the existence of the
8433 	 * device. The message is always printed in the system logfile, but
8434 	 * only appears on the console if the system is booted with the
8435 	 * -v (verbose) argument.
8436 	 */
8437 	ddi_report_dev(devi);
8438 
8439 	/*
8440 	 * The framework calls driver attach routines single-threaded
8441 	 * for a given instance.  However we still acquire SD_MUTEX here
8442 	 * because this required for calling the sd_validate_geometry()
8443 	 * and sd_register_devid() functions.
8444 	 */
8445 	mutex_enter(SD_MUTEX(un));
8446 	un->un_f_geometry_is_valid = FALSE;
8447 	un->un_mediastate = DKIO_NONE;
8448 	un->un_reserved = -1;
8449 
8450 	/*
8451 	 * Read and validate the device's geometry (ie, disk label)
8452 	 * A new unformatted drive will not have a valid geometry, but
8453 	 * the driver needs to successfully attach to this device so
8454 	 * the drive can be formatted via ioctls.
8455 	 */
8456 	if (((sd_validate_geometry(un, SD_PATH_DIRECT) ==
8457 	    ENOTSUP)) &&
8458 	    (un->un_blockcount < DK_MAX_BLOCKS)) {
8459 		/*
8460 		 * We found a small disk with an EFI label on it;
8461 		 * we need to fix up the minor nodes accordingly.
8462 		 */
8463 		ddi_remove_minor_node(devi, "h");
8464 		ddi_remove_minor_node(devi, "h,raw");
8465 		(void) ddi_create_minor_node(devi, "wd",
8466 		    S_IFBLK,
8467 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8468 		    un->un_node_type, NULL);
8469 		(void) ddi_create_minor_node(devi, "wd,raw",
8470 		    S_IFCHR,
8471 		    (instance << SDUNIT_SHIFT) | WD_NODE,
8472 		    un->un_node_type, NULL);
8473 	}
8474 
8475 	/*
8476 	 * Read and initialize the devid for the unit.
8477 	 */
8478 	ASSERT(un->un_errstats != NULL);
8479 	if (un->un_f_devid_supported) {
8480 		sd_register_devid(un, devi, reservation_flag);
8481 	}
8482 	mutex_exit(SD_MUTEX(un));
8483 
8484 #if (defined(__fibre))
8485 	/*
8486 	 * Register callbacks for fibre only.  You can't do this soley
8487 	 * on the basis of the devid_type because this is hba specific.
8488 	 * We need to query our hba capabilities to find out whether to
8489 	 * register or not.
8490 	 */
8491 	if (un->un_f_is_fibre) {
8492 	    if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8493 		sd_init_event_callbacks(un);
8494 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8495 		    "sd_unit_attach: un:0x%p event callbacks inserted", un);
8496 	    }
8497 	}
8498 #endif
8499 
8500 	if (un->un_f_opt_disable_cache == TRUE) {
8501 		/*
8502 		 * Disable both read cache and write cache.  This is
8503 		 * the historic behavior of the keywords in the config file.
8504 		 */
8505 		if (sd_cache_control(un, SD_CACHE_DISABLE, SD_CACHE_DISABLE) !=
8506 		    0) {
8507 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8508 			    "sd_unit_attach: un:0x%p Could not disable "
8509 			    "caching", un);
8510 			goto devid_failed;
8511 		}
8512 	}
8513 
8514 	/*
8515 	 * Check the value of the WCE bit now and
8516 	 * set un_f_write_cache_enabled accordingly.
8517 	 */
8518 	(void) sd_get_write_cache_enabled(un, &wc_enabled);
8519 	mutex_enter(SD_MUTEX(un));
8520 	un->un_f_write_cache_enabled = (wc_enabled != 0);
8521 	mutex_exit(SD_MUTEX(un));
8522 
8523 	/*
8524 	 * Set the pstat and error stat values here, so data obtained during the
8525 	 * previous attach-time routines is available.
8526 	 *
8527 	 * Note: This is a critical sequence that needs to be maintained:
8528 	 *	1) Instantiate the kstats before any routines using the iopath
8529 	 *	   (i.e. sd_send_scsi_cmd).
8530 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8531 	 *	   stats (sd_set_pstats)here, following sd_validate_geometry(),
8532 	 *	   sd_register_devid(), and sd_cache_control().
8533 	 */
8534 	if (un->un_f_pkstats_enabled) {
8535 		sd_set_pstats(un);
8536 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8537 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8538 	}
8539 
8540 	sd_set_errstats(un);
8541 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8542 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8543 
8544 	/*
8545 	 * Find out what type of reservation this disk supports.
8546 	 */
8547 	switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) {
8548 	case 0:
8549 		/*
8550 		 * SCSI-3 reservations are supported.
8551 		 */
8552 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8553 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8554 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8555 		break;
8556 	case ENOTSUP:
8557 		/*
8558 		 * The PERSISTENT RESERVE IN command would not be recognized by
8559 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8560 		 */
8561 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8562 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8563 		un->un_reservation_type = SD_SCSI2_RESERVATION;
8564 		break;
8565 	default:
8566 		/*
8567 		 * default to SCSI-3 reservations
8568 		 */
8569 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8570 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8571 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8572 		break;
8573 	}
8574 
8575 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8576 	    "sd_unit_attach: un:0x%p exit success\n", un);
8577 
8578 	return (DDI_SUCCESS);
8579 
8580 	/*
8581 	 * An error occurred during the attach; clean up & return failure.
8582 	 */
8583 
8584 devid_failed:
8585 
8586 setup_pm_failed:
8587 	ddi_remove_minor_node(devi, NULL);
8588 
8589 create_minor_nodes_failed:
8590 	/*
8591 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8592 	 */
8593 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8594 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8595 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8596 
8597 	if (un->un_f_is_fibre == FALSE) {
8598 	    (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8599 	}
8600 
8601 spinup_failed:
8602 
8603 	mutex_enter(SD_MUTEX(un));
8604 
8605 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8606 	if (un->un_direct_priority_timeid != NULL) {
8607 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8608 		un->un_direct_priority_timeid = NULL;
8609 		mutex_exit(SD_MUTEX(un));
8610 		(void) untimeout(temp_id);
8611 		mutex_enter(SD_MUTEX(un));
8612 	}
8613 
8614 	/* Cancel any pending start/stop timeouts */
8615 	if (un->un_startstop_timeid != NULL) {
8616 		timeout_id_t temp_id = un->un_startstop_timeid;
8617 		un->un_startstop_timeid = NULL;
8618 		mutex_exit(SD_MUTEX(un));
8619 		(void) untimeout(temp_id);
8620 		mutex_enter(SD_MUTEX(un));
8621 	}
8622 
8623 	/* Cancel any pending reset-throttle timeouts */
8624 	if (un->un_reset_throttle_timeid != NULL) {
8625 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8626 		un->un_reset_throttle_timeid = NULL;
8627 		mutex_exit(SD_MUTEX(un));
8628 		(void) untimeout(temp_id);
8629 		mutex_enter(SD_MUTEX(un));
8630 	}
8631 
8632 	/* Cancel any pending retry timeouts */
8633 	if (un->un_retry_timeid != NULL) {
8634 		timeout_id_t temp_id = un->un_retry_timeid;
8635 		un->un_retry_timeid = NULL;
8636 		mutex_exit(SD_MUTEX(un));
8637 		(void) untimeout(temp_id);
8638 		mutex_enter(SD_MUTEX(un));
8639 	}
8640 
8641 	/* Cancel any pending delayed cv broadcast timeouts */
8642 	if (un->un_dcvb_timeid != NULL) {
8643 		timeout_id_t temp_id = un->un_dcvb_timeid;
8644 		un->un_dcvb_timeid = NULL;
8645 		mutex_exit(SD_MUTEX(un));
8646 		(void) untimeout(temp_id);
8647 		mutex_enter(SD_MUTEX(un));
8648 	}
8649 
8650 	mutex_exit(SD_MUTEX(un));
8651 
8652 	/* There should not be any in-progress I/O so ASSERT this check */
8653 	ASSERT(un->un_ncmds_in_transport == 0);
8654 	ASSERT(un->un_ncmds_in_driver == 0);
8655 
8656 	/* Do not free the softstate if the callback routine is active */
8657 	sd_sync_with_callback(un);
8658 
8659 	/*
8660 	 * Partition stats apparently are not used with removables. These would
8661 	 * not have been created during attach, so no need to clean them up...
8662 	 */
8663 	if (un->un_stats != NULL) {
8664 		kstat_delete(un->un_stats);
8665 		un->un_stats = NULL;
8666 	}
8667 	if (un->un_errstats != NULL) {
8668 		kstat_delete(un->un_errstats);
8669 		un->un_errstats = NULL;
8670 	}
8671 
8672 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8673 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8674 
8675 	ddi_prop_remove_all(devi);
8676 	sema_destroy(&un->un_semoclose);
8677 	cv_destroy(&un->un_state_cv);
8678 
8679 getrbuf_failed:
8680 
8681 	sd_free_rqs(un);
8682 
8683 alloc_rqs_failed:
8684 
8685 	devp->sd_private = NULL;
8686 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
8687 
8688 get_softstate_failed:
8689 	/*
8690 	 * Note: the man pages are unclear as to whether or not doing a
8691 	 * ddi_soft_state_free(sd_state, instance) is the right way to
8692 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
8693 	 * ddi_get_soft_state() fails.  The implication seems to be
8694 	 * that the get_soft_state cannot fail if the zalloc succeeds.
8695 	 */
8696 	ddi_soft_state_free(sd_state, instance);
8697 
8698 probe_failed:
8699 	scsi_unprobe(devp);
8700 #ifdef SDDEBUG
8701 	if ((sd_component_mask & SD_LOG_ATTACH_DETACH) &&
8702 	    (sd_level_mask & SD_LOGMASK_TRACE)) {
8703 		cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n",
8704 		    (void *)un);
8705 	}
8706 #endif
8707 	return (DDI_FAILURE);
8708 }
8709 
8710 
8711 /*
8712  *    Function: sd_unit_detach
8713  *
8714  * Description: Performs DDI_DETACH processing for sddetach().
8715  *
8716  * Return Code: DDI_SUCCESS
8717  *		DDI_FAILURE
8718  *
8719  *     Context: Kernel thread context
8720  */
8721 
8722 static int
8723 sd_unit_detach(dev_info_t *devi)
8724 {
8725 	struct scsi_device	*devp;
8726 	struct sd_lun		*un;
8727 	int			i;
8728 	dev_t			dev;
8729 	int			instance = ddi_get_instance(devi);
8730 
8731 	mutex_enter(&sd_detach_mutex);
8732 
8733 	/*
8734 	 * Fail the detach for any of the following:
8735 	 *  - Unable to get the sd_lun struct for the instance
8736 	 *  - A layered driver has an outstanding open on the instance
8737 	 *  - Another thread is already detaching this instance
8738 	 *  - Another thread is currently performing an open
8739 	 */
8740 	devp = ddi_get_driver_private(devi);
8741 	if ((devp == NULL) ||
8742 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8743 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8744 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8745 		mutex_exit(&sd_detach_mutex);
8746 		return (DDI_FAILURE);
8747 	}
8748 
8749 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8750 
8751 	/*
8752 	 * Mark this instance as currently in a detach, to inhibit any
8753 	 * opens from a layered driver.
8754 	 */
8755 	un->un_detach_count++;
8756 	mutex_exit(&sd_detach_mutex);
8757 
8758 	dev = sd_make_device(SD_DEVINFO(un));
8759 
8760 	_NOTE(COMPETING_THREADS_NOW);
8761 
8762 	mutex_enter(SD_MUTEX(un));
8763 
8764 	/*
8765 	 * Fail the detach if there are any outstanding layered
8766 	 * opens on this device.
8767 	 */
8768 	for (i = 0; i < NDKMAP; i++) {
8769 		if (un->un_ocmap.lyropen[i] != 0) {
8770 			goto err_notclosed;
8771 		}
8772 	}
8773 
8774 	/*
8775 	 * Verify there are NO outstanding commands issued to this device.
8776 	 * ie, un_ncmds_in_transport == 0.
8777 	 * It's possible to have outstanding commands through the physio
8778 	 * code path, even though everything's closed.
8779 	 */
8780 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8781 	    (un->un_direct_priority_timeid != NULL) ||
8782 	    (un->un_state == SD_STATE_RWAIT)) {
8783 		mutex_exit(SD_MUTEX(un));
8784 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8785 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
8786 		goto err_stillbusy;
8787 	}
8788 
8789 	/*
8790 	 * If we have the device reserved, release the reservation.
8791 	 */
8792 	if ((un->un_resvd_status & SD_RESERVE) &&
8793 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
8794 		mutex_exit(SD_MUTEX(un));
8795 		/*
8796 		 * Note: sd_reserve_release sends a command to the device
8797 		 * via the sd_ioctlcmd() path, and can sleep.
8798 		 */
8799 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8800 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8801 			    "sd_dr_detach: Cannot release reservation \n");
8802 		}
8803 	} else {
8804 		mutex_exit(SD_MUTEX(un));
8805 	}
8806 
8807 	/*
8808 	 * Untimeout any reserve recover, throttle reset, restart unit
8809 	 * and delayed broadcast timeout threads. Protect the timeout pointer
8810 	 * from getting nulled by their callback functions.
8811 	 */
8812 	mutex_enter(SD_MUTEX(un));
8813 	if (un->un_resvd_timeid != NULL) {
8814 		timeout_id_t temp_id = un->un_resvd_timeid;
8815 		un->un_resvd_timeid = NULL;
8816 		mutex_exit(SD_MUTEX(un));
8817 		(void) untimeout(temp_id);
8818 		mutex_enter(SD_MUTEX(un));
8819 	}
8820 
8821 	if (un->un_reset_throttle_timeid != NULL) {
8822 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8823 		un->un_reset_throttle_timeid = NULL;
8824 		mutex_exit(SD_MUTEX(un));
8825 		(void) untimeout(temp_id);
8826 		mutex_enter(SD_MUTEX(un));
8827 	}
8828 
8829 	if (un->un_startstop_timeid != NULL) {
8830 		timeout_id_t temp_id = un->un_startstop_timeid;
8831 		un->un_startstop_timeid = NULL;
8832 		mutex_exit(SD_MUTEX(un));
8833 		(void) untimeout(temp_id);
8834 		mutex_enter(SD_MUTEX(un));
8835 	}
8836 
8837 	if (un->un_dcvb_timeid != NULL) {
8838 		timeout_id_t temp_id = un->un_dcvb_timeid;
8839 		un->un_dcvb_timeid = NULL;
8840 		mutex_exit(SD_MUTEX(un));
8841 		(void) untimeout(temp_id);
8842 	} else {
8843 		mutex_exit(SD_MUTEX(un));
8844 	}
8845 
8846 	/* Remove any pending reservation reclaim requests for this device */
8847 	sd_rmv_resv_reclaim_req(dev);
8848 
8849 	mutex_enter(SD_MUTEX(un));
8850 
8851 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8852 	if (un->un_direct_priority_timeid != NULL) {
8853 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8854 		un->un_direct_priority_timeid = NULL;
8855 		mutex_exit(SD_MUTEX(un));
8856 		(void) untimeout(temp_id);
8857 		mutex_enter(SD_MUTEX(un));
8858 	}
8859 
8860 	/* Cancel any active multi-host disk watch thread requests */
8861 	if (un->un_mhd_token != NULL) {
8862 		mutex_exit(SD_MUTEX(un));
8863 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8864 		if (scsi_watch_request_terminate(un->un_mhd_token,
8865 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8866 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8867 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
8868 			/*
8869 			 * Note: We are returning here after having removed
8870 			 * some driver timeouts above. This is consistent with
8871 			 * the legacy implementation but perhaps the watch
8872 			 * terminate call should be made with the wait flag set.
8873 			 */
8874 			goto err_stillbusy;
8875 		}
8876 		mutex_enter(SD_MUTEX(un));
8877 		un->un_mhd_token = NULL;
8878 	}
8879 
8880 	if (un->un_swr_token != NULL) {
8881 		mutex_exit(SD_MUTEX(un));
8882 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8883 		if (scsi_watch_request_terminate(un->un_swr_token,
8884 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8885 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8886 			    "sd_dr_detach: Cannot cancel swr watch request\n");
8887 			/*
8888 			 * Note: We are returning here after having removed
8889 			 * some driver timeouts above. This is consistent with
8890 			 * the legacy implementation but perhaps the watch
8891 			 * terminate call should be made with the wait flag set.
8892 			 */
8893 			goto err_stillbusy;
8894 		}
8895 		mutex_enter(SD_MUTEX(un));
8896 		un->un_swr_token = NULL;
8897 	}
8898 
8899 	mutex_exit(SD_MUTEX(un));
8900 
8901 	/*
8902 	 * Clear any scsi_reset_notifies. We clear the reset notifies
8903 	 * if we have not registered one.
8904 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8905 	 */
8906 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8907 	    sd_mhd_reset_notify_cb, (caddr_t)un);
8908 
8909 	/*
8910 	 * protect the timeout pointers from getting nulled by
8911 	 * their callback functions during the cancellation process.
8912 	 * In such a scenario untimeout can be invoked with a null value.
8913 	 */
8914 	_NOTE(NO_COMPETING_THREADS_NOW);
8915 
8916 	mutex_enter(&un->un_pm_mutex);
8917 	if (un->un_pm_idle_timeid != NULL) {
8918 		timeout_id_t temp_id = un->un_pm_idle_timeid;
8919 		un->un_pm_idle_timeid = NULL;
8920 		mutex_exit(&un->un_pm_mutex);
8921 
8922 		/*
8923 		 * Timeout is active; cancel it.
8924 		 * Note that it'll never be active on a device
8925 		 * that does not support PM therefore we don't
8926 		 * have to check before calling pm_idle_component.
8927 		 */
8928 		(void) untimeout(temp_id);
8929 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8930 		mutex_enter(&un->un_pm_mutex);
8931 	}
8932 
8933 	/*
8934 	 * Check whether there is already a timeout scheduled for power
8935 	 * management. If yes then don't lower the power here, that's.
8936 	 * the timeout handler's job.
8937 	 */
8938 	if (un->un_pm_timeid != NULL) {
8939 		timeout_id_t temp_id = un->un_pm_timeid;
8940 		un->un_pm_timeid = NULL;
8941 		mutex_exit(&un->un_pm_mutex);
8942 		/*
8943 		 * Timeout is active; cancel it.
8944 		 * Note that it'll never be active on a device
8945 		 * that does not support PM therefore we don't
8946 		 * have to check before calling pm_idle_component.
8947 		 */
8948 		(void) untimeout(temp_id);
8949 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8950 
8951 	} else {
8952 		mutex_exit(&un->un_pm_mutex);
8953 		if ((un->un_f_pm_is_enabled == TRUE) &&
8954 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) !=
8955 		    DDI_SUCCESS)) {
8956 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8957 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
8958 			/*
8959 			 * Fix for bug: 4297749, item # 13
8960 			 * The above test now includes a check to see if PM is
8961 			 * supported by this device before call
8962 			 * pm_lower_power().
8963 			 * Note, the following is not dead code. The call to
8964 			 * pm_lower_power above will generate a call back into
8965 			 * our sdpower routine which might result in a timeout
8966 			 * handler getting activated. Therefore the following
8967 			 * code is valid and necessary.
8968 			 */
8969 			mutex_enter(&un->un_pm_mutex);
8970 			if (un->un_pm_timeid != NULL) {
8971 				timeout_id_t temp_id = un->un_pm_timeid;
8972 				un->un_pm_timeid = NULL;
8973 				mutex_exit(&un->un_pm_mutex);
8974 				(void) untimeout(temp_id);
8975 				(void) pm_idle_component(SD_DEVINFO(un), 0);
8976 			} else {
8977 				mutex_exit(&un->un_pm_mutex);
8978 			}
8979 		}
8980 	}
8981 
8982 	/*
8983 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8984 	 * Relocated here from above to be after the call to
8985 	 * pm_lower_power, which was getting errors.
8986 	 */
8987 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8988 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8989 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8990 
8991 	if (un->un_f_is_fibre == FALSE) {
8992 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8993 	}
8994 
8995 	/*
8996 	 * Remove any event callbacks, fibre only
8997 	 */
8998 	if (un->un_f_is_fibre == TRUE) {
8999 		if ((un->un_insert_event != NULL) &&
9000 			(ddi_remove_event_handler(un->un_insert_cb_id) !=
9001 				DDI_SUCCESS)) {
9002 			/*
9003 			 * Note: We are returning here after having done
9004 			 * substantial cleanup above. This is consistent
9005 			 * with the legacy implementation but this may not
9006 			 * be the right thing to do.
9007 			 */
9008 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9009 				"sd_dr_detach: Cannot cancel insert event\n");
9010 			goto err_remove_event;
9011 		}
9012 		un->un_insert_event = NULL;
9013 
9014 		if ((un->un_remove_event != NULL) &&
9015 			(ddi_remove_event_handler(un->un_remove_cb_id) !=
9016 				DDI_SUCCESS)) {
9017 			/*
9018 			 * Note: We are returning here after having done
9019 			 * substantial cleanup above. This is consistent
9020 			 * with the legacy implementation but this may not
9021 			 * be the right thing to do.
9022 			 */
9023 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9024 				"sd_dr_detach: Cannot cancel remove event\n");
9025 			goto err_remove_event;
9026 		}
9027 		un->un_remove_event = NULL;
9028 	}
9029 
9030 	/* Do not free the softstate if the callback routine is active */
9031 	sd_sync_with_callback(un);
9032 
9033 	/*
9034 	 * Hold the detach mutex here, to make sure that no other threads ever
9035 	 * can access a (partially) freed soft state structure.
9036 	 */
9037 	mutex_enter(&sd_detach_mutex);
9038 
9039 	/*
9040 	 * Clean up the soft state struct.
9041 	 * Cleanup is done in reverse order of allocs/inits.
9042 	 * At this point there should be no competing threads anymore.
9043 	 */
9044 
9045 	/* Unregister and free device id. */
9046 	ddi_devid_unregister(devi);
9047 	if (un->un_devid) {
9048 		ddi_devid_free(un->un_devid);
9049 		un->un_devid = NULL;
9050 	}
9051 
9052 	/*
9053 	 * Destroy wmap cache if it exists.
9054 	 */
9055 	if (un->un_wm_cache != NULL) {
9056 		kmem_cache_destroy(un->un_wm_cache);
9057 		un->un_wm_cache = NULL;
9058 	}
9059 
9060 	/* Remove minor nodes */
9061 	ddi_remove_minor_node(devi, NULL);
9062 
9063 	/*
9064 	 * kstat cleanup is done in detach for all device types (4363169).
9065 	 * We do not want to fail detach if the device kstats are not deleted
9066 	 * since there is a confusion about the devo_refcnt for the device.
9067 	 * We just delete the kstats and let detach complete successfully.
9068 	 */
9069 	if (un->un_stats != NULL) {
9070 		kstat_delete(un->un_stats);
9071 		un->un_stats = NULL;
9072 	}
9073 	if (un->un_errstats != NULL) {
9074 		kstat_delete(un->un_errstats);
9075 		un->un_errstats = NULL;
9076 	}
9077 
9078 	/* Remove partition stats */
9079 	if (un->un_f_pkstats_enabled) {
9080 		for (i = 0; i < NSDMAP; i++) {
9081 			if (un->un_pstats[i] != NULL) {
9082 				kstat_delete(un->un_pstats[i]);
9083 				un->un_pstats[i] = NULL;
9084 			}
9085 		}
9086 	}
9087 
9088 	/* Remove xbuf registration */
9089 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
9090 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
9091 
9092 	/* Remove driver properties */
9093 	ddi_prop_remove_all(devi);
9094 
9095 	mutex_destroy(&un->un_pm_mutex);
9096 	cv_destroy(&un->un_pm_busy_cv);
9097 
9098 	cv_destroy(&un->un_wcc_cv);
9099 
9100 	/* Open/close semaphore */
9101 	sema_destroy(&un->un_semoclose);
9102 
9103 	/* Removable media condvar. */
9104 	cv_destroy(&un->un_state_cv);
9105 
9106 	/* Suspend/resume condvar. */
9107 	cv_destroy(&un->un_suspend_cv);
9108 	cv_destroy(&un->un_disk_busy_cv);
9109 
9110 	sd_free_rqs(un);
9111 
9112 	/* Free up soft state */
9113 	devp->sd_private = NULL;
9114 	bzero(un, sizeof (struct sd_lun));
9115 	ddi_soft_state_free(sd_state, instance);
9116 
9117 	mutex_exit(&sd_detach_mutex);
9118 
9119 	/* This frees up the INQUIRY data associated with the device. */
9120 	scsi_unprobe(devp);
9121 
9122 	return (DDI_SUCCESS);
9123 
9124 err_notclosed:
9125 	mutex_exit(SD_MUTEX(un));
9126 
9127 err_stillbusy:
9128 	_NOTE(NO_COMPETING_THREADS_NOW);
9129 
9130 err_remove_event:
9131 	mutex_enter(&sd_detach_mutex);
9132 	un->un_detach_count--;
9133 	mutex_exit(&sd_detach_mutex);
9134 
9135 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9136 	return (DDI_FAILURE);
9137 }
9138 
9139 
9140 /*
9141  * Driver minor node structure and data table
9142  */
9143 struct driver_minor_data {
9144 	char	*name;
9145 	minor_t	minor;
9146 	int	type;
9147 };
9148 
9149 static struct driver_minor_data sd_minor_data[] = {
9150 	{"a", 0, S_IFBLK},
9151 	{"b", 1, S_IFBLK},
9152 	{"c", 2, S_IFBLK},
9153 	{"d", 3, S_IFBLK},
9154 	{"e", 4, S_IFBLK},
9155 	{"f", 5, S_IFBLK},
9156 	{"g", 6, S_IFBLK},
9157 	{"h", 7, S_IFBLK},
9158 #if defined(_SUNOS_VTOC_16)
9159 	{"i", 8, S_IFBLK},
9160 	{"j", 9, S_IFBLK},
9161 	{"k", 10, S_IFBLK},
9162 	{"l", 11, S_IFBLK},
9163 	{"m", 12, S_IFBLK},
9164 	{"n", 13, S_IFBLK},
9165 	{"o", 14, S_IFBLK},
9166 	{"p", 15, S_IFBLK},
9167 #endif			/* defined(_SUNOS_VTOC_16) */
9168 #if defined(_FIRMWARE_NEEDS_FDISK)
9169 	{"q", 16, S_IFBLK},
9170 	{"r", 17, S_IFBLK},
9171 	{"s", 18, S_IFBLK},
9172 	{"t", 19, S_IFBLK},
9173 	{"u", 20, S_IFBLK},
9174 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9175 	{"a,raw", 0, S_IFCHR},
9176 	{"b,raw", 1, S_IFCHR},
9177 	{"c,raw", 2, S_IFCHR},
9178 	{"d,raw", 3, S_IFCHR},
9179 	{"e,raw", 4, S_IFCHR},
9180 	{"f,raw", 5, S_IFCHR},
9181 	{"g,raw", 6, S_IFCHR},
9182 	{"h,raw", 7, S_IFCHR},
9183 #if defined(_SUNOS_VTOC_16)
9184 	{"i,raw", 8, S_IFCHR},
9185 	{"j,raw", 9, S_IFCHR},
9186 	{"k,raw", 10, S_IFCHR},
9187 	{"l,raw", 11, S_IFCHR},
9188 	{"m,raw", 12, S_IFCHR},
9189 	{"n,raw", 13, S_IFCHR},
9190 	{"o,raw", 14, S_IFCHR},
9191 	{"p,raw", 15, S_IFCHR},
9192 #endif			/* defined(_SUNOS_VTOC_16) */
9193 #if defined(_FIRMWARE_NEEDS_FDISK)
9194 	{"q,raw", 16, S_IFCHR},
9195 	{"r,raw", 17, S_IFCHR},
9196 	{"s,raw", 18, S_IFCHR},
9197 	{"t,raw", 19, S_IFCHR},
9198 	{"u,raw", 20, S_IFCHR},
9199 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9200 	{0}
9201 };
9202 
9203 static struct driver_minor_data sd_minor_data_efi[] = {
9204 	{"a", 0, S_IFBLK},
9205 	{"b", 1, S_IFBLK},
9206 	{"c", 2, S_IFBLK},
9207 	{"d", 3, S_IFBLK},
9208 	{"e", 4, S_IFBLK},
9209 	{"f", 5, S_IFBLK},
9210 	{"g", 6, S_IFBLK},
9211 	{"wd", 7, S_IFBLK},
9212 #if defined(_FIRMWARE_NEEDS_FDISK)
9213 	{"q", 16, S_IFBLK},
9214 	{"r", 17, S_IFBLK},
9215 	{"s", 18, S_IFBLK},
9216 	{"t", 19, S_IFBLK},
9217 	{"u", 20, S_IFBLK},
9218 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9219 	{"a,raw", 0, S_IFCHR},
9220 	{"b,raw", 1, S_IFCHR},
9221 	{"c,raw", 2, S_IFCHR},
9222 	{"d,raw", 3, S_IFCHR},
9223 	{"e,raw", 4, S_IFCHR},
9224 	{"f,raw", 5, S_IFCHR},
9225 	{"g,raw", 6, S_IFCHR},
9226 	{"wd,raw", 7, S_IFCHR},
9227 #if defined(_FIRMWARE_NEEDS_FDISK)
9228 	{"q,raw", 16, S_IFCHR},
9229 	{"r,raw", 17, S_IFCHR},
9230 	{"s,raw", 18, S_IFCHR},
9231 	{"t,raw", 19, S_IFCHR},
9232 	{"u,raw", 20, S_IFCHR},
9233 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9234 	{0}
9235 };
9236 
9237 
9238 /*
9239  *    Function: sd_create_minor_nodes
9240  *
9241  * Description: Create the minor device nodes for the instance.
9242  *
9243  *   Arguments: un - driver soft state (unit) structure
9244  *		devi - pointer to device info structure
9245  *
9246  * Return Code: DDI_SUCCESS
9247  *		DDI_FAILURE
9248  *
9249  *     Context: Kernel thread context
9250  */
9251 
9252 static int
9253 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi)
9254 {
9255 	struct driver_minor_data	*dmdp;
9256 	struct scsi_device		*devp;
9257 	int				instance;
9258 	char				name[48];
9259 
9260 	ASSERT(un != NULL);
9261 	devp = ddi_get_driver_private(devi);
9262 	instance = ddi_get_instance(devp->sd_dev);
9263 
9264 	/*
9265 	 * Create all the minor nodes for this target.
9266 	 */
9267 	if (un->un_blockcount > DK_MAX_BLOCKS)
9268 		dmdp = sd_minor_data_efi;
9269 	else
9270 		dmdp = sd_minor_data;
9271 	while (dmdp->name != NULL) {
9272 
9273 		(void) sprintf(name, "%s", dmdp->name);
9274 
9275 		if (ddi_create_minor_node(devi, name, dmdp->type,
9276 		    (instance << SDUNIT_SHIFT) | dmdp->minor,
9277 		    un->un_node_type, NULL) == DDI_FAILURE) {
9278 			/*
9279 			 * Clean up any nodes that may have been created, in
9280 			 * case this fails in the middle of the loop.
9281 			 */
9282 			ddi_remove_minor_node(devi, NULL);
9283 			return (DDI_FAILURE);
9284 		}
9285 		dmdp++;
9286 	}
9287 
9288 	return (DDI_SUCCESS);
9289 }
9290 
9291 
9292 /*
9293  *    Function: sd_create_errstats
9294  *
9295  * Description: This routine instantiates the device error stats.
9296  *
9297  *		Note: During attach the stats are instantiated first so they are
9298  *		available for attach-time routines that utilize the driver
9299  *		iopath to send commands to the device. The stats are initialized
9300  *		separately so data obtained during some attach-time routines is
9301  *		available. (4362483)
9302  *
9303  *   Arguments: un - driver soft state (unit) structure
9304  *		instance - driver instance
9305  *
9306  *     Context: Kernel thread context
9307  */
9308 
9309 static void
9310 sd_create_errstats(struct sd_lun *un, int instance)
9311 {
9312 	struct	sd_errstats	*stp;
9313 	char	kstatmodule_err[KSTAT_STRLEN];
9314 	char	kstatname[KSTAT_STRLEN];
9315 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9316 
9317 	ASSERT(un != NULL);
9318 
9319 	if (un->un_errstats != NULL) {
9320 		return;
9321 	}
9322 
9323 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9324 	    "%serr", sd_label);
9325 	(void) snprintf(kstatname, sizeof (kstatname),
9326 	    "%s%d,err", sd_label, instance);
9327 
9328 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9329 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9330 
9331 	if (un->un_errstats == NULL) {
9332 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9333 		    "sd_create_errstats: Failed kstat_create\n");
9334 		return;
9335 	}
9336 
9337 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9338 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9339 	    KSTAT_DATA_UINT32);
9340 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9341 	    KSTAT_DATA_UINT32);
9342 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9343 	    KSTAT_DATA_UINT32);
9344 	kstat_named_init(&stp->sd_vid,		"Vendor",
9345 	    KSTAT_DATA_CHAR);
9346 	kstat_named_init(&stp->sd_pid,		"Product",
9347 	    KSTAT_DATA_CHAR);
9348 	kstat_named_init(&stp->sd_revision,	"Revision",
9349 	    KSTAT_DATA_CHAR);
9350 	kstat_named_init(&stp->sd_serial,	"Serial No",
9351 	    KSTAT_DATA_CHAR);
9352 	kstat_named_init(&stp->sd_capacity,	"Size",
9353 	    KSTAT_DATA_ULONGLONG);
9354 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9355 	    KSTAT_DATA_UINT32);
9356 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9357 	    KSTAT_DATA_UINT32);
9358 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9359 	    KSTAT_DATA_UINT32);
9360 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9361 	    KSTAT_DATA_UINT32);
9362 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9363 	    KSTAT_DATA_UINT32);
9364 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9365 	    KSTAT_DATA_UINT32);
9366 
9367 	un->un_errstats->ks_private = un;
9368 	un->un_errstats->ks_update  = nulldev;
9369 
9370 	kstat_install(un->un_errstats);
9371 }
9372 
9373 
9374 /*
9375  *    Function: sd_set_errstats
9376  *
9377  * Description: This routine sets the value of the vendor id, product id,
9378  *		revision, serial number, and capacity device error stats.
9379  *
9380  *		Note: During attach the stats are instantiated first so they are
9381  *		available for attach-time routines that utilize the driver
9382  *		iopath to send commands to the device. The stats are initialized
9383  *		separately so data obtained during some attach-time routines is
9384  *		available. (4362483)
9385  *
9386  *   Arguments: un - driver soft state (unit) structure
9387  *
9388  *     Context: Kernel thread context
9389  */
9390 
9391 static void
9392 sd_set_errstats(struct sd_lun *un)
9393 {
9394 	struct	sd_errstats	*stp;
9395 
9396 	ASSERT(un != NULL);
9397 	ASSERT(un->un_errstats != NULL);
9398 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9399 	ASSERT(stp != NULL);
9400 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9401 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9402 	(void) strncpy(stp->sd_revision.value.c,
9403 	    un->un_sd->sd_inq->inq_revision, 4);
9404 
9405 	/*
9406 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9407 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9408 	 * (4376302))
9409 	 */
9410 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9411 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9412 		    sizeof (SD_INQUIRY(un)->inq_serial));
9413 	}
9414 
9415 	if (un->un_f_blockcount_is_valid != TRUE) {
9416 		/*
9417 		 * Set capacity error stat to 0 for no media. This ensures
9418 		 * a valid capacity is displayed in response to 'iostat -E'
9419 		 * when no media is present in the device.
9420 		 */
9421 		stp->sd_capacity.value.ui64 = 0;
9422 	} else {
9423 		/*
9424 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9425 		 * capacity.
9426 		 *
9427 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9428 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9429 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9430 		 */
9431 		stp->sd_capacity.value.ui64 = (uint64_t)
9432 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9433 	}
9434 }
9435 
9436 
9437 /*
9438  *    Function: sd_set_pstats
9439  *
9440  * Description: This routine instantiates and initializes the partition
9441  *              stats for each partition with more than zero blocks.
9442  *		(4363169)
9443  *
9444  *   Arguments: un - driver soft state (unit) structure
9445  *
9446  *     Context: Kernel thread context
9447  */
9448 
9449 static void
9450 sd_set_pstats(struct sd_lun *un)
9451 {
9452 	char	kstatname[KSTAT_STRLEN];
9453 	int	instance;
9454 	int	i;
9455 
9456 	ASSERT(un != NULL);
9457 
9458 	instance = ddi_get_instance(SD_DEVINFO(un));
9459 
9460 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9461 	for (i = 0; i < NSDMAP; i++) {
9462 		if ((un->un_pstats[i] == NULL) &&
9463 		    (un->un_map[i].dkl_nblk != 0)) {
9464 			(void) snprintf(kstatname, sizeof (kstatname),
9465 			    "%s%d,%s", sd_label, instance,
9466 			    sd_minor_data[i].name);
9467 			un->un_pstats[i] = kstat_create(sd_label,
9468 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9469 			    1, KSTAT_FLAG_PERSISTENT);
9470 			if (un->un_pstats[i] != NULL) {
9471 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9472 				kstat_install(un->un_pstats[i]);
9473 			}
9474 		}
9475 	}
9476 }
9477 
9478 
9479 #if (defined(__fibre))
9480 /*
9481  *    Function: sd_init_event_callbacks
9482  *
9483  * Description: This routine initializes the insertion and removal event
9484  *		callbacks. (fibre only)
9485  *
9486  *   Arguments: un - driver soft state (unit) structure
9487  *
9488  *     Context: Kernel thread context
9489  */
9490 
9491 static void
9492 sd_init_event_callbacks(struct sd_lun *un)
9493 {
9494 	ASSERT(un != NULL);
9495 
9496 	if ((un->un_insert_event == NULL) &&
9497 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9498 	    &un->un_insert_event) == DDI_SUCCESS)) {
9499 		/*
9500 		 * Add the callback for an insertion event
9501 		 */
9502 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9503 		    un->un_insert_event, sd_event_callback, (void *)un,
9504 		    &(un->un_insert_cb_id));
9505 	}
9506 
9507 	if ((un->un_remove_event == NULL) &&
9508 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9509 	    &un->un_remove_event) == DDI_SUCCESS)) {
9510 		/*
9511 		 * Add the callback for a removal event
9512 		 */
9513 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9514 		    un->un_remove_event, sd_event_callback, (void *)un,
9515 		    &(un->un_remove_cb_id));
9516 	}
9517 }
9518 
9519 
9520 /*
9521  *    Function: sd_event_callback
9522  *
9523  * Description: This routine handles insert/remove events (photon). The
9524  *		state is changed to OFFLINE which can be used to supress
9525  *		error msgs. (fibre only)
9526  *
9527  *   Arguments: un - driver soft state (unit) structure
9528  *
9529  *     Context: Callout thread context
9530  */
9531 /* ARGSUSED */
9532 static void
9533 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9534     void *bus_impldata)
9535 {
9536 	struct sd_lun *un = (struct sd_lun *)arg;
9537 
9538 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9539 	if (event == un->un_insert_event) {
9540 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9541 		mutex_enter(SD_MUTEX(un));
9542 		if (un->un_state == SD_STATE_OFFLINE) {
9543 			if (un->un_last_state != SD_STATE_SUSPENDED) {
9544 				un->un_state = un->un_last_state;
9545 			} else {
9546 				/*
9547 				 * We have gone through SUSPEND/RESUME while
9548 				 * we were offline. Restore the last state
9549 				 */
9550 				un->un_state = un->un_save_state;
9551 			}
9552 		}
9553 		mutex_exit(SD_MUTEX(un));
9554 
9555 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9556 	} else if (event == un->un_remove_event) {
9557 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9558 		mutex_enter(SD_MUTEX(un));
9559 		/*
9560 		 * We need to handle an event callback that occurs during
9561 		 * the suspend operation, since we don't prevent it.
9562 		 */
9563 		if (un->un_state != SD_STATE_OFFLINE) {
9564 			if (un->un_state != SD_STATE_SUSPENDED) {
9565 				New_state(un, SD_STATE_OFFLINE);
9566 			} else {
9567 				un->un_last_state = SD_STATE_OFFLINE;
9568 			}
9569 		}
9570 		mutex_exit(SD_MUTEX(un));
9571 	} else {
9572 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9573 		    "!Unknown event\n");
9574 	}
9575 
9576 }
9577 #endif
9578 
9579 /*
9580  *    Function: sd_cache_control()
9581  *
9582  * Description: This routine is the driver entry point for setting
9583  *		read and write caching by modifying the WCE (write cache
9584  *		enable) and RCD (read cache disable) bits of mode
9585  *		page 8 (MODEPAGE_CACHING).
9586  *
9587  *   Arguments: un - driver soft state (unit) structure
9588  *		rcd_flag - flag for controlling the read cache
9589  *		wce_flag - flag for controlling the write cache
9590  *
9591  * Return Code: EIO
9592  *		code returned by sd_send_scsi_MODE_SENSE and
9593  *		sd_send_scsi_MODE_SELECT
9594  *
9595  *     Context: Kernel Thread
9596  */
9597 
9598 static int
9599 sd_cache_control(struct sd_lun *un, int rcd_flag, int wce_flag)
9600 {
9601 	struct mode_caching	*mode_caching_page;
9602 	uchar_t			*header;
9603 	size_t			buflen;
9604 	int			hdrlen;
9605 	int			bd_len;
9606 	int			rval = 0;
9607 	struct mode_header_grp2	*mhp;
9608 
9609 	ASSERT(un != NULL);
9610 
9611 	/*
9612 	 * Do a test unit ready, otherwise a mode sense may not work if this
9613 	 * is the first command sent to the device after boot.
9614 	 */
9615 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
9616 
9617 	if (un->un_f_cfg_is_atapi == TRUE) {
9618 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9619 	} else {
9620 		hdrlen = MODE_HEADER_LENGTH;
9621 	}
9622 
9623 	/*
9624 	 * Allocate memory for the retrieved mode page and its headers.  Set
9625 	 * a pointer to the page itself.  Use mode_cache_scsi3 to insure
9626 	 * we get all of the mode sense data otherwise, the mode select
9627 	 * will fail.  mode_cache_scsi3 is a superset of mode_caching.
9628 	 */
9629 	buflen = hdrlen + MODE_BLK_DESC_LENGTH +
9630 		sizeof (struct mode_cache_scsi3);
9631 
9632 	header = kmem_zalloc(buflen, KM_SLEEP);
9633 
9634 	/* Get the information from the device. */
9635 	if (un->un_f_cfg_is_atapi == TRUE) {
9636 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
9637 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9638 	} else {
9639 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
9640 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9641 	}
9642 	if (rval != 0) {
9643 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9644 		    "sd_cache_control: Mode Sense Failed\n");
9645 		kmem_free(header, buflen);
9646 		return (rval);
9647 	}
9648 
9649 	/*
9650 	 * Determine size of Block Descriptors in order to locate
9651 	 * the mode page data. ATAPI devices return 0, SCSI devices
9652 	 * should return MODE_BLK_DESC_LENGTH.
9653 	 */
9654 	if (un->un_f_cfg_is_atapi == TRUE) {
9655 		mhp	= (struct mode_header_grp2 *)header;
9656 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9657 	} else {
9658 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9659 	}
9660 
9661 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9662 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
9663 		    "sd_cache_control: Mode Sense returned invalid "
9664 		    "block descriptor length\n");
9665 		kmem_free(header, buflen);
9666 		return (EIO);
9667 	}
9668 
9669 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9670 
9671 	/* Check the relevant bits on successful mode sense. */
9672 	if ((mode_caching_page->rcd && rcd_flag == SD_CACHE_ENABLE) ||
9673 	    (!mode_caching_page->rcd && rcd_flag == SD_CACHE_DISABLE) ||
9674 	    (mode_caching_page->wce && wce_flag == SD_CACHE_DISABLE) ||
9675 	    (!mode_caching_page->wce && wce_flag == SD_CACHE_ENABLE)) {
9676 
9677 		size_t sbuflen;
9678 		uchar_t save_pg;
9679 
9680 		/*
9681 		 * Construct select buffer length based on the
9682 		 * length of the sense data returned.
9683 		 */
9684 		sbuflen =  hdrlen + MODE_BLK_DESC_LENGTH +
9685 				sizeof (struct mode_page) +
9686 				(int)mode_caching_page->mode_page.length;
9687 
9688 		/*
9689 		 * Set the caching bits as requested.
9690 		 */
9691 		if (rcd_flag == SD_CACHE_ENABLE)
9692 			mode_caching_page->rcd = 0;
9693 		else if (rcd_flag == SD_CACHE_DISABLE)
9694 			mode_caching_page->rcd = 1;
9695 
9696 		if (wce_flag == SD_CACHE_ENABLE)
9697 			mode_caching_page->wce = 1;
9698 		else if (wce_flag == SD_CACHE_DISABLE)
9699 			mode_caching_page->wce = 0;
9700 
9701 		/*
9702 		 * Save the page if the mode sense says the
9703 		 * drive supports it.
9704 		 */
9705 		save_pg = mode_caching_page->mode_page.ps ?
9706 				SD_SAVE_PAGE : SD_DONTSAVE_PAGE;
9707 
9708 		/* Clear reserved bits before mode select. */
9709 		mode_caching_page->mode_page.ps = 0;
9710 
9711 		/*
9712 		 * Clear out mode header for mode select.
9713 		 * The rest of the retrieved page will be reused.
9714 		 */
9715 		bzero(header, hdrlen);
9716 
9717 		if (un->un_f_cfg_is_atapi == TRUE) {
9718 			mhp = (struct mode_header_grp2 *)header;
9719 			mhp->bdesc_length_hi = bd_len >> 8;
9720 			mhp->bdesc_length_lo = (uchar_t)bd_len & 0xff;
9721 		} else {
9722 			((struct mode_header *)header)->bdesc_length = bd_len;
9723 		}
9724 
9725 		/* Issue mode select to change the cache settings */
9726 		if (un->un_f_cfg_is_atapi == TRUE) {
9727 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header,
9728 			    sbuflen, save_pg, SD_PATH_DIRECT);
9729 		} else {
9730 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
9731 			    sbuflen, save_pg, SD_PATH_DIRECT);
9732 		}
9733 	}
9734 
9735 	kmem_free(header, buflen);
9736 	return (rval);
9737 }
9738 
9739 
9740 /*
9741  *    Function: sd_get_write_cache_enabled()
9742  *
9743  * Description: This routine is the driver entry point for determining if
9744  *		write caching is enabled.  It examines the WCE (write cache
9745  *		enable) bits of mode page 8 (MODEPAGE_CACHING).
9746  *
9747  *   Arguments: un - driver soft state (unit) structure
9748  *   		is_enabled - pointer to int where write cache enabled state
9749  *   			is returned (non-zero -> write cache enabled)
9750  *
9751  *
9752  * Return Code: EIO
9753  *		code returned by sd_send_scsi_MODE_SENSE
9754  *
9755  *     Context: Kernel Thread
9756  *
9757  * NOTE: If ioctl is added to disable write cache, this sequence should
9758  * be followed so that no locking is required for accesses to
9759  * un->un_f_write_cache_enabled:
9760  * 	do mode select to clear wce
9761  * 	do synchronize cache to flush cache
9762  * 	set un->un_f_write_cache_enabled = FALSE
9763  *
9764  * Conversely, an ioctl to enable the write cache should be done
9765  * in this order:
9766  * 	set un->un_f_write_cache_enabled = TRUE
9767  * 	do mode select to set wce
9768  */
9769 
9770 static int
9771 sd_get_write_cache_enabled(struct sd_lun *un, int *is_enabled)
9772 {
9773 	struct mode_caching	*mode_caching_page;
9774 	uchar_t			*header;
9775 	size_t			buflen;
9776 	int			hdrlen;
9777 	int			bd_len;
9778 	int			rval = 0;
9779 
9780 	ASSERT(un != NULL);
9781 	ASSERT(is_enabled != NULL);
9782 
9783 	/* in case of error, flag as enabled */
9784 	*is_enabled = TRUE;
9785 
9786 	/*
9787 	 * Do a test unit ready, otherwise a mode sense may not work if this
9788 	 * is the first command sent to the device after boot.
9789 	 */
9790 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
9791 
9792 	if (un->un_f_cfg_is_atapi == TRUE) {
9793 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9794 	} else {
9795 		hdrlen = MODE_HEADER_LENGTH;
9796 	}
9797 
9798 	/*
9799 	 * Allocate memory for the retrieved mode page and its headers.  Set
9800 	 * a pointer to the page itself.
9801 	 */
9802 	buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
9803 	header = kmem_zalloc(buflen, KM_SLEEP);
9804 
9805 	/* Get the information from the device. */
9806 	if (un->un_f_cfg_is_atapi == TRUE) {
9807 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
9808 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9809 	} else {
9810 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
9811 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9812 	}
9813 	if (rval != 0) {
9814 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9815 		    "sd_get_write_cache_enabled: Mode Sense Failed\n");
9816 		kmem_free(header, buflen);
9817 		return (rval);
9818 	}
9819 
9820 	/*
9821 	 * Determine size of Block Descriptors in order to locate
9822 	 * the mode page data. ATAPI devices return 0, SCSI devices
9823 	 * should return MODE_BLK_DESC_LENGTH.
9824 	 */
9825 	if (un->un_f_cfg_is_atapi == TRUE) {
9826 		struct mode_header_grp2	*mhp;
9827 		mhp	= (struct mode_header_grp2 *)header;
9828 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9829 	} else {
9830 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9831 	}
9832 
9833 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9834 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
9835 		    "sd_get_write_cache_enabled: Mode Sense returned invalid "
9836 		    "block descriptor length\n");
9837 		kmem_free(header, buflen);
9838 		return (EIO);
9839 	}
9840 
9841 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9842 	*is_enabled = mode_caching_page->wce;
9843 
9844 	kmem_free(header, buflen);
9845 	return (0);
9846 }
9847 
9848 
9849 /*
9850  *    Function: sd_make_device
9851  *
9852  * Description: Utility routine to return the Solaris device number from
9853  *		the data in the device's dev_info structure.
9854  *
9855  * Return Code: The Solaris device number
9856  *
9857  *     Context: Any
9858  */
9859 
9860 static dev_t
9861 sd_make_device(dev_info_t *devi)
9862 {
9863 	return (makedevice(ddi_name_to_major(ddi_get_name(devi)),
9864 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
9865 }
9866 
9867 
9868 /*
9869  *    Function: sd_pm_entry
9870  *
9871  * Description: Called at the start of a new command to manage power
9872  *		and busy status of a device. This includes determining whether
9873  *		the current power state of the device is sufficient for
9874  *		performing the command or whether it must be changed.
9875  *		The PM framework is notified appropriately.
9876  *		Only with a return status of DDI_SUCCESS will the
9877  *		component be busy to the framework.
9878  *
9879  *		All callers of sd_pm_entry must check the return status
9880  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
9881  *		of DDI_FAILURE indicates the device failed to power up.
9882  *		In this case un_pm_count has been adjusted so the result
9883  *		on exit is still powered down, ie. count is less than 0.
9884  *		Calling sd_pm_exit with this count value hits an ASSERT.
9885  *
9886  * Return Code: DDI_SUCCESS or DDI_FAILURE
9887  *
9888  *     Context: Kernel thread context.
9889  */
9890 
9891 static int
9892 sd_pm_entry(struct sd_lun *un)
9893 {
9894 	int return_status = DDI_SUCCESS;
9895 
9896 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9897 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9898 
9899 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9900 
9901 	if (un->un_f_pm_is_enabled == FALSE) {
9902 		SD_TRACE(SD_LOG_IO_PM, un,
9903 		    "sd_pm_entry: exiting, PM not enabled\n");
9904 		return (return_status);
9905 	}
9906 
9907 	/*
9908 	 * Just increment a counter if PM is enabled. On the transition from
9909 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9910 	 * the count with each IO and mark the device as idle when the count
9911 	 * hits 0.
9912 	 *
9913 	 * If the count is less than 0 the device is powered down. If a powered
9914 	 * down device is successfully powered up then the count must be
9915 	 * incremented to reflect the power up. Note that it'll get incremented
9916 	 * a second time to become busy.
9917 	 *
9918 	 * Because the following has the potential to change the device state
9919 	 * and must release the un_pm_mutex to do so, only one thread can be
9920 	 * allowed through at a time.
9921 	 */
9922 
9923 	mutex_enter(&un->un_pm_mutex);
9924 	while (un->un_pm_busy == TRUE) {
9925 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9926 	}
9927 	un->un_pm_busy = TRUE;
9928 
9929 	if (un->un_pm_count < 1) {
9930 
9931 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9932 
9933 		/*
9934 		 * Indicate we are now busy so the framework won't attempt to
9935 		 * power down the device. This call will only fail if either
9936 		 * we passed a bad component number or the device has no
9937 		 * components. Neither of these should ever happen.
9938 		 */
9939 		mutex_exit(&un->un_pm_mutex);
9940 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
9941 		ASSERT(return_status == DDI_SUCCESS);
9942 
9943 		mutex_enter(&un->un_pm_mutex);
9944 
9945 		if (un->un_pm_count < 0) {
9946 			mutex_exit(&un->un_pm_mutex);
9947 
9948 			SD_TRACE(SD_LOG_IO_PM, un,
9949 			    "sd_pm_entry: power up component\n");
9950 
9951 			/*
9952 			 * pm_raise_power will cause sdpower to be called
9953 			 * which brings the device power level to the
9954 			 * desired state, ON in this case. If successful,
9955 			 * un_pm_count and un_power_level will be updated
9956 			 * appropriately.
9957 			 */
9958 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
9959 			    SD_SPINDLE_ON);
9960 
9961 			mutex_enter(&un->un_pm_mutex);
9962 
9963 			if (return_status != DDI_SUCCESS) {
9964 				/*
9965 				 * Power up failed.
9966 				 * Idle the device and adjust the count
9967 				 * so the result on exit is that we're
9968 				 * still powered down, ie. count is less than 0.
9969 				 */
9970 				SD_TRACE(SD_LOG_IO_PM, un,
9971 				    "sd_pm_entry: power up failed,"
9972 				    " idle the component\n");
9973 
9974 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9975 				un->un_pm_count--;
9976 			} else {
9977 				/*
9978 				 * Device is powered up, verify the
9979 				 * count is non-negative.
9980 				 * This is debug only.
9981 				 */
9982 				ASSERT(un->un_pm_count == 0);
9983 			}
9984 		}
9985 
9986 		if (return_status == DDI_SUCCESS) {
9987 			/*
9988 			 * For performance, now that the device has been tagged
9989 			 * as busy, and it's known to be powered up, update the
9990 			 * chain types to use jump tables that do not include
9991 			 * pm. This significantly lowers the overhead and
9992 			 * therefore improves performance.
9993 			 */
9994 
9995 			mutex_exit(&un->un_pm_mutex);
9996 			mutex_enter(SD_MUTEX(un));
9997 			SD_TRACE(SD_LOG_IO_PM, un,
9998 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
9999 			    un->un_uscsi_chain_type);
10000 
10001 			if (un->un_f_non_devbsize_supported) {
10002 				un->un_buf_chain_type =
10003 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
10004 			} else {
10005 				un->un_buf_chain_type =
10006 				    SD_CHAIN_INFO_DISK_NO_PM;
10007 			}
10008 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
10009 
10010 			SD_TRACE(SD_LOG_IO_PM, un,
10011 			    "             changed  uscsi_chain_type to   %d\n",
10012 			    un->un_uscsi_chain_type);
10013 			mutex_exit(SD_MUTEX(un));
10014 			mutex_enter(&un->un_pm_mutex);
10015 
10016 			if (un->un_pm_idle_timeid == NULL) {
10017 				/* 300 ms. */
10018 				un->un_pm_idle_timeid =
10019 				    timeout(sd_pm_idletimeout_handler, un,
10020 				    (drv_usectohz((clock_t)300000)));
10021 				/*
10022 				 * Include an extra call to busy which keeps the
10023 				 * device busy with-respect-to the PM layer
10024 				 * until the timer fires, at which time it'll
10025 				 * get the extra idle call.
10026 				 */
10027 				(void) pm_busy_component(SD_DEVINFO(un), 0);
10028 			}
10029 		}
10030 	}
10031 	un->un_pm_busy = FALSE;
10032 	/* Next... */
10033 	cv_signal(&un->un_pm_busy_cv);
10034 
10035 	un->un_pm_count++;
10036 
10037 	SD_TRACE(SD_LOG_IO_PM, un,
10038 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
10039 
10040 	mutex_exit(&un->un_pm_mutex);
10041 
10042 	return (return_status);
10043 }
10044 
10045 
10046 /*
10047  *    Function: sd_pm_exit
10048  *
10049  * Description: Called at the completion of a command to manage busy
10050  *		status for the device. If the device becomes idle the
10051  *		PM framework is notified.
10052  *
10053  *     Context: Kernel thread context
10054  */
10055 
10056 static void
10057 sd_pm_exit(struct sd_lun *un)
10058 {
10059 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10060 	ASSERT(!mutex_owned(&un->un_pm_mutex));
10061 
10062 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
10063 
10064 	/*
10065 	 * After attach the following flag is only read, so don't
10066 	 * take the penalty of acquiring a mutex for it.
10067 	 */
10068 	if (un->un_f_pm_is_enabled == TRUE) {
10069 
10070 		mutex_enter(&un->un_pm_mutex);
10071 		un->un_pm_count--;
10072 
10073 		SD_TRACE(SD_LOG_IO_PM, un,
10074 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
10075 
10076 		ASSERT(un->un_pm_count >= 0);
10077 		if (un->un_pm_count == 0) {
10078 			mutex_exit(&un->un_pm_mutex);
10079 
10080 			SD_TRACE(SD_LOG_IO_PM, un,
10081 			    "sd_pm_exit: idle component\n");
10082 
10083 			(void) pm_idle_component(SD_DEVINFO(un), 0);
10084 
10085 		} else {
10086 			mutex_exit(&un->un_pm_mutex);
10087 		}
10088 	}
10089 
10090 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
10091 }
10092 
10093 
10094 /*
10095  *    Function: sdopen
10096  *
10097  * Description: Driver's open(9e) entry point function.
10098  *
10099  *   Arguments: dev_i   - pointer to device number
10100  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
10101  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10102  *		cred_p  - user credential pointer
10103  *
10104  * Return Code: EINVAL
10105  *		ENXIO
10106  *		EIO
10107  *		EROFS
10108  *		EBUSY
10109  *
10110  *     Context: Kernel thread context
10111  */
10112 /* ARGSUSED */
10113 static int
10114 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
10115 {
10116 	struct sd_lun	*un;
10117 	int		nodelay;
10118 	int		part;
10119 	uint64_t	partmask;
10120 	int		instance;
10121 	dev_t		dev;
10122 	int		rval = EIO;
10123 
10124 	/* Validate the open type */
10125 	if (otyp >= OTYPCNT) {
10126 		return (EINVAL);
10127 	}
10128 
10129 	dev = *dev_p;
10130 	instance = SDUNIT(dev);
10131 	mutex_enter(&sd_detach_mutex);
10132 
10133 	/*
10134 	 * Fail the open if there is no softstate for the instance, or
10135 	 * if another thread somewhere is trying to detach the instance.
10136 	 */
10137 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
10138 	    (un->un_detach_count != 0)) {
10139 		mutex_exit(&sd_detach_mutex);
10140 		/*
10141 		 * The probe cache only needs to be cleared when open (9e) fails
10142 		 * with ENXIO (4238046).
10143 		 */
10144 		/*
10145 		 * un-conditionally clearing probe cache is ok with
10146 		 * separate sd/ssd binaries
10147 		 * x86 platform can be an issue with both parallel
10148 		 * and fibre in 1 binary
10149 		 */
10150 		sd_scsi_clear_probe_cache();
10151 		return (ENXIO);
10152 	}
10153 
10154 	/*
10155 	 * The un_layer_count is to prevent another thread in specfs from
10156 	 * trying to detach the instance, which can happen when we are
10157 	 * called from a higher-layer driver instead of thru specfs.
10158 	 * This will not be needed when DDI provides a layered driver
10159 	 * interface that allows specfs to know that an instance is in
10160 	 * use by a layered driver & should not be detached.
10161 	 *
10162 	 * Note: the semantics for layered driver opens are exactly one
10163 	 * close for every open.
10164 	 */
10165 	if (otyp == OTYP_LYR) {
10166 		un->un_layer_count++;
10167 	}
10168 
10169 	/*
10170 	 * Keep a count of the current # of opens in progress. This is because
10171 	 * some layered drivers try to call us as a regular open. This can
10172 	 * cause problems that we cannot prevent, however by keeping this count
10173 	 * we can at least keep our open and detach routines from racing against
10174 	 * each other under such conditions.
10175 	 */
10176 	un->un_opens_in_progress++;
10177 	mutex_exit(&sd_detach_mutex);
10178 
10179 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
10180 	part	 = SDPART(dev);
10181 	partmask = 1 << part;
10182 
10183 	/*
10184 	 * We use a semaphore here in order to serialize
10185 	 * open and close requests on the device.
10186 	 */
10187 	sema_p(&un->un_semoclose);
10188 
10189 	mutex_enter(SD_MUTEX(un));
10190 
10191 	/*
10192 	 * All device accesses go thru sdstrategy() where we check
10193 	 * on suspend status but there could be a scsi_poll command,
10194 	 * which bypasses sdstrategy(), so we need to check pm
10195 	 * status.
10196 	 */
10197 
10198 	if (!nodelay) {
10199 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10200 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10201 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10202 		}
10203 
10204 		mutex_exit(SD_MUTEX(un));
10205 		if (sd_pm_entry(un) != DDI_SUCCESS) {
10206 			rval = EIO;
10207 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
10208 			    "sdopen: sd_pm_entry failed\n");
10209 			goto open_failed_with_pm;
10210 		}
10211 		mutex_enter(SD_MUTEX(un));
10212 	}
10213 
10214 	/* check for previous exclusive open */
10215 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
10216 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10217 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
10218 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
10219 
10220 	if (un->un_exclopen & (partmask)) {
10221 		goto excl_open_fail;
10222 	}
10223 
10224 	if (flag & FEXCL) {
10225 		int i;
10226 		if (un->un_ocmap.lyropen[part]) {
10227 			goto excl_open_fail;
10228 		}
10229 		for (i = 0; i < (OTYPCNT - 1); i++) {
10230 			if (un->un_ocmap.regopen[i] & (partmask)) {
10231 				goto excl_open_fail;
10232 			}
10233 		}
10234 	}
10235 
10236 	/*
10237 	 * Check the write permission if this is a removable media device,
10238 	 * NDELAY has not been set, and writable permission is requested.
10239 	 *
10240 	 * Note: If NDELAY was set and this is write-protected media the WRITE
10241 	 * attempt will fail with EIO as part of the I/O processing. This is a
10242 	 * more permissive implementation that allows the open to succeed and
10243 	 * WRITE attempts to fail when appropriate.
10244 	 */
10245 	if (un->un_f_chk_wp_open) {
10246 		if ((flag & FWRITE) && (!nodelay)) {
10247 			mutex_exit(SD_MUTEX(un));
10248 			/*
10249 			 * Defer the check for write permission on writable
10250 			 * DVD drive till sdstrategy and will not fail open even
10251 			 * if FWRITE is set as the device can be writable
10252 			 * depending upon the media and the media can change
10253 			 * after the call to open().
10254 			 */
10255 			if (un->un_f_dvdram_writable_device == FALSE) {
10256 				if (ISCD(un) || sr_check_wp(dev)) {
10257 				rval = EROFS;
10258 				mutex_enter(SD_MUTEX(un));
10259 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10260 				    "write to cd or write protected media\n");
10261 				goto open_fail;
10262 				}
10263 			}
10264 			mutex_enter(SD_MUTEX(un));
10265 		}
10266 	}
10267 
10268 	/*
10269 	 * If opening in NDELAY/NONBLOCK mode, just return.
10270 	 * Check if disk is ready and has a valid geometry later.
10271 	 */
10272 	if (!nodelay) {
10273 		mutex_exit(SD_MUTEX(un));
10274 		rval = sd_ready_and_valid(un);
10275 		mutex_enter(SD_MUTEX(un));
10276 		/*
10277 		 * Fail if device is not ready or if the number of disk
10278 		 * blocks is zero or negative for non CD devices.
10279 		 */
10280 		if ((rval != SD_READY_VALID) ||
10281 		    (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) {
10282 			rval = un->un_f_has_removable_media ? ENXIO : EIO;
10283 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10284 			    "device not ready or invalid disk block value\n");
10285 			goto open_fail;
10286 		}
10287 #if defined(__i386) || defined(__amd64)
10288 	} else {
10289 		uchar_t *cp;
10290 		/*
10291 		 * x86 requires special nodelay handling, so that p0 is
10292 		 * always defined and accessible.
10293 		 * Invalidate geometry only if device is not already open.
10294 		 */
10295 		cp = &un->un_ocmap.chkd[0];
10296 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10297 			if (*cp != (uchar_t)0) {
10298 			    break;
10299 			}
10300 			cp++;
10301 		}
10302 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10303 			un->un_f_geometry_is_valid = FALSE;
10304 		}
10305 
10306 #endif
10307 	}
10308 
10309 	if (otyp == OTYP_LYR) {
10310 		un->un_ocmap.lyropen[part]++;
10311 	} else {
10312 		un->un_ocmap.regopen[otyp] |= partmask;
10313 	}
10314 
10315 	/* Set up open and exclusive open flags */
10316 	if (flag & FEXCL) {
10317 		un->un_exclopen |= (partmask);
10318 	}
10319 
10320 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10321 	    "open of part %d type %d\n", part, otyp);
10322 
10323 	mutex_exit(SD_MUTEX(un));
10324 	if (!nodelay) {
10325 		sd_pm_exit(un);
10326 	}
10327 
10328 	sema_v(&un->un_semoclose);
10329 
10330 	mutex_enter(&sd_detach_mutex);
10331 	un->un_opens_in_progress--;
10332 	mutex_exit(&sd_detach_mutex);
10333 
10334 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10335 	return (DDI_SUCCESS);
10336 
10337 excl_open_fail:
10338 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10339 	rval = EBUSY;
10340 
10341 open_fail:
10342 	mutex_exit(SD_MUTEX(un));
10343 
10344 	/*
10345 	 * On a failed open we must exit the pm management.
10346 	 */
10347 	if (!nodelay) {
10348 		sd_pm_exit(un);
10349 	}
10350 open_failed_with_pm:
10351 	sema_v(&un->un_semoclose);
10352 
10353 	mutex_enter(&sd_detach_mutex);
10354 	un->un_opens_in_progress--;
10355 	if (otyp == OTYP_LYR) {
10356 		un->un_layer_count--;
10357 	}
10358 	mutex_exit(&sd_detach_mutex);
10359 
10360 	return (rval);
10361 }
10362 
10363 
10364 /*
10365  *    Function: sdclose
10366  *
10367  * Description: Driver's close(9e) entry point function.
10368  *
10369  *   Arguments: dev    - device number
10370  *		flag   - file status flag, informational only
10371  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10372  *		cred_p - user credential pointer
10373  *
10374  * Return Code: ENXIO
10375  *
10376  *     Context: Kernel thread context
10377  */
10378 /* ARGSUSED */
10379 static int
10380 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10381 {
10382 	struct sd_lun	*un;
10383 	uchar_t		*cp;
10384 	int		part;
10385 	int		nodelay;
10386 	int		rval = 0;
10387 
10388 	/* Validate the open type */
10389 	if (otyp >= OTYPCNT) {
10390 		return (ENXIO);
10391 	}
10392 
10393 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10394 		return (ENXIO);
10395 	}
10396 
10397 	part = SDPART(dev);
10398 	nodelay = flag & (FNDELAY | FNONBLOCK);
10399 
10400 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10401 	    "sdclose: close of part %d type %d\n", part, otyp);
10402 
10403 	/*
10404 	 * We use a semaphore here in order to serialize
10405 	 * open and close requests on the device.
10406 	 */
10407 	sema_p(&un->un_semoclose);
10408 
10409 	mutex_enter(SD_MUTEX(un));
10410 
10411 	/* Don't proceed if power is being changed. */
10412 	while (un->un_state == SD_STATE_PM_CHANGING) {
10413 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10414 	}
10415 
10416 	if (un->un_exclopen & (1 << part)) {
10417 		un->un_exclopen &= ~(1 << part);
10418 	}
10419 
10420 	/* Update the open partition map */
10421 	if (otyp == OTYP_LYR) {
10422 		un->un_ocmap.lyropen[part] -= 1;
10423 	} else {
10424 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10425 	}
10426 
10427 	cp = &un->un_ocmap.chkd[0];
10428 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10429 		if (*cp != NULL) {
10430 			break;
10431 		}
10432 		cp++;
10433 	}
10434 
10435 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10436 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10437 
10438 		/*
10439 		 * We avoid persistance upon the last close, and set
10440 		 * the throttle back to the maximum.
10441 		 */
10442 		un->un_throttle = un->un_saved_throttle;
10443 
10444 		if (un->un_state == SD_STATE_OFFLINE) {
10445 			if (un->un_f_is_fibre == FALSE) {
10446 				scsi_log(SD_DEVINFO(un), sd_label,
10447 					CE_WARN, "offline\n");
10448 			}
10449 			un->un_f_geometry_is_valid = FALSE;
10450 
10451 		} else {
10452 			/*
10453 			 * Flush any outstanding writes in NVRAM cache.
10454 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10455 			 * cmd, it may not work for non-Pluto devices.
10456 			 * SYNCHRONIZE CACHE is not required for removables,
10457 			 * except DVD-RAM drives.
10458 			 *
10459 			 * Also note: because SYNCHRONIZE CACHE is currently
10460 			 * the only command issued here that requires the
10461 			 * drive be powered up, only do the power up before
10462 			 * sending the Sync Cache command. If additional
10463 			 * commands are added which require a powered up
10464 			 * drive, the following sequence may have to change.
10465 			 *
10466 			 * And finally, note that parallel SCSI on SPARC
10467 			 * only issues a Sync Cache to DVD-RAM, a newly
10468 			 * supported device.
10469 			 */
10470 #if defined(__i386) || defined(__amd64)
10471 			if (un->un_f_sync_cache_supported ||
10472 			    un->un_f_dvdram_writable_device == TRUE) {
10473 #else
10474 			if (un->un_f_dvdram_writable_device == TRUE) {
10475 #endif
10476 				mutex_exit(SD_MUTEX(un));
10477 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10478 					rval =
10479 					    sd_send_scsi_SYNCHRONIZE_CACHE(un,
10480 					    NULL);
10481 					/* ignore error if not supported */
10482 					if (rval == ENOTSUP) {
10483 						rval = 0;
10484 					} else if (rval != 0) {
10485 						rval = EIO;
10486 					}
10487 					sd_pm_exit(un);
10488 				} else {
10489 					rval = EIO;
10490 				}
10491 				mutex_enter(SD_MUTEX(un));
10492 			}
10493 
10494 			/*
10495 			 * For devices which supports DOOR_LOCK, send an ALLOW
10496 			 * MEDIA REMOVAL command, but don't get upset if it
10497 			 * fails. We need to raise the power of the drive before
10498 			 * we can call sd_send_scsi_DOORLOCK()
10499 			 */
10500 			if (un->un_f_doorlock_supported) {
10501 				mutex_exit(SD_MUTEX(un));
10502 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10503 					rval = sd_send_scsi_DOORLOCK(un,
10504 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10505 
10506 					sd_pm_exit(un);
10507 					if (ISCD(un) && (rval != 0) &&
10508 					    (nodelay != 0)) {
10509 						rval = ENXIO;
10510 					}
10511 				} else {
10512 					rval = EIO;
10513 				}
10514 				mutex_enter(SD_MUTEX(un));
10515 			}
10516 
10517 			/*
10518 			 * If a device has removable media, invalidate all
10519 			 * parameters related to media, such as geometry,
10520 			 * blocksize, and blockcount.
10521 			 */
10522 			if (un->un_f_has_removable_media) {
10523 				sr_ejected(un);
10524 			}
10525 
10526 			/*
10527 			 * Destroy the cache (if it exists) which was
10528 			 * allocated for the write maps since this is
10529 			 * the last close for this media.
10530 			 */
10531 			if (un->un_wm_cache) {
10532 				/*
10533 				 * Check if there are pending commands.
10534 				 * and if there are give a warning and
10535 				 * do not destroy the cache.
10536 				 */
10537 				if (un->un_ncmds_in_driver > 0) {
10538 					scsi_log(SD_DEVINFO(un),
10539 					    sd_label, CE_WARN,
10540 					    "Unable to clean up memory "
10541 					    "because of pending I/O\n");
10542 				} else {
10543 					kmem_cache_destroy(
10544 					    un->un_wm_cache);
10545 					un->un_wm_cache = NULL;
10546 				}
10547 			}
10548 		}
10549 	}
10550 
10551 	mutex_exit(SD_MUTEX(un));
10552 	sema_v(&un->un_semoclose);
10553 
10554 	if (otyp == OTYP_LYR) {
10555 		mutex_enter(&sd_detach_mutex);
10556 		/*
10557 		 * The detach routine may run when the layer count
10558 		 * drops to zero.
10559 		 */
10560 		un->un_layer_count--;
10561 		mutex_exit(&sd_detach_mutex);
10562 	}
10563 
10564 	return (rval);
10565 }
10566 
10567 
10568 /*
10569  *    Function: sd_ready_and_valid
10570  *
10571  * Description: Test if device is ready and has a valid geometry.
10572  *
10573  *   Arguments: dev - device number
10574  *		un  - driver soft state (unit) structure
10575  *
10576  * Return Code: SD_READY_VALID		ready and valid label
10577  *		SD_READY_NOT_VALID	ready, geom ops never applicable
10578  *		SD_NOT_READY_VALID	not ready, no label
10579  *
10580  *     Context: Never called at interrupt context.
10581  */
10582 
10583 static int
10584 sd_ready_and_valid(struct sd_lun *un)
10585 {
10586 	struct sd_errstats	*stp;
10587 	uint64_t		capacity;
10588 	uint_t			lbasize;
10589 	int			rval = SD_READY_VALID;
10590 	char			name_str[48];
10591 
10592 	ASSERT(un != NULL);
10593 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10594 
10595 	mutex_enter(SD_MUTEX(un));
10596 	/*
10597 	 * If a device has removable media, we must check if media is
10598 	 * ready when checking if this device is ready and valid.
10599 	 */
10600 	if (un->un_f_has_removable_media) {
10601 		mutex_exit(SD_MUTEX(un));
10602 		if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) {
10603 			rval = SD_NOT_READY_VALID;
10604 			mutex_enter(SD_MUTEX(un));
10605 			goto done;
10606 		}
10607 
10608 		mutex_enter(SD_MUTEX(un));
10609 		if ((un->un_f_geometry_is_valid == FALSE) ||
10610 		    (un->un_f_blockcount_is_valid == FALSE) ||
10611 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10612 
10613 			/* capacity has to be read every open. */
10614 			mutex_exit(SD_MUTEX(un));
10615 			if (sd_send_scsi_READ_CAPACITY(un, &capacity,
10616 			    &lbasize, SD_PATH_DIRECT) != 0) {
10617 				mutex_enter(SD_MUTEX(un));
10618 				un->un_f_geometry_is_valid = FALSE;
10619 				rval = SD_NOT_READY_VALID;
10620 				goto done;
10621 			} else {
10622 				mutex_enter(SD_MUTEX(un));
10623 				sd_update_block_info(un, lbasize, capacity);
10624 			}
10625 		}
10626 
10627 		/*
10628 		 * Check if the media in the device is writable or not.
10629 		 */
10630 		if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) {
10631 			sd_check_for_writable_cd(un);
10632 		}
10633 
10634 	} else {
10635 		/*
10636 		 * Do a test unit ready to clear any unit attention from non-cd
10637 		 * devices.
10638 		 */
10639 		mutex_exit(SD_MUTEX(un));
10640 		(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
10641 		mutex_enter(SD_MUTEX(un));
10642 	}
10643 
10644 
10645 	/*
10646 	 * If this is a non 512 block device, allocate space for
10647 	 * the wmap cache. This is being done here since every time
10648 	 * a media is changed this routine will be called and the
10649 	 * block size is a function of media rather than device.
10650 	 */
10651 	if (un->un_f_non_devbsize_supported && NOT_DEVBSIZE(un)) {
10652 		if (!(un->un_wm_cache)) {
10653 			(void) snprintf(name_str, sizeof (name_str),
10654 			    "%s%d_cache",
10655 			    ddi_driver_name(SD_DEVINFO(un)),
10656 			    ddi_get_instance(SD_DEVINFO(un)));
10657 			un->un_wm_cache = kmem_cache_create(
10658 			    name_str, sizeof (struct sd_w_map),
10659 			    8, sd_wm_cache_constructor,
10660 			    sd_wm_cache_destructor, NULL,
10661 			    (void *)un, NULL, 0);
10662 			if (!(un->un_wm_cache)) {
10663 					rval = ENOMEM;
10664 					goto done;
10665 			}
10666 		}
10667 	}
10668 
10669 	if (un->un_state == SD_STATE_NORMAL) {
10670 		/*
10671 		 * If the target is not yet ready here (defined by a TUR
10672 		 * failure), invalidate the geometry and print an 'offline'
10673 		 * message. This is a legacy message, as the state of the
10674 		 * target is not actually changed to SD_STATE_OFFLINE.
10675 		 *
10676 		 * If the TUR fails for EACCES (Reservation Conflict), it
10677 		 * means there actually is nothing wrong with the target that
10678 		 * would require invalidating the geometry, so continue in
10679 		 * that case as if the TUR was successful.
10680 		 */
10681 		int err;
10682 
10683 		mutex_exit(SD_MUTEX(un));
10684 		err = sd_send_scsi_TEST_UNIT_READY(un, 0);
10685 		mutex_enter(SD_MUTEX(un));
10686 
10687 		if ((err != 0) && (err != EACCES)) {
10688 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10689 			    "offline\n");
10690 			un->un_f_geometry_is_valid = FALSE;
10691 			rval = SD_NOT_READY_VALID;
10692 			goto done;
10693 		}
10694 	}
10695 
10696 	if (un->un_f_format_in_progress == FALSE) {
10697 		/*
10698 		 * Note: sd_validate_geometry may return TRUE, but that does
10699 		 * not necessarily mean un_f_geometry_is_valid == TRUE!
10700 		 */
10701 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
10702 		if (rval == ENOTSUP) {
10703 			if (un->un_f_geometry_is_valid == TRUE)
10704 				rval = 0;
10705 			else {
10706 				rval = SD_READY_NOT_VALID;
10707 				goto done;
10708 			}
10709 		}
10710 		if (rval != 0) {
10711 			/*
10712 			 * We don't check the validity of geometry for
10713 			 * CDROMs. Also we assume we have a good label
10714 			 * even if sd_validate_geometry returned ENOMEM.
10715 			 */
10716 			if (!ISCD(un) && rval != ENOMEM) {
10717 				rval = SD_NOT_READY_VALID;
10718 				goto done;
10719 			}
10720 		}
10721 	}
10722 
10723 #ifdef DOESNTWORK /* on eliteII, see 1118607 */
10724 	/*
10725 	 * check to see if this disk is write protected, if it is and we have
10726 	 * not set read-only, then fail
10727 	 */
10728 	if ((flag & FWRITE) && (sr_check_wp(dev))) {
10729 		New_state(un, SD_STATE_CLOSED);
10730 		goto done;
10731 	}
10732 #endif
10733 
10734 	/*
10735 	 * If this device supports DOOR_LOCK command, try and send
10736 	 * this command to PREVENT MEDIA REMOVAL, but don't get upset
10737 	 * if it fails. For a CD, however, it is an error
10738 	 */
10739 	if (un->un_f_doorlock_supported) {
10740 		mutex_exit(SD_MUTEX(un));
10741 		if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
10742 		    SD_PATH_DIRECT) != 0) && ISCD(un)) {
10743 			rval = SD_NOT_READY_VALID;
10744 			mutex_enter(SD_MUTEX(un));
10745 			goto done;
10746 		}
10747 		mutex_enter(SD_MUTEX(un));
10748 	}
10749 
10750 	/* The state has changed, inform the media watch routines */
10751 	un->un_mediastate = DKIO_INSERTED;
10752 	cv_broadcast(&un->un_state_cv);
10753 	rval = SD_READY_VALID;
10754 
10755 done:
10756 
10757 	/*
10758 	 * Initialize the capacity kstat value, if no media previously
10759 	 * (capacity kstat is 0) and a media has been inserted
10760 	 * (un_blockcount > 0).
10761 	 */
10762 	if (un->un_errstats != NULL) {
10763 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
10764 		if ((stp->sd_capacity.value.ui64 == 0) &&
10765 		    (un->un_f_blockcount_is_valid == TRUE)) {
10766 			stp->sd_capacity.value.ui64 =
10767 			    (uint64_t)((uint64_t)un->un_blockcount *
10768 			    un->un_sys_blocksize);
10769 		}
10770 	}
10771 
10772 	mutex_exit(SD_MUTEX(un));
10773 	return (rval);
10774 }
10775 
10776 
10777 /*
10778  *    Function: sdmin
10779  *
10780  * Description: Routine to limit the size of a data transfer. Used in
10781  *		conjunction with physio(9F).
10782  *
10783  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10784  *
10785  *     Context: Kernel thread context.
10786  */
10787 
10788 static void
10789 sdmin(struct buf *bp)
10790 {
10791 	struct sd_lun	*un;
10792 	int		instance;
10793 
10794 	instance = SDUNIT(bp->b_edev);
10795 
10796 	un = ddi_get_soft_state(sd_state, instance);
10797 	ASSERT(un != NULL);
10798 
10799 	if (bp->b_bcount > un->un_max_xfer_size) {
10800 		bp->b_bcount = un->un_max_xfer_size;
10801 	}
10802 }
10803 
10804 
10805 /*
10806  *    Function: sdread
10807  *
10808  * Description: Driver's read(9e) entry point function.
10809  *
10810  *   Arguments: dev   - device number
10811  *		uio   - structure pointer describing where data is to be stored
10812  *			in user's space
10813  *		cred_p  - user credential pointer
10814  *
10815  * Return Code: ENXIO
10816  *		EIO
10817  *		EINVAL
10818  *		value returned by physio
10819  *
10820  *     Context: Kernel thread context.
10821  */
10822 /* ARGSUSED */
10823 static int
10824 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10825 {
10826 	struct sd_lun	*un = NULL;
10827 	int		secmask;
10828 	int		err;
10829 
10830 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10831 		return (ENXIO);
10832 	}
10833 
10834 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10835 
10836 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10837 		mutex_enter(SD_MUTEX(un));
10838 		/*
10839 		 * Because the call to sd_ready_and_valid will issue I/O we
10840 		 * must wait here if either the device is suspended or
10841 		 * if it's power level is changing.
10842 		 */
10843 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10844 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10845 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10846 		}
10847 		un->un_ncmds_in_driver++;
10848 		mutex_exit(SD_MUTEX(un));
10849 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
10850 			mutex_enter(SD_MUTEX(un));
10851 			un->un_ncmds_in_driver--;
10852 			ASSERT(un->un_ncmds_in_driver >= 0);
10853 			mutex_exit(SD_MUTEX(un));
10854 			return (EIO);
10855 		}
10856 		mutex_enter(SD_MUTEX(un));
10857 		un->un_ncmds_in_driver--;
10858 		ASSERT(un->un_ncmds_in_driver >= 0);
10859 		mutex_exit(SD_MUTEX(un));
10860 	}
10861 
10862 	/*
10863 	 * Read requests are restricted to multiples of the system block size.
10864 	 */
10865 	secmask = un->un_sys_blocksize - 1;
10866 
10867 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10868 		SD_ERROR(SD_LOG_READ_WRITE, un,
10869 		    "sdread: file offset not modulo %d\n",
10870 		    un->un_sys_blocksize);
10871 		err = EINVAL;
10872 	} else if (uio->uio_iov->iov_len & (secmask)) {
10873 		SD_ERROR(SD_LOG_READ_WRITE, un,
10874 		    "sdread: transfer length not modulo %d\n",
10875 		    un->un_sys_blocksize);
10876 		err = EINVAL;
10877 	} else {
10878 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10879 	}
10880 	return (err);
10881 }
10882 
10883 
10884 /*
10885  *    Function: sdwrite
10886  *
10887  * Description: Driver's write(9e) entry point function.
10888  *
10889  *   Arguments: dev   - device number
10890  *		uio   - structure pointer describing where data is stored in
10891  *			user's space
10892  *		cred_p  - user credential pointer
10893  *
10894  * Return Code: ENXIO
10895  *		EIO
10896  *		EINVAL
10897  *		value returned by physio
10898  *
10899  *     Context: Kernel thread context.
10900  */
10901 /* ARGSUSED */
10902 static int
10903 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10904 {
10905 	struct sd_lun	*un = NULL;
10906 	int		secmask;
10907 	int		err;
10908 
10909 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10910 		return (ENXIO);
10911 	}
10912 
10913 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10914 
10915 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10916 		mutex_enter(SD_MUTEX(un));
10917 		/*
10918 		 * Because the call to sd_ready_and_valid will issue I/O we
10919 		 * must wait here if either the device is suspended or
10920 		 * if it's power level is changing.
10921 		 */
10922 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10923 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10924 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10925 		}
10926 		un->un_ncmds_in_driver++;
10927 		mutex_exit(SD_MUTEX(un));
10928 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
10929 			mutex_enter(SD_MUTEX(un));
10930 			un->un_ncmds_in_driver--;
10931 			ASSERT(un->un_ncmds_in_driver >= 0);
10932 			mutex_exit(SD_MUTEX(un));
10933 			return (EIO);
10934 		}
10935 		mutex_enter(SD_MUTEX(un));
10936 		un->un_ncmds_in_driver--;
10937 		ASSERT(un->un_ncmds_in_driver >= 0);
10938 		mutex_exit(SD_MUTEX(un));
10939 	}
10940 
10941 	/*
10942 	 * Write requests are restricted to multiples of the system block size.
10943 	 */
10944 	secmask = un->un_sys_blocksize - 1;
10945 
10946 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10947 		SD_ERROR(SD_LOG_READ_WRITE, un,
10948 		    "sdwrite: file offset not modulo %d\n",
10949 		    un->un_sys_blocksize);
10950 		err = EINVAL;
10951 	} else if (uio->uio_iov->iov_len & (secmask)) {
10952 		SD_ERROR(SD_LOG_READ_WRITE, un,
10953 		    "sdwrite: transfer length not modulo %d\n",
10954 		    un->un_sys_blocksize);
10955 		err = EINVAL;
10956 	} else {
10957 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
10958 	}
10959 	return (err);
10960 }
10961 
10962 
10963 /*
10964  *    Function: sdaread
10965  *
10966  * Description: Driver's aread(9e) entry point function.
10967  *
10968  *   Arguments: dev   - device number
10969  *		aio   - structure pointer describing where data is to be stored
10970  *		cred_p  - user credential pointer
10971  *
10972  * Return Code: ENXIO
10973  *		EIO
10974  *		EINVAL
10975  *		value returned by aphysio
10976  *
10977  *     Context: Kernel thread context.
10978  */
10979 /* ARGSUSED */
10980 static int
10981 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10982 {
10983 	struct sd_lun	*un = NULL;
10984 	struct uio	*uio = aio->aio_uio;
10985 	int		secmask;
10986 	int		err;
10987 
10988 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10989 		return (ENXIO);
10990 	}
10991 
10992 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10993 
10994 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10995 		mutex_enter(SD_MUTEX(un));
10996 		/*
10997 		 * Because the call to sd_ready_and_valid will issue I/O we
10998 		 * must wait here if either the device is suspended or
10999 		 * if it's power level is changing.
11000 		 */
11001 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11002 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11003 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11004 		}
11005 		un->un_ncmds_in_driver++;
11006 		mutex_exit(SD_MUTEX(un));
11007 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11008 			mutex_enter(SD_MUTEX(un));
11009 			un->un_ncmds_in_driver--;
11010 			ASSERT(un->un_ncmds_in_driver >= 0);
11011 			mutex_exit(SD_MUTEX(un));
11012 			return (EIO);
11013 		}
11014 		mutex_enter(SD_MUTEX(un));
11015 		un->un_ncmds_in_driver--;
11016 		ASSERT(un->un_ncmds_in_driver >= 0);
11017 		mutex_exit(SD_MUTEX(un));
11018 	}
11019 
11020 	/*
11021 	 * Read requests are restricted to multiples of the system block size.
11022 	 */
11023 	secmask = un->un_sys_blocksize - 1;
11024 
11025 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11026 		SD_ERROR(SD_LOG_READ_WRITE, un,
11027 		    "sdaread: file offset not modulo %d\n",
11028 		    un->un_sys_blocksize);
11029 		err = EINVAL;
11030 	} else if (uio->uio_iov->iov_len & (secmask)) {
11031 		SD_ERROR(SD_LOG_READ_WRITE, un,
11032 		    "sdaread: transfer length not modulo %d\n",
11033 		    un->un_sys_blocksize);
11034 		err = EINVAL;
11035 	} else {
11036 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
11037 	}
11038 	return (err);
11039 }
11040 
11041 
11042 /*
11043  *    Function: sdawrite
11044  *
11045  * Description: Driver's awrite(9e) entry point function.
11046  *
11047  *   Arguments: dev   - device number
11048  *		aio   - structure pointer describing where data is stored
11049  *		cred_p  - user credential pointer
11050  *
11051  * Return Code: ENXIO
11052  *		EIO
11053  *		EINVAL
11054  *		value returned by aphysio
11055  *
11056  *     Context: Kernel thread context.
11057  */
11058 /* ARGSUSED */
11059 static int
11060 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
11061 {
11062 	struct sd_lun	*un = NULL;
11063 	struct uio	*uio = aio->aio_uio;
11064 	int		secmask;
11065 	int		err;
11066 
11067 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
11068 		return (ENXIO);
11069 	}
11070 
11071 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11072 
11073 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
11074 		mutex_enter(SD_MUTEX(un));
11075 		/*
11076 		 * Because the call to sd_ready_and_valid will issue I/O we
11077 		 * must wait here if either the device is suspended or
11078 		 * if it's power level is changing.
11079 		 */
11080 		while ((un->un_state == SD_STATE_SUSPENDED) ||
11081 		    (un->un_state == SD_STATE_PM_CHANGING)) {
11082 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11083 		}
11084 		un->un_ncmds_in_driver++;
11085 		mutex_exit(SD_MUTEX(un));
11086 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
11087 			mutex_enter(SD_MUTEX(un));
11088 			un->un_ncmds_in_driver--;
11089 			ASSERT(un->un_ncmds_in_driver >= 0);
11090 			mutex_exit(SD_MUTEX(un));
11091 			return (EIO);
11092 		}
11093 		mutex_enter(SD_MUTEX(un));
11094 		un->un_ncmds_in_driver--;
11095 		ASSERT(un->un_ncmds_in_driver >= 0);
11096 		mutex_exit(SD_MUTEX(un));
11097 	}
11098 
11099 	/*
11100 	 * Write requests are restricted to multiples of the system block size.
11101 	 */
11102 	secmask = un->un_sys_blocksize - 1;
11103 
11104 	if (uio->uio_loffset & ((offset_t)(secmask))) {
11105 		SD_ERROR(SD_LOG_READ_WRITE, un,
11106 		    "sdawrite: file offset not modulo %d\n",
11107 		    un->un_sys_blocksize);
11108 		err = EINVAL;
11109 	} else if (uio->uio_iov->iov_len & (secmask)) {
11110 		SD_ERROR(SD_LOG_READ_WRITE, un,
11111 		    "sdawrite: transfer length not modulo %d\n",
11112 		    un->un_sys_blocksize);
11113 		err = EINVAL;
11114 	} else {
11115 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
11116 	}
11117 	return (err);
11118 }
11119 
11120 
11121 
11122 
11123 
11124 /*
11125  * Driver IO processing follows the following sequence:
11126  *
11127  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
11128  *         |                |                     ^
11129  *         v                v                     |
11130  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
11131  *         |                |                     |                   |
11132  *         v                |                     |                   |
11133  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
11134  *         |                |                     ^                   ^
11135  *         v                v                     |                   |
11136  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
11137  *         |                |                     |                   |
11138  *     +---+                |                     +------------+      +-------+
11139  *     |                    |                                  |              |
11140  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11141  *     |                    v                                  |              |
11142  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
11143  *     |                    |                                  ^              |
11144  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11145  *     |                    v                                  |              |
11146  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
11147  *     |                    |                                  ^              |
11148  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
11149  *     |                    v                                  |              |
11150  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
11151  *     |                    |                                  ^              |
11152  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
11153  *     |                    v                                  |              |
11154  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
11155  *     |                    |                                  ^              |
11156  *     |                    |                                  |              |
11157  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
11158  *                          |                           ^
11159  *                          v                           |
11160  *                   sd_core_iostart()                  |
11161  *                          |                           |
11162  *                          |                           +------>(*destroypkt)()
11163  *                          +-> sd_start_cmds() <-+     |           |
11164  *                          |                     |     |           v
11165  *                          |                     |     |  scsi_destroy_pkt(9F)
11166  *                          |                     |     |
11167  *                          +->(*initpkt)()       +- sdintr()
11168  *                          |  |                        |  |
11169  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
11170  *                          |  +-> scsi_setup_cdb(9F)   |
11171  *                          |                           |
11172  *                          +--> scsi_transport(9F)     |
11173  *                                     |                |
11174  *                                     +----> SCSA ---->+
11175  *
11176  *
11177  * This code is based upon the following presumtions:
11178  *
11179  *   - iostart and iodone functions operate on buf(9S) structures. These
11180  *     functions perform the necessary operations on the buf(9S) and pass
11181  *     them along to the next function in the chain by using the macros
11182  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
11183  *     (for iodone side functions).
11184  *
11185  *   - The iostart side functions may sleep. The iodone side functions
11186  *     are called under interrupt context and may NOT sleep. Therefore
11187  *     iodone side functions also may not call iostart side functions.
11188  *     (NOTE: iostart side functions should NOT sleep for memory, as
11189  *     this could result in deadlock.)
11190  *
11191  *   - An iostart side function may call its corresponding iodone side
11192  *     function directly (if necessary).
11193  *
11194  *   - In the event of an error, an iostart side function can return a buf(9S)
11195  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
11196  *     b_error in the usual way of course).
11197  *
11198  *   - The taskq mechanism may be used by the iodone side functions to dispatch
11199  *     requests to the iostart side functions.  The iostart side functions in
11200  *     this case would be called under the context of a taskq thread, so it's
11201  *     OK for them to block/sleep/spin in this case.
11202  *
11203  *   - iostart side functions may allocate "shadow" buf(9S) structs and
11204  *     pass them along to the next function in the chain.  The corresponding
11205  *     iodone side functions must coalesce the "shadow" bufs and return
11206  *     the "original" buf to the next higher layer.
11207  *
11208  *   - The b_private field of the buf(9S) struct holds a pointer to
11209  *     an sd_xbuf struct, which contains information needed to
11210  *     construct the scsi_pkt for the command.
11211  *
11212  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
11213  *     layer must acquire & release the SD_MUTEX(un) as needed.
11214  */
11215 
11216 
11217 /*
11218  * Create taskq for all targets in the system. This is created at
11219  * _init(9E) and destroyed at _fini(9E).
11220  *
11221  * Note: here we set the minalloc to a reasonably high number to ensure that
11222  * we will have an adequate supply of task entries available at interrupt time.
11223  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
11224  * sd_create_taskq().  Since we do not want to sleep for allocations at
11225  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
11226  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
11227  * requests any one instant in time.
11228  */
11229 #define	SD_TASKQ_NUMTHREADS	8
11230 #define	SD_TASKQ_MINALLOC	256
11231 #define	SD_TASKQ_MAXALLOC	256
11232 
11233 static taskq_t	*sd_tq = NULL;
11234 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_tq))
11235 
11236 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
11237 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
11238 
11239 /*
11240  * The following task queue is being created for the write part of
11241  * read-modify-write of non-512 block size devices.
11242  * Limit the number of threads to 1 for now. This number has been choosen
11243  * considering the fact that it applies only to dvd ram drives/MO drives
11244  * currently. Performance for which is not main criteria at this stage.
11245  * Note: It needs to be explored if we can use a single taskq in future
11246  */
11247 #define	SD_WMR_TASKQ_NUMTHREADS	1
11248 static taskq_t	*sd_wmr_tq = NULL;
11249 _NOTE(SCHEME_PROTECTS_DATA("stable data", sd_wmr_tq))
11250 
11251 /*
11252  *    Function: sd_taskq_create
11253  *
11254  * Description: Create taskq thread(s) and preallocate task entries
11255  *
11256  * Return Code: Returns a pointer to the allocated taskq_t.
11257  *
11258  *     Context: Can sleep. Requires blockable context.
11259  *
11260  *       Notes: - The taskq() facility currently is NOT part of the DDI.
11261  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
11262  *		- taskq_create() will block for memory, also it will panic
11263  *		  if it cannot create the requested number of threads.
11264  *		- Currently taskq_create() creates threads that cannot be
11265  *		  swapped.
11266  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
11267  *		  supply of taskq entries at interrupt time (ie, so that we
11268  *		  do not have to sleep for memory)
11269  */
11270 
11271 static void
11272 sd_taskq_create(void)
11273 {
11274 	char	taskq_name[TASKQ_NAMELEN];
11275 
11276 	ASSERT(sd_tq == NULL);
11277 	ASSERT(sd_wmr_tq == NULL);
11278 
11279 	(void) snprintf(taskq_name, sizeof (taskq_name),
11280 	    "%s_drv_taskq", sd_label);
11281 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
11282 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11283 	    TASKQ_PREPOPULATE));
11284 
11285 	(void) snprintf(taskq_name, sizeof (taskq_name),
11286 	    "%s_rmw_taskq", sd_label);
11287 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11288 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11289 	    TASKQ_PREPOPULATE));
11290 }
11291 
11292 
11293 /*
11294  *    Function: sd_taskq_delete
11295  *
11296  * Description: Complementary cleanup routine for sd_taskq_create().
11297  *
11298  *     Context: Kernel thread context.
11299  */
11300 
11301 static void
11302 sd_taskq_delete(void)
11303 {
11304 	ASSERT(sd_tq != NULL);
11305 	ASSERT(sd_wmr_tq != NULL);
11306 	taskq_destroy(sd_tq);
11307 	taskq_destroy(sd_wmr_tq);
11308 	sd_tq = NULL;
11309 	sd_wmr_tq = NULL;
11310 }
11311 
11312 
11313 /*
11314  *    Function: sdstrategy
11315  *
11316  * Description: Driver's strategy (9E) entry point function.
11317  *
11318  *   Arguments: bp - pointer to buf(9S)
11319  *
11320  * Return Code: Always returns zero
11321  *
11322  *     Context: Kernel thread context.
11323  */
11324 
11325 static int
11326 sdstrategy(struct buf *bp)
11327 {
11328 	struct sd_lun *un;
11329 
11330 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11331 	if (un == NULL) {
11332 		bioerror(bp, EIO);
11333 		bp->b_resid = bp->b_bcount;
11334 		biodone(bp);
11335 		return (0);
11336 	}
11337 	/* As was done in the past, fail new cmds. if state is dumping. */
11338 	if (un->un_state == SD_STATE_DUMPING) {
11339 		bioerror(bp, ENXIO);
11340 		bp->b_resid = bp->b_bcount;
11341 		biodone(bp);
11342 		return (0);
11343 	}
11344 
11345 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11346 
11347 	/*
11348 	 * Commands may sneak in while we released the mutex in
11349 	 * DDI_SUSPEND, we should block new commands. However, old
11350 	 * commands that are still in the driver at this point should
11351 	 * still be allowed to drain.
11352 	 */
11353 	mutex_enter(SD_MUTEX(un));
11354 	/*
11355 	 * Must wait here if either the device is suspended or
11356 	 * if it's power level is changing.
11357 	 */
11358 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11359 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11360 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11361 	}
11362 
11363 	un->un_ncmds_in_driver++;
11364 
11365 	/*
11366 	 * atapi: Since we are running the CD for now in PIO mode we need to
11367 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11368 	 * the HBA's init_pkt routine.
11369 	 */
11370 	if (un->un_f_cfg_is_atapi == TRUE) {
11371 		mutex_exit(SD_MUTEX(un));
11372 		bp_mapin(bp);
11373 		mutex_enter(SD_MUTEX(un));
11374 	}
11375 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11376 	    un->un_ncmds_in_driver);
11377 
11378 	mutex_exit(SD_MUTEX(un));
11379 
11380 	/*
11381 	 * This will (eventually) allocate the sd_xbuf area and
11382 	 * call sd_xbuf_strategy().  We just want to return the
11383 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11384 	 * imized tail call which saves us a stack frame.
11385 	 */
11386 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11387 }
11388 
11389 
11390 /*
11391  *    Function: sd_xbuf_strategy
11392  *
11393  * Description: Function for initiating IO operations via the
11394  *		ddi_xbuf_qstrategy() mechanism.
11395  *
11396  *     Context: Kernel thread context.
11397  */
11398 
11399 static void
11400 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11401 {
11402 	struct sd_lun *un = arg;
11403 
11404 	ASSERT(bp != NULL);
11405 	ASSERT(xp != NULL);
11406 	ASSERT(un != NULL);
11407 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11408 
11409 	/*
11410 	 * Initialize the fields in the xbuf and save a pointer to the
11411 	 * xbuf in bp->b_private.
11412 	 */
11413 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11414 
11415 	/* Send the buf down the iostart chain */
11416 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11417 }
11418 
11419 
11420 /*
11421  *    Function: sd_xbuf_init
11422  *
11423  * Description: Prepare the given sd_xbuf struct for use.
11424  *
11425  *   Arguments: un - ptr to softstate
11426  *		bp - ptr to associated buf(9S)
11427  *		xp - ptr to associated sd_xbuf
11428  *		chain_type - IO chain type to use:
11429  *			SD_CHAIN_NULL
11430  *			SD_CHAIN_BUFIO
11431  *			SD_CHAIN_USCSI
11432  *			SD_CHAIN_DIRECT
11433  *			SD_CHAIN_DIRECT_PRIORITY
11434  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11435  *			initialization; may be NULL if none.
11436  *
11437  *     Context: Kernel thread context
11438  */
11439 
11440 static void
11441 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11442 	uchar_t chain_type, void *pktinfop)
11443 {
11444 	int index;
11445 
11446 	ASSERT(un != NULL);
11447 	ASSERT(bp != NULL);
11448 	ASSERT(xp != NULL);
11449 
11450 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11451 	    bp, chain_type);
11452 
11453 	xp->xb_un	= un;
11454 	xp->xb_pktp	= NULL;
11455 	xp->xb_pktinfo	= pktinfop;
11456 	xp->xb_private	= bp->b_private;
11457 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11458 
11459 	/*
11460 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11461 	 * upon the specified chain type to use.
11462 	 */
11463 	switch (chain_type) {
11464 	case SD_CHAIN_NULL:
11465 		/*
11466 		 * Fall thru to just use the values for the buf type, even
11467 		 * tho for the NULL chain these values will never be used.
11468 		 */
11469 		/* FALLTHRU */
11470 	case SD_CHAIN_BUFIO:
11471 		index = un->un_buf_chain_type;
11472 		break;
11473 	case SD_CHAIN_USCSI:
11474 		index = un->un_uscsi_chain_type;
11475 		break;
11476 	case SD_CHAIN_DIRECT:
11477 		index = un->un_direct_chain_type;
11478 		break;
11479 	case SD_CHAIN_DIRECT_PRIORITY:
11480 		index = un->un_priority_chain_type;
11481 		break;
11482 	default:
11483 		/* We're really broken if we ever get here... */
11484 		panic("sd_xbuf_init: illegal chain type!");
11485 		/*NOTREACHED*/
11486 	}
11487 
11488 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11489 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11490 
11491 	/*
11492 	 * It might be a bit easier to simply bzero the entire xbuf above,
11493 	 * but it turns out that since we init a fair number of members anyway,
11494 	 * we save a fair number cycles by doing explicit assignment of zero.
11495 	 */
11496 	xp->xb_pkt_flags	= 0;
11497 	xp->xb_dma_resid	= 0;
11498 	xp->xb_retry_count	= 0;
11499 	xp->xb_victim_retry_count = 0;
11500 	xp->xb_ua_retry_count	= 0;
11501 	xp->xb_sense_bp		= NULL;
11502 	xp->xb_sense_status	= 0;
11503 	xp->xb_sense_state	= 0;
11504 	xp->xb_sense_resid	= 0;
11505 
11506 	bp->b_private	= xp;
11507 	bp->b_flags	&= ~(B_DONE | B_ERROR);
11508 	bp->b_resid	= 0;
11509 	bp->av_forw	= NULL;
11510 	bp->av_back	= NULL;
11511 	bioerror(bp, 0);
11512 
11513 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11514 }
11515 
11516 
11517 /*
11518  *    Function: sd_uscsi_strategy
11519  *
11520  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11521  *
11522  *   Arguments: bp - buf struct ptr
11523  *
11524  * Return Code: Always returns 0
11525  *
11526  *     Context: Kernel thread context
11527  */
11528 
11529 static int
11530 sd_uscsi_strategy(struct buf *bp)
11531 {
11532 	struct sd_lun		*un;
11533 	struct sd_uscsi_info	*uip;
11534 	struct sd_xbuf		*xp;
11535 	uchar_t			chain_type;
11536 
11537 	ASSERT(bp != NULL);
11538 
11539 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11540 	if (un == NULL) {
11541 		bioerror(bp, EIO);
11542 		bp->b_resid = bp->b_bcount;
11543 		biodone(bp);
11544 		return (0);
11545 	}
11546 
11547 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11548 
11549 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11550 
11551 	mutex_enter(SD_MUTEX(un));
11552 	/*
11553 	 * atapi: Since we are running the CD for now in PIO mode we need to
11554 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11555 	 * the HBA's init_pkt routine.
11556 	 */
11557 	if (un->un_f_cfg_is_atapi == TRUE) {
11558 		mutex_exit(SD_MUTEX(un));
11559 		bp_mapin(bp);
11560 		mutex_enter(SD_MUTEX(un));
11561 	}
11562 	un->un_ncmds_in_driver++;
11563 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11564 	    un->un_ncmds_in_driver);
11565 	mutex_exit(SD_MUTEX(un));
11566 
11567 	/*
11568 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11569 	 */
11570 	ASSERT(bp->b_private != NULL);
11571 	uip = (struct sd_uscsi_info *)bp->b_private;
11572 
11573 	switch (uip->ui_flags) {
11574 	case SD_PATH_DIRECT:
11575 		chain_type = SD_CHAIN_DIRECT;
11576 		break;
11577 	case SD_PATH_DIRECT_PRIORITY:
11578 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
11579 		break;
11580 	default:
11581 		chain_type = SD_CHAIN_USCSI;
11582 		break;
11583 	}
11584 
11585 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
11586 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11587 
11588 	/* Use the index obtained within xbuf_init */
11589 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11590 
11591 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11592 
11593 	return (0);
11594 }
11595 
11596 
11597 /*
11598  * These routines perform raw i/o operations.
11599  */
11600 /*ARGSUSED*/
11601 static void
11602 sduscsimin(struct buf *bp)
11603 {
11604 	/*
11605 	 * do not break up because the CDB count would then
11606 	 * be incorrect and data underruns would result (incomplete
11607 	 * read/writes which would be retried and then failed, see
11608 	 * sdintr().
11609 	 */
11610 }
11611 
11612 
11613 
11614 /*
11615  *    Function: sd_send_scsi_cmd
11616  *
11617  * Description: Runs a USCSI command for user (when called thru sdioctl),
11618  *		or for the driver
11619  *
11620  *   Arguments: dev - the dev_t for the device
11621  *		incmd - ptr to a valid uscsi_cmd struct
11622  *		cdbspace - UIO_USERSPACE or UIO_SYSSPACE
11623  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11624  *		rqbufspace - UIO_USERSPACE or UIO_SYSSPACE
11625  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11626  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11627  *			to use the USCSI "direct" chain and bypass the normal
11628  *			command waitq.
11629  *
11630  * Return Code: 0 -  successful completion of the given command
11631  *		EIO - scsi_reset() failed, or see biowait()/physio() codes.
11632  *		ENXIO  - soft state not found for specified dev
11633  *		EINVAL
11634  *		EFAULT - copyin/copyout error
11635  *		return code of biowait(9F) or physio(9F):
11636  *			EIO - IO error, caller may check incmd->uscsi_status
11637  *			ENXIO
11638  *			EACCES - reservation conflict
11639  *
11640  *     Context: Waits for command to complete. Can sleep.
11641  */
11642 
11643 static int
11644 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd,
11645 	enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace,
11646 	int path_flag)
11647 {
11648 	struct sd_uscsi_info	*uip;
11649 	struct uscsi_cmd	*uscmd;
11650 	struct sd_lun	*un;
11651 	struct buf	*bp;
11652 	int	rval;
11653 	int	flags;
11654 
11655 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11656 	if (un == NULL) {
11657 		return (ENXIO);
11658 	}
11659 
11660 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11661 
11662 #ifdef SDDEBUG
11663 	switch (dataspace) {
11664 	case UIO_USERSPACE:
11665 		SD_TRACE(SD_LOG_IO, un,
11666 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un);
11667 		break;
11668 	case UIO_SYSSPACE:
11669 		SD_TRACE(SD_LOG_IO, un,
11670 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un);
11671 		break;
11672 	default:
11673 		SD_TRACE(SD_LOG_IO, un,
11674 		    "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un);
11675 		break;
11676 	}
11677 #endif
11678 
11679 	/*
11680 	 * Perform resets directly; no need to generate a command to do it.
11681 	 */
11682 	if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) {
11683 		flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ?
11684 		    RESET_ALL : RESET_TARGET;
11685 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n");
11686 		if (scsi_reset(SD_ADDRESS(un), flags) == 0) {
11687 			/* Reset attempt was unsuccessful */
11688 			SD_TRACE(SD_LOG_IO, un,
11689 			    "sd_send_scsi_cmd: reset: failure\n");
11690 			return (EIO);
11691 		}
11692 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n");
11693 		return (0);
11694 	}
11695 
11696 	/* Perfunctory sanity check... */
11697 	if (incmd->uscsi_cdblen <= 0) {
11698 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11699 		    "invalid uscsi_cdblen, returning EINVAL\n");
11700 		return (EINVAL);
11701 	}
11702 
11703 	/*
11704 	 * In order to not worry about where the uscsi structure came from
11705 	 * (or where the cdb it points to came from) we're going to make
11706 	 * kmem_alloc'd copies of them here. This will also allow reference
11707 	 * to the data they contain long after this process has gone to
11708 	 * sleep and its kernel stack has been unmapped, etc.
11709 	 *
11710 	 * First get some memory for the uscsi_cmd struct and copy the
11711 	 * contents of the given uscsi_cmd struct into it.
11712 	 */
11713 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11714 	bcopy(incmd, uscmd, sizeof (struct uscsi_cmd));
11715 
11716 	SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd",
11717 	    (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX);
11718 
11719 	/*
11720 	 * Now get some space for the CDB, and copy the given CDB into
11721 	 * it. Use ddi_copyin() in case the data is in user space.
11722 	 */
11723 	uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP);
11724 	flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0;
11725 	if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb,
11726 	    (uint_t)incmd->uscsi_cdblen, flags) != 0) {
11727 		kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen);
11728 		kmem_free(uscmd, sizeof (struct uscsi_cmd));
11729 		return (EFAULT);
11730 	}
11731 
11732 	SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB",
11733 	    (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX);
11734 
11735 	bp = getrbuf(KM_SLEEP);
11736 
11737 	/*
11738 	 * Allocate an sd_uscsi_info struct and fill it with the info
11739 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
11740 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
11741 	 * since we allocate the buf here in this function, we do not
11742 	 * need to preserve the prior contents of b_private.
11743 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
11744 	 */
11745 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11746 	uip->ui_flags = path_flag;
11747 	uip->ui_cmdp  = uscmd;
11748 	bp->b_private = uip;
11749 
11750 	/*
11751 	 * Initialize Request Sense buffering, if requested.
11752 	 */
11753 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
11754 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
11755 		/*
11756 		 * Here uscmd->uscsi_rqbuf currently points to the caller's
11757 		 * buffer, but we replace this with a kernel buffer that
11758 		 * we allocate to use with the sense data. The sense data
11759 		 * (if present) gets copied into this new buffer before the
11760 		 * command is completed.  Then we copy the sense data from
11761 		 * our allocated buf into the caller's buffer below. Note
11762 		 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used
11763 		 * below to perform the copy back to the caller's buf.
11764 		 */
11765 		uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
11766 		if (rqbufspace == UIO_USERSPACE) {
11767 			uscmd->uscsi_rqlen   = SENSE_LENGTH;
11768 			uscmd->uscsi_rqresid = SENSE_LENGTH;
11769 		} else {
11770 			uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen);
11771 			uscmd->uscsi_rqlen   = rlen;
11772 			uscmd->uscsi_rqresid = rlen;
11773 		}
11774 	} else {
11775 		uscmd->uscsi_rqbuf = NULL;
11776 		uscmd->uscsi_rqlen   = 0;
11777 		uscmd->uscsi_rqresid = 0;
11778 	}
11779 
11780 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p  rqlen:%d\n",
11781 	    uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen);
11782 
11783 	if (un->un_f_is_fibre == FALSE) {
11784 		/*
11785 		 * Force asynchronous mode, if necessary.  Doing this here
11786 		 * has the unfortunate effect of running other queued
11787 		 * commands async also, but since the main purpose of this
11788 		 * capability is downloading new drive firmware, we can
11789 		 * probably live with it.
11790 		 */
11791 		if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) {
11792 			if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1)
11793 				== 1) {
11794 				if (scsi_ifsetcap(SD_ADDRESS(un),
11795 					    "synchronous", 0, 1) == 1) {
11796 					SD_TRACE(SD_LOG_IO, un,
11797 					"sd_send_scsi_cmd: forced async ok\n");
11798 				} else {
11799 					SD_TRACE(SD_LOG_IO, un,
11800 					"sd_send_scsi_cmd:\
11801 					forced async failed\n");
11802 					rval = EINVAL;
11803 					goto done;
11804 				}
11805 			}
11806 		}
11807 
11808 		/*
11809 		 * Re-enable synchronous mode, if requested
11810 		 */
11811 		if (uscmd->uscsi_flags & USCSI_SYNC) {
11812 			if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1)
11813 				== 0) {
11814 				int i = scsi_ifsetcap(SD_ADDRESS(un),
11815 						"synchronous", 1, 1);
11816 				SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11817 					"re-enabled sync %s\n",
11818 					(i == 1) ? "ok" : "failed");
11819 			}
11820 		}
11821 	}
11822 
11823 	/*
11824 	 * Commands sent with priority are intended for error recovery
11825 	 * situations, and do not have retries performed.
11826 	 */
11827 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
11828 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
11829 	}
11830 
11831 	/*
11832 	 * If we're going to do actual I/O, let physio do all the right things
11833 	 */
11834 	if (uscmd->uscsi_buflen != 0) {
11835 		struct iovec	aiov;
11836 		struct uio	auio;
11837 		struct uio	*uio = &auio;
11838 
11839 		bzero(&auio, sizeof (struct uio));
11840 		bzero(&aiov, sizeof (struct iovec));
11841 		aiov.iov_base = uscmd->uscsi_bufaddr;
11842 		aiov.iov_len  = uscmd->uscsi_buflen;
11843 		uio->uio_iov  = &aiov;
11844 
11845 		uio->uio_iovcnt  = 1;
11846 		uio->uio_resid   = uscmd->uscsi_buflen;
11847 		uio->uio_segflg  = dataspace;
11848 
11849 		/*
11850 		 * physio() will block here until the command completes....
11851 		 */
11852 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n");
11853 
11854 		rval = physio(sd_uscsi_strategy, bp, dev,
11855 		    ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE),
11856 		    sduscsimin, uio);
11857 
11858 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11859 		    "returned from physio with 0x%x\n", rval);
11860 
11861 	} else {
11862 		/*
11863 		 * We have to mimic what physio would do here! Argh!
11864 		 */
11865 		bp->b_flags  = B_BUSY |
11866 		    ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE);
11867 		bp->b_edev   = dev;
11868 		bp->b_dev    = cmpdev(dev);	/* maybe unnecessary? */
11869 		bp->b_bcount = 0;
11870 		bp->b_blkno  = 0;
11871 
11872 		SD_TRACE(SD_LOG_IO, un,
11873 		    "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n");
11874 
11875 		(void) sd_uscsi_strategy(bp);
11876 
11877 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n");
11878 
11879 		rval = biowait(bp);
11880 
11881 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11882 		    "returned from  biowait with 0x%x\n", rval);
11883 	}
11884 
11885 done:
11886 
11887 #ifdef SDDEBUG
11888 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11889 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
11890 	    uscmd->uscsi_status, uscmd->uscsi_resid);
11891 	if (uscmd->uscsi_bufaddr != NULL) {
11892 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11893 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
11894 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
11895 		if (dataspace == UIO_SYSSPACE) {
11896 			SD_DUMP_MEMORY(un, SD_LOG_IO,
11897 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
11898 			    uscmd->uscsi_buflen, SD_LOG_HEX);
11899 		}
11900 	}
11901 #endif
11902 
11903 	/*
11904 	 * Get the status and residual to return to the caller.
11905 	 */
11906 	incmd->uscsi_status = uscmd->uscsi_status;
11907 	incmd->uscsi_resid  = uscmd->uscsi_resid;
11908 
11909 	/*
11910 	 * If the caller wants sense data, copy back whatever sense data
11911 	 * we may have gotten, and update the relevant rqsense info.
11912 	 */
11913 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
11914 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
11915 
11916 		int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid;
11917 		rqlen = min(((int)incmd->uscsi_rqlen), rqlen);
11918 
11919 		/* Update the Request Sense status and resid */
11920 		incmd->uscsi_rqresid  = incmd->uscsi_rqlen - rqlen;
11921 		incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus;
11922 
11923 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11924 		    "uscsi_rqstatus: 0x%02x  uscsi_rqresid:0x%x\n",
11925 		    incmd->uscsi_rqstatus, incmd->uscsi_rqresid);
11926 
11927 		/* Copy out the sense data for user processes */
11928 		if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) {
11929 			int flags =
11930 			    (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL;
11931 			if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf,
11932 			    rqlen, flags) != 0) {
11933 				rval = EFAULT;
11934 			}
11935 			/*
11936 			 * Note: Can't touch incmd->uscsi_rqbuf so use
11937 			 * uscmd->uscsi_rqbuf instead. They're the same.
11938 			 */
11939 			SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11940 			    "incmd->uscsi_rqbuf: 0x%p  rqlen:%d\n",
11941 			    incmd->uscsi_rqbuf, rqlen);
11942 			SD_DUMP_MEMORY(un, SD_LOG_IO, "rq",
11943 			    (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX);
11944 		}
11945 	}
11946 
11947 	/*
11948 	 * Free allocated resources and return; mapout the buf in case it was
11949 	 * mapped in by a lower layer.
11950 	 */
11951 	bp_mapout(bp);
11952 	freerbuf(bp);
11953 	kmem_free(uip, sizeof (struct sd_uscsi_info));
11954 	if (uscmd->uscsi_rqbuf != NULL) {
11955 		kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
11956 	}
11957 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
11958 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
11959 
11960 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n");
11961 
11962 	return (rval);
11963 }
11964 
11965 
11966 /*
11967  *    Function: sd_buf_iodone
11968  *
11969  * Description: Frees the sd_xbuf & returns the buf to its originator.
11970  *
11971  *     Context: May be called from interrupt context.
11972  */
11973 /* ARGSUSED */
11974 static void
11975 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
11976 {
11977 	struct sd_xbuf *xp;
11978 
11979 	ASSERT(un != NULL);
11980 	ASSERT(bp != NULL);
11981 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11982 
11983 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
11984 
11985 	xp = SD_GET_XBUF(bp);
11986 	ASSERT(xp != NULL);
11987 
11988 	mutex_enter(SD_MUTEX(un));
11989 
11990 	/*
11991 	 * Grab time when the cmd completed.
11992 	 * This is used for determining if the system has been
11993 	 * idle long enough to make it idle to the PM framework.
11994 	 * This is for lowering the overhead, and therefore improving
11995 	 * performance per I/O operation.
11996 	 */
11997 	un->un_pm_idle_time = ddi_get_time();
11998 
11999 	un->un_ncmds_in_driver--;
12000 	ASSERT(un->un_ncmds_in_driver >= 0);
12001 	SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
12002 	    un->un_ncmds_in_driver);
12003 
12004 	mutex_exit(SD_MUTEX(un));
12005 
12006 	ddi_xbuf_done(bp, un->un_xbuf_attr);	/* xbuf is gone after this */
12007 	biodone(bp);				/* bp is gone after this */
12008 
12009 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
12010 }
12011 
12012 
12013 /*
12014  *    Function: sd_uscsi_iodone
12015  *
12016  * Description: Frees the sd_xbuf & returns the buf to its originator.
12017  *
12018  *     Context: May be called from interrupt context.
12019  */
12020 /* ARGSUSED */
12021 static void
12022 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12023 {
12024 	struct sd_xbuf *xp;
12025 
12026 	ASSERT(un != NULL);
12027 	ASSERT(bp != NULL);
12028 
12029 	xp = SD_GET_XBUF(bp);
12030 	ASSERT(xp != NULL);
12031 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12032 
12033 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
12034 
12035 	bp->b_private = xp->xb_private;
12036 
12037 	mutex_enter(SD_MUTEX(un));
12038 
12039 	/*
12040 	 * Grab time when the cmd completed.
12041 	 * This is used for determining if the system has been
12042 	 * idle long enough to make it idle to the PM framework.
12043 	 * This is for lowering the overhead, and therefore improving
12044 	 * performance per I/O operation.
12045 	 */
12046 	un->un_pm_idle_time = ddi_get_time();
12047 
12048 	un->un_ncmds_in_driver--;
12049 	ASSERT(un->un_ncmds_in_driver >= 0);
12050 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
12051 	    un->un_ncmds_in_driver);
12052 
12053 	mutex_exit(SD_MUTEX(un));
12054 
12055 	kmem_free(xp, sizeof (struct sd_xbuf));
12056 	biodone(bp);
12057 
12058 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
12059 }
12060 
12061 
12062 /*
12063  *    Function: sd_mapblockaddr_iostart
12064  *
12065  * Description: Verify request lies withing the partition limits for
12066  *		the indicated minor device.  Issue "overrun" buf if
12067  *		request would exceed partition range.  Converts
12068  *		partition-relative block address to absolute.
12069  *
12070  *     Context: Can sleep
12071  *
12072  *      Issues: This follows what the old code did, in terms of accessing
12073  *		some of the partition info in the unit struct without holding
12074  *		the mutext.  This is a general issue, if the partition info
12075  *		can be altered while IO is in progress... as soon as we send
12076  *		a buf, its partitioning can be invalid before it gets to the
12077  *		device.  Probably the right fix is to move partitioning out
12078  *		of the driver entirely.
12079  */
12080 
12081 static void
12082 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
12083 {
12084 	daddr_t	nblocks;	/* #blocks in the given partition */
12085 	daddr_t	blocknum;	/* Block number specified by the buf */
12086 	size_t	requested_nblocks;
12087 	size_t	available_nblocks;
12088 	int	partition;
12089 	diskaddr_t	partition_offset;
12090 	struct sd_xbuf *xp;
12091 
12092 
12093 	ASSERT(un != NULL);
12094 	ASSERT(bp != NULL);
12095 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12096 
12097 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12098 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
12099 
12100 	xp = SD_GET_XBUF(bp);
12101 	ASSERT(xp != NULL);
12102 
12103 	/*
12104 	 * If the geometry is not indicated as valid, attempt to access
12105 	 * the unit & verify the geometry/label. This can be the case for
12106 	 * removable-media devices, of if the device was opened in
12107 	 * NDELAY/NONBLOCK mode.
12108 	 */
12109 	if ((un->un_f_geometry_is_valid != TRUE) &&
12110 	    (sd_ready_and_valid(un) != SD_READY_VALID)) {
12111 		/*
12112 		 * For removable devices it is possible to start an I/O
12113 		 * without a media by opening the device in nodelay mode.
12114 		 * Also for writable CDs there can be many scenarios where
12115 		 * there is no geometry yet but volume manager is trying to
12116 		 * issue a read() just because it can see TOC on the CD. So
12117 		 * do not print a message for removables.
12118 		 */
12119 		if (!un->un_f_has_removable_media) {
12120 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12121 			    "i/o to invalid geometry\n");
12122 		}
12123 		bioerror(bp, EIO);
12124 		bp->b_resid = bp->b_bcount;
12125 		SD_BEGIN_IODONE(index, un, bp);
12126 		return;
12127 	}
12128 
12129 	partition = SDPART(bp->b_edev);
12130 
12131 	/* #blocks in partition */
12132 	nblocks = un->un_map[partition].dkl_nblk;    /* #blocks in partition */
12133 
12134 	/* Use of a local variable potentially improves performance slightly */
12135 	partition_offset = un->un_offset[partition];
12136 
12137 	/*
12138 	 * blocknum is the starting block number of the request. At this
12139 	 * point it is still relative to the start of the minor device.
12140 	 */
12141 	blocknum = xp->xb_blkno;
12142 
12143 	/*
12144 	 * Legacy: If the starting block number is one past the last block
12145 	 * in the partition, do not set B_ERROR in the buf.
12146 	 */
12147 	if (blocknum == nblocks)  {
12148 		goto error_exit;
12149 	}
12150 
12151 	/*
12152 	 * Confirm that the first block of the request lies within the
12153 	 * partition limits. Also the requested number of bytes must be
12154 	 * a multiple of the system block size.
12155 	 */
12156 	if ((blocknum < 0) || (blocknum >= nblocks) ||
12157 	    ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) {
12158 		bp->b_flags |= B_ERROR;
12159 		goto error_exit;
12160 	}
12161 
12162 	/*
12163 	 * If the requsted # blocks exceeds the available # blocks, that
12164 	 * is an overrun of the partition.
12165 	 */
12166 	requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount);
12167 	available_nblocks = (size_t)(nblocks - blocknum);
12168 	ASSERT(nblocks >= blocknum);
12169 
12170 	if (requested_nblocks > available_nblocks) {
12171 		/*
12172 		 * Allocate an "overrun" buf to allow the request to proceed
12173 		 * for the amount of space available in the partition. The
12174 		 * amount not transferred will be added into the b_resid
12175 		 * when the operation is complete. The overrun buf
12176 		 * replaces the original buf here, and the original buf
12177 		 * is saved inside the overrun buf, for later use.
12178 		 */
12179 		size_t resid = SD_SYSBLOCKS2BYTES(un,
12180 		    (offset_t)(requested_nblocks - available_nblocks));
12181 		size_t count = bp->b_bcount - resid;
12182 		/*
12183 		 * Note: count is an unsigned entity thus it'll NEVER
12184 		 * be less than 0 so ASSERT the original values are
12185 		 * correct.
12186 		 */
12187 		ASSERT(bp->b_bcount >= resid);
12188 
12189 		bp = sd_bioclone_alloc(bp, count, blocknum,
12190 			(int (*)(struct buf *)) sd_mapblockaddr_iodone);
12191 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
12192 		ASSERT(xp != NULL);
12193 	}
12194 
12195 	/* At this point there should be no residual for this buf. */
12196 	ASSERT(bp->b_resid == 0);
12197 
12198 	/* Convert the block number to an absolute address. */
12199 	xp->xb_blkno += partition_offset;
12200 
12201 	SD_NEXT_IOSTART(index, un, bp);
12202 
12203 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12204 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
12205 
12206 	return;
12207 
12208 error_exit:
12209 	bp->b_resid = bp->b_bcount;
12210 	SD_BEGIN_IODONE(index, un, bp);
12211 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12212 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
12213 }
12214 
12215 
12216 /*
12217  *    Function: sd_mapblockaddr_iodone
12218  *
12219  * Description: Completion-side processing for partition management.
12220  *
12221  *     Context: May be called under interrupt context
12222  */
12223 
12224 static void
12225 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
12226 {
12227 	/* int	partition; */	/* Not used, see below. */
12228 	ASSERT(un != NULL);
12229 	ASSERT(bp != NULL);
12230 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12231 
12232 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12233 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
12234 
12235 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
12236 		/*
12237 		 * We have an "overrun" buf to deal with...
12238 		 */
12239 		struct sd_xbuf	*xp;
12240 		struct buf	*obp;	/* ptr to the original buf */
12241 
12242 		xp = SD_GET_XBUF(bp);
12243 		ASSERT(xp != NULL);
12244 
12245 		/* Retrieve the pointer to the original buf */
12246 		obp = (struct buf *)xp->xb_private;
12247 		ASSERT(obp != NULL);
12248 
12249 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
12250 		bioerror(obp, bp->b_error);
12251 
12252 		sd_bioclone_free(bp);
12253 
12254 		/*
12255 		 * Get back the original buf.
12256 		 * Note that since the restoration of xb_blkno below
12257 		 * was removed, the sd_xbuf is not needed.
12258 		 */
12259 		bp = obp;
12260 		/*
12261 		 * xp = SD_GET_XBUF(bp);
12262 		 * ASSERT(xp != NULL);
12263 		 */
12264 	}
12265 
12266 	/*
12267 	 * Convert sd->xb_blkno back to a minor-device relative value.
12268 	 * Note: this has been commented out, as it is not needed in the
12269 	 * current implementation of the driver (ie, since this function
12270 	 * is at the top of the layering chains, so the info will be
12271 	 * discarded) and it is in the "hot" IO path.
12272 	 *
12273 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
12274 	 * xp->xb_blkno -= un->un_offset[partition];
12275 	 */
12276 
12277 	SD_NEXT_IODONE(index, un, bp);
12278 
12279 	SD_TRACE(SD_LOG_IO_PARTITION, un,
12280 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
12281 }
12282 
12283 
12284 /*
12285  *    Function: sd_mapblocksize_iostart
12286  *
12287  * Description: Convert between system block size (un->un_sys_blocksize)
12288  *		and target block size (un->un_tgt_blocksize).
12289  *
12290  *     Context: Can sleep to allocate resources.
12291  *
12292  * Assumptions: A higher layer has already performed any partition validation,
12293  *		and converted the xp->xb_blkno to an absolute value relative
12294  *		to the start of the device.
12295  *
12296  *		It is also assumed that the higher layer has implemented
12297  *		an "overrun" mechanism for the case where the request would
12298  *		read/write beyond the end of a partition.  In this case we
12299  *		assume (and ASSERT) that bp->b_resid == 0.
12300  *
12301  *		Note: The implementation for this routine assumes the target
12302  *		block size remains constant between allocation and transport.
12303  */
12304 
12305 static void
12306 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12307 {
12308 	struct sd_mapblocksize_info	*bsp;
12309 	struct sd_xbuf			*xp;
12310 	offset_t first_byte;
12311 	daddr_t	start_block, end_block;
12312 	daddr_t	request_bytes;
12313 	ushort_t is_aligned = FALSE;
12314 
12315 	ASSERT(un != NULL);
12316 	ASSERT(bp != NULL);
12317 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12318 	ASSERT(bp->b_resid == 0);
12319 
12320 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12321 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12322 
12323 	/*
12324 	 * For a non-writable CD, a write request is an error
12325 	 */
12326 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12327 	    (un->un_f_mmc_writable_media == FALSE)) {
12328 		bioerror(bp, EIO);
12329 		bp->b_resid = bp->b_bcount;
12330 		SD_BEGIN_IODONE(index, un, bp);
12331 		return;
12332 	}
12333 
12334 	/*
12335 	 * We do not need a shadow buf if the device is using
12336 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12337 	 * In this case there is no layer-private data block allocated.
12338 	 */
12339 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12340 	    (bp->b_bcount == 0)) {
12341 		goto done;
12342 	}
12343 
12344 #if defined(__i386) || defined(__amd64)
12345 	/* We do not support non-block-aligned transfers for ROD devices */
12346 	ASSERT(!ISROD(un));
12347 #endif
12348 
12349 	xp = SD_GET_XBUF(bp);
12350 	ASSERT(xp != NULL);
12351 
12352 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12353 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12354 	    un->un_tgt_blocksize, un->un_sys_blocksize);
12355 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12356 	    "request start block:0x%x\n", xp->xb_blkno);
12357 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12358 	    "request len:0x%x\n", bp->b_bcount);
12359 
12360 	/*
12361 	 * Allocate the layer-private data area for the mapblocksize layer.
12362 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12363 	 * struct to store the pointer to their layer-private data block, but
12364 	 * each layer also has the responsibility of restoring the prior
12365 	 * contents of xb_private before returning the buf/xbuf to the
12366 	 * higher layer that sent it.
12367 	 *
12368 	 * Here we save the prior contents of xp->xb_private into the
12369 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12370 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12371 	 * the layer-private area and returning the buf/xbuf to the layer
12372 	 * that sent it.
12373 	 *
12374 	 * Note that here we use kmem_zalloc for the allocation as there are
12375 	 * parts of the mapblocksize code that expect certain fields to be
12376 	 * zero unless explicitly set to a required value.
12377 	 */
12378 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12379 	bsp->mbs_oprivate = xp->xb_private;
12380 	xp->xb_private = bsp;
12381 
12382 	/*
12383 	 * This treats the data on the disk (target) as an array of bytes.
12384 	 * first_byte is the byte offset, from the beginning of the device,
12385 	 * to the location of the request. This is converted from a
12386 	 * un->un_sys_blocksize block address to a byte offset, and then back
12387 	 * to a block address based upon a un->un_tgt_blocksize block size.
12388 	 *
12389 	 * xp->xb_blkno should be absolute upon entry into this function,
12390 	 * but, but it is based upon partitions that use the "system"
12391 	 * block size. It must be adjusted to reflect the block size of
12392 	 * the target.
12393 	 *
12394 	 * Note that end_block is actually the block that follows the last
12395 	 * block of the request, but that's what is needed for the computation.
12396 	 */
12397 	first_byte  = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12398 	start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12399 	end_block   = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) /
12400 	    un->un_tgt_blocksize;
12401 
12402 	/* request_bytes is rounded up to a multiple of the target block size */
12403 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12404 
12405 	/*
12406 	 * See if the starting address of the request and the request
12407 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12408 	 * then we do not need to allocate a shadow buf to handle the request.
12409 	 */
12410 	if (((first_byte   % un->un_tgt_blocksize) == 0) &&
12411 	    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12412 		is_aligned = TRUE;
12413 	}
12414 
12415 	if ((bp->b_flags & B_READ) == 0) {
12416 		/*
12417 		 * Lock the range for a write operation. An aligned request is
12418 		 * considered a simple write; otherwise the request must be a
12419 		 * read-modify-write.
12420 		 */
12421 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12422 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12423 	}
12424 
12425 	/*
12426 	 * Alloc a shadow buf if the request is not aligned. Also, this is
12427 	 * where the READ command is generated for a read-modify-write. (The
12428 	 * write phase is deferred until after the read completes.)
12429 	 */
12430 	if (is_aligned == FALSE) {
12431 
12432 		struct sd_mapblocksize_info	*shadow_bsp;
12433 		struct sd_xbuf	*shadow_xp;
12434 		struct buf	*shadow_bp;
12435 
12436 		/*
12437 		 * Allocate the shadow buf and it associated xbuf. Note that
12438 		 * after this call the xb_blkno value in both the original
12439 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12440 		 * same: absolute relative to the start of the device, and
12441 		 * adjusted for the target block size. The b_blkno in the
12442 		 * shadow buf will also be set to this value. We should never
12443 		 * change b_blkno in the original bp however.
12444 		 *
12445 		 * Note also that the shadow buf will always need to be a
12446 		 * READ command, regardless of whether the incoming command
12447 		 * is a READ or a WRITE.
12448 		 */
12449 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12450 		    xp->xb_blkno,
12451 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
12452 
12453 		shadow_xp = SD_GET_XBUF(shadow_bp);
12454 
12455 		/*
12456 		 * Allocate the layer-private data for the shadow buf.
12457 		 * (No need to preserve xb_private in the shadow xbuf.)
12458 		 */
12459 		shadow_xp->xb_private = shadow_bsp =
12460 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12461 
12462 		/*
12463 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
12464 		 * to figure out where the start of the user data is (based upon
12465 		 * the system block size) in the data returned by the READ
12466 		 * command (which will be based upon the target blocksize). Note
12467 		 * that this is only really used if the request is unaligned.
12468 		 */
12469 		bsp->mbs_copy_offset = (ssize_t)(first_byte -
12470 		    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
12471 		ASSERT((bsp->mbs_copy_offset >= 0) &&
12472 		    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
12473 
12474 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
12475 
12476 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
12477 
12478 		/* Transfer the wmap (if any) to the shadow buf */
12479 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
12480 		bsp->mbs_wmp = NULL;
12481 
12482 		/*
12483 		 * The shadow buf goes on from here in place of the
12484 		 * original buf.
12485 		 */
12486 		shadow_bsp->mbs_orig_bp = bp;
12487 		bp = shadow_bp;
12488 	}
12489 
12490 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12491 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
12492 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12493 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
12494 	    request_bytes);
12495 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12496 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
12497 
12498 done:
12499 	SD_NEXT_IOSTART(index, un, bp);
12500 
12501 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12502 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
12503 }
12504 
12505 
12506 /*
12507  *    Function: sd_mapblocksize_iodone
12508  *
12509  * Description: Completion side processing for block-size mapping.
12510  *
12511  *     Context: May be called under interrupt context
12512  */
12513 
12514 static void
12515 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
12516 {
12517 	struct sd_mapblocksize_info	*bsp;
12518 	struct sd_xbuf	*xp;
12519 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
12520 	struct buf	*orig_bp;	/* ptr to the original buf */
12521 	offset_t	shadow_end;
12522 	offset_t	request_end;
12523 	offset_t	shadow_start;
12524 	ssize_t		copy_offset;
12525 	size_t		copy_length;
12526 	size_t		shortfall;
12527 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
12528 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
12529 
12530 	ASSERT(un != NULL);
12531 	ASSERT(bp != NULL);
12532 
12533 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12534 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
12535 
12536 	/*
12537 	 * There is no shadow buf or layer-private data if the target is
12538 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
12539 	 */
12540 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12541 	    (bp->b_bcount == 0)) {
12542 		goto exit;
12543 	}
12544 
12545 	xp = SD_GET_XBUF(bp);
12546 	ASSERT(xp != NULL);
12547 
12548 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
12549 	bsp = xp->xb_private;
12550 
12551 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
12552 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
12553 
12554 	if (is_write) {
12555 		/*
12556 		 * For a WRITE request we must free up the block range that
12557 		 * we have locked up.  This holds regardless of whether this is
12558 		 * an aligned write request or a read-modify-write request.
12559 		 */
12560 		sd_range_unlock(un, bsp->mbs_wmp);
12561 		bsp->mbs_wmp = NULL;
12562 	}
12563 
12564 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
12565 		/*
12566 		 * An aligned read or write command will have no shadow buf;
12567 		 * there is not much else to do with it.
12568 		 */
12569 		goto done;
12570 	}
12571 
12572 	orig_bp = bsp->mbs_orig_bp;
12573 	ASSERT(orig_bp != NULL);
12574 	orig_xp = SD_GET_XBUF(orig_bp);
12575 	ASSERT(orig_xp != NULL);
12576 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12577 
12578 	if (!is_write && has_wmap) {
12579 		/*
12580 		 * A READ with a wmap means this is the READ phase of a
12581 		 * read-modify-write. If an error occurred on the READ then
12582 		 * we do not proceed with the WRITE phase or copy any data.
12583 		 * Just release the write maps and return with an error.
12584 		 */
12585 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
12586 			orig_bp->b_resid = orig_bp->b_bcount;
12587 			bioerror(orig_bp, bp->b_error);
12588 			sd_range_unlock(un, bsp->mbs_wmp);
12589 			goto freebuf_done;
12590 		}
12591 	}
12592 
12593 	/*
12594 	 * Here is where we set up to copy the data from the shadow buf
12595 	 * into the space associated with the original buf.
12596 	 *
12597 	 * To deal with the conversion between block sizes, these
12598 	 * computations treat the data as an array of bytes, with the
12599 	 * first byte (byte 0) corresponding to the first byte in the
12600 	 * first block on the disk.
12601 	 */
12602 
12603 	/*
12604 	 * shadow_start and shadow_len indicate the location and size of
12605 	 * the data returned with the shadow IO request.
12606 	 */
12607 	shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12608 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
12609 
12610 	/*
12611 	 * copy_offset gives the offset (in bytes) from the start of the first
12612 	 * block of the READ request to the beginning of the data.  We retrieve
12613 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
12614 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
12615 	 * data to be copied (in bytes).
12616 	 */
12617 	copy_offset  = bsp->mbs_copy_offset;
12618 	ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize));
12619 	copy_length  = orig_bp->b_bcount;
12620 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
12621 
12622 	/*
12623 	 * Set up the resid and error fields of orig_bp as appropriate.
12624 	 */
12625 	if (shadow_end >= request_end) {
12626 		/* We got all the requested data; set resid to zero */
12627 		orig_bp->b_resid = 0;
12628 	} else {
12629 		/*
12630 		 * We failed to get enough data to fully satisfy the original
12631 		 * request. Just copy back whatever data we got and set
12632 		 * up the residual and error code as required.
12633 		 *
12634 		 * 'shortfall' is the amount by which the data received with the
12635 		 * shadow buf has "fallen short" of the requested amount.
12636 		 */
12637 		shortfall = (size_t)(request_end - shadow_end);
12638 
12639 		if (shortfall > orig_bp->b_bcount) {
12640 			/*
12641 			 * We did not get enough data to even partially
12642 			 * fulfill the original request.  The residual is
12643 			 * equal to the amount requested.
12644 			 */
12645 			orig_bp->b_resid = orig_bp->b_bcount;
12646 		} else {
12647 			/*
12648 			 * We did not get all the data that we requested
12649 			 * from the device, but we will try to return what
12650 			 * portion we did get.
12651 			 */
12652 			orig_bp->b_resid = shortfall;
12653 		}
12654 		ASSERT(copy_length >= orig_bp->b_resid);
12655 		copy_length  -= orig_bp->b_resid;
12656 	}
12657 
12658 	/* Propagate the error code from the shadow buf to the original buf */
12659 	bioerror(orig_bp, bp->b_error);
12660 
12661 	if (is_write) {
12662 		goto freebuf_done;	/* No data copying for a WRITE */
12663 	}
12664 
12665 	if (has_wmap) {
12666 		/*
12667 		 * This is a READ command from the READ phase of a
12668 		 * read-modify-write request. We have to copy the data given
12669 		 * by the user OVER the data returned by the READ command,
12670 		 * then convert the command from a READ to a WRITE and send
12671 		 * it back to the target.
12672 		 */
12673 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
12674 		    copy_length);
12675 
12676 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
12677 
12678 		/*
12679 		 * Dispatch the WRITE command to the taskq thread, which
12680 		 * will in turn send the command to the target. When the
12681 		 * WRITE command completes, we (sd_mapblocksize_iodone())
12682 		 * will get called again as part of the iodone chain
12683 		 * processing for it. Note that we will still be dealing
12684 		 * with the shadow buf at that point.
12685 		 */
12686 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
12687 		    KM_NOSLEEP) != 0) {
12688 			/*
12689 			 * Dispatch was successful so we are done. Return
12690 			 * without going any higher up the iodone chain. Do
12691 			 * not free up any layer-private data until after the
12692 			 * WRITE completes.
12693 			 */
12694 			return;
12695 		}
12696 
12697 		/*
12698 		 * Dispatch of the WRITE command failed; set up the error
12699 		 * condition and send this IO back up the iodone chain.
12700 		 */
12701 		bioerror(orig_bp, EIO);
12702 		orig_bp->b_resid = orig_bp->b_bcount;
12703 
12704 	} else {
12705 		/*
12706 		 * This is a regular READ request (ie, not a RMW). Copy the
12707 		 * data from the shadow buf into the original buf. The
12708 		 * copy_offset compensates for any "misalignment" between the
12709 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
12710 		 * original buf (with its un->un_sys_blocksize blocks).
12711 		 */
12712 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
12713 		    copy_length);
12714 	}
12715 
12716 freebuf_done:
12717 
12718 	/*
12719 	 * At this point we still have both the shadow buf AND the original
12720 	 * buf to deal with, as well as the layer-private data area in each.
12721 	 * Local variables are as follows:
12722 	 *
12723 	 * bp -- points to shadow buf
12724 	 * xp -- points to xbuf of shadow buf
12725 	 * bsp -- points to layer-private data area of shadow buf
12726 	 * orig_bp -- points to original buf
12727 	 *
12728 	 * First free the shadow buf and its associated xbuf, then free the
12729 	 * layer-private data area from the shadow buf. There is no need to
12730 	 * restore xb_private in the shadow xbuf.
12731 	 */
12732 	sd_shadow_buf_free(bp);
12733 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
12734 
12735 	/*
12736 	 * Now update the local variables to point to the original buf, xbuf,
12737 	 * and layer-private area.
12738 	 */
12739 	bp = orig_bp;
12740 	xp = SD_GET_XBUF(bp);
12741 	ASSERT(xp != NULL);
12742 	ASSERT(xp == orig_xp);
12743 	bsp = xp->xb_private;
12744 	ASSERT(bsp != NULL);
12745 
12746 done:
12747 	/*
12748 	 * Restore xb_private to whatever it was set to by the next higher
12749 	 * layer in the chain, then free the layer-private data area.
12750 	 */
12751 	xp->xb_private = bsp->mbs_oprivate;
12752 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
12753 
12754 exit:
12755 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
12756 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
12757 
12758 	SD_NEXT_IODONE(index, un, bp);
12759 }
12760 
12761 
12762 /*
12763  *    Function: sd_checksum_iostart
12764  *
12765  * Description: A stub function for a layer that's currently not used.
12766  *		For now just a placeholder.
12767  *
12768  *     Context: Kernel thread context
12769  */
12770 
12771 static void
12772 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
12773 {
12774 	ASSERT(un != NULL);
12775 	ASSERT(bp != NULL);
12776 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12777 	SD_NEXT_IOSTART(index, un, bp);
12778 }
12779 
12780 
12781 /*
12782  *    Function: sd_checksum_iodone
12783  *
12784  * Description: A stub function for a layer that's currently not used.
12785  *		For now just a placeholder.
12786  *
12787  *     Context: May be called under interrupt context
12788  */
12789 
12790 static void
12791 sd_checksum_iodone(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_IODONE(index, un, bp);
12797 }
12798 
12799 
12800 /*
12801  *    Function: sd_checksum_uscsi_iostart
12802  *
12803  * Description: A stub function for a layer that's currently not used.
12804  *		For now just a placeholder.
12805  *
12806  *     Context: Kernel thread context
12807  */
12808 
12809 static void
12810 sd_checksum_uscsi_iostart(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_IOSTART(index, un, bp);
12816 }
12817 
12818 
12819 /*
12820  *    Function: sd_checksum_uscsi_iodone
12821  *
12822  * Description: A stub function for a layer that's currently not used.
12823  *		For now just a placeholder.
12824  *
12825  *     Context: May be called under interrupt context
12826  */
12827 
12828 static void
12829 sd_checksum_uscsi_iodone(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_IODONE(index, un, bp);
12835 }
12836 
12837 
12838 /*
12839  *    Function: sd_pm_iostart
12840  *
12841  * Description: iostart-side routine for Power mangement.
12842  *
12843  *     Context: Kernel thread context
12844  */
12845 
12846 static void
12847 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
12848 {
12849 	ASSERT(un != NULL);
12850 	ASSERT(bp != NULL);
12851 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12852 	ASSERT(!mutex_owned(&un->un_pm_mutex));
12853 
12854 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
12855 
12856 	if (sd_pm_entry(un) != DDI_SUCCESS) {
12857 		/*
12858 		 * Set up to return the failed buf back up the 'iodone'
12859 		 * side of the calling chain.
12860 		 */
12861 		bioerror(bp, EIO);
12862 		bp->b_resid = bp->b_bcount;
12863 
12864 		SD_BEGIN_IODONE(index, un, bp);
12865 
12866 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
12867 		return;
12868 	}
12869 
12870 	SD_NEXT_IOSTART(index, un, bp);
12871 
12872 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
12873 }
12874 
12875 
12876 /*
12877  *    Function: sd_pm_iodone
12878  *
12879  * Description: iodone-side routine for power mangement.
12880  *
12881  *     Context: may be called from interrupt context
12882  */
12883 
12884 static void
12885 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
12886 {
12887 	ASSERT(un != NULL);
12888 	ASSERT(bp != NULL);
12889 	ASSERT(!mutex_owned(&un->un_pm_mutex));
12890 
12891 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
12892 
12893 	/*
12894 	 * After attach the following flag is only read, so don't
12895 	 * take the penalty of acquiring a mutex for it.
12896 	 */
12897 	if (un->un_f_pm_is_enabled == TRUE) {
12898 		sd_pm_exit(un);
12899 	}
12900 
12901 	SD_NEXT_IODONE(index, un, bp);
12902 
12903 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
12904 }
12905 
12906 
12907 /*
12908  *    Function: sd_core_iostart
12909  *
12910  * Description: Primary driver function for enqueuing buf(9S) structs from
12911  *		the system and initiating IO to the target device
12912  *
12913  *     Context: Kernel thread context. Can sleep.
12914  *
12915  * Assumptions:  - The given xp->xb_blkno is absolute
12916  *		   (ie, relative to the start of the device).
12917  *		 - The IO is to be done using the native blocksize of
12918  *		   the device, as specified in un->un_tgt_blocksize.
12919  */
12920 /* ARGSUSED */
12921 static void
12922 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
12923 {
12924 	struct sd_xbuf *xp;
12925 
12926 	ASSERT(un != NULL);
12927 	ASSERT(bp != NULL);
12928 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12929 	ASSERT(bp->b_resid == 0);
12930 
12931 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
12932 
12933 	xp = SD_GET_XBUF(bp);
12934 	ASSERT(xp != NULL);
12935 
12936 	mutex_enter(SD_MUTEX(un));
12937 
12938 	/*
12939 	 * If we are currently in the failfast state, fail any new IO
12940 	 * that has B_FAILFAST set, then return.
12941 	 */
12942 	if ((bp->b_flags & B_FAILFAST) &&
12943 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
12944 		mutex_exit(SD_MUTEX(un));
12945 		bioerror(bp, EIO);
12946 		bp->b_resid = bp->b_bcount;
12947 		SD_BEGIN_IODONE(index, un, bp);
12948 		return;
12949 	}
12950 
12951 	if (SD_IS_DIRECT_PRIORITY(xp)) {
12952 		/*
12953 		 * Priority command -- transport it immediately.
12954 		 *
12955 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
12956 		 * because all direct priority commands should be associated
12957 		 * with error recovery actions which we don't want to retry.
12958 		 */
12959 		sd_start_cmds(un, bp);
12960 	} else {
12961 		/*
12962 		 * Normal command -- add it to the wait queue, then start
12963 		 * transporting commands from the wait queue.
12964 		 */
12965 		sd_add_buf_to_waitq(un, bp);
12966 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
12967 		sd_start_cmds(un, NULL);
12968 	}
12969 
12970 	mutex_exit(SD_MUTEX(un));
12971 
12972 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
12973 }
12974 
12975 
12976 /*
12977  *    Function: sd_init_cdb_limits
12978  *
12979  * Description: This is to handle scsi_pkt initialization differences
12980  *		between the driver platforms.
12981  *
12982  *		Legacy behaviors:
12983  *
12984  *		If the block number or the sector count exceeds the
12985  *		capabilities of a Group 0 command, shift over to a
12986  *		Group 1 command. We don't blindly use Group 1
12987  *		commands because a) some drives (CDC Wren IVs) get a
12988  *		bit confused, and b) there is probably a fair amount
12989  *		of speed difference for a target to receive and decode
12990  *		a 10 byte command instead of a 6 byte command.
12991  *
12992  *		The xfer time difference of 6 vs 10 byte CDBs is
12993  *		still significant so this code is still worthwhile.
12994  *		10 byte CDBs are very inefficient with the fas HBA driver
12995  *		and older disks. Each CDB byte took 1 usec with some
12996  *		popular disks.
12997  *
12998  *     Context: Must be called at attach time
12999  */
13000 
13001 static void
13002 sd_init_cdb_limits(struct sd_lun *un)
13003 {
13004 	/*
13005 	 * Use CDB_GROUP1 commands for most devices except for
13006 	 * parallel SCSI fixed drives in which case we get better
13007 	 * performance using CDB_GROUP0 commands (where applicable).
13008 	 */
13009 	un->un_mincdb = SD_CDB_GROUP1;
13010 #if !defined(__fibre)
13011 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
13012 	    !un->un_f_has_removable_media) {
13013 		un->un_mincdb = SD_CDB_GROUP0;
13014 	}
13015 #endif
13016 
13017 	/*
13018 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
13019 	 * commands for fixed disks unless we are building for a 32 bit
13020 	 * kernel.
13021 	 */
13022 #ifdef _LP64
13023 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13024 	    SD_CDB_GROUP4;
13025 #else
13026 	un->un_maxcdb = (un->un_f_has_removable_media) ? SD_CDB_GROUP5 :
13027 	    SD_CDB_GROUP1;
13028 #endif
13029 
13030 	/*
13031 	 * x86 systems require the PKT_DMA_PARTIAL flag
13032 	 */
13033 #if defined(__x86)
13034 	un->un_pkt_flags = PKT_DMA_PARTIAL;
13035 #else
13036 	un->un_pkt_flags = 0;
13037 #endif
13038 
13039 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
13040 	    ? sizeof (struct scsi_arq_status) : 1);
13041 	un->un_cmd_timeout = (ushort_t)sd_io_time;
13042 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
13043 }
13044 
13045 
13046 /*
13047  *    Function: sd_initpkt_for_buf
13048  *
13049  * Description: Allocate and initialize for transport a scsi_pkt struct,
13050  *		based upon the info specified in the given buf struct.
13051  *
13052  *		Assumes the xb_blkno in the request is absolute (ie,
13053  *		relative to the start of the device (NOT partition!).
13054  *		Also assumes that the request is using the native block
13055  *		size of the device (as returned by the READ CAPACITY
13056  *		command).
13057  *
13058  * Return Code: SD_PKT_ALLOC_SUCCESS
13059  *		SD_PKT_ALLOC_FAILURE
13060  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13061  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13062  *
13063  *     Context: Kernel thread and may be called from software interrupt context
13064  *		as part of a sdrunout callback. This function may not block or
13065  *		call routines that block
13066  */
13067 
13068 static int
13069 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
13070 {
13071 	struct sd_xbuf	*xp;
13072 	struct scsi_pkt *pktp = NULL;
13073 	struct sd_lun	*un;
13074 	size_t		blockcount;
13075 	daddr_t		startblock;
13076 	int		rval;
13077 	int		cmd_flags;
13078 
13079 	ASSERT(bp != NULL);
13080 	ASSERT(pktpp != NULL);
13081 	xp = SD_GET_XBUF(bp);
13082 	ASSERT(xp != NULL);
13083 	un = SD_GET_UN(bp);
13084 	ASSERT(un != NULL);
13085 	ASSERT(mutex_owned(SD_MUTEX(un)));
13086 	ASSERT(bp->b_resid == 0);
13087 
13088 	SD_TRACE(SD_LOG_IO_CORE, un,
13089 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
13090 
13091 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13092 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
13093 		/*
13094 		 * Already have a scsi_pkt -- just need DMA resources.
13095 		 * We must recompute the CDB in case the mapping returns
13096 		 * a nonzero pkt_resid.
13097 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
13098 		 * that is being retried, the unmap/remap of the DMA resouces
13099 		 * will result in the entire transfer starting over again
13100 		 * from the very first block.
13101 		 */
13102 		ASSERT(xp->xb_pktp != NULL);
13103 		pktp = xp->xb_pktp;
13104 	} else {
13105 		pktp = NULL;
13106 	}
13107 #endif /* __i386 || __amd64 */
13108 
13109 	startblock = xp->xb_blkno;	/* Absolute block num. */
13110 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
13111 
13112 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13113 
13114 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
13115 
13116 #else
13117 
13118 	cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags;
13119 
13120 #endif
13121 
13122 	/*
13123 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
13124 	 * call scsi_init_pkt, and build the CDB.
13125 	 */
13126 	rval = sd_setup_rw_pkt(un, &pktp, bp,
13127 	    cmd_flags, sdrunout, (caddr_t)un,
13128 	    startblock, blockcount);
13129 
13130 	if (rval == 0) {
13131 		/*
13132 		 * Success.
13133 		 *
13134 		 * If partial DMA is being used and required for this transfer.
13135 		 * set it up here.
13136 		 */
13137 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
13138 		    (pktp->pkt_resid != 0)) {
13139 
13140 			/*
13141 			 * Save the CDB length and pkt_resid for the
13142 			 * next xfer
13143 			 */
13144 			xp->xb_dma_resid = pktp->pkt_resid;
13145 
13146 			/* rezero resid */
13147 			pktp->pkt_resid = 0;
13148 
13149 		} else {
13150 			xp->xb_dma_resid = 0;
13151 		}
13152 
13153 		pktp->pkt_flags = un->un_tagflags;
13154 		pktp->pkt_time  = un->un_cmd_timeout;
13155 		pktp->pkt_comp  = sdintr;
13156 
13157 		pktp->pkt_private = bp;
13158 		*pktpp = pktp;
13159 
13160 		SD_TRACE(SD_LOG_IO_CORE, un,
13161 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
13162 
13163 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13164 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
13165 #endif
13166 
13167 		return (SD_PKT_ALLOC_SUCCESS);
13168 
13169 	}
13170 
13171 	/*
13172 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
13173 	 * from sd_setup_rw_pkt.
13174 	 */
13175 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
13176 
13177 	if (rval == SD_PKT_ALLOC_FAILURE) {
13178 		*pktpp = NULL;
13179 		/*
13180 		 * Set the driver state to RWAIT to indicate the driver
13181 		 * is waiting on resource allocations. The driver will not
13182 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13183 		 */
13184 		New_state(un, SD_STATE_RWAIT);
13185 
13186 		SD_ERROR(SD_LOG_IO_CORE, un,
13187 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
13188 
13189 		if ((bp->b_flags & B_ERROR) != 0) {
13190 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13191 		}
13192 		return (SD_PKT_ALLOC_FAILURE);
13193 	} else {
13194 		/*
13195 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13196 		 *
13197 		 * This should never happen.  Maybe someone messed with the
13198 		 * kernel's minphys?
13199 		 */
13200 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13201 		    "Request rejected: too large for CDB: "
13202 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
13203 		SD_ERROR(SD_LOG_IO_CORE, un,
13204 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
13205 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13206 
13207 	}
13208 }
13209 
13210 
13211 /*
13212  *    Function: sd_destroypkt_for_buf
13213  *
13214  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
13215  *
13216  *     Context: Kernel thread or interrupt context
13217  */
13218 
13219 static void
13220 sd_destroypkt_for_buf(struct buf *bp)
13221 {
13222 	ASSERT(bp != NULL);
13223 	ASSERT(SD_GET_UN(bp) != NULL);
13224 
13225 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13226 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
13227 
13228 	ASSERT(SD_GET_PKTP(bp) != NULL);
13229 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13230 
13231 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
13232 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
13233 }
13234 
13235 /*
13236  *    Function: sd_setup_rw_pkt
13237  *
13238  * Description: Determines appropriate CDB group for the requested LBA
13239  *		and transfer length, calls scsi_init_pkt, and builds
13240  *		the CDB.  Do not use for partial DMA transfers except
13241  *		for the initial transfer since the CDB size must
13242  *		remain constant.
13243  *
13244  *     Context: Kernel thread and may be called from software interrupt
13245  *		context as part of a sdrunout callback. This function may not
13246  *		block or call routines that block
13247  */
13248 
13249 
13250 int
13251 sd_setup_rw_pkt(struct sd_lun *un,
13252     struct scsi_pkt **pktpp, struct buf *bp, int flags,
13253     int (*callback)(caddr_t), caddr_t callback_arg,
13254     diskaddr_t lba, uint32_t blockcount)
13255 {
13256 	struct scsi_pkt *return_pktp;
13257 	union scsi_cdb *cdbp;
13258 	struct sd_cdbinfo *cp = NULL;
13259 	int i;
13260 
13261 	/*
13262 	 * See which size CDB to use, based upon the request.
13263 	 */
13264 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
13265 
13266 		/*
13267 		 * Check lba and block count against sd_cdbtab limits.
13268 		 * In the partial DMA case, we have to use the same size
13269 		 * CDB for all the transfers.  Check lba + blockcount
13270 		 * against the max LBA so we know that segment of the
13271 		 * transfer can use the CDB we select.
13272 		 */
13273 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
13274 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
13275 
13276 			/*
13277 			 * The command will fit into the CDB type
13278 			 * specified by sd_cdbtab[i].
13279 			 */
13280 			cp = sd_cdbtab + i;
13281 
13282 			/*
13283 			 * Call scsi_init_pkt so we can fill in the
13284 			 * CDB.
13285 			 */
13286 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
13287 			    bp, cp->sc_grpcode, un->un_status_len, 0,
13288 			    flags, callback, callback_arg);
13289 
13290 			if (return_pktp != NULL) {
13291 
13292 				/*
13293 				 * Return new value of pkt
13294 				 */
13295 				*pktpp = return_pktp;
13296 
13297 				/*
13298 				 * To be safe, zero the CDB insuring there is
13299 				 * no leftover data from a previous command.
13300 				 */
13301 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13302 
13303 				/*
13304 				 * Handle partial DMA mapping
13305 				 */
13306 				if (return_pktp->pkt_resid != 0) {
13307 
13308 					/*
13309 					 * Not going to xfer as many blocks as
13310 					 * originally expected
13311 					 */
13312 					blockcount -=
13313 					    SD_BYTES2TGTBLOCKS(un,
13314 						return_pktp->pkt_resid);
13315 				}
13316 
13317 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13318 
13319 				/*
13320 				 * Set command byte based on the CDB
13321 				 * type we matched.
13322 				 */
13323 				cdbp->scc_cmd = cp->sc_grpmask |
13324 				    ((bp->b_flags & B_READ) ?
13325 					SCMD_READ : SCMD_WRITE);
13326 
13327 				SD_FILL_SCSI1_LUN(un, return_pktp);
13328 
13329 				/*
13330 				 * Fill in LBA and length
13331 				 */
13332 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13333 				    (cp->sc_grpcode == CDB_GROUP4) ||
13334 				    (cp->sc_grpcode == CDB_GROUP0) ||
13335 				    (cp->sc_grpcode == CDB_GROUP5));
13336 
13337 				if (cp->sc_grpcode == CDB_GROUP1) {
13338 					FORMG1ADDR(cdbp, lba);
13339 					FORMG1COUNT(cdbp, blockcount);
13340 					return (0);
13341 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13342 					FORMG4LONGADDR(cdbp, lba);
13343 					FORMG4COUNT(cdbp, blockcount);
13344 					return (0);
13345 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13346 					FORMG0ADDR(cdbp, lba);
13347 					FORMG0COUNT(cdbp, blockcount);
13348 					return (0);
13349 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13350 					FORMG5ADDR(cdbp, lba);
13351 					FORMG5COUNT(cdbp, blockcount);
13352 					return (0);
13353 				}
13354 
13355 				/*
13356 				 * It should be impossible to not match one
13357 				 * of the CDB types above, so we should never
13358 				 * reach this point.  Set the CDB command byte
13359 				 * to test-unit-ready to avoid writing
13360 				 * to somewhere we don't intend.
13361 				 */
13362 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13363 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13364 			} else {
13365 				/*
13366 				 * Couldn't get scsi_pkt
13367 				 */
13368 				return (SD_PKT_ALLOC_FAILURE);
13369 			}
13370 		}
13371 	}
13372 
13373 	/*
13374 	 * None of the available CDB types were suitable.  This really
13375 	 * should never happen:  on a 64 bit system we support
13376 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
13377 	 * and on a 32 bit system we will refuse to bind to a device
13378 	 * larger than 2TB so addresses will never be larger than 32 bits.
13379 	 */
13380 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13381 }
13382 
13383 #if defined(__i386) || defined(__amd64)
13384 /*
13385  *    Function: sd_setup_next_rw_pkt
13386  *
13387  * Description: Setup packet for partial DMA transfers, except for the
13388  * 		initial transfer.  sd_setup_rw_pkt should be used for
13389  *		the initial transfer.
13390  *
13391  *     Context: Kernel thread and may be called from interrupt context.
13392  */
13393 
13394 int
13395 sd_setup_next_rw_pkt(struct sd_lun *un,
13396     struct scsi_pkt *pktp, struct buf *bp,
13397     diskaddr_t lba, uint32_t blockcount)
13398 {
13399 	uchar_t com;
13400 	union scsi_cdb *cdbp;
13401 	uchar_t cdb_group_id;
13402 
13403 	ASSERT(pktp != NULL);
13404 	ASSERT(pktp->pkt_cdbp != NULL);
13405 
13406 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13407 	com = cdbp->scc_cmd;
13408 	cdb_group_id = CDB_GROUPID(com);
13409 
13410 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13411 	    (cdb_group_id == CDB_GROUPID_1) ||
13412 	    (cdb_group_id == CDB_GROUPID_4) ||
13413 	    (cdb_group_id == CDB_GROUPID_5));
13414 
13415 	/*
13416 	 * Move pkt to the next portion of the xfer.
13417 	 * func is NULL_FUNC so we do not have to release
13418 	 * the disk mutex here.
13419 	 */
13420 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13421 	    NULL_FUNC, NULL) == pktp) {
13422 		/* Success.  Handle partial DMA */
13423 		if (pktp->pkt_resid != 0) {
13424 			blockcount -=
13425 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13426 		}
13427 
13428 		cdbp->scc_cmd = com;
13429 		SD_FILL_SCSI1_LUN(un, pktp);
13430 		if (cdb_group_id == CDB_GROUPID_1) {
13431 			FORMG1ADDR(cdbp, lba);
13432 			FORMG1COUNT(cdbp, blockcount);
13433 			return (0);
13434 		} else if (cdb_group_id == CDB_GROUPID_4) {
13435 			FORMG4LONGADDR(cdbp, lba);
13436 			FORMG4COUNT(cdbp, blockcount);
13437 			return (0);
13438 		} else if (cdb_group_id == CDB_GROUPID_0) {
13439 			FORMG0ADDR(cdbp, lba);
13440 			FORMG0COUNT(cdbp, blockcount);
13441 			return (0);
13442 		} else if (cdb_group_id == CDB_GROUPID_5) {
13443 			FORMG5ADDR(cdbp, lba);
13444 			FORMG5COUNT(cdbp, blockcount);
13445 			return (0);
13446 		}
13447 
13448 		/* Unreachable */
13449 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13450 	}
13451 
13452 	/*
13453 	 * Error setting up next portion of cmd transfer.
13454 	 * Something is definitely very wrong and this
13455 	 * should not happen.
13456 	 */
13457 	return (SD_PKT_ALLOC_FAILURE);
13458 }
13459 #endif /* defined(__i386) || defined(__amd64) */
13460 
13461 /*
13462  *    Function: sd_initpkt_for_uscsi
13463  *
13464  * Description: Allocate and initialize for transport a scsi_pkt struct,
13465  *		based upon the info specified in the given uscsi_cmd struct.
13466  *
13467  * Return Code: SD_PKT_ALLOC_SUCCESS
13468  *		SD_PKT_ALLOC_FAILURE
13469  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13470  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13471  *
13472  *     Context: Kernel thread and may be called from software interrupt context
13473  *		as part of a sdrunout callback. This function may not block or
13474  *		call routines that block
13475  */
13476 
13477 static int
13478 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
13479 {
13480 	struct uscsi_cmd *uscmd;
13481 	struct sd_xbuf	*xp;
13482 	struct scsi_pkt	*pktp;
13483 	struct sd_lun	*un;
13484 	uint32_t	flags = 0;
13485 
13486 	ASSERT(bp != NULL);
13487 	ASSERT(pktpp != NULL);
13488 	xp = SD_GET_XBUF(bp);
13489 	ASSERT(xp != NULL);
13490 	un = SD_GET_UN(bp);
13491 	ASSERT(un != NULL);
13492 	ASSERT(mutex_owned(SD_MUTEX(un)));
13493 
13494 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13495 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13496 	ASSERT(uscmd != NULL);
13497 
13498 	SD_TRACE(SD_LOG_IO_CORE, un,
13499 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
13500 
13501 	/*
13502 	 * Allocate the scsi_pkt for the command.
13503 	 * Note: If PKT_DMA_PARTIAL flag is set, scsi_vhci binds a path
13504 	 *	 during scsi_init_pkt time and will continue to use the
13505 	 *	 same path as long as the same scsi_pkt is used without
13506 	 *	 intervening scsi_dma_free(). Since uscsi command does
13507 	 *	 not call scsi_dmafree() before retry failed command, it
13508 	 *	 is necessary to make sure PKT_DMA_PARTIAL flag is NOT
13509 	 *	 set such that scsi_vhci can use other available path for
13510 	 *	 retry. Besides, ucsci command does not allow DMA breakup,
13511 	 *	 so there is no need to set PKT_DMA_PARTIAL flag.
13512 	 */
13513 	pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13514 	    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13515 	    sizeof (struct scsi_arq_status), 0,
13516 	    (un->un_pkt_flags & ~PKT_DMA_PARTIAL),
13517 	    sdrunout, (caddr_t)un);
13518 
13519 	if (pktp == NULL) {
13520 		*pktpp = NULL;
13521 		/*
13522 		 * Set the driver state to RWAIT to indicate the driver
13523 		 * is waiting on resource allocations. The driver will not
13524 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13525 		 */
13526 		New_state(un, SD_STATE_RWAIT);
13527 
13528 		SD_ERROR(SD_LOG_IO_CORE, un,
13529 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
13530 
13531 		if ((bp->b_flags & B_ERROR) != 0) {
13532 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13533 		}
13534 		return (SD_PKT_ALLOC_FAILURE);
13535 	}
13536 
13537 	/*
13538 	 * We do not do DMA breakup for USCSI commands, so return failure
13539 	 * here if all the needed DMA resources were not allocated.
13540 	 */
13541 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
13542 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
13543 		scsi_destroy_pkt(pktp);
13544 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
13545 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
13546 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
13547 	}
13548 
13549 	/* Init the cdb from the given uscsi struct */
13550 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
13551 	    uscmd->uscsi_cdb[0], 0, 0, 0);
13552 
13553 	SD_FILL_SCSI1_LUN(un, pktp);
13554 
13555 	/*
13556 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
13557 	 * for listing of the supported flags.
13558 	 */
13559 
13560 	if (uscmd->uscsi_flags & USCSI_SILENT) {
13561 		flags |= FLAG_SILENT;
13562 	}
13563 
13564 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
13565 		flags |= FLAG_DIAGNOSE;
13566 	}
13567 
13568 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
13569 		flags |= FLAG_ISOLATE;
13570 	}
13571 
13572 	if (un->un_f_is_fibre == FALSE) {
13573 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
13574 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
13575 		}
13576 	}
13577 
13578 	/*
13579 	 * Set the pkt flags here so we save time later.
13580 	 * Note: These flags are NOT in the uscsi man page!!!
13581 	 */
13582 	if (uscmd->uscsi_flags & USCSI_HEAD) {
13583 		flags |= FLAG_HEAD;
13584 	}
13585 
13586 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
13587 		flags |= FLAG_NOINTR;
13588 	}
13589 
13590 	/*
13591 	 * For tagged queueing, things get a bit complicated.
13592 	 * Check first for head of queue and last for ordered queue.
13593 	 * If neither head nor order, use the default driver tag flags.
13594 	 */
13595 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
13596 		if (uscmd->uscsi_flags & USCSI_HTAG) {
13597 			flags |= FLAG_HTAG;
13598 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
13599 			flags |= FLAG_OTAG;
13600 		} else {
13601 			flags |= un->un_tagflags & FLAG_TAGMASK;
13602 		}
13603 	}
13604 
13605 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
13606 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
13607 	}
13608 
13609 	pktp->pkt_flags = flags;
13610 
13611 	/* Copy the caller's CDB into the pkt... */
13612 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
13613 
13614 	if (uscmd->uscsi_timeout == 0) {
13615 		pktp->pkt_time = un->un_uscsi_timeout;
13616 	} else {
13617 		pktp->pkt_time = uscmd->uscsi_timeout;
13618 	}
13619 
13620 	/* need it later to identify USCSI request in sdintr */
13621 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
13622 
13623 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
13624 
13625 	pktp->pkt_private = bp;
13626 	pktp->pkt_comp = sdintr;
13627 	*pktpp = pktp;
13628 
13629 	SD_TRACE(SD_LOG_IO_CORE, un,
13630 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
13631 
13632 	return (SD_PKT_ALLOC_SUCCESS);
13633 }
13634 
13635 
13636 /*
13637  *    Function: sd_destroypkt_for_uscsi
13638  *
13639  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
13640  *		IOs.. Also saves relevant info into the associated uscsi_cmd
13641  *		struct.
13642  *
13643  *     Context: May be called under interrupt context
13644  */
13645 
13646 static void
13647 sd_destroypkt_for_uscsi(struct buf *bp)
13648 {
13649 	struct uscsi_cmd *uscmd;
13650 	struct sd_xbuf	*xp;
13651 	struct scsi_pkt	*pktp;
13652 	struct sd_lun	*un;
13653 
13654 	ASSERT(bp != NULL);
13655 	xp = SD_GET_XBUF(bp);
13656 	ASSERT(xp != NULL);
13657 	un = SD_GET_UN(bp);
13658 	ASSERT(un != NULL);
13659 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13660 	pktp = SD_GET_PKTP(bp);
13661 	ASSERT(pktp != NULL);
13662 
13663 	SD_TRACE(SD_LOG_IO_CORE, un,
13664 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
13665 
13666 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13667 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13668 	ASSERT(uscmd != NULL);
13669 
13670 	/* Save the status and the residual into the uscsi_cmd struct */
13671 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
13672 	uscmd->uscsi_resid  = bp->b_resid;
13673 
13674 	/*
13675 	 * If enabled, copy any saved sense data into the area specified
13676 	 * by the uscsi command.
13677 	 */
13678 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
13679 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
13680 		/*
13681 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
13682 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
13683 		 */
13684 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
13685 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
13686 		bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH);
13687 	}
13688 
13689 	/* We are done with the scsi_pkt; free it now */
13690 	ASSERT(SD_GET_PKTP(bp) != NULL);
13691 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13692 
13693 	SD_TRACE(SD_LOG_IO_CORE, un,
13694 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
13695 }
13696 
13697 
13698 /*
13699  *    Function: sd_bioclone_alloc
13700  *
13701  * Description: Allocate a buf(9S) and init it as per the given buf
13702  *		and the various arguments.  The associated sd_xbuf
13703  *		struct is (nearly) duplicated.  The struct buf *bp
13704  *		argument is saved in new_xp->xb_private.
13705  *
13706  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
13707  *		datalen - size of data area for the shadow bp
13708  *		blkno - starting LBA
13709  *		func - function pointer for b_iodone in the shadow buf. (May
13710  *			be NULL if none.)
13711  *
13712  * Return Code: Pointer to allocates buf(9S) struct
13713  *
13714  *     Context: Can sleep.
13715  */
13716 
13717 static struct buf *
13718 sd_bioclone_alloc(struct buf *bp, size_t datalen,
13719 	daddr_t blkno, int (*func)(struct buf *))
13720 {
13721 	struct	sd_lun	*un;
13722 	struct	sd_xbuf	*xp;
13723 	struct	sd_xbuf	*new_xp;
13724 	struct	buf	*new_bp;
13725 
13726 	ASSERT(bp != NULL);
13727 	xp = SD_GET_XBUF(bp);
13728 	ASSERT(xp != NULL);
13729 	un = SD_GET_UN(bp);
13730 	ASSERT(un != NULL);
13731 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13732 
13733 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
13734 	    NULL, KM_SLEEP);
13735 
13736 	new_bp->b_lblkno	= blkno;
13737 
13738 	/*
13739 	 * Allocate an xbuf for the shadow bp and copy the contents of the
13740 	 * original xbuf into it.
13741 	 */
13742 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
13743 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
13744 
13745 	/*
13746 	 * The given bp is automatically saved in the xb_private member
13747 	 * of the new xbuf.  Callers are allowed to depend on this.
13748 	 */
13749 	new_xp->xb_private = bp;
13750 
13751 	new_bp->b_private  = new_xp;
13752 
13753 	return (new_bp);
13754 }
13755 
13756 /*
13757  *    Function: sd_shadow_buf_alloc
13758  *
13759  * Description: Allocate a buf(9S) and init it as per the given buf
13760  *		and the various arguments.  The associated sd_xbuf
13761  *		struct is (nearly) duplicated.  The struct buf *bp
13762  *		argument is saved in new_xp->xb_private.
13763  *
13764  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
13765  *		datalen - size of data area for the shadow bp
13766  *		bflags - B_READ or B_WRITE (pseudo flag)
13767  *		blkno - starting LBA
13768  *		func - function pointer for b_iodone in the shadow buf. (May
13769  *			be NULL if none.)
13770  *
13771  * Return Code: Pointer to allocates buf(9S) struct
13772  *
13773  *     Context: Can sleep.
13774  */
13775 
13776 static struct buf *
13777 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
13778 	daddr_t blkno, int (*func)(struct buf *))
13779 {
13780 	struct	sd_lun	*un;
13781 	struct	sd_xbuf	*xp;
13782 	struct	sd_xbuf	*new_xp;
13783 	struct	buf	*new_bp;
13784 
13785 	ASSERT(bp != NULL);
13786 	xp = SD_GET_XBUF(bp);
13787 	ASSERT(xp != NULL);
13788 	un = SD_GET_UN(bp);
13789 	ASSERT(un != NULL);
13790 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13791 
13792 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
13793 		bp_mapin(bp);
13794 	}
13795 
13796 	bflags &= (B_READ | B_WRITE);
13797 #if defined(__i386) || defined(__amd64)
13798 	new_bp = getrbuf(KM_SLEEP);
13799 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
13800 	new_bp->b_bcount = datalen;
13801 	new_bp->b_flags	= bp->b_flags | bflags;
13802 #else
13803 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
13804 	    datalen, bflags, SLEEP_FUNC, NULL);
13805 #endif
13806 	new_bp->av_forw	= NULL;
13807 	new_bp->av_back	= NULL;
13808 	new_bp->b_dev	= bp->b_dev;
13809 	new_bp->b_blkno	= blkno;
13810 	new_bp->b_iodone = func;
13811 	new_bp->b_edev	= bp->b_edev;
13812 	new_bp->b_resid	= 0;
13813 
13814 	/* We need to preserve the B_FAILFAST flag */
13815 	if (bp->b_flags & B_FAILFAST) {
13816 		new_bp->b_flags |= B_FAILFAST;
13817 	}
13818 
13819 	/*
13820 	 * Allocate an xbuf for the shadow bp and copy the contents of the
13821 	 * original xbuf into it.
13822 	 */
13823 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
13824 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
13825 
13826 	/* Need later to copy data between the shadow buf & original buf! */
13827 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
13828 
13829 	/*
13830 	 * The given bp is automatically saved in the xb_private member
13831 	 * of the new xbuf.  Callers are allowed to depend on this.
13832 	 */
13833 	new_xp->xb_private = bp;
13834 
13835 	new_bp->b_private  = new_xp;
13836 
13837 	return (new_bp);
13838 }
13839 
13840 /*
13841  *    Function: sd_bioclone_free
13842  *
13843  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
13844  *		in the larger than partition operation.
13845  *
13846  *     Context: May be called under interrupt context
13847  */
13848 
13849 static void
13850 sd_bioclone_free(struct buf *bp)
13851 {
13852 	struct sd_xbuf	*xp;
13853 
13854 	ASSERT(bp != NULL);
13855 	xp = SD_GET_XBUF(bp);
13856 	ASSERT(xp != NULL);
13857 
13858 	/*
13859 	 * Call bp_mapout() before freeing the buf,  in case a lower
13860 	 * layer or HBA  had done a bp_mapin().  we must do this here
13861 	 * as we are the "originator" of the shadow buf.
13862 	 */
13863 	bp_mapout(bp);
13864 
13865 	/*
13866 	 * Null out b_iodone before freeing the bp, to ensure that the driver
13867 	 * never gets confused by a stale value in this field. (Just a little
13868 	 * extra defensiveness here.)
13869 	 */
13870 	bp->b_iodone = NULL;
13871 
13872 	freerbuf(bp);
13873 
13874 	kmem_free(xp, sizeof (struct sd_xbuf));
13875 }
13876 
13877 /*
13878  *    Function: sd_shadow_buf_free
13879  *
13880  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
13881  *
13882  *     Context: May be called under interrupt context
13883  */
13884 
13885 static void
13886 sd_shadow_buf_free(struct buf *bp)
13887 {
13888 	struct sd_xbuf	*xp;
13889 
13890 	ASSERT(bp != NULL);
13891 	xp = SD_GET_XBUF(bp);
13892 	ASSERT(xp != NULL);
13893 
13894 #if defined(__sparc)
13895 	/*
13896 	 * Call bp_mapout() before freeing the buf,  in case a lower
13897 	 * layer or HBA  had done a bp_mapin().  we must do this here
13898 	 * as we are the "originator" of the shadow buf.
13899 	 */
13900 	bp_mapout(bp);
13901 #endif
13902 
13903 	/*
13904 	 * Null out b_iodone before freeing the bp, to ensure that the driver
13905 	 * never gets confused by a stale value in this field. (Just a little
13906 	 * extra defensiveness here.)
13907 	 */
13908 	bp->b_iodone = NULL;
13909 
13910 #if defined(__i386) || defined(__amd64)
13911 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
13912 	freerbuf(bp);
13913 #else
13914 	scsi_free_consistent_buf(bp);
13915 #endif
13916 
13917 	kmem_free(xp, sizeof (struct sd_xbuf));
13918 }
13919 
13920 
13921 /*
13922  *    Function: sd_print_transport_rejected_message
13923  *
13924  * Description: This implements the ludicrously complex rules for printing
13925  *		a "transport rejected" message.  This is to address the
13926  *		specific problem of having a flood of this error message
13927  *		produced when a failover occurs.
13928  *
13929  *     Context: Any.
13930  */
13931 
13932 static void
13933 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
13934 	int code)
13935 {
13936 	ASSERT(un != NULL);
13937 	ASSERT(mutex_owned(SD_MUTEX(un)));
13938 	ASSERT(xp != NULL);
13939 
13940 	/*
13941 	 * Print the "transport rejected" message under the following
13942 	 * conditions:
13943 	 *
13944 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
13945 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
13946 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
13947 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
13948 	 *   scsi_transport(9F) (which indicates that the target might have
13949 	 *   gone off-line).  This uses the un->un_tran_fatal_count
13950 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
13951 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
13952 	 *   from scsi_transport().
13953 	 *
13954 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
13955 	 * the preceeding cases in order for the message to be printed.
13956 	 */
13957 	if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) {
13958 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
13959 		    (code != TRAN_FATAL_ERROR) ||
13960 		    (un->un_tran_fatal_count == 1)) {
13961 			switch (code) {
13962 			case TRAN_BADPKT:
13963 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13964 				    "transport rejected bad packet\n");
13965 				break;
13966 			case TRAN_FATAL_ERROR:
13967 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13968 				    "transport rejected fatal error\n");
13969 				break;
13970 			default:
13971 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13972 				    "transport rejected (%d)\n", code);
13973 				break;
13974 			}
13975 		}
13976 	}
13977 }
13978 
13979 
13980 /*
13981  *    Function: sd_add_buf_to_waitq
13982  *
13983  * Description: Add the given buf(9S) struct to the wait queue for the
13984  *		instance.  If sorting is enabled, then the buf is added
13985  *		to the queue via an elevator sort algorithm (a la
13986  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
13987  *		If sorting is not enabled, then the buf is just added
13988  *		to the end of the wait queue.
13989  *
13990  * Return Code: void
13991  *
13992  *     Context: Does not sleep/block, therefore technically can be called
13993  *		from any context.  However if sorting is enabled then the
13994  *		execution time is indeterminate, and may take long if
13995  *		the wait queue grows large.
13996  */
13997 
13998 static void
13999 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
14000 {
14001 	struct buf *ap;
14002 
14003 	ASSERT(bp != NULL);
14004 	ASSERT(un != NULL);
14005 	ASSERT(mutex_owned(SD_MUTEX(un)));
14006 
14007 	/* If the queue is empty, add the buf as the only entry & return. */
14008 	if (un->un_waitq_headp == NULL) {
14009 		ASSERT(un->un_waitq_tailp == NULL);
14010 		un->un_waitq_headp = un->un_waitq_tailp = bp;
14011 		bp->av_forw = NULL;
14012 		return;
14013 	}
14014 
14015 	ASSERT(un->un_waitq_tailp != NULL);
14016 
14017 	/*
14018 	 * If sorting is disabled, just add the buf to the tail end of
14019 	 * the wait queue and return.
14020 	 */
14021 	if (un->un_f_disksort_disabled) {
14022 		un->un_waitq_tailp->av_forw = bp;
14023 		un->un_waitq_tailp = bp;
14024 		bp->av_forw = NULL;
14025 		return;
14026 	}
14027 
14028 	/*
14029 	 * Sort thru the list of requests currently on the wait queue
14030 	 * and add the new buf request at the appropriate position.
14031 	 *
14032 	 * The un->un_waitq_headp is an activity chain pointer on which
14033 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
14034 	 * first queue holds those requests which are positioned after
14035 	 * the current SD_GET_BLKNO() (in the first request); the second holds
14036 	 * requests which came in after their SD_GET_BLKNO() number was passed.
14037 	 * Thus we implement a one way scan, retracting after reaching
14038 	 * the end of the drive to the first request on the second
14039 	 * queue, at which time it becomes the first queue.
14040 	 * A one-way scan is natural because of the way UNIX read-ahead
14041 	 * blocks are allocated.
14042 	 *
14043 	 * If we lie after the first request, then we must locate the
14044 	 * second request list and add ourselves to it.
14045 	 */
14046 	ap = un->un_waitq_headp;
14047 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
14048 		while (ap->av_forw != NULL) {
14049 			/*
14050 			 * Look for an "inversion" in the (normally
14051 			 * ascending) block numbers. This indicates
14052 			 * the start of the second request list.
14053 			 */
14054 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
14055 				/*
14056 				 * Search the second request list for the
14057 				 * first request at a larger block number.
14058 				 * We go before that; however if there is
14059 				 * no such request, we go at the end.
14060 				 */
14061 				do {
14062 					if (SD_GET_BLKNO(bp) <
14063 					    SD_GET_BLKNO(ap->av_forw)) {
14064 						goto insert;
14065 					}
14066 					ap = ap->av_forw;
14067 				} while (ap->av_forw != NULL);
14068 				goto insert;		/* after last */
14069 			}
14070 			ap = ap->av_forw;
14071 		}
14072 
14073 		/*
14074 		 * No inversions... we will go after the last, and
14075 		 * be the first request in the second request list.
14076 		 */
14077 		goto insert;
14078 	}
14079 
14080 	/*
14081 	 * Request is at/after the current request...
14082 	 * sort in the first request list.
14083 	 */
14084 	while (ap->av_forw != NULL) {
14085 		/*
14086 		 * We want to go after the current request (1) if
14087 		 * there is an inversion after it (i.e. it is the end
14088 		 * of the first request list), or (2) if the next
14089 		 * request is a larger block no. than our request.
14090 		 */
14091 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
14092 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
14093 			goto insert;
14094 		}
14095 		ap = ap->av_forw;
14096 	}
14097 
14098 	/*
14099 	 * Neither a second list nor a larger request, therefore
14100 	 * we go at the end of the first list (which is the same
14101 	 * as the end of the whole schebang).
14102 	 */
14103 insert:
14104 	bp->av_forw = ap->av_forw;
14105 	ap->av_forw = bp;
14106 
14107 	/*
14108 	 * If we inserted onto the tail end of the waitq, make sure the
14109 	 * tail pointer is updated.
14110 	 */
14111 	if (ap == un->un_waitq_tailp) {
14112 		un->un_waitq_tailp = bp;
14113 	}
14114 }
14115 
14116 
14117 /*
14118  *    Function: sd_start_cmds
14119  *
14120  * Description: Remove and transport cmds from the driver queues.
14121  *
14122  *   Arguments: un - pointer to the unit (soft state) struct for the target.
14123  *
14124  *		immed_bp - ptr to a buf to be transported immediately. Only
14125  *		the immed_bp is transported; bufs on the waitq are not
14126  *		processed and the un_retry_bp is not checked.  If immed_bp is
14127  *		NULL, then normal queue processing is performed.
14128  *
14129  *     Context: May be called from kernel thread context, interrupt context,
14130  *		or runout callback context. This function may not block or
14131  *		call routines that block.
14132  */
14133 
14134 static void
14135 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
14136 {
14137 	struct	sd_xbuf	*xp;
14138 	struct	buf	*bp;
14139 	void	(*statp)(kstat_io_t *);
14140 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14141 	void	(*saved_statp)(kstat_io_t *);
14142 #endif
14143 	int	rval;
14144 
14145 	ASSERT(un != NULL);
14146 	ASSERT(mutex_owned(SD_MUTEX(un)));
14147 	ASSERT(un->un_ncmds_in_transport >= 0);
14148 	ASSERT(un->un_throttle >= 0);
14149 
14150 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
14151 
14152 	do {
14153 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14154 		saved_statp = NULL;
14155 #endif
14156 
14157 		/*
14158 		 * If we are syncing or dumping, fail the command to
14159 		 * avoid recursively calling back into scsi_transport().
14160 		 * The dump I/O itself uses a separate code path so this
14161 		 * only prevents non-dump I/O from being sent while dumping.
14162 		 * File system sync takes place before dumping begins.
14163 		 * During panic, filesystem I/O is allowed provided
14164 		 * un_in_callback is <= 1.  This is to prevent recursion
14165 		 * such as sd_start_cmds -> scsi_transport -> sdintr ->
14166 		 * sd_start_cmds and so on.  See panic.c for more information
14167 		 * about the states the system can be in during panic.
14168 		 */
14169 		if ((un->un_state == SD_STATE_DUMPING) ||
14170 		    (ddi_in_panic() && (un->un_in_callback > 1))) {
14171 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14172 			    "sd_start_cmds: panicking\n");
14173 			goto exit;
14174 		}
14175 
14176 		if ((bp = immed_bp) != NULL) {
14177 			/*
14178 			 * We have a bp that must be transported immediately.
14179 			 * It's OK to transport the immed_bp here without doing
14180 			 * the throttle limit check because the immed_bp is
14181 			 * always used in a retry/recovery case. This means
14182 			 * that we know we are not at the throttle limit by
14183 			 * virtue of the fact that to get here we must have
14184 			 * already gotten a command back via sdintr(). This also
14185 			 * relies on (1) the command on un_retry_bp preventing
14186 			 * further commands from the waitq from being issued;
14187 			 * and (2) the code in sd_retry_command checking the
14188 			 * throttle limit before issuing a delayed or immediate
14189 			 * retry. This holds even if the throttle limit is
14190 			 * currently ratcheted down from its maximum value.
14191 			 */
14192 			statp = kstat_runq_enter;
14193 			if (bp == un->un_retry_bp) {
14194 				ASSERT((un->un_retry_statp == NULL) ||
14195 				    (un->un_retry_statp == kstat_waitq_enter) ||
14196 				    (un->un_retry_statp ==
14197 				    kstat_runq_back_to_waitq));
14198 				/*
14199 				 * If the waitq kstat was incremented when
14200 				 * sd_set_retry_bp() queued this bp for a retry,
14201 				 * then we must set up statp so that the waitq
14202 				 * count will get decremented correctly below.
14203 				 * Also we must clear un->un_retry_statp to
14204 				 * ensure that we do not act on a stale value
14205 				 * in this field.
14206 				 */
14207 				if ((un->un_retry_statp == kstat_waitq_enter) ||
14208 				    (un->un_retry_statp ==
14209 				    kstat_runq_back_to_waitq)) {
14210 					statp = kstat_waitq_to_runq;
14211 				}
14212 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14213 				saved_statp = un->un_retry_statp;
14214 #endif
14215 				un->un_retry_statp = NULL;
14216 
14217 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14218 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
14219 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
14220 				    un, un->un_retry_bp, un->un_throttle,
14221 				    un->un_ncmds_in_transport);
14222 			} else {
14223 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
14224 				    "processing priority bp:0x%p\n", bp);
14225 			}
14226 
14227 		} else if ((bp = un->un_waitq_headp) != NULL) {
14228 			/*
14229 			 * A command on the waitq is ready to go, but do not
14230 			 * send it if:
14231 			 *
14232 			 * (1) the throttle limit has been reached, or
14233 			 * (2) a retry is pending, or
14234 			 * (3) a START_STOP_UNIT callback pending, or
14235 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
14236 			 *	command is pending.
14237 			 *
14238 			 * For all of these conditions, IO processing will
14239 			 * restart after the condition is cleared.
14240 			 */
14241 			if (un->un_ncmds_in_transport >= un->un_throttle) {
14242 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14243 				    "sd_start_cmds: exiting, "
14244 				    "throttle limit reached!\n");
14245 				goto exit;
14246 			}
14247 			if (un->un_retry_bp != NULL) {
14248 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14249 				    "sd_start_cmds: exiting, retry pending!\n");
14250 				goto exit;
14251 			}
14252 			if (un->un_startstop_timeid != NULL) {
14253 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14254 				    "sd_start_cmds: exiting, "
14255 				    "START_STOP pending!\n");
14256 				goto exit;
14257 			}
14258 			if (un->un_direct_priority_timeid != NULL) {
14259 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14260 				    "sd_start_cmds: exiting, "
14261 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
14262 				goto exit;
14263 			}
14264 
14265 			/* Dequeue the command */
14266 			un->un_waitq_headp = bp->av_forw;
14267 			if (un->un_waitq_headp == NULL) {
14268 				un->un_waitq_tailp = NULL;
14269 			}
14270 			bp->av_forw = NULL;
14271 			statp = kstat_waitq_to_runq;
14272 			SD_TRACE(SD_LOG_IO_CORE, un,
14273 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
14274 
14275 		} else {
14276 			/* No work to do so bail out now */
14277 			SD_TRACE(SD_LOG_IO_CORE, un,
14278 			    "sd_start_cmds: no more work, exiting!\n");
14279 			goto exit;
14280 		}
14281 
14282 		/*
14283 		 * Reset the state to normal. This is the mechanism by which
14284 		 * the state transitions from either SD_STATE_RWAIT or
14285 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
14286 		 * If state is SD_STATE_PM_CHANGING then this command is
14287 		 * part of the device power control and the state must
14288 		 * not be put back to normal. Doing so would would
14289 		 * allow new commands to proceed when they shouldn't,
14290 		 * the device may be going off.
14291 		 */
14292 		if ((un->un_state != SD_STATE_SUSPENDED) &&
14293 		    (un->un_state != SD_STATE_PM_CHANGING)) {
14294 			New_state(un, SD_STATE_NORMAL);
14295 		    }
14296 
14297 		xp = SD_GET_XBUF(bp);
14298 		ASSERT(xp != NULL);
14299 
14300 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14301 		/*
14302 		 * Allocate the scsi_pkt if we need one, or attach DMA
14303 		 * resources if we have a scsi_pkt that needs them. The
14304 		 * latter should only occur for commands that are being
14305 		 * retried.
14306 		 */
14307 		if ((xp->xb_pktp == NULL) ||
14308 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
14309 #else
14310 		if (xp->xb_pktp == NULL) {
14311 #endif
14312 			/*
14313 			 * There is no scsi_pkt allocated for this buf. Call
14314 			 * the initpkt function to allocate & init one.
14315 			 *
14316 			 * The scsi_init_pkt runout callback functionality is
14317 			 * implemented as follows:
14318 			 *
14319 			 * 1) The initpkt function always calls
14320 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14321 			 *    callback routine.
14322 			 * 2) A successful packet allocation is initialized and
14323 			 *    the I/O is transported.
14324 			 * 3) The I/O associated with an allocation resource
14325 			 *    failure is left on its queue to be retried via
14326 			 *    runout or the next I/O.
14327 			 * 4) The I/O associated with a DMA error is removed
14328 			 *    from the queue and failed with EIO. Processing of
14329 			 *    the transport queues is also halted to be
14330 			 *    restarted via runout or the next I/O.
14331 			 * 5) The I/O associated with a CDB size or packet
14332 			 *    size error is removed from the queue and failed
14333 			 *    with EIO. Processing of the transport queues is
14334 			 *    continued.
14335 			 *
14336 			 * Note: there is no interface for canceling a runout
14337 			 * callback. To prevent the driver from detaching or
14338 			 * suspending while a runout is pending the driver
14339 			 * state is set to SD_STATE_RWAIT
14340 			 *
14341 			 * Note: using the scsi_init_pkt callback facility can
14342 			 * result in an I/O request persisting at the head of
14343 			 * the list which cannot be satisfied even after
14344 			 * multiple retries. In the future the driver may
14345 			 * implement some kind of maximum runout count before
14346 			 * failing an I/O.
14347 			 *
14348 			 * Note: the use of funcp below may seem superfluous,
14349 			 * but it helps warlock figure out the correct
14350 			 * initpkt function calls (see [s]sd.wlcmd).
14351 			 */
14352 			struct scsi_pkt	*pktp;
14353 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14354 
14355 			ASSERT(bp != un->un_rqs_bp);
14356 
14357 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
14358 			switch ((*funcp)(bp, &pktp)) {
14359 			case  SD_PKT_ALLOC_SUCCESS:
14360 				xp->xb_pktp = pktp;
14361 				SD_TRACE(SD_LOG_IO_CORE, un,
14362 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14363 				    pktp);
14364 				goto got_pkt;
14365 
14366 			case SD_PKT_ALLOC_FAILURE:
14367 				/*
14368 				 * Temporary (hopefully) resource depletion.
14369 				 * Since retries and RQS commands always have a
14370 				 * scsi_pkt allocated, these cases should never
14371 				 * get here. So the only cases this needs to
14372 				 * handle is a bp from the waitq (which we put
14373 				 * back onto the waitq for sdrunout), or a bp
14374 				 * sent as an immed_bp (which we just fail).
14375 				 */
14376 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14377 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14378 
14379 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14380 
14381 				if (bp == immed_bp) {
14382 					/*
14383 					 * If SD_XB_DMA_FREED is clear, then
14384 					 * this is a failure to allocate a
14385 					 * scsi_pkt, and we must fail the
14386 					 * command.
14387 					 */
14388 					if ((xp->xb_pkt_flags &
14389 					    SD_XB_DMA_FREED) == 0) {
14390 						break;
14391 					}
14392 
14393 					/*
14394 					 * If this immediate command is NOT our
14395 					 * un_retry_bp, then we must fail it.
14396 					 */
14397 					if (bp != un->un_retry_bp) {
14398 						break;
14399 					}
14400 
14401 					/*
14402 					 * We get here if this cmd is our
14403 					 * un_retry_bp that was DMAFREED, but
14404 					 * scsi_init_pkt() failed to reallocate
14405 					 * DMA resources when we attempted to
14406 					 * retry it. This can happen when an
14407 					 * mpxio failover is in progress, but
14408 					 * we don't want to just fail the
14409 					 * command in this case.
14410 					 *
14411 					 * Use timeout(9F) to restart it after
14412 					 * a 100ms delay.  We don't want to
14413 					 * let sdrunout() restart it, because
14414 					 * sdrunout() is just supposed to start
14415 					 * commands that are sitting on the
14416 					 * wait queue.  The un_retry_bp stays
14417 					 * set until the command completes, but
14418 					 * sdrunout can be called many times
14419 					 * before that happens.  Since sdrunout
14420 					 * cannot tell if the un_retry_bp is
14421 					 * already in the transport, it could
14422 					 * end up calling scsi_transport() for
14423 					 * the un_retry_bp multiple times.
14424 					 *
14425 					 * Also: don't schedule the callback
14426 					 * if some other callback is already
14427 					 * pending.
14428 					 */
14429 					if (un->un_retry_statp == NULL) {
14430 						/*
14431 						 * restore the kstat pointer to
14432 						 * keep kstat counts coherent
14433 						 * when we do retry the command.
14434 						 */
14435 						un->un_retry_statp =
14436 						    saved_statp;
14437 					}
14438 
14439 					if ((un->un_startstop_timeid == NULL) &&
14440 					    (un->un_retry_timeid == NULL) &&
14441 					    (un->un_direct_priority_timeid ==
14442 					    NULL)) {
14443 
14444 						un->un_retry_timeid =
14445 						    timeout(
14446 						    sd_start_retry_command,
14447 						    un, SD_RESTART_TIMEOUT);
14448 					}
14449 					goto exit;
14450 				}
14451 
14452 #else
14453 				if (bp == immed_bp) {
14454 					break;	/* Just fail the command */
14455 				}
14456 #endif
14457 
14458 				/* Add the buf back to the head of the waitq */
14459 				bp->av_forw = un->un_waitq_headp;
14460 				un->un_waitq_headp = bp;
14461 				if (un->un_waitq_tailp == NULL) {
14462 					un->un_waitq_tailp = bp;
14463 				}
14464 				goto exit;
14465 
14466 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
14467 				/*
14468 				 * HBA DMA resource failure. Fail the command
14469 				 * and continue processing of the queues.
14470 				 */
14471 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14472 				    "sd_start_cmds: "
14473 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
14474 				break;
14475 
14476 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
14477 				/*
14478 				 * Note:x86: Partial DMA mapping not supported
14479 				 * for USCSI commands, and all the needed DMA
14480 				 * resources were not allocated.
14481 				 */
14482 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14483 				    "sd_start_cmds: "
14484 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
14485 				break;
14486 
14487 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
14488 				/*
14489 				 * Note:x86: Request cannot fit into CDB based
14490 				 * on lba and len.
14491 				 */
14492 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14493 				    "sd_start_cmds: "
14494 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
14495 				break;
14496 
14497 			default:
14498 				/* Should NEVER get here! */
14499 				panic("scsi_initpkt error");
14500 				/*NOTREACHED*/
14501 			}
14502 
14503 			/*
14504 			 * Fatal error in allocating a scsi_pkt for this buf.
14505 			 * Update kstats & return the buf with an error code.
14506 			 * We must use sd_return_failed_command_no_restart() to
14507 			 * avoid a recursive call back into sd_start_cmds().
14508 			 * However this also means that we must keep processing
14509 			 * the waitq here in order to avoid stalling.
14510 			 */
14511 			if (statp == kstat_waitq_to_runq) {
14512 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
14513 			}
14514 			sd_return_failed_command_no_restart(un, bp, EIO);
14515 			if (bp == immed_bp) {
14516 				/* immed_bp is gone by now, so clear this */
14517 				immed_bp = NULL;
14518 			}
14519 			continue;
14520 		}
14521 got_pkt:
14522 		if (bp == immed_bp) {
14523 			/* goto the head of the class.... */
14524 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
14525 		}
14526 
14527 		un->un_ncmds_in_transport++;
14528 		SD_UPDATE_KSTATS(un, statp, bp);
14529 
14530 		/*
14531 		 * Call scsi_transport() to send the command to the target.
14532 		 * According to SCSA architecture, we must drop the mutex here
14533 		 * before calling scsi_transport() in order to avoid deadlock.
14534 		 * Note that the scsi_pkt's completion routine can be executed
14535 		 * (from interrupt context) even before the call to
14536 		 * scsi_transport() returns.
14537 		 */
14538 		SD_TRACE(SD_LOG_IO_CORE, un,
14539 		    "sd_start_cmds: calling scsi_transport()\n");
14540 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
14541 
14542 		mutex_exit(SD_MUTEX(un));
14543 		rval = scsi_transport(xp->xb_pktp);
14544 		mutex_enter(SD_MUTEX(un));
14545 
14546 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14547 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
14548 
14549 		switch (rval) {
14550 		case TRAN_ACCEPT:
14551 			/* Clear this with every pkt accepted by the HBA */
14552 			un->un_tran_fatal_count = 0;
14553 			break;	/* Success; try the next cmd (if any) */
14554 
14555 		case TRAN_BUSY:
14556 			un->un_ncmds_in_transport--;
14557 			ASSERT(un->un_ncmds_in_transport >= 0);
14558 
14559 			/*
14560 			 * Don't retry request sense, the sense data
14561 			 * is lost when another request is sent.
14562 			 * Free up the rqs buf and retry
14563 			 * the original failed cmd.  Update kstat.
14564 			 */
14565 			if (bp == un->un_rqs_bp) {
14566 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14567 				bp = sd_mark_rqs_idle(un, xp);
14568 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
14569 					NULL, NULL, EIO, SD_BSY_TIMEOUT / 500,
14570 					kstat_waitq_enter);
14571 				goto exit;
14572 			}
14573 
14574 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14575 			/*
14576 			 * Free the DMA resources for the  scsi_pkt. This will
14577 			 * allow mpxio to select another path the next time
14578 			 * we call scsi_transport() with this scsi_pkt.
14579 			 * See sdintr() for the rationalization behind this.
14580 			 */
14581 			if ((un->un_f_is_fibre == TRUE) &&
14582 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
14583 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
14584 				scsi_dmafree(xp->xb_pktp);
14585 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
14586 			}
14587 #endif
14588 
14589 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
14590 				/*
14591 				 * Commands that are SD_PATH_DIRECT_PRIORITY
14592 				 * are for error recovery situations. These do
14593 				 * not use the normal command waitq, so if they
14594 				 * get a TRAN_BUSY we cannot put them back onto
14595 				 * the waitq for later retry. One possible
14596 				 * problem is that there could already be some
14597 				 * other command on un_retry_bp that is waiting
14598 				 * for this one to complete, so we would be
14599 				 * deadlocked if we put this command back onto
14600 				 * the waitq for later retry (since un_retry_bp
14601 				 * must complete before the driver gets back to
14602 				 * commands on the waitq).
14603 				 *
14604 				 * To avoid deadlock we must schedule a callback
14605 				 * that will restart this command after a set
14606 				 * interval.  This should keep retrying for as
14607 				 * long as the underlying transport keeps
14608 				 * returning TRAN_BUSY (just like for other
14609 				 * commands).  Use the same timeout interval as
14610 				 * for the ordinary TRAN_BUSY retry.
14611 				 */
14612 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14613 				    "sd_start_cmds: scsi_transport() returned "
14614 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
14615 
14616 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14617 				un->un_direct_priority_timeid =
14618 				    timeout(sd_start_direct_priority_command,
14619 				    bp, SD_BSY_TIMEOUT / 500);
14620 
14621 				goto exit;
14622 			}
14623 
14624 			/*
14625 			 * For TRAN_BUSY, we want to reduce the throttle value,
14626 			 * unless we are retrying a command.
14627 			 */
14628 			if (bp != un->un_retry_bp) {
14629 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
14630 			}
14631 
14632 			/*
14633 			 * Set up the bp to be tried again 10 ms later.
14634 			 * Note:x86: Is there a timeout value in the sd_lun
14635 			 * for this condition?
14636 			 */
14637 			sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500,
14638 				kstat_runq_back_to_waitq);
14639 			goto exit;
14640 
14641 		case TRAN_FATAL_ERROR:
14642 			un->un_tran_fatal_count++;
14643 			/* FALLTHRU */
14644 
14645 		case TRAN_BADPKT:
14646 		default:
14647 			un->un_ncmds_in_transport--;
14648 			ASSERT(un->un_ncmds_in_transport >= 0);
14649 
14650 			/*
14651 			 * If this is our REQUEST SENSE command with a
14652 			 * transport error, we must get back the pointers
14653 			 * to the original buf, and mark the REQUEST
14654 			 * SENSE command as "available".
14655 			 */
14656 			if (bp == un->un_rqs_bp) {
14657 				bp = sd_mark_rqs_idle(un, xp);
14658 				xp = SD_GET_XBUF(bp);
14659 			} else {
14660 				/*
14661 				 * Legacy behavior: do not update transport
14662 				 * error count for request sense commands.
14663 				 */
14664 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
14665 			}
14666 
14667 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14668 			sd_print_transport_rejected_message(un, xp, rval);
14669 
14670 			/*
14671 			 * We must use sd_return_failed_command_no_restart() to
14672 			 * avoid a recursive call back into sd_start_cmds().
14673 			 * However this also means that we must keep processing
14674 			 * the waitq here in order to avoid stalling.
14675 			 */
14676 			sd_return_failed_command_no_restart(un, bp, EIO);
14677 
14678 			/*
14679 			 * Notify any threads waiting in sd_ddi_suspend() that
14680 			 * a command completion has occurred.
14681 			 */
14682 			if (un->un_state == SD_STATE_SUSPENDED) {
14683 				cv_broadcast(&un->un_disk_busy_cv);
14684 			}
14685 
14686 			if (bp == immed_bp) {
14687 				/* immed_bp is gone by now, so clear this */
14688 				immed_bp = NULL;
14689 			}
14690 			break;
14691 		}
14692 
14693 	} while (immed_bp == NULL);
14694 
14695 exit:
14696 	ASSERT(mutex_owned(SD_MUTEX(un)));
14697 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
14698 }
14699 
14700 
14701 /*
14702  *    Function: sd_return_command
14703  *
14704  * Description: Returns a command to its originator (with or without an
14705  *		error).  Also starts commands waiting to be transported
14706  *		to the target.
14707  *
14708  *     Context: May be called from interrupt, kernel, or timeout context
14709  */
14710 
14711 static void
14712 sd_return_command(struct sd_lun *un, struct buf *bp)
14713 {
14714 	struct sd_xbuf *xp;
14715 #if defined(__i386) || defined(__amd64)
14716 	struct scsi_pkt *pktp;
14717 #endif
14718 
14719 	ASSERT(bp != NULL);
14720 	ASSERT(un != NULL);
14721 	ASSERT(mutex_owned(SD_MUTEX(un)));
14722 	ASSERT(bp != un->un_rqs_bp);
14723 	xp = SD_GET_XBUF(bp);
14724 	ASSERT(xp != NULL);
14725 
14726 #if defined(__i386) || defined(__amd64)
14727 	pktp = SD_GET_PKTP(bp);
14728 #endif
14729 
14730 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
14731 
14732 #if defined(__i386) || defined(__amd64)
14733 	/*
14734 	 * Note:x86: check for the "sdrestart failed" case.
14735 	 */
14736 	if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
14737 		(geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
14738 		(xp->xb_pktp->pkt_resid == 0)) {
14739 
14740 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
14741 			/*
14742 			 * Successfully set up next portion of cmd
14743 			 * transfer, try sending it
14744 			 */
14745 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
14746 			    NULL, NULL, 0, (clock_t)0, NULL);
14747 			sd_start_cmds(un, NULL);
14748 			return;	/* Note:x86: need a return here? */
14749 		}
14750 	}
14751 #endif
14752 
14753 	/*
14754 	 * If this is the failfast bp, clear it from un_failfast_bp. This
14755 	 * can happen if upon being re-tried the failfast bp either
14756 	 * succeeded or encountered another error (possibly even a different
14757 	 * error than the one that precipitated the failfast state, but in
14758 	 * that case it would have had to exhaust retries as well). Regardless,
14759 	 * this should not occur whenever the instance is in the active
14760 	 * failfast state.
14761 	 */
14762 	if (bp == un->un_failfast_bp) {
14763 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
14764 		un->un_failfast_bp = NULL;
14765 	}
14766 
14767 	/*
14768 	 * Clear the failfast state upon successful completion of ANY cmd.
14769 	 */
14770 	if (bp->b_error == 0) {
14771 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
14772 	}
14773 
14774 	/*
14775 	 * This is used if the command was retried one or more times. Show that
14776 	 * we are done with it, and allow processing of the waitq to resume.
14777 	 */
14778 	if (bp == un->un_retry_bp) {
14779 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14780 		    "sd_return_command: un:0x%p: "
14781 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
14782 		un->un_retry_bp = NULL;
14783 		un->un_retry_statp = NULL;
14784 	}
14785 
14786 	SD_UPDATE_RDWR_STATS(un, bp);
14787 	SD_UPDATE_PARTITION_STATS(un, bp);
14788 
14789 	switch (un->un_state) {
14790 	case SD_STATE_SUSPENDED:
14791 		/*
14792 		 * Notify any threads waiting in sd_ddi_suspend() that
14793 		 * a command completion has occurred.
14794 		 */
14795 		cv_broadcast(&un->un_disk_busy_cv);
14796 		break;
14797 	default:
14798 		sd_start_cmds(un, NULL);
14799 		break;
14800 	}
14801 
14802 	/* Return this command up the iodone chain to its originator. */
14803 	mutex_exit(SD_MUTEX(un));
14804 
14805 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
14806 	xp->xb_pktp = NULL;
14807 
14808 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
14809 
14810 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14811 	mutex_enter(SD_MUTEX(un));
14812 
14813 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
14814 }
14815 
14816 
14817 /*
14818  *    Function: sd_return_failed_command
14819  *
14820  * Description: Command completion when an error occurred.
14821  *
14822  *     Context: May be called from interrupt context
14823  */
14824 
14825 static void
14826 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
14827 {
14828 	ASSERT(bp != NULL);
14829 	ASSERT(un != NULL);
14830 	ASSERT(mutex_owned(SD_MUTEX(un)));
14831 
14832 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14833 	    "sd_return_failed_command: entry\n");
14834 
14835 	/*
14836 	 * b_resid could already be nonzero due to a partial data
14837 	 * transfer, so do not change it here.
14838 	 */
14839 	SD_BIOERROR(bp, errcode);
14840 
14841 	sd_return_command(un, bp);
14842 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14843 	    "sd_return_failed_command: exit\n");
14844 }
14845 
14846 
14847 /*
14848  *    Function: sd_return_failed_command_no_restart
14849  *
14850  * Description: Same as sd_return_failed_command, but ensures that no
14851  *		call back into sd_start_cmds will be issued.
14852  *
14853  *     Context: May be called from interrupt context
14854  */
14855 
14856 static void
14857 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
14858 	int errcode)
14859 {
14860 	struct sd_xbuf *xp;
14861 
14862 	ASSERT(bp != NULL);
14863 	ASSERT(un != NULL);
14864 	ASSERT(mutex_owned(SD_MUTEX(un)));
14865 	xp = SD_GET_XBUF(bp);
14866 	ASSERT(xp != NULL);
14867 	ASSERT(errcode != 0);
14868 
14869 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14870 	    "sd_return_failed_command_no_restart: entry\n");
14871 
14872 	/*
14873 	 * b_resid could already be nonzero due to a partial data
14874 	 * transfer, so do not change it here.
14875 	 */
14876 	SD_BIOERROR(bp, errcode);
14877 
14878 	/*
14879 	 * If this is the failfast bp, clear it. This can happen if the
14880 	 * failfast bp encounterd a fatal error when we attempted to
14881 	 * re-try it (such as a scsi_transport(9F) failure).  However
14882 	 * we should NOT be in an active failfast state if the failfast
14883 	 * bp is not NULL.
14884 	 */
14885 	if (bp == un->un_failfast_bp) {
14886 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
14887 		un->un_failfast_bp = NULL;
14888 	}
14889 
14890 	if (bp == un->un_retry_bp) {
14891 		/*
14892 		 * This command was retried one or more times. Show that we are
14893 		 * done with it, and allow processing of the waitq to resume.
14894 		 */
14895 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14896 		    "sd_return_failed_command_no_restart: "
14897 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
14898 		un->un_retry_bp = NULL;
14899 		un->un_retry_statp = NULL;
14900 	}
14901 
14902 	SD_UPDATE_RDWR_STATS(un, bp);
14903 	SD_UPDATE_PARTITION_STATS(un, bp);
14904 
14905 	mutex_exit(SD_MUTEX(un));
14906 
14907 	if (xp->xb_pktp != NULL) {
14908 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
14909 		xp->xb_pktp = NULL;
14910 	}
14911 
14912 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
14913 
14914 	mutex_enter(SD_MUTEX(un));
14915 
14916 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14917 	    "sd_return_failed_command_no_restart: exit\n");
14918 }
14919 
14920 
14921 /*
14922  *    Function: sd_retry_command
14923  *
14924  * Description: queue up a command for retry, or (optionally) fail it
14925  *		if retry counts are exhausted.
14926  *
14927  *   Arguments: un - Pointer to the sd_lun struct for the target.
14928  *
14929  *		bp - Pointer to the buf for the command to be retried.
14930  *
14931  *		retry_check_flag - Flag to see which (if any) of the retry
14932  *		   counts should be decremented/checked. If the indicated
14933  *		   retry count is exhausted, then the command will not be
14934  *		   retried; it will be failed instead. This should use a
14935  *		   value equal to one of the following:
14936  *
14937  *			SD_RETRIES_NOCHECK
14938  *			SD_RESD_RETRIES_STANDARD
14939  *			SD_RETRIES_VICTIM
14940  *
14941  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
14942  *		   if the check should be made to see of FLAG_ISOLATE is set
14943  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
14944  *		   not retried, it is simply failed.
14945  *
14946  *		user_funcp - Ptr to function to call before dispatching the
14947  *		   command. May be NULL if no action needs to be performed.
14948  *		   (Primarily intended for printing messages.)
14949  *
14950  *		user_arg - Optional argument to be passed along to
14951  *		   the user_funcp call.
14952  *
14953  *		failure_code - errno return code to set in the bp if the
14954  *		   command is going to be failed.
14955  *
14956  *		retry_delay - Retry delay interval in (clock_t) units. May
14957  *		   be zero which indicates that the retry should be retried
14958  *		   immediately (ie, without an intervening delay).
14959  *
14960  *		statp - Ptr to kstat function to be updated if the command
14961  *		   is queued for a delayed retry. May be NULL if no kstat
14962  *		   update is desired.
14963  *
14964  *     Context: May be called from interupt context.
14965  */
14966 
14967 static void
14968 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
14969 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
14970 	code), void *user_arg, int failure_code,  clock_t retry_delay,
14971 	void (*statp)(kstat_io_t *))
14972 {
14973 	struct sd_xbuf	*xp;
14974 	struct scsi_pkt	*pktp;
14975 
14976 	ASSERT(un != NULL);
14977 	ASSERT(mutex_owned(SD_MUTEX(un)));
14978 	ASSERT(bp != NULL);
14979 	xp = SD_GET_XBUF(bp);
14980 	ASSERT(xp != NULL);
14981 	pktp = SD_GET_PKTP(bp);
14982 	ASSERT(pktp != NULL);
14983 
14984 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14985 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
14986 
14987 	/*
14988 	 * If we are syncing or dumping, fail the command to avoid
14989 	 * recursively calling back into scsi_transport().
14990 	 */
14991 	if (ddi_in_panic()) {
14992 		goto fail_command_no_log;
14993 	}
14994 
14995 	/*
14996 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
14997 	 * log an error and fail the command.
14998 	 */
14999 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15000 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
15001 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
15002 		sd_dump_memory(un, SD_LOG_IO, "CDB",
15003 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
15004 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
15005 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
15006 		goto fail_command;
15007 	}
15008 
15009 	/*
15010 	 * If we are suspended, then put the command onto head of the
15011 	 * wait queue since we don't want to start more commands.
15012 	 */
15013 	switch (un->un_state) {
15014 	case SD_STATE_SUSPENDED:
15015 	case SD_STATE_DUMPING:
15016 		bp->av_forw = un->un_waitq_headp;
15017 		un->un_waitq_headp = bp;
15018 		if (un->un_waitq_tailp == NULL) {
15019 			un->un_waitq_tailp = bp;
15020 		}
15021 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
15022 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
15023 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
15024 		return;
15025 	default:
15026 		break;
15027 	}
15028 
15029 	/*
15030 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
15031 	 * is set; if it is then we do not want to retry the command.
15032 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
15033 	 */
15034 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
15035 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
15036 			goto fail_command;
15037 		}
15038 	}
15039 
15040 
15041 	/*
15042 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
15043 	 * command timeout or a selection timeout has occurred. This means
15044 	 * that we were unable to establish an kind of communication with
15045 	 * the target, and subsequent retries and/or commands are likely
15046 	 * to encounter similar results and take a long time to complete.
15047 	 *
15048 	 * If this is a failfast error condition, we need to update the
15049 	 * failfast state, even if this bp does not have B_FAILFAST set.
15050 	 */
15051 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
15052 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
15053 			ASSERT(un->un_failfast_bp == NULL);
15054 			/*
15055 			 * If we are already in the active failfast state, and
15056 			 * another failfast error condition has been detected,
15057 			 * then fail this command if it has B_FAILFAST set.
15058 			 * If B_FAILFAST is clear, then maintain the legacy
15059 			 * behavior of retrying heroically, even tho this will
15060 			 * take a lot more time to fail the command.
15061 			 */
15062 			if (bp->b_flags & B_FAILFAST) {
15063 				goto fail_command;
15064 			}
15065 		} else {
15066 			/*
15067 			 * We're not in the active failfast state, but we
15068 			 * have a failfast error condition, so we must begin
15069 			 * transition to the next state. We do this regardless
15070 			 * of whether or not this bp has B_FAILFAST set.
15071 			 */
15072 			if (un->un_failfast_bp == NULL) {
15073 				/*
15074 				 * This is the first bp to meet a failfast
15075 				 * condition so save it on un_failfast_bp &
15076 				 * do normal retry processing. Do not enter
15077 				 * active failfast state yet. This marks
15078 				 * entry into the "failfast pending" state.
15079 				 */
15080 				un->un_failfast_bp = bp;
15081 
15082 			} else if (un->un_failfast_bp == bp) {
15083 				/*
15084 				 * This is the second time *this* bp has
15085 				 * encountered a failfast error condition,
15086 				 * so enter active failfast state & flush
15087 				 * queues as appropriate.
15088 				 */
15089 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
15090 				un->un_failfast_bp = NULL;
15091 				sd_failfast_flushq(un);
15092 
15093 				/*
15094 				 * Fail this bp now if B_FAILFAST set;
15095 				 * otherwise continue with retries. (It would
15096 				 * be pretty ironic if this bp succeeded on a
15097 				 * subsequent retry after we just flushed all
15098 				 * the queues).
15099 				 */
15100 				if (bp->b_flags & B_FAILFAST) {
15101 					goto fail_command;
15102 				}
15103 
15104 #if !defined(lint) && !defined(__lint)
15105 			} else {
15106 				/*
15107 				 * If neither of the preceeding conditionals
15108 				 * was true, it means that there is some
15109 				 * *other* bp that has met an inital failfast
15110 				 * condition and is currently either being
15111 				 * retried or is waiting to be retried. In
15112 				 * that case we should perform normal retry
15113 				 * processing on *this* bp, since there is a
15114 				 * chance that the current failfast condition
15115 				 * is transient and recoverable. If that does
15116 				 * not turn out to be the case, then retries
15117 				 * will be cleared when the wait queue is
15118 				 * flushed anyway.
15119 				 */
15120 #endif
15121 			}
15122 		}
15123 	} else {
15124 		/*
15125 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
15126 		 * likely were able to at least establish some level of
15127 		 * communication with the target and subsequent commands
15128 		 * and/or retries are likely to get through to the target,
15129 		 * In this case we want to be aggressive about clearing
15130 		 * the failfast state. Note that this does not affect
15131 		 * the "failfast pending" condition.
15132 		 */
15133 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
15134 	}
15135 
15136 
15137 	/*
15138 	 * Check the specified retry count to see if we can still do
15139 	 * any retries with this pkt before we should fail it.
15140 	 */
15141 	switch (retry_check_flag & SD_RETRIES_MASK) {
15142 	case SD_RETRIES_VICTIM:
15143 		/*
15144 		 * Check the victim retry count. If exhausted, then fall
15145 		 * thru & check against the standard retry count.
15146 		 */
15147 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
15148 			/* Increment count & proceed with the retry */
15149 			xp->xb_victim_retry_count++;
15150 			break;
15151 		}
15152 		/* Victim retries exhausted, fall back to std. retries... */
15153 		/* FALLTHRU */
15154 
15155 	case SD_RETRIES_STANDARD:
15156 		if (xp->xb_retry_count >= un->un_retry_count) {
15157 			/* Retries exhausted, fail the command */
15158 			SD_TRACE(SD_LOG_IO_CORE, un,
15159 			    "sd_retry_command: retries exhausted!\n");
15160 			/*
15161 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
15162 			 * commands with nonzero pkt_resid.
15163 			 */
15164 			if ((pktp->pkt_reason == CMD_CMPLT) &&
15165 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
15166 			    (pktp->pkt_resid != 0)) {
15167 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
15168 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
15169 					SD_UPDATE_B_RESID(bp, pktp);
15170 				}
15171 			}
15172 			goto fail_command;
15173 		}
15174 		xp->xb_retry_count++;
15175 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15176 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15177 		break;
15178 
15179 	case SD_RETRIES_UA:
15180 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
15181 			/* Retries exhausted, fail the command */
15182 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
15183 			    "Unit Attention retries exhausted. "
15184 			    "Check the target.\n");
15185 			goto fail_command;
15186 		}
15187 		xp->xb_ua_retry_count++;
15188 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15189 		    "sd_retry_command: retry count:%d\n",
15190 			xp->xb_ua_retry_count);
15191 		break;
15192 
15193 	case SD_RETRIES_BUSY:
15194 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
15195 			/* Retries exhausted, fail the command */
15196 			SD_TRACE(SD_LOG_IO_CORE, un,
15197 			    "sd_retry_command: retries exhausted!\n");
15198 			goto fail_command;
15199 		}
15200 		xp->xb_retry_count++;
15201 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15202 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
15203 		break;
15204 
15205 	case SD_RETRIES_NOCHECK:
15206 	default:
15207 		/* No retry count to check. Just proceed with the retry */
15208 		break;
15209 	}
15210 
15211 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
15212 
15213 	/*
15214 	 * If we were given a zero timeout, we must attempt to retry the
15215 	 * command immediately (ie, without a delay).
15216 	 */
15217 	if (retry_delay == 0) {
15218 		/*
15219 		 * Check some limiting conditions to see if we can actually
15220 		 * do the immediate retry.  If we cannot, then we must
15221 		 * fall back to queueing up a delayed retry.
15222 		 */
15223 		if (un->un_ncmds_in_transport >= un->un_throttle) {
15224 			/*
15225 			 * We are at the throttle limit for the target,
15226 			 * fall back to delayed retry.
15227 			 */
15228 			retry_delay = SD_BSY_TIMEOUT;
15229 			statp = kstat_waitq_enter;
15230 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15231 			    "sd_retry_command: immed. retry hit "
15232 			    "throttle!\n");
15233 		} else {
15234 			/*
15235 			 * We're clear to proceed with the immediate retry.
15236 			 * First call the user-provided function (if any)
15237 			 */
15238 			if (user_funcp != NULL) {
15239 				(*user_funcp)(un, bp, user_arg,
15240 				    SD_IMMEDIATE_RETRY_ISSUED);
15241 #ifdef __lock_lint
15242 				sd_print_incomplete_msg(un, bp, user_arg,
15243 				    SD_IMMEDIATE_RETRY_ISSUED);
15244 				sd_print_cmd_incomplete_msg(un, bp, user_arg,
15245 				    SD_IMMEDIATE_RETRY_ISSUED);
15246 				sd_print_sense_failed_msg(un, bp, user_arg,
15247 				    SD_IMMEDIATE_RETRY_ISSUED);
15248 #endif
15249 			}
15250 
15251 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15252 			    "sd_retry_command: issuing immediate retry\n");
15253 
15254 			/*
15255 			 * Call sd_start_cmds() to transport the command to
15256 			 * the target.
15257 			 */
15258 			sd_start_cmds(un, bp);
15259 
15260 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15261 			    "sd_retry_command exit\n");
15262 			return;
15263 		}
15264 	}
15265 
15266 	/*
15267 	 * Set up to retry the command after a delay.
15268 	 * First call the user-provided function (if any)
15269 	 */
15270 	if (user_funcp != NULL) {
15271 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
15272 	}
15273 
15274 	sd_set_retry_bp(un, bp, retry_delay, statp);
15275 
15276 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15277 	return;
15278 
15279 fail_command:
15280 
15281 	if (user_funcp != NULL) {
15282 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
15283 	}
15284 
15285 fail_command_no_log:
15286 
15287 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15288 	    "sd_retry_command: returning failed command\n");
15289 
15290 	sd_return_failed_command(un, bp, failure_code);
15291 
15292 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
15293 }
15294 
15295 
15296 /*
15297  *    Function: sd_set_retry_bp
15298  *
15299  * Description: Set up the given bp for retry.
15300  *
15301  *   Arguments: un - ptr to associated softstate
15302  *		bp - ptr to buf(9S) for the command
15303  *		retry_delay - time interval before issuing retry (may be 0)
15304  *		statp - optional pointer to kstat function
15305  *
15306  *     Context: May be called under interrupt context
15307  */
15308 
15309 static void
15310 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
15311 	void (*statp)(kstat_io_t *))
15312 {
15313 	ASSERT(un != NULL);
15314 	ASSERT(mutex_owned(SD_MUTEX(un)));
15315 	ASSERT(bp != NULL);
15316 
15317 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15318 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
15319 
15320 	/*
15321 	 * Indicate that the command is being retried. This will not allow any
15322 	 * other commands on the wait queue to be transported to the target
15323 	 * until this command has been completed (success or failure). The
15324 	 * "retry command" is not transported to the target until the given
15325 	 * time delay expires, unless the user specified a 0 retry_delay.
15326 	 *
15327 	 * Note: the timeout(9F) callback routine is what actually calls
15328 	 * sd_start_cmds() to transport the command, with the exception of a
15329 	 * zero retry_delay. The only current implementor of a zero retry delay
15330 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
15331 	 */
15332 	if (un->un_retry_bp == NULL) {
15333 		ASSERT(un->un_retry_statp == NULL);
15334 		un->un_retry_bp = bp;
15335 
15336 		/*
15337 		 * If the user has not specified a delay the command should
15338 		 * be queued and no timeout should be scheduled.
15339 		 */
15340 		if (retry_delay == 0) {
15341 			/*
15342 			 * Save the kstat pointer that will be used in the
15343 			 * call to SD_UPDATE_KSTATS() below, so that
15344 			 * sd_start_cmds() can correctly decrement the waitq
15345 			 * count when it is time to transport this command.
15346 			 */
15347 			un->un_retry_statp = statp;
15348 			goto done;
15349 		}
15350 	}
15351 
15352 	if (un->un_retry_bp == bp) {
15353 		/*
15354 		 * Save the kstat pointer that will be used in the call to
15355 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
15356 		 * correctly decrement the waitq count when it is time to
15357 		 * transport this command.
15358 		 */
15359 		un->un_retry_statp = statp;
15360 
15361 		/*
15362 		 * Schedule a timeout if:
15363 		 *   1) The user has specified a delay.
15364 		 *   2) There is not a START_STOP_UNIT callback pending.
15365 		 *
15366 		 * If no delay has been specified, then it is up to the caller
15367 		 * to ensure that IO processing continues without stalling.
15368 		 * Effectively, this means that the caller will issue the
15369 		 * required call to sd_start_cmds(). The START_STOP_UNIT
15370 		 * callback does this after the START STOP UNIT command has
15371 		 * completed. In either of these cases we should not schedule
15372 		 * a timeout callback here.  Also don't schedule the timeout if
15373 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
15374 		 */
15375 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
15376 		    (un->un_direct_priority_timeid == NULL)) {
15377 			un->un_retry_timeid =
15378 			    timeout(sd_start_retry_command, un, retry_delay);
15379 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15380 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
15381 			    " bp:0x%p un_retry_timeid:0x%p\n",
15382 			    un, bp, un->un_retry_timeid);
15383 		}
15384 	} else {
15385 		/*
15386 		 * We only get in here if there is already another command
15387 		 * waiting to be retried.  In this case, we just put the
15388 		 * given command onto the wait queue, so it can be transported
15389 		 * after the current retry command has completed.
15390 		 *
15391 		 * Also we have to make sure that if the command at the head
15392 		 * of the wait queue is the un_failfast_bp, that we do not
15393 		 * put ahead of it any other commands that are to be retried.
15394 		 */
15395 		if ((un->un_failfast_bp != NULL) &&
15396 		    (un->un_failfast_bp == un->un_waitq_headp)) {
15397 			/*
15398 			 * Enqueue this command AFTER the first command on
15399 			 * the wait queue (which is also un_failfast_bp).
15400 			 */
15401 			bp->av_forw = un->un_waitq_headp->av_forw;
15402 			un->un_waitq_headp->av_forw = bp;
15403 			if (un->un_waitq_headp == un->un_waitq_tailp) {
15404 				un->un_waitq_tailp = bp;
15405 			}
15406 		} else {
15407 			/* Enqueue this command at the head of the waitq. */
15408 			bp->av_forw = un->un_waitq_headp;
15409 			un->un_waitq_headp = bp;
15410 			if (un->un_waitq_tailp == NULL) {
15411 				un->un_waitq_tailp = bp;
15412 			}
15413 		}
15414 
15415 		if (statp == NULL) {
15416 			statp = kstat_waitq_enter;
15417 		}
15418 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15419 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
15420 	}
15421 
15422 done:
15423 	if (statp != NULL) {
15424 		SD_UPDATE_KSTATS(un, statp, bp);
15425 	}
15426 
15427 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15428 	    "sd_set_retry_bp: exit un:0x%p\n", un);
15429 }
15430 
15431 
15432 /*
15433  *    Function: sd_start_retry_command
15434  *
15435  * Description: Start the command that has been waiting on the target's
15436  *		retry queue.  Called from timeout(9F) context after the
15437  *		retry delay interval has expired.
15438  *
15439  *   Arguments: arg - pointer to associated softstate for the device.
15440  *
15441  *     Context: timeout(9F) thread context.  May not sleep.
15442  */
15443 
15444 static void
15445 sd_start_retry_command(void *arg)
15446 {
15447 	struct sd_lun *un = arg;
15448 
15449 	ASSERT(un != NULL);
15450 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15451 
15452 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15453 	    "sd_start_retry_command: entry\n");
15454 
15455 	mutex_enter(SD_MUTEX(un));
15456 
15457 	un->un_retry_timeid = NULL;
15458 
15459 	if (un->un_retry_bp != NULL) {
15460 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15461 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
15462 		    un, un->un_retry_bp);
15463 		sd_start_cmds(un, un->un_retry_bp);
15464 	}
15465 
15466 	mutex_exit(SD_MUTEX(un));
15467 
15468 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15469 	    "sd_start_retry_command: exit\n");
15470 }
15471 
15472 
15473 /*
15474  *    Function: sd_start_direct_priority_command
15475  *
15476  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
15477  *		received TRAN_BUSY when we called scsi_transport() to send it
15478  *		to the underlying HBA. This function is called from timeout(9F)
15479  *		context after the delay interval has expired.
15480  *
15481  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
15482  *
15483  *     Context: timeout(9F) thread context.  May not sleep.
15484  */
15485 
15486 static void
15487 sd_start_direct_priority_command(void *arg)
15488 {
15489 	struct buf	*priority_bp = arg;
15490 	struct sd_lun	*un;
15491 
15492 	ASSERT(priority_bp != NULL);
15493 	un = SD_GET_UN(priority_bp);
15494 	ASSERT(un != NULL);
15495 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15496 
15497 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15498 	    "sd_start_direct_priority_command: entry\n");
15499 
15500 	mutex_enter(SD_MUTEX(un));
15501 	un->un_direct_priority_timeid = NULL;
15502 	sd_start_cmds(un, priority_bp);
15503 	mutex_exit(SD_MUTEX(un));
15504 
15505 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15506 	    "sd_start_direct_priority_command: exit\n");
15507 }
15508 
15509 
15510 /*
15511  *    Function: sd_send_request_sense_command
15512  *
15513  * Description: Sends a REQUEST SENSE command to the target
15514  *
15515  *     Context: May be called from interrupt context.
15516  */
15517 
15518 static void
15519 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
15520 	struct scsi_pkt *pktp)
15521 {
15522 	ASSERT(bp != NULL);
15523 	ASSERT(un != NULL);
15524 	ASSERT(mutex_owned(SD_MUTEX(un)));
15525 
15526 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
15527 	    "entry: buf:0x%p\n", bp);
15528 
15529 	/*
15530 	 * If we are syncing or dumping, then fail the command to avoid a
15531 	 * recursive callback into scsi_transport(). Also fail the command
15532 	 * if we are suspended (legacy behavior).
15533 	 */
15534 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
15535 	    (un->un_state == SD_STATE_DUMPING)) {
15536 		sd_return_failed_command(un, bp, EIO);
15537 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15538 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
15539 		return;
15540 	}
15541 
15542 	/*
15543 	 * Retry the failed command and don't issue the request sense if:
15544 	 *    1) the sense buf is busy
15545 	 *    2) we have 1 or more outstanding commands on the target
15546 	 *    (the sense data will be cleared or invalidated any way)
15547 	 *
15548 	 * Note: There could be an issue with not checking a retry limit here,
15549 	 * the problem is determining which retry limit to check.
15550 	 */
15551 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
15552 		/* Don't retry if the command is flagged as non-retryable */
15553 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
15554 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15555 			    NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter);
15556 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15557 			    "sd_send_request_sense_command: "
15558 			    "at full throttle, retrying exit\n");
15559 		} else {
15560 			sd_return_failed_command(un, bp, EIO);
15561 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15562 			    "sd_send_request_sense_command: "
15563 			    "at full throttle, non-retryable exit\n");
15564 		}
15565 		return;
15566 	}
15567 
15568 	sd_mark_rqs_busy(un, bp);
15569 	sd_start_cmds(un, un->un_rqs_bp);
15570 
15571 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15572 	    "sd_send_request_sense_command: exit\n");
15573 }
15574 
15575 
15576 /*
15577  *    Function: sd_mark_rqs_busy
15578  *
15579  * Description: Indicate that the request sense bp for this instance is
15580  *		in use.
15581  *
15582  *     Context: May be called under interrupt context
15583  */
15584 
15585 static void
15586 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
15587 {
15588 	struct sd_xbuf	*sense_xp;
15589 
15590 	ASSERT(un != NULL);
15591 	ASSERT(bp != NULL);
15592 	ASSERT(mutex_owned(SD_MUTEX(un)));
15593 	ASSERT(un->un_sense_isbusy == 0);
15594 
15595 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
15596 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
15597 
15598 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
15599 	ASSERT(sense_xp != NULL);
15600 
15601 	SD_INFO(SD_LOG_IO, un,
15602 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
15603 
15604 	ASSERT(sense_xp->xb_pktp != NULL);
15605 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
15606 	    == (FLAG_SENSING | FLAG_HEAD));
15607 
15608 	un->un_sense_isbusy = 1;
15609 	un->un_rqs_bp->b_resid = 0;
15610 	sense_xp->xb_pktp->pkt_resid  = 0;
15611 	sense_xp->xb_pktp->pkt_reason = 0;
15612 
15613 	/* So we can get back the bp at interrupt time! */
15614 	sense_xp->xb_sense_bp = bp;
15615 
15616 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
15617 
15618 	/*
15619 	 * Mark this buf as awaiting sense data. (This is already set in
15620 	 * the pkt_flags for the RQS packet.)
15621 	 */
15622 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
15623 
15624 	sense_xp->xb_retry_count	= 0;
15625 	sense_xp->xb_victim_retry_count = 0;
15626 	sense_xp->xb_ua_retry_count	= 0;
15627 	sense_xp->xb_dma_resid  = 0;
15628 
15629 	/* Clean up the fields for auto-request sense */
15630 	sense_xp->xb_sense_status = 0;
15631 	sense_xp->xb_sense_state  = 0;
15632 	sense_xp->xb_sense_resid  = 0;
15633 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
15634 
15635 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
15636 }
15637 
15638 
15639 /*
15640  *    Function: sd_mark_rqs_idle
15641  *
15642  * Description: SD_MUTEX must be held continuously through this routine
15643  *		to prevent reuse of the rqs struct before the caller can
15644  *		complete it's processing.
15645  *
15646  * Return Code: Pointer to the RQS buf
15647  *
15648  *     Context: May be called under interrupt context
15649  */
15650 
15651 static struct buf *
15652 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
15653 {
15654 	struct buf *bp;
15655 	ASSERT(un != NULL);
15656 	ASSERT(sense_xp != NULL);
15657 	ASSERT(mutex_owned(SD_MUTEX(un)));
15658 	ASSERT(un->un_sense_isbusy != 0);
15659 
15660 	un->un_sense_isbusy = 0;
15661 	bp = sense_xp->xb_sense_bp;
15662 	sense_xp->xb_sense_bp = NULL;
15663 
15664 	/* This pkt is no longer interested in getting sense data */
15665 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
15666 
15667 	return (bp);
15668 }
15669 
15670 
15671 
15672 /*
15673  *    Function: sd_alloc_rqs
15674  *
15675  * Description: Set up the unit to receive auto request sense data
15676  *
15677  * Return Code: DDI_SUCCESS or DDI_FAILURE
15678  *
15679  *     Context: Called under attach(9E) context
15680  */
15681 
15682 static int
15683 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
15684 {
15685 	struct sd_xbuf *xp;
15686 
15687 	ASSERT(un != NULL);
15688 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15689 	ASSERT(un->un_rqs_bp == NULL);
15690 	ASSERT(un->un_rqs_pktp == NULL);
15691 
15692 	/*
15693 	 * First allocate the required buf and scsi_pkt structs, then set up
15694 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
15695 	 */
15696 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
15697 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
15698 	if (un->un_rqs_bp == NULL) {
15699 		return (DDI_FAILURE);
15700 	}
15701 
15702 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
15703 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
15704 
15705 	if (un->un_rqs_pktp == NULL) {
15706 		sd_free_rqs(un);
15707 		return (DDI_FAILURE);
15708 	}
15709 
15710 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
15711 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
15712 	    SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
15713 
15714 	SD_FILL_SCSI1_LUN(un, un->un_rqs_pktp);
15715 
15716 	/* Set up the other needed members in the ARQ scsi_pkt. */
15717 	un->un_rqs_pktp->pkt_comp   = sdintr;
15718 	un->un_rqs_pktp->pkt_time   = sd_io_time;
15719 	un->un_rqs_pktp->pkt_flags |=
15720 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
15721 
15722 	/*
15723 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
15724 	 * provide any intpkt, destroypkt routines as we take care of
15725 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
15726 	 */
15727 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
15728 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
15729 	xp->xb_pktp = un->un_rqs_pktp;
15730 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
15731 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
15732 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
15733 
15734 	/*
15735 	 * Save the pointer to the request sense private bp so it can
15736 	 * be retrieved in sdintr.
15737 	 */
15738 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
15739 	ASSERT(un->un_rqs_bp->b_private == xp);
15740 
15741 	/*
15742 	 * See if the HBA supports auto-request sense for the specified
15743 	 * target/lun. If it does, then try to enable it (if not already
15744 	 * enabled).
15745 	 *
15746 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
15747 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
15748 	 * return success.  However, in both of these cases ARQ is always
15749 	 * enabled and scsi_ifgetcap will always return true. The best approach
15750 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
15751 	 *
15752 	 * The 3rd case is the HBA (adp) always return enabled on
15753 	 * scsi_ifgetgetcap even when it's not enable, the best approach
15754 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
15755 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
15756 	 */
15757 
15758 	if (un->un_f_is_fibre == TRUE) {
15759 		un->un_f_arq_enabled = TRUE;
15760 	} else {
15761 #if defined(__i386) || defined(__amd64)
15762 		/*
15763 		 * Circumvent the Adaptec bug, remove this code when
15764 		 * the bug is fixed
15765 		 */
15766 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
15767 #endif
15768 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
15769 		case 0:
15770 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15771 				"sd_alloc_rqs: HBA supports ARQ\n");
15772 			/*
15773 			 * ARQ is supported by this HBA but currently is not
15774 			 * enabled. Attempt to enable it and if successful then
15775 			 * mark this instance as ARQ enabled.
15776 			 */
15777 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
15778 				== 1) {
15779 				/* Successfully enabled ARQ in the HBA */
15780 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
15781 					"sd_alloc_rqs: ARQ enabled\n");
15782 				un->un_f_arq_enabled = TRUE;
15783 			} else {
15784 				/* Could not enable ARQ in the HBA */
15785 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
15786 				"sd_alloc_rqs: failed ARQ enable\n");
15787 				un->un_f_arq_enabled = FALSE;
15788 			}
15789 			break;
15790 		case 1:
15791 			/*
15792 			 * ARQ is supported by this HBA and is already enabled.
15793 			 * Just mark ARQ as enabled for this instance.
15794 			 */
15795 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15796 				"sd_alloc_rqs: ARQ already enabled\n");
15797 			un->un_f_arq_enabled = TRUE;
15798 			break;
15799 		default:
15800 			/*
15801 			 * ARQ is not supported by this HBA; disable it for this
15802 			 * instance.
15803 			 */
15804 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15805 				"sd_alloc_rqs: HBA does not support ARQ\n");
15806 			un->un_f_arq_enabled = FALSE;
15807 			break;
15808 		}
15809 	}
15810 
15811 	return (DDI_SUCCESS);
15812 }
15813 
15814 
15815 /*
15816  *    Function: sd_free_rqs
15817  *
15818  * Description: Cleanup for the pre-instance RQS command.
15819  *
15820  *     Context: Kernel thread context
15821  */
15822 
15823 static void
15824 sd_free_rqs(struct sd_lun *un)
15825 {
15826 	ASSERT(un != NULL);
15827 
15828 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
15829 
15830 	/*
15831 	 * If consistent memory is bound to a scsi_pkt, the pkt
15832 	 * has to be destroyed *before* freeing the consistent memory.
15833 	 * Don't change the sequence of this operations.
15834 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
15835 	 * after it was freed in scsi_free_consistent_buf().
15836 	 */
15837 	if (un->un_rqs_pktp != NULL) {
15838 		scsi_destroy_pkt(un->un_rqs_pktp);
15839 		un->un_rqs_pktp = NULL;
15840 	}
15841 
15842 	if (un->un_rqs_bp != NULL) {
15843 		kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf));
15844 		scsi_free_consistent_buf(un->un_rqs_bp);
15845 		un->un_rqs_bp = NULL;
15846 	}
15847 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
15848 }
15849 
15850 
15851 
15852 /*
15853  *    Function: sd_reduce_throttle
15854  *
15855  * Description: Reduces the maximun # of outstanding commands on a
15856  *		target to the current number of outstanding commands.
15857  *		Queues a tiemout(9F) callback to restore the limit
15858  *		after a specified interval has elapsed.
15859  *		Typically used when we get a TRAN_BUSY return code
15860  *		back from scsi_transport().
15861  *
15862  *   Arguments: un - ptr to the sd_lun softstate struct
15863  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
15864  *
15865  *     Context: May be called from interrupt context
15866  */
15867 
15868 static void
15869 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
15870 {
15871 	ASSERT(un != NULL);
15872 	ASSERT(mutex_owned(SD_MUTEX(un)));
15873 	ASSERT(un->un_ncmds_in_transport >= 0);
15874 
15875 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
15876 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
15877 	    un, un->un_throttle, un->un_ncmds_in_transport);
15878 
15879 	if (un->un_throttle > 1) {
15880 		if (un->un_f_use_adaptive_throttle == TRUE) {
15881 			switch (throttle_type) {
15882 			case SD_THROTTLE_TRAN_BUSY:
15883 				if (un->un_busy_throttle == 0) {
15884 					un->un_busy_throttle = un->un_throttle;
15885 				}
15886 				break;
15887 			case SD_THROTTLE_QFULL:
15888 				un->un_busy_throttle = 0;
15889 				break;
15890 			default:
15891 				ASSERT(FALSE);
15892 			}
15893 
15894 			if (un->un_ncmds_in_transport > 0) {
15895 			    un->un_throttle = un->un_ncmds_in_transport;
15896 			}
15897 
15898 		} else {
15899 			if (un->un_ncmds_in_transport == 0) {
15900 				un->un_throttle = 1;
15901 			} else {
15902 				un->un_throttle = un->un_ncmds_in_transport;
15903 			}
15904 		}
15905 	}
15906 
15907 	/* Reschedule the timeout if none is currently active */
15908 	if (un->un_reset_throttle_timeid == NULL) {
15909 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
15910 		    un, SD_THROTTLE_RESET_INTERVAL);
15911 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15912 		    "sd_reduce_throttle: timeout scheduled!\n");
15913 	}
15914 
15915 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
15916 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
15917 }
15918 
15919 
15920 
15921 /*
15922  *    Function: sd_restore_throttle
15923  *
15924  * Description: Callback function for timeout(9F).  Resets the current
15925  *		value of un->un_throttle to its default.
15926  *
15927  *   Arguments: arg - pointer to associated softstate for the device.
15928  *
15929  *     Context: May be called from interrupt context
15930  */
15931 
15932 static void
15933 sd_restore_throttle(void *arg)
15934 {
15935 	struct sd_lun	*un = arg;
15936 
15937 	ASSERT(un != NULL);
15938 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15939 
15940 	mutex_enter(SD_MUTEX(un));
15941 
15942 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
15943 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
15944 
15945 	un->un_reset_throttle_timeid = NULL;
15946 
15947 	if (un->un_f_use_adaptive_throttle == TRUE) {
15948 		/*
15949 		 * If un_busy_throttle is nonzero, then it contains the
15950 		 * value that un_throttle was when we got a TRAN_BUSY back
15951 		 * from scsi_transport(). We want to revert back to this
15952 		 * value.
15953 		 *
15954 		 * In the QFULL case, the throttle limit will incrementally
15955 		 * increase until it reaches max throttle.
15956 		 */
15957 		if (un->un_busy_throttle > 0) {
15958 			un->un_throttle = un->un_busy_throttle;
15959 			un->un_busy_throttle = 0;
15960 		} else {
15961 			/*
15962 			 * increase throttle by 10% open gate slowly, schedule
15963 			 * another restore if saved throttle has not been
15964 			 * reached
15965 			 */
15966 			short throttle;
15967 			if (sd_qfull_throttle_enable) {
15968 				throttle = un->un_throttle +
15969 				    max((un->un_throttle / 10), 1);
15970 				un->un_throttle =
15971 				    (throttle < un->un_saved_throttle) ?
15972 				    throttle : un->un_saved_throttle;
15973 				if (un->un_throttle < un->un_saved_throttle) {
15974 				    un->un_reset_throttle_timeid =
15975 					timeout(sd_restore_throttle,
15976 					un, SD_QFULL_THROTTLE_RESET_INTERVAL);
15977 				}
15978 			}
15979 		}
15980 
15981 		/*
15982 		 * If un_throttle has fallen below the low-water mark, we
15983 		 * restore the maximum value here (and allow it to ratchet
15984 		 * down again if necessary).
15985 		 */
15986 		if (un->un_throttle < un->un_min_throttle) {
15987 			un->un_throttle = un->un_saved_throttle;
15988 		}
15989 	} else {
15990 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
15991 		    "restoring limit from 0x%x to 0x%x\n",
15992 		    un->un_throttle, un->un_saved_throttle);
15993 		un->un_throttle = un->un_saved_throttle;
15994 	}
15995 
15996 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15997 	    "sd_restore_throttle: calling sd_start_cmds!\n");
15998 
15999 	sd_start_cmds(un, NULL);
16000 
16001 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
16002 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
16003 	    un, un->un_throttle);
16004 
16005 	mutex_exit(SD_MUTEX(un));
16006 
16007 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
16008 }
16009 
16010 /*
16011  *    Function: sdrunout
16012  *
16013  * Description: Callback routine for scsi_init_pkt when a resource allocation
16014  *		fails.
16015  *
16016  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
16017  *		soft state instance.
16018  *
16019  * Return Code: The scsi_init_pkt routine allows for the callback function to
16020  *		return a 0 indicating the callback should be rescheduled or a 1
16021  *		indicating not to reschedule. This routine always returns 1
16022  *		because the driver always provides a callback function to
16023  *		scsi_init_pkt. This results in a callback always being scheduled
16024  *		(via the scsi_init_pkt callback implementation) if a resource
16025  *		failure occurs.
16026  *
16027  *     Context: This callback function may not block or call routines that block
16028  *
16029  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
16030  *		request persisting at the head of the list which cannot be
16031  *		satisfied even after multiple retries. In the future the driver
16032  *		may implement some time of maximum runout count before failing
16033  *		an I/O.
16034  */
16035 
16036 static int
16037 sdrunout(caddr_t arg)
16038 {
16039 	struct sd_lun	*un = (struct sd_lun *)arg;
16040 
16041 	ASSERT(un != NULL);
16042 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16043 
16044 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
16045 
16046 	mutex_enter(SD_MUTEX(un));
16047 	sd_start_cmds(un, NULL);
16048 	mutex_exit(SD_MUTEX(un));
16049 	/*
16050 	 * This callback routine always returns 1 (i.e. do not reschedule)
16051 	 * because we always specify sdrunout as the callback handler for
16052 	 * scsi_init_pkt inside the call to sd_start_cmds.
16053 	 */
16054 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
16055 	return (1);
16056 }
16057 
16058 
16059 /*
16060  *    Function: sdintr
16061  *
16062  * Description: Completion callback routine for scsi_pkt(9S) structs
16063  *		sent to the HBA driver via scsi_transport(9F).
16064  *
16065  *     Context: Interrupt context
16066  */
16067 
16068 static void
16069 sdintr(struct scsi_pkt *pktp)
16070 {
16071 	struct buf	*bp;
16072 	struct sd_xbuf	*xp;
16073 	struct sd_lun	*un;
16074 
16075 	ASSERT(pktp != NULL);
16076 	bp = (struct buf *)pktp->pkt_private;
16077 	ASSERT(bp != NULL);
16078 	xp = SD_GET_XBUF(bp);
16079 	ASSERT(xp != NULL);
16080 	ASSERT(xp->xb_pktp != NULL);
16081 	un = SD_GET_UN(bp);
16082 	ASSERT(un != NULL);
16083 	ASSERT(!mutex_owned(SD_MUTEX(un)));
16084 
16085 #ifdef SD_FAULT_INJECTION
16086 
16087 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
16088 	/* SD FaultInjection */
16089 	sd_faultinjection(pktp);
16090 
16091 #endif /* SD_FAULT_INJECTION */
16092 
16093 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
16094 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
16095 
16096 	mutex_enter(SD_MUTEX(un));
16097 
16098 	/* Reduce the count of the #commands currently in transport */
16099 	un->un_ncmds_in_transport--;
16100 	ASSERT(un->un_ncmds_in_transport >= 0);
16101 
16102 	/* Increment counter to indicate that the callback routine is active */
16103 	un->un_in_callback++;
16104 
16105 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
16106 
16107 #ifdef	SDDEBUG
16108 	if (bp == un->un_retry_bp) {
16109 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
16110 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
16111 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
16112 	}
16113 #endif
16114 
16115 	/*
16116 	 * If pkt_reason is CMD_DEV_GONE, just fail the command
16117 	 */
16118 	if (pktp->pkt_reason == CMD_DEV_GONE) {
16119 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16120 			    "Device is gone\n");
16121 		sd_return_failed_command(un, bp, EIO);
16122 		goto exit;
16123 	}
16124 
16125 	/*
16126 	 * First see if the pkt has auto-request sense data with it....
16127 	 * Look at the packet state first so we don't take a performance
16128 	 * hit looking at the arq enabled flag unless absolutely necessary.
16129 	 */
16130 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
16131 	    (un->un_f_arq_enabled == TRUE)) {
16132 		/*
16133 		 * The HBA did an auto request sense for this command so check
16134 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16135 		 * driver command that should not be retried.
16136 		 */
16137 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16138 			/*
16139 			 * Save the relevant sense info into the xp for the
16140 			 * original cmd.
16141 			 */
16142 			struct scsi_arq_status *asp;
16143 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16144 			xp->xb_sense_status =
16145 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
16146 			xp->xb_sense_state  = asp->sts_rqpkt_state;
16147 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16148 			bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16149 			    min(sizeof (struct scsi_extended_sense),
16150 			    SENSE_LENGTH));
16151 
16152 			/* fail the command */
16153 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16154 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
16155 			sd_return_failed_command(un, bp, EIO);
16156 			goto exit;
16157 		}
16158 
16159 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16160 		/*
16161 		 * We want to either retry or fail this command, so free
16162 		 * the DMA resources here.  If we retry the command then
16163 		 * the DMA resources will be reallocated in sd_start_cmds().
16164 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
16165 		 * causes the *entire* transfer to start over again from the
16166 		 * beginning of the request, even for PARTIAL chunks that
16167 		 * have already transferred successfully.
16168 		 */
16169 		if ((un->un_f_is_fibre == TRUE) &&
16170 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16171 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16172 			scsi_dmafree(pktp);
16173 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16174 		}
16175 #endif
16176 
16177 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16178 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
16179 
16180 		sd_handle_auto_request_sense(un, bp, xp, pktp);
16181 		goto exit;
16182 	}
16183 
16184 	/* Next see if this is the REQUEST SENSE pkt for the instance */
16185 	if (pktp->pkt_flags & FLAG_SENSING)  {
16186 		/* This pktp is from the unit's REQUEST_SENSE command */
16187 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16188 		    "sdintr: sd_handle_request_sense\n");
16189 		sd_handle_request_sense(un, bp, xp, pktp);
16190 		goto exit;
16191 	}
16192 
16193 	/*
16194 	 * Check to see if the command successfully completed as requested;
16195 	 * this is the most common case (and also the hot performance path).
16196 	 *
16197 	 * Requirements for successful completion are:
16198 	 * pkt_reason is CMD_CMPLT and packet status is status good.
16199 	 * In addition:
16200 	 * - A residual of zero indicates successful completion no matter what
16201 	 *   the command is.
16202 	 * - If the residual is not zero and the command is not a read or
16203 	 *   write, then it's still defined as successful completion. In other
16204 	 *   words, if the command is a read or write the residual must be
16205 	 *   zero for successful completion.
16206 	 * - If the residual is not zero and the command is a read or
16207 	 *   write, and it's a USCSICMD, then it's still defined as
16208 	 *   successful completion.
16209 	 */
16210 	if ((pktp->pkt_reason == CMD_CMPLT) &&
16211 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
16212 
16213 		/*
16214 		 * Since this command is returned with a good status, we
16215 		 * can reset the count for Sonoma failover.
16216 		 */
16217 		un->un_sonoma_failure_count = 0;
16218 
16219 		/*
16220 		 * Return all USCSI commands on good status
16221 		 */
16222 		if (pktp->pkt_resid == 0) {
16223 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16224 			    "sdintr: returning command for resid == 0\n");
16225 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
16226 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
16227 			SD_UPDATE_B_RESID(bp, pktp);
16228 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16229 			    "sdintr: returning command for resid != 0\n");
16230 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
16231 			SD_UPDATE_B_RESID(bp, pktp);
16232 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16233 				"sdintr: returning uscsi command\n");
16234 		} else {
16235 			goto not_successful;
16236 		}
16237 		sd_return_command(un, bp);
16238 
16239 		/*
16240 		 * Decrement counter to indicate that the callback routine
16241 		 * is done.
16242 		 */
16243 		un->un_in_callback--;
16244 		ASSERT(un->un_in_callback >= 0);
16245 		mutex_exit(SD_MUTEX(un));
16246 
16247 		return;
16248 	}
16249 
16250 not_successful:
16251 
16252 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
16253 	/*
16254 	 * The following is based upon knowledge of the underlying transport
16255 	 * and its use of DMA resources.  This code should be removed when
16256 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
16257 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
16258 	 * and sd_start_cmds().
16259 	 *
16260 	 * Free any DMA resources associated with this command if there
16261 	 * is a chance it could be retried or enqueued for later retry.
16262 	 * If we keep the DMA binding then mpxio cannot reissue the
16263 	 * command on another path whenever a path failure occurs.
16264 	 *
16265 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
16266 	 * causes the *entire* transfer to start over again from the
16267 	 * beginning of the request, even for PARTIAL chunks that
16268 	 * have already transferred successfully.
16269 	 *
16270 	 * This is only done for non-uscsi commands (and also skipped for the
16271 	 * driver's internal RQS command). Also just do this for Fibre Channel
16272 	 * devices as these are the only ones that support mpxio.
16273 	 */
16274 	if ((un->un_f_is_fibre == TRUE) &&
16275 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
16276 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
16277 		scsi_dmafree(pktp);
16278 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
16279 	}
16280 #endif
16281 
16282 	/*
16283 	 * The command did not successfully complete as requested so check
16284 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
16285 	 * driver command that should not be retried so just return. If
16286 	 * FLAG_DIAGNOSE is not set the error will be processed below.
16287 	 */
16288 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
16289 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16290 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
16291 		/*
16292 		 * Issue a request sense if a check condition caused the error
16293 		 * (we handle the auto request sense case above), otherwise
16294 		 * just fail the command.
16295 		 */
16296 		if ((pktp->pkt_reason == CMD_CMPLT) &&
16297 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
16298 			sd_send_request_sense_command(un, bp, pktp);
16299 		} else {
16300 			sd_return_failed_command(un, bp, EIO);
16301 		}
16302 		goto exit;
16303 	}
16304 
16305 	/*
16306 	 * The command did not successfully complete as requested so process
16307 	 * the error, retry, and/or attempt recovery.
16308 	 */
16309 	switch (pktp->pkt_reason) {
16310 	case CMD_CMPLT:
16311 		switch (SD_GET_PKT_STATUS(pktp)) {
16312 		case STATUS_GOOD:
16313 			/*
16314 			 * The command completed successfully with a non-zero
16315 			 * residual
16316 			 */
16317 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16318 			    "sdintr: STATUS_GOOD \n");
16319 			sd_pkt_status_good(un, bp, xp, pktp);
16320 			break;
16321 
16322 		case STATUS_CHECK:
16323 		case STATUS_TERMINATED:
16324 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16325 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
16326 			sd_pkt_status_check_condition(un, bp, xp, pktp);
16327 			break;
16328 
16329 		case STATUS_BUSY:
16330 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16331 			    "sdintr: STATUS_BUSY\n");
16332 			sd_pkt_status_busy(un, bp, xp, pktp);
16333 			break;
16334 
16335 		case STATUS_RESERVATION_CONFLICT:
16336 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16337 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
16338 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16339 			break;
16340 
16341 		case STATUS_QFULL:
16342 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16343 			    "sdintr: STATUS_QFULL\n");
16344 			sd_pkt_status_qfull(un, bp, xp, pktp);
16345 			break;
16346 
16347 		case STATUS_MET:
16348 		case STATUS_INTERMEDIATE:
16349 		case STATUS_SCSI2:
16350 		case STATUS_INTERMEDIATE_MET:
16351 		case STATUS_ACA_ACTIVE:
16352 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16353 			    "Unexpected SCSI status received: 0x%x\n",
16354 			    SD_GET_PKT_STATUS(pktp));
16355 			sd_return_failed_command(un, bp, EIO);
16356 			break;
16357 
16358 		default:
16359 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16360 			    "Invalid SCSI status received: 0x%x\n",
16361 			    SD_GET_PKT_STATUS(pktp));
16362 			sd_return_failed_command(un, bp, EIO);
16363 			break;
16364 
16365 		}
16366 		break;
16367 
16368 	case CMD_INCOMPLETE:
16369 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16370 		    "sdintr:  CMD_INCOMPLETE\n");
16371 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
16372 		break;
16373 	case CMD_TRAN_ERR:
16374 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16375 		    "sdintr: CMD_TRAN_ERR\n");
16376 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
16377 		break;
16378 	case CMD_RESET:
16379 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16380 		    "sdintr: CMD_RESET \n");
16381 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
16382 		break;
16383 	case CMD_ABORTED:
16384 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16385 		    "sdintr: CMD_ABORTED \n");
16386 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
16387 		break;
16388 	case CMD_TIMEOUT:
16389 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16390 		    "sdintr: CMD_TIMEOUT\n");
16391 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
16392 		break;
16393 	case CMD_UNX_BUS_FREE:
16394 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16395 		    "sdintr: CMD_UNX_BUS_FREE \n");
16396 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
16397 		break;
16398 	case CMD_TAG_REJECT:
16399 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16400 		    "sdintr: CMD_TAG_REJECT\n");
16401 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
16402 		break;
16403 	default:
16404 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16405 		    "sdintr: default\n");
16406 		sd_pkt_reason_default(un, bp, xp, pktp);
16407 		break;
16408 	}
16409 
16410 exit:
16411 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
16412 
16413 	/* Decrement counter to indicate that the callback routine is done. */
16414 	un->un_in_callback--;
16415 	ASSERT(un->un_in_callback >= 0);
16416 
16417 	/*
16418 	 * At this point, the pkt has been dispatched, ie, it is either
16419 	 * being re-tried or has been returned to its caller and should
16420 	 * not be referenced.
16421 	 */
16422 
16423 	mutex_exit(SD_MUTEX(un));
16424 }
16425 
16426 
16427 /*
16428  *    Function: sd_print_incomplete_msg
16429  *
16430  * Description: Prints the error message for a CMD_INCOMPLETE error.
16431  *
16432  *   Arguments: un - ptr to associated softstate for the device.
16433  *		bp - ptr to the buf(9S) for the command.
16434  *		arg - message string ptr
16435  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
16436  *			or SD_NO_RETRY_ISSUED.
16437  *
16438  *     Context: May be called under interrupt context
16439  */
16440 
16441 static void
16442 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
16443 {
16444 	struct scsi_pkt	*pktp;
16445 	char	*msgp;
16446 	char	*cmdp = arg;
16447 
16448 	ASSERT(un != NULL);
16449 	ASSERT(mutex_owned(SD_MUTEX(un)));
16450 	ASSERT(bp != NULL);
16451 	ASSERT(arg != NULL);
16452 	pktp = SD_GET_PKTP(bp);
16453 	ASSERT(pktp != NULL);
16454 
16455 	switch (code) {
16456 	case SD_DELAYED_RETRY_ISSUED:
16457 	case SD_IMMEDIATE_RETRY_ISSUED:
16458 		msgp = "retrying";
16459 		break;
16460 	case SD_NO_RETRY_ISSUED:
16461 	default:
16462 		msgp = "giving up";
16463 		break;
16464 	}
16465 
16466 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
16467 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16468 		    "incomplete %s- %s\n", cmdp, msgp);
16469 	}
16470 }
16471 
16472 
16473 
16474 /*
16475  *    Function: sd_pkt_status_good
16476  *
16477  * Description: Processing for a STATUS_GOOD code in pkt_status.
16478  *
16479  *     Context: May be called under interrupt context
16480  */
16481 
16482 static void
16483 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
16484 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16485 {
16486 	char	*cmdp;
16487 
16488 	ASSERT(un != NULL);
16489 	ASSERT(mutex_owned(SD_MUTEX(un)));
16490 	ASSERT(bp != NULL);
16491 	ASSERT(xp != NULL);
16492 	ASSERT(pktp != NULL);
16493 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
16494 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
16495 	ASSERT(pktp->pkt_resid != 0);
16496 
16497 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
16498 
16499 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
16500 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
16501 	case SCMD_READ:
16502 		cmdp = "read";
16503 		break;
16504 	case SCMD_WRITE:
16505 		cmdp = "write";
16506 		break;
16507 	default:
16508 		SD_UPDATE_B_RESID(bp, pktp);
16509 		sd_return_command(un, bp);
16510 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16511 		return;
16512 	}
16513 
16514 	/*
16515 	 * See if we can retry the read/write, preferrably immediately.
16516 	 * If retries are exhaused, then sd_retry_command() will update
16517 	 * the b_resid count.
16518 	 */
16519 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
16520 	    cmdp, EIO, (clock_t)0, NULL);
16521 
16522 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16523 }
16524 
16525 
16526 
16527 
16528 
16529 /*
16530  *    Function: sd_handle_request_sense
16531  *
16532  * Description: Processing for non-auto Request Sense command.
16533  *
16534  *   Arguments: un - ptr to associated softstate
16535  *		sense_bp - ptr to buf(9S) for the RQS command
16536  *		sense_xp - ptr to the sd_xbuf for the RQS command
16537  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
16538  *
16539  *     Context: May be called under interrupt context
16540  */
16541 
16542 static void
16543 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
16544 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
16545 {
16546 	struct buf	*cmd_bp;	/* buf for the original command */
16547 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
16548 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
16549 
16550 	ASSERT(un != NULL);
16551 	ASSERT(mutex_owned(SD_MUTEX(un)));
16552 	ASSERT(sense_bp != NULL);
16553 	ASSERT(sense_xp != NULL);
16554 	ASSERT(sense_pktp != NULL);
16555 
16556 	/*
16557 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
16558 	 * RQS command and not the original command.
16559 	 */
16560 	ASSERT(sense_pktp == un->un_rqs_pktp);
16561 	ASSERT(sense_bp   == un->un_rqs_bp);
16562 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
16563 	    (FLAG_SENSING | FLAG_HEAD));
16564 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
16565 	    FLAG_SENSING) == FLAG_SENSING);
16566 
16567 	/* These are the bp, xp, and pktp for the original command */
16568 	cmd_bp = sense_xp->xb_sense_bp;
16569 	cmd_xp = SD_GET_XBUF(cmd_bp);
16570 	cmd_pktp = SD_GET_PKTP(cmd_bp);
16571 
16572 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
16573 		/*
16574 		 * The REQUEST SENSE command failed.  Release the REQUEST
16575 		 * SENSE command for re-use, get back the bp for the original
16576 		 * command, and attempt to re-try the original command if
16577 		 * FLAG_DIAGNOSE is not set in the original packet.
16578 		 */
16579 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
16580 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16581 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
16582 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
16583 			    NULL, NULL, EIO, (clock_t)0, NULL);
16584 			return;
16585 		}
16586 	}
16587 
16588 	/*
16589 	 * Save the relevant sense info into the xp for the original cmd.
16590 	 *
16591 	 * Note: if the request sense failed the state info will be zero
16592 	 * as set in sd_mark_rqs_busy()
16593 	 */
16594 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
16595 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
16596 	cmd_xp->xb_sense_resid  = sense_pktp->pkt_resid;
16597 	bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH);
16598 
16599 	/*
16600 	 *  Free up the RQS command....
16601 	 *  NOTE:
16602 	 *	Must do this BEFORE calling sd_validate_sense_data!
16603 	 *	sd_validate_sense_data may return the original command in
16604 	 *	which case the pkt will be freed and the flags can no
16605 	 *	longer be touched.
16606 	 *	SD_MUTEX is held through this process until the command
16607 	 *	is dispatched based upon the sense data, so there are
16608 	 *	no race conditions.
16609 	 */
16610 	(void) sd_mark_rqs_idle(un, sense_xp);
16611 
16612 	/*
16613 	 * For a retryable command see if we have valid sense data, if so then
16614 	 * turn it over to sd_decode_sense() to figure out the right course of
16615 	 * action. Just fail a non-retryable command.
16616 	 */
16617 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16618 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp) ==
16619 		    SD_SENSE_DATA_IS_VALID) {
16620 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
16621 		}
16622 	} else {
16623 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
16624 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
16625 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
16626 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
16627 		sd_return_failed_command(un, cmd_bp, EIO);
16628 	}
16629 }
16630 
16631 
16632 
16633 
16634 /*
16635  *    Function: sd_handle_auto_request_sense
16636  *
16637  * Description: Processing for auto-request sense information.
16638  *
16639  *   Arguments: un - ptr to associated softstate
16640  *		bp - ptr to buf(9S) for the command
16641  *		xp - ptr to the sd_xbuf for the command
16642  *		pktp - ptr to the scsi_pkt(9S) for the command
16643  *
16644  *     Context: May be called under interrupt context
16645  */
16646 
16647 static void
16648 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
16649 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16650 {
16651 	struct scsi_arq_status *asp;
16652 
16653 	ASSERT(un != NULL);
16654 	ASSERT(mutex_owned(SD_MUTEX(un)));
16655 	ASSERT(bp != NULL);
16656 	ASSERT(xp != NULL);
16657 	ASSERT(pktp != NULL);
16658 	ASSERT(pktp != un->un_rqs_pktp);
16659 	ASSERT(bp   != un->un_rqs_bp);
16660 
16661 	/*
16662 	 * For auto-request sense, we get a scsi_arq_status back from
16663 	 * the HBA, with the sense data in the sts_sensedata member.
16664 	 * The pkt_scbp of the packet points to this scsi_arq_status.
16665 	 */
16666 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16667 
16668 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
16669 		/*
16670 		 * The auto REQUEST SENSE failed; see if we can re-try
16671 		 * the original command.
16672 		 */
16673 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16674 		    "auto request sense failed (reason=%s)\n",
16675 		    scsi_rname(asp->sts_rqpkt_reason));
16676 
16677 		sd_reset_target(un, pktp);
16678 
16679 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16680 		    NULL, NULL, EIO, (clock_t)0, NULL);
16681 		return;
16682 	}
16683 
16684 	/* Save the relevant sense info into the xp for the original cmd. */
16685 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
16686 	xp->xb_sense_state  = asp->sts_rqpkt_state;
16687 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16688 	bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16689 	    min(sizeof (struct scsi_extended_sense), SENSE_LENGTH));
16690 
16691 	/*
16692 	 * See if we have valid sense data, if so then turn it over to
16693 	 * sd_decode_sense() to figure out the right course of action.
16694 	 */
16695 	if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) {
16696 		sd_decode_sense(un, bp, xp, pktp);
16697 	}
16698 }
16699 
16700 
16701 /*
16702  *    Function: sd_print_sense_failed_msg
16703  *
16704  * Description: Print log message when RQS has failed.
16705  *
16706  *   Arguments: un - ptr to associated softstate
16707  *		bp - ptr to buf(9S) for the command
16708  *		arg - generic message string ptr
16709  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
16710  *			or SD_NO_RETRY_ISSUED
16711  *
16712  *     Context: May be called from interrupt context
16713  */
16714 
16715 static void
16716 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
16717 	int code)
16718 {
16719 	char	*msgp = arg;
16720 
16721 	ASSERT(un != NULL);
16722 	ASSERT(mutex_owned(SD_MUTEX(un)));
16723 	ASSERT(bp != NULL);
16724 
16725 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
16726 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
16727 	}
16728 }
16729 
16730 
16731 /*
16732  *    Function: sd_validate_sense_data
16733  *
16734  * Description: Check the given sense data for validity.
16735  *		If the sense data is not valid, the command will
16736  *		be either failed or retried!
16737  *
16738  * Return Code: SD_SENSE_DATA_IS_INVALID
16739  *		SD_SENSE_DATA_IS_VALID
16740  *
16741  *     Context: May be called from interrupt context
16742  */
16743 
16744 static int
16745 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp)
16746 {
16747 	struct scsi_extended_sense *esp;
16748 	struct	scsi_pkt *pktp;
16749 	size_t	actual_len;
16750 	char	*msgp = NULL;
16751 
16752 	ASSERT(un != NULL);
16753 	ASSERT(mutex_owned(SD_MUTEX(un)));
16754 	ASSERT(bp != NULL);
16755 	ASSERT(bp != un->un_rqs_bp);
16756 	ASSERT(xp != NULL);
16757 
16758 	pktp = SD_GET_PKTP(bp);
16759 	ASSERT(pktp != NULL);
16760 
16761 	/*
16762 	 * Check the status of the RQS command (auto or manual).
16763 	 */
16764 	switch (xp->xb_sense_status & STATUS_MASK) {
16765 	case STATUS_GOOD:
16766 		break;
16767 
16768 	case STATUS_RESERVATION_CONFLICT:
16769 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16770 		return (SD_SENSE_DATA_IS_INVALID);
16771 
16772 	case STATUS_BUSY:
16773 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16774 		    "Busy Status on REQUEST SENSE\n");
16775 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
16776 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
16777 		return (SD_SENSE_DATA_IS_INVALID);
16778 
16779 	case STATUS_QFULL:
16780 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16781 		    "QFULL Status on REQUEST SENSE\n");
16782 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
16783 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
16784 		return (SD_SENSE_DATA_IS_INVALID);
16785 
16786 	case STATUS_CHECK:
16787 	case STATUS_TERMINATED:
16788 		msgp = "Check Condition on REQUEST SENSE\n";
16789 		goto sense_failed;
16790 
16791 	default:
16792 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
16793 		goto sense_failed;
16794 	}
16795 
16796 	/*
16797 	 * See if we got the minimum required amount of sense data.
16798 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
16799 	 * or less.
16800 	 */
16801 	actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid);
16802 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
16803 	    (actual_len == 0)) {
16804 		msgp = "Request Sense couldn't get sense data\n";
16805 		goto sense_failed;
16806 	}
16807 
16808 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
16809 		msgp = "Not enough sense information\n";
16810 		goto sense_failed;
16811 	}
16812 
16813 	/*
16814 	 * We require the extended sense data
16815 	 */
16816 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
16817 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
16818 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
16819 			static char tmp[8];
16820 			static char buf[148];
16821 			char *p = (char *)(xp->xb_sense_data);
16822 			int i;
16823 
16824 			mutex_enter(&sd_sense_mutex);
16825 			(void) strcpy(buf, "undecodable sense information:");
16826 			for (i = 0; i < actual_len; i++) {
16827 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
16828 				(void) strcpy(&buf[strlen(buf)], tmp);
16829 			}
16830 			i = strlen(buf);
16831 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
16832 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf);
16833 			mutex_exit(&sd_sense_mutex);
16834 		}
16835 		/* Note: Legacy behavior, fail the command with no retry */
16836 		sd_return_failed_command(un, bp, EIO);
16837 		return (SD_SENSE_DATA_IS_INVALID);
16838 	}
16839 
16840 	/*
16841 	 * Check that es_code is valid (es_class concatenated with es_code
16842 	 * make up the "response code" field.  es_class will always be 7, so
16843 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
16844 	 * format.
16845 	 */
16846 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
16847 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
16848 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
16849 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
16850 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
16851 		goto sense_failed;
16852 	}
16853 
16854 	return (SD_SENSE_DATA_IS_VALID);
16855 
16856 sense_failed:
16857 	/*
16858 	 * If the request sense failed (for whatever reason), attempt
16859 	 * to retry the original command.
16860 	 */
16861 #if defined(__i386) || defined(__amd64)
16862 	/*
16863 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
16864 	 * sddef.h for Sparc platform, and x86 uses 1 binary
16865 	 * for both SCSI/FC.
16866 	 * The SD_RETRY_DELAY value need to be adjusted here
16867 	 * when SD_RETRY_DELAY change in sddef.h
16868 	 */
16869 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16870 	    sd_print_sense_failed_msg, msgp, EIO,
16871 		un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
16872 #else
16873 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16874 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
16875 #endif
16876 
16877 	return (SD_SENSE_DATA_IS_INVALID);
16878 }
16879 
16880 
16881 
16882 /*
16883  *    Function: sd_decode_sense
16884  *
16885  * Description: Take recovery action(s) when SCSI Sense Data is received.
16886  *
16887  *     Context: Interrupt context.
16888  */
16889 
16890 static void
16891 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
16892 	struct scsi_pkt *pktp)
16893 {
16894 	struct scsi_extended_sense *esp;
16895 	struct scsi_descr_sense_hdr *sdsp;
16896 	uint8_t asc, ascq, sense_key;
16897 
16898 	ASSERT(un != NULL);
16899 	ASSERT(mutex_owned(SD_MUTEX(un)));
16900 	ASSERT(bp != NULL);
16901 	ASSERT(bp != un->un_rqs_bp);
16902 	ASSERT(xp != NULL);
16903 	ASSERT(pktp != NULL);
16904 
16905 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
16906 
16907 	switch (esp->es_code) {
16908 	case CODE_FMT_DESCR_CURRENT:
16909 	case CODE_FMT_DESCR_DEFERRED:
16910 		sdsp = (struct scsi_descr_sense_hdr *)xp->xb_sense_data;
16911 		sense_key = sdsp->ds_key;
16912 		asc = sdsp->ds_add_code;
16913 		ascq = sdsp->ds_qual_code;
16914 		break;
16915 	case CODE_FMT_VENDOR_SPECIFIC:
16916 	case CODE_FMT_FIXED_CURRENT:
16917 	case CODE_FMT_FIXED_DEFERRED:
16918 	default:
16919 		sense_key = esp->es_key;
16920 		asc = esp->es_add_code;
16921 		ascq = esp->es_qual_code;
16922 		break;
16923 	}
16924 
16925 	switch (sense_key) {
16926 	case KEY_NO_SENSE:
16927 		sd_sense_key_no_sense(un, bp, xp, pktp);
16928 		break;
16929 	case KEY_RECOVERABLE_ERROR:
16930 		sd_sense_key_recoverable_error(un, asc, bp, xp, pktp);
16931 		break;
16932 	case KEY_NOT_READY:
16933 		sd_sense_key_not_ready(un, asc, ascq, bp, xp, pktp);
16934 		break;
16935 	case KEY_MEDIUM_ERROR:
16936 	case KEY_HARDWARE_ERROR:
16937 		sd_sense_key_medium_or_hardware_error(un,
16938 		    sense_key, asc, bp, xp, pktp);
16939 		break;
16940 	case KEY_ILLEGAL_REQUEST:
16941 		sd_sense_key_illegal_request(un, bp, xp, pktp);
16942 		break;
16943 	case KEY_UNIT_ATTENTION:
16944 		sd_sense_key_unit_attention(un, asc, bp, xp, pktp);
16945 		break;
16946 	case KEY_WRITE_PROTECT:
16947 	case KEY_VOLUME_OVERFLOW:
16948 	case KEY_MISCOMPARE:
16949 		sd_sense_key_fail_command(un, bp, xp, pktp);
16950 		break;
16951 	case KEY_BLANK_CHECK:
16952 		sd_sense_key_blank_check(un, bp, xp, pktp);
16953 		break;
16954 	case KEY_ABORTED_COMMAND:
16955 		sd_sense_key_aborted_command(un, bp, xp, pktp);
16956 		break;
16957 	case KEY_VENDOR_UNIQUE:
16958 	case KEY_COPY_ABORTED:
16959 	case KEY_EQUAL:
16960 	case KEY_RESERVED:
16961 	default:
16962 		sd_sense_key_default(un, sense_key, bp, xp, pktp);
16963 		break;
16964 	}
16965 }
16966 
16967 
16968 /*
16969  *    Function: sd_dump_memory
16970  *
16971  * Description: Debug logging routine to print the contents of a user provided
16972  *		buffer. The output of the buffer is broken up into 256 byte
16973  *		segments due to a size constraint of the scsi_log.
16974  *		implementation.
16975  *
16976  *   Arguments: un - ptr to softstate
16977  *		comp - component mask
16978  *		title - "title" string to preceed data when printed
16979  *		data - ptr to data block to be printed
16980  *		len - size of data block to be printed
16981  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
16982  *
16983  *     Context: May be called from interrupt context
16984  */
16985 
16986 #define	SD_DUMP_MEMORY_BUF_SIZE	256
16987 
16988 static char *sd_dump_format_string[] = {
16989 		" 0x%02x",
16990 		" %c"
16991 };
16992 
16993 static void
16994 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
16995     int len, int fmt)
16996 {
16997 	int	i, j;
16998 	int	avail_count;
16999 	int	start_offset;
17000 	int	end_offset;
17001 	size_t	entry_len;
17002 	char	*bufp;
17003 	char	*local_buf;
17004 	char	*format_string;
17005 
17006 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
17007 
17008 	/*
17009 	 * In the debug version of the driver, this function is called from a
17010 	 * number of places which are NOPs in the release driver.
17011 	 * The debug driver therefore has additional methods of filtering
17012 	 * debug output.
17013 	 */
17014 #ifdef SDDEBUG
17015 	/*
17016 	 * In the debug version of the driver we can reduce the amount of debug
17017 	 * messages by setting sd_error_level to something other than
17018 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
17019 	 * sd_component_mask.
17020 	 */
17021 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
17022 	    (sd_error_level != SCSI_ERR_ALL)) {
17023 		return;
17024 	}
17025 	if (((sd_component_mask & comp) == 0) ||
17026 	    (sd_error_level != SCSI_ERR_ALL)) {
17027 		return;
17028 	}
17029 #else
17030 	if (sd_error_level != SCSI_ERR_ALL) {
17031 		return;
17032 	}
17033 #endif
17034 
17035 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
17036 	bufp = local_buf;
17037 	/*
17038 	 * Available length is the length of local_buf[], minus the
17039 	 * length of the title string, minus one for the ":", minus
17040 	 * one for the newline, minus one for the NULL terminator.
17041 	 * This gives the #bytes available for holding the printed
17042 	 * values from the given data buffer.
17043 	 */
17044 	if (fmt == SD_LOG_HEX) {
17045 		format_string = sd_dump_format_string[0];
17046 	} else /* SD_LOG_CHAR */ {
17047 		format_string = sd_dump_format_string[1];
17048 	}
17049 	/*
17050 	 * Available count is the number of elements from the given
17051 	 * data buffer that we can fit into the available length.
17052 	 * This is based upon the size of the format string used.
17053 	 * Make one entry and find it's size.
17054 	 */
17055 	(void) sprintf(bufp, format_string, data[0]);
17056 	entry_len = strlen(bufp);
17057 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
17058 
17059 	j = 0;
17060 	while (j < len) {
17061 		bufp = local_buf;
17062 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
17063 		start_offset = j;
17064 
17065 		end_offset = start_offset + avail_count;
17066 
17067 		(void) sprintf(bufp, "%s:", title);
17068 		bufp += strlen(bufp);
17069 		for (i = start_offset; ((i < end_offset) && (j < len));
17070 		    i++, j++) {
17071 			(void) sprintf(bufp, format_string, data[i]);
17072 			bufp += entry_len;
17073 		}
17074 		(void) sprintf(bufp, "\n");
17075 
17076 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
17077 	}
17078 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
17079 }
17080 
17081 /*
17082  *    Function: sd_print_sense_msg
17083  *
17084  * Description: Log a message based upon the given sense data.
17085  *
17086  *   Arguments: un - ptr to associated softstate
17087  *		bp - ptr to buf(9S) for the command
17088  *		arg - ptr to associate sd_sense_info struct
17089  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17090  *			or SD_NO_RETRY_ISSUED
17091  *
17092  *     Context: May be called from interrupt context
17093  */
17094 
17095 static void
17096 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
17097 {
17098 	struct sd_xbuf	*xp;
17099 	struct scsi_pkt	*pktp;
17100 	struct scsi_extended_sense *sensep;
17101 	daddr_t request_blkno;
17102 	diskaddr_t err_blkno;
17103 	int severity;
17104 	int pfa_flag;
17105 	int fixed_format = TRUE;
17106 	extern struct scsi_key_strings scsi_cmds[];
17107 
17108 	ASSERT(un != NULL);
17109 	ASSERT(mutex_owned(SD_MUTEX(un)));
17110 	ASSERT(bp != NULL);
17111 	xp = SD_GET_XBUF(bp);
17112 	ASSERT(xp != NULL);
17113 	pktp = SD_GET_PKTP(bp);
17114 	ASSERT(pktp != NULL);
17115 	ASSERT(arg != NULL);
17116 
17117 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
17118 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
17119 
17120 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
17121 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
17122 		severity = SCSI_ERR_RETRYABLE;
17123 	}
17124 
17125 	/* Use absolute block number for the request block number */
17126 	request_blkno = xp->xb_blkno;
17127 
17128 	/*
17129 	 * Now try to get the error block number from the sense data
17130 	 */
17131 	sensep = (struct scsi_extended_sense *)xp->xb_sense_data;
17132 	switch (sensep->es_code) {
17133 	case CODE_FMT_DESCR_CURRENT:
17134 	case CODE_FMT_DESCR_DEFERRED:
17135 		err_blkno =
17136 		    sd_extract_sense_info_descr(
17137 			(struct scsi_descr_sense_hdr *)sensep);
17138 		fixed_format = FALSE;
17139 		break;
17140 	case CODE_FMT_FIXED_CURRENT:
17141 	case CODE_FMT_FIXED_DEFERRED:
17142 	case CODE_FMT_VENDOR_SPECIFIC:
17143 	default:
17144 		/*
17145 		 * With the es_valid bit set, we assume that the error
17146 		 * blkno is in the sense data.  Also, if xp->xb_blkno is
17147 		 * greater than 0xffffffff then the target *should* have used
17148 		 * a descriptor sense format (or it shouldn't have set
17149 		 * the es_valid bit), and we may as well ignore the
17150 		 * 32-bit value.
17151 		 */
17152 		if ((sensep->es_valid != 0) && (xp->xb_blkno <= 0xffffffff)) {
17153 			err_blkno = (diskaddr_t)
17154 			    ((sensep->es_info_1 << 24) |
17155 			    (sensep->es_info_2 << 16) |
17156 			    (sensep->es_info_3 << 8)  |
17157 			    (sensep->es_info_4));
17158 		} else {
17159 			err_blkno = (diskaddr_t)-1;
17160 		}
17161 		break;
17162 	}
17163 
17164 	if (err_blkno == (diskaddr_t)-1) {
17165 		/*
17166 		 * Without the es_valid bit set (for fixed format) or an
17167 		 * information descriptor (for descriptor format) we cannot
17168 		 * be certain of the error blkno, so just use the
17169 		 * request_blkno.
17170 		 */
17171 		err_blkno = (diskaddr_t)request_blkno;
17172 	} else {
17173 		/*
17174 		 * We retrieved the error block number from the information
17175 		 * portion of the sense data.
17176 		 *
17177 		 * For USCSI commands we are better off using the error
17178 		 * block no. as the requested block no. (This is the best
17179 		 * we can estimate.)
17180 		 */
17181 		if ((SD_IS_BUFIO(xp) == FALSE) &&
17182 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
17183 			request_blkno = err_blkno;
17184 		}
17185 	}
17186 
17187 	/*
17188 	 * The following will log the buffer contents for the release driver
17189 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
17190 	 * level is set to verbose.
17191 	 */
17192 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
17193 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
17194 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
17195 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
17196 
17197 	if (pfa_flag == FALSE) {
17198 		/* This is normally only set for USCSI */
17199 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
17200 			return;
17201 		}
17202 
17203 		if ((SD_IS_BUFIO(xp) == TRUE) &&
17204 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
17205 		    (severity < sd_error_level))) {
17206 			return;
17207 		}
17208 	}
17209 
17210 	/*
17211 	 * If the data is fixed format then check for Sonoma Failover,
17212 	 * and keep a count of how many failed I/O's.  We should not have
17213 	 * to worry about Sonoma returning descriptor format sense data,
17214 	 * and asc/ascq are in a different location in descriptor format.
17215 	 */
17216 	if (fixed_format &&
17217 	    (SD_IS_LSI(un)) && (sensep->es_key == KEY_ILLEGAL_REQUEST) &&
17218 	    (sensep->es_add_code == 0x94) && (sensep->es_qual_code == 0x01)) {
17219 		un->un_sonoma_failure_count++;
17220 		if (un->un_sonoma_failure_count > 1) {
17221 			return;
17222 		}
17223 	}
17224 
17225 	scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
17226 	    request_blkno, err_blkno, scsi_cmds, sensep,
17227 	    un->un_additional_codes, NULL);
17228 }
17229 
17230 /*
17231  *    Function: sd_extract_sense_info_descr
17232  *
17233  * Description: Retrieve "information" field from descriptor format
17234  *              sense data.  Iterates through each sense descriptor
17235  *              looking for the information descriptor and returns
17236  *              the information field from that descriptor.
17237  *
17238  *     Context: May be called from interrupt context
17239  */
17240 
17241 static diskaddr_t
17242 sd_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp)
17243 {
17244 	diskaddr_t result;
17245 	uint8_t *descr_offset;
17246 	int valid_sense_length;
17247 	struct scsi_information_sense_descr *isd;
17248 
17249 	/*
17250 	 * Initialize result to -1 indicating there is no information
17251 	 * descriptor
17252 	 */
17253 	result = (diskaddr_t)-1;
17254 
17255 	/*
17256 	 * The first descriptor will immediately follow the header
17257 	 */
17258 	descr_offset = (uint8_t *)(sdsp+1); /* Pointer arithmetic */
17259 
17260 	/*
17261 	 * Calculate the amount of valid sense data
17262 	 */
17263 	valid_sense_length =
17264 	    min((sizeof (struct scsi_descr_sense_hdr) +
17265 	    sdsp->ds_addl_sense_length),
17266 	    SENSE_LENGTH);
17267 
17268 	/*
17269 	 * Iterate through the list of descriptors, stopping when we
17270 	 * run out of sense data
17271 	 */
17272 	while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <=
17273 	    (uint8_t *)sdsp + valid_sense_length) {
17274 		/*
17275 		 * Check if this is an information descriptor.  We can
17276 		 * use the scsi_information_sense_descr structure as a
17277 		 * template sense the first two fields are always the
17278 		 * same
17279 		 */
17280 		isd = (struct scsi_information_sense_descr *)descr_offset;
17281 		if (isd->isd_descr_type == DESCR_INFORMATION) {
17282 			/*
17283 			 * Found an information descriptor.  Copy the
17284 			 * information field.  There will only be one
17285 			 * information descriptor so we can stop looking.
17286 			 */
17287 			result =
17288 			    (((diskaddr_t)isd->isd_information[0] << 56) |
17289 				((diskaddr_t)isd->isd_information[1] << 48) |
17290 				((diskaddr_t)isd->isd_information[2] << 40) |
17291 				((diskaddr_t)isd->isd_information[3] << 32) |
17292 				((diskaddr_t)isd->isd_information[4] << 24) |
17293 				((diskaddr_t)isd->isd_information[5] << 16) |
17294 				((diskaddr_t)isd->isd_information[6] << 8)  |
17295 				((diskaddr_t)isd->isd_information[7]));
17296 			break;
17297 		}
17298 
17299 		/*
17300 		 * Get pointer to the next descriptor.  The "additional
17301 		 * length" field holds the length of the descriptor except
17302 		 * for the "type" and "additional length" fields, so
17303 		 * we need to add 2 to get the total length.
17304 		 */
17305 		descr_offset += (isd->isd_addl_length + 2);
17306 	}
17307 
17308 	return (result);
17309 }
17310 
17311 /*
17312  *    Function: sd_sense_key_no_sense
17313  *
17314  * Description: Recovery action when sense data was not received.
17315  *
17316  *     Context: May be called from interrupt context
17317  */
17318 
17319 static void
17320 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
17321 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17322 {
17323 	struct sd_sense_info	si;
17324 
17325 	ASSERT(un != NULL);
17326 	ASSERT(mutex_owned(SD_MUTEX(un)));
17327 	ASSERT(bp != NULL);
17328 	ASSERT(xp != NULL);
17329 	ASSERT(pktp != NULL);
17330 
17331 	si.ssi_severity = SCSI_ERR_FATAL;
17332 	si.ssi_pfa_flag = FALSE;
17333 
17334 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17335 
17336 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17337 		&si, EIO, (clock_t)0, NULL);
17338 }
17339 
17340 
17341 /*
17342  *    Function: sd_sense_key_recoverable_error
17343  *
17344  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
17345  *
17346  *     Context: May be called from interrupt context
17347  */
17348 
17349 static void
17350 sd_sense_key_recoverable_error(struct sd_lun *un,
17351 	uint8_t asc,
17352 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17353 {
17354 	struct sd_sense_info	si;
17355 
17356 	ASSERT(un != NULL);
17357 	ASSERT(mutex_owned(SD_MUTEX(un)));
17358 	ASSERT(bp != NULL);
17359 	ASSERT(xp != NULL);
17360 	ASSERT(pktp != NULL);
17361 
17362 	/*
17363 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17364 	 */
17365 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
17366 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17367 		si.ssi_severity = SCSI_ERR_INFO;
17368 		si.ssi_pfa_flag = TRUE;
17369 	} else {
17370 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
17371 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
17372 		si.ssi_severity = SCSI_ERR_RECOVERED;
17373 		si.ssi_pfa_flag = FALSE;
17374 	}
17375 
17376 	if (pktp->pkt_resid == 0) {
17377 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17378 		sd_return_command(un, bp);
17379 		return;
17380 	}
17381 
17382 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17383 	    &si, EIO, (clock_t)0, NULL);
17384 }
17385 
17386 
17387 
17388 
17389 /*
17390  *    Function: sd_sense_key_not_ready
17391  *
17392  * Description: Recovery actions for a SCSI "Not Ready" sense key.
17393  *
17394  *     Context: May be called from interrupt context
17395  */
17396 
17397 static void
17398 sd_sense_key_not_ready(struct sd_lun *un,
17399 	uint8_t asc, uint8_t ascq,
17400 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17401 {
17402 	struct sd_sense_info	si;
17403 
17404 	ASSERT(un != NULL);
17405 	ASSERT(mutex_owned(SD_MUTEX(un)));
17406 	ASSERT(bp != NULL);
17407 	ASSERT(xp != NULL);
17408 	ASSERT(pktp != NULL);
17409 
17410 	si.ssi_severity = SCSI_ERR_FATAL;
17411 	si.ssi_pfa_flag = FALSE;
17412 
17413 	/*
17414 	 * Update error stats after first NOT READY error. Disks may have
17415 	 * been powered down and may need to be restarted.  For CDROMs,
17416 	 * report NOT READY errors only if media is present.
17417 	 */
17418 	if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) ||
17419 	    (xp->xb_retry_count > 0)) {
17420 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17421 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
17422 	}
17423 
17424 	/*
17425 	 * Just fail if the "not ready" retry limit has been reached.
17426 	 */
17427 	if (xp->xb_retry_count >= un->un_notready_retry_count) {
17428 		/* Special check for error message printing for removables. */
17429 		if (un->un_f_has_removable_media && (asc == 0x04) &&
17430 		    (ascq >= 0x04)) {
17431 			si.ssi_severity = SCSI_ERR_ALL;
17432 		}
17433 		goto fail_command;
17434 	}
17435 
17436 	/*
17437 	 * Check the ASC and ASCQ in the sense data as needed, to determine
17438 	 * what to do.
17439 	 */
17440 	switch (asc) {
17441 	case 0x04:	/* LOGICAL UNIT NOT READY */
17442 		/*
17443 		 * disk drives that don't spin up result in a very long delay
17444 		 * in format without warning messages. We will log a message
17445 		 * if the error level is set to verbose.
17446 		 */
17447 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17448 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17449 			    "logical unit not ready, resetting disk\n");
17450 		}
17451 
17452 		/*
17453 		 * There are different requirements for CDROMs and disks for
17454 		 * the number of retries.  If a CD-ROM is giving this, it is
17455 		 * probably reading TOC and is in the process of getting
17456 		 * ready, so we should keep on trying for a long time to make
17457 		 * sure that all types of media are taken in account (for
17458 		 * some media the drive takes a long time to read TOC).  For
17459 		 * disks we do not want to retry this too many times as this
17460 		 * can cause a long hang in format when the drive refuses to
17461 		 * spin up (a very common failure).
17462 		 */
17463 		switch (ascq) {
17464 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
17465 			/*
17466 			 * Disk drives frequently refuse to spin up which
17467 			 * results in a very long hang in format without
17468 			 * warning messages.
17469 			 *
17470 			 * Note: This code preserves the legacy behavior of
17471 			 * comparing xb_retry_count against zero for fibre
17472 			 * channel targets instead of comparing against the
17473 			 * un_reset_retry_count value.  The reason for this
17474 			 * discrepancy has been so utterly lost beneath the
17475 			 * Sands of Time that even Indiana Jones could not
17476 			 * find it.
17477 			 */
17478 			if (un->un_f_is_fibre == TRUE) {
17479 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17480 					(xp->xb_retry_count > 0)) &&
17481 					(un->un_startstop_timeid == NULL)) {
17482 					scsi_log(SD_DEVINFO(un), sd_label,
17483 					CE_WARN, "logical unit not ready, "
17484 					"resetting disk\n");
17485 					sd_reset_target(un, pktp);
17486 				}
17487 			} else {
17488 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17489 					(xp->xb_retry_count >
17490 					un->un_reset_retry_count)) &&
17491 					(un->un_startstop_timeid == NULL)) {
17492 					scsi_log(SD_DEVINFO(un), sd_label,
17493 					CE_WARN, "logical unit not ready, "
17494 					"resetting disk\n");
17495 					sd_reset_target(un, pktp);
17496 				}
17497 			}
17498 			break;
17499 
17500 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
17501 			/*
17502 			 * If the target is in the process of becoming
17503 			 * ready, just proceed with the retry. This can
17504 			 * happen with CD-ROMs that take a long time to
17505 			 * read TOC after a power cycle or reset.
17506 			 */
17507 			goto do_retry;
17508 
17509 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
17510 			break;
17511 
17512 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
17513 			/*
17514 			 * Retries cannot help here so just fail right away.
17515 			 */
17516 			goto fail_command;
17517 
17518 		case 0x88:
17519 			/*
17520 			 * Vendor-unique code for T3/T4: it indicates a
17521 			 * path problem in a mutipathed config, but as far as
17522 			 * the target driver is concerned it equates to a fatal
17523 			 * error, so we should just fail the command right away
17524 			 * (without printing anything to the console). If this
17525 			 * is not a T3/T4, fall thru to the default recovery
17526 			 * action.
17527 			 * T3/T4 is FC only, don't need to check is_fibre
17528 			 */
17529 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
17530 				sd_return_failed_command(un, bp, EIO);
17531 				return;
17532 			}
17533 			/* FALLTHRU */
17534 
17535 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
17536 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
17537 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
17538 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
17539 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
17540 		default:    /* Possible future codes in SCSI spec? */
17541 			/*
17542 			 * For removable-media devices, do not retry if
17543 			 * ASCQ > 2 as these result mostly from USCSI commands
17544 			 * on MMC devices issued to check status of an
17545 			 * operation initiated in immediate mode.  Also for
17546 			 * ASCQ >= 4 do not print console messages as these
17547 			 * mainly represent a user-initiated operation
17548 			 * instead of a system failure.
17549 			 */
17550 			if (un->un_f_has_removable_media) {
17551 				si.ssi_severity = SCSI_ERR_ALL;
17552 				goto fail_command;
17553 			}
17554 			break;
17555 		}
17556 
17557 		/*
17558 		 * As part of our recovery attempt for the NOT READY
17559 		 * condition, we issue a START STOP UNIT command. However
17560 		 * we want to wait for a short delay before attempting this
17561 		 * as there may still be more commands coming back from the
17562 		 * target with the check condition. To do this we use
17563 		 * timeout(9F) to call sd_start_stop_unit_callback() after
17564 		 * the delay interval expires. (sd_start_stop_unit_callback()
17565 		 * dispatches sd_start_stop_unit_task(), which will issue
17566 		 * the actual START STOP UNIT command. The delay interval
17567 		 * is one-half of the delay that we will use to retry the
17568 		 * command that generated the NOT READY condition.
17569 		 *
17570 		 * Note that we could just dispatch sd_start_stop_unit_task()
17571 		 * from here and allow it to sleep for the delay interval,
17572 		 * but then we would be tying up the taskq thread
17573 		 * uncesessarily for the duration of the delay.
17574 		 *
17575 		 * Do not issue the START STOP UNIT if the current command
17576 		 * is already a START STOP UNIT.
17577 		 */
17578 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
17579 			break;
17580 		}
17581 
17582 		/*
17583 		 * Do not schedule the timeout if one is already pending.
17584 		 */
17585 		if (un->un_startstop_timeid != NULL) {
17586 			SD_INFO(SD_LOG_ERROR, un,
17587 			    "sd_sense_key_not_ready: restart already issued to"
17588 			    " %s%d\n", ddi_driver_name(SD_DEVINFO(un)),
17589 			    ddi_get_instance(SD_DEVINFO(un)));
17590 			break;
17591 		}
17592 
17593 		/*
17594 		 * Schedule the START STOP UNIT command, then queue the command
17595 		 * for a retry.
17596 		 *
17597 		 * Note: A timeout is not scheduled for this retry because we
17598 		 * want the retry to be serial with the START_STOP_UNIT. The
17599 		 * retry will be started when the START_STOP_UNIT is completed
17600 		 * in sd_start_stop_unit_task.
17601 		 */
17602 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
17603 		    un, SD_BSY_TIMEOUT / 2);
17604 		xp->xb_retry_count++;
17605 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
17606 		return;
17607 
17608 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
17609 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17610 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17611 			    "unit does not respond to selection\n");
17612 		}
17613 		break;
17614 
17615 	case 0x3A:	/* MEDIUM NOT PRESENT */
17616 		if (sd_error_level >= SCSI_ERR_FATAL) {
17617 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17618 			    "Caddy not inserted in drive\n");
17619 		}
17620 
17621 		sr_ejected(un);
17622 		un->un_mediastate = DKIO_EJECTED;
17623 		/* The state has changed, inform the media watch routines */
17624 		cv_broadcast(&un->un_state_cv);
17625 		/* Just fail if no media is present in the drive. */
17626 		goto fail_command;
17627 
17628 	default:
17629 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17630 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
17631 			    "Unit not Ready. Additional sense code 0x%x\n",
17632 			    asc);
17633 		}
17634 		break;
17635 	}
17636 
17637 do_retry:
17638 
17639 	/*
17640 	 * Retry the command, as some targets may report NOT READY for
17641 	 * several seconds after being reset.
17642 	 */
17643 	xp->xb_retry_count++;
17644 	si.ssi_severity = SCSI_ERR_RETRYABLE;
17645 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
17646 	    &si, EIO, SD_BSY_TIMEOUT, NULL);
17647 
17648 	return;
17649 
17650 fail_command:
17651 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17652 	sd_return_failed_command(un, bp, EIO);
17653 }
17654 
17655 
17656 
17657 /*
17658  *    Function: sd_sense_key_medium_or_hardware_error
17659  *
17660  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
17661  *		sense key.
17662  *
17663  *     Context: May be called from interrupt context
17664  */
17665 
17666 static void
17667 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
17668 	int sense_key, uint8_t asc,
17669 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17670 {
17671 	struct sd_sense_info	si;
17672 
17673 	ASSERT(un != NULL);
17674 	ASSERT(mutex_owned(SD_MUTEX(un)));
17675 	ASSERT(bp != NULL);
17676 	ASSERT(xp != NULL);
17677 	ASSERT(pktp != NULL);
17678 
17679 	si.ssi_severity = SCSI_ERR_FATAL;
17680 	si.ssi_pfa_flag = FALSE;
17681 
17682 	if (sense_key == KEY_MEDIUM_ERROR) {
17683 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
17684 	}
17685 
17686 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17687 
17688 	if ((un->un_reset_retry_count != 0) &&
17689 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
17690 		mutex_exit(SD_MUTEX(un));
17691 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
17692 		if (un->un_f_allow_bus_device_reset == TRUE) {
17693 
17694 			boolean_t try_resetting_target = B_TRUE;
17695 
17696 			/*
17697 			 * We need to be able to handle specific ASC when we are
17698 			 * handling a KEY_HARDWARE_ERROR. In particular
17699 			 * taking the default action of resetting the target may
17700 			 * not be the appropriate way to attempt recovery.
17701 			 * Resetting a target because of a single LUN failure
17702 			 * victimizes all LUNs on that target.
17703 			 *
17704 			 * This is true for the LSI arrays, if an LSI
17705 			 * array controller returns an ASC of 0x84 (LUN Dead) we
17706 			 * should trust it.
17707 			 */
17708 
17709 			if (sense_key == KEY_HARDWARE_ERROR) {
17710 				switch (asc) {
17711 				case 0x84:
17712 					if (SD_IS_LSI(un)) {
17713 						try_resetting_target = B_FALSE;
17714 					}
17715 					break;
17716 				default:
17717 					break;
17718 				}
17719 			}
17720 
17721 			if (try_resetting_target == B_TRUE) {
17722 				int reset_retval = 0;
17723 				if (un->un_f_lun_reset_enabled == TRUE) {
17724 					SD_TRACE(SD_LOG_IO_CORE, un,
17725 					    "sd_sense_key_medium_or_hardware_"
17726 					    "error: issuing RESET_LUN\n");
17727 					reset_retval =
17728 					    scsi_reset(SD_ADDRESS(un),
17729 					    RESET_LUN);
17730 				}
17731 				if (reset_retval == 0) {
17732 					SD_TRACE(SD_LOG_IO_CORE, un,
17733 					    "sd_sense_key_medium_or_hardware_"
17734 					    "error: issuing RESET_TARGET\n");
17735 					(void) scsi_reset(SD_ADDRESS(un),
17736 					    RESET_TARGET);
17737 				}
17738 			}
17739 		}
17740 		mutex_enter(SD_MUTEX(un));
17741 	}
17742 
17743 	/*
17744 	 * This really ought to be a fatal error, but we will retry anyway
17745 	 * as some drives report this as a spurious error.
17746 	 */
17747 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17748 	    &si, EIO, (clock_t)0, NULL);
17749 }
17750 
17751 
17752 
17753 /*
17754  *    Function: sd_sense_key_illegal_request
17755  *
17756  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
17757  *
17758  *     Context: May be called from interrupt context
17759  */
17760 
17761 static void
17762 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
17763 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17764 {
17765 	struct sd_sense_info	si;
17766 
17767 	ASSERT(un != NULL);
17768 	ASSERT(mutex_owned(SD_MUTEX(un)));
17769 	ASSERT(bp != NULL);
17770 	ASSERT(xp != NULL);
17771 	ASSERT(pktp != NULL);
17772 
17773 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17774 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
17775 
17776 	si.ssi_severity = SCSI_ERR_INFO;
17777 	si.ssi_pfa_flag = FALSE;
17778 
17779 	/* Pointless to retry if the target thinks it's an illegal request */
17780 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17781 	sd_return_failed_command(un, bp, EIO);
17782 }
17783 
17784 
17785 
17786 
17787 /*
17788  *    Function: sd_sense_key_unit_attention
17789  *
17790  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
17791  *
17792  *     Context: May be called from interrupt context
17793  */
17794 
17795 static void
17796 sd_sense_key_unit_attention(struct sd_lun *un,
17797 	uint8_t asc,
17798 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17799 {
17800 	/*
17801 	 * For UNIT ATTENTION we allow retries for one minute. Devices
17802 	 * like Sonoma can return UNIT ATTENTION close to a minute
17803 	 * under certain conditions.
17804 	 */
17805 	int	retry_check_flag = SD_RETRIES_UA;
17806 	boolean_t	kstat_updated = B_FALSE;
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 	si.ssi_severity = SCSI_ERR_INFO;
17816 	si.ssi_pfa_flag = FALSE;
17817 
17818 
17819 	switch (asc) {
17820 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
17821 		if (sd_report_pfa != 0) {
17822 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17823 			si.ssi_pfa_flag = TRUE;
17824 			retry_check_flag = SD_RETRIES_STANDARD;
17825 			goto do_retry;
17826 		}
17827 		break;
17828 
17829 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
17830 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
17831 			un->un_resvd_status |=
17832 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
17833 		}
17834 		/* FALLTHRU */
17835 
17836 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
17837 		if (!un->un_f_has_removable_media) {
17838 			break;
17839 		}
17840 
17841 		/*
17842 		 * When we get a unit attention from a removable-media device,
17843 		 * it may be in a state that will take a long time to recover
17844 		 * (e.g., from a reset).  Since we are executing in interrupt
17845 		 * context here, we cannot wait around for the device to come
17846 		 * back. So hand this command off to sd_media_change_task()
17847 		 * for deferred processing under taskq thread context. (Note
17848 		 * that the command still may be failed if a problem is
17849 		 * encountered at a later time.)
17850 		 */
17851 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
17852 		    KM_NOSLEEP) == 0) {
17853 			/*
17854 			 * Cannot dispatch the request so fail the command.
17855 			 */
17856 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
17857 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
17858 			si.ssi_severity = SCSI_ERR_FATAL;
17859 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17860 			sd_return_failed_command(un, bp, EIO);
17861 		}
17862 
17863 		/*
17864 		 * If failed to dispatch sd_media_change_task(), we already
17865 		 * updated kstat. If succeed to dispatch sd_media_change_task(),
17866 		 * we should update kstat later if it encounters an error. So,
17867 		 * we update kstat_updated flag here.
17868 		 */
17869 		kstat_updated = B_TRUE;
17870 
17871 		/*
17872 		 * Either the command has been successfully dispatched to a
17873 		 * task Q for retrying, or the dispatch failed. In either case
17874 		 * do NOT retry again by calling sd_retry_command. This sets up
17875 		 * two retries of the same command and when one completes and
17876 		 * frees the resources the other will access freed memory,
17877 		 * a bad thing.
17878 		 */
17879 		return;
17880 
17881 	default:
17882 		break;
17883 	}
17884 
17885 	/*
17886 	 * Update kstat if we haven't done that.
17887 	 */
17888 	if (!kstat_updated) {
17889 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17890 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
17891 	}
17892 
17893 do_retry:
17894 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
17895 	    EIO, SD_UA_RETRY_DELAY, NULL);
17896 }
17897 
17898 
17899 
17900 /*
17901  *    Function: sd_sense_key_fail_command
17902  *
17903  * Description: Use to fail a command when we don't like the sense key that
17904  *		was returned.
17905  *
17906  *     Context: May be called from interrupt context
17907  */
17908 
17909 static void
17910 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
17911 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17912 {
17913 	struct sd_sense_info	si;
17914 
17915 	ASSERT(un != NULL);
17916 	ASSERT(mutex_owned(SD_MUTEX(un)));
17917 	ASSERT(bp != NULL);
17918 	ASSERT(xp != NULL);
17919 	ASSERT(pktp != NULL);
17920 
17921 	si.ssi_severity = SCSI_ERR_FATAL;
17922 	si.ssi_pfa_flag = FALSE;
17923 
17924 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17925 	sd_return_failed_command(un, bp, EIO);
17926 }
17927 
17928 
17929 
17930 /*
17931  *    Function: sd_sense_key_blank_check
17932  *
17933  * Description: Recovery actions for a SCSI "Blank Check" sense key.
17934  *		Has no monetary connotation.
17935  *
17936  *     Context: May be called from interrupt context
17937  */
17938 
17939 static void
17940 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
17941 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17942 {
17943 	struct sd_sense_info	si;
17944 
17945 	ASSERT(un != NULL);
17946 	ASSERT(mutex_owned(SD_MUTEX(un)));
17947 	ASSERT(bp != NULL);
17948 	ASSERT(xp != NULL);
17949 	ASSERT(pktp != NULL);
17950 
17951 	/*
17952 	 * Blank check is not fatal for removable devices, therefore
17953 	 * it does not require a console message.
17954 	 */
17955 	si.ssi_severity = (un->un_f_has_removable_media) ? SCSI_ERR_ALL :
17956 	    SCSI_ERR_FATAL;
17957 	si.ssi_pfa_flag = FALSE;
17958 
17959 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17960 	sd_return_failed_command(un, bp, EIO);
17961 }
17962 
17963 
17964 
17965 
17966 /*
17967  *    Function: sd_sense_key_aborted_command
17968  *
17969  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
17970  *
17971  *     Context: May be called from interrupt context
17972  */
17973 
17974 static void
17975 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
17976 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17977 {
17978 	struct sd_sense_info	si;
17979 
17980 	ASSERT(un != NULL);
17981 	ASSERT(mutex_owned(SD_MUTEX(un)));
17982 	ASSERT(bp != NULL);
17983 	ASSERT(xp != NULL);
17984 	ASSERT(pktp != NULL);
17985 
17986 	si.ssi_severity = SCSI_ERR_FATAL;
17987 	si.ssi_pfa_flag = FALSE;
17988 
17989 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17990 
17991 	/*
17992 	 * This really ought to be a fatal error, but we will retry anyway
17993 	 * as some drives report this as a spurious error.
17994 	 */
17995 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17996 	    &si, EIO, (clock_t)0, NULL);
17997 }
17998 
17999 
18000 
18001 /*
18002  *    Function: sd_sense_key_default
18003  *
18004  * Description: Default recovery action for several SCSI sense keys (basically
18005  *		attempts a retry).
18006  *
18007  *     Context: May be called from interrupt context
18008  */
18009 
18010 static void
18011 sd_sense_key_default(struct sd_lun *un,
18012 	int sense_key,
18013 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
18014 {
18015 	struct sd_sense_info	si;
18016 
18017 	ASSERT(un != NULL);
18018 	ASSERT(mutex_owned(SD_MUTEX(un)));
18019 	ASSERT(bp != NULL);
18020 	ASSERT(xp != NULL);
18021 	ASSERT(pktp != NULL);
18022 
18023 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18024 
18025 	/*
18026 	 * Undecoded sense key.	Attempt retries and hope that will fix
18027 	 * the problem.  Otherwise, we're dead.
18028 	 */
18029 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
18030 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18031 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
18032 	}
18033 
18034 	si.ssi_severity = SCSI_ERR_FATAL;
18035 	si.ssi_pfa_flag = FALSE;
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_print_retry_msg
18045  *
18046  * Description: Print a message indicating the retry action being taken.
18047  *
18048  *   Arguments: un - ptr to associated softstate
18049  *		bp - ptr to buf(9S) for the command
18050  *		arg - not used.
18051  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18052  *			or SD_NO_RETRY_ISSUED
18053  *
18054  *     Context: May be called from interrupt context
18055  */
18056 /* ARGSUSED */
18057 static void
18058 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
18059 {
18060 	struct sd_xbuf	*xp;
18061 	struct scsi_pkt *pktp;
18062 	char *reasonp;
18063 	char *msgp;
18064 
18065 	ASSERT(un != NULL);
18066 	ASSERT(mutex_owned(SD_MUTEX(un)));
18067 	ASSERT(bp != NULL);
18068 	pktp = SD_GET_PKTP(bp);
18069 	ASSERT(pktp != NULL);
18070 	xp = SD_GET_XBUF(bp);
18071 	ASSERT(xp != NULL);
18072 
18073 	ASSERT(!mutex_owned(&un->un_pm_mutex));
18074 	mutex_enter(&un->un_pm_mutex);
18075 	if ((un->un_state == SD_STATE_SUSPENDED) ||
18076 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
18077 	    (pktp->pkt_flags & FLAG_SILENT)) {
18078 		mutex_exit(&un->un_pm_mutex);
18079 		goto update_pkt_reason;
18080 	}
18081 	mutex_exit(&un->un_pm_mutex);
18082 
18083 	/*
18084 	 * Suppress messages if they are all the same pkt_reason; with
18085 	 * TQ, many (up to 256) are returned with the same pkt_reason.
18086 	 * If we are in panic, then suppress the retry messages.
18087 	 */
18088 	switch (flag) {
18089 	case SD_NO_RETRY_ISSUED:
18090 		msgp = "giving up";
18091 		break;
18092 	case SD_IMMEDIATE_RETRY_ISSUED:
18093 	case SD_DELAYED_RETRY_ISSUED:
18094 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
18095 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
18096 		    (sd_error_level != SCSI_ERR_ALL))) {
18097 			return;
18098 		}
18099 		msgp = "retrying command";
18100 		break;
18101 	default:
18102 		goto update_pkt_reason;
18103 	}
18104 
18105 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
18106 	    scsi_rname(pktp->pkt_reason));
18107 
18108 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18109 	    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
18110 
18111 update_pkt_reason:
18112 	/*
18113 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
18114 	 * This is to prevent multiple console messages for the same failure
18115 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
18116 	 * when the command is retried successfully because there still may be
18117 	 * more commands coming back with the same value of pktp->pkt_reason.
18118 	 */
18119 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
18120 		un->un_last_pkt_reason = pktp->pkt_reason;
18121 	}
18122 }
18123 
18124 
18125 /*
18126  *    Function: sd_print_cmd_incomplete_msg
18127  *
18128  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
18129  *
18130  *   Arguments: un - ptr to associated softstate
18131  *		bp - ptr to buf(9S) for the command
18132  *		arg - passed to sd_print_retry_msg()
18133  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
18134  *			or SD_NO_RETRY_ISSUED
18135  *
18136  *     Context: May be called from interrupt context
18137  */
18138 
18139 static void
18140 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
18141 	int code)
18142 {
18143 	dev_info_t	*dip;
18144 
18145 	ASSERT(un != NULL);
18146 	ASSERT(mutex_owned(SD_MUTEX(un)));
18147 	ASSERT(bp != NULL);
18148 
18149 	switch (code) {
18150 	case SD_NO_RETRY_ISSUED:
18151 		/* Command was failed. Someone turned off this target? */
18152 		if (un->un_state != SD_STATE_OFFLINE) {
18153 			/*
18154 			 * Suppress message if we are detaching and
18155 			 * device has been disconnected
18156 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
18157 			 * private interface and not part of the DDI
18158 			 */
18159 			dip = un->un_sd->sd_dev;
18160 			if (!(DEVI_IS_DETACHING(dip) &&
18161 			    DEVI_IS_DEVICE_REMOVED(dip))) {
18162 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18163 				"disk not responding to selection\n");
18164 			}
18165 			New_state(un, SD_STATE_OFFLINE);
18166 		}
18167 		break;
18168 
18169 	case SD_DELAYED_RETRY_ISSUED:
18170 	case SD_IMMEDIATE_RETRY_ISSUED:
18171 	default:
18172 		/* Command was successfully queued for retry */
18173 		sd_print_retry_msg(un, bp, arg, code);
18174 		break;
18175 	}
18176 }
18177 
18178 
18179 /*
18180  *    Function: sd_pkt_reason_cmd_incomplete
18181  *
18182  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
18183  *
18184  *     Context: May be called from interrupt context
18185  */
18186 
18187 static void
18188 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
18189 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18190 {
18191 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
18192 
18193 	ASSERT(un != NULL);
18194 	ASSERT(mutex_owned(SD_MUTEX(un)));
18195 	ASSERT(bp != NULL);
18196 	ASSERT(xp != NULL);
18197 	ASSERT(pktp != NULL);
18198 
18199 	/* Do not do a reset if selection did not complete */
18200 	/* Note: Should this not just check the bit? */
18201 	if (pktp->pkt_state != STATE_GOT_BUS) {
18202 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18203 		sd_reset_target(un, pktp);
18204 	}
18205 
18206 	/*
18207 	 * If the target was not successfully selected, then set
18208 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
18209 	 * with the target, and further retries and/or commands are
18210 	 * likely to take a long time.
18211 	 */
18212 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
18213 		flag |= SD_RETRIES_FAILFAST;
18214 	}
18215 
18216 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18217 
18218 	sd_retry_command(un, bp, flag,
18219 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18220 }
18221 
18222 
18223 
18224 /*
18225  *    Function: sd_pkt_reason_cmd_tran_err
18226  *
18227  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
18228  *
18229  *     Context: May be called from interrupt context
18230  */
18231 
18232 static void
18233 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
18234 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18235 {
18236 	ASSERT(un != NULL);
18237 	ASSERT(mutex_owned(SD_MUTEX(un)));
18238 	ASSERT(bp != NULL);
18239 	ASSERT(xp != NULL);
18240 	ASSERT(pktp != NULL);
18241 
18242 	/*
18243 	 * Do not reset if we got a parity error, or if
18244 	 * selection did not complete.
18245 	 */
18246 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18247 	/* Note: Should this not just check the bit for pkt_state? */
18248 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
18249 	    (pktp->pkt_state != STATE_GOT_BUS)) {
18250 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
18251 		sd_reset_target(un, pktp);
18252 	}
18253 
18254 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18255 
18256 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18257 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18258 }
18259 
18260 
18261 
18262 /*
18263  *    Function: sd_pkt_reason_cmd_reset
18264  *
18265  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
18266  *
18267  *     Context: May be called from interrupt context
18268  */
18269 
18270 static void
18271 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
18272 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18273 {
18274 	ASSERT(un != NULL);
18275 	ASSERT(mutex_owned(SD_MUTEX(un)));
18276 	ASSERT(bp != NULL);
18277 	ASSERT(xp != NULL);
18278 	ASSERT(pktp != NULL);
18279 
18280 	/* The target may still be running the command, so try to reset. */
18281 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18282 	sd_reset_target(un, pktp);
18283 
18284 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18285 
18286 	/*
18287 	 * If pkt_reason is CMD_RESET chances are that this pkt got
18288 	 * reset because another target on this bus caused it. The target
18289 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18290 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18291 	 */
18292 
18293 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18294 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18295 }
18296 
18297 
18298 
18299 
18300 /*
18301  *    Function: sd_pkt_reason_cmd_aborted
18302  *
18303  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
18304  *
18305  *     Context: May be called from interrupt context
18306  */
18307 
18308 static void
18309 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
18310 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18311 {
18312 	ASSERT(un != NULL);
18313 	ASSERT(mutex_owned(SD_MUTEX(un)));
18314 	ASSERT(bp != NULL);
18315 	ASSERT(xp != NULL);
18316 	ASSERT(pktp != NULL);
18317 
18318 	/* The target may still be running the command, so try to reset. */
18319 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18320 	sd_reset_target(un, pktp);
18321 
18322 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18323 
18324 	/*
18325 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
18326 	 * aborted because another target on this bus caused it. The target
18327 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
18328 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
18329 	 */
18330 
18331 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
18332 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18333 }
18334 
18335 
18336 
18337 /*
18338  *    Function: sd_pkt_reason_cmd_timeout
18339  *
18340  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
18341  *
18342  *     Context: May be called from interrupt context
18343  */
18344 
18345 static void
18346 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
18347 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18348 {
18349 	ASSERT(un != NULL);
18350 	ASSERT(mutex_owned(SD_MUTEX(un)));
18351 	ASSERT(bp != NULL);
18352 	ASSERT(xp != NULL);
18353 	ASSERT(pktp != NULL);
18354 
18355 
18356 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18357 	sd_reset_target(un, pktp);
18358 
18359 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18360 
18361 	/*
18362 	 * A command timeout indicates that we could not establish
18363 	 * communication with the target, so set SD_RETRIES_FAILFAST
18364 	 * as further retries/commands are likely to take a long time.
18365 	 */
18366 	sd_retry_command(un, bp,
18367 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
18368 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18369 }
18370 
18371 
18372 
18373 /*
18374  *    Function: sd_pkt_reason_cmd_unx_bus_free
18375  *
18376  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
18377  *
18378  *     Context: May be called from interrupt context
18379  */
18380 
18381 static void
18382 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
18383 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18384 {
18385 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
18386 
18387 	ASSERT(un != NULL);
18388 	ASSERT(mutex_owned(SD_MUTEX(un)));
18389 	ASSERT(bp != NULL);
18390 	ASSERT(xp != NULL);
18391 	ASSERT(pktp != NULL);
18392 
18393 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18394 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18395 
18396 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
18397 	    sd_print_retry_msg : NULL;
18398 
18399 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18400 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18401 }
18402 
18403 
18404 /*
18405  *    Function: sd_pkt_reason_cmd_tag_reject
18406  *
18407  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
18408  *
18409  *     Context: May be called from interrupt context
18410  */
18411 
18412 static void
18413 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
18414 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18415 {
18416 	ASSERT(un != NULL);
18417 	ASSERT(mutex_owned(SD_MUTEX(un)));
18418 	ASSERT(bp != NULL);
18419 	ASSERT(xp != NULL);
18420 	ASSERT(pktp != NULL);
18421 
18422 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18423 	pktp->pkt_flags = 0;
18424 	un->un_tagflags = 0;
18425 	if (un->un_f_opt_queueing == TRUE) {
18426 		un->un_throttle = min(un->un_throttle, 3);
18427 	} else {
18428 		un->un_throttle = 1;
18429 	}
18430 	mutex_exit(SD_MUTEX(un));
18431 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
18432 	mutex_enter(SD_MUTEX(un));
18433 
18434 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18435 
18436 	/* Legacy behavior not to check retry counts here. */
18437 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
18438 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18439 }
18440 
18441 
18442 /*
18443  *    Function: sd_pkt_reason_default
18444  *
18445  * Description: Default recovery actions for SCSA pkt_reason values that
18446  *		do not have more explicit recovery actions.
18447  *
18448  *     Context: May be called from interrupt context
18449  */
18450 
18451 static void
18452 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
18453 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18454 {
18455 	ASSERT(un != NULL);
18456 	ASSERT(mutex_owned(SD_MUTEX(un)));
18457 	ASSERT(bp != NULL);
18458 	ASSERT(xp != NULL);
18459 	ASSERT(pktp != NULL);
18460 
18461 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18462 	sd_reset_target(un, pktp);
18463 
18464 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18465 
18466 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18467 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18468 }
18469 
18470 
18471 
18472 /*
18473  *    Function: sd_pkt_status_check_condition
18474  *
18475  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
18476  *
18477  *     Context: May be called from interrupt context
18478  */
18479 
18480 static void
18481 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
18482 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18483 {
18484 	ASSERT(un != NULL);
18485 	ASSERT(mutex_owned(SD_MUTEX(un)));
18486 	ASSERT(bp != NULL);
18487 	ASSERT(xp != NULL);
18488 	ASSERT(pktp != NULL);
18489 
18490 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
18491 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
18492 
18493 	/*
18494 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
18495 	 * command will be retried after the request sense). Otherwise, retry
18496 	 * the command. Note: we are issuing the request sense even though the
18497 	 * retry limit may have been reached for the failed command.
18498 	 */
18499 	if (un->un_f_arq_enabled == FALSE) {
18500 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18501 		    "no ARQ, sending request sense command\n");
18502 		sd_send_request_sense_command(un, bp, pktp);
18503 	} else {
18504 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18505 		    "ARQ,retrying request sense command\n");
18506 #if defined(__i386) || defined(__amd64)
18507 		/*
18508 		 * The SD_RETRY_DELAY value need to be adjusted here
18509 		 * when SD_RETRY_DELAY change in sddef.h
18510 		 */
18511 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18512 			un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
18513 			NULL);
18514 #else
18515 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
18516 		    EIO, SD_RETRY_DELAY, NULL);
18517 #endif
18518 	}
18519 
18520 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
18521 }
18522 
18523 
18524 /*
18525  *    Function: sd_pkt_status_busy
18526  *
18527  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
18528  *
18529  *     Context: May be called from interrupt context
18530  */
18531 
18532 static void
18533 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18534 	struct scsi_pkt *pktp)
18535 {
18536 	ASSERT(un != NULL);
18537 	ASSERT(mutex_owned(SD_MUTEX(un)));
18538 	ASSERT(bp != NULL);
18539 	ASSERT(xp != NULL);
18540 	ASSERT(pktp != NULL);
18541 
18542 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18543 	    "sd_pkt_status_busy: entry\n");
18544 
18545 	/* If retries are exhausted, just fail the command. */
18546 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
18547 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18548 		    "device busy too long\n");
18549 		sd_return_failed_command(un, bp, EIO);
18550 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18551 		    "sd_pkt_status_busy: exit\n");
18552 		return;
18553 	}
18554 	xp->xb_retry_count++;
18555 
18556 	/*
18557 	 * Try to reset the target. However, we do not want to perform
18558 	 * more than one reset if the device continues to fail. The reset
18559 	 * will be performed when the retry count reaches the reset
18560 	 * threshold.  This threshold should be set such that at least
18561 	 * one retry is issued before the reset is performed.
18562 	 */
18563 	if (xp->xb_retry_count ==
18564 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
18565 		int rval = 0;
18566 		mutex_exit(SD_MUTEX(un));
18567 		if (un->un_f_allow_bus_device_reset == TRUE) {
18568 			/*
18569 			 * First try to reset the LUN; if we cannot then
18570 			 * try to reset the target.
18571 			 */
18572 			if (un->un_f_lun_reset_enabled == TRUE) {
18573 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18574 				    "sd_pkt_status_busy: RESET_LUN\n");
18575 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18576 			}
18577 			if (rval == 0) {
18578 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18579 				    "sd_pkt_status_busy: RESET_TARGET\n");
18580 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
18581 			}
18582 		}
18583 		if (rval == 0) {
18584 			/*
18585 			 * If the RESET_LUN and/or RESET_TARGET failed,
18586 			 * try RESET_ALL
18587 			 */
18588 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18589 			    "sd_pkt_status_busy: RESET_ALL\n");
18590 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
18591 		}
18592 		mutex_enter(SD_MUTEX(un));
18593 		if (rval == 0) {
18594 			/*
18595 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
18596 			 * At this point we give up & fail the command.
18597 			 */
18598 			sd_return_failed_command(un, bp, EIO);
18599 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18600 			    "sd_pkt_status_busy: exit (failed cmd)\n");
18601 			return;
18602 		}
18603 	}
18604 
18605 	/*
18606 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
18607 	 * we have already checked the retry counts above.
18608 	 */
18609 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
18610 	    EIO, SD_BSY_TIMEOUT, NULL);
18611 
18612 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18613 	    "sd_pkt_status_busy: exit\n");
18614 }
18615 
18616 
18617 /*
18618  *    Function: sd_pkt_status_reservation_conflict
18619  *
18620  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
18621  *		command status.
18622  *
18623  *     Context: May be called from interrupt context
18624  */
18625 
18626 static void
18627 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
18628 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18629 {
18630 	ASSERT(un != NULL);
18631 	ASSERT(mutex_owned(SD_MUTEX(un)));
18632 	ASSERT(bp != NULL);
18633 	ASSERT(xp != NULL);
18634 	ASSERT(pktp != NULL);
18635 
18636 	/*
18637 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
18638 	 * conflict could be due to various reasons like incorrect keys, not
18639 	 * registered or not reserved etc. So, we return EACCES to the caller.
18640 	 */
18641 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
18642 		int cmd = SD_GET_PKT_OPCODE(pktp);
18643 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
18644 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
18645 			sd_return_failed_command(un, bp, EACCES);
18646 			return;
18647 		}
18648 	}
18649 
18650 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
18651 
18652 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
18653 		if (sd_failfast_enable != 0) {
18654 			/* By definition, we must panic here.... */
18655 			sd_panic_for_res_conflict(un);
18656 			/*NOTREACHED*/
18657 		}
18658 		SD_ERROR(SD_LOG_IO, un,
18659 		    "sd_handle_resv_conflict: Disk Reserved\n");
18660 		sd_return_failed_command(un, bp, EACCES);
18661 		return;
18662 	}
18663 
18664 	/*
18665 	 * 1147670: retry only if sd_retry_on_reservation_conflict
18666 	 * property is set (default is 1). Retries will not succeed
18667 	 * on a disk reserved by another initiator. HA systems
18668 	 * may reset this via sd.conf to avoid these retries.
18669 	 *
18670 	 * Note: The legacy return code for this failure is EIO, however EACCES
18671 	 * seems more appropriate for a reservation conflict.
18672 	 */
18673 	if (sd_retry_on_reservation_conflict == 0) {
18674 		SD_ERROR(SD_LOG_IO, un,
18675 		    "sd_handle_resv_conflict: Device Reserved\n");
18676 		sd_return_failed_command(un, bp, EIO);
18677 		return;
18678 	}
18679 
18680 	/*
18681 	 * Retry the command if we can.
18682 	 *
18683 	 * Note: The legacy return code for this failure is EIO, however EACCES
18684 	 * seems more appropriate for a reservation conflict.
18685 	 */
18686 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18687 	    (clock_t)2, NULL);
18688 }
18689 
18690 
18691 
18692 /*
18693  *    Function: sd_pkt_status_qfull
18694  *
18695  * Description: Handle a QUEUE FULL condition from the target.  This can
18696  *		occur if the HBA does not handle the queue full condition.
18697  *		(Basically this means third-party HBAs as Sun HBAs will
18698  *		handle the queue full condition.)  Note that if there are
18699  *		some commands already in the transport, then the queue full
18700  *		has occurred because the queue for this nexus is actually
18701  *		full. If there are no commands in the transport, then the
18702  *		queue full is resulting from some other initiator or lun
18703  *		consuming all the resources at the target.
18704  *
18705  *     Context: May be called from interrupt context
18706  */
18707 
18708 static void
18709 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
18710 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18711 {
18712 	ASSERT(un != NULL);
18713 	ASSERT(mutex_owned(SD_MUTEX(un)));
18714 	ASSERT(bp != NULL);
18715 	ASSERT(xp != NULL);
18716 	ASSERT(pktp != NULL);
18717 
18718 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18719 	    "sd_pkt_status_qfull: entry\n");
18720 
18721 	/*
18722 	 * Just lower the QFULL throttle and retry the command.  Note that
18723 	 * we do not limit the number of retries here.
18724 	 */
18725 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
18726 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
18727 	    SD_RESTART_TIMEOUT, NULL);
18728 
18729 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18730 	    "sd_pkt_status_qfull: exit\n");
18731 }
18732 
18733 
18734 /*
18735  *    Function: sd_reset_target
18736  *
18737  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
18738  *		RESET_TARGET, or RESET_ALL.
18739  *
18740  *     Context: May be called under interrupt context.
18741  */
18742 
18743 static void
18744 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
18745 {
18746 	int rval = 0;
18747 
18748 	ASSERT(un != NULL);
18749 	ASSERT(mutex_owned(SD_MUTEX(un)));
18750 	ASSERT(pktp != NULL);
18751 
18752 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
18753 
18754 	/*
18755 	 * No need to reset if the transport layer has already done so.
18756 	 */
18757 	if ((pktp->pkt_statistics &
18758 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
18759 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18760 		    "sd_reset_target: no reset\n");
18761 		return;
18762 	}
18763 
18764 	mutex_exit(SD_MUTEX(un));
18765 
18766 	if (un->un_f_allow_bus_device_reset == TRUE) {
18767 		if (un->un_f_lun_reset_enabled == TRUE) {
18768 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18769 			    "sd_reset_target: RESET_LUN\n");
18770 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18771 		}
18772 		if (rval == 0) {
18773 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18774 			    "sd_reset_target: RESET_TARGET\n");
18775 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
18776 		}
18777 	}
18778 
18779 	if (rval == 0) {
18780 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18781 		    "sd_reset_target: RESET_ALL\n");
18782 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
18783 	}
18784 
18785 	mutex_enter(SD_MUTEX(un));
18786 
18787 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
18788 }
18789 
18790 
18791 /*
18792  *    Function: sd_media_change_task
18793  *
18794  * Description: Recovery action for CDROM to become available.
18795  *
18796  *     Context: Executes in a taskq() thread context
18797  */
18798 
18799 static void
18800 sd_media_change_task(void *arg)
18801 {
18802 	struct	scsi_pkt	*pktp = arg;
18803 	struct	sd_lun		*un;
18804 	struct	buf		*bp;
18805 	struct	sd_xbuf		*xp;
18806 	int	err		= 0;
18807 	int	retry_count	= 0;
18808 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
18809 	struct	sd_sense_info	si;
18810 
18811 	ASSERT(pktp != NULL);
18812 	bp = (struct buf *)pktp->pkt_private;
18813 	ASSERT(bp != NULL);
18814 	xp = SD_GET_XBUF(bp);
18815 	ASSERT(xp != NULL);
18816 	un = SD_GET_UN(bp);
18817 	ASSERT(un != NULL);
18818 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18819 	ASSERT(un->un_f_monitor_media_state);
18820 
18821 	si.ssi_severity = SCSI_ERR_INFO;
18822 	si.ssi_pfa_flag = FALSE;
18823 
18824 	/*
18825 	 * When a reset is issued on a CDROM, it takes a long time to
18826 	 * recover. First few attempts to read capacity and other things
18827 	 * related to handling unit attention fail (with a ASC 0x4 and
18828 	 * ASCQ 0x1). In that case we want to do enough retries and we want
18829 	 * to limit the retries in other cases of genuine failures like
18830 	 * no media in drive.
18831 	 */
18832 	while (retry_count++ < retry_limit) {
18833 		if ((err = sd_handle_mchange(un)) == 0) {
18834 			break;
18835 		}
18836 		if (err == EAGAIN) {
18837 			retry_limit = SD_UNIT_ATTENTION_RETRY;
18838 		}
18839 		/* Sleep for 0.5 sec. & try again */
18840 		delay(drv_usectohz(500000));
18841 	}
18842 
18843 	/*
18844 	 * Dispatch (retry or fail) the original command here,
18845 	 * along with appropriate console messages....
18846 	 *
18847 	 * Must grab the mutex before calling sd_retry_command,
18848 	 * sd_print_sense_msg and sd_return_failed_command.
18849 	 */
18850 	mutex_enter(SD_MUTEX(un));
18851 	if (err != SD_CMD_SUCCESS) {
18852 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18853 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18854 		si.ssi_severity = SCSI_ERR_FATAL;
18855 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18856 		sd_return_failed_command(un, bp, EIO);
18857 	} else {
18858 		sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18859 		    &si, EIO, (clock_t)0, NULL);
18860 	}
18861 	mutex_exit(SD_MUTEX(un));
18862 }
18863 
18864 
18865 
18866 /*
18867  *    Function: sd_handle_mchange
18868  *
18869  * Description: Perform geometry validation & other recovery when CDROM
18870  *		has been removed from drive.
18871  *
18872  * Return Code: 0 for success
18873  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
18874  *		sd_send_scsi_READ_CAPACITY()
18875  *
18876  *     Context: Executes in a taskq() thread context
18877  */
18878 
18879 static int
18880 sd_handle_mchange(struct sd_lun *un)
18881 {
18882 	uint64_t	capacity;
18883 	uint32_t	lbasize;
18884 	int		rval;
18885 
18886 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18887 	ASSERT(un->un_f_monitor_media_state);
18888 
18889 	if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
18890 	    SD_PATH_DIRECT_PRIORITY)) != 0) {
18891 		return (rval);
18892 	}
18893 
18894 	mutex_enter(SD_MUTEX(un));
18895 	sd_update_block_info(un, lbasize, capacity);
18896 
18897 	if (un->un_errstats != NULL) {
18898 		struct	sd_errstats *stp =
18899 		    (struct sd_errstats *)un->un_errstats->ks_data;
18900 		stp->sd_capacity.value.ui64 = (uint64_t)
18901 		    ((uint64_t)un->un_blockcount *
18902 		    (uint64_t)un->un_tgt_blocksize);
18903 	}
18904 
18905 	/*
18906 	 * Note: Maybe let the strategy/partitioning chain worry about getting
18907 	 * valid geometry.
18908 	 */
18909 	un->un_f_geometry_is_valid = FALSE;
18910 	(void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY);
18911 	if (un->un_f_geometry_is_valid == FALSE) {
18912 		mutex_exit(SD_MUTEX(un));
18913 		return (EIO);
18914 	}
18915 
18916 	mutex_exit(SD_MUTEX(un));
18917 
18918 	/*
18919 	 * Try to lock the door
18920 	 */
18921 	return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
18922 	    SD_PATH_DIRECT_PRIORITY));
18923 }
18924 
18925 
18926 /*
18927  *    Function: sd_send_scsi_DOORLOCK
18928  *
18929  * Description: Issue the scsi DOOR LOCK command
18930  *
18931  *   Arguments: un    - pointer to driver soft state (unit) structure for
18932  *			this target.
18933  *		flag  - SD_REMOVAL_ALLOW
18934  *			SD_REMOVAL_PREVENT
18935  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
18936  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
18937  *			to use the USCSI "direct" chain and bypass the normal
18938  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
18939  *			command is issued as part of an error recovery action.
18940  *
18941  * Return Code: 0   - Success
18942  *		errno return code from sd_send_scsi_cmd()
18943  *
18944  *     Context: Can sleep.
18945  */
18946 
18947 static int
18948 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag)
18949 {
18950 	union scsi_cdb		cdb;
18951 	struct uscsi_cmd	ucmd_buf;
18952 	struct scsi_extended_sense	sense_buf;
18953 	int			status;
18954 
18955 	ASSERT(un != NULL);
18956 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18957 
18958 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
18959 
18960 	/* already determined doorlock is not supported, fake success */
18961 	if (un->un_f_doorlock_supported == FALSE) {
18962 		return (0);
18963 	}
18964 
18965 	bzero(&cdb, sizeof (cdb));
18966 	bzero(&ucmd_buf, sizeof (ucmd_buf));
18967 
18968 	cdb.scc_cmd = SCMD_DOORLOCK;
18969 	cdb.cdb_opaque[4] = (uchar_t)flag;
18970 
18971 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
18972 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
18973 	ucmd_buf.uscsi_bufaddr	= NULL;
18974 	ucmd_buf.uscsi_buflen	= 0;
18975 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
18976 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
18977 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
18978 	ucmd_buf.uscsi_timeout	= 15;
18979 
18980 	SD_TRACE(SD_LOG_IO, un,
18981 	    "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n");
18982 
18983 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
18984 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
18985 
18986 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
18987 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
18988 	    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
18989 		/* fake success and skip subsequent doorlock commands */
18990 		un->un_f_doorlock_supported = FALSE;
18991 		return (0);
18992 	}
18993 
18994 	return (status);
18995 }
18996 
18997 /*
18998  *    Function: sd_send_scsi_READ_CAPACITY
18999  *
19000  * Description: This routine uses the scsi READ CAPACITY command to determine
19001  *		the device capacity in number of blocks and the device native
19002  *		block size. If this function returns a failure, then the
19003  *		values in *capp and *lbap are undefined.  If the capacity
19004  *		returned is 0xffffffff then the lun is too large for a
19005  *		normal READ CAPACITY command and the results of a
19006  *		READ CAPACITY 16 will be used instead.
19007  *
19008  *   Arguments: un   - ptr to soft state struct for the target
19009  *		capp - ptr to unsigned 64-bit variable to receive the
19010  *			capacity value from the command.
19011  *		lbap - ptr to unsigned 32-bit varaible to receive the
19012  *			block size value from the command
19013  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19014  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19015  *			to use the USCSI "direct" chain and bypass the normal
19016  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19017  *			command is issued as part of an error recovery action.
19018  *
19019  * Return Code: 0   - Success
19020  *		EIO - IO error
19021  *		EACCES - Reservation conflict detected
19022  *		EAGAIN - Device is becoming ready
19023  *		errno return code from sd_send_scsi_cmd()
19024  *
19025  *     Context: Can sleep.  Blocks until command completes.
19026  */
19027 
19028 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
19029 
19030 static int
19031 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap,
19032 	int path_flag)
19033 {
19034 	struct	scsi_extended_sense	sense_buf;
19035 	struct	uscsi_cmd	ucmd_buf;
19036 	union	scsi_cdb	cdb;
19037 	uint32_t		*capacity_buf;
19038 	uint64_t		capacity;
19039 	uint32_t		lbasize;
19040 	int			status;
19041 
19042 	ASSERT(un != NULL);
19043 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19044 	ASSERT(capp != NULL);
19045 	ASSERT(lbap != NULL);
19046 
19047 	SD_TRACE(SD_LOG_IO, un,
19048 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19049 
19050 	/*
19051 	 * First send a READ_CAPACITY command to the target.
19052 	 * (This command is mandatory under SCSI-2.)
19053 	 *
19054 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
19055 	 * Medium Indicator bit is cleared.  The address field must be
19056 	 * zero if the PMI bit is zero.
19057 	 */
19058 	bzero(&cdb, sizeof (cdb));
19059 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19060 
19061 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
19062 
19063 	cdb.scc_cmd = SCMD_READ_CAPACITY;
19064 
19065 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19066 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19067 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
19068 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
19069 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19070 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19071 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19072 	ucmd_buf.uscsi_timeout	= 60;
19073 
19074 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19075 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19076 
19077 	switch (status) {
19078 	case 0:
19079 		/* Return failure if we did not get valid capacity data. */
19080 		if (ucmd_buf.uscsi_resid != 0) {
19081 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19082 			return (EIO);
19083 		}
19084 
19085 		/*
19086 		 * Read capacity and block size from the READ CAPACITY 10 data.
19087 		 * This data may be adjusted later due to device specific
19088 		 * issues.
19089 		 *
19090 		 * According to the SCSI spec, the READ CAPACITY 10
19091 		 * command returns the following:
19092 		 *
19093 		 *  bytes 0-3: Maximum logical block address available.
19094 		 *		(MSB in byte:0 & LSB in byte:3)
19095 		 *
19096 		 *  bytes 4-7: Block length in bytes
19097 		 *		(MSB in byte:4 & LSB in byte:7)
19098 		 *
19099 		 */
19100 		capacity = BE_32(capacity_buf[0]);
19101 		lbasize = BE_32(capacity_buf[1]);
19102 
19103 		/*
19104 		 * Done with capacity_buf
19105 		 */
19106 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19107 
19108 		/*
19109 		 * if the reported capacity is set to all 0xf's, then
19110 		 * this disk is too large and requires SBC-2 commands.
19111 		 * Reissue the request using READ CAPACITY 16.
19112 		 */
19113 		if (capacity == 0xffffffff) {
19114 			status = sd_send_scsi_READ_CAPACITY_16(un, &capacity,
19115 			    &lbasize, path_flag);
19116 			if (status != 0) {
19117 				return (status);
19118 			}
19119 		}
19120 		break;	/* Success! */
19121 	case EIO:
19122 		switch (ucmd_buf.uscsi_status) {
19123 		case STATUS_RESERVATION_CONFLICT:
19124 			status = EACCES;
19125 			break;
19126 		case STATUS_CHECK:
19127 			/*
19128 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
19129 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19130 			 */
19131 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19132 			    (sense_buf.es_add_code  == 0x04) &&
19133 			    (sense_buf.es_qual_code == 0x01)) {
19134 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19135 				return (EAGAIN);
19136 			}
19137 			break;
19138 		default:
19139 			break;
19140 		}
19141 		/* FALLTHRU */
19142 	default:
19143 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
19144 		return (status);
19145 	}
19146 
19147 	/*
19148 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
19149 	 * (2352 and 0 are common) so for these devices always force the value
19150 	 * to 2048 as required by the ATAPI specs.
19151 	 */
19152 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
19153 		lbasize = 2048;
19154 	}
19155 
19156 	/*
19157 	 * Get the maximum LBA value from the READ CAPACITY data.
19158 	 * Here we assume that the Partial Medium Indicator (PMI) bit
19159 	 * was cleared when issuing the command. This means that the LBA
19160 	 * returned from the device is the LBA of the last logical block
19161 	 * on the logical unit.  The actual logical block count will be
19162 	 * this value plus one.
19163 	 *
19164 	 * Currently the capacity is saved in terms of un->un_sys_blocksize,
19165 	 * so scale the capacity value to reflect this.
19166 	 */
19167 	capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize);
19168 
19169 #if defined(__i386) || defined(__amd64)
19170 	/*
19171 	 * On x86, compensate for off-by-1 error (number of sectors on
19172 	 * media)  (1175930)
19173 	 */
19174 	if (!un->un_f_has_removable_media && !un->un_f_is_hotpluggable &&
19175 	    (lbasize == un->un_sys_blocksize)) {
19176 		capacity -= 1;
19177 	}
19178 #endif
19179 
19180 	/*
19181 	 * Copy the values from the READ CAPACITY command into the space
19182 	 * provided by the caller.
19183 	 */
19184 	*capp = capacity;
19185 	*lbap = lbasize;
19186 
19187 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
19188 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19189 
19190 	/*
19191 	 * Both the lbasize and capacity from the device must be nonzero,
19192 	 * otherwise we assume that the values are not valid and return
19193 	 * failure to the caller. (4203735)
19194 	 */
19195 	if ((capacity == 0) || (lbasize == 0)) {
19196 		return (EIO);
19197 	}
19198 
19199 	return (0);
19200 }
19201 
19202 /*
19203  *    Function: sd_send_scsi_READ_CAPACITY_16
19204  *
19205  * Description: This routine uses the scsi READ CAPACITY 16 command to
19206  *		determine the device capacity in number of blocks and the
19207  *		device native block size.  If this function returns a failure,
19208  *		then the values in *capp and *lbap are undefined.
19209  *		This routine should always be called by
19210  *		sd_send_scsi_READ_CAPACITY which will appy any device
19211  *		specific adjustments to capacity and lbasize.
19212  *
19213  *   Arguments: un   - ptr to soft state struct for the target
19214  *		capp - ptr to unsigned 64-bit variable to receive the
19215  *			capacity value from the command.
19216  *		lbap - ptr to unsigned 32-bit varaible to receive the
19217  *			block size value from the command
19218  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19219  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19220  *			to use the USCSI "direct" chain and bypass the normal
19221  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
19222  *			this command is issued as part of an error recovery
19223  *			action.
19224  *
19225  * Return Code: 0   - Success
19226  *		EIO - IO error
19227  *		EACCES - Reservation conflict detected
19228  *		EAGAIN - Device is becoming ready
19229  *		errno return code from sd_send_scsi_cmd()
19230  *
19231  *     Context: Can sleep.  Blocks until command completes.
19232  */
19233 
19234 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
19235 
19236 static int
19237 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
19238 	uint32_t *lbap, int path_flag)
19239 {
19240 	struct	scsi_extended_sense	sense_buf;
19241 	struct	uscsi_cmd	ucmd_buf;
19242 	union	scsi_cdb	cdb;
19243 	uint64_t		*capacity16_buf;
19244 	uint64_t		capacity;
19245 	uint32_t		lbasize;
19246 	int			status;
19247 
19248 	ASSERT(un != NULL);
19249 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19250 	ASSERT(capp != NULL);
19251 	ASSERT(lbap != NULL);
19252 
19253 	SD_TRACE(SD_LOG_IO, un,
19254 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
19255 
19256 	/*
19257 	 * First send a READ_CAPACITY_16 command to the target.
19258 	 *
19259 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
19260 	 * Medium Indicator bit is cleared.  The address field must be
19261 	 * zero if the PMI bit is zero.
19262 	 */
19263 	bzero(&cdb, sizeof (cdb));
19264 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19265 
19266 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
19267 
19268 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19269 	ucmd_buf.uscsi_cdblen	= CDB_GROUP4;
19270 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
19271 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
19272 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19273 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
19274 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19275 	ucmd_buf.uscsi_timeout	= 60;
19276 
19277 	/*
19278 	 * Read Capacity (16) is a Service Action In command.  One
19279 	 * command byte (0x9E) is overloaded for multiple operations,
19280 	 * with the second CDB byte specifying the desired operation
19281 	 */
19282 	cdb.scc_cmd = SCMD_SVC_ACTION_IN_G4;
19283 	cdb.cdb_opaque[1] = SSVC_ACTION_READ_CAPACITY_G4;
19284 
19285 	/*
19286 	 * Fill in allocation length field
19287 	 */
19288 	FORMG4COUNT(&cdb, ucmd_buf.uscsi_buflen);
19289 
19290 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19291 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19292 
19293 	switch (status) {
19294 	case 0:
19295 		/* Return failure if we did not get valid capacity data. */
19296 		if (ucmd_buf.uscsi_resid > 20) {
19297 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19298 			return (EIO);
19299 		}
19300 
19301 		/*
19302 		 * Read capacity and block size from the READ CAPACITY 10 data.
19303 		 * This data may be adjusted later due to device specific
19304 		 * issues.
19305 		 *
19306 		 * According to the SCSI spec, the READ CAPACITY 10
19307 		 * command returns the following:
19308 		 *
19309 		 *  bytes 0-7: Maximum logical block address available.
19310 		 *		(MSB in byte:0 & LSB in byte:7)
19311 		 *
19312 		 *  bytes 8-11: Block length in bytes
19313 		 *		(MSB in byte:8 & LSB in byte:11)
19314 		 *
19315 		 */
19316 		capacity = BE_64(capacity16_buf[0]);
19317 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
19318 
19319 		/*
19320 		 * Done with capacity16_buf
19321 		 */
19322 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19323 
19324 		/*
19325 		 * if the reported capacity is set to all 0xf's, then
19326 		 * this disk is too large.  This could only happen with
19327 		 * a device that supports LBAs larger than 64 bits which
19328 		 * are not defined by any current T10 standards.
19329 		 */
19330 		if (capacity == 0xffffffffffffffff) {
19331 			return (EIO);
19332 		}
19333 		break;	/* Success! */
19334 	case EIO:
19335 		switch (ucmd_buf.uscsi_status) {
19336 		case STATUS_RESERVATION_CONFLICT:
19337 			status = EACCES;
19338 			break;
19339 		case STATUS_CHECK:
19340 			/*
19341 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
19342 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
19343 			 */
19344 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19345 			    (sense_buf.es_add_code  == 0x04) &&
19346 			    (sense_buf.es_qual_code == 0x01)) {
19347 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19348 				return (EAGAIN);
19349 			}
19350 			break;
19351 		default:
19352 			break;
19353 		}
19354 		/* FALLTHRU */
19355 	default:
19356 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19357 		return (status);
19358 	}
19359 
19360 	*capp = capacity;
19361 	*lbap = lbasize;
19362 
19363 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
19364 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19365 
19366 	return (0);
19367 }
19368 
19369 
19370 /*
19371  *    Function: sd_send_scsi_START_STOP_UNIT
19372  *
19373  * Description: Issue a scsi START STOP UNIT command to the target.
19374  *
19375  *   Arguments: un    - pointer to driver soft state (unit) structure for
19376  *			this target.
19377  *		flag  - SD_TARGET_START
19378  *			SD_TARGET_STOP
19379  *			SD_TARGET_EJECT
19380  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19381  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19382  *			to use the USCSI "direct" chain and bypass the normal
19383  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19384  *			command is issued as part of an error recovery action.
19385  *
19386  * Return Code: 0   - Success
19387  *		EIO - IO error
19388  *		EACCES - Reservation conflict detected
19389  *		ENXIO  - Not Ready, medium not present
19390  *		errno return code from sd_send_scsi_cmd()
19391  *
19392  *     Context: Can sleep.
19393  */
19394 
19395 static int
19396 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag)
19397 {
19398 	struct	scsi_extended_sense	sense_buf;
19399 	union scsi_cdb		cdb;
19400 	struct uscsi_cmd	ucmd_buf;
19401 	int			status;
19402 
19403 	ASSERT(un != NULL);
19404 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19405 
19406 	SD_TRACE(SD_LOG_IO, un,
19407 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
19408 
19409 	if (un->un_f_check_start_stop &&
19410 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
19411 	    (un->un_f_start_stop_supported != TRUE)) {
19412 		return (0);
19413 	}
19414 
19415 	bzero(&cdb, sizeof (cdb));
19416 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19417 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19418 
19419 	cdb.scc_cmd = SCMD_START_STOP;
19420 	cdb.cdb_opaque[4] = (uchar_t)flag;
19421 
19422 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19423 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19424 	ucmd_buf.uscsi_bufaddr	= NULL;
19425 	ucmd_buf.uscsi_buflen	= 0;
19426 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19427 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19428 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19429 	ucmd_buf.uscsi_timeout	= 200;
19430 
19431 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19432 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19433 
19434 	switch (status) {
19435 	case 0:
19436 		break;	/* Success! */
19437 	case EIO:
19438 		switch (ucmd_buf.uscsi_status) {
19439 		case STATUS_RESERVATION_CONFLICT:
19440 			status = EACCES;
19441 			break;
19442 		case STATUS_CHECK:
19443 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
19444 				switch (sense_buf.es_key) {
19445 				case KEY_ILLEGAL_REQUEST:
19446 					status = ENOTSUP;
19447 					break;
19448 				case KEY_NOT_READY:
19449 					if (sense_buf.es_add_code == 0x3A) {
19450 						status = ENXIO;
19451 					}
19452 					break;
19453 				default:
19454 					break;
19455 				}
19456 			}
19457 			break;
19458 		default:
19459 			break;
19460 		}
19461 		break;
19462 	default:
19463 		break;
19464 	}
19465 
19466 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
19467 
19468 	return (status);
19469 }
19470 
19471 
19472 /*
19473  *    Function: sd_start_stop_unit_callback
19474  *
19475  * Description: timeout(9F) callback to begin recovery process for a
19476  *		device that has spun down.
19477  *
19478  *   Arguments: arg - pointer to associated softstate struct.
19479  *
19480  *     Context: Executes in a timeout(9F) thread context
19481  */
19482 
19483 static void
19484 sd_start_stop_unit_callback(void *arg)
19485 {
19486 	struct sd_lun	*un = arg;
19487 	ASSERT(un != NULL);
19488 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19489 
19490 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
19491 
19492 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
19493 }
19494 
19495 
19496 /*
19497  *    Function: sd_start_stop_unit_task
19498  *
19499  * Description: Recovery procedure when a drive is spun down.
19500  *
19501  *   Arguments: arg - pointer to associated softstate struct.
19502  *
19503  *     Context: Executes in a taskq() thread context
19504  */
19505 
19506 static void
19507 sd_start_stop_unit_task(void *arg)
19508 {
19509 	struct sd_lun	*un = arg;
19510 
19511 	ASSERT(un != NULL);
19512 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19513 
19514 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
19515 
19516 	/*
19517 	 * Some unformatted drives report not ready error, no need to
19518 	 * restart if format has been initiated.
19519 	 */
19520 	mutex_enter(SD_MUTEX(un));
19521 	if (un->un_f_format_in_progress == TRUE) {
19522 		mutex_exit(SD_MUTEX(un));
19523 		return;
19524 	}
19525 	mutex_exit(SD_MUTEX(un));
19526 
19527 	/*
19528 	 * When a START STOP command is issued from here, it is part of a
19529 	 * failure recovery operation and must be issued before any other
19530 	 * commands, including any pending retries. Thus it must be sent
19531 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
19532 	 * succeeds or not, we will start I/O after the attempt.
19533 	 */
19534 	(void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
19535 	    SD_PATH_DIRECT_PRIORITY);
19536 
19537 	/*
19538 	 * The above call blocks until the START_STOP_UNIT command completes.
19539 	 * Now that it has completed, we must re-try the original IO that
19540 	 * received the NOT READY condition in the first place. There are
19541 	 * three possible conditions here:
19542 	 *
19543 	 *  (1) The original IO is on un_retry_bp.
19544 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
19545 	 *	is NULL.
19546 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
19547 	 *	points to some other, unrelated bp.
19548 	 *
19549 	 * For each case, we must call sd_start_cmds() with un_retry_bp
19550 	 * as the argument. If un_retry_bp is NULL, this will initiate
19551 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
19552 	 * then this will process the bp on un_retry_bp. That may or may not
19553 	 * be the original IO, but that does not matter: the important thing
19554 	 * is to keep the IO processing going at this point.
19555 	 *
19556 	 * Note: This is a very specific error recovery sequence associated
19557 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
19558 	 * serialize the I/O with completion of the spin-up.
19559 	 */
19560 	mutex_enter(SD_MUTEX(un));
19561 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19562 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
19563 	    un, un->un_retry_bp);
19564 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
19565 	sd_start_cmds(un, un->un_retry_bp);
19566 	mutex_exit(SD_MUTEX(un));
19567 
19568 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
19569 }
19570 
19571 
19572 /*
19573  *    Function: sd_send_scsi_INQUIRY
19574  *
19575  * Description: Issue the scsi INQUIRY command.
19576  *
19577  *   Arguments: un
19578  *		bufaddr
19579  *		buflen
19580  *		evpd
19581  *		page_code
19582  *		page_length
19583  *
19584  * Return Code: 0   - Success
19585  *		errno return code from sd_send_scsi_cmd()
19586  *
19587  *     Context: Can sleep. Does not return until command is completed.
19588  */
19589 
19590 static int
19591 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen,
19592 	uchar_t evpd, uchar_t page_code, size_t *residp)
19593 {
19594 	union scsi_cdb		cdb;
19595 	struct uscsi_cmd	ucmd_buf;
19596 	int			status;
19597 
19598 	ASSERT(un != NULL);
19599 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19600 	ASSERT(bufaddr != NULL);
19601 
19602 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
19603 
19604 	bzero(&cdb, sizeof (cdb));
19605 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19606 	bzero(bufaddr, buflen);
19607 
19608 	cdb.scc_cmd = SCMD_INQUIRY;
19609 	cdb.cdb_opaque[1] = evpd;
19610 	cdb.cdb_opaque[2] = page_code;
19611 	FORMG0COUNT(&cdb, buflen);
19612 
19613 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19614 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19615 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
19616 	ucmd_buf.uscsi_buflen	= buflen;
19617 	ucmd_buf.uscsi_rqbuf	= NULL;
19618 	ucmd_buf.uscsi_rqlen	= 0;
19619 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
19620 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
19621 
19622 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19623 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT);
19624 
19625 	if ((status == 0) && (residp != NULL)) {
19626 		*residp = ucmd_buf.uscsi_resid;
19627 	}
19628 
19629 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
19630 
19631 	return (status);
19632 }
19633 
19634 
19635 /*
19636  *    Function: sd_send_scsi_TEST_UNIT_READY
19637  *
19638  * Description: Issue the scsi TEST UNIT READY command.
19639  *		This routine can be told to set the flag USCSI_DIAGNOSE to
19640  *		prevent retrying failed commands. Use this when the intent
19641  *		is either to check for device readiness, to clear a Unit
19642  *		Attention, or to clear any outstanding sense data.
19643  *		However under specific conditions the expected behavior
19644  *		is for retries to bring a device ready, so use the flag
19645  *		with caution.
19646  *
19647  *   Arguments: un
19648  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
19649  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
19650  *			0: dont check for media present, do retries on cmd.
19651  *
19652  * Return Code: 0   - Success
19653  *		EIO - IO error
19654  *		EACCES - Reservation conflict detected
19655  *		ENXIO  - Not Ready, medium not present
19656  *		errno return code from sd_send_scsi_cmd()
19657  *
19658  *     Context: Can sleep. Does not return until command is completed.
19659  */
19660 
19661 static int
19662 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag)
19663 {
19664 	struct	scsi_extended_sense	sense_buf;
19665 	union scsi_cdb		cdb;
19666 	struct uscsi_cmd	ucmd_buf;
19667 	int			status;
19668 
19669 	ASSERT(un != NULL);
19670 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19671 
19672 	SD_TRACE(SD_LOG_IO, un,
19673 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
19674 
19675 	/*
19676 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
19677 	 * timeouts when they receive a TUR and the queue is not empty. Check
19678 	 * the configuration flag set during attach (indicating the drive has
19679 	 * this firmware bug) and un_ncmds_in_transport before issuing the
19680 	 * TUR. If there are
19681 	 * pending commands return success, this is a bit arbitrary but is ok
19682 	 * for non-removables (i.e. the eliteI disks) and non-clustering
19683 	 * configurations.
19684 	 */
19685 	if (un->un_f_cfg_tur_check == TRUE) {
19686 		mutex_enter(SD_MUTEX(un));
19687 		if (un->un_ncmds_in_transport != 0) {
19688 			mutex_exit(SD_MUTEX(un));
19689 			return (0);
19690 		}
19691 		mutex_exit(SD_MUTEX(un));
19692 	}
19693 
19694 	bzero(&cdb, sizeof (cdb));
19695 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19696 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19697 
19698 	cdb.scc_cmd = SCMD_TEST_UNIT_READY;
19699 
19700 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19701 	ucmd_buf.uscsi_cdblen	= CDB_GROUP0;
19702 	ucmd_buf.uscsi_bufaddr	= NULL;
19703 	ucmd_buf.uscsi_buflen	= 0;
19704 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19705 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19706 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19707 
19708 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
19709 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
19710 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
19711 	}
19712 	ucmd_buf.uscsi_timeout	= 60;
19713 
19714 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19715 	    UIO_SYSSPACE, UIO_SYSSPACE,
19716 	    ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD));
19717 
19718 	switch (status) {
19719 	case 0:
19720 		break;	/* Success! */
19721 	case EIO:
19722 		switch (ucmd_buf.uscsi_status) {
19723 		case STATUS_RESERVATION_CONFLICT:
19724 			status = EACCES;
19725 			break;
19726 		case STATUS_CHECK:
19727 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
19728 				break;
19729 			}
19730 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19731 			    (sense_buf.es_key == KEY_NOT_READY) &&
19732 			    (sense_buf.es_add_code == 0x3A)) {
19733 				status = ENXIO;
19734 			}
19735 			break;
19736 		default:
19737 			break;
19738 		}
19739 		break;
19740 	default:
19741 		break;
19742 	}
19743 
19744 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
19745 
19746 	return (status);
19747 }
19748 
19749 
19750 /*
19751  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
19752  *
19753  * Description: Issue the scsi PERSISTENT RESERVE IN command.
19754  *
19755  *   Arguments: un
19756  *
19757  * Return Code: 0   - Success
19758  *		EACCES
19759  *		ENOTSUP
19760  *		errno return code from sd_send_scsi_cmd()
19761  *
19762  *     Context: Can sleep. Does not return until command is completed.
19763  */
19764 
19765 static int
19766 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t  usr_cmd,
19767 	uint16_t data_len, uchar_t *data_bufp)
19768 {
19769 	struct scsi_extended_sense	sense_buf;
19770 	union scsi_cdb		cdb;
19771 	struct uscsi_cmd	ucmd_buf;
19772 	int			status;
19773 	int			no_caller_buf = FALSE;
19774 
19775 	ASSERT(un != NULL);
19776 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19777 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
19778 
19779 	SD_TRACE(SD_LOG_IO, un,
19780 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
19781 
19782 	bzero(&cdb, sizeof (cdb));
19783 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19784 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19785 	if (data_bufp == NULL) {
19786 		/* Allocate a default buf if the caller did not give one */
19787 		ASSERT(data_len == 0);
19788 		data_len  = MHIOC_RESV_KEY_SIZE;
19789 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
19790 		no_caller_buf = TRUE;
19791 	}
19792 
19793 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_IN;
19794 	cdb.cdb_opaque[1] = usr_cmd;
19795 	FORMG1COUNT(&cdb, data_len);
19796 
19797 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19798 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19799 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
19800 	ucmd_buf.uscsi_buflen	= data_len;
19801 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19802 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19803 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19804 	ucmd_buf.uscsi_timeout	= 60;
19805 
19806 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19807 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
19808 
19809 	switch (status) {
19810 	case 0:
19811 		break;	/* Success! */
19812 	case EIO:
19813 		switch (ucmd_buf.uscsi_status) {
19814 		case STATUS_RESERVATION_CONFLICT:
19815 			status = EACCES;
19816 			break;
19817 		case STATUS_CHECK:
19818 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19819 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
19820 				status = ENOTSUP;
19821 			}
19822 			break;
19823 		default:
19824 			break;
19825 		}
19826 		break;
19827 	default:
19828 		break;
19829 	}
19830 
19831 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
19832 
19833 	if (no_caller_buf == TRUE) {
19834 		kmem_free(data_bufp, data_len);
19835 	}
19836 
19837 	return (status);
19838 }
19839 
19840 
19841 /*
19842  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
19843  *
19844  * Description: This routine is the driver entry point for handling CD-ROM
19845  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
19846  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
19847  *		device.
19848  *
19849  *   Arguments: un  -   Pointer to soft state struct for the target.
19850  *		usr_cmd SCSI-3 reservation facility command (one of
19851  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
19852  *			SD_SCSI3_PREEMPTANDABORT)
19853  *		usr_bufp - user provided pointer register, reserve descriptor or
19854  *			preempt and abort structure (mhioc_register_t,
19855  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
19856  *
19857  * Return Code: 0   - Success
19858  *		EACCES
19859  *		ENOTSUP
19860  *		errno return code from sd_send_scsi_cmd()
19861  *
19862  *     Context: Can sleep. Does not return until command is completed.
19863  */
19864 
19865 static int
19866 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd,
19867 	uchar_t	*usr_bufp)
19868 {
19869 	struct scsi_extended_sense	sense_buf;
19870 	union scsi_cdb		cdb;
19871 	struct uscsi_cmd	ucmd_buf;
19872 	int			status;
19873 	uchar_t			data_len = sizeof (sd_prout_t);
19874 	sd_prout_t		*prp;
19875 
19876 	ASSERT(un != NULL);
19877 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19878 	ASSERT(data_len == 24);	/* required by scsi spec */
19879 
19880 	SD_TRACE(SD_LOG_IO, un,
19881 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
19882 
19883 	if (usr_bufp == NULL) {
19884 		return (EINVAL);
19885 	}
19886 
19887 	bzero(&cdb, sizeof (cdb));
19888 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19889 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19890 	prp = kmem_zalloc(data_len, KM_SLEEP);
19891 
19892 	cdb.scc_cmd = SCMD_PERSISTENT_RESERVE_OUT;
19893 	cdb.cdb_opaque[1] = usr_cmd;
19894 	FORMG1COUNT(&cdb, data_len);
19895 
19896 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
19897 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
19898 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
19899 	ucmd_buf.uscsi_buflen	= data_len;
19900 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19901 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19902 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
19903 	ucmd_buf.uscsi_timeout	= 60;
19904 
19905 	switch (usr_cmd) {
19906 	case SD_SCSI3_REGISTER: {
19907 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
19908 
19909 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19910 		bcopy(ptr->newkey.key, prp->service_key,
19911 		    MHIOC_RESV_KEY_SIZE);
19912 		prp->aptpl = ptr->aptpl;
19913 		break;
19914 	}
19915 	case SD_SCSI3_RESERVE:
19916 	case SD_SCSI3_RELEASE: {
19917 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
19918 
19919 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19920 		prp->scope_address = BE_32(ptr->scope_specific_addr);
19921 		cdb.cdb_opaque[2] = ptr->type;
19922 		break;
19923 	}
19924 	case SD_SCSI3_PREEMPTANDABORT: {
19925 		mhioc_preemptandabort_t *ptr =
19926 		    (mhioc_preemptandabort_t *)usr_bufp;
19927 
19928 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19929 		bcopy(ptr->victim_key.key, prp->service_key,
19930 		    MHIOC_RESV_KEY_SIZE);
19931 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
19932 		cdb.cdb_opaque[2] = ptr->resvdesc.type;
19933 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
19934 		break;
19935 	}
19936 	case SD_SCSI3_REGISTERANDIGNOREKEY:
19937 	{
19938 		mhioc_registerandignorekey_t *ptr;
19939 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
19940 		bcopy(ptr->newkey.key,
19941 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
19942 		prp->aptpl = ptr->aptpl;
19943 		break;
19944 	}
19945 	default:
19946 		ASSERT(FALSE);
19947 		break;
19948 	}
19949 
19950 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19951 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
19952 
19953 	switch (status) {
19954 	case 0:
19955 		break;	/* Success! */
19956 	case EIO:
19957 		switch (ucmd_buf.uscsi_status) {
19958 		case STATUS_RESERVATION_CONFLICT:
19959 			status = EACCES;
19960 			break;
19961 		case STATUS_CHECK:
19962 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19963 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
19964 				status = ENOTSUP;
19965 			}
19966 			break;
19967 		default:
19968 			break;
19969 		}
19970 		break;
19971 	default:
19972 		break;
19973 	}
19974 
19975 	kmem_free(prp, data_len);
19976 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
19977 	return (status);
19978 }
19979 
19980 
19981 /*
19982  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
19983  *
19984  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
19985  *
19986  *   Arguments: un - pointer to the target's soft state struct
19987  *
19988  * Return Code: 0 - success
19989  *		errno-type error code
19990  *
19991  *     Context: kernel thread context only.
19992  */
19993 
19994 static int
19995 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un, struct dk_callback *dkc)
19996 {
19997 	struct sd_uscsi_info	*uip;
19998 	struct uscsi_cmd	*uscmd;
19999 	union scsi_cdb		*cdb;
20000 	struct buf		*bp;
20001 	int			rval = 0;
20002 
20003 	SD_TRACE(SD_LOG_IO, un,
20004 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
20005 
20006 	ASSERT(un != NULL);
20007 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20008 
20009 	cdb = kmem_zalloc(CDB_GROUP1, KM_SLEEP);
20010 	cdb->scc_cmd = SCMD_SYNCHRONIZE_CACHE;
20011 
20012 	/*
20013 	 * First get some memory for the uscsi_cmd struct and cdb
20014 	 * and initialize for SYNCHRONIZE_CACHE cmd.
20015 	 */
20016 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
20017 	uscmd->uscsi_cdblen = CDB_GROUP1;
20018 	uscmd->uscsi_cdb = (caddr_t)cdb;
20019 	uscmd->uscsi_bufaddr = NULL;
20020 	uscmd->uscsi_buflen = 0;
20021 	uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
20022 	uscmd->uscsi_rqlen = SENSE_LENGTH;
20023 	uscmd->uscsi_rqresid = SENSE_LENGTH;
20024 	uscmd->uscsi_flags = USCSI_RQENABLE | USCSI_SILENT;
20025 	uscmd->uscsi_timeout = sd_io_time;
20026 
20027 	/*
20028 	 * Allocate an sd_uscsi_info struct and fill it with the info
20029 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
20030 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
20031 	 * since we allocate the buf here in this function, we do not
20032 	 * need to preserve the prior contents of b_private.
20033 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
20034 	 */
20035 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
20036 	uip->ui_flags = SD_PATH_DIRECT;
20037 	uip->ui_cmdp  = uscmd;
20038 
20039 	bp = getrbuf(KM_SLEEP);
20040 	bp->b_private = uip;
20041 
20042 	/*
20043 	 * Setup buffer to carry uscsi request.
20044 	 */
20045 	bp->b_flags  = B_BUSY;
20046 	bp->b_bcount = 0;
20047 	bp->b_blkno  = 0;
20048 
20049 	if (dkc != NULL) {
20050 		bp->b_iodone = sd_send_scsi_SYNCHRONIZE_CACHE_biodone;
20051 		uip->ui_dkc = *dkc;
20052 	}
20053 
20054 	bp->b_edev = SD_GET_DEV(un);
20055 	bp->b_dev = cmpdev(bp->b_edev);	/* maybe unnecessary? */
20056 
20057 	(void) sd_uscsi_strategy(bp);
20058 
20059 	/*
20060 	 * If synchronous request, wait for completion
20061 	 * If async just return and let b_iodone callback
20062 	 * cleanup.
20063 	 * NOTE: On return, u_ncmds_in_driver will be decremented,
20064 	 * but it was also incremented in sd_uscsi_strategy(), so
20065 	 * we should be ok.
20066 	 */
20067 	if (dkc == NULL) {
20068 		(void) biowait(bp);
20069 		rval = sd_send_scsi_SYNCHRONIZE_CACHE_biodone(bp);
20070 	}
20071 
20072 	return (rval);
20073 }
20074 
20075 
20076 static int
20077 sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp)
20078 {
20079 	struct sd_uscsi_info *uip;
20080 	struct uscsi_cmd *uscmd;
20081 	struct scsi_extended_sense *sense_buf;
20082 	struct sd_lun *un;
20083 	int status;
20084 
20085 	uip = (struct sd_uscsi_info *)(bp->b_private);
20086 	ASSERT(uip != NULL);
20087 
20088 	uscmd = uip->ui_cmdp;
20089 	ASSERT(uscmd != NULL);
20090 
20091 	sense_buf = (struct scsi_extended_sense *)uscmd->uscsi_rqbuf;
20092 	ASSERT(sense_buf != NULL);
20093 
20094 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
20095 	ASSERT(un != NULL);
20096 
20097 	status = geterror(bp);
20098 	switch (status) {
20099 	case 0:
20100 		break;	/* Success! */
20101 	case EIO:
20102 		switch (uscmd->uscsi_status) {
20103 		case STATUS_RESERVATION_CONFLICT:
20104 			/* Ignore reservation conflict */
20105 			status = 0;
20106 			goto done;
20107 
20108 		case STATUS_CHECK:
20109 			if ((uscmd->uscsi_rqstatus == STATUS_GOOD) &&
20110 			    (sense_buf->es_key == KEY_ILLEGAL_REQUEST)) {
20111 				/* Ignore Illegal Request error */
20112 				mutex_enter(SD_MUTEX(un));
20113 				un->un_f_sync_cache_supported = FALSE;
20114 				mutex_exit(SD_MUTEX(un));
20115 				status = ENOTSUP;
20116 				goto done;
20117 			}
20118 			break;
20119 		default:
20120 			break;
20121 		}
20122 		/* FALLTHRU */
20123 	default:
20124 		/* Ignore error if the media is not present */
20125 		if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) {
20126 			status = 0;
20127 			goto done;
20128 		}
20129 		/* If we reach this, we had an error */
20130 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
20131 		    "SYNCHRONIZE CACHE command failed (%d)\n", status);
20132 		break;
20133 	}
20134 
20135 done:
20136 	if (uip->ui_dkc.dkc_callback != NULL) {
20137 		(*uip->ui_dkc.dkc_callback)(uip->ui_dkc.dkc_cookie, status);
20138 	}
20139 
20140 	ASSERT((bp->b_flags & B_REMAPPED) == 0);
20141 	freerbuf(bp);
20142 	kmem_free(uip, sizeof (struct sd_uscsi_info));
20143 	kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
20144 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
20145 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
20146 
20147 	return (status);
20148 }
20149 
20150 
20151 /*
20152  *    Function: sd_send_scsi_GET_CONFIGURATION
20153  *
20154  * Description: Issues the get configuration command to the device.
20155  *		Called from sd_check_for_writable_cd & sd_get_media_info
20156  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
20157  *   Arguments: un
20158  *		ucmdbuf
20159  *		rqbuf
20160  *		rqbuflen
20161  *		bufaddr
20162  *		buflen
20163  *
20164  * Return Code: 0   - Success
20165  *		errno return code from sd_send_scsi_cmd()
20166  *
20167  *     Context: Can sleep. Does not return until command is completed.
20168  *
20169  */
20170 
20171 static int
20172 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf,
20173 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen)
20174 {
20175 	char	cdb[CDB_GROUP1];
20176 	int	status;
20177 
20178 	ASSERT(un != NULL);
20179 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20180 	ASSERT(bufaddr != NULL);
20181 	ASSERT(ucmdbuf != NULL);
20182 	ASSERT(rqbuf != NULL);
20183 
20184 	SD_TRACE(SD_LOG_IO, un,
20185 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
20186 
20187 	bzero(cdb, sizeof (cdb));
20188 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
20189 	bzero(rqbuf, rqbuflen);
20190 	bzero(bufaddr, buflen);
20191 
20192 	/*
20193 	 * Set up cdb field for the get configuration command.
20194 	 */
20195 	cdb[0] = SCMD_GET_CONFIGURATION;
20196 	cdb[1] = 0x02;  /* Requested Type */
20197 	cdb[8] = SD_PROFILE_HEADER_LEN;
20198 	ucmdbuf->uscsi_cdb = cdb;
20199 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
20200 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
20201 	ucmdbuf->uscsi_buflen = buflen;
20202 	ucmdbuf->uscsi_timeout = sd_io_time;
20203 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
20204 	ucmdbuf->uscsi_rqlen = rqbuflen;
20205 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
20206 
20207 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE,
20208 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
20209 
20210 	switch (status) {
20211 	case 0:
20212 		break;  /* Success! */
20213 	case EIO:
20214 		switch (ucmdbuf->uscsi_status) {
20215 		case STATUS_RESERVATION_CONFLICT:
20216 			status = EACCES;
20217 			break;
20218 		default:
20219 			break;
20220 		}
20221 		break;
20222 	default:
20223 		break;
20224 	}
20225 
20226 	if (status == 0) {
20227 		SD_DUMP_MEMORY(un, SD_LOG_IO,
20228 		    "sd_send_scsi_GET_CONFIGURATION: data",
20229 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
20230 	}
20231 
20232 	SD_TRACE(SD_LOG_IO, un,
20233 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
20234 
20235 	return (status);
20236 }
20237 
20238 /*
20239  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
20240  *
20241  * Description: Issues the get configuration command to the device to
20242  *              retrieve a specfic feature. Called from
20243  *		sd_check_for_writable_cd & sd_set_mmc_caps.
20244  *   Arguments: un
20245  *              ucmdbuf
20246  *              rqbuf
20247  *              rqbuflen
20248  *              bufaddr
20249  *              buflen
20250  *		feature
20251  *
20252  * Return Code: 0   - Success
20253  *              errno return code from sd_send_scsi_cmd()
20254  *
20255  *     Context: Can sleep. Does not return until command is completed.
20256  *
20257  */
20258 static int
20259 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
20260 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
20261 	uchar_t *bufaddr, uint_t buflen, char feature)
20262 {
20263 	char    cdb[CDB_GROUP1];
20264 	int	status;
20265 
20266 	ASSERT(un != NULL);
20267 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20268 	ASSERT(bufaddr != NULL);
20269 	ASSERT(ucmdbuf != NULL);
20270 	ASSERT(rqbuf != NULL);
20271 
20272 	SD_TRACE(SD_LOG_IO, un,
20273 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
20274 
20275 	bzero(cdb, sizeof (cdb));
20276 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
20277 	bzero(rqbuf, rqbuflen);
20278 	bzero(bufaddr, buflen);
20279 
20280 	/*
20281 	 * Set up cdb field for the get configuration command.
20282 	 */
20283 	cdb[0] = SCMD_GET_CONFIGURATION;
20284 	cdb[1] = 0x02;  /* Requested Type */
20285 	cdb[3] = feature;
20286 	cdb[8] = buflen;
20287 	ucmdbuf->uscsi_cdb = cdb;
20288 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
20289 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
20290 	ucmdbuf->uscsi_buflen = buflen;
20291 	ucmdbuf->uscsi_timeout = sd_io_time;
20292 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
20293 	ucmdbuf->uscsi_rqlen = rqbuflen;
20294 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
20295 
20296 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE,
20297 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
20298 
20299 	switch (status) {
20300 	case 0:
20301 		break;  /* Success! */
20302 	case EIO:
20303 		switch (ucmdbuf->uscsi_status) {
20304 		case STATUS_RESERVATION_CONFLICT:
20305 			status = EACCES;
20306 			break;
20307 		default:
20308 			break;
20309 		}
20310 		break;
20311 	default:
20312 		break;
20313 	}
20314 
20315 	if (status == 0) {
20316 		SD_DUMP_MEMORY(un, SD_LOG_IO,
20317 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
20318 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
20319 	}
20320 
20321 	SD_TRACE(SD_LOG_IO, un,
20322 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
20323 
20324 	return (status);
20325 }
20326 
20327 
20328 /*
20329  *    Function: sd_send_scsi_MODE_SENSE
20330  *
20331  * Description: Utility function for issuing a scsi MODE SENSE command.
20332  *		Note: This routine uses a consistent implementation for Group0,
20333  *		Group1, and Group2 commands across all platforms. ATAPI devices
20334  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20335  *
20336  *   Arguments: un - pointer to the softstate struct for the target.
20337  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20338  *			  CDB_GROUP[1|2] (10 byte).
20339  *		bufaddr - buffer for page data retrieved from the target.
20340  *		buflen - size of page to be retrieved.
20341  *		page_code - page code of data to be retrieved from the target.
20342  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20343  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20344  *			to use the USCSI "direct" chain and bypass the normal
20345  *			command waitq.
20346  *
20347  * Return Code: 0   - Success
20348  *		errno return code from sd_send_scsi_cmd()
20349  *
20350  *     Context: Can sleep. Does not return until command is completed.
20351  */
20352 
20353 static int
20354 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20355 	size_t buflen,  uchar_t page_code, int path_flag)
20356 {
20357 	struct	scsi_extended_sense	sense_buf;
20358 	union scsi_cdb		cdb;
20359 	struct uscsi_cmd	ucmd_buf;
20360 	int			status;
20361 
20362 	ASSERT(un != NULL);
20363 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20364 	ASSERT(bufaddr != NULL);
20365 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20366 	    (cdbsize == CDB_GROUP2));
20367 
20368 	SD_TRACE(SD_LOG_IO, un,
20369 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
20370 
20371 	bzero(&cdb, sizeof (cdb));
20372 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20373 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20374 	bzero(bufaddr, buflen);
20375 
20376 	if (cdbsize == CDB_GROUP0) {
20377 		cdb.scc_cmd = SCMD_MODE_SENSE;
20378 		cdb.cdb_opaque[2] = page_code;
20379 		FORMG0COUNT(&cdb, buflen);
20380 	} else {
20381 		cdb.scc_cmd = SCMD_MODE_SENSE_G1;
20382 		cdb.cdb_opaque[2] = page_code;
20383 		FORMG1COUNT(&cdb, buflen);
20384 	}
20385 
20386 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20387 
20388 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20389 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20390 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20391 	ucmd_buf.uscsi_buflen	= buflen;
20392 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20393 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20394 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20395 	ucmd_buf.uscsi_timeout	= 60;
20396 
20397 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20398 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20399 
20400 	switch (status) {
20401 	case 0:
20402 		break;	/* Success! */
20403 	case EIO:
20404 		switch (ucmd_buf.uscsi_status) {
20405 		case STATUS_RESERVATION_CONFLICT:
20406 			status = EACCES;
20407 			break;
20408 		default:
20409 			break;
20410 		}
20411 		break;
20412 	default:
20413 		break;
20414 	}
20415 
20416 	if (status == 0) {
20417 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
20418 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20419 	}
20420 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
20421 
20422 	return (status);
20423 }
20424 
20425 
20426 /*
20427  *    Function: sd_send_scsi_MODE_SELECT
20428  *
20429  * Description: Utility function for issuing a scsi MODE SELECT command.
20430  *		Note: This routine uses a consistent implementation for Group0,
20431  *		Group1, and Group2 commands across all platforms. ATAPI devices
20432  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20433  *
20434  *   Arguments: un - pointer to the softstate struct for the target.
20435  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20436  *			  CDB_GROUP[1|2] (10 byte).
20437  *		bufaddr - buffer for page data retrieved from the target.
20438  *		buflen - size of page to be retrieved.
20439  *		save_page - boolean to determin if SP bit should be set.
20440  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20441  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20442  *			to use the USCSI "direct" chain and bypass the normal
20443  *			command waitq.
20444  *
20445  * Return Code: 0   - Success
20446  *		errno return code from sd_send_scsi_cmd()
20447  *
20448  *     Context: Can sleep. Does not return until command is completed.
20449  */
20450 
20451 static int
20452 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20453 	size_t buflen,  uchar_t save_page, int path_flag)
20454 {
20455 	struct	scsi_extended_sense	sense_buf;
20456 	union scsi_cdb		cdb;
20457 	struct uscsi_cmd	ucmd_buf;
20458 	int			status;
20459 
20460 	ASSERT(un != NULL);
20461 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20462 	ASSERT(bufaddr != NULL);
20463 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20464 	    (cdbsize == CDB_GROUP2));
20465 
20466 	SD_TRACE(SD_LOG_IO, un,
20467 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
20468 
20469 	bzero(&cdb, sizeof (cdb));
20470 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20471 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20472 
20473 	/* Set the PF bit for many third party drives */
20474 	cdb.cdb_opaque[1] = 0x10;
20475 
20476 	/* Set the savepage(SP) bit if given */
20477 	if (save_page == SD_SAVE_PAGE) {
20478 		cdb.cdb_opaque[1] |= 0x01;
20479 	}
20480 
20481 	if (cdbsize == CDB_GROUP0) {
20482 		cdb.scc_cmd = SCMD_MODE_SELECT;
20483 		FORMG0COUNT(&cdb, buflen);
20484 	} else {
20485 		cdb.scc_cmd = SCMD_MODE_SELECT_G1;
20486 		FORMG1COUNT(&cdb, buflen);
20487 	}
20488 
20489 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20490 
20491 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20492 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20493 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20494 	ucmd_buf.uscsi_buflen	= buflen;
20495 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20496 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20497 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20498 	ucmd_buf.uscsi_timeout	= 60;
20499 
20500 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20501 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20502 
20503 	switch (status) {
20504 	case 0:
20505 		break;	/* Success! */
20506 	case EIO:
20507 		switch (ucmd_buf.uscsi_status) {
20508 		case STATUS_RESERVATION_CONFLICT:
20509 			status = EACCES;
20510 			break;
20511 		default:
20512 			break;
20513 		}
20514 		break;
20515 	default:
20516 		break;
20517 	}
20518 
20519 	if (status == 0) {
20520 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
20521 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20522 	}
20523 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
20524 
20525 	return (status);
20526 }
20527 
20528 
20529 /*
20530  *    Function: sd_send_scsi_RDWR
20531  *
20532  * Description: Issue a scsi READ or WRITE command with the given parameters.
20533  *
20534  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20535  *		cmd:	 SCMD_READ or SCMD_WRITE
20536  *		bufaddr: Address of caller's buffer to receive the RDWR data
20537  *		buflen:  Length of caller's buffer receive the RDWR data.
20538  *		start_block: Block number for the start of the RDWR operation.
20539  *			 (Assumes target-native block size.)
20540  *		residp:  Pointer to variable to receive the redisual of the
20541  *			 RDWR operation (may be NULL of no residual requested).
20542  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20543  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20544  *			to use the USCSI "direct" chain and bypass the normal
20545  *			command waitq.
20546  *
20547  * Return Code: 0   - Success
20548  *		errno return code from sd_send_scsi_cmd()
20549  *
20550  *     Context: Can sleep. Does not return until command is completed.
20551  */
20552 
20553 static int
20554 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
20555 	size_t buflen, daddr_t start_block, int path_flag)
20556 {
20557 	struct	scsi_extended_sense	sense_buf;
20558 	union scsi_cdb		cdb;
20559 	struct uscsi_cmd	ucmd_buf;
20560 	uint32_t		block_count;
20561 	int			status;
20562 	int			cdbsize;
20563 	uchar_t			flag;
20564 
20565 	ASSERT(un != NULL);
20566 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20567 	ASSERT(bufaddr != NULL);
20568 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
20569 
20570 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
20571 
20572 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
20573 		return (EINVAL);
20574 	}
20575 
20576 	mutex_enter(SD_MUTEX(un));
20577 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
20578 	mutex_exit(SD_MUTEX(un));
20579 
20580 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
20581 
20582 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
20583 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
20584 	    bufaddr, buflen, start_block, block_count);
20585 
20586 	bzero(&cdb, sizeof (cdb));
20587 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20588 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20589 
20590 	/* Compute CDB size to use */
20591 	if (start_block > 0xffffffff)
20592 		cdbsize = CDB_GROUP4;
20593 	else if ((start_block & 0xFFE00000) ||
20594 	    (un->un_f_cfg_is_atapi == TRUE))
20595 		cdbsize = CDB_GROUP1;
20596 	else
20597 		cdbsize = CDB_GROUP0;
20598 
20599 	switch (cdbsize) {
20600 	case CDB_GROUP0:	/* 6-byte CDBs */
20601 		cdb.scc_cmd = cmd;
20602 		FORMG0ADDR(&cdb, start_block);
20603 		FORMG0COUNT(&cdb, block_count);
20604 		break;
20605 	case CDB_GROUP1:	/* 10-byte CDBs */
20606 		cdb.scc_cmd = cmd | SCMD_GROUP1;
20607 		FORMG1ADDR(&cdb, start_block);
20608 		FORMG1COUNT(&cdb, block_count);
20609 		break;
20610 	case CDB_GROUP4:	/* 16-byte CDBs */
20611 		cdb.scc_cmd = cmd | SCMD_GROUP4;
20612 		FORMG4LONGADDR(&cdb, (uint64_t)start_block);
20613 		FORMG4COUNT(&cdb, block_count);
20614 		break;
20615 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
20616 	default:
20617 		/* All others reserved */
20618 		return (EINVAL);
20619 	}
20620 
20621 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
20622 	SD_FILL_SCSI1_LUN_CDB(un, &cdb);
20623 
20624 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20625 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20626 	ucmd_buf.uscsi_bufaddr	= bufaddr;
20627 	ucmd_buf.uscsi_buflen	= buflen;
20628 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20629 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20630 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
20631 	ucmd_buf.uscsi_timeout	= 60;
20632 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20633 				UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20634 	switch (status) {
20635 	case 0:
20636 		break;	/* Success! */
20637 	case EIO:
20638 		switch (ucmd_buf.uscsi_status) {
20639 		case STATUS_RESERVATION_CONFLICT:
20640 			status = EACCES;
20641 			break;
20642 		default:
20643 			break;
20644 		}
20645 		break;
20646 	default:
20647 		break;
20648 	}
20649 
20650 	if (status == 0) {
20651 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
20652 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20653 	}
20654 
20655 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
20656 
20657 	return (status);
20658 }
20659 
20660 
20661 /*
20662  *    Function: sd_send_scsi_LOG_SENSE
20663  *
20664  * Description: Issue a scsi LOG_SENSE command with the given parameters.
20665  *
20666  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20667  *
20668  * Return Code: 0   - Success
20669  *		errno return code from sd_send_scsi_cmd()
20670  *
20671  *     Context: Can sleep. Does not return until command is completed.
20672  */
20673 
20674 static int
20675 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen,
20676 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
20677 	int path_flag)
20678 
20679 {
20680 	struct	scsi_extended_sense	sense_buf;
20681 	union scsi_cdb		cdb;
20682 	struct uscsi_cmd	ucmd_buf;
20683 	int			status;
20684 
20685 	ASSERT(un != NULL);
20686 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20687 
20688 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
20689 
20690 	bzero(&cdb, sizeof (cdb));
20691 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20692 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20693 
20694 	cdb.scc_cmd = SCMD_LOG_SENSE_G1;
20695 	cdb.cdb_opaque[2] = (page_control << 6) | page_code;
20696 	cdb.cdb_opaque[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
20697 	cdb.cdb_opaque[6] = (uchar_t)(param_ptr  & 0x00FF);
20698 	FORMG1COUNT(&cdb, buflen);
20699 
20700 	ucmd_buf.uscsi_cdb	= (char *)&cdb;
20701 	ucmd_buf.uscsi_cdblen	= CDB_GROUP1;
20702 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20703 	ucmd_buf.uscsi_buflen	= buflen;
20704 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20705 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20706 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20707 	ucmd_buf.uscsi_timeout	= 60;
20708 
20709 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20710 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20711 
20712 	switch (status) {
20713 	case 0:
20714 		break;
20715 	case EIO:
20716 		switch (ucmd_buf.uscsi_status) {
20717 		case STATUS_RESERVATION_CONFLICT:
20718 			status = EACCES;
20719 			break;
20720 		case STATUS_CHECK:
20721 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20722 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST) &&
20723 			    (sense_buf.es_add_code == 0x24)) {
20724 				/*
20725 				 * ASC 0x24: INVALID FIELD IN CDB
20726 				 */
20727 				switch (page_code) {
20728 				case START_STOP_CYCLE_PAGE:
20729 					/*
20730 					 * The start stop cycle counter is
20731 					 * implemented as page 0x31 in earlier
20732 					 * generation disks. In new generation
20733 					 * disks the start stop cycle counter is
20734 					 * implemented as page 0xE. To properly
20735 					 * handle this case if an attempt for
20736 					 * log page 0xE is made and fails we
20737 					 * will try again using page 0x31.
20738 					 *
20739 					 * Network storage BU committed to
20740 					 * maintain the page 0x31 for this
20741 					 * purpose and will not have any other
20742 					 * page implemented with page code 0x31
20743 					 * until all disks transition to the
20744 					 * standard page.
20745 					 */
20746 					mutex_enter(SD_MUTEX(un));
20747 					un->un_start_stop_cycle_page =
20748 					    START_STOP_CYCLE_VU_PAGE;
20749 					cdb.cdb_opaque[2] =
20750 					    (char)(page_control << 6) |
20751 					    un->un_start_stop_cycle_page;
20752 					mutex_exit(SD_MUTEX(un));
20753 					status = sd_send_scsi_cmd(
20754 					    SD_GET_DEV(un), &ucmd_buf,
20755 					    UIO_SYSSPACE, UIO_SYSSPACE,
20756 					    UIO_SYSSPACE, path_flag);
20757 
20758 					break;
20759 				case TEMPERATURE_PAGE:
20760 					status = ENOTTY;
20761 					break;
20762 				default:
20763 					break;
20764 				}
20765 			}
20766 			break;
20767 		default:
20768 			break;
20769 		}
20770 		break;
20771 	default:
20772 		break;
20773 	}
20774 
20775 	if (status == 0) {
20776 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
20777 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20778 	}
20779 
20780 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
20781 
20782 	return (status);
20783 }
20784 
20785 
20786 /*
20787  *    Function: sdioctl
20788  *
20789  * Description: Driver's ioctl(9e) entry point function.
20790  *
20791  *   Arguments: dev     - device number
20792  *		cmd     - ioctl operation to be performed
20793  *		arg     - user argument, contains data to be set or reference
20794  *			  parameter for get
20795  *		flag    - bit flag, indicating open settings, 32/64 bit type
20796  *		cred_p  - user credential pointer
20797  *		rval_p  - calling process return value (OPT)
20798  *
20799  * Return Code: EINVAL
20800  *		ENOTTY
20801  *		ENXIO
20802  *		EIO
20803  *		EFAULT
20804  *		ENOTSUP
20805  *		EPERM
20806  *
20807  *     Context: Called from the device switch at normal priority.
20808  */
20809 
20810 static int
20811 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
20812 {
20813 	struct sd_lun	*un = NULL;
20814 	int		geom_validated = FALSE;
20815 	int		err = 0;
20816 	int		i = 0;
20817 	cred_t		*cr;
20818 
20819 	/*
20820 	 * All device accesses go thru sdstrategy where we check on suspend
20821 	 * status
20822 	 */
20823 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
20824 		return (ENXIO);
20825 	}
20826 
20827 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20828 
20829 	/*
20830 	 * Moved this wait from sd_uscsi_strategy to here for
20831 	 * reasons of deadlock prevention. Internal driver commands,
20832 	 * specifically those to change a devices power level, result
20833 	 * in a call to sd_uscsi_strategy.
20834 	 */
20835 	mutex_enter(SD_MUTEX(un));
20836 	while ((un->un_state == SD_STATE_SUSPENDED) ||
20837 	    (un->un_state == SD_STATE_PM_CHANGING)) {
20838 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
20839 	}
20840 	/*
20841 	 * Twiddling the counter here protects commands from now
20842 	 * through to the top of sd_uscsi_strategy. Without the
20843 	 * counter inc. a power down, for example, could get in
20844 	 * after the above check for state is made and before
20845 	 * execution gets to the top of sd_uscsi_strategy.
20846 	 * That would cause problems.
20847 	 */
20848 	un->un_ncmds_in_driver++;
20849 
20850 	if ((un->un_f_geometry_is_valid == FALSE) &&
20851 	    (flag & (FNDELAY | FNONBLOCK))) {
20852 		switch (cmd) {
20853 		case CDROMPAUSE:
20854 		case CDROMRESUME:
20855 		case CDROMPLAYMSF:
20856 		case CDROMPLAYTRKIND:
20857 		case CDROMREADTOCHDR:
20858 		case CDROMREADTOCENTRY:
20859 		case CDROMSTOP:
20860 		case CDROMSTART:
20861 		case CDROMVOLCTRL:
20862 		case CDROMSUBCHNL:
20863 		case CDROMREADMODE2:
20864 		case CDROMREADMODE1:
20865 		case CDROMREADOFFSET:
20866 		case CDROMSBLKMODE:
20867 		case CDROMGBLKMODE:
20868 		case CDROMGDRVSPEED:
20869 		case CDROMSDRVSPEED:
20870 		case CDROMCDDA:
20871 		case CDROMCDXA:
20872 		case CDROMSUBCODE:
20873 			if (!ISCD(un)) {
20874 				un->un_ncmds_in_driver--;
20875 				ASSERT(un->un_ncmds_in_driver >= 0);
20876 				mutex_exit(SD_MUTEX(un));
20877 				return (ENOTTY);
20878 			}
20879 			break;
20880 		case FDEJECT:
20881 		case DKIOCEJECT:
20882 		case CDROMEJECT:
20883 			if (!un->un_f_eject_media_supported) {
20884 				un->un_ncmds_in_driver--;
20885 				ASSERT(un->un_ncmds_in_driver >= 0);
20886 				mutex_exit(SD_MUTEX(un));
20887 				return (ENOTTY);
20888 			}
20889 			break;
20890 		case DKIOCSVTOC:
20891 		case DKIOCSETEFI:
20892 		case DKIOCSMBOOT:
20893 		case DKIOCFLUSHWRITECACHE:
20894 			mutex_exit(SD_MUTEX(un));
20895 			err = sd_send_scsi_TEST_UNIT_READY(un, 0);
20896 			if (err != 0) {
20897 				mutex_enter(SD_MUTEX(un));
20898 				un->un_ncmds_in_driver--;
20899 				ASSERT(un->un_ncmds_in_driver >= 0);
20900 				mutex_exit(SD_MUTEX(un));
20901 				return (EIO);
20902 			}
20903 			mutex_enter(SD_MUTEX(un));
20904 			/* FALLTHROUGH */
20905 		case DKIOCREMOVABLE:
20906 		case DKIOCHOTPLUGGABLE:
20907 		case DKIOCINFO:
20908 		case DKIOCGMEDIAINFO:
20909 		case MHIOCENFAILFAST:
20910 		case MHIOCSTATUS:
20911 		case MHIOCTKOWN:
20912 		case MHIOCRELEASE:
20913 		case MHIOCGRP_INKEYS:
20914 		case MHIOCGRP_INRESV:
20915 		case MHIOCGRP_REGISTER:
20916 		case MHIOCGRP_RESERVE:
20917 		case MHIOCGRP_PREEMPTANDABORT:
20918 		case MHIOCGRP_REGISTERANDIGNOREKEY:
20919 		case CDROMCLOSETRAY:
20920 		case USCSICMD:
20921 			goto skip_ready_valid;
20922 		default:
20923 			break;
20924 		}
20925 
20926 		mutex_exit(SD_MUTEX(un));
20927 		err = sd_ready_and_valid(un);
20928 		mutex_enter(SD_MUTEX(un));
20929 		if (err == SD_READY_NOT_VALID) {
20930 			switch (cmd) {
20931 			case DKIOCGAPART:
20932 			case DKIOCGGEOM:
20933 			case DKIOCSGEOM:
20934 			case DKIOCGVTOC:
20935 			case DKIOCSVTOC:
20936 			case DKIOCSAPART:
20937 			case DKIOCG_PHYGEOM:
20938 			case DKIOCG_VIRTGEOM:
20939 				err = ENOTSUP;
20940 				un->un_ncmds_in_driver--;
20941 				ASSERT(un->un_ncmds_in_driver >= 0);
20942 				mutex_exit(SD_MUTEX(un));
20943 				return (err);
20944 			}
20945 		}
20946 		if (err != SD_READY_VALID) {
20947 			switch (cmd) {
20948 			case DKIOCSTATE:
20949 			case CDROMGDRVSPEED:
20950 			case CDROMSDRVSPEED:
20951 			case FDEJECT:	/* for eject command */
20952 			case DKIOCEJECT:
20953 			case CDROMEJECT:
20954 			case DKIOCGETEFI:
20955 			case DKIOCSGEOM:
20956 			case DKIOCREMOVABLE:
20957 			case DKIOCHOTPLUGGABLE:
20958 			case DKIOCSAPART:
20959 			case DKIOCSETEFI:
20960 				break;
20961 			default:
20962 				if (un->un_f_has_removable_media) {
20963 					err = ENXIO;
20964 				} else {
20965 					/* Do not map EACCES to EIO */
20966 					if (err != EACCES)
20967 						err = EIO;
20968 				}
20969 				un->un_ncmds_in_driver--;
20970 				ASSERT(un->un_ncmds_in_driver >= 0);
20971 				mutex_exit(SD_MUTEX(un));
20972 				return (err);
20973 			}
20974 		}
20975 		geom_validated = TRUE;
20976 	}
20977 	if ((un->un_f_geometry_is_valid == TRUE) &&
20978 	    (un->un_solaris_size > 0)) {
20979 		/*
20980 		 * the "geometry_is_valid" flag could be true if we
20981 		 * have an fdisk table but no Solaris partition
20982 		 */
20983 		if (un->un_vtoc.v_sanity != VTOC_SANE) {
20984 			/* it is EFI, so return ENOTSUP for these */
20985 			switch (cmd) {
20986 			case DKIOCGAPART:
20987 			case DKIOCGGEOM:
20988 			case DKIOCGVTOC:
20989 			case DKIOCSVTOC:
20990 			case DKIOCSAPART:
20991 				err = ENOTSUP;
20992 				un->un_ncmds_in_driver--;
20993 				ASSERT(un->un_ncmds_in_driver >= 0);
20994 				mutex_exit(SD_MUTEX(un));
20995 				return (err);
20996 			}
20997 		}
20998 	}
20999 
21000 skip_ready_valid:
21001 	mutex_exit(SD_MUTEX(un));
21002 
21003 	switch (cmd) {
21004 	case DKIOCINFO:
21005 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
21006 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
21007 		break;
21008 
21009 	case DKIOCGMEDIAINFO:
21010 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
21011 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
21012 		break;
21013 
21014 	case DKIOCGGEOM:
21015 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n");
21016 		err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag,
21017 		    geom_validated);
21018 		break;
21019 
21020 	case DKIOCSGEOM:
21021 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n");
21022 		err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag);
21023 		break;
21024 
21025 	case DKIOCGAPART:
21026 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n");
21027 		err = sd_dkio_get_partition(dev, (caddr_t)arg, flag,
21028 		    geom_validated);
21029 		break;
21030 
21031 	case DKIOCSAPART:
21032 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n");
21033 		err = sd_dkio_set_partition(dev, (caddr_t)arg, flag);
21034 		break;
21035 
21036 	case DKIOCGVTOC:
21037 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n");
21038 		err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag,
21039 		    geom_validated);
21040 		break;
21041 
21042 	case DKIOCGETEFI:
21043 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n");
21044 		err = sd_dkio_get_efi(dev, (caddr_t)arg, flag);
21045 		break;
21046 
21047 	case DKIOCPARTITION:
21048 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n");
21049 		err = sd_dkio_partition(dev, (caddr_t)arg, flag);
21050 		break;
21051 
21052 	case DKIOCSVTOC:
21053 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n");
21054 		err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag);
21055 		break;
21056 
21057 	case DKIOCSETEFI:
21058 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n");
21059 		err = sd_dkio_set_efi(dev, (caddr_t)arg, flag);
21060 		break;
21061 
21062 	case DKIOCGMBOOT:
21063 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n");
21064 		err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag);
21065 		break;
21066 
21067 	case DKIOCSMBOOT:
21068 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n");
21069 		err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag);
21070 		break;
21071 
21072 	case DKIOCLOCK:
21073 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
21074 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
21075 		    SD_PATH_STANDARD);
21076 		break;
21077 
21078 	case DKIOCUNLOCK:
21079 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
21080 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
21081 		    SD_PATH_STANDARD);
21082 		break;
21083 
21084 	case DKIOCSTATE: {
21085 		enum dkio_state		state;
21086 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
21087 
21088 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
21089 			err = EFAULT;
21090 		} else {
21091 			err = sd_check_media(dev, state);
21092 			if (err == 0) {
21093 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
21094 				    sizeof (int), flag) != 0)
21095 					err = EFAULT;
21096 			}
21097 		}
21098 		break;
21099 	}
21100 
21101 	case DKIOCREMOVABLE:
21102 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
21103 		/*
21104 		 * At present, vold only does automount for removable-media
21105 		 * devices, in order not to break current applications, we
21106 		 * still let hopluggable devices pretend to be removable media
21107 		 * devices for vold. In the near future, once vold is EOL'ed,
21108 		 * we should remove this workaround.
21109 		 */
21110 		if (un->un_f_has_removable_media || un->un_f_is_hotpluggable) {
21111 			i = 1;
21112 		} else {
21113 			i = 0;
21114 		}
21115 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
21116 			err = EFAULT;
21117 		} else {
21118 			err = 0;
21119 		}
21120 		break;
21121 
21122 	case DKIOCHOTPLUGGABLE:
21123 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCHOTPLUGGABLE\n");
21124 		if (un->un_f_is_hotpluggable) {
21125 			i = 1;
21126 		} else {
21127 			i = 0;
21128 		}
21129 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
21130 			err = EFAULT;
21131 		} else {
21132 			err = 0;
21133 		}
21134 		break;
21135 
21136 	case DKIOCGTEMPERATURE:
21137 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
21138 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
21139 		break;
21140 
21141 	case MHIOCENFAILFAST:
21142 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
21143 		if ((err = drv_priv(cred_p)) == 0) {
21144 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
21145 		}
21146 		break;
21147 
21148 	case MHIOCTKOWN:
21149 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
21150 		if ((err = drv_priv(cred_p)) == 0) {
21151 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
21152 		}
21153 		break;
21154 
21155 	case MHIOCRELEASE:
21156 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
21157 		if ((err = drv_priv(cred_p)) == 0) {
21158 			err = sd_mhdioc_release(dev);
21159 		}
21160 		break;
21161 
21162 	case MHIOCSTATUS:
21163 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
21164 		if ((err = drv_priv(cred_p)) == 0) {
21165 			switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) {
21166 			case 0:
21167 				err = 0;
21168 				break;
21169 			case EACCES:
21170 				*rval_p = 1;
21171 				err = 0;
21172 				break;
21173 			default:
21174 				err = EIO;
21175 				break;
21176 			}
21177 		}
21178 		break;
21179 
21180 	case MHIOCQRESERVE:
21181 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
21182 		if ((err = drv_priv(cred_p)) == 0) {
21183 			err = sd_reserve_release(dev, SD_RESERVE);
21184 		}
21185 		break;
21186 
21187 	case MHIOCREREGISTERDEVID:
21188 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
21189 		if (drv_priv(cred_p) == EPERM) {
21190 			err = EPERM;
21191 		} else if (!un->un_f_devid_supported) {
21192 			err = ENOTTY;
21193 		} else {
21194 			err = sd_mhdioc_register_devid(dev);
21195 		}
21196 		break;
21197 
21198 	case MHIOCGRP_INKEYS:
21199 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
21200 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
21201 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21202 				err = ENOTSUP;
21203 			} else {
21204 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
21205 				    flag);
21206 			}
21207 		}
21208 		break;
21209 
21210 	case MHIOCGRP_INRESV:
21211 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
21212 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
21213 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21214 				err = ENOTSUP;
21215 			} else {
21216 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
21217 			}
21218 		}
21219 		break;
21220 
21221 	case MHIOCGRP_REGISTER:
21222 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
21223 		if ((err = drv_priv(cred_p)) != EPERM) {
21224 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21225 				err = ENOTSUP;
21226 			} else if (arg != NULL) {
21227 				mhioc_register_t reg;
21228 				if (ddi_copyin((void *)arg, &reg,
21229 				    sizeof (mhioc_register_t), flag) != 0) {
21230 					err = EFAULT;
21231 				} else {
21232 					err =
21233 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21234 					    un, SD_SCSI3_REGISTER,
21235 					    (uchar_t *)&reg);
21236 				}
21237 			}
21238 		}
21239 		break;
21240 
21241 	case MHIOCGRP_RESERVE:
21242 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
21243 		if ((err = drv_priv(cred_p)) != EPERM) {
21244 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21245 				err = ENOTSUP;
21246 			} else if (arg != NULL) {
21247 				mhioc_resv_desc_t resv_desc;
21248 				if (ddi_copyin((void *)arg, &resv_desc,
21249 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
21250 					err = EFAULT;
21251 				} else {
21252 					err =
21253 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21254 					    un, SD_SCSI3_RESERVE,
21255 					    (uchar_t *)&resv_desc);
21256 				}
21257 			}
21258 		}
21259 		break;
21260 
21261 	case MHIOCGRP_PREEMPTANDABORT:
21262 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
21263 		if ((err = drv_priv(cred_p)) != EPERM) {
21264 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21265 				err = ENOTSUP;
21266 			} else if (arg != NULL) {
21267 				mhioc_preemptandabort_t preempt_abort;
21268 				if (ddi_copyin((void *)arg, &preempt_abort,
21269 				    sizeof (mhioc_preemptandabort_t),
21270 				    flag) != 0) {
21271 					err = EFAULT;
21272 				} else {
21273 					err =
21274 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21275 					    un, SD_SCSI3_PREEMPTANDABORT,
21276 					    (uchar_t *)&preempt_abort);
21277 				}
21278 			}
21279 		}
21280 		break;
21281 
21282 	case MHIOCGRP_REGISTERANDIGNOREKEY:
21283 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
21284 		if ((err = drv_priv(cred_p)) != EPERM) {
21285 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
21286 				err = ENOTSUP;
21287 			} else if (arg != NULL) {
21288 				mhioc_registerandignorekey_t r_and_i;
21289 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
21290 				    sizeof (mhioc_registerandignorekey_t),
21291 				    flag) != 0) {
21292 					err = EFAULT;
21293 				} else {
21294 					err =
21295 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
21296 					    un, SD_SCSI3_REGISTERANDIGNOREKEY,
21297 					    (uchar_t *)&r_and_i);
21298 				}
21299 			}
21300 		}
21301 		break;
21302 
21303 	case USCSICMD:
21304 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
21305 		cr = ddi_get_cred();
21306 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
21307 			err = EPERM;
21308 		} else {
21309 			err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag);
21310 		}
21311 		break;
21312 
21313 	case CDROMPAUSE:
21314 	case CDROMRESUME:
21315 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
21316 		if (!ISCD(un)) {
21317 			err = ENOTTY;
21318 		} else {
21319 			err = sr_pause_resume(dev, cmd);
21320 		}
21321 		break;
21322 
21323 	case CDROMPLAYMSF:
21324 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
21325 		if (!ISCD(un)) {
21326 			err = ENOTTY;
21327 		} else {
21328 			err = sr_play_msf(dev, (caddr_t)arg, flag);
21329 		}
21330 		break;
21331 
21332 	case CDROMPLAYTRKIND:
21333 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
21334 #if defined(__i386) || defined(__amd64)
21335 		/*
21336 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
21337 		 */
21338 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21339 #else
21340 		if (!ISCD(un)) {
21341 #endif
21342 			err = ENOTTY;
21343 		} else {
21344 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
21345 		}
21346 		break;
21347 
21348 	case CDROMREADTOCHDR:
21349 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
21350 		if (!ISCD(un)) {
21351 			err = ENOTTY;
21352 		} else {
21353 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
21354 		}
21355 		break;
21356 
21357 	case CDROMREADTOCENTRY:
21358 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
21359 		if (!ISCD(un)) {
21360 			err = ENOTTY;
21361 		} else {
21362 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
21363 		}
21364 		break;
21365 
21366 	case CDROMSTOP:
21367 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
21368 		if (!ISCD(un)) {
21369 			err = ENOTTY;
21370 		} else {
21371 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP,
21372 			    SD_PATH_STANDARD);
21373 		}
21374 		break;
21375 
21376 	case CDROMSTART:
21377 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
21378 		if (!ISCD(un)) {
21379 			err = ENOTTY;
21380 		} else {
21381 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
21382 			    SD_PATH_STANDARD);
21383 		}
21384 		break;
21385 
21386 	case CDROMCLOSETRAY:
21387 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
21388 		if (!ISCD(un)) {
21389 			err = ENOTTY;
21390 		} else {
21391 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE,
21392 			    SD_PATH_STANDARD);
21393 		}
21394 		break;
21395 
21396 	case FDEJECT:	/* for eject command */
21397 	case DKIOCEJECT:
21398 	case CDROMEJECT:
21399 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
21400 		if (!un->un_f_eject_media_supported) {
21401 			err = ENOTTY;
21402 		} else {
21403 			err = sr_eject(dev);
21404 		}
21405 		break;
21406 
21407 	case CDROMVOLCTRL:
21408 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
21409 		if (!ISCD(un)) {
21410 			err = ENOTTY;
21411 		} else {
21412 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
21413 		}
21414 		break;
21415 
21416 	case CDROMSUBCHNL:
21417 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
21418 		if (!ISCD(un)) {
21419 			err = ENOTTY;
21420 		} else {
21421 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
21422 		}
21423 		break;
21424 
21425 	case CDROMREADMODE2:
21426 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
21427 		if (!ISCD(un)) {
21428 			err = ENOTTY;
21429 		} else if (un->un_f_cfg_is_atapi == TRUE) {
21430 			/*
21431 			 * If the drive supports READ CD, use that instead of
21432 			 * switching the LBA size via a MODE SELECT
21433 			 * Block Descriptor
21434 			 */
21435 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
21436 		} else {
21437 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
21438 		}
21439 		break;
21440 
21441 	case CDROMREADMODE1:
21442 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
21443 		if (!ISCD(un)) {
21444 			err = ENOTTY;
21445 		} else {
21446 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
21447 		}
21448 		break;
21449 
21450 	case CDROMREADOFFSET:
21451 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
21452 		if (!ISCD(un)) {
21453 			err = ENOTTY;
21454 		} else {
21455 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
21456 			    flag);
21457 		}
21458 		break;
21459 
21460 	case CDROMSBLKMODE:
21461 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
21462 		/*
21463 		 * There is no means of changing block size in case of atapi
21464 		 * drives, thus return ENOTTY if drive type is atapi
21465 		 */
21466 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21467 			err = ENOTTY;
21468 		} else if (un->un_f_mmc_cap == TRUE) {
21469 
21470 			/*
21471 			 * MMC Devices do not support changing the
21472 			 * logical block size
21473 			 *
21474 			 * Note: EINVAL is being returned instead of ENOTTY to
21475 			 * maintain consistancy with the original mmc
21476 			 * driver update.
21477 			 */
21478 			err = EINVAL;
21479 		} else {
21480 			mutex_enter(SD_MUTEX(un));
21481 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
21482 			    (un->un_ncmds_in_transport > 0)) {
21483 				mutex_exit(SD_MUTEX(un));
21484 				err = EINVAL;
21485 			} else {
21486 				mutex_exit(SD_MUTEX(un));
21487 				err = sr_change_blkmode(dev, cmd, arg, flag);
21488 			}
21489 		}
21490 		break;
21491 
21492 	case CDROMGBLKMODE:
21493 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
21494 		if (!ISCD(un)) {
21495 			err = ENOTTY;
21496 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
21497 		    (un->un_f_blockcount_is_valid != FALSE)) {
21498 			/*
21499 			 * Drive is an ATAPI drive so return target block
21500 			 * size for ATAPI drives since we cannot change the
21501 			 * blocksize on ATAPI drives. Used primarily to detect
21502 			 * if an ATAPI cdrom is present.
21503 			 */
21504 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
21505 			    sizeof (int), flag) != 0) {
21506 				err = EFAULT;
21507 			} else {
21508 				err = 0;
21509 			}
21510 
21511 		} else {
21512 			/*
21513 			 * Drive supports changing block sizes via a Mode
21514 			 * Select.
21515 			 */
21516 			err = sr_change_blkmode(dev, cmd, arg, flag);
21517 		}
21518 		break;
21519 
21520 	case CDROMGDRVSPEED:
21521 	case CDROMSDRVSPEED:
21522 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
21523 		if (!ISCD(un)) {
21524 			err = ENOTTY;
21525 		} else if (un->un_f_mmc_cap == TRUE) {
21526 			/*
21527 			 * Note: In the future the driver implementation
21528 			 * for getting and
21529 			 * setting cd speed should entail:
21530 			 * 1) If non-mmc try the Toshiba mode page
21531 			 *    (sr_change_speed)
21532 			 * 2) If mmc but no support for Real Time Streaming try
21533 			 *    the SET CD SPEED (0xBB) command
21534 			 *   (sr_atapi_change_speed)
21535 			 * 3) If mmc and support for Real Time Streaming
21536 			 *    try the GET PERFORMANCE and SET STREAMING
21537 			 *    commands (not yet implemented, 4380808)
21538 			 */
21539 			/*
21540 			 * As per recent MMC spec, CD-ROM speed is variable
21541 			 * and changes with LBA. Since there is no such
21542 			 * things as drive speed now, fail this ioctl.
21543 			 *
21544 			 * Note: EINVAL is returned for consistancy of original
21545 			 * implementation which included support for getting
21546 			 * the drive speed of mmc devices but not setting
21547 			 * the drive speed. Thus EINVAL would be returned
21548 			 * if a set request was made for an mmc device.
21549 			 * We no longer support get or set speed for
21550 			 * mmc but need to remain consistant with regard
21551 			 * to the error code returned.
21552 			 */
21553 			err = EINVAL;
21554 		} else if (un->un_f_cfg_is_atapi == TRUE) {
21555 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
21556 		} else {
21557 			err = sr_change_speed(dev, cmd, arg, flag);
21558 		}
21559 		break;
21560 
21561 	case CDROMCDDA:
21562 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
21563 		if (!ISCD(un)) {
21564 			err = ENOTTY;
21565 		} else {
21566 			err = sr_read_cdda(dev, (void *)arg, flag);
21567 		}
21568 		break;
21569 
21570 	case CDROMCDXA:
21571 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
21572 		if (!ISCD(un)) {
21573 			err = ENOTTY;
21574 		} else {
21575 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
21576 		}
21577 		break;
21578 
21579 	case CDROMSUBCODE:
21580 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
21581 		if (!ISCD(un)) {
21582 			err = ENOTTY;
21583 		} else {
21584 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
21585 		}
21586 		break;
21587 
21588 	case DKIOCPARTINFO: {
21589 		/*
21590 		 * Return parameters describing the selected disk slice.
21591 		 * Note: this ioctl is for the intel platform only
21592 		 */
21593 #if defined(__i386) || defined(__amd64)
21594 		int part;
21595 
21596 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
21597 		part = SDPART(dev);
21598 
21599 		/* don't check un_solaris_size for pN */
21600 		if (part < P0_RAW_DISK && un->un_solaris_size == 0) {
21601 			err = EIO;
21602 		} else {
21603 			struct part_info p;
21604 
21605 			p.p_start = (daddr_t)un->un_offset[part];
21606 			p.p_length = (int)un->un_map[part].dkl_nblk;
21607 #ifdef _MULTI_DATAMODEL
21608 			switch (ddi_model_convert_from(flag & FMODELS)) {
21609 			case DDI_MODEL_ILP32:
21610 			{
21611 				struct part_info32 p32;
21612 
21613 				p32.p_start = (daddr32_t)p.p_start;
21614 				p32.p_length = p.p_length;
21615 				if (ddi_copyout(&p32, (void *)arg,
21616 				    sizeof (p32), flag))
21617 					err = EFAULT;
21618 				break;
21619 			}
21620 
21621 			case DDI_MODEL_NONE:
21622 			{
21623 				if (ddi_copyout(&p, (void *)arg, sizeof (p),
21624 				    flag))
21625 					err = EFAULT;
21626 				break;
21627 			}
21628 			}
21629 #else /* ! _MULTI_DATAMODEL */
21630 			if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
21631 				err = EFAULT;
21632 #endif /* _MULTI_DATAMODEL */
21633 		}
21634 #else
21635 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
21636 		err = ENOTTY;
21637 #endif
21638 		break;
21639 	}
21640 
21641 	case DKIOCG_PHYGEOM: {
21642 		/* Return the driver's notion of the media physical geometry */
21643 #if defined(__i386) || defined(__amd64)
21644 		struct dk_geom	disk_geom;
21645 		struct dk_geom	*dkgp = &disk_geom;
21646 
21647 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
21648 		mutex_enter(SD_MUTEX(un));
21649 
21650 		if (un->un_g.dkg_nhead != 0 &&
21651 		    un->un_g.dkg_nsect != 0) {
21652 			/*
21653 			 * We succeeded in getting a geometry, but
21654 			 * right now it is being reported as just the
21655 			 * Solaris fdisk partition, just like for
21656 			 * DKIOCGGEOM. We need to change that to be
21657 			 * correct for the entire disk now.
21658 			 */
21659 			bcopy(&un->un_g, dkgp, sizeof (*dkgp));
21660 			dkgp->dkg_acyl = 0;
21661 			dkgp->dkg_ncyl = un->un_blockcount /
21662 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
21663 		} else {
21664 			bzero(dkgp, sizeof (struct dk_geom));
21665 			/*
21666 			 * This disk does not have a Solaris VTOC
21667 			 * so we must present a physical geometry
21668 			 * that will remain consistent regardless
21669 			 * of how the disk is used. This will ensure
21670 			 * that the geometry does not change regardless
21671 			 * of the fdisk partition type (ie. EFI, FAT32,
21672 			 * Solaris, etc).
21673 			 */
21674 			if (ISCD(un)) {
21675 				dkgp->dkg_nhead = un->un_pgeom.g_nhead;
21676 				dkgp->dkg_nsect = un->un_pgeom.g_nsect;
21677 				dkgp->dkg_ncyl = un->un_pgeom.g_ncyl;
21678 				dkgp->dkg_acyl = un->un_pgeom.g_acyl;
21679 			} else {
21680 				/*
21681 				 * Invalid un_blockcount can generate invalid
21682 				 * dk_geom and may result in division by zero
21683 				 * system failure. Should make sure blockcount
21684 				 * is valid before using it here.
21685 				 */
21686 				if (un->un_f_blockcount_is_valid == FALSE) {
21687 					mutex_exit(SD_MUTEX(un));
21688 					err = EIO;
21689 
21690 					break;
21691 				}
21692 				sd_convert_geometry(un->un_blockcount, dkgp);
21693 				dkgp->dkg_acyl = 0;
21694 				dkgp->dkg_ncyl = un->un_blockcount /
21695 				    (dkgp->dkg_nhead * dkgp->dkg_nsect);
21696 			}
21697 		}
21698 		dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
21699 
21700 		if (ddi_copyout(dkgp, (void *)arg,
21701 		    sizeof (struct dk_geom), flag)) {
21702 			mutex_exit(SD_MUTEX(un));
21703 			err = EFAULT;
21704 		} else {
21705 			mutex_exit(SD_MUTEX(un));
21706 			err = 0;
21707 		}
21708 #else
21709 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
21710 		err = ENOTTY;
21711 #endif
21712 		break;
21713 	}
21714 
21715 	case DKIOCG_VIRTGEOM: {
21716 		/* Return the driver's notion of the media's logical geometry */
21717 #if defined(__i386) || defined(__amd64)
21718 		struct dk_geom	disk_geom;
21719 		struct dk_geom	*dkgp = &disk_geom;
21720 
21721 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
21722 		mutex_enter(SD_MUTEX(un));
21723 		/*
21724 		 * If there is no HBA geometry available, or
21725 		 * if the HBA returned us something that doesn't
21726 		 * really fit into an Int 13/function 8 geometry
21727 		 * result, just fail the ioctl.  See PSARC 1998/313.
21728 		 */
21729 		if (un->un_lgeom.g_nhead == 0 ||
21730 		    un->un_lgeom.g_nsect == 0 ||
21731 		    un->un_lgeom.g_ncyl > 1024) {
21732 			mutex_exit(SD_MUTEX(un));
21733 			err = EINVAL;
21734 		} else {
21735 			dkgp->dkg_ncyl	= un->un_lgeom.g_ncyl;
21736 			dkgp->dkg_acyl	= un->un_lgeom.g_acyl;
21737 			dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
21738 			dkgp->dkg_nhead	= un->un_lgeom.g_nhead;
21739 			dkgp->dkg_nsect	= un->un_lgeom.g_nsect;
21740 
21741 			if (ddi_copyout(dkgp, (void *)arg,
21742 			    sizeof (struct dk_geom), flag)) {
21743 				mutex_exit(SD_MUTEX(un));
21744 				err = EFAULT;
21745 			} else {
21746 				mutex_exit(SD_MUTEX(un));
21747 				err = 0;
21748 			}
21749 		}
21750 #else
21751 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
21752 		err = ENOTTY;
21753 #endif
21754 		break;
21755 	}
21756 #ifdef SDDEBUG
21757 /* RESET/ABORTS testing ioctls */
21758 	case DKIOCRESET: {
21759 		int	reset_level;
21760 
21761 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
21762 			err = EFAULT;
21763 		} else {
21764 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
21765 			    "reset_level = 0x%lx\n", reset_level);
21766 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
21767 				err = 0;
21768 			} else {
21769 				err = EIO;
21770 			}
21771 		}
21772 		break;
21773 	}
21774 
21775 	case DKIOCABORT:
21776 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
21777 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
21778 			err = 0;
21779 		} else {
21780 			err = EIO;
21781 		}
21782 		break;
21783 #endif
21784 
21785 #ifdef SD_FAULT_INJECTION
21786 /* SDIOC FaultInjection testing ioctls */
21787 	case SDIOCSTART:
21788 	case SDIOCSTOP:
21789 	case SDIOCINSERTPKT:
21790 	case SDIOCINSERTXB:
21791 	case SDIOCINSERTUN:
21792 	case SDIOCINSERTARQ:
21793 	case SDIOCPUSH:
21794 	case SDIOCRETRIEVE:
21795 	case SDIOCRUN:
21796 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
21797 		    "SDIOC detected cmd:0x%X:\n", cmd);
21798 		/* call error generator */
21799 		sd_faultinjection_ioctl(cmd, arg, un);
21800 		err = 0;
21801 		break;
21802 
21803 #endif /* SD_FAULT_INJECTION */
21804 
21805 	case DKIOCFLUSHWRITECACHE:
21806 		{
21807 			struct dk_callback *dkc = (struct dk_callback *)arg;
21808 
21809 			mutex_enter(SD_MUTEX(un));
21810 			if (!un->un_f_sync_cache_supported ||
21811 			    !un->un_f_write_cache_enabled) {
21812 				err = un->un_f_sync_cache_supported ?
21813 					0 : ENOTSUP;
21814 				mutex_exit(SD_MUTEX(un));
21815 				if ((flag & FKIOCTL) && dkc != NULL &&
21816 				    dkc->dkc_callback != NULL) {
21817 					(*dkc->dkc_callback)(dkc->dkc_cookie,
21818 					    err);
21819 					/*
21820 					 * Did callback and reported error.
21821 					 * Since we did a callback, ioctl
21822 					 * should return 0.
21823 					 */
21824 					err = 0;
21825 				}
21826 				break;
21827 			}
21828 			mutex_exit(SD_MUTEX(un));
21829 
21830 			if ((flag & FKIOCTL) && dkc != NULL &&
21831 			    dkc->dkc_callback != NULL) {
21832 				/* async SYNC CACHE request */
21833 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, dkc);
21834 			} else {
21835 				/* synchronous SYNC CACHE request */
21836 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
21837 			}
21838 		}
21839 		break;
21840 
21841 	case DKIOCGETWCE: {
21842 
21843 		int wce;
21844 
21845 		if ((err = sd_get_write_cache_enabled(un, &wce)) != 0) {
21846 			break;
21847 		}
21848 
21849 		if (ddi_copyout(&wce, (void *)arg, sizeof (wce), flag)) {
21850 			err = EFAULT;
21851 		}
21852 		break;
21853 	}
21854 
21855 	case DKIOCSETWCE: {
21856 
21857 		int wce, sync_supported;
21858 
21859 		if (ddi_copyin((void *)arg, &wce, sizeof (wce), flag)) {
21860 			err = EFAULT;
21861 			break;
21862 		}
21863 
21864 		/*
21865 		 * Synchronize multiple threads trying to enable
21866 		 * or disable the cache via the un_f_wcc_cv
21867 		 * condition variable.
21868 		 */
21869 		mutex_enter(SD_MUTEX(un));
21870 
21871 		/*
21872 		 * Don't allow the cache to be enabled if the
21873 		 * config file has it disabled.
21874 		 */
21875 		if (un->un_f_opt_disable_cache && wce) {
21876 			mutex_exit(SD_MUTEX(un));
21877 			err = EINVAL;
21878 			break;
21879 		}
21880 
21881 		/*
21882 		 * Wait for write cache change in progress
21883 		 * bit to be clear before proceeding.
21884 		 */
21885 		while (un->un_f_wcc_inprog)
21886 			cv_wait(&un->un_wcc_cv, SD_MUTEX(un));
21887 
21888 		un->un_f_wcc_inprog = 1;
21889 
21890 		if (un->un_f_write_cache_enabled && wce == 0) {
21891 			/*
21892 			 * Disable the write cache.  Don't clear
21893 			 * un_f_write_cache_enabled until after
21894 			 * the mode select and flush are complete.
21895 			 */
21896 			sync_supported = un->un_f_sync_cache_supported;
21897 			mutex_exit(SD_MUTEX(un));
21898 			if ((err = sd_cache_control(un, SD_CACHE_NOCHANGE,
21899 			    SD_CACHE_DISABLE)) == 0 && sync_supported) {
21900 				err = sd_send_scsi_SYNCHRONIZE_CACHE(un, NULL);
21901 			}
21902 
21903 			mutex_enter(SD_MUTEX(un));
21904 			if (err == 0) {
21905 				un->un_f_write_cache_enabled = 0;
21906 			}
21907 
21908 		} else if (!un->un_f_write_cache_enabled && wce != 0) {
21909 			/*
21910 			 * Set un_f_write_cache_enabled first, so there is
21911 			 * no window where the cache is enabled, but the
21912 			 * bit says it isn't.
21913 			 */
21914 			un->un_f_write_cache_enabled = 1;
21915 			mutex_exit(SD_MUTEX(un));
21916 
21917 			err = sd_cache_control(un, SD_CACHE_NOCHANGE,
21918 				SD_CACHE_ENABLE);
21919 
21920 			mutex_enter(SD_MUTEX(un));
21921 
21922 			if (err) {
21923 				un->un_f_write_cache_enabled = 0;
21924 			}
21925 		}
21926 
21927 		un->un_f_wcc_inprog = 0;
21928 		cv_broadcast(&un->un_wcc_cv);
21929 		mutex_exit(SD_MUTEX(un));
21930 		break;
21931 	}
21932 
21933 	default:
21934 		err = ENOTTY;
21935 		break;
21936 	}
21937 	mutex_enter(SD_MUTEX(un));
21938 	un->un_ncmds_in_driver--;
21939 	ASSERT(un->un_ncmds_in_driver >= 0);
21940 	mutex_exit(SD_MUTEX(un));
21941 
21942 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
21943 	return (err);
21944 }
21945 
21946 
21947 /*
21948  *    Function: sd_uscsi_ioctl
21949  *
21950  * Description: This routine is the driver entry point for handling USCSI ioctl
21951  *		requests (USCSICMD).
21952  *
21953  *   Arguments: dev	- the device number
21954  *		arg	- user provided scsi command
21955  *		flag	- this argument is a pass through to ddi_copyxxx()
21956  *			  directly from the mode argument of ioctl().
21957  *
21958  * Return Code: code returned by sd_send_scsi_cmd
21959  *		ENXIO
21960  *		EFAULT
21961  *		EAGAIN
21962  */
21963 
21964 static int
21965 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag)
21966 {
21967 #ifdef _MULTI_DATAMODEL
21968 	/*
21969 	 * For use when a 32 bit app makes a call into a
21970 	 * 64 bit ioctl
21971 	 */
21972 	struct uscsi_cmd32	uscsi_cmd_32_for_64;
21973 	struct uscsi_cmd32	*ucmd32 = &uscsi_cmd_32_for_64;
21974 	model_t			model;
21975 #endif /* _MULTI_DATAMODEL */
21976 	struct uscsi_cmd	*scmd = NULL;
21977 	struct sd_lun		*un = NULL;
21978 	enum uio_seg		uioseg;
21979 	char			cdb[CDB_GROUP0];
21980 	int			rval = 0;
21981 
21982 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21983 		return (ENXIO);
21984 	}
21985 
21986 	SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un);
21987 
21988 	scmd = (struct uscsi_cmd *)
21989 	    kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21990 
21991 #ifdef _MULTI_DATAMODEL
21992 	switch (model = ddi_model_convert_from(flag & FMODELS)) {
21993 	case DDI_MODEL_ILP32:
21994 	{
21995 		if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) {
21996 			rval = EFAULT;
21997 			goto done;
21998 		}
21999 		/*
22000 		 * Convert the ILP32 uscsi data from the
22001 		 * application to LP64 for internal use.
22002 		 */
22003 		uscsi_cmd32touscsi_cmd(ucmd32, scmd);
22004 		break;
22005 	}
22006 	case DDI_MODEL_NONE:
22007 		if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) {
22008 			rval = EFAULT;
22009 			goto done;
22010 		}
22011 		break;
22012 	}
22013 #else /* ! _MULTI_DATAMODEL */
22014 	if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) {
22015 		rval = EFAULT;
22016 		goto done;
22017 	}
22018 #endif /* _MULTI_DATAMODEL */
22019 
22020 	scmd->uscsi_flags &= ~USCSI_NOINTR;
22021 	uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE;
22022 	if (un->un_f_format_in_progress == TRUE) {
22023 		rval = EAGAIN;
22024 		goto done;
22025 	}
22026 
22027 	/*
22028 	 * Gotta do the ddi_copyin() here on the uscsi_cdb so that
22029 	 * we will have a valid cdb[0] to test.
22030 	 */
22031 	if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) &&
22032 	    (cdb[0] == SCMD_FORMAT)) {
22033 		SD_TRACE(SD_LOG_IOCTL, un,
22034 		    "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]);
22035 		mutex_enter(SD_MUTEX(un));
22036 		un->un_f_format_in_progress = TRUE;
22037 		mutex_exit(SD_MUTEX(un));
22038 		rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg,
22039 		    SD_PATH_STANDARD);
22040 		mutex_enter(SD_MUTEX(un));
22041 		un->un_f_format_in_progress = FALSE;
22042 		mutex_exit(SD_MUTEX(un));
22043 	} else {
22044 		SD_TRACE(SD_LOG_IOCTL, un,
22045 		    "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]);
22046 		/*
22047 		 * It's OK to fall into here even if the ddi_copyin()
22048 		 * on the uscsi_cdb above fails, because sd_send_scsi_cmd()
22049 		 * does this same copyin and will return the EFAULT
22050 		 * if it fails.
22051 		 */
22052 		rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg,
22053 		    SD_PATH_STANDARD);
22054 	}
22055 #ifdef _MULTI_DATAMODEL
22056 	switch (model) {
22057 	case DDI_MODEL_ILP32:
22058 		/*
22059 		 * Convert back to ILP32 before copyout to the
22060 		 * application
22061 		 */
22062 		uscsi_cmdtouscsi_cmd32(scmd, ucmd32);
22063 		if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) {
22064 			if (rval != 0) {
22065 				rval = EFAULT;
22066 			}
22067 		}
22068 		break;
22069 	case DDI_MODEL_NONE:
22070 		if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) {
22071 			if (rval != 0) {
22072 				rval = EFAULT;
22073 			}
22074 		}
22075 		break;
22076 	}
22077 #else /* ! _MULTI_DATAMODE */
22078 	if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) {
22079 		if (rval != 0) {
22080 			rval = EFAULT;
22081 		}
22082 	}
22083 #endif /* _MULTI_DATAMODE */
22084 done:
22085 	kmem_free(scmd, sizeof (struct uscsi_cmd));
22086 
22087 	SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un);
22088 
22089 	return (rval);
22090 }
22091 
22092 
22093 /*
22094  *    Function: sd_dkio_ctrl_info
22095  *
22096  * Description: This routine is the driver entry point for handling controller
22097  *		information ioctl requests (DKIOCINFO).
22098  *
22099  *   Arguments: dev  - the device number
22100  *		arg  - pointer to user provided dk_cinfo structure
22101  *		       specifying the controller type and attributes.
22102  *		flag - this argument is a pass through to ddi_copyxxx()
22103  *		       directly from the mode argument of ioctl().
22104  *
22105  * Return Code: 0
22106  *		EFAULT
22107  *		ENXIO
22108  */
22109 
22110 static int
22111 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
22112 {
22113 	struct sd_lun	*un = NULL;
22114 	struct dk_cinfo	*info;
22115 	dev_info_t	*pdip;
22116 	int		lun, tgt;
22117 
22118 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22119 		return (ENXIO);
22120 	}
22121 
22122 	info = (struct dk_cinfo *)
22123 		kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
22124 
22125 	switch (un->un_ctype) {
22126 	case CTYPE_CDROM:
22127 		info->dki_ctype = DKC_CDROM;
22128 		break;
22129 	default:
22130 		info->dki_ctype = DKC_SCSI_CCS;
22131 		break;
22132 	}
22133 	pdip = ddi_get_parent(SD_DEVINFO(un));
22134 	info->dki_cnum = ddi_get_instance(pdip);
22135 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
22136 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
22137 	} else {
22138 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
22139 		    DK_DEVLEN - 1);
22140 	}
22141 
22142 	lun = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
22143 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);
22144 	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, SD_DEVINFO(un),
22145 	    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_TARGET, 0);
22146 
22147 	/* Unit Information */
22148 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
22149 	info->dki_slave = ((tgt << 3) | lun);
22150 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
22151 	    DK_DEVLEN - 1);
22152 	info->dki_flags = DKI_FMTVOL;
22153 	info->dki_partition = SDPART(dev);
22154 
22155 	/* Max Transfer size of this device in blocks */
22156 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
22157 	info->dki_addr = 0;
22158 	info->dki_space = 0;
22159 	info->dki_prio = 0;
22160 	info->dki_vec = 0;
22161 
22162 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
22163 		kmem_free(info, sizeof (struct dk_cinfo));
22164 		return (EFAULT);
22165 	} else {
22166 		kmem_free(info, sizeof (struct dk_cinfo));
22167 		return (0);
22168 	}
22169 }
22170 
22171 
22172 /*
22173  *    Function: sd_get_media_info
22174  *
22175  * Description: This routine is the driver entry point for handling ioctl
22176  *		requests for the media type or command set profile used by the
22177  *		drive to operate on the media (DKIOCGMEDIAINFO).
22178  *
22179  *   Arguments: dev	- the device number
22180  *		arg	- pointer to user provided dk_minfo structure
22181  *			  specifying the media type, logical block size and
22182  *			  drive capacity.
22183  *		flag	- this argument is a pass through to ddi_copyxxx()
22184  *			  directly from the mode argument of ioctl().
22185  *
22186  * Return Code: 0
22187  *		EACCESS
22188  *		EFAULT
22189  *		ENXIO
22190  *		EIO
22191  */
22192 
22193 static int
22194 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
22195 {
22196 	struct sd_lun		*un = NULL;
22197 	struct uscsi_cmd	com;
22198 	struct scsi_inquiry	*sinq;
22199 	struct dk_minfo		media_info;
22200 	u_longlong_t		media_capacity;
22201 	uint64_t		capacity;
22202 	uint_t			lbasize;
22203 	uchar_t			*out_data;
22204 	uchar_t			*rqbuf;
22205 	int			rval = 0;
22206 	int			rtn;
22207 
22208 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
22209 	    (un->un_state == SD_STATE_OFFLINE)) {
22210 		return (ENXIO);
22211 	}
22212 
22213 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n");
22214 
22215 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
22216 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
22217 
22218 	/* Issue a TUR to determine if the drive is ready with media present */
22219 	rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA);
22220 	if (rval == ENXIO) {
22221 		goto done;
22222 	}
22223 
22224 	/* Now get configuration data */
22225 	if (ISCD(un)) {
22226 		media_info.dki_media_type = DK_CDROM;
22227 
22228 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
22229 		if (un->un_f_mmc_cap == TRUE) {
22230 			rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf,
22231 				SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN);
22232 
22233 			if (rtn) {
22234 				/*
22235 				 * Failed for other than an illegal request
22236 				 * or command not supported
22237 				 */
22238 				if ((com.uscsi_status == STATUS_CHECK) &&
22239 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
22240 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
22241 					    (rqbuf[12] != 0x20)) {
22242 						rval = EIO;
22243 						goto done;
22244 					}
22245 				}
22246 			} else {
22247 				/*
22248 				 * The GET CONFIGURATION command succeeded
22249 				 * so set the media type according to the
22250 				 * returned data
22251 				 */
22252 				media_info.dki_media_type = out_data[6];
22253 				media_info.dki_media_type <<= 8;
22254 				media_info.dki_media_type |= out_data[7];
22255 			}
22256 		}
22257 	} else {
22258 		/*
22259 		 * The profile list is not available, so we attempt to identify
22260 		 * the media type based on the inquiry data
22261 		 */
22262 		sinq = un->un_sd->sd_inq;
22263 		if (sinq->inq_qual == 0) {
22264 			/* This is a direct access device */
22265 			media_info.dki_media_type = DK_FIXED_DISK;
22266 
22267 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
22268 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
22269 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
22270 					media_info.dki_media_type = DK_ZIP;
22271 				} else if (
22272 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
22273 					media_info.dki_media_type = DK_JAZ;
22274 				}
22275 			}
22276 		} else {
22277 			/* Not a CD or direct access so return unknown media */
22278 			media_info.dki_media_type = DK_UNKNOWN;
22279 		}
22280 	}
22281 
22282 	/* Now read the capacity so we can provide the lbasize and capacity */
22283 	switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
22284 	    SD_PATH_DIRECT)) {
22285 	case 0:
22286 		break;
22287 	case EACCES:
22288 		rval = EACCES;
22289 		goto done;
22290 	default:
22291 		rval = EIO;
22292 		goto done;
22293 	}
22294 
22295 	media_info.dki_lbsize = lbasize;
22296 	media_capacity = capacity;
22297 
22298 	/*
22299 	 * sd_send_scsi_READ_CAPACITY() reports capacity in
22300 	 * un->un_sys_blocksize chunks. So we need to convert it into
22301 	 * cap.lbasize chunks.
22302 	 */
22303 	media_capacity *= un->un_sys_blocksize;
22304 	media_capacity /= lbasize;
22305 	media_info.dki_capacity = media_capacity;
22306 
22307 	if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) {
22308 		rval = EFAULT;
22309 		/* Put goto. Anybody might add some code below in future */
22310 		goto done;
22311 	}
22312 done:
22313 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
22314 	kmem_free(rqbuf, SENSE_LENGTH);
22315 	return (rval);
22316 }
22317 
22318 
22319 /*
22320  *    Function: sd_dkio_get_geometry
22321  *
22322  * Description: This routine is the driver entry point for handling user
22323  *		requests to get the device geometry (DKIOCGGEOM).
22324  *
22325  *   Arguments: dev  - the device number
22326  *		arg  - pointer to user provided dk_geom structure specifying
22327  *			the controller's notion of the current geometry.
22328  *		flag - this argument is a pass through to ddi_copyxxx()
22329  *		       directly from the mode argument of ioctl().
22330  *		geom_validated - flag indicating if the device geometry has been
22331  *				 previously validated in the sdioctl routine.
22332  *
22333  * Return Code: 0
22334  *		EFAULT
22335  *		ENXIO
22336  *		EIO
22337  */
22338 
22339 static int
22340 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated)
22341 {
22342 	struct sd_lun	*un = NULL;
22343 	struct dk_geom	*tmp_geom = NULL;
22344 	int		rval = 0;
22345 
22346 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22347 		return (ENXIO);
22348 	}
22349 
22350 #if defined(__i386) || defined(__amd64)
22351 	if (un->un_solaris_size == 0) {
22352 		return (EIO);
22353 	}
22354 #endif
22355 	if (geom_validated == FALSE) {
22356 		/*
22357 		 * sd_validate_geometry does not spin a disk up
22358 		 * if it was spun down. We need to make sure it
22359 		 * is ready.
22360 		 */
22361 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22362 			return (rval);
22363 		}
22364 		mutex_enter(SD_MUTEX(un));
22365 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
22366 		mutex_exit(SD_MUTEX(un));
22367 	}
22368 	if (rval)
22369 		return (rval);
22370 
22371 	/*
22372 	 * Make a local copy of the soft state geometry to avoid some potential
22373 	 * race conditions associated with holding the mutex and updating the
22374 	 * write_reinstruct value
22375 	 */
22376 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
22377 	mutex_enter(SD_MUTEX(un));
22378 	bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom));
22379 	mutex_exit(SD_MUTEX(un));
22380 
22381 	if (tmp_geom->dkg_write_reinstruct == 0) {
22382 		tmp_geom->dkg_write_reinstruct =
22383 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
22384 		    sd_rot_delay) / (int)60000);
22385 	}
22386 
22387 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
22388 	    flag);
22389 	if (rval != 0) {
22390 		rval = EFAULT;
22391 	}
22392 
22393 	kmem_free(tmp_geom, sizeof (struct dk_geom));
22394 	return (rval);
22395 
22396 }
22397 
22398 
22399 /*
22400  *    Function: sd_dkio_set_geometry
22401  *
22402  * Description: This routine is the driver entry point for handling user
22403  *		requests to set the device geometry (DKIOCSGEOM). The actual
22404  *		device geometry is not updated, just the driver "notion" of it.
22405  *
22406  *   Arguments: dev  - the device number
22407  *		arg  - pointer to user provided dk_geom structure used to set
22408  *			the controller's notion of the current geometry.
22409  *		flag - this argument is a pass through to ddi_copyxxx()
22410  *		       directly from the mode argument of ioctl().
22411  *
22412  * Return Code: 0
22413  *		EFAULT
22414  *		ENXIO
22415  *		EIO
22416  */
22417 
22418 static int
22419 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag)
22420 {
22421 	struct sd_lun	*un = NULL;
22422 	struct dk_geom	*tmp_geom;
22423 	struct dk_map	*lp;
22424 	int		rval = 0;
22425 	int		i;
22426 
22427 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22428 		return (ENXIO);
22429 	}
22430 
22431 #if defined(__i386) || defined(__amd64)
22432 	if (un->un_solaris_size == 0) {
22433 		return (EIO);
22434 	}
22435 #endif
22436 	/*
22437 	 * We need to copy the user specified geometry into local
22438 	 * storage and then update the softstate. We don't want to hold
22439 	 * the mutex and copyin directly from the user to the soft state
22440 	 */
22441 	tmp_geom = (struct dk_geom *)
22442 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
22443 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
22444 	if (rval != 0) {
22445 		kmem_free(tmp_geom, sizeof (struct dk_geom));
22446 		return (EFAULT);
22447 	}
22448 
22449 	mutex_enter(SD_MUTEX(un));
22450 	bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom));
22451 	for (i = 0; i < NDKMAP; i++) {
22452 		lp  = &un->un_map[i];
22453 		un->un_offset[i] =
22454 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
22455 #if defined(__i386) || defined(__amd64)
22456 		un->un_offset[i] += un->un_solaris_offset;
22457 #endif
22458 	}
22459 	un->un_f_geometry_is_valid = FALSE;
22460 	mutex_exit(SD_MUTEX(un));
22461 	kmem_free(tmp_geom, sizeof (struct dk_geom));
22462 
22463 	return (rval);
22464 }
22465 
22466 
22467 /*
22468  *    Function: sd_dkio_get_partition
22469  *
22470  * Description: This routine is the driver entry point for handling user
22471  *		requests to get the partition table (DKIOCGAPART).
22472  *
22473  *   Arguments: dev  - the device number
22474  *		arg  - pointer to user provided dk_allmap structure specifying
22475  *			the controller's notion of the current partition table.
22476  *		flag - this argument is a pass through to ddi_copyxxx()
22477  *		       directly from the mode argument of ioctl().
22478  *		geom_validated - flag indicating if the device geometry has been
22479  *				 previously validated in the sdioctl routine.
22480  *
22481  * Return Code: 0
22482  *		EFAULT
22483  *		ENXIO
22484  *		EIO
22485  */
22486 
22487 static int
22488 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated)
22489 {
22490 	struct sd_lun	*un = NULL;
22491 	int		rval = 0;
22492 	int		size;
22493 
22494 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22495 		return (ENXIO);
22496 	}
22497 
22498 #if defined(__i386) || defined(__amd64)
22499 	if (un->un_solaris_size == 0) {
22500 		return (EIO);
22501 	}
22502 #endif
22503 	/*
22504 	 * Make sure the geometry is valid before getting the partition
22505 	 * information.
22506 	 */
22507 	mutex_enter(SD_MUTEX(un));
22508 	if (geom_validated == FALSE) {
22509 		/*
22510 		 * sd_validate_geometry does not spin a disk up
22511 		 * if it was spun down. We need to make sure it
22512 		 * is ready before validating the geometry.
22513 		 */
22514 		mutex_exit(SD_MUTEX(un));
22515 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22516 			return (rval);
22517 		}
22518 		mutex_enter(SD_MUTEX(un));
22519 
22520 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
22521 			mutex_exit(SD_MUTEX(un));
22522 			return (rval);
22523 		}
22524 	}
22525 	mutex_exit(SD_MUTEX(un));
22526 
22527 #ifdef _MULTI_DATAMODEL
22528 	switch (ddi_model_convert_from(flag & FMODELS)) {
22529 	case DDI_MODEL_ILP32: {
22530 		struct dk_map32 dk_map32[NDKMAP];
22531 		int		i;
22532 
22533 		for (i = 0; i < NDKMAP; i++) {
22534 			dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno;
22535 			dk_map32[i].dkl_nblk  = un->un_map[i].dkl_nblk;
22536 		}
22537 		size = NDKMAP * sizeof (struct dk_map32);
22538 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
22539 		if (rval != 0) {
22540 			rval = EFAULT;
22541 		}
22542 		break;
22543 	}
22544 	case DDI_MODEL_NONE:
22545 		size = NDKMAP * sizeof (struct dk_map);
22546 		rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
22547 		if (rval != 0) {
22548 			rval = EFAULT;
22549 		}
22550 		break;
22551 	}
22552 #else /* ! _MULTI_DATAMODEL */
22553 	size = NDKMAP * sizeof (struct dk_map);
22554 	rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
22555 	if (rval != 0) {
22556 		rval = EFAULT;
22557 	}
22558 #endif /* _MULTI_DATAMODEL */
22559 	return (rval);
22560 }
22561 
22562 
22563 /*
22564  *    Function: sd_dkio_set_partition
22565  *
22566  * Description: This routine is the driver entry point for handling user
22567  *		requests to set the partition table (DKIOCSAPART). The actual
22568  *		device partition is not updated.
22569  *
22570  *   Arguments: dev  - the device number
22571  *		arg  - pointer to user provided dk_allmap structure used to set
22572  *			the controller's notion of the partition table.
22573  *		flag - this argument is a pass through to ddi_copyxxx()
22574  *		       directly from the mode argument of ioctl().
22575  *
22576  * Return Code: 0
22577  *		EINVAL
22578  *		EFAULT
22579  *		ENXIO
22580  *		EIO
22581  */
22582 
22583 static int
22584 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag)
22585 {
22586 	struct sd_lun	*un = NULL;
22587 	struct dk_map	dk_map[NDKMAP];
22588 	struct dk_map	*lp;
22589 	int		rval = 0;
22590 	int		size;
22591 	int		i;
22592 #if defined(_SUNOS_VTOC_16)
22593 	struct dkl_partition	*vp;
22594 #endif
22595 
22596 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22597 		return (ENXIO);
22598 	}
22599 
22600 	/*
22601 	 * Set the map for all logical partitions.  We lock
22602 	 * the priority just to make sure an interrupt doesn't
22603 	 * come in while the map is half updated.
22604 	 */
22605 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size))
22606 	mutex_enter(SD_MUTEX(un));
22607 	if (un->un_blockcount > DK_MAX_BLOCKS) {
22608 		mutex_exit(SD_MUTEX(un));
22609 		return (ENOTSUP);
22610 	}
22611 	mutex_exit(SD_MUTEX(un));
22612 	if (un->un_solaris_size == 0) {
22613 		return (EIO);
22614 	}
22615 
22616 #ifdef _MULTI_DATAMODEL
22617 	switch (ddi_model_convert_from(flag & FMODELS)) {
22618 	case DDI_MODEL_ILP32: {
22619 		struct dk_map32 dk_map32[NDKMAP];
22620 
22621 		size = NDKMAP * sizeof (struct dk_map32);
22622 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
22623 		if (rval != 0) {
22624 			return (EFAULT);
22625 		}
22626 		for (i = 0; i < NDKMAP; i++) {
22627 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
22628 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
22629 		}
22630 		break;
22631 	}
22632 	case DDI_MODEL_NONE:
22633 		size = NDKMAP * sizeof (struct dk_map);
22634 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
22635 		if (rval != 0) {
22636 			return (EFAULT);
22637 		}
22638 		break;
22639 	}
22640 #else /* ! _MULTI_DATAMODEL */
22641 	size = NDKMAP * sizeof (struct dk_map);
22642 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
22643 	if (rval != 0) {
22644 		return (EFAULT);
22645 	}
22646 #endif /* _MULTI_DATAMODEL */
22647 
22648 	mutex_enter(SD_MUTEX(un));
22649 	/* Note: The size used in this bcopy is set based upon the data model */
22650 	bcopy(dk_map, un->un_map, size);
22651 #if defined(_SUNOS_VTOC_16)
22652 	vp = (struct dkl_partition *)&(un->un_vtoc);
22653 #endif	/* defined(_SUNOS_VTOC_16) */
22654 	for (i = 0; i < NDKMAP; i++) {
22655 		lp  = &un->un_map[i];
22656 		un->un_offset[i] =
22657 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
22658 #if defined(_SUNOS_VTOC_16)
22659 		vp->p_start = un->un_offset[i];
22660 		vp->p_size = lp->dkl_nblk;
22661 		vp++;
22662 #endif	/* defined(_SUNOS_VTOC_16) */
22663 #if defined(__i386) || defined(__amd64)
22664 		un->un_offset[i] += un->un_solaris_offset;
22665 #endif
22666 	}
22667 	mutex_exit(SD_MUTEX(un));
22668 	return (rval);
22669 }
22670 
22671 
22672 /*
22673  *    Function: sd_dkio_get_vtoc
22674  *
22675  * Description: This routine is the driver entry point for handling user
22676  *		requests to get the current volume table of contents
22677  *		(DKIOCGVTOC).
22678  *
22679  *   Arguments: dev  - the device number
22680  *		arg  - pointer to user provided vtoc structure specifying
22681  *			the current vtoc.
22682  *		flag - this argument is a pass through to ddi_copyxxx()
22683  *		       directly from the mode argument of ioctl().
22684  *		geom_validated - flag indicating if the device geometry has been
22685  *				 previously validated in the sdioctl routine.
22686  *
22687  * Return Code: 0
22688  *		EFAULT
22689  *		ENXIO
22690  *		EIO
22691  */
22692 
22693 static int
22694 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated)
22695 {
22696 	struct sd_lun	*un = NULL;
22697 #if defined(_SUNOS_VTOC_8)
22698 	struct vtoc	user_vtoc;
22699 #endif	/* defined(_SUNOS_VTOC_8) */
22700 	int		rval = 0;
22701 
22702 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22703 		return (ENXIO);
22704 	}
22705 
22706 	mutex_enter(SD_MUTEX(un));
22707 	if (geom_validated == FALSE) {
22708 		/*
22709 		 * sd_validate_geometry does not spin a disk up
22710 		 * if it was spun down. We need to make sure it
22711 		 * is ready.
22712 		 */
22713 		mutex_exit(SD_MUTEX(un));
22714 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22715 			return (rval);
22716 		}
22717 		mutex_enter(SD_MUTEX(un));
22718 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
22719 			mutex_exit(SD_MUTEX(un));
22720 			return (rval);
22721 		}
22722 	}
22723 
22724 #if defined(_SUNOS_VTOC_8)
22725 	sd_build_user_vtoc(un, &user_vtoc);
22726 	mutex_exit(SD_MUTEX(un));
22727 
22728 #ifdef _MULTI_DATAMODEL
22729 	switch (ddi_model_convert_from(flag & FMODELS)) {
22730 	case DDI_MODEL_ILP32: {
22731 		struct vtoc32 user_vtoc32;
22732 
22733 		vtoctovtoc32(user_vtoc, user_vtoc32);
22734 		if (ddi_copyout(&user_vtoc32, (void *)arg,
22735 		    sizeof (struct vtoc32), flag)) {
22736 			return (EFAULT);
22737 		}
22738 		break;
22739 	}
22740 
22741 	case DDI_MODEL_NONE:
22742 		if (ddi_copyout(&user_vtoc, (void *)arg,
22743 		    sizeof (struct vtoc), flag)) {
22744 			return (EFAULT);
22745 		}
22746 		break;
22747 	}
22748 #else /* ! _MULTI_DATAMODEL */
22749 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
22750 		return (EFAULT);
22751 	}
22752 #endif /* _MULTI_DATAMODEL */
22753 
22754 #elif defined(_SUNOS_VTOC_16)
22755 	mutex_exit(SD_MUTEX(un));
22756 
22757 #ifdef _MULTI_DATAMODEL
22758 	/*
22759 	 * The un_vtoc structure is a "struct dk_vtoc"  which is always
22760 	 * 32-bit to maintain compatibility with existing on-disk
22761 	 * structures.  Thus, we need to convert the structure when copying
22762 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
22763 	 * program.  If the target is a 32-bit program, then no conversion
22764 	 * is necessary.
22765 	 */
22766 	/* LINTED: logical expression always true: op "||" */
22767 	ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32));
22768 	switch (ddi_model_convert_from(flag & FMODELS)) {
22769 	case DDI_MODEL_ILP32:
22770 		if (ddi_copyout(&(un->un_vtoc), (void *)arg,
22771 		    sizeof (un->un_vtoc), flag)) {
22772 			return (EFAULT);
22773 		}
22774 		break;
22775 
22776 	case DDI_MODEL_NONE: {
22777 		struct vtoc user_vtoc;
22778 
22779 		vtoc32tovtoc(un->un_vtoc, user_vtoc);
22780 		if (ddi_copyout(&user_vtoc, (void *)arg,
22781 		    sizeof (struct vtoc), flag)) {
22782 			return (EFAULT);
22783 		}
22784 		break;
22785 	}
22786 	}
22787 #else /* ! _MULTI_DATAMODEL */
22788 	if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc),
22789 	    flag)) {
22790 		return (EFAULT);
22791 	}
22792 #endif /* _MULTI_DATAMODEL */
22793 #else
22794 #error "No VTOC format defined."
22795 #endif
22796 
22797 	return (rval);
22798 }
22799 
22800 static int
22801 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag)
22802 {
22803 	struct sd_lun	*un = NULL;
22804 	dk_efi_t	user_efi;
22805 	int		rval = 0;
22806 	void		*buffer;
22807 
22808 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
22809 		return (ENXIO);
22810 
22811 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
22812 		return (EFAULT);
22813 
22814 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
22815 
22816 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
22817 	    (user_efi.dki_length > un->un_max_xfer_size))
22818 		return (EINVAL);
22819 
22820 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
22821 	rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length,
22822 	    user_efi.dki_lba, SD_PATH_DIRECT);
22823 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
22824 	    user_efi.dki_length, flag) != 0)
22825 		rval = EFAULT;
22826 
22827 	kmem_free(buffer, user_efi.dki_length);
22828 	return (rval);
22829 }
22830 
22831 /*
22832  *    Function: sd_build_user_vtoc
22833  *
22834  * Description: This routine populates a pass by reference variable with the
22835  *		current volume table of contents.
22836  *
22837  *   Arguments: un - driver soft state (unit) structure
22838  *		user_vtoc - pointer to vtoc structure to be populated
22839  */
22840 
22841 static void
22842 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
22843 {
22844 	struct dk_map2		*lpart;
22845 	struct dk_map		*lmap;
22846 	struct partition	*vpart;
22847 	int			nblks;
22848 	int			i;
22849 
22850 	ASSERT(mutex_owned(SD_MUTEX(un)));
22851 
22852 	/*
22853 	 * Return vtoc structure fields in the provided VTOC area, addressed
22854 	 * by *vtoc.
22855 	 */
22856 	bzero(user_vtoc, sizeof (struct vtoc));
22857 	user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0];
22858 	user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1];
22859 	user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2];
22860 	user_vtoc->v_sanity	= VTOC_SANE;
22861 	user_vtoc->v_version	= un->un_vtoc.v_version;
22862 	bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
22863 	user_vtoc->v_sectorsz = un->un_sys_blocksize;
22864 	user_vtoc->v_nparts = un->un_vtoc.v_nparts;
22865 	bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved,
22866 	    sizeof (un->un_vtoc.v_reserved));
22867 	/*
22868 	 * Convert partitioning information.
22869 	 *
22870 	 * Note the conversion from starting cylinder number
22871 	 * to starting sector number.
22872 	 */
22873 	lmap = un->un_map;
22874 	lpart = (struct dk_map2 *)un->un_vtoc.v_part;
22875 	vpart = user_vtoc->v_part;
22876 
22877 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
22878 
22879 	for (i = 0; i < V_NUMPAR; i++) {
22880 		vpart->p_tag	= lpart->p_tag;
22881 		vpart->p_flag	= lpart->p_flag;
22882 		vpart->p_start	= lmap->dkl_cylno * nblks;
22883 		vpart->p_size	= lmap->dkl_nblk;
22884 		lmap++;
22885 		lpart++;
22886 		vpart++;
22887 
22888 		/* (4364927) */
22889 		user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i];
22890 	}
22891 
22892 	bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
22893 }
22894 
22895 static int
22896 sd_dkio_partition(dev_t dev, caddr_t arg, int flag)
22897 {
22898 	struct sd_lun		*un = NULL;
22899 	struct partition64	p64;
22900 	int			rval = 0;
22901 	uint_t			nparts;
22902 	efi_gpe_t		*partitions;
22903 	efi_gpt_t		*buffer;
22904 	diskaddr_t		gpe_lba;
22905 
22906 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22907 		return (ENXIO);
22908 	}
22909 
22910 	if (ddi_copyin((const void *)arg, &p64,
22911 	    sizeof (struct partition64), flag)) {
22912 		return (EFAULT);
22913 	}
22914 
22915 	buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
22916 	rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE,
22917 		1, SD_PATH_DIRECT);
22918 	if (rval != 0)
22919 		goto done_error;
22920 
22921 	sd_swap_efi_gpt(buffer);
22922 
22923 	if ((rval = sd_validate_efi(buffer)) != 0)
22924 		goto done_error;
22925 
22926 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
22927 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
22928 	if (p64.p_partno > nparts) {
22929 		/* couldn't find it */
22930 		rval = ESRCH;
22931 		goto done_error;
22932 	}
22933 	/*
22934 	 * if we're dealing with a partition that's out of the normal
22935 	 * 16K block, adjust accordingly
22936 	 */
22937 	gpe_lba += p64.p_partno / sizeof (efi_gpe_t);
22938 	rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE,
22939 			gpe_lba, SD_PATH_DIRECT);
22940 	if (rval) {
22941 		goto done_error;
22942 	}
22943 	partitions = (efi_gpe_t *)buffer;
22944 
22945 	sd_swap_efi_gpe(nparts, partitions);
22946 
22947 	partitions += p64.p_partno;
22948 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
22949 	    sizeof (struct uuid));
22950 	p64.p_start = partitions->efi_gpe_StartingLBA;
22951 	p64.p_size = partitions->efi_gpe_EndingLBA -
22952 			p64.p_start + 1;
22953 
22954 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
22955 		rval = EFAULT;
22956 
22957 done_error:
22958 	kmem_free(buffer, EFI_MIN_ARRAY_SIZE);
22959 	return (rval);
22960 }
22961 
22962 
22963 /*
22964  *    Function: sd_dkio_set_vtoc
22965  *
22966  * Description: This routine is the driver entry point for handling user
22967  *		requests to set the current volume table of contents
22968  *		(DKIOCSVTOC).
22969  *
22970  *   Arguments: dev  - the device number
22971  *		arg  - pointer to user provided vtoc structure used to set the
22972  *			current vtoc.
22973  *		flag - this argument is a pass through to ddi_copyxxx()
22974  *		       directly from the mode argument of ioctl().
22975  *
22976  * Return Code: 0
22977  *		EFAULT
22978  *		ENXIO
22979  *		EINVAL
22980  *		ENOTSUP
22981  */
22982 
22983 static int
22984 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag)
22985 {
22986 	struct sd_lun	*un = NULL;
22987 	struct vtoc	user_vtoc;
22988 	int		rval = 0;
22989 
22990 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22991 		return (ENXIO);
22992 	}
22993 
22994 #if defined(__i386) || defined(__amd64)
22995 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
22996 		return (EINVAL);
22997 	}
22998 #endif
22999 
23000 #ifdef _MULTI_DATAMODEL
23001 	switch (ddi_model_convert_from(flag & FMODELS)) {
23002 	case DDI_MODEL_ILP32: {
23003 		struct vtoc32 user_vtoc32;
23004 
23005 		if (ddi_copyin((const void *)arg, &user_vtoc32,
23006 		    sizeof (struct vtoc32), flag)) {
23007 			return (EFAULT);
23008 		}
23009 		vtoc32tovtoc(user_vtoc32, user_vtoc);
23010 		break;
23011 	}
23012 
23013 	case DDI_MODEL_NONE:
23014 		if (ddi_copyin((const void *)arg, &user_vtoc,
23015 		    sizeof (struct vtoc), flag)) {
23016 			return (EFAULT);
23017 		}
23018 		break;
23019 	}
23020 #else /* ! _MULTI_DATAMODEL */
23021 	if (ddi_copyin((const void *)arg, &user_vtoc,
23022 	    sizeof (struct vtoc), flag)) {
23023 		return (EFAULT);
23024 	}
23025 #endif /* _MULTI_DATAMODEL */
23026 
23027 	mutex_enter(SD_MUTEX(un));
23028 	if (un->un_blockcount > DK_MAX_BLOCKS) {
23029 		mutex_exit(SD_MUTEX(un));
23030 		return (ENOTSUP);
23031 	}
23032 	if (un->un_g.dkg_ncyl == 0) {
23033 		mutex_exit(SD_MUTEX(un));
23034 		return (EINVAL);
23035 	}
23036 
23037 	mutex_exit(SD_MUTEX(un));
23038 	sd_clear_efi(un);
23039 	ddi_remove_minor_node(SD_DEVINFO(un), "wd");
23040 	ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw");
23041 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h",
23042 	    S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23043 	    un->un_node_type, NULL);
23044 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw",
23045 	    S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23046 	    un->un_node_type, NULL);
23047 	mutex_enter(SD_MUTEX(un));
23048 
23049 	if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) {
23050 		if ((rval = sd_write_label(dev)) == 0) {
23051 			if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT))
23052 			    != 0) {
23053 				SD_ERROR(SD_LOG_IOCTL_DKIO, un,
23054 				    "sd_dkio_set_vtoc: "
23055 				    "Failed validate geometry\n");
23056 			}
23057 		}
23058 	}
23059 
23060 	/*
23061 	 * If sd_build_label_vtoc, or sd_write_label failed above write the
23062 	 * devid anyway, what can it hurt? Also preserve the device id by
23063 	 * writing to the disk acyl for the case where a devid has been
23064 	 * fabricated.
23065 	 */
23066 	if (un->un_f_devid_supported &&
23067 	    (un->un_f_opt_fab_devid == TRUE)) {
23068 		if (un->un_devid == NULL) {
23069 			sd_register_devid(un, SD_DEVINFO(un),
23070 			    SD_TARGET_IS_UNRESERVED);
23071 		} else {
23072 			/*
23073 			 * The device id for this disk has been
23074 			 * fabricated. Fabricated device id's are
23075 			 * managed by storing them in the last 2
23076 			 * available sectors on the drive. The device
23077 			 * id must be preserved by writing it back out
23078 			 * to this location.
23079 			 */
23080 			if (sd_write_deviceid(un) != 0) {
23081 				ddi_devid_free(un->un_devid);
23082 				un->un_devid = NULL;
23083 			}
23084 		}
23085 	}
23086 	mutex_exit(SD_MUTEX(un));
23087 	return (rval);
23088 }
23089 
23090 
23091 /*
23092  *    Function: sd_build_label_vtoc
23093  *
23094  * Description: This routine updates the driver soft state current volume table
23095  *		of contents based on a user specified vtoc.
23096  *
23097  *   Arguments: un - driver soft state (unit) structure
23098  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
23099  *			    to update the driver soft state.
23100  *
23101  * Return Code: 0
23102  *		EINVAL
23103  */
23104 
23105 static int
23106 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
23107 {
23108 	struct dk_map		*lmap;
23109 	struct partition	*vpart;
23110 	int			nblks;
23111 #if defined(_SUNOS_VTOC_8)
23112 	int			ncyl;
23113 	struct dk_map2		*lpart;
23114 #endif	/* defined(_SUNOS_VTOC_8) */
23115 	int			i;
23116 
23117 	ASSERT(mutex_owned(SD_MUTEX(un)));
23118 
23119 	/* Sanity-check the vtoc */
23120 	if (user_vtoc->v_sanity != VTOC_SANE ||
23121 	    user_vtoc->v_sectorsz != un->un_sys_blocksize ||
23122 	    user_vtoc->v_nparts != V_NUMPAR) {
23123 		return (EINVAL);
23124 	}
23125 
23126 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
23127 	if (nblks == 0) {
23128 		return (EINVAL);
23129 	}
23130 
23131 #if defined(_SUNOS_VTOC_8)
23132 	vpart = user_vtoc->v_part;
23133 	for (i = 0; i < V_NUMPAR; i++) {
23134 		if ((vpart->p_start % nblks) != 0) {
23135 			return (EINVAL);
23136 		}
23137 		ncyl = vpart->p_start / nblks;
23138 		ncyl += vpart->p_size / nblks;
23139 		if ((vpart->p_size % nblks) != 0) {
23140 			ncyl++;
23141 		}
23142 		if (ncyl > (int)un->un_g.dkg_ncyl) {
23143 			return (EINVAL);
23144 		}
23145 		vpart++;
23146 	}
23147 #endif	/* defined(_SUNOS_VTOC_8) */
23148 
23149 	/* Put appropriate vtoc structure fields into the disk label */
23150 #if defined(_SUNOS_VTOC_16)
23151 	/*
23152 	 * The vtoc is always a 32bit data structure to maintain the
23153 	 * on-disk format. Convert "in place" instead of bcopying it.
23154 	 */
23155 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc))));
23156 
23157 	/*
23158 	 * in the 16-slice vtoc, starting sectors are expressed in
23159 	 * numbers *relative* to the start of the Solaris fdisk partition.
23160 	 */
23161 	lmap = un->un_map;
23162 	vpart = user_vtoc->v_part;
23163 
23164 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
23165 		lmap->dkl_cylno = vpart->p_start / nblks;
23166 		lmap->dkl_nblk = vpart->p_size;
23167 	}
23168 
23169 #elif defined(_SUNOS_VTOC_8)
23170 
23171 	un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
23172 	un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
23173 	un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
23174 
23175 	un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
23176 	un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version;
23177 
23178 	bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL);
23179 
23180 	un->un_vtoc.v_nparts = user_vtoc->v_nparts;
23181 
23182 	bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved,
23183 	    sizeof (un->un_vtoc.v_reserved));
23184 
23185 	/*
23186 	 * Note the conversion from starting sector number
23187 	 * to starting cylinder number.
23188 	 * Return error if division results in a remainder.
23189 	 */
23190 	lmap = un->un_map;
23191 	lpart = un->un_vtoc.v_part;
23192 	vpart = user_vtoc->v_part;
23193 
23194 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
23195 		lpart->p_tag  = vpart->p_tag;
23196 		lpart->p_flag = vpart->p_flag;
23197 		lmap->dkl_cylno = vpart->p_start / nblks;
23198 		lmap->dkl_nblk = vpart->p_size;
23199 
23200 		lmap++;
23201 		lpart++;
23202 		vpart++;
23203 
23204 		/* (4387723) */
23205 #ifdef _LP64
23206 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
23207 			un->un_vtoc.v_timestamp[i] = TIME32_MAX;
23208 		} else {
23209 			un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
23210 		}
23211 #else
23212 		un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
23213 #endif
23214 	}
23215 
23216 	bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
23217 #else
23218 #error "No VTOC format defined."
23219 #endif
23220 	return (0);
23221 }
23222 
23223 /*
23224  *    Function: sd_clear_efi
23225  *
23226  * Description: This routine clears all EFI labels.
23227  *
23228  *   Arguments: un - driver soft state (unit) structure
23229  *
23230  * Return Code: void
23231  */
23232 
23233 static void
23234 sd_clear_efi(struct sd_lun *un)
23235 {
23236 	efi_gpt_t	*gpt;
23237 	uint_t		lbasize;
23238 	uint64_t	cap;
23239 	int rval;
23240 
23241 	ASSERT(!mutex_owned(SD_MUTEX(un)));
23242 
23243 	gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP);
23244 
23245 	if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) {
23246 		goto done;
23247 	}
23248 
23249 	sd_swap_efi_gpt(gpt);
23250 	rval = sd_validate_efi(gpt);
23251 	if (rval == 0) {
23252 		/* clear primary */
23253 		bzero(gpt, sizeof (efi_gpt_t));
23254 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1,
23255 			SD_PATH_DIRECT))) {
23256 			SD_INFO(SD_LOG_IO_PARTITION, un,
23257 				"sd_clear_efi: clear primary label failed\n");
23258 		}
23259 	}
23260 	/* the backup */
23261 	rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
23262 	    SD_PATH_DIRECT);
23263 	if (rval) {
23264 		goto done;
23265 	}
23266 	/*
23267 	 * The MMC standard allows READ CAPACITY to be
23268 	 * inaccurate by a bounded amount (in the interest of
23269 	 * response latency).  As a result, failed READs are
23270 	 * commonplace (due to the reading of metadata and not
23271 	 * data). Depending on the per-Vendor/drive Sense data,
23272 	 * the failed READ can cause many (unnecessary) retries.
23273 	 */
23274 	if ((rval = sd_send_scsi_READ(un, gpt, lbasize,
23275 	    cap - 1, ISCD(un) ? SD_PATH_DIRECT_PRIORITY :
23276 		SD_PATH_DIRECT)) != 0) {
23277 		goto done;
23278 	}
23279 	sd_swap_efi_gpt(gpt);
23280 	rval = sd_validate_efi(gpt);
23281 	if (rval == 0) {
23282 		/* clear backup */
23283 		SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n",
23284 			cap-1);
23285 		bzero(gpt, sizeof (efi_gpt_t));
23286 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE,
23287 		    cap-1, SD_PATH_DIRECT))) {
23288 			SD_INFO(SD_LOG_IO_PARTITION, un,
23289 				"sd_clear_efi: clear backup label failed\n");
23290 		}
23291 	}
23292 
23293 done:
23294 	kmem_free(gpt, sizeof (efi_gpt_t));
23295 }
23296 
23297 /*
23298  *    Function: sd_set_vtoc
23299  *
23300  * Description: This routine writes data to the appropriate positions
23301  *
23302  *   Arguments: un - driver soft state (unit) structure
23303  *              dkl  - the data to be written
23304  *
23305  * Return: void
23306  */
23307 
23308 static int
23309 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl)
23310 {
23311 	void			*shadow_buf;
23312 	uint_t			label_addr;
23313 	int			sec;
23314 	int			blk;
23315 	int			head;
23316 	int			cyl;
23317 	int			rval;
23318 
23319 #if defined(__i386) || defined(__amd64)
23320 	label_addr = un->un_solaris_offset + DK_LABEL_LOC;
23321 #else
23322 	/* Write the primary label at block 0 of the solaris partition. */
23323 	label_addr = 0;
23324 #endif
23325 
23326 	if (NOT_DEVBSIZE(un)) {
23327 		shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP);
23328 		/*
23329 		 * Read the target's first block.
23330 		 */
23331 		if ((rval = sd_send_scsi_READ(un, shadow_buf,
23332 		    un->un_tgt_blocksize, label_addr,
23333 		    SD_PATH_STANDARD)) != 0) {
23334 			goto exit;
23335 		}
23336 		/*
23337 		 * Copy the contents of the label into the shadow buffer
23338 		 * which is of the size of target block size.
23339 		 */
23340 		bcopy(dkl, shadow_buf, sizeof (struct dk_label));
23341 	}
23342 
23343 	/* Write the primary label */
23344 	if (NOT_DEVBSIZE(un)) {
23345 		rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize,
23346 		    label_addr, SD_PATH_STANDARD);
23347 	} else {
23348 		rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
23349 		    label_addr, SD_PATH_STANDARD);
23350 	}
23351 	if (rval != 0) {
23352 		return (rval);
23353 	}
23354 
23355 	/*
23356 	 * Calculate where the backup labels go.  They are always on
23357 	 * the last alternate cylinder, but some older drives put them
23358 	 * on head 2 instead of the last head.	They are always on the
23359 	 * first 5 odd sectors of the appropriate track.
23360 	 *
23361 	 * We have no choice at this point, but to believe that the
23362 	 * disk label is valid.	 Use the geometry of the disk
23363 	 * as described in the label.
23364 	 */
23365 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
23366 	head = dkl->dkl_nhead - 1;
23367 
23368 	/*
23369 	 * Write and verify the backup labels. Make sure we don't try to
23370 	 * write past the last cylinder.
23371 	 */
23372 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
23373 		blk = (daddr_t)(
23374 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
23375 		    (head * dkl->dkl_nsect) + sec);
23376 #if defined(__i386) || defined(__amd64)
23377 		blk += un->un_solaris_offset;
23378 #endif
23379 		if (NOT_DEVBSIZE(un)) {
23380 			uint64_t	tblk;
23381 			/*
23382 			 * Need to read the block first for read modify write.
23383 			 */
23384 			tblk = (uint64_t)blk;
23385 			blk = (int)((tblk * un->un_sys_blocksize) /
23386 			    un->un_tgt_blocksize);
23387 			if ((rval = sd_send_scsi_READ(un, shadow_buf,
23388 			    un->un_tgt_blocksize, blk,
23389 			    SD_PATH_STANDARD)) != 0) {
23390 				goto exit;
23391 			}
23392 			/*
23393 			 * Modify the shadow buffer with the label.
23394 			 */
23395 			bcopy(dkl, shadow_buf, sizeof (struct dk_label));
23396 			rval = sd_send_scsi_WRITE(un, shadow_buf,
23397 			    un->un_tgt_blocksize, blk, SD_PATH_STANDARD);
23398 		} else {
23399 			rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
23400 			    blk, SD_PATH_STANDARD);
23401 			SD_INFO(SD_LOG_IO_PARTITION, un,
23402 			"sd_set_vtoc: wrote backup label %d\n", blk);
23403 		}
23404 		if (rval != 0) {
23405 			goto exit;
23406 		}
23407 	}
23408 exit:
23409 	if (NOT_DEVBSIZE(un)) {
23410 		kmem_free(shadow_buf, un->un_tgt_blocksize);
23411 	}
23412 	return (rval);
23413 }
23414 
23415 /*
23416  *    Function: sd_clear_vtoc
23417  *
23418  * Description: This routine clears out the VTOC labels.
23419  *
23420  *   Arguments: un - driver soft state (unit) structure
23421  *
23422  * Return: void
23423  */
23424 
23425 static void
23426 sd_clear_vtoc(struct sd_lun *un)
23427 {
23428 	struct dk_label		*dkl;
23429 
23430 	mutex_exit(SD_MUTEX(un));
23431 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
23432 	mutex_enter(SD_MUTEX(un));
23433 	/*
23434 	 * sd_set_vtoc uses these fields in order to figure out
23435 	 * where to overwrite the backup labels
23436 	 */
23437 	dkl->dkl_apc    = un->un_g.dkg_apc;
23438 	dkl->dkl_ncyl   = un->un_g.dkg_ncyl;
23439 	dkl->dkl_acyl   = un->un_g.dkg_acyl;
23440 	dkl->dkl_nhead  = un->un_g.dkg_nhead;
23441 	dkl->dkl_nsect  = un->un_g.dkg_nsect;
23442 	mutex_exit(SD_MUTEX(un));
23443 	(void) sd_set_vtoc(un, dkl);
23444 	kmem_free(dkl, sizeof (struct dk_label));
23445 
23446 	mutex_enter(SD_MUTEX(un));
23447 }
23448 
23449 /*
23450  *    Function: sd_write_label
23451  *
23452  * Description: This routine will validate and write the driver soft state vtoc
23453  *		contents to the device.
23454  *
23455  *   Arguments: dev - the device number
23456  *
23457  * Return Code: the code returned by sd_send_scsi_cmd()
23458  *		0
23459  *		EINVAL
23460  *		ENXIO
23461  *		ENOMEM
23462  */
23463 
23464 static int
23465 sd_write_label(dev_t dev)
23466 {
23467 	struct sd_lun		*un;
23468 	struct dk_label		*dkl;
23469 	short			sum;
23470 	short			*sp;
23471 	int			i;
23472 	int			rval;
23473 
23474 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
23475 	    (un->un_state == SD_STATE_OFFLINE)) {
23476 		return (ENXIO);
23477 	}
23478 	ASSERT(mutex_owned(SD_MUTEX(un)));
23479 	mutex_exit(SD_MUTEX(un));
23480 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
23481 	mutex_enter(SD_MUTEX(un));
23482 
23483 	bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
23484 	dkl->dkl_rpm	= un->un_g.dkg_rpm;
23485 	dkl->dkl_pcyl	= un->un_g.dkg_pcyl;
23486 	dkl->dkl_apc	= un->un_g.dkg_apc;
23487 	dkl->dkl_intrlv = un->un_g.dkg_intrlv;
23488 	dkl->dkl_ncyl	= un->un_g.dkg_ncyl;
23489 	dkl->dkl_acyl	= un->un_g.dkg_acyl;
23490 	dkl->dkl_nhead	= un->un_g.dkg_nhead;
23491 	dkl->dkl_nsect	= un->un_g.dkg_nsect;
23492 
23493 #if defined(_SUNOS_VTOC_8)
23494 	dkl->dkl_obs1	= un->un_g.dkg_obs1;
23495 	dkl->dkl_obs2	= un->un_g.dkg_obs2;
23496 	dkl->dkl_obs3	= un->un_g.dkg_obs3;
23497 	for (i = 0; i < NDKMAP; i++) {
23498 		dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno;
23499 		dkl->dkl_map[i].dkl_nblk  = un->un_map[i].dkl_nblk;
23500 	}
23501 	bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
23502 #elif defined(_SUNOS_VTOC_16)
23503 	dkl->dkl_skew	= un->un_dkg_skew;
23504 #else
23505 #error "No VTOC format defined."
23506 #endif
23507 
23508 	dkl->dkl_magic			= DKL_MAGIC;
23509 	dkl->dkl_write_reinstruct	= un->un_g.dkg_write_reinstruct;
23510 	dkl->dkl_read_reinstruct	= un->un_g.dkg_read_reinstruct;
23511 
23512 	/* Construct checksum for the new disk label */
23513 	sum = 0;
23514 	sp = (short *)dkl;
23515 	i = sizeof (struct dk_label) / sizeof (short);
23516 	while (i--) {
23517 		sum ^= *sp++;
23518 	}
23519 	dkl->dkl_cksum = sum;
23520 
23521 	mutex_exit(SD_MUTEX(un));
23522 
23523 	rval = sd_set_vtoc(un, dkl);
23524 exit:
23525 	kmem_free(dkl, sizeof (struct dk_label));
23526 	mutex_enter(SD_MUTEX(un));
23527 	return (rval);
23528 }
23529 
23530 static int
23531 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag)
23532 {
23533 	struct sd_lun	*un = NULL;
23534 	dk_efi_t	user_efi;
23535 	int		rval = 0;
23536 	void		*buffer;
23537 
23538 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
23539 		return (ENXIO);
23540 
23541 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
23542 		return (EFAULT);
23543 
23544 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
23545 
23546 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
23547 	    (user_efi.dki_length > un->un_max_xfer_size))
23548 		return (EINVAL);
23549 
23550 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
23551 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
23552 		rval = EFAULT;
23553 	} else {
23554 		/*
23555 		 * let's clear the vtoc labels and clear the softstate
23556 		 * vtoc.
23557 		 */
23558 		mutex_enter(SD_MUTEX(un));
23559 		if (un->un_vtoc.v_sanity == VTOC_SANE) {
23560 			SD_TRACE(SD_LOG_IO_PARTITION, un,
23561 				"sd_dkio_set_efi: CLEAR VTOC\n");
23562 			sd_clear_vtoc(un);
23563 			bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
23564 			mutex_exit(SD_MUTEX(un));
23565 			ddi_remove_minor_node(SD_DEVINFO(un), "h");
23566 			ddi_remove_minor_node(SD_DEVINFO(un), "h,raw");
23567 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd",
23568 			    S_IFBLK,
23569 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23570 			    un->un_node_type, NULL);
23571 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw",
23572 			    S_IFCHR,
23573 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
23574 			    un->un_node_type, NULL);
23575 		} else
23576 			mutex_exit(SD_MUTEX(un));
23577 		rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length,
23578 		    user_efi.dki_lba, SD_PATH_DIRECT);
23579 		if (rval == 0) {
23580 			mutex_enter(SD_MUTEX(un));
23581 			un->un_f_geometry_is_valid = FALSE;
23582 			mutex_exit(SD_MUTEX(un));
23583 		}
23584 	}
23585 	kmem_free(buffer, user_efi.dki_length);
23586 	return (rval);
23587 }
23588 
23589 /*
23590  *    Function: sd_dkio_get_mboot
23591  *
23592  * Description: This routine is the driver entry point for handling user
23593  *		requests to get the current device mboot (DKIOCGMBOOT)
23594  *
23595  *   Arguments: dev  - the device number
23596  *		arg  - pointer to user provided mboot structure specifying
23597  *			the current mboot.
23598  *		flag - this argument is a pass through to ddi_copyxxx()
23599  *		       directly from the mode argument of ioctl().
23600  *
23601  * Return Code: 0
23602  *		EINVAL
23603  *		EFAULT
23604  *		ENXIO
23605  */
23606 
23607 static int
23608 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag)
23609 {
23610 	struct sd_lun	*un;
23611 	struct mboot	*mboot;
23612 	int		rval;
23613 	size_t		buffer_size;
23614 
23615 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
23616 	    (un->un_state == SD_STATE_OFFLINE)) {
23617 		return (ENXIO);
23618 	}
23619 
23620 	if (!un->un_f_mboot_supported || arg == NULL) {
23621 		return (EINVAL);
23622 	}
23623 
23624 	/*
23625 	 * Read the mboot block, located at absolute block 0 on the target.
23626 	 */
23627 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot));
23628 
23629 	SD_TRACE(SD_LOG_IO_PARTITION, un,
23630 	    "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
23631 
23632 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
23633 	if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0,
23634 	    SD_PATH_STANDARD)) == 0) {
23635 		if (ddi_copyout(mboot, (void *)arg,
23636 		    sizeof (struct mboot), flag) != 0) {
23637 			rval = EFAULT;
23638 		}
23639 	}
23640 	kmem_free(mboot, buffer_size);
23641 	return (rval);
23642 }
23643 
23644 
23645 /*
23646  *    Function: sd_dkio_set_mboot
23647  *
23648  * Description: This routine is the driver entry point for handling user
23649  *		requests to validate and set the device master boot
23650  *		(DKIOCSMBOOT).
23651  *
23652  *   Arguments: dev  - the device number
23653  *		arg  - pointer to user provided mboot structure used to set the
23654  *			master boot.
23655  *		flag - this argument is a pass through to ddi_copyxxx()
23656  *		       directly from the mode argument of ioctl().
23657  *
23658  * Return Code: 0
23659  *		EINVAL
23660  *		EFAULT
23661  *		ENXIO
23662  */
23663 
23664 static int
23665 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag)
23666 {
23667 	struct sd_lun	*un = NULL;
23668 	struct mboot	*mboot = NULL;
23669 	int		rval;
23670 	ushort_t	magic;
23671 
23672 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23673 		return (ENXIO);
23674 	}
23675 
23676 	ASSERT(!mutex_owned(SD_MUTEX(un)));
23677 
23678 	if (!un->un_f_mboot_supported) {
23679 		return (EINVAL);
23680 	}
23681 
23682 	if (arg == NULL) {
23683 		return (EINVAL);
23684 	}
23685 
23686 	mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP);
23687 
23688 	if (ddi_copyin((const void *)arg, mboot,
23689 	    sizeof (struct mboot), flag) != 0) {
23690 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23691 		return (EFAULT);
23692 	}
23693 
23694 	/* Is this really a master boot record? */
23695 	magic = LE_16(mboot->signature);
23696 	if (magic != MBB_MAGIC) {
23697 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23698 		return (EINVAL);
23699 	}
23700 
23701 	rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0,
23702 	    SD_PATH_STANDARD);
23703 
23704 	mutex_enter(SD_MUTEX(un));
23705 #if defined(__i386) || defined(__amd64)
23706 	if (rval == 0) {
23707 		/*
23708 		 * mboot has been written successfully.
23709 		 * update the fdisk and vtoc tables in memory
23710 		 */
23711 		rval = sd_update_fdisk_and_vtoc(un);
23712 		if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) {
23713 			mutex_exit(SD_MUTEX(un));
23714 			kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23715 			return (rval);
23716 		}
23717 	}
23718 
23719 	/*
23720 	 * If the mboot write fails, write the devid anyway, what can it hurt?
23721 	 * Also preserve the device id by writing to the disk acyl for the case
23722 	 * where a devid has been fabricated.
23723 	 */
23724 	if (un->un_f_devid_supported && un->un_f_opt_fab_devid) {
23725 		if (un->un_devid == NULL) {
23726 			sd_register_devid(un, SD_DEVINFO(un),
23727 			    SD_TARGET_IS_UNRESERVED);
23728 		} else {
23729 			/*
23730 			 * The device id for this disk has been
23731 			 * fabricated. Fabricated device id's are
23732 			 * managed by storing them in the last 2
23733 			 * available sectors on the drive. The device
23734 			 * id must be preserved by writing it back out
23735 			 * to this location.
23736 			 */
23737 			if (sd_write_deviceid(un) != 0) {
23738 				ddi_devid_free(un->un_devid);
23739 				un->un_devid = NULL;
23740 			}
23741 		}
23742 	}
23743 
23744 #ifdef __lock_lint
23745 	sd_setup_default_geometry(un);
23746 #endif
23747 
23748 #else
23749 	if (rval == 0) {
23750 		/*
23751 		 * mboot has been written successfully.
23752 		 * set up the default geometry and VTOC
23753 		 */
23754 		if (un->un_blockcount <= DK_MAX_BLOCKS)
23755 			sd_setup_default_geometry(un);
23756 	}
23757 #endif
23758 	mutex_exit(SD_MUTEX(un));
23759 	kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23760 	return (rval);
23761 }
23762 
23763 
23764 /*
23765  *    Function: sd_setup_default_geometry
23766  *
23767  * Description: This local utility routine sets the default geometry as part of
23768  *		setting the device mboot.
23769  *
23770  *   Arguments: un - driver soft state (unit) structure
23771  *
23772  * Note: This may be redundant with sd_build_default_label.
23773  */
23774 
23775 static void
23776 sd_setup_default_geometry(struct sd_lun *un)
23777 {
23778 	/* zero out the soft state geometry and partition table. */
23779 	bzero(&un->un_g, sizeof (struct dk_geom));
23780 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
23781 	bzero(un->un_map, NDKMAP * (sizeof (struct dk_map)));
23782 	un->un_asciilabel[0] = '\0';
23783 
23784 	/*
23785 	 * For the rpm, we use the minimum for the disk.
23786 	 * For the head, cyl and number of sector per track,
23787 	 * if the capacity <= 1GB, head = 64, sect = 32.
23788 	 * else head = 255, sect 63
23789 	 * Note: the capacity should be equal to C*H*S values.
23790 	 * This will cause some truncation of size due to
23791 	 * round off errors. For CD-ROMs, this truncation can
23792 	 * have adverse side effects, so returning ncyl and
23793 	 * nhead as 1. The nsect will overflow for most of
23794 	 * CD-ROMs as nsect is of type ushort.
23795 	 */
23796 	if (ISCD(un)) {
23797 		un->un_g.dkg_ncyl = 1;
23798 		un->un_g.dkg_nhead = 1;
23799 		un->un_g.dkg_nsect = un->un_blockcount;
23800 	} else {
23801 		if (un->un_blockcount <= 0x1000) {
23802 			/* Needed for unlabeled SCSI floppies. */
23803 			un->un_g.dkg_nhead = 2;
23804 			un->un_g.dkg_ncyl = 80;
23805 			un->un_g.dkg_pcyl = 80;
23806 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
23807 		} else if (un->un_blockcount <= 0x200000) {
23808 			un->un_g.dkg_nhead = 64;
23809 			un->un_g.dkg_nsect = 32;
23810 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
23811 		} else {
23812 			un->un_g.dkg_nhead = 255;
23813 			un->un_g.dkg_nsect = 63;
23814 			un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63);
23815 		}
23816 		un->un_blockcount = un->un_g.dkg_ncyl *
23817 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
23818 	}
23819 	un->un_g.dkg_acyl = 0;
23820 	un->un_g.dkg_bcyl = 0;
23821 	un->un_g.dkg_intrlv = 1;
23822 	un->un_g.dkg_rpm = 200;
23823 	un->un_g.dkg_read_reinstruct = 0;
23824 	un->un_g.dkg_write_reinstruct = 0;
23825 	if (un->un_g.dkg_pcyl == 0) {
23826 		un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl;
23827 	}
23828 
23829 	un->un_map['a'-'a'].dkl_cylno = 0;
23830 	un->un_map['a'-'a'].dkl_nblk = un->un_blockcount;
23831 	un->un_map['c'-'a'].dkl_cylno = 0;
23832 	un->un_map['c'-'a'].dkl_nblk = un->un_blockcount;
23833 	un->un_f_geometry_is_valid = FALSE;
23834 }
23835 
23836 
23837 #if defined(__i386) || defined(__amd64)
23838 /*
23839  *    Function: sd_update_fdisk_and_vtoc
23840  *
23841  * Description: This local utility routine updates the device fdisk and vtoc
23842  *		as part of setting the device mboot.
23843  *
23844  *   Arguments: un - driver soft state (unit) structure
23845  *
23846  * Return Code: 0 for success or errno-type return code.
23847  *
23848  *    Note:x86: This looks like a duplicate of sd_validate_geometry(), but
23849  *		these did exist seperately in x86 sd.c!!!
23850  */
23851 
23852 static int
23853 sd_update_fdisk_and_vtoc(struct sd_lun *un)
23854 {
23855 	static char	labelstring[128];
23856 	static char	buf[256];
23857 	char		*label = 0;
23858 	int		count;
23859 	int		label_rc = 0;
23860 	int		gvalid = un->un_f_geometry_is_valid;
23861 	int		fdisk_rval;
23862 	int		lbasize;
23863 	int		capacity;
23864 
23865 	ASSERT(mutex_owned(SD_MUTEX(un)));
23866 
23867 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
23868 		return (EINVAL);
23869 	}
23870 
23871 	if (un->un_f_blockcount_is_valid == FALSE) {
23872 		return (EINVAL);
23873 	}
23874 
23875 #if defined(_SUNOS_VTOC_16)
23876 	/*
23877 	 * Set up the "whole disk" fdisk partition; this should always
23878 	 * exist, regardless of whether the disk contains an fdisk table
23879 	 * or vtoc.
23880 	 */
23881 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
23882 	un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount;
23883 #endif	/* defined(_SUNOS_VTOC_16) */
23884 
23885 	/*
23886 	 * copy the lbasize and capacity so that if they're
23887 	 * reset while we're not holding the SD_MUTEX(un), we will
23888 	 * continue to use valid values after the SD_MUTEX(un) is
23889 	 * reacquired.
23890 	 */
23891 	lbasize  = un->un_tgt_blocksize;
23892 	capacity = un->un_blockcount;
23893 
23894 	/*
23895 	 * refresh the logical and physical geometry caches.
23896 	 * (data from mode sense format/rigid disk geometry pages,
23897 	 * and scsi_ifgetcap("geometry").
23898 	 */
23899 	sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT);
23900 
23901 	/*
23902 	 * Only DIRECT ACCESS devices will have Sun labels.
23903 	 * CD's supposedly have a Sun label, too
23904 	 */
23905 	if (un->un_f_vtoc_label_supported) {
23906 		fdisk_rval = sd_read_fdisk(un, capacity, lbasize,
23907 		    SD_PATH_DIRECT);
23908 		if (fdisk_rval == SD_CMD_FAILURE) {
23909 			ASSERT(mutex_owned(SD_MUTEX(un)));
23910 			return (EIO);
23911 		}
23912 
23913 		if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) {
23914 			ASSERT(mutex_owned(SD_MUTEX(un)));
23915 			return (EACCES);
23916 		}
23917 
23918 		if (un->un_solaris_size <= DK_LABEL_LOC) {
23919 			/*
23920 			 * Found fdisk table but no Solaris partition entry,
23921 			 * so don't call sd_uselabel() and don't create
23922 			 * a default label.
23923 			 */
23924 			label_rc = 0;
23925 			un->un_f_geometry_is_valid = TRUE;
23926 			goto no_solaris_partition;
23927 		}
23928 
23929 #if defined(_SUNOS_VTOC_8)
23930 		label = (char *)un->un_asciilabel;
23931 #elif defined(_SUNOS_VTOC_16)
23932 		label = (char *)un->un_vtoc.v_asciilabel;
23933 #else
23934 #error "No VTOC format defined."
23935 #endif
23936 	} else if (capacity < 0) {
23937 		ASSERT(mutex_owned(SD_MUTEX(un)));
23938 		return (EINVAL);
23939 	}
23940 
23941 	/*
23942 	 * For Removable media We reach here if we have found a
23943 	 * SOLARIS PARTITION.
23944 	 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS
23945 	 * PARTITION has changed from the previous one, hence we will setup a
23946 	 * default VTOC in this case.
23947 	 */
23948 	if (un->un_f_geometry_is_valid == FALSE) {
23949 		sd_build_default_label(un);
23950 		label_rc = 0;
23951 	}
23952 
23953 no_solaris_partition:
23954 	if ((!un->un_f_has_removable_media ||
23955 	    (un->un_f_has_removable_media &&
23956 	    un->un_mediastate == DKIO_EJECTED)) &&
23957 		(un->un_state == SD_STATE_NORMAL && !gvalid)) {
23958 		/*
23959 		 * Print out a message indicating who and what we are.
23960 		 * We do this only when we happen to really validate the
23961 		 * geometry. We may call sd_validate_geometry() at other
23962 		 * times, ioctl()'s like Get VTOC in which case we
23963 		 * don't want to print the label.
23964 		 * If the geometry is valid, print the label string,
23965 		 * else print vendor and product info, if available
23966 		 */
23967 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
23968 			SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label);
23969 		} else {
23970 			mutex_enter(&sd_label_mutex);
23971 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
23972 			    labelstring);
23973 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
23974 			    &labelstring[64]);
23975 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
23976 			    labelstring, &labelstring[64]);
23977 			if (un->un_f_blockcount_is_valid == TRUE) {
23978 				(void) sprintf(&buf[strlen(buf)],
23979 				    ", %" PRIu64 " %u byte blocks\n",
23980 				    un->un_blockcount,
23981 				    un->un_tgt_blocksize);
23982 			} else {
23983 				(void) sprintf(&buf[strlen(buf)],
23984 				    ", (unknown capacity)\n");
23985 			}
23986 			SD_INFO(SD_LOG_IOCTL_DKIO, un, buf);
23987 			mutex_exit(&sd_label_mutex);
23988 		}
23989 	}
23990 
23991 #if defined(_SUNOS_VTOC_16)
23992 	/*
23993 	 * If we have valid geometry, set up the remaining fdisk partitions.
23994 	 * Note that dkl_cylno is not used for the fdisk map entries, so
23995 	 * we set it to an entirely bogus value.
23996 	 */
23997 	for (count = 0; count < FD_NUMPART; count++) {
23998 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
23999 		un->un_map[FDISK_P1 + count].dkl_nblk =
24000 		    un->un_fmap[count].fmap_nblk;
24001 		un->un_offset[FDISK_P1 + count] =
24002 		    un->un_fmap[count].fmap_start;
24003 	}
24004 #endif
24005 
24006 	for (count = 0; count < NDKMAP; count++) {
24007 #if defined(_SUNOS_VTOC_8)
24008 		struct dk_map *lp  = &un->un_map[count];
24009 		un->un_offset[count] =
24010 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
24011 #elif defined(_SUNOS_VTOC_16)
24012 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
24013 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
24014 #else
24015 #error "No VTOC format defined."
24016 #endif
24017 	}
24018 
24019 	ASSERT(mutex_owned(SD_MUTEX(un)));
24020 	return (label_rc);
24021 }
24022 #endif
24023 
24024 
24025 /*
24026  *    Function: sd_check_media
24027  *
24028  * Description: This utility routine implements the functionality for the
24029  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
24030  *		driver state changes from that specified by the user
24031  *		(inserted or ejected). For example, if the user specifies
24032  *		DKIO_EJECTED and the current media state is inserted this
24033  *		routine will immediately return DKIO_INSERTED. However, if the
24034  *		current media state is not inserted the user thread will be
24035  *		blocked until the drive state changes. If DKIO_NONE is specified
24036  *		the user thread will block until a drive state change occurs.
24037  *
24038  *   Arguments: dev  - the device number
24039  *		state  - user pointer to a dkio_state, updated with the current
24040  *			drive state at return.
24041  *
24042  * Return Code: ENXIO
24043  *		EIO
24044  *		EAGAIN
24045  *		EINTR
24046  */
24047 
24048 static int
24049 sd_check_media(dev_t dev, enum dkio_state state)
24050 {
24051 	struct sd_lun		*un = NULL;
24052 	enum dkio_state		prev_state;
24053 	opaque_t		token = NULL;
24054 	int			rval = 0;
24055 
24056 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24057 		return (ENXIO);
24058 	}
24059 
24060 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
24061 
24062 	mutex_enter(SD_MUTEX(un));
24063 
24064 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
24065 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
24066 
24067 	prev_state = un->un_mediastate;
24068 
24069 	/* is there anything to do? */
24070 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
24071 		/*
24072 		 * submit the request to the scsi_watch service;
24073 		 * scsi_media_watch_cb() does the real work
24074 		 */
24075 		mutex_exit(SD_MUTEX(un));
24076 
24077 		/*
24078 		 * This change handles the case where a scsi watch request is
24079 		 * added to a device that is powered down. To accomplish this
24080 		 * we power up the device before adding the scsi watch request,
24081 		 * since the scsi watch sends a TUR directly to the device
24082 		 * which the device cannot handle if it is powered down.
24083 		 */
24084 		if (sd_pm_entry(un) != DDI_SUCCESS) {
24085 			mutex_enter(SD_MUTEX(un));
24086 			goto done;
24087 		}
24088 
24089 		token = scsi_watch_request_submit(SD_SCSI_DEVP(un),
24090 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
24091 		    (caddr_t)dev);
24092 
24093 		sd_pm_exit(un);
24094 
24095 		mutex_enter(SD_MUTEX(un));
24096 		if (token == NULL) {
24097 			rval = EAGAIN;
24098 			goto done;
24099 		}
24100 
24101 		/*
24102 		 * This is a special case IOCTL that doesn't return
24103 		 * until the media state changes. Routine sdpower
24104 		 * knows about and handles this so don't count it
24105 		 * as an active cmd in the driver, which would
24106 		 * keep the device busy to the pm framework.
24107 		 * If the count isn't decremented the device can't
24108 		 * be powered down.
24109 		 */
24110 		un->un_ncmds_in_driver--;
24111 		ASSERT(un->un_ncmds_in_driver >= 0);
24112 
24113 		/*
24114 		 * if a prior request had been made, this will be the same
24115 		 * token, as scsi_watch was designed that way.
24116 		 */
24117 		un->un_swr_token = token;
24118 		un->un_specified_mediastate = state;
24119 
24120 		/*
24121 		 * now wait for media change
24122 		 * we will not be signalled unless mediastate == state but it is
24123 		 * still better to test for this condition, since there is a
24124 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
24125 		 */
24126 		SD_TRACE(SD_LOG_COMMON, un,
24127 		    "sd_check_media: waiting for media state change\n");
24128 		while (un->un_mediastate == state) {
24129 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
24130 				SD_TRACE(SD_LOG_COMMON, un,
24131 				    "sd_check_media: waiting for media state "
24132 				    "was interrupted\n");
24133 				un->un_ncmds_in_driver++;
24134 				rval = EINTR;
24135 				goto done;
24136 			}
24137 			SD_TRACE(SD_LOG_COMMON, un,
24138 			    "sd_check_media: received signal, state=%x\n",
24139 			    un->un_mediastate);
24140 		}
24141 		/*
24142 		 * Inc the counter to indicate the device once again
24143 		 * has an active outstanding cmd.
24144 		 */
24145 		un->un_ncmds_in_driver++;
24146 	}
24147 
24148 	/* invalidate geometry */
24149 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
24150 		sr_ejected(un);
24151 	}
24152 
24153 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
24154 		uint64_t	capacity;
24155 		uint_t		lbasize;
24156 
24157 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
24158 		mutex_exit(SD_MUTEX(un));
24159 		/*
24160 		 * Since the following routines use SD_PATH_DIRECT, we must
24161 		 * call PM directly before the upcoming disk accesses. This
24162 		 * may cause the disk to be power/spin up.
24163 		 */
24164 
24165 		if (sd_pm_entry(un) == DDI_SUCCESS) {
24166 			rval = sd_send_scsi_READ_CAPACITY(un,
24167 			    &capacity,
24168 			    &lbasize, SD_PATH_DIRECT);
24169 			if (rval != 0) {
24170 				sd_pm_exit(un);
24171 				mutex_enter(SD_MUTEX(un));
24172 				goto done;
24173 			}
24174 		} else {
24175 			rval = EIO;
24176 			mutex_enter(SD_MUTEX(un));
24177 			goto done;
24178 		}
24179 		mutex_enter(SD_MUTEX(un));
24180 
24181 		sd_update_block_info(un, lbasize, capacity);
24182 
24183 		un->un_f_geometry_is_valid	= FALSE;
24184 		(void) sd_validate_geometry(un, SD_PATH_DIRECT);
24185 
24186 		mutex_exit(SD_MUTEX(un));
24187 		rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
24188 		    SD_PATH_DIRECT);
24189 		sd_pm_exit(un);
24190 
24191 		mutex_enter(SD_MUTEX(un));
24192 	}
24193 done:
24194 	un->un_f_watcht_stopped = FALSE;
24195 	if (un->un_swr_token) {
24196 		/*
24197 		 * Use of this local token and the mutex ensures that we avoid
24198 		 * some race conditions associated with terminating the
24199 		 * scsi watch.
24200 		 */
24201 		token = un->un_swr_token;
24202 		un->un_swr_token = (opaque_t)NULL;
24203 		mutex_exit(SD_MUTEX(un));
24204 		(void) scsi_watch_request_terminate(token,
24205 		    SCSI_WATCH_TERMINATE_WAIT);
24206 		mutex_enter(SD_MUTEX(un));
24207 	}
24208 
24209 	/*
24210 	 * Update the capacity kstat value, if no media previously
24211 	 * (capacity kstat is 0) and a media has been inserted
24212 	 * (un_f_blockcount_is_valid == TRUE)
24213 	 */
24214 	if (un->un_errstats) {
24215 		struct sd_errstats	*stp = NULL;
24216 
24217 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
24218 		if ((stp->sd_capacity.value.ui64 == 0) &&
24219 		    (un->un_f_blockcount_is_valid == TRUE)) {
24220 			stp->sd_capacity.value.ui64 =
24221 			    (uint64_t)((uint64_t)un->un_blockcount *
24222 			    un->un_sys_blocksize);
24223 		}
24224 	}
24225 	mutex_exit(SD_MUTEX(un));
24226 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
24227 	return (rval);
24228 }
24229 
24230 
24231 /*
24232  *    Function: sd_delayed_cv_broadcast
24233  *
24234  * Description: Delayed cv_broadcast to allow for target to recover from media
24235  *		insertion.
24236  *
24237  *   Arguments: arg - driver soft state (unit) structure
24238  */
24239 
24240 static void
24241 sd_delayed_cv_broadcast(void *arg)
24242 {
24243 	struct sd_lun *un = arg;
24244 
24245 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
24246 
24247 	mutex_enter(SD_MUTEX(un));
24248 	un->un_dcvb_timeid = NULL;
24249 	cv_broadcast(&un->un_state_cv);
24250 	mutex_exit(SD_MUTEX(un));
24251 }
24252 
24253 
24254 /*
24255  *    Function: sd_media_watch_cb
24256  *
24257  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
24258  *		routine processes the TUR sense data and updates the driver
24259  *		state if a transition has occurred. The user thread
24260  *		(sd_check_media) is then signalled.
24261  *
24262  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24263  *			among multiple watches that share this callback function
24264  *		resultp - scsi watch facility result packet containing scsi
24265  *			  packet, status byte and sense data
24266  *
24267  * Return Code: 0 for success, -1 for failure
24268  */
24269 
24270 static int
24271 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24272 {
24273 	struct sd_lun			*un;
24274 	struct scsi_status		*statusp = resultp->statusp;
24275 	struct scsi_extended_sense	*sensep = resultp->sensep;
24276 	enum dkio_state			state = DKIO_NONE;
24277 	dev_t				dev = (dev_t)arg;
24278 	uchar_t				actual_sense_length;
24279 
24280 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24281 		return (-1);
24282 	}
24283 	actual_sense_length = resultp->actual_sense_length;
24284 
24285 	mutex_enter(SD_MUTEX(un));
24286 	SD_TRACE(SD_LOG_COMMON, un,
24287 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
24288 	    *((char *)statusp), (void *)sensep, actual_sense_length);
24289 
24290 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
24291 		un->un_mediastate = DKIO_DEV_GONE;
24292 		cv_broadcast(&un->un_state_cv);
24293 		mutex_exit(SD_MUTEX(un));
24294 
24295 		return (0);
24296 	}
24297 
24298 	/*
24299 	 * If there was a check condition then sensep points to valid sense data
24300 	 * If status was not a check condition but a reservation or busy status
24301 	 * then the new state is DKIO_NONE
24302 	 */
24303 	if (sensep != NULL) {
24304 		SD_INFO(SD_LOG_COMMON, un,
24305 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
24306 		    sensep->es_key, sensep->es_add_code, sensep->es_qual_code);
24307 		/* This routine only uses up to 13 bytes of sense data. */
24308 		if (actual_sense_length >= 13) {
24309 			if (sensep->es_key == KEY_UNIT_ATTENTION) {
24310 				if (sensep->es_add_code == 0x28) {
24311 					state = DKIO_INSERTED;
24312 				}
24313 			} else {
24314 				/*
24315 				 * if 02/04/02  means that the host
24316 				 * should send start command. Explicitly
24317 				 * leave the media state as is
24318 				 * (inserted) as the media is inserted
24319 				 * and host has stopped device for PM
24320 				 * reasons. Upon next true read/write
24321 				 * to this media will bring the
24322 				 * device to the right state good for
24323 				 * media access.
24324 				 */
24325 				if ((sensep->es_key == KEY_NOT_READY) &&
24326 				    (sensep->es_add_code == 0x3a)) {
24327 					state = DKIO_EJECTED;
24328 				}
24329 
24330 				/*
24331 				 * If the drivge is busy with an operation
24332 				 * or long write, keep the media in an
24333 				 * inserted state.
24334 				 */
24335 
24336 				if ((sensep->es_key == KEY_NOT_READY) &&
24337 				    (sensep->es_add_code == 0x04) &&
24338 				    ((sensep->es_qual_code == 0x02) ||
24339 				    (sensep->es_qual_code == 0x07) ||
24340 				    (sensep->es_qual_code == 0x08))) {
24341 					state = DKIO_INSERTED;
24342 				}
24343 			}
24344 		}
24345 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
24346 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
24347 		state = DKIO_INSERTED;
24348 	}
24349 
24350 	SD_TRACE(SD_LOG_COMMON, un,
24351 	    "sd_media_watch_cb: state=%x, specified=%x\n",
24352 	    state, un->un_specified_mediastate);
24353 
24354 	/*
24355 	 * now signal the waiting thread if this is *not* the specified state;
24356 	 * delay the signal if the state is DKIO_INSERTED to allow the target
24357 	 * to recover
24358 	 */
24359 	if (state != un->un_specified_mediastate) {
24360 		un->un_mediastate = state;
24361 		if (state == DKIO_INSERTED) {
24362 			/*
24363 			 * delay the signal to give the drive a chance
24364 			 * to do what it apparently needs to do
24365 			 */
24366 			SD_TRACE(SD_LOG_COMMON, un,
24367 			    "sd_media_watch_cb: delayed cv_broadcast\n");
24368 			if (un->un_dcvb_timeid == NULL) {
24369 				un->un_dcvb_timeid =
24370 				    timeout(sd_delayed_cv_broadcast, un,
24371 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
24372 			}
24373 		} else {
24374 			SD_TRACE(SD_LOG_COMMON, un,
24375 			    "sd_media_watch_cb: immediate cv_broadcast\n");
24376 			cv_broadcast(&un->un_state_cv);
24377 		}
24378 	}
24379 	mutex_exit(SD_MUTEX(un));
24380 	return (0);
24381 }
24382 
24383 
24384 /*
24385  *    Function: sd_dkio_get_temp
24386  *
24387  * Description: This routine is the driver entry point for handling ioctl
24388  *		requests to get the disk temperature.
24389  *
24390  *   Arguments: dev  - the device number
24391  *		arg  - pointer to user provided dk_temperature structure.
24392  *		flag - this argument is a pass through to ddi_copyxxx()
24393  *		       directly from the mode argument of ioctl().
24394  *
24395  * Return Code: 0
24396  *		EFAULT
24397  *		ENXIO
24398  *		EAGAIN
24399  */
24400 
24401 static int
24402 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
24403 {
24404 	struct sd_lun		*un = NULL;
24405 	struct dk_temperature	*dktemp = NULL;
24406 	uchar_t			*temperature_page;
24407 	int			rval = 0;
24408 	int			path_flag = SD_PATH_STANDARD;
24409 
24410 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24411 		return (ENXIO);
24412 	}
24413 
24414 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
24415 
24416 	/* copyin the disk temp argument to get the user flags */
24417 	if (ddi_copyin((void *)arg, dktemp,
24418 	    sizeof (struct dk_temperature), flag) != 0) {
24419 		rval = EFAULT;
24420 		goto done;
24421 	}
24422 
24423 	/* Initialize the temperature to invalid. */
24424 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24425 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24426 
24427 	/*
24428 	 * Note: Investigate removing the "bypass pm" semantic.
24429 	 * Can we just bypass PM always?
24430 	 */
24431 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
24432 		path_flag = SD_PATH_DIRECT;
24433 		ASSERT(!mutex_owned(&un->un_pm_mutex));
24434 		mutex_enter(&un->un_pm_mutex);
24435 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
24436 			/*
24437 			 * If DKT_BYPASS_PM is set, and the drive happens to be
24438 			 * in low power mode, we can not wake it up, Need to
24439 			 * return EAGAIN.
24440 			 */
24441 			mutex_exit(&un->un_pm_mutex);
24442 			rval = EAGAIN;
24443 			goto done;
24444 		} else {
24445 			/*
24446 			 * Indicate to PM the device is busy. This is required
24447 			 * to avoid a race - i.e. the ioctl is issuing a
24448 			 * command and the pm framework brings down the device
24449 			 * to low power mode (possible power cut-off on some
24450 			 * platforms).
24451 			 */
24452 			mutex_exit(&un->un_pm_mutex);
24453 			if (sd_pm_entry(un) != DDI_SUCCESS) {
24454 				rval = EAGAIN;
24455 				goto done;
24456 			}
24457 		}
24458 	}
24459 
24460 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
24461 
24462 	if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page,
24463 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) {
24464 		goto done2;
24465 	}
24466 
24467 	/*
24468 	 * For the current temperature verify that the parameter length is 0x02
24469 	 * and the parameter code is 0x00
24470 	 */
24471 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
24472 	    (temperature_page[5] == 0x00)) {
24473 		if (temperature_page[9] == 0xFF) {
24474 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
24475 		} else {
24476 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
24477 		}
24478 	}
24479 
24480 	/*
24481 	 * For the reference temperature verify that the parameter
24482 	 * length is 0x02 and the parameter code is 0x01
24483 	 */
24484 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
24485 	    (temperature_page[11] == 0x01)) {
24486 		if (temperature_page[15] == 0xFF) {
24487 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
24488 		} else {
24489 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
24490 		}
24491 	}
24492 
24493 	/* Do the copyout regardless of the temperature commands status. */
24494 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
24495 	    flag) != 0) {
24496 		rval = EFAULT;
24497 	}
24498 
24499 done2:
24500 	if (path_flag == SD_PATH_DIRECT) {
24501 		sd_pm_exit(un);
24502 	}
24503 
24504 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
24505 done:
24506 	if (dktemp != NULL) {
24507 		kmem_free(dktemp, sizeof (struct dk_temperature));
24508 	}
24509 
24510 	return (rval);
24511 }
24512 
24513 
24514 /*
24515  *    Function: sd_log_page_supported
24516  *
24517  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
24518  *		supported log pages.
24519  *
24520  *   Arguments: un -
24521  *		log_page -
24522  *
24523  * Return Code: -1 - on error (log sense is optional and may not be supported).
24524  *		0  - log page not found.
24525  *  		1  - log page found.
24526  */
24527 
24528 static int
24529 sd_log_page_supported(struct sd_lun *un, int log_page)
24530 {
24531 	uchar_t *log_page_data;
24532 	int	i;
24533 	int	match = 0;
24534 	int	log_size;
24535 
24536 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
24537 
24538 	if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0,
24539 	    SD_PATH_DIRECT) != 0) {
24540 		SD_ERROR(SD_LOG_COMMON, un,
24541 		    "sd_log_page_supported: failed log page retrieval\n");
24542 		kmem_free(log_page_data, 0xFF);
24543 		return (-1);
24544 	}
24545 	log_size = log_page_data[3];
24546 
24547 	/*
24548 	 * The list of supported log pages start from the fourth byte. Check
24549 	 * until we run out of log pages or a match is found.
24550 	 */
24551 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
24552 		if (log_page_data[i] == log_page) {
24553 			match++;
24554 		}
24555 	}
24556 	kmem_free(log_page_data, 0xFF);
24557 	return (match);
24558 }
24559 
24560 
24561 /*
24562  *    Function: sd_mhdioc_failfast
24563  *
24564  * Description: This routine is the driver entry point for handling ioctl
24565  *		requests to enable/disable the multihost failfast option.
24566  *		(MHIOCENFAILFAST)
24567  *
24568  *   Arguments: dev	- the device number
24569  *		arg	- user specified probing interval.
24570  *		flag	- this argument is a pass through to ddi_copyxxx()
24571  *			  directly from the mode argument of ioctl().
24572  *
24573  * Return Code: 0
24574  *		EFAULT
24575  *		ENXIO
24576  */
24577 
24578 static int
24579 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
24580 {
24581 	struct sd_lun	*un = NULL;
24582 	int		mh_time;
24583 	int		rval = 0;
24584 
24585 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24586 		return (ENXIO);
24587 	}
24588 
24589 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24590 		return (EFAULT);
24591 
24592 	if (mh_time) {
24593 		mutex_enter(SD_MUTEX(un));
24594 		un->un_resvd_status |= SD_FAILFAST;
24595 		mutex_exit(SD_MUTEX(un));
24596 		/*
24597 		 * If mh_time is INT_MAX, then this ioctl is being used for
24598 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24599 		 */
24600 		if (mh_time != INT_MAX) {
24601 			rval = sd_check_mhd(dev, mh_time);
24602 		}
24603 	} else {
24604 		(void) sd_check_mhd(dev, 0);
24605 		mutex_enter(SD_MUTEX(un));
24606 		un->un_resvd_status &= ~SD_FAILFAST;
24607 		mutex_exit(SD_MUTEX(un));
24608 	}
24609 	return (rval);
24610 }
24611 
24612 
24613 /*
24614  *    Function: sd_mhdioc_takeown
24615  *
24616  * Description: This routine is the driver entry point for handling ioctl
24617  *		requests to forcefully acquire exclusive access rights to the
24618  *		multihost disk (MHIOCTKOWN).
24619  *
24620  *   Arguments: dev	- the device number
24621  *		arg	- user provided structure specifying the delay
24622  *			  parameters in milliseconds
24623  *		flag	- this argument is a pass through to ddi_copyxxx()
24624  *			  directly from the mode argument of ioctl().
24625  *
24626  * Return Code: 0
24627  *		EFAULT
24628  *		ENXIO
24629  */
24630 
24631 static int
24632 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24633 {
24634 	struct sd_lun		*un = NULL;
24635 	struct mhioctkown	*tkown = NULL;
24636 	int			rval = 0;
24637 
24638 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24639 		return (ENXIO);
24640 	}
24641 
24642 	if (arg != NULL) {
24643 		tkown = (struct mhioctkown *)
24644 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24645 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24646 		if (rval != 0) {
24647 			rval = EFAULT;
24648 			goto error;
24649 		}
24650 	}
24651 
24652 	rval = sd_take_ownership(dev, tkown);
24653 	mutex_enter(SD_MUTEX(un));
24654 	if (rval == 0) {
24655 		un->un_resvd_status |= SD_RESERVE;
24656 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24657 			sd_reinstate_resv_delay =
24658 			    tkown->reinstate_resv_delay * 1000;
24659 		} else {
24660 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24661 		}
24662 		/*
24663 		 * Give the scsi_watch routine interval set by
24664 		 * the MHIOCENFAILFAST ioctl precedence here.
24665 		 */
24666 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24667 			mutex_exit(SD_MUTEX(un));
24668 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24669 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24670 			    "sd_mhdioc_takeown : %d\n",
24671 			    sd_reinstate_resv_delay);
24672 		} else {
24673 			mutex_exit(SD_MUTEX(un));
24674 		}
24675 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24676 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24677 	} else {
24678 		un->un_resvd_status &= ~SD_RESERVE;
24679 		mutex_exit(SD_MUTEX(un));
24680 	}
24681 
24682 error:
24683 	if (tkown != NULL) {
24684 		kmem_free(tkown, sizeof (struct mhioctkown));
24685 	}
24686 	return (rval);
24687 }
24688 
24689 
24690 /*
24691  *    Function: sd_mhdioc_release
24692  *
24693  * Description: This routine is the driver entry point for handling ioctl
24694  *		requests to release exclusive access rights to the multihost
24695  *		disk (MHIOCRELEASE).
24696  *
24697  *   Arguments: dev	- the device number
24698  *
24699  * Return Code: 0
24700  *		ENXIO
24701  */
24702 
24703 static int
24704 sd_mhdioc_release(dev_t dev)
24705 {
24706 	struct sd_lun		*un = NULL;
24707 	timeout_id_t		resvd_timeid_save;
24708 	int			resvd_status_save;
24709 	int			rval = 0;
24710 
24711 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24712 		return (ENXIO);
24713 	}
24714 
24715 	mutex_enter(SD_MUTEX(un));
24716 	resvd_status_save = un->un_resvd_status;
24717 	un->un_resvd_status &=
24718 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24719 	if (un->un_resvd_timeid) {
24720 		resvd_timeid_save = un->un_resvd_timeid;
24721 		un->un_resvd_timeid = NULL;
24722 		mutex_exit(SD_MUTEX(un));
24723 		(void) untimeout(resvd_timeid_save);
24724 	} else {
24725 		mutex_exit(SD_MUTEX(un));
24726 	}
24727 
24728 	/*
24729 	 * destroy any pending timeout thread that may be attempting to
24730 	 * reinstate reservation on this device.
24731 	 */
24732 	sd_rmv_resv_reclaim_req(dev);
24733 
24734 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24735 		mutex_enter(SD_MUTEX(un));
24736 		if ((un->un_mhd_token) &&
24737 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24738 			mutex_exit(SD_MUTEX(un));
24739 			(void) sd_check_mhd(dev, 0);
24740 		} else {
24741 			mutex_exit(SD_MUTEX(un));
24742 		}
24743 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24744 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24745 	} else {
24746 		/*
24747 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24748 		 */
24749 		mutex_enter(SD_MUTEX(un));
24750 		un->un_resvd_status = resvd_status_save;
24751 		mutex_exit(SD_MUTEX(un));
24752 	}
24753 	return (rval);
24754 }
24755 
24756 
24757 /*
24758  *    Function: sd_mhdioc_register_devid
24759  *
24760  * Description: This routine is the driver entry point for handling ioctl
24761  *		requests to register the device id (MHIOCREREGISTERDEVID).
24762  *
24763  *		Note: The implementation for this ioctl has been updated to
24764  *		be consistent with the original PSARC case (1999/357)
24765  *		(4375899, 4241671, 4220005)
24766  *
24767  *   Arguments: dev	- the device number
24768  *
24769  * Return Code: 0
24770  *		ENXIO
24771  */
24772 
24773 static int
24774 sd_mhdioc_register_devid(dev_t dev)
24775 {
24776 	struct sd_lun	*un = NULL;
24777 	int		rval = 0;
24778 
24779 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24780 		return (ENXIO);
24781 	}
24782 
24783 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24784 
24785 	mutex_enter(SD_MUTEX(un));
24786 
24787 	/* If a devid already exists, de-register it */
24788 	if (un->un_devid != NULL) {
24789 		ddi_devid_unregister(SD_DEVINFO(un));
24790 		/*
24791 		 * After unregister devid, needs to free devid memory
24792 		 */
24793 		ddi_devid_free(un->un_devid);
24794 		un->un_devid = NULL;
24795 	}
24796 
24797 	/* Check for reservation conflict */
24798 	mutex_exit(SD_MUTEX(un));
24799 	rval = sd_send_scsi_TEST_UNIT_READY(un, 0);
24800 	mutex_enter(SD_MUTEX(un));
24801 
24802 	switch (rval) {
24803 	case 0:
24804 		sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24805 		break;
24806 	case EACCES:
24807 		break;
24808 	default:
24809 		rval = EIO;
24810 	}
24811 
24812 	mutex_exit(SD_MUTEX(un));
24813 	return (rval);
24814 }
24815 
24816 
24817 /*
24818  *    Function: sd_mhdioc_inkeys
24819  *
24820  * Description: This routine is the driver entry point for handling ioctl
24821  *		requests to issue the SCSI-3 Persistent In Read Keys command
24822  *		to the device (MHIOCGRP_INKEYS).
24823  *
24824  *   Arguments: dev	- the device number
24825  *		arg	- user provided in_keys structure
24826  *		flag	- this argument is a pass through to ddi_copyxxx()
24827  *			  directly from the mode argument of ioctl().
24828  *
24829  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24830  *		ENXIO
24831  *		EFAULT
24832  */
24833 
24834 static int
24835 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24836 {
24837 	struct sd_lun		*un;
24838 	mhioc_inkeys_t		inkeys;
24839 	int			rval = 0;
24840 
24841 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24842 		return (ENXIO);
24843 	}
24844 
24845 #ifdef _MULTI_DATAMODEL
24846 	switch (ddi_model_convert_from(flag & FMODELS)) {
24847 	case DDI_MODEL_ILP32: {
24848 		struct mhioc_inkeys32	inkeys32;
24849 
24850 		if (ddi_copyin(arg, &inkeys32,
24851 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
24852 			return (EFAULT);
24853 		}
24854 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24855 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24856 		    &inkeys, flag)) != 0) {
24857 			return (rval);
24858 		}
24859 		inkeys32.generation = inkeys.generation;
24860 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24861 		    flag) != 0) {
24862 			return (EFAULT);
24863 		}
24864 		break;
24865 	}
24866 	case DDI_MODEL_NONE:
24867 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24868 		    flag) != 0) {
24869 			return (EFAULT);
24870 		}
24871 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24872 		    &inkeys, flag)) != 0) {
24873 			return (rval);
24874 		}
24875 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24876 		    flag) != 0) {
24877 			return (EFAULT);
24878 		}
24879 		break;
24880 	}
24881 
24882 #else /* ! _MULTI_DATAMODEL */
24883 
24884 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24885 		return (EFAULT);
24886 	}
24887 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24888 	if (rval != 0) {
24889 		return (rval);
24890 	}
24891 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24892 		return (EFAULT);
24893 	}
24894 
24895 #endif /* _MULTI_DATAMODEL */
24896 
24897 	return (rval);
24898 }
24899 
24900 
24901 /*
24902  *    Function: sd_mhdioc_inresv
24903  *
24904  * Description: This routine is the driver entry point for handling ioctl
24905  *		requests to issue the SCSI-3 Persistent In Read Reservations
24906  *		command to the device (MHIOCGRP_INKEYS).
24907  *
24908  *   Arguments: dev	- the device number
24909  *		arg	- user provided in_resv structure
24910  *		flag	- this argument is a pass through to ddi_copyxxx()
24911  *			  directly from the mode argument of ioctl().
24912  *
24913  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24914  *		ENXIO
24915  *		EFAULT
24916  */
24917 
24918 static int
24919 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24920 {
24921 	struct sd_lun		*un;
24922 	mhioc_inresvs_t		inresvs;
24923 	int			rval = 0;
24924 
24925 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24926 		return (ENXIO);
24927 	}
24928 
24929 #ifdef _MULTI_DATAMODEL
24930 
24931 	switch (ddi_model_convert_from(flag & FMODELS)) {
24932 	case DDI_MODEL_ILP32: {
24933 		struct mhioc_inresvs32	inresvs32;
24934 
24935 		if (ddi_copyin(arg, &inresvs32,
24936 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24937 			return (EFAULT);
24938 		}
24939 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24940 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24941 		    &inresvs, flag)) != 0) {
24942 			return (rval);
24943 		}
24944 		inresvs32.generation = inresvs.generation;
24945 		if (ddi_copyout(&inresvs32, arg,
24946 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24947 			return (EFAULT);
24948 		}
24949 		break;
24950 	}
24951 	case DDI_MODEL_NONE:
24952 		if (ddi_copyin(arg, &inresvs,
24953 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24954 			return (EFAULT);
24955 		}
24956 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24957 		    &inresvs, flag)) != 0) {
24958 			return (rval);
24959 		}
24960 		if (ddi_copyout(&inresvs, arg,
24961 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24962 			return (EFAULT);
24963 		}
24964 		break;
24965 	}
24966 
24967 #else /* ! _MULTI_DATAMODEL */
24968 
24969 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24970 		return (EFAULT);
24971 	}
24972 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24973 	if (rval != 0) {
24974 		return (rval);
24975 	}
24976 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24977 		return (EFAULT);
24978 	}
24979 
24980 #endif /* ! _MULTI_DATAMODEL */
24981 
24982 	return (rval);
24983 }
24984 
24985 
24986 /*
24987  * The following routines support the clustering functionality described below
24988  * and implement lost reservation reclaim functionality.
24989  *
24990  * Clustering
24991  * ----------
24992  * The clustering code uses two different, independent forms of SCSI
24993  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24994  * Persistent Group Reservations. For any particular disk, it will use either
24995  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24996  *
24997  * SCSI-2
24998  * The cluster software takes ownership of a multi-hosted disk by issuing the
24999  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
25000  * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster,
25001  * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues
25002  * the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the driver. The
25003  * meaning of failfast is that if the driver (on this host) ever encounters the
25004  * scsi error return code RESERVATION_CONFLICT from the device, it should
25005  * immediately panic the host. The motivation for this ioctl is that if this
25006  * host does encounter reservation conflict, the underlying cause is that some
25007  * other host of the cluster has decided that this host is no longer in the
25008  * cluster and has seized control of the disks for itself. Since this host is no
25009  * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl
25010  * does two things:
25011  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
25012  *      error to panic the host
25013  *      (b) it sets up a periodic timer to test whether this host still has
25014  *      "access" (in that no other host has reserved the device):  if the
25015  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
25016  *      purpose of that periodic timer is to handle scenarios where the host is
25017  *      otherwise temporarily quiescent, temporarily doing no real i/o.
25018  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
25019  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
25020  * the device itself.
25021  *
25022  * SCSI-3 PGR
25023  * A direct semantic implementation of the SCSI-3 Persistent Reservation
25024  * facility is supported through the shared multihost disk ioctls
25025  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
25026  * MHIOCGRP_PREEMPTANDABORT)
25027  *
25028  * Reservation Reclaim:
25029  * --------------------
25030  * To support the lost reservation reclaim operations this driver creates a
25031  * single thread to handle reinstating reservations on all devices that have
25032  * lost reservations sd_resv_reclaim_requests are logged for all devices that
25033  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
25034  * and the reservation reclaim thread loops through the requests to regain the
25035  * lost reservations.
25036  */
25037 
25038 /*
25039  *    Function: sd_check_mhd()
25040  *
25041  * Description: This function sets up and submits a scsi watch request or
25042  *		terminates an existing watch request. This routine is used in
25043  *		support of reservation reclaim.
25044  *
25045  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
25046  *			 among multiple watches that share the callback function
25047  *		interval - the number of microseconds specifying the watch
25048  *			   interval for issuing TEST UNIT READY commands. If
25049  *			   set to 0 the watch should be terminated. If the
25050  *			   interval is set to 0 and if the device is required
25051  *			   to hold reservation while disabling failfast, the
25052  *			   watch is restarted with an interval of
25053  *			   reinstate_resv_delay.
25054  *
25055  * Return Code: 0	   - Successful submit/terminate of scsi watch request
25056  *		ENXIO      - Indicates an invalid device was specified
25057  *		EAGAIN     - Unable to submit the scsi watch request
25058  */
25059 
25060 static int
25061 sd_check_mhd(dev_t dev, int interval)
25062 {
25063 	struct sd_lun	*un;
25064 	opaque_t	token;
25065 
25066 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25067 		return (ENXIO);
25068 	}
25069 
25070 	/* is this a watch termination request? */
25071 	if (interval == 0) {
25072 		mutex_enter(SD_MUTEX(un));
25073 		/* if there is an existing watch task then terminate it */
25074 		if (un->un_mhd_token) {
25075 			token = un->un_mhd_token;
25076 			un->un_mhd_token = NULL;
25077 			mutex_exit(SD_MUTEX(un));
25078 			(void) scsi_watch_request_terminate(token,
25079 			    SCSI_WATCH_TERMINATE_WAIT);
25080 			mutex_enter(SD_MUTEX(un));
25081 		} else {
25082 			mutex_exit(SD_MUTEX(un));
25083 			/*
25084 			 * Note: If we return here we don't check for the
25085 			 * failfast case. This is the original legacy
25086 			 * implementation but perhaps we should be checking
25087 			 * the failfast case.
25088 			 */
25089 			return (0);
25090 		}
25091 		/*
25092 		 * If the device is required to hold reservation while
25093 		 * disabling failfast, we need to restart the scsi_watch
25094 		 * routine with an interval of reinstate_resv_delay.
25095 		 */
25096 		if (un->un_resvd_status & SD_RESERVE) {
25097 			interval = sd_reinstate_resv_delay/1000;
25098 		} else {
25099 			/* no failfast so bail */
25100 			mutex_exit(SD_MUTEX(un));
25101 			return (0);
25102 		}
25103 		mutex_exit(SD_MUTEX(un));
25104 	}
25105 
25106 	/*
25107 	 * adjust minimum time interval to 1 second,
25108 	 * and convert from msecs to usecs
25109 	 */
25110 	if (interval > 0 && interval < 1000) {
25111 		interval = 1000;
25112 	}
25113 	interval *= 1000;
25114 
25115 	/*
25116 	 * submit the request to the scsi_watch service
25117 	 */
25118 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
25119 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
25120 	if (token == NULL) {
25121 		return (EAGAIN);
25122 	}
25123 
25124 	/*
25125 	 * save token for termination later on
25126 	 */
25127 	mutex_enter(SD_MUTEX(un));
25128 	un->un_mhd_token = token;
25129 	mutex_exit(SD_MUTEX(un));
25130 	return (0);
25131 }
25132 
25133 
25134 /*
25135  *    Function: sd_mhd_watch_cb()
25136  *
25137  * Description: This function is the call back function used by the scsi watch
25138  *		facility. The scsi watch facility sends the "Test Unit Ready"
25139  *		and processes the status. If applicable (i.e. a "Unit Attention"
25140  *		status and automatic "Request Sense" not used) the scsi watch
25141  *		facility will send a "Request Sense" and retrieve the sense data
25142  *		to be passed to this callback function. In either case the
25143  *		automatic "Request Sense" or the facility submitting one, this
25144  *		callback is passed the status and sense data.
25145  *
25146  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25147  *			among multiple watches that share this callback function
25148  *		resultp - scsi watch facility result packet containing scsi
25149  *			  packet, status byte and sense data
25150  *
25151  * Return Code: 0 - continue the watch task
25152  *		non-zero - terminate the watch task
25153  */
25154 
25155 static int
25156 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
25157 {
25158 	struct sd_lun			*un;
25159 	struct scsi_status		*statusp;
25160 	struct scsi_extended_sense	*sensep;
25161 	struct scsi_pkt			*pkt;
25162 	uchar_t				actual_sense_length;
25163 	dev_t  				dev = (dev_t)arg;
25164 
25165 	ASSERT(resultp != NULL);
25166 	statusp			= resultp->statusp;
25167 	sensep			= resultp->sensep;
25168 	pkt			= resultp->pkt;
25169 	actual_sense_length	= resultp->actual_sense_length;
25170 
25171 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25172 		return (ENXIO);
25173 	}
25174 
25175 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25176 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
25177 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
25178 
25179 	/* Begin processing of the status and/or sense data */
25180 	if (pkt->pkt_reason != CMD_CMPLT) {
25181 		/* Handle the incomplete packet */
25182 		sd_mhd_watch_incomplete(un, pkt);
25183 		return (0);
25184 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
25185 		if (*((unsigned char *)statusp)
25186 		    == STATUS_RESERVATION_CONFLICT) {
25187 			/*
25188 			 * Handle a reservation conflict by panicking if
25189 			 * configured for failfast or by logging the conflict
25190 			 * and updating the reservation status
25191 			 */
25192 			mutex_enter(SD_MUTEX(un));
25193 			if ((un->un_resvd_status & SD_FAILFAST) &&
25194 			    (sd_failfast_enable)) {
25195 				sd_panic_for_res_conflict(un);
25196 				/*NOTREACHED*/
25197 			}
25198 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25199 			    "sd_mhd_watch_cb: Reservation Conflict\n");
25200 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
25201 			mutex_exit(SD_MUTEX(un));
25202 		}
25203 	}
25204 
25205 	if (sensep != NULL) {
25206 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
25207 			mutex_enter(SD_MUTEX(un));
25208 			if ((sensep->es_add_code == SD_SCSI_RESET_SENSE_CODE) &&
25209 			    (un->un_resvd_status & SD_RESERVE)) {
25210 				/*
25211 				 * The additional sense code indicates a power
25212 				 * on or bus device reset has occurred; update
25213 				 * the reservation status.
25214 				 */
25215 				un->un_resvd_status |=
25216 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25217 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25218 				    "sd_mhd_watch_cb: Lost Reservation\n");
25219 			}
25220 		} else {
25221 			return (0);
25222 		}
25223 	} else {
25224 		mutex_enter(SD_MUTEX(un));
25225 	}
25226 
25227 	if ((un->un_resvd_status & SD_RESERVE) &&
25228 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
25229 		if (un->un_resvd_status & SD_WANT_RESERVE) {
25230 			/*
25231 			 * A reset occurred in between the last probe and this
25232 			 * one so if a timeout is pending cancel it.
25233 			 */
25234 			if (un->un_resvd_timeid) {
25235 				timeout_id_t temp_id = un->un_resvd_timeid;
25236 				un->un_resvd_timeid = NULL;
25237 				mutex_exit(SD_MUTEX(un));
25238 				(void) untimeout(temp_id);
25239 				mutex_enter(SD_MUTEX(un));
25240 			}
25241 			un->un_resvd_status &= ~SD_WANT_RESERVE;
25242 		}
25243 		if (un->un_resvd_timeid == 0) {
25244 			/* Schedule a timeout to handle the lost reservation */
25245 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
25246 			    (void *)dev,
25247 			    drv_usectohz(sd_reinstate_resv_delay));
25248 		}
25249 	}
25250 	mutex_exit(SD_MUTEX(un));
25251 	return (0);
25252 }
25253 
25254 
25255 /*
25256  *    Function: sd_mhd_watch_incomplete()
25257  *
25258  * Description: This function is used to find out why a scsi pkt sent by the
25259  *		scsi watch facility was not completed. Under some scenarios this
25260  *		routine will return. Otherwise it will send a bus reset to see
25261  *		if the drive is still online.
25262  *
25263  *   Arguments: un  - driver soft state (unit) structure
25264  *		pkt - incomplete scsi pkt
25265  */
25266 
25267 static void
25268 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
25269 {
25270 	int	be_chatty;
25271 	int	perr;
25272 
25273 	ASSERT(pkt != NULL);
25274 	ASSERT(un != NULL);
25275 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
25276 	perr		= (pkt->pkt_statistics & STAT_PERR);
25277 
25278 	mutex_enter(SD_MUTEX(un));
25279 	if (un->un_state == SD_STATE_DUMPING) {
25280 		mutex_exit(SD_MUTEX(un));
25281 		return;
25282 	}
25283 
25284 	switch (pkt->pkt_reason) {
25285 	case CMD_UNX_BUS_FREE:
25286 		/*
25287 		 * If we had a parity error that caused the target to drop BSY*,
25288 		 * don't be chatty about it.
25289 		 */
25290 		if (perr && be_chatty) {
25291 			be_chatty = 0;
25292 		}
25293 		break;
25294 	case CMD_TAG_REJECT:
25295 		/*
25296 		 * The SCSI-2 spec states that a tag reject will be sent by the
25297 		 * target if tagged queuing is not supported. A tag reject may
25298 		 * also be sent during certain initialization periods or to
25299 		 * control internal resources. For the latter case the target
25300 		 * may also return Queue Full.
25301 		 *
25302 		 * If this driver receives a tag reject from a target that is
25303 		 * going through an init period or controlling internal
25304 		 * resources tagged queuing will be disabled. This is a less
25305 		 * than optimal behavior but the driver is unable to determine
25306 		 * the target state and assumes tagged queueing is not supported
25307 		 */
25308 		pkt->pkt_flags = 0;
25309 		un->un_tagflags = 0;
25310 
25311 		if (un->un_f_opt_queueing == TRUE) {
25312 			un->un_throttle = min(un->un_throttle, 3);
25313 		} else {
25314 			un->un_throttle = 1;
25315 		}
25316 		mutex_exit(SD_MUTEX(un));
25317 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
25318 		mutex_enter(SD_MUTEX(un));
25319 		break;
25320 	case CMD_INCOMPLETE:
25321 		/*
25322 		 * The transport stopped with an abnormal state, fallthrough and
25323 		 * reset the target and/or bus unless selection did not complete
25324 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
25325 		 * go through a target/bus reset
25326 		 */
25327 		if (pkt->pkt_state == STATE_GOT_BUS) {
25328 			break;
25329 		}
25330 		/*FALLTHROUGH*/
25331 
25332 	case CMD_TIMEOUT:
25333 	default:
25334 		/*
25335 		 * The lun may still be running the command, so a lun reset
25336 		 * should be attempted. If the lun reset fails or cannot be
25337 		 * issued, than try a target reset. Lastly try a bus reset.
25338 		 */
25339 		if ((pkt->pkt_statistics &
25340 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
25341 			int reset_retval = 0;
25342 			mutex_exit(SD_MUTEX(un));
25343 			if (un->un_f_allow_bus_device_reset == TRUE) {
25344 				if (un->un_f_lun_reset_enabled == TRUE) {
25345 					reset_retval =
25346 					    scsi_reset(SD_ADDRESS(un),
25347 					    RESET_LUN);
25348 				}
25349 				if (reset_retval == 0) {
25350 					reset_retval =
25351 					    scsi_reset(SD_ADDRESS(un),
25352 					    RESET_TARGET);
25353 				}
25354 			}
25355 			if (reset_retval == 0) {
25356 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25357 			}
25358 			mutex_enter(SD_MUTEX(un));
25359 		}
25360 		break;
25361 	}
25362 
25363 	/* A device/bus reset has occurred; update the reservation status. */
25364 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
25365 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
25366 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25367 			un->un_resvd_status |=
25368 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
25369 			SD_INFO(SD_LOG_IOCTL_MHD, un,
25370 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
25371 		}
25372 	}
25373 
25374 	/*
25375 	 * The disk has been turned off; Update the device state.
25376 	 *
25377 	 * Note: Should we be offlining the disk here?
25378 	 */
25379 	if (pkt->pkt_state == STATE_GOT_BUS) {
25380 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
25381 		    "Disk not responding to selection\n");
25382 		if (un->un_state != SD_STATE_OFFLINE) {
25383 			New_state(un, SD_STATE_OFFLINE);
25384 		}
25385 	} else if (be_chatty) {
25386 		/*
25387 		 * suppress messages if they are all the same pkt reason;
25388 		 * with TQ, many (up to 256) are returned with the same
25389 		 * pkt_reason
25390 		 */
25391 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
25392 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
25393 			    "sd_mhd_watch_incomplete: "
25394 			    "SCSI transport failed: reason '%s'\n",
25395 			    scsi_rname(pkt->pkt_reason));
25396 		}
25397 	}
25398 	un->un_last_pkt_reason = pkt->pkt_reason;
25399 	mutex_exit(SD_MUTEX(un));
25400 }
25401 
25402 
25403 /*
25404  *    Function: sd_sname()
25405  *
25406  * Description: This is a simple little routine to return a string containing
25407  *		a printable description of command status byte for use in
25408  *		logging.
25409  *
25410  *   Arguments: status - pointer to a status byte
25411  *
25412  * Return Code: char * - string containing status description.
25413  */
25414 
25415 static char *
25416 sd_sname(uchar_t status)
25417 {
25418 	switch (status & STATUS_MASK) {
25419 	case STATUS_GOOD:
25420 		return ("good status");
25421 	case STATUS_CHECK:
25422 		return ("check condition");
25423 	case STATUS_MET:
25424 		return ("condition met");
25425 	case STATUS_BUSY:
25426 		return ("busy");
25427 	case STATUS_INTERMEDIATE:
25428 		return ("intermediate");
25429 	case STATUS_INTERMEDIATE_MET:
25430 		return ("intermediate - condition met");
25431 	case STATUS_RESERVATION_CONFLICT:
25432 		return ("reservation_conflict");
25433 	case STATUS_TERMINATED:
25434 		return ("command terminated");
25435 	case STATUS_QFULL:
25436 		return ("queue full");
25437 	default:
25438 		return ("<unknown status>");
25439 	}
25440 }
25441 
25442 
25443 /*
25444  *    Function: sd_mhd_resvd_recover()
25445  *
25446  * Description: This function adds a reservation entry to the
25447  *		sd_resv_reclaim_request list and signals the reservation
25448  *		reclaim thread that there is work pending. If the reservation
25449  *		reclaim thread has not been previously created this function
25450  *		will kick it off.
25451  *
25452  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
25453  *			among multiple watches that share this callback function
25454  *
25455  *     Context: This routine is called by timeout() and is run in interrupt
25456  *		context. It must not sleep or call other functions which may
25457  *		sleep.
25458  */
25459 
25460 static void
25461 sd_mhd_resvd_recover(void *arg)
25462 {
25463 	dev_t			dev = (dev_t)arg;
25464 	struct sd_lun		*un;
25465 	struct sd_thr_request	*sd_treq = NULL;
25466 	struct sd_thr_request	*sd_cur = NULL;
25467 	struct sd_thr_request	*sd_prev = NULL;
25468 	int			already_there = 0;
25469 
25470 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25471 		return;
25472 	}
25473 
25474 	mutex_enter(SD_MUTEX(un));
25475 	un->un_resvd_timeid = NULL;
25476 	if (un->un_resvd_status & SD_WANT_RESERVE) {
25477 		/*
25478 		 * There was a reset so don't issue the reserve, allow the
25479 		 * sd_mhd_watch_cb callback function to notice this and
25480 		 * reschedule the timeout for reservation.
25481 		 */
25482 		mutex_exit(SD_MUTEX(un));
25483 		return;
25484 	}
25485 	mutex_exit(SD_MUTEX(un));
25486 
25487 	/*
25488 	 * Add this device to the sd_resv_reclaim_request list and the
25489 	 * sd_resv_reclaim_thread should take care of the rest.
25490 	 *
25491 	 * Note: We can't sleep in this context so if the memory allocation
25492 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
25493 	 * reschedule the timeout for reservation.  (4378460)
25494 	 */
25495 	sd_treq = (struct sd_thr_request *)
25496 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
25497 	if (sd_treq == NULL) {
25498 		return;
25499 	}
25500 
25501 	sd_treq->sd_thr_req_next = NULL;
25502 	sd_treq->dev = dev;
25503 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25504 	if (sd_tr.srq_thr_req_head == NULL) {
25505 		sd_tr.srq_thr_req_head = sd_treq;
25506 	} else {
25507 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
25508 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
25509 			if (sd_cur->dev == dev) {
25510 				/*
25511 				 * already in Queue so don't log
25512 				 * another request for the device
25513 				 */
25514 				already_there = 1;
25515 				break;
25516 			}
25517 			sd_prev = sd_cur;
25518 		}
25519 		if (!already_there) {
25520 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
25521 			    "logging request for %lx\n", dev);
25522 			sd_prev->sd_thr_req_next = sd_treq;
25523 		} else {
25524 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
25525 		}
25526 	}
25527 
25528 	/*
25529 	 * Create a kernel thread to do the reservation reclaim and free up this
25530 	 * thread. We cannot block this thread while we go away to do the
25531 	 * reservation reclaim
25532 	 */
25533 	if (sd_tr.srq_resv_reclaim_thread == NULL)
25534 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
25535 		    sd_resv_reclaim_thread, NULL,
25536 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
25537 
25538 	/* Tell the reservation reclaim thread that it has work to do */
25539 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
25540 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25541 }
25542 
25543 /*
25544  *    Function: sd_resv_reclaim_thread()
25545  *
25546  * Description: This function implements the reservation reclaim operations
25547  *
25548  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
25549  *		      among multiple watches that share this callback function
25550  */
25551 
25552 static void
25553 sd_resv_reclaim_thread()
25554 {
25555 	struct sd_lun		*un;
25556 	struct sd_thr_request	*sd_mhreq;
25557 
25558 	/* Wait for work */
25559 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25560 	if (sd_tr.srq_thr_req_head == NULL) {
25561 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
25562 		    &sd_tr.srq_resv_reclaim_mutex);
25563 	}
25564 
25565 	/* Loop while we have work */
25566 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
25567 		un = ddi_get_soft_state(sd_state,
25568 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
25569 		if (un == NULL) {
25570 			/*
25571 			 * softstate structure is NULL so just
25572 			 * dequeue the request and continue
25573 			 */
25574 			sd_tr.srq_thr_req_head =
25575 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25576 			kmem_free(sd_tr.srq_thr_cur_req,
25577 			    sizeof (struct sd_thr_request));
25578 			continue;
25579 		}
25580 
25581 		/* dequeue the request */
25582 		sd_mhreq = sd_tr.srq_thr_cur_req;
25583 		sd_tr.srq_thr_req_head =
25584 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
25585 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25586 
25587 		/*
25588 		 * Reclaim reservation only if SD_RESERVE is still set. There
25589 		 * may have been a call to MHIOCRELEASE before we got here.
25590 		 */
25591 		mutex_enter(SD_MUTEX(un));
25592 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25593 			/*
25594 			 * Note: The SD_LOST_RESERVE flag is cleared before
25595 			 * reclaiming the reservation. If this is done after the
25596 			 * call to sd_reserve_release a reservation loss in the
25597 			 * window between pkt completion of reserve cmd and
25598 			 * mutex_enter below may not be recognized
25599 			 */
25600 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25601 			mutex_exit(SD_MUTEX(un));
25602 
25603 			if (sd_reserve_release(sd_mhreq->dev,
25604 			    SD_RESERVE) == 0) {
25605 				mutex_enter(SD_MUTEX(un));
25606 				un->un_resvd_status |= SD_RESERVE;
25607 				mutex_exit(SD_MUTEX(un));
25608 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25609 				    "sd_resv_reclaim_thread: "
25610 				    "Reservation Recovered\n");
25611 			} else {
25612 				mutex_enter(SD_MUTEX(un));
25613 				un->un_resvd_status |= SD_LOST_RESERVE;
25614 				mutex_exit(SD_MUTEX(un));
25615 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25616 				    "sd_resv_reclaim_thread: Failed "
25617 				    "Reservation Recovery\n");
25618 			}
25619 		} else {
25620 			mutex_exit(SD_MUTEX(un));
25621 		}
25622 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25623 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25624 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25625 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25626 		/*
25627 		 * wakeup the destroy thread if anyone is waiting on
25628 		 * us to complete.
25629 		 */
25630 		cv_signal(&sd_tr.srq_inprocess_cv);
25631 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25632 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25633 	}
25634 
25635 	/*
25636 	 * cleanup the sd_tr structure now that this thread will not exist
25637 	 */
25638 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25639 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25640 	sd_tr.srq_resv_reclaim_thread = NULL;
25641 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25642 	thread_exit();
25643 }
25644 
25645 
25646 /*
25647  *    Function: sd_rmv_resv_reclaim_req()
25648  *
25649  * Description: This function removes any pending reservation reclaim requests
25650  *		for the specified device.
25651  *
25652  *   Arguments: dev - the device 'dev_t'
25653  */
25654 
25655 static void
25656 sd_rmv_resv_reclaim_req(dev_t dev)
25657 {
25658 	struct sd_thr_request *sd_mhreq;
25659 	struct sd_thr_request *sd_prev;
25660 
25661 	/* Remove a reservation reclaim request from the list */
25662 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25663 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25664 		/*
25665 		 * We are attempting to reinstate reservation for
25666 		 * this device. We wait for sd_reserve_release()
25667 		 * to return before we return.
25668 		 */
25669 		cv_wait(&sd_tr.srq_inprocess_cv,
25670 		    &sd_tr.srq_resv_reclaim_mutex);
25671 	} else {
25672 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25673 		if (sd_mhreq && sd_mhreq->dev == dev) {
25674 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25675 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25676 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25677 			return;
25678 		}
25679 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25680 			if (sd_mhreq && sd_mhreq->dev == dev) {
25681 				break;
25682 			}
25683 			sd_prev = sd_mhreq;
25684 		}
25685 		if (sd_mhreq != NULL) {
25686 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25687 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25688 		}
25689 	}
25690 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25691 }
25692 
25693 
25694 /*
25695  *    Function: sd_mhd_reset_notify_cb()
25696  *
25697  * Description: This is a call back function for scsi_reset_notify. This
25698  *		function updates the softstate reserved status and logs the
25699  *		reset. The driver scsi watch facility callback function
25700  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25701  *		will reclaim the reservation.
25702  *
25703  *   Arguments: arg  - driver soft state (unit) structure
25704  */
25705 
25706 static void
25707 sd_mhd_reset_notify_cb(caddr_t arg)
25708 {
25709 	struct sd_lun *un = (struct sd_lun *)arg;
25710 
25711 	mutex_enter(SD_MUTEX(un));
25712 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25713 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25714 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25715 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25716 	}
25717 	mutex_exit(SD_MUTEX(un));
25718 }
25719 
25720 
25721 /*
25722  *    Function: sd_take_ownership()
25723  *
25724  * Description: This routine implements an algorithm to achieve a stable
25725  *		reservation on disks which don't implement priority reserve,
25726  *		and makes sure that other host lose re-reservation attempts.
25727  *		This algorithm contains of a loop that keeps issuing the RESERVE
25728  *		for some period of time (min_ownership_delay, default 6 seconds)
25729  *		During that loop, it looks to see if there has been a bus device
25730  *		reset or bus reset (both of which cause an existing reservation
25731  *		to be lost). If the reservation is lost issue RESERVE until a
25732  *		period of min_ownership_delay with no resets has gone by, or
25733  *		until max_ownership_delay has expired. This loop ensures that
25734  *		the host really did manage to reserve the device, in spite of
25735  *		resets. The looping for min_ownership_delay (default six
25736  *		seconds) is important to early generation clustering products,
25737  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25738  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25739  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25740  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25741  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25742  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25743  *		no longer "owns" the disk and will have panicked itself.  Thus,
25744  *		the host issuing the MHIOCTKOWN is assured (with timing
25745  *		dependencies) that by the time it actually starts to use the
25746  *		disk for real work, the old owner is no longer accessing it.
25747  *
25748  *		min_ownership_delay is the minimum amount of time for which the
25749  *		disk must be reserved continuously devoid of resets before the
25750  *		MHIOCTKOWN ioctl will return success.
25751  *
25752  *		max_ownership_delay indicates the amount of time by which the
25753  *		take ownership should succeed or timeout with an error.
25754  *
25755  *   Arguments: dev - the device 'dev_t'
25756  *		*p  - struct containing timing info.
25757  *
25758  * Return Code: 0 for success or error code
25759  */
25760 
25761 static int
25762 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25763 {
25764 	struct sd_lun	*un;
25765 	int		rval;
25766 	int		err;
25767 	int		reservation_count   = 0;
25768 	int		min_ownership_delay =  6000000; /* in usec */
25769 	int		max_ownership_delay = 30000000; /* in usec */
25770 	clock_t		start_time;	/* starting time of this algorithm */
25771 	clock_t		end_time;	/* time limit for giving up */
25772 	clock_t		ownership_time;	/* time limit for stable ownership */
25773 	clock_t		current_time;
25774 	clock_t		previous_current_time;
25775 
25776 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25777 		return (ENXIO);
25778 	}
25779 
25780 	/*
25781 	 * Attempt a device reservation. A priority reservation is requested.
25782 	 */
25783 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25784 	    != SD_SUCCESS) {
25785 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25786 		    "sd_take_ownership: return(1)=%d\n", rval);
25787 		return (rval);
25788 	}
25789 
25790 	/* Update the softstate reserved status to indicate the reservation */
25791 	mutex_enter(SD_MUTEX(un));
25792 	un->un_resvd_status |= SD_RESERVE;
25793 	un->un_resvd_status &=
25794 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25795 	mutex_exit(SD_MUTEX(un));
25796 
25797 	if (p != NULL) {
25798 		if (p->min_ownership_delay != 0) {
25799 			min_ownership_delay = p->min_ownership_delay * 1000;
25800 		}
25801 		if (p->max_ownership_delay != 0) {
25802 			max_ownership_delay = p->max_ownership_delay * 1000;
25803 		}
25804 	}
25805 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25806 	    "sd_take_ownership: min, max delays: %d, %d\n",
25807 	    min_ownership_delay, max_ownership_delay);
25808 
25809 	start_time = ddi_get_lbolt();
25810 	current_time	= start_time;
25811 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25812 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25813 
25814 	while (current_time - end_time < 0) {
25815 		delay(drv_usectohz(500000));
25816 
25817 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25818 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25819 				mutex_enter(SD_MUTEX(un));
25820 				rval = (un->un_resvd_status &
25821 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25822 				mutex_exit(SD_MUTEX(un));
25823 				break;
25824 			}
25825 		}
25826 		previous_current_time = current_time;
25827 		current_time = ddi_get_lbolt();
25828 		mutex_enter(SD_MUTEX(un));
25829 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25830 			ownership_time = ddi_get_lbolt() +
25831 			    drv_usectohz(min_ownership_delay);
25832 			reservation_count = 0;
25833 		} else {
25834 			reservation_count++;
25835 		}
25836 		un->un_resvd_status |= SD_RESERVE;
25837 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25838 		mutex_exit(SD_MUTEX(un));
25839 
25840 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25841 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25842 		    "reservation=%s\n", (current_time - previous_current_time),
25843 		    reservation_count ? "ok" : "reclaimed");
25844 
25845 		if (current_time - ownership_time >= 0 &&
25846 		    reservation_count >= 4) {
25847 			rval = 0; /* Achieved a stable ownership */
25848 			break;
25849 		}
25850 		if (current_time - end_time >= 0) {
25851 			rval = EACCES; /* No ownership in max possible time */
25852 			break;
25853 		}
25854 	}
25855 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25856 	    "sd_take_ownership: return(2)=%d\n", rval);
25857 	return (rval);
25858 }
25859 
25860 
25861 /*
25862  *    Function: sd_reserve_release()
25863  *
25864  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25865  *		PRIORITY RESERVE commands based on a user specified command type
25866  *
25867  *   Arguments: dev - the device 'dev_t'
25868  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25869  *		      SD_RESERVE, SD_RELEASE
25870  *
25871  * Return Code: 0 or Error Code
25872  */
25873 
25874 static int
25875 sd_reserve_release(dev_t dev, int cmd)
25876 {
25877 	struct uscsi_cmd	*com = NULL;
25878 	struct sd_lun		*un = NULL;
25879 	char			cdb[CDB_GROUP0];
25880 	int			rval;
25881 
25882 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25883 	    (cmd == SD_PRIORITY_RESERVE));
25884 
25885 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25886 		return (ENXIO);
25887 	}
25888 
25889 	/* instantiate and initialize the command and cdb */
25890 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25891 	bzero(cdb, CDB_GROUP0);
25892 	com->uscsi_flags   = USCSI_SILENT;
25893 	com->uscsi_timeout = un->un_reserve_release_time;
25894 	com->uscsi_cdblen  = CDB_GROUP0;
25895 	com->uscsi_cdb	   = cdb;
25896 	if (cmd == SD_RELEASE) {
25897 		cdb[0] = SCMD_RELEASE;
25898 	} else {
25899 		cdb[0] = SCMD_RESERVE;
25900 	}
25901 
25902 	/* Send the command. */
25903 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
25904 	    UIO_SYSSPACE, SD_PATH_STANDARD);
25905 
25906 	/*
25907 	 * "break" a reservation that is held by another host, by issuing a
25908 	 * reset if priority reserve is desired, and we could not get the
25909 	 * device.
25910 	 */
25911 	if ((cmd == SD_PRIORITY_RESERVE) &&
25912 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25913 		/*
25914 		 * First try to reset the LUN. If we cannot, then try a target
25915 		 * reset, followed by a bus reset if the target reset fails.
25916 		 */
25917 		int reset_retval = 0;
25918 		if (un->un_f_lun_reset_enabled == TRUE) {
25919 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25920 		}
25921 		if (reset_retval == 0) {
25922 			/* The LUN reset either failed or was not issued */
25923 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25924 		}
25925 		if ((reset_retval == 0) &&
25926 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25927 			rval = EIO;
25928 			kmem_free(com, sizeof (*com));
25929 			return (rval);
25930 		}
25931 
25932 		bzero(com, sizeof (struct uscsi_cmd));
25933 		com->uscsi_flags   = USCSI_SILENT;
25934 		com->uscsi_cdb	   = cdb;
25935 		com->uscsi_cdblen  = CDB_GROUP0;
25936 		com->uscsi_timeout = 5;
25937 
25938 		/*
25939 		 * Reissue the last reserve command, this time without request
25940 		 * sense.  Assume that it is just a regular reserve command.
25941 		 */
25942 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
25943 		    UIO_SYSSPACE, SD_PATH_STANDARD);
25944 	}
25945 
25946 	/* Return an error if still getting a reservation conflict. */
25947 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25948 		rval = EACCES;
25949 	}
25950 
25951 	kmem_free(com, sizeof (*com));
25952 	return (rval);
25953 }
25954 
25955 
25956 #define	SD_NDUMP_RETRIES	12
25957 /*
25958  *	System Crash Dump routine
25959  */
25960 
25961 static int
25962 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25963 {
25964 	int		instance;
25965 	int		partition;
25966 	int		i;
25967 	int		err;
25968 	struct sd_lun	*un;
25969 	struct dk_map	*lp;
25970 	struct scsi_pkt *wr_pktp;
25971 	struct buf	*wr_bp;
25972 	struct buf	wr_buf;
25973 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
25974 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
25975 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
25976 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
25977 	size_t		io_start_offset;
25978 	int		doing_rmw = FALSE;
25979 	int		rval;
25980 #if defined(__i386) || defined(__amd64)
25981 	ssize_t dma_resid;
25982 	daddr_t oblkno;
25983 #endif
25984 
25985 	instance = SDUNIT(dev);
25986 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25987 	    (!un->un_f_geometry_is_valid) || ISCD(un)) {
25988 		return (ENXIO);
25989 	}
25990 
25991 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25992 
25993 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25994 
25995 	partition = SDPART(dev);
25996 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25997 
25998 	/* Validate blocks to dump at against partition size. */
25999 	lp = &un->un_map[partition];
26000 	if ((blkno + nblk) > lp->dkl_nblk) {
26001 		SD_TRACE(SD_LOG_DUMP, un,
26002 		    "sddump: dump range larger than partition: "
26003 		    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
26004 		    blkno, nblk, lp->dkl_nblk);
26005 		return (EINVAL);
26006 	}
26007 
26008 	mutex_enter(&un->un_pm_mutex);
26009 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
26010 		struct scsi_pkt *start_pktp;
26011 
26012 		mutex_exit(&un->un_pm_mutex);
26013 
26014 		/*
26015 		 * use pm framework to power on HBA 1st
26016 		 */
26017 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
26018 
26019 		/*
26020 		 * Dump no long uses sdpower to power on a device, it's
26021 		 * in-line here so it can be done in polled mode.
26022 		 */
26023 
26024 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
26025 
26026 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
26027 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
26028 
26029 		if (start_pktp == NULL) {
26030 			/* We were not given a SCSI packet, fail. */
26031 			return (EIO);
26032 		}
26033 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
26034 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
26035 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
26036 		start_pktp->pkt_flags = FLAG_NOINTR;
26037 
26038 		mutex_enter(SD_MUTEX(un));
26039 		SD_FILL_SCSI1_LUN(un, start_pktp);
26040 		mutex_exit(SD_MUTEX(un));
26041 		/*
26042 		 * Scsi_poll returns 0 (success) if the command completes and
26043 		 * the status block is STATUS_GOOD.
26044 		 */
26045 		if (sd_scsi_poll(un, start_pktp) != 0) {
26046 			scsi_destroy_pkt(start_pktp);
26047 			return (EIO);
26048 		}
26049 		scsi_destroy_pkt(start_pktp);
26050 		(void) sd_ddi_pm_resume(un);
26051 	} else {
26052 		mutex_exit(&un->un_pm_mutex);
26053 	}
26054 
26055 	mutex_enter(SD_MUTEX(un));
26056 	un->un_throttle = 0;
26057 
26058 	/*
26059 	 * The first time through, reset the specific target device.
26060 	 * However, when cpr calls sddump we know that sd is in a
26061 	 * a good state so no bus reset is required.
26062 	 * Clear sense data via Request Sense cmd.
26063 	 * In sddump we don't care about allow_bus_device_reset anymore
26064 	 */
26065 
26066 	if ((un->un_state != SD_STATE_SUSPENDED) &&
26067 	    (un->un_state != SD_STATE_DUMPING)) {
26068 
26069 		New_state(un, SD_STATE_DUMPING);
26070 
26071 		if (un->un_f_is_fibre == FALSE) {
26072 			mutex_exit(SD_MUTEX(un));
26073 			/*
26074 			 * Attempt a bus reset for parallel scsi.
26075 			 *
26076 			 * Note: A bus reset is required because on some host
26077 			 * systems (i.e. E420R) a bus device reset is
26078 			 * insufficient to reset the state of the target.
26079 			 *
26080 			 * Note: Don't issue the reset for fibre-channel,
26081 			 * because this tends to hang the bus (loop) for
26082 			 * too long while everyone is logging out and in
26083 			 * and the deadman timer for dumping will fire
26084 			 * before the dump is complete.
26085 			 */
26086 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
26087 				mutex_enter(SD_MUTEX(un));
26088 				Restore_state(un);
26089 				mutex_exit(SD_MUTEX(un));
26090 				return (EIO);
26091 			}
26092 
26093 			/* Delay to give the device some recovery time. */
26094 			drv_usecwait(10000);
26095 
26096 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
26097 				SD_INFO(SD_LOG_DUMP, un,
26098 					"sddump: sd_send_polled_RQS failed\n");
26099 			}
26100 			mutex_enter(SD_MUTEX(un));
26101 		}
26102 	}
26103 
26104 	/*
26105 	 * Convert the partition-relative block number to a
26106 	 * disk physical block number.
26107 	 */
26108 	blkno += un->un_offset[partition];
26109 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
26110 
26111 
26112 	/*
26113 	 * Check if the device has a non-512 block size.
26114 	 */
26115 	wr_bp = NULL;
26116 	if (NOT_DEVBSIZE(un)) {
26117 		tgt_byte_offset = blkno * un->un_sys_blocksize;
26118 		tgt_byte_count = nblk * un->un_sys_blocksize;
26119 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
26120 		    (tgt_byte_count % un->un_tgt_blocksize)) {
26121 			doing_rmw = TRUE;
26122 			/*
26123 			 * Calculate the block number and number of block
26124 			 * in terms of the media block size.
26125 			 */
26126 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26127 			tgt_nblk =
26128 			    ((tgt_byte_offset + tgt_byte_count +
26129 				(un->un_tgt_blocksize - 1)) /
26130 				un->un_tgt_blocksize) - tgt_blkno;
26131 
26132 			/*
26133 			 * Invoke the routine which is going to do read part
26134 			 * of read-modify-write.
26135 			 * Note that this routine returns a pointer to
26136 			 * a valid bp in wr_bp.
26137 			 */
26138 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
26139 			    &wr_bp);
26140 			if (err) {
26141 				mutex_exit(SD_MUTEX(un));
26142 				return (err);
26143 			}
26144 			/*
26145 			 * Offset is being calculated as -
26146 			 * (original block # * system block size) -
26147 			 * (new block # * target block size)
26148 			 */
26149 			io_start_offset =
26150 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
26151 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
26152 
26153 			ASSERT((io_start_offset >= 0) &&
26154 			    (io_start_offset < un->un_tgt_blocksize));
26155 			/*
26156 			 * Do the modify portion of read modify write.
26157 			 */
26158 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
26159 			    (size_t)nblk * un->un_sys_blocksize);
26160 		} else {
26161 			doing_rmw = FALSE;
26162 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
26163 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
26164 		}
26165 
26166 		/* Convert blkno and nblk to target blocks */
26167 		blkno = tgt_blkno;
26168 		nblk = tgt_nblk;
26169 	} else {
26170 		wr_bp = &wr_buf;
26171 		bzero(wr_bp, sizeof (struct buf));
26172 		wr_bp->b_flags		= B_BUSY;
26173 		wr_bp->b_un.b_addr	= addr;
26174 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
26175 		wr_bp->b_resid		= 0;
26176 	}
26177 
26178 	mutex_exit(SD_MUTEX(un));
26179 
26180 	/*
26181 	 * Obtain a SCSI packet for the write command.
26182 	 * It should be safe to call the allocator here without
26183 	 * worrying about being locked for DVMA mapping because
26184 	 * the address we're passed is already a DVMA mapping
26185 	 *
26186 	 * We are also not going to worry about semaphore ownership
26187 	 * in the dump buffer. Dumping is single threaded at present.
26188 	 */
26189 
26190 	wr_pktp = NULL;
26191 
26192 #if defined(__i386) || defined(__amd64)
26193 	dma_resid = wr_bp->b_bcount;
26194 	oblkno = blkno;
26195 	while (dma_resid != 0) {
26196 #endif
26197 
26198 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26199 		wr_bp->b_flags &= ~B_ERROR;
26200 
26201 #if defined(__i386) || defined(__amd64)
26202 		blkno = oblkno +
26203 			((wr_bp->b_bcount - dma_resid) /
26204 			    un->un_tgt_blocksize);
26205 		nblk = dma_resid / un->un_tgt_blocksize;
26206 
26207 		if (wr_pktp) {
26208 			/* Partial DMA transfers after initial transfer */
26209 			rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
26210 			    blkno, nblk);
26211 		} else {
26212 			/* Initial transfer */
26213 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26214 			    un->un_pkt_flags, NULL_FUNC, NULL,
26215 			    blkno, nblk);
26216 		}
26217 #else
26218 		rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
26219 		    0, NULL_FUNC, NULL, blkno, nblk);
26220 #endif
26221 
26222 		if (rval == 0) {
26223 			/* We were given a SCSI packet, continue. */
26224 			break;
26225 		}
26226 
26227 		if (i == 0) {
26228 			if (wr_bp->b_flags & B_ERROR) {
26229 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26230 				    "no resources for dumping; "
26231 				    "error code: 0x%x, retrying",
26232 				    geterror(wr_bp));
26233 			} else {
26234 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26235 				    "no resources for dumping; retrying");
26236 			}
26237 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
26238 			if (wr_bp->b_flags & B_ERROR) {
26239 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26240 				    "no resources for dumping; error code: "
26241 				    "0x%x, retrying\n", geterror(wr_bp));
26242 			}
26243 		} else {
26244 			if (wr_bp->b_flags & B_ERROR) {
26245 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26246 				    "no resources for dumping; "
26247 				    "error code: 0x%x, retries failed, "
26248 				    "giving up.\n", geterror(wr_bp));
26249 			} else {
26250 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26251 				    "no resources for dumping; "
26252 				    "retries failed, giving up.\n");
26253 			}
26254 			mutex_enter(SD_MUTEX(un));
26255 			Restore_state(un);
26256 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
26257 				mutex_exit(SD_MUTEX(un));
26258 				scsi_free_consistent_buf(wr_bp);
26259 			} else {
26260 				mutex_exit(SD_MUTEX(un));
26261 			}
26262 			return (EIO);
26263 		}
26264 		drv_usecwait(10000);
26265 	}
26266 
26267 #if defined(__i386) || defined(__amd64)
26268 	/*
26269 	 * save the resid from PARTIAL_DMA
26270 	 */
26271 	dma_resid = wr_pktp->pkt_resid;
26272 	if (dma_resid != 0)
26273 		nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
26274 	wr_pktp->pkt_resid = 0;
26275 #endif
26276 
26277 	/* SunBug 1222170 */
26278 	wr_pktp->pkt_flags = FLAG_NOINTR;
26279 
26280 	err = EIO;
26281 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
26282 
26283 		/*
26284 		 * Scsi_poll returns 0 (success) if the command completes and
26285 		 * the status block is STATUS_GOOD.  We should only check
26286 		 * errors if this condition is not true.  Even then we should
26287 		 * send our own request sense packet only if we have a check
26288 		 * condition and auto request sense has not been performed by
26289 		 * the hba.
26290 		 */
26291 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
26292 
26293 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
26294 		    (wr_pktp->pkt_resid == 0)) {
26295 			err = SD_SUCCESS;
26296 			break;
26297 		}
26298 
26299 		/*
26300 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
26301 		 */
26302 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
26303 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
26304 			    "Device is gone\n");
26305 			break;
26306 		}
26307 
26308 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
26309 			SD_INFO(SD_LOG_DUMP, un,
26310 			    "sddump: write failed with CHECK, try # %d\n", i);
26311 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
26312 				(void) sd_send_polled_RQS(un);
26313 			}
26314 
26315 			continue;
26316 		}
26317 
26318 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
26319 			int reset_retval = 0;
26320 
26321 			SD_INFO(SD_LOG_DUMP, un,
26322 			    "sddump: write failed with BUSY, try # %d\n", i);
26323 
26324 			if (un->un_f_lun_reset_enabled == TRUE) {
26325 				reset_retval = scsi_reset(SD_ADDRESS(un),
26326 				    RESET_LUN);
26327 			}
26328 			if (reset_retval == 0) {
26329 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
26330 			}
26331 			(void) sd_send_polled_RQS(un);
26332 
26333 		} else {
26334 			SD_INFO(SD_LOG_DUMP, un,
26335 			    "sddump: write failed with 0x%x, try # %d\n",
26336 			    SD_GET_PKT_STATUS(wr_pktp), i);
26337 			mutex_enter(SD_MUTEX(un));
26338 			sd_reset_target(un, wr_pktp);
26339 			mutex_exit(SD_MUTEX(un));
26340 		}
26341 
26342 		/*
26343 		 * If we are not getting anywhere with lun/target resets,
26344 		 * let's reset the bus.
26345 		 */
26346 		if (i == SD_NDUMP_RETRIES/2) {
26347 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
26348 			(void) sd_send_polled_RQS(un);
26349 		}
26350 
26351 	}
26352 #if defined(__i386) || defined(__amd64)
26353 	}	/* dma_resid */
26354 #endif
26355 
26356 	scsi_destroy_pkt(wr_pktp);
26357 	mutex_enter(SD_MUTEX(un));
26358 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
26359 		mutex_exit(SD_MUTEX(un));
26360 		scsi_free_consistent_buf(wr_bp);
26361 	} else {
26362 		mutex_exit(SD_MUTEX(un));
26363 	}
26364 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
26365 	return (err);
26366 }
26367 
26368 /*
26369  *    Function: sd_scsi_poll()
26370  *
26371  * Description: This is a wrapper for the scsi_poll call.
26372  *
26373  *   Arguments: sd_lun - The unit structure
26374  *              scsi_pkt - The scsi packet being sent to the device.
26375  *
26376  * Return Code: 0 - Command completed successfully with good status
26377  *             -1 - Command failed.  This could indicate a check condition
26378  *                  or other status value requiring recovery action.
26379  *
26380  */
26381 
26382 static int
26383 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
26384 {
26385 	int status;
26386 
26387 	ASSERT(un != NULL);
26388 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26389 	ASSERT(pktp != NULL);
26390 
26391 	status = SD_SUCCESS;
26392 
26393 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
26394 		pktp->pkt_flags |= un->un_tagflags;
26395 		pktp->pkt_flags &= ~FLAG_NODISCON;
26396 	}
26397 
26398 	status = sd_ddi_scsi_poll(pktp);
26399 	/*
26400 	 * Scsi_poll returns 0 (success) if the command completes and the
26401 	 * status block is STATUS_GOOD.  We should only check errors if this
26402 	 * condition is not true.  Even then we should send our own request
26403 	 * sense packet only if we have a check condition and auto
26404 	 * request sense has not been performed by the hba.
26405 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
26406 	 */
26407 	if ((status != SD_SUCCESS) &&
26408 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
26409 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
26410 	    (pktp->pkt_reason != CMD_DEV_GONE))
26411 		(void) sd_send_polled_RQS(un);
26412 
26413 	return (status);
26414 }
26415 
26416 /*
26417  *    Function: sd_send_polled_RQS()
26418  *
26419  * Description: This sends the request sense command to a device.
26420  *
26421  *   Arguments: sd_lun - The unit structure
26422  *
26423  * Return Code: 0 - Command completed successfully with good status
26424  *             -1 - Command failed.
26425  *
26426  */
26427 
26428 static int
26429 sd_send_polled_RQS(struct sd_lun *un)
26430 {
26431 	int	ret_val;
26432 	struct	scsi_pkt	*rqs_pktp;
26433 	struct	buf		*rqs_bp;
26434 
26435 	ASSERT(un != NULL);
26436 	ASSERT(!mutex_owned(SD_MUTEX(un)));
26437 
26438 	ret_val = SD_SUCCESS;
26439 
26440 	rqs_pktp = un->un_rqs_pktp;
26441 	rqs_bp	 = un->un_rqs_bp;
26442 
26443 	mutex_enter(SD_MUTEX(un));
26444 
26445 	if (un->un_sense_isbusy) {
26446 		ret_val = SD_FAILURE;
26447 		mutex_exit(SD_MUTEX(un));
26448 		return (ret_val);
26449 	}
26450 
26451 	/*
26452 	 * If the request sense buffer (and packet) is not in use,
26453 	 * let's set the un_sense_isbusy and send our packet
26454 	 */
26455 	un->un_sense_isbusy 	= 1;
26456 	rqs_pktp->pkt_resid  	= 0;
26457 	rqs_pktp->pkt_reason 	= 0;
26458 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
26459 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
26460 
26461 	mutex_exit(SD_MUTEX(un));
26462 
26463 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
26464 	    " 0x%p\n", rqs_bp->b_un.b_addr);
26465 
26466 	/*
26467 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
26468 	 * axle - it has a call into us!
26469 	 */
26470 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
26471 		SD_INFO(SD_LOG_COMMON, un,
26472 		    "sd_send_polled_RQS: RQS failed\n");
26473 	}
26474 
26475 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
26476 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
26477 
26478 	mutex_enter(SD_MUTEX(un));
26479 	un->un_sense_isbusy = 0;
26480 	mutex_exit(SD_MUTEX(un));
26481 
26482 	return (ret_val);
26483 }
26484 
26485 /*
26486  * Defines needed for localized version of the scsi_poll routine.
26487  */
26488 #define	SD_CSEC		10000			/* usecs */
26489 #define	SD_SEC_TO_CSEC	(1000000/SD_CSEC)
26490 
26491 
26492 /*
26493  *    Function: sd_ddi_scsi_poll()
26494  *
26495  * Description: Localized version of the scsi_poll routine.  The purpose is to
26496  *		send a scsi_pkt to a device as a polled command.  This version
26497  *		is to ensure more robust handling of transport errors.
26498  *		Specifically this routine cures not ready, coming ready
26499  *		transition for power up and reset of sonoma's.  This can take
26500  *		up to 45 seconds for power-on and 20 seconds for reset of a
26501  * 		sonoma lun.
26502  *
26503  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
26504  *
26505  * Return Code: 0 - Command completed successfully with good status
26506  *             -1 - Command failed.
26507  *
26508  */
26509 
26510 static int
26511 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
26512 {
26513 	int busy_count;
26514 	int timeout;
26515 	int rval = SD_FAILURE;
26516 	int savef;
26517 	struct scsi_extended_sense *sensep;
26518 	long savet;
26519 	void (*savec)();
26520 	/*
26521 	 * The following is defined in machdep.c and is used in determining if
26522 	 * the scsi transport system will do polled I/O instead of interrupt
26523 	 * I/O when called from xx_dump().
26524 	 */
26525 	extern int do_polled_io;
26526 
26527 	/*
26528 	 * save old flags in pkt, to restore at end
26529 	 */
26530 	savef = pkt->pkt_flags;
26531 	savec = pkt->pkt_comp;
26532 	savet = pkt->pkt_time;
26533 
26534 	pkt->pkt_flags |= FLAG_NOINTR;
26535 
26536 	/*
26537 	 * XXX there is nothing in the SCSA spec that states that we should not
26538 	 * do a callback for polled cmds; however, removing this will break sd
26539 	 * and probably other target drivers
26540 	 */
26541 	pkt->pkt_comp = NULL;
26542 
26543 	/*
26544 	 * we don't like a polled command without timeout.
26545 	 * 60 seconds seems long enough.
26546 	 */
26547 	if (pkt->pkt_time == 0) {
26548 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
26549 	}
26550 
26551 	/*
26552 	 * Send polled cmd.
26553 	 *
26554 	 * We do some error recovery for various errors.  Tran_busy,
26555 	 * queue full, and non-dispatched commands are retried every 10 msec.
26556 	 * as they are typically transient failures.  Busy status and Not
26557 	 * Ready are retried every second as this status takes a while to
26558 	 * change.  Unit attention is retried for pkt_time (60) times
26559 	 * with no delay.
26560 	 */
26561 	timeout = pkt->pkt_time * SD_SEC_TO_CSEC;
26562 
26563 	for (busy_count = 0; busy_count < timeout; busy_count++) {
26564 		int rc;
26565 		int poll_delay;
26566 
26567 		/*
26568 		 * Initialize pkt status variables.
26569 		 */
26570 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
26571 
26572 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
26573 			if (rc != TRAN_BUSY) {
26574 				/* Transport failed - give up. */
26575 				break;
26576 			} else {
26577 				/* Transport busy - try again. */
26578 				poll_delay = 1 * SD_CSEC; /* 10 msec */
26579 			}
26580 		} else {
26581 			/*
26582 			 * Transport accepted - check pkt status.
26583 			 */
26584 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
26585 			if (pkt->pkt_reason == CMD_CMPLT &&
26586 			    rc == STATUS_CHECK &&
26587 			    pkt->pkt_state & STATE_ARQ_DONE) {
26588 				struct scsi_arq_status *arqstat =
26589 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26590 
26591 				sensep = &arqstat->sts_sensedata;
26592 			} else {
26593 				sensep = NULL;
26594 			}
26595 
26596 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26597 			    (rc == STATUS_GOOD)) {
26598 				/* No error - we're done */
26599 				rval = SD_SUCCESS;
26600 				break;
26601 
26602 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26603 				/* Lost connection - give up */
26604 				break;
26605 
26606 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26607 			    (pkt->pkt_state == 0)) {
26608 				/* Pkt not dispatched - try again. */
26609 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
26610 
26611 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26612 			    (rc == STATUS_QFULL)) {
26613 				/* Queue full - try again. */
26614 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
26615 
26616 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26617 			    (rc == STATUS_BUSY)) {
26618 				/* Busy - try again. */
26619 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
26620 				busy_count += (SD_SEC_TO_CSEC - 1);
26621 
26622 			} else if ((sensep != NULL) &&
26623 			    (sensep->es_key == KEY_UNIT_ATTENTION)) {
26624 				/* Unit Attention - try again */
26625 				busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */
26626 				continue;
26627 
26628 			} else if ((sensep != NULL) &&
26629 			    (sensep->es_key == KEY_NOT_READY) &&
26630 			    (sensep->es_add_code == 0x04) &&
26631 			    (sensep->es_qual_code == 0x01)) {
26632 				/* Not ready -> ready - try again. */
26633 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
26634 				busy_count += (SD_SEC_TO_CSEC - 1);
26635 
26636 			} else {
26637 				/* BAD status - give up. */
26638 				break;
26639 			}
26640 		}
26641 
26642 		if ((curthread->t_flag & T_INTR_THREAD) == 0 &&
26643 		    !do_polled_io) {
26644 			delay(drv_usectohz(poll_delay));
26645 		} else {
26646 			/* we busy wait during cpr_dump or interrupt threads */
26647 			drv_usecwait(poll_delay);
26648 		}
26649 	}
26650 
26651 	pkt->pkt_flags = savef;
26652 	pkt->pkt_comp = savec;
26653 	pkt->pkt_time = savet;
26654 	return (rval);
26655 }
26656 
26657 
26658 /*
26659  *    Function: sd_persistent_reservation_in_read_keys
26660  *
26661  * Description: This routine is the driver entry point for handling CD-ROM
26662  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26663  *		by sending the SCSI-3 PRIN commands to the device.
26664  *		Processes the read keys command response by copying the
26665  *		reservation key information into the user provided buffer.
26666  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26667  *
26668  *   Arguments: un   -  Pointer to soft state struct for the target.
26669  *		usrp -	user provided pointer to multihost Persistent In Read
26670  *			Keys structure (mhioc_inkeys_t)
26671  *		flag -	this argument is a pass through to ddi_copyxxx()
26672  *			directly from the mode argument of ioctl().
26673  *
26674  * Return Code: 0   - Success
26675  *		EACCES
26676  *		ENOTSUP
26677  *		errno return code from sd_send_scsi_cmd()
26678  *
26679  *     Context: Can sleep. Does not return until command is completed.
26680  */
26681 
26682 static int
26683 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26684     mhioc_inkeys_t *usrp, int flag)
26685 {
26686 #ifdef _MULTI_DATAMODEL
26687 	struct mhioc_key_list32	li32;
26688 #endif
26689 	sd_prin_readkeys_t	*in;
26690 	mhioc_inkeys_t		*ptr;
26691 	mhioc_key_list_t	li;
26692 	uchar_t			*data_bufp;
26693 	int 			data_len;
26694 	int			rval;
26695 	size_t			copysz;
26696 
26697 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26698 		return (EINVAL);
26699 	}
26700 	bzero(&li, sizeof (mhioc_key_list_t));
26701 
26702 	/*
26703 	 * Get the listsize from user
26704 	 */
26705 #ifdef _MULTI_DATAMODEL
26706 
26707 	switch (ddi_model_convert_from(flag & FMODELS)) {
26708 	case DDI_MODEL_ILP32:
26709 		copysz = sizeof (struct mhioc_key_list32);
26710 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26711 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26712 			    "sd_persistent_reservation_in_read_keys: "
26713 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26714 			rval = EFAULT;
26715 			goto done;
26716 		}
26717 		li.listsize = li32.listsize;
26718 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26719 		break;
26720 
26721 	case DDI_MODEL_NONE:
26722 		copysz = sizeof (mhioc_key_list_t);
26723 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26724 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26725 			    "sd_persistent_reservation_in_read_keys: "
26726 			    "failed ddi_copyin: mhioc_key_list_t\n");
26727 			rval = EFAULT;
26728 			goto done;
26729 		}
26730 		break;
26731 	}
26732 
26733 #else /* ! _MULTI_DATAMODEL */
26734 	copysz = sizeof (mhioc_key_list_t);
26735 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26736 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26737 		    "sd_persistent_reservation_in_read_keys: "
26738 		    "failed ddi_copyin: mhioc_key_list_t\n");
26739 		rval = EFAULT;
26740 		goto done;
26741 	}
26742 #endif
26743 
26744 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26745 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26746 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26747 
26748 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS,
26749 	    data_len, data_bufp)) != 0) {
26750 		goto done;
26751 	}
26752 	in = (sd_prin_readkeys_t *)data_bufp;
26753 	ptr->generation = BE_32(in->generation);
26754 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26755 
26756 	/*
26757 	 * Return the min(listsize, listlen) keys
26758 	 */
26759 #ifdef _MULTI_DATAMODEL
26760 
26761 	switch (ddi_model_convert_from(flag & FMODELS)) {
26762 	case DDI_MODEL_ILP32:
26763 		li32.listlen = li.listlen;
26764 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26765 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26766 			    "sd_persistent_reservation_in_read_keys: "
26767 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26768 			rval = EFAULT;
26769 			goto done;
26770 		}
26771 		break;
26772 
26773 	case DDI_MODEL_NONE:
26774 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26775 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26776 			    "sd_persistent_reservation_in_read_keys: "
26777 			    "failed ddi_copyout: mhioc_key_list_t\n");
26778 			rval = EFAULT;
26779 			goto done;
26780 		}
26781 		break;
26782 	}
26783 
26784 #else /* ! _MULTI_DATAMODEL */
26785 
26786 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26787 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26788 		    "sd_persistent_reservation_in_read_keys: "
26789 		    "failed ddi_copyout: mhioc_key_list_t\n");
26790 		rval = EFAULT;
26791 		goto done;
26792 	}
26793 
26794 #endif /* _MULTI_DATAMODEL */
26795 
26796 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26797 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26798 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26799 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26800 		    "sd_persistent_reservation_in_read_keys: "
26801 		    "failed ddi_copyout: keylist\n");
26802 		rval = EFAULT;
26803 	}
26804 done:
26805 	kmem_free(data_bufp, data_len);
26806 	return (rval);
26807 }
26808 
26809 
26810 /*
26811  *    Function: sd_persistent_reservation_in_read_resv
26812  *
26813  * Description: This routine is the driver entry point for handling CD-ROM
26814  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
26815  *		by sending the SCSI-3 PRIN commands to the device.
26816  *		Process the read persistent reservations command response by
26817  *		copying the reservation information into the user provided
26818  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26819  *
26820  *   Arguments: un   -  Pointer to soft state struct for the target.
26821  *		usrp -	user provided pointer to multihost Persistent In Read
26822  *			Keys structure (mhioc_inkeys_t)
26823  *		flag -	this argument is a pass through to ddi_copyxxx()
26824  *			directly from the mode argument of ioctl().
26825  *
26826  * Return Code: 0   - Success
26827  *		EACCES
26828  *		ENOTSUP
26829  *		errno return code from sd_send_scsi_cmd()
26830  *
26831  *     Context: Can sleep. Does not return until command is completed.
26832  */
26833 
26834 static int
26835 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26836     mhioc_inresvs_t *usrp, int flag)
26837 {
26838 #ifdef _MULTI_DATAMODEL
26839 	struct mhioc_resv_desc_list32 resvlist32;
26840 #endif
26841 	sd_prin_readresv_t	*in;
26842 	mhioc_inresvs_t		*ptr;
26843 	sd_readresv_desc_t	*readresv_ptr;
26844 	mhioc_resv_desc_list_t	resvlist;
26845 	mhioc_resv_desc_t 	resvdesc;
26846 	uchar_t			*data_bufp;
26847 	int 			data_len;
26848 	int			rval;
26849 	int			i;
26850 	size_t			copysz;
26851 	mhioc_resv_desc_t	*bufp;
26852 
26853 	if ((ptr = usrp) == NULL) {
26854 		return (EINVAL);
26855 	}
26856 
26857 	/*
26858 	 * Get the listsize from user
26859 	 */
26860 #ifdef _MULTI_DATAMODEL
26861 	switch (ddi_model_convert_from(flag & FMODELS)) {
26862 	case DDI_MODEL_ILP32:
26863 		copysz = sizeof (struct mhioc_resv_desc_list32);
26864 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26865 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26866 			    "sd_persistent_reservation_in_read_resv: "
26867 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26868 			rval = EFAULT;
26869 			goto done;
26870 		}
26871 		resvlist.listsize = resvlist32.listsize;
26872 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26873 		break;
26874 
26875 	case DDI_MODEL_NONE:
26876 		copysz = sizeof (mhioc_resv_desc_list_t);
26877 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26878 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26879 			    "sd_persistent_reservation_in_read_resv: "
26880 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26881 			rval = EFAULT;
26882 			goto done;
26883 		}
26884 		break;
26885 	}
26886 #else /* ! _MULTI_DATAMODEL */
26887 	copysz = sizeof (mhioc_resv_desc_list_t);
26888 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26889 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26890 		    "sd_persistent_reservation_in_read_resv: "
26891 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26892 		rval = EFAULT;
26893 		goto done;
26894 	}
26895 #endif /* ! _MULTI_DATAMODEL */
26896 
26897 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26898 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26899 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26900 
26901 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV,
26902 	    data_len, data_bufp)) != 0) {
26903 		goto done;
26904 	}
26905 	in = (sd_prin_readresv_t *)data_bufp;
26906 	ptr->generation = BE_32(in->generation);
26907 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26908 
26909 	/*
26910 	 * Return the min(listsize, listlen( keys
26911 	 */
26912 #ifdef _MULTI_DATAMODEL
26913 
26914 	switch (ddi_model_convert_from(flag & FMODELS)) {
26915 	case DDI_MODEL_ILP32:
26916 		resvlist32.listlen = resvlist.listlen;
26917 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26918 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26919 			    "sd_persistent_reservation_in_read_resv: "
26920 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26921 			rval = EFAULT;
26922 			goto done;
26923 		}
26924 		break;
26925 
26926 	case DDI_MODEL_NONE:
26927 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26928 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26929 			    "sd_persistent_reservation_in_read_resv: "
26930 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26931 			rval = EFAULT;
26932 			goto done;
26933 		}
26934 		break;
26935 	}
26936 
26937 #else /* ! _MULTI_DATAMODEL */
26938 
26939 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26940 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26941 		    "sd_persistent_reservation_in_read_resv: "
26942 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26943 		rval = EFAULT;
26944 		goto done;
26945 	}
26946 
26947 #endif /* ! _MULTI_DATAMODEL */
26948 
26949 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26950 	bufp = resvlist.list;
26951 	copysz = sizeof (mhioc_resv_desc_t);
26952 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26953 	    i++, readresv_ptr++, bufp++) {
26954 
26955 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26956 		    MHIOC_RESV_KEY_SIZE);
26957 		resvdesc.type  = readresv_ptr->type;
26958 		resvdesc.scope = readresv_ptr->scope;
26959 		resvdesc.scope_specific_addr =
26960 		    BE_32(readresv_ptr->scope_specific_addr);
26961 
26962 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26963 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26964 			    "sd_persistent_reservation_in_read_resv: "
26965 			    "failed ddi_copyout: resvlist\n");
26966 			rval = EFAULT;
26967 			goto done;
26968 		}
26969 	}
26970 done:
26971 	kmem_free(data_bufp, data_len);
26972 	return (rval);
26973 }
26974 
26975 
26976 /*
26977  *    Function: sr_change_blkmode()
26978  *
26979  * Description: This routine is the driver entry point for handling CD-ROM
26980  *		block mode ioctl requests. Support for returning and changing
26981  *		the current block size in use by the device is implemented. The
26982  *		LBA size is changed via a MODE SELECT Block Descriptor.
26983  *
26984  *		This routine issues a mode sense with an allocation length of
26985  *		12 bytes for the mode page header and a single block descriptor.
26986  *
26987  *   Arguments: dev - the device 'dev_t'
26988  *		cmd - the request type; one of CDROMGBLKMODE (get) or
26989  *		      CDROMSBLKMODE (set)
26990  *		data - current block size or requested block size
26991  *		flag - this argument is a pass through to ddi_copyxxx() directly
26992  *		       from the mode argument of ioctl().
26993  *
26994  * Return Code: the code returned by sd_send_scsi_cmd()
26995  *		EINVAL if invalid arguments are provided
26996  *		EFAULT if ddi_copyxxx() fails
26997  *		ENXIO if fail ddi_get_soft_state
26998  *		EIO if invalid mode sense block descriptor length
26999  *
27000  */
27001 
27002 static int
27003 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
27004 {
27005 	struct sd_lun			*un = NULL;
27006 	struct mode_header		*sense_mhp, *select_mhp;
27007 	struct block_descriptor		*sense_desc, *select_desc;
27008 	int				current_bsize;
27009 	int				rval = EINVAL;
27010 	uchar_t				*sense = NULL;
27011 	uchar_t				*select = NULL;
27012 
27013 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
27014 
27015 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27016 		return (ENXIO);
27017 	}
27018 
27019 	/*
27020 	 * The block length is changed via the Mode Select block descriptor, the
27021 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
27022 	 * required as part of this routine. Therefore the mode sense allocation
27023 	 * length is specified to be the length of a mode page header and a
27024 	 * block descriptor.
27025 	 */
27026 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27027 
27028 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
27029 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) {
27030 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27031 		    "sr_change_blkmode: Mode Sense Failed\n");
27032 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27033 		return (rval);
27034 	}
27035 
27036 	/* Check the block descriptor len to handle only 1 block descriptor */
27037 	sense_mhp = (struct mode_header *)sense;
27038 	if ((sense_mhp->bdesc_length == 0) ||
27039 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
27040 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27041 		    "sr_change_blkmode: Mode Sense returned invalid block"
27042 		    " descriptor length\n");
27043 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27044 		return (EIO);
27045 	}
27046 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
27047 	current_bsize = ((sense_desc->blksize_hi << 16) |
27048 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
27049 
27050 	/* Process command */
27051 	switch (cmd) {
27052 	case CDROMGBLKMODE:
27053 		/* Return the block size obtained during the mode sense */
27054 		if (ddi_copyout(&current_bsize, (void *)data,
27055 		    sizeof (int), flag) != 0)
27056 			rval = EFAULT;
27057 		break;
27058 	case CDROMSBLKMODE:
27059 		/* Validate the requested block size */
27060 		switch (data) {
27061 		case CDROM_BLK_512:
27062 		case CDROM_BLK_1024:
27063 		case CDROM_BLK_2048:
27064 		case CDROM_BLK_2056:
27065 		case CDROM_BLK_2336:
27066 		case CDROM_BLK_2340:
27067 		case CDROM_BLK_2352:
27068 		case CDROM_BLK_2368:
27069 		case CDROM_BLK_2448:
27070 		case CDROM_BLK_2646:
27071 		case CDROM_BLK_2647:
27072 			break;
27073 		default:
27074 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27075 			    "sr_change_blkmode: "
27076 			    "Block Size '%ld' Not Supported\n", data);
27077 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27078 			return (EINVAL);
27079 		}
27080 
27081 		/*
27082 		 * The current block size matches the requested block size so
27083 		 * there is no need to send the mode select to change the size
27084 		 */
27085 		if (current_bsize == data) {
27086 			break;
27087 		}
27088 
27089 		/* Build the select data for the requested block size */
27090 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
27091 		select_mhp = (struct mode_header *)select;
27092 		select_desc =
27093 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
27094 		/*
27095 		 * The LBA size is changed via the block descriptor, so the
27096 		 * descriptor is built according to the user data
27097 		 */
27098 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
27099 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
27100 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
27101 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
27102 
27103 		/* Send the mode select for the requested block size */
27104 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
27105 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27106 		    SD_PATH_STANDARD)) != 0) {
27107 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27108 			    "sr_change_blkmode: Mode Select Failed\n");
27109 			/*
27110 			 * The mode select failed for the requested block size,
27111 			 * so reset the data for the original block size and
27112 			 * send it to the target. The error is indicated by the
27113 			 * return value for the failed mode select.
27114 			 */
27115 			select_desc->blksize_hi  = sense_desc->blksize_hi;
27116 			select_desc->blksize_mid = sense_desc->blksize_mid;
27117 			select_desc->blksize_lo  = sense_desc->blksize_lo;
27118 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
27119 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
27120 			    SD_PATH_STANDARD);
27121 		} else {
27122 			ASSERT(!mutex_owned(SD_MUTEX(un)));
27123 			mutex_enter(SD_MUTEX(un));
27124 			sd_update_block_info(un, (uint32_t)data, 0);
27125 
27126 			mutex_exit(SD_MUTEX(un));
27127 		}
27128 		break;
27129 	default:
27130 		/* should not reach here, but check anyway */
27131 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27132 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
27133 		rval = EINVAL;
27134 		break;
27135 	}
27136 
27137 	if (select) {
27138 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
27139 	}
27140 	if (sense) {
27141 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
27142 	}
27143 	return (rval);
27144 }
27145 
27146 
27147 /*
27148  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
27149  * implement driver support for getting and setting the CD speed. The command
27150  * set used will be based on the device type. If the device has not been
27151  * identified as MMC the Toshiba vendor specific mode page will be used. If
27152  * the device is MMC but does not support the Real Time Streaming feature
27153  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
27154  * be used to read the speed.
27155  */
27156 
27157 /*
27158  *    Function: sr_change_speed()
27159  *
27160  * Description: This routine is the driver entry point for handling CD-ROM
27161  *		drive speed ioctl requests for devices supporting the Toshiba
27162  *		vendor specific drive speed mode page. Support for returning
27163  *		and changing the current drive speed in use by the device is
27164  *		implemented.
27165  *
27166  *   Arguments: dev - the device 'dev_t'
27167  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
27168  *		      CDROMSDRVSPEED (set)
27169  *		data - current drive speed or requested drive speed
27170  *		flag - this argument is a pass through to ddi_copyxxx() directly
27171  *		       from the mode argument of ioctl().
27172  *
27173  * Return Code: the code returned by sd_send_scsi_cmd()
27174  *		EINVAL if invalid arguments are provided
27175  *		EFAULT if ddi_copyxxx() fails
27176  *		ENXIO if fail ddi_get_soft_state
27177  *		EIO if invalid mode sense block descriptor length
27178  */
27179 
27180 static int
27181 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27182 {
27183 	struct sd_lun			*un = NULL;
27184 	struct mode_header		*sense_mhp, *select_mhp;
27185 	struct mode_speed		*sense_page, *select_page;
27186 	int				current_speed;
27187 	int				rval = EINVAL;
27188 	int				bd_len;
27189 	uchar_t				*sense = NULL;
27190 	uchar_t				*select = NULL;
27191 
27192 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27193 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27194 		return (ENXIO);
27195 	}
27196 
27197 	/*
27198 	 * Note: The drive speed is being modified here according to a Toshiba
27199 	 * vendor specific mode page (0x31).
27200 	 */
27201 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27202 
27203 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
27204 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
27205 	    SD_PATH_STANDARD)) != 0) {
27206 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27207 		    "sr_change_speed: Mode Sense Failed\n");
27208 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27209 		return (rval);
27210 	}
27211 	sense_mhp  = (struct mode_header *)sense;
27212 
27213 	/* Check the block descriptor len to handle only 1 block descriptor */
27214 	bd_len = sense_mhp->bdesc_length;
27215 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27216 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27217 		    "sr_change_speed: Mode Sense returned invalid block "
27218 		    "descriptor length\n");
27219 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27220 		return (EIO);
27221 	}
27222 
27223 	sense_page = (struct mode_speed *)
27224 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
27225 	current_speed = sense_page->speed;
27226 
27227 	/* Process command */
27228 	switch (cmd) {
27229 	case CDROMGDRVSPEED:
27230 		/* Return the drive speed obtained during the mode sense */
27231 		if (current_speed == 0x2) {
27232 			current_speed = CDROM_TWELVE_SPEED;
27233 		}
27234 		if (ddi_copyout(&current_speed, (void *)data,
27235 		    sizeof (int), flag) != 0) {
27236 			rval = EFAULT;
27237 		}
27238 		break;
27239 	case CDROMSDRVSPEED:
27240 		/* Validate the requested drive speed */
27241 		switch ((uchar_t)data) {
27242 		case CDROM_TWELVE_SPEED:
27243 			data = 0x2;
27244 			/*FALLTHROUGH*/
27245 		case CDROM_NORMAL_SPEED:
27246 		case CDROM_DOUBLE_SPEED:
27247 		case CDROM_QUAD_SPEED:
27248 		case CDROM_MAXIMUM_SPEED:
27249 			break;
27250 		default:
27251 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27252 			    "sr_change_speed: "
27253 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
27254 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27255 			return (EINVAL);
27256 		}
27257 
27258 		/*
27259 		 * The current drive speed matches the requested drive speed so
27260 		 * there is no need to send the mode select to change the speed
27261 		 */
27262 		if (current_speed == data) {
27263 			break;
27264 		}
27265 
27266 		/* Build the select data for the requested drive speed */
27267 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
27268 		select_mhp = (struct mode_header *)select;
27269 		select_mhp->bdesc_length = 0;
27270 		select_page =
27271 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27272 		select_page =
27273 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
27274 		select_page->mode_page.code = CDROM_MODE_SPEED;
27275 		select_page->mode_page.length = 2;
27276 		select_page->speed = (uchar_t)data;
27277 
27278 		/* Send the mode select for the requested block size */
27279 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
27280 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27281 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
27282 			/*
27283 			 * The mode select failed for the requested drive speed,
27284 			 * so reset the data for the original drive speed and
27285 			 * send it to the target. The error is indicated by the
27286 			 * return value for the failed mode select.
27287 			 */
27288 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27289 			    "sr_drive_speed: Mode Select Failed\n");
27290 			select_page->speed = sense_page->speed;
27291 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
27292 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
27293 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
27294 		}
27295 		break;
27296 	default:
27297 		/* should not reach here, but check anyway */
27298 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27299 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
27300 		rval = EINVAL;
27301 		break;
27302 	}
27303 
27304 	if (select) {
27305 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
27306 	}
27307 	if (sense) {
27308 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
27309 	}
27310 
27311 	return (rval);
27312 }
27313 
27314 
27315 /*
27316  *    Function: sr_atapi_change_speed()
27317  *
27318  * Description: This routine is the driver entry point for handling CD-ROM
27319  *		drive speed ioctl requests for MMC devices that do not support
27320  *		the Real Time Streaming feature (0x107).
27321  *
27322  *		Note: This routine will use the SET SPEED command which may not
27323  *		be supported by all devices.
27324  *
27325  *   Arguments: dev- the device 'dev_t'
27326  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
27327  *		     CDROMSDRVSPEED (set)
27328  *		data- current drive speed or requested drive speed
27329  *		flag- this argument is a pass through to ddi_copyxxx() directly
27330  *		      from the mode argument of ioctl().
27331  *
27332  * Return Code: the code returned by sd_send_scsi_cmd()
27333  *		EINVAL if invalid arguments are provided
27334  *		EFAULT if ddi_copyxxx() fails
27335  *		ENXIO if fail ddi_get_soft_state
27336  *		EIO if invalid mode sense block descriptor length
27337  */
27338 
27339 static int
27340 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
27341 {
27342 	struct sd_lun			*un;
27343 	struct uscsi_cmd		*com = NULL;
27344 	struct mode_header_grp2		*sense_mhp;
27345 	uchar_t				*sense_page;
27346 	uchar_t				*sense = NULL;
27347 	char				cdb[CDB_GROUP5];
27348 	int				bd_len;
27349 	int				current_speed = 0;
27350 	int				max_speed = 0;
27351 	int				rval;
27352 
27353 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
27354 
27355 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27356 		return (ENXIO);
27357 	}
27358 
27359 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
27360 
27361 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
27362 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
27363 	    SD_PATH_STANDARD)) != 0) {
27364 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27365 		    "sr_atapi_change_speed: Mode Sense Failed\n");
27366 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27367 		return (rval);
27368 	}
27369 
27370 	/* Check the block descriptor len to handle only 1 block descriptor */
27371 	sense_mhp = (struct mode_header_grp2 *)sense;
27372 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
27373 	if (bd_len > MODE_BLK_DESC_LENGTH) {
27374 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27375 		    "sr_atapi_change_speed: Mode Sense returned invalid "
27376 		    "block descriptor length\n");
27377 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27378 		return (EIO);
27379 	}
27380 
27381 	/* Calculate the current and maximum drive speeds */
27382 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
27383 	current_speed = (sense_page[14] << 8) | sense_page[15];
27384 	max_speed = (sense_page[8] << 8) | sense_page[9];
27385 
27386 	/* Process the command */
27387 	switch (cmd) {
27388 	case CDROMGDRVSPEED:
27389 		current_speed /= SD_SPEED_1X;
27390 		if (ddi_copyout(&current_speed, (void *)data,
27391 		    sizeof (int), flag) != 0)
27392 			rval = EFAULT;
27393 		break;
27394 	case CDROMSDRVSPEED:
27395 		/* Convert the speed code to KB/sec */
27396 		switch ((uchar_t)data) {
27397 		case CDROM_NORMAL_SPEED:
27398 			current_speed = SD_SPEED_1X;
27399 			break;
27400 		case CDROM_DOUBLE_SPEED:
27401 			current_speed = 2 * SD_SPEED_1X;
27402 			break;
27403 		case CDROM_QUAD_SPEED:
27404 			current_speed = 4 * SD_SPEED_1X;
27405 			break;
27406 		case CDROM_TWELVE_SPEED:
27407 			current_speed = 12 * SD_SPEED_1X;
27408 			break;
27409 		case CDROM_MAXIMUM_SPEED:
27410 			current_speed = 0xffff;
27411 			break;
27412 		default:
27413 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27414 			    "sr_atapi_change_speed: invalid drive speed %d\n",
27415 			    (uchar_t)data);
27416 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27417 			return (EINVAL);
27418 		}
27419 
27420 		/* Check the request against the drive's max speed. */
27421 		if (current_speed != 0xffff) {
27422 			if (current_speed > max_speed) {
27423 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27424 				return (EINVAL);
27425 			}
27426 		}
27427 
27428 		/*
27429 		 * Build and send the SET SPEED command
27430 		 *
27431 		 * Note: The SET SPEED (0xBB) command used in this routine is
27432 		 * obsolete per the SCSI MMC spec but still supported in the
27433 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27434 		 * therefore the command is still implemented in this routine.
27435 		 */
27436 		bzero(cdb, sizeof (cdb));
27437 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
27438 		cdb[2] = (uchar_t)(current_speed >> 8);
27439 		cdb[3] = (uchar_t)current_speed;
27440 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27441 		com->uscsi_cdb	   = (caddr_t)cdb;
27442 		com->uscsi_cdblen  = CDB_GROUP5;
27443 		com->uscsi_bufaddr = NULL;
27444 		com->uscsi_buflen  = 0;
27445 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
27446 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0,
27447 		    UIO_SYSSPACE, SD_PATH_STANDARD);
27448 		break;
27449 	default:
27450 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27451 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
27452 		rval = EINVAL;
27453 	}
27454 
27455 	if (sense) {
27456 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
27457 	}
27458 	if (com) {
27459 		kmem_free(com, sizeof (*com));
27460 	}
27461 	return (rval);
27462 }
27463 
27464 
27465 /*
27466  *    Function: sr_pause_resume()
27467  *
27468  * Description: This routine is the driver entry point for handling CD-ROM
27469  *		pause/resume ioctl requests. This only affects the audio play
27470  *		operation.
27471  *
27472  *   Arguments: dev - the device 'dev_t'
27473  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
27474  *		      for setting the resume bit of the cdb.
27475  *
27476  * Return Code: the code returned by sd_send_scsi_cmd()
27477  *		EINVAL if invalid mode specified
27478  *
27479  */
27480 
27481 static int
27482 sr_pause_resume(dev_t dev, int cmd)
27483 {
27484 	struct sd_lun		*un;
27485 	struct uscsi_cmd	*com;
27486 	char			cdb[CDB_GROUP1];
27487 	int			rval;
27488 
27489 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27490 		return (ENXIO);
27491 	}
27492 
27493 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27494 	bzero(cdb, CDB_GROUP1);
27495 	cdb[0] = SCMD_PAUSE_RESUME;
27496 	switch (cmd) {
27497 	case CDROMRESUME:
27498 		cdb[8] = 1;
27499 		break;
27500 	case CDROMPAUSE:
27501 		cdb[8] = 0;
27502 		break;
27503 	default:
27504 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
27505 		    " Command '%x' Not Supported\n", cmd);
27506 		rval = EINVAL;
27507 		goto done;
27508 	}
27509 
27510 	com->uscsi_cdb    = cdb;
27511 	com->uscsi_cdblen = CDB_GROUP1;
27512 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27513 
27514 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27515 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27516 
27517 done:
27518 	kmem_free(com, sizeof (*com));
27519 	return (rval);
27520 }
27521 
27522 
27523 /*
27524  *    Function: sr_play_msf()
27525  *
27526  * Description: This routine is the driver entry point for handling CD-ROM
27527  *		ioctl requests to output the audio signals at the specified
27528  *		starting address and continue the audio play until the specified
27529  *		ending address (CDROMPLAYMSF) The address is in Minute Second
27530  *		Frame (MSF) format.
27531  *
27532  *   Arguments: dev	- the device 'dev_t'
27533  *		data	- pointer to user provided audio msf structure,
27534  *		          specifying start/end addresses.
27535  *		flag	- this argument is a pass through to ddi_copyxxx()
27536  *		          directly from the mode argument of ioctl().
27537  *
27538  * Return Code: the code returned by sd_send_scsi_cmd()
27539  *		EFAULT if ddi_copyxxx() fails
27540  *		ENXIO if fail ddi_get_soft_state
27541  *		EINVAL if data pointer is NULL
27542  */
27543 
27544 static int
27545 sr_play_msf(dev_t dev, caddr_t data, int flag)
27546 {
27547 	struct sd_lun		*un;
27548 	struct uscsi_cmd	*com;
27549 	struct cdrom_msf	msf_struct;
27550 	struct cdrom_msf	*msf = &msf_struct;
27551 	char			cdb[CDB_GROUP1];
27552 	int			rval;
27553 
27554 	if (data == NULL) {
27555 		return (EINVAL);
27556 	}
27557 
27558 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27559 		return (ENXIO);
27560 	}
27561 
27562 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
27563 		return (EFAULT);
27564 	}
27565 
27566 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27567 	bzero(cdb, CDB_GROUP1);
27568 	cdb[0] = SCMD_PLAYAUDIO_MSF;
27569 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
27570 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
27571 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
27572 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
27573 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
27574 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
27575 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
27576 	} else {
27577 		cdb[3] = msf->cdmsf_min0;
27578 		cdb[4] = msf->cdmsf_sec0;
27579 		cdb[5] = msf->cdmsf_frame0;
27580 		cdb[6] = msf->cdmsf_min1;
27581 		cdb[7] = msf->cdmsf_sec1;
27582 		cdb[8] = msf->cdmsf_frame1;
27583 	}
27584 	com->uscsi_cdb    = cdb;
27585 	com->uscsi_cdblen = CDB_GROUP1;
27586 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27587 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27588 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27589 	kmem_free(com, sizeof (*com));
27590 	return (rval);
27591 }
27592 
27593 
27594 /*
27595  *    Function: sr_play_trkind()
27596  *
27597  * Description: This routine is the driver entry point for handling CD-ROM
27598  *		ioctl requests to output the audio signals at the specified
27599  *		starting address and continue the audio play until the specified
27600  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27601  *		format.
27602  *
27603  *   Arguments: dev	- the device 'dev_t'
27604  *		data	- pointer to user provided audio track/index structure,
27605  *		          specifying start/end addresses.
27606  *		flag	- this argument is a pass through to ddi_copyxxx()
27607  *		          directly from the mode argument of ioctl().
27608  *
27609  * Return Code: the code returned by sd_send_scsi_cmd()
27610  *		EFAULT if ddi_copyxxx() fails
27611  *		ENXIO if fail ddi_get_soft_state
27612  *		EINVAL if data pointer is NULL
27613  */
27614 
27615 static int
27616 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27617 {
27618 	struct cdrom_ti		ti_struct;
27619 	struct cdrom_ti		*ti = &ti_struct;
27620 	struct uscsi_cmd	*com = NULL;
27621 	char			cdb[CDB_GROUP1];
27622 	int			rval;
27623 
27624 	if (data == NULL) {
27625 		return (EINVAL);
27626 	}
27627 
27628 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27629 		return (EFAULT);
27630 	}
27631 
27632 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27633 	bzero(cdb, CDB_GROUP1);
27634 	cdb[0] = SCMD_PLAYAUDIO_TI;
27635 	cdb[4] = ti->cdti_trk0;
27636 	cdb[5] = ti->cdti_ind0;
27637 	cdb[7] = ti->cdti_trk1;
27638 	cdb[8] = ti->cdti_ind1;
27639 	com->uscsi_cdb    = cdb;
27640 	com->uscsi_cdblen = CDB_GROUP1;
27641 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27642 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27643 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27644 	kmem_free(com, sizeof (*com));
27645 	return (rval);
27646 }
27647 
27648 
27649 /*
27650  *    Function: sr_read_all_subcodes()
27651  *
27652  * Description: This routine is the driver entry point for handling CD-ROM
27653  *		ioctl requests to return raw subcode data while the target is
27654  *		playing audio (CDROMSUBCODE).
27655  *
27656  *   Arguments: dev	- the device 'dev_t'
27657  *		data	- pointer to user provided cdrom subcode structure,
27658  *		          specifying the transfer length and address.
27659  *		flag	- this argument is a pass through to ddi_copyxxx()
27660  *		          directly from the mode argument of ioctl().
27661  *
27662  * Return Code: the code returned by sd_send_scsi_cmd()
27663  *		EFAULT if ddi_copyxxx() fails
27664  *		ENXIO if fail ddi_get_soft_state
27665  *		EINVAL if data pointer is NULL
27666  */
27667 
27668 static int
27669 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27670 {
27671 	struct sd_lun		*un = NULL;
27672 	struct uscsi_cmd	*com = NULL;
27673 	struct cdrom_subcode	*subcode = NULL;
27674 	int			rval;
27675 	size_t			buflen;
27676 	char			cdb[CDB_GROUP5];
27677 
27678 #ifdef _MULTI_DATAMODEL
27679 	/* To support ILP32 applications in an LP64 world */
27680 	struct cdrom_subcode32		cdrom_subcode32;
27681 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27682 #endif
27683 	if (data == NULL) {
27684 		return (EINVAL);
27685 	}
27686 
27687 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27688 		return (ENXIO);
27689 	}
27690 
27691 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27692 
27693 #ifdef _MULTI_DATAMODEL
27694 	switch (ddi_model_convert_from(flag & FMODELS)) {
27695 	case DDI_MODEL_ILP32:
27696 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27697 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27698 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27699 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27700 			return (EFAULT);
27701 		}
27702 		/* Convert the ILP32 uscsi data from the application to LP64 */
27703 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27704 		break;
27705 	case DDI_MODEL_NONE:
27706 		if (ddi_copyin(data, subcode,
27707 		    sizeof (struct cdrom_subcode), flag)) {
27708 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27709 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27710 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27711 			return (EFAULT);
27712 		}
27713 		break;
27714 	}
27715 #else /* ! _MULTI_DATAMODEL */
27716 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27717 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27718 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27719 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27720 		return (EFAULT);
27721 	}
27722 #endif /* _MULTI_DATAMODEL */
27723 
27724 	/*
27725 	 * Since MMC-2 expects max 3 bytes for length, check if the
27726 	 * length input is greater than 3 bytes
27727 	 */
27728 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27729 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27730 		    "sr_read_all_subcodes: "
27731 		    "cdrom transfer length too large: %d (limit %d)\n",
27732 		    subcode->cdsc_length, 0xFFFFFF);
27733 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27734 		return (EINVAL);
27735 	}
27736 
27737 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27738 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27739 	bzero(cdb, CDB_GROUP5);
27740 
27741 	if (un->un_f_mmc_cap == TRUE) {
27742 		cdb[0] = (char)SCMD_READ_CD;
27743 		cdb[2] = (char)0xff;
27744 		cdb[3] = (char)0xff;
27745 		cdb[4] = (char)0xff;
27746 		cdb[5] = (char)0xff;
27747 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27748 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27749 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27750 		cdb[10] = 1;
27751 	} else {
27752 		/*
27753 		 * Note: A vendor specific command (0xDF) is being used her to
27754 		 * request a read of all subcodes.
27755 		 */
27756 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27757 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27758 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27759 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27760 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27761 	}
27762 	com->uscsi_cdb	   = cdb;
27763 	com->uscsi_cdblen  = CDB_GROUP5;
27764 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27765 	com->uscsi_buflen  = buflen;
27766 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27767 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
27768 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27769 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27770 	kmem_free(com, sizeof (*com));
27771 	return (rval);
27772 }
27773 
27774 
27775 /*
27776  *    Function: sr_read_subchannel()
27777  *
27778  * Description: This routine is the driver entry point for handling CD-ROM
27779  *		ioctl requests to return the Q sub-channel data of the CD
27780  *		current position block. (CDROMSUBCHNL) The data includes the
27781  *		track number, index number, absolute CD-ROM address (LBA or MSF
27782  *		format per the user) , track relative CD-ROM address (LBA or MSF
27783  *		format per the user), control data and audio status.
27784  *
27785  *   Arguments: dev	- the device 'dev_t'
27786  *		data	- pointer to user provided cdrom sub-channel structure
27787  *		flag	- this argument is a pass through to ddi_copyxxx()
27788  *		          directly from the mode argument of ioctl().
27789  *
27790  * Return Code: the code returned by sd_send_scsi_cmd()
27791  *		EFAULT if ddi_copyxxx() fails
27792  *		ENXIO if fail ddi_get_soft_state
27793  *		EINVAL if data pointer is NULL
27794  */
27795 
27796 static int
27797 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27798 {
27799 	struct sd_lun		*un;
27800 	struct uscsi_cmd	*com;
27801 	struct cdrom_subchnl	subchanel;
27802 	struct cdrom_subchnl	*subchnl = &subchanel;
27803 	char			cdb[CDB_GROUP1];
27804 	caddr_t			buffer;
27805 	int			rval;
27806 
27807 	if (data == NULL) {
27808 		return (EINVAL);
27809 	}
27810 
27811 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27812 	    (un->un_state == SD_STATE_OFFLINE)) {
27813 		return (ENXIO);
27814 	}
27815 
27816 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27817 		return (EFAULT);
27818 	}
27819 
27820 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27821 	bzero(cdb, CDB_GROUP1);
27822 	cdb[0] = SCMD_READ_SUBCHANNEL;
27823 	/* Set the MSF bit based on the user requested address format */
27824 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27825 	/*
27826 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27827 	 * returned
27828 	 */
27829 	cdb[2] = 0x40;
27830 	/*
27831 	 * Set byte 3 to specify the return data format. A value of 0x01
27832 	 * indicates that the CD-ROM current position should be returned.
27833 	 */
27834 	cdb[3] = 0x01;
27835 	cdb[8] = 0x10;
27836 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27837 	com->uscsi_cdb	   = cdb;
27838 	com->uscsi_cdblen  = CDB_GROUP1;
27839 	com->uscsi_bufaddr = buffer;
27840 	com->uscsi_buflen  = 16;
27841 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27842 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27843 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27844 	if (rval != 0) {
27845 		kmem_free(buffer, 16);
27846 		kmem_free(com, sizeof (*com));
27847 		return (rval);
27848 	}
27849 
27850 	/* Process the returned Q sub-channel data */
27851 	subchnl->cdsc_audiostatus = buffer[1];
27852 	subchnl->cdsc_adr	= (buffer[5] & 0xF0);
27853 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
27854 	subchnl->cdsc_trk	= buffer[6];
27855 	subchnl->cdsc_ind	= buffer[7];
27856 	if (subchnl->cdsc_format & CDROM_LBA) {
27857 		subchnl->cdsc_absaddr.lba =
27858 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27859 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27860 		subchnl->cdsc_reladdr.lba =
27861 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27862 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27863 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
27864 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27865 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27866 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27867 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27868 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27869 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27870 	} else {
27871 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
27872 		subchnl->cdsc_absaddr.msf.second = buffer[10];
27873 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27874 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
27875 		subchnl->cdsc_reladdr.msf.second = buffer[14];
27876 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27877 	}
27878 	kmem_free(buffer, 16);
27879 	kmem_free(com, sizeof (*com));
27880 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27881 	    != 0) {
27882 		return (EFAULT);
27883 	}
27884 	return (rval);
27885 }
27886 
27887 
27888 /*
27889  *    Function: sr_read_tocentry()
27890  *
27891  * Description: This routine is the driver entry point for handling CD-ROM
27892  *		ioctl requests to read from the Table of Contents (TOC)
27893  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27894  *		fields, the starting address (LBA or MSF format per the user)
27895  *		and the data mode if the user specified track is a data track.
27896  *
27897  *		Note: The READ HEADER (0x44) command used in this routine is
27898  *		obsolete per the SCSI MMC spec but still supported in the
27899  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27900  *		therefore the command is still implemented in this routine.
27901  *
27902  *   Arguments: dev	- the device 'dev_t'
27903  *		data	- pointer to user provided toc entry structure,
27904  *			  specifying the track # and the address format
27905  *			  (LBA or MSF).
27906  *		flag	- this argument is a pass through to ddi_copyxxx()
27907  *		          directly from the mode argument of ioctl().
27908  *
27909  * Return Code: the code returned by sd_send_scsi_cmd()
27910  *		EFAULT if ddi_copyxxx() fails
27911  *		ENXIO if fail ddi_get_soft_state
27912  *		EINVAL if data pointer is NULL
27913  */
27914 
27915 static int
27916 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27917 {
27918 	struct sd_lun		*un = NULL;
27919 	struct uscsi_cmd	*com;
27920 	struct cdrom_tocentry	toc_entry;
27921 	struct cdrom_tocentry	*entry = &toc_entry;
27922 	caddr_t			buffer;
27923 	int			rval;
27924 	char			cdb[CDB_GROUP1];
27925 
27926 	if (data == NULL) {
27927 		return (EINVAL);
27928 	}
27929 
27930 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27931 	    (un->un_state == SD_STATE_OFFLINE)) {
27932 		return (ENXIO);
27933 	}
27934 
27935 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27936 		return (EFAULT);
27937 	}
27938 
27939 	/* Validate the requested track and address format */
27940 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27941 		return (EINVAL);
27942 	}
27943 
27944 	if (entry->cdte_track == 0) {
27945 		return (EINVAL);
27946 	}
27947 
27948 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27949 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27950 	bzero(cdb, CDB_GROUP1);
27951 
27952 	cdb[0] = SCMD_READ_TOC;
27953 	/* Set the MSF bit based on the user requested address format  */
27954 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27955 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27956 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27957 	} else {
27958 		cdb[6] = entry->cdte_track;
27959 	}
27960 
27961 	/*
27962 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27963 	 * (4 byte TOC response header + 8 byte track descriptor)
27964 	 */
27965 	cdb[8] = 12;
27966 	com->uscsi_cdb	   = cdb;
27967 	com->uscsi_cdblen  = CDB_GROUP1;
27968 	com->uscsi_bufaddr = buffer;
27969 	com->uscsi_buflen  = 0x0C;
27970 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27971 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27972 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27973 	if (rval != 0) {
27974 		kmem_free(buffer, 12);
27975 		kmem_free(com, sizeof (*com));
27976 		return (rval);
27977 	}
27978 
27979 	/* Process the toc entry */
27980 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
27981 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
27982 	if (entry->cdte_format & CDROM_LBA) {
27983 		entry->cdte_addr.lba =
27984 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27985 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27986 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27987 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
27988 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
27989 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
27990 		/*
27991 		 * Send a READ TOC command using the LBA address format to get
27992 		 * the LBA for the track requested so it can be used in the
27993 		 * READ HEADER request
27994 		 *
27995 		 * Note: The MSF bit of the READ HEADER command specifies the
27996 		 * output format. The block address specified in that command
27997 		 * must be in LBA format.
27998 		 */
27999 		cdb[1] = 0;
28000 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28001 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28002 		if (rval != 0) {
28003 			kmem_free(buffer, 12);
28004 			kmem_free(com, sizeof (*com));
28005 			return (rval);
28006 		}
28007 	} else {
28008 		entry->cdte_addr.msf.minute	= buffer[9];
28009 		entry->cdte_addr.msf.second	= buffer[10];
28010 		entry->cdte_addr.msf.frame	= buffer[11];
28011 		/*
28012 		 * Send a READ TOC command using the LBA address format to get
28013 		 * the LBA for the track requested so it can be used in the
28014 		 * READ HEADER request
28015 		 *
28016 		 * Note: The MSF bit of the READ HEADER command specifies the
28017 		 * output format. The block address specified in that command
28018 		 * must be in LBA format.
28019 		 */
28020 		cdb[1] = 0;
28021 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28022 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28023 		if (rval != 0) {
28024 			kmem_free(buffer, 12);
28025 			kmem_free(com, sizeof (*com));
28026 			return (rval);
28027 		}
28028 	}
28029 
28030 	/*
28031 	 * Build and send the READ HEADER command to determine the data mode of
28032 	 * the user specified track.
28033 	 */
28034 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
28035 	    (entry->cdte_track != CDROM_LEADOUT)) {
28036 		bzero(cdb, CDB_GROUP1);
28037 		cdb[0] = SCMD_READ_HEADER;
28038 		cdb[2] = buffer[8];
28039 		cdb[3] = buffer[9];
28040 		cdb[4] = buffer[10];
28041 		cdb[5] = buffer[11];
28042 		cdb[8] = 0x08;
28043 		com->uscsi_buflen = 0x08;
28044 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28045 		    UIO_SYSSPACE, SD_PATH_STANDARD);
28046 		if (rval == 0) {
28047 			entry->cdte_datamode = buffer[0];
28048 		} else {
28049 			/*
28050 			 * READ HEADER command failed, since this is
28051 			 * obsoleted in one spec, its better to return
28052 			 * -1 for an invlid track so that we can still
28053 			 * recieve the rest of the TOC data.
28054 			 */
28055 			entry->cdte_datamode = (uchar_t)-1;
28056 		}
28057 	} else {
28058 		entry->cdte_datamode = (uchar_t)-1;
28059 	}
28060 
28061 	kmem_free(buffer, 12);
28062 	kmem_free(com, sizeof (*com));
28063 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
28064 		return (EFAULT);
28065 
28066 	return (rval);
28067 }
28068 
28069 
28070 /*
28071  *    Function: sr_read_tochdr()
28072  *
28073  * Description: This routine is the driver entry point for handling CD-ROM
28074  * 		ioctl requests to read the Table of Contents (TOC) header
28075  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
28076  *		and ending track numbers
28077  *
28078  *   Arguments: dev	- the device 'dev_t'
28079  *		data	- pointer to user provided toc header structure,
28080  *			  specifying the starting and ending track numbers.
28081  *		flag	- this argument is a pass through to ddi_copyxxx()
28082  *			  directly from the mode argument of ioctl().
28083  *
28084  * Return Code: the code returned by sd_send_scsi_cmd()
28085  *		EFAULT if ddi_copyxxx() fails
28086  *		ENXIO if fail ddi_get_soft_state
28087  *		EINVAL if data pointer is NULL
28088  */
28089 
28090 static int
28091 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
28092 {
28093 	struct sd_lun		*un;
28094 	struct uscsi_cmd	*com;
28095 	struct cdrom_tochdr	toc_header;
28096 	struct cdrom_tochdr	*hdr = &toc_header;
28097 	char			cdb[CDB_GROUP1];
28098 	int			rval;
28099 	caddr_t			buffer;
28100 
28101 	if (data == NULL) {
28102 		return (EINVAL);
28103 	}
28104 
28105 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28106 	    (un->un_state == SD_STATE_OFFLINE)) {
28107 		return (ENXIO);
28108 	}
28109 
28110 	buffer = kmem_zalloc(4, KM_SLEEP);
28111 	bzero(cdb, CDB_GROUP1);
28112 	cdb[0] = SCMD_READ_TOC;
28113 	/*
28114 	 * Specifying a track number of 0x00 in the READ TOC command indicates
28115 	 * that the TOC header should be returned
28116 	 */
28117 	cdb[6] = 0x00;
28118 	/*
28119 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
28120 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
28121 	 */
28122 	cdb[8] = 0x04;
28123 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28124 	com->uscsi_cdb	   = cdb;
28125 	com->uscsi_cdblen  = CDB_GROUP1;
28126 	com->uscsi_bufaddr = buffer;
28127 	com->uscsi_buflen  = 0x04;
28128 	com->uscsi_timeout = 300;
28129 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28130 
28131 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28132 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28133 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
28134 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
28135 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
28136 	} else {
28137 		hdr->cdth_trk0 = buffer[2];
28138 		hdr->cdth_trk1 = buffer[3];
28139 	}
28140 	kmem_free(buffer, 4);
28141 	kmem_free(com, sizeof (*com));
28142 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
28143 		return (EFAULT);
28144 	}
28145 	return (rval);
28146 }
28147 
28148 
28149 /*
28150  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
28151  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
28152  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
28153  * digital audio and extended architecture digital audio. These modes are
28154  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
28155  * MMC specs.
28156  *
28157  * In addition to support for the various data formats these routines also
28158  * include support for devices that implement only the direct access READ
28159  * commands (0x08, 0x28), devices that implement the READ_CD commands
28160  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
28161  * READ CDXA commands (0xD8, 0xDB)
28162  */
28163 
28164 /*
28165  *    Function: sr_read_mode1()
28166  *
28167  * Description: This routine is the driver entry point for handling CD-ROM
28168  *		ioctl read mode1 requests (CDROMREADMODE1).
28169  *
28170  *   Arguments: dev	- the device 'dev_t'
28171  *		data	- pointer to user provided cd read structure specifying
28172  *			  the lba buffer address and length.
28173  *		flag	- this argument is a pass through to ddi_copyxxx()
28174  *			  directly from the mode argument of ioctl().
28175  *
28176  * Return Code: the code returned by sd_send_scsi_cmd()
28177  *		EFAULT if ddi_copyxxx() fails
28178  *		ENXIO if fail ddi_get_soft_state
28179  *		EINVAL if data pointer is NULL
28180  */
28181 
28182 static int
28183 sr_read_mode1(dev_t dev, caddr_t data, int flag)
28184 {
28185 	struct sd_lun		*un;
28186 	struct cdrom_read	mode1_struct;
28187 	struct cdrom_read	*mode1 = &mode1_struct;
28188 	int			rval;
28189 #ifdef _MULTI_DATAMODEL
28190 	/* To support ILP32 applications in an LP64 world */
28191 	struct cdrom_read32	cdrom_read32;
28192 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28193 #endif /* _MULTI_DATAMODEL */
28194 
28195 	if (data == NULL) {
28196 		return (EINVAL);
28197 	}
28198 
28199 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28200 	    (un->un_state == SD_STATE_OFFLINE)) {
28201 		return (ENXIO);
28202 	}
28203 
28204 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28205 	    "sd_read_mode1: entry: un:0x%p\n", un);
28206 
28207 #ifdef _MULTI_DATAMODEL
28208 	switch (ddi_model_convert_from(flag & FMODELS)) {
28209 	case DDI_MODEL_ILP32:
28210 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28211 			return (EFAULT);
28212 		}
28213 		/* Convert the ILP32 uscsi data from the application to LP64 */
28214 		cdrom_read32tocdrom_read(cdrd32, mode1);
28215 		break;
28216 	case DDI_MODEL_NONE:
28217 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28218 			return (EFAULT);
28219 		}
28220 	}
28221 #else /* ! _MULTI_DATAMODEL */
28222 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
28223 		return (EFAULT);
28224 	}
28225 #endif /* _MULTI_DATAMODEL */
28226 
28227 	rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr,
28228 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
28229 
28230 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28231 	    "sd_read_mode1: exit: un:0x%p\n", un);
28232 
28233 	return (rval);
28234 }
28235 
28236 
28237 /*
28238  *    Function: sr_read_cd_mode2()
28239  *
28240  * Description: This routine is the driver entry point for handling CD-ROM
28241  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28242  *		support the READ CD (0xBE) command or the 1st generation
28243  *		READ CD (0xD4) command.
28244  *
28245  *   Arguments: dev	- the device 'dev_t'
28246  *		data	- pointer to user provided cd read structure specifying
28247  *			  the lba buffer address and length.
28248  *		flag	- this argument is a pass through to ddi_copyxxx()
28249  *			  directly from the mode argument of ioctl().
28250  *
28251  * Return Code: the code returned by sd_send_scsi_cmd()
28252  *		EFAULT if ddi_copyxxx() fails
28253  *		ENXIO if fail ddi_get_soft_state
28254  *		EINVAL if data pointer is NULL
28255  */
28256 
28257 static int
28258 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
28259 {
28260 	struct sd_lun		*un;
28261 	struct uscsi_cmd	*com;
28262 	struct cdrom_read	mode2_struct;
28263 	struct cdrom_read	*mode2 = &mode2_struct;
28264 	uchar_t			cdb[CDB_GROUP5];
28265 	int			nblocks;
28266 	int			rval;
28267 #ifdef _MULTI_DATAMODEL
28268 	/*  To support ILP32 applications in an LP64 world */
28269 	struct cdrom_read32	cdrom_read32;
28270 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28271 #endif /* _MULTI_DATAMODEL */
28272 
28273 	if (data == NULL) {
28274 		return (EINVAL);
28275 	}
28276 
28277 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28278 	    (un->un_state == SD_STATE_OFFLINE)) {
28279 		return (ENXIO);
28280 	}
28281 
28282 #ifdef _MULTI_DATAMODEL
28283 	switch (ddi_model_convert_from(flag & FMODELS)) {
28284 	case DDI_MODEL_ILP32:
28285 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28286 			return (EFAULT);
28287 		}
28288 		/* Convert the ILP32 uscsi data from the application to LP64 */
28289 		cdrom_read32tocdrom_read(cdrd32, mode2);
28290 		break;
28291 	case DDI_MODEL_NONE:
28292 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28293 			return (EFAULT);
28294 		}
28295 		break;
28296 	}
28297 
28298 #else /* ! _MULTI_DATAMODEL */
28299 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28300 		return (EFAULT);
28301 	}
28302 #endif /* _MULTI_DATAMODEL */
28303 
28304 	bzero(cdb, sizeof (cdb));
28305 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
28306 		/* Read command supported by 1st generation atapi drives */
28307 		cdb[0] = SCMD_READ_CDD4;
28308 	} else {
28309 		/* Universal CD Access Command */
28310 		cdb[0] = SCMD_READ_CD;
28311 	}
28312 
28313 	/*
28314 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
28315 	 */
28316 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
28317 
28318 	/* set the start address */
28319 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
28320 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
28321 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28322 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
28323 
28324 	/* set the transfer length */
28325 	nblocks = mode2->cdread_buflen / 2336;
28326 	cdb[6] = (uchar_t)(nblocks >> 16);
28327 	cdb[7] = (uchar_t)(nblocks >> 8);
28328 	cdb[8] = (uchar_t)nblocks;
28329 
28330 	/* set the filter bits */
28331 	cdb[9] = CDROM_READ_CD_USERDATA;
28332 
28333 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28334 	com->uscsi_cdb = (caddr_t)cdb;
28335 	com->uscsi_cdblen = sizeof (cdb);
28336 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28337 	com->uscsi_buflen = mode2->cdread_buflen;
28338 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28339 
28340 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28341 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28342 	kmem_free(com, sizeof (*com));
28343 	return (rval);
28344 }
28345 
28346 
28347 /*
28348  *    Function: sr_read_mode2()
28349  *
28350  * Description: This routine is the driver entry point for handling CD-ROM
28351  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
28352  *		do not support the READ CD (0xBE) command.
28353  *
28354  *   Arguments: dev	- the device 'dev_t'
28355  *		data	- pointer to user provided cd read structure specifying
28356  *			  the lba buffer address and length.
28357  *		flag	- this argument is a pass through to ddi_copyxxx()
28358  *			  directly from the mode argument of ioctl().
28359  *
28360  * Return Code: the code returned by sd_send_scsi_cmd()
28361  *		EFAULT if ddi_copyxxx() fails
28362  *		ENXIO if fail ddi_get_soft_state
28363  *		EINVAL if data pointer is NULL
28364  *		EIO if fail to reset block size
28365  *		EAGAIN if commands are in progress in the driver
28366  */
28367 
28368 static int
28369 sr_read_mode2(dev_t dev, caddr_t data, int flag)
28370 {
28371 	struct sd_lun		*un;
28372 	struct cdrom_read	mode2_struct;
28373 	struct cdrom_read	*mode2 = &mode2_struct;
28374 	int			rval;
28375 	uint32_t		restore_blksize;
28376 	struct uscsi_cmd	*com;
28377 	uchar_t			cdb[CDB_GROUP0];
28378 	int			nblocks;
28379 
28380 #ifdef _MULTI_DATAMODEL
28381 	/* To support ILP32 applications in an LP64 world */
28382 	struct cdrom_read32	cdrom_read32;
28383 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
28384 #endif /* _MULTI_DATAMODEL */
28385 
28386 	if (data == NULL) {
28387 		return (EINVAL);
28388 	}
28389 
28390 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28391 	    (un->un_state == SD_STATE_OFFLINE)) {
28392 		return (ENXIO);
28393 	}
28394 
28395 	/*
28396 	 * Because this routine will update the device and driver block size
28397 	 * being used we want to make sure there are no commands in progress.
28398 	 * If commands are in progress the user will have to try again.
28399 	 *
28400 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
28401 	 * in sdioctl to protect commands from sdioctl through to the top of
28402 	 * sd_uscsi_strategy. See sdioctl for details.
28403 	 */
28404 	mutex_enter(SD_MUTEX(un));
28405 	if (un->un_ncmds_in_driver != 1) {
28406 		mutex_exit(SD_MUTEX(un));
28407 		return (EAGAIN);
28408 	}
28409 	mutex_exit(SD_MUTEX(un));
28410 
28411 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28412 	    "sd_read_mode2: entry: un:0x%p\n", un);
28413 
28414 #ifdef _MULTI_DATAMODEL
28415 	switch (ddi_model_convert_from(flag & FMODELS)) {
28416 	case DDI_MODEL_ILP32:
28417 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
28418 			return (EFAULT);
28419 		}
28420 		/* Convert the ILP32 uscsi data from the application to LP64 */
28421 		cdrom_read32tocdrom_read(cdrd32, mode2);
28422 		break;
28423 	case DDI_MODEL_NONE:
28424 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
28425 			return (EFAULT);
28426 		}
28427 		break;
28428 	}
28429 #else /* ! _MULTI_DATAMODEL */
28430 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
28431 		return (EFAULT);
28432 	}
28433 #endif /* _MULTI_DATAMODEL */
28434 
28435 	/* Store the current target block size for restoration later */
28436 	restore_blksize = un->un_tgt_blocksize;
28437 
28438 	/* Change the device and soft state target block size to 2336 */
28439 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
28440 		rval = EIO;
28441 		goto done;
28442 	}
28443 
28444 
28445 	bzero(cdb, sizeof (cdb));
28446 
28447 	/* set READ operation */
28448 	cdb[0] = SCMD_READ;
28449 
28450 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
28451 	mode2->cdread_lba >>= 2;
28452 
28453 	/* set the start address */
28454 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
28455 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
28456 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
28457 
28458 	/* set the transfer length */
28459 	nblocks = mode2->cdread_buflen / 2336;
28460 	cdb[4] = (uchar_t)nblocks & 0xFF;
28461 
28462 	/* build command */
28463 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28464 	com->uscsi_cdb = (caddr_t)cdb;
28465 	com->uscsi_cdblen = sizeof (cdb);
28466 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
28467 	com->uscsi_buflen = mode2->cdread_buflen;
28468 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28469 
28470 	/*
28471 	 * Issue SCSI command with user space address for read buffer.
28472 	 *
28473 	 * This sends the command through main channel in the driver.
28474 	 *
28475 	 * Since this is accessed via an IOCTL call, we go through the
28476 	 * standard path, so that if the device was powered down, then
28477 	 * it would be 'awakened' to handle the command.
28478 	 */
28479 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28480 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28481 
28482 	kmem_free(com, sizeof (*com));
28483 
28484 	/* Restore the device and soft state target block size */
28485 	if (sr_sector_mode(dev, restore_blksize) != 0) {
28486 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28487 		    "can't do switch back to mode 1\n");
28488 		/*
28489 		 * If sd_send_scsi_READ succeeded we still need to report
28490 		 * an error because we failed to reset the block size
28491 		 */
28492 		if (rval == 0) {
28493 			rval = EIO;
28494 		}
28495 	}
28496 
28497 done:
28498 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
28499 	    "sd_read_mode2: exit: un:0x%p\n", un);
28500 
28501 	return (rval);
28502 }
28503 
28504 
28505 /*
28506  *    Function: sr_sector_mode()
28507  *
28508  * Description: This utility function is used by sr_read_mode2 to set the target
28509  *		block size based on the user specified size. This is a legacy
28510  *		implementation based upon a vendor specific mode page
28511  *
28512  *   Arguments: dev	- the device 'dev_t'
28513  *		data	- flag indicating if block size is being set to 2336 or
28514  *			  512.
28515  *
28516  * Return Code: the code returned by sd_send_scsi_cmd()
28517  *		EFAULT if ddi_copyxxx() fails
28518  *		ENXIO if fail ddi_get_soft_state
28519  *		EINVAL if data pointer is NULL
28520  */
28521 
28522 static int
28523 sr_sector_mode(dev_t dev, uint32_t blksize)
28524 {
28525 	struct sd_lun	*un;
28526 	uchar_t		*sense;
28527 	uchar_t		*select;
28528 	int		rval;
28529 
28530 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28531 	    (un->un_state == SD_STATE_OFFLINE)) {
28532 		return (ENXIO);
28533 	}
28534 
28535 	sense = kmem_zalloc(20, KM_SLEEP);
28536 
28537 	/* Note: This is a vendor specific mode page (0x81) */
28538 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81,
28539 	    SD_PATH_STANDARD)) != 0) {
28540 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28541 		    "sr_sector_mode: Mode Sense failed\n");
28542 		kmem_free(sense, 20);
28543 		return (rval);
28544 	}
28545 	select = kmem_zalloc(20, KM_SLEEP);
28546 	select[3] = 0x08;
28547 	select[10] = ((blksize >> 8) & 0xff);
28548 	select[11] = (blksize & 0xff);
28549 	select[12] = 0x01;
28550 	select[13] = 0x06;
28551 	select[14] = sense[14];
28552 	select[15] = sense[15];
28553 	if (blksize == SD_MODE2_BLKSIZE) {
28554 		select[14] |= 0x01;
28555 	}
28556 
28557 	if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20,
28558 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
28559 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28560 		    "sr_sector_mode: Mode Select failed\n");
28561 	} else {
28562 		/*
28563 		 * Only update the softstate block size if we successfully
28564 		 * changed the device block mode.
28565 		 */
28566 		mutex_enter(SD_MUTEX(un));
28567 		sd_update_block_info(un, blksize, 0);
28568 		mutex_exit(SD_MUTEX(un));
28569 	}
28570 	kmem_free(sense, 20);
28571 	kmem_free(select, 20);
28572 	return (rval);
28573 }
28574 
28575 
28576 /*
28577  *    Function: sr_read_cdda()
28578  *
28579  * Description: This routine is the driver entry point for handling CD-ROM
28580  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
28581  *		the target supports CDDA these requests are handled via a vendor
28582  *		specific command (0xD8) If the target does not support CDDA
28583  *		these requests are handled via the READ CD command (0xBE).
28584  *
28585  *   Arguments: dev	- the device 'dev_t'
28586  *		data	- pointer to user provided CD-DA structure specifying
28587  *			  the track starting address, transfer length, and
28588  *			  subcode options.
28589  *		flag	- this argument is a pass through to ddi_copyxxx()
28590  *			  directly from the mode argument of ioctl().
28591  *
28592  * Return Code: the code returned by sd_send_scsi_cmd()
28593  *		EFAULT if ddi_copyxxx() fails
28594  *		ENXIO if fail ddi_get_soft_state
28595  *		EINVAL if invalid arguments are provided
28596  *		ENOTTY
28597  */
28598 
28599 static int
28600 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28601 {
28602 	struct sd_lun			*un;
28603 	struct uscsi_cmd		*com;
28604 	struct cdrom_cdda		*cdda;
28605 	int				rval;
28606 	size_t				buflen;
28607 	char				cdb[CDB_GROUP5];
28608 
28609 #ifdef _MULTI_DATAMODEL
28610 	/* To support ILP32 applications in an LP64 world */
28611 	struct cdrom_cdda32	cdrom_cdda32;
28612 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28613 #endif /* _MULTI_DATAMODEL */
28614 
28615 	if (data == NULL) {
28616 		return (EINVAL);
28617 	}
28618 
28619 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28620 		return (ENXIO);
28621 	}
28622 
28623 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28624 
28625 #ifdef _MULTI_DATAMODEL
28626 	switch (ddi_model_convert_from(flag & FMODELS)) {
28627 	case DDI_MODEL_ILP32:
28628 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28629 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28630 			    "sr_read_cdda: ddi_copyin Failed\n");
28631 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28632 			return (EFAULT);
28633 		}
28634 		/* Convert the ILP32 uscsi data from the application to LP64 */
28635 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28636 		break;
28637 	case DDI_MODEL_NONE:
28638 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28639 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28640 			    "sr_read_cdda: ddi_copyin Failed\n");
28641 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28642 			return (EFAULT);
28643 		}
28644 		break;
28645 	}
28646 #else /* ! _MULTI_DATAMODEL */
28647 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28648 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28649 		    "sr_read_cdda: ddi_copyin Failed\n");
28650 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28651 		return (EFAULT);
28652 	}
28653 #endif /* _MULTI_DATAMODEL */
28654 
28655 	/*
28656 	 * Since MMC-2 expects max 3 bytes for length, check if the
28657 	 * length input is greater than 3 bytes
28658 	 */
28659 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28660 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28661 		    "cdrom transfer length too large: %d (limit %d)\n",
28662 		    cdda->cdda_length, 0xFFFFFF);
28663 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28664 		return (EINVAL);
28665 	}
28666 
28667 	switch (cdda->cdda_subcode) {
28668 	case CDROM_DA_NO_SUBCODE:
28669 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28670 		break;
28671 	case CDROM_DA_SUBQ:
28672 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28673 		break;
28674 	case CDROM_DA_ALL_SUBCODE:
28675 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28676 		break;
28677 	case CDROM_DA_SUBCODE_ONLY:
28678 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28679 		break;
28680 	default:
28681 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28682 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28683 		    cdda->cdda_subcode);
28684 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28685 		return (EINVAL);
28686 	}
28687 
28688 	/* Build and send the command */
28689 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28690 	bzero(cdb, CDB_GROUP5);
28691 
28692 	if (un->un_f_cfg_cdda == TRUE) {
28693 		cdb[0] = (char)SCMD_READ_CD;
28694 		cdb[1] = 0x04;
28695 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28696 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28697 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28698 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28699 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28700 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28701 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28702 		cdb[9] = 0x10;
28703 		switch (cdda->cdda_subcode) {
28704 		case CDROM_DA_NO_SUBCODE :
28705 			cdb[10] = 0x0;
28706 			break;
28707 		case CDROM_DA_SUBQ :
28708 			cdb[10] = 0x2;
28709 			break;
28710 		case CDROM_DA_ALL_SUBCODE :
28711 			cdb[10] = 0x1;
28712 			break;
28713 		case CDROM_DA_SUBCODE_ONLY :
28714 			/* FALLTHROUGH */
28715 		default :
28716 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28717 			kmem_free(com, sizeof (*com));
28718 			return (ENOTTY);
28719 		}
28720 	} else {
28721 		cdb[0] = (char)SCMD_READ_CDDA;
28722 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28723 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28724 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28725 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28726 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28727 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28728 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28729 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28730 		cdb[10] = cdda->cdda_subcode;
28731 	}
28732 
28733 	com->uscsi_cdb = cdb;
28734 	com->uscsi_cdblen = CDB_GROUP5;
28735 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28736 	com->uscsi_buflen = buflen;
28737 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28738 
28739 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28740 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28741 
28742 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28743 	kmem_free(com, sizeof (*com));
28744 	return (rval);
28745 }
28746 
28747 
28748 /*
28749  *    Function: sr_read_cdxa()
28750  *
28751  * Description: This routine is the driver entry point for handling CD-ROM
28752  *		ioctl requests to return CD-XA (Extended Architecture) data.
28753  *		(CDROMCDXA).
28754  *
28755  *   Arguments: dev	- the device 'dev_t'
28756  *		data	- pointer to user provided CD-XA structure specifying
28757  *			  the data starting address, transfer length, and format
28758  *		flag	- this argument is a pass through to ddi_copyxxx()
28759  *			  directly from the mode argument of ioctl().
28760  *
28761  * Return Code: the code returned by sd_send_scsi_cmd()
28762  *		EFAULT if ddi_copyxxx() fails
28763  *		ENXIO if fail ddi_get_soft_state
28764  *		EINVAL if data pointer is NULL
28765  */
28766 
28767 static int
28768 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28769 {
28770 	struct sd_lun		*un;
28771 	struct uscsi_cmd	*com;
28772 	struct cdrom_cdxa	*cdxa;
28773 	int			rval;
28774 	size_t			buflen;
28775 	char			cdb[CDB_GROUP5];
28776 	uchar_t			read_flags;
28777 
28778 #ifdef _MULTI_DATAMODEL
28779 	/* To support ILP32 applications in an LP64 world */
28780 	struct cdrom_cdxa32		cdrom_cdxa32;
28781 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28782 #endif /* _MULTI_DATAMODEL */
28783 
28784 	if (data == NULL) {
28785 		return (EINVAL);
28786 	}
28787 
28788 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28789 		return (ENXIO);
28790 	}
28791 
28792 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28793 
28794 #ifdef _MULTI_DATAMODEL
28795 	switch (ddi_model_convert_from(flag & FMODELS)) {
28796 	case DDI_MODEL_ILP32:
28797 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28798 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28799 			return (EFAULT);
28800 		}
28801 		/*
28802 		 * Convert the ILP32 uscsi data from the
28803 		 * application to LP64 for internal use.
28804 		 */
28805 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28806 		break;
28807 	case DDI_MODEL_NONE:
28808 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28809 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28810 			return (EFAULT);
28811 		}
28812 		break;
28813 	}
28814 #else /* ! _MULTI_DATAMODEL */
28815 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28816 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28817 		return (EFAULT);
28818 	}
28819 #endif /* _MULTI_DATAMODEL */
28820 
28821 	/*
28822 	 * Since MMC-2 expects max 3 bytes for length, check if the
28823 	 * length input is greater than 3 bytes
28824 	 */
28825 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28826 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28827 		    "cdrom transfer length too large: %d (limit %d)\n",
28828 		    cdxa->cdxa_length, 0xFFFFFF);
28829 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28830 		return (EINVAL);
28831 	}
28832 
28833 	switch (cdxa->cdxa_format) {
28834 	case CDROM_XA_DATA:
28835 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28836 		read_flags = 0x10;
28837 		break;
28838 	case CDROM_XA_SECTOR_DATA:
28839 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28840 		read_flags = 0xf8;
28841 		break;
28842 	case CDROM_XA_DATA_W_ERROR:
28843 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28844 		read_flags = 0xfc;
28845 		break;
28846 	default:
28847 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28848 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
28849 		    cdxa->cdxa_format);
28850 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28851 		return (EINVAL);
28852 	}
28853 
28854 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28855 	bzero(cdb, CDB_GROUP5);
28856 	if (un->un_f_mmc_cap == TRUE) {
28857 		cdb[0] = (char)SCMD_READ_CD;
28858 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28859 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28860 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28861 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28862 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28863 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28864 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28865 		cdb[9] = (char)read_flags;
28866 	} else {
28867 		/*
28868 		 * Note: A vendor specific command (0xDB) is being used her to
28869 		 * request a read of all subcodes.
28870 		 */
28871 		cdb[0] = (char)SCMD_READ_CDXA;
28872 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28873 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28874 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28875 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28876 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28877 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28878 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28879 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28880 		cdb[10] = cdxa->cdxa_format;
28881 	}
28882 	com->uscsi_cdb	   = cdb;
28883 	com->uscsi_cdblen  = CDB_GROUP5;
28884 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28885 	com->uscsi_buflen  = buflen;
28886 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28887 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28888 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28889 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28890 	kmem_free(com, sizeof (*com));
28891 	return (rval);
28892 }
28893 
28894 
28895 /*
28896  *    Function: sr_eject()
28897  *
28898  * Description: This routine is the driver entry point for handling CD-ROM
28899  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28900  *
28901  *   Arguments: dev	- the device 'dev_t'
28902  *
28903  * Return Code: the code returned by sd_send_scsi_cmd()
28904  */
28905 
28906 static int
28907 sr_eject(dev_t dev)
28908 {
28909 	struct sd_lun	*un;
28910 	int		rval;
28911 
28912 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28913 	    (un->un_state == SD_STATE_OFFLINE)) {
28914 		return (ENXIO);
28915 	}
28916 	if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
28917 	    SD_PATH_STANDARD)) != 0) {
28918 		return (rval);
28919 	}
28920 
28921 	rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT,
28922 	    SD_PATH_STANDARD);
28923 
28924 	if (rval == 0) {
28925 		mutex_enter(SD_MUTEX(un));
28926 		sr_ejected(un);
28927 		un->un_mediastate = DKIO_EJECTED;
28928 		cv_broadcast(&un->un_state_cv);
28929 		mutex_exit(SD_MUTEX(un));
28930 	}
28931 	return (rval);
28932 }
28933 
28934 
28935 /*
28936  *    Function: sr_ejected()
28937  *
28938  * Description: This routine updates the soft state structure to invalidate the
28939  *		geometry information after the media has been ejected or a
28940  *		media eject has been detected.
28941  *
28942  *   Arguments: un - driver soft state (unit) structure
28943  */
28944 
28945 static void
28946 sr_ejected(struct sd_lun *un)
28947 {
28948 	struct sd_errstats *stp;
28949 
28950 	ASSERT(un != NULL);
28951 	ASSERT(mutex_owned(SD_MUTEX(un)));
28952 
28953 	un->un_f_blockcount_is_valid	= FALSE;
28954 	un->un_f_tgt_blocksize_is_valid	= FALSE;
28955 	un->un_f_geometry_is_valid	= FALSE;
28956 
28957 	if (un->un_errstats != NULL) {
28958 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
28959 		stp->sd_capacity.value.ui64 = 0;
28960 	}
28961 }
28962 
28963 
28964 /*
28965  *    Function: sr_check_wp()
28966  *
28967  * Description: This routine checks the write protection of a removable media
28968  *		disk via the write protect bit of the Mode Page Header device
28969  *		specific field.  This routine has been implemented to use the
28970  *		error recovery mode page for all device types.
28971  *		Note: In the future use a sd_send_scsi_MODE_SENSE() routine
28972  *
28973  *   Arguments: dev		- the device 'dev_t'
28974  *
28975  * Return Code: int indicating if the device is write protected (1) or not (0)
28976  *
28977  *     Context: Kernel thread.
28978  *
28979  */
28980 
28981 static int
28982 sr_check_wp(dev_t dev)
28983 {
28984 	struct sd_lun	*un;
28985 	uchar_t		device_specific;
28986 	uchar_t		*sense;
28987 	int		hdrlen;
28988 	int		rval;
28989 	int		retry_flag = FALSE;
28990 
28991 	/*
28992 	 * Note: The return codes for this routine should be reworked to
28993 	 * properly handle the case of a NULL softstate.
28994 	 */
28995 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28996 		return (FALSE);
28997 	}
28998 
28999 	if (un->un_f_cfg_is_atapi == TRUE) {
29000 		retry_flag = TRUE;
29001 	}
29002 
29003 retry:
29004 	if (un->un_f_cfg_is_atapi == TRUE) {
29005 		/*
29006 		 * The mode page contents are not required; set the allocation
29007 		 * length for the mode page header only
29008 		 */
29009 		hdrlen = MODE_HEADER_LENGTH_GRP2;
29010 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29011 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen,
29012 		    MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
29013 		device_specific =
29014 		    ((struct mode_header_grp2 *)sense)->device_specific;
29015 	} else {
29016 		hdrlen = MODE_HEADER_LENGTH;
29017 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
29018 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen,
29019 		    MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
29020 		device_specific =
29021 		    ((struct mode_header *)sense)->device_specific;
29022 	}
29023 
29024 	if (rval != 0) {
29025 		if ((un->un_f_cfg_is_atapi == TRUE) && (retry_flag)) {
29026 			/*
29027 			 * For an Atapi Zip drive, observed the drive
29028 			 * reporting check condition for the first attempt.
29029 			 * Sense data indicating power on or bus device/reset.
29030 			 * Hence in case of failure need to try at least once
29031 			 * for Atapi devices.
29032 			 */
29033 			retry_flag = FALSE;
29034 			kmem_free(sense, hdrlen);
29035 			goto retry;
29036 		} else {
29037 			/*
29038 			 * Write protect mode sense failed; not all disks
29039 			 * understand this query. Return FALSE assuming that
29040 			 * these devices are not writable.
29041 			 */
29042 			rval = FALSE;
29043 		}
29044 	} else {
29045 		if (device_specific & WRITE_PROTECT) {
29046 			rval = TRUE;
29047 		} else {
29048 			rval = FALSE;
29049 		}
29050 	}
29051 	kmem_free(sense, hdrlen);
29052 	return (rval);
29053 }
29054 
29055 
29056 /*
29057  *    Function: sr_volume_ctrl()
29058  *
29059  * Description: This routine is the driver entry point for handling CD-ROM
29060  *		audio output volume ioctl requests. (CDROMVOLCTRL)
29061  *
29062  *   Arguments: dev	- the device 'dev_t'
29063  *		data	- pointer to user audio volume control structure
29064  *		flag	- this argument is a pass through to ddi_copyxxx()
29065  *			  directly from the mode argument of ioctl().
29066  *
29067  * Return Code: the code returned by sd_send_scsi_cmd()
29068  *		EFAULT if ddi_copyxxx() fails
29069  *		ENXIO if fail ddi_get_soft_state
29070  *		EINVAL if data pointer is NULL
29071  *
29072  */
29073 
29074 static int
29075 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
29076 {
29077 	struct sd_lun		*un;
29078 	struct cdrom_volctrl    volume;
29079 	struct cdrom_volctrl    *vol = &volume;
29080 	uchar_t			*sense_page;
29081 	uchar_t			*select_page;
29082 	uchar_t			*sense;
29083 	uchar_t			*select;
29084 	int			sense_buflen;
29085 	int			select_buflen;
29086 	int			rval;
29087 
29088 	if (data == NULL) {
29089 		return (EINVAL);
29090 	}
29091 
29092 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29093 	    (un->un_state == SD_STATE_OFFLINE)) {
29094 		return (ENXIO);
29095 	}
29096 
29097 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
29098 		return (EFAULT);
29099 	}
29100 
29101 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29102 		struct mode_header_grp2		*sense_mhp;
29103 		struct mode_header_grp2		*select_mhp;
29104 		int				bd_len;
29105 
29106 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
29107 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
29108 		    MODEPAGE_AUDIO_CTRL_LEN;
29109 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29110 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29111 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
29112 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29113 		    SD_PATH_STANDARD)) != 0) {
29114 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
29115 			    "sr_volume_ctrl: Mode Sense Failed\n");
29116 			kmem_free(sense, sense_buflen);
29117 			kmem_free(select, select_buflen);
29118 			return (rval);
29119 		}
29120 		sense_mhp = (struct mode_header_grp2 *)sense;
29121 		select_mhp = (struct mode_header_grp2 *)select;
29122 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
29123 		    sense_mhp->bdesc_length_lo;
29124 		if (bd_len > MODE_BLK_DESC_LENGTH) {
29125 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29126 			    "sr_volume_ctrl: Mode Sense returned invalid "
29127 			    "block descriptor length\n");
29128 			kmem_free(sense, sense_buflen);
29129 			kmem_free(select, select_buflen);
29130 			return (EIO);
29131 		}
29132 		sense_page = (uchar_t *)
29133 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
29134 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
29135 		select_mhp->length_msb = 0;
29136 		select_mhp->length_lsb = 0;
29137 		select_mhp->bdesc_length_hi = 0;
29138 		select_mhp->bdesc_length_lo = 0;
29139 	} else {
29140 		struct mode_header		*sense_mhp, *select_mhp;
29141 
29142 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29143 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
29144 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
29145 		select = kmem_zalloc(select_buflen, KM_SLEEP);
29146 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
29147 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
29148 		    SD_PATH_STANDARD)) != 0) {
29149 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29150 			    "sr_volume_ctrl: Mode Sense Failed\n");
29151 			kmem_free(sense, sense_buflen);
29152 			kmem_free(select, select_buflen);
29153 			return (rval);
29154 		}
29155 		sense_mhp  = (struct mode_header *)sense;
29156 		select_mhp = (struct mode_header *)select;
29157 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
29158 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29159 			    "sr_volume_ctrl: Mode Sense returned invalid "
29160 			    "block descriptor length\n");
29161 			kmem_free(sense, sense_buflen);
29162 			kmem_free(select, select_buflen);
29163 			return (EIO);
29164 		}
29165 		sense_page = (uchar_t *)
29166 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
29167 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
29168 		select_mhp->length = 0;
29169 		select_mhp->bdesc_length = 0;
29170 	}
29171 	/*
29172 	 * Note: An audio control data structure could be created and overlayed
29173 	 * on the following in place of the array indexing method implemented.
29174 	 */
29175 
29176 	/* Build the select data for the user volume data */
29177 	select_page[0] = MODEPAGE_AUDIO_CTRL;
29178 	select_page[1] = 0xE;
29179 	/* Set the immediate bit */
29180 	select_page[2] = 0x04;
29181 	/* Zero out reserved fields */
29182 	select_page[3] = 0x00;
29183 	select_page[4] = 0x00;
29184 	/* Return sense data for fields not to be modified */
29185 	select_page[5] = sense_page[5];
29186 	select_page[6] = sense_page[6];
29187 	select_page[7] = sense_page[7];
29188 	/* Set the user specified volume levels for channel 0 and 1 */
29189 	select_page[8] = 0x01;
29190 	select_page[9] = vol->channel0;
29191 	select_page[10] = 0x02;
29192 	select_page[11] = vol->channel1;
29193 	/* Channel 2 and 3 are currently unsupported so return the sense data */
29194 	select_page[12] = sense_page[12];
29195 	select_page[13] = sense_page[13];
29196 	select_page[14] = sense_page[14];
29197 	select_page[15] = sense_page[15];
29198 
29199 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
29200 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select,
29201 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29202 	} else {
29203 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
29204 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
29205 	}
29206 
29207 	kmem_free(sense, sense_buflen);
29208 	kmem_free(select, select_buflen);
29209 	return (rval);
29210 }
29211 
29212 
29213 /*
29214  *    Function: sr_read_sony_session_offset()
29215  *
29216  * Description: This routine is the driver entry point for handling CD-ROM
29217  *		ioctl requests for session offset information. (CDROMREADOFFSET)
29218  *		The address of the first track in the last session of a
29219  *		multi-session CD-ROM is returned
29220  *
29221  *		Note: This routine uses a vendor specific key value in the
29222  *		command control field without implementing any vendor check here
29223  *		or in the ioctl routine.
29224  *
29225  *   Arguments: dev	- the device 'dev_t'
29226  *		data	- pointer to an int to hold the requested address
29227  *		flag	- this argument is a pass through to ddi_copyxxx()
29228  *			  directly from the mode argument of ioctl().
29229  *
29230  * Return Code: the code returned by sd_send_scsi_cmd()
29231  *		EFAULT if ddi_copyxxx() fails
29232  *		ENXIO if fail ddi_get_soft_state
29233  *		EINVAL if data pointer is NULL
29234  */
29235 
29236 static int
29237 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
29238 {
29239 	struct sd_lun		*un;
29240 	struct uscsi_cmd	*com;
29241 	caddr_t			buffer;
29242 	char			cdb[CDB_GROUP1];
29243 	int			session_offset = 0;
29244 	int			rval;
29245 
29246 	if (data == NULL) {
29247 		return (EINVAL);
29248 	}
29249 
29250 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
29251 	    (un->un_state == SD_STATE_OFFLINE)) {
29252 		return (ENXIO);
29253 	}
29254 
29255 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
29256 	bzero(cdb, CDB_GROUP1);
29257 	cdb[0] = SCMD_READ_TOC;
29258 	/*
29259 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
29260 	 * (4 byte TOC response header + 8 byte response data)
29261 	 */
29262 	cdb[8] = SONY_SESSION_OFFSET_LEN;
29263 	/* Byte 9 is the control byte. A vendor specific value is used */
29264 	cdb[9] = SONY_SESSION_OFFSET_KEY;
29265 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
29266 	com->uscsi_cdb = cdb;
29267 	com->uscsi_cdblen = CDB_GROUP1;
29268 	com->uscsi_bufaddr = buffer;
29269 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
29270 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
29271 
29272 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
29273 	    UIO_SYSSPACE, SD_PATH_STANDARD);
29274 	if (rval != 0) {
29275 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29276 		kmem_free(com, sizeof (*com));
29277 		return (rval);
29278 	}
29279 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
29280 		session_offset =
29281 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
29282 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
29283 		/*
29284 		 * Offset returned offset in current lbasize block's. Convert to
29285 		 * 2k block's to return to the user
29286 		 */
29287 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
29288 			session_offset >>= 2;
29289 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
29290 			session_offset >>= 1;
29291 		}
29292 	}
29293 
29294 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
29295 		rval = EFAULT;
29296 	}
29297 
29298 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
29299 	kmem_free(com, sizeof (*com));
29300 	return (rval);
29301 }
29302 
29303 
29304 /*
29305  *    Function: sd_wm_cache_constructor()
29306  *
29307  * Description: Cache Constructor for the wmap cache for the read/modify/write
29308  * 		devices.
29309  *
29310  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29311  *		un	- sd_lun structure for the device.
29312  *		flag	- the km flags passed to constructor
29313  *
29314  * Return Code: 0 on success.
29315  *		-1 on failure.
29316  */
29317 
29318 /*ARGSUSED*/
29319 static int
29320 sd_wm_cache_constructor(void *wm, void *un, int flags)
29321 {
29322 	bzero(wm, sizeof (struct sd_w_map));
29323 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
29324 	return (0);
29325 }
29326 
29327 
29328 /*
29329  *    Function: sd_wm_cache_destructor()
29330  *
29331  * Description: Cache destructor for the wmap cache for the read/modify/write
29332  * 		devices.
29333  *
29334  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
29335  *		un	- sd_lun structure for the device.
29336  */
29337 /*ARGSUSED*/
29338 static void
29339 sd_wm_cache_destructor(void *wm, void *un)
29340 {
29341 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
29342 }
29343 
29344 
29345 /*
29346  *    Function: sd_range_lock()
29347  *
29348  * Description: Lock the range of blocks specified as parameter to ensure
29349  *		that read, modify write is atomic and no other i/o writes
29350  *		to the same location. The range is specified in terms
29351  *		of start and end blocks. Block numbers are the actual
29352  *		media block numbers and not system.
29353  *
29354  *   Arguments: un	- sd_lun structure for the device.
29355  *		startb - The starting block number
29356  *		endb - The end block number
29357  *		typ - type of i/o - simple/read_modify_write
29358  *
29359  * Return Code: wm  - pointer to the wmap structure.
29360  *
29361  *     Context: This routine can sleep.
29362  */
29363 
29364 static struct sd_w_map *
29365 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
29366 {
29367 	struct sd_w_map *wmp = NULL;
29368 	struct sd_w_map *sl_wmp = NULL;
29369 	struct sd_w_map *tmp_wmp;
29370 	wm_state state = SD_WM_CHK_LIST;
29371 
29372 
29373 	ASSERT(un != NULL);
29374 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29375 
29376 	mutex_enter(SD_MUTEX(un));
29377 
29378 	while (state != SD_WM_DONE) {
29379 
29380 		switch (state) {
29381 		case SD_WM_CHK_LIST:
29382 			/*
29383 			 * This is the starting state. Check the wmap list
29384 			 * to see if the range is currently available.
29385 			 */
29386 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
29387 				/*
29388 				 * If this is a simple write and no rmw
29389 				 * i/o is pending then try to lock the
29390 				 * range as the range should be available.
29391 				 */
29392 				state = SD_WM_LOCK_RANGE;
29393 			} else {
29394 				tmp_wmp = sd_get_range(un, startb, endb);
29395 				if (tmp_wmp != NULL) {
29396 					if ((wmp != NULL) && ONLIST(un, wmp)) {
29397 						/*
29398 						 * Should not keep onlist wmps
29399 						 * while waiting this macro
29400 						 * will also do wmp = NULL;
29401 						 */
29402 						FREE_ONLIST_WMAP(un, wmp);
29403 					}
29404 					/*
29405 					 * sl_wmp is the wmap on which wait
29406 					 * is done, since the tmp_wmp points
29407 					 * to the inuse wmap, set sl_wmp to
29408 					 * tmp_wmp and change the state to sleep
29409 					 */
29410 					sl_wmp = tmp_wmp;
29411 					state = SD_WM_WAIT_MAP;
29412 				} else {
29413 					state = SD_WM_LOCK_RANGE;
29414 				}
29415 
29416 			}
29417 			break;
29418 
29419 		case SD_WM_LOCK_RANGE:
29420 			ASSERT(un->un_wm_cache);
29421 			/*
29422 			 * The range need to be locked, try to get a wmap.
29423 			 * First attempt it with NO_SLEEP, want to avoid a sleep
29424 			 * if possible as we will have to release the sd mutex
29425 			 * if we have to sleep.
29426 			 */
29427 			if (wmp == NULL)
29428 				wmp = kmem_cache_alloc(un->un_wm_cache,
29429 				    KM_NOSLEEP);
29430 			if (wmp == NULL) {
29431 				mutex_exit(SD_MUTEX(un));
29432 				_NOTE(DATA_READABLE_WITHOUT_LOCK
29433 				    (sd_lun::un_wm_cache))
29434 				wmp = kmem_cache_alloc(un->un_wm_cache,
29435 				    KM_SLEEP);
29436 				mutex_enter(SD_MUTEX(un));
29437 				/*
29438 				 * we released the mutex so recheck and go to
29439 				 * check list state.
29440 				 */
29441 				state = SD_WM_CHK_LIST;
29442 			} else {
29443 				/*
29444 				 * We exit out of state machine since we
29445 				 * have the wmap. Do the housekeeping first.
29446 				 * place the wmap on the wmap list if it is not
29447 				 * on it already and then set the state to done.
29448 				 */
29449 				wmp->wm_start = startb;
29450 				wmp->wm_end = endb;
29451 				wmp->wm_flags = typ | SD_WM_BUSY;
29452 				if (typ & SD_WTYPE_RMW) {
29453 					un->un_rmw_count++;
29454 				}
29455 				/*
29456 				 * If not already on the list then link
29457 				 */
29458 				if (!ONLIST(un, wmp)) {
29459 					wmp->wm_next = un->un_wm;
29460 					wmp->wm_prev = NULL;
29461 					if (wmp->wm_next)
29462 						wmp->wm_next->wm_prev = wmp;
29463 					un->un_wm = wmp;
29464 				}
29465 				state = SD_WM_DONE;
29466 			}
29467 			break;
29468 
29469 		case SD_WM_WAIT_MAP:
29470 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
29471 			/*
29472 			 * Wait is done on sl_wmp, which is set in the
29473 			 * check_list state.
29474 			 */
29475 			sl_wmp->wm_wanted_count++;
29476 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
29477 			sl_wmp->wm_wanted_count--;
29478 			/*
29479 			 * We can reuse the memory from the completed sl_wmp
29480 			 * lock range for our new lock, but only if noone is
29481 			 * waiting for it.
29482 			 */
29483 			ASSERT(!(sl_wmp->wm_flags & SD_WM_BUSY));
29484 			if (sl_wmp->wm_wanted_count == 0) {
29485 				if (wmp != NULL)
29486 					CHK_N_FREEWMP(un, wmp);
29487 				wmp = sl_wmp;
29488 			}
29489 			sl_wmp = NULL;
29490 			/*
29491 			 * After waking up, need to recheck for availability of
29492 			 * range.
29493 			 */
29494 			state = SD_WM_CHK_LIST;
29495 			break;
29496 
29497 		default:
29498 			panic("sd_range_lock: "
29499 			    "Unknown state %d in sd_range_lock", state);
29500 			/*NOTREACHED*/
29501 		} /* switch(state) */
29502 
29503 	} /* while(state != SD_WM_DONE) */
29504 
29505 	mutex_exit(SD_MUTEX(un));
29506 
29507 	ASSERT(wmp != NULL);
29508 
29509 	return (wmp);
29510 }
29511 
29512 
29513 /*
29514  *    Function: sd_get_range()
29515  *
29516  * Description: Find if there any overlapping I/O to this one
29517  *		Returns the write-map of 1st such I/O, NULL otherwise.
29518  *
29519  *   Arguments: un	- sd_lun structure for the device.
29520  *		startb - The starting block number
29521  *		endb - The end block number
29522  *
29523  * Return Code: wm  - pointer to the wmap structure.
29524  */
29525 
29526 static struct sd_w_map *
29527 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
29528 {
29529 	struct sd_w_map *wmp;
29530 
29531 	ASSERT(un != NULL);
29532 
29533 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
29534 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
29535 			continue;
29536 		}
29537 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
29538 			break;
29539 		}
29540 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
29541 			break;
29542 		}
29543 	}
29544 
29545 	return (wmp);
29546 }
29547 
29548 
29549 /*
29550  *    Function: sd_free_inlist_wmap()
29551  *
29552  * Description: Unlink and free a write map struct.
29553  *
29554  *   Arguments: un      - sd_lun structure for the device.
29555  *		wmp	- sd_w_map which needs to be unlinked.
29556  */
29557 
29558 static void
29559 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
29560 {
29561 	ASSERT(un != NULL);
29562 
29563 	if (un->un_wm == wmp) {
29564 		un->un_wm = wmp->wm_next;
29565 	} else {
29566 		wmp->wm_prev->wm_next = wmp->wm_next;
29567 	}
29568 
29569 	if (wmp->wm_next) {
29570 		wmp->wm_next->wm_prev = wmp->wm_prev;
29571 	}
29572 
29573 	wmp->wm_next = wmp->wm_prev = NULL;
29574 
29575 	kmem_cache_free(un->un_wm_cache, wmp);
29576 }
29577 
29578 
29579 /*
29580  *    Function: sd_range_unlock()
29581  *
29582  * Description: Unlock the range locked by wm.
29583  *		Free write map if nobody else is waiting on it.
29584  *
29585  *   Arguments: un      - sd_lun structure for the device.
29586  *              wmp     - sd_w_map which needs to be unlinked.
29587  */
29588 
29589 static void
29590 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
29591 {
29592 	ASSERT(un != NULL);
29593 	ASSERT(wm != NULL);
29594 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29595 
29596 	mutex_enter(SD_MUTEX(un));
29597 
29598 	if (wm->wm_flags & SD_WTYPE_RMW) {
29599 		un->un_rmw_count--;
29600 	}
29601 
29602 	if (wm->wm_wanted_count) {
29603 		wm->wm_flags = 0;
29604 		/*
29605 		 * Broadcast that the wmap is available now.
29606 		 */
29607 		cv_broadcast(&wm->wm_avail);
29608 	} else {
29609 		/*
29610 		 * If no one is waiting on the map, it should be free'ed.
29611 		 */
29612 		sd_free_inlist_wmap(un, wm);
29613 	}
29614 
29615 	mutex_exit(SD_MUTEX(un));
29616 }
29617 
29618 
29619 /*
29620  *    Function: sd_read_modify_write_task
29621  *
29622  * Description: Called from a taskq thread to initiate the write phase of
29623  *		a read-modify-write request.  This is used for targets where
29624  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29625  *
29626  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29627  *
29628  *     Context: Called under taskq thread context.
29629  */
29630 
29631 static void
29632 sd_read_modify_write_task(void *arg)
29633 {
29634 	struct sd_mapblocksize_info	*bsp;
29635 	struct buf	*bp;
29636 	struct sd_xbuf	*xp;
29637 	struct sd_lun	*un;
29638 
29639 	bp = arg;	/* The bp is given in arg */
29640 	ASSERT(bp != NULL);
29641 
29642 	/* Get the pointer to the layer-private data struct */
29643 	xp = SD_GET_XBUF(bp);
29644 	ASSERT(xp != NULL);
29645 	bsp = xp->xb_private;
29646 	ASSERT(bsp != NULL);
29647 
29648 	un = SD_GET_UN(bp);
29649 	ASSERT(un != NULL);
29650 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29651 
29652 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29653 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29654 
29655 	/*
29656 	 * This is the write phase of a read-modify-write request, called
29657 	 * under the context of a taskq thread in response to the completion
29658 	 * of the read portion of the rmw request completing under interrupt
29659 	 * context. The write request must be sent from here down the iostart
29660 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29661 	 * we use the layer index saved in the layer-private data area.
29662 	 */
29663 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29664 
29665 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29666 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29667 }
29668 
29669 
29670 /*
29671  *    Function: sddump_do_read_of_rmw()
29672  *
29673  * Description: This routine will be called from sddump, If sddump is called
29674  *		with an I/O which not aligned on device blocksize boundary
29675  *		then the write has to be converted to read-modify-write.
29676  *		Do the read part here in order to keep sddump simple.
29677  *		Note - That the sd_mutex is held across the call to this
29678  *		routine.
29679  *
29680  *   Arguments: un	- sd_lun
29681  *		blkno	- block number in terms of media block size.
29682  *		nblk	- number of blocks.
29683  *		bpp	- pointer to pointer to the buf structure. On return
29684  *			from this function, *bpp points to the valid buffer
29685  *			to which the write has to be done.
29686  *
29687  * Return Code: 0 for success or errno-type return code
29688  */
29689 
29690 static int
29691 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29692 	struct buf **bpp)
29693 {
29694 	int err;
29695 	int i;
29696 	int rval;
29697 	struct buf *bp;
29698 	struct scsi_pkt *pkt = NULL;
29699 	uint32_t target_blocksize;
29700 
29701 	ASSERT(un != NULL);
29702 	ASSERT(mutex_owned(SD_MUTEX(un)));
29703 
29704 	target_blocksize = un->un_tgt_blocksize;
29705 
29706 	mutex_exit(SD_MUTEX(un));
29707 
29708 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29709 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29710 	if (bp == NULL) {
29711 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29712 		    "no resources for dumping; giving up");
29713 		err = ENOMEM;
29714 		goto done;
29715 	}
29716 
29717 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29718 	    blkno, nblk);
29719 	if (rval != 0) {
29720 		scsi_free_consistent_buf(bp);
29721 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29722 		    "no resources for dumping; giving up");
29723 		err = ENOMEM;
29724 		goto done;
29725 	}
29726 
29727 	pkt->pkt_flags |= FLAG_NOINTR;
29728 
29729 	err = EIO;
29730 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29731 
29732 		/*
29733 		 * Scsi_poll returns 0 (success) if the command completes and
29734 		 * the status block is STATUS_GOOD.  We should only check
29735 		 * errors if this condition is not true.  Even then we should
29736 		 * send our own request sense packet only if we have a check
29737 		 * condition and auto request sense has not been performed by
29738 		 * the hba.
29739 		 */
29740 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29741 
29742 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29743 			err = 0;
29744 			break;
29745 		}
29746 
29747 		/*
29748 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29749 		 * no need to read RQS data.
29750 		 */
29751 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29752 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29753 			    "Device is gone\n");
29754 			break;
29755 		}
29756 
29757 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29758 			SD_INFO(SD_LOG_DUMP, un,
29759 			    "sddump: read failed with CHECK, try # %d\n", i);
29760 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29761 				(void) sd_send_polled_RQS(un);
29762 			}
29763 
29764 			continue;
29765 		}
29766 
29767 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29768 			int reset_retval = 0;
29769 
29770 			SD_INFO(SD_LOG_DUMP, un,
29771 			    "sddump: read failed with BUSY, try # %d\n", i);
29772 
29773 			if (un->un_f_lun_reset_enabled == TRUE) {
29774 				reset_retval = scsi_reset(SD_ADDRESS(un),
29775 				    RESET_LUN);
29776 			}
29777 			if (reset_retval == 0) {
29778 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29779 			}
29780 			(void) sd_send_polled_RQS(un);
29781 
29782 		} else {
29783 			SD_INFO(SD_LOG_DUMP, un,
29784 			    "sddump: read failed with 0x%x, try # %d\n",
29785 			    SD_GET_PKT_STATUS(pkt), i);
29786 			mutex_enter(SD_MUTEX(un));
29787 			sd_reset_target(un, pkt);
29788 			mutex_exit(SD_MUTEX(un));
29789 		}
29790 
29791 		/*
29792 		 * If we are not getting anywhere with lun/target resets,
29793 		 * let's reset the bus.
29794 		 */
29795 		if (i > SD_NDUMP_RETRIES/2) {
29796 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29797 			(void) sd_send_polled_RQS(un);
29798 		}
29799 
29800 	}
29801 	scsi_destroy_pkt(pkt);
29802 
29803 	if (err != 0) {
29804 		scsi_free_consistent_buf(bp);
29805 		*bpp = NULL;
29806 	} else {
29807 		*bpp = bp;
29808 	}
29809 
29810 done:
29811 	mutex_enter(SD_MUTEX(un));
29812 	return (err);
29813 }
29814 
29815 
29816 /*
29817  *    Function: sd_failfast_flushq
29818  *
29819  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29820  *		in b_flags and move them onto the failfast queue, then kick
29821  *		off a thread to return all bp's on the failfast queue to
29822  *		their owners with an error set.
29823  *
29824  *   Arguments: un - pointer to the soft state struct for the instance.
29825  *
29826  *     Context: may execute in interrupt context.
29827  */
29828 
29829 static void
29830 sd_failfast_flushq(struct sd_lun *un)
29831 {
29832 	struct buf *bp;
29833 	struct buf *next_waitq_bp;
29834 	struct buf *prev_waitq_bp = NULL;
29835 
29836 	ASSERT(un != NULL);
29837 	ASSERT(mutex_owned(SD_MUTEX(un)));
29838 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29839 	ASSERT(un->un_failfast_bp == NULL);
29840 
29841 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29842 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
29843 
29844 	/*
29845 	 * Check if we should flush all bufs when entering failfast state, or
29846 	 * just those with B_FAILFAST set.
29847 	 */
29848 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29849 		/*
29850 		 * Move *all* bp's on the wait queue to the failfast flush
29851 		 * queue, including those that do NOT have B_FAILFAST set.
29852 		 */
29853 		if (un->un_failfast_headp == NULL) {
29854 			ASSERT(un->un_failfast_tailp == NULL);
29855 			un->un_failfast_headp = un->un_waitq_headp;
29856 		} else {
29857 			ASSERT(un->un_failfast_tailp != NULL);
29858 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29859 		}
29860 
29861 		un->un_failfast_tailp = un->un_waitq_tailp;
29862 
29863 		/* update kstat for each bp moved out of the waitq */
29864 		for (bp = un->un_waitq_headp; bp != NULL; bp = bp->av_forw) {
29865 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29866 		}
29867 
29868 		/* empty the waitq */
29869 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
29870 
29871 	} else {
29872 		/*
29873 		 * Go thru the wait queue, pick off all entries with
29874 		 * B_FAILFAST set, and move these onto the failfast queue.
29875 		 */
29876 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29877 			/*
29878 			 * Save the pointer to the next bp on the wait queue,
29879 			 * so we get to it on the next iteration of this loop.
29880 			 */
29881 			next_waitq_bp = bp->av_forw;
29882 
29883 			/*
29884 			 * If this bp from the wait queue does NOT have
29885 			 * B_FAILFAST set, just move on to the next element
29886 			 * in the wait queue. Note, this is the only place
29887 			 * where it is correct to set prev_waitq_bp.
29888 			 */
29889 			if ((bp->b_flags & B_FAILFAST) == 0) {
29890 				prev_waitq_bp = bp;
29891 				continue;
29892 			}
29893 
29894 			/*
29895 			 * Remove the bp from the wait queue.
29896 			 */
29897 			if (bp == un->un_waitq_headp) {
29898 				/* The bp is the first element of the waitq. */
29899 				un->un_waitq_headp = next_waitq_bp;
29900 				if (un->un_waitq_headp == NULL) {
29901 					/* The wait queue is now empty */
29902 					un->un_waitq_tailp = NULL;
29903 				}
29904 			} else {
29905 				/*
29906 				 * The bp is either somewhere in the middle
29907 				 * or at the end of the wait queue.
29908 				 */
29909 				ASSERT(un->un_waitq_headp != NULL);
29910 				ASSERT(prev_waitq_bp != NULL);
29911 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29912 				    == 0);
29913 				if (bp == un->un_waitq_tailp) {
29914 					/* bp is the last entry on the waitq. */
29915 					ASSERT(next_waitq_bp == NULL);
29916 					un->un_waitq_tailp = prev_waitq_bp;
29917 				}
29918 				prev_waitq_bp->av_forw = next_waitq_bp;
29919 			}
29920 			bp->av_forw = NULL;
29921 
29922 			/*
29923 			 * update kstat since the bp is moved out of
29924 			 * the waitq
29925 			 */
29926 			SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
29927 
29928 			/*
29929 			 * Now put the bp onto the failfast queue.
29930 			 */
29931 			if (un->un_failfast_headp == NULL) {
29932 				/* failfast queue is currently empty */
29933 				ASSERT(un->un_failfast_tailp == NULL);
29934 				un->un_failfast_headp =
29935 				    un->un_failfast_tailp = bp;
29936 			} else {
29937 				/* Add the bp to the end of the failfast q */
29938 				ASSERT(un->un_failfast_tailp != NULL);
29939 				ASSERT(un->un_failfast_tailp->b_flags &
29940 				    B_FAILFAST);
29941 				un->un_failfast_tailp->av_forw = bp;
29942 				un->un_failfast_tailp = bp;
29943 			}
29944 		}
29945 	}
29946 
29947 	/*
29948 	 * Now return all bp's on the failfast queue to their owners.
29949 	 */
29950 	while ((bp = un->un_failfast_headp) != NULL) {
29951 
29952 		un->un_failfast_headp = bp->av_forw;
29953 		if (un->un_failfast_headp == NULL) {
29954 			un->un_failfast_tailp = NULL;
29955 		}
29956 
29957 		/*
29958 		 * We want to return the bp with a failure error code, but
29959 		 * we do not want a call to sd_start_cmds() to occur here,
29960 		 * so use sd_return_failed_command_no_restart() instead of
29961 		 * sd_return_failed_command().
29962 		 */
29963 		sd_return_failed_command_no_restart(un, bp, EIO);
29964 	}
29965 
29966 	/* Flush the xbuf queues if required. */
29967 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29968 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29969 	}
29970 
29971 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29972 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
29973 }
29974 
29975 
29976 /*
29977  *    Function: sd_failfast_flushq_callback
29978  *
29979  * Description: Return TRUE if the given bp meets the criteria for failfast
29980  *		flushing. Used with ddi_xbuf_flushq(9F).
29981  *
29982  *   Arguments: bp - ptr to buf struct to be examined.
29983  *
29984  *     Context: Any
29985  */
29986 
29987 static int
29988 sd_failfast_flushq_callback(struct buf *bp)
29989 {
29990 	/*
29991 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29992 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
29993 	 */
29994 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29995 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29996 }
29997 
29998 
29999 
30000 #if defined(__i386) || defined(__amd64)
30001 /*
30002  * Function: sd_setup_next_xfer
30003  *
30004  * Description: Prepare next I/O operation using DMA_PARTIAL
30005  *
30006  */
30007 
30008 static int
30009 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
30010     struct scsi_pkt *pkt, struct sd_xbuf *xp)
30011 {
30012 	ssize_t	num_blks_not_xfered;
30013 	daddr_t	strt_blk_num;
30014 	ssize_t	bytes_not_xfered;
30015 	int	rval;
30016 
30017 	ASSERT(pkt->pkt_resid == 0);
30018 
30019 	/*
30020 	 * Calculate next block number and amount to be transferred.
30021 	 *
30022 	 * How much data NOT transfered to the HBA yet.
30023 	 */
30024 	bytes_not_xfered = xp->xb_dma_resid;
30025 
30026 	/*
30027 	 * figure how many blocks NOT transfered to the HBA yet.
30028 	 */
30029 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
30030 
30031 	/*
30032 	 * set starting block number to the end of what WAS transfered.
30033 	 */
30034 	strt_blk_num = xp->xb_blkno +
30035 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
30036 
30037 	/*
30038 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
30039 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
30040 	 * the disk mutex here.
30041 	 */
30042 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
30043 	    strt_blk_num, num_blks_not_xfered);
30044 
30045 	if (rval == 0) {
30046 
30047 		/*
30048 		 * Success.
30049 		 *
30050 		 * Adjust things if there are still more blocks to be
30051 		 * transfered.
30052 		 */
30053 		xp->xb_dma_resid = pkt->pkt_resid;
30054 		pkt->pkt_resid = 0;
30055 
30056 		return (1);
30057 	}
30058 
30059 	/*
30060 	 * There's really only one possible return value from
30061 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
30062 	 * returns NULL.
30063 	 */
30064 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
30065 
30066 	bp->b_resid = bp->b_bcount;
30067 	bp->b_flags |= B_ERROR;
30068 
30069 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
30070 	    "Error setting up next portion of DMA transfer\n");
30071 
30072 	return (0);
30073 }
30074 #endif
30075 
30076 /*
30077  *    Function: sd_panic_for_res_conflict
30078  *
30079  * Description: Call panic with a string formated with "Reservation Conflict"
30080  *		and a human readable identifier indicating the SD instance
30081  *		that experienced the reservation conflict.
30082  *
30083  *   Arguments: un - pointer to the soft state struct for the instance.
30084  *
30085  *     Context: may execute in interrupt context.
30086  */
30087 
30088 #define	SD_RESV_CONFLICT_FMT_LEN 40
30089 void
30090 sd_panic_for_res_conflict(struct sd_lun *un)
30091 {
30092 	char panic_str[SD_RESV_CONFLICT_FMT_LEN+MAXPATHLEN];
30093 	char path_str[MAXPATHLEN];
30094 
30095 	(void) snprintf(panic_str, sizeof (panic_str),
30096 	    "Reservation Conflict\nDisk: %s",
30097 	    ddi_pathname(SD_DEVINFO(un), path_str));
30098 
30099 	panic(panic_str);
30100 }
30101 
30102 /*
30103  * Note: The following sd_faultinjection_ioctl( ) routines implement
30104  * driver support for handling fault injection for error analysis
30105  * causing faults in multiple layers of the driver.
30106  *
30107  */
30108 
30109 #ifdef SD_FAULT_INJECTION
30110 static uint_t   sd_fault_injection_on = 0;
30111 
30112 /*
30113  *    Function: sd_faultinjection_ioctl()
30114  *
30115  * Description: This routine is the driver entry point for handling
30116  *              faultinjection ioctls to inject errors into the
30117  *              layer model
30118  *
30119  *   Arguments: cmd	- the ioctl cmd recieved
30120  *		arg	- the arguments from user and returns
30121  */
30122 
30123 static void
30124 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
30125 
30126 	uint_t i;
30127 	uint_t rval;
30128 
30129 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
30130 
30131 	mutex_enter(SD_MUTEX(un));
30132 
30133 	switch (cmd) {
30134 	case SDIOCRUN:
30135 		/* Allow pushed faults to be injected */
30136 		SD_INFO(SD_LOG_SDTEST, un,
30137 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
30138 
30139 		sd_fault_injection_on = 1;
30140 
30141 		SD_INFO(SD_LOG_IOERR, un,
30142 		    "sd_faultinjection_ioctl: run finished\n");
30143 		break;
30144 
30145 	case SDIOCSTART:
30146 		/* Start Injection Session */
30147 		SD_INFO(SD_LOG_SDTEST, un,
30148 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
30149 
30150 		sd_fault_injection_on = 0;
30151 		un->sd_injection_mask = 0xFFFFFFFF;
30152 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30153 			un->sd_fi_fifo_pkt[i] = NULL;
30154 			un->sd_fi_fifo_xb[i] = NULL;
30155 			un->sd_fi_fifo_un[i] = NULL;
30156 			un->sd_fi_fifo_arq[i] = NULL;
30157 		}
30158 		un->sd_fi_fifo_start = 0;
30159 		un->sd_fi_fifo_end = 0;
30160 
30161 		mutex_enter(&(un->un_fi_mutex));
30162 		un->sd_fi_log[0] = '\0';
30163 		un->sd_fi_buf_len = 0;
30164 		mutex_exit(&(un->un_fi_mutex));
30165 
30166 		SD_INFO(SD_LOG_IOERR, un,
30167 		    "sd_faultinjection_ioctl: start finished\n");
30168 		break;
30169 
30170 	case SDIOCSTOP:
30171 		/* Stop Injection Session */
30172 		SD_INFO(SD_LOG_SDTEST, un,
30173 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
30174 		sd_fault_injection_on = 0;
30175 		un->sd_injection_mask = 0x0;
30176 
30177 		/* Empty stray or unuseds structs from fifo */
30178 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
30179 			if (un->sd_fi_fifo_pkt[i] != NULL) {
30180 				kmem_free(un->sd_fi_fifo_pkt[i],
30181 				    sizeof (struct sd_fi_pkt));
30182 			}
30183 			if (un->sd_fi_fifo_xb[i] != NULL) {
30184 				kmem_free(un->sd_fi_fifo_xb[i],
30185 				    sizeof (struct sd_fi_xb));
30186 			}
30187 			if (un->sd_fi_fifo_un[i] != NULL) {
30188 				kmem_free(un->sd_fi_fifo_un[i],
30189 				    sizeof (struct sd_fi_un));
30190 			}
30191 			if (un->sd_fi_fifo_arq[i] != NULL) {
30192 				kmem_free(un->sd_fi_fifo_arq[i],
30193 				    sizeof (struct sd_fi_arq));
30194 			}
30195 			un->sd_fi_fifo_pkt[i] = NULL;
30196 			un->sd_fi_fifo_un[i] = NULL;
30197 			un->sd_fi_fifo_xb[i] = NULL;
30198 			un->sd_fi_fifo_arq[i] = NULL;
30199 		}
30200 		un->sd_fi_fifo_start = 0;
30201 		un->sd_fi_fifo_end = 0;
30202 
30203 		SD_INFO(SD_LOG_IOERR, un,
30204 		    "sd_faultinjection_ioctl: stop finished\n");
30205 		break;
30206 
30207 	case SDIOCINSERTPKT:
30208 		/* Store a packet struct to be pushed onto fifo */
30209 		SD_INFO(SD_LOG_SDTEST, un,
30210 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
30211 
30212 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30213 
30214 		sd_fault_injection_on = 0;
30215 
30216 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
30217 		if (un->sd_fi_fifo_pkt[i] != NULL) {
30218 			kmem_free(un->sd_fi_fifo_pkt[i],
30219 			    sizeof (struct sd_fi_pkt));
30220 		}
30221 		if (arg != NULL) {
30222 			un->sd_fi_fifo_pkt[i] =
30223 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
30224 			if (un->sd_fi_fifo_pkt[i] == NULL) {
30225 				/* Alloc failed don't store anything */
30226 				break;
30227 			}
30228 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
30229 			    sizeof (struct sd_fi_pkt), 0);
30230 			if (rval == -1) {
30231 				kmem_free(un->sd_fi_fifo_pkt[i],
30232 				    sizeof (struct sd_fi_pkt));
30233 				un->sd_fi_fifo_pkt[i] = NULL;
30234 			}
30235 		} else {
30236 			SD_INFO(SD_LOG_IOERR, un,
30237 			    "sd_faultinjection_ioctl: pkt null\n");
30238 		}
30239 		break;
30240 
30241 	case SDIOCINSERTXB:
30242 		/* Store a xb struct to be pushed onto fifo */
30243 		SD_INFO(SD_LOG_SDTEST, un,
30244 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
30245 
30246 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30247 
30248 		sd_fault_injection_on = 0;
30249 
30250 		if (un->sd_fi_fifo_xb[i] != NULL) {
30251 			kmem_free(un->sd_fi_fifo_xb[i],
30252 			    sizeof (struct sd_fi_xb));
30253 			un->sd_fi_fifo_xb[i] = NULL;
30254 		}
30255 		if (arg != NULL) {
30256 			un->sd_fi_fifo_xb[i] =
30257 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
30258 			if (un->sd_fi_fifo_xb[i] == NULL) {
30259 				/* Alloc failed don't store anything */
30260 				break;
30261 			}
30262 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
30263 			    sizeof (struct sd_fi_xb), 0);
30264 
30265 			if (rval == -1) {
30266 				kmem_free(un->sd_fi_fifo_xb[i],
30267 				    sizeof (struct sd_fi_xb));
30268 				un->sd_fi_fifo_xb[i] = NULL;
30269 			}
30270 		} else {
30271 			SD_INFO(SD_LOG_IOERR, un,
30272 			    "sd_faultinjection_ioctl: xb null\n");
30273 		}
30274 		break;
30275 
30276 	case SDIOCINSERTUN:
30277 		/* Store a un struct to be pushed onto fifo */
30278 		SD_INFO(SD_LOG_SDTEST, un,
30279 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
30280 
30281 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30282 
30283 		sd_fault_injection_on = 0;
30284 
30285 		if (un->sd_fi_fifo_un[i] != NULL) {
30286 			kmem_free(un->sd_fi_fifo_un[i],
30287 			    sizeof (struct sd_fi_un));
30288 			un->sd_fi_fifo_un[i] = NULL;
30289 		}
30290 		if (arg != NULL) {
30291 			un->sd_fi_fifo_un[i] =
30292 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
30293 			if (un->sd_fi_fifo_un[i] == NULL) {
30294 				/* Alloc failed don't store anything */
30295 				break;
30296 			}
30297 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
30298 			    sizeof (struct sd_fi_un), 0);
30299 			if (rval == -1) {
30300 				kmem_free(un->sd_fi_fifo_un[i],
30301 				    sizeof (struct sd_fi_un));
30302 				un->sd_fi_fifo_un[i] = NULL;
30303 			}
30304 
30305 		} else {
30306 			SD_INFO(SD_LOG_IOERR, un,
30307 			    "sd_faultinjection_ioctl: un null\n");
30308 		}
30309 
30310 		break;
30311 
30312 	case SDIOCINSERTARQ:
30313 		/* Store a arq struct to be pushed onto fifo */
30314 		SD_INFO(SD_LOG_SDTEST, un,
30315 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
30316 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
30317 
30318 		sd_fault_injection_on = 0;
30319 
30320 		if (un->sd_fi_fifo_arq[i] != NULL) {
30321 			kmem_free(un->sd_fi_fifo_arq[i],
30322 			    sizeof (struct sd_fi_arq));
30323 			un->sd_fi_fifo_arq[i] = NULL;
30324 		}
30325 		if (arg != NULL) {
30326 			un->sd_fi_fifo_arq[i] =
30327 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
30328 			if (un->sd_fi_fifo_arq[i] == NULL) {
30329 				/* Alloc failed don't store anything */
30330 				break;
30331 			}
30332 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
30333 			    sizeof (struct sd_fi_arq), 0);
30334 			if (rval == -1) {
30335 				kmem_free(un->sd_fi_fifo_arq[i],
30336 				    sizeof (struct sd_fi_arq));
30337 				un->sd_fi_fifo_arq[i] = NULL;
30338 			}
30339 
30340 		} else {
30341 			SD_INFO(SD_LOG_IOERR, un,
30342 			    "sd_faultinjection_ioctl: arq null\n");
30343 		}
30344 
30345 		break;
30346 
30347 	case SDIOCPUSH:
30348 		/* Push stored xb, pkt, un, and arq onto fifo */
30349 		sd_fault_injection_on = 0;
30350 
30351 		if (arg != NULL) {
30352 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
30353 			if (rval != -1 &&
30354 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30355 				un->sd_fi_fifo_end += i;
30356 			}
30357 		} else {
30358 			SD_INFO(SD_LOG_IOERR, un,
30359 			    "sd_faultinjection_ioctl: push arg null\n");
30360 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
30361 				un->sd_fi_fifo_end++;
30362 			}
30363 		}
30364 		SD_INFO(SD_LOG_IOERR, un,
30365 		    "sd_faultinjection_ioctl: push to end=%d\n",
30366 		    un->sd_fi_fifo_end);
30367 		break;
30368 
30369 	case SDIOCRETRIEVE:
30370 		/* Return buffer of log from Injection session */
30371 		SD_INFO(SD_LOG_SDTEST, un,
30372 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
30373 
30374 		sd_fault_injection_on = 0;
30375 
30376 		mutex_enter(&(un->un_fi_mutex));
30377 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
30378 		    un->sd_fi_buf_len+1, 0);
30379 		mutex_exit(&(un->un_fi_mutex));
30380 
30381 		if (rval == -1) {
30382 			/*
30383 			 * arg is possibly invalid setting
30384 			 * it to NULL for return
30385 			 */
30386 			arg = NULL;
30387 		}
30388 		break;
30389 	}
30390 
30391 	mutex_exit(SD_MUTEX(un));
30392 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
30393 			    " exit\n");
30394 }
30395 
30396 
30397 /*
30398  *    Function: sd_injection_log()
30399  *
30400  * Description: This routine adds buff to the already existing injection log
30401  *              for retrieval via faultinjection_ioctl for use in fault
30402  *              detection and recovery
30403  *
30404  *   Arguments: buf - the string to add to the log
30405  */
30406 
30407 static void
30408 sd_injection_log(char *buf, struct sd_lun *un)
30409 {
30410 	uint_t len;
30411 
30412 	ASSERT(un != NULL);
30413 	ASSERT(buf != NULL);
30414 
30415 	mutex_enter(&(un->un_fi_mutex));
30416 
30417 	len = min(strlen(buf), 255);
30418 	/* Add logged value to Injection log to be returned later */
30419 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
30420 		uint_t	offset = strlen((char *)un->sd_fi_log);
30421 		char *destp = (char *)un->sd_fi_log + offset;
30422 		int i;
30423 		for (i = 0; i < len; i++) {
30424 			*destp++ = *buf++;
30425 		}
30426 		un->sd_fi_buf_len += len;
30427 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
30428 	}
30429 
30430 	mutex_exit(&(un->un_fi_mutex));
30431 }
30432 
30433 
30434 /*
30435  *    Function: sd_faultinjection()
30436  *
30437  * Description: This routine takes the pkt and changes its
30438  *		content based on error injection scenerio.
30439  *
30440  *   Arguments: pktp	- packet to be changed
30441  */
30442 
30443 static void
30444 sd_faultinjection(struct scsi_pkt *pktp)
30445 {
30446 	uint_t i;
30447 	struct sd_fi_pkt *fi_pkt;
30448 	struct sd_fi_xb *fi_xb;
30449 	struct sd_fi_un *fi_un;
30450 	struct sd_fi_arq *fi_arq;
30451 	struct buf *bp;
30452 	struct sd_xbuf *xb;
30453 	struct sd_lun *un;
30454 
30455 	ASSERT(pktp != NULL);
30456 
30457 	/* pull bp xb and un from pktp */
30458 	bp = (struct buf *)pktp->pkt_private;
30459 	xb = SD_GET_XBUF(bp);
30460 	un = SD_GET_UN(bp);
30461 
30462 	ASSERT(un != NULL);
30463 
30464 	mutex_enter(SD_MUTEX(un));
30465 
30466 	SD_TRACE(SD_LOG_SDTEST, un,
30467 	    "sd_faultinjection: entry Injection from sdintr\n");
30468 
30469 	/* if injection is off return */
30470 	if (sd_fault_injection_on == 0 ||
30471 		un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
30472 		mutex_exit(SD_MUTEX(un));
30473 		return;
30474 	}
30475 
30476 
30477 	/* take next set off fifo */
30478 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
30479 
30480 	fi_pkt = un->sd_fi_fifo_pkt[i];
30481 	fi_xb = un->sd_fi_fifo_xb[i];
30482 	fi_un = un->sd_fi_fifo_un[i];
30483 	fi_arq = un->sd_fi_fifo_arq[i];
30484 
30485 
30486 	/* set variables accordingly */
30487 	/* set pkt if it was on fifo */
30488 	if (fi_pkt != NULL) {
30489 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
30490 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
30491 		SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
30492 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
30493 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
30494 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
30495 
30496 	}
30497 
30498 	/* set xb if it was on fifo */
30499 	if (fi_xb != NULL) {
30500 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
30501 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
30502 		SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
30503 		SD_CONDSET(xb, xb, xb_victim_retry_count,
30504 		    "xb_victim_retry_count");
30505 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
30506 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
30507 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
30508 
30509 		/* copy in block data from sense */
30510 		if (fi_xb->xb_sense_data[0] != -1) {
30511 			bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
30512 			    SENSE_LENGTH);
30513 		}
30514 
30515 		/* copy in extended sense codes */
30516 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code,
30517 		    "es_code");
30518 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key,
30519 		    "es_key");
30520 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code,
30521 		    "es_add_code");
30522 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb,
30523 		    es_qual_code, "es_qual_code");
30524 	}
30525 
30526 	/* set un if it was on fifo */
30527 	if (fi_un != NULL) {
30528 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
30529 		SD_CONDSET(un, un, un_ctype, "un_ctype");
30530 		SD_CONDSET(un, un, un_reset_retry_count,
30531 		    "un_reset_retry_count");
30532 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
30533 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
30534 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
30535 		SD_CONDSET(un, un, un_f_geometry_is_valid,
30536 		    "un_f_geometry_is_valid");
30537 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
30538 		    "un_f_allow_bus_device_reset");
30539 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
30540 
30541 	}
30542 
30543 	/* copy in auto request sense if it was on fifo */
30544 	if (fi_arq != NULL) {
30545 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
30546 	}
30547 
30548 	/* free structs */
30549 	if (un->sd_fi_fifo_pkt[i] != NULL) {
30550 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
30551 	}
30552 	if (un->sd_fi_fifo_xb[i] != NULL) {
30553 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
30554 	}
30555 	if (un->sd_fi_fifo_un[i] != NULL) {
30556 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
30557 	}
30558 	if (un->sd_fi_fifo_arq[i] != NULL) {
30559 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
30560 	}
30561 
30562 	/*
30563 	 * kmem_free does not gurantee to set to NULL
30564 	 * since we uses these to determine if we set
30565 	 * values or not lets confirm they are always
30566 	 * NULL after free
30567 	 */
30568 	un->sd_fi_fifo_pkt[i] = NULL;
30569 	un->sd_fi_fifo_un[i] = NULL;
30570 	un->sd_fi_fifo_xb[i] = NULL;
30571 	un->sd_fi_fifo_arq[i] = NULL;
30572 
30573 	un->sd_fi_fifo_start++;
30574 
30575 	mutex_exit(SD_MUTEX(un));
30576 
30577 	SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
30578 }
30579 
30580 #endif /* SD_FAULT_INJECTION */
30581 
30582 /*
30583  * This routine is invoked in sd_unit_attach(). Before calling it, the
30584  * properties in conf file should be processed already, and "hotpluggable"
30585  * property was processed also.
30586  *
30587  * The sd driver distinguishes 3 different type of devices: removable media,
30588  * non-removable media, and hotpluggable. Below the differences are defined:
30589  *
30590  * 1. Device ID
30591  *
30592  *     The device ID of a device is used to identify this device. Refer to
30593  *     ddi_devid_register(9F).
30594  *
30595  *     For a non-removable media disk device which can provide 0x80 or 0x83
30596  *     VPD page (refer to INQUIRY command of SCSI SPC specification), a unique
30597  *     device ID is created to identify this device. For other non-removable
30598  *     media devices, a default device ID is created only if this device has
30599  *     at least 2 alter cylinders. Otherwise, this device has no devid.
30600  *
30601  *     -------------------------------------------------------
30602  *     removable media   hotpluggable  | Can Have Device ID
30603  *     -------------------------------------------------------
30604  *         false             false     |     Yes
30605  *         false             true      |     Yes
30606  *         true                x       |     No
30607  *     ------------------------------------------------------
30608  *
30609  *
30610  * 2. SCSI group 4 commands
30611  *
30612  *     In SCSI specs, only some commands in group 4 command set can use
30613  *     8-byte addresses that can be used to access >2TB storage spaces.
30614  *     Other commands have no such capability. Without supporting group4,
30615  *     it is impossible to make full use of storage spaces of a disk with
30616  *     capacity larger than 2TB.
30617  *
30618  *     -----------------------------------------------
30619  *     removable media   hotpluggable   LP64  |  Group
30620  *     -----------------------------------------------
30621  *           false          false       false |   1
30622  *           false          false       true  |   4
30623  *           false          true        false |   1
30624  *           false          true        true  |   4
30625  *           true             x           x   |   5
30626  *     -----------------------------------------------
30627  *
30628  *
30629  * 3. Check for VTOC Label
30630  *
30631  *     If a direct-access disk has no EFI label, sd will check if it has a
30632  *     valid VTOC label. Now, sd also does that check for removable media
30633  *     and hotpluggable devices.
30634  *
30635  *     --------------------------------------------------------------
30636  *     Direct-Access   removable media    hotpluggable |  Check Label
30637  *     -------------------------------------------------------------
30638  *         false          false           false        |   No
30639  *         false          false           true         |   No
30640  *         false          true            false        |   Yes
30641  *         false          true            true         |   Yes
30642  *         true            x                x          |   Yes
30643  *     --------------------------------------------------------------
30644  *
30645  *
30646  * 4. Building default VTOC label
30647  *
30648  *     As section 3 says, sd checks if some kinds of devices have VTOC label.
30649  *     If those devices have no valid VTOC label, sd(7d) will attempt to
30650  *     create default VTOC for them. Currently sd creates default VTOC label
30651  *     for all devices on x86 platform (VTOC_16), but only for removable
30652  *     media devices on SPARC (VTOC_8).
30653  *
30654  *     -----------------------------------------------------------
30655  *       removable media hotpluggable platform   |   Default Label
30656  *     -----------------------------------------------------------
30657  *             false          false    sparc     |     No
30658  *             false          true      x86      |     Yes
30659  *             false          true     sparc     |     Yes
30660  *             true             x        x       |     Yes
30661  *     ----------------------------------------------------------
30662  *
30663  *
30664  * 5. Supported blocksizes of target devices
30665  *
30666  *     Sd supports non-512-byte blocksize for removable media devices only.
30667  *     For other devices, only 512-byte blocksize is supported. This may be
30668  *     changed in near future because some RAID devices require non-512-byte
30669  *     blocksize
30670  *
30671  *     -----------------------------------------------------------
30672  *     removable media    hotpluggable    | non-512-byte blocksize
30673  *     -----------------------------------------------------------
30674  *           false          false         |   No
30675  *           false          true          |   No
30676  *           true             x           |   Yes
30677  *     -----------------------------------------------------------
30678  *
30679  *
30680  * 6. Automatic mount & unmount (i.e. vold)
30681  *
30682  *     Sd(7d) driver provides DKIOCREMOVABLE ioctl. This ioctl is used to query
30683  *     if a device is removable media device. It return 1 for removable media
30684  *     devices, and 0 for others.
30685  *
30686  *     Vold treats a device as removable one only if DKIOREMOVABLE returns 1.
30687  *     And it does automounting only for removable media devices. In order to
30688  *     preserve users' experience and let vold continue to do automounting for
30689  *     USB disk devices, DKIOCREMOVABLE ioctl still returns 1 for USB/1394 disk
30690  *     devices.
30691  *
30692  *      ------------------------------------------------------
30693  *       removable media    hotpluggable   |  automatic mount
30694  *      ------------------------------------------------------
30695  *             false          false        |   No
30696  *             false          true         |   Yes
30697  *             true             x          |   Yes
30698  *      ------------------------------------------------------
30699  *
30700  *
30701  * 7. fdisk partition management
30702  *
30703  *     Fdisk is traditional partition method on x86 platform. Sd(7d) driver
30704  *     just supports fdisk partitions on x86 platform. On sparc platform, sd
30705  *     doesn't support fdisk partitions at all. Note: pcfs(7fs) can recognize
30706  *     fdisk partitions on both x86 and SPARC platform.
30707  *
30708  *     -----------------------------------------------------------
30709  *       platform   removable media  USB/1394  |  fdisk supported
30710  *     -----------------------------------------------------------
30711  *        x86         X               X        |       true
30712  *     ------------------------------------------------------------
30713  *        sparc       X               X        |       false
30714  *     ------------------------------------------------------------
30715  *
30716  *
30717  * 8. MBOOT/MBR
30718  *
30719  *     Although sd(7d) doesn't support fdisk on SPARC platform, it does support
30720  *     read/write mboot for removable media devices on sparc platform.
30721  *
30722  *     -----------------------------------------------------------
30723  *       platform   removable media  USB/1394  |  mboot supported
30724  *     -----------------------------------------------------------
30725  *        x86         X               X        |       true
30726  *     ------------------------------------------------------------
30727  *        sparc      false           false     |       false
30728  *        sparc      false           true      |       true
30729  *        sparc      true            false     |       true
30730  *        sparc      true            true      |       true
30731  *     ------------------------------------------------------------
30732  *
30733  *
30734  * 9.  error handling during opening device
30735  *
30736  *     If failed to open a disk device, an errno is returned. For some kinds
30737  *     of errors, different errno is returned depending on if this device is
30738  *     a removable media device. This brings USB/1394 hard disks in line with
30739  *     expected hard disk behavior. It is not expected that this breaks any
30740  *     application.
30741  *
30742  *     ------------------------------------------------------
30743  *       removable media    hotpluggable   |  errno
30744  *     ------------------------------------------------------
30745  *             false          false        |   EIO
30746  *             false          true         |   EIO
30747  *             true             x          |   ENXIO
30748  *     ------------------------------------------------------
30749  *
30750  *
30751  * 10. off-by-1 workaround (bug 1175930, and 4996920) (x86 only)
30752  *
30753  *     [ this is a bit of very ugly history, soon to be removed ]
30754  *
30755  *     SCSI READ_CAPACITY command returns the last valid logical block number
30756  *     which starts from 0. So real capacity is larger than the returned
30757  *     value by 1. However, because scdk.c (which was EOL'ed) directly used
30758  *     the logical block number as capacity of disk devices, off-by-1 work-
30759  *     around was applied. This workaround causes fixed SCSI disk to loss a
30760  *     sector on x86 platform, and precludes exchanging fixed hard disks
30761  *     between sparc and x86.
30762  *
30763  *     ------------------------------------------------------
30764  *       removable media    hotplug        |   Off-by-1 works
30765  *     -------------------------------------------------------
30766  *             false          false        |     Yes
30767  *             false          true         |     No
30768  *             true           false        |     No
30769  *             true           true         |     No
30770  *     ------------------------------------------------------
30771  *
30772  *
30773  * 11. ioctls: DKIOCEJECT, CDROMEJECT
30774  *
30775  *     These IOCTLs are applicable only to removable media devices.
30776  *
30777  *     -----------------------------------------------------------
30778  *       removable media    hotpluggable   |DKIOCEJECT, CDROMEJECT
30779  *     -----------------------------------------------------------
30780  *             false          false        |     No
30781  *             false          true         |     No
30782  *             true            x           |     Yes
30783  *     -----------------------------------------------------------
30784  *
30785  *
30786  * 12. Kstats for partitions
30787  *
30788  *     sd creates partition kstat for non-removable media devices. USB and
30789  *     Firewire hard disks now have partition kstats
30790  *
30791  *      ------------------------------------------------------
30792  *       removable media    hotplugable    |   kstat
30793  *      ------------------------------------------------------
30794  *             false          false        |    Yes
30795  *             false          true         |    Yes
30796  *             true             x          |    No
30797  *       ------------------------------------------------------
30798  *
30799  *
30800  * 13. Removable media & hotpluggable properties
30801  *
30802  *     Sd driver creates a "removable-media" property for removable media
30803  *     devices. Parent nexus drivers create a "hotpluggable" property if
30804  *     it supports hotplugging.
30805  *
30806  *     ---------------------------------------------------------------------
30807  *     removable media   hotpluggable |  "removable-media"   " hotpluggable"
30808  *     ---------------------------------------------------------------------
30809  *       false            false       |    No                   No
30810  *       false            true        |    No                   Yes
30811  *       true             false       |    Yes                  No
30812  *       true             true        |    Yes                  Yes
30813  *     ---------------------------------------------------------------------
30814  *
30815  *
30816  * 14. Power Management
30817  *
30818  *     sd only power manages removable media devices or devices that support
30819  *     LOG_SENSE or have a "pm-capable" property  (PSARC/2002/250)
30820  *
30821  *     A parent nexus that supports hotplugging can also set "pm-capable"
30822  *     if the disk can be power managed.
30823  *
30824  *     ------------------------------------------------------------
30825  *       removable media hotpluggable pm-capable  |   power manage
30826  *     ------------------------------------------------------------
30827  *             false          false     false     |     No
30828  *             false          false     true      |     Yes
30829  *             false          true      false     |     No
30830  *             false          true      true      |     Yes
30831  *             true             x        x        |     Yes
30832  *     ------------------------------------------------------------
30833  *
30834  *      USB and firewire hard disks can now be power managed independently
30835  *      of the framebuffer
30836  *
30837  *
30838  * 15. Support for USB disks with capacity larger than 1TB
30839  *
30840  *     Currently, sd doesn't permit a fixed disk device with capacity
30841  *     larger than 1TB to be used in a 32-bit operating system environment.
30842  *     However, sd doesn't do that for removable media devices. Instead, it
30843  *     assumes that removable media devices cannot have a capacity larger
30844  *     than 1TB. Therefore, using those devices on 32-bit system is partially
30845  *     supported, which can cause some unexpected results.
30846  *
30847  *     ---------------------------------------------------------------------
30848  *       removable media    USB/1394 | Capacity > 1TB |   Used in 32-bit env
30849  *     ---------------------------------------------------------------------
30850  *             false          false  |   true         |     no
30851  *             false          true   |   true         |     no
30852  *             true           false  |   true         |     Yes
30853  *             true           true   |   true         |     Yes
30854  *     ---------------------------------------------------------------------
30855  *
30856  *
30857  * 16. Check write-protection at open time
30858  *
30859  *     When a removable media device is being opened for writing without NDELAY
30860  *     flag, sd will check if this device is writable. If attempting to open
30861  *     without NDELAY flag a write-protected device, this operation will abort.
30862  *
30863  *     ------------------------------------------------------------
30864  *       removable media    USB/1394   |   WP Check
30865  *     ------------------------------------------------------------
30866  *             false          false    |     No
30867  *             false          true     |     No
30868  *             true           false    |     Yes
30869  *             true           true     |     Yes
30870  *     ------------------------------------------------------------
30871  *
30872  *
30873  * 17. syslog when corrupted VTOC is encountered
30874  *
30875  *      Currently, if an invalid VTOC is encountered, sd only print syslog
30876  *      for fixed SCSI disks.
30877  *     ------------------------------------------------------------
30878  *       removable media    USB/1394   |   print syslog
30879  *     ------------------------------------------------------------
30880  *             false          false    |     Yes
30881  *             false          true     |     No
30882  *             true           false    |     No
30883  *             true           true     |     No
30884  *     ------------------------------------------------------------
30885  */
30886 static void
30887 sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi)
30888 {
30889 	int	pm_capable_prop;
30890 
30891 	ASSERT(un->un_sd);
30892 	ASSERT(un->un_sd->sd_inq);
30893 
30894 #if defined(_SUNOS_VTOC_16)
30895 	/*
30896 	 * For VTOC_16 devices, the default label will be created for all
30897 	 * devices. (see sd_build_default_label)
30898 	 */
30899 	un->un_f_default_vtoc_supported = TRUE;
30900 #endif
30901 
30902 	if (un->un_sd->sd_inq->inq_rmb) {
30903 		/*
30904 		 * The media of this device is removable. And for this kind
30905 		 * of devices, it is possible to change medium after openning
30906 		 * devices. Thus we should support this operation.
30907 		 */
30908 		un->un_f_has_removable_media = TRUE;
30909 
30910 #if defined(_SUNOS_VTOC_8)
30911 		/*
30912 		 * Note: currently, for VTOC_8 devices, default label is
30913 		 * created for removable and hotpluggable devices only.
30914 		 */
30915 		un->un_f_default_vtoc_supported = TRUE;
30916 #endif
30917 		/*
30918 		 * support non-512-byte blocksize of removable media devices
30919 		 */
30920 		un->un_f_non_devbsize_supported = TRUE;
30921 
30922 		/*
30923 		 * Assume that all removable media devices support DOOR_LOCK
30924 		 */
30925 		un->un_f_doorlock_supported = TRUE;
30926 
30927 		/*
30928 		 * For a removable media device, it is possible to be opened
30929 		 * with NDELAY flag when there is no media in drive, in this
30930 		 * case we don't care if device is writable. But if without
30931 		 * NDELAY flag, we need to check if media is write-protected.
30932 		 */
30933 		un->un_f_chk_wp_open = TRUE;
30934 
30935 		/*
30936 		 * need to start a SCSI watch thread to monitor media state,
30937 		 * when media is being inserted or ejected, notify syseventd.
30938 		 */
30939 		un->un_f_monitor_media_state = TRUE;
30940 
30941 		/*
30942 		 * Some devices don't support START_STOP_UNIT command.
30943 		 * Therefore, we'd better check if a device supports it
30944 		 * before sending it.
30945 		 */
30946 		un->un_f_check_start_stop = TRUE;
30947 
30948 		/*
30949 		 * support eject media ioctl:
30950 		 *		FDEJECT, DKIOCEJECT, CDROMEJECT
30951 		 */
30952 		un->un_f_eject_media_supported = TRUE;
30953 
30954 		/*
30955 		 * Because many removable-media devices don't support
30956 		 * LOG_SENSE, we couldn't use this command to check if
30957 		 * a removable media device support power-management.
30958 		 * We assume that they support power-management via
30959 		 * START_STOP_UNIT command and can be spun up and down
30960 		 * without limitations.
30961 		 */
30962 		un->un_f_pm_supported = TRUE;
30963 
30964 		/*
30965 		 * Need to create a zero length (Boolean) property
30966 		 * removable-media for the removable media devices.
30967 		 * Note that the return value of the property is not being
30968 		 * checked, since if unable to create the property
30969 		 * then do not want the attach to fail altogether. Consistent
30970 		 * with other property creation in attach.
30971 		 */
30972 		(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
30973 		    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
30974 
30975 	} else {
30976 		/*
30977 		 * create device ID for device
30978 		 */
30979 		un->un_f_devid_supported = TRUE;
30980 
30981 		/*
30982 		 * Spin up non-removable-media devices once it is attached
30983 		 */
30984 		un->un_f_attach_spinup = TRUE;
30985 
30986 		/*
30987 		 * According to SCSI specification, Sense data has two kinds of
30988 		 * format: fixed format, and descriptor format. At present, we
30989 		 * don't support descriptor format sense data for removable
30990 		 * media.
30991 		 */
30992 		if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
30993 			un->un_f_descr_format_supported = TRUE;
30994 		}
30995 
30996 		/*
30997 		 * kstats are created only for non-removable media devices.
30998 		 *
30999 		 * Set this in sd.conf to 0 in order to disable kstats.  The
31000 		 * default is 1, so they are enabled by default.
31001 		 */
31002 		un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
31003 		    SD_DEVINFO(un), DDI_PROP_DONTPASS,
31004 			"enable-partition-kstats", 1));
31005 
31006 		/*
31007 		 * Check if HBA has set the "pm-capable" property.
31008 		 * If "pm-capable" exists and is non-zero then we can
31009 		 * power manage the device without checking the start/stop
31010 		 * cycle count log sense page.
31011 		 *
31012 		 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0)
31013 		 * then we should not power manage the device.
31014 		 *
31015 		 * If "pm-capable" doesn't exist then pm_capable_prop will
31016 		 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case,
31017 		 * sd will check the start/stop cycle count log sense page
31018 		 * and power manage the device if the cycle count limit has
31019 		 * not been exceeded.
31020 		 */
31021 		pm_capable_prop = ddi_prop_get_int(DDI_DEV_T_ANY, devi,
31022 		    DDI_PROP_DONTPASS, "pm-capable", SD_PM_CAPABLE_UNDEFINED);
31023 		if (pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) {
31024 			un->un_f_log_sense_supported = TRUE;
31025 		} else {
31026 			/*
31027 			 * pm-capable property exists.
31028 			 *
31029 			 * Convert "TRUE" values for pm_capable_prop to
31030 			 * SD_PM_CAPABLE_TRUE (1) to make it easier to check
31031 			 * later. "TRUE" values are any values except
31032 			 * SD_PM_CAPABLE_FALSE (0) and
31033 			 * SD_PM_CAPABLE_UNDEFINED (-1)
31034 			 */
31035 			if (pm_capable_prop == SD_PM_CAPABLE_FALSE) {
31036 				un->un_f_log_sense_supported = FALSE;
31037 			} else {
31038 				un->un_f_pm_supported = TRUE;
31039 			}
31040 
31041 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
31042 			    "sd_unit_attach: un:0x%p pm-capable "
31043 			    "property set to %d.\n", un, un->un_f_pm_supported);
31044 		}
31045 	}
31046 
31047 	if (un->un_f_is_hotpluggable) {
31048 #if defined(_SUNOS_VTOC_8)
31049 		/*
31050 		 * Note: currently, for VTOC_8 devices, default label is
31051 		 * created for removable and hotpluggable devices only.
31052 		 */
31053 		un->un_f_default_vtoc_supported = TRUE;
31054 #endif
31055 
31056 		/*
31057 		 * Temporarily, let hotpluggable devices pretend to be
31058 		 * removable-media devices for vold.
31059 		 */
31060 		un->un_f_monitor_media_state = TRUE;
31061 
31062 		un->un_f_check_start_stop = TRUE;
31063 
31064 	}
31065 
31066 	/*
31067 	 * By default, only DIRECT ACCESS devices and CDs will have Sun
31068 	 * labels.
31069 	 */
31070 	if ((SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) ||
31071 	    (un->un_sd->sd_inq->inq_rmb)) {
31072 		/*
31073 		 * Direct access devices have disk label
31074 		 */
31075 		un->un_f_vtoc_label_supported = TRUE;
31076 	}
31077 
31078 	/*
31079 	 * Fdisk partitions are supported for all direct access devices on
31080 	 * x86 platform, and just for removable media and hotpluggable
31081 	 * devices on SPARC platform. Later, we will set the following flag
31082 	 * to FALSE if current device is not removable media or hotpluggable
31083 	 * device and if sd works on SAPRC platform.
31084 	 */
31085 	if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT) {
31086 		un->un_f_mboot_supported = TRUE;
31087 	}
31088 
31089 	if (!un->un_f_is_hotpluggable &&
31090 	    !un->un_sd->sd_inq->inq_rmb) {
31091 
31092 #if defined(_SUNOS_VTOC_8)
31093 		/*
31094 		 * Don't support fdisk on fixed disk
31095 		 */
31096 		un->un_f_mboot_supported = FALSE;
31097 #endif
31098 
31099 		/*
31100 		 * Fixed disk support SYNC CACHE
31101 		 */
31102 		un->un_f_sync_cache_supported = TRUE;
31103 
31104 		/*
31105 		 * For fixed disk, if its VTOC is not valid, we will write
31106 		 * errlog into system log
31107 		 */
31108 		if (un->un_f_vtoc_label_supported)
31109 			un->un_f_vtoc_errlog_supported = TRUE;
31110 	}
31111 }
31112