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