xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/st.c (revision 6954e8c5cca918ccea54c08370e374fc0db429da)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * SCSI	 SCSA-compliant and not-so-DDI-compliant Tape Driver
31  */
32 
33 #if defined(lint) && !defined(DEBUG)
34 #define	DEBUG	1
35 #endif
36 
37 #include <sys/modctl.h>
38 #include <sys/scsi/scsi.h>
39 #include <sys/mtio.h>
40 #include <sys/scsi/targets/stdef.h>
41 #include <sys/file.h>
42 #include <sys/stat.h>
43 #include <sys/kstat.h>
44 #include <sys/ddidmareq.h>
45 #include <sys/ddi.h>
46 #include <sys/sunddi.h>
47 
48 #define	IOSP	KSTAT_IO_PTR(un->un_stats)
49 /*
50  * stats maintained only for reads/writes as commands
51  * like rewind etc skew the wait/busy times
52  */
53 #define	IS_RW(bp) 	((bp)->b_bcount > 0)
54 #define	ST_DO_KSTATS(bp, kstat_function) \
55 	if ((bp != un->un_sbufp) && un->un_stats && IS_RW(bp)) { \
56 		kstat_function(IOSP); \
57 	}
58 
59 #define	ST_DO_ERRSTATS(un, x)  \
60 	if (un->un_errstats) { \
61 		struct st_errstats *stp; \
62 		stp = (struct st_errstats *)un->un_errstats->ks_data; \
63 		stp->x.value.ul++; \
64 	}
65 
66 #define	FILL_SCSI1_LUN(devp, pkt) 					\
67 	if ((devp)->sd_inq->inq_ansi == 0x1) {				\
68 		int _lun;						\
69 		_lun = ddi_prop_get_int(DDI_DEV_T_ANY, (devp)->sd_dev,	\
70 		    DDI_PROP_DONTPASS, SCSI_ADDR_PROP_LUN, 0);		\
71 		if (_lun > 0) {						\
72 			((union scsi_cdb *)(pkt)->pkt_cdbp)->scc_lun =	\
73 			    _lun;					\
74 		}							\
75 	}
76 
77 /*
78  * get an available contig mem header, cp.
79  * when big_enough is true, we will return NULL, if no big enough
80  * contig mem is found.
81  * when big_enough is false, we will try to find cp containing big
82  * enough contig mem. if not found, we will ruturn the last cp available.
83  *
84  * used by st_get_contig_mem()
85  */
86 #define	ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough) {		\
87 	struct contig_mem *tmp_cp = NULL;				\
88 	for ((cp) = (un)->un_contig_mem;				\
89 	    (cp) != NULL;						\
90 	    tmp_cp = (cp), (cp) = (cp)->cm_next) { 			\
91 		if (((cp)->cm_len >= (len)) || 				\
92 		    (!(big_enough) && ((cp)->cm_next == NULL))) { 	\
93 			if (tmp_cp == NULL) { 				\
94 				(un)->un_contig_mem = (cp)->cm_next; 	\
95 			} else { 					\
96 				tmp_cp->cm_next = (cp)->cm_next; 	\
97 			} 						\
98 			(cp)->cm_next = NULL; 				\
99 			(un)->un_contig_mem_available_num--; 		\
100 			break; 						\
101 		} 							\
102 	} 								\
103 }
104 
105 #define	ST_NUM_MEMBERS(array)	(sizeof (array) / sizeof (array[0]))
106 
107 /*
108  * Global External Data Definitions
109  */
110 extern struct scsi_key_strings scsi_cmds[];
111 extern uchar_t	scsi_cdb_size[];
112 
113 /*
114  * Local Static Data
115  */
116 static void *st_state;
117 static char *st_label = "st";
118 
119 #if defined(__i386) || defined(__amd64)
120 /*
121  * We need to use below DMA attr to alloc physically contiguous
122  * memory to do I/O in big block size
123  */
124 static ddi_dma_attr_t st_contig_mem_dma_attr = {
125 	DMA_ATTR_V0,    /* version number */
126 	0x0,		/* lowest usable address */
127 	0xFFFFFFFFull,  /* high DMA address range */
128 	0xFFFFFFFFull,  /* DMA counter register */
129 	1,		/* DMA address alignment */
130 	1,		/* DMA burstsizes */
131 	1,		/* min effective DMA size */
132 	0xFFFFFFFFull,  /* max DMA xfer size */
133 	0xFFFFFFFFull,  /* segment boundary */
134 	1,		/* s/g list length */
135 	1,		/* granularity of device */
136 	0		/* DMA transfer flags */
137 };
138 
139 static ddi_device_acc_attr_t st_acc_attr = {
140 	DDI_DEVICE_ATTR_V0,
141 	DDI_NEVERSWAP_ACC,
142 	DDI_STRICTORDER_ACC
143 };
144 
145 /* set limitation for the number of contig_mem */
146 static int st_max_contig_mem_num = ST_MAX_CONTIG_MEM_NUM;
147 #endif
148 
149 /*
150  * Tunable parameters
151  *
152  * DISCLAIMER
153  * ----------
154  * These parameters are intended for use only in system testing; if you use
155  * them in production systems, you do so at your own risk. Altering any
156  * variable not listed below may cause unpredictable system behavior.
157  *
158  * st_check_media_time
159  *
160  *   Three second state check
161  *
162  * st_allow_large_xfer
163  *
164  *   Gated with ST_NO_RECSIZE_LIMIT
165  *
166  *   0 - Transfers larger than 64KB will not be allowed
167  *       regardless of the setting of ST_NO_RECSIZE_LIMIT
168  *   1 - Transfers larger than 64KB will be allowed
169  *       if ST_NO_RECSIZE_LIMIT is TRUE for the drive
170  *
171  * st_report_soft_errors_on_close
172  *
173  *  Gated with ST_SOFT_ERROR_REPORTING
174  *
175  *  0 - Errors will not be reported on close regardless
176  *      of the setting of ST_SOFT_ERROR_REPORTING
177  *
178  *  1 - Errors will be reported on close if
179  *      ST_SOFT_ERROR_REPORTING is TRUE for the drive
180  */
181 static int st_selection_retry_count = ST_SEL_RETRY_COUNT;
182 static int st_retry_count	= ST_RETRY_COUNT;
183 
184 static int st_io_time		= ST_IO_TIME;
185 static int st_long_timeout_x	= ST_LONG_TIMEOUT_X;
186 
187 static int st_space_time	= ST_SPACE_TIME;
188 static int st_long_space_time_x	= ST_LONG_SPACE_TIME_X;
189 
190 static int st_error_level	= SCSI_ERR_RETRYABLE;
191 static int st_check_media_time	= 3000000;	/* 3 Second State Check */
192 
193 static int st_max_throttle	= ST_MAX_THROTTLE;
194 
195 static clock_t st_wait_cmds_complete = ST_WAIT_CMDS_COMPLETE;
196 
197 static int st_allow_large_xfer = 1;
198 static int st_report_soft_errors_on_close = 1;
199 
200 /*
201  * End of tunable parameters list
202  */
203 
204 
205 
206 /*
207  * Asynchronous I/O and persistent errors, refer to PSARC/1995/228
208  *
209  * Asynchronous I/O's main offering is that it is a non-blocking way to do
210  * reads and writes.  The driver will queue up all the requests it gets and
211  * have them ready to transport to the HBA.  Unfortunately, we cannot always
212  * just ship the I/O requests to the HBA, as there errors and exceptions
213  * that may happen when we don't want the HBA to continue.  Therein comes
214  * the flush-on-errors capability.  If the HBA supports it, then st will
215  * send in st_max_throttle I/O requests at the same time.
216  *
217  * Persistent errors : This was also reasonably simple.  In the interrupt
218  * routines, if there was an error or exception (FM, LEOT, media error,
219  * transport error), the persistent error bits are set and shuts everything
220  * down, but setting the throttle to zero.  If we hit and exception in the
221  * HBA, and flush-on-errors were set, we wait for all outstanding I/O's to
222  * come back (with CMD_ABORTED), then flush all bp's in the wait queue with
223  * the appropriate error, and this will preserve order. Of course, depending
224  * on the exception we have to show a zero read or write before we show
225  * errors back to the application.
226  */
227 
228 extern const int st_ndrivetypes;	/* defined in st_conf.c */
229 extern const struct st_drivetype st_drivetypes[];
230 
231 #ifdef STDEBUG
232 static int st_soft_error_report_debug = 0;
233 volatile int st_debug = 0;
234 #endif
235 
236 #define	ST_MT02_NAME	"Emulex  MT02 QIC-11/24  "
237 
238 static const struct driver_minor_data {
239 	char	*name;
240 	int	minor;
241 } st_minor_data[] = {
242 	/*
243 	 * The top 4 entries are for the default densities,
244 	 * don't alter their position.
245 	 */
246 	{"",	0},
247 	{"n",	MT_NOREWIND},
248 	{"b",	MT_BSD},
249 	{"bn",	MT_NOREWIND | MT_BSD},
250 	{"l",	MT_DENSITY1},
251 	{"m",	MT_DENSITY2},
252 	{"h",	MT_DENSITY3},
253 	{"c",	MT_DENSITY4},
254 	{"u",	MT_DENSITY4},
255 	{"ln",	MT_DENSITY1 | MT_NOREWIND},
256 	{"mn",	MT_DENSITY2 | MT_NOREWIND},
257 	{"hn",	MT_DENSITY3 | MT_NOREWIND},
258 	{"cn",	MT_DENSITY4 | MT_NOREWIND},
259 	{"un",	MT_DENSITY4 | MT_NOREWIND},
260 	{"lb",	MT_DENSITY1 | MT_BSD},
261 	{"mb",	MT_DENSITY2 | MT_BSD},
262 	{"hb",	MT_DENSITY3 | MT_BSD},
263 	{"cb",	MT_DENSITY4 | MT_BSD},
264 	{"ub",	MT_DENSITY4 | MT_BSD},
265 	{"lbn",	MT_DENSITY1 | MT_NOREWIND | MT_BSD},
266 	{"mbn",	MT_DENSITY2 | MT_NOREWIND | MT_BSD},
267 	{"hbn",	MT_DENSITY3 | MT_NOREWIND | MT_BSD},
268 	{"cbn",	MT_DENSITY4 | MT_NOREWIND | MT_BSD},
269 	{"ubn",	MT_DENSITY4 | MT_NOREWIND | MT_BSD}
270 };
271 
272 /* strings used in many debug and warning messages */
273 static const char wr_str[]  = "write";
274 static const char rd_str[]  = "read";
275 static const char wrg_str[] = "writing";
276 static const char rdg_str[] = "reading";
277 
278 /* default density offsets in the table above */
279 #define	DEF_BLANK	0
280 #define	DEF_NOREWIND	1
281 #define	DEF_BSD		2
282 #define	DEF_BSD_NR	3
283 
284 /* Sense Key, ASC/ASCQ for which tape ejection is needed */
285 
286 static struct tape_failure_code {
287 	uchar_t key;
288 	uchar_t add_code;
289 	uchar_t qual_code;
290 } st_tape_failure_code[] = {
291 	{ KEY_HARDWARE_ERROR, 0x15, 0x01},
292 	{ KEY_HARDWARE_ERROR, 0x44, 0x00},
293 	{ KEY_HARDWARE_ERROR, 0x53, 0x00},
294 	{ KEY_HARDWARE_ERROR, 0x53, 0x01},
295 	{ KEY_NOT_READY, 0x53, 0x00},
296 	{ 0xff}
297 };
298 
299 /*  clean bit position and mask */
300 
301 static struct cln_bit_position {
302 	ushort_t cln_bit_byte;
303 	uchar_t cln_bit_mask;
304 } st_cln_bit_position[] = {
305 	{ 21, 0x08},
306 	{ 70, 0xc0},
307 	{ 18, 0x81}  /* 80 bit indicates in bit mode, 1 bit clean light is on */
308 };
309 
310 /*
311  * architecture dependent allocation restrictions. For x86, we'll set
312  * dma_attr_addr_hi to st_max_phys_addr and dma_attr_sgllen to
313  * st_sgl_size during _init().
314  */
315 #if defined(__sparc)
316 static ddi_dma_attr_t st_alloc_attr = {
317 	DMA_ATTR_V0,	/* version number */
318 	0x0,		/* lowest usable address */
319 	0xFFFFFFFFull,	/* high DMA address range */
320 	0xFFFFFFFFull,	/* DMA counter register */
321 	1,		/* DMA address alignment */
322 	1,		/* DMA burstsizes */
323 	1,		/* min effective DMA size */
324 	0xFFFFFFFFull,	/* max DMA xfer size */
325 	0xFFFFFFFFull,	/* segment boundary */
326 	1,		/* s/g list length */
327 	512,		/* granularity of device */
328 	0		/* DMA transfer flags */
329 };
330 #elif defined(__x86)
331 static ddi_dma_attr_t st_alloc_attr = {
332 	DMA_ATTR_V0,	/* version number */
333 	0x0,		/* lowest usable address */
334 	0x0,		/* high DMA address range [set in _init()] */
335 	0xFFFFull,	/* DMA counter register */
336 	512,		/* DMA address alignment */
337 	1,		/* DMA burstsizes */
338 	1,		/* min effective DMA size */
339 	0xFFFFFFFFull,	/* max DMA xfer size */
340 	0xFFFFFFFFull,  /* segment boundary */
341 	0,		/* s/g list length */
342 	512,		/* granularity of device [set in _init()] */
343 	0		/* DMA transfer flags */
344 };
345 uint64_t st_max_phys_addr = 0xFFFFFFFFull;
346 int st_sgl_size = 0xF;
347 
348 #endif
349 
350 /*
351  * Configuration Data:
352  *
353  * Device driver ops vector
354  */
355 static int st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p);
356 static int st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p);
357 static int st_read(dev_t  dev,  struct   uio   *uio_p,   cred_t *cred_p);
358 static int st_write(dev_t  dev,  struct  uio   *uio_p,   cred_t *cred_p);
359 static int st_open(dev_t  *devp,  int  flag,  int  otyp,  cred_t *cred_p);
360 static int st_close(dev_t  dev,  int  flag,  int  otyp,  cred_t *cred_p);
361 static int st_strategy(struct buf *bp);
362 static int st_ioctl(dev_t dev, int cmd, intptr_t arg, int  flag,
363 	cred_t *cred_p, int *rval_p);
364 extern int nulldev(), nodev();
365 
366 static struct cb_ops st_cb_ops = {
367 	st_open,			/* open */
368 	st_close,		/* close */
369 	st_strategy,		/* strategy */
370 	nodev,			/* print */
371 	nodev,			/* dump */
372 	st_read,		/* read */
373 	st_write,		/* write */
374 	st_ioctl,		/* ioctl */
375 	nodev,			/* devmap */
376 	nodev,			/* mmap */
377 	nodev,			/* segmap */
378 	nochpoll,		/* poll */
379 	ddi_prop_op,		/* cb_prop_op */
380 	0,			/* streamtab  */
381 	D_64BIT | D_MP | D_NEW | D_HOTPLUG,	/* Driver compatibility flag */
382 	CB_REV,			/* cb_rev */
383 	st_aread, 		/* async I/O read entry point */
384 	st_awrite		/* async I/O write entry point */
385 
386 };
387 
388 static int stinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
389 		void **result);
390 static int st_probe(dev_info_t *dev);
391 static int st_attach(dev_info_t *dev, ddi_attach_cmd_t cmd);
392 static int st_detach(dev_info_t *dev, ddi_detach_cmd_t cmd);
393 
394 static struct dev_ops st_ops = {
395 	DEVO_REV,		/* devo_rev, */
396 	0,			/* refcnt  */
397 	stinfo,			/* info */
398 	nulldev,		/* identify */
399 	st_probe,		/* probe */
400 	st_attach,		/* attach */
401 	st_detach,		/* detach */
402 	nodev,			/* reset */
403 	&st_cb_ops,		/* driver operations */
404 	(struct bus_ops *)0,	/* bus operations */
405 	nulldev			/* power */
406 };
407 
408 /*
409  * Local Function Declarations
410  */
411 static void st_clean_print(dev_info_t *dev, char *label, uint_t level,
412 	char *title, char *data, int len);
413 static int st_doattach(struct scsi_device *devp, int (*canwait)());
414 static void st_known_tape_type(struct scsi_tape *un);
415 static int st_get_conf_from_st_dot_conf(struct scsi_tape *, char *,
416     struct st_drivetype *);
417 static int st_get_conf_from_st_conf_dot_c(struct scsi_tape *, char *,
418     struct st_drivetype *);
419 static int st_get_default_conf(struct scsi_tape *, char *,
420     struct st_drivetype *);
421 static int st_rw(dev_t dev, struct uio *uio, int flag);
422 static int st_arw(dev_t dev, struct aio_req *aio, int flag);
423 static int st_find_eom(dev_t dev);
424 static int st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag);
425 static int st_ioctl_cmd(dev_t dev, struct uscsi_cmd *,
426 	enum uio_seg, enum uio_seg, enum uio_seg);
427 static int st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag);
428 static void st_start(struct scsi_tape *un);
429 static int st_handle_start_busy(struct scsi_tape *un, struct buf *bp,
430     clock_t timeout_interval);
431 static int st_handle_intr_busy(struct scsi_tape *un, struct buf *bp,
432     clock_t timeout_interval);
433 static int st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp);
434 static void st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp);
435 static void st_init(struct scsi_tape *un);
436 static void st_make_cmd(struct scsi_tape *un, struct buf *bp,
437     int (*func)(caddr_t));
438 static void st_make_uscsi_cmd(struct scsi_tape *, struct uscsi_cmd *,
439     struct buf *bp, int (*func)(caddr_t));
440 static void st_intr(struct scsi_pkt *pkt);
441 static void st_set_state(struct scsi_tape *un);
442 static void st_test_append(struct buf *bp);
443 static int st_runout(caddr_t);
444 static int st_cmd(dev_t dev, int com, int count, int wait);
445 static int st_set_compression(struct scsi_tape *un);
446 static int st_write_fm(dev_t dev, int wfm);
447 static int st_determine_generic(dev_t dev);
448 static int st_determine_density(dev_t dev, int rw);
449 static int st_get_density(dev_t dev);
450 static int st_set_density(dev_t dev);
451 static int st_loadtape(dev_t dev);
452 static int st_modesense(struct scsi_tape *un);
453 static int st_modeselect(struct scsi_tape *un);
454 static int st_handle_incomplete(struct scsi_tape *un, struct buf *bp);
455 static int st_wrongtapetype(struct scsi_tape *un);
456 static int st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt);
457 static int st_handle_sense(struct scsi_tape *un, struct buf *bp);
458 static int st_handle_autosense(struct scsi_tape *un, struct buf *bp);
459 static int st_decode_sense(struct scsi_tape *un, struct buf *bp, int amt,
460 	struct scsi_status *);
461 static int st_report_soft_errors(dev_t dev, int flag);
462 static void st_delayed_cv_broadcast(void *arg);
463 static int st_check_media(dev_t dev, enum mtio_state state);
464 static int st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp);
465 static void st_intr_restart(void *arg);
466 static void st_start_restart(void *arg);
467 static int st_gen_mode_sense(struct scsi_tape *un, int page,
468     struct seq_mode *page_data, int page_size);
469 static int st_change_block_size(dev_t dev, uint32_t nblksz);
470 static int st_gen_mode_select(struct scsi_tape *un, struct seq_mode *page_data,
471     int page_size);
472 static int st_tape_init(dev_t dev);
473 static void st_flush(struct scsi_tape *un);
474 static void st_set_pe_errno(struct scsi_tape *un);
475 static void st_hba_unflush(struct scsi_tape *un);
476 static void st_turn_pe_on(struct scsi_tape *un);
477 static void st_turn_pe_off(struct scsi_tape *un);
478 static void st_set_pe_flag(struct scsi_tape *un);
479 static void st_clear_pe(struct scsi_tape *un);
480 static void st_wait_for_io(struct scsi_tape *un);
481 static int st_set_devconfig_page(struct scsi_tape *un, int compression_on);
482 static int st_set_datacomp_page(struct scsi_tape *un, int compression_on);
483 static int st_reserve_release(struct scsi_tape *un, int command);
484 static int st_check_cdb_for_need_to_reserve(struct scsi_tape *un, caddr_t cdb);
485 static int st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd,
486     int count);
487 static int st_take_ownership(dev_t dev);
488 static int st_check_asc_ascq(struct scsi_tape *un);
489 static int st_check_clean_bit(dev_t dev);
490 static int st_check_alert_flags(dev_t dev);
491 static int st_check_sequential_clean_bit(dev_t dev);
492 static int st_check_sense_clean_bit(dev_t dev);
493 static int st_clear_unit_attentions(dev_t dev_instance, int max_trys);
494 static void st_calculate_timeouts(struct scsi_tape *un);
495 static writablity st_is_drive_worm(struct scsi_tape *un);
496 static int st_read_attributes(struct scsi_tape *un, uint16_t attribute,
497     caddr_t buf, size_t size);
498 static int st_get_special_inquiry(struct scsi_tape *un, uchar_t size,
499     caddr_t dest, uchar_t page);
500 
501 #if defined(__i386) || defined(__amd64)
502 /*
503  * routines for I/O in big block size
504  */
505 static void st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp);
506 static struct contig_mem *st_get_contig_mem(struct scsi_tape *un, size_t len,
507     int alloc_flags);
508 static int st_bigblk_xfer_done(struct buf *bp);
509 static struct buf *st_get_bigblk_bp(struct buf *bp);
510 #endif
511 
512 /*
513  * error statistics create/update functions
514  */
515 static int st_create_errstats(struct scsi_tape *, int);
516 static void st_uscsi_minphys(struct buf *bp);
517 static int st_validate_tapemarks(struct scsi_tape *un, int fileno, daddr_t bn);
518 
519 #ifdef STDEBUG
520 static void st_debug_cmds(struct scsi_tape *un, int com, int count, int wait);
521 static char *st_dev_name(dev_t dev);
522 #endif /* STDEBUG */
523 
524 #if !defined(lint)
525 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_pkt buf uio scsi_cdb))
526 _NOTE(SCHEME_PROTECTS_DATA("unique per pkt", scsi_extended_sense scsi_status))
527 _NOTE(SCHEME_PROTECTS_DATA("stable data", scsi_device))
528 _NOTE(DATA_READABLE_WITHOUT_LOCK(st_drivetype scsi_address))
529 #endif
530 
531 /*
532  * autoconfiguration routines.
533  */
534 char _depends_on[] = "misc/scsi";
535 
536 static struct modldrv modldrv = {
537 	&mod_driverops,		/* Type of module. This one is a driver */
538 	"SCSI tape Driver %I%", /* Name of the module. */
539 	&st_ops			/* driver ops */
540 };
541 
542 static struct modlinkage modlinkage = {
543 	MODREV_1, &modldrv, NULL
544 };
545 
546 /*
547  * Notes on Post Reset Behavior in the tape driver:
548  *
549  * When the tape drive is opened, the driver  attempts  to make sure that
550  * the tape head is positioned exactly where it was left when it was last
551  * closed  provided  the  medium  is not  changed.  If the tape  drive is
552  * opened in O_NDELAY mode, the repositioning  (if necessary for any loss
553  * of position due to reset) will happen when the first tape operation or
554  * I/O occurs.  The repositioning (if required) may not be possible under
555  * certain situations such as when the device firmware not able to report
556  * the medium  change in the REQUEST  SENSE data  because of a reset or a
557  * misbehaving  bus  not  allowing  the  reposition  to  happen.  In such
558  * extraordinary  situations, where the driver fails to position the head
559  * at its  original  position,  it will fail the open the first  time, to
560  * save the applications from overwriting the data.  All further attempts
561  * to open the tape device will result in the driver  attempting  to load
562  * the  tape at BOT  (beginning  of  tape).  Also a  warning  message  to
563  * indicate  that further  attempts to open the tape device may result in
564  * the tape being  loaded at BOT will be printed on the  console.  If the
565  * tape  device is opened  in  O_NDELAY  mode,  failure  to  restore  the
566  * original tape head  position,  will result in the failure of the first
567  * tape  operation  or I/O,  Further,  the  driver  will  invalidate  its
568  * internal tape position  which will  necessitate  the  applications  to
569  * validate the position by using either a tape  positioning  ioctl (such
570  * as MTREW) or closing and reopening the tape device.
571  *
572  */
573 
574 int
575 _init(void)
576 {
577 	int e;
578 
579 	if (((e = ddi_soft_state_init(&st_state,
580 	    sizeof (struct scsi_tape), ST_MAXUNIT)) != 0)) {
581 		return (e);
582 	}
583 
584 	if ((e = mod_install(&modlinkage)) != 0) {
585 		ddi_soft_state_fini(&st_state);
586 	}
587 
588 #if defined(__x86)
589 	/* set the max physical address for iob allocs on x86 */
590 	st_alloc_attr.dma_attr_addr_hi = st_max_phys_addr;
591 
592 	/*
593 	 * set the sgllen for iob allocs on x86. If this is set less than
594 	 * the number of pages the buffer will take (taking into account
595 	 * alignment), it would force the allocator to try and allocate
596 	 * contiguous pages.
597 	 */
598 	st_alloc_attr.dma_attr_sgllen = st_sgl_size;
599 #endif
600 
601 	return (e);
602 }
603 
604 int
605 _fini(void)
606 {
607 	int e;
608 
609 	if ((e = mod_remove(&modlinkage)) != 0) {
610 		return (e);
611 	}
612 
613 	ddi_soft_state_fini(&st_state);
614 
615 	return (e);
616 }
617 
618 int
619 _info(struct modinfo *modinfop)
620 {
621 	return (mod_info(&modlinkage, modinfop));
622 }
623 
624 
625 static int
626 st_probe(dev_info_t *devi)
627 {
628 	int instance;
629 	struct scsi_device *devp;
630 	int rval;
631 
632 #if !defined(__sparc)
633 	char    *tape_prop;
634 	int	tape_prop_len;
635 #endif
636 
637 	/* If self identifying device */
638 	if (ddi_dev_is_sid(devi) == DDI_SUCCESS) {
639 		return (DDI_PROBE_DONTCARE);
640 	}
641 
642 #if !defined(__sparc)
643 	/*
644 	 * Since some x86 HBAs have devnodes that look like SCSI as
645 	 * far as we can tell but aren't really SCSI (DADK, like mlx)
646 	 * we check for the presence of the "tape" property.
647 	 */
648 	if (ddi_prop_op(DDI_DEV_T_NONE, devi, PROP_LEN_AND_VAL_ALLOC,
649 	    DDI_PROP_CANSLEEP, "tape",
650 	    (caddr_t)&tape_prop, &tape_prop_len) != DDI_PROP_SUCCESS) {
651 		return (DDI_PROBE_FAILURE);
652 	}
653 	if (strncmp(tape_prop, "sctp", tape_prop_len) != 0) {
654 		kmem_free(tape_prop, tape_prop_len);
655 		return (DDI_PROBE_FAILURE);
656 	}
657 	kmem_free(tape_prop, tape_prop_len);
658 #endif
659 
660 	devp = ddi_get_driver_private(devi);
661 	instance = ddi_get_instance(devi);
662 
663 	if (ddi_get_soft_state(st_state, instance) != NULL) {
664 		return (DDI_PROBE_PARTIAL);
665 	}
666 
667 
668 	/*
669 	 * Turn around and call probe routine to see whether
670 	 * we actually have a tape at this SCSI nexus.
671 	 */
672 	if (scsi_probe(devp, NULL_FUNC) == SCSIPROBE_EXISTS) {
673 
674 		/*
675 		 * In checking the whole inq_dtype byte we are looking at both
676 		 * the Peripheral Qualifier and the Peripheral Device Type.
677 		 * For this driver we are only interested in sequential devices
678 		 * that are connected or capable if connecting to this logical
679 		 * unit.
680 		 */
681 		if (devp->sd_inq->inq_dtype ==
682 		    (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) {
683 			ST_DEBUG6(devi, st_label, SCSI_DEBUG,
684 			    "probe exists\n");
685 			rval = DDI_PROBE_SUCCESS;
686 		} else {
687 			rval = DDI_PROBE_FAILURE;
688 		}
689 	} else {
690 		ST_DEBUG6(devi, st_label, SCSI_DEBUG,
691 		    "probe failure: nothing there\n");
692 		rval = DDI_PROBE_FAILURE;
693 	}
694 	scsi_unprobe(devp);
695 	return (rval);
696 }
697 
698 static int
699 st_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
700 {
701 	int 	instance;
702 	int	wide;
703 	int 	dev_instance;
704 	int	ret_status;
705 	struct	scsi_device *devp;
706 	int	node_ix;
707 	struct	scsi_tape *un;
708 
709 	devp = ddi_get_driver_private(devi);
710 	instance = ddi_get_instance(devi);
711 
712 	switch (cmd) {
713 		case DDI_ATTACH:
714 			if (st_doattach(devp, SLEEP_FUNC) == DDI_FAILURE) {
715 				return (DDI_FAILURE);
716 			}
717 			break;
718 		case DDI_RESUME:
719 			/*
720 			 * Suspend/Resume
721 			 *
722 			 * When the driver suspended, there might be
723 			 * outstanding cmds and therefore we need to
724 			 * reset the suspended flag and resume the scsi
725 			 * watch thread and restart commands and timeouts
726 			 */
727 
728 			if (!(un = ddi_get_soft_state(st_state, instance))) {
729 				return (DDI_FAILURE);
730 			}
731 			dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) :
732 			    un->un_dev);
733 
734 			mutex_enter(ST_MUTEX);
735 
736 			un->un_throttle = un->un_max_throttle;
737 			un->un_tids_at_suspend = 0;
738 			un->un_pwr_mgmt = ST_PWR_NORMAL;
739 
740 			if (un->un_swr_token) {
741 				scsi_watch_resume(un->un_swr_token);
742 			}
743 
744 			/*
745 			 * Restart timeouts
746 			 */
747 			if ((un->un_tids_at_suspend & ST_DELAY_TID) != 0) {
748 				mutex_exit(ST_MUTEX);
749 				un->un_delay_tid =
750 				    timeout(st_delayed_cv_broadcast, un,
751 					drv_usectohz((clock_t)
752 					MEDIA_ACCESS_DELAY));
753 				mutex_enter(ST_MUTEX);
754 			}
755 
756 			if (un->un_tids_at_suspend & ST_HIB_TID) {
757 				mutex_exit(ST_MUTEX);
758 				un->un_hib_tid = timeout(st_intr_restart, un,
759 				    ST_STATUS_BUSY_TIMEOUT);
760 				mutex_enter(ST_MUTEX);
761 			}
762 
763 			ret_status = st_clear_unit_attentions(dev_instance, 5);
764 
765 			/*
766 			 * now check if we need to restore the tape position
767 			 */
768 			if ((un->un_suspend_fileno > 0) ||
769 			    (un->un_suspend_blkno > 0)) {
770 				if (ret_status != 0) {
771 					/*
772 					 * tape didn't get good TUR
773 					 * just print out error messages
774 					 */
775 					scsi_log(ST_DEVINFO, st_label, CE_WARN,
776 					    "st_attach-RESUME: tape failure "
777 					    " tape position will be lost");
778 				} else {
779 					/* this prints errors */
780 					(void) st_validate_tapemarks(un,
781 					    un->un_suspend_fileno,
782 					    un->un_suspend_blkno);
783 				}
784 				/*
785 				 * there are no retries, if there is an error
786 				 * we don't know if the tape has changed
787 				 */
788 				un->un_suspend_fileno = 0;
789 				un->un_suspend_blkno = 0;
790 			}
791 
792 			/* now we are ready to start up any queued I/Os */
793 			if (un->un_ncmds || un->un_quef) {
794 				st_start(un);
795 			}
796 
797 			cv_broadcast(&un->un_suspend_cv);
798 			mutex_exit(ST_MUTEX);
799 			return (DDI_SUCCESS);
800 
801 		default:
802 			return (DDI_FAILURE);
803 	}
804 
805 	un = ddi_get_soft_state(st_state, instance);
806 
807 	ST_DEBUG(devi, st_label, SCSI_DEBUG,
808 	    "st_attach: instance=%x\n", instance);
809 
810 	/*
811 	 * find the drive type for this target
812 	 */
813 	st_known_tape_type(un);
814 
815 	for (node_ix = 0; node_ix < ST_NUM_MEMBERS(st_minor_data); node_ix++) {
816 		int minor;
817 		char *name;
818 
819 		name  = st_minor_data[node_ix].name;
820 		minor = st_minor_data[node_ix].minor;
821 
822 		/*
823 		 * For default devices set the density to the
824 		 * preferred default density for this device.
825 		 */
826 		if (node_ix <= DEF_BSD_NR) {
827 			minor |= un->un_dp->default_density;
828 		}
829 		minor |= MTMINOR(instance);
830 
831 		if (ddi_create_minor_node(devi, name, S_IFCHR, minor,
832 		    DDI_NT_TAPE, NULL) == DDI_SUCCESS) {
833 			continue;
834 		}
835 
836 		ddi_remove_minor_node(devi, NULL);
837 		if (un) {
838 			cv_destroy(&un->un_clscv);
839 			cv_destroy(&un->un_sbuf_cv);
840 			cv_destroy(&un->un_queue_cv);
841 			cv_destroy(&un->un_state_cv);
842 			cv_destroy(&un->un_suspend_cv);
843 			cv_destroy(&un->un_tape_busy_cv);
844 
845 			if (un->un_sbufp) {
846 				freerbuf(un->un_sbufp);
847 			}
848 			if (un->un_uscsi_rqs_buf) {
849 				kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH);
850 			}
851 			if (un->un_mspl) {
852 				i_ddi_mem_free((caddr_t)un->un_mspl, NULL);
853 			}
854 			scsi_destroy_pkt(un->un_rqs);
855 			scsi_free_consistent_buf(un->un_rqs_bp);
856 			ddi_soft_state_free(st_state, instance);
857 			devp->sd_private = NULL;
858 			devp->sd_sense = NULL;
859 
860 		}
861 		ddi_prop_remove_all(devi);
862 		return (DDI_FAILURE);
863 	}
864 
865 	/*
866 	 * Add a zero-length attribute to tell the world we support
867 	 * kernel ioctls (for layered drivers)
868 	 */
869 	(void) ddi_prop_create(DDI_DEV_T_NONE, devi, DDI_PROP_CANSLEEP,
870 	    DDI_KERNEL_IOCTL, NULL, 0);
871 
872 	ddi_report_dev((dev_info_t *)devi);
873 
874 	/*
875 	 * If it's a SCSI-2 tape drive which supports wide,
876 	 * tell the host adapter to use wide.
877 	 */
878 	wide = ((devp->sd_inq->inq_rdf == RDF_SCSI2) &&
879 	    (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) ?
880 		1 : 0;
881 
882 	if (scsi_ifsetcap(ROUTE, "wide-xfer", wide, 1) == 1) {
883 		ST_DEBUG(devi, st_label, SCSI_DEBUG,
884 		    "Wide Transfer %s\n", wide ? "enabled" : "disabled");
885 	}
886 
887 	/*
888 	 * enable autorequest sense; keep the rq packet around in case
889 	 * the autorequest sense fails because of a busy condition
890 	 * do a getcap first in case the capability is not variable
891 	 */
892 	if (scsi_ifgetcap(ROUTE, "auto-rqsense", 1) == 1) {
893 		un->un_arq_enabled = 1;
894 	} else {
895 		un->un_arq_enabled =
896 		    ((scsi_ifsetcap(ROUTE, "auto-rqsense", 1, 1) == 1) ? 1 : 0);
897 	}
898 
899 
900 	ST_DEBUG(devi, st_label, SCSI_DEBUG, "auto request sense %s\n",
901 		(un->un_arq_enabled ? "enabled" : "disabled"));
902 
903 	un->un_untagged_qing =
904 	    (scsi_ifgetcap(ROUTE, "untagged-qing", 0) == 1);
905 
906 	/*
907 	 * XXX - This is just for 2.6.  to tell users that write buffering
908 	 *	has gone away.
909 	 */
910 	if (un->un_arq_enabled && un->un_untagged_qing) {
911 		if (ddi_getprop(DDI_DEV_T_ANY, devi, DDI_PROP_DONTPASS,
912 		    "tape-driver-buffering", 0) != 0) {
913 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
914 			    "Write Data Buffering has been depricated. Your "
915 			    "applications should continue to work normally.\n"
916 			    " But, they should  ported to use Asynchronous "
917 			    " I/O\n"
918 			    " For more information, read about "
919 			    " tape-driver-buffering "
920 			    "property in the st(7d) man page\n");
921 		}
922 	}
923 
924 	un->un_max_throttle = un->un_throttle = un->un_last_throttle = 1;
925 	un->un_flush_on_errors = 0;
926 	un->un_mkr_pkt = (struct scsi_pkt *)NULL;
927 
928 	ST_DEBUG(devi, st_label, SCSI_DEBUG,
929 	    "throttle=%x, max_throttle = %x\n",
930 	    un->un_throttle, un->un_max_throttle);
931 
932 	/* initialize persistent errors to nil */
933 	un->un_persistence = 0;
934 	un->un_persist_errors = 0;
935 
936 	/*
937 	 * Get dma-max from HBA driver. If it is not defined, use 64k
938 	 */
939 	un->un_maxdma	= scsi_ifgetcap(&devp->sd_address, "dma-max", 1);
940 	if (un->un_maxdma == -1) {
941 		ST_DEBUG(devi, st_label, SCSI_DEBUG,
942 		    "Receaved a value that looked like -1. Using 64k maxdma");
943 		un->un_maxdma = (64 * 1024);
944 	}
945 
946 	/*
947 	 * Get the max allowable cdb size
948 	 */
949 	un->un_max_cdb_sz =
950 	    scsi_ifgetcap(&devp->sd_address, "max-cdb-length", 1);
951 	if (un->un_max_cdb_sz < CDB_GROUP0) {
952 		ST_DEBUG(devi, st_label, SCSI_DEBUG,
953 		    "HBA reported max-cdb-length as %d\n", un->un_max_cdb_sz);
954 		un->un_max_cdb_sz = CDB_GROUP4; /* optimistic default */
955 	}
956 
957 	un->un_maxbsize = MAXBSIZE_UNKNOWN;
958 
959 	un->un_mediastate = MTIO_NONE;
960 	un->un_HeadClean  = TAPE_ALERT_SUPPORT_UNKNOWN;
961 
962 	/*
963 	 * initialize kstats
964 	 */
965 	un->un_stats = kstat_create("st", instance, NULL, "tape",
966 			KSTAT_TYPE_IO, 1, KSTAT_FLAG_PERSISTENT);
967 	if (un->un_stats) {
968 		un->un_stats->ks_lock = ST_MUTEX;
969 		kstat_install(un->un_stats);
970 	}
971 	(void) st_create_errstats(un, instance);
972 
973 	return (DDI_SUCCESS);
974 }
975 
976 /*
977  * st_detach:
978  *
979  * we allow a detach if and only if:
980  *	- no tape is currently inserted
981  *	- tape position is at BOT or unknown
982  *		(if it is not at BOT then a no rewind
983  *		device was opened and we have to preserve state)
984  *	- it must be in a closed state : no timeouts or scsi_watch requests
985  *		will exist if it is closed, so we don't need to check for
986  *		them here.
987  */
988 /*ARGSUSED*/
989 static int
990 st_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
991 {
992 	int 	instance;
993 	int 	dev_instance;
994 	struct scsi_device *devp;
995 	struct scsi_tape *un;
996 	clock_t wait_cmds_complete;
997 
998 	instance = ddi_get_instance(devi);
999 
1000 	if (!(un = ddi_get_soft_state(st_state, instance))) {
1001 		return (DDI_FAILURE);
1002 	}
1003 
1004 	switch (cmd) {
1005 
1006 	case DDI_DETACH:
1007 		/*
1008 		 * Undo what we did in st_attach & st_doattach,
1009 		 * freeing resources and removing things we installed.
1010 		 * The system framework guarantees we are not active
1011 		 * with this devinfo node in any other entry points at
1012 		 * this time.
1013 		 */
1014 
1015 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
1016 		    "st_detach: instance=%x, un=%p\n", instance,
1017 		    (void *)un);
1018 
1019 		if (((un->un_dp->options & ST_UNLOADABLE) == 0) ||
1020 		    (un->un_ncmds != 0) || (un->un_quef != NULL) ||
1021 		    (un->un_state != ST_STATE_CLOSED)) {
1022 			/*
1023 			 * we cannot unload some targets because the
1024 			 * inquiry returns junk unless immediately
1025 			 * after a reset
1026 			 */
1027 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
1028 			    "cannot unload instance %x\n", instance);
1029 			return (DDI_FAILURE);
1030 		}
1031 
1032 		/*
1033 		 * if the tape has been removed then we may unload;
1034 		 * do a test unit ready and if it returns NOT READY
1035 		 * then we assume that it is safe to unload.
1036 		 * as a side effect, fileno may be set to -1 if the
1037 		 * the test unit ready fails;
1038 		 * also un_state may be set to non-closed, so reset it
1039 		 */
1040 		if ((un->un_dev) &&		/* Been opened since attach */
1041 		    ((un->un_fileno > 0) ||	/* Known position not rewound */
1042 		    (un->un_blkno != 0))) {	/* Or within first file */
1043 			mutex_enter(ST_MUTEX);
1044 			/*
1045 			 * Send Test Unit Ready in the hopes that if
1046 			 * the drive is not in the state we think it is.
1047 			 * And the state will be changed so it can be detached.
1048 			 * If the command fails to reach the device and
1049 			 * the drive was not rewound or unloaded we want
1050 			 * to fail the detach till a user command fails
1051 			 * where after the detach will succead.
1052 			 */
1053 			(void) st_cmd(un->un_dev, SCMD_TEST_UNIT_READY,
1054 			    0, SYNC_CMD);
1055 			/*
1056 			 * After TUR un_state may be set to non-closed,
1057 			 * so reset it back.
1058 			 */
1059 			un->un_state = ST_STATE_CLOSED;
1060 			mutex_exit(ST_MUTEX);
1061 		}
1062 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
1063 		    "un_status=%x, fileno=%x, blkno=%lx\n",
1064 		    un->un_status, un->un_fileno, un->un_blkno);
1065 
1066 		/*
1067 		 * check again:
1068 		 * if we are not at BOT then it is not safe to unload
1069 		 */
1070 		if ((un->un_dev) &&		/* Been opened since attach */
1071 		    ((un->un_fileno > 0) ||	/* Known position not rewound */
1072 		    (un->un_blkno != 0))) {	/* Or within first file */
1073 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
1074 			    "cannot detach: fileno=%x, blkno=%lx\n",
1075 			    un->un_fileno, un->un_blkno);
1076 			return (DDI_FAILURE);
1077 		}
1078 
1079 		/*
1080 		 * Just To make sure that we have released the
1081 		 * tape unit .
1082 		 */
1083 		if (un->un_dev && (un->un_rsvd_status & ST_RESERVE) &&
1084 		    !DEVI_IS_DEVICE_REMOVED(devi)) {
1085 			mutex_enter(ST_MUTEX);
1086 			(void) st_reserve_release(un, ST_RELEASE);
1087 			mutex_exit(ST_MUTEX);
1088 		}
1089 
1090 		/*
1091 		 * now remove other data structures allocated in st_doattach()
1092 		 */
1093 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
1094 		    "destroying/freeing\n");
1095 		cv_destroy(&un->un_clscv);
1096 		cv_destroy(&un->un_sbuf_cv);
1097 		cv_destroy(&un->un_queue_cv);
1098 		cv_destroy(&un->un_suspend_cv);
1099 		cv_destroy(&un->un_tape_busy_cv);
1100 
1101 		if (un->un_hib_tid) {
1102 			(void) untimeout(un->un_hib_tid);
1103 			un->un_hib_tid = 0;
1104 		}
1105 
1106 		if (un->un_delay_tid) {
1107 			(void) untimeout(un->un_delay_tid);
1108 			un->un_delay_tid = 0;
1109 		}
1110 		cv_destroy(&un->un_state_cv);
1111 
1112 #if defined(__i386) || defined(__amd64)
1113 		if (un->un_contig_mem_hdl != NULL) {
1114 			ddi_dma_free_handle(&un->un_contig_mem_hdl);
1115 		}
1116 #endif
1117 		if (un->un_sbufp) {
1118 			freerbuf(un->un_sbufp);
1119 		}
1120 		if (un->un_uscsi_rqs_buf) {
1121 			kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH);
1122 		}
1123 		if (un->un_mspl) {
1124 			i_ddi_mem_free((caddr_t)un->un_mspl, NULL);
1125 		}
1126 		if (un->un_rqs) {
1127 			scsi_destroy_pkt(un->un_rqs);
1128 			scsi_free_consistent_buf(un->un_rqs_bp);
1129 		}
1130 		if (un->un_mkr_pkt) {
1131 			scsi_destroy_pkt(un->un_mkr_pkt);
1132 		}
1133 		if (un->un_arq_enabled) {
1134 			(void) scsi_ifsetcap(ROUTE, "auto-rqsense", 0, 1);
1135 		}
1136 		if (un->un_dp_size) {
1137 			kmem_free(un->un_dp, un->un_dp_size);
1138 		}
1139 		if (un->un_stats) {
1140 			kstat_delete(un->un_stats);
1141 			un->un_stats = (kstat_t *)0;
1142 		}
1143 		if (un->un_errstats) {
1144 			kstat_delete(un->un_errstats);
1145 			un->un_errstats = (kstat_t *)0;
1146 		}
1147 		devp = ST_SCSI_DEVP;
1148 		ddi_soft_state_free(st_state, instance);
1149 		devp->sd_private = NULL;
1150 		devp->sd_sense = NULL;
1151 		scsi_unprobe(devp);
1152 		ddi_prop_remove_all(devi);
1153 		ddi_remove_minor_node(devi, NULL);
1154 		ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach done\n");
1155 		return (DDI_SUCCESS);
1156 
1157 	case DDI_SUSPEND:
1158 
1159 		/*
1160 		 * Suspend/Resume
1161 		 *
1162 		 * To process DDI_SUSPEND, we must do the following:
1163 		 *
1164 		 *  - check ddi_removing_power to see if power will be turned
1165 		 *    off. if so, return DDI_FAILURE
1166 		 *  - check if we are already suspended,
1167 		 *    if so, return DDI_FAILURE
1168 		 *  - check if device state is CLOSED,
1169 		 *    if not, return DDI_FAILURE.
1170 		 *  - wait until outstanding operations complete
1171 		 *  - save tape state
1172 		 *  - block new operations
1173 		 *  - cancel pending timeouts
1174 		 *
1175 		 */
1176 
1177 		if (ddi_removing_power(devi))
1178 			return (DDI_FAILURE);
1179 
1180 		mutex_enter(ST_MUTEX);
1181 
1182 		/*
1183 		 * Shouldn't already be suspended, if so return failure
1184 		 */
1185 		if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) {
1186 			mutex_exit(ST_MUTEX);
1187 			return (DDI_FAILURE);
1188 		}
1189 		if (un->un_state != ST_STATE_CLOSED) {
1190 			mutex_exit(ST_MUTEX);
1191 			return (DDI_FAILURE);
1192 		}
1193 
1194 		/*
1195 		 * Wait for all outstanding I/O's to complete
1196 		 *
1197 		 * we wait on both ncmds and the wait queue for times
1198 		 * when we are flushing after persistent errors are
1199 		 * flagged, which is when ncmds can be 0, and the
1200 		 * queue can still have I/O's.  This way we preserve
1201 		 * order of biodone's.
1202 		 */
1203 		wait_cmds_complete = ddi_get_lbolt();
1204 		wait_cmds_complete +=
1205 		    st_wait_cmds_complete * drv_usectohz(1000000);
1206 		while (un->un_ncmds || un->un_quef ||
1207 		    (un->un_state == ST_STATE_RESOURCE_WAIT)) {
1208 
1209 			if (cv_timedwait(&un->un_tape_busy_cv, ST_MUTEX,
1210 			    wait_cmds_complete) == -1) {
1211 				/*
1212 				 * Time expired then cancel the command
1213 				 */
1214 				mutex_exit(ST_MUTEX);
1215 				if (scsi_reset(ROUTE, RESET_TARGET) == 0) {
1216 					mutex_enter(ST_MUTEX);
1217 					if (un->un_last_throttle) {
1218 						un->un_throttle =
1219 						    un->un_last_throttle;
1220 					}
1221 					mutex_exit(ST_MUTEX);
1222 					return (DDI_FAILURE);
1223 				} else {
1224 					mutex_enter(ST_MUTEX);
1225 					break;
1226 				}
1227 			}
1228 		}
1229 
1230 		/*
1231 		 * DDI_SUSPEND says that the system "may" power down, we
1232 		 * remember the file and block number before rewinding.
1233 		 * we also need to save state before issuing
1234 		 * any WRITE_FILE_MARK command.
1235 		 */
1236 		if (un->un_fileno < 0) {
1237 			un->un_suspend_fileno = 0;
1238 			un->un_suspend_blkno  = 0;
1239 		} else {
1240 			un->un_suspend_fileno = un->un_fileno;
1241 			un->un_suspend_blkno = un->un_blkno;
1242 		}
1243 		dev_instance = ((un->un_dev == 0) ? MTMINOR(instance) :
1244 		    un->un_dev);
1245 
1246 		/*
1247 		 * Issue a zero write file fmk command to tell the drive to
1248 		 * flush any buffered tape marks
1249 		 */
1250 		(void) st_cmd(dev_instance, SCMD_WRITE_FILE_MARK, 0, SYNC_CMD);
1251 
1252 		/*
1253 		 * Because not all tape drives correctly implement buffer
1254 		 * flushing with the zero write file fmk command, issue a
1255 		 * synchronous rewind command to force data flushing.
1256 		 * st_validate_tapemarks() will do a rewind during DDI_RESUME
1257 		 * anyway.
1258 		 */
1259 		(void) st_cmd(dev_instance, SCMD_REWIND, 0, SYNC_CMD);
1260 
1261 		/* stop any new operations */
1262 		un->un_pwr_mgmt = ST_PWR_SUSPENDED;
1263 		un->un_throttle = 0;
1264 
1265 		/*
1266 		 * cancel any outstanding timeouts
1267 		 */
1268 		if (un->un_delay_tid) {
1269 			timeout_id_t temp_id = un->un_delay_tid;
1270 			un->un_delay_tid = 0;
1271 			un->un_tids_at_suspend |= ST_DELAY_TID;
1272 			mutex_exit(ST_MUTEX);
1273 			(void) untimeout(temp_id);
1274 			mutex_enter(ST_MUTEX);
1275 		}
1276 
1277 		if (un->un_hib_tid) {
1278 			timeout_id_t temp_id = un->un_hib_tid;
1279 			un->un_hib_tid = 0;
1280 			un->un_tids_at_suspend |= ST_HIB_TID;
1281 			mutex_exit(ST_MUTEX);
1282 			(void) untimeout(temp_id);
1283 			mutex_enter(ST_MUTEX);
1284 		}
1285 
1286 		/*
1287 		 * Suspend the scsi_watch_thread
1288 		 */
1289 		if (un->un_swr_token) {
1290 			opaque_t temp_token = un->un_swr_token;
1291 			mutex_exit(ST_MUTEX);
1292 			scsi_watch_suspend(temp_token);
1293 		} else {
1294 			mutex_exit(ST_MUTEX);
1295 		}
1296 
1297 		return (DDI_SUCCESS);
1298 
1299 	default:
1300 		ST_DEBUG(0, st_label, SCSI_DEBUG, "st_detach failed\n");
1301 		return (DDI_FAILURE);
1302 	}
1303 }
1304 
1305 
1306 /* ARGSUSED */
1307 static int
1308 stinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
1309 {
1310 	dev_t dev;
1311 	struct scsi_tape *un;
1312 	int instance, error;
1313 	switch (infocmd) {
1314 	case DDI_INFO_DEVT2DEVINFO:
1315 		dev = (dev_t)arg;
1316 		instance = MTUNIT(dev);
1317 		if ((un = ddi_get_soft_state(st_state, instance)) == NULL)
1318 			return (DDI_FAILURE);
1319 		*result = (void *) ST_DEVINFO;
1320 		error = DDI_SUCCESS;
1321 		break;
1322 	case DDI_INFO_DEVT2INSTANCE:
1323 		dev = (dev_t)arg;
1324 		instance = MTUNIT(dev);
1325 		*result = (void *)(uintptr_t)instance;
1326 		error = DDI_SUCCESS;
1327 		break;
1328 	default:
1329 		error = DDI_FAILURE;
1330 	}
1331 	return (error);
1332 }
1333 
1334 static int
1335 st_doattach(struct scsi_device *devp, int (*canwait)())
1336 {
1337 	struct scsi_pkt *rqpkt = NULL;
1338 	struct scsi_tape *un = NULL;
1339 	int km_flags = (canwait != NULL_FUNC) ? KM_SLEEP : KM_NOSLEEP;
1340 	int instance;
1341 	struct buf *bp;
1342 	size_t rlen;
1343 
1344 	/*
1345 	 * Call the routine scsi_probe to do some of the dirty work.
1346 	 * If the INQUIRY command succeeds, the field sd_inq in the
1347 	 * device structure will be filled in.
1348 	 */
1349 	ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
1350 		"st_doattach(): probing\n");
1351 
1352 	if (scsi_probe(devp, canwait) == SCSIPROBE_EXISTS) {
1353 
1354 		/*
1355 		 * In checking the whole inq_dtype byte we are looking at both
1356 		 * the Peripheral Qualifier and the Peripheral Device Type.
1357 		 * For this driver we are only interested in sequential devices
1358 		 * that are connected or capable if connecting to this logical
1359 		 * unit.
1360 		 */
1361 		if (devp->sd_inq->inq_dtype ==
1362 		    (DTYPE_SEQUENTIAL | DPQ_POSSIBLE)) {
1363 			ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
1364 			    "probe exists\n");
1365 		} else {
1366 			/* Something there but not a tape device */
1367 			scsi_unprobe(devp);
1368 			return (DDI_FAILURE);
1369 		}
1370 	} else {
1371 		/* Nothing there */
1372 		ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
1373 		    "probe failure: nothing there\n");
1374 		scsi_unprobe(devp);
1375 		return (DDI_FAILURE);
1376 	}
1377 
1378 	bp = scsi_alloc_consistent_buf(&devp->sd_address, (struct buf *)NULL,
1379 	    SENSE_LENGTH, B_READ, canwait, NULL);
1380 	if (!bp) {
1381 		goto error;
1382 	}
1383 	rqpkt = scsi_init_pkt(&devp->sd_address,
1384 	    (struct scsi_pkt *)NULL, bp, CDB_GROUP0, 1, 0,
1385 	    PKT_CONSISTENT, canwait, NULL);
1386 	if (!rqpkt) {
1387 		goto error;
1388 	}
1389 	devp->sd_sense = (struct scsi_extended_sense *)bp->b_un.b_addr;
1390 	ASSERT(geterror(bp) == NULL);
1391 
1392 	(void) scsi_setup_cdb((union scsi_cdb *)rqpkt->pkt_cdbp,
1393 	    SCMD_REQUEST_SENSE, 0, SENSE_LENGTH, 0);
1394 	FILL_SCSI1_LUN(devp, rqpkt);
1395 
1396 	/*
1397 	 * The actual unit is present.
1398 	 * Now is the time to fill in the rest of our info..
1399 	 */
1400 	instance = ddi_get_instance(devp->sd_dev);
1401 
1402 	if (ddi_soft_state_zalloc(st_state, instance) != DDI_SUCCESS) {
1403 		goto error;
1404 	}
1405 	un = ddi_get_soft_state(st_state, instance);
1406 
1407 	ASSERT(un != NULL);
1408 
1409 	un->un_sbufp = getrbuf(km_flags);
1410 
1411 	un->un_uscsi_rqs_buf = kmem_alloc(SENSE_LENGTH, KM_SLEEP);
1412 
1413 	/*
1414 	 * use i_ddi_mem_alloc() for now until we have an interface to allocate
1415 	 * memory for DMA which doesn't require a DMA handle. ddi_iopb_alloc()
1416 	 * is obsolete and we want more flexibility in controlling the DMA
1417 	 * address constraints.
1418 	 */
1419 	(void) i_ddi_mem_alloc(devp->sd_dev, &st_alloc_attr,
1420 	    sizeof (struct seq_mode), ((km_flags == KM_SLEEP) ? 1 : 0), 0,
1421 	    NULL, (caddr_t *)&un->un_mspl, &rlen, NULL);
1422 
1423 	if (!un->un_sbufp || !un->un_mspl) {
1424 		if (un->un_mspl) {
1425 			i_ddi_mem_free((caddr_t)un->un_mspl, NULL);
1426 		}
1427 		ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG,
1428 		    "probe partial failure: no space\n");
1429 		goto error;
1430 	}
1431 
1432 	bzero(un->un_mspl, sizeof (struct seq_mode));
1433 
1434 	cv_init(&un->un_sbuf_cv, NULL, CV_DRIVER, NULL);
1435 	cv_init(&un->un_queue_cv, NULL, CV_DRIVER, NULL);
1436 	cv_init(&un->un_clscv, NULL, CV_DRIVER, NULL);
1437 	cv_init(&un->un_state_cv, NULL, CV_DRIVER, NULL);
1438 #if defined(__i386) || defined(__amd64)
1439 	cv_init(&un->un_contig_mem_cv, NULL, CV_DRIVER, NULL);
1440 #endif
1441 
1442 	/* Initialize power managemnet condition variable */
1443 	cv_init(&un->un_suspend_cv, NULL, CV_DRIVER, NULL);
1444 	cv_init(&un->un_tape_busy_cv, NULL, CV_DRIVER, NULL);
1445 
1446 	rqpkt->pkt_flags |= (FLAG_SENSING | FLAG_HEAD | FLAG_NODISCON);
1447 
1448 	un->un_fileno	= -1;
1449 	rqpkt->pkt_time = st_io_time;
1450 	rqpkt->pkt_comp = st_intr;
1451 	un->un_rqs	= rqpkt;
1452 	un->un_sd	= devp;
1453 	un->un_rqs_bp	= bp;
1454 	un->un_swr_token = (opaque_t)NULL;
1455 	un->un_comp_page = ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE;
1456 	un->un_wormable = st_is_drive_worm;
1457 
1458 	un->un_suspend_fileno 	= 0;
1459 	un->un_suspend_blkno 	= 0;
1460 
1461 #if defined(__i386) || defined(__amd64)
1462 	if (ddi_dma_alloc_handle(ST_DEVINFO, &st_contig_mem_dma_attr,
1463 		DDI_DMA_SLEEP, NULL, &un->un_contig_mem_hdl) != DDI_SUCCESS) {
1464 		ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG,
1465 		    "allocation of contiguous memory dma handle failed!");
1466 		un->un_contig_mem_hdl = NULL;
1467 		goto error;
1468 	}
1469 #endif
1470 
1471 	/*
1472 	 * Since this driver manages devices with "remote" hardware,
1473 	 * i.e. the devices themselves have no "reg" properties,
1474 	 * the SUSPEND/RESUME commands in detach/attach will not be
1475 	 * called by the power management framework unless we request
1476 	 * it by creating a "pm-hardware-state" property and setting it
1477 	 * to value "needs-suspend-resume".
1478 	 */
1479 	if (ddi_prop_update_string(DDI_DEV_T_NONE, devp->sd_dev,
1480 	    "pm-hardware-state", "needs-suspend-resume") !=
1481 	    DDI_PROP_SUCCESS) {
1482 
1483 		ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
1484 		    "ddi_prop_update(\"pm-hardware-state\") failed\n");
1485 		goto error;
1486 	}
1487 
1488 	if (ddi_prop_create(DDI_DEV_T_NONE, devp->sd_dev, DDI_PROP_CANSLEEP,
1489 	    "no-involuntary-power-cycles", NULL, 0) != DDI_PROP_SUCCESS) {
1490 
1491 		ST_DEBUG(devp->sd_dev, st_label, SCSI_DEBUG,
1492 		    "ddi_prop_create(\"no-involuntary-power-cycles\") "
1493 		    "failed\n");
1494 		goto error;
1495 	}
1496 
1497 	ST_DEBUG6(devp->sd_dev, st_label, SCSI_DEBUG, "probe success\n");
1498 	return (DDI_SUCCESS);
1499 
1500 error:
1501 	devp->sd_sense = NULL;
1502 
1503 	ddi_remove_minor_node(devp->sd_dev, NULL);
1504 	if (un) {
1505 		if (un->un_mspl) {
1506 			i_ddi_mem_free((caddr_t)un->un_mspl, NULL);
1507 		}
1508 		if (un->un_sbufp) {
1509 			freerbuf(un->un_sbufp);
1510 		}
1511 		if (un->un_uscsi_rqs_buf) {
1512 			kmem_free(un->un_uscsi_rqs_buf, SENSE_LENGTH);
1513 		}
1514 #if defined(__i386) || defined(__amd64)
1515 		if (un->un_contig_mem_hdl != NULL) {
1516 			ddi_dma_free_handle(&un->un_contig_mem_hdl);
1517 		}
1518 #endif
1519 		ddi_soft_state_free(st_state, instance);
1520 		devp->sd_private = NULL;
1521 	}
1522 
1523 	if (rqpkt) {
1524 		scsi_destroy_pkt(rqpkt);
1525 	}
1526 
1527 	if (bp) {
1528 		scsi_free_consistent_buf(bp);
1529 	}
1530 
1531 	if (devp->sd_inq) {
1532 		scsi_unprobe(devp);
1533 	}
1534 	return (DDI_FAILURE);
1535 }
1536 
1537 typedef int
1538 (*cfg_functp)(struct scsi_tape *, char *vidpid, struct st_drivetype *);
1539 
1540 static cfg_functp config_functs[] = {
1541 	st_get_conf_from_st_dot_conf,
1542 	st_get_conf_from_st_conf_dot_c,
1543 	st_get_default_conf
1544 };
1545 
1546 
1547 /*
1548  * determine tape type, using tape-config-list or built-in table or
1549  * use a generic tape config entry
1550  */
1551 static void
1552 st_known_tape_type(struct scsi_tape *un)
1553 {
1554 	struct st_drivetype *dp;
1555 	cfg_functp *config_funct;
1556 
1557 	/*
1558 	 * XXX:  Emulex MT-02 (and emulators) predates SCSI-1 and has
1559 	 *	 no vid & pid inquiry data.  So, we provide one.
1560 	 */
1561 	if (ST_INQUIRY->inq_len == 0 ||
1562 		(bcmp("\0\0\0\0\0\0\0\0", ST_INQUIRY->inq_vid, 8) == 0)) {
1563 		(void) strcpy((char *)ST_INQUIRY->inq_vid, ST_MT02_NAME);
1564 	}
1565 
1566 	un->un_dp_size = sizeof (struct st_drivetype);
1567 	dp = kmem_zalloc((size_t)un->un_dp_size, KM_SLEEP);
1568 	un->un_dp = dp;
1569 
1570 	/*
1571 	 * Loop through the configuration methods till one works.
1572 	 */
1573 	for (config_funct = &config_functs[0]; ; config_funct++) {
1574 		if ((*config_funct)(un, ST_INQUIRY->inq_vid, dp)) {
1575 			break;
1576 		}
1577 	}
1578 
1579 	/*
1580 	 * If we didn't just make up this configuration and
1581 	 * all the density codes are the same..
1582 	 * Set Auto Density over ride.
1583 	 */
1584 	if (*config_funct != st_get_default_conf) {
1585 		/*
1586 		 * If this device is one that is configured and all
1587 		 * densities are the same, This saves doing gets and set
1588 		 * that yield nothing.
1589 		 */
1590 		if ((dp->densities[0]) == (dp->densities[1]) &&
1591 		    (dp->densities[0]) == (dp->densities[2]) &&
1592 		    (dp->densities[0]) == (dp->densities[3])) {
1593 
1594 			dp->options |= ST_AUTODEN_OVERRIDE;
1595 		}
1596 	}
1597 
1598 
1599 	/*
1600 	 * Store tape drive characteristics.
1601 	 */
1602 	un->un_status = 0;
1603 	un->un_attached = 1;
1604 	un->un_init_options = dp->options;
1605 
1606 	/* setup operation time-outs based on options */
1607 	st_calculate_timeouts(un);
1608 
1609 	/* make sure if we are supposed to be variable, make it variable */
1610 	if (dp->options & ST_VARIABLE) {
1611 		dp->bsize = 0;
1612 	}
1613 
1614 	scsi_log(ST_DEVINFO, st_label, CE_NOTE, "?<%s>\n", dp->name);
1615 }
1616 
1617 
1618 typedef struct {
1619 	int mask;
1620 	int bottom;
1621 	int top;
1622 	char *name;
1623 } conf_limit;
1624 
1625 static const conf_limit conf_limits[] = {
1626 
1627 	-1,		1,		2,		"conf version",
1628 	-1,		MT_ISTS,	ST_LAST_TYPE,	"drive type",
1629 	-1,		0,		0xffffff,	"block size",
1630 	ST_VALID_OPTS,	0,		ST_VALID_OPTS,	"options",
1631 	-1,		0,		4,		"number of densities",
1632 	-1,		0,		UINT8_MAX,	"density code",
1633 	-1,		0,		3,		"default density",
1634 	-1,		0,		UINT16_MAX,	"non motion timeout",
1635 	-1,		0,		UINT16_MAX,	"I/O timeout",
1636 	-1,		0,		UINT16_MAX,	"space timeout",
1637 	-1,		0,		UINT16_MAX,	"load timeout",
1638 	-1,		0,		UINT16_MAX,	"unload timeout",
1639 	-1,		0,		UINT16_MAX,	"erase timeout",
1640 	0,		0,		0,		NULL
1641 };
1642 
1643 static int
1644 st_validate_conf_data(struct scsi_tape *un, int *list, int list_len,
1645     const char *conf_name)
1646 {
1647 	int dens;
1648 	int ndens;
1649 	int value;
1650 	int type;
1651 	int count;
1652 	const conf_limit *limit = &conf_limits[0];
1653 
1654 	ST_DEBUG3(ST_DEVINFO, st_label, CE_NOTE,
1655 	    "Checking %d entrys total with %d densities\n", list_len, list[4]);
1656 
1657 	count = list_len;
1658 	type = *list;
1659 	for (;  count && limit->name; count--, list++, limit++) {
1660 
1661 		value = *list;
1662 		if (value & ~limit->mask) {
1663 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
1664 			    "%s %s value invalid bits set: 0x%X\n",
1665 			    conf_name, limit->name, value & ~limit->mask);
1666 			*list &= limit->mask;
1667 		} else if (value < limit->bottom) {
1668 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
1669 			    "%s %s value too low: value = %d limit %d\n",
1670 			    conf_name, limit->name, value, limit->bottom);
1671 		} else if (value > limit->top) {
1672 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
1673 			    "%s %s value too high: value = %d limit %d\n",
1674 			    conf_name, limit->name, value, limit->top);
1675 		} else {
1676 			ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT,
1677 			    "%s %s value = 0x%X\n",
1678 			    conf_name, limit->name, value);
1679 		}
1680 
1681 		/* If not the number of densities continue */
1682 		if (limit != &conf_limits[4]) {
1683 			continue;
1684 		}
1685 
1686 		/* If number of densities is not in range can't use config */
1687 		if (value < limit->bottom || value > limit->top) {
1688 			return (-1);
1689 		}
1690 
1691 		ndens = min(value, NDENSITIES);
1692 		if ((type == 1) && (list_len - ndens) != 6) {
1693 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
1694 			    "%s conf version 1 with %d densities has %d items"
1695 			    " should have %d",
1696 			    conf_name, ndens, list_len, 6 + ndens);
1697 		} else if ((type == 2) && (list_len - ndens) != 13) {
1698 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
1699 			    "%s conf version 2 with %d densities has %d items"
1700 			    " should have %d",
1701 			    conf_name, ndens, list_len, 13 + ndens);
1702 		}
1703 
1704 		limit++;
1705 		for (dens = 0; dens < ndens && count; dens++) {
1706 			count--;
1707 			list++;
1708 			value = *list;
1709 			if (value < limit->bottom) {
1710 				scsi_log(ST_DEVINFO, st_label, CE_NOTE,
1711 				    "%s density[%d] value too low: value ="
1712 				    " 0x%X limit 0x%X\n",
1713 				    conf_name, dens, value, limit->bottom);
1714 			} else if (value > limit->top) {
1715 				scsi_log(ST_DEVINFO, st_label, CE_NOTE,
1716 				    "%s density[%d] value too high: value ="
1717 				    " 0x%X limit 0x%X\n",
1718 				    conf_name, dens, value, limit->top);
1719 			} else {
1720 				ST_DEBUG3(ST_DEVINFO, st_label, CE_CONT,
1721 				    "%s density[%d] value = 0x%X\n",
1722 				    conf_name, dens, value);
1723 			}
1724 		}
1725 	}
1726 
1727 	return (0);
1728 }
1729 
1730 static int
1731 st_get_conf_from_st_dot_conf(struct scsi_tape *un, char *vidpid,
1732     struct st_drivetype *dp)
1733 {
1734 	caddr_t config_list = NULL;
1735 	caddr_t data_list = NULL;
1736 	int	*data_ptr;
1737 	caddr_t vidptr, prettyptr, datanameptr;
1738 	size_t	vidlen, prettylen, datanamelen, tripletlen = 0;
1739 	int config_list_len, data_list_len, len, i;
1740 	int version;
1741 	int found = 0;
1742 
1743 
1744 	/*
1745 	 * Determine type of tape controller. Type is determined by
1746 	 * checking the vendor ids of the earlier inquiry command and
1747 	 * comparing those with vids in tape-config-list defined in st.conf
1748 	 */
1749 	if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, DDI_PROP_DONTPASS,
1750 	    "tape-config-list", (caddr_t)&config_list, &config_list_len)
1751 	    != DDI_PROP_SUCCESS) {
1752 		return (found);
1753 	}
1754 
1755 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
1756 	    "st_get_conf_from_st_dot_conf(): st.conf has tape-config-list\n");
1757 
1758 	/*
1759 	 * Compare vids in each triplet - if it matches, get value for
1760 	 * data_name and contruct a st_drivetype struct
1761 	 * tripletlen is not set yet!
1762 	 */
1763 	for (len = config_list_len, vidptr = config_list;
1764 	    len > 0;
1765 	    vidptr += tripletlen, len -= tripletlen) {
1766 
1767 		vidlen = strlen(vidptr);
1768 		prettyptr = vidptr + vidlen + 1;
1769 		prettylen = strlen(prettyptr);
1770 		datanameptr = prettyptr + prettylen + 1;
1771 		datanamelen = strlen(datanameptr);
1772 		tripletlen = vidlen + prettylen + datanamelen + 3;
1773 
1774 		if (vidlen == 0) {
1775 			continue;
1776 		}
1777 
1778 		/*
1779 		 * If inquiry vid dosen't match this triplets vid,
1780 		 * try the next.
1781 		 */
1782 		if (strncasecmp(vidpid, vidptr, vidlen)) {
1783 			continue;
1784 		}
1785 
1786 		/*
1787 		 * if prettylen is zero then use the vid string
1788 		 */
1789 		if (prettylen == 0) {
1790 			prettyptr = vidptr;
1791 			prettylen = vidlen;
1792 		}
1793 
1794 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
1795 		    "vid = %s, pretty=%s, dataname = %s\n",
1796 		    vidptr, prettyptr, datanameptr);
1797 
1798 		/*
1799 		 * get the data list
1800 		 */
1801 		if (ddi_getlongprop(DDI_DEV_T_ANY, ST_DEVINFO, 0,
1802 		    datanameptr, (caddr_t)&data_list,
1803 		    &data_list_len) != DDI_PROP_SUCCESS) {
1804 			/*
1805 			 * Error in getting property value
1806 			 * print warning!
1807 			 */
1808 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
1809 			    "data property (%s) has no value\n",
1810 			    datanameptr);
1811 			continue;
1812 		}
1813 
1814 		/*
1815 		 * now initialize the st_drivetype struct
1816 		 */
1817 		(void) strncpy(dp->name, prettyptr, ST_NAMESIZE - 1);
1818 		dp->length = (int)min(vidlen, (VIDPIDLEN - 1));
1819 		(void) strncpy(dp->vid, vidptr, dp->length);
1820 		data_ptr = (int *)data_list;
1821 		/*
1822 		 * check if data is enough for version, type,
1823 		 * bsize, options, # of densities, density1,
1824 		 * density2, ..., default_density
1825 		 */
1826 		if ((data_list_len < 5 * sizeof (int)) ||
1827 		    (data_list_len < 6 * sizeof (int) +
1828 		    *(data_ptr + 4) * sizeof (int))) {
1829 			/*
1830 			 * print warning and skip to next triplet.
1831 			 */
1832 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
1833 			    "data property (%s) incomplete\n",
1834 			    datanameptr);
1835 			kmem_free(data_list, data_list_len);
1836 			continue;
1837 		}
1838 
1839 		if (st_validate_conf_data(un, data_ptr,
1840 		    data_list_len / sizeof (int), datanameptr)) {
1841 			kmem_free(data_list, data_list_len);
1842 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
1843 			    "data property (%s) rejected\n",
1844 			    datanameptr);
1845 			continue;
1846 		}
1847 
1848 		/*
1849 		 * check version
1850 		 */
1851 		version = *data_ptr++;
1852 		if (version != 1 && version != 2) {
1853 			/* print warning but accept it */
1854 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
1855 			    "Version # for data property (%s) "
1856 			    "not set to 1 or 2\n", datanameptr);
1857 		}
1858 
1859 		dp->type    = *data_ptr++;
1860 		dp->bsize   = *data_ptr++;
1861 		dp->options = *data_ptr++;
1862 		dp->options |= ST_DYNAMIC;
1863 		len = *data_ptr++;
1864 		for (i = 0; i < NDENSITIES; i++) {
1865 			if (i < len) {
1866 				dp->densities[i] = *data_ptr++;
1867 			}
1868 		}
1869 		dp->default_density = *data_ptr << 3;
1870 		if (version == 2 &&
1871 		    data_list_len >= (13 + len) * sizeof (int)) {
1872 			data_ptr++;
1873 			dp->non_motion_timeout	= *data_ptr++;
1874 			dp->io_timeout		= *data_ptr++;
1875 			dp->rewind_timeout	= *data_ptr++;
1876 			dp->space_timeout	= *data_ptr++;
1877 			dp->load_timeout	= *data_ptr++;
1878 			dp->unload_timeout	= *data_ptr++;
1879 			dp->erase_timeout	= *data_ptr++;
1880 		}
1881 		kmem_free(data_list, data_list_len);
1882 		found = 1;
1883 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
1884 		    "found in st.conf: vid = %s, pretty=%s\n",
1885 		    dp->vid, dp->name);
1886 		break;
1887 	}
1888 
1889 	/*
1890 	 * free up the memory allocated by ddi_getlongprop
1891 	 */
1892 	if (config_list) {
1893 		kmem_free(config_list, config_list_len);
1894 	}
1895 	return (found);
1896 }
1897 
1898 static int
1899 st_get_conf_from_st_conf_dot_c(struct scsi_tape *un, char *vidpid,
1900     struct st_drivetype *dp)
1901 {
1902 	int i;
1903 
1904 	/*
1905 	 * Determine type of tape controller.  Type is determined by
1906 	 * checking the result of the earlier inquiry command and
1907 	 * comparing vendor ids with strings in a table declared in st_conf.c.
1908 	 */
1909 	ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
1910 	    "st_get_conf_from_st_conf_dot_c(): looking at st_drivetypes\n");
1911 
1912 	for (i = 0; i < st_ndrivetypes; i++) {
1913 		if (st_drivetypes[i].length == 0) {
1914 			continue;
1915 		}
1916 		if (strncasecmp(vidpid, st_drivetypes[i].vid,
1917 		    st_drivetypes[i].length)) {
1918 			continue;
1919 		}
1920 		bcopy(&st_drivetypes[i], dp, sizeof (st_drivetypes[i]));
1921 		return (1);
1922 	}
1923 	return (0);
1924 }
1925 
1926 static int
1927 st_get_default_conf(struct scsi_tape *un, char *vidpid, struct st_drivetype *dp)
1928 {
1929 	int i;
1930 
1931 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
1932 	    "st_get_default_conf(): making drivetype from INQ cmd\n");
1933 
1934 
1935 	/*
1936 	 * Make up a name
1937 	 */
1938 	bcopy("Vendor '", dp->name, 8);
1939 	bcopy(vidpid, &dp->name[8], VIDLEN);
1940 	bcopy("' Product '", &dp->name[16], 11);
1941 	bcopy(&vidpid[8], &dp->name[27], PIDLEN);
1942 	dp->name[ST_NAMESIZE - 2] = '\'';
1943 	dp->name[ST_NAMESIZE - 1] = '\0';
1944 	dp->length = min(strlen(ST_INQUIRY->inq_vid), (VIDPIDLEN - 1));
1945 	(void) strncpy(dp->vid, ST_INQUIRY->inq_vid, dp->length);
1946 	/*
1947 	 * 'clean' vendor and product strings of non-printing chars
1948 	 */
1949 	for (i = 0; i < ST_NAMESIZE - 2; i++) {
1950 		if (dp->name[i] < ' ' || dp->name[i] > '~') {
1951 			dp->name[i] = '.';
1952 		}
1953 	}
1954 	dp->type = ST_TYPE_INVALID;
1955 	dp->options |= (ST_DYNAMIC | ST_UNLOADABLE | ST_MODE_SEL_COMP);
1956 
1957 	return (1); /* Can Not Fail */
1958 }
1959 
1960 /*
1961  * Regular Unix Entry points
1962  */
1963 
1964 
1965 
1966 /* ARGSUSED */
1967 static int
1968 st_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p)
1969 {
1970 	dev_t dev = *dev_p;
1971 	int rval = 0;
1972 
1973 	GET_SOFT_STATE(dev);
1974 
1975 	/*
1976 	 * validate that we are addressing a sensible unit
1977 	 */
1978 	mutex_enter(ST_MUTEX);
1979 
1980 #ifdef	STDEBUG
1981 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
1982 	    "st_open(node = %s dev = 0x%lx, flag = %d, otyp = %d)\n",
1983 	    st_dev_name(dev), *dev_p, flag, otyp);
1984 #endif
1985 
1986 	/*
1987 	 * All device accesss go thru st_strategy() where we check
1988 	 * suspend status
1989 	 */
1990 
1991 	if (!un->un_attached) {
1992 		st_known_tape_type(un);
1993 		if (!un->un_attached) {
1994 			rval = ENXIO;
1995 			goto exit;
1996 		}
1997 
1998 	}
1999 
2000 	/*
2001 	 * Check for the case of the tape in the middle of closing.
2002 	 * This isn't simply a check of the current state, because
2003 	 * we could be in state of sensing with the previous state
2004 	 * that of closing.
2005 	 *
2006 	 * And don't allow multiple opens.
2007 	 */
2008 	if (!(flag & (FNDELAY | FNONBLOCK)) && IS_CLOSING(un)) {
2009 		un->un_laststate = un->un_state;
2010 		un->un_state = ST_STATE_CLOSE_PENDING_OPEN;
2011 		while (IS_CLOSING(un) ||
2012 		    un->un_state == ST_STATE_CLOSE_PENDING_OPEN) {
2013 			if (cv_wait_sig(&un->un_clscv, ST_MUTEX) == 0) {
2014 				rval = EINTR;
2015 				un->un_state = un->un_laststate;
2016 				goto exit;
2017 			}
2018 		}
2019 	} else if (un->un_state != ST_STATE_CLOSED) {
2020 		rval = EBUSY;
2021 		goto busy;
2022 	}
2023 
2024 	/*
2025 	 * record current dev
2026 	 */
2027 	un->un_dev = dev;
2028 	un->un_oflags = flag;	/* save for use in st_tape_init() */
2029 	un->un_errno = 0;	/* no errors yet */
2030 	un->un_restore_pos = 0;
2031 	un->un_rqs_state = 0;
2032 
2033 	/*
2034 	 * If we are opening O_NDELAY, or O_NONBLOCK, we don't check for
2035 	 * anything, leave internal states alone, if fileno >= 0
2036 	 */
2037 	if (flag & (FNDELAY | FNONBLOCK)) {
2038 		if (un->un_fileno < 0 || (un->un_fileno == 0 &&
2039 		    un->un_blkno == 0)) {
2040 			un->un_state = ST_STATE_OFFLINE;
2041 		} else if (un->un_fileno > 0 ||
2042 		    (un->un_fileno == 0 && un->un_blkno != 0)) {
2043 			/*
2044 			 * set un_read_only/write-protect status.
2045 			 *
2046 			 * If the tape is not bot we can assume
2047 			 * that mspl->wp_status is set properly.
2048 			 * else
2049 			 * we need to do a mode sense/Tur once
2050 			 * again to get the actual tape status.(since
2051 			 * user might have replaced the tape)
2052 			 * Hence make the st state OFFLINE so that
2053 			 * we re-intialize the tape once again.
2054 			 */
2055 			un->un_read_only =
2056 			    (un->un_oflags & FWRITE) ? RDWR : RDONLY;
2057 			un->un_state = ST_STATE_OPEN_PENDING_IO;
2058 		} else {
2059 			un->un_state = ST_STATE_OFFLINE;
2060 		}
2061 		rval = 0;
2062 	} else {
2063 		/*
2064 		 * Not opening O_NDELAY.
2065 		 */
2066 		un->un_state = ST_STATE_OPENING;
2067 
2068 		rval = st_tape_init(dev);
2069 		if ((rval == EACCES) && (un->un_read_only & WORM)) {
2070 			un->un_state = ST_STATE_OPEN_PENDING_IO;
2071 			rval = 0; /* so open doesn't fail */
2072 		} else if (rval) {
2073 			/*
2074 			 * Release the tape unit, if reserved and not
2075 			 * preserve reserve.
2076 			 */
2077 			if ((un->un_rsvd_status &
2078 			    (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) {
2079 				(void) st_reserve_release(un, ST_RELEASE);
2080 			}
2081 		} else {
2082 			un->un_state = ST_STATE_OPEN_PENDING_IO;
2083 		}
2084 	}
2085 
2086 exit:
2087 	/*
2088 	 * we don't want any uninvited guests scrogging our data when we're
2089 	 * busy with something, so for successful opens or failed opens
2090 	 * (except for EBUSY), reset these counters and state appropriately.
2091 	 */
2092 	if (rval != EBUSY) {
2093 		if (rval) {
2094 			un->un_state = ST_STATE_CLOSED;
2095 		}
2096 		un->un_err_resid = 0;
2097 		un->un_retry_ct = 0;
2098 		un->un_tran_retry_ct = 0;
2099 	}
2100 busy:
2101 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
2102 	    "st_open: return val = %x, state = %d\n", rval, un->un_state);
2103 	mutex_exit(ST_MUTEX);
2104 	return (rval);
2105 
2106 }
2107 
2108 static int
2109 st_tape_init(dev_t dev)
2110 {
2111 	int err;
2112 	int rval = 0;
2113 
2114 	GET_SOFT_STATE(dev);
2115 
2116 	ASSERT(mutex_owned(ST_MUTEX));
2117 
2118 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
2119 	    "st_tape_init(dev = 0x%lx, oflags = %d)\n", dev, un->un_oflags);
2120 
2121 	/*
2122 	 * Clean up after any errors left by 'last' close.
2123 	 * This also handles the case of the initial open.
2124 	 */
2125 	if (un->un_state != ST_STATE_INITIALIZING) {
2126 		un->un_laststate = un->un_state;
2127 		un->un_state = ST_STATE_OPENING;
2128 	}
2129 
2130 	un->un_kbytes_xferred = 0;
2131 
2132 	/*
2133 	 * do a throw away TUR to clear check condition
2134 	 */
2135 	err = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD);
2136 
2137 	/*
2138 	 * If test unit ready fails because the drive is reserved
2139 	 * by another host fail the open for no access.
2140 	 */
2141 	if (err) {
2142 		if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) {
2143 			un->un_state = ST_STATE_CLOSED;
2144 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
2145 			    "st_tape_init: RESERVATION CONFLICT\n");
2146 			rval = EACCES;
2147 			goto exit;
2148 		}
2149 	}
2150 
2151 	/*
2152 	 * See whether this is a generic device that we haven't figured
2153 	 * anything out about yet.
2154 	 */
2155 	if (un->un_dp->type == ST_TYPE_INVALID) {
2156 		rval = st_determine_generic(dev);
2157 		if (rval) {
2158 			if (rval != EACCES) {
2159 				rval = EIO;
2160 			}
2161 			un->un_state = ST_STATE_CLOSED;
2162 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
2163 			    "st_tape_init: %s invalid type\n",
2164 			    rval == EACCES ? "EACCES" : "EIO");
2165 			goto exit;
2166 		}
2167 		/*
2168 		 * If this is a Unknown Type drive,
2169 		 * Use the READ BLOCK LIMITS to determine if
2170 		 * allow large xfer is approprate if not globally
2171 		 * disabled with st_allow_large_xfer.
2172 		 */
2173 		un->un_allow_large_xfer = (uchar_t)st_allow_large_xfer;
2174 	} else {
2175 
2176 		/*
2177 		 * If we allow_large_xfer (ie >64k) and have not yet found out
2178 		 * the max block size supported by the drive,
2179 		 * find it by issueing a READ_BLKLIM command.
2180 		 * if READ_BLKLIM cmd fails, assume drive doesn't
2181 		 * allow_large_xfer and min/max block sizes as 1 byte and 63k.
2182 		 */
2183 		un->un_allow_large_xfer = st_allow_large_xfer &&
2184 		    (un->un_dp->options & ST_NO_RECSIZE_LIMIT);
2185 	}
2186 	/*
2187 	 * if maxbsize is unknown, set the maximum block size.
2188 	 */
2189 	if (un->un_maxbsize == MAXBSIZE_UNKNOWN) {
2190 
2191 		/*
2192 		 * Get the Block limits of the tape drive.
2193 		 * if un->un_allow_large_xfer = 0 , then make sure
2194 		 * that maxbsize is <= ST_MAXRECSIZE_FIXED.
2195 		 */
2196 		un->un_rbl = kmem_zalloc(RBLSIZE, KM_SLEEP);
2197 
2198 		err = st_cmd(dev, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD);
2199 		if (err) {
2200 			/* Retry */
2201 			err = st_cmd(dev, SCMD_READ_BLKLIM, RBLSIZE, SYNC_CMD);
2202 		}
2203 		if (!err) {
2204 
2205 			/*
2206 			 * if cmd successful, use limit returned
2207 			 */
2208 			un->un_maxbsize = (un->un_rbl->max_hi << 16) +
2209 					(un->un_rbl->max_mid << 8) +
2210 					un->un_rbl->max_lo;
2211 			un->un_minbsize = (un->un_rbl->min_hi << 8) +
2212 					un->un_rbl->min_lo;
2213 			un->un_data_mod = 1 << un->un_rbl->granularity;
2214 			if ((un->un_maxbsize == 0) ||
2215 			    (un->un_allow_large_xfer == 0 &&
2216 			    un->un_maxbsize > ST_MAXRECSIZE_FIXED)) {
2217 				un->un_maxbsize = ST_MAXRECSIZE_FIXED;
2218 
2219 			} else if (un->un_dp->type == ST_TYPE_DEFAULT) {
2220 				/*
2221 				 * Drive is not one that is configured, But the
2222 				 * READ BLOCK LIMITS tells us it can do large
2223 				 * xfers.
2224 				 */
2225 				if (un->un_maxbsize > ST_MAXRECSIZE_FIXED) {
2226 					un->un_dp->options |=
2227 					    ST_NO_RECSIZE_LIMIT;
2228 				}
2229 				/*
2230 				 * If max and mimimum block limits are the
2231 				 * same this is a fixed block size device.
2232 				 */
2233 				if (un->un_maxbsize == un->un_minbsize) {
2234 					un->un_dp->options &= ~ST_VARIABLE;
2235 				}
2236 			}
2237 
2238 			if (un->un_minbsize == 0) {
2239 				un->un_minbsize = 1;
2240 			}
2241 
2242 		} else { /* error on read block limits */
2243 
2244 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
2245 				"!st_tape_init: Error on READ BLOCK LIMITS,"
2246 				" errno = %d un_rsvd_status = 0x%X\n",
2247 				err, un->un_rsvd_status);
2248 
2249 			/*
2250 			 * since read block limits cmd failed,
2251 			 * do not allow large xfers.
2252 			 * use old values in st_minphys
2253 			 */
2254 			if (un->un_rsvd_status & ST_RESERVATION_CONFLICT) {
2255 				rval = EACCES;
2256 			} else {
2257 				un->un_allow_large_xfer = 0;
2258 				scsi_log(ST_DEVINFO, st_label, CE_NOTE,
2259 					"!Disabling large transfers\n");
2260 
2261 				/*
2262 				 * we guess maxbsize and minbsize
2263 				 */
2264 				if (un->un_bsize) {
2265 					un->un_maxbsize = un->un_minbsize =
2266 						un->un_bsize;
2267 				} else {
2268 					un->un_maxbsize = ST_MAXRECSIZE_FIXED;
2269 					un->un_minbsize = 1;
2270 				}
2271 				/*
2272 				 * Data Mod must be set,
2273 				 * Even if read block limits fails.
2274 				 * Prevents Divide By Zero in st_rw().
2275 				 */
2276 				un->un_data_mod = 1;
2277 			}
2278 		}
2279 		if (un->un_rbl) {
2280 			kmem_free(un->un_rbl, RBLSIZE);
2281 			un->un_rbl = NULL;
2282 		}
2283 
2284 		if (rval) {
2285 			goto exit;
2286 		}
2287 	}
2288 
2289 	ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
2290 	    "maxdma = %d, maxbsize = %d, minbsize = %d, %s large xfer\n",
2291 	    un->un_maxdma, un->un_maxbsize, un->un_minbsize,
2292 	    (un->un_allow_large_xfer ? "ALLOW": "DON'T ALLOW"));
2293 
2294 	err = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD);
2295 
2296 	if (err != 0) {
2297 		if (err == EINTR) {
2298 			un->un_laststate = un->un_state;
2299 			un->un_state = ST_STATE_CLOSED;
2300 			rval = EINTR;
2301 			goto exit;
2302 		}
2303 		/*
2304 		 * Make sure the tape is ready
2305 		 */
2306 		un->un_fileno = -1;
2307 		if (un->un_status != KEY_UNIT_ATTENTION) {
2308 			/*
2309 			 * allow open no media.  Subsequent MTIOCSTATE
2310 			 * with media present will complete the open
2311 			 * logic.
2312 			 */
2313 			un->un_laststate = un->un_state;
2314 			if (un->un_oflags & (FNONBLOCK|FNDELAY)) {
2315 				un->un_mediastate = MTIO_EJECTED;
2316 				un->un_state = ST_STATE_OFFLINE;
2317 				rval = 0;
2318 				goto exit;
2319 			} else {
2320 				un->un_state = ST_STATE_CLOSED;
2321 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
2322 				    "st_tape_init EIO no media, not opened "
2323 				    "O_NONBLOCK|O_EXCL\n");
2324 				rval = EIO;
2325 				goto exit;
2326 			}
2327 		}
2328 	}
2329 
2330 	/*
2331 	 * On each open, initialize block size from drivetype struct,
2332 	 * as it could have been changed by MTSRSZ ioctl.
2333 	 * Now, ST_VARIABLE simply means drive is capable of variable
2334 	 * mode. All drives are assumed to support fixed records.
2335 	 * Hence, un_bsize tells what mode the drive is in.
2336 	 *	un_bsize	= 0	- variable record length
2337 	 *			= x	- fixed record length is x
2338 	 */
2339 	un->un_bsize = un->un_dp->bsize;
2340 
2341 	if (un->un_restore_pos) {
2342 		rval = st_validate_tapemarks(un, un->un_save_fileno,
2343 		    un->un_save_blkno);
2344 		if (rval != 0) {
2345 			if (rval != EACCES) {
2346 				rval = EIO;
2347 			}
2348 			un->un_restore_pos = 0;
2349 			un->un_laststate = un->un_state;
2350 			un->un_state = ST_STATE_CLOSED;
2351 			goto exit;
2352 		}
2353 		un->un_restore_pos = 0;
2354 	}
2355 
2356 	if (un->un_fileno < 0) {
2357 		rval = st_loadtape(dev);
2358 		if (rval) {
2359 			if (rval != EACCES) {
2360 				rval = EIO;
2361 			}
2362 			un->un_laststate = un->un_state;
2363 			un->un_state = ST_STATE_CLOSED;
2364 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
2365 			    "st_tape_init: %s can't open tape\n",
2366 			    rval == EACCES ? "EACCES" : "EIO");
2367 			goto exit;
2368 		}
2369 	}
2370 
2371 	/*
2372 	 * do a mode sense to pick up state of current write-protect,
2373 	 * Could cause reserve and fail due to conflict.
2374 	 */
2375 	rval = st_modesense(un);
2376 	if (rval == EACCES) {
2377 		goto exit;
2378 	}
2379 
2380 	/*
2381 	 * If we are opening the tape for writing, check
2382 	 * to make sure that the tape can be written.
2383 	 */
2384 	if (un->un_oflags & FWRITE) {
2385 		err = 0;
2386 		if (un->un_mspl->wp) {
2387 			un->un_status = KEY_WRITE_PROTECT;
2388 			un->un_laststate = un->un_state;
2389 			un->un_state = ST_STATE_CLOSED;
2390 			rval = EACCES;
2391 			/*
2392 			 * STK sets the wp bit if volsafe tape is loaded.
2393 			 */
2394 			if ((un->un_dp->type == MT_ISSTK9840) &&
2395 			    (un->un_dp->options & ST_WORMABLE)) {
2396 				un->un_read_only = RDONLY;
2397 			} else {
2398 				goto exit;
2399 			}
2400 		} else {
2401 			un->un_read_only = RDWR;
2402 		}
2403 	} else {
2404 		un->un_read_only = RDONLY;
2405 	}
2406 
2407 	if (un->un_dp->options & ST_WORMABLE) {
2408 		un->un_read_only |= un->un_wormable(un);
2409 
2410 		if (((un->un_read_only == WORM) ||
2411 		    (un->un_read_only == RDWORM)) &&
2412 		    ((un->un_oflags & FWRITE) == FWRITE)) {
2413 			un->un_status = KEY_DATA_PROTECT;
2414 			rval = EACCES;
2415 			ST_DEBUG4(ST_DEVINFO, st_label, CE_NOTE,
2416 			    "read_only = %d eof = %d oflag = %d\n",
2417 			    un->un_read_only, un->un_eof, un->un_oflags);
2418 		}
2419 	}
2420 
2421 	/*
2422 	 * If we're opening the tape write-only, we need to
2423 	 * write 2 filemarks on the HP 1/2 inch drive, to
2424 	 * create a null file.
2425 	 */
2426 	if ((un->un_read_only == RDWR) ||
2427 	    (un->un_read_only == WORM) && (un->un_oflags & FWRITE)) {
2428 		if (un->un_dp->options & ST_REEL) {
2429 			un->un_fmneeded = 2;
2430 		} else {
2431 			un->un_fmneeded = 1;
2432 		}
2433 	} else {
2434 		un->un_fmneeded = 0;
2435 	}
2436 
2437 	ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
2438 	    "fmneeded = %x\n", un->un_fmneeded);
2439 
2440 	/*
2441 	 * Make sure the density can be selected correctly.
2442 	 * If WORM can only write at the append point which in most cases
2443 	 * isn't BOP. st_determine_density() with a B_WRITE only attempts
2444 	 * to set and try densities if a BOP.
2445 	 */
2446 	if (st_determine_density(dev,
2447 	    un->un_read_only == RDWR ? B_WRITE : B_READ)) {
2448 		un->un_status = KEY_ILLEGAL_REQUEST;
2449 		un->un_laststate = un->un_state;
2450 		un->un_state = ST_STATE_CLOSED;
2451 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
2452 		    "st_tape_init: EIO can't determine density\n");
2453 		rval = EIO;
2454 		goto exit;
2455 	}
2456 
2457 	/*
2458 	 * Destroy the knowledge that we have 'determined'
2459 	 * density so that a later read at BOT comes along
2460 	 * does the right density determination.
2461 	 */
2462 
2463 	un->un_density_known = 0;
2464 
2465 
2466 	/*
2467 	 * Okay, the tape is loaded and either at BOT or somewhere past.
2468 	 * Mark the state such that any I/O or tape space operations
2469 	 * will get/set the right density, etc..
2470 	 */
2471 	un->un_laststate = un->un_state;
2472 	un->un_lastop = ST_OP_NIL;
2473 	un->un_mediastate = MTIO_INSERTED;
2474 	cv_broadcast(&un->un_state_cv);
2475 
2476 	/*
2477 	 *  Set test append flag if writing.
2478 	 *  First write must check that tape is positioned correctly.
2479 	 */
2480 	un->un_test_append = (un->un_oflags & FWRITE);
2481 
2482 exit:
2483 	un->un_err_resid = 0;
2484 	un->un_last_resid = 0;
2485 	un->un_last_count = 0;
2486 
2487 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
2488 	    "st_tape_init: return val = %x\n", rval);
2489 	return (rval);
2490 
2491 }
2492 
2493 
2494 
2495 /* ARGSUSED */
2496 static int
2497 st_close(dev_t dev, int flag, int otyp, cred_t *cred_p)
2498 {
2499 	int err = 0;
2500 	int norew, count, last_state;
2501 #if defined(__i386) || defined(__amd64)
2502 	struct contig_mem *cp, *cp_temp;
2503 #endif
2504 
2505 	GET_SOFT_STATE(dev);
2506 
2507 	/*
2508 	 * wait till all cmds in the pipeline have been completed
2509 	 */
2510 	mutex_enter(ST_MUTEX);
2511 
2512 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
2513 	    "st_close(dev = 0x%lx, flag = %d, otyp = %d)\n", dev, flag, otyp);
2514 
2515 	st_wait_for_io(un);
2516 
2517 	/* turn off persistent errors on close, as we want close to succeed */
2518 	TURN_PE_OFF(un);
2519 
2520 	/*
2521 	 * set state to indicate that we are in process of closing
2522 	 */
2523 	last_state = un->un_laststate = un->un_state;
2524 	un->un_state = ST_STATE_CLOSING;
2525 
2526 	/*
2527 	 * BSD behavior:
2528 	 * a close always causes a silent span to the next file if we've hit
2529 	 * an EOF (but not yet read across it).
2530 	 */
2531 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
2532 	    "st_close1: fileno=%x, blkno=%lx, un_eof=%x\n", un->un_fileno,
2533 	    un->un_blkno, un->un_eof);
2534 
2535 
2536 	if (BSD_BEHAVIOR && (un->un_eof == ST_EOF)) {
2537 		if (un->un_fileno >= 0) {
2538 			un->un_fileno++;
2539 			un->un_blkno = 0;
2540 		}
2541 		un->un_eof = ST_NO_EOF;
2542 	}
2543 
2544 	/*
2545 	 * rewinding?
2546 	 */
2547 	norew = (getminor(dev) & MT_NOREWIND);
2548 
2549 	/*
2550 	 * SVR4 behavior for skipping to next file:
2551 	 *
2552 	 * If we have not seen a filemark, space to the next file
2553 	 *
2554 	 * If we have already seen the filemark we are physically in the next
2555 	 * file and we only increment the filenumber
2556 	 */
2557 
2558 	if (norew && SVR4_BEHAVIOR && (flag & FREAD) && (un->un_blkno != 0) &&
2559 	    (un->un_lastop != ST_OP_WRITE)) {
2560 		switch (un->un_eof) {
2561 		case ST_NO_EOF:
2562 			/*
2563 			 * if we were reading and did not read the complete file
2564 			 * skip to the next file, leaving the tape correctly
2565 			 * positioned to read the first record of the next file
2566 			 * Check first for REEL if we are at EOT by trying to
2567 			 * read a block
2568 			 */
2569 			if ((un->un_dp->options & ST_REEL) &&
2570 				(!(un->un_dp->options & ST_READ_IGNORE_EOFS)) &&
2571 			    (un->un_blkno == 0)) {
2572 				if (st_cmd(dev, SCMD_SPACE, Blk(1), SYNC_CMD)) {
2573 					ST_DEBUG2(ST_DEVINFO, st_label,
2574 					    SCSI_DEBUG,
2575 					    "st_close : EIO can't space\n");
2576 					err = EIO;
2577 					break;
2578 				}
2579 				if (un->un_eof >= ST_EOF_PENDING) {
2580 					un->un_eof = ST_EOT_PENDING;
2581 					un->un_fileno += 1;
2582 					un->un_blkno   = 0;
2583 					break;
2584 				}
2585 			}
2586 			if (st_cmd(dev, SCMD_SPACE, Fmk(1), SYNC_CMD)) {
2587 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
2588 				    "st_close: EIO can't space #2\n");
2589 				err = EIO;
2590 			} else {
2591 				ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
2592 				    "st_close2: fileno=%x,blkno=%lx,"
2593 				    "un_eof=%x\n",
2594 				    un->un_fileno, un->un_blkno, un->un_eof);
2595 				un->un_eof = ST_NO_EOF;
2596 			}
2597 			break;
2598 
2599 		case ST_EOF_PENDING:
2600 		case ST_EOF:
2601 			un->un_fileno += 1;
2602 			un->un_blkno   = 0;
2603 			un->un_eof = ST_NO_EOF;
2604 			break;
2605 
2606 		case ST_EOT:
2607 		case ST_EOT_PENDING:
2608 			/* nothing to do */
2609 			break;
2610 		default:
2611 			scsi_log(ST_DEVINFO, st_label, CE_PANIC,
2612 			    "Undefined state 0x%x", un->un_eof);
2613 
2614 		}
2615 	}
2616 
2617 
2618 	/*
2619 	 * For performance reasons (HP 88780), the driver should
2620 	 * postpone writing the second tape mark until just before a file
2621 	 * positioning ioctl is issued (e.g., rewind).	This means that
2622 	 * the user must not manually rewind the tape because the tape will
2623 	 * be missing the second tape mark which marks EOM.
2624 	 * However, this small performance improvement is not worth the risk.
2625 	 */
2626 
2627 	/*
2628 	 * We need to back up over the filemark we inadvertently popped
2629 	 * over doing a read in between the two filemarks that constitute
2630 	 * logical eot for 1/2" tapes. Note that ST_EOT_PENDING is only
2631 	 * set while reading.
2632 	 *
2633 	 * If we happen to be at physical eot (ST_EOM) (writing case),
2634 	 * the writing of filemark(s) will clear the ST_EOM state, which
2635 	 * we don't want, so we save this state and restore it later.
2636 	 */
2637 
2638 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
2639 	    "flag=%x, fmneeded=%x, lastop=%x, eof=%x\n",
2640 		flag, un->un_fmneeded, un->un_lastop, un->un_eof);
2641 
2642 	if (un->un_eof == ST_EOT_PENDING) {
2643 		if (norew) {
2644 			if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) {
2645 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
2646 				    "st_close: EIO can't space #3\n");
2647 				err = EIO;
2648 			} else {
2649 				un->un_blkno = 0;
2650 				un->un_eof = ST_EOT;
2651 			}
2652 		} else {
2653 			un->un_eof = ST_NO_EOF;
2654 		}
2655 
2656 	/*
2657 	 * Do we need to write a file mark?
2658 	 *
2659 	 * only write filemarks if there are fmks to be written and
2660 	 *   - open for write (possibly read/write)
2661 	 *   - the last operation was a write
2662 	 * or:
2663 	 *   -	opened for wronly
2664 	 *   -	no data was written
2665 	 */
2666 	} else if ((un->un_fileno >= 0) && (un->un_fmneeded > 0) &&
2667 	    (((flag & FWRITE) && (un->un_lastop == ST_OP_WRITE)) ||
2668 	    ((flag & FWRITE) && (un->un_lastop == ST_OP_WEOF)) ||
2669 	    ((flag == FWRITE) && (un->un_lastop == ST_OP_NIL)))) {
2670 
2671 		/* save ST_EOM state */
2672 		int was_at_eom = (un->un_eof == ST_EOM) ? 1 : 0;
2673 
2674 		/*
2675 		 * Note that we will write a filemark if we had opened
2676 		 * the tape write only and no data was written, thus
2677 		 * creating a null file.
2678 		 *
2679 		 * If the user already wrote one, we only have to write 1 more.
2680 		 * If they wrote two, we don't have to write any.
2681 		 */
2682 
2683 		count = un->un_fmneeded;
2684 		if (count > 0) {
2685 			if (st_cmd(dev, SCMD_WRITE_FILE_MARK,
2686 			    count, SYNC_CMD)) {
2687 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
2688 				    "st_close : EIO can't wfm\n");
2689 				err = EIO;
2690 			}
2691 			if ((un->un_dp->options & ST_REEL) && norew) {
2692 				if (st_cmd(dev, SCMD_SPACE, Fmk((-1)),
2693 				    SYNC_CMD)) {
2694 					ST_DEBUG2(ST_DEVINFO, st_label,
2695 					    SCSI_DEBUG,
2696 					    "st_close : EIO space fmk(-1)\n");
2697 					err = EIO;
2698 				}
2699 				un->un_eof = ST_NO_EOF;
2700 				/* fix up block number */
2701 				un->un_blkno = 0;
2702 			}
2703 		}
2704 
2705 		/*
2706 		 * If we aren't going to be rewinding, and we were at
2707 		 * physical eot, restore the state that indicates we
2708 		 * are at physical eot. Once you have reached physical
2709 		 * eot, and you close the tape, the only thing you can
2710 		 * do on the next open is to rewind. Access to trailer
2711 		 * records is only allowed without closing the device.
2712 		 */
2713 		if (norew == 0 && was_at_eom)
2714 			un->un_eof = ST_EOM;
2715 	}
2716 
2717 	/*
2718 	 * report soft errors if enabled and available, if we never accessed
2719 	 * the drive, don't get errors. This will prevent some DAT error
2720 	 * messages upon LOG SENSE.
2721 	 */
2722 	if (st_report_soft_errors_on_close &&
2723 	    (un->un_dp->options & ST_SOFT_ERROR_REPORTING) &&
2724 	    (last_state != ST_STATE_OFFLINE)) {
2725 		(void) st_report_soft_errors(dev, flag);
2726 	}
2727 
2728 
2729 	/*
2730 	 * Do we need to rewind? Can we rewind?
2731 	 */
2732 	if (norew == 0 && un->un_fileno >= 0 && err == 0) {
2733 		/*
2734 		 * We'd like to rewind with the
2735 		 * 'immediate' bit set, but this
2736 		 * causes problems on some drives
2737 		 * where subsequent opens get a
2738 		 * 'NOT READY' error condition
2739 		 * back while the tape is rewinding,
2740 		 * which is impossible to distinguish
2741 		 * from the condition of 'no tape loaded'.
2742 		 *
2743 		 * Also, for some targets, if you disconnect
2744 		 * with the 'immediate' bit set, you don't
2745 		 * actually return right away, i.e., the
2746 		 * target ignores your request for immediate
2747 		 * return.
2748 		 *
2749 		 * Instead, we'll fire off an async rewind
2750 		 * command. We'll mark the device as closed,
2751 		 * and any subsequent open will stall on
2752 		 * the first TEST_UNIT_READY until the rewind
2753 		 * completes.
2754 		 */
2755 
2756 		/*
2757 		 * Used to be if reserve was not supported we'd send an
2758 		 * asynchronious rewind. Comments above may be slightly invalid
2759 		 * as the immediate bit was never set. Doing an immedate rewind
2760 		 * makes sense, I think fixes to not ready status might handle
2761 		 * the problems described above.
2762 		 */
2763 		if (un->un_sd->sd_inq->inq_ansi < 2) {
2764 			(void) st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD);
2765 		} else {
2766 			(void) st_cmd(dev, SCMD_REWIND, 0, ASYNC_CMD);
2767 		}
2768 	}
2769 
2770 	/*
2771 	 * eject tape if necessary
2772 	 */
2773 	if (un->un_eject_tape_on_failure) {
2774 		un->un_eject_tape_on_failure = 0;
2775 		if (st_cmd(dev, SCMD_LOAD, 0, SYNC_CMD)) {
2776 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
2777 			    "st_close : can't unload tape\n");
2778 		} else {
2779 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
2780 			    "st_close : tape unloaded \n");
2781 			un->un_eof = ST_NO_EOF;
2782 			un->un_mediastate = MTIO_EJECTED;
2783 		}
2784 	}
2785 	/*
2786 	 * Release the tape unit, if default reserve/release
2787 	 * behaviour.
2788 	 */
2789 	if ((un->un_rsvd_status &
2790 	    (ST_RESERVE | ST_PRESERVE_RESERVE)) == ST_RESERVE) {
2791 		(void) st_reserve_release(un, ST_RELEASE);
2792 	}
2793 
2794 	/*
2795 	 * clear up state
2796 	 */
2797 	un->un_laststate = un->un_state;
2798 	un->un_state = ST_STATE_CLOSED;
2799 	un->un_lastop = ST_OP_NIL;
2800 	un->un_throttle = 1;	/* assume one request at time, for now */
2801 	un->un_retry_ct = 0;
2802 	un->un_tran_retry_ct = 0;
2803 	un->un_errno = 0;
2804 	un->un_swr_token = (opaque_t)NULL;
2805 	un->un_rsvd_status &= ~(ST_INIT_RESERVE);
2806 
2807 	/* Restore the options to the init time settings */
2808 	if (un->un_init_options & ST_READ_IGNORE_ILI) {
2809 		un->un_dp->options |= ST_READ_IGNORE_ILI;
2810 	} else {
2811 		un->un_dp->options &= ~ST_READ_IGNORE_ILI;
2812 	}
2813 
2814 	if (un->un_init_options & ST_READ_IGNORE_EOFS) {
2815 		un->un_dp->options |= ST_READ_IGNORE_EOFS;
2816 	} else {
2817 		un->un_dp->options &= ~ST_READ_IGNORE_EOFS;
2818 	}
2819 
2820 	if (un->un_init_options & ST_SHORT_FILEMARKS) {
2821 		un->un_dp->options |= ST_SHORT_FILEMARKS;
2822 	} else {
2823 		un->un_dp->options &= ~ST_SHORT_FILEMARKS;
2824 	}
2825 
2826 	ASSERT(mutex_owned(ST_MUTEX));
2827 
2828 	/*
2829 	 * Signal anyone awaiting a close operation to complete.
2830 	 */
2831 	cv_signal(&un->un_clscv);
2832 
2833 	/*
2834 	 * any kind of error on closing causes all state to be tossed
2835 	 */
2836 	if (err && un->un_status != KEY_ILLEGAL_REQUEST) {
2837 		un->un_density_known = 0;
2838 		/*
2839 		 * note that st_intr has already set un_fileno to -1
2840 		 */
2841 	}
2842 
2843 #if defined(__i386) || defined(__amd64)
2844 	/*
2845 	 * free any contiguous mem alloc'ed for big block I/O
2846 	 */
2847 	cp = un->un_contig_mem;
2848 	while (cp) {
2849 		if (cp->cm_addr) {
2850 			ddi_dma_mem_free(&cp->cm_acc_hdl);
2851 		}
2852 		cp_temp = cp;
2853 		cp = cp->cm_next;
2854 		kmem_free(cp_temp,
2855 		    sizeof (struct contig_mem) + biosize());
2856 	}
2857 	un->un_contig_mem_total_num = 0;
2858 	un->un_contig_mem_available_num = 0;
2859 	un->un_contig_mem = NULL;
2860 	un->un_max_contig_mem_len = 0;
2861 #endif
2862 
2863 	ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
2864 	    "st_close3: return val = %x, fileno=%x, blkno=%lx, un_eof=%x\n",
2865 			    err, un->un_fileno, un->un_blkno, un->un_eof);
2866 
2867 	mutex_exit(ST_MUTEX);
2868 	return (err);
2869 }
2870 
2871 /*
2872  * These routines perform raw i/o operations.
2873  */
2874 
2875 /* ARGSUSED2 */
2876 static int
2877 st_aread(dev_t dev, struct aio_req *aio, cred_t *cred_p)
2878 {
2879 	return (st_arw(dev, aio, B_READ));
2880 }
2881 
2882 
2883 /* ARGSUSED2 */
2884 static int
2885 st_awrite(dev_t dev, struct aio_req *aio, cred_t *cred_p)
2886 {
2887 	return (st_arw(dev, aio, B_WRITE));
2888 }
2889 
2890 
2891 
2892 /* ARGSUSED */
2893 static int
2894 st_read(dev_t dev, struct uio *uiop, cred_t *cred_p)
2895 {
2896 	return (st_rw(dev, uiop, B_READ));
2897 }
2898 
2899 /* ARGSUSED */
2900 static int
2901 st_write(dev_t dev, struct uio *uiop, cred_t *cred_p)
2902 {
2903 	return (st_rw(dev, uiop, B_WRITE));
2904 }
2905 
2906 /*
2907  * Due to historical reasons, old limits are: For variable-length devices:
2908  * if greater than 64KB - 1 (ST_MAXRECSIZE_VARIABLE), block into 64 KB - 2
2909  * ST_MAXRECSIZE_VARIABLE_LIMIT) requests; otherwise,
2910  * (let it through unmodified. For fixed-length record devices:
2911  * 63K (ST_MAXRECSIZE_FIXED) is max (default minphys).
2912  *
2913  * The new limits used are un_maxdma (retrieved using scsi_ifgetcap()
2914  * from the HBA) and un_maxbsize (retrieved by sending SCMD_READ_BLKLIM
2915  * command to the drive).
2916  *
2917  */
2918 static void
2919 st_minphys(struct buf *bp)
2920 {
2921 	struct scsi_tape *un;
2922 
2923 	un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev));
2924 
2925 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
2926 	    "st_minphys(bp = 0x%p): b_bcount = 0x%lx\n", (void *)bp,
2927 	    bp->b_bcount);
2928 
2929 	if (un->un_allow_large_xfer) {
2930 
2931 		/*
2932 		 * check un_maxbsize for variable length devices only
2933 		 */
2934 		if (un->un_bsize == 0 && bp->b_bcount > un->un_maxbsize) {
2935 			bp->b_bcount = un->un_maxbsize;
2936 		}
2937 		/*
2938 		 * can't go more that HBA maxdma limit in either fixed-length
2939 		 * or variable-length tape drives.
2940 		 */
2941 		if (bp->b_bcount > un->un_maxdma) {
2942 			bp->b_bcount = un->un_maxdma;
2943 		}
2944 	} else {
2945 
2946 		/*
2947 		 *  use old fixed limits
2948 		 */
2949 		if (un->un_bsize == 0) {
2950 			if (bp->b_bcount > ST_MAXRECSIZE_VARIABLE) {
2951 				bp->b_bcount = ST_MAXRECSIZE_VARIABLE_LIMIT;
2952 			}
2953 		} else {
2954 			if (bp->b_bcount > ST_MAXRECSIZE_FIXED) {
2955 				bp->b_bcount = ST_MAXRECSIZE_FIXED;
2956 			}
2957 		}
2958 	}
2959 
2960 	/*
2961 	 * For regular raw I/O and Fixed Block length devices, make sure
2962 	 * the adjusted block count is a whole multiple of the device
2963 	 * block size.
2964 	 */
2965 	if (bp != un->un_sbufp && un->un_bsize) {
2966 		bp->b_bcount -= (bp->b_bcount % un->un_bsize);
2967 	}
2968 }
2969 
2970 /*ARGSUSED*/
2971 static void
2972 st_uscsi_minphys(struct buf *bp)
2973 {
2974 	/*
2975 	 * do not break up because the CDB count would then be
2976 	 * incorrect and create spurious data underrun errors.
2977 	 */
2978 }
2979 
2980 static int
2981 st_rw(dev_t dev, struct uio *uio, int flag)
2982 {
2983 	int rval = 0;
2984 	long len;
2985 
2986 	GET_SOFT_STATE(dev);
2987 
2988 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
2989 	    "st_rw(dev = 0x%lx, flag = %s)\n", dev,
2990 	    (flag == B_READ ? rd_str: wr_str));
2991 
2992 	/* get local copy of transfer length */
2993 	len = uio->uio_iov->iov_len;
2994 
2995 	mutex_enter(ST_MUTEX);
2996 
2997 	/*
2998 	 * If in fixed block size mode and requested read or write
2999 	 * is not an even multiple of that block size.
3000 	 */
3001 	if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) {
3002 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
3003 		    "%s: not modulo %d block size\n",
3004 		    (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize);
3005 		rval = EINVAL;
3006 	}
3007 
3008 	/* If device has set granularity in the READ_BLKLIM we honor it. */
3009 	if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) {
3010 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
3011 		    "%s: not modulo %d device granularity\n",
3012 		    (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod);
3013 		rval = EINVAL;
3014 	}
3015 
3016 	if (rval != 0) {
3017 		un->un_errno = rval;
3018 		mutex_exit(ST_MUTEX);
3019 		return (rval);
3020 	}
3021 
3022 	un->un_silent_skip = 0;
3023 	mutex_exit(ST_MUTEX);
3024 
3025 	len = uio->uio_resid;
3026 
3027 	rval = physio(st_strategy, (struct buf *)NULL,
3028 		dev, flag, st_minphys, uio);
3029 	/*
3030 	 * if we have hit logical EOT during this xfer and there is not a
3031 	 * full residue, then set un_eof back  to ST_EOM to make sure that
3032 	 * the user will see at least one zero write
3033 	 * after this short write
3034 	 */
3035 	mutex_enter(ST_MUTEX);
3036 	if (un->un_eof > ST_NO_EOF) {
3037 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3038 		"un_eof=%d resid=%lx\n", un->un_eof, uio->uio_resid);
3039 	}
3040 	if (un->un_eof >= ST_EOM && (flag == B_WRITE)) {
3041 		if ((uio->uio_resid != len) && (uio->uio_resid != 0)) {
3042 			un->un_eof = ST_EOM;
3043 		} else if (uio->uio_resid == len) {
3044 			un->un_eof = ST_NO_EOF;
3045 		}
3046 	}
3047 
3048 	if (un->un_silent_skip && uio->uio_resid != len) {
3049 		un->un_eof = ST_EOF;
3050 		un->un_blkno = un->un_save_blkno;
3051 		un->un_fileno--;
3052 	}
3053 
3054 	un->un_errno = rval;
3055 
3056 	mutex_exit(ST_MUTEX);
3057 
3058 	return (rval);
3059 }
3060 
3061 static int
3062 st_arw(dev_t dev, struct aio_req *aio, int flag)
3063 {
3064 	struct uio *uio = aio->aio_uio;
3065 	int rval = 0;
3066 	long len;
3067 
3068 	GET_SOFT_STATE(dev);
3069 
3070 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
3071 	    "st_rw(dev = 0x%lx, flag = %s)\n", dev,
3072 	    (flag == B_READ ? rd_str: wr_str));
3073 
3074 	/* get local copy of transfer length */
3075 	len = uio->uio_iov->iov_len;
3076 
3077 	mutex_enter(ST_MUTEX);
3078 
3079 	/*
3080 	 * If in fixed block size mode and requested read or write
3081 	 * is not an even multiple of that block size.
3082 	 */
3083 	if ((un->un_bsize != 0) && (len % un->un_bsize != 0)) {
3084 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
3085 		    "%s: not modulo %d block size\n",
3086 		    (flag == B_WRITE) ? wr_str : rd_str, un->un_bsize);
3087 		rval = EINVAL;
3088 	}
3089 
3090 	/* If device has set granularity in the READ_BLKLIM we honor it. */
3091 	if ((un->un_data_mod != 0) && (len % un->un_data_mod != 0)) {
3092 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
3093 		    "%s: not modulo %d device granularity\n",
3094 		    (flag == B_WRITE) ? wr_str : rd_str, un->un_data_mod);
3095 		rval = EINVAL;
3096 	}
3097 
3098 	if (rval != 0) {
3099 		un->un_errno = rval;
3100 		mutex_exit(ST_MUTEX);
3101 		return (rval);
3102 	}
3103 
3104 	mutex_exit(ST_MUTEX);
3105 
3106 	len = uio->uio_resid;
3107 
3108 	rval = aphysio(st_strategy, anocancel, dev, flag, st_minphys, aio);
3109 
3110 	/*
3111 	 * if we have hit logical EOT during this xfer and there is not a
3112 	 * full residue, then set un_eof back  to ST_EOM to make sure that
3113 	 * the user will see at least one zero write
3114 	 * after this short write
3115 	 *
3116 	 * we keep this here just in case the application is not using
3117 	 * persistent errors
3118 	 */
3119 	mutex_enter(ST_MUTEX);
3120 	if (un->un_eof > ST_NO_EOF) {
3121 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3122 		    "un_eof=%d resid=%lx\n", un->un_eof, uio->uio_resid);
3123 	}
3124 	if (un->un_eof >= ST_EOM && (flag == B_WRITE)) {
3125 		if ((uio->uio_resid != len) && (uio->uio_resid != 0)) {
3126 			un->un_eof = ST_EOM;
3127 		} else if (uio->uio_resid == len && !IS_PE_FLAG_SET(un)) {
3128 			un->un_eof = ST_NO_EOF;
3129 		}
3130 	}
3131 	un->un_errno = rval;
3132 	mutex_exit(ST_MUTEX);
3133 
3134 	return (rval);
3135 }
3136 
3137 
3138 
3139 static int
3140 st_strategy(struct buf *bp)
3141 {
3142 	struct scsi_tape *un;
3143 	dev_t dev = bp->b_edev;
3144 
3145 	/*
3146 	 * validate arguments
3147 	 */
3148 	if ((un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev))) == NULL) {
3149 		bp->b_resid = bp->b_bcount;
3150 		mutex_enter(ST_MUTEX);
3151 		st_bioerror(bp, ENXIO);
3152 		mutex_exit(ST_MUTEX);
3153 		goto error;
3154 	}
3155 
3156 	mutex_enter(ST_MUTEX);
3157 
3158 	while (un->un_pwr_mgmt == ST_PWR_SUSPENDED) {
3159 		cv_wait(&un->un_suspend_cv, ST_MUTEX);
3160 	}
3161 
3162 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
3163 	    "st_strategy(): bcount=0x%lx, fileno=%d, blkno=%lx, eof=%d\n",
3164 	    bp->b_bcount, un->un_fileno, un->un_blkno, un->un_eof);
3165 
3166 	/*
3167 	 * If persistent errors have been flagged, just nix this one. We wait
3168 	 * for any outstanding I/O's below, so we will be in order.
3169 	 */
3170 	if (IS_PE_FLAG_SET(un))
3171 		goto exit;
3172 
3173 	if (bp != un->un_sbufp) {
3174 		char reading = bp->b_flags & B_READ;
3175 		int wasopening = 0;
3176 
3177 		/*
3178 		 * If we haven't done/checked reservation on the tape unit
3179 		 * do it now.
3180 		 */
3181 		if ((un->un_rsvd_status &
3182 		    (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) {
3183 			if ((un->un_dp->options & ST_NO_RESERVE_RELEASE) == 0) {
3184 				if (st_reserve_release(un, ST_RESERVE)) {
3185 					st_bioerror(bp, un->un_errno);
3186 					goto exit;
3187 				}
3188 			} else if (un->un_state == ST_STATE_OPEN_PENDING_IO) {
3189 				/*
3190 				 * Enter here to restore position for possible
3191 				 * resets when the device was closed and opened
3192 				 * in O_NDELAY mode subsequently
3193 				 */
3194 				un->un_state = ST_STATE_INITIALIZING;
3195 				(void) st_cmd(dev, SCMD_TEST_UNIT_READY,
3196 				    0, SYNC_CMD);
3197 				un->un_state = ST_STATE_OPEN_PENDING_IO;
3198 			}
3199 			un->un_rsvd_status |= ST_INIT_RESERVE;
3200 		}
3201 
3202 		/*
3203 		 * If we are offline, we have to initialize everything first.
3204 		 * This is to handle either when opened with O_NDELAY, or
3205 		 * we just got a new tape in the drive, after an offline.
3206 		 * We don't observe O_NDELAY past the open,
3207 		 * as it will not make sense for tapes.
3208 		 */
3209 		if (un->un_state == ST_STATE_OFFLINE || un->un_restore_pos) {
3210 			/* reset state to avoid recursion */
3211 			un->un_state = ST_STATE_INITIALIZING;
3212 			if (st_tape_init(dev)) {
3213 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
3214 				    "stioctl : OFFLINE init failure ");
3215 				un->un_state = ST_STATE_OFFLINE;
3216 				un->un_fileno = -1;
3217 				goto b_done_err;
3218 			}
3219 			un->un_state = ST_STATE_OPEN_PENDING_IO;
3220 		}
3221 		/*
3222 		 * Check for legal operations
3223 		 */
3224 		if (un->un_fileno < 0) {
3225 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3226 			    "strategy with un->un_fileno < 0\n");
3227 			goto b_done_err;
3228 		}
3229 
3230 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3231 		    "st_strategy(): regular io\n");
3232 
3233 		/*
3234 		 * Process this first. If we were reading, and we're pending
3235 		 * logical eot, that means we've bumped one file mark too far.
3236 		 */
3237 
3238 		/*
3239 		 * Recursion warning: st_cmd will route back through here.
3240 		 */
3241 		if (un->un_eof == ST_EOT_PENDING) {
3242 			if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) {
3243 				un->un_fileno = -1;
3244 				un->un_density_known = 0;
3245 				goto b_done_err;
3246 			}
3247 			un->un_blkno = 0; /* fix up block number.. */
3248 			un->un_eof = ST_EOT;
3249 		}
3250 
3251 		/*
3252 		 * If we are in the process of opening, we may have to
3253 		 * determine/set the correct density. We also may have
3254 		 * to do a test_append (if QIC) to see whether we are
3255 		 * in a position to append to the end of the tape.
3256 		 *
3257 		 * If we're already at logical eot, we transition
3258 		 * to ST_NO_EOF. If we're at physical eot, we punt
3259 		 * to the switch statement below to handle.
3260 		 */
3261 		if ((un->un_state == ST_STATE_OPEN_PENDING_IO) ||
3262 		    (un->un_test_append && (un->un_dp->options & ST_QIC))) {
3263 
3264 			if (un->un_state == ST_STATE_OPEN_PENDING_IO) {
3265 				if (st_determine_density(dev, (int)reading)) {
3266 					goto b_done_err;
3267 				}
3268 			}
3269 
3270 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3271 			    "pending_io@fileno %d rw %d qic %d eof %d\n",
3272 			    un->un_fileno, (int)reading,
3273 			    (un->un_dp->options & ST_QIC) ? 1 : 0,
3274 			    un->un_eof);
3275 
3276 			if (!reading && un->un_eof != ST_EOM) {
3277 				if (un->un_eof == ST_EOT) {
3278 					un->un_eof = ST_NO_EOF;
3279 				} else if (un->un_fileno > 0 &&
3280 				    (un->un_dp->options & ST_QIC)) {
3281 					/*
3282 					 * st_test_append() will do it all
3283 					 */
3284 					st_test_append(bp);
3285 					goto done;
3286 				}
3287 			}
3288 			if (un->un_state == ST_STATE_OPEN_PENDING_IO) {
3289 				wasopening = 1;
3290 			}
3291 			un->un_laststate = un->un_state;
3292 			un->un_state = ST_STATE_OPEN;
3293 		}
3294 
3295 
3296 		/*
3297 		 * Process rest of END OF FILE and END OF TAPE conditions
3298 		 */
3299 
3300 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3301 		    "un_eof=%x, wasopening=%x\n",
3302 		    un->un_eof, wasopening);
3303 
3304 		switch (un->un_eof) {
3305 		case ST_EOM:
3306 			/*
3307 			 * This allows writes to proceed past physical
3308 			 * eot. We'll *really* be in trouble if the
3309 			 * user continues blindly writing data too
3310 			 * much past this point (unwind the tape).
3311 			 * Physical eot really means 'early warning
3312 			 * eot' in this context.
3313 			 *
3314 			 * Every other write from now on will succeed
3315 			 * (if sufficient  tape left).
3316 			 * This write will return with resid == count
3317 			 * but the next one should be successful
3318 			 *
3319 			 * Note that we only transition to logical EOT
3320 			 * if the last state wasn't the OPENING state.
3321 			 * We explicitly prohibit running up to physical
3322 			 * eot, closing the device, and then re-opening
3323 			 * to proceed. Trailer records may only be gotten
3324 			 * at by keeping the tape open after hitting eot.
3325 			 *
3326 			 * Also note that ST_EOM cannot be set by reading-
3327 			 * this can only be set during writing. Reading
3328 			 * up to the end of the tape gets a blank check
3329 			 * or a double-filemark indication (ST_EOT_PENDING),
3330 			 * and we prohibit reading after that point.
3331 			 *
3332 			 */
3333 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOM\n");
3334 			if (wasopening == 0) {
3335 				/*
3336 				 * this allows st_rw() to reset it back to
3337 				 * ST_EOM to make sure that the application
3338 				 * will see a zero write
3339 				 */
3340 				un->un_eof = ST_WRITE_AFTER_EOM;
3341 			}
3342 			un->un_status = SUN_KEY_EOT;
3343 			goto b_done;
3344 
3345 		case ST_WRITE_AFTER_EOM:
3346 		case ST_EOT:
3347 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOT\n");
3348 			un->un_status = SUN_KEY_EOT;
3349 			if (SVR4_BEHAVIOR && reading) {
3350 				goto b_done_err;
3351 			}
3352 
3353 			if (reading) {
3354 				goto b_done;
3355 			}
3356 			un->un_eof = ST_NO_EOF;
3357 			break;
3358 
3359 		case ST_EOF_PENDING:
3360 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3361 			    "EOF PENDING\n");
3362 			un->un_status = SUN_KEY_EOF;
3363 			if (SVR4_BEHAVIOR) {
3364 				un->un_eof = ST_EOF;
3365 				goto b_done;
3366 			}
3367 			/* FALLTHROUGH */
3368 		case ST_EOF:
3369 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "EOF\n");
3370 			un->un_status = SUN_KEY_EOF;
3371 			if (SVR4_BEHAVIOR) {
3372 				goto b_done_err;
3373 			}
3374 
3375 			if (BSD_BEHAVIOR) {
3376 				un->un_eof = ST_NO_EOF;
3377 				un->un_fileno += 1;
3378 				un->un_blkno   = 0;
3379 			}
3380 
3381 			if (reading) {
3382 				ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3383 				    "now file %d (read)\n",
3384 				    un->un_fileno);
3385 				goto b_done;
3386 			}
3387 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3388 			    "now file %d (write)\n", un->un_fileno);
3389 			break;
3390 		default:
3391 			un->un_status = 0;
3392 			break;
3393 		}
3394 	}
3395 
3396 	bp->b_flags &= ~(B_DONE);
3397 	st_bioerror(bp, 0);
3398 	bp->av_forw = NULL;
3399 	bp->b_resid = 0;
3400 	SET_BP_PKT(bp, 0);
3401 
3402 
3403 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3404 	    "st_strategy: cmd=0x%p  count=%ld  resid=%ld flags=0x%x"
3405 	    " pkt=0x%p\n",
3406 	    (void *)bp->b_forw, bp->b_bcount,
3407 	    bp->b_resid, bp->b_flags, (void *)BP_PKT(bp));
3408 
3409 #if defined(__i386) || defined(__amd64)
3410 	/*
3411 	 * We will replace bp with a new bp that can do big blk xfer
3412 	 * if the requested xfer size is bigger than ST_BIGBLK_XFER
3413 	 *
3414 	 * Also, we need to make sure that we're handling real I/O
3415 	 * by checking group 0/1 SCSI I/O commands, if needed
3416 	 */
3417 	if (bp->b_bcount > ST_BIGBLK_XFER &&
3418 	    (bp != un->un_sbufp					||
3419 	    (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ		||
3420 	    (uchar_t)(uintptr_t)bp->b_forw == SCMD_READ_G1	||
3421 	    (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE	||
3422 	    (uchar_t)(uintptr_t)bp->b_forw == SCMD_WRITE_G1)) {
3423 		mutex_exit(ST_MUTEX);
3424 		bp = st_get_bigblk_bp(bp);
3425 		mutex_enter(ST_MUTEX);
3426 	}
3427 #endif
3428 
3429 	/* put on wait queue */
3430 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3431 	    "st_strategy: un->un_quef = 0x%p, bp = 0x%p\n",
3432 	    (void *)un->un_quef, (void *)bp);
3433 
3434 	if (un->un_quef) {
3435 		un->un_quel->b_actf = bp;
3436 	} else {
3437 		un->un_quef = bp;
3438 	}
3439 	un->un_quel = bp;
3440 
3441 	ST_DO_KSTATS(bp, kstat_waitq_enter);
3442 
3443 	st_start(un);
3444 
3445 done:
3446 	mutex_exit(ST_MUTEX);
3447 	return (0);
3448 
3449 
3450 error:
3451 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3452 	    "st_strategy: error exit\n");
3453 
3454 	biodone(bp);
3455 	return (0);
3456 
3457 b_done_err:
3458 	st_bioerror(bp, EIO);
3459 	ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
3460 	    "st_strategy : EIO b_done_err\n");
3461 
3462 b_done:
3463 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3464 	    "st_strategy: b_done\n");
3465 
3466 exit:
3467 	/*
3468 	 * make sure no commands are outstanding or waiting before closing,
3469 	 * so we can guarantee order
3470 	 */
3471 	st_wait_for_io(un);
3472 	un->un_err_resid = bp->b_resid = bp->b_bcount;
3473 
3474 	/* override errno here, if persistent errors were flagged */
3475 	if (IS_PE_FLAG_SET(un))
3476 		bioerror(bp, un->un_errno);
3477 
3478 	mutex_exit(ST_MUTEX);
3479 
3480 	biodone(bp);
3481 	ASSERT(mutex_owned(ST_MUTEX) == 0);
3482 	return (0);
3483 }
3484 
3485 
3486 
3487 /*
3488  * this routine spaces forward over filemarks
3489  */
3490 static int
3491 st_space_fmks(dev_t dev, int count)
3492 {
3493 	int rval = 0;
3494 
3495 	GET_SOFT_STATE(dev);
3496 
3497 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
3498 	    "st_space_fmks(dev = 0x%lx, count = %d)\n", dev, count);
3499 
3500 	ASSERT(mutex_owned(ST_MUTEX));
3501 
3502 	/*
3503 	 * the risk with doing only one space operation is that we
3504 	 * may accidentily jump in old data
3505 	 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD
3506 	 * because the 8200 does not append a marker; in order not to
3507 	 * sacrifice the fast file skip, we do a slow skip if the low
3508 	 * density device has been opened
3509 	 */
3510 
3511 	if ((un->un_dp->options & ST_KNOWS_EOD) &&
3512 	    !((un->un_dp->type == ST_TYPE_EXB8500 && MT_DENSITY(dev) == 0))) {
3513 		if (st_cmd(dev, SCMD_SPACE, Fmk(count), SYNC_CMD)) {
3514 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
3515 			    "space_fmks : EIO can't do space cmd #1\n");
3516 			rval = EIO;
3517 		}
3518 	} else {
3519 		while (count > 0) {
3520 			if (st_cmd(dev, SCMD_SPACE, Fmk(1), SYNC_CMD)) {
3521 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
3522 				    "space_fmks : EIO can't do space cmd #2\n");
3523 				rval = EIO;
3524 				break;
3525 			}
3526 			count -= 1;
3527 			/*
3528 			 * read a block to see if we have reached
3529 			 * end of medium (double filemark for reel or
3530 			 * medium error for others)
3531 			 */
3532 			if (count > 0) {
3533 				if (st_cmd(dev, SCMD_SPACE, Blk(1),
3534 				    SYNC_CMD)) {
3535 					ST_DEBUG2(ST_DEVINFO, st_label,
3536 					    SCSI_DEBUG,
3537 					    "space_fmks : EIO can't do "
3538 					    "space cmd #3\n");
3539 					rval = EIO;
3540 					break;
3541 				}
3542 				if ((un->un_eof >= ST_EOF_PENDING) &&
3543 				    (un->un_dp->options & ST_REEL)) {
3544 					un->un_status = SUN_KEY_EOT;
3545 					ST_DEBUG2(ST_DEVINFO, st_label,
3546 					    SCSI_DEBUG,
3547 					    "space_fmks : EIO ST_REEL\n");
3548 					rval = EIO;
3549 					break;
3550 				} else if (IN_EOF(un)) {
3551 					un->un_eof = ST_NO_EOF;
3552 					un->un_fileno++;
3553 					un->un_blkno = 0;
3554 					count--;
3555 				} else if (un->un_eof > ST_EOF) {
3556 					ST_DEBUG2(ST_DEVINFO, st_label,
3557 					    SCSI_DEBUG,
3558 					    "space_fmks, EIO > ST_EOF\n");
3559 					rval = EIO;
3560 					break;
3561 				}
3562 
3563 			}
3564 		}
3565 		un->un_err_resid = count;
3566 		un->un_err_fileno = un->un_fileno;
3567 		un->un_err_blkno = un->un_blkno;
3568 	}
3569 	ASSERT(mutex_owned(ST_MUTEX));
3570 	return (rval);
3571 }
3572 
3573 /*
3574  * this routine spaces to EOM
3575  *
3576  * it keeps track of the current filenumber and returns the filenumber after
3577  * the last successful space operation, we keep the number high because as
3578  * tapes are getting larger, the possibility of more and more files exist,
3579  * 0x100000 (1 Meg of files) probably will never have to be changed any time
3580  * soon
3581  */
3582 #define	MAX_SKIP	0x100000 /* somewhat arbitrary */
3583 
3584 static int
3585 st_find_eom(dev_t dev)
3586 {
3587 	int count, savefile;
3588 	struct scsi_tape *un;
3589 	int instance;
3590 
3591 	instance = MTUNIT(dev);
3592 	if ((un = ddi_get_soft_state(st_state, instance)) == NULL)
3593 		return (-1);
3594 
3595 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
3596 	    "st_find_eom(dev = 0x%lx): fileno = %d\n", dev, un->un_fileno);
3597 
3598 	ASSERT(mutex_owned(ST_MUTEX));
3599 
3600 	savefile = un->un_fileno;
3601 
3602 	/*
3603 	 * see if the drive is smart enough to do the skips in
3604 	 * one operation; 1/2" use two filemarks
3605 	 * the exabyte 8500 reading 8200 tapes cannot use KNOWS_EOD
3606 	 * because the 8200 does not append a marker; in order not to
3607 	 * sacrifice the fast file skip, we do a slow skip if the low
3608 	 * density device has been opened
3609 	 */
3610 	if ((un->un_dp->options & ST_KNOWS_EOD) &&
3611 	    !((un->un_dp->type == ST_TYPE_EXB8500 && MT_DENSITY(dev) == 0))) {
3612 		count = MAX_SKIP;
3613 	} else {
3614 		count = 1;
3615 	}
3616 
3617 	while (st_cmd(dev, SCMD_SPACE, Fmk(count), SYNC_CMD) == 0) {
3618 
3619 		savefile = un->un_fileno;
3620 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3621 			"count=%x, eof=%x, status=%x\n", count,  un->un_eof,
3622 			un->un_status);
3623 
3624 		/*
3625 		 * If we're not EOM smart,  space a record
3626 		 * to see whether we're now in the slot between
3627 		 * the two sequential filemarks that logical
3628 		 * EOM consists of (REEL) or hit nowhere land
3629 		 * (8mm).
3630 		 */
3631 		if (count == 1) {
3632 			/*
3633 			 * no fast skipping, check a record
3634 			 */
3635 			if (st_cmd(dev, SCMD_SPACE, Blk((1)), SYNC_CMD))
3636 				break;
3637 			else if ((un->un_eof >= ST_EOF_PENDING) &&
3638 			    (un->un_dp->options & ST_REEL)) {
3639 				un->un_status = KEY_BLANK_CHECK;
3640 				un->un_fileno++;
3641 				un->un_blkno = 0;
3642 				break;
3643 			} else if (IN_EOF(un)) {
3644 				un->un_eof = ST_NO_EOF;
3645 				un->un_fileno++;
3646 				un->un_blkno = 0;
3647 			} else if (un->un_eof > ST_EOF) {
3648 				break;
3649 			}
3650 		} else {
3651 			if (un->un_eof >  ST_EOF) {
3652 				break;
3653 			}
3654 		}
3655 	}
3656 
3657 	if (un->un_dp->options & ST_KNOWS_EOD) {
3658 		savefile = un->un_fileno;
3659 	}
3660 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
3661 	    "st_find_eom: %x\n", savefile);
3662 	ASSERT(mutex_owned(ST_MUTEX));
3663 	return (savefile);
3664 }
3665 
3666 
3667 /*
3668  * this routine is frequently used in ioctls below;
3669  * it determines whether we know the density and if not will
3670  * determine it
3671  * if we have written the tape before, one or more filemarks are written
3672  *
3673  * depending on the stepflag, the head is repositioned to where it was before
3674  * the filemarks were written in order not to confuse step counts
3675  */
3676 #define	STEPBACK    0
3677 #define	NO_STEPBACK 1
3678 
3679 static int
3680 st_check_density_or_wfm(dev_t dev, int wfm, int mode, int stepflag)
3681 {
3682 
3683 	GET_SOFT_STATE(dev);
3684 
3685 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
3686 	"st_check_density_or_wfm(dev= 0x%lx, wfm= %d, mode= %d, stpflg= %d)\n",
3687 	dev, wfm, mode, stepflag);
3688 
3689 	ASSERT(mutex_owned(ST_MUTEX));
3690 
3691 	/*
3692 	 * If we don't yet know the density of the tape we have inserted,
3693 	 * we have to either unconditionally set it (if we're 'writing'),
3694 	 * or we have to determine it. As side effects, check for any
3695 	 * write-protect errors, and for the need to put out any file-marks
3696 	 * before positioning a tape.
3697 	 *
3698 	 * If we are going to be spacing forward, and we haven't determined
3699 	 * the tape density yet, we have to do so now...
3700 	 */
3701 	if (un->un_state == ST_STATE_OPEN_PENDING_IO) {
3702 		if (st_determine_density(dev, mode)) {
3703 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
3704 			    "check_density_or_wfm : EIO can't determine "
3705 			    "density\n");
3706 			un->un_errno = EIO;
3707 			return (EIO);
3708 		}
3709 		/*
3710 		 * Presumably we are at BOT. If we attempt to write, it will
3711 		 * either work okay, or bomb. We don't do a st_test_append
3712 		 * unless we're past BOT.
3713 		 */
3714 		un->un_laststate = un->un_state;
3715 		un->un_state = ST_STATE_OPEN;
3716 
3717 	} else if (un->un_fileno >= 0 && un->un_fmneeded > 0 &&
3718 	    ((un->un_lastop == ST_OP_WEOF && wfm) ||
3719 	    (un->un_lastop == ST_OP_WRITE && wfm))) {
3720 
3721 		daddr_t blkno = un->un_blkno;
3722 		int fileno = un->un_fileno;
3723 
3724 		/*
3725 		 * We need to write one or two filemarks.
3726 		 * In the case of the HP, we need to
3727 		 * position the head between the two
3728 		 * marks.
3729 		 */
3730 		if ((un->un_fmneeded > 0) || (un->un_lastop == ST_OP_WEOF)) {
3731 			wfm = un->un_fmneeded;
3732 			un->un_fmneeded = 0;
3733 		}
3734 
3735 		if (st_write_fm(dev, wfm)) {
3736 			un->un_fileno = -1;
3737 			un->un_density_known = 0;
3738 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
3739 			    "check_density_or_wfm : EIO can't write fm\n");
3740 			un->un_errno = EIO;
3741 			return (EIO);
3742 		}
3743 
3744 		if (stepflag == STEPBACK) {
3745 			if (st_cmd(dev, SCMD_SPACE, Fmk((-wfm)), SYNC_CMD)) {
3746 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
3747 				    "check_density_or_wfm : EIO can't space "
3748 				    "(-wfm)\n");
3749 				un->un_errno = EIO;
3750 				return (EIO);
3751 			}
3752 			un->un_blkno = blkno;
3753 			un->un_fileno = fileno;
3754 		}
3755 	}
3756 
3757 	/*
3758 	 * Whatever we do at this point clears the state of the eof flag.
3759 	 */
3760 
3761 	un->un_eof = ST_NO_EOF;
3762 
3763 	/*
3764 	 * If writing, let's check that we're positioned correctly
3765 	 * at the end of tape before issuing the next write.
3766 	 */
3767 	if (un->un_read_only == RDWR) {
3768 		un->un_test_append = 1;
3769 	}
3770 
3771 	ASSERT(mutex_owned(ST_MUTEX));
3772 	return (0);
3773 }
3774 
3775 
3776 /*
3777  * Wait for all outstaning I/O's to complete
3778  *
3779  * we wait on both ncmds and the wait queue for times when we are flushing
3780  * after persistent errors are flagged, which is when ncmds can be 0, and the
3781  * queue can still have I/O's.  This way we preserve order of biodone's.
3782  */
3783 static void
3784 st_wait_for_io(struct scsi_tape *un)
3785 {
3786 	ASSERT(mutex_owned(ST_MUTEX));
3787 	while (un->un_ncmds || un->un_quef) {
3788 		cv_wait(&un->un_queue_cv, ST_MUTEX);
3789 	}
3790 }
3791 
3792 /*
3793  * This routine implements the ioctl calls.  It is called
3794  * from the device switch at normal priority.
3795  */
3796 /*ARGSUSED*/
3797 static int
3798 st_ioctl(dev_t dev, int cmd, intptr_t arg, int flag, cred_t *cred_p,
3799     int *rval_p)
3800 {
3801 	int tmp, rval = 0;
3802 
3803 	GET_SOFT_STATE(dev);
3804 
3805 	mutex_enter(ST_MUTEX);
3806 
3807 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
3808 	    "st_ioctl(): fileno=%x, blkno=%lx, un_eof=%x, state = %d, "
3809 	    "pe_flag = %d\n",
3810 	    un->un_fileno, un->un_blkno, un->un_eof, un->un_state,
3811 	    IS_PE_FLAG_SET(un));
3812 
3813 	/*
3814 	 * We don't want to block on these, so let them through
3815 	 * and we don't care about setting driver states here.
3816 	 */
3817 	if ((cmd == MTIOCGETDRIVETYPE) ||
3818 	    (cmd == MTIOCGUARANTEEDORDER) ||
3819 	    (cmd == MTIOCPERSISTENTSTATUS)) {
3820 		goto check_commands;
3821 	}
3822 
3823 	/*
3824 	 * wait for all outstanding commands to complete, or be dequeued.
3825 	 * And because ioctl's are synchronous commands, any return value
3826 	 * after this,  will be in order
3827 	 */
3828 	st_wait_for_io(un);
3829 
3830 	/*
3831 	 * allow only a through clear errors and persistent status, and
3832 	 * status
3833 	 */
3834 	if (IS_PE_FLAG_SET(un)) {
3835 		if ((cmd == MTIOCLRERR) ||
3836 		    (cmd == MTIOCPERSISTENT) ||
3837 		    (cmd == MTIOCGET) ||
3838 		    (cmd == USCSIGETRQS)) {
3839 			goto check_commands;
3840 		} else {
3841 			rval = un->un_errno;
3842 			goto exit;
3843 		}
3844 	}
3845 
3846 	un->un_throttle = 1;	/* > 1 will never happen here */
3847 	un->un_errno = 0;	/* start clean from here */
3848 
3849 	/*
3850 	 * first and foremost, handle any ST_EOT_PENDING cases.
3851 	 * That is, if a logical eot is pending notice, notice it.
3852 	 */
3853 	if (un->un_eof == ST_EOT_PENDING) {
3854 		int resid = un->un_err_resid;
3855 		uchar_t status = un->un_status;
3856 		uchar_t lastop = un->un_lastop;
3857 
3858 		if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)) {
3859 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
3860 			    "stioctl : EIO can't space fmk(-1)\n");
3861 			rval = EIO;
3862 			goto exit;
3863 		}
3864 		un->un_lastop = lastop; /* restore last operation */
3865 		if (status == SUN_KEY_EOF) {
3866 			un->un_status = SUN_KEY_EOT;
3867 		} else {
3868 			un->un_status = status;
3869 		}
3870 		un->un_err_resid  = resid;
3871 		un->un_err_blkno = un->un_blkno = 0; /* fix up block number */
3872 		un->un_eof = ST_EOT;	/* now we're at logical eot */
3873 	}
3874 
3875 	/*
3876 	 * now, handle the rest of the situations
3877 	 */
3878 check_commands:
3879 	switch (cmd) {
3880 	case MTIOCGET:
3881 		{
3882 #ifdef _MULTI_DATAMODEL
3883 			/*
3884 			 * For use when a 32 bit app makes a call into a
3885 			 * 64 bit ioctl
3886 			 */
3887 			struct mtget32		mtg_local32;
3888 			struct mtget32 		*mtget_32 = &mtg_local32;
3889 #endif /* _MULTI_DATAMODEL */
3890 
3891 			/* Get tape status */
3892 			struct mtget mtg_local;
3893 			struct mtget *mtget = &mtg_local;
3894 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
3895 				"st_ioctl: MTIOCGET\n");
3896 
3897 			bzero(mtget, sizeof (struct mtget));
3898 			mtget->mt_erreg = un->un_status;
3899 			mtget->mt_resid = un->un_err_resid;
3900 			mtget->mt_dsreg = un->un_retry_ct;
3901 			mtget->mt_fileno = un->un_err_fileno;
3902 			mtget->mt_blkno = un->un_err_blkno;
3903 			mtget->mt_type = un->un_dp->type;
3904 			mtget->mt_flags = MTF_SCSI | MTF_ASF;
3905 			if (un->un_dp->options & ST_REEL) {
3906 				mtget->mt_flags |= MTF_REEL;
3907 				mtget->mt_bf = 20;
3908 			} else {		/* 1/4" cartridges */
3909 				switch (mtget->mt_type) {
3910 				/* Emulex cartridge tape */
3911 				case MT_ISMT02:
3912 					mtget->mt_bf = 40;
3913 					break;
3914 				default:
3915 					mtget->mt_bf = 126;
3916 					break;
3917 				}
3918 			}
3919 
3920 			/*
3921 			 * If large transfers are allowed and drive options
3922 			 * has no record size limit set. Calculate blocking
3923 			 * factor from the lesser of maxbsize and maxdma.
3924 			 */
3925 			if ((un->un_allow_large_xfer) &&
3926 			    (un->un_dp->options & ST_NO_RECSIZE_LIMIT)) {
3927 				mtget->mt_bf = min(un->un_maxbsize,
3928 				    un->un_maxdma) / SECSIZE;
3929 			}
3930 
3931 			if (un->un_read_only == WORM ||
3932 			    un->un_read_only == RDWORM) {
3933 				mtget->mt_flags |= MTF_WORM_MEDIA;
3934 			}
3935 
3936 			rval = st_check_clean_bit(dev);
3937 			if (rval == -1) {
3938 				rval = EIO;
3939 				goto exit;
3940 			} else {
3941 				mtget->mt_flags |= (ushort_t)rval;
3942 				rval = 0;
3943 			}
3944 
3945 			un->un_status = 0;		/* Reset status */
3946 			un->un_err_resid = 0;
3947 			tmp = sizeof (struct mtget);
3948 
3949 #ifdef _MULTI_DATAMODEL
3950 
3951 		switch (ddi_model_convert_from(flag & FMODELS)) {
3952 		case DDI_MODEL_ILP32:
3953 			/*
3954 			 * Convert 64 bit back to 32 bit before doing
3955 			 * copyout. This is what the ILP32 app expects.
3956 			 */
3957 			mtget_32->mt_erreg = 	mtget->mt_erreg;
3958 			mtget_32->mt_resid = 	mtget->mt_resid;
3959 			mtget_32->mt_dsreg = 	mtget->mt_dsreg;
3960 			mtget_32->mt_fileno = 	(daddr32_t)mtget->mt_fileno;
3961 			mtget_32->mt_blkno = 	(daddr32_t)mtget->mt_blkno;
3962 			mtget_32->mt_type =  	mtget->mt_type;
3963 			mtget_32->mt_flags = 	mtget->mt_flags;
3964 			mtget_32->mt_bf = 	mtget->mt_bf;
3965 
3966 			if (ddi_copyout(mtget_32, (void *)arg,
3967 			    sizeof (struct mtget32), flag)) {
3968 				rval = EFAULT;
3969 			}
3970 			break;
3971 
3972 		case DDI_MODEL_NONE:
3973 			if (ddi_copyout(mtget, (void *)arg, tmp, flag)) {
3974 				rval = EFAULT;
3975 			}
3976 			break;
3977 		}
3978 #else /* ! _MULTI_DATAMODE */
3979 		if (ddi_copyout(mtget, (void *)arg, tmp, flag)) {
3980 			rval = EFAULT;
3981 		}
3982 #endif /* _MULTI_DATAMODE */
3983 
3984 			break;
3985 		}
3986 	case MTIOCSTATE:
3987 		{
3988 			/*
3989 			 * return when media presence matches state
3990 			 */
3991 			enum mtio_state state;
3992 
3993 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
3994 				"st_ioctl: MTIOCSTATE\n");
3995 
3996 			if (ddi_copyin((void *)arg, &state, sizeof (int), flag))
3997 				rval = EFAULT;
3998 
3999 			mutex_exit(ST_MUTEX);
4000 
4001 			rval = st_check_media(dev, state);
4002 
4003 			mutex_enter(ST_MUTEX);
4004 
4005 			if (rval != 0) {
4006 				break;
4007 			}
4008 
4009 			if (ddi_copyout(&un->un_mediastate, (void *)arg,
4010 			    sizeof (int), flag))
4011 				rval = EFAULT;
4012 			break;
4013 
4014 		}
4015 
4016 	case MTIOCGETDRIVETYPE:
4017 		{
4018 #ifdef _MULTI_DATAMODEL
4019 		/*
4020 		 * For use when a 32 bit app makes a call into a
4021 		 * 64 bit ioctl
4022 		 */
4023 		struct mtdrivetype_request32	mtdtrq32;
4024 #endif /* _MULTI_DATAMODEL */
4025 
4026 			/*
4027 			 * return mtdrivetype
4028 			 */
4029 			struct mtdrivetype_request mtdtrq;
4030 			struct mtdrivetype mtdrtyp;
4031 			struct mtdrivetype *mtdt = &mtdrtyp;
4032 			struct st_drivetype *stdt = un->un_dp;
4033 
4034 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4035 				"st_ioctl: MTIOCGETDRIVETYPE\n");
4036 
4037 #ifdef _MULTI_DATAMODEL
4038 		switch (ddi_model_convert_from(flag & FMODELS)) {
4039 		case DDI_MODEL_ILP32:
4040 		{
4041 			if (ddi_copyin((void *)arg, &mtdtrq32,
4042 			    sizeof (struct mtdrivetype_request32), flag)) {
4043 				rval = EFAULT;
4044 				break;
4045 			}
4046 			mtdtrq.size = mtdtrq32.size;
4047 			mtdtrq.mtdtp =
4048 			    (struct  mtdrivetype *)(uintptr_t)mtdtrq32.mtdtp;
4049 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4050 				"st_ioctl: size 0x%x\n", mtdtrq.size);
4051 			break;
4052 		}
4053 		case DDI_MODEL_NONE:
4054 			if (ddi_copyin((void *)arg, &mtdtrq,
4055 			    sizeof (struct mtdrivetype_request), flag)) {
4056 				rval = EFAULT;
4057 				break;
4058 			}
4059 			break;
4060 		}
4061 
4062 #else /* ! _MULTI_DATAMODEL */
4063 		if (ddi_copyin((void *)arg, &mtdtrq,
4064 		    sizeof (struct mtdrivetype_request), flag)) {
4065 			rval = EFAULT;
4066 			break;
4067 		}
4068 #endif /* _MULTI_DATAMODEL */
4069 
4070 			/*
4071 			 * if requested size is < 0 then return
4072 			 * error.
4073 			 */
4074 			if (mtdtrq.size < 0) {
4075 				rval = EINVAL;
4076 				break;
4077 			}
4078 			bzero(mtdt, sizeof (struct mtdrivetype));
4079 			(void) strncpy(mtdt->name, stdt->name, ST_NAMESIZE);
4080 			(void) strncpy(mtdt->vid, stdt->vid, VIDPIDLEN - 1);
4081 			mtdt->type = stdt->type;
4082 			mtdt->bsize = stdt->bsize;
4083 			mtdt->options = stdt->options;
4084 			mtdt->max_rretries = stdt->max_rretries;
4085 			mtdt->max_wretries = stdt->max_wretries;
4086 			for (tmp = 0; tmp < NDENSITIES; tmp++)
4087 				mtdt->densities[tmp] = stdt->densities[tmp];
4088 			mtdt->default_density = stdt->default_density;
4089 			/*
4090 			 * Speed hasn't been used since the hayday of reel tape.
4091 			 * For all drives not setting the option ST_KNOWS_MEDIA
4092 			 * the speed member renamed to mediatype are zeros.
4093 			 * Those drives that have ST_KNOWS_MEDIA set use the
4094 			 * new mediatype member which is used to figure the
4095 			 * type of media loaded.
4096 			 *
4097 			 * So as to not break applications speed in the
4098 			 * mtdrivetype structure is not renamed.
4099 			 */
4100 			for (tmp = 0; tmp < NDENSITIES; tmp++) {
4101 				mtdt->speeds[tmp] = stdt->mediatype[tmp];
4102 			}
4103 			mtdt->non_motion_timeout = stdt->non_motion_timeout;
4104 			mtdt->io_timeout = stdt->io_timeout;
4105 			mtdt->rewind_timeout = stdt->rewind_timeout;
4106 			mtdt->space_timeout = stdt->space_timeout;
4107 			mtdt->load_timeout = stdt->load_timeout;
4108 			mtdt->unload_timeout = stdt->unload_timeout;
4109 			mtdt->erase_timeout = stdt->erase_timeout;
4110 
4111 			/*
4112 			 * Limit the maximum length of the result to
4113 			 * sizeof (struct mtdrivetype).
4114 			 */
4115 			tmp = sizeof (struct mtdrivetype);
4116 			if (mtdtrq.size < tmp)
4117 				tmp = mtdtrq.size;
4118 			if (ddi_copyout(mtdt, mtdtrq.mtdtp, tmp, flag)) {
4119 				rval = EFAULT;
4120 			}
4121 			break;
4122 		}
4123 	case MTIOCPERSISTENT:
4124 		{
4125 			int persistence = 0;
4126 
4127 			if (ddi_copyin((void *)arg, &persistence,
4128 			    sizeof (int), flag)) {
4129 				rval = EFAULT;
4130 				break;
4131 			}
4132 
4133 			/* non zero sets it, only 0 turns it off */
4134 			un->un_persistence = (uchar_t)persistence ? 1 : 0;
4135 
4136 			if (un->un_persistence)
4137 				TURN_PE_ON(un);
4138 			else
4139 				TURN_PE_OFF(un);
4140 
4141 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4142 			    "st_ioctl: MTIOCPERSISTENT : persistence = %d\n",
4143 			    un->un_persistence);
4144 
4145 			break;
4146 		}
4147 	case MTIOCPERSISTENTSTATUS:
4148 		{
4149 			int persistence = (int)un->un_persistence;
4150 
4151 			if (ddi_copyout(&persistence, (void *)arg,
4152 			    sizeof (int), flag)) {
4153 				rval = EFAULT;
4154 			}
4155 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4156 			    "st_ioctl: MTIOCPERSISTENTSTATUS:persistece = %d\n",
4157 			    un->un_persistence);
4158 
4159 			break;
4160 		}
4161 
4162 
4163 	case MTIOCLRERR:
4164 		{
4165 			/* clear persistent errors */
4166 
4167 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4168 			    "st_ioctl: MTIOCLRERR\n");
4169 
4170 			CLEAR_PE(un);
4171 
4172 			break;
4173 		}
4174 
4175 	case MTIOCGUARANTEEDORDER:
4176 		{
4177 			/*
4178 			 * this is just a holder to make a valid ioctl and
4179 			 * it won't be in any earlier release
4180 			 */
4181 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4182 			    "st_ioctl: MTIOCGUARANTEEDORDER\n");
4183 
4184 			break;
4185 		}
4186 
4187 	case MTIOCRESERVE:
4188 		{
4189 			ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
4190 			    "st_ioctl: MTIOCRESERVE\n");
4191 
4192 			/*
4193 			 * Check if Reserve/Release is supported.
4194 			 */
4195 			if (un->un_dp->options & ST_NO_RESERVE_RELEASE) {
4196 				rval = ENOTTY;
4197 				break;
4198 			}
4199 
4200 			rval = st_reserve_release(un, ST_RESERVE);
4201 
4202 			if (rval == 0) {
4203 				un->un_rsvd_status |= ST_PRESERVE_RESERVE;
4204 			}
4205 			break;
4206 		}
4207 
4208 	case MTIOCRELEASE:
4209 		{
4210 			ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
4211 			    "st_ioctl: MTIOCRELEASE\n");
4212 
4213 			/*
4214 			 * Check if Reserve/Release is supported.
4215 			 */
4216 			if (un->un_dp->options & ST_NO_RESERVE_RELEASE) {
4217 				rval = ENOTTY;
4218 				break;
4219 			}
4220 
4221 			/*
4222 			 * Used to just clear ST_PRESERVE_RESERVE which
4223 			 * made the reservation release at next close.
4224 			 * As the user may have opened and then done a
4225 			 * persistant reservation we now need to drop
4226 			 * the reservation without closing if the user
4227 			 * attempts to do this.
4228 			 */
4229 			rval = st_reserve_release(un, ST_RELEASE);
4230 
4231 			un->un_rsvd_status &= ~ST_PRESERVE_RESERVE;
4232 
4233 			break;
4234 		}
4235 
4236 	case MTIOCFORCERESERVE:
4237 		{
4238 			ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
4239 			    "st_ioctl: MTIOCFORCERESERVE\n");
4240 
4241 			/*
4242 			 * Check if Reserve/Release is supported.
4243 			 */
4244 			if (un->un_dp->options & ST_NO_RESERVE_RELEASE) {
4245 				rval = ENOTTY;
4246 				break;
4247 			}
4248 			/*
4249 			 * allow only super user to run this.
4250 			 */
4251 			if (drv_priv(cred_p) != 0) {
4252 				rval = EPERM;
4253 				break;
4254 			}
4255 			/*
4256 			 * Throw away reserve,
4257 			 * not using test-unit-ready
4258 			 * since reserve can succeed without tape being
4259 			 * present in the drive.
4260 			 */
4261 			(void) st_reserve_release(un, ST_RESERVE);
4262 
4263 			rval = st_take_ownership(dev);
4264 
4265 			break;
4266 		}
4267 	case USCSIGETRQS:
4268 		{
4269 #ifdef _MULTI_DATAMODEL
4270 		/*
4271 		 * For use when a 32 bit app makes a call into a
4272 		 * 64 bit ioctl
4273 		 */
4274 		struct uscsi_rqs32	urqs_32;
4275 		struct uscsi_rqs32	*urqs_32_ptr = &urqs_32;
4276 #endif /* _MULTI_DATAMODEL */
4277 			struct uscsi_rqs	urqs;
4278 			struct uscsi_rqs	*urqs_ptr = &urqs;
4279 			ushort_t		len;
4280 #ifdef _MULTI_DATAMODEL
4281 		switch (ddi_model_convert_from(flag & FMODELS)) {
4282 		case DDI_MODEL_ILP32:
4283 		{
4284 			if (ddi_copyin((void *)arg, urqs_32_ptr,
4285 			    sizeof (struct uscsi_rqs32), flag)) {
4286 				rval = EFAULT;
4287 				break;
4288 			}
4289 			urqs_ptr->rqs_buflen = urqs_32_ptr->rqs_buflen;
4290 			urqs_ptr->rqs_bufaddr =
4291 			    (caddr_t)(uintptr_t)urqs_32_ptr->rqs_bufaddr;
4292 			break;
4293 		}
4294 		case DDI_MODEL_NONE:
4295 			if (ddi_copyin((void *)arg, urqs_ptr,
4296 			    sizeof (struct uscsi_rqs), flag)) {
4297 				rval = EFAULT;
4298 				break;
4299 			}
4300 		}
4301 #else /* ! _MULTI_DATAMODEL */
4302 		if (ddi_copyin((void *)arg, urqs_ptr, sizeof (urqs), flag)) {
4303 			rval = EFAULT;
4304 			break;
4305 		}
4306 #endif /* _MULTI_DATAMODEL */
4307 
4308 			urqs_ptr->rqs_flags = (int)un->un_rqs_state &
4309 			    (ST_RQS_OVR | ST_RQS_VALID);
4310 			if (urqs_ptr->rqs_buflen <= SENSE_LENGTH) {
4311 			    len = urqs_ptr->rqs_buflen;
4312 			    urqs_ptr->rqs_resid = 0;
4313 			} else {
4314 			    len = SENSE_LENGTH;
4315 			    urqs_ptr->rqs_resid = urqs_ptr->rqs_buflen
4316 				    - SENSE_LENGTH;
4317 			}
4318 			if (!(un->un_rqs_state & ST_RQS_VALID)) {
4319 			    urqs_ptr->rqs_resid = urqs_ptr->rqs_buflen;
4320 			}
4321 			un->un_rqs_state |= ST_RQS_READ;
4322 			un->un_rqs_state &= ~(ST_RQS_OVR);
4323 
4324 #ifdef _MULTI_DATAMODEL
4325 		switch (ddi_model_convert_from(flag & FMODELS)) {
4326 		case DDI_MODEL_ILP32:
4327 			urqs_32_ptr->rqs_flags = urqs_ptr->rqs_flags;
4328 			urqs_32_ptr->rqs_resid = urqs_ptr->rqs_resid;
4329 			if (ddi_copyout(&urqs_32, (void *)arg,
4330 			    sizeof (urqs_32), flag)) {
4331 				rval = EFAULT;
4332 			}
4333 			break;
4334 		case DDI_MODEL_NONE:
4335 			if (ddi_copyout(&urqs, (void *)arg, sizeof (urqs),
4336 			    flag)) {
4337 				rval = EFAULT;
4338 			}
4339 			break;
4340 		}
4341 
4342 		if (un->un_rqs_state & ST_RQS_VALID) {
4343 			if (ddi_copyout(un->un_uscsi_rqs_buf,
4344 			    urqs_ptr->rqs_bufaddr, len, flag)) {
4345 				rval = EFAULT;
4346 		    }
4347 		}
4348 #else /* ! _MULTI_DATAMODEL */
4349 		if (ddi_copyout(&urqs, (void *)arg, sizeof (urqs), flag)) {
4350 			rval = EFAULT;
4351 		}
4352 		if (un->un_rqs_state & ST_RQS_VALID) {
4353 			if (ddi_copyout(un->un_uscsi_rqs_buf,
4354 			    urqs_ptr->rqs_bufaddr, len, flag)) {
4355 				rval = EFAULT;
4356 		    }
4357 		}
4358 #endif /* _MULTI_DATAMODEL */
4359 			break;
4360 		}
4361 
4362 	case USCSICMD:
4363 	{
4364 		cred_t	*cr;
4365 #ifdef _MULTI_DATAMODEL
4366 		/*
4367 		 * For use when a 32 bit app makes a call into a
4368 		 * 64 bit ioctl
4369 		 */
4370 		struct uscsi_cmd32	ucmd_32;
4371 		struct uscsi_cmd32	*ucmd_32_ptr = &ucmd_32;
4372 #endif /* _MULTI_DATAMODEL */
4373 
4374 		/*
4375 		 * Run a generic USCSI command
4376 		 */
4377 		struct uscsi_cmd ucmd;
4378 		struct uscsi_cmd *ucmd_ptr = &ucmd;
4379 		enum uio_seg uioseg;
4380 
4381 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4382 			"st_ioctl: USCSICMD\n");
4383 
4384 		cr = ddi_get_cred();
4385 		if ((drv_priv(cred_p) != 0) && (drv_priv(cr) != 0)) {
4386 			rval = EPERM;
4387 			break;
4388 		}
4389 
4390 #ifdef _MULTI_DATAMODEL
4391 		switch (ddi_model_convert_from(flag & FMODELS)) {
4392 		case DDI_MODEL_ILP32:
4393 		{
4394 			if (ddi_copyin((void *)arg, ucmd_32_ptr,
4395 			    sizeof (struct uscsi_cmd32), flag)) {
4396 				rval = EFAULT;
4397 				break;
4398 			}
4399 			uscsi_cmd32touscsi_cmd(ucmd_32_ptr, ucmd_ptr);
4400 			break;
4401 		}
4402 		case DDI_MODEL_NONE:
4403 			if (ddi_copyin((void *)arg, ucmd_ptr, sizeof (ucmd),
4404 			    flag)) {
4405 				rval = EFAULT;
4406 				break;
4407 			}
4408 		}
4409 
4410 #else /* ! _MULTI_DATAMODEL */
4411 		if (ddi_copyin((void *)arg, ucmd_ptr, sizeof (ucmd), flag)) {
4412 			rval = EFAULT;
4413 			break;
4414 		}
4415 #endif /* _MULTI_DATAMODEL */
4416 
4417 
4418 		/*
4419 		 * although st_ioctl_cmd() never makes use of these
4420 		 * now, we are just being safe and consistent
4421 		 */
4422 		ucmd.uscsi_flags &= ~(USCSI_NOINTR | USCSI_NOPARITY |
4423 		    USCSI_OTAG | USCSI_HTAG | USCSI_HEAD);
4424 
4425 
4426 		uioseg = (flag & FKIOCTL) ?  UIO_SYSSPACE : UIO_USERSPACE;
4427 
4428 		rval = st_ioctl_cmd(dev, &ucmd, uioseg, uioseg, uioseg);
4429 
4430 
4431 #ifdef _MULTI_DATAMODEL
4432 		switch (ddi_model_convert_from(flag & FMODELS)) {
4433 		case DDI_MODEL_ILP32:
4434 			/*
4435 			 * Convert 64 bit back to 32 bit before doing
4436 			 * copyout. This is what the ILP32 app expects.
4437 			 */
4438 			uscsi_cmdtouscsi_cmd32(ucmd_ptr, ucmd_32_ptr);
4439 
4440 			if (ddi_copyout(&ucmd_32, (void *)arg,
4441 			    sizeof (ucmd_32), flag)) {
4442 				if (rval != 0)
4443 					rval = EFAULT;
4444 				}
4445 			break;
4446 
4447 		case DDI_MODEL_NONE:
4448 			if (ddi_copyout(&ucmd, (void *)arg,
4449 			    sizeof (ucmd), flag)) {
4450 				if (rval != 0)
4451 					rval = EFAULT;
4452 				}
4453 			break;
4454 		}
4455 #else /* ! _MULTI_DATAMODEL */
4456 		if (ddi_copyout(&ucmd, (void *)arg, sizeof (ucmd), flag)) {
4457 			if (rval != 0)
4458 				rval = EFAULT;
4459 		}
4460 #endif /* _MULTI_DATAMODEL */
4461 
4462 		break;
4463 	}
4464 
4465 	case MTIOCTOP:
4466 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4467 			"st_ioctl: MTIOCTOP\n");
4468 		rval = st_mtioctop(un, arg, flag);
4469 		break;
4470 
4471 	case MTIOCREADIGNOREILI:
4472 		{
4473 			int set_ili;
4474 
4475 			if (ddi_copyin((void *)arg, &set_ili,
4476 			    sizeof (set_ili), flag)) {
4477 				rval = EFAULT;
4478 				break;
4479 			}
4480 
4481 			if (un->un_bsize) {
4482 				rval = ENOTTY;
4483 				break;
4484 			}
4485 
4486 			switch (set_ili) {
4487 			case 0:
4488 				un->un_dp->options &= ~ST_READ_IGNORE_ILI;
4489 				break;
4490 
4491 			case 1:
4492 				un->un_dp->options |= ST_READ_IGNORE_ILI;
4493 				break;
4494 
4495 			default:
4496 				rval = EINVAL;
4497 				break;
4498 			}
4499 			break;
4500 		}
4501 
4502 	case MTIOCREADIGNOREEOFS:
4503 		{
4504 			int ignore_eof;
4505 
4506 			if (ddi_copyin((void *)arg, &ignore_eof,
4507 			    sizeof (ignore_eof), flag)) {
4508 				rval = EFAULT;
4509 				break;
4510 			}
4511 
4512 			if (!(un->un_dp->options & ST_REEL)) {
4513 				rval = ENOTTY;
4514 				break;
4515 			}
4516 
4517 			switch (ignore_eof) {
4518 			case 0:
4519 				un->un_dp->options &= ~ST_READ_IGNORE_EOFS;
4520 				break;
4521 
4522 			case 1:
4523 				un->un_dp->options |= ST_READ_IGNORE_EOFS;
4524 				break;
4525 
4526 			default:
4527 				rval = EINVAL;
4528 				break;
4529 			}
4530 			break;
4531 		}
4532 
4533 	case MTIOCSHORTFMK:
4534 		{
4535 			int short_fmk;
4536 
4537 			if (ddi_copyin((void *)arg, &short_fmk,
4538 			    sizeof (short_fmk), flag)) {
4539 				rval = EFAULT;
4540 				break;
4541 			}
4542 
4543 			switch (un->un_dp->type) {
4544 			case ST_TYPE_EXB8500:
4545 			case ST_TYPE_EXABYTE:
4546 				if (!short_fmk) {
4547 					un->un_dp->options &=
4548 						~ST_SHORT_FILEMARKS;
4549 				} else if (short_fmk == 1) {
4550 					un->un_dp->options |=
4551 						ST_SHORT_FILEMARKS;
4552 				} else {
4553 					rval = EINVAL;
4554 				}
4555 				break;
4556 
4557 			default:
4558 				rval = ENOTTY;
4559 				break;
4560 			}
4561 			break;
4562 		}
4563 
4564 	default:
4565 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4566 			"st_ioctl: unknown ioctl\n");
4567 		rval = ENOTTY;
4568 	}
4569 
4570 exit:
4571 	if (!IS_PE_FLAG_SET(un))
4572 		un->un_errno = rval;
4573 
4574 	mutex_exit(ST_MUTEX);
4575 
4576 	return (rval);
4577 }
4578 
4579 
4580 /*
4581  * do some MTIOCTOP tape operations
4582  */
4583 static int
4584 st_mtioctop(struct scsi_tape *un, intptr_t arg, int flag)
4585 {
4586 	struct mtop *mtop, local;
4587 	int savefile, tmp, rval = 0;
4588 	dev_t dev = un->un_dev;
4589 #ifdef _MULTI_DATAMODEL
4590 	/*
4591 	 * For use when a 32 bit app makes a call into a
4592 	 * 64 bit ioctl
4593 	 */
4594 	struct mtop32	mtop_32_for_64;
4595 #endif /* _MULTI_DATAMODEL */
4596 
4597 
4598 	ASSERT(mutex_owned(ST_MUTEX));
4599 
4600 	mtop = &local;
4601 #ifdef _MULTI_DATAMODEL
4602 		switch (ddi_model_convert_from(flag & FMODELS)) {
4603 		case DDI_MODEL_ILP32:
4604 		{
4605 			if (ddi_copyin((void *)arg, &mtop_32_for_64,
4606 			    sizeof (struct mtop32), flag)) {
4607 				return (EFAULT);
4608 			}
4609 			mtop->mt_op = mtop_32_for_64.mt_op;
4610 			mtop->mt_count =  (daddr_t)mtop_32_for_64.mt_count;
4611 			break;
4612 		}
4613 		case DDI_MODEL_NONE:
4614 			if (ddi_copyin((void *)arg, mtop,
4615 			    sizeof (struct mtop), flag)) {
4616 				return (EFAULT);
4617 			}
4618 			break;
4619 		}
4620 
4621 #else /* ! _MULTI_DATAMODEL */
4622 		if (ddi_copyin((void *)arg, mtop, sizeof (struct mtop), flag)) {
4623 			return (EFAULT);
4624 		}
4625 #endif /* _MULTI_DATAMODEL */
4626 
4627 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
4628 	    "st_mtioctop(): mt_op=%x\n", mtop->mt_op);
4629 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
4630 	    "fileno=%x, blkno=%lx, un_eof=%x\n", un->un_fileno, un->un_blkno,
4631 	    un->un_eof);
4632 
4633 	rval = 0;
4634 	un->un_status = 0;
4635 
4636 	/*
4637 	 * if we are going to mess with a tape, we have to make sure we have
4638 	 * one and are not offline (i.e. no tape is initialized).  We let
4639 	 * commands pass here that don't actually touch the tape, except for
4640 	 * loading and initialization (rewinding).
4641 	 */
4642 	if (un->un_state == ST_STATE_OFFLINE) {
4643 		switch (mtop->mt_op) {
4644 		case MTLOAD:
4645 		case MTNOP:
4646 			/*
4647 			 * We don't want strategy calling st_tape_init here,
4648 			 * so, change state
4649 			 */
4650 			un->un_state = ST_STATE_INITIALIZING;
4651 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4652 			    "st_mtioctop : OFFLINE state = %d\n",
4653 			    un->un_state);
4654 			break;
4655 		default:
4656 			/*
4657 			 * reinitialize by normal means
4658 			 */
4659 			rval = st_tape_init(dev);
4660 			if (rval) {
4661 				un->un_state = ST_STATE_INITIALIZING;
4662 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4663 				    "st_mtioctop : OFFLINE init failure ");
4664 				un->un_state = ST_STATE_OFFLINE;
4665 				un->un_fileno = -1;
4666 				if (rval != EACCES) {
4667 					rval = EIO;
4668 				}
4669 				return (rval);
4670 			}
4671 			un->un_state = ST_STATE_OPEN_PENDING_IO;
4672 			break;
4673 		}
4674 	}
4675 
4676 	/*
4677 	 * If the file position is invalid, allow only those
4678 	 * commands that properly position the tape and fail
4679 	 * the rest with EIO
4680 	 */
4681 	if (un->un_fileno < 0) {
4682 		switch (mtop->mt_op) {
4683 		case MTWEOF:
4684 		case MTRETEN:
4685 		case MTERASE:
4686 		case MTEOM:
4687 		case MTFSF:
4688 		case MTFSR:
4689 		case MTBSF:
4690 		case MTNBSF:
4691 		case MTBSR:
4692 		case MTSRSZ:
4693 		case MTGRSZ:
4694 			return (EIO);
4695 			/* NOTREACHED */
4696 		case MTREW:
4697 		case MTLOAD:
4698 		case MTOFFL:
4699 		case MTNOP:
4700 			break;
4701 
4702 		default:
4703 			return (ENOTTY);
4704 			/* NOTREACHED */
4705 		}
4706 	}
4707 
4708 	switch (mtop->mt_op) {
4709 	case MTERASE:
4710 		/*
4711 		 * MTERASE rewinds the tape, erase it completely, and returns
4712 		 * to the beginning of the tape
4713 		 */
4714 		if (un->un_mspl->wp || un->un_read_only & WORM) {
4715 			un->un_status = KEY_WRITE_PROTECT;
4716 			un->un_err_resid = mtop->mt_count;
4717 			un->un_err_fileno = un->un_fileno;
4718 			un->un_err_blkno = un->un_blkno;
4719 			return (EACCES);
4720 		}
4721 		if (un->un_dp->options & ST_REEL) {
4722 			un->un_fmneeded = 2;
4723 		} else {
4724 			un->un_fmneeded = 1;
4725 		}
4726 		if (st_check_density_or_wfm(dev, 1, B_WRITE, NO_STEPBACK) ||
4727 		    st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) ||
4728 		    st_cmd(dev, SCMD_ERASE, 0, SYNC_CMD)) {
4729 			un->un_fileno = -1;
4730 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4731 			    "st_mtioctop : EIO space or erase or check den)\n");
4732 			rval = EIO;
4733 		} else {
4734 			/* QIC and helical scan rewind after erase */
4735 			if (un->un_dp->options & ST_REEL) {
4736 				(void) st_cmd(dev, SCMD_REWIND, 0, ASYNC_CMD);
4737 			}
4738 		}
4739 		break;
4740 
4741 	case MTWEOF:
4742 		/*
4743 		 * write an end-of-file record
4744 		 */
4745 		if (un->un_mspl->wp || un->un_read_only & RDONLY) {
4746 			un->un_status = KEY_WRITE_PROTECT;
4747 			un->un_err_resid = mtop->mt_count;
4748 			un->un_err_fileno = un->un_fileno;
4749 			un->un_err_blkno = un->un_blkno;
4750 			return (EACCES);
4751 		}
4752 
4753 		/*
4754 		 * zero count means just flush buffers
4755 		 * negative count is not permitted
4756 		 */
4757 		if (mtop->mt_count < 0)
4758 			return (EINVAL);
4759 
4760 		/* Not on worm */
4761 		if (un->un_read_only == RDWR) {
4762 			un->un_test_append = 1;
4763 		}
4764 
4765 		if (un->un_state == ST_STATE_OPEN_PENDING_IO) {
4766 			if (st_determine_density(dev, B_WRITE)) {
4767 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4768 				    "st_mtioctop : EIO : MTWEOF can't determine"
4769 				    "density");
4770 				return (EIO);
4771 			}
4772 		}
4773 
4774 		rval = st_write_fm(dev, (int)mtop->mt_count);
4775 		if ((rval != 0) && (rval != EACCES)) {
4776 			/*
4777 			 * Failure due to something other than illegal
4778 			 * request results in loss of state (st_intr).
4779 			 */
4780 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4781 			    "st_mtioctop : EIO : MTWEOF can't write file mark");
4782 			rval = EIO;
4783 		}
4784 		break;
4785 
4786 	case MTRETEN:
4787 		/*
4788 		 * retension the tape
4789 		 */
4790 		if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK) ||
4791 		    st_cmd(dev, SCMD_LOAD, 3, SYNC_CMD)) {
4792 			un->un_fileno = -1;
4793 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4794 			    "st_mtioctop : EIO : MTRETEN ");
4795 			rval = EIO;
4796 		}
4797 		break;
4798 
4799 	case MTREW:
4800 		/*
4801 		 * rewind  the tape
4802 		 */
4803 		if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) {
4804 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4805 			    "st_mtioctop : EIO:MTREW check density/wfm failed");
4806 			return (EIO);
4807 		}
4808 		if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) {
4809 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4810 			    "st_mtioctop : EIO : MTREW ");
4811 			rval = EIO;
4812 		}
4813 		break;
4814 
4815 	case MTOFFL:
4816 		/*
4817 		 * rewinds, and, if appropriate, takes the device offline by
4818 		 * unloading the tape
4819 		 */
4820 		if (st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK)) {
4821 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4822 			    "st_mtioctop :EIO:MTOFFL check density/wfm failed");
4823 			return (EIO);
4824 		}
4825 		(void) st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD);
4826 		if (st_cmd(dev, SCMD_LOAD, 0, SYNC_CMD)) {
4827 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4828 			    "st_mtioctop : EIO : MTOFFL");
4829 			return (EIO);
4830 		}
4831 		un->un_eof = ST_NO_EOF;
4832 		un->un_laststate = un->un_state;
4833 		un->un_state = ST_STATE_OFFLINE;
4834 		un->un_mediastate = MTIO_EJECTED;
4835 		break;
4836 
4837 	case MTLOAD:
4838 		/*
4839 		 * This is to load a tape into the drive
4840 		 * Note that if the tape is not loaded, the device will have
4841 		 * to be opened via O_NDELAY or O_NONBLOCK.
4842 		 */
4843 		/*
4844 		 * Let's try and clean things up, if we are not
4845 		 * initializing, and then send in the load command, no
4846 		 * matter what.
4847 		 *
4848 		 * load after a media change by the user.
4849 		 */
4850 
4851 		if (un->un_state > ST_STATE_INITIALIZING)
4852 			(void) st_check_density_or_wfm(dev, 1, 0, NO_STEPBACK);
4853 		rval = st_cmd(dev, SCMD_LOAD, 1, SYNC_CMD);
4854 		if (rval) {
4855 			if (rval != EACCES) {
4856 				rval = EIO;
4857 			}
4858 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4859 			    "st_mtioctop : %s : MTLOAD\n",
4860 			    rval == EACCES ? "EACCES" : "EIO");
4861 			/*
4862 			 * If load tape fails, who knows what happened...
4863 			 */
4864 			un->un_fileno = -1;
4865 			break;
4866 		}
4867 
4868 		/*
4869 		 * reset all counters appropriately using rewind, as if LOAD
4870 		 * succeeds, we are at BOT
4871 		 */
4872 		un->un_state = ST_STATE_INITIALIZING;
4873 
4874 		rval = st_tape_init(dev);
4875 		if ((rval == EACCES) && (un->un_read_only & WORM)) {
4876 			rval = 0;
4877 			break;
4878 		}
4879 
4880 		if (rval != 0) {
4881 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4882 			    "st_mtioctop : EIO : MTLOAD calls st_tape_init\n");
4883 			rval = EIO;
4884 			un->un_state = ST_STATE_OFFLINE;
4885 		}
4886 
4887 		break;
4888 
4889 	case MTNOP:
4890 		un->un_status = 0;		/* Reset status */
4891 		un->un_err_resid = 0;
4892 		break;
4893 
4894 	case MTEOM:
4895 		/*
4896 		 * positions the tape at a location just after the last file
4897 		 * written on the tape. For cartridge and 8 mm, this after
4898 		 * the last file mark; for reel, this is inbetween the two
4899 		 * last 2 file marks
4900 		 */
4901 		if ((un->un_eof >= ST_EOT) ||
4902 		    (un->un_lastop == ST_OP_WRITE) ||
4903 		    (un->un_lastop == ST_OP_WEOF)) {
4904 			/*
4905 			 * If the command wants to move to logical end
4906 			 * of media, and we're already there, we're done.
4907 			 * If we were at logical eot, we reset the state
4908 			 * to be *not* at logical eot.
4909 			 *
4910 			 * If we're at physical or logical eot, we prohibit
4911 			 * forward space operations (unconditionally).
4912 			 *
4913 			 * Also if the last operation was a write of any
4914 			 * kind the tape is at EOD.
4915 			 */
4916 			return (0);
4917 		}
4918 		/*
4919 		 * physical tape position may not be what we've been
4920 		 * telling the user; adjust the request accordingly
4921 		 */
4922 		if (IN_EOF(un)) {
4923 			un->un_fileno++;
4924 			un->un_blkno = 0;
4925 		}
4926 
4927 		if (st_check_density_or_wfm(dev, 1, B_READ, NO_STEPBACK)) {
4928 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4929 			    "st_mtioctop : EIO:MTEOM check density/wfm failed");
4930 			return (EIO);
4931 		}
4932 
4933 		/*
4934 		 * st_find_eom() returns the last fileno we knew about;
4935 		 */
4936 		savefile = st_find_eom(dev);
4937 
4938 		if ((un->un_status != KEY_BLANK_CHECK) &&
4939 		    (un->un_status != SUN_KEY_EOT)) {
4940 			un->un_fileno = -1;
4941 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4942 			    "st_mtioctop : EIO : MTEOM status check failed");
4943 			rval = EIO;
4944 		} else {
4945 			/*
4946 			 * For 1/2" reel tapes assume logical EOT marked
4947 			 * by two file marks or we don't care that we may
4948 			 * be extending the last file on the tape.
4949 			 */
4950 			if (un->un_dp->options & ST_REEL) {
4951 				if (st_cmd(dev, SCMD_SPACE, Fmk((-1)),
4952 				    SYNC_CMD)) {
4953 					un->un_fileno = -1;
4954 					ST_DEBUG2(ST_DEVINFO, st_label,
4955 					    SCSI_DEBUG,
4956 					    "st_mtioctop : EIO : MTEOM space "
4957 					    "cmd failed");
4958 					rval = EIO;
4959 					break;
4960 				}
4961 				/*
4962 				 * Fix up the block number.
4963 				 */
4964 				un->un_blkno = 0;
4965 				un->un_err_blkno = 0;
4966 			}
4967 			un->un_err_resid = 0;
4968 			un->un_fileno = savefile;
4969 			un->un_eof = ST_EOT;
4970 		}
4971 		un->un_status = 0;
4972 		break;
4973 
4974 	case MTFSF:
4975 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
4976 		    "fsf: count=%lx, eof=%x\n", mtop->mt_count,
4977 			un->un_eof);
4978 		/*
4979 		 * forward space over filemark
4980 		 *
4981 		 * For ASF we allow a count of 0 on fsf which means
4982 		 * we just want to go to beginning of current file.
4983 		 * Equivalent to "nbsf(0)" or "bsf(1) + fsf".
4984 		 * Allow stepping over double fmk with reel
4985 		 */
4986 		if ((un->un_eof >= ST_EOT) && (mtop->mt_count > 0) &&
4987 		    ((un->un_dp->options & ST_REEL) == 0)) {
4988 			/* we're at EOM */
4989 			un->un_err_resid = mtop->mt_count;
4990 			un->un_status = KEY_BLANK_CHECK;
4991 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
4992 			    "st_mtioctop : EIO : MTFSF at EOM");
4993 			return (EIO);
4994 		}
4995 
4996 		/*
4997 		 * physical tape position may not be what we've been
4998 		 * telling the user; adjust the request accordingly
4999 		 */
5000 		if (IN_EOF(un)) {
5001 			un->un_fileno++;
5002 			un->un_blkno = 0;
5003 			/*
5004 			 * For positive direction case, we're now covered.
5005 			 * For zero or negative direction, we're covered
5006 			 * (almost)
5007 			 */
5008 			mtop->mt_count--;
5009 		}
5010 
5011 		if (st_check_density_or_wfm(dev, 1, B_READ, STEPBACK)) {
5012 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5013 			    "st_mtioctop : EIO : MTFSF density/wfm failed");
5014 			return (EIO);
5015 		}
5016 
5017 
5018 		/*
5019 		 * Forward space file marks.
5020 		 * We leave ourselves at block zero
5021 		 * of the target file number.
5022 		 */
5023 		if (mtop->mt_count < 0) {
5024 			mtop->mt_count = -mtop->mt_count;
5025 			mtop->mt_op = MTNBSF;
5026 			goto bspace;
5027 		}
5028 fspace:
5029 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5030 		    "fspace: count=%lx, eof=%x\n", mtop->mt_count,
5031 			un->un_eof);
5032 		if ((tmp = mtop->mt_count) == 0) {
5033 			if (un->un_blkno == 0) {
5034 				un->un_err_resid = 0;
5035 				un->un_err_fileno = un->un_fileno;
5036 				un->un_err_blkno = un->un_blkno;
5037 				break;
5038 			} else if (un->un_fileno == 0) {
5039 				rval = st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD);
5040 			} else if (un->un_dp->options & ST_BSF) {
5041 				rval = (st_cmd(dev, SCMD_SPACE, Fmk((-1)),
5042 				    SYNC_CMD) ||
5043 				    st_cmd(dev, SCMD_SPACE, Fmk(1), SYNC_CMD));
5044 			} else {
5045 				tmp = un->un_fileno;
5046 				rval = (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) ||
5047 				    st_cmd(dev, SCMD_SPACE, (int)Fmk(tmp),
5048 				    SYNC_CMD));
5049 			}
5050 			if (rval != 0) {
5051 				un->un_fileno = -1;
5052 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5053 				    "st_mtioctop : EIO : fspace fileno = -1");
5054 
5055 				rval = EIO;
5056 			}
5057 		} else {
5058 			rval = st_space_fmks(dev, tmp);
5059 		}
5060 
5061 		if (mtop->mt_op == MTBSF && rval != EIO) {
5062 			/*
5063 			 * we came here with a count < 0; we now need
5064 			 * to skip back to end up before the filemark
5065 			 */
5066 			mtop->mt_count = 1;
5067 			goto bspace;
5068 		}
5069 		break;
5070 
5071 
5072 	case MTFSR:
5073 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5074 		    "fsr: count=%lx, eof=%x\n", mtop->mt_count,
5075 			un->un_eof);
5076 		/*
5077 		 * forward space to inter-record gap
5078 		 *
5079 		 */
5080 		if ((un->un_eof >= ST_EOT) && (mtop->mt_count > 0)) {
5081 			/* we're at EOM */
5082 			un->un_err_resid = mtop->mt_count;
5083 			un->un_status = KEY_BLANK_CHECK;
5084 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5085 			    "st_mtioctop : EIO : MTFSR un_eof > ST_EOT");
5086 			return (EIO);
5087 		}
5088 
5089 		if (mtop->mt_count == 0) {
5090 			un->un_err_fileno = un->un_fileno;
5091 			un->un_err_blkno = un->un_blkno;
5092 			un->un_err_resid = 0;
5093 			if (IN_EOF(un) && SVR4_BEHAVIOR) {
5094 				un->un_status = SUN_KEY_EOF;
5095 			}
5096 			return (0);
5097 		}
5098 
5099 		/*
5100 		 * physical tape position may not be what we've been
5101 		 * telling the user; adjust the position accordingly
5102 		 */
5103 		if (IN_EOF(un)) {
5104 			daddr_t blkno = un->un_blkno;
5105 			int fileno = un->un_fileno;
5106 			uchar_t lastop = un->un_lastop;
5107 			if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)
5108 			    == -1) {
5109 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5110 				    "st_mtioctop : EIO :MTFSR count && IN_EOF");
5111 				return (EIO);
5112 			}
5113 
5114 			un->un_blkno = blkno;
5115 			un->un_fileno = fileno;
5116 			un->un_lastop = lastop;
5117 		}
5118 
5119 		if (st_check_density_or_wfm(dev, 1, B_READ, STEPBACK)) {
5120 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5121 			    "st_mtioctop : EIO : MTFSR st_check_den");
5122 			return (EIO);
5123 		}
5124 
5125 space_records:
5126 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5127 		    "space_records: count=%lx, eof=%x\n", mtop->mt_count,
5128 			un->un_eof);
5129 		tmp = un->un_blkno + mtop->mt_count;
5130 		if (tmp == un->un_blkno) {
5131 			un->un_err_resid = 0;
5132 			un->un_err_fileno = un->un_fileno;
5133 			un->un_err_blkno = un->un_blkno;
5134 			break;
5135 		} else if (un->un_blkno < tmp ||
5136 		    (un->un_dp->options & ST_BSR)) {
5137 			/*
5138 			 * If we're spacing forward, or the device can
5139 			 * backspace records, we can just use the SPACE
5140 			 * command.
5141 			 */
5142 			tmp = tmp - un->un_blkno;
5143 			if (st_cmd(dev, SCMD_SPACE, Blk(tmp), SYNC_CMD)) {
5144 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5145 				    "st_mtioctop :EIO:space_records can't spc");
5146 				rval = EIO;
5147 			} else if (un->un_eof >= ST_EOF_PENDING) {
5148 				/*
5149 				 * check if we hit BOT/EOT
5150 				 */
5151 				if (tmp < 0 && un->un_eof == ST_EOM) {
5152 					un->un_status = SUN_KEY_BOT;
5153 					un->un_eof = ST_NO_EOF;
5154 				} else if (tmp < 0 && un->un_eof ==
5155 				    ST_EOF_PENDING) {
5156 					int residue = un->un_err_resid;
5157 					/*
5158 					 * we skipped over a filemark
5159 					 * and need to go forward again
5160 					 */
5161 					if (st_cmd(dev, SCMD_SPACE, Fmk(1),
5162 					    SYNC_CMD)) {
5163 						ST_DEBUG2(ST_DEVINFO,
5164 						    st_label, SCSI_DEBUG,
5165 						    "st_mtioctop : EIO : "
5166 						    "space_records can't "
5167 						    "space #2");
5168 						rval = EIO;
5169 					}
5170 					un->un_err_resid = residue;
5171 				}
5172 				if (rval == 0) {
5173 					ST_DEBUG2(ST_DEVINFO, st_label,
5174 					    SCSI_DEBUG,
5175 					    "st_mtioctop : EIO : space_rec rval"
5176 					    " == 0");
5177 					rval = EIO;
5178 				}
5179 			}
5180 		} else {
5181 			/*
5182 			 * else we rewind, space forward across filemarks to
5183 			 * the desired file, and then space records to the
5184 			 * desired block.
5185 			 */
5186 
5187 			int t = un->un_fileno;	/* save current file */
5188 
5189 			if (tmp < 0) {
5190 				/*
5191 				 * Wups - we're backing up over a filemark
5192 				 */
5193 				if (un->un_blkno != 0 &&
5194 				    (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) ||
5195 				    st_cmd(dev, SCMD_SPACE, Fmk(t), SYNC_CMD)))
5196 					un->un_fileno = -1;
5197 				un->un_err_resid = -tmp;
5198 				if (un->un_fileno == 0 && un->un_blkno == 0) {
5199 					un->un_status = SUN_KEY_BOT;
5200 					un->un_eof = ST_NO_EOF;
5201 				} else if (un->un_fileno > 0) {
5202 					un->un_status = SUN_KEY_EOF;
5203 					un->un_eof = ST_NO_EOF;
5204 				}
5205 				un->un_err_fileno = un->un_fileno;
5206 				un->un_err_blkno = un->un_blkno;
5207 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5208 				    "st_mtioctop :EIO:space_records : tmp < 0");
5209 				rval = EIO;
5210 			} else if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD) ||
5211 				    st_cmd(dev, SCMD_SPACE, Fmk(t), SYNC_CMD) ||
5212 				    st_cmd(dev, SCMD_SPACE, Blk(tmp),
5213 					SYNC_CMD)) {
5214 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5215 				    "st_mtioctop : EIO :space_records : rewind "
5216 				    "and space failed");
5217 				un->un_fileno = -1;
5218 				rval = EIO;
5219 			}
5220 		}
5221 		break;
5222 
5223 
5224 	case MTBSF:
5225 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5226 		    "bsf: count=%lx, eof=%x\n", mtop->mt_count,
5227 			un->un_eof);
5228 		/*
5229 		 * backward space of file filemark (1/2" and 8mm)
5230 		 * tape position will end on the beginning of tape side
5231 		 * of the desired file mark
5232 		 */
5233 		if ((un->un_dp->options & ST_BSF) == 0) {
5234 			return (ENOTTY);
5235 		}
5236 
5237 		/*
5238 		 * If a negative count (which implies a forward space op)
5239 		 * is specified, and we're at logical or physical eot,
5240 		 * bounce the request.
5241 		 */
5242 
5243 		if (un->un_eof >= ST_EOT && mtop->mt_count < 0) {
5244 			un->un_err_resid = mtop->mt_count;
5245 			un->un_status = SUN_KEY_EOT;
5246 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5247 			    "st_ioctl : EIO : MTBSF : un_eof > ST_EOF");
5248 			return (EIO);
5249 		}
5250 		/*
5251 		 * physical tape position may not be what we've been
5252 		 * telling the user; adjust the request accordingly
5253 		 */
5254 		if (IN_EOF(un)) {
5255 			un->un_fileno++;
5256 			un->un_blkno = 0;
5257 			mtop->mt_count++;
5258 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5259 			"bsf in eof: count=%ld, op=%x\n",
5260 			mtop->mt_count, mtop->mt_op);
5261 
5262 		}
5263 
5264 		if (st_check_density_or_wfm(dev, 1, 0, STEPBACK)) {
5265 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5266 			    "st_ioctl : EIO : MTBSF : check den wfm");
5267 			return (EIO);
5268 		}
5269 
5270 		if (mtop->mt_count <= 0) {
5271 			/*
5272 			 * for a negative count, we need to step forward
5273 			 * first and then step back again
5274 			 */
5275 			mtop->mt_count = -mtop->mt_count+1;
5276 			goto fspace;
5277 		}
5278 
5279 bspace:
5280 	{
5281 		int skip_cnt, end_at_eof;
5282 
5283 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5284 		    "bspace: count=%lx, eof=%x\n", mtop->mt_count,
5285 			un->un_eof);
5286 		/*
5287 		 * Backspace files (MTNBSF):
5288 		 *
5289 		 *	For tapes that can backspace, backspace
5290 		 *	count+1 filemarks and then run forward over
5291 		 *	a filemark
5292 		 *
5293 		 *	For tapes that can't backspace,
5294 		 *		calculate desired filenumber
5295 		 *		(un->un_fileno - count), rewind,
5296 		 *		and then space forward this amount
5297 		 *
5298 		 * Backspace filemarks (MTBSF)
5299 		 *
5300 		 *	For tapes that can backspace, backspace count
5301 		 *	filemarks
5302 		 *
5303 		 *	For tapes that can't backspace, calculate
5304 		 *	desired filenumber (un->un_fileno - count),
5305 		 *	add 1, rewind, space forward this amount,
5306 		 *	and mark state as ST_EOF_PENDING appropriately.
5307 		 */
5308 
5309 		if (mtop->mt_op == MTBSF) {
5310 			end_at_eof = 1;
5311 		} else {
5312 			end_at_eof = 0;
5313 		}
5314 
5315 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5316 		    "bspace: mt_op=%x, count=%lx, fileno=%x, blkno=%lx\n",
5317 		    mtop->mt_op, mtop->mt_count, un->un_fileno, un->un_blkno);
5318 
5319 		/*
5320 		 * Handle the simple case of BOT
5321 		 * playing a role in these cmds.
5322 		 * We do this by calculating the
5323 		 * ending file number. If the ending
5324 		 * file is < BOT, rewind and set an
5325 		 * error and mark resid appropriately.
5326 		 * If we're backspacing a file (not a
5327 		 * filemark) and the target file is
5328 		 * the first file on the tape, just
5329 		 * rewind.
5330 		 */
5331 
5332 		tmp = un->un_fileno - mtop->mt_count;
5333 		if ((end_at_eof && tmp < 0) || (end_at_eof == 0 && tmp <= 0)) {
5334 			if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) {
5335 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5336 				    "st_ioctl : EIO : bspace : end_at_eof && "
5337 				    "tmp < 0");
5338 				rval = EIO;
5339 			}
5340 			if (tmp < 0) {
5341 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5342 				    "st_ioctl : EIO : bspace : tmp < 0");
5343 				rval = EIO;
5344 				un->un_err_resid = -tmp;
5345 				un->un_status = SUN_KEY_BOT;
5346 			}
5347 			break;
5348 		}
5349 
5350 		if (un->un_dp->options & ST_BSF) {
5351 			skip_cnt = 1 - end_at_eof;
5352 			/*
5353 			 * If we are going to end up at the beginning
5354 			 * of the file, we have to space one extra file
5355 			 * first, and then space forward later.
5356 			 */
5357 			tmp = -(mtop->mt_count + skip_cnt);
5358 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
5359 			    "skip_cnt=%x, tmp=%x\n", skip_cnt, tmp);
5360 			if (st_cmd(dev, SCMD_SPACE, Fmk(tmp), SYNC_CMD)) {
5361 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5362 				    "st_ioctl : EIO : bspace : can't space "
5363 				    "tmp");
5364 				rval = EIO;
5365 			}
5366 		} else {
5367 			if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) {
5368 				rval = EIO;
5369 			} else {
5370 				skip_cnt = tmp + end_at_eof;
5371 			}
5372 		}
5373 
5374 		/*
5375 		 * If we have to space forward, do so...
5376 		 */
5377 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
5378 		    "space forward skip_cnt=%x, rval=%x\n", skip_cnt, rval);
5379 		if (rval == 0 && skip_cnt) {
5380 			if (st_cmd(dev, SCMD_SPACE, Fmk(skip_cnt), SYNC_CMD)) {
5381 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5382 				    "st_ioctl : EIO : bspace : can't space "
5383 				    "skip_cnt");
5384 				rval = EIO;
5385 			} else if (end_at_eof) {
5386 				/*
5387 				 * If we had to space forward, and we're
5388 				 * not a tape that can backspace, mark state
5389 				 * as if we'd just seen a filemark during a
5390 				 * a read.
5391 				 */
5392 				if ((un->un_dp->options & ST_BSF) == 0) {
5393 					un->un_eof = ST_EOF_PENDING;
5394 					un->un_fileno -= 1;
5395 					un->un_blkno = INF;
5396 				}
5397 			}
5398 		}
5399 
5400 		if (rval != 0) {
5401 			un->un_fileno = -1;
5402 		}
5403 		break;
5404 	}
5405 
5406 	case MTNBSF:
5407 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5408 		    "nbsf: count=%lx, eof=%x\n", mtop->mt_count,
5409 			un->un_eof);
5410 		/*
5411 		 * backward space file to beginning of file
5412 		 *
5413 		 * If a negative count (which implies a forward space op)
5414 		 * is specified, and we're at logical or physical eot,
5415 		 * bounce the request.
5416 		 */
5417 
5418 		if (un->un_eof >= ST_EOT && mtop->mt_count < 0) {
5419 			un->un_err_resid = mtop->mt_count;
5420 			un->un_status = SUN_KEY_EOT;
5421 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5422 			    "st_ioctl : EIO : > EOT and count < 0");
5423 			return (EIO);
5424 		}
5425 		/*
5426 		 * physical tape position may not be what we've been
5427 		 * telling the user; adjust the request accordingly
5428 		 */
5429 		if (IN_EOF(un)) {
5430 			un->un_fileno++;
5431 			un->un_blkno = 0;
5432 			mtop->mt_count++;
5433 		}
5434 
5435 		if (st_check_density_or_wfm(dev, 1, 0, STEPBACK)) {
5436 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5437 			    "st_ioctl : EIO : MTNBSF check den and wfm");
5438 			return (EIO);
5439 		}
5440 
5441 mtnbsf:
5442 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5443 		    "mtnbsf: count=%lx, eof=%x\n", mtop->mt_count,
5444 			un->un_eof);
5445 		if (mtop->mt_count <= 0) {
5446 			mtop->mt_op = MTFSF;
5447 			mtop->mt_count = -mtop->mt_count;
5448 			goto fspace;
5449 		}
5450 		goto bspace;
5451 
5452 	case MTBSR:
5453 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5454 		    "bsr: count=%lx, eof=%x\n", mtop->mt_count,
5455 			un->un_eof);
5456 		/*
5457 		 * backward space into inter-record gap
5458 		 *
5459 		 * If a negative count (which implies a forward space op)
5460 		 * is specified, and we're at logical or physical eot,
5461 		 * bounce the request.
5462 		 */
5463 		if (un->un_eof >= ST_EOT && mtop->mt_count < 0) {
5464 			un->un_err_resid = mtop->mt_count;
5465 			un->un_status = SUN_KEY_EOT;
5466 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5467 			    "st_ioctl : EIO : MTBSR > EOT");
5468 			return (EIO);
5469 		}
5470 
5471 		if (mtop->mt_count == 0) {
5472 			un->un_err_fileno = un->un_fileno;
5473 			un->un_err_blkno = un->un_blkno;
5474 			un->un_err_resid = 0;
5475 			if (IN_EOF(un) && SVR4_BEHAVIOR) {
5476 				un->un_status = SUN_KEY_EOF;
5477 			}
5478 			return (0);
5479 		}
5480 
5481 		/*
5482 		 * physical tape position may not be what we've been
5483 		 * telling the user; adjust the position accordingly.
5484 		 * bsr can not skip filemarks and continue to skip records
5485 		 * therefore if we are logically before the filemark but
5486 		 * physically at the EOT side of the filemark, we need to step
5487 		 * back; this allows fsr N where N > number of blocks in file
5488 		 * followed by bsr 1 to position at the beginning of last block
5489 		 */
5490 		if (IN_EOF(un)) {
5491 			int blkno = un->un_blkno;
5492 			int fileno = un->un_fileno;
5493 			uchar_t lastop = un->un_lastop;
5494 			if (st_cmd(dev, SCMD_SPACE, Fmk((-1)), SYNC_CMD)
5495 			    == -1) {
5496 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5497 				    "st_write_fm : EIO : MTBSR can't space");
5498 				return (EIO);
5499 			}
5500 
5501 			un->un_blkno = blkno;
5502 			un->un_fileno = fileno;
5503 			un->un_lastop = lastop;
5504 		}
5505 
5506 		un->un_eof = ST_NO_EOF;
5507 
5508 		if (st_check_density_or_wfm(dev, 1, 0, STEPBACK)) {
5509 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5510 			    "st_ioctl : EIO : MTBSR : can't set density or "
5511 			    "wfm");
5512 			return (EIO);
5513 		}
5514 
5515 		mtop->mt_count = -mtop->mt_count;
5516 		goto space_records;
5517 
5518 	case MTSRSZ:
5519 
5520 		/*
5521 		 * Set record-size to that sent by user
5522 		 * Check to see if there is reason that the requested
5523 		 * block size should not be set.
5524 		 */
5525 
5526 		/* If requesting variable block size is it ok? */
5527 		if ((mtop->mt_count == 0) &&
5528 		    ((un->un_dp->options & ST_VARIABLE) == 0)) {
5529 			return (ENOTTY);
5530 		}
5531 
5532 		/*
5533 		 * If requested block size is not variable "0",
5534 		 * is it less then minimum.
5535 		 */
5536 		if ((mtop->mt_count != 0) &&
5537 		    (mtop->mt_count < un->un_minbsize)) {
5538 			return (EINVAL);
5539 		}
5540 
5541 		/* Is the requested block size more then maximum */
5542 		if ((mtop->mt_count > min(un->un_maxbsize, un->un_maxdma)) &&
5543 		    (un->un_maxbsize != 0)) {
5544 			return (EINVAL);
5545 		}
5546 
5547 		/* Is requested block size a modulus the device likes */
5548 		if ((mtop->mt_count % un->un_data_mod) != 0) {
5549 			return (EINVAL);
5550 		}
5551 
5552 		if (st_change_block_size(dev, (uint32_t)mtop->mt_count) != 0) {
5553 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5554 			    "st_ioctl : MTSRSZ : EIO : cant set block size");
5555 			return (EIO);
5556 		}
5557 
5558 		return (0);
5559 
5560 	case MTGRSZ:
5561 	{
5562 #ifdef _MULTI_DATAMODEL
5563 	/*
5564 	 * For use when a 32 bit app makes a call into a
5565 	 * 64 bit ioctl
5566 	 */
5567 	struct mtop32	mtop_32_for_64;
5568 #endif /* _MULTI_DATAMODEL */
5569 
5570 
5571 		/*
5572 		 * Get record-size to the user
5573 		 */
5574 		mtop->mt_count = un->un_bsize;
5575 
5576 #ifdef _MULTI_DATAMODEL
5577 		switch (ddi_model_convert_from(flag & FMODELS)) {
5578 		case DDI_MODEL_ILP32:
5579 			/*
5580 			 * Convert 64 bit back to 32 bit before doing
5581 			 * copyout. This is what the ILP32 app expects.
5582 			 */
5583 			mtop_32_for_64.mt_op = mtop->mt_op;
5584 			mtop_32_for_64.mt_count = mtop->mt_count;
5585 
5586 			if (ddi_copyout(&mtop_32_for_64, (void *)arg,
5587 			    sizeof (struct mtop32), flag)) {
5588 				return (EFAULT);
5589 			}
5590 			break;
5591 
5592 		case DDI_MODEL_NONE:
5593 			if (ddi_copyout(mtop, (void *)arg,
5594 			    sizeof (struct mtop), flag)) {
5595 				return (EFAULT);
5596 			}
5597 			break;
5598 		}
5599 #else /* ! _MULTI_DATAMODE */
5600 		if (ddi_copyout(mtop, (void *)arg, sizeof (struct mtop), flag))
5601 			return (EFAULT);
5602 
5603 #endif /* _MULTI_DATAMODE */
5604 
5605 		return (0);
5606 	}
5607 	default:
5608 		rval = ENOTTY;
5609 	}
5610 
5611 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
5612 	    "st_ioctl: fileno=%x, blkno=%lx, un_eof=%x\n", un->un_fileno,
5613 	    un->un_blkno, un->un_eof);
5614 
5615 	if (un->un_fileno < 0) {
5616 		un->un_density_known = 0;
5617 	}
5618 
5619 	ASSERT(mutex_owned(ST_MUTEX));
5620 	return (rval);
5621 }
5622 
5623 
5624 /*
5625  * Run a command for uscsi ioctl.
5626  * cdbspace is address space of cdb.
5627  * dataspace is address space of the uscsi data buffer.
5628  */
5629 static int
5630 st_ioctl_cmd(dev_t dev, struct uscsi_cmd *ucmd,
5631 	enum uio_seg cdbspace, enum uio_seg dataspace,
5632 	enum uio_seg rqbufspace)
5633 {
5634 	struct buf *bp;
5635 	struct uscsi_cmd *kcmd;
5636 	caddr_t kcdb;
5637 	int flag;
5638 	int err;
5639 	int rqlen;
5640 	int offline_state = 0;
5641 	char *krqbuf = NULL;
5642 
5643 	GET_SOFT_STATE(dev);
5644 
5645 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
5646 	    "st_ioctl_cmd(dev = 0x%lx)\n", dev);
5647 
5648 	ASSERT(mutex_owned(ST_MUTEX));
5649 
5650 	/*
5651 	 * We really don't know what commands are coming in here and
5652 	 * we don't want to limit the commands coming in.
5653 	 *
5654 	 * If st_tape_init() gets called from st_strategy(), then we
5655 	 * will hang the process waiting for un->un_sbuf_busy to be cleared,
5656 	 * which it never will, as we set it below.  To prevent
5657 	 * st_tape_init() from getting called, we have to set state to other
5658 	 * than ST_STATE_OFFLINE, so we choose ST_STATE_INITIALIZING, which
5659 	 * achieves this purpose already
5660 	 *
5661 	 * We use offline_state to preserve the OFFLINE state, if it exists,
5662 	 * so other entry points to the driver might have the chance to call
5663 	 * st_tape_init().
5664 	 */
5665 	if (un->un_state == ST_STATE_OFFLINE) {
5666 		un->un_laststate = ST_STATE_OFFLINE;
5667 		un->un_state = ST_STATE_INITIALIZING;
5668 		offline_state = 1;
5669 	}
5670 	/*
5671 	 * Is this a request to reset the bus?
5672 	 * If so, we need go no further.
5673 	 */
5674 	if (ucmd->uscsi_flags & (USCSI_RESET|USCSI_RESET_ALL)) {
5675 		flag = ((ucmd->uscsi_flags & USCSI_RESET_ALL)) ?
5676 			RESET_ALL : RESET_TARGET;
5677 
5678 		mutex_exit(ST_MUTEX);
5679 		err = (scsi_reset(ROUTE, flag)) ? 0 : EIO;
5680 		mutex_enter(ST_MUTEX);
5681 
5682 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
5683 			"reset %s %s\n",
5684 			(flag == RESET_ALL) ? "all" : "target",
5685 			(err == 0) ? "ok" : "failed");
5686 		/*
5687 		 * If scsi reset successful, don't write any filemarks.
5688 		 */
5689 		if (err == 0) {
5690 			un->un_fmneeded = 0;
5691 		} else {
5692 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5693 			    "st_ioctl_cmd : EIO : scsi_reset failed");
5694 		}
5695 		goto exit;
5696 	}
5697 
5698 	/*
5699 	 * First do some sanity checks for USCSI commands.
5700 	 */
5701 	if ((ucmd->uscsi_cdblen <= 0) ||
5702 	    (ucmd->uscsi_cdblen > un->un_max_cdb_sz)) {
5703 		if (cdbspace != UIO_SYSSPACE) {
5704 			scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
5705 			    "USCSICMD with cdb larger then transport supports");
5706 		}
5707 		return (EINVAL);
5708 	}
5709 
5710 	/*
5711 	 * In order to not worry about where the uscsi structure
5712 	 * or cdb it points to came from, we kmem_alloc copies
5713 	 * of them here.  This will allow reference to the data
5714 	 * they contain long after this process has gone to
5715 	 * sleep and its kernel stack has been unmapped, etc.
5716 	 */
5717 
5718 	kcdb = kmem_alloc((size_t)ucmd->uscsi_cdblen, KM_SLEEP);
5719 	if (cdbspace == UIO_SYSSPACE) {
5720 		bcopy(ucmd->uscsi_cdb, kcdb, ucmd->uscsi_cdblen);
5721 	} else {
5722 		if (ddi_copyin(ucmd->uscsi_cdb, kcdb,
5723 		    (size_t)ucmd->uscsi_cdblen, 0)) {
5724 			kmem_free(kcdb, (size_t)ucmd->uscsi_cdblen);
5725 			err = EFAULT;
5726 			goto exit;
5727 		}
5728 	}
5729 
5730 	/*
5731 	 * can't peek at the cdb till is copied into kernal space.
5732 	 */
5733 	if (scsi_cdb_size[CDB_GROUPID(kcdb[0])] > un->un_max_cdb_sz) {
5734 		if (cdbspace != UIO_SYSSPACE) {
5735 			scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
5736 			    "USCSICMD with cdb larger then transport supports");
5737 		}
5738 		kmem_free(kcdb, ucmd->uscsi_cdblen);
5739 		return (EINVAL);
5740 	}
5741 	kcmd = kmem_alloc(sizeof (struct uscsi_cmd), KM_SLEEP);
5742 	bcopy(ucmd, kcmd, sizeof (struct uscsi_cmd));
5743 	kcmd->uscsi_cdb = kcdb;
5744 
5745 	flag = (kcmd->uscsi_flags & USCSI_READ) ? B_READ : B_WRITE;
5746 
5747 	/* check to see if this command requires the drive to be reserved */
5748 	err = st_check_cdb_for_need_to_reserve(un, &kcdb[0]);
5749 
5750 	if (err) {
5751 		goto exit_free;
5752 	}
5753 
5754 	/*
5755 	 * Get buffer resources...
5756 	 */
5757 	while (un->un_sbuf_busy)
5758 		cv_wait(&un->un_sbuf_cv, ST_MUTEX);
5759 	un->un_sbuf_busy = 1;
5760 
5761 #ifdef STDEBUG
5762 	if (st_debug > 6) {
5763 		st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG,
5764 		    "uscsi cdb", kcdb, kcmd->uscsi_cdblen);
5765 		if (kcmd->uscsi_buflen) {
5766 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
5767 			"uscsi %s of %ld bytes %s %s space\n",
5768 			(flag == B_READ) ? rd_str : wr_str,
5769 			kcmd->uscsi_buflen,
5770 			(flag == B_READ) ? "to" : "from",
5771 			(dataspace == UIO_SYSSPACE) ? "system" : "user");
5772 		}
5773 	}
5774 #endif /* ST_DEBUG */
5775 
5776 	/*
5777 	 * Initialize Request Sense buffering, if requested.
5778 	 * For user processes, allocate a kernel copy of the sense buffer
5779 	 */
5780 	if ((kcmd->uscsi_flags & USCSI_RQENABLE) &&
5781 			kcmd->uscsi_rqlen && kcmd->uscsi_rqbuf) {
5782 		if (rqbufspace == UIO_USERSPACE) {
5783 			krqbuf = kmem_alloc(SENSE_LENGTH, KM_SLEEP);
5784 		}
5785 		kcmd->uscsi_rqlen = SENSE_LENGTH;
5786 		kcmd->uscsi_rqresid = SENSE_LENGTH;
5787 	} else {
5788 		kcmd->uscsi_rqlen = 0;
5789 		kcmd->uscsi_rqresid = 0;
5790 	}
5791 
5792 	un->un_srqbufp = krqbuf;
5793 	bp = un->un_sbufp;
5794 	bzero(bp, sizeof (buf_t));
5795 
5796 	/*
5797 	 * Force asynchronous mode, if necessary.
5798 	 */
5799 	if (ucmd->uscsi_flags & USCSI_ASYNC) {
5800 		mutex_exit(ST_MUTEX);
5801 		if (scsi_ifgetcap(ROUTE, "synchronous", 1) == 1) {
5802 			if (scsi_ifsetcap(ROUTE, "synchronous", 0, 1) == 1) {
5803 				ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
5804 				    "forced async ok\n");
5805 			} else {
5806 				ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
5807 				    "forced async failed\n");
5808 				err = EINVAL;
5809 				mutex_enter(ST_MUTEX);
5810 				goto done;
5811 			}
5812 		}
5813 		mutex_enter(ST_MUTEX);
5814 	}
5815 
5816 	/*
5817 	 * Re-enable synchronous mode, if requested
5818 	 */
5819 	if (ucmd->uscsi_flags & USCSI_SYNC) {
5820 		mutex_exit(ST_MUTEX);
5821 		if (scsi_ifgetcap(ROUTE, "synchronous", 1) == 0) {
5822 			int i = scsi_ifsetcap(ROUTE, "synchronous", 1, 1);
5823 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
5824 				"re-enabled sync %s\n",
5825 				(i == 1) ? "ok" : "failed");
5826 		}
5827 		mutex_enter(ST_MUTEX);
5828 	}
5829 
5830 	if (kcmd->uscsi_buflen) {
5831 		/*
5832 		 * We're going to do actual I/O.
5833 		 * Set things up for physio.
5834 		 */
5835 		struct iovec aiov;
5836 		struct uio auio;
5837 		struct uio *uio = &auio;
5838 
5839 		bzero(&auio, sizeof (struct uio));
5840 		bzero(&aiov, sizeof (struct iovec));
5841 		aiov.iov_base = kcmd->uscsi_bufaddr;
5842 		aiov.iov_len = kcmd->uscsi_buflen;
5843 
5844 		uio->uio_iov = &aiov;
5845 		uio->uio_iovcnt = 1;
5846 		uio->uio_resid = aiov.iov_len;
5847 		uio->uio_segflg = dataspace;
5848 
5849 		/*
5850 		 * Let physio do the rest...
5851 		 */
5852 		bp->b_forw = (struct buf *)(uintptr_t)kcdb[0];
5853 		bp->b_back = (struct buf *)kcmd;
5854 
5855 		mutex_exit(ST_MUTEX);
5856 		err = physio(st_strategy, bp, dev, flag, st_uscsi_minphys, uio);
5857 		mutex_enter(ST_MUTEX);
5858 	} else {
5859 		/*
5860 		 * Mimic physio
5861 		 */
5862 		bp->b_forw = (struct buf *)(uintptr_t)kcdb[0];
5863 		bp->b_back = (struct buf *)kcmd;
5864 		bp->b_flags = B_BUSY | flag;
5865 		bp->b_edev = dev;
5866 		bp->b_dev = cmpdev(dev);
5867 		bp->b_bcount = 0;
5868 		bp->b_blkno = 0;
5869 		bp->b_resid = 0;
5870 		mutex_exit(ST_MUTEX);
5871 		(void) st_strategy(bp);
5872 
5873 		/*
5874 		 * BugTraq #4260046
5875 		 * ----------------
5876 		 * See comments in st_cmd.
5877 		 */
5878 
5879 		err = biowait(bp);
5880 		mutex_enter(ST_MUTEX);
5881 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
5882 		    "st_ioctl_cmd: biowait returns %d\n", err);
5883 	}
5884 
5885 	/*
5886 	 * Copy status from kernel copy of uscsi_cmd to user copy
5887 	 * of uscsi_cmd - this was saved in st_done_and_mutex_exit()
5888 	 */
5889 	ucmd->uscsi_status = kcmd->uscsi_status;
5890 
5891 done:
5892 	ucmd->uscsi_resid = bp->b_resid;
5893 
5894 	/*
5895 	 * Update the Request Sense status and resid
5896 	 */
5897 	rqlen = kcmd->uscsi_rqlen - kcmd->uscsi_rqresid;
5898 	rqlen = min(((int)ucmd->uscsi_rqlen), rqlen);
5899 	ucmd->uscsi_rqresid = ucmd->uscsi_rqlen - rqlen;
5900 	ucmd->uscsi_rqstatus = kcmd->uscsi_rqstatus;
5901 	/*
5902 	 * Copy out the sense data for user processes
5903 	 */
5904 	if (ucmd->uscsi_rqbuf && rqlen && rqbufspace == UIO_USERSPACE) {
5905 		if (copyout(krqbuf, ucmd->uscsi_rqbuf, rqlen)) {
5906 			err = EFAULT;
5907 		}
5908 	}
5909 
5910 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
5911 	    "st_ioctl_cmd status is 0x%x, resid is 0x%lx\n",
5912 	    ucmd->uscsi_status, ucmd->uscsi_resid);
5913 	if (DEBUGGING && (rqlen != 0)) {
5914 		int i, n, len;
5915 		char *data = krqbuf;
5916 		scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
5917 			"rqstatus=0x%x	rqlen=0x%x  rqresid=0x%x\n",
5918 			ucmd->uscsi_rqstatus, ucmd->uscsi_rqlen,
5919 			ucmd->uscsi_rqresid);
5920 		len = (int)ucmd->uscsi_rqlen - ucmd->uscsi_rqresid;
5921 		for (i = 0; i < len; i += 16) {
5922 			n = min(16, len-1);
5923 			st_clean_print(ST_DEVINFO, st_label, CE_NOTE,
5924 				"  ", &data[i], n);
5925 		}
5926 	}
5927 
5928 exit_free:
5929 	/*
5930 	 * Free resources
5931 	 */
5932 	un->un_sbuf_busy = 0;
5933 	un->un_srqbufp = NULL;
5934 	cv_signal(&un->un_sbuf_cv);
5935 
5936 	if (krqbuf) {
5937 		kmem_free(krqbuf, SENSE_LENGTH);
5938 	}
5939 	kmem_free(kcdb, kcmd->uscsi_cdblen);
5940 	kmem_free(kcmd, sizeof (struct uscsi_cmd));
5941 
5942 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
5943 	    "st_ioctl_cmd returns 0x%x\n", err);
5944 
5945 
5946 exit:
5947 	/* don't lose offline state */
5948 	if (offline_state)
5949 		un->un_state = ST_STATE_OFFLINE;
5950 
5951 	ASSERT(mutex_owned(ST_MUTEX));
5952 	return (err);
5953 }
5954 
5955 static int
5956 st_write_fm(dev_t dev, int wfm)
5957 {
5958 	int i;
5959 	int rval;
5960 
5961 	GET_SOFT_STATE(dev);
5962 
5963 	ASSERT(mutex_owned(ST_MUTEX));
5964 
5965 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
5966 	    "st_write_fm(dev = 0x%lx, wfm = %d)\n", dev, wfm);
5967 
5968 	/*
5969 	 * write one filemark at the time after EOT
5970 	 */
5971 	if (un->un_eof >= ST_EOT) {
5972 		for (i = 0; i < wfm; i++) {
5973 			rval = st_cmd(dev, SCMD_WRITE_FILE_MARK, 1, SYNC_CMD);
5974 			if (rval == EACCES) {
5975 				return (rval);
5976 			}
5977 			if (rval != 0) {
5978 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5979 				    "st_write_fm : EIO : write EOT file mark");
5980 				return (EIO);
5981 			}
5982 		}
5983 	} else {
5984 		rval = st_cmd(dev, SCMD_WRITE_FILE_MARK, wfm, SYNC_CMD);
5985 		if (rval == EACCES) {
5986 			return (rval);
5987 		}
5988 		if (rval) {
5989 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
5990 			    "st_write_fm : EIO : write file mark");
5991 			return (EIO);
5992 		}
5993 	}
5994 
5995 	ASSERT(mutex_owned(ST_MUTEX));
5996 	return (0);
5997 }
5998 
5999 #ifdef STDEBUG
6000 static void
6001 start_dump(struct scsi_tape *un, struct buf *bp)
6002 {
6003 	struct scsi_pkt *pkt = BP_PKT(bp);
6004 	uchar_t *cdbp = (uchar_t *)pkt->pkt_cdbp;
6005 
6006 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
6007 	    "st_start: cmd=0x%p count=%ld resid=%ld flags=0x%x pkt=0x%p\n",
6008 	    (void *)bp->b_forw, bp->b_bcount,
6009 	    bp->b_resid, bp->b_flags, (void *)BP_PKT(bp));
6010 
6011 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
6012 	    "st_start: cdb %x %x %x %x %x %x, fileno=%d, blk=%ld\n",
6013 	    cdbp[0], cdbp[1], cdbp[2],
6014 	    cdbp[3], cdbp[4], cdbp[5], un->un_fileno,
6015 	    un->un_blkno);
6016 }
6017 #endif
6018 
6019 
6020 /*
6021  * Command start && done functions
6022  */
6023 
6024 /*
6025  * st_start()
6026  *
6027  * Called from:
6028  *  st_strategy() to start a command.
6029  *  st_runout() to retry when scsi_pkt allocation fails on previous attempt(s).
6030  *  st_attach() when resuming from power down state.
6031  *  st_start_restart() to retry transport when device was previously busy.
6032  *  st_done_and_mutex_exit() to start the next command when previous is done.
6033  *
6034  * On entry:
6035  *  scsi_pkt may or may not be allocated.
6036  *
6037  */
6038 static void
6039 st_start(struct scsi_tape *un)
6040 {
6041 	struct buf *bp;
6042 	int status;
6043 
6044 	ASSERT(mutex_owned(ST_MUTEX));
6045 
6046 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
6047 	    "st_start(): dev = 0x%lx\n", un->un_dev);
6048 
6049 	if ((bp = un->un_quef) == NULL) {
6050 		return;
6051 	}
6052 
6053 	ASSERT((bp->b_flags & B_DONE) == 0);
6054 
6055 	/*
6056 	 * Don't send more than un_throttle commands to the HBA
6057 	 */
6058 	if ((un->un_throttle <= 0) || (un->un_ncmds >= un->un_throttle)) {
6059 		return;
6060 	}
6061 
6062 	/*
6063 	 * If the buf has no scsi_pkt call st_make_cmd() to get one and
6064 	 * build the command.
6065 	 */
6066 	if (BP_PKT(bp) == NULL) {
6067 		ASSERT((bp->b_flags & B_DONE) == 0);
6068 		st_make_cmd(un, bp, st_runout);
6069 		ASSERT((bp->b_flags & B_DONE) == 0);
6070 		status = geterror(bp);
6071 
6072 		/*
6073 		 * Some HBA's don't call bioerror() to set an error.
6074 		 * And geterror() returns zero if B_ERROR is not set.
6075 		 * So if we get zero we must check b_error.
6076 		 */
6077 		if (status == 0 && bp->b_error != 0) {
6078 			status = bp->b_error;
6079 			bioerror(bp, status);
6080 		}
6081 
6082 		/*
6083 		 * Some HBA's convert DDI_DMA_NORESOURCES into ENOMEM.
6084 		 * In tape ENOMEM has special meaning so we'll change it.
6085 		 */
6086 		if (status == ENOMEM) {
6087 			status = 0;
6088 			bioerror(bp, status);
6089 		}
6090 
6091 		/*
6092 		 * Did it fail and is it retryable?
6093 		 * If so return and wait for the callback through st_runout.
6094 		 * Also looks like scsi_init_pkt() will setup a callback even
6095 		 * if it isn't retryable.
6096 		 */
6097 		if (BP_PKT(bp) == NULL) {
6098 			if (status == 0) {
6099 				/*
6100 				 * If first attempt save state.
6101 				 */
6102 				if (un->un_state != ST_STATE_RESOURCE_WAIT) {
6103 					un->un_laststate = un->un_state;
6104 					un->un_state = ST_STATE_RESOURCE_WAIT;
6105 				}
6106 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
6107 				    "temp no resources for pkt\n");
6108 			} else {
6109 				/*
6110 				 * Unlikely that it would be retryable then not.
6111 				 */
6112 				if (un->un_state == ST_STATE_RESOURCE_WAIT) {
6113 					un->un_state = un->un_laststate;
6114 				}
6115 				scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
6116 				    "perm no resources for pkt errno = 0x%x\n",
6117 				    status);
6118 			}
6119 			return;
6120 		}
6121 		/*
6122 		 * Worked this time set the state back.
6123 		 */
6124 		if (un->un_state == ST_STATE_RESOURCE_WAIT) {
6125 			un->un_state = un->un_laststate;
6126 		}
6127 	}
6128 
6129 	/*
6130 	 * move from waitq to runq
6131 	 */
6132 	un->un_quef = bp->b_actf;
6133 	if (un->un_quel == bp) {
6134 		/*
6135 		 *  For the case of queue having one
6136 		 *  element, set the tail pointer to
6137 		 *  point to the element.
6138 		 */
6139 		un->un_quel = bp->b_actf;
6140 	}
6141 
6142 	bp->b_actf = NULL;
6143 
6144 	if (un->un_runqf) {
6145 		un->un_runql->b_actf = bp;
6146 	} else {
6147 		un->un_runqf = bp;
6148 	}
6149 	un->un_runql = bp;
6150 
6151 
6152 #ifdef STDEBUG
6153 	start_dump(un, bp);
6154 #endif
6155 
6156 	/* could not get here if throttle was zero */
6157 	un->un_last_throttle = un->un_throttle;
6158 	un->un_throttle = 0;	/* so nothing else will come in here */
6159 	un->un_ncmds++;
6160 
6161 	ST_DO_KSTATS(bp, kstat_waitq_to_runq);
6162 
6163 	mutex_exit(ST_MUTEX);
6164 
6165 	status = scsi_transport(BP_PKT(bp));
6166 
6167 	mutex_enter(ST_MUTEX);
6168 
6169 	if (un->un_last_throttle) {
6170 		un->un_throttle = un->un_last_throttle;
6171 	}
6172 
6173 	if (status != TRAN_ACCEPT) {
6174 		ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
6175 		mutex_exit(ST_MUTEX);
6176 
6177 		if (status == TRAN_BUSY) {
6178 			/* if too many retries, fail the transport */
6179 			if (st_handle_start_busy(un, bp,
6180 			    ST_TRAN_BUSY_TIMEOUT) == 0)
6181 				goto done;
6182 		}
6183 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
6184 		    "transport rejected\n");
6185 		bp->b_resid = bp->b_bcount;
6186 
6187 
6188 #ifndef __lock_lint
6189 		/*
6190 		 * warlock doesn't understand this potential
6191 		 * recursion?
6192 		 */
6193 		mutex_enter(ST_MUTEX);
6194 		ST_DO_KSTATS(bp, kstat_waitq_exit);
6195 		ST_DO_ERRSTATS(un, st_transerrs);
6196 		st_bioerror(bp, EIO);
6197 		SET_PE_FLAG(un);
6198 		st_done_and_mutex_exit(un, bp);
6199 #endif
6200 	} else {
6201 		un->un_tran_retry_ct = 0;
6202 		mutex_exit(ST_MUTEX);
6203 	}
6204 
6205 done:
6206 
6207 	mutex_enter(ST_MUTEX);
6208 }
6209 
6210 /*
6211  * if the transport is busy, then put this bp back on the waitq
6212  */
6213 static int
6214 st_handle_start_busy(struct scsi_tape *un, struct buf *bp,
6215     clock_t timeout_interval)
6216 {
6217 	struct buf *last_quef, *runq_bp;
6218 	int rval = 0;
6219 
6220 	mutex_enter(ST_MUTEX);
6221 
6222 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
6223 	    "st_handle_start_busy()\n");
6224 
6225 	/*
6226 	 * Check to see if we hit the retry timeout and one last check for
6227 	 * making sure this is the last on the runq, if it is not, we have
6228 	 * to fail
6229 	 */
6230 	if (((int)un->un_tran_retry_ct++ > st_retry_count) ||
6231 	    (un->un_runql != bp)) {
6232 		rval = -1;
6233 		goto exit;
6234 	}
6235 
6236 	/* put the bp back on the waitq */
6237 	if (un->un_quef) {
6238 		last_quef = un->un_quef;
6239 		un->un_quef = bp;
6240 		bp->b_actf = last_quef;
6241 	} else  {
6242 		bp->b_actf = NULL;
6243 		un->un_quef = bp;
6244 		un->un_quel = bp;
6245 	}
6246 
6247 	/*
6248 	 * Decrement un_ncmds so that this
6249 	 * gets thru' st_start() again.
6250 	 */
6251 	un->un_ncmds--;
6252 
6253 	/*
6254 	 * since this is an error case, we won't have to do
6255 	 * this list walking much.  We've already made sure this bp was the
6256 	 * last on the runq
6257 	 */
6258 	runq_bp = un->un_runqf;
6259 
6260 	if (un->un_runqf == bp) {
6261 		un->un_runqf = NULL;
6262 		un->un_runql = NULL;
6263 	} else {
6264 		while (runq_bp) {
6265 			if (runq_bp->b_actf == bp) {
6266 				runq_bp->b_actf = NULL;
6267 				un->un_runql = runq_bp;
6268 				break;
6269 			}
6270 			runq_bp = runq_bp->b_actf;
6271 		}
6272 	}
6273 
6274 
6275 	/*
6276 	 * send a marker pkt, if appropriate
6277 	 */
6278 	st_hba_unflush(un);
6279 
6280 	/*
6281 	 * all queues are aligned, we are just waiting to
6282 	 * transport, don't alloc any more buf p's, when
6283 	 * st_start is reentered.
6284 	 */
6285 	(void) timeout(st_start_restart, un, timeout_interval);
6286 
6287 exit:
6288 	mutex_exit(ST_MUTEX);
6289 	return (rval);
6290 }
6291 
6292 
6293 /*
6294  * st_runout a callback that is called what a resource allocatation failed
6295  */
6296 static int
6297 st_runout(caddr_t arg)
6298 {
6299 	struct scsi_tape *un = (struct scsi_tape *)arg;
6300 	struct buf *bp;
6301 	ASSERT(un != NULL);
6302 
6303 	mutex_enter(ST_MUTEX);
6304 
6305 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_runout()\n");
6306 
6307 	bp = un->un_quef;
6308 
6309 	/*
6310 	 * failed scsi_init_pkt(). If errno is zero its retryable.
6311 	 */
6312 	if ((bp != NULL) && (geterror(bp) != 0)) {
6313 
6314 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
6315 		    "errors after pkt alloc (b_flags=0x%x, b_error=0x%x)\n",
6316 		    bp->b_flags, geterror(bp));
6317 		ASSERT((bp->b_flags & B_DONE) == 0);
6318 
6319 		un->un_quef = bp->b_actf;
6320 		if (un->un_quel == bp) {
6321 			/*
6322 			 *  For the case of queue having one
6323 			 *  element, set the tail pointer to
6324 			 *  point to the element.
6325 			 */
6326 			un->un_quel = bp->b_actf;
6327 		}
6328 		mutex_exit(ST_MUTEX);
6329 		bp->b_actf = NULL;
6330 
6331 		ASSERT((bp->b_flags & B_DONE) == 0);
6332 
6333 		/*
6334 		 * Set resid, Error already set, then unblock calling thread.
6335 		 */
6336 		bp->b_resid = bp->b_bcount;
6337 		biodone(bp);
6338 	} else {
6339 		/*
6340 		 * Try Again
6341 		 */
6342 		st_start(un);
6343 		mutex_exit(ST_MUTEX);
6344 	}
6345 
6346 	/*
6347 	 * Comments courtesy of sd.c
6348 	 * The scsi_init_pkt routine allows for the callback function to
6349 	 * return a 0 indicating the callback should be rescheduled or a 1
6350 	 * indicating not to reschedule. This routine always returns 1
6351 	 * because the driver always provides a callback function to
6352 	 * scsi_init_pkt. This results in a callback always being scheduled
6353 	 * (via the scsi_init_pkt callback implementation) if a resource
6354 	 * failure occurs.
6355 	 */
6356 
6357 	return (1);
6358 }
6359 
6360 /*
6361  * st_done_and_mutex_exit()
6362  *	- remove bp from runq
6363  *	- start up the next request
6364  *	- if this was an asynch bp, clean up
6365  *	- exit with released mutex
6366  */
6367 static void
6368 st_done_and_mutex_exit(struct scsi_tape *un, struct buf *bp)
6369 {
6370 	struct buf *runqbp, *prevbp;
6371 	int	pe_flagged = 0;
6372 
6373 	ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex));
6374 #if !defined(lint)
6375 	_NOTE(LOCK_RELEASED_AS_SIDE_EFFECT(&un->un_sd->sd_mutex))
6376 #endif
6377 	ASSERT(mutex_owned(ST_MUTEX));
6378 
6379 	/*
6380 	 * if bp is still on the runq (anywhere), then remove it
6381 	 */
6382 	prevbp = NULL;
6383 	for (runqbp = un->un_runqf; runqbp != 0; runqbp = runqbp->b_actf) {
6384 		if (runqbp == bp) {
6385 			if (runqbp == un->un_runqf) {
6386 				un->un_runqf = bp->b_actf;
6387 			} else {
6388 				prevbp->b_actf = bp->b_actf;
6389 			}
6390 			if (un->un_runql == bp) {
6391 				un->un_runql = prevbp;
6392 			}
6393 			break;
6394 		}
6395 		prevbp = runqbp;
6396 	}
6397 	bp->b_actf = NULL;
6398 
6399 	un->un_ncmds--;
6400 	cv_signal(&un->un_queue_cv);
6401 
6402 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
6403 	"st_done_and_mutex_exit(): cmd=0x%x count=%ld resid=%ld  flags=0x%x\n",
6404 		(uchar_t)*((caddr_t)(BP_PKT(bp))->pkt_cdbp),
6405 		bp->b_bcount, bp->b_resid, bp->b_flags);
6406 
6407 
6408 	/*
6409 	 * update kstats with transfer count info
6410 	 */
6411 	if (un->un_stats && (bp != un->un_sbufp) && IS_RW(bp)) {
6412 		uint32_t n_done =  bp->b_bcount - bp->b_resid;
6413 		if (bp->b_flags & B_READ) {
6414 			IOSP->reads++;
6415 			IOSP->nread += n_done;
6416 		} else {
6417 			IOSP->writes++;
6418 			IOSP->nwritten += n_done;
6419 		}
6420 	}
6421 
6422 	/*
6423 	 * Start the next one before releasing resources on this one, if
6424 	 * there is something on the queue and persistent errors has not been
6425 	 * flagged
6426 	 */
6427 
6428 	if ((pe_flagged = IS_PE_FLAG_SET(un)) != 0) {
6429 		un->un_last_resid = bp->b_resid;
6430 		un->un_last_count = bp->b_bcount;
6431 	}
6432 
6433 	if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) {
6434 		cv_broadcast(&un->un_tape_busy_cv);
6435 	} else if (un->un_quef && un->un_throttle && !pe_flagged) {
6436 		st_start(un);
6437 	}
6438 
6439 	if (bp == un->un_sbufp && (bp->b_flags & B_ASYNC)) {
6440 		/*
6441 		 * Since we marked this ourselves as ASYNC,
6442 		 * there isn't anybody around waiting for
6443 		 * completion any more.
6444 		 */
6445 		uchar_t com = (uchar_t)(uintptr_t)bp->b_forw;
6446 		if (com == SCMD_READ || com == SCMD_WRITE) {
6447 			bp->b_un.b_addr = (caddr_t)0;
6448 		}
6449 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
6450 		    "st_done_and_mutex_exit(async): freeing pkt\n");
6451 		scsi_destroy_pkt(BP_PKT(bp));
6452 		un->un_sbuf_busy = 0;
6453 		cv_signal(&un->un_sbuf_cv);
6454 		mutex_exit(ST_MUTEX);
6455 		return;
6456 	}
6457 
6458 	if (bp == un->un_sbufp && BP_UCMD(bp)) {
6459 		/*
6460 		 * Copy status from scsi_pkt to uscsi_cmd
6461 		 * since st_ioctl_cmd needs it
6462 		 */
6463 		BP_UCMD(bp)->uscsi_status = SCBP_C(BP_PKT(bp));
6464 	}
6465 
6466 
6467 #ifdef STDEBUG
6468 	if ((st_debug >= 4) &&
6469 	    (((un->un_blkno % 100) == 0) || IS_PE_FLAG_SET(un))) {
6470 
6471 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
6472 		    "st_d_a_m_exit(): ncmds = %d, thr = %d, "
6473 		    "un_errno = %d, un_pe = %d\n",
6474 		    un->un_ncmds, un->un_throttle, un->un_errno,
6475 		    un->un_persist_errors);
6476 	}
6477 
6478 #endif
6479 
6480 	mutex_exit(ST_MUTEX);
6481 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
6482 	    "st_done_and_mutex_exit: freeing pkt\n");
6483 
6484 	scsi_destroy_pkt(BP_PKT(bp));
6485 
6486 	biodone(bp);
6487 
6488 	/*
6489 	 * now that we biodoned that command, if persistent errors have been
6490 	 * flagged, flush the waitq
6491 	 */
6492 	if (pe_flagged)
6493 		st_flush(un);
6494 }
6495 
6496 
6497 /*
6498  * Tape error, flush tape driver queue.
6499  */
6500 static void
6501 st_flush(struct scsi_tape *un)
6502 {
6503 	struct buf *bp;
6504 
6505 	mutex_enter(ST_MUTEX);
6506 
6507 	ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
6508 	    "st_flush(), ncmds = %d, quef = 0x%p\n",
6509 	    un->un_ncmds, (void *)un->un_quef);
6510 
6511 	/*
6512 	 * if we still have commands outstanding, wait for them to come in
6513 	 * before flushing the queue, and make sure there is a queue
6514 	 */
6515 	if (un->un_ncmds || !un->un_quef)
6516 		goto exit;
6517 
6518 	/*
6519 	 * we have no more commands outstanding, so let's deal with special
6520 	 * cases in the queue for EOM and FM. If we are here, and un_errno
6521 	 * is 0, then we know there was no error and we return a 0 read or
6522 	 * write before showing errors
6523 	 */
6524 
6525 	/* Flush the wait queue. */
6526 	while ((bp = un->un_quef) != NULL) {
6527 		un->un_quef = bp->b_actf;
6528 
6529 		bp->b_resid = bp->b_bcount;
6530 
6531 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
6532 		    "st_flush() : blkno=%ld, err=%d, b_bcount=%ld\n",
6533 		    un->un_blkno, un->un_errno, bp->b_bcount);
6534 
6535 		st_set_pe_errno(un);
6536 
6537 		bioerror(bp, un->un_errno);
6538 
6539 		mutex_exit(ST_MUTEX);
6540 		/* it should have one, but check anyway */
6541 		if (BP_PKT(bp)) {
6542 			scsi_destroy_pkt(BP_PKT(bp));
6543 		}
6544 		biodone(bp);
6545 		mutex_enter(ST_MUTEX);
6546 	}
6547 
6548 	/*
6549 	 * It's not a bad practice to reset the
6550 	 * waitq tail pointer to NULL.
6551 	 */
6552 	un->un_quel = NULL;
6553 
6554 exit:
6555 	/* we mucked with the queue, so let others know about it */
6556 	cv_signal(&un->un_queue_cv);
6557 	mutex_exit(ST_MUTEX);
6558 }
6559 
6560 
6561 /*
6562  * Utility functions
6563  */
6564 static int
6565 st_determine_generic(dev_t dev)
6566 {
6567 	int bsize;
6568 	static char *cart = "0.25 inch cartridge";
6569 	char *sizestr;
6570 
6571 	GET_SOFT_STATE(dev);
6572 
6573 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
6574 	    "st_determine_generic(dev = 0x%lx)\n", dev);
6575 
6576 	ASSERT(mutex_owned(ST_MUTEX));
6577 
6578 	if (st_modesense(un)) {
6579 		return (-1);
6580 	}
6581 
6582 	bsize = (un->un_mspl->high_bl << 16)	|
6583 		(un->un_mspl->mid_bl << 8)	|
6584 		(un->un_mspl->low_bl);
6585 
6586 	if (bsize == 0) {
6587 		un->un_dp->options |= ST_VARIABLE;
6588 		un->un_dp->bsize = 0;
6589 		un->un_bsize = 0;
6590 	} else if (bsize > ST_MAXRECSIZE_FIXED) {
6591 		/*
6592 		 * record size of this device too big.
6593 		 * try and convert it to variable record length.
6594 		 *
6595 		 */
6596 		un->un_dp->options |= ST_VARIABLE;
6597 		if (st_change_block_size(dev, 0) != 0) {
6598 			ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
6599 			    "Fixed Record Size %d is too large\n", bsize);
6600 			ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
6601 			    "Cannot switch to variable record size\n");
6602 			un->un_dp->options &= ~ST_VARIABLE;
6603 			return (-1);
6604 		}
6605 	} else if (st_change_block_size(dev, 0) == 0) {
6606 		/*
6607 		 * If the drive was set to a non zero block size,
6608 		 * See if it can be set to a zero block size.
6609 		 * If it works, ST_VARIABLE so user can set it as they want.
6610 		 */
6611 		un->un_dp->options |= ST_VARIABLE;
6612 		un->un_dp->bsize = 0;
6613 		un->un_bsize = 0;
6614 	} else {
6615 		un->un_dp->bsize = bsize;
6616 		un->un_bsize = bsize;
6617 	}
6618 
6619 
6620 	switch (un->un_mspl->density) {
6621 	default:
6622 	case 0x0:
6623 		/*
6624 		 * default density, cannot determine any other
6625 		 * information.
6626 		 */
6627 		sizestr = "Unknown type- assuming 0.25 inch cartridge";
6628 		un->un_dp->type = ST_TYPE_DEFAULT;
6629 		un->un_dp->options |= (ST_AUTODEN_OVERRIDE|ST_QIC);
6630 		break;
6631 	case 0x1:
6632 	case 0x2:
6633 	case 0x3:
6634 	case 0x6:
6635 		/*
6636 		 * 1/2" reel
6637 		 */
6638 		sizestr = "0.50 inch reel";
6639 		un->un_dp->type = ST_TYPE_REEL;
6640 		un->un_dp->options |= ST_REEL;
6641 		un->un_dp->densities[0] = 0x1;
6642 		un->un_dp->densities[1] = 0x2;
6643 		un->un_dp->densities[2] = 0x6;
6644 		un->un_dp->densities[3] = 0x3;
6645 		break;
6646 	case 0x4:
6647 	case 0x5:
6648 	case 0x7:
6649 	case 0x0b:
6650 
6651 		/*
6652 		 * Quarter inch.
6653 		 */
6654 		sizestr = cart;
6655 		un->un_dp->type = ST_TYPE_DEFAULT;
6656 		un->un_dp->options |= ST_QIC;
6657 
6658 		un->un_dp->densities[1] = 0x4;
6659 		un->un_dp->densities[2] = 0x5;
6660 		un->un_dp->densities[3] = 0x7;
6661 		un->un_dp->densities[0] = 0x0b;
6662 		break;
6663 
6664 	case 0x0f:
6665 	case 0x10:
6666 	case 0x11:
6667 	case 0x12:
6668 		/*
6669 		 * QIC-120, QIC-150, QIC-320, QIC-600
6670 		 */
6671 		sizestr = cart;
6672 		un->un_dp->type = ST_TYPE_DEFAULT;
6673 		un->un_dp->options |= ST_QIC;
6674 		un->un_dp->densities[0] = 0x0f;
6675 		un->un_dp->densities[1] = 0x10;
6676 		un->un_dp->densities[2] = 0x11;
6677 		un->un_dp->densities[3] = 0x12;
6678 		break;
6679 
6680 	case 0x09:
6681 	case 0x0a:
6682 	case 0x0c:
6683 	case 0x0d:
6684 		/*
6685 		 * 1/2" cartridge tapes. Include HI-TC.
6686 		 */
6687 		sizestr = cart;
6688 		sizestr[2] = '5';
6689 		sizestr[3] = '0';
6690 		un->un_dp->type = ST_TYPE_HIC;
6691 		un->un_dp->densities[0] = 0x09;
6692 		un->un_dp->densities[1] = 0x0a;
6693 		un->un_dp->densities[2] = 0x0c;
6694 		un->un_dp->densities[3] = 0x0d;
6695 		break;
6696 
6697 	case 0x13:
6698 			/* DDS-2/DDS-3 scsi spec densities */
6699 	case 0x24:
6700 	case 0x25:
6701 	case 0x26:
6702 		sizestr = "DAT Data Storage (DDS)";
6703 		un->un_dp->type = ST_TYPE_DAT;
6704 		un->un_dp->options |= ST_AUTODEN_OVERRIDE;
6705 		break;
6706 
6707 	case 0x14:
6708 		/*
6709 		 * Helical Scan (Exabyte) devices
6710 		 */
6711 		sizestr = "8mm helical scan cartridge";
6712 		un->un_dp->type = ST_TYPE_EXABYTE;
6713 		un->un_dp->options |= ST_AUTODEN_OVERRIDE;
6714 		break;
6715 	}
6716 
6717 	/*
6718 	 * Assume LONG ERASE, BSF and BSR
6719 	 */
6720 
6721 	un->un_dp->options |= (ST_LONG_ERASE|ST_UNLOADABLE|ST_BSF|
6722 				ST_BSR|ST_KNOWS_EOD);
6723 
6724 	/*
6725 	 * Only if mode sense data says no buffered write, set NOBUF
6726 	 */
6727 	if (un->un_mspl->bufm == 0)
6728 		un->un_dp->options |= ST_NOBUF;
6729 
6730 	/*
6731 	 * set up large read and write retry counts
6732 	 */
6733 
6734 	un->un_dp->max_rretries = un->un_dp->max_wretries = 1000;
6735 
6736 	/*
6737 	 * If this is a 0.50 inch reel tape, and
6738 	 * it is *not* variable mode, try and
6739 	 * set it to variable record length
6740 	 * mode.
6741 	 */
6742 	if ((un->un_dp->options & ST_REEL) && un->un_bsize != 0 &&
6743 	    (un->un_dp->options & ST_VARIABLE)) {
6744 		if (st_change_block_size(dev, 0) == 0) {
6745 			un->un_dp->bsize = 0;
6746 			un->un_mspl->high_bl = un->un_mspl->mid_bl =
6747 			    un->un_mspl->low_bl = 0;
6748 		}
6749 	}
6750 
6751 	/*
6752 	 * Write to console about type of device found
6753 	 */
6754 	ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
6755 	    "Generic Drive, Vendor=%s\n\t%s", un->un_dp->name,
6756 	    sizestr);
6757 	if (un->un_dp->options & ST_VARIABLE) {
6758 		scsi_log(ST_DEVINFO, st_label, CE_NOTE,
6759 		    "!Variable record length I/O\n");
6760 	} else {
6761 		scsi_log(ST_DEVINFO, st_label, CE_NOTE,
6762 		    "!Fixed record length (%d byte blocks) I/O\n",
6763 		    un->un_dp->bsize);
6764 	}
6765 	ASSERT(mutex_owned(ST_MUTEX));
6766 	return (0);
6767 }
6768 
6769 static int
6770 st_determine_density(dev_t dev, int rw)
6771 {
6772 	int rval = 0;
6773 
6774 	GET_SOFT_STATE(dev);
6775 
6776 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
6777 	    "st_determine_density(dev = 0x%lx, rw = %s)\n",
6778 	    dev, (rw == B_WRITE ? wr_str: rd_str));
6779 
6780 	ASSERT(mutex_owned(ST_MUTEX));
6781 
6782 	/*
6783 	 * If we're past BOT, density is determined already.
6784 	 */
6785 	if (un->un_fileno > 0 || (un->un_fileno == 0 && un->un_blkno != 0)) {
6786 		/*
6787 		 * XXX: put in a bitch message about attempting to
6788 		 * XXX: change density past BOT.
6789 		 */
6790 		goto exit;
6791 	}
6792 
6793 	/*
6794 	 * If we're going to be writing, we set the density
6795 	 */
6796 	if (rw == 0 || rw == B_WRITE) {
6797 		/* un_curdens is used as an index into densities table */
6798 		un->un_curdens = MT_DENSITY(un->un_dev);
6799 		if (st_set_density(dev)) {
6800 			rval = -1;
6801 		}
6802 		goto exit;
6803 	}
6804 
6805 	/*
6806 	 * If density is known already,
6807 	 * we don't have to get it again.(?)
6808 	 */
6809 	if (!un->un_density_known) {
6810 		if (st_get_density(dev)) {
6811 			rval = -1;
6812 		}
6813 	}
6814 
6815 exit:
6816 	ASSERT(mutex_owned(ST_MUTEX));
6817 	return (rval);
6818 }
6819 
6820 
6821 /*
6822  * Try to determine density. We do this by attempting to read the
6823  * first record off the tape, cycling through the available density
6824  * codes as we go.
6825  */
6826 
6827 static int
6828 st_get_density(dev_t dev)
6829 {
6830 	int succes = 0, rval = -1, i;
6831 	uint_t size;
6832 	uchar_t dens, olddens;
6833 
6834 	GET_SOFT_STATE(dev);
6835 
6836 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
6837 	    "st_get_density(dev = 0x%lx)\n", dev);
6838 
6839 	ASSERT(mutex_owned(ST_MUTEX));
6840 
6841 	/*
6842 	 * If Auto Density override is enabled The drive has
6843 	 * only one density and there is no point in attempting
6844 	 * find the correct one.
6845 	 *
6846 	 * Since most modern drives auto detect the density
6847 	 * and format of the recorded media before they come
6848 	 * ready. What this function does is a legacy behavior
6849 	 * and modern drives not only don't need it, The backup
6850 	 * utilities that do positioning via uscsi find the un-
6851 	 * expected rewinds problematic.
6852 	 *
6853 	 * The drives that need this are old reel to reel devices.
6854 	 * I took a swag and said they must be scsi-1 or older.
6855 	 * I don't beleave there will any of the newer devices
6856 	 * that need this. There will be some scsi-1 devices that
6857 	 * don't need this but I don't think they will be using the
6858 	 * BIG aftermarket backup and restore utilitys.
6859 	 */
6860 	if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) ||
6861 	    (un->un_sd->sd_inq->inq_ansi > 1)) {
6862 		un->un_density_known = 1;
6863 		rval = 0;
6864 		goto exit;
6865 	}
6866 
6867 	/*
6868 	 * This will only work on variable record length tapes
6869 	 * if and only if all variable record length tapes autodensity
6870 	 * select.
6871 	 */
6872 	size = (unsigned)(un->un_dp->bsize ? un->un_dp->bsize : SECSIZE);
6873 	un->un_tmpbuf = kmem_alloc(size, KM_SLEEP);
6874 
6875 	/*
6876 	 * Start at the specified density
6877 	 */
6878 
6879 	dens = olddens = un->un_curdens = MT_DENSITY(un->un_dev);
6880 
6881 	for (i = 0; i < NDENSITIES; i++, ((un->un_curdens == NDENSITIES - 1) ?
6882 					(un->un_curdens = 0) :
6883 					(un->un_curdens += 1))) {
6884 		/*
6885 		 * If we've done this density before,
6886 		 * don't bother to do it again.
6887 		 */
6888 		dens = un->un_dp->densities[un->un_curdens];
6889 		if (i > 0 && dens == olddens)
6890 			continue;
6891 		olddens = dens;
6892 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
6893 		    "trying density 0x%x\n", dens);
6894 		if (st_set_density(dev)) {
6895 			continue;
6896 		}
6897 
6898 		/*
6899 		 * XXX - the creates lots of headaches and slowdowns - must
6900 		 * fix.
6901 		 */
6902 		succes = (st_cmd(dev, SCMD_READ, (int)size, SYNC_CMD) == 0);
6903 		if (st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD)) {
6904 			break;
6905 		}
6906 		if (succes) {
6907 			st_init(un);
6908 			rval = 0;
6909 			un->un_density_known = 1;
6910 			break;
6911 		}
6912 	}
6913 	kmem_free(un->un_tmpbuf, size);
6914 	un->un_tmpbuf = 0;
6915 
6916 exit:
6917 	ASSERT(mutex_owned(ST_MUTEX));
6918 	return (rval);
6919 }
6920 
6921 static int
6922 st_set_density(dev_t dev)
6923 {
6924 	int rval = 0;
6925 
6926 	GET_SOFT_STATE(dev);
6927 
6928 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
6929 	    "st_set_density(dev = 0x%lx): density = 0x%x\n", dev,
6930 	    un->un_dp->densities[un->un_curdens]);
6931 
6932 	ASSERT(mutex_owned(ST_MUTEX));
6933 
6934 	un->un_mspl->density = un->un_dp->densities[un->un_curdens];
6935 
6936 	if ((un->un_dp->options & ST_AUTODEN_OVERRIDE) == 0) {
6937 		/*
6938 		 * If auto density override is not set, Use mode select
6939 		 * to set density and compression.
6940 		 */
6941 		if (st_modeselect(un)) {
6942 			rval = -1;
6943 		}
6944 	} else if ((un->un_dp->options & ST_MODE_SEL_COMP) != 0) {
6945 		/*
6946 		 * If auto density and mode select compression are set,
6947 		 * This is a drive with one density code but compression
6948 		 * can be enabled or disabled.
6949 		 * Set compression but no need to set density.
6950 		 */
6951 		rval = st_set_compression(un);
6952 		if ((rval != 0) && (rval != EALREADY)) {
6953 			rval = -1;
6954 		} else {
6955 			rval = 0;
6956 		}
6957 	}
6958 
6959 	/* If sucessful set density and/or compression, mark density known */
6960 	if (rval == 0) {
6961 		un->un_density_known = 1;
6962 	}
6963 
6964 	ASSERT(mutex_owned(ST_MUTEX));
6965 	return (rval);
6966 }
6967 
6968 static int
6969 st_loadtape(dev_t dev)
6970 {
6971 	int rval;
6972 
6973 	GET_SOFT_STATE(dev);
6974 
6975 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
6976 	    "st_loadtape(dev = 0x%lx)\n", dev);
6977 
6978 	ASSERT(mutex_owned(ST_MUTEX));
6979 
6980 	/*
6981 	 * 'LOAD' the tape to BOT by rewinding
6982 	 */
6983 	rval = st_cmd(dev, SCMD_REWIND, 1, SYNC_CMD);
6984 	if (rval == 0) {
6985 		st_init(un);
6986 		un->un_density_known = 0;
6987 	}
6988 
6989 	ASSERT(mutex_owned(ST_MUTEX));
6990 	return (rval);
6991 }
6992 
6993 
6994 /*
6995  * Note: QIC devices aren't so smart.  If you try to append
6996  * after EOM, the write can fail because the device doesn't know
6997  * it's at EOM.	 In that case, issue a read.  The read should fail
6998  * because there's no data, but the device knows it's at EOM,
6999  * so a subsequent write should succeed.  To further confuse matters,
7000  * the target returns the same error if the tape is positioned
7001  * such that a write would overwrite existing data.  That's why
7002  * we have to do the append test.  A read in the middle of
7003  * recorded data would succeed, thus indicating we're attempting
7004  * something illegal.
7005  */
7006 
7007 void bp_mapin(struct buf *bp);
7008 
7009 static void
7010 st_test_append(struct buf *bp)
7011 {
7012 	dev_t dev = bp->b_edev;
7013 	struct scsi_tape *un;
7014 	uchar_t status;
7015 	unsigned bcount;
7016 
7017 	un = ddi_get_soft_state(st_state, MTUNIT(dev));
7018 
7019 	ASSERT(mutex_owned(ST_MUTEX));
7020 
7021 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
7022 	    "st_test_append(): fileno %d\n", un->un_fileno);
7023 
7024 	un->un_laststate = un->un_state;
7025 	un->un_state = ST_STATE_APPEND_TESTING;
7026 	un->un_test_append = 0;
7027 
7028 	/*
7029 	 * first, map in the buffer, because we're doing a double write --
7030 	 * first into the kernel, then onto the tape.
7031 	 */
7032 	bp_mapin(bp);
7033 
7034 	/*
7035 	 * get a copy of the data....
7036 	 */
7037 	un->un_tmpbuf = kmem_alloc((unsigned)bp->b_bcount, KM_SLEEP);
7038 	bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount);
7039 
7040 	/*
7041 	 * attempt the write..
7042 	 */
7043 
7044 	if (st_cmd(dev, (int)SCMD_WRITE, (int)bp->b_bcount, SYNC_CMD) == 0) {
7045 success:
7046 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7047 		    "append write succeeded\n");
7048 		bp->b_resid = un->un_sbufp->b_resid;
7049 		mutex_exit(ST_MUTEX);
7050 		bcount = (unsigned)bp->b_bcount;
7051 		biodone(bp);
7052 		mutex_enter(ST_MUTEX);
7053 		un->un_laststate = un->un_state;
7054 		un->un_state = ST_STATE_OPEN;
7055 		kmem_free(un->un_tmpbuf, bcount);
7056 		un->un_tmpbuf = NULL;
7057 		return;
7058 	}
7059 
7060 	/*
7061 	 * The append failed. Do a short read. If that fails,  we are at EOM
7062 	 * so we can retry the write command. If that succeeds, than we're
7063 	 * all screwed up (the controller reported a real error).
7064 	 *
7065 	 * XXX: should the dummy read be > SECSIZE? should it be the device's
7066 	 * XXX: block size?
7067 	 *
7068 	 */
7069 	status = un->un_status;
7070 	un->un_status = 0;
7071 	(void) st_cmd(dev, SCMD_READ, SECSIZE, SYNC_CMD);
7072 	if (un->un_status == KEY_BLANK_CHECK) {
7073 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7074 		    "append at EOM\n");
7075 		/*
7076 		 * Okay- the read failed. We should actually have confused
7077 		 * the controller enough to allow writing. In any case, the
7078 		 * i/o is on its own from here on out.
7079 		 */
7080 		un->un_laststate = un->un_state;
7081 		un->un_state = ST_STATE_OPEN;
7082 		bcopy(bp->b_un.b_addr, un->un_tmpbuf, (uint_t)bp->b_bcount);
7083 		if (st_cmd(dev, (int)SCMD_WRITE, (int)bp->b_bcount,
7084 		    SYNC_CMD) == 0) {
7085 			goto success;
7086 		}
7087 	}
7088 
7089 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7090 	    "append write failed- not at EOM\n");
7091 	bp->b_resid = bp->b_bcount;
7092 	st_bioerror(bp, EIO);
7093 
7094 	ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
7095 	    "st_test_append : EIO : append write failed - not at EOM");
7096 
7097 	/*
7098 	 * backspace one record to get back to where we were
7099 	 */
7100 	if (st_cmd(dev, SCMD_SPACE, Blk(-1), SYNC_CMD)) {
7101 		un->un_fileno = -1;
7102 	}
7103 
7104 	un->un_err_resid = bp->b_resid;
7105 	un->un_status = status;
7106 
7107 	/*
7108 	 * Note: biodone will do a bp_mapout()
7109 	 */
7110 	mutex_exit(ST_MUTEX);
7111 	bcount = (unsigned)bp->b_bcount;
7112 	biodone(bp);
7113 	mutex_enter(ST_MUTEX);
7114 	un->un_laststate = un->un_state;
7115 	un->un_state = ST_STATE_OPEN_PENDING_IO;
7116 	kmem_free(un->un_tmpbuf, bcount);
7117 	un->un_tmpbuf = NULL;
7118 }
7119 
7120 /*
7121  * Special command handler
7122  */
7123 
7124 /*
7125  * common st_cmd code. The fourth parameter states
7126  * whether the caller wishes to await the results
7127  * Note the release of the mutex during most of the function
7128  */
7129 static int
7130 st_cmd(dev_t dev, int com, int count, int wait)
7131 {
7132 	struct buf *bp;
7133 	int err;
7134 
7135 	GET_SOFT_STATE(dev);
7136 
7137 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
7138 	    "st_cmd(dev = 0x%lx, com = 0x%x, count = %x, wait = %d)\n",
7139 	    dev, com, count, wait);
7140 
7141 	ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex));
7142 	ASSERT(mutex_owned(ST_MUTEX));
7143 
7144 #ifdef STDEBUG
7145 	if (st_debug)
7146 		st_debug_cmds(un, com, count, wait);
7147 #endif
7148 
7149 	/* check to see if this command requires the drive to be reserved */
7150 	err = st_check_cmd_for_need_to_reserve(un, com, count);
7151 
7152 	if (err) {
7153 		return (err);
7154 	}
7155 
7156 	while (un->un_sbuf_busy)
7157 		cv_wait(&un->un_sbuf_cv, ST_MUTEX);
7158 	un->un_sbuf_busy = 1;
7159 
7160 	bp = un->un_sbufp;
7161 	bzero(bp, sizeof (buf_t));
7162 
7163 	bp->b_flags = (wait) ? B_BUSY : B_BUSY|B_ASYNC;
7164 
7165 	/*
7166 	 * Set count to the actual size of the data tranfer.
7167 	 * For commands with no data transfer, set bp->b_bcount
7168 	 * to the value to be used when constructing the
7169 	 * cdb in st_make_cmd().
7170 	 */
7171 	switch (com) {
7172 	case SCMD_READ:
7173 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7174 		    "special read %d\n", count);
7175 		bp->b_flags |= B_READ;
7176 		bp->b_un.b_addr = un->un_tmpbuf;
7177 		break;
7178 
7179 	case SCMD_WRITE:
7180 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7181 		    "special write %d\n", count);
7182 		bp->b_un.b_addr = un->un_tmpbuf;
7183 		break;
7184 
7185 	case SCMD_WRITE_FILE_MARK:
7186 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7187 		    "write %d file marks\n", count);
7188 		bp->b_bcount = count;
7189 		count = 0;
7190 		break;
7191 
7192 	case SCMD_REWIND:
7193 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "rewind\n");
7194 		bp->b_bcount = 0;
7195 		count = 0;
7196 		break;
7197 
7198 	case SCMD_SPACE:
7199 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "space\n");
7200 		bp->b_bcount = count;
7201 		count = 0;
7202 		break;
7203 
7204 	case SCMD_RESERVE:
7205 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "reserve");
7206 		bp->b_bcount = 0;
7207 		count = 0;
7208 		break;
7209 
7210 	case SCMD_RELEASE:
7211 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "release");
7212 		bp->b_bcount = 0;
7213 		count = 0;
7214 		break;
7215 
7216 	case SCMD_LOAD:
7217 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7218 		    "%s tape\n", (count) ? "load" : "unload");
7219 		bp->b_bcount = count;
7220 		count = 0;
7221 		break;
7222 
7223 	case SCMD_ERASE:
7224 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7225 		    "erase tape\n");
7226 		bp->b_bcount = 0;
7227 		count = 0;
7228 		break;
7229 
7230 	case SCMD_MODE_SENSE:
7231 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7232 		    "mode sense\n");
7233 		bp->b_flags |= B_READ;
7234 		bp->b_un.b_addr = (caddr_t)(un->un_mspl);
7235 		break;
7236 
7237 	case SCMD_MODE_SELECT:
7238 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7239 		    "mode select\n");
7240 		bp->b_un.b_addr = (caddr_t)(un->un_mspl);
7241 		break;
7242 
7243 	case SCMD_READ_BLKLIM:
7244 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7245 		    "read block limits\n");
7246 		bp->b_flags |= B_READ;
7247 		bp->b_un.b_addr = (caddr_t)(un->un_rbl);
7248 		break;
7249 
7250 	case SCMD_TEST_UNIT_READY:
7251 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7252 		    "test unit ready\n");
7253 		bp->b_bcount = 0;
7254 		count = 0;
7255 		break;
7256 	default:
7257 		ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC,
7258 		    "Unhandled scsi command 0x%x in st_cmd()\n", com);
7259 	}
7260 
7261 	mutex_exit(ST_MUTEX);
7262 
7263 	if (count > 0) {
7264 		/*
7265 		 * We're going to do actual I/O.
7266 		 * Set things up for physio.
7267 		 */
7268 		struct iovec aiov;
7269 		struct uio auio;
7270 		struct uio *uio = &auio;
7271 
7272 		bzero(&auio, sizeof (struct uio));
7273 		bzero(&aiov, sizeof (struct iovec));
7274 		aiov.iov_base = bp->b_un.b_addr;
7275 		aiov.iov_len = count;
7276 
7277 		uio->uio_iov = &aiov;
7278 		uio->uio_iovcnt = 1;
7279 		uio->uio_resid = aiov.iov_len;
7280 		uio->uio_segflg = UIO_SYSSPACE;
7281 
7282 		/*
7283 		 * Let physio do the rest...
7284 		 */
7285 		bp->b_forw = (struct buf *)(uintptr_t)com;
7286 		bp->b_back = NULL;
7287 		err = physio(st_strategy, bp, dev,
7288 			(bp->b_flags & B_READ) ? B_READ : B_WRITE,
7289 			st_minphys, uio);
7290 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7291 		    "st_cmd: physio returns %d\n", err);
7292 	} else {
7293 		/*
7294 		 * Mimic physio
7295 		 */
7296 		bp->b_forw = (struct buf *)(uintptr_t)com;
7297 		bp->b_back = NULL;
7298 		bp->b_edev = dev;
7299 		bp->b_dev = cmpdev(dev);
7300 		bp->b_blkno = 0;
7301 		bp->b_resid = 0;
7302 		(void) st_strategy(bp);
7303 		if (!wait) {
7304 			/*
7305 			 * This is an async command- the caller won't wait
7306 			 * and doesn't care about errors.
7307 			 */
7308 			mutex_enter(ST_MUTEX);
7309 			return (0);
7310 		}
7311 
7312 		/*
7313 		 * BugTraq #4260046
7314 		 * ----------------
7315 		 * Restore Solaris 2.5.1 behavior, namely call biowait
7316 		 * unconditionally. The old comment said...
7317 		 *
7318 		 * "if strategy was flagged with  persistent errors, we would
7319 		 *  have an error here, and the bp would never be sent, so we
7320 		 *  don't want to wait on a bp that was never sent...or hang"
7321 		 *
7322 		 * The new rationale, courtesy of Chitrank...
7323 		 *
7324 		 * "we should unconditionally biowait() here because
7325 		 *  st_strategy() will do a biodone() in the persistent error
7326 		 *  case and the following biowait() will return immediately.
7327 		 *  If not, in the case of "errors after pkt alloc" in
7328 		 *  st_start(), we will not biowait here which will cause the
7329 		 *  next biowait() to return immediately which will cause
7330 		 *  us to send out the next command. In the case where both of
7331 		 *  these use the sbuf, when the first command completes we'll
7332 		 *  free the packet attached to sbuf and the same pkt will
7333 		 *  get freed again when we complete the second command.
7334 		 *  see esc 518987.  BTW, it is necessary to do biodone() in
7335 		 *  st_start() for the pkt alloc failure case because physio()
7336 		 *  does biowait() and will hang if we don't do biodone()"
7337 		 */
7338 
7339 		err = biowait(bp);
7340 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7341 		    "st_cmd: biowait returns %d\n", err);
7342 	}
7343 	mutex_enter(ST_MUTEX);
7344 
7345 	un->un_sbuf_busy = 0;
7346 	cv_signal(&un->un_sbuf_cv);
7347 	return (err);
7348 }
7349 
7350 static int
7351 st_set_compression(struct scsi_tape *un)
7352 {
7353 	int rval;
7354 	int turn_compression_on;
7355 	minor_t minor;
7356 
7357 	/*
7358 	 * Drive either dosn't have compression or it is controlled with
7359 	 * special density codes. Return ENOTTY so caller
7360 	 * knows nothing was done.
7361 	 */
7362 	if ((un->un_dp->options & ST_MODE_SEL_COMP) == 0) {
7363 		un->un_comp_page = 0;
7364 		return (ENOTTY);
7365 	}
7366 
7367 	/* set compression based on minor node opened */
7368 	minor = MT_DENSITY(un->un_dev);
7369 
7370 	/*
7371 	 * If this the compression density or
7372 	 * the drive has two densities and uses mode select for
7373 	 * control of compression turn on compression for MT_DENSITY2
7374 	 * as well.
7375 	 */
7376 	if ((minor == ST_COMPRESSION_DENSITY) ||
7377 	    (minor == MT_DENSITY(MT_DENSITY2)) &&
7378 	    (un->un_dp->densities[0] == un->un_dp->densities[1]) &&
7379 	    (un->un_dp->densities[2] == un->un_dp->densities[3]) &&
7380 	    (un->un_dp->densities[0] != un->un_dp->densities[2])) {
7381 
7382 		turn_compression_on = 1;
7383 	} else {
7384 		turn_compression_on = 0;
7385 	}
7386 
7387 	un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16);
7388 	un->un_mspl->mid_bl  = (uchar_t)(un->un_bsize >> 8);
7389 	un->un_mspl->low_bl  = (uchar_t)(un->un_bsize);
7390 
7391 	/*
7392 	 * Need to determine which page does the device use for compression.
7393 	 * First try the data compression page. If this fails try the device
7394 	 * configuration page
7395 	 */
7396 
7397 	if ((un->un_comp_page & ST_DEV_DATACOMP_PAGE) == ST_DEV_DATACOMP_PAGE) {
7398 		rval = st_set_datacomp_page(un, turn_compression_on);
7399 		if (rval == EALREADY) {
7400 			return (rval);
7401 		}
7402 		if (rval != 0) {
7403 			if (un->un_status == KEY_ILLEGAL_REQUEST) {
7404 				/*
7405 				 * This device does not support data
7406 				 * compression page
7407 				 */
7408 				un->un_comp_page = ST_DEV_CONFIG_PAGE;
7409 			} else if (un->un_state >= ST_STATE_OPEN) {
7410 				un->un_fileno = -1;
7411 				rval = EIO;
7412 			} else {
7413 				rval = -1;
7414 			}
7415 		} else {
7416 			un->un_comp_page = ST_DEV_DATACOMP_PAGE;
7417 		}
7418 	}
7419 
7420 	if ((un->un_comp_page & ST_DEV_CONFIG_PAGE) == ST_DEV_CONFIG_PAGE) {
7421 		rval = st_set_devconfig_page(un, turn_compression_on);
7422 		if (rval == EALREADY) {
7423 			return (rval);
7424 		}
7425 		if (rval != 0) {
7426 			if (un->un_status == KEY_ILLEGAL_REQUEST) {
7427 				/*
7428 				 * This device does not support
7429 				 * compression at all advice the
7430 				 * user and unset ST_MODE_SEL_COMP
7431 				 */
7432 				un->un_dp->options &= ~ST_MODE_SEL_COMP;
7433 				un->un_comp_page = 0;
7434 				scsi_log(ST_DEVINFO, st_label, CE_NOTE,
7435 				    "Device Does Not Support Compression\n");
7436 			} else if (un->un_state >= ST_STATE_OPEN) {
7437 				un->un_fileno = -1;
7438 				rval = EIO;
7439 			} else {
7440 				rval = -1;
7441 			}
7442 		}
7443 	}
7444 
7445 	return (rval);
7446 }
7447 
7448 /*
7449  * set or unset compression thru device configuration page.
7450  */
7451 static int
7452 st_set_devconfig_page(struct scsi_tape *un, int compression_on)
7453 {
7454 	unsigned char cflag;
7455 	int rval = 0;
7456 
7457 	ASSERT(mutex_owned(ST_MUTEX));
7458 
7459 	/*
7460 	 * Figure what to set compression flag to.
7461 	 */
7462 	if (compression_on) {
7463 		/* They have selected a compression node */
7464 		if (un->un_dp->type == ST_TYPE_FUJI) {
7465 			cflag = 0x84;   /* use EDRC */
7466 		} else {
7467 			cflag = ST_DEV_CONFIG_DEF_COMP;
7468 		}
7469 	} else {
7470 		cflag = ST_DEV_CONFIG_NO_COMP;
7471 	}
7472 
7473 	/*
7474 	 * If compression is already set the way it was requested.
7475 	 * And if this not the first time we has tried.
7476 	 */
7477 	if ((cflag == un->un_mspl->page.dev.comp_alg) &&
7478 	    (un->un_comp_page == ST_DEV_DATACOMP_PAGE)) {
7479 		return (EALREADY);
7480 	}
7481 
7482 	un->un_mspl->page.dev.comp_alg = cflag;
7483 	/*
7484 	 * need to send mode select even if correct compression is
7485 	 * already set since need to set density code
7486 	 */
7487 
7488 #ifdef STDEBUG
7489 	if (st_debug >= 6) {
7490 		st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG,
7491 		    "st_set_devconfig_page: sense data for mode select",
7492 		    (char *)un->un_mspl, sizeof (struct seq_mode));
7493 	}
7494 #endif
7495 	rval = st_gen_mode_select(un, un->un_mspl, sizeof (struct seq_mode));
7496 
7497 	return (rval);
7498 }
7499 
7500 /*
7501  * set/reset compression bit thru data compression page
7502  */
7503 static int
7504 st_set_datacomp_page(struct scsi_tape *un, int compression_on)
7505 {
7506 	int compression_on_already;
7507 	int rval = 0;
7508 
7509 	ASSERT(mutex_owned(ST_MUTEX));
7510 
7511 	/*
7512 	 * If drive is not capable of compression (at this time)
7513 	 * return EALREADY so caller doesn't think that this page
7514 	 * is not supported. This check is for drives that can
7515 	 * disable compression from the front panel or configuration.
7516 	 * I doubt that a drive that supports this page is not really
7517 	 * capable of compression.
7518 	 */
7519 	if (un->un_mspl->page.comp.dcc == 0) {
7520 		return (EALREADY);
7521 	}
7522 
7523 	/* See if compression currently turned on */
7524 	if (un->un_mspl->page.comp.dce) {
7525 		compression_on_already = 1;
7526 	} else {
7527 		compression_on_already = 0;
7528 	}
7529 
7530 	/*
7531 	 * If compression is already set the way it was requested.
7532 	 * And if this not the first time we has tried.
7533 	 */
7534 	if ((compression_on == compression_on_already) &&
7535 	    (un->un_comp_page == ST_DEV_DATACOMP_PAGE)) {
7536 		return (EALREADY);
7537 	}
7538 
7539 	/*
7540 	 * if we are already set to the appropriate compression
7541 	 * mode, don't set it again
7542 	 */
7543 	if (compression_on) {
7544 		/* compression selected */
7545 		un->un_mspl->page.comp.dce = 1;
7546 	} else {
7547 		un->un_mspl->page.comp.dce = 0;
7548 	}
7549 
7550 
7551 #ifdef STDEBUG
7552 	if (st_debug >= 6) {
7553 		st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG,
7554 		    "st_set_datacomp_page: sense data for mode select",
7555 		    (char *)un->un_mspl, sizeof (struct seq_mode));
7556 	}
7557 #endif
7558 	rval = st_gen_mode_select(un, un->un_mspl, sizeof (struct seq_mode));
7559 
7560 	return (rval);
7561 }
7562 
7563 static int
7564 st_modesense(struct scsi_tape *un)
7565 {
7566 	int rval;
7567 	uchar_t page;
7568 
7569 	page = un->un_comp_page;
7570 
7571 	switch (page) {
7572 	case ST_DEV_DATACOMP_PAGE:
7573 	case ST_DEV_CONFIG_PAGE: /* fall through */
7574 		rval = st_gen_mode_sense(un, page, un->un_mspl,
7575 		    sizeof (struct seq_mode));
7576 		break;
7577 
7578 	case ST_DEV_DATACOMP_PAGE | ST_DEV_CONFIG_PAGE:
7579 		if (un->un_dp->options & ST_MODE_SEL_COMP) {
7580 			page = ST_DEV_DATACOMP_PAGE;
7581 			rval = st_gen_mode_sense(un, page, un->un_mspl,
7582 			    sizeof (struct seq_mode));
7583 			if (rval == 0 && un->un_mspl->page_code == page) {
7584 				un->un_comp_page = page;
7585 				break;
7586 			}
7587 			page = ST_DEV_CONFIG_PAGE;
7588 			rval = st_gen_mode_sense(un, page, un->un_mspl,
7589 			    sizeof (struct seq_mode));
7590 			if (rval == 0 && un->un_mspl->page_code == page) {
7591 				un->un_comp_page = page;
7592 				break;
7593 			}
7594 			un->un_dp->options &= ~ST_MODE_SEL_COMP;
7595 			un->un_comp_page = 0;
7596 		} else {
7597 			un->un_comp_page = 0;
7598 		}
7599 
7600 	default:	/* fall through */
7601 		rval = st_cmd(un->un_dev, SCMD_MODE_SENSE, MSIZE, SYNC_CMD);
7602 	}
7603 	return (rval);
7604 }
7605 
7606 static int
7607 st_modeselect(struct scsi_tape *un)
7608 {
7609 	int rval = 0;
7610 	int ix;
7611 
7612 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
7613 	    "st_modeselect(dev = 0x%lx): density = 0x%x\n",
7614 	    un->un_dev, un->un_mspl->density);
7615 
7616 	ASSERT(mutex_owned(ST_MUTEX));
7617 
7618 	/*
7619 	 * The parameter list should be the same for all of the
7620 	 * cases that follow so set them here
7621 	 *
7622 	 * Try mode select first if if fails set fields manually
7623 	 */
7624 	rval = st_modesense(un);
7625 	if (rval != 0) {
7626 		ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN,
7627 		    "st_modeselect: First mode sense failed\n");
7628 		un->un_mspl->bd_len  = 8;
7629 		un->un_mspl->high_nb = 0;
7630 		un->un_mspl->mid_nb  = 0;
7631 		un->un_mspl->low_nb  = 0;
7632 	}
7633 	un->un_mspl->high_bl = (uchar_t)(un->un_bsize >> 16);
7634 	un->un_mspl->mid_bl  = (uchar_t)(un->un_bsize >> 8);
7635 	un->un_mspl->low_bl  = (uchar_t)(un->un_bsize);
7636 
7637 
7638 	/*
7639 	 * If configured to use a specific density code for a media type.
7640 	 * curdens is previously set by the minor node opened.
7641 	 * If the media type doesn't match the minor node we change it so it
7642 	 * looks like the correct one was opened.
7643 	 */
7644 	if (un->un_dp->options & ST_KNOWS_MEDIA) {
7645 		uchar_t best;
7646 
7647 		for (best = 0xff, ix = 0; ix < NDENSITIES; ix++) {
7648 			if (un->un_mspl->media_type ==
7649 			    un->un_dp->mediatype[ix]) {
7650 				best = ix;
7651 				/*
7652 				 * It matches but it might not be the only one.
7653 				 * Use the highest matching media type but not
7654 				 * to exceed the density selected by the open.
7655 				 */
7656 				if (ix < un->un_curdens) {
7657 					continue;
7658 				}
7659 				un->un_curdens = ix;
7660 				break;
7661 			}
7662 		}
7663 		/* If a match was found best will not be 0xff any more */
7664 		if (best < NDENSITIES) {
7665 			ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN,
7666 			    "found media 0x%X using density 0x%X\n",
7667 			    un->un_mspl->media_type,
7668 			    un->un_dp->densities[best]);
7669 			un->un_mspl->density = un->un_dp->densities[best];
7670 		} else {
7671 			/* Otherwise set density based on minor node opened */
7672 			un->un_mspl->density =
7673 			    un->un_dp->densities[un->un_curdens];
7674 		}
7675 	} else {
7676 		un->un_mspl->density = un->un_dp->densities[un->un_curdens];
7677 	}
7678 
7679 	if (un->un_dp->options & ST_NOBUF) {
7680 		un->un_mspl->bufm = 0;
7681 	} else {
7682 		un->un_mspl->bufm = 1;
7683 	}
7684 
7685 	rval = st_set_compression(un);
7686 
7687 	/*
7688 	 * If st_set_compression returned invalid or already it
7689 	 * found no need to do the mode select.
7690 	 * So do it here.
7691 	 */
7692 	if ((rval == ENOTTY) || (rval == EALREADY)) {
7693 
7694 		/* Zero non-writeable fields */
7695 		un->un_mspl->data_len = 0;
7696 		un->un_mspl->media_type = 0;
7697 		un->un_mspl->wp = 0;
7698 
7699 		/* need to set the density code */
7700 		rval = st_cmd(un->un_dev, SCMD_MODE_SELECT, MSIZE, SYNC_CMD);
7701 		if (rval != 0) {
7702 			if (un->un_state >= ST_STATE_OPEN) {
7703 				ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
7704 				    "unable to set tape mode\n");
7705 				un->un_fileno = -1;
7706 				rval = EIO;
7707 			} else {
7708 				rval = -1;
7709 			}
7710 		}
7711 	}
7712 
7713 	/*
7714 	 * The spec recommends to send a mode sense after a mode select
7715 	 */
7716 	(void) st_modesense(un);
7717 
7718 	ASSERT(mutex_owned(ST_MUTEX));
7719 
7720 	return (rval);
7721 }
7722 
7723 /*
7724  * st_gen_mode_sense
7725  *
7726  * generic mode sense.. it allows for any page
7727  */
7728 static int
7729 st_gen_mode_sense(struct scsi_tape *un, int page, struct seq_mode *page_data,
7730     int page_size)
7731 {
7732 
7733 	int r;
7734 	char	cdb[CDB_GROUP0];
7735 	struct uscsi_cmd *com;
7736 
7737 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
7738 
7739 	bzero(cdb, CDB_GROUP0);
7740 	cdb[0] = SCMD_MODE_SENSE;
7741 	cdb[2] = (char)page;
7742 	cdb[4] = (char)page_size;
7743 
7744 	com->uscsi_cdb = cdb;
7745 	com->uscsi_cdblen = CDB_GROUP0;
7746 	com->uscsi_bufaddr = (caddr_t)page_data;
7747 	com->uscsi_buflen = page_size;
7748 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
7749 	com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT |
7750 			    USCSI_READ | USCSI_RQENABLE;
7751 
7752 	r = st_ioctl_cmd(un->un_dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
7753 		UIO_SYSSPACE);
7754 	kmem_free(com, sizeof (*com));
7755 	return (r);
7756 }
7757 
7758 /*
7759  * st_gen_mode_select
7760  *
7761  * generic mode select.. it allows for any page
7762  */
7763 static int
7764 st_gen_mode_select(struct scsi_tape *un, struct seq_mode *page_data,
7765     int page_size)
7766 {
7767 
7768 	int r;
7769 	char cdb[CDB_GROUP0];
7770 	struct uscsi_cmd *com;
7771 
7772 	/* Zero non-writeable fields */
7773 	page_data->data_len = 0;
7774 	page_data->media_type = 0;
7775 	page_data->wp = 0;
7776 
7777 	/*
7778 	 * If mode select has any page data, zero the ps (Page Savable) bit.
7779 	 */
7780 	if (page_size > MSIZE) {
7781 		page_data->ps = 0;
7782 	}
7783 
7784 
7785 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
7786 
7787 	/*
7788 	 * then, do a mode select to set what ever info
7789 	 */
7790 	bzero(cdb, CDB_GROUP0);
7791 	cdb[0] = SCMD_MODE_SELECT;
7792 	cdb[1] = 0x10;		/* set PF bit for many third party drives */
7793 	cdb[4] = (char)page_size;
7794 
7795 	com->uscsi_cdb = cdb;
7796 	com->uscsi_cdblen = CDB_GROUP0;
7797 	com->uscsi_bufaddr = (caddr_t)page_data;
7798 	com->uscsi_buflen = page_size;
7799 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
7800 	com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT
7801 		| USCSI_WRITE | USCSI_RQENABLE;
7802 
7803 	r = st_ioctl_cmd(un->un_dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
7804 		UIO_SYSSPACE);
7805 
7806 	kmem_free(com, sizeof (*com));
7807 	return (r);
7808 }
7809 
7810 /*
7811  * Changes devices blocksize and bsize to requested blocksize nblksz.
7812  * Returns returned value from first failed call or zero on success.
7813  */
7814 static int
7815 st_change_block_size(dev_t dev, uint32_t nblksz)
7816 {
7817 	struct seq_mode *current;
7818 	int rval;
7819 	uint32_t oldblksz;
7820 
7821 	GET_SOFT_STATE(dev);
7822 
7823 	current = kmem_zalloc(MSIZE, KM_SLEEP);
7824 
7825 	/* Read current settings */
7826 	rval = st_gen_mode_sense(un, 0, current, MSIZE);
7827 	if (rval != 0) {
7828 		scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
7829 		    "mode sense for change block size failed: rval = %d", rval);
7830 		goto finish;
7831 	}
7832 
7833 	/* Figure the current block size */
7834 	oldblksz =
7835 	    (current->high_bl << 16) |
7836 	    (current->mid_bl << 8) |
7837 	    (current->low_bl);
7838 
7839 	/* If current block size is the same as requested were done */
7840 	if (oldblksz == nblksz) {
7841 		un->un_bsize = nblksz;
7842 		rval = 0;
7843 		goto finish;
7844 	}
7845 
7846 	/* Change to requested block size */
7847 	current->high_bl = (uchar_t)(nblksz >> 16);
7848 	current->mid_bl  = (uchar_t)(nblksz >> 8);
7849 	current->low_bl  = (uchar_t)(nblksz);
7850 
7851 	/* Attempt to change block size */
7852 	rval = st_gen_mode_select(un, current, MSIZE);
7853 	if (rval != 0) {
7854 		scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
7855 		    "Set new block size failed: rval = %d", rval);
7856 		goto finish;
7857 	}
7858 
7859 	/* Read back and verify setting */
7860 	rval = st_modesense(un);
7861 	if (rval == 0) {
7862 		un->un_bsize =
7863 		    (un->un_mspl->high_bl << 16) |
7864 		    (un->un_mspl->mid_bl << 8) |
7865 		    (un->un_mspl->low_bl);
7866 
7867 		if (un->un_bsize != nblksz) {
7868 			scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
7869 			    "Blocksize set does not equal requested blocksize"
7870 			    "(read: %u requested: %u)\n", nblksz, un->un_bsize);
7871 			rval = EIO;
7872 		}
7873 	}
7874 finish:
7875 	kmem_free(current, MSIZE);
7876 	return (rval);
7877 }
7878 
7879 
7880 static void
7881 st_init(struct scsi_tape *un)
7882 {
7883 	ASSERT(mutex_owned(ST_MUTEX));
7884 
7885 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
7886 	"st_init(): dev = 0x%lx, will reset fileno, blkno, eof\n", un->un_dev);
7887 
7888 	un->un_blkno = 0;
7889 	un->un_fileno = 0;
7890 	un->un_lastop = ST_OP_NIL;
7891 	un->un_eof = ST_NO_EOF;
7892 	un->un_pwr_mgmt = ST_PWR_NORMAL;
7893 	if (st_error_level != SCSI_ERR_ALL) {
7894 		if (DEBUGGING) {
7895 			st_error_level = SCSI_ERR_ALL;
7896 		} else {
7897 			st_error_level = SCSI_ERR_RETRYABLE;
7898 		}
7899 	}
7900 }
7901 
7902 
7903 static void
7904 st_make_cmd(struct scsi_tape *un, struct buf *bp, int (*func)(caddr_t))
7905 {
7906 	struct scsi_pkt *pkt;
7907 	struct uscsi_cmd *ucmd;
7908 	int count, tval = 0;
7909 	int flags = 0;
7910 	uchar_t com;
7911 	char fixbit;
7912 
7913 	ASSERT(mutex_owned(ST_MUTEX));
7914 
7915 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
7916 	    "st_make_cmd(): dev = 0x%lx\n", un->un_dev);
7917 
7918 
7919 	/*
7920 	 * fixbit is for setting the Fixed Mode and Suppress Incorrect
7921 	 * Length Indicator bits on read/write commands, for setting
7922 	 * the Long bit on erase commands, and for setting the Code
7923 	 * Field bits on space commands.
7924 	 * XXX why do we set lastop here?
7925 	 */
7926 
7927 	if (bp != un->un_sbufp) {		/* regular raw I/O */
7928 		int stat_size = (un->un_arq_enabled ?
7929 			sizeof (struct scsi_arq_status) : 1);
7930 		pkt = scsi_init_pkt(ROUTE, NULL, bp,
7931 		    CDB_GROUP0, stat_size, 0, 0, func, (caddr_t)un);
7932 		if (pkt == NULL) {
7933 			goto exit;
7934 		}
7935 		SET_BP_PKT(bp, pkt);
7936 		if (un->un_bsize == 0) {
7937 			count = bp->b_bcount;
7938 			fixbit = 0;
7939 		} else {
7940 			count = bp->b_bcount / un->un_bsize;
7941 			fixbit = 1;
7942 		}
7943 		if (bp->b_flags & B_READ) {
7944 			com = SCMD_READ;
7945 			un->un_lastop = ST_OP_READ;
7946 			if ((un->un_bsize == 0) && /* Not Fixed Block */
7947 			    (un->un_dp->options & ST_READ_IGNORE_ILI)) {
7948 				fixbit = 2;
7949 			}
7950 		} else {
7951 			com = SCMD_WRITE;
7952 			un->un_lastop = ST_OP_WRITE;
7953 		}
7954 
7955 		tval = un->un_dp->io_timeout;
7956 
7957 		/*
7958 		 * For really large xfers, increase timeout
7959 		 */
7960 		if (bp->b_bcount > (10 * ONE_MEG))
7961 			tval *= bp->b_bcount/(10 * ONE_MEG);
7962 
7963 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7964 		    "%s %ld amt 0x%lx\n", (com == SCMD_WRITE) ?
7965 		    wr_str: rd_str, un->un_blkno, bp->b_bcount);
7966 
7967 	} else if ((ucmd = BP_UCMD(bp)) != NULL) {
7968 		/*
7969 		 * uscsi - build command, allocate scsi resources
7970 		 */
7971 		st_make_uscsi_cmd(un, ucmd, bp, func);
7972 		goto exit;
7973 
7974 	} else {				/* special I/O */
7975 		struct buf *allocbp = NULL;
7976 		int stat_size = (un->un_arq_enabled ?
7977 			sizeof (struct scsi_arq_status) : 1);
7978 
7979 
7980 		com = (uchar_t)(uintptr_t)bp->b_forw;
7981 		count = bp->b_bcount;
7982 
7983 		switch (com) {
7984 		case SCMD_READ:
7985 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7986 			    "special read %d\n", count);
7987 			if (un->un_bsize == 0) {
7988 				fixbit = 2;	/* suppress SILI */
7989 			} else {
7990 				fixbit = 1;	/* Fixed Block Mode */
7991 				count /= un->un_bsize;
7992 			}
7993 			allocbp = bp;
7994 			un->un_lastop = ST_OP_READ;
7995 			tval = un->un_dp->io_timeout;
7996 			break;
7997 
7998 		case SCMD_WRITE:
7999 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8000 			    "special write %d\n", count);
8001 			if (un->un_bsize != 0) {
8002 				fixbit = 1;	/* Fixed Block Mode */
8003 				count /= un->un_bsize;
8004 			} else {
8005 				fixbit = 0;
8006 			}
8007 			allocbp = bp;
8008 			un->un_lastop = ST_OP_WRITE;
8009 			tval = un->un_dp->io_timeout;
8010 			break;
8011 
8012 		case SCMD_WRITE_FILE_MARK:
8013 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8014 			    "write %d file marks\n", count);
8015 			un->un_lastop = ST_OP_WEOF;
8016 			fixbit = 0;
8017 			tval = un->un_dp->io_timeout;
8018 			break;
8019 
8020 		case SCMD_REWIND:
8021 			if (bp->b_flags & B_ASYNC) {
8022 				fixbit = 1;
8023 			} else {
8024 				fixbit = 0;
8025 			}
8026 			count = 0;
8027 			un->un_lastop = ST_OP_CTL;
8028 			tval = un->un_dp->rewind_timeout;
8029 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8030 			    "rewind\n");
8031 			break;
8032 
8033 		case SCMD_SPACE:
8034 			fixbit = Isfmk(count);
8035 			count = (int)space_cnt(count);
8036 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8037 			    "space %d %s from file %d blk %ld\n",
8038 			    count, (fixbit) ? "filemarks" : "records",
8039 			    un->un_fileno, un->un_blkno);
8040 			un->un_lastop = ST_OP_CTL;
8041 			tval = un->un_dp->space_timeout;
8042 			break;
8043 
8044 		case SCMD_LOAD:
8045 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8046 			    "%s tape\n", (count & 1) ? "load" : "unload");
8047 			fixbit = 0;
8048 
8049 			/* Loading or Unloading */
8050 			if (count & 1) {
8051 				tval = un->un_dp->load_timeout;
8052 			} else {
8053 				tval = un->un_dp->unload_timeout;
8054 			}
8055 			/* Is Retension requested */
8056 			if (count & 2) {
8057 				tval += un->un_dp->rewind_timeout;
8058 			}
8059 			un->un_lastop = ST_OP_CTL;
8060 			break;
8061 
8062 		case SCMD_ERASE:
8063 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8064 			    "erase tape\n");
8065 			count = 0;
8066 			/*
8067 			 * We support long erase only
8068 			 */
8069 			fixbit = 1;
8070 			tval = un->un_dp->erase_timeout;
8071 			un->un_lastop = ST_OP_CTL;
8072 			break;
8073 
8074 		case SCMD_MODE_SENSE:
8075 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8076 			    "mode sense\n");
8077 			allocbp = bp;
8078 			fixbit = 0;
8079 			tval = un->un_dp->non_motion_timeout;
8080 			break;
8081 
8082 		case SCMD_MODE_SELECT:
8083 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8084 			    "mode select\n");
8085 			allocbp = bp;
8086 			fixbit = 0;
8087 			tval = un->un_dp->non_motion_timeout;
8088 			break;
8089 
8090 		case SCMD_RESERVE:
8091 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8092 			    "reserve\n");
8093 			fixbit = 0;
8094 			tval = un->un_dp->non_motion_timeout;
8095 			break;
8096 
8097 		case SCMD_RELEASE:
8098 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8099 			    "release\n");
8100 			fixbit = 0;
8101 			tval = un->un_dp->non_motion_timeout;
8102 			break;
8103 
8104 		case SCMD_READ_BLKLIM:
8105 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8106 			    "read block limits\n");
8107 			allocbp = bp;
8108 			fixbit = count = 0;
8109 			tval = un->un_dp->non_motion_timeout;
8110 			break;
8111 
8112 		case SCMD_TEST_UNIT_READY:
8113 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8114 			    "test unit ready\n");
8115 			fixbit = 0;
8116 			tval = un->un_dp->non_motion_timeout;
8117 			break;
8118 		default:
8119 			ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC,
8120 			    "Unhandled scsi command 0x%x in st_make_cmd()\n",
8121 			    com);
8122 		}
8123 		pkt = scsi_init_pkt(ROUTE, NULL, allocbp,
8124 			CDB_GROUP0, stat_size, 0, 0, func, (caddr_t)un);
8125 		if (pkt == NULL) {
8126 			goto exit;
8127 		}
8128 		if (allocbp)
8129 			ASSERT(geterror(allocbp) == 0);
8130 
8131 	}
8132 
8133 	(void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
8134 	    com, 0, (uint_t)count, 0);
8135 	FILL_SCSI1_LUN(un->un_sd, pkt);
8136 	/*
8137 	 * Initialize the SILI/Fixed bits of the byte 1 of cdb.
8138 	 */
8139 	((union scsi_cdb *)(pkt->pkt_cdbp))->t_code = fixbit;
8140 	pkt->pkt_flags = flags;
8141 
8142 	/*
8143 	 * If ST_SHORT_FILEMARKS bit is ON for EXABYTE
8144 	 * device, set the Vendor Unique bit to
8145 	 * write Short File Mark.
8146 	 */
8147 	if (com == SCMD_WRITE_FILE_MARK &&
8148 		un->un_dp->options & ST_SHORT_FILEMARKS) {
8149 		switch (un->un_dp->type) {
8150 		case ST_TYPE_EXB8500:
8151 		case ST_TYPE_EXABYTE:
8152 			/*
8153 			 * Now the Vendor Unique bit 7 in Byte 5 of CDB
8154 			 * is set to to write Short File Mark
8155 			 */
8156 			((union scsi_cdb *)pkt->pkt_cdbp)->g0_vu_1 = 1;
8157 			break;
8158 
8159 		default:
8160 			/*
8161 			 * Well, if ST_SHORT_FILEMARKS is set for other
8162 			 * tape drives, it is just ignored
8163 			 */
8164 			break;
8165 		}
8166 	}
8167 	ASSERT(tval);
8168 	pkt->pkt_time = tval;
8169 	pkt->pkt_comp = st_intr;
8170 	pkt->pkt_private = (opaque_t)bp;
8171 	SET_BP_PKT(bp, pkt);
8172 
8173 exit:
8174 	ASSERT(mutex_owned(ST_MUTEX));
8175 }
8176 
8177 
8178 /*
8179  * Build a command based on a uscsi command;
8180  */
8181 static void
8182 st_make_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *ucmd,
8183     struct buf *bp, int (*func)(caddr_t))
8184 {
8185 	struct scsi_pkt *pkt;
8186 	caddr_t cdb;
8187 	int	cdblen;
8188 	int stat_size;
8189 
8190 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8191 	    "st_make_uscsi_cmd(): dev = 0x%lx\n", un->un_dev);
8192 
8193 	if (ucmd->uscsi_flags & USCSI_RQENABLE) {
8194 		stat_size = (un->un_arq_enabled ?
8195 		    sizeof (struct scsi_arq_status) : 1);
8196 	} else {
8197 		stat_size = 1;
8198 	}
8199 
8200 	ASSERT(mutex_owned(ST_MUTEX));
8201 
8202 	un->un_lastop = ST_OP_CTL;	/* usual */
8203 
8204 	cdb = ucmd->uscsi_cdb;
8205 	cdblen = ucmd->uscsi_cdblen;
8206 
8207 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8208 	    "st_make_uscsi_cmd: buflen=%ld bcount=%ld\n",
8209 		ucmd->uscsi_buflen, bp->b_bcount);
8210 	pkt = scsi_init_pkt(ROUTE, NULL,
8211 		(bp->b_bcount > 0) ? bp : NULL,
8212 		cdblen, stat_size, 0, 0, func, (caddr_t)un);
8213 	if (pkt == NULL) {
8214 		goto exit;
8215 	}
8216 
8217 	bcopy(cdb, pkt->pkt_cdbp, (uint_t)cdblen);
8218 
8219 #ifdef STDEBUG
8220 	if (st_debug >= 6) {
8221 		st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG,
8222 		    "pkt_cdbp", (char *)cdb, cdblen);
8223 	}
8224 #endif
8225 
8226 	if (ucmd->uscsi_flags & USCSI_SILENT) {
8227 		pkt->pkt_flags |= FLAG_SILENT;
8228 	}
8229 
8230 	pkt->pkt_time = ucmd->uscsi_timeout;
8231 	pkt->pkt_comp = st_intr;
8232 	pkt->pkt_private = (opaque_t)bp;
8233 	SET_BP_PKT(bp, pkt);
8234 exit:
8235 	ASSERT(mutex_owned(ST_MUTEX));
8236 }
8237 
8238 
8239 /*
8240  * restart cmd currently at the head of the runq
8241  *
8242  * If scsi_transport() succeeds or the retries
8243  * count exhausted, restore the throttle that was
8244  * zeroed out in st_handle_intr_busy().
8245  *
8246  */
8247 static void
8248 st_intr_restart(void *arg)
8249 {
8250 	struct scsi_tape *un = arg;
8251 	struct buf *bp;
8252 	int status = TRAN_ACCEPT;
8253 
8254 	mutex_enter(ST_MUTEX);
8255 
8256 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8257 		"st_intr_restart(), un = 0x%p\n", (void *)un);
8258 
8259 	un->un_hib_tid = 0;
8260 
8261 	/*
8262 	 * move from waitq to runq, if there is anything on the waitq
8263 	 */
8264 	if ((bp = un->un_quef) == NULL) {
8265 		mutex_exit(ST_MUTEX);
8266 		return;
8267 	}
8268 
8269 	/*
8270 	 * Here we know :
8271 	 *	throttle = 0, via st_handle_intr_busy
8272 	 */
8273 
8274 	if (un->un_quel == bp) {
8275 		un->un_quel = NULL;
8276 		un->un_quef = NULL;	/* we know it's the first one */
8277 	} else {
8278 		un->un_quef = bp->b_actf;
8279 	}
8280 	bp->b_actf = NULL;
8281 
8282 	if (un->un_runqf) {
8283 		/*
8284 		 * not good, we don't want to requeue something after
8285 		 * another.
8286 		 */
8287 		mutex_exit(ST_MUTEX);
8288 		goto done_error;
8289 	} else {
8290 		un->un_runqf = bp;
8291 		un->un_runql = bp;
8292 	}
8293 
8294 	ST_DO_KSTATS(bp, kstat_waitq_to_runq);
8295 
8296 	mutex_exit(ST_MUTEX);
8297 
8298 	status = scsi_transport(BP_PKT(bp));
8299 
8300 	mutex_enter(ST_MUTEX);
8301 
8302 	if (status != TRAN_ACCEPT) {
8303 		ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
8304 		mutex_exit(ST_MUTEX);
8305 
8306 		if (status == TRAN_BUSY) {
8307 			if (st_handle_intr_busy(un, bp,
8308 			    ST_TRAN_BUSY_TIMEOUT) == 0)
8309 				return;	/* timeout is setup again */
8310 		}
8311 
8312 	} else {
8313 		un->un_tran_retry_ct = 0;
8314 		if (un->un_last_throttle) {
8315 			un->un_throttle = un->un_last_throttle;
8316 		}
8317 		mutex_exit(ST_MUTEX);
8318 		return;
8319 	}
8320 
8321 done_error:
8322 	ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
8323 	    "restart transport rejected\n");
8324 	bp->b_resid = bp->b_bcount;
8325 
8326 #ifndef __lock_lint
8327 	/*
8328 	 * warlock doesn't understand this potential
8329 	 * recursion?
8330 	 */
8331 	mutex_enter(ST_MUTEX);
8332 	if (un->un_last_throttle) {
8333 		un->un_throttle = un->un_last_throttle;
8334 	}
8335 	if (status != TRAN_ACCEPT)
8336 		ST_DO_ERRSTATS(un, st_transerrs);
8337 	ST_DO_KSTATS(bp, kstat_waitq_exit);
8338 	SET_PE_FLAG(un);
8339 	st_bioerror(bp, EIO);
8340 	st_done_and_mutex_exit(un, bp);
8341 #endif
8342 	ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
8343 	    "busy restart aborted\n");
8344 }
8345 
8346 /*
8347  * st_check_media():
8348  * Periodically check the media state using scsi_watch service;
8349  * this service calls back after TUR and possibly request sense
8350  * the callback handler (st_media_watch_cb()) decodes the request sense
8351  * data (if any)
8352  */
8353 
8354 static int
8355 st_check_media(dev_t dev, enum mtio_state state)
8356 {
8357 	int rval = 0;
8358 	enum mtio_state	prev_state;
8359 	opaque_t token = NULL;
8360 
8361 	GET_SOFT_STATE(dev);
8362 
8363 	mutex_enter(ST_MUTEX);
8364 
8365 	ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8366 	    "st_check_media:state=%x, mediastate=%x\n",
8367 	    state, un->un_mediastate);
8368 
8369 	prev_state = un->un_mediastate;
8370 
8371 	/*
8372 	 * is there anything to do?
8373 	 */
8374 retry:
8375 	if (state == un->un_mediastate || un->un_mediastate == MTIO_NONE) {
8376 		/*
8377 		 * submit the request to the scsi_watch service;
8378 		 * scsi_media_watch_cb() does the real work
8379 		 */
8380 		mutex_exit(ST_MUTEX);
8381 		token = scsi_watch_request_submit(ST_SCSI_DEVP,
8382 			st_check_media_time, SENSE_LENGTH,
8383 			st_media_watch_cb, (caddr_t)dev);
8384 		if (token == NULL) {
8385 			rval = EAGAIN;
8386 			goto done;
8387 		}
8388 		mutex_enter(ST_MUTEX);
8389 
8390 		un->un_swr_token = token;
8391 		un->un_specified_mediastate = state;
8392 
8393 		/*
8394 		 * now wait for media change
8395 		 * we will not be signalled unless mediastate == state but it
8396 		 * still better to test for this condition, since there
8397 		 * is a 5 sec cv_broadcast delay when
8398 		 *  mediastate == MTIO_INSERTED
8399 		 */
8400 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8401 			"st_check_media:waiting for media state change\n");
8402 		while (un->un_mediastate == state) {
8403 			if (cv_wait_sig(&un->un_state_cv, ST_MUTEX) == 0) {
8404 				mutex_exit(ST_MUTEX);
8405 				ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8406 				    "st_check_media:waiting for media state "
8407 				    "was interrupted\n");
8408 				rval = EINTR;
8409 				goto done;
8410 			}
8411 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8412 			    "st_check_media:received signal, state=%x\n",
8413 			    un->un_mediastate);
8414 		}
8415 	}
8416 
8417 	/*
8418 	 * if we transitioned to MTIO_INSERTED, media has really been
8419 	 * inserted.  If TUR fails, it is probably a exabyte slow spin up.
8420 	 * Reset and retry the state change.  If everything is ok, replay
8421 	 * the open() logic.
8422 	 */
8423 	if ((un->un_mediastate == MTIO_INSERTED) &&
8424 	    (un->un_state == ST_STATE_OFFLINE)) {
8425 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8426 		    "st_check_media: calling st_cmd to confirm inserted\n");
8427 
8428 		/*
8429 		 * set this early so that TUR will make it through strategy
8430 		 * without triggering a st_tape_init().  We needed it set
8431 		 * before calling st_tape_init() ourselves anyway.  If TUR
8432 		 * fails, set it back
8433 		 */
8434 		un->un_state = ST_STATE_INITIALIZING;
8435 
8436 		/*
8437 		 * If not reserved fail as getting reservation conflict
8438 		 * will make this hang forever.
8439 		 */
8440 		if ((un->un_rsvd_status &
8441 		    (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) {
8442 			mutex_exit(ST_MUTEX);
8443 			rval = EACCES;
8444 			goto done;
8445 		}
8446 		rval = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD);
8447 		if (rval == EACCES) {
8448 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8449 			    "st_check_media: TUR got Reservation Conflict\n");
8450 			mutex_exit(ST_MUTEX);
8451 			goto done;
8452 		}
8453 		if (rval) {
8454 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8455 			    "st_check_media: TUR failed, going to retry\n");
8456 			un->un_mediastate = prev_state;
8457 			un->un_state = ST_STATE_OFFLINE;
8458 			goto retry;
8459 		}
8460 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8461 		    "st_check_media: media inserted\n");
8462 
8463 		/* this also rewinds the tape */
8464 		rval = st_tape_init(dev);
8465 		if (rval != 0) {
8466 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8467 			    "st_check_media : OFFLINE init failure ");
8468 			un->un_state = ST_STATE_OFFLINE;
8469 			un->un_fileno = -1;
8470 		} else {
8471 			un->un_state = ST_STATE_OPEN_PENDING_IO;
8472 			un->un_fileno = 0;
8473 			un->un_blkno = 0;
8474 		}
8475 	} else if ((un->un_mediastate == MTIO_EJECTED) &&
8476 		(un->un_state != ST_STATE_OFFLINE)) {
8477 		/*
8478 		 * supported devices must be rewound before ejection
8479 		 * rewind resets fileno & blkno
8480 		 */
8481 		un->un_laststate = un->un_state;
8482 		un->un_state = ST_STATE_OFFLINE;
8483 	}
8484 	mutex_exit(ST_MUTEX);
8485 done:
8486 	if (token) {
8487 		(void) scsi_watch_request_terminate(token,
8488 				SCSI_WATCH_TERMINATE_WAIT);
8489 		mutex_enter(ST_MUTEX);
8490 		un->un_swr_token = (opaque_t)NULL;
8491 		mutex_exit(ST_MUTEX);
8492 	}
8493 
8494 	ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_media: done\n");
8495 
8496 	return (rval);
8497 }
8498 
8499 /*
8500  * st_media_watch_cb() is called by scsi_watch_thread for
8501  * verifying the request sense data (if any)
8502  */
8503 static int
8504 st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
8505 {
8506 	struct scsi_status *statusp = resultp->statusp;
8507 	struct scsi_extended_sense *sensep = resultp->sensep;
8508 	uchar_t actual_sense_length = resultp->actual_sense_length;
8509 	struct scsi_tape *un;
8510 	enum mtio_state state = MTIO_NONE;
8511 	int instance;
8512 	dev_t dev = (dev_t)arg;
8513 
8514 	instance = MTUNIT(dev);
8515 	if ((un = ddi_get_soft_state(st_state, instance)) == NULL) {
8516 		return (-1);
8517 	}
8518 
8519 	mutex_enter(ST_MUTEX);
8520 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8521 		"st_media_watch_cb: status=%x, sensep=%p, len=%x\n",
8522 			*((char *)statusp), (void *)sensep,
8523 			actual_sense_length);
8524 
8525 	/*
8526 	 * if there was a check condition then sensep points to valid
8527 	 * sense data
8528 	 * if status was not a check condition but a reservation or busy
8529 	 * status then the new state is MTIO_NONE
8530 	 */
8531 	if (sensep) {
8532 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8533 		    "st_media_watch_cb: KEY=%x, ASC=%x, ASCQ=%x\n",
8534 		    sensep->es_key, sensep->es_add_code, sensep->es_qual_code);
8535 
8536 		switch (un->un_dp->type) {
8537 		default:
8538 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8539 	    "st_media_watch_cb: unknown drive type %d, default to ST_TYPE_HP\n",
8540 	    un->un_dp->type);
8541 		/* FALLTHROUGH */
8542 
8543 		case ST_TYPE_STC3490:	/* STK 4220 1/2" cartridge */
8544 		case ST_TYPE_FUJI:	/* 1/2" cartridge */
8545 		case ST_TYPE_HP:	/* HP 88780 1/2" reel */
8546 			if (un->un_dp->type == ST_TYPE_FUJI) {
8547 				ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8548 				    "st_media_watch_cb: ST_TYPE_FUJI\n");
8549 			} else {
8550 				ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8551 				    "st_media_watch_cb: ST_TYPE_HP\n");
8552 			}
8553 			switch (sensep->es_key) {
8554 			case KEY_UNIT_ATTENTION:
8555 				/* not ready to ready transition */
8556 				/* hp/es_qual_code == 80 on>off>on */
8557 				/* hp/es_qual_code == 0 on>off>unld>ld>on */
8558 				if (sensep->es_add_code == 0x28) {
8559 					state = MTIO_INSERTED;
8560 				}
8561 				break;
8562 			case KEY_NOT_READY:
8563 				/* in process, rewinding or loading */
8564 				if ((sensep->es_add_code == 0x04) &&
8565 				    (sensep->es_qual_code == 0x00)) {
8566 					state = MTIO_EJECTED;
8567 				}
8568 				break;
8569 			}
8570 			break;
8571 
8572 		case ST_TYPE_EXB8500:	/* Exabyte 8500 */
8573 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8574 			    "st_media_watch_cb: ST_TYPE_EXB8500\n");
8575 			switch (sensep->es_key) {
8576 			case KEY_UNIT_ATTENTION:
8577 				/* operator medium removal request */
8578 				if ((sensep->es_add_code == 0x5a) &&
8579 				    (sensep->es_qual_code == 0x01)) {
8580 					state = MTIO_EJECTED;
8581 				/* not ready to ready transition */
8582 				} else if ((sensep->es_add_code == 0x28) &&
8583 				    (sensep->es_qual_code == 0x00)) {
8584 					state = MTIO_INSERTED;
8585 				}
8586 				break;
8587 			case KEY_NOT_READY:
8588 				/* medium not present */
8589 				if (sensep->es_add_code == 0x3a) {
8590 					state = MTIO_EJECTED;
8591 				}
8592 				break;
8593 			}
8594 			break;
8595 		case ST_TYPE_EXABYTE:	/* Exabyte 8200 */
8596 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8597 			    "st_media_watch_cb: ST_TYPE_EXABYTE\n");
8598 			switch (sensep->es_key) {
8599 			case KEY_NOT_READY:
8600 				if ((sensep->es_add_code == 0x04) &&
8601 				    (sensep->es_qual_code == 0x00)) {
8602 					/* volume not mounted? */
8603 					state = MTIO_EJECTED;
8604 				} else if (sensep->es_add_code == 0x3a) {
8605 					state = MTIO_EJECTED;
8606 				}
8607 				break;
8608 			case KEY_UNIT_ATTENTION:
8609 				state = MTIO_EJECTED;
8610 				break;
8611 			}
8612 			break;
8613 
8614 		case ST_TYPE_DLT:		/* quantum DLT4xxx */
8615 			switch (sensep->es_key) {
8616 			case KEY_UNIT_ATTENTION:
8617 				if (sensep->es_add_code == 0x28) {
8618 					state = MTIO_INSERTED;
8619 				}
8620 				break;
8621 			case KEY_NOT_READY:
8622 				if (sensep->es_add_code == 0x04) {
8623 					/* in transition but could be either */
8624 					state = un->un_specified_mediastate;
8625 				} else if ((sensep->es_add_code == 0x3a) &&
8626 				    (sensep->es_qual_code == 0x00)) {
8627 					state = MTIO_EJECTED;
8628 				}
8629 				break;
8630 			}
8631 			break;
8632 		}
8633 	} else if (*((char *)statusp) == STATUS_GOOD) {
8634 		state = MTIO_INSERTED;
8635 	}
8636 
8637 	ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8638 	    "st_media_watch_cb:state=%x, specified=%x\n",
8639 	    state, un->un_specified_mediastate);
8640 
8641 	/*
8642 	 * now signal the waiting thread if this is *not* the specified state;
8643 	 * delay the signal if the state is MTIO_INSERTED
8644 	 * to allow the target to recover
8645 	 */
8646 	if (state != un->un_specified_mediastate) {
8647 		un->un_mediastate = state;
8648 		if (state == MTIO_INSERTED) {
8649 			/*
8650 			 * delay the signal to give the drive a chance
8651 			 * to do what it apparently needs to do
8652 			 */
8653 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8654 			    "st_media_watch_cb:delayed cv_broadcast\n");
8655 			un->un_delay_tid = timeout(st_delayed_cv_broadcast,
8656 			    un, drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
8657 		} else {
8658 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8659 			    "st_media_watch_cb:immediate cv_broadcast\n");
8660 			cv_broadcast(&un->un_state_cv);
8661 		}
8662 	}
8663 	mutex_exit(ST_MUTEX);
8664 	return (0);
8665 }
8666 
8667 /*
8668  * delayed cv_broadcast to allow for target to recover
8669  * from media insertion
8670  */
8671 static void
8672 st_delayed_cv_broadcast(void *arg)
8673 {
8674 	struct scsi_tape *un = arg;
8675 
8676 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8677 	    "st_delayed_cv_broadcast:delayed cv_broadcast\n");
8678 
8679 	mutex_enter(ST_MUTEX);
8680 	cv_broadcast(&un->un_state_cv);
8681 	mutex_exit(ST_MUTEX);
8682 }
8683 
8684 /*
8685  * restart cmd currently at the start of the waitq
8686  */
8687 static void
8688 st_start_restart(void *arg)
8689 {
8690 	struct scsi_tape *un = arg;
8691 
8692 	ASSERT(un != NULL);
8693 
8694 	mutex_enter(ST_MUTEX);
8695 
8696 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8697 		"st_tran_restart()\n");
8698 
8699 	if (un->un_quef) {
8700 		st_start(un);
8701 	}
8702 
8703 	mutex_exit(ST_MUTEX);
8704 }
8705 
8706 
8707 /*
8708  * Command completion processing
8709  *
8710  */
8711 static void
8712 st_intr(struct scsi_pkt *pkt)
8713 {
8714 	struct scsi_tape *un;
8715 	struct buf *last_runqf;
8716 	struct buf *bp;
8717 	int action = COMMAND_DONE;
8718 	clock_t	timout;
8719 	int	status;
8720 
8721 	bp = (struct buf *)pkt->pkt_private;
8722 	un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev));
8723 
8724 	mutex_enter(ST_MUTEX);
8725 
8726 	un->un_rqs_state &= ~(ST_RQS_ERROR);
8727 
8728 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_intr()\n");
8729 
8730 	if (pkt->pkt_reason != CMD_CMPLT) {
8731 
8732 		/* If device has gone away not much else to do */
8733 		if (pkt->pkt_reason == CMD_DEV_GONE) {
8734 			action = COMMAND_DONE_ERROR;
8735 		} else if (un->un_state == ST_STATE_SENSING) {
8736 			ST_DO_ERRSTATS(un, st_transerrs);
8737 			action = COMMAND_DONE_ERROR;
8738 		} else {
8739 			action = st_handle_incomplete(un, bp);
8740 		}
8741 	/*
8742 	 * At this point we know that the command was successfully
8743 	 * completed. Now what?
8744 	 */
8745 	} else if (un->un_arq_enabled &&
8746 	    (pkt->pkt_state & STATE_ARQ_DONE)) {
8747 		/*
8748 		 * the transport layer successfully completed an autorqsense
8749 		 */
8750 		action = st_handle_autosense(un, bp);
8751 
8752 	} else if (un->un_state == ST_STATE_SENSING) {
8753 		/*
8754 		 * okay. We were running a REQUEST SENSE. Find
8755 		 * out what to do next.
8756 		 * some actions are based on un_state, hence
8757 		 * restore the state st was in before ST_STATE_SENSING.
8758 		 */
8759 		un->un_state = un->un_laststate;
8760 		action = st_handle_sense(un, bp);
8761 		/*
8762 		 * set pkt back to original packet in case we will have
8763 		 * to requeue it
8764 		 */
8765 		pkt = BP_PKT(bp);
8766 	} else  if ((SCBP(pkt)->sts_busy) || (SCBP(pkt)->sts_chk)) {
8767 		/*
8768 		 * Okay, we weren't running a REQUEST SENSE. Call a routine
8769 		 * to see if the status bits we're okay. If a request sense
8770 		 * is to be run, that will happen.
8771 		 */
8772 		action = st_check_error(un, pkt);
8773 	}
8774 
8775 	if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) {
8776 		switch (action) {
8777 			case QUE_COMMAND:
8778 				/*
8779 				 * return cmd to head to the queue
8780 				 * since we are suspending so that
8781 				 * it gets restarted during resume
8782 				 */
8783 				if (un->un_runqf) {
8784 					last_runqf = un->un_runqf;
8785 					un->un_runqf = bp;
8786 					bp->b_actf = last_runqf;
8787 				} else {
8788 					bp->b_actf = NULL;
8789 					un->un_runqf = bp;
8790 					un->un_runql = bp;
8791 				}
8792 				action = JUST_RETURN;
8793 				break;
8794 
8795 			case QUE_SENSE:
8796 				action = COMMAND_DONE_ERROR;
8797 				break;
8798 
8799 			default:
8800 				break;
8801 		}
8802 	}
8803 
8804 	/*
8805 	 * Restore old state if we were sensing.
8806 	 */
8807 	if (un->un_state == ST_STATE_SENSING && action != QUE_SENSE) {
8808 		un->un_state = un->un_laststate;
8809 	}
8810 
8811 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8812 	    "st_intr: pkt=%p, bp=%p, action=%x, status=%x\n",
8813 	    (void *)pkt, (void *)bp, action, SCBP_C(pkt));
8814 
8815 
8816 	switch (action) {
8817 	case COMMAND_DONE_EACCES:
8818 		/* this is to report a reservation conflict */
8819 		st_bioerror(bp, EACCES);
8820 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8821 			"Reservation Conflict \n");
8822 
8823 		/*FALLTHROUGH*/
8824 	case COMMAND_DONE_ERROR:
8825 		if (un->un_eof < ST_EOT_PENDING &&
8826 		    un->un_state >= ST_STATE_OPEN) {
8827 			/*
8828 			 * all errors set state of the tape to 'unknown'
8829 			 * unless we're at EOT or are doing append testing.
8830 			 * If sense key was illegal request, preserve state.
8831 			 */
8832 			if (un->un_status != KEY_ILLEGAL_REQUEST) {
8833 				un->un_fileno = -1;
8834 			}
8835 		}
8836 		un->un_err_resid = bp->b_resid = bp->b_bcount;
8837 		/*
8838 		 * since we have an error (COMMAND_DONE_ERROR), we want to
8839 		 * make sure an error ocurrs, so make sure at least EIO is
8840 		 * returned
8841 		 */
8842 		if (geterror(bp) == 0)
8843 			st_bioerror(bp, EIO);
8844 
8845 		SET_PE_FLAG(un);
8846 		if (!(un->un_rqs_state & ST_RQS_ERROR) &&
8847 		    (un->un_errno == EIO)) {
8848 			un->un_rqs_state &= ~(ST_RQS_VALID);
8849 		}
8850 		goto done;
8851 
8852 	case COMMAND_DONE_ERROR_RECOVERED:
8853 		un->un_err_resid = bp->b_resid = bp->b_bcount;
8854 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
8855 		    "st_intr(): COMMAND_DONE_ERROR_RECOVERED");
8856 		if (geterror(bp) == 0)
8857 			st_bioerror(bp, EIO);
8858 		SET_PE_FLAG(un);
8859 		if (!(un->un_rqs_state & ST_RQS_ERROR) &&
8860 		    (un->un_errno == EIO)) {
8861 			un->un_rqs_state &= ~(ST_RQS_VALID);
8862 		}
8863 		/*FALLTHROUGH*/
8864 	case COMMAND_DONE:
8865 		st_set_state(un);
8866 done:
8867 		ST_DO_KSTATS(bp, kstat_runq_exit);
8868 		st_done_and_mutex_exit(un, bp);
8869 		return;
8870 
8871 	case QUE_SENSE:
8872 		if ((un->un_ncmds > 1) && !un->un_flush_on_errors)
8873 			goto sense_error;
8874 
8875 		if (un->un_state != ST_STATE_SENSING) {
8876 			un->un_laststate = un->un_state;
8877 			un->un_state = ST_STATE_SENSING;
8878 		}
8879 
8880 		un->un_rqs->pkt_private = (opaque_t)bp;
8881 		bzero(ST_RQSENSE, SENSE_LENGTH);
8882 
8883 		if (un->un_throttle) {
8884 			un->un_last_throttle = un->un_throttle;
8885 			un->un_throttle = 0;
8886 		}
8887 
8888 		mutex_exit(ST_MUTEX);
8889 
8890 		/*
8891 		 * never retry this, some other command will have nuked the
8892 		 * sense, anyway
8893 		 */
8894 		status = scsi_transport(un->un_rqs);
8895 
8896 		mutex_enter(ST_MUTEX);
8897 
8898 		if (un->un_last_throttle) {
8899 			un->un_throttle = un->un_last_throttle;
8900 		}
8901 
8902 		if (status == TRAN_ACCEPT) {
8903 			mutex_exit(ST_MUTEX);
8904 			return;
8905 		}
8906 		if (status != TRAN_BUSY)
8907 			ST_DO_ERRSTATS(un, st_transerrs);
8908 sense_error:
8909 		un->un_fileno = -1;
8910 		st_bioerror(bp, EIO);
8911 		SET_PE_FLAG(un);
8912 		goto done;
8913 
8914 	case QUE_BUSY_COMMAND:
8915 		/* longish timeout */
8916 		timout = ST_STATUS_BUSY_TIMEOUT;
8917 		goto que_it_up;
8918 
8919 	case QUE_COMMAND:
8920 		/* short timeout */
8921 		timout = ST_TRAN_BUSY_TIMEOUT;
8922 que_it_up:
8923 		/*
8924 		 * let st_handle_intr_busy put this bp back on waitq and make
8925 		 * checks to see if it is ok to requeue the command.
8926 		 */
8927 		ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
8928 
8929 		/*
8930 		 * Save the throttle before setting up the timeout
8931 		 */
8932 		if (un->un_throttle) {
8933 			un->un_last_throttle = un->un_throttle;
8934 		}
8935 		mutex_exit(ST_MUTEX);
8936 		if (st_handle_intr_busy(un, bp, timout) == 0)
8937 			return;		/* timeout is setup again */
8938 
8939 		mutex_enter(ST_MUTEX);
8940 		un->un_fileno = -1;
8941 		un->un_err_resid = bp->b_resid = bp->b_bcount;
8942 		st_bioerror(bp, EIO);
8943 		SET_PE_FLAG(un);
8944 		goto done;
8945 
8946 	case QUE_LAST_COMMAND:
8947 
8948 		if ((un->un_ncmds > 1) && !un->un_flush_on_errors) {
8949 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
8950 			    "un_ncmds: %d can't retry cmd \n", un->un_ncmds);
8951 			goto last_command_error;
8952 		}
8953 		mutex_exit(ST_MUTEX);
8954 		if (st_handle_intr_retry_lcmd(un, bp) == 0)
8955 			return;
8956 		mutex_enter(ST_MUTEX);
8957 last_command_error:
8958 		un->un_err_resid = bp->b_resid = bp->b_bcount;
8959 		un->un_fileno = -1;
8960 		st_bioerror(bp, EIO);
8961 		SET_PE_FLAG(un);
8962 		goto done;
8963 
8964 	case JUST_RETURN:
8965 	default:
8966 		ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
8967 		mutex_exit(ST_MUTEX);
8968 		return;
8969 	}
8970 	/*NOTREACHED*/
8971 }
8972 
8973 static int
8974 st_handle_incomplete(struct scsi_tape *un, struct buf *bp)
8975 {
8976 	static char *fail = "SCSI transport failed: reason '%s': %s\n";
8977 	int rval = COMMAND_DONE_ERROR;
8978 	struct scsi_pkt *pkt = (un->un_state == ST_STATE_SENSING) ?
8979 			un->un_rqs : BP_PKT(bp);
8980 	int result;
8981 
8982 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8983 		"st_handle_incomplete(): dev = 0x%lx\n", un->un_dev);
8984 
8985 	ASSERT(mutex_owned(ST_MUTEX));
8986 
8987 	switch (pkt->pkt_reason) {
8988 	case CMD_INCOMPLETE:	/* tran stopped with not normal state */
8989 		/*
8990 		 * this occurs when accessing a powered down drive, no
8991 		 * need to complain; just fail the open
8992 		 */
8993 #ifdef STDEBUG
8994 		if (st_debug >= 1) {
8995 			st_clean_print(ST_DEVINFO, st_label, CE_WARN,
8996 			    "Failed CDB", (char *)pkt->pkt_cdbp, CDB_SIZE);
8997 		}
8998 
8999 #endif
9000 		/*
9001 		 * if we have commands outstanding in HBA, and a command
9002 		 * comes back incomplete, we're hosed, so reset target
9003 		 * If we have the bus, but cmd_incomplete, we probably just
9004 		 * have a failed selection, so don't reset the target, just
9005 		 * requeue the command and try again
9006 		 */
9007 		if ((un->un_ncmds > 1) || (pkt->pkt_state != STATE_GOT_BUS)) {
9008 			goto reset_target;
9009 		}
9010 
9011 		/*
9012 		 * Retry selection a couple more times if we're
9013 		 * open.  If opening, we only try just once to
9014 		 * reduce probe time for nonexistant devices.
9015 		 */
9016 		if ((un->un_laststate > ST_STATE_OPENING) &&
9017 		    ((int)un->un_retry_ct < st_selection_retry_count)) {
9018 			rval = QUE_COMMAND;
9019 		}
9020 		ST_DO_ERRSTATS(un, st_transerrs);
9021 		break;
9022 
9023 	case CMD_ABORTED:
9024 		/*
9025 		 * most likely this is caused by flush-on-error support. If
9026 		 * it was not there, the we're in trouble.
9027 		 */
9028 		if (!un->un_flush_on_errors) {
9029 			un->un_status = SUN_KEY_FATAL;
9030 			goto reset_target;
9031 		}
9032 
9033 		st_set_pe_errno(un);
9034 		bioerror(bp, un->un_errno);
9035 		if (un->un_errno)
9036 			return (COMMAND_DONE_ERROR);
9037 		else
9038 			return (COMMAND_DONE);
9039 
9040 	case CMD_TIMEOUT:	/* Command timed out */
9041 		un->un_status = SUN_KEY_TIMEOUT;
9042 
9043 		/*FALLTHROUGH*/
9044 	default:
9045 reset_target:
9046 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9047 		    "transport completed with %s\n",
9048 		    scsi_rname(pkt->pkt_reason));
9049 		ST_DO_ERRSTATS(un, st_transerrs);
9050 		if ((pkt->pkt_state & STATE_GOT_TARGET) &&
9051 		    ((pkt->pkt_statistics & (STAT_BUS_RESET | STAT_DEV_RESET |
9052 			STAT_ABORTED)) == 0)) {
9053 
9054 			/*
9055 			 * If we haven't reserved the drive don't reset it.
9056 			 */
9057 			if ((un->un_rsvd_status &
9058 			    (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) {
9059 				return (rval);
9060 			}
9061 
9062 			mutex_exit(ST_MUTEX);
9063 
9064 			result = scsi_reset(ROUTE, RESET_TARGET);
9065 			/*
9066 			 * if target reset fails, then pull the chain
9067 			 */
9068 			if (result == 0) {
9069 				result = scsi_reset(ROUTE, RESET_ALL);
9070 			}
9071 			mutex_enter(ST_MUTEX);
9072 
9073 			if ((result == 0) && (un->un_state >= ST_STATE_OPEN)) {
9074 				/* no hope left to recover */
9075 				scsi_log(ST_DEVINFO, st_label, CE_WARN,
9076 				    "recovery by resets failed\n");
9077 				return (rval);
9078 			}
9079 		}
9080 	}
9081 
9082 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
9083 		(STAT_BUS_RESET | STAT_DEV_RESET))) {
9084 		if ((un->un_rsvd_status & ST_RESERVE)) {
9085 			un->un_rsvd_status |= ST_LOST_RESERVE;
9086 			ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN,
9087 				"Lost Reservation\n");
9088 		}
9089 	}
9090 
9091 	if ((int)un->un_retry_ct++ < st_retry_count) {
9092 		if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) {
9093 			rval = QUE_COMMAND;
9094 		} else if (bp == un->un_sbufp) {
9095 			switch ((uchar_t)(uintptr_t)bp->b_forw) {
9096 			case SCMD_MODE_SENSE:
9097 			case SCMD_MODE_SELECT:
9098 			case SCMD_READ_BLKLIM:
9099 			case SCMD_REWIND:
9100 			case SCMD_LOAD:
9101 			case SCMD_TEST_UNIT_READY:
9102 				/*
9103 				 * These commands can be rerun with impunity
9104 				 */
9105 				rval = QUE_COMMAND;
9106 				break;
9107 
9108 			default:
9109 				break;
9110 			}
9111 		}
9112 	} else {
9113 		rval = COMMAND_DONE_ERROR;
9114 	}
9115 
9116 	if (un->un_state >= ST_STATE_OPEN) {
9117 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
9118 		    fail, scsi_rname(pkt->pkt_reason),
9119 		    (rval == COMMAND_DONE_ERROR)?
9120 		    "giving up" : "retrying command");
9121 	}
9122 	return (rval);
9123 }
9124 
9125 /*
9126  * if the device is busy, then put this bp back on the waitq, on the
9127  * interrupt thread, where we want the head of the queue and not the
9128  * end
9129  *
9130  * The callers of this routine should take measures to save the
9131  * un_throttle in un_last_throttle which will be restored in
9132  * st_intr_restart(). The only exception should be st_intr_restart()
9133  * calling this routine for which the saving is already done.
9134  */
9135 static int
9136 st_handle_intr_busy(struct scsi_tape *un, struct buf *bp,
9137 	clock_t timeout_interval)
9138 {
9139 	struct buf *last_quef;
9140 	int rval = 0;
9141 
9142 	mutex_enter(ST_MUTEX);
9143 
9144 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9145 	    "st_handle_intr_busy(), un = 0x%p\n", (void *)un);
9146 
9147 	/*
9148 	 * Check to see if we hit the retry timeout. We check to make sure
9149 	 * this is the first one on the runq and make sure we have not
9150 	 * queued up any more, so this one has to be the last on the list
9151 	 * also. If it is not, we have to fail.  If it is not the first, but
9152 	 * is the last we are in trouble anyway, as we are in the interrupt
9153 	 * context here.
9154 	 */
9155 	if (((int)un->un_tran_retry_ct++ > st_retry_count) ||
9156 	    ((un->un_runqf != bp) && (un->un_runql != bp))) {
9157 		rval = -1;
9158 		goto exit;
9159 	}
9160 
9161 	/* put the bp back on the waitq */
9162 	if (un->un_quef) {
9163 		last_quef = un->un_quef;
9164 		un->un_quef = bp;
9165 		bp->b_actf = last_quef;
9166 	} else  {
9167 		bp->b_actf = NULL;
9168 		un->un_quef = bp;
9169 		un->un_quel = bp;
9170 	}
9171 
9172 	/*
9173 	 * We know that this is the first and last on the runq at this time,
9174 	 * so we just nullify those two queues
9175 	 */
9176 	un->un_runqf = NULL;
9177 	un->un_runql = NULL;
9178 
9179 	/*
9180 	 * We don't want any other commands being started in the mean time.
9181 	 * If start had just released mutex after putting something on the
9182 	 * runq, we won't even get here.
9183 	 */
9184 	un->un_throttle = 0;
9185 
9186 	/*
9187 	 * send a marker pkt, if appropriate
9188 	 */
9189 	st_hba_unflush(un);
9190 
9191 	/*
9192 	 * all queues are aligned, we are just waiting to
9193 	 * transport
9194 	 */
9195 	un->un_hib_tid = timeout(st_intr_restart, un, timeout_interval);
9196 
9197 exit:
9198 	mutex_exit(ST_MUTEX);
9199 	return (rval);
9200 }
9201 
9202 static int
9203 st_handle_sense(struct scsi_tape *un, struct buf *bp)
9204 {
9205 	struct scsi_pkt *rqpkt = un->un_rqs;
9206 	int rval = COMMAND_DONE_ERROR;
9207 	int amt;
9208 
9209 	ASSERT(mutex_owned(ST_MUTEX));
9210 
9211 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9212 		"st_handle_sense()\n");
9213 
9214 	if (SCBP(rqpkt)->sts_busy) {
9215 		ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN,
9216 		    "busy unit on request sense\n");
9217 		if ((int)un->un_retry_ct++ < st_retry_count) {
9218 			rval = QUE_BUSY_COMMAND;
9219 		}
9220 		return (rval);
9221 	} else if (SCBP(rqpkt)->sts_chk) {
9222 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9223 		    "Check Condition on REQUEST SENSE\n");
9224 		return (rval);
9225 	}
9226 
9227 	/* was there enough data? */
9228 	amt = (int)SENSE_LENGTH - rqpkt->pkt_resid;
9229 	if ((rqpkt->pkt_state & STATE_XFERRED_DATA) == 0 ||
9230 	    (amt < SUN_MIN_SENSE_LENGTH)) {
9231 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9232 		    "REQUEST SENSE couldn't get sense data\n");
9233 		return (rval);
9234 	}
9235 	return (st_decode_sense(un, bp, amt, SCBP(rqpkt)));
9236 }
9237 
9238 static int
9239 st_handle_autosense(struct scsi_tape *un, struct buf *bp)
9240 {
9241 	struct scsi_pkt *pkt = BP_PKT(bp);
9242 	struct scsi_arq_status *arqstat =
9243 	    (struct scsi_arq_status *)pkt->pkt_scbp;
9244 	int rval = COMMAND_DONE_ERROR;
9245 	int amt;
9246 
9247 	ASSERT(mutex_owned(ST_MUTEX));
9248 
9249 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9250 		"st_handle_autosense()\n");
9251 
9252 	if (arqstat->sts_rqpkt_status.sts_busy) {
9253 		ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN,
9254 		    "busy unit on request sense\n");
9255 		/*
9256 		 * we return QUE_SENSE so st_intr will setup the SENSE cmd.
9257 		 * the disadvantage is that we do not have any delay for the
9258 		 * second retry of rqsense and we have to keep a packet around
9259 		 */
9260 		return (QUE_SENSE);
9261 
9262 	} else if (arqstat->sts_rqpkt_reason != CMD_CMPLT) {
9263 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9264 		    "transport error on REQUEST SENSE\n");
9265 		if ((arqstat->sts_rqpkt_state & STATE_GOT_TARGET) &&
9266 		    ((arqstat->sts_rqpkt_statistics &
9267 		    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) == 0)) {
9268 			mutex_exit(ST_MUTEX);
9269 			if (scsi_reset(ROUTE, RESET_TARGET) == 0) {
9270 				/*
9271 				 * if target reset fails, then pull the chain
9272 				 */
9273 				if (scsi_reset(ROUTE, RESET_ALL) == 0) {
9274 					ST_DEBUG6(ST_DEVINFO, st_label,
9275 					    CE_WARN,
9276 					    "recovery by resets failed\n");
9277 				}
9278 			}
9279 			mutex_enter(ST_MUTEX);
9280 		}
9281 		return (rval);
9282 
9283 	} else if (arqstat->sts_rqpkt_status.sts_chk) {
9284 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9285 		    "Check Condition on REQUEST SENSE\n");
9286 		return (rval);
9287 	}
9288 
9289 
9290 	/* was there enough data? */
9291 	amt = (int)SENSE_LENGTH - arqstat->sts_rqpkt_resid;
9292 	if ((arqstat->sts_rqpkt_state & STATE_XFERRED_DATA) == 0 ||
9293 	    (amt < SUN_MIN_SENSE_LENGTH)) {
9294 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9295 		    "REQUEST SENSE couldn't get sense data\n");
9296 		return (rval);
9297 	}
9298 
9299 	bcopy(&arqstat->sts_sensedata, ST_RQSENSE, SENSE_LENGTH);
9300 
9301 	return (st_decode_sense(un, bp, amt, &arqstat->sts_rqpkt_status));
9302 }
9303 
9304 static int
9305 st_decode_sense(struct scsi_tape *un, struct buf *bp,  int amt,
9306 	struct scsi_status *statusp)
9307 {
9308 	struct scsi_pkt *pkt = BP_PKT(bp);
9309 	int rval = COMMAND_DONE_ERROR;
9310 	long resid;
9311 	struct scsi_extended_sense *sensep = ST_RQSENSE;
9312 	int severity;
9313 	int get_error;
9314 
9315 	ASSERT(mutex_owned(ST_MUTEX));
9316 
9317 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9318 		"st_decode_sense()\n");
9319 
9320 	/*
9321 	 * For uscsi commands, squirrel away a copy of the
9322 	 * results of the Request Sense.
9323 	 */
9324 	if (USCSI_CMD(bp)) {
9325 		struct uscsi_cmd *ucmd = BP_UCMD(bp);
9326 		ucmd->uscsi_rqstatus = *(uchar_t *)statusp;
9327 		if (ucmd->uscsi_rqlen && un->un_srqbufp) {
9328 			uchar_t rqlen = min((uchar_t)amt, ucmd->uscsi_rqlen);
9329 			ucmd->uscsi_rqresid = ucmd->uscsi_rqlen - rqlen;
9330 			bcopy(ST_RQSENSE, un->un_srqbufp, rqlen);
9331 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
9332 				"st_decode_sense: stat=0x%x resid=0x%x\n",
9333 				ucmd->uscsi_rqstatus, ucmd->uscsi_rqresid);
9334 		}
9335 	}
9336 
9337 	/*
9338 	 * If the drive is an MT-02, reposition the
9339 	 * secondary error code into the proper place.
9340 	 *
9341 	 * XXX	MT-02 is non-CCS tape, so secondary error code
9342 	 * is in byte 8.  However, in SCSI-2, tape has CCS definition
9343 	 * so it's in byte 12.
9344 	 */
9345 	if (un->un_dp->type == ST_TYPE_EMULEX) {
9346 		sensep->es_code = sensep->es_add_info[0];
9347 	}
9348 
9349 	/* for normal I/O check extract the resid values. */
9350 	if (bp != un->un_sbufp) {
9351 		if (sensep->es_valid) {
9352 			resid = (sensep->es_info_1 << 24) |
9353 				(sensep->es_info_2 << 16) |
9354 				(sensep->es_info_3 << 8)  |
9355 				(sensep->es_info_4);
9356 			if (un->un_bsize) {
9357 				resid *= un->un_bsize;
9358 			}
9359 		} else if (pkt->pkt_state & STATE_XFERRED_DATA) {
9360 			resid = pkt->pkt_resid;
9361 		} else {
9362 			resid = bp->b_bcount;
9363 		}
9364 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9365 		    "st_handle_sense (rw): xferred bit = %d, resid=%ld (%d), "
9366 		    "pkt_resid=%ld\n", pkt->pkt_state & STATE_XFERRED_DATA,
9367 		    resid,
9368 		    (sensep->es_info_1 << 24) |
9369 		    (sensep->es_info_2 << 16) |
9370 		    (sensep->es_info_3 << 8)  |
9371 		    (sensep->es_info_4),
9372 		    pkt->pkt_resid);
9373 		/*
9374 		 * The problem is, what should we believe?
9375 		 */
9376 		if (resid && (pkt->pkt_resid == 0)) {
9377 			pkt->pkt_resid = resid;
9378 		}
9379 	} else {
9380 		/*
9381 		 * If the command is SCMD_SPACE, we need to get the
9382 		 * residual as returned in the sense data, to adjust
9383 		 * our idea of current tape position correctly
9384 		 */
9385 		if ((CDBP(pkt)->scc_cmd == SCMD_SPACE ||
9386 		    CDBP(pkt)->scc_cmd == SCMD_WRITE_FILE_MARK) &&
9387 		    (sensep->es_valid)) {
9388 			resid = (sensep->es_info_1 << 24) |
9389 			    (sensep->es_info_2 << 16) |
9390 			    (sensep->es_info_3 << 8)  |
9391 			    (sensep->es_info_4);
9392 			bp->b_resid = resid;
9393 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9394 			    "st_handle_sense(other):	resid=%ld\n",
9395 			    resid);
9396 		} else {
9397 			/*
9398 			 * If the special command is SCMD_READ,
9399 			 * the correct resid will be set later.
9400 			 */
9401 			resid = bp->b_bcount;
9402 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9403 			    "st_handle_sense(special read):  resid=%ld\n",
9404 				resid);
9405 		}
9406 	}
9407 
9408 	if ((un->un_state >= ST_STATE_OPEN) &&
9409 	    (DEBUGGING || st_error_level == SCSI_ERR_ALL)) {
9410 		st_clean_print(ST_DEVINFO, st_label, CE_NOTE,
9411 		    "Failed CDB", (char *)pkt->pkt_cdbp, CDB_SIZE);
9412 		st_clean_print(ST_DEVINFO, st_label, CE_CONT,
9413 		    "sense data", (char *)sensep, amt);
9414 		scsi_log(ST_DEVINFO, st_label, CE_CONT,
9415 		    "count 0x%lx resid 0x%lx pktresid 0x%lx\n",
9416 		    bp->b_bcount, resid, pkt->pkt_resid);
9417 	}
9418 
9419 	switch (un->un_status = sensep->es_key) {
9420 	case KEY_NO_SENSE:
9421 		severity = SCSI_ERR_INFO;
9422 		goto common;
9423 
9424 	case KEY_RECOVERABLE_ERROR:
9425 		severity = SCSI_ERR_RECOVERED;
9426 		if ((sensep->es_class == CLASS_EXTENDED_SENSE) &&
9427 		    (sensep->es_code == ST_DEFERRED_ERROR)) {
9428 		    if (un->un_dp->options &
9429 			ST_RETRY_ON_RECOVERED_DEFERRED_ERROR) {
9430 			    rval = QUE_LAST_COMMAND;
9431 			    scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity,
9432 				un->un_blkno, un->un_err_blkno, scsi_cmds,
9433 				sensep);
9434 			    scsi_log(ST_DEVINFO, st_label, CE_CONT,
9435 				"Command will be retried\n");
9436 			} else {
9437 			    severity = SCSI_ERR_FATAL;
9438 			    rval = COMMAND_DONE_ERROR_RECOVERED;
9439 			    ST_DO_ERRSTATS(un, st_softerrs);
9440 			    scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity,
9441 				un->un_blkno, un->un_err_blkno, scsi_cmds,
9442 				sensep);
9443 			}
9444 			break;
9445 		}
9446 common:
9447 		/*
9448 		 * XXX only want reads to be stopped by filemarks.
9449 		 * Don't want them to be stopped by EOT.  EOT matters
9450 		 * only on write.
9451 		 */
9452 		if (sensep->es_filmk && !sensep->es_eom) {
9453 			rval = COMMAND_DONE;
9454 		} else if (sensep->es_eom) {
9455 			rval = COMMAND_DONE;
9456 		} else if (sensep->es_ili) {
9457 			/*
9458 			 * Fun with variable length record devices:
9459 			 * for specifying larger blocks sizes than the
9460 			 * actual physical record size.
9461 			 */
9462 			if (un->un_bsize == 0 && resid > 0) {
9463 				/*
9464 				 * XXX! Ugly.
9465 				 * The requested blocksize is > tape blocksize,
9466 				 * so this is ok, so we just return the
9467 				 * actual size xferred.
9468 				 */
9469 				pkt->pkt_resid = resid;
9470 				rval = COMMAND_DONE;
9471 			} else if (un->un_bsize == 0 && resid < 0) {
9472 				/*
9473 				 * The requested blocksize is < tape blocksize,
9474 				 * so this is not ok, so we err with ENOMEM
9475 				 */
9476 				rval = COMMAND_DONE_ERROR_RECOVERED;
9477 				st_bioerror(bp, ENOMEM);
9478 			} else {
9479 				ST_DO_ERRSTATS(un, st_softerrs);
9480 				severity = SCSI_ERR_FATAL;
9481 				rval = COMMAND_DONE_ERROR;
9482 				st_bioerror(bp, EINVAL);
9483 			}
9484 		} else {
9485 			/*
9486 			 * we hope and pray for this just being
9487 			 * something we can ignore (ie. a
9488 			 * truly recoverable soft error)
9489 			 */
9490 			rval = COMMAND_DONE;
9491 		}
9492 		if (sensep->es_filmk) {
9493 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9494 			    "filemark\n");
9495 			un->un_status = SUN_KEY_EOF;
9496 			un->un_eof = ST_EOF_PENDING;
9497 			SET_PE_FLAG(un);
9498 		}
9499 
9500 		/*
9501 		 * ignore eom when reading, a fmk should terminate reading
9502 		 */
9503 		if ((sensep->es_eom) &&
9504 		    (CDBP(pkt)->scc_cmd != SCMD_READ)) {
9505 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "eom\n");
9506 			un->un_status = SUN_KEY_EOT;
9507 			un->un_eof = ST_EOM;
9508 			SET_PE_FLAG(un);
9509 		}
9510 
9511 		break;
9512 
9513 	case KEY_ILLEGAL_REQUEST:
9514 
9515 		if (un->un_laststate >= ST_STATE_OPEN) {
9516 			ST_DO_ERRSTATS(un, st_softerrs);
9517 			severity = SCSI_ERR_FATAL;
9518 		} else {
9519 			severity = SCSI_ERR_INFO;
9520 		}
9521 		break;
9522 
9523 	case KEY_MEDIUM_ERROR:
9524 		ST_DO_ERRSTATS(un, st_harderrs);
9525 		severity = SCSI_ERR_FATAL;
9526 
9527 		/*
9528 		 * for (buffered) writes, a medium error must be fatal
9529 		 */
9530 		if (CDBP(pkt)->scc_cmd != SCMD_WRITE) {
9531 			rval = COMMAND_DONE_ERROR_RECOVERED;
9532 		}
9533 
9534 check_keys:
9535 		/*
9536 		 * attempt to process the keys in the presence of
9537 		 * other errors
9538 		 */
9539 		if (sensep->es_ili && rval != COMMAND_DONE_ERROR) {
9540 			/*
9541 			 * Fun with variable length record devices:
9542 			 * for specifying larger blocks sizes than the
9543 			 * actual physical record size.
9544 			 */
9545 			if (un->un_bsize == 0 && resid > 0) {
9546 				/*
9547 				 * XXX! Ugly
9548 				 */
9549 				pkt->pkt_resid = resid;
9550 			} else if (un->un_bsize == 0 && resid < 0) {
9551 				st_bioerror(bp, EINVAL);
9552 			} else {
9553 				severity = SCSI_ERR_FATAL;
9554 				rval = COMMAND_DONE_ERROR;
9555 				st_bioerror(bp, EINVAL);
9556 			}
9557 		}
9558 		if (sensep->es_filmk) {
9559 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9560 			    "filemark\n");
9561 			un->un_status = SUN_KEY_EOF;
9562 			un->un_eof = ST_EOF_PENDING;
9563 			SET_PE_FLAG(un);
9564 		}
9565 
9566 		/*
9567 		 * ignore eom when reading, a fmk should terminate reading
9568 		 */
9569 		if ((sensep->es_eom) &&
9570 		    (CDBP(pkt)->scc_cmd != SCMD_READ)) {
9571 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "eom\n");
9572 			un->un_status = SUN_KEY_EOT;
9573 			un->un_eof = ST_EOM;
9574 			SET_PE_FLAG(un);
9575 		}
9576 
9577 		break;
9578 
9579 	case KEY_VOLUME_OVERFLOW:
9580 		ST_DO_ERRSTATS(un, st_softerrs);
9581 		un->un_eof = ST_EOM;
9582 		severity = SCSI_ERR_FATAL;
9583 		rval = COMMAND_DONE_ERROR;
9584 		goto check_keys;
9585 
9586 	case KEY_HARDWARE_ERROR:
9587 		ST_DO_ERRSTATS(un, st_harderrs);
9588 		severity = SCSI_ERR_FATAL;
9589 		rval = COMMAND_DONE_ERROR;
9590 		if (un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE)
9591 			un->un_eject_tape_on_failure = st_check_asc_ascq(un);
9592 		break;
9593 
9594 	case KEY_BLANK_CHECK:
9595 		ST_DO_ERRSTATS(un, st_softerrs);
9596 		severity = SCSI_ERR_INFO;
9597 
9598 		/*
9599 		 * if not a special request and some data was xferred then it
9600 		 * it is not an error yet
9601 		 */
9602 		if (bp != un->un_sbufp && (bp->b_flags & B_READ)) {
9603 			/*
9604 			 * no error for read with or without data xferred
9605 			 */
9606 			un->un_status = SUN_KEY_EOT;
9607 			un->un_eof = ST_EOT;
9608 			rval = COMMAND_DONE_ERROR;
9609 			SET_PE_FLAG(un);
9610 			goto check_keys;
9611 		} else if (bp != un->un_sbufp &&
9612 		    (pkt->pkt_state & STATE_XFERRED_DATA)) {
9613 			rval = COMMAND_DONE;
9614 		} else {
9615 			rval = COMMAND_DONE_ERROR_RECOVERED;
9616 		}
9617 
9618 		if (un->un_laststate >= ST_STATE_OPEN) {
9619 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9620 			    "blank check\n");
9621 			un->un_eof = ST_EOM;
9622 		}
9623 		if ((CDBP(pkt)->scc_cmd == SCMD_SPACE) &&
9624 		    (un->un_dp->options & ST_KNOWS_EOD) &&
9625 		    (severity = SCSI_ERR_INFO)) {
9626 			/*
9627 			 * we were doing a fast forward by skipping
9628 			 * multiple fmk at the time
9629 			 */
9630 			st_bioerror(bp, EIO);
9631 			severity = SCSI_ERR_RECOVERED;
9632 			rval	 = COMMAND_DONE;
9633 		}
9634 		SET_PE_FLAG(un);
9635 		goto check_keys;
9636 
9637 	case KEY_WRITE_PROTECT:
9638 		if (st_wrongtapetype(un)) {
9639 			un->un_status = SUN_KEY_WRONGMEDIA;
9640 			ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9641 		"wrong tape for writing- use DC6150 tape (or equivalent)\n");
9642 			severity = SCSI_ERR_UNKNOWN;
9643 		} else {
9644 			severity = SCSI_ERR_FATAL;
9645 		}
9646 		ST_DO_ERRSTATS(un, st_harderrs);
9647 		rval = COMMAND_DONE_ERROR;
9648 		st_bioerror(bp, EACCES);
9649 		break;
9650 
9651 	case KEY_UNIT_ATTENTION:
9652 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9653 		    "KEY_UNIT_ATTENTION : un_state = %d\n", un->un_state);
9654 
9655 		/*
9656 		 * If we have detected a Bus Reset and the tape
9657 		 * drive has been reserved.
9658 		 */
9659 		if (ST_RQSENSE->es_add_code == 0x29 &&
9660 			(un->un_rsvd_status & ST_RESERVE)) {
9661 			un->un_rsvd_status |= ST_LOST_RESERVE;
9662 			ST_DEBUG(ST_DEVINFO, st_label, CE_WARN,
9663 				"st_decode_sense: Lost Reservation\n");
9664 		}
9665 
9666 		if (un->un_state <= ST_STATE_OPENING) {
9667 			/*
9668 			 * Look, the tape isn't open yet, now determine
9669 			 * if the cause is a BUS RESET, Save the file and
9670 			 * Block positions for the callers to recover from
9671 			 * the loss of position.
9672 			 */
9673 			if ((un->un_fileno >= 0) &&
9674 			(un->un_fileno || un->un_blkno)) {
9675 				if (ST_RQSENSE->es_add_code == 0x29) {
9676 					un->un_save_fileno = un->un_fileno;
9677 					un->un_save_blkno = un->un_blkno;
9678 					un->un_restore_pos = 1;
9679 				}
9680 			}
9681 
9682 			if ((int)un->un_retry_ct++ < st_retry_count) {
9683 				rval = QUE_COMMAND;
9684 			} else {
9685 				rval = COMMAND_DONE_ERROR;
9686 			}
9687 			severity = SCSI_ERR_INFO;
9688 
9689 		} else {
9690 			/*
9691 			 * Check if it is an Unexpected Unit Attention.
9692 			 * If state is >= ST_STATE_OPEN, we have
9693 			 * already done the initialization .
9694 			 * In this case it is Fatal Error
9695 			 * since no further reading/writing
9696 			 * can be done with fileno set to < 0.
9697 			 */
9698 			if (un->un_state >= ST_STATE_OPEN) {
9699 				ST_DO_ERRSTATS(un, st_harderrs);
9700 				severity = SCSI_ERR_FATAL;
9701 			} else {
9702 				severity = SCSI_ERR_INFO;
9703 			}
9704 			rval = COMMAND_DONE_ERROR;
9705 		}
9706 		un->un_fileno = -1;
9707 
9708 		break;
9709 
9710 	case KEY_NOT_READY:
9711 		/*
9712 		 * If in process of getting ready retry.
9713 		 */
9714 		if (sensep->es_add_code  == 0x04 &&
9715 		    sensep->es_qual_code == 0x01 &&
9716 		    un->un_retry_ct++ < st_retry_count) {
9717 			rval = QUE_COMMAND;
9718 			severity = SCSI_ERR_INFO;
9719 		} else {
9720 			/* give up */
9721 			rval = COMMAND_DONE_ERROR;
9722 			severity = SCSI_ERR_FATAL;
9723 		}
9724 
9725 		/*
9726 		 * If this was an error and after device opened
9727 		 * do error stats.
9728 		 */
9729 		if (rval == COMMAND_DONE_ERROR &&
9730 		    un->un_state > ST_STATE_OPENING) {
9731 			ST_DO_ERRSTATS(un, st_harderrs);
9732 		}
9733 
9734 		if (ST_RQSENSE->es_add_code == 0x3a) {
9735 			if (st_error_level >= SCSI_ERR_FATAL)
9736 				scsi_log(ST_DEVINFO, st_label, CE_NOTE,
9737 				    "Tape not inserted in drive\n");
9738 			un->un_mediastate = MTIO_EJECTED;
9739 			cv_broadcast(&un->un_state_cv);
9740 		}
9741 		if ((un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE) &&
9742 		    (rval != QUE_COMMAND))
9743 			un->un_eject_tape_on_failure = st_check_asc_ascq(un);
9744 		break;
9745 
9746 	case KEY_ABORTED_COMMAND:
9747 
9748 		/*
9749 		 * Probably a parity error...
9750 		 * if we retry here then this may cause data to be
9751 		 * written twice or data skipped during reading
9752 		 */
9753 		ST_DO_ERRSTATS(un, st_harderrs);
9754 		severity = SCSI_ERR_FATAL;
9755 		rval = COMMAND_DONE_ERROR;
9756 		goto check_keys;
9757 
9758 	default:
9759 		/*
9760 		 * Undecoded sense key.	 Try retries and hope
9761 		 * that will fix the problem.  Otherwise, we're
9762 		 * dead.
9763 		 */
9764 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9765 		    "Unhandled Sense Key '%s'\n",
9766 		    sense_keys[un->un_status]);
9767 		ST_DO_ERRSTATS(un, st_harderrs);
9768 		severity = SCSI_ERR_FATAL;
9769 		rval = COMMAND_DONE_ERROR;
9770 		goto check_keys;
9771 	}
9772 
9773 	if ((!(pkt->pkt_flags & FLAG_SILENT) &&
9774 	    un->un_state >= ST_STATE_OPEN) && (DEBUGGING ||
9775 		(un->un_laststate > ST_STATE_OPENING) &&
9776 		(severity >= st_error_level))) {
9777 
9778 		scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity,
9779 		    un->un_blkno, un->un_err_blkno, scsi_cmds, sensep);
9780 		if (sensep->es_filmk) {
9781 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
9782 			    "File Mark Detected\n");
9783 		}
9784 		if (sensep->es_eom) {
9785 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
9786 			    "End-of-Media Detected\n");
9787 		}
9788 		if (sensep->es_ili) {
9789 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
9790 			    "Incorrect Length Indicator Set\n");
9791 		}
9792 	}
9793 	get_error = geterror(bp);
9794 	if (((rval == COMMAND_DONE_ERROR) ||
9795 	    (rval == COMMAND_DONE_ERROR_RECOVERED)) &&
9796 	    ((get_error == EIO) || (get_error == 0))) {
9797 		un->un_rqs_state |= (ST_RQS_ERROR | ST_RQS_VALID);
9798 		bcopy(ST_RQSENSE, un->un_uscsi_rqs_buf, SENSE_LENGTH);
9799 		if (un->un_rqs_state & ST_RQS_READ) {
9800 		    un->un_rqs_state &= ~(ST_RQS_READ);
9801 		} else {
9802 		    un->un_rqs_state |= ST_RQS_OVR;
9803 		}
9804 	}
9805 
9806 	return (rval);
9807 }
9808 
9809 
9810 static int
9811 st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp)
9812 {
9813 	int status = TRAN_ACCEPT;
9814 
9815 	mutex_enter(ST_MUTEX);
9816 
9817 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9818 		"st_handle_intr_rtr_lcmd(), un = 0x%p\n", (void *)un);
9819 
9820 	/*
9821 	 * Check to see if we hit the retry timeout. We check to make sure
9822 	 * this is the first one on the runq and make sure we have not
9823 	 * queued up any more, so this one has to be the last on the list
9824 	 * also. If it is not, we have to fail.  If it is not the first, but
9825 	 * is the last we are in trouble anyway, as we are in the interrupt
9826 	 * context here.
9827 	 */
9828 	if (((int)un->un_retry_ct > st_retry_count) ||
9829 	    ((un->un_runqf != bp) && (un->un_runql != bp))) {
9830 	    goto exit;
9831 	}
9832 
9833 	if (un->un_throttle) {
9834 		un->un_last_throttle = un->un_throttle;
9835 		un->un_throttle = 0;
9836 	}
9837 
9838 	/*
9839 	 * Here we know : bp is the first and last one on the runq
9840 	 * it is not necessary to put it back on the head of the
9841 	 * waitq and then move from waitq to runq. Save this queuing
9842 	 * and call scsi_transport.
9843 	 */
9844 
9845 	mutex_exit(ST_MUTEX);
9846 
9847 	status = scsi_transport(BP_PKT(bp));
9848 
9849 	mutex_enter(ST_MUTEX);
9850 
9851 	if (status == TRAN_ACCEPT) {
9852 		un->un_tran_retry_ct = 0;
9853 		if (un->un_last_throttle) {
9854 			un->un_throttle = un->un_last_throttle;
9855 		}
9856 		mutex_exit(ST_MUTEX);
9857 
9858 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9859 		    "restart transport \n");
9860 		return (0);
9861 	}
9862 
9863 	ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
9864 	mutex_exit(ST_MUTEX);
9865 
9866 	if (status == TRAN_BUSY) {
9867 	    if (st_handle_intr_busy(un, bp,
9868 		ST_TRAN_BUSY_TIMEOUT) == 0)
9869 		return (0);
9870 	}
9871 	ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9872 		"restart transport rejected\n");
9873 	mutex_enter(ST_MUTEX);
9874 	ST_DO_ERRSTATS(un, st_transerrs);
9875 	if (un->un_last_throttle) {
9876 		un->un_throttle = un->un_last_throttle;
9877 	}
9878 exit:
9879 	mutex_exit(ST_MUTEX);
9880 	return (-1);
9881 }
9882 
9883 static int
9884 st_wrongtapetype(struct scsi_tape *un)
9885 {
9886 
9887 	ASSERT(mutex_owned(ST_MUTEX));
9888 
9889 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9890 		"st_wrongtapetype()\n");
9891 
9892 	/*
9893 	 * Hack to handle  600A, 600XTD, 6150 && 660 vs. 300XL tapes...
9894 	 */
9895 	if (un->un_dp && (un->un_dp->options & ST_QIC) && un->un_mspl) {
9896 		switch (un->un_dp->type) {
9897 		case ST_TYPE_WANGTEK:
9898 		case ST_TYPE_ARCHIVE:
9899 			/*
9900 			 * If this really worked, we could go off of
9901 			 * the density codes set in the modesense
9902 			 * page. For this drive, 0x10 == QIC-120,
9903 			 * 0xf == QIC-150, and 0x5 should be for
9904 			 * both QIC-24 and, maybe, QIC-11. However,
9905 			 * the h/w doesn't do what the manual says
9906 			 * that it should, so we'll key off of
9907 			 * getting a WRITE PROTECT error AND wp *not*
9908 			 * set in the mode sense information.
9909 			 */
9910 			/*
9911 			 * XXX but we already know that status is
9912 			 * write protect, so don't check it again.
9913 			 */
9914 
9915 			if (un->un_status == KEY_WRITE_PROTECT &&
9916 			    un->un_mspl->wp == 0) {
9917 				return (1);
9918 			}
9919 			break;
9920 		default:
9921 			break;
9922 		}
9923 	}
9924 	return (0);
9925 }
9926 
9927 static int
9928 st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt)
9929 {
9930 	int action;
9931 
9932 	ASSERT(mutex_owned(ST_MUTEX));
9933 
9934 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_error()\n");
9935 
9936 	if (SCBP_C(pkt) == STATUS_RESERVATION_CONFLICT) {
9937 		action = COMMAND_DONE_EACCES;
9938 		un->un_rsvd_status |= ST_RESERVATION_CONFLICT;
9939 	} else if (SCBP(pkt)->sts_busy) {
9940 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, "unit busy\n");
9941 		if ((int)un->un_retry_ct++ < st_retry_count) {
9942 			action = QUE_BUSY_COMMAND;
9943 		} else if ((un->un_rsvd_status &
9944 		    (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) {
9945 			/*
9946 			 * If this is a command done before reserve is done
9947 			 * don't reset.
9948 			 */
9949 			action = COMMAND_DONE_ERROR;
9950 		} else {
9951 			ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN,
9952 			    "unit busy too long\n");
9953 			mutex_exit(ST_MUTEX);
9954 			if (scsi_reset(ROUTE, RESET_TARGET) == 0) {
9955 				(void) scsi_reset(ROUTE, RESET_ALL);
9956 			}
9957 			mutex_enter(ST_MUTEX);
9958 			action = COMMAND_DONE_ERROR;
9959 		}
9960 	} else if (SCBP(pkt)->sts_chk) {
9961 		/*
9962 		 * we should only get here if the auto rqsense failed
9963 		 * thru a uscsi cmd without autorequest sense
9964 		 * so we just try again
9965 		 */
9966 		action = QUE_SENSE;
9967 	} else {
9968 		action = COMMAND_DONE;
9969 	}
9970 	return (action);
9971 }
9972 
9973 static void
9974 st_calc_bnum(struct scsi_tape *un, struct buf *bp)
9975 {
9976 	int n;
9977 
9978 	ASSERT(mutex_owned(ST_MUTEX));
9979 
9980 	if (un->un_bsize == 0) {
9981 		n = ((bp->b_bcount - bp->b_resid  == 0) ? 0 : 1);
9982 		un->un_kbytes_xferred += (bp->b_bcount - bp->b_resid)/1000;
9983 	} else {
9984 		n = ((bp->b_bcount - bp->b_resid) / un->un_bsize);
9985 	}
9986 	un->un_blkno += n;
9987 }
9988 
9989 static void
9990 st_set_state(struct scsi_tape *un)
9991 {
9992 	struct buf *bp = un->un_runqf;
9993 	struct scsi_pkt *sp = BP_PKT(bp);
9994 	struct uscsi_cmd *ucmd;
9995 
9996 	ASSERT(mutex_owned(ST_MUTEX));
9997 
9998 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9999 	    "st_set_state(): un_eof=%x	fmneeded=%x  pkt_resid=0x%lx (%ld)\n",
10000 	    un->un_eof, un->un_fmneeded, sp->pkt_resid, sp->pkt_resid);
10001 
10002 	if (bp != un->un_sbufp) {
10003 #ifdef STDEBUG
10004 		if (DEBUGGING && sp->pkt_resid) {
10005 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10006 			    "pkt_resid %ld bcount %ld\n",
10007 			    sp->pkt_resid, bp->b_bcount);
10008 		}
10009 #endif
10010 		bp->b_resid = sp->pkt_resid;
10011 		st_calc_bnum(un, bp);
10012 		if (bp->b_flags & B_READ) {
10013 			un->un_lastop = ST_OP_READ;
10014 			un->un_fmneeded = 0;
10015 		} else {
10016 			un->un_lastop = ST_OP_WRITE;
10017 			if (un->un_dp->options & ST_REEL) {
10018 				un->un_fmneeded = 2;
10019 			} else {
10020 				un->un_fmneeded = 1;
10021 			}
10022 		}
10023 		/*
10024 		 * all is honky dory at this point, so let's
10025 		 * readjust the throttle, to increase speed, if we
10026 		 * have not throttled down.
10027 		 */
10028 		if (un->un_throttle)
10029 			un->un_throttle = un->un_max_throttle;
10030 	} else {
10031 		char saved_lastop = un->un_lastop;
10032 		uchar_t cmd = (uchar_t)(intptr_t)bp->b_forw;
10033 
10034 		un->un_lastop = ST_OP_CTL;
10035 
10036 		switch (cmd) {
10037 		case SCMD_WRITE:
10038 			bp->b_resid = sp->pkt_resid;
10039 			un->un_lastop = ST_OP_WRITE;
10040 			st_calc_bnum(un, bp);
10041 			if (un->un_dp->options & ST_REEL) {
10042 				un->un_fmneeded = 2;
10043 			} else {
10044 				un->un_fmneeded = 1;
10045 			}
10046 			break;
10047 		case SCMD_READ:
10048 			bp->b_resid = sp->pkt_resid;
10049 			un->un_lastop = ST_OP_READ;
10050 			st_calc_bnum(un, bp);
10051 			un->un_fmneeded = 0;
10052 			break;
10053 		case SCMD_WRITE_FILE_MARK:
10054 			if (un->un_eof != ST_EOM)
10055 				un->un_eof = ST_NO_EOF;
10056 			un->un_lastop = ST_OP_WEOF;
10057 			un->un_fileno += (bp->b_bcount - bp->b_resid);
10058 			un->un_blkno = 0;
10059 			if (un->un_dp->options & ST_REEL) {
10060 				un->un_fmneeded -=
10061 					(bp->b_bcount - bp->b_resid);
10062 				if (un->un_fmneeded < 0) {
10063 					un->un_fmneeded = 0;
10064 				}
10065 			} else {
10066 				un->un_fmneeded = 0;
10067 			}
10068 
10069 			break;
10070 		case SCMD_REWIND:
10071 			un->un_eof = ST_NO_EOF;
10072 			un->un_fileno = 0;
10073 			un->un_blkno = 0;
10074 			break;
10075 
10076 		case SCMD_SPACE:
10077 		{
10078 			int space_fmk, count;
10079 			long resid;
10080 
10081 			count = (int)space_cnt(bp->b_bcount);
10082 			resid = (long)space_cnt(bp->b_resid);
10083 			space_fmk = ((bp->b_bcount) & (1<<24)) ? 1 : 0;
10084 
10085 
10086 			if (count >= 0) {
10087 				if (space_fmk) {
10088 					if (un->un_eof <= ST_EOF) {
10089 						un->un_eof = ST_NO_EOF;
10090 					}
10091 					un->un_fileno += (count - resid);
10092 					un->un_blkno = 0;
10093 				} else {
10094 					un->un_blkno += count - resid;
10095 				}
10096 			} else if (count < 0) {
10097 				if (space_fmk) {
10098 					un->un_fileno -=
10099 					    ((-count) - resid);
10100 					if (un->un_fileno < 0) {
10101 						un->un_fileno = 0;
10102 						un->un_blkno = 0;
10103 					} else {
10104 						un->un_blkno = INF;
10105 					}
10106 				} else {
10107 					if (un->un_eof >= ST_EOF_PENDING) {
10108 					/*
10109 					 * we stepped back into
10110 					 * a previous file; we are not
10111 					 * making an effort to pretend that
10112 					 * we are still in the current file
10113 					 * ie. logical == physical position
10114 					 * and leave it to st_ioctl to correct
10115 					 */
10116 						if (un->un_fileno > 0) {
10117 							un->un_fileno--;
10118 							un->un_blkno = INF;
10119 						} else {
10120 							un->un_blkno = 0;
10121 						}
10122 					} else {
10123 						un->un_blkno -=
10124 						    (-count) - resid;
10125 					}
10126 				}
10127 			}
10128 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10129 			    "aft_space rs %ld fil %d blk %ld\n",
10130 			    resid, un->un_fileno, un->un_blkno);
10131 			break;
10132 		}
10133 		case SCMD_LOAD:
10134 			if (bp->b_bcount & 0x1) {
10135 				un->un_fileno = 0;
10136 			} else {
10137 				un->un_state = ST_STATE_OFFLINE;
10138 				un->un_fileno = -1;
10139 			}
10140 			un->un_density_known = 0;
10141 			un->un_eof = ST_NO_EOF;
10142 			un->un_blkno = 0;
10143 			break;
10144 		case SCMD_ERASE:
10145 			un->un_eof = ST_NO_EOF;
10146 			un->un_blkno = 0;
10147 			un->un_fileno = 0;
10148 			break;
10149 		case SCMD_RESERVE:
10150 			un->un_rsvd_status |= ST_RESERVE;
10151 			un->un_rsvd_status &=
10152 			    ~(ST_RELEASE | ST_LOST_RESERVE |
10153 			    ST_RESERVATION_CONFLICT);
10154 			un->un_lastop = saved_lastop;
10155 			break;
10156 		case SCMD_RELEASE:
10157 			un->un_rsvd_status |= ST_RELEASE;
10158 			un->un_rsvd_status &=
10159 			    ~(ST_RESERVE | ST_LOST_RESERVE |
10160 			    ST_RESERVATION_CONFLICT);
10161 			un->un_lastop = saved_lastop;
10162 			break;
10163 		case SCMD_PERSISTENT_RESERVE_IN:
10164 			ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
10165 			    "PGR_IN command\n");
10166 			break;
10167 		case SCMD_PERSISTENT_RESERVE_OUT:
10168 			switch (sp->pkt_cdbp[1] & ST_SA_MASK) {
10169 			case ST_SA_SCSI3_RESERVE:
10170 			case ST_SA_SCSI3_PREEMPT:
10171 			case ST_SA_SCSI3_PREEMPTANDABORT:
10172 				un->un_rsvd_status |=
10173 				    ST_APPLICATION_RESERVATIONS;
10174 				ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
10175 				    "PGR Reserve and set: entering"
10176 				    " ST_APPLICATION_RESERVATIONS mode");
10177 				break;
10178 			case ST_SA_SCSI3_RELEASE:
10179 			case ST_SA_SCSI3_CLEAR:
10180 				un->un_rsvd_status &=
10181 				    ~ST_APPLICATION_RESERVATIONS;
10182 				ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
10183 				    "PGR Release and reset: exiting"
10184 				    " ST_APPLICATION_RESERVATIONS mode");
10185 				break;
10186 			}
10187 			break;
10188 		case SCMD_TEST_UNIT_READY:
10189 		case SCMD_READ_BLKLIM:
10190 		case SCMD_REQUEST_SENSE:
10191 		case SCMD_INQUIRY:
10192 		case SCMD_RECOVER_BUF:
10193 		case SCMD_MODE_SELECT:
10194 		case SCMD_MODE_SENSE:
10195 		case SCMD_DOORLOCK:
10196 		case SCMD_READ_POSITION:
10197 		case SCMD_READ_BUFFER:
10198 		case SCMD_REPORT_DENSITIES:
10199 		case SCMD_LOG_SELECT_G1:
10200 		case SCMD_LOG_SENSE_G1:
10201 		case SCMD_REPORT_LUNS:
10202 		case SCMD_READ_ATTRIBUTE:
10203 			un->un_lastop = saved_lastop;
10204 			break;
10205 		case SCMD_LOCATE:	/* Locate makes position unknown */
10206 		default:
10207 			/*
10208 			 * Unknown command, If was USCSI and USCSI_SILENT
10209 			 * flag was not set, set position to unknown.
10210 			 */
10211 			if ((((ucmd = BP_UCMD(bp)) != NULL) &&
10212 			    (ucmd->uscsi_flags & USCSI_SILENT) == 0)) {
10213 				ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN,
10214 				    "unknown cmd 0x%X caused loss of state\n",
10215 				    cmd);
10216 			} else {
10217 				break;
10218 			}
10219 			/* FALLTHROUGH */
10220 		case SCMD_WRITE_BUFFER: /* Writes new firmware to device */
10221 			un->un_fileno = -1;
10222 			break;
10223 		}
10224 	}
10225 
10226 	/*
10227 	 * In the st driver we have a logical and physical file position.
10228 	 * Under BSD behavior, when you get a zero read, the logical position
10229 	 * is before the filemark but after the last record of the file.
10230 	 * The physical position is after the filemark. MTIOCGET should always
10231 	 * return the logical file position.
10232 	 *
10233 	 * The next read gives a silent skip to the next file.
10234 	 * Under SVR4, the logical file position remains before the filemark
10235 	 * until the file is closed or a space operation is performed.
10236 	 * Hence set err_resid and err_file before changing fileno if case
10237 	 * BSD Behaviour.
10238 	 */
10239 	un->un_err_resid = bp->b_resid;
10240 	un->un_err_fileno = un->un_fileno;
10241 	un->un_err_blkno = un->un_blkno;
10242 	un->un_retry_ct = 0;
10243 
10244 
10245 	/*
10246 	 * If we've seen a filemark via the last read operation
10247 	 * advance the file counter, but mark things such that
10248 	 * the next read operation gets a zero count. We have
10249 	 * to put this here to handle the case of sitting right
10250 	 * at the end of a tape file having seen the file mark,
10251 	 * but the tape is closed and then re-opened without
10252 	 * any further i/o. That is, the position information
10253 	 * must be updated before a close.
10254 	 */
10255 
10256 	if (un->un_lastop == ST_OP_READ && un->un_eof == ST_EOF_PENDING) {
10257 		/*
10258 		 * If we're a 1/2" tape, and we get a filemark
10259 		 * right on block 0, *AND* we were not in the
10260 		 * first file on the tape, and we've hit logical EOM.
10261 		 * We'll mark the state so that later we do the
10262 		 * right thing (in st_close(), st_strategy() or
10263 		 * st_ioctl()).
10264 		 *
10265 		 */
10266 		if ((un->un_dp->options & ST_REEL) &&
10267 			!(un->un_dp->options & ST_READ_IGNORE_EOFS) &&
10268 		    un->un_blkno == 0 && un->un_fileno > 0) {
10269 			un->un_eof = ST_EOT_PENDING;
10270 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10271 			    "eot pending\n");
10272 			un->un_fileno++;
10273 			un->un_blkno = 0;
10274 		} else if (BSD_BEHAVIOR) {
10275 			/*
10276 			 * If the read of the filemark was a side effect
10277 			 * of reading some blocks (i.e., data was actually
10278 			 * read), then the EOF mark is pending and the
10279 			 * bump into the next file awaits the next read
10280 			 * operation (which will return a zero count), or
10281 			 * a close or a space operation, else the bump
10282 			 * into the next file occurs now.
10283 			 */
10284 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10285 			    "resid=%lx, bcount=%lx\n",
10286 				bp->b_resid, bp->b_bcount);
10287 			if (bp->b_resid != bp->b_bcount) {
10288 				un->un_eof = ST_EOF;
10289 			} else {
10290 				un->un_silent_skip = 1;
10291 				un->un_eof = ST_NO_EOF;
10292 				un->un_fileno++;
10293 				un->un_save_blkno = un->un_blkno;
10294 				un->un_blkno = 0;
10295 			}
10296 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10297 			    "eof of file %d, un_eof=%d\n",
10298 			    un->un_fileno, un->un_eof);
10299 		} else if (SVR4_BEHAVIOR) {
10300 			/*
10301 			 * If the read of the filemark was a side effect
10302 			 * of reading some blocks (i.e., data was actually
10303 			 * read), then the next read should return 0
10304 			 */
10305 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10306 			    "resid=%lx, bcount=%lx\n",
10307 			    bp->b_resid, bp->b_bcount);
10308 			if (bp->b_resid == bp->b_bcount) {
10309 				un->un_eof = ST_EOF;
10310 			}
10311 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10312 			    "eof of file=%d, un_eof=%d\n",
10313 			    un->un_fileno, un->un_eof);
10314 		}
10315 	}
10316 }
10317 
10318 /*
10319  * set the correct un_errno, to take corner cases into consideration
10320  */
10321 static void
10322 st_set_pe_errno(struct scsi_tape *un)
10323 {
10324 	ASSERT(mutex_owned(ST_MUTEX));
10325 
10326 	/* if errno is already set, don't reset it */
10327 	if (un->un_errno)
10328 		return;
10329 
10330 	/* here un_errno == 0 */
10331 	/*
10332 	 * if the last transfer before flushing all the
10333 	 * waiting I/O's, was 0 (resid = count), then we
10334 	 * want to give the user an error on all the rest,
10335 	 * so here.  If there was a transfer, we set the
10336 	 * resid and counts to 0, and let it drop through,
10337 	 * giving a zero return.  the next I/O will then
10338 	 * give an error.
10339 	 */
10340 	if (un->un_last_resid == un->un_last_count) {
10341 		switch (un->un_eof) {
10342 		case ST_EOM:
10343 			un->un_errno = ENOMEM;
10344 			break;
10345 		case ST_EOT:
10346 		case ST_EOF:
10347 			un->un_errno = EIO;
10348 			break;
10349 		}
10350 	} else {
10351 		/*
10352 		 * we know they did not have a zero, so make
10353 		 * sure they get one
10354 		 */
10355 		un->un_last_resid = un->un_last_count = 0;
10356 	}
10357 }
10358 
10359 
10360 /*
10361  * send in a marker pkt to terminate flushing of commands by BBA (via
10362  * flush-on-errors) property.  The HBA will always return TRAN_ACCEPT
10363  */
10364 static void
10365 st_hba_unflush(struct scsi_tape *un)
10366 {
10367 	ASSERT(mutex_owned(ST_MUTEX));
10368 
10369 	if (!un->un_flush_on_errors)
10370 		return;
10371 
10372 #ifdef FLUSH_ON_ERRORS
10373 
10374 	if (!un->un_mkr_pkt) {
10375 		un->un_mkr_pkt = scsi_init_pkt(ROUTE, NULL, (struct buf *)NULL,
10376 		    NULL, 0, 0, 0, SLEEP_FUNC, NULL);
10377 
10378 		/* we slept, so it must be there */
10379 		pkt->pkt_flags |= FLAG_FLUSH_MARKER;
10380 	}
10381 
10382 	mutex_exit(ST_MUTEX);
10383 	scsi_transport(un->un_mkr_pkt);
10384 	mutex_enter(ST_MUTEX);
10385 #endif
10386 }
10387 
10388 static void
10389 st_clean_print(dev_info_t *dev, char *label, uint_t level,
10390 	char *title, char *data, int len)
10391 {
10392 	int	i;
10393 	char	buf[256];
10394 
10395 	(void) sprintf(buf, "%s: ", title);
10396 	for (i = 0; i < len; i++) {
10397 		(void) sprintf(&buf[(int)strlen(buf)], "0x%x ",
10398 			(data[i] & 0xff));
10399 	}
10400 	(void) sprintf(&buf[(int)strlen(buf)], "\n");
10401 
10402 	scsi_log(dev, label, level, "%s", buf);
10403 }
10404 
10405 /*
10406  * Conditionally enabled debugging
10407  */
10408 #ifdef	STDEBUG
10409 static void
10410 st_debug_cmds(struct scsi_tape *un, int com, int count, int wait)
10411 {
10412 	char tmpbuf[64];
10413 
10414 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10415 	    "cmd=%s count=0x%x (%d)	 %ssync\n",
10416 	    scsi_cmd_name(com, scsi_cmds, tmpbuf),
10417 	    count, count,
10418 	    wait == ASYNC_CMD ? "a" : "");
10419 }
10420 
10421 /*
10422  * Returns pointer to name of minor node name of device 'dev'.
10423  */
10424 static char *
10425 st_dev_name(dev_t dev)
10426 {
10427 	const char density[] = { 'l', 'm', 'h', 'c' };
10428 	static char name[4];
10429 	minor_t minor;
10430 	int nprt = 0;
10431 
10432 	minor = getminor(dev);
10433 	name[nprt] = density[(minor & MT_DENSITY_MASK) >> 3];
10434 
10435 	if (minor & MT_BSD) {
10436 		name[++nprt] = 'b';
10437 	}
10438 
10439 	if (minor & MT_NOREWIND) {
10440 		name[++nprt] = 'n';
10441 	}
10442 
10443 	/* NULL terminator */
10444 	name[++nprt] = 0;
10445 
10446 	return (name);
10447 }
10448 #endif	/* STDEBUG */
10449 
10450 /*
10451  * Soft error reporting, so far unique to each drive
10452  *
10453  * Currently supported: exabyte and DAT soft error reporting
10454  */
10455 static int
10456 st_report_exabyte_soft_errors(dev_t dev, int flag)
10457 {
10458 	uchar_t *sensep;
10459 	int amt;
10460 	int rval = 0;
10461 	char cdb[CDB_GROUP0], *c = cdb;
10462 	struct uscsi_cmd *com;
10463 
10464 	GET_SOFT_STATE(dev);
10465 
10466 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10467 	    "st_report_exabyte_soft_errors(dev = 0x%lx, flag = %d)\n",
10468 	    dev, flag);
10469 
10470 	ASSERT(mutex_owned(ST_MUTEX));
10471 
10472 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
10473 	sensep = kmem_zalloc(TAPE_SENSE_LENGTH, KM_SLEEP);
10474 
10475 	*c++ = SCMD_REQUEST_SENSE;
10476 	*c++ = 0;
10477 	*c++ = 0;
10478 	*c++ = 0;
10479 	*c++ = TAPE_SENSE_LENGTH;
10480 	/*
10481 	 * set CLRCNT (byte 5, bit 7 which clears the error counts)
10482 	 */
10483 	*c   = (char)0x80;
10484 
10485 	com->uscsi_cdb = cdb;
10486 	com->uscsi_cdblen = CDB_GROUP0;
10487 	com->uscsi_bufaddr = (caddr_t)sensep;
10488 	com->uscsi_buflen = TAPE_SENSE_LENGTH;
10489 	com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT
10490 		| USCSI_READ | USCSI_RQENABLE;
10491 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
10492 
10493 	rval = st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
10494 		UIO_SYSSPACE);
10495 	if (rval || com->uscsi_status) {
10496 		goto done;
10497 	}
10498 
10499 	/*
10500 	 * was there enough data?
10501 	 */
10502 	amt = (int)TAPE_SENSE_LENGTH - com->uscsi_resid;
10503 
10504 	if ((amt >= 19) && un->un_kbytes_xferred) {
10505 		uint_t count, error_rate;
10506 		uint_t rate;
10507 
10508 		if (sensep[21] & CLN) {
10509 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
10510 			    "Periodic head cleaning required");
10511 		}
10512 		if (un->un_kbytes_xferred < (EXABYTE_MIN_TRANSFER/1000))
10513 			goto done;
10514 		/*
10515 		 * check if soft error reporting needs to be done.
10516 		 */
10517 		count = sensep[16] << 16 | sensep[17] << 8 | sensep[18];
10518 		count &= 0xffffff;
10519 		error_rate = (count * 100)/un->un_kbytes_xferred;
10520 
10521 #ifdef	STDEBUG
10522 		if (st_soft_error_report_debug) {
10523 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
10524 			    "Exabyte Soft Error Report:\n");
10525 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
10526 			    "read/write error counter: %d\n", count);
10527 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
10528 			    "number of bytes transferred: %dK\n",
10529 				un->un_kbytes_xferred);
10530 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
10531 			    "error_rate: %d%%\n", error_rate);
10532 
10533 			if (amt >= 22) {
10534 				scsi_log(ST_DEVINFO, st_label, CE_CONT,
10535 				    "unit sense: 0x%b 0x%b 0x%b\n",
10536 				    sensep[19], SENSE_19_BITS,
10537 				    sensep[20], SENSE_20_BITS,
10538 				    sensep[21], SENSE_21_BITS);
10539 			}
10540 			if (amt >= 27) {
10541 				scsi_log(ST_DEVINFO, st_label, CE_CONT,
10542 				    "tracking retry counter: %d\n",
10543 				    sensep[26]);
10544 				scsi_log(ST_DEVINFO, st_label, CE_CONT,
10545 				    "read/write retry counter: %d\n",
10546 				    sensep[27]);
10547 			}
10548 		}
10549 #endif
10550 
10551 		if (flag & FWRITE) {
10552 			rate = EXABYTE_WRITE_ERROR_THRESHOLD;
10553 		} else {
10554 			rate = EXABYTE_READ_ERROR_THRESHOLD;
10555 		}
10556 		if (error_rate >= rate) {
10557 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
10558 			    "Soft error rate (%d%%) during %s was too high",
10559 			    error_rate,
10560 			    ((flag & FWRITE) ? wrg_str : rdg_str));
10561 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
10562 			    "Please, replace tape cartridge\n");
10563 		}
10564 	}
10565 
10566 done:
10567 	kmem_free(com, sizeof (*com));
10568 	kmem_free(sensep, TAPE_SENSE_LENGTH);
10569 
10570 	if (rval != 0) {
10571 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
10572 		    "exabyte soft error reporting failed\n");
10573 	}
10574 	return (rval);
10575 }
10576 
10577 /*
10578  * this is very specific to Archive 4mm dat
10579  */
10580 #define	ONEGIG	(1024 * 1024 * 1024)
10581 
10582 static int
10583 st_report_dat_soft_errors(dev_t dev, int flag)
10584 {
10585 	uchar_t *sensep;
10586 	int amt, i;
10587 	int rval = 0;
10588 	char cdb[CDB_GROUP1], *c = cdb;
10589 	struct uscsi_cmd *com;
10590 
10591 	GET_SOFT_STATE(dev);
10592 
10593 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10594 	    "st_report_dat_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag);
10595 
10596 	ASSERT(mutex_owned(ST_MUTEX));
10597 
10598 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
10599 	sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP);
10600 
10601 	*c++ = SCMD_LOG_SENSE_G1;
10602 	*c++ = 0;
10603 	*c++ = (flag & FWRITE) ? 0x42 : 0x43;
10604 	*c++ = 0;
10605 	*c++ = 0;
10606 	*c++ = 0;
10607 	*c++ = 2;
10608 	*c++ = 0;
10609 	*c++ = (char)LOG_SENSE_LENGTH;
10610 	*c   = 0;
10611 	com->uscsi_cdb    = cdb;
10612 	com->uscsi_cdblen  = CDB_GROUP1;
10613 	com->uscsi_bufaddr = (caddr_t)sensep;
10614 	com->uscsi_buflen  = LOG_SENSE_LENGTH;
10615 	com->uscsi_flags   =
10616 	    USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
10617 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
10618 	rval =
10619 	    st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
10620 	if (rval) {
10621 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
10622 		    "DAT soft error reporting failed\n");
10623 	}
10624 	if (rval || com->uscsi_status) {
10625 		goto done;
10626 	}
10627 
10628 	/*
10629 	 * was there enough data?
10630 	 */
10631 	amt = (int)LOG_SENSE_LENGTH - com->uscsi_resid;
10632 
10633 	if ((amt >= MIN_LOG_SENSE_LENGTH) && un->un_kbytes_xferred) {
10634 		int total, retries, param_code;
10635 
10636 		total = -1;
10637 		retries = -1;
10638 		amt = sensep[3] + 4;
10639 
10640 
10641 #ifdef STDEBUG
10642 		if (st_soft_error_report_debug) {
10643 			(void) printf("logsense:");
10644 			for (i = 0; i < MIN_LOG_SENSE_LENGTH; i++) {
10645 				if (i % 16 == 0) {
10646 					(void) printf("\t\n");
10647 				}
10648 				(void) printf(" %x", sensep[i]);
10649 			}
10650 			(void) printf("\n");
10651 		}
10652 #endif
10653 
10654 		/*
10655 		 * parse the param_codes
10656 		 */
10657 		if (sensep[0] == 2 || sensep[0] == 3) {
10658 			for (i = 4; i < amt; i++) {
10659 				param_code = (sensep[i++] << 8);
10660 				param_code += sensep[i++];
10661 				i++; /* skip control byte */
10662 				if (param_code == 5) {
10663 					if (sensep[i++] == 4) {
10664 						total = (sensep[i++] << 24);
10665 						total += (sensep[i++] << 16);
10666 						total += (sensep[i++] << 8);
10667 						total += sensep[i];
10668 					}
10669 				} else if (param_code == 0x8007) {
10670 					if (sensep[i++] == 2) {
10671 						retries = sensep[i++] << 8;
10672 						retries += sensep[i];
10673 					}
10674 				} else {
10675 					i += sensep[i];
10676 				}
10677 			}
10678 		}
10679 
10680 		/*
10681 		 * if the log sense returned valid numbers then determine
10682 		 * the read and write error thresholds based on the amount of
10683 		 * data transferred
10684 		 */
10685 
10686 		if (total > 0 && retries > 0) {
10687 			short normal_retries = 0;
10688 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
10689 			"total xferred (%s) =%x, retries=%x\n",
10690 				((flag & FWRITE) ? wrg_str : rdg_str),
10691 				total, retries);
10692 
10693 			if (flag & FWRITE) {
10694 				if (total <=
10695 					WRITE_SOFT_ERROR_WARNING_THRESHOLD) {
10696 					normal_retries =
10697 						DAT_SMALL_WRITE_ERROR_THRESHOLD;
10698 				} else {
10699 					normal_retries =
10700 						DAT_LARGE_WRITE_ERROR_THRESHOLD;
10701 				}
10702 			} else {
10703 				if (total <=
10704 					READ_SOFT_ERROR_WARNING_THRESHOLD) {
10705 					normal_retries =
10706 						DAT_SMALL_READ_ERROR_THRESHOLD;
10707 				} else {
10708 					normal_retries =
10709 						DAT_LARGE_READ_ERROR_THRESHOLD;
10710 				}
10711 			}
10712 
10713 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
10714 			"normal retries=%d\n", normal_retries);
10715 
10716 			if (retries >= normal_retries) {
10717 				scsi_log(ST_DEVINFO, st_label, CE_WARN,
10718 				    "Soft error rate (retries = %d) during "
10719 				    "%s was too high",  retries,
10720 				    ((flag & FWRITE) ? wrg_str : rdg_str));
10721 				scsi_log(ST_DEVINFO, st_label, CE_CONT,
10722 				    "Periodic head cleaning required "
10723 				    "and/or replace tape cartridge\n");
10724 			}
10725 
10726 		} else if (total == -1 || retries == -1) {
10727 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
10728 			    "log sense parameter code does not make sense\n");
10729 		}
10730 	}
10731 
10732 	/*
10733 	 * reset all values
10734 	 */
10735 	c = cdb;
10736 	*c++ = SCMD_LOG_SELECT_G1;
10737 	*c++ = 2;	/* this resets all values */
10738 	*c++ = (char)0xc0;
10739 	*c++ = 0;
10740 	*c++ = 0;
10741 	*c++ = 0;
10742 	*c++ = 0;
10743 	*c++ = 0;
10744 	*c++ = 0;
10745 	*c   = 0;
10746 	com->uscsi_bufaddr = NULL;
10747 	com->uscsi_buflen  = 0;
10748 	com->uscsi_flags   = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_RQENABLE;
10749 	rval =
10750 	    st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
10751 	if (rval) {
10752 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
10753 		    "DAT soft error reset failed\n");
10754 	}
10755 done:
10756 	kmem_free(com, sizeof (*com));
10757 	kmem_free(sensep, LOG_SENSE_LENGTH);
10758 	return (rval);
10759 }
10760 
10761 static int
10762 st_report_soft_errors(dev_t dev, int flag)
10763 {
10764 	GET_SOFT_STATE(dev);
10765 
10766 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10767 	    "st_report_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag);
10768 
10769 	ASSERT(mutex_owned(ST_MUTEX));
10770 
10771 	switch (un->un_dp->type) {
10772 	case ST_TYPE_EXB8500:
10773 	case ST_TYPE_EXABYTE:
10774 		return (st_report_exabyte_soft_errors(dev, flag));
10775 		/*NOTREACHED*/
10776 	case ST_TYPE_PYTHON:
10777 		return (st_report_dat_soft_errors(dev, flag));
10778 		/*NOTREACHED*/
10779 	default:
10780 		un->un_dp->options &= ~ST_SOFT_ERROR_REPORTING;
10781 		return (-1);
10782 	}
10783 }
10784 
10785 /*
10786  * persistent error routines
10787  */
10788 
10789 /*
10790  * enable persistent errors, and set the throttle appropriately, checking
10791  * for flush-on-errors capability
10792  */
10793 static void
10794 st_turn_pe_on(struct scsi_tape *un)
10795 {
10796 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_on\n");
10797 	ASSERT(mutex_owned(ST_MUTEX));
10798 
10799 	un->un_persistence = 1;
10800 
10801 	/*
10802 	 * only use flush-on-errors if auto-request-sense and untagged-qing are
10803 	 * enabled.  This will simplify the error handling for request senses
10804 	 */
10805 
10806 	if (un->un_arq_enabled && un->un_untagged_qing) {
10807 		uchar_t f_o_e;
10808 
10809 		mutex_exit(ST_MUTEX);
10810 		f_o_e = (scsi_ifsetcap(ROUTE, "flush-on-errors", 1, 1) == 1) ?
10811 		    1 : 0;
10812 		mutex_enter(ST_MUTEX);
10813 
10814 		un->un_flush_on_errors = f_o_e;
10815 	} else {
10816 		un->un_flush_on_errors = 0;
10817 	}
10818 
10819 	if (un->un_flush_on_errors)
10820 		un->un_max_throttle = (uchar_t)st_max_throttle;
10821 	else
10822 		un->un_max_throttle = 1;
10823 
10824 	if (un->un_dp->options & ST_RETRY_ON_RECOVERED_DEFERRED_ERROR)
10825 		un->un_max_throttle = 1;
10826 
10827 	/* this will send a marker pkt */
10828 	CLEAR_PE(un);
10829 }
10830 
10831 /*
10832  * This turns persistent errors permanently off
10833  */
10834 static void
10835 st_turn_pe_off(struct scsi_tape *un)
10836 {
10837 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_off\n");
10838 	ASSERT(mutex_owned(ST_MUTEX));
10839 
10840 	/* turn it off for good */
10841 	un->un_persistence = 0;
10842 
10843 	/* this will send a marker pkt */
10844 	CLEAR_PE(un);
10845 
10846 	/* turn off flush on error capability, if enabled */
10847 	if (un->un_flush_on_errors) {
10848 		mutex_exit(ST_MUTEX);
10849 		(void) scsi_ifsetcap(ROUTE, "flush-on-errors", 0, 1);
10850 		mutex_enter(ST_MUTEX);
10851 	}
10852 
10853 
10854 	un->un_flush_on_errors = 0;
10855 }
10856 
10857 /*
10858  * This clear persistent errors, allowing more commands through, and also
10859  * sending a marker packet.
10860  */
10861 static void
10862 st_clear_pe(struct scsi_tape *un)
10863 {
10864 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_clear\n");
10865 	ASSERT(mutex_owned(ST_MUTEX));
10866 
10867 	un->un_persist_errors = 0;
10868 	un->un_throttle = un->un_last_throttle = 1;
10869 	un->un_errno = 0;
10870 	st_hba_unflush(un);
10871 }
10872 
10873 /*
10874  * This will flag persistent errors, shutting everything down, if the
10875  * application had enabled persistent errors via MTIOCPERSISTENT
10876  */
10877 static void
10878 st_set_pe_flag(struct scsi_tape *un)
10879 {
10880 	ASSERT(mutex_owned(ST_MUTEX));
10881 
10882 	if (un->un_persistence) {
10883 		ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_flag\n");
10884 		un->un_persist_errors = 1;
10885 		un->un_throttle = un->un_last_throttle = 0;
10886 	}
10887 }
10888 
10889 /*
10890  * List of commands that are allowed to be done while another host holds
10891  * the reservation.
10892  */
10893 struct {
10894 	uchar_t cmd;
10895 	uchar_t byte;	/* byte to look for data */
10896 	uint32_t mask;	/* bits that matter in the above data */
10897 } rcmds[] = {
10898 	{ SCMD_TEST_UNIT_READY, 0, 0 }, /* may fail on older drives */
10899 	{ SCMD_REQUEST_SENSE, 0, 0 },
10900 	{ SCMD_READ_BLKLIM, 0, 0 },
10901 	{ SCMD_INQUIRY, 0, 0 },
10902 	{ SCMD_RESERVE, 0, 0 },
10903 	{ SCMD_RELEASE, 0, 0 },
10904 	{ SCMD_DOORLOCK, 4, 3 },	/* allow (unlock) media access only */
10905 	{ SCMD_REPORT_DENSITIES, 0, 0 },
10906 	{ SCMD_LOG_SENSE_G1, 0, 0 },
10907 	{ SCMD_PERSISTENT_RESERVE_IN, 0, 0 },
10908 	{ SCMD_PERSISTENT_RESERVE_OUT, 0, 0 },
10909 	{ SCMD_REPORT_LUNS, 0, 0 }
10910 };
10911 
10912 static int
10913 st_do_reserve(struct scsi_tape *un)
10914 {
10915 	int rval;
10916 
10917 	/*
10918 	 * Issue a Throw-Away reserve command to clear the
10919 	 * check condition.
10920 	 * If the current behaviour of reserve/release is to
10921 	 * hold reservation across opens , and if a Bus reset
10922 	 * has been issued between opens then this command
10923 	 * would set the ST_LOST_RESERVE flags in rsvd_status.
10924 	 * In this case return an EACCES so that user knows that
10925 	 * reservation has been lost in between opens.
10926 	 * If this error is not returned and we continue with
10927 	 * successful open , then user may think position of the
10928 	 * tape is still the same but inreality we would rewind the
10929 	 * tape and continue from BOT.
10930 	 */
10931 	rval = st_reserve_release(un, ST_RESERVE);
10932 	if (rval) {
10933 		if ((un->un_rsvd_status & ST_LOST_RESERVE_BETWEEN_OPENS) ==
10934 		    ST_LOST_RESERVE_BETWEEN_OPENS) {
10935 			un->un_rsvd_status &= ~(ST_LOST_RESERVE | ST_RESERVE);
10936 			un->un_errno = EACCES;
10937 			return (EACCES);
10938 		}
10939 		rval = st_reserve_release(un, ST_RESERVE);
10940 	}
10941 	if (rval == 0) {
10942 		un->un_rsvd_status |= ST_INIT_RESERVE;
10943 	}
10944 
10945 	return (rval);
10946 }
10947 
10948 static int
10949 st_check_cdb_for_need_to_reserve(struct scsi_tape *un, caddr_t cdb)
10950 {
10951 	int i;
10952 	int rval = 0;
10953 
10954 	/*
10955 	 * If already reserved no need to do it again.
10956 	 * Also if Reserve and Release are disabled Just return.
10957 	 */
10958 	if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) ||
10959 	    (un->un_dp->options & ST_NO_RESERVE_RELEASE)) {
10960 		ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
10961 		    "st_check_cdb_for_need_to_reserve() reserve unneeded 0x%x",
10962 		    cdb[0]);
10963 		return (0);
10964 	}
10965 
10966 	/* See if command is on the list */
10967 	for (i = 0; i < ST_NUM_MEMBERS(rcmds); i++) {
10968 		if ((uchar_t)cdb[0] == rcmds[i].cmd) {
10969 			/*
10970 			 * cmd is on list.
10971 			 * if byte is zero always allowed.
10972 			 */
10973 			if (rcmds[i].byte == 0) {
10974 				return (rval);
10975 			}
10976 			if (((cdb[rcmds[i].byte]) & (rcmds[i].mask)) == 0) {
10977 				return (rval);
10978 			}
10979 			break;
10980 		}
10981 	}
10982 
10983 	ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
10984 	    "Command 0x%x requires reservation", cdb[0]);
10985 
10986 	rval = st_do_reserve(un);
10987 
10988 	return (rval);
10989 }
10990 
10991 static int
10992 st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, int cnt)
10993 {
10994 	int i;
10995 	int rval = 0;
10996 
10997 	if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) ||
10998 	    (un->un_dp->options & ST_NO_RESERVE_RELEASE)) {
10999 		ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
11000 		    "st_check_cmd_for_need_to_reserve() reserve unneeded 0x%x",
11001 		    cmd);
11002 		return (0);
11003 	}
11004 
11005 	/* See if command is on the list */
11006 	for (i = 0; i < ST_NUM_MEMBERS(rcmds); i++) {
11007 		if (cmd == rcmds[i].cmd) {
11008 			/*
11009 			 * cmd is on list.
11010 			 * if byte is zero always allowed.
11011 			 */
11012 			if (rcmds[i].byte == 0) {
11013 				return (rval);
11014 			}
11015 			if (((rcmds[i].mask) & cnt) == 0) {
11016 				return (rval);
11017 			}
11018 			break;
11019 		}
11020 	}
11021 
11022 	ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
11023 	    "Cmd 0x%x requires reservation", cmd);
11024 
11025 	rval = st_do_reserve(un);
11026 
11027 	return (rval);
11028 }
11029 
11030 static int
11031 st_reserve_release(struct scsi_tape *un, int cmd)
11032 {
11033 	struct uscsi_cmd	uscsi_cmd;
11034 	struct uscsi_cmd	*com = &uscsi_cmd;
11035 	int			rval;
11036 	char			cdb[CDB_GROUP0];
11037 
11038 
11039 	ASSERT(mutex_owned(ST_MUTEX));
11040 
11041 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
11042 	    "st_reserve_release: %s \n",
11043 	    (cmd == ST_RELEASE)?  "Releasing":"Reserving");
11044 
11045 	bzero(cdb, CDB_GROUP0);
11046 	if (cmd == ST_RELEASE) {
11047 		cdb[0] = SCMD_RELEASE;
11048 	} else {
11049 		cdb[0] = SCMD_RESERVE;
11050 	}
11051 	bzero(com, sizeof (struct uscsi_cmd));
11052 	com->uscsi_flags = USCSI_WRITE;
11053 	com->uscsi_cdb = cdb;
11054 	com->uscsi_cdblen = CDB_GROUP0;
11055 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
11056 
11057 	rval = st_ioctl_cmd(un->un_dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
11058 	    UIO_SYSSPACE);
11059 
11060 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
11061 	    "st_reserve_release: rval(1)=%d\n", rval);
11062 
11063 	if (rval) {
11064 		if (com->uscsi_status == STATUS_RESERVATION_CONFLICT) {
11065 			rval = EACCES;
11066 		}
11067 		/*
11068 		 * dynamically turn off reserve/release support
11069 		 * in case of drives which do not support
11070 		 * reserve/release command(ATAPI drives).
11071 		 */
11072 		if (un->un_status == KEY_ILLEGAL_REQUEST) {
11073 			if (un->un_dp->options & ST_NO_RESERVE_RELEASE) {
11074 				un->un_dp->options |= ST_NO_RESERVE_RELEASE;
11075 				ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
11076 				    "Tape unit does not support "
11077 				    "reserve/release \n");
11078 			}
11079 			rval = 0;
11080 		}
11081 	}
11082 	return (rval);
11083 }
11084 
11085 static int
11086 st_take_ownership(dev_t dev)
11087 {
11088 	int rval;
11089 
11090 	GET_SOFT_STATE(dev);
11091 	ASSERT(mutex_owned(ST_MUTEX));
11092 
11093 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
11094 		"st_take_ownership: Entering ...\n");
11095 
11096 
11097 	rval = st_reserve_release(un, ST_RESERVE);
11098 	/*
11099 	 * XXX -> Should reset be done only if we get EACCES.
11100 	 * .
11101 	 */
11102 	if (rval) {
11103 		mutex_exit(ST_MUTEX);
11104 		if (scsi_reset(ROUTE, RESET_TARGET) == 0) {
11105 			if (scsi_reset(ROUTE, RESET_ALL) == 0) {
11106 				mutex_enter(ST_MUTEX);
11107 				return (EIO);
11108 			}
11109 		}
11110 		mutex_enter(ST_MUTEX);
11111 		un->un_rsvd_status &=
11112 			~(ST_LOST_RESERVE | ST_RESERVATION_CONFLICT);
11113 
11114 		mutex_exit(ST_MUTEX);
11115 		delay(drv_usectohz(ST_RESERVATION_DELAY));
11116 		mutex_enter(ST_MUTEX);
11117 		/*
11118 		 * remove the check condition.
11119 		 */
11120 		(void) st_reserve_release(un, ST_RESERVE);
11121 		if ((rval = st_reserve_release(un, ST_RESERVE)) != 0) {
11122 			if ((st_reserve_release(un, ST_RESERVE)) != 0) {
11123 				rval = (un->un_rsvd_status &
11124 					ST_RESERVATION_CONFLICT) ? EACCES : EIO;
11125 				return (rval);
11126 			}
11127 		}
11128 		/*
11129 		 * Set tape state to ST_STATE_OFFLINE , in case if
11130 		 * the user wants to continue and start using
11131 		 * the tape.
11132 		 */
11133 		un->un_state = ST_STATE_OFFLINE;
11134 		un->un_rsvd_status |= ST_INIT_RESERVE;
11135 	}
11136 	return (rval);
11137 }
11138 
11139 static int
11140 st_create_errstats(struct scsi_tape *un, int instance)
11141 {
11142 	char	kstatname[KSTAT_STRLEN];
11143 
11144 	/*
11145 	 * Create device error kstats
11146 	 */
11147 
11148 	if (un->un_errstats == (kstat_t *)0) {
11149 		(void) sprintf(kstatname, "st%d,err", instance);
11150 		un->un_errstats = kstat_create("sterr", instance, kstatname,
11151 			"device_error", KSTAT_TYPE_NAMED,
11152 			sizeof (struct st_errstats) / sizeof (kstat_named_t),
11153 			KSTAT_FLAG_PERSISTENT);
11154 
11155 		if (un->un_errstats) {
11156 			struct st_errstats	*stp;
11157 
11158 			stp = (struct st_errstats *)un->un_errstats->ks_data;
11159 			kstat_named_init(&stp->st_softerrs, "Soft Errors",
11160 				KSTAT_DATA_ULONG);
11161 			kstat_named_init(&stp->st_harderrs, "Hard Errors",
11162 				KSTAT_DATA_ULONG);
11163 			kstat_named_init(&stp->st_transerrs, "Transport Errors",
11164 				KSTAT_DATA_ULONG);
11165 			kstat_named_init(&stp->st_vid, "Vendor",
11166 				KSTAT_DATA_CHAR);
11167 			kstat_named_init(&stp->st_pid, "Product",
11168 				KSTAT_DATA_CHAR);
11169 			kstat_named_init(&stp->st_revision, "Revision",
11170 				KSTAT_DATA_CHAR);
11171 			kstat_named_init(&stp->st_serial, "Serial No",
11172 				KSTAT_DATA_CHAR);
11173 			un->un_errstats->ks_private = un;
11174 			un->un_errstats->ks_update = nulldev;
11175 			kstat_install(un->un_errstats);
11176 			/*
11177 			 * Fill in the static data
11178 			 */
11179 			(void) strncpy(&stp->st_vid.value.c[0],
11180 					ST_INQUIRY->inq_vid, 8);
11181 			/*
11182 			 * XXX:  Emulex MT-02 (and emulators) predates
11183 			 *	 SCSI-1 and has no vid & pid inquiry data.
11184 			 */
11185 			if (ST_INQUIRY->inq_len != 0) {
11186 				(void) strncpy(&stp->st_pid.value.c[0],
11187 					ST_INQUIRY->inq_pid, 16);
11188 				(void) strncpy(&stp->st_revision.value.c[0],
11189 					ST_INQUIRY->inq_revision, 4);
11190 				(void) strncpy(&stp->st_serial.value.c[0],
11191 					ST_INQUIRY->inq_serial, 12);
11192 			}
11193 		}
11194 	}
11195 	return (0);
11196 }
11197 
11198 static int
11199 st_validate_tapemarks(struct scsi_tape *un, int fileno, daddr_t blkno)
11200 {
11201 	dev_t dev;
11202 	int rval;
11203 
11204 	ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex));
11205 	ASSERT(mutex_owned(ST_MUTEX));
11206 
11207 	dev = un->un_dev;
11208 
11209 	scsi_log(ST_DEVINFO, st_label, CE_NOTE, "Restoring tape"
11210 	" position at fileno=%x, blkno=%lx....", fileno, blkno);
11211 
11212 	/*
11213 	 * Rewind ? Oh yeah, Fidelity has got the STK F/W changed
11214 	 * so as not to rewind tape on RESETS: Gee, Has life ever
11215 	 * been simple in tape land ?
11216 	 */
11217 	rval = st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD);
11218 	if (rval) {
11219 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
11220 		"Failed to restore the last file and block position: In"
11221 		" this state, Tape will be loaded at BOT during next open");
11222 		un->un_fileno = -1;
11223 		return (rval);
11224 	}
11225 
11226 	if (fileno) {
11227 		rval = st_cmd(dev, SCMD_SPACE, Fmk(fileno), SYNC_CMD);
11228 		if (rval) {
11229 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
11230 			"Failed to restore the last file position: In this "
11231 			" state, Tape will be loaded at BOT during next open");
11232 			un->un_fileno = -1;
11233 			return (rval);
11234 		}
11235 	}
11236 
11237 	if (blkno) {
11238 		rval = st_cmd(dev, SCMD_SPACE, Blk(blkno), SYNC_CMD);
11239 		if (rval) {
11240 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
11241 			"Failed to restore the last block position: In this"
11242 			" state, tape will be loaded at BOT during next open");
11243 			un->un_fileno = -1;
11244 			return (rval);
11245 		}
11246 	}
11247 
11248 	return (0);
11249 }
11250 
11251 /*
11252  * check sense key, ASC, ASCQ in order to determine if the tape needs
11253  * to be ejected
11254  */
11255 
11256 static int
11257 st_check_asc_ascq(struct scsi_tape *un)
11258 {
11259 	struct scsi_extended_sense *sensep = ST_RQSENSE;
11260 	struct tape_failure_code   *code;
11261 
11262 	for (code = st_tape_failure_code; code->key != 0xff; code++) {
11263 		if ((code->key  == sensep->es_key) &&
11264 		    (code->add_code  == sensep->es_add_code) &&
11265 		    (code->qual_code == sensep->es_qual_code))
11266 			return (1);
11267 	}
11268 	return (0);
11269 }
11270 
11271 /*
11272  * st_logpage_supported() sends a Log Sense command with
11273  * page code = 0 = Supported Log Pages Page to the device,
11274  * to see whether the page 'page' is supported.
11275  * Return values are:
11276  * -1 if the Log Sense command fails
11277  * 0 if page is not supported
11278  * 1 if page is supported
11279  */
11280 
11281 static int
11282 st_logpage_supported(dev_t dev, uchar_t page)
11283 {
11284 	uchar_t *sp, *sensep;
11285 	unsigned length;
11286 	struct uscsi_cmd *com;
11287 	int rval;
11288 	char cdb[CDB_GROUP1] = {
11289 		SCMD_LOG_SENSE_G1,
11290 		0,
11291 		SUPPORTED_LOG_PAGES_PAGE,
11292 		0,
11293 		0,
11294 		0,
11295 		0,
11296 		0,
11297 		(char)LOG_SENSE_LENGTH,
11298 		0
11299 	};
11300 
11301 	GET_SOFT_STATE(dev);
11302 	ASSERT(mutex_owned(ST_MUTEX));
11303 
11304 	com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11305 	sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP);
11306 
11307 	com->uscsi_cdb = cdb;
11308 	com->uscsi_cdblen = CDB_GROUP1;
11309 	com->uscsi_bufaddr = (caddr_t)sensep;
11310 	com->uscsi_buflen = LOG_SENSE_LENGTH;
11311 	com->uscsi_flags =
11312 		USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
11313 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
11314 	rval = st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
11315 	if (rval || com->uscsi_status) {
11316 		/* uscsi-command failed */
11317 		rval = -1;
11318 	} else {
11319 
11320 		sp = sensep + 3;
11321 
11322 		for (length = *sp++; length > 0; length--, sp++) {
11323 
11324 			if (*sp == page) {
11325 				rval = 1;
11326 				break;
11327 			}
11328 		}
11329 	}
11330 	kmem_free(com, sizeof (struct uscsi_cmd));
11331 	kmem_free(sensep, LOG_SENSE_LENGTH);
11332 	return (rval);
11333 }
11334 
11335 
11336 /*
11337  * st_check_clean_bit() gets the status of the tape's cleaning bit.
11338  *
11339  * If the device does support the TapeAlert log page, then the cleaning bit
11340  * information will be read from this page. Otherwise we will see if one of
11341  * ST_CLN_TYPE_1, ST_CLN_TYPE_2 or ST_CLN_TYPE_3 is set in the properties of
11342  * the device, which means, that we can get the cleaning bit information via
11343  * a RequestSense command.
11344  * If both methods of getting cleaning bit information are not supported
11345  * st_check_clean_bit() will return with 0. Otherwise st_check_clean_bit()
11346  * returns with
11347  * - MTF_TAPE_CLN_SUPPORTED if cleaning bit is not set or
11348  * - MTF_TAPE_CLN_SUPPORTED | MTF_TAPE_HEAD_DIRTY if cleaning bit is set.
11349  * If the call to st_ioctl_cmd() to do the Log Sense or the Request Sense
11350  * command fails, or if the amount of Request Sense data is not enough, then
11351  *  st_check_clean_bit() returns with -1.
11352  */
11353 
11354 static int
11355 st_check_clean_bit(dev_t dev)
11356 {
11357 	int rval = 0;
11358 
11359 	GET_SOFT_STATE(dev);
11360 
11361 	ASSERT(mutex_owned(ST_MUTEX));
11362 
11363 	if (un->un_HeadClean & TAPE_ALERT_NOT_SUPPORTED) {
11364 		return (rval);
11365 	}
11366 
11367 	if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) {
11368 
11369 		rval = st_logpage_supported(dev, TAPE_SEQUENTIAL_PAGE);
11370 		if (rval == 1) {
11371 
11372 			un->un_HeadClean |= TAPE_SEQUENTIAL_SUPPORTED;
11373 		}
11374 
11375 		rval = st_logpage_supported(dev, TAPE_ALERT_PAGE);
11376 		if (rval == 1) {
11377 
11378 			un->un_HeadClean |= TAPE_ALERT_SUPPORTED;
11379 		}
11380 
11381 		if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) {
11382 
11383 			un->un_HeadClean = TAPE_ALERT_NOT_SUPPORTED;
11384 		}
11385 	}
11386 
11387 	rval = 0;
11388 
11389 	if (un->un_HeadClean & TAPE_SEQUENTIAL_SUPPORTED) {
11390 
11391 		rval = st_check_sequential_clean_bit(dev);
11392 	}
11393 
11394 	if ((rval <= 0) && (un->un_HeadClean & TAPE_ALERT_SUPPORTED)) {
11395 
11396 		rval = st_check_alert_flags(dev);
11397 	}
11398 
11399 	if ((rval <= 0) && (un->un_dp->options & ST_CLN_MASK)) {
11400 
11401 		rval = st_check_sense_clean_bit(dev);
11402 	}
11403 
11404 	if (rval < 0) {
11405 		return (rval);
11406 	}
11407 
11408 	/*
11409 	 * If found a supported means to check need to clean.
11410 	 */
11411 	if (rval & MTF_TAPE_CLN_SUPPORTED) {
11412 
11413 		/*
11414 		 * head needs to be cleaned.
11415 		 */
11416 		if (rval & MTF_TAPE_HEAD_DIRTY) {
11417 
11418 			/*
11419 			 * Print log message only first time
11420 			 * found needing cleaned.
11421 			 */
11422 			if ((un->un_HeadClean & TAPE_PREVIOUSLY_DIRTY) == 0) {
11423 
11424 				scsi_log(ST_DEVINFO, st_label, CE_WARN,
11425 				    "Periodic head cleaning required");
11426 
11427 				un->un_HeadClean |= TAPE_PREVIOUSLY_DIRTY;
11428 			}
11429 
11430 		} else {
11431 
11432 			un->un_HeadClean &= ~TAPE_PREVIOUSLY_DIRTY;
11433 		}
11434 	}
11435 
11436 	return (rval);
11437 }
11438 
11439 
11440 static int
11441 st_check_sequential_clean_bit(dev_t dev)
11442 {
11443 	int rval;
11444 	int ix;
11445 	ushort_t parameter;
11446 	struct uscsi_cmd *cmd;
11447 	struct log_sequential_page *sp;
11448 	struct log_sequential_page_parameter *prm;
11449 	char cdb[CDB_GROUP1] = {
11450 		SCMD_LOG_SENSE_G1,
11451 		0,
11452 		TAPE_SEQUENTIAL_PAGE | CURRENT_CUMULATIVE_VALUES,
11453 		0,
11454 		0,
11455 		0,
11456 		0,
11457 		(char)(sizeof (struct log_sequential_page) >> 8),
11458 		(char)(sizeof (struct log_sequential_page)),
11459 		0
11460 	};
11461 
11462 	GET_SOFT_STATE(dev);
11463 
11464 	cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11465 	sp  = kmem_zalloc(sizeof (struct log_sequential_page), KM_SLEEP);
11466 
11467 	cmd->uscsi_flags   =
11468 		USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
11469 	cmd->uscsi_timeout = un->un_dp->non_motion_timeout;
11470 	cmd->uscsi_cdb	   = cdb;
11471 	cmd->uscsi_cdblen  = CDB_GROUP1;
11472 	cmd->uscsi_bufaddr = (caddr_t)sp;
11473 	cmd->uscsi_buflen  = sizeof (struct log_sequential_page);
11474 
11475 	rval = st_ioctl_cmd(dev, cmd, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
11476 
11477 	if (rval || cmd->uscsi_status || cmd->uscsi_resid) {
11478 
11479 		rval = -1;
11480 
11481 	} else if (sp->log_page.code != TAPE_SEQUENTIAL_PAGE) {
11482 
11483 		rval = -1;
11484 	}
11485 
11486 	prm = &sp->param[0];
11487 
11488 	for (ix = 0; rval == 0 && ix < TAPE_SEQUENTIAL_PAGE_PARA; ix++) {
11489 
11490 		if (prm->log_param.length == 0) {
11491 			break;
11492 		}
11493 
11494 		parameter = (((prm->log_param.pc_hi << 8) & 0xff00) +
11495 			(prm->log_param.pc_lo & 0xff));
11496 
11497 		if (parameter == SEQUENTIAL_NEED_CLN) {
11498 
11499 			rval = MTF_TAPE_CLN_SUPPORTED;
11500 			if (prm->param_value[prm->log_param.length - 1]) {
11501 
11502 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11503 					    "sequential log says head dirty\n");
11504 				rval |= MTF_TAPE_HEAD_DIRTY;
11505 			}
11506 		}
11507 		prm = (struct log_sequential_page_parameter *)
11508 			&prm->param_value[prm->log_param.length];
11509 	}
11510 
11511 	kmem_free(cmd, sizeof (struct uscsi_cmd));
11512 	kmem_free(sp,  sizeof (struct log_sequential_page));
11513 
11514 	return (rval);
11515 }
11516 
11517 
11518 static int
11519 st_check_alert_flags(dev_t dev)
11520 {
11521 	struct st_tape_alert *ta;
11522 	struct uscsi_cmd *com;
11523 	unsigned ix, length;
11524 	int rval;
11525 	tape_alert_flags flag;
11526 	char cdb[CDB_GROUP1] = {
11527 		SCMD_LOG_SENSE_G1,
11528 		0,
11529 		TAPE_ALERT_PAGE | CURRENT_THRESHOLD_VALUES,
11530 		0,
11531 		0,
11532 		0,
11533 		0,
11534 		(char)(sizeof (struct st_tape_alert) >> 8),
11535 		(char)(sizeof (struct st_tape_alert)),
11536 		0
11537 	};
11538 
11539 	GET_SOFT_STATE(dev);
11540 
11541 	com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11542 	ta  = kmem_zalloc(sizeof (struct st_tape_alert), KM_SLEEP);
11543 
11544 	com->uscsi_cdb = cdb;
11545 	com->uscsi_cdblen = CDB_GROUP1;
11546 	com->uscsi_bufaddr = (caddr_t)ta;
11547 	com->uscsi_buflen = sizeof (struct st_tape_alert);
11548 	com->uscsi_flags =
11549 		USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
11550 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
11551 
11552 	rval = st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
11553 
11554 	if (rval || com->uscsi_status || com->uscsi_resid) {
11555 
11556 		rval = -1; /* uscsi-command failed */
11557 
11558 	} else if (ta->log_page.code != TAPE_ALERT_PAGE) {
11559 
11560 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11561 		"Not Alert Log Page returned 0x%X\n", ta->log_page.code);
11562 		rval = -1;
11563 	}
11564 
11565 	length = (ta->log_page.length_hi << 8) + ta->log_page.length_lo;
11566 
11567 
11568 	if (length != TAPE_ALERT_PARAMETER_LENGTH) {
11569 
11570 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11571 		    "TapeAlert length %d\n", length);
11572 	}
11573 
11574 
11575 	for (ix = 0; ix < TAPE_ALERT_MAX_PARA; ix++) {
11576 
11577 		/*
11578 		 * if rval is bad before the first pass don't bother
11579 		 */
11580 		if (ix == 0 && rval != 0) {
11581 
11582 			break;
11583 		}
11584 
11585 		flag = ((ta->param[ix].log_param.pc_hi << 8) +
11586 			ta->param[ix].log_param.pc_lo);
11587 
11588 		if ((ta->param[ix].param_value & 1) == 0) {
11589 			continue;
11590 		}
11591 		/*
11592 		 * check to see if current parameter is of interest.
11593 		 * CLEAN_FOR_ERRORS is vendor specific to 9840 9940 stk's.
11594 		 */
11595 		if ((flag == TAF_CLEAN_NOW) ||
11596 		    (flag == TAF_CLEAN_PERIODIC) ||
11597 		    ((flag == CLEAN_FOR_ERRORS) &&
11598 		    (un->un_dp->type == ST_TYPE_STK9840))) {
11599 
11600 			rval = MTF_TAPE_CLN_SUPPORTED;
11601 
11602 
11603 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11604 			    "alert_page drive needs clean %d\n", flag);
11605 			un->un_HeadClean |= TAPE_ALERT_STILL_DIRTY;
11606 			rval |= MTF_TAPE_HEAD_DIRTY;
11607 
11608 		} else if (flag == TAF_CLEANING_MEDIA) {
11609 
11610 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11611 			    "alert_page drive was cleaned\n");
11612 			un->un_HeadClean &= ~TAPE_ALERT_STILL_DIRTY;
11613 		}
11614 
11615 	}
11616 
11617 	/*
11618 	 * Report it as dirty till we see it cleaned
11619 	 */
11620 	if (un->un_HeadClean & TAPE_ALERT_STILL_DIRTY) {
11621 
11622 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11623 		    "alert_page still dirty\n");
11624 		rval |= MTF_TAPE_HEAD_DIRTY;
11625 	}
11626 
11627 	kmem_free(com, sizeof (struct uscsi_cmd));
11628 	kmem_free(ta,  sizeof (struct st_tape_alert));
11629 
11630 	return (rval);
11631 }
11632 
11633 
11634 static int
11635 st_check_sense_clean_bit(dev_t dev)
11636 {
11637 	uchar_t *sensep;
11638 	char cdb[CDB_GROUP0];
11639 	struct uscsi_cmd *com;
11640 	ushort_t byte_pos;
11641 	uchar_t bit_mask;
11642 	unsigned length;
11643 	int index;
11644 	int rval;
11645 
11646 	GET_SOFT_STATE(dev);
11647 
11648 	/*
11649 	 * Since this tape does not support Tape Alert,
11650 	 * we now try to get the cleanbit status via
11651 	 * Request Sense.
11652 	 */
11653 
11654 	if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_1) {
11655 
11656 		index = 0;
11657 
11658 	} else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_2) {
11659 
11660 		index = 1;
11661 
11662 	} else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_3) {
11663 
11664 		index = 2;
11665 
11666 	} else {
11667 
11668 		return (-1);
11669 	}
11670 
11671 	byte_pos  = st_cln_bit_position[index].cln_bit_byte;
11672 	bit_mask  = st_cln_bit_position[index].cln_bit_mask;
11673 	length = byte_pos + 1;
11674 
11675 	com    = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11676 	sensep = kmem_zalloc(length, KM_SLEEP);
11677 
11678 	cdb[0] = SCMD_REQUEST_SENSE;
11679 	cdb[1] = 0;
11680 	cdb[2] = 0;
11681 	cdb[3] = 0;
11682 	cdb[4] = (char)length;
11683 	cdb[5] = 0;
11684 
11685 	com->uscsi_cdb = cdb;
11686 	com->uscsi_cdblen = CDB_GROUP0;
11687 	com->uscsi_bufaddr = (caddr_t)sensep;
11688 	com->uscsi_buflen = length;
11689 	com->uscsi_flags =
11690 		USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
11691 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
11692 
11693 	rval = st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
11694 			UIO_SYSSPACE);
11695 
11696 	if (rval || com->uscsi_status || com->uscsi_resid) {
11697 
11698 		rval = -1;
11699 
11700 	} else {
11701 
11702 		rval = MTF_TAPE_CLN_SUPPORTED;
11703 		if ((sensep[byte_pos] & bit_mask) == bit_mask) {
11704 
11705 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11706 				    "sense data says head dirty\n");
11707 			rval |= MTF_TAPE_HEAD_DIRTY;
11708 		}
11709 	}
11710 
11711 	kmem_free(com, sizeof (struct uscsi_cmd));
11712 	kmem_free(sensep, length);
11713 	return (rval);
11714 }
11715 
11716 /*
11717  * st_clear_unit_attention
11718  *
11719  *  	run test unit ready's to clear out outstanding
11720  * 	unit attentions.
11721  * 	returns zero for SUCCESS or the errno from st_cmd call
11722  */
11723 static int
11724 st_clear_unit_attentions(dev_t dev_instance, int max_trys)
11725 {
11726 	int	i    = 0;
11727 	int	rval;
11728 
11729 	do {
11730 		rval = st_cmd(dev_instance, SCMD_TEST_UNIT_READY, 0, SYNC_CMD);
11731 	} while ((rval != 0) && (rval != ENXIO) && (++i < max_trys));
11732 	return (rval);
11733 }
11734 
11735 static void
11736 st_calculate_timeouts(struct scsi_tape *un)
11737 {
11738 	if (un->un_dp->non_motion_timeout == 0) {
11739 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11740 			un->un_dp->non_motion_timeout =
11741 				st_io_time * st_long_timeout_x;
11742 		} else {
11743 			un->un_dp->non_motion_timeout = (ushort_t)st_io_time;
11744 		}
11745 	}
11746 
11747 	if (un->un_dp->io_timeout == 0) {
11748 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11749 			un->un_dp->io_timeout = st_io_time * st_long_timeout_x;
11750 		} else {
11751 			un->un_dp->io_timeout = (ushort_t)st_io_time;
11752 		}
11753 	}
11754 
11755 	if (un->un_dp->rewind_timeout == 0) {
11756 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11757 			un->un_dp->rewind_timeout =
11758 				st_space_time * st_long_timeout_x;
11759 		} else {
11760 			un->un_dp->rewind_timeout = (ushort_t)st_space_time;
11761 		}
11762 	}
11763 
11764 	if (un->un_dp->space_timeout == 0) {
11765 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11766 			un->un_dp->space_timeout =
11767 				st_space_time * st_long_timeout_x;
11768 		} else {
11769 			un->un_dp->space_timeout = (ushort_t)st_space_time;
11770 		}
11771 	}
11772 
11773 	if (un->un_dp->load_timeout == 0) {
11774 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11775 			un->un_dp->load_timeout =
11776 				st_space_time * st_long_timeout_x;
11777 		} else {
11778 			un->un_dp->load_timeout = (ushort_t)st_space_time;
11779 		}
11780 	}
11781 
11782 	if (un->un_dp->unload_timeout == 0) {
11783 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11784 			un->un_dp->unload_timeout =
11785 				st_space_time * st_long_timeout_x;
11786 		} else {
11787 			un->un_dp->unload_timeout = (ushort_t)st_space_time;
11788 		}
11789 	}
11790 
11791 	if (un->un_dp->erase_timeout == 0) {
11792 		if (un->un_dp->options & ST_LONG_ERASE) {
11793 			un->un_dp->erase_timeout =
11794 				st_space_time * st_long_space_time_x;
11795 		} else {
11796 			un->un_dp->erase_timeout = (ushort_t)st_space_time;
11797 		}
11798 	}
11799 }
11800 
11801 
11802 /*ARGSUSED*/
11803 static writablity
11804 st_is_not_wormable(struct scsi_tape *un)
11805 {
11806 	return (RDWR);
11807 }
11808 
11809 static writablity
11810 st_is_hp_lto_tape_worm(struct scsi_tape *un)
11811 {
11812 	writablity wrt;
11813 
11814 
11815 	/* Mode sense should be current */
11816 	switch (un->un_mspl->media_type) {
11817 	case 0x00:
11818 		switch (un->un_mspl->density) {
11819 		case 0x40:
11820 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11821 			    "Drive has standard Gen I media loaded\n");
11822 			break;
11823 		case 0x42:
11824 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11825 			    "Drive has standard Gen II media loaded\n");
11826 			break;
11827 		case 0x44:
11828 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11829 			    "Drive has standard Gen III media loaded\n");
11830 			break;
11831 		default:
11832 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11833 			    "Drive has standard unknown 0x%X media loaded\n",
11834 			    un->un_mspl->density);
11835 		}
11836 		wrt = RDWR;
11837 		break;
11838 	case 0x01:
11839 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11840 		    "Drive has WORM medium loaded\n");
11841 		wrt = WORM;
11842 		break;
11843 	case 0x80:
11844 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11845 		    "Drive has CD-ROM emulation medium loaded\n");
11846 		wrt = WORM;
11847 		break;
11848 	default:
11849 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11850 		    "Drive has an unexpected medium type 0x%X loaded\n",
11851 		    un->un_mspl->media_type);
11852 		wrt = RDWR;
11853 	}
11854 
11855 	return (wrt);
11856 }
11857 
11858 #define	LTO_REQ_INQUIRY 44
11859 static writablity
11860 st_is_hp_lto_worm(struct scsi_tape *un)
11861 {
11862 	char *buf;
11863 	int result;
11864 	writablity wrt;
11865 
11866 	buf = kmem_zalloc(LTO_REQ_INQUIRY, KM_SLEEP);
11867 
11868 	result = st_get_special_inquiry(un, LTO_REQ_INQUIRY, buf, 0);
11869 
11870 	if (result != 0) {
11871 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11872 		    "Read Standard Inquiry for WORM support failed");
11873 		wrt = ERROR;
11874 	} else if ((buf[40] & 1) == 0) {
11875 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11876 		    "Drive is NOT WORMable\n");
11877 		/* This drive doesn't support it so don't check again */
11878 		un->un_dp->options &= ~ST_WORMABLE;
11879 		wrt = RDWR;
11880 		un->un_wormable = st_is_not_wormable;
11881 	} else {
11882 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11883 		    "Drive supports WORM version %d\n", buf[40] >> 1);
11884 		un->un_wormable = st_is_hp_lto_tape_worm;
11885 		wrt = un->un_wormable(un);
11886 	}
11887 
11888 	kmem_free(buf, LTO_REQ_INQUIRY);
11889 
11890 	/*
11891 	 * If drive doesn't support it no point in checking further.
11892 	 */
11893 	return (wrt);
11894 }
11895 
11896 static writablity
11897 st_is_t10_worm_device(struct scsi_tape *un)
11898 {
11899 	writablity wrt;
11900 
11901 	if (un->un_mspl->media_type == 0x3c) {
11902 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11903 		    "Drive has WORM media loaded\n");
11904 		wrt = WORM;
11905 	} else {
11906 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11907 		    "Drive has non WORM media loaded\n");
11908 		wrt = RDWR;
11909 	}
11910 	return (wrt);
11911 }
11912 
11913 #define	SEQ_CAP_PAGE	(char)0xb0
11914 static writablity
11915 st_is_t10_worm(struct scsi_tape *un)
11916 {
11917 	char *buf;
11918 	int result;
11919 	writablity wrt;
11920 
11921 	buf = kmem_zalloc(6, KM_SLEEP);
11922 
11923 	result = st_get_special_inquiry(un, 6, buf, SEQ_CAP_PAGE);
11924 
11925 	if (result != 0) {
11926 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11927 		    "Read Vitial Inquiry for Sequental Capability"
11928 		    " WORM support failed %x", result);
11929 		wrt = ERROR;
11930 	} else if ((buf[4] & 1) == 0) {
11931 		ASSERT(buf[1] == SEQ_CAP_PAGE);
11932 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11933 		    "Drive is NOT WORMable\n");
11934 		/* This drive doesn't support it so don't check again */
11935 		un->un_dp->options &= ~ST_WORMABLE;
11936 		wrt = RDWR;
11937 		un->un_wormable = st_is_not_wormable;
11938 	} else {
11939 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11940 		    "Drive supports WORM\n");
11941 		un->un_wormable = st_is_t10_worm_device;
11942 		wrt = un->un_wormable(un);
11943 	}
11944 
11945 	kmem_free(buf, 6);
11946 
11947 	return (wrt);
11948 }
11949 
11950 
11951 #define	STK_REQ_SENSE 26
11952 
11953 static writablity
11954 st_is_stk_worm(struct scsi_tape *un)
11955 {
11956 	char cdb[CDB_GROUP0] = {SCMD_REQUEST_SENSE, 0, 0, 0, STK_REQ_SENSE, 0};
11957 	struct scsi_extended_sense *sense;
11958 	struct uscsi_cmd *cmd;
11959 	char *buf;
11960 	int result;
11961 	writablity wrt;
11962 
11963 	cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11964 	buf = kmem_alloc(STK_REQ_SENSE, KM_SLEEP);
11965 	sense = (struct scsi_extended_sense *)buf;
11966 
11967 	cmd->uscsi_flags = USCSI_READ;
11968 	cmd->uscsi_timeout = un->un_dp->non_motion_timeout;
11969 	cmd->uscsi_cdb = &cdb[0];
11970 	cmd->uscsi_bufaddr = buf;
11971 	cmd->uscsi_buflen = STK_REQ_SENSE;
11972 	cmd->uscsi_cdblen = CDB_GROUP0;
11973 	cmd->uscsi_rqlen = 0;
11974 	cmd->uscsi_rqbuf = NULL;
11975 
11976 	result = st_ioctl_cmd(un->un_dev, cmd,
11977 	    UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
11978 
11979 	if (result != 0 || cmd->uscsi_status != 0) {
11980 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11981 		    "Request Sense for WORM failed");
11982 		wrt = RDWR;
11983 	} else if (sense->es_add_len + 8 < 24) {
11984 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11985 		    "Drive didn't send enough sense data for WORM byte %d\n",
11986 		    sense->es_add_len + 8);
11987 		wrt = RDWR;
11988 		un->un_wormable = st_is_not_wormable;
11989 	} else if ((buf[24]) & 0x02) {
11990 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11991 		    "Drive has WORM tape loaded\n");
11992 		wrt = WORM;
11993 		un->un_wormable = st_is_stk_worm;
11994 	} else {
11995 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11996 		    "Drive has normal tape loaded\n");
11997 		wrt = RDWR;
11998 		un->un_wormable = st_is_stk_worm;
11999 	}
12000 
12001 	kmem_free(buf, STK_REQ_SENSE);
12002 	kmem_free(cmd, sizeof (struct uscsi_cmd));
12003 	return (wrt);
12004 }
12005 
12006 #define	DLT_INQ_SZ 44
12007 
12008 static writablity
12009 st_is_dlt_tape_worm(struct scsi_tape *un)
12010 {
12011 	caddr_t buf;
12012 	int result;
12013 	writablity wrt;
12014 
12015 	buf = kmem_alloc(DLT_INQ_SZ, KM_SLEEP);
12016 
12017 	/* Read Attribute Media Type */
12018 
12019 	result = st_read_attributes(un, 0x0408, buf, 10);
12020 
12021 	/*
12022 	 * If this quantum drive is attached via an HBA that cannot
12023 	 * support thr read attributes command return error in the
12024 	 * hope that someday they will support the t10 method.
12025 	 */
12026 	if (result == EINVAL && un->un_max_cdb_sz < CDB_GROUP4) {
12027 		scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
12028 		    "Read Attribute Command for WORM Media detection is not "
12029 		    "supported on the HBA that this drive is attached to.");
12030 		wrt = RDWR;
12031 		un->un_wormable = st_is_not_wormable;
12032 		goto out;
12033 	}
12034 
12035 	if (result != 0) {
12036 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12037 		    "Read Attribute Command for WORM Media returned 0x%x",
12038 		    result);
12039 		wrt = RDWR;
12040 		un->un_dp->options &= ~ST_WORMABLE;
12041 		goto out;
12042 	}
12043 
12044 	if ((uchar_t)buf[9] == 0x80) {
12045 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12046 		    "Drive media is WORM\n");
12047 		wrt = WORM;
12048 	} else {
12049 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12050 		    "Drive media is not WORM Media 0x%x\n", (uchar_t)buf[9]);
12051 		wrt = RDWR;
12052 	}
12053 
12054 out:
12055 	kmem_free(buf, DLT_INQ_SZ);
12056 	return (wrt);
12057 }
12058 
12059 static writablity
12060 st_is_dlt_worm(struct scsi_tape *un)
12061 {
12062 	caddr_t buf;
12063 	int result;
12064 	writablity wrt;
12065 
12066 	buf = kmem_alloc(DLT_INQ_SZ, KM_SLEEP);
12067 
12068 	result = st_get_special_inquiry(un, DLT_INQ_SZ, buf, 0xC0);
12069 
12070 	if (result != 0) {
12071 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12072 		    "Read Vendor Specific Inquiry for WORM support failed");
12073 		wrt = RDWR;
12074 		goto out;
12075 	}
12076 
12077 	if ((buf[2] & 1) == 0) {
12078 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12079 		    "Drive is not WORMable\n");
12080 		wrt = RDWR;
12081 		un->un_dp->options &= ~ST_WORMABLE;
12082 		un->un_wormable = st_is_not_wormable;
12083 		goto out;
12084 	} else {
12085 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12086 		    "Drive is WORMable\n");
12087 		un->un_wormable = st_is_dlt_tape_worm;
12088 		wrt = un->un_wormable(un);
12089 	}
12090 out:
12091 	kmem_free(buf, DLT_INQ_SZ);
12092 
12093 	return (wrt);
12094 }
12095 
12096 typedef struct {
12097 	struct modeheader_seq header;
12098 #if defined(_BIT_FIELDS_LTOH) /* X86 */
12099 	uchar_t pagecode	:6,
12100 				:2;
12101 	uchar_t page_len;
12102 	uchar_t syslogalive	:2,
12103 		device		:1,
12104 		abs		:1,
12105 		ulpbot		:1,
12106 		prth		:1,
12107 		ponej		:1,
12108 		ait		:1;
12109 	uchar_t span;
12110 
12111 	uchar_t			:6,
12112 		worm		:1,
12113 		mic		:1;
12114 	uchar_t worm_cap	:1,
12115 				:7;
12116 	uint32_t		:32;
12117 #else /* SPARC */
12118 	uchar_t			:2,
12119 		pagecode	:6;
12120 	uchar_t page_len;
12121 	uchar_t ait		:1,
12122 		device		:1,
12123 		abs		:1,
12124 		ulpbot		:1,
12125 		prth		:1,
12126 		ponej		:1,
12127 		syslogalive	:2;
12128 	uchar_t span;
12129 	uchar_t mic		:1,
12130 		worm		:1,
12131 				:6;
12132 	uchar_t			:7,
12133 		worm_cap	:1;
12134 	uint32_t		:32;
12135 #endif
12136 }ait_dev_con;
12137 
12138 #define	AIT_DEV_PAGE 0x31
12139 static writablity
12140 st_is_sony_worm(struct scsi_tape *un)
12141 {
12142 	int result;
12143 	writablity wrt;
12144 	ait_dev_con *ait_conf;
12145 
12146 	ait_conf = kmem_zalloc(sizeof (ait_dev_con), KM_SLEEP);
12147 
12148 	result = st_gen_mode_sense(un, AIT_DEV_PAGE,
12149 	    (struct seq_mode *)ait_conf, sizeof (ait_dev_con));
12150 
12151 	if (result == 0) {
12152 
12153 		if (ait_conf->pagecode != AIT_DEV_PAGE) {
12154 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12155 			    "returned page 0x%x not 0x%x AIT_DEV_PAGE\n",
12156 			    ait_conf->pagecode, AIT_DEV_PAGE);
12157 			wrt = RDWR;
12158 			un->un_wormable = st_is_not_wormable;
12159 
12160 		} else if (ait_conf->worm_cap) {
12161 
12162 			un->un_wormable = st_is_sony_worm;
12163 
12164 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12165 			    "Drives is WORMable\n");
12166 			if (ait_conf->worm) {
12167 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12168 				    "Media is WORM\n");
12169 				wrt = WORM;
12170 			} else {
12171 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12172 				    "Media is not WORM\n");
12173 				wrt = RDWR;
12174 			}
12175 
12176 		} else {
12177 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12178 			    "Drives not is WORMable\n");
12179 			wrt = RDWR;
12180 			/* No further checking required */
12181 			un->un_dp->options &= ~ST_WORMABLE;
12182 		}
12183 
12184 	} else {
12185 
12186 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12187 		    "AIT device config mode sense page read command failed"
12188 		    " result = %d ", result);
12189 		wrt = ERROR;
12190 		un->un_wormable = st_is_not_wormable;
12191 	}
12192 
12193 	kmem_free(ait_conf, sizeof (ait_dev_con));
12194 	return (wrt);
12195 }
12196 
12197 static writablity
12198 st_is_drive_worm(struct scsi_tape *un)
12199 {
12200 	writablity wrt;
12201 
12202 	switch (un->un_dp->type) {
12203 	case MT_ISDLT:
12204 		wrt = st_is_dlt_worm(un);
12205 		break;
12206 
12207 	case MT_ISSTK9840:
12208 		wrt = st_is_stk_worm(un);
12209 		break;
12210 
12211 	case MT_IS8MM:
12212 	case MT_ISAIT:
12213 		wrt = st_is_sony_worm(un);
12214 		break;
12215 
12216 	case MT_LTO:
12217 		if (strncmp("HP ", un->un_dp->vid, 3) == 0) {
12218 			wrt = st_is_hp_lto_worm(un);
12219 		} else {
12220 			wrt = st_is_t10_worm(un);
12221 		}
12222 		break;
12223 
12224 	default:
12225 		wrt = ERROR;
12226 		break;
12227 	}
12228 
12229 	/*
12230 	 * If any of the above failed try the t10 standard method.
12231 	 */
12232 	if (wrt == ERROR) {
12233 		wrt = st_is_t10_worm(un);
12234 	}
12235 
12236 	/*
12237 	 * Unknown method for detecting WORM media.
12238 	 */
12239 	if (wrt == ERROR) {
12240 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12241 		    "Unknown method for WORM media detection\n");
12242 		wrt = RDWR;
12243 		un->un_dp->options &= ~ST_WORMABLE;
12244 	}
12245 
12246 	return (wrt);
12247 }
12248 
12249 static int
12250 st_read_attributes(struct scsi_tape *un, uint16_t attribute, caddr_t buf,
12251     size_t size)
12252 {
12253 	uchar_t cdb[CDB_GROUP4];
12254 	struct uscsi_cmd *cmd;
12255 	int result;
12256 
12257 	cdb[0] = SCMD_READ_ATTRIBUTE;
12258 	cdb[1] = 0;
12259 	cdb[2] = 0;
12260 	cdb[3] = 0;
12261 	cdb[4] = 0;
12262 	cdb[5] = 0;
12263 	cdb[6] = 0;
12264 	cdb[7] = 0;
12265 	cdb[8] = (uchar_t)(attribute >> 8);
12266 	cdb[9] = (uchar_t)(attribute);
12267 	cdb[10] = (uchar_t)(size >> 24);
12268 	cdb[11] = (uchar_t)(size >> 16);
12269 	cdb[12] = (uchar_t)(size >> 8);
12270 	cdb[13] = (uchar_t)(size);
12271 	cdb[14] = 0;
12272 	cdb[15] = 0;
12273 
12274 	cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
12275 
12276 	cmd->uscsi_flags = USCSI_READ | USCSI_DIAGNOSE;
12277 	cmd->uscsi_timeout = un->un_dp->non_motion_timeout;
12278 	cmd->uscsi_cdb = (caddr_t)&cdb[0];
12279 	cmd->uscsi_bufaddr = (caddr_t)buf;
12280 	cmd->uscsi_buflen = size;
12281 	cmd->uscsi_cdblen = sizeof (cdb);
12282 
12283 	result = st_ioctl_cmd(un->un_dev, cmd,
12284 	    UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
12285 
12286 	if (result != 0 || cmd->uscsi_status != 0) {
12287 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12288 		    "st_read_attribute failed: result %d status %d\n",
12289 		    result, cmd->uscsi_status);
12290 		if (result == 0) {
12291 			result = EIO;
12292 		}
12293 	}
12294 
12295 	kmem_free(cmd, sizeof (cdb));
12296 	return (result);
12297 }
12298 
12299 static int
12300 st_get_special_inquiry(struct scsi_tape *un, uchar_t size, caddr_t dest,
12301     uchar_t page)
12302 {
12303 	char cdb[CDB_GROUP0];
12304 	struct scsi_extended_sense *sense;
12305 	struct uscsi_cmd *cmd;
12306 	int result;
12307 
12308 	cdb[0] = SCMD_INQUIRY;
12309 	cdb[1] = page ? 1 : 0;
12310 	cdb[2] = page;
12311 	cdb[3] = 0;
12312 	cdb[4] = size;
12313 	cdb[5] = 0;
12314 
12315 	cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
12316 	sense = kmem_alloc(sizeof (struct scsi_extended_sense), KM_SLEEP);
12317 
12318 	cmd->uscsi_flags = USCSI_READ | USCSI_RQENABLE;
12319 	cmd->uscsi_timeout = un->un_dp->non_motion_timeout;
12320 	cmd->uscsi_cdb = &cdb[0];
12321 	cmd->uscsi_bufaddr = dest;
12322 	cmd->uscsi_buflen = size;
12323 	cmd->uscsi_cdblen = CDB_GROUP0;
12324 	cmd->uscsi_rqlen = sizeof (struct scsi_extended_sense);
12325 	cmd->uscsi_rqbuf = (caddr_t)sense;
12326 
12327 	result = st_ioctl_cmd(un->un_dev, cmd,
12328 	    UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
12329 
12330 	if (result != 0 || cmd->uscsi_status != 0) {
12331 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12332 		    "st_get_special_inquiry() failed for page %x", page);
12333 		if (result == 0) {
12334 			result = EIO;
12335 		}
12336 	}
12337 
12338 	kmem_free(sense, sizeof (struct scsi_extended_sense));
12339 	kmem_free(cmd, sizeof (struct uscsi_cmd));
12340 
12341 	return (result);
12342 }
12343 
12344 
12345 #if defined(__i386) || defined(__amd64)
12346 
12347 /*
12348  * release contig_mem and wake up waiting thread, if any
12349  */
12350 static void
12351 st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp)
12352 {
12353 	mutex_enter(ST_MUTEX);
12354 
12355 	cp->cm_next = un->un_contig_mem;
12356 	un->un_contig_mem = cp;
12357 	un->un_contig_mem_available_num++;
12358 	cv_broadcast(&un->un_contig_mem_cv);
12359 
12360 	mutex_exit(ST_MUTEX);
12361 }
12362 
12363 /*
12364  * St_get_contig_mem will return a contig_mem if there is one available
12365  * in current system. Otherwise, it will try to alloc one, if the total
12366  * number of contig_mem is within st_max_contig_mem_num.
12367  * It will sleep, if allowed by caller or return NULL, if no contig_mem
12368  * is available for now.
12369  */
12370 static struct contig_mem *
12371 st_get_contig_mem(struct scsi_tape *un, size_t len, int alloc_flags)
12372 {
12373 	size_t rlen;
12374 	struct contig_mem *cp = NULL;
12375 	ddi_acc_handle_t acc_hdl;
12376 	caddr_t addr;
12377 	int big_enough = 0;
12378 	int (*dma_alloc_cb)() = (alloc_flags == KM_SLEEP) ?
12379 		DDI_DMA_SLEEP : DDI_DMA_DONTWAIT;
12380 
12381 	/* Try to get one available contig_mem */
12382 	mutex_enter(ST_MUTEX);
12383 	if (un->un_contig_mem_available_num > 0) {
12384 		ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough);
12385 	} else if (un->un_contig_mem_total_num < st_max_contig_mem_num) {
12386 		/*
12387 		 * we failed to get one. we're going to
12388 		 * alloc one more contig_mem for this I/O
12389 		 */
12390 		mutex_exit(ST_MUTEX);
12391 		cp = (struct contig_mem *)kmem_zalloc(
12392 		    sizeof (struct contig_mem) + biosize(),
12393 		    alloc_flags);
12394 		if (cp == NULL) {
12395 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12396 			    "alloc contig_mem failure\n");
12397 			return (NULL); /* cannot get one */
12398 		}
12399 		cp->cm_bp = (struct buf *)
12400 		    (((caddr_t)cp) + sizeof (struct contig_mem));
12401 		bioinit(cp->cm_bp);
12402 		mutex_enter(ST_MUTEX);
12403 		un->un_contig_mem_total_num++; /* one more available */
12404 	} else {
12405 		/*
12406 		 * we failed to get one and we're NOT allowed to
12407 		 * alloc more contig_mem
12408 		 */
12409 		if (alloc_flags == KM_SLEEP) {
12410 			while (un->un_contig_mem_available_num <= 0) {
12411 				cv_wait(&un->un_contig_mem_cv,
12412 				    ST_MUTEX);
12413 			}
12414 			ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough);
12415 		} else {
12416 			mutex_exit(ST_MUTEX);
12417 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12418 			    "alloc contig_mem failure\n");
12419 			return (NULL); /* cannot get one */
12420 		}
12421 	}
12422 	mutex_exit(ST_MUTEX);
12423 
12424 	/* We need to check if this block of mem is big enough for this I/O */
12425 	if (cp->cm_len < len) {
12426 		/* not big enough, need to alloc a new one */
12427 		if (ddi_dma_mem_alloc(un->un_contig_mem_hdl, len, &st_acc_attr,
12428 			DDI_DMA_STREAMING, dma_alloc_cb, NULL,
12429 			&addr, &rlen, &acc_hdl) != DDI_SUCCESS) {
12430 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
12431 			    "alloc contig_mem failure: not enough mem\n");
12432 			st_release_contig_mem(un, cp);
12433 			cp = NULL;
12434 		} else {
12435 			if (cp->cm_addr) {
12436 				/* release previous one before attach new one */
12437 				ddi_dma_mem_free(&cp->cm_acc_hdl);
12438 			}
12439 			mutex_enter(ST_MUTEX);
12440 			un->un_max_contig_mem_len =
12441 			    un->un_max_contig_mem_len >= len ?
12442 			    un->un_max_contig_mem_len : len;
12443 			mutex_exit(ST_MUTEX);
12444 
12445 			/* attach new mem to this cp */
12446 			cp->cm_addr = addr;
12447 			cp->cm_acc_hdl = acc_hdl;
12448 			cp->cm_len = len;
12449 
12450 			goto alloc_ok; /* get one usable cp */
12451 		}
12452 	} else {
12453 		goto alloc_ok; /* get one usable cp */
12454 	}
12455 
12456 	/* cannot find/alloc a usable cp, when we get here */
12457 
12458 	mutex_enter(ST_MUTEX);
12459 	if ((un->un_max_contig_mem_len < len) ||
12460 	    (alloc_flags != KM_SLEEP)) {
12461 		mutex_exit(ST_MUTEX);
12462 		return (NULL);
12463 	}
12464 
12465 	/*
12466 	 * we're allowed to sleep, and there is one big enough
12467 	 * contig mem in the system, which is currently in use,
12468 	 * wait for it...
12469 	 */
12470 	big_enough = 1;
12471 	do {
12472 		cv_wait(&un->un_contig_mem_cv, ST_MUTEX);
12473 		ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough);
12474 	} while (cp == NULL);
12475 	mutex_exit(ST_MUTEX);
12476 
12477 	/* we get the big enough contig mem, finally */
12478 
12479 alloc_ok:
12480 	/* init bp attached to this cp */
12481 	bioreset(cp->cm_bp);
12482 	cp->cm_bp->b_un.b_addr = cp->cm_addr;
12483 	cp->cm_bp->b_private = (void *)cp;
12484 
12485 	return (cp);
12486 }
12487 
12488 /*
12489  * this is the biodone func for the bp used in big block I/O
12490  */
12491 static int
12492 st_bigblk_xfer_done(struct buf *bp)
12493 {
12494 	struct contig_mem *cp;
12495 	struct buf *orig_bp;
12496 	int remapped = 0;
12497 	int ioerr;
12498 	struct scsi_tape *un;
12499 
12500 	/* sanity check */
12501 	if (bp == NULL) {
12502 		return (DDI_FAILURE);
12503 	}
12504 
12505 	un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev));
12506 	if (un == NULL) {
12507 		return (DDI_FAILURE);
12508 	}
12509 
12510 	cp = (struct contig_mem *)bp->b_private;
12511 	orig_bp = cp->cm_bp; /* get back the bp we have replaced */
12512 	cp->cm_bp = bp;
12513 
12514 	/* special handling for special I/O */
12515 	if (cp->cm_use_sbuf) {
12516 #ifndef __lock_lint
12517 		ASSERT(un->un_sbuf_busy);
12518 #endif
12519 		un->un_sbufp = orig_bp;
12520 		cp->cm_use_sbuf = 0;
12521 	}
12522 
12523 	orig_bp->b_resid = bp->b_resid;
12524 	ioerr = geterror(bp);
12525 	if (ioerr != 0) {
12526 		bioerror(orig_bp, ioerr);
12527 	} else if (orig_bp->b_flags & B_READ) {
12528 		/* copy data back to original bp */
12529 		if (orig_bp->b_flags & (B_PHYS | B_PAGEIO)) {
12530 			bp_mapin(orig_bp);
12531 			remapped = 1;
12532 		}
12533 		bcopy(bp->b_un.b_addr, orig_bp->b_un.b_addr,
12534 			bp->b_bcount - bp->b_resid);
12535 		if (remapped)
12536 			bp_mapout(orig_bp);
12537 	}
12538 
12539 	st_release_contig_mem(un, cp);
12540 
12541 	biodone(orig_bp);
12542 
12543 	return (DDI_SUCCESS);
12544 }
12545 
12546 /*
12547  * We use this func to replace original bp that may not be able to do I/O
12548  * in big block size with one that can
12549  */
12550 static struct buf *
12551 st_get_bigblk_bp(struct buf *bp)
12552 {
12553 	struct contig_mem *cp;
12554 	struct scsi_tape *un;
12555 	struct buf *cont_bp;
12556 	int remapped = 0;
12557 
12558 	un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev));
12559 	if (un == NULL) {
12560 		return (bp);
12561 	}
12562 
12563 	/* try to get one contig_mem */
12564 	cp = st_get_contig_mem(un, bp->b_bcount, KM_SLEEP);
12565 	if (!cp) {
12566 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
12567 			"Cannot alloc contig buf for I/O for %lu blk size",
12568 			bp->b_bcount);
12569 		return (bp);
12570 	}
12571 	cont_bp = cp->cm_bp;
12572 	cp->cm_bp = bp;
12573 
12574 	/* make sure that we "are" using un_sbufp for special I/O */
12575 	if (bp == un->un_sbufp) {
12576 #ifndef __lock_lint
12577 		ASSERT(un->un_sbuf_busy);
12578 #endif
12579 		un->un_sbufp = cont_bp;
12580 		cp->cm_use_sbuf = 1;
12581 	}
12582 
12583 	/* clone bp */
12584 	cont_bp->b_bcount = bp->b_bcount;
12585 	cont_bp->b_resid = bp->b_resid;
12586 	cont_bp->b_iodone = st_bigblk_xfer_done;
12587 	cont_bp->b_file = bp->b_file;
12588 	cont_bp->b_offset = bp->b_offset;
12589 	cont_bp->b_dip = bp->b_dip;
12590 	cont_bp->b_error = 0;
12591 	cont_bp->b_proc = NULL;
12592 	cont_bp->b_flags = bp->b_flags & ~(B_PAGEIO | B_PHYS | B_SHADOW);
12593 	cont_bp->b_shadow = NULL;
12594 	cont_bp->b_pages = NULL;
12595 	cont_bp->b_edev = bp->b_edev;
12596 	cont_bp->b_dev = bp->b_dev;
12597 	cont_bp->b_lblkno = bp->b_lblkno;
12598 	cont_bp->b_forw = bp->b_forw;
12599 	cont_bp->b_back = bp->b_back;
12600 	cont_bp->av_forw = bp->av_forw;
12601 	cont_bp->av_back = bp->av_back;
12602 	cont_bp->b_bufsize = bp->b_bufsize;
12603 
12604 	/* get data in original bp */
12605 	if (bp->b_flags & B_WRITE) {
12606 		if (bp->b_flags & (B_PHYS | B_PAGEIO)) {
12607 			bp_mapin(bp);
12608 			remapped = 1;
12609 		}
12610 		bcopy(bp->b_un.b_addr, cont_bp->b_un.b_addr, bp->b_bcount);
12611 		if (remapped)
12612 			bp_mapout(bp);
12613 	}
12614 
12615 	return (cont_bp);
12616 }
12617 #else
12618 #ifdef __lock_lint
12619 static int
12620 st_bigblk_xfer_done(struct buf *bp)
12621 {
12622 	return (0);
12623 }
12624 #endif
12625 #endif
12626