Home | History | Annotate | Download | only in targets
      1      0    stevel /*
      2      0    stevel  * CDDL HEADER START
      3      0    stevel  *
      4      0    stevel  * The contents of this file are subject to the terms of the
      5   1106       mrj  * Common Development and Distribution License (the "License").
      6   1106       mrj  * You may not use this file except in compliance with the License.
      7      0    stevel  *
      8      0    stevel  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9      0    stevel  * or http://www.opensolaris.org/os/licensing.
     10      0    stevel  * See the License for the specific language governing permissions
     11      0    stevel  * and limitations under the License.
     12      0    stevel  *
     13      0    stevel  * When distributing Covered Code, include this CDDL HEADER in each
     14      0    stevel  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15      0    stevel  * If applicable, add the following below this CDDL HEADER, with the
     16      0    stevel  * fields enclosed by brackets "[]" replaced with your own identifying
     17      0    stevel  * information: Portions Copyright [yyyy] [name of copyright owner]
     18      0    stevel  *
     19      0    stevel  * CDDL HEADER END
     20      0    stevel  */
     21   1106       mrj 
     22      0    stevel /*
     23   8726        Bo  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24      0    stevel  * Use is subject to license terms.
     25      0    stevel  */
     26      0    stevel 
     27      0    stevel /*
     28      0    stevel  * SCSI	 SCSA-compliant and not-so-DDI-compliant Tape Driver
     29      0    stevel  */
     30      0    stevel 
     31      0    stevel #if defined(lint) && !defined(DEBUG)
     32      0    stevel #define	DEBUG	1
     33      0    stevel #endif
     34      0    stevel 
     35      0    stevel #include <sys/modctl.h>
     36      0    stevel #include <sys/scsi/scsi.h>
     37      0    stevel #include <sys/mtio.h>
     38      0    stevel #include <sys/scsi/targets/stdef.h>
     39      0    stevel #include <sys/file.h>
     40      0    stevel #include <sys/kstat.h>
     41    177  cz147101 #include <sys/ddidmareq.h>
     42    177  cz147101 #include <sys/ddi.h>
     43    177  cz147101 #include <sys/sunddi.h>
     44   5425  yl194034 #include <sys/byteorder.h>
     45      0    stevel 
     46      0    stevel #define	IOSP	KSTAT_IO_PTR(un->un_stats)
     47      0    stevel /*
     48      0    stevel  * stats maintained only for reads/writes as commands
     49      0    stevel  * like rewind etc skew the wait/busy times
     50      0    stevel  */
     51      0    stevel #define	IS_RW(bp) 	((bp)->b_bcount > 0)
     52      0    stevel #define	ST_DO_KSTATS(bp, kstat_function) \
     53      0    stevel 	if ((bp != un->un_sbufp) && un->un_stats && IS_RW(bp)) { \
     54      0    stevel 		kstat_function(IOSP); \
     55      0    stevel 	}
     56      0    stevel 
     57      0    stevel #define	ST_DO_ERRSTATS(un, x)  \
     58      0    stevel 	if (un->un_errstats) { \
     59      0    stevel 		struct st_errstats *stp; \
     60      0    stevel 		stp = (struct st_errstats *)un->un_errstats->ks_data; \
     61      0    stevel 		stp->x.value.ul++; \
     62      0    stevel 	}
     63      0    stevel 
     64    159   jongkis #define	FILL_SCSI1_LUN(devp, pkt) 					\
     65    159   jongkis 	if ((devp)->sd_inq->inq_ansi == 0x1) {				\
     66    159   jongkis 		int _lun;						\
     67    159   jongkis 		_lun = ddi_prop_get_int(DDI_DEV_T_ANY, (devp)->sd_dev,	\
     68    159   jongkis 		    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);		\
     69    159   jongkis 		if (_lun > 0) {						\
     70    159   jongkis 			((union scsi_cdb *)(pkt)->pkt_cdbp)->scc_lun =	\
     71    159   jongkis 			    _lun;					\
     72    159   jongkis 		}							\
     73      0    stevel 	}
     74    932  cz147101 
     75    932  cz147101 /*
     76    932  cz147101  * get an available contig mem header, cp.
     77    932  cz147101  * when big_enough is true, we will return NULL, if no big enough
     78    932  cz147101  * contig mem is found.
     79    932  cz147101  * when big_enough is false, we will try to find cp containing big
     80    932  cz147101  * enough contig mem. if not found, we will ruturn the last cp available.
     81    932  cz147101  *
     82    932  cz147101  * used by st_get_contig_mem()
     83    932  cz147101  */
     84    932  cz147101 #define	ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough) {		\
     85    932  cz147101 	struct contig_mem *tmp_cp = NULL;				\
     86    932  cz147101 	for ((cp) = (un)->un_contig_mem;				\
     87    932  cz147101 	    (cp) != NULL;						\
     88    932  cz147101 	    tmp_cp = (cp), (cp) = (cp)->cm_next) { 			\
     89    932  cz147101 		if (((cp)->cm_len >= (len)) || 				\
     90    932  cz147101 		    (!(big_enough) && ((cp)->cm_next == NULL))) { 	\
     91    932  cz147101 			if (tmp_cp == NULL) { 				\
     92    932  cz147101 				(un)->un_contig_mem = (cp)->cm_next; 	\
     93    932  cz147101 			} else { 					\
     94    932  cz147101 				tmp_cp->cm_next = (cp)->cm_next; 	\
     95    932  cz147101 			} 						\
     96    932  cz147101 			(cp)->cm_next = NULL; 				\
     97    932  cz147101 			(un)->un_contig_mem_available_num--; 		\
     98    932  cz147101 			break; 						\
     99    932  cz147101 		} 							\
    100    932  cz147101 	} 								\
    101    932  cz147101 }
    102      0    stevel 
    103      0    stevel #define	ST_NUM_MEMBERS(array)	(sizeof (array) / sizeof (array[0]))
    104   4549   rralphs #define	COPY_POS(dest, source) bcopy(source, dest, sizeof (tapepos_t))
    105   5628   rralphs #define	ISALNUM(byte) \
    106   5628   rralphs 	(((byte) >= 'a' && (byte) <= 'z') || \
    107   5628   rralphs 	((byte) >= 'A' && (byte) <= 'Z') || \
    108   5628   rralphs 	((byte) >= '0' && (byte) <= '9'))
    109   4549   rralphs 
    110   4549   rralphs #define	ONE_K	1024
    111      0    stevel 
    112   6941   rralphs #define	MAX_SPACE_CNT(cnt) if (cnt >= 0) { \
    113   6941   rralphs 		if (cnt > MIN(SP_CNT_MASK, INT32_MAX)) \
    114   6941   rralphs 			return (EINVAL); \
    115   6941   rralphs 	} else { \
    116   6941   rralphs 		if (-(cnt) > MIN(SP_CNT_MASK, INT32_MAX)) \
    117   6941   rralphs 			return (EINVAL); \
    118   6941   rralphs 	} \
    119   6941   rralphs 
    120      0    stevel /*
    121      0    stevel  * Global External Data Definitions
    122      0    stevel  */
    123      0    stevel extern struct scsi_key_strings scsi_cmds[];
    124   4549   rralphs extern uchar_t	scsi_cdb_size[];
    125      0    stevel 
    126      0    stevel /*
    127      0    stevel  * Local Static Data
    128      0    stevel  */
    129      0    stevel static void *st_state;
    130   4549   rralphs static char *const st_label = "st";
    131   5628   rralphs static volatile int st_recov_sz = sizeof (recov_info);
    132   6941   rralphs static const char mp_misconf[] = {
    133   6941   rralphs 	"St Tape is misconfigured, MPxIO enabled and "
    134   6941   rralphs 	"tape-command-recovery-disable set in st.conf\n"
    135   6941   rralphs };
    136    177  cz147101 
    137   5251       mrj #ifdef	__x86
    138    177  cz147101 /*
    139    177  cz147101  * We need to use below DMA attr to alloc physically contiguous
    140    177  cz147101  * memory to do I/O in big block size
    141    177  cz147101  */
    142    177  cz147101 static ddi_dma_attr_t st_contig_mem_dma_attr = {
    143    177  cz147101 	DMA_ATTR_V0,    /* version number */
    144    177  cz147101 	0x0,		/* lowest usable address */
    145    177  cz147101 	0xFFFFFFFFull,  /* high DMA address range */
    146    177  cz147101 	0xFFFFFFFFull,  /* DMA counter register */
    147    177  cz147101 	1,		/* DMA address alignment */
    148    177  cz147101 	1,		/* DMA burstsizes */
    149    177  cz147101 	1,		/* min effective DMA size */
    150    177  cz147101 	0xFFFFFFFFull,  /* max DMA xfer size */
    151    177  cz147101 	0xFFFFFFFFull,  /* segment boundary */
    152    177  cz147101 	1,		/* s/g list length */
    153    177  cz147101 	1,		/* granularity of device */
    154    177  cz147101 	0		/* DMA transfer flags */
    155    177  cz147101 };
    156    177  cz147101 
    157    177  cz147101 static ddi_device_acc_attr_t st_acc_attr = {
    158    177  cz147101 	DDI_DEVICE_ATTR_V0,
    159    177  cz147101 	DDI_NEVERSWAP_ACC,
    160    177  cz147101 	DDI_STRICTORDER_ACC
    161    177  cz147101 };
    162    177  cz147101 
    163    177  cz147101 /* set limitation for the number of contig_mem */
    164    177  cz147101 static int st_max_contig_mem_num = ST_MAX_CONTIG_MEM_NUM;
    165    177  cz147101 #endif
    166      0    stevel 
    167      0    stevel /*
    168      0    stevel  * Tunable parameters
    169      0    stevel  *
    170      0    stevel  * DISCLAIMER
    171      0    stevel  * ----------
    172      0    stevel  * These parameters are intended for use only in system testing; if you use
    173      0    stevel  * them in production systems, you do so at your own risk. Altering any
    174      0    stevel  * variable not listed below may cause unpredictable system behavior.
    175      0    stevel  *
    176      0    stevel  * st_check_media_time
    177      0    stevel  *
    178      0    stevel  *   Three second state check
    179      0    stevel  *
    180      0    stevel  * st_allow_large_xfer
    181      0    stevel  *
    182      0    stevel  *   Gated with ST_NO_RECSIZE_LIMIT
    183      0    stevel  *
    184      0    stevel  *   0 - Transfers larger than 64KB will not be allowed
    185      0    stevel  *       regardless of the setting of ST_NO_RECSIZE_LIMIT
    186      0    stevel  *   1 - Transfers larger than 64KB will be allowed
    187      0    stevel  *       if ST_NO_RECSIZE_LIMIT is TRUE for the drive
    188      0    stevel  *
    189      0    stevel  * st_report_soft_errors_on_close
    190      0    stevel  *
    191      0    stevel  *  Gated with ST_SOFT_ERROR_REPORTING
    192      0    stevel  *
    193      0    stevel  *  0 - Errors will not be reported on close regardless
    194      0    stevel  *      of the setting of ST_SOFT_ERROR_REPORTING
    195      0    stevel  *
    196      0    stevel  *  1 - Errors will be reported on close if
    197      0    stevel  *      ST_SOFT_ERROR_REPORTING is TRUE for the drive
    198      0    stevel  */
    199      0    stevel static int st_selection_retry_count = ST_SEL_RETRY_COUNT;
    200      0    stevel static int st_retry_count	= ST_RETRY_COUNT;
    201      0    stevel 
    202      0    stevel static int st_io_time		= ST_IO_TIME;
    203      0    stevel static int st_long_timeout_x	= ST_LONG_TIMEOUT_X;
    204      0    stevel 
    205      0    stevel static int st_space_time	= ST_SPACE_TIME;
    206      0    stevel static int st_long_space_time_x	= ST_LONG_SPACE_TIME_X;
    207      0    stevel 
    208      0    stevel static int st_error_level	= SCSI_ERR_RETRYABLE;
    209      0    stevel static int st_check_media_time	= 3000000;	/* 3 Second State Check */
    210      0    stevel 
    211      0    stevel static int st_max_throttle	= ST_MAX_THROTTLE;
    212      0    stevel 
    213      0    stevel static clock_t st_wait_cmds_complete = ST_WAIT_CMDS_COMPLETE;
    214      0    stevel 
    215      0    stevel static int st_allow_large_xfer = 1;
    216      0    stevel static int st_report_soft_errors_on_close = 1;
    217      0    stevel 
    218      0    stevel /*
    219      0    stevel  * End of tunable parameters list
    220      0    stevel  */
    221      0    stevel 
    222      0    stevel 
    223      0    stevel 
    224      0    stevel /*
    225      0    stevel  * Asynchronous I/O and persistent errors, refer to PSARC/1995/228
    226      0    stevel  *
    227      0    stevel  * Asynchronous I/O's main offering is that it is a non-blocking way to do
    228      0    stevel  * reads and writes.  The driver will queue up all the requests it gets and
    229      0    stevel  * have them ready to transport to the HBA.  Unfortunately, we cannot always
    230      0    stevel  * just ship the I/O requests to the HBA, as there errors and exceptions
    231      0    stevel  * that may happen when we don't want the HBA to continue.  Therein comes
    232      0    stevel  * the flush-on-errors capability.  If the HBA supports it, then st will
    233      0    stevel  * send in st_max_throttle I/O requests at the same time.
    234      0    stevel  *
    235      0    stevel  * Persistent errors : This was also reasonably simple.  In the interrupt
    236      0    stevel  * routines, if there was an error or exception (FM, LEOT, media error,
    237      0    stevel  * transport error), the persistent error bits are set and shuts everything
    238      0    stevel  * down, but setting the throttle to zero.  If we hit and exception in the
    239      0    stevel  * HBA, and flush-on-errors were set, we wait for all outstanding I/O's to
    240      0    stevel  * come back (with CMD_ABORTED), then flush all bp's in the wait queue with
    241      0    stevel  * the appropriate error, and this will preserve order. Of course, depending
    242      0    stevel  * on the exception we have to show a zero read or write before we show
    243      0    stevel  * errors back to the application.
    244      0    stevel  */
    245      0    stevel 
    246      0    stevel extern const int st_ndrivetypes;	/* defined in st_conf.c */
    247      0    stevel extern const struct st_drivetype st_drivetypes[];
    248   4549   rralphs extern const char st_conf_version[];
    249      0    stevel 
    250      0    stevel #ifdef STDEBUG
    251      0    stevel static int st_soft_error_report_debug = 0;
    252   2537   rralphs volatile int st_debug = 0;
    253   6941   rralphs static volatile dev_info_t *st_lastdev;
    254   6941   rralphs static kmutex_t st_debug_mutex;
    255      0    stevel #endif
    256      0    stevel 
    257      0    stevel #define	ST_MT02_NAME	"Emulex  MT02 QIC-11/24  "
    258   5425  yl194034 
    259   5425  yl194034 static const struct vid_drivetype {
    260   5425  yl194034 	char	*vid;
    261   5425  yl194034 	char	type;
    262   5425  yl194034 } st_vid_dt[] = {
    263   5425  yl194034 	{"LTO-CVE ",	MT_LTO},
    264   5425  yl194034 	{"QUANTUM ",    MT_ISDLT},
    265   5425  yl194034 	{"SONY    ",    MT_ISAIT},
    266   5425  yl194034 	{"STK     ",	MT_ISSTK9840}
    267   5425  yl194034 };
    268      0    stevel 
    269      0    stevel static const struct driver_minor_data {
    270      0    stevel 	char	*name;
    271      0    stevel 	int	minor;
    272      0    stevel } st_minor_data[] = {
    273      0    stevel 	/*
    274      0    stevel 	 * The top 4 entries are for the default densities,
    275      0    stevel 	 * don't alter their position.
    276      0    stevel 	 */
    277      0    stevel 	{"",	0},
    278      0    stevel 	{"n",	MT_NOREWIND},
    279      0    stevel 	{"b",	MT_BSD},
    280      0    stevel 	{"bn",	MT_NOREWIND | MT_BSD},
    281      0    stevel 	{"l",	MT_DENSITY1},
    282      0    stevel 	{"m",	MT_DENSITY2},
    283      0    stevel 	{"h",	MT_DENSITY3},
    284      0    stevel 	{"c",	MT_DENSITY4},
    285      0    stevel 	{"u",	MT_DENSITY4},
    286      0    stevel 	{"ln",	MT_DENSITY1 | MT_NOREWIND},
    287      0    stevel 	{"mn",	MT_DENSITY2 | MT_NOREWIND},
    288      0    stevel 	{"hn",	MT_DENSITY3 | MT_NOREWIND},
    289      0    stevel 	{"cn",	MT_DENSITY4 | MT_NOREWIND},
    290      0    stevel 	{"un",	MT_DENSITY4 | MT_NOREWIND},
    291      0    stevel 	{"lb",	MT_DENSITY1 | MT_BSD},
    292      0    stevel 	{"mb",	MT_DENSITY2 | MT_BSD},
    293      0    stevel 	{"hb",	MT_DENSITY3 | MT_BSD},
    294      0    stevel 	{"cb",	MT_DENSITY4 | MT_BSD},
    295      0    stevel 	{"ub",	MT_DENSITY4 | MT_BSD},
    296      0    stevel 	{"lbn",	MT_DENSITY1 | MT_NOREWIND | MT_BSD},
    297      0    stevel 	{"mbn",	MT_DENSITY2 | MT_NOREWIND | MT_BSD},
    298      0    stevel 	{"hbn",	MT_DENSITY3 | MT_NOREWIND | MT_BSD},
    299      0    stevel 	{"cbn",	MT_DENSITY4 | MT_NOREWIND | MT_BSD},
    300      0    stevel 	{"ubn",	MT_DENSITY4 | MT_NOREWIND | MT_BSD}
    301      0    stevel };
    302      0    stevel 
    303      0    stevel /* strings used in many debug and warning messages */
    304      0    stevel static const char wr_str[]  = "write";
    305      0    stevel static const char rd_str[]  = "read";
    306      0    stevel static const char wrg_str[] = "writing";
    307      0    stevel static const char rdg_str[] = "reading";
    308   4549   rralphs static const char *space_strs[] = {
    309   4549   rralphs 	"records",
    310   4549   rralphs 	"filemarks",
    311   4549   rralphs 	"sequential filemarks",
    312   4549   rralphs 	"eod",
    313   4549   rralphs 	"setmarks",
    314   4549   rralphs 	"sequential setmarks",
    315   4549   rralphs 	"Reserved",
    316   4549   rralphs 	"Reserved"
    317   4549   rralphs };
    318   5628   rralphs static const char *load_strs[] = {
    319   5628   rralphs 	"unload",		/* LD_UNLOAD		0 */
    320   5628   rralphs 	"load",			/* LD_LOAD		1 */
    321   5628   rralphs 	"retension",		/* LD_RETEN		2 */
    322   5628   rralphs 	"load reten",		/* LD_LOAD | LD_RETEN	3 */
    323   5628   rralphs 	"eod",			/* LD_EOT		4 */
    324   5628   rralphs 	"load EOD",		/* LD_LOAD | LD_EOT	5 */
    325   5628   rralphs 	"reten EOD",		/* LD_RETEN | LD_EOT	6 */
    326   5628   rralphs 	"load reten EOD"	/* LD_LOAD|LD_RETEN|LD_EOT 7 */
    327   5628   rralphs 	"hold",			/* LD_HOLD		8 */
    328   5628   rralphs 	"load and hold"		/* LD_LOAD | LD_HOLD	9 */
    329   6941   rralphs };
    330   6941   rralphs 
    331   6941   rralphs static const char *errstatenames[] = {
    332   6941   rralphs 	"COMMAND_DONE",
    333   6941   rralphs 	"COMMAND_DONE_ERROR",
    334   6941   rralphs 	"COMMAND_DONE_ERROR_RECOVERED",
    335   6941   rralphs 	"QUE_COMMAND",
    336   6941   rralphs 	"QUE_BUSY_COMMAND",
    337   6941   rralphs 	"QUE_SENSE",
    338   6941   rralphs 	"JUST_RETURN",
    339   6941   rralphs 	"COMMAND_DONE_EACCES",
    340   6941   rralphs 	"QUE_LAST_COMMAND",
    341   6941   rralphs 	"COMMAND_TIMEOUT",
    342   6941   rralphs 	"PATH_FAILED",
    343   6941   rralphs 	"DEVICE_RESET",
    344   6941   rralphs 	"DEVICE_TAMPER",
    345   6941   rralphs 	"ATTEMPT_RETRY"
    346   5628   rralphs };
    347   5628   rralphs 
    348   5628   rralphs const char *bogusID = "Unknown Media ID";
    349      0    stevel 
    350      0    stevel /* default density offsets in the table above */
    351      0    stevel #define	DEF_BLANK	0
    352      0    stevel #define	DEF_NOREWIND	1
    353      0    stevel #define	DEF_BSD		2
    354      0    stevel #define	DEF_BSD_NR	3
    355      0    stevel 
    356      0    stevel /* Sense Key, ASC/ASCQ for which tape ejection is needed */
    357      0    stevel 
    358      0    stevel static struct tape_failure_code {
    359      0    stevel 	uchar_t key;
    360      0    stevel 	uchar_t add_code;
    361      0    stevel 	uchar_t qual_code;
    362      0    stevel } st_tape_failure_code[] = {
    363      0    stevel 	{ KEY_HARDWARE_ERROR, 0x15, 0x01},
    364      0    stevel 	{ KEY_HARDWARE_ERROR, 0x44, 0x00},
    365      0    stevel 	{ KEY_HARDWARE_ERROR, 0x53, 0x00},
    366      0    stevel 	{ KEY_HARDWARE_ERROR, 0x53, 0x01},
    367      0    stevel 	{ KEY_NOT_READY, 0x53, 0x00},
    368      0    stevel 	{ 0xff}
    369      0    stevel };
    370      0    stevel 
    371      0    stevel /*  clean bit position and mask */
    372      0    stevel 
    373      0    stevel static struct cln_bit_position {
    374      0    stevel 	ushort_t cln_bit_byte;
    375      0    stevel 	uchar_t cln_bit_mask;
    376      0    stevel } st_cln_bit_position[] = {
    377      0    stevel 	{ 21, 0x08},
    378      0    stevel 	{ 70, 0xc0},
    379      0    stevel 	{ 18, 0x81}  /* 80 bit indicates in bit mode, 1 bit clean light is on */
    380      0    stevel };
    381   1106       mrj 
    382   1106       mrj /*
    383   1106       mrj  * architecture dependent allocation restrictions. For x86, we'll set
    384   1106       mrj  * dma_attr_addr_hi to st_max_phys_addr and dma_attr_sgllen to
    385   1106       mrj  * st_sgl_size during _init().
    386   1106       mrj  */
    387   1106       mrj #if defined(__sparc)
    388   1106       mrj static ddi_dma_attr_t st_alloc_attr = {
    389   1106       mrj 	DMA_ATTR_V0,	/* version number */
    390   1106       mrj 	0x0,		/* lowest usable address */
    391   1106       mrj 	0xFFFFFFFFull,	/* high DMA address range */
    392   1106       mrj 	0xFFFFFFFFull,	/* DMA counter register */
    393   1106       mrj 	1,		/* DMA address alignment */
    394   1106       mrj 	1,		/* DMA burstsizes */
    395   1106       mrj 	1,		/* min effective DMA size */
    396   1106       mrj 	0xFFFFFFFFull,	/* max DMA xfer size */
    397   1106       mrj 	0xFFFFFFFFull,	/* segment boundary */
    398   1106       mrj 	1,		/* s/g list length */
    399   1106       mrj 	512,		/* granularity of device */
    400   1106       mrj 	0		/* DMA transfer flags */
    401   1106       mrj };
    402   1106       mrj #elif defined(__x86)
    403   1106       mrj static ddi_dma_attr_t st_alloc_attr = {
    404   1106       mrj 	DMA_ATTR_V0,	/* version number */
    405   1106       mrj 	0x0,		/* lowest usable address */
    406   1106       mrj 	0x0,		/* high DMA address range [set in _init()] */
    407   1106       mrj 	0xFFFFull,	/* DMA counter register */
    408   1106       mrj 	512,		/* DMA address alignment */
    409   1106       mrj 	1,		/* DMA burstsizes */
    410   1106       mrj 	1,		/* min effective DMA size */
    411   1106       mrj 	0xFFFFFFFFull,	/* max DMA xfer size */
    412   1106       mrj 	0xFFFFFFFFull,  /* segment boundary */
    413   1106       mrj 	0,		/* s/g list length */
    414   1106       mrj 	512,		/* granularity of device [set in _init()] */
    415   1106       mrj 	0		/* DMA transfer flags */
    416   1106       mrj };
    417   1106       mrj uint64_t st_max_phys_addr = 0xFFFFFFFFull;
    418   1106       mrj int st_sgl_size = 0xF;
    419   1106       mrj 
    420   1106       mrj #endif
    421      0    stevel 
    422      0    stevel /*
    423      0    stevel  * Configuration Data:
    424      0    stevel  *
    425      0    stevel  * Device driver ops vector
    426      0    stevel  */
    427      0    stevel static int st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
    428      0    stevel static int st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
    429      0    stevel static int st_read(dev_t  dev,  struct   uio   *uio_p,   cred_t *cred_p);
    430      0    stevel static int st_write(dev_t  dev,  struct  uio   *uio_p,   cred_t *cred_p);
    431      0    stevel static int st_open(dev_t  *devp,  int  flag,  int  otyp,  cred_t *cred_p);
    432      0    stevel static int st_close(dev_t  dev,  int  flag,  int  otyp,  cred_t *cred_p);
    433      0    stevel static int st_strategy(struct buf *bp);
    434   5628   rralphs static int st_queued_strategy(buf_t *bp);
    435      0    stevel static int st_ioctl(dev_t dev, int cmd, intptr_t arg, int  flag,
    436      0    stevel 	cred_t *cred_p, int *rval_p);
    437      0    stevel extern int nulldev(), nodev();
    438      0    stevel 
    439      0    stevel static struct cb_ops st_cb_ops = {
    440   4549   rralphs 	st_open,		/* open */
    441      0    stevel 	st_close,		/* close */
    442   5628   rralphs 	st_queued_strategy,	/* strategy Not Block device but async checks */
    443      0    stevel 	nodev,			/* print */
    444      0    stevel 	nodev,			/* dump */
    445      0    stevel 	st_read,		/* read */
    446      0    stevel 	st_write,		/* write */
    447      0    stevel 	st_ioctl,		/* ioctl */
    448      0    stevel 	nodev,			/* devmap */
    449      0    stevel 	nodev,			/* mmap */
    450      0    stevel 	nodev,			/* segmap */
    451      0    stevel 	nochpoll,		/* poll */
    452      0    stevel 	ddi_prop_op,		/* cb_prop_op */
    453      0    stevel 	0,			/* streamtab  */
    454   4452       cth 	D_64BIT | D_MP | D_NEW | D_HOTPLUG |
    455   4452       cth 	D_OPEN_RETURNS_EINTR,	/* cb_flag */
    456      0    stevel 	CB_REV,			/* cb_rev */
    457      0    stevel 	st_aread, 		/* async I/O read entry point */
    458      0    stevel 	st_awrite		/* async I/O write entry point */
    459      0    stevel 
    460      0    stevel };
    461      0    stevel 
    462   5628   rralphs static int st_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
    463      0    stevel 		void **result);
    464      0    stevel static int st_probe(dev_info_t *dev);
    465      0    stevel static int st_attach(dev_info_t *dev, ddi_attach_cmd_t cmd);
    466      0    stevel static int st_detach(dev_info_t *dev, ddi_detach_cmd_t cmd);
    467      0    stevel 
    468      0    stevel static struct dev_ops st_ops = {
    469      0    stevel 	DEVO_REV,		/* devo_rev, */
    470      0    stevel 	0,			/* refcnt  */
    471   5628   rralphs 	st_info,		/* info */
    472      0    stevel 	nulldev,		/* identify */
    473      0    stevel 	st_probe,		/* probe */
    474      0    stevel 	st_attach,		/* attach */
    475      0    stevel 	st_detach,		/* detach */
    476      0    stevel 	nodev,			/* reset */
    477      0    stevel 	&st_cb_ops,		/* driver operations */
    478      0    stevel 	(struct bus_ops *)0,	/* bus operations */
    479   7656    Sherry 	nulldev,		/* power */
    480   7955        Bo 	ddi_quiesce_not_needed,	/* devo_quiesce */
    481      0    stevel };
    482      0    stevel 
    483      0    stevel /*
    484      0    stevel  * Local Function Declarations
    485      0    stevel  */
    486   4549   rralphs static char *st_print_scsi_cmd(char cmd);
    487   4549   rralphs static void st_print_cdb(dev_info_t *dip, char *label, uint_t level,
    488   4549   rralphs     char *title, char *cdb);
    489      0    stevel static void st_clean_print(dev_info_t *dev, char *label, uint_t level,
    490   5628   rralphs     char *title, char *data, int len);
    491      0    stevel static int st_doattach(struct scsi_device *devp, int (*canwait)());
    492      0    stevel static void st_known_tape_type(struct scsi_tape *un);
    493      0    stevel static int st_get_conf_from_st_dot_conf(struct scsi_tape *, char *,
    494      0    stevel     struct st_drivetype *);
    495      0    stevel static int st_get_conf_from_st_conf_dot_c(struct scsi_tape *, char *,
    496      0    stevel     struct st_drivetype *);
    497   5425  yl194034 static int st_get_conf_from_tape_drive(struct scsi_tape *, char *,
    498   5425  yl194034     struct st_drivetype *);
    499   5425  yl194034 static int st_get_densities_from_tape_drive(struct scsi_tape *,
    500   5425  yl194034     struct st_drivetype *);
    501   5425  yl194034 static int st_get_timeout_values_from_tape_drive(struct scsi_tape *,
    502   5425  yl194034     struct st_drivetype *);
    503   5425  yl194034 static int st_get_timeouts_value(struct scsi_tape *, uchar_t, ushort_t *,
    504   5425  yl194034     ushort_t);
    505      0    stevel static int st_get_default_conf(struct scsi_tape *, char *,
    506      0    stevel     struct st_drivetype *);
    507      0    stevel static int st_rw(dev_t dev, struct uio *uio, int flag);
    508      0    stevel static int st_arw(dev_t dev, struct aio_req *aio, int flag);
    509   5628   rralphs static int st_find_eod(struct scsi_tape *un);
    510      0    stevel static int st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag);
    511   5628   rralphs static int st_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *, int flag);
    512      0    stevel static int st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag);
    513   4549   rralphs static int st_mtiocltop(struct scsi_tape *un, intptr_t arg, int flag);
    514   4549   rralphs static int st_do_mtioctop(struct scsi_tape *un, struct mtlop *mtop);
    515    131   rralphs static void st_start(struct scsi_tape *un);
    516      0    stevel static int st_handle_start_busy(struct scsi_tape *un, struct buf *bp,
    517   5628   rralphs     clock_t timeout_interval, int queued);
    518      0    stevel static int st_handle_intr_busy(struct scsi_tape *un, struct buf *bp,
    519      0    stevel     clock_t timeout_interval);
    520      0    stevel static int st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp);
    521      0    stevel static void st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp);
    522      0    stevel static void st_init(struct scsi_tape *un);
    523      0    stevel static void st_make_cmd(struct scsi_tape *un, struct buf *bp,
    524    131   rralphs     int (*func)(caddr_t));
    525      0    stevel static void st_make_uscsi_cmd(struct scsi_tape *, struct uscsi_cmd *,
    526    131   rralphs     struct buf *bp, int (*func)(caddr_t));
    527      0    stevel static void st_intr(struct scsi_pkt *pkt);
    528   5628   rralphs static void st_set_state(struct scsi_tape *un, buf_t *bp);
    529      0    stevel static void st_test_append(struct buf *bp);
    530    131   rralphs static int st_runout(caddr_t);
    531   5628   rralphs static int st_cmd(struct scsi_tape *un, int com, int64_t count, int wait);
    532   5628   rralphs static int st_setup_cmd(struct scsi_tape *un, buf_t *bp, int com,
    533   5628   rralphs     int64_t count);
    534      0    stevel static int st_set_compression(struct scsi_tape *un);
    535      0    stevel static int st_write_fm(dev_t dev, int wfm);
    536   5628   rralphs static int st_determine_generic(struct scsi_tape *un);
    537   5628   rralphs static int st_determine_density(struct scsi_tape *un, int rw);
    538   5628   rralphs static int st_get_density(struct scsi_tape *un);
    539   5628   rralphs static int st_set_density(struct scsi_tape *un);
    540   5628   rralphs static int st_loadtape(struct scsi_tape *un);
    541      0    stevel static int st_modesense(struct scsi_tape *un);
    542      0    stevel static int st_modeselect(struct scsi_tape *un);
    543   5628   rralphs static errstate st_handle_incomplete(struct scsi_tape *un, struct buf *bp);
    544      0    stevel static int st_wrongtapetype(struct scsi_tape *un);
    545   5628   rralphs static errstate st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt);
    546   5628   rralphs static errstate st_handle_sense(struct scsi_tape *un, struct buf *bp,
    547   5628   rralphs     tapepos_t *);
    548   5628   rralphs static errstate st_handle_autosense(struct scsi_tape *un, struct buf *bp,
    549   5628   rralphs     tapepos_t *);
    550   5077  bz211116 static int st_get_error_entry(struct scsi_tape *un, intptr_t arg, int flag);
    551   5077  bz211116 static void st_update_error_stack(struct scsi_tape *un, struct scsi_pkt *pkt,
    552   5077  bz211116     struct scsi_arq_status *cmd);
    553   5077  bz211116 static void st_empty_error_stack(struct scsi_tape *un);
    554   5628   rralphs static errstate st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt,
    555   6941   rralphs     struct scsi_arq_status *, tapepos_t *);
    556      0    stevel static int st_report_soft_errors(dev_t dev, int flag);
    557      0    stevel static void st_delayed_cv_broadcast(void *arg);
    558      0    stevel static int st_check_media(dev_t dev, enum mtio_state state);
    559      0    stevel static int st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
    560      0    stevel static void st_intr_restart(void *arg);
    561      0    stevel static void st_start_restart(void *arg);
    562   5628   rralphs static int st_gen_mode_sense(struct scsi_tape *un, ubufunc_t ubf, int page,
    563      0    stevel     struct seq_mode *page_data, int page_size);
    564   5628   rralphs static int st_change_block_size(struct scsi_tape *un, uint32_t nblksz);
    565   5628   rralphs static int st_gen_mode_select(struct scsi_tape *un, ubufunc_t ubf,
    566   5628   rralphs     struct seq_mode *page_data, int page_size);
    567   5425  yl194034 static int st_read_block_limits(struct scsi_tape *un,
    568   5425  yl194034     struct read_blklim *read_blk);
    569   5425  yl194034 static int st_report_density_support(struct scsi_tape *un,
    570   5425  yl194034     uchar_t *density_data, size_t buflen);
    571   5425  yl194034 static int st_report_supported_operation(struct scsi_tape *un,
    572   5425  yl194034     uchar_t *oper_data, uchar_t option_code, ushort_t service_action);
    573   5628   rralphs static int st_tape_init(struct scsi_tape *un);
    574      0    stevel static void st_flush(struct scsi_tape *un);
    575      0    stevel static void st_set_pe_errno(struct scsi_tape *un);
    576      0    stevel static void st_hba_unflush(struct scsi_tape *un);
    577      0    stevel static void st_turn_pe_on(struct scsi_tape *un);
    578      0    stevel static void st_turn_pe_off(struct scsi_tape *un);
    579      0    stevel static void st_set_pe_flag(struct scsi_tape *un);
    580      0    stevel static void st_clear_pe(struct scsi_tape *un);
    581      0    stevel static void st_wait_for_io(struct scsi_tape *un);
    582      0    stevel static int st_set_devconfig_page(struct scsi_tape *un, int compression_on);
    583      0    stevel static int st_set_datacomp_page(struct scsi_tape *un, int compression_on);
    584   5628   rralphs static int st_reserve_release(struct scsi_tape *un, int command, ubufunc_t ubf);
    585   5628   rralphs static int st_check_cdb_for_need_to_reserve(struct scsi_tape *un, uchar_t *cdb);
    586   1697   rralphs static int st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd,
    587   1697   rralphs     int count);
    588   6941   rralphs static int st_take_ownership(struct scsi_tape *un, ubufunc_t ubf);
    589      0    stevel static int st_check_asc_ascq(struct scsi_tape *un);
    590   5628   rralphs static int st_check_clean_bit(struct scsi_tape *un);
    591   5628   rralphs static int st_check_alert_flags(struct scsi_tape *un);
    592   5628   rralphs static int st_check_sequential_clean_bit(struct scsi_tape *un);
    593   5628   rralphs static int st_check_sense_clean_bit(struct scsi_tape *un);
    594      0    stevel static int st_clear_unit_attentions(dev_t dev_instance, int max_trys);
    595      0    stevel static void st_calculate_timeouts(struct scsi_tape *un);
    596   2537   rralphs static writablity st_is_drive_worm(struct scsi_tape *un);
    597   2537   rralphs static int st_read_attributes(struct scsi_tape *un, uint16_t attribute,
    598   5628   rralphs     void *buf, size_t size, ubufunc_t bufunc);
    599   2537   rralphs static int st_get_special_inquiry(struct scsi_tape *un, uchar_t size,
    600   2537   rralphs     caddr_t dest, uchar_t page);
    601   5628   rralphs static int st_update_block_pos(struct scsi_tape *un, bufunc_t bf,
    602   5628   rralphs     int post_space);
    603   5628   rralphs static int st_interpret_read_pos(struct scsi_tape const *un, tapepos_t *dest,
    604   5628   rralphs     read_p_types type, size_t data_sz, const caddr_t responce, int post_space);
    605   4549   rralphs static int st_get_read_pos(struct scsi_tape *un, buf_t *bp);
    606   5628   rralphs static int st_logical_block_locate(struct scsi_tape *un, ubufunc_t ubf,
    607   5628   rralphs     tapepos_t *pos, uint64_t lblk, uchar_t partition);
    608   6941   rralphs static int st_mtfsf_ioctl(struct scsi_tape *un, int64_t files);
    609   6941   rralphs static int st_mtfsr_ioctl(struct scsi_tape *un, int64_t count);
    610   6941   rralphs static int st_mtbsf_ioctl(struct scsi_tape *un, int64_t files);
    611   6941   rralphs static int st_mtnbsf_ioctl(struct scsi_tape *un, int64_t count);
    612   6941   rralphs static int st_mtbsr_ioctl(struct scsi_tape *un, int64_t num);
    613   6941   rralphs static int st_mtfsfm_ioctl(struct scsi_tape *un, int64_t cnt);
    614   6941   rralphs static int st_mtbsfm_ioctl(struct scsi_tape *un, int64_t cnt);
    615   6941   rralphs static int st_backward_space_files(struct scsi_tape *un, int64_t count,
    616   4549   rralphs     int infront);
    617   6941   rralphs static int st_forward_space_files(struct scsi_tape *un, int64_t files);
    618   4549   rralphs static int st_scenic_route_to_begining_of_file(struct scsi_tape *un,
    619   4549   rralphs     int32_t fileno);
    620   4549   rralphs static int st_space_to_begining_of_file(struct scsi_tape *un);
    621   6941   rralphs static int st_space_records(struct scsi_tape *un, int64_t records);
    622   5628   rralphs static int st_get_media_identification(struct scsi_tape *un, ubufunc_t bufunc);
    623   5628   rralphs static errstate st_command_recovery(struct scsi_tape *un, struct scsi_pkt *pkt,
    624   5628   rralphs     errstate onentry);
    625   5628   rralphs static void st_recover(void *arg);
    626   5628   rralphs static void st_recov_cb(struct scsi_pkt *pkt);
    627   5628   rralphs static int st_rcmd(struct scsi_tape *un, int com, int64_t count, int wait);
    628   5628   rralphs static int st_uscsi_rcmd(struct scsi_tape *un, struct uscsi_cmd *ucmd,
    629   5628   rralphs     int flag);
    630   5628   rralphs static void st_add_recovery_info_to_pkt(struct scsi_tape *un, buf_t *bp,
    631   5628   rralphs     struct scsi_pkt *cmd);
    632   5628   rralphs static int st_check_mode_for_change(struct scsi_tape *un, ubufunc_t ubf);
    633   5628   rralphs static int st_test_path_to_device(struct scsi_tape *un);
    634   5628   rralphs static int st_recovery_read_pos(struct scsi_tape *un, read_p_types type,
    635   5628   rralphs     read_pos_data_t *raw);
    636   5628   rralphs static int st_recovery_get_position(struct scsi_tape *un, tapepos_t *read,
    637   5628   rralphs     read_pos_data_t *raw);
    638   5628   rralphs static int st_compare_expected_position(struct scsi_tape *un, st_err_info *ei,
    639   5628   rralphs     cmd_attribute const * cmd_att, tapepos_t *read);
    640   5628   rralphs static errstate st_recover_reissue_pkt(struct scsi_tape *us,
    641   5628   rralphs     struct scsi_pkt *pkt);
    642   5628   rralphs static int st_transport(struct scsi_tape *un, struct scsi_pkt *pkt);
    643   5628   rralphs static buf_t *st_remove_from_queue(buf_t **head, buf_t **tail, buf_t *bp);
    644   5628   rralphs static void st_add_to_queue(buf_t **head, buf_t **tail, buf_t *end, buf_t *bp);
    645   5628   rralphs static int st_reset(struct scsi_tape *un, int reset_type);
    646   5628   rralphs static void st_reset_notification(caddr_t arg);
    647   5628   rralphs static const cmd_attribute *st_lookup_cmd_attribute(unsigned char cmd);
    648      0    stevel 
    649  10740   Jianfei static int st_set_target_TLR_mode(struct scsi_tape *un, ubufunc_t ubf);
    650  10740   Jianfei static int st_make_sure_mode_data_is_correct(struct scsi_tape *un,
    651  10740   Jianfei     ubufunc_t ubf);
    652  10740   Jianfei 
    653   5251       mrj #ifdef	__x86
    654    177  cz147101 /*
    655    177  cz147101  * routines for I/O in big block size
    656    177  cz147101  */
    657    177  cz147101 static void st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp);
    658    177  cz147101 static struct contig_mem *st_get_contig_mem(struct scsi_tape *un, size_t len,
    659    177  cz147101     int alloc_flags);
    660    177  cz147101 static int st_bigblk_xfer_done(struct buf *bp);
    661    177  cz147101 static struct buf *st_get_bigblk_bp(struct buf *bp);
    662    177  cz147101 #endif
    663   5628   rralphs static void st_print_position(dev_info_t *dev, char *label, uint_t level,
    664   5628   rralphs     const char *comment, tapepos_t *pos);
    665   5628   rralphs 
    666   5628   rralphs /*
    667   5628   rralphs  * error statistics create/update functions
    668   5628   rralphs  */
    669   5628   rralphs static int st_create_errstats(struct scsi_tape *, int);
    670   5628   rralphs static int st_validate_tapemarks(struct scsi_tape *un, ubufunc_t ubf,
    671   4549   rralphs     tapepos_t *pos);
    672      0    stevel 
    673      0    stevel #ifdef STDEBUG
    674      0    stevel static void st_debug_cmds(struct scsi_tape *un, int com, int count, int wait);
    675   6941   rralphs #endif /* STDEBUG */
    676      0    stevel static char *st_dev_name(dev_t dev);
    677      0    stevel 
    678      0    stevel #if !defined(lint)
    679   3368  lh195018 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt",
    680   3368  lh195018     scsi_pkt buf uio scsi_cdb uscsi_cmd))
    681      0    stevel _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_extended_sense scsi_status))
    682   6941   rralphs _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", recov_info))
    683      0    stevel _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device))
    684      0    stevel _NOTE(DATA_READABLE_WITHOUT_LOCK(st_drivetype scsi_address))
    685      0    stevel #endif
    686      0    stevel 
    687      0    stevel /*
    688      0    stevel  * autoconfiguration routines.
    689      0    stevel  */
    690      0    stevel char _depends_on[] = "misc/scsi";
    691      0    stevel 
    692      0    stevel static struct modldrv modldrv = {
    693      0    stevel 	&mod_driverops,		/* Type of module. This one is a driver */
    694   7381        Bo 	"SCSI tape Driver", 	/* Name of the module. */
    695      0    stevel 	&st_ops			/* driver ops */
    696      0    stevel };
    697      0    stevel 
    698      0    stevel static struct modlinkage modlinkage = {
    699      0    stevel 	MODREV_1, &modldrv, NULL
    700      0    stevel };
    701      0    stevel 
    702      0    stevel /*
    703      0    stevel  * Notes on Post Reset Behavior in the tape driver:
    704      0    stevel  *
    705      0    stevel  * When the tape drive is opened, the driver  attempts  to make sure that
    706      0    stevel  * the tape head is positioned exactly where it was left when it was last
    707      0    stevel  * closed  provided  the  medium  is not  changed.  If the tape  drive is
    708      0    stevel  * opened in O_NDELAY mode, the repositioning  (if necessary for any loss
    709      0    stevel  * of position due to reset) will happen when the first tape operation or
    710      0    stevel  * I/O occurs.  The repositioning (if required) may not be possible under
    711      0    stevel  * certain situations such as when the device firmware not able to report
    712      0    stevel  * the medium  change in the REQUEST  SENSE data  because of a reset or a
    713      0    stevel  * misbehaving  bus  not  allowing  the  reposition  to  happen.  In such
    714      0    stevel  * extraordinary  situations, where the driver fails to position the head
    715      0    stevel  * at its  original  position,  it will fail the open the first  time, to
    716      0    stevel  * save the applications from overwriting the data.  All further attempts
    717      0    stevel  * to open the tape device will result in the driver  attempting  to load
    718      0    stevel  * the  tape at BOT  (beginning  of  tape).  Also a  warning  message  to
    719      0    stevel  * indicate  that further  attempts to open the tape device may result in
    720      0    stevel  * the tape being  loaded at BOT will be printed on the  console.  If the
    721      0    stevel  * tape  device is opened  in  O_NDELAY  mode,  failure  to  restore  the
    722      0    stevel  * original tape head  position,  will result in the failure of the first
    723      0    stevel  * tape  operation  or I/O,  Further,  the  driver  will  invalidate  its
    724      0    stevel  * internal tape position  which will  necessitate  the  applications  to
    725      0    stevel  * validate the position by using either a tape  positioning  ioctl (such
    726      0    stevel  * as MTREW) or closing and reopening the tape device.
    727      0    stevel  *
    728      0    stevel  */
    729      0    stevel 
    730      0    stevel int
    731      0    stevel _init(void)
    732      0    stevel {
    733   5628   rralphs 	int e;
    734      0    stevel 
    735      0    stevel 	if (((e = ddi_soft_state_init(&st_state,
    736      0    stevel 	    sizeof (struct scsi_tape), ST_MAXUNIT)) != 0)) {
    737      0    stevel 		return (e);
    738      0    stevel 	}
    739      0    stevel 
    740      0    stevel 	if ((e = mod_install(&modlinkage)) != 0) {
    741      0    stevel 		ddi_soft_state_fini(&st_state);
    742   6941   rralphs 	} else {
    743   6941   rralphs #ifdef STDEBUG
    744   6941   rralphs 		mutex_init(&st_debug_mutex, NULL, MUTEX_DRIVER, NULL);
    745   6941   rralphs #endif
    746      0    stevel 
    747   1106       mrj #if defined(__x86)
    748   6941   rralphs 		/* set the max physical address for iob allocs on x86 */
    749   6941   rralphs 		st_alloc_attr.dma_attr_addr_hi = st_max_phys_addr;
    750   6941   rralphs 
    751   6941   rralphs 		/*
    752   6941   rralphs 		 * set the sgllen for iob allocs on x86. If this is set less
    753   6941   rralphs 		 * than the number of pages the buffer will take
    754   6941   rralphs 		 * (taking into account alignment), it would force the
    755   6941   rralphs 		 * allocator to try and allocate contiguous pages.
    756   6941   rralphs 		 */
    757   6941   rralphs 		st_alloc_attr.dma_attr_sgllen = st_sgl_size;
    758   6941   rralphs #endif
    759   6941   rralphs 	}
    760   1106       mrj 
    761      0    stevel 	return (e);
    762      0    stevel }
    763      0    stevel 
    764      0    stevel int
    765      0    stevel _fini(void)
    766      0    stevel {
    767      0    stevel 	int e;
    768      0    stevel 
    769      0    stevel 	if ((e = mod_remove(&modlinkage)) != 0) {
    770      0    stevel 		return (e);
    771      0    stevel 	}
    772   6941   rralphs 
    773   6941   rralphs #ifdef STDEBUG
    774   6941   rralphs 	mutex_destroy(&st_debug_mutex);
    775   6941   rralphs #endif
    776      0    stevel 
    777      0    stevel 	ddi_soft_state_fini(&st_state);
    778      0    stevel 
    779      0    stevel 	return (e);
    780      0    stevel }
    781      0    stevel 
    782      0    stevel int
    783      0    stevel _info(struct modinfo *modinfop)
    784      0    stevel {
    785      0    stevel 	return (mod_info(&modlinkage, modinfop));
    786      0    stevel }
    787      0    stevel 
    788      0    stevel 
    789      0    stevel static int
    790      0    stevel st_probe(dev_info_t *devi)
    791      0    stevel {
    792      0    stevel 	int instance;
    793      0    stevel 	struct scsi_device *devp;
    794      0    stevel 	int rval;
    795      0    stevel 
    796      0    stevel #if !defined(__sparc)
    797      0    stevel 	char    *tape_prop;
    798      0    stevel 	int	tape_prop_len;
    799      0    stevel #endif
    800      0    stevel 
    801   4549   rralphs 	ST_ENTR(devi, st_probe);
    802   4549   rralphs 
    803      0    stevel 	/* If self identifying device */
    804      0    stevel 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
    805      0    stevel 		return (DDI_PROBE_DONTCARE);
    806      0    stevel 	}
    807      0    stevel 
    808      0    stevel #if !defined(__sparc)
    809      0    stevel 	/*
    810      0    stevel 	 * Since some x86 HBAs have devnodes that look like SCSI as
    811      0    stevel 	 * far as we can tell but aren't really SCSI (DADK, like mlx)
    812      0    stevel 	 * we check for the presence of the "tape" property.
    813      0    stevel 	 */
    814      0    stevel 	if (ddi_prop_op(DDI_DEV_T_NONE, devi, PROP_LEN_AND_VAL_ALLOC,
    815      0    stevel 	    DDI_PROP_CANSLEEP, "tape",
    816      0    stevel 	    (caddr_t)&tape_prop, &tape_prop_len) != DDI_PROP_SUCCESS) {
    817      0    stevel 		return (DDI_PROBE_FAILURE);
    818      0    stevel 	}
    819      0    stevel 	if (strncmp(tape_prop, "sctp", tape_prop_len) != 0) {
    820      0    stevel 		kmem_free(tape_prop, tape_prop_len);
    821      0    stevel 		return (DDI_PROBE_FAILURE);
    822      0    stevel 	}
    823      0    stevel 	kmem_free(tape_prop, tape_prop_len);
    824      0    stevel #endif
    825      0    stevel 
    826      0    stevel 	devp = ddi_get_driver_private(devi);
    827      0    stevel 	instance = ddi_get_instance(devi);
    828      0    stevel 
    829      0    stevel 	if (ddi_get_soft_state(st_state, instance) != NULL) {
    830      0    stevel 		return (DDI_PROBE_PARTIAL);
    831      0    stevel 	}
    832      0    stevel 
    833      0    stevel 
    834      0    stevel 	/*
    835      0    stevel 	 * Turn around and call probe routine to see whether
    836      0    stevel 	 * we actually have a tape at this SCSI nexus.
    837      0    stevel 	 */
    838      0    stevel 	if (scsi_probe(devp, NULL_FUNC) == SCSIPROBE_EXISTS) {
    839      0    stevel 
    840      0    stevel 		/*
    841      0    stevel 		 * In checking the whole inq_dtype byte we are looking at both
    842      0    stevel 		 * the Peripheral Qualifier and the Peripheral Device Type.
    843      0    stevel 		 * For this driver we are only interested in sequential devices
    844      0    stevel 		 * that are connected or capable if connecting to this logical
    845      0    stevel 		 * unit.
    846      0    stevel 		 */
    847      0    stevel 		if (devp->sd_inq->inq_dtype ==
    848      0    stevel 		    (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) {
    849      0    stevel 			ST_DEBUG6(devi, st_label, SCSI_DEBUG,
    850      0    stevel 			    "probe exists\n");
    851      0    stevel 			rval = DDI_PROBE_SUCCESS;
    852      0    stevel 		} else {
    853      0    stevel 			rval = DDI_PROBE_FAILURE;
    854      0    stevel 		}
    855      0    stevel 	} else {
    856      0    stevel 		ST_DEBUG6(devi, st_label, SCSI_DEBUG,
    857      0    stevel 		    "probe failure: nothing there\n");
    858      0    stevel 		rval = DDI_PROBE_FAILURE;
    859      0    stevel 	}
    860      0    stevel 	scsi_unprobe(devp);
    861      0    stevel 	return (rval);
    862      0    stevel }
    863      0    stevel 
    864      0    stevel static int
    865      0    stevel st_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
    866      0    stevel {
    867      0    stevel 	int 	instance;
    868      0    stevel 	int	wide;
    869      0    stevel 	int 	dev_instance;
    870      0    stevel 	int	ret_status;
    871      0    stevel 	struct	scsi_device *devp;
    872      0    stevel 	int	node_ix;
    873      0    stevel 	struct	scsi_tape *un;
    874      0    stevel 
    875   4549   rralphs 	ST_ENTR(devi, st_attach);
    876   4549   rralphs 
    877      0    stevel 	devp = ddi_get_driver_private(devi);
    878      0    stevel 	instance = ddi_get_instance(devi);
    879      0    stevel 
    880      0    stevel 	switch (cmd) {
    881      0    stevel 		case DDI_ATTACH:
    882   5628   rralphs 			if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
    883   5628   rralphs 			    "tape-command-recovery-disable", 0) != 0) {
    884   5628   rralphs 				st_recov_sz = sizeof (pkt_info);
    885   5628   rralphs 			}
    886      0    stevel 			if (st_doattach(devp, SLEEP_FUNC) == DDI_FAILURE) {
    887      0    stevel 				return (DDI_FAILURE);
    888      0    stevel 			}
    889      0    stevel 			break;
    890      0    stevel 		case DDI_RESUME:
    891      0    stevel 			/*
    892      0    stevel 			 * Suspend/Resume
    893      0    stevel 			 *
    894      0    stevel 			 * When the driver suspended, there might be
    895      0    stevel 			 * outstanding cmds and therefore we need to
    896      0    stevel 			 * reset the suspended flag and resume the scsi
    897      0    stevel 			 * watch thread and restart commands and timeouts
    898      0    stevel 			 */
    899      0    stevel 
    900      0    stevel 			if (!(un = ddi_get_soft_state(st_state, instance))) {
    901      0    stevel 				return (DDI_FAILURE);
    902      0    stevel 			}
    903      0    stevel 			dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) :
    904      0    stevel 			    un->un_dev);
    905      0    stevel 
    906      0    stevel 			mutex_enter(ST_MUTEX);
    907      0    stevel 
    908      0    stevel 			un->un_throttle = un->un_max_throttle;
    909      0    stevel 			un->un_tids_at_suspend = 0;
    910      0    stevel 			un->un_pwr_mgmt = ST_PWR_NORMAL;
    911      0    stevel 
    912      0    stevel 			if (un->un_swr_token) {
    913      0    stevel 				scsi_watch_resume(un->un_swr_token);
    914      0    stevel 			}
    915      0    stevel 
    916      0    stevel 			/*
    917      0    stevel 			 * Restart timeouts
    918      0    stevel 			 */
    919      0    stevel 			if ((un->un_tids_at_suspend & ST_DELAY_TID) != 0) {
    920      0    stevel 				mutex_exit(ST_MUTEX);
    921   4549   rralphs 				un->un_delay_tid = timeout(
    922   4549   rralphs 				    st_delayed_cv_broadcast, un,
    923   4549   rralphs 				    drv_usectohz((clock_t)
    924   4549   rralphs 				    MEDIA_ACCESS_DELAY));
    925      0    stevel 				mutex_enter(ST_MUTEX);
    926      0    stevel 			}
    927      0    stevel 
    928      0    stevel 			if (un->un_tids_at_suspend & ST_HIB_TID) {
    929      0    stevel 				mutex_exit(ST_MUTEX);
    930      0    stevel 				un->un_hib_tid = timeout(st_intr_restart, un,
    931      0    stevel 				    ST_STATUS_BUSY_TIMEOUT);
    932      0    stevel 				mutex_enter(ST_MUTEX);
    933      0    stevel 			}
    934      0    stevel 
    935      0    stevel 			ret_status = st_clear_unit_attentions(dev_instance, 5);
    936      0    stevel 
    937      0    stevel 			/*
    938      0    stevel 			 * now check if we need to restore the tape position
    939      0    stevel 			 */
    940   4549   rralphs 			if ((un->un_suspend_pos.pmode != invalid) &&
    941   4549   rralphs 			    ((un->un_suspend_pos.fileno > 0) ||
    942   4549   rralphs 			    (un->un_suspend_pos.blkno > 0)) ||
    943   4549   rralphs 			    (un->un_suspend_pos.lgclblkno > 0)) {
    944      0    stevel 				if (ret_status != 0) {
    945      0    stevel 					/*
    946      0    stevel 					 * tape didn't get good TUR
    947      0    stevel 					 * just print out error messages
    948      0    stevel 					 */
    949      0    stevel 					scsi_log(ST_DEVINFO, st_label, CE_WARN,
    950      0    stevel 					    "st_attach-RESUME: tape failure "
    951      0    stevel 					    " tape position will be lost");
    952      0    stevel 				} else {
    953      0    stevel 					/* this prints errors */
    954      0    stevel 					(void) st_validate_tapemarks(un,
    955   5628   rralphs 					    st_uscsi_cmd, &un->un_suspend_pos);
    956      0    stevel 				}
    957      0    stevel 				/*
    958      0    stevel 				 * there are no retries, if there is an error
    959      0    stevel 				 * we don't know if the tape has changed
    960      0    stevel 				 */
    961   4549   rralphs 				un->un_suspend_pos.pmode = invalid;
    962      0    stevel 			}
    963      0    stevel 
    964      0    stevel 			/* now we are ready to start up any queued I/Os */
    965      0    stevel 			if (un->un_ncmds || un->un_quef) {
    966    131   rralphs 				st_start(un);
    967      0    stevel 			}
    968      0    stevel 
    969      0    stevel 			cv_broadcast(&un->un_suspend_cv);
    970      0    stevel 			mutex_exit(ST_MUTEX);
    971      0    stevel 			return (DDI_SUCCESS);
    972      0    stevel 
    973      0    stevel 		default:
    974      0    stevel 			return (DDI_FAILURE);
    975      0    stevel 	}
    976      0    stevel 
    977      0    stevel 	un = ddi_get_soft_state(st_state, instance);
    978      0    stevel 
    979      0    stevel 	ST_DEBUG(devi, st_label, SCSI_DEBUG,
    980      0    stevel 	    "st_attach: instance=%x\n", instance);
    981      0    stevel 
    982      0    stevel 	/*
    983      0    stevel 	 * Add a zero-length attribute to tell the world we support
    984      0    stevel 	 * kernel ioctls (for layered drivers)
    985      0    stevel 	 */
    986      0    stevel 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
    987      0    stevel 	    DDI_KERNEL_IOCTL, NULL, 0);
    988      0    stevel 
    989      0    stevel 	ddi_report_dev((dev_info_t *)devi);
    990      0    stevel 
    991      0    stevel 	/*
    992      0    stevel 	 * If it's a SCSI-2 tape drive which supports wide,
    993      0    stevel 	 * tell the host adapter to use wide.
    994      0    stevel 	 */
    995      0    stevel 	wide = ((devp->sd_inq->inq_rdf == RDF_SCSI2) &&
    996   4549   rralphs 	    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) ?  1 : 0;
    997      0    stevel 
    998      0    stevel 	if (scsi_ifsetcap(ROUTE, "wide-xfer", wide, 1) == 1) {
    999      0    stevel 		ST_DEBUG(devi, st_label, SCSI_DEBUG,
   1000      0    stevel 		    "Wide Transfer %s\n", wide ? "enabled" : "disabled");
   1001      0    stevel 	}
   1002      0    stevel 
   1003      0    stevel 	/*
   1004      0    stevel 	 * enable autorequest sense; keep the rq packet around in case
   1005      0    stevel 	 * the autorequest sense fails because of a busy condition
   1006      0    stevel 	 * do a getcap first in case the capability is not variable
   1007      0    stevel 	 */
   1008      0    stevel 	if (scsi_ifgetcap(ROUTE, "auto-rqsense", 1) == 1) {
   1009      0    stevel 		un->un_arq_enabled = 1;
   1010      0    stevel 	} else {
   1011      0    stevel 		un->un_arq_enabled =
   1012      0    stevel 		    ((scsi_ifsetcap(ROUTE, "auto-rqsense", 1, 1) == 1) ? 1 : 0);
   1013      0    stevel 	}
   1014      0    stevel 
   1015      0    stevel 	ST_DEBUG(devi, st_label, SCSI_DEBUG, "auto request sense %s\n",
   1016   4549   rralphs 	    (un->un_arq_enabled ? "enabled" : "disabled"));
   1017      0    stevel 
   1018      0    stevel 	un->un_untagged_qing =
   1019      0    stevel 	    (scsi_ifgetcap(ROUTE, "untagged-qing", 0) == 1);
   1020      0    stevel 
   1021      0    stevel 	/*
   1022      0    stevel 	 * XXX - This is just for 2.6.  to tell users that write buffering
   1023      0    stevel 	 *	has gone away.
   1024      0    stevel 	 */
   1025      0    stevel 	if (un->un_arq_enabled && un->un_untagged_qing) {
   1026      0    stevel 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
   1027      0    stevel 		    "tape-driver-buffering", 0) != 0) {
   1028      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
   1029      0    stevel 			    "Write Data Buffering has been depricated. Your "
   1030      0    stevel 			    "applications should continue to work normally.\n"
   1031      0    stevel 			    " But, they should  ported to use Asynchronous "
   1032      0    stevel 			    " I/O\n"
   1033      0    stevel 			    " For more information, read about "
   1034      0    stevel 			    " tape-driver-buffering "
   1035      0    stevel 			    "property in the st(7d) man page\n");
   1036      0    stevel 		}
   1037      0    stevel 	}
   1038      0    stevel 
   1039      0    stevel 	un->un_max_throttle = un->un_throttle = un->un_last_throttle = 1;
   1040      0    stevel 	un->un_flush_on_errors = 0;
   1041      0    stevel 	un->un_mkr_pkt = (struct scsi_pkt *)NULL;
   1042      0    stevel 
   1043      0    stevel 	ST_DEBUG(devi, st_label, SCSI_DEBUG,
   1044      0    stevel 	    "throttle=%x, max_throttle = %x\n",
   1045      0    stevel 	    un->un_throttle, un->un_max_throttle);
   1046      0    stevel 
   1047      0    stevel 	/* initialize persistent errors to nil */
   1048      0    stevel 	un->un_persistence = 0;
   1049      0    stevel 	un->un_persist_errors = 0;
   1050      0    stevel 
   1051      0    stevel 	/*
   1052      0    stevel 	 * Get dma-max from HBA driver. If it is not defined, use 64k
   1053      0    stevel 	 */
   1054      0    stevel 	un->un_maxdma	= scsi_ifgetcap(&devp->sd_address, "dma-max", 1);
   1055      0    stevel 	if (un->un_maxdma == -1) {
   1056   2537   rralphs 		ST_DEBUG(devi, st_label, SCSI_DEBUG,
   1057   4549   rralphs 		    "Received a value that looked like -1. Using 64k maxdma");
   1058   4549   rralphs 		un->un_maxdma = (64 * ONE_K);
   1059   2537   rralphs 	}
   1060   2537   rralphs 
   1061   5251       mrj #ifdef	__x86
   1062   5251       mrj 	/*
   1063   5251       mrj 	 * for x86, the device may be able to DMA more than the system will
   1064   5251       mrj 	 * allow under some circumstances. We need account for both the HBA's
   1065   5251       mrj 	 * and system's contraints.
   1066   5251       mrj 	 *
   1067   5251       mrj 	 * Get the maximum DMA under worse case conditions. e.g. looking at the
   1068   5251       mrj 	 * device constraints, the max copy buffer size, and the worse case
   1069   5251       mrj 	 * fragmentation. NOTE: this may differ from dma-max since dma-max
   1070   5251       mrj 	 * doesn't take the worse case framentation into account.
   1071   5251       mrj 	 *
   1072   5251       mrj 	 * e.g. a device may be able to DMA 16MBytes, but can only DMA 1MByte
   1073   5251       mrj 	 * if none of the pages are contiguous. Keeping track of both of these
   1074   5251       mrj 	 * values allows us to support larger tape block sizes on some devices.
   1075   5251       mrj 	 */
   1076   5251       mrj 	un->un_maxdma_arch = scsi_ifgetcap(&devp->sd_address, "dma-max-arch",
   1077   5251       mrj 	    1);
   1078   5251       mrj 
   1079   5251       mrj 	/*
   1080   5251       mrj 	 * If the dma-max-arch capability is not implemented, or the value
   1081   5251       mrj 	 * comes back higher than what was reported in dma-max, use dma-max.
   1082   5251       mrj 	 */
   1083   5251       mrj 	if ((un->un_maxdma_arch == -1) ||
   1084   5251       mrj 	    ((uint_t)un->un_maxdma < (uint_t)un->un_maxdma_arch)) {
   1085   5251       mrj 		un->un_maxdma_arch = un->un_maxdma;
   1086   5251       mrj 	}
   1087   5251       mrj #endif
   1088   5251       mrj 
   1089   2537   rralphs 	/*
   1090   2537   rralphs 	 * Get the max allowable cdb size
   1091   2537   rralphs 	 */
   1092   2537   rralphs 	un->un_max_cdb_sz =
   1093   2537   rralphs 	    scsi_ifgetcap(&devp->sd_address, "max-cdb-length", 1);
   1094   2537   rralphs 	if (un->un_max_cdb_sz < CDB_GROUP0) {
   1095   2537   rralphs 		ST_DEBUG(devi, st_label, SCSI_DEBUG,
   1096   2537   rralphs 		    "HBA reported max-cdb-length as %d\n", un->un_max_cdb_sz);
   1097   2537   rralphs 		un->un_max_cdb_sz = CDB_GROUP4; /* optimistic default */
   1098      0    stevel 	}
   1099      0    stevel 
   1100   6941   rralphs 	if (strcmp(ddi_driver_name(ddi_get_parent(ST_DEVINFO)), "scsi_vhci")) {
   1101   6941   rralphs 		un->un_multipath = 0;
   1102   6941   rralphs 	} else {
   1103   6941   rralphs 		un->un_multipath = 1;
   1104   6941   rralphs 	}
   1105   6941   rralphs 
   1106      0    stevel 	un->un_maxbsize = MAXBSIZE_UNKNOWN;
   1107      0    stevel 
   1108      0    stevel 	un->un_mediastate = MTIO_NONE;
   1109      0    stevel 	un->un_HeadClean  = TAPE_ALERT_SUPPORT_UNKNOWN;
   1110      0    stevel 
   1111      0    stevel 	/*
   1112      0    stevel 	 * initialize kstats
   1113      0    stevel 	 */
   1114      0    stevel 	un->un_stats = kstat_create("st", instance, NULL, "tape",
   1115   4549   rralphs 	    KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
   1116      0    stevel 	if (un->un_stats) {
   1117      0    stevel 		un->un_stats->ks_lock = ST_MUTEX;
   1118      0    stevel 		kstat_install(un->un_stats);
   1119      0    stevel 	}
   1120      0    stevel 	(void) st_create_errstats(un, instance);
   1121   5425  yl194034 
   1122   5425  yl194034 	/*
   1123   5425  yl194034 	 * find the drive type for this target
   1124   5425  yl194034 	 */
   1125   5425  yl194034 	mutex_enter(ST_MUTEX);
   1126   6756  yl194034 	un->un_dev = MTMINOR(instance);
   1127   5425  yl194034 	st_known_tape_type(un);
   1128   5425  yl194034 	un->un_dev = 0;
   1129   5425  yl194034 	mutex_exit(ST_MUTEX);
   1130   5425  yl194034 
   1131   5425  yl194034 	for (node_ix = 0; node_ix < ST_NUM_MEMBERS(st_minor_data); node_ix++) {
   1132   5425  yl194034 		int minor;
   1133   5425  yl194034 		char *name;
   1134   5425  yl194034 
   1135   5425  yl194034 		name  = st_minor_data[node_ix].name;
   1136   5425  yl194034 		minor = st_minor_data[node_ix].minor;
   1137   5425  yl194034 
   1138   5425  yl194034 		/*
   1139   5425  yl194034 		 * For default devices set the density to the
   1140   5425  yl194034 		 * preferred default density for this device.
   1141   5425  yl194034 		 */
   1142   5425  yl194034 		if (node_ix <= DEF_BSD_NR) {
   1143   5425  yl194034 			minor |= un->un_dp->default_density;
   1144   5425  yl194034 		}
   1145   5425  yl194034 		minor |= MTMINOR(instance);
   1146   5425  yl194034 
   1147   5425  yl194034 		if (ddi_create_minor_node(devi, name, S_IFCHR, minor,
   1148   5425  yl194034 		    DDI_NT_TAPE, NULL) == DDI_SUCCESS) {
   1149   5425  yl194034 			continue;
   1150   5425  yl194034 		}
   1151   5425  yl194034 
   1152   5425  yl194034 		ddi_remove_minor_node(devi, NULL);
   1153   5425  yl194034 
   1154   5628   rralphs 		(void) scsi_reset_notify(ROUTE, SCSI_RESET_CANCEL,
   1155   5628   rralphs 		    st_reset_notification, (caddr_t)un);
   1156   5425  yl194034 		cv_destroy(&un->un_clscv);
   1157   5425  yl194034 		cv_destroy(&un->un_sbuf_cv);
   1158   5425  yl194034 		cv_destroy(&un->un_queue_cv);
   1159   5425  yl194034 		cv_destroy(&un->un_state_cv);
   1160   5628   rralphs #ifdef	__x86
   1161   5628   rralphs 		cv_destroy(&un->un_contig_mem_cv);
   1162   5628   rralphs #endif
   1163   5425  yl194034 		cv_destroy(&un->un_suspend_cv);
   1164   5425  yl194034 		cv_destroy(&un->un_tape_busy_cv);
   1165   5628   rralphs 		cv_destroy(&un->un_recov_buf_cv);
   1166   5628   rralphs 		if (un->un_recov_taskq) {
   1167   5628   rralphs 			ddi_taskq_destroy(un->un_recov_taskq);
   1168   5628   rralphs 		}
   1169   5425  yl194034 		if (un->un_sbufp) {
   1170   5425  yl194034 			freerbuf(un->un_sbufp);
   1171   5628   rralphs 		}
   1172   5628   rralphs 		if (un->un_recov_buf) {
   1173   5628   rralphs 			freerbuf(un->un_recov_buf);
   1174   5425  yl194034 		}
   1175   5425  yl194034 		if (un->un_uscsi_rqs_buf) {
   1176   5425  yl194034 			kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH);
   1177   5425  yl194034 		}
   1178   5425  yl194034 		if (un->un_mspl) {
   1179   5425  yl194034 			i_ddi_mem_free((caddr_t)un->un_mspl, NULL);
   1180   5425  yl194034 		}
   1181   5425  yl194034 		if (un->un_dp_size) {
   1182   5425  yl194034 			kmem_free(un->un_dp, un->un_dp_size);
   1183   5425  yl194034 		}
   1184   5425  yl194034 		if (un->un_state) {
   1185   5425  yl194034 			kstat_delete(un->un_stats);
   1186   5425  yl194034 		}
   1187   5425  yl194034 		if (un->un_errstats) {
   1188   5425  yl194034 			kstat_delete(un->un_errstats);
   1189   5425  yl194034 		}
   1190   5425  yl194034 
   1191   5425  yl194034 		scsi_destroy_pkt(un->un_rqs);
   1192   5425  yl194034 		scsi_free_consistent_buf(un->un_rqs_bp);
   1193   5425  yl194034 		ddi_soft_state_free(st_state, instance);
   1194   5425  yl194034 		devp->sd_private = NULL;
   1195   5425  yl194034 		devp->sd_sense = NULL;
   1196   5425  yl194034 
   1197   5425  yl194034 		ddi_prop_remove_all(devi);
   1198   5425  yl194034 		return (DDI_FAILURE);
   1199   5425  yl194034 	}
   1200      0    stevel 
   1201      0    stevel 	return (DDI_SUCCESS);
   1202      0    stevel }
   1203      0    stevel 
   1204      0    stevel /*
   1205      0    stevel  * st_detach:
   1206      0    stevel  *
   1207      0    stevel  * we allow a detach if and only if:
   1208      0    stevel  *	- no tape is currently inserted
   1209      0    stevel  *	- tape position is at BOT or unknown
   1210      0    stevel  *		(if it is not at BOT then a no rewind
   1211      0    stevel  *		device was opened and we have to preserve state)
   1212      0    stevel  *	- it must be in a closed state : no timeouts or scsi_watch requests
   1213      0    stevel  *		will exist if it is closed, so we don't need to check for
   1214      0    stevel  *		them here.
   1215      0    stevel  */
   1216      0    stevel /*ARGSUSED*/
   1217      0    stevel static int
   1218      0    stevel st_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
   1219      0    stevel {
   1220   6941   rralphs 	int instance;
   1221   6941   rralphs 	int result;
   1222      0    stevel 	struct scsi_device *devp;
   1223      0    stevel 	struct scsi_tape *un;
   1224      0    stevel 	clock_t wait_cmds_complete;
   1225      0    stevel 
   1226   4549   rralphs 	ST_ENTR(devi, st_detach);
   1227   4549   rralphs 
   1228      0    stevel 	instance = ddi_get_instance(devi);
   1229      0    stevel 
   1230      0    stevel 	if (!(un = ddi_get_soft_state(st_state, instance))) {
   1231      0    stevel 		return (DDI_FAILURE);
   1232      0    stevel 	}
   1233      0    stevel 
   1234   5371  bz211116 	mutex_enter(ST_MUTEX);
   1235   5371  bz211116 
   1236   5371  bz211116 	/*
   1237   5371  bz211116 	 * Clear error entry stack
   1238   5371  bz211116 	 */
   1239   5371  bz211116 	st_empty_error_stack(un);
   1240   5371  bz211116 
   1241   5371  bz211116 	mutex_exit(ST_MUTEX);
   1242   5371  bz211116 
   1243      0    stevel 	switch (cmd) {
   1244      0    stevel 
   1245      0    stevel 	case DDI_DETACH:
   1246      0    stevel 		/*
   1247      0    stevel 		 * Undo what we did in st_attach & st_doattach,
   1248      0    stevel 		 * freeing resources and removing things we installed.
   1249      0    stevel 		 * The system framework guarantees we are not active
   1250      0    stevel 		 * with this devinfo node in any other entry points at
   1251      0    stevel 		 * this time.
   1252      0    stevel 		 */
   1253      0    stevel 
   1254      0    stevel 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
   1255      0    stevel 		    "st_detach: instance=%x, un=%p\n", instance,
   1256      0    stevel 		    (void *)un);
   1257      0    stevel 
   1258      0    stevel 		if (((un->un_dp->options & ST_UNLOADABLE) == 0) ||
   1259   6941   rralphs 		    ((un->un_rsvd_status & ST_APPLICATION_RESERVATIONS) != 0) ||
   1260      0    stevel 		    (un->un_ncmds != 0) || (un->un_quef != NULL) ||
   1261      0    stevel 		    (un->un_state != ST_STATE_CLOSED)) {
   1262      0    stevel 			/*
   1263      0    stevel 			 * we cannot unload some targets because the
   1264      0    stevel 			 * inquiry returns junk unless immediately
   1265      0    stevel 			 * after a reset
   1266      0    stevel 			 */
   1267      0    stevel 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   1268      0    stevel 			    "cannot unload instance %x\n", instance);
   1269   6941   rralphs 			un->un_unit_attention_flags |= 4;
   1270      0    stevel 			return (DDI_FAILURE);
   1271      0    stevel 		}
   1272      0    stevel 
   1273      0    stevel 		/*
   1274      0    stevel 		 * if the tape has been removed then we may unload;
   1275      0    stevel 		 * do a test unit ready and if it returns NOT READY
   1276      0    stevel 		 * then we assume that it is safe to unload.
   1277   4549   rralphs 		 * as a side effect, pmode may be set to invalid if the
   1278      0    stevel 		 * the test unit ready fails;
   1279      0    stevel 		 * also un_state may be set to non-closed, so reset it
   1280      0    stevel 		 */
   1281    131   rralphs 		if ((un->un_dev) &&		/* Been opened since attach */
   1282   4549   rralphs 		    ((un->un_pos.pmode == legacy) &&
   1283   4549   rralphs 		    (un->un_pos.fileno > 0) ||	/* Known position not rewound */
   1284   4549   rralphs 		    (un->un_pos.blkno != 0)) ||	/* Or within first file */
   1285   4549   rralphs 		    ((un->un_pos.pmode == logical) &&
   1286   4549   rralphs 		    (un->un_pos.lgclblkno > 0))) {
   1287    131   rralphs 			mutex_enter(ST_MUTEX);
   1288    131   rralphs 			/*
   1289    131   rralphs 			 * Send Test Unit Ready in the hopes that if
   1290    131   rralphs 			 * the drive is not in the state we think it is.
   1291    131   rralphs 			 * And the state will be changed so it can be detached.
   1292    131   rralphs 			 * If the command fails to reach the device and
   1293    131   rralphs 			 * the drive was not rewound or unloaded we want
   1294    131   rralphs 			 * to fail the detach till a user command fails
   1295    131   rralphs 			 * where after the detach will succead.
   1296    131   rralphs 			 */
   1297   6941   rralphs 			result = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD);
   1298    131   rralphs 			/*
   1299    131   rralphs 			 * After TUR un_state may be set to non-closed,
   1300    131   rralphs 			 * so reset it back.
   1301    131   rralphs 			 */
   1302      0    stevel 			un->un_state = ST_STATE_CLOSED;
   1303    131   rralphs 			mutex_exit(ST_MUTEX);
   1304    131   rralphs 		}
   1305    131   rralphs 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
   1306   4549   rralphs 		    "un_status=%x, fileno=%x, blkno=%x\n",
   1307   4549   rralphs 		    un->un_status, un->un_pos.fileno, un->un_pos.blkno);
   1308    131   rralphs 
   1309    131   rralphs 		/*
   1310    131   rralphs 		 * check again:
   1311    131   rralphs 		 * if we are not at BOT then it is not safe to unload
   1312    131   rralphs 		 */
   1313    131   rralphs 		if ((un->un_dev) &&		/* Been opened since attach */
   1314   6941   rralphs 		    (result != EACCES) &&	/* drive is use by somebody */
   1315  10033  rn150214 		    ((((un->un_pos.pmode == legacy) &&
   1316   4549   rralphs 		    (un->un_pos.fileno > 0) ||	/* Known position not rewound */
   1317   4549   rralphs 		    (un->un_pos.blkno != 0)) ||	/* Or within first file */
   1318   4549   rralphs 		    ((un->un_pos.pmode == logical) &&
   1319  10033  rn150214 		    (un->un_pos.lgclblkno > 0))) &&
   1320  10571  rn150214 		    ((un->un_state == ST_STATE_CLOSED) &&
   1321  10571  rn150214 		    (un->un_laststate == ST_STATE_CLOSING)))) {
   1322   4549   rralphs 
   1323      0    stevel 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
   1324   5628   rralphs 			    "cannot detach: pmode=%d fileno=0x%x, blkno=0x%x"
   1325   4549   rralphs 			    " lgclblkno=0x%"PRIx64"\n", un->un_pos.pmode,
   1326   4549   rralphs 			    un->un_pos.fileno, un->un_pos.blkno,
   1327   4549   rralphs 			    un->un_pos.lgclblkno);
   1328   6941   rralphs 			un->un_unit_attention_flags |= 4;
   1329    131   rralphs 			return (DDI_FAILURE);
   1330    131   rralphs 		}
   1331    131   rralphs 
   1332    131   rralphs 		/*
   1333    131   rralphs 		 * Just To make sure that we have released the
   1334    131   rralphs 		 * tape unit .
   1335    131   rralphs 		 */
   1336    131   rralphs 		if (un->un_dev && (un->un_rsvd_status & ST_RESERVE) &&
   1337    131   rralphs 		    !DEVI_IS_DEVICE_REMOVED(devi)) {
   1338    131   rralphs 			mutex_enter(ST_MUTEX);
   1339   5628   rralphs 			(void) st_reserve_release(un, ST_RELEASE, st_uscsi_cmd);
   1340    131   rralphs 			mutex_exit(ST_MUTEX);
   1341      0    stevel 		}
   1342      0    stevel 
   1343      0    stevel 		/*
   1344      0    stevel 		 * now remove other data structures allocated in st_doattach()
   1345      0    stevel 		 */
   1346      0    stevel 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
   1347      0    stevel 		    "destroying/freeing\n");
   1348   5628   rralphs 
   1349   5628   rralphs 		(void) scsi_reset_notify(ROUTE, SCSI_RESET_CANCEL,
   1350   5628   rralphs 		    st_reset_notification, (caddr_t)un);
   1351      0    stevel 		cv_destroy(&un->un_clscv);
   1352      0    stevel 		cv_destroy(&un->un_sbuf_cv);
   1353      0    stevel 		cv_destroy(&un->un_queue_cv);
   1354      0    stevel 		cv_destroy(&un->un_suspend_cv);
   1355      0    stevel 		cv_destroy(&un->un_tape_busy_cv);
   1356   5628   rralphs 		cv_destroy(&un->un_recov_buf_cv);
   1357   5628   rralphs 
   1358   5628   rralphs 		if (un->un_recov_taskq) {
   1359   5628   rralphs 			ddi_taskq_destroy(un->un_recov_taskq);
   1360   5628   rralphs 		}
   1361      0    stevel 
   1362      0    stevel 		if (un->un_hib_tid) {
   1363      0    stevel 			(void) untimeout(un->un_hib_tid);
   1364      0    stevel 			un->un_hib_tid = 0;
   1365      0    stevel 		}
   1366      0    stevel 
   1367      0    stevel 		if (un->un_delay_tid) {
   1368      0    stevel 			(void) untimeout(un->un_delay_tid);
   1369      0    stevel 			un->un_delay_tid = 0;
   1370      0    stevel 		}
   1371      0    stevel 		cv_destroy(&un->un_state_cv);
   1372      0    stevel 
   1373   5251       mrj #ifdef	__x86
   1374   5628   rralphs 		cv_destroy(&un->un_contig_mem_cv);
   1375   5628   rralphs 
   1376    177  cz147101 		if (un->un_contig_mem_hdl != NULL) {
   1377    177  cz147101 			ddi_dma_free_handle(&un->un_contig_mem_hdl);
   1378    177  cz147101 		}
   1379    177  cz147101 #endif
   1380      0    stevel 		if (un->un_sbufp) {
   1381      0    stevel 			freerbuf(un->un_sbufp);
   1382   5628   rralphs 		}
   1383   5628   rralphs 		if (un->un_recov_buf) {
   1384   5628   rralphs 			freerbuf(un->un_recov_buf);
   1385      0    stevel 		}
   1386      0    stevel 		if (un->un_uscsi_rqs_buf) {
   1387      0    stevel 			kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH);
   1388      0    stevel 		}
   1389      0    stevel 		if (un->un_mspl) {
   1390   1900      eota 			i_ddi_mem_free((caddr_t)un->un_mspl, NULL);
   1391      0    stevel 		}
   1392      0    stevel 		if (un->un_rqs) {
   1393      0    stevel 			scsi_destroy_pkt(un->un_rqs);
   1394      0    stevel 			scsi_free_consistent_buf(un->un_rqs_bp);
   1395      0    stevel 		}
   1396      0    stevel 		if (un->un_mkr_pkt) {
   1397      0    stevel 			scsi_destroy_pkt(un->un_mkr_pkt);
   1398      0    stevel 		}
   1399      0    stevel 		if (un->un_arq_enabled) {
   1400      0    stevel 			(void) scsi_ifsetcap(ROUTE, "auto-rqsense", 0, 1);
   1401      0    stevel 		}
   1402      0    stevel 		if (un->un_dp_size) {
   1403      0    stevel 			kmem_free(un->un_dp, un->un_dp_size);
   1404      0    stevel 		}
   1405      0    stevel 		if (un->un_stats) {
   1406      0    stevel 			kstat_delete(un->un_stats);
   1407      0    stevel 			un->un_stats = (kstat_t *)0;
   1408      0    stevel 		}
   1409      0    stevel 		if (un->un_errstats) {
   1410      0    stevel 			kstat_delete(un->un_errstats);
   1411      0    stevel 			un->un_errstats = (kstat_t *)0;
   1412   5628   rralphs 		}
   1413   5628   rralphs 		if (un->un_media_id_len) {
   1414   5628   rralphs 			kmem_free(un->un_media_id, un->un_media_id_len);
   1415      0    stevel 		}
   1416      0    stevel 		devp = ST_SCSI_DEVP;
   1417      0    stevel 		ddi_soft_state_free(st_state, instance);
   1418      0    stevel 		devp->sd_private = NULL;
   1419      0    stevel 		devp->sd_sense = NULL;
   1420      0    stevel 		scsi_unprobe(devp);
   1421      0    stevel 		ddi_prop_remove_all(devi);
   1422      0    stevel 		ddi_remove_minor_node(devi, NULL);
   1423      0    stevel 		ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach done\n");
   1424      0    stevel 		return (DDI_SUCCESS);
   1425      0    stevel 
   1426      0    stevel 	case DDI_SUSPEND:
   1427      0    stevel 
   1428      0    stevel 		/*
   1429      0    stevel 		 * Suspend/Resume
   1430      0    stevel 		 *
   1431      0    stevel 		 * To process DDI_SUSPEND, we must do the following:
   1432      0    stevel 		 *
   1433      0    stevel 		 *  - check ddi_removing_power to see if power will be turned
   1434      0    stevel 		 *    off. if so, return DDI_FAILURE
   1435      0    stevel 		 *  - check if we are already suspended,
   1436      0    stevel 		 *    if so, return DDI_FAILURE
   1437      0    stevel 		 *  - check if device state is CLOSED,
   1438      0    stevel 		 *    if not, return DDI_FAILURE.
   1439      0    stevel 		 *  - wait until outstanding operations complete
   1440      0    stevel 		 *  - save tape state
   1441      0    stevel 		 *  - block new operations
   1442      0    stevel 		 *  - cancel pending timeouts
   1443      0    stevel 		 *
   1444      0    stevel 		 */
   1445      0    stevel 
   1446   4549   rralphs 		if (ddi_removing_power(devi)) {
   1447      0    stevel 			return (DDI_FAILURE);
   1448   4549   rralphs 		}
   1449      0    stevel 		mutex_enter(ST_MUTEX);
   1450      0    stevel 
   1451      0    stevel 		/*
   1452      0    stevel 		 * Shouldn't already be suspended, if so return failure
   1453      0    stevel 		 */
   1454      0    stevel 		if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) {
   1455      0    stevel 			mutex_exit(ST_MUTEX);
   1456      0    stevel 			return (DDI_FAILURE);
   1457      0    stevel 		}
   1458      0    stevel 		if (un->un_state != ST_STATE_CLOSED) {
   1459      0    stevel 			mutex_exit(ST_MUTEX);
   1460      0    stevel 			return (DDI_FAILURE);
   1461      0    stevel 		}
   1462      0    stevel 
   1463      0    stevel 		/*
   1464      0    stevel 		 * Wait for all outstanding I/O's to complete
   1465      0    stevel 		 *
   1466      0    stevel 		 * we wait on both ncmds and the wait queue for times
   1467      0    stevel 		 * when we are flushing after persistent errors are
   1468      0    stevel 		 * flagged, which is when ncmds can be 0, and the
   1469      0    stevel 		 * queue can still have I/O's.  This way we preserve
   1470      0    stevel 		 * order of biodone's.
   1471      0    stevel 		 */
   1472      0    stevel 		wait_cmds_complete = ddi_get_lbolt();
   1473      0    stevel 		wait_cmds_complete +=
   1474      0    stevel 		    st_wait_cmds_complete * drv_usectohz(1000000);
   1475      0    stevel 		while (un->un_ncmds || un->un_quef ||
   1476      0    stevel 		    (un->un_state == ST_STATE_RESOURCE_WAIT)) {
   1477      0    stevel 
   1478      0    stevel 			if (cv_timedwait(&un->un_tape_busy_cv, ST_MUTEX,
   1479      0    stevel 			    wait_cmds_complete) == -1) {
   1480      0    stevel 				/*
   1481      0    stevel 				 * Time expired then cancel the command
   1482      0    stevel 				 */
   1483   5628   rralphs 				if (st_reset(un, RESET_LUN) == 0) {
   1484      0    stevel 					if (un->un_last_throttle) {
   1485      0    stevel 						un->un_throttle =
   1486      0    stevel 						    un->un_last_throttle;
   1487      0    stevel 					}
   1488      0    stevel 					mutex_exit(ST_MUTEX);
   1489      0    stevel 					return (DDI_FAILURE);
   1490      0    stevel 				} else {
   1491      0    stevel 					break;
   1492      0    stevel 				}
   1493      0    stevel 			}
   1494      0    stevel 		}
   1495      0    stevel 
   1496      0    stevel 		/*
   1497      0    stevel 		 * DDI_SUSPEND says that the system "may" power down, we
   1498      0    stevel 		 * remember the file and block number before rewinding.
   1499      0    stevel 		 * we also need to save state before issuing
   1500      0    stevel 		 * any WRITE_FILE_MARK command.
   1501      0    stevel 		 */
   1502   5628   rralphs 		(void) st_update_block_pos(un, st_cmd, 0);
   1503   4549   rralphs 		COPY_POS(&un->un_suspend_pos, &un->un_pos);
   1504   4549   rralphs 
   1505      0    stevel 
   1506      0    stevel 		/*
   1507      0    stevel 		 * Issue a zero write file fmk command to tell the drive to
   1508      0    stevel 		 * flush any buffered tape marks
   1509      0    stevel 		 */
   1510   5628   rralphs 		(void) st_cmd(un, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD);
   1511      0    stevel 
   1512      0    stevel 		/*
   1513      0    stevel 		 * Because not all tape drives correctly implement buffer
   1514      0    stevel 		 * flushing with the zero write file fmk command, issue a
   1515      0    stevel 		 * synchronous rewind command to force data flushing.
   1516      0    stevel 		 * st_validate_tapemarks() will do a rewind during DDI_RESUME
   1517      0    stevel 		 * anyway.
   1518      0    stevel 		 */
   1519   5628   rralphs 		(void) st_cmd(un, SCMD_REWIND, 0, SYNC_CMD);
   1520      0    stevel 
   1521      0    stevel 		/* stop any new operations */
   1522      0    stevel 		un->un_pwr_mgmt = ST_PWR_SUSPENDED;
   1523      0    stevel 		un->un_throttle = 0;
   1524      0    stevel 
   1525      0    stevel 		/*
   1526      0    stevel 		 * cancel any outstanding timeouts
   1527      0    stevel 		 */
   1528      0    stevel 		if (un->un_delay_tid) {
   1529      0    stevel 			timeout_id_t temp_id = un->un_delay_tid;
   1530      0    stevel 			un->un_delay_tid = 0;
   1531      0    stevel 			un->un_tids_at_suspend |= ST_DELAY_TID;
   1532      0    stevel 			mutex_exit(ST_MUTEX);
   1533      0    stevel 			(void) untimeout(temp_id);
   1534      0    stevel 			mutex_enter(ST_MUTEX);
   1535      0    stevel 		}
   1536      0    stevel 
   1537      0    stevel 		if (un->un_hib_tid) {
   1538      0    stevel 			timeout_id_t temp_id = un->un_hib_tid;
   1539      0    stevel 			un->un_hib_tid = 0;
   1540      0    stevel 			un->un_tids_at_suspend |= ST_HIB_TID;
   1541      0    stevel 			mutex_exit(ST_MUTEX);
   1542      0    stevel 			(void) untimeout(temp_id);
   1543      0    stevel 			mutex_enter(ST_MUTEX);
   1544      0    stevel 		}
   1545      0    stevel 
   1546      0    stevel 		/*
   1547      0    stevel 		 * Suspend the scsi_watch_thread
   1548      0    stevel 		 */
   1549      0    stevel 		if (un->un_swr_token) {
   1550      0    stevel 			opaque_t temp_token = un->un_swr_token;
   1551      0    stevel 			mutex_exit(ST_MUTEX);
   1552      0    stevel 			scsi_watch_suspend(temp_token);
   1553      0    stevel 		} else {
   1554      0    stevel 			mutex_exit(ST_MUTEX);
   1555      0    stevel 		}
   1556      0    stevel 
   1557      0    stevel 		return (DDI_SUCCESS);
   1558      0    stevel 
   1559      0    stevel 	default:
   1560      0    stevel 		ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach failed\n");
   1561      0    stevel 		return (DDI_FAILURE);
   1562      0    stevel 	}
   1563      0    stevel }
   1564      0    stevel 
   1565      0    stevel 
   1566      0    stevel /* ARGSUSED */
   1567      0    stevel static int
   1568   5628   rralphs st_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
   1569      0    stevel {
   1570      0    stevel 	dev_t dev;
   1571      0    stevel 	struct scsi_tape *un;
   1572      0    stevel 	int instance, error;
   1573   4549   rralphs 
   1574   5628   rralphs 	ST_ENTR(dip, st_info);
   1575   4549   rralphs 
   1576      0    stevel 	switch (infocmd) {
   1577      0    stevel 	case DDI_INFO_DEVT2DEVINFO:
   1578      0    stevel 		dev = (dev_t)arg;
   1579      0    stevel 		instance = MTUNIT(dev);
   1580      0    stevel 		if ((un = ddi_get_soft_state(st_state, instance)) == NULL)
   1581      0    stevel 			return (DDI_FAILURE);
   1582      0    stevel 		*result = (void *) ST_DEVINFO;
   1583      0    stevel 		error = DDI_SUCCESS;
   1584      0    stevel 		break;
   1585      0    stevel 	case DDI_INFO_DEVT2INSTANCE:
   1586      0    stevel 		dev = (dev_t)arg;
   1587      0    stevel 		instance = MTUNIT(dev);
   1588      0    stevel 		*result = (void *)(uintptr_t)instance;
   1589      0    stevel 		error = DDI_SUCCESS;
   1590      0    stevel 		break;
   1591      0    stevel 	default:
   1592      0    stevel 		error = DDI_FAILURE;
   1593      0    stevel 	}
   1594      0    stevel 	return (error);
   1595      0    stevel }
   1596      0    stevel 
   1597      0    stevel static int
   1598      0    stevel st_doattach(struct scsi_device *devp, int (*canwait)())
   1599      0    stevel {
   1600      0    stevel 	struct scsi_tape *un = NULL;
   1601   5628   rralphs 	recov_info *ri;
   1602      0    stevel 	int km_flags = (canwait != NULL_FUNC) ? KM_SLEEP : KM_NOSLEEP;
   1603      0    stevel 	int instance;
   1604   1106       mrj 	size_t rlen;
   1605      0    stevel 
   1606   4549   rralphs 	ST_FUNC(devp->sd_dev, st_doattach);
   1607      0    stevel 	/*
   1608      0    stevel 	 * Call the routine scsi_probe to do some of the dirty work.
   1609      0    stevel 	 * If the INQUIRY command succeeds, the field sd_inq in the
   1610      0    stevel 	 * device structure will be filled in.
   1611      0    stevel 	 */
   1612      0    stevel 	ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
   1613   4549   rralphs 	    "st_doattach(): probing\n");
   1614      0    stevel 
   1615      0    stevel 	if (scsi_probe(devp, canwait) == SCSIPROBE_EXISTS) {
   1616      0    stevel 
   1617      0    stevel 		/*
   1618      0    stevel 		 * In checking the whole inq_dtype byte we are looking at both
   1619      0    stevel 		 * the Peripheral Qualifier and the Peripheral Device Type.
   1620      0    stevel 		 * For this driver we are only interested in sequential devices
   1621      0    stevel 		 * that are connected or capable if connecting to this logical
   1622      0    stevel 		 * unit.
   1623      0    stevel 		 */
   1624      0    stevel 		if (devp->sd_inq->inq_dtype ==
   1625      0    stevel 		    (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) {
   1626      0    stevel 			ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
   1627      0    stevel 			    "probe exists\n");
   1628      0    stevel 		} else {
   1629      0    stevel 			/* Something there but not a tape device */
   1630      0    stevel 			scsi_unprobe(devp);
   1631      0    stevel 			return (DDI_FAILURE);
   1632      0    stevel 		}
   1633      0    stevel 	} else {
   1634      0    stevel 		/* Nothing there */
   1635      0    stevel 		ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
   1636      0    stevel 		    "probe failure: nothing there\n");
   1637      0    stevel 		scsi_unprobe(devp);
   1638      0    stevel 		return (DDI_FAILURE);
   1639      0    stevel 	}
   1640      0    stevel 
   1641      0    stevel 
   1642      0    stevel 	/*
   1643      0    stevel 	 * The actual unit is present.
   1644      0    stevel 	 * Now is the time to fill in the rest of our info..
   1645      0    stevel 	 */
   1646      0    stevel 	instance = ddi_get_instance(devp->sd_dev);
   1647      0    stevel 
   1648      0    stevel 	if (ddi_soft_state_zalloc(st_state, instance) != DDI_SUCCESS) {
   1649      0    stevel 		goto error;
   1650      0    stevel 	}
   1651      0    stevel 	un = ddi_get_soft_state(st_state, instance);
   1652      0    stevel 
   1653   2537   rralphs 	ASSERT(un != NULL);
   1654   2537   rralphs 
   1655   5628   rralphs 	un->un_rqs_bp = scsi_alloc_consistent_buf(&devp->sd_address, NULL,
   1656   5628   rralphs 	    MAX_SENSE_LENGTH, B_READ, canwait, NULL);
   1657   5628   rralphs 	if (un->un_rqs_bp == NULL) {
   1658   5628   rralphs 		goto error;
   1659   5628   rralphs 	}
   1660   5628   rralphs 	un->un_rqs = scsi_init_pkt(&devp->sd_address, NULL, un->un_rqs_bp,
   1661   5628   rralphs 	    CDB_GROUP0, 1, st_recov_sz, PKT_CONSISTENT, canwait, NULL);
   1662   5628   rralphs 	if (!un->un_rqs) {
   1663   5628   rralphs 		goto error;
   1664   5628   rralphs 	}
   1665   5628   rralphs 	ASSERT(un->un_rqs->pkt_resid == 0);
   1666   5628   rralphs 	devp->sd_sense =
   1667   5628   rralphs 	    (struct scsi_extended_sense *)un->un_rqs_bp->b_un.b_addr;
   1668   5628   rralphs 	ASSERT(geterror(un->un_rqs_bp) == NULL);
   1669   5628   rralphs 
   1670   5628   rralphs 	(void) scsi_setup_cdb((union scsi_cdb *)un->un_rqs->pkt_cdbp,
   1671   5628   rralphs 	    SCMD_REQUEST_SENSE, 0, MAX_SENSE_LENGTH, 0);
   1672   5628   rralphs 	FILL_SCSI1_LUN(devp, un->un_rqs);
   1673   5628   rralphs 	un->un_rqs->pkt_flags |= (FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON);
   1674   5628   rralphs 	un->un_rqs->pkt_time = st_io_time;
   1675   5628   rralphs 	un->un_rqs->pkt_comp = st_intr;
   1676   5628   rralphs 	ri = (recov_info *)un->un_rqs->pkt_private;
   1677   5628   rralphs 	if (st_recov_sz == sizeof (recov_info)) {
   1678   5628   rralphs 		ri->privatelen = sizeof (recov_info);
   1679   5628   rralphs 	} else {
   1680   5628   rralphs 		ri->privatelen = sizeof (pkt_info);
   1681   5628   rralphs 	}
   1682   5628   rralphs 
   1683      0    stevel 	un->un_sbufp = getrbuf(km_flags);
   1684   5628   rralphs 	un->un_recov_buf = getrbuf(km_flags);
   1685      0    stevel 
   1686      0    stevel 	un->un_uscsi_rqs_buf = kmem_alloc(SENSE_LENGTH, KM_SLEEP);
   1687      0    stevel 
   1688   1106       mrj 	/*
   1689   1106       mrj 	 * use i_ddi_mem_alloc() for now until we have an interface to allocate
   1690   1106       mrj 	 * memory for DMA which doesn't require a DMA handle. ddi_iopb_alloc()
   1691   1106       mrj 	 * is obsolete and we want more flexibility in controlling the DMA
   1692   1106       mrj 	 * address constraints.
   1693   1106       mrj 	 */
   1694   1106       mrj 	(void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr,
   1695   1106       mrj 	    sizeof (struct seq_mode), ((km_flags == KM_SLEEP) ? 1 : 0), 0,
   1696   1106       mrj 	    NULL, (caddr_t *)&un->un_mspl, &rlen, NULL);
   1697      0    stevel 
   1698   4549   rralphs 	(void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr,
   1699   4549   rralphs 	    sizeof (read_pos_data_t), ((km_flags == KM_SLEEP) ? 1 : 0), 0,
   1700   4549   rralphs 	    NULL, (caddr_t *)&un->un_read_pos_data, &rlen, NULL);
   1701   4549   rralphs 
   1702   4549   rralphs 	if (!un->un_sbufp || !un->un_mspl || !un->un_read_pos_data) {
   1703      0    stevel 		ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG,
   1704      0    stevel 		    "probe partial failure: no space\n");
   1705      0    stevel 		goto error;
   1706      0    stevel 	}
   1707      0    stevel 
   1708      0    stevel 	bzero(un->un_mspl, sizeof (struct seq_mode));
   1709      0    stevel 
   1710      0    stevel 	cv_init(&un->un_sbuf_cv, NULL, CV_DRIVER, NULL);
   1711      0    stevel 	cv_init(&un->un_queue_cv, NULL, CV_DRIVER, NULL);
   1712      0    stevel 	cv_init(&un->un_clscv, NULL, CV_DRIVER, NULL);
   1713      0    stevel 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
   1714   5251       mrj #ifdef	__x86
   1715    177  cz147101 	cv_init(&un->un_contig_mem_cv, NULL, CV_DRIVER, NULL);
   1716    177  cz147101 #endif
   1717      0    stevel 
   1718      0    stevel 	/* Initialize power managemnet condition variable */
   1719      0    stevel 	cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL);
   1720      0    stevel 	cv_init(&un->un_tape_busy_cv, NULL, CV_DRIVER, NULL);
   1721   5628   rralphs 	cv_init(&un->un_recov_buf_cv, NULL, CV_DRIVER, NULL);
   1722   5628   rralphs 
   1723   5628   rralphs 	un->un_recov_taskq = ddi_taskq_create(devp->sd_dev,
   1724   5628   rralphs 	    "un_recov_taskq", 1, TASKQ_DEFAULTPRI, km_flags);
   1725   5628   rralphs 
   1726   5628   rralphs 	ASSERT(un->un_recov_taskq != NULL);
   1727      0    stevel 
   1728   4549   rralphs 	un->un_pos.pmode = invalid;
   1729      0    stevel 	un->un_sd	= devp;
   1730      0    stevel 	un->un_swr_token = (opaque_t)NULL;
   1731      0    stevel 	un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE;
   1732   2537   rralphs 	un->un_wormable = st_is_drive_worm;
   1733   5628   rralphs 	un->un_media_id_method = st_get_media_identification;
   1734   5628   rralphs 	/*
   1735   5628   rralphs 	 * setting long a initial as it contains logical file info.
   1736   5628   rralphs 	 * support for long format is mandatory but many drive don't do it.
   1737   5628   rralphs 	 */
   1738   4549   rralphs 	un->un_read_pos_type = LONG_POS;
   1739   4549   rralphs 
   1740   4549   rralphs 	un->un_suspend_pos.pmode = invalid;
   1741      0    stevel 
   1742   6941   rralphs 	st_add_recovery_info_to_pkt(un, un->un_rqs_bp, un->un_rqs);
   1743   6941   rralphs 
   1744   5251       mrj #ifdef	__x86
   1745    177  cz147101 	if (ddi_dma_alloc_handle(ST_DEVINFO, &st_contig_mem_dma_attr,
   1746   4549   rralphs 	    DDI_DMA_SLEEP, NULL, &un->un_contig_mem_hdl) != DDI_SUCCESS) {
   1747    177  cz147101 		ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG,
   1748    177  cz147101 		    "allocation of contiguous memory dma handle failed!");
   1749    177  cz147101 		un->un_contig_mem_hdl = NULL;
   1750    177  cz147101 		goto error;
   1751    177  cz147101 	}
   1752    177  cz147101 #endif
   1753    177  cz147101 
   1754      0    stevel 	/*
   1755      0    stevel 	 * Since this driver manages devices with "remote" hardware,
   1756      0    stevel 	 * i.e. the devices themselves have no "reg" properties,
   1757      0    stevel 	 * the SUSPEND/RESUME commands in detach/attach will not be
   1758      0    stevel 	 * called by the power management framework unless we request
   1759      0    stevel 	 * it by creating a "pm-hardware-state" property and setting it
   1760      0    stevel 	 * to value "needs-suspend-resume".
   1761      0    stevel 	 */
   1762      0    stevel 	if (ddi_prop_update_string(DDI_DEV_T_NONE, devp->sd_dev,
   1763      0    stevel 	    "pm-hardware-state", "needs-suspend-resume") !=
   1764      0    stevel 	    DDI_PROP_SUCCESS) {
   1765      0    stevel 
   1766      0    stevel 		ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
   1767      0    stevel 		    "ddi_prop_update(\"pm-hardware-state\") failed\n");
   1768      0    stevel 		goto error;
   1769      0    stevel 	}
   1770      0    stevel 
   1771      0    stevel 	if (ddi_prop_create(DDI_DEV_T_NONE, devp->sd_dev, DDI_PROP_CANSLEEP,
   1772      0    stevel 	    "no-involuntary-power-cycles", NULL, 0) != DDI_PROP_SUCCESS) {
   1773      0    stevel 
   1774      0    stevel 		ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
   1775      0    stevel 		    "ddi_prop_create(\"no-involuntary-power-cycles\") "
   1776      0    stevel 		    "failed\n");
   1777      0    stevel 		goto error;
   1778      0    stevel 	}
   1779      0    stevel 
   1780   5628   rralphs 	(void) scsi_reset_notify(ROUTE, SCSI_RESET_NOTIFY,
   1781   5628   rralphs 	    st_reset_notification, (caddr_t)un);
   1782   5628   rralphs 
   1783   5628   rralphs 	ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, "attach success\n");
   1784      0    stevel 	return (DDI_SUCCESS);
   1785      0    stevel 
   1786      0    stevel error:
   1787      0    stevel 	devp->sd_sense = NULL;
   1788      0    stevel 
   1789      0    stevel 	ddi_remove_minor_node(devp->sd_dev, NULL);
   1790      0    stevel 	if (un) {
   1791      0    stevel 		if (un->un_mspl) {
   1792   1900      eota 			i_ddi_mem_free((caddr_t)un->un_mspl, NULL);
   1793      0    stevel 		}
   1794   4549   rralphs 		if (un->un_read_pos_data) {
   1795   4549   rralphs 			i_ddi_mem_free((caddr_t)un->un_read_pos_data, 0);
   1796   4549   rralphs 		}
   1797      0    stevel 		if (un->un_sbufp) {
   1798      0    stevel 			freerbuf(un->un_sbufp);
   1799      0    stevel 		}
   1800   5628   rralphs 		if (un->un_recov_buf) {
   1801   5628   rralphs 			freerbuf(un->un_recov_buf);
   1802   5628   rralphs 		}
   1803      0    stevel 		if (un->un_uscsi_rqs_buf) {
   1804      0    stevel 			kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH);
   1805      0    stevel 		}
   1806   5251       mrj #ifdef	__x86
   1807    177  cz147101 		if (un->un_contig_mem_hdl != NULL) {
   1808    177  cz147101 			ddi_dma_free_handle(&un->un_contig_mem_hdl);
   1809    177  cz147101 		}
   1810    177  cz147101 #endif
   1811   5628   rralphs 		if (un->un_rqs) {
   1812   5628   rralphs 			scsi_destroy_pkt(un->un_rqs);
   1813   5628   rralphs 		}
   1814   5628   rralphs 
   1815   5628   rralphs 		if (un->un_rqs_bp) {
   1816   5628   rralphs 			scsi_free_consistent_buf(un->un_rqs_bp);
   1817   5628   rralphs 		}
   1818   5628   rralphs 
   1819      0    stevel 		ddi_soft_state_free(st_state, instance);
   1820      0    stevel 		devp->sd_private = NULL;
   1821      0    stevel 	}
   1822      0    stevel 
   1823      0    stevel 	if (devp->sd_inq) {
   1824      0    stevel 		scsi_unprobe(devp);
   1825      0    stevel 	}
   1826      0    stevel 	return (DDI_FAILURE);
   1827      0    stevel }
   1828      0    stevel 
   1829      0    stevel typedef int
   1830      0    stevel (*cfg_functp)(struct scsi_tape *, char *vidpid, struct st_drivetype *);
   1831      0    stevel 
   1832      0    stevel static cfg_functp config_functs[] = {
   1833      0    stevel 	st_get_conf_from_st_dot_conf,
   1834      0    stevel 	st_get_conf_from_st_conf_dot_c,
   1835   5425  yl194034 	st_get_conf_from_tape_drive,
   1836      0    stevel 	st_get_default_conf
   1837      0    stevel };
   1838      0    stevel 
   1839      0    stevel 
   1840      0    stevel /*
   1841      0    stevel  * determine tape type, using tape-config-list or built-in table or
   1842      0    stevel  * use a generic tape config entry
   1843      0    stevel  */
   1844      0    stevel static void
   1845      0    stevel st_known_tape_type(struct scsi_tape *un)
   1846      0    stevel {
   1847      0    stevel 	struct st_drivetype *dp;
   1848      0    stevel 	cfg_functp *config_funct;
   1849   5524  yl194034 	uchar_t reserved;
   1850      0    stevel 
   1851   4549   rralphs 	ST_FUNC(ST_DEVINFO, st_known_tape_type);
   1852   5524  yl194034 
   1853   5524  yl194034 	reserved = (un->un_rsvd_status & ST_RESERVE) ? ST_RESERVE
   1854   5524  yl194034 	    : ST_RELEASE;
   1855   5524  yl194034 
   1856      0    stevel 	/*
   1857      0    stevel 	 * XXX:  Emulex MT-02 (and emulators) predates SCSI-1 and has
   1858      0    stevel 	 *	 no vid & pid inquiry data.  So, we provide one.
   1859      0    stevel 	 */
   1860      0    stevel 	if (ST_INQUIRY->inq_len == 0 ||
   1861   4549   rralphs 	    (bcmp("\0\0\0\0\0\0\0\0", ST_INQUIRY->inq_vid, 8) == 0)) {
   1862      0    stevel 		(void) strcpy((char *)ST_INQUIRY->inq_vid, ST_MT02_NAME);
   1863      0    stevel 	}
   1864      0    stevel 
   1865   5524  yl194034 	if (un->un_dp_size == 0) {
   1866   5524  yl194034 		un->un_dp_size = sizeof (struct st_drivetype);
   1867   5524  yl194034 		dp = kmem_zalloc((size_t)un->un_dp_size, KM_SLEEP);
   1868   5524  yl194034 		un->un_dp = dp;
   1869   5524  yl194034 	} else {
   1870   5524  yl194034 		dp = un->un_dp;
   1871   5524  yl194034 	}
   1872      0    stevel 
   1873   5425  yl194034 	un->un_dp->non_motion_timeout = st_io_time;
   1874      0    stevel 	/*
   1875      0    stevel 	 * Loop through the configuration methods till one works.
   1876      0    stevel 	 */
   1877      0    stevel 	for (config_funct = &config_functs[0]; ; config_funct++) {
   1878      0    stevel 		if ((*config_funct)(un, ST_INQUIRY->inq_vid, dp)) {
   1879      0    stevel 			break;
   1880      0    stevel 		}
   1881      0    stevel 	}
   1882  10740   Jianfei 
   1883  11191   Jianfei 	/*
   1884  11191   Jianfei 	 * If we didn't just make up this configuration and
   1885  11191   Jianfei 	 * all the density codes are the same..
   1886  11191   Jianfei 	 * Set Auto Density over ride.
   1887  11191   Jianfei 	 */
   1888  11191   Jianfei 	if (*config_funct != st_get_default_conf) {
   1889  11191   Jianfei 		/*
   1890  11191   Jianfei 		 * If this device is one that is configured and all
   1891  11191   Jianfei 		 * densities are the same, This saves doing gets and set
   1892  11191   Jianfei 		 * that yield nothing.
   1893  11191   Jianfei 		 */
   1894  11191   Jianfei 		if ((dp->densities[0]) == (dp->densities[1]) &&
   1895  11191   Jianfei 		    (dp->densities[0]) == (dp->densities[2]) &&
   1896  11191   Jianfei 		    (dp->densities[0]) == (dp->densities[3])) {
   1897  11191   Jianfei 
   1898  11191   Jianfei 			dp->options |= ST_AUTODEN_OVERRIDE;
   1899  11191   Jianfei 		}
   1900  11191   Jianfei 	}
   1901  11191   Jianfei 
   1902  11191   Jianfei 
   1903  11191   Jianfei 	/*
   1904  11191   Jianfei 	 * Store tape drive characteristics.
   1905  11191   Jianfei 	 */
   1906  11191   Jianfei 	un->un_status = 0;
   1907  11191   Jianfei 	un->un_attached = 1;
   1908  11191   Jianfei 	un->un_init_options = dp->options;
   1909  11191   Jianfei 
   1910  11191   Jianfei 	/* setup operation time-outs based on options */
   1911  11191   Jianfei 	st_calculate_timeouts(un);
   1912  11191   Jianfei 
   1913  11191   Jianfei 	/* TLR support */
   1914  10740   Jianfei 	if (un->un_dp->type != ST_TYPE_INVALID) {
   1915  10740   Jianfei 		int result;
   1916  10740   Jianfei 
   1917  10740   Jianfei 		/* try and enable TLR */
   1918  10740   Jianfei 		un->un_tlr_flag = TLR_SAS_ONE_DEVICE;
   1919  10740   Jianfei 		result = st_set_target_TLR_mode(un, st_uscsi_cmd);
   1920  10740   Jianfei 		if (result == EACCES) {
   1921  10740   Jianfei 			/*
   1922  10740   Jianfei 			 * From attach command failed.
   1923  10740   Jianfei 			 * Set dp type so is run again on open.
   1924  10740   Jianfei 			 */
   1925  10740   Jianfei 			un->un_dp->type = ST_TYPE_INVALID;
   1926  10740   Jianfei 			un->un_tlr_flag = TLR_NOT_KNOWN;
   1927  10740   Jianfei 		} else if (result == 0) {
   1928  10740   Jianfei 			if (scsi_ifgetcap(&un->un_sd->sd_address,
   1929  10740   Jianfei 			    "tran-layer-retries", 1) == -1) {
   1930  10740   Jianfei 				un->un_tlr_flag = TLR_NOT_SUPPORTED;
   1931  10740   Jianfei 				(void) st_set_target_TLR_mode(un, st_uscsi_cmd);
   1932  10740   Jianfei 			} else {
   1933  10740   Jianfei 				un->un_tlr_flag = TLR_SAS_ONE_DEVICE;
   1934  10740   Jianfei 			}
   1935  10740   Jianfei 		} else {
   1936  10740   Jianfei 			un->un_tlr_flag = TLR_NOT_SUPPORTED;
   1937  10740   Jianfei 		}
   1938  10740   Jianfei 	}
   1939      0    stevel 
   1940      0    stevel 	/* make sure if we are supposed to be variable, make it variable */
   1941      0    stevel 	if (dp->options & ST_VARIABLE) {
   1942      0    stevel 		dp->bsize = 0;
   1943   5524  yl194034 	}
   1944   5524  yl194034 
   1945   5524  yl194034 	if (reserved != ((un->un_rsvd_status & ST_RESERVE) ? ST_RESERVE
   1946   5524  yl194034 	    : ST_RELEASE)) {
   1947   5628   rralphs 		(void) st_reserve_release(un, reserved, st_uscsi_cmd);
   1948   5628   rralphs 	}
   1949   5628   rralphs 
   1950   6941   rralphs 	un->un_unit_attention_flags |= 1;
   1951      0    stevel 
   1952      0    stevel 	scsi_log(ST_DEVINFO, st_label, CE_NOTE, "?<%s>\n", dp->name);
   1953   5628   rralphs 
   1954      0    stevel }
   1955      0    stevel 
   1956      0    stevel 
   1957      0    stevel typedef struct {
   1958      0    stevel 	int mask;
   1959      0    stevel 	int bottom;
   1960      0    stevel 	int top;
   1961      0    stevel 	char *name;
   1962      0    stevel } conf_limit;
   1963      0    stevel 
   1964      0    stevel static const conf_limit conf_limits[] = {
   1965      0    stevel 
   1966      0    stevel 	-1,		1,		2,		"conf version",
   1967      0    stevel 	-1,		MT_ISTS,	ST_LAST_TYPE,	"drive type",
   1968      0    stevel 	-1,		0,		0xffffff,	"block size",
   1969      0    stevel 	ST_VALID_OPTS,	0,		ST_VALID_OPTS,	"options",
   1970      0    stevel 	-1,		0,		4,		"number of densities",
   1971      0    stevel 	-1,		0,		UINT8_MAX,	"density code",
   1972      0    stevel 	-1,		0,		3,		"default density",
   1973      0    stevel 	-1,		0,		UINT16_MAX,	"non motion timeout",
   1974      0    stevel 	-1,		0,		UINT16_MAX,	"I/O timeout",
   1975      0    stevel 	-1,		0,		UINT16_MAX,	"space timeout",
   1976      0    stevel 	-1,		0,		UINT16_MAX,	"load timeout",
   1977      0    stevel 	-1,		0,		UINT16_MAX,	"unload timeout",
   1978      0    stevel 	-1,		0,		UINT16_MAX,	"erase timeout",
   1979      0    stevel 	0,		0,		0,		NULL
   1980      0    stevel };
   1981      0    stevel 
   1982      0    stevel static int
   1983      0    stevel st_validate_conf_data(struct scsi_tape *un, int *list, int list_len,
   1984      0    stevel     const char *conf_name)
   1985      0    stevel {
   1986      0    stevel 	int dens;
   1987      0    stevel 	int ndens;
   1988      0    stevel 	int value;
   1989      0    stevel 	int type;
   1990      0    stevel 	int count;
   1991      0    stevel 	const conf_limit *limit = &conf_limits[0];
   1992   4549   rralphs 
   1993   4549   rralphs 	ST_FUNC(ST_DEVINFO, st_validate_conf_data);
   1994      0    stevel 
   1995      0    stevel 	ST_DEBUG3(ST_DEVINFO, st_label, CE_NOTE,
   1996      0    stevel 	    "Checking %d entrys total with %d densities\n", list_len, list[4]);
   1997      0    stevel 
   1998      0    stevel 	count = list_len;
   1999      0    stevel 	type = *list;
   2000      0    stevel 	for (;  count && limit->name; count--, list++, limit++) {
   2001      0    stevel 
   2002      0    stevel 		value = *list;
   2003      0    stevel 		if (value & ~limit->mask) {
   2004      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
   2005      0    stevel 			    "%s %s value invalid bits set: 0x%X\n",
   2006      0    stevel 			    conf_name, limit->name, value & ~limit->mask);
   2007      0    stevel 			*list &= limit->mask;
   2008      0    stevel 		} else if (value < limit->bottom) {
   2009      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
   2010      0    stevel 			    "%s %s value too low: value = %d limit %d\n",
   2011      0    stevel 			    conf_name, limit->name, value, limit->bottom);
   2012      0    stevel 		} else if (value > limit->top) {
   2013      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
   2014      0    stevel 			    "%s %s value too high: value = %d limit %d\n",
   2015      0    stevel 			    conf_name, limit->name, value, limit->top);
   2016      0    stevel 		} else {
   2017      0    stevel 			ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT,
   2018      0    stevel 			    "%s %s value = 0x%X\n",
   2019      0    stevel 			    conf_name, limit->name, value);
   2020      0    stevel 		}
   2021      0    stevel 
   2022      0    stevel 		/* If not the number of densities continue */
   2023      0    stevel 		if (limit != &conf_limits[4]) {
   2024      0    stevel 			continue;
   2025      0    stevel 		}
   2026      0    stevel 
   2027      0    stevel 		/* If number of densities is not in range can't use config */
   2028      0    stevel 		if (value < limit->bottom || value > limit->top) {
   2029      0    stevel 			return (-1);
   2030      0    stevel 		}
   2031      0    stevel 
   2032      0    stevel 		ndens = min(value, NDENSITIES);
   2033      0    stevel 		if ((type == 1) && (list_len - ndens) != 6) {
   2034      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
   2035      0    stevel 			    "%s conf version 1 with %d densities has %d items"
   2036      0    stevel 			    " should have %d",
   2037      0    stevel 			    conf_name, ndens, list_len, 6 + ndens);
   2038      0    stevel 		} else if ((type == 2) && (list_len - ndens) != 13) {
   2039      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
   2040      0    stevel 			    "%s conf version 2 with %d densities has %d items"
   2041      0    stevel 			    " should have %d",
   2042      0    stevel 			    conf_name, ndens, list_len, 13 + ndens);
   2043      0    stevel 		}
   2044      0    stevel 
   2045      0    stevel 		limit++;
   2046      0    stevel 		for (dens = 0; dens < ndens && count; dens++) {
   2047      0    stevel 			count--;
   2048      0    stevel 			list++;
   2049      0    stevel 			value = *list;
   2050      0    stevel 			if (value < limit->bottom) {
   2051      0    stevel 				scsi_log(ST_DEVINFO, st_label, CE_NOTE,
   2052      0    stevel 				    "%s density[%d] value too low: value ="
   2053      0    stevel 				    " 0x%X limit 0x%X\n",
   2054      0    stevel 				    conf_name, dens, value, limit->bottom);
   2055      0    stevel 			} else if (value > limit->top) {
   2056      0    stevel 				scsi_log(ST_DEVINFO, st_label, CE_NOTE,
   2057      0    stevel 				    "%s density[%d] value too high: value ="
   2058      0    stevel 				    " 0x%X limit 0x%X\n",
   2059      0    stevel 				    conf_name, dens, value, limit->top);
   2060      0    stevel 			} else {
   2061      0    stevel 				ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT,
   2062      0    stevel 				    "%s density[%d] value = 0x%X\n",
   2063      0    stevel 				    conf_name, dens, value);
   2064      0    stevel 			}
   2065      0    stevel 		}
   2066      0    stevel 	}
   2067      0    stevel 
   2068      0    stevel 	return (0);
   2069      0    stevel }
   2070      0    stevel 
   2071      0    stevel static int
   2072      0    stevel st_get_conf_from_st_dot_conf(struct scsi_tape *un, char *vidpid,
   2073      0    stevel     struct st_drivetype *dp)
   2074      0    stevel {
   2075      0    stevel 	caddr_t config_list = NULL;
   2076      0    stevel 	caddr_t data_list = NULL;
   2077      0    stevel 	int	*data_ptr;
   2078      0    stevel 	caddr_t vidptr, prettyptr, datanameptr;
   2079      0    stevel 	size_t	vidlen, prettylen, datanamelen, tripletlen = 0;
   2080      0    stevel 	int config_list_len, data_list_len, len, i;
   2081      0    stevel 	int version;
   2082      0    stevel 	int found = 0;
   2083      0    stevel 
   2084   4549   rralphs 	ST_FUNC(ST_DEVINFO, st_get_conf_from_st_dot_conf);
   2085      0    stevel 
   2086      0    stevel 	/*
   2087      0    stevel 	 * Determine type of tape controller. Type is determined by
   2088      0    stevel 	 * checking the vendor ids of the earlier inquiry command and
   2089      0    stevel 	 * comparing those with vids in tape-config-list defined in st.conf
   2090      0    stevel 	 */
   2091      0    stevel 	if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, DDI_PROP_DONTPASS,
   2092      0    stevel 	    "tape-config-list", (caddr_t)&config_list, &config_list_len)
   2093      0    stevel 	    != DDI_PROP_SUCCESS) {
   2094      0    stevel 		return (found);
   2095      0    stevel 	}
   2096      0    stevel 
   2097      0    stevel 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
   2098      0    stevel 	    "st_get_conf_from_st_dot_conf(): st.conf has tape-config-list\n");
   2099      0    stevel 
   2100      0    stevel 	/*
   2101      0    stevel 	 * Compare vids in each triplet - if it matches, get value for
   2102      0    stevel 	 * data_name and contruct a st_drivetype struct
   2103      0    stevel 	 * tripletlen is not set yet!
   2104      0    stevel 	 */
   2105      0    stevel 	for (len = config_list_len, vidptr = config_list;
   2106      0    stevel 	    len > 0;
   2107      0    stevel 	    vidptr += tripletlen, len -= tripletlen) {
   2108      0    stevel 
   2109      0    stevel 		vidlen = strlen(vidptr);
   2110      0    stevel 		prettyptr = vidptr + vidlen + 1;
   2111      0    stevel 		prettylen = strlen(prettyptr);
   2112      0    stevel 		datanameptr = prettyptr + prettylen + 1;
   2113      0    stevel 		datanamelen = strlen(datanameptr);
   2114      0    stevel 		tripletlen = vidlen + prettylen + datanamelen + 3;
   2115      0    stevel 
   2116      0    stevel 		if (vidlen == 0) {
   2117      0    stevel 			continue;
   2118      0    stevel 		}
   2119      0    stevel 
   2120      0    stevel 		/*
   2121      0    stevel 		 * If inquiry vid dosen't match this triplets vid,
   2122      0    stevel 		 * try the next.
   2123      0    stevel 		 */
   2124      0    stevel 		if (strncasecmp(vidpid, vidptr, vidlen)) {
   2125      0    stevel 			continue;
   2126      0    stevel 		}
   2127      0    stevel 
   2128      0    stevel 		/*
   2129      0    stevel 		 * if prettylen is zero then use the vid string
   2130      0    stevel 		 */
   2131      0    stevel 		if (prettylen == 0) {
   2132      0    stevel 			prettyptr = vidptr;
   2133      0    stevel 			prettylen = vidlen;
   2134      0    stevel 		}
   2135      0    stevel 
   2136      0    stevel 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
   2137      0    stevel 		    "vid = %s, pretty=%s, dataname = %s\n",
   2138      0    stevel 		    vidptr, prettyptr, datanameptr);
   2139      0    stevel 
   2140      0    stevel 		/*
   2141      0    stevel 		 * get the data list
   2142      0    stevel 		 */
   2143      0    stevel 		if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, 0,
   2144      0    stevel 		    datanameptr, (caddr_t)&data_list,
   2145      0    stevel 		    &data_list_len) != DDI_PROP_SUCCESS) {
   2146      0    stevel 			/*
   2147      0    stevel 			 * Error in getting property value
   2148      0    stevel 			 * print warning!
   2149      0    stevel 			 */
   2150      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
   2151      0    stevel 			    "data property (%s) has no value\n",
   2152      0    stevel 			    datanameptr);
   2153      0    stevel 			continue;
   2154      0    stevel 		}
   2155      0    stevel 
   2156      0    stevel 		/*
   2157      0    stevel 		 * now initialize the st_drivetype struct
   2158      0    stevel 		 */
   2159      0    stevel 		(void) strncpy(dp->name, prettyptr, ST_NAMESIZE - 1);
   2160      0    stevel 		dp->length = (int)min(vidlen, (VIDPIDLEN - 1));
   2161      0    stevel 		(void) strncpy(dp->vid, vidptr, dp->length);
   2162      0    stevel 		data_ptr = (int *)data_list;
   2163      0    stevel 		/*
   2164      0    stevel 		 * check if data is enough for version, type,
   2165      0    stevel 		 * bsize, options, # of densities, density1,
   2166      0    stevel 		 * density2, ..., default_density
   2167      0    stevel 		 */
   2168      0    stevel 		if ((data_list_len < 5 * sizeof (int)) ||
   2169      0    stevel 		    (data_list_len < 6 * sizeof (int) +
   2170      0    stevel 		    *(data_ptr + 4) * sizeof (int))) {
   2171      0    stevel 			/*
   2172      0    stevel 			 * print warning and skip to next triplet.
   2173      0    stevel 			 */
   2174      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
   2175      0    stevel 			    "data property (%s) incomplete\n",
   2176      0    stevel 			    datanameptr);
   2177      0    stevel 			kmem_free(data_list, data_list_len);
   2178      0    stevel 			continue;
   2179      0    stevel 		}
   2180      0    stevel 
   2181      0    stevel 		if (st_validate_conf_data(un, data_ptr,
   2182      0    stevel 		    data_list_len / sizeof (int), datanameptr)) {
   2183      0    stevel 			kmem_free(data_list, data_list_len);
   2184      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
   2185      0    stevel 			    "data property (%s) rejected\n",
   2186      0    stevel 			    datanameptr);
   2187      0    stevel 			continue;
   2188      0    stevel 		}
   2189      0    stevel 
   2190      0    stevel 		/*
   2191      0    stevel 		 * check version
   2192      0    stevel 		 */
   2193      0    stevel 		version = *data_ptr++;
   2194      0    stevel 		if (version != 1 && version != 2) {
   2195      0    stevel 			/* print warning but accept it */
   2196      0    stevel 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
   2197      0    stevel 			    "Version # for data property (%s) "
   2198      0    stevel 			    "not set to 1 or 2\n", datanameptr);
   2199      0    stevel 		}
   2200      0    stevel 
   2201      0    stevel 		dp->type    = *data_ptr++;
   2202      0    stevel 		dp->bsize   = *data_ptr++;
   2203      0    stevel 		dp->options = *data_ptr++;
   2204      0    stevel 		dp->options |= ST_DYNAMIC;
   2205      0    stevel 		len = *data_ptr++;
   2206      0    stevel 		for (i = 0; i < NDENSITIES; i++) {
   2207      0    stevel 			if (i < len) {
   2208      0    stevel 				dp->densities[i] = *data_ptr++;
   2209      0    stevel 			}
   2210      0    stevel 		}
   2211      0    stevel 		dp->default_density = *data_ptr << 3;
   2212      0    stevel 		if (version == 2 &&
   2213      0    stevel 		    data_list_len >= (13 + len) * sizeof (int)) {
   2214      0    stevel 			data_ptr++;
   2215      0    stevel 			dp->non_motion_timeout	= *data_ptr++;
   2216      0    stevel 			dp->io_timeout		= *data_ptr++;
   2217      0    stevel 			dp->rewind_timeout	= *data_ptr++;
   2218      0    stevel 			dp->space_timeout	= *data_ptr++;
   2219      0    stevel 			dp->load_timeout	= *data_ptr++;
   2220      0    stevel 			dp->unload_timeout	= *data_ptr++;
   2221      0    stevel 			dp->erase_timeout	= *data_ptr++;
   2222      0    stevel 		}
   2223      0    stevel 		kmem_free(data_list, data_list_len);
   2224      0    stevel 		found = 1;
   2225      0    stevel 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
   2226      0    stevel 		    "found in st.conf: vid = %s, pretty=%s\n",
   2227      0    stevel 		    dp->vid, dp->name);
   2228      0    stevel 		break;
   2229      0    stevel 	}
   2230      0    stevel 
   2231      0    stevel 	/*
   2232      0    stevel 	 * free up the memory allocated by ddi_getlongprop
   2233      0    stevel 	 */
   2234      0    stevel 	if (config_list) {
   2235      0    stevel 		kmem_free(config_list, config_list_len);
   2236      0    stevel 	}
   2237      0    stevel 	return (found);
   2238      0    stevel }
   2239      0    stevel 
   2240      0    stevel static int
   2241      0    stevel st_get_conf_from_st_conf_dot_c(struct scsi_tape *un, char *vidpid,
   2242      0    stevel     struct st_drivetype *dp)
   2243      0    stevel {
   2244      0    stevel 	int i;
   2245      0    stevel 
   2246   4549   rralphs 	ST_FUNC(ST_DEVINFO, st_get_conf_from_st_conf_dot_c);
   2247      0    stevel 	/*
   2248      0    stevel 	 * Determine type of tape controller.  Type is determined by
   2249      0    stevel 	 * checking the result of the earlier inquiry command and
   2250      0    stevel 	 * comparing vendor ids with strings in a table declared in st_conf.c.
   2251      0    stevel 	 */
   2252      0    stevel 	ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2253      0    stevel 	    "st_get_conf_from_st_conf_dot_c(): looking at st_drivetypes\n");
   2254      0    stevel 
   2255      0    stevel 	for (i = 0; i < st_ndrivetypes; i++) {
   2256      0    stevel 		if (st_drivetypes[i].length == 0) {
   2257      0    stevel 			continue;
   2258      0    stevel 		}
   2259      0    stevel 		if (strncasecmp(vidpid, st_drivetypes[i].vid,
   2260      0    stevel 		    st_drivetypes[i].length)) {
   2261      0    stevel 			continue;
   2262      0    stevel 		}
   2263      0    stevel 		bcopy(&st_drivetypes[i], dp, sizeof (st_drivetypes[i]));
   2264      0    stevel 		return (1);
   2265      0    stevel 	}
   2266      0    stevel 	return (0);
   2267      0    stevel }
   2268      0    stevel 
   2269      0    stevel static int
   2270   5425  yl194034 st_get_conf_from_tape_drive(struct scsi_tape *un, char *vidpid,
   2271   5425  yl194034     struct st_drivetype *dp)
   2272   5425  yl194034 {
   2273   5425  yl194034 	int bsize;
   2274   5425  yl194034 	ulong_t maxbsize;
   2275   5425  yl194034 	caddr_t buf;
   2276   5425  yl194034 	struct st_drivetype *tem_dp;
   2277   5425  yl194034 	struct read_blklim *blklim;
   2278   5425  yl194034 	int rval;
   2279   5425  yl194034 	int i;
   2280   5425  yl194034 
   2281   5628   rralphs 	ST_FUNC(ST_DEVINFO, st_get_conf_from_tape_drive);
   2282   5425  yl194034 
   2283   5425  yl194034 	/*
   2284   5425  yl194034 	 * Determine the type of tape controller. Type is determined by
   2285   5425  yl194034 	 * sending SCSI commands to tape drive and deriving the type from
   2286   5425  yl194034 	 * the returned data.
   2287   5425  yl194034 	 */
   2288   5425  yl194034 	ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2289   5425  yl194034 	    "st_get_conf_from_tape_drive(): asking tape drive\n");
   2290   5425  yl194034 
   2291   5425  yl194034 	tem_dp = kmem_zalloc(sizeof (struct st_drivetype), KM_SLEEP);
   2292   5425  yl194034 
   2293   5425  yl194034 	/*
   2294   5425  yl194034 	 * Make up a name
   2295   5425  yl194034 	 */
   2296   5425  yl194034 	bcopy(vidpid, tem_dp->name, VIDPIDLEN);
   2297   5425  yl194034 	tem_dp->name[VIDPIDLEN] = '\0';
   2298   5425  yl194034 	tem_dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1));
   2299   5425  yl194034 	(void) strncpy(tem_dp->vid, ST_INQUIRY->inq_vid, tem_dp->length);
   2300   5425  yl194034 	/*
   2301   5425  yl194034 	 * 'clean' vendor and product strings of non-printing chars
   2302   5425  yl194034 	 */
   2303   5425  yl194034 	for (i = 0; i < VIDPIDLEN - 1; i ++) {
   2304   5425  yl194034 		if (tem_dp->name[i] < ' ' || tem_dp->name[i] > '~') {
   2305   5425  yl194034 			tem_dp->name[i] = '.';
   2306   5425  yl194034 		}
   2307   5425  yl194034 	}
   2308   5425  yl194034 
   2309   5425  yl194034 	/*
   2310   5425  yl194034 	 * MODE SENSE to determine block size.
   2311   5425  yl194034 	 */
   2312   5628   rralphs 	un->un_dp->options |= ST_MODE_SEL_COMP | ST_UNLOADABLE;
   2313   5524  yl194034 	rval = st_modesense(un);
   2314   5524  yl194034 	if (rval) {
   2315   5524  yl194034 		if (rval == EACCES) {
   2316   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2317   5524  yl194034 			rval = 1;
   2318   5524  yl194034 		} else {
   2319   5524  yl194034 			un->un_dp->options &= ~ST_MODE_SEL_COMP;
   2320   5524  yl194034 			rval = 0;
   2321   5524  yl194034 		}
   2322   5425  yl194034 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2323   5425  yl194034 		    "st_get_conf_from_tape_drive(): fail to mode sense\n");
   2324   5524  yl194034 		goto exit;
   2325   5425  yl194034 	}
   2326   5425  yl194034 
   2327   5425  yl194034 	/* Can mode sense page 0x10 or 0xf */
   2328   5425  yl194034 	tem_dp->options |= ST_MODE_SEL_COMP;
   2329   5425  yl194034 	bsize = (un->un_mspl->high_bl << 16)	|
   2330   5425  yl194034 	    (un->un_mspl->mid_bl << 8)		|
   2331   5425  yl194034 	    (un->un_mspl->low_bl);
   2332   5425  yl194034 
   2333   5425  yl194034 	if (bsize == 0) {
   2334   5425  yl194034 		tem_dp->options |= ST_VARIABLE;
   2335   5425  yl194034 		tem_dp->bsize = 0;
   2336   5425  yl194034 	} else if (bsize > ST_MAXRECSIZE_FIXED) {
   2337   5628   rralphs 		rval = st_change_block_size(un, 0);
   2338   5524  yl194034 		if (rval) {
   2339   5524  yl194034 			if (rval == EACCES) {
   2340   5524  yl194034 				un->un_dp->type = ST_TYPE_INVALID;
   2341   5524  yl194034 				rval = 1;
   2342   5524  yl194034 			} else {
   2343   5524  yl194034 				rval = 0;
   2344   5524  yl194034 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2345   5524  yl194034 				    "st_get_conf_from_tape_drive(): "
   2346   5524  yl194034 				    "Fixed record size is too large and"
   2347   5524  yl194034 				    "cannot switch to variable record size");
   2348   5524  yl194034 			}
   2349   5524  yl194034 			goto exit;
   2350   5425  yl194034 		}
   2351   5425  yl194034 		tem_dp->options |= ST_VARIABLE;
   2352   5524  yl194034 	} else {
   2353   5628   rralphs 		rval = st_change_block_size(un, 0);
   2354   5524  yl194034 		if (rval == 0) {
   2355   5524  yl194034 			tem_dp->options |= ST_VARIABLE;
   2356   5524  yl194034 			tem_dp->bsize = 0;
   2357   5524  yl194034 		} else if (rval != EACCES) {
   2358   5524  yl194034 			tem_dp->bsize = bsize;
   2359   5524  yl194034 		} else {
   2360   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2361   5524  yl194034 			rval = 1;
   2362   5524  yl194034 			goto exit;
   2363   5524  yl194034 		}
   2364   5425  yl194034 	}
   2365   5425  yl194034 
   2366   5425  yl194034 	/*
   2367   5425  yl194034 	 * If READ BLOCk LIMITS works and upper block size limit is
   2368   5425  yl194034 	 * more than 64K, ST_NO_RECSIZE_LIMIT is supported.
   2369   5425  yl194034 	 */
   2370   5425  yl194034 	blklim = kmem_zalloc(sizeof (struct read_blklim), KM_SLEEP);
   2371   5524  yl194034 	rval = st_read_block_limits(un, blklim);
   2372   5524  yl194034 	if (rval) {
   2373   5425  yl194034 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2374   5425  yl194034 		    "st_get_conf_from_tape_drive(): "
   2375   5425  yl194034 		    "fail to read block limits.\n");
   2376   5524  yl194034 		rval = 0;
   2377   5425  yl194034 		kmem_free(blklim, sizeof (struct read_blklim));
   2378   5524  yl194034 		goto exit;
   2379   5425  yl194034 	}
   2380   5425  yl194034 	maxbsize = (blklim->max_hi << 16) +
   2381   5425  yl194034 	    (blklim->max_mid << 8) + blklim->max_lo;
   2382   5425  yl194034 	if (maxbsize > ST_MAXRECSIZE_VARIABLE) {
   2383   5425  yl194034 		tem_dp->options |= ST_NO_RECSIZE_LIMIT;
   2384   5425  yl194034 	}
   2385   5425  yl194034 	kmem_free(blklim, sizeof (struct read_blklim));
   2386   5425  yl194034 
   2387   5425  yl194034 	/*
   2388   5425  yl194034 	 * Inquiry VPD page 0xb0 to see if the tape drive supports WORM
   2389   5425  yl194034 	 */
   2390   5425  yl194034 	buf = kmem_zalloc(6, KM_SLEEP);
   2391   5425  yl194034 	rval = st_get_special_inquiry(un, 6, buf, 0xb0);
   2392   5524  yl194034 	if (rval) {
   2393   5425  yl194034 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2394   5425  yl194034 		    "st_get_conf_from_tape_drive(): "
   2395   5425  yl194034 		    "fail to read vitial inquiry.\n");
   2396   5524  yl194034 		rval = 0;
   2397   5425  yl194034 		kmem_free(buf, 6);
   2398   5524  yl194034 		goto exit;
   2399   5425  yl194034 	}
   2400   5425  yl194034 	if (buf[4] & 1) {
   2401   5425  yl194034 		tem_dp->options |= ST_WORMABLE;
   2402   5425  yl194034 	}
   2403   5425  yl194034 	kmem_free(buf, 6);
   2404   5425  yl194034 
   2405   5425  yl194034 	/* Assume BSD BSR KNOWS_EOD */
   2406   5425  yl194034 	tem_dp->options |= ST_BSF | ST_BSR | ST_KNOWS_EOD | ST_UNLOADABLE;
   2407   5425  yl194034 	tem_dp->max_rretries = -1;
   2408   5425  yl194034 	tem_dp->max_wretries = -1;
   2409   5425  yl194034 
   2410   5425  yl194034 	/*
   2411   5425  yl194034 	 * Decide the densities supported by tape drive by sending
   2412   5425  yl194034 	 * REPORT DENSITY SUPPORT command.
   2413   5425  yl194034 	 */
   2414   5425  yl194034 	if (st_get_densities_from_tape_drive(un, tem_dp) == 0) {
   2415   5524  yl194034 		goto exit;
   2416   5425  yl194034 	}
   2417   5425  yl194034 
   2418   5425  yl194034 	/*
   2419   5425  yl194034 	 * Decide the timeout values for several commands by sending
   2420   5425  yl194034 	 * REPORT SUPPORTED OPERATION CODES command.
   2421   5425  yl194034 	 */
   2422   5524  yl194034 	rval = st_get_timeout_values_from_tape_drive(un, tem_dp);
   2423   5524  yl194034 	if (rval == 0 || ((rval == 1) && (tem_dp->type == ST_TYPE_INVALID))) {
   2424   5524  yl194034 		goto exit;
   2425   5425  yl194034 	}
   2426   5425  yl194034 
   2427   5425  yl194034 	bcopy(tem_dp, dp, sizeof (struct st_drivetype));
   2428   5524  yl194034 	rval = 1;
   2429   5524  yl194034 
   2430   5524  yl194034 exit:
   2431   5524  yl194034 	un->un_status = KEY_NO_SENSE;
   2432   5425  yl194034 	kmem_free(tem_dp, sizeof (struct st_drivetype));
   2433   5524  yl194034 	return (rval);
   2434   5425  yl194034 }
   2435   5425  yl194034 
   2436   5425  yl194034 static int
   2437   5425  yl194034 st_get_densities_from_tape_drive(struct scsi_tape *un,
   2438   5425  yl194034     struct st_drivetype *dp)
   2439   5425  yl194034 {
   2440   5425  yl194034 	int i, p;
   2441   5425  yl194034 	size_t buflen;
   2442   5425  yl194034 	ushort_t des_len;
   2443   5425  yl194034 	uchar_t *den_header;
   2444   5425  yl194034 	uchar_t num_den;
   2445   5425  yl194034 	uchar_t den[NDENSITIES];
   2446   5425  yl194034 	uchar_t deflt[NDENSITIES];
   2447   5425  yl194034 	struct report_density_desc *den_desc;
   2448   5425  yl194034 
   2449   5425  yl194034 	ST_FUNC(ST_DEVINFO, st_get_densities_from_type_drive);
   2450   5425  yl194034 
   2451   5425  yl194034 	/*
   2452   5425  yl194034 	 * Since we have no idea how many densitiy support entries
   2453   5425  yl194034 	 * will be returned, we send the command firstly assuming
   2454   5425  yl194034 	 * there is only one. Then we can decide the number of
   2455   5425  yl194034 	 * entries by available density support length. If multiple
   2456   5425  yl194034 	 * entries exist, we will resend the command with enough
   2457   5425  yl194034 	 * buffer size.
   2458   5425  yl194034 	 */
   2459   5425  yl194034 	buflen = sizeof (struct report_density_header) +
   2460   5425  yl194034 	    sizeof (struct report_density_desc);
   2461   5425  yl194034 	den_header = kmem_zalloc(buflen, KM_SLEEP);
   2462   5425  yl194034 	if (st_report_density_support(un, den_header, buflen) != 0) {
   2463   5425  yl194034 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2464   5425  yl194034 		    "st_get_conf_from_tape_drive(): fail to report density.\n");
   2465   5425  yl194034 		kmem_free(den_header, buflen);
   2466   5425  yl194034 		return (0);
   2467   5425  yl194034 	}
   2468   5425  yl194034 	des_len =
   2469   5425  yl194034 	    BE_16(((struct report_density_header *)den_header)->ava_dens_len);
   2470   5425  yl194034 	num_den = (des_len - 2) / sizeof (struct report_density_desc);
   2471   5425  yl194034 
   2472   5425  yl194034 	if (num_den > 1) {
   2473   5425  yl194034 		kmem_free(den_header, buflen);
   2474   5425  yl194034 		buflen = sizeof (struct report_density_header) +
   2475   5425  yl194034 		    sizeof (struct report_density_desc) * num_den;
   2476   5425  yl194034 		den_header = kmem_zalloc(buflen, KM_SLEEP);
   2477   5425  yl194034 		if (st_report_density_support(un, den_header, buflen) != 0) {
   2478   5425  yl194034 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2479   5425  yl194034 			    "st_get_conf_from_tape_drive(): "
   2480   5425  yl194034 			    "fail to report density.\n");
   2481   5425  yl194034 			kmem_free(den_header, buflen);
   2482   5425  yl194034 			return (0);
   2483   5425  yl194034 		}
   2484   5425  yl194034 	}
   2485   5425  yl194034 
   2486   5425  yl194034 	den_desc = (struct report_density_desc *)(den_header
   2487   5425  yl194034 	    + sizeof (struct report_density_header));
   2488   5425  yl194034 
   2489   5425  yl194034 	/*
   2490   5425  yl194034 	 * Decide the drive type by assigning organization
   2491   5425  yl194034 	 */
   2492   5425  yl194034 	for (i = 0; i < ST_NUM_MEMBERS(st_vid_dt); i ++) {
   2493   5425  yl194034 		if (strncmp(st_vid_dt[i].vid, (char *)(den_desc->ass_org),
   2494   5425  yl194034 		    8) == 0) {
   2495   5425  yl194034 			dp->type = st_vid_dt[i].type;
   2496   5425  yl194034 			break;
   2497   5425  yl194034 		}
   2498   5425  yl194034 	}
   2499   5425  yl194034 	if (i == ST_NUM_MEMBERS(st_vid_dt)) {
   2500   5425  yl194034 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2501   5425  yl194034 		    "st_get_conf_from_tape_drive(): "
   2502   5425  yl194034 		    "can't find match of assigned ort.\n");
   2503   5425  yl194034 		kmem_free(den_header, buflen);
   2504   5425  yl194034 		return (0);
   2505   5425  yl194034 	}
   2506   5425  yl194034 
   2507   5425  yl194034 	/*
   2508   5425  yl194034 	 * The tape drive may support many tape formats, but the st driver
   2509   5425  yl194034 	 * supports only the four highest densities. Since density code
   2510   5425  yl194034 	 * values are returned by ascending sequence, we start from the
   2511   5425  yl194034 	 * last entry of density support data block descriptor.
   2512   5425  yl194034 	 */
   2513   5425  yl194034 	p = 0;
   2514   5425  yl194034 	den_desc += num_den - 1;
   2515   5425  yl194034 	for (i = 0; i < num_den && p < NDENSITIES; i ++, den_desc --) {
   2516   5425  yl194034 		if ((den_desc->pri_den != 0) && (den_desc->wrtok)) {
   2517   5425  yl194034 			if (p != 0) {
   2518   5425  yl194034 				if (den_desc->pri_den >= den[p - 1]) {
   2519   5425  yl194034 					continue;
   2520   5425  yl194034 				}
   2521   5425  yl194034 			}
   2522   5425  yl194034 			den[p] = den_desc->pri_den;
   2523   5425  yl194034 			deflt[p] = den_desc->deflt;
   2524   5425  yl194034 			p ++;
   2525   5425  yl194034 		}
   2526   5425  yl194034 	}
   2527   5425  yl194034 
   2528   5425  yl194034 	switch (p) {
   2529   5425  yl194034 	case 0:
   2530   5425  yl194034 		bzero(dp->densities, NDENSITIES);
   2531   5425  yl194034 		dp->options |= ST_AUTODEN_OVERRIDE;
   2532   5425  yl194034 		dp->default_density = MT_DENSITY4;
   2533   5425  yl194034 		break;
   2534   5425  yl194034 
   2535   5425  yl194034 	case 1:
   2536   5425  yl194034 		(void) memset(dp->densities, den[0], NDENSITIES);
   2537   5425  yl194034 		dp->options |= ST_AUTODEN_OVERRIDE;
   2538   5425  yl194034 		dp->default_density = MT_DENSITY4;
   2539   5425  yl194034 		break;
   2540   5425  yl194034 
   2541   5425  yl194034 	case 2:
   2542   5425  yl194034 		dp->densities[0] = den[1];
   2543   5425  yl194034 		dp->densities[1] = den[1];
   2544   5425  yl194034 		dp->densities[2] = den[0];
   2545   5425  yl194034 		dp->densities[3] = den[0];
   2546   5425  yl194034 		if (deflt[0]) {
   2547   5425  yl194034 			dp->default_density = MT_DENSITY4;
   2548   5425  yl194034 		} else {
   2549   5425  yl194034 			dp->default_density = MT_DENSITY2;
   2550   5425  yl194034 		}
   2551   5425  yl194034 		break;
   2552   5425  yl194034 
   2553   5425  yl194034 	case 3:
   2554   5425  yl194034 		dp->densities[0] = den[2];
   2555   5425  yl194034 		dp->densities[1] = den[1];
   2556   5425  yl194034 		dp->densities[2] = den[0];
   2557   5425  yl194034 		dp->densities[3] = den[0];
   2558   5425  yl194034 		if (deflt[0]) {
   2559   5425  yl194034 			dp->default_density = MT_DENSITY4;
   2560   5425  yl194034 		} else if (deflt[1]) {
   2561   5425  yl194034 			dp->default_density = MT_DENSITY2;
   2562   5425  yl194034 		} else {
   2563   5425  yl194034 			dp->default_density = MT_DENSITY1;
   2564   5425  yl194034 		}
   2565   5425  yl194034 		break;
   2566   5425  yl194034 
   2567   5425  yl194034 	default:
   2568   5425  yl194034 		for (i = p; i > p - NDENSITIES; i --) {
   2569   5425  yl194034 			dp->densities[i - 1] = den[p - i];
   2570   5425  yl194034 		}
   2571   5425  yl194034 		if (deflt[0]) {
   2572   5425  yl194034 			dp->default_density = MT_DENSITY4;
   2573   5425  yl194034 		} else if (deflt[1]) {
   2574   5425  yl194034 			dp->default_density = MT_DENSITY3;
   2575   5425  yl194034 		} else if (deflt[2]) {
   2576   5425  yl194034 			dp->default_density = MT_DENSITY2;
   2577   5425  yl194034 		} else {
   2578   5425  yl194034 			dp->default_density = MT_DENSITY1;
   2579   5425  yl194034 		}
   2580   5425  yl194034 		break;
   2581   5425  yl194034 	}
   2582   5425  yl194034 
   2583   5425  yl194034 	bzero(dp->mediatype, NDENSITIES);
   2584   5425  yl194034 
   2585   5425  yl194034 	kmem_free(den_header, buflen);
   2586   5425  yl194034 	return (1);
   2587   5425  yl194034 }
   2588   5425  yl194034 
   2589   5425  yl194034 static int
   2590   5425  yl194034 st_get_timeout_values_from_tape_drive(struct scsi_tape *un,
   2591   5425  yl194034     struct st_drivetype *dp)
   2592   5425  yl194034 {
   2593   5425  yl194034 	ushort_t timeout;
   2594   5524  yl194034 	int rval;
   2595   5425  yl194034 
   2596   5425  yl194034 	ST_FUNC(ST_DEVINFO, st_get_timeout_values_from_type_drive);
   2597   5425  yl194034 
   2598   5524  yl194034 	rval = st_get_timeouts_value(un, SCMD_ERASE, &timeout, 0);
   2599   5524  yl194034 	if (rval) {
   2600   5524  yl194034 		if (rval == EACCES) {
   2601   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2602   5524  yl194034 			dp->type = ST_TYPE_INVALID;
   2603   5524  yl194034 			return (1);
   2604   5524  yl194034 		}
   2605   5425  yl194034 		return (0);
   2606   5425  yl194034 	}
   2607   5425  yl194034 	dp->erase_timeout = timeout;
   2608   5425  yl194034 
   2609   5524  yl194034 	rval = st_get_timeouts_value(un, SCMD_READ, &timeout, 0);
   2610   5524  yl194034 	if (rval) {
   2611   5524  yl194034 		if (rval == EACCES) {
   2612   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2613   5524  yl194034 			dp->type = ST_TYPE_INVALID;
   2614   5524  yl194034 			return (1);
   2615   5524  yl194034 		}
   2616   5425  yl194034 		return (0);
   2617   5425  yl194034 	}
   2618   5425  yl194034 	dp->io_timeout = timeout;
   2619   5425  yl194034 
   2620   5524  yl194034 	rval = st_get_timeouts_value(un, SCMD_WRITE, &timeout, 0);
   2621   5524  yl194034 	if (rval) {
   2622   5524  yl194034 		if (rval == EACCES) {
   2623   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2624   5524  yl194034 			dp->type = ST_TYPE_INVALID;
   2625   5524  yl194034 			return (1);
   2626   5524  yl194034 		}
   2627   5425  yl194034 		return (0);
   2628   5425  yl194034 	}
   2629   5425  yl194034 	dp->io_timeout = max(dp->io_timeout, timeout);
   2630   5425  yl194034 
   2631   5524  yl194034 	rval = st_get_timeouts_value(un, SCMD_SPACE, &timeout, 0);
   2632   5524  yl194034 	if (rval) {
   2633   5524  yl194034 		if (rval == EACCES) {
   2634   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2635   5524  yl194034 			dp->type = ST_TYPE_INVALID;
   2636   5524  yl194034 			return (1);
   2637   5524  yl194034 		}
   2638   5425  yl194034 		return (0);
   2639   5425  yl194034 	}
   2640   5425  yl194034 	dp->space_timeout = timeout;
   2641   5425  yl194034 
   2642   5524  yl194034 	rval = st_get_timeouts_value(un, SCMD_LOAD, &timeout, 0);
   2643   5524  yl194034 	if (rval) {
   2644   5524  yl194034 		if (rval == EACCES) {
   2645   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2646   5524  yl194034 			dp->type = ST_TYPE_INVALID;
   2647   5524  yl194034 			return (1);
   2648   5524  yl194034 		}
   2649   5425  yl194034 		return (0);
   2650   5425  yl194034 	}
   2651   5425  yl194034 	dp->load_timeout = timeout;
   2652   5425  yl194034 	dp->unload_timeout = timeout;
   2653   5425  yl194034 
   2654   5524  yl194034 	rval = st_get_timeouts_value(un, SCMD_REWIND, &timeout, 0);
   2655   5524  yl194034 	if (rval) {
   2656   5524  yl194034 		if (rval == EACCES) {
   2657   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2658   5524  yl194034 			dp->type = ST_TYPE_INVALID;
   2659   5524  yl194034 			return (1);
   2660   5524  yl194034 		}
   2661   5425  yl194034 		return (0);
   2662   5425  yl194034 	}
   2663   5425  yl194034 	dp->rewind_timeout = timeout;
   2664   5425  yl194034 
   2665   5524  yl194034 	rval = st_get_timeouts_value(un, SCMD_INQUIRY, &timeout, 0);
   2666   5524  yl194034 	if (rval) {
   2667   5524  yl194034 		if (rval == EACCES) {
   2668   5524  yl194034 			un->un_dp->type = ST_TYPE_INVALID;
   2669   5524  yl194034 			dp->type = ST_TYPE_INVALID;
   2670   5524  yl194034 			return (1);
   2671   5524  yl194034 		}
   2672   5425  yl194034 		return (0);
   2673   5425  yl194034 	}
   2674   5425  yl194034 	dp->non_motion_timeout = timeout;
   2675   5425  yl194034 
   2676   5425  yl194034 	return (1);
   2677   5425  yl194034 }
   2678   5425  yl194034 
   2679   5425  yl194034 static int
   2680   5425  yl194034 st_get_timeouts_value(struct scsi_tape *un, uchar_t option_code,
   2681   5425  yl194034     ushort_t *timeout_value, ushort_t service_action)
   2682   5425  yl194034 {
   2683   5425  yl194034 	uchar_t *timeouts;
   2684   5425  yl194034 	uchar_t *oper;
   2685   5425  yl194034 	uchar_t support;
   2686   5425  yl194034 	uchar_t cdbsize;
   2687   5425  yl194034 	uchar_t ctdp;
   2688   5425  yl194034 	size_t buflen;
   2689   5425  yl194034 	int rval;
   2690   5425  yl194034 
   2691   5425  yl194034 	ST_FUNC(ST_DEVINFO, st_get_timeouts_value);
   2692   5425  yl194034 
   2693   5425  yl194034 	buflen = sizeof (struct one_com_des) +
   2694   5425  yl194034 	    sizeof (struct com_timeout_des);
   2695   5425  yl194034 	oper = kmem_zalloc(buflen, KM_SLEEP);
   2696   5425  yl194034 	rval = st_report_supported_operation(un, oper, option_code,
   2697   5425  yl194034 	    service_action);
   2698   5425  yl194034 
   2699   5524  yl194034 	if (rval) {
   2700   5425  yl194034 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2701   5425  yl194034 		    "st_get_timeouts_value(): "
   2702   5425  yl194034 		    "fail to timeouts value for command %d.\n", option_code);
   2703   5425  yl194034 		kmem_free(oper, buflen);
   2704   5425  yl194034 		return (rval);
   2705   5425  yl194034 	}
   2706   5425  yl194034 
   2707   5425  yl194034 	support = ((struct one_com_des *)oper)->support;
   2708   5425  yl194034 	if ((support != SUPPORT_VALUES_SUPPORT_SCSI) &&
   2709   5425  yl194034 	    (support != SUPPORT_VALUES_SUPPORT_VENDOR)) {
   2710   5425  yl194034 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2711   5425  yl194034 		    "st_get_timeouts_value(): "
   2712   5425  yl194034 		    "command %d is not supported.\n", option_code);
   2713   5425  yl194034 		kmem_free(oper, buflen);
   2714   5524  yl194034 		return (ENOTSUP);
   2715   5425  yl194034 	}
   2716   5425  yl194034 
   2717   5425  yl194034 	ctdp = ((struct one_com_des *)oper)->ctdp;
   2718   5425  yl194034 	if (!ctdp) {
   2719   5425  yl194034 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   2720   5425  yl194034 		    "st_get_timeouts_value(): "
   2721   5425  yl194034 		    "command timeout is not included.\n");
   2722   5425  yl194034 		kmem_free(oper, buflen);
   2723   5524  yl194034 		return (ENOTSUP);
   2724   5425  yl194034 	}
   2725   5425  yl194034 
   2726   5425  yl194034 	cdbsize = BE_16(((struct one_com_des *)oper)->cdb_size);
   2727   5425  yl194034 	timeouts = (uchar_t *)(oper + cdbsize + 4);
   2728   5425  yl194034 
   2729   5425  yl194034 	/*
   2730   5425  yl194034 	 * Timeout value in seconds is 4 bytes, but we only support the lower 2
   2731   5425  yl194034 	 * bytes. If the higher 2 bytes are not zero, the timeout value is set
   2732   5425  yl194034 	 * to 0xFFFF.
   2733   5425  yl194034 	 */
   2734   5425  yl194034 	if (*(timeouts + 8) != 0 || *(timeouts + 9) != 0) {
   2735   5425  yl194034 		*timeout_value = USHRT_MAX;
   2736   5425  yl194034 	} else {
   2737   5425  yl194034 		*timeout_value = ((*(timeouts + 10)) << 8) |
   2738   5425  yl194034 		    (*(timeouts + 11));
   2739   5425  yl194034 	}
   2740   5425  yl194034 
   2741   5425  yl194034 	kmem_free(oper, buflen);
   2742   5425  yl194034 	return (0);
   2743   5425  yl194034 }
   2744   5425  yl194034 
   2745   5425  yl194034 static int
   2746      0    stevel st_get_default_conf(struct scsi_tape *un, char *vidpid, struct st_drivetype *dp)
   2747      0    stevel {
   2748      0    stevel 	int i;
   2749   4549   rralphs 
   2750   4549   rralphs 	ST_FUNC(ST_DEVINFO, st_get_default_conf);
   2751      0    stevel 
   2752      0    stevel 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
   2753      0    stevel 	    "st_get_default_conf(): making drivetype from INQ cmd\n");
   2754      0    stevel 
   2755      0    stevel 	/*
   2756      0    stevel 	 * Make up a name
   2757      0    stevel 	 */
   2758      0    stevel 	bcopy("Vendor '", dp->name, 8);
   2759      0    stevel 	bcopy(vidpid, &dp->name[8], VIDLEN);
   2760      0    stevel 	bcopy("' Product '", &dp->name[16], 11);
   2761      0    stevel 	bcopy(&vidpid[8], &dp->name[27], PIDLEN);
   2762      0    stevel 	dp->name[ST_NAMESIZE - 2] = '\'';
   2763      0    stevel 	dp->name[ST_NAMESIZE - 1] = '\0';
   2764      0    stevel 	dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1));
   2765      0    stevel 	(void) strncpy(dp->vid, ST_INQUIRY->inq_vid, dp->length);
   2766      0    stevel 	/*
   2767      0    stevel 	 * 'clean' vendor and product strings of non-printing chars
   2768      0    stevel 	 */
   2769      0    stevel 	for (i = 0; i < ST_NAMESIZE - 2; i++) {
   2770      0    stevel 		if (dp->name[i] < ' ' || dp->name[i] > '~') {
   2771      0    stevel 			dp->name[i] = '.';
   2772      0    stevel 		}
   2773      0    stevel 	}
   2774      0    stevel 	dp->type = ST_TYPE_INVALID;
   2775      0    stevel 	dp->options |= (ST_DYNAMIC | ST_UNLOADABLE | ST_MODE_SEL_COMP);
   2776      0    stevel 
   2777      0    stevel 	return (1); /* Can Not Fail */
   2778      0    stevel }
   2779      0    stevel 
   2780      0    stevel /*
   2781      0    stevel  * Regular Unix Entry points
   2782      0    stevel  */
   2783      0    stevel 
   2784      0    stevel 
   2785      0    stevel 
   2786      0    stevel /* ARGSUSED */
   2787      0    stevel static int
   2788      0    stevel st_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
   2789      0    stevel {
   2790      0    stevel 	dev_t dev = *dev_p;
   2791      0    stevel 	int rval = 0;
   2792      0    stevel 
   2793      0    stevel 	GET_SOFT_STATE(dev);
   2794   4549   rralphs 
   2795   4549   rralphs 	ST_ENTR(ST_DEVINFO, st_open);
   2796      0    stevel 
   2797      0    stevel 	/*
   2798      0    stevel 	 * validate that we are addressing a sensible unit
   2799      0    stevel 	 */
   2800      0    stevel 	mutex_enter(ST_MUTEX);
   2801      0    stevel 
   2802      0    stevel 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
   2803      0    stevel 	    "st_open(node = %s dev = 0x%lx, flag = %d, otyp = %d)\n",
   2804      0    stevel 	    st_dev_name(dev), *dev_p, flag, otyp);
   2805      0    stevel 
   2806      0    stevel 	/*
   2807      0    stevel 	 * All device accesss go thru st_strategy() where we check
   2808      0    stevel 	 * suspend status
   2809      0    stevel 	 */
   2810      0    stevel 
   2811      0    stevel 	if (!un->un_attached) {
   2812      0    stevel 		st_known_tape_type(un);
   2813      0    stevel 		if (!un->un_attached) {
   2814      0    stevel 			rval = ENXIO;
   2815      0    stevel 			goto exit;
   2816      0    stevel 		}
   2817      0    stevel 
   2818      0    stevel 	}
   2819      0    stevel 
   2820      0    stevel 	/*
   2821      0    stevel 	 * Check for the case of the tape in the middle of closing.
   2822      0    stevel 	 * This isn't simply a check of the current state, because
   2823      0    stevel 	 * we could be in state of sensing with the previous state
   2824      0    stevel 	 * that of closing.
   2825      0    stevel 	 *
   2826      0    stevel 	 * And don't allow multiple opens.
   2827      0    stevel 	 */
   2828      0    stevel 	if (!(flag & (FNDELAY | FNONBLOCK)) && IS_CLOSING(un)) {
   2829      0    stevel 		un->un_laststate = un->un_state;
   2830      0    stevel 		un->un_state = ST_STATE_CLOSE_PENDING_OPEN;
   2831      0    stevel 		while (IS_CLOSING(un) ||
   2832      0    stevel 		    un->un_state == ST_STATE_CLOSE_PENDING_OPEN) {
   2833      0    stevel 			if (cv_wait_sig(&un->un_clscv, ST_MUTEX) == 0) {
   2834      0    stevel 				rval = EINTR;
   2835      0    stevel 				un->un_state = un->un_laststate;
   2836      0    stevel 				goto exit;
   2837      0    stevel 			}
   2838      0    stevel 		}
   2839      0    stevel 	} else if (un->un_state != ST_STATE_CLOSED) {
   2840      0    stevel 		rval = EBUSY;
   2841      0    stevel 		goto busy;
   2842      0    stevel 	}
   2843      0    stevel 
   2844      0    stevel 	/*
   2845      0    stevel 	 * record current dev
   2846      0    stevel 	 */
   2847      0    stevel 	un->un_dev = dev;
   2848      0    stevel 	un->un_oflags = flag;	/* save for use in st_tape_init() */
   2849      0    stevel 	un->un_errno = 0;	/* no errors yet */
   2850      0    stevel 	un->un_restore_pos = 0;
   2851      0    stevel 	un->un_rqs_state = 0;
   2852      0    stevel 
   2853      0    stevel 	/*
   2854      0    stevel 	 * If we are opening O_NDELAY, or O_NONBLOCK, we don't check for
   2855      0    stevel 	 * anything, leave internal states alone, if fileno >= 0
   2856      0    stevel 	 */
   2857      0    stevel 	if (flag & (FNDELAY | FNONBLOCK)) {
   2858   4549   rralphs 		switch (un->un_pos.pmode) {
   2859   4549   rralphs 
   2860   4549   rralphs 		case invalid:
   2861      0    stevel 			un->un_state = ST_STATE_OFFLINE;
   2862   4549   rralphs 			break;
   2863   4549   rralphs 
   2864   4549   rralphs 		case legacy:
   2865   4549   rralphs 			/*
   2866   4549   rralphs 			 * If position is anything other than rewound.
   2867   4549   rralphs 			 */
   2868   4549   rralphs 			if (un->un_pos.fileno != 0 || un->un_pos.blkno != 0) {
   2869   4549   rralphs 				/*
   2870   4549   rralphs 				 * set un_read_only/write-protect status.
   2871   4549   rralphs 				 *
   2872   4549   rralphs 				 * If the tape is not bot we can assume
   2873   4549   rralphs 				 * that mspl->wp_status is set properly.
   2874   4549   rralphs 				 * else
   2875   4549   rralphs 				 * we need to do a mode sense/Tur once
   2876   4549   rralphs 				 * again to get the actual tape status.(since
   2877   4549   rralphs 				 * user might have replaced the tape)
   2878   4549   rralphs 				 * Hence make the st state OFFLINE so that
   2879   4549   rralphs 				 * we re-intialize the tape once again.
   2880   4549   rralphs 				 */
   2881   4549   rralphs 				un->un_read_only =
   2882   4549   rralphs 				    (un->un_oflags & FWRITE) ? RDWR : RDONLY;
   2883   4549   rralphs 				un->un_state = ST_STATE_OPEN_PENDING_IO;
   2884   4549   rralphs 			} else {
   2885   4549   rralphs 				un->un_state = ST_STATE_OFFLINE;
   2886   4549   rralphs 			}
   2887   4549   rralphs 			break;
   2888   4549   rralphs 		case logical:
   2889   4549   rralphs 			if (un->un_pos.lgclblkno == 0) {
   2890   4549   rralphs 				un->un_state = ST_STATE_OFFLINE;
   2891   4549   rralphs 			} else {
   2892   4549   rralphs 				un->un_read_only =
   2893   5628   rralphs 				    (un->un_oflags & FWRITE) ? RDWR : RDONLY;
   2894   4549   rralphs 				un->un_state = ST_STATE_OPEN_PENDING_IO;
   2895   4549   rralphs 			}
   2896   4549   rralphs 			break;
   2897      0    stevel 		}
   2898      0    stevel 		rval = 0;
   2899      0    stevel 	} else {
   2900      0    stevel 		/*
   2901   1697   rralphs 		 * Not opening O_NDELAY.
   2902      0    stevel 		 */
   2903      0    stevel 		un->un_state = ST_STATE_OPENING;
   2904      0    stevel 
   2905   5077  bz211116 		/*
   2906   5077  bz211116 		 * Clear error entry stack
   2907   5077  bz211116 		 */
   2908   5077  bz211116 		st_empty_error_stack(un);
   2909   5077  bz211116 
   2910   5628   rralphs 		rval = st_tape_init(un);
   2911   2537   rralphs 		if ((rval == EACCES) && (un->un_read_only & WORM)) {
   2912   2537   rralphs 			un->un_state = ST_STATE_OPEN_PENDING_IO;
   2913   2537   rralphs 			rval = 0; /* so open doesn't fail */
   2914   2537   rralphs 		} else if (rval) {
   2915      0    stevel 			/*
   2916   1697   rralphs 			 * Release the tape unit, if reserved and not
   2917   1697   rralphs 			 * preserve reserve.
   2918   1697   rralphs 			 */
   2919   1697   rralphs 			if ((un->un_rsvd_status &
   2920   1697   rralphs 			    (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) {
   2921   5628   rralphs 				(void) st_reserve_release(un, ST_RELEASE,
   2922   5628   rralphs 				    st_uscsi_cmd);
   2923      0    stevel 			}
   2924      0    stevel 		} else {
   2925      0    stevel 			un->un_state = ST_STATE_OPEN_PENDING_IO;
   2926      0    stevel 		}
   2927      0    stevel 	}
   2928      0    stevel 
   2929      0    stevel exit:
   2930      0    stevel 	/*
   2931      0    stevel 	 * we don't want any uninvited guests scrogging our data when we're
   2932      0    stevel 	 * busy with something, so for successful opens or failed opens
   2933      0    stevel 	 * (except for EBUSY), reset these counters and state appropriately.
   2934      0    stevel 	 */
   2935      0    stevel 	if (rval != EBUSY) {
   2936      0    stevel 		if (rval) {
   2937      0    stevel 			un->un_state = ST_STATE_CLOSED;
   2938      0    stevel 		}
   2939      0    stevel 		un->un_err_resid = 0;
   2940      0    stevel 		un->un_retry_ct = 0;
   2941      0    stevel 	}
   2942      0    stevel busy:
   2943      0    stevel 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
   2944      0    stevel 	    "st_open: return val = %x, state = %d\n", rval, un->un_state);
   2945      0    stevel 	mutex_exit(ST_MUTEX);
   2946      0    stevel 	return (rval);
   2947      0    stevel 
   2948      0    stevel }
   2949      0    stevel 
   2950      0    stevel static int
   2951   5628   rralphs st_tape_init(struct scsi_tape *un)
   2952      0    stevel {
   2953      0    stevel 	int err;
   2954      0    stevel 	int rval = 0;
   2955      0    stevel 
   2956   4549   rralphs 	ST_FUNC(ST_DEVINFO, st_tape_init);
   2957      0    stevel 
   2958      0    stevel 	ASSERT(mutex_owned(ST_MUTEX));
   2959      0    stevel 
   2960      0    stevel 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
   2961   5628   rralphs 	    "st_tape_init(un = 0x%p, oflags = %d)\n", (void*)un, un->un_oflags);
   2962      0    stevel 
   2963      0    stevel 	/*
   2964      0    stevel 	 * Clean up after any errors left by 'last' close.
   2965      0    stevel 	 * This also handles the case of the initial open.
   2966      0    stevel 	 */
   2967      0    stevel 	if (un->un_state != ST_STATE_INITIALIZING) {
   2968      0    stevel 		un->un_laststate = un->un_state;
   2969      0    stevel 		un->un_state = ST_STATE_OPENING;
   2970      0    stevel 	}
   2971      0    stevel 
   2972      0    stevel 	un->un_kbytes_xferred = 0;
   2973      0    stevel 
   2974      0    stevel 	/*
   2975      0    stevel 	 * do a throw away TUR to clear check condition
   2976      0    stevel 	 */
   2977   5628   rralphs 	err = st_cmd(un, SCMD_TEST_UNIT_READY, 0, SYNC_CMD);
   2978      0    stevel 
   2979      0    stevel 	/*
   2980      0    stevel 	 * If test unit ready fails because the drive is reserved
   2981      0    stevel 	 * by another host fail the open for no access.
   2982      0    stevel 	 */
   2983      0    stevel 	if (err) {
   2984      0    stevel 		if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) {
   2985      0    stevel 			un->un_state = ST_STATE_CLOSED;
   2986      0    stevel 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
   2987   1697   rralphs 			    "st_tape_init: RESERVATION CONFLICT\n");
   2988      0    stevel 			rval = EACCES;
   2989      0    stevel 			goto exit;
   2990   6365  bz211116 		} else if ((un->un_rsvd_status &
   2991   6365  bz211116 		    ST_APPLICATION_RESERVATIONS) != 0) {
   2992   6365  bz211116 			if ((ST_RQSENSE != NULL) &&
   2993   6365  bz211116 			    (ST_RQSENSE->es_add_code == 0x2a &&
   2994   6365  bz211116 			    ST_RQSENSE->es_qual_code == 0x03)) {
   2995   6365  bz211116 				un->un_state = ST_STATE_CLOSED;
   2996   6365  bz211116 				rval = EACCES;
   2997   6365  bz211116 				goto exit;
   2998   6365  bz211116 			}
   2999      0    stevel 		}
   3000      0    stevel 	}
   3001      0    stevel 
   3002      0    stevel 	/*
   3003   5524  yl194034 	 * Tape self identification could fail if the tape drive is used by
   3004   5524  yl194034 	 * another host during attach time. We try to get the tape type
   3005   5524  yl194034 	 * again. This is also applied to any posponed configuration methods.
   3006   5524  yl194034 	 */
   3007   5524  yl194034 	if (un->un_dp->type == ST_TYPE_INVALID) {
   3008   5524  yl194034 		un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE;
   3009   5524  yl194034 		st_known_tape_type(un);
   3010   5524  yl194034 	}
   3011   5524  yl194034 
   3012   5524  yl194034 	/*
   3013   5524  yl194034 	 * If the tape type is still invalid, try to determine the generic
   3014   5524  yl194034 	 * configuration.
   3015      0    stevel 	 */
   3016      0    stevel 	if (un->un_dp->type == ST_TYPE_INVALID) {
   3017   5628   rralphs 		rval = st_determine_generic(un);
   3018   1697   rralphs 		if (rval) {
   3019   1697   rralphs 			if (rval != EACCES) {
   3020   1697   rralphs 				rval = EIO;
   3021   1697   rralphs 			}
   3022      0    stevel 			un->un_state = ST_STATE_CLOSED;
   3023      0    stevel 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
   3024   1697   rralphs 			    "st_tape_init: %s invalid type\n",
   3025   1697   rralphs 			    rval == EACCES ? "EACCES" : "EIO");
   3026      0    stevel 			goto exit;
   3027      0    stevel 		}
   3028      0    stevel 		/*
   3029      0    stevel 		 * If this is a Unknown Type drive,
   3030      0    stevel 		 * Use the READ BLOCK LIMITS to determine if
   3031      0    stevel 		 * allow large xfer is approprate if not globally
   3032      0    stevel 		 * disabled with st_allow_large_xfer.
   3033      0    stevel 		 */
   3034      0    stevel 		un->un_allow_large_xfer = (uchar_t)st_allow_large_xfer;
   3035      0    stevel 	} else {
   3036      0    stevel 
   3037      0    stevel 		/*
   3038      0    stevel 		 * If we allow_large_xfer (ie >64k) and have not yet found out
   3039      0    stevel 		 * the max block size supported by