xref: /illumos-gate/usr/src/uts/common/io/scsi/targets/st.c (revision 930176a00b697a09f5256ff3d4b1550f8e73c3a6)
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, 0);
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, 0);
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, 0);
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, 0);
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 		}
7581 
7582 		un->un_mspl->density = un->un_dp->densities[best];
7583 
7584 	} else {
7585 		/* Otherwise set density based on minor node opened */
7586 		un->un_mspl->density = un->un_dp->densities[un->un_curdens];
7587 	}
7588 
7589 	if (un->un_dp->options & ST_NOBUF) {
7590 		un->un_mspl->bufm = 0;
7591 	} else {
7592 		un->un_mspl->bufm = 1;
7593 	}
7594 
7595 	rval = st_set_compression(un);
7596 
7597 	/*
7598 	 * If st_set_compression returned invalid or already it
7599 	 * found no need to do the mode select.
7600 	 * So do it here.
7601 	 */
7602 	if ((rval == ENOTTY) || (rval == EALREADY)) {
7603 
7604 		/* Zero non-writeable fields */
7605 		un->un_mspl->data_len = 0;
7606 		un->un_mspl->media_type = 0;
7607 		un->un_mspl->wp = 0;
7608 
7609 		/* need to set the density code */
7610 		rval = st_cmd(un->un_dev, SCMD_MODE_SELECT, MSIZE, SYNC_CMD);
7611 		if (rval != 0) {
7612 			if (un->un_state >= ST_STATE_OPEN) {
7613 				ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
7614 				    "unable to set tape mode\n");
7615 				un->un_fileno = -1;
7616 				rval = EIO;
7617 			} else {
7618 				rval = -1;
7619 			}
7620 		}
7621 	}
7622 
7623 	/*
7624 	 * The spec recommends to send a mode sense after a mode select
7625 	 */
7626 	(void) st_modesense(un);
7627 
7628 	ASSERT(mutex_owned(ST_MUTEX));
7629 
7630 	return (rval);
7631 }
7632 
7633 /*
7634  * st_gen_mode_sense
7635  *
7636  * generic mode sense.. it allows for any page
7637  */
7638 static int
7639 st_gen_mode_sense(struct scsi_tape *un, int page, struct seq_mode *page_data,
7640     int page_size)
7641 {
7642 
7643 	int r;
7644 	char	cdb[CDB_GROUP0];
7645 	struct uscsi_cmd *com;
7646 
7647 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
7648 
7649 	bzero(cdb, CDB_GROUP0);
7650 	cdb[0] = SCMD_MODE_SENSE;
7651 	cdb[2] = (char)page;
7652 	cdb[4] = (char)page_size;
7653 
7654 	com->uscsi_cdb = cdb;
7655 	com->uscsi_cdblen = CDB_GROUP0;
7656 	com->uscsi_bufaddr = (caddr_t)page_data;
7657 	com->uscsi_buflen = page_size;
7658 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
7659 	com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT |
7660 			    USCSI_READ | USCSI_RQENABLE;
7661 
7662 	r = st_ioctl_cmd(un->un_dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
7663 		UIO_SYSSPACE);
7664 	kmem_free(com, sizeof (*com));
7665 	return (r);
7666 }
7667 
7668 /*
7669  * st_gen_mode_select
7670  *
7671  * generic mode select.. it allows for any page
7672  */
7673 static int
7674 st_gen_mode_select(struct scsi_tape *un, struct seq_mode *page_data,
7675     int page_size)
7676 {
7677 
7678 	int r;
7679 	char cdb[CDB_GROUP0];
7680 	struct uscsi_cmd *com;
7681 
7682 	/* Zero non-writeable fields */
7683 	page_data->data_len = 0;
7684 	page_data->media_type = 0;
7685 	page_data->wp = 0;
7686 
7687 	/*
7688 	 * If mode select has any page data, zero the ps (Page Savable) bit.
7689 	 */
7690 	if (page_size > MSIZE) {
7691 		page_data->ps = 0;
7692 	}
7693 
7694 
7695 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
7696 
7697 	/*
7698 	 * then, do a mode select to set what ever info
7699 	 */
7700 	bzero(cdb, CDB_GROUP0);
7701 	cdb[0] = SCMD_MODE_SELECT;
7702 	cdb[1] = 0x10;		/* set PF bit for many third party drives */
7703 	cdb[4] = (char)page_size;
7704 
7705 	com->uscsi_cdb = cdb;
7706 	com->uscsi_cdblen = CDB_GROUP0;
7707 	com->uscsi_bufaddr = (caddr_t)page_data;
7708 	com->uscsi_buflen = page_size;
7709 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
7710 	com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT
7711 		| USCSI_WRITE | USCSI_RQENABLE;
7712 
7713 	r = st_ioctl_cmd(un->un_dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
7714 		UIO_SYSSPACE);
7715 
7716 	kmem_free(com, sizeof (*com));
7717 	return (r);
7718 }
7719 
7720 /*
7721  * Changes devices blocksize and bsize to requested blocksize nblksz.
7722  * Returns returned value from first failed call or zero on success.
7723  */
7724 static int
7725 st_change_block_size(dev_t dev, uint32_t nblksz)
7726 {
7727 	struct seq_mode *current;
7728 	int rval;
7729 	uint32_t oldblksz;
7730 
7731 	GET_SOFT_STATE(dev);
7732 
7733 	current = kmem_zalloc(MSIZE, KM_SLEEP);
7734 
7735 	/* Read current settings */
7736 	rval = st_gen_mode_sense(un, 0, current, MSIZE);
7737 	if (rval != 0) {
7738 		scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
7739 		    "mode sense for change block size failed: rval = %d", rval);
7740 		goto finish;
7741 	}
7742 
7743 	/* Figure the current block size */
7744 	oldblksz =
7745 	    (current->high_bl << 16) |
7746 	    (current->mid_bl << 8) |
7747 	    (current->low_bl);
7748 
7749 	/* If current block size is the same as requested were done */
7750 	if (oldblksz == nblksz) {
7751 		un->un_bsize = nblksz;
7752 		rval = 0;
7753 		goto finish;
7754 	}
7755 
7756 	/* Change to requested block size */
7757 	current->high_bl = (uchar_t)(nblksz >> 16);
7758 	current->mid_bl  = (uchar_t)(nblksz >> 8);
7759 	current->low_bl  = (uchar_t)(nblksz);
7760 
7761 	/* Attempt to change block size */
7762 	rval = st_gen_mode_select(un, current, MSIZE);
7763 	if (rval != 0) {
7764 		scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
7765 		    "Set new block size failed: rval = %d", rval);
7766 		goto finish;
7767 	}
7768 
7769 	/* Read back and verify setting */
7770 	rval = st_modesense(un);
7771 	if (rval == 0) {
7772 		un->un_bsize =
7773 		    (un->un_mspl->high_bl << 16) |
7774 		    (un->un_mspl->mid_bl << 8) |
7775 		    (un->un_mspl->low_bl);
7776 
7777 		if (un->un_bsize != nblksz) {
7778 			scsi_log(ST_DEVINFO, st_label, SCSI_DEBUG,
7779 			    "Blocksize set does not equal requested blocksize"
7780 			    "(read: %u requested: %u)\n", nblksz, un->un_bsize);
7781 			rval = EIO;
7782 		}
7783 	}
7784 finish:
7785 	kmem_free(current, MSIZE);
7786 	return (rval);
7787 }
7788 
7789 
7790 static void
7791 st_init(struct scsi_tape *un)
7792 {
7793 	ASSERT(mutex_owned(ST_MUTEX));
7794 
7795 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
7796 	"st_init(): dev = 0x%lx, will reset fileno, blkno, eof\n", un->un_dev);
7797 
7798 	un->un_blkno = 0;
7799 	un->un_fileno = 0;
7800 	un->un_lastop = ST_OP_NIL;
7801 	un->un_eof = ST_NO_EOF;
7802 	un->un_pwr_mgmt = ST_PWR_NORMAL;
7803 	if (st_error_level != SCSI_ERR_ALL) {
7804 		if (DEBUGGING) {
7805 			st_error_level = SCSI_ERR_ALL;
7806 		} else {
7807 			st_error_level = SCSI_ERR_RETRYABLE;
7808 		}
7809 	}
7810 }
7811 
7812 
7813 static void
7814 st_make_cmd(struct scsi_tape *un, struct buf *bp, int (*func)(caddr_t))
7815 {
7816 	struct scsi_pkt *pkt;
7817 	struct uscsi_cmd *ucmd;
7818 	int count, tval = 0;
7819 	int flags = 0;
7820 	uchar_t com;
7821 	char fixbit;
7822 
7823 	ASSERT(mutex_owned(ST_MUTEX));
7824 
7825 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
7826 	    "st_make_cmd(): dev = 0x%lx\n", un->un_dev);
7827 
7828 
7829 	/*
7830 	 * fixbit is for setting the Fixed Mode and Suppress Incorrect
7831 	 * Length Indicator bits on read/write commands, for setting
7832 	 * the Long bit on erase commands, and for setting the Code
7833 	 * Field bits on space commands.
7834 	 * XXX why do we set lastop here?
7835 	 */
7836 
7837 	if (bp != un->un_sbufp) {		/* regular raw I/O */
7838 		int stat_size = (un->un_arq_enabled ?
7839 			sizeof (struct scsi_arq_status) : 1);
7840 		pkt = scsi_init_pkt(ROUTE, NULL, bp,
7841 		    CDB_GROUP0, stat_size, 0, 0, func, (caddr_t)un);
7842 		if (pkt == NULL) {
7843 			goto exit;
7844 		}
7845 		SET_BP_PKT(bp, pkt);
7846 		if (un->un_bsize == 0) {
7847 			count = bp->b_bcount;
7848 			fixbit = 0;
7849 		} else {
7850 			count = bp->b_bcount / un->un_bsize;
7851 			fixbit = 1;
7852 		}
7853 		if (bp->b_flags & B_READ) {
7854 			com = SCMD_READ;
7855 			un->un_lastop = ST_OP_READ;
7856 			if ((un->un_bsize == 0) && /* Not Fixed Block */
7857 			    (un->un_dp->options & ST_READ_IGNORE_ILI)) {
7858 				fixbit = 2;
7859 			}
7860 		} else {
7861 			com = SCMD_WRITE;
7862 			un->un_lastop = ST_OP_WRITE;
7863 		}
7864 
7865 		tval = un->un_dp->io_timeout;
7866 
7867 		/*
7868 		 * For really large xfers, increase timeout
7869 		 */
7870 		if (bp->b_bcount > (10 * ONE_MEG))
7871 			tval *= bp->b_bcount/(10 * ONE_MEG);
7872 
7873 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7874 		    "%s %ld amt 0x%lx\n", (com == SCMD_WRITE) ?
7875 		    wr_str: rd_str, un->un_blkno, bp->b_bcount);
7876 
7877 	} else if ((ucmd = BP_UCMD(bp)) != NULL) {
7878 		/*
7879 		 * uscsi - build command, allocate scsi resources
7880 		 */
7881 		st_make_uscsi_cmd(un, ucmd, bp, func);
7882 		goto exit;
7883 
7884 	} else {				/* special I/O */
7885 		struct buf *allocbp = NULL;
7886 		int stat_size = (un->un_arq_enabled ?
7887 			sizeof (struct scsi_arq_status) : 1);
7888 
7889 
7890 		com = (uchar_t)(uintptr_t)bp->b_forw;
7891 		count = bp->b_bcount;
7892 
7893 		switch (com) {
7894 		case SCMD_READ:
7895 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7896 			    "special read %d\n", count);
7897 			if (un->un_bsize == 0) {
7898 				fixbit = 2;	/* suppress SILI */
7899 			} else {
7900 				fixbit = 1;	/* Fixed Block Mode */
7901 				count /= un->un_bsize;
7902 			}
7903 			allocbp = bp;
7904 			un->un_lastop = ST_OP_READ;
7905 			tval = un->un_dp->io_timeout;
7906 			break;
7907 
7908 		case SCMD_WRITE:
7909 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7910 			    "special write %d\n", count);
7911 			if (un->un_bsize != 0) {
7912 				fixbit = 1;	/* Fixed Block Mode */
7913 				count /= un->un_bsize;
7914 			} else {
7915 				fixbit = 0;
7916 			}
7917 			allocbp = bp;
7918 			un->un_lastop = ST_OP_WRITE;
7919 			tval = un->un_dp->io_timeout;
7920 			break;
7921 
7922 		case SCMD_WRITE_FILE_MARK:
7923 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7924 			    "write %d file marks\n", count);
7925 			un->un_lastop = ST_OP_WEOF;
7926 			fixbit = 0;
7927 			tval = un->un_dp->io_timeout;
7928 			break;
7929 
7930 		case SCMD_REWIND:
7931 			if (bp->b_flags & B_ASYNC) {
7932 				fixbit = 1;
7933 			} else {
7934 				fixbit = 0;
7935 			}
7936 			count = 0;
7937 			un->un_lastop = ST_OP_CTL;
7938 			tval = un->un_dp->rewind_timeout;
7939 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7940 			    "rewind\n");
7941 			break;
7942 
7943 		case SCMD_SPACE:
7944 			fixbit = Isfmk(count);
7945 			count = (int)space_cnt(count);
7946 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7947 			    "space %d %s from file %d blk %ld\n",
7948 			    count, (fixbit) ? "filemarks" : "records",
7949 			    un->un_fileno, un->un_blkno);
7950 			un->un_lastop = ST_OP_CTL;
7951 			tval = un->un_dp->space_timeout;
7952 			break;
7953 
7954 		case SCMD_LOAD:
7955 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7956 			    "%s tape\n", (count & 1) ? "load" : "unload");
7957 			fixbit = 0;
7958 
7959 			/* Loading or Unloading */
7960 			if (count & 1) {
7961 				tval = un->un_dp->load_timeout;
7962 			} else {
7963 				tval = un->un_dp->unload_timeout;
7964 			}
7965 			/* Is Retension requested */
7966 			if (count & 2) {
7967 				tval += un->un_dp->rewind_timeout;
7968 			}
7969 			un->un_lastop = ST_OP_CTL;
7970 			break;
7971 
7972 		case SCMD_ERASE:
7973 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7974 			    "erase tape\n");
7975 			count = 0;
7976 			/*
7977 			 * We support long erase only
7978 			 */
7979 			fixbit = 1;
7980 			tval = un->un_dp->erase_timeout;
7981 			un->un_lastop = ST_OP_CTL;
7982 			break;
7983 
7984 		case SCMD_MODE_SENSE:
7985 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7986 			    "mode sense\n");
7987 			allocbp = bp;
7988 			fixbit = 0;
7989 			tval = un->un_dp->non_motion_timeout;
7990 			break;
7991 
7992 		case SCMD_MODE_SELECT:
7993 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
7994 			    "mode select\n");
7995 			allocbp = bp;
7996 			fixbit = 0;
7997 			tval = un->un_dp->non_motion_timeout;
7998 			break;
7999 
8000 		case SCMD_RESERVE:
8001 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8002 			    "reserve\n");
8003 			fixbit = 0;
8004 			tval = un->un_dp->non_motion_timeout;
8005 			break;
8006 
8007 		case SCMD_RELEASE:
8008 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8009 			    "release\n");
8010 			fixbit = 0;
8011 			tval = un->un_dp->non_motion_timeout;
8012 			break;
8013 
8014 		case SCMD_READ_BLKLIM:
8015 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8016 			    "read block limits\n");
8017 			allocbp = bp;
8018 			fixbit = count = 0;
8019 			tval = un->un_dp->non_motion_timeout;
8020 			break;
8021 
8022 		case SCMD_TEST_UNIT_READY:
8023 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8024 			    "test unit ready\n");
8025 			fixbit = 0;
8026 			tval = un->un_dp->non_motion_timeout;
8027 			break;
8028 		default:
8029 			ST_DEBUG(ST_DEVINFO, st_label, CE_PANIC,
8030 			    "Unhandled scsi command 0x%x in st_make_cmd()\n",
8031 			    com);
8032 		}
8033 		pkt = scsi_init_pkt(ROUTE, NULL, allocbp,
8034 			CDB_GROUP0, stat_size, 0, 0, func, (caddr_t)un);
8035 		if (pkt == NULL) {
8036 			goto exit;
8037 		}
8038 		if (allocbp)
8039 			ASSERT(geterror(allocbp) == 0);
8040 
8041 	}
8042 
8043 	(void) scsi_setup_cdb((union scsi_cdb *)pkt->pkt_cdbp,
8044 	    com, 0, (uint_t)count, 0);
8045 	FILL_SCSI1_LUN(un->un_sd, pkt);
8046 	/*
8047 	 * Initialize the SILI/Fixed bits of the byte 1 of cdb.
8048 	 */
8049 	((union scsi_cdb *)(pkt->pkt_cdbp))->t_code = fixbit;
8050 	pkt->pkt_flags = flags;
8051 
8052 	/*
8053 	 * If ST_SHORT_FILEMARKS bit is ON for EXABYTE
8054 	 * device, set the Vendor Unique bit to
8055 	 * write Short File Mark.
8056 	 */
8057 	if (com == SCMD_WRITE_FILE_MARK &&
8058 		un->un_dp->options & ST_SHORT_FILEMARKS) {
8059 		switch (un->un_dp->type) {
8060 		case ST_TYPE_EXB8500:
8061 		case ST_TYPE_EXABYTE:
8062 			/*
8063 			 * Now the Vendor Unique bit 7 in Byte 5 of CDB
8064 			 * is set to to write Short File Mark
8065 			 */
8066 			((union scsi_cdb *)pkt->pkt_cdbp)->g0_vu_1 = 1;
8067 			break;
8068 
8069 		default:
8070 			/*
8071 			 * Well, if ST_SHORT_FILEMARKS is set for other
8072 			 * tape drives, it is just ignored
8073 			 */
8074 			break;
8075 		}
8076 	}
8077 	ASSERT(tval);
8078 	pkt->pkt_time = tval;
8079 	pkt->pkt_comp = st_intr;
8080 	pkt->pkt_private = (opaque_t)bp;
8081 	SET_BP_PKT(bp, pkt);
8082 
8083 exit:
8084 	ASSERT(mutex_owned(ST_MUTEX));
8085 }
8086 
8087 
8088 /*
8089  * Build a command based on a uscsi command;
8090  */
8091 static void
8092 st_make_uscsi_cmd(struct scsi_tape *un, struct uscsi_cmd *ucmd,
8093     struct buf *bp, int (*func)(caddr_t))
8094 {
8095 	struct scsi_pkt *pkt;
8096 	caddr_t cdb;
8097 	int	cdblen;
8098 	int stat_size;
8099 
8100 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8101 	    "st_make_uscsi_cmd(): dev = 0x%lx\n", un->un_dev);
8102 
8103 	if (ucmd->uscsi_flags & USCSI_RQENABLE) {
8104 		stat_size = (un->un_arq_enabled ?
8105 		    sizeof (struct scsi_arq_status) : 1);
8106 	} else {
8107 		stat_size = 1;
8108 	}
8109 
8110 	ASSERT(mutex_owned(ST_MUTEX));
8111 
8112 	un->un_lastop = ST_OP_CTL;	/* usual */
8113 
8114 	cdb = ucmd->uscsi_cdb;
8115 	cdblen = ucmd->uscsi_cdblen;
8116 
8117 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8118 	    "st_make_uscsi_cmd: buflen=%ld bcount=%ld\n",
8119 		ucmd->uscsi_buflen, bp->b_bcount);
8120 	pkt = scsi_init_pkt(ROUTE, NULL,
8121 		(bp->b_bcount > 0) ? bp : NULL,
8122 		cdblen, stat_size, 0, 0, func, (caddr_t)un);
8123 	if (pkt == NULL) {
8124 		goto exit;
8125 	}
8126 
8127 	bcopy(cdb, pkt->pkt_cdbp, (uint_t)cdblen);
8128 
8129 #ifdef STDEBUG
8130 	if (st_debug >= 6) {
8131 		st_clean_print(ST_DEVINFO, st_label, SCSI_DEBUG,
8132 		    "pkt_cdbp", (char *)cdb, cdblen);
8133 	}
8134 #endif
8135 
8136 	if (ucmd->uscsi_flags & USCSI_SILENT) {
8137 		pkt->pkt_flags |= FLAG_SILENT;
8138 	}
8139 
8140 	pkt->pkt_time = ucmd->uscsi_timeout;
8141 	pkt->pkt_comp = st_intr;
8142 	pkt->pkt_private = (opaque_t)bp;
8143 	SET_BP_PKT(bp, pkt);
8144 exit:
8145 	ASSERT(mutex_owned(ST_MUTEX));
8146 }
8147 
8148 
8149 /*
8150  * restart cmd currently at the head of the runq
8151  *
8152  * If scsi_transport() succeeds or the retries
8153  * count exhausted, restore the throttle that was
8154  * zeroed out in st_handle_intr_busy().
8155  *
8156  */
8157 static void
8158 st_intr_restart(void *arg)
8159 {
8160 	struct scsi_tape *un = arg;
8161 	struct buf *bp;
8162 	int status = TRAN_ACCEPT;
8163 
8164 	mutex_enter(ST_MUTEX);
8165 
8166 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8167 		"st_intr_restart(), un = 0x%p\n", (void *)un);
8168 
8169 	un->un_hib_tid = 0;
8170 
8171 	/*
8172 	 * move from waitq to runq, if there is anything on the waitq
8173 	 */
8174 	if ((bp = un->un_quef) == NULL) {
8175 		mutex_exit(ST_MUTEX);
8176 		return;
8177 	}
8178 
8179 	/*
8180 	 * Here we know :
8181 	 *	throttle = 0, via st_handle_intr_busy
8182 	 */
8183 
8184 	if (un->un_quel == bp) {
8185 		un->un_quel = NULL;
8186 		un->un_quef = NULL;	/* we know it's the first one */
8187 	} else {
8188 		un->un_quef = bp->b_actf;
8189 	}
8190 	bp->b_actf = NULL;
8191 
8192 	if (un->un_runqf) {
8193 		/*
8194 		 * not good, we don't want to requeue something after
8195 		 * another.
8196 		 */
8197 		mutex_exit(ST_MUTEX);
8198 		goto done_error;
8199 	} else {
8200 		un->un_runqf = bp;
8201 		un->un_runql = bp;
8202 	}
8203 
8204 	ST_DO_KSTATS(bp, kstat_waitq_to_runq);
8205 
8206 	mutex_exit(ST_MUTEX);
8207 
8208 	status = scsi_transport(BP_PKT(bp));
8209 
8210 	mutex_enter(ST_MUTEX);
8211 
8212 	if (status != TRAN_ACCEPT) {
8213 		ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
8214 		mutex_exit(ST_MUTEX);
8215 
8216 		if (status == TRAN_BUSY) {
8217 			if (st_handle_intr_busy(un, bp,
8218 			    ST_TRAN_BUSY_TIMEOUT) == 0)
8219 				return;	/* timeout is setup again */
8220 		}
8221 
8222 	} else {
8223 		un->un_tran_retry_ct = 0;
8224 		if (un->un_last_throttle) {
8225 			un->un_throttle = un->un_last_throttle;
8226 		}
8227 		mutex_exit(ST_MUTEX);
8228 		return;
8229 	}
8230 
8231 done_error:
8232 	ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
8233 	    "restart transport rejected\n");
8234 	bp->b_resid = bp->b_bcount;
8235 
8236 #ifndef __lock_lint
8237 	/*
8238 	 * warlock doesn't understand this potential
8239 	 * recursion?
8240 	 */
8241 	mutex_enter(ST_MUTEX);
8242 	if (un->un_last_throttle) {
8243 		un->un_throttle = un->un_last_throttle;
8244 	}
8245 	if (status != TRAN_ACCEPT)
8246 		ST_DO_ERRSTATS(un, st_transerrs);
8247 	ST_DO_KSTATS(bp, kstat_waitq_exit);
8248 	SET_PE_FLAG(un);
8249 	st_bioerror(bp, EIO);
8250 	st_done_and_mutex_exit(un, bp);
8251 #endif
8252 	ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
8253 	    "busy restart aborted\n");
8254 }
8255 
8256 /*
8257  * st_check_media():
8258  * Periodically check the media state using scsi_watch service;
8259  * this service calls back after TUR and possibly request sense
8260  * the callback handler (st_media_watch_cb()) decodes the request sense
8261  * data (if any)
8262  */
8263 
8264 static int
8265 st_check_media(dev_t dev, enum mtio_state state)
8266 {
8267 	int rval = 0;
8268 	enum mtio_state	prev_state;
8269 	opaque_t token = NULL;
8270 
8271 	GET_SOFT_STATE(dev);
8272 
8273 	mutex_enter(ST_MUTEX);
8274 
8275 	ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8276 	    "st_check_media:state=%x, mediastate=%x\n",
8277 	    state, un->un_mediastate);
8278 
8279 	prev_state = un->un_mediastate;
8280 
8281 	/*
8282 	 * is there anything to do?
8283 	 */
8284 retry:
8285 	if (state == un->un_mediastate || un->un_mediastate == MTIO_NONE) {
8286 		/*
8287 		 * submit the request to the scsi_watch service;
8288 		 * scsi_media_watch_cb() does the real work
8289 		 */
8290 		mutex_exit(ST_MUTEX);
8291 		token = scsi_watch_request_submit(ST_SCSI_DEVP,
8292 			st_check_media_time, SENSE_LENGTH,
8293 			st_media_watch_cb, (caddr_t)dev);
8294 		if (token == NULL) {
8295 			rval = EAGAIN;
8296 			goto done;
8297 		}
8298 		mutex_enter(ST_MUTEX);
8299 
8300 		un->un_swr_token = token;
8301 		un->un_specified_mediastate = state;
8302 
8303 		/*
8304 		 * now wait for media change
8305 		 * we will not be signalled unless mediastate == state but it
8306 		 * still better to test for this condition, since there
8307 		 * is a 5 sec cv_broadcast delay when
8308 		 *  mediastate == MTIO_INSERTED
8309 		 */
8310 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8311 			"st_check_media:waiting for media state change\n");
8312 		while (un->un_mediastate == state) {
8313 			if (cv_wait_sig(&un->un_state_cv, ST_MUTEX) == 0) {
8314 				mutex_exit(ST_MUTEX);
8315 				ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8316 				    "st_check_media:waiting for media state "
8317 				    "was interrupted\n");
8318 				rval = EINTR;
8319 				goto done;
8320 			}
8321 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8322 			    "st_check_media:received signal, state=%x\n",
8323 			    un->un_mediastate);
8324 		}
8325 	}
8326 
8327 	/*
8328 	 * if we transitioned to MTIO_INSERTED, media has really been
8329 	 * inserted.  If TUR fails, it is probably a exabyte slow spin up.
8330 	 * Reset and retry the state change.  If everything is ok, replay
8331 	 * the open() logic.
8332 	 */
8333 	if ((un->un_mediastate == MTIO_INSERTED) &&
8334 	    (un->un_state == ST_STATE_OFFLINE)) {
8335 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8336 		    "st_check_media: calling st_cmd to confirm inserted\n");
8337 
8338 		/*
8339 		 * set this early so that TUR will make it through strategy
8340 		 * without triggering a st_tape_init().  We needed it set
8341 		 * before calling st_tape_init() ourselves anyway.  If TUR
8342 		 * fails, set it back
8343 		 */
8344 		un->un_state = ST_STATE_INITIALIZING;
8345 
8346 		/*
8347 		 * If not reserved fail as getting reservation conflict
8348 		 * will make this hang forever.
8349 		 */
8350 		if ((un->un_rsvd_status &
8351 		    (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) {
8352 			mutex_exit(ST_MUTEX);
8353 			rval = EACCES;
8354 			goto done;
8355 		}
8356 		rval = st_cmd(dev, SCMD_TEST_UNIT_READY, 0, SYNC_CMD);
8357 		if (rval == EACCES) {
8358 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8359 			    "st_check_media: TUR got Reservation Conflict\n");
8360 			mutex_exit(ST_MUTEX);
8361 			goto done;
8362 		}
8363 		if (rval) {
8364 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8365 			    "st_check_media: TUR failed, going to retry\n");
8366 			un->un_mediastate = prev_state;
8367 			un->un_state = ST_STATE_OFFLINE;
8368 			goto retry;
8369 		}
8370 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8371 		    "st_check_media: media inserted\n");
8372 
8373 		/* this also rewinds the tape */
8374 		rval = st_tape_init(dev);
8375 		if (rval != 0) {
8376 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8377 			    "st_check_media : OFFLINE init failure ");
8378 			un->un_state = ST_STATE_OFFLINE;
8379 			un->un_fileno = -1;
8380 		} else {
8381 			un->un_state = ST_STATE_OPEN_PENDING_IO;
8382 			un->un_fileno = 0;
8383 			un->un_blkno = 0;
8384 		}
8385 	} else if ((un->un_mediastate == MTIO_EJECTED) &&
8386 		(un->un_state != ST_STATE_OFFLINE)) {
8387 		/*
8388 		 * supported devices must be rewound before ejection
8389 		 * rewind resets fileno & blkno
8390 		 */
8391 		un->un_laststate = un->un_state;
8392 		un->un_state = ST_STATE_OFFLINE;
8393 	}
8394 	mutex_exit(ST_MUTEX);
8395 done:
8396 	if (token) {
8397 		(void) scsi_watch_request_terminate(token,
8398 				SCSI_WATCH_TERMINATE_WAIT);
8399 		mutex_enter(ST_MUTEX);
8400 		un->un_swr_token = (opaque_t)NULL;
8401 		mutex_exit(ST_MUTEX);
8402 	}
8403 
8404 	ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_media: done\n");
8405 
8406 	return (rval);
8407 }
8408 
8409 /*
8410  * st_media_watch_cb() is called by scsi_watch_thread for
8411  * verifying the request sense data (if any)
8412  */
8413 static int
8414 st_media_watch_cb(caddr_t arg, struct scsi_watch_result *resultp)
8415 {
8416 	struct scsi_status *statusp = resultp->statusp;
8417 	struct scsi_extended_sense *sensep = resultp->sensep;
8418 	uchar_t actual_sense_length = resultp->actual_sense_length;
8419 	struct scsi_tape *un;
8420 	enum mtio_state state = MTIO_NONE;
8421 	int instance;
8422 	dev_t dev = (dev_t)arg;
8423 
8424 	instance = MTUNIT(dev);
8425 	if ((un = ddi_get_soft_state(st_state, instance)) == NULL) {
8426 		return (-1);
8427 	}
8428 
8429 	mutex_enter(ST_MUTEX);
8430 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8431 		"st_media_watch_cb: status=%x, sensep=%p, len=%x\n",
8432 			*((char *)statusp), (void *)sensep,
8433 			actual_sense_length);
8434 
8435 	/*
8436 	 * if there was a check condition then sensep points to valid
8437 	 * sense data
8438 	 * if status was not a check condition but a reservation or busy
8439 	 * status then the new state is MTIO_NONE
8440 	 */
8441 	if (sensep) {
8442 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8443 		    "st_media_watch_cb: KEY=%x, ASC=%x, ASCQ=%x\n",
8444 		    sensep->es_key, sensep->es_add_code, sensep->es_qual_code);
8445 
8446 		switch (un->un_dp->type) {
8447 		default:
8448 			ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8449 	    "st_media_watch_cb: unknown drive type %d, default to ST_TYPE_HP\n",
8450 	    un->un_dp->type);
8451 		/* FALLTHROUGH */
8452 
8453 		case ST_TYPE_STC3490:	/* STK 4220 1/2" cartridge */
8454 		case ST_TYPE_FUJI:	/* 1/2" cartridge */
8455 		case ST_TYPE_HP:	/* HP 88780 1/2" reel */
8456 			if (un->un_dp->type == ST_TYPE_FUJI) {
8457 				ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8458 				    "st_media_watch_cb: ST_TYPE_FUJI\n");
8459 			} else {
8460 				ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8461 				    "st_media_watch_cb: ST_TYPE_HP\n");
8462 			}
8463 			switch (sensep->es_key) {
8464 			case KEY_UNIT_ATTENTION:
8465 				/* not ready to ready transition */
8466 				/* hp/es_qual_code == 80 on>off>on */
8467 				/* hp/es_qual_code == 0 on>off>unld>ld>on */
8468 				if (sensep->es_add_code == 0x28) {
8469 					state = MTIO_INSERTED;
8470 				}
8471 				break;
8472 			case KEY_NOT_READY:
8473 				/* in process, rewinding or loading */
8474 				if ((sensep->es_add_code == 0x04) &&
8475 				    (sensep->es_qual_code == 0x00)) {
8476 					state = MTIO_EJECTED;
8477 				}
8478 				break;
8479 			}
8480 			break;
8481 
8482 		case ST_TYPE_EXB8500:	/* Exabyte 8500 */
8483 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8484 			    "st_media_watch_cb: ST_TYPE_EXB8500\n");
8485 			switch (sensep->es_key) {
8486 			case KEY_UNIT_ATTENTION:
8487 				/* operator medium removal request */
8488 				if ((sensep->es_add_code == 0x5a) &&
8489 				    (sensep->es_qual_code == 0x01)) {
8490 					state = MTIO_EJECTED;
8491 				/* not ready to ready transition */
8492 				} else if ((sensep->es_add_code == 0x28) &&
8493 				    (sensep->es_qual_code == 0x00)) {
8494 					state = MTIO_INSERTED;
8495 				}
8496 				break;
8497 			case KEY_NOT_READY:
8498 				/* medium not present */
8499 				if (sensep->es_add_code == 0x3a) {
8500 					state = MTIO_EJECTED;
8501 				}
8502 				break;
8503 			}
8504 			break;
8505 		case ST_TYPE_EXABYTE:	/* Exabyte 8200 */
8506 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8507 			    "st_media_watch_cb: ST_TYPE_EXABYTE\n");
8508 			switch (sensep->es_key) {
8509 			case KEY_NOT_READY:
8510 				if ((sensep->es_add_code == 0x04) &&
8511 				    (sensep->es_qual_code == 0x00)) {
8512 					/* volume not mounted? */
8513 					state = MTIO_EJECTED;
8514 				} else if (sensep->es_add_code == 0x3a) {
8515 					state = MTIO_EJECTED;
8516 				}
8517 				break;
8518 			case KEY_UNIT_ATTENTION:
8519 				state = MTIO_EJECTED;
8520 				break;
8521 			}
8522 			break;
8523 
8524 		case ST_TYPE_DLT:		/* quantum DLT4xxx */
8525 			switch (sensep->es_key) {
8526 			case KEY_UNIT_ATTENTION:
8527 				if (sensep->es_add_code == 0x28) {
8528 					state = MTIO_INSERTED;
8529 				}
8530 				break;
8531 			case KEY_NOT_READY:
8532 				if (sensep->es_add_code == 0x04) {
8533 					/* in transition but could be either */
8534 					state = un->un_specified_mediastate;
8535 				} else if ((sensep->es_add_code == 0x3a) &&
8536 				    (sensep->es_qual_code == 0x00)) {
8537 					state = MTIO_EJECTED;
8538 				}
8539 				break;
8540 			}
8541 			break;
8542 		}
8543 	} else if (*((char *)statusp) == STATUS_GOOD) {
8544 		state = MTIO_INSERTED;
8545 	}
8546 
8547 	ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8548 	    "st_media_watch_cb:state=%x, specified=%x\n",
8549 	    state, un->un_specified_mediastate);
8550 
8551 	/*
8552 	 * now signal the waiting thread if this is *not* the specified state;
8553 	 * delay the signal if the state is MTIO_INSERTED
8554 	 * to allow the target to recover
8555 	 */
8556 	if (state != un->un_specified_mediastate) {
8557 		un->un_mediastate = state;
8558 		if (state == MTIO_INSERTED) {
8559 			/*
8560 			 * delay the signal to give the drive a chance
8561 			 * to do what it apparently needs to do
8562 			 */
8563 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8564 			    "st_media_watch_cb:delayed cv_broadcast\n");
8565 			un->un_delay_tid = timeout(st_delayed_cv_broadcast,
8566 			    un, drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
8567 		} else {
8568 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
8569 			    "st_media_watch_cb:immediate cv_broadcast\n");
8570 			cv_broadcast(&un->un_state_cv);
8571 		}
8572 	}
8573 	mutex_exit(ST_MUTEX);
8574 	return (0);
8575 }
8576 
8577 /*
8578  * delayed cv_broadcast to allow for target to recover
8579  * from media insertion
8580  */
8581 static void
8582 st_delayed_cv_broadcast(void *arg)
8583 {
8584 	struct scsi_tape *un = arg;
8585 
8586 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8587 	    "st_delayed_cv_broadcast:delayed cv_broadcast\n");
8588 
8589 	mutex_enter(ST_MUTEX);
8590 	cv_broadcast(&un->un_state_cv);
8591 	mutex_exit(ST_MUTEX);
8592 }
8593 
8594 /*
8595  * restart cmd currently at the start of the waitq
8596  */
8597 static void
8598 st_start_restart(void *arg)
8599 {
8600 	struct scsi_tape *un = arg;
8601 
8602 	ASSERT(un != NULL);
8603 
8604 	mutex_enter(ST_MUTEX);
8605 
8606 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8607 		"st_tran_restart()\n");
8608 
8609 	if (un->un_quef) {
8610 		st_start(un);
8611 	}
8612 
8613 	mutex_exit(ST_MUTEX);
8614 }
8615 
8616 
8617 /*
8618  * Command completion processing
8619  *
8620  */
8621 static void
8622 st_intr(struct scsi_pkt *pkt)
8623 {
8624 	struct scsi_tape *un;
8625 	struct buf *last_runqf;
8626 	struct buf *bp;
8627 	int action = COMMAND_DONE;
8628 	clock_t	timout;
8629 	int	status;
8630 
8631 	bp = (struct buf *)pkt->pkt_private;
8632 	un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev));
8633 
8634 	mutex_enter(ST_MUTEX);
8635 
8636 	un->un_rqs_state &= ~(ST_RQS_ERROR);
8637 
8638 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_intr()\n");
8639 
8640 	if (pkt->pkt_reason != CMD_CMPLT) {
8641 
8642 		/* If device has gone away not much else to do */
8643 		if (pkt->pkt_reason == CMD_DEV_GONE) {
8644 			action = COMMAND_DONE_ERROR;
8645 		} else if (un->un_state == ST_STATE_SENSING) {
8646 			ST_DO_ERRSTATS(un, st_transerrs);
8647 			action = COMMAND_DONE_ERROR;
8648 		} else {
8649 			action = st_handle_incomplete(un, bp);
8650 		}
8651 	/*
8652 	 * At this point we know that the command was successfully
8653 	 * completed. Now what?
8654 	 */
8655 	} else if (un->un_arq_enabled &&
8656 	    (pkt->pkt_state & STATE_ARQ_DONE)) {
8657 		/*
8658 		 * the transport layer successfully completed an autorqsense
8659 		 */
8660 		action = st_handle_autosense(un, bp);
8661 
8662 	} else if (un->un_state == ST_STATE_SENSING) {
8663 		/*
8664 		 * okay. We were running a REQUEST SENSE. Find
8665 		 * out what to do next.
8666 		 * some actions are based on un_state, hence
8667 		 * restore the state st was in before ST_STATE_SENSING.
8668 		 */
8669 		un->un_state = un->un_laststate;
8670 		action = st_handle_sense(un, bp);
8671 		/*
8672 		 * set pkt back to original packet in case we will have
8673 		 * to requeue it
8674 		 */
8675 		pkt = BP_PKT(bp);
8676 	} else  if ((SCBP(pkt)->sts_busy) || (SCBP(pkt)->sts_chk)) {
8677 		/*
8678 		 * Okay, we weren't running a REQUEST SENSE. Call a routine
8679 		 * to see if the status bits we're okay. If a request sense
8680 		 * is to be run, that will happen.
8681 		 */
8682 		action = st_check_error(un, pkt);
8683 	}
8684 
8685 	if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) {
8686 		switch (action) {
8687 			case QUE_COMMAND:
8688 				/*
8689 				 * return cmd to head to the queue
8690 				 * since we are suspending so that
8691 				 * it gets restarted during resume
8692 				 */
8693 				if (un->un_runqf) {
8694 					last_runqf = un->un_runqf;
8695 					un->un_runqf = bp;
8696 					bp->b_actf = last_runqf;
8697 				} else {
8698 					bp->b_actf = NULL;
8699 					un->un_runqf = bp;
8700 					un->un_runql = bp;
8701 				}
8702 				action = JUST_RETURN;
8703 				break;
8704 
8705 			case QUE_SENSE:
8706 				action = COMMAND_DONE_ERROR;
8707 				break;
8708 
8709 			default:
8710 				break;
8711 		}
8712 	}
8713 
8714 	/*
8715 	 * Restore old state if we were sensing.
8716 	 */
8717 	if (un->un_state == ST_STATE_SENSING && action != QUE_SENSE) {
8718 		un->un_state = un->un_laststate;
8719 	}
8720 
8721 	ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
8722 	    "st_intr: pkt=%p, bp=%p, action=%x, status=%x\n",
8723 	    (void *)pkt, (void *)bp, action, SCBP_C(pkt));
8724 
8725 
8726 	switch (action) {
8727 	case COMMAND_DONE_EACCES:
8728 		/* this is to report a reservation conflict */
8729 		st_bioerror(bp, EACCES);
8730 		ST_DEBUG(ST_DEVINFO, st_label, SCSI_DEBUG,
8731 			"Reservation Conflict \n");
8732 
8733 		/*FALLTHROUGH*/
8734 	case COMMAND_DONE_ERROR:
8735 		if (un->un_eof < ST_EOT_PENDING &&
8736 		    un->un_state >= ST_STATE_OPEN) {
8737 			/*
8738 			 * all errors set state of the tape to 'unknown'
8739 			 * unless we're at EOT or are doing append testing.
8740 			 * If sense key was illegal request, preserve state.
8741 			 */
8742 			if (un->un_status != KEY_ILLEGAL_REQUEST) {
8743 				un->un_fileno = -1;
8744 			}
8745 		}
8746 		un->un_err_resid = bp->b_resid = bp->b_bcount;
8747 		/*
8748 		 * since we have an error (COMMAND_DONE_ERROR), we want to
8749 		 * make sure an error ocurrs, so make sure at least EIO is
8750 		 * returned
8751 		 */
8752 		if (geterror(bp) == 0)
8753 			st_bioerror(bp, EIO);
8754 
8755 		SET_PE_FLAG(un);
8756 		if (!(un->un_rqs_state & ST_RQS_ERROR) &&
8757 		    (un->un_errno == EIO)) {
8758 			un->un_rqs_state &= ~(ST_RQS_VALID);
8759 		}
8760 		goto done;
8761 
8762 	case COMMAND_DONE_ERROR_RECOVERED:
8763 		un->un_err_resid = bp->b_resid = bp->b_bcount;
8764 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
8765 		    "st_intr(): COMMAND_DONE_ERROR_RECOVERED");
8766 		if (geterror(bp) == 0)
8767 			st_bioerror(bp, EIO);
8768 		SET_PE_FLAG(un);
8769 		if (!(un->un_rqs_state & ST_RQS_ERROR) &&
8770 		    (un->un_errno == EIO)) {
8771 			un->un_rqs_state &= ~(ST_RQS_VALID);
8772 		}
8773 		/*FALLTHROUGH*/
8774 	case COMMAND_DONE:
8775 		st_set_state(un);
8776 done:
8777 		ST_DO_KSTATS(bp, kstat_runq_exit);
8778 		st_done_and_mutex_exit(un, bp);
8779 		return;
8780 
8781 	case QUE_SENSE:
8782 		if ((un->un_ncmds > 1) && !un->un_flush_on_errors)
8783 			goto sense_error;
8784 
8785 		if (un->un_state != ST_STATE_SENSING) {
8786 			un->un_laststate = un->un_state;
8787 			un->un_state = ST_STATE_SENSING;
8788 		}
8789 
8790 		un->un_rqs->pkt_private = (opaque_t)bp;
8791 		bzero(ST_RQSENSE, SENSE_LENGTH);
8792 
8793 		if (un->un_throttle) {
8794 			un->un_last_throttle = un->un_throttle;
8795 			un->un_throttle = 0;
8796 		}
8797 
8798 		mutex_exit(ST_MUTEX);
8799 
8800 		/*
8801 		 * never retry this, some other command will have nuked the
8802 		 * sense, anyway
8803 		 */
8804 		status = scsi_transport(un->un_rqs);
8805 
8806 		mutex_enter(ST_MUTEX);
8807 
8808 		if (un->un_last_throttle) {
8809 			un->un_throttle = un->un_last_throttle;
8810 		}
8811 
8812 		if (status == TRAN_ACCEPT) {
8813 			mutex_exit(ST_MUTEX);
8814 			return;
8815 		}
8816 		if (status != TRAN_BUSY)
8817 			ST_DO_ERRSTATS(un, st_transerrs);
8818 sense_error:
8819 		un->un_fileno = -1;
8820 		st_bioerror(bp, EIO);
8821 		SET_PE_FLAG(un);
8822 		goto done;
8823 
8824 	case QUE_BUSY_COMMAND:
8825 		/* longish timeout */
8826 		timout = ST_STATUS_BUSY_TIMEOUT;
8827 		goto que_it_up;
8828 
8829 	case QUE_COMMAND:
8830 		/* short timeout */
8831 		timout = ST_TRAN_BUSY_TIMEOUT;
8832 que_it_up:
8833 		/*
8834 		 * let st_handle_intr_busy put this bp back on waitq and make
8835 		 * checks to see if it is ok to requeue the command.
8836 		 */
8837 		ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
8838 
8839 		/*
8840 		 * Save the throttle before setting up the timeout
8841 		 */
8842 		if (un->un_throttle) {
8843 			un->un_last_throttle = un->un_throttle;
8844 		}
8845 		mutex_exit(ST_MUTEX);
8846 		if (st_handle_intr_busy(un, bp, timout) == 0)
8847 			return;		/* timeout is setup again */
8848 
8849 		mutex_enter(ST_MUTEX);
8850 		un->un_fileno = -1;
8851 		un->un_err_resid = bp->b_resid = bp->b_bcount;
8852 		st_bioerror(bp, EIO);
8853 		SET_PE_FLAG(un);
8854 		goto done;
8855 
8856 	case QUE_LAST_COMMAND:
8857 
8858 		if ((un->un_ncmds > 1) && !un->un_flush_on_errors) {
8859 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
8860 			    "un_ncmds: %d can't retry cmd \n", un->un_ncmds);
8861 			goto last_command_error;
8862 		}
8863 		mutex_exit(ST_MUTEX);
8864 		if (st_handle_intr_retry_lcmd(un, bp) == 0)
8865 			return;
8866 		mutex_enter(ST_MUTEX);
8867 last_command_error:
8868 		un->un_err_resid = bp->b_resid = bp->b_bcount;
8869 		un->un_fileno = -1;
8870 		st_bioerror(bp, EIO);
8871 		SET_PE_FLAG(un);
8872 		goto done;
8873 
8874 	case JUST_RETURN:
8875 	default:
8876 		ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
8877 		mutex_exit(ST_MUTEX);
8878 		return;
8879 	}
8880 	/*NOTREACHED*/
8881 }
8882 
8883 static int
8884 st_handle_incomplete(struct scsi_tape *un, struct buf *bp)
8885 {
8886 	static char *fail = "SCSI transport failed: reason '%s': %s\n";
8887 	int rval = COMMAND_DONE_ERROR;
8888 	struct scsi_pkt *pkt = (un->un_state == ST_STATE_SENSING) ?
8889 			un->un_rqs : BP_PKT(bp);
8890 	int result;
8891 
8892 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
8893 		"st_handle_incomplete(): dev = 0x%lx\n", un->un_dev);
8894 
8895 	ASSERT(mutex_owned(ST_MUTEX));
8896 
8897 	switch (pkt->pkt_reason) {
8898 	case CMD_INCOMPLETE:	/* tran stopped with not normal state */
8899 		/*
8900 		 * this occurs when accessing a powered down drive, no
8901 		 * need to complain; just fail the open
8902 		 */
8903 #ifdef STDEBUG
8904 		if (st_debug >= 1) {
8905 			st_clean_print(ST_DEVINFO, st_label, CE_WARN,
8906 			    "Failed CDB", (char *)pkt->pkt_cdbp, CDB_SIZE);
8907 		}
8908 
8909 #endif
8910 		/*
8911 		 * if we have commands outstanding in HBA, and a command
8912 		 * comes back incomplete, we're hosed, so reset target
8913 		 * If we have the bus, but cmd_incomplete, we probably just
8914 		 * have a failed selection, so don't reset the target, just
8915 		 * requeue the command and try again
8916 		 */
8917 		if ((un->un_ncmds > 1) || (pkt->pkt_state != STATE_GOT_BUS)) {
8918 			goto reset_target;
8919 		}
8920 
8921 		/*
8922 		 * Retry selection a couple more times if we're
8923 		 * open.  If opening, we only try just once to
8924 		 * reduce probe time for nonexistant devices.
8925 		 */
8926 		if ((un->un_laststate > ST_STATE_OPENING) &&
8927 		    ((int)un->un_retry_ct < st_selection_retry_count)) {
8928 			rval = QUE_COMMAND;
8929 		}
8930 		ST_DO_ERRSTATS(un, st_transerrs);
8931 		break;
8932 
8933 	case CMD_ABORTED:
8934 		/*
8935 		 * most likely this is caused by flush-on-error support. If
8936 		 * it was not there, the we're in trouble.
8937 		 */
8938 		if (!un->un_flush_on_errors) {
8939 			un->un_status = SUN_KEY_FATAL;
8940 			goto reset_target;
8941 		}
8942 
8943 		st_set_pe_errno(un);
8944 		bioerror(bp, un->un_errno);
8945 		if (un->un_errno)
8946 			return (COMMAND_DONE_ERROR);
8947 		else
8948 			return (COMMAND_DONE);
8949 
8950 	case CMD_TIMEOUT:	/* Command timed out */
8951 		un->un_status = SUN_KEY_TIMEOUT;
8952 
8953 		/*FALLTHROUGH*/
8954 	default:
8955 reset_target:
8956 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
8957 		    "transport completed with %s\n",
8958 		    scsi_rname(pkt->pkt_reason));
8959 		ST_DO_ERRSTATS(un, st_transerrs);
8960 		if ((pkt->pkt_state & STATE_GOT_TARGET) &&
8961 		    ((pkt->pkt_statistics & (STAT_BUS_RESET | STAT_DEV_RESET |
8962 			STAT_ABORTED)) == 0)) {
8963 
8964 			/*
8965 			 * If we haven't reserved the drive don't reset it.
8966 			 */
8967 			if ((un->un_rsvd_status &
8968 			    (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) {
8969 				return (rval);
8970 			}
8971 
8972 			mutex_exit(ST_MUTEX);
8973 
8974 			result = scsi_reset(ROUTE, RESET_TARGET);
8975 			/*
8976 			 * if target reset fails, then pull the chain
8977 			 */
8978 			if (result == 0) {
8979 				result = scsi_reset(ROUTE, RESET_ALL);
8980 			}
8981 			mutex_enter(ST_MUTEX);
8982 
8983 			if ((result == 0) && (un->un_state >= ST_STATE_OPEN)) {
8984 				/* no hope left to recover */
8985 				scsi_log(ST_DEVINFO, st_label, CE_WARN,
8986 				    "recovery by resets failed\n");
8987 				return (rval);
8988 			}
8989 		}
8990 	}
8991 
8992 	if ((pkt->pkt_reason == CMD_RESET) || (pkt->pkt_statistics &
8993 		(STAT_BUS_RESET | STAT_DEV_RESET))) {
8994 		if ((un->un_rsvd_status & ST_RESERVE)) {
8995 			un->un_rsvd_status |= ST_LOST_RESERVE;
8996 			ST_DEBUG3(ST_DEVINFO, st_label, CE_WARN,
8997 				"Lost Reservation\n");
8998 		}
8999 	}
9000 
9001 	if ((int)un->un_retry_ct++ < st_retry_count) {
9002 		if (un->un_pwr_mgmt == ST_PWR_SUSPENDED) {
9003 			rval = QUE_COMMAND;
9004 		} else if (bp == un->un_sbufp) {
9005 			switch ((uchar_t)(uintptr_t)bp->b_forw) {
9006 			case SCMD_MODE_SENSE:
9007 			case SCMD_MODE_SELECT:
9008 			case SCMD_READ_BLKLIM:
9009 			case SCMD_REWIND:
9010 			case SCMD_LOAD:
9011 			case SCMD_TEST_UNIT_READY:
9012 				/*
9013 				 * These commands can be rerun with impunity
9014 				 */
9015 				rval = QUE_COMMAND;
9016 				break;
9017 
9018 			default:
9019 				break;
9020 			}
9021 		}
9022 	} else {
9023 		rval = COMMAND_DONE_ERROR;
9024 	}
9025 
9026 	if (un->un_state >= ST_STATE_OPEN) {
9027 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
9028 		    fail, scsi_rname(pkt->pkt_reason),
9029 		    (rval == COMMAND_DONE_ERROR)?
9030 		    "giving up" : "retrying command");
9031 	}
9032 	return (rval);
9033 }
9034 
9035 /*
9036  * if the device is busy, then put this bp back on the waitq, on the
9037  * interrupt thread, where we want the head of the queue and not the
9038  * end
9039  *
9040  * The callers of this routine should take measures to save the
9041  * un_throttle in un_last_throttle which will be restored in
9042  * st_intr_restart(). The only exception should be st_intr_restart()
9043  * calling this routine for which the saving is already done.
9044  */
9045 static int
9046 st_handle_intr_busy(struct scsi_tape *un, struct buf *bp,
9047 	clock_t timeout_interval)
9048 {
9049 	struct buf *last_quef;
9050 	int rval = 0;
9051 
9052 	mutex_enter(ST_MUTEX);
9053 
9054 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9055 	    "st_handle_intr_busy(), un = 0x%p\n", (void *)un);
9056 
9057 	/*
9058 	 * Check to see if we hit the retry timeout. We check to make sure
9059 	 * this is the first one on the runq and make sure we have not
9060 	 * queued up any more, so this one has to be the last on the list
9061 	 * also. If it is not, we have to fail.  If it is not the first, but
9062 	 * is the last we are in trouble anyway, as we are in the interrupt
9063 	 * context here.
9064 	 */
9065 	if (((int)un->un_tran_retry_ct++ > st_retry_count) ||
9066 	    ((un->un_runqf != bp) && (un->un_runql != bp))) {
9067 		rval = -1;
9068 		goto exit;
9069 	}
9070 
9071 	/* put the bp back on the waitq */
9072 	if (un->un_quef) {
9073 		last_quef = un->un_quef;
9074 		un->un_quef = bp;
9075 		bp->b_actf = last_quef;
9076 	} else  {
9077 		bp->b_actf = NULL;
9078 		un->un_quef = bp;
9079 		un->un_quel = bp;
9080 	}
9081 
9082 	/*
9083 	 * We know that this is the first and last on the runq at this time,
9084 	 * so we just nullify those two queues
9085 	 */
9086 	un->un_runqf = NULL;
9087 	un->un_runql = NULL;
9088 
9089 	/*
9090 	 * We don't want any other commands being started in the mean time.
9091 	 * If start had just released mutex after putting something on the
9092 	 * runq, we won't even get here.
9093 	 */
9094 	un->un_throttle = 0;
9095 
9096 	/*
9097 	 * send a marker pkt, if appropriate
9098 	 */
9099 	st_hba_unflush(un);
9100 
9101 	/*
9102 	 * all queues are aligned, we are just waiting to
9103 	 * transport
9104 	 */
9105 	un->un_hib_tid = timeout(st_intr_restart, un, timeout_interval);
9106 
9107 exit:
9108 	mutex_exit(ST_MUTEX);
9109 	return (rval);
9110 }
9111 
9112 static int
9113 st_handle_sense(struct scsi_tape *un, struct buf *bp)
9114 {
9115 	struct scsi_pkt *rqpkt = un->un_rqs;
9116 	int rval = COMMAND_DONE_ERROR;
9117 	int amt;
9118 
9119 	ASSERT(mutex_owned(ST_MUTEX));
9120 
9121 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9122 		"st_handle_sense()\n");
9123 
9124 	if (SCBP(rqpkt)->sts_busy) {
9125 		ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN,
9126 		    "busy unit on request sense\n");
9127 		if ((int)un->un_retry_ct++ < st_retry_count) {
9128 			rval = QUE_BUSY_COMMAND;
9129 		}
9130 		return (rval);
9131 	} else if (SCBP(rqpkt)->sts_chk) {
9132 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9133 		    "Check Condition on REQUEST SENSE\n");
9134 		return (rval);
9135 	}
9136 
9137 	/* was there enough data? */
9138 	amt = (int)SENSE_LENGTH - rqpkt->pkt_resid;
9139 	if ((rqpkt->pkt_state & STATE_XFERRED_DATA) == 0 ||
9140 	    (amt < SUN_MIN_SENSE_LENGTH)) {
9141 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9142 		    "REQUEST SENSE couldn't get sense data\n");
9143 		return (rval);
9144 	}
9145 	return (st_decode_sense(un, bp, amt, SCBP(rqpkt)));
9146 }
9147 
9148 static int
9149 st_handle_autosense(struct scsi_tape *un, struct buf *bp)
9150 {
9151 	struct scsi_pkt *pkt = BP_PKT(bp);
9152 	struct scsi_arq_status *arqstat =
9153 	    (struct scsi_arq_status *)pkt->pkt_scbp;
9154 	int rval = COMMAND_DONE_ERROR;
9155 	int amt;
9156 
9157 	ASSERT(mutex_owned(ST_MUTEX));
9158 
9159 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9160 		"st_handle_autosense()\n");
9161 
9162 	if (arqstat->sts_rqpkt_status.sts_busy) {
9163 		ST_DEBUG4(ST_DEVINFO, st_label, CE_WARN,
9164 		    "busy unit on request sense\n");
9165 		/*
9166 		 * we return QUE_SENSE so st_intr will setup the SENSE cmd.
9167 		 * the disadvantage is that we do not have any delay for the
9168 		 * second retry of rqsense and we have to keep a packet around
9169 		 */
9170 		return (QUE_SENSE);
9171 
9172 	} else if (arqstat->sts_rqpkt_reason != CMD_CMPLT) {
9173 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9174 		    "transport error on REQUEST SENSE\n");
9175 		if ((arqstat->sts_rqpkt_state & STATE_GOT_TARGET) &&
9176 		    ((arqstat->sts_rqpkt_statistics &
9177 		    (STAT_BUS_RESET | STAT_DEV_RESET | STAT_ABORTED)) == 0)) {
9178 			mutex_exit(ST_MUTEX);
9179 			if (scsi_reset(ROUTE, RESET_TARGET) == 0) {
9180 				/*
9181 				 * if target reset fails, then pull the chain
9182 				 */
9183 				if (scsi_reset(ROUTE, RESET_ALL) == 0) {
9184 					ST_DEBUG6(ST_DEVINFO, st_label,
9185 					    CE_WARN,
9186 					    "recovery by resets failed\n");
9187 				}
9188 			}
9189 			mutex_enter(ST_MUTEX);
9190 		}
9191 		return (rval);
9192 
9193 	} else if (arqstat->sts_rqpkt_status.sts_chk) {
9194 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9195 		    "Check Condition on REQUEST SENSE\n");
9196 		return (rval);
9197 	}
9198 
9199 
9200 	/* was there enough data? */
9201 	amt = (int)SENSE_LENGTH - arqstat->sts_rqpkt_resid;
9202 	if ((arqstat->sts_rqpkt_state & STATE_XFERRED_DATA) == 0 ||
9203 	    (amt < SUN_MIN_SENSE_LENGTH)) {
9204 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9205 		    "REQUEST SENSE couldn't get sense data\n");
9206 		return (rval);
9207 	}
9208 
9209 	bcopy(&arqstat->sts_sensedata, ST_RQSENSE, SENSE_LENGTH);
9210 
9211 	return (st_decode_sense(un, bp, amt, &arqstat->sts_rqpkt_status));
9212 }
9213 
9214 static int
9215 st_decode_sense(struct scsi_tape *un, struct buf *bp,  int amt,
9216 	struct scsi_status *statusp)
9217 {
9218 	struct scsi_pkt *pkt = BP_PKT(bp);
9219 	int rval = COMMAND_DONE_ERROR;
9220 	long resid;
9221 	struct scsi_extended_sense *sensep = ST_RQSENSE;
9222 	int severity;
9223 	int get_error;
9224 
9225 	ASSERT(mutex_owned(ST_MUTEX));
9226 
9227 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9228 		"st_decode_sense()\n");
9229 
9230 	/*
9231 	 * For uscsi commands, squirrel away a copy of the
9232 	 * results of the Request Sense.
9233 	 */
9234 	if (USCSI_CMD(bp)) {
9235 		struct uscsi_cmd *ucmd = BP_UCMD(bp);
9236 		ucmd->uscsi_rqstatus = *(uchar_t *)statusp;
9237 		if (ucmd->uscsi_rqlen && un->un_srqbufp) {
9238 			uchar_t rqlen = min((uchar_t)amt, ucmd->uscsi_rqlen);
9239 			ucmd->uscsi_rqresid = ucmd->uscsi_rqlen - rqlen;
9240 			bcopy(ST_RQSENSE, un->un_srqbufp, rqlen);
9241 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
9242 				"st_decode_sense: stat=0x%x resid=0x%x\n",
9243 				ucmd->uscsi_rqstatus, ucmd->uscsi_rqresid);
9244 		}
9245 	}
9246 
9247 	/*
9248 	 * If the drive is an MT-02, reposition the
9249 	 * secondary error code into the proper place.
9250 	 *
9251 	 * XXX	MT-02 is non-CCS tape, so secondary error code
9252 	 * is in byte 8.  However, in SCSI-2, tape has CCS definition
9253 	 * so it's in byte 12.
9254 	 */
9255 	if (un->un_dp->type == ST_TYPE_EMULEX) {
9256 		sensep->es_code = sensep->es_add_info[0];
9257 	}
9258 
9259 	/* for normal I/O check extract the resid values. */
9260 	if (bp != un->un_sbufp) {
9261 		if (sensep->es_valid) {
9262 			resid = (sensep->es_info_1 << 24) |
9263 				(sensep->es_info_2 << 16) |
9264 				(sensep->es_info_3 << 8)  |
9265 				(sensep->es_info_4);
9266 			if (un->un_bsize) {
9267 				resid *= un->un_bsize;
9268 			}
9269 		} else if (pkt->pkt_state & STATE_XFERRED_DATA) {
9270 			resid = pkt->pkt_resid;
9271 		} else {
9272 			resid = bp->b_bcount;
9273 		}
9274 		ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9275 		    "st_handle_sense (rw): xferred bit = %d, resid=%ld (%d), "
9276 		    "pkt_resid=%ld\n", pkt->pkt_state & STATE_XFERRED_DATA,
9277 		    resid,
9278 		    (sensep->es_info_1 << 24) |
9279 		    (sensep->es_info_2 << 16) |
9280 		    (sensep->es_info_3 << 8)  |
9281 		    (sensep->es_info_4),
9282 		    pkt->pkt_resid);
9283 		/*
9284 		 * The problem is, what should we believe?
9285 		 */
9286 		if (resid && (pkt->pkt_resid == 0)) {
9287 			pkt->pkt_resid = resid;
9288 		}
9289 	} else {
9290 		/*
9291 		 * If the command is SCMD_SPACE, we need to get the
9292 		 * residual as returned in the sense data, to adjust
9293 		 * our idea of current tape position correctly
9294 		 */
9295 		if ((CDBP(pkt)->scc_cmd == SCMD_SPACE ||
9296 		    CDBP(pkt)->scc_cmd == SCMD_WRITE_FILE_MARK) &&
9297 		    (sensep->es_valid)) {
9298 			resid = (sensep->es_info_1 << 24) |
9299 			    (sensep->es_info_2 << 16) |
9300 			    (sensep->es_info_3 << 8)  |
9301 			    (sensep->es_info_4);
9302 			bp->b_resid = resid;
9303 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9304 			    "st_handle_sense(other):	resid=%ld\n",
9305 			    resid);
9306 		} else {
9307 			/*
9308 			 * If the special command is SCMD_READ,
9309 			 * the correct resid will be set later.
9310 			 */
9311 			resid = bp->b_bcount;
9312 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9313 			    "st_handle_sense(special read):  resid=%ld\n",
9314 				resid);
9315 		}
9316 	}
9317 
9318 	if ((un->un_state >= ST_STATE_OPEN) &&
9319 	    (DEBUGGING || st_error_level == SCSI_ERR_ALL)) {
9320 		st_clean_print(ST_DEVINFO, st_label, CE_NOTE,
9321 		    "Failed CDB", (char *)pkt->pkt_cdbp, CDB_SIZE);
9322 		st_clean_print(ST_DEVINFO, st_label, CE_CONT,
9323 		    "sense data", (char *)sensep, amt);
9324 		scsi_log(ST_DEVINFO, st_label, CE_CONT,
9325 		    "count 0x%lx resid 0x%lx pktresid 0x%lx\n",
9326 		    bp->b_bcount, resid, pkt->pkt_resid);
9327 	}
9328 
9329 	switch (un->un_status = sensep->es_key) {
9330 	case KEY_NO_SENSE:
9331 		severity = SCSI_ERR_INFO;
9332 		goto common;
9333 
9334 	case KEY_RECOVERABLE_ERROR:
9335 		severity = SCSI_ERR_RECOVERED;
9336 		if ((sensep->es_class == CLASS_EXTENDED_SENSE) &&
9337 		    (sensep->es_code == ST_DEFERRED_ERROR)) {
9338 		    if (un->un_dp->options &
9339 			ST_RETRY_ON_RECOVERED_DEFERRED_ERROR) {
9340 			    rval = QUE_LAST_COMMAND;
9341 			    scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity,
9342 				un->un_blkno, un->un_err_blkno, scsi_cmds,
9343 				sensep);
9344 			    scsi_log(ST_DEVINFO, st_label, CE_CONT,
9345 				"Command will be retried\n");
9346 			} else {
9347 			    severity = SCSI_ERR_FATAL;
9348 			    rval = COMMAND_DONE_ERROR_RECOVERED;
9349 			    ST_DO_ERRSTATS(un, st_softerrs);
9350 			    scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity,
9351 				un->un_blkno, un->un_err_blkno, scsi_cmds,
9352 				sensep);
9353 			}
9354 			break;
9355 		}
9356 common:
9357 		/*
9358 		 * XXX only want reads to be stopped by filemarks.
9359 		 * Don't want them to be stopped by EOT.  EOT matters
9360 		 * only on write.
9361 		 */
9362 		if (sensep->es_filmk && !sensep->es_eom) {
9363 			rval = COMMAND_DONE;
9364 		} else if (sensep->es_eom) {
9365 			rval = COMMAND_DONE;
9366 		} else if (sensep->es_ili) {
9367 			/*
9368 			 * Fun with variable length record devices:
9369 			 * for specifying larger blocks sizes than the
9370 			 * actual physical record size.
9371 			 */
9372 			if (un->un_bsize == 0 && resid > 0) {
9373 				/*
9374 				 * XXX! Ugly.
9375 				 * The requested blocksize is > tape blocksize,
9376 				 * so this is ok, so we just return the
9377 				 * actual size xferred.
9378 				 */
9379 				pkt->pkt_resid = resid;
9380 				rval = COMMAND_DONE;
9381 			} else if (un->un_bsize == 0 && resid < 0) {
9382 				/*
9383 				 * The requested blocksize is < tape blocksize,
9384 				 * so this is not ok, so we err with ENOMEM
9385 				 */
9386 				rval = COMMAND_DONE_ERROR_RECOVERED;
9387 				st_bioerror(bp, ENOMEM);
9388 			} else {
9389 				ST_DO_ERRSTATS(un, st_softerrs);
9390 				severity = SCSI_ERR_FATAL;
9391 				rval = COMMAND_DONE_ERROR;
9392 				st_bioerror(bp, EINVAL);
9393 			}
9394 		} else {
9395 			/*
9396 			 * we hope and pray for this just being
9397 			 * something we can ignore (ie. a
9398 			 * truly recoverable soft error)
9399 			 */
9400 			rval = COMMAND_DONE;
9401 		}
9402 		if (sensep->es_filmk) {
9403 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9404 			    "filemark\n");
9405 			un->un_status = SUN_KEY_EOF;
9406 			un->un_eof = ST_EOF_PENDING;
9407 			SET_PE_FLAG(un);
9408 		}
9409 
9410 		/*
9411 		 * ignore eom when reading, a fmk should terminate reading
9412 		 */
9413 		if ((sensep->es_eom) &&
9414 		    (CDBP(pkt)->scc_cmd != SCMD_READ)) {
9415 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "eom\n");
9416 			un->un_status = SUN_KEY_EOT;
9417 			un->un_eof = ST_EOM;
9418 			SET_PE_FLAG(un);
9419 		}
9420 
9421 		break;
9422 
9423 	case KEY_ILLEGAL_REQUEST:
9424 
9425 		if (un->un_laststate >= ST_STATE_OPEN) {
9426 			ST_DO_ERRSTATS(un, st_softerrs);
9427 			severity = SCSI_ERR_FATAL;
9428 		} else {
9429 			severity = SCSI_ERR_INFO;
9430 		}
9431 		break;
9432 
9433 	case KEY_MEDIUM_ERROR:
9434 		ST_DO_ERRSTATS(un, st_harderrs);
9435 		severity = SCSI_ERR_FATAL;
9436 
9437 		/*
9438 		 * for (buffered) writes, a medium error must be fatal
9439 		 */
9440 		if (CDBP(pkt)->scc_cmd != SCMD_WRITE) {
9441 			rval = COMMAND_DONE_ERROR_RECOVERED;
9442 		}
9443 
9444 check_keys:
9445 		/*
9446 		 * attempt to process the keys in the presence of
9447 		 * other errors
9448 		 */
9449 		if (sensep->es_ili && rval != COMMAND_DONE_ERROR) {
9450 			/*
9451 			 * Fun with variable length record devices:
9452 			 * for specifying larger blocks sizes than the
9453 			 * actual physical record size.
9454 			 */
9455 			if (un->un_bsize == 0 && resid > 0) {
9456 				/*
9457 				 * XXX! Ugly
9458 				 */
9459 				pkt->pkt_resid = resid;
9460 			} else if (un->un_bsize == 0 && resid < 0) {
9461 				st_bioerror(bp, EINVAL);
9462 			} else {
9463 				severity = SCSI_ERR_FATAL;
9464 				rval = COMMAND_DONE_ERROR;
9465 				st_bioerror(bp, EINVAL);
9466 			}
9467 		}
9468 		if (sensep->es_filmk) {
9469 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9470 			    "filemark\n");
9471 			un->un_status = SUN_KEY_EOF;
9472 			un->un_eof = ST_EOF_PENDING;
9473 			SET_PE_FLAG(un);
9474 		}
9475 
9476 		/*
9477 		 * ignore eom when reading, a fmk should terminate reading
9478 		 */
9479 		if ((sensep->es_eom) &&
9480 		    (CDBP(pkt)->scc_cmd != SCMD_READ)) {
9481 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG, "eom\n");
9482 			un->un_status = SUN_KEY_EOT;
9483 			un->un_eof = ST_EOM;
9484 			SET_PE_FLAG(un);
9485 		}
9486 
9487 		break;
9488 
9489 	case KEY_VOLUME_OVERFLOW:
9490 		ST_DO_ERRSTATS(un, st_softerrs);
9491 		un->un_eof = ST_EOM;
9492 		severity = SCSI_ERR_FATAL;
9493 		rval = COMMAND_DONE_ERROR;
9494 		goto check_keys;
9495 
9496 	case KEY_HARDWARE_ERROR:
9497 		ST_DO_ERRSTATS(un, st_harderrs);
9498 		severity = SCSI_ERR_FATAL;
9499 		rval = COMMAND_DONE_ERROR;
9500 		if (un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE)
9501 			un->un_eject_tape_on_failure = st_check_asc_ascq(un);
9502 		break;
9503 
9504 	case KEY_BLANK_CHECK:
9505 		ST_DO_ERRSTATS(un, st_softerrs);
9506 		severity = SCSI_ERR_INFO;
9507 
9508 		/*
9509 		 * if not a special request and some data was xferred then it
9510 		 * it is not an error yet
9511 		 */
9512 		if (bp != un->un_sbufp && (bp->b_flags & B_READ)) {
9513 			/*
9514 			 * no error for read with or without data xferred
9515 			 */
9516 			un->un_status = SUN_KEY_EOT;
9517 			un->un_eof = ST_EOT;
9518 			rval = COMMAND_DONE_ERROR;
9519 			SET_PE_FLAG(un);
9520 			goto check_keys;
9521 		} else if (bp != un->un_sbufp &&
9522 		    (pkt->pkt_state & STATE_XFERRED_DATA)) {
9523 			rval = COMMAND_DONE;
9524 		} else {
9525 			rval = COMMAND_DONE_ERROR_RECOVERED;
9526 		}
9527 
9528 		if (un->un_laststate >= ST_STATE_OPEN) {
9529 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9530 			    "blank check\n");
9531 			un->un_eof = ST_EOM;
9532 		}
9533 		if ((CDBP(pkt)->scc_cmd == SCMD_SPACE) &&
9534 		    (un->un_dp->options & ST_KNOWS_EOD) &&
9535 		    (severity = SCSI_ERR_INFO)) {
9536 			/*
9537 			 * we were doing a fast forward by skipping
9538 			 * multiple fmk at the time
9539 			 */
9540 			st_bioerror(bp, EIO);
9541 			severity = SCSI_ERR_RECOVERED;
9542 			rval	 = COMMAND_DONE;
9543 		}
9544 		SET_PE_FLAG(un);
9545 		goto check_keys;
9546 
9547 	case KEY_WRITE_PROTECT:
9548 		if (st_wrongtapetype(un)) {
9549 			un->un_status = SUN_KEY_WRONGMEDIA;
9550 			ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9551 		"wrong tape for writing- use DC6150 tape (or equivalent)\n");
9552 			severity = SCSI_ERR_UNKNOWN;
9553 		} else {
9554 			severity = SCSI_ERR_FATAL;
9555 		}
9556 		ST_DO_ERRSTATS(un, st_harderrs);
9557 		rval = COMMAND_DONE_ERROR;
9558 		st_bioerror(bp, EACCES);
9559 		break;
9560 
9561 	case KEY_UNIT_ATTENTION:
9562 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9563 		    "KEY_UNIT_ATTENTION : un_state = %d\n", un->un_state);
9564 
9565 		/*
9566 		 * If we have detected a Bus Reset and the tape
9567 		 * drive has been reserved.
9568 		 */
9569 		if (ST_RQSENSE->es_add_code == 0x29 &&
9570 			(un->un_rsvd_status & ST_RESERVE)) {
9571 			un->un_rsvd_status |= ST_LOST_RESERVE;
9572 			ST_DEBUG(ST_DEVINFO, st_label, CE_WARN,
9573 				"st_decode_sense: Lost Reservation\n");
9574 		}
9575 
9576 		if (un->un_state <= ST_STATE_OPENING) {
9577 			/*
9578 			 * Look, the tape isn't open yet, now determine
9579 			 * if the cause is a BUS RESET, Save the file and
9580 			 * Block positions for the callers to recover from
9581 			 * the loss of position.
9582 			 */
9583 			if ((un->un_fileno >= 0) &&
9584 			(un->un_fileno || un->un_blkno)) {
9585 				if (ST_RQSENSE->es_add_code == 0x29) {
9586 					un->un_save_fileno = un->un_fileno;
9587 					un->un_save_blkno = un->un_blkno;
9588 					un->un_restore_pos = 1;
9589 				}
9590 			}
9591 
9592 			if ((int)un->un_retry_ct++ < st_retry_count) {
9593 				rval = QUE_COMMAND;
9594 			} else {
9595 				rval = COMMAND_DONE_ERROR;
9596 			}
9597 			severity = SCSI_ERR_INFO;
9598 
9599 		} else {
9600 			/*
9601 			 * Check if it is an Unexpected Unit Attention.
9602 			 * If state is >= ST_STATE_OPEN, we have
9603 			 * already done the initialization .
9604 			 * In this case it is Fatal Error
9605 			 * since no further reading/writing
9606 			 * can be done with fileno set to < 0.
9607 			 */
9608 			if (un->un_state >= ST_STATE_OPEN) {
9609 				ST_DO_ERRSTATS(un, st_harderrs);
9610 				severity = SCSI_ERR_FATAL;
9611 			} else {
9612 				severity = SCSI_ERR_INFO;
9613 			}
9614 			rval = COMMAND_DONE_ERROR;
9615 		}
9616 		un->un_fileno = -1;
9617 
9618 		break;
9619 
9620 	case KEY_NOT_READY:
9621 		/*
9622 		 * If in process of getting ready retry.
9623 		 */
9624 		if (sensep->es_add_code  == 0x04 &&
9625 		    sensep->es_qual_code == 0x01 &&
9626 		    un->un_retry_ct++ < st_retry_count) {
9627 			rval = QUE_COMMAND;
9628 			severity = SCSI_ERR_INFO;
9629 		} else {
9630 			/* give up */
9631 			rval = COMMAND_DONE_ERROR;
9632 			severity = SCSI_ERR_FATAL;
9633 		}
9634 
9635 		/*
9636 		 * If this was an error and after device opened
9637 		 * do error stats.
9638 		 */
9639 		if (rval == COMMAND_DONE_ERROR &&
9640 		    un->un_state > ST_STATE_OPENING) {
9641 			ST_DO_ERRSTATS(un, st_harderrs);
9642 		}
9643 
9644 		if (ST_RQSENSE->es_add_code == 0x3a) {
9645 			if (st_error_level >= SCSI_ERR_FATAL)
9646 				scsi_log(ST_DEVINFO, st_label, CE_NOTE,
9647 				    "Tape not inserted in drive\n");
9648 			un->un_mediastate = MTIO_EJECTED;
9649 			cv_broadcast(&un->un_state_cv);
9650 		}
9651 		if ((un->un_dp->options & ST_EJECT_ON_CHANGER_FAILURE) &&
9652 		    (rval != QUE_COMMAND))
9653 			un->un_eject_tape_on_failure = st_check_asc_ascq(un);
9654 		break;
9655 
9656 	case KEY_ABORTED_COMMAND:
9657 
9658 		/*
9659 		 * Probably a parity error...
9660 		 * if we retry here then this may cause data to be
9661 		 * written twice or data skipped during reading
9662 		 */
9663 		ST_DO_ERRSTATS(un, st_harderrs);
9664 		severity = SCSI_ERR_FATAL;
9665 		rval = COMMAND_DONE_ERROR;
9666 		goto check_keys;
9667 
9668 	default:
9669 		/*
9670 		 * Undecoded sense key.	 Try retries and hope
9671 		 * that will fix the problem.  Otherwise, we're
9672 		 * dead.
9673 		 */
9674 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9675 		    "Unhandled Sense Key '%s'\n",
9676 		    sense_keys[un->un_status]);
9677 		ST_DO_ERRSTATS(un, st_harderrs);
9678 		severity = SCSI_ERR_FATAL;
9679 		rval = COMMAND_DONE_ERROR;
9680 		goto check_keys;
9681 	}
9682 
9683 	if ((!(pkt->pkt_flags & FLAG_SILENT) &&
9684 	    un->un_state >= ST_STATE_OPEN) && (DEBUGGING ||
9685 		(un->un_laststate > ST_STATE_OPENING) &&
9686 		(severity >= st_error_level))) {
9687 
9688 		scsi_errmsg(ST_SCSI_DEVP, pkt, st_label, severity,
9689 		    un->un_blkno, un->un_err_blkno, scsi_cmds, sensep);
9690 		if (sensep->es_filmk) {
9691 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
9692 			    "File Mark Detected\n");
9693 		}
9694 		if (sensep->es_eom) {
9695 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
9696 			    "End-of-Media Detected\n");
9697 		}
9698 		if (sensep->es_ili) {
9699 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
9700 			    "Incorrect Length Indicator Set\n");
9701 		}
9702 	}
9703 	get_error = geterror(bp);
9704 	if (((rval == COMMAND_DONE_ERROR) ||
9705 	    (rval == COMMAND_DONE_ERROR_RECOVERED)) &&
9706 	    ((get_error == EIO) || (get_error == 0))) {
9707 		un->un_rqs_state |= (ST_RQS_ERROR | ST_RQS_VALID);
9708 		bcopy(ST_RQSENSE, un->un_uscsi_rqs_buf, SENSE_LENGTH);
9709 		if (un->un_rqs_state & ST_RQS_READ) {
9710 		    un->un_rqs_state &= ~(ST_RQS_READ);
9711 		} else {
9712 		    un->un_rqs_state |= ST_RQS_OVR;
9713 		}
9714 	}
9715 
9716 	return (rval);
9717 }
9718 
9719 
9720 static int
9721 st_handle_intr_retry_lcmd(struct scsi_tape *un, struct buf *bp)
9722 {
9723 	int status = TRAN_ACCEPT;
9724 
9725 	mutex_enter(ST_MUTEX);
9726 
9727 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9728 		"st_handle_intr_rtr_lcmd(), un = 0x%p\n", (void *)un);
9729 
9730 	/*
9731 	 * Check to see if we hit the retry timeout. We check to make sure
9732 	 * this is the first one on the runq and make sure we have not
9733 	 * queued up any more, so this one has to be the last on the list
9734 	 * also. If it is not, we have to fail.  If it is not the first, but
9735 	 * is the last we are in trouble anyway, as we are in the interrupt
9736 	 * context here.
9737 	 */
9738 	if (((int)un->un_retry_ct > st_retry_count) ||
9739 	    ((un->un_runqf != bp) && (un->un_runql != bp))) {
9740 	    goto exit;
9741 	}
9742 
9743 	if (un->un_throttle) {
9744 		un->un_last_throttle = un->un_throttle;
9745 		un->un_throttle = 0;
9746 	}
9747 
9748 	/*
9749 	 * Here we know : bp is the first and last one on the runq
9750 	 * it is not necessary to put it back on the head of the
9751 	 * waitq and then move from waitq to runq. Save this queuing
9752 	 * and call scsi_transport.
9753 	 */
9754 
9755 	mutex_exit(ST_MUTEX);
9756 
9757 	status = scsi_transport(BP_PKT(bp));
9758 
9759 	mutex_enter(ST_MUTEX);
9760 
9761 	if (status == TRAN_ACCEPT) {
9762 		un->un_tran_retry_ct = 0;
9763 		if (un->un_last_throttle) {
9764 			un->un_throttle = un->un_last_throttle;
9765 		}
9766 		mutex_exit(ST_MUTEX);
9767 
9768 		ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9769 		    "restart transport \n");
9770 		return (0);
9771 	}
9772 
9773 	ST_DO_KSTATS(bp, kstat_runq_back_to_waitq);
9774 	mutex_exit(ST_MUTEX);
9775 
9776 	if (status == TRAN_BUSY) {
9777 	    if (st_handle_intr_busy(un, bp,
9778 		ST_TRAN_BUSY_TIMEOUT) == 0)
9779 		return (0);
9780 	}
9781 	ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
9782 		"restart transport rejected\n");
9783 	mutex_enter(ST_MUTEX);
9784 	ST_DO_ERRSTATS(un, st_transerrs);
9785 	if (un->un_last_throttle) {
9786 		un->un_throttle = un->un_last_throttle;
9787 	}
9788 exit:
9789 	mutex_exit(ST_MUTEX);
9790 	return (-1);
9791 }
9792 
9793 static int
9794 st_wrongtapetype(struct scsi_tape *un)
9795 {
9796 
9797 	ASSERT(mutex_owned(ST_MUTEX));
9798 
9799 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9800 		"st_wrongtapetype()\n");
9801 
9802 	/*
9803 	 * Hack to handle  600A, 600XTD, 6150 && 660 vs. 300XL tapes...
9804 	 */
9805 	if (un->un_dp && (un->un_dp->options & ST_QIC) && un->un_mspl) {
9806 		switch (un->un_dp->type) {
9807 		case ST_TYPE_WANGTEK:
9808 		case ST_TYPE_ARCHIVE:
9809 			/*
9810 			 * If this really worked, we could go off of
9811 			 * the density codes set in the modesense
9812 			 * page. For this drive, 0x10 == QIC-120,
9813 			 * 0xf == QIC-150, and 0x5 should be for
9814 			 * both QIC-24 and, maybe, QIC-11. However,
9815 			 * the h/w doesn't do what the manual says
9816 			 * that it should, so we'll key off of
9817 			 * getting a WRITE PROTECT error AND wp *not*
9818 			 * set in the mode sense information.
9819 			 */
9820 			/*
9821 			 * XXX but we already know that status is
9822 			 * write protect, so don't check it again.
9823 			 */
9824 
9825 			if (un->un_status == KEY_WRITE_PROTECT &&
9826 			    un->un_mspl->wp == 0) {
9827 				return (1);
9828 			}
9829 			break;
9830 		default:
9831 			break;
9832 		}
9833 	}
9834 	return (0);
9835 }
9836 
9837 static int
9838 st_check_error(struct scsi_tape *un, struct scsi_pkt *pkt)
9839 {
9840 	int action;
9841 
9842 	ASSERT(mutex_owned(ST_MUTEX));
9843 
9844 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_check_error()\n");
9845 
9846 	if (SCBP_C(pkt) == STATUS_RESERVATION_CONFLICT) {
9847 		action = COMMAND_DONE_EACCES;
9848 		un->un_rsvd_status |= ST_RESERVATION_CONFLICT;
9849 	} else if (SCBP(pkt)->sts_busy) {
9850 		ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG, "unit busy\n");
9851 		if ((int)un->un_retry_ct++ < st_retry_count) {
9852 			action = QUE_BUSY_COMMAND;
9853 		} else if ((un->un_rsvd_status &
9854 		    (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) == 0) {
9855 			/*
9856 			 * If this is a command done before reserve is done
9857 			 * don't reset.
9858 			 */
9859 			action = COMMAND_DONE_ERROR;
9860 		} else {
9861 			ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN,
9862 			    "unit busy too long\n");
9863 			mutex_exit(ST_MUTEX);
9864 			if (scsi_reset(ROUTE, RESET_TARGET) == 0) {
9865 				(void) scsi_reset(ROUTE, RESET_ALL);
9866 			}
9867 			mutex_enter(ST_MUTEX);
9868 			action = COMMAND_DONE_ERROR;
9869 		}
9870 	} else if (SCBP(pkt)->sts_chk) {
9871 		/*
9872 		 * we should only get here if the auto rqsense failed
9873 		 * thru a uscsi cmd without autorequest sense
9874 		 * so we just try again
9875 		 */
9876 		action = QUE_SENSE;
9877 	} else {
9878 		action = COMMAND_DONE;
9879 	}
9880 	return (action);
9881 }
9882 
9883 static void
9884 st_calc_bnum(struct scsi_tape *un, struct buf *bp)
9885 {
9886 	int n;
9887 
9888 	ASSERT(mutex_owned(ST_MUTEX));
9889 
9890 	if (un->un_bsize == 0) {
9891 		n = ((bp->b_bcount - bp->b_resid  == 0) ? 0 : 1);
9892 		un->un_kbytes_xferred += (bp->b_bcount - bp->b_resid)/1000;
9893 	} else {
9894 		n = ((bp->b_bcount - bp->b_resid) / un->un_bsize);
9895 	}
9896 	un->un_blkno += n;
9897 }
9898 
9899 static void
9900 st_set_state(struct scsi_tape *un)
9901 {
9902 	struct buf *bp = un->un_runqf;
9903 	struct scsi_pkt *sp = BP_PKT(bp);
9904 	struct uscsi_cmd *ucmd;
9905 
9906 	ASSERT(mutex_owned(ST_MUTEX));
9907 
9908 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
9909 	    "st_set_state(): un_eof=%x	fmneeded=%x  pkt_resid=0x%lx (%ld)\n",
9910 		un->un_eof, un->un_fmneeded, sp->pkt_resid, sp->pkt_resid);
9911 
9912 	if (bp != un->un_sbufp) {
9913 #ifdef STDEBUG
9914 		if (DEBUGGING && sp->pkt_resid) {
9915 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
9916 			    "pkt_resid %ld bcount %ld\n",
9917 			    sp->pkt_resid, bp->b_bcount);
9918 		}
9919 #endif
9920 		bp->b_resid = sp->pkt_resid;
9921 		st_calc_bnum(un, bp);
9922 		if (bp->b_flags & B_READ) {
9923 			un->un_lastop = ST_OP_READ;
9924 			un->un_fmneeded = 0;
9925 		} else {
9926 			un->un_lastop = ST_OP_WRITE;
9927 			if (un->un_dp->options & ST_REEL) {
9928 				un->un_fmneeded = 2;
9929 			} else {
9930 				un->un_fmneeded = 1;
9931 			}
9932 		}
9933 		/*
9934 		 * all is honky dory at this point, so let's
9935 		 * readjust the throttle, to increase speed, if we
9936 		 * have not throttled down.
9937 		 */
9938 		if (un->un_throttle)
9939 			un->un_throttle = un->un_max_throttle;
9940 	} else {
9941 		char saved_lastop = un->un_lastop;
9942 		uchar_t cmd = (uchar_t)(intptr_t)bp->b_forw;
9943 
9944 		un->un_lastop = ST_OP_CTL;
9945 
9946 		switch (cmd) {
9947 		case SCMD_WRITE:
9948 			bp->b_resid = sp->pkt_resid;
9949 			un->un_lastop = ST_OP_WRITE;
9950 			st_calc_bnum(un, bp);
9951 			if (un->un_dp->options & ST_REEL) {
9952 				un->un_fmneeded = 2;
9953 			} else {
9954 				un->un_fmneeded = 1;
9955 			}
9956 			break;
9957 		case SCMD_READ:
9958 			bp->b_resid = sp->pkt_resid;
9959 			un->un_lastop = ST_OP_READ;
9960 			st_calc_bnum(un, bp);
9961 			un->un_fmneeded = 0;
9962 			break;
9963 		case SCMD_WRITE_FILE_MARK:
9964 			if (un->un_eof != ST_EOM)
9965 				un->un_eof = ST_NO_EOF;
9966 			un->un_lastop = ST_OP_WEOF;
9967 			un->un_fileno += (bp->b_bcount - bp->b_resid);
9968 			un->un_blkno = 0;
9969 			if (un->un_dp->options & ST_REEL) {
9970 				un->un_fmneeded -=
9971 					(bp->b_bcount - bp->b_resid);
9972 				if (un->un_fmneeded < 0) {
9973 					un->un_fmneeded = 0;
9974 				}
9975 			} else {
9976 				un->un_fmneeded = 0;
9977 			}
9978 
9979 			break;
9980 		case SCMD_REWIND:
9981 			un->un_eof = ST_NO_EOF;
9982 			un->un_fileno = 0;
9983 			un->un_blkno = 0;
9984 			break;
9985 
9986 		case SCMD_SPACE:
9987 		{
9988 			int space_fmk, count;
9989 			long resid;
9990 
9991 			count = (int)space_cnt(bp->b_bcount);
9992 			resid = (long)space_cnt(bp->b_resid);
9993 			space_fmk = ((bp->b_bcount) & (1<<24)) ? 1 : 0;
9994 
9995 
9996 			if (count >= 0) {
9997 				if (space_fmk) {
9998 					if (un->un_eof <= ST_EOF) {
9999 						un->un_eof = ST_NO_EOF;
10000 					}
10001 					un->un_fileno += (count - resid);
10002 					un->un_blkno = 0;
10003 				} else {
10004 					un->un_blkno += count - resid;
10005 				}
10006 			} else if (count < 0) {
10007 				if (space_fmk) {
10008 					un->un_fileno -=
10009 					    ((-count) - resid);
10010 					if (un->un_fileno < 0) {
10011 						un->un_fileno = 0;
10012 						un->un_blkno = 0;
10013 					} else {
10014 						un->un_blkno = INF;
10015 					}
10016 				} else {
10017 					if (un->un_eof >= ST_EOF_PENDING) {
10018 					/*
10019 					 * we stepped back into
10020 					 * a previous file; we are not
10021 					 * making an effort to pretend that
10022 					 * we are still in the current file
10023 					 * ie. logical == physical position
10024 					 * and leave it to st_ioctl to correct
10025 					 */
10026 						if (un->un_fileno > 0) {
10027 							un->un_fileno--;
10028 							un->un_blkno = INF;
10029 						} else {
10030 							un->un_blkno = 0;
10031 						}
10032 					} else {
10033 						un->un_blkno -=
10034 						    (-count) - resid;
10035 					}
10036 				}
10037 			}
10038 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10039 			    "aft_space rs %ld fil %d blk %ld\n",
10040 			    resid, un->un_fileno, un->un_blkno);
10041 			break;
10042 		}
10043 		case SCMD_LOAD:
10044 			if (bp->b_bcount & 0x1) {
10045 				un->un_fileno = 0;
10046 			} else {
10047 				un->un_state = ST_STATE_OFFLINE;
10048 				un->un_fileno = -1;
10049 			}
10050 			un->un_density_known = 0;
10051 			un->un_eof = ST_NO_EOF;
10052 			un->un_blkno = 0;
10053 			break;
10054 		case SCMD_ERASE:
10055 			un->un_eof = ST_NO_EOF;
10056 			un->un_blkno = 0;
10057 			un->un_fileno = 0;
10058 			break;
10059 		case SCMD_RESERVE:
10060 			un->un_rsvd_status |= ST_RESERVE;
10061 			un->un_rsvd_status &=
10062 			    ~(ST_RELEASE | ST_LOST_RESERVE |
10063 			    ST_RESERVATION_CONFLICT);
10064 			un->un_lastop = saved_lastop;
10065 			break;
10066 		case SCMD_RELEASE:
10067 			un->un_rsvd_status |= ST_RELEASE;
10068 			un->un_rsvd_status &=
10069 			    ~(ST_RESERVE | ST_LOST_RESERVE |
10070 			    ST_RESERVATION_CONFLICT);
10071 			un->un_lastop = saved_lastop;
10072 			break;
10073 		case SCMD_PERSISTENT_RESERVE_IN:
10074 			ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
10075 			    "PGR_IN command\n");
10076 			break;
10077 		case SCMD_PERSISTENT_RESERVE_OUT:
10078 			switch (sp->pkt_cdbp[1] & ST_SA_MASK) {
10079 			case ST_SA_SCSI3_RESERVE:
10080 			case ST_SA_SCSI3_PREEMPT:
10081 			case ST_SA_SCSI3_PREEMPTANDABORT:
10082 				un->un_rsvd_status |=
10083 				    ST_APPLICATION_RESERVATIONS;
10084 				ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
10085 				    "PGR Reserve and set: entering"
10086 				    " ST_APPLICATION_RESERVATIONS mode");
10087 				break;
10088 			case ST_SA_SCSI3_RELEASE:
10089 			case ST_SA_SCSI3_CLEAR:
10090 				un->un_rsvd_status &=
10091 				    ~ST_APPLICATION_RESERVATIONS;
10092 				ST_DEBUG6(ST_DEVINFO, st_label, CE_WARN,
10093 				    "PGR Release and reset: exiting"
10094 				    " ST_APPLICATION_RESERVATIONS mode");
10095 				break;
10096 			}
10097 			break;
10098 		case SCMD_TEST_UNIT_READY:
10099 		case SCMD_READ_BLKLIM:
10100 		case SCMD_REQUEST_SENSE:
10101 		case SCMD_INQUIRY:
10102 		case SCMD_RECOVER_BUF:
10103 		case SCMD_MODE_SELECT:
10104 		case SCMD_MODE_SENSE:
10105 		case SCMD_DOORLOCK:
10106 		case SCMD_READ_POSITION:
10107 		case SCMD_READ_BUFFER:
10108 		case SCMD_REPORT_DENSITIES:
10109 		case SCMD_LOG_SELECT_G1:
10110 		case SCMD_LOG_SENSE_G1:
10111 		case SCMD_REPORT_LUNS:
10112 			un->un_lastop = saved_lastop;
10113 			break;
10114 		case SCMD_LOCATE:	/* Locate makes position unknown */
10115 		default:
10116 			/*
10117 			 * Unknown command, If was USCSI and USCSI_SILENT
10118 			 * flag was not set, set position to unknown.
10119 			 */
10120 			if ((((ucmd = BP_UCMD(bp)) != NULL) &&
10121 			    (ucmd->uscsi_flags & USCSI_SILENT) == 0)) {
10122 				ST_DEBUG2(ST_DEVINFO, st_label, CE_WARN,
10123 				    "unknown cmd 0x%X caused loss of state\n",
10124 				    cmd);
10125 			} else {
10126 				break;
10127 			}
10128 			/* FALLTHROUGH */
10129 		case SCMD_WRITE_BUFFER: /* Writes new firmware to device */
10130 			un->un_fileno = -1;
10131 			break;
10132 		}
10133 	}
10134 
10135 	/*
10136 	 * In the st driver we have a logical and physical file position.
10137 	 * Under BSD behavior, when you get a zero read, the logical position
10138 	 * is before the filemark but after the last record of the file.
10139 	 * The physical position is after the filemark. MTIOCGET should always
10140 	 * return the logical file position.
10141 	 *
10142 	 * The next read gives a silent skip to the next file.
10143 	 * Under SVR4, the logical file position remains before the filemark
10144 	 * until the file is closed or a space operation is performed.
10145 	 * Hence set err_resid and err_file before changing fileno if case
10146 	 * BSD Behaviour.
10147 	 */
10148 	un->un_err_resid = bp->b_resid;
10149 	un->un_err_fileno = un->un_fileno;
10150 	un->un_err_blkno = un->un_blkno;
10151 	un->un_retry_ct = 0;
10152 
10153 
10154 	/*
10155 	 * If we've seen a filemark via the last read operation
10156 	 * advance the file counter, but mark things such that
10157 	 * the next read operation gets a zero count. We have
10158 	 * to put this here to handle the case of sitting right
10159 	 * at the end of a tape file having seen the file mark,
10160 	 * but the tape is closed and then re-opened without
10161 	 * any further i/o. That is, the position information
10162 	 * must be updated before a close.
10163 	 */
10164 
10165 	if (un->un_lastop == ST_OP_READ && un->un_eof == ST_EOF_PENDING) {
10166 		/*
10167 		 * If we're a 1/2" tape, and we get a filemark
10168 		 * right on block 0, *AND* we were not in the
10169 		 * first file on the tape, and we've hit logical EOM.
10170 		 * We'll mark the state so that later we do the
10171 		 * right thing (in st_close(), st_strategy() or
10172 		 * st_ioctl()).
10173 		 *
10174 		 */
10175 		if ((un->un_dp->options & ST_REEL) &&
10176 			!(un->un_dp->options & ST_READ_IGNORE_EOFS) &&
10177 		    un->un_blkno == 0 && un->un_fileno > 0) {
10178 			un->un_eof = ST_EOT_PENDING;
10179 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10180 			    "eot pending\n");
10181 			un->un_fileno++;
10182 			un->un_blkno = 0;
10183 		} else if (BSD_BEHAVIOR) {
10184 			/*
10185 			 * If the read of the filemark was a side effect
10186 			 * of reading some blocks (i.e., data was actually
10187 			 * read), then the EOF mark is pending and the
10188 			 * bump into the next file awaits the next read
10189 			 * operation (which will return a zero count), or
10190 			 * a close or a space operation, else the bump
10191 			 * into the next file occurs now.
10192 			 */
10193 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10194 			    "resid=%lx, bcount=%lx\n",
10195 				bp->b_resid, bp->b_bcount);
10196 			if (bp->b_resid != bp->b_bcount) {
10197 				un->un_eof = ST_EOF;
10198 			} else {
10199 				un->un_silent_skip = 1;
10200 				un->un_eof = ST_NO_EOF;
10201 				un->un_fileno++;
10202 				un->un_save_blkno = un->un_blkno;
10203 				un->un_blkno = 0;
10204 			}
10205 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10206 			    "eof of file %d, un_eof=%d\n",
10207 			    un->un_fileno, un->un_eof);
10208 		} else if (SVR4_BEHAVIOR) {
10209 			/*
10210 			 * If the read of the filemark was a side effect
10211 			 * of reading some blocks (i.e., data was actually
10212 			 * read), then the next read should return 0
10213 			 */
10214 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10215 			    "resid=%lx, bcount=%lx\n",
10216 			    bp->b_resid, bp->b_bcount);
10217 			if (bp->b_resid == bp->b_bcount) {
10218 				un->un_eof = ST_EOF;
10219 			}
10220 			ST_DEBUG6(ST_DEVINFO, st_label, SCSI_DEBUG,
10221 			    "eof of file=%d, un_eof=%d\n",
10222 			    un->un_fileno, un->un_eof);
10223 		}
10224 	}
10225 }
10226 
10227 /*
10228  * set the correct un_errno, to take corner cases into consideration
10229  */
10230 static void
10231 st_set_pe_errno(struct scsi_tape *un)
10232 {
10233 	ASSERT(mutex_owned(ST_MUTEX));
10234 
10235 	/* if errno is already set, don't reset it */
10236 	if (un->un_errno)
10237 		return;
10238 
10239 	/* here un_errno == 0 */
10240 	/*
10241 	 * if the last transfer before flushing all the
10242 	 * waiting I/O's, was 0 (resid = count), then we
10243 	 * want to give the user an error on all the rest,
10244 	 * so here.  If there was a transfer, we set the
10245 	 * resid and counts to 0, and let it drop through,
10246 	 * giving a zero return.  the next I/O will then
10247 	 * give an error.
10248 	 */
10249 	if (un->un_last_resid == un->un_last_count) {
10250 		switch (un->un_eof) {
10251 		case ST_EOM:
10252 			un->un_errno = ENOMEM;
10253 			break;
10254 		case ST_EOT:
10255 		case ST_EOF:
10256 			un->un_errno = EIO;
10257 			break;
10258 		}
10259 	} else {
10260 		/*
10261 		 * we know they did not have a zero, so make
10262 		 * sure they get one
10263 		 */
10264 		un->un_last_resid = un->un_last_count = 0;
10265 	}
10266 }
10267 
10268 
10269 /*
10270  * send in a marker pkt to terminate flushing of commands by BBA (via
10271  * flush-on-errors) property.  The HBA will always return TRAN_ACCEPT
10272  */
10273 static void
10274 st_hba_unflush(struct scsi_tape *un)
10275 {
10276 	ASSERT(mutex_owned(ST_MUTEX));
10277 
10278 	if (!un->un_flush_on_errors)
10279 		return;
10280 
10281 #ifdef FLUSH_ON_ERRORS
10282 
10283 	if (!un->un_mkr_pkt) {
10284 		un->un_mkr_pkt = scsi_init_pkt(ROUTE, NULL, (struct buf *)NULL,
10285 		    NULL, 0, 0, 0, SLEEP_FUNC, NULL);
10286 
10287 		/* we slept, so it must be there */
10288 		pkt->pkt_flags |= FLAG_FLUSH_MARKER;
10289 	}
10290 
10291 	mutex_exit(ST_MUTEX);
10292 	scsi_transport(un->un_mkr_pkt);
10293 	mutex_enter(ST_MUTEX);
10294 #endif
10295 }
10296 
10297 static void
10298 st_clean_print(dev_info_t *dev, char *label, uint_t level,
10299 	char *title, char *data, int len)
10300 {
10301 	int	i;
10302 	char	buf[256];
10303 
10304 	(void) sprintf(buf, "%s: ", title);
10305 	for (i = 0; i < len; i++) {
10306 		(void) sprintf(&buf[(int)strlen(buf)], "0x%x ",
10307 			(data[i] & 0xff));
10308 	}
10309 	(void) sprintf(&buf[(int)strlen(buf)], "\n");
10310 
10311 	scsi_log(dev, label, level, "%s", buf);
10312 }
10313 
10314 /*
10315  * Conditionally enabled debugging
10316  */
10317 #ifdef	STDEBUG
10318 static void
10319 st_debug_cmds(struct scsi_tape *un, int com, int count, int wait)
10320 {
10321 	char tmpbuf[64];
10322 
10323 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10324 	    "cmd=%s count=0x%x (%d)	 %ssync\n",
10325 	    scsi_cmd_name(com, scsi_cmds, tmpbuf),
10326 	    count, count,
10327 	    wait == ASYNC_CMD ? "a" : "");
10328 }
10329 
10330 /*
10331  * Returns pointer to name of minor node name of device 'dev'.
10332  */
10333 static char *
10334 st_dev_name(dev_t dev)
10335 {
10336 	const char density[] = { 'l', 'm', 'h', 'c' };
10337 	static char name[4];
10338 	minor_t minor;
10339 	int nprt = 0;
10340 
10341 	minor = getminor(dev);
10342 	name[nprt] = density[(minor & MT_DENSITY_MASK) >> 3];
10343 
10344 	if (minor & MT_BSD) {
10345 		name[++nprt] = 'b';
10346 	}
10347 
10348 	if (minor & MT_NOREWIND) {
10349 		name[++nprt] = 'n';
10350 	}
10351 
10352 	/* NULL terminator */
10353 	name[++nprt] = 0;
10354 
10355 	return (name);
10356 }
10357 #endif	/* STDEBUG */
10358 
10359 /*
10360  * Soft error reporting, so far unique to each drive
10361  *
10362  * Currently supported: exabyte and DAT soft error reporting
10363  */
10364 static int
10365 st_report_exabyte_soft_errors(dev_t dev, int flag)
10366 {
10367 	uchar_t *sensep;
10368 	int amt;
10369 	int rval = 0;
10370 	char cdb[CDB_GROUP0], *c = cdb;
10371 	struct uscsi_cmd *com;
10372 
10373 	GET_SOFT_STATE(dev);
10374 
10375 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10376 	    "st_report_exabyte_soft_errors(dev = 0x%lx, flag = %d)\n",
10377 	    dev, flag);
10378 
10379 	ASSERT(mutex_owned(ST_MUTEX));
10380 
10381 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
10382 	sensep = kmem_zalloc(TAPE_SENSE_LENGTH, KM_SLEEP);
10383 
10384 	*c++ = SCMD_REQUEST_SENSE;
10385 	*c++ = 0;
10386 	*c++ = 0;
10387 	*c++ = 0;
10388 	*c++ = TAPE_SENSE_LENGTH;
10389 	/*
10390 	 * set CLRCNT (byte 5, bit 7 which clears the error counts)
10391 	 */
10392 	*c   = (char)0x80;
10393 
10394 	com->uscsi_cdb = cdb;
10395 	com->uscsi_cdblen = CDB_GROUP0;
10396 	com->uscsi_bufaddr = (caddr_t)sensep;
10397 	com->uscsi_buflen = TAPE_SENSE_LENGTH;
10398 	com->uscsi_flags = USCSI_DIAGNOSE | USCSI_SILENT
10399 		| USCSI_READ | USCSI_RQENABLE;
10400 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
10401 
10402 	rval = st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
10403 		UIO_SYSSPACE);
10404 	if (rval || com->uscsi_status) {
10405 		goto done;
10406 	}
10407 
10408 	/*
10409 	 * was there enough data?
10410 	 */
10411 	amt = (int)TAPE_SENSE_LENGTH - com->uscsi_resid;
10412 
10413 	if ((amt >= 19) && un->un_kbytes_xferred) {
10414 		uint_t count, error_rate;
10415 		uint_t rate;
10416 
10417 		if (sensep[21] & CLN) {
10418 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
10419 			    "Periodic head cleaning required");
10420 		}
10421 		if (un->un_kbytes_xferred < (EXABYTE_MIN_TRANSFER/1000))
10422 			goto done;
10423 		/*
10424 		 * check if soft error reporting needs to be done.
10425 		 */
10426 		count = sensep[16] << 16 | sensep[17] << 8 | sensep[18];
10427 		count &= 0xffffff;
10428 		error_rate = (count * 100)/un->un_kbytes_xferred;
10429 
10430 #ifdef	STDEBUG
10431 		if (st_soft_error_report_debug) {
10432 			scsi_log(ST_DEVINFO, st_label, CE_NOTE,
10433 			    "Exabyte Soft Error Report:\n");
10434 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
10435 			    "read/write error counter: %d\n", count);
10436 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
10437 			    "number of bytes transferred: %dK\n",
10438 				un->un_kbytes_xferred);
10439 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
10440 			    "error_rate: %d%%\n", error_rate);
10441 
10442 			if (amt >= 22) {
10443 				scsi_log(ST_DEVINFO, st_label, CE_CONT,
10444 				    "unit sense: 0x%b 0x%b 0x%b\n",
10445 				    sensep[19], SENSE_19_BITS,
10446 				    sensep[20], SENSE_20_BITS,
10447 				    sensep[21], SENSE_21_BITS);
10448 			}
10449 			if (amt >= 27) {
10450 				scsi_log(ST_DEVINFO, st_label, CE_CONT,
10451 				    "tracking retry counter: %d\n",
10452 				    sensep[26]);
10453 				scsi_log(ST_DEVINFO, st_label, CE_CONT,
10454 				    "read/write retry counter: %d\n",
10455 				    sensep[27]);
10456 			}
10457 		}
10458 #endif
10459 
10460 		if (flag & FWRITE) {
10461 			rate = EXABYTE_WRITE_ERROR_THRESHOLD;
10462 		} else {
10463 			rate = EXABYTE_READ_ERROR_THRESHOLD;
10464 		}
10465 		if (error_rate >= rate) {
10466 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
10467 			    "Soft error rate (%d%%) during %s was too high",
10468 			    error_rate,
10469 			    ((flag & FWRITE) ? wrg_str : rdg_str));
10470 			scsi_log(ST_DEVINFO, st_label, CE_CONT,
10471 			    "Please, replace tape cartridge\n");
10472 		}
10473 	}
10474 
10475 done:
10476 	kmem_free(com, sizeof (*com));
10477 	kmem_free(sensep, TAPE_SENSE_LENGTH);
10478 
10479 	if (rval != 0) {
10480 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
10481 		    "exabyte soft error reporting failed\n");
10482 	}
10483 	return (rval);
10484 }
10485 
10486 /*
10487  * this is very specific to Archive 4mm dat
10488  */
10489 #define	ONEGIG	(1024 * 1024 * 1024)
10490 
10491 static int
10492 st_report_dat_soft_errors(dev_t dev, int flag)
10493 {
10494 	uchar_t *sensep;
10495 	int amt, i;
10496 	int rval = 0;
10497 	char cdb[CDB_GROUP1], *c = cdb;
10498 	struct uscsi_cmd *com;
10499 
10500 	GET_SOFT_STATE(dev);
10501 
10502 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10503 	    "st_report_dat_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag);
10504 
10505 	ASSERT(mutex_owned(ST_MUTEX));
10506 
10507 	com = kmem_zalloc(sizeof (*com), KM_SLEEP);
10508 	sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP);
10509 
10510 	*c++ = SCMD_LOG_SENSE_G1;
10511 	*c++ = 0;
10512 	*c++ = (flag & FWRITE) ? 0x42 : 0x43;
10513 	*c++ = 0;
10514 	*c++ = 0;
10515 	*c++ = 0;
10516 	*c++ = 2;
10517 	*c++ = 0;
10518 	*c++ = (char)LOG_SENSE_LENGTH;
10519 	*c   = 0;
10520 	com->uscsi_cdb    = cdb;
10521 	com->uscsi_cdblen  = CDB_GROUP1;
10522 	com->uscsi_bufaddr = (caddr_t)sensep;
10523 	com->uscsi_buflen  = LOG_SENSE_LENGTH;
10524 	com->uscsi_flags   =
10525 	    USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
10526 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
10527 	rval =
10528 	    st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
10529 	if (rval) {
10530 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
10531 		    "DAT soft error reporting failed\n");
10532 	}
10533 	if (rval || com->uscsi_status) {
10534 		goto done;
10535 	}
10536 
10537 	/*
10538 	 * was there enough data?
10539 	 */
10540 	amt = (int)LOG_SENSE_LENGTH - com->uscsi_resid;
10541 
10542 	if ((amt >= MIN_LOG_SENSE_LENGTH) && un->un_kbytes_xferred) {
10543 		int total, retries, param_code;
10544 
10545 		total = -1;
10546 		retries = -1;
10547 		amt = sensep[3] + 4;
10548 
10549 
10550 #ifdef STDEBUG
10551 		if (st_soft_error_report_debug) {
10552 			(void) printf("logsense:");
10553 			for (i = 0; i < MIN_LOG_SENSE_LENGTH; i++) {
10554 				if (i % 16 == 0) {
10555 					(void) printf("\t\n");
10556 				}
10557 				(void) printf(" %x", sensep[i]);
10558 			}
10559 			(void) printf("\n");
10560 		}
10561 #endif
10562 
10563 		/*
10564 		 * parse the param_codes
10565 		 */
10566 		if (sensep[0] == 2 || sensep[0] == 3) {
10567 			for (i = 4; i < amt; i++) {
10568 				param_code = (sensep[i++] << 8);
10569 				param_code += sensep[i++];
10570 				i++; /* skip control byte */
10571 				if (param_code == 5) {
10572 					if (sensep[i++] == 4) {
10573 						total = (sensep[i++] << 24);
10574 						total += (sensep[i++] << 16);
10575 						total += (sensep[i++] << 8);
10576 						total += sensep[i];
10577 					}
10578 				} else if (param_code == 0x8007) {
10579 					if (sensep[i++] == 2) {
10580 						retries = sensep[i++] << 8;
10581 						retries += sensep[i];
10582 					}
10583 				} else {
10584 					i += sensep[i];
10585 				}
10586 			}
10587 		}
10588 
10589 		/*
10590 		 * if the log sense returned valid numbers then determine
10591 		 * the read and write error thresholds based on the amount of
10592 		 * data transferred
10593 		 */
10594 
10595 		if (total > 0 && retries > 0) {
10596 			short normal_retries = 0;
10597 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
10598 			"total xferred (%s) =%x, retries=%x\n",
10599 				((flag & FWRITE) ? wrg_str : rdg_str),
10600 				total, retries);
10601 
10602 			if (flag & FWRITE) {
10603 				if (total <=
10604 					WRITE_SOFT_ERROR_WARNING_THRESHOLD) {
10605 					normal_retries =
10606 						DAT_SMALL_WRITE_ERROR_THRESHOLD;
10607 				} else {
10608 					normal_retries =
10609 						DAT_LARGE_WRITE_ERROR_THRESHOLD;
10610 				}
10611 			} else {
10612 				if (total <=
10613 					READ_SOFT_ERROR_WARNING_THRESHOLD) {
10614 					normal_retries =
10615 						DAT_SMALL_READ_ERROR_THRESHOLD;
10616 				} else {
10617 					normal_retries =
10618 						DAT_LARGE_READ_ERROR_THRESHOLD;
10619 				}
10620 			}
10621 
10622 			ST_DEBUG4(ST_DEVINFO, st_label, SCSI_DEBUG,
10623 			"normal retries=%d\n", normal_retries);
10624 
10625 			if (retries >= normal_retries) {
10626 				scsi_log(ST_DEVINFO, st_label, CE_WARN,
10627 				    "Soft error rate (retries = %d) during "
10628 				    "%s was too high",  retries,
10629 				    ((flag & FWRITE) ? wrg_str : rdg_str));
10630 				scsi_log(ST_DEVINFO, st_label, CE_CONT,
10631 				    "Periodic head cleaning required "
10632 				    "and/or replace tape cartridge\n");
10633 			}
10634 
10635 		} else if (total == -1 || retries == -1) {
10636 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
10637 			    "log sense parameter code does not make sense\n");
10638 		}
10639 	}
10640 
10641 	/*
10642 	 * reset all values
10643 	 */
10644 	c = cdb;
10645 	*c++ = SCMD_LOG_SELECT_G1;
10646 	*c++ = 2;	/* this resets all values */
10647 	*c++ = (char)0xc0;
10648 	*c++ = 0;
10649 	*c++ = 0;
10650 	*c++ = 0;
10651 	*c++ = 0;
10652 	*c++ = 0;
10653 	*c++ = 0;
10654 	*c   = 0;
10655 	com->uscsi_bufaddr = NULL;
10656 	com->uscsi_buflen  = 0;
10657 	com->uscsi_flags   = USCSI_DIAGNOSE | USCSI_SILENT | USCSI_RQENABLE;
10658 	rval =
10659 	    st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
10660 	if (rval) {
10661 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
10662 		    "DAT soft error reset failed\n");
10663 	}
10664 done:
10665 	kmem_free(com, sizeof (*com));
10666 	kmem_free(sensep, LOG_SENSE_LENGTH);
10667 	return (rval);
10668 }
10669 
10670 static int
10671 st_report_soft_errors(dev_t dev, int flag)
10672 {
10673 	GET_SOFT_STATE(dev);
10674 
10675 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10676 	    "st_report_soft_errors(dev = 0x%lx, flag = %d)\n", dev, flag);
10677 
10678 	ASSERT(mutex_owned(ST_MUTEX));
10679 
10680 	switch (un->un_dp->type) {
10681 	case ST_TYPE_EXB8500:
10682 	case ST_TYPE_EXABYTE:
10683 		return (st_report_exabyte_soft_errors(dev, flag));
10684 		/*NOTREACHED*/
10685 	case ST_TYPE_PYTHON:
10686 		return (st_report_dat_soft_errors(dev, flag));
10687 		/*NOTREACHED*/
10688 	default:
10689 		un->un_dp->options &= ~ST_SOFT_ERROR_REPORTING;
10690 		return (-1);
10691 	}
10692 }
10693 
10694 /*
10695  * persistent error routines
10696  */
10697 
10698 /*
10699  * enable persistent errors, and set the throttle appropriately, checking
10700  * for flush-on-errors capability
10701  */
10702 static void
10703 st_turn_pe_on(struct scsi_tape *un)
10704 {
10705 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_on\n");
10706 	ASSERT(mutex_owned(ST_MUTEX));
10707 
10708 	un->un_persistence = 1;
10709 
10710 	/*
10711 	 * only use flush-on-errors if auto-request-sense and untagged-qing are
10712 	 * enabled.  This will simplify the error handling for request senses
10713 	 */
10714 
10715 	if (un->un_arq_enabled && un->un_untagged_qing) {
10716 		uchar_t f_o_e;
10717 
10718 		mutex_exit(ST_MUTEX);
10719 		f_o_e = (scsi_ifsetcap(ROUTE, "flush-on-errors", 1, 1) == 1) ?
10720 		    1 : 0;
10721 		mutex_enter(ST_MUTEX);
10722 
10723 		un->un_flush_on_errors = f_o_e;
10724 	} else {
10725 		un->un_flush_on_errors = 0;
10726 	}
10727 
10728 	if (un->un_flush_on_errors)
10729 		un->un_max_throttle = (uchar_t)st_max_throttle;
10730 	else
10731 		un->un_max_throttle = 1;
10732 
10733 	if (un->un_dp->options & ST_RETRY_ON_RECOVERED_DEFERRED_ERROR)
10734 		un->un_max_throttle = 1;
10735 
10736 	/* this will send a marker pkt */
10737 	CLEAR_PE(un);
10738 }
10739 
10740 /*
10741  * This turns persistent errors permanently off
10742  */
10743 static void
10744 st_turn_pe_off(struct scsi_tape *un)
10745 {
10746 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_off\n");
10747 	ASSERT(mutex_owned(ST_MUTEX));
10748 
10749 	/* turn it off for good */
10750 	un->un_persistence = 0;
10751 
10752 	/* this will send a marker pkt */
10753 	CLEAR_PE(un);
10754 
10755 	/* turn off flush on error capability, if enabled */
10756 	if (un->un_flush_on_errors) {
10757 		mutex_exit(ST_MUTEX);
10758 		(void) scsi_ifsetcap(ROUTE, "flush-on-errors", 0, 1);
10759 		mutex_enter(ST_MUTEX);
10760 	}
10761 
10762 
10763 	un->un_flush_on_errors = 0;
10764 }
10765 
10766 /*
10767  * This clear persistent errors, allowing more commands through, and also
10768  * sending a marker packet.
10769  */
10770 static void
10771 st_clear_pe(struct scsi_tape *un)
10772 {
10773 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_clear\n");
10774 	ASSERT(mutex_owned(ST_MUTEX));
10775 
10776 	un->un_persist_errors = 0;
10777 	un->un_throttle = un->un_last_throttle = 1;
10778 	un->un_errno = 0;
10779 	st_hba_unflush(un);
10780 }
10781 
10782 /*
10783  * This will flag persistent errors, shutting everything down, if the
10784  * application had enabled persistent errors via MTIOCPERSISTENT
10785  */
10786 static void
10787 st_set_pe_flag(struct scsi_tape *un)
10788 {
10789 	ASSERT(mutex_owned(ST_MUTEX));
10790 
10791 	if (un->un_persistence) {
10792 		ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG, "st_pe_flag\n");
10793 		un->un_persist_errors = 1;
10794 		un->un_throttle = un->un_last_throttle = 0;
10795 	}
10796 }
10797 
10798 /*
10799  * List of commands that are allowed to be done while another host holds
10800  * the reservation.
10801  */
10802 struct {
10803 	uchar_t cmd;
10804 	uchar_t byte;	/* byte to look for data */
10805 	uint32_t mask;	/* bits that matter in the above data */
10806 } rcmds[] = {
10807 	{ SCMD_TEST_UNIT_READY, 0, 0 }, /* may fail on older drives */
10808 	{ SCMD_REQUEST_SENSE, 0, 0 },
10809 	{ SCMD_READ_BLKLIM, 0, 0 },
10810 	{ SCMD_INQUIRY, 0, 0 },
10811 	{ SCMD_RESERVE, 0, 0 },
10812 	{ SCMD_RELEASE, 0, 0 },
10813 	{ SCMD_DOORLOCK, 4, 3 },	/* allow (unlock) media access only */
10814 	{ SCMD_REPORT_DENSITIES, 0, 0 },
10815 	{ SCMD_LOG_SENSE_G1, 0, 0 },
10816 	{ SCMD_PERSISTENT_RESERVE_IN, 0, 0 },
10817 	{ SCMD_PERSISTENT_RESERVE_OUT, 0, 0 },
10818 	{ SCMD_REPORT_LUNS, 0, 0 }
10819 };
10820 
10821 static int
10822 st_do_reserve(struct scsi_tape *un)
10823 {
10824 	int rval;
10825 
10826 	/*
10827 	 * Issue a Throw-Away reserve command to clear the
10828 	 * check condition.
10829 	 * If the current behaviour of reserve/release is to
10830 	 * hold reservation across opens , and if a Bus reset
10831 	 * has been issued between opens then this command
10832 	 * would set the ST_LOST_RESERVE flags in rsvd_status.
10833 	 * In this case return an EACCES so that user knows that
10834 	 * reservation has been lost in between opens.
10835 	 * If this error is not returned and we continue with
10836 	 * successful open , then user may think position of the
10837 	 * tape is still the same but inreality we would rewind the
10838 	 * tape and continue from BOT.
10839 	 */
10840 	rval = st_reserve_release(un, ST_RESERVE);
10841 	if (rval) {
10842 		if ((un->un_rsvd_status & ST_LOST_RESERVE_BETWEEN_OPENS) ==
10843 		    ST_LOST_RESERVE_BETWEEN_OPENS) {
10844 			un->un_rsvd_status &= ~(ST_LOST_RESERVE | ST_RESERVE);
10845 			un->un_errno = EACCES;
10846 			return (EACCES);
10847 		}
10848 		rval = st_reserve_release(un, ST_RESERVE);
10849 	}
10850 	if (rval == 0) {
10851 		un->un_rsvd_status |= ST_INIT_RESERVE;
10852 	}
10853 
10854 	return (rval);
10855 }
10856 
10857 static int
10858 st_check_cdb_for_need_to_reserve(struct scsi_tape *un, caddr_t cdb)
10859 {
10860 	int i;
10861 	int rval = 0;
10862 
10863 	/*
10864 	 * If already reserved no need to do it again.
10865 	 * Also if Reserve and Release are disabled Just return.
10866 	 */
10867 	if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) ||
10868 	    (un->un_dp->options & ST_NO_RESERVE_RELEASE)) {
10869 		ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
10870 		    "st_check_cdb_for_need_to_reserve() reserve unneeded 0x%x",
10871 		    cdb[0]);
10872 		return (0);
10873 	}
10874 
10875 	/* See if command is on the list */
10876 	for (i = 0; i < ST_NUM_MEMBERS(rcmds); i++) {
10877 		if ((uchar_t)cdb[0] == rcmds[i].cmd) {
10878 			/*
10879 			 * cmd is on list.
10880 			 * if byte is zero always allowed.
10881 			 */
10882 			if (rcmds[i].byte == 0) {
10883 				return (rval);
10884 			}
10885 			if (((cdb[rcmds[i].byte]) & (rcmds[i].mask)) == 0) {
10886 				return (rval);
10887 			}
10888 			break;
10889 		}
10890 	}
10891 
10892 	ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
10893 	    "Command 0x%x requires reservation", cdb[0]);
10894 
10895 	rval = st_do_reserve(un);
10896 
10897 	return (rval);
10898 }
10899 
10900 static int
10901 st_check_cmd_for_need_to_reserve(struct scsi_tape *un, uchar_t cmd, int cnt)
10902 {
10903 	int i;
10904 	int rval = 0;
10905 
10906 	if ((un->un_rsvd_status & (ST_RESERVE | ST_APPLICATION_RESERVATIONS)) ||
10907 	    (un->un_dp->options & ST_NO_RESERVE_RELEASE)) {
10908 		ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
10909 		    "st_check_cmd_for_need_to_reserve() reserve unneeded 0x%x",
10910 		    cmd);
10911 		return (0);
10912 	}
10913 
10914 	/* See if command is on the list */
10915 	for (i = 0; i < ST_NUM_MEMBERS(rcmds); i++) {
10916 		if (cmd == rcmds[i].cmd) {
10917 			/*
10918 			 * cmd is on list.
10919 			 * if byte is zero always allowed.
10920 			 */
10921 			if (rcmds[i].byte == 0) {
10922 				return (rval);
10923 			}
10924 			if (((rcmds[i].mask) & cnt) == 0) {
10925 				return (rval);
10926 			}
10927 			break;
10928 		}
10929 	}
10930 
10931 	ST_DEBUG6(ST_DEVINFO, st_label, CE_NOTE,
10932 	    "Cmd 0x%x requires reservation", cmd);
10933 
10934 	rval = st_do_reserve(un);
10935 
10936 	return (rval);
10937 }
10938 
10939 static int
10940 st_reserve_release(struct scsi_tape *un, int cmd)
10941 {
10942 	struct uscsi_cmd	uscsi_cmd;
10943 	struct uscsi_cmd	*com = &uscsi_cmd;
10944 	int			rval;
10945 	char			cdb[CDB_GROUP0];
10946 
10947 
10948 	ASSERT(mutex_owned(ST_MUTEX));
10949 
10950 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10951 	    "st_reserve_release: %s \n",
10952 	    (cmd == ST_RELEASE)?  "Releasing":"Reserving");
10953 
10954 	bzero(cdb, CDB_GROUP0);
10955 	if (cmd == ST_RELEASE) {
10956 		cdb[0] = SCMD_RELEASE;
10957 	} else {
10958 		cdb[0] = SCMD_RESERVE;
10959 	}
10960 	bzero(com, sizeof (struct uscsi_cmd));
10961 	com->uscsi_flags = USCSI_WRITE;
10962 	com->uscsi_cdb = cdb;
10963 	com->uscsi_cdblen = CDB_GROUP0;
10964 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
10965 
10966 	rval = st_ioctl_cmd(un->un_dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
10967 	    UIO_SYSSPACE);
10968 
10969 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10970 	    "st_reserve_release: rval(1)=%d\n", rval);
10971 
10972 	if (rval) {
10973 		if (com->uscsi_status == STATUS_RESERVATION_CONFLICT) {
10974 			rval = EACCES;
10975 		}
10976 		/*
10977 		 * dynamically turn off reserve/release support
10978 		 * in case of drives which do not support
10979 		 * reserve/release command(ATAPI drives).
10980 		 */
10981 		if (un->un_status == KEY_ILLEGAL_REQUEST) {
10982 			if (un->un_dp->options & ST_NO_RESERVE_RELEASE) {
10983 				un->un_dp->options |= ST_NO_RESERVE_RELEASE;
10984 				ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
10985 				    "Tape unit does not support "
10986 				    "reserve/release \n");
10987 			}
10988 			rval = 0;
10989 		}
10990 	}
10991 	return (rval);
10992 }
10993 
10994 static int
10995 st_take_ownership(dev_t dev)
10996 {
10997 	int rval;
10998 
10999 	GET_SOFT_STATE(dev);
11000 	ASSERT(mutex_owned(ST_MUTEX));
11001 
11002 	ST_DEBUG3(ST_DEVINFO, st_label, SCSI_DEBUG,
11003 		"st_take_ownership: Entering ...\n");
11004 
11005 
11006 	rval = st_reserve_release(un, ST_RESERVE);
11007 	/*
11008 	 * XXX -> Should reset be done only if we get EACCES.
11009 	 * .
11010 	 */
11011 	if (rval) {
11012 		mutex_exit(ST_MUTEX);
11013 		if (scsi_reset(ROUTE, RESET_TARGET) == 0) {
11014 			if (scsi_reset(ROUTE, RESET_ALL) == 0) {
11015 				mutex_enter(ST_MUTEX);
11016 				return (EIO);
11017 			}
11018 		}
11019 		mutex_enter(ST_MUTEX);
11020 		un->un_rsvd_status &=
11021 			~(ST_LOST_RESERVE | ST_RESERVATION_CONFLICT);
11022 
11023 		mutex_exit(ST_MUTEX);
11024 		delay(drv_usectohz(ST_RESERVATION_DELAY));
11025 		mutex_enter(ST_MUTEX);
11026 		/*
11027 		 * remove the check condition.
11028 		 */
11029 		(void) st_reserve_release(un, ST_RESERVE);
11030 		if ((rval = st_reserve_release(un, ST_RESERVE)) != 0) {
11031 			if ((st_reserve_release(un, ST_RESERVE)) != 0) {
11032 				rval = (un->un_rsvd_status &
11033 					ST_RESERVATION_CONFLICT) ? EACCES : EIO;
11034 				return (rval);
11035 			}
11036 		}
11037 		/*
11038 		 * Set tape state to ST_STATE_OFFLINE , in case if
11039 		 * the user wants to continue and start using
11040 		 * the tape.
11041 		 */
11042 		un->un_state = ST_STATE_OFFLINE;
11043 		un->un_rsvd_status |= ST_INIT_RESERVE;
11044 	}
11045 	return (rval);
11046 }
11047 
11048 static int
11049 st_create_errstats(struct scsi_tape *un, int instance)
11050 {
11051 	char	kstatname[KSTAT_STRLEN];
11052 
11053 	/*
11054 	 * Create device error kstats
11055 	 */
11056 
11057 	if (un->un_errstats == (kstat_t *)0) {
11058 		(void) sprintf(kstatname, "st%d,err", instance);
11059 		un->un_errstats = kstat_create("sterr", instance, kstatname,
11060 			"device_error", KSTAT_TYPE_NAMED,
11061 			sizeof (struct st_errstats) / sizeof (kstat_named_t),
11062 			KSTAT_FLAG_PERSISTENT);
11063 
11064 		if (un->un_errstats) {
11065 			struct st_errstats	*stp;
11066 
11067 			stp = (struct st_errstats *)un->un_errstats->ks_data;
11068 			kstat_named_init(&stp->st_softerrs, "Soft Errors",
11069 				KSTAT_DATA_ULONG);
11070 			kstat_named_init(&stp->st_harderrs, "Hard Errors",
11071 				KSTAT_DATA_ULONG);
11072 			kstat_named_init(&stp->st_transerrs, "Transport Errors",
11073 				KSTAT_DATA_ULONG);
11074 			kstat_named_init(&stp->st_vid, "Vendor",
11075 				KSTAT_DATA_CHAR);
11076 			kstat_named_init(&stp->st_pid, "Product",
11077 				KSTAT_DATA_CHAR);
11078 			kstat_named_init(&stp->st_revision, "Revision",
11079 				KSTAT_DATA_CHAR);
11080 			kstat_named_init(&stp->st_serial, "Serial No",
11081 				KSTAT_DATA_CHAR);
11082 			un->un_errstats->ks_private = un;
11083 			un->un_errstats->ks_update = nulldev;
11084 			kstat_install(un->un_errstats);
11085 			/*
11086 			 * Fill in the static data
11087 			 */
11088 			(void) strncpy(&stp->st_vid.value.c[0],
11089 					ST_INQUIRY->inq_vid, 8);
11090 			/*
11091 			 * XXX:  Emulex MT-02 (and emulators) predates
11092 			 *	 SCSI-1 and has no vid & pid inquiry data.
11093 			 */
11094 			if (ST_INQUIRY->inq_len != 0) {
11095 				(void) strncpy(&stp->st_pid.value.c[0],
11096 					ST_INQUIRY->inq_pid, 16);
11097 				(void) strncpy(&stp->st_revision.value.c[0],
11098 					ST_INQUIRY->inq_revision, 4);
11099 				(void) strncpy(&stp->st_serial.value.c[0],
11100 					ST_INQUIRY->inq_serial, 12);
11101 			}
11102 		}
11103 	}
11104 	return (0);
11105 }
11106 
11107 static int
11108 st_validate_tapemarks(struct scsi_tape *un, int fileno, daddr_t blkno)
11109 {
11110 	dev_t dev;
11111 	int rval;
11112 
11113 	ASSERT(MUTEX_HELD(&un->un_sd->sd_mutex));
11114 	ASSERT(mutex_owned(ST_MUTEX));
11115 
11116 	dev = un->un_dev;
11117 
11118 	scsi_log(ST_DEVINFO, st_label, CE_NOTE, "Restoring tape"
11119 	" position at fileno=%x, blkno=%lx....", fileno, blkno);
11120 
11121 	/*
11122 	 * Rewind ? Oh yeah, Fidelity has got the STK F/W changed
11123 	 * so as not to rewind tape on RESETS: Gee, Has life ever
11124 	 * been simple in tape land ?
11125 	 */
11126 	rval = st_cmd(dev, SCMD_REWIND, 0, SYNC_CMD);
11127 	if (rval) {
11128 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
11129 		"Failed to restore the last file and block position: In"
11130 		" this state, Tape will be loaded at BOT during next open");
11131 		un->un_fileno = -1;
11132 		return (rval);
11133 	}
11134 
11135 	if (fileno) {
11136 		rval = st_cmd(dev, SCMD_SPACE, Fmk(fileno), SYNC_CMD);
11137 		if (rval) {
11138 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
11139 			"Failed to restore the last file position: In this "
11140 			" state, Tape will be loaded at BOT during next open");
11141 			un->un_fileno = -1;
11142 			return (rval);
11143 		}
11144 	}
11145 
11146 	if (blkno) {
11147 		rval = st_cmd(dev, SCMD_SPACE, Blk(blkno), SYNC_CMD);
11148 		if (rval) {
11149 			scsi_log(ST_DEVINFO, st_label, CE_WARN,
11150 			"Failed to restore the last block position: In this"
11151 			" state, tape will be loaded at BOT during next open");
11152 			un->un_fileno = -1;
11153 			return (rval);
11154 		}
11155 	}
11156 
11157 	return (0);
11158 }
11159 
11160 /*
11161  * check sense key, ASC, ASCQ in order to determine if the tape needs
11162  * to be ejected
11163  */
11164 
11165 static int
11166 st_check_asc_ascq(struct scsi_tape *un)
11167 {
11168 	struct scsi_extended_sense *sensep = ST_RQSENSE;
11169 	struct tape_failure_code   *code;
11170 
11171 	for (code = st_tape_failure_code; code->key != 0xff; code++) {
11172 		if ((code->key  == sensep->es_key) &&
11173 		    (code->add_code  == sensep->es_add_code) &&
11174 		    (code->qual_code == sensep->es_qual_code))
11175 			return (1);
11176 	}
11177 	return (0);
11178 }
11179 
11180 /*
11181  * st_logpage_supported() sends a Log Sense command with
11182  * page code = 0 = Supported Log Pages Page to the device,
11183  * to see whether the page 'page' is supported.
11184  * Return values are:
11185  * -1 if the Log Sense command fails
11186  * 0 if page is not supported
11187  * 1 if page is supported
11188  */
11189 
11190 static int
11191 st_logpage_supported(dev_t dev, uchar_t page)
11192 {
11193 	uchar_t *sp, *sensep;
11194 	unsigned length;
11195 	struct uscsi_cmd *com;
11196 	int rval;
11197 	char cdb[CDB_GROUP1] = {
11198 		SCMD_LOG_SENSE_G1,
11199 		0,
11200 		SUPPORTED_LOG_PAGES_PAGE,
11201 		0,
11202 		0,
11203 		0,
11204 		0,
11205 		0,
11206 		(char)LOG_SENSE_LENGTH,
11207 		0
11208 	};
11209 
11210 	GET_SOFT_STATE(dev);
11211 	ASSERT(mutex_owned(ST_MUTEX));
11212 
11213 	com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11214 	sensep = kmem_zalloc(LOG_SENSE_LENGTH, KM_SLEEP);
11215 
11216 	com->uscsi_cdb = cdb;
11217 	com->uscsi_cdblen = CDB_GROUP1;
11218 	com->uscsi_bufaddr = (caddr_t)sensep;
11219 	com->uscsi_buflen = LOG_SENSE_LENGTH;
11220 	com->uscsi_flags =
11221 		USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
11222 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
11223 	rval = st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
11224 	if (rval || com->uscsi_status) {
11225 		/* uscsi-command failed */
11226 		rval = -1;
11227 	} else {
11228 
11229 		sp = sensep + 3;
11230 
11231 		for (length = *sp++; length > 0; length--, sp++) {
11232 
11233 			if (*sp == page) {
11234 				rval = 1;
11235 				break;
11236 			}
11237 		}
11238 	}
11239 	kmem_free(com, sizeof (struct uscsi_cmd));
11240 	kmem_free(sensep, LOG_SENSE_LENGTH);
11241 	return (rval);
11242 }
11243 
11244 
11245 /*
11246  * st_check_clean_bit() gets the status of the tape's cleaning bit.
11247  *
11248  * If the device does support the TapeAlert log page, then the cleaning bit
11249  * information will be read from this page. Otherwise we will see if one of
11250  * ST_CLN_TYPE_1, ST_CLN_TYPE_2 or ST_CLN_TYPE_3 is set in the properties of
11251  * the device, which means, that we can get the cleaning bit information via
11252  * a RequestSense command.
11253  * If both methods of getting cleaning bit information are not supported
11254  * st_check_clean_bit() will return with 0. Otherwise st_check_clean_bit()
11255  * returns with
11256  * - MTF_TAPE_CLN_SUPPORTED if cleaning bit is not set or
11257  * - MTF_TAPE_CLN_SUPPORTED | MTF_TAPE_HEAD_DIRTY if cleaning bit is set.
11258  * If the call to st_ioctl_cmd() to do the Log Sense or the Request Sense
11259  * command fails, or if the amount of Request Sense data is not enough, then
11260  *  st_check_clean_bit() returns with -1.
11261  */
11262 
11263 static int
11264 st_check_clean_bit(dev_t dev)
11265 {
11266 	int rval = 0;
11267 
11268 	GET_SOFT_STATE(dev);
11269 
11270 	ASSERT(mutex_owned(ST_MUTEX));
11271 
11272 	if (un->un_HeadClean & TAPE_ALERT_NOT_SUPPORTED) {
11273 		return (rval);
11274 	}
11275 
11276 	if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) {
11277 
11278 		rval = st_logpage_supported(dev, TAPE_SEQUENTIAL_PAGE);
11279 		if (rval == 1) {
11280 
11281 			un->un_HeadClean |= TAPE_SEQUENTIAL_SUPPORTED;
11282 		}
11283 
11284 		rval = st_logpage_supported(dev, TAPE_ALERT_PAGE);
11285 		if (rval == 1) {
11286 
11287 			un->un_HeadClean |= TAPE_ALERT_SUPPORTED;
11288 		}
11289 
11290 		if (un->un_HeadClean == TAPE_ALERT_SUPPORT_UNKNOWN) {
11291 
11292 			un->un_HeadClean = TAPE_ALERT_NOT_SUPPORTED;
11293 		}
11294 	}
11295 
11296 	rval = 0;
11297 
11298 	if (un->un_HeadClean & TAPE_SEQUENTIAL_SUPPORTED) {
11299 
11300 		rval = st_check_sequential_clean_bit(dev);
11301 	}
11302 
11303 	if ((rval <= 0) && (un->un_HeadClean & TAPE_ALERT_SUPPORTED)) {
11304 
11305 		rval = st_check_alert_clean_bit(dev);
11306 	}
11307 
11308 	if ((rval <= 0) && (un->un_dp->options & ST_CLN_MASK)) {
11309 
11310 		rval = st_check_sense_clean_bit(dev);
11311 	}
11312 
11313 	if (rval < 0) {
11314 		return (rval);
11315 	}
11316 
11317 	/*
11318 	 * If found a supported means to check need to clean.
11319 	 */
11320 	if (rval & MTF_TAPE_CLN_SUPPORTED) {
11321 
11322 		/*
11323 		 * head needs to be cleaned.
11324 		 */
11325 		if (rval & MTF_TAPE_HEAD_DIRTY) {
11326 
11327 			/*
11328 			 * Print log message only first time
11329 			 * found needing cleaned.
11330 			 */
11331 			if ((un->un_HeadClean & TAPE_PREVIOUSLY_DIRTY) == 0) {
11332 
11333 				scsi_log(ST_DEVINFO, st_label, CE_WARN,
11334 				    "Periodic head cleaning required");
11335 
11336 				un->un_HeadClean |= TAPE_PREVIOUSLY_DIRTY;
11337 			}
11338 
11339 		} else {
11340 
11341 			un->un_HeadClean &= ~TAPE_PREVIOUSLY_DIRTY;
11342 		}
11343 	}
11344 
11345 	return (rval);
11346 }
11347 
11348 
11349 static int
11350 st_check_sequential_clean_bit(dev_t dev)
11351 {
11352 	int rval;
11353 	int ix;
11354 	ushort_t parameter;
11355 	struct uscsi_cmd *cmd;
11356 	struct log_sequential_page *sp;
11357 	struct log_sequential_page_parameter *prm;
11358 	char cdb[CDB_GROUP1] = {
11359 		SCMD_LOG_SENSE_G1,
11360 		0,
11361 		TAPE_SEQUENTIAL_PAGE | CURRENT_CUMULATIVE_VALUES,
11362 		0,
11363 		0,
11364 		0,
11365 		0,
11366 		(char)(sizeof (struct log_sequential_page) >> 8),
11367 		(char)(sizeof (struct log_sequential_page)),
11368 		0
11369 	};
11370 
11371 	GET_SOFT_STATE(dev);
11372 
11373 	cmd = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11374 	sp  = kmem_zalloc(sizeof (struct log_sequential_page), KM_SLEEP);
11375 
11376 	cmd->uscsi_flags   =
11377 		USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
11378 	cmd->uscsi_timeout = un->un_dp->non_motion_timeout;
11379 	cmd->uscsi_cdb	   = cdb;
11380 	cmd->uscsi_cdblen  = CDB_GROUP1;
11381 	cmd->uscsi_bufaddr = (caddr_t)sp;
11382 	cmd->uscsi_buflen  = sizeof (struct log_sequential_page);
11383 
11384 	rval = st_ioctl_cmd(dev, cmd, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
11385 
11386 	if (rval || cmd->uscsi_status || cmd->uscsi_resid) {
11387 
11388 		rval = -1;
11389 
11390 	} else if (sp->log_page.code != TAPE_SEQUENTIAL_PAGE) {
11391 
11392 		rval = -1;
11393 	}
11394 
11395 	prm = &sp->param[0];
11396 
11397 	for (ix = 0; rval == 0 && ix < TAPE_SEQUENTIAL_PAGE_PARA; ix++) {
11398 
11399 		if (prm->log_param.length == 0) {
11400 			break;
11401 		}
11402 
11403 		parameter = (((prm->log_param.pc_hi << 8) & 0xff00) +
11404 			(prm->log_param.pc_lo & 0xff));
11405 
11406 		if (parameter == SEQUENTIAL_NEED_CLN) {
11407 
11408 			rval = MTF_TAPE_CLN_SUPPORTED;
11409 			if (prm->param_value[prm->log_param.length - 1]) {
11410 
11411 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11412 					    "sequential log says head dirty\n");
11413 				rval |= MTF_TAPE_HEAD_DIRTY;
11414 			}
11415 		}
11416 		prm = (struct log_sequential_page_parameter *)
11417 			&prm->param_value[prm->log_param.length];
11418 	}
11419 
11420 	kmem_free(cmd, sizeof (struct uscsi_cmd));
11421 	kmem_free(sp,  sizeof (struct log_sequential_page));
11422 
11423 	return (rval);
11424 }
11425 
11426 
11427 static int
11428 st_check_alert_clean_bit(dev_t dev)
11429 {
11430 	struct st_tape_alert *ta;
11431 	struct uscsi_cmd *com;
11432 	unsigned ix, length;
11433 	int rval;
11434 	ushort_t parameter;
11435 	char cdb[CDB_GROUP1] = {
11436 		SCMD_LOG_SENSE_G1,
11437 		0,
11438 		TAPE_ALERT_PAGE | CURRENT_THRESHOLD_VALUES,
11439 		0,
11440 		0,
11441 		0,
11442 		0,
11443 		(char)(sizeof (struct st_tape_alert) >> 8),
11444 		(char)(sizeof (struct st_tape_alert)),
11445 		0
11446 	};
11447 
11448 	GET_SOFT_STATE(dev);
11449 
11450 	com = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11451 	ta  = kmem_zalloc(sizeof (struct st_tape_alert), KM_SLEEP);
11452 
11453 	com->uscsi_cdb = cdb;
11454 	com->uscsi_cdblen = CDB_GROUP1;
11455 	com->uscsi_bufaddr = (caddr_t)ta;
11456 	com->uscsi_buflen = sizeof (struct st_tape_alert);
11457 	com->uscsi_flags =
11458 		USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
11459 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
11460 
11461 	rval = st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE, UIO_SYSSPACE);
11462 
11463 	if (rval || com->uscsi_status || com->uscsi_resid) {
11464 
11465 		rval = -1; /* uscsi-command failed */
11466 
11467 	} else if (ta->log_page.code != TAPE_ALERT_PAGE) {
11468 
11469 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11470 		"Not Alert Log Page returned 0x%X\n", ta->log_page.code);
11471 		rval = -1;
11472 	}
11473 
11474 	length = (ta->log_page.length_hi << 8) + ta->log_page.length_lo;
11475 
11476 
11477 	if (length != TAPE_ALERT_PARAMETER_LENGTH) {
11478 
11479 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11480 		    "TapeAlert length %d\n", length);
11481 	}
11482 
11483 
11484 	for (ix = 0; ix < TAPE_ALERT_MAX_PARA; ix++) {
11485 
11486 		/*
11487 		 * if rval is bad before the first pass don't bother
11488 		 */
11489 		if (ix == 0 && rval != 0) {
11490 
11491 			break;
11492 		}
11493 
11494 		parameter = ((ta->param[ix].log_param.pc_hi << 8) +
11495 			ta->param[ix].log_param.pc_lo);
11496 
11497 		/*
11498 		 * check to see if current parameter is of interest.
11499 		 * CLEAN_FOR_ERRORS is vendor specific to 9840 9940 stk's.
11500 		 */
11501 		if ((parameter == CLEAN_NOW) ||
11502 		    (parameter == CLEAN_PERIODIC) ||
11503 		    ((parameter == CLEAN_FOR_ERRORS) &&
11504 		    (un->un_dp->type == ST_TYPE_STK9840))) {
11505 
11506 			rval = MTF_TAPE_CLN_SUPPORTED;
11507 
11508 			if (ta->param[ix].param_value & 1) {
11509 
11510 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11511 					    "alert_page drive needs clean %d\n",
11512 					    parameter);
11513 				un->un_HeadClean |= TAPE_ALERT_STILL_DIRTY;
11514 				rval |= MTF_TAPE_HEAD_DIRTY;
11515 			}
11516 
11517 		} else if (parameter == CLEANING_MEDIA) {
11518 
11519 			if (ta->param[ix].param_value & 1) {
11520 
11521 				ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11522 					    "alert_page drive was cleaned\n");
11523 				un->un_HeadClean &= ~TAPE_ALERT_STILL_DIRTY;
11524 			}
11525 		}
11526 
11527 	}
11528 
11529 	/*
11530 	 * Report it as dirty till we see it cleaned
11531 	 */
11532 	if (un->un_HeadClean & TAPE_ALERT_STILL_DIRTY) {
11533 
11534 		ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11535 			    "alert_page still dirty\n");
11536 		rval |= MTF_TAPE_HEAD_DIRTY;
11537 	}
11538 
11539 	kmem_free(com, sizeof (struct uscsi_cmd));
11540 	kmem_free(ta,  sizeof (struct st_tape_alert));
11541 
11542 	return (rval);
11543 }
11544 
11545 
11546 static int
11547 st_check_sense_clean_bit(dev_t dev)
11548 {
11549 	uchar_t *sensep;
11550 	char cdb[CDB_GROUP0];
11551 	struct uscsi_cmd *com;
11552 	ushort_t byte_pos;
11553 	uchar_t bit_mask;
11554 	unsigned length;
11555 	int index;
11556 	int rval;
11557 
11558 	GET_SOFT_STATE(dev);
11559 
11560 	/*
11561 	 * Since this tape does not support Tape Alert,
11562 	 * we now try to get the cleanbit status via
11563 	 * Request Sense.
11564 	 */
11565 
11566 	if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_1) {
11567 
11568 		index = 0;
11569 
11570 	} else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_2) {
11571 
11572 		index = 1;
11573 
11574 	} else if ((un->un_dp->options & ST_CLN_MASK) == ST_CLN_TYPE_3) {
11575 
11576 		index = 2;
11577 
11578 	} else {
11579 
11580 		return (-1);
11581 	}
11582 
11583 	byte_pos  = st_cln_bit_position[index].cln_bit_byte;
11584 	bit_mask  = st_cln_bit_position[index].cln_bit_mask;
11585 	length = byte_pos + 1;
11586 
11587 	com    = kmem_zalloc(sizeof (struct uscsi_cmd), KM_SLEEP);
11588 	sensep = kmem_zalloc(length, KM_SLEEP);
11589 
11590 	cdb[0] = SCMD_REQUEST_SENSE;
11591 	cdb[1] = 0;
11592 	cdb[2] = 0;
11593 	cdb[3] = 0;
11594 	cdb[4] = (char)length;
11595 	cdb[5] = 0;
11596 
11597 	com->uscsi_cdb = cdb;
11598 	com->uscsi_cdblen = CDB_GROUP0;
11599 	com->uscsi_bufaddr = (caddr_t)sensep;
11600 	com->uscsi_buflen = length;
11601 	com->uscsi_flags =
11602 		USCSI_DIAGNOSE | USCSI_SILENT | USCSI_READ | USCSI_RQENABLE;
11603 	com->uscsi_timeout = un->un_dp->non_motion_timeout;
11604 
11605 	rval = st_ioctl_cmd(dev, com, UIO_SYSSPACE, UIO_SYSSPACE,
11606 			UIO_SYSSPACE);
11607 
11608 	if (rval || com->uscsi_status || com->uscsi_resid) {
11609 
11610 		rval = -1;
11611 
11612 	} else {
11613 
11614 		rval = MTF_TAPE_CLN_SUPPORTED;
11615 		if ((sensep[byte_pos] & bit_mask) == bit_mask) {
11616 
11617 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11618 				    "sense data says head dirty\n");
11619 			rval |= MTF_TAPE_HEAD_DIRTY;
11620 		}
11621 	}
11622 
11623 	kmem_free(com, sizeof (struct uscsi_cmd));
11624 	kmem_free(sensep, length);
11625 	return (rval);
11626 }
11627 
11628 /*
11629  * st_clear_unit_attention
11630  *
11631  *  	run test unit ready's to clear out outstanding
11632  * 	unit attentions.
11633  * 	returns zero for SUCCESS or the errno from st_cmd call
11634  */
11635 static int
11636 st_clear_unit_attentions(dev_t dev_instance, int max_trys)
11637 {
11638 	int	i    = 0;
11639 	int	rval;
11640 
11641 	do {
11642 		rval = st_cmd(dev_instance, SCMD_TEST_UNIT_READY, 0, SYNC_CMD);
11643 	} while ((rval != 0) && (rval != ENXIO) && (++i < max_trys));
11644 	return (rval);
11645 }
11646 
11647 static void
11648 st_calculate_timeouts(struct scsi_tape *un)
11649 {
11650 	if (un->un_dp->non_motion_timeout == 0) {
11651 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11652 			un->un_dp->non_motion_timeout =
11653 				st_io_time * st_long_timeout_x;
11654 		} else {
11655 			un->un_dp->non_motion_timeout = (ushort_t)st_io_time;
11656 		}
11657 	}
11658 
11659 	if (un->un_dp->io_timeout == 0) {
11660 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11661 			un->un_dp->io_timeout = st_io_time * st_long_timeout_x;
11662 		} else {
11663 			un->un_dp->io_timeout = (ushort_t)st_io_time;
11664 		}
11665 	}
11666 
11667 	if (un->un_dp->rewind_timeout == 0) {
11668 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11669 			un->un_dp->rewind_timeout =
11670 				st_space_time * st_long_timeout_x;
11671 		} else {
11672 			un->un_dp->rewind_timeout = (ushort_t)st_space_time;
11673 		}
11674 	}
11675 
11676 	if (un->un_dp->space_timeout == 0) {
11677 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11678 			un->un_dp->space_timeout =
11679 				st_space_time * st_long_timeout_x;
11680 		} else {
11681 			un->un_dp->space_timeout = (ushort_t)st_space_time;
11682 		}
11683 	}
11684 
11685 	if (un->un_dp->load_timeout == 0) {
11686 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11687 			un->un_dp->load_timeout =
11688 				st_space_time * st_long_timeout_x;
11689 		} else {
11690 			un->un_dp->load_timeout = (ushort_t)st_space_time;
11691 		}
11692 	}
11693 
11694 	if (un->un_dp->unload_timeout == 0) {
11695 		if (un->un_dp->options & ST_LONG_TIMEOUTS) {
11696 			un->un_dp->unload_timeout =
11697 				st_space_time * st_long_timeout_x;
11698 		} else {
11699 			un->un_dp->unload_timeout = (ushort_t)st_space_time;
11700 		}
11701 	}
11702 
11703 	if (un->un_dp->erase_timeout == 0) {
11704 		if (un->un_dp->options & ST_LONG_ERASE) {
11705 			un->un_dp->erase_timeout =
11706 				st_space_time * st_long_space_time_x;
11707 		} else {
11708 			un->un_dp->erase_timeout = (ushort_t)st_space_time;
11709 		}
11710 	}
11711 }
11712 
11713 #if defined(__i386) || defined(__amd64)
11714 
11715 /*
11716  * release contig_mem and wake up waiting thread, if any
11717  */
11718 static void
11719 st_release_contig_mem(struct scsi_tape *un, struct contig_mem *cp)
11720 {
11721 	mutex_enter(ST_MUTEX);
11722 
11723 	cp->cm_next = un->un_contig_mem;
11724 	un->un_contig_mem = cp;
11725 	un->un_contig_mem_available_num++;
11726 	cv_broadcast(&un->un_contig_mem_cv);
11727 
11728 	mutex_exit(ST_MUTEX);
11729 }
11730 
11731 /*
11732  * St_get_contig_mem will return a contig_mem if there is one available
11733  * in current system. Otherwise, it will try to alloc one, if the total
11734  * number of contig_mem is within st_max_contig_mem_num.
11735  * It will sleep, if allowed by caller or return NULL, if no contig_mem
11736  * is available for now.
11737  */
11738 static struct contig_mem *
11739 st_get_contig_mem(struct scsi_tape *un, size_t len, int alloc_flags)
11740 {
11741 	size_t rlen;
11742 	struct contig_mem *cp = NULL;
11743 	ddi_acc_handle_t acc_hdl;
11744 	caddr_t addr;
11745 	int big_enough = 0;
11746 	int (*dma_alloc_cb)() = (alloc_flags == KM_SLEEP) ?
11747 		DDI_DMA_SLEEP : DDI_DMA_DONTWAIT;
11748 
11749 	/* Try to get one available contig_mem */
11750 	mutex_enter(ST_MUTEX);
11751 	if (un->un_contig_mem_available_num > 0) {
11752 		ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough);
11753 	} else if (un->un_contig_mem_total_num < st_max_contig_mem_num) {
11754 		/*
11755 		 * we failed to get one. we're going to
11756 		 * alloc one more contig_mem for this I/O
11757 		 */
11758 		mutex_exit(ST_MUTEX);
11759 		cp = (struct contig_mem *)kmem_zalloc(
11760 		    sizeof (struct contig_mem) + biosize(),
11761 		    alloc_flags);
11762 		if (cp == NULL) {
11763 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11764 			    "alloc contig_mem failure\n");
11765 			return (NULL); /* cannot get one */
11766 		}
11767 		cp->cm_bp = (struct buf *)
11768 		    (((caddr_t)cp) + sizeof (struct contig_mem));
11769 		bioinit(cp->cm_bp);
11770 		mutex_enter(ST_MUTEX);
11771 		un->un_contig_mem_total_num++; /* one more available */
11772 	} else {
11773 		/*
11774 		 * we failed to get one and we're NOT allowed to
11775 		 * alloc more contig_mem
11776 		 */
11777 		if (alloc_flags == KM_SLEEP) {
11778 			while (un->un_contig_mem_available_num <= 0) {
11779 				cv_wait(&un->un_contig_mem_cv,
11780 				    ST_MUTEX);
11781 			}
11782 			ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough);
11783 		} else {
11784 			mutex_exit(ST_MUTEX);
11785 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11786 			    "alloc contig_mem failure\n");
11787 			return (NULL); /* cannot get one */
11788 		}
11789 	}
11790 	mutex_exit(ST_MUTEX);
11791 
11792 	/* We need to check if this block of mem is big enough for this I/O */
11793 	if (cp->cm_len < len) {
11794 		/* not big enough, need to alloc a new one */
11795 		if (ddi_dma_mem_alloc(un->un_contig_mem_hdl, len, &st_acc_attr,
11796 			DDI_DMA_STREAMING, dma_alloc_cb, NULL,
11797 			&addr, &rlen, &acc_hdl) != DDI_SUCCESS) {
11798 			ST_DEBUG2(ST_DEVINFO, st_label, SCSI_DEBUG,
11799 			    "alloc contig_mem failure: not enough mem\n");
11800 			st_release_contig_mem(un, cp);
11801 			cp = NULL;
11802 		} else {
11803 			if (cp->cm_addr) {
11804 				/* release previous one before attach new one */
11805 				ddi_dma_mem_free(&cp->cm_acc_hdl);
11806 			}
11807 			mutex_enter(ST_MUTEX);
11808 			un->un_max_contig_mem_len =
11809 			    un->un_max_contig_mem_len >= len ?
11810 			    un->un_max_contig_mem_len : len;
11811 			mutex_exit(ST_MUTEX);
11812 
11813 			/* attach new mem to this cp */
11814 			cp->cm_addr = addr;
11815 			cp->cm_acc_hdl = acc_hdl;
11816 			cp->cm_len = len;
11817 
11818 			goto alloc_ok; /* get one usable cp */
11819 		}
11820 	} else {
11821 		goto alloc_ok; /* get one usable cp */
11822 	}
11823 
11824 	/* cannot find/alloc a usable cp, when we get here */
11825 
11826 	if ((un->un_max_contig_mem_len < len) ||
11827 	    (alloc_flags != KM_SLEEP)) {
11828 		return (NULL);
11829 	}
11830 
11831 	/*
11832 	 * we're allowed to sleep, and there is one big enough
11833 	 * contig mem in the system, which is currently in use,
11834 	 * wait for it...
11835 	 */
11836 	mutex_enter(ST_MUTEX);
11837 	big_enough = 1;
11838 	do {
11839 		cv_wait(&un->un_contig_mem_cv, ST_MUTEX);
11840 		ST_GET_CONTIG_MEM_HEAD(un, cp, len, big_enough);
11841 	} while (cp == NULL);
11842 	mutex_exit(ST_MUTEX);
11843 
11844 	/* we get the big enough contig mem, finally */
11845 
11846 alloc_ok:
11847 	/* init bp attached to this cp */
11848 	bioreset(cp->cm_bp);
11849 	cp->cm_bp->b_un.b_addr = cp->cm_addr;
11850 	cp->cm_bp->b_private = (void *)cp;
11851 
11852 	return (cp);
11853 }
11854 
11855 /*
11856  * this is the biodone func for the bp used in big block I/O
11857  */
11858 static int
11859 st_bigblk_xfer_done(struct buf *bp)
11860 {
11861 	struct contig_mem *cp;
11862 	struct buf *orig_bp;
11863 	int remapped = 0;
11864 	int ioerr;
11865 	struct scsi_tape *un;
11866 
11867 	/* sanity check */
11868 	if (bp == NULL) {
11869 		return (DDI_FAILURE);
11870 	}
11871 
11872 	un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev));
11873 	if (un == NULL) {
11874 		return (DDI_FAILURE);
11875 	}
11876 
11877 	cp = (struct contig_mem *)bp->b_private;
11878 	orig_bp = cp->cm_bp; /* get back the bp we have replaced */
11879 	cp->cm_bp = bp;
11880 
11881 	/* special handling for special I/O */
11882 	if (cp->cm_use_sbuf) {
11883 		ASSERT(un->un_sbuf_busy);
11884 		un->un_sbufp = orig_bp;
11885 		cp->cm_use_sbuf = 0;
11886 	}
11887 
11888 	orig_bp->b_resid = bp->b_resid;
11889 	ioerr = geterror(bp);
11890 	if (ioerr != 0) {
11891 		bioerror(orig_bp, ioerr);
11892 	} else if (orig_bp->b_flags & B_READ) {
11893 		/* copy data back to original bp */
11894 		if (orig_bp->b_flags & (B_PHYS | B_PAGEIO)) {
11895 			bp_mapin(orig_bp);
11896 			remapped = 1;
11897 		}
11898 		bcopy(bp->b_un.b_addr, orig_bp->b_un.b_addr,
11899 			bp->b_bcount - bp->b_resid);
11900 		if (remapped)
11901 			bp_mapout(orig_bp);
11902 	}
11903 
11904 	st_release_contig_mem(un, cp);
11905 
11906 	biodone(orig_bp);
11907 
11908 	return (DDI_SUCCESS);
11909 }
11910 
11911 /*
11912  * We use this func to replace original bp that may not be able to do I/O
11913  * in big block size with one that can
11914  */
11915 static struct buf *
11916 st_get_bigblk_bp(struct buf *bp)
11917 {
11918 	struct contig_mem *cp;
11919 	struct scsi_tape *un;
11920 	struct buf *cont_bp;
11921 	int remapped = 0;
11922 
11923 	un = ddi_get_soft_state(st_state, MTUNIT(bp->b_edev));
11924 	if (un == NULL) {
11925 		return (bp);
11926 	}
11927 
11928 	/* try to get one contig_mem */
11929 	cp = st_get_contig_mem(un, bp->b_bcount, KM_SLEEP);
11930 	if (!cp) {
11931 		scsi_log(ST_DEVINFO, st_label, CE_WARN,
11932 			"Cannot alloc contig buf for I/O for %lu blk size",
11933 			bp->b_bcount);
11934 		return (bp);
11935 	}
11936 	cont_bp = cp->cm_bp;
11937 	cp->cm_bp = bp;
11938 
11939 	/* make sure that we "are" using un_sbufp for special I/O */
11940 	if (bp == un->un_sbufp) {
11941 		ASSERT(un->un_sbuf_busy);
11942 		un->un_sbufp = cont_bp;
11943 		cp->cm_use_sbuf = 1;
11944 	}
11945 
11946 	/* clone bp */
11947 	cont_bp->b_bcount = bp->b_bcount;
11948 	cont_bp->b_resid = bp->b_resid;
11949 	cont_bp->b_iodone = st_bigblk_xfer_done;
11950 	cont_bp->b_file = bp->b_file;
11951 	cont_bp->b_offset = bp->b_offset;
11952 	cont_bp->b_dip = bp->b_dip;
11953 	cont_bp->b_error = 0;
11954 	cont_bp->b_proc = NULL;
11955 	cont_bp->b_flags = bp->b_flags & ~(B_PAGEIO | B_PHYS | B_SHADOW);
11956 	cont_bp->b_shadow = NULL;
11957 	cont_bp->b_pages = NULL;
11958 	cont_bp->b_edev = bp->b_edev;
11959 	cont_bp->b_dev = bp->b_dev;
11960 	cont_bp->b_lblkno = bp->b_lblkno;
11961 	cont_bp->b_forw = bp->b_forw;
11962 	cont_bp->b_back = bp->b_back;
11963 	cont_bp->av_forw = bp->av_forw;
11964 	cont_bp->av_back = bp->av_back;
11965 	cont_bp->b_bufsize = bp->b_bufsize;
11966 
11967 	/* get data in original bp */
11968 	if (bp->b_flags & B_WRITE) {
11969 		if (bp->b_flags & (B_PHYS | B_PAGEIO)) {
11970 			bp_mapin(bp);
11971 			remapped = 1;
11972 		}
11973 		bcopy(bp->b_un.b_addr, cont_bp->b_un.b_addr, bp->b_bcount);
11974 		if (remapped)
11975 			bp_mapout(bp);
11976 	}
11977 
11978 	return (cont_bp);
11979 }
11980 #endif
11981