1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright (c) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
24 */
25/*
26 * Copyright (c) 2011 Bayard G. Bell.  All rights reserved.
27 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28 * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
29 * Copyright 2016 Joyent, Inc.
30 * Copyright 2017 Nexenta Systems, Inc.
31 */
32/*
33 * Copyright 2011 cyril.galibern@opensvc.com
34 */
35
36/*
37 * SCSI disk target driver.
38 */
39#include <sys/scsi/scsi.h>
40#include <sys/dkbad.h>
41#include <sys/dklabel.h>
42#include <sys/dkio.h>
43#include <sys/fdio.h>
44#include <sys/cdio.h>
45#include <sys/mhd.h>
46#include <sys/vtoc.h>
47#include <sys/dktp/fdisk.h>
48#include <sys/kstat.h>
49#include <sys/vtrace.h>
50#include <sys/note.h>
51#include <sys/thread.h>
52#include <sys/proc.h>
53#include <sys/efi_partition.h>
54#include <sys/var.h>
55#include <sys/aio_req.h>
56#include <sys/dkioc_free_util.h>
57
58#ifdef __lock_lint
59#define	_LP64
60#define	__amd64
61#endif
62
63#if (defined(__fibre))
64/* Note: is there a leadville version of the following? */
65#include <sys/fc4/fcal_linkapp.h>
66#endif
67#include <sys/taskq.h>
68#include <sys/uuid.h>
69#include <sys/byteorder.h>
70#include <sys/sdt.h>
71
72#include "sd_xbuf.h"
73
74#include <sys/scsi/targets/sddef.h>
75#include <sys/cmlb.h>
76#include <sys/sysevent/eventdefs.h>
77#include <sys/sysevent/dev.h>
78
79#include <sys/fm/protocol.h>
80
81/*
82 * Loadable module info.
83 */
84#if (defined(__fibre))
85#define	SD_MODULE_NAME	"SCSI SSA/FCAL Disk Driver"
86#else /* !__fibre */
87#define	SD_MODULE_NAME	"SCSI Disk Driver"
88#endif /* !__fibre */
89
90/*
91 * Define the interconnect type, to allow the driver to distinguish
92 * between parallel SCSI (sd) and fibre channel (ssd) behaviors.
93 *
94 * This is really for backward compatibility. In the future, the driver
95 * should actually check the "interconnect-type" property as reported by
96 * the HBA; however at present this property is not defined by all HBAs,
97 * so we will use this #define (1) to permit the driver to run in
98 * backward-compatibility mode; and (2) to print a notification message
99 * if an FC HBA does not support the "interconnect-type" property.  The
100 * behavior of the driver will be to assume parallel SCSI behaviors unless
101 * the "interconnect-type" property is defined by the HBA **AND** has a
102 * value of either INTERCONNECT_FIBRE, INTERCONNECT_SSA, or
103 * INTERCONNECT_FABRIC, in which case the driver will assume Fibre
104 * Channel behaviors (as per the old ssd).  (Note that the
105 * INTERCONNECT_1394 and INTERCONNECT_USB types are not supported and
106 * will result in the driver assuming parallel SCSI behaviors.)
107 *
108 * (see common/sys/scsi/impl/services.h)
109 *
110 * Note: For ssd semantics, don't use INTERCONNECT_FABRIC as the default
111 * since some FC HBAs may already support that, and there is some code in
112 * the driver that already looks for it.  Using INTERCONNECT_FABRIC as the
113 * default would confuse that code, and besides things should work fine
114 * anyways if the FC HBA already reports INTERCONNECT_FABRIC for the
115 * "interconnect_type" property.
116 *
117 */
118#if (defined(__fibre))
119#define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_FIBRE
120#else
121#define	SD_DEFAULT_INTERCONNECT_TYPE	SD_INTERCONNECT_PARALLEL
122#endif
123
124/*
125 * The name of the driver, established from the module name in _init.
126 */
127static	char *sd_label			= NULL;
128
129/*
130 * Driver name is unfortunately prefixed on some driver.conf properties.
131 */
132#if (defined(__fibre))
133#define	sd_max_xfer_size		ssd_max_xfer_size
134#define	sd_config_list			ssd_config_list
135static	char *sd_max_xfer_size		= "ssd_max_xfer_size";
136static	char *sd_config_list		= "ssd-config-list";
137#else
138static	char *sd_max_xfer_size		= "sd_max_xfer_size";
139static	char *sd_config_list		= "sd-config-list";
140#endif
141
142/*
143 * Driver global variables
144 */
145
146#if (defined(__fibre))
147/*
148 * These #defines are to avoid namespace collisions that occur because this
149 * code is currently used to compile two separate driver modules: sd and ssd.
150 * All global variables need to be treated this way (even if declared static)
151 * in order to allow the debugger to resolve the names properly.
152 * It is anticipated that in the near future the ssd module will be obsoleted,
153 * at which time this namespace issue should go away.
154 */
155#define	sd_state			ssd_state
156#define	sd_io_time			ssd_io_time
157#define	sd_failfast_enable		ssd_failfast_enable
158#define	sd_ua_retry_count		ssd_ua_retry_count
159#define	sd_report_pfa			ssd_report_pfa
160#define	sd_max_throttle			ssd_max_throttle
161#define	sd_min_throttle			ssd_min_throttle
162#define	sd_rot_delay			ssd_rot_delay
163
164#define	sd_retry_on_reservation_conflict	\
165					ssd_retry_on_reservation_conflict
166#define	sd_reinstate_resv_delay		ssd_reinstate_resv_delay
167#define	sd_resv_conflict_name		ssd_resv_conflict_name
168
169#define	sd_component_mask		ssd_component_mask
170#define	sd_level_mask			ssd_level_mask
171#define	sd_debug_un			ssd_debug_un
172#define	sd_error_level			ssd_error_level
173
174#define	sd_xbuf_active_limit		ssd_xbuf_active_limit
175#define	sd_xbuf_reserve_limit		ssd_xbuf_reserve_limit
176
177#define	sd_tr				ssd_tr
178#define	sd_reset_throttle_timeout	ssd_reset_throttle_timeout
179#define	sd_qfull_throttle_timeout	ssd_qfull_throttle_timeout
180#define	sd_qfull_throttle_enable	ssd_qfull_throttle_enable
181#define	sd_check_media_time		ssd_check_media_time
182#define	sd_wait_cmds_complete		ssd_wait_cmds_complete
183#define	sd_label_mutex			ssd_label_mutex
184#define	sd_detach_mutex			ssd_detach_mutex
185#define	sd_log_buf			ssd_log_buf
186#define	sd_log_mutex			ssd_log_mutex
187
188#define	sd_disk_table			ssd_disk_table
189#define	sd_disk_table_size		ssd_disk_table_size
190#define	sd_sense_mutex			ssd_sense_mutex
191#define	sd_cdbtab			ssd_cdbtab
192
193#define	sd_cb_ops			ssd_cb_ops
194#define	sd_ops				ssd_ops
195#define	sd_additional_codes		ssd_additional_codes
196#define	sd_tgops			ssd_tgops
197
198#define	sd_minor_data			ssd_minor_data
199#define	sd_minor_data_efi		ssd_minor_data_efi
200
201#define	sd_tq				ssd_tq
202#define	sd_wmr_tq			ssd_wmr_tq
203#define	sd_taskq_name			ssd_taskq_name
204#define	sd_wmr_taskq_name		ssd_wmr_taskq_name
205#define	sd_taskq_minalloc		ssd_taskq_minalloc
206#define	sd_taskq_maxalloc		ssd_taskq_maxalloc
207
208#define	sd_dump_format_string		ssd_dump_format_string
209
210#define	sd_iostart_chain		ssd_iostart_chain
211#define	sd_iodone_chain			ssd_iodone_chain
212
213#define	sd_pm_idletime			ssd_pm_idletime
214
215#define	sd_force_pm_supported		ssd_force_pm_supported
216
217#define	sd_dtype_optical_bind		ssd_dtype_optical_bind
218
219#define	sd_ssc_init			ssd_ssc_init
220#define	sd_ssc_send			ssd_ssc_send
221#define	sd_ssc_fini			ssd_ssc_fini
222#define	sd_ssc_assessment		ssd_ssc_assessment
223#define	sd_ssc_post			ssd_ssc_post
224#define	sd_ssc_print			ssd_ssc_print
225#define	sd_ssc_ereport_post		ssd_ssc_ereport_post
226#define	sd_ssc_set_info			ssd_ssc_set_info
227#define	sd_ssc_extract_info		ssd_ssc_extract_info
228
229#endif
230
231#ifdef	SDDEBUG
232int	sd_force_pm_supported		= 0;
233#endif	/* SDDEBUG */
234
235void *sd_state				= NULL;
236int sd_io_time				= SD_IO_TIME;
237int sd_failfast_enable			= 1;
238int sd_ua_retry_count			= SD_UA_RETRY_COUNT;
239int sd_report_pfa			= 1;
240int sd_max_throttle			= SD_MAX_THROTTLE;
241int sd_min_throttle			= SD_MIN_THROTTLE;
242int sd_rot_delay			= 4; /* Default 4ms Rotation delay */
243int sd_qfull_throttle_enable		= TRUE;
244
245int sd_retry_on_reservation_conflict	= 1;
246int sd_reinstate_resv_delay		= SD_REINSTATE_RESV_DELAY;
247_NOTE(SCHEME_PROTECTS_DATA("safe sharing", sd_reinstate_resv_delay))
248
249static int sd_dtype_optical_bind	= -1;
250
251/* Note: the following is not a bug, it really is "sd_" and not "ssd_" */
252static	char *sd_resv_conflict_name	= "sd_retry_on_reservation_conflict";
253
254/*
255 * Global data for debug logging. To enable debug printing, sd_component_mask
256 * and sd_level_mask should be set to the desired bit patterns as outlined in
257 * sddef.h.
258 */
259uint_t	sd_component_mask		= 0x0;
260uint_t	sd_level_mask			= 0x0;
261struct	sd_lun *sd_debug_un		= NULL;
262uint_t	sd_error_level			= SCSI_ERR_RETRYABLE;
263
264/* Note: these may go away in the future... */
265static uint32_t	sd_xbuf_active_limit	= 512;
266static uint32_t sd_xbuf_reserve_limit	= 16;
267
268static struct sd_resv_reclaim_request	sd_tr = { NULL, NULL, NULL, 0, 0, 0 };
269
270/*
271 * Timer value used to reset the throttle after it has been reduced
272 * (typically in response to TRAN_BUSY or STATUS_QFULL)
273 */
274static int sd_reset_throttle_timeout	= SD_RESET_THROTTLE_TIMEOUT;
275static int sd_qfull_throttle_timeout	= SD_QFULL_THROTTLE_TIMEOUT;
276
277/*
278 * Interval value associated with the media change scsi watch.
279 */
280static int sd_check_media_time		= 3000000;
281
282/*
283 * Wait value used for in progress operations during a DDI_SUSPEND
284 */
285static int sd_wait_cmds_complete	= SD_WAIT_CMDS_COMPLETE;
286
287/*
288 * sd_label_mutex protects a static buffer used in the disk label
289 * component of the driver
290 */
291static kmutex_t sd_label_mutex;
292
293/*
294 * sd_detach_mutex protects un_layer_count, un_detach_count, and
295 * un_opens_in_progress in the sd_lun structure.
296 */
297static kmutex_t sd_detach_mutex;
298
299_NOTE(MUTEX_PROTECTS_DATA(sd_detach_mutex,
300	sd_lun::{un_layer_count un_detach_count un_opens_in_progress}))
301
302/*
303 * Global buffer and mutex for debug logging
304 */
305static char	sd_log_buf[1024];
306static kmutex_t	sd_log_mutex;
307
308/*
309 * Structs and globals for recording attached lun information.
310 * This maintains a chain. Each node in the chain represents a SCSI controller.
311 * The structure records the number of luns attached to each target connected
312 * with the controller.
313 * For parallel scsi device only.
314 */
315struct sd_scsi_hba_tgt_lun {
316	struct sd_scsi_hba_tgt_lun	*next;
317	dev_info_t			*pdip;
318	int				nlun[NTARGETS_WIDE];
319};
320
321/*
322 * Flag to indicate the lun is attached or detached
323 */
324#define	SD_SCSI_LUN_ATTACH	0
325#define	SD_SCSI_LUN_DETACH	1
326
327static kmutex_t	sd_scsi_target_lun_mutex;
328static struct sd_scsi_hba_tgt_lun	*sd_scsi_target_lun_head = NULL;
329
330_NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
331    sd_scsi_hba_tgt_lun::next sd_scsi_hba_tgt_lun::pdip))
332
333_NOTE(MUTEX_PROTECTS_DATA(sd_scsi_target_lun_mutex,
334    sd_scsi_target_lun_head))
335
336/*
337 * "Smart" Probe Caching structs, globals, #defines, etc.
338 * For parallel scsi and non-self-identify device only.
339 */
340
341/*
342 * The following resources and routines are implemented to support
343 * "smart" probing, which caches the scsi_probe() results in an array,
344 * in order to help avoid long probe times.
345 */
346struct sd_scsi_probe_cache {
347	struct	sd_scsi_probe_cache	*next;
348	dev_info_t	*pdip;
349	int		cache[NTARGETS_WIDE];
350};
351
352static kmutex_t	sd_scsi_probe_cache_mutex;
353static struct	sd_scsi_probe_cache *sd_scsi_probe_cache_head = NULL;
354
355/*
356 * Really we only need protection on the head of the linked list, but
357 * better safe than sorry.
358 */
359_NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
360    sd_scsi_probe_cache::next sd_scsi_probe_cache::pdip))
361
362_NOTE(MUTEX_PROTECTS_DATA(sd_scsi_probe_cache_mutex,
363    sd_scsi_probe_cache_head))
364
365/*
366 * Power attribute table
367 */
368static sd_power_attr_ss sd_pwr_ss = {
369	{ "NAME=spindle-motor", "0=off", "1=on", NULL },
370	{0, 100},
371	{30, 0},
372	{20000, 0}
373};
374
375static sd_power_attr_pc sd_pwr_pc = {
376	{ "NAME=spindle-motor", "0=stopped", "1=standby", "2=idle",
377		"3=active", NULL },
378	{0, 0, 0, 100},
379	{90, 90, 20, 0},
380	{15000, 15000, 1000, 0}
381};
382
383/*
384 * Power level to power condition
385 */
386static int sd_pl2pc[] = {
387	SD_TARGET_START_VALID,
388	SD_TARGET_STANDBY,
389	SD_TARGET_IDLE,
390	SD_TARGET_ACTIVE
391};
392
393/*
394 * Vendor specific data name property declarations
395 */
396
397#if defined(__fibre) || defined(__i386) ||defined(__amd64)
398
399static sd_tunables seagate_properties = {
400	SEAGATE_THROTTLE_VALUE,
401	0,
402	0,
403	0,
404	0,
405	0,
406	0,
407	0,
408	0
409};
410
411
412static sd_tunables fujitsu_properties = {
413	FUJITSU_THROTTLE_VALUE,
414	0,
415	0,
416	0,
417	0,
418	0,
419	0,
420	0,
421	0
422};
423
424static sd_tunables ibm_properties = {
425	IBM_THROTTLE_VALUE,
426	0,
427	0,
428	0,
429	0,
430	0,
431	0,
432	0,
433	0
434};
435
436static sd_tunables purple_properties = {
437	PURPLE_THROTTLE_VALUE,
438	0,
439	0,
440	PURPLE_BUSY_RETRIES,
441	PURPLE_RESET_RETRY_COUNT,
442	PURPLE_RESERVE_RELEASE_TIME,
443	0,
444	0,
445	0
446};
447
448static sd_tunables sve_properties = {
449	SVE_THROTTLE_VALUE,
450	0,
451	0,
452	SVE_BUSY_RETRIES,
453	SVE_RESET_RETRY_COUNT,
454	SVE_RESERVE_RELEASE_TIME,
455	SVE_MIN_THROTTLE_VALUE,
456	SVE_DISKSORT_DISABLED_FLAG,
457	0
458};
459
460static sd_tunables maserati_properties = {
461	0,
462	0,
463	0,
464	0,
465	0,
466	0,
467	0,
468	MASERATI_DISKSORT_DISABLED_FLAG,
469	MASERATI_LUN_RESET_ENABLED_FLAG
470};
471
472static sd_tunables pirus_properties = {
473	PIRUS_THROTTLE_VALUE,
474	0,
475	PIRUS_NRR_COUNT,
476	PIRUS_BUSY_RETRIES,
477	PIRUS_RESET_RETRY_COUNT,
478	0,
479	PIRUS_MIN_THROTTLE_VALUE,
480	PIRUS_DISKSORT_DISABLED_FLAG,
481	PIRUS_LUN_RESET_ENABLED_FLAG
482};
483
484#endif
485
486#if (defined(__sparc) && !defined(__fibre)) || \
487	(defined(__i386) || defined(__amd64))
488
489
490static sd_tunables elite_properties = {
491	ELITE_THROTTLE_VALUE,
492	0,
493	0,
494	0,
495	0,
496	0,
497	0,
498	0,
499	0
500};
501
502static sd_tunables st31200n_properties = {
503	ST31200N_THROTTLE_VALUE,
504	0,
505	0,
506	0,
507	0,
508	0,
509	0,
510	0,
511	0
512};
513
514#endif /* Fibre or not */
515
516static sd_tunables lsi_properties_scsi = {
517	LSI_THROTTLE_VALUE,
518	0,
519	LSI_NOTREADY_RETRIES,
520	0,
521	0,
522	0,
523	0,
524	0,
525	0
526};
527
528static sd_tunables symbios_properties = {
529	SYMBIOS_THROTTLE_VALUE,
530	0,
531	SYMBIOS_NOTREADY_RETRIES,
532	0,
533	0,
534	0,
535	0,
536	0,
537	0
538};
539
540static sd_tunables lsi_properties = {
541	0,
542	0,
543	LSI_NOTREADY_RETRIES,
544	0,
545	0,
546	0,
547	0,
548	0,
549	0
550};
551
552static sd_tunables lsi_oem_properties = {
553	0,
554	0,
555	LSI_OEM_NOTREADY_RETRIES,
556	0,
557	0,
558	0,
559	0,
560	0,
561	0,
562	1
563};
564
565
566
567#if (defined(SD_PROP_TST))
568
569#define	SD_TST_CTYPE_VAL	CTYPE_CDROM
570#define	SD_TST_THROTTLE_VAL	16
571#define	SD_TST_NOTREADY_VAL	12
572#define	SD_TST_BUSY_VAL		60
573#define	SD_TST_RST_RETRY_VAL	36
574#define	SD_TST_RSV_REL_TIME	60
575
576static sd_tunables tst_properties = {
577	SD_TST_THROTTLE_VAL,
578	SD_TST_CTYPE_VAL,
579	SD_TST_NOTREADY_VAL,
580	SD_TST_BUSY_VAL,
581	SD_TST_RST_RETRY_VAL,
582	SD_TST_RSV_REL_TIME,
583	0,
584	0,
585	0
586};
587#endif
588
589/* This is similar to the ANSI toupper implementation */
590#define	SD_TOUPPER(C)	(((C) >= 'a' && (C) <= 'z') ? (C) - 'a' + 'A' : (C))
591
592/*
593 * Static Driver Configuration Table
594 *
595 * This is the table of disks which need throttle adjustment (or, perhaps
596 * something else as defined by the flags at a future time.)  device_id
597 * is a string consisting of concatenated vid (vendor), pid (product/model)
598 * and revision strings as defined in the scsi_inquiry structure.  Offsets of
599 * the parts of the string are as defined by the sizes in the scsi_inquiry
600 * structure.  Device type is searched as far as the device_id string is
601 * defined.  Flags defines which values are to be set in the driver from the
602 * properties list.
603 *
604 * Entries below which begin and end with a "*" are a special case.
605 * These do not have a specific vendor, and the string which follows
606 * can appear anywhere in the 16 byte PID portion of the inquiry data.
607 *
608 * Entries below which begin and end with a " " (blank) are a special
609 * case. The comparison function will treat multiple consecutive blanks
610 * as equivalent to a single blank. For example, this causes a
611 * sd_disk_table entry of " NEC CDROM " to match a device's id string
612 * of  "NEC       CDROM".
613 *
614 * Note: The MD21 controller type has been obsoleted.
615 *	 ST318202F is a Legacy device
616 *	 MAM3182FC, MAM3364FC, MAM3738FC do not appear to have ever been
617 *	 made with an FC connection. The entries here are a legacy.
618 */
619static sd_disk_config_t sd_disk_table[] = {
620#if defined(__fibre) || defined(__i386) || defined(__amd64)
621	{ "SEAGATE ST34371FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
622	{ "SEAGATE ST19171FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
623	{ "SEAGATE ST39102FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
624	{ "SEAGATE ST39103FC", SD_CONF_BSET_THROTTLE, &seagate_properties },
625	{ "SEAGATE ST118273F", SD_CONF_BSET_THROTTLE, &seagate_properties },
626	{ "SEAGATE ST318202F", SD_CONF_BSET_THROTTLE, &seagate_properties },
627	{ "SEAGATE ST318203F", SD_CONF_BSET_THROTTLE, &seagate_properties },
628	{ "SEAGATE ST136403F", SD_CONF_BSET_THROTTLE, &seagate_properties },
629	{ "SEAGATE ST318304F", SD_CONF_BSET_THROTTLE, &seagate_properties },
630	{ "SEAGATE ST336704F", SD_CONF_BSET_THROTTLE, &seagate_properties },
631	{ "SEAGATE ST373405F", SD_CONF_BSET_THROTTLE, &seagate_properties },
632	{ "SEAGATE ST336605F", SD_CONF_BSET_THROTTLE, &seagate_properties },
633	{ "SEAGATE ST336752F", SD_CONF_BSET_THROTTLE, &seagate_properties },
634	{ "SEAGATE ST318452F", SD_CONF_BSET_THROTTLE, &seagate_properties },
635	{ "FUJITSU MAG3091F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
636	{ "FUJITSU MAG3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
637	{ "FUJITSU MAA3182F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
638	{ "FUJITSU MAF3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
639	{ "FUJITSU MAL3364F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
640	{ "FUJITSU MAL3738F",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
641	{ "FUJITSU MAM3182FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
642	{ "FUJITSU MAM3364FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
643	{ "FUJITSU MAM3738FC",  SD_CONF_BSET_THROTTLE, &fujitsu_properties },
644	{ "IBM     DDYFT1835",  SD_CONF_BSET_THROTTLE, &ibm_properties },
645	{ "IBM     DDYFT3695",  SD_CONF_BSET_THROTTLE, &ibm_properties },
646	{ "IBM     IC35LF2D2",  SD_CONF_BSET_THROTTLE, &ibm_properties },
647	{ "IBM     IC35LF2PR",  SD_CONF_BSET_THROTTLE, &ibm_properties },
648	{ "IBM     1724-100",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
649	{ "IBM     1726-2xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
650	{ "IBM     1726-22x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
651	{ "IBM     1726-4xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
652	{ "IBM     1726-42x",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
653	{ "IBM     1726-3xx",   SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
654	{ "IBM     3526",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
655	{ "IBM     3542",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
656	{ "IBM     3552",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
657	{ "IBM     1722",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
658	{ "IBM     1742",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
659	{ "IBM     1815",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
660	{ "IBM     FAStT",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
661	{ "IBM     1814",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
662	{ "IBM     1814-200",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
663	{ "IBM     1818",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
664	{ "DELL    MD3000",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
665	{ "DELL    MD3000i",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
666	{ "LSI     INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
667	{ "ENGENIO INF",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
668	{ "SGI     TP",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
669	{ "SGI     IS",		SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
670	{ "*CSM100_*",		SD_CONF_BSET_NRR_COUNT |
671			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
672	{ "*CSM200_*",		SD_CONF_BSET_NRR_COUNT |
673			SD_CONF_BSET_CACHE_IS_NV, &lsi_oem_properties },
674	{ "Fujitsu SX300",	SD_CONF_BSET_THROTTLE,  &lsi_oem_properties },
675	{ "LSI",		SD_CONF_BSET_NRR_COUNT, &lsi_properties },
676	{ "SUN     T3", SD_CONF_BSET_THROTTLE |
677			SD_CONF_BSET_BSY_RETRY_COUNT|
678			SD_CONF_BSET_RST_RETRIES|
679			SD_CONF_BSET_RSV_REL_TIME,
680		&purple_properties },
681	{ "SUN     SESS01", SD_CONF_BSET_THROTTLE |
682		SD_CONF_BSET_BSY_RETRY_COUNT|
683		SD_CONF_BSET_RST_RETRIES|
684		SD_CONF_BSET_RSV_REL_TIME|
685		SD_CONF_BSET_MIN_THROTTLE|
686		SD_CONF_BSET_DISKSORT_DISABLED,
687		&sve_properties },
688	{ "SUN     T4", SD_CONF_BSET_THROTTLE |
689			SD_CONF_BSET_BSY_RETRY_COUNT|
690			SD_CONF_BSET_RST_RETRIES|
691			SD_CONF_BSET_RSV_REL_TIME,
692		&purple_properties },
693	{ "SUN     SVE01", SD_CONF_BSET_DISKSORT_DISABLED |
694		SD_CONF_BSET_LUN_RESET_ENABLED,
695		&maserati_properties },
696	{ "SUN     SE6920", SD_CONF_BSET_THROTTLE |
697		SD_CONF_BSET_NRR_COUNT|
698		SD_CONF_BSET_BSY_RETRY_COUNT|
699		SD_CONF_BSET_RST_RETRIES|
700		SD_CONF_BSET_MIN_THROTTLE|
701		SD_CONF_BSET_DISKSORT_DISABLED|
702		SD_CONF_BSET_LUN_RESET_ENABLED,
703		&pirus_properties },
704	{ "SUN     SE6940", SD_CONF_BSET_THROTTLE |
705		SD_CONF_BSET_NRR_COUNT|
706		SD_CONF_BSET_BSY_RETRY_COUNT|
707		SD_CONF_BSET_RST_RETRIES|
708		SD_CONF_BSET_MIN_THROTTLE|
709		SD_CONF_BSET_DISKSORT_DISABLED|
710		SD_CONF_BSET_LUN_RESET_ENABLED,
711		&pirus_properties },
712	{ "SUN     StorageTek 6920", SD_CONF_BSET_THROTTLE |
713		SD_CONF_BSET_NRR_COUNT|
714		SD_CONF_BSET_BSY_RETRY_COUNT|
715		SD_CONF_BSET_RST_RETRIES|
716		SD_CONF_BSET_MIN_THROTTLE|
717		SD_CONF_BSET_DISKSORT_DISABLED|
718		SD_CONF_BSET_LUN_RESET_ENABLED,
719		&pirus_properties },
720	{ "SUN     StorageTek 6940", SD_CONF_BSET_THROTTLE |
721		SD_CONF_BSET_NRR_COUNT|
722		SD_CONF_BSET_BSY_RETRY_COUNT|
723		SD_CONF_BSET_RST_RETRIES|
724		SD_CONF_BSET_MIN_THROTTLE|
725		SD_CONF_BSET_DISKSORT_DISABLED|
726		SD_CONF_BSET_LUN_RESET_ENABLED,
727		&pirus_properties },
728	{ "SUN     PSX1000", SD_CONF_BSET_THROTTLE |
729		SD_CONF_BSET_NRR_COUNT|
730		SD_CONF_BSET_BSY_RETRY_COUNT|
731		SD_CONF_BSET_RST_RETRIES|
732		SD_CONF_BSET_MIN_THROTTLE|
733		SD_CONF_BSET_DISKSORT_DISABLED|
734		SD_CONF_BSET_LUN_RESET_ENABLED,
735		&pirus_properties },
736	{ "SUN     SE6330", SD_CONF_BSET_THROTTLE |
737		SD_CONF_BSET_NRR_COUNT|
738		SD_CONF_BSET_BSY_RETRY_COUNT|
739		SD_CONF_BSET_RST_RETRIES|
740		SD_CONF_BSET_MIN_THROTTLE|
741		SD_CONF_BSET_DISKSORT_DISABLED|
742		SD_CONF_BSET_LUN_RESET_ENABLED,
743		&pirus_properties },
744	{ "SUN     STK6580_6780", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
745	{ "SUN     SUN_6180", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
746	{ "STK     OPENstorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
747	{ "STK     OpenStorage", SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
748	{ "STK     BladeCtlr",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
749	{ "STK     FLEXLINE",	SD_CONF_BSET_NRR_COUNT, &lsi_oem_properties },
750	{ "SYMBIOS", SD_CONF_BSET_NRR_COUNT, &symbios_properties },
751#endif /* fibre or NON-sparc platforms */
752#if ((defined(__sparc) && !defined(__fibre)) ||\
753	(defined(__i386) || defined(__amd64)))
754	{ "SEAGATE ST42400N", SD_CONF_BSET_THROTTLE, &elite_properties },
755	{ "SEAGATE ST31200N", SD_CONF_BSET_THROTTLE, &st31200n_properties },
756	{ "SEAGATE ST41600N", SD_CONF_BSET_TUR_CHECK, NULL },
757	{ "CONNER  CP30540",  SD_CONF_BSET_NOCACHE,  NULL },
758	{ "*SUN0104*", SD_CONF_BSET_FAB_DEVID, NULL },
759	{ "*SUN0207*", SD_CONF_BSET_FAB_DEVID, NULL },
760	{ "*SUN0327*", SD_CONF_BSET_FAB_DEVID, NULL },
761	{ "*SUN0340*", SD_CONF_BSET_FAB_DEVID, NULL },
762	{ "*SUN0424*", SD_CONF_BSET_FAB_DEVID, NULL },
763	{ "*SUN0669*", SD_CONF_BSET_FAB_DEVID, NULL },
764	{ "*SUN1.0G*", SD_CONF_BSET_FAB_DEVID, NULL },
765	{ "SYMBIOS INF-01-00       ", SD_CONF_BSET_FAB_DEVID, NULL },
766	{ "SYMBIOS", SD_CONF_BSET_THROTTLE|SD_CONF_BSET_NRR_COUNT,
767	    &symbios_properties },
768	{ "LSI", SD_CONF_BSET_THROTTLE | SD_CONF_BSET_NRR_COUNT,
769	    &lsi_properties_scsi },
770#if defined(__i386) || defined(__amd64)
771	{ " NEC CD-ROM DRIVE:260 ", (SD_CONF_BSET_PLAYMSF_BCD
772				    | SD_CONF_BSET_READSUB_BCD
773				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
774				    | SD_CONF_BSET_NO_READ_HEADER
775				    | SD_CONF_BSET_READ_CD_XD4), NULL },
776
777	{ " NEC CD-ROM DRIVE:270 ", (SD_CONF_BSET_PLAYMSF_BCD
778				    | SD_CONF_BSET_READSUB_BCD
779				    | SD_CONF_BSET_READ_TOC_ADDR_BCD
780				    | SD_CONF_BSET_NO_READ_HEADER
781				    | SD_CONF_BSET_READ_CD_XD4), NULL },
782#endif /* __i386 || __amd64 */
783#endif /* sparc NON-fibre or NON-sparc platforms */
784
785#if (defined(SD_PROP_TST))
786	{ "VENDOR  PRODUCT ", (SD_CONF_BSET_THROTTLE
787				| SD_CONF_BSET_CTYPE
788				| SD_CONF_BSET_NRR_COUNT
789				| SD_CONF_BSET_FAB_DEVID
790				| SD_CONF_BSET_NOCACHE
791				| SD_CONF_BSET_BSY_RETRY_COUNT
792				| SD_CONF_BSET_PLAYMSF_BCD
793				| SD_CONF_BSET_READSUB_BCD
794				| SD_CONF_BSET_READ_TOC_TRK_BCD
795				| SD_CONF_BSET_READ_TOC_ADDR_BCD
796				| SD_CONF_BSET_NO_READ_HEADER
797				| SD_CONF_BSET_READ_CD_XD4
798				| SD_CONF_BSET_RST_RETRIES
799				| SD_CONF_BSET_RSV_REL_TIME
800				| SD_CONF_BSET_TUR_CHECK), &tst_properties},
801#endif
802};
803
804static const int sd_disk_table_size =
805	sizeof (sd_disk_table)/ sizeof (sd_disk_config_t);
806
807/*
808 * Emulation mode disk drive VID/PID table
809 */
810static char sd_flash_dev_table[][25] = {
811	"ATA     MARVELL SD88SA02",
812	"MARVELL SD88SA02",
813	"TOSHIBA THNSNV05",
814};
815
816static const int sd_flash_dev_table_size =
817	sizeof (sd_flash_dev_table) / sizeof (sd_flash_dev_table[0]);
818
819#define	SD_INTERCONNECT_PARALLEL	0
820#define	SD_INTERCONNECT_FABRIC		1
821#define	SD_INTERCONNECT_FIBRE		2
822#define	SD_INTERCONNECT_SSA		3
823#define	SD_INTERCONNECT_SATA		4
824#define	SD_INTERCONNECT_SAS		5
825
826#define	SD_IS_PARALLEL_SCSI(un)		\
827	((un)->un_interconnect_type == SD_INTERCONNECT_PARALLEL)
828#define	SD_IS_SERIAL(un)		\
829	(((un)->un_interconnect_type == SD_INTERCONNECT_SATA) ||\
830	((un)->un_interconnect_type == SD_INTERCONNECT_SAS))
831
832/*
833 * Definitions used by device id registration routines
834 */
835#define	VPD_HEAD_OFFSET		3	/* size of head for vpd page */
836#define	VPD_PAGE_LENGTH		3	/* offset for pge length data */
837#define	VPD_MODE_PAGE		1	/* offset into vpd pg for "page code" */
838
839static kmutex_t sd_sense_mutex = {0};
840
841/*
842 * Macros for updates of the driver state
843 */
844#define	New_state(un, s)        \
845	(un)->un_last_state = (un)->un_state, (un)->un_state = (s)
846#define	Restore_state(un)	\
847	{ uchar_t tmp = (un)->un_last_state; New_state((un), tmp); }
848
849static struct sd_cdbinfo sd_cdbtab[] = {
850	{ CDB_GROUP0, 0x00,	   0x1FFFFF,   0xFF,	    },
851	{ CDB_GROUP1, SCMD_GROUP1, 0xFFFFFFFF, 0xFFFF,	    },
852	{ CDB_GROUP5, SCMD_GROUP5, 0xFFFFFFFF, 0xFFFFFFFF,  },
853	{ CDB_GROUP4, SCMD_GROUP4, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFF, },
854};
855
856/*
857 * Specifies the number of seconds that must have elapsed since the last
858 * cmd. has completed for a device to be declared idle to the PM framework.
859 */
860static int sd_pm_idletime = 1;
861
862/*
863 * Internal function prototypes
864 */
865
866#if (defined(__fibre))
867/*
868 * These #defines are to avoid namespace collisions that occur because this
869 * code is currently used to compile two separate driver modules: sd and ssd.
870 * All function names need to be treated this way (even if declared static)
871 * in order to allow the debugger to resolve the names properly.
872 * It is anticipated that in the near future the ssd module will be obsoleted,
873 * at which time this ugliness should go away.
874 */
875#define	sd_log_trace			ssd_log_trace
876#define	sd_log_info			ssd_log_info
877#define	sd_log_err			ssd_log_err
878#define	sdprobe				ssdprobe
879#define	sdinfo				ssdinfo
880#define	sd_prop_op			ssd_prop_op
881#define	sd_scsi_probe_cache_init	ssd_scsi_probe_cache_init
882#define	sd_scsi_probe_cache_fini	ssd_scsi_probe_cache_fini
883#define	sd_scsi_clear_probe_cache	ssd_scsi_clear_probe_cache
884#define	sd_scsi_probe_with_cache	ssd_scsi_probe_with_cache
885#define	sd_scsi_target_lun_init		ssd_scsi_target_lun_init
886#define	sd_scsi_target_lun_fini		ssd_scsi_target_lun_fini
887#define	sd_scsi_get_target_lun_count	ssd_scsi_get_target_lun_count
888#define	sd_scsi_update_lun_on_target	ssd_scsi_update_lun_on_target
889#define	sd_spin_up_unit			ssd_spin_up_unit
890#define	sd_enable_descr_sense		ssd_enable_descr_sense
891#define	sd_reenable_dsense_task		ssd_reenable_dsense_task
892#define	sd_set_mmc_caps			ssd_set_mmc_caps
893#define	sd_read_unit_properties		ssd_read_unit_properties
894#define	sd_process_sdconf_file		ssd_process_sdconf_file
895#define	sd_process_sdconf_table		ssd_process_sdconf_table
896#define	sd_sdconf_id_match		ssd_sdconf_id_match
897#define	sd_blank_cmp			ssd_blank_cmp
898#define	sd_chk_vers1_data		ssd_chk_vers1_data
899#define	sd_set_vers1_properties		ssd_set_vers1_properties
900#define	sd_check_bdc_vpd		ssd_check_bdc_vpd
901#define	sd_check_emulation_mode		ssd_check_emulation_mode
902
903#define	sd_get_physical_geometry	ssd_get_physical_geometry
904#define	sd_get_virtual_geometry		ssd_get_virtual_geometry
905#define	sd_update_block_info		ssd_update_block_info
906#define	sd_register_devid		ssd_register_devid
907#define	sd_get_devid			ssd_get_devid
908#define	sd_create_devid			ssd_create_devid
909#define	sd_write_deviceid		ssd_write_deviceid
910#define	sd_check_vpd_page_support	ssd_check_vpd_page_support
911#define	sd_setup_pm			ssd_setup_pm
912#define	sd_create_pm_components		ssd_create_pm_components
913#define	sd_ddi_suspend			ssd_ddi_suspend
914#define	sd_ddi_resume			ssd_ddi_resume
915#define	sd_pm_state_change		ssd_pm_state_change
916#define	sdpower				ssdpower
917#define	sdattach			ssdattach
918#define	sddetach			ssddetach
919#define	sd_unit_attach			ssd_unit_attach
920#define	sd_unit_detach			ssd_unit_detach
921#define	sd_set_unit_attributes		ssd_set_unit_attributes
922#define	sd_create_errstats		ssd_create_errstats
923#define	sd_set_errstats			ssd_set_errstats
924#define	sd_set_pstats			ssd_set_pstats
925#define	sddump				ssddump
926#define	sd_scsi_poll			ssd_scsi_poll
927#define	sd_send_polled_RQS		ssd_send_polled_RQS
928#define	sd_ddi_scsi_poll		ssd_ddi_scsi_poll
929#define	sd_init_event_callbacks		ssd_init_event_callbacks
930#define	sd_event_callback		ssd_event_callback
931#define	sd_cache_control		ssd_cache_control
932#define	sd_get_write_cache_enabled	ssd_get_write_cache_enabled
933#define	sd_get_write_cache_changeable	ssd_get_write_cache_changeable
934#define	sd_get_nv_sup			ssd_get_nv_sup
935#define	sd_make_device			ssd_make_device
936#define	sdopen				ssdopen
937#define	sdclose				ssdclose
938#define	sd_ready_and_valid		ssd_ready_and_valid
939#define	sdmin				ssdmin
940#define	sdread				ssdread
941#define	sdwrite				ssdwrite
942#define	sdaread				ssdaread
943#define	sdawrite			ssdawrite
944#define	sdstrategy			ssdstrategy
945#define	sdioctl				ssdioctl
946#define	sd_mapblockaddr_iostart		ssd_mapblockaddr_iostart
947#define	sd_mapblocksize_iostart		ssd_mapblocksize_iostart
948#define	sd_checksum_iostart		ssd_checksum_iostart
949#define	sd_checksum_uscsi_iostart	ssd_checksum_uscsi_iostart
950#define	sd_pm_iostart			ssd_pm_iostart
951#define	sd_core_iostart			ssd_core_iostart
952#define	sd_mapblockaddr_iodone		ssd_mapblockaddr_iodone
953#define	sd_mapblocksize_iodone		ssd_mapblocksize_iodone
954#define	sd_checksum_iodone		ssd_checksum_iodone
955#define	sd_checksum_uscsi_iodone	ssd_checksum_uscsi_iodone
956#define	sd_pm_iodone			ssd_pm_iodone
957#define	sd_initpkt_for_buf		ssd_initpkt_for_buf
958#define	sd_destroypkt_for_buf		ssd_destroypkt_for_buf
959#define	sd_setup_rw_pkt			ssd_setup_rw_pkt
960#define	sd_setup_next_rw_pkt		ssd_setup_next_rw_pkt
961#define	sd_buf_iodone			ssd_buf_iodone
962#define	sd_uscsi_strategy		ssd_uscsi_strategy
963#define	sd_initpkt_for_uscsi		ssd_initpkt_for_uscsi
964#define	sd_destroypkt_for_uscsi		ssd_destroypkt_for_uscsi
965#define	sd_uscsi_iodone			ssd_uscsi_iodone
966#define	sd_xbuf_strategy		ssd_xbuf_strategy
967#define	sd_xbuf_init			ssd_xbuf_init
968#define	sd_pm_entry			ssd_pm_entry
969#define	sd_pm_exit			ssd_pm_exit
970
971#define	sd_pm_idletimeout_handler	ssd_pm_idletimeout_handler
972#define	sd_pm_timeout_handler		ssd_pm_timeout_handler
973
974#define	sd_add_buf_to_waitq		ssd_add_buf_to_waitq
975#define	sdintr				ssdintr
976#define	sd_start_cmds			ssd_start_cmds
977#define	sd_send_scsi_cmd		ssd_send_scsi_cmd
978#define	sd_bioclone_alloc		ssd_bioclone_alloc
979#define	sd_bioclone_free		ssd_bioclone_free
980#define	sd_shadow_buf_alloc		ssd_shadow_buf_alloc
981#define	sd_shadow_buf_free		ssd_shadow_buf_free
982#define	sd_print_transport_rejected_message	\
983					ssd_print_transport_rejected_message
984#define	sd_retry_command		ssd_retry_command
985#define	sd_set_retry_bp			ssd_set_retry_bp
986#define	sd_send_request_sense_command	ssd_send_request_sense_command
987#define	sd_start_retry_command		ssd_start_retry_command
988#define	sd_start_direct_priority_command	\
989					ssd_start_direct_priority_command
990#define	sd_return_failed_command	ssd_return_failed_command
991#define	sd_return_failed_command_no_restart	\
992					ssd_return_failed_command_no_restart
993#define	sd_return_command		ssd_return_command
994#define	sd_sync_with_callback		ssd_sync_with_callback
995#define	sdrunout			ssdrunout
996#define	sd_mark_rqs_busy		ssd_mark_rqs_busy
997#define	sd_mark_rqs_idle		ssd_mark_rqs_idle
998#define	sd_reduce_throttle		ssd_reduce_throttle
999#define	sd_restore_throttle		ssd_restore_throttle
1000#define	sd_print_incomplete_msg		ssd_print_incomplete_msg
1001#define	sd_init_cdb_limits		ssd_init_cdb_limits
1002#define	sd_pkt_status_good		ssd_pkt_status_good
1003#define	sd_pkt_status_check_condition	ssd_pkt_status_check_condition
1004#define	sd_pkt_status_busy		ssd_pkt_status_busy
1005#define	sd_pkt_status_reservation_conflict	\
1006					ssd_pkt_status_reservation_conflict
1007#define	sd_pkt_status_qfull		ssd_pkt_status_qfull
1008#define	sd_handle_request_sense		ssd_handle_request_sense
1009#define	sd_handle_auto_request_sense	ssd_handle_auto_request_sense
1010#define	sd_print_sense_failed_msg	ssd_print_sense_failed_msg
1011#define	sd_validate_sense_data		ssd_validate_sense_data
1012#define	sd_decode_sense			ssd_decode_sense
1013#define	sd_print_sense_msg		ssd_print_sense_msg
1014#define	sd_sense_key_no_sense		ssd_sense_key_no_sense
1015#define	sd_sense_key_recoverable_error	ssd_sense_key_recoverable_error
1016#define	sd_sense_key_not_ready		ssd_sense_key_not_ready
1017#define	sd_sense_key_medium_or_hardware_error	\
1018					ssd_sense_key_medium_or_hardware_error
1019#define	sd_sense_key_illegal_request	ssd_sense_key_illegal_request
1020#define	sd_sense_key_unit_attention	ssd_sense_key_unit_attention
1021#define	sd_sense_key_fail_command	ssd_sense_key_fail_command
1022#define	sd_sense_key_blank_check	ssd_sense_key_blank_check
1023#define	sd_sense_key_aborted_command	ssd_sense_key_aborted_command
1024#define	sd_sense_key_default		ssd_sense_key_default
1025#define	sd_print_retry_msg		ssd_print_retry_msg
1026#define	sd_print_cmd_incomplete_msg	ssd_print_cmd_incomplete_msg
1027#define	sd_pkt_reason_cmd_incomplete	ssd_pkt_reason_cmd_incomplete
1028#define	sd_pkt_reason_cmd_tran_err	ssd_pkt_reason_cmd_tran_err
1029#define	sd_pkt_reason_cmd_reset		ssd_pkt_reason_cmd_reset
1030#define	sd_pkt_reason_cmd_aborted	ssd_pkt_reason_cmd_aborted
1031#define	sd_pkt_reason_cmd_timeout	ssd_pkt_reason_cmd_timeout
1032#define	sd_pkt_reason_cmd_unx_bus_free	ssd_pkt_reason_cmd_unx_bus_free
1033#define	sd_pkt_reason_cmd_tag_reject	ssd_pkt_reason_cmd_tag_reject
1034#define	sd_pkt_reason_default		ssd_pkt_reason_default
1035#define	sd_reset_target			ssd_reset_target
1036#define	sd_start_stop_unit_callback	ssd_start_stop_unit_callback
1037#define	sd_start_stop_unit_task		ssd_start_stop_unit_task
1038#define	sd_taskq_create			ssd_taskq_create
1039#define	sd_taskq_delete			ssd_taskq_delete
1040#define	sd_target_change_task		ssd_target_change_task
1041#define	sd_log_dev_status_event		ssd_log_dev_status_event
1042#define	sd_log_lun_expansion_event	ssd_log_lun_expansion_event
1043#define	sd_log_eject_request_event	ssd_log_eject_request_event
1044#define	sd_media_change_task		ssd_media_change_task
1045#define	sd_handle_mchange		ssd_handle_mchange
1046#define	sd_send_scsi_DOORLOCK		ssd_send_scsi_DOORLOCK
1047#define	sd_send_scsi_READ_CAPACITY	ssd_send_scsi_READ_CAPACITY
1048#define	sd_send_scsi_READ_CAPACITY_16	ssd_send_scsi_READ_CAPACITY_16
1049#define	sd_send_scsi_GET_CONFIGURATION	ssd_send_scsi_GET_CONFIGURATION
1050#define	sd_send_scsi_feature_GET_CONFIGURATION	\
1051					sd_send_scsi_feature_GET_CONFIGURATION
1052#define	sd_send_scsi_START_STOP_UNIT	ssd_send_scsi_START_STOP_UNIT
1053#define	sd_send_scsi_INQUIRY		ssd_send_scsi_INQUIRY
1054#define	sd_send_scsi_TEST_UNIT_READY	ssd_send_scsi_TEST_UNIT_READY
1055#define	sd_send_scsi_PERSISTENT_RESERVE_IN	\
1056					ssd_send_scsi_PERSISTENT_RESERVE_IN
1057#define	sd_send_scsi_PERSISTENT_RESERVE_OUT	\
1058					ssd_send_scsi_PERSISTENT_RESERVE_OUT
1059#define	sd_send_scsi_SYNCHRONIZE_CACHE	ssd_send_scsi_SYNCHRONIZE_CACHE
1060#define	sd_send_scsi_SYNCHRONIZE_CACHE_biodone	\
1061					ssd_send_scsi_SYNCHRONIZE_CACHE_biodone
1062#define	sd_send_scsi_MODE_SENSE		ssd_send_scsi_MODE_SENSE
1063#define	sd_send_scsi_MODE_SELECT	ssd_send_scsi_MODE_SELECT
1064#define	sd_send_scsi_RDWR		ssd_send_scsi_RDWR
1065#define	sd_send_scsi_LOG_SENSE		ssd_send_scsi_LOG_SENSE
1066#define	sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION	\
1067				ssd_send_scsi_GET_EVENT_STATUS_NOTIFICATION
1068#define	sd_gesn_media_data_valid	ssd_gesn_media_data_valid
1069#define	sd_alloc_rqs			ssd_alloc_rqs
1070#define	sd_free_rqs			ssd_free_rqs
1071#define	sd_dump_memory			ssd_dump_memory
1072#define	sd_get_media_info_com		ssd_get_media_info_com
1073#define	sd_get_media_info		ssd_get_media_info
1074#define	sd_get_media_info_ext		ssd_get_media_info_ext
1075#define	sd_dkio_ctrl_info		ssd_dkio_ctrl_info
1076#define	sd_nvpair_str_decode		ssd_nvpair_str_decode
1077#define	sd_strtok_r			ssd_strtok_r
1078#define	sd_set_properties		ssd_set_properties
1079#define	sd_get_tunables_from_conf	ssd_get_tunables_from_conf
1080#define	sd_setup_next_xfer		ssd_setup_next_xfer
1081#define	sd_dkio_get_temp		ssd_dkio_get_temp
1082#define	sd_check_mhd			ssd_check_mhd
1083#define	sd_mhd_watch_cb			ssd_mhd_watch_cb
1084#define	sd_mhd_watch_incomplete		ssd_mhd_watch_incomplete
1085#define	sd_sname			ssd_sname
1086#define	sd_mhd_resvd_recover		ssd_mhd_resvd_recover
1087#define	sd_resv_reclaim_thread		ssd_resv_reclaim_thread
1088#define	sd_take_ownership		ssd_take_ownership
1089#define	sd_reserve_release		ssd_reserve_release
1090#define	sd_rmv_resv_reclaim_req		ssd_rmv_resv_reclaim_req
1091#define	sd_mhd_reset_notify_cb		ssd_mhd_reset_notify_cb
1092#define	sd_persistent_reservation_in_read_keys	\
1093					ssd_persistent_reservation_in_read_keys
1094#define	sd_persistent_reservation_in_read_resv	\
1095					ssd_persistent_reservation_in_read_resv
1096#define	sd_mhdioc_takeown		ssd_mhdioc_takeown
1097#define	sd_mhdioc_failfast		ssd_mhdioc_failfast
1098#define	sd_mhdioc_release		ssd_mhdioc_release
1099#define	sd_mhdioc_register_devid	ssd_mhdioc_register_devid
1100#define	sd_mhdioc_inkeys		ssd_mhdioc_inkeys
1101#define	sd_mhdioc_inresv		ssd_mhdioc_inresv
1102#define	sr_change_blkmode		ssr_change_blkmode
1103#define	sr_change_speed			ssr_change_speed
1104#define	sr_atapi_change_speed		ssr_atapi_change_speed
1105#define	sr_pause_resume			ssr_pause_resume
1106#define	sr_play_msf			ssr_play_msf
1107#define	sr_play_trkind			ssr_play_trkind
1108#define	sr_read_all_subcodes		ssr_read_all_subcodes
1109#define	sr_read_subchannel		ssr_read_subchannel
1110#define	sr_read_tocentry		ssr_read_tocentry
1111#define	sr_read_tochdr			ssr_read_tochdr
1112#define	sr_read_cdda			ssr_read_cdda
1113#define	sr_read_cdxa			ssr_read_cdxa
1114#define	sr_read_mode1			ssr_read_mode1
1115#define	sr_read_mode2			ssr_read_mode2
1116#define	sr_read_cd_mode2		ssr_read_cd_mode2
1117#define	sr_sector_mode			ssr_sector_mode
1118#define	sr_eject			ssr_eject
1119#define	sr_ejected			ssr_ejected
1120#define	sr_check_wp			ssr_check_wp
1121#define	sd_watch_request_submit		ssd_watch_request_submit
1122#define	sd_check_media			ssd_check_media
1123#define	sd_media_watch_cb		ssd_media_watch_cb
1124#define	sd_delayed_cv_broadcast		ssd_delayed_cv_broadcast
1125#define	sr_volume_ctrl			ssr_volume_ctrl
1126#define	sr_read_sony_session_offset	ssr_read_sony_session_offset
1127#define	sd_log_page_supported		ssd_log_page_supported
1128#define	sd_check_for_writable_cd	ssd_check_for_writable_cd
1129#define	sd_wm_cache_constructor		ssd_wm_cache_constructor
1130#define	sd_wm_cache_destructor		ssd_wm_cache_destructor
1131#define	sd_range_lock			ssd_range_lock
1132#define	sd_get_range			ssd_get_range
1133#define	sd_free_inlist_wmap		ssd_free_inlist_wmap
1134#define	sd_range_unlock			ssd_range_unlock
1135#define	sd_read_modify_write_task	ssd_read_modify_write_task
1136#define	sddump_do_read_of_rmw		ssddump_do_read_of_rmw
1137
1138#define	sd_iostart_chain		ssd_iostart_chain
1139#define	sd_iodone_chain			ssd_iodone_chain
1140#define	sd_initpkt_map			ssd_initpkt_map
1141#define	sd_destroypkt_map		ssd_destroypkt_map
1142#define	sd_chain_type_map		ssd_chain_type_map
1143#define	sd_chain_index_map		ssd_chain_index_map
1144
1145#define	sd_failfast_flushctl		ssd_failfast_flushctl
1146#define	sd_failfast_flushq		ssd_failfast_flushq
1147#define	sd_failfast_flushq_callback	ssd_failfast_flushq_callback
1148
1149#define	sd_is_lsi			ssd_is_lsi
1150#define	sd_tg_rdwr			ssd_tg_rdwr
1151#define	sd_tg_getinfo			ssd_tg_getinfo
1152#define	sd_rmw_msg_print_handler	ssd_rmw_msg_print_handler
1153
1154#endif	/* #if (defined(__fibre)) */
1155
1156typedef struct unmap_param_hdr_s {
1157	uint16_t	uph_data_len;
1158	uint16_t	uph_descr_data_len;
1159	uint32_t	uph_reserved;
1160} unmap_param_hdr_t;
1161
1162typedef struct unmap_blk_descr_s {
1163	uint64_t	ubd_lba;
1164	uint32_t	ubd_lba_cnt;
1165	uint32_t	ubd_reserved;
1166} unmap_blk_descr_t;
1167
1168/* Max number of block descriptors in UNMAP command */
1169#define	SD_UNMAP_MAX_DESCR \
1170	((UINT16_MAX - sizeof (unmap_param_hdr_t)) / sizeof (unmap_blk_descr_t))
1171/* Max size of the UNMAP parameter list in bytes */
1172#define	SD_UNMAP_PARAM_LIST_MAXSZ	(sizeof (unmap_param_hdr_t) + \
1173	SD_UNMAP_MAX_DESCR * sizeof (unmap_blk_descr_t))
1174
1175int _init(void);
1176int _fini(void);
1177int _info(struct modinfo *modinfop);
1178
1179/*PRINTFLIKE3*/
1180static void sd_log_trace(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1181/*PRINTFLIKE3*/
1182static void sd_log_info(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1183/*PRINTFLIKE3*/
1184static void sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...);
1185
1186static int sdprobe(dev_info_t *devi);
1187static int sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
1188    void **result);
1189static int sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op,
1190    int mod_flags, char *name, caddr_t valuep, int *lengthp);
1191
1192/*
1193 * Smart probe for parallel scsi
1194 */
1195static void sd_scsi_probe_cache_init(void);
1196static void sd_scsi_probe_cache_fini(void);
1197static void sd_scsi_clear_probe_cache(void);
1198static int  sd_scsi_probe_with_cache(struct scsi_device *devp, int (*fn)());
1199
1200/*
1201 * Attached luns on target for parallel scsi
1202 */
1203static void sd_scsi_target_lun_init(void);
1204static void sd_scsi_target_lun_fini(void);
1205static int  sd_scsi_get_target_lun_count(dev_info_t *dip, int target);
1206static void sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag);
1207
1208static int sd_spin_up_unit(sd_ssc_t *ssc);
1209
1210/*
1211 * Using sd_ssc_init to establish sd_ssc_t struct
1212 * Using sd_ssc_send to send uscsi internal command
1213 * Using sd_ssc_fini to free sd_ssc_t struct
1214 */
1215static sd_ssc_t *sd_ssc_init(struct sd_lun *un);
1216static int sd_ssc_send(sd_ssc_t *ssc, struct uscsi_cmd *incmd,
1217    int flag, enum uio_seg dataspace, int path_flag);
1218static void sd_ssc_fini(sd_ssc_t *ssc);
1219
1220/*
1221 * Using sd_ssc_assessment to set correct type-of-assessment
1222 * Using sd_ssc_post to post ereport & system log
1223 *       sd_ssc_post will call sd_ssc_print to print system log
1224 *       sd_ssc_post will call sd_ssd_ereport_post to post ereport
1225 */
1226static void sd_ssc_assessment(sd_ssc_t *ssc,
1227    enum sd_type_assessment tp_assess);
1228
1229static void sd_ssc_post(sd_ssc_t *ssc, enum sd_driver_assessment sd_assess);
1230static void sd_ssc_print(sd_ssc_t *ssc, int sd_severity);
1231static void sd_ssc_ereport_post(sd_ssc_t *ssc,
1232    enum sd_driver_assessment drv_assess);
1233
1234/*
1235 * Using sd_ssc_set_info to mark an un-decodable-data error.
1236 * Using sd_ssc_extract_info to transfer information from internal
1237 *       data structures to sd_ssc_t.
1238 */
1239static void sd_ssc_set_info(sd_ssc_t *ssc, int ssc_flags, uint_t comp,
1240    const char *fmt, ...);
1241static void sd_ssc_extract_info(sd_ssc_t *ssc, struct sd_lun *un,
1242    struct scsi_pkt *pktp, struct buf *bp, struct sd_xbuf *xp);
1243
1244static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1245    enum uio_seg dataspace, int path_flag);
1246
1247#ifdef _LP64
1248static void	sd_enable_descr_sense(sd_ssc_t *ssc);
1249static void	sd_reenable_dsense_task(void *arg);
1250#endif /* _LP64 */
1251
1252static void	sd_set_mmc_caps(sd_ssc_t *ssc);
1253
1254static void sd_read_unit_properties(struct sd_lun *un);
1255static int  sd_process_sdconf_file(struct sd_lun *un);
1256static void sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str);
1257static char *sd_strtok_r(char *string, const char *sepset, char **lasts);
1258static void sd_set_properties(struct sd_lun *un, char *name, char *value);
1259static void sd_get_tunables_from_conf(struct sd_lun *un, int flags,
1260    int *data_list, sd_tunables *values);
1261static void sd_process_sdconf_table(struct sd_lun *un);
1262static int  sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen);
1263static int  sd_blank_cmp(struct sd_lun *un, char *id, int idlen);
1264static int  sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
1265    int list_len, char *dataname_ptr);
1266static void sd_set_vers1_properties(struct sd_lun *un, int flags,
1267    sd_tunables *prop_list);
1268
1269static void sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi,
1270    int reservation_flag);
1271static int  sd_get_devid(sd_ssc_t *ssc);
1272static ddi_devid_t sd_create_devid(sd_ssc_t *ssc);
1273static int  sd_write_deviceid(sd_ssc_t *ssc);
1274static int  sd_check_vpd_page_support(sd_ssc_t *ssc);
1275
1276static void sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi);
1277static void sd_create_pm_components(dev_info_t *devi, struct sd_lun *un);
1278
1279static int  sd_ddi_suspend(dev_info_t *devi);
1280static int  sd_ddi_resume(dev_info_t *devi);
1281static int  sd_pm_state_change(struct sd_lun *un, int level, int flag);
1282static int  sdpower(dev_info_t *devi, int component, int level);
1283
1284static int  sdattach(dev_info_t *devi, ddi_attach_cmd_t cmd);
1285static int  sddetach(dev_info_t *devi, ddi_detach_cmd_t cmd);
1286static int  sd_unit_attach(dev_info_t *devi);
1287static int  sd_unit_detach(dev_info_t *devi);
1288
1289static void sd_set_unit_attributes(struct sd_lun *un, dev_info_t *devi);
1290static void sd_create_errstats(struct sd_lun *un, int instance);
1291static void sd_set_errstats(struct sd_lun *un);
1292static void sd_set_pstats(struct sd_lun *un);
1293
1294static int  sddump(dev_t dev, caddr_t addr, daddr_t blkno, int nblk);
1295static int  sd_scsi_poll(struct sd_lun *un, struct scsi_pkt *pkt);
1296static int  sd_send_polled_RQS(struct sd_lun *un);
1297static int  sd_ddi_scsi_poll(struct scsi_pkt *pkt);
1298
1299#if (defined(__fibre))
1300/*
1301 * Event callbacks (photon)
1302 */
1303static void sd_init_event_callbacks(struct sd_lun *un);
1304static void  sd_event_callback(dev_info_t *, ddi_eventcookie_t, void *, void *);
1305#endif
1306
1307/*
1308 * Defines for sd_cache_control
1309 */
1310
1311#define	SD_CACHE_ENABLE		1
1312#define	SD_CACHE_DISABLE	0
1313#define	SD_CACHE_NOCHANGE	-1
1314
1315static int   sd_cache_control(sd_ssc_t *ssc, int rcd_flag, int wce_flag);
1316static int   sd_get_write_cache_enabled(sd_ssc_t *ssc, int *is_enabled);
1317static void  sd_get_write_cache_changeable(sd_ssc_t *ssc, int *is_changeable);
1318static void  sd_get_nv_sup(sd_ssc_t *ssc);
1319static dev_t sd_make_device(dev_info_t *devi);
1320static void  sd_check_bdc_vpd(sd_ssc_t *ssc);
1321static void  sd_check_emulation_mode(sd_ssc_t *ssc);
1322static void  sd_update_block_info(struct sd_lun *un, uint32_t lbasize,
1323    uint64_t capacity);
1324
1325/*
1326 * Driver entry point functions.
1327 */
1328static int  sdopen(dev_t *dev_p, int flag, int otyp, cred_t *cred_p);
1329static int  sdclose(dev_t dev, int flag, int otyp, cred_t *cred_p);
1330static int  sd_ready_and_valid(sd_ssc_t *ssc, int part);
1331
1332static void sdmin(struct buf *bp);
1333static int sdread(dev_t dev, struct uio *uio, cred_t *cred_p);
1334static int sdwrite(dev_t dev, struct uio *uio, cred_t *cred_p);
1335static int sdaread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1336static int sdawrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
1337
1338static int sdstrategy(struct buf *bp);
1339static int sdioctl(dev_t, int, intptr_t, int, cred_t *, int *);
1340
1341/*
1342 * Function prototypes for layering functions in the iostart chain.
1343 */
1344static void sd_mapblockaddr_iostart(int index, struct sd_lun *un,
1345    struct buf *bp);
1346static void sd_mapblocksize_iostart(int index, struct sd_lun *un,
1347    struct buf *bp);
1348static void sd_checksum_iostart(int index, struct sd_lun *un, struct buf *bp);
1349static void sd_checksum_uscsi_iostart(int index, struct sd_lun *un,
1350    struct buf *bp);
1351static void sd_pm_iostart(int index, struct sd_lun *un, struct buf *bp);
1352static void sd_core_iostart(int index, struct sd_lun *un, struct buf *bp);
1353
1354/*
1355 * Function prototypes for layering functions in the iodone chain.
1356 */
1357static void sd_buf_iodone(int index, struct sd_lun *un, struct buf *bp);
1358static void sd_uscsi_iodone(int index, struct sd_lun *un, struct buf *bp);
1359static void sd_mapblockaddr_iodone(int index, struct sd_lun *un,
1360    struct buf *bp);
1361static void sd_mapblocksize_iodone(int index, struct sd_lun *un,
1362    struct buf *bp);
1363static void sd_checksum_iodone(int index, struct sd_lun *un, struct buf *bp);
1364static void sd_checksum_uscsi_iodone(int index, struct sd_lun *un,
1365    struct buf *bp);
1366static void sd_pm_iodone(int index, struct sd_lun *un, struct buf *bp);
1367
1368/*
1369 * Prototypes for functions to support buf(9S) based IO.
1370 */
1371static void sd_xbuf_strategy(struct buf *bp, ddi_xbuf_t xp, void *arg);
1372static int sd_initpkt_for_buf(struct buf *, struct scsi_pkt **);
1373static void sd_destroypkt_for_buf(struct buf *);
1374static int sd_setup_rw_pkt(struct sd_lun *un, struct scsi_pkt **pktpp,
1375    struct buf *bp, int flags,
1376    int (*callback)(caddr_t), caddr_t callback_arg,
1377    diskaddr_t lba, uint32_t blockcount);
1378static int sd_setup_next_rw_pkt(struct sd_lun *un, struct scsi_pkt *pktp,
1379    struct buf *bp, diskaddr_t lba, uint32_t blockcount);
1380
1381/*
1382 * Prototypes for functions to support USCSI IO.
1383 */
1384static int sd_uscsi_strategy(struct buf *bp);
1385static int sd_initpkt_for_uscsi(struct buf *, struct scsi_pkt **);
1386static void sd_destroypkt_for_uscsi(struct buf *);
1387
1388static void sd_xbuf_init(struct sd_lun *un, struct buf *bp, struct sd_xbuf *xp,
1389    uchar_t chain_type, void *pktinfop);
1390
1391static int  sd_pm_entry(struct sd_lun *un);
1392static void sd_pm_exit(struct sd_lun *un);
1393
1394static void sd_pm_idletimeout_handler(void *arg);
1395
1396/*
1397 * sd_core internal functions (used at the sd_core_io layer).
1398 */
1399static void sd_add_buf_to_waitq(struct sd_lun *un, struct buf *bp);
1400static void sdintr(struct scsi_pkt *pktp);
1401static void sd_start_cmds(struct sd_lun *un, struct buf *immed_bp);
1402
1403static int sd_send_scsi_cmd(dev_t dev, struct uscsi_cmd *incmd, int flag,
1404    enum uio_seg dataspace, int path_flag);
1405
1406static struct buf *sd_bioclone_alloc(struct buf *bp, size_t datalen,
1407    daddr_t blkno, int (*func)(struct buf *));
1408static struct buf *sd_shadow_buf_alloc(struct buf *bp, size_t datalen,
1409    uint_t bflags, daddr_t blkno, int (*func)(struct buf *));
1410static void sd_bioclone_free(struct buf *bp);
1411static void sd_shadow_buf_free(struct buf *bp);
1412
1413static void sd_print_transport_rejected_message(struct sd_lun *un,
1414    struct sd_xbuf *xp, int code);
1415static void sd_print_incomplete_msg(struct sd_lun *un, struct buf *bp,
1416    void *arg, int code);
1417static void sd_print_sense_failed_msg(struct sd_lun *un, struct buf *bp,
1418    void *arg, int code);
1419static void sd_print_cmd_incomplete_msg(struct sd_lun *un, struct buf *bp,
1420    void *arg, int code);
1421
1422static void sd_retry_command(struct sd_lun *un, struct buf *bp,
1423    int retry_check_flag,
1424    void (*user_funcp)(struct sd_lun *un, struct buf *bp, void *argp, int c),
1425    void *user_arg, int failure_code,  clock_t retry_delay,
1426    void (*statp)(kstat_io_t *));
1427
1428static void sd_set_retry_bp(struct sd_lun *un, struct buf *bp,
1429    clock_t retry_delay, void (*statp)(kstat_io_t *));
1430
1431static void sd_send_request_sense_command(struct sd_lun *un, struct buf *bp,
1432    struct scsi_pkt *pktp);
1433static void sd_start_retry_command(void *arg);
1434static void sd_start_direct_priority_command(void *arg);
1435static void sd_return_failed_command(struct sd_lun *un, struct buf *bp,
1436    int errcode);
1437static void sd_return_failed_command_no_restart(struct sd_lun *un,
1438    struct buf *bp, int errcode);
1439static void sd_return_command(struct sd_lun *un, struct buf *bp);
1440static void sd_sync_with_callback(struct sd_lun *un);
1441static int sdrunout(caddr_t arg);
1442
1443static void sd_mark_rqs_busy(struct sd_lun *un, struct buf *bp);
1444static struct buf *sd_mark_rqs_idle(struct sd_lun *un, struct sd_xbuf *xp);
1445
1446static void sd_reduce_throttle(struct sd_lun *un, int throttle_type);
1447static void sd_restore_throttle(void *arg);
1448
1449static void sd_init_cdb_limits(struct sd_lun *un);
1450
1451static void sd_pkt_status_good(struct sd_lun *un, struct buf *bp,
1452    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1453
1454/*
1455 * Error handling functions
1456 */
1457static void sd_pkt_status_check_condition(struct sd_lun *un, struct buf *bp,
1458    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1459static void sd_pkt_status_busy(struct sd_lun *un, struct buf *bp,
1460    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1461static void sd_pkt_status_reservation_conflict(struct sd_lun *un,
1462    struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1463static void sd_pkt_status_qfull(struct sd_lun *un, struct buf *bp,
1464    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1465
1466static void sd_handle_request_sense(struct sd_lun *un, struct buf *bp,
1467    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1468static void sd_handle_auto_request_sense(struct sd_lun *un, struct buf *bp,
1469    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1470static int sd_validate_sense_data(struct sd_lun *un, struct buf *bp,
1471    struct sd_xbuf *xp, size_t actual_len);
1472static void sd_decode_sense(struct sd_lun *un, struct buf *bp,
1473    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1474
1475static void sd_print_sense_msg(struct sd_lun *un, struct buf *bp,
1476    void *arg, int code);
1477
1478static void sd_sense_key_no_sense(struct sd_lun *un, struct buf *bp,
1479    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1480static void sd_sense_key_recoverable_error(struct sd_lun *un,
1481    uint8_t *sense_datap,
1482    struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1483static void sd_sense_key_not_ready(struct sd_lun *un,
1484    uint8_t *sense_datap,
1485    struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1486static void sd_sense_key_medium_or_hardware_error(struct sd_lun *un,
1487    uint8_t *sense_datap,
1488    struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1489static void sd_sense_key_illegal_request(struct sd_lun *un, struct buf *bp,
1490    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1491static void sd_sense_key_unit_attention(struct sd_lun *un,
1492    uint8_t *sense_datap,
1493    struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1494static void sd_sense_key_fail_command(struct sd_lun *un, struct buf *bp,
1495    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1496static void sd_sense_key_blank_check(struct sd_lun *un, struct buf *bp,
1497    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1498static void sd_sense_key_aborted_command(struct sd_lun *un, struct buf *bp,
1499    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1500static void sd_sense_key_default(struct sd_lun *un,
1501    uint8_t *sense_datap,
1502    struct buf *bp, struct sd_xbuf *xp, struct scsi_pkt *pktp);
1503
1504static void sd_print_retry_msg(struct sd_lun *un, struct buf *bp,
1505    void *arg, int flag);
1506
1507static void sd_pkt_reason_cmd_incomplete(struct sd_lun *un, struct buf *bp,
1508    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1509static void sd_pkt_reason_cmd_tran_err(struct sd_lun *un, struct buf *bp,
1510    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1511static void sd_pkt_reason_cmd_reset(struct sd_lun *un, struct buf *bp,
1512    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1513static void sd_pkt_reason_cmd_aborted(struct sd_lun *un, struct buf *bp,
1514    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1515static void sd_pkt_reason_cmd_timeout(struct sd_lun *un, struct buf *bp,
1516    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1517static void sd_pkt_reason_cmd_unx_bus_free(struct sd_lun *un, struct buf *bp,
1518    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1519static void sd_pkt_reason_cmd_tag_reject(struct sd_lun *un, struct buf *bp,
1520    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1521static void sd_pkt_reason_default(struct sd_lun *un, struct buf *bp,
1522    struct sd_xbuf *xp, struct scsi_pkt *pktp);
1523
1524static void sd_reset_target(struct sd_lun *un, struct scsi_pkt *pktp);
1525
1526static void sd_start_stop_unit_callback(void *arg);
1527static void sd_start_stop_unit_task(void *arg);
1528
1529static void sd_taskq_create(void);
1530static void sd_taskq_delete(void);
1531static void sd_target_change_task(void *arg);
1532static void sd_log_dev_status_event(struct sd_lun *un, char *esc, int km_flag);
1533static void sd_log_lun_expansion_event(struct sd_lun *un, int km_flag);
1534static void sd_log_eject_request_event(struct sd_lun *un, int km_flag);
1535static void sd_media_change_task(void *arg);
1536
1537static int sd_handle_mchange(struct sd_lun *un);
1538static int sd_send_scsi_DOORLOCK(sd_ssc_t *ssc, int flag, int path_flag);
1539static int sd_send_scsi_READ_CAPACITY(sd_ssc_t *ssc, uint64_t *capp,
1540    uint32_t *lbap, int path_flag);
1541static int sd_send_scsi_READ_CAPACITY_16(sd_ssc_t *ssc, uint64_t *capp,
1542    uint32_t *lbap, uint32_t *psp, int path_flag);
1543static int sd_send_scsi_START_STOP_UNIT(sd_ssc_t *ssc, int pc_flag,
1544    int flag, int path_flag);
1545static int sd_send_scsi_INQUIRY(sd_ssc_t *ssc, uchar_t *bufaddr,
1546    size_t buflen, uchar_t evpd, uchar_t page_code, size_t *residp);
1547static int sd_send_scsi_TEST_UNIT_READY(sd_ssc_t *ssc, int flag);
1548static int sd_send_scsi_PERSISTENT_RESERVE_IN(sd_ssc_t *ssc,
1549    uchar_t usr_cmd, uint16_t data_len, uchar_t *data_bufp);
1550static int sd_send_scsi_PERSISTENT_RESERVE_OUT(sd_ssc_t *ssc,
1551    uchar_t usr_cmd, uchar_t *usr_bufp);
1552static int sd_send_scsi_SYNCHRONIZE_CACHE(struct sd_lun *un,
1553    struct dk_callback *dkc);
1554static int sd_send_scsi_SYNCHRONIZE_CACHE_biodone(struct buf *bp);
1555static int sd_send_scsi_UNMAP(dev_t dev, sd_ssc_t *ssc, dkioc_free_list_t *dfl,
1556    int flag);
1557static int sd_send_scsi_GET_CONFIGURATION(sd_ssc_t *ssc,
1558    struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1559    uchar_t *bufaddr, uint_t buflen, int path_flag);
1560static int sd_send_scsi_feature_GET_CONFIGURATION(sd_ssc_t *ssc,
1561    struct uscsi_cmd *ucmdbuf, uchar_t *rqbuf, uint_t rqbuflen,
1562    uchar_t *bufaddr, uint_t buflen, char feature, int path_flag);
1563static int sd_send_scsi_MODE_SENSE(sd_ssc_t *ssc, int cdbsize,
1564    uchar_t *bufaddr, size_t buflen, uchar_t page_code, int path_flag);
1565static int sd_send_scsi_MODE_SELECT(sd_ssc_t *ssc, int cdbsize,
1566    uchar_t *bufaddr, size_t buflen, uchar_t save_page, int path_flag);
1567static int sd_send_scsi_RDWR(sd_ssc_t *ssc, uchar_t cmd, void *bufaddr,
1568    size_t buflen, daddr_t start_block, int path_flag);
1569#define	sd_send_scsi_READ(ssc, bufaddr, buflen, start_block, path_flag)	\
1570    sd_send_scsi_RDWR(ssc, SCMD_READ, bufaddr, buflen, start_block, \
1571    path_flag)
1572#define	sd_send_scsi_WRITE(ssc, bufaddr, buflen, start_block, path_flag)\
1573    sd_send_scsi_RDWR(ssc, SCMD_WRITE, bufaddr, buflen, start_block,\
1574    path_flag)
1575
1576static int sd_send_scsi_LOG_SENSE(sd_ssc_t *ssc, uchar_t *bufaddr,
1577    uint16_t buflen, uchar_t page_code, uchar_t page_control,
1578    uint16_t param_ptr, int path_flag);
1579static int sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(sd_ssc_t *ssc,
1580    uchar_t *bufaddr, size_t buflen, uchar_t class_req);
1581static boolean_t sd_gesn_media_data_valid(uchar_t *data);
1582
1583static int  sd_alloc_rqs(struct scsi_device *devp, struct sd_lun *un);
1584static void sd_free_rqs(struct sd_lun *un);
1585
1586static void sd_dump_memory(struct sd_lun *un, uint_t comp, char *title,
1587    uchar_t *data, int len, int fmt);
1588static void sd_panic_for_res_conflict(struct sd_lun *un);
1589
1590/*
1591 * Disk Ioctl Function Prototypes
1592 */
1593static int sd_get_media_info(dev_t dev, caddr_t arg, int flag);
1594static int sd_get_media_info_ext(dev_t dev, caddr_t arg, int flag);
1595static int sd_dkio_ctrl_info(dev_t dev, caddr_t arg, int flag);
1596static int sd_dkio_get_temp(dev_t dev, caddr_t arg, int flag);
1597
1598/*
1599 * Multi-host Ioctl Prototypes
1600 */
1601static int sd_check_mhd(dev_t dev, int interval);
1602static int sd_mhd_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1603static void sd_mhd_watch_incomplete(struct sd_lun *un, struct scsi_pkt *pkt);
1604static char *sd_sname(uchar_t status);
1605static void sd_mhd_resvd_recover(void *arg);
1606static void sd_resv_reclaim_thread();
1607static int sd_take_ownership(dev_t dev, struct mhioctkown *p);
1608static int sd_reserve_release(dev_t dev, int cmd);
1609static void sd_rmv_resv_reclaim_req(dev_t dev);
1610static void sd_mhd_reset_notify_cb(caddr_t arg);
1611static int sd_persistent_reservation_in_read_keys(struct sd_lun *un,
1612    mhioc_inkeys_t *usrp, int flag);
1613static int sd_persistent_reservation_in_read_resv(struct sd_lun *un,
1614    mhioc_inresvs_t *usrp, int flag);
1615static int sd_mhdioc_takeown(dev_t dev, caddr_t arg, int flag);
1616static int sd_mhdioc_failfast(dev_t dev, caddr_t arg, int flag);
1617static int sd_mhdioc_release(dev_t dev);
1618static int sd_mhdioc_register_devid(dev_t dev);
1619static int sd_mhdioc_inkeys(dev_t dev, caddr_t arg, int flag);
1620static int sd_mhdioc_inresv(dev_t dev, caddr_t arg, int flag);
1621
1622/*
1623 * SCSI removable prototypes
1624 */
1625static int sr_change_blkmode(dev_t dev, int cmd, intptr_t data, int flag);
1626static int sr_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1627static int sr_atapi_change_speed(dev_t dev, int cmd, intptr_t data, int flag);
1628static int sr_pause_resume(dev_t dev, int mode);
1629static int sr_play_msf(dev_t dev, caddr_t data, int flag);
1630static int sr_play_trkind(dev_t dev, caddr_t data, int flag);
1631static int sr_read_all_subcodes(dev_t dev, caddr_t data, int flag);
1632static int sr_read_subchannel(dev_t dev, caddr_t data, int flag);
1633static int sr_read_tocentry(dev_t dev, caddr_t data, int flag);
1634static int sr_read_tochdr(dev_t dev, caddr_t data, int flag);
1635static int sr_read_cdda(dev_t dev, caddr_t data, int flag);
1636static int sr_read_cdxa(dev_t dev, caddr_t data, int flag);
1637static int sr_read_mode1(dev_t dev, caddr_t data, int flag);
1638static int sr_read_mode2(dev_t dev, caddr_t data, int flag);
1639static int sr_read_cd_mode2(dev_t dev, caddr_t data, int flag);
1640static int sr_sector_mode(dev_t dev, uint32_t blksize);
1641static int sr_eject(dev_t dev);
1642static void sr_ejected(register struct sd_lun *un);
1643static int sr_check_wp(dev_t dev);
1644static opaque_t sd_watch_request_submit(struct sd_lun *un);
1645static int sd_check_media(dev_t dev, enum dkio_state state);
1646static int sd_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
1647static void sd_delayed_cv_broadcast(void *arg);
1648static int sr_volume_ctrl(dev_t dev, caddr_t data, int flag);
1649static int sr_read_sony_session_offset(dev_t dev, caddr_t data, int flag);
1650
1651static int sd_log_page_supported(sd_ssc_t *ssc, int log_page);
1652
1653/*
1654 * Function Prototype for the non-512 support (DVDRAM, MO etc.) functions.
1655 */
1656static void sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag);
1657static int sd_wm_cache_constructor(void *wm, void *un, int flags);
1658static void sd_wm_cache_destructor(void *wm, void *un);
1659static struct sd_w_map *sd_range_lock(struct sd_lun *un, daddr_t startb,
1660    daddr_t endb, ushort_t typ);
1661static struct sd_w_map *sd_get_range(struct sd_lun *un, daddr_t startb,
1662    daddr_t endb);
1663static void sd_free_inlist_wmap(struct sd_lun *un, struct sd_w_map *wmp);
1664static void sd_range_unlock(struct sd_lun *un, struct sd_w_map *wm);
1665static void sd_read_modify_write_task(void * arg);
1666static int
1667sddump_do_read_of_rmw(struct sd_lun *un, uint64_t blkno, uint64_t nblk,
1668    struct buf **bpp);
1669
1670
1671/*
1672 * Function prototypes for failfast support.
1673 */
1674static void sd_failfast_flushq(struct sd_lun *un);
1675static int sd_failfast_flushq_callback(struct buf *bp);
1676
1677/*
1678 * Function prototypes to check for lsi devices
1679 */
1680static void sd_is_lsi(struct sd_lun *un);
1681
1682/*
1683 * Function prototypes for partial DMA support
1684 */
1685static int sd_setup_next_xfer(struct sd_lun *un, struct buf *bp,
1686		struct scsi_pkt *pkt, struct sd_xbuf *xp);
1687
1688
1689/* Function prototypes for cmlb */
1690static int sd_tg_rdwr(dev_info_t *devi, uchar_t cmd, void *bufaddr,
1691    diskaddr_t start_block, size_t reqlength, void *tg_cookie);
1692
1693static int sd_tg_getinfo(dev_info_t *devi, int cmd, void *arg, void *tg_cookie);
1694
1695/*
1696 * For printing RMW warning message timely
1697 */
1698static void sd_rmw_msg_print_handler(void *arg);
1699
1700/*
1701 * Constants for failfast support:
1702 *
1703 * SD_FAILFAST_INACTIVE: Instance is currently in a normal state, with NO
1704 * failfast processing being performed.
1705 *
1706 * SD_FAILFAST_ACTIVE: Instance is in the failfast state and is performing
1707 * failfast processing on all bufs with B_FAILFAST set.
1708 */
1709
1710#define	SD_FAILFAST_INACTIVE		0
1711#define	SD_FAILFAST_ACTIVE		1
1712
1713/*
1714 * Bitmask to control behavior of buf(9S) flushes when a transition to
1715 * the failfast state occurs. Optional bits include:
1716 *
1717 * SD_FAILFAST_FLUSH_ALL_BUFS: When set, flush ALL bufs including those that
1718 * do NOT have B_FAILFAST set. When clear, only bufs with B_FAILFAST will
1719 * be flushed.
1720 *
1721 * SD_FAILFAST_FLUSH_ALL_QUEUES: When set, flush any/all other queues in the
1722 * driver, in addition to the regular wait queue. This includes the xbuf
1723 * queues. When clear, only the driver's wait queue will be flushed.
1724 */
1725#define	SD_FAILFAST_FLUSH_ALL_BUFS	0x01
1726#define	SD_FAILFAST_FLUSH_ALL_QUEUES	0x02
1727
1728/*
1729 * The default behavior is to only flush bufs that have B_FAILFAST set, but
1730 * to flush all queues within the driver.
1731 */
1732static int sd_failfast_flushctl = SD_FAILFAST_FLUSH_ALL_QUEUES;
1733
1734
1735/*
1736 * SD Testing Fault Injection
1737 */
1738#ifdef SD_FAULT_INJECTION
1739static void sd_faultinjection_ioctl(int cmd, intptr_t arg, struct sd_lun *un);
1740static void sd_faultinjection(struct scsi_pkt *pktp);
1741static void sd_injection_log(char *buf, struct sd_lun *un);
1742#endif
1743
1744/*
1745 * Device driver ops vector
1746 */
1747static struct cb_ops sd_cb_ops = {
1748	sdopen,			/* open */
1749	sdclose,		/* close */
1750	sdstrategy,		/* strategy */
1751	nodev,			/* print */
1752	sddump,			/* dump */
1753	sdread,			/* read */
1754	sdwrite,		/* write */
1755	sdioctl,		/* ioctl */
1756	nodev,			/* devmap */
1757	nodev,			/* mmap */
1758	nodev,			/* segmap */
1759	nochpoll,		/* poll */
1760	sd_prop_op,		/* cb_prop_op */
1761	0,			/* streamtab  */
1762	D_64BIT | D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flags */
1763	CB_REV,			/* cb_rev */
1764	sdaread,		/* async I/O read entry point */
1765	sdawrite		/* async I/O write entry point */
1766};
1767
1768struct dev_ops sd_ops = {
1769	DEVO_REV,		/* devo_rev, */
1770	0,			/* refcnt  */
1771	sdinfo,			/* info */
1772	nulldev,		/* identify */
1773	sdprobe,		/* probe */
1774	sdattach,		/* attach */
1775	sddetach,		/* detach */
1776	nodev,			/* reset */
1777	&sd_cb_ops,		/* driver operations */
1778	NULL,			/* bus operations */
1779	sdpower,		/* power */
1780	ddi_quiesce_not_needed,		/* quiesce */
1781};
1782
1783/*
1784 * This is the loadable module wrapper.
1785 */
1786#include <sys/modctl.h>
1787
1788static struct modldrv modldrv = {
1789	&mod_driverops,		/* Type of module. This one is a driver */
1790	SD_MODULE_NAME,		/* Module name. */
1791	&sd_ops			/* driver ops */
1792};
1793
1794static struct modlinkage modlinkage = {
1795	MODREV_1, &modldrv, NULL
1796};
1797
1798static cmlb_tg_ops_t sd_tgops = {
1799	TG_DK_OPS_VERSION_1,
1800	sd_tg_rdwr,
1801	sd_tg_getinfo
1802};
1803
1804static struct scsi_asq_key_strings sd_additional_codes[] = {
1805	0x81, 0, "Logical Unit is Reserved",
1806	0x85, 0, "Audio Address Not Valid",
1807	0xb6, 0, "Media Load Mechanism Failed",
1808	0xB9, 0, "Audio Play Operation Aborted",
1809	0xbf, 0, "Buffer Overflow for Read All Subcodes Command",
1810	0x53, 2, "Medium removal prevented",
1811	0x6f, 0, "Authentication failed during key exchange",
1812	0x6f, 1, "Key not present",
1813	0x6f, 2, "Key not established",
1814	0x6f, 3, "Read without proper authentication",
1815	0x6f, 4, "Mismatched region to this logical unit",
1816	0x6f, 5, "Region reset count error",
1817	0xffff, 0x0, NULL
1818};
1819
1820
1821/*
1822 * Struct for passing printing information for sense data messages
1823 */
1824struct sd_sense_info {
1825	int	ssi_severity;
1826	int	ssi_pfa_flag;
1827};
1828
1829/*
1830 * Table of function pointers for iostart-side routines. Separate "chains"
1831 * of layered function calls are formed by placing the function pointers
1832 * sequentially in the desired order. Functions are called according to an
1833 * incrementing table index ordering. The last function in each chain must
1834 * be sd_core_iostart(). The corresponding iodone-side routines are expected
1835 * in the sd_iodone_chain[] array.
1836 *
1837 * Note: It may seem more natural to organize both the iostart and iodone
1838 * functions together, into an array of structures (or some similar
1839 * organization) with a common index, rather than two separate arrays which
1840 * must be maintained in synchronization. The purpose of this division is
1841 * to achieve improved performance: individual arrays allows for more
1842 * effective cache line utilization on certain platforms.
1843 */
1844
1845typedef void (*sd_chain_t)(int index, struct sd_lun *un, struct buf *bp);
1846
1847
1848static sd_chain_t sd_iostart_chain[] = {
1849
1850	/* Chain for buf IO for disk drive targets (PM enabled) */
1851	sd_mapblockaddr_iostart,	/* Index: 0 */
1852	sd_pm_iostart,			/* Index: 1 */
1853	sd_core_iostart,		/* Index: 2 */
1854
1855	/* Chain for buf IO for disk drive targets (PM disabled) */
1856	sd_mapblockaddr_iostart,	/* Index: 3 */
1857	sd_core_iostart,		/* Index: 4 */
1858
1859	/*
1860	 * Chain for buf IO for removable-media or large sector size
1861	 * disk drive targets with RMW needed (PM enabled)
1862	 */
1863	sd_mapblockaddr_iostart,	/* Index: 5 */
1864	sd_mapblocksize_iostart,	/* Index: 6 */
1865	sd_pm_iostart,			/* Index: 7 */
1866	sd_core_iostart,		/* Index: 8 */
1867
1868	/*
1869	 * Chain for buf IO for removable-media or large sector size
1870	 * disk drive targets with RMW needed (PM disabled)
1871	 */
1872	sd_mapblockaddr_iostart,	/* Index: 9 */
1873	sd_mapblocksize_iostart,	/* Index: 10 */
1874	sd_core_iostart,		/* Index: 11 */
1875
1876	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1877	sd_mapblockaddr_iostart,	/* Index: 12 */
1878	sd_checksum_iostart,		/* Index: 13 */
1879	sd_pm_iostart,			/* Index: 14 */
1880	sd_core_iostart,		/* Index: 15 */
1881
1882	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1883	sd_mapblockaddr_iostart,	/* Index: 16 */
1884	sd_checksum_iostart,		/* Index: 17 */
1885	sd_core_iostart,		/* Index: 18 */
1886
1887	/* Chain for USCSI commands (all targets) */
1888	sd_pm_iostart,			/* Index: 19 */
1889	sd_core_iostart,		/* Index: 20 */
1890
1891	/* Chain for checksumming USCSI commands (all targets) */
1892	sd_checksum_uscsi_iostart,	/* Index: 21 */
1893	sd_pm_iostart,			/* Index: 22 */
1894	sd_core_iostart,		/* Index: 23 */
1895
1896	/* Chain for "direct" USCSI commands (all targets) */
1897	sd_core_iostart,		/* Index: 24 */
1898
1899	/* Chain for "direct priority" USCSI commands (all targets) */
1900	sd_core_iostart,		/* Index: 25 */
1901
1902	/*
1903	 * Chain for buf IO for large sector size disk drive targets
1904	 * with RMW needed with checksumming (PM enabled)
1905	 */
1906	sd_mapblockaddr_iostart,	/* Index: 26 */
1907	sd_mapblocksize_iostart,	/* Index: 27 */
1908	sd_checksum_iostart,		/* Index: 28 */
1909	sd_pm_iostart,			/* Index: 29 */
1910	sd_core_iostart,		/* Index: 30 */
1911
1912	/*
1913	 * Chain for buf IO for large sector size disk drive targets
1914	 * with RMW needed with checksumming (PM disabled)
1915	 */
1916	sd_mapblockaddr_iostart,	/* Index: 31 */
1917	sd_mapblocksize_iostart,	/* Index: 32 */
1918	sd_checksum_iostart,		/* Index: 33 */
1919	sd_core_iostart,		/* Index: 34 */
1920
1921};
1922
1923/*
1924 * Macros to locate the first function of each iostart chain in the
1925 * sd_iostart_chain[] array. These are located by the index in the array.
1926 */
1927#define	SD_CHAIN_DISK_IOSTART			0
1928#define	SD_CHAIN_DISK_IOSTART_NO_PM		3
1929#define	SD_CHAIN_MSS_DISK_IOSTART		5
1930#define	SD_CHAIN_RMMEDIA_IOSTART		5
1931#define	SD_CHAIN_MSS_DISK_IOSTART_NO_PM		9
1932#define	SD_CHAIN_RMMEDIA_IOSTART_NO_PM		9
1933#define	SD_CHAIN_CHKSUM_IOSTART			12
1934#define	SD_CHAIN_CHKSUM_IOSTART_NO_PM		16
1935#define	SD_CHAIN_USCSI_CMD_IOSTART		19
1936#define	SD_CHAIN_USCSI_CHKSUM_IOSTART		21
1937#define	SD_CHAIN_DIRECT_CMD_IOSTART		24
1938#define	SD_CHAIN_PRIORITY_CMD_IOSTART		25
1939#define	SD_CHAIN_MSS_CHKSUM_IOSTART		26
1940#define	SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM	31
1941
1942
1943/*
1944 * Table of function pointers for the iodone-side routines for the driver-
1945 * internal layering mechanism.  The calling sequence for iodone routines
1946 * uses a decrementing table index, so the last routine called in a chain
1947 * must be at the lowest array index location for that chain.  The last
1948 * routine for each chain must be either sd_buf_iodone() (for buf(9S) IOs)
1949 * or sd_uscsi_iodone() (for uscsi IOs).  Other than this, the ordering
1950 * of the functions in an iodone side chain must correspond to the ordering
1951 * of the iostart routines for that chain.  Note that there is no iodone
1952 * side routine that corresponds to sd_core_iostart(), so there is no
1953 * entry in the table for this.
1954 */
1955
1956static sd_chain_t sd_iodone_chain[] = {
1957
1958	/* Chain for buf IO for disk drive targets (PM enabled) */
1959	sd_buf_iodone,			/* Index: 0 */
1960	sd_mapblockaddr_iodone,		/* Index: 1 */
1961	sd_pm_iodone,			/* Index: 2 */
1962
1963	/* Chain for buf IO for disk drive targets (PM disabled) */
1964	sd_buf_iodone,			/* Index: 3 */
1965	sd_mapblockaddr_iodone,		/* Index: 4 */
1966
1967	/*
1968	 * Chain for buf IO for removable-media or large sector size
1969	 * disk drive targets with RMW needed (PM enabled)
1970	 */
1971	sd_buf_iodone,			/* Index: 5 */
1972	sd_mapblockaddr_iodone,		/* Index: 6 */
1973	sd_mapblocksize_iodone,		/* Index: 7 */
1974	sd_pm_iodone,			/* Index: 8 */
1975
1976	/*
1977	 * Chain for buf IO for removable-media or large sector size
1978	 * disk drive targets with RMW needed (PM disabled)
1979	 */
1980	sd_buf_iodone,			/* Index: 9 */
1981	sd_mapblockaddr_iodone,		/* Index: 10 */
1982	sd_mapblocksize_iodone,		/* Index: 11 */
1983
1984	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
1985	sd_buf_iodone,			/* Index: 12 */
1986	sd_mapblockaddr_iodone,		/* Index: 13 */
1987	sd_checksum_iodone,		/* Index: 14 */
1988	sd_pm_iodone,			/* Index: 15 */
1989
1990	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
1991	sd_buf_iodone,			/* Index: 16 */
1992	sd_mapblockaddr_iodone,		/* Index: 17 */
1993	sd_checksum_iodone,		/* Index: 18 */
1994
1995	/* Chain for USCSI commands (non-checksum targets) */
1996	sd_uscsi_iodone,		/* Index: 19 */
1997	sd_pm_iodone,			/* Index: 20 */
1998
1999	/* Chain for USCSI commands (checksum targets) */
2000	sd_uscsi_iodone,		/* Index: 21 */
2001	sd_checksum_uscsi_iodone,	/* Index: 22 */
2002	sd_pm_iodone,			/* Index: 22 */
2003
2004	/* Chain for "direct" USCSI commands (all targets) */
2005	sd_uscsi_iodone,		/* Index: 24 */
2006
2007	/* Chain for "direct priority" USCSI commands (all targets) */
2008	sd_uscsi_iodone,		/* Index: 25 */
2009
2010	/*
2011	 * Chain for buf IO for large sector size disk drive targets
2012	 * with checksumming (PM enabled)
2013	 */
2014	sd_buf_iodone,			/* Index: 26 */
2015	sd_mapblockaddr_iodone,		/* Index: 27 */
2016	sd_mapblocksize_iodone,		/* Index: 28 */
2017	sd_checksum_iodone,		/* Index: 29 */
2018	sd_pm_iodone,			/* Index: 30 */
2019
2020	/*
2021	 * Chain for buf IO for large sector size disk drive targets
2022	 * with checksumming (PM disabled)
2023	 */
2024	sd_buf_iodone,			/* Index: 31 */
2025	sd_mapblockaddr_iodone,		/* Index: 32 */
2026	sd_mapblocksize_iodone,		/* Index: 33 */
2027	sd_checksum_iodone,		/* Index: 34 */
2028};
2029
2030
2031/*
2032 * Macros to locate the "first" function in the sd_iodone_chain[] array for
2033 * each iodone-side chain. These are located by the array index, but as the
2034 * iodone side functions are called in a decrementing-index order, the
2035 * highest index number in each chain must be specified (as these correspond
2036 * to the first function in the iodone chain that will be called by the core
2037 * at IO completion time).
2038 */
2039
2040#define	SD_CHAIN_DISK_IODONE			2
2041#define	SD_CHAIN_DISK_IODONE_NO_PM		4
2042#define	SD_CHAIN_RMMEDIA_IODONE			8
2043#define	SD_CHAIN_MSS_DISK_IODONE		8
2044#define	SD_CHAIN_RMMEDIA_IODONE_NO_PM		11
2045#define	SD_CHAIN_MSS_DISK_IODONE_NO_PM		11
2046#define	SD_CHAIN_CHKSUM_IODONE			15
2047#define	SD_CHAIN_CHKSUM_IODONE_NO_PM		18
2048#define	SD_CHAIN_USCSI_CMD_IODONE		20
2049#define	SD_CHAIN_USCSI_CHKSUM_IODONE		22
2050#define	SD_CHAIN_DIRECT_CMD_IODONE		24
2051#define	SD_CHAIN_PRIORITY_CMD_IODONE		25
2052#define	SD_CHAIN_MSS_CHKSUM_IODONE		30
2053#define	SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM	34
2054
2055
2056
2057/*
2058 * Array to map a layering chain index to the appropriate initpkt routine.
2059 * The redundant entries are present so that the index used for accessing
2060 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2061 * with this table as well.
2062 */
2063typedef int (*sd_initpkt_t)(struct buf *, struct scsi_pkt **);
2064
2065static sd_initpkt_t	sd_initpkt_map[] = {
2066
2067	/* Chain for buf IO for disk drive targets (PM enabled) */
2068	sd_initpkt_for_buf,		/* Index: 0 */
2069	sd_initpkt_for_buf,		/* Index: 1 */
2070	sd_initpkt_for_buf,		/* Index: 2 */
2071
2072	/* Chain for buf IO for disk drive targets (PM disabled) */
2073	sd_initpkt_for_buf,		/* Index: 3 */
2074	sd_initpkt_for_buf,		/* Index: 4 */
2075
2076	/*
2077	 * Chain for buf IO for removable-media or large sector size
2078	 * disk drive targets (PM enabled)
2079	 */
2080	sd_initpkt_for_buf,		/* Index: 5 */
2081	sd_initpkt_for_buf,		/* Index: 6 */
2082	sd_initpkt_for_buf,		/* Index: 7 */
2083	sd_initpkt_for_buf,		/* Index: 8 */
2084
2085	/*
2086	 * Chain for buf IO for removable-media or large sector size
2087	 * disk drive targets (PM disabled)
2088	 */
2089	sd_initpkt_for_buf,		/* Index: 9 */
2090	sd_initpkt_for_buf,		/* Index: 10 */
2091	sd_initpkt_for_buf,		/* Index: 11 */
2092
2093	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2094	sd_initpkt_for_buf,		/* Index: 12 */
2095	sd_initpkt_for_buf,		/* Index: 13 */
2096	sd_initpkt_for_buf,		/* Index: 14 */
2097	sd_initpkt_for_buf,		/* Index: 15 */
2098
2099	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2100	sd_initpkt_for_buf,		/* Index: 16 */
2101	sd_initpkt_for_buf,		/* Index: 17 */
2102	sd_initpkt_for_buf,		/* Index: 18 */
2103
2104	/* Chain for USCSI commands (non-checksum targets) */
2105	sd_initpkt_for_uscsi,		/* Index: 19 */
2106	sd_initpkt_for_uscsi,		/* Index: 20 */
2107
2108	/* Chain for USCSI commands (checksum targets) */
2109	sd_initpkt_for_uscsi,		/* Index: 21 */
2110	sd_initpkt_for_uscsi,		/* Index: 22 */
2111	sd_initpkt_for_uscsi,		/* Index: 22 */
2112
2113	/* Chain for "direct" USCSI commands (all targets) */
2114	sd_initpkt_for_uscsi,		/* Index: 24 */
2115
2116	/* Chain for "direct priority" USCSI commands (all targets) */
2117	sd_initpkt_for_uscsi,		/* Index: 25 */
2118
2119	/*
2120	 * Chain for buf IO for large sector size disk drive targets
2121	 * with checksumming (PM enabled)
2122	 */
2123	sd_initpkt_for_buf,		/* Index: 26 */
2124	sd_initpkt_for_buf,		/* Index: 27 */
2125	sd_initpkt_for_buf,		/* Index: 28 */
2126	sd_initpkt_for_buf,		/* Index: 29 */
2127	sd_initpkt_for_buf,		/* Index: 30 */
2128
2129	/*
2130	 * Chain for buf IO for large sector size disk drive targets
2131	 * with checksumming (PM disabled)
2132	 */
2133	sd_initpkt_for_buf,		/* Index: 31 */
2134	sd_initpkt_for_buf,		/* Index: 32 */
2135	sd_initpkt_for_buf,		/* Index: 33 */
2136	sd_initpkt_for_buf,		/* Index: 34 */
2137};
2138
2139
2140/*
2141 * Array to map a layering chain index to the appropriate destroypktpkt routine.
2142 * The redundant entries are present so that the index used for accessing
2143 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2144 * with this table as well.
2145 */
2146typedef void (*sd_destroypkt_t)(struct buf *);
2147
2148static sd_destroypkt_t	sd_destroypkt_map[] = {
2149
2150	/* Chain for buf IO for disk drive targets (PM enabled) */
2151	sd_destroypkt_for_buf,		/* Index: 0 */
2152	sd_destroypkt_for_buf,		/* Index: 1 */
2153	sd_destroypkt_for_buf,		/* Index: 2 */
2154
2155	/* Chain for buf IO for disk drive targets (PM disabled) */
2156	sd_destroypkt_for_buf,		/* Index: 3 */
2157	sd_destroypkt_for_buf,		/* Index: 4 */
2158
2159	/*
2160	 * Chain for buf IO for removable-media or large sector size
2161	 * disk drive targets (PM enabled)
2162	 */
2163	sd_destroypkt_for_buf,		/* Index: 5 */
2164	sd_destroypkt_for_buf,		/* Index: 6 */
2165	sd_destroypkt_for_buf,		/* Index: 7 */
2166	sd_destroypkt_for_buf,		/* Index: 8 */
2167
2168	/*
2169	 * Chain for buf IO for removable-media or large sector size
2170	 * disk drive targets (PM disabled)
2171	 */
2172	sd_destroypkt_for_buf,		/* Index: 9 */
2173	sd_destroypkt_for_buf,		/* Index: 10 */
2174	sd_destroypkt_for_buf,		/* Index: 11 */
2175
2176	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2177	sd_destroypkt_for_buf,		/* Index: 12 */
2178	sd_destroypkt_for_buf,		/* Index: 13 */
2179	sd_destroypkt_for_buf,		/* Index: 14 */
2180	sd_destroypkt_for_buf,		/* Index: 15 */
2181
2182	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2183	sd_destroypkt_for_buf,		/* Index: 16 */
2184	sd_destroypkt_for_buf,		/* Index: 17 */
2185	sd_destroypkt_for_buf,		/* Index: 18 */
2186
2187	/* Chain for USCSI commands (non-checksum targets) */
2188	sd_destroypkt_for_uscsi,	/* Index: 19 */
2189	sd_destroypkt_for_uscsi,	/* Index: 20 */
2190
2191	/* Chain for USCSI commands (checksum targets) */
2192	sd_destroypkt_for_uscsi,	/* Index: 21 */
2193	sd_destroypkt_for_uscsi,	/* Index: 22 */
2194	sd_destroypkt_for_uscsi,	/* Index: 22 */
2195
2196	/* Chain for "direct" USCSI commands (all targets) */
2197	sd_destroypkt_for_uscsi,	/* Index: 24 */
2198
2199	/* Chain for "direct priority" USCSI commands (all targets) */
2200	sd_destroypkt_for_uscsi,	/* Index: 25 */
2201
2202	/*
2203	 * Chain for buf IO for large sector size disk drive targets
2204	 * with checksumming (PM disabled)
2205	 */
2206	sd_destroypkt_for_buf,		/* Index: 26 */
2207	sd_destroypkt_for_buf,		/* Index: 27 */
2208	sd_destroypkt_for_buf,		/* Index: 28 */
2209	sd_destroypkt_for_buf,		/* Index: 29 */
2210	sd_destroypkt_for_buf,		/* Index: 30 */
2211
2212	/*
2213	 * Chain for buf IO for large sector size disk drive targets
2214	 * with checksumming (PM enabled)
2215	 */
2216	sd_destroypkt_for_buf,		/* Index: 31 */
2217	sd_destroypkt_for_buf,		/* Index: 32 */
2218	sd_destroypkt_for_buf,		/* Index: 33 */
2219	sd_destroypkt_for_buf,		/* Index: 34 */
2220};
2221
2222
2223
2224/*
2225 * Array to map a layering chain index to the appropriate chain "type".
2226 * The chain type indicates a specific property/usage of the chain.
2227 * The redundant entries are present so that the index used for accessing
2228 * the above sd_iostart_chain and sd_iodone_chain tables can be used directly
2229 * with this table as well.
2230 */
2231
2232#define	SD_CHAIN_NULL			0	/* for the special RQS cmd */
2233#define	SD_CHAIN_BUFIO			1	/* regular buf IO */
2234#define	SD_CHAIN_USCSI			2	/* regular USCSI commands */
2235#define	SD_CHAIN_DIRECT			3	/* uscsi, w/ bypass power mgt */
2236#define	SD_CHAIN_DIRECT_PRIORITY	4	/* uscsi, w/ bypass power mgt */
2237						/* (for error recovery) */
2238
2239static int sd_chain_type_map[] = {
2240
2241	/* Chain for buf IO for disk drive targets (PM enabled) */
2242	SD_CHAIN_BUFIO,			/* Index: 0 */
2243	SD_CHAIN_BUFIO,			/* Index: 1 */
2244	SD_CHAIN_BUFIO,			/* Index: 2 */
2245
2246	/* Chain for buf IO for disk drive targets (PM disabled) */
2247	SD_CHAIN_BUFIO,			/* Index: 3 */
2248	SD_CHAIN_BUFIO,			/* Index: 4 */
2249
2250	/*
2251	 * Chain for buf IO for removable-media or large sector size
2252	 * disk drive targets (PM enabled)
2253	 */
2254	SD_CHAIN_BUFIO,			/* Index: 5 */
2255	SD_CHAIN_BUFIO,			/* Index: 6 */
2256	SD_CHAIN_BUFIO,			/* Index: 7 */
2257	SD_CHAIN_BUFIO,			/* Index: 8 */
2258
2259	/*
2260	 * Chain for buf IO for removable-media or large sector size
2261	 * disk drive targets (PM disabled)
2262	 */
2263	SD_CHAIN_BUFIO,			/* Index: 9 */
2264	SD_CHAIN_BUFIO,			/* Index: 10 */
2265	SD_CHAIN_BUFIO,			/* Index: 11 */
2266
2267	/* Chain for buf IO for disk drives with checksumming (PM enabled) */
2268	SD_CHAIN_BUFIO,			/* Index: 12 */
2269	SD_CHAIN_BUFIO,			/* Index: 13 */
2270	SD_CHAIN_BUFIO,			/* Index: 14 */
2271	SD_CHAIN_BUFIO,			/* Index: 15 */
2272
2273	/* Chain for buf IO for disk drives with checksumming (PM disabled) */
2274	SD_CHAIN_BUFIO,			/* Index: 16 */
2275	SD_CHAIN_BUFIO,			/* Index: 17 */
2276	SD_CHAIN_BUFIO,			/* Index: 18 */
2277
2278	/* Chain for USCSI commands (non-checksum targets) */
2279	SD_CHAIN_USCSI,			/* Index: 19 */
2280	SD_CHAIN_USCSI,			/* Index: 20 */
2281
2282	/* Chain for USCSI commands (checksum targets) */
2283	SD_CHAIN_USCSI,			/* Index: 21 */
2284	SD_CHAIN_USCSI,			/* Index: 22 */
2285	SD_CHAIN_USCSI,			/* Index: 23 */
2286
2287	/* Chain for "direct" USCSI commands (all targets) */
2288	SD_CHAIN_DIRECT,		/* Index: 24 */
2289
2290	/* Chain for "direct priority" USCSI commands (all targets) */
2291	SD_CHAIN_DIRECT_PRIORITY,	/* Index: 25 */
2292
2293	/*
2294	 * Chain for buf IO for large sector size disk drive targets
2295	 * with checksumming (PM enabled)
2296	 */
2297	SD_CHAIN_BUFIO,			/* Index: 26 */
2298	SD_CHAIN_BUFIO,			/* Index: 27 */
2299	SD_CHAIN_BUFIO,			/* Index: 28 */
2300	SD_CHAIN_BUFIO,			/* Index: 29 */
2301	SD_CHAIN_BUFIO,			/* Index: 30 */
2302
2303	/*
2304	 * Chain for buf IO for large sector size disk drive targets
2305	 * with checksumming (PM disabled)
2306	 */
2307	SD_CHAIN_BUFIO,			/* Index: 31 */
2308	SD_CHAIN_BUFIO,			/* Index: 32 */
2309	SD_CHAIN_BUFIO,			/* Index: 33 */
2310	SD_CHAIN_BUFIO,			/* Index: 34 */
2311};
2312
2313
2314/* Macro to return TRUE if the IO has come from the sd_buf_iostart() chain. */
2315#define	SD_IS_BUFIO(xp)			\
2316	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_BUFIO)
2317
2318/* Macro to return TRUE if the IO has come from the "direct priority" chain. */
2319#define	SD_IS_DIRECT_PRIORITY(xp)	\
2320	(sd_chain_type_map[(xp)->xb_chain_iostart] == SD_CHAIN_DIRECT_PRIORITY)
2321
2322
2323
2324/*
2325 * Struct, array, and macros to map a specific chain to the appropriate
2326 * layering indexes in the sd_iostart_chain[] and sd_iodone_chain[] arrays.
2327 *
2328 * The sd_chain_index_map[] array is used at attach time to set the various
2329 * un_xxx_chain type members of the sd_lun softstate to the specific layering
2330 * chain to be used with the instance. This allows different instances to use
2331 * different chain for buf IO, uscsi IO, etc.. Also, since the xb_chain_iostart
2332 * and xb_chain_iodone index values in the sd_xbuf are initialized to these
2333 * values at sd_xbuf init time, this allows (1) layering chains may be changed
2334 * dynamically & without the use of locking; and (2) a layer may update the
2335 * xb_chain_io[start|done] member in a given xbuf with its current index value,
2336 * to allow for deferred processing of an IO within the same chain from a
2337 * different execution context.
2338 */
2339
2340struct sd_chain_index {
2341	int	sci_iostart_index;
2342	int	sci_iodone_index;
2343};
2344
2345static struct sd_chain_index	sd_chain_index_map[] = {
2346	{ SD_CHAIN_DISK_IOSTART,		SD_CHAIN_DISK_IODONE },
2347	{ SD_CHAIN_DISK_IOSTART_NO_PM,		SD_CHAIN_DISK_IODONE_NO_PM },
2348	{ SD_CHAIN_RMMEDIA_IOSTART,		SD_CHAIN_RMMEDIA_IODONE },
2349	{ SD_CHAIN_RMMEDIA_IOSTART_NO_PM,	SD_CHAIN_RMMEDIA_IODONE_NO_PM },
2350	{ SD_CHAIN_CHKSUM_IOSTART,		SD_CHAIN_CHKSUM_IODONE },
2351	{ SD_CHAIN_CHKSUM_IOSTART_NO_PM,	SD_CHAIN_CHKSUM_IODONE_NO_PM },
2352	{ SD_CHAIN_USCSI_CMD_IOSTART,		SD_CHAIN_USCSI_CMD_IODONE },
2353	{ SD_CHAIN_USCSI_CHKSUM_IOSTART,	SD_CHAIN_USCSI_CHKSUM_IODONE },
2354	{ SD_CHAIN_DIRECT_CMD_IOSTART,		SD_CHAIN_DIRECT_CMD_IODONE },
2355	{ SD_CHAIN_PRIORITY_CMD_IOSTART,	SD_CHAIN_PRIORITY_CMD_IODONE },
2356	{ SD_CHAIN_MSS_CHKSUM_IOSTART,		SD_CHAIN_MSS_CHKSUM_IODONE },
2357	{ SD_CHAIN_MSS_CHKSUM_IOSTART_NO_PM, SD_CHAIN_MSS_CHKSUM_IODONE_NO_PM },
2358
2359};
2360
2361
2362/*
2363 * The following are indexes into the sd_chain_index_map[] array.
2364 */
2365
2366/* un->un_buf_chain_type must be set to one of these */
2367#define	SD_CHAIN_INFO_DISK		0
2368#define	SD_CHAIN_INFO_DISK_NO_PM	1
2369#define	SD_CHAIN_INFO_RMMEDIA		2
2370#define	SD_CHAIN_INFO_MSS_DISK		2
2371#define	SD_CHAIN_INFO_RMMEDIA_NO_PM	3
2372#define	SD_CHAIN_INFO_MSS_DSK_NO_PM	3
2373#define	SD_CHAIN_INFO_CHKSUM		4
2374#define	SD_CHAIN_INFO_CHKSUM_NO_PM	5
2375#define	SD_CHAIN_INFO_MSS_DISK_CHKSUM	10
2376#define	SD_CHAIN_INFO_MSS_DISK_CHKSUM_NO_PM	11
2377
2378/* un->un_uscsi_chain_type must be set to one of these */
2379#define	SD_CHAIN_INFO_USCSI_CMD		6
2380/* USCSI with PM disabled is the same as DIRECT */
2381#define	SD_CHAIN_INFO_USCSI_CMD_NO_PM	8
2382#define	SD_CHAIN_INFO_USCSI_CHKSUM	7
2383
2384/* un->un_direct_chain_type must be set to one of these */
2385#define	SD_CHAIN_INFO_DIRECT_CMD	8
2386
2387/* un->un_priority_chain_type must be set to one of these */
2388#define	SD_CHAIN_INFO_PRIORITY_CMD	9
2389
2390/* size for devid inquiries */
2391#define	MAX_INQUIRY_SIZE		0xF0
2392
2393/*
2394 * Macros used by functions to pass a given buf(9S) struct along to the
2395 * next function in the layering chain for further processing.
2396 *
2397 * In the following macros, passing more than three arguments to the called
2398 * routines causes the optimizer for the SPARC compiler to stop doing tail
2399 * call elimination which results in significant performance degradation.
2400 */
2401#define	SD_BEGIN_IOSTART(index, un, bp)	\
2402	((*(sd_iostart_chain[index]))(index, un, bp))
2403
2404#define	SD_BEGIN_IODONE(index, un, bp)	\
2405	((*(sd_iodone_chain[index]))(index, un, bp))
2406
2407#define	SD_NEXT_IOSTART(index, un, bp)				\
2408	((*(sd_iostart_chain[(index) + 1]))((index) + 1, un, bp))
2409
2410#define	SD_NEXT_IODONE(index, un, bp)				\
2411	((*(sd_iodone_chain[(index) - 1]))((index) - 1, un, bp))
2412
2413/*
2414 *    Function: _init
2415 *
2416 * Description: This is the driver _init(9E) entry point.
2417 *
2418 * Return Code: Returns the value from mod_install(9F) or
2419 *		ddi_soft_state_init(9F) as appropriate.
2420 *
2421 *     Context: Called when driver module loaded.
2422 */
2423
2424int
2425_init(void)
2426{
2427	int	err;
2428
2429	/* establish driver name from module name */
2430	sd_label = (char *)mod_modname(&modlinkage);
2431
2432	err = ddi_soft_state_init(&sd_state, sizeof (struct sd_lun),
2433	    SD_MAXUNIT);
2434	if (err != 0) {
2435		return (err);
2436	}
2437
2438	mutex_init(&sd_detach_mutex, NULL, MUTEX_DRIVER, NULL);
2439	mutex_init(&sd_log_mutex,    NULL, MUTEX_DRIVER, NULL);
2440	mutex_init(&sd_label_mutex,  NULL, MUTEX_DRIVER, NULL);
2441
2442	mutex_init(&sd_tr.srq_resv_reclaim_mutex, NULL, MUTEX_DRIVER, NULL);
2443	cv_init(&sd_tr.srq_resv_reclaim_cv, NULL, CV_DRIVER, NULL);
2444	cv_init(&sd_tr.srq_inprocess_cv, NULL, CV_DRIVER, NULL);
2445
2446	/*
2447	 * it's ok to init here even for fibre device
2448	 */
2449	sd_scsi_probe_cache_init();
2450
2451	sd_scsi_target_lun_init();
2452
2453	/*
2454	 * Creating taskq before mod_install ensures that all callers (threads)
2455	 * that enter the module after a successful mod_install encounter
2456	 * a valid taskq.
2457	 */
2458	sd_taskq_create();
2459
2460	err = mod_install(&modlinkage);
2461	if (err != 0) {
2462		/* delete taskq if install fails */
2463		sd_taskq_delete();
2464
2465		mutex_destroy(&sd_detach_mutex);
2466		mutex_destroy(&sd_log_mutex);
2467		mutex_destroy(&sd_label_mutex);
2468
2469		mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2470		cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2471		cv_destroy(&sd_tr.srq_inprocess_cv);
2472
2473		sd_scsi_probe_cache_fini();
2474
2475		sd_scsi_target_lun_fini();
2476
2477		ddi_soft_state_fini(&sd_state);
2478
2479		return (err);
2480	}
2481
2482	return (err);
2483}
2484
2485
2486/*
2487 *    Function: _fini
2488 *
2489 * Description: This is the driver _fini(9E) entry point.
2490 *
2491 * Return Code: Returns the value from mod_remove(9F)
2492 *
2493 *     Context: Called when driver module is unloaded.
2494 */
2495
2496int
2497_fini(void)
2498{
2499	int err;
2500
2501	if ((err = mod_remove(&modlinkage)) != 0) {
2502		return (err);
2503	}
2504
2505	sd_taskq_delete();
2506
2507	mutex_destroy(&sd_detach_mutex);
2508	mutex_destroy(&sd_log_mutex);
2509	mutex_destroy(&sd_label_mutex);
2510	mutex_destroy(&sd_tr.srq_resv_reclaim_mutex);
2511
2512	sd_scsi_probe_cache_fini();
2513
2514	sd_scsi_target_lun_fini();
2515
2516	cv_destroy(&sd_tr.srq_resv_reclaim_cv);
2517	cv_destroy(&sd_tr.srq_inprocess_cv);
2518
2519	ddi_soft_state_fini(&sd_state);
2520
2521	return (err);
2522}
2523
2524
2525/*
2526 *    Function: _info
2527 *
2528 * Description: This is the driver _info(9E) entry point.
2529 *
2530 *   Arguments: modinfop - pointer to the driver modinfo structure
2531 *
2532 * Return Code: Returns the value from mod_info(9F).
2533 *
2534 *     Context: Kernel thread context
2535 */
2536
2537int
2538_info(struct modinfo *modinfop)
2539{
2540	return (mod_info(&modlinkage, modinfop));
2541}
2542
2543
2544/*
2545 * The following routines implement the driver message logging facility.
2546 * They provide component- and level- based debug output filtering.
2547 * Output may also be restricted to messages for a single instance by
2548 * specifying a soft state pointer in sd_debug_un. If sd_debug_un is set
2549 * to NULL, then messages for all instances are printed.
2550 *
2551 * These routines have been cloned from each other due to the language
2552 * constraints of macros and variable argument list processing.
2553 */
2554
2555
2556/*
2557 *    Function: sd_log_err
2558 *
2559 * Description: This routine is called by the SD_ERROR macro for debug
2560 *		logging of error conditions.
2561 *
2562 *   Arguments: comp - driver component being logged
2563 *		dev  - pointer to driver info structure
2564 *		fmt  - error string and format to be logged
2565 */
2566
2567static void
2568sd_log_err(uint_t comp, struct sd_lun *un, const char *fmt, ...)
2569{
2570	va_list		ap;
2571	dev_info_t	*dev;
2572
2573	ASSERT(un != NULL);
2574	dev = SD_DEVINFO(un);
2575	ASSERT(dev != NULL);
2576
2577	/*
2578	 * Filter messages based on the global component and level masks.
2579	 * Also print if un matches the value of sd_debug_un, or if
2580	 * sd_debug_un is set to NULL.
2581	 */
2582	if ((sd_component_mask & comp) && (sd_level_mask & SD_LOGMASK_ERROR) &&
2583	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2584		mutex_enter(&sd_log_mutex);
2585		va_start(ap, fmt);
2586		(void) vsprintf(sd_log_buf, fmt, ap);
2587		va_end(ap);
2588		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2589		mutex_exit(&sd_log_mutex);
2590	}
2591#ifdef SD_FAULT_INJECTION
2592	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2593	if (un->sd_injection_mask & comp) {
2594		mutex_enter(&sd_log_mutex);
2595		va_start(ap, fmt);
2596		(void) vsprintf(sd_log_buf, fmt, ap);
2597		va_end(ap);
2598		sd_injection_log(sd_log_buf, un);
2599		mutex_exit(&sd_log_mutex);
2600	}
2601#endif
2602}
2603
2604
2605/*
2606 *    Function: sd_log_info
2607 *
2608 * Description: This routine is called by the SD_INFO macro for debug
2609 *		logging of general purpose informational conditions.
2610 *
2611 *   Arguments: comp - driver component being logged
2612 *		dev  - pointer to driver info structure
2613 *		fmt  - info string and format to be logged
2614 */
2615
2616static void
2617sd_log_info(uint_t component, struct sd_lun *un, const char *fmt, ...)
2618{
2619	va_list		ap;
2620	dev_info_t	*dev;
2621
2622	ASSERT(un != NULL);
2623	dev = SD_DEVINFO(un);
2624	ASSERT(dev != NULL);
2625
2626	/*
2627	 * Filter messages based on the global component and level masks.
2628	 * Also print if un matches the value of sd_debug_un, or if
2629	 * sd_debug_un is set to NULL.
2630	 */
2631	if ((sd_component_mask & component) &&
2632	    (sd_level_mask & SD_LOGMASK_INFO) &&
2633	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2634		mutex_enter(&sd_log_mutex);
2635		va_start(ap, fmt);
2636		(void) vsprintf(sd_log_buf, fmt, ap);
2637		va_end(ap);
2638		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2639		mutex_exit(&sd_log_mutex);
2640	}
2641#ifdef SD_FAULT_INJECTION
2642	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2643	if (un->sd_injection_mask & component) {
2644		mutex_enter(&sd_log_mutex);
2645		va_start(ap, fmt);
2646		(void) vsprintf(sd_log_buf, fmt, ap);
2647		va_end(ap);
2648		sd_injection_log(sd_log_buf, un);
2649		mutex_exit(&sd_log_mutex);
2650	}
2651#endif
2652}
2653
2654
2655/*
2656 *    Function: sd_log_trace
2657 *
2658 * Description: This routine is called by the SD_TRACE macro for debug
2659 *		logging of trace conditions (i.e. function entry/exit).
2660 *
2661 *   Arguments: comp - driver component being logged
2662 *		dev  - pointer to driver info structure
2663 *		fmt  - trace string and format to be logged
2664 */
2665
2666static void
2667sd_log_trace(uint_t component, struct sd_lun *un, const char *fmt, ...)
2668{
2669	va_list		ap;
2670	dev_info_t	*dev;
2671
2672	ASSERT(un != NULL);
2673	dev = SD_DEVINFO(un);
2674	ASSERT(dev != NULL);
2675
2676	/*
2677	 * Filter messages based on the global component and level masks.
2678	 * Also print if un matches the value of sd_debug_un, or if
2679	 * sd_debug_un is set to NULL.
2680	 */
2681	if ((sd_component_mask & component) &&
2682	    (sd_level_mask & SD_LOGMASK_TRACE) &&
2683	    ((sd_debug_un == NULL) || (sd_debug_un == un))) {
2684		mutex_enter(&sd_log_mutex);
2685		va_start(ap, fmt);
2686		(void) vsprintf(sd_log_buf, fmt, ap);
2687		va_end(ap);
2688		scsi_log(dev, sd_label, CE_CONT, "%s", sd_log_buf);
2689		mutex_exit(&sd_log_mutex);
2690	}
2691#ifdef SD_FAULT_INJECTION
2692	_NOTE(DATA_READABLE_WITHOUT_LOCK(sd_lun::sd_injection_mask));
2693	if (un->sd_injection_mask & component) {
2694		mutex_enter(&sd_log_mutex);
2695		va_start(ap, fmt);
2696		(void) vsprintf(sd_log_buf, fmt, ap);
2697		va_end(ap);
2698		sd_injection_log(sd_log_buf, un);
2699		mutex_exit(&sd_log_mutex);
2700	}
2701#endif
2702}
2703
2704
2705/*
2706 *    Function: sdprobe
2707 *
2708 * Description: This is the driver probe(9e) entry point function.
2709 *
2710 *   Arguments: devi - opaque device info handle
2711 *
2712 * Return Code: DDI_PROBE_SUCCESS: If the probe was successful.
2713 *              DDI_PROBE_FAILURE: If the probe failed.
2714 *              DDI_PROBE_PARTIAL: If the instance is not present now,
2715 *				   but may be present in the future.
2716 */
2717
2718static int
2719sdprobe(dev_info_t *devi)
2720{
2721	struct scsi_device	*devp;
2722	int			rval;
2723	int			instance = ddi_get_instance(devi);
2724
2725	/*
2726	 * if it wasn't for pln, sdprobe could actually be nulldev
2727	 * in the "__fibre" case.
2728	 */
2729	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
2730		return (DDI_PROBE_DONTCARE);
2731	}
2732
2733	devp = ddi_get_driver_private(devi);
2734
2735	if (devp == NULL) {
2736		/* Ooops... nexus driver is mis-configured... */
2737		return (DDI_PROBE_FAILURE);
2738	}
2739
2740	if (ddi_get_soft_state(sd_state, instance) != NULL) {
2741		return (DDI_PROBE_PARTIAL);
2742	}
2743
2744	/*
2745	 * Call the SCSA utility probe routine to see if we actually
2746	 * have a target at this SCSI nexus.
2747	 */
2748	switch (sd_scsi_probe_with_cache(devp, NULL_FUNC)) {
2749	case SCSIPROBE_EXISTS:
2750		switch (devp->sd_inq->inq_dtype) {
2751		case DTYPE_DIRECT:
2752			rval = DDI_PROBE_SUCCESS;
2753			break;
2754		case DTYPE_RODIRECT:
2755			/* CDs etc. Can be removable media */
2756			rval = DDI_PROBE_SUCCESS;
2757			break;
2758		case DTYPE_OPTICAL:
2759			/*
2760			 * Rewritable optical driver HP115AA
2761			 * Can also be removable media
2762			 */
2763
2764			/*
2765			 * Do not attempt to bind to  DTYPE_OPTICAL if
2766			 * pre solaris 9 sparc sd behavior is required
2767			 *
2768			 * If first time through and sd_dtype_optical_bind
2769			 * has not been set in /etc/system check properties
2770			 */
2771
2772			if (sd_dtype_optical_bind  < 0) {
2773				sd_dtype_optical_bind = ddi_prop_get_int
2774				    (DDI_DEV_T_ANY, devi, 0,
2775				    "optical-device-bind", 1);
2776			}
2777
2778			if (sd_dtype_optical_bind == 0) {
2779				rval = DDI_PROBE_FAILURE;
2780			} else {
2781				rval = DDI_PROBE_SUCCESS;
2782			}
2783			break;
2784
2785		case DTYPE_NOTPRESENT:
2786		default:
2787			rval = DDI_PROBE_FAILURE;
2788			break;
2789		}
2790		break;
2791	default:
2792		rval = DDI_PROBE_PARTIAL;
2793		break;
2794	}
2795
2796	/*
2797	 * This routine checks for resource allocation prior to freeing,
2798	 * so it will take care of the "smart probing" case where a
2799	 * scsi_probe() may or may not have been issued and will *not*
2800	 * free previously-freed resources.
2801	 */
2802	scsi_unprobe(devp);
2803	return (rval);
2804}
2805
2806
2807/*
2808 *    Function: sdinfo
2809 *
2810 * Description: This is the driver getinfo(9e) entry point function.
2811 *		Given the device number, return the devinfo pointer from
2812 *		the scsi_device structure or the instance number
2813 *		associated with the dev_t.
2814 *
2815 *   Arguments: dip     - pointer to device info structure
2816 *		infocmd - command argument (DDI_INFO_DEVT2DEVINFO,
2817 *			  DDI_INFO_DEVT2INSTANCE)
2818 *		arg     - driver dev_t
2819 *		resultp - user buffer for request response
2820 *
2821 * Return Code: DDI_SUCCESS
2822 *              DDI_FAILURE
2823 */
2824/* ARGSUSED */
2825static int
2826sdinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
2827{
2828	struct sd_lun	*un;
2829	dev_t		dev;
2830	int		instance;
2831	int		error;
2832
2833	switch (infocmd) {
2834	case DDI_INFO_DEVT2DEVINFO:
2835		dev = (dev_t)arg;
2836		instance = SDUNIT(dev);
2837		if ((un = ddi_get_soft_state(sd_state, instance)) == NULL) {
2838			return (DDI_FAILURE);
2839		}
2840		*result = (void *) SD_DEVINFO(un);
2841		error = DDI_SUCCESS;
2842		break;
2843	case DDI_INFO_DEVT2INSTANCE:
2844		dev = (dev_t)arg;
2845		instance = SDUNIT(dev);
2846		*result = (void *)(uintptr_t)instance;
2847		error = DDI_SUCCESS;
2848		break;
2849	default:
2850		error = DDI_FAILURE;
2851	}
2852	return (error);
2853}
2854
2855/*
2856 *    Function: sd_prop_op
2857 *
2858 * Description: This is the driver prop_op(9e) entry point function.
2859 *		Return the number of blocks for the partition in question
2860 *		or forward the request to the property facilities.
2861 *
2862 *   Arguments: dev       - device number
2863 *		dip       - pointer to device info structure
2864 *		prop_op   - property operator
2865 *		mod_flags - DDI_PROP_DONTPASS, don't pass to parent
2866 *		name      - pointer to property name
2867 *		valuep    - pointer or address of the user buffer
2868 *		lengthp   - property length
2869 *
2870 * Return Code: DDI_PROP_SUCCESS
2871 *              DDI_PROP_NOT_FOUND
2872 *              DDI_PROP_UNDEFINED
2873 *              DDI_PROP_NO_MEMORY
2874 *              DDI_PROP_BUF_TOO_SMALL
2875 */
2876
2877static int
2878sd_prop_op(dev_t dev, dev_info_t *dip, ddi_prop_op_t prop_op, int mod_flags,
2879    char *name, caddr_t valuep, int *lengthp)
2880{
2881	struct sd_lun	*un;
2882
2883	if ((un = ddi_get_soft_state(sd_state, ddi_get_instance(dip))) == NULL)
2884		return (ddi_prop_op(dev, dip, prop_op, mod_flags,
2885		    name, valuep, lengthp));
2886
2887	return (cmlb_prop_op(un->un_cmlbhandle,
2888	    dev, dip, prop_op, mod_flags, name, valuep, lengthp,
2889	    SDPART(dev), (void *)SD_PATH_DIRECT));
2890}
2891
2892/*
2893 * The following functions are for smart probing:
2894 * sd_scsi_probe_cache_init()
2895 * sd_scsi_probe_cache_fini()
2896 * sd_scsi_clear_probe_cache()
2897 * sd_scsi_probe_with_cache()
2898 */
2899
2900/*
2901 *    Function: sd_scsi_probe_cache_init
2902 *
2903 * Description: Initializes the probe response cache mutex and head pointer.
2904 *
2905 *     Context: Kernel thread context
2906 */
2907
2908static void
2909sd_scsi_probe_cache_init(void)
2910{
2911	mutex_init(&sd_scsi_probe_cache_mutex, NULL, MUTEX_DRIVER, NULL);
2912	sd_scsi_probe_cache_head = NULL;
2913}
2914
2915
2916/*
2917 *    Function: sd_scsi_probe_cache_fini
2918 *
2919 * Description: Frees all resources associated with the probe response cache.
2920 *
2921 *     Context: Kernel thread context
2922 */
2923
2924static void
2925sd_scsi_probe_cache_fini(void)
2926{
2927	struct sd_scsi_probe_cache *cp;
2928	struct sd_scsi_probe_cache *ncp;
2929
2930	/* Clean up our smart probing linked list */
2931	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = ncp) {
2932		ncp = cp->next;
2933		kmem_free(cp, sizeof (struct sd_scsi_probe_cache));
2934	}
2935	sd_scsi_probe_cache_head = NULL;
2936	mutex_destroy(&sd_scsi_probe_cache_mutex);
2937}
2938
2939
2940/*
2941 *    Function: sd_scsi_clear_probe_cache
2942 *
2943 * Description: This routine clears the probe response cache. This is
2944 *		done when open() returns ENXIO so that when deferred
2945 *		attach is attempted (possibly after a device has been
2946 *		turned on) we will retry the probe. Since we don't know
2947 *		which target we failed to open, we just clear the
2948 *		entire cache.
2949 *
2950 *     Context: Kernel thread context
2951 */
2952
2953static void
2954sd_scsi_clear_probe_cache(void)
2955{
2956	struct sd_scsi_probe_cache	*cp;
2957	int				i;
2958
2959	mutex_enter(&sd_scsi_probe_cache_mutex);
2960	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
2961		/*
2962		 * Reset all entries to SCSIPROBE_EXISTS.  This will
2963		 * force probing to be performed the next time
2964		 * sd_scsi_probe_with_cache is called.
2965		 */
2966		for (i = 0; i < NTARGETS_WIDE; i++) {
2967			cp->cache[i] = SCSIPROBE_EXISTS;
2968		}
2969	}
2970	mutex_exit(&sd_scsi_probe_cache_mutex);
2971}
2972
2973
2974/*
2975 *    Function: sd_scsi_probe_with_cache
2976 *
2977 * Description: This routine implements support for a scsi device probe
2978 *		with cache. The driver maintains a cache of the target
2979 *		responses to scsi probes. If we get no response from a
2980 *		target during a probe inquiry, we remember that, and we
2981 *		avoid additional calls to scsi_probe on non-zero LUNs
2982 *		on the same target until the cache is cleared. By doing
2983 *		so we avoid the 1/4 sec selection timeout for nonzero
2984 *		LUNs. lun0 of a target is always probed.
2985 *
2986 *   Arguments: devp     - Pointer to a scsi_device(9S) structure
2987 *              waitfunc - indicates what the allocator routines should
2988 *			   do when resources are not available. This value
2989 *			   is passed on to scsi_probe() when that routine
2990 *			   is called.
2991 *
2992 * Return Code: SCSIPROBE_NORESP if a NORESP in probe response cache;
2993 *		otherwise the value returned by scsi_probe(9F).
2994 *
2995 *     Context: Kernel thread context
2996 */
2997
2998static int
2999sd_scsi_probe_with_cache(struct scsi_device *devp, int (*waitfn)())
3000{
3001	struct sd_scsi_probe_cache	*cp;
3002	dev_info_t	*pdip = ddi_get_parent(devp->sd_dev);
3003	int		lun, tgt;
3004
3005	lun = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3006	    SCSI_ADDR_PROP_LUN, 0);
3007	tgt = ddi_prop_get_int(DDI_DEV_T_ANY, devp->sd_dev, DDI_PROP_DONTPASS,
3008	    SCSI_ADDR_PROP_TARGET, -1);
3009
3010	/* Make sure caching enabled and target in range */
3011	if ((tgt < 0) || (tgt >= NTARGETS_WIDE)) {
3012		/* do it the old way (no cache) */
3013		return (scsi_probe(devp, waitfn));
3014	}
3015
3016	mutex_enter(&sd_scsi_probe_cache_mutex);
3017
3018	/* Find the cache for this scsi bus instance */
3019	for (cp = sd_scsi_probe_cache_head; cp != NULL; cp = cp->next) {
3020		if (cp->pdip == pdip) {
3021			break;
3022		}
3023	}
3024
3025	/* If we can't find a cache for this pdip, create one */
3026	if (cp == NULL) {
3027		int i;
3028
3029		cp = kmem_zalloc(sizeof (struct sd_scsi_probe_cache),
3030		    KM_SLEEP);
3031		cp->pdip = pdip;
3032		cp->next = sd_scsi_probe_cache_head;
3033		sd_scsi_probe_cache_head = cp;
3034		for (i = 0; i < NTARGETS_WIDE; i++) {
3035			cp->cache[i] = SCSIPROBE_EXISTS;
3036		}
3037	}
3038
3039	mutex_exit(&sd_scsi_probe_cache_mutex);
3040
3041	/* Recompute the cache for this target if LUN zero */
3042	if (lun == 0) {
3043		cp->cache[tgt] = SCSIPROBE_EXISTS;
3044	}
3045
3046	/* Don't probe if cache remembers a NORESP from a previous LUN. */
3047	if (cp->cache[tgt] != SCSIPROBE_EXISTS) {
3048		return (SCSIPROBE_NORESP);
3049	}
3050
3051	/* Do the actual probe; save & return the result */
3052	return (cp->cache[tgt] = scsi_probe(devp, waitfn));
3053}
3054
3055
3056/*
3057 *    Function: sd_scsi_target_lun_init
3058 *
3059 * Description: Initializes the attached lun chain mutex and head pointer.
3060 *
3061 *     Context: Kernel thread context
3062 */
3063
3064static void
3065sd_scsi_target_lun_init(void)
3066{
3067	mutex_init(&sd_scsi_target_lun_mutex, NULL, MUTEX_DRIVER, NULL);
3068	sd_scsi_target_lun_head = NULL;
3069}
3070
3071
3072/*
3073 *    Function: sd_scsi_target_lun_fini
3074 *
3075 * Description: Frees all resources associated with the attached lun
3076 *              chain
3077 *
3078 *     Context: Kernel thread context
3079 */
3080
3081static void
3082sd_scsi_target_lun_fini(void)
3083{
3084	struct sd_scsi_hba_tgt_lun	*cp;
3085	struct sd_scsi_hba_tgt_lun	*ncp;
3086
3087	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = ncp) {
3088		ncp = cp->next;
3089		kmem_free(cp, sizeof (struct sd_scsi_hba_tgt_lun));
3090	}
3091	sd_scsi_target_lun_head = NULL;
3092	mutex_destroy(&sd_scsi_target_lun_mutex);
3093}
3094
3095
3096/*
3097 *    Function: sd_scsi_get_target_lun_count
3098 *
3099 * Description: This routine will check in the attached lun chain to see
3100 *		how many luns are attached on the required SCSI controller
3101 *		and target. Currently, some capabilities like tagged queue
3102 *		are supported per target based by HBA. So all luns in a
3103 *		target have the same capabilities. Based on this assumption,
3104 *		sd should only set these capabilities once per target. This
3105 *		function is called when sd needs to decide how many luns
3106 *		already attached on a target.
3107 *
3108 *   Arguments: dip	- Pointer to the system's dev_info_t for the SCSI
3109 *			  controller device.
3110 *              target	- The target ID on the controller's SCSI bus.
3111 *
3112 * Return Code: The number of luns attached on the required target and
3113 *		controller.
3114 *		-1 if target ID is not in parallel SCSI scope or the given
3115 *		dip is not in the chain.
3116 *
3117 *     Context: Kernel thread context
3118 */
3119
3120static int
3121sd_scsi_get_target_lun_count(dev_info_t *dip, int target)
3122{
3123	struct sd_scsi_hba_tgt_lun	*cp;
3124
3125	if ((target < 0) || (target >= NTARGETS_WIDE)) {
3126		return (-1);
3127	}
3128
3129	mutex_enter(&sd_scsi_target_lun_mutex);
3130
3131	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3132		if (cp->pdip == dip) {
3133			break;
3134		}
3135	}
3136
3137	mutex_exit(&sd_scsi_target_lun_mutex);
3138
3139	if (cp == NULL) {
3140		return (-1);
3141	}
3142
3143	return (cp->nlun[target]);
3144}
3145
3146
3147/*
3148 *    Function: sd_scsi_update_lun_on_target
3149 *
3150 * Description: This routine is used to update the attached lun chain when a
3151 *		lun is attached or detached on a target.
3152 *
3153 *   Arguments: dip     - Pointer to the system's dev_info_t for the SCSI
3154 *                        controller device.
3155 *              target  - The target ID on the controller's SCSI bus.
3156 *		flag	- Indicate the lun is attached or detached.
3157 *
3158 *     Context: Kernel thread context
3159 */
3160
3161static void
3162sd_scsi_update_lun_on_target(dev_info_t *dip, int target, int flag)
3163{
3164	struct sd_scsi_hba_tgt_lun	*cp;
3165
3166	mutex_enter(&sd_scsi_target_lun_mutex);
3167
3168	for (cp = sd_scsi_target_lun_head; cp != NULL; cp = cp->next) {
3169		if (cp->pdip == dip) {
3170			break;
3171		}
3172	}
3173
3174	if ((cp == NULL) && (flag == SD_SCSI_LUN_ATTACH)) {
3175		cp = kmem_zalloc(sizeof (struct sd_scsi_hba_tgt_lun),
3176		    KM_SLEEP);
3177		cp->pdip = dip;
3178		cp->next = sd_scsi_target_lun_head;
3179		sd_scsi_target_lun_head = cp;
3180	}
3181
3182	mutex_exit(&sd_scsi_target_lun_mutex);
3183
3184	if (cp != NULL) {
3185		if (flag == SD_SCSI_LUN_ATTACH) {
3186			cp->nlun[target] ++;
3187		} else {
3188			cp->nlun[target] --;
3189		}
3190	}
3191}
3192
3193
3194/*
3195 *    Function: sd_spin_up_unit
3196 *
3197 * Description: Issues the following commands to spin-up the device:
3198 *		START STOP UNIT, and INQUIRY.
3199 *
3200 *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3201 *                      structure for this target.
3202 *
3203 * Return Code: 0 - success
3204 *		EIO - failure
3205 *		EACCES - reservation conflict
3206 *
3207 *     Context: Kernel thread context
3208 */
3209
3210static int
3211sd_spin_up_unit(sd_ssc_t *ssc)
3212{
3213	size_t	resid		= 0;
3214	int	has_conflict	= FALSE;
3215	uchar_t *bufaddr;
3216	int	status;
3217	struct sd_lun	*un;
3218
3219	ASSERT(ssc != NULL);
3220	un = ssc->ssc_un;
3221	ASSERT(un != NULL);
3222
3223	/*
3224	 * Send a throwaway START UNIT command.
3225	 *
3226	 * If we fail on this, we don't care presently what precisely
3227	 * is wrong.  EMC's arrays will also fail this with a check
3228	 * condition (0x2/0x4/0x3) if the device is "inactive," but
3229	 * we don't want to fail the attach because it may become
3230	 * "active" later.
3231	 * We don't know if power condition is supported or not at
3232	 * this stage, use START STOP bit.
3233	 */
3234	status = sd_send_scsi_START_STOP_UNIT(ssc, SD_START_STOP,
3235	    SD_TARGET_START, SD_PATH_DIRECT);
3236
3237	if (status != 0) {
3238		if (status == EACCES)
3239			has_conflict = TRUE;
3240		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3241	}
3242
3243	/*
3244	 * Send another INQUIRY command to the target. This is necessary for
3245	 * non-removable media direct access devices because their INQUIRY data
3246	 * may not be fully qualified until they are spun up (perhaps via the
3247	 * START command above).  Note: This seems to be needed for some
3248	 * legacy devices only.) The INQUIRY command should succeed even if a
3249	 * Reservation Conflict is present.
3250	 */
3251	bufaddr = kmem_zalloc(SUN_INQSIZE, KM_SLEEP);
3252
3253	if (sd_send_scsi_INQUIRY(ssc, bufaddr, SUN_INQSIZE, 0, 0, &resid)
3254	    != 0) {
3255		kmem_free(bufaddr, SUN_INQSIZE);
3256		sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
3257		return (EIO);
3258	}
3259
3260	/*
3261	 * If we got enough INQUIRY data, copy it over the old INQUIRY data.
3262	 * Note that this routine does not return a failure here even if the
3263	 * INQUIRY command did not return any data.  This is a legacy behavior.
3264	 */
3265	if ((SUN_INQSIZE - resid) >= SUN_MIN_INQLEN) {
3266		bcopy(bufaddr, SD_INQUIRY(un), SUN_INQSIZE);
3267	}
3268
3269	kmem_free(bufaddr, SUN_INQSIZE);
3270
3271	/* If we hit a reservation conflict above, tell the caller. */
3272	if (has_conflict == TRUE) {
3273		return (EACCES);
3274	}
3275
3276	return (0);
3277}
3278
3279#ifdef _LP64
3280/*
3281 *    Function: sd_enable_descr_sense
3282 *
3283 * Description: This routine attempts to select descriptor sense format
3284 *		using the Control mode page.  Devices that support 64 bit
3285 *		LBAs (for >2TB luns) should also implement descriptor
3286 *		sense data so we will call this function whenever we see
3287 *		a lun larger than 2TB.  If for some reason the device
3288 *		supports 64 bit LBAs but doesn't support descriptor sense
3289 *		presumably the mode select will fail.  Everything will
3290 *		continue to work normally except that we will not get
3291 *		complete sense data for commands that fail with an LBA
3292 *		larger than 32 bits.
3293 *
3294 *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3295 *                      structure for this target.
3296 *
3297 *     Context: Kernel thread context only
3298 */
3299
3300static void
3301sd_enable_descr_sense(sd_ssc_t *ssc)
3302{
3303	uchar_t			*header;
3304	struct mode_control_scsi3 *ctrl_bufp;
3305	size_t			buflen;
3306	size_t			bd_len;
3307	int			status;
3308	struct sd_lun		*un;
3309
3310	ASSERT(ssc != NULL);
3311	un = ssc->ssc_un;
3312	ASSERT(un != NULL);
3313
3314	/*
3315	 * Read MODE SENSE page 0xA, Control Mode Page
3316	 */
3317	buflen = MODE_HEADER_LENGTH + MODE_BLK_DESC_LENGTH +
3318	    sizeof (struct mode_control_scsi3);
3319	header = kmem_zalloc(buflen, KM_SLEEP);
3320
3321	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP0, header, buflen,
3322	    MODEPAGE_CTRL_MODE, SD_PATH_DIRECT);
3323
3324	if (status != 0) {
3325		SD_ERROR(SD_LOG_COMMON, un,
3326		    "sd_enable_descr_sense: mode sense ctrl page failed\n");
3327		goto eds_exit;
3328	}
3329
3330	/*
3331	 * Determine size of Block Descriptors in order to locate
3332	 * the mode page data. ATAPI devices return 0, SCSI devices
3333	 * should return MODE_BLK_DESC_LENGTH.
3334	 */
3335	bd_len  = ((struct mode_header *)header)->bdesc_length;
3336
3337	/* Clear the mode data length field for MODE SELECT */
3338	((struct mode_header *)header)->length = 0;
3339
3340	ctrl_bufp = (struct mode_control_scsi3 *)
3341	    (header + MODE_HEADER_LENGTH + bd_len);
3342
3343	/*
3344	 * If the page length is smaller than the expected value,
3345	 * the target device doesn't support D_SENSE. Bail out here.
3346	 */
3347	if (ctrl_bufp->mode_page.length <
3348	    sizeof (struct mode_control_scsi3) - 2) {
3349		SD_ERROR(SD_LOG_COMMON, un,
3350		    "sd_enable_descr_sense: enable D_SENSE failed\n");
3351		goto eds_exit;
3352	}
3353
3354	/*
3355	 * Clear PS bit for MODE SELECT
3356	 */
3357	ctrl_bufp->mode_page.ps = 0;
3358
3359	/*
3360	 * Set D_SENSE to enable descriptor sense format.
3361	 */
3362	ctrl_bufp->d_sense = 1;
3363
3364	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3365
3366	/*
3367	 * Use MODE SELECT to commit the change to the D_SENSE bit
3368	 */
3369	status = sd_send_scsi_MODE_SELECT(ssc, CDB_GROUP0, header,
3370	    buflen, SD_DONTSAVE_PAGE, SD_PATH_DIRECT);
3371
3372	if (status != 0) {
3373		SD_INFO(SD_LOG_COMMON, un,
3374		    "sd_enable_descr_sense: mode select ctrl page failed\n");
3375	} else {
3376		kmem_free(header, buflen);
3377		return;
3378	}
3379
3380eds_exit:
3381	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3382	kmem_free(header, buflen);
3383}
3384
3385/*
3386 *    Function: sd_reenable_dsense_task
3387 *
3388 * Description: Re-enable descriptor sense after device or bus reset
3389 *
3390 *     Context: Executes in a taskq() thread context
3391 */
3392static void
3393sd_reenable_dsense_task(void *arg)
3394{
3395	struct	sd_lun	*un = arg;
3396	sd_ssc_t	*ssc;
3397
3398	ASSERT(un != NULL);
3399
3400	ssc = sd_ssc_init(un);
3401	sd_enable_descr_sense(ssc);
3402	sd_ssc_fini(ssc);
3403}
3404#endif /* _LP64 */
3405
3406/*
3407 *    Function: sd_set_mmc_caps
3408 *
3409 * Description: This routine determines if the device is MMC compliant and if
3410 *		the device supports CDDA via a mode sense of the CDVD
3411 *		capabilities mode page. Also checks if the device is a
3412 *		dvdram writable device.
3413 *
3414 *   Arguments: ssc   - ssc contains pointer to driver soft state (unit)
3415 *                      structure for this target.
3416 *
3417 *     Context: Kernel thread context only
3418 */
3419
3420static void
3421sd_set_mmc_caps(sd_ssc_t *ssc)
3422{
3423	struct mode_header_grp2		*sense_mhp;
3424	uchar_t				*sense_page;
3425	caddr_t				buf;
3426	int				bd_len;
3427	int				status;
3428	struct uscsi_cmd		com;
3429	int				rtn;
3430	uchar_t				*out_data_rw, *out_data_hd;
3431	uchar_t				*rqbuf_rw, *rqbuf_hd;
3432	uchar_t				*out_data_gesn;
3433	int				gesn_len;
3434	struct sd_lun			*un;
3435
3436	ASSERT(ssc != NULL);
3437	un = ssc->ssc_un;
3438	ASSERT(un != NULL);
3439
3440	/*
3441	 * The flags which will be set in this function are - mmc compliant,
3442	 * dvdram writable device, cdda support. Initialize them to FALSE
3443	 * and if a capability is detected - it will be set to TRUE.
3444	 */
3445	un->un_f_mmc_cap = FALSE;
3446	un->un_f_dvdram_writable_device = FALSE;
3447	un->un_f_cfg_cdda = FALSE;
3448
3449	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3450	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3451	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, SD_PATH_DIRECT);
3452
3453	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3454
3455	if (status != 0) {
3456		/* command failed; just return */
3457		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3458		return;
3459	}
3460	/*
3461	 * If the mode sense request for the CDROM CAPABILITIES
3462	 * page (0x2A) succeeds the device is assumed to be MMC.
3463	 */
3464	un->un_f_mmc_cap = TRUE;
3465
3466	/* See if GET STATUS EVENT NOTIFICATION is supported */
3467	if (un->un_f_mmc_gesn_polling) {
3468		gesn_len = SD_GESN_HEADER_LEN + SD_GESN_MEDIA_DATA_LEN;
3469		out_data_gesn = kmem_zalloc(gesn_len, KM_SLEEP);
3470
3471		rtn = sd_send_scsi_GET_EVENT_STATUS_NOTIFICATION(ssc,
3472		    out_data_gesn, gesn_len, 1 << SD_GESN_MEDIA_CLASS);
3473
3474		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3475
3476		if ((rtn != 0) || !sd_gesn_media_data_valid(out_data_gesn)) {
3477			un->un_f_mmc_gesn_polling = FALSE;
3478			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3479			    "sd_set_mmc_caps: gesn not supported "
3480			    "%d %x %x %x %x\n", rtn,
3481			    out_data_gesn[0], out_data_gesn[1],
3482			    out_data_gesn[2], out_data_gesn[3]);
3483		}
3484
3485		kmem_free(out_data_gesn, gesn_len);
3486	}
3487
3488	/* Get to the page data */
3489	sense_mhp = (struct mode_header_grp2 *)buf;
3490	bd_len = (sense_mhp->bdesc_length_hi << 8) |
3491	    sense_mhp->bdesc_length_lo;
3492	if (bd_len > MODE_BLK_DESC_LENGTH) {
3493		/*
3494		 * We did not get back the expected block descriptor
3495		 * length so we cannot determine if the device supports
3496		 * CDDA. However, we still indicate the device is MMC
3497		 * according to the successful response to the page
3498		 * 0x2A mode sense request.
3499		 */
3500		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3501		    "sd_set_mmc_caps: Mode Sense returned "
3502		    "invalid block descriptor length\n");
3503		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3504		return;
3505	}
3506
3507	/* See if read CDDA is supported */
3508	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 +
3509	    bd_len);
3510	un->un_f_cfg_cdda = (sense_page[5] & 0x01) ? TRUE : FALSE;
3511
3512	/* See if writing DVD RAM is supported. */
3513	un->un_f_dvdram_writable_device = (sense_page[3] & 0x20) ? TRUE : FALSE;
3514	if (un->un_f_dvdram_writable_device == TRUE) {
3515		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3516		return;
3517	}
3518
3519	/*
3520	 * If the device presents DVD or CD capabilities in the mode
3521	 * page, we can return here since a RRD will not have
3522	 * these capabilities.
3523	 */
3524	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3525		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3526		return;
3527	}
3528	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3529
3530	/*
3531	 * If un->un_f_dvdram_writable_device is still FALSE,
3532	 * check for a Removable Rigid Disk (RRD).  A RRD
3533	 * device is identified by the features RANDOM_WRITABLE and
3534	 * HARDWARE_DEFECT_MANAGEMENT.
3535	 */
3536	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3537	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3538
3539	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3540	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3541	    RANDOM_WRITABLE, SD_PATH_STANDARD);
3542
3543	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3544
3545	if (rtn != 0) {
3546		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3547		kmem_free(rqbuf_rw, SENSE_LENGTH);
3548		return;
3549	}
3550
3551	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3552	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3553
3554	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3555	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3556	    HARDWARE_DEFECT_MANAGEMENT, SD_PATH_STANDARD);
3557
3558	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3559
3560	if (rtn == 0) {
3561		/*
3562		 * We have good information, check for random writable
3563		 * and hardware defect features.
3564		 */
3565		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3566		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT)) {
3567			un->un_f_dvdram_writable_device = TRUE;
3568		}
3569	}
3570
3571	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3572	kmem_free(rqbuf_rw, SENSE_LENGTH);
3573	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3574	kmem_free(rqbuf_hd, SENSE_LENGTH);
3575}
3576
3577/*
3578 *    Function: sd_check_for_writable_cd
3579 *
3580 * Description: This routine determines if the media in the device is
3581 *		writable or not. It uses the get configuration command (0x46)
3582 *		to determine if the media is writable
3583 *
3584 *   Arguments: un - driver soft state (unit) structure
3585 *              path_flag - SD_PATH_DIRECT to use the USCSI "direct"
3586 *                           chain and the normal command waitq, or
3587 *                           SD_PATH_DIRECT_PRIORITY to use the USCSI
3588 *                           "direct" chain and bypass the normal command
3589 *                           waitq.
3590 *
3591 *     Context: Never called at interrupt context.
3592 */
3593
3594static void
3595sd_check_for_writable_cd(sd_ssc_t *ssc, int path_flag)
3596{
3597	struct uscsi_cmd		com;
3598	uchar_t				*out_data;
3599	uchar_t				*rqbuf;
3600	int				rtn;
3601	uchar_t				*out_data_rw, *out_data_hd;
3602	uchar_t				*rqbuf_rw, *rqbuf_hd;
3603	struct mode_header_grp2		*sense_mhp;
3604	uchar_t				*sense_page;
3605	caddr_t				buf;
3606	int				bd_len;
3607	int				status;
3608	struct sd_lun			*un;
3609
3610	ASSERT(ssc != NULL);
3611	un = ssc->ssc_un;
3612	ASSERT(un != NULL);
3613	ASSERT(mutex_owned(SD_MUTEX(un)));
3614
3615	/*
3616	 * Initialize the writable media to false, if configuration info.
3617	 * tells us otherwise then only we will set it.
3618	 */
3619	un->un_f_mmc_writable_media = FALSE;
3620	mutex_exit(SD_MUTEX(un));
3621
3622	out_data = kmem_zalloc(SD_PROFILE_HEADER_LEN, KM_SLEEP);
3623	rqbuf = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3624
3625	rtn = sd_send_scsi_GET_CONFIGURATION(ssc, &com, rqbuf, SENSE_LENGTH,
3626	    out_data, SD_PROFILE_HEADER_LEN, path_flag);
3627
3628	if (rtn != 0)
3629		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3630
3631	mutex_enter(SD_MUTEX(un));
3632	if (rtn == 0) {
3633		/*
3634		 * We have good information, check for writable DVD.
3635		 */
3636		if ((out_data[6] == 0) && (out_data[7] == 0x12)) {
3637			un->un_f_mmc_writable_media = TRUE;
3638			kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3639			kmem_free(rqbuf, SENSE_LENGTH);
3640			return;
3641		}
3642	}
3643
3644	kmem_free(out_data, SD_PROFILE_HEADER_LEN);
3645	kmem_free(rqbuf, SENSE_LENGTH);
3646
3647	/*
3648	 * Determine if this is a RRD type device.
3649	 */
3650	mutex_exit(SD_MUTEX(un));
3651	buf = kmem_zalloc(BUFLEN_MODE_CDROM_CAP, KM_SLEEP);
3652	status = sd_send_scsi_MODE_SENSE(ssc, CDB_GROUP1, (uchar_t *)buf,
3653	    BUFLEN_MODE_CDROM_CAP, MODEPAGE_CDROM_CAP, path_flag);
3654
3655	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3656
3657	mutex_enter(SD_MUTEX(un));
3658	if (status != 0) {
3659		/* command failed; just return */
3660		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3661		return;
3662	}
3663
3664	/* Get to the page data */
3665	sense_mhp = (struct mode_header_grp2 *)buf;
3666	bd_len = (sense_mhp->bdesc_length_hi << 8) | sense_mhp->bdesc_length_lo;
3667	if (bd_len > MODE_BLK_DESC_LENGTH) {
3668		/*
3669		 * We did not get back the expected block descriptor length so
3670		 * we cannot check the mode page.
3671		 */
3672		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3673		    "sd_check_for_writable_cd: Mode Sense returned "
3674		    "invalid block descriptor length\n");
3675		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3676		return;
3677	}
3678
3679	/*
3680	 * If the device presents DVD or CD capabilities in the mode
3681	 * page, we can return here since a RRD device will not have
3682	 * these capabilities.
3683	 */
3684	sense_page = (uchar_t *)(buf + MODE_HEADER_LENGTH_GRP2 + bd_len);
3685	if ((sense_page[2] & 0x3f) || (sense_page[3] & 0x3f)) {
3686		kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3687		return;
3688	}
3689	kmem_free(buf, BUFLEN_MODE_CDROM_CAP);
3690
3691	/*
3692	 * If un->un_f_mmc_writable_media is still FALSE,
3693	 * check for RRD type media.  A RRD device is identified
3694	 * by the features RANDOM_WRITABLE and HARDWARE_DEFECT_MANAGEMENT.
3695	 */
3696	mutex_exit(SD_MUTEX(un));
3697	out_data_rw = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3698	rqbuf_rw = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3699
3700	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_rw,
3701	    SENSE_LENGTH, out_data_rw, SD_CURRENT_FEATURE_LEN,
3702	    RANDOM_WRITABLE, path_flag);
3703
3704	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3705	if (rtn != 0) {
3706		kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3707		kmem_free(rqbuf_rw, SENSE_LENGTH);
3708		mutex_enter(SD_MUTEX(un));
3709		return;
3710	}
3711
3712	out_data_hd = kmem_zalloc(SD_CURRENT_FEATURE_LEN, KM_SLEEP);
3713	rqbuf_hd = kmem_zalloc(SENSE_LENGTH, KM_SLEEP);
3714
3715	rtn = sd_send_scsi_feature_GET_CONFIGURATION(ssc, &com, rqbuf_hd,
3716	    SENSE_LENGTH, out_data_hd, SD_CURRENT_FEATURE_LEN,
3717	    HARDWARE_DEFECT_MANAGEMENT, path_flag);
3718
3719	sd_ssc_assessment(ssc, SD_FMT_IGNORE);
3720	mutex_enter(SD_MUTEX(un));
3721	if (rtn == 0) {
3722		/*
3723		 * We have good information, check for random writable
3724		 * and hardware defect features as current.
3725		 */
3726		if ((out_data_rw[9] & RANDOM_WRITABLE) &&
3727		    (out_data_rw[10] & 0x1) &&
3728		    (out_data_hd[9] & HARDWARE_DEFECT_MANAGEMENT) &&
3729		    (out_data_hd[10] & 0x1)) {
3730			un->un_f_mmc_writable_media = TRUE;
3731		}
3732	}
3733
3734	kmem_free(out_data_rw, SD_CURRENT_FEATURE_LEN);
3735	kmem_free(rqbuf_rw, SENSE_LENGTH);
3736	kmem_free(out_data_hd, SD_CURRENT_FEATURE_LEN);
3737	kmem_free(rqbuf_hd, SENSE_LENGTH);
3738}
3739
3740/*
3741 *    Function: sd_read_unit_properties
3742 *
3743 * Description: The following implements a property lookup mechanism.
3744 *		Properties for particular disks (keyed on vendor, model
3745 *		and rev numbers) are sought in the sd.conf file via
3746 *		sd_process_sdconf_file(), and if not found there, are
3747 *		looked for in a list hardcoded in this driver via
3748 *		sd_process_sdconf_table() Once located the properties
3749 *		are used to update the driver unit structure.
3750 *
3751 *   Arguments: un - driver soft state (unit) structure
3752 */
3753
3754static void
3755sd_read_unit_properties(struct sd_lun *un)
3756{
3757	/*
3758	 * sd_process_sdconf_file returns SD_FAILURE if it cannot find
3759	 * the "sd-config-list" property (from the sd.conf file) or if
3760	 * there was not a match for the inquiry vid/pid. If this event
3761	 * occurs the static driver configuration table is searched for
3762	 * a match.
3763	 */
3764	ASSERT(un != NULL);
3765	if (sd_process_sdconf_file(un) == SD_FAILURE) {
3766		sd_process_sdconf_table(un);
3767	}
3768
3769	/* check for LSI device */
3770	sd_is_lsi(un);
3771
3772
3773}
3774
3775
3776/*
3777 *    Function: sd_process_sdconf_file
3778 *
3779 * Description: Use ddi_prop_lookup(9F) to obtain the properties from the
3780 *		driver's config file (ie, sd.conf) and update the driver
3781 *		soft state structure accordingly.
3782 *
3783 *   Arguments: un - driver soft state (unit) structure
3784 *
3785 * Return Code: SD_SUCCESS - The properties were successfully set according
3786 *			     to the driver configuration file.
3787 *		SD_FAILURE - The driver config list was not obtained or
3788 *			     there was no vid/pid match. This indicates that
3789 *			     the static config table should be used.
3790 *
3791 * The config file has a property, "sd-config-list". Currently we support
3792 * two kinds of formats. For both formats, the value of this property
3793 * is a list of duplets:
3794 *
3795 *  sd-config-list=
3796 *	<duplet>,
3797 *	[,<duplet>]*;
3798 *
3799 * For the improved format, where
3800 *
3801 *     <duplet>:= "<vid+pid>","<tunable-list>"
3802 *
3803 * and
3804 *
3805 *     <tunable-list>:=   <tunable> [, <tunable> ]*;
3806 *     <tunable> =        <name> : <value>
3807 *
3808 * The <vid+pid> is the string that is returned by the target device on a
3809 * SCSI inquiry command, the <tunable-list> contains one or more tunables
3810 * to apply to all target devices with the specified <vid+pid>.
3811 *
3812 * Each <tunable> is a "<name> : <value>" pair.
3813 *
3814 * For the old format, the structure of each duplet is as follows:
3815 *
3816 *  <duplet>:= "<vid+pid>","<data-property-name_list>"
3817 *
3818 * The first entry of the duplet is the device ID string (the concatenated
3819 * vid & pid; not to be confused with a device_id).  This is defined in
3820 * the same way as in the sd_disk_table.
3821 *
3822 * The second part of the duplet is a string that identifies a
3823 * data-property-name-list. The data-property-name-list is defined as
3824 * follows:
3825 *
3826 *  <data-property-name-list>:=<data-property-name> [<data-property-name>]
3827 *
3828 * The syntax of <data-property-name> depends on the <version> field.
3829 *
3830 * If version = SD_CONF_VERSION_1 we have the following syntax:
3831 *
3832 *	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
3833 *
3834 * where the prop0 value will be used to set prop0 if bit0 set in the
3835 * flags, prop1 if bit1 set, etc. and N = SD_CONF_MAX_ITEMS -1
3836 *
3837 */
3838
3839static int
3840sd_process_sdconf_file(struct sd_lun *un)
3841{
3842	char	**config_list = NULL;
3843	uint_t	nelements;
3844	char	*vidptr;
3845	int	vidlen;
3846	char	*dnlist_ptr;
3847	char	*dataname_ptr;
3848	char	*dataname_lasts;
3849	int	*data_list = NULL;
3850	uint_t	data_list_len;
3851	int	rval = SD_FAILURE;
3852	int	i;
3853
3854	ASSERT(un != NULL);
3855
3856	/* Obtain the configuration list associated with the .conf file */
3857	if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, SD_DEVINFO(un),
3858	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, sd_config_list,
3859	    &config_list, &nelements) != DDI_PROP_SUCCESS) {
3860		return (SD_FAILURE);
3861	}
3862
3863	/*
3864	 * Compare vids in each duplet to the inquiry vid - if a match is
3865	 * made, get the data value and update the soft state structure
3866	 * accordingly.
3867	 *
3868	 * Each duplet should show as a pair of strings, return SD_FAILURE
3869	 * otherwise.
3870	 */
3871	if (nelements & 1) {
3872		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
3873		    "sd-config-list should show as pairs of strings.\n");
3874		if (config_list)
3875			ddi_prop_free(config_list);
3876		return (SD_FAILURE);
3877	}
3878
3879	for (i = 0; i < nelements; i += 2) {
3880		/*
3881		 * Note: The assumption here is that each vid entry is on
3882		 * a unique line from its associated duplet.
3883		 */
3884		vidptr = config_list[i];
3885		vidlen = (int)strlen(vidptr);
3886		if (sd_sdconf_id_match(un, vidptr, vidlen) != SD_SUCCESS) {
3887			continue;
3888		}
3889
3890		/*
3891		 * dnlist contains 1 or more blank separated
3892		 * data-property-name entries
3893		 */
3894		dnlist_ptr = config_list[i + 1];
3895
3896		if (strchr(dnlist_ptr, ':') != NULL) {
3897			/*
3898			 * Decode the improved format sd-config-list.
3899			 */
3900			sd_nvpair_str_decode(un, dnlist_ptr);
3901		} else {
3902			/*
3903			 * The old format sd-config-list, loop through all
3904			 * data-property-name entries in the
3905			 * data-property-name-list
3906			 * setting the properties for each.
3907			 */
3908			for (dataname_ptr = sd_strtok_r(dnlist_ptr, " \t",
3909			    &dataname_lasts); dataname_ptr != NULL;
3910			    dataname_ptr = sd_strtok_r(NULL, " \t",
3911			    &dataname_lasts)) {
3912				int version;
3913
3914				SD_INFO(SD_LOG_ATTACH_DETACH, un,
3915				    "sd_process_sdconf_file: disk:%s, "
3916				    "data:%s\n", vidptr, dataname_ptr);
3917
3918				/* Get the data list */
3919				if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY,
3920				    SD_DEVINFO(un), 0, dataname_ptr, &data_list,
3921				    &data_list_len) != DDI_PROP_SUCCESS) {
3922					SD_INFO(SD_LOG_ATTACH_DETACH, un,
3923					    "sd_process_sdconf_file: data "
3924					    "property (%s) has no value\n",
3925					    dataname_ptr);
3926					continue;
3927				}
3928
3929				version = data_list[0];
3930
3931				if (version == SD_CONF_VERSION_1) {
3932					sd_tunables values;
3933
3934					/* Set the properties */
3935					if (sd_chk_vers1_data(un, data_list[1],
3936					    &data_list[2], data_list_len,
3937					    dataname_ptr) == SD_SUCCESS) {
3938						sd_get_tunables_from_conf(un,
3939						    data_list[1], &data_list[2],
3940						    &values);
3941						sd_set_vers1_properties(un,
3942						    data_list[1], &values);
3943						rval = SD_SUCCESS;
3944					} else {
3945						rval = SD_FAILURE;
3946					}
3947				} else {
3948					scsi_log(SD_DEVINFO(un), sd_label,
3949					    CE_WARN, "data property %s version "
3950					    "0x%x is invalid.",
3951					    dataname_ptr, version);
3952					rval = SD_FAILURE;
3953				}
3954				if (data_list)
3955					ddi_prop_free(data_list);
3956			}
3957		}
3958	}
3959
3960	/* free up the memory allocated by ddi_prop_lookup_string_array(). */
3961	if (config_list) {
3962		ddi_prop_free(config_list);
3963	}
3964
3965	return (rval);
3966}
3967
3968/*
3969 *    Function: sd_nvpair_str_decode()
3970 *
3971 * Description: Parse the improved format sd-config-list to get
3972 *    each entry of tunable, which includes a name-value pair.
3973 *    Then call sd_set_properties() to set the property.
3974 *
3975 *   Arguments: un - driver soft state (unit) structure
3976 *    nvpair_str - the tunable list
3977 */
3978static void
3979sd_nvpair_str_decode(struct sd_lun *un, char *nvpair_str)
3980{
3981	char	*nv, *name, *value, *token;
3982	char	*nv_lasts, *v_lasts, *x_lasts;
3983
3984	for (nv = sd_strtok_r(nvpair_str, ",", &nv_lasts); nv != NULL;
3985	    nv = sd_strtok_r(NULL, ",", &nv_lasts)) {
3986		token = sd_strtok_r(nv, ":", &v_lasts);
3987		name  = sd_strtok_r(token, " \t", &x_lasts);
3988		token = sd_strtok_r(NULL, ":", &v_lasts);
3989		value = sd_strtok_r(token, " \t", &x_lasts);
3990		if (name == NULL || value == NULL) {
3991			SD_INFO(SD_LOG_ATTACH_DETACH, un,
3992			    "sd_nvpair_str_decode: "
3993			    "name or value is not valid!\n");
3994		} else {
3995			sd_set_properties(un, name, value);
3996		}
3997	}
3998}
3999
4000/*
4001 *    Function: sd_strtok_r()
4002 *
4003 * Description: This function uses strpbrk and strspn to break
4004 *    string into tokens on sequentially subsequent calls. Return
4005 *    NULL when no non-separator characters remain. The first
4006 *    argument is NULL for subsequent calls.
4007 */
4008static char *
4009sd_strtok_r(char *string, const char *sepset, char **lasts)
4010{
4011	char	*q, *r;
4012
4013	/* First or subsequent call */
4014	if (string == NULL)
4015		string = *lasts;
4016
4017	if (string == NULL)
4018		return (NULL);
4019
4020	/* Skip leading separators */
4021	q = string + strspn(string, sepset);
4022
4023	if (*q == '\0')
4024		return (NULL);
4025
4026	if ((r = strpbrk(q, sepset)) == NULL) {
4027		*lasts = NULL;
4028	} else {
4029		*r = '\0';
4030		*lasts = r + 1;
4031	}
4032	return (q);
4033}
4034
4035/*
4036 *    Function: sd_set_properties()
4037 *
4038 * Description: Set device properties based on the improved
4039 *    format sd-config-list.
4040 *
4041 *   Arguments: un - driver soft state (unit) structure
4042 *    name  - supported tunable name
4043 *    value - tunable value
4044 */
4045static void
4046sd_set_properties(struct sd_lun *un, char *name, char *value)
4047{
4048	char	*endptr = NULL;
4049	long	val = 0;
4050
4051	if (strcasecmp(name, "cache-nonvolatile") == 0) {
4052		if (strcasecmp(value, "true") == 0) {
4053			un->un_f_suppress_cache_flush = TRUE;
4054		} else if (strcasecmp(value, "false") == 0) {
4055			un->un_f_suppress_cache_flush = FALSE;
4056		} else {
4057			goto value_invalid;
4058		}
4059		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4060		    "suppress_cache_flush flag set to %d\n",
4061		    un->un_f_suppress_cache_flush);
4062		return;
4063	}
4064
4065	if (strcasecmp(name, "controller-type") == 0) {
4066		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4067			un->un_ctype = val;
4068		} else {
4069			goto value_invalid;
4070		}
4071		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4072		    "ctype set to %d\n", un->un_ctype);
4073		return;
4074	}
4075
4076	if (strcasecmp(name, "delay-busy") == 0) {
4077		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4078			un->un_busy_timeout = drv_usectohz(val / 1000);
4079		} else {
4080			goto value_invalid;
4081		}
4082		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4083		    "busy_timeout set to %d\n", un->un_busy_timeout);
4084		return;
4085	}
4086
4087	if (strcasecmp(name, "disksort") == 0) {
4088		if (strcasecmp(value, "true") == 0) {
4089			un->un_f_disksort_disabled = FALSE;
4090		} else if (strcasecmp(value, "false") == 0) {
4091			un->un_f_disksort_disabled = TRUE;
4092		} else {
4093			goto value_invalid;
4094		}
4095		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4096		    "disksort disabled flag set to %d\n",
4097		    un->un_f_disksort_disabled);
4098		return;
4099	}
4100
4101	if (strcasecmp(name, "power-condition") == 0) {
4102		if (strcasecmp(value, "true") == 0) {
4103			un->un_f_power_condition_disabled = FALSE;
4104		} else if (strcasecmp(value, "false") == 0) {
4105			un->un_f_power_condition_disabled = TRUE;
4106		} else {
4107			goto value_invalid;
4108		}
4109		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4110		    "power condition disabled flag set to %d\n",
4111		    un->un_f_power_condition_disabled);
4112		return;
4113	}
4114
4115	if (strcasecmp(name, "timeout-releasereservation") == 0) {
4116		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4117			un->un_reserve_release_time = val;
4118		} else {
4119			goto value_invalid;
4120		}
4121		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4122		    "reservation release timeout set to %d\n",
4123		    un->un_reserve_release_time);
4124		return;
4125	}
4126
4127	if (strcasecmp(name, "reset-lun") == 0) {
4128		if (strcasecmp(value, "true") == 0) {
4129			un->un_f_lun_reset_enabled = TRUE;
4130		} else if (strcasecmp(value, "false") == 0) {
4131			un->un_f_lun_reset_enabled = FALSE;
4132		} else {
4133			goto value_invalid;
4134		}
4135		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4136		    "lun reset enabled flag set to %d\n",
4137		    un->un_f_lun_reset_enabled);
4138		return;
4139	}
4140
4141	if (strcasecmp(name, "retries-busy") == 0) {
4142		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4143			un->un_busy_retry_count = val;
4144		} else {
4145			goto value_invalid;
4146		}
4147		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4148		    "busy retry count set to %d\n", un->un_busy_retry_count);
4149		return;
4150	}
4151
4152	if (strcasecmp(name, "retries-timeout") == 0) {
4153		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4154			un->un_retry_count = val;
4155		} else {
4156			goto value_invalid;
4157		}
4158		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4159		    "timeout retry count set to %d\n", un->un_retry_count);
4160		return;
4161	}
4162
4163	if (strcasecmp(name, "retries-notready") == 0) {
4164		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4165			un->un_notready_retry_count = val;
4166		} else {
4167			goto value_invalid;
4168		}
4169		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4170		    "notready retry count set to %d\n",
4171		    un->un_notready_retry_count);
4172		return;
4173	}
4174
4175	if (strcasecmp(name, "retries-reset") == 0) {
4176		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4177			un->un_reset_retry_count = val;
4178		} else {
4179			goto value_invalid;
4180		}
4181		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4182		    "reset retry count set to %d\n",
4183		    un->un_reset_retry_count);
4184		return;
4185	}
4186
4187	if (strcasecmp(name, "throttle-max") == 0) {
4188		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4189			un->un_saved_throttle = un->un_throttle = val;
4190		} else {
4191			goto value_invalid;
4192		}
4193		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4194		    "throttle set to %d\n", un->un_throttle);
4195	}
4196
4197	if (strcasecmp(name, "throttle-min") == 0) {
4198		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4199			un->un_min_throttle = val;
4200		} else {
4201			goto value_invalid;
4202		}
4203		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4204		    "min throttle set to %d\n", un->un_min_throttle);
4205	}
4206
4207	if (strcasecmp(name, "rmw-type") == 0) {
4208		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4209			un->un_f_rmw_type = val;
4210		} else {
4211			goto value_invalid;
4212		}
4213		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4214		    "RMW type set to %d\n", un->un_f_rmw_type);
4215	}
4216
4217	if (strcasecmp(name, "physical-block-size") == 0) {
4218		if (ddi_strtol(value, &endptr, 0, &val) == 0 &&
4219		    ISP2(val) && val >= un->un_tgt_blocksize &&
4220		    val >= un->un_sys_blocksize) {
4221			un->un_phy_blocksize = val;
4222		} else {
4223			goto value_invalid;
4224		}
4225		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4226		    "physical block size set to %d\n", un->un_phy_blocksize);
4227	}
4228
4229	if (strcasecmp(name, "retries-victim") == 0) {
4230		if (ddi_strtol(value, &endptr, 0, &val) == 0) {
4231			un->un_victim_retry_count = val;
4232		} else {
4233			goto value_invalid;
4234		}
4235		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4236		    "victim retry count set to %d\n",
4237		    un->un_victim_retry_count);
4238		return;
4239	}
4240
4241	/*
4242	 * Validate the throttle values.
4243	 * If any of the numbers are invalid, set everything to defaults.
4244	 */
4245	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4246	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4247	    (un->un_min_throttle > un->un_throttle)) {
4248		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4249		un->un_min_throttle = sd_min_throttle;
4250	}
4251
4252	if (strcasecmp(name, "mmc-gesn-polling") == 0) {
4253		if (strcasecmp(value, "true") == 0) {
4254			un->un_f_mmc_gesn_polling = TRUE;
4255		} else if (strcasecmp(value, "false") == 0) {
4256			un->un_f_mmc_gesn_polling = FALSE;
4257		} else {
4258			goto value_invalid;
4259		}
4260		SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4261		    "mmc-gesn-polling set to %d\n",
4262		    un->un_f_mmc_gesn_polling);
4263	}
4264
4265	return;
4266
4267value_invalid:
4268	SD_INFO(SD_LOG_ATTACH_DETACH, un, "sd_set_properties: "
4269	    "value of prop %s is invalid\n", name);
4270}
4271
4272/*
4273 *    Function: sd_get_tunables_from_conf()
4274 *
4275 *
4276 *    This function reads the data list from the sd.conf file and pulls
4277 *    the values that can have numeric values as arguments and places
4278 *    the values in the appropriate sd_tunables member.
4279 *    Since the order of the data list members varies across platforms
4280 *    This function reads them from the data list in a platform specific
4281 *    order and places them into the correct sd_tunable member that is
4282 *    consistent across all platforms.
4283 */
4284static void
4285sd_get_tunables_from_conf(struct sd_lun *un, int flags, int *data_list,
4286    sd_tunables *values)
4287{
4288	int i;
4289	int mask;
4290
4291	bzero(values, sizeof (sd_tunables));
4292
4293	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4294
4295		mask = 1 << i;
4296		if (mask > flags) {
4297			break;
4298		}
4299
4300		switch (mask & flags) {
4301		case 0:	/* This mask bit not set in flags */
4302			continue;
4303		case SD_CONF_BSET_THROTTLE:
4304			values->sdt_throttle = data_list[i];
4305			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4306			    "sd_get_tunables_from_conf: throttle = %d\n",
4307			    values->sdt_throttle);
4308			break;
4309		case SD_CONF_BSET_CTYPE:
4310			values->sdt_ctype = data_list[i];
4311			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4312			    "sd_get_tunables_from_conf: ctype = %d\n",
4313			    values->sdt_ctype);
4314			break;
4315		case SD_CONF_BSET_NRR_COUNT:
4316			values->sdt_not_rdy_retries = data_list[i];
4317			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4318			    "sd_get_tunables_from_conf: not_rdy_retries = %d\n",
4319			    values->sdt_not_rdy_retries);
4320			break;
4321		case SD_CONF_BSET_BSY_RETRY_COUNT:
4322			values->sdt_busy_retries = data_list[i];
4323			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4324			    "sd_get_tunables_from_conf: busy_retries = %d\n",
4325			    values->sdt_busy_retries);
4326			break;
4327		case SD_CONF_BSET_RST_RETRIES:
4328			values->sdt_reset_retries = data_list[i];
4329			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4330			    "sd_get_tunables_from_conf: reset_retries = %d\n",
4331			    values->sdt_reset_retries);
4332			break;
4333		case SD_CONF_BSET_RSV_REL_TIME:
4334			values->sdt_reserv_rel_time = data_list[i];
4335			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4336			    "sd_get_tunables_from_conf: reserv_rel_time = %d\n",
4337			    values->sdt_reserv_rel_time);
4338			break;
4339		case SD_CONF_BSET_MIN_THROTTLE:
4340			values->sdt_min_throttle = data_list[i];
4341			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4342			    "sd_get_tunables_from_conf: min_throttle = %d\n",
4343			    values->sdt_min_throttle);
4344			break;
4345		case SD_CONF_BSET_DISKSORT_DISABLED:
4346			values->sdt_disk_sort_dis = data_list[i];
4347			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4348			    "sd_get_tunables_from_conf: disk_sort_dis = %d\n",
4349			    values->sdt_disk_sort_dis);
4350			break;
4351		case SD_CONF_BSET_LUN_RESET_ENABLED:
4352			values->sdt_lun_reset_enable = data_list[i];
4353			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4354			    "sd_get_tunables_from_conf: lun_reset_enable = %d"
4355			    "\n", values->sdt_lun_reset_enable);
4356			break;
4357		case SD_CONF_BSET_CACHE_IS_NV:
4358			values->sdt_suppress_cache_flush = data_list[i];
4359			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4360			    "sd_get_tunables_from_conf: \
4361			    suppress_cache_flush = %d"
4362			    "\n", values->sdt_suppress_cache_flush);
4363			break;
4364		case SD_CONF_BSET_PC_DISABLED:
4365			values->sdt_disk_sort_dis = data_list[i];
4366			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4367			    "sd_get_tunables_from_conf: power_condition_dis = "
4368			    "%d\n", values->sdt_power_condition_dis);
4369			break;
4370		}
4371	}
4372}
4373
4374/*
4375 *    Function: sd_process_sdconf_table
4376 *
4377 * Description: Search the static configuration table for a match on the
4378 *		inquiry vid/pid and update the driver soft state structure
4379 *		according to the table property values for the device.
4380 *
4381 *		The form of a configuration table entry is:
4382 *		  <vid+pid>,<flags>,<property-data>
4383 *		  "SEAGATE ST42400N",1,0x40000,
4384 *		  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1;
4385 *
4386 *   Arguments: un - driver soft state (unit) structure
4387 */
4388
4389static void
4390sd_process_sdconf_table(struct sd_lun *un)
4391{
4392	char	*id = NULL;
4393	int	table_index;
4394	int	idlen;
4395
4396	ASSERT(un != NULL);
4397	for (table_index = 0; table_index < sd_disk_table_size;
4398	    table_index++) {
4399		id = sd_disk_table[table_index].device_id;
4400		idlen = strlen(id);
4401
4402		/*
4403		 * The static configuration table currently does not
4404		 * implement version 10 properties. Additionally,
4405		 * multiple data-property-name entries are not
4406		 * implemented in the static configuration table.
4407		 */
4408		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4409			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4410			    "sd_process_sdconf_table: disk %s\n", id);
4411			sd_set_vers1_properties(un,
4412			    sd_disk_table[table_index].flags,
4413			    sd_disk_table[table_index].properties);
4414			break;
4415		}
4416	}
4417}
4418
4419
4420/*
4421 *    Function: sd_sdconf_id_match
4422 *
4423 * Description: This local function implements a case sensitive vid/pid
4424 *		comparison as well as the boundary cases of wild card and
4425 *		multiple blanks.
4426 *
4427 *		Note: An implicit assumption made here is that the scsi
4428 *		inquiry structure will always keep the vid, pid and
4429 *		revision strings in consecutive sequence, so they can be
4430 *		read as a single string. If this assumption is not the
4431 *		case, a separate string, to be used for the check, needs
4432 *		to be built with these strings concatenated.
4433 *
4434 *   Arguments: un - driver soft state (unit) structure
4435 *		id - table or config file vid/pid
4436 *		idlen  - length of the vid/pid (bytes)
4437 *
4438 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4439 *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4440 */
4441
4442static int
4443sd_sdconf_id_match(struct sd_lun *un, char *id, int idlen)
4444{
4445	struct scsi_inquiry	*sd_inq;
4446	int			rval = SD_SUCCESS;
4447
4448	ASSERT(un != NULL);
4449	sd_inq = un->un_sd->sd_inq;
4450	ASSERT(id != NULL);
4451
4452	/*
4453	 * We use the inq_vid as a pointer to a buffer containing the
4454	 * vid and pid and use the entire vid/pid length of the table
4455	 * entry for the comparison. This works because the inq_pid
4456	 * data member follows inq_vid in the scsi_inquiry structure.
4457	 */
4458	if (strncasecmp(sd_inq->inq_vid, id, idlen) != 0) {
4459		/*
4460		 * The user id string is compared to the inquiry vid/pid
4461		 * using a case insensitive comparison and ignoring
4462		 * multiple spaces.
4463		 */
4464		rval = sd_blank_cmp(un, id, idlen);
4465		if (rval != SD_SUCCESS) {
4466			/*
4467			 * User id strings that start and end with a "*"
4468			 * are a special case. These do not have a
4469			 * specific vendor, and the product string can
4470			 * appear anywhere in the 16 byte PID portion of
4471			 * the inquiry data. This is a simple strstr()
4472			 * type search for the user id in the inquiry data.
4473			 */
4474			if ((id[0] == '*') && (id[idlen - 1] == '*')) {
4475				char	*pidptr = &id[1];
4476				int	i;
4477				int	j;
4478				int	pidstrlen = idlen - 2;
4479				j = sizeof (SD_INQUIRY(un)->inq_pid) -
4480				    pidstrlen;
4481
4482				if (j < 0) {
4483					return (SD_FAILURE);
4484				}
4485				for (i = 0; i < j; i++) {
4486					if (bcmp(&SD_INQUIRY(un)->inq_pid[i],
4487					    pidptr, pidstrlen) == 0) {
4488						rval = SD_SUCCESS;
4489						break;
4490					}
4491				}
4492			}
4493		}
4494	}
4495	return (rval);
4496}
4497
4498
4499/*
4500 *    Function: sd_blank_cmp
4501 *
4502 * Description: If the id string starts and ends with a space, treat
4503 *		multiple consecutive spaces as equivalent to a single
4504 *		space. For example, this causes a sd_disk_table entry
4505 *		of " NEC CDROM " to match a device's id string of
4506 *		"NEC       CDROM".
4507 *
4508 *		Note: The success exit condition for this routine is if
4509 *		the pointer to the table entry is '\0' and the cnt of
4510 *		the inquiry length is zero. This will happen if the inquiry
4511 *		string returned by the device is padded with spaces to be
4512 *		exactly 24 bytes in length (8 byte vid + 16 byte pid). The
4513 *		SCSI spec states that the inquiry string is to be padded with
4514 *		spaces.
4515 *
4516 *   Arguments: un - driver soft state (unit) structure
4517 *		id - table or config file vid/pid
4518 *		idlen  - length of the vid/pid (bytes)
4519 *
4520 * Return Code: SD_SUCCESS - Indicates a match with the inquiry vid/pid
4521 *		SD_FAILURE - Indicates no match with the inquiry vid/pid
4522 */
4523
4524static int
4525sd_blank_cmp(struct sd_lun *un, char *id, int idlen)
4526{
4527	char		*p1;
4528	char		*p2;
4529	int		cnt;
4530	cnt = sizeof (SD_INQUIRY(un)->inq_vid) +
4531	    sizeof (SD_INQUIRY(un)->inq_pid);
4532
4533	ASSERT(un != NULL);
4534	p2 = un->un_sd->sd_inq->inq_vid;
4535	ASSERT(id != NULL);
4536	p1 = id;
4537
4538	if ((id[0] == ' ') && (id[idlen - 1] == ' ')) {
4539		/*
4540		 * Note: string p1 is terminated by a NUL but string p2
4541		 * isn't.  The end of p2 is determined by cnt.
4542		 */
4543		for (;;) {
4544			/* skip over any extra blanks in both strings */
4545			while ((*p1 != '\0') && (*p1 == ' ')) {
4546				p1++;
4547			}
4548			while ((cnt != 0) && (*p2 == ' ')) {
4549				p2++;
4550				cnt--;
4551			}
4552
4553			/* compare the two strings */
4554			if ((cnt == 0) ||
4555			    (SD_TOUPPER(*p1) != SD_TOUPPER(*p2))) {
4556				break;
4557			}
4558			while ((cnt > 0) &&
4559			    (SD_TOUPPER(*p1) == SD_TOUPPER(*p2))) {
4560				p1++;
4561				p2++;
4562				cnt--;
4563			}
4564		}
4565	}
4566
4567	/* return SD_SUCCESS if both strings match */
4568	return (((*p1 == '\0') && (cnt == 0)) ? SD_SUCCESS : SD_FAILURE);
4569}
4570
4571
4572/*
4573 *    Function: sd_chk_vers1_data
4574 *
4575 * Description: Verify the version 1 device properties provided by the
4576 *		user via the configuration file
4577 *
4578 *   Arguments: un	     - driver soft state (unit) structure
4579 *		flags	     - integer mask indicating properties to be set
4580 *		prop_list    - integer list of property values
4581 *		list_len     - number of the elements
4582 *
4583 * Return Code: SD_SUCCESS - Indicates the user provided data is valid
4584 *		SD_FAILURE - Indicates the user provided data is invalid
4585 */
4586
4587static int
4588sd_chk_vers1_data(struct sd_lun *un, int flags, int *prop_list,
4589    int list_len, char *dataname_ptr)
4590{
4591	int i;
4592	int mask = 1;
4593	int index = 0;
4594
4595	ASSERT(un != NULL);
4596
4597	/* Check for a NULL property name and list */
4598	if (dataname_ptr == NULL) {
4599		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4600		    "sd_chk_vers1_data: NULL data property name.");
4601		return (SD_FAILURE);
4602	}
4603	if (prop_list == NULL) {
4604		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4605		    "sd_chk_vers1_data: %s NULL data property list.",
4606		    dataname_ptr);
4607		return (SD_FAILURE);
4608	}
4609
4610	/* Display a warning if undefined bits are set in the flags */
4611	if (flags & ~SD_CONF_BIT_MASK) {
4612		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4613		    "sd_chk_vers1_data: invalid bits 0x%x in data list %s. "
4614		    "Properties not set.",
4615		    (flags & ~SD_CONF_BIT_MASK), dataname_ptr);
4616		return (SD_FAILURE);
4617	}
4618
4619	/*
4620	 * Verify the length of the list by identifying the highest bit set
4621	 * in the flags and validating that the property list has a length
4622	 * up to the index of this bit.
4623	 */
4624	for (i = 0; i < SD_CONF_MAX_ITEMS; i++) {
4625		if (flags & mask) {
4626			index++;
4627		}
4628		mask = 1 << i;
4629	}
4630	if (list_len < (index + 2)) {
4631		scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4632		    "sd_chk_vers1_data: "
4633		    "Data property list %s size is incorrect. "
4634		    "Properties not set.", dataname_ptr);
4635		scsi_log(SD_DEVINFO(un), sd_label, CE_CONT, "Size expected: "
4636		    "version + 1 flagword + %d properties", SD_CONF_MAX_ITEMS);
4637		return (SD_FAILURE);
4638	}
4639	return (SD_SUCCESS);
4640}
4641
4642
4643/*
4644 *    Function: sd_set_vers1_properties
4645 *
4646 * Description: Set version 1 device properties based on a property list
4647 *		retrieved from the driver configuration file or static
4648 *		configuration table. Version 1 properties have the format:
4649 *
4650 *	<data-property-name>:=<version>,<flags>,<prop0>,<prop1>,.....<propN>
4651 *
4652 *		where the prop0 value will be used to set prop0 if bit0
4653 *		is set in the flags
4654 *
4655 *   Arguments: un	     - driver soft state (unit) structure
4656 *		flags	     - integer mask indicating properties to be set
4657 *		prop_list    - integer list of property values
4658 */
4659
4660static void
4661sd_set_vers1_properties(struct sd_lun *un, int flags, sd_tunables *prop_list)
4662{
4663	ASSERT(un != NULL);
4664
4665	/*
4666	 * Set the flag to indicate cache is to be disabled. An attempt
4667	 * to disable the cache via sd_cache_control() will be made
4668	 * later during attach once the basic initialization is complete.
4669	 */
4670	if (flags & SD_CONF_BSET_NOCACHE) {
4671		un->un_f_opt_disable_cache = TRUE;
4672		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4673		    "sd_set_vers1_properties: caching disabled flag set\n");
4674	}
4675
4676	/* CD-specific configuration parameters */
4677	if (flags & SD_CONF_BSET_PLAYMSF_BCD) {
4678		un->un_f_cfg_playmsf_bcd = TRUE;
4679		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4680		    "sd_set_vers1_properties: playmsf_bcd set\n");
4681	}
4682	if (flags & SD_CONF_BSET_READSUB_BCD) {
4683		un->un_f_cfg_readsub_bcd = TRUE;
4684		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4685		    "sd_set_vers1_properties: readsub_bcd set\n");
4686	}
4687	if (flags & SD_CONF_BSET_READ_TOC_TRK_BCD) {
4688		un->un_f_cfg_read_toc_trk_bcd = TRUE;
4689		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4690		    "sd_set_vers1_properties: read_toc_trk_bcd set\n");
4691	}
4692	if (flags & SD_CONF_BSET_READ_TOC_ADDR_BCD) {
4693		un->un_f_cfg_read_toc_addr_bcd = TRUE;
4694		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4695		    "sd_set_vers1_properties: read_toc_addr_bcd set\n");
4696	}
4697	if (flags & SD_CONF_BSET_NO_READ_HEADER) {
4698		un->un_f_cfg_no_read_header = TRUE;
4699		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4700		    "sd_set_vers1_properties: no_read_header set\n");
4701	}
4702	if (flags & SD_CONF_BSET_READ_CD_XD4) {
4703		un->un_f_cfg_read_cd_xd4 = TRUE;
4704		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4705		    "sd_set_vers1_properties: read_cd_xd4 set\n");
4706	}
4707
4708	/* Support for devices which do not have valid/unique serial numbers */
4709	if (flags & SD_CONF_BSET_FAB_DEVID) {
4710		un->un_f_opt_fab_devid = TRUE;
4711		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4712		    "sd_set_vers1_properties: fab_devid bit set\n");
4713	}
4714
4715	/* Support for user throttle configuration */
4716	if (flags & SD_CONF_BSET_THROTTLE) {
4717		ASSERT(prop_list != NULL);
4718		un->un_saved_throttle = un->un_throttle =
4719		    prop_list->sdt_throttle;
4720		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4721		    "sd_set_vers1_properties: throttle set to %d\n",
4722		    prop_list->sdt_throttle);
4723	}
4724
4725	/* Set the per disk retry count according to the conf file or table. */
4726	if (flags & SD_CONF_BSET_NRR_COUNT) {
4727		ASSERT(prop_list != NULL);
4728		if (prop_list->sdt_not_rdy_retries) {
4729			un->un_notready_retry_count =
4730			    prop_list->sdt_not_rdy_retries;
4731			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4732			    "sd_set_vers1_properties: not ready retry count"
4733			    " set to %d\n", un->un_notready_retry_count);
4734		}
4735	}
4736
4737	/* The controller type is reported for generic disk driver ioctls */
4738	if (flags & SD_CONF_BSET_CTYPE) {
4739		ASSERT(prop_list != NULL);
4740		switch (prop_list->sdt_ctype) {
4741		case CTYPE_CDROM:
4742			un->un_ctype = prop_list->sdt_ctype;
4743			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4744			    "sd_set_vers1_properties: ctype set to "
4745			    "CTYPE_CDROM\n");
4746			break;
4747		case CTYPE_CCS:
4748			un->un_ctype = prop_list->sdt_ctype;
4749			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4750			    "sd_set_vers1_properties: ctype set to "
4751			    "CTYPE_CCS\n");
4752			break;
4753		case CTYPE_ROD:		/* RW optical */
4754			un->un_ctype = prop_list->sdt_ctype;
4755			SD_INFO(SD_LOG_ATTACH_DETACH, un,
4756			    "sd_set_vers1_properties: ctype set to "
4757			    "CTYPE_ROD\n");
4758			break;
4759		default:
4760			scsi_log(SD_DEVINFO(un), sd_label, CE_WARN,
4761			    "sd_set_vers1_properties: Could not set "
4762			    "invalid ctype value (%d)",
4763			    prop_list->sdt_ctype);
4764		}
4765	}
4766
4767	/* Purple failover timeout */
4768	if (flags & SD_CONF_BSET_BSY_RETRY_COUNT) {
4769		ASSERT(prop_list != NULL);
4770		un->un_busy_retry_count =
4771		    prop_list->sdt_busy_retries;
4772		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4773		    "sd_set_vers1_properties: "
4774		    "busy retry count set to %d\n",
4775		    un->un_busy_retry_count);
4776	}
4777
4778	/* Purple reset retry count */
4779	if (flags & SD_CONF_BSET_RST_RETRIES) {
4780		ASSERT(prop_list != NULL);
4781		un->un_reset_retry_count =
4782		    prop_list->sdt_reset_retries;
4783		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4784		    "sd_set_vers1_properties: "
4785		    "reset retry count set to %d\n",
4786		    un->un_reset_retry_count);
4787	}
4788
4789	/* Purple reservation release timeout */
4790	if (flags & SD_CONF_BSET_RSV_REL_TIME) {
4791		ASSERT(prop_list != NULL);
4792		un->un_reserve_release_time =
4793		    prop_list->sdt_reserv_rel_time;
4794		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4795		    "sd_set_vers1_properties: "
4796		    "reservation release timeout set to %d\n",
4797		    un->un_reserve_release_time);
4798	}
4799
4800	/*
4801	 * Driver flag telling the driver to verify that no commands are pending
4802	 * for a device before issuing a Test Unit Ready. This is a workaround
4803	 * for a firmware bug in some Seagate eliteI drives.
4804	 */
4805	if (flags & SD_CONF_BSET_TUR_CHECK) {
4806		un->un_f_cfg_tur_check = TRUE;
4807		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4808		    "sd_set_vers1_properties: tur queue check set\n");
4809	}
4810
4811	if (flags & SD_CONF_BSET_MIN_THROTTLE) {
4812		un->un_min_throttle = prop_list->sdt_min_throttle;
4813		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4814		    "sd_set_vers1_properties: min throttle set to %d\n",
4815		    un->un_min_throttle);
4816	}
4817
4818	if (flags & SD_CONF_BSET_DISKSORT_DISABLED) {
4819		un->un_f_disksort_disabled =
4820		    (prop_list->sdt_disk_sort_dis != 0) ?
4821		    TRUE : FALSE;
4822		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4823		    "sd_set_vers1_properties: disksort disabled "
4824		    "flag set to %d\n",
4825		    prop_list->sdt_disk_sort_dis);
4826	}
4827
4828	if (flags & SD_CONF_BSET_LUN_RESET_ENABLED) {
4829		un->un_f_lun_reset_enabled =
4830		    (prop_list->sdt_lun_reset_enable != 0) ?
4831		    TRUE : FALSE;
4832		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4833		    "sd_set_vers1_properties: lun reset enabled "
4834		    "flag set to %d\n",
4835		    prop_list->sdt_lun_reset_enable);
4836	}
4837
4838	if (flags & SD_CONF_BSET_CACHE_IS_NV) {
4839		un->un_f_suppress_cache_flush =
4840		    (prop_list->sdt_suppress_cache_flush != 0) ?
4841		    TRUE : FALSE;
4842		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4843		    "sd_set_vers1_properties: suppress_cache_flush "
4844		    "flag set to %d\n",
4845		    prop_list->sdt_suppress_cache_flush);
4846	}
4847
4848	if (flags & SD_CONF_BSET_PC_DISABLED) {
4849		un->un_f_power_condition_disabled =
4850		    (prop_list->sdt_power_condition_dis != 0) ?
4851		    TRUE : FALSE;
4852		SD_INFO(SD_LOG_ATTACH_DETACH, un,
4853		    "sd_set_vers1_properties: power_condition_disabled "
4854		    "flag set to %d\n",
4855		    prop_list->sdt_power_condition_dis);
4856	}
4857
4858	/*
4859	 * Validate the throttle values.
4860	 * If any of the numbers are invalid, set everything to defaults.
4861	 */
4862	if ((un->un_throttle < SD_LOWEST_VALID_THROTTLE) ||
4863	    (un->un_min_throttle < SD_LOWEST_VALID_THROTTLE) ||
4864	    (un->un_min_throttle > un->un_throttle)) {
4865		un->un_saved_throttle = un->un_throttle = sd_max_throttle;
4866		un->un_min_throttle = sd_min_throttle;
4867	}
4868}
4869
4870/*
4871 *   Function: sd_is_lsi()
4872 *
4873 *   Description: Check for lsi devices, step through the static device
4874 *	table to match vid/pid.
4875 *
4876 *   Args: un - ptr to sd_lun
4877 *
4878 *   Notes:  When creating new LSI property, need to add the new LSI property
4879 *		to this function.
4880 */
4881static void
4882sd_is_lsi(struct sd_lun *un)
4883{
4884	char	*id = NULL;
4885	int	table_index;
4886	int	idlen;
4887	void	*prop;
4888
4889	ASSERT(un != NULL);
4890	for (table_index = 0; table_index < sd_disk_table_size;
4891	    table_index++) {
4892		id = sd_disk_table[table_index].device_id;
4893		idlen = strlen(id);
4894		if (idlen == 0) {
4895			continue;
4896		}
4897
4898		if (sd_sdconf_id_match(un, id, idlen) == SD_SUCCESS) {
4899			prop = sd_disk_table[table_index].properties;
4900			if (prop == &lsi_properties ||
4901			    prop == &lsi_oem_properties ||
4902			    prop == &lsi_properties_scsi ||
4903			    prop == &symbios_properties) {
4904				un->un_f_cfg_is_lsi = TRUE;
4905			}
4906			break;
4907		}
4908	}
4909}
4910
4911/*
4912 *    Function: sd_get_physical_geometry
4913 *
4914 * Description: Retrieve the MODE SENSE page 3 (Format Device Page) and
4915 *		MODE SENSE page 4 (Rigid Disk Drive Geometry Page) from the
4916 *		target, and use this information to initialize the physical
4917 *		geometry cache specified by pgeom_p.
4918 *
4919 *		MODE SENSE is an optional command, so failure in this case
4920 *		does not necessarily denote an error. We want to use the
4921 *		MODE SENSE commands to derive the physical geometry of the
4922 *		device, but if either command fails, the logical geometry is
4923 *		used as the fallback for disk label geometry in cmlb.
4924 *
4925 *		This requires that un->un_blockcount and un->un_tgt_blocksize
4926 *		have already been initialized for the current target and
4927 *		that the current values be passed as args so that we don't
4928 *		end up ever trying to use -1 as a valid value. This could
4929 *		happen if either value is reset while we're not holding
4930 *		the mutex.
4931 *
4932 *   Arguments: un - driver soft state (unit) structure
4933 *		path_flag - SD_PATH_DIRECT to use the USCSI "direct" chain and
4934 *			the normal command waitq, or SD_PATH_DIRECT_PRIORITY
4935 *			to use the USCSI "direct" chain and bypass the normal
4936 *			command waitq.
4937 *
4938 *     Context: Kernel thread only (can sleep).
4939 */
4940
4941static int
4942sd_get_physical_geometry(struct sd_lun *un, cmlb_geom_t *pgeom_p,
4943    diskaddr_t capacity, int lbasize, int path_flag)
4944{
4945	struct	mode_format	*page3p;
4946	struct	mode_geometry	*page4p;
4947	struct	mode_header	*headerp;
4948	int	sector_size;
4949	int	nsect;
4950	int	nhead;
4951	int	ncyl;
4952	int	intrlv;
4953	int	spc;
4954	diskaddr_t	modesense_capacity;
4955	int	rpm;
4956	int	bd_len;
4957	int	mode_header_length;
4958	uchar_t	*p3bufp;
4959	uchar_t	*p4bufp;
4960	int	cdbsize;
4961	int	ret = EIO;
4962	sd_ssc_t *ssc;
4963	int	status;
4964
4965	ASSERT(un != NULL);
4966
4967	if (lbasize == 0) {
4968		if (ISCD(un)) {
4969			lbasize = 2048;
4970		} else {
4971			lbasize = un->un_sys_blocksize;
4972		}
4973	}
4974	pgeom_p->g_secsize = (unsigned short)lbasize;
4975
4976	/*
4977	 * If the unit is a cd/dvd drive MODE SENSE page three
4978	 * and MODE SENSE page four are reserved (see SBC spec
4979	 * and MMC spec). To prevent soft errors just return
4980	 * using the default LBA size.
4981	 *
4982	 * Since SATA MODE SENSE function (sata_txlt_mode_sense()) does not
4983	 * implement support for mode pages 3 and 4 return here to prevent
4984	 * illegal requests on SATA drives.
4985	 *
4986	 * These pages are also reserved in SBC-2 and later.  We assume SBC-2
4987	 * or later for a direct-attached block device if the SCSI version is
4988	 * at least SPC-3.
4989	 */
4990
4991	if (ISCD(un) ||
4992	    un->un_interconnect_type == SD_INTERCONNECT_SATA ||
4993	    (un->un_ctype == CTYPE_CCS && SD_INQUIRY(un)->inq_ansi >= 5))
4994		return (ret);
4995
4996	cdbsize = (un->un_f_cfg_is_atapi == TRUE) ? CDB_GROUP2 : CDB_GROUP0;
4997
4998	/*
4999	 * Retrieve MODE SENSE page 3 - Format Device Page
5000	 */
5001	p3bufp = kmem_zalloc(SD_MODE_SENSE_PAGE3_LENGTH, KM_SLEEP);
5002	ssc = sd_ssc_init(un);
5003	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p3bufp,
5004	    SD_MODE_SENSE_PAGE3_LENGTH, SD_MODE_SENSE_PAGE3_CODE, path_flag);
5005	if (status != 0) {
5006		SD_ERROR(SD_LOG_COMMON, un,
5007		    "sd_get_physical_geometry: mode sense page 3 failed\n");
5008		goto page3_exit;
5009	}
5010
5011	/*
5012	 * Determine size of Block Descriptors in order to locate the mode
5013	 * page data.  ATAPI devices return 0, SCSI devices should return
5014	 * MODE_BLK_DESC_LENGTH.
5015	 */
5016	headerp = (struct mode_header *)p3bufp;
5017	if (un->un_f_cfg_is_atapi == TRUE) {
5018		struct mode_header_grp2 *mhp =
5019		    (struct mode_header_grp2 *)headerp;
5020		mode_header_length = MODE_HEADER_LENGTH_GRP2;
5021		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5022	} else {
5023		mode_header_length = MODE_HEADER_LENGTH;
5024		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5025	}
5026
5027	if (bd_len > MODE_BLK_DESC_LENGTH) {
5028		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5029		    "sd_get_physical_geometry: received unexpected bd_len "
5030		    "of %d, page3\n", bd_len);
5031		status = EIO;
5032		goto page3_exit;
5033	}
5034
5035	page3p = (struct mode_format *)
5036	    ((caddr_t)headerp + mode_header_length + bd_len);
5037
5038	if (page3p->mode_page.code != SD_MODE_SENSE_PAGE3_CODE) {
5039		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5040		    "sd_get_physical_geometry: mode sense pg3 code mismatch "
5041		    "%d\n", page3p->mode_page.code);
5042		status = EIO;
5043		goto page3_exit;
5044	}
5045
5046	/*
5047	 * Use this physical geometry data only if BOTH MODE SENSE commands
5048	 * complete successfully; otherwise, revert to the logical geometry.
5049	 * So, we need to save everything in temporary variables.
5050	 */
5051	sector_size = BE_16(page3p->data_bytes_sect);
5052
5053	/*
5054	 * 1243403: The NEC D38x7 drives do not support MODE SENSE sector size
5055	 */
5056	if (sector_size == 0) {
5057		sector_size = un->un_sys_blocksize;
5058	} else {
5059		sector_size &= ~(un->un_sys_blocksize - 1);
5060	}
5061
5062	nsect  = BE_16(page3p->sect_track);
5063	intrlv = BE_16(page3p->interleave);
5064
5065	SD_INFO(SD_LOG_COMMON, un,
5066	    "sd_get_physical_geometry: Format Parameters (page 3)\n");
5067	SD_INFO(SD_LOG_COMMON, un,
5068	    "   mode page: %d; nsect: %d; sector size: %d;\n",
5069	    page3p->mode_page.code, nsect, sector_size);
5070	SD_INFO(SD_LOG_COMMON, un,
5071	    "   interleave: %d; track skew: %d; cylinder skew: %d;\n", intrlv,
5072	    BE_16(page3p->track_skew),
5073	    BE_16(page3p->cylinder_skew));
5074
5075	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5076
5077	/*
5078	 * Retrieve MODE SENSE page 4 - Rigid Disk Drive Geometry Page
5079	 */
5080	p4bufp = kmem_zalloc(SD_MODE_SENSE_PAGE4_LENGTH, KM_SLEEP);
5081	status = sd_send_scsi_MODE_SENSE(ssc, cdbsize, p4bufp,
5082	    SD_MODE_SENSE_PAGE4_LENGTH, SD_MODE_SENSE_PAGE4_CODE, path_flag);
5083	if (status != 0) {
5084		SD_ERROR(SD_LOG_COMMON, un,
5085		    "sd_get_physical_geometry: mode sense page 4 failed\n");
5086		goto page4_exit;
5087	}
5088
5089	/*
5090	 * Determine size of Block Descriptors in order to locate the mode
5091	 * page data.  ATAPI devices return 0, SCSI devices should return
5092	 * MODE_BLK_DESC_LENGTH.
5093	 */
5094	headerp = (struct mode_header *)p4bufp;
5095	if (un->un_f_cfg_is_atapi == TRUE) {
5096		struct mode_header_grp2 *mhp =
5097		    (struct mode_header_grp2 *)headerp;
5098		bd_len = (mhp->bdesc_length_hi << 8) | mhp->bdesc_length_lo;
5099	} else {
5100		bd_len = ((struct mode_header *)headerp)->bdesc_length;
5101	}
5102
5103	if (bd_len > MODE_BLK_DESC_LENGTH) {
5104		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5105		    "sd_get_physical_geometry: received unexpected bd_len of "
5106		    "%d, page4\n", bd_len);
5107		status = EIO;
5108		goto page4_exit;
5109	}
5110
5111	page4p = (struct mode_geometry *)
5112	    ((caddr_t)headerp + mode_header_length + bd_len);
5113
5114	if (page4p->mode_page.code != SD_MODE_SENSE_PAGE4_CODE) {
5115		sd_ssc_set_info(ssc, SSC_FLAGS_INVALID_DATA, SD_LOG_COMMON,
5116		    "sd_get_physical_geometry: mode sense pg4 code mismatch "
5117		    "%d\n", page4p->mode_page.code);
5118		status = EIO;
5119		goto page4_exit;
5120	}
5121
5122	/*
5123	 * Stash the data now, after we know that both commands completed.
5124	 */
5125
5126
5127	nhead = (int)page4p->heads;	/* uchar, so no conversion needed */
5128	spc   = nhead * nsect;
5129	ncyl  = (page4p->cyl_ub << 16) + (page4p->cyl_mb << 8) + page4p->cyl_lb;
5130	rpm   = BE_16(page4p->rpm);
5131
5132	modesense_capacity = spc * ncyl;
5133
5134	SD_INFO(SD_LOG_COMMON, un,
5135	    "sd_get_physical_geometry: Geometry Parameters (page 4)\n");
5136	SD_INFO(SD_LOG_COMMON, un,
5137	    "   cylinders: %d; heads: %d; rpm: %d;\n", ncyl, nhead, rpm);
5138	SD_INFO(SD_LOG_COMMON, un,
5139	    "   computed capacity(h*s*c): %d;\n", modesense_capacity);
5140	SD_INFO(SD_LOG_COMMON, un, "   pgeom_p: %p; read cap: %d\n",
5141	    (void *)pgeom_p, capacity);
5142
5143	/*
5144	 * Compensate if the drive's geometry is not rectangular, i.e.,
5145	 * the product of C * H * S returned by MODE SENSE >= that returned
5146	 * by read capacity. This is an idiosyncrasy of the original x86
5147	 * disk subsystem.
5148	 */
5149	if (modesense_capacity >= capacity) {
5150		SD_INFO(SD_LOG_COMMON, un,
5151		    "sd_get_physical_geometry: adjusting acyl; "
5152		    "old: %d; new: %d\n", pgeom_p->g_acyl,
5153		    (modesense_capacity - capacity + spc - 1) / spc);
5154		if (sector_size != 0) {
5155			/* 1243403: NEC D38x7 drives don't support sec size */
5156			pgeom_p->g_secsize = (unsigned short)sector_size;
5157		}
5158		pgeom_p->g_nsect    = (unsigned short)nsect;
5159		pgeom_p->g_nhead    = (unsigned short)nhead;
5160		pgeom_p->g_capacity = capacity;
5161		pgeom_p->g_acyl	    =
5162		    (modesense_capacity - pgeom_p->g_capacity + spc - 1) / spc;
5163		pgeom_p->g_ncyl	    = ncyl - pgeom_p->g_acyl;
5164	}
5165
5166	pgeom_p->g_rpm    = (unsigned short)rpm;
5167	pgeom_p->g_intrlv = (unsigned short)intrlv;
5168	ret = 0;
5169
5170	SD_INFO(SD_LOG_COMMON, un,
5171	    "sd_get_physical_geometry: mode sense geometry:\n");
5172	SD_INFO(SD_LOG_COMMON, un,
5173	    "   nsect: %d; sector size: %d; interlv: %d\n",
5174	    nsect, sector_size, intrlv);
5175	SD_INFO(SD_LOG_COMMON, un,
5176	    "   nhead: %d; ncyl: %d; rpm: %d; capacity(ms): %d\n",
5177	    nhead, ncyl, rpm, modesense_capacity);
5178	SD_INFO(SD_LOG_COMMON, un,
5179	    "sd_get_physical_geometry: (cached)\n");
5180	SD_INFO(SD_LOG_COMMON, un,
5181	    "   ncyl: %ld; acyl: %d; nhead: %d; nsect: %d\n",
5182	    pgeom_p->g_ncyl,  pgeom_p->g_acyl,
5183	    pgeom_p->g_nhead, pgeom_p->g_nsect);
5184	SD_INFO(SD_LOG_COMMON, un,
5185	    "   lbasize: %d; capacity: %ld; intrlv: %d; rpm: %d\n",
5186	    pgeom_p->g_secsize, pgeom_p->g_capacity,
5187	    pgeom_p->g_intrlv, pgeom_p->g_rpm);
5188	sd_ssc_assessment(ssc, SD_FMT_STANDARD);
5189
5190page4_exit:
5191	kmem_free(p4bufp, SD_MODE_SENSE_PAGE4_LENGTH);
5192
5193page3_exit:
5194	kmem_free(p3bufp, SD_MODE_SENSE_PAGE3_LENGTH);
5195
5196	if (status != 0) {
5197		if (status == EIO) {
5198			/*
5199			 * Some disks do not support mode sense(6), we
5200			 * should ignore this kind of error(sense key is
5201			 * 0x5 - illegal request).
5202			 */
5203			uint8_t *sensep;
5204			int senlen;
5205
5206			sensep = (uint8_t *)ssc->ssc_uscsi_cmd->uscsi_rqbuf;
5207			senlen = (int)(ssc->ssc_uscsi_cmd->uscsi_rqlen -
5208			    ssc->ssc_uscsi_cmd->uscsi_rqresid);
5209
5210			if (senlen > 0 &&
5211			    scsi_sense_key(sensep) == KEY_ILLEGAL_REQUEST) {
5212				sd_ssc_assessment(ssc,
5213				    SD_FMT_IGNORE_COMPROMISE);
5214			} else {
5215				sd_ssc_assessment(ssc, SD_FMT_STATUS_CHECK);
5216			}
5217		} else {
5218			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5219		}
5220	}
5221	sd_ssc_fini(ssc);
5222	return (ret);
5223}
5224
5225/*
5226 *    Function: sd_get_virtual_geometry
5227 *
5228 * Description: Ask the controller to tell us about the target device.
5229 *
5230 *   Arguments: un - pointer to softstate
5231 *		capacity - disk capacity in #blocks
5232 *		lbasize - disk block size in bytes
5233 *
5234 *     Context: Kernel thread only
5235 */
5236
5237static int
5238sd_get_virtual_geometry(struct sd_lun *un, cmlb_geom_t *lgeom_p,
5239    diskaddr_t capacity, int lbasize)
5240{
5241	uint_t	geombuf;
5242	int	spc;
5243
5244	ASSERT(un != NULL);
5245
5246	/* Set sector size, and total number of sectors */
5247	(void) scsi_ifsetcap(SD_ADDRESS(un), "sector-size",   lbasize,  1);
5248	(void) scsi_ifsetcap(SD_ADDRESS(un), "total-sectors", capacity, 1);
5249
5250	/* Let the HBA tell us its geometry */
5251	geombuf = (uint_t)scsi_ifgetcap(SD_ADDRESS(un), "geometry", 1);
5252
5253	/* A value of -1 indicates an undefined "geometry" property */
5254	if (geombuf == (-1)) {
5255		return (EINVAL);
5256	}
5257
5258	/* Initialize the logical geometry cache. */
5259	lgeom_p->g_nhead   = (geombuf >> 16) & 0xffff;
5260	lgeom_p->g_nsect   = geombuf & 0xffff;
5261	lgeom_p->g_secsize = un->un_sys_blocksize;
5262
5263	spc = lgeom_p->g_nhead * lgeom_p->g_nsect;
5264
5265	/*
5266	 * Note: The driver originally converted the capacity value from
5267	 * target blocks to system blocks. However, the capacity value passed
5268	 * to this routine is already in terms of system blocks (this scaling
5269	 * is done when the READ CAPACITY command is issued and processed).
5270	 * This 'error' may have gone undetected because the usage of g_ncyl
5271	 * (which is based upon g_capacity) is very limited within the driver
5272	 */
5273	lgeom_p->g_capacity = capacity;
5274
5275	/*
5276	 * Set ncyl to zero if the hba returned a zero nhead or nsect value. The
5277	 * hba may return zero values if the device has been removed.
5278	 */
5279	if (spc == 0) {
5280		lgeom_p->g_ncyl = 0;
5281	} else {
5282		lgeom_p->g_ncyl = lgeom_p->g_capacity / spc;
5283	}
5284	lgeom_p->g_acyl = 0;
5285
5286	SD_INFO(SD_LOG_COMMON, un, "sd_get_virtual_geometry: (cached)\n");
5287	return (0);
5288
5289}
5290/*
5291 *    Function: sd_update_block_info
5292 *
5293 * Description: Calculate a byte count to sector count bitshift value
5294 *		from sector size.
5295 *
5296 *   Arguments: un: unit struct.
5297 *		lbasize: new target sector size
5298 *		capacity: new target capacity, ie. block count
5299 *
5300 *     Context: Kernel thread context
5301 */
5302
5303static void
5304sd_update_block_info(struct sd_lun *un, uint32_t lbasize, uint64_t capacity)
5305{
5306	if (lbasize != 0) {
5307		un->un_tgt_blocksize = lbasize;
5308		un->un_f_tgt_blocksize_is_valid = TRUE;
5309		if (!un->un_f_has_removable_media) {
5310			un->un_sys_blocksize = lbasize;
5311		}
5312	}
5313
5314	if (capacity != 0) {
5315		un->un_blockcount		= capacity;
5316		un->un_f_blockcount_is_valid	= TRUE;
5317
5318		/*
5319		 * The capacity has changed so update the errstats.
5320		 */
5321		if (un->un_errstats != NULL) {
5322			struct sd_errstats *stp;
5323
5324			capacity *= un->un_sys_blocksize;
5325			stp = (struct sd_errstats *)un->un_errstats->ks_data;
5326			if (stp->sd_capacity.value.ui64 < capacity)
5327				stp->sd_capacity.value.ui64 = capacity;
5328		}
5329	}
5330}
5331
5332/*
5333 * Parses the SCSI Block Limits VPD page (0xB0). It's legal to pass NULL for
5334 * vpd_pg, in which case all the block limits will be reset to the defaults.
5335 */
5336static void
5337sd_parse_blk_limits_vpd(struct sd_lun *un, uchar_t *vpd_pg)
5338{
5339	sd_blk_limits_t *lim = &un->un_blk_lim;
5340	unsigned pg_len;
5341
5342	if (vpd_pg != NULL)
5343		pg_len = BE_IN16(&vpd_pg[2]);
5344	else
5345		pg_len = 0;
5346
5347	/* Block Limits VPD can be 16 bytes or 64 bytes long - support both */
5348	if (pg_len >= 0x10) {
5349		lim->lim_opt_xfer_len_gran = BE_IN16(&vpd_pg[6]);
5350		lim->lim_max_xfer_len = BE_IN32(&vpd_pg[8]);
5351		lim->lim_opt_xfer_len = BE_IN32(&vpd_pg[12]);
5352
5353		/* Zero means not reported, so use "unlimited" */
5354		if (lim->lim_max_xfer_len == 0)
5355			lim->lim_max_xfer_len = UINT32_MAX;
5356		if (lim->lim_opt_xfer_len == 0)
5357			lim->lim_opt_xfer_len = UINT32_MAX;
5358	} else {
5359		lim->lim_opt_xfer_len_gran = 0;
5360		lim->lim_max_xfer_len = UINT32_MAX;
5361		lim->lim_opt_xfer_len = UINT32_MAX;
5362	}
5363	if (pg_len >= 0x3c) {
5364		lim->lim_max_pfetch_len = BE_IN32(&vpd_pg[16]);
5365		/*
5366		 * A zero in either of the following two fields indicates lack
5367		 * of UNMAP support.
5368		 */
5369		lim->lim_max_unmap_lba_cnt = BE_IN32(&vpd_pg[20]);
5370		lim->lim_max_unmap_descr_cnt = BE_IN32(&vpd_pg[24]);
5371		lim->lim_opt_unmap_gran = BE_IN32(&vpd_pg[28]);
5372		if ((vpd_pg[32] >> 7) == 1) {
5373			lim->lim_unmap_gran_align =
5374			    ((vpd_pg[32] & 0x7f) << 24) | (vpd_pg[33] << 16) |
5375			    (vpd_pg[34] << 8) | vpd_pg[35];
5376		} else {
5377			lim->lim_unmap_gran_align = 0;
5378		}
5379		lim->lim_max_write_same_len = BE_IN64(&vpd_pg[36]);
5380	} else {
5381		lim->lim_max_pfetch_len = UINT32_MAX;
5382		lim->lim_max_unmap_lba_cnt = UINT32_MAX;
5383		lim->lim_max_unmap_descr_cnt = SD_UNMAP_MAX_DESCR;
5384		lim->lim_opt_unmap_gran = 0;
5385		lim->lim_unmap_gran_align = 0;
5386		lim->lim_max_write_same_len = UINT64_MAX;
5387	}
5388}
5389
5390/*
5391 * Collects VPD page B0 data if available (block limits). If the data is
5392 * not available or querying the device failed, we revert to the defaults.
5393 */
5394static void
5395sd_setup_blk_limits(sd_ssc_t *ssc)
5396{
5397	struct sd_lun	*un		= ssc->ssc_un;
5398	uchar_t		*inqB0		= NULL;
5399	size_t		inqB0_resid	= 0;
5400	int		rval;
5401
5402	if (un->un_vpd_page_mask & SD_VPD_BLK_LIMITS_PG) {
5403		inqB0 = kmem_zalloc(MAX_INQUIRY_SIZE, KM_SLEEP);
5404		rval = sd_send_scsi_INQUIRY(ssc, inqB0, MAX_INQUIRY_SIZE, 0x01,
5405		    0xB0, &inqB0_resid);
5406		if (rval != 0) {
5407			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5408			kmem_free(inqB0, MAX_INQUIRY_SIZE);
5409			inqB0 = NULL;
5410		}
5411	}
5412	/* passing NULL inqB0 will reset to defaults */
5413	sd_parse_blk_limits_vpd(ssc->ssc_un, inqB0);
5414	if (inqB0)
5415		kmem_free(inqB0, MAX_INQUIRY_SIZE);
5416}
5417
5418/*
5419 *    Function: sd_register_devid
5420 *
5421 * Description: This routine will obtain the device id information from the
5422 *		target, obtain the serial number, and register the device
5423 *		id with the ddi framework.
5424 *
5425 *   Arguments: devi - the system's dev_info_t for the device.
5426 *		un - driver soft state (unit) structure
5427 *		reservation_flag - indicates if a reservation conflict
5428 *		occurred during attach
5429 *
5430 *     Context: Kernel Thread
5431 */
5432static void
5433sd_register_devid(sd_ssc_t *ssc, dev_info_t *devi, int reservation_flag)
5434{
5435	int		rval		= 0;
5436	uchar_t		*inq80		= NULL;
5437	size_t		inq80_len	= MAX_INQUIRY_SIZE;
5438	size_t		inq80_resid	= 0;
5439	uchar_t		*inq83		= NULL;
5440	size_t		inq83_len	= MAX_INQUIRY_SIZE;
5441	size_t		inq83_resid	= 0;
5442	int		dlen, len;
5443	char		*sn;
5444	struct sd_lun	*un;
5445
5446	ASSERT(ssc != NULL);
5447	un = ssc->ssc_un;
5448	ASSERT(un != NULL);
5449	ASSERT(mutex_owned(SD_MUTEX(un)));
5450	ASSERT((SD_DEVINFO(un)) == devi);
5451
5452
5453	/*
5454	 * We check the availability of the World Wide Name (0x83) and Unit
5455	 * Serial Number (0x80) pages in sd_check_vpd_page_support(), and using
5456	 * un_vpd_page_mask from them, we decide which way to get the WWN.  If
5457	 * 0x83 is available, that is the best choice.  Our next choice is
5458	 * 0x80.  If neither are available, we munge the devid from the device
5459	 * vid/pid/serial # for Sun qualified disks, or use the ddi framework
5460	 * to fabricate a devid for non-Sun qualified disks.
5461	 */
5462	if (sd_check_vpd_page_support(ssc) == 0) {
5463		/* collect page 80 data if available */
5464		if (un->un_vpd_page_mask & SD_VPD_UNIT_SERIAL_PG) {
5465
5466			mutex_exit(SD_MUTEX(un));
5467			inq80 = kmem_zalloc(inq80_len, KM_SLEEP);
5468
5469			rval = sd_send_scsi_INQUIRY(ssc, inq80, inq80_len,
5470			    0x01, 0x80, &inq80_resid);
5471
5472			if (rval != 0) {
5473				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5474				kmem_free(inq80, inq80_len);
5475				inq80 = NULL;
5476				inq80_len = 0;
5477			} else if (ddi_prop_exists(
5478			    DDI_DEV_T_NONE, SD_DEVINFO(un),
5479			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
5480			    INQUIRY_SERIAL_NO) == 0) {
5481				/*
5482				 * If we don't already have a serial number
5483				 * property, do quick verify of data returned
5484				 * and define property.
5485				 */
5486				dlen = inq80_len - inq80_resid;
5487				len = (size_t)inq80[3];
5488				if ((dlen >= 4) && ((len + 4) <= dlen)) {
5489					/*
5490					 * Ensure sn termination, skip leading
5491					 * blanks, and create property
5492					 * 'inquiry-serial-no'.
5493					 */
5494					sn = (char *)&inq80[4];
5495					sn[len] = 0;
5496					while (*sn && (*sn == ' '))
5497						sn++;
5498					if (*sn) {
5499						(void) ddi_prop_update_string(
5500						    DDI_DEV_T_NONE,
5501						    SD_DEVINFO(un),
5502						    INQUIRY_SERIAL_NO, sn);
5503					}
5504				}
5505			}
5506			mutex_enter(SD_MUTEX(un));
5507		}
5508
5509		/* collect page 83 data if available */
5510		if (un->un_vpd_page_mask & SD_VPD_DEVID_WWN_PG) {
5511			mutex_exit(SD_MUTEX(un));
5512			inq83 = kmem_zalloc(inq83_len, KM_SLEEP);
5513
5514			rval = sd_send_scsi_INQUIRY(ssc, inq83, inq83_len,
5515			    0x01, 0x83, &inq83_resid);
5516
5517			if (rval != 0) {
5518				sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5519				kmem_free(inq83, inq83_len);
5520				inq83 = NULL;
5521				inq83_len = 0;
5522			}
5523			mutex_enter(SD_MUTEX(un));
5524		}
5525	}
5526
5527	/*
5528	 * If transport has already registered a devid for this target
5529	 * then that takes precedence over the driver's determination
5530	 * of the devid.
5531	 *
5532	 * NOTE: The reason this check is done here instead of at the beginning
5533	 * of the function is to allow the code above to create the
5534	 * 'inquiry-serial-no' property.
5535	 */
5536	if (ddi_devid_get(SD_DEVINFO(un), &un->un_devid) == DDI_SUCCESS) {
5537		ASSERT(un->un_devid);
5538		un->un_f_devid_transport_defined = TRUE;
5539		goto cleanup; /* use devid registered by the transport */
5540	}
5541
5542	/*
5543	 * This is the case of antiquated Sun disk drives that have the
5544	 * FAB_DEVID property set in the disk_table.  These drives
5545	 * manage the devid's by storing them in last 2 available sectors
5546	 * on the drive and have them fabricated by the ddi layer by calling
5547	 * ddi_devid_init and passing the DEVID_FAB flag.
5548	 */
5549	if (un->un_f_opt_fab_devid == TRUE) {
5550		/*
5551		 * Depending on EINVAL isn't reliable, since a reserved disk
5552		 * may result in invalid geometry, so check to make sure a
5553		 * reservation conflict did not occur during attach.
5554		 */
5555		if ((sd_get_devid(ssc) == EINVAL) &&
5556		    (reservation_flag != SD_TARGET_IS_RESERVED)) {
5557			/*
5558			 * The devid is invalid AND there is no reservation
5559			 * conflict.  Fabricate a new devid.
5560			 */
5561			(void) sd_create_devid(ssc);
5562		}
5563
5564		/* Register the devid if it exists */
5565		if (un->un_devid != NULL) {
5566			(void) ddi_devid_register(SD_DEVINFO(un),
5567			    un->un_devid);
5568			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5569			    "sd_register_devid: Devid Fabricated\n");
5570		}
5571		goto cleanup;
5572	}
5573
5574	/* encode best devid possible based on data available */
5575	if (ddi_devid_scsi_encode(DEVID_SCSI_ENCODE_VERSION_LATEST,
5576	    (char *)ddi_driver_name(SD_DEVINFO(un)),
5577	    (uchar_t *)SD_INQUIRY(un), sizeof (*SD_INQUIRY(un)),
5578	    inq80, inq80_len - inq80_resid, inq83, inq83_len -
5579	    inq83_resid, &un->un_devid) == DDI_SUCCESS) {
5580
5581		/* devid successfully encoded, register devid */
5582		(void) ddi_devid_register(SD_DEVINFO(un), un->un_devid);
5583
5584	} else {
5585		/*
5586		 * Unable to encode a devid based on data available.
5587		 * This is not a Sun qualified disk.  Older Sun disk
5588		 * drives that have the SD_FAB_DEVID property
5589		 * set in the disk_table and non Sun qualified
5590		 * disks are treated in the same manner.  These
5591		 * drives manage the devid's by storing them in
5592		 * last 2 available sectors on the drive and
5593		 * have them fabricated by the ddi layer by
5594		 * calling ddi_devid_init and passing the
5595		 * DEVID_FAB flag.
5596		 * Create a fabricate devid only if there's no
5597		 * fabricate devid existed.
5598		 */
5599		if (sd_get_devid(ssc) == EINVAL) {
5600			(void) sd_create_devid(ssc);
5601		}
5602		un->un_f_opt_fab_devid = TRUE;
5603
5604		/* Register the devid if it exists */
5605		if (un->un_devid != NULL) {
5606			(void) ddi_devid_register(SD_DEVINFO(un),
5607			    un->un_devid);
5608			SD_INFO(SD_LOG_ATTACH_DETACH, un,
5609			    "sd_register_devid: devid fabricated using "
5610			    "ddi framework\n");
5611		}
5612	}
5613
5614cleanup:
5615	/* clean up resources */
5616	if (inq80 != NULL) {
5617		kmem_free(inq80, inq80_len);
5618	}
5619	if (inq83 != NULL) {
5620		kmem_free(inq83, inq83_len);
5621	}
5622}
5623
5624
5625
5626/*
5627 *    Function: sd_get_devid
5628 *
5629 * Description: This routine will return 0 if a valid device id has been
5630 *		obtained from the target and stored in the soft state. If a
5631 *		valid device id has not been previously read and stored, a
5632 *		read attempt will be made.
5633 *
5634 *   Arguments: un - driver soft state (unit) structure
5635 *
5636 * Return Code: 0 if we successfully get the device id
5637 *
5638 *     Context: Kernel Thread
5639 */
5640
5641static int
5642sd_get_devid(sd_ssc_t *ssc)
5643{
5644	struct dk_devid		*dkdevid;
5645	ddi_devid_t		tmpid;
5646	uint_t			*ip;
5647	size_t			sz;
5648	diskaddr_t		blk;
5649	int			status;
5650	int			chksum;
5651	int			i;
5652	size_t			buffer_size;
5653	struct sd_lun		*un;
5654
5655	ASSERT(ssc != NULL);
5656	un = ssc->ssc_un;
5657	ASSERT(un != NULL);
5658	ASSERT(mutex_owned(SD_MUTEX(un)));
5659
5660	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: entry: un: 0x%p\n",
5661	    un);
5662
5663	if (un->un_devid != NULL) {
5664		return (0);
5665	}
5666
5667	mutex_exit(SD_MUTEX(un));
5668	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5669	    (void *)SD_PATH_DIRECT) != 0) {
5670		mutex_enter(SD_MUTEX(un));
5671		return (EINVAL);
5672	}
5673
5674	/*
5675	 * Read and verify device id, stored in the reserved cylinders at the
5676	 * end of the disk. Backup label is on the odd sectors of the last
5677	 * track of the last cylinder. Device id will be on track of the next
5678	 * to last cylinder.
5679	 */
5680	mutex_enter(SD_MUTEX(un));
5681	buffer_size = SD_REQBYTES2TGTBYTES(un, sizeof (struct dk_devid));
5682	mutex_exit(SD_MUTEX(un));
5683	dkdevid = kmem_alloc(buffer_size, KM_SLEEP);
5684	status = sd_send_scsi_READ(ssc, dkdevid, buffer_size, blk,
5685	    SD_PATH_DIRECT);
5686
5687	if (status != 0) {
5688		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5689		goto error;
5690	}
5691
5692	/* Validate the revision */
5693	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
5694	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
5695		status = EINVAL;
5696		goto error;
5697	}
5698
5699	/* Calculate the checksum */
5700	chksum = 0;
5701	ip = (uint_t *)dkdevid;
5702	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5703	    i++) {
5704		chksum ^= ip[i];
5705	}
5706
5707	/* Compare the checksums */
5708	if (DKD_GETCHKSUM(dkdevid) != chksum) {
5709		status = EINVAL;
5710		goto error;
5711	}
5712
5713	/* Validate the device id */
5714	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
5715		status = EINVAL;
5716		goto error;
5717	}
5718
5719	/*
5720	 * Store the device id in the driver soft state
5721	 */
5722	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
5723	tmpid = kmem_alloc(sz, KM_SLEEP);
5724
5725	mutex_enter(SD_MUTEX(un));
5726
5727	un->un_devid = tmpid;
5728	bcopy(&dkdevid->dkd_devid, un->un_devid, sz);
5729
5730	kmem_free(dkdevid, buffer_size);
5731
5732	SD_TRACE(SD_LOG_ATTACH_DETACH, un, "sd_get_devid: exit: un:0x%p\n", un);
5733
5734	return (status);
5735error:
5736	mutex_enter(SD_MUTEX(un));
5737	kmem_free(dkdevid, buffer_size);
5738	return (status);
5739}
5740
5741
5742/*
5743 *    Function: sd_create_devid
5744 *
5745 * Description: This routine will fabricate the device id and write it
5746 *		to the disk.
5747 *
5748 *   Arguments: un - driver soft state (unit) structure
5749 *
5750 * Return Code: value of the fabricated device id
5751 *
5752 *     Context: Kernel Thread
5753 */
5754
5755static ddi_devid_t
5756sd_create_devid(sd_ssc_t *ssc)
5757{
5758	struct sd_lun	*un;
5759
5760	ASSERT(ssc != NULL);
5761	un = ssc->ssc_un;
5762	ASSERT(un != NULL);
5763
5764	/* Fabricate the devid */
5765	if (ddi_devid_init(SD_DEVINFO(un), DEVID_FAB, 0, NULL, &un->un_devid)
5766	    == DDI_FAILURE) {
5767		return (NULL);
5768	}
5769
5770	/* Write the devid to disk */
5771	if (sd_write_deviceid(ssc) != 0) {
5772		ddi_devid_free(un->un_devid);
5773		un->un_devid = NULL;
5774	}
5775
5776	return (un->un_devid);
5777}
5778
5779
5780/*
5781 *    Function: sd_write_deviceid
5782 *
5783 * Description: This routine will write the device id to the disk
5784 *		reserved sector.
5785 *
5786 *   Arguments: un - driver soft state (unit) structure
5787 *
5788 * Return Code: EINVAL
5789 *		value returned by sd_send_scsi_cmd
5790 *
5791 *     Context: Kernel Thread
5792 */
5793
5794static int
5795sd_write_deviceid(sd_ssc_t *ssc)
5796{
5797	struct dk_devid		*dkdevid;
5798	uchar_t			*buf;
5799	diskaddr_t		blk;
5800	uint_t			*ip, chksum;
5801	int			status;
5802	int			i;
5803	struct sd_lun		*un;
5804
5805	ASSERT(ssc != NULL);
5806	un = ssc->ssc_un;
5807	ASSERT(un != NULL);
5808	ASSERT(mutex_owned(SD_MUTEX(un)));
5809
5810	mutex_exit(SD_MUTEX(un));
5811	if (cmlb_get_devid_block(un->un_cmlbhandle, &blk,
5812	    (void *)SD_PATH_DIRECT) != 0) {
5813		mutex_enter(SD_MUTEX(un));
5814		return (-1);
5815	}
5816
5817
5818	/* Allocate the buffer */
5819	buf = kmem_zalloc(un->un_sys_blocksize, KM_SLEEP);
5820	dkdevid = (struct dk_devid *)buf;
5821
5822	/* Fill in the revision */
5823	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
5824	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
5825
5826	/* Copy in the device id */
5827	mutex_enter(SD_MUTEX(un));
5828	bcopy(un->un_devid, &dkdevid->dkd_devid,
5829	    ddi_devid_sizeof(un->un_devid));
5830	mutex_exit(SD_MUTEX(un));
5831
5832	/* Calculate the checksum */
5833	chksum = 0;
5834	ip = (uint_t *)dkdevid;
5835	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int));
5836	    i++) {
5837		chksum ^= ip[i];
5838	}
5839
5840	/* Fill-in checksum */
5841	DKD_FORMCHKSUM(chksum, dkdevid);
5842
5843	/* Write the reserved sector */
5844	status = sd_send_scsi_WRITE(ssc, buf, un->un_sys_blocksize, blk,
5845	    SD_PATH_DIRECT);
5846	if (status != 0)
5847		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5848
5849	kmem_free(buf, un->un_sys_blocksize);
5850
5851	mutex_enter(SD_MUTEX(un));
5852	return (status);
5853}
5854
5855
5856/*
5857 *    Function: sd_check_vpd_page_support
5858 *
5859 * Description: This routine sends an inquiry command with the EVPD bit set and
5860 *		a page code of 0x00 to the device. It is used to determine which
5861 *		vital product pages are available to find the devid. We are
5862 *		looking for pages 0x83 0x80 or 0xB1.  If we return a negative 1,
5863 *		the device does not support that command.
5864 *
5865 *   Arguments: un  - driver soft state (unit) structure
5866 *
5867 * Return Code: 0 - success
5868 *		1 - check condition
5869 *
5870 *     Context: This routine can sleep.
5871 */
5872
5873static int
5874sd_check_vpd_page_support(sd_ssc_t *ssc)
5875{
5876	uchar_t	*page_list	= NULL;
5877	uchar_t	page_length	= 0xff;	/* Use max possible length */
5878	uchar_t	evpd		= 0x01;	/* Set the EVPD bit */
5879	uchar_t	page_code	= 0x00;	/* Supported VPD Pages */
5880	int	rval		= 0;
5881	int	counter;
5882	struct sd_lun		*un;
5883
5884	ASSERT(ssc != NULL);
5885	un = ssc->ssc_un;
5886	ASSERT(un != NULL);
5887	ASSERT(mutex_owned(SD_MUTEX(un)));
5888
5889	mutex_exit(SD_MUTEX(un));
5890
5891	/*
5892	 * We'll set the page length to the maximum to save figuring it out
5893	 * with an additional call.
5894	 */
5895	page_list =  kmem_zalloc(page_length, KM_SLEEP);
5896
5897	rval = sd_send_scsi_INQUIRY(ssc, page_list, page_length, evpd,
5898	    page_code, NULL);
5899
5900	if (rval != 0)
5901		sd_ssc_assessment(ssc, SD_FMT_IGNORE);
5902
5903	mutex_enter(SD_MUTEX(un));
5904
5905	/*
5906	 * Now we must validate that the device accepted the command, as some
5907	 * drives do not support it.  If the drive does support it, we will
5908	 * return 0, and the supported pages will be in un_vpd_page_mask.  If
5909	 * not, we return -1.
5910	 */
5911	if ((rval == 0) && (page_list[VPD_MODE_PAGE] == 0x00)) {
5912		/* Loop to find one of the 2 pages we need */
5913		counter = 4;  /* Supported pages start at byte 4, with 0x00 */
5914
5915		/*
5916		 * Pages are returned in ascending order, and 0x83 is what we
5917		 * are hoping for.
5918		 */
5919		while ((page_list[counter] <= 0xB1) &&
5920		    (counter <= (page_list[VPD_PAGE_LENGTH] +
5921		    VPD_HEAD_OFFSET))) {
5922			/*
5923			 * Add 3 because page_list[3] is the number of
5924			 * pages minus 3
5925			 */
5926
5927			switch (page_list[counter]) {
5928			case 0x00:
5929				un->un_vpd_page_mask |= SD_VPD_SUPPORTED_PG;
5930				break;
5931			case 0x80:
5932				un->un_vpd_page_mask |= SD_VPD_UNIT_SERIAL_PG;
5933				break;
5934			case 0x81:
5935				un->un_vpd_page_mask |= SD_VPD_OPERATING_PG;
5936				break;
5937			case 0x82:
5938				un->un_vpd_page_mask |= SD_VPD_ASCII_OP_PG;
5939				break;
5940			case 0x83:
5941				un->un_vpd_page_mask |= SD_VPD_DEVID_WWN_PG;
5942				break;
5943			case 0x86:
5944				un->un_vpd_page_mask |= SD_VPD_EXTENDED_DATA_PG;
5945				break;
5946			case 0xB0:
5947				un->un_vpd_page_mask |= SD_VPD_BLK_LIMITS_PG;
5948				break;
5949			case 0xB1:
5950				un->un_vpd_page_mask |= SD_VPD_DEV_CHARACTER_PG;
5951				break;
5952			}
5953			counter++;
5954		}
5955
5956	} else {
5957		rval = -1;
5958
5959		SD_INFO(SD_LOG_ATTACH_DETACH, un,
5960		    "sd_check_vpd_page_support: This drive does not implement "
5961		    "VPD pages.\n");
5962	}
5963
5964	kmem_free(page_list, page_length);
5965
5966	return (rval);
5967}
5968
5969
5970/*
5971 *    Function: sd_setup_pm
5972 *
5973 * Description: Initialize Power Management on the device
5974 *
5975 *     Context: Kernel Thread
5976 */
5977
5978static void
5979sd_setup_pm(sd_ssc_t *ssc, dev_info_t *devi)
5980{
5981	uint_t		log_page_size;
5982	uchar_t		*log_page_data;
5983	int		rval = 0;
5984	struct sd_lun	*un;
5985
5986	ASSERT(ssc != NULL);
5987	un = ssc->ssc_un;
5988	ASSERT(un != NULL);
5989
5990	/*
5991	 * Since we are called from attach, holding a mutex for
5992	 * un is unnecessary. Because some of the routines called
5993	 * from here require SD_MUTEX to not be held, assert this
5994	 * right up front.
5995	 */
5996	ASSERT(!mutex_owned(SD_MUTEX(un)));
5997	/*
5998	 * Since the sd device does not have the 'reg' property,
5999	 * cpr will not call its DDI_SUSPEND/DDI_RESUME entries.
6000	 * The following code is to tell cpr that this device
6001	 * DOES need to be suspended and resumed.
6002	 */
6003	(void) ddi_prop_update_string(DDI_DEV_T_NONE, devi,
6004	    "pm-hardware-state", "needs-suspend-resume");
6005
6006	/*
6007	 * This complies with the new power management framework
6008	 * for certain desktop machines. Create the pm_components
6009	 * property as a string array property.
6010	 * If un_f_pm_supported is TRUE, that means the disk
6011	 * attached HBA has set the "pm-capable" property and
6012	 * the value of this property is bigger than 0.
6013	 */
6014	if (un->un_f_pm_supported) {
6015		/*
6016		 * not all devices have a motor, try it first.
6017		 * some devices may return ILLEGAL REQUEST, some
6018		 * will hang
6019		 * The following START_STOP_UNIT is used to check if target
6020		 * device has a motor.
6021		 */
6022		un->un_f_start_stop_supported = TRUE;
6023
6024		if (un->un_f_power_condition_supported) {
6025			rval = sd_send_scsi_START_STOP_UNIT(ssc,
6026			    SD_POWER_CONDITION, SD_TARGET_ACTIVE,
6027			    SD_PATH_DIRECT);
6028			if (rval != 0) {
6029				un->un_f_power_condition_supported = FALSE;
6030			}
6031		}
6032		if (!un->un_f_power_condition_supported) {
6033			rval = sd_send_scsi_START_STOP_UNIT(ssc,
6034			    SD_START_STOP, SD_TARGET_START, SD_PATH_DIRECT);
6035		}
6036		if (rval != 0) {
6037			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6038			un->un_f_start_stop_supported = FALSE;
6039		}
6040
6041		/*
6042		 * create pm properties anyways otherwise the parent can't
6043		 * go to sleep
6044		 */
6045		un->un_f_pm_is_enabled = TRUE;
6046		(void) sd_create_pm_components(devi, un);
6047
6048		/*
6049		 * If it claims that log sense is supported, check it out.
6050		 */
6051		if (un->un_f_log_sense_supported) {
6052			rval = sd_log_page_supported(ssc,
6053			    START_STOP_CYCLE_PAGE);
6054			if (rval == 1) {
6055				/* Page found, use it. */
6056				un->un_start_stop_cycle_page =
6057				    START_STOP_CYCLE_PAGE;
6058			} else {
6059				/*
6060				 * Page not found or log sense is not
6061				 * supported.
6062				 * Notice we do not check the old style
6063				 * START_STOP_CYCLE_VU_PAGE because this
6064				 * code path does not apply to old disks.
6065				 */
6066				un->un_f_log_sense_supported = FALSE;
6067				un->un_f_pm_log_sense_smart = FALSE;
6068			}
6069		}
6070
6071		return;
6072	}
6073
6074	/*
6075	 * For the disk whose attached HBA has not set the "pm-capable"
6076	 * property, check if it supports the power management.
6077	 */
6078	if (!un->un_f_log_sense_supported) {
6079		un->un_power_level = SD_SPINDLE_ON;
6080		un->un_f_pm_is_enabled = FALSE;
6081		return;
6082	}
6083
6084	rval = sd_log_page_supported(ssc, START_STOP_CYCLE_PAGE);
6085
6086#ifdef	SDDEBUG
6087	if (sd_force_pm_supported) {
6088		/* Force a successful result */
6089		rval = 1;
6090	}
6091#endif
6092
6093	/*
6094	 * If the start-stop cycle counter log page is not supported
6095	 * or if the pm-capable property is set to be false (0),
6096	 * then we should not create the pm_components property.
6097	 */
6098	if (rval == -1) {
6099		/*
6100		 * Error.
6101		 * Reading log sense failed, most likely this is
6102		 * an older drive that does not support log sense.
6103		 * If this fails auto-pm is not supported.
6104		 */
6105		un->un_power_level = SD_SPINDLE_ON;
6106		un->un_f_pm_is_enabled = FALSE;
6107
6108	} else if (rval == 0) {
6109		/*
6110		 * Page not found.
6111		 * The start stop cycle counter is implemented as page
6112		 * START_STOP_CYCLE_PAGE_VU_PAGE (0x31) in older disks. For
6113		 * newer disks it is implemented as START_STOP_CYCLE_PAGE (0xE).
6114		 */
6115		if (sd_log_page_supported(ssc, START_STOP_CYCLE_VU_PAGE) == 1) {
6116			/*
6117			 * Page found, use this one.
6118			 */
6119			un->un_start_stop_cycle_page = START_STOP_CYCLE_VU_PAGE;
6120			un->un_f_pm_is_enabled = TRUE;
6121		} else {
6122			/*
6123			 * Error or page not found.
6124			 * auto-pm is not supported for this device.
6125			 */
6126			un->un_power_level = SD_SPINDLE_ON;
6127			un->un_f_pm_is_enabled = FALSE;
6128		}
6129	} else {
6130		/*
6131		 * Page found, use it.
6132		 */
6133		un->un_start_stop_cycle_page = START_STOP_CYCLE_PAGE;
6134		un->un_f_pm_is_enabled = TRUE;
6135	}
6136
6137
6138	if (un->un_f_pm_is_enabled == TRUE) {
6139		log_page_size = START_STOP_CYCLE_COUNTER_PAGE_SIZE;
6140		log_page_data = kmem_zalloc(log_page_size, KM_SLEEP);
6141
6142		rval = sd_send_scsi_LOG_SENSE(ssc, log_page_data,
6143		    log_page_size, un->un_start_stop_cycle_page,
6144		    0x01, 0, SD_PATH_DIRECT);
6145
6146		if (rval != 0) {
6147			sd_ssc_assessment(ssc, SD_FMT_IGNORE);
6148		}
6149
6150#ifdef	SDDEBUG
6151		if (sd_force_pm_supported) {
6152			/* Force a successful result */
6153			rval = 0;
6154		}
6155#endif
6156
6157		/*
6158		 * If the Log sense for Page( Start/stop cycle counter page)
6159		 * succeeds, then power management is supported and we can
6160		 * enable auto-pm.
6161		 */
6162		if (rval == 0)  {
6163			(void) sd_create_pm_components(devi, un);
6164		} else {
6165			un->un_power_level = SD_SPINDLE_ON;
6166			un->un_f_pm_is_enabled = FALSE;
6167		}
6168
6169		kmem_free(log_page_data, log_page_size);
6170	}
6171}
6172
6173
6174/*
6175 *    Function: sd_create_pm_components
6176 *
6177 * Description: Initialize PM property.
6178 *
6179 *     Context: Kernel thread context
6180 */
6181
6182static void
6183sd_create_pm_components(dev_info_t *devi, struct sd_lun *un)
6184{
6185	ASSERT(!mutex_owned(SD_MUTEX(un)));
6186
6187	if (un->un_f_power_condition_supported) {
6188		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6189		    "pm-components", sd_pwr_pc.pm_comp, 5)
6190		    != DDI_PROP_SUCCESS) {
6191			un->un_power_level = SD_SPINDLE_ACTIVE;
6192			un->un_f_pm_is_enabled = FALSE;
6193			return;
6194		}
6195	} else {
6196		if (ddi_prop_update_string_array(DDI_DEV_T_NONE, devi,
6197		    "pm-components", sd_pwr_ss.pm_comp, 3)
6198		    != DDI_PROP_SUCCESS) {
6199			un->un_power_level = SD_SPINDLE_ON;
6200			un->un_f_pm_is_enabled = FALSE;
6201			return;
6202		}
6203	}
6204	/*
6205	 * When components are initially created they are idle,
6206	 * power up any non-removables.
6207	 * Note: the return value of pm_raise_power can't be used
6208	 * for determining if PM should be enabled for this device.
6209	 * Even if you check the return values and remove this
6210	 * property created above, the PM framework will not honor the
6211	 * change after the first call to pm_raise_power. Hence,
6212	 * removal of that property does not help if pm_raise_power
6213	 * fails. In the case of removable media, the start/stop
6214	 * will fail if the media is not present.
6215	 */
6216	if (un->un_f_attach_spinup && (pm_raise_power(SD_DEVINFO(un), 0,
6217	    SD_PM_STATE_ACTIVE(un)) == DDI_SUCCESS)) {
6218		mutex_enter(SD_MUTEX(un));
6219		un->un_power_level = SD_PM_STATE_ACTIVE(un);
6220		mutex_enter(&un->un_pm_mutex);
6221		/* Set to on and not busy. */
6222		un->un_pm_count = 0;
6223	} else {
6224		mutex_enter(SD_MUTEX(un));
6225		un->un_power_level = SD_PM_STATE_STOPPED(un);
6226		mutex_enter(&un->un_pm_mutex);
6227		/* Set to off. */
6228		un->un_pm_count = -1;
6229	}
6230	mutex_exit(&un->un_pm_mutex);
6231	mutex_exit(SD_MUTEX(un));
6232}
6233
6234
6235/*
6236 *    Function: sd_ddi_suspend
6237 *
6238 * Description: Performs system power-down operations. This includes
6239 *		setting the drive state to indicate its suspended so
6240 *		that no new commands will be accepted. Also, wait for
6241 *		all commands that are in transport or queued to a timer
6242 *		for retry to complete. All timeout threads are cancelled.
6243 *
6244 * Return Code: DDI_FAILURE or DDI_SUCCESS
6245 *
6246 *     Context: Kernel thread context
6247 */
6248
6249static int
6250sd_ddi_suspend(dev_info_t *devi)
6251{
6252	struct	sd_lun	*un;
6253	clock_t		wait_cmds_complete;
6254
6255	un = ddi_get_soft_state(sd_state, ddi_get_instance(devi));
6256	if (un == NULL) {
6257		return (DDI_FAILURE);
6258	}
6259
6260	SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: entry\n");
6261
6262	mutex_enter(SD_MUTEX(un));
6263
6264	/* Return success if the device is already suspended. */
6265	if (un->un_state == SD_STATE_SUSPENDED) {
6266		mutex_exit(SD_MUTEX(un));
6267		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6268		    "device already suspended, exiting\n");
6269		return (DDI_SUCCESS);
6270	}
6271
6272	/* Return failure if the device is being used by HA */
6273	if (un->un_resvd_status &
6274	    (SD_RESERVE | SD_WANT_RESERVE | SD_LOST_RESERVE)) {
6275		mutex_exit(SD_MUTEX(un));
6276		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6277		    "device in use by HA, exiting\n");
6278		return (DDI_FAILURE);
6279	}
6280
6281	/*
6282	 * Return failure if the device is in a resource wait
6283	 * or power changing state.
6284	 */
6285	if ((un->un_state == SD_STATE_RWAIT) ||
6286	    (un->un_state == SD_STATE_PM_CHANGING)) {
6287		mutex_exit(SD_MUTEX(un));
6288		SD_TRACE(SD_LOG_IO_PM, un, "sd_ddi_suspend: "
6289		    "device in resource wait state, exiting\n");
6290		return (DDI_FAILURE);
6291	}
6292
6293
6294	un->un_save_state = un->un_last_state;
6295	New_state(un, SD_STATE_SUSPENDED);
6296
6297	/*
6298	 * Wait for all commands that are in transport or queued to a timer
6299	 * for retry to complete.
6300	 *
6301	 * While waiting, no new commands will be accepted or sent because of
6302	 * the new state we set above.
6303	 *
6304	 * Wait till current operation has completed. If we are in the resource
6305	 * wait state (with an intr outstanding) then we need to wait till the
6306	 * intr completes and starts the next cmd. We want to wait for
6307	 * SD_WAIT_CMDS_COMPLETE seconds before failing the DDI_SUSPEND.
6308	 */
6309	wait_cmds_complete = ddi_get_lbolt() +
6310	    (sd_wait_cmds_complete * drv_usectohz(1000000));
6311
6312	while (un->un_ncmds_in_transport != 0) {
6313		/*