xref: /titanic_50/usr/src/uts/common/io/scsi/targets/sd.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * SCSI disk target driver.
31  */
32 
33 #include <sys/scsi/scsi.h>
34 #include <sys/dkbad.h>
35 #include <sys/dklabel.h>
36 #include <sys/dkio.h>
37 #include <sys/fdio.h>
38 #include <sys/cdio.h>
39 #include <sys/mhd.h>
40 #include <sys/vtoc.h>
41 #include <sys/dktp/fdisk.h>
42 #include <sys/file.h>
43 #include <sys/stat.h>
44 #include <sys/kstat.h>
45 #include <sys/vtrace.h>
46 #include <sys/note.h>
47 #include <sys/thread.h>
48 #include <sys/proc.h>
49 #include <sys/efi_partition.h>
50 #include <sys/var.h>
51 #include <sys/aio_req.h>
52 #if (defined(__fibre))
53 /* Note: is there a leadville version of the following? */
54 #include <sys/fc4/fcal_linkapp.h>
55 #endif
56 #include <sys/taskq.h>
57 #include <sys/uuid.h>
58 #include <sys/byteorder.h>
59 #include <sys/sdt.h>
60 
61 #include "sd_xbuf.h"
62 
63 #include <sys/scsi/targets/sddef.h>
64 
65 
66 /*
67  * Loadable module info.
68  */
69 #if (defined(__fibre))
70 #define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver %I%"
71 char _depends_on[]	= "misc/scsi drv/fcp";
72 #else
73 #define	SD_MODULE_NAME	"SCSI Disk Driver %I%"
74 char _depends_on[]	= "misc/scsi";
75 #endif
76 
77 /*
78  * Define the interconnect type, to allow the driver to distinguish
79  * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
80  *
81  * This is really for backward compatability. In the future, the driver
82  * should actually check the "interconnect-type" property as reported by
83  * the HBA; however at present this property is not defined by all HBAs,
84  * so we will use this #define (1) to permit the driver to run in
85  * backward-compatability mode; and (2) to print a notification message
86  * if an FC HBA does not support the "interconnect-type" property.  The
87  * behavior of the driver will be to assume parallel SCSI behaviors unless
88  * the "interconnect-type" property is defined by the HBA **AND** has a
89  * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
90  * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
91  * Channel behaviors (as per the old ssd).  (Note that the
92  * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
93  * will result in the driver assuming parallel SCSI behaviors.)
94  *
95  * (see common/sys/scsi/impl/services.h)
96  *
97  * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
98  * since some FC HBAs may already support that, and there is some code in
99  * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
100  * default would confuse that code, and besides things should work fine
101  * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
102  * "interconnect_type" property.
103  */
104 #if (defined(__fibre))
105 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
106 #else
107 #define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
108 #endif
109 
110 /*
111  * The name of the driver, established from the module name in _init.
112  */
113 static	char *sd_label			= NULL;
114 
115 /*
116  * Driver name is unfortunately prefixed on some driver.conf properties.
117  */
118 #if (defined(__fibre))
119 #define	sd_max_xfer_size		ssd_max_xfer_size
120 #define	sd_config_list			ssd_config_list
121 static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
122 static	char *sd_config_list		= "ssd-config-list";
123 #else
124 static	char *sd_max_xfer_size		= "sd_max_xfer_size";
125 static	char *sd_config_list		= "sd-config-list";
126 #endif
127 
128 /*
129  * Driver global variables
130  */
131 
132 #if (defined(__fibre))
133 /*
134  * These #defines are to avoid namespace collisions that occur because this
135  * code is currently used to compile two seperate driver modules: sd and ssd.
136  * All global variables need to be treated this way (even if declared static)
137  * in order to allow the debugger to resolve the names properly.
138  * It is anticipated that in the near future the ssd module will be obsoleted,
139  * at which time this namespace issue should go away.
140  */
141 #define	sd_state			ssd_state
142 #define	sd_io_time			ssd_io_time
143 #define	sd_failfast_enable		ssd_failfast_enable
144 #define	sd_ua_retry_count		ssd_ua_retry_count
145 #define	sd_report_pfa			ssd_report_pfa
146 #define	sd_max_throttle			ssd_max_throttle
147 #define	sd_min_throttle			ssd_min_throttle
148 #define	sd_rot_delay			ssd_rot_delay
149 
150 #define	sd_retry_on_reservation_conflict	\
151 					ssd_retry_on_reservation_conflict
152 #define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
153 #define	sd_resv_conflict_name		ssd_resv_conflict_name
154 
155 #define	sd_component_mask		ssd_component_mask
156 #define	sd_level_mask			ssd_level_mask
157 #define	sd_debug_un			ssd_debug_un
158 #define	sd_error_level			ssd_error_level
159 
160 #define	sd_xbuf_active_limit		ssd_xbuf_active_limit
161 #define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
162 
163 #define	sd_tr				ssd_tr
164 #define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
165 #define	sd_check_media_time		ssd_check_media_time
166 #define	sd_wait_cmds_complete		ssd_wait_cmds_complete
167 #define	sd_label_mutex			ssd_label_mutex
168 #define	sd_detach_mutex			ssd_detach_mutex
169 #define	sd_log_buf			ssd_log_buf
170 #define	sd_log_mutex			ssd_log_mutex
171 
172 #define	sd_disk_table			ssd_disk_table
173 #define	sd_disk_table_size		ssd_disk_table_size
174 #define	sd_sense_mutex			ssd_sense_mutex
175 #define	sd_cdbtab			ssd_cdbtab
176 
177 #define	sd_cb_ops			ssd_cb_ops
178 #define	sd_ops				ssd_ops
179 #define	sd_additional_codes		ssd_additional_codes
180 
181 #define	sd_minor_data			ssd_minor_data
182 #define	sd_minor_data_efi		ssd_minor_data_efi
183 
184 #define	sd_tq				ssd_tq
185 #define	sd_wmr_tq			ssd_wmr_tq
186 #define	sd_taskq_name			ssd_taskq_name
187 #define	sd_wmr_taskq_name		ssd_wmr_taskq_name
188 #define	sd_taskq_minalloc		ssd_taskq_minalloc
189 #define	sd_taskq_maxalloc		ssd_taskq_maxalloc
190 
191 #define	sd_dump_format_string		ssd_dump_format_string
192 
193 #define	sd_iostart_chain		ssd_iostart_chain
194 #define	sd_iodone_chain			ssd_iodone_chain
195 
196 #define	sd_pm_idletime			ssd_pm_idletime
197 
198 #define	sd_force_pm_supported		ssd_force_pm_supported
199 
200 #define	sd_dtype_optical_bind		ssd_dtype_optical_bind
201 #endif
202 
203 
204 #ifdef	SDDEBUG
205 int	sd_force_pm_supported		= 0;
206 #endif	/* SDDEBUG */
207 
208 void *sd_state				= NULL;
209 int sd_io_time				= SD_IO_TIME;
210 int sd_failfast_enable			= 1;
211 int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
212 int sd_report_pfa			= 1;
213 int sd_max_throttle			= SD_MAX_THROTTLE;
214 int sd_min_throttle			= SD_MIN_THROTTLE;
215 int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
216 
217 int sd_retry_on_reservation_conflict	= 1;
218 int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
219 _NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
220 
221 static int sd_dtype_optical_bind	= -1;
222 
223 /* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
224 static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
225 
226 /*
227  * Global data for debug logging. To enable debug printing, sd_component_mask
228  * and sd_level_mask should be set to the desired bit patterns as outlined in
229  * sddef.h.
230  */
231 uint_t	sd_component_mask		= 0x0;
232 uint_t	sd_level_mask			= 0x0;
233 struct	sd_lun *sd_debug_un		= NULL;
234 uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
235 
236 /* Note: these may go away in the future... */
237 static uint32_t	sd_xbuf_active_limit	= 512;
238 static uint32_t sd_xbuf_reserve_limit	= 16;
239 
240 static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
241 
242 /*
243  * Timer value used to reset the throttle after it has been reduced
244  * (typically in response to TRAN_BUSY)
245  */
246 static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
247 
248 /*
249  * Interval value associated with the media change scsi watch.
250  */
251 static int sd_check_media_time		= 3000000;
252 
253 /*
254  * Wait value used for in progress operations during a DDI_SUSPEND
255  */
256 static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
257 
258 /*
259  * sd_label_mutex protects a static buffer used in the disk label
260  * component of the driver
261  */
262 static kmutex_t sd_label_mutex;
263 
264 /*
265  * sd_detach_mutex protects un_layer_count, un_detach_count, and
266  * un_opens_in_progress in the sd_lun structure.
267  */
268 static kmutex_t sd_detach_mutex;
269 
270 _NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
271 	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
272 
273 /*
274  * Global buffer and mutex for debug logging
275  */
276 static char	sd_log_buf[1024];
277 static kmutex_t	sd_log_mutex;
278 
279 
280 /*
281  * "Smart" Probe Caching structs, globals, #defines, etc.
282  * For parallel scsi and non-self-identify device only.
283  */
284 
285 /*
286  * The following resources and routines are implemented to support
287  * "smart" probing, which caches the scsi_probe() results in an array,
288  * in order to help avoid long probe times.
289  */
290 struct sd_scsi_probe_cache {
291 	struct	sd_scsi_probe_cache	*next;
292 	dev_info_t	*pdip;
293 	int		cache[NTARGETS_WIDE];
294 };
295 
296 static kmutex_t	sd_scsi_probe_cache_mutex;
297 static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
298 
299 /*
300  * Really we only need protection on the head of the linked list, but
301  * better safe than sorry.
302  */
303 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
304     sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
305 
306 _NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
307     sd_scsi_probe_cache_head))
308 
309 
310 /*
311  * Vendor specific data name property declarations
312  */
313 
314 #if defined(__fibre) || defined(__i386) ||defined(__amd64)
315 
316 static sd_tunables seagate_properties = {
317 	SEAGATE_THROTTLE_VALUE,
318 	0,
319 	0,
320 	0,
321 	0,
322 	0,
323 	0,
324 	0,
325 	0
326 };
327 
328 static sd_tunables lsi_properties = {
329 	0,
330 	0,
331 	LSI_NOTREADY_RETRIES,
332 	0,
333 	0,
334 	0,
335 	0,
336 	0,
337 	0
338 };
339 
340 static sd_tunables lsi_oem_properties = {
341 	0,
342 	0,
343 	LSI_OEM_NOTREADY_RETRIES,
344 	0,
345 	0,
346 	0,
347 	0,
348 	0,
349 	0
350 };
351 
352 static sd_tunables fujitsu_properties = {
353 	FUJITSU_THROTTLE_VALUE,
354 	0,
355 	0,
356 	0,
357 	0,
358 	0,
359 	0,
360 	0,
361 	0
362 };
363 
364 static sd_tunables ibm_properties = {
365 	IBM_THROTTLE_VALUE,
366 	0,
367 	0,
368 	0,
369 	0,
370 	0,
371 	0,
372 	0,
373 	0
374 };
375 
376 static sd_tunables purple_properties = {
377 	PURPLE_THROTTLE_VALUE,
378 	0,
379 	0,
380 	PURPLE_BUSY_RETRIES,
381 	PURPLE_RESET_RETRY_COUNT,
382 	PURPLE_RESERVE_RELEASE_TIME,
383 	0,
384 	0,
385 	0
386 };
387 
388 static sd_tunables sve_properties = {
389 	SVE_THROTTLE_VALUE,
390 	0,
391 	0,
392 	SVE_BUSY_RETRIES,
393 	SVE_RESET_RETRY_COUNT,
394 	SVE_RESERVE_RELEASE_TIME,
395 	SVE_MIN_THROTTLE_VALUE,
396 	SVE_DISKSORT_DISABLED_FLAG,
397 	0
398 };
399 
400 static sd_tunables maserati_properties = {
401 	0,
402 	0,
403 	0,
404 	0,
405 	0,
406 	0,
407 	0,
408 	MASERATI_DISKSORT_DISABLED_FLAG,
409 	MASERATI_LUN_RESET_ENABLED_FLAG
410 };
411 
412 static sd_tunables pirus_properties = {
413 	PIRUS_THROTTLE_VALUE,
414 	0,
415 	PIRUS_NRR_COUNT,
416 	PIRUS_BUSY_RETRIES,
417 	PIRUS_RESET_RETRY_COUNT,
418 	0,
419 	PIRUS_MIN_THROTTLE_VALUE,
420 	PIRUS_DISKSORT_DISABLED_FLAG,
421 	PIRUS_LUN_RESET_ENABLED_FLAG
422 };
423 
424 #endif
425 #if (defined(__sparc) && !defined(__fibre)) || \
426 	(defined(__i386) || defined(__amd64))
427 
428 static sd_tunables lsi_properties_scsi = {
429 	LSI_THROTTLE_VALUE,
430 	0,
431 	LSI_NOTREADY_RETRIES,
432 	0,
433 	0,
434 	0,
435 	0,
436 	0,
437 	0
438 };
439 
440 static sd_tunables elite_properties = {
441 	ELITE_THROTTLE_VALUE,
442 	0,
443 	0,
444 	0,
445 	0,
446 	0,
447 	0,
448 	0,
449 	0
450 };
451 
452 static sd_tunables st31200n_properties = {
453 	ST31200N_THROTTLE_VALUE,
454 	0,
455 	0,
456 	0,
457 	0,
458 	0,
459 	0,
460 	0,
461 	0
462 };
463 
464 #endif /* Fibre or not */
465 
466 static sd_tunables symbios_properties = {
467 	SYMBIOS_THROTTLE_VALUE,
468 	0,
469 	SYMBIOS_NOTREADY_RETRIES,
470 	0,
471 	0,
472 	0,
473 	0,
474 	0,
475 	0
476 };
477 
478 
479 
480 
481 #if (defined(SD_PROP_TST))
482 
483 #define	SD_TST_CTYPE_VAL	CTYPE_CDROM
484 #define	SD_TST_THROTTLE_VAL	16
485 #define	SD_TST_NOTREADY_VAL	12
486 #define	SD_TST_BUSY_VAL		60
487 #define	SD_TST_RST_RETRY_VAL	36
488 #define	SD_TST_RSV_REL_TIME	60
489 
490 static sd_tunables tst_properties = {
491 	SD_TST_THROTTLE_VAL,
492 	SD_TST_CTYPE_VAL,
493 	SD_TST_NOTREADY_VAL,
494 	SD_TST_BUSY_VAL,
495 	SD_TST_RST_RETRY_VAL,
496 	SD_TST_RSV_REL_TIME,
497 	0,
498 	0,
499 	0
500 };
501 #endif
502 
503 /* This is similiar to the ANSI toupper implementation */
504 #define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
505 
506 /*
507  * Static Driver Configuration Table
508  *
509  * This is the table of disks which need throttle adjustment (or, perhaps
510  * something else as defined by the flags at a future time.)  device_id
511  * is a string consisting of concatenated vid (vendor), pid (product/model)
512  * and revision strings as defined in the scsi_inquiry structure.  Offsets of
513  * the parts of the string are as defined by the sizes in the scsi_inquiry
514  * structure.  Device type is searched as far as the device_id string is
515  * defined.  Flags defines which values are to be set in the driver from the
516  * properties list.
517  *
518  * Entries below which begin and end with a "*" are a special case.
519  * These do not have a specific vendor, and the string which follows
520  * can appear anywhere in the 16 byte PID portion of the inquiry data.
521  *
522  * Entries below which begin and end with a " " (blank) are a special
523  * case. The comparison function will treat multiple consecutive blanks
524  * as equivalent to a single blank. For example, this causes a
525  * sd_disk_table entry of " NEC CDROM " to match a device's id string
526  * of  "NEC       CDROM".
527  *
528  * Note: The MD21 controller type has been obsoleted.
529  *	 ST318202F is a Legacy device
530  *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
531  *	 made with an FC connection. The entries here are a legacy.
532  */
533 static sd_disk_config_t sd_disk_table[] = {
534 #if defined(__fibre) || defined(__i386) || defined(__amd64)
535 	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
536 	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
537 	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
538 	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
539 	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
540 	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
541 	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
542 	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
543 	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
544 	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
545 	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
546 	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
547 	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
548 	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
549 	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
550 	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
551 	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
552 	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
553 	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
554 	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
555 	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
556 	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
557 	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
558 	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
559 	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
560 	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
561 	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
562 	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
563 	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
564 	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
565 	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
566 	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
567 	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
568 	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
569 	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
570 	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
571 	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
572 			SD_CONF_BSET_BSY_RETRY_COUNT|
573 			SD_CONF_BSET_RST_RETRIES|
574 			SD_CONF_BSET_RSV_REL_TIME,
575 		&purple_properties },
576 	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
577 		SD_CONF_BSET_BSY_RETRY_COUNT|
578 		SD_CONF_BSET_RST_RETRIES|
579 		SD_CONF_BSET_RSV_REL_TIME|
580 		SD_CONF_BSET_MIN_THROTTLE|
581 		SD_CONF_BSET_DISKSORT_DISABLED,
582 		&sve_properties },
583 	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
584 			SD_CONF_BSET_BSY_RETRY_COUNT|
585 			SD_CONF_BSET_RST_RETRIES|
586 			SD_CONF_BSET_RSV_REL_TIME,
587 		&purple_properties },
588 	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
589 		SD_CONF_BSET_LUN_RESET_ENABLED,
590 		&maserati_properties },
591 	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
592 		SD_CONF_BSET_NRR_COUNT|
593 		SD_CONF_BSET_BSY_RETRY_COUNT|
594 		SD_CONF_BSET_RST_RETRIES|
595 		SD_CONF_BSET_MIN_THROTTLE|
596 		SD_CONF_BSET_DISKSORT_DISABLED|
597 		SD_CONF_BSET_LUN_RESET_ENABLED,
598 		&pirus_properties },
599 	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
600 		SD_CONF_BSET_NRR_COUNT|
601 		SD_CONF_BSET_BSY_RETRY_COUNT|
602 		SD_CONF_BSET_RST_RETRIES|
603 		SD_CONF_BSET_MIN_THROTTLE|
604 		SD_CONF_BSET_DISKSORT_DISABLED|
605 		SD_CONF_BSET_LUN_RESET_ENABLED,
606 		&pirus_properties },
607 	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
608 		SD_CONF_BSET_NRR_COUNT|
609 		SD_CONF_BSET_BSY_RETRY_COUNT|
610 		SD_CONF_BSET_RST_RETRIES|
611 		SD_CONF_BSET_MIN_THROTTLE|
612 		SD_CONF_BSET_DISKSORT_DISABLED|
613 		SD_CONF_BSET_LUN_RESET_ENABLED,
614 		&pirus_properties },
615 	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
616 	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
617 	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
618 	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
619 #endif /* fibre or NON-sparc platforms */
620 #if ((defined(__sparc) && !defined(__fibre)) ||\
621 	(defined(__i386) || defined(__amd64)))
622 	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
623 	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
624 	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
625 	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
626 	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
627 	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
628 	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
629 	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
630 	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
631 	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
632 	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
633 	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
634 	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
635 	    &symbios_properties },
636 	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
637 	    &lsi_properties_scsi },
638 #if defined(__i386) || defined(__amd64)
639 	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
640 				    | SD_CONF_BSET_READSUB_BCD
641 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
642 				    | SD_CONF_BSET_NO_READ_HEADER
643 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
644 
645 	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
646 				    | SD_CONF_BSET_READSUB_BCD
647 				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
648 				    | SD_CONF_BSET_NO_READ_HEADER
649 				    | SD_CONF_BSET_READ_CD_XD4), NULL },
650 #endif /* __i386 || __amd64 */
651 #endif /* sparc NON-fibre or NON-sparc platforms */
652 
653 #if (defined(SD_PROP_TST))
654 	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
655 				| SD_CONF_BSET_CTYPE
656 				| SD_CONF_BSET_NRR_COUNT
657 				| SD_CONF_BSET_FAB_DEVID
658 				| SD_CONF_BSET_NOCACHE
659 				| SD_CONF_BSET_BSY_RETRY_COUNT
660 				| SD_CONF_BSET_PLAYMSF_BCD
661 				| SD_CONF_BSET_READSUB_BCD
662 				| SD_CONF_BSET_READ_TOC_TRK_BCD
663 				| SD_CONF_BSET_READ_TOC_ADDR_BCD
664 				| SD_CONF_BSET_NO_READ_HEADER
665 				| SD_CONF_BSET_READ_CD_XD4
666 				| SD_CONF_BSET_RST_RETRIES
667 				| SD_CONF_BSET_RSV_REL_TIME
668 				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
669 #endif
670 };
671 
672 static const int sd_disk_table_size =
673 	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
674 
675 
676 /*
677  * Return codes of sd_uselabel().
678  */
679 #define	SD_LABEL_IS_VALID		0
680 #define	SD_LABEL_IS_INVALID		1
681 
682 #define	SD_INTERCONNECT_PARALLEL	0
683 #define	SD_INTERCONNECT_FABRIC		1
684 #define	SD_INTERCONNECT_FIBRE		2
685 #define	SD_INTERCONNECT_SSA		3
686 #define	SD_IS_PARALLEL_SCSI(un)		\
687 	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
688 
689 /*
690  * Definitions used by device id registration routines
691  */
692 #define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
693 #define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
694 #define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
695 #define	WD_NODE			7	/* the whole disk minor */
696 
697 static kmutex_t sd_sense_mutex = {0};
698 
699 /*
700  * Macros for updates of the driver state
701  */
702 #define	New_state(un, s)        \
703 	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
704 #define	Restore_state(un)	\
705 	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
706 
707 static struct sd_cdbinfo sd_cdbtab[] = {
708 	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
709 	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
710 	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
711 	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
712 };
713 
714 /*
715  * Specifies the number of seconds that must have elapsed since the last
716  * cmd. has completed for a device to be declared idle to the PM framework.
717  */
718 static int sd_pm_idletime = 1;
719 
720 /*
721  * Internal function prototypes
722  */
723 
724 #if (defined(__fibre))
725 /*
726  * These #defines are to avoid namespace collisions that occur because this
727  * code is currently used to compile two seperate driver modules: sd and ssd.
728  * All function names need to be treated this way (even if declared static)
729  * in order to allow the debugger to resolve the names properly.
730  * It is anticipated that in the near future the ssd module will be obsoleted,
731  * at which time this ugliness should go away.
732  */
733 #define	sd_log_trace			ssd_log_trace
734 #define	sd_log_info			ssd_log_info
735 #define	sd_log_err			ssd_log_err
736 #define	sdprobe				ssdprobe
737 #define	sdinfo				ssdinfo
738 #define	sd_prop_op			ssd_prop_op
739 #define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
740 #define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
741 #define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
742 #define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
743 #define	sd_spin_up_unit			ssd_spin_up_unit
744 #define	sd_enable_descr_sense		ssd_enable_descr_sense
745 #define	sd_set_mmc_caps			ssd_set_mmc_caps
746 #define	sd_read_unit_properties		ssd_read_unit_properties
747 #define	sd_process_sdconf_file		ssd_process_sdconf_file
748 #define	sd_process_sdconf_table		ssd_process_sdconf_table
749 #define	sd_sdconf_id_match		ssd_sdconf_id_match
750 #define	sd_blank_cmp			ssd_blank_cmp
751 #define	sd_chk_vers1_data		ssd_chk_vers1_data
752 #define	sd_set_vers1_properties		ssd_set_vers1_properties
753 #define	sd_validate_geometry		ssd_validate_geometry
754 
755 #if defined(_SUNOS_VTOC_16)
756 #define	sd_convert_geometry		ssd_convert_geometry
757 #endif
758 
759 #define	sd_resync_geom_caches		ssd_resync_geom_caches
760 #define	sd_read_fdisk			ssd_read_fdisk
761 #define	sd_get_physical_geometry	ssd_get_physical_geometry
762 #define	sd_get_virtual_geometry		ssd_get_virtual_geometry
763 #define	sd_update_block_info		ssd_update_block_info
764 #define	sd_swap_efi_gpt			ssd_swap_efi_gpt
765 #define	sd_swap_efi_gpe			ssd_swap_efi_gpe
766 #define	sd_validate_efi			ssd_validate_efi
767 #define	sd_use_efi			ssd_use_efi
768 #define	sd_uselabel			ssd_uselabel
769 #define	sd_build_default_label		ssd_build_default_label
770 #define	sd_has_max_chs_vals		ssd_has_max_chs_vals
771 #define	sd_inq_fill			ssd_inq_fill
772 #define	sd_register_devid		ssd_register_devid
773 #define	sd_get_devid_block		ssd_get_devid_block
774 #define	sd_get_devid			ssd_get_devid
775 #define	sd_create_devid			ssd_create_devid
776 #define	sd_write_deviceid		ssd_write_deviceid
777 #define	sd_check_vpd_page_support	ssd_check_vpd_page_support
778 #define	sd_setup_pm			ssd_setup_pm
779 #define	sd_create_pm_components		ssd_create_pm_components
780 #define	sd_ddi_suspend			ssd_ddi_suspend
781 #define	sd_ddi_pm_suspend		ssd_ddi_pm_suspend
782 #define	sd_ddi_resume			ssd_ddi_resume
783 #define	sd_ddi_pm_resume		ssd_ddi_pm_resume
784 #define	sdpower				ssdpower
785 #define	sdattach			ssdattach
786 #define	sddetach			ssddetach
787 #define	sd_unit_attach			ssd_unit_attach
788 #define	sd_unit_detach			ssd_unit_detach
789 #define	sd_create_minor_nodes		ssd_create_minor_nodes
790 #define	sd_create_errstats		ssd_create_errstats
791 #define	sd_set_errstats			ssd_set_errstats
792 #define	sd_set_pstats			ssd_set_pstats
793 #define	sddump				ssddump
794 #define	sd_scsi_poll			ssd_scsi_poll
795 #define	sd_send_polled_RQS		ssd_send_polled_RQS
796 #define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
797 #define	sd_init_event_callbacks		ssd_init_event_callbacks
798 #define	sd_event_callback		ssd_event_callback
799 #define	sd_disable_caching		ssd_disable_caching
800 #define	sd_make_device			ssd_make_device
801 #define	sdopen				ssdopen
802 #define	sdclose				ssdclose
803 #define	sd_ready_and_valid		ssd_ready_and_valid
804 #define	sdmin				ssdmin
805 #define	sdread				ssdread
806 #define	sdwrite				ssdwrite
807 #define	sdaread				ssdaread
808 #define	sdawrite			ssdawrite
809 #define	sdstrategy			ssdstrategy
810 #define	sdioctl				ssdioctl
811 #define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
812 #define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
813 #define	sd_checksum_iostart		ssd_checksum_iostart
814 #define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
815 #define	sd_pm_iostart			ssd_pm_iostart
816 #define	sd_core_iostart			ssd_core_iostart
817 #define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
818 #define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
819 #define	sd_checksum_iodone		ssd_checksum_iodone
820 #define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
821 #define	sd_pm_iodone			ssd_pm_iodone
822 #define	sd_initpkt_for_buf		ssd_initpkt_for_buf
823 #define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
824 #define	sd_setup_rw_pkt			ssd_setup_rw_pkt
825 #define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
826 #define	sd_buf_iodone			ssd_buf_iodone
827 #define	sd_uscsi_strategy		ssd_uscsi_strategy
828 #define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
829 #define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
830 #define	sd_uscsi_iodone			ssd_uscsi_iodone
831 #define	sd_xbuf_strategy		ssd_xbuf_strategy
832 #define	sd_xbuf_init			ssd_xbuf_init
833 #define	sd_pm_entry			ssd_pm_entry
834 #define	sd_pm_exit			ssd_pm_exit
835 
836 #define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
837 #define	sd_pm_timeout_handler		ssd_pm_timeout_handler
838 
839 #define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
840 #define	sdintr				ssdintr
841 #define	sd_start_cmds			ssd_start_cmds
842 #define	sd_send_scsi_cmd		ssd_send_scsi_cmd
843 #define	sd_bioclone_alloc		ssd_bioclone_alloc
844 #define	sd_bioclone_free		ssd_bioclone_free
845 #define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
846 #define	sd_shadow_buf_free		ssd_shadow_buf_free
847 #define	sd_print_transport_rejected_message	\
848 					ssd_print_transport_rejected_message
849 #define	sd_retry_command		ssd_retry_command
850 #define	sd_set_retry_bp			ssd_set_retry_bp
851 #define	sd_send_request_sense_command	ssd_send_request_sense_command
852 #define	sd_start_retry_command		ssd_start_retry_command
853 #define	sd_start_direct_priority_command	\
854 					ssd_start_direct_priority_command
855 #define	sd_return_failed_command	ssd_return_failed_command
856 #define	sd_return_failed_command_no_restart	\
857 					ssd_return_failed_command_no_restart
858 #define	sd_return_command		ssd_return_command
859 #define	sd_sync_with_callback		ssd_sync_with_callback
860 #define	sdrunout			ssdrunout
861 #define	sd_mark_rqs_busy		ssd_mark_rqs_busy
862 #define	sd_mark_rqs_idle		ssd_mark_rqs_idle
863 #define	sd_reduce_throttle		ssd_reduce_throttle
864 #define	sd_restore_throttle		ssd_restore_throttle
865 #define	sd_print_incomplete_msg		ssd_print_incomplete_msg
866 #define	sd_init_cdb_limits		ssd_init_cdb_limits
867 #define	sd_pkt_status_good		ssd_pkt_status_good
868 #define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
869 #define	sd_pkt_status_busy		ssd_pkt_status_busy
870 #define	sd_pkt_status_reservation_conflict	\
871 					ssd_pkt_status_reservation_conflict
872 #define	sd_pkt_status_qfull		ssd_pkt_status_qfull
873 #define	sd_handle_request_sense		ssd_handle_request_sense
874 #define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
875 #define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
876 #define	sd_validate_sense_data		ssd_validate_sense_data
877 #define	sd_decode_sense			ssd_decode_sense
878 #define	sd_print_sense_msg		ssd_print_sense_msg
879 #define	sd_extract_sense_info_descr	ssd_extract_sense_info_descr
880 #define	sd_sense_key_no_sense		ssd_sense_key_no_sense
881 #define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
882 #define	sd_sense_key_not_ready		ssd_sense_key_not_ready
883 #define	sd_sense_key_medium_or_hardware_error	\
884 					ssd_sense_key_medium_or_hardware_error
885 #define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
886 #define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
887 #define	sd_sense_key_fail_command	ssd_sense_key_fail_command
888 #define	sd_sense_key_blank_check	ssd_sense_key_blank_check
889 #define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
890 #define	sd_sense_key_default		ssd_sense_key_default
891 #define	sd_print_retry_msg		ssd_print_retry_msg
892 #define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
893 #define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
894 #define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
895 #define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
896 #define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
897 #define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
898 #define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
899 #define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
900 #define	sd_pkt_reason_default		ssd_pkt_reason_default
901 #define	sd_reset_target			ssd_reset_target
902 #define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
903 #define	sd_start_stop_unit_task		ssd_start_stop_unit_task
904 #define	sd_taskq_create			ssd_taskq_create
905 #define	sd_taskq_delete			ssd_taskq_delete
906 #define	sd_media_change_task		ssd_media_change_task
907 #define	sd_handle_mchange		ssd_handle_mchange
908 #define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
909 #define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
910 #define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
911 #define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
912 #define	sd_send_scsi_feature_GET_CONFIGURATION	\
913 					sd_send_scsi_feature_GET_CONFIGURATION
914 #define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
915 #define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
916 #define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
917 #define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
918 					ssd_send_scsi_PERSISTENT_RESERVE_IN
919 #define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
920 					ssd_send_scsi_PERSISTENT_RESERVE_OUT
921 #define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
922 #define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
923 #define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
924 #define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
925 #define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
926 #define	sd_alloc_rqs			ssd_alloc_rqs
927 #define	sd_free_rqs			ssd_free_rqs
928 #define	sd_dump_memory			ssd_dump_memory
929 #define	sd_uscsi_ioctl			ssd_uscsi_ioctl
930 #define	sd_get_media_info		ssd_get_media_info
931 #define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
932 #define	sd_dkio_get_geometry		ssd_dkio_get_geometry
933 #define	sd_dkio_set_geometry		ssd_dkio_set_geometry
934 #define	sd_dkio_get_partition		ssd_dkio_get_partition
935 #define	sd_dkio_set_partition		ssd_dkio_set_partition
936 #define	sd_dkio_partition		ssd_dkio_partition
937 #define	sd_dkio_get_vtoc		ssd_dkio_get_vtoc
938 #define	sd_dkio_get_efi			ssd_dkio_get_efi
939 #define	sd_build_user_vtoc		ssd_build_user_vtoc
940 #define	sd_dkio_set_vtoc		ssd_dkio_set_vtoc
941 #define	sd_dkio_set_efi			ssd_dkio_set_efi
942 #define	sd_build_label_vtoc		ssd_build_label_vtoc
943 #define	sd_write_label			ssd_write_label
944 #define	sd_clear_vtoc			ssd_clear_vtoc
945 #define	sd_clear_efi			ssd_clear_efi
946 #define	sd_fill_scsi1_lun		ssd_fill_scsi1_lun
947 #define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
948 #define	sd_setup_next_xfer		ssd_setup_next_xfer
949 #define	sd_dkio_get_temp		ssd_dkio_get_temp
950 #define	sd_dkio_get_mboot		ssd_dkio_get_mboot
951 #define	sd_dkio_set_mboot		ssd_dkio_set_mboot
952 #define	sd_setup_default_geometry	ssd_setup_default_geometry
953 #define	sd_update_fdisk_and_vtoc	ssd_update_fdisk_and_vtoc
954 #define	sd_check_mhd			ssd_check_mhd
955 #define	sd_mhd_watch_cb			ssd_mhd_watch_cb
956 #define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
957 #define	sd_sname			ssd_sname
958 #define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
959 #define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
960 #define	sd_take_ownership		ssd_take_ownership
961 #define	sd_reserve_release		ssd_reserve_release
962 #define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
963 #define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
964 #define	sd_persistent_reservation_in_read_keys	\
965 					ssd_persistent_reservation_in_read_keys
966 #define	sd_persistent_reservation_in_read_resv	\
967 					ssd_persistent_reservation_in_read_resv
968 #define	sd_mhdioc_takeown		ssd_mhdioc_takeown
969 #define	sd_mhdioc_failfast		ssd_mhdioc_failfast
970 #define	sd_mhdioc_release		ssd_mhdioc_release
971 #define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
972 #define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
973 #define	sd_mhdioc_inresv		ssd_mhdioc_inresv
974 #define	sr_change_blkmode		ssr_change_blkmode
975 #define	sr_change_speed			ssr_change_speed
976 #define	sr_atapi_change_speed		ssr_atapi_change_speed
977 #define	sr_pause_resume			ssr_pause_resume
978 #define	sr_play_msf			ssr_play_msf
979 #define	sr_play_trkind			ssr_play_trkind
980 #define	sr_read_all_subcodes		ssr_read_all_subcodes
981 #define	sr_read_subchannel		ssr_read_subchannel
982 #define	sr_read_tocentry		ssr_read_tocentry
983 #define	sr_read_tochdr			ssr_read_tochdr
984 #define	sr_read_cdda			ssr_read_cdda
985 #define	sr_read_cdxa			ssr_read_cdxa
986 #define	sr_read_mode1			ssr_read_mode1
987 #define	sr_read_mode2			ssr_read_mode2
988 #define	sr_read_cd_mode2		ssr_read_cd_mode2
989 #define	sr_sector_mode			ssr_sector_mode
990 #define	sr_eject			ssr_eject
991 #define	sr_ejected			ssr_ejected
992 #define	sr_check_wp			ssr_check_wp
993 #define	sd_check_media			ssd_check_media
994 #define	sd_media_watch_cb		ssd_media_watch_cb
995 #define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
996 #define	sr_volume_ctrl			ssr_volume_ctrl
997 #define	sr_read_sony_session_offset	ssr_read_sony_session_offset
998 #define	sd_log_page_supported		ssd_log_page_supported
999 #define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1000 #define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1001 #define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1002 #define	sd_range_lock			ssd_range_lock
1003 #define	sd_get_range			ssd_get_range
1004 #define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1005 #define	sd_range_unlock			ssd_range_unlock
1006 #define	sd_read_modify_write_task	ssd_read_modify_write_task
1007 #define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1008 
1009 #define	sd_iostart_chain		ssd_iostart_chain
1010 #define	sd_iodone_chain			ssd_iodone_chain
1011 #define	sd_initpkt_map			ssd_initpkt_map
1012 #define	sd_destroypkt_map		ssd_destroypkt_map
1013 #define	sd_chain_type_map		ssd_chain_type_map
1014 #define	sd_chain_index_map		ssd_chain_index_map
1015 
1016 #define	sd_failfast_flushctl		ssd_failfast_flushctl
1017 #define	sd_failfast_flushq		ssd_failfast_flushq
1018 #define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1019 
1020 #endif	/* #if (defined(__fibre)) */
1021 
1022 
1023 int _init(void);
1024 int _fini(void);
1025 int _info(struct modinfo *modinfop);
1026 
1027 /*PRINTFLIKE3*/
1028 static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1029 /*PRINTFLIKE3*/
1030 static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1031 /*PRINTFLIKE3*/
1032 static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1033 
1034 static int sdprobe(dev_info_t *devi);
1035 static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1036     void **result);
1037 static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1038     int mod_flags, char *name, caddr_t valuep, int *lengthp);
1039 
1040 /*
1041  * Smart probe for parallel scsi
1042  */
1043 static void sd_scsi_probe_cache_init(void);
1044 static void sd_scsi_probe_cache_fini(void);
1045 static void sd_scsi_clear_probe_cache(void);
1046 static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1047 
1048 static int	sd_spin_up_unit(struct sd_lun *un);
1049 static void	sd_enable_descr_sense(struct sd_lun *un);
1050 static void	sd_set_mmc_caps(struct sd_lun *un);
1051 
1052 static void sd_fill_scsi1_lun(struct sd_lun *un, struct scsi_pkt *);
1053 static void sd_read_unit_properties(struct sd_lun *un);
1054 static int  sd_process_sdconf_file(struct sd_lun *un);
1055 static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1056     int *data_list, sd_tunables *values);
1057 static void sd_process_sdconf_table(struct sd_lun *un);
1058 static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1059 static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1060 static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1061 	int list_len, char *dataname_ptr);
1062 static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1063     sd_tunables *prop_list);
1064 static int  sd_validate_geometry(struct sd_lun *un, int path_flag);
1065 
1066 #if defined(_SUNOS_VTOC_16)
1067 static void sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g);
1068 #endif
1069 
1070 static void sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize,
1071 	int path_flag);
1072 static int  sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize,
1073 	int path_flag);
1074 static void sd_get_physical_geometry(struct sd_lun *un,
1075 	struct geom_cache *pgeom_p, int capacity, int lbasize, int path_flag);
1076 static void sd_get_virtual_geometry(struct sd_lun *un, int capacity,
1077 	int lbasize);
1078 static int  sd_uselabel(struct sd_lun *un, struct dk_label *l, int path_flag);
1079 static void sd_swap_efi_gpt(efi_gpt_t *);
1080 static void sd_swap_efi_gpe(int nparts, efi_gpe_t *);
1081 static int sd_validate_efi(efi_gpt_t *);
1082 static int sd_use_efi(struct sd_lun *, int);
1083 static void sd_build_default_label(struct sd_lun *un);
1084 
1085 #if defined(_FIRMWARE_NEEDS_FDISK)
1086 static int  sd_has_max_chs_vals(struct ipart *fdp);
1087 #endif
1088 static void sd_inq_fill(char *p, int l, char *s);
1089 
1090 
1091 static void sd_register_devid(struct sd_lun *un, dev_info_t *devi,
1092     int reservation_flag);
1093 static daddr_t  sd_get_devid_block(struct sd_lun *un);
1094 static int  sd_get_devid(struct sd_lun *un);
1095 static int  sd_get_serialnum(struct sd_lun *un, uchar_t *wwn, int *len);
1096 static ddi_devid_t sd_create_devid(struct sd_lun *un);
1097 static int  sd_write_deviceid(struct sd_lun *un);
1098 static int  sd_get_devid_page(struct sd_lun *un, uchar_t *wwn, int *len);
1099 static int  sd_check_vpd_page_support(struct sd_lun *un);
1100 
1101 static void sd_setup_pm(struct sd_lun *un, dev_info_t *devi);
1102 static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1103 
1104 static int  sd_ddi_suspend(dev_info_t *devi);
1105 static int  sd_ddi_pm_suspend(struct sd_lun *un);
1106 static int  sd_ddi_resume(dev_info_t *devi);
1107 static int  sd_ddi_pm_resume(struct sd_lun *un);
1108 static int  sdpower(dev_info_t *devi, int component, int level);
1109 
1110 static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1111 static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1112 static int  sd_unit_attach(dev_info_t *devi);
1113 static int  sd_unit_detach(dev_info_t *devi);
1114 
1115 static int  sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi);
1116 static void sd_create_errstats(struct sd_lun *un, int instance);
1117 static void sd_set_errstats(struct sd_lun *un);
1118 static void sd_set_pstats(struct sd_lun *un);
1119 
1120 static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1121 static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1122 static int  sd_send_polled_RQS(struct sd_lun *un);
1123 static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1124 
1125 #if (defined(__fibre))
1126 /*
1127  * Event callbacks (photon)
1128  */
1129 static void sd_init_event_callbacks(struct sd_lun *un);
1130 static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1131 #endif
1132 
1133 
1134 static int   sd_disable_caching(struct sd_lun *un);
1135 static dev_t sd_make_device(dev_info_t *devi);
1136 
1137 static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1138 	uint64_t capacity);
1139 
1140 /*
1141  * Driver entry point functions.
1142  */
1143 static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1144 static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1145 static int  sd_ready_and_valid(struct sd_lun *un);
1146 
1147 static void sdmin(struct buf *bp);
1148 static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1149 static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1150 static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1151 static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1152 
1153 static int sdstrategy(struct buf *bp);
1154 static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1155 
1156 /*
1157  * Function prototypes for layering functions in the iostart chain.
1158  */
1159 static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1160 	struct buf *bp);
1161 static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1162 	struct buf *bp);
1163 static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1164 static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1165 	struct buf *bp);
1166 static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1167 static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1168 
1169 /*
1170  * Function prototypes for layering functions in the iodone chain.
1171  */
1172 static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1173 static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1174 static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1175 	struct buf *bp);
1176 static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1177 	struct buf *bp);
1178 static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1179 static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1180 	struct buf *bp);
1181 static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1182 
1183 /*
1184  * Prototypes for functions to support buf(9S) based IO.
1185  */
1186 static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1187 static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1188 static void sd_destroypkt_for_buf(struct buf *);
1189 static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1190 	struct buf *bp, int flags,
1191 	int (*callback)(caddr_t), caddr_t callback_arg,
1192 	diskaddr_t lba, uint32_t blockcount);
1193 static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1194 	struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1195 
1196 /*
1197  * Prototypes for functions to support USCSI IO.
1198  */
1199 static int sd_uscsi_strategy(struct buf *bp);
1200 static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1201 static void sd_destroypkt_for_uscsi(struct buf *);
1202 
1203 static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1204 	uchar_t chain_type, void *pktinfop);
1205 
1206 static int  sd_pm_entry(struct sd_lun *un);
1207 static void sd_pm_exit(struct sd_lun *un);
1208 
1209 static void sd_pm_idletimeout_handler(void *arg);
1210 
1211 /*
1212  * sd_core internal functions (used at the sd_core_io layer).
1213  */
1214 static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1215 static void sdintr(struct scsi_pkt *pktp);
1216 static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1217 
1218 static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd,
1219 	enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace,
1220 	int path_flag);
1221 
1222 static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1223 	daddr_t blkno, int (*func)(struct buf *));
1224 static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1225 	uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1226 static void sd_bioclone_free(struct buf *bp);
1227 static void sd_shadow_buf_free(struct buf *bp);
1228 
1229 static void sd_print_transport_rejected_message(struct sd_lun *un,
1230 	struct sd_xbuf *xp, int code);
1231 
1232 static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1233 	int retry_check_flag,
1234 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp,
1235 		int c),
1236 	void *user_arg, int failure_code,  clock_t retry_delay,
1237 	void (*statp)(kstat_io_t *));
1238 
1239 static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1240 	clock_t retry_delay, void (*statp)(kstat_io_t *));
1241 
1242 static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1243 	struct scsi_pkt *pktp);
1244 static void sd_start_retry_command(void *arg);
1245 static void sd_start_direct_priority_command(void *arg);
1246 static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1247 	int errcode);
1248 static void sd_return_failed_command_no_restart(struct sd_lun *un,
1249 	struct buf *bp, int errcode);
1250 static void sd_return_command(struct sd_lun *un, struct buf *bp);
1251 static void sd_sync_with_callback(struct sd_lun *un);
1252 static int sdrunout(caddr_t arg);
1253 
1254 static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1255 static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1256 
1257 static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1258 static void sd_restore_throttle(void *arg);
1259 
1260 static void sd_init_cdb_limits(struct sd_lun *un);
1261 
1262 static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1263 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1264 
1265 /*
1266  * Error handling functions
1267  */
1268 static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1269 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1270 static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1271 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1272 static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1273 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1274 static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1275 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1276 
1277 static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1278 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1279 static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1280 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1281 static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1282 	struct sd_xbuf *xp);
1283 static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1284 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1285 
1286 static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1287 	void *arg, int code);
1288 static diskaddr_t sd_extract_sense_info_descr(
1289 	struct scsi_descr_sense_hdr *sdsp);
1290 
1291 static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1292 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1293 static void sd_sense_key_recoverable_error(struct sd_lun *un,
1294 	uint8_t asc,
1295 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1296 static void sd_sense_key_not_ready(struct sd_lun *un,
1297 	uint8_t asc, uint8_t ascq,
1298 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1299 static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1300 	int sense_key, uint8_t asc,
1301 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1302 static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1303 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1304 static void sd_sense_key_unit_attention(struct sd_lun *un,
1305 	uint8_t asc,
1306 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1307 static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1308 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1309 static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1310 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1311 static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1312 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1313 static void sd_sense_key_default(struct sd_lun *un,
1314 	int sense_key,
1315 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1316 
1317 static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1318 	void *arg, int flag);
1319 
1320 static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1321 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1322 static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1323 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1324 static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1325 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1326 static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1327 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1328 static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1329 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1330 static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1331 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1332 static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1333 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1334 static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1335 	struct sd_xbuf *xp, struct scsi_pkt *pktp);
1336 
1337 static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1338 
1339 static void sd_start_stop_unit_callback(void *arg);
1340 static void sd_start_stop_unit_task(void *arg);
1341 
1342 static void sd_taskq_create(void);
1343 static void sd_taskq_delete(void);
1344 static void sd_media_change_task(void *arg);
1345 
1346 static int sd_handle_mchange(struct sd_lun *un);
1347 static int sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag);
1348 static int sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp,
1349 	uint32_t *lbap, int path_flag);
1350 static int sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
1351 	uint32_t *lbap, int path_flag);
1352 static int sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag,
1353 	int path_flag);
1354 static int sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr,
1355 	size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1356 static int sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag);
1357 static int sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un,
1358 	uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1359 static int sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un,
1360 	uchar_t usr_cmd, uchar_t *usr_bufp);
1361 static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un);
1362 static int sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un,
1363 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1364 	uchar_t *bufaddr, uint_t buflen);
1365 static int sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
1366 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1367 	uchar_t *bufaddr, uint_t buflen, char feature);
1368 static int sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize,
1369 	uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1370 static int sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize,
1371 	uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1372 static int sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
1373 	size_t buflen, daddr_t start_block, int path_flag);
1374 #define	sd_send_scsi_READ(un, bufaddr, buflen, start_block, path_flag)	\
1375 	sd_send_scsi_RDWR(un, SCMD_READ, bufaddr, buflen, start_block, \
1376 	path_flag)
1377 #define	sd_send_scsi_WRITE(un, bufaddr, buflen, start_block, path_flag)	\
1378 	sd_send_scsi_RDWR(un, SCMD_WRITE, bufaddr, buflen, start_block,\
1379 	path_flag)
1380 
1381 static int sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr,
1382 	uint16_t buflen, uchar_t page_code, uchar_t page_control,
1383 	uint16_t param_ptr, int path_flag);
1384 
1385 static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1386 static void sd_free_rqs(struct sd_lun *un);
1387 
1388 static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1389 	uchar_t *data, int len, int fmt);
1390 
1391 /*
1392  * Disk Ioctl Function Prototypes
1393  */
1394 static int sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag);
1395 static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1396 static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1397 static int sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag,
1398 	int geom_validated);
1399 static int sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag);
1400 static int sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag,
1401 	int geom_validated);
1402 static int sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag);
1403 static int sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag,
1404 	int geom_validated);
1405 static int sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag);
1406 static int sd_dkio_partition(dev_t dev, caddr_t arg, int flag);
1407 static void sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1408 static int sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag);
1409 static int sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag);
1410 static int sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc);
1411 static int sd_write_label(dev_t dev);
1412 static int sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl);
1413 static void sd_clear_vtoc(struct sd_lun *un);
1414 static void sd_clear_efi(struct sd_lun *un);
1415 static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1416 static int sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag);
1417 static int sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag);
1418 static void sd_setup_default_geometry(struct sd_lun *un);
1419 #if defined(__i386) || defined(__amd64)
1420 static int sd_update_fdisk_and_vtoc(struct sd_lun *un);
1421 #endif
1422 
1423 /*
1424  * Multi-host Ioctl Prototypes
1425  */
1426 static int sd_check_mhd(dev_t dev, int interval);
1427 static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1428 static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1429 static char *sd_sname(uchar_t status);
1430 static void sd_mhd_resvd_recover(void *arg);
1431 static void sd_resv_reclaim_thread();
1432 static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1433 static int sd_reserve_release(dev_t dev, int cmd);
1434 static void sd_rmv_resv_reclaim_req(dev_t dev);
1435 static void sd_mhd_reset_notify_cb(caddr_t arg);
1436 static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1437 	mhioc_inkeys_t *usrp, int flag);
1438 static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1439 	mhioc_inresvs_t *usrp, int flag);
1440 static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1441 static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1442 static int sd_mhdioc_release(dev_t dev);
1443 static int sd_mhdioc_register_devid(dev_t dev);
1444 static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1445 static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1446 
1447 /*
1448  * SCSI removable prototypes
1449  */
1450 static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1451 static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1452 static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1453 static int sr_pause_resume(dev_t dev, int mode);
1454 static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1455 static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1456 static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1457 static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1458 static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1459 static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1460 static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1461 static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1462 static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1463 static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1464 static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1465 static int sr_sector_mode(dev_t dev, uint32_t blksize);
1466 static int sr_eject(dev_t dev);
1467 static void sr_ejected(register struct sd_lun *un);
1468 static int sr_check_wp(dev_t dev);
1469 static int sd_check_media(dev_t dev, enum dkio_state state);
1470 static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1471 static void sd_delayed_cv_broadcast(void *arg);
1472 static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1473 static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1474 
1475 static int sd_log_page_supported(struct sd_lun *un, int log_page);
1476 
1477 /*
1478  * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1479  */
1480 static void sd_check_for_writable_cd(struct sd_lun *un);
1481 static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1482 static void sd_wm_cache_destructor(void *wm, void *un);
1483 static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1484 	daddr_t endb, ushort_t typ);
1485 static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1486 	daddr_t endb);
1487 static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1488 static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1489 static void sd_read_modify_write_task(void * arg);
1490 static int
1491 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1492 	struct buf **bpp);
1493 
1494 
1495 /*
1496  * Function prototypes for failfast support.
1497  */
1498 static void sd_failfast_flushq(struct sd_lun *un);
1499 static int sd_failfast_flushq_callback(struct buf *bp);
1500 
1501 /*
1502  * Function prototypes for x86 support
1503  */
1504 #if defined(__i386) || defined(__amd64)
1505 static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1506 		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1507 #endif
1508 
1509 /*
1510  * Constants for failfast support:
1511  *
1512  * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1513  * failfast processing being performed.
1514  *
1515  * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1516  * failfast processing on all bufs with B_FAILFAST set.
1517  */
1518 
1519 #define	SD_FAILFAST_INACTIVE		0
1520 #define	SD_FAILFAST_ACTIVE		1
1521 
1522 /*
1523  * Bitmask to control behavior of buf(9S) flushes when a transition to
1524  * the failfast state occurs. Optional bits include:
1525  *
1526  * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1527  * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1528  * be flushed.
1529  *
1530  * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1531  * driver, in addition to the regular wait queue. This includes the xbuf
1532  * queues. When clear, only the driver's wait queue will be flushed.
1533  */
1534 #define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1535 #define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1536 
1537 /*
1538  * The default behavior is to only flush bufs that have B_FAILFAST set, but
1539  * to flush all queues within the driver.
1540  */
1541 static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1542 
1543 
1544 /*
1545  * SD Testing Fault Injection
1546  */
1547 #ifdef SD_FAULT_INJECTION
1548 static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1549 static void sd_faultinjection(struct scsi_pkt *pktp);
1550 static void sd_injection_log(char *buf, struct sd_lun *un);
1551 #endif
1552 
1553 /*
1554  * Device driver ops vector
1555  */
1556 static struct cb_ops sd_cb_ops = {
1557 	sdopen,			/* open */
1558 	sdclose,		/* close */
1559 	sdstrategy,		/* strategy */
1560 	nodev,			/* print */
1561 	sddump,			/* dump */
1562 	sdread,			/* read */
1563 	sdwrite,		/* write */
1564 	sdioctl,		/* ioctl */
1565 	nodev,			/* devmap */
1566 	nodev,			/* mmap */
1567 	nodev,			/* segmap */
1568 	nochpoll,		/* poll */
1569 	sd_prop_op,		/* cb_prop_op */
1570 	0,			/* streamtab  */
1571 	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1572 	CB_REV,			/* cb_rev */
1573 	sdaread, 		/* async I/O read entry point */
1574 	sdawrite		/* async I/O write entry point */
1575 };
1576 
1577 static struct dev_ops sd_ops = {
1578 	DEVO_REV,		/* devo_rev, */
1579 	0,			/* refcnt  */
1580 	sdinfo,			/* info */
1581 	nulldev,		/* identify */
1582 	sdprobe,		/* probe */
1583 	sdattach,		/* attach */
1584 	sddetach,		/* detach */
1585 	nodev,			/* reset */
1586 	&sd_cb_ops,		/* driver operations */
1587 	NULL,			/* bus operations */
1588 	sdpower			/* power */
1589 };
1590 
1591 
1592 /*
1593  * This is the loadable module wrapper.
1594  */
1595 #include <sys/modctl.h>
1596 
1597 static struct modldrv modldrv = {
1598 	&mod_driverops,		/* Type of module. This one is a driver */
1599 	SD_MODULE_NAME,		/* Module name. */
1600 	&sd_ops			/* driver ops */
1601 };
1602 
1603 
1604 static struct modlinkage modlinkage = {
1605 	MODREV_1,
1606 	&modldrv,
1607 	NULL
1608 };
1609 
1610 
1611 static struct scsi_asq_key_strings sd_additional_codes[] = {
1612 	0x81, 0, "Logical Unit is Reserved",
1613 	0x85, 0, "Audio Address Not Valid",
1614 	0xb6, 0, "Media Load Mechanism Failed",
1615 	0xB9, 0, "Audio Play Operation Aborted",
1616 	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1617 	0x53, 2, "Medium removal prevented",
1618 	0x6f, 0, "Authentication failed during key exchange",
1619 	0x6f, 1, "Key not present",
1620 	0x6f, 2, "Key not established",
1621 	0x6f, 3, "Read without proper authentication",
1622 	0x6f, 4, "Mismatched region to this logical unit",
1623 	0x6f, 5, "Region reset count error",
1624 	0xffff, 0x0, NULL
1625 };
1626 
1627 
1628 /*
1629  * Struct for passing printing information for sense data messages
1630  */
1631 struct sd_sense_info {
1632 	int	ssi_severity;
1633 	int	ssi_pfa_flag;
1634 };
1635 
1636 /*
1637  * Table of function pointers for iostart-side routines. Seperate "chains"
1638  * of layered function calls are formed by placing the function pointers
1639  * sequentially in the desired order. Functions are called according to an
1640  * incrementing table index ordering. The last function in each chain must
1641  * be sd_core_iostart(). The corresponding iodone-side routines are expected
1642  * in the sd_iodone_chain[] array.
1643  *
1644  * Note: It may seem more natural to organize both the iostart and iodone
1645  * functions together, into an array of structures (or some similar
1646  * organization) with a common index, rather than two seperate arrays which
1647  * must be maintained in synchronization. The purpose of this division is
1648  * to achiece improved performance: individual arrays allows for more
1649  * effective cache line utilization on certain platforms.
1650  */
1651 
1652 typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1653 
1654 
1655 static sd_chain_t sd_iostart_chain[] = {
1656 
1657 	/* Chain for buf IO for disk drive targets (PM enabled) */
1658 	sd_mapblockaddr_iostart,	/* Index: 0 */
1659 	sd_pm_iostart,			/* Index: 1 */
1660 	sd_core_iostart,		/* Index: 2 */
1661 
1662 	/* Chain for buf IO for disk drive targets (PM disabled) */
1663 	sd_mapblockaddr_iostart,	/* Index: 3 */
1664 	sd_core_iostart,		/* Index: 4 */
1665 
1666 	/* Chain for buf IO for removable-media targets (PM enabled) */
1667 	sd_mapblockaddr_iostart,	/* Index: 5 */
1668 	sd_mapblocksize_iostart,	/* Index: 6 */
1669 	sd_pm_iostart,			/* Index: 7 */
1670 	sd_core_iostart,		/* Index: 8 */
1671 
1672 	/* Chain for buf IO for removable-media targets (PM disabled) */
1673 	sd_mapblockaddr_iostart,	/* Index: 9 */
1674 	sd_mapblocksize_iostart,	/* Index: 10 */
1675 	sd_core_iostart,		/* Index: 11 */
1676 
1677 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1678 	sd_mapblockaddr_iostart,	/* Index: 12 */
1679 	sd_checksum_iostart,		/* Index: 13 */
1680 	sd_pm_iostart,			/* Index: 14 */
1681 	sd_core_iostart,		/* Index: 15 */
1682 
1683 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1684 	sd_mapblockaddr_iostart,	/* Index: 16 */
1685 	sd_checksum_iostart,		/* Index: 17 */
1686 	sd_core_iostart,		/* Index: 18 */
1687 
1688 	/* Chain for USCSI commands (all targets) */
1689 	sd_pm_iostart,			/* Index: 19 */
1690 	sd_core_iostart,		/* Index: 20 */
1691 
1692 	/* Chain for checksumming USCSI commands (all targets) */
1693 	sd_checksum_uscsi_iostart,	/* Index: 21 */
1694 	sd_pm_iostart,			/* Index: 22 */
1695 	sd_core_iostart,		/* Index: 23 */
1696 
1697 	/* Chain for "direct" USCSI commands (all targets) */
1698 	sd_core_iostart,		/* Index: 24 */
1699 
1700 	/* Chain for "direct priority" USCSI commands (all targets) */
1701 	sd_core_iostart,		/* Index: 25 */
1702 };
1703 
1704 /*
1705  * Macros to locate the first function of each iostart chain in the
1706  * sd_iostart_chain[] array. These are located by the index in the array.
1707  */
1708 #define	SD_CHAIN_DISK_IOSTART			0
1709 #define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1710 #define	SD_CHAIN_RMMEDIA_IOSTART		5
1711 #define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1712 #define	SD_CHAIN_CHKSUM_IOSTART			12
1713 #define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1714 #define	SD_CHAIN_USCSI_CMD_IOSTART		19
1715 #define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1716 #define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1717 #define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1718 
1719 
1720 /*
1721  * Table of function pointers for the iodone-side routines for the driver-
1722  * internal layering mechanism.  The calling sequence for iodone routines
1723  * uses a decrementing table index, so the last routine called in a chain
1724  * must be at the lowest array index location for that chain.  The last
1725  * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1726  * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1727  * of the functions in an iodone side chain must correspond to the ordering
1728  * of the iostart routines for that chain.  Note that there is no iodone
1729  * side routine that corresponds to sd_core_iostart(), so there is no
1730  * entry in the table for this.
1731  */
1732 
1733 static sd_chain_t sd_iodone_chain[] = {
1734 
1735 	/* Chain for buf IO for disk drive targets (PM enabled) */
1736 	sd_buf_iodone,			/* Index: 0 */
1737 	sd_mapblockaddr_iodone,		/* Index: 1 */
1738 	sd_pm_iodone,			/* Index: 2 */
1739 
1740 	/* Chain for buf IO for disk drive targets (PM disabled) */
1741 	sd_buf_iodone,			/* Index: 3 */
1742 	sd_mapblockaddr_iodone,		/* Index: 4 */
1743 
1744 	/* Chain for buf IO for removable-media targets (PM enabled) */
1745 	sd_buf_iodone,			/* Index: 5 */
1746 	sd_mapblockaddr_iodone,		/* Index: 6 */
1747 	sd_mapblocksize_iodone,		/* Index: 7 */
1748 	sd_pm_iodone,			/* Index: 8 */
1749 
1750 	/* Chain for buf IO for removable-media targets (PM disabled) */
1751 	sd_buf_iodone,			/* Index: 9 */
1752 	sd_mapblockaddr_iodone,		/* Index: 10 */
1753 	sd_mapblocksize_iodone,		/* Index: 11 */
1754 
1755 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1756 	sd_buf_iodone,			/* Index: 12 */
1757 	sd_mapblockaddr_iodone,		/* Index: 13 */
1758 	sd_checksum_iodone,		/* Index: 14 */
1759 	sd_pm_iodone,			/* Index: 15 */
1760 
1761 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1762 	sd_buf_iodone,			/* Index: 16 */
1763 	sd_mapblockaddr_iodone,		/* Index: 17 */
1764 	sd_checksum_iodone,		/* Index: 18 */
1765 
1766 	/* Chain for USCSI commands (non-checksum targets) */
1767 	sd_uscsi_iodone,		/* Index: 19 */
1768 	sd_pm_iodone,			/* Index: 20 */
1769 
1770 	/* Chain for USCSI commands (checksum targets) */
1771 	sd_uscsi_iodone,		/* Index: 21 */
1772 	sd_checksum_uscsi_iodone,	/* Index: 22 */
1773 	sd_pm_iodone,			/* Index: 22 */
1774 
1775 	/* Chain for "direct" USCSI commands (all targets) */
1776 	sd_uscsi_iodone,		/* Index: 24 */
1777 
1778 	/* Chain for "direct priority" USCSI commands (all targets) */
1779 	sd_uscsi_iodone,		/* Index: 25 */
1780 };
1781 
1782 
1783 /*
1784  * Macros to locate the "first" function in the sd_iodone_chain[] array for
1785  * each iodone-side chain. These are located by the array index, but as the
1786  * iodone side functions are called in a decrementing-index order, the
1787  * highest index number in each chain must be specified (as these correspond
1788  * to the first function in the iodone chain that will be called by the core
1789  * at IO completion time).
1790  */
1791 
1792 #define	SD_CHAIN_DISK_IODONE			2
1793 #define	SD_CHAIN_DISK_IODONE_NO_PM		4
1794 #define	SD_CHAIN_RMMEDIA_IODONE			8
1795 #define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
1796 #define	SD_CHAIN_CHKSUM_IODONE			15
1797 #define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
1798 #define	SD_CHAIN_USCSI_CMD_IODONE		20
1799 #define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
1800 #define	SD_CHAIN_DIRECT_CMD_IODONE		24
1801 #define	SD_CHAIN_PRIORITY_CMD_IODONE		25
1802 
1803 
1804 
1805 
1806 /*
1807  * Array to map a layering chain index to the appropriate initpkt routine.
1808  * The redundant entries are present so that the index used for accessing
1809  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1810  * with this table as well.
1811  */
1812 typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
1813 
1814 static sd_initpkt_t	sd_initpkt_map[] = {
1815 
1816 	/* Chain for buf IO for disk drive targets (PM enabled) */
1817 	sd_initpkt_for_buf,		/* Index: 0 */
1818 	sd_initpkt_for_buf,		/* Index: 1 */
1819 	sd_initpkt_for_buf,		/* Index: 2 */
1820 
1821 	/* Chain for buf IO for disk drive targets (PM disabled) */
1822 	sd_initpkt_for_buf,		/* Index: 3 */
1823 	sd_initpkt_for_buf,		/* Index: 4 */
1824 
1825 	/* Chain for buf IO for removable-media targets (PM enabled) */
1826 	sd_initpkt_for_buf,		/* Index: 5 */
1827 	sd_initpkt_for_buf,		/* Index: 6 */
1828 	sd_initpkt_for_buf,		/* Index: 7 */
1829 	sd_initpkt_for_buf,		/* Index: 8 */
1830 
1831 	/* Chain for buf IO for removable-media targets (PM disabled) */
1832 	sd_initpkt_for_buf,		/* Index: 9 */
1833 	sd_initpkt_for_buf,		/* Index: 10 */
1834 	sd_initpkt_for_buf,		/* Index: 11 */
1835 
1836 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1837 	sd_initpkt_for_buf,		/* Index: 12 */
1838 	sd_initpkt_for_buf,		/* Index: 13 */
1839 	sd_initpkt_for_buf,		/* Index: 14 */
1840 	sd_initpkt_for_buf,		/* Index: 15 */
1841 
1842 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1843 	sd_initpkt_for_buf,		/* Index: 16 */
1844 	sd_initpkt_for_buf,		/* Index: 17 */
1845 	sd_initpkt_for_buf,		/* Index: 18 */
1846 
1847 	/* Chain for USCSI commands (non-checksum targets) */
1848 	sd_initpkt_for_uscsi,		/* Index: 19 */
1849 	sd_initpkt_for_uscsi,		/* Index: 20 */
1850 
1851 	/* Chain for USCSI commands (checksum targets) */
1852 	sd_initpkt_for_uscsi,		/* Index: 21 */
1853 	sd_initpkt_for_uscsi,		/* Index: 22 */
1854 	sd_initpkt_for_uscsi,		/* Index: 22 */
1855 
1856 	/* Chain for "direct" USCSI commands (all targets) */
1857 	sd_initpkt_for_uscsi,		/* Index: 24 */
1858 
1859 	/* Chain for "direct priority" USCSI commands (all targets) */
1860 	sd_initpkt_for_uscsi,		/* Index: 25 */
1861 
1862 };
1863 
1864 
1865 /*
1866  * Array to map a layering chain index to the appropriate destroypktpkt routine.
1867  * The redundant entries are present so that the index used for accessing
1868  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1869  * with this table as well.
1870  */
1871 typedef void (*sd_destroypkt_t)(struct buf *);
1872 
1873 static sd_destroypkt_t	sd_destroypkt_map[] = {
1874 
1875 	/* Chain for buf IO for disk drive targets (PM enabled) */
1876 	sd_destroypkt_for_buf,		/* Index: 0 */
1877 	sd_destroypkt_for_buf,		/* Index: 1 */
1878 	sd_destroypkt_for_buf,		/* Index: 2 */
1879 
1880 	/* Chain for buf IO for disk drive targets (PM disabled) */
1881 	sd_destroypkt_for_buf,		/* Index: 3 */
1882 	sd_destroypkt_for_buf,		/* Index: 4 */
1883 
1884 	/* Chain for buf IO for removable-media targets (PM enabled) */
1885 	sd_destroypkt_for_buf,		/* Index: 5 */
1886 	sd_destroypkt_for_buf,		/* Index: 6 */
1887 	sd_destroypkt_for_buf,		/* Index: 7 */
1888 	sd_destroypkt_for_buf,		/* Index: 8 */
1889 
1890 	/* Chain for buf IO for removable-media targets (PM disabled) */
1891 	sd_destroypkt_for_buf,		/* Index: 9 */
1892 	sd_destroypkt_for_buf,		/* Index: 10 */
1893 	sd_destroypkt_for_buf,		/* Index: 11 */
1894 
1895 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1896 	sd_destroypkt_for_buf,		/* Index: 12 */
1897 	sd_destroypkt_for_buf,		/* Index: 13 */
1898 	sd_destroypkt_for_buf,		/* Index: 14 */
1899 	sd_destroypkt_for_buf,		/* Index: 15 */
1900 
1901 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1902 	sd_destroypkt_for_buf,		/* Index: 16 */
1903 	sd_destroypkt_for_buf,		/* Index: 17 */
1904 	sd_destroypkt_for_buf,		/* Index: 18 */
1905 
1906 	/* Chain for USCSI commands (non-checksum targets) */
1907 	sd_destroypkt_for_uscsi,	/* Index: 19 */
1908 	sd_destroypkt_for_uscsi,	/* Index: 20 */
1909 
1910 	/* Chain for USCSI commands (checksum targets) */
1911 	sd_destroypkt_for_uscsi,	/* Index: 21 */
1912 	sd_destroypkt_for_uscsi,	/* Index: 22 */
1913 	sd_destroypkt_for_uscsi,	/* Index: 22 */
1914 
1915 	/* Chain for "direct" USCSI commands (all targets) */
1916 	sd_destroypkt_for_uscsi,	/* Index: 24 */
1917 
1918 	/* Chain for "direct priority" USCSI commands (all targets) */
1919 	sd_destroypkt_for_uscsi,	/* Index: 25 */
1920 
1921 };
1922 
1923 
1924 
1925 /*
1926  * Array to map a layering chain index to the appropriate chain "type".
1927  * The chain type indicates a specific property/usage of the chain.
1928  * The redundant entries are present so that the index used for accessing
1929  * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
1930  * with this table as well.
1931  */
1932 
1933 #define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
1934 #define	SD_CHAIN_BUFIO			1	/* regular buf IO */
1935 #define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
1936 #define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
1937 #define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
1938 						/* (for error recovery) */
1939 
1940 static int sd_chain_type_map[] = {
1941 
1942 	/* Chain for buf IO for disk drive targets (PM enabled) */
1943 	SD_CHAIN_BUFIO,			/* Index: 0 */
1944 	SD_CHAIN_BUFIO,			/* Index: 1 */
1945 	SD_CHAIN_BUFIO,			/* Index: 2 */
1946 
1947 	/* Chain for buf IO for disk drive targets (PM disabled) */
1948 	SD_CHAIN_BUFIO,			/* Index: 3 */
1949 	SD_CHAIN_BUFIO,			/* Index: 4 */
1950 
1951 	/* Chain for buf IO for removable-media targets (PM enabled) */
1952 	SD_CHAIN_BUFIO,			/* Index: 5 */
1953 	SD_CHAIN_BUFIO,			/* Index: 6 */
1954 	SD_CHAIN_BUFIO,			/* Index: 7 */
1955 	SD_CHAIN_BUFIO,			/* Index: 8 */
1956 
1957 	/* Chain for buf IO for removable-media targets (PM disabled) */
1958 	SD_CHAIN_BUFIO,			/* Index: 9 */
1959 	SD_CHAIN_BUFIO,			/* Index: 10 */
1960 	SD_CHAIN_BUFIO,			/* Index: 11 */
1961 
1962 	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1963 	SD_CHAIN_BUFIO,			/* Index: 12 */
1964 	SD_CHAIN_BUFIO,			/* Index: 13 */
1965 	SD_CHAIN_BUFIO,			/* Index: 14 */
1966 	SD_CHAIN_BUFIO,			/* Index: 15 */
1967 
1968 	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1969 	SD_CHAIN_BUFIO,			/* Index: 16 */
1970 	SD_CHAIN_BUFIO,			/* Index: 17 */
1971 	SD_CHAIN_BUFIO,			/* Index: 18 */
1972 
1973 	/* Chain for USCSI commands (non-checksum targets) */
1974 	SD_CHAIN_USCSI,			/* Index: 19 */
1975 	SD_CHAIN_USCSI,			/* Index: 20 */
1976 
1977 	/* Chain for USCSI commands (checksum targets) */
1978 	SD_CHAIN_USCSI,			/* Index: 21 */
1979 	SD_CHAIN_USCSI,			/* Index: 22 */
1980 	SD_CHAIN_USCSI,			/* Index: 22 */
1981 
1982 	/* Chain for "direct" USCSI commands (all targets) */
1983 	SD_CHAIN_DIRECT,		/* Index: 24 */
1984 
1985 	/* Chain for "direct priority" USCSI commands (all targets) */
1986 	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
1987 };
1988 
1989 
1990 /* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
1991 #define	SD_IS_BUFIO(xp)			\
1992 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
1993 
1994 /* Macro to return TRUE if the IO has come from the "direct priority" chain. */
1995 #define	SD_IS_DIRECT_PRIORITY(xp)	\
1996 	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
1997 
1998 
1999 
2000 /*
2001  * Struct, array, and macros to map a specific chain to the appropriate
2002  * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2003  *
2004  * The sd_chain_index_map[] array is used at attach time to set the various
2005  * un_xxx_chain type members of the sd_lun softstate to the specific layering
2006  * chain to be used with the instance. This allows different instances to use
2007  * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2008  * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2009  * values at sd_xbuf init time, this allows (1) layering chains may be changed
2010  * dynamically & without the use of locking; and (2) a layer may update the
2011  * xb_chain_io[start|done] member in a given xbuf with its current index value,
2012  * to allow for deferred processing of an IO within the same chain from a
2013  * different execution context.
2014  */
2015 
2016 struct sd_chain_index {
2017 	int	sci_iostart_index;
2018 	int	sci_iodone_index;
2019 };
2020 
2021 static struct sd_chain_index	sd_chain_index_map[] = {
2022 	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2023 	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2024 	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2025 	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2026 	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2027 	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2028 	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2029 	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2030 	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2031 	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2032 };
2033 
2034 
2035 /*
2036  * The following are indexes into the sd_chain_index_map[] array.
2037  */
2038 
2039 /* un->un_buf_chain_type must be set to one of these */
2040 #define	SD_CHAIN_INFO_DISK		0
2041 #define	SD_CHAIN_INFO_DISK_NO_PM	1
2042 #define	SD_CHAIN_INFO_RMMEDIA		2
2043 #define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2044 #define	SD_CHAIN_INFO_CHKSUM		4
2045 #define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2046 
2047 /* un->un_uscsi_chain_type must be set to one of these */
2048 #define	SD_CHAIN_INFO_USCSI_CMD		6
2049 /* USCSI with PM disabled is the same as DIRECT */
2050 #define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2051 #define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2052 
2053 /* un->un_direct_chain_type must be set to one of these */
2054 #define	SD_CHAIN_INFO_DIRECT_CMD	8
2055 
2056 /* un->un_priority_chain_type must be set to one of these */
2057 #define	SD_CHAIN_INFO_PRIORITY_CMD	9
2058 
2059 /* size for devid inquiries */
2060 #define	MAX_INQUIRY_SIZE		0xF0
2061 
2062 /*
2063  * Macros used by functions to pass a given buf(9S) struct along to the
2064  * next function in the layering chain for further processing.
2065  *
2066  * In the following macros, passing more than three arguments to the called
2067  * routines causes the optimizer for the SPARC compiler to stop doing tail
2068  * call elimination which results in significant performance degradation.
2069  */
2070 #define	SD_BEGIN_IOSTART(index, un, bp)	\
2071 	((*(sd_iostart_chain[index]))(index, un, bp))
2072 
2073 #define	SD_BEGIN_IODONE(index, un, bp)	\
2074 	((*(sd_iodone_chain[index]))(index, un, bp))
2075 
2076 #define	SD_NEXT_IOSTART(index, un, bp)				\
2077 	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2078 
2079 #define	SD_NEXT_IODONE(index, un, bp)				\
2080 	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2081 
2082 
2083 /*
2084  *    Function: _init
2085  *
2086  * Description: This is the driver _init(9E) entry point.
2087  *
2088  * Return Code: Returns the value from mod_install(9F) or
2089  *		ddi_soft_state_init(9F) as appropriate.
2090  *
2091  *     Context: Called when driver module loaded.
2092  */
2093 
2094 int
2095 _init(void)
2096 {
2097 	int	err;
2098 
2099 	/* establish driver name from module name */
2100 	sd_label = mod_modname(&modlinkage);
2101 
2102 	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2103 		SD_MAXUNIT);
2104 
2105 	if (err != 0) {
2106 		return (err);
2107 	}
2108 
2109 	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2110 	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2111 	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2112 
2113 	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2114 	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2115 	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2116 
2117 	/*
2118 	 * it's ok to init here even for fibre device
2119 	 */
2120 	sd_scsi_probe_cache_init();
2121 
2122 	/*
2123 	 * Creating taskq before mod_install ensures that all callers (threads)
2124 	 * that enter the module after a successfull mod_install encounter
2125 	 * a valid taskq.
2126 	 */
2127 	sd_taskq_create();
2128 
2129 	err = mod_install(&modlinkage);
2130 	if (err != 0) {
2131 		/* delete taskq if install fails */
2132 		sd_taskq_delete();
2133 
2134 		mutex_destroy(&sd_detach_mutex);
2135 		mutex_destroy(&sd_log_mutex);
2136 		mutex_destroy(&sd_label_mutex);
2137 
2138 		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2139 		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2140 		cv_destroy(&sd_tr.srq_inprocess_cv);
2141 
2142 		sd_scsi_probe_cache_fini();
2143 
2144 		ddi_soft_state_fini(&sd_state);
2145 		return (err);
2146 	}
2147 
2148 	return (err);
2149 }
2150 
2151 
2152 /*
2153  *    Function: _fini
2154  *
2155  * Description: This is the driver _fini(9E) entry point.
2156  *
2157  * Return Code: Returns the value from mod_remove(9F)
2158  *
2159  *     Context: Called when driver module is unloaded.
2160  */
2161 
2162 int
2163 _fini(void)
2164 {
2165 	int err;
2166 
2167 	if ((err = mod_remove(&modlinkage)) != 0) {
2168 		return (err);
2169 	}
2170 
2171 	sd_taskq_delete();
2172 
2173 	mutex_destroy(&sd_detach_mutex);
2174 	mutex_destroy(&sd_log_mutex);
2175 	mutex_destroy(&sd_label_mutex);
2176 	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2177 
2178 	sd_scsi_probe_cache_fini();
2179 
2180 	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2181 	cv_destroy(&sd_tr.srq_inprocess_cv);
2182 
2183 	ddi_soft_state_fini(&sd_state);
2184 
2185 	return (err);
2186 }
2187 
2188 
2189 /*
2190  *    Function: _info
2191  *
2192  * Description: This is the driver _info(9E) entry point.
2193  *
2194  *   Arguments: modinfop - pointer to the driver modinfo structure
2195  *
2196  * Return Code: Returns the value from mod_info(9F).
2197  *
2198  *     Context: Kernel thread context
2199  */
2200 
2201 int
2202 _info(struct modinfo *modinfop)
2203 {
2204 	return (mod_info(&modlinkage, modinfop));
2205 }
2206 
2207 
2208 static void
2209 sd_fill_scsi1_lun(struct sd_lun *un, struct scsi_pkt *pktp)
2210 {
2211 	ASSERT(pktp != NULL);
2212 	if (un->un_f_is_fibre == TRUE) {
2213 		return;
2214 	}
2215 
2216 	SD_FILL_SCSI1_LUN(SD_SCSI_DEVP(un), pktp);
2217 }
2218 
2219 /*
2220  * The following routines implement the driver message logging facility.
2221  * They provide component- and level- based debug output filtering.
2222  * Output may also be restricted to messages for a single instance by
2223  * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2224  * to NULL, then messages for all instances are printed.
2225  *
2226  * These routines have been cloned from each other due to the language
2227  * constraints of macros and variable argument list processing.
2228  */
2229 
2230 
2231 /*
2232  *    Function: sd_log_err
2233  *
2234  * Description: This routine is called by the SD_ERROR macro for debug
2235  *		logging of error conditions.
2236  *
2237  *   Arguments: comp - driver component being logged
2238  *		dev  - pointer to driver info structure
2239  *		fmt  - error string and format to be logged
2240  */
2241 
2242 static void
2243 sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2244 {
2245 	va_list		ap;
2246 	dev_info_t	*dev;
2247 
2248 	ASSERT(un != NULL);
2249 	dev = SD_DEVINFO(un);
2250 	ASSERT(dev != NULL);
2251 
2252 	/*
2253 	 * Filter messages based on the global component and level masks.
2254 	 * Also print if un matches the value of sd_debug_un, or if
2255 	 * sd_debug_un is set to NULL.
2256 	 */
2257 	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2258 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2259 		mutex_enter(&sd_log_mutex);
2260 		va_start(ap, fmt);
2261 		(void) vsprintf(sd_log_buf, fmt, ap);
2262 		va_end(ap);
2263 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2264 		mutex_exit(&sd_log_mutex);
2265 	}
2266 #ifdef SD_FAULT_INJECTION
2267 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2268 	if (un->sd_injection_mask & comp) {
2269 		mutex_enter(&sd_log_mutex);
2270 		va_start(ap, fmt);
2271 		(void) vsprintf(sd_log_buf, fmt, ap);
2272 		va_end(ap);
2273 		sd_injection_log(sd_log_buf, un);
2274 		mutex_exit(&sd_log_mutex);
2275 	}
2276 #endif
2277 }
2278 
2279 
2280 /*
2281  *    Function: sd_log_info
2282  *
2283  * Description: This routine is called by the SD_INFO macro for debug
2284  *		logging of general purpose informational conditions.
2285  *
2286  *   Arguments: comp - driver component being logged
2287  *		dev  - pointer to driver info structure
2288  *		fmt  - info string and format to be logged
2289  */
2290 
2291 static void
2292 sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2293 {
2294 	va_list		ap;
2295 	dev_info_t	*dev;
2296 
2297 	ASSERT(un != NULL);
2298 	dev = SD_DEVINFO(un);
2299 	ASSERT(dev != NULL);
2300 
2301 	/*
2302 	 * Filter messages based on the global component and level masks.
2303 	 * Also print if un matches the value of sd_debug_un, or if
2304 	 * sd_debug_un is set to NULL.
2305 	 */
2306 	if ((sd_component_mask & component) &&
2307 	    (sd_level_mask & SD_LOGMASK_INFO) &&
2308 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2309 		mutex_enter(&sd_log_mutex);
2310 		va_start(ap, fmt);
2311 		(void) vsprintf(sd_log_buf, fmt, ap);
2312 		va_end(ap);
2313 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2314 		mutex_exit(&sd_log_mutex);
2315 	}
2316 #ifdef SD_FAULT_INJECTION
2317 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2318 	if (un->sd_injection_mask & component) {
2319 		mutex_enter(&sd_log_mutex);
2320 		va_start(ap, fmt);
2321 		(void) vsprintf(sd_log_buf, fmt, ap);
2322 		va_end(ap);
2323 		sd_injection_log(sd_log_buf, un);
2324 		mutex_exit(&sd_log_mutex);
2325 	}
2326 #endif
2327 }
2328 
2329 
2330 /*
2331  *    Function: sd_log_trace
2332  *
2333  * Description: This routine is called by the SD_TRACE macro for debug
2334  *		logging of trace conditions (i.e. function entry/exit).
2335  *
2336  *   Arguments: comp - driver component being logged
2337  *		dev  - pointer to driver info structure
2338  *		fmt  - trace string and format to be logged
2339  */
2340 
2341 static void
2342 sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2343 {
2344 	va_list		ap;
2345 	dev_info_t	*dev;
2346 
2347 	ASSERT(un != NULL);
2348 	dev = SD_DEVINFO(un);
2349 	ASSERT(dev != NULL);
2350 
2351 	/*
2352 	 * Filter messages based on the global component and level masks.
2353 	 * Also print if un matches the value of sd_debug_un, or if
2354 	 * sd_debug_un is set to NULL.
2355 	 */
2356 	if ((sd_component_mask & component) &&
2357 	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2358 	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2359 		mutex_enter(&sd_log_mutex);
2360 		va_start(ap, fmt);
2361 		(void) vsprintf(sd_log_buf, fmt, ap);
2362 		va_end(ap);
2363 		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2364 		mutex_exit(&sd_log_mutex);
2365 	}
2366 #ifdef SD_FAULT_INJECTION
2367 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2368 	if (un->sd_injection_mask & component) {
2369 		mutex_enter(&sd_log_mutex);
2370 		va_start(ap, fmt);
2371 		(void) vsprintf(sd_log_buf, fmt, ap);
2372 		va_end(ap);
2373 		sd_injection_log(sd_log_buf, un);
2374 		mutex_exit(&sd_log_mutex);
2375 	}
2376 #endif
2377 }
2378 
2379 
2380 /*
2381  *    Function: sdprobe
2382  *
2383  * Description: This is the driver probe(9e) entry point function.
2384  *
2385  *   Arguments: devi - opaque device info handle
2386  *
2387  * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2388  *              DDI_PROBE_FAILURE: If the probe failed.
2389  *              DDI_PROBE_PARTIAL: If the instance is not present now,
2390  *				   but may be present in the future.
2391  */
2392 
2393 static int
2394 sdprobe(dev_info_t *devi)
2395 {
2396 	struct scsi_device	*devp;
2397 	int			rval;
2398 	int			instance;
2399 
2400 	/*
2401 	 * if it wasn't for pln, sdprobe could actually be nulldev
2402 	 * in the "__fibre" case.
2403 	 */
2404 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2405 		return (DDI_PROBE_DONTCARE);
2406 	}
2407 
2408 	devp = ddi_get_driver_private(devi);
2409 
2410 	if (devp == NULL) {
2411 		/* Ooops... nexus driver is mis-configured... */
2412 		return (DDI_PROBE_FAILURE);
2413 	}
2414 
2415 	instance = ddi_get_instance(devi);
2416 
2417 	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2418 		return (DDI_PROBE_PARTIAL);
2419 	}
2420 
2421 	/*
2422 	 * Call the SCSA utility probe routine to see if we actually
2423 	 * have a target at this SCSI nexus.
2424 	 */
2425 	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2426 	case SCSIPROBE_EXISTS:
2427 		switch (devp->sd_inq->inq_dtype) {
2428 		case DTYPE_DIRECT:
2429 			rval = DDI_PROBE_SUCCESS;
2430 			break;
2431 		case DTYPE_RODIRECT:
2432 			/* CDs etc. Can be removable media */
2433 			rval = DDI_PROBE_SUCCESS;
2434 			break;
2435 		case DTYPE_OPTICAL:
2436 			/*
2437 			 * Rewritable optical driver HP115AA
2438 			 * Can also be removable media
2439 			 */
2440 
2441 			/*
2442 			 * Do not attempt to bind to  DTYPE_OPTICAL if
2443 			 * pre solaris 9 sparc sd behavior is required
2444 			 *
2445 			 * If first time through and sd_dtype_optical_bind
2446 			 * has not been set in /etc/system check properties
2447 			 */
2448 
2449 			if (sd_dtype_optical_bind  < 0) {
2450 			    sd_dtype_optical_bind = ddi_prop_get_int
2451 				(DDI_DEV_T_ANY,	devi,	0,
2452 				"optical-device-bind",	1);
2453 			}
2454 
2455 			if (sd_dtype_optical_bind == 0) {
2456 				rval = DDI_PROBE_FAILURE;
2457 			} else {
2458 				rval = DDI_PROBE_SUCCESS;
2459 			}
2460 			break;
2461 
2462 		case DTYPE_NOTPRESENT:
2463 		default:
2464 			rval = DDI_PROBE_FAILURE;
2465 			break;
2466 		}
2467 		break;
2468 	default:
2469 		rval = DDI_PROBE_PARTIAL;
2470 		break;
2471 	}
2472 
2473 	/*
2474 	 * This routine checks for resource allocation prior to freeing,
2475 	 * so it will take care of the "smart probing" case where a
2476 	 * scsi_probe() may or may not have been issued and will *not*
2477 	 * free previously-freed resources.
2478 	 */
2479 	scsi_unprobe(devp);
2480 	return (rval);
2481 }
2482 
2483 
2484 /*
2485  *    Function: sdinfo
2486  *
2487  * Description: This is the driver getinfo(9e) entry point function.
2488  * 		Given the device number, return the devinfo pointer from
2489  *		the scsi_device structure or the instance number
2490  *		associated with the dev_t.
2491  *
2492  *   Arguments: dip     - pointer to device info structure
2493  *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2494  *			  DDI_INFO_DEVT2INSTANCE)
2495  *		arg     - driver dev_t
2496  *		resultp - user buffer for request response
2497  *
2498  * Return Code: DDI_SUCCESS
2499  *              DDI_FAILURE
2500  */
2501 /* ARGSUSED */
2502 static int
2503 sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2504 {
2505 	struct sd_lun	*un;
2506 	dev_t		dev;
2507 	int		instance;
2508 	int		error;
2509 
2510 	switch (infocmd) {
2511 	case DDI_INFO_DEVT2DEVINFO:
2512 		dev = (dev_t)arg;
2513 		instance = SDUNIT(dev);
2514 		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2515 			return (DDI_FAILURE);
2516 		}
2517 		*result = (void *) SD_DEVINFO(un);
2518 		error = DDI_SUCCESS;
2519 		break;
2520 	case DDI_INFO_DEVT2INSTANCE:
2521 		dev = (dev_t)arg;
2522 		instance = SDUNIT(dev);
2523 		*result = (void *)(uintptr_t)instance;
2524 		error = DDI_SUCCESS;
2525 		break;
2526 	default:
2527 		error = DDI_FAILURE;
2528 	}
2529 	return (error);
2530 }
2531 
2532 /*
2533  *    Function: sd_prop_op
2534  *
2535  * Description: This is the driver prop_op(9e) entry point function.
2536  *		Return the number of blocks for the partition in question
2537  *		or forward the request to the property facilities.
2538  *
2539  *   Arguments: dev       - device number
2540  *		dip       - pointer to device info structure
2541  *		prop_op   - property operator
2542  *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2543  *		name      - pointer to property name
2544  *		valuep    - pointer or address of the user buffer
2545  *		lengthp   - property length
2546  *
2547  * Return Code: DDI_PROP_SUCCESS
2548  *              DDI_PROP_NOT_FOUND
2549  *              DDI_PROP_UNDEFINED
2550  *              DDI_PROP_NO_MEMORY
2551  *              DDI_PROP_BUF_TOO_SMALL
2552  */
2553 
2554 static int
2555 sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2556 	char *name, caddr_t valuep, int *lengthp)
2557 {
2558 	int		instance = ddi_get_instance(dip);
2559 	struct sd_lun	*un;
2560 	uint64_t	nblocks64;
2561 
2562 	/*
2563 	 * Our dynamic properties are all device specific and size oriented.
2564 	 * Requests issued under conditions where size is valid are passed
2565 	 * to ddi_prop_op_nblocks with the size information, otherwise the
2566 	 * request is passed to ddi_prop_op. Size depends on valid geometry.
2567 	 */
2568 	un = ddi_get_soft_state(sd_state, instance);
2569 	if ((dev == DDI_DEV_T_ANY) || (un == NULL) ||
2570 	    (un->un_f_geometry_is_valid == FALSE)) {
2571 		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2572 		    name, valuep, lengthp));
2573 	} else {
2574 		/* get nblocks value */
2575 		ASSERT(!mutex_owned(SD_MUTEX(un)));
2576 		mutex_enter(SD_MUTEX(un));
2577 		nblocks64 = (ulong_t)un->un_map[SDPART(dev)].dkl_nblk;
2578 		mutex_exit(SD_MUTEX(un));
2579 
2580 		return (ddi_prop_op_nblocks(dev, dip, prop_op, mod_flags,
2581 		    name, valuep, lengthp, nblocks64));
2582 	}
2583 }
2584 
2585 /*
2586  * The following functions are for smart probing:
2587  * sd_scsi_probe_cache_init()
2588  * sd_scsi_probe_cache_fini()
2589  * sd_scsi_clear_probe_cache()
2590  * sd_scsi_probe_with_cache()
2591  */
2592 
2593 /*
2594  *    Function: sd_scsi_probe_cache_init
2595  *
2596  * Description: Initializes the probe response cache mutex and head pointer.
2597  *
2598  *     Context: Kernel thread context
2599  */
2600 
2601 static void
2602 sd_scsi_probe_cache_init(void)
2603 {
2604 	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2605 	sd_scsi_probe_cache_head = NULL;
2606 }
2607 
2608 
2609 /*
2610  *    Function: sd_scsi_probe_cache_fini
2611  *
2612  * Description: Frees all resources associated with the probe response cache.
2613  *
2614  *     Context: Kernel thread context
2615  */
2616 
2617 static void
2618 sd_scsi_probe_cache_fini(void)
2619 {
2620 	struct sd_scsi_probe_cache *cp;
2621 	struct sd_scsi_probe_cache *ncp;
2622 
2623 	/* Clean up our smart probing linked list */
2624 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2625 		ncp = cp->next;
2626 		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2627 	}
2628 	sd_scsi_probe_cache_head = NULL;
2629 	mutex_destroy(&sd_scsi_probe_cache_mutex);
2630 }
2631 
2632 
2633 /*
2634  *    Function: sd_scsi_clear_probe_cache
2635  *
2636  * Description: This routine clears the probe response cache. This is
2637  *		done when open() returns ENXIO so that when deferred
2638  *		attach is attempted (possibly after a device has been
2639  *		turned on) we will retry the probe. Since we don't know
2640  *		which target we failed to open, we just clear the
2641  *		entire cache.
2642  *
2643  *     Context: Kernel thread context
2644  */
2645 
2646 static void
2647 sd_scsi_clear_probe_cache(void)
2648 {
2649 	struct sd_scsi_probe_cache	*cp;
2650 	int				i;
2651 
2652 	mutex_enter(&sd_scsi_probe_cache_mutex);
2653 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2654 		/*
2655 		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2656 		 * force probing to be performed the next time
2657 		 * sd_scsi_probe_with_cache is called.
2658 		 */
2659 		for (i = 0; i < NTARGETS_WIDE; i++) {
2660 			cp->cache[i] = SCSIPROBE_EXISTS;
2661 		}
2662 	}
2663 	mutex_exit(&sd_scsi_probe_cache_mutex);
2664 }
2665 
2666 
2667 /*
2668  *    Function: sd_scsi_probe_with_cache
2669  *
2670  * Description: This routine implements support for a scsi device probe
2671  *		with cache. The driver maintains a cache of the target
2672  *		responses to scsi probes. If we get no response from a
2673  *		target during a probe inquiry, we remember that, and we
2674  *		avoid additional calls to scsi_probe on non-zero LUNs
2675  *		on the same target until the cache is cleared. By doing
2676  *		so we avoid the 1/4 sec selection timeout for nonzero
2677  *		LUNs. lun0 of a target is always probed.
2678  *
2679  *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2680  *              waitfunc - indicates what the allocator routines should
2681  *			   do when resources are not available. This value
2682  *			   is passed on to scsi_probe() when that routine
2683  *			   is called.
2684  *
2685  * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2686  *		otherwise the value returned by scsi_probe(9F).
2687  *
2688  *     Context: Kernel thread context
2689  */
2690 
2691 static int
2692 sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
2693 {
2694 	struct sd_scsi_probe_cache	*cp;
2695 	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
2696 	int		lun   = devp->sd_address.a_lun;
2697 	int		tgt   = devp->sd_address.a_target;
2698 
2699 	/* Make sure caching enabled and target in range */
2700 	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
2701 		/* do it the old way (no cache) */
2702 		return (scsi_probe(devp, waitfn));
2703 	}
2704 
2705 	mutex_enter(&sd_scsi_probe_cache_mutex);
2706 
2707 	/* Find the cache for this scsi bus instance */
2708 	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2709 		if (cp->pdip == pdip) {
2710 			break;
2711 		}
2712 	}
2713 
2714 	/* If we can't find a cache for this pdip, create one */
2715 	if (cp == NULL) {
2716 		int i;
2717 
2718 		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
2719 		    KM_SLEEP);
2720 		cp->pdip = pdip;
2721 		cp->next = sd_scsi_probe_cache_head;
2722 		sd_scsi_probe_cache_head = cp;
2723 		for (i = 0; i < NTARGETS_WIDE; i++) {
2724 			cp->cache[i] = SCSIPROBE_EXISTS;
2725 		}
2726 	}
2727 
2728 	mutex_exit(&sd_scsi_probe_cache_mutex);
2729 
2730 	/* Recompute the cache for this target if LUN zero */
2731 	if (lun == 0) {
2732 		cp->cache[tgt] = SCSIPROBE_EXISTS;
2733 	}
2734 
2735 	/* Don't probe if cache remembers a NORESP from a previous LUN. */
2736 	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
2737 		return (SCSIPROBE_NORESP);
2738 	}
2739 
2740 	/* Do the actual probe; save & return the result */
2741 	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
2742 }
2743 
2744 
2745 /*
2746  *    Function: sd_spin_up_unit
2747  *
2748  * Description: Issues the following commands to spin-up the device:
2749  *		START STOP UNIT, and INQUIRY.
2750  *
2751  *   Arguments: un - driver soft state (unit) structure
2752  *
2753  * Return Code: 0 - success
2754  *		EIO - failure
2755  *		EACCES - reservation conflict
2756  *
2757  *     Context: Kernel thread context
2758  */
2759 
2760 static int
2761 sd_spin_up_unit(struct sd_lun *un)
2762 {
2763 	size_t	resid		= 0;
2764 	int	has_conflict	= FALSE;
2765 	uchar_t *bufaddr;
2766 
2767 	ASSERT(un != NULL);
2768 
2769 	/*
2770 	 * Send a throwaway START UNIT command.
2771 	 *
2772 	 * If we fail on this, we don't care presently what precisely
2773 	 * is wrong.  EMC's arrays will also fail this with a check
2774 	 * condition (0x2/0x4/0x3) if the device is "inactive," but
2775 	 * we don't want to fail the attach because it may become
2776 	 * "active" later.
2777 	 */
2778 	if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START, SD_PATH_DIRECT)
2779 	    == EACCES)
2780 		has_conflict = TRUE;
2781 
2782 	/*
2783 	 * Send another INQUIRY command to the target. This is necessary for
2784 	 * non-removable media direct access devices because their INQUIRY data
2785 	 * may not be fully qualified until they are spun up (perhaps via the
2786 	 * START command above).  Note: This seems to be needed for some
2787 	 * legacy devices only.) The INQUIRY command should succeed even if a
2788 	 * Reservation Conflict is present.
2789 	 */
2790 	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
2791 	if (sd_send_scsi_INQUIRY(un, bufaddr, SUN_INQSIZE, 0, 0, &resid) != 0) {
2792 		kmem_free(bufaddr, SUN_INQSIZE);
2793 		return (EIO);
2794 	}
2795 
2796 	/*
2797 	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
2798 	 * Note that this routine does not return a failure here even if the
2799 	 * INQUIRY command did not return any data.  This is a legacy behavior.
2800 	 */
2801 	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
2802 		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
2803 	}
2804 
2805 	kmem_free(bufaddr, SUN_INQSIZE);
2806 
2807 	/* If we hit a reservation conflict above, tell the caller. */
2808 	if (has_conflict == TRUE) {
2809 		return (EACCES);
2810 	}
2811 
2812 	return (0);
2813 }
2814 
2815 /*
2816  *    Function: sd_enable_descr_sense
2817  *
2818  * Description: This routine attempts to select descriptor sense format
2819  *		using the Control mode page.  Devices that support 64 bit
2820  *		LBAs (for >2TB luns) should also implement descriptor
2821  *		sense data so we will call this function whenever we see
2822  *		a lun larger than 2TB.  If for some reason the device
2823  *		supports 64 bit LBAs but doesn't support descriptor sense
2824  *		presumably the mode select will fail.  Everything will
2825  *		continue to work normally except that we will not get
2826  *		complete sense data for commands that fail with an LBA
2827  *		larger than 32 bits.
2828  *
2829  *   Arguments: un - driver soft state (unit) structure
2830  *
2831  *     Context: Kernel thread context only
2832  */
2833 
2834 static void
2835 sd_enable_descr_sense(struct sd_lun *un)
2836 {
2837 	uchar_t			*header;
2838 	struct mode_control_scsi3 *ctrl_bufp;
2839 	size_t			buflen;
2840 	size_t			bd_len;
2841 
2842 	/*
2843 	 * Read MODE SENSE page 0xA, Control Mode Page
2844 	 */
2845 	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
2846 	    sizeof (struct mode_control_scsi3);
2847 	header = kmem_zalloc(buflen, KM_SLEEP);
2848 	if (sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
2849 	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT) != 0) {
2850 		SD_ERROR(SD_LOG_COMMON, un,
2851 		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
2852 		goto eds_exit;
2853 	}
2854 
2855 	/*
2856 	 * Determine size of Block Descriptors in order to locate
2857 	 * the mode page data. ATAPI devices return 0, SCSI devices
2858 	 * should return MODE_BLK_DESC_LENGTH.
2859 	 */
2860 	bd_len  = ((struct mode_header *)header)->bdesc_length;
2861 
2862 	ctrl_bufp = (struct mode_control_scsi3 *)
2863 	    (header + MODE_HEADER_LENGTH + bd_len);
2864 
2865 	/*
2866 	 * Clear PS bit for MODE SELECT
2867 	 */
2868 	ctrl_bufp->mode_page.ps = 0;
2869 
2870 	/*
2871 	 * Set D_SENSE to enable descriptor sense format.
2872 	 */
2873 	ctrl_bufp->d_sense = 1;
2874 
2875 	/*
2876 	 * Use MODE SELECT to commit the change to the D_SENSE bit
2877 	 */
2878 	if (sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
2879 	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT) != 0) {
2880 		SD_INFO(SD_LOG_COMMON, un,
2881 		    "sd_enable_descr_sense: mode select ctrl page failed\n");
2882 		goto eds_exit;
2883 	}
2884 
2885 eds_exit:
2886 	kmem_free(header, buflen);
2887 }
2888 
2889 
2890 /*
2891  *    Function: sd_set_mmc_caps
2892  *
2893  * Description: This routine determines if the device is MMC compliant and if
2894  *		the device supports CDDA via a mode sense of the CDVD
2895  *		capabilities mode page. Also checks if the device is a
2896  *		dvdram writable device.
2897  *
2898  *   Arguments: un - driver soft state (unit) structure
2899  *
2900  *     Context: Kernel thread context only
2901  */
2902 
2903 static void
2904 sd_set_mmc_caps(struct sd_lun *un)
2905 {
2906 	struct mode_header_grp2		*sense_mhp;
2907 	uchar_t				*sense_page;
2908 	caddr_t				buf;
2909 	int				bd_len;
2910 	int				status;
2911 	struct uscsi_cmd		com;
2912 	int				rtn;
2913 	uchar_t				*out_data_rw, *out_data_hd;
2914 	uchar_t				*rqbuf_rw, *rqbuf_hd;
2915 
2916 	ASSERT(un != NULL);
2917 
2918 	/*
2919 	 * The flags which will be set in this function are - mmc compliant,
2920 	 * dvdram writable device, cdda support. Initialize them to FALSE
2921 	 * and if a capability is detected - it will be set to TRUE.
2922 	 */
2923 	un->un_f_mmc_cap = FALSE;
2924 	un->un_f_dvdram_writable_device = FALSE;
2925 	un->un_f_cfg_cdda = FALSE;
2926 
2927 	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
2928 	status = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, (uchar_t *)buf,
2929 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
2930 
2931 	if (status != 0) {
2932 		/* command failed; just return */
2933 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
2934 		return;
2935 	}
2936 	/*
2937 	 * If the mode sense request for the CDROM CAPABILITIES
2938 	 * page (0x2A) succeeds the device is assumed to be MMC.
2939 	 */
2940 	un->un_f_mmc_cap = TRUE;
2941 
2942 	/* Get to the page data */
2943 	sense_mhp = (struct mode_header_grp2 *)buf;
2944 	bd_len = (sense_mhp->bdesc_length_hi << 8) |
2945 	    sense_mhp->bdesc_length_lo;
2946 	if (bd_len > MODE_BLK_DESC_LENGTH) {
2947 		/*
2948 		 * We did not get back the expected block descriptor
2949 		 * length so we cannot determine if the device supports
2950 		 * CDDA. However, we still indicate the device is MMC
2951 		 * according to the successful response to the page
2952 		 * 0x2A mode sense request.
2953 		 */
2954 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
2955 		    "sd_set_mmc_caps: Mode Sense returned "
2956 		    "invalid block descriptor length\n");
2957 		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
2958 		return;
2959 	}
2960 
2961 	/* See if read CDDA is supported */
2962 	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
2963 	    bd_len);
2964 	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
2965 
2966 	/* See if writing DVD RAM is supported. */
2967 	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
2968 	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
2969 	if (un->un_f_dvdram_writable_device == TRUE) {
2970 		return;
2971 	}
2972 
2973 	/*
2974 	 * If un->un_f_dvdram_writable_device is still FALSE,
2975 	 * check for Iomega RRD type device.  Iomega is identifying
2976 	 * their RRD type devices by the features RANDOM_WRITABLE and
2977 	 * HARDWARE_DEFECT_MANAGEMENT.
2978 	 */
2979 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
2980 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
2981 
2982 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
2983 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
2984 	    RANDOM_WRITABLE);
2985 	if (rtn != 0) {
2986 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
2987 		kmem_free(rqbuf_rw, SENSE_LENGTH);
2988 		return;
2989 	}
2990 
2991 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
2992 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
2993 
2994 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
2995 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
2996 	    HARDWARE_DEFECT_MANAGEMENT);
2997 	if (rtn == 0) {
2998 		/*
2999 		 * We have good information, check for random writable
3000 		 * and hardware defect features.
3001 		 */
3002 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3003 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3004 			un->un_f_dvdram_writable_device = TRUE;
3005 		}
3006 	}
3007 
3008 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3009 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3010 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3011 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3012 }
3013 
3014 /*
3015  *    Function: sd_check_for_writable_cd
3016  *
3017  * Description: This routine determines if the media in the device is
3018  *		writable or not. It uses the get configuration command (0x46)
3019  *		to determine if the media is writable
3020  *
3021  *   Arguments: un - driver soft state (unit) structure
3022  *
3023  *     Context: Never called at interrupt context.
3024  */
3025 
3026 static void
3027 sd_check_for_writable_cd(struct sd_lun *un)
3028 {
3029 	struct uscsi_cmd		com;
3030 	uchar_t				*out_data;
3031 	uchar_t				*rqbuf;
3032 	int				rtn;
3033 	uchar_t				*out_data_rw, *out_data_hd;
3034 	uchar_t				*rqbuf_rw, *rqbuf_hd;
3035 
3036 	ASSERT(un != NULL);
3037 	ASSERT(mutex_owned(SD_MUTEX(un)));
3038 
3039 	/*
3040 	 * Initialize the writable media to false, if configuration info.
3041 	 * tells us otherwise then only we will set it.
3042 	 */
3043 	un->un_f_mmc_writable_media = FALSE;
3044 	mutex_exit(SD_MUTEX(un));
3045 
3046 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3047 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3048 
3049 	rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf, SENSE_LENGTH,
3050 	    out_data, SD_PROFILE_HEADER_LEN);
3051 
3052 	mutex_enter(SD_MUTEX(un));
3053 	if (rtn == 0) {
3054 		/*
3055 		 * We have good information, check for writable DVD.
3056 		 */
3057 		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3058 			un->un_f_mmc_writable_media = TRUE;
3059 			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3060 			kmem_free(rqbuf, SENSE_LENGTH);
3061 			return;
3062 		}
3063 	}
3064 
3065 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3066 	kmem_free(rqbuf, SENSE_LENGTH);
3067 
3068 	/*
3069 	 * If un->un_f_mmc_writable_media is still FALSE,
3070 	 * check for Iomega RRD type media.  Iomega is identifying
3071 	 * their RRD type devices by the features RANDOM_WRITABLE and
3072 	 * HARDWARE_DEFECT_MANAGEMENT.
3073 	 */
3074 	mutex_exit(SD_MUTEX(un));
3075 	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3076 	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3077 
3078 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_rw,
3079 	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3080 	    RANDOM_WRITABLE);
3081 	if (rtn != 0) {
3082 		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3083 		kmem_free(rqbuf_rw, SENSE_LENGTH);
3084 		mutex_enter(SD_MUTEX(un));
3085 		return;
3086 	}
3087 
3088 	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3089 	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3090 
3091 	rtn = sd_send_scsi_feature_GET_CONFIGURATION(un, &com, rqbuf_hd,
3092 	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3093 	    HARDWARE_DEFECT_MANAGEMENT);
3094 	mutex_enter(SD_MUTEX(un));
3095 	if (rtn == 0) {
3096 		/*
3097 		 * We have good information, check for random writable
3098 		 * and hardware defect features as current.
3099 		 */
3100 		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3101 		    (out_data_rw[10] & 0x1) &&
3102 		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3103 		    (out_data_hd[10] & 0x1)) {
3104 			un->un_f_mmc_writable_media = TRUE;
3105 		}
3106 	}
3107 
3108 	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3109 	kmem_free(rqbuf_rw, SENSE_LENGTH);
3110 	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3111 	kmem_free(rqbuf_hd, SENSE_LENGTH);
3112 }
3113 
3114 /*
3115  *    Function: sd_read_unit_properties
3116  *
3117  * Description: The following implements a property lookup mechanism.
3118  *		Properties for particular disks (keyed on vendor, model
3119  *		and rev numbers) are sought in the sd.conf file via
3120  *		sd_process_sdconf_file(), and if not found there, are
3121  *		looked for in a list hardcoded in this driver via
3122  *		sd_process_sdconf_table() Once located the properties
3123  *		are used to update the driver unit structure.
3124  *
3125  *   Arguments: un - driver soft state (unit) structure
3126  */
3127 
3128 static void
3129 sd_read_unit_properties(struct sd_lun *un)
3130 {
3131 	/*
3132 	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3133 	 * the "sd-config-list" property (from the sd.conf file) or if
3134 	 * there was not a match for the inquiry vid/pid. If this event
3135 	 * occurs the static driver configuration table is searched for
3136 	 * a match.
3137 	 */
3138 	ASSERT(un != NULL);
3139 	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3140 		sd_process_sdconf_table(un);
3141 	}
3142 
3143 	/*
3144 	 * Set this in sd.conf to 0 in order to disable kstats.  The default
3145 	 * is 1, so they are enabled by default.
3146 	 */
3147 	un->un_f_pkstats_enabled = (ddi_prop_get_int(DDI_DEV_T_ANY,
3148 	    SD_DEVINFO(un), DDI_PROP_DONTPASS, "enable-partition-kstats", 1));
3149 }
3150 
3151 
3152 /*
3153  *    Function: sd_process_sdconf_file
3154  *
3155  * Description: Use ddi_getlongprop to obtain the properties from the
3156  *		driver's config file (ie, sd.conf) and update the driver
3157  *		soft state structure accordingly.
3158  *
3159  *   Arguments: un - driver soft state (unit) structure
3160  *
3161  * Return Code: SD_SUCCESS - The properties were successfully set according
3162  *			     to the driver configuration file.
3163  *		SD_FAILURE - The driver config list was not obtained or
3164  *			     there was no vid/pid match. This indicates that
3165  *			     the static config table should be used.
3166  *
3167  * The config file has a property, "sd-config-list", which consists of
3168  * one or more duplets as follows:
3169  *
3170  *  sd-config-list=
3171  *	<duplet>,
3172  *	[<duplet>,]
3173  *	[<duplet>];
3174  *
3175  * The structure of each duplet is as follows:
3176  *
3177  *  <duplet>:= <vid+pid>,<data-property-name_list>
3178  *
3179  * The first entry of the duplet is the device ID string (the concatenated
3180  * vid & pid; not to be confused with a device_id).  This is defined in
3181  * the same way as in the sd_disk_table.
3182  *
3183  * The second part of the duplet is a string that identifies a
3184  * data-property-name-list. The data-property-name-list is defined as
3185  * follows:
3186  *
3187  *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3188  *
3189  * The syntax of <data-property-name> depends on the <version> field.
3190  *
3191  * If version = SD_CONF_VERSION_1 we have the following syntax:
3192  *
3193  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3194  *
3195  * where the prop0 value will be used to set prop0 if bit0 set in the
3196  * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3197  *
3198  * If version = SD_CONF_VERSION_10 we have the following syntax:
3199  *
3200  * 	<data-property-name>:=<version>,<prop0>,<prop1>,<prop2>,<prop3>
3201  */
3202 
3203 static int
3204 sd_process_sdconf_file(struct sd_lun *un)
3205 {
3206 	char	*config_list = NULL;
3207 	int	config_list_len;
3208 	int	len;
3209 	int	dupletlen = 0;
3210 	char	*vidptr;
3211 	int	vidlen;
3212 	char	*dnlist_ptr;
3213 	char	*dataname_ptr;
3214 	int	dnlist_len;
3215 	int	dataname_len;
3216 	int	*data_list;
3217 	int	data_list_len;
3218 	int	rval = SD_FAILURE;
3219 	int	i;
3220 
3221 	ASSERT(un != NULL);
3222 
3223 	/* Obtain the configuration list associated with the .conf file */
3224 	if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), DDI_PROP_DONTPASS,
3225 	    sd_config_list, (caddr_t)&config_list, &config_list_len)
3226 	    != DDI_PROP_SUCCESS) {
3227 		return (SD_FAILURE);
3228 	}
3229 
3230 	/*
3231 	 * Compare vids in each duplet to the inquiry vid - if a match is
3232 	 * made, get the data value and update the soft state structure
3233 	 * accordingly.
3234 	 *
3235 	 * Note: This algorithm is complex and difficult to maintain. It should
3236 	 * be replaced with a more robust implementation.
3237 	 */
3238 	for (len = config_list_len, vidptr = config_list; len > 0;
3239 	    vidptr += dupletlen, len -= dupletlen) {
3240 		/*
3241 		 * Note: The assumption here is that each vid entry is on
3242 		 * a unique line from its associated duplet.
3243 		 */
3244 		vidlen = dupletlen = (int)strlen(vidptr);
3245 		if ((vidlen == 0) ||
3246 		    (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS)) {
3247 			dupletlen++;
3248 			continue;
3249 		}
3250 
3251 		/*
3252 		 * dnlist contains 1 or more blank separated
3253 		 * data-property-name entries
3254 		 */
3255 		dnlist_ptr = vidptr + vidlen + 1;
3256 		dnlist_len = (int)strlen(dnlist_ptr);
3257 		dupletlen += dnlist_len + 2;
3258 
3259 		/*
3260 		 * Set a pointer for the first data-property-name
3261 		 * entry in the list
3262 		 */
3263 		dataname_ptr = dnlist_ptr;
3264 		dataname_len = 0;
3265 
3266 		/*
3267 		 * Loop through all data-property-name entries in the
3268 		 * data-property-name-list setting the properties for each.
3269 		 */
3270 		while (dataname_len < dnlist_len) {
3271 			int version;
3272 
3273 			/*
3274 			 * Determine the length of the current
3275 			 * data-property-name entry by indexing until a
3276 			 * blank or NULL is encountered. When the space is
3277 			 * encountered reset it to a NULL for compliance
3278 			 * with ddi_getlongprop().
3279 			 */
3280 			for (i = 0; ((dataname_ptr[i] != ' ') &&
3281 			    (dataname_ptr[i] != '\0')); i++) {
3282 				;
3283 			}
3284 
3285 			dataname_len += i;
3286 			/* If not null terminated, Make it so */
3287 			if (dataname_ptr[i] == ' ') {
3288 				dataname_ptr[i] = '\0';
3289 			}
3290 			dataname_len++;
3291 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3292 			    "sd_process_sdconf_file: disk:%s, data:%s\n",
3293 			    vidptr, dataname_ptr);
3294 
3295 			/* Get the data list */
3296 			if (ddi_getlongprop(DDI_DEV_T_ANY, SD_DEVINFO(un), 0,
3297 			    dataname_ptr, (caddr_t)&data_list, &data_list_len)
3298 			    != DDI_PROP_SUCCESS) {
3299 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3300 				    "sd_process_sdconf_file: data property (%s)"
3301 				    " has no value\n", dataname_ptr);
3302 				dataname_ptr = dnlist_ptr + dataname_len;
3303 				continue;
3304 			}
3305 
3306 			version = data_list[0];
3307 
3308 			if (version == SD_CONF_VERSION_1) {
3309 				sd_tunables values;
3310 
3311 				/* Set the properties */
3312 				if (sd_chk_vers1_data(un, data_list[1],
3313 				    &data_list[2], data_list_len, dataname_ptr)
3314 				    == SD_SUCCESS) {
3315 					sd_get_tunables_from_conf(un,
3316 					    data_list[1], &data_list[2],
3317 					    &values);
3318 					sd_set_vers1_properties(un,
3319 					    data_list[1], &values);
3320 					rval = SD_SUCCESS;
3321 				} else {
3322 					rval = SD_FAILURE;
3323 				}
3324 			} else {
3325 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3326 				    "data property %s version 0x%x is invalid.",
3327 				    dataname_ptr, version);
3328 				rval = SD_FAILURE;
3329 			}
3330 			kmem_free(data_list, data_list_len);
3331 			dataname_ptr = dnlist_ptr + dataname_len;
3332 		}
3333 	}
3334 
3335 	/* free up the memory allocated by ddi_getlongprop */
3336 	if (config_list) {
3337 		kmem_free(config_list, config_list_len);
3338 	}
3339 
3340 	return (rval);
3341 }
3342 
3343 /*
3344  *    Function: sd_get_tunables_from_conf()
3345  *
3346  *
3347  *    This function reads the data list from the sd.conf file and pulls
3348  *    the values that can have numeric values as arguments and places
3349  *    the values in the apropriate sd_tunables member.
3350  *    Since the order of the data list members varies across platforms
3351  *    This function reads them from the data list in a platform specific
3352  *    order and places them into the correct sd_tunable member that is
3353  *    a consistant across all platforms.
3354  */
3355 static void
3356 sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
3357     sd_tunables *values)
3358 {
3359 	int i;
3360 	int mask;
3361 
3362 	bzero(values, sizeof (sd_tunables));
3363 
3364 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3365 
3366 		mask = 1 << i;
3367 		if (mask > flags) {
3368 			break;
3369 		}
3370 
3371 		switch (mask & flags) {
3372 		case 0:	/* This mask bit not set in flags */
3373 			continue;
3374 		case SD_CONF_BSET_THROTTLE:
3375 			values->sdt_throttle = data_list[i];
3376 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3377 			    "sd_get_tunables_from_conf: throttle = %d\n",
3378 			    values->sdt_throttle);
3379 			break;
3380 		case SD_CONF_BSET_CTYPE:
3381 			values->sdt_ctype = data_list[i];
3382 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3383 			    "sd_get_tunables_from_conf: ctype = %d\n",
3384 			    values->sdt_ctype);
3385 			break;
3386 		case SD_CONF_BSET_NRR_COUNT:
3387 			values->sdt_not_rdy_retries = data_list[i];
3388 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3389 			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
3390 			    values->sdt_not_rdy_retries);
3391 			break;
3392 		case SD_CONF_BSET_BSY_RETRY_COUNT:
3393 			values->sdt_busy_retries = data_list[i];
3394 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3395 			    "sd_get_tunables_from_conf: busy_retries = %d\n",
3396 			    values->sdt_busy_retries);
3397 			break;
3398 		case SD_CONF_BSET_RST_RETRIES:
3399 			values->sdt_reset_retries = data_list[i];
3400 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3401 			    "sd_get_tunables_from_conf: reset_retries = %d\n",
3402 			    values->sdt_reset_retries);
3403 			break;
3404 		case SD_CONF_BSET_RSV_REL_TIME:
3405 			values->sdt_reserv_rel_time = data_list[i];
3406 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3407 			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
3408 			    values->sdt_reserv_rel_time);
3409 			break;
3410 		case SD_CONF_BSET_MIN_THROTTLE:
3411 			values->sdt_min_throttle = data_list[i];
3412 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3413 			    "sd_get_tunables_from_conf: min_throttle = %d\n",
3414 			    values->sdt_min_throttle);
3415 			break;
3416 		case SD_CONF_BSET_DISKSORT_DISABLED:
3417 			values->sdt_disk_sort_dis = data_list[i];
3418 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3419 			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
3420 			    values->sdt_disk_sort_dis);
3421 			break;
3422 		case SD_CONF_BSET_LUN_RESET_ENABLED:
3423 			values->sdt_lun_reset_enable = data_list[i];
3424 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3425 			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
3426 			    "\n", values->sdt_lun_reset_enable);
3427 			break;
3428 		}
3429 	}
3430 }
3431 
3432 /*
3433  *    Function: sd_process_sdconf_table
3434  *
3435  * Description: Search the static configuration table for a match on the
3436  *		inquiry vid/pid and update the driver soft state structure
3437  *		according to the table property values for the device.
3438  *
3439  *		The form of a configuration table entry is:
3440  *		  <vid+pid>,<flags>,<property-data>
3441  *		  "SEAGATE ST42400N",1,63,0,0			(Fibre)
3442  *		  "SEAGATE ST42400N",1,63,0,0,0,0		(Sparc)
3443  *		  "SEAGATE ST42400N",1,63,0,0,0,0,0,0,0,0,0,0	(Intel)
3444  *
3445  *   Arguments: un - driver soft state (unit) structure
3446  */
3447 
3448 static void
3449 sd_process_sdconf_table(struct sd_lun *un)
3450 {
3451 	char	*id = NULL;
3452 	int	table_index;
3453 	int	idlen;
3454 
3455 	ASSERT(un != NULL);
3456 	for (table_index = 0; table_index < sd_disk_table_size;
3457 	    table_index++) {
3458 		id = sd_disk_table[table_index].device_id;
3459 		idlen = strlen(id);
3460 		if (idlen == 0) {
3461 			continue;
3462 		}
3463 
3464 		/*
3465 		 * The static configuration table currently does not
3466 		 * implement version 10 properties. Additionally,
3467 		 * multiple data-property-name entries are not
3468 		 * implemented in the static configuration table.
3469 		 */
3470 		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
3471 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3472 			    "sd_process_sdconf_table: disk %s\n", id);
3473 			sd_set_vers1_properties(un,
3474 			    sd_disk_table[table_index].flags,
3475 			    sd_disk_table[table_index].properties);
3476 			break;
3477 		}
3478 	}
3479 }
3480 
3481 
3482 /*
3483  *    Function: sd_sdconf_id_match
3484  *
3485  * Description: This local function implements a case sensitive vid/pid
3486  *		comparison as well as the boundary cases of wild card and
3487  *		multiple blanks.
3488  *
3489  *		Note: An implicit assumption made here is that the scsi
3490  *		inquiry structure will always keep the vid, pid and
3491  *		revision strings in consecutive sequence, so they can be
3492  *		read as a single string. If this assumption is not the
3493  *		case, a separate string, to be used for the check, needs
3494  *		to be built with these strings concatenated.
3495  *
3496  *   Arguments: un - driver soft state (unit) structure
3497  *		id - table or config file vid/pid
3498  *		idlen  - length of the vid/pid (bytes)
3499  *
3500  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3501  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3502  */
3503 
3504 static int
3505 sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
3506 {
3507 	struct scsi_inquiry	*sd_inq;
3508 	int 			rval = SD_SUCCESS;
3509 
3510 	ASSERT(un != NULL);
3511 	sd_inq = un->un_sd->sd_inq;
3512 	ASSERT(id != NULL);
3513 
3514 	/*
3515 	 * We use the inq_vid as a pointer to a buffer containing the
3516 	 * vid and pid and use the entire vid/pid length of the table
3517 	 * entry for the comparison. This works because the inq_pid
3518 	 * data member follows inq_vid in the scsi_inquiry structure.
3519 	 */
3520 	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
3521 		/*
3522 		 * The user id string is compared to the inquiry vid/pid
3523 		 * using a case insensitive comparison and ignoring
3524 		 * multiple spaces.
3525 		 */
3526 		rval = sd_blank_cmp(un, id, idlen);
3527 		if (rval != SD_SUCCESS) {
3528 			/*
3529 			 * User id strings that start and end with a "*"
3530 			 * are a special case. These do not have a
3531 			 * specific vendor, and the product string can
3532 			 * appear anywhere in the 16 byte PID portion of
3533 			 * the inquiry data. This is a simple strstr()
3534 			 * type search for the user id in the inquiry data.
3535 			 */
3536 			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
3537 				char	*pidptr = &id[1];
3538 				int	i;
3539 				int	j;
3540 				int	pidstrlen = idlen - 2;
3541 				j = sizeof (SD_INQUIRY(un)->inq_pid) -
3542 				    pidstrlen;
3543 
3544 				if (j < 0) {
3545 					return (SD_FAILURE);
3546 				}
3547 				for (i = 0; i < j; i++) {
3548 					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
3549 					    pidptr, pidstrlen) == 0) {
3550 						rval = SD_SUCCESS;
3551 						break;
3552 					}
3553 				}
3554 			}
3555 		}
3556 	}
3557 	return (rval);
3558 }
3559 
3560 
3561 /*
3562  *    Function: sd_blank_cmp
3563  *
3564  * Description: If the id string starts and ends with a space, treat
3565  *		multiple consecutive spaces as equivalent to a single
3566  *		space. For example, this causes a sd_disk_table entry
3567  *		of " NEC CDROM " to match a device's id string of
3568  *		"NEC       CDROM".
3569  *
3570  *		Note: The success exit condition for this routine is if
3571  *		the pointer to the table entry is '\0' and the cnt of
3572  *		the inquiry length is zero. This will happen if the inquiry
3573  *		string returned by the device is padded with spaces to be
3574  *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
3575  *		SCSI spec states that the inquiry string is to be padded with
3576  *		spaces.
3577  *
3578  *   Arguments: un - driver soft state (unit) structure
3579  *		id - table or config file vid/pid
3580  *		idlen  - length of the vid/pid (bytes)
3581  *
3582  * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
3583  *		SD_FAILURE - Indicates no match with the inquiry vid/pid
3584  */
3585 
3586 static int
3587 sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
3588 {
3589 	char		*p1;
3590 	char		*p2;
3591 	int		cnt;
3592 	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
3593 	    sizeof (SD_INQUIRY(un)->inq_pid);
3594 
3595 	ASSERT(un != NULL);
3596 	p2 = un->un_sd->sd_inq->inq_vid;
3597 	ASSERT(id != NULL);
3598 	p1 = id;
3599 
3600 	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
3601 		/*
3602 		 * Note: string p1 is terminated by a NUL but string p2
3603 		 * isn't.  The end of p2 is determined by cnt.
3604 		 */
3605 		for (;;) {
3606 			/* skip over any extra blanks in both strings */
3607 			while ((*p1 != '\0') && (*p1 == ' ')) {
3608 				p1++;
3609 			}
3610 			while ((cnt != 0) && (*p2 == ' ')) {
3611 				p2++;
3612 				cnt--;
3613 			}
3614 
3615 			/* compare the two strings */
3616 			if ((cnt == 0) ||
3617 			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
3618 				break;
3619 			}
3620 			while ((cnt > 0) &&
3621 			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
3622 				p1++;
3623 				p2++;
3624 				cnt--;
3625 			}
3626 		}
3627 	}
3628 
3629 	/* return SD_SUCCESS if both strings match */
3630 	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
3631 }
3632 
3633 
3634 /*
3635  *    Function: sd_chk_vers1_data
3636  *
3637  * Description: Verify the version 1 device properties provided by the
3638  *		user via the configuration file
3639  *
3640  *   Arguments: un	     - driver soft state (unit) structure
3641  *		flags	     - integer mask indicating properties to be set
3642  *		prop_list    - integer list of property values
3643  *		list_len     - length of user provided data
3644  *
3645  * Return Code: SD_SUCCESS - Indicates the user provided data is valid
3646  *		SD_FAILURE - Indicates the user provided data is invalid
3647  */
3648 
3649 static int
3650 sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
3651     int list_len, char *dataname_ptr)
3652 {
3653 	int i;
3654 	int mask = 1;
3655 	int index = 0;
3656 
3657 	ASSERT(un != NULL);
3658 
3659 	/* Check for a NULL property name and list */
3660 	if (dataname_ptr == NULL) {
3661 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3662 		    "sd_chk_vers1_data: NULL data property name.");
3663 		return (SD_FAILURE);
3664 	}
3665 	if (prop_list == NULL) {
3666 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3667 		    "sd_chk_vers1_data: %s NULL data property list.",
3668 		    dataname_ptr);
3669 		return (SD_FAILURE);
3670 	}
3671 
3672 	/* Display a warning if undefined bits are set in the flags */
3673 	if (flags & ~SD_CONF_BIT_MASK) {
3674 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3675 		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
3676 		    "Properties not set.",
3677 		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
3678 		return (SD_FAILURE);
3679 	}
3680 
3681 	/*
3682 	 * Verify the length of the list by identifying the highest bit set
3683 	 * in the flags and validating that the property list has a length
3684 	 * up to the index of this bit.
3685 	 */
3686 	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
3687 		if (flags & mask) {
3688 			index++;
3689 		}
3690 		mask = 1 << i;
3691 	}
3692 	if ((list_len / sizeof (int)) < (index + 2)) {
3693 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3694 		    "sd_chk_vers1_data: "
3695 		    "Data property list %s size is incorrect. "
3696 		    "Properties not set.", dataname_ptr);
3697 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
3698 		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
3699 		return (SD_FAILURE);
3700 	}
3701 	return (SD_SUCCESS);
3702 }
3703 
3704 
3705 /*
3706  *    Function: sd_set_vers1_properties
3707  *
3708  * Description: Set version 1 device properties based on a property list
3709  *		retrieved from the driver configuration file or static
3710  *		configuration table. Version 1 properties have the format:
3711  *
3712  * 	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3713  *
3714  *		where the prop0 value will be used to set prop0 if bit0
3715  *		is set in the flags
3716  *
3717  *   Arguments: un	     - driver soft state (unit) structure
3718  *		flags	     - integer mask indicating properties to be set
3719  *		prop_list    - integer list of property values
3720  */
3721 
3722 static void
3723 sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
3724 {
3725 	ASSERT(un != NULL);
3726 
3727 	/*
3728 	 * Set the flag to indicate cache is to be disabled. An attempt
3729 	 * to disable the cache via sd_disable_caching() will be made
3730 	 * later during attach once the basic initialization is complete.
3731 	 */
3732 	if (flags & SD_CONF_BSET_NOCACHE) {
3733 		un->un_f_opt_disable_cache = TRUE;
3734 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3735 		    "sd_set_vers1_properties: caching disabled flag set\n");
3736 	}
3737 
3738 	/* CD-specific configuration parameters */
3739 	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
3740 		un->un_f_cfg_playmsf_bcd = TRUE;
3741 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3742 		    "sd_set_vers1_properties: playmsf_bcd set\n");
3743 	}
3744 	if (flags & SD_CONF_BSET_READSUB_BCD) {
3745 		un->un_f_cfg_readsub_bcd = TRUE;
3746 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3747 		    "sd_set_vers1_properties: readsub_bcd set\n");
3748 	}
3749 	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
3750 		un->un_f_cfg_read_toc_trk_bcd = TRUE;
3751 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3752 		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
3753 	}
3754 	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
3755 		un->un_f_cfg_read_toc_addr_bcd = TRUE;
3756 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3757 		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
3758 	}
3759 	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
3760 		un->un_f_cfg_no_read_header = TRUE;
3761 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3762 			    "sd_set_vers1_properties: no_read_header set\n");
3763 	}
3764 	if (flags & SD_CONF_BSET_READ_CD_XD4) {
3765 		un->un_f_cfg_read_cd_xd4 = TRUE;
3766 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3767 		    "sd_set_vers1_properties: read_cd_xd4 set\n");
3768 	}
3769 
3770 	/* Support for devices which do not have valid/unique serial numbers */
3771 	if (flags & SD_CONF_BSET_FAB_DEVID) {
3772 		un->un_f_opt_fab_devid = TRUE;
3773 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3774 		    "sd_set_vers1_properties: fab_devid bit set\n");
3775 	}
3776 
3777 	/* Support for user throttle configuration */
3778 	if (flags & SD_CONF_BSET_THROTTLE) {
3779 		ASSERT(prop_list != NULL);
3780 		un->un_saved_throttle = un->un_throttle =
3781 		    prop_list->sdt_throttle;
3782 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3783 		    "sd_set_vers1_properties: throttle set to %d\n",
3784 		    prop_list->sdt_throttle);
3785 	}
3786 
3787 	/* Set the per disk retry count according to the conf file or table. */
3788 	if (flags & SD_CONF_BSET_NRR_COUNT) {
3789 		ASSERT(prop_list != NULL);
3790 		if (prop_list->sdt_not_rdy_retries) {
3791 			un->un_notready_retry_count =
3792 				prop_list->sdt_not_rdy_retries;
3793 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3794 			    "sd_set_vers1_properties: not ready retry count"
3795 			    " set to %d\n", un->un_notready_retry_count);
3796 		}
3797 	}
3798 
3799 	/* The controller type is reported for generic disk driver ioctls */
3800 	if (flags & SD_CONF_BSET_CTYPE) {
3801 		ASSERT(prop_list != NULL);
3802 		switch (prop_list->sdt_ctype) {
3803 		case CTYPE_CDROM:
3804 			un->un_ctype = prop_list->sdt_ctype;
3805 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3806 			    "sd_set_vers1_properties: ctype set to "
3807 			    "CTYPE_CDROM\n");
3808 			break;
3809 		case CTYPE_CCS:
3810 			un->un_ctype = prop_list->sdt_ctype;
3811 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3812 				"sd_set_vers1_properties: ctype set to "
3813 				"CTYPE_CCS\n");
3814 			break;
3815 		case CTYPE_ROD:		/* RW optical */
3816 			un->un_ctype = prop_list->sdt_ctype;
3817 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3818 			    "sd_set_vers1_properties: ctype set to "
3819 			    "CTYPE_ROD\n");
3820 			break;
3821 		default:
3822 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3823 			    "sd_set_vers1_properties: Could not set "
3824 			    "invalid ctype value (%d)",
3825 			    prop_list->sdt_ctype);
3826 		}
3827 	}
3828 
3829 	/* Purple failover timeout */
3830 	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
3831 		ASSERT(prop_list != NULL);
3832 		un->un_busy_retry_count =
3833 			prop_list->sdt_busy_retries;
3834 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3835 		    "sd_set_vers1_properties: "
3836 		    "busy retry count set to %d\n",
3837 		    un->un_busy_retry_count);
3838 	}
3839 
3840 	/* Purple reset retry count */
3841 	if (flags & SD_CONF_BSET_RST_RETRIES) {
3842 		ASSERT(prop_list != NULL);
3843 		un->un_reset_retry_count =
3844 			prop_list->sdt_reset_retries;
3845 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3846 		    "sd_set_vers1_properties: "
3847 		    "reset retry count set to %d\n",
3848 		    un->un_reset_retry_count);
3849 	}
3850 
3851 	/* Purple reservation release timeout */
3852 	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
3853 		ASSERT(prop_list != NULL);
3854 		un->un_reserve_release_time =
3855 			prop_list->sdt_reserv_rel_time;
3856 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3857 		    "sd_set_vers1_properties: "
3858 		    "reservation release timeout set to %d\n",
3859 		    un->un_reserve_release_time);
3860 	}
3861 
3862 	/*
3863 	 * Driver flag telling the driver to verify that no commands are pending
3864 	 * for a device before issuing a Test Unit Ready. This is a workaround
3865 	 * for a firmware bug in some Seagate eliteI drives.
3866 	 */
3867 	if (flags & SD_CONF_BSET_TUR_CHECK) {
3868 		un->un_f_cfg_tur_check = TRUE;
3869 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3870 		    "sd_set_vers1_properties: tur queue check set\n");
3871 	}
3872 
3873 	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
3874 		un->un_min_throttle = prop_list->sdt_min_throttle;
3875 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3876 		    "sd_set_vers1_properties: min throttle set to %d\n",
3877 		    un->un_min_throttle);
3878 	}
3879 
3880 	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
3881 		un->un_f_disksort_disabled =
3882 		    (prop_list->sdt_disk_sort_dis != 0) ?
3883 		    TRUE : FALSE;
3884 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3885 		    "sd_set_vers1_properties: disksort disabled "
3886 		    "flag set to %d\n",
3887 		    prop_list->sdt_disk_sort_dis);
3888 	}
3889 
3890 	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
3891 		un->un_f_lun_reset_enabled =
3892 		    (prop_list->sdt_lun_reset_enable != 0) ?
3893 		    TRUE : FALSE;
3894 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
3895 		    "sd_set_vers1_properties: lun reset enabled "
3896 		    "flag set to %d\n",
3897 		    prop_list->sdt_lun_reset_enable);
3898 	}
3899 
3900 	/*
3901 	 * Validate the throttle values.
3902 	 * If any of the numbers are invalid, set everything to defaults.
3903 	 */
3904 	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
3905 	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
3906 	    (un->un_min_throttle > un->un_throttle)) {
3907 		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
3908 		un->un_min_throttle = sd_min_throttle;
3909 	}
3910 }
3911 
3912 /*
3913  * The following routines support reading and interpretation of disk labels,
3914  * including Solaris BE (8-slice) vtoc's, Solaris LE (16-slice) vtoc's, and
3915  * fdisk tables.
3916  */
3917 
3918 /*
3919  *    Function: sd_validate_geometry
3920  *
3921  * Description: Read the label from the disk (if present). Update the unit's
3922  *		geometry and vtoc information from the data in the label.
3923  *		Verify that the label is valid.
3924  *
3925  *   Arguments: un - driver soft state (unit) structure
3926  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
3927  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
3928  *			to use the USCSI "direct" chain and bypass the normal
3929  *			command waitq.
3930  *
3931  * Return Code: 0 - Successful completion
3932  *		EINVAL  - Invalid value in un->un_tgt_blocksize or
3933  *			  un->un_blockcount; or label on disk is corrupted
3934  *			  or unreadable.
3935  *		EACCES  - Reservation conflict at the device.
3936  *		ENOMEM  - Resource allocation error
3937  *		ENOTSUP - geometry not applicable
3938  *
3939  *     Context: Kernel thread only (can sleep).
3940  */
3941 
3942 static int
3943 sd_validate_geometry(struct sd_lun *un, int path_flag)
3944 {
3945 	static	char		labelstring[128];
3946 	static	char		buf[256];
3947 	char	*label		= NULL;
3948 	int	label_error	= 0;
3949 	int	gvalid		= un->un_f_geometry_is_valid;
3950 	int	lbasize;
3951 	uint_t	capacity;
3952 	int	count;
3953 
3954 	ASSERT(un != NULL);
3955 	ASSERT(mutex_owned(SD_MUTEX(un)));
3956 
3957 	/*
3958 	 * If the required values are not valid, then try getting them
3959 	 * once via read capacity. If that fails, then fail this call.
3960 	 * This is necessary with the new mpxio failover behavior in
3961 	 * the T300 where we can get an attach for the inactive path
3962 	 * before the active path. The inactive path fails commands with
3963 	 * sense data of 02,04,88 which happens to the read capacity
3964 	 * before mpxio has had sufficient knowledge to know if it should
3965 	 * force a fail over or not. (Which it won't do at attach anyhow).
3966 	 * If the read capacity at attach time fails, un_tgt_blocksize and
3967 	 * un_blockcount won't be valid.
3968 	 */
3969 	if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
3970 	    (un->un_f_blockcount_is_valid != TRUE)) {
3971 		uint64_t	cap;
3972 		uint32_t	lbasz;
3973 		int		rval;
3974 
3975 		mutex_exit(SD_MUTEX(un));
3976 		rval = sd_send_scsi_READ_CAPACITY(un, &cap,
3977 		    &lbasz, SD_PATH_DIRECT);
3978 		mutex_enter(SD_MUTEX(un));
3979 		if (rval == 0) {
3980 			/*
3981 			 * The following relies on
3982 			 * sd_send_scsi_READ_CAPACITY never
3983 			 * returning 0 for capacity and/or lbasize.
3984 			 */
3985 			sd_update_block_info(un, lbasz, cap);
3986 		}
3987 
3988 		if ((un->un_f_tgt_blocksize_is_valid != TRUE) ||
3989 		    (un->un_f_blockcount_is_valid != TRUE)) {
3990 			return (EINVAL);
3991 		}
3992 	}
3993 
3994 	/*
3995 	 * Copy the lbasize and capacity so that if they're reset while we're
3996 	 * not holding the SD_MUTEX, we will continue to use valid values
3997 	 * after the SD_MUTEX is reacquired. (4119659)
3998 	 */
3999 	lbasize  = un->un_tgt_blocksize;
4000 	capacity = un->un_blockcount;
4001 
4002 #if defined(_SUNOS_VTOC_16)
4003 	/*
4004 	 * Set up the "whole disk" fdisk partition; this should always
4005 	 * exist, regardless of whether the disk contains an fdisk table
4006 	 * or vtoc.
4007 	 */
4008 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
4009 	un->un_map[P0_RAW_DISK].dkl_nblk  = capacity;
4010 #endif
4011 
4012 	/*
4013 	 * Refresh the logical and physical geometry caches.
4014 	 * (data from MODE SENSE format/rigid disk geometry pages,
4015 	 * and scsi_ifgetcap("geometry").
4016 	 */
4017 	sd_resync_geom_caches(un, capacity, lbasize, path_flag);
4018 
4019 	label_error = sd_use_efi(un, path_flag);
4020 	if (label_error == 0) {
4021 		/* found a valid EFI label */
4022 		SD_TRACE(SD_LOG_IO_PARTITION, un,
4023 			"sd_validate_geometry: found EFI label\n");
4024 		un->un_solaris_offset = 0;
4025 		un->un_solaris_size = capacity;
4026 		return (ENOTSUP);
4027 	}
4028 	if (un->un_blockcount > DK_MAX_BLOCKS) {
4029 		if (label_error == ESRCH) {
4030 			/*
4031 			 * they've configured a LUN over 1TB, but used
4032 			 * format.dat to restrict format's view of the
4033 			 * capacity to be under 1TB
4034 			 */
4035 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4036 "is >1TB and has a VTOC label: use format(1M) to either decrease the");
4037 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
4038 "size to be < 1TB or relabel the disk with an EFI label");
4039 		} else {
4040 			/* unlabeled disk over 1TB */
4041 			return (ENOTSUP);
4042 		}
4043 	}
4044 	label_error = 0;
4045 
4046 	/*
4047 	 * at this point it is either labeled with a VTOC or it is
4048 	 * under 1TB
4049 	 */
4050 
4051 	/*
4052 	 * Only DIRECT ACCESS devices will have Sun labels.
4053 	 * CD's supposedly have a Sun label, too
4054 	 */
4055 	if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT || ISREMOVABLE(un)) {
4056 		struct	dk_label *dkl;
4057 		offset_t dkl1;
4058 		offset_t label_addr, real_addr;
4059 		int	rval;
4060 		size_t	buffer_size;
4061 
4062 		/*
4063 		 * Note: This will set up un->un_solaris_size and
4064 		 * un->un_solaris_offset.
4065 		 */
4066 		switch (sd_read_fdisk(un, capacity, lbasize, path_flag)) {
4067 		case SD_CMD_RESERVATION_CONFLICT:
4068 			ASSERT(mutex_owned(SD_MUTEX(un)));
4069 			return (EACCES);
4070 		case SD_CMD_FAILURE:
4071 			ASSERT(mutex_owned(SD_MUTEX(un)));
4072 			return (ENOMEM);
4073 		}
4074 
4075 		if (un->un_solaris_size <= DK_LABEL_LOC) {
4076 			/*
4077 			 * Found fdisk table but no Solaris partition entry,
4078 			 * so don't call sd_uselabel() and don't create
4079 			 * a default label.
4080 			 */
4081 			label_error = 0;
4082 			un->un_f_geometry_is_valid = TRUE;
4083 			goto no_solaris_partition;
4084 		}
4085 		label_addr = (daddr_t)(un->un_solaris_offset + DK_LABEL_LOC);
4086 
4087 		/*
4088 		 * sys_blocksize != tgt_blocksize, need to re-adjust
4089 		 * blkno and save the index to beginning of dk_label
4090 		 */
4091 		real_addr = SD_SYS2TGTBLOCK(un, label_addr);
4092 		buffer_size = SD_REQBYTES2TGTBYTES(un,
4093 		    sizeof (struct dk_label));
4094 
4095 		SD_TRACE(SD_LOG_IO_PARTITION, un, "sd_validate_geometry: "
4096 		    "label_addr: 0x%x allocation size: 0x%x\n",
4097 		    label_addr, buffer_size);
4098 		dkl = kmem_zalloc(buffer_size, KM_NOSLEEP);
4099 		if (dkl == NULL) {
4100 			return (ENOMEM);
4101 		}
4102 
4103 		mutex_exit(SD_MUTEX(un));
4104 		rval = sd_send_scsi_READ(un, dkl, buffer_size, real_addr,
4105 		    path_flag);
4106 		mutex_enter(SD_MUTEX(un));
4107 
4108 		switch (rval) {
4109 		case 0:
4110 			/*
4111 			 * sd_uselabel will establish that the geometry
4112 			 * is valid.
4113 			 * For sys_blocksize != tgt_blocksize, need
4114 			 * to index into the beginning of dk_label
4115 			 */
4116 			dkl1 = (daddr_t)dkl
4117 				+ SD_TGTBYTEOFFSET(un, label_addr, real_addr);
4118 			if (sd_uselabel(un, (struct dk_label *)(uintptr_t)dkl1,
4119 			    path_flag) != SD_LABEL_IS_VALID) {
4120 				label_error = EINVAL;
4121 			}
4122 			break;
4123 		case EACCES:
4124 			label_error = EACCES;
4125 			break;
4126 		default:
4127 			label_error = EINVAL;
4128 			break;
4129 		}
4130 
4131 		kmem_free(dkl, buffer_size);
4132 
4133 #if defined(_SUNOS_VTOC_8)
4134 		label = (char *)un->un_asciilabel;
4135 #elif defined(_SUNOS_VTOC_16)
4136 		label = (char *)un->un_vtoc.v_asciilabel;
4137 #else
4138 #error "No VTOC format defined."
4139 #endif
4140 	}
4141 
4142 	/*
4143 	 * If a valid label was not found, AND if no reservation conflict
4144 	 * was detected, then go ahead and create a default label (4069506).
4145 	 *
4146 	 * Note: currently, for VTOC_8 devices, the default label is created
4147 	 * for removables only.  For VTOC_16 devices, the default label will
4148 	 * be created for both removables and non-removables alike.
4149 	 * (see sd_build_default_label)
4150 	 */
4151 #if defined(_SUNOS_VTOC_8)
4152 	if (ISREMOVABLE(un) && (label_error != EACCES)) {
4153 #elif defined(_SUNOS_VTOC_16)
4154 	if (label_error != EACCES) {
4155 #endif
4156 		if (un->un_f_geometry_is_valid == FALSE) {
4157 			sd_build_default_label(un);
4158 		}
4159 		label_error = 0;
4160 	}
4161 
4162 no_solaris_partition:
4163 	if ((!ISREMOVABLE(un) ||
4164 	    (ISREMOVABLE(un) && un->un_mediastate == DKIO_EJECTED)) &&
4165 	    (un->un_state == SD_STATE_NORMAL && gvalid == FALSE)) {
4166 		/*
4167 		 * Print out a message indicating who and what we are.
4168 		 * We do this only when we happen to really validate the
4169 		 * geometry. We may call sd_validate_geometry() at other
4170 		 * times, e.g., ioctl()'s like Get VTOC in which case we
4171 		 * don't want to print the label.
4172 		 * If the geometry is valid, print the label string,
4173 		 * else print vendor and product info, if available
4174 		 */
4175 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
4176 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "?<%s>\n", label);
4177 		} else {
4178 			mutex_enter(&sd_label_mutex);
4179 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
4180 			    labelstring);
4181 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
4182 			    &labelstring[64]);
4183 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
4184 			    labelstring, &labelstring[64]);
4185 			if (un->un_f_blockcount_is_valid == TRUE) {
4186 				(void) sprintf(&buf[strlen(buf)],
4187 				    ", %llu %u byte blocks\n",
4188 				    (longlong_t)un->un_blockcount,
4189 				    un->un_tgt_blocksize);
4190 			} else {
4191 				(void) sprintf(&buf[strlen(buf)],
4192 				    ", (unknown capacity)\n");
4193 			}
4194 			SD_INFO(SD_LOG_ATTACH_DETACH, un, buf);
4195 			mutex_exit(&sd_label_mutex);
4196 		}
4197 	}
4198 
4199 #if defined(_SUNOS_VTOC_16)
4200 	/*
4201 	 * If we have valid geometry, set up the remaining fdisk partitions.
4202 	 * Note that dkl_cylno is not used for the fdisk map entries, so
4203 	 * we set it to an entirely bogus value.
4204 	 */
4205 	for (count = 0; count < FD_NUMPART; count++) {
4206 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
4207 		un->un_map[FDISK_P1 + count].dkl_nblk =
4208 		    un->un_fmap[count].fmap_nblk;
4209 
4210 		un->un_offset[FDISK_P1 + count] =
4211 		    un->un_fmap[count].fmap_start;
4212 	}
4213 #endif
4214 
4215 	for (count = 0; count < NDKMAP; count++) {
4216 #if defined(_SUNOS_VTOC_8)
4217 		struct dk_map *lp  = &un->un_map[count];
4218 		un->un_offset[count] =
4219 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
4220 #elif defined(_SUNOS_VTOC_16)
4221 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
4222 
4223 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
4224 #else
4225 #error "No VTOC format defined."
4226 #endif
4227 	}
4228 
4229 	return (label_error);
4230 }
4231 
4232 
4233 #if defined(_SUNOS_VTOC_16)
4234 /*
4235  * Macro: MAX_BLKS
4236  *
4237  *	This macro is used for table entries where we need to have the largest
4238  *	possible sector value for that head & SPT (sectors per track)
4239  *	combination.  Other entries for some smaller disk sizes are set by
4240  *	convention to match those used by X86 BIOS usage.
4241  */
4242 #define	MAX_BLKS(heads, spt)	UINT16_MAX * heads * spt, heads, spt
4243 
4244 /*
4245  *    Function: sd_convert_geometry
4246  *
4247  * Description: Convert physical geometry into a dk_geom structure. In
4248  *		other words, make sure we don't wrap 16-bit values.
4249  *		e.g. converting from geom_cache to dk_geom
4250  *
4251  *     Context: Kernel thread only
4252  */
4253 static void
4254 sd_convert_geometry(uint64_t capacity, struct dk_geom *un_g)
4255 {
4256 	int i;
4257 	static const struct chs_values {
4258 		uint_t max_cap;		/* Max Capacity for this HS. */
4259 		uint_t nhead;		/* Heads to use. */
4260 		uint_t nsect;		/* SPT to use. */
4261 	} CHS_values[] = {
4262 		{0x00200000,  64, 32},		/* 1GB or smaller disk. */
4263 		{0x01000000, 128, 32},		/* 8GB or smaller disk. */
4264 		{MAX_BLKS(255,  63)},		/* 502.02GB or smaller disk. */
4265 		{MAX_BLKS(255, 126)},		/* .98TB or smaller disk. */
4266 		{DK_MAX_BLOCKS, 255, 189}	/* Max size is just under 1TB */
4267 	};
4268 
4269 	/* Unlabeled SCSI floppy device */
4270 	if (capacity <= 0x1000) {
4271 		un_g->dkg_nhead = 2;
4272 		un_g->dkg_ncyl = 80;
4273 		un_g->dkg_nsect = capacity / (un_g->dkg_nhead * un_g->dkg_ncyl);
4274 		return;
4275 	}
4276 
4277 	/*
4278 	 * For all devices we calculate cylinders using the
4279 	 * heads and sectors we assign based on capacity of the
4280 	 * device.  The table is designed to be compatible with the
4281 	 * way other operating systems lay out fdisk tables for X86
4282 	 * and to insure that the cylinders never exceed 65535 to
4283 	 * prevent problems with X86 ioctls that report geometry.
4284 	 * We use SPT that are multiples of 63, since other OSes that
4285 	 * are not limited to 16-bits for cylinders stop at 63 SPT
4286 	 * we make do by using multiples of 63 SPT.
4287 	 *
4288 	 * Note than capacities greater than or equal to 1TB will simply
4289 	 * get the largest geometry from the table. This should be okay
4290 	 * since disks this large shouldn't be using CHS values anyway.
4291 	 */
4292 	for (i = 0; CHS_values[i].max_cap < capacity &&
4293 	    CHS_values[i].max_cap != DK_MAX_BLOCKS; i++)
4294 		;
4295 
4296 	un_g->dkg_nhead = CHS_values[i].nhead;
4297 	un_g->dkg_nsect = CHS_values[i].nsect;
4298 }
4299 #endif
4300 
4301 
4302 /*
4303  *    Function: sd_resync_geom_caches
4304  *
4305  * Description: (Re)initialize both geometry caches: the virtual geometry
4306  *		information is extracted from the HBA (the "geometry"
4307  *		capability), and the physical geometry cache data is
4308  *		generated by issuing MODE SENSE commands.
4309  *
4310  *   Arguments: un - driver soft state (unit) structure
4311  *		capacity - disk capacity in #blocks
4312  *		lbasize - disk block size in bytes
4313  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4314  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4315  *			to use the USCSI "direct" chain and bypass the normal
4316  *			command waitq.
4317  *
4318  *     Context: Kernel thread only (can sleep).
4319  */
4320 
4321 static void
4322 sd_resync_geom_caches(struct sd_lun *un, int capacity, int lbasize,
4323 	int path_flag)
4324 {
4325 	struct 	geom_cache 	pgeom;
4326 	struct 	geom_cache	*pgeom_p = &pgeom;
4327 	int 	spc;
4328 	unsigned short nhead;
4329 	unsigned short nsect;
4330 
4331 	ASSERT(un != NULL);
4332 	ASSERT(mutex_owned(SD_MUTEX(un)));
4333 
4334 	/*
4335 	 * Ask the controller for its logical geometry.
4336 	 * Note: if the HBA does not support scsi_ifgetcap("geometry"),
4337 	 * then the lgeom cache will be invalid.
4338 	 */
4339 	sd_get_virtual_geometry(un, capacity, lbasize);
4340 
4341 	/*
4342 	 * Initialize the pgeom cache from lgeom, so that if MODE SENSE
4343 	 * doesn't work, DKIOCG_PHYSGEOM can return reasonable values.
4344 	 */
4345 	if (un->un_lgeom.g_nsect == 0 || un->un_lgeom.g_nhead == 0) {
4346 		/*
4347 		 * Note: Perhaps this needs to be more adaptive? The rationale
4348 		 * is that, if there's no HBA geometry from the HBA driver, any
4349 		 * guess is good, since this is the physical geometry. If MODE
4350 		 * SENSE fails this gives a max cylinder size for non-LBA access
4351 		 */
4352 		nhead = 255;
4353 		nsect = 63;
4354 	} else {
4355 		nhead = un->un_lgeom.g_nhead;
4356 		nsect = un->un_lgeom.g_nsect;
4357 	}
4358 
4359 	if (ISCD(un)) {
4360 		pgeom_p->g_nhead = 1;
4361 		pgeom_p->g_nsect = nsect * nhead;
4362 	} else {
4363 		pgeom_p->g_nhead = nhead;
4364 		pgeom_p->g_nsect = nsect;
4365 	}
4366 
4367 	spc = pgeom_p->g_nhead * pgeom_p->g_nsect;
4368 	pgeom_p->g_capacity = capacity;
4369 	pgeom_p->g_ncyl = pgeom_p->g_capacity / spc;
4370 	pgeom_p->g_acyl = 0;
4371 
4372 	/*
4373 	 * Retrieve fresh geometry data from the hardware, stash it
4374 	 * here temporarily before we rebuild the incore label.
4375 	 *
4376 	 * We want to use the MODE SENSE commands to derive the
4377 	 * physical geometry of the device, but if either command
4378 	 * fails, the logical geometry is used as the fallback for
4379 	 * disk label geometry.
4380 	 */
4381 	mutex_exit(SD_MUTEX(un));
4382 	sd_get_physical_geometry(un, pgeom_p, capacity, lbasize, path_flag);
4383 	mutex_enter(SD_MUTEX(un));
4384 
4385 	/*
4386 	 * Now update the real copy while holding the mutex. This
4387 	 * way the global copy is never in an inconsistent state.
4388 	 */
4389 	bcopy(pgeom_p, &un->un_pgeom,  sizeof (un->un_pgeom));
4390 
4391 	SD_INFO(SD_LOG_COMMON, un, "sd_resync_geom_caches: "
4392 	    "(cached from lgeom)\n");
4393 	SD_INFO(SD_LOG_COMMON, un,
4394 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4395 	    un->un_pgeom.g_ncyl, un->un_pgeom.g_acyl,
4396 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
4397 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
4398 	    "intrlv: %d; rpm: %d\n", un->un_pgeom.g_secsize,
4399 	    un->un_pgeom.g_capacity, un->un_pgeom.g_intrlv,
4400 	    un->un_pgeom.g_rpm);
4401 }
4402 
4403 
4404 /*
4405  *    Function: sd_read_fdisk
4406  *
4407  * Description: utility routine to read the fdisk table.
4408  *
4409  *   Arguments: un - driver soft state (unit) structure
4410  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4411  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4412  *			to use the USCSI "direct" chain and bypass the normal
4413  *			command waitq.
4414  *
4415  * Return Code: SD_CMD_SUCCESS
4416  *		SD_CMD_FAILURE
4417  *
4418  *     Context: Kernel thread only (can sleep).
4419  */
4420 /* ARGSUSED */
4421 static int
4422 sd_read_fdisk(struct sd_lun *un, uint_t capacity, int lbasize, int path_flag)
4423 {
4424 #if defined(_NO_FDISK_PRESENT)
4425 
4426 	un->un_solaris_offset = 0;
4427 	un->un_solaris_size = capacity;
4428 	bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4429 	return (SD_CMD_SUCCESS);
4430 
4431 #elif defined(_FIRMWARE_NEEDS_FDISK)
4432 
4433 	struct ipart	*fdp;
4434 	struct mboot	*mbp;
4435 	struct ipart	fdisk[FD_NUMPART];
4436 	int		i;
4437 	char		sigbuf[2];
4438 	caddr_t		bufp;
4439 	int		uidx;
4440 	int		rval;
4441 	int		lba = 0;
4442 	uint_t		solaris_offset;	/* offset to solaris part. */
4443 	daddr_t		solaris_size;	/* size of solaris partition */
4444 	uint32_t	blocksize;
4445 
4446 	ASSERT(un != NULL);
4447 	ASSERT(mutex_owned(SD_MUTEX(un)));
4448 	ASSERT(un->un_f_tgt_blocksize_is_valid == TRUE);
4449 
4450 	blocksize = un->un_tgt_blocksize;
4451 
4452 	/*
4453 	 * Start off assuming no fdisk table
4454 	 */
4455 	solaris_offset = 0;
4456 	solaris_size   = capacity;
4457 
4458 	mutex_exit(SD_MUTEX(un));
4459 	bufp = kmem_zalloc(blocksize, KM_SLEEP);
4460 	rval = sd_send_scsi_READ(un, bufp, blocksize, 0, path_flag);
4461 	mutex_enter(SD_MUTEX(un));
4462 
4463 	if (rval != 0) {
4464 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4465 		    "sd_read_fdisk: fdisk read err\n");
4466 		kmem_free(bufp, blocksize);
4467 		return (SD_CMD_FAILURE);
4468 	}
4469 
4470 	mbp = (struct mboot *)bufp;
4471 
4472 	/*
4473 	 * The fdisk table does not begin on a 4-byte boundary within the
4474 	 * master boot record, so we copy it to an aligned structure to avoid
4475 	 * alignment exceptions on some processors.
4476 	 */
4477 	bcopy(&mbp->parts[0], fdisk, sizeof (fdisk));
4478 
4479 	/*
4480 	 * Check for lba support before verifying sig; sig might not be
4481 	 * there, say on a blank disk, but the max_chs mark may still
4482 	 * be present.
4483 	 *
4484 	 * Note: LBA support and BEFs are an x86-only concept but this
4485 	 * code should work OK on SPARC as well.
4486 	 */
4487 
4488 	/*
4489 	 * First, check for lba-access-ok on root node (or prom root node)
4490 	 * if present there, don't need to search fdisk table.
4491 	 */
4492 	if (ddi_getprop(DDI_DEV_T_ANY, ddi_root_node(), 0,
4493 	    "lba-access-ok", 0) != 0) {
4494 		/* All drives do LBA; don't search fdisk table */
4495 		lba = 1;
4496 	} else {
4497 		/* Okay, look for mark in fdisk table */
4498 		for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4499 			/* accumulate "lba" value from all partitions */
4500 			lba = (lba || sd_has_max_chs_vals(fdp));
4501 		}
4502 	}
4503 
4504 	/*
4505 	 * Next, look for 'no-bef-lba-access' prop on parent.
4506 	 * Its presence means the realmode driver doesn't support
4507 	 * LBA, so the target driver shouldn't advertise it as ok.
4508 	 * This should be a temporary condition; one day all
4509 	 * BEFs should support the LBA access functions.
4510 	 */
4511 	if ((lba != 0) && (ddi_getprop(DDI_DEV_T_ANY,
4512 	    ddi_get_parent(SD_DEVINFO(un)), DDI_PROP_DONTPASS,
4513 	    "no-bef-lba-access", 0) != 0)) {
4514 		/* BEF doesn't support LBA; don't advertise it as ok */
4515 		lba = 0;
4516 	}
4517 
4518 	if (lba != 0) {
4519 		dev_t dev = sd_make_device(SD_DEVINFO(un));
4520 
4521 		if (ddi_getprop(dev, SD_DEVINFO(un), DDI_PROP_DONTPASS,
4522 		    "lba-access-ok", 0) == 0) {
4523 			/* not found; create it */
4524 			if (ddi_prop_create(dev, SD_DEVINFO(un), 0,
4525 			    "lba-access-ok", (caddr_t)NULL, 0) !=
4526 			    DDI_PROP_SUCCESS) {
4527 				SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4528 				    "sd_read_fdisk: Can't create lba property "
4529 				    "for instance %d\n",
4530 				    ddi_get_instance(SD_DEVINFO(un)));
4531 			}
4532 		}
4533 	}
4534 
4535 	bcopy(&mbp->signature, sigbuf, sizeof (sigbuf));
4536 
4537 	/*
4538 	 * Endian-independent signature check
4539 	 */
4540 	if (((sigbuf[1] & 0xFF) != ((MBB_MAGIC >> 8) & 0xFF)) ||
4541 	    (sigbuf[0] != (MBB_MAGIC & 0xFF))) {
4542 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
4543 		    "sd_read_fdisk: no fdisk\n");
4544 		bzero(un->un_fmap, sizeof (struct fmap) * FD_NUMPART);
4545 		rval = SD_CMD_SUCCESS;
4546 		goto done;
4547 	}
4548 
4549 #ifdef SDDEBUG
4550 	if (sd_level_mask & SD_LOGMASK_INFO) {
4551 		fdp = fdisk;
4552 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_read_fdisk:\n");
4553 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "         relsect    "
4554 		    "numsect         sysid       bootid\n");
4555 		for (i = 0; i < FD_NUMPART; i++, fdp++) {
4556 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4557 			    "    %d:  %8d   %8d     0x%08x     0x%08x\n",
4558 			    i, fdp->relsect, fdp->numsect,
4559 			    fdp->systid, fdp->bootid);
4560 		}
4561 	}
4562 #endif
4563 
4564 	/*
4565 	 * Try to find the unix partition
4566 	 */
4567 	uidx = -1;
4568 	solaris_offset = 0;
4569 	solaris_size   = 0;
4570 
4571 	for (fdp = fdisk, i = 0; i < FD_NUMPART; i++, fdp++) {
4572 		int	relsect;
4573 		int	numsect;
4574 
4575 		if (fdp->numsect == 0) {
4576 			un->un_fmap[i].fmap_start = 0;
4577 			un->un_fmap[i].fmap_nblk  = 0;
4578 			continue;
4579 		}
4580 
4581 		/*
4582 		 * Data in the fdisk table is little-endian.
4583 		 */
4584 		relsect = LE_32(fdp->relsect);
4585 		numsect = LE_32(fdp->numsect);
4586 
4587 		un->un_fmap[i].fmap_start = relsect;
4588 		un->un_fmap[i].fmap_nblk  = numsect;
4589 
4590 		if (fdp->systid != SUNIXOS &&
4591 		    fdp->systid != SUNIXOS2 &&
4592 		    fdp->systid != EFI_PMBR) {
4593 			continue;
4594 		}
4595 
4596 		/*
4597 		 * use the last active solaris partition id found
4598 		 * (there should only be 1 active partition id)
4599 		 *
4600 		 * if there are no active solaris partition id
4601 		 * then use the first inactive solaris partition id
4602 		 */
4603 		if ((uidx == -1) || (fdp->bootid == ACTIVE)) {
4604 			uidx = i;
4605 			solaris_offset = relsect;
4606 			solaris_size   = numsect;
4607 		}
4608 	}
4609 
4610 	SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk 0x%x 0x%lx",
4611 	    un->un_solaris_offset, un->un_solaris_size);
4612 
4613 	rval = SD_CMD_SUCCESS;
4614 
4615 done:
4616 
4617 	/*
4618 	 * Clear the VTOC info, only if the Solaris partition entry
4619 	 * has moved, changed size, been deleted, or if the size of
4620 	 * the partition is too small to even fit the label sector.
4621 	 */
4622 	if ((un->un_solaris_offset != solaris_offset) ||
4623 	    (un->un_solaris_size != solaris_size) ||
4624 	    solaris_size <= DK_LABEL_LOC) {
4625 		SD_INFO(SD_LOG_ATTACH_DETACH, un, "fdisk moved 0x%x 0x%lx",
4626 			solaris_offset, solaris_size);
4627 		bzero(&un->un_g, sizeof (struct dk_geom));
4628 		bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
4629 		bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
4630 		un->un_f_geometry_is_valid = FALSE;
4631 	}
4632 	un->un_solaris_offset = solaris_offset;
4633 	un->un_solaris_size = solaris_size;
4634 	kmem_free(bufp, blocksize);
4635 	return (rval);
4636 
4637 #else	/* #elif defined(_FIRMWARE_NEEDS_FDISK) */
4638 #error "fdisk table presence undetermined for this platform."
4639 #endif	/* #if defined(_NO_FDISK_PRESENT) */
4640 }
4641 
4642 
4643 /*
4644  *    Function: sd_get_physical_geometry
4645  *
4646  * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4647  *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4648  *		target, and use this information to initialize the physical
4649  *		geometry cache specified by pgeom_p.
4650  *
4651  *		MODE SENSE is an optional command, so failure in this case
4652  *		does not necessarily denote an error. We want to use the
4653  *		MODE SENSE commands to derive the physical geometry of the
4654  *		device, but if either command fails, the logical geometry is
4655  *		used as the fallback for disk label geometry.
4656  *
4657  *		This requires that un->un_blockcount and un->un_tgt_blocksize
4658  *		have already been initialized for the current target and
4659  *		that the current values be passed as args so that we don't
4660  *		end up ever trying to use -1 as a valid value. This could
4661  *		happen if either value is reset while we're not holding
4662  *		the mutex.
4663  *
4664  *   Arguments: un - driver soft state (unit) structure
4665  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4666  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4667  *			to use the USCSI "direct" chain and bypass the normal
4668  *			command waitq.
4669  *
4670  *     Context: Kernel thread only (can sleep).
4671  */
4672 
4673 static void
4674 sd_get_physical_geometry(struct sd_lun *un, struct geom_cache *pgeom_p,
4675 	int capacity, int lbasize, int path_flag)
4676 {
4677 	struct	mode_format	*page3p;
4678 	struct	mode_geometry	*page4p;
4679 	struct	mode_header	*headerp;
4680 	int	sector_size;
4681 	int	nsect;
4682 	int	nhead;
4683 	int	ncyl;
4684 	int	intrlv;
4685 	int	spc;
4686 	int	modesense_capacity;
4687 	int	rpm;
4688 	int	bd_len;
4689 	int	mode_header_length;
4690 	uchar_t	*p3bufp;
4691 	uchar_t	*p4bufp;
4692 	int	cdbsize;
4693 
4694 	ASSERT(un != NULL);
4695 	ASSERT(!(mutex_owned(SD_MUTEX(un))));
4696 
4697 	if (un->un_f_blockcount_is_valid != TRUE) {
4698 		return;
4699 	}
4700 
4701 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
4702 		return;
4703 	}
4704 
4705 	if (lbasize == 0) {
4706 		if (ISCD(un)) {
4707 			lbasize = 2048;
4708 		} else {
4709 			lbasize = un->un_sys_blocksize;
4710 		}
4711 	}
4712 	pgeom_p->g_secsize = (unsigned short)lbasize;
4713 
4714 	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4715 
4716 	/*
4717 	 * Retrieve MODE SENSE page 3 - Format Device Page
4718 	 */
4719 	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
4720 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p3bufp,
4721 	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag)
4722 	    != 0) {
4723 		SD_ERROR(SD_LOG_COMMON, un,
4724 		    "sd_get_physical_geometry: mode sense page 3 failed\n");
4725 		goto page3_exit;
4726 	}
4727 
4728 	/*
4729 	 * Determine size of Block Descriptors in order to locate the mode
4730 	 * page data.  ATAPI devices return 0, SCSI devices should return
4731 	 * MODE_BLK_DESC_LENGTH.
4732 	 */
4733 	headerp = (struct mode_header *)p3bufp;
4734 	if (un->un_f_cfg_is_atapi == TRUE) {
4735 		struct mode_header_grp2 *mhp =
4736 		    (struct mode_header_grp2 *)headerp;
4737 		mode_header_length = MODE_HEADER_LENGTH_GRP2;
4738 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4739 	} else {
4740 		mode_header_length = MODE_HEADER_LENGTH;
4741 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
4742 	}
4743 
4744 	if (bd_len > MODE_BLK_DESC_LENGTH) {
4745 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4746 		    "received unexpected bd_len of %d, page3\n", bd_len);
4747 		goto page3_exit;
4748 	}
4749 
4750 	page3p = (struct mode_format *)
4751 	    ((caddr_t)headerp + mode_header_length + bd_len);
4752 
4753 	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
4754 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4755 		    "mode sense pg3 code mismatch %d\n",
4756 		    page3p->mode_page.code);
4757 		goto page3_exit;
4758 	}
4759 
4760 	/*
4761 	 * Use this physical geometry data only if BOTH MODE SENSE commands
4762 	 * complete successfully; otherwise, revert to the logical geometry.
4763 	 * So, we need to save everything in temporary variables.
4764 	 */
4765 	sector_size = BE_16(page3p->data_bytes_sect);
4766 
4767 	/*
4768 	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
4769 	 */
4770 	if (sector_size == 0) {
4771 		sector_size = (ISCD(un)) ? 2048 : un->un_sys_blocksize;
4772 	} else {
4773 		sector_size &= ~(un->un_sys_blocksize - 1);
4774 	}
4775 
4776 	nsect  = BE_16(page3p->sect_track);
4777 	intrlv = BE_16(page3p->interleave);
4778 
4779 	SD_INFO(SD_LOG_COMMON, un,
4780 	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
4781 	SD_INFO(SD_LOG_COMMON, un,
4782 	    "   mode page: %d; nsect: %d; sector size: %d;\n",
4783 	    page3p->mode_page.code, nsect, sector_size);
4784 	SD_INFO(SD_LOG_COMMON, un,
4785 	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
4786 	    BE_16(page3p->track_skew),
4787 	    BE_16(page3p->cylinder_skew));
4788 
4789 
4790 	/*
4791 	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
4792 	 */
4793 	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
4794 	if (sd_send_scsi_MODE_SENSE(un, cdbsize, p4bufp,
4795 	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag)
4796 	    != 0) {
4797 		SD_ERROR(SD_LOG_COMMON, un,
4798 		    "sd_get_physical_geometry: mode sense page 4 failed\n");
4799 		goto page4_exit;
4800 	}
4801 
4802 	/*
4803 	 * Determine size of Block Descriptors in order to locate the mode
4804 	 * page data.  ATAPI devices return 0, SCSI devices should return
4805 	 * MODE_BLK_DESC_LENGTH.
4806 	 */
4807 	headerp = (struct mode_header *)p4bufp;
4808 	if (un->un_f_cfg_is_atapi == TRUE) {
4809 		struct mode_header_grp2 *mhp =
4810 		    (struct mode_header_grp2 *)headerp;
4811 		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
4812 	} else {
4813 		bd_len = ((struct mode_header *)headerp)->bdesc_length;
4814 	}
4815 
4816 	if (bd_len > MODE_BLK_DESC_LENGTH) {
4817 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4818 		    "received unexpected bd_len of %d, page4\n", bd_len);
4819 		goto page4_exit;
4820 	}
4821 
4822 	page4p = (struct mode_geometry *)
4823 	    ((caddr_t)headerp + mode_header_length + bd_len);
4824 
4825 	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
4826 		SD_ERROR(SD_LOG_COMMON, un, "sd_get_physical_geometry: "
4827 		    "mode sense pg4 code mismatch %d\n",
4828 		    page4p->mode_page.code);
4829 		goto page4_exit;
4830 	}
4831 
4832 	/*
4833 	 * Stash the data now, after we know that both commands completed.
4834 	 */
4835 
4836 	mutex_enter(SD_MUTEX(un));
4837 
4838 	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
4839 	spc   = nhead * nsect;
4840 	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
4841 	rpm   = BE_16(page4p->rpm);
4842 
4843 	modesense_capacity = spc * ncyl;
4844 
4845 	SD_INFO(SD_LOG_COMMON, un,
4846 	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
4847 	SD_INFO(SD_LOG_COMMON, un,
4848 	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
4849 	SD_INFO(SD_LOG_COMMON, un,
4850 	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
4851 	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
4852 	    (void *)pgeom_p, capacity);
4853 
4854 	/*
4855 	 * Compensate if the drive's geometry is not rectangular, i.e.,
4856 	 * the product of C * H * S returned by MODE SENSE >= that returned
4857 	 * by read capacity. This is an idiosyncrasy of the original x86
4858 	 * disk subsystem.
4859 	 */
4860 	if (modesense_capacity >= capacity) {
4861 		SD_INFO(SD_LOG_COMMON, un,
4862 		    "sd_get_physical_geometry: adjusting acyl; "
4863 		    "old: %d; new: %d\n", pgeom_p->g_acyl,
4864 		    (modesense_capacity - capacity + spc - 1) / spc);
4865 		if (sector_size != 0) {
4866 			/* 1243403: NEC D38x7 drives don't support sec size */
4867 			pgeom_p->g_secsize = (unsigned short)sector_size;
4868 		}
4869 		pgeom_p->g_nsect    = (unsigned short)nsect;
4870 		pgeom_p->g_nhead    = (unsigned short)nhead;
4871 		pgeom_p->g_capacity = capacity;
4872 		pgeom_p->g_acyl	    =
4873 		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
4874 		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
4875 	}
4876 
4877 	pgeom_p->g_rpm    = (unsigned short)rpm;
4878 	pgeom_p->g_intrlv = (unsigned short)intrlv;
4879 
4880 	SD_INFO(SD_LOG_COMMON, un,
4881 	    "sd_get_physical_geometry: mode sense geometry:\n");
4882 	SD_INFO(SD_LOG_COMMON, un,
4883 	    "   nsect: %d; sector size: %d; interlv: %d\n",
4884 	    nsect, sector_size, intrlv);
4885 	SD_INFO(SD_LOG_COMMON, un,
4886 	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
4887 	    nhead, ncyl, rpm, modesense_capacity);
4888 	SD_INFO(SD_LOG_COMMON, un,
4889 	    "sd_get_physical_geometry: (cached)\n");
4890 	SD_INFO(SD_LOG_COMMON, un,
4891 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4892 	    un->un_pgeom.g_ncyl,  un->un_pgeom.g_acyl,
4893 	    un->un_pgeom.g_nhead, un->un_pgeom.g_nsect);
4894 	SD_INFO(SD_LOG_COMMON, un,
4895 	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
4896 	    un->un_pgeom.g_secsize, un->un_pgeom.g_capacity,
4897 	    un->un_pgeom.g_intrlv, un->un_pgeom.g_rpm);
4898 
4899 	mutex_exit(SD_MUTEX(un));
4900 
4901 page4_exit:
4902 	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
4903 page3_exit:
4904 	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
4905 }
4906 
4907 
4908 /*
4909  *    Function: sd_get_virtual_geometry
4910  *
4911  * Description: Ask the controller to tell us about the target device.
4912  *
4913  *   Arguments: un - pointer to softstate
4914  *		capacity - disk capacity in #blocks
4915  *		lbasize - disk block size in bytes
4916  *
4917  *     Context: Kernel thread only
4918  */
4919 
4920 static void
4921 sd_get_virtual_geometry(struct sd_lun *un, int capacity, int lbasize)
4922 {
4923 	struct	geom_cache 	*lgeom_p = &un->un_lgeom;
4924 	uint_t	geombuf;
4925 	int	spc;
4926 
4927 	ASSERT(un != NULL);
4928 	ASSERT(mutex_owned(SD_MUTEX(un)));
4929 
4930 	mutex_exit(SD_MUTEX(un));
4931 
4932 	/* Set sector size, and total number of sectors */
4933 	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
4934 	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
4935 
4936 	/* Let the HBA tell us its geometry */
4937 	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
4938 
4939 	mutex_enter(SD_MUTEX(un));
4940 
4941 	/* A value of -1 indicates an undefined "geometry" property */
4942 	if (geombuf == (-1)) {
4943 		return;
4944 	}
4945 
4946 	/* Initialize the logical geometry cache. */
4947 	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
4948 	lgeom_p->g_nsect   = geombuf & 0xffff;
4949 	lgeom_p->g_secsize = un->un_sys_blocksize;
4950 
4951 	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
4952 
4953 	/*
4954 	 * Note: The driver originally converted the capacity value from
4955 	 * target blocks to system blocks. However, the capacity value passed
4956 	 * to this routine is already in terms of system blocks (this scaling
4957 	 * is done when the READ CAPACITY command is issued and processed).
4958 	 * This 'error' may have gone undetected because the usage of g_ncyl
4959 	 * (which is based upon g_capacity) is very limited within the driver
4960 	 */
4961 	lgeom_p->g_capacity = capacity;
4962 
4963 	/*
4964 	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
4965 	 * hba may return zero values if the device has been removed.
4966 	 */
4967 	if (spc == 0) {
4968 		lgeom_p->g_ncyl = 0;
4969 	} else {
4970 		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
4971 	}
4972 	lgeom_p->g_acyl = 0;
4973 
4974 	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
4975 	SD_INFO(SD_LOG_COMMON, un,
4976 	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
4977 	    un->un_lgeom.g_ncyl,  un->un_lgeom.g_acyl,
4978 	    un->un_lgeom.g_nhead, un->un_lgeom.g_nsect);
4979 	SD_INFO(SD_LOG_COMMON, un, "   lbasize: %d; capacity: %ld; "
4980 	    "intrlv: %d; rpm: %d\n", un->un_lgeom.g_secsize,
4981 	    un->un_lgeom.g_capacity, un->un_lgeom.g_intrlv, un->un_lgeom.g_rpm);
4982 }
4983 
4984 
4985 /*
4986  *    Function: sd_update_block_info
4987  *
4988  * Description: Calculate a byte count to sector count bitshift value
4989  *		from sector size.
4990  *
4991  *   Arguments: un: unit struct.
4992  *		lbasize: new target sector size
4993  *		capacity: new target capacity, ie. block count
4994  *
4995  *     Context: Kernel thread context
4996  */
4997 
4998 static void
4999 sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5000 {
5001 	if (lbasize != 0) {
5002 		un->un_tgt_blocksize = lbasize;
5003 		un->un_f_tgt_blocksize_is_valid	= TRUE;
5004 	}
5005 
5006 	if (capacity != 0) {
5007 		un->un_blockcount		= capacity;
5008 		un->un_f_blockcount_is_valid	= TRUE;
5009 	}
5010 }
5011 
5012 
5013 static void
5014 sd_swap_efi_gpt(efi_gpt_t *e)
5015 {
5016 	_NOTE(ASSUMING_PROTECTED(*e))
5017 	e->efi_gpt_Signature = LE_64(e->efi_gpt_Signature);
5018 	e->efi_gpt_Revision = LE_32(e->efi_gpt_Revision);
5019 	e->efi_gpt_HeaderSize = LE_32(e->efi_gpt_HeaderSize);
5020 	e->efi_gpt_HeaderCRC32 = LE_32(e->efi_gpt_HeaderCRC32);
5021 	e->efi_gpt_MyLBA = LE_64(e->efi_gpt_MyLBA);
5022 	e->efi_gpt_AlternateLBA = LE_64(e->efi_gpt_AlternateLBA);
5023 	e->efi_gpt_FirstUsableLBA = LE_64(e->efi_gpt_FirstUsableLBA);
5024 	e->efi_gpt_LastUsableLBA = LE_64(e->efi_gpt_LastUsableLBA);
5025 	UUID_LE_CONVERT(e->efi_gpt_DiskGUID, e->efi_gpt_DiskGUID);
5026 	e->efi_gpt_PartitionEntryLBA = LE_64(e->efi_gpt_PartitionEntryLBA);
5027 	e->efi_gpt_NumberOfPartitionEntries =
5028 	    LE_32(e->efi_gpt_NumberOfPartitionEntries);
5029 	e->efi_gpt_SizeOfPartitionEntry =
5030 	    LE_32(e->efi_gpt_SizeOfPartitionEntry);
5031 	e->efi_gpt_PartitionEntryArrayCRC32 =
5032 	    LE_32(e->efi_gpt_PartitionEntryArrayCRC32);
5033 }
5034 
5035 static void
5036 sd_swap_efi_gpe(int nparts, efi_gpe_t *p)
5037 {
5038 	int i;
5039 
5040 	_NOTE(ASSUMING_PROTECTED(*p))
5041 	for (i = 0; i < nparts; i++) {
5042 		UUID_LE_CONVERT(p[i].efi_gpe_PartitionTypeGUID,
5043 		    p[i].efi_gpe_PartitionTypeGUID);
5044 		p[i].efi_gpe_StartingLBA = LE_64(p[i].efi_gpe_StartingLBA);
5045 		p[i].efi_gpe_EndingLBA = LE_64(p[i].efi_gpe_EndingLBA);
5046 		/* PartitionAttrs */
5047 	}
5048 }
5049 
5050 static int
5051 sd_validate_efi(efi_gpt_t *labp)
5052 {
5053 	if (labp->efi_gpt_Signature != EFI_SIGNATURE)
5054 		return (EINVAL);
5055 	/* at least 96 bytes in this version of the spec. */
5056 	if (sizeof (efi_gpt_t) - sizeof (labp->efi_gpt_Reserved2) >
5057 	    labp->efi_gpt_HeaderSize)
5058 		return (EINVAL);
5059 	/* this should be 128 bytes */
5060 	if (labp->efi_gpt_SizeOfPartitionEntry != sizeof (efi_gpe_t))
5061 		return (EINVAL);
5062 	return (0);
5063 }
5064 
5065 static int
5066 sd_use_efi(struct sd_lun *un, int path_flag)
5067 {
5068 	int		i;
5069 	int		rval = 0;
5070 	efi_gpe_t	*partitions;
5071 	uchar_t		*buf;
5072 	uint_t		lbasize;
5073 	uint64_t	cap;
5074 	uint_t		nparts;
5075 	diskaddr_t	gpe_lba;
5076 
5077 	ASSERT(mutex_owned(SD_MUTEX(un)));
5078 	lbasize = un->un_tgt_blocksize;
5079 
5080 	mutex_exit(SD_MUTEX(un));
5081 
5082 	buf = kmem_zalloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
5083 
5084 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
5085 		rval = EINVAL;
5086 		goto done_err;
5087 	}
5088 
5089 	rval = sd_send_scsi_READ(un, buf, lbasize, 0, path_flag);
5090 	if (rval) {
5091 		goto done_err;
5092 	}
5093 	if (((struct dk_label *)buf)->dkl_magic == DKL_MAGIC) {
5094 		/* not ours */
5095 		rval = ESRCH;
5096 		goto done_err;
5097 	}
5098 
5099 	rval = sd_send_scsi_READ(un, buf, lbasize, 1, path_flag);
5100 	if (rval) {
5101 		goto done_err;
5102 	}
5103 	sd_swap_efi_gpt((efi_gpt_t *)buf);
5104 
5105 	if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0) {
5106 		/*
5107 		 * Couldn't read the primary, try the backup.  Our
5108 		 * capacity at this point could be based on CHS, so
5109 		 * check what the device reports.
5110 		 */
5111 		rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
5112 		    path_flag);
5113 		if (rval) {
5114 			goto done_err;
5115 		}
5116 		if ((rval = sd_send_scsi_READ(un, buf, lbasize,
5117 		    cap - 1, path_flag)) != 0) {
5118 			goto done_err;
5119 		}
5120 		sd_swap_efi_gpt((efi_gpt_t *)buf);
5121 		if ((rval = sd_validate_efi((efi_gpt_t *)buf)) != 0)
5122 			goto done_err;
5123 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5124 		    "primary label corrupt; using backup\n");
5125 	}
5126 
5127 	nparts = ((efi_gpt_t *)buf)->efi_gpt_NumberOfPartitionEntries;
5128 	gpe_lba = ((efi_gpt_t *)buf)->efi_gpt_PartitionEntryLBA;
5129 
5130 	rval = sd_send_scsi_READ(un, buf, EFI_MIN_ARRAY_SIZE, gpe_lba,
5131 	    path_flag);
5132 	if (rval) {
5133 		goto done_err;
5134 	}
5135 	partitions = (efi_gpe_t *)buf;
5136 
5137 	if (nparts > MAXPART) {
5138 		nparts = MAXPART;
5139 	}
5140 	sd_swap_efi_gpe(nparts, partitions);
5141 
5142 	mutex_enter(SD_MUTEX(un));
5143 
5144 	/* Fill in partition table. */
5145 	for (i = 0; i < nparts; i++) {
5146 		if (partitions->efi_gpe_StartingLBA != 0 ||
5147 		    partitions->efi_gpe_EndingLBA != 0) {
5148 			un->un_map[i].dkl_cylno =
5149 			    partitions->efi_gpe_StartingLBA;
5150 			un->un_map[i].dkl_nblk =
5151 			    partitions->efi_gpe_EndingLBA -
5152 			    partitions->efi_gpe_StartingLBA + 1;
5153 			un->un_offset[i] =
5154 			    partitions->efi_gpe_StartingLBA;
5155 		}
5156 		if (i == WD_NODE) {
5157 			/*
5158 			 * minor number 7 corresponds to the whole disk
5159 			 */
5160 			un->un_map[i].dkl_cylno = 0;
5161 			un->un_map[i].dkl_nblk = un->un_blockcount;
5162 			un->un_offset[i] = 0;
5163 		}
5164 		partitions++;
5165 	}
5166 	un->un_solaris_offset = 0;
5167 	un->un_solaris_size = cap;
5168 	un->un_f_geometry_is_valid = TRUE;
5169 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
5170 	return (0);
5171 
5172 done_err:
5173 	kmem_free(buf, EFI_MIN_ARRAY_SIZE);
5174 	mutex_enter(SD_MUTEX(un));
5175 	/*
5176 	 * if we didn't find something that could look like a VTOC
5177 	 * and the disk is over 1TB, we know there isn't a valid label.
5178 	 * Otherwise let sd_uselabel decide what to do.  We only
5179 	 * want to invalidate this if we're certain the label isn't
5180 	 * valid because sd_prop_op will now fail, which in turn
5181 	 * causes things like opens and stats on the partition to fail.
5182 	 */
5183 	if ((un->un_blockcount > DK_MAX_BLOCKS) && (rval != ESRCH)) {
5184 		un->un_f_geometry_is_valid = FALSE;
5185 	}
5186 	return (rval);
5187 }
5188 
5189 
5190 /*
5191  *    Function: sd_uselabel
5192  *
5193  * Description: Validate the disk label and update the relevant data (geometry,
5194  *		partition, vtoc, and capacity data) in the sd_lun struct.
5195  *		Marks the geometry of the unit as being valid.
5196  *
5197  *   Arguments: un: unit struct.
5198  *		dk_label: disk label
5199  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
5200  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
5201  *			to use the USCSI "direct" chain and bypass the normal
5202  *			command waitq.
5203  *
5204  * Return Code: SD_LABEL_IS_VALID: Label read from disk is OK; geometry,
5205  *		partition, vtoc, and capacity data are good.
5206  *
5207  *		SD_LABEL_IS_INVALID: Magic number or checksum error in the
5208  *		label; or computed capacity does not jibe with capacity
5209  *		reported from the READ CAPACITY command.
5210  *
5211  *     Context: Kernel thread only (can sleep).
5212  */
5213 
5214 static int
5215 sd_uselabel(struct sd_lun *un, struct dk_label *labp, int path_flag)
5216 {
5217 	short	*sp;
5218 	short	sum;
5219 	short	count;
5220 	int	label_error = SD_LABEL_IS_VALID;
5221 	int	i;
5222 	int	capacity;
5223 	int	part_end;
5224 	int	track_capacity;
5225 	int	err;
5226 #if defined(_SUNOS_VTOC_16)
5227 	struct	dkl_partition	*vpartp;
5228 #endif
5229 	ASSERT(un != NULL);
5230 	ASSERT(mutex_owned(SD_MUTEX(un)));
5231 
5232 	/* Validate the magic number of the label. */
5233 	if (labp->dkl_magic != DKL_MAGIC) {
5234 #if defined(__sparc)
5235 		if ((un->un_state == SD_STATE_NORMAL) &&
5236 		    !ISREMOVABLE(un)) {
5237 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5238 			    "Corrupt label; wrong magic number\n");
5239 		}
5240 #endif
5241 		return (SD_LABEL_IS_INVALID);
5242 	}
5243 
5244 	/* Validate the checksum of the label. */
5245 	sp  = (short *)labp;
5246 	sum = 0;
5247 	count = sizeof (struct dk_label) / sizeof (short);
5248 	while (count--)	 {
5249 		sum ^= *sp++;
5250 	}
5251 
5252 	if (sum != 0) {
5253 #if defined(_SUNOS_VTOC_16)
5254 		if (un->un_state == SD_STATE_NORMAL && !ISCD(un)) {
5255 #elif defined(_SUNOS_VTOC_8)
5256 		if (un->un_state == SD_STATE_NORMAL && !ISREMOVABLE(un)) {
5257 #endif
5258 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5259 			    "Corrupt label - label checksum failed\n");
5260 		}
5261 		return (SD_LABEL_IS_INVALID);
5262 	}
5263 
5264 
5265 	/*
5266 	 * Fill in geometry structure with data from label.
5267 	 */
5268 	bzero(&un->un_g, sizeof (struct dk_geom));
5269 	un->un_g.dkg_ncyl   = labp->dkl_ncyl;
5270 	un->un_g.dkg_acyl   = labp->dkl_acyl;
5271 	un->un_g.dkg_bcyl   = 0;
5272 	un->un_g.dkg_nhead  = labp->dkl_nhead;
5273 	un->un_g.dkg_nsect  = labp->dkl_nsect;
5274 	un->un_g.dkg_intrlv = labp->dkl_intrlv;
5275 
5276 #if defined(_SUNOS_VTOC_8)
5277 	un->un_g.dkg_gap1   = labp->dkl_gap1;
5278 	un->un_g.dkg_gap2   = labp->dkl_gap2;
5279 	un->un_g.dkg_bhead  = labp->dkl_bhead;
5280 #endif
5281 #if defined(_SUNOS_VTOC_16)
5282 	un->un_dkg_skew = labp->dkl_skew;
5283 #endif
5284 
5285 #if defined(__i386) || defined(__amd64)
5286 	un->un_g.dkg_apc = labp->dkl_apc;
5287 #endif
5288 
5289 	/*
5290 	 * Currently we rely on the values in the label being accurate. If
5291 	 * dlk_rpm or dlk_pcly are zero in the label, use a default value.
5292 	 *
5293 	 * Note: In the future a MODE SENSE may be used to retrieve this data,
5294 	 * although this command is optional in SCSI-2.
5295 	 */
5296 	un->un_g.dkg_rpm  = (labp->dkl_rpm  != 0) ? labp->dkl_rpm  : 3600;
5297 	un->un_g.dkg_pcyl = (labp->dkl_pcyl != 0) ? labp->dkl_pcyl :
5298 	    (un->un_g.dkg_ncyl + un->un_g.dkg_acyl);
5299 
5300 	/*
5301 	 * The Read and Write reinstruct values may not be valid
5302 	 * for older disks.
5303 	 */
5304 	un->un_g.dkg_read_reinstruct  = labp->dkl_read_reinstruct;
5305 	un->un_g.dkg_write_reinstruct = labp->dkl_write_reinstruct;
5306 
5307 	/* Fill in partition table. */
5308 #if defined(_SUNOS_VTOC_8)
5309 	for (i = 0; i < NDKMAP; i++) {
5310 		un->un_map[i].dkl_cylno = labp->dkl_map[i].dkl_cylno;
5311 		un->un_map[i].dkl_nblk  = labp->dkl_map[i].dkl_nblk;
5312 	}
5313 #endif
5314 #if  defined(_SUNOS_VTOC_16)
5315 	vpartp		= labp->dkl_vtoc.v_part;
5316 	track_capacity	= labp->dkl_nhead * labp->dkl_nsect;
5317 
5318 	for (i = 0; i < NDKMAP; i++, vpartp++) {
5319 		un->un_map[i].dkl_cylno = vpartp->p_start / track_capacity;
5320 		un->un_map[i].dkl_nblk  = vpartp->p_size;
5321 	}
5322 #endif
5323 
5324 	/* Fill in VTOC Structure. */
5325 	bcopy(&labp->dkl_vtoc, &un->un_vtoc, sizeof (struct dk_vtoc));
5326 #if defined(_SUNOS_VTOC_8)
5327 	/*
5328 	 * The 8-slice vtoc does not include the ascii label; save it into
5329 	 * the device's soft state structure here.
5330 	 */
5331 	bcopy(labp->dkl_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
5332 #endif
5333 
5334 	/* Mark the geometry as valid. */
5335 	un->un_f_geometry_is_valid = TRUE;
5336 
5337 	/* Now look for a valid capacity. */
5338 	track_capacity	= (un->un_g.dkg_nhead * un->un_g.dkg_nsect);
5339 	capacity	= (un->un_g.dkg_ncyl  * track_capacity);
5340 
5341 	if (un->un_g.dkg_acyl) {
5342 #if defined(__i386) || defined(__amd64)
5343 		/* we may have > 1 alts cylinder */
5344 		capacity += (track_capacity * un->un_g.dkg_acyl);
5345 #else
5346 		capacity += track_capacity;
5347 #endif
5348 	}
5349 
5350 	/*
5351 	 * At this point, un->un_blockcount should contain valid data from
5352 	 * the READ CAPACITY command.
5353 	 */
5354 	if (un->un_f_blockcount_is_valid != TRUE) {
5355 		/*
5356 		 * We have a situation where the target didn't give us a good
5357 		 * READ CAPACITY value, yet there appears to be a valid label.
5358 		 * In this case, we'll fake the capacity.
5359 		 */
5360 		un->un_blockcount = capacity;
5361 		un->un_f_blockcount_is_valid = TRUE;
5362 		goto done;
5363 	}
5364 
5365 
5366 	if ((capacity <= un->un_blockcount) ||
5367 	    (un->un_state != SD_STATE_NORMAL)) {
5368 #if defined(_SUNOS_VTOC_8)
5369 		/*
5370 		 * We can't let this happen on drives that are subdivided
5371 		 * into logical disks (i.e., that have an fdisk table).
5372 		 * The un_blockcount field should always hold the full media
5373 		 * size in sectors, period.  This code would overwrite
5374 		 * un_blockcount with the size of the Solaris fdisk partition.
5375 		 */
5376 		SD_ERROR(SD_LOG_COMMON, un,
5377 		    "sd_uselabel: Label %d blocks; Drive %d blocks\n",
5378 		    capacity, un->un_blockcount);
5379 		un->un_blockcount = capacity;
5380 		un->un_f_blockcount_is_valid = TRUE;
5381 #endif	/* defined(_SUNOS_VTOC_8) */
5382 		goto done;
5383 	}
5384 
5385 	if (ISCD(un)) {
5386 		/* For CDROMs, we trust that the data in the label is OK. */
5387 #if defined(_SUNOS_VTOC_8)
5388 		for (i = 0; i < NDKMAP; i++) {
5389 			part_end = labp->dkl_nhead * labp->dkl_nsect *
5390 			    labp->dkl_map[i].dkl_cylno +
5391 			    labp->dkl_map[i].dkl_nblk  - 1;
5392 
5393 			if ((labp->dkl_map[i].dkl_nblk) &&
5394 			    (part_end > un->un_blockcount)) {
5395 				un->un_f_geometry_is_valid = FALSE;
5396 				break;
5397 			}
5398 		}
5399 #endif
5400 #if defined(_SUNOS_VTOC_16)
5401 		vpartp = &(labp->dkl_vtoc.v_part[0]);
5402 		for (i = 0; i < NDKMAP; i++, vpartp++) {
5403 			part_end = vpartp->p_start + vpartp->p_size;
5404 			if ((vpartp->p_size > 0) &&
5405 			    (part_end > un->un_blockcount)) {
5406 				un->un_f_geometry_is_valid = FALSE;
5407 				break;
5408 			}
5409 		}
5410 #endif
5411 	} else {
5412 		uint64_t t_capacity;
5413 		uint32_t t_lbasize;
5414 
5415 		mutex_exit(SD_MUTEX(un));
5416 		err = sd_send_scsi_READ_CAPACITY(un, &t_capacity, &t_lbasize,
5417 		    path_flag);
5418 		ASSERT(t_capacity <= DK_MAX_BLOCKS);
5419 		mutex_enter(SD_MUTEX(un));
5420 
5421 		if (err == 0) {
5422 			sd_update_block_info(un, t_lbasize, t_capacity);
5423 		}
5424 
5425 		if (capacity > un->un_blockcount) {
5426 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
5427 			    "Corrupt label - bad geometry\n");
5428 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
5429 			    "Label says %u blocks; Drive says %llu blocks\n",
5430 			    capacity, (unsigned long long)un->un_blockcount);
5431 			un->un_f_geometry_is_valid = FALSE;
5432 			label_error = SD_LABEL_IS_INVALID;
5433 		}
5434 	}
5435 
5436 done:
5437 
5438 	SD_INFO(SD_LOG_COMMON, un, "sd_uselabel: (label geometry)\n");
5439 	SD_INFO(SD_LOG_COMMON, un,
5440 	    "   ncyl: %d; acyl: %d; nhead: %d; nsect: %d\n",
5441 	    un->un_g.dkg_ncyl,  un->un_g.dkg_acyl,
5442 	    un->un_g.dkg_nhead, un->un_g.dkg_nsect);
5443 	SD_INFO(SD_LOG_COMMON, un,
5444 	    "   lbasize: %d; capacity: %d; intrlv: %d; rpm: %d\n",
5445 	    un->un_tgt_blocksize, un->un_blockcount,
5446 	    un->un_g.dkg_intrlv, un->un_g.dkg_rpm);
5447 	SD_INFO(SD_LOG_COMMON, un, "   wrt_reinstr: %d; rd_reinstr: %d\n",
5448 	    un->un_g.dkg_write_reinstruct, un->un_g.dkg_read_reinstruct);
5449 
5450 	ASSERT(mutex_owned(SD_MUTEX(un)));
5451 
5452 	return (label_error);
5453 }
5454 
5455 
5456 /*
5457  *    Function: sd_build_default_label
5458  *
5459  * Description: Generate a default label for those devices that do not have
5460  *		one, e.g., new media, removable cartridges, etc..
5461  *
5462  *     Context: Kernel thread only
5463  */
5464 
5465 static void
5466 sd_build_default_label(struct sd_lun *un)
5467 {
5468 #if defined(_SUNOS_VTOC_16)
5469 	uint_t	phys_spc;
5470 	uint_t	disksize;
5471 	struct	dk_geom un_g;
5472 #endif
5473 
5474 	ASSERT(un != NULL);
5475 	ASSERT(mutex_owned(SD_MUTEX(un)));
5476 
5477 #if defined(_SUNOS_VTOC_8)
5478 	/*
5479 	 * Note: This is a legacy check for non-removable devices on VTOC_8
5480 	 * only. This may be a valid check for VTOC_16 as well.
5481 	 */
5482 	if (!ISREMOVABLE(un)) {
5483 		return;
5484 	}
5485 #endif
5486 
5487 	bzero(&un->un_g, sizeof (struct dk_geom));
5488 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
5489 	bzero(&un->un_map, NDKMAP * (sizeof (struct dk_map)));
5490 
5491 #if defined(_SUNOS_VTOC_8)
5492 
5493 	/*
5494 	 * It's a REMOVABLE media, therefore no label (on sparc, anyway).
5495 	 * But it is still necessary to set up various geometry information,
5496 	 * and we are doing this here.
5497 	 */
5498 
5499 	/*
5500 	 * For the rpm, we use the minimum for the disk.  For the head, cyl,
5501 	 * and number of sector per track, if the capacity <= 1GB, head = 64,
5502 	 * sect = 32.  else head = 255, sect 63 Note: the capacity should be
5503 	 * equal to C*H*S values.  This will cause some truncation of size due
5504 	 * to round off errors. For CD-ROMs, this truncation can have adverse
5505 	 * side effects, so returning ncyl and nhead as 1. The nsect will
5506 	 * overflow for most of CD-ROMs as nsect is of type ushort. (4190569)
5507 	 */
5508 	if (ISCD(un)) {
5509 		/*
5510 		 * Preserve the old behavior for non-writable
5511 		 * medias. Since dkg_nsect is a ushort, it
5512 		 * will lose bits as cdroms have more than
5513 		 * 65536 sectors. So if we recalculate
5514 		 * capacity, it will become much shorter.
5515 		 * But the dkg_* information is not
5516 		 * used for CDROMs so it is OK. But for
5517 		 * Writable CDs we need this information
5518 		 * to be valid (for newfs say). So we
5519 		 * make nsect and nhead > 1 that way
5520 		 * nsect can still stay within ushort limit
5521 		 * without losing any bits.
5522 		 */
5523 		if (un->un_f_mmc_writable_media == TRUE) {
5524 			un->un_g.dkg_nhead = 64;
5525 			un->un_g.dkg_nsect = 32;
5526 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
5527 			un->un_blockcount = un->un_g.dkg_ncyl *
5528 			    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
5529 		} else {
5530 			un->un_g.dkg_ncyl  = 1;
5531 			un->un_g.dkg_nhead = 1;
5532 			un->un_g.dkg_nsect = un->un_blockcount;
5533 		}
5534 	} else {
5535 		if (un->un_blockcount <= 0x1000) {
5536 			/* unlabeled SCSI floppy device */
5537 			un->un_g.dkg_nhead = 2;
5538 			un->un_g.dkg_ncyl = 80;
5539 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
5540 		} else if (un->un_blockcount <= 0x200000) {
5541 			un->un_g.dkg_nhead = 64;
5542 			un->un_g.dkg_nsect = 32;
5543 			un->un_g.dkg_ncyl  = un->un_blockcount / (64 * 32);
5544 		} else {
5545 			un->un_g.dkg_nhead = 255;
5546 			un->un_g.dkg_nsect = 63;
5547 			un->un_g.dkg_ncyl  = un->un_blockcount / (255 * 63);
5548 		}
5549 		un->un_blockcount =
5550 		    un->un_g.dkg_ncyl * un->un_g.dkg_nhead * un->un_g.dkg_nsect;
5551 	}
5552 
5553 	un->un_g.dkg_acyl	= 0;
5554 	un->un_g.dkg_bcyl	= 0;
5555 	un->un_g.dkg_rpm	= 200;
5556 	un->un_asciilabel[0]	= '\0';
5557 	un->un_g.dkg_pcyl	= un->un_g.dkg_ncyl;
5558 
5559 	un->un_map[0].dkl_cylno = 0;
5560 	un->un_map[0].dkl_nblk  = un->un_blockcount;
5561 	un->un_map[2].dkl_cylno = 0;
5562 	un->un_map[2].dkl_nblk  = un->un_blockcount;
5563 
5564 #elif defined(_SUNOS_VTOC_16)
5565 
5566 	if (un->un_solaris_size == 0) {
5567 		/*
5568 		 * Got fdisk table but no solaris entry therefore
5569 		 * don't create a default label
5570 		 */
5571 		un->un_f_geometry_is_valid = TRUE;
5572 		return;
5573 	}
5574 
5575 	/*
5576 	 * For CDs we continue to use the physical geometry to calculate
5577 	 * number of cylinders. All other devices must convert the
5578 	 * physical geometry (geom_cache) to values that will fit
5579 	 * in a dk_geom structure.
5580 	 */
5581 	if (ISCD(un)) {
5582 		phys_spc = un->un_pgeom.g_nhead * un->un_pgeom.g_nsect;
5583 	} else {
5584 		/* Convert physical geometry to disk geometry */
5585 		bzero(&un_g, sizeof (struct dk_geom));
5586 		sd_convert_geometry(un->un_blockcount, &un_g);
5587 		bcopy(&un_g, &un->un_g, sizeof (un->un_g));
5588 		phys_spc = un->un_g.dkg_nhead * un->un_g.dkg_nsect;
5589 	}
5590 
5591 	un->un_g.dkg_pcyl = un->un_solaris_size / phys_spc;
5592 	un->un_g.dkg_acyl = DK_ACYL;
5593 	un->un_g.dkg_ncyl = un->un_g.dkg_pcyl - DK_ACYL;
5594 	disksize = un->un_g.dkg_ncyl * phys_spc;
5595 
5596 	if (ISCD(un)) {
5597 		/*
5598 		 * CD's don't use the "heads * sectors * cyls"-type of
5599 		 * geometry, but instead use the entire capacity of the media.
5600 		 */
5601 		disksize = un->un_solaris_size;
5602 		un->un_g.dkg_nhead = 1;
5603 		un->un_g.dkg_nsect = 1;
5604 		un->un_g.dkg_rpm =
5605 		    (un->un_pgeom.g_rpm == 0) ? 200 : un->un_pgeom.g_rpm;
5606 
5607 		un->un_vtoc.v_part[0].p_start = 0;
5608 		un->un_vtoc.v_part[0].p_size  = disksize;
5609 		un->un_vtoc.v_part[0].p_tag   = V_BACKUP;
5610 		un->un_vtoc.v_part[0].p_flag  = V_UNMNT;
5611 
5612 		un->un_map[0].dkl_cylno = 0;
5613 		un->un_map[0].dkl_nblk  = disksize;
5614 		un->un_offset[0] = 0;
5615 
5616 	} else {
5617 		/*
5618 		 * Hard disks and removable media cartridges
5619 		 */
5620 		un->un_g.dkg_rpm =
5621 		    (un->un_pgeom.g_rpm == 0) ? 3600: un->un_pgeom.g_rpm;
5622 		un->un_vtoc.v_sectorsz = un->un_sys_blocksize;
5623 
5624 		/* Add boot slice */
5625 		un->un_vtoc.v_part[8].p_start = 0;
5626 		un->un_vtoc.v_part[8].p_size  = phys_spc;
5627 		un->un_vtoc.v_part[8].p_tag   = V_BOOT;
5628 		un->un_vtoc.v_part[8].p_flag  = V_UNMNT;
5629 
5630 		un->un_map[8].dkl_cylno = 0;
5631 		un->un_map[8].dkl_nblk  = phys_spc;
5632 		un->un_offset[8] = 0;
5633 	}
5634 
5635 	un->un_g.dkg_apc = 0;
5636 	un->un_vtoc.v_nparts = V_NUMPAR;
5637 	un->un_vtoc.v_version = V_VERSION;
5638 
5639 	/* Add backup slice */
5640 	un->un_vtoc.v_part[2].p_start = 0;
5641 	un->un_vtoc.v_part[2].p_size  = disksize;
5642 	un->un_vtoc.v_part[2].p_tag   = V_BACKUP;
5643 	un->un_vtoc.v_part[2].p_flag  = V_UNMNT;
5644 
5645 	un->un_map[2].dkl_cylno = 0;
5646 	un->un_map[2].dkl_nblk  = disksize;
5647 	un->un_offset[2] = 0;
5648 
5649 	(void) sprintf(un->un_vtoc.v_asciilabel, "DEFAULT cyl %d alt %d"
5650 	    " hd %d sec %d", un->un_g.dkg_ncyl, un->un_g.dkg_acyl,
5651 	    un->un_g.dkg_nhead, un->un_g.dkg_nsect);
5652 
5653 #else
5654 #error "No VTOC format defined."
5655 #endif
5656 
5657 	un->un_g.dkg_read_reinstruct  = 0;
5658 	un->un_g.dkg_write_reinstruct = 0;
5659 
5660 	un->un_g.dkg_intrlv = 1;
5661 
5662 	un->un_vtoc.v_sanity  = VTOC_SANE;
5663 
5664 	un->un_f_geometry_is_valid = TRUE;
5665 
5666 	SD_INFO(SD_LOG_COMMON, un,
5667 	    "sd_build_default_label: Default label created: "
5668 	    "cyl: %d\tacyl: %d\tnhead: %d\tnsect: %d\tcap: %d\n",
5669 	    un->un_g.dkg_ncyl, un->un_g.dkg_acyl, un->un_g.dkg_nhead,
5670 	    un->un_g.dkg_nsect, un->un_blockcount);
5671 }
5672 
5673 
5674 #if defined(_FIRMWARE_NEEDS_FDISK)
5675 /*
5676  * Max CHS values, as they are encoded into bytes, for 1022/254/63
5677  */
5678 #define	LBA_MAX_SECT	(63 | ((1022 & 0x300) >> 2))
5679 #define	LBA_MAX_CYL	(1022 & 0xFF)
5680 #define	LBA_MAX_HEAD	(254)
5681 
5682 
5683 /*
5684  *    Function: sd_has_max_chs_vals
5685  *
5686  * Description: Return TRUE if Cylinder-Head-Sector values are all at maximum.
5687  *
5688  *   Arguments: fdp - ptr to CHS info
5689  *
5690  * Return Code: True or false
5691  *
5692  *     Context: Any.
5693  */
5694 
5695 static int
5696 sd_has_max_chs_vals(struct ipart *fdp)
5697 {
5698 	return ((fdp->begcyl  == LBA_MAX_CYL)	&&
5699 	    (fdp->beghead == LBA_MAX_HEAD)	&&
5700 	    (fdp->begsect == LBA_MAX_SECT)	&&
5701 	    (fdp->endcyl  == LBA_MAX_CYL)	&&
5702 	    (fdp->endhead == LBA_MAX_HEAD)	&&
5703 	    (fdp->endsect == LBA_MAX_SECT));
5704 }
5705 #endif
5706 
5707 
5708 /*
5709  *    Function: sd_inq_fill
5710  *
5711  * Description: Print a piece of inquiry data, cleaned up for non-printable
5712  *		characters and stopping at the first space character after
5713  *		the beginning of the passed string;
5714  *
5715  *   Arguments: p - source string
5716  *		l - maximum length to copy
5717  *		s - destination string
5718  *
5719  *     Context: Any.
5720  */
5721 
5722 static void
5723 sd_inq_fill(char *p, int l, char *s)
5724 {
5725 	unsigned i = 0;
5726 	char c;
5727 
5728 	while (i++ < l) {
5729 		if ((c = *p++) < ' ' || c >= 0x7F) {
5730 			c = '*';
5731 		} else if (i != 1 && c == ' ') {
5732 			break;
5733 		}
5734 		*s++ = c;
5735 	}
5736 	*s++ = 0;
5737 }
5738 
5739 
5740 /*
5741  *    Function: sd_register_devid
5742  *
5743  * Description: This routine will obtain the device id information from the
5744  *		target, obtain the serial number, and register the device
5745  *		id with the ddi framework.
5746  *
5747  *   Arguments: devi - the system's dev_info_t for the device.
5748  *		un - driver soft state (unit) structure
5749  *		reservation_flag - indicates if a reservation conflict
5750  *		occurred during attach
5751  *
5752  *     Context: Kernel Thread
5753  */
5754 static void
5755 sd_register_devid(struct sd_lun *un, dev_info_t *devi, int reservation_flag)
5756 {
5757 	int		rval		= 0;
5758 	uchar_t		*inq80		= NULL;
5759 	size_t		inq80_len	= MAX_INQUIRY_SIZE;
5760 	size_t		inq80_resid	= 0;
5761 	uchar_t		*inq83		= NULL;
5762 	size_t		inq83_len	= MAX_INQUIRY_SIZE;
5763 	size_t		inq83_resid	= 0;
5764 
5765 	ASSERT(un != NULL);
5766 	ASSERT(mutex_owned(SD_MUTEX(un)));
5767 	ASSERT((SD_DEVINFO(un)) == devi);
5768 
5769 	/*
5770 	 * This is the case of antiquated Sun disk drives that have the
5771 	 * FAB_DEVID property set in the disk_table.  These drives
5772 	 * manage the devid's by storing them in last 2 available sectors
5773 	 * on the drive and have them fabricated by the ddi layer by calling
5774 	 * ddi_devid_init and passing the DEVID_FAB flag.
5775 	 */
5776 	if (un->un_f_opt_fab_devid == TRUE) {
5777 		/*
5778 		 * Depending on EINVAL isn't reliable, since a reserved disk
5779 		 * may result in invalid geometry, so check to make sure a
5780 		 * reservation conflict did not occur during attach.
5781 		 */
5782 		if ((sd_get_devid(un) == EINVAL) &&
5783 		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
5784 			/*
5785 			 * The devid is invalid AND there is no reservation
5786 			 * conflict.  Fabricate a new devid.
5787 			 */
5788 			(void) sd_create_devid(un);
5789 		}
5790 
5791 		/* Register the devid if it exists */
5792 		if (un->un_devid != NULL) {
5793 			(void) ddi_devid_register(SD_DEVINFO(un),
5794 			    un->un_devid);
5795 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5796 			    "sd_register_devid: Devid Fabricated\n");
5797 		}
5798 		return;
5799 	}
5800 
5801 	/*
5802 	 * We check the availibility of the World Wide Name (0x83) and Unit
5803 	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5804 	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5805 	 * 0x83 is availible, that is the best choice.  Our next choice is
5806 	 * 0x80.  If neither are availible, we munge the devid from the device
5807 	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5808 	 * to fabricate a devid for non-Sun qualified disks.
5809 	 */
5810 	if (sd_check_vpd_page_support(un) == 0) {
5811 		/* collect page 80 data if available */
5812 		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5813 
5814 			mutex_exit(SD_MUTEX(un));
5815 			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5816 			rval = sd_send_scsi_INQUIRY(un, inq80, inq80_len,
5817 			    0x01, 0x80, &inq80_resid);
5818 
5819 			if (rval != 0) {
5820 				kmem_free(inq80, inq80_len);
5821 				inq80 = NULL;
5822 				inq80_len = 0;
5823 			}
5824 			mutex_enter(SD_MUTEX(un));
5825 		}
5826 
5827 		/* collect page 83 data if available */
5828 		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5829 
5830 			mutex_exit(SD_MUTEX(un));
5831 			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5832 			rval = sd_send_scsi_INQUIRY(un, inq83, inq83_len,
5833 			    0x01, 0x83, &inq83_resid);
5834 
5835 			if (rval != 0) {
5836 				kmem_free(inq83, inq83_len);
5837 				inq83 = NULL;
5838 				inq83_len = 0;
5839 			}
5840 			mutex_enter(SD_MUTEX(un));
5841 		}
5842 	}
5843 
5844 	/* encode best devid possible based on data available */
5845 	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5846 	    (char *)ddi_driver_name(SD_DEVINFO(un)),
5847 	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5848 	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
5849 	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5850 
5851 		/* devid successfully encoded, register devid */
5852 		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5853 
5854 	} else {
5855 		/*
5856 		 * Unable to encode a devid based on data available.
5857 		 * This is not a Sun qualified disk.  Older Sun disk
5858 		 * drives that have the SD_FAB_DEVID property
5859 		 * set in the disk_table and non Sun qualified
5860 		 * disks are treated in the same manner.  These
5861 		 * drives manage the devid's by storing them in
5862 		 * last 2 available sectors on the drive and
5863 		 * have them fabricated by the ddi layer by
5864 		 * calling ddi_devid_init and passing the
5865 		 * DEVID_FAB flag.
5866 		 * Create a fabricate devid only if there's no
5867 		 * fabricate devid existed.
5868 		 */
5869 		if (sd_get_devid(un) == EINVAL) {
5870 			(void) sd_create_devid(un);
5871 			un->un_f_opt_fab_devid = TRUE;
5872 		}
5873 
5874 		/* Register the devid if it exists */
5875 		if (un->un_devid != NULL) {
5876 			(void) ddi_devid_register(SD_DEVINFO(un),
5877 			    un->un_devid);
5878 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5879 			    "sd_register_devid: devid fabricated using "
5880 			    "ddi framework\n");
5881 		}
5882 	}
5883 
5884 	/* clean up resources */
5885 	if (inq80 != NULL) {
5886 		kmem_free(inq80, inq80_len);
5887 	}
5888 	if (inq83 != NULL) {
5889 		kmem_free(inq83, inq83_len);
5890 	}
5891 }
5892 
5893 static daddr_t
5894 sd_get_devid_block(struct sd_lun *un)
5895 {
5896 	daddr_t			spc, blk, head, cyl;
5897 
5898 	if (un->un_blockcount <= DK_MAX_BLOCKS) {
5899 		/* this geometry doesn't allow us to write a devid */
5900 		if (un->un_g.dkg_acyl < 2) {
5901 			return (-1);
5902 		}
5903 
5904 		/*
5905 		 * Subtract 2 guarantees that the next to last cylinder
5906 		 * is used
5907 		 */
5908 		cyl  = un->un_g.dkg_ncyl  + un->un_g.dkg_acyl - 2;
5909 		spc  = un->un_g.dkg_nhead * un->un_g.dkg_nsect;
5910 		head = un->un_g.dkg_nhead - 1;
5911 		blk  = (cyl * (spc - un->un_g.dkg_apc)) +
5912 		    (head * un->un_g.dkg_nsect) + 1;
5913 	} else {
5914 		if (un->un_reserved != -1) {
5915 			blk = un->un_map[un->un_reserved].dkl_cylno + 1;
5916 		} else {
5917 			return (-1);
5918 		}
5919 	}
5920 	return (blk);
5921 }
5922 
5923 /*
5924  *    Function: sd_get_devid
5925  *
5926  * Description: This routine will return 0 if a valid device id has been
5927  *		obtained from the target and stored in the soft state. If a
5928  *		valid device id has not been previously read and stored, a
5929  *		read attempt will be made.
5930  *
5931  *   Arguments: un - driver soft state (unit) structure
5932  *
5933  * Return Code: 0 if we successfully get the device id
5934  *
5935  *     Context: Kernel Thread
5936  */
5937 
5938 static int
5939 sd_get_devid(struct sd_lun *un)
5940 {
5941 	struct dk_devid		*dkdevid;
5942 	ddi_devid_t		tmpid;
5943 	uint_t			*ip;
5944 	size_t			sz;
5945 	daddr_t			blk;
5946 	int			status;
5947 	int			chksum;
5948 	int			i;
5949 	size_t			buffer_size;
5950 
5951 	ASSERT(un != NULL);
5952 	ASSERT(mutex_owned(SD_MUTEX(un)));
5953 
5954 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5955 	    un);
5956 
5957 	if (un->un_devid != NULL) {
5958 		return (0);
5959 	}
5960 
5961 	blk = sd_get_devid_block(un);
5962 	if (blk < 0)
5963 		return (EINVAL);
5964 
5965 	/*
5966 	 * Read and verify device id, stored in the reserved cylinders at the
5967 	 * end of the disk. Backup label is on the odd sectors of the last
5968 	 * track of the last cylinder. Device id will be on track of the next
5969 	 * to last cylinder.
5970 	 */
5971 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5972 	mutex_exit(SD_MUTEX(un));
5973 	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5974 	status = sd_send_scsi_READ(un, dkdevid, buffer_size, blk,
5975 	    SD_PATH_DIRECT);
5976 	if (status != 0) {
5977 		goto error;
5978 	}
5979 
5980 	/* Validate the revision */
5981 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5982 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5983 		status = EINVAL;
5984 		goto error;
5985 	}
5986 
5987 	/* Calculate the checksum */
5988 	chksum = 0;
5989 	ip = (uint_t *)dkdevid;
5990 	for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int));
5991 	    i++) {
5992 		chksum ^= ip[i];
5993 	}
5994 
5995 	/* Compare the checksums */
5996 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
5997 		status = EINVAL;
5998 		goto error;
5999 	}
6000 
6001 	/* Validate the device id */
6002 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
6003 		status = EINVAL;
6004 		goto error;
6005 	}
6006 
6007 	/*
6008 	 * Store the device id in the driver soft state
6009 	 */
6010 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
6011 	tmpid = kmem_alloc(sz, KM_SLEEP);
6012 
6013 	mutex_enter(SD_MUTEX(un));
6014 
6015 	un->un_devid = tmpid;
6016 	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
6017 
6018 	kmem_free(dkdevid, buffer_size);
6019 
6020 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
6021 
6022 	return (status);
6023 error:
6024 	mutex_enter(SD_MUTEX(un));
6025 	kmem_free(dkdevid, buffer_size);
6026 	return (status);
6027 }
6028 
6029 
6030 /*
6031  *    Function: sd_create_devid
6032  *
6033  * Description: This routine will fabricate the device id and write it
6034  *		to the disk.
6035  *
6036  *   Arguments: un - driver soft state (unit) structure
6037  *
6038  * Return Code: value of the fabricated device id
6039  *
6040  *     Context: Kernel Thread
6041  */
6042 
6043 static ddi_devid_t
6044 sd_create_devid(struct sd_lun *un)
6045 {
6046 	ASSERT(un != NULL);
6047 
6048 	/* Fabricate the devid */
6049 	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
6050 	    == DDI_FAILURE) {
6051 		return (NULL);
6052 	}
6053 
6054 	/* Write the devid to disk */
6055 	if (sd_write_deviceid(un) != 0) {
6056 		ddi_devid_free(un->un_devid);
6057 		un->un_devid = NULL;
6058 	}
6059 
6060 	return (un->un_devid);
6061 }
6062 
6063 
6064 /*
6065  *    Function: sd_write_deviceid
6066  *
6067  * Description: This routine will write the device id to the disk
6068  *		reserved sector.
6069  *
6070  *   Arguments: un - driver soft state (unit) structure
6071  *
6072  * Return Code: EINVAL
6073  *		value returned by sd_send_scsi_cmd
6074  *
6075  *     Context: Kernel Thread
6076  */
6077 
6078 static int
6079 sd_write_deviceid(struct sd_lun *un)
6080 {
6081 	struct dk_devid		*dkdevid;
6082 	daddr_t			blk;
6083 	uint_t			*ip, chksum;
6084 	int			status;
6085 	int			i;
6086 
6087 	ASSERT(mutex_owned(SD_MUTEX(un)));
6088 
6089 	blk = sd_get_devid_block(un);
6090 	if (blk < 0)
6091 		return (-1);
6092 	mutex_exit(SD_MUTEX(un));
6093 
6094 	/* Allocate the buffer */
6095 	dkdevid = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
6096 
6097 	/* Fill in the revision */
6098 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
6099 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
6100 
6101 	/* Copy in the device id */
6102 	mutex_enter(SD_MUTEX(un));
6103 	bcopy(un->un_devid, &dkdevid->dkd_devid,
6104 	    ddi_devid_sizeof(un->un_devid));
6105 	mutex_exit(SD_MUTEX(un));
6106 
6107 	/* Calculate the checksum */
6108 	chksum = 0;
6109 	ip = (uint_t *)dkdevid;
6110 	for (i = 0; i < ((un->un_sys_blocksize - sizeof (int))/sizeof (int));
6111 	    i++) {
6112 		chksum ^= ip[i];
6113 	}
6114 
6115 	/* Fill-in checksum */
6116 	DKD_FORMCHKSUM(chksum, dkdevid);
6117 
6118 	/* Write the reserved sector */
6119 	status = sd_send_scsi_WRITE(un, dkdevid, un->un_sys_blocksize, blk,
6120 	    SD_PATH_DIRECT);
6121 
6122 	kmem_free(dkdevid, un->un_sys_blocksize);
6123 
6124 	mutex_enter(SD_MUTEX(un));
6125 	return (status);
6126 }
6127 
6128 
6129 /*
6130  *    Function: sd_check_vpd_page_support
6131  *
6132  * Description: This routine sends an inquiry command with the EVPD bit set and
6133  *		a page code of 0x00 to the device. It is used to determine which
6134  *		vital product pages are availible to find the devid. We are
6135  *		looking for pages 0x83 or 0x80.  If we return a negative 1, the
6136  *		device does not support that command.
6137  *
6138  *   Arguments: un  - driver soft state (unit) structure
6139  *
6140  * Return Code: 0 - success
6141  *		1 - check condition
6142  *
6143  *     Context: This routine can sleep.
6144  */
6145 
6146 static int
6147 sd_check_vpd_page_support(struct sd_lun *un)
6148 {
6149 	uchar_t	*page_list	= NULL;
6150 	uchar_t	page_length	= 0xff;	/* Use max possible length */
6151 	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
6152 	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
6153 	int    	rval		= 0;
6154 	int	counter;
6155 
6156 	ASSERT(un != NULL);
6157 	ASSERT(mutex_owned(SD_MUTEX(un)));
6158 
6159 	mutex_exit(SD_MUTEX(un));
6160 
6161 	/*
6162 	 * We'll set the page length to the maximum to save figuring it out
6163 	 * with an additional call.
6164 	 */
6165 	page_list =  kmem_zalloc(page_length, KM_SLEEP);
6166 
6167 	rval = sd_send_scsi_INQUIRY(un, page_list, page_length, evpd,
6168 	    page_code, NULL);
6169 
6170 	mutex_enter(SD_MUTEX(un));
6171 
6172 	/*
6173 	 * Now we must validate that the device accepted the command, as some
6174 	 * drives do not support it.  If the drive does support it, we will
6175 	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
6176 	 * not, we return -1.
6177 	 */
6178 	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
6179 		/* Loop to find one of the 2 pages we need */
6180 		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
6181 
6182 		/*
6183 		 * Pages are returned in ascending order, and 0x83 is what we
6184 		 * are hoping for.
6185 		 */
6186 		while ((page_list[counter] <= 0x83) &&
6187 		    (counter <= (page_list[VPD_PAGE_LENGTH] +
6188 		    VPD_HEAD_OFFSET))) {
6189 			/*
6190 			 * Add 3 because page_list[3] is the number of
6191 			 * pages minus 3
6192 			 */
6193 
6194 			switch (page_list[counter]) {
6195 			case 0x00:
6196 				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
6197 				break;
6198 			case 0x80:
6199 				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
6200 				break;
6201 			case 0x81:
6202 				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
6203 				break;
6204 			case 0x82:
6205 				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
6206 				break;
6207 			case 0x83:
6208 				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
6209 				break;
6210 			}
6211 			counter++;
6212 		}
6213 
6214 	} else {
6215 		rval = -1;
6216 
6217 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
6218 		    "sd_check_vpd_page_support: This drive does not implement "
6219 		    "VPD pages.\n");
6220 	}
6221 
6222 	kmem_free(page_list, page_length);
6223 
6224 	return (rval);
6225 }
6226 
6227 
6228 /*
6229  *    Function: sd_setup_pm
6230  *
6231  * Description: Initialize Power Management on the device
6232  *
6233  *     Context: Kernel Thread
6234  */
6235 
6236 static void
6237 sd_setup_pm(struct sd_lun *un, dev_info_t *devi)
6238 {
6239 	uint_t	log_page_size;
6240 	uchar_t	*log_page_data;
6241 	int	rval;
6242 
6243 	/*
6244 	 * Since we are called from attach, holding a mutex for
6245 	 * un is unnecessary. Because some of the routines called
6246 	 * from here require SD_MUTEX to not be held, assert this
6247 	 * right up front.
6248 	 */
6249 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6250 	/*
6251 	 * Since the sd device does not have the 'reg' property,
6252 	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
6253 	 * The following code is to tell cpr that this device
6254 	 * DOES need to be suspended and resumed.
6255 	 */
6256 	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
6257 	    "pm-hardware-state", "needs-suspend-resume");
6258 
6259 	/*
6260 	 * Check if HBA has set the "pm-capable" property.
6261 	 * If "pm-capable" exists and is non-zero then we can
6262 	 * power manage the device without checking the start/stop
6263 	 * cycle count log sense page.
6264 	 *
6265 	 * If "pm-capable" exists and is SD_PM_CAPABLE_FALSE (0)
6266 	 * then we should not power manage the device.
6267 	 *
6268 	 * If "pm-capable" doesn't exist then un->un_pm_capable_prop will
6269 	 * be set to SD_PM_CAPABLE_UNDEFINED (-1).  In this case, sd will
6270 	 * check the start/stop cycle count log sense page and power manage
6271 	 * the device if the cycle count limit has not been exceeded.
6272 	 */
6273 	un->un_pm_capable_prop =
6274 	    ddi_prop_get_int(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
6275 		"pm-capable", SD_PM_CAPABLE_UNDEFINED);
6276 	if (un->un_pm_capable_prop != SD_PM_CAPABLE_UNDEFINED) {
6277 		/*
6278 		 * pm-capable property exists.
6279 		 *
6280 		 * Convert "TRUE" values for un_pm_capable_prop to
6281 		 * SD_PM_CAPABLE_TRUE (1) to make it easier to check later.
6282 		 * "TRUE" values are any values except SD_PM_CAPABLE_FALSE (0)
6283 		 *  and SD_PM_CAPABLE_UNDEFINED (-1)
6284 		 */
6285 		if (un->un_pm_capable_prop != SD_PM_CAPABLE_FALSE) {
6286 			un->un_pm_capable_prop = SD_PM_CAPABLE_TRUE;
6287 		}
6288 
6289 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
6290 		    "sd_unit_attach: un:0x%p pm-capable "
6291 		    "property set to %d.\n", un, un->un_pm_capable_prop);
6292 	}
6293 
6294 	/*
6295 	 * This complies with the new power management framework
6296 	 * for certain desktop machines. Create the pm_components
6297 	 * property as a string array property.
6298 	 *
6299 	 * If this is a removable device or if the pm-capable property
6300 	 * is SD_PM_CAPABLE_TRUE (1) then we should create the
6301 	 * pm_components property without checking for the existance of
6302 	 * the start-stop cycle counter log page
6303 	 */
6304 	if (ISREMOVABLE(un) ||
6305 	    un->un_pm_capable_prop == SD_PM_CAPABLE_TRUE) {
6306 		/*
6307 		 * not all devices have a motor, try it first.
6308 		 * some devices may return ILLEGAL REQUEST, some
6309 		 * will hang
6310 		 */
6311 		un->un_f_start_stop_supported = TRUE;
6312 		if (sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
6313 		    SD_PATH_DIRECT) != 0) {
6314 			un->un_f_start_stop_supported = FALSE;
6315 		}
6316 
6317 		/*
6318 		 * create pm properties anyways otherwise the parent can't
6319 		 * go to sleep
6320 		 */
6321 		(void) sd_create_pm_components(devi, un);
6322 		un->un_f_pm_is_enabled = TRUE;
6323 
6324 		/*
6325 		 * Need to create a zero length (Boolean) property
6326 		 * removable-media for the removable media devices.
6327 		 * Note that the return value of the property is not being
6328 		 * checked, since if unable to create the property
6329 		 * then do not want the attach to fail altogether. Consistent
6330 		 * with other property creation in attach.
6331 		 */
6332 		if (ISREMOVABLE(un)) {
6333 			(void) ddi_prop_create(DDI_DEV_T_NONE, devi,
6334 			    DDI_PROP_CANSLEEP, "removable-media", NULL, 0);
6335 		}
6336 		return;
6337 	}
6338 
6339 	rval = sd_log_page_supported(un, START_STOP_CYCLE_PAGE);
6340 
6341 #ifdef	SDDEBUG
6342 	if (sd_force_pm_supported) {
6343 		/* Force a successful result */
6344 		rval = 1;
6345 	}
6346 #endif
6347 
6348 	/*
6349 	 * If the start-stop cycle counter log page is not supported
6350 	 * or if the pm-capable property is SD_PM_CAPABLE_FALSE (0)
6351 	 * then we should not create the pm_components property.
6352 	 */
6353 	if (rval == -1 || un->un_pm_capable_prop == SD_PM_CAPABLE_FALSE) {
6354 		/*
6355 		 * Error.
6356 		 * Reading log sense failed, most likely this is
6357 		 * an older drive that does not support log sense.
6358 		 * If this fails auto-pm is not supported.
6359 		 */
6360 		un->un_power_level = SD_SPINDLE_ON;
6361 		un->un_f_pm_is_enabled = FALSE;
6362 
6363 	} else if (rval == 0) {
6364 		/*
6365 		 * Page not found.
6366 		 * The start stop cycle counter is implemented as page
6367 		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6368 		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6369 		 */
6370 		if (sd_log_page_supported(un, START_STOP_CYCLE_VU_PAGE) == 1) {
6371 			/*
6372 			 * Page found, use this one.
6373 			 */
6374 			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6375 			un->un_f_pm_is_enabled = TRUE;
6376 		} else {
6377 			/*
6378 			 * Error or page not found.
6379 			 * auto-pm is not supported for this device.
6380 			 */
6381 			un->un_power_level = SD_SPINDLE_ON;
6382 			un->un_f_pm_is_enabled = FALSE;
6383 		}
6384 	} else {
6385 		/*
6386 		 * Page found, use it.
6387 		 */
6388 		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6389 		un->un_f_pm_is_enabled = TRUE;
6390 	}
6391 
6392 
6393 	if (un->un_f_pm_is_enabled == TRUE) {
6394 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6395 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6396 
6397 		rval = sd_send_scsi_LOG_SENSE(un, log_page_data,
6398 		    log_page_size, un->un_start_stop_cycle_page,
6399 		    0x01, 0, SD_PATH_DIRECT);
6400 #ifdef	SDDEBUG
6401 		if (sd_force_pm_supported) {
6402 			/* Force a successful result */
6403 			rval = 0;
6404 		}
6405 #endif
6406 
6407 		/*
6408 		 * If the Log sense for Page( Start/stop cycle counter page)
6409 		 * succeeds, then power managment is supported and we can
6410 		 * enable auto-pm.
6411 		 */
6412 		if (rval == 0)  {
6413 			(void) sd_create_pm_components(devi, un);
6414 		} else {
6415 			un->un_power_level = SD_SPINDLE_ON;
6416 			un->un_f_pm_is_enabled = FALSE;
6417 		}
6418 
6419 		kmem_free(log_page_data, log_page_size);
6420 	}
6421 }
6422 
6423 
6424 /*
6425  *    Function: sd_create_pm_components
6426  *
6427  * Description: Initialize PM property.
6428  *
6429  *     Context: Kernel thread context
6430  */
6431 
6432 static void
6433 sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6434 {
6435 	char *pm_comp[] = { "NAME=spindle-motor", "0=off", "1=on", NULL };
6436 
6437 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6438 
6439 	if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6440 	    "pm-components", pm_comp, 3) == DDI_PROP_SUCCESS) {
6441 		/*
6442 		 * When components are initially created they are idle,
6443 		 * power up any non-removables.
6444 		 * Note: the return value of pm_raise_power can't be used
6445 		 * for determining if PM should be enabled for this device.
6446 		 * Even if you check the return values and remove this
6447 		 * property created above, the PM framework will not honor the
6448 		 * change after the first call to pm_raise_power. Hence,
6449 		 * removal of that property does not help if pm_raise_power
6450 		 * fails. In the case of removable media, the start/stop
6451 		 * will fail if the media is not present.
6452 		 */
6453 		if ((!ISREMOVABLE(un)) && (pm_raise_power(SD_DEVINFO(un), 0,
6454 		    SD_SPINDLE_ON) == DDI_SUCCESS)) {
6455 			mutex_enter(SD_MUTEX(un));
6456 			un->un_power_level = SD_SPINDLE_ON;
6457 			mutex_enter(&un->un_pm_mutex);
6458 			/* Set to on and not busy. */
6459 			un->un_pm_count = 0;
6460 		} else {
6461 			mutex_enter(SD_MUTEX(un));
6462 			un->un_power_level = SD_SPINDLE_OFF;
6463 			mutex_enter(&un->un_pm_mutex);
6464 			/* Set to off. */
6465 			un->un_pm_count = -1;
6466 		}
6467 		mutex_exit(&un->un_pm_mutex);
6468 		mutex_exit(SD_MUTEX(un));
6469 	} else {
6470 		un->un_power_level = SD_SPINDLE_ON;
6471 		un->un_f_pm_is_enabled = FALSE;
6472 	}
6473 }
6474 
6475 
6476 /*
6477  *    Function: sd_ddi_suspend
6478  *
6479  * Description: Performs system power-down operations. This includes
6480  *		setting the drive state to indicate its suspended so
6481  *		that no new commands will be accepted. Also, wait for
6482  *		all commands that are in transport or queued to a timer
6483  *		for retry to complete. All timeout threads are cancelled.
6484  *
6485  * Return Code: DDI_FAILURE or DDI_SUCCESS
6486  *
6487  *     Context: Kernel thread context
6488  */
6489 
6490 static int
6491 sd_ddi_suspend(dev_info_t *devi)
6492 {
6493 	struct	sd_lun	*un;
6494 	clock_t		wait_cmds_complete;
6495 
6496 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6497 	if (un == NULL) {
6498 		return (DDI_FAILURE);
6499 	}
6500 
6501 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6502 
6503 	mutex_enter(SD_MUTEX(un));
6504 
6505 	/* Return success if the device is already suspended. */
6506 	if (un->un_state == SD_STATE_SUSPENDED) {
6507 		mutex_exit(SD_MUTEX(un));
6508 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6509 		    "device already suspended, exiting\n");
6510 		return (DDI_SUCCESS);
6511 	}
6512 
6513 	/* Return failure if the device is being used by HA */
6514 	if (un->un_resvd_status &
6515 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6516 		mutex_exit(SD_MUTEX(un));
6517 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6518 		    "device in use by HA, exiting\n");
6519 		return (DDI_FAILURE);
6520 	}
6521 
6522 	/*
6523 	 * Return failure if the device is in a resource wait
6524 	 * or power changing state.
6525 	 */
6526 	if ((un->un_state == SD_STATE_RWAIT) ||
6527 	    (un->un_state == SD_STATE_PM_CHANGING)) {
6528 		mutex_exit(SD_MUTEX(un));
6529 		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6530 		    "device in resource wait state, exiting\n");
6531 		return (DDI_FAILURE);
6532 	}
6533 
6534 
6535 	un->un_save_state = un->un_last_state;
6536 	New_state(un, SD_STATE_SUSPENDED);
6537 
6538 	/*
6539 	 * Wait for all commands that are in transport or queued to a timer
6540 	 * for retry to complete.
6541 	 *
6542 	 * While waiting, no new commands will be accepted or sent because of
6543 	 * the new state we set above.
6544 	 *
6545 	 * Wait till current operation has completed. If we are in the resource
6546 	 * wait state (with an intr outstanding) then we need to wait till the
6547 	 * intr completes and starts the next cmd. We want to wait for
6548 	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6549 	 */
6550 	wait_cmds_complete = ddi_get_lbolt() +
6551 	    (sd_wait_cmds_complete * drv_usectohz(1000000));
6552 
6553 	while (un->un_ncmds_in_transport != 0) {
6554 		/*
6555 		 * Fail if commands do not finish in the specified time.
6556 		 */
6557 		if (cv_timedwait(&un->un_disk_busy_cv, SD_MUTEX(un),
6558 		    wait_cmds_complete) == -1) {
6559 			/*
6560 			 * Undo the state changes made above. Everything
6561 			 * must go back to it's original value.
6562 			 */
6563 			Restore_state(un);
6564 			un->un_last_state = un->un_save_state;
6565 			/* Wake up any threads that might be waiting. */
6566 			cv_broadcast(&un->un_suspend_cv);
6567 			mutex_exit(SD_MUTEX(un));
6568 			SD_ERROR(SD_LOG_IO_PM, un,
6569 			    "sd_ddi_suspend: failed due to outstanding cmds\n");
6570 			SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exiting\n");
6571 			return (DDI_FAILURE);
6572 		}
6573 	}
6574 
6575 	/*
6576 	 * Cancel SCSI watch thread and timeouts, if any are active
6577 	 */
6578 
6579 	if (SD_OK_TO_SUSPEND_SCSI_WATCHER(un)) {
6580 		opaque_t temp_token = un->un_swr_token;
6581 		mutex_exit(SD_MUTEX(un));
6582 		scsi_watch_suspend(temp_token);
6583 		mutex_enter(SD_MUTEX(un));
6584 	}
6585 
6586 	if (un->un_reset_throttle_timeid != NULL) {
6587 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
6588 		un->un_reset_throttle_timeid = NULL;
6589 		mutex_exit(SD_MUTEX(un));
6590 		(void) untimeout(temp_id);
6591 		mutex_enter(SD_MUTEX(un));
6592 	}
6593 
6594 	if (un->un_dcvb_timeid != NULL) {
6595 		timeout_id_t temp_id = un->un_dcvb_timeid;
6596 		un->un_dcvb_timeid = NULL;
6597 		mutex_exit(SD_MUTEX(un));
6598 		(void) untimeout(temp_id);
6599 		mutex_enter(SD_MUTEX(un));
6600 	}
6601 
6602 	mutex_enter(&un->un_pm_mutex);
6603 	if (un->un_pm_timeid != NULL) {
6604 		timeout_id_t temp_id = un->un_pm_timeid;
6605 		un->un_pm_timeid = NULL;
6606 		mutex_exit(&un->un_pm_mutex);
6607 		mutex_exit(SD_MUTEX(un));
6608 		(void) untimeout(temp_id);
6609 		mutex_enter(SD_MUTEX(un));
6610 	} else {
6611 		mutex_exit(&un->un_pm_mutex);
6612 	}
6613 
6614 	if (un->un_retry_timeid != NULL) {
6615 		timeout_id_t temp_id = un->un_retry_timeid;
6616 		un->un_retry_timeid = NULL;
6617 		mutex_exit(SD_MUTEX(un));
6618 		(void) untimeout(temp_id);
6619 		mutex_enter(SD_MUTEX(un));
6620 	}
6621 
6622 	if (un->un_direct_priority_timeid != NULL) {
6623 		timeout_id_t temp_id = un->un_direct_priority_timeid;
6624 		un->un_direct_priority_timeid = NULL;
6625 		mutex_exit(SD_MUTEX(un));
6626 		(void) untimeout(temp_id);
6627 		mutex_enter(SD_MUTEX(un));
6628 	}
6629 
6630 	if (un->un_f_is_fibre == TRUE) {
6631 		/*
6632 		 * Remove callbacks for insert and remove events
6633 		 */
6634 		if (un->un_insert_event != NULL) {
6635 			mutex_exit(SD_MUTEX(un));
6636 			(void) ddi_remove_event_handler(un->un_insert_cb_id);
6637 			mutex_enter(SD_MUTEX(un));
6638 			un->un_insert_event = NULL;
6639 		}
6640 
6641 		if (un->un_remove_event != NULL) {
6642 			mutex_exit(SD_MUTEX(un));
6643 			(void) ddi_remove_event_handler(un->un_remove_cb_id);
6644 			mutex_enter(SD_MUTEX(un));
6645 			un->un_remove_event = NULL;
6646 		}
6647 	}
6648 
6649 	mutex_exit(SD_MUTEX(un));
6650 
6651 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: exit\n");
6652 
6653 	return (DDI_SUCCESS);
6654 }
6655 
6656 
6657 /*
6658  *    Function: sd_ddi_pm_suspend
6659  *
6660  * Description: Set the drive state to low power.
6661  *		Someone else is required to actually change the drive
6662  *		power level.
6663  *
6664  *   Arguments: un - driver soft state (unit) structure
6665  *
6666  * Return Code: DDI_FAILURE or DDI_SUCCESS
6667  *
6668  *     Context: Kernel thread context
6669  */
6670 
6671 static int
6672 sd_ddi_pm_suspend(struct sd_lun *un)
6673 {
6674 	ASSERT(un != NULL);
6675 	SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: entry\n");
6676 
6677 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6678 	mutex_enter(SD_MUTEX(un));
6679 
6680 	/*
6681 	 * Exit if power management is not enabled for this device, or if
6682 	 * the device is being used by HA.
6683 	 */
6684 	if ((un->un_f_pm_is_enabled == FALSE) || (un->un_resvd_status &
6685 	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE))) {
6686 		mutex_exit(SD_MUTEX(un));
6687 		SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exiting\n");
6688 		return (DDI_SUCCESS);
6689 	}
6690 
6691 	SD_INFO(SD_LOG_POWER, un, "sd_ddi_pm_suspend: un_ncmds_in_driver=%ld\n",
6692 	    un->un_ncmds_in_driver);
6693 
6694 	/*
6695 	 * See if the device is not busy, ie.:
6696 	 *    - we have no commands in the driver for this device
6697 	 *    - not waiting for resources
6698 	 */
6699 	if ((un->un_ncmds_in_driver == 0) &&
6700 	    (un->un_state != SD_STATE_RWAIT)) {
6701 		/*
6702 		 * The device is not busy, so it is OK to go to low power state.
6703 		 * Indicate low power, but rely on someone else to actually
6704 		 * change it.
6705 		 */
6706 		mutex_enter(&un->un_pm_mutex);
6707 		un->un_pm_count = -1;
6708 		mutex_exit(&un->un_pm_mutex);
6709 		un->un_power_level = SD_SPINDLE_OFF;
6710 	}
6711 
6712 	mutex_exit(SD_MUTEX(un));
6713 
6714 	SD_TRACE(SD_LOG_POWER, un, "sd_ddi_pm_suspend: exit\n");
6715 
6716 	return (DDI_SUCCESS);
6717 }
6718 
6719 
6720 /*
6721  *    Function: sd_ddi_resume
6722  *
6723  * Description: Performs system power-up operations..
6724  *
6725  * Return Code: DDI_SUCCESS
6726  *		DDI_FAILURE
6727  *
6728  *     Context: Kernel thread context
6729  */
6730 
6731 static int
6732 sd_ddi_resume(dev_info_t *devi)
6733 {
6734 	struct	sd_lun	*un;
6735 
6736 	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6737 	if (un == NULL) {
6738 		return (DDI_FAILURE);
6739 	}
6740 
6741 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: entry\n");
6742 
6743 	mutex_enter(SD_MUTEX(un));
6744 	Restore_state(un);
6745 
6746 	/*
6747 	 * Restore the state which was saved to give the
6748 	 * the right state in un_last_state
6749 	 */
6750 	un->un_last_state = un->un_save_state;
6751 	/*
6752 	 * Note: throttle comes back at full.
6753 	 * Also note: this MUST be done before calling pm_raise_power
6754 	 * otherwise the system can get hung in biowait. The scenario where
6755 	 * this'll happen is under cpr suspend. Writing of the system
6756 	 * state goes through sddump, which writes 0 to un_throttle. If
6757 	 * writing the system state then fails, example if the partition is
6758 	 * too small, then cpr attempts a resume. If throttle isn't restored
6759 	 * from the saved value until after calling pm_raise_power then
6760 	 * cmds sent in sdpower are not transported and sd_send_scsi_cmd hangs
6761 	 * in biowait.
6762 	 */
6763 	un->un_throttle = un->un_saved_throttle;
6764 
6765 	/*
6766 	 * The chance of failure is very rare as the only command done in power
6767 	 * entry point is START command when you transition from 0->1 or
6768 	 * unknown->1. Put it to SPINDLE ON state irrespective of the state at
6769 	 * which suspend was done. Ignore the return value as the resume should
6770 	 * not be failed. In the case of removable media the media need not be
6771 	 * inserted and hence there is a chance that raise power will fail with
6772 	 * media not present.
6773 	 */
6774 	if (!ISREMOVABLE(un)) {
6775 		mutex_exit(SD_MUTEX(un));
6776 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
6777 		mutex_enter(SD_MUTEX(un));
6778 	}
6779 
6780 	/*
6781 	 * Don't broadcast to the suspend cv and therefore possibly
6782 	 * start I/O until after power has been restored.
6783 	 */
6784 	cv_broadcast(&un->un_suspend_cv);
6785 	cv_broadcast(&un->un_state_cv);
6786 
6787 	/* restart thread */
6788 	if (SD_OK_TO_RESUME_SCSI_WATCHER(un)) {
6789 		scsi_watch_resume(un->un_swr_token);
6790 	}
6791 
6792 #if (defined(__fibre))
6793 	if (un->un_f_is_fibre == TRUE) {
6794 		/*
6795 		 * Add callbacks for insert and remove events
6796 		 */
6797 		if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
6798 			sd_init_event_callbacks(un);
6799 		}
6800 	}
6801 #endif
6802 
6803 	/*
6804 	 * Transport any pending commands to the target.
6805 	 *
6806 	 * If this is a low-activity device commands in queue will have to wait
6807 	 * until new commands come in, which may take awhile. Also, we
6808 	 * specifically don't check un_ncmds_in_transport because we know that
6809 	 * there really are no commands in progress after the unit was
6810 	 * suspended and we could have reached the throttle level, been
6811 	 * suspended, and have no new commands coming in for awhile. Highly
6812 	 * unlikely, but so is the low-activity disk scenario.
6813 	 */
6814 	ddi_xbuf_dispatch(un->un_xbuf_attr);
6815 
6816 	sd_start_cmds(un, NULL);
6817 	mutex_exit(SD_MUTEX(un));
6818 
6819 	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_resume: exit\n");
6820 
6821 	return (DDI_SUCCESS);
6822 }
6823 
6824 
6825 /*
6826  *    Function: sd_ddi_pm_resume
6827  *
6828  * Description: Set the drive state to powered on.
6829  *		Someone else is required to actually change the drive
6830  *		power level.
6831  *
6832  *   Arguments: un - driver soft state (unit) structure
6833  *
6834  * Return Code: DDI_SUCCESS
6835  *
6836  *     Context: Kernel thread context
6837  */
6838 
6839 static int
6840 sd_ddi_pm_resume(struct sd_lun *un)
6841 {
6842 	ASSERT(un != NULL);
6843 
6844 	ASSERT(!mutex_owned(SD_MUTEX(un)));
6845 	mutex_enter(SD_MUTEX(un));
6846 	un->un_power_level = SD_SPINDLE_ON;
6847 
6848 	ASSERT(!mutex_owned(&un->un_pm_mutex));
6849 	mutex_enter(&un->un_pm_mutex);
6850 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
6851 		un->un_pm_count++;
6852 		ASSERT(un->un_pm_count == 0);
6853 		/*
6854 		 * Note: no longer do the cv_broadcast on un_suspend_cv. The
6855 		 * un_suspend_cv is for a system resume, not a power management
6856 		 * device resume. (4297749)
6857 		 *	 cv_broadcast(&un->un_suspend_cv);
6858 		 */
6859 	}
6860 	mutex_exit(&un->un_pm_mutex);
6861 	mutex_exit(SD_MUTEX(un));
6862 
6863 	return (DDI_SUCCESS);
6864 }
6865 
6866 
6867 /*
6868  *    Function: sd_pm_idletimeout_handler
6869  *
6870  * Description: A timer routine that's active only while a device is busy.
6871  *		The purpose is to extend slightly the pm framework's busy
6872  *		view of the device to prevent busy/idle thrashing for
6873  *		back-to-back commands. Do this by comparing the current time
6874  *		to the time at which the last command completed and when the
6875  *		difference is greater than sd_pm_idletime, call
6876  *		pm_idle_component. In addition to indicating idle to the pm
6877  *		framework, update the chain type to again use the internal pm
6878  *		layers of the driver.
6879  *
6880  *   Arguments: arg - driver soft state (unit) structure
6881  *
6882  *     Context: Executes in a timeout(9F) thread context
6883  */
6884 
6885 static void
6886 sd_pm_idletimeout_handler(void *arg)
6887 {
6888 	struct sd_lun *un = arg;
6889 
6890 	time_t	now;
6891 
6892 	mutex_enter(&sd_detach_mutex);
6893 	if (un->un_detach_count != 0) {
6894 		/* Abort if the instance is detaching */
6895 		mutex_exit(&sd_detach_mutex);
6896 		return;
6897 	}
6898 	mutex_exit(&sd_detach_mutex);
6899 
6900 	now = ddi_get_time();
6901 	/*
6902 	 * Grab both mutexes, in the proper order, since we're accessing
6903 	 * both PM and softstate variables.
6904 	 */
6905 	mutex_enter(SD_MUTEX(un));
6906 	mutex_enter(&un->un_pm_mutex);
6907 	if (((now - un->un_pm_idle_time) > sd_pm_idletime) &&
6908 	    (un->un_ncmds_in_driver == 0) && (un->un_pm_count == 0)) {
6909 		/*
6910 		 * Update the chain types.
6911 		 * This takes affect on the next new command received.
6912 		 */
6913 		if (ISREMOVABLE(un)) {
6914 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
6915 		} else {
6916 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
6917 		}
6918 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
6919 
6920 		SD_TRACE(SD_LOG_IO_PM, un,
6921 		    "sd_pm_idletimeout_handler: idling device\n");
6922 		(void) pm_idle_component(SD_DEVINFO(un), 0);
6923 		un->un_pm_idle_timeid = NULL;
6924 	} else {
6925 		un->un_pm_idle_timeid =
6926 			timeout(sd_pm_idletimeout_handler, un,
6927 			(drv_usectohz((clock_t)300000))); /* 300 ms. */
6928 	}
6929 	mutex_exit(&un->un_pm_mutex);
6930 	mutex_exit(SD_MUTEX(un));
6931 }
6932 
6933 
6934 /*
6935  *    Function: sd_pm_timeout_handler
6936  *
6937  * Description: Callback to tell framework we are idle.
6938  *
6939  *     Context: timeout(9f) thread context.
6940  */
6941 
6942 static void
6943 sd_pm_timeout_handler(void *arg)
6944 {
6945 	struct sd_lun *un = arg;
6946 
6947 	(void) pm_idle_component(SD_DEVINFO(un), 0);
6948 	mutex_enter(&un->un_pm_mutex);
6949 	un->un_pm_timeid = NULL;
6950 	mutex_exit(&un->un_pm_mutex);
6951 }
6952 
6953 
6954 /*
6955  *    Function: sdpower
6956  *
6957  * Description: PM entry point.
6958  *
6959  * Return Code: DDI_SUCCESS
6960  *		DDI_FAILURE
6961  *
6962  *     Context: Kernel thread context
6963  */
6964 
6965 static int
6966 sdpower(dev_info_t *devi, int component, int level)
6967 {
6968 	struct sd_lun	*un;
6969 	int		instance;
6970 	int		rval = DDI_SUCCESS;
6971 	uint_t		i, log_page_size, maxcycles, ncycles;
6972 	uchar_t		*log_page_data;
6973 	int		log_sense_page;
6974 	int		medium_present;
6975 	time_t		intvlp;
6976 	dev_t		dev;
6977 	struct pm_trans_data	sd_pm_tran_data;
6978 	uchar_t		save_state;
6979 	int		sval;
6980 	uchar_t		state_before_pm;
6981 	int		got_semaphore_here;
6982 
6983 	instance = ddi_get_instance(devi);
6984 
6985 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
6986 	    (SD_SPINDLE_OFF > level) || (level > SD_SPINDLE_ON) ||
6987 	    component != 0) {
6988 		return (DDI_FAILURE);
6989 	}
6990 
6991 	dev = sd_make_device(SD_DEVINFO(un));
6992 
6993 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: entry, level = %d\n", level);
6994 
6995 	/*
6996 	 * Must synchronize power down with close.
6997 	 * Attempt to decrement/acquire the open/close semaphore,
6998 	 * but do NOT wait on it. If it's not greater than zero,
6999 	 * ie. it can't be decremented without waiting, then
7000 	 * someone else, either open or close, already has it
7001 	 * and the try returns 0. Use that knowledge here to determine
7002 	 * if it's OK to change the device power level.
7003 	 * Also, only increment it on exit if it was decremented, ie. gotten,
7004 	 * here.
7005 	 */
7006 	got_semaphore_here = sema_tryp(&un->un_semoclose);
7007 
7008 	mutex_enter(SD_MUTEX(un));
7009 
7010 	SD_INFO(SD_LOG_POWER, un, "sdpower: un_ncmds_in_driver = %ld\n",
7011 	    un->un_ncmds_in_driver);
7012 
7013 	/*
7014 	 * If un_ncmds_in_driver is non-zero it indicates commands are
7015 	 * already being processed in the driver, or if the semaphore was
7016 	 * not gotten here it indicates an open or close is being processed.
7017 	 * At the same time somebody is requesting to go low power which
7018 	 * can't happen, therefore we need to return failure.
7019 	 */
7020 	if ((level == SD_SPINDLE_OFF) &&
7021 	    ((un->un_ncmds_in_driver != 0) || (got_semaphore_here == 0))) {
7022 		mutex_exit(SD_MUTEX(un));
7023 
7024 		if (got_semaphore_here != 0) {
7025 			sema_v(&un->un_semoclose);
7026 		}
7027 		SD_TRACE(SD_LOG_IO_PM, un,
7028 		    "sdpower: exit, device has queued cmds.\n");
7029 		return (DDI_FAILURE);
7030 	}
7031 
7032 	/*
7033 	 * if it is OFFLINE that means the disk is completely dead
7034 	 * in our case we have to put the disk in on or off by sending commands
7035 	 * Of course that will fail anyway so return back here.
7036 	 *
7037 	 * Power changes to a device that's OFFLINE or SUSPENDED
7038 	 * are not allowed.
7039 	 */
7040 	if ((un->un_state == SD_STATE_OFFLINE) ||
7041 	    (un->un_state == SD_STATE_SUSPENDED)) {
7042 		mutex_exit(SD_MUTEX(un));
7043 
7044 		if (got_semaphore_here != 0) {
7045 			sema_v(&un->un_semoclose);
7046 		}
7047 		SD_TRACE(SD_LOG_IO_PM, un,
7048 		    "sdpower: exit, device is off-line.\n");
7049 		return (DDI_FAILURE);
7050 	}
7051 
7052 	/*
7053 	 * Change the device's state to indicate it's power level
7054 	 * is being changed. Do this to prevent a power off in the
7055 	 * middle of commands, which is especially bad on devices
7056 	 * that are really powered off instead of just spun down.
7057 	 */
7058 	state_before_pm = un->un_state;
7059 	un->un_state = SD_STATE_PM_CHANGING;
7060 
7061 	mutex_exit(SD_MUTEX(un));
7062 
7063 	/*
7064 	 * Bypass checking the log sense information for removables
7065 	 * and devices for which the HBA set the pm-capable property.
7066 	 * If un->un_pm_capable_prop is SD_PM_CAPABLE_UNDEFINED (-1)
7067 	 * then the HBA did not create the property.
7068 	 */
7069 	if ((level == SD_SPINDLE_OFF) && (!ISREMOVABLE(un)) &&
7070 	    un->un_pm_capable_prop == SD_PM_CAPABLE_UNDEFINED) {
7071 		/*
7072 		 * Get the log sense information to understand whether the
7073 		 * the powercycle counts have gone beyond the threshhold.
7074 		 */
7075 		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
7076 		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
7077 
7078 		mutex_enter(SD_MUTEX(un));
7079 		log_sense_page = un->un_start_stop_cycle_page;
7080 		mutex_exit(SD_MUTEX(un));
7081 
7082 		rval = sd_send_scsi_LOG_SENSE(un, log_page_data,
7083 		    log_page_size, log_sense_page, 0x01, 0, SD_PATH_DIRECT);
7084 #ifdef	SDDEBUG
7085 		if (sd_force_pm_supported) {
7086 			/* Force a successful result */
7087 			rval = 0;
7088 		}
7089 #endif
7090 		if (rval != 0) {
7091 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
7092 			    "Log Sense Failed\n");
7093 			kmem_free(log_page_data, log_page_size);
7094 			/* Cannot support power management on those drives */
7095 
7096 			if (got_semaphore_here != 0) {
7097 				sema_v(&un->un_semoclose);
7098 			}
7099 			/*
7100 			 * On exit put the state back to it's original value
7101 			 * and broadcast to anyone waiting for the power
7102 			 * change completion.
7103 			 */
7104 			mutex_enter(SD_MUTEX(un));
7105 			un->un_state = state_before_pm;
7106 			cv_broadcast(&un->un_suspend_cv);
7107 			mutex_exit(SD_MUTEX(un));
7108 			SD_TRACE(SD_LOG_IO_PM, un,
7109 			    "sdpower: exit, Log Sense Failed.\n");
7110 			return (DDI_FAILURE);
7111 		}
7112 
7113 		/*
7114 		 * From the page data - Convert the essential information to
7115 		 * pm_trans_data
7116 		 */
7117 		maxcycles =
7118 		    (log_page_data[0x1c] << 24) | (log_page_data[0x1d] << 16) |
7119 		    (log_page_data[0x1E] << 8)  | log_page_data[0x1F];
7120 
7121 		sd_pm_tran_data.un.scsi_cycles.lifemax = maxcycles;
7122 
7123 		ncycles =
7124 		    (log_page_data[0x24] << 24) | (log_page_data[0x25] << 16) |
7125 		    (log_page_data[0x26] << 8)  | log_page_data[0x27];
7126 
7127 		sd_pm_tran_data.un.scsi_cycles.ncycles = ncycles;
7128 
7129 		for (i = 0; i < DC_SCSI_MFR_LEN; i++) {
7130 			sd_pm_tran_data.un.scsi_cycles.svc_date[i] =
7131 			    log_page_data[8+i];
7132 		}
7133 
7134 		kmem_free(log_page_data, log_page_size);
7135 
7136 		/*
7137 		 * Call pm_trans_check routine to get the Ok from
7138 		 * the global policy
7139 		 */
7140 
7141 		sd_pm_tran_data.format = DC_SCSI_FORMAT;
7142 		sd_pm_tran_data.un.scsi_cycles.flag = 0;
7143 
7144 		rval = pm_trans_check(&sd_pm_tran_data, &intvlp);
7145 #ifdef	SDDEBUG
7146 		if (sd_force_pm_supported) {
7147 			/* Force a successful result */
7148 			rval = 1;
7149 		}
7150 #endif
7151 		switch (rval) {
7152 		case 0:
7153 			/*
7154 			 * Not Ok to Power cycle or error in parameters passed
7155 			 * Would have given the advised time to consider power
7156 			 * cycle. Based on the new intvlp parameter we are
7157 			 * supposed to pretend we are busy so that pm framework
7158 			 * will never call our power entry point. Because of
7159 			 * that install a timeout handler and wait for the
7160 			 * recommended time to elapse so that power management
7161 			 * can be effective again.
7162 			 *
7163 			 * To effect this behavior, call pm_busy_component to
7164 			 * indicate to the framework this device is busy.
7165 			 * By not adjusting un_pm_count the rest of PM in
7166 			 * the driver will function normally, and independant
7167 			 * of this but because the framework is told the device
7168 			 * is busy it won't attempt powering down until it gets
7169 			 * a matching idle. The timeout handler sends this.
7170 			 * Note: sd_pm_entry can't be called here to do this
7171 			 * because sdpower may have been called as a result
7172 			 * of a call to pm_raise_power from within sd_pm_entry.
7173 			 *
7174 			 * If a timeout handler is already active then
7175 			 * don't install another.
7176 			 */
7177 			mutex_enter(&un->un_pm_mutex);
7178 			if (un->un_pm_timeid == NULL) {
7179 				un->un_pm_timeid =
7180 				    timeout(sd_pm_timeout_handler,
7181 				    un, intvlp * drv_usectohz(1000000));
7182 				mutex_exit(&un->un_pm_mutex);
7183 				(void) pm_busy_component(SD_DEVINFO(un), 0);
7184 			} else {
7185 				mutex_exit(&un->un_pm_mutex);
7186 			}
7187 			if (got_semaphore_here != 0) {
7188 				sema_v(&un->un_semoclose);
7189 			}
7190 			/*
7191 			 * On exit put the state back to it's original value
7192 			 * and broadcast to anyone waiting for the power
7193 			 * change completion.
7194 			 */
7195 			mutex_enter(SD_MUTEX(un));
7196 			un->un_state = state_before_pm;
7197 			cv_broadcast(&un->un_suspend_cv);
7198 			mutex_exit(SD_MUTEX(un));
7199 
7200 			SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, "
7201 			    "trans check Failed, not ok to power cycle.\n");
7202 			return (DDI_FAILURE);
7203 
7204 		case -1:
7205 			if (got_semaphore_here != 0) {
7206 				sema_v(&un->un_semoclose);
7207 			}
7208 			/*
7209 			 * On exit put the state back to it's original value
7210 			 * and broadcast to anyone waiting for the power
7211 			 * change completion.
7212 			 */
7213 			mutex_enter(SD_MUTEX(un));
7214 			un->un_state = state_before_pm;
7215 			cv_broadcast(&un->un_suspend_cv);
7216 			mutex_exit(SD_MUTEX(un));
7217 			SD_TRACE(SD_LOG_IO_PM, un,
7218 			    "sdpower: exit, trans check command Failed.\n");
7219 			return (DDI_FAILURE);
7220 		}
7221 	}
7222 
7223 	if (level == SD_SPINDLE_OFF) {
7224 		/*
7225 		 * Save the last state... if the STOP FAILS we need it
7226 		 * for restoring
7227 		 */
7228 		mutex_enter(SD_MUTEX(un));
7229 		save_state = un->un_last_state;
7230 		/*
7231 		 * There must not be any cmds. getting processed
7232 		 * in the driver when we get here. Power to the
7233 		 * device is potentially going off.
7234 		 */
7235 		ASSERT(un->un_ncmds_in_driver == 0);
7236 		mutex_exit(SD_MUTEX(un));
7237 
7238 		/*
7239 		 * For now suspend the device completely before spindle is
7240 		 * turned off
7241 		 */
7242 		if ((rval = sd_ddi_pm_suspend(un)) == DDI_FAILURE) {
7243 			if (got_semaphore_here != 0) {
7244 				sema_v(&un->un_semoclose);
7245 			}
7246 			/*
7247 			 * On exit put the state back to it's original value
7248 			 * and broadcast to anyone waiting for the power
7249 			 * change completion.
7250 			 */
7251 			mutex_enter(SD_MUTEX(un));
7252 			un->un_state = state_before_pm;
7253 			cv_broadcast(&un->un_suspend_cv);
7254 			mutex_exit(SD_MUTEX(un));
7255 			SD_TRACE(SD_LOG_IO_PM, un,
7256 			    "sdpower: exit, PM suspend Failed.\n");
7257 			return (DDI_FAILURE);
7258 		}
7259 	}
7260 
7261 	/*
7262 	 * The transition from SPINDLE_OFF to SPINDLE_ON can happen in open,
7263 	 * close, or strategy. Dump no long uses this routine, it uses it's
7264 	 * own code so it can be done in polled mode.
7265 	 */
7266 
7267 	medium_present = TRUE;
7268 
7269 	/*
7270 	 * When powering up, issue a TUR in case the device is at unit
7271 	 * attention.  Don't do retries. Bypass the PM layer, otherwise
7272 	 * a deadlock on un_pm_busy_cv will occur.
7273 	 */
7274 	if (level == SD_SPINDLE_ON) {
7275 		(void) sd_send_scsi_TEST_UNIT_READY(un,
7276 		    SD_DONT_RETRY_TUR | SD_BYPASS_PM);
7277 	}
7278 
7279 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: sending \'%s\' unit\n",
7280 	    ((level == SD_SPINDLE_ON) ? "START" : "STOP"));
7281 
7282 	sval = sd_send_scsi_START_STOP_UNIT(un,
7283 	    ((level == SD_SPINDLE_ON) ? SD_TARGET_START : SD_TARGET_STOP),
7284 	    SD_PATH_DIRECT);
7285 	/* Command failed, check for media present. */
7286 	if ((sval == ENXIO) && ISREMOVABLE(un)) {
7287 		medium_present = FALSE;
7288 	}
7289 
7290 	/*
7291 	 * The conditions of interest here are:
7292 	 *   if a spindle off with media present fails,
7293 	 *	then restore the state and return an error.
7294 	 *   else if a spindle on fails,
7295 	 *	then return an error (there's no state to restore).
7296 	 * In all other cases we setup for the new state
7297 	 * and return success.
7298 	 */
7299 	switch (level) {
7300 	case SD_SPINDLE_OFF:
7301 		if ((medium_present == TRUE) && (sval != 0)) {
7302 			/* The stop command from above failed */
7303 			rval = DDI_FAILURE;
7304 			/*
7305 			 * The stop command failed, and we have media
7306 			 * present. Put the level back by calling the
7307 			 * sd_pm_resume() and set the state back to
7308 			 * it's previous value.
7309 			 */
7310 			(void) sd_ddi_pm_resume(un);
7311 			mutex_enter(SD_MUTEX(un));
7312 			un->un_last_state = save_state;
7313 			mutex_exit(SD_MUTEX(un));
7314 			break;
7315 		}
7316 		/*
7317 		 * The stop command from above succeeded.
7318 		 */
7319 		if (ISREMOVABLE(un)) {
7320 			/*
7321 			 * Terminate watch thread in case of removable media
7322 			 * devices going into low power state. This is as per
7323 			 * the requirements of pm framework, otherwise commands
7324 			 * will be generated for the device (through watch
7325 			 * thread), even when the device is in low power state.
7326 			 */
7327 			mutex_enter(SD_MUTEX(un));
7328 			un->un_f_watcht_stopped = FALSE;
7329 			if (un->un_swr_token != NULL) {
7330 				opaque_t temp_token = un->un_swr_token;
7331 				un->un_f_watcht_stopped = TRUE;
7332 				un->un_swr_token = NULL;
7333 				mutex_exit(SD_MUTEX(un));
7334 				(void) scsi_watch_request_terminate(temp_token,
7335 				    SCSI_WATCH_TERMINATE_WAIT);
7336 			} else {
7337 				mutex_exit(SD_MUTEX(un));
7338 			}
7339 		}
7340 		break;
7341 
7342 	default:	/* The level requested is spindle on... */
7343 		/*
7344 		 * Legacy behavior: return success on a failed spinup
7345 		 * if there is no media in the drive.
7346 		 * Do this by looking at medium_present here.
7347 		 */
7348 		if ((sval != 0) && medium_present) {
7349 			/* The start command from above failed */
7350 			rval = DDI_FAILURE;
7351 			break;
7352 		}
7353 		/*
7354 		 * The start command from above succeeded
7355 		 * Resume the devices now that we have
7356 		 * started the disks
7357 		 */
7358 		(void) sd_ddi_pm_resume(un);
7359 
7360 		/*
7361 		 * Resume the watch thread since it was suspended
7362 		 * when the device went into low power mode.
7363 		 */
7364 		if (ISREMOVABLE(un)) {
7365 			mutex_enter(SD_MUTEX(un));
7366 			if (un->un_f_watcht_stopped == TRUE) {
7367 				opaque_t temp_token;
7368 
7369 				un->un_f_watcht_stopped = FALSE;
7370 				mutex_exit(SD_MUTEX(un));
7371 				temp_token = scsi_watch_request_submit(
7372 				    SD_SCSI_DEVP(un),
7373 				    sd_check_media_time,
7374 				    SENSE_LENGTH, sd_media_watch_cb,
7375 				    (caddr_t)dev);
7376 				mutex_enter(SD_MUTEX(un));
7377 				un->un_swr_token = temp_token;
7378 			}
7379 			mutex_exit(SD_MUTEX(un));
7380 		}
7381 	}
7382 	if (got_semaphore_here != 0) {
7383 		sema_v(&un->un_semoclose);
7384 	}
7385 	/*
7386 	 * On exit put the state back to it's original value
7387 	 * and broadcast to anyone waiting for the power
7388 	 * change completion.
7389 	 */
7390 	mutex_enter(SD_MUTEX(un));
7391 	un->un_state = state_before_pm;
7392 	cv_broadcast(&un->un_suspend_cv);
7393 	mutex_exit(SD_MUTEX(un));
7394 
7395 	SD_TRACE(SD_LOG_IO_PM, un, "sdpower: exit, status = 0x%x\n", rval);
7396 
7397 	return (rval);
7398 }
7399 
7400 
7401 
7402 /*
7403  *    Function: sdattach
7404  *
7405  * Description: Driver's attach(9e) entry point function.
7406  *
7407  *   Arguments: devi - opaque device info handle
7408  *		cmd  - attach  type
7409  *
7410  * Return Code: DDI_SUCCESS
7411  *		DDI_FAILURE
7412  *
7413  *     Context: Kernel thread context
7414  */
7415 
7416 static int
7417 sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd)
7418 {
7419 	switch (cmd) {
7420 	case DDI_ATTACH:
7421 		return (sd_unit_attach(devi));
7422 	case DDI_RESUME:
7423 		return (sd_ddi_resume(devi));
7424 	default:
7425 		break;
7426 	}
7427 	return (DDI_FAILURE);
7428 }
7429 
7430 
7431 /*
7432  *    Function: sddetach
7433  *
7434  * Description: Driver's detach(9E) entry point function.
7435  *
7436  *   Arguments: devi - opaque device info handle
7437  *		cmd  - detach  type
7438  *
7439  * Return Code: DDI_SUCCESS
7440  *		DDI_FAILURE
7441  *
7442  *     Context: Kernel thread context
7443  */
7444 
7445 static int
7446 sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd)
7447 {
7448 	switch (cmd) {
7449 	case DDI_DETACH:
7450 		return (sd_unit_detach(devi));
7451 	case DDI_SUSPEND:
7452 		return (sd_ddi_suspend(devi));
7453 	default:
7454 		break;
7455 	}
7456 	return (DDI_FAILURE);
7457 }
7458 
7459 
7460 /*
7461  *     Function: sd_sync_with_callback
7462  *
7463  *  Description: Prevents sd_unit_attach or sd_unit_detach from freeing the soft
7464  *		 state while the callback routine is active.
7465  *
7466  *    Arguments: un: softstate structure for the instance
7467  *
7468  *	Context: Kernel thread context
7469  */
7470 
7471 static void
7472 sd_sync_with_callback(struct sd_lun *un)
7473 {
7474 	ASSERT(un != NULL);
7475 
7476 	mutex_enter(SD_MUTEX(un));
7477 
7478 	ASSERT(un->un_in_callback >= 0);
7479 
7480 	while (un->un_in_callback > 0) {
7481 		mutex_exit(SD_MUTEX(un));
7482 		delay(2);
7483 		mutex_enter(SD_MUTEX(un));
7484 	}
7485 
7486 	mutex_exit(SD_MUTEX(un));
7487 }
7488 
7489 /*
7490  *    Function: sd_unit_attach
7491  *
7492  * Description: Performs DDI_ATTACH processing for sdattach(). Allocates
7493  *		the soft state structure for the device and performs
7494  *		all necessary structure and device initializations.
7495  *
7496  *   Arguments: devi: the system's dev_info_t for the device.
7497  *
7498  * Return Code: DDI_SUCCESS if attach is successful.
7499  *		DDI_FAILURE if any part of the attach fails.
7500  *
7501  *     Context: Called at attach(9e) time for the DDI_ATTACH flag.
7502  *		Kernel thread context only.  Can sleep.
7503  */
7504 
7505 static int
7506 sd_unit_attach(dev_info_t *devi)
7507 {
7508 	struct	scsi_device	*devp;
7509 	struct	sd_lun		*un;
7510 	char			*variantp;
7511 	int	reservation_flag = SD_TARGET_IS_UNRESERVED;
7512 	int	instance;
7513 	int	rval;
7514 	uint64_t	capacity;
7515 	uint_t		lbasize;
7516 
7517 	/*
7518 	 * Retrieve the target driver's private data area. This was set
7519 	 * up by the HBA.
7520 	 */
7521 	devp = ddi_get_driver_private(devi);
7522 
7523 	/*
7524 	 * Since we have no idea what state things were left in by the last
7525 	 * user of the device, set up some 'default' settings, ie. turn 'em
7526 	 * off. The scsi_ifsetcap calls force re-negotiations with the drive.
7527 	 * Do this before the scsi_probe, which sends an inquiry.
7528 	 * This is a fix for bug (4430280).
7529 	 * Of special importance is wide-xfer. The drive could have been left
7530 	 * in wide transfer mode by the last driver to communicate with it,
7531 	 * this includes us. If that's the case, and if the following is not
7532 	 * setup properly or we don't re-negotiate with the drive prior to
7533 	 * transferring data to/from the drive, it causes bus parity errors,
7534 	 * data overruns, and unexpected interrupts. This first occurred when
7535 	 * the fix for bug (4378686) was made.
7536 	 */
7537 	(void) scsi_ifsetcap(&devp->sd_address, "lun-reset", 0, 1);
7538 	(void) scsi_ifsetcap(&devp->sd_address, "wide-xfer", 0, 1);
7539 	(void) scsi_ifsetcap(&devp->sd_address, "tagged-qing", 0, 1);
7540 	(void) scsi_ifsetcap(&devp->sd_address, "auto-rqsense", 0, 1);
7541 
7542 	/*
7543 	 * Use scsi_probe() to issue an INQUIRY command to the device.
7544 	 * This call will allocate and fill in the scsi_inquiry structure
7545 	 * and point the sd_inq member of the scsi_device structure to it.
7546 	 * If the attach succeeds, then this memory will not be de-allocated
7547 	 * (via scsi_unprobe()) until the instance is detached.
7548 	 */
7549 	if (scsi_probe(devp, SLEEP_FUNC) != SCSIPROBE_EXISTS) {
7550 		goto probe_failed;
7551 	}
7552 
7553 	/*
7554 	 * Check the device type as specified in the inquiry data and
7555 	 * claim it if it is of a type that we support.
7556 	 */
7557 	switch (devp->sd_inq->inq_dtype) {
7558 	case DTYPE_DIRECT:
7559 		break;
7560 	case DTYPE_RODIRECT:
7561 		break;
7562 	case DTYPE_OPTICAL:
7563 		break;
7564 	case DTYPE_NOTPRESENT:
7565 	default:
7566 		/* Unsupported device type; fail the attach. */
7567 		goto probe_failed;
7568 	}
7569 
7570 	/*
7571 	 * Allocate the soft state structure for this unit.
7572 	 *
7573 	 * We rely upon this memory being set to all zeroes by
7574 	 * ddi_soft_state_zalloc().  We assume that any member of the
7575 	 * soft state structure that is not explicitly initialized by
7576 	 * this routine will have a value of zero.
7577 	 */
7578 	instance = ddi_get_instance(devp->sd_dev);
7579 	if (ddi_soft_state_zalloc(sd_state, instance) != DDI_SUCCESS) {
7580 		goto probe_failed;
7581 	}
7582 
7583 	/*
7584 	 * Retrieve a pointer to the newly-allocated soft state.
7585 	 *
7586 	 * This should NEVER fail if the ddi_soft_state_zalloc() call above
7587 	 * was successful, unless something has gone horribly wrong and the
7588 	 * ddi's soft state internals are corrupt (in which case it is
7589 	 * probably better to halt here than just fail the attach....)
7590 	 */
7591 	if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
7592 		panic("sd_unit_attach: NULL soft state on instance:0x%x",
7593 		    instance);
7594 		/*NOTREACHED*/
7595 	}
7596 
7597 	/*
7598 	 * Link the back ptr of the driver soft state to the scsi_device
7599 	 * struct for this lun.
7600 	 * Save a pointer to the softstate in the driver-private area of
7601 	 * the scsi_device struct.
7602 	 * Note: We cannot call SD_INFO, SD_TRACE, SD_ERROR, or SD_DIAG until
7603 	 * we first set un->un_sd below.
7604 	 */
7605 	un->un_sd = devp;
7606 	devp->sd_private = (opaque_t)un;
7607 
7608 	/*
7609 	 * The following must be after devp is stored in the soft state struct.
7610 	 */
7611 #ifdef SDDEBUG
7612 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7613 	    "%s_unit_attach: un:0x%p instance:%d\n",
7614 	    ddi_driver_name(devi), un, instance);
7615 #endif
7616 
7617 	/*
7618 	 * Set up the device type and node type (for the minor nodes).
7619 	 * By default we assume that the device can at least support the
7620 	 * Common Command Set. Call it a CD-ROM if it reports itself
7621 	 * as a RODIRECT device.
7622 	 */
7623 	switch (devp->sd_inq->inq_dtype) {
7624 	case DTYPE_RODIRECT:
7625 		un->un_node_type = DDI_NT_CD_CHAN;
7626 		un->un_ctype	 = CTYPE_CDROM;
7627 		break;
7628 	case DTYPE_OPTICAL:
7629 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7630 		un->un_ctype	 = CTYPE_ROD;
7631 		break;
7632 	default:
7633 		un->un_node_type = DDI_NT_BLOCK_CHAN;
7634 		un->un_ctype	 = CTYPE_CCS;
7635 		break;
7636 	}
7637 
7638 	/*
7639 	 * Try to read the interconnect type from the HBA.
7640 	 *
7641 	 * Note: This driver is currently compiled as two binaries, a parallel
7642 	 * scsi version (sd) and a fibre channel version (ssd). All functional
7643 	 * differences are determined at compile time. In the future a single
7644 	 * binary will be provided and the inteconnect type will be used to
7645 	 * differentiate between fibre and parallel scsi behaviors. At that time
7646 	 * it will be necessary for all fibre channel HBAs to support this
7647 	 * property.
7648 	 *
7649 	 * set un_f_is_fiber to TRUE ( default fiber )
7650 	 */
7651 	un->un_f_is_fibre = TRUE;
7652 	switch (scsi_ifgetcap(SD_ADDRESS(un), "interconnect-type", -1)) {
7653 	case INTERCONNECT_SSA:
7654 		un->un_interconnect_type = SD_INTERCONNECT_SSA;
7655 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7656 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_SSA\n", un);
7657 		break;
7658 	case INTERCONNECT_PARALLEL:
7659 		un->un_f_is_fibre = FALSE;
7660 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7661 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7662 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_PARALLEL\n", un);
7663 		break;
7664 	case INTERCONNECT_FIBRE:
7665 		un->un_interconnect_type = SD_INTERCONNECT_FIBRE;
7666 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7667 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FIBRE\n", un);
7668 		break;
7669 	case INTERCONNECT_FABRIC:
7670 		un->un_interconnect_type = SD_INTERCONNECT_FABRIC;
7671 		un->un_node_type = DDI_NT_BLOCK_FABRIC;
7672 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7673 		    "sd_unit_attach: un:0x%p SD_INTERCONNECT_FABRIC\n", un);
7674 		break;
7675 	default:
7676 #ifdef SD_DEFAULT_INTERCONNECT_TYPE
7677 		/*
7678 		 * The HBA does not support the "interconnect-type" property
7679 		 * (or did not provide a recognized type).
7680 		 *
7681 		 * Note: This will be obsoleted when a single fibre channel
7682 		 * and parallel scsi driver is delivered. In the meantime the
7683 		 * interconnect type will be set to the platform default.If that
7684 		 * type is not parallel SCSI, it means that we should be
7685 		 * assuming "ssd" semantics. However, here this also means that
7686 		 * the FC HBA is not supporting the "interconnect-type" property
7687 		 * like we expect it to, so log this occurrence.
7688 		 */
7689 		un->un_interconnect_type = SD_DEFAULT_INTERCONNECT_TYPE;
7690 		if (!SD_IS_PARALLEL_SCSI(un)) {
7691 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7692 			    "sd_unit_attach: un:0x%p Assuming "
7693 			    "INTERCONNECT_FIBRE\n", un);
7694 		} else {
7695 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7696 			    "sd_unit_attach: un:0x%p Assuming "
7697 			    "INTERCONNECT_PARALLEL\n", un);
7698 			un->un_f_is_fibre = FALSE;
7699 		}
7700 #else
7701 		/*
7702 		 * Note: This source will be implemented when a single fibre
7703 		 * channel and parallel scsi driver is delivered. The default
7704 		 * will be to assume that if a device does not support the
7705 		 * "interconnect-type" property it is a parallel SCSI HBA and
7706 		 * we will set the interconnect type for parallel scsi.
7707 		 */
7708 		un->un_interconnect_type = SD_INTERCONNECT_PARALLEL;
7709 		un->un_f_is_fibre = FALSE;
7710 #endif
7711 		break;
7712 	}
7713 
7714 	if (un->un_f_is_fibre == TRUE) {
7715 		if (scsi_ifgetcap(SD_ADDRESS(un), "scsi-version", 1) ==
7716 			SCSI_VERSION_3) {
7717 			switch (un->un_interconnect_type) {
7718 			case SD_INTERCONNECT_FIBRE:
7719 			case SD_INTERCONNECT_SSA:
7720 				un->un_node_type = DDI_NT_BLOCK_WWN;
7721 				break;
7722 			default:
7723 				break;
7724 			}
7725 		}
7726 	}
7727 
7728 	/*
7729 	 * Initialize the Request Sense command for the target
7730 	 */
7731 	if (sd_alloc_rqs(devp, un) != DDI_SUCCESS) {
7732 		goto alloc_rqs_failed;
7733 	}
7734 
7735 	/*
7736 	 * Set un_retry_count with SD_RETRY_COUNT, this is ok for Sparc
7737 	 * with seperate binary for sd and ssd.
7738 	 *
7739 	 * x86 has 1 binary, un_retry_count is set base on connection type.
7740 	 * The hardcoded values will go away when Sparc uses 1 binary
7741 	 * for sd and ssd.  This hardcoded values need to match
7742 	 * SD_RETRY_COUNT in sddef.h
7743 	 * The value used is base on interconnect type.
7744 	 * fibre = 3, parallel = 5
7745 	 */
7746 #if defined(__i386) || defined(__amd64)
7747 	un->un_retry_count = un->un_f_is_fibre ? 3 : 5;
7748 #else
7749 	un->un_retry_count = SD_RETRY_COUNT;
7750 #endif
7751 
7752 	/*
7753 	 * Set the per disk retry count to the default number of retries
7754 	 * for disks and CDROMs. This value can be overridden by the
7755 	 * disk property list or an entry in sd.conf.
7756 	 */
7757 	un->un_notready_retry_count =
7758 	    ISCD(un) ? CD_NOT_READY_RETRY_COUNT(un)
7759 			: DISK_NOT_READY_RETRY_COUNT(un);
7760 
7761 	/*
7762 	 * Set the busy retry count to the default value of un_retry_count.
7763 	 * This can be overridden by entries in sd.conf or the device
7764 	 * config table.
7765 	 */
7766 	un->un_busy_retry_count = un->un_retry_count;
7767 
7768 	/*
7769 	 * Init the reset threshold for retries.  This number determines
7770 	 * how many retries must be performed before a reset can be issued
7771 	 * (for certain error conditions). This can be overridden by entries
7772 	 * in sd.conf or the device config table.
7773 	 */
7774 	un->un_reset_retry_count = (un->un_retry_count / 2);
7775 
7776 	/*
7777 	 * Set the victim_retry_count to the default un_retry_count
7778 	 */
7779 	un->un_victim_retry_count = (2 * un->un_retry_count);
7780 
7781 	/*
7782 	 * Set the reservation release timeout to the default value of
7783 	 * 5 seconds. This can be overridden by entries in ssd.conf or the
7784 	 * device config table.
7785 	 */
7786 	un->un_reserve_release_time = 5;
7787 
7788 	/*
7789 	 * Set up the default maximum transfer size. Note that this may
7790 	 * get updated later in the attach, when setting up default wide
7791 	 * operations for disks.
7792 	 */
7793 #if defined(__i386) || defined(__amd64)
7794 	un->un_max_xfer_size = (uint_t)SD_DEFAULT_MAX_XFER_SIZE;
7795 #else
7796 	un->un_max_xfer_size = (uint_t)maxphys;
7797 #endif
7798 
7799 	/*
7800 	 * Get "allow bus device reset" property (defaults to "enabled" if
7801 	 * the property was not defined). This is to disable bus resets for
7802 	 * certain kinds of error recovery. Note: In the future when a run-time
7803 	 * fibre check is available the soft state flag should default to
7804 	 * enabled.
7805 	 */
7806 	if (un->un_f_is_fibre == TRUE) {
7807 		un->un_f_allow_bus_device_reset = TRUE;
7808 	} else {
7809 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
7810 			"allow-bus-device-reset", 1) != 0) {
7811 			un->un_f_allow_bus_device_reset = TRUE;
7812 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7813 			"sd_unit_attach: un:0x%p Bus device reset enabled\n",
7814 				un);
7815 		} else {
7816 			un->un_f_allow_bus_device_reset = FALSE;
7817 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7818 			"sd_unit_attach: un:0x%p Bus device reset disabled\n",
7819 				un);
7820 		}
7821 	}
7822 
7823 	/*
7824 	 * Check if this is an ATAPI device. ATAPI devices use Group 1
7825 	 * Read/Write commands and Group 2 Mode Sense/Select commands.
7826 	 *
7827 	 * Note: The "obsolete" way of doing this is to check for the "atapi"
7828 	 * property. The new "variant" property with a value of "atapi" has been
7829 	 * introduced so that future 'variants' of standard SCSI behavior (like
7830 	 * atapi) could be specified by the underlying HBA drivers by supplying
7831 	 * a new value for the "variant" property, instead of having to define a
7832 	 * new property.
7833 	 */
7834 	if (ddi_prop_get_int(DDI_DEV_T_ANY, devi, 0, "atapi", -1) != -1) {
7835 		un->un_f_cfg_is_atapi = TRUE;
7836 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
7837 		    "sd_unit_attach: un:0x%p Atapi device\n", un);
7838 	}
7839 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, devi, 0, "variant",
7840 	    &variantp) == DDI_PROP_SUCCESS) {
7841 		if (strcmp(variantp, "atapi") == 0) {
7842 			un->un_f_cfg_is_atapi = TRUE;
7843 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
7844 			    "sd_unit_attach: un:0x%p Atapi device\n", un);
7845 		}
7846 		ddi_prop_free(variantp);
7847 	}
7848 
7849 	/*
7850 	 * Assume doorlock commands are supported. If not, the first
7851 	 * call to sd_send_scsi_DOORLOCK() will set to FALSE
7852 	 */
7853 	un->un_f_doorlock_supported = TRUE;
7854 
7855 	un->un_cmd_timeout	= SD_IO_TIME;
7856 
7857 	/* Info on current states, statuses, etc. (Updated frequently) */
7858 	un->un_state		= SD_STATE_NORMAL;
7859 	un->un_last_state	= SD_STATE_NORMAL;
7860 
7861 	/* Control & status info for command throttling */
7862 	un->un_throttle		= sd_max_throttle;
7863 	un->un_saved_throttle	= sd_max_throttle;
7864 	un->un_min_throttle	= sd_min_throttle;
7865 
7866 	if (un->un_f_is_fibre == TRUE) {
7867 		un->un_f_use_adaptive_throttle = TRUE;
7868 	} else {
7869 		un->un_f_use_adaptive_throttle = FALSE;
7870 	}
7871 
7872 	/* Removable media support. */
7873 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
7874 	un->un_mediastate		= DKIO_NONE;
7875 	un->un_specified_mediastate	= DKIO_NONE;
7876 
7877 	/* CVs for suspend/resume (PM or DR) */
7878 	cv_init(&un->un_suspend_cv,   NULL, CV_DRIVER, NULL);
7879 	cv_init(&un->un_disk_busy_cv, NULL, CV_DRIVER, NULL);
7880 
7881 	/* Power management support. */
7882 	un->un_power_level = SD_SPINDLE_UNINIT;
7883 
7884 	/*
7885 	 * The open/close semaphore is used to serialize threads executing
7886 	 * in the driver's open & close entry point routines for a given
7887 	 * instance.
7888 	 */
7889 	(void) sema_init(&un->un_semoclose, 1, NULL, SEMA_DRIVER, NULL);
7890 
7891 	/*
7892 	 * The conf file entry and softstate variable is a forceful override,
7893 	 * meaning a non-zero value must be entered to change the default.
7894 	 */
7895 	un->un_f_disksort_disabled = FALSE;
7896 
7897 	/*
7898 	 * Retrieve the properties from the static driver table or the driver
7899 	 * configuration file (.conf) for this unit and update the soft state
7900 	 * for the device as needed for the indicated properties.
7901 	 * Note: the property configuration needs to occur here as some of the
7902 	 * following routines may have dependancies on soft state flags set
7903 	 * as part of the driver property configuration.
7904 	 */
7905 	sd_read_unit_properties(un);
7906 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7907 	    "sd_unit_attach: un:0x%p property configuration complete.\n", un);
7908 
7909 	/*
7910 	 * By default, we mark the capacity, lbazize, and geometry
7911 	 * as invalid. Only if we successfully read a valid capacity
7912 	 * will we update the un_blockcount and un_tgt_blocksize with the
7913 	 * valid values (the geometry will be validated later).
7914 	 */
7915 	un->un_f_blockcount_is_valid	= FALSE;
7916 	un->un_f_tgt_blocksize_is_valid	= FALSE;
7917 	un->un_f_geometry_is_valid	= FALSE;
7918 
7919 	/*
7920 	 * Use DEV_BSIZE and DEV_BSHIFT as defaults, until we can determine
7921 	 * otherwise.
7922 	 */
7923 	un->un_tgt_blocksize  = un->un_sys_blocksize  = DEV_BSIZE;
7924 	un->un_blockcount = 0;
7925 
7926 	/*
7927 	 * Set up the per-instance info needed to determine the correct
7928 	 * CDBs and other info for issuing commands to the target.
7929 	 */
7930 	sd_init_cdb_limits(un);
7931 
7932 	/*
7933 	 * Set up the IO chains to use, based upon the target type.
7934 	 */
7935 	if (ISREMOVABLE(un)) {
7936 		un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA;
7937 	} else {
7938 		un->un_buf_chain_type = SD_CHAIN_INFO_DISK;
7939 	}
7940 	un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD;
7941 	un->un_direct_chain_type = SD_CHAIN_INFO_DIRECT_CMD;
7942 	un->un_priority_chain_type = SD_CHAIN_INFO_PRIORITY_CMD;
7943 
7944 	un->un_xbuf_attr = ddi_xbuf_attr_create(sizeof (struct sd_xbuf),
7945 	    sd_xbuf_strategy, un, sd_xbuf_active_limit,  sd_xbuf_reserve_limit,
7946 	    ddi_driver_major(devi), DDI_XBUF_QTHREAD_DRIVER);
7947 	ddi_xbuf_attr_register_devinfo(un->un_xbuf_attr, devi);
7948 
7949 
7950 	if (ISCD(un)) {
7951 		un->un_additional_codes = sd_additional_codes;
7952 	} else {
7953 		un->un_additional_codes = NULL;
7954 	}
7955 
7956 	/*
7957 	 * Create the kstats here so they can be available for attach-time
7958 	 * routines that send commands to the unit (either polled or via
7959 	 * sd_send_scsi_cmd).
7960 	 *
7961 	 * Note: This is a critical sequence that needs to be maintained:
7962 	 *	1) Instantiate the kstats here, before any routines using the
7963 	 *	   iopath (i.e. sd_send_scsi_cmd).
7964 	 *	2) Initialize the error stats (sd_set_errstats) and partition
7965 	 *	   stats (sd_set_pstats), following sd_validate_geometry(),
7966 	 *	   sd_register_devid(), and sd_disable_caching().
7967 	 */
7968 
7969 	un->un_stats = kstat_create(sd_label, instance,
7970 	    NULL, "disk", KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
7971 	if (un->un_stats != NULL) {
7972 		un->un_stats->ks_lock = SD_MUTEX(un);
7973 		kstat_install(un->un_stats);
7974 	}
7975 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7976 	    "sd_unit_attach: un:0x%p un_stats created\n", un);
7977 
7978 	sd_create_errstats(un, instance);
7979 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
7980 	    "sd_unit_attach: un:0x%p errstats created\n", un);
7981 
7982 	/*
7983 	 * The following if/else code was relocated here from below as part
7984 	 * of the fix for bug (4430280). However with the default setup added
7985 	 * on entry to this routine, it's no longer absolutely necessary for
7986 	 * this to be before the call to sd_spin_up_unit.
7987 	 */
7988 	if (SD_IS_PARALLEL_SCSI(un)) {
7989 		/*
7990 		 * If SCSI-2 tagged queueing is supported by the target
7991 		 * and by the host adapter then we will enable it.
7992 		 */
7993 		un->un_tagflags = 0;
7994 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) &&
7995 		    (devp->sd_inq->inq_cmdque) &&
7996 		    (un->un_f_arq_enabled == TRUE)) {
7997 			if (scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing",
7998 			    1, 1) == 1) {
7999 				un->un_tagflags = FLAG_STAG;
8000 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8001 				    "sd_unit_attach: un:0x%p tag queueing "
8002 				    "enabled\n", un);
8003 			} else if (scsi_ifgetcap(SD_ADDRESS(un),
8004 			    "untagged-qing", 0) == 1) {
8005 				un->un_f_opt_queueing = TRUE;
8006 				un->un_saved_throttle = un->un_throttle =
8007 				    min(un->un_throttle, 3);
8008 			} else {
8009 				un->un_f_opt_queueing = FALSE;
8010 				un->un_saved_throttle = un->un_throttle = 1;
8011 			}
8012 		} else if ((scsi_ifgetcap(SD_ADDRESS(un), "untagged-qing", 0)
8013 		    == 1) && (un->un_f_arq_enabled == TRUE)) {
8014 			/* The Host Adapter supports internal queueing. */
8015 			un->un_f_opt_queueing = TRUE;
8016 			un->un_saved_throttle = un->un_throttle =
8017 			    min(un->un_throttle, 3);
8018 		} else {
8019 			un->un_f_opt_queueing = FALSE;
8020 			un->un_saved_throttle = un->un_throttle = 1;
8021 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8022 			    "sd_unit_attach: un:0x%p no tag queueing\n", un);
8023 		}
8024 
8025 
8026 		/* Setup or tear down default wide operations for disks */
8027 
8028 		/*
8029 		 * Note: Legacy: it may be possible for both "sd_max_xfer_size"
8030 		 * and "ssd_max_xfer_size" to exist simultaneously on the same
8031 		 * system and be set to different values. In the future this
8032 		 * code may need to be updated when the ssd module is
8033 		 * obsoleted and removed from the system. (4299588)
8034 		 */
8035 		if ((devp->sd_inq->inq_rdf == RDF_SCSI2) &&
8036 		    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) {
8037 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
8038 			    1, 1) == 1) {
8039 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8040 				    "sd_unit_attach: un:0x%p Wide Transfer "
8041 				    "enabled\n", un);
8042 			}
8043 
8044 			/*
8045 			 * If tagged queuing has also been enabled, then
8046 			 * enable large xfers
8047 			 */
8048 			if (un->un_saved_throttle == sd_max_throttle) {
8049 				un->un_max_xfer_size =
8050 				    ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8051 				    sd_max_xfer_size, SD_MAX_XFER_SIZE);
8052 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8053 				    "sd_unit_attach: un:0x%p max transfer "
8054 				    "size=0x%x\n", un, un->un_max_xfer_size);
8055 			}
8056 		} else {
8057 			if (scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer",
8058 			    0, 1) == 1) {
8059 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8060 				    "sd_unit_attach: un:0x%p "
8061 				    "Wide Transfer disabled\n", un);
8062 			}
8063 		}
8064 	} else {
8065 		un->un_tagflags = FLAG_STAG;
8066 		un->un_max_xfer_size = ddi_getprop(DDI_DEV_T_ANY,
8067 		    devi, 0, sd_max_xfer_size, SD_MAX_XFER_SIZE);
8068 	}
8069 
8070 	/*
8071 	 * If this target supports LUN reset, try to enable it.
8072 	 */
8073 	if (un->un_f_lun_reset_enabled) {
8074 		if (scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 1, 1) == 1) {
8075 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8076 			    "un:0x%p lun_reset capability set\n", un);
8077 		} else {
8078 			SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_unit_attach: "
8079 			    "un:0x%p lun-reset capability not set\n", un);
8080 		}
8081 	}
8082 
8083 	/*
8084 	 * At this point in the attach, we have enough info in the
8085 	 * soft state to be able to issue commands to the target.
8086 	 *
8087 	 * All command paths used below MUST issue their commands as
8088 	 * SD_PATH_DIRECT. This is important as intermediate layers
8089 	 * are not all initialized yet (such as PM).
8090 	 */
8091 
8092 	/*
8093 	 * Send a TEST UNIT READY command to the device. This should clear
8094 	 * any outstanding UNIT ATTENTION that may be present.
8095 	 *
8096 	 * Note: Don't check for success, just track if there is a reservation,
8097 	 * this is a throw away command to clear any unit attentions.
8098 	 *
8099 	 * Note: This MUST be the first command issued to the target during
8100 	 * attach to ensure power on UNIT ATTENTIONS are cleared.
8101 	 * Pass in flag SD_DONT_RETRY_TUR to prevent the long delays associated
8102 	 * with attempts at spinning up a device with no media.
8103 	 */
8104 	if (sd_send_scsi_TEST_UNIT_READY(un, SD_DONT_RETRY_TUR) == EACCES) {
8105 		reservation_flag = SD_TARGET_IS_RESERVED;
8106 	}
8107 
8108 	/*
8109 	 * If the device is NOT a removable media device, attempt to spin
8110 	 * it up (using the START_STOP_UNIT command) and read its capacity
8111 	 * (using the READ CAPACITY command).  Note, however, that either
8112 	 * of these could fail and in some cases we would continue with
8113 	 * the attach despite the failure (see below).
8114 	 */
8115 	if (devp->sd_inq->inq_dtype == DTYPE_DIRECT && !ISREMOVABLE(un)) {
8116 		switch (sd_spin_up_unit(un)) {
8117 		case 0:
8118 			/*
8119 			 * Spin-up was successful; now try to read the
8120 			 * capacity.  If successful then save the results
8121 			 * and mark the capacity & lbasize as valid.
8122 			 */
8123 			SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8124 			    "sd_unit_attach: un:0x%p spin-up successful\n", un);
8125 
8126 			switch (sd_send_scsi_READ_CAPACITY(un, &capacity,
8127 			    &lbasize, SD_PATH_DIRECT)) {
8128 			case 0: {
8129 				if (capacity > DK_MAX_BLOCKS) {
8130 #ifdef _LP64
8131 					/*
8132 					 * Enable descriptor format sense data
8133 					 * so that we can get 64 bit sense
8134 					 * data fields.
8135 					 */
8136 					sd_enable_descr_sense(un);
8137 #else
8138 					/* 32-bit kernels can't handle this */
8139 					scsi_log(SD_DEVINFO(un),
8140 					    sd_label, CE_WARN,
8141 					    "disk has %llu blocks, which "
8142 					    "is too large for a 32-bit "
8143 					    "kernel", capacity);
8144 					goto spinup_failed;
8145 #endif
8146 				}
8147 				/*
8148 				 * The following relies on
8149 				 * sd_send_scsi_READ_CAPACITY never
8150 				 * returning 0 for capacity and/or lbasize.
8151 				 */
8152 				sd_update_block_info(un, lbasize, capacity);
8153 
8154 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8155 				    "sd_unit_attach: un:0x%p capacity = %ld "
8156 				    "blocks; lbasize= %ld.\n", un,
8157 				    un->un_blockcount, un->un_tgt_blocksize);
8158 
8159 				break;
8160 			}
8161 			case EACCES:
8162 				/*
8163 				 * Should never get here if the spin-up
8164 				 * succeeded, but code it in anyway.
8165 				 * From here, just continue with the attach...
8166 				 */
8167 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
8168 				    "sd_unit_attach: un:0x%p "
8169 				    "sd_send_scsi_READ_CAPACITY "
8170 				    "returned reservation conflict\n", un);
8171 				reservation_flag = SD_TARGET_IS_RESERVED;
8172 				break;
8173 			default:
8174 				/*
8175 				 * Likewise, should never get here if the
8176 				 * spin-up succeeded. Just continue with
8177 				 * the attach...
8178 				 */
8179 				break;
8180 			}
8181 			break;
8182 		case EACCES:
8183 			/*
8184 			 * Device is reserved by another host.  In this case
8185 			 * we could not spin it up or read the capacity, but
8186 			 * we continue with the attach anyway.
8187 			 */
8188 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8189 			    "sd_unit_attach: un:0x%p spin-up reservation "
8190 			    "conflict.\n", un);
8191 			reservation_flag = SD_TARGET_IS_RESERVED;
8192 			break;
8193 		default:
8194 			/* Fail the attach if the spin-up failed. */
8195 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
8196 			    "sd_unit_attach: un:0x%p spin-up failed.", un);
8197 			goto spinup_failed;
8198 		}
8199 	}
8200 
8201 	/*
8202 	 * Check to see if this is a MMC drive
8203 	 */
8204 	if (ISCD(un)) {
8205 		sd_set_mmc_caps(un);
8206 	}
8207 
8208 	/*
8209 	 * Create the minor nodes for the device.
8210 	 * Note: If we want to support fdisk on both sparc and intel, this will
8211 	 * have to separate out the notion that VTOC8 is always sparc, and
8212 	 * VTOC16 is always intel (tho these can be the defaults).  The vtoc
8213 	 * type will have to be determined at run-time, and the fdisk
8214 	 * partitioning will have to have been read & set up before we
8215 	 * create the minor nodes. (any other inits (such as kstats) that
8216 	 * also ought to be done before creating the minor nodes?) (Doesn't
8217 	 * setting up the minor nodes kind of imply that we're ready to
8218 	 * handle an open from userland?)
8219 	 */
8220 	if (sd_create_minor_nodes(un, devi) != DDI_SUCCESS) {
8221 		goto create_minor_nodes_failed;
8222 	}
8223 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8224 	    "sd_unit_attach: un:0x%p minor nodes created\n", un);
8225 
8226 	/*
8227 	 * Add a zero-length attribute to tell the world we support
8228 	 * kernel ioctls (for layered drivers)
8229 	 */
8230 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8231 	    DDI_KERNEL_IOCTL, NULL, 0);
8232 
8233 	/*
8234 	 * Add a boolean property to tell the world we support
8235 	 * the B_FAILFAST flag (for layered drivers)
8236 	 */
8237 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
8238 	    "ddi-failfast-supported", NULL, 0);
8239 
8240 	/*
8241 	 * Initialize power management
8242 	 */
8243 	mutex_init(&un->un_pm_mutex, NULL, MUTEX_DRIVER, NULL);
8244 	cv_init(&un->un_pm_busy_cv, NULL, CV_DRIVER, NULL);
8245 	sd_setup_pm(un, devi);
8246 	if (un->un_f_pm_is_enabled == FALSE) {
8247 		/*
8248 		 * For performance, point to a jump table that does
8249 		 * not include pm.
8250 		 * The direct and priority chains don't change with PM.
8251 		 *
8252 		 * Note: this is currently done based on individual device
8253 		 * capabilities. When an interface for determining system
8254 		 * power enabled state becomes available, or when additional
8255 		 * layers are added to the command chain, these values will
8256 		 * have to be re-evaluated for correctness.
8257 		 */
8258 		if (ISREMOVABLE(un)) {
8259 			un->un_buf_chain_type = SD_CHAIN_INFO_RMMEDIA_NO_PM;
8260 		} else {
8261 			un->un_buf_chain_type = SD_CHAIN_INFO_DISK_NO_PM;
8262 		}
8263 		un->un_uscsi_chain_type  = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
8264 	}
8265 
8266 	/*
8267 	 * This property is set to 0 by HA software to avoid retries
8268 	 * on a reserved disk. (The preferred property name is
8269 	 * "retry-on-reservation-conflict") (1189689)
8270 	 *
8271 	 * Note: The use of a global here can have unintended consequences. A
8272 	 * per instance variable is preferrable to match the capabilities of
8273 	 * different underlying hba's (4402600)
8274 	 */
8275 	sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY, devi,
8276 	    DDI_PROP_DONTPASS, "retry-on-reservation-conflict",
8277 	    sd_retry_on_reservation_conflict);
8278 	if (sd_retry_on_reservation_conflict != 0) {
8279 		sd_retry_on_reservation_conflict = ddi_getprop(DDI_DEV_T_ANY,
8280 		    devi, DDI_PROP_DONTPASS, sd_resv_conflict_name,
8281 		    sd_retry_on_reservation_conflict);
8282 	}
8283 
8284 	/* Set up options for QFULL handling. */
8285 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8286 	    "qfull-retries", -1)) != -1) {
8287 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retries",
8288 		    rval, 1);
8289 	}
8290 	if ((rval = ddi_getprop(DDI_DEV_T_ANY, devi, 0,
8291 	    "qfull-retry-interval", -1)) != -1) {
8292 		(void) scsi_ifsetcap(SD_ADDRESS(un), "qfull-retry-interval",
8293 		    rval, 1);
8294 	}
8295 
8296 	/*
8297 	 * This just prints a message that announces the existence of the
8298 	 * device. The message is always printed in the system logfile, but
8299 	 * only appears on the console if the system is booted with the
8300 	 * -v (verbose) argument.
8301 	 */
8302 	ddi_report_dev(devi);
8303 
8304 	/*
8305 	 * The framework calls driver attach routines single-threaded
8306 	 * for a given instance.  However we still acquire SD_MUTEX here
8307 	 * because this required for calling the sd_validate_geometry()
8308 	 * and sd_register_devid() functions.
8309 	 */
8310 	mutex_enter(SD_MUTEX(un));
8311 	un->un_f_geometry_is_valid = FALSE;
8312 	un->un_mediastate = DKIO_NONE;
8313 	un->un_reserved = -1;
8314 	if (!ISREMOVABLE(un)) {
8315 		/*
8316 		 * Read and validate the device's geometry (ie, disk label)
8317 		 * A new unformatted drive will not have a valid geometry, but
8318 		 * the driver needs to successfully attach to this device so
8319 		 * the drive can be formatted via ioctls.
8320 		 */
8321 		if (((sd_validate_geometry(un, SD_PATH_DIRECT) ==
8322 		    ENOTSUP)) &&
8323 		    (un->un_blockcount < DK_MAX_BLOCKS)) {
8324 			/*
8325 			 * We found a small disk with an EFI label on it;
8326 			 * we need to fix up the minor nodes accordingly.
8327 			 */
8328 			ddi_remove_minor_node(devi, "h");
8329 			ddi_remove_minor_node(devi, "h,raw");
8330 			(void) ddi_create_minor_node(devi, "wd",
8331 			    S_IFBLK,
8332 			    (instance << SDUNIT_SHIFT) | WD_NODE,
8333 			    un->un_node_type, NULL);
8334 			(void) ddi_create_minor_node(devi, "wd,raw",
8335 			    S_IFCHR,
8336 			    (instance << SDUNIT_SHIFT) | WD_NODE,
8337 			    un->un_node_type, NULL);
8338 		}
8339 	}
8340 
8341 	/*
8342 	 * Read and initialize the devid for the unit.
8343 	 */
8344 	ASSERT(un->un_errstats != NULL);
8345 	if (!ISREMOVABLE(un)) {
8346 		sd_register_devid(un, devi, reservation_flag);
8347 	}
8348 	mutex_exit(SD_MUTEX(un));
8349 
8350 #if (defined(__fibre))
8351 	/*
8352 	 * Register callbacks for fibre only.  You can't do this soley
8353 	 * on the basis of the devid_type because this is hba specific.
8354 	 * We need to query our hba capabilities to find out whether to
8355 	 * register or not.
8356 	 */
8357 	if (un->un_f_is_fibre) {
8358 	    if (strcmp(un->un_node_type, DDI_NT_BLOCK_CHAN)) {
8359 		sd_init_event_callbacks(un);
8360 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8361 		    "sd_unit_attach: un:0x%p event callbacks inserted", un);
8362 	    }
8363 	}
8364 #endif
8365 
8366 	if (un->un_f_opt_disable_cache == TRUE) {
8367 		if (sd_disable_caching(un) != 0) {
8368 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8369 			    "sd_unit_attach: un:0x%p Could not disable "
8370 			    "caching", un);
8371 			goto devid_failed;
8372 		}
8373 	}
8374 
8375 	/*
8376 	 * Set the pstat and error stat values here, so data obtained during the
8377 	 * previous attach-time routines is available.
8378 	 *
8379 	 * Note: This is a critical sequence that needs to be maintained:
8380 	 *	1) Instantiate the kstats before any routines using the iopath
8381 	 *	   (i.e. sd_send_scsi_cmd).
8382 	 *	2) Initialize the error stats (sd_set_errstats) and partition
8383 	 *	   stats (sd_set_pstats)here, following sd_validate_geometry(),
8384 	 *	   sd_register_devid(), and sd_disable_caching().
8385 	 */
8386 	if (!ISREMOVABLE(un) && (un->un_f_pkstats_enabled == TRUE)) {
8387 		sd_set_pstats(un);
8388 		SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8389 		    "sd_unit_attach: un:0x%p pstats created and set\n", un);
8390 	}
8391 
8392 	sd_set_errstats(un);
8393 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8394 	    "sd_unit_attach: un:0x%p errstats set\n", un);
8395 
8396 	/*
8397 	 * Find out what type of reservation this disk supports.
8398 	 */
8399 	switch (sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS, 0, NULL)) {
8400 	case 0:
8401 		/*
8402 		 * SCSI-3 reservations are supported.
8403 		 */
8404 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8405 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8406 		    "sd_unit_attach: un:0x%p SCSI-3 reservations\n", un);
8407 		break;
8408 	case ENOTSUP:
8409 		/*
8410 		 * The PERSISTENT RESERVE IN command would not be recognized by
8411 		 * a SCSI-2 device, so assume the reservation type is SCSI-2.
8412 		 */
8413 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8414 		    "sd_unit_attach: un:0x%p SCSI-2 reservations\n", un);
8415 		un->un_reservation_type = SD_SCSI2_RESERVATION;
8416 		break;
8417 	default:
8418 		/*
8419 		 * default to SCSI-3 reservations
8420 		 */
8421 		SD_INFO(SD_LOG_ATTACH_DETACH, un,
8422 		    "sd_unit_attach: un:0x%p default SCSI3 reservations\n", un);
8423 		un->un_reservation_type = SD_SCSI3_RESERVATION;
8424 		break;
8425 	}
8426 
8427 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
8428 	    "sd_unit_attach: un:0x%p exit success\n", un);
8429 
8430 	return (DDI_SUCCESS);
8431 
8432 	/*
8433 	 * An error occurred during the attach; clean up & return failure.
8434 	 */
8435 
8436 devid_failed:
8437 
8438 setup_pm_failed:
8439 	ddi_remove_minor_node(devi, NULL);
8440 
8441 create_minor_nodes_failed:
8442 	/*
8443 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8444 	 */
8445 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8446 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8447 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8448 
8449 	if (un->un_f_is_fibre == FALSE) {
8450 	    (void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8451 	}
8452 
8453 spinup_failed:
8454 
8455 	mutex_enter(SD_MUTEX(un));
8456 
8457 	/* Cancel callback for SD_PATH_DIRECT_PRIORITY cmd. restart */
8458 	if (un->un_direct_priority_timeid != NULL) {
8459 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8460 		un->un_direct_priority_timeid = NULL;
8461 		mutex_exit(SD_MUTEX(un));
8462 		(void) untimeout(temp_id);
8463 		mutex_enter(SD_MUTEX(un));
8464 	}
8465 
8466 	/* Cancel any pending start/stop timeouts */
8467 	if (un->un_startstop_timeid != NULL) {
8468 		timeout_id_t temp_id = un->un_startstop_timeid;
8469 		un->un_startstop_timeid = NULL;
8470 		mutex_exit(SD_MUTEX(un));
8471 		(void) untimeout(temp_id);
8472 		mutex_enter(SD_MUTEX(un));
8473 	}
8474 
8475 	mutex_exit(SD_MUTEX(un));
8476 
8477 	/* There should not be any in-progress I/O so ASSERT this check */
8478 	ASSERT(un->un_ncmds_in_transport == 0);
8479 	ASSERT(un->un_ncmds_in_driver == 0);
8480 
8481 	/* Do not free the softstate if the callback routine is active */
8482 	sd_sync_with_callback(un);
8483 
8484 	/*
8485 	 * Partition stats apparently are not used with removables. These would
8486 	 * not have been created during attach, so no need to clean them up...
8487 	 */
8488 	if (un->un_stats != NULL) {
8489 		kstat_delete(un->un_stats);
8490 		un->un_stats = NULL;
8491 	}
8492 	if (un->un_errstats != NULL) {
8493 		kstat_delete(un->un_errstats);
8494 		un->un_errstats = NULL;
8495 	}
8496 
8497 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8498 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8499 
8500 	ddi_prop_remove_all(devi);
8501 	sema_destroy(&un->un_semoclose);
8502 	cv_destroy(&un->un_state_cv);
8503 
8504 getrbuf_failed:
8505 
8506 	sd_free_rqs(un);
8507 
8508 alloc_rqs_failed:
8509 
8510 	devp->sd_private = NULL;
8511 	bzero(un, sizeof (struct sd_lun));	/* Clear any stale data! */
8512 
8513 get_softstate_failed:
8514 	/*
8515 	 * Note: the man pages are unclear as to whether or not doing a
8516 	 * ddi_soft_state_free(sd_state, instance) is the right way to
8517 	 * clean up after the ddi_soft_state_zalloc() if the subsequent
8518 	 * ddi_get_soft_state() fails.  The implication seems to be
8519 	 * that the get_soft_state cannot fail if the zalloc succeeds.
8520 	 */
8521 	ddi_soft_state_free(sd_state, instance);
8522 
8523 probe_failed:
8524 	scsi_unprobe(devp);
8525 #ifdef SDDEBUG
8526 	if ((sd_component_mask & SD_LOG_ATTACH_DETACH) &&
8527 	    (sd_level_mask & SD_LOGMASK_TRACE)) {
8528 		cmn_err(CE_CONT, "sd_unit_attach: un:0x%p exit failure\n",
8529 		    (void *)un);
8530 	}
8531 #endif
8532 	return (DDI_FAILURE);
8533 }
8534 
8535 
8536 /*
8537  *    Function: sd_unit_detach
8538  *
8539  * Description: Performs DDI_DETACH processing for sddetach().
8540  *
8541  * Return Code: DDI_SUCCESS
8542  *		DDI_FAILURE
8543  *
8544  *     Context: Kernel thread context
8545  */
8546 
8547 static int
8548 sd_unit_detach(dev_info_t *devi)
8549 {
8550 	struct scsi_device	*devp;
8551 	struct sd_lun		*un;
8552 	int			i;
8553 	dev_t			dev;
8554 #if !(defined(__i386) || defined(__amd64)) && !defined(__fibre)
8555 	int			reset_retval;
8556 #endif
8557 	int			instance = ddi_get_instance(devi);
8558 
8559 	mutex_enter(&sd_detach_mutex);
8560 
8561 	/*
8562 	 * Fail the detach for any of the following:
8563 	 *  - Unable to get the sd_lun struct for the instance
8564 	 *  - A layered driver has an outstanding open on the instance
8565 	 *  - Another thread is already detaching this instance
8566 	 *  - Another thread is currently performing an open
8567 	 */
8568 	devp = ddi_get_driver_private(devi);
8569 	if ((devp == NULL) ||
8570 	    ((un = (struct sd_lun *)devp->sd_private) == NULL) ||
8571 	    (un->un_ncmds_in_driver != 0) || (un->un_layer_count != 0) ||
8572 	    (un->un_detach_count != 0) || (un->un_opens_in_progress != 0)) {
8573 		mutex_exit(&sd_detach_mutex);
8574 		return (DDI_FAILURE);
8575 	}
8576 
8577 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: entry 0x%p\n", un);
8578 
8579 	/*
8580 	 * Mark this instance as currently in a detach, to inhibit any
8581 	 * opens from a layered driver.
8582 	 */
8583 	un->un_detach_count++;
8584 	mutex_exit(&sd_detach_mutex);
8585 
8586 	dev = sd_make_device(SD_DEVINFO(un));
8587 
8588 	_NOTE(COMPETING_THREADS_NOW);
8589 
8590 	mutex_enter(SD_MUTEX(un));
8591 
8592 	/*
8593 	 * Fail the detach if there are any outstanding layered
8594 	 * opens on this device.
8595 	 */
8596 	for (i = 0; i < NDKMAP; i++) {
8597 		if (un->un_ocmap.lyropen[i] != 0) {
8598 			goto err_notclosed;
8599 		}
8600 	}
8601 
8602 	/*
8603 	 * Verify there are NO outstanding commands issued to this device.
8604 	 * ie, un_ncmds_in_transport == 0.
8605 	 * It's possible to have outstanding commands through the physio
8606 	 * code path, even though everything's closed.
8607 	 */
8608 	if ((un->un_ncmds_in_transport != 0) || (un->un_retry_timeid != NULL) ||
8609 	    (un->un_direct_priority_timeid != NULL) ||
8610 	    (un->un_state == SD_STATE_RWAIT)) {
8611 		mutex_exit(SD_MUTEX(un));
8612 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8613 		    "sd_dr_detach: Detach failure due to outstanding cmds\n");
8614 		goto err_stillbusy;
8615 	}
8616 
8617 	/*
8618 	 * If we have the device reserved, release the reservation.
8619 	 */
8620 	if ((un->un_resvd_status & SD_RESERVE) &&
8621 	    !(un->un_resvd_status & SD_LOST_RESERVE)) {
8622 		mutex_exit(SD_MUTEX(un));
8623 		/*
8624 		 * Note: sd_reserve_release sends a command to the device
8625 		 * via the sd_ioctlcmd() path, and can sleep.
8626 		 */
8627 		if (sd_reserve_release(dev, SD_RELEASE) != 0) {
8628 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8629 			    "sd_dr_detach: Cannot release reservation \n");
8630 		}
8631 	} else {
8632 		mutex_exit(SD_MUTEX(un));
8633 	}
8634 
8635 	/*
8636 	 * Untimeout any reserve recover, throttle reset, restart unit
8637 	 * and delayed broadcast timeout threads. Protect the timeout pointer
8638 	 * from getting nulled by their callback functions.
8639 	 */
8640 	mutex_enter(SD_MUTEX(un));
8641 	if (un->un_resvd_timeid != NULL) {
8642 		timeout_id_t temp_id = un->un_resvd_timeid;
8643 		un->un_resvd_timeid = NULL;
8644 		mutex_exit(SD_MUTEX(un));
8645 		(void) untimeout(temp_id);
8646 		mutex_enter(SD_MUTEX(un));
8647 	}
8648 
8649 	if (un->un_reset_throttle_timeid != NULL) {
8650 		timeout_id_t temp_id = un->un_reset_throttle_timeid;
8651 		un->un_reset_throttle_timeid = NULL;
8652 		mutex_exit(SD_MUTEX(un));
8653 		(void) untimeout(temp_id);
8654 		mutex_enter(SD_MUTEX(un));
8655 	}
8656 
8657 	if (un->un_startstop_timeid != NULL) {
8658 		timeout_id_t temp_id = un->un_startstop_timeid;
8659 		un->un_startstop_timeid = NULL;
8660 		mutex_exit(SD_MUTEX(un));
8661 		(void) untimeout(temp_id);
8662 		mutex_enter(SD_MUTEX(un));
8663 	}
8664 
8665 	if (un->un_dcvb_timeid != NULL) {
8666 		timeout_id_t temp_id = un->un_dcvb_timeid;
8667 		un->un_dcvb_timeid = NULL;
8668 		mutex_exit(SD_MUTEX(un));
8669 		(void) untimeout(temp_id);
8670 	} else {
8671 		mutex_exit(SD_MUTEX(un));
8672 	}
8673 
8674 	/* Remove any pending reservation reclaim requests for this device */
8675 	sd_rmv_resv_reclaim_req(dev);
8676 
8677 	mutex_enter(SD_MUTEX(un));
8678 
8679 	/* Cancel any pending callbacks for SD_PATH_DIRECT_PRIORITY cmd. */
8680 	if (un->un_direct_priority_timeid != NULL) {
8681 		timeout_id_t temp_id = un->un_direct_priority_timeid;
8682 		un->un_direct_priority_timeid = NULL;
8683 		mutex_exit(SD_MUTEX(un));
8684 		(void) untimeout(temp_id);
8685 		mutex_enter(SD_MUTEX(un));
8686 	}
8687 
8688 	/* Cancel any active multi-host disk watch thread requests */
8689 	if (un->un_mhd_token != NULL) {
8690 		mutex_exit(SD_MUTEX(un));
8691 		 _NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_mhd_token));
8692 		if (scsi_watch_request_terminate(un->un_mhd_token,
8693 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8694 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8695 			    "sd_dr_detach: Cannot cancel mhd watch request\n");
8696 			/*
8697 			 * Note: We are returning here after having removed
8698 			 * some driver timeouts above. This is consistent with
8699 			 * the legacy implementation but perhaps the watch
8700 			 * terminate call should be made with the wait flag set.
8701 			 */
8702 			goto err_stillbusy;
8703 		}
8704 		mutex_enter(SD_MUTEX(un));
8705 		un->un_mhd_token = NULL;
8706 	}
8707 
8708 	if (un->un_swr_token != NULL) {
8709 		mutex_exit(SD_MUTEX(un));
8710 		_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_swr_token));
8711 		if (scsi_watch_request_terminate(un->un_swr_token,
8712 		    SCSI_WATCH_TERMINATE_NOWAIT)) {
8713 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8714 			    "sd_dr_detach: Cannot cancel swr watch request\n");
8715 			/*
8716 			 * Note: We are returning here after having removed
8717 			 * some driver timeouts above. This is consistent with
8718 			 * the legacy implementation but perhaps the watch
8719 			 * terminate call should be made with the wait flag set.
8720 			 */
8721 			goto err_stillbusy;
8722 		}
8723 		mutex_enter(SD_MUTEX(un));
8724 		un->un_swr_token = NULL;
8725 	}
8726 
8727 	mutex_exit(SD_MUTEX(un));
8728 
8729 	/*
8730 	 * Clear any scsi_reset_notifies. We clear the reset notifies
8731 	 * if we have not registered one.
8732 	 * Note: The sd_mhd_reset_notify_cb() fn tries to acquire SD_MUTEX!
8733 	 */
8734 	(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
8735 	    sd_mhd_reset_notify_cb, (caddr_t)un);
8736 
8737 
8738 
8739 #if defined(__i386) || defined(__amd64)
8740 	/*
8741 	 * Gratuitous bus resets sometimes cause an otherwise
8742 	 * okay ATA/ATAPI bus to hang. This is due the lack of
8743 	 * a clear spec of how resets should be implemented by ATA
8744 	 * disk drives.
8745 	 */
8746 #elif !defined(__fibre)		/* "#else if" does NOT work! */
8747 	/*
8748 	 * Reset target/bus.
8749 	 *
8750 	 * Note: This is a legacy workaround for Elite III dual-port drives that
8751 	 * will not come online after an aborted detach and subsequent re-attach
8752 	 * It should be removed when the Elite III FW is fixed, or the drives
8753 	 * are no longer supported.
8754 	 */
8755 	if (un->un_f_cfg_is_atapi == FALSE) {
8756 		reset_retval = 0;
8757 
8758 		/* If the device is in low power mode don't reset it */
8759 
8760 		mutex_enter(&un->un_pm_mutex);
8761 		if (!SD_DEVICE_IS_IN_LOW_POWER(un)) {
8762 			/*
8763 			 * First try a LUN reset if we can, then move on to a
8764 			 * target reset if needed; swat the bus as a last
8765 			 * resort.
8766 			 */
8767 			mutex_exit(&un->un_pm_mutex);
8768 			if (un->un_f_allow_bus_device_reset == TRUE) {
8769 				if (un->un_f_lun_reset_enabled == TRUE) {
8770 					reset_retval =
8771 					    scsi_reset(SD_ADDRESS(un),
8772 					    RESET_LUN);
8773 				}
8774 				if (reset_retval == 0) {
8775 					reset_retval =
8776 					    scsi_reset(SD_ADDRESS(un),
8777 					    RESET_TARGET);
8778 				}
8779 			}
8780 			if (reset_retval == 0) {
8781 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
8782 			}
8783 		} else {
8784 			mutex_exit(&un->un_pm_mutex);
8785 		}
8786 	}
8787 #endif
8788 
8789 	/*
8790 	 * protect the timeout pointers from getting nulled by
8791 	 * their callback functions during the cancellation process.
8792 	 * In such a scenario untimeout can be invoked with a null value.
8793 	 */
8794 	_NOTE(NO_COMPETING_THREADS_NOW);
8795 
8796 	mutex_enter(&un->un_pm_mutex);
8797 	if (un->un_pm_idle_timeid != NULL) {
8798 		timeout_id_t temp_id = un->un_pm_idle_timeid;
8799 		un->un_pm_idle_timeid = NULL;
8800 		mutex_exit(&un->un_pm_mutex);
8801 
8802 		/*
8803 		 * Timeout is active; cancel it.
8804 		 * Note that it'll never be active on a device
8805 		 * that does not support PM therefore we don't
8806 		 * have to check before calling pm_idle_component.
8807 		 */
8808 		(void) untimeout(temp_id);
8809 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8810 		mutex_enter(&un->un_pm_mutex);
8811 	}
8812 
8813 	/*
8814 	 * Check whether there is already a timeout scheduled for power
8815 	 * management. If yes then don't lower the power here, that's.
8816 	 * the timeout handler's job.
8817 	 */
8818 	if (un->un_pm_timeid != NULL) {
8819 		timeout_id_t temp_id = un->un_pm_timeid;
8820 		un->un_pm_timeid = NULL;
8821 		mutex_exit(&un->un_pm_mutex);
8822 		/*
8823 		 * Timeout is active; cancel it.
8824 		 * Note that it'll never be active on a device
8825 		 * that does not support PM therefore we don't
8826 		 * have to check before calling pm_idle_component.
8827 		 */
8828 		(void) untimeout(temp_id);
8829 		(void) pm_idle_component(SD_DEVINFO(un), 0);
8830 
8831 	} else {
8832 		mutex_exit(&un->un_pm_mutex);
8833 		if ((un->un_f_pm_is_enabled == TRUE) &&
8834 		    (pm_lower_power(SD_DEVINFO(un), 0, SD_SPINDLE_OFF) !=
8835 		    DDI_SUCCESS)) {
8836 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8837 		    "sd_dr_detach: Lower power request failed, ignoring.\n");
8838 			/*
8839 			 * Fix for bug: 4297749, item # 13
8840 			 * The above test now includes a check to see if PM is
8841 			 * supported by this device before call
8842 			 * pm_lower_power().
8843 			 * Note, the following is not dead code. The call to
8844 			 * pm_lower_power above will generate a call back into
8845 			 * our sdpower routine which might result in a timeout
8846 			 * handler getting activated. Therefore the following
8847 			 * code is valid and necessary.
8848 			 */
8849 			mutex_enter(&un->un_pm_mutex);
8850 			if (un->un_pm_timeid != NULL) {
8851 				timeout_id_t temp_id = un->un_pm_timeid;
8852 				un->un_pm_timeid = NULL;
8853 				mutex_exit(&un->un_pm_mutex);
8854 				(void) untimeout(temp_id);
8855 				(void) pm_idle_component(SD_DEVINFO(un), 0);
8856 			} else {
8857 				mutex_exit(&un->un_pm_mutex);
8858 			}
8859 		}
8860 	}
8861 
8862 	/*
8863 	 * Cleanup from the scsi_ifsetcap() calls (437868)
8864 	 * Relocated here from above to be after the call to
8865 	 * pm_lower_power, which was getting errors.
8866 	 */
8867 	(void) scsi_ifsetcap(SD_ADDRESS(un), "lun-reset", 0, 1);
8868 	(void) scsi_ifsetcap(SD_ADDRESS(un), "wide-xfer", 0, 1);
8869 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
8870 
8871 	if (un->un_f_is_fibre == FALSE) {
8872 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 0, 1);
8873 	}
8874 
8875 	/*
8876 	 * Remove any event callbacks, fibre only
8877 	 */
8878 	if (un->un_f_is_fibre == TRUE) {
8879 		if ((un->un_insert_event != NULL) &&
8880 			(ddi_remove_event_handler(un->un_insert_cb_id) !=
8881 				DDI_SUCCESS)) {
8882 			/*
8883 			 * Note: We are returning here after having done
8884 			 * substantial cleanup above. This is consistent
8885 			 * with the legacy implementation but this may not
8886 			 * be the right thing to do.
8887 			 */
8888 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8889 				"sd_dr_detach: Cannot cancel insert event\n");
8890 			goto err_remove_event;
8891 		}
8892 		un->un_insert_event = NULL;
8893 
8894 		if ((un->un_remove_event != NULL) &&
8895 			(ddi_remove_event_handler(un->un_remove_cb_id) !=
8896 				DDI_SUCCESS)) {
8897 			/*
8898 			 * Note: We are returning here after having done
8899 			 * substantial cleanup above. This is consistent
8900 			 * with the legacy implementation but this may not
8901 			 * be the right thing to do.
8902 			 */
8903 			SD_ERROR(SD_LOG_ATTACH_DETACH, un,
8904 				"sd_dr_detach: Cannot cancel remove event\n");
8905 			goto err_remove_event;
8906 		}
8907 		un->un_remove_event = NULL;
8908 	}
8909 
8910 	/* Do not free the softstate if the callback routine is active */
8911 	sd_sync_with_callback(un);
8912 
8913 	/*
8914 	 * Hold the detach mutex here, to make sure that no other threads ever
8915 	 * can access a (partially) freed soft state structure.
8916 	 */
8917 	mutex_enter(&sd_detach_mutex);
8918 
8919 	/*
8920 	 * Clean up the soft state struct.
8921 	 * Cleanup is done in reverse order of allocs/inits.
8922 	 * At this point there should be no competing threads anymore.
8923 	 */
8924 
8925 	/* Unregister and free device id. */
8926 	ddi_devid_unregister(devi);
8927 	if (un->un_devid) {
8928 		ddi_devid_free(un->un_devid);
8929 		un->un_devid = NULL;
8930 	}
8931 
8932 	/*
8933 	 * Destroy wmap cache if it exists.
8934 	 */
8935 	if (un->un_wm_cache != NULL) {
8936 		kmem_cache_destroy(un->un_wm_cache);
8937 		un->un_wm_cache = NULL;
8938 	}
8939 
8940 	/* Remove minor nodes */
8941 	ddi_remove_minor_node(devi, NULL);
8942 
8943 	/*
8944 	 * kstat cleanup is done in detach for all device types (4363169).
8945 	 * We do not want to fail detach if the device kstats are not deleted
8946 	 * since there is a confusion about the devo_refcnt for the device.
8947 	 * We just delete the kstats and let detach complete successfully.
8948 	 */
8949 	if (un->un_stats != NULL) {
8950 		kstat_delete(un->un_stats);
8951 		un->un_stats = NULL;
8952 	}
8953 	if (un->un_errstats != NULL) {
8954 		kstat_delete(un->un_errstats);
8955 		un->un_errstats = NULL;
8956 	}
8957 
8958 	/* Remove partition stats (not created for removables) */
8959 	if (!ISREMOVABLE(un)) {
8960 		for (i = 0; i < NSDMAP; i++) {
8961 			if (un->un_pstats[i] != NULL) {
8962 				kstat_delete(un->un_pstats[i]);
8963 				un->un_pstats[i] = NULL;
8964 			}
8965 		}
8966 	}
8967 
8968 	/* Remove xbuf registration */
8969 	ddi_xbuf_attr_unregister_devinfo(un->un_xbuf_attr, devi);
8970 	ddi_xbuf_attr_destroy(un->un_xbuf_attr);
8971 
8972 	/* Remove driver properties */
8973 	ddi_prop_remove_all(devi);
8974 
8975 	mutex_destroy(&un->un_pm_mutex);
8976 	cv_destroy(&un->un_pm_busy_cv);
8977 
8978 	/* Open/close semaphore */
8979 	sema_destroy(&un->un_semoclose);
8980 
8981 	/* Removable media condvar. */
8982 	cv_destroy(&un->un_state_cv);
8983 
8984 	/* Suspend/resume condvar. */
8985 	cv_destroy(&un->un_suspend_cv);
8986 	cv_destroy(&un->un_disk_busy_cv);
8987 
8988 	sd_free_rqs(un);
8989 
8990 	/* Free up soft state */
8991 	devp->sd_private = NULL;
8992 	bzero(un, sizeof (struct sd_lun));
8993 	ddi_soft_state_free(sd_state, instance);
8994 
8995 	mutex_exit(&sd_detach_mutex);
8996 
8997 	/* This frees up the INQUIRY data associated with the device. */
8998 	scsi_unprobe(devp);
8999 
9000 	return (DDI_SUCCESS);
9001 
9002 err_notclosed:
9003 	mutex_exit(SD_MUTEX(un));
9004 
9005 err_stillbusy:
9006 	_NOTE(NO_COMPETING_THREADS_NOW);
9007 
9008 err_remove_event:
9009 	mutex_enter(&sd_detach_mutex);
9010 	un->un_detach_count--;
9011 	mutex_exit(&sd_detach_mutex);
9012 
9013 	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_unit_detach: exit failure\n");
9014 	return (DDI_FAILURE);
9015 }
9016 
9017 
9018 /*
9019  * Driver minor node structure and data table
9020  */
9021 struct driver_minor_data {
9022 	char	*name;
9023 	minor_t	minor;
9024 	int	type;
9025 };
9026 
9027 static struct driver_minor_data sd_minor_data[] = {
9028 	{"a", 0, S_IFBLK},
9029 	{"b", 1, S_IFBLK},
9030 	{"c", 2, S_IFBLK},
9031 	{"d", 3, S_IFBLK},
9032 	{"e", 4, S_IFBLK},
9033 	{"f", 5, S_IFBLK},
9034 	{"g", 6, S_IFBLK},
9035 	{"h", 7, S_IFBLK},
9036 #if defined(_SUNOS_VTOC_16)
9037 	{"i", 8, S_IFBLK},
9038 	{"j", 9, S_IFBLK},
9039 	{"k", 10, S_IFBLK},
9040 	{"l", 11, S_IFBLK},
9041 	{"m", 12, S_IFBLK},
9042 	{"n", 13, S_IFBLK},
9043 	{"o", 14, S_IFBLK},
9044 	{"p", 15, S_IFBLK},
9045 #endif			/* defined(_SUNOS_VTOC_16) */
9046 #if defined(_FIRMWARE_NEEDS_FDISK)
9047 	{"q", 16, S_IFBLK},
9048 	{"r", 17, S_IFBLK},
9049 	{"s", 18, S_IFBLK},
9050 	{"t", 19, S_IFBLK},
9051 	{"u", 20, S_IFBLK},
9052 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9053 	{"a,raw", 0, S_IFCHR},
9054 	{"b,raw", 1, S_IFCHR},
9055 	{"c,raw", 2, S_IFCHR},
9056 	{"d,raw", 3, S_IFCHR},
9057 	{"e,raw", 4, S_IFCHR},
9058 	{"f,raw", 5, S_IFCHR},
9059 	{"g,raw", 6, S_IFCHR},
9060 	{"h,raw", 7, S_IFCHR},
9061 #if defined(_SUNOS_VTOC_16)
9062 	{"i,raw", 8, S_IFCHR},
9063 	{"j,raw", 9, S_IFCHR},
9064 	{"k,raw", 10, S_IFCHR},
9065 	{"l,raw", 11, S_IFCHR},
9066 	{"m,raw", 12, S_IFCHR},
9067 	{"n,raw", 13, S_IFCHR},
9068 	{"o,raw", 14, S_IFCHR},
9069 	{"p,raw", 15, S_IFCHR},
9070 #endif			/* defined(_SUNOS_VTOC_16) */
9071 #if defined(_FIRMWARE_NEEDS_FDISK)
9072 	{"q,raw", 16, S_IFCHR},
9073 	{"r,raw", 17, S_IFCHR},
9074 	{"s,raw", 18, S_IFCHR},
9075 	{"t,raw", 19, S_IFCHR},
9076 	{"u,raw", 20, S_IFCHR},
9077 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9078 	{0}
9079 };
9080 
9081 static struct driver_minor_data sd_minor_data_efi[] = {
9082 	{"a", 0, S_IFBLK},
9083 	{"b", 1, S_IFBLK},
9084 	{"c", 2, S_IFBLK},
9085 	{"d", 3, S_IFBLK},
9086 	{"e", 4, S_IFBLK},
9087 	{"f", 5, S_IFBLK},
9088 	{"g", 6, S_IFBLK},
9089 	{"wd", 7, S_IFBLK},
9090 #if defined(_FIRMWARE_NEEDS_FDISK)
9091 	{"q", 16, S_IFBLK},
9092 	{"r", 17, S_IFBLK},
9093 	{"s", 18, S_IFBLK},
9094 	{"t", 19, S_IFBLK},
9095 	{"u", 20, S_IFBLK},
9096 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9097 	{"a,raw", 0, S_IFCHR},
9098 	{"b,raw", 1, S_IFCHR},
9099 	{"c,raw", 2, S_IFCHR},
9100 	{"d,raw", 3, S_IFCHR},
9101 	{"e,raw", 4, S_IFCHR},
9102 	{"f,raw", 5, S_IFCHR},
9103 	{"g,raw", 6, S_IFCHR},
9104 	{"wd,raw", 7, S_IFCHR},
9105 #if defined(_FIRMWARE_NEEDS_FDISK)
9106 	{"q,raw", 16, S_IFCHR},
9107 	{"r,raw", 17, S_IFCHR},
9108 	{"s,raw", 18, S_IFCHR},
9109 	{"t,raw", 19, S_IFCHR},
9110 	{"u,raw", 20, S_IFCHR},
9111 #endif			/* defined(_FIRMWARE_NEEDS_FDISK) */
9112 	{0}
9113 };
9114 
9115 
9116 /*
9117  *    Function: sd_create_minor_nodes
9118  *
9119  * Description: Create the minor device nodes for the instance.
9120  *
9121  *   Arguments: un - driver soft state (unit) structure
9122  *		devi - pointer to device info structure
9123  *
9124  * Return Code: DDI_SUCCESS
9125  *		DDI_FAILURE
9126  *
9127  *     Context: Kernel thread context
9128  */
9129 
9130 static int
9131 sd_create_minor_nodes(struct sd_lun *un, dev_info_t *devi)
9132 {
9133 	struct driver_minor_data	*dmdp;
9134 	struct scsi_device		*devp;
9135 	int				instance;
9136 	char				name[48];
9137 
9138 	ASSERT(un != NULL);
9139 	devp = ddi_get_driver_private(devi);
9140 	instance = ddi_get_instance(devp->sd_dev);
9141 
9142 	/*
9143 	 * Create all the minor nodes for this target.
9144 	 */
9145 	if (un->un_blockcount > DK_MAX_BLOCKS)
9146 		dmdp = sd_minor_data_efi;
9147 	else
9148 		dmdp = sd_minor_data;
9149 	while (dmdp->name != NULL) {
9150 
9151 		(void) sprintf(name, "%s", dmdp->name);
9152 
9153 		if (ddi_create_minor_node(devi, name, dmdp->type,
9154 		    (instance << SDUNIT_SHIFT) | dmdp->minor,
9155 		    un->un_node_type, NULL) == DDI_FAILURE) {
9156 			/*
9157 			 * Clean up any nodes that may have been created, in
9158 			 * case this fails in the middle of the loop.
9159 			 */
9160 			ddi_remove_minor_node(devi, NULL);
9161 			return (DDI_FAILURE);
9162 		}
9163 		dmdp++;
9164 	}
9165 
9166 	return (DDI_SUCCESS);
9167 }
9168 
9169 
9170 /*
9171  *    Function: sd_create_errstats
9172  *
9173  * Description: This routine instantiates the device error stats.
9174  *
9175  *		Note: During attach the stats are instantiated first so they are
9176  *		available for attach-time routines that utilize the driver
9177  *		iopath to send commands to the device. The stats are initialized
9178  *		separately so data obtained during some attach-time routines is
9179  *		available. (4362483)
9180  *
9181  *   Arguments: un - driver soft state (unit) structure
9182  *		instance - driver instance
9183  *
9184  *     Context: Kernel thread context
9185  */
9186 
9187 static void
9188 sd_create_errstats(struct sd_lun *un, int instance)
9189 {
9190 	struct	sd_errstats	*stp;
9191 	char	kstatmodule_err[KSTAT_STRLEN];
9192 	char	kstatname[KSTAT_STRLEN];
9193 	int	ndata = (sizeof (struct sd_errstats) / sizeof (kstat_named_t));
9194 
9195 	ASSERT(un != NULL);
9196 
9197 	if (un->un_errstats != NULL) {
9198 		return;
9199 	}
9200 
9201 	(void) snprintf(kstatmodule_err, sizeof (kstatmodule_err),
9202 	    "%serr", sd_label);
9203 	(void) snprintf(kstatname, sizeof (kstatname),
9204 	    "%s%d,err", sd_label, instance);
9205 
9206 	un->un_errstats = kstat_create(kstatmodule_err, instance, kstatname,
9207 	    "device_error", KSTAT_TYPE_NAMED, ndata, KSTAT_FLAG_PERSISTENT);
9208 
9209 	if (un->un_errstats == NULL) {
9210 		SD_ERROR(SD_LOG_ATTACH_DETACH, un,
9211 		    "sd_create_errstats: Failed kstat_create\n");
9212 		return;
9213 	}
9214 
9215 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9216 	kstat_named_init(&stp->sd_softerrs,	"Soft Errors",
9217 	    KSTAT_DATA_UINT32);
9218 	kstat_named_init(&stp->sd_harderrs,	"Hard Errors",
9219 	    KSTAT_DATA_UINT32);
9220 	kstat_named_init(&stp->sd_transerrs,	"Transport Errors",
9221 	    KSTAT_DATA_UINT32);
9222 	kstat_named_init(&stp->sd_vid,		"Vendor",
9223 	    KSTAT_DATA_CHAR);
9224 	kstat_named_init(&stp->sd_pid,		"Product",
9225 	    KSTAT_DATA_CHAR);
9226 	kstat_named_init(&stp->sd_revision,	"Revision",
9227 	    KSTAT_DATA_CHAR);
9228 	kstat_named_init(&stp->sd_serial,	"Serial No",
9229 	    KSTAT_DATA_CHAR);
9230 	kstat_named_init(&stp->sd_capacity,	"Size",
9231 	    KSTAT_DATA_ULONGLONG);
9232 	kstat_named_init(&stp->sd_rq_media_err,	"Media Error",
9233 	    KSTAT_DATA_UINT32);
9234 	kstat_named_init(&stp->sd_rq_ntrdy_err,	"Device Not Ready",
9235 	    KSTAT_DATA_UINT32);
9236 	kstat_named_init(&stp->sd_rq_nodev_err,	"No Device",
9237 	    KSTAT_DATA_UINT32);
9238 	kstat_named_init(&stp->sd_rq_recov_err,	"Recoverable",
9239 	    KSTAT_DATA_UINT32);
9240 	kstat_named_init(&stp->sd_rq_illrq_err,	"Illegal Request",
9241 	    KSTAT_DATA_UINT32);
9242 	kstat_named_init(&stp->sd_rq_pfa_err,	"Predictive Failure Analysis",
9243 	    KSTAT_DATA_UINT32);
9244 
9245 	un->un_errstats->ks_private = un;
9246 	un->un_errstats->ks_update  = nulldev;
9247 
9248 	kstat_install(un->un_errstats);
9249 }
9250 
9251 
9252 /*
9253  *    Function: sd_set_errstats
9254  *
9255  * Description: This routine sets the value of the vendor id, product id,
9256  *		revision, serial number, and capacity device error stats.
9257  *
9258  *		Note: During attach the stats are instantiated first so they are
9259  *		available for attach-time routines that utilize the driver
9260  *		iopath to send commands to the device. The stats are initialized
9261  *		separately so data obtained during some attach-time routines is
9262  *		available. (4362483)
9263  *
9264  *   Arguments: un - driver soft state (unit) structure
9265  *
9266  *     Context: Kernel thread context
9267  */
9268 
9269 static void
9270 sd_set_errstats(struct sd_lun *un)
9271 {
9272 	struct	sd_errstats	*stp;
9273 
9274 	ASSERT(un != NULL);
9275 	ASSERT(un->un_errstats != NULL);
9276 	stp = (struct sd_errstats *)un->un_errstats->ks_data;
9277 	ASSERT(stp != NULL);
9278 	(void) strncpy(stp->sd_vid.value.c, un->un_sd->sd_inq->inq_vid, 8);
9279 	(void) strncpy(stp->sd_pid.value.c, un->un_sd->sd_inq->inq_pid, 16);
9280 	(void) strncpy(stp->sd_revision.value.c,
9281 	    un->un_sd->sd_inq->inq_revision, 4);
9282 
9283 	/*
9284 	 * Set the "Serial No" kstat for Sun qualified drives (indicated by
9285 	 * "SUN" in bytes 25-27 of the inquiry data (bytes 9-11 of the pid)
9286 	 * (4376302))
9287 	 */
9288 	if (bcmp(&SD_INQUIRY(un)->inq_pid[9], "SUN", 3) == 0) {
9289 		bcopy(&SD_INQUIRY(un)->inq_serial, stp->sd_serial.value.c,
9290 		    sizeof (SD_INQUIRY(un)->inq_serial));
9291 	}
9292 
9293 	if (un->un_f_blockcount_is_valid != TRUE) {
9294 		/*
9295 		 * Set capacity error stat to 0 for no media. This ensures
9296 		 * a valid capacity is displayed in response to 'iostat -E'
9297 		 * when no media is present in the device.
9298 		 */
9299 		stp->sd_capacity.value.ui64 = 0;
9300 	} else {
9301 		/*
9302 		 * Multiply un_blockcount by un->un_sys_blocksize to get
9303 		 * capacity.
9304 		 *
9305 		 * Note: for non-512 blocksize devices "un_blockcount" has been
9306 		 * "scaled" in sd_send_scsi_READ_CAPACITY by multiplying by
9307 		 * (un_tgt_blocksize / un->un_sys_blocksize).
9308 		 */
9309 		stp->sd_capacity.value.ui64 = (uint64_t)
9310 		    ((uint64_t)un->un_blockcount * un->un_sys_blocksize);
9311 	}
9312 }
9313 
9314 
9315 /*
9316  *    Function: sd_set_pstats
9317  *
9318  * Description: This routine instantiates and initializes the partition
9319  *              stats for each partition with more than zero blocks.
9320  *		(4363169)
9321  *
9322  *   Arguments: un - driver soft state (unit) structure
9323  *
9324  *     Context: Kernel thread context
9325  */
9326 
9327 static void
9328 sd_set_pstats(struct sd_lun *un)
9329 {
9330 	char	kstatname[KSTAT_STRLEN];
9331 	int	instance;
9332 	int	i;
9333 
9334 	ASSERT(un != NULL);
9335 
9336 	instance = ddi_get_instance(SD_DEVINFO(un));
9337 
9338 	/* Note:x86: is this a VTOC8/VTOC16 difference? */
9339 	for (i = 0; i < NSDMAP; i++) {
9340 		if ((un->un_pstats[i] == NULL) &&
9341 		    (un->un_map[i].dkl_nblk != 0)) {
9342 			(void) snprintf(kstatname, sizeof (kstatname),
9343 			    "%s%d,%s", sd_label, instance,
9344 			    sd_minor_data[i].name);
9345 			un->un_pstats[i] = kstat_create(sd_label,
9346 			    instance, kstatname, "partition", KSTAT_TYPE_IO,
9347 			    1, KSTAT_FLAG_PERSISTENT);
9348 			if (un->un_pstats[i] != NULL) {
9349 				un->un_pstats[i]->ks_lock = SD_MUTEX(un);
9350 				kstat_install(un->un_pstats[i]);
9351 			}
9352 		}
9353 	}
9354 }
9355 
9356 
9357 #if (defined(__fibre))
9358 /*
9359  *    Function: sd_init_event_callbacks
9360  *
9361  * Description: This routine initializes the insertion and removal event
9362  *		callbacks. (fibre only)
9363  *
9364  *   Arguments: un - driver soft state (unit) structure
9365  *
9366  *     Context: Kernel thread context
9367  */
9368 
9369 static void
9370 sd_init_event_callbacks(struct sd_lun *un)
9371 {
9372 	ASSERT(un != NULL);
9373 
9374 	if ((un->un_insert_event == NULL) &&
9375 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_INSERT_EVENT,
9376 	    &un->un_insert_event) == DDI_SUCCESS)) {
9377 		/*
9378 		 * Add the callback for an insertion event
9379 		 */
9380 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9381 		    un->un_insert_event, sd_event_callback, (void *)un,
9382 		    &(un->un_insert_cb_id));
9383 	}
9384 
9385 	if ((un->un_remove_event == NULL) &&
9386 	    (ddi_get_eventcookie(SD_DEVINFO(un), FCAL_REMOVE_EVENT,
9387 	    &un->un_remove_event) == DDI_SUCCESS)) {
9388 		/*
9389 		 * Add the callback for a removal event
9390 		 */
9391 		(void) ddi_add_event_handler(SD_DEVINFO(un),
9392 		    un->un_remove_event, sd_event_callback, (void *)un,
9393 		    &(un->un_remove_cb_id));
9394 	}
9395 }
9396 
9397 
9398 /*
9399  *    Function: sd_event_callback
9400  *
9401  * Description: This routine handles insert/remove events (photon). The
9402  *		state is changed to OFFLINE which can be used to supress
9403  *		error msgs. (fibre only)
9404  *
9405  *   Arguments: un - driver soft state (unit) structure
9406  *
9407  *     Context: Callout thread context
9408  */
9409 /* ARGSUSED */
9410 static void
9411 sd_event_callback(dev_info_t *dip, ddi_eventcookie_t event, void *arg,
9412     void *bus_impldata)
9413 {
9414 	struct sd_lun *un = (struct sd_lun *)arg;
9415 
9416 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_insert_event));
9417 	if (event == un->un_insert_event) {
9418 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: insert event");
9419 		mutex_enter(SD_MUTEX(un));
9420 		if (un->un_state == SD_STATE_OFFLINE) {
9421 			if (un->un_last_state != SD_STATE_SUSPENDED) {
9422 				un->un_state = un->un_last_state;
9423 			} else {
9424 				/*
9425 				 * We have gone through SUSPEND/RESUME while
9426 				 * we were offline. Restore the last state
9427 				 */
9428 				un->un_state = un->un_save_state;
9429 			}
9430 		}
9431 		mutex_exit(SD_MUTEX(un));
9432 
9433 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_remove_event));
9434 	} else if (event == un->un_remove_event) {
9435 		SD_TRACE(SD_LOG_COMMON, un, "sd_event_callback: remove event");
9436 		mutex_enter(SD_MUTEX(un));
9437 		/*
9438 		 * We need to handle an event callback that occurs during
9439 		 * the suspend operation, since we don't prevent it.
9440 		 */
9441 		if (un->un_state != SD_STATE_OFFLINE) {
9442 			if (un->un_state != SD_STATE_SUSPENDED) {
9443 				New_state(un, SD_STATE_OFFLINE);
9444 			} else {
9445 				un->un_last_state = SD_STATE_OFFLINE;
9446 			}
9447 		}
9448 		mutex_exit(SD_MUTEX(un));
9449 	} else {
9450 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
9451 		    "!Unknown event\n");
9452 	}
9453 
9454 }
9455 #endif
9456 
9457 
9458 /*
9459  *    Function: sd_disable_caching()
9460  *
9461  * Description: This routine is the driver entry point for disabling
9462  *		read and write caching by modifying the WCE (write cache
9463  *		enable) and RCD (read cache disable) bits of mode
9464  *		page 8 (MODEPAGE_CACHING).
9465  *
9466  *   Arguments: un - driver soft state (unit) structure
9467  *
9468  * Return Code: EIO
9469  *		code returned by sd_send_scsi_MODE_SENSE and
9470  *		sd_send_scsi_MODE_SELECT
9471  *
9472  *     Context: Kernel Thread
9473  */
9474 
9475 static int
9476 sd_disable_caching(struct sd_lun *un)
9477 {
9478 	struct mode_caching	*mode_caching_page;
9479 	uchar_t			*header;
9480 	size_t			buflen;
9481 	int			hdrlen;
9482 	int			bd_len;
9483 	int			rval = 0;
9484 
9485 	ASSERT(un != NULL);
9486 
9487 	/*
9488 	 * Do a test unit ready, otherwise a mode sense may not work if this
9489 	 * is the first command sent to the device after boot.
9490 	 */
9491 	(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
9492 
9493 	if (un->un_f_cfg_is_atapi == TRUE) {
9494 		hdrlen = MODE_HEADER_LENGTH_GRP2;
9495 	} else {
9496 		hdrlen = MODE_HEADER_LENGTH;
9497 	}
9498 
9499 	/*
9500 	 * Allocate memory for the retrieved mode page and its headers.  Set
9501 	 * a pointer to the page itself.
9502 	 */
9503 	buflen = hdrlen + MODE_BLK_DESC_LENGTH + sizeof (struct mode_caching);
9504 	header = kmem_zalloc(buflen, KM_SLEEP);
9505 
9506 	/* Get the information from the device. */
9507 	if (un->un_f_cfg_is_atapi == TRUE) {
9508 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, header, buflen,
9509 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9510 	} else {
9511 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, header, buflen,
9512 		    MODEPAGE_CACHING, SD_PATH_DIRECT);
9513 	}
9514 	if (rval != 0) {
9515 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
9516 		    "sd_disable_caching: Mode Sense Failed\n");
9517 		kmem_free(header, buflen);
9518 		return (rval);
9519 	}
9520 
9521 	/*
9522 	 * Determine size of Block Descriptors in order to locate
9523 	 * the mode page data. ATAPI devices return 0, SCSI devices
9524 	 * should return MODE_BLK_DESC_LENGTH.
9525 	 */
9526 	if (un->un_f_cfg_is_atapi == TRUE) {
9527 		struct mode_header_grp2	*mhp;
9528 		mhp	= (struct mode_header_grp2 *)header;
9529 		bd_len  = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
9530 	} else {
9531 		bd_len  = ((struct mode_header *)header)->bdesc_length;
9532 	}
9533 
9534 	if (bd_len > MODE_BLK_DESC_LENGTH) {
9535 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
9536 		    "sd_disable_caching: Mode Sense returned invalid "
9537 		    "block descriptor length\n");
9538 		kmem_free(header, buflen);
9539 		return (EIO);
9540 	}
9541 
9542 	mode_caching_page = (struct mode_caching *)(header + hdrlen + bd_len);
9543 
9544 	/* Check the relevant bits on successful mode sense. */
9545 	if ((mode_caching_page->wce) || !(mode_caching_page->rcd)) {
9546 		/*
9547 		 * Read or write caching is enabled.  Disable both of them.
9548 		 */
9549 		mode_caching_page->wce = 0;
9550 		mode_caching_page->rcd = 1;
9551 
9552 		/* Clear reserved bits before mode select. */
9553 		mode_caching_page->mode_page.ps = 0;
9554 
9555 		/*
9556 		 * Clear out mode header for mode select.
9557 		 * The rest of the retrieved page will be reused.
9558 		 */
9559 		bzero(header, hdrlen);
9560 
9561 		/* Change the cache page to disable all caching. */
9562 		if (un->un_f_cfg_is_atapi == TRUE) {
9563 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, header,
9564 			    buflen, SD_SAVE_PAGE, SD_PATH_DIRECT);
9565 		} else {
9566 			rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, header,
9567 			    buflen, SD_SAVE_PAGE, SD_PATH_DIRECT);
9568 		}
9569 	}
9570 
9571 	kmem_free(header, buflen);
9572 	return (rval);
9573 }
9574 
9575 
9576 /*
9577  *    Function: sd_make_device
9578  *
9579  * Description: Utility routine to return the Solaris device number from
9580  *		the data in the device's dev_info structure.
9581  *
9582  * Return Code: The Solaris device number
9583  *
9584  *     Context: Any
9585  */
9586 
9587 static dev_t
9588 sd_make_device(dev_info_t *devi)
9589 {
9590 	return (makedevice(ddi_name_to_major(ddi_get_name(devi)),
9591 	    ddi_get_instance(devi) << SDUNIT_SHIFT));
9592 }
9593 
9594 
9595 /*
9596  *    Function: sd_pm_entry
9597  *
9598  * Description: Called at the start of a new command to manage power
9599  *		and busy status of a device. This includes determining whether
9600  *		the current power state of the device is sufficient for
9601  *		performing the command or whether it must be changed.
9602  *		The PM framework is notified appropriately.
9603  *		Only with a return status of DDI_SUCCESS will the
9604  *		component be busy to the framework.
9605  *
9606  *		All callers of sd_pm_entry must check the return status
9607  *		and only call sd_pm_exit it it was DDI_SUCCESS. A status
9608  *		of DDI_FAILURE indicates the device failed to power up.
9609  *		In this case un_pm_count has been adjusted so the result
9610  *		on exit is still powered down, ie. count is less than 0.
9611  *		Calling sd_pm_exit with this count value hits an ASSERT.
9612  *
9613  * Return Code: DDI_SUCCESS or DDI_FAILURE
9614  *
9615  *     Context: Kernel thread context.
9616  */
9617 
9618 static int
9619 sd_pm_entry(struct sd_lun *un)
9620 {
9621 	int return_status = DDI_SUCCESS;
9622 
9623 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9624 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9625 
9626 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: entry\n");
9627 
9628 	if (un->un_f_pm_is_enabled == FALSE) {
9629 		SD_TRACE(SD_LOG_IO_PM, un,
9630 		    "sd_pm_entry: exiting, PM not enabled\n");
9631 		return (return_status);
9632 	}
9633 
9634 	/*
9635 	 * Just increment a counter if PM is enabled. On the transition from
9636 	 * 0 ==> 1, mark the device as busy.  The iodone side will decrement
9637 	 * the count with each IO and mark the device as idle when the count
9638 	 * hits 0.
9639 	 *
9640 	 * If the count is less than 0 the device is powered down. If a powered
9641 	 * down device is successfully powered up then the count must be
9642 	 * incremented to reflect the power up. Note that it'll get incremented
9643 	 * a second time to become busy.
9644 	 *
9645 	 * Because the following has the potential to change the device state
9646 	 * and must release the un_pm_mutex to do so, only one thread can be
9647 	 * allowed through at a time.
9648 	 */
9649 
9650 	mutex_enter(&un->un_pm_mutex);
9651 	while (un->un_pm_busy == TRUE) {
9652 		cv_wait(&un->un_pm_busy_cv, &un->un_pm_mutex);
9653 	}
9654 	un->un_pm_busy = TRUE;
9655 
9656 	if (un->un_pm_count < 1) {
9657 
9658 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_entry: busy component\n");
9659 
9660 		/*
9661 		 * Indicate we are now busy so the framework won't attempt to
9662 		 * power down the device. This call will only fail if either
9663 		 * we passed a bad component number or the device has no
9664 		 * components. Neither of these should ever happen.
9665 		 */
9666 		mutex_exit(&un->un_pm_mutex);
9667 		return_status = pm_busy_component(SD_DEVINFO(un), 0);
9668 		ASSERT(return_status == DDI_SUCCESS);
9669 
9670 		mutex_enter(&un->un_pm_mutex);
9671 
9672 		if (un->un_pm_count < 0) {
9673 			mutex_exit(&un->un_pm_mutex);
9674 
9675 			SD_TRACE(SD_LOG_IO_PM, un,
9676 			    "sd_pm_entry: power up component\n");
9677 
9678 			/*
9679 			 * pm_raise_power will cause sdpower to be called
9680 			 * which brings the device power level to the
9681 			 * desired state, ON in this case. If successful,
9682 			 * un_pm_count and un_power_level will be updated
9683 			 * appropriately.
9684 			 */
9685 			return_status = pm_raise_power(SD_DEVINFO(un), 0,
9686 			    SD_SPINDLE_ON);
9687 
9688 			mutex_enter(&un->un_pm_mutex);
9689 
9690 			if (return_status != DDI_SUCCESS) {
9691 				/*
9692 				 * Power up failed.
9693 				 * Idle the device and adjust the count
9694 				 * so the result on exit is that we're
9695 				 * still powered down, ie. count is less than 0.
9696 				 */
9697 				SD_TRACE(SD_LOG_IO_PM, un,
9698 				    "sd_pm_entry: power up failed,"
9699 				    " idle the component\n");
9700 
9701 				(void) pm_idle_component(SD_DEVINFO(un), 0);
9702 				un->un_pm_count--;
9703 			} else {
9704 				/*
9705 				 * Device is powered up, verify the
9706 				 * count is non-negative.
9707 				 * This is debug only.
9708 				 */
9709 				ASSERT(un->un_pm_count == 0);
9710 			}
9711 		}
9712 
9713 		if (return_status == DDI_SUCCESS) {
9714 			/*
9715 			 * For performance, now that the device has been tagged
9716 			 * as busy, and it's known to be powered up, update the
9717 			 * chain types to use jump tables that do not include
9718 			 * pm. This significantly lowers the overhead and
9719 			 * therefore improves performance.
9720 			 */
9721 
9722 			mutex_exit(&un->un_pm_mutex);
9723 			mutex_enter(SD_MUTEX(un));
9724 			SD_TRACE(SD_LOG_IO_PM, un,
9725 			    "sd_pm_entry: changing uscsi_chain_type from %d\n",
9726 			    un->un_uscsi_chain_type);
9727 
9728 			if (ISREMOVABLE(un)) {
9729 				un->un_buf_chain_type =
9730 				    SD_CHAIN_INFO_RMMEDIA_NO_PM;
9731 			} else {
9732 				un->un_buf_chain_type =
9733 				    SD_CHAIN_INFO_DISK_NO_PM;
9734 			}
9735 			un->un_uscsi_chain_type = SD_CHAIN_INFO_USCSI_CMD_NO_PM;
9736 
9737 			SD_TRACE(SD_LOG_IO_PM, un,
9738 			    "             changed  uscsi_chain_type to   %d\n",
9739 			    un->un_uscsi_chain_type);
9740 			mutex_exit(SD_MUTEX(un));
9741 			mutex_enter(&un->un_pm_mutex);
9742 
9743 			if (un->un_pm_idle_timeid == NULL) {
9744 				/* 300 ms. */
9745 				un->un_pm_idle_timeid =
9746 				    timeout(sd_pm_idletimeout_handler, un,
9747 				    (drv_usectohz((clock_t)300000)));
9748 				/*
9749 				 * Include an extra call to busy which keeps the
9750 				 * device busy with-respect-to the PM layer
9751 				 * until the timer fires, at which time it'll
9752 				 * get the extra idle call.
9753 				 */
9754 				(void) pm_busy_component(SD_DEVINFO(un), 0);
9755 			}
9756 		}
9757 	}
9758 	un->un_pm_busy = FALSE;
9759 	/* Next... */
9760 	cv_signal(&un->un_pm_busy_cv);
9761 
9762 	un->un_pm_count++;
9763 
9764 	SD_TRACE(SD_LOG_IO_PM, un,
9765 	    "sd_pm_entry: exiting, un_pm_count = %d\n", un->un_pm_count);
9766 
9767 	mutex_exit(&un->un_pm_mutex);
9768 
9769 	return (return_status);
9770 }
9771 
9772 
9773 /*
9774  *    Function: sd_pm_exit
9775  *
9776  * Description: Called at the completion of a command to manage busy
9777  *		status for the device. If the device becomes idle the
9778  *		PM framework is notified.
9779  *
9780  *     Context: Kernel thread context
9781  */
9782 
9783 static void
9784 sd_pm_exit(struct sd_lun *un)
9785 {
9786 	ASSERT(!mutex_owned(SD_MUTEX(un)));
9787 	ASSERT(!mutex_owned(&un->un_pm_mutex));
9788 
9789 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: entry\n");
9790 
9791 	/*
9792 	 * After attach the following flag is only read, so don't
9793 	 * take the penalty of acquiring a mutex for it.
9794 	 */
9795 	if (un->un_f_pm_is_enabled == TRUE) {
9796 
9797 		mutex_enter(&un->un_pm_mutex);
9798 		un->un_pm_count--;
9799 
9800 		SD_TRACE(SD_LOG_IO_PM, un,
9801 		    "sd_pm_exit: un_pm_count = %d\n", un->un_pm_count);
9802 
9803 		ASSERT(un->un_pm_count >= 0);
9804 		if (un->un_pm_count == 0) {
9805 			mutex_exit(&un->un_pm_mutex);
9806 
9807 			SD_TRACE(SD_LOG_IO_PM, un,
9808 			    "sd_pm_exit: idle component\n");
9809 
9810 			(void) pm_idle_component(SD_DEVINFO(un), 0);
9811 
9812 		} else {
9813 			mutex_exit(&un->un_pm_mutex);
9814 		}
9815 	}
9816 
9817 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_exit: exiting\n");
9818 }
9819 
9820 
9821 /*
9822  *    Function: sdopen
9823  *
9824  * Description: Driver's open(9e) entry point function.
9825  *
9826  *   Arguments: dev_i   - pointer to device number
9827  *		flag    - how to open file (FEXCL, FNDELAY, FREAD, FWRITE)
9828  *		otyp    - open type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
9829  *		cred_p  - user credential pointer
9830  *
9831  * Return Code: EINVAL
9832  *		ENXIO
9833  *		EIO
9834  *		EROFS
9835  *		EBUSY
9836  *
9837  *     Context: Kernel thread context
9838  */
9839 /* ARGSUSED */
9840 static int
9841 sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
9842 {
9843 	struct sd_lun	*un;
9844 	int		nodelay;
9845 	int		part;
9846 	int		partmask;
9847 	int		instance;
9848 	dev_t		dev;
9849 	int		rval = EIO;
9850 
9851 	/* Validate the open type */
9852 	if (otyp >= OTYPCNT) {
9853 		return (EINVAL);
9854 	}
9855 
9856 	dev = *dev_p;
9857 	instance = SDUNIT(dev);
9858 	mutex_enter(&sd_detach_mutex);
9859 
9860 	/*
9861 	 * Fail the open if there is no softstate for the instance, or
9862 	 * if another thread somewhere is trying to detach the instance.
9863 	 */
9864 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
9865 	    (un->un_detach_count != 0)) {
9866 		mutex_exit(&sd_detach_mutex);
9867 		/*
9868 		 * The probe cache only needs to be cleared when open (9e) fails
9869 		 * with ENXIO (4238046).
9870 		 */
9871 		/*
9872 		 * un-conditionally clearing probe cache is ok with
9873 		 * separate sd/ssd binaries
9874 		 * x86 platform can be an issue with both parallel
9875 		 * and fibre in 1 binary
9876 		 */
9877 		sd_scsi_clear_probe_cache();
9878 		return (ENXIO);
9879 	}
9880 
9881 	/*
9882 	 * The un_layer_count is to prevent another thread in specfs from
9883 	 * trying to detach the instance, which can happen when we are
9884 	 * called from a higher-layer driver instead of thru specfs.
9885 	 * This will not be needed when DDI provides a layered driver
9886 	 * interface that allows specfs to know that an instance is in
9887 	 * use by a layered driver & should not be detached.
9888 	 *
9889 	 * Note: the semantics for layered driver opens are exactly one
9890 	 * close for every open.
9891 	 */
9892 	if (otyp == OTYP_LYR) {
9893 		un->un_layer_count++;
9894 	}
9895 
9896 	/*
9897 	 * Keep a count of the current # of opens in progress. This is because
9898 	 * some layered drivers try to call us as a regular open. This can
9899 	 * cause problems that we cannot prevent, however by keeping this count
9900 	 * we can at least keep our open and detach routines from racing against
9901 	 * each other under such conditions.
9902 	 */
9903 	un->un_opens_in_progress++;
9904 	mutex_exit(&sd_detach_mutex);
9905 
9906 	nodelay  = (flag & (FNDELAY | FNONBLOCK));
9907 	part	 = SDPART(dev);
9908 	partmask = 1 << part;
9909 
9910 	/*
9911 	 * We use a semaphore here in order to serialize
9912 	 * open and close requests on the device.
9913 	 */
9914 	sema_p(&un->un_semoclose);
9915 
9916 	mutex_enter(SD_MUTEX(un));
9917 
9918 	/*
9919 	 * All device accesses go thru sdstrategy() where we check
9920 	 * on suspend status but there could be a scsi_poll command,
9921 	 * which bypasses sdstrategy(), so we need to check pm
9922 	 * status.
9923 	 */
9924 
9925 	if (!nodelay) {
9926 		while ((un->un_state == SD_STATE_SUSPENDED) ||
9927 		    (un->un_state == SD_STATE_PM_CHANGING)) {
9928 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
9929 		}
9930 
9931 		mutex_exit(SD_MUTEX(un));
9932 		if (sd_pm_entry(un) != DDI_SUCCESS) {
9933 			rval = EIO;
9934 			SD_ERROR(SD_LOG_OPEN_CLOSE, un,
9935 			    "sdopen: sd_pm_entry failed\n");
9936 			goto open_failed_with_pm;
9937 		}
9938 		mutex_enter(SD_MUTEX(un));
9939 	}
9940 
9941 	/* check for previous exclusive open */
9942 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: un=%p\n", (void *)un);
9943 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
9944 	    "sdopen: exclopen=%x, flag=%x, regopen=%x\n",
9945 	    un->un_exclopen, flag, un->un_ocmap.regopen[otyp]);
9946 
9947 	if (un->un_exclopen & (partmask)) {
9948 		goto excl_open_fail;
9949 	}
9950 
9951 	if (flag & FEXCL) {
9952 		int i;
9953 		if (un->un_ocmap.lyropen[part]) {
9954 			goto excl_open_fail;
9955 		}
9956 		for (i = 0; i < (OTYPCNT - 1); i++) {
9957 			if (un->un_ocmap.regopen[i] & (partmask)) {
9958 				goto excl_open_fail;
9959 			}
9960 		}
9961 	}
9962 
9963 	/*
9964 	 * Check the write permission if this is a removable media device,
9965 	 * NDELAY has not been set, and writable permission is requested.
9966 	 *
9967 	 * Note: If NDELAY was set and this is write-protected media the WRITE
9968 	 * attempt will fail with EIO as part of the I/O processing. This is a
9969 	 * more permissive implementation that allows the open to succeed and
9970 	 * WRITE attempts to fail when appropriate.
9971 	 */
9972 	if (ISREMOVABLE(un)) {
9973 		if ((flag & FWRITE) && (!nodelay)) {
9974 			mutex_exit(SD_MUTEX(un));
9975 			/*
9976 			 * Defer the check for write permission on writable
9977 			 * DVD drive till sdstrategy and will not fail open even
9978 			 * if FWRITE is set as the device can be writable
9979 			 * depending upon the media and the media can change
9980 			 * after the call to open().
9981 			 */
9982 			if (un->un_f_dvdram_writable_device == FALSE) {
9983 				if (ISCD(un) || sr_check_wp(dev)) {
9984 				rval = EROFS;
9985 				mutex_enter(SD_MUTEX(un));
9986 				SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
9987 				    "write to cd or write protected media\n");
9988 				goto open_fail;
9989 				}
9990 			}
9991 			mutex_enter(SD_MUTEX(un));
9992 		}
9993 	}
9994 
9995 	/*
9996 	 * If opening in NDELAY/NONBLOCK mode, just return.
9997 	 * Check if disk is ready and has a valid geometry later.
9998 	 */
9999 	if (!nodelay) {
10000 		mutex_exit(SD_MUTEX(un));
10001 		rval = sd_ready_and_valid(un);
10002 		mutex_enter(SD_MUTEX(un));
10003 		/*
10004 		 * Fail if device is not ready or if the number of disk
10005 		 * blocks is zero or negative for non CD devices.
10006 		 */
10007 		if ((rval != SD_READY_VALID) ||
10008 		    (!ISCD(un) && un->un_map[part].dkl_nblk <= 0)) {
10009 			if (ISREMOVABLE(un)) {
10010 				rval = ENXIO;
10011 			} else {
10012 				rval = EIO;
10013 			}
10014 			SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10015 			    "device not ready or invalid disk block value\n");
10016 			goto open_fail;
10017 		}
10018 #if defined(__i386) || defined(__amd64)
10019 	} else {
10020 		uchar_t *cp;
10021 		/*
10022 		 * x86 requires special nodelay handling, so that p0 is
10023 		 * always defined and accessible.
10024 		 * Invalidate geometry only if device is not already open.
10025 		 */
10026 		cp = &un->un_ocmap.chkd[0];
10027 		while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10028 			if (*cp != (uchar_t)0) {
10029 			    break;
10030 			}
10031 			cp++;
10032 		}
10033 		if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10034 			un->un_f_geometry_is_valid = FALSE;
10035 		}
10036 
10037 #endif
10038 	}
10039 
10040 	if (otyp == OTYP_LYR) {
10041 		un->un_ocmap.lyropen[part]++;
10042 	} else {
10043 		un->un_ocmap.regopen[otyp] |= partmask;
10044 	}
10045 
10046 	/* Set up open and exclusive open flags */
10047 	if (flag & FEXCL) {
10048 		un->un_exclopen |= (partmask);
10049 	}
10050 
10051 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: "
10052 	    "open of part %d type %d\n", part, otyp);
10053 
10054 	mutex_exit(SD_MUTEX(un));
10055 	if (!nodelay) {
10056 		sd_pm_exit(un);
10057 	}
10058 
10059 	sema_v(&un->un_semoclose);
10060 
10061 	mutex_enter(&sd_detach_mutex);
10062 	un->un_opens_in_progress--;
10063 	mutex_exit(&sd_detach_mutex);
10064 
10065 	SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdopen: exit success\n");
10066 	return (DDI_SUCCESS);
10067 
10068 excl_open_fail:
10069 	SD_ERROR(SD_LOG_OPEN_CLOSE, un, "sdopen: fail exclusive open\n");
10070 	rval = EBUSY;
10071 
10072 open_fail:
10073 	mutex_exit(SD_MUTEX(un));
10074 
10075 	/*
10076 	 * On a failed open we must exit the pm management.
10077 	 */
10078 	if (!nodelay) {
10079 		sd_pm_exit(un);
10080 	}
10081 open_failed_with_pm:
10082 	sema_v(&un->un_semoclose);
10083 
10084 	mutex_enter(&sd_detach_mutex);
10085 	un->un_opens_in_progress--;
10086 	if (otyp == OTYP_LYR) {
10087 		un->un_layer_count--;
10088 	}
10089 	mutex_exit(&sd_detach_mutex);
10090 
10091 	return (rval);
10092 }
10093 
10094 
10095 /*
10096  *    Function: sdclose
10097  *
10098  * Description: Driver's close(9e) entry point function.
10099  *
10100  *   Arguments: dev    - device number
10101  *		flag   - file status flag, informational only
10102  *		otyp   - close type (OTYP_BLK, OTYP_CHR, OTYP_LYR)
10103  *		cred_p - user credential pointer
10104  *
10105  * Return Code: ENXIO
10106  *
10107  *     Context: Kernel thread context
10108  */
10109 /* ARGSUSED */
10110 static int
10111 sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p)
10112 {
10113 	struct sd_lun	*un;
10114 	uchar_t		*cp;
10115 	int		part;
10116 	int		nodelay;
10117 	int		rval = 0;
10118 
10119 	/* Validate the open type */
10120 	if (otyp >= OTYPCNT) {
10121 		return (ENXIO);
10122 	}
10123 
10124 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10125 		return (ENXIO);
10126 	}
10127 
10128 	part = SDPART(dev);
10129 	nodelay = flag & (FNDELAY | FNONBLOCK);
10130 
10131 	SD_TRACE(SD_LOG_OPEN_CLOSE, un,
10132 	    "sdclose: close of part %d type %d\n", part, otyp);
10133 
10134 	/*
10135 	 * We use a semaphore here in order to serialize
10136 	 * open and close requests on the device.
10137 	 */
10138 	sema_p(&un->un_semoclose);
10139 
10140 	mutex_enter(SD_MUTEX(un));
10141 
10142 	/* Don't proceed if power is being changed. */
10143 	while (un->un_state == SD_STATE_PM_CHANGING) {
10144 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10145 	}
10146 
10147 	if (un->un_exclopen & (1 << part)) {
10148 		un->un_exclopen &= ~(1 << part);
10149 	}
10150 
10151 	/* Update the open partition map */
10152 	if (otyp == OTYP_LYR) {
10153 		un->un_ocmap.lyropen[part] -= 1;
10154 	} else {
10155 		un->un_ocmap.regopen[otyp] &= ~(1 << part);
10156 	}
10157 
10158 	cp = &un->un_ocmap.chkd[0];
10159 	while (cp < &un->un_ocmap.chkd[OCSIZE]) {
10160 		if (*cp != NULL) {
10161 			break;
10162 		}
10163 		cp++;
10164 	}
10165 
10166 	if (cp == &un->un_ocmap.chkd[OCSIZE]) {
10167 		SD_TRACE(SD_LOG_OPEN_CLOSE, un, "sdclose: last close\n");
10168 
10169 		/*
10170 		 * We avoid persistance upon the last close, and set
10171 		 * the throttle back to the maximum.
10172 		 */
10173 		un->un_throttle = un->un_saved_throttle;
10174 
10175 		if (un->un_state == SD_STATE_OFFLINE) {
10176 			if (un->un_f_is_fibre == FALSE) {
10177 				scsi_log(SD_DEVINFO(un), sd_label,
10178 					CE_WARN, "offline\n");
10179 			}
10180 			un->un_f_geometry_is_valid = FALSE;
10181 
10182 		} else {
10183 			/*
10184 			 * Flush any outstanding writes in NVRAM cache.
10185 			 * Note: SYNCHRONIZE CACHE is an optional SCSI-2
10186 			 * cmd, it may not work for non-Pluto devices.
10187 			 * SYNCHRONIZE CACHE is not required for removables,
10188 			 * except DVD-RAM drives.
10189 			 *
10190 			 * Also note: because SYNCHRONIZE CACHE is currently
10191 			 * the only command issued here that requires the
10192 			 * drive be powered up, only do the power up before
10193 			 * sending the Sync Cache command. If additional
10194 			 * commands are added which require a powered up
10195 			 * drive, the following sequence may have to change.
10196 			 *
10197 			 * And finally, note that parallel SCSI on SPARC
10198 			 * only issues a Sync Cache to DVD-RAM, a newly
10199 			 * supported device.
10200 			 */
10201 #if defined(__i386) || defined(__amd64)
10202 			if (!ISREMOVABLE(un) ||
10203 			    un->un_f_dvdram_writable_device == TRUE) {
10204 #else
10205 			if (un->un_f_dvdram_writable_device == TRUE) {
10206 #endif
10207 				mutex_exit(SD_MUTEX(un));
10208 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10209 					if (sd_send_scsi_SYNCHRONIZE_CACHE(un)
10210 					    != 0) {
10211 						rval = EIO;
10212 					}
10213 					sd_pm_exit(un);
10214 				} else {
10215 					rval = EIO;
10216 				}
10217 				mutex_enter(SD_MUTEX(un));
10218 			}
10219 
10220 			/*
10221 			 * For removable media devices, send an ALLOW MEDIA
10222 			 * REMOVAL command, but don't get upset if it fails.
10223 			 * Also invalidate the geometry. We need to raise
10224 			 * the power of the drive before we can call
10225 			 * sd_send_scsi_DOORLOCK()
10226 			 */
10227 			if (ISREMOVABLE(un)) {
10228 				mutex_exit(SD_MUTEX(un));
10229 				if (sd_pm_entry(un) == DDI_SUCCESS) {
10230 					rval = sd_send_scsi_DOORLOCK(un,
10231 					    SD_REMOVAL_ALLOW, SD_PATH_DIRECT);
10232 
10233 					sd_pm_exit(un);
10234 					if (ISCD(un) && (rval != 0) &&
10235 					    (nodelay != 0)) {
10236 						rval = ENXIO;
10237 					}
10238 				} else {
10239 					rval = EIO;
10240 				}
10241 				mutex_enter(SD_MUTEX(un));
10242 
10243 				sr_ejected(un);
10244 				/*
10245 				 * Destroy the cache (if it exists) which was
10246 				 * allocated for the write maps since this is
10247 				 * the last close for this media.
10248 				 */
10249 				if (un->un_wm_cache) {
10250 					/*
10251 					 * Check if there are pending commands.
10252 					 * and if there are give a warning and
10253 					 * do not destroy the cache.
10254 					 */
10255 					if (un->un_ncmds_in_driver > 0) {
10256 						scsi_log(SD_DEVINFO(un),
10257 						    sd_label, CE_WARN,
10258 						    "Unable to clean up memory "
10259 						    "because of pending I/O\n");
10260 					} else {
10261 						kmem_cache_destroy(
10262 						    un->un_wm_cache);
10263 						un->un_wm_cache = NULL;
10264 					}
10265 				}
10266 			}
10267 		}
10268 	}
10269 
10270 	mutex_exit(SD_MUTEX(un));
10271 	sema_v(&un->un_semoclose);
10272 
10273 	if (otyp == OTYP_LYR) {
10274 		mutex_enter(&sd_detach_mutex);
10275 		/*
10276 		 * The detach routine may run when the layer count
10277 		 * drops to zero.
10278 		 */
10279 		un->un_layer_count--;
10280 		mutex_exit(&sd_detach_mutex);
10281 	}
10282 
10283 	return (rval);
10284 }
10285 
10286 
10287 /*
10288  *    Function: sd_ready_and_valid
10289  *
10290  * Description: Test if device is ready and has a valid geometry.
10291  *
10292  *   Arguments: dev - device number
10293  *		un  - driver soft state (unit) structure
10294  *
10295  * Return Code: SD_READY_VALID		ready and valid label
10296  *		SD_READY_NOT_VALID	ready, geom ops never applicable
10297  *		SD_NOT_READY_VALID	not ready, no label
10298  *
10299  *     Context: Never called at interrupt context.
10300  */
10301 
10302 static int
10303 sd_ready_and_valid(struct sd_lun *un)
10304 {
10305 	struct sd_errstats	*stp;
10306 	uint64_t		capacity;
10307 	uint_t			lbasize;
10308 	int			rval = SD_READY_VALID;
10309 	char			name_str[48];
10310 
10311 	ASSERT(un != NULL);
10312 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10313 
10314 	mutex_enter(SD_MUTEX(un));
10315 	if (ISREMOVABLE(un)) {
10316 		mutex_exit(SD_MUTEX(un));
10317 		if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) {
10318 			rval = SD_NOT_READY_VALID;
10319 			mutex_enter(SD_MUTEX(un));
10320 			goto done;
10321 		}
10322 
10323 		mutex_enter(SD_MUTEX(un));
10324 		if ((un->un_f_geometry_is_valid == FALSE) ||
10325 		    (un->un_f_blockcount_is_valid == FALSE) ||
10326 		    (un->un_f_tgt_blocksize_is_valid == FALSE)) {
10327 
10328 			/* capacity has to be read every open. */
10329 			mutex_exit(SD_MUTEX(un));
10330 			if (sd_send_scsi_READ_CAPACITY(un, &capacity,
10331 			    &lbasize, SD_PATH_DIRECT) != 0) {
10332 				mutex_enter(SD_MUTEX(un));
10333 				un->un_f_geometry_is_valid = FALSE;
10334 				rval = SD_NOT_READY_VALID;
10335 				goto done;
10336 			} else {
10337 				mutex_enter(SD_MUTEX(un));
10338 				sd_update_block_info(un, lbasize, capacity);
10339 			}
10340 		}
10341 
10342 		/*
10343 		 * If this is a non 512 block device, allocate space for
10344 		 * the wmap cache. This is being done here since every time
10345 		 * a media is changed this routine will be called and the
10346 		 * block size is a function of media rather than device.
10347 		 */
10348 		if (NOT_DEVBSIZE(un)) {
10349 			if (!(un->un_wm_cache)) {
10350 				(void) snprintf(name_str, sizeof (name_str),
10351 				    "%s%d_cache",
10352 				    ddi_driver_name(SD_DEVINFO(un)),
10353 				    ddi_get_instance(SD_DEVINFO(un)));
10354 				un->un_wm_cache = kmem_cache_create(
10355 				    name_str, sizeof (struct sd_w_map),
10356 				    8, sd_wm_cache_constructor,
10357 				    sd_wm_cache_destructor, NULL,
10358 				    (void *)un, NULL, 0);
10359 				if (!(un->un_wm_cache)) {
10360 					rval = ENOMEM;
10361 					goto done;
10362 				}
10363 			}
10364 		}
10365 
10366 		/*
10367 		 * Check if the media in the device is writable or not.
10368 		 */
10369 		if ((un->un_f_geometry_is_valid == FALSE) && ISCD(un)) {
10370 			sd_check_for_writable_cd(un);
10371 		}
10372 
10373 	} else {
10374 		/*
10375 		 * Do a test unit ready to clear any unit attention from non-cd
10376 		 * devices.
10377 		 */
10378 		mutex_exit(SD_MUTEX(un));
10379 		(void) sd_send_scsi_TEST_UNIT_READY(un, 0);
10380 		mutex_enter(SD_MUTEX(un));
10381 	}
10382 
10383 
10384 	if (un->un_state == SD_STATE_NORMAL) {
10385 		/*
10386 		 * If the target is not yet ready here (defined by a TUR
10387 		 * failure), invalidate the geometry and print an 'offline'
10388 		 * message. This is a legacy message, as the state of the
10389 		 * target is not actually changed to SD_STATE_OFFLINE.
10390 		 *
10391 		 * If the TUR fails for EACCES (Reservation Conflict), it
10392 		 * means there actually is nothing wrong with the target that
10393 		 * would require invalidating the geometry, so continue in
10394 		 * that case as if the TUR was successful.
10395 		 */
10396 		int err;
10397 
10398 		mutex_exit(SD_MUTEX(un));
10399 		err = sd_send_scsi_TEST_UNIT_READY(un, 0);
10400 		mutex_enter(SD_MUTEX(un));
10401 
10402 		if ((err != 0) && (err != EACCES)) {
10403 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
10404 			    "offline\n");
10405 			un->un_f_geometry_is_valid = FALSE;
10406 			rval = SD_NOT_READY_VALID;
10407 			goto done;
10408 		}
10409 	}
10410 
10411 	if (un->un_f_format_in_progress == FALSE) {
10412 		/*
10413 		 * Note: sd_validate_geometry may return TRUE, but that does
10414 		 * not necessarily mean un_f_geometry_is_valid == TRUE!
10415 		 */
10416 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
10417 		if (rval == ENOTSUP) {
10418 			if (un->un_f_geometry_is_valid == TRUE)
10419 				rval = 0;
10420 			else {
10421 				rval = SD_READY_NOT_VALID;
10422 				goto done;
10423 			}
10424 		}
10425 		if (rval != 0) {
10426 			/*
10427 			 * We don't check the validity of geometry for
10428 			 * CDROMs. Also we assume we have a good label
10429 			 * even if sd_validate_geometry returned ENOMEM.
10430 			 */
10431 			if (!ISCD(un) && rval != ENOMEM) {
10432 				rval = SD_NOT_READY_VALID;
10433 				goto done;
10434 			}
10435 		}
10436 	}
10437 
10438 #ifdef DOESNTWORK /* on eliteII, see 1118607 */
10439 	/*
10440 	 * check to see if this disk is write protected, if it is and we have
10441 	 * not set read-only, then fail
10442 	 */
10443 	if ((flag & FWRITE) && (sr_check_wp(dev))) {
10444 		New_state(un, SD_STATE_CLOSED);
10445 		goto done;
10446 	}
10447 #endif
10448 
10449 	/*
10450 	 * If this is a removable media device, try and send
10451 	 * a PREVENT MEDIA REMOVAL command, but don't get upset
10452 	 * if it fails. For a CD, however, it is an error
10453 	 */
10454 	if (ISREMOVABLE(un)) {
10455 		mutex_exit(SD_MUTEX(un));
10456 		if ((sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
10457 		    SD_PATH_DIRECT) != 0) && ISCD(un)) {
10458 			rval = SD_NOT_READY_VALID;
10459 			mutex_enter(SD_MUTEX(un));
10460 			goto done;
10461 		}
10462 		mutex_enter(SD_MUTEX(un));
10463 	}
10464 
10465 	/* The state has changed, inform the media watch routines */
10466 	un->un_mediastate = DKIO_INSERTED;
10467 	cv_broadcast(&un->un_state_cv);
10468 	rval = SD_READY_VALID;
10469 
10470 done:
10471 
10472 	/*
10473 	 * Initialize the capacity kstat value, if no media previously
10474 	 * (capacity kstat is 0) and a media has been inserted
10475 	 * (un_blockcount > 0).
10476 	 * This is a more generic way then checking for ISREMOVABLE.
10477 	 */
10478 	if (un->un_errstats != NULL) {
10479 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
10480 		if ((stp->sd_capacity.value.ui64 == 0) &&
10481 		    (un->un_f_blockcount_is_valid == TRUE)) {
10482 			stp->sd_capacity.value.ui64 =
10483 			    (uint64_t)((uint64_t)un->un_blockcount *
10484 			    un->un_sys_blocksize);
10485 		}
10486 	}
10487 
10488 	mutex_exit(SD_MUTEX(un));
10489 	return (rval);
10490 }
10491 
10492 
10493 /*
10494  *    Function: sdmin
10495  *
10496  * Description: Routine to limit the size of a data transfer. Used in
10497  *		conjunction with physio(9F).
10498  *
10499  *   Arguments: bp - pointer to the indicated buf(9S) struct.
10500  *
10501  *     Context: Kernel thread context.
10502  */
10503 
10504 static void
10505 sdmin(struct buf *bp)
10506 {
10507 	struct sd_lun	*un;
10508 	int		instance;
10509 
10510 	instance = SDUNIT(bp->b_edev);
10511 
10512 	un = ddi_get_soft_state(sd_state, instance);
10513 	ASSERT(un != NULL);
10514 
10515 	if (bp->b_bcount > un->un_max_xfer_size) {
10516 		bp->b_bcount = un->un_max_xfer_size;
10517 	}
10518 }
10519 
10520 
10521 /*
10522  *    Function: sdread
10523  *
10524  * Description: Driver's read(9e) entry point function.
10525  *
10526  *   Arguments: dev   - device number
10527  *		uio   - structure pointer describing where data is to be stored
10528  *			in user's space
10529  *		cred_p  - user credential pointer
10530  *
10531  * Return Code: ENXIO
10532  *		EIO
10533  *		EINVAL
10534  *		value returned by physio
10535  *
10536  *     Context: Kernel thread context.
10537  */
10538 /* ARGSUSED */
10539 static int
10540 sdread(dev_t dev, struct uio *uio, cred_t *cred_p)
10541 {
10542 	struct sd_lun	*un = NULL;
10543 	int		secmask;
10544 	int		err;
10545 
10546 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10547 		return (ENXIO);
10548 	}
10549 
10550 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10551 
10552 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10553 		mutex_enter(SD_MUTEX(un));
10554 		/*
10555 		 * Because the call to sd_ready_and_valid will issue I/O we
10556 		 * must wait here if either the device is suspended or
10557 		 * if it's power level is changing.
10558 		 */
10559 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10560 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10561 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10562 		}
10563 		un->un_ncmds_in_driver++;
10564 		mutex_exit(SD_MUTEX(un));
10565 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
10566 			mutex_enter(SD_MUTEX(un));
10567 			un->un_ncmds_in_driver--;
10568 			ASSERT(un->un_ncmds_in_driver >= 0);
10569 			mutex_exit(SD_MUTEX(un));
10570 			return (EIO);
10571 		}
10572 		mutex_enter(SD_MUTEX(un));
10573 		un->un_ncmds_in_driver--;
10574 		ASSERT(un->un_ncmds_in_driver >= 0);
10575 		mutex_exit(SD_MUTEX(un));
10576 	}
10577 
10578 	/*
10579 	 * Read requests are restricted to multiples of the system block size.
10580 	 */
10581 	secmask = un->un_sys_blocksize - 1;
10582 
10583 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10584 		SD_ERROR(SD_LOG_READ_WRITE, un,
10585 		    "sdread: file offset not modulo %d\n",
10586 		    un->un_sys_blocksize);
10587 		err = EINVAL;
10588 	} else if (uio->uio_iov->iov_len & (secmask)) {
10589 		SD_ERROR(SD_LOG_READ_WRITE, un,
10590 		    "sdread: transfer length not modulo %d\n",
10591 		    un->un_sys_blocksize);
10592 		err = EINVAL;
10593 	} else {
10594 		err = physio(sdstrategy, NULL, dev, B_READ, sdmin, uio);
10595 	}
10596 	return (err);
10597 }
10598 
10599 
10600 /*
10601  *    Function: sdwrite
10602  *
10603  * Description: Driver's write(9e) entry point function.
10604  *
10605  *   Arguments: dev   - device number
10606  *		uio   - structure pointer describing where data is stored in
10607  *			user's space
10608  *		cred_p  - user credential pointer
10609  *
10610  * Return Code: ENXIO
10611  *		EIO
10612  *		EINVAL
10613  *		value returned by physio
10614  *
10615  *     Context: Kernel thread context.
10616  */
10617 /* ARGSUSED */
10618 static int
10619 sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p)
10620 {
10621 	struct sd_lun	*un = NULL;
10622 	int		secmask;
10623 	int		err;
10624 
10625 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10626 		return (ENXIO);
10627 	}
10628 
10629 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10630 
10631 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10632 		mutex_enter(SD_MUTEX(un));
10633 		/*
10634 		 * Because the call to sd_ready_and_valid will issue I/O we
10635 		 * must wait here if either the device is suspended or
10636 		 * if it's power level is changing.
10637 		 */
10638 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10639 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10640 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10641 		}
10642 		un->un_ncmds_in_driver++;
10643 		mutex_exit(SD_MUTEX(un));
10644 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
10645 			mutex_enter(SD_MUTEX(un));
10646 			un->un_ncmds_in_driver--;
10647 			ASSERT(un->un_ncmds_in_driver >= 0);
10648 			mutex_exit(SD_MUTEX(un));
10649 			return (EIO);
10650 		}
10651 		mutex_enter(SD_MUTEX(un));
10652 		un->un_ncmds_in_driver--;
10653 		ASSERT(un->un_ncmds_in_driver >= 0);
10654 		mutex_exit(SD_MUTEX(un));
10655 	}
10656 
10657 	/*
10658 	 * Write requests are restricted to multiples of the system block size.
10659 	 */
10660 	secmask = un->un_sys_blocksize - 1;
10661 
10662 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10663 		SD_ERROR(SD_LOG_READ_WRITE, un,
10664 		    "sdwrite: file offset not modulo %d\n",
10665 		    un->un_sys_blocksize);
10666 		err = EINVAL;
10667 	} else if (uio->uio_iov->iov_len & (secmask)) {
10668 		SD_ERROR(SD_LOG_READ_WRITE, un,
10669 		    "sdwrite: transfer length not modulo %d\n",
10670 		    un->un_sys_blocksize);
10671 		err = EINVAL;
10672 	} else {
10673 		err = physio(sdstrategy, NULL, dev, B_WRITE, sdmin, uio);
10674 	}
10675 	return (err);
10676 }
10677 
10678 
10679 /*
10680  *    Function: sdaread
10681  *
10682  * Description: Driver's aread(9e) entry point function.
10683  *
10684  *   Arguments: dev   - device number
10685  *		aio   - structure pointer describing where data is to be stored
10686  *		cred_p  - user credential pointer
10687  *
10688  * Return Code: ENXIO
10689  *		EIO
10690  *		EINVAL
10691  *		value returned by aphysio
10692  *
10693  *     Context: Kernel thread context.
10694  */
10695 /* ARGSUSED */
10696 static int
10697 sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10698 {
10699 	struct sd_lun	*un = NULL;
10700 	struct uio	*uio = aio->aio_uio;
10701 	int		secmask;
10702 	int		err;
10703 
10704 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10705 		return (ENXIO);
10706 	}
10707 
10708 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10709 
10710 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10711 		mutex_enter(SD_MUTEX(un));
10712 		/*
10713 		 * Because the call to sd_ready_and_valid will issue I/O we
10714 		 * must wait here if either the device is suspended or
10715 		 * if it's power level is changing.
10716 		 */
10717 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10718 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10719 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10720 		}
10721 		un->un_ncmds_in_driver++;
10722 		mutex_exit(SD_MUTEX(un));
10723 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
10724 			mutex_enter(SD_MUTEX(un));
10725 			un->un_ncmds_in_driver--;
10726 			ASSERT(un->un_ncmds_in_driver >= 0);
10727 			mutex_exit(SD_MUTEX(un));
10728 			return (EIO);
10729 		}
10730 		mutex_enter(SD_MUTEX(un));
10731 		un->un_ncmds_in_driver--;
10732 		ASSERT(un->un_ncmds_in_driver >= 0);
10733 		mutex_exit(SD_MUTEX(un));
10734 	}
10735 
10736 	/*
10737 	 * Read requests are restricted to multiples of the system block size.
10738 	 */
10739 	secmask = un->un_sys_blocksize - 1;
10740 
10741 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10742 		SD_ERROR(SD_LOG_READ_WRITE, un,
10743 		    "sdaread: file offset not modulo %d\n",
10744 		    un->un_sys_blocksize);
10745 		err = EINVAL;
10746 	} else if (uio->uio_iov->iov_len & (secmask)) {
10747 		SD_ERROR(SD_LOG_READ_WRITE, un,
10748 		    "sdaread: transfer length not modulo %d\n",
10749 		    un->un_sys_blocksize);
10750 		err = EINVAL;
10751 	} else {
10752 		err = aphysio(sdstrategy, anocancel, dev, B_READ, sdmin, aio);
10753 	}
10754 	return (err);
10755 }
10756 
10757 
10758 /*
10759  *    Function: sdawrite
10760  *
10761  * Description: Driver's awrite(9e) entry point function.
10762  *
10763  *   Arguments: dev   - device number
10764  *		aio   - structure pointer describing where data is stored
10765  *		cred_p  - user credential pointer
10766  *
10767  * Return Code: ENXIO
10768  *		EIO
10769  *		EINVAL
10770  *		value returned by aphysio
10771  *
10772  *     Context: Kernel thread context.
10773  */
10774 /* ARGSUSED */
10775 static int
10776 sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
10777 {
10778 	struct sd_lun	*un = NULL;
10779 	struct uio	*uio = aio->aio_uio;
10780 	int		secmask;
10781 	int		err;
10782 
10783 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
10784 		return (ENXIO);
10785 	}
10786 
10787 	ASSERT(!mutex_owned(SD_MUTEX(un)));
10788 
10789 	if ((un->un_f_geometry_is_valid == FALSE) && !ISCD(un)) {
10790 		mutex_enter(SD_MUTEX(un));
10791 		/*
10792 		 * Because the call to sd_ready_and_valid will issue I/O we
10793 		 * must wait here if either the device is suspended or
10794 		 * if it's power level is changing.
10795 		 */
10796 		while ((un->un_state == SD_STATE_SUSPENDED) ||
10797 		    (un->un_state == SD_STATE_PM_CHANGING)) {
10798 			cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
10799 		}
10800 		un->un_ncmds_in_driver++;
10801 		mutex_exit(SD_MUTEX(un));
10802 		if ((sd_ready_and_valid(un)) != SD_READY_VALID) {
10803 			mutex_enter(SD_MUTEX(un));
10804 			un->un_ncmds_in_driver--;
10805 			ASSERT(un->un_ncmds_in_driver >= 0);
10806 			mutex_exit(SD_MUTEX(un));
10807 			return (EIO);
10808 		}
10809 		mutex_enter(SD_MUTEX(un));
10810 		un->un_ncmds_in_driver--;
10811 		ASSERT(un->un_ncmds_in_driver >= 0);
10812 		mutex_exit(SD_MUTEX(un));
10813 	}
10814 
10815 	/*
10816 	 * Write requests are restricted to multiples of the system block size.
10817 	 */
10818 	secmask = un->un_sys_blocksize - 1;
10819 
10820 	if (uio->uio_loffset & ((offset_t)(secmask))) {
10821 		SD_ERROR(SD_LOG_READ_WRITE, un,
10822 		    "sdawrite: file offset not modulo %d\n",
10823 		    un->un_sys_blocksize);
10824 		err = EINVAL;
10825 	} else if (uio->uio_iov->iov_len & (secmask)) {
10826 		SD_ERROR(SD_LOG_READ_WRITE, un,
10827 		    "sdawrite: transfer length not modulo %d\n",
10828 		    un->un_sys_blocksize);
10829 		err = EINVAL;
10830 	} else {
10831 		err = aphysio(sdstrategy, anocancel, dev, B_WRITE, sdmin, aio);
10832 	}
10833 	return (err);
10834 }
10835 
10836 
10837 
10838 
10839 
10840 /*
10841  * Driver IO processing follows the following sequence:
10842  *
10843  *     sdioctl(9E)     sdstrategy(9E)         biodone(9F)
10844  *         |                |                     ^
10845  *         v                v                     |
10846  * sd_send_scsi_cmd()  ddi_xbuf_qstrategy()       +-------------------+
10847  *         |                |                     |                   |
10848  *         v                |                     |                   |
10849  * sd_uscsi_strategy() sd_xbuf_strategy()   sd_buf_iodone()   sd_uscsi_iodone()
10850  *         |                |                     ^                   ^
10851  *         v                v                     |                   |
10852  * SD_BEGIN_IOSTART()  SD_BEGIN_IOSTART()         |                   |
10853  *         |                |                     |                   |
10854  *     +---+                |                     +------------+      +-------+
10855  *     |                    |                                  |              |
10856  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
10857  *     |                    v                                  |              |
10858  *     |         sd_mapblockaddr_iostart()           sd_mapblockaddr_iodone() |
10859  *     |                    |                                  ^              |
10860  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
10861  *     |                    v                                  |              |
10862  *     |         sd_mapblocksize_iostart()           sd_mapblocksize_iodone() |
10863  *     |                    |                                  ^              |
10864  *     |   SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()|              |
10865  *     |                    v                                  |              |
10866  *     |           sd_checksum_iostart()               sd_checksum_iodone()   |
10867  *     |                    |                                  ^              |
10868  *     +-> SD_NEXT_IOSTART()|                  SD_NEXT_IODONE()+------------->+
10869  *     |                    v                                  |              |
10870  *     |              sd_pm_iostart()                     sd_pm_iodone()      |
10871  *     |                    |                                  ^              |
10872  *     |                    |                                  |              |
10873  *     +-> SD_NEXT_IOSTART()|               SD_BEGIN_IODONE()--+--------------+
10874  *                          |                           ^
10875  *                          v                           |
10876  *                   sd_core_iostart()                  |
10877  *                          |                           |
10878  *                          |                           +------>(*destroypkt)()
10879  *                          +-> sd_start_cmds() <-+     |           |
10880  *                          |                     |     |           v
10881  *                          |                     |     |  scsi_destroy_pkt(9F)
10882  *                          |                     |     |
10883  *                          +->(*initpkt)()       +- sdintr()
10884  *                          |  |                        |  |
10885  *                          |  +-> scsi_init_pkt(9F)    |  +-> sd_handle_xxx()
10886  *                          |  +-> scsi_setup_cdb(9F)   |
10887  *                          |                           |
10888  *                          +--> scsi_transport(9F)     |
10889  *                                     |                |
10890  *                                     +----> SCSA ---->+
10891  *
10892  *
10893  * This code is based upon the following presumtions:
10894  *
10895  *   - iostart and iodone functions operate on buf(9S) structures. These
10896  *     functions perform the necessary operations on the buf(9S) and pass
10897  *     them along to the next function in the chain by using the macros
10898  *     SD_NEXT_IOSTART() (for iostart side functions) and SD_NEXT_IODONE()
10899  *     (for iodone side functions).
10900  *
10901  *   - The iostart side functions may sleep. The iodone side functions
10902  *     are called under interrupt context and may NOT sleep. Therefore
10903  *     iodone side functions also may not call iostart side functions.
10904  *     (NOTE: iostart side functions should NOT sleep for memory, as
10905  *     this could result in deadlock.)
10906  *
10907  *   - An iostart side function may call its corresponding iodone side
10908  *     function directly (if necessary).
10909  *
10910  *   - In the event of an error, an iostart side function can return a buf(9S)
10911  *     to its caller by calling SD_BEGIN_IODONE() (after setting B_ERROR and
10912  *     b_error in the usual way of course).
10913  *
10914  *   - The taskq mechanism may be used by the iodone side functions to dispatch
10915  *     requests to the iostart side functions.  The iostart side functions in
10916  *     this case would be called under the context of a taskq thread, so it's
10917  *     OK for them to block/sleep/spin in this case.
10918  *
10919  *   - iostart side functions may allocate "shadow" buf(9S) structs and
10920  *     pass them along to the next function in the chain.  The corresponding
10921  *     iodone side functions must coalesce the "shadow" bufs and return
10922  *     the "original" buf to the next higher layer.
10923  *
10924  *   - The b_private field of the buf(9S) struct holds a pointer to
10925  *     an sd_xbuf struct, which contains information needed to
10926  *     construct the scsi_pkt for the command.
10927  *
10928  *   - The SD_MUTEX(un) is NOT held across calls to the next layer. Each
10929  *     layer must acquire & release the SD_MUTEX(un) as needed.
10930  */
10931 
10932 
10933 /*
10934  * Create taskq for all targets in the system. This is created at
10935  * _init(9E) and destroyed at _fini(9E).
10936  *
10937  * Note: here we set the minalloc to a reasonably high number to ensure that
10938  * we will have an adequate supply of task entries available at interrupt time.
10939  * This is used in conjunction with the TASKQ_PREPOPULATE flag in
10940  * sd_create_taskq().  Since we do not want to sleep for allocations at
10941  * interrupt time, set maxalloc equal to minalloc. That way we will just fail
10942  * the command if we ever try to dispatch more than SD_TASKQ_MAXALLOC taskq
10943  * requests any one instant in time.
10944  */
10945 #define	SD_TASKQ_NUMTHREADS	8
10946 #define	SD_TASKQ_MINALLOC	256
10947 #define	SD_TASKQ_MAXALLOC	256
10948 
10949 static taskq_t	*sd_tq = NULL;
10950 static int	sd_taskq_minalloc = SD_TASKQ_MINALLOC;
10951 static int	sd_taskq_maxalloc = SD_TASKQ_MAXALLOC;
10952 
10953 /*
10954  * The following task queue is being created for the write part of
10955  * read-modify-write of non-512 block size devices.
10956  * Limit the number of threads to 1 for now. This number has been choosen
10957  * considering the fact that it applies only to dvd ram drives/MO drives
10958  * currently. Performance for which is not main criteria at this stage.
10959  * Note: It needs to be explored if we can use a single taskq in future
10960  */
10961 #define	SD_WMR_TASKQ_NUMTHREADS	1
10962 static taskq_t	*sd_wmr_tq = NULL;
10963 
10964 /*
10965  *    Function: sd_taskq_create
10966  *
10967  * Description: Create taskq thread(s) and preallocate task entries
10968  *
10969  * Return Code: Returns a pointer to the allocated taskq_t.
10970  *
10971  *     Context: Can sleep. Requires blockable context.
10972  *
10973  *       Notes: - The taskq() facility currently is NOT part of the DDI.
10974  *		  (definitely NOT recommeded for 3rd-party drivers!) :-)
10975  *		- taskq_create() will block for memory, also it will panic
10976  *		  if it cannot create the requested number of threads.
10977  *		- Currently taskq_create() creates threads that cannot be
10978  *		  swapped.
10979  *		- We use TASKQ_PREPOPULATE to ensure we have an adequate
10980  *		  supply of taskq entries at interrupt time (ie, so that we
10981  *		  do not have to sleep for memory)
10982  */
10983 
10984 static void
10985 sd_taskq_create(void)
10986 {
10987 	char	taskq_name[TASKQ_NAMELEN];
10988 
10989 	ASSERT(sd_tq == NULL);
10990 	ASSERT(sd_wmr_tq == NULL);
10991 
10992 	(void) snprintf(taskq_name, sizeof (taskq_name),
10993 	    "%s_drv_taskq", sd_label);
10994 	sd_tq = (taskq_create(taskq_name, SD_TASKQ_NUMTHREADS,
10995 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
10996 	    TASKQ_PREPOPULATE));
10997 
10998 	(void) snprintf(taskq_name, sizeof (taskq_name),
10999 	    "%s_rmw_taskq", sd_label);
11000 	sd_wmr_tq = (taskq_create(taskq_name, SD_WMR_TASKQ_NUMTHREADS,
11001 	    (v.v_maxsyspri - 2), sd_taskq_minalloc, sd_taskq_maxalloc,
11002 	    TASKQ_PREPOPULATE));
11003 }
11004 
11005 
11006 /*
11007  *    Function: sd_taskq_delete
11008  *
11009  * Description: Complementary cleanup routine for sd_taskq_create().
11010  *
11011  *     Context: Kernel thread context.
11012  */
11013 
11014 static void
11015 sd_taskq_delete(void)
11016 {
11017 	ASSERT(sd_tq != NULL);
11018 	ASSERT(sd_wmr_tq != NULL);
11019 	taskq_destroy(sd_tq);
11020 	taskq_destroy(sd_wmr_tq);
11021 	sd_tq = NULL;
11022 	sd_wmr_tq = NULL;
11023 }
11024 
11025 
11026 /*
11027  *    Function: sdstrategy
11028  *
11029  * Description: Driver's strategy (9E) entry point function.
11030  *
11031  *   Arguments: bp - pointer to buf(9S)
11032  *
11033  * Return Code: Always returns zero
11034  *
11035  *     Context: Kernel thread context.
11036  */
11037 
11038 static int
11039 sdstrategy(struct buf *bp)
11040 {
11041 	struct sd_lun *un;
11042 
11043 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11044 	if (un == NULL) {
11045 		bioerror(bp, EIO);
11046 		bp->b_resid = bp->b_bcount;
11047 		biodone(bp);
11048 		return (0);
11049 	}
11050 	/* As was done in the past, fail new cmds. if state is dumping. */
11051 	if (un->un_state == SD_STATE_DUMPING) {
11052 		bioerror(bp, ENXIO);
11053 		bp->b_resid = bp->b_bcount;
11054 		biodone(bp);
11055 		return (0);
11056 	}
11057 
11058 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11059 
11060 	/*
11061 	 * Commands may sneak in while we released the mutex in
11062 	 * DDI_SUSPEND, we should block new commands. However, old
11063 	 * commands that are still in the driver at this point should
11064 	 * still be allowed to drain.
11065 	 */
11066 	mutex_enter(SD_MUTEX(un));
11067 	/*
11068 	 * Must wait here if either the device is suspended or
11069 	 * if it's power level is changing.
11070 	 */
11071 	while ((un->un_state == SD_STATE_SUSPENDED) ||
11072 	    (un->un_state == SD_STATE_PM_CHANGING)) {
11073 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
11074 	}
11075 
11076 	un->un_ncmds_in_driver++;
11077 
11078 	/*
11079 	 * atapi: Since we are running the CD for now in PIO mode we need to
11080 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11081 	 * the HBA's init_pkt routine.
11082 	 */
11083 	if (un->un_f_cfg_is_atapi == TRUE) {
11084 		mutex_exit(SD_MUTEX(un));
11085 		bp_mapin(bp);
11086 		mutex_enter(SD_MUTEX(un));
11087 	}
11088 	SD_INFO(SD_LOG_IO, un, "sdstrategy: un_ncmds_in_driver = %ld\n",
11089 	    un->un_ncmds_in_driver);
11090 
11091 	mutex_exit(SD_MUTEX(un));
11092 
11093 	/*
11094 	 * This will (eventually) allocate the sd_xbuf area and
11095 	 * call sd_xbuf_strategy().  We just want to return the
11096 	 * result of ddi_xbuf_qstrategy so that we have an opt-
11097 	 * imized tail call which saves us a stack frame.
11098 	 */
11099 	return (ddi_xbuf_qstrategy(bp, un->un_xbuf_attr));
11100 }
11101 
11102 
11103 /*
11104  *    Function: sd_xbuf_strategy
11105  *
11106  * Description: Function for initiating IO operations via the
11107  *		ddi_xbuf_qstrategy() mechanism.
11108  *
11109  *     Context: Kernel thread context.
11110  */
11111 
11112 static void
11113 sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg)
11114 {
11115 	struct sd_lun *un = arg;
11116 
11117 	ASSERT(bp != NULL);
11118 	ASSERT(xp != NULL);
11119 	ASSERT(un != NULL);
11120 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11121 
11122 	/*
11123 	 * Initialize the fields in the xbuf and save a pointer to the
11124 	 * xbuf in bp->b_private.
11125 	 */
11126 	sd_xbuf_init(un, bp, xp, SD_CHAIN_BUFIO, NULL);
11127 
11128 	/* Send the buf down the iostart chain */
11129 	SD_BEGIN_IOSTART(((struct sd_xbuf *)xp)->xb_chain_iostart, un, bp);
11130 }
11131 
11132 
11133 /*
11134  *    Function: sd_xbuf_init
11135  *
11136  * Description: Prepare the given sd_xbuf struct for use.
11137  *
11138  *   Arguments: un - ptr to softstate
11139  *		bp - ptr to associated buf(9S)
11140  *		xp - ptr to associated sd_xbuf
11141  *		chain_type - IO chain type to use:
11142  *			SD_CHAIN_NULL
11143  *			SD_CHAIN_BUFIO
11144  *			SD_CHAIN_USCSI
11145  *			SD_CHAIN_DIRECT
11146  *			SD_CHAIN_DIRECT_PRIORITY
11147  *		pktinfop - ptr to private data struct for scsi_pkt(9S)
11148  *			initialization; may be NULL if none.
11149  *
11150  *     Context: Kernel thread context
11151  */
11152 
11153 static void
11154 sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
11155 	uchar_t chain_type, void *pktinfop)
11156 {
11157 	int index;
11158 
11159 	ASSERT(un != NULL);
11160 	ASSERT(bp != NULL);
11161 	ASSERT(xp != NULL);
11162 
11163 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: buf:0x%p chain type:0x%x\n",
11164 	    bp, chain_type);
11165 
11166 	xp->xb_un	= un;
11167 	xp->xb_pktp	= NULL;
11168 	xp->xb_pktinfo	= pktinfop;
11169 	xp->xb_private	= bp->b_private;
11170 	xp->xb_blkno	= (daddr_t)bp->b_blkno;
11171 
11172 	/*
11173 	 * Set up the iostart and iodone chain indexes in the xbuf, based
11174 	 * upon the specified chain type to use.
11175 	 */
11176 	switch (chain_type) {
11177 	case SD_CHAIN_NULL:
11178 		/*
11179 		 * Fall thru to just use the values for the buf type, even
11180 		 * tho for the NULL chain these values will never be used.
11181 		 */
11182 		/* FALLTHRU */
11183 	case SD_CHAIN_BUFIO:
11184 		index = un->un_buf_chain_type;
11185 		break;
11186 	case SD_CHAIN_USCSI:
11187 		index = un->un_uscsi_chain_type;
11188 		break;
11189 	case SD_CHAIN_DIRECT:
11190 		index = un->un_direct_chain_type;
11191 		break;
11192 	case SD_CHAIN_DIRECT_PRIORITY:
11193 		index = un->un_priority_chain_type;
11194 		break;
11195 	default:
11196 		/* We're really broken if we ever get here... */
11197 		panic("sd_xbuf_init: illegal chain type!");
11198 		/*NOTREACHED*/
11199 	}
11200 
11201 	xp->xb_chain_iostart = sd_chain_index_map[index].sci_iostart_index;
11202 	xp->xb_chain_iodone = sd_chain_index_map[index].sci_iodone_index;
11203 
11204 	/*
11205 	 * It might be a bit easier to simply bzero the entire xbuf above,
11206 	 * but it turns out that since we init a fair number of members anyway,
11207 	 * we save a fair number cycles by doing explicit assignment of zero.
11208 	 */
11209 	xp->xb_pkt_flags	= 0;
11210 	xp->xb_dma_resid	= 0;
11211 	xp->xb_retry_count	= 0;
11212 	xp->xb_victim_retry_count = 0;
11213 	xp->xb_ua_retry_count	= 0;
11214 	xp->xb_sense_bp		= NULL;
11215 	xp->xb_sense_status	= 0;
11216 	xp->xb_sense_state	= 0;
11217 	xp->xb_sense_resid	= 0;
11218 
11219 	bp->b_private	= xp;
11220 	bp->b_flags	&= ~(B_DONE | B_ERROR);
11221 	bp->b_resid	= 0;
11222 	bp->av_forw	= NULL;
11223 	bp->av_back	= NULL;
11224 	bioerror(bp, 0);
11225 
11226 	SD_INFO(SD_LOG_IO, un, "sd_xbuf_init: done.\n");
11227 }
11228 
11229 
11230 /*
11231  *    Function: sd_uscsi_strategy
11232  *
11233  * Description: Wrapper for calling into the USCSI chain via physio(9F)
11234  *
11235  *   Arguments: bp - buf struct ptr
11236  *
11237  * Return Code: Always returns 0
11238  *
11239  *     Context: Kernel thread context
11240  */
11241 
11242 static int
11243 sd_uscsi_strategy(struct buf *bp)
11244 {
11245 	struct sd_lun		*un;
11246 	struct sd_uscsi_info	*uip;
11247 	struct sd_xbuf		*xp;
11248 	uchar_t			chain_type;
11249 
11250 	ASSERT(bp != NULL);
11251 
11252 	un = ddi_get_soft_state(sd_state, SD_GET_INSTANCE_FROM_BUF(bp));
11253 	if (un == NULL) {
11254 		bioerror(bp, EIO);
11255 		bp->b_resid = bp->b_bcount;
11256 		biodone(bp);
11257 		return (0);
11258 	}
11259 
11260 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11261 
11262 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: entry: buf:0x%p\n", bp);
11263 
11264 	mutex_enter(SD_MUTEX(un));
11265 	/*
11266 	 * atapi: Since we are running the CD for now in PIO mode we need to
11267 	 * call bp_mapin here to avoid bp_mapin called interrupt context under
11268 	 * the HBA's init_pkt routine.
11269 	 */
11270 	if (un->un_f_cfg_is_atapi == TRUE) {
11271 		mutex_exit(SD_MUTEX(un));
11272 		bp_mapin(bp);
11273 		mutex_enter(SD_MUTEX(un));
11274 	}
11275 	un->un_ncmds_in_driver++;
11276 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_strategy: un_ncmds_in_driver = %ld\n",
11277 	    un->un_ncmds_in_driver);
11278 	mutex_exit(SD_MUTEX(un));
11279 
11280 	/*
11281 	 * A pointer to a struct sd_uscsi_info is expected in bp->b_private
11282 	 */
11283 	ASSERT(bp->b_private != NULL);
11284 	uip = (struct sd_uscsi_info *)bp->b_private;
11285 
11286 	switch (uip->ui_flags) {
11287 	case SD_PATH_DIRECT:
11288 		chain_type = SD_CHAIN_DIRECT;
11289 		break;
11290 	case SD_PATH_DIRECT_PRIORITY:
11291 		chain_type = SD_CHAIN_DIRECT_PRIORITY;
11292 		break;
11293 	default:
11294 		chain_type = SD_CHAIN_USCSI;
11295 		break;
11296 	}
11297 
11298 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
11299 	sd_xbuf_init(un, bp, xp, chain_type, uip->ui_cmdp);
11300 
11301 	/* Use the index obtained within xbuf_init */
11302 	SD_BEGIN_IOSTART(xp->xb_chain_iostart, un, bp);
11303 
11304 	SD_TRACE(SD_LOG_IO, un, "sd_uscsi_strategy: exit: buf:0x%p\n", bp);
11305 
11306 	return (0);
11307 }
11308 
11309 
11310 /*
11311  * These routines perform raw i/o operations.
11312  */
11313 /*ARGSUSED*/
11314 static void
11315 sduscsimin(struct buf *bp)
11316 {
11317 	/*
11318 	 * do not break up because the CDB count would then
11319 	 * be incorrect and data underruns would result (incomplete
11320 	 * read/writes which would be retried and then failed, see
11321 	 * sdintr().
11322 	 */
11323 }
11324 
11325 
11326 
11327 /*
11328  *    Function: sd_send_scsi_cmd
11329  *
11330  * Description: Runs a USCSI command for user (when called thru sdioctl),
11331  *		or for the driver
11332  *
11333  *   Arguments: dev - the dev_t for the device
11334  *		incmd - ptr to a valid uscsi_cmd struct
11335  *		cdbspace - UIO_USERSPACE or UIO_SYSSPACE
11336  *		dataspace - UIO_USERSPACE or UIO_SYSSPACE
11337  *		rqbufspace - UIO_USERSPACE or UIO_SYSSPACE
11338  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
11339  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
11340  *			to use the USCSI "direct" chain and bypass the normal
11341  *			command waitq.
11342  *
11343  * Return Code: 0 -  successful completion of the given command
11344  *		EIO - scsi_reset() failed, or see biowait()/physio() codes.
11345  *		ENXIO  - soft state not found for specified dev
11346  *		EINVAL
11347  *		EFAULT - copyin/copyout error
11348  *		return code of biowait(9F) or physio(9F):
11349  *			EIO - IO error, caller may check incmd->uscsi_status
11350  *			ENXIO
11351  *			EACCES - reservation conflict
11352  *
11353  *     Context: Waits for command to complete. Can sleep.
11354  */
11355 
11356 static int
11357 sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd,
11358 	enum uio_seg cdbspace, enum uio_seg dataspace, enum uio_seg rqbufspace,
11359 	int path_flag)
11360 {
11361 	struct sd_uscsi_info	*uip;
11362 	struct uscsi_cmd	*uscmd;
11363 	struct sd_lun	*un;
11364 	struct buf	*bp;
11365 	int	rval;
11366 	int	flags;
11367 
11368 	un = ddi_get_soft_state(sd_state, SDUNIT(dev));
11369 	if (un == NULL) {
11370 		return (ENXIO);
11371 	}
11372 
11373 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11374 
11375 #ifdef SDDEBUG
11376 	switch (dataspace) {
11377 	case UIO_USERSPACE:
11378 		SD_TRACE(SD_LOG_IO, un,
11379 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_USERSPACE\n", un);
11380 		break;
11381 	case UIO_SYSSPACE:
11382 		SD_TRACE(SD_LOG_IO, un,
11383 		    "sd_send_scsi_cmd: entry: un:0x%p UIO_SYSSPACE\n", un);
11384 		break;
11385 	default:
11386 		SD_TRACE(SD_LOG_IO, un,
11387 		    "sd_send_scsi_cmd: entry: un:0x%p UNEXPECTED SPACE\n", un);
11388 		break;
11389 	}
11390 #endif
11391 
11392 	/*
11393 	 * Perform resets directly; no need to generate a command to do it.
11394 	 */
11395 	if (incmd->uscsi_flags & (USCSI_RESET | USCSI_RESET_ALL)) {
11396 		flags = ((incmd->uscsi_flags & USCSI_RESET_ALL) != 0) ?
11397 		    RESET_ALL : RESET_TARGET;
11398 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: Issuing reset\n");
11399 		if (scsi_reset(SD_ADDRESS(un), flags) == 0) {
11400 			/* Reset attempt was unsuccessful */
11401 			SD_TRACE(SD_LOG_IO, un,
11402 			    "sd_send_scsi_cmd: reset: failure\n");
11403 			return (EIO);
11404 		}
11405 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: reset: success\n");
11406 		return (0);
11407 	}
11408 
11409 	/* Perfunctory sanity check... */
11410 	if (incmd->uscsi_cdblen <= 0) {
11411 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11412 		    "invalid uscsi_cdblen, returning EINVAL\n");
11413 		return (EINVAL);
11414 	}
11415 
11416 	/*
11417 	 * In order to not worry about where the uscsi structure came from
11418 	 * (or where the cdb it points to came from) we're going to make
11419 	 * kmem_alloc'd copies of them here. This will also allow reference
11420 	 * to the data they contain long after this process has gone to
11421 	 * sleep and its kernel stack has been unmapped, etc.
11422 	 *
11423 	 * First get some memory for the uscsi_cmd struct and copy the
11424 	 * contents of the given uscsi_cmd struct into it.
11425 	 */
11426 	uscmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11427 	bcopy(incmd, uscmd, sizeof (struct uscsi_cmd));
11428 
11429 	SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: uscsi_cmd",
11430 	    (uchar_t *)uscmd, sizeof (struct uscsi_cmd), SD_LOG_HEX);
11431 
11432 	/*
11433 	 * Now get some space for the CDB, and copy the given CDB into
11434 	 * it. Use ddi_copyin() in case the data is in user space.
11435 	 */
11436 	uscmd->uscsi_cdb = kmem_zalloc((size_t)incmd->uscsi_cdblen, KM_SLEEP);
11437 	flags = (cdbspace == UIO_SYSSPACE) ? FKIOCTL : 0;
11438 	if (ddi_copyin(incmd->uscsi_cdb, uscmd->uscsi_cdb,
11439 	    (uint_t)incmd->uscsi_cdblen, flags) != 0) {
11440 		kmem_free(uscmd->uscsi_cdb, (size_t)incmd->uscsi_cdblen);
11441 		kmem_free(uscmd, sizeof (struct uscsi_cmd));
11442 		return (EFAULT);
11443 	}
11444 
11445 	SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_cmd: CDB",
11446 	    (uchar_t *)uscmd->uscsi_cdb, incmd->uscsi_cdblen, SD_LOG_HEX);
11447 
11448 	bp = getrbuf(KM_SLEEP);
11449 
11450 	/*
11451 	 * Allocate an sd_uscsi_info struct and fill it with the info
11452 	 * needed by sd_initpkt_for_uscsi().  Then put the pointer into
11453 	 * b_private in the buf for sd_initpkt_for_uscsi().  Note that
11454 	 * since we allocate the buf here in this function, we do not
11455 	 * need to preserve the prior contents of b_private.
11456 	 * The sd_uscsi_info struct is also used by sd_uscsi_strategy()
11457 	 */
11458 	uip = kmem_zalloc(sizeof (struct sd_uscsi_info), KM_SLEEP);
11459 	uip->ui_flags = path_flag;
11460 	uip->ui_cmdp  = uscmd;
11461 	bp->b_private = uip;
11462 
11463 	/*
11464 	 * Initialize Request Sense buffering, if requested.
11465 	 */
11466 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
11467 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
11468 		/*
11469 		 * Here uscmd->uscsi_rqbuf currently points to the caller's
11470 		 * buffer, but we replace this with a kernel buffer that
11471 		 * we allocate to use with the sense data. The sense data
11472 		 * (if present) gets copied into this new buffer before the
11473 		 * command is completed.  Then we copy the sense data from
11474 		 * our allocated buf into the caller's buffer below. Note
11475 		 * that incmd->uscsi_rqbuf and incmd->uscsi_rqlen are used
11476 		 * below to perform the copy back to the caller's buf.
11477 		 */
11478 		uscmd->uscsi_rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
11479 		if (rqbufspace == UIO_USERSPACE) {
11480 			uscmd->uscsi_rqlen   = SENSE_LENGTH;
11481 			uscmd->uscsi_rqresid = SENSE_LENGTH;
11482 		} else {
11483 			uchar_t rlen = min(SENSE_LENGTH, uscmd->uscsi_rqlen);
11484 			uscmd->uscsi_rqlen   = rlen;
11485 			uscmd->uscsi_rqresid = rlen;
11486 		}
11487 	} else {
11488 		uscmd->uscsi_rqbuf = NULL;
11489 		uscmd->uscsi_rqlen   = 0;
11490 		uscmd->uscsi_rqresid = 0;
11491 	}
11492 
11493 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: rqbuf:0x%p  rqlen:%d\n",
11494 	    uscmd->uscsi_rqbuf, uscmd->uscsi_rqlen);
11495 
11496 	if (un->un_f_is_fibre == FALSE) {
11497 		/*
11498 		 * Force asynchronous mode, if necessary.  Doing this here
11499 		 * has the unfortunate effect of running other queued
11500 		 * commands async also, but since the main purpose of this
11501 		 * capability is downloading new drive firmware, we can
11502 		 * probably live with it.
11503 		 */
11504 		if ((uscmd->uscsi_flags & USCSI_ASYNC) != 0) {
11505 			if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1)
11506 				== 1) {
11507 				if (scsi_ifsetcap(SD_ADDRESS(un),
11508 					    "synchronous", 0, 1) == 1) {
11509 					SD_TRACE(SD_LOG_IO, un,
11510 					"sd_send_scsi_cmd: forced async ok\n");
11511 				} else {
11512 					SD_TRACE(SD_LOG_IO, un,
11513 					"sd_send_scsi_cmd:\
11514 					forced async failed\n");
11515 					rval = EINVAL;
11516 					goto done;
11517 				}
11518 			}
11519 		}
11520 
11521 		/*
11522 		 * Re-enable synchronous mode, if requested
11523 		 */
11524 		if (uscmd->uscsi_flags & USCSI_SYNC) {
11525 			if (scsi_ifgetcap(SD_ADDRESS(un), "synchronous", 1)
11526 				== 0) {
11527 				int i = scsi_ifsetcap(SD_ADDRESS(un),
11528 						"synchronous", 1, 1);
11529 				SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11530 					"re-enabled sync %s\n",
11531 					(i == 1) ? "ok" : "failed");
11532 			}
11533 		}
11534 	}
11535 
11536 	/*
11537 	 * Commands sent with priority are intended for error recovery
11538 	 * situations, and do not have retries performed.
11539 	 */
11540 	if (path_flag == SD_PATH_DIRECT_PRIORITY) {
11541 		uscmd->uscsi_flags |= USCSI_DIAGNOSE;
11542 	}
11543 
11544 	/*
11545 	 * If we're going to do actual I/O, let physio do all the right things
11546 	 */
11547 	if (uscmd->uscsi_buflen != 0) {
11548 		struct iovec	aiov;
11549 		struct uio	auio;
11550 		struct uio	*uio = &auio;
11551 
11552 		bzero(&auio, sizeof (struct uio));
11553 		bzero(&aiov, sizeof (struct iovec));
11554 		aiov.iov_base = uscmd->uscsi_bufaddr;
11555 		aiov.iov_len  = uscmd->uscsi_buflen;
11556 		uio->uio_iov  = &aiov;
11557 
11558 		uio->uio_iovcnt  = 1;
11559 		uio->uio_resid   = uscmd->uscsi_buflen;
11560 		uio->uio_segflg  = dataspace;
11561 
11562 		/*
11563 		 * physio() will block here until the command completes....
11564 		 */
11565 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling physio.\n");
11566 
11567 		rval = physio(sd_uscsi_strategy, bp, dev,
11568 		    ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE),
11569 		    sduscsimin, uio);
11570 
11571 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11572 		    "returned from physio with 0x%x\n", rval);
11573 
11574 	} else {
11575 		/*
11576 		 * We have to mimic what physio would do here! Argh!
11577 		 */
11578 		bp->b_flags  = B_BUSY |
11579 		    ((uscmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE);
11580 		bp->b_edev   = dev;
11581 		bp->b_dev    = cmpdev(dev);	/* maybe unnecessary? */
11582 		bp->b_bcount = 0;
11583 		bp->b_blkno  = 0;
11584 
11585 		SD_TRACE(SD_LOG_IO, un,
11586 		    "sd_send_scsi_cmd: calling sd_uscsi_strategy...\n");
11587 
11588 		(void) sd_uscsi_strategy(bp);
11589 
11590 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: calling biowait\n");
11591 
11592 		rval = biowait(bp);
11593 
11594 		SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11595 		    "returned from  biowait with 0x%x\n", rval);
11596 	}
11597 
11598 done:
11599 
11600 #ifdef SDDEBUG
11601 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11602 	    "uscsi_status: 0x%02x  uscsi_resid:0x%x\n",
11603 	    uscmd->uscsi_status, uscmd->uscsi_resid);
11604 	if (uscmd->uscsi_bufaddr != NULL) {
11605 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11606 		    "uscmd->uscsi_bufaddr: 0x%p  uscmd->uscsi_buflen:%d\n",
11607 		    uscmd->uscsi_bufaddr, uscmd->uscsi_buflen);
11608 		if (dataspace == UIO_SYSSPACE) {
11609 			SD_DUMP_MEMORY(un, SD_LOG_IO,
11610 			    "data", (uchar_t *)uscmd->uscsi_bufaddr,
11611 			    uscmd->uscsi_buflen, SD_LOG_HEX);
11612 		}
11613 	}
11614 #endif
11615 
11616 	/*
11617 	 * Get the status and residual to return to the caller.
11618 	 */
11619 	incmd->uscsi_status = uscmd->uscsi_status;
11620 	incmd->uscsi_resid  = uscmd->uscsi_resid;
11621 
11622 	/*
11623 	 * If the caller wants sense data, copy back whatever sense data
11624 	 * we may have gotten, and update the relevant rqsense info.
11625 	 */
11626 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
11627 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
11628 
11629 		int rqlen = uscmd->uscsi_rqlen - uscmd->uscsi_rqresid;
11630 		rqlen = min(((int)incmd->uscsi_rqlen), rqlen);
11631 
11632 		/* Update the Request Sense status and resid */
11633 		incmd->uscsi_rqresid  = incmd->uscsi_rqlen - rqlen;
11634 		incmd->uscsi_rqstatus = uscmd->uscsi_rqstatus;
11635 
11636 		SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11637 		    "uscsi_rqstatus: 0x%02x  uscsi_rqresid:0x%x\n",
11638 		    incmd->uscsi_rqstatus, incmd->uscsi_rqresid);
11639 
11640 		/* Copy out the sense data for user processes */
11641 		if ((incmd->uscsi_rqbuf != NULL) && (rqlen != 0)) {
11642 			int flags =
11643 			    (rqbufspace == UIO_USERSPACE) ? 0 : FKIOCTL;
11644 			if (ddi_copyout(uscmd->uscsi_rqbuf, incmd->uscsi_rqbuf,
11645 			    rqlen, flags) != 0) {
11646 				rval = EFAULT;
11647 			}
11648 			/*
11649 			 * Note: Can't touch incmd->uscsi_rqbuf so use
11650 			 * uscmd->uscsi_rqbuf instead. They're the same.
11651 			 */
11652 			SD_INFO(SD_LOG_IO, un, "sd_send_scsi_cmd: "
11653 			    "incmd->uscsi_rqbuf: 0x%p  rqlen:%d\n",
11654 			    incmd->uscsi_rqbuf, rqlen);
11655 			SD_DUMP_MEMORY(un, SD_LOG_IO, "rq",
11656 			    (uchar_t *)uscmd->uscsi_rqbuf, rqlen, SD_LOG_HEX);
11657 		}
11658 	}
11659 
11660 	/*
11661 	 * Free allocated resources and return; mapout the buf in case it was
11662 	 * mapped in by a lower layer.
11663 	 */
11664 	bp_mapout(bp);
11665 	freerbuf(bp);
11666 	kmem_free(uip, sizeof (struct sd_uscsi_info));
11667 	if (uscmd->uscsi_rqbuf != NULL) {
11668 		kmem_free(uscmd->uscsi_rqbuf, SENSE_LENGTH);
11669 	}
11670 	kmem_free(uscmd->uscsi_cdb, (size_t)uscmd->uscsi_cdblen);
11671 	kmem_free(uscmd, sizeof (struct uscsi_cmd));
11672 
11673 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_cmd: exit\n");
11674 
11675 	return (rval);
11676 }
11677 
11678 
11679 /*
11680  *    Function: sd_buf_iodone
11681  *
11682  * Description: Frees the sd_xbuf & returns the buf to its originator.
11683  *
11684  *     Context: May be called from interrupt context.
11685  */
11686 /* ARGSUSED */
11687 static void
11688 sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp)
11689 {
11690 	struct sd_xbuf *xp;
11691 
11692 	ASSERT(un != NULL);
11693 	ASSERT(bp != NULL);
11694 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11695 
11696 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: entry.\n");
11697 
11698 	xp = SD_GET_XBUF(bp);
11699 	ASSERT(xp != NULL);
11700 
11701 	mutex_enter(SD_MUTEX(un));
11702 
11703 	/*
11704 	 * Grab time when the cmd completed.
11705 	 * This is used for determining if the system has been
11706 	 * idle long enough to make it idle to the PM framework.
11707 	 * This is for lowering the overhead, and therefore improving
11708 	 * performance per I/O operation.
11709 	 */
11710 	un->un_pm_idle_time = ddi_get_time();
11711 
11712 	un->un_ncmds_in_driver--;
11713 	ASSERT(un->un_ncmds_in_driver >= 0);
11714 	SD_INFO(SD_LOG_IO, un, "sd_buf_iodone: un_ncmds_in_driver = %ld\n",
11715 	    un->un_ncmds_in_driver);
11716 
11717 	mutex_exit(SD_MUTEX(un));
11718 
11719 	ddi_xbuf_done(bp, un->un_xbuf_attr);	/* xbuf is gone after this */
11720 	biodone(bp);				/* bp is gone after this */
11721 
11722 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_buf_iodone: exit.\n");
11723 }
11724 
11725 
11726 /*
11727  *    Function: sd_uscsi_iodone
11728  *
11729  * Description: Frees the sd_xbuf & returns the buf to its originator.
11730  *
11731  *     Context: May be called from interrupt context.
11732  */
11733 /* ARGSUSED */
11734 static void
11735 sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
11736 {
11737 	struct sd_xbuf *xp;
11738 
11739 	ASSERT(un != NULL);
11740 	ASSERT(bp != NULL);
11741 
11742 	xp = SD_GET_XBUF(bp);
11743 	ASSERT(xp != NULL);
11744 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11745 
11746 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: entry.\n");
11747 
11748 	mutex_enter(SD_MUTEX(un));
11749 
11750 	/*
11751 	 * Grab time when the cmd completed.
11752 	 * This is used for determining if the system has been
11753 	 * idle long enough to make it idle to the PM framework.
11754 	 * This is for lowering the overhead, and therefore improving
11755 	 * performance per I/O operation.
11756 	 */
11757 	un->un_pm_idle_time = ddi_get_time();
11758 
11759 	un->un_ncmds_in_driver--;
11760 	ASSERT(un->un_ncmds_in_driver >= 0);
11761 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: un_ncmds_in_driver = %ld\n",
11762 	    un->un_ncmds_in_driver);
11763 
11764 	mutex_exit(SD_MUTEX(un));
11765 
11766 	kmem_free(xp, sizeof (struct sd_xbuf));
11767 	biodone(bp);
11768 
11769 	SD_INFO(SD_LOG_IO, un, "sd_uscsi_iodone: exit.\n");
11770 }
11771 
11772 
11773 /*
11774  *    Function: sd_mapblockaddr_iostart
11775  *
11776  * Description: Verify request lies withing the partition limits for
11777  *		the indicated minor device.  Issue "overrun" buf if
11778  *		request would exceed partition range.  Converts
11779  *		partition-relative block address to absolute.
11780  *
11781  *     Context: Can sleep
11782  *
11783  *      Issues: This follows what the old code did, in terms of accessing
11784  *		some of the partition info in the unit struct without holding
11785  *		the mutext.  This is a general issue, if the partition info
11786  *		can be altered while IO is in progress... as soon as we send
11787  *		a buf, its partitioning can be invalid before it gets to the
11788  *		device.  Probably the right fix is to move partitioning out
11789  *		of the driver entirely.
11790  */
11791 
11792 static void
11793 sd_mapblockaddr_iostart(int index, struct sd_lun *un, struct buf *bp)
11794 {
11795 	daddr_t	nblocks;	/* #blocks in the given partition */
11796 	daddr_t	blocknum;	/* Block number specified by the buf */
11797 	size_t	requested_nblocks;
11798 	size_t	available_nblocks;
11799 	int	partition;
11800 	diskaddr_t	partition_offset;
11801 	struct sd_xbuf *xp;
11802 
11803 
11804 	ASSERT(un != NULL);
11805 	ASSERT(bp != NULL);
11806 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11807 
11808 	SD_TRACE(SD_LOG_IO_PARTITION, un,
11809 	    "sd_mapblockaddr_iostart: entry: buf:0x%p\n", bp);
11810 
11811 	xp = SD_GET_XBUF(bp);
11812 	ASSERT(xp != NULL);
11813 
11814 	/*
11815 	 * If the geometry is not indicated as valid, attempt to access
11816 	 * the unit & verify the geometry/label. This can be the case for
11817 	 * removable-media devices, of if the device was opened in
11818 	 * NDELAY/NONBLOCK mode.
11819 	 */
11820 	if ((un->un_f_geometry_is_valid != TRUE) &&
11821 	    (sd_ready_and_valid(un) != SD_READY_VALID)) {
11822 		/*
11823 		 * For removable devices it is possible to start an I/O
11824 		 * without a media by opening the device in nodelay mode.
11825 		 * Also for writable CDs there can be many scenarios where
11826 		 * there is no geometry yet but volume manager is trying to
11827 		 * issue a read() just because it can see TOC on the CD. So
11828 		 * do not print a message for removables.
11829 		 */
11830 		if (!ISREMOVABLE(un)) {
11831 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
11832 			    "i/o to invalid geometry\n");
11833 		}
11834 		bioerror(bp, EIO);
11835 		bp->b_resid = bp->b_bcount;
11836 		SD_BEGIN_IODONE(index, un, bp);
11837 		return;
11838 	}
11839 
11840 	partition = SDPART(bp->b_edev);
11841 
11842 	/* #blocks in partition */
11843 	nblocks = un->un_map[partition].dkl_nblk;    /* #blocks in partition */
11844 
11845 	/* Use of a local variable potentially improves performance slightly */
11846 	partition_offset = un->un_offset[partition];
11847 
11848 	/*
11849 	 * blocknum is the starting block number of the request. At this
11850 	 * point it is still relative to the start of the minor device.
11851 	 */
11852 	blocknum = xp->xb_blkno;
11853 
11854 	/*
11855 	 * Legacy: If the starting block number is one past the last block
11856 	 * in the partition, do not set B_ERROR in the buf.
11857 	 */
11858 	if (blocknum == nblocks)  {
11859 		goto error_exit;
11860 	}
11861 
11862 	/*
11863 	 * Confirm that the first block of the request lies within the
11864 	 * partition limits. Also the requested number of bytes must be
11865 	 * a multiple of the system block size.
11866 	 */
11867 	if ((blocknum < 0) || (blocknum >= nblocks) ||
11868 	    ((bp->b_bcount & (un->un_sys_blocksize - 1)) != 0)) {
11869 		bp->b_flags |= B_ERROR;
11870 		goto error_exit;
11871 	}
11872 
11873 	/*
11874 	 * If the requsted # blocks exceeds the available # blocks, that
11875 	 * is an overrun of the partition.
11876 	 */
11877 	requested_nblocks = SD_BYTES2SYSBLOCKS(un, bp->b_bcount);
11878 	available_nblocks = (size_t)(nblocks - blocknum);
11879 	ASSERT(nblocks >= blocknum);
11880 
11881 	if (requested_nblocks > available_nblocks) {
11882 		/*
11883 		 * Allocate an "overrun" buf to allow the request to proceed
11884 		 * for the amount of space available in the partition. The
11885 		 * amount not transferred will be added into the b_resid
11886 		 * when the operation is complete. The overrun buf
11887 		 * replaces the original buf here, and the original buf
11888 		 * is saved inside the overrun buf, for later use.
11889 		 */
11890 		size_t resid = SD_SYSBLOCKS2BYTES(un,
11891 		    (offset_t)(requested_nblocks - available_nblocks));
11892 		size_t count = bp->b_bcount - resid;
11893 		/*
11894 		 * Note: count is an unsigned entity thus it'll NEVER
11895 		 * be less than 0 so ASSERT the original values are
11896 		 * correct.
11897 		 */
11898 		ASSERT(bp->b_bcount >= resid);
11899 
11900 		bp = sd_bioclone_alloc(bp, count, blocknum,
11901 			(int (*)(struct buf *)) sd_mapblockaddr_iodone);
11902 		xp = SD_GET_XBUF(bp); /* Update for 'new' bp! */
11903 		ASSERT(xp != NULL);
11904 	}
11905 
11906 	/* At this point there should be no residual for this buf. */
11907 	ASSERT(bp->b_resid == 0);
11908 
11909 	/* Convert the block number to an absolute address. */
11910 	xp->xb_blkno += partition_offset;
11911 
11912 	SD_NEXT_IOSTART(index, un, bp);
11913 
11914 	SD_TRACE(SD_LOG_IO_PARTITION, un,
11915 	    "sd_mapblockaddr_iostart: exit 0: buf:0x%p\n", bp);
11916 
11917 	return;
11918 
11919 error_exit:
11920 	bp->b_resid = bp->b_bcount;
11921 	SD_BEGIN_IODONE(index, un, bp);
11922 	SD_TRACE(SD_LOG_IO_PARTITION, un,
11923 	    "sd_mapblockaddr_iostart: exit 1: buf:0x%p\n", bp);
11924 }
11925 
11926 
11927 /*
11928  *    Function: sd_mapblockaddr_iodone
11929  *
11930  * Description: Completion-side processing for partition management.
11931  *
11932  *     Context: May be called under interrupt context
11933  */
11934 
11935 static void
11936 sd_mapblockaddr_iodone(int index, struct sd_lun *un, struct buf *bp)
11937 {
11938 	/* int	partition; */	/* Not used, see below. */
11939 	ASSERT(un != NULL);
11940 	ASSERT(bp != NULL);
11941 	ASSERT(!mutex_owned(SD_MUTEX(un)));
11942 
11943 	SD_TRACE(SD_LOG_IO_PARTITION, un,
11944 	    "sd_mapblockaddr_iodone: entry: buf:0x%p\n", bp);
11945 
11946 	if (bp->b_iodone == (int (*)(struct buf *)) sd_mapblockaddr_iodone) {
11947 		/*
11948 		 * We have an "overrun" buf to deal with...
11949 		 */
11950 		struct sd_xbuf	*xp;
11951 		struct buf	*obp;	/* ptr to the original buf */
11952 
11953 		xp = SD_GET_XBUF(bp);
11954 		ASSERT(xp != NULL);
11955 
11956 		/* Retrieve the pointer to the original buf */
11957 		obp = (struct buf *)xp->xb_private;
11958 		ASSERT(obp != NULL);
11959 
11960 		obp->b_resid = obp->b_bcount - (bp->b_bcount - bp->b_resid);
11961 		bioerror(obp, bp->b_error);
11962 
11963 		sd_bioclone_free(bp);
11964 
11965 		/*
11966 		 * Get back the original buf.
11967 		 * Note that since the restoration of xb_blkno below
11968 		 * was removed, the sd_xbuf is not needed.
11969 		 */
11970 		bp = obp;
11971 		/*
11972 		 * xp = SD_GET_XBUF(bp);
11973 		 * ASSERT(xp != NULL);
11974 		 */
11975 	}
11976 
11977 	/*
11978 	 * Convert sd->xb_blkno back to a minor-device relative value.
11979 	 * Note: this has been commented out, as it is not needed in the
11980 	 * current implementation of the driver (ie, since this function
11981 	 * is at the top of the layering chains, so the info will be
11982 	 * discarded) and it is in the "hot" IO path.
11983 	 *
11984 	 * partition = getminor(bp->b_edev) & SDPART_MASK;
11985 	 * xp->xb_blkno -= un->un_offset[partition];
11986 	 */
11987 
11988 	SD_NEXT_IODONE(index, un, bp);
11989 
11990 	SD_TRACE(SD_LOG_IO_PARTITION, un,
11991 	    "sd_mapblockaddr_iodone: exit: buf:0x%p\n", bp);
11992 }
11993 
11994 
11995 /*
11996  *    Function: sd_mapblocksize_iostart
11997  *
11998  * Description: Convert between system block size (un->un_sys_blocksize)
11999  *		and target block size (un->un_tgt_blocksize).
12000  *
12001  *     Context: Can sleep to allocate resources.
12002  *
12003  * Assumptions: A higher layer has already performed any partition validation,
12004  *		and converted the xp->xb_blkno to an absolute value relative
12005  *		to the start of the device.
12006  *
12007  *		It is also assumed that the higher layer has implemented
12008  *		an "overrun" mechanism for the case where the request would
12009  *		read/write beyond the end of a partition.  In this case we
12010  *		assume (and ASSERT) that bp->b_resid == 0.
12011  *
12012  *		Note: The implementation for this routine assumes the target
12013  *		block size remains constant between allocation and transport.
12014  */
12015 
12016 static void
12017 sd_mapblocksize_iostart(int index, struct sd_lun *un, struct buf *bp)
12018 {
12019 	struct sd_mapblocksize_info	*bsp;
12020 	struct sd_xbuf			*xp;
12021 	offset_t first_byte;
12022 	daddr_t	start_block, end_block;
12023 	daddr_t	request_bytes;
12024 	ushort_t is_aligned = FALSE;
12025 
12026 	ASSERT(un != NULL);
12027 	ASSERT(bp != NULL);
12028 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12029 	ASSERT(bp->b_resid == 0);
12030 
12031 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12032 	    "sd_mapblocksize_iostart: entry: buf:0x%p\n", bp);
12033 
12034 	/*
12035 	 * For a non-writable CD, a write request is an error
12036 	 */
12037 	if (ISCD(un) && ((bp->b_flags & B_READ) == 0) &&
12038 	    (un->un_f_mmc_writable_media == FALSE)) {
12039 		bioerror(bp, EIO);
12040 		bp->b_resid = bp->b_bcount;
12041 		SD_BEGIN_IODONE(index, un, bp);
12042 		return;
12043 	}
12044 
12045 	/*
12046 	 * We do not need a shadow buf if the device is using
12047 	 * un->un_sys_blocksize as its block size or if bcount == 0.
12048 	 * In this case there is no layer-private data block allocated.
12049 	 */
12050 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12051 	    (bp->b_bcount == 0)) {
12052 		goto done;
12053 	}
12054 
12055 #if defined(__i386) || defined(__amd64)
12056 	/* We do not support non-block-aligned transfers for ROD devices */
12057 	ASSERT(!ISROD(un));
12058 #endif
12059 
12060 	xp = SD_GET_XBUF(bp);
12061 	ASSERT(xp != NULL);
12062 
12063 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12064 	    "tgt_blocksize:0x%x sys_blocksize: 0x%x\n",
12065 	    un->un_tgt_blocksize, un->un_sys_blocksize);
12066 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12067 	    "request start block:0x%x\n", xp->xb_blkno);
12068 	SD_INFO(SD_LOG_IO_RMMEDIA, un, "sd_mapblocksize_iostart: "
12069 	    "request len:0x%x\n", bp->b_bcount);
12070 
12071 	/*
12072 	 * Allocate the layer-private data area for the mapblocksize layer.
12073 	 * Layers are allowed to use the xp_private member of the sd_xbuf
12074 	 * struct to store the pointer to their layer-private data block, but
12075 	 * each layer also has the responsibility of restoring the prior
12076 	 * contents of xb_private before returning the buf/xbuf to the
12077 	 * higher layer that sent it.
12078 	 *
12079 	 * Here we save the prior contents of xp->xb_private into the
12080 	 * bsp->mbs_oprivate field of our layer-private data area. This value
12081 	 * is restored by sd_mapblocksize_iodone() just prior to freeing up
12082 	 * the layer-private area and returning the buf/xbuf to the layer
12083 	 * that sent it.
12084 	 *
12085 	 * Note that here we use kmem_zalloc for the allocation as there are
12086 	 * parts of the mapblocksize code that expect certain fields to be
12087 	 * zero unless explicitly set to a required value.
12088 	 */
12089 	bsp = kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12090 	bsp->mbs_oprivate = xp->xb_private;
12091 	xp->xb_private = bsp;
12092 
12093 	/*
12094 	 * This treats the data on the disk (target) as an array of bytes.
12095 	 * first_byte is the byte offset, from the beginning of the device,
12096 	 * to the location of the request. This is converted from a
12097 	 * un->un_sys_blocksize block address to a byte offset, and then back
12098 	 * to a block address based upon a un->un_tgt_blocksize block size.
12099 	 *
12100 	 * xp->xb_blkno should be absolute upon entry into this function,
12101 	 * but, but it is based upon partitions that use the "system"
12102 	 * block size. It must be adjusted to reflect the block size of
12103 	 * the target.
12104 	 *
12105 	 * Note that end_block is actually the block that follows the last
12106 	 * block of the request, but that's what is needed for the computation.
12107 	 */
12108 	first_byte  = SD_SYSBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12109 	start_block = xp->xb_blkno = first_byte / un->un_tgt_blocksize;
12110 	end_block   = (first_byte + bp->b_bcount + un->un_tgt_blocksize - 1) /
12111 	    un->un_tgt_blocksize;
12112 
12113 	/* request_bytes is rounded up to a multiple of the target block size */
12114 	request_bytes = (end_block - start_block) * un->un_tgt_blocksize;
12115 
12116 	/*
12117 	 * See if the starting address of the request and the request
12118 	 * length are aligned on a un->un_tgt_blocksize boundary. If aligned
12119 	 * then we do not need to allocate a shadow buf to handle the request.
12120 	 */
12121 	if (((first_byte   % un->un_tgt_blocksize) == 0) &&
12122 	    ((bp->b_bcount % un->un_tgt_blocksize) == 0)) {
12123 		is_aligned = TRUE;
12124 	}
12125 
12126 	if ((bp->b_flags & B_READ) == 0) {
12127 		/*
12128 		 * Lock the range for a write operation. An aligned request is
12129 		 * considered a simple write; otherwise the request must be a
12130 		 * read-modify-write.
12131 		 */
12132 		bsp->mbs_wmp = sd_range_lock(un, start_block, end_block - 1,
12133 		    (is_aligned == TRUE) ? SD_WTYPE_SIMPLE : SD_WTYPE_RMW);
12134 	}
12135 
12136 	/*
12137 	 * Alloc a shadow buf if the request is not aligned. Also, this is
12138 	 * where the READ command is generated for a read-modify-write. (The
12139 	 * write phase is deferred until after the read completes.)
12140 	 */
12141 	if (is_aligned == FALSE) {
12142 
12143 		struct sd_mapblocksize_info	*shadow_bsp;
12144 		struct sd_xbuf	*shadow_xp;
12145 		struct buf	*shadow_bp;
12146 
12147 		/*
12148 		 * Allocate the shadow buf and it associated xbuf. Note that
12149 		 * after this call the xb_blkno value in both the original
12150 		 * buf's sd_xbuf _and_ the shadow buf's sd_xbuf will be the
12151 		 * same: absolute relative to the start of the device, and
12152 		 * adjusted for the target block size. The b_blkno in the
12153 		 * shadow buf will also be set to this value. We should never
12154 		 * change b_blkno in the original bp however.
12155 		 *
12156 		 * Note also that the shadow buf will always need to be a
12157 		 * READ command, regardless of whether the incoming command
12158 		 * is a READ or a WRITE.
12159 		 */
12160 		shadow_bp = sd_shadow_buf_alloc(bp, request_bytes, B_READ,
12161 		    xp->xb_blkno,
12162 		    (int (*)(struct buf *)) sd_mapblocksize_iodone);
12163 
12164 		shadow_xp = SD_GET_XBUF(shadow_bp);
12165 
12166 		/*
12167 		 * Allocate the layer-private data for the shadow buf.
12168 		 * (No need to preserve xb_private in the shadow xbuf.)
12169 		 */
12170 		shadow_xp->xb_private = shadow_bsp =
12171 		    kmem_zalloc(sizeof (struct sd_mapblocksize_info), KM_SLEEP);
12172 
12173 		/*
12174 		 * bsp->mbs_copy_offset is used later by sd_mapblocksize_iodone
12175 		 * to figure out where the start of the user data is (based upon
12176 		 * the system block size) in the data returned by the READ
12177 		 * command (which will be based upon the target blocksize). Note
12178 		 * that this is only really used if the request is unaligned.
12179 		 */
12180 		bsp->mbs_copy_offset = (ssize_t)(first_byte -
12181 		    ((offset_t)xp->xb_blkno * un->un_tgt_blocksize));
12182 		ASSERT((bsp->mbs_copy_offset >= 0) &&
12183 		    (bsp->mbs_copy_offset < un->un_tgt_blocksize));
12184 
12185 		shadow_bsp->mbs_copy_offset = bsp->mbs_copy_offset;
12186 
12187 		shadow_bsp->mbs_layer_index = bsp->mbs_layer_index = index;
12188 
12189 		/* Transfer the wmap (if any) to the shadow buf */
12190 		shadow_bsp->mbs_wmp = bsp->mbs_wmp;
12191 		bsp->mbs_wmp = NULL;
12192 
12193 		/*
12194 		 * The shadow buf goes on from here in place of the
12195 		 * original buf.
12196 		 */
12197 		shadow_bsp->mbs_orig_bp = bp;
12198 		bp = shadow_bp;
12199 	}
12200 
12201 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12202 	    "sd_mapblocksize_iostart: tgt start block:0x%x\n", xp->xb_blkno);
12203 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12204 	    "sd_mapblocksize_iostart: tgt request len:0x%x\n",
12205 	    request_bytes);
12206 	SD_INFO(SD_LOG_IO_RMMEDIA, un,
12207 	    "sd_mapblocksize_iostart: shadow buf:0x%x\n", bp);
12208 
12209 done:
12210 	SD_NEXT_IOSTART(index, un, bp);
12211 
12212 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12213 	    "sd_mapblocksize_iostart: exit: buf:0x%p\n", bp);
12214 }
12215 
12216 
12217 /*
12218  *    Function: sd_mapblocksize_iodone
12219  *
12220  * Description: Completion side processing for block-size mapping.
12221  *
12222  *     Context: May be called under interrupt context
12223  */
12224 
12225 static void
12226 sd_mapblocksize_iodone(int index, struct sd_lun *un, struct buf *bp)
12227 {
12228 	struct sd_mapblocksize_info	*bsp;
12229 	struct sd_xbuf	*xp;
12230 	struct sd_xbuf	*orig_xp;	/* sd_xbuf for the original buf */
12231 	struct buf	*orig_bp;	/* ptr to the original buf */
12232 	offset_t	shadow_end;
12233 	offset_t	request_end;
12234 	offset_t	shadow_start;
12235 	ssize_t		copy_offset;
12236 	size_t		copy_length;
12237 	size_t		shortfall;
12238 	uint_t		is_write;	/* TRUE if this bp is a WRITE */
12239 	uint_t		has_wmap;	/* TRUE is this bp has a wmap */
12240 
12241 	ASSERT(un != NULL);
12242 	ASSERT(bp != NULL);
12243 
12244 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
12245 	    "sd_mapblocksize_iodone: entry: buf:0x%p\n", bp);
12246 
12247 	/*
12248 	 * There is no shadow buf or layer-private data if the target is
12249 	 * using un->un_sys_blocksize as its block size or if bcount == 0.
12250 	 */
12251 	if ((un->un_tgt_blocksize == un->un_sys_blocksize) ||
12252 	    (bp->b_bcount == 0)) {
12253 		goto exit;
12254 	}
12255 
12256 	xp = SD_GET_XBUF(bp);
12257 	ASSERT(xp != NULL);
12258 
12259 	/* Retrieve the pointer to the layer-private data area from the xbuf. */
12260 	bsp = xp->xb_private;
12261 
12262 	is_write = ((bp->b_flags & B_READ) == 0) ? TRUE : FALSE;
12263 	has_wmap = (bsp->mbs_wmp != NULL) ? TRUE : FALSE;
12264 
12265 	if (is_write) {
12266 		/*
12267 		 * For a WRITE request we must free up the block range that
12268 		 * we have locked up.  This holds regardless of whether this is
12269 		 * an aligned write request or a read-modify-write request.
12270 		 */
12271 		sd_range_unlock(un, bsp->mbs_wmp);
12272 		bsp->mbs_wmp = NULL;
12273 	}
12274 
12275 	if ((bp->b_iodone != (int(*)(struct buf *))sd_mapblocksize_iodone)) {
12276 		/*
12277 		 * An aligned read or write command will have no shadow buf;
12278 		 * there is not much else to do with it.
12279 		 */
12280 		goto done;
12281 	}
12282 
12283 	orig_bp = bsp->mbs_orig_bp;
12284 	ASSERT(orig_bp != NULL);
12285 	orig_xp = SD_GET_XBUF(orig_bp);
12286 	ASSERT(orig_xp != NULL);
12287 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12288 
12289 	if (!is_write && has_wmap) {
12290 		/*
12291 		 * A READ with a wmap means this is the READ phase of a
12292 		 * read-modify-write. If an error occurred on the READ then
12293 		 * we do not proceed with the WRITE phase or copy any data.
12294 		 * Just release the write maps and return with an error.
12295 		 */
12296 		if ((bp->b_resid != 0) || (bp->b_error != 0)) {
12297 			orig_bp->b_resid = orig_bp->b_bcount;
12298 			bioerror(orig_bp, bp->b_error);
12299 			sd_range_unlock(un, bsp->mbs_wmp);
12300 			goto freebuf_done;
12301 		}
12302 	}
12303 
12304 	/*
12305 	 * Here is where we set up to copy the data from the shadow buf
12306 	 * into the space associated with the original buf.
12307 	 *
12308 	 * To deal with the conversion between block sizes, these
12309 	 * computations treat the data as an array of bytes, with the
12310 	 * first byte (byte 0) corresponding to the first byte in the
12311 	 * first block on the disk.
12312 	 */
12313 
12314 	/*
12315 	 * shadow_start and shadow_len indicate the location and size of
12316 	 * the data returned with the shadow IO request.
12317 	 */
12318 	shadow_start  = SD_TGTBLOCKS2BYTES(un, (offset_t)xp->xb_blkno);
12319 	shadow_end    = shadow_start + bp->b_bcount - bp->b_resid;
12320 
12321 	/*
12322 	 * copy_offset gives the offset (in bytes) from the start of the first
12323 	 * block of the READ request to the beginning of the data.  We retrieve
12324 	 * this value from xb_pktp in the ORIGINAL xbuf, as it has been saved
12325 	 * there by sd_mapblockize_iostart(). copy_length gives the amount of
12326 	 * data to be copied (in bytes).
12327 	 */
12328 	copy_offset  = bsp->mbs_copy_offset;
12329 	ASSERT((copy_offset >= 0) && (copy_offset < un->un_tgt_blocksize));
12330 	copy_length  = orig_bp->b_bcount;
12331 	request_end  = shadow_start + copy_offset + orig_bp->b_bcount;
12332 
12333 	/*
12334 	 * Set up the resid and error fields of orig_bp as appropriate.
12335 	 */
12336 	if (shadow_end >= request_end) {
12337 		/* We got all the requested data; set resid to zero */
12338 		orig_bp->b_resid = 0;
12339 	} else {
12340 		/*
12341 		 * We failed to get enough data to fully satisfy the original
12342 		 * request. Just copy back whatever data we got and set
12343 		 * up the residual and error code as required.
12344 		 *
12345 		 * 'shortfall' is the amount by which the data received with the
12346 		 * shadow buf has "fallen short" of the requested amount.
12347 		 */
12348 		shortfall = (size_t)(request_end - shadow_end);
12349 
12350 		if (shortfall > orig_bp->b_bcount) {
12351 			/*
12352 			 * We did not get enough data to even partially
12353 			 * fulfill the original request.  The residual is
12354 			 * equal to the amount requested.
12355 			 */
12356 			orig_bp->b_resid = orig_bp->b_bcount;
12357 		} else {
12358 			/*
12359 			 * We did not get all the data that we requested
12360 			 * from the device, but we will try to return what
12361 			 * portion we did get.
12362 			 */
12363 			orig_bp->b_resid = shortfall;
12364 		}
12365 		ASSERT(copy_length >= orig_bp->b_resid);
12366 		copy_length  -= orig_bp->b_resid;
12367 	}
12368 
12369 	/* Propagate the error code from the shadow buf to the original buf */
12370 	bioerror(orig_bp, bp->b_error);
12371 
12372 	if (is_write) {
12373 		goto freebuf_done;	/* No data copying for a WRITE */
12374 	}
12375 
12376 	if (has_wmap) {
12377 		/*
12378 		 * This is a READ command from the READ phase of a
12379 		 * read-modify-write request. We have to copy the data given
12380 		 * by the user OVER the data returned by the READ command,
12381 		 * then convert the command from a READ to a WRITE and send
12382 		 * it back to the target.
12383 		 */
12384 		bcopy(orig_bp->b_un.b_addr, bp->b_un.b_addr + copy_offset,
12385 		    copy_length);
12386 
12387 		bp->b_flags &= ~((int)B_READ);	/* Convert to a WRITE */
12388 
12389 		/*
12390 		 * Dispatch the WRITE command to the taskq thread, which
12391 		 * will in turn send the command to the target. When the
12392 		 * WRITE command completes, we (sd_mapblocksize_iodone())
12393 		 * will get called again as part of the iodone chain
12394 		 * processing for it. Note that we will still be dealing
12395 		 * with the shadow buf at that point.
12396 		 */
12397 		if (taskq_dispatch(sd_wmr_tq, sd_read_modify_write_task, bp,
12398 		    KM_NOSLEEP) != 0) {
12399 			/*
12400 			 * Dispatch was successful so we are done. Return
12401 			 * without going any higher up the iodone chain. Do
12402 			 * not free up any layer-private data until after the
12403 			 * WRITE completes.
12404 			 */
12405 			return;
12406 		}
12407 
12408 		/*
12409 		 * Dispatch of the WRITE command failed; set up the error
12410 		 * condition and send this IO back up the iodone chain.
12411 		 */
12412 		bioerror(orig_bp, EIO);
12413 		orig_bp->b_resid = orig_bp->b_bcount;
12414 
12415 	} else {
12416 		/*
12417 		 * This is a regular READ request (ie, not a RMW). Copy the
12418 		 * data from the shadow buf into the original buf. The
12419 		 * copy_offset compensates for any "misalignment" between the
12420 		 * shadow buf (with its un->un_tgt_blocksize blocks) and the
12421 		 * original buf (with its un->un_sys_blocksize blocks).
12422 		 */
12423 		bcopy(bp->b_un.b_addr + copy_offset, orig_bp->b_un.b_addr,
12424 		    copy_length);
12425 	}
12426 
12427 freebuf_done:
12428 
12429 	/*
12430 	 * At this point we still have both the shadow buf AND the original
12431 	 * buf to deal with, as well as the layer-private data area in each.
12432 	 * Local variables are as follows:
12433 	 *
12434 	 * bp -- points to shadow buf
12435 	 * xp -- points to xbuf of shadow buf
12436 	 * bsp -- points to layer-private data area of shadow buf
12437 	 * orig_bp -- points to original buf
12438 	 *
12439 	 * First free the shadow buf and its associated xbuf, then free the
12440 	 * layer-private data area from the shadow buf. There is no need to
12441 	 * restore xb_private in the shadow xbuf.
12442 	 */
12443 	sd_shadow_buf_free(bp);
12444 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
12445 
12446 	/*
12447 	 * Now update the local variables to point to the original buf, xbuf,
12448 	 * and layer-private area.
12449 	 */
12450 	bp = orig_bp;
12451 	xp = SD_GET_XBUF(bp);
12452 	ASSERT(xp != NULL);
12453 	ASSERT(xp == orig_xp);
12454 	bsp = xp->xb_private;
12455 	ASSERT(bsp != NULL);
12456 
12457 done:
12458 	/*
12459 	 * Restore xb_private to whatever it was set to by the next higher
12460 	 * layer in the chain, then free the layer-private data area.
12461 	 */
12462 	xp->xb_private = bsp->mbs_oprivate;
12463 	kmem_free(bsp, sizeof (struct sd_mapblocksize_info));
12464 
12465 exit:
12466 	SD_TRACE(SD_LOG_IO_RMMEDIA, SD_GET_UN(bp),
12467 	    "sd_mapblocksize_iodone: calling SD_NEXT_IODONE: buf:0x%p\n", bp);
12468 
12469 	SD_NEXT_IODONE(index, un, bp);
12470 }
12471 
12472 
12473 /*
12474  *    Function: sd_checksum_iostart
12475  *
12476  * Description: A stub function for a layer that's currently not used.
12477  *		For now just a placeholder.
12478  *
12479  *     Context: Kernel thread context
12480  */
12481 
12482 static void
12483 sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp)
12484 {
12485 	ASSERT(un != NULL);
12486 	ASSERT(bp != NULL);
12487 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12488 	SD_NEXT_IOSTART(index, un, bp);
12489 }
12490 
12491 
12492 /*
12493  *    Function: sd_checksum_iodone
12494  *
12495  * Description: A stub function for a layer that's currently not used.
12496  *		For now just a placeholder.
12497  *
12498  *     Context: May be called under interrupt context
12499  */
12500 
12501 static void
12502 sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp)
12503 {
12504 	ASSERT(un != NULL);
12505 	ASSERT(bp != NULL);
12506 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12507 	SD_NEXT_IODONE(index, un, bp);
12508 }
12509 
12510 
12511 /*
12512  *    Function: sd_checksum_uscsi_iostart
12513  *
12514  * Description: A stub function for a layer that's currently not used.
12515  *		For now just a placeholder.
12516  *
12517  *     Context: Kernel thread context
12518  */
12519 
12520 static void
12521 sd_checksum_uscsi_iostart(int index, struct sd_lun *un, struct buf *bp)
12522 {
12523 	ASSERT(un != NULL);
12524 	ASSERT(bp != NULL);
12525 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12526 	SD_NEXT_IOSTART(index, un, bp);
12527 }
12528 
12529 
12530 /*
12531  *    Function: sd_checksum_uscsi_iodone
12532  *
12533  * Description: A stub function for a layer that's currently not used.
12534  *		For now just a placeholder.
12535  *
12536  *     Context: May be called under interrupt context
12537  */
12538 
12539 static void
12540 sd_checksum_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp)
12541 {
12542 	ASSERT(un != NULL);
12543 	ASSERT(bp != NULL);
12544 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12545 	SD_NEXT_IODONE(index, un, bp);
12546 }
12547 
12548 
12549 /*
12550  *    Function: sd_pm_iostart
12551  *
12552  * Description: iostart-side routine for Power mangement.
12553  *
12554  *     Context: Kernel thread context
12555  */
12556 
12557 static void
12558 sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp)
12559 {
12560 	ASSERT(un != NULL);
12561 	ASSERT(bp != NULL);
12562 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12563 	ASSERT(!mutex_owned(&un->un_pm_mutex));
12564 
12565 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: entry\n");
12566 
12567 	if (sd_pm_entry(un) != DDI_SUCCESS) {
12568 		/*
12569 		 * Set up to return the failed buf back up the 'iodone'
12570 		 * side of the calling chain.
12571 		 */
12572 		bioerror(bp, EIO);
12573 		bp->b_resid = bp->b_bcount;
12574 
12575 		SD_BEGIN_IODONE(index, un, bp);
12576 
12577 		SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
12578 		return;
12579 	}
12580 
12581 	SD_NEXT_IOSTART(index, un, bp);
12582 
12583 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iostart: exit\n");
12584 }
12585 
12586 
12587 /*
12588  *    Function: sd_pm_iodone
12589  *
12590  * Description: iodone-side routine for power mangement.
12591  *
12592  *     Context: may be called from interrupt context
12593  */
12594 
12595 static void
12596 sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp)
12597 {
12598 	ASSERT(un != NULL);
12599 	ASSERT(bp != NULL);
12600 	ASSERT(!mutex_owned(&un->un_pm_mutex));
12601 
12602 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: entry\n");
12603 
12604 	/*
12605 	 * After attach the following flag is only read, so don't
12606 	 * take the penalty of acquiring a mutex for it.
12607 	 */
12608 	if (un->un_f_pm_is_enabled == TRUE) {
12609 		sd_pm_exit(un);
12610 	}
12611 
12612 	SD_NEXT_IODONE(index, un, bp);
12613 
12614 	SD_TRACE(SD_LOG_IO_PM, un, "sd_pm_iodone: exit\n");
12615 }
12616 
12617 
12618 /*
12619  *    Function: sd_core_iostart
12620  *
12621  * Description: Primary driver function for enqueuing buf(9S) structs from
12622  *		the system and initiating IO to the target device
12623  *
12624  *     Context: Kernel thread context. Can sleep.
12625  *
12626  * Assumptions:  - The given xp->xb_blkno is absolute
12627  *		   (ie, relative to the start of the device).
12628  *		 - The IO is to be done using the native blocksize of
12629  *		   the device, as specified in un->un_tgt_blocksize.
12630  */
12631 /* ARGSUSED */
12632 static void
12633 sd_core_iostart(int index, struct sd_lun *un, struct buf *bp)
12634 {
12635 	struct sd_xbuf *xp;
12636 
12637 	ASSERT(un != NULL);
12638 	ASSERT(bp != NULL);
12639 	ASSERT(!mutex_owned(SD_MUTEX(un)));
12640 	ASSERT(bp->b_resid == 0);
12641 
12642 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: entry: bp:0x%p\n", bp);
12643 
12644 	xp = SD_GET_XBUF(bp);
12645 	ASSERT(xp != NULL);
12646 
12647 	mutex_enter(SD_MUTEX(un));
12648 
12649 	/*
12650 	 * If we are currently in the failfast state, fail any new IO
12651 	 * that has B_FAILFAST set, then return.
12652 	 */
12653 	if ((bp->b_flags & B_FAILFAST) &&
12654 	    (un->un_failfast_state == SD_FAILFAST_ACTIVE)) {
12655 		mutex_exit(SD_MUTEX(un));
12656 		bioerror(bp, EIO);
12657 		bp->b_resid = bp->b_bcount;
12658 		SD_BEGIN_IODONE(index, un, bp);
12659 		return;
12660 	}
12661 
12662 	if (SD_IS_DIRECT_PRIORITY(xp)) {
12663 		/*
12664 		 * Priority command -- transport it immediately.
12665 		 *
12666 		 * Note: We may want to assert that USCSI_DIAGNOSE is set,
12667 		 * because all direct priority commands should be associated
12668 		 * with error recovery actions which we don't want to retry.
12669 		 */
12670 		sd_start_cmds(un, bp);
12671 	} else {
12672 		/*
12673 		 * Normal command -- add it to the wait queue, then start
12674 		 * transporting commands from the wait queue.
12675 		 */
12676 		sd_add_buf_to_waitq(un, bp);
12677 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
12678 		sd_start_cmds(un, NULL);
12679 	}
12680 
12681 	mutex_exit(SD_MUTEX(un));
12682 
12683 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_core_iostart: exit: bp:0x%p\n", bp);
12684 }
12685 
12686 
12687 /*
12688  *    Function: sd_init_cdb_limits
12689  *
12690  * Description: This is to handle scsi_pkt initialization differences
12691  *		between the driver platforms.
12692  *
12693  *		Legacy behaviors:
12694  *
12695  *		If the block number or the sector count exceeds the
12696  *		capabilities of a Group 0 command, shift over to a
12697  *		Group 1 command. We don't blindly use Group 1
12698  *		commands because a) some drives (CDC Wren IVs) get a
12699  *		bit confused, and b) there is probably a fair amount
12700  *		of speed difference for a target to receive and decode
12701  *		a 10 byte command instead of a 6 byte command.
12702  *
12703  *		The xfer time difference of 6 vs 10 byte CDBs is
12704  *		still significant so this code is still worthwhile.
12705  *		10 byte CDBs are very inefficient with the fas HBA driver
12706  *		and older disks. Each CDB byte took 1 usec with some
12707  *		popular disks.
12708  *
12709  *     Context: Must be called at attach time
12710  */
12711 
12712 static void
12713 sd_init_cdb_limits(struct sd_lun *un)
12714 {
12715 	/*
12716 	 * Use CDB_GROUP1 commands for most devices except for
12717 	 * parallel SCSI fixed drives in which case we get better
12718 	 * performance using CDB_GROUP0 commands (where applicable).
12719 	 */
12720 	un->un_mincdb = SD_CDB_GROUP1;
12721 #if !defined(__fibre)
12722 	if (!un->un_f_is_fibre && !un->un_f_cfg_is_atapi && !ISROD(un) &&
12723 	    !ISREMOVABLE(un)) {
12724 		un->un_mincdb = SD_CDB_GROUP0;
12725 	}
12726 #endif
12727 
12728 	/*
12729 	 * Use CDB_GROUP5 commands for removable devices.  Use CDB_GROUP4
12730 	 * commands for fixed disks unless we are building for a 32 bit
12731 	 * kernel.
12732 	 */
12733 #ifdef _LP64
12734 	un->un_maxcdb = (ISREMOVABLE(un)) ? SD_CDB_GROUP5 : SD_CDB_GROUP4;
12735 #else
12736 	un->un_maxcdb = (ISREMOVABLE(un)) ? SD_CDB_GROUP5 : SD_CDB_GROUP1;
12737 #endif
12738 
12739 	/*
12740 	 * x86 systems require the PKT_DMA_PARTIAL flag
12741 	 */
12742 #if defined(__x86)
12743 	un->un_pkt_flags = PKT_DMA_PARTIAL;
12744 #else
12745 	un->un_pkt_flags = 0;
12746 #endif
12747 
12748 	un->un_status_len = (int)((un->un_f_arq_enabled == TRUE)
12749 	    ? sizeof (struct scsi_arq_status) : 1);
12750 	un->un_cmd_timeout = (ushort_t)sd_io_time;
12751 	un->un_uscsi_timeout = ((ISCD(un)) ? 2 : 1) * un->un_cmd_timeout;
12752 }
12753 
12754 
12755 /*
12756  *    Function: sd_initpkt_for_buf
12757  *
12758  * Description: Allocate and initialize for transport a scsi_pkt struct,
12759  *		based upon the info specified in the given buf struct.
12760  *
12761  *		Assumes the xb_blkno in the request is absolute (ie,
12762  *		relative to the start of the device (NOT partition!).
12763  *		Also assumes that the request is using the native block
12764  *		size of the device (as returned by the READ CAPACITY
12765  *		command).
12766  *
12767  * Return Code: SD_PKT_ALLOC_SUCCESS
12768  *		SD_PKT_ALLOC_FAILURE
12769  *		SD_PKT_ALLOC_FAILURE_NO_DMA
12770  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
12771  *
12772  *     Context: Kernel thread and may be called from software interrupt context
12773  *		as part of a sdrunout callback. This function may not block or
12774  *		call routines that block
12775  */
12776 
12777 static int
12778 sd_initpkt_for_buf(struct buf *bp, struct scsi_pkt **pktpp)
12779 {
12780 	struct sd_xbuf	*xp;
12781 	struct scsi_pkt *pktp = NULL;
12782 	struct sd_lun	*un;
12783 	size_t		blockcount;
12784 	daddr_t		startblock;
12785 	int		rval;
12786 	int		cmd_flags;
12787 
12788 	ASSERT(bp != NULL);
12789 	ASSERT(pktpp != NULL);
12790 	xp = SD_GET_XBUF(bp);
12791 	ASSERT(xp != NULL);
12792 	un = SD_GET_UN(bp);
12793 	ASSERT(un != NULL);
12794 	ASSERT(mutex_owned(SD_MUTEX(un)));
12795 	ASSERT(bp->b_resid == 0);
12796 
12797 	SD_TRACE(SD_LOG_IO_CORE, un,
12798 	    "sd_initpkt_for_buf: entry: buf:0x%p\n", bp);
12799 
12800 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
12801 	if (xp->xb_pkt_flags & SD_XB_DMA_FREED) {
12802 		/*
12803 		 * Already have a scsi_pkt -- just need DMA resources.
12804 		 * We must recompute the CDB in case the mapping returns
12805 		 * a nonzero pkt_resid.
12806 		 * Note: if this is a portion of a PKT_DMA_PARTIAL transfer
12807 		 * that is being retried, the unmap/remap of the DMA resouces
12808 		 * will result in the entire transfer starting over again
12809 		 * from the very first block.
12810 		 */
12811 		ASSERT(xp->xb_pktp != NULL);
12812 		pktp = xp->xb_pktp;
12813 	} else {
12814 		pktp = NULL;
12815 	}
12816 #endif /* __i386 || __amd64 */
12817 
12818 	startblock = xp->xb_blkno;	/* Absolute block num. */
12819 	blockcount = SD_BYTES2TGTBLOCKS(un, bp->b_bcount);
12820 
12821 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
12822 
12823 	cmd_flags = un->un_pkt_flags | (xp->xb_pkt_flags & SD_XB_INITPKT_MASK);
12824 
12825 #else
12826 
12827 	cmd_flags = un->un_pkt_flags | xp->xb_pkt_flags;
12828 
12829 #endif
12830 
12831 	/*
12832 	 * sd_setup_rw_pkt will determine the appropriate CDB group to use,
12833 	 * call scsi_init_pkt, and build the CDB.
12834 	 */
12835 	rval = sd_setup_rw_pkt(un, &pktp, bp,
12836 	    cmd_flags, sdrunout, (caddr_t)un,
12837 	    startblock, blockcount);
12838 
12839 	if (rval == 0) {
12840 		/*
12841 		 * Success.
12842 		 *
12843 		 * If partial DMA is being used and required for this transfer.
12844 		 * set it up here.
12845 		 */
12846 		if ((un->un_pkt_flags & PKT_DMA_PARTIAL) != 0 &&
12847 		    (pktp->pkt_resid != 0)) {
12848 
12849 			/*
12850 			 * Save the CDB length and pkt_resid for the
12851 			 * next xfer
12852 			 */
12853 			xp->xb_dma_resid = pktp->pkt_resid;
12854 
12855 			/* rezero resid */
12856 			pktp->pkt_resid = 0;
12857 
12858 		} else {
12859 			xp->xb_dma_resid = 0;
12860 		}
12861 
12862 		pktp->pkt_flags = un->un_tagflags;
12863 		pktp->pkt_time  = un->un_cmd_timeout;
12864 		pktp->pkt_comp  = sdintr;
12865 
12866 		pktp->pkt_private = bp;
12867 		*pktpp = pktp;
12868 
12869 		SD_TRACE(SD_LOG_IO_CORE, un,
12870 		    "sd_initpkt_for_buf: exit: buf:0x%p\n", bp);
12871 
12872 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
12873 		xp->xb_pkt_flags &= ~SD_XB_DMA_FREED;
12874 #endif
12875 
12876 		return (SD_PKT_ALLOC_SUCCESS);
12877 
12878 	}
12879 
12880 	/*
12881 	 * SD_PKT_ALLOC_FAILURE is the only expected failure code
12882 	 * from sd_setup_rw_pkt.
12883 	 */
12884 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
12885 
12886 	if (rval == SD_PKT_ALLOC_FAILURE) {
12887 		*pktpp = NULL;
12888 		/*
12889 		 * Set the driver state to RWAIT to indicate the driver
12890 		 * is waiting on resource allocations. The driver will not
12891 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
12892 		 */
12893 		New_state(un, SD_STATE_RWAIT);
12894 
12895 		SD_ERROR(SD_LOG_IO_CORE, un,
12896 		    "sd_initpkt_for_buf: No pktp. exit bp:0x%p\n", bp);
12897 
12898 		if ((bp->b_flags & B_ERROR) != 0) {
12899 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
12900 		}
12901 		return (SD_PKT_ALLOC_FAILURE);
12902 	} else {
12903 		/*
12904 		 * PKT_ALLOC_FAILURE_CDB_TOO_SMALL
12905 		 *
12906 		 * This should never happen.  Maybe someone messed with the
12907 		 * kernel's minphys?
12908 		 */
12909 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
12910 		    "Request rejected: too large for CDB: "
12911 		    "lba:0x%08lx  len:0x%08lx\n", startblock, blockcount);
12912 		SD_ERROR(SD_LOG_IO_CORE, un,
12913 		    "sd_initpkt_for_buf: No cp. exit bp:0x%p\n", bp);
12914 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
12915 
12916 	}
12917 }
12918 
12919 
12920 /*
12921  *    Function: sd_destroypkt_for_buf
12922  *
12923  * Description: Free the scsi_pkt(9S) for the given bp (buf IO processing).
12924  *
12925  *     Context: Kernel thread or interrupt context
12926  */
12927 
12928 static void
12929 sd_destroypkt_for_buf(struct buf *bp)
12930 {
12931 	ASSERT(bp != NULL);
12932 	ASSERT(SD_GET_UN(bp) != NULL);
12933 
12934 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
12935 	    "sd_destroypkt_for_buf: entry: buf:0x%p\n", bp);
12936 
12937 	ASSERT(SD_GET_PKTP(bp) != NULL);
12938 	scsi_destroy_pkt(SD_GET_PKTP(bp));
12939 
12940 	SD_TRACE(SD_LOG_IO_CORE, SD_GET_UN(bp),
12941 	    "sd_destroypkt_for_buf: exit: buf:0x%p\n", bp);
12942 }
12943 
12944 /*
12945  *    Function: sd_setup_rw_pkt
12946  *
12947  * Description: Determines appropriate CDB group for the requested LBA
12948  *		and transfer length, calls scsi_init_pkt, and builds
12949  *		the CDB.  Do not use for partial DMA transfers except
12950  *		for the initial transfer since the CDB size must
12951  *		remain constant.
12952  *
12953  *     Context: Kernel thread and may be called from software interrupt
12954  *		context as part of a sdrunout callback. This function may not
12955  *		block or call routines that block
12956  */
12957 
12958 
12959 int
12960 sd_setup_rw_pkt(struct sd_lun *un,
12961     struct scsi_pkt **pktpp, struct buf *bp, int flags,
12962     int (*callback)(caddr_t), caddr_t callback_arg,
12963     diskaddr_t lba, uint32_t blockcount)
12964 {
12965 	struct scsi_pkt *return_pktp;
12966 	union scsi_cdb *cdbp;
12967 	struct sd_cdbinfo *cp = NULL;
12968 	int i;
12969 
12970 	/*
12971 	 * See which size CDB to use, based upon the request.
12972 	 */
12973 	for (i = un->un_mincdb; i <= un->un_maxcdb; i++) {
12974 
12975 		/*
12976 		 * Check lba and block count against sd_cdbtab limits.
12977 		 * In the partial DMA case, we have to use the same size
12978 		 * CDB for all the transfers.  Check lba + blockcount
12979 		 * against the max LBA so we know that segment of the
12980 		 * transfer can use the CDB we select.
12981 		 */
12982 		if ((lba + blockcount - 1 <= sd_cdbtab[i].sc_maxlba) &&
12983 		    (blockcount <= sd_cdbtab[i].sc_maxlen)) {
12984 
12985 			/*
12986 			 * The command will fit into the CDB type
12987 			 * specified by sd_cdbtab[i].
12988 			 */
12989 			cp = sd_cdbtab + i;
12990 
12991 			/*
12992 			 * Call scsi_init_pkt so we can fill in the
12993 			 * CDB.
12994 			 */
12995 			return_pktp = scsi_init_pkt(SD_ADDRESS(un), *pktpp,
12996 			    bp, cp->sc_grpcode, un->un_status_len, 0,
12997 			    flags, callback, callback_arg);
12998 
12999 			if (return_pktp != NULL) {
13000 
13001 				/*
13002 				 * Return new value of pkt
13003 				 */
13004 				*pktpp = return_pktp;
13005 
13006 				/*
13007 				 * To be safe, zero the CDB insuring there is
13008 				 * no leftover data from a previous command.
13009 				 */
13010 				bzero(return_pktp->pkt_cdbp, cp->sc_grpcode);
13011 
13012 				/*
13013 				 * Handle partial DMA mapping
13014 				 */
13015 				if (return_pktp->pkt_resid != 0) {
13016 
13017 					/*
13018 					 * Not going to xfer as many blocks as
13019 					 * originally expected
13020 					 */
13021 					blockcount -=
13022 					    SD_BYTES2TGTBLOCKS(un,
13023 						return_pktp->pkt_resid);
13024 				}
13025 
13026 				cdbp = (union scsi_cdb *)return_pktp->pkt_cdbp;
13027 
13028 				/*
13029 				 * Set command byte based on the CDB
13030 				 * type we matched.
13031 				 */
13032 				cdbp->scc_cmd = cp->sc_grpmask |
13033 				    ((bp->b_flags & B_READ) ?
13034 					SCMD_READ : SCMD_WRITE);
13035 
13036 				sd_fill_scsi1_lun(un, return_pktp);
13037 
13038 				/*
13039 				 * Fill in LBA and length
13040 				 */
13041 				ASSERT((cp->sc_grpcode == CDB_GROUP1) ||
13042 				    (cp->sc_grpcode == CDB_GROUP4) ||
13043 				    (cp->sc_grpcode == CDB_GROUP0) ||
13044 				    (cp->sc_grpcode == CDB_GROUP5));
13045 
13046 				if (cp->sc_grpcode == CDB_GROUP1) {
13047 					FORMG1ADDR(cdbp, lba);
13048 					FORMG1COUNT(cdbp, blockcount);
13049 					return (0);
13050 				} else if (cp->sc_grpcode == CDB_GROUP4) {
13051 					FORMG4LONGADDR(cdbp, lba);
13052 					FORMG4COUNT(cdbp, blockcount);
13053 					return (0);
13054 				} else if (cp->sc_grpcode == CDB_GROUP0) {
13055 					FORMG0ADDR(cdbp, lba);
13056 					FORMG0COUNT(cdbp, blockcount);
13057 					return (0);
13058 				} else if (cp->sc_grpcode == CDB_GROUP5) {
13059 					FORMG5ADDR(cdbp, lba);
13060 					FORMG5COUNT(cdbp, blockcount);
13061 					return (0);
13062 				}
13063 
13064 				/*
13065 				 * It should be impossible to not match one
13066 				 * of the CDB types above, so we should never
13067 				 * reach this point.  Set the CDB command byte
13068 				 * to test-unit-ready to avoid writing
13069 				 * to somewhere we don't intend.
13070 				 */
13071 				cdbp->scc_cmd = SCMD_TEST_UNIT_READY;
13072 				return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13073 			} else {
13074 				/*
13075 				 * Couldn't get scsi_pkt
13076 				 */
13077 				return (SD_PKT_ALLOC_FAILURE);
13078 			}
13079 		}
13080 	}
13081 
13082 	/*
13083 	 * None of the available CDB types were suitable.  This really
13084 	 * should never happen:  on a 64 bit system we support
13085 	 * READ16/WRITE16 which will hold an entire 64 bit disk address
13086 	 * and on a 32 bit system we will refuse to bind to a device
13087 	 * larger than 2TB so addresses will never be larger than 32 bits.
13088 	 */
13089 	return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13090 }
13091 
13092 /*
13093  *    Function: sd_setup_next_rw_pkt
13094  *
13095  * Description: Setup packet for partial DMA transfers, except for the
13096  * 		initial transfer.  sd_setup_rw_pkt should be used for
13097  *		the initial transfer.
13098  *
13099  *     Context: Kernel thread and may be called from interrupt context.
13100  */
13101 
13102 int
13103 sd_setup_next_rw_pkt(struct sd_lun *un,
13104     struct scsi_pkt *pktp, struct buf *bp,
13105     diskaddr_t lba, uint32_t blockcount)
13106 {
13107 	uchar_t com;
13108 	union scsi_cdb *cdbp;
13109 	uchar_t cdb_group_id;
13110 
13111 	ASSERT(pktp != NULL);
13112 	ASSERT(pktp->pkt_cdbp != NULL);
13113 
13114 	cdbp = (union scsi_cdb *)pktp->pkt_cdbp;
13115 	com = cdbp->scc_cmd;
13116 	cdb_group_id = CDB_GROUPID(com);
13117 
13118 	ASSERT((cdb_group_id == CDB_GROUPID_0) ||
13119 	    (cdb_group_id == CDB_GROUPID_1) ||
13120 	    (cdb_group_id == CDB_GROUPID_4) ||
13121 	    (cdb_group_id == CDB_GROUPID_5));
13122 
13123 	/*
13124 	 * Move pkt to the next portion of the xfer.
13125 	 * func is NULL_FUNC so we do not have to release
13126 	 * the disk mutex here.
13127 	 */
13128 	if (scsi_init_pkt(SD_ADDRESS(un), pktp, bp, 0, 0, 0, 0,
13129 	    NULL_FUNC, NULL) == pktp) {
13130 		/* Success.  Handle partial DMA */
13131 		if (pktp->pkt_resid != 0) {
13132 			blockcount -=
13133 			    SD_BYTES2TGTBLOCKS(un, pktp->pkt_resid);
13134 		}
13135 
13136 		cdbp->scc_cmd = com;
13137 		sd_fill_scsi1_lun(un, pktp);
13138 		if (cdb_group_id == CDB_GROUPID_1) {
13139 			FORMG1ADDR(cdbp, lba);
13140 			FORMG1COUNT(cdbp, blockcount);
13141 			return (0);
13142 		} else if (cdb_group_id == CDB_GROUPID_4) {
13143 			FORMG4LONGADDR(cdbp, lba);
13144 			FORMG4COUNT(cdbp, blockcount);
13145 			return (0);
13146 		} else if (cdb_group_id == CDB_GROUPID_0) {
13147 			FORMG0ADDR(cdbp, lba);
13148 			FORMG0COUNT(cdbp, blockcount);
13149 			return (0);
13150 		} else if (cdb_group_id == CDB_GROUPID_5) {
13151 			FORMG5ADDR(cdbp, lba);
13152 			FORMG5COUNT(cdbp, blockcount);
13153 			return (0);
13154 		}
13155 
13156 		/* Unreachable */
13157 		return (SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL);
13158 	}
13159 
13160 	/*
13161 	 * Error setting up next portion of cmd transfer.
13162 	 * Something is definitely very wrong and this
13163 	 * should not happen.
13164 	 */
13165 	return (SD_PKT_ALLOC_FAILURE);
13166 }
13167 
13168 /*
13169  *    Function: sd_initpkt_for_uscsi
13170  *
13171  * Description: Allocate and initialize for transport a scsi_pkt struct,
13172  *		based upon the info specified in the given uscsi_cmd struct.
13173  *
13174  * Return Code: SD_PKT_ALLOC_SUCCESS
13175  *		SD_PKT_ALLOC_FAILURE
13176  *		SD_PKT_ALLOC_FAILURE_NO_DMA
13177  *		SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL
13178  *
13179  *     Context: Kernel thread and may be called from software interrupt context
13180  *		as part of a sdrunout callback. This function may not block or
13181  *		call routines that block
13182  */
13183 
13184 static int
13185 sd_initpkt_for_uscsi(struct buf *bp, struct scsi_pkt **pktpp)
13186 {
13187 	struct uscsi_cmd *uscmd;
13188 	struct sd_xbuf	*xp;
13189 	struct scsi_pkt	*pktp;
13190 	struct sd_lun	*un;
13191 	uint32_t	flags = 0;
13192 
13193 	ASSERT(bp != NULL);
13194 	ASSERT(pktpp != NULL);
13195 	xp = SD_GET_XBUF(bp);
13196 	ASSERT(xp != NULL);
13197 	un = SD_GET_UN(bp);
13198 	ASSERT(un != NULL);
13199 	ASSERT(mutex_owned(SD_MUTEX(un)));
13200 
13201 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13202 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13203 	ASSERT(uscmd != NULL);
13204 
13205 	SD_TRACE(SD_LOG_IO_CORE, un,
13206 	    "sd_initpkt_for_uscsi: entry: buf:0x%p\n", bp);
13207 
13208 	/* Allocate the scsi_pkt for the command. */
13209 	pktp = scsi_init_pkt(SD_ADDRESS(un), NULL,
13210 	    ((bp->b_bcount != 0) ? bp : NULL), uscmd->uscsi_cdblen,
13211 	    sizeof (struct scsi_arq_status), 0, un->un_pkt_flags,
13212 	    sdrunout, (caddr_t)un);
13213 
13214 	if (pktp == NULL) {
13215 		*pktpp = NULL;
13216 		/*
13217 		 * Set the driver state to RWAIT to indicate the driver
13218 		 * is waiting on resource allocations. The driver will not
13219 		 * suspend, pm_suspend, or detatch while the state is RWAIT.
13220 		 */
13221 		New_state(un, SD_STATE_RWAIT);
13222 
13223 		SD_ERROR(SD_LOG_IO_CORE, un,
13224 		    "sd_initpkt_for_uscsi: No pktp. exit bp:0x%p\n", bp);
13225 
13226 		if ((bp->b_flags & B_ERROR) != 0) {
13227 			return (SD_PKT_ALLOC_FAILURE_NO_DMA);
13228 		}
13229 		return (SD_PKT_ALLOC_FAILURE);
13230 	}
13231 
13232 	/*
13233 	 * We do not do DMA breakup for USCSI commands, so return failure
13234 	 * here if all the needed DMA resources were not allocated.
13235 	 */
13236 	if ((un->un_pkt_flags & PKT_DMA_PARTIAL) &&
13237 	    (bp->b_bcount != 0) && (pktp->pkt_resid != 0)) {
13238 		scsi_destroy_pkt(pktp);
13239 		SD_ERROR(SD_LOG_IO_CORE, un, "sd_initpkt_for_uscsi: "
13240 		    "No partial DMA for USCSI. exit: buf:0x%p\n", bp);
13241 		return (SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL);
13242 	}
13243 
13244 	/* Init the cdb from the given uscsi struct */
13245 	(void) scsi_setup_cdb((union scsi_cdb *)pktp->pkt_cdbp,
13246 	    uscmd->uscsi_cdb[0], 0, 0, 0);
13247 
13248 	sd_fill_scsi1_lun(un, pktp);
13249 
13250 	/*
13251 	 * Set up the optional USCSI flags. See the uscsi (7I) man page
13252 	 * for listing of the supported flags.
13253 	 */
13254 
13255 	if (uscmd->uscsi_flags & USCSI_SILENT) {
13256 		flags |= FLAG_SILENT;
13257 	}
13258 
13259 	if (uscmd->uscsi_flags & USCSI_DIAGNOSE) {
13260 		flags |= FLAG_DIAGNOSE;
13261 	}
13262 
13263 	if (uscmd->uscsi_flags & USCSI_ISOLATE) {
13264 		flags |= FLAG_ISOLATE;
13265 	}
13266 
13267 	if (un->un_f_is_fibre == FALSE) {
13268 		if (uscmd->uscsi_flags & USCSI_RENEGOT) {
13269 			flags |= FLAG_RENEGOTIATE_WIDE_SYNC;
13270 		}
13271 	}
13272 
13273 	/*
13274 	 * Set the pkt flags here so we save time later.
13275 	 * Note: These flags are NOT in the uscsi man page!!!
13276 	 */
13277 	if (uscmd->uscsi_flags & USCSI_HEAD) {
13278 		flags |= FLAG_HEAD;
13279 	}
13280 
13281 	if (uscmd->uscsi_flags & USCSI_NOINTR) {
13282 		flags |= FLAG_NOINTR;
13283 	}
13284 
13285 	/*
13286 	 * For tagged queueing, things get a bit complicated.
13287 	 * Check first for head of queue and last for ordered queue.
13288 	 * If neither head nor order, use the default driver tag flags.
13289 	 */
13290 	if ((uscmd->uscsi_flags & USCSI_NOTAG) == 0) {
13291 		if (uscmd->uscsi_flags & USCSI_HTAG) {
13292 			flags |= FLAG_HTAG;
13293 		} else if (uscmd->uscsi_flags & USCSI_OTAG) {
13294 			flags |= FLAG_OTAG;
13295 		} else {
13296 			flags |= un->un_tagflags & FLAG_TAGMASK;
13297 		}
13298 	}
13299 
13300 	if (uscmd->uscsi_flags & USCSI_NODISCON) {
13301 		flags = (flags & ~FLAG_TAGMASK) | FLAG_NODISCON;
13302 	}
13303 
13304 	pktp->pkt_flags = flags;
13305 
13306 	/* Copy the caller's CDB into the pkt... */
13307 	bcopy(uscmd->uscsi_cdb, pktp->pkt_cdbp, uscmd->uscsi_cdblen);
13308 
13309 	if (uscmd->uscsi_timeout == 0) {
13310 		pktp->pkt_time = un->un_uscsi_timeout;
13311 	} else {
13312 		pktp->pkt_time = uscmd->uscsi_timeout;
13313 	}
13314 
13315 	/* need it later to identify USCSI request in sdintr */
13316 	xp->xb_pkt_flags |= SD_XB_USCSICMD;
13317 
13318 	xp->xb_sense_resid = uscmd->uscsi_rqresid;
13319 
13320 	pktp->pkt_private = bp;
13321 	pktp->pkt_comp = sdintr;
13322 	*pktpp = pktp;
13323 
13324 	SD_TRACE(SD_LOG_IO_CORE, un,
13325 	    "sd_initpkt_for_uscsi: exit: buf:0x%p\n", bp);
13326 
13327 	return (SD_PKT_ALLOC_SUCCESS);
13328 }
13329 
13330 
13331 /*
13332  *    Function: sd_destroypkt_for_uscsi
13333  *
13334  * Description: Free the scsi_pkt(9S) struct for the given bp, for uscsi
13335  *		IOs.. Also saves relevant info into the associated uscsi_cmd
13336  *		struct.
13337  *
13338  *     Context: May be called under interrupt context
13339  */
13340 
13341 static void
13342 sd_destroypkt_for_uscsi(struct buf *bp)
13343 {
13344 	struct uscsi_cmd *uscmd;
13345 	struct sd_xbuf	*xp;
13346 	struct scsi_pkt	*pktp;
13347 	struct sd_lun	*un;
13348 
13349 	ASSERT(bp != NULL);
13350 	xp = SD_GET_XBUF(bp);
13351 	ASSERT(xp != NULL);
13352 	un = SD_GET_UN(bp);
13353 	ASSERT(un != NULL);
13354 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13355 	pktp = SD_GET_PKTP(bp);
13356 	ASSERT(pktp != NULL);
13357 
13358 	SD_TRACE(SD_LOG_IO_CORE, un,
13359 	    "sd_destroypkt_for_uscsi: entry: buf:0x%p\n", bp);
13360 
13361 	/* The pointer to the uscsi_cmd struct is expected in xb_pktinfo */
13362 	uscmd = (struct uscsi_cmd *)xp->xb_pktinfo;
13363 	ASSERT(uscmd != NULL);
13364 
13365 	/* Save the status and the residual into the uscsi_cmd struct */
13366 	uscmd->uscsi_status = ((*(pktp)->pkt_scbp) & STATUS_MASK);
13367 	uscmd->uscsi_resid  = bp->b_resid;
13368 
13369 	/*
13370 	 * If enabled, copy any saved sense data into the area specified
13371 	 * by the uscsi command.
13372 	 */
13373 	if (((uscmd->uscsi_flags & USCSI_RQENABLE) != 0) &&
13374 	    (uscmd->uscsi_rqlen != 0) && (uscmd->uscsi_rqbuf != NULL)) {
13375 		/*
13376 		 * Note: uscmd->uscsi_rqbuf should always point to a buffer
13377 		 * at least SENSE_LENGTH bytes in size (see sd_send_scsi_cmd())
13378 		 */
13379 		uscmd->uscsi_rqstatus = xp->xb_sense_status;
13380 		uscmd->uscsi_rqresid  = xp->xb_sense_resid;
13381 		bcopy(xp->xb_sense_data, uscmd->uscsi_rqbuf, SENSE_LENGTH);
13382 	}
13383 
13384 	/* We are done with the scsi_pkt; free it now */
13385 	ASSERT(SD_GET_PKTP(bp) != NULL);
13386 	scsi_destroy_pkt(SD_GET_PKTP(bp));
13387 
13388 	SD_TRACE(SD_LOG_IO_CORE, un,
13389 	    "sd_destroypkt_for_uscsi: exit: buf:0x%p\n", bp);
13390 }
13391 
13392 
13393 /*
13394  *    Function: sd_bioclone_alloc
13395  *
13396  * Description: Allocate a buf(9S) and init it as per the given buf
13397  *		and the various arguments.  The associated sd_xbuf
13398  *		struct is (nearly) duplicated.  The struct buf *bp
13399  *		argument is saved in new_xp->xb_private.
13400  *
13401  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
13402  *		datalen - size of data area for the shadow bp
13403  *		blkno - starting LBA
13404  *		func - function pointer for b_iodone in the shadow buf. (May
13405  *			be NULL if none.)
13406  *
13407  * Return Code: Pointer to allocates buf(9S) struct
13408  *
13409  *     Context: Can sleep.
13410  */
13411 
13412 static struct buf *
13413 sd_bioclone_alloc(struct buf *bp, size_t datalen,
13414 	daddr_t blkno, int (*func)(struct buf *))
13415 {
13416 	struct	sd_lun	*un;
13417 	struct	sd_xbuf	*xp;
13418 	struct	sd_xbuf	*new_xp;
13419 	struct	buf	*new_bp;
13420 
13421 	ASSERT(bp != NULL);
13422 	xp = SD_GET_XBUF(bp);
13423 	ASSERT(xp != NULL);
13424 	un = SD_GET_UN(bp);
13425 	ASSERT(un != NULL);
13426 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13427 
13428 	new_bp = bioclone(bp, 0, datalen, SD_GET_DEV(un), blkno, func,
13429 	    NULL, KM_SLEEP);
13430 
13431 	new_bp->b_lblkno	= blkno;
13432 
13433 	/*
13434 	 * Allocate an xbuf for the shadow bp and copy the contents of the
13435 	 * original xbuf into it.
13436 	 */
13437 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
13438 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
13439 
13440 	/*
13441 	 * The given bp is automatically saved in the xb_private member
13442 	 * of the new xbuf.  Callers are allowed to depend on this.
13443 	 */
13444 	new_xp->xb_private = bp;
13445 
13446 	new_bp->b_private  = new_xp;
13447 
13448 	return (new_bp);
13449 }
13450 
13451 /*
13452  *    Function: sd_shadow_buf_alloc
13453  *
13454  * Description: Allocate a buf(9S) and init it as per the given buf
13455  *		and the various arguments.  The associated sd_xbuf
13456  *		struct is (nearly) duplicated.  The struct buf *bp
13457  *		argument is saved in new_xp->xb_private.
13458  *
13459  *   Arguments: bp - ptr the the buf(9S) to be "shadowed"
13460  *		datalen - size of data area for the shadow bp
13461  *		bflags - B_READ or B_WRITE (pseudo flag)
13462  *		blkno - starting LBA
13463  *		func - function pointer for b_iodone in the shadow buf. (May
13464  *			be NULL if none.)
13465  *
13466  * Return Code: Pointer to allocates buf(9S) struct
13467  *
13468  *     Context: Can sleep.
13469  */
13470 
13471 static struct buf *
13472 sd_shadow_buf_alloc(struct buf *bp, size_t datalen, uint_t bflags,
13473 	daddr_t blkno, int (*func)(struct buf *))
13474 {
13475 	struct	sd_lun	*un;
13476 	struct	sd_xbuf	*xp;
13477 	struct	sd_xbuf	*new_xp;
13478 	struct	buf	*new_bp;
13479 
13480 	ASSERT(bp != NULL);
13481 	xp = SD_GET_XBUF(bp);
13482 	ASSERT(xp != NULL);
13483 	un = SD_GET_UN(bp);
13484 	ASSERT(un != NULL);
13485 	ASSERT(!mutex_owned(SD_MUTEX(un)));
13486 
13487 	if (bp->b_flags & (B_PAGEIO | B_PHYS)) {
13488 		bp_mapin(bp);
13489 	}
13490 
13491 	bflags &= (B_READ | B_WRITE);
13492 #if defined(__i386) || defined(__amd64)
13493 	new_bp = getrbuf(KM_SLEEP);
13494 	new_bp->b_un.b_addr = kmem_zalloc(datalen, KM_SLEEP);
13495 	new_bp->b_bcount = datalen;
13496 	new_bp->b_flags	= bp->b_flags | bflags;
13497 #else
13498 	new_bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), NULL,
13499 	    datalen, bflags, SLEEP_FUNC, NULL);
13500 #endif
13501 	new_bp->av_forw	= NULL;
13502 	new_bp->av_back	= NULL;
13503 	new_bp->b_dev	= bp->b_dev;
13504 	new_bp->b_blkno	= blkno;
13505 	new_bp->b_iodone = func;
13506 	new_bp->b_edev	= bp->b_edev;
13507 	new_bp->b_resid	= 0;
13508 
13509 	/* We need to preserve the B_FAILFAST flag */
13510 	if (bp->b_flags & B_FAILFAST) {
13511 		new_bp->b_flags |= B_FAILFAST;
13512 	}
13513 
13514 	/*
13515 	 * Allocate an xbuf for the shadow bp and copy the contents of the
13516 	 * original xbuf into it.
13517 	 */
13518 	new_xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
13519 	bcopy(xp, new_xp, sizeof (struct sd_xbuf));
13520 
13521 	/* Need later to copy data between the shadow buf & original buf! */
13522 	new_xp->xb_pkt_flags |= PKT_CONSISTENT;
13523 
13524 	/*
13525 	 * The given bp is automatically saved in the xb_private member
13526 	 * of the new xbuf.  Callers are allowed to depend on this.
13527 	 */
13528 	new_xp->xb_private = bp;
13529 
13530 	new_bp->b_private  = new_xp;
13531 
13532 	return (new_bp);
13533 }
13534 
13535 /*
13536  *    Function: sd_bioclone_free
13537  *
13538  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations
13539  *		in the larger than partition operation.
13540  *
13541  *     Context: May be called under interrupt context
13542  */
13543 
13544 static void
13545 sd_bioclone_free(struct buf *bp)
13546 {
13547 	struct sd_xbuf	*xp;
13548 
13549 	ASSERT(bp != NULL);
13550 	xp = SD_GET_XBUF(bp);
13551 	ASSERT(xp != NULL);
13552 
13553 	/*
13554 	 * Call bp_mapout() before freeing the buf,  in case a lower
13555 	 * layer or HBA  had done a bp_mapin().  we must do this here
13556 	 * as we are the "originator" of the shadow buf.
13557 	 */
13558 	bp_mapout(bp);
13559 
13560 	/*
13561 	 * Null out b_iodone before freeing the bp, to ensure that the driver
13562 	 * never gets confused by a stale value in this field. (Just a little
13563 	 * extra defensiveness here.)
13564 	 */
13565 	bp->b_iodone = NULL;
13566 
13567 	freerbuf(bp);
13568 
13569 	kmem_free(xp, sizeof (struct sd_xbuf));
13570 }
13571 
13572 /*
13573  *    Function: sd_shadow_buf_free
13574  *
13575  * Description: Deallocate a buf(9S) that was used for 'shadow' IO operations.
13576  *
13577  *     Context: May be called under interrupt context
13578  */
13579 
13580 static void
13581 sd_shadow_buf_free(struct buf *bp)
13582 {
13583 	struct sd_xbuf	*xp;
13584 
13585 	ASSERT(bp != NULL);
13586 	xp = SD_GET_XBUF(bp);
13587 	ASSERT(xp != NULL);
13588 
13589 #if defined(__sparc)
13590 	/*
13591 	 * Call bp_mapout() before freeing the buf,  in case a lower
13592 	 * layer or HBA  had done a bp_mapin().  we must do this here
13593 	 * as we are the "originator" of the shadow buf.
13594 	 */
13595 	bp_mapout(bp);
13596 #endif
13597 
13598 	/*
13599 	 * Null out b_iodone before freeing the bp, to ensure that the driver
13600 	 * never gets confused by a stale value in this field. (Just a little
13601 	 * extra defensiveness here.)
13602 	 */
13603 	bp->b_iodone = NULL;
13604 
13605 #if defined(__i386) || defined(__amd64)
13606 	kmem_free(bp->b_un.b_addr, bp->b_bcount);
13607 	freerbuf(bp);
13608 #else
13609 	scsi_free_consistent_buf(bp);
13610 #endif
13611 
13612 	kmem_free(xp, sizeof (struct sd_xbuf));
13613 }
13614 
13615 
13616 /*
13617  *    Function: sd_print_transport_rejected_message
13618  *
13619  * Description: This implements the ludicrously complex rules for printing
13620  *		a "transport rejected" message.  This is to address the
13621  *		specific problem of having a flood of this error message
13622  *		produced when a failover occurs.
13623  *
13624  *     Context: Any.
13625  */
13626 
13627 static void
13628 sd_print_transport_rejected_message(struct sd_lun *un, struct sd_xbuf *xp,
13629 	int code)
13630 {
13631 	ASSERT(un != NULL);
13632 	ASSERT(mutex_owned(SD_MUTEX(un)));
13633 	ASSERT(xp != NULL);
13634 
13635 	/*
13636 	 * Print the "transport rejected" message under the following
13637 	 * conditions:
13638 	 *
13639 	 * - Whenever the SD_LOGMASK_DIAG bit of sd_level_mask is set
13640 	 * - The error code from scsi_transport() is NOT a TRAN_FATAL_ERROR.
13641 	 * - If the error code IS a TRAN_FATAL_ERROR, then the message is
13642 	 *   printed the FIRST time a TRAN_FATAL_ERROR is returned from
13643 	 *   scsi_transport(9F) (which indicates that the target might have
13644 	 *   gone off-line).  This uses the un->un_tran_fatal_count
13645 	 *   count, which is incremented whenever a TRAN_FATAL_ERROR is
13646 	 *   received, and reset to zero whenver a TRAN_ACCEPT is returned
13647 	 *   from scsi_transport().
13648 	 *
13649 	 * The FLAG_SILENT in the scsi_pkt must be CLEARED in ALL of
13650 	 * the preceeding cases in order for the message to be printed.
13651 	 */
13652 	if ((xp->xb_pktp->pkt_flags & FLAG_SILENT) == 0) {
13653 		if ((sd_level_mask & SD_LOGMASK_DIAG) ||
13654 		    (code != TRAN_FATAL_ERROR) ||
13655 		    (un->un_tran_fatal_count == 1)) {
13656 			switch (code) {
13657 			case TRAN_BADPKT:
13658 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13659 				    "transport rejected bad packet\n");
13660 				break;
13661 			case TRAN_FATAL_ERROR:
13662 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13663 				    "transport rejected fatal error\n");
13664 				break;
13665 			default:
13666 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
13667 				    "transport rejected (%d)\n", code);
13668 				break;
13669 			}
13670 		}
13671 	}
13672 }
13673 
13674 
13675 /*
13676  *    Function: sd_add_buf_to_waitq
13677  *
13678  * Description: Add the given buf(9S) struct to the wait queue for the
13679  *		instance.  If sorting is enabled, then the buf is added
13680  *		to the queue via an elevator sort algorithm (a la
13681  *		disksort(9F)).  The SD_GET_BLKNO(bp) is used as the sort key.
13682  *		If sorting is not enabled, then the buf is just added
13683  *		to the end of the wait queue.
13684  *
13685  * Return Code: void
13686  *
13687  *     Context: Does not sleep/block, therefore technically can be called
13688  *		from any context.  However if sorting is enabled then the
13689  *		execution time is indeterminate, and may take long if
13690  *		the wait queue grows large.
13691  */
13692 
13693 static void
13694 sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp)
13695 {
13696 	struct buf *ap;
13697 
13698 	ASSERT(bp != NULL);
13699 	ASSERT(un != NULL);
13700 	ASSERT(mutex_owned(SD_MUTEX(un)));
13701 
13702 	/* If the queue is empty, add the buf as the only entry & return. */
13703 	if (un->un_waitq_headp == NULL) {
13704 		ASSERT(un->un_waitq_tailp == NULL);
13705 		un->un_waitq_headp = un->un_waitq_tailp = bp;
13706 		bp->av_forw = NULL;
13707 		return;
13708 	}
13709 
13710 	ASSERT(un->un_waitq_tailp != NULL);
13711 
13712 	/*
13713 	 * If sorting is disabled, just add the buf to the tail end of
13714 	 * the wait queue and return.
13715 	 */
13716 	if (un->un_f_disksort_disabled) {
13717 		un->un_waitq_tailp->av_forw = bp;
13718 		un->un_waitq_tailp = bp;
13719 		bp->av_forw = NULL;
13720 		return;
13721 	}
13722 
13723 	/*
13724 	 * Sort thru the list of requests currently on the wait queue
13725 	 * and add the new buf request at the appropriate position.
13726 	 *
13727 	 * The un->un_waitq_headp is an activity chain pointer on which
13728 	 * we keep two queues, sorted in ascending SD_GET_BLKNO() order. The
13729 	 * first queue holds those requests which are positioned after
13730 	 * the current SD_GET_BLKNO() (in the first request); the second holds
13731 	 * requests which came in after their SD_GET_BLKNO() number was passed.
13732 	 * Thus we implement a one way scan, retracting after reaching
13733 	 * the end of the drive to the first request on the second
13734 	 * queue, at which time it becomes the first queue.
13735 	 * A one-way scan is natural because of the way UNIX read-ahead
13736 	 * blocks are allocated.
13737 	 *
13738 	 * If we lie after the first request, then we must locate the
13739 	 * second request list and add ourselves to it.
13740 	 */
13741 	ap = un->un_waitq_headp;
13742 	if (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap)) {
13743 		while (ap->av_forw != NULL) {
13744 			/*
13745 			 * Look for an "inversion" in the (normally
13746 			 * ascending) block numbers. This indicates
13747 			 * the start of the second request list.
13748 			 */
13749 			if (SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) {
13750 				/*
13751 				 * Search the second request list for the
13752 				 * first request at a larger block number.
13753 				 * We go before that; however if there is
13754 				 * no such request, we go at the end.
13755 				 */
13756 				do {
13757 					if (SD_GET_BLKNO(bp) <
13758 					    SD_GET_BLKNO(ap->av_forw)) {
13759 						goto insert;
13760 					}
13761 					ap = ap->av_forw;
13762 				} while (ap->av_forw != NULL);
13763 				goto insert;		/* after last */
13764 			}
13765 			ap = ap->av_forw;
13766 		}
13767 
13768 		/*
13769 		 * No inversions... we will go after the last, and
13770 		 * be the first request in the second request list.
13771 		 */
13772 		goto insert;
13773 	}
13774 
13775 	/*
13776 	 * Request is at/after the current request...
13777 	 * sort in the first request list.
13778 	 */
13779 	while (ap->av_forw != NULL) {
13780 		/*
13781 		 * We want to go after the current request (1) if
13782 		 * there is an inversion after it (i.e. it is the end
13783 		 * of the first request list), or (2) if the next
13784 		 * request is a larger block no. than our request.
13785 		 */
13786 		if ((SD_GET_BLKNO(ap->av_forw) < SD_GET_BLKNO(ap)) ||
13787 		    (SD_GET_BLKNO(bp) < SD_GET_BLKNO(ap->av_forw))) {
13788 			goto insert;
13789 		}
13790 		ap = ap->av_forw;
13791 	}
13792 
13793 	/*
13794 	 * Neither a second list nor a larger request, therefore
13795 	 * we go at the end of the first list (which is the same
13796 	 * as the end of the whole schebang).
13797 	 */
13798 insert:
13799 	bp->av_forw = ap->av_forw;
13800 	ap->av_forw = bp;
13801 
13802 	/*
13803 	 * If we inserted onto the tail end of the waitq, make sure the
13804 	 * tail pointer is updated.
13805 	 */
13806 	if (ap == un->un_waitq_tailp) {
13807 		un->un_waitq_tailp = bp;
13808 	}
13809 }
13810 
13811 
13812 /*
13813  *    Function: sd_start_cmds
13814  *
13815  * Description: Remove and transport cmds from the driver queues.
13816  *
13817  *   Arguments: un - pointer to the unit (soft state) struct for the target.
13818  *
13819  *		immed_bp - ptr to a buf to be transported immediately. Only
13820  *		the immed_bp is transported; bufs on the waitq are not
13821  *		processed and the un_retry_bp is not checked.  If immed_bp is
13822  *		NULL, then normal queue processing is performed.
13823  *
13824  *     Context: May be called from kernel thread context, interrupt context,
13825  *		or runout callback context. This function may not block or
13826  *		call routines that block.
13827  */
13828 
13829 static void
13830 sd_start_cmds(struct sd_lun *un, struct buf *immed_bp)
13831 {
13832 	struct	sd_xbuf	*xp;
13833 	struct	buf	*bp;
13834 	void	(*statp)(kstat_io_t *);
13835 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13836 	void	(*saved_statp)(kstat_io_t *);
13837 #endif
13838 	int	rval;
13839 
13840 	ASSERT(un != NULL);
13841 	ASSERT(mutex_owned(SD_MUTEX(un)));
13842 	ASSERT(un->un_ncmds_in_transport >= 0);
13843 	ASSERT(un->un_throttle >= 0);
13844 
13845 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: entry\n");
13846 
13847 	do {
13848 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13849 		saved_statp = NULL;
13850 #endif
13851 
13852 		/*
13853 		 * If we are syncing or dumping, fail the command to
13854 		 * avoid recursively calling back into scsi_transport().
13855 		 */
13856 		if (ddi_in_panic()) {
13857 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
13858 			    "sd_start_cmds: panicking\n");
13859 			goto exit;
13860 		}
13861 
13862 		if ((bp = immed_bp) != NULL) {
13863 			/*
13864 			 * We have a bp that must be transported immediately.
13865 			 * It's OK to transport the immed_bp here without doing
13866 			 * the throttle limit check because the immed_bp is
13867 			 * always used in a retry/recovery case. This means
13868 			 * that we know we are not at the throttle limit by
13869 			 * virtue of the fact that to get here we must have
13870 			 * already gotten a command back via sdintr(). This also
13871 			 * relies on (1) the command on un_retry_bp preventing
13872 			 * further commands from the waitq from being issued;
13873 			 * and (2) the code in sd_retry_command checking the
13874 			 * throttle limit before issuing a delayed or immediate
13875 			 * retry. This holds even if the throttle limit is
13876 			 * currently ratcheted down from its maximum value.
13877 			 */
13878 			statp = kstat_runq_enter;
13879 			if (bp == un->un_retry_bp) {
13880 				ASSERT((un->un_retry_statp == NULL) ||
13881 				    (un->un_retry_statp == kstat_waitq_enter) ||
13882 				    (un->un_retry_statp ==
13883 				    kstat_runq_back_to_waitq));
13884 				/*
13885 				 * If the waitq kstat was incremented when
13886 				 * sd_set_retry_bp() queued this bp for a retry,
13887 				 * then we must set up statp so that the waitq
13888 				 * count will get decremented correctly below.
13889 				 * Also we must clear un->un_retry_statp to
13890 				 * ensure that we do not act on a stale value
13891 				 * in this field.
13892 				 */
13893 				if ((un->un_retry_statp == kstat_waitq_enter) ||
13894 				    (un->un_retry_statp ==
13895 				    kstat_runq_back_to_waitq)) {
13896 					statp = kstat_waitq_to_runq;
13897 				}
13898 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13899 				saved_statp = un->un_retry_statp;
13900 #endif
13901 				un->un_retry_statp = NULL;
13902 
13903 				SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
13904 				    "sd_start_cmds: un:0x%p: GOT retry_bp:0x%p "
13905 				    "un_throttle:%d un_ncmds_in_transport:%d\n",
13906 				    un, un->un_retry_bp, un->un_throttle,
13907 				    un->un_ncmds_in_transport);
13908 			} else {
13909 				SD_TRACE(SD_LOG_IO_CORE, un, "sd_start_cmds: "
13910 				    "processing priority bp:0x%p\n", bp);
13911 			}
13912 
13913 		} else if ((bp = un->un_waitq_headp) != NULL) {
13914 			/*
13915 			 * A command on the waitq is ready to go, but do not
13916 			 * send it if:
13917 			 *
13918 			 * (1) the throttle limit has been reached, or
13919 			 * (2) a retry is pending, or
13920 			 * (3) a START_STOP_UNIT callback pending, or
13921 			 * (4) a callback for a SD_PATH_DIRECT_PRIORITY
13922 			 *	command is pending.
13923 			 *
13924 			 * For all of these conditions, IO processing will
13925 			 * restart after the condition is cleared.
13926 			 */
13927 			if (un->un_ncmds_in_transport >= un->un_throttle) {
13928 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
13929 				    "sd_start_cmds: exiting, "
13930 				    "throttle limit reached!\n");
13931 				goto exit;
13932 			}
13933 			if (un->un_retry_bp != NULL) {
13934 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
13935 				    "sd_start_cmds: exiting, retry pending!\n");
13936 				goto exit;
13937 			}
13938 			if (un->un_startstop_timeid != NULL) {
13939 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
13940 				    "sd_start_cmds: exiting, "
13941 				    "START_STOP pending!\n");
13942 				goto exit;
13943 			}
13944 			if (un->un_direct_priority_timeid != NULL) {
13945 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
13946 				    "sd_start_cmds: exiting, "
13947 				    "SD_PATH_DIRECT_PRIORITY cmd. pending!\n");
13948 				goto exit;
13949 			}
13950 
13951 			/* Dequeue the command */
13952 			un->un_waitq_headp = bp->av_forw;
13953 			if (un->un_waitq_headp == NULL) {
13954 				un->un_waitq_tailp = NULL;
13955 			}
13956 			bp->av_forw = NULL;
13957 			statp = kstat_waitq_to_runq;
13958 			SD_TRACE(SD_LOG_IO_CORE, un,
13959 			    "sd_start_cmds: processing waitq bp:0x%p\n", bp);
13960 
13961 		} else {
13962 			/* No work to do so bail out now */
13963 			SD_TRACE(SD_LOG_IO_CORE, un,
13964 			    "sd_start_cmds: no more work, exiting!\n");
13965 			goto exit;
13966 		}
13967 
13968 		/*
13969 		 * Reset the state to normal. This is the mechanism by which
13970 		 * the state transitions from either SD_STATE_RWAIT or
13971 		 * SD_STATE_OFFLINE to SD_STATE_NORMAL.
13972 		 * If state is SD_STATE_PM_CHANGING then this command is
13973 		 * part of the device power control and the state must
13974 		 * not be put back to normal. Doing so would would
13975 		 * allow new commands to proceed when they shouldn't,
13976 		 * the device may be going off.
13977 		 */
13978 		if ((un->un_state != SD_STATE_SUSPENDED) &&
13979 		    (un->un_state != SD_STATE_PM_CHANGING)) {
13980 			New_state(un, SD_STATE_NORMAL);
13981 		    }
13982 
13983 		xp = SD_GET_XBUF(bp);
13984 		ASSERT(xp != NULL);
13985 
13986 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
13987 		/*
13988 		 * Allocate the scsi_pkt if we need one, or attach DMA
13989 		 * resources if we have a scsi_pkt that needs them. The
13990 		 * latter should only occur for commands that are being
13991 		 * retried.
13992 		 */
13993 		if ((xp->xb_pktp == NULL) ||
13994 		    ((xp->xb_pkt_flags & SD_XB_DMA_FREED) != 0)) {
13995 #else
13996 		if (xp->xb_pktp == NULL) {
13997 #endif
13998 			/*
13999 			 * There is no scsi_pkt allocated for this buf. Call
14000 			 * the initpkt function to allocate & init one.
14001 			 *
14002 			 * The scsi_init_pkt runout callback functionality is
14003 			 * implemented as follows:
14004 			 *
14005 			 * 1) The initpkt function always calls
14006 			 *    scsi_init_pkt(9F) with sdrunout specified as the
14007 			 *    callback routine.
14008 			 * 2) A successful packet allocation is initialized and
14009 			 *    the I/O is transported.
14010 			 * 3) The I/O associated with an allocation resource
14011 			 *    failure is left on its queue to be retried via
14012 			 *    runout or the next I/O.
14013 			 * 4) The I/O associated with a DMA error is removed
14014 			 *    from the queue and failed with EIO. Processing of
14015 			 *    the transport queues is also halted to be
14016 			 *    restarted via runout or the next I/O.
14017 			 * 5) The I/O associated with a CDB size or packet
14018 			 *    size error is removed from the queue and failed
14019 			 *    with EIO. Processing of the transport queues is
14020 			 *    continued.
14021 			 *
14022 			 * Note: there is no interface for canceling a runout
14023 			 * callback. To prevent the driver from detaching or
14024 			 * suspending while a runout is pending the driver
14025 			 * state is set to SD_STATE_RWAIT
14026 			 *
14027 			 * Note: using the scsi_init_pkt callback facility can
14028 			 * result in an I/O request persisting at the head of
14029 			 * the list which cannot be satisfied even after
14030 			 * multiple retries. In the future the driver may
14031 			 * implement some kind of maximum runout count before
14032 			 * failing an I/O.
14033 			 *
14034 			 * Note: the use of funcp below may seem superfluous,
14035 			 * but it helps warlock figure out the correct
14036 			 * initpkt function calls (see [s]sd.wlcmd).
14037 			 */
14038 			struct scsi_pkt	*pktp;
14039 			int (*funcp)(struct buf *bp, struct scsi_pkt **pktp);
14040 
14041 			ASSERT(bp != un->un_rqs_bp);
14042 
14043 			funcp = sd_initpkt_map[xp->xb_chain_iostart];
14044 			switch ((*funcp)(bp, &pktp)) {
14045 			case  SD_PKT_ALLOC_SUCCESS:
14046 				xp->xb_pktp = pktp;
14047 				SD_TRACE(SD_LOG_IO_CORE, un,
14048 				    "sd_start_cmd: SD_PKT_ALLOC_SUCCESS 0x%p\n",
14049 				    pktp);
14050 				goto got_pkt;
14051 
14052 			case SD_PKT_ALLOC_FAILURE:
14053 				/*
14054 				 * Temporary (hopefully) resource depletion.
14055 				 * Since retries and RQS commands always have a
14056 				 * scsi_pkt allocated, these cases should never
14057 				 * get here. So the only cases this needs to
14058 				 * handle is a bp from the waitq (which we put
14059 				 * back onto the waitq for sdrunout), or a bp
14060 				 * sent as an immed_bp (which we just fail).
14061 				 */
14062 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14063 				    "sd_start_cmds: SD_PKT_ALLOC_FAILURE\n");
14064 
14065 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14066 
14067 				if (bp == immed_bp) {
14068 					/*
14069 					 * If SD_XB_DMA_FREED is clear, then
14070 					 * this is a failure to allocate a
14071 					 * scsi_pkt, and we must fail the
14072 					 * command.
14073 					 */
14074 					if ((xp->xb_pkt_flags &
14075 					    SD_XB_DMA_FREED) == 0) {
14076 						break;
14077 					}
14078 
14079 					/*
14080 					 * If this immediate command is NOT our
14081 					 * un_retry_bp, then we must fail it.
14082 					 */
14083 					if (bp != un->un_retry_bp) {
14084 						break;
14085 					}
14086 
14087 					/*
14088 					 * We get here if this cmd is our
14089 					 * un_retry_bp that was DMAFREED, but
14090 					 * scsi_init_pkt() failed to reallocate
14091 					 * DMA resources when we attempted to
14092 					 * retry it. This can happen when an
14093 					 * mpxio failover is in progress, but
14094 					 * we don't want to just fail the
14095 					 * command in this case.
14096 					 *
14097 					 * Use timeout(9F) to restart it after
14098 					 * a 100ms delay.  We don't want to
14099 					 * let sdrunout() restart it, because
14100 					 * sdrunout() is just supposed to start
14101 					 * commands that are sitting on the
14102 					 * wait queue.  The un_retry_bp stays
14103 					 * set until the command completes, but
14104 					 * sdrunout can be called many times
14105 					 * before that happens.  Since sdrunout
14106 					 * cannot tell if the un_retry_bp is
14107 					 * already in the transport, it could
14108 					 * end up calling scsi_transport() for
14109 					 * the un_retry_bp multiple times.
14110 					 *
14111 					 * Also: don't schedule the callback
14112 					 * if some other callback is already
14113 					 * pending.
14114 					 */
14115 					if (un->un_retry_statp == NULL) {
14116 						/*
14117 						 * restore the kstat pointer to
14118 						 * keep kstat counts coherent
14119 						 * when we do retry the command.
14120 						 */
14121 						un->un_retry_statp =
14122 						    saved_statp;
14123 					}
14124 
14125 					if ((un->un_startstop_timeid == NULL) &&
14126 					    (un->un_retry_timeid == NULL) &&
14127 					    (un->un_direct_priority_timeid ==
14128 					    NULL)) {
14129 
14130 						un->un_retry_timeid =
14131 						    timeout(
14132 						    sd_start_retry_command,
14133 						    un, SD_RESTART_TIMEOUT);
14134 					}
14135 					goto exit;
14136 				}
14137 
14138 #else
14139 				if (bp == immed_bp) {
14140 					break;	/* Just fail the command */
14141 				}
14142 #endif
14143 
14144 				/* Add the buf back to the head of the waitq */
14145 				bp->av_forw = un->un_waitq_headp;
14146 				un->un_waitq_headp = bp;
14147 				if (un->un_waitq_tailp == NULL) {
14148 					un->un_waitq_tailp = bp;
14149 				}
14150 				goto exit;
14151 
14152 			case SD_PKT_ALLOC_FAILURE_NO_DMA:
14153 				/*
14154 				 * HBA DMA resource failure. Fail the command
14155 				 * and continue processing of the queues.
14156 				 */
14157 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14158 				    "sd_start_cmds: "
14159 				    "SD_PKT_ALLOC_FAILURE_NO_DMA\n");
14160 				break;
14161 
14162 			case SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL:
14163 				/*
14164 				 * Note:x86: Partial DMA mapping not supported
14165 				 * for USCSI commands, and all the needed DMA
14166 				 * resources were not allocated.
14167 				 */
14168 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14169 				    "sd_start_cmds: "
14170 				    "SD_PKT_ALLOC_FAILURE_PKT_TOO_SMALL\n");
14171 				break;
14172 
14173 			case SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL:
14174 				/*
14175 				 * Note:x86: Request cannot fit into CDB based
14176 				 * on lba and len.
14177 				 */
14178 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14179 				    "sd_start_cmds: "
14180 				    "SD_PKT_ALLOC_FAILURE_CDB_TOO_SMALL\n");
14181 				break;
14182 
14183 			default:
14184 				/* Should NEVER get here! */
14185 				panic("scsi_initpkt error");
14186 				/*NOTREACHED*/
14187 			}
14188 
14189 			/*
14190 			 * Fatal error in allocating a scsi_pkt for this buf.
14191 			 * Update kstats & return the buf with an error code.
14192 			 * We must use sd_return_failed_command_no_restart() to
14193 			 * avoid a recursive call back into sd_start_cmds().
14194 			 * However this also means that we must keep processing
14195 			 * the waitq here in order to avoid stalling.
14196 			 */
14197 			if (statp == kstat_waitq_to_runq) {
14198 				SD_UPDATE_KSTATS(un, kstat_waitq_exit, bp);
14199 			}
14200 			sd_return_failed_command_no_restart(un, bp, EIO);
14201 			if (bp == immed_bp) {
14202 				/* immed_bp is gone by now, so clear this */
14203 				immed_bp = NULL;
14204 			}
14205 			continue;
14206 		}
14207 got_pkt:
14208 		if (bp == immed_bp) {
14209 			/* goto the head of the class.... */
14210 			xp->xb_pktp->pkt_flags |= FLAG_HEAD;
14211 		}
14212 
14213 		un->un_ncmds_in_transport++;
14214 		SD_UPDATE_KSTATS(un, statp, bp);
14215 
14216 		/*
14217 		 * Call scsi_transport() to send the command to the target.
14218 		 * According to SCSA architecture, we must drop the mutex here
14219 		 * before calling scsi_transport() in order to avoid deadlock.
14220 		 * Note that the scsi_pkt's completion routine can be executed
14221 		 * (from interrupt context) even before the call to
14222 		 * scsi_transport() returns.
14223 		 */
14224 		SD_TRACE(SD_LOG_IO_CORE, un,
14225 		    "sd_start_cmds: calling scsi_transport()\n");
14226 		DTRACE_PROBE1(scsi__transport__dispatch, struct buf *, bp);
14227 
14228 		mutex_exit(SD_MUTEX(un));
14229 		rval = scsi_transport(xp->xb_pktp);
14230 		mutex_enter(SD_MUTEX(un));
14231 
14232 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14233 		    "sd_start_cmds: scsi_transport() returned %d\n", rval);
14234 
14235 		switch (rval) {
14236 		case TRAN_ACCEPT:
14237 			/* Clear this with every pkt accepted by the HBA */
14238 			un->un_tran_fatal_count = 0;
14239 			break;	/* Success; try the next cmd (if any) */
14240 
14241 		case TRAN_BUSY:
14242 			un->un_ncmds_in_transport--;
14243 			ASSERT(un->un_ncmds_in_transport >= 0);
14244 
14245 			/*
14246 			 * Don't retry request sense, the sense data
14247 			 * is lost when another request is sent.
14248 			 * Free up the rqs buf and retry
14249 			 * the original failed cmd.  Update kstat.
14250 			 */
14251 			if (bp == un->un_rqs_bp) {
14252 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14253 				bp = sd_mark_rqs_idle(un, xp);
14254 				sd_retry_command(un, bp, SD_RETRIES_STANDARD,
14255 					NULL, NULL, EIO, SD_BSY_TIMEOUT / 500,
14256 					kstat_waitq_enter);
14257 				goto exit;
14258 			}
14259 
14260 #if defined(__i386) || defined(__amd64)	/* DMAFREE for x86 only */
14261 			/*
14262 			 * Free the DMA resources for the  scsi_pkt. This will
14263 			 * allow mpxio to select another path the next time
14264 			 * we call scsi_transport() with this scsi_pkt.
14265 			 * See sdintr() for the rationalization behind this.
14266 			 */
14267 			if ((un->un_f_is_fibre == TRUE) &&
14268 			    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
14269 			    ((xp->xb_pktp->pkt_flags & FLAG_SENSING) == 0)) {
14270 				scsi_dmafree(xp->xb_pktp);
14271 				xp->xb_pkt_flags |= SD_XB_DMA_FREED;
14272 			}
14273 #endif
14274 
14275 			if (SD_IS_DIRECT_PRIORITY(SD_GET_XBUF(bp))) {
14276 				/*
14277 				 * Commands that are SD_PATH_DIRECT_PRIORITY
14278 				 * are for error recovery situations. These do
14279 				 * not use the normal command waitq, so if they
14280 				 * get a TRAN_BUSY we cannot put them back onto
14281 				 * the waitq for later retry. One possible
14282 				 * problem is that there could already be some
14283 				 * other command on un_retry_bp that is waiting
14284 				 * for this one to complete, so we would be
14285 				 * deadlocked if we put this command back onto
14286 				 * the waitq for later retry (since un_retry_bp
14287 				 * must complete before the driver gets back to
14288 				 * commands on the waitq).
14289 				 *
14290 				 * To avoid deadlock we must schedule a callback
14291 				 * that will restart this command after a set
14292 				 * interval.  This should keep retrying for as
14293 				 * long as the underlying transport keeps
14294 				 * returning TRAN_BUSY (just like for other
14295 				 * commands).  Use the same timeout interval as
14296 				 * for the ordinary TRAN_BUSY retry.
14297 				 */
14298 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14299 				    "sd_start_cmds: scsi_transport() returned "
14300 				    "TRAN_BUSY for DIRECT_PRIORITY cmd!\n");
14301 
14302 				SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14303 				un->un_direct_priority_timeid =
14304 				    timeout(sd_start_direct_priority_command,
14305 				    bp, SD_BSY_TIMEOUT / 500);
14306 
14307 				goto exit;
14308 			}
14309 
14310 			/*
14311 			 * For TRAN_BUSY, we want to reduce the throttle value,
14312 			 * unless we are retrying a command.
14313 			 */
14314 			if (bp != un->un_retry_bp) {
14315 				sd_reduce_throttle(un, SD_THROTTLE_TRAN_BUSY);
14316 			}
14317 
14318 			/*
14319 			 * Set up the bp to be tried again 10 ms later.
14320 			 * Note:x86: Is there a timeout value in the sd_lun
14321 			 * for this condition?
14322 			 */
14323 			sd_set_retry_bp(un, bp, SD_BSY_TIMEOUT / 500,
14324 				kstat_runq_back_to_waitq);
14325 			goto exit;
14326 
14327 		case TRAN_FATAL_ERROR:
14328 			un->un_tran_fatal_count++;
14329 			/* FALLTHRU */
14330 
14331 		case TRAN_BADPKT:
14332 		default:
14333 			un->un_ncmds_in_transport--;
14334 			ASSERT(un->un_ncmds_in_transport >= 0);
14335 
14336 			/*
14337 			 * If this is our REQUEST SENSE command with a
14338 			 * transport error, we must get back the pointers
14339 			 * to the original buf, and mark the REQUEST
14340 			 * SENSE command as "available".
14341 			 */
14342 			if (bp == un->un_rqs_bp) {
14343 				bp = sd_mark_rqs_idle(un, xp);
14344 				xp = SD_GET_XBUF(bp);
14345 			} else {
14346 				/*
14347 				 * Legacy behavior: do not update transport
14348 				 * error count for request sense commands.
14349 				 */
14350 				SD_UPDATE_ERRSTATS(un, sd_transerrs);
14351 			}
14352 
14353 			SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
14354 			sd_print_transport_rejected_message(un, xp, rval);
14355 
14356 			/*
14357 			 * We must use sd_return_failed_command_no_restart() to
14358 			 * avoid a recursive call back into sd_start_cmds().
14359 			 * However this also means that we must keep processing
14360 			 * the waitq here in order to avoid stalling.
14361 			 */
14362 			sd_return_failed_command_no_restart(un, bp, EIO);
14363 
14364 			/*
14365 			 * Notify any threads waiting in sd_ddi_suspend() that
14366 			 * a command completion has occurred.
14367 			 */
14368 			if (un->un_state == SD_STATE_SUSPENDED) {
14369 				cv_broadcast(&un->un_disk_busy_cv);
14370 			}
14371 
14372 			if (bp == immed_bp) {
14373 				/* immed_bp is gone by now, so clear this */
14374 				immed_bp = NULL;
14375 			}
14376 			break;
14377 		}
14378 
14379 	} while (immed_bp == NULL);
14380 
14381 exit:
14382 	ASSERT(mutex_owned(SD_MUTEX(un)));
14383 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_start_cmds: exit\n");
14384 }
14385 
14386 
14387 /*
14388  *    Function: sd_return_command
14389  *
14390  * Description: Returns a command to its originator (with or without an
14391  *		error).  Also starts commands waiting to be transported
14392  *		to the target.
14393  *
14394  *     Context: May be called from interrupt, kernel, or timeout context
14395  */
14396 
14397 static void
14398 sd_return_command(struct sd_lun *un, struct buf *bp)
14399 {
14400 	struct sd_xbuf *xp;
14401 #if defined(__i386) || defined(__amd64)
14402 	struct scsi_pkt *pktp;
14403 #endif
14404 
14405 	ASSERT(bp != NULL);
14406 	ASSERT(un != NULL);
14407 	ASSERT(mutex_owned(SD_MUTEX(un)));
14408 	ASSERT(bp != un->un_rqs_bp);
14409 	xp = SD_GET_XBUF(bp);
14410 	ASSERT(xp != NULL);
14411 
14412 #if defined(__i386) || defined(__amd64)
14413 	pktp = SD_GET_PKTP(bp);
14414 #endif
14415 
14416 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: entry\n");
14417 
14418 #if defined(__i386) || defined(__amd64)
14419 	/*
14420 	 * Note:x86: check for the "sdrestart failed" case.
14421 	 */
14422 	if (((xp->xb_pkt_flags & SD_XB_USCSICMD) != SD_XB_USCSICMD) &&
14423 		(geterror(bp) == 0) && (xp->xb_dma_resid != 0) &&
14424 		(xp->xb_pktp->pkt_resid == 0)) {
14425 
14426 		if (sd_setup_next_xfer(un, bp, pktp, xp) != 0) {
14427 			/*
14428 			 * Successfully set up next portion of cmd
14429 			 * transfer, try sending it
14430 			 */
14431 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
14432 			    NULL, NULL, 0, (clock_t)0, NULL);
14433 			sd_start_cmds(un, NULL);
14434 			return;	/* Note:x86: need a return here? */
14435 		}
14436 	}
14437 #endif
14438 
14439 	/*
14440 	 * If this is the failfast bp, clear it from un_failfast_bp. This
14441 	 * can happen if upon being re-tried the failfast bp either
14442 	 * succeeded or encountered another error (possibly even a different
14443 	 * error than the one that precipitated the failfast state, but in
14444 	 * that case it would have had to exhaust retries as well). Regardless,
14445 	 * this should not occur whenever the instance is in the active
14446 	 * failfast state.
14447 	 */
14448 	if (bp == un->un_failfast_bp) {
14449 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
14450 		un->un_failfast_bp = NULL;
14451 	}
14452 
14453 	/*
14454 	 * Clear the failfast state upon successful completion of ANY cmd.
14455 	 */
14456 	if (bp->b_error == 0) {
14457 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
14458 	}
14459 
14460 	/*
14461 	 * This is used if the command was retried one or more times. Show that
14462 	 * we are done with it, and allow processing of the waitq to resume.
14463 	 */
14464 	if (bp == un->un_retry_bp) {
14465 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14466 		    "sd_return_command: un:0x%p: "
14467 		    "RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
14468 		un->un_retry_bp = NULL;
14469 		un->un_retry_statp = NULL;
14470 	}
14471 
14472 	SD_UPDATE_RDWR_STATS(un, bp);
14473 	SD_UPDATE_PARTITION_STATS(un, bp);
14474 
14475 	switch (un->un_state) {
14476 	case SD_STATE_SUSPENDED:
14477 		/*
14478 		 * Notify any threads waiting in sd_ddi_suspend() that
14479 		 * a command completion has occurred.
14480 		 */
14481 		cv_broadcast(&un->un_disk_busy_cv);
14482 		break;
14483 	default:
14484 		sd_start_cmds(un, NULL);
14485 		break;
14486 	}
14487 
14488 	/* Return this command up the iodone chain to its originator. */
14489 	mutex_exit(SD_MUTEX(un));
14490 
14491 	(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
14492 	xp->xb_pktp = NULL;
14493 
14494 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
14495 
14496 	ASSERT(!mutex_owned(SD_MUTEX(un)));
14497 	mutex_enter(SD_MUTEX(un));
14498 
14499 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_return_command: exit\n");
14500 }
14501 
14502 
14503 /*
14504  *    Function: sd_return_failed_command
14505  *
14506  * Description: Command completion when an error occurred.
14507  *
14508  *     Context: May be called from interrupt context
14509  */
14510 
14511 static void
14512 sd_return_failed_command(struct sd_lun *un, struct buf *bp, int errcode)
14513 {
14514 	ASSERT(bp != NULL);
14515 	ASSERT(un != NULL);
14516 	ASSERT(mutex_owned(SD_MUTEX(un)));
14517 
14518 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14519 	    "sd_return_failed_command: entry\n");
14520 
14521 	/*
14522 	 * b_resid could already be nonzero due to a partial data
14523 	 * transfer, so do not change it here.
14524 	 */
14525 	SD_BIOERROR(bp, errcode);
14526 
14527 	sd_return_command(un, bp);
14528 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14529 	    "sd_return_failed_command: exit\n");
14530 }
14531 
14532 
14533 /*
14534  *    Function: sd_return_failed_command_no_restart
14535  *
14536  * Description: Same as sd_return_failed_command, but ensures that no
14537  *		call back into sd_start_cmds will be issued.
14538  *
14539  *     Context: May be called from interrupt context
14540  */
14541 
14542 static void
14543 sd_return_failed_command_no_restart(struct sd_lun *un, struct buf *bp,
14544 	int errcode)
14545 {
14546 	struct sd_xbuf *xp;
14547 
14548 	ASSERT(bp != NULL);
14549 	ASSERT(un != NULL);
14550 	ASSERT(mutex_owned(SD_MUTEX(un)));
14551 	xp = SD_GET_XBUF(bp);
14552 	ASSERT(xp != NULL);
14553 	ASSERT(errcode != 0);
14554 
14555 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14556 	    "sd_return_failed_command_no_restart: entry\n");
14557 
14558 	/*
14559 	 * b_resid could already be nonzero due to a partial data
14560 	 * transfer, so do not change it here.
14561 	 */
14562 	SD_BIOERROR(bp, errcode);
14563 
14564 	/*
14565 	 * If this is the failfast bp, clear it. This can happen if the
14566 	 * failfast bp encounterd a fatal error when we attempted to
14567 	 * re-try it (such as a scsi_transport(9F) failure).  However
14568 	 * we should NOT be in an active failfast state if the failfast
14569 	 * bp is not NULL.
14570 	 */
14571 	if (bp == un->un_failfast_bp) {
14572 		ASSERT(un->un_failfast_state == SD_FAILFAST_INACTIVE);
14573 		un->un_failfast_bp = NULL;
14574 	}
14575 
14576 	if (bp == un->un_retry_bp) {
14577 		/*
14578 		 * This command was retried one or more times. Show that we are
14579 		 * done with it, and allow processing of the waitq to resume.
14580 		 */
14581 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14582 		    "sd_return_failed_command_no_restart: "
14583 		    " un:0x%p: RETURNING retry_bp:0x%p\n", un, un->un_retry_bp);
14584 		un->un_retry_bp = NULL;
14585 		un->un_retry_statp = NULL;
14586 	}
14587 
14588 	SD_UPDATE_RDWR_STATS(un, bp);
14589 	SD_UPDATE_PARTITION_STATS(un, bp);
14590 
14591 	mutex_exit(SD_MUTEX(un));
14592 
14593 	if (xp->xb_pktp != NULL) {
14594 		(*(sd_destroypkt_map[xp->xb_chain_iodone]))(bp);
14595 		xp->xb_pktp = NULL;
14596 	}
14597 
14598 	SD_BEGIN_IODONE(xp->xb_chain_iodone, un, bp);
14599 
14600 	mutex_enter(SD_MUTEX(un));
14601 
14602 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14603 	    "sd_return_failed_command_no_restart: exit\n");
14604 }
14605 
14606 
14607 /*
14608  *    Function: sd_retry_command
14609  *
14610  * Description: queue up a command for retry, or (optionally) fail it
14611  *		if retry counts are exhausted.
14612  *
14613  *   Arguments: un - Pointer to the sd_lun struct for the target.
14614  *
14615  *		bp - Pointer to the buf for the command to be retried.
14616  *
14617  *		retry_check_flag - Flag to see which (if any) of the retry
14618  *		   counts should be decremented/checked. If the indicated
14619  *		   retry count is exhausted, then the command will not be
14620  *		   retried; it will be failed instead. This should use a
14621  *		   value equal to one of the following:
14622  *
14623  *			SD_RETRIES_NOCHECK
14624  *			SD_RESD_RETRIES_STANDARD
14625  *			SD_RETRIES_VICTIM
14626  *
14627  *		   Optionally may be bitwise-OR'ed with SD_RETRIES_ISOLATE
14628  *		   if the check should be made to see of FLAG_ISOLATE is set
14629  *		   in the pkt. If FLAG_ISOLATE is set, then the command is
14630  *		   not retried, it is simply failed.
14631  *
14632  *		user_funcp - Ptr to function to call before dispatching the
14633  *		   command. May be NULL if no action needs to be performed.
14634  *		   (Primarily intended for printing messages.)
14635  *
14636  *		user_arg - Optional argument to be passed along to
14637  *		   the user_funcp call.
14638  *
14639  *		failure_code - errno return code to set in the bp if the
14640  *		   command is going to be failed.
14641  *
14642  *		retry_delay - Retry delay interval in (clock_t) units. May
14643  *		   be zero which indicates that the retry should be retried
14644  *		   immediately (ie, without an intervening delay).
14645  *
14646  *		statp - Ptr to kstat function to be updated if the command
14647  *		   is queued for a delayed retry. May be NULL if no kstat
14648  *		   update is desired.
14649  *
14650  *     Context: May be called from interupt context.
14651  */
14652 
14653 static void
14654 sd_retry_command(struct sd_lun *un, struct buf *bp, int retry_check_flag,
14655 	void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int
14656 	code), void *user_arg, int failure_code,  clock_t retry_delay,
14657 	void (*statp)(kstat_io_t *))
14658 {
14659 	struct sd_xbuf	*xp;
14660 	struct scsi_pkt	*pktp;
14661 
14662 	ASSERT(un != NULL);
14663 	ASSERT(mutex_owned(SD_MUTEX(un)));
14664 	ASSERT(bp != NULL);
14665 	xp = SD_GET_XBUF(bp);
14666 	ASSERT(xp != NULL);
14667 	pktp = SD_GET_PKTP(bp);
14668 	ASSERT(pktp != NULL);
14669 
14670 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14671 	    "sd_retry_command: entry: bp:0x%p xp:0x%p\n", bp, xp);
14672 
14673 	/*
14674 	 * If we are syncing or dumping, fail the command to avoid
14675 	 * recursively calling back into scsi_transport().
14676 	 */
14677 	if (ddi_in_panic()) {
14678 		goto fail_command_no_log;
14679 	}
14680 
14681 	/*
14682 	 * We should never be be retrying a command with FLAG_DIAGNOSE set, so
14683 	 * log an error and fail the command.
14684 	 */
14685 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
14686 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
14687 		    "ERROR, retrying FLAG_DIAGNOSE command.\n");
14688 		sd_dump_memory(un, SD_LOG_IO, "CDB",
14689 		    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
14690 		sd_dump_memory(un, SD_LOG_IO, "Sense Data",
14691 		    (uchar_t *)xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
14692 		goto fail_command;
14693 	}
14694 
14695 	/*
14696 	 * If we are suspended, then put the command onto head of the
14697 	 * wait queue since we don't want to start more commands.
14698 	 */
14699 	switch (un->un_state) {
14700 	case SD_STATE_SUSPENDED:
14701 	case SD_STATE_DUMPING:
14702 		bp->av_forw = un->un_waitq_headp;
14703 		un->un_waitq_headp = bp;
14704 		if (un->un_waitq_tailp == NULL) {
14705 			un->un_waitq_tailp = bp;
14706 		}
14707 		SD_UPDATE_KSTATS(un, kstat_waitq_enter, bp);
14708 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: "
14709 		    "exiting; cmd bp:0x%p requeued for SUSPEND/DUMP\n", bp);
14710 		return;
14711 	default:
14712 		break;
14713 	}
14714 
14715 	/*
14716 	 * If the caller wants us to check FLAG_ISOLATE, then see if that
14717 	 * is set; if it is then we do not want to retry the command.
14718 	 * Normally, FLAG_ISOLATE is only used with USCSI cmds.
14719 	 */
14720 	if ((retry_check_flag & SD_RETRIES_ISOLATE) != 0) {
14721 		if ((pktp->pkt_flags & FLAG_ISOLATE) != 0) {
14722 			goto fail_command;
14723 		}
14724 	}
14725 
14726 
14727 	/*
14728 	 * If SD_RETRIES_FAILFAST is set, it indicates that either a
14729 	 * command timeout or a selection timeout has occurred. This means
14730 	 * that we were unable to establish an kind of communication with
14731 	 * the target, and subsequent retries and/or commands are likely
14732 	 * to encounter similar results and take a long time to complete.
14733 	 *
14734 	 * If this is a failfast error condition, we need to update the
14735 	 * failfast state, even if this bp does not have B_FAILFAST set.
14736 	 */
14737 	if (retry_check_flag & SD_RETRIES_FAILFAST) {
14738 		if (un->un_failfast_state == SD_FAILFAST_ACTIVE) {
14739 			ASSERT(un->un_failfast_bp == NULL);
14740 			/*
14741 			 * If we are already in the active failfast state, and
14742 			 * another failfast error condition has been detected,
14743 			 * then fail this command if it has B_FAILFAST set.
14744 			 * If B_FAILFAST is clear, then maintain the legacy
14745 			 * behavior of retrying heroically, even tho this will
14746 			 * take a lot more time to fail the command.
14747 			 */
14748 			if (bp->b_flags & B_FAILFAST) {
14749 				goto fail_command;
14750 			}
14751 		} else {
14752 			/*
14753 			 * We're not in the active failfast state, but we
14754 			 * have a failfast error condition, so we must begin
14755 			 * transition to the next state. We do this regardless
14756 			 * of whether or not this bp has B_FAILFAST set.
14757 			 */
14758 			if (un->un_failfast_bp == NULL) {
14759 				/*
14760 				 * This is the first bp to meet a failfast
14761 				 * condition so save it on un_failfast_bp &
14762 				 * do normal retry processing. Do not enter
14763 				 * active failfast state yet. This marks
14764 				 * entry into the "failfast pending" state.
14765 				 */
14766 				un->un_failfast_bp = bp;
14767 
14768 			} else if (un->un_failfast_bp == bp) {
14769 				/*
14770 				 * This is the second time *this* bp has
14771 				 * encountered a failfast error condition,
14772 				 * so enter active failfast state & flush
14773 				 * queues as appropriate.
14774 				 */
14775 				un->un_failfast_state = SD_FAILFAST_ACTIVE;
14776 				un->un_failfast_bp = NULL;
14777 				sd_failfast_flushq(un);
14778 
14779 				/*
14780 				 * Fail this bp now if B_FAILFAST set;
14781 				 * otherwise continue with retries. (It would
14782 				 * be pretty ironic if this bp succeeded on a
14783 				 * subsequent retry after we just flushed all
14784 				 * the queues).
14785 				 */
14786 				if (bp->b_flags & B_FAILFAST) {
14787 					goto fail_command;
14788 				}
14789 
14790 #if !defined(lint) && !defined(__lint)
14791 			} else {
14792 				/*
14793 				 * If neither of the preceeding conditionals
14794 				 * was true, it means that there is some
14795 				 * *other* bp that has met an inital failfast
14796 				 * condition and is currently either being
14797 				 * retried or is waiting to be retried. In
14798 				 * that case we should perform normal retry
14799 				 * processing on *this* bp, since there is a
14800 				 * chance that the current failfast condition
14801 				 * is transient and recoverable. If that does
14802 				 * not turn out to be the case, then retries
14803 				 * will be cleared when the wait queue is
14804 				 * flushed anyway.
14805 				 */
14806 #endif
14807 			}
14808 		}
14809 	} else {
14810 		/*
14811 		 * SD_RETRIES_FAILFAST is clear, which indicates that we
14812 		 * likely were able to at least establish some level of
14813 		 * communication with the target and subsequent commands
14814 		 * and/or retries are likely to get through to the target,
14815 		 * In this case we want to be aggressive about clearing
14816 		 * the failfast state. Note that this does not affect
14817 		 * the "failfast pending" condition.
14818 		 */
14819 		un->un_failfast_state = SD_FAILFAST_INACTIVE;
14820 	}
14821 
14822 
14823 	/*
14824 	 * Check the specified retry count to see if we can still do
14825 	 * any retries with this pkt before we should fail it.
14826 	 */
14827 	switch (retry_check_flag & SD_RETRIES_MASK) {
14828 	case SD_RETRIES_VICTIM:
14829 		/*
14830 		 * Check the victim retry count. If exhausted, then fall
14831 		 * thru & check against the standard retry count.
14832 		 */
14833 		if (xp->xb_victim_retry_count < un->un_victim_retry_count) {
14834 			/* Increment count & proceed with the retry */
14835 			xp->xb_victim_retry_count++;
14836 			break;
14837 		}
14838 		/* Victim retries exhausted, fall back to std. retries... */
14839 		/* FALLTHRU */
14840 
14841 	case SD_RETRIES_STANDARD:
14842 		if (xp->xb_retry_count >= un->un_retry_count) {
14843 			/* Retries exhausted, fail the command */
14844 			SD_TRACE(SD_LOG_IO_CORE, un,
14845 			    "sd_retry_command: retries exhausted!\n");
14846 			/*
14847 			 * update b_resid for failed SCMD_READ & SCMD_WRITE
14848 			 * commands with nonzero pkt_resid.
14849 			 */
14850 			if ((pktp->pkt_reason == CMD_CMPLT) &&
14851 			    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD) &&
14852 			    (pktp->pkt_resid != 0)) {
14853 				uchar_t op = SD_GET_PKT_OPCODE(pktp) & 0x1F;
14854 				if ((op == SCMD_READ) || (op == SCMD_WRITE)) {
14855 					SD_UPDATE_B_RESID(bp, pktp);
14856 				}
14857 			}
14858 			goto fail_command;
14859 		}
14860 		xp->xb_retry_count++;
14861 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14862 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
14863 		break;
14864 
14865 	case SD_RETRIES_UA:
14866 		if (xp->xb_ua_retry_count >= sd_ua_retry_count) {
14867 			/* Retries exhausted, fail the command */
14868 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
14869 			    "Unit Attention retries exhausted. "
14870 			    "Check the target.\n");
14871 			goto fail_command;
14872 		}
14873 		xp->xb_ua_retry_count++;
14874 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14875 		    "sd_retry_command: retry count:%d\n",
14876 			xp->xb_ua_retry_count);
14877 		break;
14878 
14879 	case SD_RETRIES_BUSY:
14880 		if (xp->xb_retry_count >= un->un_busy_retry_count) {
14881 			/* Retries exhausted, fail the command */
14882 			SD_TRACE(SD_LOG_IO_CORE, un,
14883 			    "sd_retry_command: retries exhausted!\n");
14884 			goto fail_command;
14885 		}
14886 		xp->xb_retry_count++;
14887 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14888 		    "sd_retry_command: retry count:%d\n", xp->xb_retry_count);
14889 		break;
14890 
14891 	case SD_RETRIES_NOCHECK:
14892 	default:
14893 		/* No retry count to check. Just proceed with the retry */
14894 		break;
14895 	}
14896 
14897 	xp->xb_pktp->pkt_flags |= FLAG_HEAD;
14898 
14899 	/*
14900 	 * If we were given a zero timeout, we must attempt to retry the
14901 	 * command immediately (ie, without a delay).
14902 	 */
14903 	if (retry_delay == 0) {
14904 		/*
14905 		 * Check some limiting conditions to see if we can actually
14906 		 * do the immediate retry.  If we cannot, then we must
14907 		 * fall back to queueing up a delayed retry.
14908 		 */
14909 		if (un->un_ncmds_in_transport >= un->un_throttle) {
14910 			/*
14911 			 * We are at the throttle limit for the target,
14912 			 * fall back to delayed retry.
14913 			 */
14914 			retry_delay = SD_BSY_TIMEOUT;
14915 			statp = kstat_waitq_enter;
14916 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14917 			    "sd_retry_command: immed. retry hit throttle!\n");
14918 		} else {
14919 			/*
14920 			 * We're clear to proceed with the immediate retry.
14921 			 * First call the user-provided function (if any)
14922 			 */
14923 			if (user_funcp != NULL) {
14924 				(*user_funcp)(un, bp, user_arg,
14925 				    SD_IMMEDIATE_RETRY_ISSUED);
14926 			}
14927 
14928 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14929 			    "sd_retry_command: issuing immediate retry\n");
14930 
14931 			/*
14932 			 * Call sd_start_cmds() to transport the command to
14933 			 * the target.
14934 			 */
14935 			sd_start_cmds(un, bp);
14936 
14937 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14938 			    "sd_retry_command exit\n");
14939 			return;
14940 		}
14941 	}
14942 
14943 	/*
14944 	 * Set up to retry the command after a delay.
14945 	 * First call the user-provided function (if any)
14946 	 */
14947 	if (user_funcp != NULL) {
14948 		(*user_funcp)(un, bp, user_arg, SD_DELAYED_RETRY_ISSUED);
14949 	}
14950 
14951 	sd_set_retry_bp(un, bp, retry_delay, statp);
14952 
14953 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
14954 	return;
14955 
14956 fail_command:
14957 
14958 	if (user_funcp != NULL) {
14959 		(*user_funcp)(un, bp, user_arg, SD_NO_RETRY_ISSUED);
14960 	}
14961 
14962 fail_command_no_log:
14963 
14964 	SD_INFO(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
14965 	    "sd_retry_command: returning failed command\n");
14966 
14967 	sd_return_failed_command(un, bp, failure_code);
14968 
14969 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_retry_command: exit\n");
14970 }
14971 
14972 
14973 /*
14974  *    Function: sd_set_retry_bp
14975  *
14976  * Description: Set up the given bp for retry.
14977  *
14978  *   Arguments: un - ptr to associated softstate
14979  *		bp - ptr to buf(9S) for the command
14980  *		retry_delay - time interval before issuing retry (may be 0)
14981  *		statp - optional pointer to kstat function
14982  *
14983  *     Context: May be called under interrupt context
14984  */
14985 
14986 static void
14987 sd_set_retry_bp(struct sd_lun *un, struct buf *bp, clock_t retry_delay,
14988 	void (*statp)(kstat_io_t *))
14989 {
14990 	ASSERT(un != NULL);
14991 	ASSERT(mutex_owned(SD_MUTEX(un)));
14992 	ASSERT(bp != NULL);
14993 
14994 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
14995 	    "sd_set_retry_bp: entry: un:0x%p bp:0x%p\n", un, bp);
14996 
14997 	/*
14998 	 * Indicate that the command is being retried. This will not allow any
14999 	 * other commands on the wait queue to be transported to the target
15000 	 * until this command has been completed (success or failure). The
15001 	 * "retry command" is not transported to the target until the given
15002 	 * time delay expires, unless the user specified a 0 retry_delay.
15003 	 *
15004 	 * Note: the timeout(9F) callback routine is what actually calls
15005 	 * sd_start_cmds() to transport the command, with the exception of a
15006 	 * zero retry_delay. The only current implementor of a zero retry delay
15007 	 * is the case where a START_STOP_UNIT is sent to spin-up a device.
15008 	 */
15009 	if (un->un_retry_bp == NULL) {
15010 		ASSERT(un->un_retry_statp == NULL);
15011 		un->un_retry_bp = bp;
15012 
15013 		/*
15014 		 * If the user has not specified a delay the command should
15015 		 * be queued and no timeout should be scheduled.
15016 		 */
15017 		if (retry_delay == 0) {
15018 			/*
15019 			 * Save the kstat pointer that will be used in the
15020 			 * call to SD_UPDATE_KSTATS() below, so that
15021 			 * sd_start_cmds() can correctly decrement the waitq
15022 			 * count when it is time to transport this command.
15023 			 */
15024 			un->un_retry_statp = statp;
15025 			goto done;
15026 		}
15027 	}
15028 
15029 	if (un->un_retry_bp == bp) {
15030 		/*
15031 		 * Save the kstat pointer that will be used in the call to
15032 		 * SD_UPDATE_KSTATS() below, so that sd_start_cmds() can
15033 		 * correctly decrement the waitq count when it is time to
15034 		 * transport this command.
15035 		 */
15036 		un->un_retry_statp = statp;
15037 
15038 		/*
15039 		 * Schedule a timeout if:
15040 		 *   1) The user has specified a delay.
15041 		 *   2) There is not a START_STOP_UNIT callback pending.
15042 		 *
15043 		 * If no delay has been specified, then it is up to the caller
15044 		 * to ensure that IO processing continues without stalling.
15045 		 * Effectively, this means that the caller will issue the
15046 		 * required call to sd_start_cmds(). The START_STOP_UNIT
15047 		 * callback does this after the START STOP UNIT command has
15048 		 * completed. In either of these cases we should not schedule
15049 		 * a timeout callback here.  Also don't schedule the timeout if
15050 		 * an SD_PATH_DIRECT_PRIORITY command is waiting to restart.
15051 		 */
15052 		if ((retry_delay != 0) && (un->un_startstop_timeid == NULL) &&
15053 		    (un->un_direct_priority_timeid == NULL)) {
15054 			un->un_retry_timeid =
15055 			    timeout(sd_start_retry_command, un, retry_delay);
15056 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15057 			    "sd_set_retry_bp: setting timeout: un: 0x%p"
15058 			    " bp:0x%p un_retry_timeid:0x%p\n",
15059 			    un, bp, un->un_retry_timeid);
15060 		}
15061 	} else {
15062 		/*
15063 		 * We only get in here if there is already another command
15064 		 * waiting to be retried.  In this case, we just put the
15065 		 * given command onto the wait queue, so it can be transported
15066 		 * after the current retry command has completed.
15067 		 *
15068 		 * Also we have to make sure that if the command at the head
15069 		 * of the wait queue is the un_failfast_bp, that we do not
15070 		 * put ahead of it any other commands that are to be retried.
15071 		 */
15072 		if ((un->un_failfast_bp != NULL) &&
15073 		    (un->un_failfast_bp == un->un_waitq_headp)) {
15074 			/*
15075 			 * Enqueue this command AFTER the first command on
15076 			 * the wait queue (which is also un_failfast_bp).
15077 			 */
15078 			bp->av_forw = un->un_waitq_headp->av_forw;
15079 			un->un_waitq_headp->av_forw = bp;
15080 			if (un->un_waitq_headp == un->un_waitq_tailp) {
15081 				un->un_waitq_tailp = bp;
15082 			}
15083 		} else {
15084 			/* Enqueue this command at the head of the waitq. */
15085 			bp->av_forw = un->un_waitq_headp;
15086 			un->un_waitq_headp = bp;
15087 			if (un->un_waitq_tailp == NULL) {
15088 				un->un_waitq_tailp = bp;
15089 			}
15090 		}
15091 
15092 		if (statp == NULL) {
15093 			statp = kstat_waitq_enter;
15094 		}
15095 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15096 		    "sd_set_retry_bp: un:0x%p already delayed retry\n", un);
15097 	}
15098 
15099 done:
15100 	if (statp != NULL) {
15101 		SD_UPDATE_KSTATS(un, statp, bp);
15102 	}
15103 
15104 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15105 	    "sd_set_retry_bp: exit un:0x%p\n", un);
15106 }
15107 
15108 
15109 /*
15110  *    Function: sd_start_retry_command
15111  *
15112  * Description: Start the command that has been waiting on the target's
15113  *		retry queue.  Called from timeout(9F) context after the
15114  *		retry delay interval has expired.
15115  *
15116  *   Arguments: arg - pointer to associated softstate for the device.
15117  *
15118  *     Context: timeout(9F) thread context.  May not sleep.
15119  */
15120 
15121 static void
15122 sd_start_retry_command(void *arg)
15123 {
15124 	struct sd_lun *un = arg;
15125 
15126 	ASSERT(un != NULL);
15127 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15128 
15129 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15130 	    "sd_start_retry_command: entry\n");
15131 
15132 	mutex_enter(SD_MUTEX(un));
15133 
15134 	un->un_retry_timeid = NULL;
15135 
15136 	if (un->un_retry_bp != NULL) {
15137 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15138 		    "sd_start_retry_command: un:0x%p STARTING bp:0x%p\n",
15139 		    un, un->un_retry_bp);
15140 		sd_start_cmds(un, un->un_retry_bp);
15141 	}
15142 
15143 	mutex_exit(SD_MUTEX(un));
15144 
15145 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15146 	    "sd_start_retry_command: exit\n");
15147 }
15148 
15149 
15150 /*
15151  *    Function: sd_start_direct_priority_command
15152  *
15153  * Description: Used to re-start an SD_PATH_DIRECT_PRIORITY command that had
15154  *		received TRAN_BUSY when we called scsi_transport() to send it
15155  *		to the underlying HBA. This function is called from timeout(9F)
15156  *		context after the delay interval has expired.
15157  *
15158  *   Arguments: arg - pointer to associated buf(9S) to be restarted.
15159  *
15160  *     Context: timeout(9F) thread context.  May not sleep.
15161  */
15162 
15163 static void
15164 sd_start_direct_priority_command(void *arg)
15165 {
15166 	struct buf	*priority_bp = arg;
15167 	struct sd_lun	*un;
15168 
15169 	ASSERT(priority_bp != NULL);
15170 	un = SD_GET_UN(priority_bp);
15171 	ASSERT(un != NULL);
15172 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15173 
15174 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15175 	    "sd_start_direct_priority_command: entry\n");
15176 
15177 	mutex_enter(SD_MUTEX(un));
15178 	un->un_direct_priority_timeid = NULL;
15179 	sd_start_cmds(un, priority_bp);
15180 	mutex_exit(SD_MUTEX(un));
15181 
15182 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15183 	    "sd_start_direct_priority_command: exit\n");
15184 }
15185 
15186 
15187 /*
15188  *    Function: sd_send_request_sense_command
15189  *
15190  * Description: Sends a REQUEST SENSE command to the target
15191  *
15192  *     Context: May be called from interrupt context.
15193  */
15194 
15195 static void
15196 sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
15197 	struct scsi_pkt *pktp)
15198 {
15199 	ASSERT(bp != NULL);
15200 	ASSERT(un != NULL);
15201 	ASSERT(mutex_owned(SD_MUTEX(un)));
15202 
15203 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_send_request_sense_command: "
15204 	    "entry: buf:0x%p\n", bp);
15205 
15206 	/*
15207 	 * If we are syncing or dumping, then fail the command to avoid a
15208 	 * recursive callback into scsi_transport(). Also fail the command
15209 	 * if we are suspended (legacy behavior).
15210 	 */
15211 	if (ddi_in_panic() || (un->un_state == SD_STATE_SUSPENDED) ||
15212 	    (un->un_state == SD_STATE_DUMPING)) {
15213 		sd_return_failed_command(un, bp, EIO);
15214 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15215 		    "sd_send_request_sense_command: syncing/dumping, exit\n");
15216 		return;
15217 	}
15218 
15219 	/*
15220 	 * Retry the failed command and don't issue the request sense if:
15221 	 *    1) the sense buf is busy
15222 	 *    2) we have 1 or more outstanding commands on the target
15223 	 *    (the sense data will be cleared or invalidated any way)
15224 	 *
15225 	 * Note: There could be an issue with not checking a retry limit here,
15226 	 * the problem is determining which retry limit to check.
15227 	 */
15228 	if ((un->un_sense_isbusy != 0) || (un->un_ncmds_in_transport > 0)) {
15229 		/* Don't retry if the command is flagged as non-retryable */
15230 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
15231 			sd_retry_command(un, bp, SD_RETRIES_NOCHECK,
15232 			    NULL, NULL, 0, SD_BSY_TIMEOUT, kstat_waitq_enter);
15233 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15234 			    "sd_send_request_sense_command: "
15235 			    "at full throttle, retrying exit\n");
15236 		} else {
15237 			sd_return_failed_command(un, bp, EIO);
15238 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15239 			    "sd_send_request_sense_command: "
15240 			    "at full throttle, non-retryable exit\n");
15241 		}
15242 		return;
15243 	}
15244 
15245 	sd_mark_rqs_busy(un, bp);
15246 	sd_start_cmds(un, un->un_rqs_bp);
15247 
15248 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15249 	    "sd_send_request_sense_command: exit\n");
15250 }
15251 
15252 
15253 /*
15254  *    Function: sd_mark_rqs_busy
15255  *
15256  * Description: Indicate that the request sense bp for this instance is
15257  *		in use.
15258  *
15259  *     Context: May be called under interrupt context
15260  */
15261 
15262 static void
15263 sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp)
15264 {
15265 	struct sd_xbuf	*sense_xp;
15266 
15267 	ASSERT(un != NULL);
15268 	ASSERT(bp != NULL);
15269 	ASSERT(mutex_owned(SD_MUTEX(un)));
15270 	ASSERT(un->un_sense_isbusy == 0);
15271 
15272 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: entry: "
15273 	    "buf:0x%p xp:0x%p un:0x%p\n", bp, SD_GET_XBUF(bp), un);
15274 
15275 	sense_xp = SD_GET_XBUF(un->un_rqs_bp);
15276 	ASSERT(sense_xp != NULL);
15277 
15278 	SD_INFO(SD_LOG_IO, un,
15279 	    "sd_mark_rqs_busy: entry: sense_xp:0x%p\n", sense_xp);
15280 
15281 	ASSERT(sense_xp->xb_pktp != NULL);
15282 	ASSERT((sense_xp->xb_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD))
15283 	    == (FLAG_SENSING | FLAG_HEAD));
15284 
15285 	un->un_sense_isbusy = 1;
15286 	un->un_rqs_bp->b_resid = 0;
15287 	sense_xp->xb_pktp->pkt_resid  = 0;
15288 	sense_xp->xb_pktp->pkt_reason = 0;
15289 
15290 	/* So we can get back the bp at interrupt time! */
15291 	sense_xp->xb_sense_bp = bp;
15292 
15293 	bzero(un->un_rqs_bp->b_un.b_addr, SENSE_LENGTH);
15294 
15295 	/*
15296 	 * Mark this buf as awaiting sense data. (This is already set in
15297 	 * the pkt_flags for the RQS packet.)
15298 	 */
15299 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags |= FLAG_SENSING;
15300 
15301 	sense_xp->xb_retry_count	= 0;
15302 	sense_xp->xb_victim_retry_count = 0;
15303 	sense_xp->xb_ua_retry_count	= 0;
15304 	sense_xp->xb_dma_resid  = 0;
15305 
15306 	/* Clean up the fields for auto-request sense */
15307 	sense_xp->xb_sense_status = 0;
15308 	sense_xp->xb_sense_state  = 0;
15309 	sense_xp->xb_sense_resid  = 0;
15310 	bzero(sense_xp->xb_sense_data, sizeof (sense_xp->xb_sense_data));
15311 
15312 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_mark_rqs_busy: exit\n");
15313 }
15314 
15315 
15316 /*
15317  *    Function: sd_mark_rqs_idle
15318  *
15319  * Description: SD_MUTEX must be held continuously through this routine
15320  *		to prevent reuse of the rqs struct before the caller can
15321  *		complete it's processing.
15322  *
15323  * Return Code: Pointer to the RQS buf
15324  *
15325  *     Context: May be called under interrupt context
15326  */
15327 
15328 static struct buf *
15329 sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *sense_xp)
15330 {
15331 	struct buf *bp;
15332 	ASSERT(un != NULL);
15333 	ASSERT(sense_xp != NULL);
15334 	ASSERT(mutex_owned(SD_MUTEX(un)));
15335 	ASSERT(un->un_sense_isbusy != 0);
15336 
15337 	un->un_sense_isbusy = 0;
15338 	bp = sense_xp->xb_sense_bp;
15339 	sense_xp->xb_sense_bp = NULL;
15340 
15341 	/* This pkt is no longer interested in getting sense data */
15342 	((SD_GET_XBUF(bp))->xb_pktp)->pkt_flags &= ~FLAG_SENSING;
15343 
15344 	return (bp);
15345 }
15346 
15347 
15348 
15349 /*
15350  *    Function: sd_alloc_rqs
15351  *
15352  * Description: Set up the unit to receive auto request sense data
15353  *
15354  * Return Code: DDI_SUCCESS or DDI_FAILURE
15355  *
15356  *     Context: Called under attach(9E) context
15357  */
15358 
15359 static int
15360 sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un)
15361 {
15362 	struct sd_xbuf *xp;
15363 
15364 	ASSERT(un != NULL);
15365 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15366 	ASSERT(un->un_rqs_bp == NULL);
15367 	ASSERT(un->un_rqs_pktp == NULL);
15368 
15369 	/*
15370 	 * First allocate the required buf and scsi_pkt structs, then set up
15371 	 * the CDB in the scsi_pkt for a REQUEST SENSE command.
15372 	 */
15373 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
15374 	    SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL);
15375 	if (un->un_rqs_bp == NULL) {
15376 		return (DDI_FAILURE);
15377 	}
15378 
15379 	un->un_rqs_pktp = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
15380 	    CDB_GROUP0, 1, 0, PKT_CONSISTENT, SLEEP_FUNC, NULL);
15381 
15382 	if (un->un_rqs_pktp == NULL) {
15383 		sd_free_rqs(un);
15384 		return (DDI_FAILURE);
15385 	}
15386 
15387 	/* Set up the CDB in the scsi_pkt for a REQUEST SENSE command. */
15388 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs_pktp->pkt_cdbp,
15389 	    SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
15390 
15391 	sd_fill_scsi1_lun(un, un->un_rqs_pktp);
15392 
15393 	/* Set up the other needed members in the ARQ scsi_pkt. */
15394 	un->un_rqs_pktp->pkt_comp   = sdintr;
15395 	un->un_rqs_pktp->pkt_time   = sd_io_time;
15396 	un->un_rqs_pktp->pkt_flags |=
15397 	    (FLAG_SENSING | FLAG_HEAD);	/* (1222170) */
15398 
15399 	/*
15400 	 * Allocate  & init the sd_xbuf struct for the RQS command. Do not
15401 	 * provide any intpkt, destroypkt routines as we take care of
15402 	 * scsi_pkt allocation/freeing here and in sd_free_rqs().
15403 	 */
15404 	xp = kmem_alloc(sizeof (struct sd_xbuf), KM_SLEEP);
15405 	sd_xbuf_init(un, un->un_rqs_bp, xp, SD_CHAIN_NULL, NULL);
15406 	xp->xb_pktp = un->un_rqs_pktp;
15407 	SD_INFO(SD_LOG_ATTACH_DETACH, un,
15408 	    "sd_alloc_rqs: un 0x%p, rqs  xp 0x%p,  pkt 0x%p,  buf 0x%p\n",
15409 	    un, xp, un->un_rqs_pktp, un->un_rqs_bp);
15410 
15411 	/*
15412 	 * Save the pointer to the request sense private bp so it can
15413 	 * be retrieved in sdintr.
15414 	 */
15415 	un->un_rqs_pktp->pkt_private = un->un_rqs_bp;
15416 	ASSERT(un->un_rqs_bp->b_private == xp);
15417 
15418 	/*
15419 	 * See if the HBA supports auto-request sense for the specified
15420 	 * target/lun. If it does, then try to enable it (if not already
15421 	 * enabled).
15422 	 *
15423 	 * Note: For some HBAs (ifp & sf), scsi_ifsetcap will always return
15424 	 * failure, while for other HBAs (pln) scsi_ifsetcap will always
15425 	 * return success.  However, in both of these cases ARQ is always
15426 	 * enabled and scsi_ifgetcap will always return true. The best approach
15427 	 * is to issue the scsi_ifgetcap() first, then try the scsi_ifsetcap().
15428 	 *
15429 	 * The 3rd case is the HBA (adp) always return enabled on
15430 	 * scsi_ifgetgetcap even when it's not enable, the best approach
15431 	 * is issue a scsi_ifsetcap then a scsi_ifgetcap
15432 	 * Note: this case is to circumvent the Adaptec bug. (x86 only)
15433 	 */
15434 
15435 	if (un->un_f_is_fibre == TRUE) {
15436 		un->un_f_arq_enabled = TRUE;
15437 	} else {
15438 #if defined(__i386) || defined(__amd64)
15439 		/*
15440 		 * Circumvent the Adaptec bug, remove this code when
15441 		 * the bug is fixed
15442 		 */
15443 		(void) scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1);
15444 #endif
15445 		switch (scsi_ifgetcap(SD_ADDRESS(un), "auto-rqsense", 1)) {
15446 		case 0:
15447 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15448 				"sd_alloc_rqs: HBA supports ARQ\n");
15449 			/*
15450 			 * ARQ is supported by this HBA but currently is not
15451 			 * enabled. Attempt to enable it and if successful then
15452 			 * mark this instance as ARQ enabled.
15453 			 */
15454 			if (scsi_ifsetcap(SD_ADDRESS(un), "auto-rqsense", 1, 1)
15455 				== 1) {
15456 				/* Successfully enabled ARQ in the HBA */
15457 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
15458 					"sd_alloc_rqs: ARQ enabled\n");
15459 				un->un_f_arq_enabled = TRUE;
15460 			} else {
15461 				/* Could not enable ARQ in the HBA */
15462 				SD_INFO(SD_LOG_ATTACH_DETACH, un,
15463 				"sd_alloc_rqs: failed ARQ enable\n");
15464 				un->un_f_arq_enabled = FALSE;
15465 			}
15466 			break;
15467 		case 1:
15468 			/*
15469 			 * ARQ is supported by this HBA and is already enabled.
15470 			 * Just mark ARQ as enabled for this instance.
15471 			 */
15472 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15473 				"sd_alloc_rqs: ARQ already enabled\n");
15474 			un->un_f_arq_enabled = TRUE;
15475 			break;
15476 		default:
15477 			/*
15478 			 * ARQ is not supported by this HBA; disable it for this
15479 			 * instance.
15480 			 */
15481 			SD_INFO(SD_LOG_ATTACH_DETACH, un,
15482 				"sd_alloc_rqs: HBA does not support ARQ\n");
15483 			un->un_f_arq_enabled = FALSE;
15484 			break;
15485 		}
15486 	}
15487 
15488 	return (DDI_SUCCESS);
15489 }
15490 
15491 
15492 /*
15493  *    Function: sd_free_rqs
15494  *
15495  * Description: Cleanup for the pre-instance RQS command.
15496  *
15497  *     Context: Kernel thread context
15498  */
15499 
15500 static void
15501 sd_free_rqs(struct sd_lun *un)
15502 {
15503 	ASSERT(un != NULL);
15504 
15505 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: entry\n");
15506 
15507 	/*
15508 	 * If consistent memory is bound to a scsi_pkt, the pkt
15509 	 * has to be destroyed *before* freeing the consistent memory.
15510 	 * Don't change the sequence of this operations.
15511 	 * scsi_destroy_pkt() might access memory, which isn't allowed,
15512 	 * after it was freed in scsi_free_consistent_buf().
15513 	 */
15514 	if (un->un_rqs_pktp != NULL) {
15515 		scsi_destroy_pkt(un->un_rqs_pktp);
15516 		un->un_rqs_pktp = NULL;
15517 	}
15518 
15519 	if (un->un_rqs_bp != NULL) {
15520 		kmem_free(SD_GET_XBUF(un->un_rqs_bp), sizeof (struct sd_xbuf));
15521 		scsi_free_consistent_buf(un->un_rqs_bp);
15522 		un->un_rqs_bp = NULL;
15523 	}
15524 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_free_rqs: exit\n");
15525 }
15526 
15527 
15528 
15529 /*
15530  *    Function: sd_reduce_throttle
15531  *
15532  * Description: Reduces the maximun # of outstanding commands on a
15533  *		target to the current number of outstanding commands.
15534  *		Queues a tiemout(9F) callback to restore the limit
15535  *		after a specified interval has elapsed.
15536  *		Typically used when we get a TRAN_BUSY return code
15537  *		back from scsi_transport().
15538  *
15539  *   Arguments: un - ptr to the sd_lun softstate struct
15540  *		throttle_type: SD_THROTTLE_TRAN_BUSY or SD_THROTTLE_QFULL
15541  *
15542  *     Context: May be called from interrupt context
15543  */
15544 
15545 static void
15546 sd_reduce_throttle(struct sd_lun *un, int throttle_type)
15547 {
15548 	ASSERT(un != NULL);
15549 	ASSERT(mutex_owned(SD_MUTEX(un)));
15550 	ASSERT(un->un_ncmds_in_transport >= 0);
15551 
15552 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
15553 	    "entry: un:0x%p un_throttle:%d un_ncmds_in_transport:%d\n",
15554 	    un, un->un_throttle, un->un_ncmds_in_transport);
15555 
15556 	if (un->un_throttle > 1) {
15557 		if (un->un_f_use_adaptive_throttle == TRUE) {
15558 			switch (throttle_type) {
15559 			case SD_THROTTLE_TRAN_BUSY:
15560 				if (un->un_busy_throttle == 0) {
15561 					un->un_busy_throttle = un->un_throttle;
15562 				}
15563 				break;
15564 			case SD_THROTTLE_QFULL:
15565 				un->un_busy_throttle = 0;
15566 				break;
15567 			default:
15568 				ASSERT(FALSE);
15569 			}
15570 
15571 			if (un->un_ncmds_in_transport > 0) {
15572 				un->un_throttle = un->un_ncmds_in_transport;
15573 			}
15574 		} else {
15575 			if (un->un_ncmds_in_transport == 0) {
15576 				un->un_throttle = 1;
15577 			} else {
15578 				un->un_throttle = un->un_ncmds_in_transport;
15579 			}
15580 		}
15581 	}
15582 
15583 	/* Reschedule the timeout if none is currently active */
15584 	if (un->un_reset_throttle_timeid == NULL) {
15585 		un->un_reset_throttle_timeid = timeout(sd_restore_throttle,
15586 		    un, sd_reset_throttle_timeout);
15587 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15588 		    "sd_reduce_throttle: timeout scheduled!\n");
15589 	}
15590 
15591 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reduce_throttle: "
15592 	    "exit: un:0x%p un_throttle:%d\n", un, un->un_throttle);
15593 }
15594 
15595 
15596 
15597 /*
15598  *    Function: sd_restore_throttle
15599  *
15600  * Description: Callback function for timeout(9F).  Resets the current
15601  *		value of un->un_throttle to its default.
15602  *
15603  *   Arguments: arg - pointer to associated softstate for the device.
15604  *
15605  *     Context: May be called from interrupt context
15606  */
15607 
15608 static void
15609 sd_restore_throttle(void *arg)
15610 {
15611 	struct sd_lun	*un = arg;
15612 
15613 	ASSERT(un != NULL);
15614 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15615 
15616 	mutex_enter(SD_MUTEX(un));
15617 
15618 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
15619 	    "entry: un:0x%p un_throttle:%d\n", un, un->un_throttle);
15620 
15621 	un->un_reset_throttle_timeid = NULL;
15622 
15623 	if (un->un_f_use_adaptive_throttle == TRUE) {
15624 		/*
15625 		 * If un_busy_throttle is nonzero, then it contains the
15626 		 * value that un_throttle was when we got a TRAN_BUSY back
15627 		 * from scsi_transport(). We want to revert back to this
15628 		 * value.
15629 		 */
15630 		if (un->un_busy_throttle > 0) {
15631 			un->un_throttle = un->un_busy_throttle;
15632 			un->un_busy_throttle = 0;
15633 		}
15634 
15635 		/*
15636 		 * If un_throttle has fallen below the low-water mark, we
15637 		 * restore the maximum value here (and allow it to ratchet
15638 		 * down again if necessary).
15639 		 */
15640 		if (un->un_throttle < un->un_min_throttle) {
15641 			un->un_throttle = un->un_saved_throttle;
15642 		}
15643 	} else {
15644 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: "
15645 		    "restoring limit from 0x%x to 0x%x\n",
15646 		    un->un_throttle, un->un_saved_throttle);
15647 		un->un_throttle = un->un_saved_throttle;
15648 	}
15649 
15650 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15651 	    "sd_restore_throttle: calling sd_start_cmds!\n");
15652 
15653 	sd_start_cmds(un, NULL);
15654 
15655 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un,
15656 	    "sd_restore_throttle: exit: un:0x%p un_throttle:%d\n",
15657 	    un, un->un_throttle);
15658 
15659 	mutex_exit(SD_MUTEX(un));
15660 
15661 	SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sd_restore_throttle: exit\n");
15662 }
15663 
15664 /*
15665  *    Function: sdrunout
15666  *
15667  * Description: Callback routine for scsi_init_pkt when a resource allocation
15668  *		fails.
15669  *
15670  *   Arguments: arg - a pointer to the sd_lun unit struct for the particular
15671  *		soft state instance.
15672  *
15673  * Return Code: The scsi_init_pkt routine allows for the callback function to
15674  *		return a 0 indicating the callback should be rescheduled or a 1
15675  *		indicating not to reschedule. This routine always returns 1
15676  *		because the driver always provides a callback function to
15677  *		scsi_init_pkt. This results in a callback always being scheduled
15678  *		(via the scsi_init_pkt callback implementation) if a resource
15679  *		failure occurs.
15680  *
15681  *     Context: This callback function may not block or call routines that block
15682  *
15683  *        Note: Using the scsi_init_pkt callback facility can result in an I/O
15684  *		request persisting at the head of the list which cannot be
15685  *		satisfied even after multiple retries. In the future the driver
15686  *		may implement some time of maximum runout count before failing
15687  *		an I/O.
15688  */
15689 
15690 static int
15691 sdrunout(caddr_t arg)
15692 {
15693 	struct sd_lun	*un = (struct sd_lun *)arg;
15694 
15695 	ASSERT(un != NULL);
15696 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15697 
15698 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: entry\n");
15699 
15700 	mutex_enter(SD_MUTEX(un));
15701 	sd_start_cmds(un, NULL);
15702 	mutex_exit(SD_MUTEX(un));
15703 	/*
15704 	 * This callback routine always returns 1 (i.e. do not reschedule)
15705 	 * because we always specify sdrunout as the callback handler for
15706 	 * scsi_init_pkt inside the call to sd_start_cmds.
15707 	 */
15708 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdrunout: exit\n");
15709 	return (1);
15710 }
15711 
15712 
15713 /*
15714  *    Function: sdintr
15715  *
15716  * Description: Completion callback routine for scsi_pkt(9S) structs
15717  *		sent to the HBA driver via scsi_transport(9F).
15718  *
15719  *     Context: Interrupt context
15720  */
15721 
15722 static void
15723 sdintr(struct scsi_pkt *pktp)
15724 {
15725 	struct buf	*bp;
15726 	struct sd_xbuf	*xp;
15727 	struct sd_lun	*un;
15728 
15729 	ASSERT(pktp != NULL);
15730 	bp = (struct buf *)pktp->pkt_private;
15731 	ASSERT(bp != NULL);
15732 	xp = SD_GET_XBUF(bp);
15733 	ASSERT(xp != NULL);
15734 	ASSERT(xp->xb_pktp != NULL);
15735 	un = SD_GET_UN(bp);
15736 	ASSERT(un != NULL);
15737 	ASSERT(!mutex_owned(SD_MUTEX(un)));
15738 
15739 #ifdef SD_FAULT_INJECTION
15740 
15741 	SD_INFO(SD_LOG_IOERR, un, "sdintr: sdintr calling Fault injection\n");
15742 	/* SD FaultInjection */
15743 	sd_faultinjection(pktp);
15744 
15745 #endif /* SD_FAULT_INJECTION */
15746 
15747 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: entry: buf:0x%p,"
15748 	    " xp:0x%p, un:0x%p\n", bp, xp, un);
15749 
15750 	mutex_enter(SD_MUTEX(un));
15751 
15752 	/* Reduce the count of the #commands currently in transport */
15753 	un->un_ncmds_in_transport--;
15754 	ASSERT(un->un_ncmds_in_transport >= 0);
15755 
15756 	/* Increment counter to indicate that the callback routine is active */
15757 	un->un_in_callback++;
15758 
15759 	SD_UPDATE_KSTATS(un, kstat_runq_exit, bp);
15760 
15761 #ifdef	SDDEBUG
15762 	if (bp == un->un_retry_bp) {
15763 		SD_TRACE(SD_LOG_IO | SD_LOG_ERROR, un, "sdintr: "
15764 		    "un:0x%p: GOT retry_bp:0x%p un_ncmds_in_transport:%d\n",
15765 		    un, un->un_retry_bp, un->un_ncmds_in_transport);
15766 	}
15767 #endif
15768 
15769 	/*
15770 	 * If pkt_reason is CMD_DEV_GONE, just fail the command
15771 	 */
15772 	if (pktp->pkt_reason == CMD_DEV_GONE) {
15773 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
15774 			    "Device is gone\n");
15775 		sd_return_failed_command(un, bp, EIO);
15776 		goto exit;
15777 	}
15778 
15779 	/*
15780 	 * First see if the pkt has auto-request sense data with it....
15781 	 * Look at the packet state first so we don't take a performance
15782 	 * hit looking at the arq enabled flag unless absolutely necessary.
15783 	 */
15784 	if ((pktp->pkt_state & STATE_ARQ_DONE) &&
15785 	    (un->un_f_arq_enabled == TRUE)) {
15786 		/*
15787 		 * The HBA did an auto request sense for this command so check
15788 		 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
15789 		 * driver command that should not be retried.
15790 		 */
15791 		if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15792 			/*
15793 			 * Save the relevant sense info into the xp for the
15794 			 * original cmd.
15795 			 */
15796 			struct scsi_arq_status *asp;
15797 			asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
15798 			xp->xb_sense_status =
15799 			    *((uchar_t *)(&(asp->sts_rqpkt_status)));
15800 			xp->xb_sense_state  = asp->sts_rqpkt_state;
15801 			xp->xb_sense_resid  = asp->sts_rqpkt_resid;
15802 			bcopy(&asp->sts_sensedata, xp->xb_sense_data,
15803 			    min(sizeof (struct scsi_extended_sense),
15804 			    SENSE_LENGTH));
15805 
15806 			/* fail the command */
15807 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15808 			    "sdintr: arq done and FLAG_DIAGNOSE set\n");
15809 			sd_return_failed_command(un, bp, EIO);
15810 			goto exit;
15811 		}
15812 
15813 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
15814 		/*
15815 		 * We want to either retry or fail this command, so free
15816 		 * the DMA resources here.  If we retry the command then
15817 		 * the DMA resources will be reallocated in sd_start_cmds().
15818 		 * Note that when PKT_DMA_PARTIAL is used, this reallocation
15819 		 * causes the *entire* transfer to start over again from the
15820 		 * beginning of the request, even for PARTIAL chunks that
15821 		 * have already transferred successfully.
15822 		 */
15823 		if ((un->un_f_is_fibre == TRUE) &&
15824 		    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15825 		    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
15826 			scsi_dmafree(pktp);
15827 			xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15828 		}
15829 #endif
15830 
15831 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15832 		    "sdintr: arq done, sd_handle_auto_request_sense\n");
15833 
15834 		sd_handle_auto_request_sense(un, bp, xp, pktp);
15835 		goto exit;
15836 	}
15837 
15838 	/* Next see if this is the REQUEST SENSE pkt for the instance */
15839 	if (pktp->pkt_flags & FLAG_SENSING)  {
15840 		/* This pktp is from the unit's REQUEST_SENSE command */
15841 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15842 		    "sdintr: sd_handle_request_sense\n");
15843 		sd_handle_request_sense(un, bp, xp, pktp);
15844 		goto exit;
15845 	}
15846 
15847 	/*
15848 	 * Check to see if the command successfully completed as requested;
15849 	 * this is the most common case (and also the hot performance path).
15850 	 *
15851 	 * Requirements for successful completion are:
15852 	 * pkt_reason is CMD_CMPLT and packet status is status good.
15853 	 * In addition:
15854 	 * - A residual of zero indicates successful completion no matter what
15855 	 *   the command is.
15856 	 * - If the residual is not zero and the command is not a read or
15857 	 *   write, then it's still defined as successful completion. In other
15858 	 *   words, if the command is a read or write the residual must be
15859 	 *   zero for successful completion.
15860 	 * - If the residual is not zero and the command is a read or
15861 	 *   write, and it's a USCSICMD, then it's still defined as
15862 	 *   successful completion.
15863 	 */
15864 	if ((pktp->pkt_reason == CMD_CMPLT) &&
15865 	    (SD_GET_PKT_STATUS(pktp) == STATUS_GOOD)) {
15866 
15867 		/*
15868 		 * Since this command is returned with a good status, we
15869 		 * can reset the count for Sonoma failover.
15870 		 */
15871 		un->un_sonoma_failure_count = 0;
15872 
15873 		/*
15874 		 * Return all USCSI commands on good status
15875 		 */
15876 		if (pktp->pkt_resid == 0) {
15877 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15878 			    "sdintr: returning command for resid == 0\n");
15879 		} else if (((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_READ) &&
15880 		    ((SD_GET_PKT_OPCODE(pktp) & 0x1F) != SCMD_WRITE)) {
15881 			SD_UPDATE_B_RESID(bp, pktp);
15882 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15883 			    "sdintr: returning command for resid != 0\n");
15884 		} else if (xp->xb_pkt_flags & SD_XB_USCSICMD) {
15885 			SD_UPDATE_B_RESID(bp, pktp);
15886 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15887 				"sdintr: returning uscsi command\n");
15888 		} else {
15889 			goto not_successful;
15890 		}
15891 		sd_return_command(un, bp);
15892 
15893 		/*
15894 		 * Decrement counter to indicate that the callback routine
15895 		 * is done.
15896 		 */
15897 		un->un_in_callback--;
15898 		ASSERT(un->un_in_callback >= 0);
15899 		mutex_exit(SD_MUTEX(un));
15900 
15901 		return;
15902 	}
15903 
15904 not_successful:
15905 
15906 #if (defined(__i386) || defined(__amd64))	/* DMAFREE for x86 only */
15907 	/*
15908 	 * The following is based upon knowledge of the underlying transport
15909 	 * and its use of DMA resources.  This code should be removed when
15910 	 * PKT_DMA_PARTIAL support is taken out of the disk driver in favor
15911 	 * of the new PKT_CMD_BREAKUP protocol. See also sd_initpkt_for_buf()
15912 	 * and sd_start_cmds().
15913 	 *
15914 	 * Free any DMA resources associated with this command if there
15915 	 * is a chance it could be retried or enqueued for later retry.
15916 	 * If we keep the DMA binding then mpxio cannot reissue the
15917 	 * command on another path whenever a path failure occurs.
15918 	 *
15919 	 * Note that when PKT_DMA_PARTIAL is used, free/reallocation
15920 	 * causes the *entire* transfer to start over again from the
15921 	 * beginning of the request, even for PARTIAL chunks that
15922 	 * have already transferred successfully.
15923 	 *
15924 	 * This is only done for non-uscsi commands (and also skipped for the
15925 	 * driver's internal RQS command). Also just do this for Fibre Channel
15926 	 * devices as these are the only ones that support mpxio.
15927 	 */
15928 	if ((un->un_f_is_fibre == TRUE) &&
15929 	    ((xp->xb_pkt_flags & SD_XB_USCSICMD) == 0) &&
15930 	    ((pktp->pkt_flags & FLAG_SENSING) == 0))  {
15931 		scsi_dmafree(pktp);
15932 		xp->xb_pkt_flags |= SD_XB_DMA_FREED;
15933 	}
15934 #endif
15935 
15936 	/*
15937 	 * The command did not successfully complete as requested so check
15938 	 * for FLAG_DIAGNOSE. If set this indicates a uscsi or internal
15939 	 * driver command that should not be retried so just return. If
15940 	 * FLAG_DIAGNOSE is not set the error will be processed below.
15941 	 */
15942 	if ((pktp->pkt_flags & FLAG_DIAGNOSE) != 0) {
15943 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15944 		    "sdintr: FLAG_DIAGNOSE: sd_return_failed_command\n");
15945 		/*
15946 		 * Issue a request sense if a check condition caused the error
15947 		 * (we handle the auto request sense case above), otherwise
15948 		 * just fail the command.
15949 		 */
15950 		if ((pktp->pkt_reason == CMD_CMPLT) &&
15951 		    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK)) {
15952 			sd_send_request_sense_command(un, bp, pktp);
15953 		} else {
15954 			sd_return_failed_command(un, bp, EIO);
15955 		}
15956 		goto exit;
15957 	}
15958 
15959 	/*
15960 	 * The command did not successfully complete as requested so process
15961 	 * the error, retry, and/or attempt recovery.
15962 	 */
15963 	switch (pktp->pkt_reason) {
15964 	case CMD_CMPLT:
15965 		switch (SD_GET_PKT_STATUS(pktp)) {
15966 		case STATUS_GOOD:
15967 			/*
15968 			 * The command completed successfully with a non-zero
15969 			 * residual
15970 			 */
15971 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15972 			    "sdintr: STATUS_GOOD \n");
15973 			sd_pkt_status_good(un, bp, xp, pktp);
15974 			break;
15975 
15976 		case STATUS_CHECK:
15977 		case STATUS_TERMINATED:
15978 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15979 			    "sdintr: STATUS_TERMINATED | STATUS_CHECK\n");
15980 			sd_pkt_status_check_condition(un, bp, xp, pktp);
15981 			break;
15982 
15983 		case STATUS_BUSY:
15984 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15985 			    "sdintr: STATUS_BUSY\n");
15986 			sd_pkt_status_busy(un, bp, xp, pktp);
15987 			break;
15988 
15989 		case STATUS_RESERVATION_CONFLICT:
15990 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15991 			    "sdintr: STATUS_RESERVATION_CONFLICT\n");
15992 			sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
15993 			break;
15994 
15995 		case STATUS_QFULL:
15996 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
15997 			    "sdintr: STATUS_QFULL\n");
15998 			sd_pkt_status_qfull(un, bp, xp, pktp);
15999 			break;
16000 
16001 		case STATUS_MET:
16002 		case STATUS_INTERMEDIATE:
16003 		case STATUS_SCSI2:
16004 		case STATUS_INTERMEDIATE_MET:
16005 		case STATUS_ACA_ACTIVE:
16006 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16007 			    "Unexpected SCSI status received: 0x%x\n",
16008 			    SD_GET_PKT_STATUS(pktp));
16009 			sd_return_failed_command(un, bp, EIO);
16010 			break;
16011 
16012 		default:
16013 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
16014 			    "Invalid SCSI status received: 0x%x\n",
16015 			    SD_GET_PKT_STATUS(pktp));
16016 			sd_return_failed_command(un, bp, EIO);
16017 			break;
16018 
16019 		}
16020 		break;
16021 
16022 	case CMD_INCOMPLETE:
16023 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16024 		    "sdintr:  CMD_INCOMPLETE\n");
16025 		sd_pkt_reason_cmd_incomplete(un, bp, xp, pktp);
16026 		break;
16027 	case CMD_TRAN_ERR:
16028 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16029 		    "sdintr: CMD_TRAN_ERR\n");
16030 		sd_pkt_reason_cmd_tran_err(un, bp, xp, pktp);
16031 		break;
16032 	case CMD_RESET:
16033 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16034 		    "sdintr: CMD_RESET \n");
16035 		sd_pkt_reason_cmd_reset(un, bp, xp, pktp);
16036 		break;
16037 	case CMD_ABORTED:
16038 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16039 		    "sdintr: CMD_ABORTED \n");
16040 		sd_pkt_reason_cmd_aborted(un, bp, xp, pktp);
16041 		break;
16042 	case CMD_TIMEOUT:
16043 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16044 		    "sdintr: CMD_TIMEOUT\n");
16045 		sd_pkt_reason_cmd_timeout(un, bp, xp, pktp);
16046 		break;
16047 	case CMD_UNX_BUS_FREE:
16048 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16049 		    "sdintr: CMD_UNX_BUS_FREE \n");
16050 		sd_pkt_reason_cmd_unx_bus_free(un, bp, xp, pktp);
16051 		break;
16052 	case CMD_TAG_REJECT:
16053 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16054 		    "sdintr: CMD_TAG_REJECT\n");
16055 		sd_pkt_reason_cmd_tag_reject(un, bp, xp, pktp);
16056 		break;
16057 	default:
16058 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
16059 		    "sdintr: default\n");
16060 		sd_pkt_reason_default(un, bp, xp, pktp);
16061 		break;
16062 	}
16063 
16064 exit:
16065 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sdintr: exit\n");
16066 
16067 	/* Decrement counter to indicate that the callback routine is done. */
16068 	un->un_in_callback--;
16069 	ASSERT(un->un_in_callback >= 0);
16070 
16071 	/*
16072 	 * At this point, the pkt has been dispatched, ie, it is either
16073 	 * being re-tried or has been returned to its caller and should
16074 	 * not be referenced.
16075 	 */
16076 
16077 	mutex_exit(SD_MUTEX(un));
16078 }
16079 
16080 
16081 /*
16082  *    Function: sd_print_incomplete_msg
16083  *
16084  * Description: Prints the error message for a CMD_INCOMPLETE error.
16085  *
16086  *   Arguments: un - ptr to associated softstate for the device.
16087  *		bp - ptr to the buf(9S) for the command.
16088  *		arg - message string ptr
16089  *		code - SD_DELAYED_RETRY_ISSUED, SD_IMMEDIATE_RETRY_ISSUED,
16090  *			or SD_NO_RETRY_ISSUED.
16091  *
16092  *     Context: May be called under interrupt context
16093  */
16094 
16095 static void
16096 sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
16097 {
16098 	struct scsi_pkt	*pktp;
16099 	char	*msgp;
16100 	char	*cmdp = arg;
16101 
16102 	ASSERT(un != NULL);
16103 	ASSERT(mutex_owned(SD_MUTEX(un)));
16104 	ASSERT(bp != NULL);
16105 	ASSERT(arg != NULL);
16106 	pktp = SD_GET_PKTP(bp);
16107 	ASSERT(pktp != NULL);
16108 
16109 	switch (code) {
16110 	case SD_DELAYED_RETRY_ISSUED:
16111 	case SD_IMMEDIATE_RETRY_ISSUED:
16112 		msgp = "retrying";
16113 		break;
16114 	case SD_NO_RETRY_ISSUED:
16115 	default:
16116 		msgp = "giving up";
16117 		break;
16118 	}
16119 
16120 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
16121 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16122 		    "incomplete %s- %s\n", cmdp, msgp);
16123 	}
16124 }
16125 
16126 
16127 
16128 /*
16129  *    Function: sd_pkt_status_good
16130  *
16131  * Description: Processing for a STATUS_GOOD code in pkt_status.
16132  *
16133  *     Context: May be called under interrupt context
16134  */
16135 
16136 static void
16137 sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
16138 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16139 {
16140 	char	*cmdp;
16141 
16142 	ASSERT(un != NULL);
16143 	ASSERT(mutex_owned(SD_MUTEX(un)));
16144 	ASSERT(bp != NULL);
16145 	ASSERT(xp != NULL);
16146 	ASSERT(pktp != NULL);
16147 	ASSERT(pktp->pkt_reason == CMD_CMPLT);
16148 	ASSERT(SD_GET_PKT_STATUS(pktp) == STATUS_GOOD);
16149 	ASSERT(pktp->pkt_resid != 0);
16150 
16151 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: entry\n");
16152 
16153 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
16154 	switch (SD_GET_PKT_OPCODE(pktp) & 0x1F) {
16155 	case SCMD_READ:
16156 		cmdp = "read";
16157 		break;
16158 	case SCMD_WRITE:
16159 		cmdp = "write";
16160 		break;
16161 	default:
16162 		SD_UPDATE_B_RESID(bp, pktp);
16163 		sd_return_command(un, bp);
16164 		SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16165 		return;
16166 	}
16167 
16168 	/*
16169 	 * See if we can retry the read/write, preferrably immediately.
16170 	 * If retries are exhaused, then sd_retry_command() will update
16171 	 * the b_resid count.
16172 	 */
16173 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_incomplete_msg,
16174 	    cmdp, EIO, (clock_t)0, NULL);
16175 
16176 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_good: exit\n");
16177 }
16178 
16179 
16180 
16181 
16182 
16183 /*
16184  *    Function: sd_handle_request_sense
16185  *
16186  * Description: Processing for non-auto Request Sense command.
16187  *
16188  *   Arguments: un - ptr to associated softstate
16189  *		sense_bp - ptr to buf(9S) for the RQS command
16190  *		sense_xp - ptr to the sd_xbuf for the RQS command
16191  *		sense_pktp - ptr to the scsi_pkt(9S) for the RQS command
16192  *
16193  *     Context: May be called under interrupt context
16194  */
16195 
16196 static void
16197 sd_handle_request_sense(struct sd_lun *un, struct buf *sense_bp,
16198 	struct sd_xbuf *sense_xp, struct scsi_pkt *sense_pktp)
16199 {
16200 	struct buf	*cmd_bp;	/* buf for the original command */
16201 	struct sd_xbuf	*cmd_xp;	/* sd_xbuf for the original command */
16202 	struct scsi_pkt *cmd_pktp;	/* pkt for the original command */
16203 
16204 	ASSERT(un != NULL);
16205 	ASSERT(mutex_owned(SD_MUTEX(un)));
16206 	ASSERT(sense_bp != NULL);
16207 	ASSERT(sense_xp != NULL);
16208 	ASSERT(sense_pktp != NULL);
16209 
16210 	/*
16211 	 * Note the sense_bp, sense_xp, and sense_pktp here are for the
16212 	 * RQS command and not the original command.
16213 	 */
16214 	ASSERT(sense_pktp == un->un_rqs_pktp);
16215 	ASSERT(sense_bp   == un->un_rqs_bp);
16216 	ASSERT((sense_pktp->pkt_flags & (FLAG_SENSING | FLAG_HEAD)) ==
16217 	    (FLAG_SENSING | FLAG_HEAD));
16218 	ASSERT((((SD_GET_XBUF(sense_xp->xb_sense_bp))->xb_pktp->pkt_flags) &
16219 	    FLAG_SENSING) == FLAG_SENSING);
16220 
16221 	/* These are the bp, xp, and pktp for the original command */
16222 	cmd_bp = sense_xp->xb_sense_bp;
16223 	cmd_xp = SD_GET_XBUF(cmd_bp);
16224 	cmd_pktp = SD_GET_PKTP(cmd_bp);
16225 
16226 	if (sense_pktp->pkt_reason != CMD_CMPLT) {
16227 		/*
16228 		 * The REQUEST SENSE command failed.  Release the REQUEST
16229 		 * SENSE command for re-use, get back the bp for the original
16230 		 * command, and attempt to re-try the original command if
16231 		 * FLAG_DIAGNOSE is not set in the original packet.
16232 		 */
16233 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
16234 		if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16235 			cmd_bp = sd_mark_rqs_idle(un, sense_xp);
16236 			sd_retry_command(un, cmd_bp, SD_RETRIES_STANDARD,
16237 			    NULL, NULL, EIO, (clock_t)0, NULL);
16238 			return;
16239 		}
16240 	}
16241 
16242 	/*
16243 	 * Save the relevant sense info into the xp for the original cmd.
16244 	 *
16245 	 * Note: if the request sense failed the state info will be zero
16246 	 * as set in sd_mark_rqs_busy()
16247 	 */
16248 	cmd_xp->xb_sense_status = *(sense_pktp->pkt_scbp);
16249 	cmd_xp->xb_sense_state  = sense_pktp->pkt_state;
16250 	cmd_xp->xb_sense_resid  = sense_pktp->pkt_resid;
16251 	bcopy(sense_bp->b_un.b_addr, cmd_xp->xb_sense_data, SENSE_LENGTH);
16252 
16253 	/*
16254 	 *  Free up the RQS command....
16255 	 *  NOTE:
16256 	 *	Must do this BEFORE calling sd_validate_sense_data!
16257 	 *	sd_validate_sense_data may return the original command in
16258 	 *	which case the pkt will be freed and the flags can no
16259 	 *	longer be touched.
16260 	 *	SD_MUTEX is held through this process until the command
16261 	 *	is dispatched based upon the sense data, so there are
16262 	 *	no race conditions.
16263 	 */
16264 	(void) sd_mark_rqs_idle(un, sense_xp);
16265 
16266 	/*
16267 	 * For a retryable command see if we have valid sense data, if so then
16268 	 * turn it over to sd_decode_sense() to figure out the right course of
16269 	 * action. Just fail a non-retryable command.
16270 	 */
16271 	if ((cmd_pktp->pkt_flags & FLAG_DIAGNOSE) == 0) {
16272 		if (sd_validate_sense_data(un, cmd_bp, cmd_xp) ==
16273 		    SD_SENSE_DATA_IS_VALID) {
16274 			sd_decode_sense(un, cmd_bp, cmd_xp, cmd_pktp);
16275 		}
16276 	} else {
16277 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Failed CDB",
16278 		    (uchar_t *)cmd_pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
16279 		SD_DUMP_MEMORY(un, SD_LOG_IO_CORE, "Sense Data",
16280 		    (uchar_t *)cmd_xp->xb_sense_data, SENSE_LENGTH, SD_LOG_HEX);
16281 		sd_return_failed_command(un, cmd_bp, EIO);
16282 	}
16283 }
16284 
16285 
16286 
16287 
16288 /*
16289  *    Function: sd_handle_auto_request_sense
16290  *
16291  * Description: Processing for auto-request sense information.
16292  *
16293  *   Arguments: un - ptr to associated softstate
16294  *		bp - ptr to buf(9S) for the command
16295  *		xp - ptr to the sd_xbuf for the command
16296  *		pktp - ptr to the scsi_pkt(9S) for the command
16297  *
16298  *     Context: May be called under interrupt context
16299  */
16300 
16301 static void
16302 sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
16303 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16304 {
16305 	struct scsi_arq_status *asp;
16306 
16307 	ASSERT(un != NULL);
16308 	ASSERT(mutex_owned(SD_MUTEX(un)));
16309 	ASSERT(bp != NULL);
16310 	ASSERT(xp != NULL);
16311 	ASSERT(pktp != NULL);
16312 	ASSERT(pktp != un->un_rqs_pktp);
16313 	ASSERT(bp   != un->un_rqs_bp);
16314 
16315 	/*
16316 	 * For auto-request sense, we get a scsi_arq_status back from
16317 	 * the HBA, with the sense data in the sts_sensedata member.
16318 	 * The pkt_scbp of the packet points to this scsi_arq_status.
16319 	 */
16320 	asp = (struct scsi_arq_status *)(pktp->pkt_scbp);
16321 
16322 	if (asp->sts_rqpkt_reason != CMD_CMPLT) {
16323 		/*
16324 		 * The auto REQUEST SENSE failed; see if we can re-try
16325 		 * the original command.
16326 		 */
16327 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16328 		    "auto request sense failed (reason=%s)\n",
16329 		    scsi_rname(asp->sts_rqpkt_reason));
16330 
16331 		sd_reset_target(un, pktp);
16332 
16333 		sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16334 		    NULL, NULL, EIO, (clock_t)0, NULL);
16335 		return;
16336 	}
16337 
16338 	/* Save the relevant sense info into the xp for the original cmd. */
16339 	xp->xb_sense_status = *((uchar_t *)(&(asp->sts_rqpkt_status)));
16340 	xp->xb_sense_state  = asp->sts_rqpkt_state;
16341 	xp->xb_sense_resid  = asp->sts_rqpkt_resid;
16342 	bcopy(&asp->sts_sensedata, xp->xb_sense_data,
16343 	    min(sizeof (struct scsi_extended_sense), SENSE_LENGTH));
16344 
16345 	/*
16346 	 * See if we have valid sense data, if so then turn it over to
16347 	 * sd_decode_sense() to figure out the right course of action.
16348 	 */
16349 	if (sd_validate_sense_data(un, bp, xp) == SD_SENSE_DATA_IS_VALID) {
16350 		sd_decode_sense(un, bp, xp, pktp);
16351 	}
16352 }
16353 
16354 
16355 /*
16356  *    Function: sd_print_sense_failed_msg
16357  *
16358  * Description: Print log message when RQS has failed.
16359  *
16360  *   Arguments: un - ptr to associated softstate
16361  *		bp - ptr to buf(9S) for the command
16362  *		arg - generic message string ptr
16363  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
16364  *			or SD_NO_RETRY_ISSUED
16365  *
16366  *     Context: May be called from interrupt context
16367  */
16368 
16369 static void
16370 sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp, void *arg,
16371 	int code)
16372 {
16373 	char	*msgp = arg;
16374 
16375 	ASSERT(un != NULL);
16376 	ASSERT(mutex_owned(SD_MUTEX(un)));
16377 	ASSERT(bp != NULL);
16378 
16379 	if ((code == SD_NO_RETRY_ISSUED) && (msgp != NULL)) {
16380 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, msgp);
16381 	}
16382 }
16383 
16384 
16385 /*
16386  *    Function: sd_validate_sense_data
16387  *
16388  * Description: Check the given sense data for validity.
16389  *		If the sense data is not valid, the command will
16390  *		be either failed or retried!
16391  *
16392  * Return Code: SD_SENSE_DATA_IS_INVALID
16393  *		SD_SENSE_DATA_IS_VALID
16394  *
16395  *     Context: May be called from interrupt context
16396  */
16397 
16398 static int
16399 sd_validate_sense_data(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp)
16400 {
16401 	struct scsi_extended_sense *esp;
16402 	struct	scsi_pkt *pktp;
16403 	size_t	actual_len;
16404 	char	*msgp = NULL;
16405 
16406 	ASSERT(un != NULL);
16407 	ASSERT(mutex_owned(SD_MUTEX(un)));
16408 	ASSERT(bp != NULL);
16409 	ASSERT(bp != un->un_rqs_bp);
16410 	ASSERT(xp != NULL);
16411 
16412 	pktp = SD_GET_PKTP(bp);
16413 	ASSERT(pktp != NULL);
16414 
16415 	/*
16416 	 * Check the status of the RQS command (auto or manual).
16417 	 */
16418 	switch (xp->xb_sense_status & STATUS_MASK) {
16419 	case STATUS_GOOD:
16420 		break;
16421 
16422 	case STATUS_RESERVATION_CONFLICT:
16423 		sd_pkt_status_reservation_conflict(un, bp, xp, pktp);
16424 		return (SD_SENSE_DATA_IS_INVALID);
16425 
16426 	case STATUS_BUSY:
16427 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16428 		    "Busy Status on REQUEST SENSE\n");
16429 		sd_retry_command(un, bp, SD_RETRIES_BUSY, NULL,
16430 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
16431 		return (SD_SENSE_DATA_IS_INVALID);
16432 
16433 	case STATUS_QFULL:
16434 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
16435 		    "QFULL Status on REQUEST SENSE\n");
16436 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL,
16437 		    NULL, EIO, SD_BSY_TIMEOUT / 500, kstat_waitq_enter);
16438 		return (SD_SENSE_DATA_IS_INVALID);
16439 
16440 	case STATUS_CHECK:
16441 	case STATUS_TERMINATED:
16442 		msgp = "Check Condition on REQUEST SENSE\n";
16443 		goto sense_failed;
16444 
16445 	default:
16446 		msgp = "Not STATUS_GOOD on REQUEST_SENSE\n";
16447 		goto sense_failed;
16448 	}
16449 
16450 	/*
16451 	 * See if we got the minimum required amount of sense data.
16452 	 * Note: We are assuming the returned sense data is SENSE_LENGTH bytes
16453 	 * or less.
16454 	 */
16455 	actual_len = (int)(SENSE_LENGTH - xp->xb_sense_resid);
16456 	if (((xp->xb_sense_state & STATE_XFERRED_DATA) == 0) ||
16457 	    (actual_len == 0)) {
16458 		msgp = "Request Sense couldn't get sense data\n";
16459 		goto sense_failed;
16460 	}
16461 
16462 	if (actual_len < SUN_MIN_SENSE_LENGTH) {
16463 		msgp = "Not enough sense information\n";
16464 		goto sense_failed;
16465 	}
16466 
16467 	/*
16468 	 * We require the extended sense data
16469 	 */
16470 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
16471 	if (esp->es_class != CLASS_EXTENDED_SENSE) {
16472 		if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
16473 			static char tmp[8];
16474 			static char buf[148];
16475 			char *p = (char *)(xp->xb_sense_data);
16476 			int i;
16477 
16478 			mutex_enter(&sd_sense_mutex);
16479 			(void) strcpy(buf, "undecodable sense information:");
16480 			for (i = 0; i < actual_len; i++) {
16481 				(void) sprintf(tmp, " 0x%x", *(p++)&0xff);
16482 				(void) strcpy(&buf[strlen(buf)], tmp);
16483 			}
16484 			i = strlen(buf);
16485 			(void) strcpy(&buf[i], "-(assumed fatal)\n");
16486 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, buf);
16487 			mutex_exit(&sd_sense_mutex);
16488 		}
16489 		/* Note: Legacy behavior, fail the command with no retry */
16490 		sd_return_failed_command(un, bp, EIO);
16491 		return (SD_SENSE_DATA_IS_INVALID);
16492 	}
16493 
16494 	/*
16495 	 * Check that es_code is valid (es_class concatenated with es_code
16496 	 * make up the "response code" field.  es_class will always be 7, so
16497 	 * make sure es_code is 0, 1, 2, 3 or 0xf.  es_code will indicate the
16498 	 * format.
16499 	 */
16500 	if ((esp->es_code != CODE_FMT_FIXED_CURRENT) &&
16501 	    (esp->es_code != CODE_FMT_FIXED_DEFERRED) &&
16502 	    (esp->es_code != CODE_FMT_DESCR_CURRENT) &&
16503 	    (esp->es_code != CODE_FMT_DESCR_DEFERRED) &&
16504 	    (esp->es_code != CODE_FMT_VENDOR_SPECIFIC)) {
16505 		goto sense_failed;
16506 	}
16507 
16508 	return (SD_SENSE_DATA_IS_VALID);
16509 
16510 sense_failed:
16511 	/*
16512 	 * If the request sense failed (for whatever reason), attempt
16513 	 * to retry the original command.
16514 	 */
16515 #if defined(__i386) || defined(__amd64)
16516 	/*
16517 	 * SD_RETRY_DELAY is conditionally compile (#if fibre) in
16518 	 * sddef.h for Sparc platform, and x86 uses 1 binary
16519 	 * for both SCSI/FC.
16520 	 * The SD_RETRY_DELAY value need to be adjusted here
16521 	 * when SD_RETRY_DELAY change in sddef.h
16522 	 */
16523 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16524 	    sd_print_sense_failed_msg, msgp, EIO,
16525 		un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0, NULL);
16526 #else
16527 	sd_retry_command(un, bp, SD_RETRIES_STANDARD,
16528 	    sd_print_sense_failed_msg, msgp, EIO, SD_RETRY_DELAY, NULL);
16529 #endif
16530 
16531 	return (SD_SENSE_DATA_IS_INVALID);
16532 }
16533 
16534 
16535 
16536 /*
16537  *    Function: sd_decode_sense
16538  *
16539  * Description: Take recovery action(s) when SCSI Sense Data is received.
16540  *
16541  *     Context: Interrupt context.
16542  */
16543 
16544 static void
16545 sd_decode_sense(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
16546 	struct scsi_pkt *pktp)
16547 {
16548 	struct scsi_extended_sense *esp;
16549 	struct scsi_descr_sense_hdr *sdsp;
16550 	uint8_t asc, ascq, sense_key;
16551 
16552 	ASSERT(un != NULL);
16553 	ASSERT(mutex_owned(SD_MUTEX(un)));
16554 	ASSERT(bp != NULL);
16555 	ASSERT(bp != un->un_rqs_bp);
16556 	ASSERT(xp != NULL);
16557 	ASSERT(pktp != NULL);
16558 
16559 	esp = (struct scsi_extended_sense *)xp->xb_sense_data;
16560 
16561 	switch (esp->es_code) {
16562 	case CODE_FMT_DESCR_CURRENT:
16563 	case CODE_FMT_DESCR_DEFERRED:
16564 		sdsp = (struct scsi_descr_sense_hdr *)xp->xb_sense_data;
16565 		sense_key = sdsp->ds_key;
16566 		asc = sdsp->ds_add_code;
16567 		ascq = sdsp->ds_qual_code;
16568 		break;
16569 	case CODE_FMT_VENDOR_SPECIFIC:
16570 	case CODE_FMT_FIXED_CURRENT:
16571 	case CODE_FMT_FIXED_DEFERRED:
16572 	default:
16573 		sense_key = esp->es_key;
16574 		asc = esp->es_add_code;
16575 		ascq = esp->es_qual_code;
16576 		break;
16577 	}
16578 
16579 	switch (sense_key) {
16580 	case KEY_NO_SENSE:
16581 		sd_sense_key_no_sense(un, bp, xp, pktp);
16582 		break;
16583 	case KEY_RECOVERABLE_ERROR:
16584 		sd_sense_key_recoverable_error(un, asc, bp, xp, pktp);
16585 		break;
16586 	case KEY_NOT_READY:
16587 		sd_sense_key_not_ready(un, asc, ascq, bp, xp, pktp);
16588 		break;
16589 	case KEY_MEDIUM_ERROR:
16590 	case KEY_HARDWARE_ERROR:
16591 		sd_sense_key_medium_or_hardware_error(un,
16592 		    sense_key, asc, bp, xp, pktp);
16593 		break;
16594 	case KEY_ILLEGAL_REQUEST:
16595 		sd_sense_key_illegal_request(un, bp, xp, pktp);
16596 		break;
16597 	case KEY_UNIT_ATTENTION:
16598 		sd_sense_key_unit_attention(un, asc, bp, xp, pktp);
16599 		break;
16600 	case KEY_WRITE_PROTECT:
16601 	case KEY_VOLUME_OVERFLOW:
16602 	case KEY_MISCOMPARE:
16603 		sd_sense_key_fail_command(un, bp, xp, pktp);
16604 		break;
16605 	case KEY_BLANK_CHECK:
16606 		sd_sense_key_blank_check(un, bp, xp, pktp);
16607 		break;
16608 	case KEY_ABORTED_COMMAND:
16609 		sd_sense_key_aborted_command(un, bp, xp, pktp);
16610 		break;
16611 	case KEY_VENDOR_UNIQUE:
16612 	case KEY_COPY_ABORTED:
16613 	case KEY_EQUAL:
16614 	case KEY_RESERVED:
16615 	default:
16616 		sd_sense_key_default(un, sense_key, bp, xp, pktp);
16617 		break;
16618 	}
16619 }
16620 
16621 
16622 /*
16623  *    Function: sd_dump_memory
16624  *
16625  * Description: Debug logging routine to print the contents of a user provided
16626  *		buffer. The output of the buffer is broken up into 256 byte
16627  *		segments due to a size constraint of the scsi_log.
16628  *		implementation.
16629  *
16630  *   Arguments: un - ptr to softstate
16631  *		comp - component mask
16632  *		title - "title" string to preceed data when printed
16633  *		data - ptr to data block to be printed
16634  *		len - size of data block to be printed
16635  *		fmt - SD_LOG_HEX (use 0x%02x format) or SD_LOG_CHAR (use %c)
16636  *
16637  *     Context: May be called from interrupt context
16638  */
16639 
16640 #define	SD_DUMP_MEMORY_BUF_SIZE	256
16641 
16642 static char *sd_dump_format_string[] = {
16643 		" 0x%02x",
16644 		" %c"
16645 };
16646 
16647 static void
16648 sd_dump_memory(struct sd_lun *un, uint_t comp, char *title, uchar_t *data,
16649     int len, int fmt)
16650 {
16651 	int	i, j;
16652 	int	avail_count;
16653 	int	start_offset;
16654 	int	end_offset;
16655 	size_t	entry_len;
16656 	char	*bufp;
16657 	char	*local_buf;
16658 	char	*format_string;
16659 
16660 	ASSERT((fmt == SD_LOG_HEX) || (fmt == SD_LOG_CHAR));
16661 
16662 	/*
16663 	 * In the debug version of the driver, this function is called from a
16664 	 * number of places which are NOPs in the release driver.
16665 	 * The debug driver therefore has additional methods of filtering
16666 	 * debug output.
16667 	 */
16668 #ifdef SDDEBUG
16669 	/*
16670 	 * In the debug version of the driver we can reduce the amount of debug
16671 	 * messages by setting sd_error_level to something other than
16672 	 * SCSI_ERR_ALL and clearing bits in sd_level_mask and
16673 	 * sd_component_mask.
16674 	 */
16675 	if (((sd_level_mask & (SD_LOGMASK_DUMP_MEM | SD_LOGMASK_DIAG)) == 0) ||
16676 	    (sd_error_level != SCSI_ERR_ALL)) {
16677 		return;
16678 	}
16679 	if (((sd_component_mask & comp) == 0) ||
16680 	    (sd_error_level != SCSI_ERR_ALL)) {
16681 		return;
16682 	}
16683 #else
16684 	if (sd_error_level != SCSI_ERR_ALL) {
16685 		return;
16686 	}
16687 #endif
16688 
16689 	local_buf = kmem_zalloc(SD_DUMP_MEMORY_BUF_SIZE, KM_SLEEP);
16690 	bufp = local_buf;
16691 	/*
16692 	 * Available length is the length of local_buf[], minus the
16693 	 * length of the title string, minus one for the ":", minus
16694 	 * one for the newline, minus one for the NULL terminator.
16695 	 * This gives the #bytes available for holding the printed
16696 	 * values from the given data buffer.
16697 	 */
16698 	if (fmt == SD_LOG_HEX) {
16699 		format_string = sd_dump_format_string[0];
16700 	} else /* SD_LOG_CHAR */ {
16701 		format_string = sd_dump_format_string[1];
16702 	}
16703 	/*
16704 	 * Available count is the number of elements from the given
16705 	 * data buffer that we can fit into the available length.
16706 	 * This is based upon the size of the format string used.
16707 	 * Make one entry and find it's size.
16708 	 */
16709 	(void) sprintf(bufp, format_string, data[0]);
16710 	entry_len = strlen(bufp);
16711 	avail_count = (SD_DUMP_MEMORY_BUF_SIZE - strlen(title) - 3) / entry_len;
16712 
16713 	j = 0;
16714 	while (j < len) {
16715 		bufp = local_buf;
16716 		bzero(bufp, SD_DUMP_MEMORY_BUF_SIZE);
16717 		start_offset = j;
16718 
16719 		end_offset = start_offset + avail_count;
16720 
16721 		(void) sprintf(bufp, "%s:", title);
16722 		bufp += strlen(bufp);
16723 		for (i = start_offset; ((i < end_offset) && (j < len));
16724 		    i++, j++) {
16725 			(void) sprintf(bufp, format_string, data[i]);
16726 			bufp += entry_len;
16727 		}
16728 		(void) sprintf(bufp, "\n");
16729 
16730 		scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE, "%s", local_buf);
16731 	}
16732 	kmem_free(local_buf, SD_DUMP_MEMORY_BUF_SIZE);
16733 }
16734 
16735 /*
16736  *    Function: sd_print_sense_msg
16737  *
16738  * Description: Log a message based upon the given sense data.
16739  *
16740  *   Arguments: un - ptr to associated softstate
16741  *		bp - ptr to buf(9S) for the command
16742  *		arg - ptr to associate sd_sense_info struct
16743  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
16744  *			or SD_NO_RETRY_ISSUED
16745  *
16746  *     Context: May be called from interrupt context
16747  */
16748 
16749 static void
16750 sd_print_sense_msg(struct sd_lun *un, struct buf *bp, void *arg, int code)
16751 {
16752 	struct sd_xbuf	*xp;
16753 	struct scsi_pkt	*pktp;
16754 	struct scsi_extended_sense *sensep;
16755 	daddr_t request_blkno;
16756 	diskaddr_t err_blkno;
16757 	int severity;
16758 	int pfa_flag;
16759 	int fixed_format = TRUE;
16760 	extern struct scsi_key_strings scsi_cmds[];
16761 
16762 	ASSERT(un != NULL);
16763 	ASSERT(mutex_owned(SD_MUTEX(un)));
16764 	ASSERT(bp != NULL);
16765 	xp = SD_GET_XBUF(bp);
16766 	ASSERT(xp != NULL);
16767 	pktp = SD_GET_PKTP(bp);
16768 	ASSERT(pktp != NULL);
16769 	ASSERT(arg != NULL);
16770 
16771 	severity = ((struct sd_sense_info *)(arg))->ssi_severity;
16772 	pfa_flag = ((struct sd_sense_info *)(arg))->ssi_pfa_flag;
16773 
16774 	if ((code == SD_DELAYED_RETRY_ISSUED) ||
16775 	    (code == SD_IMMEDIATE_RETRY_ISSUED)) {
16776 		severity = SCSI_ERR_RETRYABLE;
16777 	}
16778 
16779 	/* Use absolute block number for the request block number */
16780 	request_blkno = xp->xb_blkno;
16781 
16782 	/*
16783 	 * Now try to get the error block number from the sense data
16784 	 */
16785 	sensep = (struct scsi_extended_sense *)xp->xb_sense_data;
16786 	switch (sensep->es_code) {
16787 	case CODE_FMT_DESCR_CURRENT:
16788 	case CODE_FMT_DESCR_DEFERRED:
16789 		err_blkno =
16790 		    sd_extract_sense_info_descr(
16791 			(struct scsi_descr_sense_hdr *)sensep);
16792 		fixed_format = FALSE;
16793 		break;
16794 	case CODE_FMT_FIXED_CURRENT:
16795 	case CODE_FMT_FIXED_DEFERRED:
16796 	case CODE_FMT_VENDOR_SPECIFIC:
16797 	default:
16798 		/*
16799 		 * With the es_valid bit set, we assume that the error
16800 		 * blkno is in the sense data.  Also, if xp->xb_blkno is
16801 		 * greater than 0xffffffff then the target *should* have used
16802 		 * a descriptor sense format (or it shouldn't have set
16803 		 * the es_valid bit), and we may as well ignore the
16804 		 * 32-bit value.
16805 		 */
16806 		if ((sensep->es_valid != 0) && (xp->xb_blkno <= 0xffffffff)) {
16807 			err_blkno = (diskaddr_t)
16808 			    ((sensep->es_info_1 << 24) |
16809 			    (sensep->es_info_2 << 16) |
16810 			    (sensep->es_info_3 << 8)  |
16811 			    (sensep->es_info_4));
16812 		} else {
16813 			err_blkno = (diskaddr_t)-1;
16814 		}
16815 		break;
16816 	}
16817 
16818 	if (err_blkno == (diskaddr_t)-1) {
16819 		/*
16820 		 * Without the es_valid bit set (for fixed format) or an
16821 		 * information descriptor (for descriptor format) we cannot
16822 		 * be certain of the error blkno, so just use the
16823 		 * request_blkno.
16824 		 */
16825 		err_blkno = (diskaddr_t)request_blkno;
16826 	} else {
16827 		/*
16828 		 * We retrieved the error block number from the information
16829 		 * portion of the sense data.
16830 		 *
16831 		 * For USCSI commands we are better off using the error
16832 		 * block no. as the requested block no. (This is the best
16833 		 * we can estimate.)
16834 		 */
16835 		if ((SD_IS_BUFIO(xp) == FALSE) &&
16836 		    ((pktp->pkt_flags & FLAG_SILENT) == 0)) {
16837 			request_blkno = err_blkno;
16838 		}
16839 	}
16840 
16841 	/*
16842 	 * The following will log the buffer contents for the release driver
16843 	 * if the SD_LOGMASK_DIAG bit of sd_level_mask is set, or the error
16844 	 * level is set to verbose.
16845 	 */
16846 	sd_dump_memory(un, SD_LOG_IO, "Failed CDB",
16847 	    (uchar_t *)pktp->pkt_cdbp, CDB_SIZE, SD_LOG_HEX);
16848 	sd_dump_memory(un, SD_LOG_IO, "Sense Data",
16849 	    (uchar_t *)sensep, SENSE_LENGTH, SD_LOG_HEX);
16850 
16851 	if (pfa_flag == FALSE) {
16852 		/* This is normally only set for USCSI */
16853 		if ((pktp->pkt_flags & FLAG_SILENT) != 0) {
16854 			return;
16855 		}
16856 
16857 		if ((SD_IS_BUFIO(xp) == TRUE) &&
16858 		    (((sd_level_mask & SD_LOGMASK_DIAG) == 0) &&
16859 		    (severity < sd_error_level))) {
16860 			return;
16861 		}
16862 	}
16863 
16864 	/*
16865 	 * If the data is fixed format then check for Sonoma Failover,
16866 	 * and keep a count of how many failed I/O's.  We should not have
16867 	 * to worry about Sonoma returning descriptor format sense data,
16868 	 * and asc/ascq are in a different location in descriptor format.
16869 	 */
16870 	if (fixed_format &&
16871 	    (SD_IS_LSI(un)) && (sensep->es_key == KEY_ILLEGAL_REQUEST) &&
16872 	    (sensep->es_add_code == 0x94) && (sensep->es_qual_code == 0x01)) {
16873 		un->un_sonoma_failure_count++;
16874 		if (un->un_sonoma_failure_count > 1) {
16875 			return;
16876 		}
16877 	}
16878 
16879 	scsi_vu_errmsg(SD_SCSI_DEVP(un), pktp, sd_label, severity,
16880 	    request_blkno, err_blkno, scsi_cmds, sensep,
16881 	    un->un_additional_codes, NULL);
16882 }
16883 
16884 /*
16885  *    Function: sd_extract_sense_info_descr
16886  *
16887  * Description: Retrieve "information" field from descriptor format
16888  *              sense data.  Iterates through each sense descriptor
16889  *              looking for the information descriptor and returns
16890  *              the information field from that descriptor.
16891  *
16892  *     Context: May be called from interrupt context
16893  */
16894 
16895 static diskaddr_t
16896 sd_extract_sense_info_descr(struct scsi_descr_sense_hdr *sdsp)
16897 {
16898 	diskaddr_t result;
16899 	uint8_t *descr_offset;
16900 	int valid_sense_length;
16901 	struct scsi_information_sense_descr *isd;
16902 
16903 	/*
16904 	 * Initialize result to -1 indicating there is no information
16905 	 * descriptor
16906 	 */
16907 	result = (diskaddr_t)-1;
16908 
16909 	/*
16910 	 * The first descriptor will immediately follow the header
16911 	 */
16912 	descr_offset = (uint8_t *)(sdsp+1); /* Pointer arithmetic */
16913 
16914 	/*
16915 	 * Calculate the amount of valid sense data
16916 	 */
16917 	valid_sense_length =
16918 	    min((sizeof (struct scsi_descr_sense_hdr) +
16919 	    sdsp->ds_addl_sense_length),
16920 	    SENSE_LENGTH);
16921 
16922 	/*
16923 	 * Iterate through the list of descriptors, stopping when we
16924 	 * run out of sense data
16925 	 */
16926 	while ((descr_offset + sizeof (struct scsi_information_sense_descr)) <=
16927 	    (uint8_t *)sdsp + valid_sense_length) {
16928 		/*
16929 		 * Check if this is an information descriptor.  We can
16930 		 * use the scsi_information_sense_descr structure as a
16931 		 * template sense the first two fields are always the
16932 		 * same
16933 		 */
16934 		isd = (struct scsi_information_sense_descr *)descr_offset;
16935 		if (isd->isd_descr_type == DESCR_INFORMATION) {
16936 			/*
16937 			 * Found an information descriptor.  Copy the
16938 			 * information field.  There will only be one
16939 			 * information descriptor so we can stop looking.
16940 			 */
16941 			result =
16942 			    (((diskaddr_t)isd->isd_information[0] << 56) |
16943 				((diskaddr_t)isd->isd_information[1] << 48) |
16944 				((diskaddr_t)isd->isd_information[2] << 40) |
16945 				((diskaddr_t)isd->isd_information[3] << 32) |
16946 				((diskaddr_t)isd->isd_information[4] << 24) |
16947 				((diskaddr_t)isd->isd_information[5] << 16) |
16948 				((diskaddr_t)isd->isd_information[6] << 8)  |
16949 				((diskaddr_t)isd->isd_information[7]));
16950 			break;
16951 		}
16952 
16953 		/*
16954 		 * Get pointer to the next descriptor.  The "additional
16955 		 * length" field holds the length of the descriptor except
16956 		 * for the "type" and "additional length" fields, so
16957 		 * we need to add 2 to get the total length.
16958 		 */
16959 		descr_offset += (isd->isd_addl_length + 2);
16960 	}
16961 
16962 	return (result);
16963 }
16964 
16965 /*
16966  *    Function: sd_sense_key_no_sense
16967  *
16968  * Description: Recovery action when sense data was not received.
16969  *
16970  *     Context: May be called from interrupt context
16971  */
16972 
16973 static void
16974 sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
16975 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
16976 {
16977 	struct sd_sense_info	si;
16978 
16979 	ASSERT(un != NULL);
16980 	ASSERT(mutex_owned(SD_MUTEX(un)));
16981 	ASSERT(bp != NULL);
16982 	ASSERT(xp != NULL);
16983 	ASSERT(pktp != NULL);
16984 
16985 	si.ssi_severity = SCSI_ERR_FATAL;
16986 	si.ssi_pfa_flag = FALSE;
16987 
16988 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
16989 
16990 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
16991 		&si, EIO, (clock_t)0, NULL);
16992 }
16993 
16994 
16995 /*
16996  *    Function: sd_sense_key_recoverable_error
16997  *
16998  * Description: Recovery actions for a SCSI "Recovered Error" sense key.
16999  *
17000  *     Context: May be called from interrupt context
17001  */
17002 
17003 static void
17004 sd_sense_key_recoverable_error(struct sd_lun *un,
17005 	uint8_t asc,
17006 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17007 {
17008 	struct sd_sense_info	si;
17009 
17010 	ASSERT(un != NULL);
17011 	ASSERT(mutex_owned(SD_MUTEX(un)));
17012 	ASSERT(bp != NULL);
17013 	ASSERT(xp != NULL);
17014 	ASSERT(pktp != NULL);
17015 
17016 	/*
17017 	 * 0x5D: FAILURE PREDICTION THRESHOLD EXCEEDED
17018 	 */
17019 	if ((asc == 0x5D) && (sd_report_pfa != 0)) {
17020 		SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17021 		si.ssi_severity = SCSI_ERR_INFO;
17022 		si.ssi_pfa_flag = TRUE;
17023 	} else {
17024 		SD_UPDATE_ERRSTATS(un, sd_softerrs);
17025 		SD_UPDATE_ERRSTATS(un, sd_rq_recov_err);
17026 		si.ssi_severity = SCSI_ERR_RECOVERED;
17027 		si.ssi_pfa_flag = FALSE;
17028 	}
17029 
17030 	if (pktp->pkt_resid == 0) {
17031 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17032 		sd_return_command(un, bp);
17033 		return;
17034 	}
17035 
17036 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17037 	    &si, EIO, (clock_t)0, NULL);
17038 }
17039 
17040 
17041 
17042 
17043 /*
17044  *    Function: sd_sense_key_not_ready
17045  *
17046  * Description: Recovery actions for a SCSI "Not Ready" sense key.
17047  *
17048  *     Context: May be called from interrupt context
17049  */
17050 
17051 static void
17052 sd_sense_key_not_ready(struct sd_lun *un,
17053 	uint8_t asc, uint8_t ascq,
17054 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17055 {
17056 	struct sd_sense_info	si;
17057 
17058 	ASSERT(un != NULL);
17059 	ASSERT(mutex_owned(SD_MUTEX(un)));
17060 	ASSERT(bp != NULL);
17061 	ASSERT(xp != NULL);
17062 	ASSERT(pktp != NULL);
17063 
17064 	si.ssi_severity = SCSI_ERR_FATAL;
17065 	si.ssi_pfa_flag = FALSE;
17066 
17067 	/*
17068 	 * Update error stats after first NOT READY error. Disks may have
17069 	 * been powered down and may need to be restarted.  For CDROMs,
17070 	 * report NOT READY errors only if media is present.
17071 	 */
17072 	if ((ISCD(un) && (un->un_f_geometry_is_valid == TRUE)) ||
17073 	    (xp->xb_retry_count > 0)) {
17074 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17075 		SD_UPDATE_ERRSTATS(un, sd_rq_ntrdy_err);
17076 	}
17077 
17078 	/*
17079 	 * Just fail if the "not ready" retry limit has been reached.
17080 	 */
17081 	if (xp->xb_retry_count >= un->un_notready_retry_count) {
17082 		/* Special check for error message printing for removables. */
17083 		if ((ISREMOVABLE(un)) && (asc == 0x04) &&
17084 		    (ascq >= 0x04)) {
17085 			si.ssi_severity = SCSI_ERR_ALL;
17086 		}
17087 		goto fail_command;
17088 	}
17089 
17090 	/*
17091 	 * Check the ASC and ASCQ in the sense data as needed, to determine
17092 	 * what to do.
17093 	 */
17094 	switch (asc) {
17095 	case 0x04:	/* LOGICAL UNIT NOT READY */
17096 		/*
17097 		 * disk drives that don't spin up result in a very long delay
17098 		 * in format without warning messages. We will log a message
17099 		 * if the error level is set to verbose.
17100 		 */
17101 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17102 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17103 			    "logical unit not ready, resetting disk\n");
17104 		}
17105 
17106 		/*
17107 		 * There are different requirements for CDROMs and disks for
17108 		 * the number of retries.  If a CD-ROM is giving this, it is
17109 		 * probably reading TOC and is in the process of getting
17110 		 * ready, so we should keep on trying for a long time to make
17111 		 * sure that all types of media are taken in account (for
17112 		 * some media the drive takes a long time to read TOC).  For
17113 		 * disks we do not want to retry this too many times as this
17114 		 * can cause a long hang in format when the drive refuses to
17115 		 * spin up (a very common failure).
17116 		 */
17117 		switch (ascq) {
17118 		case 0x00:  /* LUN NOT READY, CAUSE NOT REPORTABLE */
17119 			/*
17120 			 * Disk drives frequently refuse to spin up which
17121 			 * results in a very long hang in format without
17122 			 * warning messages.
17123 			 *
17124 			 * Note: This code preserves the legacy behavior of
17125 			 * comparing xb_retry_count against zero for fibre
17126 			 * channel targets instead of comparing against the
17127 			 * un_reset_retry_count value.  The reason for this
17128 			 * discrepancy has been so utterly lost beneath the
17129 			 * Sands of Time that even Indiana Jones could not
17130 			 * find it.
17131 			 */
17132 			if (un->un_f_is_fibre == TRUE) {
17133 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17134 					(xp->xb_retry_count > 0)) &&
17135 					(un->un_startstop_timeid == NULL)) {
17136 					scsi_log(SD_DEVINFO(un), sd_label,
17137 					CE_WARN, "logical unit not ready, "
17138 					"resetting disk\n");
17139 					sd_reset_target(un, pktp);
17140 				}
17141 			} else {
17142 				if (((sd_level_mask & SD_LOGMASK_DIAG) ||
17143 					(xp->xb_retry_count >
17144 					un->un_reset_retry_count)) &&
17145 					(un->un_startstop_timeid == NULL)) {
17146 					scsi_log(SD_DEVINFO(un), sd_label,
17147 					CE_WARN, "logical unit not ready, "
17148 					"resetting disk\n");
17149 					sd_reset_target(un, pktp);
17150 				}
17151 			}
17152 			break;
17153 
17154 		case 0x01:  /* LUN IS IN PROCESS OF BECOMING READY */
17155 			/*
17156 			 * If the target is in the process of becoming
17157 			 * ready, just proceed with the retry. This can
17158 			 * happen with CD-ROMs that take a long time to
17159 			 * read TOC after a power cycle or reset.
17160 			 */
17161 			goto do_retry;
17162 
17163 		case 0x02:  /* LUN NOT READY, INITITIALIZING CMD REQUIRED */
17164 			break;
17165 
17166 		case 0x03:  /* LUN NOT READY, MANUAL INTERVENTION REQUIRED */
17167 			/*
17168 			 * Retries cannot help here so just fail right away.
17169 			 */
17170 			goto fail_command;
17171 
17172 		case 0x88:
17173 			/*
17174 			 * Vendor-unique code for T3/T4: it indicates a
17175 			 * path problem in a mutipathed config, but as far as
17176 			 * the target driver is concerned it equates to a fatal
17177 			 * error, so we should just fail the command right away
17178 			 * (without printing anything to the console). If this
17179 			 * is not a T3/T4, fall thru to the default recovery
17180 			 * action.
17181 			 * T3/T4 is FC only, don't need to check is_fibre
17182 			 */
17183 			if (SD_IS_T3(un) || SD_IS_T4(un)) {
17184 				sd_return_failed_command(un, bp, EIO);
17185 				return;
17186 			}
17187 			/* FALLTHRU */
17188 
17189 		case 0x04:  /* LUN NOT READY, FORMAT IN PROGRESS */
17190 		case 0x05:  /* LUN NOT READY, REBUILD IN PROGRESS */
17191 		case 0x06:  /* LUN NOT READY, RECALCULATION IN PROGRESS */
17192 		case 0x07:  /* LUN NOT READY, OPERATION IN PROGRESS */
17193 		case 0x08:  /* LUN NOT READY, LONG WRITE IN PROGRESS */
17194 		default:    /* Possible future codes in SCSI spec? */
17195 			/*
17196 			 * For removable-media devices, do not retry if
17197 			 * ASCQ > 2 as these result mostly from USCSI commands
17198 			 * on MMC devices issued to check status of an
17199 			 * operation initiated in immediate mode.  Also for
17200 			 * ASCQ >= 4 do not print console messages as these
17201 			 * mainly represent a user-initiated operation
17202 			 * instead of a system failure.
17203 			 */
17204 			if (ISREMOVABLE(un)) {
17205 				si.ssi_severity = SCSI_ERR_ALL;
17206 				goto fail_command;
17207 			}
17208 			break;
17209 		}
17210 
17211 		/*
17212 		 * As part of our recovery attempt for the NOT READY
17213 		 * condition, we issue a START STOP UNIT command. However
17214 		 * we want to wait for a short delay before attempting this
17215 		 * as there may still be more commands coming back from the
17216 		 * target with the check condition. To do this we use
17217 		 * timeout(9F) to call sd_start_stop_unit_callback() after
17218 		 * the delay interval expires. (sd_start_stop_unit_callback()
17219 		 * dispatches sd_start_stop_unit_task(), which will issue
17220 		 * the actual START STOP UNIT command. The delay interval
17221 		 * is one-half of the delay that we will use to retry the
17222 		 * command that generated the NOT READY condition.
17223 		 *
17224 		 * Note that we could just dispatch sd_start_stop_unit_task()
17225 		 * from here and allow it to sleep for the delay interval,
17226 		 * but then we would be tying up the taskq thread
17227 		 * uncesessarily for the duration of the delay.
17228 		 *
17229 		 * Do not issue the START STOP UNIT if the current command
17230 		 * is already a START STOP UNIT.
17231 		 */
17232 		if (pktp->pkt_cdbp[0] == SCMD_START_STOP) {
17233 			break;
17234 		}
17235 
17236 		/*
17237 		 * Do not schedule the timeout if one is already pending.
17238 		 */
17239 		if (un->un_startstop_timeid != NULL) {
17240 			SD_INFO(SD_LOG_ERROR, un,
17241 			    "sd_sense_key_not_ready: restart already issued to"
17242 			    " 0x%x : 0x%x\n", SD_TARGET(un), SD_LUN(un));
17243 			break;
17244 		}
17245 
17246 		/*
17247 		 * Schedule the START STOP UNIT command, then queue the command
17248 		 * for a retry.
17249 		 *
17250 		 * Note: A timeout is not scheduled for this retry because we
17251 		 * want the retry to be serial with the START_STOP_UNIT. The
17252 		 * retry will be started when the START_STOP_UNIT is completed
17253 		 * in sd_start_stop_unit_task.
17254 		 */
17255 		un->un_startstop_timeid = timeout(sd_start_stop_unit_callback,
17256 		    un, SD_BSY_TIMEOUT / 2);
17257 		xp->xb_retry_count++;
17258 		sd_set_retry_bp(un, bp, 0, kstat_waitq_enter);
17259 		return;
17260 
17261 	case 0x05:	/* LOGICAL UNIT DOES NOT RESPOND TO SELECTION */
17262 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17263 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17264 			    "unit does not respond to selection\n");
17265 		}
17266 		break;
17267 
17268 	case 0x3A:	/* MEDIUM NOT PRESENT */
17269 		if (sd_error_level >= SCSI_ERR_FATAL) {
17270 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17271 			    "Caddy not inserted in drive\n");
17272 		}
17273 
17274 		sr_ejected(un);
17275 		un->un_mediastate = DKIO_EJECTED;
17276 		/* The state has changed, inform the media watch routines */
17277 		cv_broadcast(&un->un_state_cv);
17278 		/* Just fail if no media is present in the drive. */
17279 		goto fail_command;
17280 
17281 	default:
17282 		if (sd_error_level < SCSI_ERR_RETRYABLE) {
17283 			scsi_log(SD_DEVINFO(un), sd_label, CE_NOTE,
17284 			    "Unit not Ready. Additional sense code 0x%x\n",
17285 			    asc);
17286 		}
17287 		break;
17288 	}
17289 
17290 do_retry:
17291 
17292 	/*
17293 	 * Retry the command, as some targets may report NOT READY for
17294 	 * several seconds after being reset.
17295 	 */
17296 	xp->xb_retry_count++;
17297 	si.ssi_severity = SCSI_ERR_RETRYABLE;
17298 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
17299 	    &si, EIO, SD_BSY_TIMEOUT, NULL);
17300 
17301 	return;
17302 
17303 fail_command:
17304 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17305 	sd_return_failed_command(un, bp, EIO);
17306 }
17307 
17308 
17309 
17310 /*
17311  *    Function: sd_sense_key_medium_or_hardware_error
17312  *
17313  * Description: Recovery actions for a SCSI "Medium Error" or "Hardware Error"
17314  *		sense key.
17315  *
17316  *     Context: May be called from interrupt context
17317  */
17318 
17319 static void
17320 sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
17321 	int sense_key, uint8_t asc,
17322 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17323 {
17324 	struct sd_sense_info	si;
17325 
17326 	ASSERT(un != NULL);
17327 	ASSERT(mutex_owned(SD_MUTEX(un)));
17328 	ASSERT(bp != NULL);
17329 	ASSERT(xp != NULL);
17330 	ASSERT(pktp != NULL);
17331 
17332 	si.ssi_severity = SCSI_ERR_FATAL;
17333 	si.ssi_pfa_flag = FALSE;
17334 
17335 	if (sense_key == KEY_MEDIUM_ERROR) {
17336 		SD_UPDATE_ERRSTATS(un, sd_rq_media_err);
17337 	}
17338 
17339 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17340 
17341 	if ((un->un_reset_retry_count != 0) &&
17342 	    (xp->xb_retry_count == un->un_reset_retry_count)) {
17343 		mutex_exit(SD_MUTEX(un));
17344 		/* Do NOT do a RESET_ALL here: too intrusive. (4112858) */
17345 		if (un->un_f_allow_bus_device_reset == TRUE) {
17346 
17347 			boolean_t try_resetting_target = B_TRUE;
17348 
17349 			/*
17350 			 * We need to be able to handle specific ASC when we are
17351 			 * handling a KEY_HARDWARE_ERROR. In particular
17352 			 * taking the default action of resetting the target may
17353 			 * not be the appropriate way to attempt recovery.
17354 			 * Resetting a target because of a single LUN failure
17355 			 * victimizes all LUNs on that target.
17356 			 *
17357 			 * This is true for the LSI arrays, if an LSI
17358 			 * array controller returns an ASC of 0x84 (LUN Dead) we
17359 			 * should trust it.
17360 			 */
17361 
17362 			if (sense_key == KEY_HARDWARE_ERROR) {
17363 				switch (asc) {
17364 				case 0x84:
17365 					if (SD_IS_LSI(un)) {
17366 						try_resetting_target = B_FALSE;
17367 					}
17368 					break;
17369 				default:
17370 					break;
17371 				}
17372 			}
17373 
17374 			if (try_resetting_target == B_TRUE) {
17375 				int reset_retval = 0;
17376 				if (un->un_f_lun_reset_enabled == TRUE) {
17377 					SD_TRACE(SD_LOG_IO_CORE, un,
17378 					    "sd_sense_key_medium_or_hardware_"
17379 					    "error: issuing RESET_LUN\n");
17380 					reset_retval =
17381 					    scsi_reset(SD_ADDRESS(un),
17382 					    RESET_LUN);
17383 				}
17384 				if (reset_retval == 0) {
17385 					SD_TRACE(SD_LOG_IO_CORE, un,
17386 					    "sd_sense_key_medium_or_hardware_"
17387 					    "error: issuing RESET_TARGET\n");
17388 					(void) scsi_reset(SD_ADDRESS(un),
17389 					    RESET_TARGET);
17390 				}
17391 			}
17392 		}
17393 		mutex_enter(SD_MUTEX(un));
17394 	}
17395 
17396 	/*
17397 	 * This really ought to be a fatal error, but we will retry anyway
17398 	 * as some drives report this as a spurious error.
17399 	 */
17400 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17401 	    &si, EIO, (clock_t)0, NULL);
17402 }
17403 
17404 
17405 
17406 /*
17407  *    Function: sd_sense_key_illegal_request
17408  *
17409  * Description: Recovery actions for a SCSI "Illegal Request" sense key.
17410  *
17411  *     Context: May be called from interrupt context
17412  */
17413 
17414 static void
17415 sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
17416 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17417 {
17418 	struct sd_sense_info	si;
17419 
17420 	ASSERT(un != NULL);
17421 	ASSERT(mutex_owned(SD_MUTEX(un)));
17422 	ASSERT(bp != NULL);
17423 	ASSERT(xp != NULL);
17424 	ASSERT(pktp != NULL);
17425 
17426 	SD_UPDATE_ERRSTATS(un, sd_softerrs);
17427 	SD_UPDATE_ERRSTATS(un, sd_rq_illrq_err);
17428 
17429 	si.ssi_severity = SCSI_ERR_INFO;
17430 	si.ssi_pfa_flag = FALSE;
17431 
17432 	/* Pointless to retry if the target thinks it's an illegal request */
17433 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17434 	sd_return_failed_command(un, bp, EIO);
17435 }
17436 
17437 
17438 
17439 
17440 /*
17441  *    Function: sd_sense_key_unit_attention
17442  *
17443  * Description: Recovery actions for a SCSI "Unit Attention" sense key.
17444  *
17445  *     Context: May be called from interrupt context
17446  */
17447 
17448 static void
17449 sd_sense_key_unit_attention(struct sd_lun *un,
17450 	uint8_t asc,
17451 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17452 {
17453 	/*
17454 	 * For UNIT ATTENTION we allow retries for one minute. Devices
17455 	 * like Sonoma can return UNIT ATTENTION close to a minute
17456 	 * under certain conditions.
17457 	 */
17458 	int	retry_check_flag = SD_RETRIES_UA;
17459 	struct	sd_sense_info		si;
17460 
17461 	ASSERT(un != NULL);
17462 	ASSERT(mutex_owned(SD_MUTEX(un)));
17463 	ASSERT(bp != NULL);
17464 	ASSERT(xp != NULL);
17465 	ASSERT(pktp != NULL);
17466 
17467 	si.ssi_severity = SCSI_ERR_INFO;
17468 	si.ssi_pfa_flag = FALSE;
17469 
17470 
17471 	switch (asc) {
17472 	case 0x5D:  /* FAILURE PREDICTION THRESHOLD EXCEEDED */
17473 		if (sd_report_pfa != 0) {
17474 			SD_UPDATE_ERRSTATS(un, sd_rq_pfa_err);
17475 			si.ssi_pfa_flag = TRUE;
17476 			retry_check_flag = SD_RETRIES_STANDARD;
17477 			goto do_retry;
17478 		}
17479 		break;
17480 
17481 	case 0x29:  /* POWER ON, RESET, OR BUS DEVICE RESET OCCURRED */
17482 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
17483 			un->un_resvd_status |=
17484 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
17485 		}
17486 		/* FALLTHRU */
17487 
17488 	case 0x28: /* NOT READY TO READY CHANGE, MEDIUM MAY HAVE CHANGED */
17489 		if (!ISREMOVABLE(un)) {
17490 			break;
17491 		}
17492 
17493 		/*
17494 		 * When we get a unit attention from a removable-media device,
17495 		 * it may be in a state that will take a long time to recover
17496 		 * (e.g., from a reset).  Since we are executing in interrupt
17497 		 * context here, we cannot wait around for the device to come
17498 		 * back. So hand this command off to sd_media_change_task()
17499 		 * for deferred processing under taskq thread context. (Note
17500 		 * that the command still may be failed if a problem is
17501 		 * encountered at a later time.)
17502 		 */
17503 		if (taskq_dispatch(sd_tq, sd_media_change_task, pktp,
17504 		    KM_NOSLEEP) == 0) {
17505 			/*
17506 			 * Cannot dispatch the request so fail the command.
17507 			 */
17508 			SD_UPDATE_ERRSTATS(un, sd_harderrs);
17509 			SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
17510 			si.ssi_severity = SCSI_ERR_FATAL;
17511 			sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17512 			sd_return_failed_command(un, bp, EIO);
17513 		}
17514 		/*
17515 		 * Either the command has been successfully dispatched to a
17516 		 * task Q for retrying, or the dispatch failed. In either case
17517 		 * do NOT retry again by calling sd_retry_command. This sets up
17518 		 * two retries of the same command and when one completes and
17519 		 * frees the resources the other will access freed memory,
17520 		 * a bad thing.
17521 		 */
17522 		return;
17523 
17524 	default:
17525 		break;
17526 	}
17527 
17528 	if (!ISREMOVABLE(un)) {
17529 		/*
17530 		 * Do not update these here for removables. For removables
17531 		 * these stats are updated (1) above if we failed to dispatch
17532 		 * sd_media_change_task(), or (2) sd_media_change_task() may
17533 		 * update these later if it encounters an error.
17534 		 */
17535 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
17536 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
17537 	}
17538 
17539 do_retry:
17540 	sd_retry_command(un, bp, retry_check_flag, sd_print_sense_msg, &si,
17541 	    EIO, SD_UA_RETRY_DELAY, NULL);
17542 }
17543 
17544 
17545 
17546 /*
17547  *    Function: sd_sense_key_fail_command
17548  *
17549  * Description: Use to fail a command when we don't like the sense key that
17550  *		was returned.
17551  *
17552  *     Context: May be called from interrupt context
17553  */
17554 
17555 static void
17556 sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
17557 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17558 {
17559 	struct sd_sense_info	si;
17560 
17561 	ASSERT(un != NULL);
17562 	ASSERT(mutex_owned(SD_MUTEX(un)));
17563 	ASSERT(bp != NULL);
17564 	ASSERT(xp != NULL);
17565 	ASSERT(pktp != NULL);
17566 
17567 	si.ssi_severity = SCSI_ERR_FATAL;
17568 	si.ssi_pfa_flag = FALSE;
17569 
17570 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17571 	sd_return_failed_command(un, bp, EIO);
17572 }
17573 
17574 
17575 
17576 /*
17577  *    Function: sd_sense_key_blank_check
17578  *
17579  * Description: Recovery actions for a SCSI "Blank Check" sense key.
17580  *		Has no monetary connotation.
17581  *
17582  *     Context: May be called from interrupt context
17583  */
17584 
17585 static void
17586 sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
17587 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17588 {
17589 	struct sd_sense_info	si;
17590 
17591 	ASSERT(un != NULL);
17592 	ASSERT(mutex_owned(SD_MUTEX(un)));
17593 	ASSERT(bp != NULL);
17594 	ASSERT(xp != NULL);
17595 	ASSERT(pktp != NULL);
17596 
17597 	/*
17598 	 * Blank check is not fatal for removable devices, therefore
17599 	 * it does not require a console message.
17600 	 */
17601 	si.ssi_severity = (ISREMOVABLE(un)) ? SCSI_ERR_ALL : SCSI_ERR_FATAL;
17602 	si.ssi_pfa_flag = FALSE;
17603 
17604 	sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
17605 	sd_return_failed_command(un, bp, EIO);
17606 }
17607 
17608 
17609 
17610 
17611 /*
17612  *    Function: sd_sense_key_aborted_command
17613  *
17614  * Description: Recovery actions for a SCSI "Aborted Command" sense key.
17615  *
17616  *     Context: May be called from interrupt context
17617  */
17618 
17619 static void
17620 sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
17621 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17622 {
17623 	struct sd_sense_info	si;
17624 
17625 	ASSERT(un != NULL);
17626 	ASSERT(mutex_owned(SD_MUTEX(un)));
17627 	ASSERT(bp != NULL);
17628 	ASSERT(xp != NULL);
17629 	ASSERT(pktp != NULL);
17630 
17631 	si.ssi_severity = SCSI_ERR_FATAL;
17632 	si.ssi_pfa_flag = FALSE;
17633 
17634 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17635 
17636 	/*
17637 	 * This really ought to be a fatal error, but we will retry anyway
17638 	 * as some drives report this as a spurious error.
17639 	 */
17640 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17641 	    &si, EIO, (clock_t)0, NULL);
17642 }
17643 
17644 
17645 
17646 /*
17647  *    Function: sd_sense_key_default
17648  *
17649  * Description: Default recovery action for several SCSI sense keys (basically
17650  *		attempts a retry).
17651  *
17652  *     Context: May be called from interrupt context
17653  */
17654 
17655 static void
17656 sd_sense_key_default(struct sd_lun *un,
17657 	int sense_key,
17658 	struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp)
17659 {
17660 	struct sd_sense_info	si;
17661 
17662 	ASSERT(un != NULL);
17663 	ASSERT(mutex_owned(SD_MUTEX(un)));
17664 	ASSERT(bp != NULL);
17665 	ASSERT(xp != NULL);
17666 	ASSERT(pktp != NULL);
17667 
17668 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17669 
17670 	/*
17671 	 * Undecoded sense key.	Attempt retries and hope that will fix
17672 	 * the problem.  Otherwise, we're dead.
17673 	 */
17674 	if ((pktp->pkt_flags & FLAG_SILENT) == 0) {
17675 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17676 		    "Unhandled Sense Key '%s'\n", sense_keys[sense_key]);
17677 	}
17678 
17679 	si.ssi_severity = SCSI_ERR_FATAL;
17680 	si.ssi_pfa_flag = FALSE;
17681 
17682 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, sd_print_sense_msg,
17683 	    &si, EIO, (clock_t)0, NULL);
17684 }
17685 
17686 
17687 
17688 /*
17689  *    Function: sd_print_retry_msg
17690  *
17691  * Description: Print a message indicating the retry action being taken.
17692  *
17693  *   Arguments: un - ptr to associated softstate
17694  *		bp - ptr to buf(9S) for the command
17695  *		arg - not used.
17696  *		flag - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17697  *			or SD_NO_RETRY_ISSUED
17698  *
17699  *     Context: May be called from interrupt context
17700  */
17701 /* ARGSUSED */
17702 static void
17703 sd_print_retry_msg(struct sd_lun *un, struct buf *bp, void *arg, int flag)
17704 {
17705 	struct sd_xbuf	*xp;
17706 	struct scsi_pkt *pktp;
17707 	char *reasonp;
17708 	char *msgp;
17709 
17710 	ASSERT(un != NULL);
17711 	ASSERT(mutex_owned(SD_MUTEX(un)));
17712 	ASSERT(bp != NULL);
17713 	pktp = SD_GET_PKTP(bp);
17714 	ASSERT(pktp != NULL);
17715 	xp = SD_GET_XBUF(bp);
17716 	ASSERT(xp != NULL);
17717 
17718 	ASSERT(!mutex_owned(&un->un_pm_mutex));
17719 	mutex_enter(&un->un_pm_mutex);
17720 	if ((un->un_state == SD_STATE_SUSPENDED) ||
17721 	    (SD_DEVICE_IS_IN_LOW_POWER(un)) ||
17722 	    (pktp->pkt_flags & FLAG_SILENT)) {
17723 		mutex_exit(&un->un_pm_mutex);
17724 		goto update_pkt_reason;
17725 	}
17726 	mutex_exit(&un->un_pm_mutex);
17727 
17728 	/*
17729 	 * Suppress messages if they are all the same pkt_reason; with
17730 	 * TQ, many (up to 256) are returned with the same pkt_reason.
17731 	 * If we are in panic, then suppress the retry messages.
17732 	 */
17733 	switch (flag) {
17734 	case SD_NO_RETRY_ISSUED:
17735 		msgp = "giving up";
17736 		break;
17737 	case SD_IMMEDIATE_RETRY_ISSUED:
17738 	case SD_DELAYED_RETRY_ISSUED:
17739 		if (ddi_in_panic() || (un->un_state == SD_STATE_OFFLINE) ||
17740 		    ((pktp->pkt_reason == un->un_last_pkt_reason) &&
17741 		    (sd_error_level != SCSI_ERR_ALL))) {
17742 			return;
17743 		}
17744 		msgp = "retrying command";
17745 		break;
17746 	default:
17747 		goto update_pkt_reason;
17748 	}
17749 
17750 	reasonp = (((pktp->pkt_statistics & STAT_PERR) != 0) ? "parity error" :
17751 	    scsi_rname(pktp->pkt_reason));
17752 
17753 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17754 	    "SCSI transport failed: reason '%s': %s\n", reasonp, msgp);
17755 
17756 update_pkt_reason:
17757 	/*
17758 	 * Update un->un_last_pkt_reason with the value in pktp->pkt_reason.
17759 	 * This is to prevent multiple console messages for the same failure
17760 	 * condition.  Note that un->un_last_pkt_reason is NOT restored if &
17761 	 * when the command is retried successfully because there still may be
17762 	 * more commands coming back with the same value of pktp->pkt_reason.
17763 	 */
17764 	if ((pktp->pkt_reason != CMD_CMPLT) || (xp->xb_retry_count == 0)) {
17765 		un->un_last_pkt_reason = pktp->pkt_reason;
17766 	}
17767 }
17768 
17769 
17770 /*
17771  *    Function: sd_print_cmd_incomplete_msg
17772  *
17773  * Description: Message logging fn. for a SCSA "CMD_INCOMPLETE" pkt_reason.
17774  *
17775  *   Arguments: un - ptr to associated softstate
17776  *		bp - ptr to buf(9S) for the command
17777  *		arg - passed to sd_print_retry_msg()
17778  *		code - SD_IMMEDIATE_RETRY_ISSUED, SD_DELAYED_RETRY_ISSUED,
17779  *			or SD_NO_RETRY_ISSUED
17780  *
17781  *     Context: May be called from interrupt context
17782  */
17783 
17784 static void
17785 sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp, void *arg,
17786 	int code)
17787 {
17788 	dev_info_t	*dip;
17789 
17790 	ASSERT(un != NULL);
17791 	ASSERT(mutex_owned(SD_MUTEX(un)));
17792 	ASSERT(bp != NULL);
17793 
17794 	switch (code) {
17795 	case SD_NO_RETRY_ISSUED:
17796 		/* Command was failed. Someone turned off this target? */
17797 		if (un->un_state != SD_STATE_OFFLINE) {
17798 			/*
17799 			 * Suppress message if we are detaching and
17800 			 * device has been disconnected
17801 			 * Note that DEVI_IS_DEVICE_REMOVED is a consolidation
17802 			 * private interface and not part of the DDI
17803 			 */
17804 			dip = un->un_sd->sd_dev;
17805 			if (!(DEVI_IS_DETACHING(dip) &&
17806 			    DEVI_IS_DEVICE_REMOVED(dip))) {
17807 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
17808 				"disk not responding to selection\n");
17809 			}
17810 			New_state(un, SD_STATE_OFFLINE);
17811 		}
17812 		break;
17813 
17814 	case SD_DELAYED_RETRY_ISSUED:
17815 	case SD_IMMEDIATE_RETRY_ISSUED:
17816 	default:
17817 		/* Command was successfully queued for retry */
17818 		sd_print_retry_msg(un, bp, arg, code);
17819 		break;
17820 	}
17821 }
17822 
17823 
17824 /*
17825  *    Function: sd_pkt_reason_cmd_incomplete
17826  *
17827  * Description: Recovery actions for a SCSA "CMD_INCOMPLETE" pkt_reason.
17828  *
17829  *     Context: May be called from interrupt context
17830  */
17831 
17832 static void
17833 sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
17834 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17835 {
17836 	int flag = SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE;
17837 
17838 	ASSERT(un != NULL);
17839 	ASSERT(mutex_owned(SD_MUTEX(un)));
17840 	ASSERT(bp != NULL);
17841 	ASSERT(xp != NULL);
17842 	ASSERT(pktp != NULL);
17843 
17844 	/* Do not do a reset if selection did not complete */
17845 	/* Note: Should this not just check the bit? */
17846 	if (pktp->pkt_state != STATE_GOT_BUS) {
17847 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
17848 		sd_reset_target(un, pktp);
17849 	}
17850 
17851 	/*
17852 	 * If the target was not successfully selected, then set
17853 	 * SD_RETRIES_FAILFAST to indicate that we lost communication
17854 	 * with the target, and further retries and/or commands are
17855 	 * likely to take a long time.
17856 	 */
17857 	if ((pktp->pkt_state & STATE_GOT_TARGET) == 0) {
17858 		flag |= SD_RETRIES_FAILFAST;
17859 	}
17860 
17861 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
17862 
17863 	sd_retry_command(un, bp, flag,
17864 	    sd_print_cmd_incomplete_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
17865 }
17866 
17867 
17868 
17869 /*
17870  *    Function: sd_pkt_reason_cmd_tran_err
17871  *
17872  * Description: Recovery actions for a SCSA "CMD_TRAN_ERR" pkt_reason.
17873  *
17874  *     Context: May be called from interrupt context
17875  */
17876 
17877 static void
17878 sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
17879 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17880 {
17881 	ASSERT(un != NULL);
17882 	ASSERT(mutex_owned(SD_MUTEX(un)));
17883 	ASSERT(bp != NULL);
17884 	ASSERT(xp != NULL);
17885 	ASSERT(pktp != NULL);
17886 
17887 	/*
17888 	 * Do not reset if we got a parity error, or if
17889 	 * selection did not complete.
17890 	 */
17891 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
17892 	/* Note: Should this not just check the bit for pkt_state? */
17893 	if (((pktp->pkt_statistics & STAT_PERR) == 0) &&
17894 	    (pktp->pkt_state != STATE_GOT_BUS)) {
17895 		SD_UPDATE_ERRSTATS(un, sd_transerrs);
17896 		sd_reset_target(un, pktp);
17897 	}
17898 
17899 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
17900 
17901 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
17902 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
17903 }
17904 
17905 
17906 
17907 /*
17908  *    Function: sd_pkt_reason_cmd_reset
17909  *
17910  * Description: Recovery actions for a SCSA "CMD_RESET" pkt_reason.
17911  *
17912  *     Context: May be called from interrupt context
17913  */
17914 
17915 static void
17916 sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
17917 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17918 {
17919 	ASSERT(un != NULL);
17920 	ASSERT(mutex_owned(SD_MUTEX(un)));
17921 	ASSERT(bp != NULL);
17922 	ASSERT(xp != NULL);
17923 	ASSERT(pktp != NULL);
17924 
17925 	/* The target may still be running the command, so try to reset. */
17926 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
17927 	sd_reset_target(un, pktp);
17928 
17929 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
17930 
17931 	/*
17932 	 * If pkt_reason is CMD_RESET chances are that this pkt got
17933 	 * reset because another target on this bus caused it. The target
17934 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
17935 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
17936 	 */
17937 
17938 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
17939 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
17940 }
17941 
17942 
17943 
17944 
17945 /*
17946  *    Function: sd_pkt_reason_cmd_aborted
17947  *
17948  * Description: Recovery actions for a SCSA "CMD_ABORTED" pkt_reason.
17949  *
17950  *     Context: May be called from interrupt context
17951  */
17952 
17953 static void
17954 sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
17955 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17956 {
17957 	ASSERT(un != NULL);
17958 	ASSERT(mutex_owned(SD_MUTEX(un)));
17959 	ASSERT(bp != NULL);
17960 	ASSERT(xp != NULL);
17961 	ASSERT(pktp != NULL);
17962 
17963 	/* The target may still be running the command, so try to reset. */
17964 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
17965 	sd_reset_target(un, pktp);
17966 
17967 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
17968 
17969 	/*
17970 	 * If pkt_reason is CMD_ABORTED chances are that this pkt got
17971 	 * aborted because another target on this bus caused it. The target
17972 	 * that caused it should get CMD_TIMEOUT with pkt_statistics
17973 	 * of STAT_TIMEOUT/STAT_DEV_RESET.
17974 	 */
17975 
17976 	sd_retry_command(un, bp, (SD_RETRIES_VICTIM | SD_RETRIES_ISOLATE),
17977 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
17978 }
17979 
17980 
17981 
17982 /*
17983  *    Function: sd_pkt_reason_cmd_timeout
17984  *
17985  * Description: Recovery actions for a SCSA "CMD_TIMEOUT" pkt_reason.
17986  *
17987  *     Context: May be called from interrupt context
17988  */
17989 
17990 static void
17991 sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
17992 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
17993 {
17994 	ASSERT(un != NULL);
17995 	ASSERT(mutex_owned(SD_MUTEX(un)));
17996 	ASSERT(bp != NULL);
17997 	ASSERT(xp != NULL);
17998 	ASSERT(pktp != NULL);
17999 
18000 
18001 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18002 	sd_reset_target(un, pktp);
18003 
18004 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18005 
18006 	/*
18007 	 * A command timeout indicates that we could not establish
18008 	 * communication with the target, so set SD_RETRIES_FAILFAST
18009 	 * as further retries/commands are likely to take a long time.
18010 	 */
18011 	sd_retry_command(un, bp,
18012 	    (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE | SD_RETRIES_FAILFAST),
18013 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18014 }
18015 
18016 
18017 
18018 /*
18019  *    Function: sd_pkt_reason_cmd_unx_bus_free
18020  *
18021  * Description: Recovery actions for a SCSA "CMD_UNX_BUS_FREE" pkt_reason.
18022  *
18023  *     Context: May be called from interrupt context
18024  */
18025 
18026 static void
18027 sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
18028 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18029 {
18030 	void (*funcp)(struct sd_lun *un, struct buf *bp, void *arg, int code);
18031 
18032 	ASSERT(un != NULL);
18033 	ASSERT(mutex_owned(SD_MUTEX(un)));
18034 	ASSERT(bp != NULL);
18035 	ASSERT(xp != NULL);
18036 	ASSERT(pktp != NULL);
18037 
18038 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18039 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18040 
18041 	funcp = ((pktp->pkt_statistics & STAT_PERR) == 0) ?
18042 	    sd_print_retry_msg : NULL;
18043 
18044 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18045 	    funcp, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18046 }
18047 
18048 
18049 /*
18050  *    Function: sd_pkt_reason_cmd_tag_reject
18051  *
18052  * Description: Recovery actions for a SCSA "CMD_TAG_REJECT" pkt_reason.
18053  *
18054  *     Context: May be called from interrupt context
18055  */
18056 
18057 static void
18058 sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
18059 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18060 {
18061 	ASSERT(un != NULL);
18062 	ASSERT(mutex_owned(SD_MUTEX(un)));
18063 	ASSERT(bp != NULL);
18064 	ASSERT(xp != NULL);
18065 	ASSERT(pktp != NULL);
18066 
18067 	SD_UPDATE_ERRSTATS(un, sd_harderrs);
18068 	pktp->pkt_flags = 0;
18069 	un->un_tagflags = 0;
18070 	if (un->un_f_opt_queueing == TRUE) {
18071 		un->un_throttle = min(un->un_throttle, 3);
18072 	} else {
18073 		un->un_throttle = 1;
18074 	}
18075 	mutex_exit(SD_MUTEX(un));
18076 	(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
18077 	mutex_enter(SD_MUTEX(un));
18078 
18079 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18080 
18081 	/* Legacy behavior not to check retry counts here. */
18082 	sd_retry_command(un, bp, (SD_RETRIES_NOCHECK | SD_RETRIES_ISOLATE),
18083 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18084 }
18085 
18086 
18087 /*
18088  *    Function: sd_pkt_reason_default
18089  *
18090  * Description: Default recovery actions for SCSA pkt_reason values that
18091  *		do not have more explicit recovery actions.
18092  *
18093  *     Context: May be called from interrupt context
18094  */
18095 
18096 static void
18097 sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
18098 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18099 {
18100 	ASSERT(un != NULL);
18101 	ASSERT(mutex_owned(SD_MUTEX(un)));
18102 	ASSERT(bp != NULL);
18103 	ASSERT(xp != NULL);
18104 	ASSERT(pktp != NULL);
18105 
18106 	SD_UPDATE_ERRSTATS(un, sd_transerrs);
18107 	sd_reset_target(un, pktp);
18108 
18109 	SD_UPDATE_RESERVATION_STATUS(un, pktp);
18110 
18111 	sd_retry_command(un, bp, (SD_RETRIES_STANDARD | SD_RETRIES_ISOLATE),
18112 	    sd_print_retry_msg, NULL, EIO, SD_RESTART_TIMEOUT, NULL);
18113 }
18114 
18115 
18116 
18117 /*
18118  *    Function: sd_pkt_status_check_condition
18119  *
18120  * Description: Recovery actions for a "STATUS_CHECK" SCSI command status.
18121  *
18122  *     Context: May be called from interrupt context
18123  */
18124 
18125 static void
18126 sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
18127 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18128 {
18129 	ASSERT(un != NULL);
18130 	ASSERT(mutex_owned(SD_MUTEX(un)));
18131 	ASSERT(bp != NULL);
18132 	ASSERT(xp != NULL);
18133 	ASSERT(pktp != NULL);
18134 
18135 	SD_TRACE(SD_LOG_IO, un, "sd_pkt_status_check_condition: "
18136 	    "entry: buf:0x%p xp:0x%p\n", bp, xp);
18137 
18138 	/*
18139 	 * If ARQ is NOT enabled, then issue a REQUEST SENSE command (the
18140 	 * command will be retried after the request sense). Otherwise, retry
18141 	 * the command. Note: we are issuing the request sense even though the
18142 	 * retry limit may have been reached for the failed command.
18143 	 */
18144 	if (un->un_f_arq_enabled == FALSE) {
18145 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18146 		    "no ARQ, sending request sense command\n");
18147 		sd_send_request_sense_command(un, bp, pktp);
18148 	} else {
18149 		SD_INFO(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: "
18150 		    "ARQ,retrying request sense command\n");
18151 #if defined(__i386) || defined(__amd64)
18152 		/*
18153 		 * The SD_RETRY_DELAY value need to be adjusted here
18154 		 * when SD_RETRY_DELAY change in sddef.h
18155 		 */
18156 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, 0,
18157 			un->un_f_is_fibre?drv_usectohz(100000):(clock_t)0,
18158 			NULL);
18159 #else
18160 		sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL,
18161 		    0, SD_RETRY_DELAY, NULL);
18162 #endif
18163 	}
18164 
18165 	SD_TRACE(SD_LOG_IO_CORE, un, "sd_pkt_status_check_condition: exit\n");
18166 }
18167 
18168 
18169 /*
18170  *    Function: sd_pkt_status_busy
18171  *
18172  * Description: Recovery actions for a "STATUS_BUSY" SCSI command status.
18173  *
18174  *     Context: May be called from interrupt context
18175  */
18176 
18177 static void
18178 sd_pkt_status_busy(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
18179 	struct scsi_pkt *pktp)
18180 {
18181 	ASSERT(un != NULL);
18182 	ASSERT(mutex_owned(SD_MUTEX(un)));
18183 	ASSERT(bp != NULL);
18184 	ASSERT(xp != NULL);
18185 	ASSERT(pktp != NULL);
18186 
18187 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18188 	    "sd_pkt_status_busy: entry\n");
18189 
18190 	/* If retries are exhausted, just fail the command. */
18191 	if (xp->xb_retry_count >= un->un_busy_retry_count) {
18192 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
18193 		    "device busy too long\n");
18194 		sd_return_failed_command(un, bp, EIO);
18195 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18196 		    "sd_pkt_status_busy: exit\n");
18197 		return;
18198 	}
18199 	xp->xb_retry_count++;
18200 
18201 	/*
18202 	 * Try to reset the target. However, we do not want to perform
18203 	 * more than one reset if the device continues to fail. The reset
18204 	 * will be performed when the retry count reaches the reset
18205 	 * threshold.  This threshold should be set such that at least
18206 	 * one retry is issued before the reset is performed.
18207 	 */
18208 	if (xp->xb_retry_count ==
18209 	    ((un->un_reset_retry_count < 2) ? 2 : un->un_reset_retry_count)) {
18210 		int rval = 0;
18211 		mutex_exit(SD_MUTEX(un));
18212 		if (un->un_f_allow_bus_device_reset == TRUE) {
18213 			/*
18214 			 * First try to reset the LUN; if we cannot then
18215 			 * try to reset the target.
18216 			 */
18217 			if (un->un_f_lun_reset_enabled == TRUE) {
18218 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18219 				    "sd_pkt_status_busy: RESET_LUN\n");
18220 				rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18221 			}
18222 			if (rval == 0) {
18223 				SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18224 				    "sd_pkt_status_busy: RESET_TARGET\n");
18225 				rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
18226 			}
18227 		}
18228 		if (rval == 0) {
18229 			/*
18230 			 * If the RESET_LUN and/or RESET_TARGET failed,
18231 			 * try RESET_ALL
18232 			 */
18233 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18234 			    "sd_pkt_status_busy: RESET_ALL\n");
18235 			rval = scsi_reset(SD_ADDRESS(un), RESET_ALL);
18236 		}
18237 		mutex_enter(SD_MUTEX(un));
18238 		if (rval == 0) {
18239 			/*
18240 			 * The RESET_LUN, RESET_TARGET, and/or RESET_ALL failed.
18241 			 * At this point we give up & fail the command.
18242 			 */
18243 			sd_return_failed_command(un, bp, EIO);
18244 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18245 			    "sd_pkt_status_busy: exit (failed cmd)\n");
18246 			return;
18247 		}
18248 	}
18249 
18250 	/*
18251 	 * Retry the command. Be sure to specify SD_RETRIES_NOCHECK as
18252 	 * we have already checked the retry counts above.
18253 	 */
18254 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL,
18255 	    EIO, SD_BSY_TIMEOUT, NULL);
18256 
18257 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18258 	    "sd_pkt_status_busy: exit\n");
18259 }
18260 
18261 
18262 /*
18263  *    Function: sd_pkt_status_reservation_conflict
18264  *
18265  * Description: Recovery actions for a "STATUS_RESERVATION_CONFLICT" SCSI
18266  *		command status.
18267  *
18268  *     Context: May be called from interrupt context
18269  */
18270 
18271 static void
18272 sd_pkt_status_reservation_conflict(struct sd_lun *un, struct buf *bp,
18273 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18274 {
18275 	ASSERT(un != NULL);
18276 	ASSERT(mutex_owned(SD_MUTEX(un)));
18277 	ASSERT(bp != NULL);
18278 	ASSERT(xp != NULL);
18279 	ASSERT(pktp != NULL);
18280 
18281 	/*
18282 	 * If the command was PERSISTENT_RESERVATION_[IN|OUT] then reservation
18283 	 * conflict could be due to various reasons like incorrect keys, not
18284 	 * registered or not reserved etc. So, we return EACCES to the caller.
18285 	 */
18286 	if (un->un_reservation_type == SD_SCSI3_RESERVATION) {
18287 		int cmd = SD_GET_PKT_OPCODE(pktp);
18288 		if ((cmd == SCMD_PERSISTENT_RESERVE_IN) ||
18289 		    (cmd == SCMD_PERSISTENT_RESERVE_OUT)) {
18290 			sd_return_failed_command(un, bp, EACCES);
18291 			return;
18292 		}
18293 	}
18294 
18295 	un->un_resvd_status |= SD_RESERVATION_CONFLICT;
18296 
18297 	if ((un->un_resvd_status & SD_FAILFAST) != 0) {
18298 		if (sd_failfast_enable != 0) {
18299 			/* By definition, we must panic here.... */
18300 			panic("Reservation Conflict");
18301 			/*NOTREACHED*/
18302 		}
18303 		SD_ERROR(SD_LOG_IO, un,
18304 		    "sd_handle_resv_conflict: Disk Reserved\n");
18305 		sd_return_failed_command(un, bp, EACCES);
18306 		return;
18307 	}
18308 
18309 	/*
18310 	 * 1147670: retry only if sd_retry_on_reservation_conflict
18311 	 * property is set (default is 1). Retries will not succeed
18312 	 * on a disk reserved by another initiator. HA systems
18313 	 * may reset this via sd.conf to avoid these retries.
18314 	 *
18315 	 * Note: The legacy return code for this failure is EIO, however EACCES
18316 	 * seems more appropriate for a reservation conflict.
18317 	 */
18318 	if (sd_retry_on_reservation_conflict == 0) {
18319 		SD_ERROR(SD_LOG_IO, un,
18320 		    "sd_handle_resv_conflict: Device Reserved\n");
18321 		sd_return_failed_command(un, bp, EIO);
18322 		return;
18323 	}
18324 
18325 	/*
18326 	 * Retry the command if we can.
18327 	 *
18328 	 * Note: The legacy return code for this failure is EIO, however EACCES
18329 	 * seems more appropriate for a reservation conflict.
18330 	 */
18331 	sd_retry_command(un, bp, SD_RETRIES_STANDARD, NULL, NULL, EIO,
18332 	    (clock_t)2, NULL);
18333 }
18334 
18335 
18336 
18337 /*
18338  *    Function: sd_pkt_status_qfull
18339  *
18340  * Description: Handle a QUEUE FULL condition from the target.  This can
18341  *		occur if the HBA does not handle the queue full condition.
18342  *		(Basically this means third-party HBAs as Sun HBAs will
18343  *		handle the queue full condition.)  Note that if there are
18344  *		some commands already in the transport, then the queue full
18345  *		has occurred because the queue for this nexus is actually
18346  *		full. If there are no commands in the transport, then the
18347  *		queue full is resulting from some other initiator or lun
18348  *		consuming all the resources at the target.
18349  *
18350  *     Context: May be called from interrupt context
18351  */
18352 
18353 static void
18354 sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
18355 	struct sd_xbuf *xp, struct scsi_pkt *pktp)
18356 {
18357 	ASSERT(un != NULL);
18358 	ASSERT(mutex_owned(SD_MUTEX(un)));
18359 	ASSERT(bp != NULL);
18360 	ASSERT(xp != NULL);
18361 	ASSERT(pktp != NULL);
18362 
18363 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18364 	    "sd_pkt_status_qfull: entry\n");
18365 
18366 	/*
18367 	 * Just lower the QFULL throttle and retry the command.  Note that
18368 	 * we do not limit the number of retries here.
18369 	 */
18370 	sd_reduce_throttle(un, SD_THROTTLE_QFULL);
18371 	sd_retry_command(un, bp, SD_RETRIES_NOCHECK, NULL, NULL, 0,
18372 	    SD_RESTART_TIMEOUT, NULL);
18373 
18374 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18375 	    "sd_pkt_status_qfull: exit\n");
18376 }
18377 
18378 
18379 /*
18380  *    Function: sd_reset_target
18381  *
18382  * Description: Issue a scsi_reset(9F), with either RESET_LUN,
18383  *		RESET_TARGET, or RESET_ALL.
18384  *
18385  *     Context: May be called under interrupt context.
18386  */
18387 
18388 static void
18389 sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp)
18390 {
18391 	int rval = 0;
18392 
18393 	ASSERT(un != NULL);
18394 	ASSERT(mutex_owned(SD_MUTEX(un)));
18395 	ASSERT(pktp != NULL);
18396 
18397 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: entry\n");
18398 
18399 	/*
18400 	 * No need to reset if the transport layer has already done so.
18401 	 */
18402 	if ((pktp->pkt_statistics &
18403 	    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) != 0) {
18404 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18405 		    "sd_reset_target: no reset\n");
18406 		return;
18407 	}
18408 
18409 	mutex_exit(SD_MUTEX(un));
18410 
18411 	if (un->un_f_allow_bus_device_reset == TRUE) {
18412 		if (un->un_f_lun_reset_enabled == TRUE) {
18413 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18414 			    "sd_reset_target: RESET_LUN\n");
18415 			rval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
18416 		}
18417 		if (rval == 0) {
18418 			SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18419 			    "sd_reset_target: RESET_TARGET\n");
18420 			rval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
18421 		}
18422 	}
18423 
18424 	if (rval == 0) {
18425 		SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
18426 		    "sd_reset_target: RESET_ALL\n");
18427 		(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
18428 	}
18429 
18430 	mutex_enter(SD_MUTEX(un));
18431 
18432 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un, "sd_reset_target: exit\n");
18433 }
18434 
18435 
18436 /*
18437  *    Function: sd_media_change_task
18438  *
18439  * Description: Recovery action for CDROM to become available.
18440  *
18441  *     Context: Executes in a taskq() thread context
18442  */
18443 
18444 static void
18445 sd_media_change_task(void *arg)
18446 {
18447 	struct	scsi_pkt	*pktp = arg;
18448 	struct	sd_lun		*un;
18449 	struct	buf		*bp;
18450 	struct	sd_xbuf		*xp;
18451 	int	err		= 0;
18452 	int	retry_count	= 0;
18453 	int	retry_limit	= SD_UNIT_ATTENTION_RETRY/10;
18454 	struct	sd_sense_info	si;
18455 
18456 	ASSERT(pktp != NULL);
18457 	bp = (struct buf *)pktp->pkt_private;
18458 	ASSERT(bp != NULL);
18459 	xp = SD_GET_XBUF(bp);
18460 	ASSERT(xp != NULL);
18461 	un = SD_GET_UN(bp);
18462 	ASSERT(un != NULL);
18463 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18464 	ASSERT(ISREMOVABLE(un));
18465 
18466 	si.ssi_severity = SCSI_ERR_INFO;
18467 	si.ssi_pfa_flag = FALSE;
18468 
18469 	/*
18470 	 * When a reset is issued on a CDROM, it takes a long time to
18471 	 * recover. First few attempts to read capacity and other things
18472 	 * related to handling unit attention fail (with a ASC 0x4 and
18473 	 * ASCQ 0x1). In that case we want to do enough retries and we want
18474 	 * to limit the retries in other cases of genuine failures like
18475 	 * no media in drive.
18476 	 */
18477 	while (retry_count++ < retry_limit) {
18478 		if ((err = sd_handle_mchange(un)) == 0) {
18479 			break;
18480 		}
18481 		if (err == EAGAIN) {
18482 			retry_limit = SD_UNIT_ATTENTION_RETRY;
18483 		}
18484 		/* Sleep for 0.5 sec. & try again */
18485 		delay(drv_usectohz(500000));
18486 	}
18487 
18488 	/*
18489 	 * Dispatch (retry or fail) the original command here,
18490 	 * along with appropriate console messages....
18491 	 *
18492 	 * Must grab the mutex before calling sd_retry_command,
18493 	 * sd_print_sense_msg and sd_return_failed_command.
18494 	 */
18495 	mutex_enter(SD_MUTEX(un));
18496 	if (err != SD_CMD_SUCCESS) {
18497 		SD_UPDATE_ERRSTATS(un, sd_harderrs);
18498 		SD_UPDATE_ERRSTATS(un, sd_rq_nodev_err);
18499 		si.ssi_severity = SCSI_ERR_FATAL;
18500 		sd_print_sense_msg(un, bp, &si, SD_NO_RETRY_ISSUED);
18501 		sd_return_failed_command(un, bp, EIO);
18502 	} else {
18503 		sd_retry_command(un, bp, SD_RETRIES_NOCHECK, sd_print_sense_msg,
18504 		    &si, EIO, (clock_t)0, NULL);
18505 	}
18506 	mutex_exit(SD_MUTEX(un));
18507 }
18508 
18509 
18510 
18511 /*
18512  *    Function: sd_handle_mchange
18513  *
18514  * Description: Perform geometry validation & other recovery when CDROM
18515  *		has been removed from drive.
18516  *
18517  * Return Code: 0 for success
18518  *		errno-type return code of either sd_send_scsi_DOORLOCK() or
18519  *		sd_send_scsi_READ_CAPACITY()
18520  *
18521  *     Context: Executes in a taskq() thread context
18522  */
18523 
18524 static int
18525 sd_handle_mchange(struct sd_lun *un)
18526 {
18527 	uint64_t	capacity;
18528 	uint32_t	lbasize;
18529 	int		rval;
18530 
18531 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18532 	ASSERT(ISREMOVABLE(un));
18533 
18534 	if ((rval = sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
18535 	    SD_PATH_DIRECT_PRIORITY)) != 0) {
18536 		return (rval);
18537 	}
18538 
18539 	mutex_enter(SD_MUTEX(un));
18540 	sd_update_block_info(un, lbasize, capacity);
18541 
18542 	if (un->un_errstats != NULL) {
18543 		struct	sd_errstats *stp =
18544 		    (struct sd_errstats *)un->un_errstats->ks_data;
18545 		stp->sd_capacity.value.ui64 = (uint64_t)
18546 		    ((uint64_t)un->un_blockcount *
18547 		    (uint64_t)un->un_tgt_blocksize);
18548 	}
18549 
18550 	/*
18551 	 * Note: Maybe let the strategy/partitioning chain worry about getting
18552 	 * valid geometry.
18553 	 */
18554 	un->un_f_geometry_is_valid = FALSE;
18555 	(void) sd_validate_geometry(un, SD_PATH_DIRECT_PRIORITY);
18556 	if (un->un_f_geometry_is_valid == FALSE) {
18557 		mutex_exit(SD_MUTEX(un));
18558 		return (EIO);
18559 	}
18560 
18561 	mutex_exit(SD_MUTEX(un));
18562 
18563 	/*
18564 	 * Try to lock the door
18565 	 */
18566 	return (sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
18567 	    SD_PATH_DIRECT_PRIORITY));
18568 }
18569 
18570 
18571 /*
18572  *    Function: sd_send_scsi_DOORLOCK
18573  *
18574  * Description: Issue the scsi DOOR LOCK command
18575  *
18576  *   Arguments: un    - pointer to driver soft state (unit) structure for
18577  *			this target.
18578  *		flag  - SD_REMOVAL_ALLOW
18579  *			SD_REMOVAL_PREVENT
18580  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
18581  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
18582  *			to use the USCSI "direct" chain and bypass the normal
18583  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
18584  *			command is issued as part of an error recovery action.
18585  *
18586  * Return Code: 0   - Success
18587  *		errno return code from sd_send_scsi_cmd()
18588  *
18589  *     Context: Can sleep.
18590  */
18591 
18592 static int
18593 sd_send_scsi_DOORLOCK(struct sd_lun *un, int flag, int path_flag)
18594 {
18595 	uchar_t			cdb_buf[CDB_GROUP0];
18596 	struct uscsi_cmd	ucmd_buf;
18597 	struct scsi_extended_sense	sense_buf;
18598 	int			status;
18599 
18600 	ASSERT(un != NULL);
18601 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18602 
18603 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_DOORLOCK: entry: un:0x%p\n", un);
18604 
18605 	/* already determined doorlock is not supported, fake success */
18606 	if (un->un_f_doorlock_supported == FALSE) {
18607 		return (0);
18608 	}
18609 
18610 	bzero(cdb_buf, sizeof (cdb_buf));
18611 	bzero(&ucmd_buf, sizeof (ucmd_buf));
18612 
18613 	cdb_buf[0] = SCMD_DOORLOCK;
18614 	cdb_buf[4] = (uchar_t)flag;
18615 
18616 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
18617 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
18618 	ucmd_buf.uscsi_bufaddr	= NULL;
18619 	ucmd_buf.uscsi_buflen	= 0;
18620 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
18621 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
18622 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
18623 	ucmd_buf.uscsi_timeout	= 15;
18624 
18625 	SD_TRACE(SD_LOG_IO, un,
18626 	    "sd_send_scsi_DOORLOCK: returning sd_send_scsi_cmd()\n");
18627 
18628 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
18629 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
18630 
18631 	if ((status == EIO) && (ucmd_buf.uscsi_status == STATUS_CHECK) &&
18632 	    (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
18633 	    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
18634 		/* fake success and skip subsequent doorlock commands */
18635 		un->un_f_doorlock_supported = FALSE;
18636 		return (0);
18637 	}
18638 
18639 	return (status);
18640 }
18641 
18642 
18643 /*
18644  *    Function: sd_send_scsi_READ_CAPACITY
18645  *
18646  * Description: This routine uses the scsi READ CAPACITY command to determine
18647  *		the device capacity in number of blocks and the device native
18648  *		block size. If this function returns a failure, then the
18649  *		values in *capp and *lbap are undefined.  If the capacity
18650  *		returned is 0xffffffff then the lun is too large for a
18651  *		normal READ CAPACITY command and the results of a
18652  *		READ CAPACITY 16 will be used instead.
18653  *
18654  *   Arguments: un   - ptr to soft state struct for the target
18655  *		capp - ptr to unsigned 64-bit variable to receive the
18656  *			capacity value from the command.
18657  *		lbap - ptr to unsigned 32-bit varaible to receive the
18658  *			block size value from the command
18659  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
18660  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
18661  *			to use the USCSI "direct" chain and bypass the normal
18662  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
18663  *			command is issued as part of an error recovery action.
18664  *
18665  * Return Code: 0   - Success
18666  *		EIO - IO error
18667  *		EACCES - Reservation conflict detected
18668  *		EAGAIN - Device is becoming ready
18669  *		errno return code from sd_send_scsi_cmd()
18670  *
18671  *     Context: Can sleep.  Blocks until command completes.
18672  */
18673 
18674 #define	SD_CAPACITY_SIZE	sizeof (struct scsi_capacity)
18675 
18676 static int
18677 sd_send_scsi_READ_CAPACITY(struct sd_lun *un, uint64_t *capp, uint32_t *lbap,
18678 	int path_flag)
18679 {
18680 	struct	scsi_extended_sense	sense_buf;
18681 	struct	uscsi_cmd	ucmd_buf;
18682 	uchar_t			cdb_buf[CDB_GROUP1];
18683 	uint32_t		*capacity_buf;
18684 	uint64_t		capacity;
18685 	uint32_t		lbasize;
18686 	int			status;
18687 
18688 	ASSERT(un != NULL);
18689 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18690 	ASSERT(capp != NULL);
18691 	ASSERT(lbap != NULL);
18692 
18693 	SD_TRACE(SD_LOG_IO, un,
18694 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
18695 
18696 	/*
18697 	 * First send a READ_CAPACITY command to the target.
18698 	 * (This command is mandatory under SCSI-2.)
18699 	 *
18700 	 * Set up the CDB for the READ_CAPACITY command.  The Partial
18701 	 * Medium Indicator bit is cleared.  The address field must be
18702 	 * zero if the PMI bit is zero.
18703 	 */
18704 	bzero(cdb_buf, sizeof (cdb_buf));
18705 	bzero(&ucmd_buf, sizeof (ucmd_buf));
18706 
18707 	capacity_buf = kmem_zalloc(SD_CAPACITY_SIZE, KM_SLEEP);
18708 
18709 	cdb_buf[0] = SCMD_READ_CAPACITY;
18710 
18711 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
18712 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
18713 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity_buf;
18714 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_SIZE;
18715 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
18716 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
18717 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
18718 	ucmd_buf.uscsi_timeout	= 60;
18719 
18720 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
18721 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
18722 
18723 	switch (status) {
18724 	case 0:
18725 		/* Return failure if we did not get valid capacity data. */
18726 		if (ucmd_buf.uscsi_resid != 0) {
18727 			kmem_free(capacity_buf, SD_CAPACITY_SIZE);
18728 			return (EIO);
18729 		}
18730 
18731 		/*
18732 		 * Read capacity and block size from the READ CAPACITY 10 data.
18733 		 * This data may be adjusted later due to device specific
18734 		 * issues.
18735 		 *
18736 		 * According to the SCSI spec, the READ CAPACITY 10
18737 		 * command returns the following:
18738 		 *
18739 		 *  bytes 0-3: Maximum logical block address available.
18740 		 *		(MSB in byte:0 & LSB in byte:3)
18741 		 *
18742 		 *  bytes 4-7: Block length in bytes
18743 		 *		(MSB in byte:4 & LSB in byte:7)
18744 		 *
18745 		 */
18746 		capacity = BE_32(capacity_buf[0]);
18747 		lbasize = BE_32(capacity_buf[1]);
18748 
18749 		/*
18750 		 * Done with capacity_buf
18751 		 */
18752 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
18753 
18754 		/*
18755 		 * if the reported capacity is set to all 0xf's, then
18756 		 * this disk is too large and requires SBC-2 commands.
18757 		 * Reissue the request using READ CAPACITY 16.
18758 		 */
18759 		if (capacity == 0xffffffff) {
18760 			status = sd_send_scsi_READ_CAPACITY_16(un, &capacity,
18761 			    &lbasize, path_flag);
18762 			if (status != 0) {
18763 				return (status);
18764 			}
18765 		}
18766 		break;	/* Success! */
18767 	case EIO:
18768 		switch (ucmd_buf.uscsi_status) {
18769 		case STATUS_RESERVATION_CONFLICT:
18770 			status = EACCES;
18771 			break;
18772 		case STATUS_CHECK:
18773 			/*
18774 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
18775 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
18776 			 */
18777 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
18778 			    (sense_buf.es_add_code  == 0x04) &&
18779 			    (sense_buf.es_qual_code == 0x01)) {
18780 				kmem_free(capacity_buf, SD_CAPACITY_SIZE);
18781 				return (EAGAIN);
18782 			}
18783 			break;
18784 		default:
18785 			break;
18786 		}
18787 		/* FALLTHRU */
18788 	default:
18789 		kmem_free(capacity_buf, SD_CAPACITY_SIZE);
18790 		return (status);
18791 	}
18792 
18793 	/*
18794 	 * Some ATAPI CD-ROM drives report inaccurate LBA size values
18795 	 * (2352 and 0 are common) so for these devices always force the value
18796 	 * to 2048 as required by the ATAPI specs.
18797 	 */
18798 	if ((un->un_f_cfg_is_atapi == TRUE) && (ISCD(un))) {
18799 		lbasize = 2048;
18800 	}
18801 
18802 	/*
18803 	 * Get the maximum LBA value from the READ CAPACITY data.
18804 	 * Here we assume that the Partial Medium Indicator (PMI) bit
18805 	 * was cleared when issuing the command. This means that the LBA
18806 	 * returned from the device is the LBA of the last logical block
18807 	 * on the logical unit.  The actual logical block count will be
18808 	 * this value plus one.
18809 	 *
18810 	 * Currently the capacity is saved in terms of un->un_sys_blocksize,
18811 	 * so scale the capacity value to reflect this.
18812 	 */
18813 	capacity = (capacity + 1) * (lbasize / un->un_sys_blocksize);
18814 
18815 #if defined(__i386) || defined(__amd64)
18816 	/*
18817 	 * On x86, compensate for off-by-1 error (number of sectors on
18818 	 * media)  (1175930)
18819 	 */
18820 	if (!ISREMOVABLE(un) && (lbasize == un->un_sys_blocksize)) {
18821 		capacity -= 1;
18822 	}
18823 #endif
18824 
18825 	/*
18826 	 * Copy the values from the READ CAPACITY command into the space
18827 	 * provided by the caller.
18828 	 */
18829 	*capp = capacity;
18830 	*lbap = lbasize;
18831 
18832 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY: "
18833 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
18834 
18835 	/*
18836 	 * Both the lbasize and capacity from the device must be nonzero,
18837 	 * otherwise we assume that the values are not valid and return
18838 	 * failure to the caller. (4203735)
18839 	 */
18840 	if ((capacity == 0) || (lbasize == 0)) {
18841 		return (EIO);
18842 	}
18843 
18844 	return (0);
18845 }
18846 
18847 /*
18848  *    Function: sd_send_scsi_READ_CAPACITY_16
18849  *
18850  * Description: This routine uses the scsi READ CAPACITY 16 command to
18851  *		determine the device capacity in number of blocks and the
18852  *		device native block size.  If this function returns a failure,
18853  *		then the values in *capp and *lbap are undefined.
18854  *		This routine should always be called by
18855  *		sd_send_scsi_READ_CAPACITY which will appy any device
18856  *		specific adjustments to capacity and lbasize.
18857  *
18858  *   Arguments: un   - ptr to soft state struct for the target
18859  *		capp - ptr to unsigned 64-bit variable to receive the
18860  *			capacity value from the command.
18861  *		lbap - ptr to unsigned 32-bit varaible to receive the
18862  *			block size value from the command
18863  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
18864  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
18865  *			to use the USCSI "direct" chain and bypass the normal
18866  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when
18867  *			this command is issued as part of an error recovery
18868  *			action.
18869  *
18870  * Return Code: 0   - Success
18871  *		EIO - IO error
18872  *		EACCES - Reservation conflict detected
18873  *		EAGAIN - Device is becoming ready
18874  *		errno return code from sd_send_scsi_cmd()
18875  *
18876  *     Context: Can sleep.  Blocks until command completes.
18877  */
18878 
18879 #define	SD_CAPACITY_16_SIZE	sizeof (struct scsi_capacity_16)
18880 
18881 static int
18882 sd_send_scsi_READ_CAPACITY_16(struct sd_lun *un, uint64_t *capp,
18883 	uint32_t *lbap, int path_flag)
18884 {
18885 	struct	scsi_extended_sense	sense_buf;
18886 	struct	uscsi_cmd	ucmd_buf;
18887 	uchar_t			cdb_buf[CDB_GROUP4];
18888 	uint64_t		*capacity16_buf;
18889 	uint64_t		capacity;
18890 	uint32_t		lbasize;
18891 	int			status;
18892 
18893 	ASSERT(un != NULL);
18894 	ASSERT(!mutex_owned(SD_MUTEX(un)));
18895 	ASSERT(capp != NULL);
18896 	ASSERT(lbap != NULL);
18897 
18898 	SD_TRACE(SD_LOG_IO, un,
18899 	    "sd_send_scsi_READ_CAPACITY: entry: un:0x%p\n", un);
18900 
18901 	/*
18902 	 * First send a READ_CAPACITY_16 command to the target.
18903 	 *
18904 	 * Set up the CDB for the READ_CAPACITY_16 command.  The Partial
18905 	 * Medium Indicator bit is cleared.  The address field must be
18906 	 * zero if the PMI bit is zero.
18907 	 */
18908 	bzero(cdb_buf, sizeof (cdb_buf));
18909 	bzero(&ucmd_buf, sizeof (ucmd_buf));
18910 
18911 	capacity16_buf = kmem_zalloc(SD_CAPACITY_16_SIZE, KM_SLEEP);
18912 
18913 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
18914 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
18915 	ucmd_buf.uscsi_bufaddr	= (caddr_t)capacity16_buf;
18916 	ucmd_buf.uscsi_buflen	= SD_CAPACITY_16_SIZE;
18917 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
18918 	ucmd_buf.uscsi_rqlen	= sizeof (sense_buf);
18919 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
18920 	ucmd_buf.uscsi_timeout	= 60;
18921 
18922 	/*
18923 	 * Read Capacity (16) is a Service Action In command.  One
18924 	 * command byte (0x9E) is overloaded for multiple operations,
18925 	 * with the second CDB byte specifying the desired operation
18926 	 */
18927 	cdb_buf[0] = SCMD_SVC_ACTION_IN_G4;
18928 	cdb_buf[1] = SSVC_ACTION_READ_CAPACITY_G4;
18929 
18930 	/*
18931 	 * Fill in allocation length field
18932 	 */
18933 	cdb_buf[10] = (uchar_t)((ucmd_buf.uscsi_buflen & 0xff000000) >> 24);
18934 	cdb_buf[11] = (uchar_t)((ucmd_buf.uscsi_buflen & 0x00ff0000) >> 16);
18935 	cdb_buf[12] = (uchar_t)((ucmd_buf.uscsi_buflen & 0x0000ff00) >> 8);
18936 	cdb_buf[13] = (uchar_t)(ucmd_buf.uscsi_buflen & 0x000000ff);
18937 
18938 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
18939 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
18940 
18941 	switch (status) {
18942 	case 0:
18943 		/* Return failure if we did not get valid capacity data. */
18944 		if (ucmd_buf.uscsi_resid > 20) {
18945 			kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
18946 			return (EIO);
18947 		}
18948 
18949 		/*
18950 		 * Read capacity and block size from the READ CAPACITY 10 data.
18951 		 * This data may be adjusted later due to device specific
18952 		 * issues.
18953 		 *
18954 		 * According to the SCSI spec, the READ CAPACITY 10
18955 		 * command returns the following:
18956 		 *
18957 		 *  bytes 0-7: Maximum logical block address available.
18958 		 *		(MSB in byte:0 & LSB in byte:7)
18959 		 *
18960 		 *  bytes 8-11: Block length in bytes
18961 		 *		(MSB in byte:8 & LSB in byte:11)
18962 		 *
18963 		 */
18964 		capacity = BE_64(capacity16_buf[0]);
18965 		lbasize = BE_32(*(uint32_t *)&capacity16_buf[1]);
18966 
18967 		/*
18968 		 * Done with capacity16_buf
18969 		 */
18970 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
18971 
18972 		/*
18973 		 * if the reported capacity is set to all 0xf's, then
18974 		 * this disk is too large.  This could only happen with
18975 		 * a device that supports LBAs larger than 64 bits which
18976 		 * are not defined by any current T10 standards.
18977 		 */
18978 		if (capacity == 0xffffffffffffffff) {
18979 			return (EIO);
18980 		}
18981 		break;	/* Success! */
18982 	case EIO:
18983 		switch (ucmd_buf.uscsi_status) {
18984 		case STATUS_RESERVATION_CONFLICT:
18985 			status = EACCES;
18986 			break;
18987 		case STATUS_CHECK:
18988 			/*
18989 			 * Check condition; look for ASC/ASCQ of 0x04/0x01
18990 			 * (LOGICAL UNIT IS IN PROCESS OF BECOMING READY)
18991 			 */
18992 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
18993 			    (sense_buf.es_add_code  == 0x04) &&
18994 			    (sense_buf.es_qual_code == 0x01)) {
18995 				kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
18996 				return (EAGAIN);
18997 			}
18998 			break;
18999 		default:
19000 			break;
19001 		}
19002 		/* FALLTHRU */
19003 	default:
19004 		kmem_free(capacity16_buf, SD_CAPACITY_16_SIZE);
19005 		return (status);
19006 	}
19007 
19008 	*capp = capacity;
19009 	*lbap = lbasize;
19010 
19011 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_READ_CAPACITY_16: "
19012 	    "capacity:0x%llx  lbasize:0x%x\n", capacity, lbasize);
19013 
19014 	return (0);
19015 }
19016 
19017 
19018 /*
19019  *    Function: sd_send_scsi_START_STOP_UNIT
19020  *
19021  * Description: Issue a scsi START STOP UNIT command to the target.
19022  *
19023  *   Arguments: un    - pointer to driver soft state (unit) structure for
19024  *			this target.
19025  *		flag  - SD_TARGET_START
19026  *			SD_TARGET_STOP
19027  *			SD_TARGET_EJECT
19028  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19029  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19030  *			to use the USCSI "direct" chain and bypass the normal
19031  *			command waitq. SD_PATH_DIRECT_PRIORITY is used when this
19032  *			command is issued as part of an error recovery action.
19033  *
19034  * Return Code: 0   - Success
19035  *		EIO - IO error
19036  *		EACCES - Reservation conflict detected
19037  *		ENXIO  - Not Ready, medium not present
19038  *		errno return code from sd_send_scsi_cmd()
19039  *
19040  *     Context: Can sleep.
19041  */
19042 
19043 static int
19044 sd_send_scsi_START_STOP_UNIT(struct sd_lun *un, int flag, int path_flag)
19045 {
19046 	struct	scsi_extended_sense	sense_buf;
19047 	uchar_t			cdb_buf[CDB_GROUP0];
19048 	struct uscsi_cmd	ucmd_buf;
19049 	int			status;
19050 
19051 	ASSERT(un != NULL);
19052 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19053 
19054 	SD_TRACE(SD_LOG_IO, un,
19055 	    "sd_send_scsi_START_STOP_UNIT: entry: un:0x%p\n", un);
19056 
19057 	if (ISREMOVABLE(un) &&
19058 	    ((flag == SD_TARGET_START) || (flag == SD_TARGET_STOP)) &&
19059 	    (un->un_f_start_stop_supported != TRUE)) {
19060 		return (0);
19061 	}
19062 
19063 	bzero(cdb_buf, sizeof (cdb_buf));
19064 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19065 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19066 
19067 	cdb_buf[0] = SCMD_START_STOP;
19068 	cdb_buf[4] |= (uchar_t)flag;
19069 
19070 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
19071 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
19072 	ucmd_buf.uscsi_bufaddr	= NULL;
19073 	ucmd_buf.uscsi_buflen	= 0;
19074 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19075 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19076 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19077 	ucmd_buf.uscsi_timeout	= 200;
19078 
19079 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19080 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19081 
19082 	switch (status) {
19083 	case 0:
19084 		break;	/* Success! */
19085 	case EIO:
19086 		switch (ucmd_buf.uscsi_status) {
19087 		case STATUS_RESERVATION_CONFLICT:
19088 			status = EACCES;
19089 			break;
19090 		case STATUS_CHECK:
19091 			if (ucmd_buf.uscsi_rqstatus == STATUS_GOOD) {
19092 				switch (sense_buf.es_key) {
19093 				case KEY_ILLEGAL_REQUEST:
19094 					status = ENOTSUP;
19095 					break;
19096 				case KEY_NOT_READY:
19097 					if (sense_buf.es_add_code == 0x3A) {
19098 						status = ENXIO;
19099 					}
19100 					break;
19101 				default:
19102 					break;
19103 				}
19104 			}
19105 			break;
19106 		default:
19107 			break;
19108 		}
19109 		break;
19110 	default:
19111 		break;
19112 	}
19113 
19114 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_START_STOP_UNIT: exit\n");
19115 
19116 	return (status);
19117 }
19118 
19119 
19120 /*
19121  *    Function: sd_start_stop_unit_callback
19122  *
19123  * Description: timeout(9F) callback to begin recovery process for a
19124  *		device that has spun down.
19125  *
19126  *   Arguments: arg - pointer to associated softstate struct.
19127  *
19128  *     Context: Executes in a timeout(9F) thread context
19129  */
19130 
19131 static void
19132 sd_start_stop_unit_callback(void *arg)
19133 {
19134 	struct sd_lun	*un = arg;
19135 	ASSERT(un != NULL);
19136 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19137 
19138 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_callback: entry\n");
19139 
19140 	(void) taskq_dispatch(sd_tq, sd_start_stop_unit_task, un, KM_NOSLEEP);
19141 }
19142 
19143 
19144 /*
19145  *    Function: sd_start_stop_unit_task
19146  *
19147  * Description: Recovery procedure when a drive is spun down.
19148  *
19149  *   Arguments: arg - pointer to associated softstate struct.
19150  *
19151  *     Context: Executes in a taskq() thread context
19152  */
19153 
19154 static void
19155 sd_start_stop_unit_task(void *arg)
19156 {
19157 	struct sd_lun	*un = arg;
19158 
19159 	ASSERT(un != NULL);
19160 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19161 
19162 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: entry\n");
19163 
19164 	/*
19165 	 * Some unformatted drives report not ready error, no need to
19166 	 * restart if format has been initiated.
19167 	 */
19168 	mutex_enter(SD_MUTEX(un));
19169 	if (un->un_f_format_in_progress == TRUE) {
19170 		mutex_exit(SD_MUTEX(un));
19171 		return;
19172 	}
19173 	mutex_exit(SD_MUTEX(un));
19174 
19175 	/*
19176 	 * When a START STOP command is issued from here, it is part of a
19177 	 * failure recovery operation and must be issued before any other
19178 	 * commands, including any pending retries. Thus it must be sent
19179 	 * using SD_PATH_DIRECT_PRIORITY. It doesn't matter if the spin up
19180 	 * succeeds or not, we will start I/O after the attempt.
19181 	 */
19182 	(void) sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
19183 	    SD_PATH_DIRECT_PRIORITY);
19184 
19185 	/*
19186 	 * The above call blocks until the START_STOP_UNIT command completes.
19187 	 * Now that it has completed, we must re-try the original IO that
19188 	 * received the NOT READY condition in the first place. There are
19189 	 * three possible conditions here:
19190 	 *
19191 	 *  (1) The original IO is on un_retry_bp.
19192 	 *  (2) The original IO is on the regular wait queue, and un_retry_bp
19193 	 *	is NULL.
19194 	 *  (3) The original IO is on the regular wait queue, and un_retry_bp
19195 	 *	points to some other, unrelated bp.
19196 	 *
19197 	 * For each case, we must call sd_start_cmds() with un_retry_bp
19198 	 * as the argument. If un_retry_bp is NULL, this will initiate
19199 	 * processing of the regular wait queue.  If un_retry_bp is not NULL,
19200 	 * then this will process the bp on un_retry_bp. That may or may not
19201 	 * be the original IO, but that does not matter: the important thing
19202 	 * is to keep the IO processing going at this point.
19203 	 *
19204 	 * Note: This is a very specific error recovery sequence associated
19205 	 * with a drive that is not spun up. We attempt a START_STOP_UNIT and
19206 	 * serialize the I/O with completion of the spin-up.
19207 	 */
19208 	mutex_enter(SD_MUTEX(un));
19209 	SD_TRACE(SD_LOG_IO_CORE | SD_LOG_ERROR, un,
19210 	    "sd_start_stop_unit_task: un:0x%p starting bp:0x%p\n",
19211 	    un, un->un_retry_bp);
19212 	un->un_startstop_timeid = NULL;	/* Timeout is no longer pending */
19213 	sd_start_cmds(un, un->un_retry_bp);
19214 	mutex_exit(SD_MUTEX(un));
19215 
19216 	SD_TRACE(SD_LOG_IO, un, "sd_start_stop_unit_task: exit\n");
19217 }
19218 
19219 
19220 /*
19221  *    Function: sd_send_scsi_INQUIRY
19222  *
19223  * Description: Issue the scsi INQUIRY command.
19224  *
19225  *   Arguments: un
19226  *		bufaddr
19227  *		buflen
19228  *		evpd
19229  *		page_code
19230  *		page_length
19231  *
19232  * Return Code: 0   - Success
19233  *		errno return code from sd_send_scsi_cmd()
19234  *
19235  *     Context: Can sleep. Does not return until command is completed.
19236  */
19237 
19238 static int
19239 sd_send_scsi_INQUIRY(struct sd_lun *un, uchar_t *bufaddr, size_t buflen,
19240 	uchar_t evpd, uchar_t page_code, size_t *residp)
19241 {
19242 	uchar_t			cdb_buf[CDB_GROUP0];
19243 	struct uscsi_cmd	ucmd_buf;
19244 	int			status;
19245 
19246 	ASSERT(un != NULL);
19247 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19248 	ASSERT(bufaddr != NULL);
19249 
19250 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: entry: un:0x%p\n", un);
19251 
19252 	bzero(cdb_buf, sizeof (cdb_buf));
19253 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19254 	bzero(bufaddr, buflen);
19255 
19256 	cdb_buf[0] = SCMD_INQUIRY;
19257 	cdb_buf[1] = evpd;
19258 	cdb_buf[2] = page_code;
19259 	cdb_buf[4] = buflen;
19260 
19261 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
19262 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
19263 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
19264 	ucmd_buf.uscsi_buflen	= buflen;
19265 	ucmd_buf.uscsi_rqbuf	= NULL;
19266 	ucmd_buf.uscsi_rqlen	= 0;
19267 	ucmd_buf.uscsi_flags	= USCSI_READ | USCSI_SILENT;
19268 	ucmd_buf.uscsi_timeout	= 200;	/* Excessive legacy value */
19269 
19270 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19271 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT);
19272 
19273 	if ((status == 0) && (residp != NULL)) {
19274 		*residp = ucmd_buf.uscsi_resid;
19275 	}
19276 
19277 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_INQUIRY: exit\n");
19278 
19279 	return (status);
19280 }
19281 
19282 
19283 /*
19284  *    Function: sd_send_scsi_TEST_UNIT_READY
19285  *
19286  * Description: Issue the scsi TEST UNIT READY command.
19287  *		This routine can be told to set the flag USCSI_DIAGNOSE to
19288  *		prevent retrying failed commands. Use this when the intent
19289  *		is either to check for device readiness, to clear a Unit
19290  *		Attention, or to clear any outstanding sense data.
19291  *		However under specific conditions the expected behavior
19292  *		is for retries to bring a device ready, so use the flag
19293  *		with caution.
19294  *
19295  *   Arguments: un
19296  *		flag:   SD_CHECK_FOR_MEDIA: return ENXIO if no media present
19297  *			SD_DONT_RETRY_TUR: include uscsi flag USCSI_DIAGNOSE.
19298  *			0: dont check for media present, do retries on cmd.
19299  *
19300  * Return Code: 0   - Success
19301  *		EIO - IO error
19302  *		EACCES - Reservation conflict detected
19303  *		ENXIO  - Not Ready, medium not present
19304  *		errno return code from sd_send_scsi_cmd()
19305  *
19306  *     Context: Can sleep. Does not return until command is completed.
19307  */
19308 
19309 static int
19310 sd_send_scsi_TEST_UNIT_READY(struct sd_lun *un, int flag)
19311 {
19312 	struct	scsi_extended_sense	sense_buf;
19313 	uchar_t			cdb_buf[CDB_GROUP0];
19314 	struct uscsi_cmd	ucmd_buf;
19315 	int			status;
19316 
19317 	ASSERT(un != NULL);
19318 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19319 
19320 	SD_TRACE(SD_LOG_IO, un,
19321 	    "sd_send_scsi_TEST_UNIT_READY: entry: un:0x%p\n", un);
19322 
19323 	/*
19324 	 * Some Seagate elite1 TQ devices get hung with disconnect/reconnect
19325 	 * timeouts when they receive a TUR and the queue is not empty. Check
19326 	 * the configuration flag set during attach (indicating the drive has
19327 	 * this firmware bug) and un_ncmds_in_transport before issuing the
19328 	 * TUR. If there are
19329 	 * pending commands return success, this is a bit arbitrary but is ok
19330 	 * for non-removables (i.e. the eliteI disks) and non-clustering
19331 	 * configurations.
19332 	 */
19333 	if (un->un_f_cfg_tur_check == TRUE) {
19334 		mutex_enter(SD_MUTEX(un));
19335 		if (un->un_ncmds_in_transport != 0) {
19336 			mutex_exit(SD_MUTEX(un));
19337 			return (0);
19338 		}
19339 		mutex_exit(SD_MUTEX(un));
19340 	}
19341 
19342 	bzero(cdb_buf, sizeof (cdb_buf));
19343 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19344 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19345 
19346 	cdb_buf[0] = SCMD_TEST_UNIT_READY;
19347 
19348 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
19349 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
19350 	ucmd_buf.uscsi_bufaddr	= NULL;
19351 	ucmd_buf.uscsi_buflen	= 0;
19352 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19353 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19354 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19355 
19356 	/* Use flag USCSI_DIAGNOSE to prevent retries if it fails. */
19357 	if ((flag & SD_DONT_RETRY_TUR) != 0) {
19358 		ucmd_buf.uscsi_flags |= USCSI_DIAGNOSE;
19359 	}
19360 	ucmd_buf.uscsi_timeout	= 60;
19361 
19362 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19363 	    UIO_SYSSPACE, UIO_SYSSPACE,
19364 	    ((flag & SD_BYPASS_PM) ? SD_PATH_DIRECT : SD_PATH_STANDARD));
19365 
19366 	switch (status) {
19367 	case 0:
19368 		break;	/* Success! */
19369 	case EIO:
19370 		switch (ucmd_buf.uscsi_status) {
19371 		case STATUS_RESERVATION_CONFLICT:
19372 			status = EACCES;
19373 			break;
19374 		case STATUS_CHECK:
19375 			if ((flag & SD_CHECK_FOR_MEDIA) == 0) {
19376 				break;
19377 			}
19378 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19379 			    (sense_buf.es_key == KEY_NOT_READY) &&
19380 			    (sense_buf.es_add_code == 0x3A)) {
19381 				status = ENXIO;
19382 			}
19383 			break;
19384 		default:
19385 			break;
19386 		}
19387 		break;
19388 	default:
19389 		break;
19390 	}
19391 
19392 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_TEST_UNIT_READY: exit\n");
19393 
19394 	return (status);
19395 }
19396 
19397 
19398 /*
19399  *    Function: sd_send_scsi_PERSISTENT_RESERVE_IN
19400  *
19401  * Description: Issue the scsi PERSISTENT RESERVE IN command.
19402  *
19403  *   Arguments: un
19404  *
19405  * Return Code: 0   - Success
19406  *		EACCES
19407  *		ENOTSUP
19408  *		errno return code from sd_send_scsi_cmd()
19409  *
19410  *     Context: Can sleep. Does not return until command is completed.
19411  */
19412 
19413 static int
19414 sd_send_scsi_PERSISTENT_RESERVE_IN(struct sd_lun *un, uchar_t  usr_cmd,
19415 	uint16_t data_len, uchar_t *data_bufp)
19416 {
19417 	struct scsi_extended_sense	sense_buf;
19418 	struct uscsi_cmd	ucmd_buf;
19419 	uchar_t			cdb_buf[CDB_GROUP1];
19420 	int			status;
19421 	int			no_caller_buf = FALSE;
19422 
19423 	ASSERT(un != NULL);
19424 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19425 	ASSERT((usr_cmd == SD_READ_KEYS) || (usr_cmd == SD_READ_RESV));
19426 
19427 	SD_TRACE(SD_LOG_IO, un,
19428 	    "sd_send_scsi_PERSISTENT_RESERVE_IN: entry: un:0x%p\n", un);
19429 
19430 	bzero(cdb_buf, sizeof (cdb_buf));
19431 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19432 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19433 	if (data_bufp == NULL) {
19434 		/* Allocate a default buf if the caller did not give one */
19435 		ASSERT(data_len == 0);
19436 		data_len  = MHIOC_RESV_KEY_SIZE;
19437 		data_bufp = kmem_zalloc(MHIOC_RESV_KEY_SIZE, KM_SLEEP);
19438 		no_caller_buf = TRUE;
19439 	}
19440 
19441 	cdb_buf[0] = SCMD_PERSISTENT_RESERVE_IN;
19442 	cdb_buf[1] = usr_cmd;
19443 	cdb_buf[7] = (uchar_t)(data_len >> 8);
19444 	cdb_buf[8] = (uchar_t)data_len;
19445 
19446 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
19447 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
19448 	ucmd_buf.uscsi_bufaddr	= (caddr_t)data_bufp;
19449 	ucmd_buf.uscsi_buflen	= data_len;
19450 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19451 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19452 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19453 	ucmd_buf.uscsi_timeout	= 60;
19454 
19455 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19456 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
19457 
19458 	switch (status) {
19459 	case 0:
19460 		break;	/* Success! */
19461 	case EIO:
19462 		switch (ucmd_buf.uscsi_status) {
19463 		case STATUS_RESERVATION_CONFLICT:
19464 			status = EACCES;
19465 			break;
19466 		case STATUS_CHECK:
19467 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19468 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
19469 				status = ENOTSUP;
19470 			}
19471 			break;
19472 		default:
19473 			break;
19474 		}
19475 		break;
19476 	default:
19477 		break;
19478 	}
19479 
19480 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_IN: exit\n");
19481 
19482 	if (no_caller_buf == TRUE) {
19483 		kmem_free(data_bufp, data_len);
19484 	}
19485 
19486 	return (status);
19487 }
19488 
19489 
19490 /*
19491  *    Function: sd_send_scsi_PERSISTENT_RESERVE_OUT
19492  *
19493  * Description: This routine is the driver entry point for handling CD-ROM
19494  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS,
19495  *		MHIOCGRP_INRESV) by sending the SCSI-3 PROUT commands to the
19496  *		device.
19497  *
19498  *   Arguments: un  -   Pointer to soft state struct for the target.
19499  *		usr_cmd SCSI-3 reservation facility command (one of
19500  *			SD_SCSI3_REGISTER, SD_SCSI3_RESERVE, SD_SCSI3_RELEASE,
19501  *			SD_SCSI3_PREEMPTANDABORT)
19502  *		usr_bufp - user provided pointer register, reserve descriptor or
19503  *			preempt and abort structure (mhioc_register_t,
19504  *                      mhioc_resv_desc_t, mhioc_preemptandabort_t)
19505  *
19506  * Return Code: 0   - Success
19507  *		EACCES
19508  *		ENOTSUP
19509  *		errno return code from sd_send_scsi_cmd()
19510  *
19511  *     Context: Can sleep. Does not return until command is completed.
19512  */
19513 
19514 static int
19515 sd_send_scsi_PERSISTENT_RESERVE_OUT(struct sd_lun *un, uchar_t usr_cmd,
19516 	uchar_t	*usr_bufp)
19517 {
19518 	struct scsi_extended_sense	sense_buf;
19519 	struct uscsi_cmd	ucmd_buf;
19520 	uchar_t			cdb_buf[CDB_GROUP1];
19521 	int			status;
19522 	uchar_t			data_len = sizeof (sd_prout_t);
19523 	sd_prout_t		*prp;
19524 
19525 	ASSERT(un != NULL);
19526 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19527 	ASSERT(data_len == 24);	/* required by scsi spec */
19528 
19529 	SD_TRACE(SD_LOG_IO, un,
19530 	    "sd_send_scsi_PERSISTENT_RESERVE_OUT: entry: un:0x%p\n", un);
19531 
19532 	if (usr_bufp == NULL) {
19533 		return (EINVAL);
19534 	}
19535 
19536 	bzero(cdb_buf, sizeof (cdb_buf));
19537 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19538 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19539 	prp = kmem_zalloc(data_len, KM_SLEEP);
19540 
19541 	cdb_buf[0] = SCMD_PERSISTENT_RESERVE_OUT;
19542 	cdb_buf[1] = usr_cmd;
19543 	cdb_buf[8] = data_len;
19544 
19545 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
19546 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
19547 	ucmd_buf.uscsi_bufaddr	= (caddr_t)prp;
19548 	ucmd_buf.uscsi_buflen	= data_len;
19549 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19550 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19551 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
19552 	ucmd_buf.uscsi_timeout	= 60;
19553 
19554 	switch (usr_cmd) {
19555 	case SD_SCSI3_REGISTER: {
19556 		mhioc_register_t *ptr = (mhioc_register_t *)usr_bufp;
19557 
19558 		bcopy(ptr->oldkey.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19559 		bcopy(ptr->newkey.key, prp->service_key,
19560 		    MHIOC_RESV_KEY_SIZE);
19561 		prp->aptpl = ptr->aptpl;
19562 		break;
19563 	}
19564 	case SD_SCSI3_RESERVE:
19565 	case SD_SCSI3_RELEASE: {
19566 		mhioc_resv_desc_t *ptr = (mhioc_resv_desc_t *)usr_bufp;
19567 
19568 		bcopy(ptr->key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19569 		prp->scope_address = BE_32(ptr->scope_specific_addr);
19570 		cdb_buf[2] = ptr->type;
19571 		break;
19572 	}
19573 	case SD_SCSI3_PREEMPTANDABORT: {
19574 		mhioc_preemptandabort_t *ptr =
19575 		    (mhioc_preemptandabort_t *)usr_bufp;
19576 
19577 		bcopy(ptr->resvdesc.key.key, prp->res_key, MHIOC_RESV_KEY_SIZE);
19578 		bcopy(ptr->victim_key.key, prp->service_key,
19579 		    MHIOC_RESV_KEY_SIZE);
19580 		prp->scope_address = BE_32(ptr->resvdesc.scope_specific_addr);
19581 		cdb_buf[2] = ptr->resvdesc.type;
19582 		ucmd_buf.uscsi_flags |= USCSI_HEAD;
19583 		break;
19584 	}
19585 	case SD_SCSI3_REGISTERANDIGNOREKEY:
19586 	{
19587 		mhioc_registerandignorekey_t *ptr;
19588 		ptr = (mhioc_registerandignorekey_t *)usr_bufp;
19589 		bcopy(ptr->newkey.key,
19590 		    prp->service_key, MHIOC_RESV_KEY_SIZE);
19591 		prp->aptpl = ptr->aptpl;
19592 		break;
19593 	}
19594 	default:
19595 		ASSERT(FALSE);
19596 		break;
19597 	}
19598 
19599 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19600 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
19601 
19602 	switch (status) {
19603 	case 0:
19604 		break;	/* Success! */
19605 	case EIO:
19606 		switch (ucmd_buf.uscsi_status) {
19607 		case STATUS_RESERVATION_CONFLICT:
19608 			status = EACCES;
19609 			break;
19610 		case STATUS_CHECK:
19611 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19612 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
19613 				status = ENOTSUP;
19614 			}
19615 			break;
19616 		default:
19617 			break;
19618 		}
19619 		break;
19620 	default:
19621 		break;
19622 	}
19623 
19624 	kmem_free(prp, data_len);
19625 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_PERSISTENT_RESERVE_OUT: exit\n");
19626 	return (status);
19627 }
19628 
19629 
19630 /*
19631  *    Function: sd_send_scsi_SYNCHRONIZE_CACHE
19632  *
19633  * Description: Issues a scsi SYNCHRONIZE CACHE command to the target
19634  *
19635  *   Arguments: un - pointer to the target's soft state struct
19636  *
19637  * Return Code: 0 - success
19638  *		errno-type error code
19639  *
19640  *     Context: kernel thread context only.
19641  */
19642 
19643 static int
19644 sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un)
19645 {
19646 	struct	scsi_extended_sense	sense_buf;
19647 	uchar_t			cdb_buf[CDB_GROUP1];
19648 	struct uscsi_cmd	ucmd_buf;
19649 	int			status;
19650 
19651 	ASSERT(un != NULL);
19652 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19653 
19654 	SD_TRACE(SD_LOG_IO, un,
19655 	    "sd_send_scsi_SYNCHRONIZE_CACHE: entry: un:0x%p\n", un);
19656 
19657 	bzero(cdb_buf, sizeof (cdb_buf));
19658 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19659 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19660 
19661 	cdb_buf[0] = SCMD_SYNCHRONIZE_CACHE;
19662 
19663 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
19664 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
19665 	ucmd_buf.uscsi_bufaddr	= NULL;
19666 	ucmd_buf.uscsi_buflen	= 0;
19667 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19668 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19669 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_SILENT;
19670 	ucmd_buf.uscsi_timeout	= 240;
19671 
19672 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19673 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_DIRECT);
19674 
19675 	switch (status) {
19676 	case 0:
19677 		break;	/* Success! */
19678 	case EIO:
19679 		switch (ucmd_buf.uscsi_status) {
19680 		case STATUS_RESERVATION_CONFLICT:
19681 			/* Ignore reservation conflict */
19682 			status = 0;
19683 			goto done;
19684 
19685 		case STATUS_CHECK:
19686 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
19687 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST)) {
19688 				/* Ignore Illegal Request error */
19689 				status = 0;
19690 				goto done;
19691 			}
19692 			break;
19693 		default:
19694 			break;
19695 		}
19696 		/* FALLTHRU */
19697 	default:
19698 		/* Ignore error if the media is not present. */
19699 		if (sd_send_scsi_TEST_UNIT_READY(un, 0) != 0) {
19700 			status = 0;
19701 			goto done;
19702 		}
19703 		/* If we reach this, we had an error */
19704 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
19705 		    "SYNCHRONIZE CACHE command failed (%d)\n", status);
19706 		break;
19707 	}
19708 
19709 done:
19710 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_SYNCHRONIZE_CACHE: exit\n");
19711 
19712 	return (status);
19713 }
19714 
19715 
19716 /*
19717  *    Function: sd_send_scsi_GET_CONFIGURATION
19718  *
19719  * Description: Issues the get configuration command to the device.
19720  *		Called from sd_check_for_writable_cd & sd_get_media_info
19721  *		caller needs to ensure that buflen = SD_PROFILE_HEADER_LEN
19722  *   Arguments: un
19723  *		ucmdbuf
19724  *		rqbuf
19725  *		rqbuflen
19726  *		bufaddr
19727  *		buflen
19728  *
19729  * Return Code: 0   - Success
19730  *		errno return code from sd_send_scsi_cmd()
19731  *
19732  *     Context: Can sleep. Does not return until command is completed.
19733  *
19734  */
19735 
19736 static int
19737 sd_send_scsi_GET_CONFIGURATION(struct sd_lun *un, struct uscsi_cmd *ucmdbuf,
19738 	uchar_t *rqbuf, uint_t rqbuflen, uchar_t *bufaddr, uint_t buflen)
19739 {
19740 	char	cdb[CDB_GROUP1];
19741 	int	status;
19742 
19743 	ASSERT(un != NULL);
19744 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19745 	ASSERT(bufaddr != NULL);
19746 	ASSERT(ucmdbuf != NULL);
19747 	ASSERT(rqbuf != NULL);
19748 
19749 	SD_TRACE(SD_LOG_IO, un,
19750 	    "sd_send_scsi_GET_CONFIGURATION: entry: un:0x%p\n", un);
19751 
19752 	bzero(cdb, sizeof (cdb));
19753 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
19754 	bzero(rqbuf, rqbuflen);
19755 	bzero(bufaddr, buflen);
19756 
19757 	/*
19758 	 * Set up cdb field for the get configuration command.
19759 	 */
19760 	cdb[0] = SCMD_GET_CONFIGURATION;
19761 	cdb[1] = 0x02;  /* Requested Type */
19762 	cdb[8] = SD_PROFILE_HEADER_LEN;
19763 	ucmdbuf->uscsi_cdb = cdb;
19764 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
19765 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
19766 	ucmdbuf->uscsi_buflen = buflen;
19767 	ucmdbuf->uscsi_timeout = sd_io_time;
19768 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
19769 	ucmdbuf->uscsi_rqlen = rqbuflen;
19770 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
19771 
19772 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE,
19773 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
19774 
19775 	switch (status) {
19776 	case 0:
19777 		break;  /* Success! */
19778 	case EIO:
19779 		switch (ucmdbuf->uscsi_status) {
19780 		case STATUS_RESERVATION_CONFLICT:
19781 			status = EACCES;
19782 			break;
19783 		default:
19784 			break;
19785 		}
19786 		break;
19787 	default:
19788 		break;
19789 	}
19790 
19791 	if (status == 0) {
19792 		SD_DUMP_MEMORY(un, SD_LOG_IO,
19793 		    "sd_send_scsi_GET_CONFIGURATION: data",
19794 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
19795 	}
19796 
19797 	SD_TRACE(SD_LOG_IO, un,
19798 	    "sd_send_scsi_GET_CONFIGURATION: exit\n");
19799 
19800 	return (status);
19801 }
19802 
19803 /*
19804  *    Function: sd_send_scsi_feature_GET_CONFIGURATION
19805  *
19806  * Description: Issues the get configuration command to the device to
19807  *              retrieve a specfic feature. Called from
19808  *		sd_check_for_writable_cd & sd_set_mmc_caps.
19809  *   Arguments: un
19810  *              ucmdbuf
19811  *              rqbuf
19812  *              rqbuflen
19813  *              bufaddr
19814  *              buflen
19815  *		feature
19816  *
19817  * Return Code: 0   - Success
19818  *              errno return code from sd_send_scsi_cmd()
19819  *
19820  *     Context: Can sleep. Does not return until command is completed.
19821  *
19822  */
19823 static int
19824 sd_send_scsi_feature_GET_CONFIGURATION(struct sd_lun *un,
19825 	struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
19826 	uchar_t *bufaddr, uint_t buflen, char feature)
19827 {
19828 	char    cdb[CDB_GROUP1];
19829 	int	status;
19830 
19831 	ASSERT(un != NULL);
19832 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19833 	ASSERT(bufaddr != NULL);
19834 	ASSERT(ucmdbuf != NULL);
19835 	ASSERT(rqbuf != NULL);
19836 
19837 	SD_TRACE(SD_LOG_IO, un,
19838 	    "sd_send_scsi_feature_GET_CONFIGURATION: entry: un:0x%p\n", un);
19839 
19840 	bzero(cdb, sizeof (cdb));
19841 	bzero(ucmdbuf, sizeof (struct uscsi_cmd));
19842 	bzero(rqbuf, rqbuflen);
19843 	bzero(bufaddr, buflen);
19844 
19845 	/*
19846 	 * Set up cdb field for the get configuration command.
19847 	 */
19848 	cdb[0] = SCMD_GET_CONFIGURATION;
19849 	cdb[1] = 0x02;  /* Requested Type */
19850 	cdb[3] = feature;
19851 	cdb[8] = buflen;
19852 	ucmdbuf->uscsi_cdb = cdb;
19853 	ucmdbuf->uscsi_cdblen = CDB_GROUP1;
19854 	ucmdbuf->uscsi_bufaddr = (caddr_t)bufaddr;
19855 	ucmdbuf->uscsi_buflen = buflen;
19856 	ucmdbuf->uscsi_timeout = sd_io_time;
19857 	ucmdbuf->uscsi_rqbuf = (caddr_t)rqbuf;
19858 	ucmdbuf->uscsi_rqlen = rqbuflen;
19859 	ucmdbuf->uscsi_flags = USCSI_RQENABLE|USCSI_SILENT|USCSI_READ;
19860 
19861 	status = sd_send_scsi_cmd(SD_GET_DEV(un), ucmdbuf, UIO_SYSSPACE,
19862 	    UIO_SYSSPACE, UIO_SYSSPACE, SD_PATH_STANDARD);
19863 
19864 	switch (status) {
19865 	case 0:
19866 		break;  /* Success! */
19867 	case EIO:
19868 		switch (ucmdbuf->uscsi_status) {
19869 		case STATUS_RESERVATION_CONFLICT:
19870 			status = EACCES;
19871 			break;
19872 		default:
19873 			break;
19874 		}
19875 		break;
19876 	default:
19877 		break;
19878 	}
19879 
19880 	if (status == 0) {
19881 		SD_DUMP_MEMORY(un, SD_LOG_IO,
19882 		    "sd_send_scsi_feature_GET_CONFIGURATION: data",
19883 		    (uchar_t *)bufaddr, SD_PROFILE_HEADER_LEN, SD_LOG_HEX);
19884 	}
19885 
19886 	SD_TRACE(SD_LOG_IO, un,
19887 	    "sd_send_scsi_feature_GET_CONFIGURATION: exit\n");
19888 
19889 	return (status);
19890 }
19891 
19892 
19893 /*
19894  *    Function: sd_send_scsi_MODE_SENSE
19895  *
19896  * Description: Utility function for issuing a scsi MODE SENSE command.
19897  *		Note: This routine uses a consistent implementation for Group0,
19898  *		Group1, and Group2 commands across all platforms. ATAPI devices
19899  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
19900  *
19901  *   Arguments: un - pointer to the softstate struct for the target.
19902  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
19903  *			  CDB_GROUP[1|2] (10 byte).
19904  *		bufaddr - buffer for page data retrieved from the target.
19905  *		buflen - size of page to be retrieved.
19906  *		page_code - page code of data to be retrieved from the target.
19907  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
19908  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
19909  *			to use the USCSI "direct" chain and bypass the normal
19910  *			command waitq.
19911  *
19912  * Return Code: 0   - Success
19913  *		errno return code from sd_send_scsi_cmd()
19914  *
19915  *     Context: Can sleep. Does not return until command is completed.
19916  */
19917 
19918 static int
19919 sd_send_scsi_MODE_SENSE(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
19920 	size_t buflen,  uchar_t page_code, int path_flag)
19921 {
19922 	struct	scsi_extended_sense	sense_buf;
19923 	uchar_t			cdb_buf[CDB_GROUP1];
19924 	struct uscsi_cmd	ucmd_buf;
19925 	int			status;
19926 
19927 	ASSERT(un != NULL);
19928 	ASSERT(!mutex_owned(SD_MUTEX(un)));
19929 	ASSERT(bufaddr != NULL);
19930 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
19931 	    (cdbsize == CDB_GROUP2));
19932 
19933 	SD_TRACE(SD_LOG_IO, un,
19934 	    "sd_send_scsi_MODE_SENSE: entry: un:0x%p\n", un);
19935 
19936 	bzero(cdb_buf, sizeof (cdb_buf));
19937 	bzero(&ucmd_buf, sizeof (ucmd_buf));
19938 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
19939 	bzero(bufaddr, buflen);
19940 
19941 	if (cdbsize == CDB_GROUP0) {
19942 		cdb_buf[0] = SCMD_MODE_SENSE;
19943 		cdb_buf[2] = page_code;
19944 		cdb_buf[4] = buflen;
19945 	} else {
19946 		cdb_buf[0] = SCMD_MODE_SENSE_G1;
19947 		cdb_buf[2] = page_code;
19948 		cdb_buf[7] = (uchar_t)((buflen & 0xFF00) >> 8);
19949 		cdb_buf[8] = (uchar_t)(buflen & 0xFF);
19950 	}
19951 
19952 	if ((SD_LUN(un) > 0) &&	(un->un_sd->sd_inq->inq_ansi == 0x01)) {
19953 		cdb_buf[1] |= (SD_LUN(un) << 5);
19954 	}
19955 
19956 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
19957 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
19958 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
19959 	ucmd_buf.uscsi_buflen	= buflen;
19960 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
19961 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
19962 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
19963 	ucmd_buf.uscsi_timeout	= 60;
19964 
19965 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
19966 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
19967 
19968 	switch (status) {
19969 	case 0:
19970 		break;	/* Success! */
19971 	case EIO:
19972 		switch (ucmd_buf.uscsi_status) {
19973 		case STATUS_RESERVATION_CONFLICT:
19974 			status = EACCES;
19975 			break;
19976 		default:
19977 			break;
19978 		}
19979 		break;
19980 	default:
19981 		break;
19982 	}
19983 
19984 	if (status == 0) {
19985 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SENSE: data",
19986 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
19987 	}
19988 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SENSE: exit\n");
19989 
19990 	return (status);
19991 }
19992 
19993 
19994 /*
19995  *    Function: sd_send_scsi_MODE_SELECT
19996  *
19997  * Description: Utility function for issuing a scsi MODE SELECT command.
19998  *		Note: This routine uses a consistent implementation for Group0,
19999  *		Group1, and Group2 commands across all platforms. ATAPI devices
20000  *		use Group 1 Read/Write commands and Group 2 Mode Sense/Select
20001  *
20002  *   Arguments: un - pointer to the softstate struct for the target.
20003  *		cdbsize - size CDB to be used (CDB_GROUP0 (6 byte), or
20004  *			  CDB_GROUP[1|2] (10 byte).
20005  *		bufaddr - buffer for page data retrieved from the target.
20006  *		buflen - size of page to be retrieved.
20007  *		save_page - boolean to determin if SP bit should be set.
20008  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20009  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20010  *			to use the USCSI "direct" chain and bypass the normal
20011  *			command waitq.
20012  *
20013  * Return Code: 0   - Success
20014  *		errno return code from sd_send_scsi_cmd()
20015  *
20016  *     Context: Can sleep. Does not return until command is completed.
20017  */
20018 
20019 static int
20020 sd_send_scsi_MODE_SELECT(struct sd_lun *un, int cdbsize, uchar_t *bufaddr,
20021 	size_t buflen,  uchar_t save_page, int path_flag)
20022 {
20023 	struct	scsi_extended_sense	sense_buf;
20024 	uchar_t			cdb_buf[CDB_GROUP1];
20025 	struct uscsi_cmd	ucmd_buf;
20026 	int			status;
20027 
20028 	ASSERT(un != NULL);
20029 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20030 	ASSERT(bufaddr != NULL);
20031 	ASSERT((cdbsize == CDB_GROUP0) || (cdbsize == CDB_GROUP1) ||
20032 	    (cdbsize == CDB_GROUP2));
20033 
20034 	SD_TRACE(SD_LOG_IO, un,
20035 	    "sd_send_scsi_MODE_SELECT: entry: un:0x%p\n", un);
20036 
20037 	bzero(cdb_buf, sizeof (cdb_buf));
20038 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20039 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20040 
20041 	cdb_buf[1] = 0x10;	/* Set the PF bit for many third party drives */
20042 
20043 	if (save_page == SD_SAVE_PAGE) {
20044 		cdb_buf[1] |= 0x01;	/* Set the savepage(SP) bit if given */
20045 	}
20046 
20047 	if (cdbsize == CDB_GROUP0) {
20048 		cdb_buf[0] = SCMD_MODE_SELECT;
20049 		cdb_buf[4] = buflen;
20050 	} else {
20051 		cdb_buf[0] = SCMD_MODE_SELECT_G1;
20052 		cdb_buf[7] = (uchar_t)((buflen & 0xFF00) >> 8);
20053 		cdb_buf[8] = (uchar_t)(buflen & 0xFF);
20054 	}
20055 
20056 	if ((SD_LUN(un) > 0) &&	(un->un_sd->sd_inq->inq_ansi == 0x01)) {
20057 		cdb_buf[1] |= (SD_LUN(un) << 5);
20058 	}
20059 
20060 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
20061 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20062 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20063 	ucmd_buf.uscsi_buflen	= buflen;
20064 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20065 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20066 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_WRITE | USCSI_SILENT;
20067 	ucmd_buf.uscsi_timeout	= 60;
20068 
20069 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20070 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20071 
20072 	switch (status) {
20073 	case 0:
20074 		break;	/* Success! */
20075 	case EIO:
20076 		switch (ucmd_buf.uscsi_status) {
20077 		case STATUS_RESERVATION_CONFLICT:
20078 			status = EACCES;
20079 			break;
20080 		default:
20081 			break;
20082 		}
20083 		break;
20084 	default:
20085 		break;
20086 	}
20087 
20088 	if (status == 0) {
20089 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_MODE_SELECT: data",
20090 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20091 	}
20092 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_MODE_SELECT: exit\n");
20093 
20094 	return (status);
20095 }
20096 
20097 
20098 /*
20099  *    Function: sd_send_scsi_RDWR
20100  *
20101  * Description: Issue a scsi READ or WRITE command with the given parameters.
20102  *
20103  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20104  *		cmd:	 SCMD_READ or SCMD_WRITE
20105  *		bufaddr: Address of caller's buffer to receive the RDWR data
20106  *		buflen:  Length of caller's buffer receive the RDWR data.
20107  *		start_block: Block number for the start of the RDWR operation.
20108  *			 (Assumes target-native block size.)
20109  *		residp:  Pointer to variable to receive the redisual of the
20110  *			 RDWR operation (may be NULL of no residual requested).
20111  *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
20112  *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
20113  *			to use the USCSI "direct" chain and bypass the normal
20114  *			command waitq.
20115  *
20116  * Return Code: 0   - Success
20117  *		errno return code from sd_send_scsi_cmd()
20118  *
20119  *     Context: Can sleep. Does not return until command is completed.
20120  */
20121 
20122 static int
20123 sd_send_scsi_RDWR(struct sd_lun *un, uchar_t cmd, void *bufaddr,
20124 	size_t buflen, daddr_t start_block, int path_flag)
20125 {
20126 	struct	scsi_extended_sense	sense_buf;
20127 	uchar_t			cdb_buf[CDB_GROUP4];	/* Use max size */
20128 	struct uscsi_cmd	ucmd_buf;
20129 	uint32_t		block_count;
20130 	int			status;
20131 	int			cdbsize;
20132 	uchar_t			flag;
20133 	int			i;
20134 
20135 	ASSERT(un != NULL);
20136 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20137 	ASSERT(bufaddr != NULL);
20138 	ASSERT((cmd == SCMD_READ) || (cmd == SCMD_WRITE));
20139 
20140 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: entry: un:0x%p\n", un);
20141 
20142 	if (un->un_f_tgt_blocksize_is_valid != TRUE) {
20143 		return (EINVAL);
20144 	}
20145 
20146 	mutex_enter(SD_MUTEX(un));
20147 	block_count = SD_BYTES2TGTBLOCKS(un, buflen);
20148 	mutex_exit(SD_MUTEX(un));
20149 
20150 	flag = (cmd == SCMD_READ) ? USCSI_READ : USCSI_WRITE;
20151 
20152 	SD_INFO(SD_LOG_IO, un, "sd_send_scsi_RDWR: "
20153 	    "bufaddr:0x%p buflen:0x%x start_block:0x%p block_count:0x%x\n",
20154 	    bufaddr, buflen, start_block, block_count);
20155 
20156 	bzero(cdb_buf, sizeof (cdb_buf));
20157 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20158 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20159 
20160 	/* Compute CDB size to use */
20161 	if (start_block > 0xffffffff)
20162 		cdbsize = CDB_GROUP4;
20163 	else if ((start_block & 0xFFE00000) ||
20164 	    (un->un_f_cfg_is_atapi == TRUE))
20165 		cdbsize = CDB_GROUP1;
20166 	else
20167 		cdbsize = CDB_GROUP0;
20168 
20169 	switch (cdbsize) {
20170 	case CDB_GROUP0:	/* 6-byte CDBs */
20171 		cdb_buf[0] = cmd;
20172 		cdb_buf[1] = (uchar_t)((start_block & 0x001F0000) >> 16);
20173 		cdb_buf[2] = (uchar_t)((start_block & 0x0000FF00) >> 8);
20174 		cdb_buf[3] = (uchar_t)(start_block  & 0x000000FF);
20175 		cdb_buf[4] = (uchar_t)(block_count  & 0xFF);
20176 		break;
20177 	case CDB_GROUP1:	/* 10-byte CDBs */
20178 		cdb_buf[0] = cmd | SCMD_GROUP1;
20179 		cdb_buf[2] = (uchar_t)((start_block & 0xFF000000) >> 24);
20180 		cdb_buf[3] = (uchar_t)((start_block & 0x00FF0000) >> 16);
20181 		cdb_buf[4] = (uchar_t)((start_block & 0x0000FF00) >> 8);
20182 		cdb_buf[5] = (uchar_t)(start_block  & 0x000000FF);
20183 		cdb_buf[7] = (uchar_t)((block_count & 0xFF00) >> 8);
20184 		cdb_buf[8] = (uchar_t)(block_count  & 0xFF);
20185 		break;
20186 	case CDB_GROUP4:	/* 16-byte CDBs */
20187 		cdb_buf[0] = cmd | SCMD_GROUP4;
20188 		/* Block address is in bytes 2 - 9 */
20189 		for (i = 9; i > 1; i--) {
20190 			cdb_buf[i] = (uchar_t)(start_block & 0xFF);
20191 			start_block >>= 8;
20192 		}
20193 		/* Block count is in bytes 10 - 13 */
20194 		for (i = 13; i > 9; i--) {
20195 			cdb_buf[i] = (uchar_t)(block_count & 0xFF);
20196 			block_count >>= 8;
20197 		}
20198 		break;
20199 	case CDB_GROUP5:	/* 12-byte CDBs (currently unsupported) */
20200 	default:
20201 		/* All others reserved */
20202 		return (EINVAL);
20203 	}
20204 
20205 	/* Set LUN bit(s) in CDB if this is a SCSI-1 device */
20206 	if ((SD_LUN(un) > 0) &&	(un->un_sd->sd_inq->inq_ansi == 0x01)) {
20207 		cdb_buf[1] |= (SD_LUN(un) << 5);
20208 	}
20209 
20210 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
20211 	ucmd_buf.uscsi_cdblen	= (uchar_t)cdbsize;
20212 	ucmd_buf.uscsi_bufaddr	= bufaddr;
20213 	ucmd_buf.uscsi_buflen	= buflen;
20214 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20215 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20216 	ucmd_buf.uscsi_flags	= flag | USCSI_RQENABLE | USCSI_SILENT;
20217 	ucmd_buf.uscsi_timeout	= 60;
20218 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20219 				UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20220 	switch (status) {
20221 	case 0:
20222 		break;	/* Success! */
20223 	case EIO:
20224 		switch (ucmd_buf.uscsi_status) {
20225 		case STATUS_RESERVATION_CONFLICT:
20226 			status = EACCES;
20227 			break;
20228 		default:
20229 			break;
20230 		}
20231 		break;
20232 	default:
20233 		break;
20234 	}
20235 
20236 	if (status == 0) {
20237 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_RDWR: data",
20238 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20239 	}
20240 
20241 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_RDWR: exit\n");
20242 
20243 	return (status);
20244 }
20245 
20246 
20247 /*
20248  *    Function: sd_send_scsi_LOG_SENSE
20249  *
20250  * Description: Issue a scsi LOG_SENSE command with the given parameters.
20251  *
20252  *   Arguments: un:      Pointer to the sd_lun struct for the target.
20253  *
20254  * Return Code: 0   - Success
20255  *		errno return code from sd_send_scsi_cmd()
20256  *
20257  *     Context: Can sleep. Does not return until command is completed.
20258  */
20259 
20260 static int
20261 sd_send_scsi_LOG_SENSE(struct sd_lun *un, uchar_t *bufaddr, uint16_t buflen,
20262 	uchar_t page_code, uchar_t page_control, uint16_t param_ptr,
20263 	int path_flag)
20264 
20265 {
20266 	struct	scsi_extended_sense	sense_buf;
20267 	uchar_t			cdb_buf[CDB_GROUP1];
20268 	struct uscsi_cmd	ucmd_buf;
20269 	int			status;
20270 
20271 	ASSERT(un != NULL);
20272 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20273 
20274 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: entry: un:0x%p\n", un);
20275 
20276 	bzero(cdb_buf, sizeof (cdb_buf));
20277 	bzero(&ucmd_buf, sizeof (ucmd_buf));
20278 	bzero(&sense_buf, sizeof (struct scsi_extended_sense));
20279 
20280 	cdb_buf[0] = SCMD_LOG_SENSE_G1;
20281 	cdb_buf[2] = (page_control << 6) | page_code;
20282 	cdb_buf[5] = (uchar_t)((param_ptr & 0xFF00) >> 8);
20283 	cdb_buf[6] = (uchar_t)(param_ptr  & 0x00FF);
20284 	cdb_buf[7] = (uchar_t)((buflen & 0xFF00) >> 8);
20285 	cdb_buf[8] = (uchar_t)(buflen  & 0x00FF);
20286 
20287 	ucmd_buf.uscsi_cdb	= (char *)cdb_buf;
20288 	ucmd_buf.uscsi_cdblen	= sizeof (cdb_buf);
20289 	ucmd_buf.uscsi_bufaddr	= (caddr_t)bufaddr;
20290 	ucmd_buf.uscsi_buflen	= buflen;
20291 	ucmd_buf.uscsi_rqbuf	= (caddr_t)&sense_buf;
20292 	ucmd_buf.uscsi_rqlen	= sizeof (struct scsi_extended_sense);
20293 	ucmd_buf.uscsi_flags	= USCSI_RQENABLE | USCSI_READ | USCSI_SILENT;
20294 	ucmd_buf.uscsi_timeout	= 60;
20295 
20296 	status = sd_send_scsi_cmd(SD_GET_DEV(un), &ucmd_buf, UIO_SYSSPACE,
20297 	    UIO_SYSSPACE, UIO_SYSSPACE, path_flag);
20298 
20299 	switch (status) {
20300 	case 0:
20301 		break;
20302 	case EIO:
20303 		switch (ucmd_buf.uscsi_status) {
20304 		case STATUS_RESERVATION_CONFLICT:
20305 			status = EACCES;
20306 			break;
20307 		case STATUS_CHECK:
20308 			if ((ucmd_buf.uscsi_rqstatus == STATUS_GOOD) &&
20309 			    (sense_buf.es_key == KEY_ILLEGAL_REQUEST) &&
20310 			    (sense_buf.es_add_code == 0x24)) {
20311 				/*
20312 				 * ASC 0x24: INVALID FIELD IN CDB
20313 				 */
20314 				switch (page_code) {
20315 				case START_STOP_CYCLE_PAGE:
20316 					/*
20317 					 * The start stop cycle counter is
20318 					 * implemented as page 0x31 in earlier
20319 					 * generation disks. In new generation
20320 					 * disks the start stop cycle counter is
20321 					 * implemented as page 0xE. To properly
20322 					 * handle this case if an attempt for
20323 					 * log page 0xE is made and fails we
20324 					 * will try again using page 0x31.
20325 					 *
20326 					 * Network storage BU committed to
20327 					 * maintain the page 0x31 for this
20328 					 * purpose and will not have any other
20329 					 * page implemented with page code 0x31
20330 					 * until all disks transition to the
20331 					 * standard page.
20332 					 */
20333 					mutex_enter(SD_MUTEX(un));
20334 					un->un_start_stop_cycle_page =
20335 					    START_STOP_CYCLE_VU_PAGE;
20336 					cdb_buf[2] = (char)(page_control << 6) |
20337 					    un->un_start_stop_cycle_page;
20338 					mutex_exit(SD_MUTEX(un));
20339 					status = sd_send_scsi_cmd(
20340 					    SD_GET_DEV(un), &ucmd_buf,
20341 					    UIO_SYSSPACE, UIO_SYSSPACE,
20342 					    UIO_SYSSPACE, path_flag);
20343 
20344 					break;
20345 				case TEMPERATURE_PAGE:
20346 					status = ENOTTY;
20347 					break;
20348 				default:
20349 					break;
20350 				}
20351 			}
20352 			break;
20353 		default:
20354 			break;
20355 		}
20356 		break;
20357 	default:
20358 		break;
20359 	}
20360 
20361 	if (status == 0) {
20362 		SD_DUMP_MEMORY(un, SD_LOG_IO, "sd_send_scsi_LOG_SENSE: data",
20363 		    (uchar_t *)bufaddr, buflen, SD_LOG_HEX);
20364 	}
20365 
20366 	SD_TRACE(SD_LOG_IO, un, "sd_send_scsi_LOG_SENSE: exit\n");
20367 
20368 	return (status);
20369 }
20370 
20371 
20372 /*
20373  *    Function: sdioctl
20374  *
20375  * Description: Driver's ioctl(9e) entry point function.
20376  *
20377  *   Arguments: dev     - device number
20378  *		cmd     - ioctl operation to be performed
20379  *		arg     - user argument, contains data to be set or reference
20380  *			  parameter for get
20381  *		flag    - bit flag, indicating open settings, 32/64 bit type
20382  *		cred_p  - user credential pointer
20383  *		rval_p  - calling process return value (OPT)
20384  *
20385  * Return Code: EINVAL
20386  *		ENOTTY
20387  *		ENXIO
20388  *		EIO
20389  *		EFAULT
20390  *		ENOTSUP
20391  *		EPERM
20392  *
20393  *     Context: Called from the device switch at normal priority.
20394  */
20395 
20396 static int
20397 sdioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p, int *rval_p)
20398 {
20399 	struct sd_lun	*un = NULL;
20400 	int		geom_validated = FALSE;
20401 	int		err = 0;
20402 	int		i = 0;
20403 	cred_t		*cr;
20404 
20405 	/*
20406 	 * All device accesses go thru sdstrategy where we check on suspend
20407 	 * status
20408 	 */
20409 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
20410 		return (ENXIO);
20411 	}
20412 
20413 	ASSERT(!mutex_owned(SD_MUTEX(un)));
20414 
20415 	/*
20416 	 * Moved this wait from sd_uscsi_strategy to here for
20417 	 * reasons of deadlock prevention. Internal driver commands,
20418 	 * specifically those to change a devices power level, result
20419 	 * in a call to sd_uscsi_strategy.
20420 	 */
20421 	mutex_enter(SD_MUTEX(un));
20422 	while ((un->un_state == SD_STATE_SUSPENDED) ||
20423 	    (un->un_state == SD_STATE_PM_CHANGING)) {
20424 		cv_wait(&un->un_suspend_cv, SD_MUTEX(un));
20425 	}
20426 	/*
20427 	 * Twiddling the counter here protects commands from now
20428 	 * through to the top of sd_uscsi_strategy. Without the
20429 	 * counter inc. a power down, for example, could get in
20430 	 * after the above check for state is made and before
20431 	 * execution gets to the top of sd_uscsi_strategy.
20432 	 * That would cause problems.
20433 	 */
20434 	un->un_ncmds_in_driver++;
20435 
20436 	if ((un->un_f_geometry_is_valid == FALSE) &&
20437 	    (flag & (FNDELAY | FNONBLOCK))) {
20438 		switch (cmd) {
20439 		case CDROMPAUSE:
20440 		case CDROMRESUME:
20441 		case CDROMPLAYMSF:
20442 		case CDROMPLAYTRKIND:
20443 		case CDROMREADTOCHDR:
20444 		case CDROMREADTOCENTRY:
20445 		case CDROMSTOP:
20446 		case CDROMSTART:
20447 		case CDROMVOLCTRL:
20448 		case CDROMSUBCHNL:
20449 		case CDROMREADMODE2:
20450 		case CDROMREADMODE1:
20451 		case CDROMREADOFFSET:
20452 		case CDROMSBLKMODE:
20453 		case CDROMGBLKMODE:
20454 		case CDROMGDRVSPEED:
20455 		case CDROMSDRVSPEED:
20456 		case CDROMCDDA:
20457 		case CDROMCDXA:
20458 		case CDROMSUBCODE:
20459 			if (!ISCD(un)) {
20460 				un->un_ncmds_in_driver--;
20461 				ASSERT(un->un_ncmds_in_driver >= 0);
20462 				mutex_exit(SD_MUTEX(un));
20463 				return (ENOTTY);
20464 			}
20465 			break;
20466 		case FDEJECT:
20467 		case DKIOCEJECT:
20468 		case CDROMEJECT:
20469 			if (!ISREMOVABLE(un)) {
20470 				un->un_ncmds_in_driver--;
20471 				ASSERT(un->un_ncmds_in_driver >= 0);
20472 				mutex_exit(SD_MUTEX(un));
20473 				return (ENOTTY);
20474 			}
20475 			break;
20476 		case DKIOCSVTOC:
20477 		case DKIOCSETEFI:
20478 		case DKIOCSMBOOT:
20479 			mutex_exit(SD_MUTEX(un));
20480 			err = sd_send_scsi_TEST_UNIT_READY(un, 0);
20481 			if (err != 0) {
20482 				mutex_enter(SD_MUTEX(un));
20483 				un->un_ncmds_in_driver--;
20484 				ASSERT(un->un_ncmds_in_driver >= 0);
20485 				mutex_exit(SD_MUTEX(un));
20486 				return (EIO);
20487 			}
20488 			mutex_enter(SD_MUTEX(un));
20489 			/* FALLTHROUGH */
20490 		case DKIOCREMOVABLE:
20491 		case DKIOCINFO:
20492 		case DKIOCGMEDIAINFO:
20493 		case MHIOCENFAILFAST:
20494 		case MHIOCSTATUS:
20495 		case MHIOCTKOWN:
20496 		case MHIOCRELEASE:
20497 		case MHIOCGRP_INKEYS:
20498 		case MHIOCGRP_INRESV:
20499 		case MHIOCGRP_REGISTER:
20500 		case MHIOCGRP_RESERVE:
20501 		case MHIOCGRP_PREEMPTANDABORT:
20502 		case MHIOCGRP_REGISTERANDIGNOREKEY:
20503 		case CDROMCLOSETRAY:
20504 		case USCSICMD:
20505 			goto skip_ready_valid;
20506 		default:
20507 			break;
20508 		}
20509 
20510 		mutex_exit(SD_MUTEX(un));
20511 		err = sd_ready_and_valid(un);
20512 		mutex_enter(SD_MUTEX(un));
20513 		if (err == SD_READY_NOT_VALID) {
20514 			switch (cmd) {
20515 			case DKIOCGAPART:
20516 			case DKIOCGGEOM:
20517 			case DKIOCSGEOM:
20518 			case DKIOCGVTOC:
20519 			case DKIOCSVTOC:
20520 			case DKIOCSAPART:
20521 			case DKIOCG_PHYGEOM:
20522 			case DKIOCG_VIRTGEOM:
20523 				err = ENOTSUP;
20524 				un->un_ncmds_in_driver--;
20525 				ASSERT(un->un_ncmds_in_driver >= 0);
20526 				mutex_exit(SD_MUTEX(un));
20527 				return (err);
20528 			}
20529 		}
20530 		if (err != SD_READY_VALID) {
20531 			switch (cmd) {
20532 			case DKIOCSTATE:
20533 			case CDROMGDRVSPEED:
20534 			case CDROMSDRVSPEED:
20535 			case FDEJECT:	/* for eject command */
20536 			case DKIOCEJECT:
20537 			case CDROMEJECT:
20538 			case DKIOCGETEFI:
20539 			case DKIOCSGEOM:
20540 			case DKIOCREMOVABLE:
20541 			case DKIOCSAPART:
20542 			case DKIOCSETEFI:
20543 				break;
20544 			default:
20545 				if (ISREMOVABLE(un)) {
20546 					err = ENXIO;
20547 				} else {
20548 					/* Do not map EACCES to EIO */
20549 					if (err != EACCES)
20550 						err = EIO;
20551 				}
20552 				un->un_ncmds_in_driver--;
20553 				ASSERT(un->un_ncmds_in_driver >= 0);
20554 				mutex_exit(SD_MUTEX(un));
20555 				return (err);
20556 			}
20557 		}
20558 		geom_validated = TRUE;
20559 	}
20560 	if ((un->un_f_geometry_is_valid == TRUE) &&
20561 	    (un->un_solaris_size > 0)) {
20562 		/*
20563 		 * the "geometry_is_valid" flag could be true if we
20564 		 * have an fdisk table but no Solaris partition
20565 		 */
20566 		if (un->un_vtoc.v_sanity != VTOC_SANE) {
20567 			/* it is EFI, so return ENOTSUP for these */
20568 			switch (cmd) {
20569 			case DKIOCGAPART:
20570 			case DKIOCGGEOM:
20571 			case DKIOCGVTOC:
20572 			case DKIOCSVTOC:
20573 			case DKIOCSAPART:
20574 				err = ENOTSUP;
20575 				un->un_ncmds_in_driver--;
20576 				ASSERT(un->un_ncmds_in_driver >= 0);
20577 				mutex_exit(SD_MUTEX(un));
20578 				return (err);
20579 			}
20580 		}
20581 	}
20582 
20583 skip_ready_valid:
20584 	mutex_exit(SD_MUTEX(un));
20585 
20586 	switch (cmd) {
20587 	case DKIOCINFO:
20588 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCINFO\n");
20589 		err = sd_dkio_ctrl_info(dev, (caddr_t)arg, flag);
20590 		break;
20591 
20592 	case DKIOCGMEDIAINFO:
20593 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMEDIAINFO\n");
20594 		err = sd_get_media_info(dev, (caddr_t)arg, flag);
20595 		break;
20596 
20597 	case DKIOCGGEOM:
20598 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGGEOM\n");
20599 		err = sd_dkio_get_geometry(dev, (caddr_t)arg, flag,
20600 		    geom_validated);
20601 		break;
20602 
20603 	case DKIOCSGEOM:
20604 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSGEOM\n");
20605 		err = sd_dkio_set_geometry(dev, (caddr_t)arg, flag);
20606 		break;
20607 
20608 	case DKIOCGAPART:
20609 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGAPART\n");
20610 		err = sd_dkio_get_partition(dev, (caddr_t)arg, flag,
20611 		    geom_validated);
20612 		break;
20613 
20614 	case DKIOCSAPART:
20615 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSAPART\n");
20616 		err = sd_dkio_set_partition(dev, (caddr_t)arg, flag);
20617 		break;
20618 
20619 	case DKIOCGVTOC:
20620 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGVTOC\n");
20621 		err = sd_dkio_get_vtoc(dev, (caddr_t)arg, flag,
20622 		    geom_validated);
20623 		break;
20624 
20625 	case DKIOCGETEFI:
20626 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGETEFI\n");
20627 		err = sd_dkio_get_efi(dev, (caddr_t)arg, flag);
20628 		break;
20629 
20630 	case DKIOCPARTITION:
20631 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTITION\n");
20632 		err = sd_dkio_partition(dev, (caddr_t)arg, flag);
20633 		break;
20634 
20635 	case DKIOCSVTOC:
20636 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSVTOC\n");
20637 		err = sd_dkio_set_vtoc(dev, (caddr_t)arg, flag);
20638 		break;
20639 
20640 	case DKIOCSETEFI:
20641 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSETEFI\n");
20642 		err = sd_dkio_set_efi(dev, (caddr_t)arg, flag);
20643 		break;
20644 
20645 	case DKIOCGMBOOT:
20646 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGMBOOT\n");
20647 		err = sd_dkio_get_mboot(dev, (caddr_t)arg, flag);
20648 		break;
20649 
20650 	case DKIOCSMBOOT:
20651 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSMBOOT\n");
20652 		err = sd_dkio_set_mboot(dev, (caddr_t)arg, flag);
20653 		break;
20654 
20655 	case DKIOCLOCK:
20656 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCLOCK\n");
20657 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
20658 		    SD_PATH_STANDARD);
20659 		break;
20660 
20661 	case DKIOCUNLOCK:
20662 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCUNLOCK\n");
20663 		err = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
20664 		    SD_PATH_STANDARD);
20665 		break;
20666 
20667 	case DKIOCSTATE: {
20668 		enum dkio_state		state;
20669 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCSTATE\n");
20670 
20671 		if (ddi_copyin((void *)arg, &state, sizeof (int), flag) != 0) {
20672 			err = EFAULT;
20673 		} else {
20674 			err = sd_check_media(dev, state);
20675 			if (err == 0) {
20676 				if (ddi_copyout(&un->un_mediastate, (void *)arg,
20677 				    sizeof (int), flag) != 0)
20678 					err = EFAULT;
20679 			}
20680 		}
20681 		break;
20682 	}
20683 
20684 	case DKIOCREMOVABLE:
20685 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCREMOVABLE\n");
20686 		if (ISREMOVABLE(un)) {
20687 			i = 1;
20688 		} else {
20689 			i = 0;
20690 		}
20691 		if (ddi_copyout(&i, (void *)arg, sizeof (int), flag) != 0) {
20692 			err = EFAULT;
20693 		} else {
20694 			err = 0;
20695 		}
20696 		break;
20697 
20698 	case DKIOCGTEMPERATURE:
20699 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCGTEMPERATURE\n");
20700 		err = sd_dkio_get_temp(dev, (caddr_t)arg, flag);
20701 		break;
20702 
20703 	case MHIOCENFAILFAST:
20704 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCENFAILFAST\n");
20705 		if ((err = drv_priv(cred_p)) == 0) {
20706 			err = sd_mhdioc_failfast(dev, (caddr_t)arg, flag);
20707 		}
20708 		break;
20709 
20710 	case MHIOCTKOWN:
20711 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCTKOWN\n");
20712 		if ((err = drv_priv(cred_p)) == 0) {
20713 			err = sd_mhdioc_takeown(dev, (caddr_t)arg, flag);
20714 		}
20715 		break;
20716 
20717 	case MHIOCRELEASE:
20718 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCRELEASE\n");
20719 		if ((err = drv_priv(cred_p)) == 0) {
20720 			err = sd_mhdioc_release(dev);
20721 		}
20722 		break;
20723 
20724 	case MHIOCSTATUS:
20725 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCSTATUS\n");
20726 		if ((err = drv_priv(cred_p)) == 0) {
20727 			switch (sd_send_scsi_TEST_UNIT_READY(un, 0)) {
20728 			case 0:
20729 				err = 0;
20730 				break;
20731 			case EACCES:
20732 				*rval_p = 1;
20733 				err = 0;
20734 				break;
20735 			default:
20736 				err = EIO;
20737 				break;
20738 			}
20739 		}
20740 		break;
20741 
20742 	case MHIOCQRESERVE:
20743 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCQRESERVE\n");
20744 		if ((err = drv_priv(cred_p)) == 0) {
20745 			err = sd_reserve_release(dev, SD_RESERVE);
20746 		}
20747 		break;
20748 
20749 	case MHIOCREREGISTERDEVID:
20750 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCREREGISTERDEVID\n");
20751 		if (drv_priv(cred_p) == EPERM) {
20752 			err = EPERM;
20753 		} else if (ISREMOVABLE(un) || ISCD(un)) {
20754 			err = ENOTTY;
20755 		} else {
20756 			err = sd_mhdioc_register_devid(dev);
20757 		}
20758 		break;
20759 
20760 	case MHIOCGRP_INKEYS:
20761 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INKEYS\n");
20762 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
20763 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
20764 				err = ENOTSUP;
20765 			} else {
20766 				err = sd_mhdioc_inkeys(dev, (caddr_t)arg,
20767 				    flag);
20768 			}
20769 		}
20770 		break;
20771 
20772 	case MHIOCGRP_INRESV:
20773 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_INRESV\n");
20774 		if (((err = drv_priv(cred_p)) != EPERM) && arg != NULL) {
20775 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
20776 				err = ENOTSUP;
20777 			} else {
20778 				err = sd_mhdioc_inresv(dev, (caddr_t)arg, flag);
20779 			}
20780 		}
20781 		break;
20782 
20783 	case MHIOCGRP_REGISTER:
20784 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_REGISTER\n");
20785 		if ((err = drv_priv(cred_p)) != EPERM) {
20786 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
20787 				err = ENOTSUP;
20788 			} else if (arg != NULL) {
20789 				mhioc_register_t reg;
20790 				if (ddi_copyin((void *)arg, &reg,
20791 				    sizeof (mhioc_register_t), flag) != 0) {
20792 					err = EFAULT;
20793 				} else {
20794 					err =
20795 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
20796 					    un, SD_SCSI3_REGISTER,
20797 					    (uchar_t *)&reg);
20798 				}
20799 			}
20800 		}
20801 		break;
20802 
20803 	case MHIOCGRP_RESERVE:
20804 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_RESERVE\n");
20805 		if ((err = drv_priv(cred_p)) != EPERM) {
20806 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
20807 				err = ENOTSUP;
20808 			} else if (arg != NULL) {
20809 				mhioc_resv_desc_t resv_desc;
20810 				if (ddi_copyin((void *)arg, &resv_desc,
20811 				    sizeof (mhioc_resv_desc_t), flag) != 0) {
20812 					err = EFAULT;
20813 				} else {
20814 					err =
20815 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
20816 					    un, SD_SCSI3_RESERVE,
20817 					    (uchar_t *)&resv_desc);
20818 				}
20819 			}
20820 		}
20821 		break;
20822 
20823 	case MHIOCGRP_PREEMPTANDABORT:
20824 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
20825 		if ((err = drv_priv(cred_p)) != EPERM) {
20826 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
20827 				err = ENOTSUP;
20828 			} else if (arg != NULL) {
20829 				mhioc_preemptandabort_t preempt_abort;
20830 				if (ddi_copyin((void *)arg, &preempt_abort,
20831 				    sizeof (mhioc_preemptandabort_t),
20832 				    flag) != 0) {
20833 					err = EFAULT;
20834 				} else {
20835 					err =
20836 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
20837 					    un, SD_SCSI3_PREEMPTANDABORT,
20838 					    (uchar_t *)&preempt_abort);
20839 				}
20840 			}
20841 		}
20842 		break;
20843 
20844 	case MHIOCGRP_REGISTERANDIGNOREKEY:
20845 		SD_TRACE(SD_LOG_IOCTL, un, "MHIOCGRP_PREEMPTANDABORT\n");
20846 		if ((err = drv_priv(cred_p)) != EPERM) {
20847 			if (un->un_reservation_type == SD_SCSI2_RESERVATION) {
20848 				err = ENOTSUP;
20849 			} else if (arg != NULL) {
20850 				mhioc_registerandignorekey_t r_and_i;
20851 				if (ddi_copyin((void *)arg, (void *)&r_and_i,
20852 				    sizeof (mhioc_registerandignorekey_t),
20853 				    flag) != 0) {
20854 					err = EFAULT;
20855 				} else {
20856 					err =
20857 					    sd_send_scsi_PERSISTENT_RESERVE_OUT(
20858 					    un, SD_SCSI3_REGISTERANDIGNOREKEY,
20859 					    (uchar_t *)&r_and_i);
20860 				}
20861 			}
20862 		}
20863 		break;
20864 
20865 	case USCSICMD:
20866 		SD_TRACE(SD_LOG_IOCTL, un, "USCSICMD\n");
20867 		cr = ddi_get_cred();
20868 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
20869 			err = EPERM;
20870 		} else {
20871 			err = sd_uscsi_ioctl(dev, (caddr_t)arg, flag);
20872 		}
20873 		break;
20874 
20875 	case CDROMPAUSE:
20876 	case CDROMRESUME:
20877 		SD_TRACE(SD_LOG_IOCTL, un, "PAUSE-RESUME\n");
20878 		if (!ISCD(un)) {
20879 			err = ENOTTY;
20880 		} else {
20881 			err = sr_pause_resume(dev, cmd);
20882 		}
20883 		break;
20884 
20885 	case CDROMPLAYMSF:
20886 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYMSF\n");
20887 		if (!ISCD(un)) {
20888 			err = ENOTTY;
20889 		} else {
20890 			err = sr_play_msf(dev, (caddr_t)arg, flag);
20891 		}
20892 		break;
20893 
20894 	case CDROMPLAYTRKIND:
20895 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMPLAYTRKIND\n");
20896 #if defined(__i386) || defined(__amd64)
20897 		/*
20898 		 * not supported on ATAPI CD drives, use CDROMPLAYMSF instead
20899 		 */
20900 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
20901 #else
20902 		if (!ISCD(un)) {
20903 #endif
20904 			err = ENOTTY;
20905 		} else {
20906 			err = sr_play_trkind(dev, (caddr_t)arg, flag);
20907 		}
20908 		break;
20909 
20910 	case CDROMREADTOCHDR:
20911 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCHDR\n");
20912 		if (!ISCD(un)) {
20913 			err = ENOTTY;
20914 		} else {
20915 			err = sr_read_tochdr(dev, (caddr_t)arg, flag);
20916 		}
20917 		break;
20918 
20919 	case CDROMREADTOCENTRY:
20920 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADTOCENTRY\n");
20921 		if (!ISCD(un)) {
20922 			err = ENOTTY;
20923 		} else {
20924 			err = sr_read_tocentry(dev, (caddr_t)arg, flag);
20925 		}
20926 		break;
20927 
20928 	case CDROMSTOP:
20929 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTOP\n");
20930 		if (!ISCD(un)) {
20931 			err = ENOTTY;
20932 		} else {
20933 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_STOP,
20934 			    SD_PATH_STANDARD);
20935 		}
20936 		break;
20937 
20938 	case CDROMSTART:
20939 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSTART\n");
20940 		if (!ISCD(un)) {
20941 			err = ENOTTY;
20942 		} else {
20943 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_START,
20944 			    SD_PATH_STANDARD);
20945 		}
20946 		break;
20947 
20948 	case CDROMCLOSETRAY:
20949 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCLOSETRAY\n");
20950 		if (!ISCD(un)) {
20951 			err = ENOTTY;
20952 		} else {
20953 			err = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_CLOSE,
20954 			    SD_PATH_STANDARD);
20955 		}
20956 		break;
20957 
20958 	case FDEJECT:	/* for eject command */
20959 	case DKIOCEJECT:
20960 	case CDROMEJECT:
20961 		SD_TRACE(SD_LOG_IOCTL, un, "EJECT\n");
20962 		if (!ISREMOVABLE(un)) {
20963 			err = ENOTTY;
20964 		} else {
20965 			err = sr_eject(dev);
20966 		}
20967 		break;
20968 
20969 	case CDROMVOLCTRL:
20970 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMVOLCTRL\n");
20971 		if (!ISCD(un)) {
20972 			err = ENOTTY;
20973 		} else {
20974 			err = sr_volume_ctrl(dev, (caddr_t)arg, flag);
20975 		}
20976 		break;
20977 
20978 	case CDROMSUBCHNL:
20979 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCHNL\n");
20980 		if (!ISCD(un)) {
20981 			err = ENOTTY;
20982 		} else {
20983 			err = sr_read_subchannel(dev, (caddr_t)arg, flag);
20984 		}
20985 		break;
20986 
20987 	case CDROMREADMODE2:
20988 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE2\n");
20989 		if (!ISCD(un)) {
20990 			err = ENOTTY;
20991 		} else if (un->un_f_cfg_is_atapi == TRUE) {
20992 			/*
20993 			 * If the drive supports READ CD, use that instead of
20994 			 * switching the LBA size via a MODE SELECT
20995 			 * Block Descriptor
20996 			 */
20997 			err = sr_read_cd_mode2(dev, (caddr_t)arg, flag);
20998 		} else {
20999 			err = sr_read_mode2(dev, (caddr_t)arg, flag);
21000 		}
21001 		break;
21002 
21003 	case CDROMREADMODE1:
21004 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADMODE1\n");
21005 		if (!ISCD(un)) {
21006 			err = ENOTTY;
21007 		} else {
21008 			err = sr_read_mode1(dev, (caddr_t)arg, flag);
21009 		}
21010 		break;
21011 
21012 	case CDROMREADOFFSET:
21013 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMREADOFFSET\n");
21014 		if (!ISCD(un)) {
21015 			err = ENOTTY;
21016 		} else {
21017 			err = sr_read_sony_session_offset(dev, (caddr_t)arg,
21018 			    flag);
21019 		}
21020 		break;
21021 
21022 	case CDROMSBLKMODE:
21023 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSBLKMODE\n");
21024 		/*
21025 		 * There is no means of changing block size in case of atapi
21026 		 * drives, thus return ENOTTY if drive type is atapi
21027 		 */
21028 		if (!ISCD(un) || (un->un_f_cfg_is_atapi == TRUE)) {
21029 			err = ENOTTY;
21030 		} else if (un->un_f_mmc_cap == TRUE) {
21031 
21032 			/*
21033 			 * MMC Devices do not support changing the
21034 			 * logical block size
21035 			 *
21036 			 * Note: EINVAL is being returned instead of ENOTTY to
21037 			 * maintain consistancy with the original mmc
21038 			 * driver update.
21039 			 */
21040 			err = EINVAL;
21041 		} else {
21042 			mutex_enter(SD_MUTEX(un));
21043 			if ((!(un->un_exclopen & (1<<SDPART(dev)))) ||
21044 			    (un->un_ncmds_in_transport > 0)) {
21045 				mutex_exit(SD_MUTEX(un));
21046 				err = EINVAL;
21047 			} else {
21048 				mutex_exit(SD_MUTEX(un));
21049 				err = sr_change_blkmode(dev, cmd, arg, flag);
21050 			}
21051 		}
21052 		break;
21053 
21054 	case CDROMGBLKMODE:
21055 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMGBLKMODE\n");
21056 		if (!ISCD(un)) {
21057 			err = ENOTTY;
21058 		} else if ((un->un_f_cfg_is_atapi != FALSE) &&
21059 		    (un->un_f_blockcount_is_valid != FALSE)) {
21060 			/*
21061 			 * Drive is an ATAPI drive so return target block
21062 			 * size for ATAPI drives since we cannot change the
21063 			 * blocksize on ATAPI drives. Used primarily to detect
21064 			 * if an ATAPI cdrom is present.
21065 			 */
21066 			if (ddi_copyout(&un->un_tgt_blocksize, (void *)arg,
21067 			    sizeof (int), flag) != 0) {
21068 				err = EFAULT;
21069 			} else {
21070 				err = 0;
21071 			}
21072 
21073 		} else {
21074 			/*
21075 			 * Drive supports changing block sizes via a Mode
21076 			 * Select.
21077 			 */
21078 			err = sr_change_blkmode(dev, cmd, arg, flag);
21079 		}
21080 		break;
21081 
21082 	case CDROMGDRVSPEED:
21083 	case CDROMSDRVSPEED:
21084 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMXDRVSPEED\n");
21085 		if (!ISCD(un)) {
21086 			err = ENOTTY;
21087 		} else if (un->un_f_mmc_cap == TRUE) {
21088 			/*
21089 			 * Note: In the future the driver implementation
21090 			 * for getting and
21091 			 * setting cd speed should entail:
21092 			 * 1) If non-mmc try the Toshiba mode page
21093 			 *    (sr_change_speed)
21094 			 * 2) If mmc but no support for Real Time Streaming try
21095 			 *    the SET CD SPEED (0xBB) command
21096 			 *   (sr_atapi_change_speed)
21097 			 * 3) If mmc and support for Real Time Streaming
21098 			 *    try the GET PERFORMANCE and SET STREAMING
21099 			 *    commands (not yet implemented, 4380808)
21100 			 */
21101 			/*
21102 			 * As per recent MMC spec, CD-ROM speed is variable
21103 			 * and changes with LBA. Since there is no such
21104 			 * things as drive speed now, fail this ioctl.
21105 			 *
21106 			 * Note: EINVAL is returned for consistancy of original
21107 			 * implementation which included support for getting
21108 			 * the drive speed of mmc devices but not setting
21109 			 * the drive speed. Thus EINVAL would be returned
21110 			 * if a set request was made for an mmc device.
21111 			 * We no longer support get or set speed for
21112 			 * mmc but need to remain consistant with regard
21113 			 * to the error code returned.
21114 			 */
21115 			err = EINVAL;
21116 		} else if (un->un_f_cfg_is_atapi == TRUE) {
21117 			err = sr_atapi_change_speed(dev, cmd, arg, flag);
21118 		} else {
21119 			err = sr_change_speed(dev, cmd, arg, flag);
21120 		}
21121 		break;
21122 
21123 	case CDROMCDDA:
21124 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDDA\n");
21125 		if (!ISCD(un)) {
21126 			err = ENOTTY;
21127 		} else {
21128 			err = sr_read_cdda(dev, (void *)arg, flag);
21129 		}
21130 		break;
21131 
21132 	case CDROMCDXA:
21133 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMCDXA\n");
21134 		if (!ISCD(un)) {
21135 			err = ENOTTY;
21136 		} else {
21137 			err = sr_read_cdxa(dev, (caddr_t)arg, flag);
21138 		}
21139 		break;
21140 
21141 	case CDROMSUBCODE:
21142 		SD_TRACE(SD_LOG_IOCTL, un, "CDROMSUBCODE\n");
21143 		if (!ISCD(un)) {
21144 			err = ENOTTY;
21145 		} else {
21146 			err = sr_read_all_subcodes(dev, (caddr_t)arg, flag);
21147 		}
21148 		break;
21149 
21150 	case DKIOCPARTINFO: {
21151 		/*
21152 		 * Return parameters describing the selected disk slice.
21153 		 * Note: this ioctl is for the intel platform only
21154 		 */
21155 #if defined(__i386) || defined(__amd64)
21156 		int part;
21157 
21158 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
21159 		part = SDPART(dev);
21160 
21161 		/* don't check un_solaris_size for pN */
21162 		if (part < P0_RAW_DISK && un->un_solaris_size == 0) {
21163 			err = EIO;
21164 		} else {
21165 			struct part_info p;
21166 
21167 			p.p_start = (daddr_t)un->un_offset[part];
21168 			p.p_length = (int)un->un_map[part].dkl_nblk;
21169 #ifdef _MULTI_DATAMODEL
21170 			switch (ddi_model_convert_from(flag & FMODELS)) {
21171 			case DDI_MODEL_ILP32:
21172 			{
21173 				struct part_info32 p32;
21174 
21175 				p32.p_start = (daddr32_t)p.p_start;
21176 				p32.p_length = p.p_length;
21177 				if (ddi_copyout(&p32, (void *)arg,
21178 				    sizeof (p32), flag))
21179 					err = EFAULT;
21180 				break;
21181 			}
21182 
21183 			case DDI_MODEL_NONE:
21184 			{
21185 				if (ddi_copyout(&p, (void *)arg, sizeof (p),
21186 				    flag))
21187 					err = EFAULT;
21188 				break;
21189 			}
21190 			}
21191 #else /* ! _MULTI_DATAMODEL */
21192 			if (ddi_copyout(&p, (void *)arg, sizeof (p), flag))
21193 				err = EFAULT;
21194 #endif /* _MULTI_DATAMODEL */
21195 		}
21196 #else
21197 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCPARTINFO\n");
21198 		err = ENOTTY;
21199 #endif
21200 		break;
21201 	}
21202 
21203 	case DKIOCG_PHYGEOM: {
21204 		/* Return the driver's notion of the media physical geometry */
21205 #if defined(__i386) || defined(__amd64)
21206 		struct dk_geom	disk_geom;
21207 		struct dk_geom	*dkgp = &disk_geom;
21208 
21209 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
21210 		mutex_enter(SD_MUTEX(un));
21211 
21212 		if (un->un_g.dkg_nhead != 0 &&
21213 		    un->un_g.dkg_nsect != 0) {
21214 			/*
21215 			 * We succeeded in getting a geometry, but
21216 			 * right now it is being reported as just the
21217 			 * Solaris fdisk partition, just like for
21218 			 * DKIOCGGEOM. We need to change that to be
21219 			 * correct for the entire disk now.
21220 			 */
21221 			bcopy(&un->un_g, dkgp, sizeof (*dkgp));
21222 			dkgp->dkg_acyl = 0;
21223 			dkgp->dkg_ncyl = un->un_blockcount /
21224 			    (dkgp->dkg_nhead * dkgp->dkg_nsect);
21225 		} else {
21226 			bzero(dkgp, sizeof (struct dk_geom));
21227 			/*
21228 			 * This disk does not have a Solaris VTOC
21229 			 * so we must present a physical geometry
21230 			 * that will remain consistent regardless
21231 			 * of how the disk is used. This will ensure
21232 			 * that the geometry does not change regardless
21233 			 * of the fdisk partition type (ie. EFI, FAT32,
21234 			 * Solaris, etc).
21235 			 */
21236 			if (ISCD(un)) {
21237 				dkgp->dkg_nhead = un->un_pgeom.g_nhead;
21238 				dkgp->dkg_nsect = un->un_pgeom.g_nsect;
21239 				dkgp->dkg_ncyl = un->un_pgeom.g_ncyl;
21240 				dkgp->dkg_acyl = un->un_pgeom.g_acyl;
21241 			} else {
21242 				sd_convert_geometry(un->un_blockcount, dkgp);
21243 				dkgp->dkg_acyl = 0;
21244 				dkgp->dkg_ncyl = un->un_blockcount /
21245 				    (dkgp->dkg_nhead * dkgp->dkg_nsect);
21246 			}
21247 		}
21248 		dkgp->dkg_pcyl = dkgp->dkg_ncyl + dkgp->dkg_acyl;
21249 
21250 		if (ddi_copyout(dkgp, (void *)arg,
21251 		    sizeof (struct dk_geom), flag)) {
21252 			mutex_exit(SD_MUTEX(un));
21253 			err = EFAULT;
21254 		} else {
21255 			mutex_exit(SD_MUTEX(un));
21256 			err = 0;
21257 		}
21258 #else
21259 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_PHYGEOM\n");
21260 		err = ENOTTY;
21261 #endif
21262 		break;
21263 	}
21264 
21265 	case DKIOCG_VIRTGEOM: {
21266 		/* Return the driver's notion of the media's logical geometry */
21267 #if defined(__i386) || defined(__amd64)
21268 		struct dk_geom	disk_geom;
21269 		struct dk_geom	*dkgp = &disk_geom;
21270 
21271 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
21272 		mutex_enter(SD_MUTEX(un));
21273 		/*
21274 		 * If there is no HBA geometry available, or
21275 		 * if the HBA returned us something that doesn't
21276 		 * really fit into an Int 13/function 8 geometry
21277 		 * result, just fail the ioctl.  See PSARC 1998/313.
21278 		 */
21279 		if (un->un_lgeom.g_nhead == 0 ||
21280 		    un->un_lgeom.g_nsect == 0 ||
21281 		    un->un_lgeom.g_ncyl > 1024) {
21282 			mutex_exit(SD_MUTEX(un));
21283 			err = EINVAL;
21284 		} else {
21285 			dkgp->dkg_ncyl	= un->un_lgeom.g_ncyl;
21286 			dkgp->dkg_acyl	= un->un_lgeom.g_acyl;
21287 			dkgp->dkg_pcyl	= dkgp->dkg_ncyl + dkgp->dkg_acyl;
21288 			dkgp->dkg_nhead	= un->un_lgeom.g_nhead;
21289 			dkgp->dkg_nsect	= un->un_lgeom.g_nsect;
21290 
21291 			if (ddi_copyout(dkgp, (void *)arg,
21292 			    sizeof (struct dk_geom), flag)) {
21293 				mutex_exit(SD_MUTEX(un));
21294 				err = EFAULT;
21295 			} else {
21296 				mutex_exit(SD_MUTEX(un));
21297 				err = 0;
21298 			}
21299 		}
21300 #else
21301 		SD_TRACE(SD_LOG_IOCTL, un, "DKIOCG_VIRTGEOM\n");
21302 		err = ENOTTY;
21303 #endif
21304 		break;
21305 	}
21306 #ifdef SDDEBUG
21307 /* RESET/ABORTS testing ioctls */
21308 	case DKIOCRESET: {
21309 		int	reset_level;
21310 
21311 		if (ddi_copyin((void *)arg, &reset_level, sizeof (int), flag)) {
21312 			err = EFAULT;
21313 		} else {
21314 			SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCRESET: "
21315 			    "reset_level = 0x%lx\n", reset_level);
21316 			if (scsi_reset(SD_ADDRESS(un), reset_level)) {
21317 				err = 0;
21318 			} else {
21319 				err = EIO;
21320 			}
21321 		}
21322 		break;
21323 	}
21324 
21325 	case DKIOCABORT:
21326 		SD_INFO(SD_LOG_IOCTL, un, "sdioctl: DKIOCABORT:\n");
21327 		if (scsi_abort(SD_ADDRESS(un), NULL)) {
21328 			err = 0;
21329 		} else {
21330 			err = EIO;
21331 		}
21332 		break;
21333 #endif
21334 
21335 #ifdef SD_FAULT_INJECTION
21336 /* SDIOC FaultInjection testing ioctls */
21337 	case SDIOCSTART:
21338 	case SDIOCSTOP:
21339 	case SDIOCINSERTPKT:
21340 	case SDIOCINSERTXB:
21341 	case SDIOCINSERTUN:
21342 	case SDIOCINSERTARQ:
21343 	case SDIOCPUSH:
21344 	case SDIOCRETRIEVE:
21345 	case SDIOCRUN:
21346 		SD_INFO(SD_LOG_SDTEST, un, "sdioctl:"
21347 		    "SDIOC detected cmd:0x%X:\n", cmd);
21348 		/* call error generator */
21349 		sd_faultinjection_ioctl(cmd, arg, un);
21350 		err = 0;
21351 		break;
21352 
21353 #endif /* SD_FAULT_INJECTION */
21354 
21355 	default:
21356 		err = ENOTTY;
21357 		break;
21358 	}
21359 	mutex_enter(SD_MUTEX(un));
21360 	un->un_ncmds_in_driver--;
21361 	ASSERT(un->un_ncmds_in_driver >= 0);
21362 	mutex_exit(SD_MUTEX(un));
21363 
21364 	SD_TRACE(SD_LOG_IOCTL, un, "sdioctl: exit: %d\n", err);
21365 	return (err);
21366 }
21367 
21368 
21369 /*
21370  *    Function: sd_uscsi_ioctl
21371  *
21372  * Description: This routine is the driver entry point for handling USCSI ioctl
21373  *		requests (USCSICMD).
21374  *
21375  *   Arguments: dev	- the device number
21376  *		arg	- user provided scsi command
21377  *		flag	- this argument is a pass through to ddi_copyxxx()
21378  *			  directly from the mode argument of ioctl().
21379  *
21380  * Return Code: code returned by sd_send_scsi_cmd
21381  *		ENXIO
21382  *		EFAULT
21383  *		EAGAIN
21384  */
21385 
21386 static int
21387 sd_uscsi_ioctl(dev_t dev, caddr_t arg, int flag)
21388 {
21389 #ifdef _MULTI_DATAMODEL
21390 	/*
21391 	 * For use when a 32 bit app makes a call into a
21392 	 * 64 bit ioctl
21393 	 */
21394 	struct uscsi_cmd32	uscsi_cmd_32_for_64;
21395 	struct uscsi_cmd32	*ucmd32 = &uscsi_cmd_32_for_64;
21396 	model_t			model;
21397 #endif /* _MULTI_DATAMODEL */
21398 	struct uscsi_cmd	*scmd = NULL;
21399 	struct sd_lun		*un = NULL;
21400 	enum uio_seg		uioseg;
21401 	char			cdb[CDB_GROUP0];
21402 	int			rval = 0;
21403 
21404 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21405 		return (ENXIO);
21406 	}
21407 
21408 	SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: entry: un:0x%p\n", un);
21409 
21410 	scmd = (struct uscsi_cmd *)
21411 	    kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
21412 
21413 #ifdef _MULTI_DATAMODEL
21414 	switch (model = ddi_model_convert_from(flag & FMODELS)) {
21415 	case DDI_MODEL_ILP32:
21416 	{
21417 		if (ddi_copyin((void *)arg, ucmd32, sizeof (*ucmd32), flag)) {
21418 			rval = EFAULT;
21419 			goto done;
21420 		}
21421 		/*
21422 		 * Convert the ILP32 uscsi data from the
21423 		 * application to LP64 for internal use.
21424 		 */
21425 		uscsi_cmd32touscsi_cmd(ucmd32, scmd);
21426 		break;
21427 	}
21428 	case DDI_MODEL_NONE:
21429 		if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) {
21430 			rval = EFAULT;
21431 			goto done;
21432 		}
21433 		break;
21434 	}
21435 #else /* ! _MULTI_DATAMODEL */
21436 	if (ddi_copyin((void *)arg, scmd, sizeof (*scmd), flag)) {
21437 		rval = EFAULT;
21438 		goto done;
21439 	}
21440 #endif /* _MULTI_DATAMODEL */
21441 
21442 	scmd->uscsi_flags &= ~USCSI_NOINTR;
21443 	uioseg = (flag & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE;
21444 	if (un->un_f_format_in_progress == TRUE) {
21445 		rval = EAGAIN;
21446 		goto done;
21447 	}
21448 
21449 	/*
21450 	 * Gotta do the ddi_copyin() here on the uscsi_cdb so that
21451 	 * we will have a valid cdb[0] to test.
21452 	 */
21453 	if ((ddi_copyin(scmd->uscsi_cdb, cdb, CDB_GROUP0, flag) == 0) &&
21454 	    (cdb[0] == SCMD_FORMAT)) {
21455 		SD_TRACE(SD_LOG_IOCTL, un,
21456 		    "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]);
21457 		mutex_enter(SD_MUTEX(un));
21458 		un->un_f_format_in_progress = TRUE;
21459 		mutex_exit(SD_MUTEX(un));
21460 		rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg,
21461 		    SD_PATH_STANDARD);
21462 		mutex_enter(SD_MUTEX(un));
21463 		un->un_f_format_in_progress = FALSE;
21464 		mutex_exit(SD_MUTEX(un));
21465 	} else {
21466 		SD_TRACE(SD_LOG_IOCTL, un,
21467 		    "sd_uscsi_ioctl: scmd->uscsi_cdb 0x%x\n", cdb[0]);
21468 		/*
21469 		 * It's OK to fall into here even if the ddi_copyin()
21470 		 * on the uscsi_cdb above fails, because sd_send_scsi_cmd()
21471 		 * does this same copyin and will return the EFAULT
21472 		 * if it fails.
21473 		 */
21474 		rval = sd_send_scsi_cmd(dev, scmd, uioseg, uioseg, uioseg,
21475 		    SD_PATH_STANDARD);
21476 	}
21477 #ifdef _MULTI_DATAMODEL
21478 	switch (model) {
21479 	case DDI_MODEL_ILP32:
21480 		/*
21481 		 * Convert back to ILP32 before copyout to the
21482 		 * application
21483 		 */
21484 		uscsi_cmdtouscsi_cmd32(scmd, ucmd32);
21485 		if (ddi_copyout(ucmd32, (void *)arg, sizeof (*ucmd32), flag)) {
21486 			if (rval != 0) {
21487 				rval = EFAULT;
21488 			}
21489 		}
21490 		break;
21491 	case DDI_MODEL_NONE:
21492 		if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) {
21493 			if (rval != 0) {
21494 				rval = EFAULT;
21495 			}
21496 		}
21497 		break;
21498 	}
21499 #else /* ! _MULTI_DATAMODE */
21500 	if (ddi_copyout(scmd, (void *)arg, sizeof (*scmd), flag)) {
21501 		if (rval != 0) {
21502 			rval = EFAULT;
21503 		}
21504 	}
21505 #endif /* _MULTI_DATAMODE */
21506 done:
21507 	kmem_free(scmd, sizeof (struct uscsi_cmd));
21508 
21509 	SD_TRACE(SD_LOG_IOCTL, un, "sd_uscsi_ioctl: exit: un:0x%p\n", un);
21510 
21511 	return (rval);
21512 }
21513 
21514 
21515 /*
21516  *    Function: sd_dkio_ctrl_info
21517  *
21518  * Description: This routine is the driver entry point for handling controller
21519  *		information ioctl requests (DKIOCINFO).
21520  *
21521  *   Arguments: dev  - the device number
21522  *		arg  - pointer to user provided dk_cinfo structure
21523  *		       specifying the controller type and attributes.
21524  *		flag - this argument is a pass through to ddi_copyxxx()
21525  *		       directly from the mode argument of ioctl().
21526  *
21527  * Return Code: 0
21528  *		EFAULT
21529  *		ENXIO
21530  */
21531 
21532 static int
21533 sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag)
21534 {
21535 	struct sd_lun	*un = NULL;
21536 	struct dk_cinfo	*info;
21537 	dev_info_t	*pdip;
21538 
21539 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21540 		return (ENXIO);
21541 	}
21542 
21543 	info = (struct dk_cinfo *)
21544 		kmem_zalloc(sizeof (struct dk_cinfo), KM_SLEEP);
21545 
21546 	switch (un->un_ctype) {
21547 	case CTYPE_CDROM:
21548 		info->dki_ctype = DKC_CDROM;
21549 		break;
21550 	default:
21551 		info->dki_ctype = DKC_SCSI_CCS;
21552 		break;
21553 	}
21554 	pdip = ddi_get_parent(SD_DEVINFO(un));
21555 	info->dki_cnum = ddi_get_instance(pdip);
21556 	if (strlen(ddi_get_name(pdip)) < DK_DEVLEN) {
21557 		(void) strcpy(info->dki_cname, ddi_get_name(pdip));
21558 	} else {
21559 		(void) strncpy(info->dki_cname, ddi_node_name(pdip),
21560 		    DK_DEVLEN - 1);
21561 	}
21562 
21563 	/* Unit Information */
21564 	info->dki_unit = ddi_get_instance(SD_DEVINFO(un));
21565 	info->dki_slave = ((SD_TARGET(un) << 3) | SD_LUN(un));
21566 	(void) strncpy(info->dki_dname, ddi_driver_name(SD_DEVINFO(un)),
21567 	    DK_DEVLEN - 1);
21568 	info->dki_flags = DKI_FMTVOL;
21569 	info->dki_partition = SDPART(dev);
21570 
21571 	/* Max Transfer size of this device in blocks */
21572 	info->dki_maxtransfer = un->un_max_xfer_size / un->un_sys_blocksize;
21573 	info->dki_addr = 0;
21574 	info->dki_space = 0;
21575 	info->dki_prio = 0;
21576 	info->dki_vec = 0;
21577 
21578 	if (ddi_copyout(info, arg, sizeof (struct dk_cinfo), flag) != 0) {
21579 		kmem_free(info, sizeof (struct dk_cinfo));
21580 		return (EFAULT);
21581 	} else {
21582 		kmem_free(info, sizeof (struct dk_cinfo));
21583 		return (0);
21584 	}
21585 }
21586 
21587 
21588 /*
21589  *    Function: sd_get_media_info
21590  *
21591  * Description: This routine is the driver entry point for handling ioctl
21592  *		requests for the media type or command set profile used by the
21593  *		drive to operate on the media (DKIOCGMEDIAINFO).
21594  *
21595  *   Arguments: dev	- the device number
21596  *		arg	- pointer to user provided dk_minfo structure
21597  *			  specifying the media type, logical block size and
21598  *			  drive capacity.
21599  *		flag	- this argument is a pass through to ddi_copyxxx()
21600  *			  directly from the mode argument of ioctl().
21601  *
21602  * Return Code: 0
21603  *		EACCESS
21604  *		EFAULT
21605  *		ENXIO
21606  *		EIO
21607  */
21608 
21609 static int
21610 sd_get_media_info(dev_t dev, caddr_t arg, int flag)
21611 {
21612 	struct sd_lun		*un = NULL;
21613 	struct uscsi_cmd	com;
21614 	struct scsi_inquiry	*sinq;
21615 	struct dk_minfo		media_info;
21616 	u_longlong_t		media_capacity;
21617 	uint64_t		capacity;
21618 	uint_t			lbasize;
21619 	uchar_t			*out_data;
21620 	uchar_t			*rqbuf;
21621 	int			rval = 0;
21622 	int			rtn;
21623 
21624 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
21625 	    (un->un_state == SD_STATE_OFFLINE)) {
21626 		return (ENXIO);
21627 	}
21628 
21629 	SD_TRACE(SD_LOG_IOCTL_DKIO, un, "sd_get_media_info: entry\n");
21630 
21631 	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
21632 	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
21633 
21634 	/* Issue a TUR to determine if the drive is ready with media present */
21635 	rval = sd_send_scsi_TEST_UNIT_READY(un, SD_CHECK_FOR_MEDIA);
21636 	if (rval == ENXIO) {
21637 		goto done;
21638 	}
21639 
21640 	/* Now get configuration data */
21641 	if (ISCD(un)) {
21642 		media_info.dki_media_type = DK_CDROM;
21643 
21644 		/* Allow SCMD_GET_CONFIGURATION to MMC devices only */
21645 		if (un->un_f_mmc_cap == TRUE) {
21646 			rtn = sd_send_scsi_GET_CONFIGURATION(un, &com, rqbuf,
21647 				SENSE_LENGTH, out_data, SD_PROFILE_HEADER_LEN);
21648 
21649 			if (rtn) {
21650 				/*
21651 				 * Failed for other than an illegal request
21652 				 * or command not supported
21653 				 */
21654 				if ((com.uscsi_status == STATUS_CHECK) &&
21655 				    (com.uscsi_rqstatus == STATUS_GOOD)) {
21656 					if ((rqbuf[2] != KEY_ILLEGAL_REQUEST) ||
21657 					    (rqbuf[12] != 0x20)) {
21658 						rval = EIO;
21659 						goto done;
21660 					}
21661 				}
21662 			} else {
21663 				/*
21664 				 * The GET CONFIGURATION command succeeded
21665 				 * so set the media type according to the
21666 				 * returned data
21667 				 */
21668 				media_info.dki_media_type = out_data[6];
21669 				media_info.dki_media_type <<= 8;
21670 				media_info.dki_media_type |= out_data[7];
21671 			}
21672 		}
21673 	} else {
21674 		/*
21675 		 * The profile list is not available, so we attempt to identify
21676 		 * the media type based on the inquiry data
21677 		 */
21678 		sinq = un->un_sd->sd_inq;
21679 		if (sinq->inq_qual == 0) {
21680 			/* This is a direct access device */
21681 			media_info.dki_media_type = DK_FIXED_DISK;
21682 
21683 			if ((bcmp(sinq->inq_vid, "IOMEGA", 6) == 0) ||
21684 			    (bcmp(sinq->inq_vid, "iomega", 6) == 0)) {
21685 				if ((bcmp(sinq->inq_pid, "ZIP", 3) == 0)) {
21686 					media_info.dki_media_type = DK_ZIP;
21687 				} else if (
21688 				    (bcmp(sinq->inq_pid, "jaz", 3) == 0)) {
21689 					media_info.dki_media_type = DK_JAZ;
21690 				}
21691 			}
21692 		} else {
21693 			/* Not a CD or direct access so return unknown media */
21694 			media_info.dki_media_type = DK_UNKNOWN;
21695 		}
21696 	}
21697 
21698 	/* Now read the capacity so we can provide the lbasize and capacity */
21699 	switch (sd_send_scsi_READ_CAPACITY(un, &capacity, &lbasize,
21700 	    SD_PATH_DIRECT)) {
21701 	case 0:
21702 		break;
21703 	case EACCES:
21704 		rval = EACCES;
21705 		goto done;
21706 	default:
21707 		rval = EIO;
21708 		goto done;
21709 	}
21710 
21711 	media_info.dki_lbsize = lbasize;
21712 	media_capacity = capacity;
21713 
21714 	/*
21715 	 * sd_send_scsi_READ_CAPACITY() reports capacity in
21716 	 * un->un_sys_blocksize chunks. So we need to convert it into
21717 	 * cap.lbasize chunks.
21718 	 */
21719 	media_capacity *= un->un_sys_blocksize;
21720 	media_capacity /= lbasize;
21721 	media_info.dki_capacity = media_capacity;
21722 
21723 	if (ddi_copyout(&media_info, arg, sizeof (struct dk_minfo), flag)) {
21724 		rval = EFAULT;
21725 		/* Put goto. Anybody might add some code below in future */
21726 		goto done;
21727 	}
21728 done:
21729 	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
21730 	kmem_free(rqbuf, SENSE_LENGTH);
21731 	return (rval);
21732 }
21733 
21734 
21735 /*
21736  *    Function: sd_dkio_get_geometry
21737  *
21738  * Description: This routine is the driver entry point for handling user
21739  *		requests to get the device geometry (DKIOCGGEOM).
21740  *
21741  *   Arguments: dev  - the device number
21742  *		arg  - pointer to user provided dk_geom structure specifying
21743  *			the controller's notion of the current geometry.
21744  *		flag - this argument is a pass through to ddi_copyxxx()
21745  *		       directly from the mode argument of ioctl().
21746  *		geom_validated - flag indicating if the device geometry has been
21747  *				 previously validated in the sdioctl routine.
21748  *
21749  * Return Code: 0
21750  *		EFAULT
21751  *		ENXIO
21752  *		EIO
21753  */
21754 
21755 static int
21756 sd_dkio_get_geometry(dev_t dev, caddr_t arg, int flag, int geom_validated)
21757 {
21758 	struct sd_lun	*un = NULL;
21759 	struct dk_geom	*tmp_geom = NULL;
21760 	int		rval = 0;
21761 
21762 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21763 		return (ENXIO);
21764 	}
21765 
21766 #if defined(__i386) || defined(__amd64)
21767 	if (un->un_solaris_size == 0) {
21768 		return (EIO);
21769 	}
21770 #endif
21771 	if (geom_validated == FALSE) {
21772 		/*
21773 		 * sd_validate_geometry does not spin a disk up
21774 		 * if it was spun down. We need to make sure it
21775 		 * is ready.
21776 		 */
21777 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
21778 			return (rval);
21779 		}
21780 		mutex_enter(SD_MUTEX(un));
21781 		rval = sd_validate_geometry(un, SD_PATH_DIRECT);
21782 		mutex_exit(SD_MUTEX(un));
21783 	}
21784 	if (rval)
21785 		return (rval);
21786 
21787 	/*
21788 	 * Make a local copy of the soft state geometry to avoid some potential
21789 	 * race conditions associated with holding the mutex and updating the
21790 	 * write_reinstruct value
21791 	 */
21792 	tmp_geom = kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
21793 	mutex_enter(SD_MUTEX(un));
21794 	bcopy(&un->un_g, tmp_geom, sizeof (struct dk_geom));
21795 	mutex_exit(SD_MUTEX(un));
21796 
21797 	if (tmp_geom->dkg_write_reinstruct == 0) {
21798 		tmp_geom->dkg_write_reinstruct =
21799 		    (int)((int)(tmp_geom->dkg_nsect * tmp_geom->dkg_rpm *
21800 		    sd_rot_delay) / (int)60000);
21801 	}
21802 
21803 	rval = ddi_copyout(tmp_geom, (void *)arg, sizeof (struct dk_geom),
21804 	    flag);
21805 	if (rval != 0) {
21806 		rval = EFAULT;
21807 	}
21808 
21809 	kmem_free(tmp_geom, sizeof (struct dk_geom));
21810 	return (rval);
21811 
21812 }
21813 
21814 
21815 /*
21816  *    Function: sd_dkio_set_geometry
21817  *
21818  * Description: This routine is the driver entry point for handling user
21819  *		requests to set the device geometry (DKIOCSGEOM). The actual
21820  *		device geometry is not updated, just the driver "notion" of it.
21821  *
21822  *   Arguments: dev  - the device number
21823  *		arg  - pointer to user provided dk_geom structure used to set
21824  *			the controller's notion of the current geometry.
21825  *		flag - this argument is a pass through to ddi_copyxxx()
21826  *		       directly from the mode argument of ioctl().
21827  *
21828  * Return Code: 0
21829  *		EFAULT
21830  *		ENXIO
21831  *		EIO
21832  */
21833 
21834 static int
21835 sd_dkio_set_geometry(dev_t dev, caddr_t arg, int flag)
21836 {
21837 	struct sd_lun	*un = NULL;
21838 	struct dk_geom	*tmp_geom;
21839 	struct dk_map	*lp;
21840 	int		rval = 0;
21841 	int		i;
21842 
21843 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21844 		return (ENXIO);
21845 	}
21846 
21847 #if defined(__i386) || defined(__amd64)
21848 	if (un->un_solaris_size == 0) {
21849 		return (EIO);
21850 	}
21851 #endif
21852 	/*
21853 	 * We need to copy the user specified geometry into local
21854 	 * storage and then update the softstate. We don't want to hold
21855 	 * the mutex and copyin directly from the user to the soft state
21856 	 */
21857 	tmp_geom = (struct dk_geom *)
21858 	    kmem_zalloc(sizeof (struct dk_geom), KM_SLEEP);
21859 	rval = ddi_copyin(arg, tmp_geom, sizeof (struct dk_geom), flag);
21860 	if (rval != 0) {
21861 		kmem_free(tmp_geom, sizeof (struct dk_geom));
21862 		return (EFAULT);
21863 	}
21864 
21865 	mutex_enter(SD_MUTEX(un));
21866 	bcopy(tmp_geom, &un->un_g, sizeof (struct dk_geom));
21867 	for (i = 0; i < NDKMAP; i++) {
21868 		lp  = &un->un_map[i];
21869 		un->un_offset[i] =
21870 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
21871 #if defined(__i386) || defined(__amd64)
21872 		un->un_offset[i] += un->un_solaris_offset;
21873 #endif
21874 	}
21875 	un->un_f_geometry_is_valid = FALSE;
21876 	mutex_exit(SD_MUTEX(un));
21877 	kmem_free(tmp_geom, sizeof (struct dk_geom));
21878 
21879 	return (rval);
21880 }
21881 
21882 
21883 /*
21884  *    Function: sd_dkio_get_partition
21885  *
21886  * Description: This routine is the driver entry point for handling user
21887  *		requests to get the partition table (DKIOCGAPART).
21888  *
21889  *   Arguments: dev  - the device number
21890  *		arg  - pointer to user provided dk_allmap structure specifying
21891  *			the controller's notion of the current partition table.
21892  *		flag - this argument is a pass through to ddi_copyxxx()
21893  *		       directly from the mode argument of ioctl().
21894  *		geom_validated - flag indicating if the device geometry has been
21895  *				 previously validated in the sdioctl routine.
21896  *
21897  * Return Code: 0
21898  *		EFAULT
21899  *		ENXIO
21900  *		EIO
21901  */
21902 
21903 static int
21904 sd_dkio_get_partition(dev_t dev, caddr_t arg, int flag, int geom_validated)
21905 {
21906 	struct sd_lun	*un = NULL;
21907 	int		rval = 0;
21908 	int		size;
21909 
21910 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
21911 		return (ENXIO);
21912 	}
21913 
21914 #if defined(__i386) || defined(__amd64)
21915 	if (un->un_solaris_size == 0) {
21916 		return (EIO);
21917 	}
21918 #endif
21919 	/*
21920 	 * Make sure the geometry is valid before getting the partition
21921 	 * information.
21922 	 */
21923 	mutex_enter(SD_MUTEX(un));
21924 	if (geom_validated == FALSE) {
21925 		/*
21926 		 * sd_validate_geometry does not spin a disk up
21927 		 * if it was spun down. We need to make sure it
21928 		 * is ready before validating the geometry.
21929 		 */
21930 		mutex_exit(SD_MUTEX(un));
21931 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
21932 			return (rval);
21933 		}
21934 		mutex_enter(SD_MUTEX(un));
21935 
21936 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
21937 			mutex_exit(SD_MUTEX(un));
21938 			return (rval);
21939 		}
21940 	}
21941 	mutex_exit(SD_MUTEX(un));
21942 
21943 #ifdef _MULTI_DATAMODEL
21944 	switch (ddi_model_convert_from(flag & FMODELS)) {
21945 	case DDI_MODEL_ILP32: {
21946 		struct dk_map32 dk_map32[NDKMAP];
21947 		int		i;
21948 
21949 		for (i = 0; i < NDKMAP; i++) {
21950 			dk_map32[i].dkl_cylno = un->un_map[i].dkl_cylno;
21951 			dk_map32[i].dkl_nblk  = un->un_map[i].dkl_nblk;
21952 		}
21953 		size = NDKMAP * sizeof (struct dk_map32);
21954 		rval = ddi_copyout(dk_map32, (void *)arg, size, flag);
21955 		if (rval != 0) {
21956 			rval = EFAULT;
21957 		}
21958 		break;
21959 	}
21960 	case DDI_MODEL_NONE:
21961 		size = NDKMAP * sizeof (struct dk_map);
21962 		rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
21963 		if (rval != 0) {
21964 			rval = EFAULT;
21965 		}
21966 		break;
21967 	}
21968 #else /* ! _MULTI_DATAMODEL */
21969 	size = NDKMAP * sizeof (struct dk_map);
21970 	rval = ddi_copyout(un->un_map, (void *)arg, size, flag);
21971 	if (rval != 0) {
21972 		rval = EFAULT;
21973 	}
21974 #endif /* _MULTI_DATAMODEL */
21975 	return (rval);
21976 }
21977 
21978 
21979 /*
21980  *    Function: sd_dkio_set_partition
21981  *
21982  * Description: This routine is the driver entry point for handling user
21983  *		requests to set the partition table (DKIOCSAPART). The actual
21984  *		device partition is not updated.
21985  *
21986  *   Arguments: dev  - the device number
21987  *		arg  - pointer to user provided dk_allmap structure used to set
21988  *			the controller's notion of the partition table.
21989  *		flag - this argument is a pass through to ddi_copyxxx()
21990  *		       directly from the mode argument of ioctl().
21991  *
21992  * Return Code: 0
21993  *		EINVAL
21994  *		EFAULT
21995  *		ENXIO
21996  *		EIO
21997  */
21998 
21999 static int
22000 sd_dkio_set_partition(dev_t dev, caddr_t arg, int flag)
22001 {
22002 	struct sd_lun	*un = NULL;
22003 	struct dk_map	dk_map[NDKMAP];
22004 	struct dk_map	*lp;
22005 	int		rval = 0;
22006 	int		size;
22007 	int		i;
22008 #if defined(_SUNOS_VTOC_16)
22009 	struct dkl_partition	*vp;
22010 #endif
22011 
22012 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22013 		return (ENXIO);
22014 	}
22015 
22016 	/*
22017 	 * Set the map for all logical partitions.  We lock
22018 	 * the priority just to make sure an interrupt doesn't
22019 	 * come in while the map is half updated.
22020 	 */
22021 	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::un_solaris_size))
22022 	mutex_enter(SD_MUTEX(un));
22023 	if (un->un_blockcount > DK_MAX_BLOCKS) {
22024 		mutex_exit(SD_MUTEX(un));
22025 		return (ENOTSUP);
22026 	}
22027 	mutex_exit(SD_MUTEX(un));
22028 	if (un->un_solaris_size == 0) {
22029 		return (EIO);
22030 	}
22031 
22032 #ifdef _MULTI_DATAMODEL
22033 	switch (ddi_model_convert_from(flag & FMODELS)) {
22034 	case DDI_MODEL_ILP32: {
22035 		struct dk_map32 dk_map32[NDKMAP];
22036 
22037 		size = NDKMAP * sizeof (struct dk_map32);
22038 		rval = ddi_copyin((void *)arg, dk_map32, size, flag);
22039 		if (rval != 0) {
22040 			return (EFAULT);
22041 		}
22042 		for (i = 0; i < NDKMAP; i++) {
22043 			dk_map[i].dkl_cylno = dk_map32[i].dkl_cylno;
22044 			dk_map[i].dkl_nblk  = dk_map32[i].dkl_nblk;
22045 		}
22046 		break;
22047 	}
22048 	case DDI_MODEL_NONE:
22049 		size = NDKMAP * sizeof (struct dk_map);
22050 		rval = ddi_copyin((void *)arg, dk_map, size, flag);
22051 		if (rval != 0) {
22052 			return (EFAULT);
22053 		}
22054 		break;
22055 	}
22056 #else /* ! _MULTI_DATAMODEL */
22057 	size = NDKMAP * sizeof (struct dk_map);
22058 	rval = ddi_copyin((void *)arg, dk_map, size, flag);
22059 	if (rval != 0) {
22060 		return (EFAULT);
22061 	}
22062 #endif /* _MULTI_DATAMODEL */
22063 
22064 	mutex_enter(SD_MUTEX(un));
22065 	/* Note: The size used in this bcopy is set based upon the data model */
22066 	bcopy(dk_map, un->un_map, size);
22067 #if defined(_SUNOS_VTOC_16)
22068 	vp = (struct dkl_partition *)&(un->un_vtoc);
22069 #endif	/* defined(_SUNOS_VTOC_16) */
22070 	for (i = 0; i < NDKMAP; i++) {
22071 		lp  = &un->un_map[i];
22072 		un->un_offset[i] =
22073 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
22074 #if defined(_SUNOS_VTOC_16)
22075 		vp->p_start = un->un_offset[i];
22076 		vp->p_size = lp->dkl_nblk;
22077 		vp++;
22078 #endif	/* defined(_SUNOS_VTOC_16) */
22079 #if defined(__i386) || defined(__amd64)
22080 		un->un_offset[i] += un->un_solaris_offset;
22081 #endif
22082 	}
22083 	mutex_exit(SD_MUTEX(un));
22084 	return (rval);
22085 }
22086 
22087 
22088 /*
22089  *    Function: sd_dkio_get_vtoc
22090  *
22091  * Description: This routine is the driver entry point for handling user
22092  *		requests to get the current volume table of contents
22093  *		(DKIOCGVTOC).
22094  *
22095  *   Arguments: dev  - the device number
22096  *		arg  - pointer to user provided vtoc structure specifying
22097  *			the current vtoc.
22098  *		flag - this argument is a pass through to ddi_copyxxx()
22099  *		       directly from the mode argument of ioctl().
22100  *		geom_validated - flag indicating if the device geometry has been
22101  *				 previously validated in the sdioctl routine.
22102  *
22103  * Return Code: 0
22104  *		EFAULT
22105  *		ENXIO
22106  *		EIO
22107  */
22108 
22109 static int
22110 sd_dkio_get_vtoc(dev_t dev, caddr_t arg, int flag, int geom_validated)
22111 {
22112 	struct sd_lun	*un = NULL;
22113 #if defined(_SUNOS_VTOC_8)
22114 	struct vtoc	user_vtoc;
22115 #endif	/* defined(_SUNOS_VTOC_8) */
22116 	int		rval = 0;
22117 
22118 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22119 		return (ENXIO);
22120 	}
22121 
22122 	mutex_enter(SD_MUTEX(un));
22123 	if (geom_validated == FALSE) {
22124 		/*
22125 		 * sd_validate_geometry does not spin a disk up
22126 		 * if it was spun down. We need to make sure it
22127 		 * is ready.
22128 		 */
22129 		mutex_exit(SD_MUTEX(un));
22130 		if ((rval = sd_send_scsi_TEST_UNIT_READY(un, 0)) != 0) {
22131 			return (rval);
22132 		}
22133 		mutex_enter(SD_MUTEX(un));
22134 		if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT)) != 0) {
22135 			mutex_exit(SD_MUTEX(un));
22136 			return (rval);
22137 		}
22138 	}
22139 
22140 #if defined(_SUNOS_VTOC_8)
22141 	sd_build_user_vtoc(un, &user_vtoc);
22142 	mutex_exit(SD_MUTEX(un));
22143 
22144 #ifdef _MULTI_DATAMODEL
22145 	switch (ddi_model_convert_from(flag & FMODELS)) {
22146 	case DDI_MODEL_ILP32: {
22147 		struct vtoc32 user_vtoc32;
22148 
22149 		vtoctovtoc32(user_vtoc, user_vtoc32);
22150 		if (ddi_copyout(&user_vtoc32, (void *)arg,
22151 		    sizeof (struct vtoc32), flag)) {
22152 			return (EFAULT);
22153 		}
22154 		break;
22155 	}
22156 
22157 	case DDI_MODEL_NONE:
22158 		if (ddi_copyout(&user_vtoc, (void *)arg,
22159 		    sizeof (struct vtoc), flag)) {
22160 			return (EFAULT);
22161 		}
22162 		break;
22163 	}
22164 #else /* ! _MULTI_DATAMODEL */
22165 	if (ddi_copyout(&user_vtoc, (void *)arg, sizeof (struct vtoc), flag)) {
22166 		return (EFAULT);
22167 	}
22168 #endif /* _MULTI_DATAMODEL */
22169 
22170 #elif defined(_SUNOS_VTOC_16)
22171 	mutex_exit(SD_MUTEX(un));
22172 
22173 #ifdef _MULTI_DATAMODEL
22174 	/*
22175 	 * The un_vtoc structure is a "struct dk_vtoc"  which is always
22176 	 * 32-bit to maintain compatibility with existing on-disk
22177 	 * structures.  Thus, we need to convert the structure when copying
22178 	 * it out to a datamodel-dependent "struct vtoc" in a 64-bit
22179 	 * program.  If the target is a 32-bit program, then no conversion
22180 	 * is necessary.
22181 	 */
22182 	/* LINTED: logical expression always true: op "||" */
22183 	ASSERT(sizeof (un->un_vtoc) == sizeof (struct vtoc32));
22184 	switch (ddi_model_convert_from(flag & FMODELS)) {
22185 	case DDI_MODEL_ILP32:
22186 		if (ddi_copyout(&(un->un_vtoc), (void *)arg,
22187 		    sizeof (un->un_vtoc), flag)) {
22188 			return (EFAULT);
22189 		}
22190 		break;
22191 
22192 	case DDI_MODEL_NONE: {
22193 		struct vtoc user_vtoc;
22194 
22195 		vtoc32tovtoc(un->un_vtoc, user_vtoc);
22196 		if (ddi_copyout(&user_vtoc, (void *)arg,
22197 		    sizeof (struct vtoc), flag)) {
22198 			return (EFAULT);
22199 		}
22200 		break;
22201 	}
22202 	}
22203 #else /* ! _MULTI_DATAMODEL */
22204 	if (ddi_copyout(&(un->un_vtoc), (void *)arg, sizeof (un->un_vtoc),
22205 	    flag)) {
22206 		return (EFAULT);
22207 	}
22208 #endif /* _MULTI_DATAMODEL */
22209 #else
22210 #error "No VTOC format defined."
22211 #endif
22212 
22213 	return (rval);
22214 }
22215 
22216 static int
22217 sd_dkio_get_efi(dev_t dev, caddr_t arg, int flag)
22218 {
22219 	struct sd_lun	*un = NULL;
22220 	dk_efi_t	user_efi;
22221 	int		rval = 0;
22222 	void		*buffer;
22223 
22224 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
22225 		return (ENXIO);
22226 
22227 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
22228 		return (EFAULT);
22229 
22230 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
22231 
22232 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
22233 	    (user_efi.dki_length > un->un_max_xfer_size))
22234 		return (EINVAL);
22235 
22236 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
22237 	rval = sd_send_scsi_READ(un, buffer, user_efi.dki_length,
22238 	    user_efi.dki_lba, SD_PATH_DIRECT);
22239 	if (rval == 0 && ddi_copyout(buffer, user_efi.dki_data,
22240 	    user_efi.dki_length, flag) != 0)
22241 		rval = EFAULT;
22242 
22243 	kmem_free(buffer, user_efi.dki_length);
22244 	return (rval);
22245 }
22246 
22247 /*
22248  *    Function: sd_build_user_vtoc
22249  *
22250  * Description: This routine populates a pass by reference variable with the
22251  *		current volume table of contents.
22252  *
22253  *   Arguments: un - driver soft state (unit) structure
22254  *		user_vtoc - pointer to vtoc structure to be populated
22255  */
22256 
22257 static void
22258 sd_build_user_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
22259 {
22260 	struct dk_map2		*lpart;
22261 	struct dk_map		*lmap;
22262 	struct partition	*vpart;
22263 	int			nblks;
22264 	int			i;
22265 
22266 	ASSERT(mutex_owned(SD_MUTEX(un)));
22267 
22268 	/*
22269 	 * Return vtoc structure fields in the provided VTOC area, addressed
22270 	 * by *vtoc.
22271 	 */
22272 	bzero(user_vtoc, sizeof (struct vtoc));
22273 	user_vtoc->v_bootinfo[0] = un->un_vtoc.v_bootinfo[0];
22274 	user_vtoc->v_bootinfo[1] = un->un_vtoc.v_bootinfo[1];
22275 	user_vtoc->v_bootinfo[2] = un->un_vtoc.v_bootinfo[2];
22276 	user_vtoc->v_sanity	= VTOC_SANE;
22277 	user_vtoc->v_version	= un->un_vtoc.v_version;
22278 	bcopy(un->un_vtoc.v_volume, user_vtoc->v_volume, LEN_DKL_VVOL);
22279 	user_vtoc->v_sectorsz = un->un_sys_blocksize;
22280 	user_vtoc->v_nparts = un->un_vtoc.v_nparts;
22281 	bcopy(un->un_vtoc.v_reserved, user_vtoc->v_reserved,
22282 	    sizeof (un->un_vtoc.v_reserved));
22283 	/*
22284 	 * Convert partitioning information.
22285 	 *
22286 	 * Note the conversion from starting cylinder number
22287 	 * to starting sector number.
22288 	 */
22289 	lmap = un->un_map;
22290 	lpart = (struct dk_map2 *)un->un_vtoc.v_part;
22291 	vpart = user_vtoc->v_part;
22292 
22293 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
22294 
22295 	for (i = 0; i < V_NUMPAR; i++) {
22296 		vpart->p_tag	= lpart->p_tag;
22297 		vpart->p_flag	= lpart->p_flag;
22298 		vpart->p_start	= lmap->dkl_cylno * nblks;
22299 		vpart->p_size	= lmap->dkl_nblk;
22300 		lmap++;
22301 		lpart++;
22302 		vpart++;
22303 
22304 		/* (4364927) */
22305 		user_vtoc->timestamp[i] = (time_t)un->un_vtoc.v_timestamp[i];
22306 	}
22307 
22308 	bcopy(un->un_asciilabel, user_vtoc->v_asciilabel, LEN_DKL_ASCII);
22309 }
22310 
22311 static int
22312 sd_dkio_partition(dev_t dev, caddr_t arg, int flag)
22313 {
22314 	struct sd_lun		*un = NULL;
22315 	struct partition64	p64;
22316 	int			rval = 0;
22317 	uint_t			nparts;
22318 	efi_gpe_t		*partitions;
22319 	efi_gpt_t		*buffer;
22320 	diskaddr_t		gpe_lba;
22321 
22322 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22323 		return (ENXIO);
22324 	}
22325 
22326 	if (ddi_copyin((const void *)arg, &p64,
22327 	    sizeof (struct partition64), flag)) {
22328 		return (EFAULT);
22329 	}
22330 
22331 	buffer = kmem_alloc(EFI_MIN_ARRAY_SIZE, KM_SLEEP);
22332 	rval = sd_send_scsi_READ(un, buffer, DEV_BSIZE,
22333 		1, SD_PATH_DIRECT);
22334 	if (rval != 0)
22335 		goto done_error;
22336 
22337 	sd_swap_efi_gpt(buffer);
22338 
22339 	if ((rval = sd_validate_efi(buffer)) != 0)
22340 		goto done_error;
22341 
22342 	nparts = buffer->efi_gpt_NumberOfPartitionEntries;
22343 	gpe_lba = buffer->efi_gpt_PartitionEntryLBA;
22344 	if (p64.p_partno > nparts) {
22345 		/* couldn't find it */
22346 		rval = ESRCH;
22347 		goto done_error;
22348 	}
22349 	/*
22350 	 * if we're dealing with a partition that's out of the normal
22351 	 * 16K block, adjust accordingly
22352 	 */
22353 	gpe_lba += p64.p_partno / sizeof (efi_gpe_t);
22354 	rval = sd_send_scsi_READ(un, buffer, EFI_MIN_ARRAY_SIZE,
22355 			gpe_lba, SD_PATH_DIRECT);
22356 	if (rval) {
22357 		goto done_error;
22358 	}
22359 	partitions = (efi_gpe_t *)buffer;
22360 
22361 	sd_swap_efi_gpe(nparts, partitions);
22362 
22363 	partitions += p64.p_partno;
22364 	bcopy(&partitions->efi_gpe_PartitionTypeGUID, &p64.p_type,
22365 	    sizeof (struct uuid));
22366 	p64.p_start = partitions->efi_gpe_StartingLBA;
22367 	p64.p_size = partitions->efi_gpe_EndingLBA -
22368 			p64.p_start + 1;
22369 
22370 	if (ddi_copyout(&p64, (void *)arg, sizeof (struct partition64), flag))
22371 		rval = EFAULT;
22372 
22373 done_error:
22374 	kmem_free(buffer, EFI_MIN_ARRAY_SIZE);
22375 	return (rval);
22376 }
22377 
22378 
22379 /*
22380  *    Function: sd_dkio_set_vtoc
22381  *
22382  * Description: This routine is the driver entry point for handling user
22383  *		requests to set the current volume table of contents
22384  *		(DKIOCSVTOC).
22385  *
22386  *   Arguments: dev  - the device number
22387  *		arg  - pointer to user provided vtoc structure used to set the
22388  *			current vtoc.
22389  *		flag - this argument is a pass through to ddi_copyxxx()
22390  *		       directly from the mode argument of ioctl().
22391  *
22392  * Return Code: 0
22393  *		EFAULT
22394  *		ENXIO
22395  *		EINVAL
22396  *		ENOTSUP
22397  */
22398 
22399 static int
22400 sd_dkio_set_vtoc(dev_t dev, caddr_t arg, int flag)
22401 {
22402 	struct sd_lun	*un = NULL;
22403 	struct vtoc	user_vtoc;
22404 	int		rval = 0;
22405 
22406 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
22407 		return (ENXIO);
22408 	}
22409 
22410 #if defined(__i386) || defined(__amd64)
22411 	if (un->un_tgt_blocksize != un->un_sys_blocksize) {
22412 		return (EINVAL);
22413 	}
22414 #endif
22415 
22416 #ifdef _MULTI_DATAMODEL
22417 	switch (ddi_model_convert_from(flag & FMODELS)) {
22418 	case DDI_MODEL_ILP32: {
22419 		struct vtoc32 user_vtoc32;
22420 
22421 		if (ddi_copyin((const void *)arg, &user_vtoc32,
22422 		    sizeof (struct vtoc32), flag)) {
22423 			return (EFAULT);
22424 		}
22425 		vtoc32tovtoc(user_vtoc32, user_vtoc);
22426 		break;
22427 	}
22428 
22429 	case DDI_MODEL_NONE:
22430 		if (ddi_copyin((const void *)arg, &user_vtoc,
22431 		    sizeof (struct vtoc), flag)) {
22432 			return (EFAULT);
22433 		}
22434 		break;
22435 	}
22436 #else /* ! _MULTI_DATAMODEL */
22437 	if (ddi_copyin((const void *)arg, &user_vtoc,
22438 	    sizeof (struct vtoc), flag)) {
22439 		return (EFAULT);
22440 	}
22441 #endif /* _MULTI_DATAMODEL */
22442 
22443 	mutex_enter(SD_MUTEX(un));
22444 	if (un->un_blockcount > DK_MAX_BLOCKS) {
22445 		mutex_exit(SD_MUTEX(un));
22446 		return (ENOTSUP);
22447 	}
22448 	if (un->un_g.dkg_ncyl == 0) {
22449 		mutex_exit(SD_MUTEX(un));
22450 		return (EINVAL);
22451 	}
22452 
22453 	mutex_exit(SD_MUTEX(un));
22454 	sd_clear_efi(un);
22455 	ddi_remove_minor_node(SD_DEVINFO(un), "wd");
22456 	ddi_remove_minor_node(SD_DEVINFO(un), "wd,raw");
22457 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h",
22458 	    S_IFBLK, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
22459 	    un->un_node_type, NULL);
22460 	(void) ddi_create_minor_node(SD_DEVINFO(un), "h,raw",
22461 	    S_IFCHR, (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
22462 	    un->un_node_type, NULL);
22463 	mutex_enter(SD_MUTEX(un));
22464 
22465 	if ((rval = sd_build_label_vtoc(un, &user_vtoc)) == 0) {
22466 		if ((rval = sd_write_label(dev)) == 0) {
22467 			if ((rval = sd_validate_geometry(un, SD_PATH_DIRECT))
22468 			    != 0) {
22469 				SD_ERROR(SD_LOG_IOCTL_DKIO, un,
22470 				    "sd_dkio_set_vtoc: "
22471 				    "Failed validate geometry\n");
22472 			}
22473 		}
22474 	}
22475 
22476 	/*
22477 	 * If sd_build_label_vtoc, or sd_write_label failed above write the
22478 	 * devid anyway, what can it hurt? Also preserve the device id by
22479 	 * writing to the disk acyl for the case where a devid has been
22480 	 * fabricated.
22481 	 */
22482 	if (!ISREMOVABLE(un) && !ISCD(un) &&
22483 	    (un->un_f_opt_fab_devid == TRUE)) {
22484 		if (un->un_devid == NULL) {
22485 			sd_register_devid(un, SD_DEVINFO(un),
22486 			    SD_TARGET_IS_UNRESERVED);
22487 		} else {
22488 			/*
22489 			 * The device id for this disk has been
22490 			 * fabricated. Fabricated device id's are
22491 			 * managed by storing them in the last 2
22492 			 * available sectors on the drive. The device
22493 			 * id must be preserved by writing it back out
22494 			 * to this location.
22495 			 */
22496 			if (sd_write_deviceid(un) != 0) {
22497 				ddi_devid_free(un->un_devid);
22498 				un->un_devid = NULL;
22499 			}
22500 		}
22501 	}
22502 	mutex_exit(SD_MUTEX(un));
22503 	return (rval);
22504 }
22505 
22506 
22507 /*
22508  *    Function: sd_build_label_vtoc
22509  *
22510  * Description: This routine updates the driver soft state current volume table
22511  *		of contents based on a user specified vtoc.
22512  *
22513  *   Arguments: un - driver soft state (unit) structure
22514  *		user_vtoc - pointer to vtoc structure specifying vtoc to be used
22515  *			    to update the driver soft state.
22516  *
22517  * Return Code: 0
22518  *		EINVAL
22519  */
22520 
22521 static int
22522 sd_build_label_vtoc(struct sd_lun *un, struct vtoc *user_vtoc)
22523 {
22524 	struct dk_map		*lmap;
22525 	struct partition	*vpart;
22526 	int			nblks;
22527 #if defined(_SUNOS_VTOC_8)
22528 	int			ncyl;
22529 	struct dk_map2		*lpart;
22530 #endif	/* defined(_SUNOS_VTOC_8) */
22531 	int			i;
22532 
22533 	ASSERT(mutex_owned(SD_MUTEX(un)));
22534 
22535 	/* Sanity-check the vtoc */
22536 	if (user_vtoc->v_sanity != VTOC_SANE ||
22537 	    user_vtoc->v_sectorsz != un->un_sys_blocksize ||
22538 	    user_vtoc->v_nparts != V_NUMPAR) {
22539 		return (EINVAL);
22540 	}
22541 
22542 	nblks = un->un_g.dkg_nsect * un->un_g.dkg_nhead;
22543 	if (nblks == 0) {
22544 		return (EINVAL);
22545 	}
22546 
22547 #if defined(_SUNOS_VTOC_8)
22548 	vpart = user_vtoc->v_part;
22549 	for (i = 0; i < V_NUMPAR; i++) {
22550 		if ((vpart->p_start % nblks) != 0) {
22551 			return (EINVAL);
22552 		}
22553 		ncyl = vpart->p_start / nblks;
22554 		ncyl += vpart->p_size / nblks;
22555 		if ((vpart->p_size % nblks) != 0) {
22556 			ncyl++;
22557 		}
22558 		if (ncyl > (int)un->un_g.dkg_ncyl) {
22559 			return (EINVAL);
22560 		}
22561 		vpart++;
22562 	}
22563 #endif	/* defined(_SUNOS_VTOC_8) */
22564 
22565 	/* Put appropriate vtoc structure fields into the disk label */
22566 #if defined(_SUNOS_VTOC_16)
22567 	/*
22568 	 * The vtoc is always a 32bit data structure to maintain the
22569 	 * on-disk format. Convert "in place" instead of bcopying it.
22570 	 */
22571 	vtoctovtoc32((*user_vtoc), (*((struct vtoc32 *)&(un->un_vtoc))));
22572 
22573 	/*
22574 	 * in the 16-slice vtoc, starting sectors are expressed in
22575 	 * numbers *relative* to the start of the Solaris fdisk partition.
22576 	 */
22577 	lmap = un->un_map;
22578 	vpart = user_vtoc->v_part;
22579 
22580 	for (i = 0; i < (int)user_vtoc->v_nparts; i++, lmap++, vpart++) {
22581 		lmap->dkl_cylno = vpart->p_start / nblks;
22582 		lmap->dkl_nblk = vpart->p_size;
22583 	}
22584 
22585 #elif defined(_SUNOS_VTOC_8)
22586 
22587 	un->un_vtoc.v_bootinfo[0] = (uint32_t)user_vtoc->v_bootinfo[0];
22588 	un->un_vtoc.v_bootinfo[1] = (uint32_t)user_vtoc->v_bootinfo[1];
22589 	un->un_vtoc.v_bootinfo[2] = (uint32_t)user_vtoc->v_bootinfo[2];
22590 
22591 	un->un_vtoc.v_sanity = (uint32_t)user_vtoc->v_sanity;
22592 	un->un_vtoc.v_version = (uint32_t)user_vtoc->v_version;
22593 
22594 	bcopy(user_vtoc->v_volume, un->un_vtoc.v_volume, LEN_DKL_VVOL);
22595 
22596 	un->un_vtoc.v_nparts = user_vtoc->v_nparts;
22597 
22598 	bcopy(user_vtoc->v_reserved, un->un_vtoc.v_reserved,
22599 	    sizeof (un->un_vtoc.v_reserved));
22600 
22601 	/*
22602 	 * Note the conversion from starting sector number
22603 	 * to starting cylinder number.
22604 	 * Return error if division results in a remainder.
22605 	 */
22606 	lmap = un->un_map;
22607 	lpart = un->un_vtoc.v_part;
22608 	vpart = user_vtoc->v_part;
22609 
22610 	for (i = 0; i < (int)user_vtoc->v_nparts; i++) {
22611 		lpart->p_tag  = vpart->p_tag;
22612 		lpart->p_flag = vpart->p_flag;
22613 		lmap->dkl_cylno = vpart->p_start / nblks;
22614 		lmap->dkl_nblk = vpart->p_size;
22615 
22616 		lmap++;
22617 		lpart++;
22618 		vpart++;
22619 
22620 		/* (4387723) */
22621 #ifdef _LP64
22622 		if (user_vtoc->timestamp[i] > TIME32_MAX) {
22623 			un->un_vtoc.v_timestamp[i] = TIME32_MAX;
22624 		} else {
22625 			un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
22626 		}
22627 #else
22628 		un->un_vtoc.v_timestamp[i] = user_vtoc->timestamp[i];
22629 #endif
22630 	}
22631 
22632 	bcopy(user_vtoc->v_asciilabel, un->un_asciilabel, LEN_DKL_ASCII);
22633 #else
22634 #error "No VTOC format defined."
22635 #endif
22636 	return (0);
22637 }
22638 
22639 /*
22640  *    Function: sd_clear_efi
22641  *
22642  * Description: This routine clears all EFI labels.
22643  *
22644  *   Arguments: un - driver soft state (unit) structure
22645  *
22646  * Return Code: void
22647  */
22648 
22649 static void
22650 sd_clear_efi(struct sd_lun *un)
22651 {
22652 	efi_gpt_t	*gpt;
22653 	uint_t		lbasize;
22654 	uint64_t	cap;
22655 	int rval;
22656 
22657 	ASSERT(!mutex_owned(SD_MUTEX(un)));
22658 
22659 	gpt = kmem_alloc(sizeof (efi_gpt_t), KM_SLEEP);
22660 
22661 	if (sd_send_scsi_READ(un, gpt, DEV_BSIZE, 1, SD_PATH_DIRECT) != 0) {
22662 		goto done;
22663 	}
22664 
22665 	sd_swap_efi_gpt(gpt);
22666 	rval = sd_validate_efi(gpt);
22667 	if (rval == 0) {
22668 		/* clear primary */
22669 		bzero(gpt, sizeof (efi_gpt_t));
22670 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE, 1,
22671 			SD_PATH_DIRECT))) {
22672 			SD_INFO(SD_LOG_IO_PARTITION, un,
22673 				"sd_clear_efi: clear primary label failed\n");
22674 		}
22675 	}
22676 	/* the backup */
22677 	rval = sd_send_scsi_READ_CAPACITY(un, &cap, &lbasize,
22678 	    SD_PATH_DIRECT);
22679 	if (rval) {
22680 		goto done;
22681 	}
22682 	if ((rval = sd_send_scsi_READ(un, gpt, lbasize,
22683 	    cap - 1, SD_PATH_DIRECT)) != 0) {
22684 		goto done;
22685 	}
22686 	sd_swap_efi_gpt(gpt);
22687 	rval = sd_validate_efi(gpt);
22688 	if (rval == 0) {
22689 		/* clear backup */
22690 		SD_TRACE(SD_LOG_IOCTL, un, "sd_clear_efi clear backup@%lu\n",
22691 			cap-1);
22692 		bzero(gpt, sizeof (efi_gpt_t));
22693 		if ((rval = sd_send_scsi_WRITE(un, gpt, EFI_LABEL_SIZE,
22694 		    cap-1, SD_PATH_DIRECT))) {
22695 			SD_INFO(SD_LOG_IO_PARTITION, un,
22696 				"sd_clear_efi: clear backup label failed\n");
22697 		}
22698 	}
22699 
22700 done:
22701 	kmem_free(gpt, sizeof (efi_gpt_t));
22702 }
22703 
22704 /*
22705  *    Function: sd_set_vtoc
22706  *
22707  * Description: This routine writes data to the appropriate positions
22708  *
22709  *   Arguments: un - driver soft state (unit) structure
22710  *              dkl  - the data to be written
22711  *
22712  * Return: void
22713  */
22714 
22715 static int
22716 sd_set_vtoc(struct sd_lun *un, struct dk_label *dkl)
22717 {
22718 	void			*shadow_buf;
22719 	uint_t			label_addr;
22720 	int			sec;
22721 	int			blk;
22722 	int			head;
22723 	int			cyl;
22724 	int			rval;
22725 
22726 #if defined(__i386) || defined(__amd64)
22727 	label_addr = un->un_solaris_offset + DK_LABEL_LOC;
22728 #else
22729 	/* Write the primary label at block 0 of the solaris partition. */
22730 	label_addr = 0;
22731 #endif
22732 
22733 	if (NOT_DEVBSIZE(un)) {
22734 		shadow_buf = kmem_zalloc(un->un_tgt_blocksize, KM_SLEEP);
22735 		/*
22736 		 * Read the target's first block.
22737 		 */
22738 		if ((rval = sd_send_scsi_READ(un, shadow_buf,
22739 		    un->un_tgt_blocksize, label_addr,
22740 		    SD_PATH_STANDARD)) != 0) {
22741 			goto exit;
22742 		}
22743 		/*
22744 		 * Copy the contents of the label into the shadow buffer
22745 		 * which is of the size of target block size.
22746 		 */
22747 		bcopy(dkl, shadow_buf, sizeof (struct dk_label));
22748 	}
22749 
22750 	/* Write the primary label */
22751 	if (NOT_DEVBSIZE(un)) {
22752 		rval = sd_send_scsi_WRITE(un, shadow_buf, un->un_tgt_blocksize,
22753 		    label_addr, SD_PATH_STANDARD);
22754 	} else {
22755 		rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
22756 		    label_addr, SD_PATH_STANDARD);
22757 	}
22758 	if (rval != 0) {
22759 		return (rval);
22760 	}
22761 
22762 	/*
22763 	 * Calculate where the backup labels go.  They are always on
22764 	 * the last alternate cylinder, but some older drives put them
22765 	 * on head 2 instead of the last head.	They are always on the
22766 	 * first 5 odd sectors of the appropriate track.
22767 	 *
22768 	 * We have no choice at this point, but to believe that the
22769 	 * disk label is valid.	 Use the geometry of the disk
22770 	 * as described in the label.
22771 	 */
22772 	cyl  = dkl->dkl_ncyl  + dkl->dkl_acyl - 1;
22773 	head = dkl->dkl_nhead - 1;
22774 
22775 	/*
22776 	 * Write and verify the backup labels. Make sure we don't try to
22777 	 * write past the last cylinder.
22778 	 */
22779 	for (sec = 1; ((sec < 5 * 2 + 1) && (sec < dkl->dkl_nsect)); sec += 2) {
22780 		blk = (daddr_t)(
22781 		    (cyl * ((dkl->dkl_nhead * dkl->dkl_nsect) - dkl->dkl_apc)) +
22782 		    (head * dkl->dkl_nsect) + sec);
22783 #if defined(__i386) || defined(__amd64)
22784 		blk += un->un_solaris_offset;
22785 #endif
22786 		if (NOT_DEVBSIZE(un)) {
22787 			uint64_t	tblk;
22788 			/*
22789 			 * Need to read the block first for read modify write.
22790 			 */
22791 			tblk = (uint64_t)blk;
22792 			blk = (int)((tblk * un->un_sys_blocksize) /
22793 			    un->un_tgt_blocksize);
22794 			if ((rval = sd_send_scsi_READ(un, shadow_buf,
22795 			    un->un_tgt_blocksize, blk,
22796 			    SD_PATH_STANDARD)) != 0) {
22797 				goto exit;
22798 			}
22799 			/*
22800 			 * Modify the shadow buffer with the label.
22801 			 */
22802 			bcopy(dkl, shadow_buf, sizeof (struct dk_label));
22803 			rval = sd_send_scsi_WRITE(un, shadow_buf,
22804 			    un->un_tgt_blocksize, blk, SD_PATH_STANDARD);
22805 		} else {
22806 			rval = sd_send_scsi_WRITE(un, dkl, un->un_sys_blocksize,
22807 			    blk, SD_PATH_STANDARD);
22808 			SD_INFO(SD_LOG_IO_PARTITION, un,
22809 			"sd_set_vtoc: wrote backup label %d\n", blk);
22810 		}
22811 		if (rval != 0) {
22812 			goto exit;
22813 		}
22814 	}
22815 exit:
22816 	if (NOT_DEVBSIZE(un)) {
22817 		kmem_free(shadow_buf, un->un_tgt_blocksize);
22818 	}
22819 	return (rval);
22820 }
22821 
22822 /*
22823  *    Function: sd_clear_vtoc
22824  *
22825  * Description: This routine clears out the VTOC labels.
22826  *
22827  *   Arguments: un - driver soft state (unit) structure
22828  *
22829  * Return: void
22830  */
22831 
22832 static void
22833 sd_clear_vtoc(struct sd_lun *un)
22834 {
22835 	struct dk_label		*dkl;
22836 
22837 	mutex_exit(SD_MUTEX(un));
22838 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
22839 	mutex_enter(SD_MUTEX(un));
22840 	/*
22841 	 * sd_set_vtoc uses these fields in order to figure out
22842 	 * where to overwrite the backup labels
22843 	 */
22844 	dkl->dkl_apc    = un->un_g.dkg_apc;
22845 	dkl->dkl_ncyl   = un->un_g.dkg_ncyl;
22846 	dkl->dkl_acyl   = un->un_g.dkg_acyl;
22847 	dkl->dkl_nhead  = un->un_g.dkg_nhead;
22848 	dkl->dkl_nsect  = un->un_g.dkg_nsect;
22849 	mutex_exit(SD_MUTEX(un));
22850 	(void) sd_set_vtoc(un, dkl);
22851 	kmem_free(dkl, sizeof (struct dk_label));
22852 
22853 	mutex_enter(SD_MUTEX(un));
22854 }
22855 
22856 /*
22857  *    Function: sd_write_label
22858  *
22859  * Description: This routine will validate and write the driver soft state vtoc
22860  *		contents to the device.
22861  *
22862  *   Arguments: dev - the device number
22863  *
22864  * Return Code: the code returned by sd_send_scsi_cmd()
22865  *		0
22866  *		EINVAL
22867  *		ENXIO
22868  *		ENOMEM
22869  */
22870 
22871 static int
22872 sd_write_label(dev_t dev)
22873 {
22874 	struct sd_lun		*un;
22875 	struct dk_label		*dkl;
22876 	short			sum;
22877 	short			*sp;
22878 	int			i;
22879 	int			rval;
22880 
22881 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
22882 	    (un->un_state == SD_STATE_OFFLINE)) {
22883 		return (ENXIO);
22884 	}
22885 	ASSERT(mutex_owned(SD_MUTEX(un)));
22886 	mutex_exit(SD_MUTEX(un));
22887 	dkl = kmem_zalloc(sizeof (struct dk_label), KM_SLEEP);
22888 	mutex_enter(SD_MUTEX(un));
22889 
22890 	bcopy(&un->un_vtoc, &dkl->dkl_vtoc, sizeof (struct dk_vtoc));
22891 	dkl->dkl_rpm	= un->un_g.dkg_rpm;
22892 	dkl->dkl_pcyl	= un->un_g.dkg_pcyl;
22893 	dkl->dkl_apc	= un->un_g.dkg_apc;
22894 	dkl->dkl_intrlv = un->un_g.dkg_intrlv;
22895 	dkl->dkl_ncyl	= un->un_g.dkg_ncyl;
22896 	dkl->dkl_acyl	= un->un_g.dkg_acyl;
22897 	dkl->dkl_nhead	= un->un_g.dkg_nhead;
22898 	dkl->dkl_nsect	= un->un_g.dkg_nsect;
22899 
22900 #if defined(_SUNOS_VTOC_8)
22901 	dkl->dkl_obs1	= un->un_g.dkg_obs1;
22902 	dkl->dkl_obs2	= un->un_g.dkg_obs2;
22903 	dkl->dkl_obs3	= un->un_g.dkg_obs3;
22904 	for (i = 0; i < NDKMAP; i++) {
22905 		dkl->dkl_map[i].dkl_cylno = un->un_map[i].dkl_cylno;
22906 		dkl->dkl_map[i].dkl_nblk  = un->un_map[i].dkl_nblk;
22907 	}
22908 	bcopy(un->un_asciilabel, dkl->dkl_asciilabel, LEN_DKL_ASCII);
22909 #elif defined(_SUNOS_VTOC_16)
22910 	dkl->dkl_skew	= un->un_dkg_skew;
22911 #else
22912 #error "No VTOC format defined."
22913 #endif
22914 
22915 	dkl->dkl_magic			= DKL_MAGIC;
22916 	dkl->dkl_write_reinstruct	= un->un_g.dkg_write_reinstruct;
22917 	dkl->dkl_read_reinstruct	= un->un_g.dkg_read_reinstruct;
22918 
22919 	/* Construct checksum for the new disk label */
22920 	sum = 0;
22921 	sp = (short *)dkl;
22922 	i = sizeof (struct dk_label) / sizeof (short);
22923 	while (i--) {
22924 		sum ^= *sp++;
22925 	}
22926 	dkl->dkl_cksum = sum;
22927 
22928 	mutex_exit(SD_MUTEX(un));
22929 
22930 	rval = sd_set_vtoc(un, dkl);
22931 exit:
22932 	kmem_free(dkl, sizeof (struct dk_label));
22933 	mutex_enter(SD_MUTEX(un));
22934 	return (rval);
22935 }
22936 
22937 static int
22938 sd_dkio_set_efi(dev_t dev, caddr_t arg, int flag)
22939 {
22940 	struct sd_lun	*un = NULL;
22941 	dk_efi_t	user_efi;
22942 	int		rval = 0;
22943 	void		*buffer;
22944 
22945 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL)
22946 		return (ENXIO);
22947 
22948 	if (ddi_copyin(arg, &user_efi, sizeof (dk_efi_t), flag))
22949 		return (EFAULT);
22950 
22951 	user_efi.dki_data = (void *)(uintptr_t)user_efi.dki_data_64;
22952 
22953 	if ((user_efi.dki_length % un->un_tgt_blocksize) ||
22954 	    (user_efi.dki_length > un->un_max_xfer_size))
22955 		return (EINVAL);
22956 
22957 	buffer = kmem_alloc(user_efi.dki_length, KM_SLEEP);
22958 	if (ddi_copyin(user_efi.dki_data, buffer, user_efi.dki_length, flag)) {
22959 		rval = EFAULT;
22960 	} else {
22961 		/*
22962 		 * let's clear the vtoc labels and clear the softstate
22963 		 * vtoc.
22964 		 */
22965 		mutex_enter(SD_MUTEX(un));
22966 		if (un->un_vtoc.v_sanity == VTOC_SANE) {
22967 			SD_TRACE(SD_LOG_IO_PARTITION, un,
22968 				"sd_dkio_set_efi: CLEAR VTOC\n");
22969 			sd_clear_vtoc(un);
22970 			bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
22971 			mutex_exit(SD_MUTEX(un));
22972 			ddi_remove_minor_node(SD_DEVINFO(un), "h");
22973 			ddi_remove_minor_node(SD_DEVINFO(un), "h,raw");
22974 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd",
22975 			    S_IFBLK,
22976 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
22977 			    un->un_node_type, NULL);
22978 			(void) ddi_create_minor_node(SD_DEVINFO(un), "wd,raw",
22979 			    S_IFCHR,
22980 			    (SDUNIT(dev) << SDUNIT_SHIFT) | WD_NODE,
22981 			    un->un_node_type, NULL);
22982 		} else
22983 			mutex_exit(SD_MUTEX(un));
22984 		rval = sd_send_scsi_WRITE(un, buffer, user_efi.dki_length,
22985 		    user_efi.dki_lba, SD_PATH_DIRECT);
22986 		if (rval == 0) {
22987 			mutex_enter(SD_MUTEX(un));
22988 			un->un_f_geometry_is_valid = FALSE;
22989 			mutex_exit(SD_MUTEX(un));
22990 		}
22991 	}
22992 	kmem_free(buffer, user_efi.dki_length);
22993 	return (rval);
22994 }
22995 
22996 /*
22997  *    Function: sd_dkio_get_mboot
22998  *
22999  * Description: This routine is the driver entry point for handling user
23000  *		requests to get the current device mboot (DKIOCGMBOOT)
23001  *
23002  *   Arguments: dev  - the device number
23003  *		arg  - pointer to user provided mboot structure specifying
23004  *			the current mboot.
23005  *		flag - this argument is a pass through to ddi_copyxxx()
23006  *		       directly from the mode argument of ioctl().
23007  *
23008  * Return Code: 0
23009  *		EINVAL
23010  *		EFAULT
23011  *		ENXIO
23012  */
23013 
23014 static int
23015 sd_dkio_get_mboot(dev_t dev, caddr_t arg, int flag)
23016 {
23017 	struct sd_lun	*un;
23018 	struct mboot	*mboot;
23019 	int		rval;
23020 	size_t		buffer_size;
23021 
23022 	if (((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) ||
23023 	    (un->un_state == SD_STATE_OFFLINE)) {
23024 		return (ENXIO);
23025 	}
23026 
23027 #if defined(_SUNOS_VTOC_8)
23028 	if ((!ISREMOVABLE(un)) || (arg == NULL)) {
23029 #elif defined(_SUNOS_VTOC_16)
23030 	if (arg == NULL) {
23031 #endif
23032 		return (EINVAL);
23033 	}
23034 
23035 	/*
23036 	 * Read the mboot block, located at absolute block 0 on the target.
23037 	 */
23038 	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct mboot));
23039 
23040 	SD_TRACE(SD_LOG_IO_PARTITION, un,
23041 	    "sd_dkio_get_mboot: allocation size: 0x%x\n", buffer_size);
23042 
23043 	mboot = kmem_zalloc(buffer_size, KM_SLEEP);
23044 	if ((rval = sd_send_scsi_READ(un, mboot, buffer_size, 0,
23045 	    SD_PATH_STANDARD)) == 0) {
23046 		if (ddi_copyout(mboot, (void *)arg,
23047 		    sizeof (struct mboot), flag) != 0) {
23048 			rval = EFAULT;
23049 		}
23050 	}
23051 	kmem_free(mboot, buffer_size);
23052 	return (rval);
23053 }
23054 
23055 
23056 /*
23057  *    Function: sd_dkio_set_mboot
23058  *
23059  * Description: This routine is the driver entry point for handling user
23060  *		requests to validate and set the device master boot
23061  *		(DKIOCSMBOOT).
23062  *
23063  *   Arguments: dev  - the device number
23064  *		arg  - pointer to user provided mboot structure used to set the
23065  *			master boot.
23066  *		flag - this argument is a pass through to ddi_copyxxx()
23067  *		       directly from the mode argument of ioctl().
23068  *
23069  * Return Code: 0
23070  *		EINVAL
23071  *		EFAULT
23072  *		ENXIO
23073  */
23074 
23075 static int
23076 sd_dkio_set_mboot(dev_t dev, caddr_t arg, int flag)
23077 {
23078 	struct sd_lun	*un = NULL;
23079 	struct mboot	*mboot = NULL;
23080 	int		rval;
23081 	ushort_t	magic;
23082 
23083 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23084 		return (ENXIO);
23085 	}
23086 
23087 	ASSERT(!mutex_owned(SD_MUTEX(un)));
23088 
23089 #if defined(_SUNOS_VTOC_8)
23090 	if (!ISREMOVABLE(un)) {
23091 		return (EINVAL);
23092 	}
23093 #endif
23094 
23095 	if (arg == NULL) {
23096 		return (EINVAL);
23097 	}
23098 
23099 	mboot = kmem_zalloc(sizeof (struct mboot), KM_SLEEP);
23100 
23101 	if (ddi_copyin((const void *)arg, mboot,
23102 	    sizeof (struct mboot), flag) != 0) {
23103 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23104 		return (EFAULT);
23105 	}
23106 
23107 	/* Is this really a master boot record? */
23108 	magic = LE_16(mboot->signature);
23109 	if (magic != MBB_MAGIC) {
23110 		kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23111 		return (EINVAL);
23112 	}
23113 
23114 	rval = sd_send_scsi_WRITE(un, mboot, un->un_sys_blocksize, 0,
23115 	    SD_PATH_STANDARD);
23116 
23117 	mutex_enter(SD_MUTEX(un));
23118 #if defined(__i386) || defined(__amd64)
23119 	if (rval == 0) {
23120 		/*
23121 		 * mboot has been written successfully.
23122 		 * update the fdisk and vtoc tables in memory
23123 		 */
23124 		rval = sd_update_fdisk_and_vtoc(un);
23125 		if ((un->un_f_geometry_is_valid == FALSE) || (rval != 0)) {
23126 			mutex_exit(SD_MUTEX(un));
23127 			kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23128 			return (rval);
23129 		}
23130 	}
23131 
23132 	/*
23133 	 * If the mboot write fails, write the devid anyway, what can it hurt?
23134 	 * Also preserve the device id by writing to the disk acyl for the case
23135 	 * where a devid has been fabricated.
23136 	 */
23137 	if (!ISREMOVABLE(un) && !ISCD(un) &&
23138 	    (un->un_f_opt_fab_devid == TRUE)) {
23139 		if (un->un_devid == NULL) {
23140 			sd_register_devid(un, SD_DEVINFO(un),
23141 			    SD_TARGET_IS_UNRESERVED);
23142 		} else {
23143 			/*
23144 			 * The device id for this disk has been
23145 			 * fabricated. Fabricated device id's are
23146 			 * managed by storing them in the last 2
23147 			 * available sectors on the drive. The device
23148 			 * id must be preserved by writing it back out
23149 			 * to this location.
23150 			 */
23151 			if (sd_write_deviceid(un) != 0) {
23152 				ddi_devid_free(un->un_devid);
23153 				un->un_devid = NULL;
23154 			}
23155 		}
23156 	}
23157 #else
23158 	if (rval == 0) {
23159 		/*
23160 		 * mboot has been written successfully.
23161 		 * set up the default geometry and VTOC
23162 		 */
23163 		if (un->un_blockcount <= DK_MAX_BLOCKS)
23164 			sd_setup_default_geometry(un);
23165 	}
23166 #endif
23167 	mutex_exit(SD_MUTEX(un));
23168 	kmem_free(mboot, (size_t)(sizeof (struct mboot)));
23169 	return (rval);
23170 }
23171 
23172 
23173 /*
23174  *    Function: sd_setup_default_geometry
23175  *
23176  * Description: This local utility routine sets the default geometry as part of
23177  *		setting the device mboot.
23178  *
23179  *   Arguments: un - driver soft state (unit) structure
23180  *
23181  * Note: This may be redundant with sd_build_default_label.
23182  */
23183 
23184 static void
23185 sd_setup_default_geometry(struct sd_lun *un)
23186 {
23187 	/* zero out the soft state geometry and partition table. */
23188 	bzero(&un->un_g, sizeof (struct dk_geom));
23189 	bzero(&un->un_vtoc, sizeof (struct dk_vtoc));
23190 	bzero(un->un_map, NDKMAP * (sizeof (struct dk_map)));
23191 	un->un_asciilabel[0] = '\0';
23192 
23193 	/*
23194 	 * For the rpm, we use the minimum for the disk.
23195 	 * For the head, cyl and number of sector per track,
23196 	 * if the capacity <= 1GB, head = 64, sect = 32.
23197 	 * else head = 255, sect 63
23198 	 * Note: the capacity should be equal to C*H*S values.
23199 	 * This will cause some truncation of size due to
23200 	 * round off errors. For CD-ROMs, this truncation can
23201 	 * have adverse side effects, so returning ncyl and
23202 	 * nhead as 1. The nsect will overflow for most of
23203 	 * CD-ROMs as nsect is of type ushort.
23204 	 */
23205 	if (ISCD(un)) {
23206 		un->un_g.dkg_ncyl = 1;
23207 		un->un_g.dkg_nhead = 1;
23208 		un->un_g.dkg_nsect = un->un_blockcount;
23209 	} else {
23210 		if (un->un_blockcount <= 0x1000) {
23211 			/* Needed for unlabeled SCSI floppies. */
23212 			un->un_g.dkg_nhead = 2;
23213 			un->un_g.dkg_ncyl = 80;
23214 			un->un_g.dkg_pcyl = 80;
23215 			un->un_g.dkg_nsect = un->un_blockcount / (2 * 80);
23216 		} else if (un->un_blockcount <= 0x200000) {
23217 			un->un_g.dkg_nhead = 64;
23218 			un->un_g.dkg_nsect = 32;
23219 			un->un_g.dkg_ncyl = un->un_blockcount / (64 * 32);
23220 		} else {
23221 			un->un_g.dkg_nhead = 255;
23222 			un->un_g.dkg_nsect = 63;
23223 			un->un_g.dkg_ncyl = un->un_blockcount / (255 * 63);
23224 		}
23225 		un->un_blockcount = un->un_g.dkg_ncyl *
23226 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect;
23227 	}
23228 	un->un_g.dkg_acyl = 0;
23229 	un->un_g.dkg_bcyl = 0;
23230 	un->un_g.dkg_intrlv = 1;
23231 	un->un_g.dkg_rpm = 200;
23232 	un->un_g.dkg_read_reinstruct = 0;
23233 	un->un_g.dkg_write_reinstruct = 0;
23234 	if (un->un_g.dkg_pcyl == 0) {
23235 		un->un_g.dkg_pcyl = un->un_g.dkg_ncyl + un->un_g.dkg_acyl;
23236 	}
23237 
23238 	un->un_map['a'-'a'].dkl_cylno = 0;
23239 	un->un_map['a'-'a'].dkl_nblk = un->un_blockcount;
23240 	un->un_map['c'-'a'].dkl_cylno = 0;
23241 	un->un_map['c'-'a'].dkl_nblk = un->un_blockcount;
23242 	un->un_f_geometry_is_valid = FALSE;
23243 }
23244 
23245 
23246 #if defined(__i386) || defined(__amd64)
23247 /*
23248  *    Function: sd_update_fdisk_and_vtoc
23249  *
23250  * Description: This local utility routine updates the device fdisk and vtoc
23251  *		as part of setting the device mboot.
23252  *
23253  *   Arguments: un - driver soft state (unit) structure
23254  *
23255  * Return Code: 0 for success or errno-type return code.
23256  *
23257  *    Note:x86: This looks like a duplicate of sd_validate_geometry(), but
23258  *		these did exist seperately in x86 sd.c!!!
23259  */
23260 
23261 static int
23262 sd_update_fdisk_and_vtoc(struct sd_lun *un)
23263 {
23264 	static char	labelstring[128];
23265 	static char	buf[256];
23266 	char		*label = 0;
23267 	int		count;
23268 	int		label_rc = 0;
23269 	int		gvalid = un->un_f_geometry_is_valid;
23270 	int		fdisk_rval;
23271 	int		lbasize;
23272 	int		capacity;
23273 
23274 	ASSERT(mutex_owned(SD_MUTEX(un)));
23275 
23276 	if (un->un_f_tgt_blocksize_is_valid == FALSE) {
23277 		return (EINVAL);
23278 	}
23279 
23280 	if (un->un_f_blockcount_is_valid == FALSE) {
23281 		return (EINVAL);
23282 	}
23283 
23284 #if defined(_SUNOS_VTOC_16)
23285 	/*
23286 	 * Set up the "whole disk" fdisk partition; this should always
23287 	 * exist, regardless of whether the disk contains an fdisk table
23288 	 * or vtoc.
23289 	 */
23290 	un->un_map[P0_RAW_DISK].dkl_cylno = 0;
23291 	un->un_map[P0_RAW_DISK].dkl_nblk = un->un_blockcount;
23292 #endif	/* defined(_SUNOS_VTOC_16) */
23293 
23294 	/*
23295 	 * copy the lbasize and capacity so that if they're
23296 	 * reset while we're not holding the SD_MUTEX(un), we will
23297 	 * continue to use valid values after the SD_MUTEX(un) is
23298 	 * reacquired.
23299 	 */
23300 	lbasize  = un->un_tgt_blocksize;
23301 	capacity = un->un_blockcount;
23302 
23303 	/*
23304 	 * refresh the logical and physical geometry caches.
23305 	 * (data from mode sense format/rigid disk geometry pages,
23306 	 * and scsi_ifgetcap("geometry").
23307 	 */
23308 	sd_resync_geom_caches(un, capacity, lbasize, SD_PATH_DIRECT);
23309 
23310 	/*
23311 	 * Only DIRECT ACCESS devices will have Sun labels.
23312 	 * CD's supposedly have a Sun label, too
23313 	 */
23314 	if (SD_INQUIRY(un)->inq_dtype == DTYPE_DIRECT || ISREMOVABLE(un)) {
23315 		fdisk_rval = sd_read_fdisk(un, capacity, lbasize,
23316 		    SD_PATH_DIRECT);
23317 		if (fdisk_rval == SD_CMD_FAILURE) {
23318 			ASSERT(mutex_owned(SD_MUTEX(un)));
23319 			return (EIO);
23320 		}
23321 
23322 		if (fdisk_rval == SD_CMD_RESERVATION_CONFLICT) {
23323 			ASSERT(mutex_owned(SD_MUTEX(un)));
23324 			return (EACCES);
23325 		}
23326 
23327 		if (un->un_solaris_size <= DK_LABEL_LOC) {
23328 			/*
23329 			 * Found fdisk table but no Solaris partition entry,
23330 			 * so don't call sd_uselabel() and don't create
23331 			 * a default label.
23332 			 */
23333 			label_rc = 0;
23334 			un->un_f_geometry_is_valid = TRUE;
23335 			goto no_solaris_partition;
23336 		}
23337 
23338 #if defined(_SUNOS_VTOC_8)
23339 		label = (char *)un->un_asciilabel;
23340 #elif defined(_SUNOS_VTOC_16)
23341 		label = (char *)un->un_vtoc.v_asciilabel;
23342 #else
23343 #error "No VTOC format defined."
23344 #endif
23345 	} else if (capacity < 0) {
23346 		ASSERT(mutex_owned(SD_MUTEX(un)));
23347 		return (EINVAL);
23348 	}
23349 
23350 	/*
23351 	 * For Removable media We reach here if we have found a
23352 	 * SOLARIS PARTITION.
23353 	 * If un_f_geometry_is_valid is FALSE it indicates that the SOLARIS
23354 	 * PARTITION has changed from the previous one, hence we will setup a
23355 	 * default VTOC in this case.
23356 	 */
23357 	if (un->un_f_geometry_is_valid == FALSE) {
23358 		sd_build_default_label(un);
23359 		label_rc = 0;
23360 	}
23361 
23362 no_solaris_partition:
23363 	if ((!ISREMOVABLE(un) ||
23364 	    (ISREMOVABLE(un) && un->un_mediastate == DKIO_EJECTED)) &&
23365 	    (un->un_state == SD_STATE_NORMAL && gvalid == FALSE)) {
23366 		/*
23367 		 * Print out a message indicating who and what we are.
23368 		 * We do this only when we happen to really validate the
23369 		 * geometry. We may call sd_validate_geometry() at other
23370 		 * times, ioctl()'s like Get VTOC in which case we
23371 		 * don't want to print the label.
23372 		 * If the geometry is valid, print the label string,
23373 		 * else print vendor and product info, if available
23374 		 */
23375 		if ((un->un_f_geometry_is_valid == TRUE) && (label != NULL)) {
23376 			SD_INFO(SD_LOG_IOCTL_DKIO, un, "?<%s>\n", label);
23377 		} else {
23378 			mutex_enter(&sd_label_mutex);
23379 			sd_inq_fill(SD_INQUIRY(un)->inq_vid, VIDMAX,
23380 			    labelstring);
23381 			sd_inq_fill(SD_INQUIRY(un)->inq_pid, PIDMAX,
23382 			    &labelstring[64]);
23383 			(void) sprintf(buf, "?Vendor '%s', product '%s'",
23384 			    labelstring, &labelstring[64]);
23385 			if (un->un_f_blockcount_is_valid == TRUE) {
23386 				(void) sprintf(&buf[strlen(buf)],
23387 				    ", %" PRIu64 " %u byte blocks\n",
23388 				    un->un_blockcount,
23389 				    un->un_tgt_blocksize);
23390 			} else {
23391 				(void) sprintf(&buf[strlen(buf)],
23392 				    ", (unknown capacity)\n");
23393 			}
23394 			SD_INFO(SD_LOG_IOCTL_DKIO, un, buf);
23395 			mutex_exit(&sd_label_mutex);
23396 		}
23397 	}
23398 
23399 #if defined(_SUNOS_VTOC_16)
23400 	/*
23401 	 * If we have valid geometry, set up the remaining fdisk partitions.
23402 	 * Note that dkl_cylno is not used for the fdisk map entries, so
23403 	 * we set it to an entirely bogus value.
23404 	 */
23405 	for (count = 0; count < FD_NUMPART; count++) {
23406 		un->un_map[FDISK_P1 + count].dkl_cylno = -1;
23407 		un->un_map[FDISK_P1 + count].dkl_nblk =
23408 		    un->un_fmap[count].fmap_nblk;
23409 		un->un_offset[FDISK_P1 + count] =
23410 		    un->un_fmap[count].fmap_start;
23411 	}
23412 #endif
23413 
23414 	for (count = 0; count < NDKMAP; count++) {
23415 #if defined(_SUNOS_VTOC_8)
23416 		struct dk_map *lp  = &un->un_map[count];
23417 		un->un_offset[count] =
23418 		    un->un_g.dkg_nhead * un->un_g.dkg_nsect * lp->dkl_cylno;
23419 #elif defined(_SUNOS_VTOC_16)
23420 		struct dkl_partition *vp = &un->un_vtoc.v_part[count];
23421 		un->un_offset[count] = vp->p_start + un->un_solaris_offset;
23422 #else
23423 #error "No VTOC format defined."
23424 #endif
23425 	}
23426 
23427 	ASSERT(mutex_owned(SD_MUTEX(un)));
23428 	return (label_rc);
23429 }
23430 #endif
23431 
23432 
23433 /*
23434  *    Function: sd_check_media
23435  *
23436  * Description: This utility routine implements the functionality for the
23437  *		DKIOCSTATE ioctl. This ioctl blocks the user thread until the
23438  *		driver state changes from that specified by the user
23439  *		(inserted or ejected). For example, if the user specifies
23440  *		DKIO_EJECTED and the current media state is inserted this
23441  *		routine will immediately return DKIO_INSERTED. However, if the
23442  *		current media state is not inserted the user thread will be
23443  *		blocked until the drive state changes. If DKIO_NONE is specified
23444  *		the user thread will block until a drive state change occurs.
23445  *
23446  *   Arguments: dev  - the device number
23447  *		state  - user pointer to a dkio_state, updated with the current
23448  *			drive state at return.
23449  *
23450  * Return Code: ENXIO
23451  *		EIO
23452  *		EAGAIN
23453  *		EINTR
23454  */
23455 
23456 static int
23457 sd_check_media(dev_t dev, enum dkio_state state)
23458 {
23459 	struct sd_lun		*un = NULL;
23460 	enum dkio_state		prev_state;
23461 	opaque_t		token = NULL;
23462 	int			rval = 0;
23463 
23464 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23465 		return (ENXIO);
23466 	}
23467 
23468 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: entry\n");
23469 
23470 	mutex_enter(SD_MUTEX(un));
23471 
23472 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: "
23473 	    "state=%x, mediastate=%x\n", state, un->un_mediastate);
23474 
23475 	prev_state = un->un_mediastate;
23476 
23477 	/* is there anything to do? */
23478 	if (state == un->un_mediastate || un->un_mediastate == DKIO_NONE) {
23479 		/*
23480 		 * submit the request to the scsi_watch service;
23481 		 * scsi_media_watch_cb() does the real work
23482 		 */
23483 		mutex_exit(SD_MUTEX(un));
23484 
23485 		/*
23486 		 * This change handles the case where a scsi watch request is
23487 		 * added to a device that is powered down. To accomplish this
23488 		 * we power up the device before adding the scsi watch request,
23489 		 * since the scsi watch sends a TUR directly to the device
23490 		 * which the device cannot handle if it is powered down.
23491 		 */
23492 		if (sd_pm_entry(un) != DDI_SUCCESS) {
23493 			mutex_enter(SD_MUTEX(un));
23494 			goto done;
23495 		}
23496 
23497 		token = scsi_watch_request_submit(SD_SCSI_DEVP(un),
23498 		    sd_check_media_time, SENSE_LENGTH, sd_media_watch_cb,
23499 		    (caddr_t)dev);
23500 
23501 		sd_pm_exit(un);
23502 
23503 		mutex_enter(SD_MUTEX(un));
23504 		if (token == NULL) {
23505 			rval = EAGAIN;
23506 			goto done;
23507 		}
23508 
23509 		/*
23510 		 * This is a special case IOCTL that doesn't return
23511 		 * until the media state changes. Routine sdpower
23512 		 * knows about and handles this so don't count it
23513 		 * as an active cmd in the driver, which would
23514 		 * keep the device busy to the pm framework.
23515 		 * If the count isn't decremented the device can't
23516 		 * be powered down.
23517 		 */
23518 		un->un_ncmds_in_driver--;
23519 		ASSERT(un->un_ncmds_in_driver >= 0);
23520 
23521 		/*
23522 		 * if a prior request had been made, this will be the same
23523 		 * token, as scsi_watch was designed that way.
23524 		 */
23525 		un->un_swr_token = token;
23526 		un->un_specified_mediastate = state;
23527 
23528 		/*
23529 		 * now wait for media change
23530 		 * we will not be signalled unless mediastate == state but it is
23531 		 * still better to test for this condition, since there is a
23532 		 * 2 sec cv_broadcast delay when mediastate == DKIO_INSERTED
23533 		 */
23534 		SD_TRACE(SD_LOG_COMMON, un,
23535 		    "sd_check_media: waiting for media state change\n");
23536 		while (un->un_mediastate == state) {
23537 			if (cv_wait_sig(&un->un_state_cv, SD_MUTEX(un)) == 0) {
23538 				SD_TRACE(SD_LOG_COMMON, un,
23539 				    "sd_check_media: waiting for media state "
23540 				    "was interrupted\n");
23541 				un->un_ncmds_in_driver++;
23542 				rval = EINTR;
23543 				goto done;
23544 			}
23545 			SD_TRACE(SD_LOG_COMMON, un,
23546 			    "sd_check_media: received signal, state=%x\n",
23547 			    un->un_mediastate);
23548 		}
23549 		/*
23550 		 * Inc the counter to indicate the device once again
23551 		 * has an active outstanding cmd.
23552 		 */
23553 		un->un_ncmds_in_driver++;
23554 	}
23555 
23556 	/* invalidate geometry */
23557 	if (prev_state == DKIO_INSERTED && un->un_mediastate == DKIO_EJECTED) {
23558 		sr_ejected(un);
23559 	}
23560 
23561 	if (un->un_mediastate == DKIO_INSERTED && prev_state != DKIO_INSERTED) {
23562 		uint64_t	capacity;
23563 		uint_t		lbasize;
23564 
23565 		SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: media inserted\n");
23566 		mutex_exit(SD_MUTEX(un));
23567 		/*
23568 		 * Since the following routines use SD_PATH_DIRECT, we must
23569 		 * call PM directly before the upcoming disk accesses. This
23570 		 * may cause the disk to be power/spin up.
23571 		 */
23572 
23573 		if (sd_pm_entry(un) == DDI_SUCCESS) {
23574 			rval = sd_send_scsi_READ_CAPACITY(un,
23575 			    &capacity,
23576 			    &lbasize, SD_PATH_DIRECT);
23577 			if (rval != 0) {
23578 				sd_pm_exit(un);
23579 				mutex_enter(SD_MUTEX(un));
23580 				goto done;
23581 			}
23582 		} else {
23583 			rval = EIO;
23584 			mutex_enter(SD_MUTEX(un));
23585 			goto done;
23586 		}
23587 		mutex_enter(SD_MUTEX(un));
23588 
23589 		sd_update_block_info(un, lbasize, capacity);
23590 
23591 		un->un_f_geometry_is_valid	= FALSE;
23592 		(void) sd_validate_geometry(un, SD_PATH_DIRECT);
23593 
23594 		mutex_exit(SD_MUTEX(un));
23595 		rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_PREVENT,
23596 		    SD_PATH_DIRECT);
23597 		sd_pm_exit(un);
23598 
23599 		mutex_enter(SD_MUTEX(un));
23600 	}
23601 done:
23602 	un->un_f_watcht_stopped = FALSE;
23603 	if (un->un_swr_token) {
23604 		/*
23605 		 * Use of this local token and the mutex ensures that we avoid
23606 		 * some race conditions associated with terminating the
23607 		 * scsi watch.
23608 		 */
23609 		token = un->un_swr_token;
23610 		un->un_swr_token = (opaque_t)NULL;
23611 		mutex_exit(SD_MUTEX(un));
23612 		(void) scsi_watch_request_terminate(token,
23613 		    SCSI_WATCH_TERMINATE_WAIT);
23614 		mutex_enter(SD_MUTEX(un));
23615 	}
23616 
23617 	/*
23618 	 * Update the capacity kstat value, if no media previously
23619 	 * (capacity kstat is 0) and a media has been inserted
23620 	 * (un_f_blockcount_is_valid == TRUE)
23621 	 * This is a more generic way then checking for ISREMOVABLE.
23622 	 */
23623 	if (un->un_errstats) {
23624 		struct sd_errstats	*stp = NULL;
23625 
23626 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
23627 		if ((stp->sd_capacity.value.ui64 == 0) &&
23628 		    (un->un_f_blockcount_is_valid == TRUE)) {
23629 			stp->sd_capacity.value.ui64 =
23630 			    (uint64_t)((uint64_t)un->un_blockcount *
23631 			    un->un_sys_blocksize);
23632 		}
23633 	}
23634 	mutex_exit(SD_MUTEX(un));
23635 	SD_TRACE(SD_LOG_COMMON, un, "sd_check_media: done\n");
23636 	return (rval);
23637 }
23638 
23639 
23640 /*
23641  *    Function: sd_delayed_cv_broadcast
23642  *
23643  * Description: Delayed cv_broadcast to allow for target to recover from media
23644  *		insertion.
23645  *
23646  *   Arguments: arg - driver soft state (unit) structure
23647  */
23648 
23649 static void
23650 sd_delayed_cv_broadcast(void *arg)
23651 {
23652 	struct sd_lun *un = arg;
23653 
23654 	SD_TRACE(SD_LOG_COMMON, un, "sd_delayed_cv_broadcast\n");
23655 
23656 	mutex_enter(SD_MUTEX(un));
23657 	un->un_dcvb_timeid = NULL;
23658 	cv_broadcast(&un->un_state_cv);
23659 	mutex_exit(SD_MUTEX(un));
23660 }
23661 
23662 
23663 /*
23664  *    Function: sd_media_watch_cb
23665  *
23666  * Description: Callback routine used for support of the DKIOCSTATE ioctl. This
23667  *		routine processes the TUR sense data and updates the driver
23668  *		state if a transition has occurred. The user thread
23669  *		(sd_check_media) is then signalled.
23670  *
23671  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
23672  *			among multiple watches that share this callback function
23673  *		resultp - scsi watch facility result packet containing scsi
23674  *			  packet, status byte and sense data
23675  *
23676  * Return Code: 0 for success, -1 for failure
23677  */
23678 
23679 static int
23680 sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
23681 {
23682 	struct sd_lun			*un;
23683 	struct scsi_status		*statusp = resultp->statusp;
23684 	struct scsi_extended_sense	*sensep = resultp->sensep;
23685 	enum dkio_state			state = DKIO_NONE;
23686 	dev_t				dev = (dev_t)arg;
23687 	uchar_t				actual_sense_length;
23688 
23689 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23690 		return (-1);
23691 	}
23692 	actual_sense_length = resultp->actual_sense_length;
23693 
23694 	mutex_enter(SD_MUTEX(un));
23695 	SD_TRACE(SD_LOG_COMMON, un,
23696 	    "sd_media_watch_cb: status=%x, sensep=%p, len=%x\n",
23697 	    *((char *)statusp), (void *)sensep, actual_sense_length);
23698 
23699 	if (resultp->pkt->pkt_reason == CMD_DEV_GONE) {
23700 		un->un_mediastate = DKIO_DEV_GONE;
23701 		printf("sd_media_watch_cb: dev gone\n");
23702 		cv_broadcast(&un->un_state_cv);
23703 		mutex_exit(SD_MUTEX(un));
23704 
23705 		return (0);
23706 	}
23707 
23708 	/*
23709 	 * If there was a check condition then sensep points to valid sense data
23710 	 * If status was not a check condition but a reservation or busy status
23711 	 * then the new state is DKIO_NONE
23712 	 */
23713 	if (sensep != NULL) {
23714 		SD_INFO(SD_LOG_COMMON, un,
23715 		    "sd_media_watch_cb: sense KEY=%x, ASC=%x, ASCQ=%x\n",
23716 		    sensep->es_key, sensep->es_add_code, sensep->es_qual_code);
23717 		/* This routine only uses up to 13 bytes of sense data. */
23718 		if (actual_sense_length >= 13) {
23719 			if (sensep->es_key == KEY_UNIT_ATTENTION) {
23720 				if (sensep->es_add_code == 0x28) {
23721 					state = DKIO_INSERTED;
23722 				}
23723 			} else {
23724 				/*
23725 				 * if 02/04/02  means that the host
23726 				 * should send start command. Explicitly
23727 				 * leave the media state as is
23728 				 * (inserted) as the media is inserted
23729 				 * and host has stopped device for PM
23730 				 * reasons. Upon next true read/write
23731 				 * to this media will bring the
23732 				 * device to the right state good for
23733 				 * media access.
23734 				 */
23735 				if ((sensep->es_key == KEY_NOT_READY) &&
23736 				    (sensep->es_add_code == 0x3a)) {
23737 					state = DKIO_EJECTED;
23738 				}
23739 
23740 				/*
23741 				 * If the drivge is busy with an operation
23742 				 * or long write, keep the media in an
23743 				 * inserted state.
23744 				 */
23745 
23746 				if ((sensep->es_key == KEY_NOT_READY) &&
23747 				    (sensep->es_add_code == 0x04) &&
23748 				    ((sensep->es_qual_code == 0x02) ||
23749 				    (sensep->es_qual_code == 0x07) ||
23750 				    (sensep->es_qual_code == 0x08))) {
23751 					state = DKIO_INSERTED;
23752 				}
23753 			}
23754 		}
23755 	} else if ((*((char *)statusp) == STATUS_GOOD) &&
23756 	    (resultp->pkt->pkt_reason == CMD_CMPLT)) {
23757 		state = DKIO_INSERTED;
23758 	}
23759 
23760 	SD_TRACE(SD_LOG_COMMON, un,
23761 	    "sd_media_watch_cb: state=%x, specified=%x\n",
23762 	    state, un->un_specified_mediastate);
23763 
23764 	/*
23765 	 * now signal the waiting thread if this is *not* the specified state;
23766 	 * delay the signal if the state is DKIO_INSERTED to allow the target
23767 	 * to recover
23768 	 */
23769 	if (state != un->un_specified_mediastate) {
23770 		un->un_mediastate = state;
23771 		if (state == DKIO_INSERTED) {
23772 			/*
23773 			 * delay the signal to give the drive a chance
23774 			 * to do what it apparently needs to do
23775 			 */
23776 			SD_TRACE(SD_LOG_COMMON, un,
23777 			    "sd_media_watch_cb: delayed cv_broadcast\n");
23778 			if (un->un_dcvb_timeid == NULL) {
23779 				un->un_dcvb_timeid =
23780 				    timeout(sd_delayed_cv_broadcast, un,
23781 				    drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
23782 			}
23783 		} else {
23784 			SD_TRACE(SD_LOG_COMMON, un,
23785 			    "sd_media_watch_cb: immediate cv_broadcast\n");
23786 			cv_broadcast(&un->un_state_cv);
23787 		}
23788 	}
23789 	mutex_exit(SD_MUTEX(un));
23790 	return (0);
23791 }
23792 
23793 
23794 /*
23795  *    Function: sd_dkio_get_temp
23796  *
23797  * Description: This routine is the driver entry point for handling ioctl
23798  *		requests to get the disk temperature.
23799  *
23800  *   Arguments: dev  - the device number
23801  *		arg  - pointer to user provided dk_temperature structure.
23802  *		flag - this argument is a pass through to ddi_copyxxx()
23803  *		       directly from the mode argument of ioctl().
23804  *
23805  * Return Code: 0
23806  *		EFAULT
23807  *		ENXIO
23808  *		EAGAIN
23809  */
23810 
23811 static int
23812 sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag)
23813 {
23814 	struct sd_lun		*un = NULL;
23815 	struct dk_temperature	*dktemp = NULL;
23816 	uchar_t			*temperature_page;
23817 	int			rval = 0;
23818 	int			path_flag = SD_PATH_STANDARD;
23819 
23820 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23821 		return (ENXIO);
23822 	}
23823 
23824 	dktemp = kmem_zalloc(sizeof (struct dk_temperature), KM_SLEEP);
23825 
23826 	/* copyin the disk temp argument to get the user flags */
23827 	if (ddi_copyin((void *)arg, dktemp,
23828 	    sizeof (struct dk_temperature), flag) != 0) {
23829 		rval = EFAULT;
23830 		goto done;
23831 	}
23832 
23833 	/* Initialize the temperature to invalid. */
23834 	dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
23835 	dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
23836 
23837 	/*
23838 	 * Note: Investigate removing the "bypass pm" semantic.
23839 	 * Can we just bypass PM always?
23840 	 */
23841 	if (dktemp->dkt_flags & DKT_BYPASS_PM) {
23842 		path_flag = SD_PATH_DIRECT;
23843 		ASSERT(!mutex_owned(&un->un_pm_mutex));
23844 		mutex_enter(&un->un_pm_mutex);
23845 		if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
23846 			/*
23847 			 * If DKT_BYPASS_PM is set, and the drive happens to be
23848 			 * in low power mode, we can not wake it up, Need to
23849 			 * return EAGAIN.
23850 			 */
23851 			mutex_exit(&un->un_pm_mutex);
23852 			rval = EAGAIN;
23853 			goto done;
23854 		} else {
23855 			/*
23856 			 * Indicate to PM the device is busy. This is required
23857 			 * to avoid a race - i.e. the ioctl is issuing a
23858 			 * command and the pm framework brings down the device
23859 			 * to low power mode (possible power cut-off on some
23860 			 * platforms).
23861 			 */
23862 			mutex_exit(&un->un_pm_mutex);
23863 			if (sd_pm_entry(un) != DDI_SUCCESS) {
23864 				rval = EAGAIN;
23865 				goto done;
23866 			}
23867 		}
23868 	}
23869 
23870 	temperature_page = kmem_zalloc(TEMPERATURE_PAGE_SIZE, KM_SLEEP);
23871 
23872 	if ((rval = sd_send_scsi_LOG_SENSE(un, temperature_page,
23873 	    TEMPERATURE_PAGE_SIZE, TEMPERATURE_PAGE, 1, 0, path_flag)) != 0) {
23874 		goto done2;
23875 	}
23876 
23877 	/*
23878 	 * For the current temperature verify that the parameter length is 0x02
23879 	 * and the parameter code is 0x00
23880 	 */
23881 	if ((temperature_page[7] == 0x02) && (temperature_page[4] == 0x00) &&
23882 	    (temperature_page[5] == 0x00)) {
23883 		if (temperature_page[9] == 0xFF) {
23884 			dktemp->dkt_cur_temp = (short)DKT_INVALID_TEMP;
23885 		} else {
23886 			dktemp->dkt_cur_temp = (short)(temperature_page[9]);
23887 		}
23888 	}
23889 
23890 	/*
23891 	 * For the reference temperature verify that the parameter
23892 	 * length is 0x02 and the parameter code is 0x01
23893 	 */
23894 	if ((temperature_page[13] == 0x02) && (temperature_page[10] == 0x00) &&
23895 	    (temperature_page[11] == 0x01)) {
23896 		if (temperature_page[15] == 0xFF) {
23897 			dktemp->dkt_ref_temp = (short)DKT_INVALID_TEMP;
23898 		} else {
23899 			dktemp->dkt_ref_temp = (short)(temperature_page[15]);
23900 		}
23901 	}
23902 
23903 	/* Do the copyout regardless of the temperature commands status. */
23904 	if (ddi_copyout(dktemp, (void *)arg, sizeof (struct dk_temperature),
23905 	    flag) != 0) {
23906 		rval = EFAULT;
23907 	}
23908 
23909 done2:
23910 	if (path_flag == SD_PATH_DIRECT) {
23911 		sd_pm_exit(un);
23912 	}
23913 
23914 	kmem_free(temperature_page, TEMPERATURE_PAGE_SIZE);
23915 done:
23916 	if (dktemp != NULL) {
23917 		kmem_free(dktemp, sizeof (struct dk_temperature));
23918 	}
23919 
23920 	return (rval);
23921 }
23922 
23923 
23924 /*
23925  *    Function: sd_log_page_supported
23926  *
23927  * Description: This routine uses sd_send_scsi_LOG_SENSE to find the list of
23928  *		supported log pages.
23929  *
23930  *   Arguments: un -
23931  *		log_page -
23932  *
23933  * Return Code: -1 - on error (log sense is optional and may not be supported).
23934  *		0  - log page not found.
23935  *  		1  - log page found.
23936  */
23937 
23938 static int
23939 sd_log_page_supported(struct sd_lun *un, int log_page)
23940 {
23941 	uchar_t *log_page_data;
23942 	int	i;
23943 	int	match = 0;
23944 	int	log_size;
23945 
23946 	log_page_data = kmem_zalloc(0xFF, KM_SLEEP);
23947 
23948 	if (sd_send_scsi_LOG_SENSE(un, log_page_data, 0xFF, 0, 0x01, 0,
23949 	    SD_PATH_DIRECT) != 0) {
23950 		SD_ERROR(SD_LOG_COMMON, un,
23951 		    "sd_log_page_supported: failed log page retrieval\n");
23952 		kmem_free(log_page_data, 0xFF);
23953 		return (-1);
23954 	}
23955 	log_size = log_page_data[3];
23956 
23957 	/*
23958 	 * The list of supported log pages start from the fourth byte. Check
23959 	 * until we run out of log pages or a match is found.
23960 	 */
23961 	for (i = 4; (i < (log_size + 4)) && !match; i++) {
23962 		if (log_page_data[i] == log_page) {
23963 			match++;
23964 		}
23965 	}
23966 	kmem_free(log_page_data, 0xFF);
23967 	return (match);
23968 }
23969 
23970 
23971 /*
23972  *    Function: sd_mhdioc_failfast
23973  *
23974  * Description: This routine is the driver entry point for handling ioctl
23975  *		requests to enable/disable the multihost failfast option.
23976  *		(MHIOCENFAILFAST)
23977  *
23978  *   Arguments: dev	- the device number
23979  *		arg	- user specified probing interval.
23980  *		flag	- this argument is a pass through to ddi_copyxxx()
23981  *			  directly from the mode argument of ioctl().
23982  *
23983  * Return Code: 0
23984  *		EFAULT
23985  *		ENXIO
23986  */
23987 
23988 static int
23989 sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag)
23990 {
23991 	struct sd_lun	*un = NULL;
23992 	int		mh_time;
23993 	int		rval = 0;
23994 
23995 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
23996 		return (ENXIO);
23997 	}
23998 
23999 	if (ddi_copyin((void *)arg, &mh_time, sizeof (int), flag))
24000 		return (EFAULT);
24001 
24002 	if (mh_time) {
24003 		mutex_enter(SD_MUTEX(un));
24004 		un->un_resvd_status |= SD_FAILFAST;
24005 		mutex_exit(SD_MUTEX(un));
24006 		/*
24007 		 * If mh_time is INT_MAX, then this ioctl is being used for
24008 		 * SCSI-3 PGR purposes, and we don't need to spawn watch thread.
24009 		 */
24010 		if (mh_time != INT_MAX) {
24011 			rval = sd_check_mhd(dev, mh_time);
24012 		}
24013 	} else {
24014 		(void) sd_check_mhd(dev, 0);
24015 		mutex_enter(SD_MUTEX(un));
24016 		un->un_resvd_status &= ~SD_FAILFAST;
24017 		mutex_exit(SD_MUTEX(un));
24018 	}
24019 	return (rval);
24020 }
24021 
24022 
24023 /*
24024  *    Function: sd_mhdioc_takeown
24025  *
24026  * Description: This routine is the driver entry point for handling ioctl
24027  *		requests to forcefully acquire exclusive access rights to the
24028  *		multihost disk (MHIOCTKOWN).
24029  *
24030  *   Arguments: dev	- the device number
24031  *		arg	- user provided structure specifying the delay
24032  *			  parameters in milliseconds
24033  *		flag	- this argument is a pass through to ddi_copyxxx()
24034  *			  directly from the mode argument of ioctl().
24035  *
24036  * Return Code: 0
24037  *		EFAULT
24038  *		ENXIO
24039  */
24040 
24041 static int
24042 sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag)
24043 {
24044 	struct sd_lun		*un = NULL;
24045 	struct mhioctkown	*tkown = NULL;
24046 	int			rval = 0;
24047 
24048 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24049 		return (ENXIO);
24050 	}
24051 
24052 	if (arg != NULL) {
24053 		tkown = (struct mhioctkown *)
24054 		    kmem_zalloc(sizeof (struct mhioctkown), KM_SLEEP);
24055 		rval = ddi_copyin(arg, tkown, sizeof (struct mhioctkown), flag);
24056 		if (rval != 0) {
24057 			rval = EFAULT;
24058 			goto error;
24059 		}
24060 	}
24061 
24062 	rval = sd_take_ownership(dev, tkown);
24063 	mutex_enter(SD_MUTEX(un));
24064 	if (rval == 0) {
24065 		un->un_resvd_status |= SD_RESERVE;
24066 		if (tkown != NULL && tkown->reinstate_resv_delay != 0) {
24067 			sd_reinstate_resv_delay =
24068 			    tkown->reinstate_resv_delay * 1000;
24069 		} else {
24070 			sd_reinstate_resv_delay = SD_REINSTATE_RESV_DELAY;
24071 		}
24072 		/*
24073 		 * Give the scsi_watch routine interval set by
24074 		 * the MHIOCENFAILFAST ioctl precedence here.
24075 		 */
24076 		if ((un->un_resvd_status & SD_FAILFAST) == 0) {
24077 			mutex_exit(SD_MUTEX(un));
24078 			(void) sd_check_mhd(dev, sd_reinstate_resv_delay/1000);
24079 			SD_TRACE(SD_LOG_IOCTL_MHD, un,
24080 			    "sd_mhdioc_takeown : %d\n",
24081 			    sd_reinstate_resv_delay);
24082 		} else {
24083 			mutex_exit(SD_MUTEX(un));
24084 		}
24085 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_NOTIFY,
24086 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24087 	} else {
24088 		un->un_resvd_status &= ~SD_RESERVE;
24089 		mutex_exit(SD_MUTEX(un));
24090 	}
24091 
24092 error:
24093 	if (tkown != NULL) {
24094 		kmem_free(tkown, sizeof (struct mhioctkown));
24095 	}
24096 	return (rval);
24097 }
24098 
24099 
24100 /*
24101  *    Function: sd_mhdioc_release
24102  *
24103  * Description: This routine is the driver entry point for handling ioctl
24104  *		requests to release exclusive access rights to the multihost
24105  *		disk (MHIOCRELEASE).
24106  *
24107  *   Arguments: dev	- the device number
24108  *
24109  * Return Code: 0
24110  *		ENXIO
24111  */
24112 
24113 static int
24114 sd_mhdioc_release(dev_t dev)
24115 {
24116 	struct sd_lun		*un = NULL;
24117 	timeout_id_t		resvd_timeid_save;
24118 	int			resvd_status_save;
24119 	int			rval = 0;
24120 
24121 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24122 		return (ENXIO);
24123 	}
24124 
24125 	mutex_enter(SD_MUTEX(un));
24126 	resvd_status_save = un->un_resvd_status;
24127 	un->un_resvd_status &=
24128 	    ~(SD_RESERVE | SD_LOST_RESERVE | SD_WANT_RESERVE);
24129 	if (un->un_resvd_timeid) {
24130 		resvd_timeid_save = un->un_resvd_timeid;
24131 		un->un_resvd_timeid = NULL;
24132 		mutex_exit(SD_MUTEX(un));
24133 		(void) untimeout(resvd_timeid_save);
24134 	} else {
24135 		mutex_exit(SD_MUTEX(un));
24136 	}
24137 
24138 	/*
24139 	 * destroy any pending timeout thread that may be attempting to
24140 	 * reinstate reservation on this device.
24141 	 */
24142 	sd_rmv_resv_reclaim_req(dev);
24143 
24144 	if ((rval = sd_reserve_release(dev, SD_RELEASE)) == 0) {
24145 		mutex_enter(SD_MUTEX(un));
24146 		if ((un->un_mhd_token) &&
24147 		    ((un->un_resvd_status & SD_FAILFAST) == 0)) {
24148 			mutex_exit(SD_MUTEX(un));
24149 			(void) sd_check_mhd(dev, 0);
24150 		} else {
24151 			mutex_exit(SD_MUTEX(un));
24152 		}
24153 		(void) scsi_reset_notify(SD_ADDRESS(un), SCSI_RESET_CANCEL,
24154 		    sd_mhd_reset_notify_cb, (caddr_t)un);
24155 	} else {
24156 		/*
24157 		 * sd_mhd_watch_cb will restart the resvd recover timeout thread
24158 		 */
24159 		mutex_enter(SD_MUTEX(un));
24160 		un->un_resvd_status = resvd_status_save;
24161 		mutex_exit(SD_MUTEX(un));
24162 	}
24163 	return (rval);
24164 }
24165 
24166 
24167 /*
24168  *    Function: sd_mhdioc_register_devid
24169  *
24170  * Description: This routine is the driver entry point for handling ioctl
24171  *		requests to register the device id (MHIOCREREGISTERDEVID).
24172  *
24173  *		Note: The implementation for this ioctl has been updated to
24174  *		be consistent with the original PSARC case (1999/357)
24175  *		(4375899, 4241671, 4220005)
24176  *
24177  *   Arguments: dev	- the device number
24178  *
24179  * Return Code: 0
24180  *		ENXIO
24181  */
24182 
24183 static int
24184 sd_mhdioc_register_devid(dev_t dev)
24185 {
24186 	struct sd_lun	*un = NULL;
24187 	int		rval = 0;
24188 
24189 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24190 		return (ENXIO);
24191 	}
24192 
24193 	ASSERT(!mutex_owned(SD_MUTEX(un)));
24194 
24195 	mutex_enter(SD_MUTEX(un));
24196 
24197 	/* If a devid already exists, de-register it */
24198 	if (un->un_devid != NULL) {
24199 		ddi_devid_unregister(SD_DEVINFO(un));
24200 		/*
24201 		 * After unregister devid, needs to free devid memory
24202 		 */
24203 		ddi_devid_free(un->un_devid);
24204 		un->un_devid = NULL;
24205 	}
24206 
24207 	/* Check for reservation conflict */
24208 	mutex_exit(SD_MUTEX(un));
24209 	rval = sd_send_scsi_TEST_UNIT_READY(un, 0);
24210 	mutex_enter(SD_MUTEX(un));
24211 
24212 	switch (rval) {
24213 	case 0:
24214 		sd_register_devid(un, SD_DEVINFO(un), SD_TARGET_IS_UNRESERVED);
24215 		break;
24216 	case EACCES:
24217 		break;
24218 	default:
24219 		rval = EIO;
24220 	}
24221 
24222 	mutex_exit(SD_MUTEX(un));
24223 	return (rval);
24224 }
24225 
24226 
24227 /*
24228  *    Function: sd_mhdioc_inkeys
24229  *
24230  * Description: This routine is the driver entry point for handling ioctl
24231  *		requests to issue the SCSI-3 Persistent In Read Keys command
24232  *		to the device (MHIOCGRP_INKEYS).
24233  *
24234  *   Arguments: dev	- the device number
24235  *		arg	- user provided in_keys structure
24236  *		flag	- this argument is a pass through to ddi_copyxxx()
24237  *			  directly from the mode argument of ioctl().
24238  *
24239  * Return Code: code returned by sd_persistent_reservation_in_read_keys()
24240  *		ENXIO
24241  *		EFAULT
24242  */
24243 
24244 static int
24245 sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag)
24246 {
24247 	struct sd_lun		*un;
24248 	mhioc_inkeys_t		inkeys;
24249 	int			rval = 0;
24250 
24251 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24252 		return (ENXIO);
24253 	}
24254 
24255 #ifdef _MULTI_DATAMODEL
24256 	switch (ddi_model_convert_from(flag & FMODELS)) {
24257 	case DDI_MODEL_ILP32: {
24258 		struct mhioc_inkeys32	inkeys32;
24259 
24260 		if (ddi_copyin(arg, &inkeys32,
24261 		    sizeof (struct mhioc_inkeys32), flag) != 0) {
24262 			return (EFAULT);
24263 		}
24264 		inkeys.li = (mhioc_key_list_t *)(uintptr_t)inkeys32.li;
24265 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24266 		    &inkeys, flag)) != 0) {
24267 			return (rval);
24268 		}
24269 		inkeys32.generation = inkeys.generation;
24270 		if (ddi_copyout(&inkeys32, arg, sizeof (struct mhioc_inkeys32),
24271 		    flag) != 0) {
24272 			return (EFAULT);
24273 		}
24274 		break;
24275 	}
24276 	case DDI_MODEL_NONE:
24277 		if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t),
24278 		    flag) != 0) {
24279 			return (EFAULT);
24280 		}
24281 		if ((rval = sd_persistent_reservation_in_read_keys(un,
24282 		    &inkeys, flag)) != 0) {
24283 			return (rval);
24284 		}
24285 		if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t),
24286 		    flag) != 0) {
24287 			return (EFAULT);
24288 		}
24289 		break;
24290 	}
24291 
24292 #else /* ! _MULTI_DATAMODEL */
24293 
24294 	if (ddi_copyin(arg, &inkeys, sizeof (mhioc_inkeys_t), flag) != 0) {
24295 		return (EFAULT);
24296 	}
24297 	rval = sd_persistent_reservation_in_read_keys(un, &inkeys, flag);
24298 	if (rval != 0) {
24299 		return (rval);
24300 	}
24301 	if (ddi_copyout(&inkeys, arg, sizeof (mhioc_inkeys_t), flag) != 0) {
24302 		return (EFAULT);
24303 	}
24304 
24305 #endif /* _MULTI_DATAMODEL */
24306 
24307 	return (rval);
24308 }
24309 
24310 
24311 /*
24312  *    Function: sd_mhdioc_inresv
24313  *
24314  * Description: This routine is the driver entry point for handling ioctl
24315  *		requests to issue the SCSI-3 Persistent In Read Reservations
24316  *		command to the device (MHIOCGRP_INKEYS).
24317  *
24318  *   Arguments: dev	- the device number
24319  *		arg	- user provided in_resv structure
24320  *		flag	- this argument is a pass through to ddi_copyxxx()
24321  *			  directly from the mode argument of ioctl().
24322  *
24323  * Return Code: code returned by sd_persistent_reservation_in_read_resv()
24324  *		ENXIO
24325  *		EFAULT
24326  */
24327 
24328 static int
24329 sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag)
24330 {
24331 	struct sd_lun		*un;
24332 	mhioc_inresvs_t		inresvs;
24333 	int			rval = 0;
24334 
24335 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24336 		return (ENXIO);
24337 	}
24338 
24339 #ifdef _MULTI_DATAMODEL
24340 
24341 	switch (ddi_model_convert_from(flag & FMODELS)) {
24342 	case DDI_MODEL_ILP32: {
24343 		struct mhioc_inresvs32	inresvs32;
24344 
24345 		if (ddi_copyin(arg, &inresvs32,
24346 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24347 			return (EFAULT);
24348 		}
24349 		inresvs.li = (mhioc_resv_desc_list_t *)(uintptr_t)inresvs32.li;
24350 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24351 		    &inresvs, flag)) != 0) {
24352 			return (rval);
24353 		}
24354 		inresvs32.generation = inresvs.generation;
24355 		if (ddi_copyout(&inresvs32, arg,
24356 		    sizeof (struct mhioc_inresvs32), flag) != 0) {
24357 			return (EFAULT);
24358 		}
24359 		break;
24360 	}
24361 	case DDI_MODEL_NONE:
24362 		if (ddi_copyin(arg, &inresvs,
24363 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24364 			return (EFAULT);
24365 		}
24366 		if ((rval = sd_persistent_reservation_in_read_resv(un,
24367 		    &inresvs, flag)) != 0) {
24368 			return (rval);
24369 		}
24370 		if (ddi_copyout(&inresvs, arg,
24371 		    sizeof (mhioc_inresvs_t), flag) != 0) {
24372 			return (EFAULT);
24373 		}
24374 		break;
24375 	}
24376 
24377 #else /* ! _MULTI_DATAMODEL */
24378 
24379 	if (ddi_copyin(arg, &inresvs, sizeof (mhioc_inresvs_t), flag) != 0) {
24380 		return (EFAULT);
24381 	}
24382 	rval = sd_persistent_reservation_in_read_resv(un, &inresvs, flag);
24383 	if (rval != 0) {
24384 		return (rval);
24385 	}
24386 	if (ddi_copyout(&inresvs, arg, sizeof (mhioc_inresvs_t), flag)) {
24387 		return (EFAULT);
24388 	}
24389 
24390 #endif /* ! _MULTI_DATAMODEL */
24391 
24392 	return (rval);
24393 }
24394 
24395 
24396 /*
24397  * The following routines support the clustering functionality described below
24398  * and implement lost reservation reclaim functionality.
24399  *
24400  * Clustering
24401  * ----------
24402  * The clustering code uses two different, independent forms of SCSI
24403  * reservation. Traditional SCSI-2 Reserve/Release and the newer SCSI-3
24404  * Persistent Group Reservations. For any particular disk, it will use either
24405  * SCSI-2 or SCSI-3 PGR but never both at the same time for the same disk.
24406  *
24407  * SCSI-2
24408  * The cluster software takes ownership of a multi-hosted disk by issuing the
24409  * MHIOCTKOWN ioctl to the disk driver. It releases ownership by issuing the
24410  * MHIOCRELEASE ioctl.Closely related is the MHIOCENFAILFAST ioctl -- a cluster,
24411  * just after taking ownership of the disk with the MHIOCTKOWN ioctl then issues
24412  * the MHIOCENFAILFAST ioctl.  This ioctl "enables failfast" in the driver. The
24413  * meaning of failfast is that if the driver (on this host) ever encounters the
24414  * scsi error return code RESERVATION_CONFLICT from the device, it should
24415  * immediately panic the host. The motivation for this ioctl is that if this
24416  * host does encounter reservation conflict, the underlying cause is that some
24417  * other host of the cluster has decided that this host is no longer in the
24418  * cluster and has seized control of the disks for itself. Since this host is no
24419  * longer in the cluster, it ought to panic itself. The MHIOCENFAILFAST ioctl
24420  * does two things:
24421  *	(a) it sets a flag that will cause any returned RESERVATION_CONFLICT
24422  *      error to panic the host
24423  *      (b) it sets up a periodic timer to test whether this host still has
24424  *      "access" (in that no other host has reserved the device):  if the
24425  *      periodic timer gets RESERVATION_CONFLICT, the host is panicked. The
24426  *      purpose of that periodic timer is to handle scenarios where the host is
24427  *      otherwise temporarily quiescent, temporarily doing no real i/o.
24428  * The MHIOCTKOWN ioctl will "break" a reservation that is held by another host,
24429  * by issuing a SCSI Bus Device Reset.  It will then issue a SCSI Reserve for
24430  * the device itself.
24431  *
24432  * SCSI-3 PGR
24433  * A direct semantic implementation of the SCSI-3 Persistent Reservation
24434  * facility is supported through the shared multihost disk ioctls
24435  * (MHIOCGRP_INKEYS, MHIOCGRP_INRESV, MHIOCGRP_REGISTER, MHIOCGRP_RESERVE,
24436  * MHIOCGRP_PREEMPTANDABORT)
24437  *
24438  * Reservation Reclaim:
24439  * --------------------
24440  * To support the lost reservation reclaim operations this driver creates a
24441  * single thread to handle reinstating reservations on all devices that have
24442  * lost reservations sd_resv_reclaim_requests are logged for all devices that
24443  * have LOST RESERVATIONS when the scsi watch facility callsback sd_mhd_watch_cb
24444  * and the reservation reclaim thread loops through the requests to regain the
24445  * lost reservations.
24446  */
24447 
24448 /*
24449  *    Function: sd_check_mhd()
24450  *
24451  * Description: This function sets up and submits a scsi watch request or
24452  *		terminates an existing watch request. This routine is used in
24453  *		support of reservation reclaim.
24454  *
24455  *   Arguments: dev    - the device 'dev_t' is used for context to discriminate
24456  *			 among multiple watches that share the callback function
24457  *		interval - the number of microseconds specifying the watch
24458  *			   interval for issuing TEST UNIT READY commands. If
24459  *			   set to 0 the watch should be terminated. If the
24460  *			   interval is set to 0 and if the device is required
24461  *			   to hold reservation while disabling failfast, the
24462  *			   watch is restarted with an interval of
24463  *			   reinstate_resv_delay.
24464  *
24465  * Return Code: 0	   - Successful submit/terminate of scsi watch request
24466  *		ENXIO      - Indicates an invalid device was specified
24467  *		EAGAIN     - Unable to submit the scsi watch request
24468  */
24469 
24470 static int
24471 sd_check_mhd(dev_t dev, int interval)
24472 {
24473 	struct sd_lun	*un;
24474 	opaque_t	token;
24475 
24476 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24477 		return (ENXIO);
24478 	}
24479 
24480 	/* is this a watch termination request? */
24481 	if (interval == 0) {
24482 		mutex_enter(SD_MUTEX(un));
24483 		/* if there is an existing watch task then terminate it */
24484 		if (un->un_mhd_token) {
24485 			token = un->un_mhd_token;
24486 			un->un_mhd_token = NULL;
24487 			mutex_exit(SD_MUTEX(un));
24488 			(void) scsi_watch_request_terminate(token,
24489 			    SCSI_WATCH_TERMINATE_WAIT);
24490 			mutex_enter(SD_MUTEX(un));
24491 		} else {
24492 			mutex_exit(SD_MUTEX(un));
24493 			/*
24494 			 * Note: If we return here we don't check for the
24495 			 * failfast case. This is the original legacy
24496 			 * implementation but perhaps we should be checking
24497 			 * the failfast case.
24498 			 */
24499 			return (0);
24500 		}
24501 		/*
24502 		 * If the device is required to hold reservation while
24503 		 * disabling failfast, we need to restart the scsi_watch
24504 		 * routine with an interval of reinstate_resv_delay.
24505 		 */
24506 		if (un->un_resvd_status & SD_RESERVE) {
24507 			interval = sd_reinstate_resv_delay/1000;
24508 		} else {
24509 			/* no failfast so bail */
24510 			mutex_exit(SD_MUTEX(un));
24511 			return (0);
24512 		}
24513 		mutex_exit(SD_MUTEX(un));
24514 	}
24515 
24516 	/*
24517 	 * adjust minimum time interval to 1 second,
24518 	 * and convert from msecs to usecs
24519 	 */
24520 	if (interval > 0 && interval < 1000) {
24521 		interval = 1000;
24522 	}
24523 	interval *= 1000;
24524 
24525 	/*
24526 	 * submit the request to the scsi_watch service
24527 	 */
24528 	token = scsi_watch_request_submit(SD_SCSI_DEVP(un), interval,
24529 	    SENSE_LENGTH, sd_mhd_watch_cb, (caddr_t)dev);
24530 	if (token == NULL) {
24531 		return (EAGAIN);
24532 	}
24533 
24534 	/*
24535 	 * save token for termination later on
24536 	 */
24537 	mutex_enter(SD_MUTEX(un));
24538 	un->un_mhd_token = token;
24539 	mutex_exit(SD_MUTEX(un));
24540 	return (0);
24541 }
24542 
24543 
24544 /*
24545  *    Function: sd_mhd_watch_cb()
24546  *
24547  * Description: This function is the call back function used by the scsi watch
24548  *		facility. The scsi watch facility sends the "Test Unit Ready"
24549  *		and processes the status. If applicable (i.e. a "Unit Attention"
24550  *		status and automatic "Request Sense" not used) the scsi watch
24551  *		facility will send a "Request Sense" and retrieve the sense data
24552  *		to be passed to this callback function. In either case the
24553  *		automatic "Request Sense" or the facility submitting one, this
24554  *		callback is passed the status and sense data.
24555  *
24556  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24557  *			among multiple watches that share this callback function
24558  *		resultp - scsi watch facility result packet containing scsi
24559  *			  packet, status byte and sense data
24560  *
24561  * Return Code: 0 - continue the watch task
24562  *		non-zero - terminate the watch task
24563  */
24564 
24565 static int
24566 sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
24567 {
24568 	struct sd_lun			*un;
24569 	struct scsi_status		*statusp;
24570 	struct scsi_extended_sense	*sensep;
24571 	struct scsi_pkt			*pkt;
24572 	uchar_t				actual_sense_length;
24573 	dev_t  				dev = (dev_t)arg;
24574 
24575 	ASSERT(resultp != NULL);
24576 	statusp			= resultp->statusp;
24577 	sensep			= resultp->sensep;
24578 	pkt			= resultp->pkt;
24579 	actual_sense_length	= resultp->actual_sense_length;
24580 
24581 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24582 		return (ENXIO);
24583 	}
24584 
24585 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
24586 	    "sd_mhd_watch_cb: reason '%s', status '%s'\n",
24587 	    scsi_rname(pkt->pkt_reason), sd_sname(*((unsigned char *)statusp)));
24588 
24589 	/* Begin processing of the status and/or sense data */
24590 	if (pkt->pkt_reason != CMD_CMPLT) {
24591 		/* Handle the incomplete packet */
24592 		sd_mhd_watch_incomplete(un, pkt);
24593 		return (0);
24594 	} else if (*((unsigned char *)statusp) != STATUS_GOOD) {
24595 		if (*((unsigned char *)statusp)
24596 		    == STATUS_RESERVATION_CONFLICT) {
24597 			/*
24598 			 * Handle a reservation conflict by panicking if
24599 			 * configured for failfast or by logging the conflict
24600 			 * and updating the reservation status
24601 			 */
24602 			mutex_enter(SD_MUTEX(un));
24603 			if ((un->un_resvd_status & SD_FAILFAST) &&
24604 			    (sd_failfast_enable)) {
24605 				panic("Reservation Conflict");
24606 				/*NOTREACHED*/
24607 			}
24608 			SD_INFO(SD_LOG_IOCTL_MHD, un,
24609 			    "sd_mhd_watch_cb: Reservation Conflict\n");
24610 			un->un_resvd_status |= SD_RESERVATION_CONFLICT;
24611 			mutex_exit(SD_MUTEX(un));
24612 		}
24613 	}
24614 
24615 	if (sensep != NULL) {
24616 		if (actual_sense_length >= (SENSE_LENGTH - 2)) {
24617 			mutex_enter(SD_MUTEX(un));
24618 			if ((sensep->es_add_code == SD_SCSI_RESET_SENSE_CODE) &&
24619 			    (un->un_resvd_status & SD_RESERVE)) {
24620 				/*
24621 				 * The additional sense code indicates a power
24622 				 * on or bus device reset has occurred; update
24623 				 * the reservation status.
24624 				 */
24625 				un->un_resvd_status |=
24626 				    (SD_LOST_RESERVE | SD_WANT_RESERVE);
24627 				SD_INFO(SD_LOG_IOCTL_MHD, un,
24628 				    "sd_mhd_watch_cb: Lost Reservation\n");
24629 			}
24630 		} else {
24631 			return (0);
24632 		}
24633 	} else {
24634 		mutex_enter(SD_MUTEX(un));
24635 	}
24636 
24637 	if ((un->un_resvd_status & SD_RESERVE) &&
24638 	    (un->un_resvd_status & SD_LOST_RESERVE)) {
24639 		if (un->un_resvd_status & SD_WANT_RESERVE) {
24640 			/*
24641 			 * A reset occurred in between the last probe and this
24642 			 * one so if a timeout is pending cancel it.
24643 			 */
24644 			if (un->un_resvd_timeid) {
24645 				timeout_id_t temp_id = un->un_resvd_timeid;
24646 				un->un_resvd_timeid = NULL;
24647 				mutex_exit(SD_MUTEX(un));
24648 				(void) untimeout(temp_id);
24649 				mutex_enter(SD_MUTEX(un));
24650 			}
24651 			un->un_resvd_status &= ~SD_WANT_RESERVE;
24652 		}
24653 		if (un->un_resvd_timeid == 0) {
24654 			/* Schedule a timeout to handle the lost reservation */
24655 			un->un_resvd_timeid = timeout(sd_mhd_resvd_recover,
24656 			    (void *)dev,
24657 			    drv_usectohz(sd_reinstate_resv_delay));
24658 		}
24659 	}
24660 	mutex_exit(SD_MUTEX(un));
24661 	return (0);
24662 }
24663 
24664 
24665 /*
24666  *    Function: sd_mhd_watch_incomplete()
24667  *
24668  * Description: This function is used to find out why a scsi pkt sent by the
24669  *		scsi watch facility was not completed. Under some scenarios this
24670  *		routine will return. Otherwise it will send a bus reset to see
24671  *		if the drive is still online.
24672  *
24673  *   Arguments: un  - driver soft state (unit) structure
24674  *		pkt - incomplete scsi pkt
24675  */
24676 
24677 static void
24678 sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt)
24679 {
24680 	int	be_chatty;
24681 	int	perr;
24682 
24683 	ASSERT(pkt != NULL);
24684 	ASSERT(un != NULL);
24685 	be_chatty	= (!(pkt->pkt_flags & FLAG_SILENT));
24686 	perr		= (pkt->pkt_statistics & STAT_PERR);
24687 
24688 	mutex_enter(SD_MUTEX(un));
24689 	if (un->un_state == SD_STATE_DUMPING) {
24690 		mutex_exit(SD_MUTEX(un));
24691 		return;
24692 	}
24693 
24694 	switch (pkt->pkt_reason) {
24695 	case CMD_UNX_BUS_FREE:
24696 		/*
24697 		 * If we had a parity error that caused the target to drop BSY*,
24698 		 * don't be chatty about it.
24699 		 */
24700 		if (perr && be_chatty) {
24701 			be_chatty = 0;
24702 		}
24703 		break;
24704 	case CMD_TAG_REJECT:
24705 		/*
24706 		 * The SCSI-2 spec states that a tag reject will be sent by the
24707 		 * target if tagged queuing is not supported. A tag reject may
24708 		 * also be sent during certain initialization periods or to
24709 		 * control internal resources. For the latter case the target
24710 		 * may also return Queue Full.
24711 		 *
24712 		 * If this driver receives a tag reject from a target that is
24713 		 * going through an init period or controlling internal
24714 		 * resources tagged queuing will be disabled. This is a less
24715 		 * than optimal behavior but the driver is unable to determine
24716 		 * the target state and assumes tagged queueing is not supported
24717 		 */
24718 		pkt->pkt_flags = 0;
24719 		un->un_tagflags = 0;
24720 
24721 		if (un->un_f_opt_queueing == TRUE) {
24722 			un->un_throttle = min(un->un_throttle, 3);
24723 		} else {
24724 			un->un_throttle = 1;
24725 		}
24726 		mutex_exit(SD_MUTEX(un));
24727 		(void) scsi_ifsetcap(SD_ADDRESS(un), "tagged-qing", 0, 1);
24728 		mutex_enter(SD_MUTEX(un));
24729 		break;
24730 	case CMD_INCOMPLETE:
24731 		/*
24732 		 * The transport stopped with an abnormal state, fallthrough and
24733 		 * reset the target and/or bus unless selection did not complete
24734 		 * (indicated by STATE_GOT_BUS) in which case we don't want to
24735 		 * go through a target/bus reset
24736 		 */
24737 		if (pkt->pkt_state == STATE_GOT_BUS) {
24738 			break;
24739 		}
24740 		/*FALLTHROUGH*/
24741 
24742 	case CMD_TIMEOUT:
24743 	default:
24744 		/*
24745 		 * The lun may still be running the command, so a lun reset
24746 		 * should be attempted. If the lun reset fails or cannot be
24747 		 * issued, than try a target reset. Lastly try a bus reset.
24748 		 */
24749 		if ((pkt->pkt_statistics &
24750 		    (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED)) == 0) {
24751 			int reset_retval = 0;
24752 			mutex_exit(SD_MUTEX(un));
24753 			if (un->un_f_allow_bus_device_reset == TRUE) {
24754 				if (un->un_f_lun_reset_enabled == TRUE) {
24755 					reset_retval =
24756 					    scsi_reset(SD_ADDRESS(un),
24757 					    RESET_LUN);
24758 				}
24759 				if (reset_retval == 0) {
24760 					reset_retval =
24761 					    scsi_reset(SD_ADDRESS(un),
24762 					    RESET_TARGET);
24763 				}
24764 			}
24765 			if (reset_retval == 0) {
24766 				(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
24767 			}
24768 			mutex_enter(SD_MUTEX(un));
24769 		}
24770 		break;
24771 	}
24772 
24773 	/* A device/bus reset has occurred; update the reservation status. */
24774 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
24775 	    (STAT_BUS_RESET | STAT_DEV_RESET))) {
24776 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
24777 			un->un_resvd_status |=
24778 			    (SD_LOST_RESERVE | SD_WANT_RESERVE);
24779 			SD_INFO(SD_LOG_IOCTL_MHD, un,
24780 			    "sd_mhd_watch_incomplete: Lost Reservation\n");
24781 		}
24782 	}
24783 
24784 	/*
24785 	 * The disk has been turned off; Update the device state.
24786 	 *
24787 	 * Note: Should we be offlining the disk here?
24788 	 */
24789 	if (pkt->pkt_state == STATE_GOT_BUS) {
24790 		SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_watch_incomplete: "
24791 		    "Disk not responding to selection\n");
24792 		if (un->un_state != SD_STATE_OFFLINE) {
24793 			New_state(un, SD_STATE_OFFLINE);
24794 		}
24795 	} else if (be_chatty) {
24796 		/*
24797 		 * suppress messages if they are all the same pkt reason;
24798 		 * with TQ, many (up to 256) are returned with the same
24799 		 * pkt_reason
24800 		 */
24801 		if (pkt->pkt_reason != un->un_last_pkt_reason) {
24802 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
24803 			    "sd_mhd_watch_incomplete: "
24804 			    "SCSI transport failed: reason '%s'\n",
24805 			    scsi_rname(pkt->pkt_reason));
24806 		}
24807 	}
24808 	un->un_last_pkt_reason = pkt->pkt_reason;
24809 	mutex_exit(SD_MUTEX(un));
24810 }
24811 
24812 
24813 /*
24814  *    Function: sd_sname()
24815  *
24816  * Description: This is a simple little routine to return a string containing
24817  *		a printable description of command status byte for use in
24818  *		logging.
24819  *
24820  *   Arguments: status - pointer to a status byte
24821  *
24822  * Return Code: char * - string containing status description.
24823  */
24824 
24825 static char *
24826 sd_sname(uchar_t status)
24827 {
24828 	switch (status & STATUS_MASK) {
24829 	case STATUS_GOOD:
24830 		return ("good status");
24831 	case STATUS_CHECK:
24832 		return ("check condition");
24833 	case STATUS_MET:
24834 		return ("condition met");
24835 	case STATUS_BUSY:
24836 		return ("busy");
24837 	case STATUS_INTERMEDIATE:
24838 		return ("intermediate");
24839 	case STATUS_INTERMEDIATE_MET:
24840 		return ("intermediate - condition met");
24841 	case STATUS_RESERVATION_CONFLICT:
24842 		return ("reservation_conflict");
24843 	case STATUS_TERMINATED:
24844 		return ("command terminated");
24845 	case STATUS_QFULL:
24846 		return ("queue full");
24847 	default:
24848 		return ("<unknown status>");
24849 	}
24850 }
24851 
24852 
24853 /*
24854  *    Function: sd_mhd_resvd_recover()
24855  *
24856  * Description: This function adds a reservation entry to the
24857  *		sd_resv_reclaim_request list and signals the reservation
24858  *		reclaim thread that there is work pending. If the reservation
24859  *		reclaim thread has not been previously created this function
24860  *		will kick it off.
24861  *
24862  *   Arguments: arg -   the device 'dev_t' is used for context to discriminate
24863  *			among multiple watches that share this callback function
24864  *
24865  *     Context: This routine is called by timeout() and is run in interrupt
24866  *		context. It must not sleep or call other functions which may
24867  *		sleep.
24868  */
24869 
24870 static void
24871 sd_mhd_resvd_recover(void *arg)
24872 {
24873 	dev_t			dev = (dev_t)arg;
24874 	struct sd_lun		*un;
24875 	struct sd_thr_request	*sd_treq = NULL;
24876 	struct sd_thr_request	*sd_cur = NULL;
24877 	struct sd_thr_request	*sd_prev = NULL;
24878 	int			already_there = 0;
24879 
24880 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
24881 		return;
24882 	}
24883 
24884 	mutex_enter(SD_MUTEX(un));
24885 	un->un_resvd_timeid = NULL;
24886 	if (un->un_resvd_status & SD_WANT_RESERVE) {
24887 		/*
24888 		 * There was a reset so don't issue the reserve, allow the
24889 		 * sd_mhd_watch_cb callback function to notice this and
24890 		 * reschedule the timeout for reservation.
24891 		 */
24892 		mutex_exit(SD_MUTEX(un));
24893 		return;
24894 	}
24895 	mutex_exit(SD_MUTEX(un));
24896 
24897 	/*
24898 	 * Add this device to the sd_resv_reclaim_request list and the
24899 	 * sd_resv_reclaim_thread should take care of the rest.
24900 	 *
24901 	 * Note: We can't sleep in this context so if the memory allocation
24902 	 * fails allow the sd_mhd_watch_cb callback function to notice this and
24903 	 * reschedule the timeout for reservation.  (4378460)
24904 	 */
24905 	sd_treq = (struct sd_thr_request *)
24906 	    kmem_zalloc(sizeof (struct sd_thr_request), KM_NOSLEEP);
24907 	if (sd_treq == NULL) {
24908 		return;
24909 	}
24910 
24911 	sd_treq->sd_thr_req_next = NULL;
24912 	sd_treq->dev = dev;
24913 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
24914 	if (sd_tr.srq_thr_req_head == NULL) {
24915 		sd_tr.srq_thr_req_head = sd_treq;
24916 	} else {
24917 		sd_cur = sd_prev = sd_tr.srq_thr_req_head;
24918 		for (; sd_cur != NULL; sd_cur = sd_cur->sd_thr_req_next) {
24919 			if (sd_cur->dev == dev) {
24920 				/*
24921 				 * already in Queue so don't log
24922 				 * another request for the device
24923 				 */
24924 				already_there = 1;
24925 				break;
24926 			}
24927 			sd_prev = sd_cur;
24928 		}
24929 		if (!already_there) {
24930 			SD_INFO(SD_LOG_IOCTL_MHD, un, "sd_mhd_resvd_recover: "
24931 			    "logging request for %lx\n", dev);
24932 			sd_prev->sd_thr_req_next = sd_treq;
24933 		} else {
24934 			kmem_free(sd_treq, sizeof (struct sd_thr_request));
24935 		}
24936 	}
24937 
24938 	/*
24939 	 * Create a kernel thread to do the reservation reclaim and free up this
24940 	 * thread. We cannot block this thread while we go away to do the
24941 	 * reservation reclaim
24942 	 */
24943 	if (sd_tr.srq_resv_reclaim_thread == NULL)
24944 		sd_tr.srq_resv_reclaim_thread = thread_create(NULL, 0,
24945 		    sd_resv_reclaim_thread, NULL,
24946 		    0, &p0, TS_RUN, v.v_maxsyspri - 2);
24947 
24948 	/* Tell the reservation reclaim thread that it has work to do */
24949 	cv_signal(&sd_tr.srq_resv_reclaim_cv);
24950 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
24951 }
24952 
24953 /*
24954  *    Function: sd_resv_reclaim_thread()
24955  *
24956  * Description: This function implements the reservation reclaim operations
24957  *
24958  *   Arguments: arg - the device 'dev_t' is used for context to discriminate
24959  *		      among multiple watches that share this callback function
24960  */
24961 
24962 static void
24963 sd_resv_reclaim_thread()
24964 {
24965 	struct sd_lun		*un;
24966 	struct sd_thr_request	*sd_mhreq;
24967 
24968 	/* Wait for work */
24969 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
24970 	if (sd_tr.srq_thr_req_head == NULL) {
24971 		cv_wait(&sd_tr.srq_resv_reclaim_cv,
24972 		    &sd_tr.srq_resv_reclaim_mutex);
24973 	}
24974 
24975 	/* Loop while we have work */
24976 	while ((sd_tr.srq_thr_cur_req = sd_tr.srq_thr_req_head) != NULL) {
24977 		un = ddi_get_soft_state(sd_state,
24978 		    SDUNIT(sd_tr.srq_thr_cur_req->dev));
24979 		if (un == NULL) {
24980 			/*
24981 			 * softstate structure is NULL so just
24982 			 * dequeue the request and continue
24983 			 */
24984 			sd_tr.srq_thr_req_head =
24985 			    sd_tr.srq_thr_cur_req->sd_thr_req_next;
24986 			kmem_free(sd_tr.srq_thr_cur_req,
24987 			    sizeof (struct sd_thr_request));
24988 			continue;
24989 		}
24990 
24991 		/* dequeue the request */
24992 		sd_mhreq = sd_tr.srq_thr_cur_req;
24993 		sd_tr.srq_thr_req_head =
24994 		    sd_tr.srq_thr_cur_req->sd_thr_req_next;
24995 		mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
24996 
24997 		/*
24998 		 * Reclaim reservation only if SD_RESERVE is still set. There
24999 		 * may have been a call to MHIOCRELEASE before we got here.
25000 		 */
25001 		mutex_enter(SD_MUTEX(un));
25002 		if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25003 			/*
25004 			 * Note: The SD_LOST_RESERVE flag is cleared before
25005 			 * reclaiming the reservation. If this is done after the
25006 			 * call to sd_reserve_release a reservation loss in the
25007 			 * window between pkt completion of reserve cmd and
25008 			 * mutex_enter below may not be recognized
25009 			 */
25010 			un->un_resvd_status &= ~SD_LOST_RESERVE;
25011 			mutex_exit(SD_MUTEX(un));
25012 
25013 			if (sd_reserve_release(sd_mhreq->dev,
25014 			    SD_RESERVE) == 0) {
25015 				mutex_enter(SD_MUTEX(un));
25016 				un->un_resvd_status |= SD_RESERVE;
25017 				mutex_exit(SD_MUTEX(un));
25018 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25019 				    "sd_resv_reclaim_thread: "
25020 				    "Reservation Recovered\n");
25021 			} else {
25022 				mutex_enter(SD_MUTEX(un));
25023 				un->un_resvd_status |= SD_LOST_RESERVE;
25024 				mutex_exit(SD_MUTEX(un));
25025 				SD_INFO(SD_LOG_IOCTL_MHD, un,
25026 				    "sd_resv_reclaim_thread: Failed "
25027 				    "Reservation Recovery\n");
25028 			}
25029 		} else {
25030 			mutex_exit(SD_MUTEX(un));
25031 		}
25032 		mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25033 		ASSERT(sd_mhreq == sd_tr.srq_thr_cur_req);
25034 		kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25035 		sd_mhreq = sd_tr.srq_thr_cur_req = NULL;
25036 		/*
25037 		 * wakeup the destroy thread if anyone is waiting on
25038 		 * us to complete.
25039 		 */
25040 		cv_signal(&sd_tr.srq_inprocess_cv);
25041 		SD_TRACE(SD_LOG_IOCTL_MHD, un,
25042 		    "sd_resv_reclaim_thread: cv_signalling current request \n");
25043 	}
25044 
25045 	/*
25046 	 * cleanup the sd_tr structure now that this thread will not exist
25047 	 */
25048 	ASSERT(sd_tr.srq_thr_req_head == NULL);
25049 	ASSERT(sd_tr.srq_thr_cur_req == NULL);
25050 	sd_tr.srq_resv_reclaim_thread = NULL;
25051 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25052 	thread_exit();
25053 }
25054 
25055 
25056 /*
25057  *    Function: sd_rmv_resv_reclaim_req()
25058  *
25059  * Description: This function removes any pending reservation reclaim requests
25060  *		for the specified device.
25061  *
25062  *   Arguments: dev - the device 'dev_t'
25063  */
25064 
25065 static void
25066 sd_rmv_resv_reclaim_req(dev_t dev)
25067 {
25068 	struct sd_thr_request *sd_mhreq;
25069 	struct sd_thr_request *sd_prev;
25070 
25071 	/* Remove a reservation reclaim request from the list */
25072 	mutex_enter(&sd_tr.srq_resv_reclaim_mutex);
25073 	if (sd_tr.srq_thr_cur_req && sd_tr.srq_thr_cur_req->dev == dev) {
25074 		/*
25075 		 * We are attempting to reinstate reservation for
25076 		 * this device. We wait for sd_reserve_release()
25077 		 * to return before we return.
25078 		 */
25079 		cv_wait(&sd_tr.srq_inprocess_cv,
25080 		    &sd_tr.srq_resv_reclaim_mutex);
25081 	} else {
25082 		sd_prev = sd_mhreq = sd_tr.srq_thr_req_head;
25083 		if (sd_mhreq && sd_mhreq->dev == dev) {
25084 			sd_tr.srq_thr_req_head = sd_mhreq->sd_thr_req_next;
25085 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25086 			mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25087 			return;
25088 		}
25089 		for (; sd_mhreq != NULL; sd_mhreq = sd_mhreq->sd_thr_req_next) {
25090 			if (sd_mhreq && sd_mhreq->dev == dev) {
25091 				break;
25092 			}
25093 			sd_prev = sd_mhreq;
25094 		}
25095 		if (sd_mhreq != NULL) {
25096 			sd_prev->sd_thr_req_next = sd_mhreq->sd_thr_req_next;
25097 			kmem_free(sd_mhreq, sizeof (struct sd_thr_request));
25098 		}
25099 	}
25100 	mutex_exit(&sd_tr.srq_resv_reclaim_mutex);
25101 }
25102 
25103 
25104 /*
25105  *    Function: sd_mhd_reset_notify_cb()
25106  *
25107  * Description: This is a call back function for scsi_reset_notify. This
25108  *		function updates the softstate reserved status and logs the
25109  *		reset. The driver scsi watch facility callback function
25110  *		(sd_mhd_watch_cb) and reservation reclaim thread functionality
25111  *		will reclaim the reservation.
25112  *
25113  *   Arguments: arg  - driver soft state (unit) structure
25114  */
25115 
25116 static void
25117 sd_mhd_reset_notify_cb(caddr_t arg)
25118 {
25119 	struct sd_lun *un = (struct sd_lun *)arg;
25120 
25121 	mutex_enter(SD_MUTEX(un));
25122 	if ((un->un_resvd_status & SD_RESERVE) == SD_RESERVE) {
25123 		un->un_resvd_status |= (SD_LOST_RESERVE | SD_WANT_RESERVE);
25124 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25125 		    "sd_mhd_reset_notify_cb: Lost Reservation\n");
25126 	}
25127 	mutex_exit(SD_MUTEX(un));
25128 }
25129 
25130 
25131 /*
25132  *    Function: sd_take_ownership()
25133  *
25134  * Description: This routine implements an algorithm to achieve a stable
25135  *		reservation on disks which don't implement priority reserve,
25136  *		and makes sure that other host lose re-reservation attempts.
25137  *		This algorithm contains of a loop that keeps issuing the RESERVE
25138  *		for some period of time (min_ownership_delay, default 6 seconds)
25139  *		During that loop, it looks to see if there has been a bus device
25140  *		reset or bus reset (both of which cause an existing reservation
25141  *		to be lost). If the reservation is lost issue RESERVE until a
25142  *		period of min_ownership_delay with no resets has gone by, or
25143  *		until max_ownership_delay has expired. This loop ensures that
25144  *		the host really did manage to reserve the device, in spite of
25145  *		resets. The looping for min_ownership_delay (default six
25146  *		seconds) is important to early generation clustering products,
25147  *		Solstice HA 1.x and Sun Cluster 2.x. Those products use an
25148  *		MHIOCENFAILFAST periodic timer of two seconds. By having
25149  *		MHIOCTKOWN issue Reserves in a loop for six seconds, and having
25150  *		MHIOCENFAILFAST poll every two seconds, the idea is that by the
25151  *		time the MHIOCTKOWN ioctl returns, the other host (if any) will
25152  *		have already noticed, via the MHIOCENFAILFAST polling, that it
25153  *		no longer "owns" the disk and will have panicked itself.  Thus,
25154  *		the host issuing the MHIOCTKOWN is assured (with timing
25155  *		dependencies) that by the time it actually starts to use the
25156  *		disk for real work, the old owner is no longer accessing it.
25157  *
25158  *		min_ownership_delay is the minimum amount of time for which the
25159  *		disk must be reserved continuously devoid of resets before the
25160  *		MHIOCTKOWN ioctl will return success.
25161  *
25162  *		max_ownership_delay indicates the amount of time by which the
25163  *		take ownership should succeed or timeout with an error.
25164  *
25165  *   Arguments: dev - the device 'dev_t'
25166  *		*p  - struct containing timing info.
25167  *
25168  * Return Code: 0 for success or error code
25169  */
25170 
25171 static int
25172 sd_take_ownership(dev_t dev, struct mhioctkown *p)
25173 {
25174 	struct sd_lun	*un;
25175 	int		rval;
25176 	int		err;
25177 	int		reservation_count   = 0;
25178 	int		min_ownership_delay =  6000000; /* in usec */
25179 	int		max_ownership_delay = 30000000; /* in usec */
25180 	clock_t		start_time;	/* starting time of this algorithm */
25181 	clock_t		end_time;	/* time limit for giving up */
25182 	clock_t		ownership_time;	/* time limit for stable ownership */
25183 	clock_t		current_time;
25184 	clock_t		previous_current_time;
25185 
25186 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25187 		return (ENXIO);
25188 	}
25189 
25190 	/*
25191 	 * Attempt a device reservation. A priority reservation is requested.
25192 	 */
25193 	if ((rval = sd_reserve_release(dev, SD_PRIORITY_RESERVE))
25194 	    != SD_SUCCESS) {
25195 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
25196 		    "sd_take_ownership: return(1)=%d\n", rval);
25197 		return (rval);
25198 	}
25199 
25200 	/* Update the softstate reserved status to indicate the reservation */
25201 	mutex_enter(SD_MUTEX(un));
25202 	un->un_resvd_status |= SD_RESERVE;
25203 	un->un_resvd_status &=
25204 	    ~(SD_LOST_RESERVE | SD_WANT_RESERVE | SD_RESERVATION_CONFLICT);
25205 	mutex_exit(SD_MUTEX(un));
25206 
25207 	if (p != NULL) {
25208 		if (p->min_ownership_delay != 0) {
25209 			min_ownership_delay = p->min_ownership_delay * 1000;
25210 		}
25211 		if (p->max_ownership_delay != 0) {
25212 			max_ownership_delay = p->max_ownership_delay * 1000;
25213 		}
25214 	}
25215 	SD_INFO(SD_LOG_IOCTL_MHD, un,
25216 	    "sd_take_ownership: min, max delays: %d, %d\n",
25217 	    min_ownership_delay, max_ownership_delay);
25218 
25219 	start_time = ddi_get_lbolt();
25220 	current_time	= start_time;
25221 	ownership_time	= current_time + drv_usectohz(min_ownership_delay);
25222 	end_time	= start_time + drv_usectohz(max_ownership_delay);
25223 
25224 	while (current_time - end_time < 0) {
25225 		delay(drv_usectohz(500000));
25226 
25227 		if ((err = sd_reserve_release(dev, SD_RESERVE)) != 0) {
25228 			if ((sd_reserve_release(dev, SD_RESERVE)) != 0) {
25229 				mutex_enter(SD_MUTEX(un));
25230 				rval = (un->un_resvd_status &
25231 				    SD_RESERVATION_CONFLICT) ? EACCES : EIO;
25232 				mutex_exit(SD_MUTEX(un));
25233 				break;
25234 			}
25235 		}
25236 		previous_current_time = current_time;
25237 		current_time = ddi_get_lbolt();
25238 		mutex_enter(SD_MUTEX(un));
25239 		if (err || (un->un_resvd_status & SD_LOST_RESERVE)) {
25240 			ownership_time = ddi_get_lbolt() +
25241 			    drv_usectohz(min_ownership_delay);
25242 			reservation_count = 0;
25243 		} else {
25244 			reservation_count++;
25245 		}
25246 		un->un_resvd_status |= SD_RESERVE;
25247 		un->un_resvd_status &= ~(SD_LOST_RESERVE | SD_WANT_RESERVE);
25248 		mutex_exit(SD_MUTEX(un));
25249 
25250 		SD_INFO(SD_LOG_IOCTL_MHD, un,
25251 		    "sd_take_ownership: ticks for loop iteration=%ld, "
25252 		    "reservation=%s\n", (current_time - previous_current_time),
25253 		    reservation_count ? "ok" : "reclaimed");
25254 
25255 		if (current_time - ownership_time >= 0 &&
25256 		    reservation_count >= 4) {
25257 			rval = 0; /* Achieved a stable ownership */
25258 			break;
25259 		}
25260 		if (current_time - end_time >= 0) {
25261 			rval = EACCES; /* No ownership in max possible time */
25262 			break;
25263 		}
25264 	}
25265 	SD_TRACE(SD_LOG_IOCTL_MHD, un,
25266 	    "sd_take_ownership: return(2)=%d\n", rval);
25267 	return (rval);
25268 }
25269 
25270 
25271 /*
25272  *    Function: sd_reserve_release()
25273  *
25274  * Description: This function builds and sends scsi RESERVE, RELEASE, and
25275  *		PRIORITY RESERVE commands based on a user specified command type
25276  *
25277  *   Arguments: dev - the device 'dev_t'
25278  *		cmd - user specified command type; one of SD_PRIORITY_RESERVE,
25279  *		      SD_RESERVE, SD_RELEASE
25280  *
25281  * Return Code: 0 or Error Code
25282  */
25283 
25284 static int
25285 sd_reserve_release(dev_t dev, int cmd)
25286 {
25287 	struct uscsi_cmd	*com = NULL;
25288 	struct sd_lun		*un = NULL;
25289 	char			cdb[CDB_GROUP0];
25290 	int			rval;
25291 
25292 	ASSERT((cmd == SD_RELEASE) || (cmd == SD_RESERVE) ||
25293 	    (cmd == SD_PRIORITY_RESERVE));
25294 
25295 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
25296 		return (ENXIO);
25297 	}
25298 
25299 	/* instantiate and initialize the command and cdb */
25300 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
25301 	bzero(cdb, CDB_GROUP0);
25302 	com->uscsi_flags   = USCSI_SILENT;
25303 	com->uscsi_timeout = un->un_reserve_release_time;
25304 	com->uscsi_cdblen  = CDB_GROUP0;
25305 	com->uscsi_cdb	   = cdb;
25306 	if (cmd == SD_RELEASE) {
25307 		cdb[0] = SCMD_RELEASE;
25308 	} else {
25309 		cdb[0] = SCMD_RESERVE;
25310 	}
25311 
25312 	/* Send the command. */
25313 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
25314 	    UIO_SYSSPACE, SD_PATH_STANDARD);
25315 
25316 	/*
25317 	 * "break" a reservation that is held by another host, by issuing a
25318 	 * reset if priority reserve is desired, and we could not get the
25319 	 * device.
25320 	 */
25321 	if ((cmd == SD_PRIORITY_RESERVE) &&
25322 	    (rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25323 		/*
25324 		 * First try to reset the LUN. If we cannot, then try a target
25325 		 * reset, followed by a bus reset if the target reset fails.
25326 		 */
25327 		int reset_retval = 0;
25328 		if (un->un_f_lun_reset_enabled == TRUE) {
25329 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_LUN);
25330 		}
25331 		if (reset_retval == 0) {
25332 			/* The LUN reset either failed or was not issued */
25333 			reset_retval = scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25334 		}
25335 		if ((reset_retval == 0) &&
25336 		    (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0)) {
25337 			rval = EIO;
25338 			kmem_free(com, sizeof (*com));
25339 			return (rval);
25340 		}
25341 
25342 		bzero(com, sizeof (struct uscsi_cmd));
25343 		com->uscsi_flags   = USCSI_SILENT;
25344 		com->uscsi_cdb	   = cdb;
25345 		com->uscsi_cdblen  = CDB_GROUP0;
25346 		com->uscsi_timeout = 5;
25347 
25348 		/*
25349 		 * Reissue the last reserve command, this time without request
25350 		 * sense.  Assume that it is just a regular reserve command.
25351 		 */
25352 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
25353 		    UIO_SYSSPACE, SD_PATH_STANDARD);
25354 	}
25355 
25356 	/* Return an error if still getting a reservation conflict. */
25357 	if ((rval != 0) && (com->uscsi_status == STATUS_RESERVATION_CONFLICT)) {
25358 		rval = EACCES;
25359 	}
25360 
25361 	kmem_free(com, sizeof (*com));
25362 	return (rval);
25363 }
25364 
25365 
25366 #define	SD_NDUMP_RETRIES	12
25367 /*
25368  *	System Crash Dump routine
25369  */
25370 
25371 static int
25372 sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk)
25373 {
25374 	int		instance;
25375 	int		partition;
25376 	int		i;
25377 	int		err;
25378 	struct sd_lun	*un;
25379 	struct dk_map	*lp;
25380 	struct scsi_pkt *wr_pktp;
25381 	struct buf	*wr_bp;
25382 	struct buf	wr_buf;
25383 	daddr_t		tgt_byte_offset; /* rmw - byte offset for target */
25384 	daddr_t		tgt_blkno;	/* rmw - blkno for target */
25385 	size_t		tgt_byte_count; /* rmw -  # of bytes to xfer */
25386 	size_t		tgt_nblk; /* rmw -  # of tgt blks to xfer */
25387 	size_t		io_start_offset;
25388 	int		doing_rmw = FALSE;
25389 	int		rval;
25390 #if defined(__i386) || defined(__amd64)
25391 	ssize_t dma_resid;
25392 	daddr_t oblkno;
25393 #endif
25394 
25395 	instance = SDUNIT(dev);
25396 	if (((un = ddi_get_soft_state(sd_state, instance)) == NULL) ||
25397 	    (!un->un_f_geometry_is_valid) || ISCD(un)) {
25398 		return (ENXIO);
25399 	}
25400 
25401 	_NOTE(NOW_INVISIBLE_TO_OTHER_THREADS(*un))
25402 
25403 	SD_TRACE(SD_LOG_DUMP, un, "sddump: entry\n");
25404 
25405 	partition = SDPART(dev);
25406 	SD_INFO(SD_LOG_DUMP, un, "sddump: partition = %d\n", partition);
25407 
25408 	/* Validate blocks to dump at against partition size. */
25409 	lp = &un->un_map[partition];
25410 	if ((blkno + nblk) > lp->dkl_nblk) {
25411 		SD_TRACE(SD_LOG_DUMP, un,
25412 		    "sddump: dump range larger than partition: "
25413 		    "blkno = 0x%x, nblk = 0x%x, dkl_nblk = 0x%x\n",
25414 		    blkno, nblk, lp->dkl_nblk);
25415 		return (EINVAL);
25416 	}
25417 
25418 	mutex_enter(&un->un_pm_mutex);
25419 	if (SD_DEVICE_IS_IN_LOW_POWER(un)) {
25420 		struct scsi_pkt *start_pktp;
25421 
25422 		mutex_exit(&un->un_pm_mutex);
25423 
25424 		/*
25425 		 * use pm framework to power on HBA 1st
25426 		 */
25427 		(void) pm_raise_power(SD_DEVINFO(un), 0, SD_SPINDLE_ON);
25428 
25429 		/*
25430 		 * Dump no long uses sdpower to power on a device, it's
25431 		 * in-line here so it can be done in polled mode.
25432 		 */
25433 
25434 		SD_INFO(SD_LOG_DUMP, un, "sddump: starting device\n");
25435 
25436 		start_pktp = scsi_init_pkt(SD_ADDRESS(un), NULL, NULL,
25437 		    CDB_GROUP0, un->un_status_len, 0, 0, NULL_FUNC, NULL);
25438 
25439 		if (start_pktp == NULL) {
25440 			/* We were not given a SCSI packet, fail. */
25441 			return (EIO);
25442 		}
25443 		bzero(start_pktp->pkt_cdbp, CDB_GROUP0);
25444 		start_pktp->pkt_cdbp[0] = SCMD_START_STOP;
25445 		start_pktp->pkt_cdbp[4] = SD_TARGET_START;
25446 		start_pktp->pkt_flags = FLAG_NOINTR;
25447 
25448 		mutex_enter(SD_MUTEX(un));
25449 		sd_fill_scsi1_lun(un, start_pktp);
25450 		mutex_exit(SD_MUTEX(un));
25451 		/*
25452 		 * Scsi_poll returns 0 (success) if the command completes and
25453 		 * the status block is STATUS_GOOD.
25454 		 */
25455 		if (sd_scsi_poll(un, start_pktp) != 0) {
25456 			scsi_destroy_pkt(start_pktp);
25457 			return (EIO);
25458 		}
25459 		scsi_destroy_pkt(start_pktp);
25460 		(void) sd_ddi_pm_resume(un);
25461 	} else {
25462 		mutex_exit(&un->un_pm_mutex);
25463 	}
25464 
25465 	mutex_enter(SD_MUTEX(un));
25466 	un->un_throttle = 0;
25467 
25468 	/*
25469 	 * The first time through, reset the specific target device.
25470 	 * However, when cpr calls sddump we know that sd is in a
25471 	 * a good state so no bus reset is required.
25472 	 * Clear sense data via Request Sense cmd.
25473 	 * In sddump we don't care about allow_bus_device_reset anymore
25474 	 */
25475 
25476 	if ((un->un_state != SD_STATE_SUSPENDED) &&
25477 	    (un->un_state != SD_STATE_DUMPING)) {
25478 
25479 		New_state(un, SD_STATE_DUMPING);
25480 
25481 		if (un->un_f_is_fibre == FALSE) {
25482 			mutex_exit(SD_MUTEX(un));
25483 			/*
25484 			 * Attempt a bus reset for parallel scsi.
25485 			 *
25486 			 * Note: A bus reset is required because on some host
25487 			 * systems (i.e. E420R) a bus device reset is
25488 			 * insufficient to reset the state of the target.
25489 			 *
25490 			 * Note: Don't issue the reset for fibre-channel,
25491 			 * because this tends to hang the bus (loop) for
25492 			 * too long while everyone is logging out and in
25493 			 * and the deadman timer for dumping will fire
25494 			 * before the dump is complete.
25495 			 */
25496 			if (scsi_reset(SD_ADDRESS(un), RESET_ALL) == 0) {
25497 				mutex_enter(SD_MUTEX(un));
25498 				Restore_state(un);
25499 				mutex_exit(SD_MUTEX(un));
25500 				return (EIO);
25501 			}
25502 
25503 			/* Delay to give the device some recovery time. */
25504 			drv_usecwait(10000);
25505 
25506 			if (sd_send_polled_RQS(un) == SD_FAILURE) {
25507 				SD_INFO(SD_LOG_DUMP, un,
25508 					"sddump: sd_send_polled_RQS failed\n");
25509 			}
25510 			mutex_enter(SD_MUTEX(un));
25511 		}
25512 	}
25513 
25514 	/*
25515 	 * Convert the partition-relative block number to a
25516 	 * disk physical block number.
25517 	 */
25518 	blkno += un->un_offset[partition];
25519 	SD_INFO(SD_LOG_DUMP, un, "sddump: disk blkno = 0x%x\n", blkno);
25520 
25521 
25522 	/*
25523 	 * Check if the device has a non-512 block size.
25524 	 */
25525 	wr_bp = NULL;
25526 	if (NOT_DEVBSIZE(un)) {
25527 		tgt_byte_offset = blkno * un->un_sys_blocksize;
25528 		tgt_byte_count = nblk * un->un_sys_blocksize;
25529 		if ((tgt_byte_offset % un->un_tgt_blocksize) ||
25530 		    (tgt_byte_count % un->un_tgt_blocksize)) {
25531 			doing_rmw = TRUE;
25532 			/*
25533 			 * Calculate the block number and number of block
25534 			 * in terms of the media block size.
25535 			 */
25536 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25537 			tgt_nblk =
25538 			    ((tgt_byte_offset + tgt_byte_count +
25539 				(un->un_tgt_blocksize - 1)) /
25540 				un->un_tgt_blocksize) - tgt_blkno;
25541 
25542 			/*
25543 			 * Invoke the routine which is going to do read part
25544 			 * of read-modify-write.
25545 			 * Note that this routine returns a pointer to
25546 			 * a valid bp in wr_bp.
25547 			 */
25548 			err = sddump_do_read_of_rmw(un, tgt_blkno, tgt_nblk,
25549 			    &wr_bp);
25550 			if (err) {
25551 				mutex_exit(SD_MUTEX(un));
25552 				return (err);
25553 			}
25554 			/*
25555 			 * Offset is being calculated as -
25556 			 * (original block # * system block size) -
25557 			 * (new block # * target block size)
25558 			 */
25559 			io_start_offset =
25560 			    ((uint64_t)(blkno * un->un_sys_blocksize)) -
25561 			    ((uint64_t)(tgt_blkno * un->un_tgt_blocksize));
25562 
25563 			ASSERT((io_start_offset >= 0) &&
25564 			    (io_start_offset < un->un_tgt_blocksize));
25565 			/*
25566 			 * Do the modify portion of read modify write.
25567 			 */
25568 			bcopy(addr, &wr_bp->b_un.b_addr[io_start_offset],
25569 			    (size_t)nblk * un->un_sys_blocksize);
25570 		} else {
25571 			doing_rmw = FALSE;
25572 			tgt_blkno = tgt_byte_offset / un->un_tgt_blocksize;
25573 			tgt_nblk = tgt_byte_count / un->un_tgt_blocksize;
25574 		}
25575 
25576 		/* Convert blkno and nblk to target blocks */
25577 		blkno = tgt_blkno;
25578 		nblk = tgt_nblk;
25579 	} else {
25580 		wr_bp = &wr_buf;
25581 		bzero(wr_bp, sizeof (struct buf));
25582 		wr_bp->b_flags		= B_BUSY;
25583 		wr_bp->b_un.b_addr	= addr;
25584 		wr_bp->b_bcount		= nblk << DEV_BSHIFT;
25585 		wr_bp->b_resid		= 0;
25586 	}
25587 
25588 	mutex_exit(SD_MUTEX(un));
25589 
25590 	/*
25591 	 * Obtain a SCSI packet for the write command.
25592 	 * It should be safe to call the allocator here without
25593 	 * worrying about being locked for DVMA mapping because
25594 	 * the address we're passed is already a DVMA mapping
25595 	 *
25596 	 * We are also not going to worry about semaphore ownership
25597 	 * in the dump buffer. Dumping is single threaded at present.
25598 	 */
25599 
25600 	wr_pktp = NULL;
25601 
25602 #if defined(__i386) || defined(__amd64)
25603 	dma_resid = wr_bp->b_bcount;
25604 	oblkno = blkno;
25605 	while (dma_resid != 0) {
25606 #endif
25607 
25608 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25609 		wr_bp->b_flags &= ~B_ERROR;
25610 
25611 #if defined(__i386) || defined(__amd64)
25612 		blkno = oblkno +
25613 			((wr_bp->b_bcount - dma_resid) /
25614 			    un->un_tgt_blocksize);
25615 		nblk = dma_resid / un->un_tgt_blocksize;
25616 
25617 		if (wr_pktp) {
25618 			/* Partial DMA transfers after initial transfer */
25619 			rval = sd_setup_next_rw_pkt(un, wr_pktp, wr_bp,
25620 			    blkno, nblk);
25621 		} else {
25622 			/* Initial transfer */
25623 			rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
25624 			    un->un_pkt_flags, NULL_FUNC, NULL,
25625 			    blkno, nblk);
25626 		}
25627 #else
25628 		rval = sd_setup_rw_pkt(un, &wr_pktp, wr_bp,
25629 		    0, NULL_FUNC, NULL, blkno, nblk);
25630 #endif
25631 
25632 		if (rval == 0) {
25633 			/* We were given a SCSI packet, continue. */
25634 			break;
25635 		}
25636 
25637 		if (i == 0) {
25638 			if (wr_bp->b_flags & B_ERROR) {
25639 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25640 				    "no resources for dumping; "
25641 				    "error code: 0x%x, retrying",
25642 				    geterror(wr_bp));
25643 			} else {
25644 				scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
25645 				    "no resources for dumping; retrying");
25646 			}
25647 		} else if (i != (SD_NDUMP_RETRIES - 1)) {
25648 			if (wr_bp->b_flags & B_ERROR) {
25649 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25650 				    "no resources for dumping; error code: "
25651 				    "0x%x, retrying\n", geterror(wr_bp));
25652 			}
25653 		} else {
25654 			if (wr_bp->b_flags & B_ERROR) {
25655 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25656 				    "no resources for dumping; "
25657 				    "error code: 0x%x, retries failed, "
25658 				    "giving up.\n", geterror(wr_bp));
25659 			} else {
25660 				scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25661 				    "no resources for dumping; "
25662 				    "retries failed, giving up.\n");
25663 			}
25664 			mutex_enter(SD_MUTEX(un));
25665 			Restore_state(un);
25666 			if (NOT_DEVBSIZE(un) && (doing_rmw == TRUE)) {
25667 				mutex_exit(SD_MUTEX(un));
25668 				scsi_free_consistent_buf(wr_bp);
25669 			} else {
25670 				mutex_exit(SD_MUTEX(un));
25671 			}
25672 			return (EIO);
25673 		}
25674 		drv_usecwait(10000);
25675 	}
25676 
25677 #if defined(__i386) || defined(__amd64)
25678 	/*
25679 	 * save the resid from PARTIAL_DMA
25680 	 */
25681 	dma_resid = wr_pktp->pkt_resid;
25682 	if (dma_resid != 0)
25683 		nblk -= SD_BYTES2TGTBLOCKS(un, dma_resid);
25684 	wr_pktp->pkt_resid = 0;
25685 #endif
25686 
25687 	/* SunBug 1222170 */
25688 	wr_pktp->pkt_flags = FLAG_NOINTR;
25689 
25690 	err = EIO;
25691 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
25692 
25693 		/*
25694 		 * Scsi_poll returns 0 (success) if the command completes and
25695 		 * the status block is STATUS_GOOD.  We should only check
25696 		 * errors if this condition is not true.  Even then we should
25697 		 * send our own request sense packet only if we have a check
25698 		 * condition and auto request sense has not been performed by
25699 		 * the hba.
25700 		 */
25701 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending write\n");
25702 
25703 		if ((sd_scsi_poll(un, wr_pktp) == 0) &&
25704 		    (wr_pktp->pkt_resid == 0)) {
25705 			err = SD_SUCCESS;
25706 			break;
25707 		}
25708 
25709 		/*
25710 		 * Check CMD_DEV_GONE 1st, give up if device is gone.
25711 		 */
25712 		if (wr_pktp->pkt_reason == CMD_DEV_GONE) {
25713 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
25714 			    "Device is gone\n");
25715 			break;
25716 		}
25717 
25718 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_CHECK) {
25719 			SD_INFO(SD_LOG_DUMP, un,
25720 			    "sddump: write failed with CHECK, try # %d\n", i);
25721 			if (((wr_pktp->pkt_state & STATE_ARQ_DONE) == 0)) {
25722 				(void) sd_send_polled_RQS(un);
25723 			}
25724 
25725 			continue;
25726 		}
25727 
25728 		if (SD_GET_PKT_STATUS(wr_pktp) == STATUS_BUSY) {
25729 			int reset_retval = 0;
25730 
25731 			SD_INFO(SD_LOG_DUMP, un,
25732 			    "sddump: write failed with BUSY, try # %d\n", i);
25733 
25734 			if (un->un_f_lun_reset_enabled == TRUE) {
25735 				reset_retval = scsi_reset(SD_ADDRESS(un),
25736 				    RESET_LUN);
25737 			}
25738 			if (reset_retval == 0) {
25739 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
25740 			}
25741 			(void) sd_send_polled_RQS(un);
25742 
25743 		} else {
25744 			SD_INFO(SD_LOG_DUMP, un,
25745 			    "sddump: write failed with 0x%x, try # %d\n",
25746 			    SD_GET_PKT_STATUS(wr_pktp), i);
25747 			mutex_enter(SD_MUTEX(un));
25748 			sd_reset_target(un, wr_pktp);
25749 			mutex_exit(SD_MUTEX(un));
25750 		}
25751 
25752 		/*
25753 		 * If we are not getting anywhere with lun/target resets,
25754 		 * let's reset the bus.
25755 		 */
25756 		if (i == SD_NDUMP_RETRIES/2) {
25757 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
25758 			(void) sd_send_polled_RQS(un);
25759 		}
25760 
25761 	}
25762 #if defined(__i386) || defined(__amd64)
25763 	}	/* dma_resid */
25764 #endif
25765 
25766 	scsi_destroy_pkt(wr_pktp);
25767 	mutex_enter(SD_MUTEX(un));
25768 	if ((NOT_DEVBSIZE(un)) && (doing_rmw == TRUE)) {
25769 		mutex_exit(SD_MUTEX(un));
25770 		scsi_free_consistent_buf(wr_bp);
25771 	} else {
25772 		mutex_exit(SD_MUTEX(un));
25773 	}
25774 	SD_TRACE(SD_LOG_DUMP, un, "sddump: exit: err = %d\n", err);
25775 	return (err);
25776 }
25777 
25778 /*
25779  *    Function: sd_scsi_poll()
25780  *
25781  * Description: This is a wrapper for the scsi_poll call.
25782  *
25783  *   Arguments: sd_lun - The unit structure
25784  *              scsi_pkt - The scsi packet being sent to the device.
25785  *
25786  * Return Code: 0 - Command completed successfully with good status
25787  *             -1 - Command failed.  This could indicate a check condition
25788  *                  or other status value requiring recovery action.
25789  *
25790  */
25791 
25792 static int
25793 sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pktp)
25794 {
25795 	int status;
25796 
25797 	ASSERT(un != NULL);
25798 	ASSERT(!mutex_owned(SD_MUTEX(un)));
25799 	ASSERT(pktp != NULL);
25800 
25801 	status = SD_SUCCESS;
25802 
25803 	if (scsi_ifgetcap(&pktp->pkt_address, "tagged-qing", 1) == 1) {
25804 		pktp->pkt_flags |= un->un_tagflags;
25805 		pktp->pkt_flags &= ~FLAG_NODISCON;
25806 	}
25807 
25808 	status = sd_ddi_scsi_poll(pktp);
25809 	/*
25810 	 * Scsi_poll returns 0 (success) if the command completes and the
25811 	 * status block is STATUS_GOOD.  We should only check errors if this
25812 	 * condition is not true.  Even then we should send our own request
25813 	 * sense packet only if we have a check condition and auto
25814 	 * request sense has not been performed by the hba.
25815 	 * Don't get RQS data if pkt_reason is CMD_DEV_GONE.
25816 	 */
25817 	if ((status != SD_SUCCESS) &&
25818 	    (SD_GET_PKT_STATUS(pktp) == STATUS_CHECK) &&
25819 	    (pktp->pkt_state & STATE_ARQ_DONE) == 0 &&
25820 	    (pktp->pkt_reason != CMD_DEV_GONE))
25821 		(void) sd_send_polled_RQS(un);
25822 
25823 	return (status);
25824 }
25825 
25826 /*
25827  *    Function: sd_send_polled_RQS()
25828  *
25829  * Description: This sends the request sense command to a device.
25830  *
25831  *   Arguments: sd_lun - The unit structure
25832  *
25833  * Return Code: 0 - Command completed successfully with good status
25834  *             -1 - Command failed.
25835  *
25836  */
25837 
25838 static int
25839 sd_send_polled_RQS(struct sd_lun *un)
25840 {
25841 	int	ret_val;
25842 	struct	scsi_pkt	*rqs_pktp;
25843 	struct	buf		*rqs_bp;
25844 
25845 	ASSERT(un != NULL);
25846 	ASSERT(!mutex_owned(SD_MUTEX(un)));
25847 
25848 	ret_val = SD_SUCCESS;
25849 
25850 	rqs_pktp = un->un_rqs_pktp;
25851 	rqs_bp	 = un->un_rqs_bp;
25852 
25853 	mutex_enter(SD_MUTEX(un));
25854 
25855 	if (un->un_sense_isbusy) {
25856 		ret_val = SD_FAILURE;
25857 		mutex_exit(SD_MUTEX(un));
25858 		return (ret_val);
25859 	}
25860 
25861 	/*
25862 	 * If the request sense buffer (and packet) is not in use,
25863 	 * let's set the un_sense_isbusy and send our packet
25864 	 */
25865 	un->un_sense_isbusy 	= 1;
25866 	rqs_pktp->pkt_resid  	= 0;
25867 	rqs_pktp->pkt_reason 	= 0;
25868 	rqs_pktp->pkt_flags |= FLAG_NOINTR;
25869 	bzero(rqs_bp->b_un.b_addr, SENSE_LENGTH);
25870 
25871 	mutex_exit(SD_MUTEX(un));
25872 
25873 	SD_INFO(SD_LOG_COMMON, un, "sd_send_polled_RQS: req sense buf at"
25874 	    " 0x%p\n", rqs_bp->b_un.b_addr);
25875 
25876 	/*
25877 	 * Can't send this to sd_scsi_poll, we wrap ourselves around the
25878 	 * axle - it has a call into us!
25879 	 */
25880 	if ((ret_val = sd_ddi_scsi_poll(rqs_pktp)) != 0) {
25881 		SD_INFO(SD_LOG_COMMON, un,
25882 		    "sd_send_polled_RQS: RQS failed\n");
25883 	}
25884 
25885 	SD_DUMP_MEMORY(un, SD_LOG_COMMON, "sd_send_polled_RQS:",
25886 	    (uchar_t *)rqs_bp->b_un.b_addr, SENSE_LENGTH, SD_LOG_HEX);
25887 
25888 	mutex_enter(SD_MUTEX(un));
25889 	un->un_sense_isbusy = 0;
25890 	mutex_exit(SD_MUTEX(un));
25891 
25892 	return (ret_val);
25893 }
25894 
25895 /*
25896  * Defines needed for localized version of the scsi_poll routine.
25897  */
25898 #define	SD_CSEC		10000			/* usecs */
25899 #define	SD_SEC_TO_CSEC	(1000000/SD_CSEC)
25900 
25901 
25902 /*
25903  *    Function: sd_ddi_scsi_poll()
25904  *
25905  * Description: Localized version of the scsi_poll routine.  The purpose is to
25906  *		send a scsi_pkt to a device as a polled command.  This version
25907  *		is to ensure more robust handling of transport errors.
25908  *		Specifically this routine cures not ready, coming ready
25909  *		transition for power up and reset of sonoma's.  This can take
25910  *		up to 45 seconds for power-on and 20 seconds for reset of a
25911  * 		sonoma lun.
25912  *
25913  *   Arguments: scsi_pkt - The scsi_pkt being sent to a device
25914  *
25915  * Return Code: 0 - Command completed successfully with good status
25916  *             -1 - Command failed.
25917  *
25918  */
25919 
25920 static int
25921 sd_ddi_scsi_poll(struct scsi_pkt *pkt)
25922 {
25923 	int busy_count;
25924 	int timeout;
25925 	int rval = SD_FAILURE;
25926 	int savef;
25927 	struct scsi_extended_sense *sensep;
25928 	long savet;
25929 	void (*savec)();
25930 	/*
25931 	 * The following is defined in machdep.c and is used in determining if
25932 	 * the scsi transport system will do polled I/O instead of interrupt
25933 	 * I/O when called from xx_dump().
25934 	 */
25935 	extern int do_polled_io;
25936 
25937 	/*
25938 	 * save old flags in pkt, to restore at end
25939 	 */
25940 	savef = pkt->pkt_flags;
25941 	savec = pkt->pkt_comp;
25942 	savet = pkt->pkt_time;
25943 
25944 	pkt->pkt_flags |= FLAG_NOINTR;
25945 
25946 	/*
25947 	 * XXX there is nothing in the SCSA spec that states that we should not
25948 	 * do a callback for polled cmds; however, removing this will break sd
25949 	 * and probably other target drivers
25950 	 */
25951 	pkt->pkt_comp = NULL;
25952 
25953 	/*
25954 	 * we don't like a polled command without timeout.
25955 	 * 60 seconds seems long enough.
25956 	 */
25957 	if (pkt->pkt_time == 0) {
25958 		pkt->pkt_time = SCSI_POLL_TIMEOUT;
25959 	}
25960 
25961 	/*
25962 	 * Send polled cmd.
25963 	 *
25964 	 * We do some error recovery for various errors.  Tran_busy,
25965 	 * queue full, and non-dispatched commands are retried every 10 msec.
25966 	 * as they are typically transient failures.  Busy status and Not
25967 	 * Ready are retried every second as this status takes a while to
25968 	 * change.  Unit attention is retried for pkt_time (60) times
25969 	 * with no delay.
25970 	 */
25971 	timeout = pkt->pkt_time * SD_SEC_TO_CSEC;
25972 
25973 	for (busy_count = 0; busy_count < timeout; busy_count++) {
25974 		int rc;
25975 		int poll_delay;
25976 
25977 		/*
25978 		 * Initialize pkt status variables.
25979 		 */
25980 		*pkt->pkt_scbp = pkt->pkt_reason = pkt->pkt_state = 0;
25981 
25982 		if ((rc = scsi_transport(pkt)) != TRAN_ACCEPT) {
25983 			if (rc != TRAN_BUSY) {
25984 				/* Transport failed - give up. */
25985 				break;
25986 			} else {
25987 				/* Transport busy - try again. */
25988 				poll_delay = 1 * SD_CSEC; /* 10 msec */
25989 			}
25990 		} else {
25991 			/*
25992 			 * Transport accepted - check pkt status.
25993 			 */
25994 			rc = (*pkt->pkt_scbp) & STATUS_MASK;
25995 			if (pkt->pkt_reason == CMD_CMPLT &&
25996 			    rc == STATUS_CHECK &&
25997 			    pkt->pkt_state & STATE_ARQ_DONE) {
25998 				struct scsi_arq_status *arqstat =
25999 				    (struct scsi_arq_status *)(pkt->pkt_scbp);
26000 
26001 				sensep = &arqstat->sts_sensedata;
26002 			} else {
26003 				sensep = NULL;
26004 			}
26005 
26006 			if ((pkt->pkt_reason == CMD_CMPLT) &&
26007 			    (rc == STATUS_GOOD)) {
26008 				/* No error - we're done */
26009 				rval = SD_SUCCESS;
26010 				break;
26011 
26012 			} else if (pkt->pkt_reason == CMD_DEV_GONE) {
26013 				/* Lost connection - give up */
26014 				break;
26015 
26016 			} else if ((pkt->pkt_reason == CMD_INCOMPLETE) &&
26017 			    (pkt->pkt_state == 0)) {
26018 				/* Pkt not dispatched - try again. */
26019 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
26020 
26021 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26022 			    (rc == STATUS_QFULL)) {
26023 				/* Queue full - try again. */
26024 				poll_delay = 1 * SD_CSEC; /* 10 msec. */
26025 
26026 			} else if ((pkt->pkt_reason == CMD_CMPLT) &&
26027 			    (rc == STATUS_BUSY)) {
26028 				/* Busy - try again. */
26029 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
26030 				busy_count += (SD_SEC_TO_CSEC - 1);
26031 
26032 			} else if ((sensep != NULL) &&
26033 			    (sensep->es_key == KEY_UNIT_ATTENTION)) {
26034 				/* Unit Attention - try again */
26035 				busy_count += (SD_SEC_TO_CSEC - 1); /* 1 */
26036 				continue;
26037 
26038 			} else if ((sensep != NULL) &&
26039 			    (sensep->es_key == KEY_NOT_READY) &&
26040 			    (sensep->es_add_code == 0x04) &&
26041 			    (sensep->es_qual_code == 0x01)) {
26042 				/* Not ready -> ready - try again. */
26043 				poll_delay = 100 * SD_CSEC; /* 1 sec. */
26044 				busy_count += (SD_SEC_TO_CSEC - 1);
26045 
26046 			} else {
26047 				/* BAD status - give up. */
26048 				break;
26049 			}
26050 		}
26051 
26052 		if ((curthread->t_flag & T_INTR_THREAD) == 0 &&
26053 		    !do_polled_io) {
26054 			delay(drv_usectohz(poll_delay));
26055 		} else {
26056 			/* we busy wait during cpr_dump or interrupt threads */
26057 			drv_usecwait(poll_delay);
26058 		}
26059 	}
26060 
26061 	pkt->pkt_flags = savef;
26062 	pkt->pkt_comp = savec;
26063 	pkt->pkt_time = savet;
26064 	return (rval);
26065 }
26066 
26067 
26068 /*
26069  *    Function: sd_persistent_reservation_in_read_keys
26070  *
26071  * Description: This routine is the driver entry point for handling CD-ROM
26072  *		multi-host persistent reservation requests (MHIOCGRP_INKEYS)
26073  *		by sending the SCSI-3 PRIN commands to the device.
26074  *		Processes the read keys command response by copying the
26075  *		reservation key information into the user provided buffer.
26076  *		Support for the 32/64 bit _MULTI_DATAMODEL is implemented.
26077  *
26078  *   Arguments: un   -  Pointer to soft state struct for the target.
26079  *		usrp -	user provided pointer to multihost Persistent In Read
26080  *			Keys structure (mhioc_inkeys_t)
26081  *		flag -	this argument is a pass through to ddi_copyxxx()
26082  *			directly from the mode argument of ioctl().
26083  *
26084  * Return Code: 0   - Success
26085  *		EACCES
26086  *		ENOTSUP
26087  *		errno return code from sd_send_scsi_cmd()
26088  *
26089  *     Context: Can sleep. Does not return until command is completed.
26090  */
26091 
26092 static int
26093 sd_persistent_reservation_in_read_keys(struct sd_lun *un,
26094     mhioc_inkeys_t *usrp, int flag)
26095 {
26096 #ifdef _MULTI_DATAMODEL
26097 	struct mhioc_key_list32	li32;
26098 #endif
26099 	sd_prin_readkeys_t	*in;
26100 	mhioc_inkeys_t		*ptr;
26101 	mhioc_key_list_t	li;
26102 	uchar_t			*data_bufp;
26103 	int 			data_len;
26104 	int			rval;
26105 	size_t			copysz;
26106 
26107 	if ((ptr = (mhioc_inkeys_t *)usrp) == NULL) {
26108 		return (EINVAL);
26109 	}
26110 	bzero(&li, sizeof (mhioc_key_list_t));
26111 
26112 	/*
26113 	 * Get the listsize from user
26114 	 */
26115 #ifdef _MULTI_DATAMODEL
26116 
26117 	switch (ddi_model_convert_from(flag & FMODELS)) {
26118 	case DDI_MODEL_ILP32:
26119 		copysz = sizeof (struct mhioc_key_list32);
26120 		if (ddi_copyin(ptr->li, &li32, copysz, flag)) {
26121 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26122 			    "sd_persistent_reservation_in_read_keys: "
26123 			    "failed ddi_copyin: mhioc_key_list32_t\n");
26124 			rval = EFAULT;
26125 			goto done;
26126 		}
26127 		li.listsize = li32.listsize;
26128 		li.list = (mhioc_resv_key_t *)(uintptr_t)li32.list;
26129 		break;
26130 
26131 	case DDI_MODEL_NONE:
26132 		copysz = sizeof (mhioc_key_list_t);
26133 		if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26134 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26135 			    "sd_persistent_reservation_in_read_keys: "
26136 			    "failed ddi_copyin: mhioc_key_list_t\n");
26137 			rval = EFAULT;
26138 			goto done;
26139 		}
26140 		break;
26141 	}
26142 
26143 #else /* ! _MULTI_DATAMODEL */
26144 	copysz = sizeof (mhioc_key_list_t);
26145 	if (ddi_copyin(ptr->li, &li, copysz, flag)) {
26146 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26147 		    "sd_persistent_reservation_in_read_keys: "
26148 		    "failed ddi_copyin: mhioc_key_list_t\n");
26149 		rval = EFAULT;
26150 		goto done;
26151 	}
26152 #endif
26153 
26154 	data_len  = li.listsize * MHIOC_RESV_KEY_SIZE;
26155 	data_len += (sizeof (sd_prin_readkeys_t) - sizeof (caddr_t));
26156 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26157 
26158 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_KEYS,
26159 	    data_len, data_bufp)) != 0) {
26160 		goto done;
26161 	}
26162 	in = (sd_prin_readkeys_t *)data_bufp;
26163 	ptr->generation = BE_32(in->generation);
26164 	li.listlen = BE_32(in->len) / MHIOC_RESV_KEY_SIZE;
26165 
26166 	/*
26167 	 * Return the min(listsize, listlen) keys
26168 	 */
26169 #ifdef _MULTI_DATAMODEL
26170 
26171 	switch (ddi_model_convert_from(flag & FMODELS)) {
26172 	case DDI_MODEL_ILP32:
26173 		li32.listlen = li.listlen;
26174 		if (ddi_copyout(&li32, ptr->li, copysz, flag)) {
26175 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26176 			    "sd_persistent_reservation_in_read_keys: "
26177 			    "failed ddi_copyout: mhioc_key_list32_t\n");
26178 			rval = EFAULT;
26179 			goto done;
26180 		}
26181 		break;
26182 
26183 	case DDI_MODEL_NONE:
26184 		if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26185 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26186 			    "sd_persistent_reservation_in_read_keys: "
26187 			    "failed ddi_copyout: mhioc_key_list_t\n");
26188 			rval = EFAULT;
26189 			goto done;
26190 		}
26191 		break;
26192 	}
26193 
26194 #else /* ! _MULTI_DATAMODEL */
26195 
26196 	if (ddi_copyout(&li, ptr->li, copysz, flag)) {
26197 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26198 		    "sd_persistent_reservation_in_read_keys: "
26199 		    "failed ddi_copyout: mhioc_key_list_t\n");
26200 		rval = EFAULT;
26201 		goto done;
26202 	}
26203 
26204 #endif /* _MULTI_DATAMODEL */
26205 
26206 	copysz = min(li.listlen * MHIOC_RESV_KEY_SIZE,
26207 	    li.listsize * MHIOC_RESV_KEY_SIZE);
26208 	if (ddi_copyout(&in->keylist, li.list, copysz, flag)) {
26209 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26210 		    "sd_persistent_reservation_in_read_keys: "
26211 		    "failed ddi_copyout: keylist\n");
26212 		rval = EFAULT;
26213 	}
26214 done:
26215 	kmem_free(data_bufp, data_len);
26216 	return (rval);
26217 }
26218 
26219 
26220 /*
26221  *    Function: sd_persistent_reservation_in_read_resv
26222  *
26223  * Description: This routine is the driver entry point for handling CD-ROM
26224  *		multi-host persistent reservation requests (MHIOCGRP_INRESV)
26225  *		by sending the SCSI-3 PRIN commands to the device.
26226  *		Process the read persistent reservations command response by
26227  *		copying the reservation information into the user provided
26228  *		buffer. Support for the 32/64 _MULTI_DATAMODEL is implemented.
26229  *
26230  *   Arguments: un   -  Pointer to soft state struct for the target.
26231  *		usrp -	user provided pointer to multihost Persistent In Read
26232  *			Keys structure (mhioc_inkeys_t)
26233  *		flag -	this argument is a pass through to ddi_copyxxx()
26234  *			directly from the mode argument of ioctl().
26235  *
26236  * Return Code: 0   - Success
26237  *		EACCES
26238  *		ENOTSUP
26239  *		errno return code from sd_send_scsi_cmd()
26240  *
26241  *     Context: Can sleep. Does not return until command is completed.
26242  */
26243 
26244 static int
26245 sd_persistent_reservation_in_read_resv(struct sd_lun *un,
26246     mhioc_inresvs_t *usrp, int flag)
26247 {
26248 #ifdef _MULTI_DATAMODEL
26249 	struct mhioc_resv_desc_list32 resvlist32;
26250 #endif
26251 	sd_prin_readresv_t	*in;
26252 	mhioc_inresvs_t		*ptr;
26253 	sd_readresv_desc_t	*readresv_ptr;
26254 	mhioc_resv_desc_list_t	resvlist;
26255 	mhioc_resv_desc_t 	resvdesc;
26256 	uchar_t			*data_bufp;
26257 	int 			data_len;
26258 	int			rval;
26259 	int			i;
26260 	size_t			copysz;
26261 	mhioc_resv_desc_t	*bufp;
26262 
26263 	if ((ptr = usrp) == NULL) {
26264 		return (EINVAL);
26265 	}
26266 
26267 	/*
26268 	 * Get the listsize from user
26269 	 */
26270 #ifdef _MULTI_DATAMODEL
26271 	switch (ddi_model_convert_from(flag & FMODELS)) {
26272 	case DDI_MODEL_ILP32:
26273 		copysz = sizeof (struct mhioc_resv_desc_list32);
26274 		if (ddi_copyin(ptr->li, &resvlist32, copysz, flag)) {
26275 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26276 			    "sd_persistent_reservation_in_read_resv: "
26277 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26278 			rval = EFAULT;
26279 			goto done;
26280 		}
26281 		resvlist.listsize = resvlist32.listsize;
26282 		resvlist.list = (mhioc_resv_desc_t *)(uintptr_t)resvlist32.list;
26283 		break;
26284 
26285 	case DDI_MODEL_NONE:
26286 		copysz = sizeof (mhioc_resv_desc_list_t);
26287 		if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26288 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26289 			    "sd_persistent_reservation_in_read_resv: "
26290 			    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26291 			rval = EFAULT;
26292 			goto done;
26293 		}
26294 		break;
26295 	}
26296 #else /* ! _MULTI_DATAMODEL */
26297 	copysz = sizeof (mhioc_resv_desc_list_t);
26298 	if (ddi_copyin(ptr->li, &resvlist, copysz, flag)) {
26299 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26300 		    "sd_persistent_reservation_in_read_resv: "
26301 		    "failed ddi_copyin: mhioc_resv_desc_list_t\n");
26302 		rval = EFAULT;
26303 		goto done;
26304 	}
26305 #endif /* ! _MULTI_DATAMODEL */
26306 
26307 	data_len  = resvlist.listsize * SCSI3_RESV_DESC_LEN;
26308 	data_len += (sizeof (sd_prin_readresv_t) - sizeof (caddr_t));
26309 	data_bufp = kmem_zalloc(data_len, KM_SLEEP);
26310 
26311 	if ((rval = sd_send_scsi_PERSISTENT_RESERVE_IN(un, SD_READ_RESV,
26312 	    data_len, data_bufp)) != 0) {
26313 		goto done;
26314 	}
26315 	in = (sd_prin_readresv_t *)data_bufp;
26316 	ptr->generation = BE_32(in->generation);
26317 	resvlist.listlen = BE_32(in->len) / SCSI3_RESV_DESC_LEN;
26318 
26319 	/*
26320 	 * Return the min(listsize, listlen( keys
26321 	 */
26322 #ifdef _MULTI_DATAMODEL
26323 
26324 	switch (ddi_model_convert_from(flag & FMODELS)) {
26325 	case DDI_MODEL_ILP32:
26326 		resvlist32.listlen = resvlist.listlen;
26327 		if (ddi_copyout(&resvlist32, ptr->li, copysz, flag)) {
26328 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26329 			    "sd_persistent_reservation_in_read_resv: "
26330 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26331 			rval = EFAULT;
26332 			goto done;
26333 		}
26334 		break;
26335 
26336 	case DDI_MODEL_NONE:
26337 		if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26338 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26339 			    "sd_persistent_reservation_in_read_resv: "
26340 			    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26341 			rval = EFAULT;
26342 			goto done;
26343 		}
26344 		break;
26345 	}
26346 
26347 #else /* ! _MULTI_DATAMODEL */
26348 
26349 	if (ddi_copyout(&resvlist, ptr->li, copysz, flag)) {
26350 		SD_ERROR(SD_LOG_IOCTL_MHD, un,
26351 		    "sd_persistent_reservation_in_read_resv: "
26352 		    "failed ddi_copyout: mhioc_resv_desc_list_t\n");
26353 		rval = EFAULT;
26354 		goto done;
26355 	}
26356 
26357 #endif /* ! _MULTI_DATAMODEL */
26358 
26359 	readresv_ptr = (sd_readresv_desc_t *)&in->readresv_desc;
26360 	bufp = resvlist.list;
26361 	copysz = sizeof (mhioc_resv_desc_t);
26362 	for (i = 0; i < min(resvlist.listlen, resvlist.listsize);
26363 	    i++, readresv_ptr++, bufp++) {
26364 
26365 		bcopy(&readresv_ptr->resvkey, &resvdesc.key,
26366 		    MHIOC_RESV_KEY_SIZE);
26367 		resvdesc.type  = readresv_ptr->type;
26368 		resvdesc.scope = readresv_ptr->scope;
26369 		resvdesc.scope_specific_addr =
26370 		    BE_32(readresv_ptr->scope_specific_addr);
26371 
26372 		if (ddi_copyout(&resvdesc, bufp, copysz, flag)) {
26373 			SD_ERROR(SD_LOG_IOCTL_MHD, un,
26374 			    "sd_persistent_reservation_in_read_resv: "
26375 			    "failed ddi_copyout: resvlist\n");
26376 			rval = EFAULT;
26377 			goto done;
26378 		}
26379 	}
26380 done:
26381 	kmem_free(data_bufp, data_len);
26382 	return (rval);
26383 }
26384 
26385 
26386 /*
26387  *    Function: sr_change_blkmode()
26388  *
26389  * Description: This routine is the driver entry point for handling CD-ROM
26390  *		block mode ioctl requests. Support for returning and changing
26391  *		the current block size in use by the device is implemented. The
26392  *		LBA size is changed via a MODE SELECT Block Descriptor.
26393  *
26394  *		This routine issues a mode sense with an allocation length of
26395  *		12 bytes for the mode page header and a single block descriptor.
26396  *
26397  *   Arguments: dev - the device 'dev_t'
26398  *		cmd - the request type; one of CDROMGBLKMODE (get) or
26399  *		      CDROMSBLKMODE (set)
26400  *		data - current block size or requested block size
26401  *		flag - this argument is a pass through to ddi_copyxxx() directly
26402  *		       from the mode argument of ioctl().
26403  *
26404  * Return Code: the code returned by sd_send_scsi_cmd()
26405  *		EINVAL if invalid arguments are provided
26406  *		EFAULT if ddi_copyxxx() fails
26407  *		ENXIO if fail ddi_get_soft_state
26408  *		EIO if invalid mode sense block descriptor length
26409  *
26410  */
26411 
26412 static int
26413 sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag)
26414 {
26415 	struct sd_lun			*un = NULL;
26416 	struct mode_header		*sense_mhp, *select_mhp;
26417 	struct block_descriptor		*sense_desc, *select_desc;
26418 	int				current_bsize;
26419 	int				rval = EINVAL;
26420 	uchar_t				*sense = NULL;
26421 	uchar_t				*select = NULL;
26422 
26423 	ASSERT((cmd == CDROMGBLKMODE) || (cmd == CDROMSBLKMODE));
26424 
26425 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26426 		return (ENXIO);
26427 	}
26428 
26429 	/*
26430 	 * The block length is changed via the Mode Select block descriptor, the
26431 	 * "Read/Write Error Recovery" mode page (0x1) contents are not actually
26432 	 * required as part of this routine. Therefore the mode sense allocation
26433 	 * length is specified to be the length of a mode page header and a
26434 	 * block descriptor.
26435 	 */
26436 	sense = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26437 
26438 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
26439 	    BUFLEN_CHG_BLK_MODE, MODEPAGE_ERR_RECOV, SD_PATH_STANDARD)) != 0) {
26440 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26441 		    "sr_change_blkmode: Mode Sense Failed\n");
26442 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26443 		return (rval);
26444 	}
26445 
26446 	/* Check the block descriptor len to handle only 1 block descriptor */
26447 	sense_mhp = (struct mode_header *)sense;
26448 	if ((sense_mhp->bdesc_length == 0) ||
26449 	    (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH)) {
26450 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26451 		    "sr_change_blkmode: Mode Sense returned invalid block"
26452 		    " descriptor length\n");
26453 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26454 		return (EIO);
26455 	}
26456 	sense_desc = (struct block_descriptor *)(sense + MODE_HEADER_LENGTH);
26457 	current_bsize = ((sense_desc->blksize_hi << 16) |
26458 	    (sense_desc->blksize_mid << 8) | sense_desc->blksize_lo);
26459 
26460 	/* Process command */
26461 	switch (cmd) {
26462 	case CDROMGBLKMODE:
26463 		/* Return the block size obtained during the mode sense */
26464 		if (ddi_copyout(&current_bsize, (void *)data,
26465 		    sizeof (int), flag) != 0)
26466 			rval = EFAULT;
26467 		break;
26468 	case CDROMSBLKMODE:
26469 		/* Validate the requested block size */
26470 		switch (data) {
26471 		case CDROM_BLK_512:
26472 		case CDROM_BLK_1024:
26473 		case CDROM_BLK_2048:
26474 		case CDROM_BLK_2056:
26475 		case CDROM_BLK_2336:
26476 		case CDROM_BLK_2340:
26477 		case CDROM_BLK_2352:
26478 		case CDROM_BLK_2368:
26479 		case CDROM_BLK_2448:
26480 		case CDROM_BLK_2646:
26481 		case CDROM_BLK_2647:
26482 			break;
26483 		default:
26484 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26485 			    "sr_change_blkmode: "
26486 			    "Block Size '%ld' Not Supported\n", data);
26487 			kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26488 			return (EINVAL);
26489 		}
26490 
26491 		/*
26492 		 * The current block size matches the requested block size so
26493 		 * there is no need to send the mode select to change the size
26494 		 */
26495 		if (current_bsize == data) {
26496 			break;
26497 		}
26498 
26499 		/* Build the select data for the requested block size */
26500 		select = kmem_zalloc(BUFLEN_CHG_BLK_MODE, KM_SLEEP);
26501 		select_mhp = (struct mode_header *)select;
26502 		select_desc =
26503 		    (struct block_descriptor *)(select + MODE_HEADER_LENGTH);
26504 		/*
26505 		 * The LBA size is changed via the block descriptor, so the
26506 		 * descriptor is built according to the user data
26507 		 */
26508 		select_mhp->bdesc_length = MODE_BLK_DESC_LENGTH;
26509 		select_desc->blksize_hi  = (char)(((data) & 0x00ff0000) >> 16);
26510 		select_desc->blksize_mid = (char)(((data) & 0x0000ff00) >> 8);
26511 		select_desc->blksize_lo  = (char)((data) & 0x000000ff);
26512 
26513 		/* Send the mode select for the requested block size */
26514 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
26515 		    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26516 		    SD_PATH_STANDARD)) != 0) {
26517 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26518 			    "sr_change_blkmode: Mode Select Failed\n");
26519 			/*
26520 			 * The mode select failed for the requested block size,
26521 			 * so reset the data for the original block size and
26522 			 * send it to the target. The error is indicated by the
26523 			 * return value for the failed mode select.
26524 			 */
26525 			select_desc->blksize_hi  = sense_desc->blksize_hi;
26526 			select_desc->blksize_mid = sense_desc->blksize_mid;
26527 			select_desc->blksize_lo  = sense_desc->blksize_lo;
26528 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0,
26529 			    select, BUFLEN_CHG_BLK_MODE, SD_DONTSAVE_PAGE,
26530 			    SD_PATH_STANDARD);
26531 		} else {
26532 			ASSERT(!mutex_owned(SD_MUTEX(un)));
26533 			mutex_enter(SD_MUTEX(un));
26534 			sd_update_block_info(un, (uint32_t)data, 0);
26535 
26536 			mutex_exit(SD_MUTEX(un));
26537 		}
26538 		break;
26539 	default:
26540 		/* should not reach here, but check anyway */
26541 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26542 		    "sr_change_blkmode: Command '%x' Not Supported\n", cmd);
26543 		rval = EINVAL;
26544 		break;
26545 	}
26546 
26547 	if (select) {
26548 		kmem_free(select, BUFLEN_CHG_BLK_MODE);
26549 	}
26550 	if (sense) {
26551 		kmem_free(sense, BUFLEN_CHG_BLK_MODE);
26552 	}
26553 	return (rval);
26554 }
26555 
26556 
26557 /*
26558  * Note: The following sr_change_speed() and sr_atapi_change_speed() routines
26559  * implement driver support for getting and setting the CD speed. The command
26560  * set used will be based on the device type. If the device has not been
26561  * identified as MMC the Toshiba vendor specific mode page will be used. If
26562  * the device is MMC but does not support the Real Time Streaming feature
26563  * the SET CD SPEED command will be used to set speed and mode page 0x2A will
26564  * be used to read the speed.
26565  */
26566 
26567 /*
26568  *    Function: sr_change_speed()
26569  *
26570  * Description: This routine is the driver entry point for handling CD-ROM
26571  *		drive speed ioctl requests for devices supporting the Toshiba
26572  *		vendor specific drive speed mode page. Support for returning
26573  *		and changing the current drive speed in use by the device is
26574  *		implemented.
26575  *
26576  *   Arguments: dev - the device 'dev_t'
26577  *		cmd - the request type; one of CDROMGDRVSPEED (get) or
26578  *		      CDROMSDRVSPEED (set)
26579  *		data - current drive speed or requested drive speed
26580  *		flag - this argument is a pass through to ddi_copyxxx() directly
26581  *		       from the mode argument of ioctl().
26582  *
26583  * Return Code: the code returned by sd_send_scsi_cmd()
26584  *		EINVAL if invalid arguments are provided
26585  *		EFAULT if ddi_copyxxx() fails
26586  *		ENXIO if fail ddi_get_soft_state
26587  *		EIO if invalid mode sense block descriptor length
26588  */
26589 
26590 static int
26591 sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
26592 {
26593 	struct sd_lun			*un = NULL;
26594 	struct mode_header		*sense_mhp, *select_mhp;
26595 	struct mode_speed		*sense_page, *select_page;
26596 	int				current_speed;
26597 	int				rval = EINVAL;
26598 	int				bd_len;
26599 	uchar_t				*sense = NULL;
26600 	uchar_t				*select = NULL;
26601 
26602 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
26603 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26604 		return (ENXIO);
26605 	}
26606 
26607 	/*
26608 	 * Note: The drive speed is being modified here according to a Toshiba
26609 	 * vendor specific mode page (0x31).
26610 	 */
26611 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
26612 
26613 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
26614 	    BUFLEN_MODE_CDROM_SPEED, CDROM_MODE_SPEED,
26615 	    SD_PATH_STANDARD)) != 0) {
26616 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26617 		    "sr_change_speed: Mode Sense Failed\n");
26618 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26619 		return (rval);
26620 	}
26621 	sense_mhp  = (struct mode_header *)sense;
26622 
26623 	/* Check the block descriptor len to handle only 1 block descriptor */
26624 	bd_len = sense_mhp->bdesc_length;
26625 	if (bd_len > MODE_BLK_DESC_LENGTH) {
26626 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26627 		    "sr_change_speed: Mode Sense returned invalid block "
26628 		    "descriptor length\n");
26629 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26630 		return (EIO);
26631 	}
26632 
26633 	sense_page = (struct mode_speed *)
26634 	    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
26635 	current_speed = sense_page->speed;
26636 
26637 	/* Process command */
26638 	switch (cmd) {
26639 	case CDROMGDRVSPEED:
26640 		/* Return the drive speed obtained during the mode sense */
26641 		if (current_speed == 0x2) {
26642 			current_speed = CDROM_TWELVE_SPEED;
26643 		}
26644 		if (ddi_copyout(&current_speed, (void *)data,
26645 		    sizeof (int), flag) != 0) {
26646 			rval = EFAULT;
26647 		}
26648 		break;
26649 	case CDROMSDRVSPEED:
26650 		/* Validate the requested drive speed */
26651 		switch ((uchar_t)data) {
26652 		case CDROM_TWELVE_SPEED:
26653 			data = 0x2;
26654 			/*FALLTHROUGH*/
26655 		case CDROM_NORMAL_SPEED:
26656 		case CDROM_DOUBLE_SPEED:
26657 		case CDROM_QUAD_SPEED:
26658 		case CDROM_MAXIMUM_SPEED:
26659 			break;
26660 		default:
26661 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26662 			    "sr_change_speed: "
26663 			    "Drive Speed '%d' Not Supported\n", (uchar_t)data);
26664 			kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26665 			return (EINVAL);
26666 		}
26667 
26668 		/*
26669 		 * The current drive speed matches the requested drive speed so
26670 		 * there is no need to send the mode select to change the speed
26671 		 */
26672 		if (current_speed == data) {
26673 			break;
26674 		}
26675 
26676 		/* Build the select data for the requested drive speed */
26677 		select = kmem_zalloc(BUFLEN_MODE_CDROM_SPEED, KM_SLEEP);
26678 		select_mhp = (struct mode_header *)select;
26679 		select_mhp->bdesc_length = 0;
26680 		select_page =
26681 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
26682 		select_page =
26683 		    (struct mode_speed *)(select + MODE_HEADER_LENGTH);
26684 		select_page->mode_page.code = CDROM_MODE_SPEED;
26685 		select_page->mode_page.length = 2;
26686 		select_page->speed = (uchar_t)data;
26687 
26688 		/* Send the mode select for the requested block size */
26689 		if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
26690 		    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
26691 		    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
26692 			/*
26693 			 * The mode select failed for the requested drive speed,
26694 			 * so reset the data for the original drive speed and
26695 			 * send it to the target. The error is indicated by the
26696 			 * return value for the failed mode select.
26697 			 */
26698 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26699 			    "sr_drive_speed: Mode Select Failed\n");
26700 			select_page->speed = sense_page->speed;
26701 			(void) sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
26702 			    MODEPAGE_CDROM_SPEED_LEN + MODE_HEADER_LENGTH,
26703 			    SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
26704 		}
26705 		break;
26706 	default:
26707 		/* should not reach here, but check anyway */
26708 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26709 		    "sr_change_speed: Command '%x' Not Supported\n", cmd);
26710 		rval = EINVAL;
26711 		break;
26712 	}
26713 
26714 	if (select) {
26715 		kmem_free(select, BUFLEN_MODE_CDROM_SPEED);
26716 	}
26717 	if (sense) {
26718 		kmem_free(sense, BUFLEN_MODE_CDROM_SPEED);
26719 	}
26720 
26721 	return (rval);
26722 }
26723 
26724 
26725 /*
26726  *    Function: sr_atapi_change_speed()
26727  *
26728  * Description: This routine is the driver entry point for handling CD-ROM
26729  *		drive speed ioctl requests for MMC devices that do not support
26730  *		the Real Time Streaming feature (0x107).
26731  *
26732  *		Note: This routine will use the SET SPEED command which may not
26733  *		be supported by all devices.
26734  *
26735  *   Arguments: dev- the device 'dev_t'
26736  *		cmd- the request type; one of CDROMGDRVSPEED (get) or
26737  *		     CDROMSDRVSPEED (set)
26738  *		data- current drive speed or requested drive speed
26739  *		flag- this argument is a pass through to ddi_copyxxx() directly
26740  *		      from the mode argument of ioctl().
26741  *
26742  * Return Code: the code returned by sd_send_scsi_cmd()
26743  *		EINVAL if invalid arguments are provided
26744  *		EFAULT if ddi_copyxxx() fails
26745  *		ENXIO if fail ddi_get_soft_state
26746  *		EIO if invalid mode sense block descriptor length
26747  */
26748 
26749 static int
26750 sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag)
26751 {
26752 	struct sd_lun			*un;
26753 	struct uscsi_cmd		*com = NULL;
26754 	struct mode_header_grp2		*sense_mhp;
26755 	uchar_t				*sense_page;
26756 	uchar_t				*sense = NULL;
26757 	char				cdb[CDB_GROUP5];
26758 	int				bd_len;
26759 	int				current_speed = 0;
26760 	int				max_speed = 0;
26761 	int				rval;
26762 
26763 	ASSERT((cmd == CDROMGDRVSPEED) || (cmd == CDROMSDRVSPEED));
26764 
26765 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26766 		return (ENXIO);
26767 	}
26768 
26769 	sense = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
26770 
26771 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
26772 	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP,
26773 	    SD_PATH_STANDARD)) != 0) {
26774 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26775 		    "sr_atapi_change_speed: Mode Sense Failed\n");
26776 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
26777 		return (rval);
26778 	}
26779 
26780 	/* Check the block descriptor len to handle only 1 block descriptor */
26781 	sense_mhp = (struct mode_header_grp2 *)sense;
26782 	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
26783 	if (bd_len > MODE_BLK_DESC_LENGTH) {
26784 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26785 		    "sr_atapi_change_speed: Mode Sense returned invalid "
26786 		    "block descriptor length\n");
26787 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
26788 		return (EIO);
26789 	}
26790 
26791 	/* Calculate the current and maximum drive speeds */
26792 	sense_page = (uchar_t *)(sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
26793 	current_speed = (sense_page[14] << 8) | sense_page[15];
26794 	max_speed = (sense_page[8] << 8) | sense_page[9];
26795 
26796 	/* Process the command */
26797 	switch (cmd) {
26798 	case CDROMGDRVSPEED:
26799 		current_speed /= SD_SPEED_1X;
26800 		if (ddi_copyout(&current_speed, (void *)data,
26801 		    sizeof (int), flag) != 0)
26802 			rval = EFAULT;
26803 		break;
26804 	case CDROMSDRVSPEED:
26805 		/* Convert the speed code to KB/sec */
26806 		switch ((uchar_t)data) {
26807 		case CDROM_NORMAL_SPEED:
26808 			current_speed = SD_SPEED_1X;
26809 			break;
26810 		case CDROM_DOUBLE_SPEED:
26811 			current_speed = 2 * SD_SPEED_1X;
26812 			break;
26813 		case CDROM_QUAD_SPEED:
26814 			current_speed = 4 * SD_SPEED_1X;
26815 			break;
26816 		case CDROM_TWELVE_SPEED:
26817 			current_speed = 12 * SD_SPEED_1X;
26818 			break;
26819 		case CDROM_MAXIMUM_SPEED:
26820 			current_speed = 0xffff;
26821 			break;
26822 		default:
26823 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26824 			    "sr_atapi_change_speed: invalid drive speed %d\n",
26825 			    (uchar_t)data);
26826 			kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
26827 			return (EINVAL);
26828 		}
26829 
26830 		/* Check the request against the drive's max speed. */
26831 		if (current_speed != 0xffff) {
26832 			if (current_speed > max_speed) {
26833 				kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
26834 				return (EINVAL);
26835 			}
26836 		}
26837 
26838 		/*
26839 		 * Build and send the SET SPEED command
26840 		 *
26841 		 * Note: The SET SPEED (0xBB) command used in this routine is
26842 		 * obsolete per the SCSI MMC spec but still supported in the
26843 		 * MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
26844 		 * therefore the command is still implemented in this routine.
26845 		 */
26846 		bzero(cdb, sizeof (cdb));
26847 		cdb[0] = (char)SCMD_SET_CDROM_SPEED;
26848 		cdb[2] = (uchar_t)(current_speed >> 8);
26849 		cdb[3] = (uchar_t)current_speed;
26850 		com = kmem_zalloc(sizeof (*com), KM_SLEEP);
26851 		com->uscsi_cdb	   = (caddr_t)cdb;
26852 		com->uscsi_cdblen  = CDB_GROUP5;
26853 		com->uscsi_bufaddr = NULL;
26854 		com->uscsi_buflen  = 0;
26855 		com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT;
26856 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, 0,
26857 		    UIO_SYSSPACE, SD_PATH_STANDARD);
26858 		break;
26859 	default:
26860 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
26861 		    "sr_atapi_change_speed: Command '%x' Not Supported\n", cmd);
26862 		rval = EINVAL;
26863 	}
26864 
26865 	if (sense) {
26866 		kmem_free(sense, BUFLEN_MODE_CDROM_CAP);
26867 	}
26868 	if (com) {
26869 		kmem_free(com, sizeof (*com));
26870 	}
26871 	return (rval);
26872 }
26873 
26874 
26875 /*
26876  *    Function: sr_pause_resume()
26877  *
26878  * Description: This routine is the driver entry point for handling CD-ROM
26879  *		pause/resume ioctl requests. This only affects the audio play
26880  *		operation.
26881  *
26882  *   Arguments: dev - the device 'dev_t'
26883  *		cmd - the request type; one of CDROMPAUSE or CDROMRESUME, used
26884  *		      for setting the resume bit of the cdb.
26885  *
26886  * Return Code: the code returned by sd_send_scsi_cmd()
26887  *		EINVAL if invalid mode specified
26888  *
26889  */
26890 
26891 static int
26892 sr_pause_resume(dev_t dev, int cmd)
26893 {
26894 	struct sd_lun		*un;
26895 	struct uscsi_cmd	*com;
26896 	char			cdb[CDB_GROUP1];
26897 	int			rval;
26898 
26899 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26900 		return (ENXIO);
26901 	}
26902 
26903 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
26904 	bzero(cdb, CDB_GROUP1);
26905 	cdb[0] = SCMD_PAUSE_RESUME;
26906 	switch (cmd) {
26907 	case CDROMRESUME:
26908 		cdb[8] = 1;
26909 		break;
26910 	case CDROMPAUSE:
26911 		cdb[8] = 0;
26912 		break;
26913 	default:
26914 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_pause_resume:"
26915 		    " Command '%x' Not Supported\n", cmd);
26916 		rval = EINVAL;
26917 		goto done;
26918 	}
26919 
26920 	com->uscsi_cdb    = cdb;
26921 	com->uscsi_cdblen = CDB_GROUP1;
26922 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
26923 
26924 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
26925 	    UIO_SYSSPACE, SD_PATH_STANDARD);
26926 
26927 done:
26928 	kmem_free(com, sizeof (*com));
26929 	return (rval);
26930 }
26931 
26932 
26933 /*
26934  *    Function: sr_play_msf()
26935  *
26936  * Description: This routine is the driver entry point for handling CD-ROM
26937  *		ioctl requests to output the audio signals at the specified
26938  *		starting address and continue the audio play until the specified
26939  *		ending address (CDROMPLAYMSF) The address is in Minute Second
26940  *		Frame (MSF) format.
26941  *
26942  *   Arguments: dev	- the device 'dev_t'
26943  *		data	- pointer to user provided audio msf structure,
26944  *		          specifying start/end addresses.
26945  *		flag	- this argument is a pass through to ddi_copyxxx()
26946  *		          directly from the mode argument of ioctl().
26947  *
26948  * Return Code: the code returned by sd_send_scsi_cmd()
26949  *		EFAULT if ddi_copyxxx() fails
26950  *		ENXIO if fail ddi_get_soft_state
26951  *		EINVAL if data pointer is NULL
26952  */
26953 
26954 static int
26955 sr_play_msf(dev_t dev, caddr_t data, int flag)
26956 {
26957 	struct sd_lun		*un;
26958 	struct uscsi_cmd	*com;
26959 	struct cdrom_msf	msf_struct;
26960 	struct cdrom_msf	*msf = &msf_struct;
26961 	char			cdb[CDB_GROUP1];
26962 	int			rval;
26963 
26964 	if (data == NULL) {
26965 		return (EINVAL);
26966 	}
26967 
26968 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
26969 		return (ENXIO);
26970 	}
26971 
26972 	if (ddi_copyin(data, msf, sizeof (struct cdrom_msf), flag)) {
26973 		return (EFAULT);
26974 	}
26975 
26976 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
26977 	bzero(cdb, CDB_GROUP1);
26978 	cdb[0] = SCMD_PLAYAUDIO_MSF;
26979 	if (un->un_f_cfg_playmsf_bcd == TRUE) {
26980 		cdb[3] = BYTE_TO_BCD(msf->cdmsf_min0);
26981 		cdb[4] = BYTE_TO_BCD(msf->cdmsf_sec0);
26982 		cdb[5] = BYTE_TO_BCD(msf->cdmsf_frame0);
26983 		cdb[6] = BYTE_TO_BCD(msf->cdmsf_min1);
26984 		cdb[7] = BYTE_TO_BCD(msf->cdmsf_sec1);
26985 		cdb[8] = BYTE_TO_BCD(msf->cdmsf_frame1);
26986 	} else {
26987 		cdb[3] = msf->cdmsf_min0;
26988 		cdb[4] = msf->cdmsf_sec0;
26989 		cdb[5] = msf->cdmsf_frame0;
26990 		cdb[6] = msf->cdmsf_min1;
26991 		cdb[7] = msf->cdmsf_sec1;
26992 		cdb[8] = msf->cdmsf_frame1;
26993 	}
26994 	com->uscsi_cdb    = cdb;
26995 	com->uscsi_cdblen = CDB_GROUP1;
26996 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
26997 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
26998 	    UIO_SYSSPACE, SD_PATH_STANDARD);
26999 	kmem_free(com, sizeof (*com));
27000 	return (rval);
27001 }
27002 
27003 
27004 /*
27005  *    Function: sr_play_trkind()
27006  *
27007  * Description: This routine is the driver entry point for handling CD-ROM
27008  *		ioctl requests to output the audio signals at the specified
27009  *		starting address and continue the audio play until the specified
27010  *		ending address (CDROMPLAYTRKIND). The address is in Track Index
27011  *		format.
27012  *
27013  *   Arguments: dev	- the device 'dev_t'
27014  *		data	- pointer to user provided audio track/index structure,
27015  *		          specifying start/end addresses.
27016  *		flag	- this argument is a pass through to ddi_copyxxx()
27017  *		          directly from the mode argument of ioctl().
27018  *
27019  * Return Code: the code returned by sd_send_scsi_cmd()
27020  *		EFAULT if ddi_copyxxx() fails
27021  *		ENXIO if fail ddi_get_soft_state
27022  *		EINVAL if data pointer is NULL
27023  */
27024 
27025 static int
27026 sr_play_trkind(dev_t dev, caddr_t data, int flag)
27027 {
27028 	struct cdrom_ti		ti_struct;
27029 	struct cdrom_ti		*ti = &ti_struct;
27030 	struct uscsi_cmd	*com = NULL;
27031 	char			cdb[CDB_GROUP1];
27032 	int			rval;
27033 
27034 	if (data == NULL) {
27035 		return (EINVAL);
27036 	}
27037 
27038 	if (ddi_copyin(data, ti, sizeof (struct cdrom_ti), flag)) {
27039 		return (EFAULT);
27040 	}
27041 
27042 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27043 	bzero(cdb, CDB_GROUP1);
27044 	cdb[0] = SCMD_PLAYAUDIO_TI;
27045 	cdb[4] = ti->cdti_trk0;
27046 	cdb[5] = ti->cdti_ind0;
27047 	cdb[7] = ti->cdti_trk1;
27048 	cdb[8] = ti->cdti_ind1;
27049 	com->uscsi_cdb    = cdb;
27050 	com->uscsi_cdblen = CDB_GROUP1;
27051 	com->uscsi_flags  = USCSI_DIAGNOSE|USCSI_SILENT;
27052 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27053 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27054 	kmem_free(com, sizeof (*com));
27055 	return (rval);
27056 }
27057 
27058 
27059 /*
27060  *    Function: sr_read_all_subcodes()
27061  *
27062  * Description: This routine is the driver entry point for handling CD-ROM
27063  *		ioctl requests to return raw subcode data while the target is
27064  *		playing audio (CDROMSUBCODE).
27065  *
27066  *   Arguments: dev	- the device 'dev_t'
27067  *		data	- pointer to user provided cdrom subcode structure,
27068  *		          specifying the transfer length and address.
27069  *		flag	- this argument is a pass through to ddi_copyxxx()
27070  *		          directly from the mode argument of ioctl().
27071  *
27072  * Return Code: the code returned by sd_send_scsi_cmd()
27073  *		EFAULT if ddi_copyxxx() fails
27074  *		ENXIO if fail ddi_get_soft_state
27075  *		EINVAL if data pointer is NULL
27076  */
27077 
27078 static int
27079 sr_read_all_subcodes(dev_t dev, caddr_t data, int flag)
27080 {
27081 	struct sd_lun		*un = NULL;
27082 	struct uscsi_cmd	*com = NULL;
27083 	struct cdrom_subcode	*subcode = NULL;
27084 	int			rval;
27085 	size_t			buflen;
27086 	char			cdb[CDB_GROUP5];
27087 
27088 #ifdef _MULTI_DATAMODEL
27089 	/* To support ILP32 applications in an LP64 world */
27090 	struct cdrom_subcode32		cdrom_subcode32;
27091 	struct cdrom_subcode32		*cdsc32 = &cdrom_subcode32;
27092 #endif
27093 	if (data == NULL) {
27094 		return (EINVAL);
27095 	}
27096 
27097 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
27098 		return (ENXIO);
27099 	}
27100 
27101 	subcode = kmem_zalloc(sizeof (struct cdrom_subcode), KM_SLEEP);
27102 
27103 #ifdef _MULTI_DATAMODEL
27104 	switch (ddi_model_convert_from(flag & FMODELS)) {
27105 	case DDI_MODEL_ILP32:
27106 		if (ddi_copyin(data, cdsc32, sizeof (*cdsc32), flag)) {
27107 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27108 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27109 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27110 			return (EFAULT);
27111 		}
27112 		/* Convert the ILP32 uscsi data from the application to LP64 */
27113 		cdrom_subcode32tocdrom_subcode(cdsc32, subcode);
27114 		break;
27115 	case DDI_MODEL_NONE:
27116 		if (ddi_copyin(data, subcode,
27117 		    sizeof (struct cdrom_subcode), flag)) {
27118 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27119 			    "sr_read_all_subcodes: ddi_copyin Failed\n");
27120 			kmem_free(subcode, sizeof (struct cdrom_subcode));
27121 			return (EFAULT);
27122 		}
27123 		break;
27124 	}
27125 #else /* ! _MULTI_DATAMODEL */
27126 	if (ddi_copyin(data, subcode, sizeof (struct cdrom_subcode), flag)) {
27127 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27128 		    "sr_read_all_subcodes: ddi_copyin Failed\n");
27129 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27130 		return (EFAULT);
27131 	}
27132 #endif /* _MULTI_DATAMODEL */
27133 
27134 	/*
27135 	 * Since MMC-2 expects max 3 bytes for length, check if the
27136 	 * length input is greater than 3 bytes
27137 	 */
27138 	if ((subcode->cdsc_length & 0xFF000000) != 0) {
27139 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27140 		    "sr_read_all_subcodes: "
27141 		    "cdrom transfer length too large: %d (limit %d)\n",
27142 		    subcode->cdsc_length, 0xFFFFFF);
27143 		kmem_free(subcode, sizeof (struct cdrom_subcode));
27144 		return (EINVAL);
27145 	}
27146 
27147 	buflen = CDROM_BLK_SUBCODE * subcode->cdsc_length;
27148 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27149 	bzero(cdb, CDB_GROUP5);
27150 
27151 	if (un->un_f_mmc_cap == TRUE) {
27152 		cdb[0] = (char)SCMD_READ_CD;
27153 		cdb[2] = (char)0xff;
27154 		cdb[3] = (char)0xff;
27155 		cdb[4] = (char)0xff;
27156 		cdb[5] = (char)0xff;
27157 		cdb[6] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27158 		cdb[7] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27159 		cdb[8] = ((subcode->cdsc_length) & 0x000000ff);
27160 		cdb[10] = 1;
27161 	} else {
27162 		/*
27163 		 * Note: A vendor specific command (0xDF) is being used her to
27164 		 * request a read of all subcodes.
27165 		 */
27166 		cdb[0] = (char)SCMD_READ_ALL_SUBCODES;
27167 		cdb[6] = (((subcode->cdsc_length) & 0xff000000) >> 24);
27168 		cdb[7] = (((subcode->cdsc_length) & 0x00ff0000) >> 16);
27169 		cdb[8] = (((subcode->cdsc_length) & 0x0000ff00) >> 8);
27170 		cdb[9] = ((subcode->cdsc_length) & 0x000000ff);
27171 	}
27172 	com->uscsi_cdb	   = cdb;
27173 	com->uscsi_cdblen  = CDB_GROUP5;
27174 	com->uscsi_bufaddr = (caddr_t)subcode->cdsc_addr;
27175 	com->uscsi_buflen  = buflen;
27176 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27177 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
27178 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27179 	kmem_free(subcode, sizeof (struct cdrom_subcode));
27180 	kmem_free(com, sizeof (*com));
27181 	return (rval);
27182 }
27183 
27184 
27185 /*
27186  *    Function: sr_read_subchannel()
27187  *
27188  * Description: This routine is the driver entry point for handling CD-ROM
27189  *		ioctl requests to return the Q sub-channel data of the CD
27190  *		current position block. (CDROMSUBCHNL) The data includes the
27191  *		track number, index number, absolute CD-ROM address (LBA or MSF
27192  *		format per the user) , track relative CD-ROM address (LBA or MSF
27193  *		format per the user), control data and audio status.
27194  *
27195  *   Arguments: dev	- the device 'dev_t'
27196  *		data	- pointer to user provided cdrom sub-channel structure
27197  *		flag	- this argument is a pass through to ddi_copyxxx()
27198  *		          directly from the mode argument of ioctl().
27199  *
27200  * Return Code: the code returned by sd_send_scsi_cmd()
27201  *		EFAULT if ddi_copyxxx() fails
27202  *		ENXIO if fail ddi_get_soft_state
27203  *		EINVAL if data pointer is NULL
27204  */
27205 
27206 static int
27207 sr_read_subchannel(dev_t dev, caddr_t data, int flag)
27208 {
27209 	struct sd_lun		*un;
27210 	struct uscsi_cmd	*com;
27211 	struct cdrom_subchnl	subchanel;
27212 	struct cdrom_subchnl	*subchnl = &subchanel;
27213 	char			cdb[CDB_GROUP1];
27214 	caddr_t			buffer;
27215 	int			rval;
27216 
27217 	if (data == NULL) {
27218 		return (EINVAL);
27219 	}
27220 
27221 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27222 	    (un->un_state == SD_STATE_OFFLINE)) {
27223 		return (ENXIO);
27224 	}
27225 
27226 	if (ddi_copyin(data, subchnl, sizeof (struct cdrom_subchnl), flag)) {
27227 		return (EFAULT);
27228 	}
27229 
27230 	buffer = kmem_zalloc((size_t)16, KM_SLEEP);
27231 	bzero(cdb, CDB_GROUP1);
27232 	cdb[0] = SCMD_READ_SUBCHANNEL;
27233 	/* Set the MSF bit based on the user requested address format */
27234 	cdb[1] = (subchnl->cdsc_format & CDROM_LBA) ? 0 : 0x02;
27235 	/*
27236 	 * Set the Q bit in byte 2 to indicate that Q sub-channel data be
27237 	 * returned
27238 	 */
27239 	cdb[2] = 0x40;
27240 	/*
27241 	 * Set byte 3 to specify the return data format. A value of 0x01
27242 	 * indicates that the CD-ROM current position should be returned.
27243 	 */
27244 	cdb[3] = 0x01;
27245 	cdb[8] = 0x10;
27246 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27247 	com->uscsi_cdb	   = cdb;
27248 	com->uscsi_cdblen  = CDB_GROUP1;
27249 	com->uscsi_bufaddr = buffer;
27250 	com->uscsi_buflen  = 16;
27251 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27252 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27253 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27254 	if (rval != 0) {
27255 		kmem_free(buffer, 16);
27256 		kmem_free(com, sizeof (*com));
27257 		return (rval);
27258 	}
27259 
27260 	/* Process the returned Q sub-channel data */
27261 	subchnl->cdsc_audiostatus = buffer[1];
27262 	subchnl->cdsc_adr	= (buffer[5] & 0xF0);
27263 	subchnl->cdsc_ctrl	= (buffer[5] & 0x0F);
27264 	subchnl->cdsc_trk	= buffer[6];
27265 	subchnl->cdsc_ind	= buffer[7];
27266 	if (subchnl->cdsc_format & CDROM_LBA) {
27267 		subchnl->cdsc_absaddr.lba =
27268 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27269 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27270 		subchnl->cdsc_reladdr.lba =
27271 		    ((uchar_t)buffer[12] << 24) + ((uchar_t)buffer[13] << 16) +
27272 		    ((uchar_t)buffer[14] << 8) + ((uchar_t)buffer[15]);
27273 	} else if (un->un_f_cfg_readsub_bcd == TRUE) {
27274 		subchnl->cdsc_absaddr.msf.minute = BCD_TO_BYTE(buffer[9]);
27275 		subchnl->cdsc_absaddr.msf.second = BCD_TO_BYTE(buffer[10]);
27276 		subchnl->cdsc_absaddr.msf.frame  = BCD_TO_BYTE(buffer[11]);
27277 		subchnl->cdsc_reladdr.msf.minute = BCD_TO_BYTE(buffer[13]);
27278 		subchnl->cdsc_reladdr.msf.second = BCD_TO_BYTE(buffer[14]);
27279 		subchnl->cdsc_reladdr.msf.frame  = BCD_TO_BYTE(buffer[15]);
27280 	} else {
27281 		subchnl->cdsc_absaddr.msf.minute = buffer[9];
27282 		subchnl->cdsc_absaddr.msf.second = buffer[10];
27283 		subchnl->cdsc_absaddr.msf.frame  = buffer[11];
27284 		subchnl->cdsc_reladdr.msf.minute = buffer[13];
27285 		subchnl->cdsc_reladdr.msf.second = buffer[14];
27286 		subchnl->cdsc_reladdr.msf.frame  = buffer[15];
27287 	}
27288 	kmem_free(buffer, 16);
27289 	kmem_free(com, sizeof (*com));
27290 	if (ddi_copyout(subchnl, data, sizeof (struct cdrom_subchnl), flag)
27291 	    != 0) {
27292 		return (EFAULT);
27293 	}
27294 	return (rval);
27295 }
27296 
27297 
27298 /*
27299  *    Function: sr_read_tocentry()
27300  *
27301  * Description: This routine is the driver entry point for handling CD-ROM
27302  *		ioctl requests to read from the Table of Contents (TOC)
27303  *		(CDROMREADTOCENTRY). This routine provides the ADR and CTRL
27304  *		fields, the starting address (LBA or MSF format per the user)
27305  *		and the data mode if the user specified track is a data track.
27306  *
27307  *		Note: The READ HEADER (0x44) command used in this routine is
27308  *		obsolete per the SCSI MMC spec but still supported in the
27309  *		MT FUJI vendor spec. Most equipment is adhereing to MT FUJI
27310  *		therefore the command is still implemented in this routine.
27311  *
27312  *   Arguments: dev	- the device 'dev_t'
27313  *		data	- pointer to user provided toc entry structure,
27314  *			  specifying the track # and the address format
27315  *			  (LBA or MSF).
27316  *		flag	- this argument is a pass through to ddi_copyxxx()
27317  *		          directly from the mode argument of ioctl().
27318  *
27319  * Return Code: the code returned by sd_send_scsi_cmd()
27320  *		EFAULT if ddi_copyxxx() fails
27321  *		ENXIO if fail ddi_get_soft_state
27322  *		EINVAL if data pointer is NULL
27323  */
27324 
27325 static int
27326 sr_read_tocentry(dev_t dev, caddr_t data, int flag)
27327 {
27328 	struct sd_lun		*un = NULL;
27329 	struct uscsi_cmd	*com;
27330 	struct cdrom_tocentry	toc_entry;
27331 	struct cdrom_tocentry	*entry = &toc_entry;
27332 	caddr_t			buffer;
27333 	int			rval;
27334 	char			cdb[CDB_GROUP1];
27335 
27336 	if (data == NULL) {
27337 		return (EINVAL);
27338 	}
27339 
27340 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27341 	    (un->un_state == SD_STATE_OFFLINE)) {
27342 		return (ENXIO);
27343 	}
27344 
27345 	if (ddi_copyin(data, entry, sizeof (struct cdrom_tocentry), flag)) {
27346 		return (EFAULT);
27347 	}
27348 
27349 	/* Validate the requested track and address format */
27350 	if (!(entry->cdte_format & (CDROM_LBA | CDROM_MSF))) {
27351 		return (EINVAL);
27352 	}
27353 
27354 	if (entry->cdte_track == 0) {
27355 		return (EINVAL);
27356 	}
27357 
27358 	buffer = kmem_zalloc((size_t)12, KM_SLEEP);
27359 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27360 	bzero(cdb, CDB_GROUP1);
27361 
27362 	cdb[0] = SCMD_READ_TOC;
27363 	/* Set the MSF bit based on the user requested address format  */
27364 	cdb[1] = ((entry->cdte_format & CDROM_LBA) ? 0 : 2);
27365 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27366 		cdb[6] = BYTE_TO_BCD(entry->cdte_track);
27367 	} else {
27368 		cdb[6] = entry->cdte_track;
27369 	}
27370 
27371 	/*
27372 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
27373 	 * (4 byte TOC response header + 8 byte track descriptor)
27374 	 */
27375 	cdb[8] = 12;
27376 	com->uscsi_cdb	   = cdb;
27377 	com->uscsi_cdblen  = CDB_GROUP1;
27378 	com->uscsi_bufaddr = buffer;
27379 	com->uscsi_buflen  = 0x0C;
27380 	com->uscsi_flags   = (USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ);
27381 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27382 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27383 	if (rval != 0) {
27384 		kmem_free(buffer, 12);
27385 		kmem_free(com, sizeof (*com));
27386 		return (rval);
27387 	}
27388 
27389 	/* Process the toc entry */
27390 	entry->cdte_adr		= (buffer[5] & 0xF0) >> 4;
27391 	entry->cdte_ctrl	= (buffer[5] & 0x0F);
27392 	if (entry->cdte_format & CDROM_LBA) {
27393 		entry->cdte_addr.lba =
27394 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
27395 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
27396 	} else if (un->un_f_cfg_read_toc_addr_bcd == TRUE) {
27397 		entry->cdte_addr.msf.minute	= BCD_TO_BYTE(buffer[9]);
27398 		entry->cdte_addr.msf.second	= BCD_TO_BYTE(buffer[10]);
27399 		entry->cdte_addr.msf.frame	= BCD_TO_BYTE(buffer[11]);
27400 		/*
27401 		 * Send a READ TOC command using the LBA address format to get
27402 		 * the LBA for the track requested so it can be used in the
27403 		 * READ HEADER request
27404 		 *
27405 		 * Note: The MSF bit of the READ HEADER command specifies the
27406 		 * output format. The block address specified in that command
27407 		 * must be in LBA format.
27408 		 */
27409 		cdb[1] = 0;
27410 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27411 		    UIO_SYSSPACE, SD_PATH_STANDARD);
27412 		if (rval != 0) {
27413 			kmem_free(buffer, 12);
27414 			kmem_free(com, sizeof (*com));
27415 			return (rval);
27416 		}
27417 	} else {
27418 		entry->cdte_addr.msf.minute	= buffer[9];
27419 		entry->cdte_addr.msf.second	= buffer[10];
27420 		entry->cdte_addr.msf.frame	= buffer[11];
27421 		/*
27422 		 * Send a READ TOC command using the LBA address format to get
27423 		 * the LBA for the track requested so it can be used in the
27424 		 * READ HEADER request
27425 		 *
27426 		 * Note: The MSF bit of the READ HEADER command specifies the
27427 		 * output format. The block address specified in that command
27428 		 * must be in LBA format.
27429 		 */
27430 		cdb[1] = 0;
27431 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27432 		    UIO_SYSSPACE, SD_PATH_STANDARD);
27433 		if (rval != 0) {
27434 			kmem_free(buffer, 12);
27435 			kmem_free(com, sizeof (*com));
27436 			return (rval);
27437 		}
27438 	}
27439 
27440 	/*
27441 	 * Build and send the READ HEADER command to determine the data mode of
27442 	 * the user specified track.
27443 	 */
27444 	if ((entry->cdte_ctrl & CDROM_DATA_TRACK) &&
27445 	    (entry->cdte_track != CDROM_LEADOUT)) {
27446 		bzero(cdb, CDB_GROUP1);
27447 		cdb[0] = SCMD_READ_HEADER;
27448 		cdb[2] = buffer[8];
27449 		cdb[3] = buffer[9];
27450 		cdb[4] = buffer[10];
27451 		cdb[5] = buffer[11];
27452 		cdb[8] = 0x08;
27453 		com->uscsi_buflen = 0x08;
27454 		rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27455 		    UIO_SYSSPACE, SD_PATH_STANDARD);
27456 		if (rval == 0) {
27457 			entry->cdte_datamode = buffer[0];
27458 		} else {
27459 			/*
27460 			 * READ HEADER command failed, since this is
27461 			 * obsoleted in one spec, its better to return
27462 			 * -1 for an invlid track so that we can still
27463 			 * recieve the rest of the TOC data.
27464 			 */
27465 			entry->cdte_datamode = (uchar_t)-1;
27466 		}
27467 	} else {
27468 		entry->cdte_datamode = (uchar_t)-1;
27469 	}
27470 
27471 	kmem_free(buffer, 12);
27472 	kmem_free(com, sizeof (*com));
27473 	if (ddi_copyout(entry, data, sizeof (struct cdrom_tocentry), flag) != 0)
27474 		return (EFAULT);
27475 
27476 	return (rval);
27477 }
27478 
27479 
27480 /*
27481  *    Function: sr_read_tochdr()
27482  *
27483  * Description: This routine is the driver entry point for handling CD-ROM
27484  * 		ioctl requests to read the Table of Contents (TOC) header
27485  *		(CDROMREADTOHDR). The TOC header consists of the disk starting
27486  *		and ending track numbers
27487  *
27488  *   Arguments: dev	- the device 'dev_t'
27489  *		data	- pointer to user provided toc header structure,
27490  *			  specifying the starting and ending track numbers.
27491  *		flag	- this argument is a pass through to ddi_copyxxx()
27492  *			  directly from the mode argument of ioctl().
27493  *
27494  * Return Code: the code returned by sd_send_scsi_cmd()
27495  *		EFAULT if ddi_copyxxx() fails
27496  *		ENXIO if fail ddi_get_soft_state
27497  *		EINVAL if data pointer is NULL
27498  */
27499 
27500 static int
27501 sr_read_tochdr(dev_t dev, caddr_t data, int flag)
27502 {
27503 	struct sd_lun		*un;
27504 	struct uscsi_cmd	*com;
27505 	struct cdrom_tochdr	toc_header;
27506 	struct cdrom_tochdr	*hdr = &toc_header;
27507 	char			cdb[CDB_GROUP1];
27508 	int			rval;
27509 	caddr_t			buffer;
27510 
27511 	if (data == NULL) {
27512 		return (EINVAL);
27513 	}
27514 
27515 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27516 	    (un->un_state == SD_STATE_OFFLINE)) {
27517 		return (ENXIO);
27518 	}
27519 
27520 	buffer = kmem_zalloc(4, KM_SLEEP);
27521 	bzero(cdb, CDB_GROUP1);
27522 	cdb[0] = SCMD_READ_TOC;
27523 	/*
27524 	 * Specifying a track number of 0x00 in the READ TOC command indicates
27525 	 * that the TOC header should be returned
27526 	 */
27527 	cdb[6] = 0x00;
27528 	/*
27529 	 * Bytes 7 & 8 are the 4 byte allocation length for TOC header.
27530 	 * (2 byte data len + 1 byte starting track # + 1 byte ending track #)
27531 	 */
27532 	cdb[8] = 0x04;
27533 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27534 	com->uscsi_cdb	   = cdb;
27535 	com->uscsi_cdblen  = CDB_GROUP1;
27536 	com->uscsi_bufaddr = buffer;
27537 	com->uscsi_buflen  = 0x04;
27538 	com->uscsi_timeout = 300;
27539 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27540 
27541 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
27542 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27543 	if (un->un_f_cfg_read_toc_trk_bcd == TRUE) {
27544 		hdr->cdth_trk0 = BCD_TO_BYTE(buffer[2]);
27545 		hdr->cdth_trk1 = BCD_TO_BYTE(buffer[3]);
27546 	} else {
27547 		hdr->cdth_trk0 = buffer[2];
27548 		hdr->cdth_trk1 = buffer[3];
27549 	}
27550 	kmem_free(buffer, 4);
27551 	kmem_free(com, sizeof (*com));
27552 	if (ddi_copyout(hdr, data, sizeof (struct cdrom_tochdr), flag) != 0) {
27553 		return (EFAULT);
27554 	}
27555 	return (rval);
27556 }
27557 
27558 
27559 /*
27560  * Note: The following sr_read_mode1(), sr_read_cd_mode2(), sr_read_mode2(),
27561  * sr_read_cdda(), sr_read_cdxa(), routines implement driver support for
27562  * handling CDROMREAD ioctl requests for mode 1 user data, mode 2 user data,
27563  * digital audio and extended architecture digital audio. These modes are
27564  * defined in the IEC908 (Red Book), ISO10149 (Yellow Book), and the SCSI3
27565  * MMC specs.
27566  *
27567  * In addition to support for the various data formats these routines also
27568  * include support for devices that implement only the direct access READ
27569  * commands (0x08, 0x28), devices that implement the READ_CD commands
27570  * (0xBE, 0xD4), and devices that implement the vendor unique READ CDDA and
27571  * READ CDXA commands (0xD8, 0xDB)
27572  */
27573 
27574 /*
27575  *    Function: sr_read_mode1()
27576  *
27577  * Description: This routine is the driver entry point for handling CD-ROM
27578  *		ioctl read mode1 requests (CDROMREADMODE1).
27579  *
27580  *   Arguments: dev	- the device 'dev_t'
27581  *		data	- pointer to user provided cd read structure specifying
27582  *			  the lba buffer address and length.
27583  *		flag	- this argument is a pass through to ddi_copyxxx()
27584  *			  directly from the mode argument of ioctl().
27585  *
27586  * Return Code: the code returned by sd_send_scsi_cmd()
27587  *		EFAULT if ddi_copyxxx() fails
27588  *		ENXIO if fail ddi_get_soft_state
27589  *		EINVAL if data pointer is NULL
27590  */
27591 
27592 static int
27593 sr_read_mode1(dev_t dev, caddr_t data, int flag)
27594 {
27595 	struct sd_lun		*un;
27596 	struct cdrom_read	mode1_struct;
27597 	struct cdrom_read	*mode1 = &mode1_struct;
27598 	int			rval;
27599 #ifdef _MULTI_DATAMODEL
27600 	/* To support ILP32 applications in an LP64 world */
27601 	struct cdrom_read32	cdrom_read32;
27602 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
27603 #endif /* _MULTI_DATAMODEL */
27604 
27605 	if (data == NULL) {
27606 		return (EINVAL);
27607 	}
27608 
27609 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27610 	    (un->un_state == SD_STATE_OFFLINE)) {
27611 		return (ENXIO);
27612 	}
27613 
27614 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27615 	    "sd_read_mode1: entry: un:0x%p\n", un);
27616 
27617 #ifdef _MULTI_DATAMODEL
27618 	switch (ddi_model_convert_from(flag & FMODELS)) {
27619 	case DDI_MODEL_ILP32:
27620 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
27621 			return (EFAULT);
27622 		}
27623 		/* Convert the ILP32 uscsi data from the application to LP64 */
27624 		cdrom_read32tocdrom_read(cdrd32, mode1);
27625 		break;
27626 	case DDI_MODEL_NONE:
27627 		if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
27628 			return (EFAULT);
27629 		}
27630 	}
27631 #else /* ! _MULTI_DATAMODEL */
27632 	if (ddi_copyin(data, mode1, sizeof (struct cdrom_read), flag)) {
27633 		return (EFAULT);
27634 	}
27635 #endif /* _MULTI_DATAMODEL */
27636 
27637 	rval = sd_send_scsi_READ(un, mode1->cdread_bufaddr,
27638 	    mode1->cdread_buflen, mode1->cdread_lba, SD_PATH_STANDARD);
27639 
27640 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27641 	    "sd_read_mode1: exit: un:0x%p\n", un);
27642 
27643 	return (rval);
27644 }
27645 
27646 
27647 /*
27648  *    Function: sr_read_cd_mode2()
27649  *
27650  * Description: This routine is the driver entry point for handling CD-ROM
27651  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
27652  *		support the READ CD (0xBE) command or the 1st generation
27653  *		READ CD (0xD4) command.
27654  *
27655  *   Arguments: dev	- the device 'dev_t'
27656  *		data	- pointer to user provided cd read structure specifying
27657  *			  the lba buffer address and length.
27658  *		flag	- this argument is a pass through to ddi_copyxxx()
27659  *			  directly from the mode argument of ioctl().
27660  *
27661  * Return Code: the code returned by sd_send_scsi_cmd()
27662  *		EFAULT if ddi_copyxxx() fails
27663  *		ENXIO if fail ddi_get_soft_state
27664  *		EINVAL if data pointer is NULL
27665  */
27666 
27667 static int
27668 sr_read_cd_mode2(dev_t dev, caddr_t data, int flag)
27669 {
27670 	struct sd_lun		*un;
27671 	struct uscsi_cmd	*com;
27672 	struct cdrom_read	mode2_struct;
27673 	struct cdrom_read	*mode2 = &mode2_struct;
27674 	uchar_t			cdb[CDB_GROUP5];
27675 	int			nblocks;
27676 	int			rval;
27677 #ifdef _MULTI_DATAMODEL
27678 	/*  To support ILP32 applications in an LP64 world */
27679 	struct cdrom_read32	cdrom_read32;
27680 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
27681 #endif /* _MULTI_DATAMODEL */
27682 
27683 	if (data == NULL) {
27684 		return (EINVAL);
27685 	}
27686 
27687 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27688 	    (un->un_state == SD_STATE_OFFLINE)) {
27689 		return (ENXIO);
27690 	}
27691 
27692 #ifdef _MULTI_DATAMODEL
27693 	switch (ddi_model_convert_from(flag & FMODELS)) {
27694 	case DDI_MODEL_ILP32:
27695 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
27696 			return (EFAULT);
27697 		}
27698 		/* Convert the ILP32 uscsi data from the application to LP64 */
27699 		cdrom_read32tocdrom_read(cdrd32, mode2);
27700 		break;
27701 	case DDI_MODEL_NONE:
27702 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
27703 			return (EFAULT);
27704 		}
27705 		break;
27706 	}
27707 
27708 #else /* ! _MULTI_DATAMODEL */
27709 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
27710 		return (EFAULT);
27711 	}
27712 #endif /* _MULTI_DATAMODEL */
27713 
27714 	bzero(cdb, sizeof (cdb));
27715 	if (un->un_f_cfg_read_cd_xd4 == TRUE) {
27716 		/* Read command supported by 1st generation atapi drives */
27717 		cdb[0] = SCMD_READ_CDD4;
27718 	} else {
27719 		/* Universal CD Access Command */
27720 		cdb[0] = SCMD_READ_CD;
27721 	}
27722 
27723 	/*
27724 	 * Set expected sector type to: 2336s byte, Mode 2 Yellow Book
27725 	 */
27726 	cdb[1] = CDROM_SECTOR_TYPE_MODE2;
27727 
27728 	/* set the start address */
27729 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 24) & 0XFF);
27730 	cdb[3] = (uchar_t)((mode2->cdread_lba >> 16) & 0XFF);
27731 	cdb[4] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
27732 	cdb[5] = (uchar_t)(mode2->cdread_lba & 0xFF);
27733 
27734 	/* set the transfer length */
27735 	nblocks = mode2->cdread_buflen / 2336;
27736 	cdb[6] = (uchar_t)(nblocks >> 16);
27737 	cdb[7] = (uchar_t)(nblocks >> 8);
27738 	cdb[8] = (uchar_t)nblocks;
27739 
27740 	/* set the filter bits */
27741 	cdb[9] = CDROM_READ_CD_USERDATA;
27742 
27743 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27744 	com->uscsi_cdb = (caddr_t)cdb;
27745 	com->uscsi_cdblen = sizeof (cdb);
27746 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
27747 	com->uscsi_buflen = mode2->cdread_buflen;
27748 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27749 
27750 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
27751 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27752 	kmem_free(com, sizeof (*com));
27753 	return (rval);
27754 }
27755 
27756 
27757 /*
27758  *    Function: sr_read_mode2()
27759  *
27760  * Description: This routine is the driver entry point for handling CD-ROM
27761  *		ioctl read mode2 requests (CDROMREADMODE2) for devices that
27762  *		do not support the READ CD (0xBE) command.
27763  *
27764  *   Arguments: dev	- the device 'dev_t'
27765  *		data	- pointer to user provided cd read structure specifying
27766  *			  the lba buffer address and length.
27767  *		flag	- this argument is a pass through to ddi_copyxxx()
27768  *			  directly from the mode argument of ioctl().
27769  *
27770  * Return Code: the code returned by sd_send_scsi_cmd()
27771  *		EFAULT if ddi_copyxxx() fails
27772  *		ENXIO if fail ddi_get_soft_state
27773  *		EINVAL if data pointer is NULL
27774  *		EIO if fail to reset block size
27775  *		EAGAIN if commands are in progress in the driver
27776  */
27777 
27778 static int
27779 sr_read_mode2(dev_t dev, caddr_t data, int flag)
27780 {
27781 	struct sd_lun		*un;
27782 	struct cdrom_read	mode2_struct;
27783 	struct cdrom_read	*mode2 = &mode2_struct;
27784 	int			rval;
27785 	uint32_t		restore_blksize;
27786 	struct uscsi_cmd	*com;
27787 	uchar_t			cdb[CDB_GROUP0];
27788 	int			nblocks;
27789 
27790 #ifdef _MULTI_DATAMODEL
27791 	/* To support ILP32 applications in an LP64 world */
27792 	struct cdrom_read32	cdrom_read32;
27793 	struct cdrom_read32	*cdrd32 = &cdrom_read32;
27794 #endif /* _MULTI_DATAMODEL */
27795 
27796 	if (data == NULL) {
27797 		return (EINVAL);
27798 	}
27799 
27800 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27801 	    (un->un_state == SD_STATE_OFFLINE)) {
27802 		return (ENXIO);
27803 	}
27804 
27805 	/*
27806 	 * Because this routine will update the device and driver block size
27807 	 * being used we want to make sure there are no commands in progress.
27808 	 * If commands are in progress the user will have to try again.
27809 	 *
27810 	 * We check for 1 instead of 0 because we increment un_ncmds_in_driver
27811 	 * in sdioctl to protect commands from sdioctl through to the top of
27812 	 * sd_uscsi_strategy. See sdioctl for details.
27813 	 */
27814 	mutex_enter(SD_MUTEX(un));
27815 	if (un->un_ncmds_in_driver != 1) {
27816 		mutex_exit(SD_MUTEX(un));
27817 		return (EAGAIN);
27818 	}
27819 	mutex_exit(SD_MUTEX(un));
27820 
27821 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27822 	    "sd_read_mode2: entry: un:0x%p\n", un);
27823 
27824 #ifdef _MULTI_DATAMODEL
27825 	switch (ddi_model_convert_from(flag & FMODELS)) {
27826 	case DDI_MODEL_ILP32:
27827 		if (ddi_copyin(data, cdrd32, sizeof (*cdrd32), flag) != 0) {
27828 			return (EFAULT);
27829 		}
27830 		/* Convert the ILP32 uscsi data from the application to LP64 */
27831 		cdrom_read32tocdrom_read(cdrd32, mode2);
27832 		break;
27833 	case DDI_MODEL_NONE:
27834 		if (ddi_copyin(data, mode2, sizeof (*mode2), flag) != 0) {
27835 			return (EFAULT);
27836 		}
27837 		break;
27838 	}
27839 #else /* ! _MULTI_DATAMODEL */
27840 	if (ddi_copyin(data, mode2, sizeof (*mode2), flag)) {
27841 		return (EFAULT);
27842 	}
27843 #endif /* _MULTI_DATAMODEL */
27844 
27845 	/* Store the current target block size for restoration later */
27846 	restore_blksize = un->un_tgt_blocksize;
27847 
27848 	/* Change the device and soft state target block size to 2336 */
27849 	if (sr_sector_mode(dev, SD_MODE2_BLKSIZE) != 0) {
27850 		rval = EIO;
27851 		goto done;
27852 	}
27853 
27854 
27855 	bzero(cdb, sizeof (cdb));
27856 
27857 	/* set READ operation */
27858 	cdb[0] = SCMD_READ;
27859 
27860 	/* adjust lba for 2kbyte blocks from 512 byte blocks */
27861 	mode2->cdread_lba >>= 2;
27862 
27863 	/* set the start address */
27864 	cdb[1] = (uchar_t)((mode2->cdread_lba >> 16) & 0X1F);
27865 	cdb[2] = (uchar_t)((mode2->cdread_lba >> 8) & 0xFF);
27866 	cdb[3] = (uchar_t)(mode2->cdread_lba & 0xFF);
27867 
27868 	/* set the transfer length */
27869 	nblocks = mode2->cdread_buflen / 2336;
27870 	cdb[4] = (uchar_t)nblocks & 0xFF;
27871 
27872 	/* build command */
27873 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
27874 	com->uscsi_cdb = (caddr_t)cdb;
27875 	com->uscsi_cdblen = sizeof (cdb);
27876 	com->uscsi_bufaddr = mode2->cdread_bufaddr;
27877 	com->uscsi_buflen = mode2->cdread_buflen;
27878 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
27879 
27880 	/*
27881 	 * Issue SCSI command with user space address for read buffer.
27882 	 *
27883 	 * This sends the command through main channel in the driver.
27884 	 *
27885 	 * Since this is accessed via an IOCTL call, we go through the
27886 	 * standard path, so that if the device was powered down, then
27887 	 * it would be 'awakened' to handle the command.
27888 	 */
27889 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
27890 	    UIO_SYSSPACE, SD_PATH_STANDARD);
27891 
27892 	kmem_free(com, sizeof (*com));
27893 
27894 	/* Restore the device and soft state target block size */
27895 	if (sr_sector_mode(dev, restore_blksize) != 0) {
27896 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
27897 		    "can't do switch back to mode 1\n");
27898 		/*
27899 		 * If sd_send_scsi_READ succeeded we still need to report
27900 		 * an error because we failed to reset the block size
27901 		 */
27902 		if (rval == 0) {
27903 			rval = EIO;
27904 		}
27905 	}
27906 
27907 done:
27908 	SD_TRACE(SD_LOG_ATTACH_DETACH, un,
27909 	    "sd_read_mode2: exit: un:0x%p\n", un);
27910 
27911 	return (rval);
27912 }
27913 
27914 
27915 /*
27916  *    Function: sr_sector_mode()
27917  *
27918  * Description: This utility function is used by sr_read_mode2 to set the target
27919  *		block size based on the user specified size. This is a legacy
27920  *		implementation based upon a vendor specific mode page
27921  *
27922  *   Arguments: dev	- the device 'dev_t'
27923  *		data	- flag indicating if block size is being set to 2336 or
27924  *			  512.
27925  *
27926  * Return Code: the code returned by sd_send_scsi_cmd()
27927  *		EFAULT if ddi_copyxxx() fails
27928  *		ENXIO if fail ddi_get_soft_state
27929  *		EINVAL if data pointer is NULL
27930  */
27931 
27932 static int
27933 sr_sector_mode(dev_t dev, uint32_t blksize)
27934 {
27935 	struct sd_lun	*un;
27936 	uchar_t		*sense;
27937 	uchar_t		*select;
27938 	int		rval;
27939 
27940 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
27941 	    (un->un_state == SD_STATE_OFFLINE)) {
27942 		return (ENXIO);
27943 	}
27944 
27945 	sense = kmem_zalloc(20, KM_SLEEP);
27946 
27947 	/* Note: This is a vendor specific mode page (0x81) */
27948 	if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, 20, 0x81,
27949 	    SD_PATH_STANDARD)) != 0) {
27950 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
27951 		    "sr_sector_mode: Mode Sense failed\n");
27952 		kmem_free(sense, 20);
27953 		return (rval);
27954 	}
27955 	select = kmem_zalloc(20, KM_SLEEP);
27956 	select[3] = 0x08;
27957 	select[10] = ((blksize >> 8) & 0xff);
27958 	select[11] = (blksize & 0xff);
27959 	select[12] = 0x01;
27960 	select[13] = 0x06;
27961 	select[14] = sense[14];
27962 	select[15] = sense[15];
27963 	if (blksize == SD_MODE2_BLKSIZE) {
27964 		select[14] |= 0x01;
27965 	}
27966 
27967 	if ((rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select, 20,
27968 	    SD_DONTSAVE_PAGE, SD_PATH_STANDARD)) != 0) {
27969 		SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
27970 		    "sr_sector_mode: Mode Select failed\n");
27971 	} else {
27972 		/*
27973 		 * Only update the softstate block size if we successfully
27974 		 * changed the device block mode.
27975 		 */
27976 		mutex_enter(SD_MUTEX(un));
27977 		sd_update_block_info(un, blksize, 0);
27978 		mutex_exit(SD_MUTEX(un));
27979 	}
27980 	kmem_free(sense, 20);
27981 	kmem_free(select, 20);
27982 	return (rval);
27983 }
27984 
27985 
27986 /*
27987  *    Function: sr_read_cdda()
27988  *
27989  * Description: This routine is the driver entry point for handling CD-ROM
27990  *		ioctl requests to return CD-DA or subcode data. (CDROMCDDA) If
27991  *		the target supports CDDA these requests are handled via a vendor
27992  *		specific command (0xD8) If the target does not support CDDA
27993  *		these requests are handled via the READ CD command (0xBE).
27994  *
27995  *   Arguments: dev	- the device 'dev_t'
27996  *		data	- pointer to user provided CD-DA structure specifying
27997  *			  the track starting address, transfer length, and
27998  *			  subcode options.
27999  *		flag	- this argument is a pass through to ddi_copyxxx()
28000  *			  directly from the mode argument of ioctl().
28001  *
28002  * Return Code: the code returned by sd_send_scsi_cmd()
28003  *		EFAULT if ddi_copyxxx() fails
28004  *		ENXIO if fail ddi_get_soft_state
28005  *		EINVAL if invalid arguments are provided
28006  *		ENOTTY
28007  */
28008 
28009 static int
28010 sr_read_cdda(dev_t dev, caddr_t data, int flag)
28011 {
28012 	struct sd_lun			*un;
28013 	struct uscsi_cmd		*com;
28014 	struct cdrom_cdda		*cdda;
28015 	int				rval;
28016 	size_t				buflen;
28017 	char				cdb[CDB_GROUP5];
28018 
28019 #ifdef _MULTI_DATAMODEL
28020 	/* To support ILP32 applications in an LP64 world */
28021 	struct cdrom_cdda32	cdrom_cdda32;
28022 	struct cdrom_cdda32	*cdda32 = &cdrom_cdda32;
28023 #endif /* _MULTI_DATAMODEL */
28024 
28025 	if (data == NULL) {
28026 		return (EINVAL);
28027 	}
28028 
28029 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28030 		return (ENXIO);
28031 	}
28032 
28033 	cdda = kmem_zalloc(sizeof (struct cdrom_cdda), KM_SLEEP);
28034 
28035 #ifdef _MULTI_DATAMODEL
28036 	switch (ddi_model_convert_from(flag & FMODELS)) {
28037 	case DDI_MODEL_ILP32:
28038 		if (ddi_copyin(data, cdda32, sizeof (*cdda32), flag)) {
28039 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28040 			    "sr_read_cdda: ddi_copyin Failed\n");
28041 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28042 			return (EFAULT);
28043 		}
28044 		/* Convert the ILP32 uscsi data from the application to LP64 */
28045 		cdrom_cdda32tocdrom_cdda(cdda32, cdda);
28046 		break;
28047 	case DDI_MODEL_NONE:
28048 		if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28049 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28050 			    "sr_read_cdda: ddi_copyin Failed\n");
28051 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28052 			return (EFAULT);
28053 		}
28054 		break;
28055 	}
28056 #else /* ! _MULTI_DATAMODEL */
28057 	if (ddi_copyin(data, cdda, sizeof (struct cdrom_cdda), flag)) {
28058 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28059 		    "sr_read_cdda: ddi_copyin Failed\n");
28060 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28061 		return (EFAULT);
28062 	}
28063 #endif /* _MULTI_DATAMODEL */
28064 
28065 	/*
28066 	 * Since MMC-2 expects max 3 bytes for length, check if the
28067 	 * length input is greater than 3 bytes
28068 	 */
28069 	if ((cdda->cdda_length & 0xFF000000) != 0) {
28070 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdda: "
28071 		    "cdrom transfer length too large: %d (limit %d)\n",
28072 		    cdda->cdda_length, 0xFFFFFF);
28073 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28074 		return (EINVAL);
28075 	}
28076 
28077 	switch (cdda->cdda_subcode) {
28078 	case CDROM_DA_NO_SUBCODE:
28079 		buflen = CDROM_BLK_2352 * cdda->cdda_length;
28080 		break;
28081 	case CDROM_DA_SUBQ:
28082 		buflen = CDROM_BLK_2368 * cdda->cdda_length;
28083 		break;
28084 	case CDROM_DA_ALL_SUBCODE:
28085 		buflen = CDROM_BLK_2448 * cdda->cdda_length;
28086 		break;
28087 	case CDROM_DA_SUBCODE_ONLY:
28088 		buflen = CDROM_BLK_SUBCODE * cdda->cdda_length;
28089 		break;
28090 	default:
28091 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28092 		    "sr_read_cdda: Subcode '0x%x' Not Supported\n",
28093 		    cdda->cdda_subcode);
28094 		kmem_free(cdda, sizeof (struct cdrom_cdda));
28095 		return (EINVAL);
28096 	}
28097 
28098 	/* Build and send the command */
28099 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28100 	bzero(cdb, CDB_GROUP5);
28101 
28102 	if (un->un_f_cfg_cdda == TRUE) {
28103 		cdb[0] = (char)SCMD_READ_CD;
28104 		cdb[1] = 0x04;
28105 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28106 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28107 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28108 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28109 		cdb[6] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28110 		cdb[7] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28111 		cdb[8] = ((cdda->cdda_length) & 0x000000ff);
28112 		cdb[9] = 0x10;
28113 		switch (cdda->cdda_subcode) {
28114 		case CDROM_DA_NO_SUBCODE :
28115 			cdb[10] = 0x0;
28116 			break;
28117 		case CDROM_DA_SUBQ :
28118 			cdb[10] = 0x2;
28119 			break;
28120 		case CDROM_DA_ALL_SUBCODE :
28121 			cdb[10] = 0x1;
28122 			break;
28123 		case CDROM_DA_SUBCODE_ONLY :
28124 			/* FALLTHROUGH */
28125 		default :
28126 			kmem_free(cdda, sizeof (struct cdrom_cdda));
28127 			kmem_free(com, sizeof (*com));
28128 			return (ENOTTY);
28129 		}
28130 	} else {
28131 		cdb[0] = (char)SCMD_READ_CDDA;
28132 		cdb[2] = (((cdda->cdda_addr) & 0xff000000) >> 24);
28133 		cdb[3] = (((cdda->cdda_addr) & 0x00ff0000) >> 16);
28134 		cdb[4] = (((cdda->cdda_addr) & 0x0000ff00) >> 8);
28135 		cdb[5] = ((cdda->cdda_addr) & 0x000000ff);
28136 		cdb[6] = (((cdda->cdda_length) & 0xff000000) >> 24);
28137 		cdb[7] = (((cdda->cdda_length) & 0x00ff0000) >> 16);
28138 		cdb[8] = (((cdda->cdda_length) & 0x0000ff00) >> 8);
28139 		cdb[9] = ((cdda->cdda_length) & 0x000000ff);
28140 		cdb[10] = cdda->cdda_subcode;
28141 	}
28142 
28143 	com->uscsi_cdb = cdb;
28144 	com->uscsi_cdblen = CDB_GROUP5;
28145 	com->uscsi_bufaddr = (caddr_t)cdda->cdda_data;
28146 	com->uscsi_buflen = buflen;
28147 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28148 
28149 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28150 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28151 
28152 	kmem_free(cdda, sizeof (struct cdrom_cdda));
28153 	kmem_free(com, sizeof (*com));
28154 	return (rval);
28155 }
28156 
28157 
28158 /*
28159  *    Function: sr_read_cdxa()
28160  *
28161  * Description: This routine is the driver entry point for handling CD-ROM
28162  *		ioctl requests to return CD-XA (Extended Architecture) data.
28163  *		(CDROMCDXA).
28164  *
28165  *   Arguments: dev	- the device 'dev_t'
28166  *		data	- pointer to user provided CD-XA structure specifying
28167  *			  the data starting address, transfer length, and format
28168  *		flag	- this argument is a pass through to ddi_copyxxx()
28169  *			  directly from the mode argument of ioctl().
28170  *
28171  * Return Code: the code returned by sd_send_scsi_cmd()
28172  *		EFAULT if ddi_copyxxx() fails
28173  *		ENXIO if fail ddi_get_soft_state
28174  *		EINVAL if data pointer is NULL
28175  */
28176 
28177 static int
28178 sr_read_cdxa(dev_t dev, caddr_t data, int flag)
28179 {
28180 	struct sd_lun		*un;
28181 	struct uscsi_cmd	*com;
28182 	struct cdrom_cdxa	*cdxa;
28183 	int			rval;
28184 	size_t			buflen;
28185 	char			cdb[CDB_GROUP5];
28186 	uchar_t			read_flags;
28187 
28188 #ifdef _MULTI_DATAMODEL
28189 	/* To support ILP32 applications in an LP64 world */
28190 	struct cdrom_cdxa32		cdrom_cdxa32;
28191 	struct cdrom_cdxa32		*cdxa32 = &cdrom_cdxa32;
28192 #endif /* _MULTI_DATAMODEL */
28193 
28194 	if (data == NULL) {
28195 		return (EINVAL);
28196 	}
28197 
28198 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28199 		return (ENXIO);
28200 	}
28201 
28202 	cdxa = kmem_zalloc(sizeof (struct cdrom_cdxa), KM_SLEEP);
28203 
28204 #ifdef _MULTI_DATAMODEL
28205 	switch (ddi_model_convert_from(flag & FMODELS)) {
28206 	case DDI_MODEL_ILP32:
28207 		if (ddi_copyin(data, cdxa32, sizeof (*cdxa32), flag)) {
28208 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28209 			return (EFAULT);
28210 		}
28211 		/*
28212 		 * Convert the ILP32 uscsi data from the
28213 		 * application to LP64 for internal use.
28214 		 */
28215 		cdrom_cdxa32tocdrom_cdxa(cdxa32, cdxa);
28216 		break;
28217 	case DDI_MODEL_NONE:
28218 		if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28219 			kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28220 			return (EFAULT);
28221 		}
28222 		break;
28223 	}
28224 #else /* ! _MULTI_DATAMODEL */
28225 	if (ddi_copyin(data, cdxa, sizeof (struct cdrom_cdxa), flag)) {
28226 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28227 		return (EFAULT);
28228 	}
28229 #endif /* _MULTI_DATAMODEL */
28230 
28231 	/*
28232 	 * Since MMC-2 expects max 3 bytes for length, check if the
28233 	 * length input is greater than 3 bytes
28234 	 */
28235 	if ((cdxa->cdxa_length & 0xFF000000) != 0) {
28236 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN, "sr_read_cdxa: "
28237 		    "cdrom transfer length too large: %d (limit %d)\n",
28238 		    cdxa->cdxa_length, 0xFFFFFF);
28239 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28240 		return (EINVAL);
28241 	}
28242 
28243 	switch (cdxa->cdxa_format) {
28244 	case CDROM_XA_DATA:
28245 		buflen = CDROM_BLK_2048 * cdxa->cdxa_length;
28246 		read_flags = 0x10;
28247 		break;
28248 	case CDROM_XA_SECTOR_DATA:
28249 		buflen = CDROM_BLK_2352 * cdxa->cdxa_length;
28250 		read_flags = 0xf8;
28251 		break;
28252 	case CDROM_XA_DATA_W_ERROR:
28253 		buflen = CDROM_BLK_2646 * cdxa->cdxa_length;
28254 		read_flags = 0xfc;
28255 		break;
28256 	default:
28257 		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28258 		    "sr_read_cdxa: Format '0x%x' Not Supported\n",
28259 		    cdxa->cdxa_format);
28260 		kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28261 		return (EINVAL);
28262 	}
28263 
28264 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28265 	bzero(cdb, CDB_GROUP5);
28266 	if (un->un_f_mmc_cap == TRUE) {
28267 		cdb[0] = (char)SCMD_READ_CD;
28268 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28269 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28270 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28271 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28272 		cdb[6] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28273 		cdb[7] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28274 		cdb[8] = ((cdxa->cdxa_length) & 0x000000ff);
28275 		cdb[9] = (char)read_flags;
28276 	} else {
28277 		/*
28278 		 * Note: A vendor specific command (0xDB) is being used her to
28279 		 * request a read of all subcodes.
28280 		 */
28281 		cdb[0] = (char)SCMD_READ_CDXA;
28282 		cdb[2] = (((cdxa->cdxa_addr) & 0xff000000) >> 24);
28283 		cdb[3] = (((cdxa->cdxa_addr) & 0x00ff0000) >> 16);
28284 		cdb[4] = (((cdxa->cdxa_addr) & 0x0000ff00) >> 8);
28285 		cdb[5] = ((cdxa->cdxa_addr) & 0x000000ff);
28286 		cdb[6] = (((cdxa->cdxa_length) & 0xff000000) >> 24);
28287 		cdb[7] = (((cdxa->cdxa_length) & 0x00ff0000) >> 16);
28288 		cdb[8] = (((cdxa->cdxa_length) & 0x0000ff00) >> 8);
28289 		cdb[9] = ((cdxa->cdxa_length) & 0x000000ff);
28290 		cdb[10] = cdxa->cdxa_format;
28291 	}
28292 	com->uscsi_cdb	   = cdb;
28293 	com->uscsi_cdblen  = CDB_GROUP5;
28294 	com->uscsi_bufaddr = (caddr_t)cdxa->cdxa_data;
28295 	com->uscsi_buflen  = buflen;
28296 	com->uscsi_flags   = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28297 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_USERSPACE,
28298 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28299 	kmem_free(cdxa, sizeof (struct cdrom_cdxa));
28300 	kmem_free(com, sizeof (*com));
28301 	return (rval);
28302 }
28303 
28304 
28305 /*
28306  *    Function: sr_eject()
28307  *
28308  * Description: This routine is the driver entry point for handling CD-ROM
28309  *		eject ioctl requests (FDEJECT, DKIOCEJECT, CDROMEJECT)
28310  *
28311  *   Arguments: dev	- the device 'dev_t'
28312  *
28313  * Return Code: the code returned by sd_send_scsi_cmd()
28314  */
28315 
28316 static int
28317 sr_eject(dev_t dev)
28318 {
28319 	struct sd_lun	*un;
28320 	int		rval;
28321 
28322 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28323 	    (un->un_state == SD_STATE_OFFLINE)) {
28324 		return (ENXIO);
28325 	}
28326 	if ((rval = sd_send_scsi_DOORLOCK(un, SD_REMOVAL_ALLOW,
28327 	    SD_PATH_STANDARD)) != 0) {
28328 		return (rval);
28329 	}
28330 
28331 	rval = sd_send_scsi_START_STOP_UNIT(un, SD_TARGET_EJECT,
28332 	    SD_PATH_STANDARD);
28333 
28334 	if (rval == 0) {
28335 		mutex_enter(SD_MUTEX(un));
28336 		sr_ejected(un);
28337 		un->un_mediastate = DKIO_EJECTED;
28338 		cv_broadcast(&un->un_state_cv);
28339 		mutex_exit(SD_MUTEX(un));
28340 	}
28341 	return (rval);
28342 }
28343 
28344 
28345 /*
28346  *    Function: sr_ejected()
28347  *
28348  * Description: This routine updates the soft state structure to invalidate the
28349  *		geometry information after the media has been ejected or a
28350  *		media eject has been detected.
28351  *
28352  *   Arguments: un - driver soft state (unit) structure
28353  */
28354 
28355 static void
28356 sr_ejected(struct sd_lun *un)
28357 {
28358 	struct sd_errstats *stp;
28359 
28360 	ASSERT(un != NULL);
28361 	ASSERT(mutex_owned(SD_MUTEX(un)));
28362 
28363 	un->un_f_blockcount_is_valid	= FALSE;
28364 	un->un_f_tgt_blocksize_is_valid	= FALSE;
28365 	un->un_f_geometry_is_valid	= FALSE;
28366 
28367 	if (un->un_errstats != NULL) {
28368 		stp = (struct sd_errstats *)un->un_errstats->ks_data;
28369 		stp->sd_capacity.value.ui64 = 0;
28370 	}
28371 }
28372 
28373 
28374 /*
28375  *    Function: sr_check_wp()
28376  *
28377  * Description: This routine checks the write protection of a removable media
28378  *		disk via the write protect bit of the Mode Page Header device
28379  *		specific field.  This routine has been implemented to use the
28380  *		error recovery mode page for all device types.
28381  *		Note: In the future use a sd_send_scsi_MODE_SENSE() routine
28382  *
28383  *   Arguments: dev		- the device 'dev_t'
28384  *
28385  * Return Code: int indicating if the device is write protected (1) or not (0)
28386  *
28387  *     Context: Kernel thread.
28388  *
28389  */
28390 
28391 static int
28392 sr_check_wp(dev_t dev)
28393 {
28394 	struct sd_lun	*un;
28395 	uchar_t		device_specific;
28396 	uchar_t		*sense;
28397 	int		hdrlen;
28398 	int		rval;
28399 	int		retry_flag = FALSE;
28400 
28401 	/*
28402 	 * Note: The return codes for this routine should be reworked to
28403 	 * properly handle the case of a NULL softstate.
28404 	 */
28405 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL) {
28406 		return (FALSE);
28407 	}
28408 
28409 	if (un->un_f_cfg_is_atapi == TRUE) {
28410 		retry_flag = TRUE;
28411 	}
28412 
28413 retry:
28414 	if (un->un_f_cfg_is_atapi == TRUE) {
28415 		/*
28416 		 * The mode page contents are not required; set the allocation
28417 		 * length for the mode page header only
28418 		 */
28419 		hdrlen = MODE_HEADER_LENGTH_GRP2;
28420 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28421 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense, hdrlen,
28422 		    MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
28423 		device_specific =
28424 		    ((struct mode_header_grp2 *)sense)->device_specific;
28425 	} else {
28426 		hdrlen = MODE_HEADER_LENGTH;
28427 		sense = kmem_zalloc(hdrlen, KM_SLEEP);
28428 		rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense, hdrlen,
28429 		    MODEPAGE_ERR_RECOV, SD_PATH_STANDARD);
28430 		device_specific =
28431 		    ((struct mode_header *)sense)->device_specific;
28432 	}
28433 
28434 	if (rval != 0) {
28435 		if ((un->un_f_cfg_is_atapi == TRUE) && (retry_flag)) {
28436 			/*
28437 			 * For an Atapi Zip drive, observed the drive
28438 			 * reporting check condition for the first attempt.
28439 			 * Sense data indicating power on or bus device/reset.
28440 			 * Hence in case of failure need to try at least once
28441 			 * for Atapi devices.
28442 			 */
28443 			retry_flag = FALSE;
28444 			kmem_free(sense, hdrlen);
28445 			goto retry;
28446 		} else {
28447 			/*
28448 			 * Write protect mode sense failed; not all disks
28449 			 * understand this query. Return FALSE assuming that
28450 			 * these devices are not writable.
28451 			 */
28452 			rval = FALSE;
28453 		}
28454 	} else {
28455 		if (device_specific & WRITE_PROTECT) {
28456 			rval = TRUE;
28457 		} else {
28458 			rval = FALSE;
28459 		}
28460 	}
28461 	kmem_free(sense, hdrlen);
28462 	return (rval);
28463 }
28464 
28465 
28466 /*
28467  *    Function: sr_volume_ctrl()
28468  *
28469  * Description: This routine is the driver entry point for handling CD-ROM
28470  *		audio output volume ioctl requests. (CDROMVOLCTRL)
28471  *
28472  *   Arguments: dev	- the device 'dev_t'
28473  *		data	- pointer to user audio volume control structure
28474  *		flag	- this argument is a pass through to ddi_copyxxx()
28475  *			  directly from the mode argument of ioctl().
28476  *
28477  * Return Code: the code returned by sd_send_scsi_cmd()
28478  *		EFAULT if ddi_copyxxx() fails
28479  *		ENXIO if fail ddi_get_soft_state
28480  *		EINVAL if data pointer is NULL
28481  *
28482  */
28483 
28484 static int
28485 sr_volume_ctrl(dev_t dev, caddr_t data, int flag)
28486 {
28487 	struct sd_lun		*un;
28488 	struct cdrom_volctrl    volume;
28489 	struct cdrom_volctrl    *vol = &volume;
28490 	uchar_t			*sense_page;
28491 	uchar_t			*select_page;
28492 	uchar_t			*sense;
28493 	uchar_t			*select;
28494 	int			sense_buflen;
28495 	int			select_buflen;
28496 	int			rval;
28497 
28498 	if (data == NULL) {
28499 		return (EINVAL);
28500 	}
28501 
28502 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28503 	    (un->un_state == SD_STATE_OFFLINE)) {
28504 		return (ENXIO);
28505 	}
28506 
28507 	if (ddi_copyin(data, vol, sizeof (struct cdrom_volctrl), flag)) {
28508 		return (EFAULT);
28509 	}
28510 
28511 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
28512 		struct mode_header_grp2		*sense_mhp;
28513 		struct mode_header_grp2		*select_mhp;
28514 		int				bd_len;
28515 
28516 		sense_buflen = MODE_PARAM_LENGTH_GRP2 + MODEPAGE_AUDIO_CTRL_LEN;
28517 		select_buflen = MODE_HEADER_LENGTH_GRP2 +
28518 		    MODEPAGE_AUDIO_CTRL_LEN;
28519 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
28520 		select = kmem_zalloc(select_buflen, KM_SLEEP);
28521 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP1, sense,
28522 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
28523 		    SD_PATH_STANDARD)) != 0) {
28524 			SD_ERROR(SD_LOG_IOCTL_RMMEDIA, un,
28525 			    "sr_volume_ctrl: Mode Sense Failed\n");
28526 			kmem_free(sense, sense_buflen);
28527 			kmem_free(select, select_buflen);
28528 			return (rval);
28529 		}
28530 		sense_mhp = (struct mode_header_grp2 *)sense;
28531 		select_mhp = (struct mode_header_grp2 *)select;
28532 		bd_len = (sense_mhp->bdesc_length_hi << 8) |
28533 		    sense_mhp->bdesc_length_lo;
28534 		if (bd_len > MODE_BLK_DESC_LENGTH) {
28535 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28536 			    "sr_volume_ctrl: Mode Sense returned invalid "
28537 			    "block descriptor length\n");
28538 			kmem_free(sense, sense_buflen);
28539 			kmem_free(select, select_buflen);
28540 			return (EIO);
28541 		}
28542 		sense_page = (uchar_t *)
28543 		    (sense + MODE_HEADER_LENGTH_GRP2 + bd_len);
28544 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH_GRP2);
28545 		select_mhp->length_msb = 0;
28546 		select_mhp->length_lsb = 0;
28547 		select_mhp->bdesc_length_hi = 0;
28548 		select_mhp->bdesc_length_lo = 0;
28549 	} else {
28550 		struct mode_header		*sense_mhp, *select_mhp;
28551 
28552 		sense_buflen = MODE_PARAM_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
28553 		select_buflen = MODE_HEADER_LENGTH + MODEPAGE_AUDIO_CTRL_LEN;
28554 		sense  = kmem_zalloc(sense_buflen, KM_SLEEP);
28555 		select = kmem_zalloc(select_buflen, KM_SLEEP);
28556 		if ((rval = sd_send_scsi_MODE_SENSE(un, CDB_GROUP0, sense,
28557 		    sense_buflen, MODEPAGE_AUDIO_CTRL,
28558 		    SD_PATH_STANDARD)) != 0) {
28559 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28560 			    "sr_volume_ctrl: Mode Sense Failed\n");
28561 			kmem_free(sense, sense_buflen);
28562 			kmem_free(select, select_buflen);
28563 			return (rval);
28564 		}
28565 		sense_mhp  = (struct mode_header *)sense;
28566 		select_mhp = (struct mode_header *)select;
28567 		if (sense_mhp->bdesc_length > MODE_BLK_DESC_LENGTH) {
28568 			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
28569 			    "sr_volume_ctrl: Mode Sense returned invalid "
28570 			    "block descriptor length\n");
28571 			kmem_free(sense, sense_buflen);
28572 			kmem_free(select, select_buflen);
28573 			return (EIO);
28574 		}
28575 		sense_page = (uchar_t *)
28576 		    (sense + MODE_HEADER_LENGTH + sense_mhp->bdesc_length);
28577 		select_page = (uchar_t *)(select + MODE_HEADER_LENGTH);
28578 		select_mhp->length = 0;
28579 		select_mhp->bdesc_length = 0;
28580 	}
28581 	/*
28582 	 * Note: An audio control data structure could be created and overlayed
28583 	 * on the following in place of the array indexing method implemented.
28584 	 */
28585 
28586 	/* Build the select data for the user volume data */
28587 	select_page[0] = MODEPAGE_AUDIO_CTRL;
28588 	select_page[1] = 0xE;
28589 	/* Set the immediate bit */
28590 	select_page[2] = 0x04;
28591 	/* Zero out reserved fields */
28592 	select_page[3] = 0x00;
28593 	select_page[4] = 0x00;
28594 	/* Return sense data for fields not to be modified */
28595 	select_page[5] = sense_page[5];
28596 	select_page[6] = sense_page[6];
28597 	select_page[7] = sense_page[7];
28598 	/* Set the user specified volume levels for channel 0 and 1 */
28599 	select_page[8] = 0x01;
28600 	select_page[9] = vol->channel0;
28601 	select_page[10] = 0x02;
28602 	select_page[11] = vol->channel1;
28603 	/* Channel 2 and 3 are currently unsupported so return the sense data */
28604 	select_page[12] = sense_page[12];
28605 	select_page[13] = sense_page[13];
28606 	select_page[14] = sense_page[14];
28607 	select_page[15] = sense_page[15];
28608 
28609 	if ((un->un_f_cfg_is_atapi == TRUE) || (un->un_f_mmc_cap == TRUE)) {
28610 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP1, select,
28611 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28612 	} else {
28613 		rval = sd_send_scsi_MODE_SELECT(un, CDB_GROUP0, select,
28614 		    select_buflen, SD_DONTSAVE_PAGE, SD_PATH_STANDARD);
28615 	}
28616 
28617 	kmem_free(sense, sense_buflen);
28618 	kmem_free(select, select_buflen);
28619 	return (rval);
28620 }
28621 
28622 
28623 /*
28624  *    Function: sr_read_sony_session_offset()
28625  *
28626  * Description: This routine is the driver entry point for handling CD-ROM
28627  *		ioctl requests for session offset information. (CDROMREADOFFSET)
28628  *		The address of the first track in the last session of a
28629  *		multi-session CD-ROM is returned
28630  *
28631  *		Note: This routine uses a vendor specific key value in the
28632  *		command control field without implementing any vendor check here
28633  *		or in the ioctl routine.
28634  *
28635  *   Arguments: dev	- the device 'dev_t'
28636  *		data	- pointer to an int to hold the requested address
28637  *		flag	- this argument is a pass through to ddi_copyxxx()
28638  *			  directly from the mode argument of ioctl().
28639  *
28640  * Return Code: the code returned by sd_send_scsi_cmd()
28641  *		EFAULT if ddi_copyxxx() fails
28642  *		ENXIO if fail ddi_get_soft_state
28643  *		EINVAL if data pointer is NULL
28644  */
28645 
28646 static int
28647 sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag)
28648 {
28649 	struct sd_lun		*un;
28650 	struct uscsi_cmd	*com;
28651 	caddr_t			buffer;
28652 	char			cdb[CDB_GROUP1];
28653 	int			session_offset = 0;
28654 	int			rval;
28655 
28656 	if (data == NULL) {
28657 		return (EINVAL);
28658 	}
28659 
28660 	if ((un = ddi_get_soft_state(sd_state, SDUNIT(dev))) == NULL ||
28661 	    (un->un_state == SD_STATE_OFFLINE)) {
28662 		return (ENXIO);
28663 	}
28664 
28665 	buffer = kmem_zalloc((size_t)SONY_SESSION_OFFSET_LEN, KM_SLEEP);
28666 	bzero(cdb, CDB_GROUP1);
28667 	cdb[0] = SCMD_READ_TOC;
28668 	/*
28669 	 * Bytes 7 & 8 are the 12 byte allocation length for a single entry.
28670 	 * (4 byte TOC response header + 8 byte response data)
28671 	 */
28672 	cdb[8] = SONY_SESSION_OFFSET_LEN;
28673 	/* Byte 9 is the control byte. A vendor specific value is used */
28674 	cdb[9] = SONY_SESSION_OFFSET_KEY;
28675 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
28676 	com->uscsi_cdb = cdb;
28677 	com->uscsi_cdblen = CDB_GROUP1;
28678 	com->uscsi_bufaddr = buffer;
28679 	com->uscsi_buflen = SONY_SESSION_OFFSET_LEN;
28680 	com->uscsi_flags = USCSI_DIAGNOSE|USCSI_SILENT|USCSI_READ;
28681 
28682 	rval = sd_send_scsi_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
28683 	    UIO_SYSSPACE, SD_PATH_STANDARD);
28684 	if (rval != 0) {
28685 		kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
28686 		kmem_free(com, sizeof (*com));
28687 		return (rval);
28688 	}
28689 	if (buffer[1] == SONY_SESSION_OFFSET_VALID) {
28690 		session_offset =
28691 		    ((uchar_t)buffer[8] << 24) + ((uchar_t)buffer[9] << 16) +
28692 		    ((uchar_t)buffer[10] << 8) + ((uchar_t)buffer[11]);
28693 		/*
28694 		 * Offset returned offset in current lbasize block's. Convert to
28695 		 * 2k block's to return to the user
28696 		 */
28697 		if (un->un_tgt_blocksize == CDROM_BLK_512) {
28698 			session_offset >>= 2;
28699 		} else if (un->un_tgt_blocksize == CDROM_BLK_1024) {
28700 			session_offset >>= 1;
28701 		}
28702 	}
28703 
28704 	if (ddi_copyout(&session_offset, data, sizeof (int), flag) != 0) {
28705 		rval = EFAULT;
28706 	}
28707 
28708 	kmem_free(buffer, SONY_SESSION_OFFSET_LEN);
28709 	kmem_free(com, sizeof (*com));
28710 	return (rval);
28711 }
28712 
28713 
28714 /*
28715  *    Function: sd_wm_cache_constructor()
28716  *
28717  * Description: Cache Constructor for the wmap cache for the read/modify/write
28718  * 		devices.
28719  *
28720  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
28721  *		un	- sd_lun structure for the device.
28722  *		flag	- the km flags passed to constructor
28723  *
28724  * Return Code: 0 on success.
28725  *		-1 on failure.
28726  */
28727 
28728 /*ARGSUSED*/
28729 static int
28730 sd_wm_cache_constructor(void *wm, void *un, int flags)
28731 {
28732 	bzero(wm, sizeof (struct sd_w_map));
28733 	cv_init(&((struct sd_w_map *)wm)->wm_avail, NULL, CV_DRIVER, NULL);
28734 	return (0);
28735 }
28736 
28737 
28738 /*
28739  *    Function: sd_wm_cache_destructor()
28740  *
28741  * Description: Cache destructor for the wmap cache for the read/modify/write
28742  * 		devices.
28743  *
28744  *   Arguments: wm      - A pointer to the sd_w_map to be initialized.
28745  *		un	- sd_lun structure for the device.
28746  */
28747 /*ARGSUSED*/
28748 static void
28749 sd_wm_cache_destructor(void *wm, void *un)
28750 {
28751 	cv_destroy(&((struct sd_w_map *)wm)->wm_avail);
28752 }
28753 
28754 
28755 /*
28756  *    Function: sd_range_lock()
28757  *
28758  * Description: Lock the range of blocks specified as parameter to ensure
28759  *		that read, modify write is atomic and no other i/o writes
28760  *		to the same location. The range is specified in terms
28761  *		of start and end blocks. Block numbers are the actual
28762  *		media block numbers and not system.
28763  *
28764  *   Arguments: un	- sd_lun structure for the device.
28765  *		startb - The starting block number
28766  *		endb - The end block number
28767  *		typ - type of i/o - simple/read_modify_write
28768  *
28769  * Return Code: wm  - pointer to the wmap structure.
28770  *
28771  *     Context: This routine can sleep.
28772  */
28773 
28774 static struct sd_w_map *
28775 sd_range_lock(struct sd_lun *un, daddr_t startb, daddr_t endb, ushort_t typ)
28776 {
28777 	struct sd_w_map *wmp = NULL;
28778 	struct sd_w_map *sl_wmp = NULL;
28779 	struct sd_w_map *tmp_wmp;
28780 	wm_state state = SD_WM_CHK_LIST;
28781 
28782 
28783 	ASSERT(un != NULL);
28784 	ASSERT(!mutex_owned(SD_MUTEX(un)));
28785 
28786 	mutex_enter(SD_MUTEX(un));
28787 
28788 	while (state != SD_WM_DONE) {
28789 
28790 		switch (state) {
28791 		case SD_WM_CHK_LIST:
28792 			/*
28793 			 * This is the starting state. Check the wmap list
28794 			 * to see if the range is currently available.
28795 			 */
28796 			if (!(typ & SD_WTYPE_RMW) && !(un->un_rmw_count)) {
28797 				/*
28798 				 * If this is a simple write and no rmw
28799 				 * i/o is pending then try to lock the
28800 				 * range as the range should be available.
28801 				 */
28802 				state = SD_WM_LOCK_RANGE;
28803 			} else {
28804 				tmp_wmp = sd_get_range(un, startb, endb);
28805 				if (tmp_wmp != NULL) {
28806 					if ((wmp != NULL) && ONLIST(un, wmp)) {
28807 						/*
28808 						 * Should not keep onlist wmps
28809 						 * while waiting this macro
28810 						 * will also do wmp = NULL;
28811 						 */
28812 						FREE_ONLIST_WMAP(un, wmp);
28813 					}
28814 					/*
28815 					 * sl_wmp is the wmap on which wait
28816 					 * is done, since the tmp_wmp points
28817 					 * to the inuse wmap, set sl_wmp to
28818 					 * tmp_wmp and change the state to sleep
28819 					 */
28820 					sl_wmp = tmp_wmp;
28821 					state = SD_WM_WAIT_MAP;
28822 				} else {
28823 					state = SD_WM_LOCK_RANGE;
28824 				}
28825 
28826 			}
28827 			break;
28828 
28829 		case SD_WM_LOCK_RANGE:
28830 			ASSERT(un->un_wm_cache);
28831 			/*
28832 			 * The range need to be locked, try to get a wmap.
28833 			 * First attempt it with NO_SLEEP, want to avoid a sleep
28834 			 * if possible as we will have to release the sd mutex
28835 			 * if we have to sleep.
28836 			 */
28837 			if (wmp == NULL)
28838 				wmp = kmem_cache_alloc(un->un_wm_cache,
28839 				    KM_NOSLEEP);
28840 			if (wmp == NULL) {
28841 				mutex_exit(SD_MUTEX(un));
28842 				_NOTE(DATA_READABLE_WITHOUT_LOCK
28843 				    (sd_lun::un_wm_cache))
28844 				wmp = kmem_cache_alloc(un->un_wm_cache,
28845 				    KM_SLEEP);
28846 				mutex_enter(SD_MUTEX(un));
28847 				/*
28848 				 * we released the mutex so recheck and go to
28849 				 * check list state.
28850 				 */
28851 				state = SD_WM_CHK_LIST;
28852 			} else {
28853 				/*
28854 				 * We exit out of state machine since we
28855 				 * have the wmap. Do the housekeeping first.
28856 				 * place the wmap on the wmap list if it is not
28857 				 * on it already and then set the state to done.
28858 				 */
28859 				wmp->wm_start = startb;
28860 				wmp->wm_end = endb;
28861 				wmp->wm_flags = typ | SD_WM_BUSY;
28862 				if (typ & SD_WTYPE_RMW) {
28863 					un->un_rmw_count++;
28864 				}
28865 				/*
28866 				 * If not already on the list then link
28867 				 */
28868 				if (!ONLIST(un, wmp)) {
28869 					wmp->wm_next = un->un_wm;
28870 					wmp->wm_prev = NULL;
28871 					if (wmp->wm_next)
28872 						wmp->wm_next->wm_prev = wmp;
28873 					un->un_wm = wmp;
28874 				}
28875 				state = SD_WM_DONE;
28876 			}
28877 			break;
28878 
28879 		case SD_WM_WAIT_MAP:
28880 			ASSERT(sl_wmp->wm_flags & SD_WM_BUSY);
28881 			/*
28882 			 * Wait is done on sl_wmp, which is set in the
28883 			 * check_list state.
28884 			 */
28885 			sl_wmp->wm_wanted_count++;
28886 			cv_wait(&sl_wmp->wm_avail, SD_MUTEX(un));
28887 			sl_wmp->wm_wanted_count--;
28888 			if (!(sl_wmp->wm_flags & SD_WM_BUSY)) {
28889 				if (wmp != NULL)
28890 					CHK_N_FREEWMP(un, wmp);
28891 				wmp = sl_wmp;
28892 			}
28893 			sl_wmp = NULL;
28894 			/*
28895 			 * After waking up, need to recheck for availability of
28896 			 * range.
28897 			 */
28898 			state = SD_WM_CHK_LIST;
28899 			break;
28900 
28901 		default:
28902 			panic("sd_range_lock: "
28903 			    "Unknown state %d in sd_range_lock", state);
28904 			/*NOTREACHED*/
28905 		} /* switch(state) */
28906 
28907 	} /* while(state != SD_WM_DONE) */
28908 
28909 	mutex_exit(SD_MUTEX(un));
28910 
28911 	ASSERT(wmp != NULL);
28912 
28913 	return (wmp);
28914 }
28915 
28916 
28917 /*
28918  *    Function: sd_get_range()
28919  *
28920  * Description: Find if there any overlapping I/O to this one
28921  *		Returns the write-map of 1st such I/O, NULL otherwise.
28922  *
28923  *   Arguments: un	- sd_lun structure for the device.
28924  *		startb - The starting block number
28925  *		endb - The end block number
28926  *
28927  * Return Code: wm  - pointer to the wmap structure.
28928  */
28929 
28930 static struct sd_w_map *
28931 sd_get_range(struct sd_lun *un, daddr_t startb, daddr_t endb)
28932 {
28933 	struct sd_w_map *wmp;
28934 
28935 	ASSERT(un != NULL);
28936 
28937 	for (wmp = un->un_wm; wmp != NULL; wmp = wmp->wm_next) {
28938 		if (!(wmp->wm_flags & SD_WM_BUSY)) {
28939 			continue;
28940 		}
28941 		if ((startb >= wmp->wm_start) && (startb <= wmp->wm_end)) {
28942 			break;
28943 		}
28944 		if ((endb >= wmp->wm_start) && (endb <= wmp->wm_end)) {
28945 			break;
28946 		}
28947 	}
28948 
28949 	return (wmp);
28950 }
28951 
28952 
28953 /*
28954  *    Function: sd_free_inlist_wmap()
28955  *
28956  * Description: Unlink and free a write map struct.
28957  *
28958  *   Arguments: un      - sd_lun structure for the device.
28959  *		wmp	- sd_w_map which needs to be unlinked.
28960  */
28961 
28962 static void
28963 sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp)
28964 {
28965 	ASSERT(un != NULL);
28966 
28967 	if (un->un_wm == wmp) {
28968 		un->un_wm = wmp->wm_next;
28969 	} else {
28970 		wmp->wm_prev->wm_next = wmp->wm_next;
28971 	}
28972 
28973 	if (wmp->wm_next) {
28974 		wmp->wm_next->wm_prev = wmp->wm_prev;
28975 	}
28976 
28977 	wmp->wm_next = wmp->wm_prev = NULL;
28978 
28979 	kmem_cache_free(un->un_wm_cache, wmp);
28980 }
28981 
28982 
28983 /*
28984  *    Function: sd_range_unlock()
28985  *
28986  * Description: Unlock the range locked by wm.
28987  *		Free write map if nobody else is waiting on it.
28988  *
28989  *   Arguments: un      - sd_lun structure for the device.
28990  *              wmp     - sd_w_map which needs to be unlinked.
28991  */
28992 
28993 static void
28994 sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm)
28995 {
28996 	ASSERT(un != NULL);
28997 	ASSERT(wm != NULL);
28998 	ASSERT(!mutex_owned(SD_MUTEX(un)));
28999 
29000 	mutex_enter(SD_MUTEX(un));
29001 
29002 	if (wm->wm_flags & SD_WTYPE_RMW) {
29003 		un->un_rmw_count--;
29004 	}
29005 
29006 	if (wm->wm_wanted_count) {
29007 		wm->wm_flags = 0;
29008 		/*
29009 		 * Broadcast that the wmap is available now.
29010 		 */
29011 		cv_broadcast(&wm->wm_avail);
29012 	} else {
29013 		/*
29014 		 * If no one is waiting on the map, it should be free'ed.
29015 		 */
29016 		sd_free_inlist_wmap(un, wm);
29017 	}
29018 
29019 	mutex_exit(SD_MUTEX(un));
29020 }
29021 
29022 
29023 /*
29024  *    Function: sd_read_modify_write_task
29025  *
29026  * Description: Called from a taskq thread to initiate the write phase of
29027  *		a read-modify-write request.  This is used for targets where
29028  *		un->un_sys_blocksize != un->un_tgt_blocksize.
29029  *
29030  *   Arguments: arg - a pointer to the buf(9S) struct for the write command.
29031  *
29032  *     Context: Called under taskq thread context.
29033  */
29034 
29035 static void
29036 sd_read_modify_write_task(void *arg)
29037 {
29038 	struct sd_mapblocksize_info	*bsp;
29039 	struct buf	*bp;
29040 	struct sd_xbuf	*xp;
29041 	struct sd_lun	*un;
29042 
29043 	bp = arg;	/* The bp is given in arg */
29044 	ASSERT(bp != NULL);
29045 
29046 	/* Get the pointer to the layer-private data struct */
29047 	xp = SD_GET_XBUF(bp);
29048 	ASSERT(xp != NULL);
29049 	bsp = xp->xb_private;
29050 	ASSERT(bsp != NULL);
29051 
29052 	un = SD_GET_UN(bp);
29053 	ASSERT(un != NULL);
29054 	ASSERT(!mutex_owned(SD_MUTEX(un)));
29055 
29056 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29057 	    "sd_read_modify_write_task: entry: buf:0x%p\n", bp);
29058 
29059 	/*
29060 	 * This is the write phase of a read-modify-write request, called
29061 	 * under the context of a taskq thread in response to the completion
29062 	 * of the read portion of the rmw request completing under interrupt
29063 	 * context. The write request must be sent from here down the iostart
29064 	 * chain as if it were being sent from sd_mapblocksize_iostart(), so
29065 	 * we use the layer index saved in the layer-private data area.
29066 	 */
29067 	SD_NEXT_IOSTART(bsp->mbs_layer_index, un, bp);
29068 
29069 	SD_TRACE(SD_LOG_IO_RMMEDIA, un,
29070 	    "sd_read_modify_write_task: exit: buf:0x%p\n", bp);
29071 }
29072 
29073 
29074 /*
29075  *    Function: sddump_do_read_of_rmw()
29076  *
29077  * Description: This routine will be called from sddump, If sddump is called
29078  *		with an I/O which not aligned on device blocksize boundary
29079  *		then the write has to be converted to read-modify-write.
29080  *		Do the read part here in order to keep sddump simple.
29081  *		Note - That the sd_mutex is held across the call to this
29082  *		routine.
29083  *
29084  *   Arguments: un	- sd_lun
29085  *		blkno	- block number in terms of media block size.
29086  *		nblk	- number of blocks.
29087  *		bpp	- pointer to pointer to the buf structure. On return
29088  *			from this function, *bpp points to the valid buffer
29089  *			to which the write has to be done.
29090  *
29091  * Return Code: 0 for success or errno-type return code
29092  */
29093 
29094 static int
29095 sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
29096 	struct buf **bpp)
29097 {
29098 	int err;
29099 	int i;
29100 	int rval;
29101 	struct buf *bp;
29102 	struct scsi_pkt *pkt = NULL;
29103 	uint32_t target_blocksize;
29104 
29105 	ASSERT(un != NULL);
29106 	ASSERT(mutex_owned(SD_MUTEX(un)));
29107 
29108 	target_blocksize = un->un_tgt_blocksize;
29109 
29110 	mutex_exit(SD_MUTEX(un));
29111 
29112 	bp = scsi_alloc_consistent_buf(SD_ADDRESS(un), (struct buf *)NULL,
29113 	    (size_t)(nblk * target_blocksize), B_READ, NULL_FUNC, NULL);
29114 	if (bp == NULL) {
29115 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29116 		    "no resources for dumping; giving up");
29117 		err = ENOMEM;
29118 		goto done;
29119 	}
29120 
29121 	rval = sd_setup_rw_pkt(un, &pkt, bp, 0, NULL_FUNC, NULL,
29122 	    blkno, nblk);
29123 	if (rval != 0) {
29124 		scsi_free_consistent_buf(bp);
29125 		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29126 		    "no resources for dumping; giving up");
29127 		err = ENOMEM;
29128 		goto done;
29129 	}
29130 
29131 	pkt->pkt_flags |= FLAG_NOINTR;
29132 
29133 	err = EIO;
29134 	for (i = 0; i < SD_NDUMP_RETRIES; i++) {
29135 
29136 		/*
29137 		 * Scsi_poll returns 0 (success) if the command completes and
29138 		 * the status block is STATUS_GOOD.  We should only check
29139 		 * errors if this condition is not true.  Even then we should
29140 		 * send our own request sense packet only if we have a check
29141 		 * condition and auto request sense has not been performed by
29142 		 * the hba.
29143 		 */
29144 		SD_TRACE(SD_LOG_DUMP, un, "sddump: sending read\n");
29145 
29146 		if ((sd_scsi_poll(un, pkt) == 0) && (pkt->pkt_resid == 0)) {
29147 			err = 0;
29148 			break;
29149 		}
29150 
29151 		/*
29152 		 * Check CMD_DEV_GONE 1st, give up if device is gone,
29153 		 * no need to read RQS data.
29154 		 */
29155 		if (pkt->pkt_reason == CMD_DEV_GONE) {
29156 			scsi_log(SD_DEVINFO(un), sd_label, CE_CONT,
29157 			    "Device is gone\n");
29158 			break;
29159 		}
29160 
29161 		if (SD_GET_PKT_STATUS(pkt) == STATUS_CHECK) {
29162 			SD_INFO(SD_LOG_DUMP, un,
29163 			    "sddump: read failed with CHECK, try # %d\n", i);
29164 			if (((pkt->pkt_state & STATE_ARQ_DONE) == 0)) {
29165 				(void) sd_send_polled_RQS(un);
29166 			}
29167 
29168 			continue;
29169 		}
29170 
29171 		if (SD_GET_PKT_STATUS(pkt) == STATUS_BUSY) {
29172 			int reset_retval = 0;
29173 
29174 			SD_INFO(SD_LOG_DUMP, un,
29175 			    "sddump: read failed with BUSY, try # %d\n", i);
29176 
29177 			if (un->un_f_lun_reset_enabled == TRUE) {
29178 				reset_retval = scsi_reset(SD_ADDRESS(un),
29179 				    RESET_LUN);
29180 			}
29181 			if (reset_retval == 0) {
29182 				(void) scsi_reset(SD_ADDRESS(un), RESET_TARGET);
29183 			}
29184 			(void) sd_send_polled_RQS(un);
29185 
29186 		} else {
29187 			SD_INFO(SD_LOG_DUMP, un,
29188 			    "sddump: read failed with 0x%x, try # %d\n",
29189 			    SD_GET_PKT_STATUS(pkt), i);
29190 			mutex_enter(SD_MUTEX(un));
29191 			sd_reset_target(un, pkt);
29192 			mutex_exit(SD_MUTEX(un));
29193 		}
29194 
29195 		/*
29196 		 * If we are not getting anywhere with lun/target resets,
29197 		 * let's reset the bus.
29198 		 */
29199 		if (i > SD_NDUMP_RETRIES/2) {
29200 			(void) scsi_reset(SD_ADDRESS(un), RESET_ALL);
29201 			(void) sd_send_polled_RQS(un);
29202 		}
29203 
29204 	}
29205 	scsi_destroy_pkt(pkt);
29206 
29207 	if (err != 0) {
29208 		scsi_free_consistent_buf(bp);
29209 		*bpp = NULL;
29210 	} else {
29211 		*bpp = bp;
29212 	}
29213 
29214 done:
29215 	mutex_enter(SD_MUTEX(un));
29216 	return (err);
29217 }
29218 
29219 
29220 /*
29221  *    Function: sd_failfast_flushq
29222  *
29223  * Description: Take all bp's on the wait queue that have B_FAILFAST set
29224  *		in b_flags and move them onto the failfast queue, then kick
29225  *		off a thread to return all bp's on the failfast queue to
29226  *		their owners with an error set.
29227  *
29228  *   Arguments: un - pointer to the soft state struct for the instance.
29229  *
29230  *     Context: may execute in interrupt context.
29231  */
29232 
29233 static void
29234 sd_failfast_flushq(struct sd_lun *un)
29235 {
29236 	struct buf *bp;
29237 	struct buf *next_waitq_bp;
29238 	struct buf *prev_waitq_bp = NULL;
29239 
29240 	ASSERT(un != NULL);
29241 	ASSERT(mutex_owned(SD_MUTEX(un)));
29242 	ASSERT(un->un_failfast_state == SD_FAILFAST_ACTIVE);
29243 	ASSERT(un->un_failfast_bp == NULL);
29244 
29245 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29246 	    "sd_failfast_flushq: entry: un:0x%p\n", un);
29247 
29248 	/*
29249 	 * Check if we should flush all bufs when entering failfast state, or
29250 	 * just those with B_FAILFAST set.
29251 	 */
29252 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) {
29253 		/*
29254 		 * Move *all* bp's on the wait queue to the failfast flush
29255 		 * queue, including those that do NOT have B_FAILFAST set.
29256 		 */
29257 		if (un->un_failfast_headp == NULL) {
29258 			ASSERT(un->un_failfast_tailp == NULL);
29259 			un->un_failfast_headp = un->un_waitq_headp;
29260 		} else {
29261 			ASSERT(un->un_failfast_tailp != NULL);
29262 			un->un_failfast_tailp->av_forw = un->un_waitq_headp;
29263 		}
29264 
29265 		un->un_failfast_tailp = un->un_waitq_tailp;
29266 		un->un_waitq_headp = un->un_waitq_tailp = NULL;
29267 
29268 	} else {
29269 		/*
29270 		 * Go thru the wait queue, pick off all entries with
29271 		 * B_FAILFAST set, and move these onto the failfast queue.
29272 		 */
29273 		for (bp = un->un_waitq_headp; bp != NULL; bp = next_waitq_bp) {
29274 			/*
29275 			 * Save the pointer to the next bp on the wait queue,
29276 			 * so we get to it on the next iteration of this loop.
29277 			 */
29278 			next_waitq_bp = bp->av_forw;
29279 
29280 			/*
29281 			 * If this bp from the wait queue does NOT have
29282 			 * B_FAILFAST set, just move on to the next element
29283 			 * in the wait queue. Note, this is the only place
29284 			 * where it is correct to set prev_waitq_bp.
29285 			 */
29286 			if ((bp->b_flags & B_FAILFAST) == 0) {
29287 				prev_waitq_bp = bp;
29288 				continue;
29289 			}
29290 
29291 			/*
29292 			 * Remove the bp from the wait queue.
29293 			 */
29294 			if (bp == un->un_waitq_headp) {
29295 				/* The bp is the first element of the waitq. */
29296 				un->un_waitq_headp = next_waitq_bp;
29297 				if (un->un_waitq_headp == NULL) {
29298 					/* The wait queue is now empty */
29299 					un->un_waitq_tailp = NULL;
29300 				}
29301 			} else {
29302 				/*
29303 				 * The bp is either somewhere in the middle
29304 				 * or at the end of the wait queue.
29305 				 */
29306 				ASSERT(un->un_waitq_headp != NULL);
29307 				ASSERT(prev_waitq_bp != NULL);
29308 				ASSERT((prev_waitq_bp->b_flags & B_FAILFAST)
29309 				    == 0);
29310 				if (bp == un->un_waitq_tailp) {
29311 					/* bp is the last entry on the waitq. */
29312 					ASSERT(next_waitq_bp == NULL);
29313 					un->un_waitq_tailp = prev_waitq_bp;
29314 				}
29315 				prev_waitq_bp->av_forw = next_waitq_bp;
29316 			}
29317 			bp->av_forw = NULL;
29318 
29319 			/*
29320 			 * Now put the bp onto the failfast queue.
29321 			 */
29322 			if (un->un_failfast_headp == NULL) {
29323 				/* failfast queue is currently empty */
29324 				ASSERT(un->un_failfast_tailp == NULL);
29325 				un->un_failfast_headp =
29326 				    un->un_failfast_tailp = bp;
29327 			} else {
29328 				/* Add the bp to the end of the failfast q */
29329 				ASSERT(un->un_failfast_tailp != NULL);
29330 				ASSERT(un->un_failfast_tailp->b_flags &
29331 				    B_FAILFAST);
29332 				un->un_failfast_tailp->av_forw = bp;
29333 				un->un_failfast_tailp = bp;
29334 			}
29335 		}
29336 	}
29337 
29338 	/*
29339 	 * Now return all bp's on the failfast queue to their owners.
29340 	 */
29341 	while ((bp = un->un_failfast_headp) != NULL) {
29342 
29343 		un->un_failfast_headp = bp->av_forw;
29344 		if (un->un_failfast_headp == NULL) {
29345 			un->un_failfast_tailp = NULL;
29346 		}
29347 
29348 		/*
29349 		 * We want to return the bp with a failure error code, but
29350 		 * we do not want a call to sd_start_cmds() to occur here,
29351 		 * so use sd_return_failed_command_no_restart() instead of
29352 		 * sd_return_failed_command().
29353 		 */
29354 		sd_return_failed_command_no_restart(un, bp, EIO);
29355 	}
29356 
29357 	/* Flush the xbuf queues if required. */
29358 	if (sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_QUEUES) {
29359 		ddi_xbuf_flushq(un->un_xbuf_attr, sd_failfast_flushq_callback);
29360 	}
29361 
29362 	SD_TRACE(SD_LOG_IO_FAILFAST, un,
29363 	    "sd_failfast_flushq: exit: un:0x%p\n", un);
29364 }
29365 
29366 
29367 /*
29368  *    Function: sd_failfast_flushq_callback
29369  *
29370  * Description: Return TRUE if the given bp meets the criteria for failfast
29371  *		flushing. Used with ddi_xbuf_flushq(9F).
29372  *
29373  *   Arguments: bp - ptr to buf struct to be examined.
29374  *
29375  *     Context: Any
29376  */
29377 
29378 static int
29379 sd_failfast_flushq_callback(struct buf *bp)
29380 {
29381 	/*
29382 	 * Return TRUE if (1) we want to flush ALL bufs when the failfast
29383 	 * state is entered; OR (2) the given bp has B_FAILFAST set.
29384 	 */
29385 	return (((sd_failfast_flushctl & SD_FAILFAST_FLUSH_ALL_BUFS) ||
29386 	    (bp->b_flags & B_FAILFAST)) ? TRUE : FALSE);
29387 }
29388 
29389 
29390 #if defined(__i386) || defined(__amd64)
29391 /*
29392  * Function: sd_setup_next_xfer
29393  *
29394  * Description: Prepare next I/O operation using DMA_PARTIAL
29395  *
29396  */
29397 
29398 static int
29399 sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
29400     struct scsi_pkt *pkt, struct sd_xbuf *xp)
29401 {
29402 	ssize_t	num_blks_not_xfered;
29403 	daddr_t	strt_blk_num;
29404 	ssize_t	bytes_not_xfered;
29405 	int	rval;
29406 
29407 	ASSERT(pkt->pkt_resid == 0);
29408 
29409 	/*
29410 	 * Calculate next block number and amount to be transferred.
29411 	 *
29412 	 * How much data NOT transfered to the HBA yet.
29413 	 */
29414 	bytes_not_xfered = xp->xb_dma_resid;
29415 
29416 	/*
29417 	 * figure how many blocks NOT transfered to the HBA yet.
29418 	 */
29419 	num_blks_not_xfered = SD_BYTES2TGTBLOCKS(un, bytes_not_xfered);
29420 
29421 	/*
29422 	 * set starting block number to the end of what WAS transfered.
29423 	 */
29424 	strt_blk_num = xp->xb_blkno +
29425 	    SD_BYTES2TGTBLOCKS(un, bp->b_bcount - bytes_not_xfered);
29426 
29427 	/*
29428 	 * Move pkt to the next portion of the xfer.  sd_setup_next_rw_pkt
29429 	 * will call scsi_initpkt with NULL_FUNC so we do not have to release
29430 	 * the disk mutex here.
29431 	 */
29432 	rval = sd_setup_next_rw_pkt(un, pkt, bp,
29433 	    strt_blk_num, num_blks_not_xfered);
29434 
29435 	if (rval == 0) {
29436 
29437 		/*
29438 		 * Success.
29439 		 *
29440 		 * Adjust things if there are still more blocks to be
29441 		 * transfered.
29442 		 */
29443 		xp->xb_dma_resid = pkt->pkt_resid;
29444 		pkt->pkt_resid = 0;
29445 
29446 		return (1);
29447 	}
29448 
29449 	/*
29450 	 * There's really only one possible return value from
29451 	 * sd_setup_next_rw_pkt which occurs when scsi_init_pkt
29452 	 * returns NULL.
29453 	 */
29454 	ASSERT(rval == SD_PKT_ALLOC_FAILURE);
29455 
29456 	bp->b_resid = bp->b_bcount;
29457 	bp->b_flags |= B_ERROR;
29458 
29459 	scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
29460 	    "Error setting up next portion of DMA transfer\n");
29461 
29462 	return (0);
29463 }
29464 #endif
29465 
29466 /*
29467  * Note: The following sd_faultinjection_ioctl( ) routines implement
29468  * driver support for handling fault injection for error analysis
29469  * causing faults in multiple layers of the driver.
29470  *
29471  */
29472 
29473 #ifdef SD_FAULT_INJECTION
29474 static uint_t   sd_fault_injection_on = 0;
29475 
29476 /*
29477  *    Function: sd_faultinjection_ioctl()
29478  *
29479  * Description: This routine is the driver entry point for handling
29480  *              faultinjection ioctls to inject errors into the
29481  *              layer model
29482  *
29483  *   Arguments: cmd	- the ioctl cmd recieved
29484  *		arg	- the arguments from user and returns
29485  */
29486 
29487 static void
29488 sd_faultinjection_ioctl(int cmd, intptr_t arg,  struct sd_lun *un) {
29489 
29490 	uint_t i;
29491 	uint_t rval;
29492 
29493 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl: entry\n");
29494 
29495 	mutex_enter(SD_MUTEX(un));
29496 
29497 	switch (cmd) {
29498 	case SDIOCRUN:
29499 		/* Allow pushed faults to be injected */
29500 		SD_INFO(SD_LOG_SDTEST, un,
29501 		    "sd_faultinjection_ioctl: Injecting Fault Run\n");
29502 
29503 		sd_fault_injection_on = 1;
29504 
29505 		SD_INFO(SD_LOG_IOERR, un,
29506 		    "sd_faultinjection_ioctl: run finished\n");
29507 		break;
29508 
29509 	case SDIOCSTART:
29510 		/* Start Injection Session */
29511 		SD_INFO(SD_LOG_SDTEST, un,
29512 		    "sd_faultinjection_ioctl: Injecting Fault Start\n");
29513 
29514 		sd_fault_injection_on = 0;
29515 		un->sd_injection_mask = 0xFFFFFFFF;
29516 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
29517 			un->sd_fi_fifo_pkt[i] = NULL;
29518 			un->sd_fi_fifo_xb[i] = NULL;
29519 			un->sd_fi_fifo_un[i] = NULL;
29520 			un->sd_fi_fifo_arq[i] = NULL;
29521 		}
29522 		un->sd_fi_fifo_start = 0;
29523 		un->sd_fi_fifo_end = 0;
29524 
29525 		mutex_enter(&(un->un_fi_mutex));
29526 		un->sd_fi_log[0] = '\0';
29527 		un->sd_fi_buf_len = 0;
29528 		mutex_exit(&(un->un_fi_mutex));
29529 
29530 		SD_INFO(SD_LOG_IOERR, un,
29531 		    "sd_faultinjection_ioctl: start finished\n");
29532 		break;
29533 
29534 	case SDIOCSTOP:
29535 		/* Stop Injection Session */
29536 		SD_INFO(SD_LOG_SDTEST, un,
29537 		    "sd_faultinjection_ioctl: Injecting Fault Stop\n");
29538 		sd_fault_injection_on = 0;
29539 		un->sd_injection_mask = 0x0;
29540 
29541 		/* Empty stray or unuseds structs from fifo */
29542 		for (i = 0; i < SD_FI_MAX_ERROR; i++) {
29543 			if (un->sd_fi_fifo_pkt[i] != NULL) {
29544 				kmem_free(un->sd_fi_fifo_pkt[i],
29545 				    sizeof (struct sd_fi_pkt));
29546 			}
29547 			if (un->sd_fi_fifo_xb[i] != NULL) {
29548 				kmem_free(un->sd_fi_fifo_xb[i],
29549 				    sizeof (struct sd_fi_xb));
29550 			}
29551 			if (un->sd_fi_fifo_un[i] != NULL) {
29552 				kmem_free(un->sd_fi_fifo_un[i],
29553 				    sizeof (struct sd_fi_un));
29554 			}
29555 			if (un->sd_fi_fifo_arq[i] != NULL) {
29556 				kmem_free(un->sd_fi_fifo_arq[i],
29557 				    sizeof (struct sd_fi_arq));
29558 			}
29559 			un->sd_fi_fifo_pkt[i] = NULL;
29560 			un->sd_fi_fifo_un[i] = NULL;
29561 			un->sd_fi_fifo_xb[i] = NULL;
29562 			un->sd_fi_fifo_arq[i] = NULL;
29563 		}
29564 		un->sd_fi_fifo_start = 0;
29565 		un->sd_fi_fifo_end = 0;
29566 
29567 		SD_INFO(SD_LOG_IOERR, un,
29568 		    "sd_faultinjection_ioctl: stop finished\n");
29569 		break;
29570 
29571 	case SDIOCINSERTPKT:
29572 		/* Store a packet struct to be pushed onto fifo */
29573 		SD_INFO(SD_LOG_SDTEST, un,
29574 		    "sd_faultinjection_ioctl: Injecting Fault Insert Pkt\n");
29575 
29576 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
29577 
29578 		sd_fault_injection_on = 0;
29579 
29580 		/* No more that SD_FI_MAX_ERROR allowed in Queue */
29581 		if (un->sd_fi_fifo_pkt[i] != NULL) {
29582 			kmem_free(un->sd_fi_fifo_pkt[i],
29583 			    sizeof (struct sd_fi_pkt));
29584 		}
29585 		if (arg != NULL) {
29586 			un->sd_fi_fifo_pkt[i] =
29587 			    kmem_alloc(sizeof (struct sd_fi_pkt), KM_NOSLEEP);
29588 			if (un->sd_fi_fifo_pkt[i] == NULL) {
29589 				/* Alloc failed don't store anything */
29590 				break;
29591 			}
29592 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_pkt[i],
29593 			    sizeof (struct sd_fi_pkt), 0);
29594 			if (rval == -1) {
29595 				kmem_free(un->sd_fi_fifo_pkt[i],
29596 				    sizeof (struct sd_fi_pkt));
29597 				un->sd_fi_fifo_pkt[i] = NULL;
29598 			}
29599 		} else {
29600 			SD_INFO(SD_LOG_IOERR, un,
29601 			    "sd_faultinjection_ioctl: pkt null\n");
29602 		}
29603 		break;
29604 
29605 	case SDIOCINSERTXB:
29606 		/* Store a xb struct to be pushed onto fifo */
29607 		SD_INFO(SD_LOG_SDTEST, un,
29608 		    "sd_faultinjection_ioctl: Injecting Fault Insert XB\n");
29609 
29610 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
29611 
29612 		sd_fault_injection_on = 0;
29613 
29614 		if (un->sd_fi_fifo_xb[i] != NULL) {
29615 			kmem_free(un->sd_fi_fifo_xb[i],
29616 			    sizeof (struct sd_fi_xb));
29617 			un->sd_fi_fifo_xb[i] = NULL;
29618 		}
29619 		if (arg != NULL) {
29620 			un->sd_fi_fifo_xb[i] =
29621 			    kmem_alloc(sizeof (struct sd_fi_xb), KM_NOSLEEP);
29622 			if (un->sd_fi_fifo_xb[i] == NULL) {
29623 				/* Alloc failed don't store anything */
29624 				break;
29625 			}
29626 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_xb[i],
29627 			    sizeof (struct sd_fi_xb), 0);
29628 
29629 			if (rval == -1) {
29630 				kmem_free(un->sd_fi_fifo_xb[i],
29631 				    sizeof (struct sd_fi_xb));
29632 				un->sd_fi_fifo_xb[i] = NULL;
29633 			}
29634 		} else {
29635 			SD_INFO(SD_LOG_IOERR, un,
29636 			    "sd_faultinjection_ioctl: xb null\n");
29637 		}
29638 		break;
29639 
29640 	case SDIOCINSERTUN:
29641 		/* Store a un struct to be pushed onto fifo */
29642 		SD_INFO(SD_LOG_SDTEST, un,
29643 		    "sd_faultinjection_ioctl: Injecting Fault Insert UN\n");
29644 
29645 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
29646 
29647 		sd_fault_injection_on = 0;
29648 
29649 		if (un->sd_fi_fifo_un[i] != NULL) {
29650 			kmem_free(un->sd_fi_fifo_un[i],
29651 			    sizeof (struct sd_fi_un));
29652 			un->sd_fi_fifo_un[i] = NULL;
29653 		}
29654 		if (arg != NULL) {
29655 			un->sd_fi_fifo_un[i] =
29656 			    kmem_alloc(sizeof (struct sd_fi_un), KM_NOSLEEP);
29657 			if (un->sd_fi_fifo_un[i] == NULL) {
29658 				/* Alloc failed don't store anything */
29659 				break;
29660 			}
29661 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_un[i],
29662 			    sizeof (struct sd_fi_un), 0);
29663 			if (rval == -1) {
29664 				kmem_free(un->sd_fi_fifo_un[i],
29665 				    sizeof (struct sd_fi_un));
29666 				un->sd_fi_fifo_un[i] = NULL;
29667 			}
29668 
29669 		} else {
29670 			SD_INFO(SD_LOG_IOERR, un,
29671 			    "sd_faultinjection_ioctl: un null\n");
29672 		}
29673 
29674 		break;
29675 
29676 	case SDIOCINSERTARQ:
29677 		/* Store a arq struct to be pushed onto fifo */
29678 		SD_INFO(SD_LOG_SDTEST, un,
29679 		    "sd_faultinjection_ioctl: Injecting Fault Insert ARQ\n");
29680 		i = un->sd_fi_fifo_end % SD_FI_MAX_ERROR;
29681 
29682 		sd_fault_injection_on = 0;
29683 
29684 		if (un->sd_fi_fifo_arq[i] != NULL) {
29685 			kmem_free(un->sd_fi_fifo_arq[i],
29686 			    sizeof (struct sd_fi_arq));
29687 			un->sd_fi_fifo_arq[i] = NULL;
29688 		}
29689 		if (arg != NULL) {
29690 			un->sd_fi_fifo_arq[i] =
29691 			    kmem_alloc(sizeof (struct sd_fi_arq), KM_NOSLEEP);
29692 			if (un->sd_fi_fifo_arq[i] == NULL) {
29693 				/* Alloc failed don't store anything */
29694 				break;
29695 			}
29696 			rval = ddi_copyin((void *)arg, un->sd_fi_fifo_arq[i],
29697 			    sizeof (struct sd_fi_arq), 0);
29698 			if (rval == -1) {
29699 				kmem_free(un->sd_fi_fifo_arq[i],
29700 				    sizeof (struct sd_fi_arq));
29701 				un->sd_fi_fifo_arq[i] = NULL;
29702 			}
29703 
29704 		} else {
29705 			SD_INFO(SD_LOG_IOERR, un,
29706 			    "sd_faultinjection_ioctl: arq null\n");
29707 		}
29708 
29709 		break;
29710 
29711 	case SDIOCPUSH:
29712 		/* Push stored xb, pkt, un, and arq onto fifo */
29713 		sd_fault_injection_on = 0;
29714 
29715 		if (arg != NULL) {
29716 			rval = ddi_copyin((void *)arg, &i, sizeof (uint_t), 0);
29717 			if (rval != -1 &&
29718 			    un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
29719 				un->sd_fi_fifo_end += i;
29720 			}
29721 		} else {
29722 			SD_INFO(SD_LOG_IOERR, un,
29723 			    "sd_faultinjection_ioctl: push arg null\n");
29724 			if (un->sd_fi_fifo_end + i < SD_FI_MAX_ERROR) {
29725 				un->sd_fi_fifo_end++;
29726 			}
29727 		}
29728 		SD_INFO(SD_LOG_IOERR, un,
29729 		    "sd_faultinjection_ioctl: push to end=%d\n",
29730 		    un->sd_fi_fifo_end);
29731 		break;
29732 
29733 	case SDIOCRETRIEVE:
29734 		/* Return buffer of log from Injection session */
29735 		SD_INFO(SD_LOG_SDTEST, un,
29736 		    "sd_faultinjection_ioctl: Injecting Fault Retreive");
29737 
29738 		sd_fault_injection_on = 0;
29739 
29740 		mutex_enter(&(un->un_fi_mutex));
29741 		rval = ddi_copyout(un->sd_fi_log, (void *)arg,
29742 		    un->sd_fi_buf_len+1, 0);
29743 		mutex_exit(&(un->un_fi_mutex));
29744 
29745 		if (rval == -1) {
29746 			/*
29747 			 * arg is possibly invalid setting
29748 			 * it to NULL for return
29749 			 */
29750 			arg = NULL;
29751 		}
29752 		break;
29753 	}
29754 
29755 	mutex_exit(SD_MUTEX(un));
29756 	SD_TRACE(SD_LOG_IOERR, un, "sd_faultinjection_ioctl:"
29757 			    " exit\n");
29758 }
29759 
29760 
29761 /*
29762  *    Function: sd_injection_log()
29763  *
29764  * Description: This routine adds buff to the already existing injection log
29765  *              for retrieval via faultinjection_ioctl for use in fault
29766  *              detection and recovery
29767  *
29768  *   Arguments: buf - the string to add to the log
29769  */
29770 
29771 static void
29772 sd_injection_log(char *buf, struct sd_lun *un)
29773 {
29774 	uint_t len;
29775 
29776 	ASSERT(un != NULL);
29777 	ASSERT(buf != NULL);
29778 
29779 	mutex_enter(&(un->un_fi_mutex));
29780 
29781 	len = min(strlen(buf), 255);
29782 	/* Add logged value to Injection log to be returned later */
29783 	if (len + un->sd_fi_buf_len < SD_FI_MAX_BUF) {
29784 		uint_t	offset = strlen((char *)un->sd_fi_log);
29785 		char *destp = (char *)un->sd_fi_log + offset;
29786 		int i;
29787 		for (i = 0; i < len; i++) {
29788 			*destp++ = *buf++;
29789 		}
29790 		un->sd_fi_buf_len += len;
29791 		un->sd_fi_log[un->sd_fi_buf_len] = '\0';
29792 	}
29793 
29794 	mutex_exit(&(un->un_fi_mutex));
29795 }
29796 
29797 
29798 /*
29799  *    Function: sd_faultinjection()
29800  *
29801  * Description: This routine takes the pkt and changes its
29802  *		content based on error injection scenerio.
29803  *
29804  *   Arguments: pktp	- packet to be changed
29805  */
29806 
29807 static void
29808 sd_faultinjection(struct scsi_pkt *pktp)
29809 {
29810 	uint_t i;
29811 	struct sd_fi_pkt *fi_pkt;
29812 	struct sd_fi_xb *fi_xb;
29813 	struct sd_fi_un *fi_un;
29814 	struct sd_fi_arq *fi_arq;
29815 	struct buf *bp;
29816 	struct sd_xbuf *xb;
29817 	struct sd_lun *un;
29818 
29819 	ASSERT(pktp != NULL);
29820 
29821 	/* pull bp xb and un from pktp */
29822 	bp = (struct buf *)pktp->pkt_private;
29823 	xb = SD_GET_XBUF(bp);
29824 	un = SD_GET_UN(bp);
29825 
29826 	ASSERT(un != NULL);
29827 
29828 	mutex_enter(SD_MUTEX(un));
29829 
29830 	SD_TRACE(SD_LOG_SDTEST, un,
29831 	    "sd_faultinjection: entry Injection from sdintr\n");
29832 
29833 	/* if injection is off return */
29834 	if (sd_fault_injection_on == 0 ||
29835 		un->sd_fi_fifo_start == un->sd_fi_fifo_end) {
29836 		mutex_exit(SD_MUTEX(un));
29837 		return;
29838 	}
29839 
29840 
29841 	/* take next set off fifo */
29842 	i = un->sd_fi_fifo_start % SD_FI_MAX_ERROR;
29843 
29844 	fi_pkt = un->sd_fi_fifo_pkt[i];
29845 	fi_xb = un->sd_fi_fifo_xb[i];
29846 	fi_un = un->sd_fi_fifo_un[i];
29847 	fi_arq = un->sd_fi_fifo_arq[i];
29848 
29849 
29850 	/* set variables accordingly */
29851 	/* set pkt if it was on fifo */
29852 	if (fi_pkt != NULL) {
29853 		SD_CONDSET(pktp, pkt, pkt_flags, "pkt_flags");
29854 		SD_CONDSET(*pktp, pkt, pkt_scbp, "pkt_scbp");
29855 		SD_CONDSET(*pktp, pkt, pkt_cdbp, "pkt_cdbp");
29856 		SD_CONDSET(pktp, pkt, pkt_state, "pkt_state");
29857 		SD_CONDSET(pktp, pkt, pkt_statistics, "pkt_statistics");
29858 		SD_CONDSET(pktp, pkt, pkt_reason, "pkt_reason");
29859 
29860 	}
29861 
29862 	/* set xb if it was on fifo */
29863 	if (fi_xb != NULL) {
29864 		SD_CONDSET(xb, xb, xb_blkno, "xb_blkno");
29865 		SD_CONDSET(xb, xb, xb_dma_resid, "xb_dma_resid");
29866 		SD_CONDSET(xb, xb, xb_retry_count, "xb_retry_count");
29867 		SD_CONDSET(xb, xb, xb_victim_retry_count,
29868 		    "xb_victim_retry_count");
29869 		SD_CONDSET(xb, xb, xb_sense_status, "xb_sense_status");
29870 		SD_CONDSET(xb, xb, xb_sense_state, "xb_sense_state");
29871 		SD_CONDSET(xb, xb, xb_sense_resid, "xb_sense_resid");
29872 
29873 		/* copy in block data from sense */
29874 		if (fi_xb->xb_sense_data[0] != -1) {
29875 			bcopy(fi_xb->xb_sense_data, xb->xb_sense_data,
29876 			    SENSE_LENGTH);
29877 		}
29878 
29879 		/* copy in extended sense codes */
29880 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_code,
29881 		    "es_code");
29882 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_key,
29883 		    "es_key");
29884 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb, es_add_code,
29885 		    "es_add_code");
29886 		SD_CONDSET(((struct scsi_extended_sense *)xb), xb,
29887 		    es_qual_code, "es_qual_code");
29888 	}
29889 
29890 	/* set un if it was on fifo */
29891 	if (fi_un != NULL) {
29892 		SD_CONDSET(un->un_sd->sd_inq, un, inq_rmb, "inq_rmb");
29893 		SD_CONDSET(un, un, un_ctype, "un_ctype");
29894 		SD_CONDSET(un, un, un_reset_retry_count,
29895 		    "un_reset_retry_count");
29896 		SD_CONDSET(un, un, un_reservation_type, "un_reservation_type");
29897 		SD_CONDSET(un, un, un_resvd_status, "un_resvd_status");
29898 		SD_CONDSET(un, un, un_f_arq_enabled, "un_f_arq_enabled");
29899 		SD_CONDSET(un, un, un_f_geometry_is_valid,
29900 		    "un_f_geometry_is_valid");
29901 		SD_CONDSET(un, un, un_f_allow_bus_device_reset,
29902 		    "un_f_allow_bus_device_reset");
29903 		SD_CONDSET(un, un, un_f_opt_queueing, "un_f_opt_queueing");
29904 
29905 	}
29906 
29907 	/* copy in auto request sense if it was on fifo */
29908 	if (fi_arq != NULL) {
29909 		bcopy(fi_arq, pktp->pkt_scbp, sizeof (struct sd_fi_arq));
29910 	}
29911 
29912 	/* free structs */
29913 	if (un->sd_fi_fifo_pkt[i] != NULL) {
29914 		kmem_free(un->sd_fi_fifo_pkt[i], sizeof (struct sd_fi_pkt));
29915 	}
29916 	if (un->sd_fi_fifo_xb[i] != NULL) {
29917 		kmem_free(un->sd_fi_fifo_xb[i], sizeof (struct sd_fi_xb));
29918 	}
29919 	if (un->sd_fi_fifo_un[i] != NULL) {
29920 		kmem_free(un->sd_fi_fifo_un[i], sizeof (struct sd_fi_un));
29921 	}
29922 	if (un->sd_fi_fifo_arq[i] != NULL) {
29923 		kmem_free(un->sd_fi_fifo_arq[i], sizeof (struct sd_fi_arq));
29924 	}
29925 
29926 	/*
29927 	 * kmem_free does not gurantee to set to NULL
29928 	 * since we uses these to determine if we set
29929 	 * values or not lets confirm they are always
29930 	 * NULL after free
29931 	 */
29932 	un->sd_fi_fifo_pkt[i] = NULL;
29933 	un->sd_fi_fifo_un[i] = NULL;
29934 	un->sd_fi_fifo_xb[i] = NULL;
29935 	un->sd_fi_fifo_arq[i] = NULL;
29936 
29937 	un->sd_fi_fifo_start++;
29938 
29939 	mutex_exit(SD_MUTEX(un));
29940 
29941 	SD_TRACE(SD_LOG_SDTEST, un, "sd_faultinjection: exit\n");
29942 }
29943 
29944 #endif /* SD_FAULT_INJECTION */
29945