st.c revision 1.106
1/*	$OpenBSD: st.c,v 1.106 2010/07/22 05:26:34 matthew Exp $	*/
2/*	$NetBSD: st.c,v 1.71 1997/02/21 23:03:49 thorpej Exp $	*/
3
4/*
5 * Copyright (c) 1994 Charles Hannum.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Charles Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33/*
34 * Originally written by Julian Elischer (julian@tfs.com)
35 * for TRW Financial Systems for use under the MACH(2.5) operating system.
36 *
37 * TRW Financial Systems, in accordance with their agreement with Carnegie
38 * Mellon University, makes this software available to CMU to distribute
39 * or use in any manner that they see fit as long as this message is kept with
40 * the software. For this reason TFS also grants any other persons or
41 * organisations permission to use or modify this software.
42 *
43 * TFS supplies this software to be publicly redistributed
44 * on the understanding that TFS is not responsible for the correct
45 * functioning of this software in any circumstances.
46 *
47 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
48 * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993
49 */
50
51/*
52 * To do:
53 * work out some better way of guessing what a good timeout is going
54 * to be depending on whether we expect to retension or not.
55 */
56
57#include <sys/types.h>
58#include <sys/param.h>
59#include <sys/systm.h>
60#include <sys/timeout.h>
61#include <sys/fcntl.h>
62#include <sys/errno.h>
63#include <sys/ioctl.h>
64#include <sys/stat.h>
65#include <sys/malloc.h>
66#include <sys/buf.h>
67#include <sys/proc.h>
68#include <sys/mtio.h>
69#include <sys/device.h>
70#include <sys/conf.h>
71#include <sys/vnode.h>
72
73#include <scsi/scsi_all.h>
74#include <scsi/scsi_tape.h>
75#include <scsi/scsiconf.h>
76
77/* Defines for device specific stuff */
78#define DEF_FIXED_BSIZE  512
79
80#define STMODE(z)	( minor(z)	 & 0x03)
81#define STUNIT(z)	((minor(z) >> 4)       )
82
83#define	ST_IO_TIME	(3 * 60 * 1000)		/* 3 minutes */
84#define	ST_CTL_TIME	(30 * 1000)		/* 30 seconds */
85#define	ST_SPC_TIME	(4 * 60 * 60 * 1000)	/* 4 hours */
86
87/*
88 * Maximum density code allowed in SCSI spec (SSC2R08f, Section 8.3).
89 */
90#define SCSI_MAX_DENSITY_CODE		0xff
91
92/*
93 * Define various devices that we know mis-behave in some way,
94 * and note how they are bad, so we can correct for them
95 */
96struct modes {
97	u_int quirks;			/* same definitions as in quirkdata */
98	int blksize;
99	u_int8_t density;
100};
101
102struct quirkdata {
103	u_int quirks;
104#define	ST_Q_FORCE_BLKSIZE	0x0001
105#define	ST_Q_SENSE_HELP		0x0002	/* must do READ for good MODE SENSE */
106#define	ST_Q_IGNORE_LOADS	0x0004
107#define	ST_Q_BLKSIZE		0x0008	/* variable-block media_blksize > 0 */
108#define	ST_Q_UNIMODAL		0x0010	/* unimode drive rejects mode select */
109	struct modes modes;
110};
111
112struct st_quirk_inquiry_pattern {
113	struct scsi_inquiry_pattern pattern;
114	struct quirkdata quirkdata;
115};
116
117const struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
118	{{T_SEQUENTIAL, T_REMOV,
119	 "        ", "                ", "    "}, {0,
120		{ST_Q_FORCE_BLKSIZE, 512, 0}}},		/* minor 0-3 */
121	{{T_SEQUENTIAL, T_REMOV,
122	 "TANDBERG", " TDC 3600       ", ""},     {0,
123		{0, 0, 0}}},				/* minor 0-3 */
124 	{{T_SEQUENTIAL, T_REMOV,
125 	 "TANDBERG", " TDC 3800       ", ""},     {0,
126		{ST_Q_FORCE_BLKSIZE, 512, 0}}},		/* minor 0-3 */
127	/*
128	 * At least -005 and -007 need this.  I'll assume they all do unless I
129	 * hear otherwise.  - mycroft, 31MAR1994
130	 */
131	{{T_SEQUENTIAL, T_REMOV,
132	 "ARCHIVE ", "VIPER 2525 25462", ""},     {0,
133		{ST_Q_SENSE_HELP, 0, 0}}},		/* minor 0-3 */
134	/*
135	 * One user reports that this works for his tape drive.  It probably
136	 * needs more work.  - mycroft, 09APR1994
137	 */
138	{{T_SEQUENTIAL, T_REMOV,
139	 "SANKYO  ", "CP525           ", ""},    {0,
140		{ST_Q_FORCE_BLKSIZE, 512, 0}}},		/* minor 0-3 */
141	{{T_SEQUENTIAL, T_REMOV,
142	 "ANRITSU ", "DMT780          ", ""},     {0,
143		{ST_Q_FORCE_BLKSIZE, 512, 0}}},		/* minor 0-3 */
144	{{T_SEQUENTIAL, T_REMOV,
145	 "ARCHIVE ", "VIPER 150  21247", ""},     {0,
146		{0, 0, 0}}},				/* minor 0-3 */
147	{{T_SEQUENTIAL, T_REMOV,
148	 "ARCHIVE ", "VIPER 150  21531", ""},     {0,
149		{ST_Q_SENSE_HELP, 0, 0}}},		/* minor 0-3 */
150	{{T_SEQUENTIAL, T_REMOV,
151	 "WANGTEK ", "5099ES SCSI", ""},          {0,
152		{ST_Q_FORCE_BLKSIZE, 512, 0}}},		/* minor 0-3 */
153	{{T_SEQUENTIAL, T_REMOV,
154	 "WANGTEK ", "5150ES SCSI", ""},          {0,
155		{ST_Q_FORCE_BLKSIZE, 512, 0}}},		/* minor 0-3 */
156	{{T_SEQUENTIAL, T_REMOV,
157	 "WANGTEK ", "5525ES SCSI REV7", ""},     {0,
158		{0, 0, 0}}},				/* minor 0-3 */
159	{{T_SEQUENTIAL, T_REMOV,
160	 "WangDAT ", "Model 1300      ", ""},     {0,
161		{0, 0, 0}}},				/* minor 0-3 */
162	{{T_SEQUENTIAL, T_REMOV,
163	 "EXABYTE ", "EXB-8200        ", "263H"}, {0,
164		{0, 0, 0}}},				/* minor 0-3 */
165	{{T_SEQUENTIAL, T_REMOV,
166	 "HP      ", "T4000s          ", ""},     {ST_Q_UNIMODAL,
167		{0, 0, QIC_3095}}},			/* minor 0-3 */
168#if 0
169	{{T_SEQUENTIAL, T_REMOV,
170	 "EXABYTE ", "EXB-8200        ", ""},     {0,
171		{0, 0, 0}}},				/* minor 0-3 */
172#endif
173	{{T_SEQUENTIAL, T_REMOV,
174	 "WANGTEK ", "5150ES SCSI FA15\0""01 A", "????"}, {0,
175		{ST_Q_IGNORE_LOADS, 0, 0}}},		/* minor 0-3 */
176	{{T_SEQUENTIAL, T_REMOV,
177	 "TEAC    ", "MT-2ST/N50      ", ""},     {ST_Q_IGNORE_LOADS,
178		{0, 0, 0}}},				/* minor 0-3 */
179};
180
181#define NOEJECT 0
182#define EJECT 1
183
184#define NOREWIND 0
185#define DOREWIND 1
186
187struct st_softc {
188	struct device sc_dev;
189
190	int flags;		/* see below                          */
191	u_int quirks;		/* quirks for the open mode           */
192	int blksize;		/* blksize we are using               */
193	u_int8_t density;	/* present density                    */
194	short mt_resid;		/* last (short) resid                 */
195	short mt_erreg;		/* last error (sense key) seen        */
196
197	struct scsi_link *sc_link;	/* our link to the adpter etc.        */
198
199	int blkmin;		/* min blk size                       */
200	int blkmax;		/* max blk size                       */
201	const struct quirkdata *quirkdata;	/* if we have a rogue entry */
202
203	u_int64_t numblks;		/* nominal blocks capacity            */
204	u_int32_t media_blksize;	/* 0 if not ST_FIXEDBLOCKS            */
205	u_int32_t media_density;	/* this is what it said when asked    */
206	int media_fileno;		/* relative to BOT. -1 means unknown. */
207	int media_blkno;		/* relative to BOF. -1 means unknown. */
208
209	u_int drive_quirks;	/* quirks of this drive               */
210
211	struct modes modes;	/* plus more for each mode            */
212	u_int8_t  modeflags;	/* flags for the modes                */
213#define DENSITY_SET_BY_USER	0x01
214#define DENSITY_SET_BY_QUIRK	0x02
215#define BLKSIZE_SET_BY_USER	0x04
216#define BLKSIZE_SET_BY_QUIRK	0x08
217
218	struct bufq *sc_bufq;
219	struct timeout sc_timeout;
220	struct scsi_xshandler sc_xsh;
221};
222
223
224int	stmatch(struct device *, void *, void *);
225void	stattach(struct device *, struct device *, void *);
226int	stactivate(struct device *, int);
227int	stdetach(struct device *, int);
228
229void	stminphys(struct buf *);
230void	st_identify_drive(struct st_softc *, struct scsi_inquiry_data *);
231void	st_loadquirks(struct st_softc *);
232int	st_mount_tape(dev_t, int);
233void	st_unmount(struct st_softc *, int, int);
234int	st_decide_mode(struct st_softc *, int);
235void	ststart(struct scsi_xfer *);
236void	st_buf_done(struct scsi_xfer *);
237int	st_read(struct st_softc *, char *, int, int);
238int	st_read_block_limits(struct st_softc *, int);
239int	st_mode_sense(struct st_softc *, int);
240int	st_mode_select(struct st_softc *, int);
241int	st_space(struct st_softc *, int, u_int, int);
242int	st_write_filemarks(struct st_softc *, int, int);
243int	st_check_eod(struct st_softc *, int, int *, int);
244int	st_load(struct st_softc *, u_int, int);
245int	st_rewind(struct st_softc *, u_int, int);
246int	st_interpret_sense(struct scsi_xfer *);
247int	st_touch_tape(struct st_softc *);
248int	st_erase(struct st_softc *, int, int);
249
250struct cfattach st_ca = {
251	sizeof(struct st_softc), stmatch, stattach,
252	stdetach, stactivate
253};
254
255struct cfdriver st_cd = {
256	NULL, "st", DV_TAPE
257};
258
259#define	ST_INFO_VALID	0x0001
260#define	ST_BLOCK_SET	0x0002	/* block size, mode set by ioctl      */
261#define	ST_WRITTEN	0x0004	/* data have been written, EOD needed */
262#define	ST_FIXEDBLOCKS	0x0008
263#define	ST_AT_FILEMARK	0x0010
264#define	ST_EIO_PENDING	0x0020	/* we couldn't report it then (had data) */
265#define	ST_READONLY	0x0080	/* st_mode_sense says write protected */
266#define	ST_FM_WRITTEN	0x0100	/*
267				 * EOF file mark written  -- used with
268				 * ~ST_WRITTEN to indicate that multiple file
269				 * marks have been written
270				 */
271#define	ST_DYING	0x40	/* dying, when deactivated */
272#define	ST_BLANK_READ	0x0200	/* BLANK CHECK encountered already */
273#define	ST_2FM_AT_EOD	0x0400	/* write 2 file marks at EOD */
274#define	ST_MOUNTED	0x0800	/* Device is presently mounted */
275#define	ST_DONTBUFFER	0x1000	/* Disable buffering/caching */
276#define ST_WAITING	0x2000
277
278#define	ST_PER_ACTION	(ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
279#define	ST_PER_MOUNT	(ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
280			 ST_FIXEDBLOCKS | ST_READONLY | ST_FM_WRITTEN | \
281			 ST_2FM_AT_EOD | ST_PER_ACTION)
282
283#define stlookup(unit) (struct st_softc *)device_lookup(&st_cd, (unit))
284
285const struct scsi_inquiry_pattern st_patterns[] = {
286	{T_SEQUENTIAL, T_REMOV,
287	 "",         "",                 ""},
288};
289
290int
291stmatch(struct device *parent, void *match, void *aux)
292{
293	struct scsi_attach_args *sa = aux;
294	int priority;
295
296	(void)scsi_inqmatch(sa->sa_inqbuf,
297	    st_patterns, sizeof(st_patterns)/sizeof(st_patterns[0]),
298	    sizeof(st_patterns[0]), &priority);
299	return (priority);
300}
301
302/*
303 * The routine called by the low level scsi routine when it discovers
304 * A device suitable for this driver
305 */
306void
307stattach(struct device *parent, struct device *self, void *aux)
308{
309	struct st_softc *st = (void *)self;
310	struct scsi_attach_args *sa = aux;
311	struct scsi_link *sc_link = sa->sa_sc_link;
312
313	SC_DEBUG(sc_link, SDEV_DB2, ("stattach:\n"));
314
315	/*
316	 * Store information needed to contact our base driver
317	 */
318	st->sc_link = sc_link;
319	sc_link->interpret_sense = st_interpret_sense;
320	sc_link->device_softc = st;
321
322	/*
323	 * Check if the drive is a known criminal and take
324	 * Any steps needed to bring it into line
325	 */
326	st_identify_drive(st, sa->sa_inqbuf);
327	printf("\n");
328
329	scsi_xsh_set(&st->sc_xsh, sc_link, ststart);
330	timeout_set(&st->sc_timeout, (void (*)(void *))scsi_xsh_set,
331	    &st->sc_xsh);
332
333	/* Set up the buf queue for this device. */
334	st->sc_bufq = bufq_init(BUFQ_FIFO);
335
336	/* Start up with media position unknown. */
337	st->media_fileno = -1;
338	st->media_blkno = -1;
339
340	/*
341	 * Reset the media loaded flag, sometimes the data
342	 * acquired at boot time is not quite accurate.  This
343	 * will be checked again at the first open.
344	 */
345	sc_link->flags &= ~SDEV_MEDIA_LOADED;
346}
347
348int
349stactivate(struct device *self, int act)
350{
351	struct st_softc *st = (struct st_softc *)self;
352	int rv = 0;
353
354	switch (act) {
355	case DVACT_ACTIVATE:
356		break;
357
358	case DVACT_DEACTIVATE:
359		st->flags |= ST_DYING;
360		bufq_drain(st->sc_bufq);
361		break;
362	}
363
364	return (rv);
365}
366
367int
368stdetach(struct device *self, int flags)
369{
370	struct st_softc *st = (struct st_softc *)self;
371	int bmaj, cmaj, mn;
372
373	bufq_drain(st->sc_bufq);
374
375	/* Locate the lowest minor number to be detached. */
376	mn = STUNIT(self->dv_unit);
377
378	for (bmaj = 0; bmaj < nblkdev; bmaj++)
379		if (bdevsw[bmaj].d_open == stopen) {
380			vdevgone(bmaj, mn, mn + 0, VBLK);
381			vdevgone(bmaj, mn, mn + 1, VBLK);
382			vdevgone(bmaj, mn, mn + 2, VBLK);
383			vdevgone(bmaj, mn, mn + 3, VBLK);
384		}
385	for (cmaj = 0; cmaj < nchrdev; cmaj++)
386		if (cdevsw[cmaj].d_open == stopen) {
387			vdevgone(cmaj, mn, mn + 0, VCHR);
388			vdevgone(cmaj, mn, mn + 1, VCHR);
389			vdevgone(cmaj, mn, mn + 2, VCHR);
390			vdevgone(cmaj, mn, mn + 3, VCHR);
391		}
392
393	bufq_destroy(st->sc_bufq);
394
395	return (0);
396}
397
398/*
399 * Use the inquiry routine in 'scsi_base' to get drive info so we can
400 * Further tailor our behaviour.
401 */
402void
403st_identify_drive(struct st_softc *st, struct scsi_inquiry_data *inqbuf)
404{
405	const struct st_quirk_inquiry_pattern *finger;
406	int priority;
407
408	finger = (const struct st_quirk_inquiry_pattern *)scsi_inqmatch(inqbuf,
409	    st_quirk_patterns,
410	    sizeof(st_quirk_patterns)/sizeof(st_quirk_patterns[0]),
411	    sizeof(st_quirk_patterns[0]), &priority);
412	if (priority != 0) {
413		st->quirkdata = &finger->quirkdata;
414		st->drive_quirks = finger->quirkdata.quirks;
415		st->quirks = finger->quirkdata.quirks;	/* start value */
416		st_loadquirks(st);
417	}
418}
419
420/*
421 * initialise the subdevices to the default (QUIRK) state.
422 * this will remove any setting made by the system operator or previous
423 * operations.
424 */
425void
426st_loadquirks(struct st_softc *st)
427{
428	const struct	modes *mode;
429	struct	modes *mode2;
430
431	mode = &st->quirkdata->modes;
432	mode2 = &st->modes;
433	bzero(mode2, sizeof(struct modes));
434	st->modeflags &= ~(BLKSIZE_SET_BY_QUIRK |
435	    DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
436	    DENSITY_SET_BY_USER);
437	if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
438		mode2->blksize = mode->blksize;
439		st->modeflags |= BLKSIZE_SET_BY_QUIRK;
440	}
441	if (mode->density) {
442		mode2->density = mode->density;
443		st->modeflags |= DENSITY_SET_BY_QUIRK;
444	}
445}
446
447/*
448 * open the device.
449 */
450int
451stopen(dev_t dev, int flags, int fmt, struct proc *p)
452{
453	struct scsi_link *sc_link;
454	struct st_softc *st;
455	int error = 0;
456
457	st = stlookup(STUNIT(dev));
458	if (st == NULL)
459		return (ENXIO);
460	if (st->flags & ST_DYING) {
461		error = ENXIO;
462		goto done;
463	}
464	sc_link = st->sc_link;
465
466	SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
467	    STUNIT(dev), st_cd.cd_ndevs));
468
469	/*
470	 * Tape is an exclusive media. Only one open at a time.
471	 */
472	if (sc_link->flags & SDEV_OPEN) {
473		SC_DEBUG(sc_link, SDEV_DB4, ("already open\n"));
474		error = EBUSY;
475		goto done;
476	}
477
478	/* Use st_interpret_sense() now. */
479	sc_link->flags |= SDEV_OPEN;
480
481	/*
482	 * Check the unit status. This clears any outstanding errors and
483	 * will ensure that media is present.
484	 */
485	error = scsi_test_unit_ready(sc_link, TEST_READY_RETRIES,
486	    SCSI_SILENT | SCSI_IGNORE_MEDIA_CHANGE |
487	    SCSI_IGNORE_ILLEGAL_REQUEST);
488
489	/*
490	 * Terminate any exising mount session if there is no media.
491	 */
492	if ((sc_link->flags & SDEV_MEDIA_LOADED) == 0)
493		st_unmount(st, NOEJECT, DOREWIND);
494
495	if (error) {
496		sc_link->flags &= ~SDEV_OPEN;
497		goto done;
498	}
499
500	if ((st->flags & ST_MOUNTED) == 0) {
501		error = st_mount_tape(dev, flags);
502		if (error) {
503			sc_link->flags &= ~SDEV_OPEN;
504			goto done;
505		}
506	}
507
508	/*
509	 * Make sure that a tape opened in write-only mode will have
510	 * file marks written on it when closed, even if not written to.
511	 * This is for SUN compatibility
512	 */
513	if ((flags & O_ACCMODE) == FWRITE)
514		st->flags |= ST_WRITTEN;
515
516done:
517	SC_DEBUG(sc_link, SDEV_DB2, ("open complete\n"));
518	device_unref(&st->sc_dev);
519	return (error);
520}
521
522/*
523 * close the device.. only called if we are the LAST
524 * occurrence of an open device
525 */
526int
527stclose(dev_t dev, int flags, int mode, struct proc *p)
528{
529	struct scsi_link *sc_link;
530	struct st_softc *st;
531	int error = 0;
532
533	st = stlookup(STUNIT(dev));
534	if (st == NULL)
535		return (ENXIO);
536	if (st->flags & ST_DYING) {
537		error = ENXIO;
538		goto done;
539	}
540	sc_link = st->sc_link;
541
542	SC_DEBUG(sc_link, SDEV_DB1, ("closing\n"));
543
544	if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
545		st_write_filemarks(st, 1, 0);
546
547	switch (STMODE(dev)) {
548	case 0:		/* normal */
549		st_unmount(st, NOEJECT, DOREWIND);
550		break;
551	case 3:		/* eject, no rewind */
552		st_unmount(st, EJECT, NOREWIND);
553		break;
554	case 1:		/* no rewind */
555		/* leave mounted unless media seems to have been removed */
556		if (!(sc_link->flags & SDEV_MEDIA_LOADED))
557			st_unmount(st, NOEJECT, NOREWIND);
558		break;
559	case 2:		/* rewind, eject */
560		st_unmount(st, EJECT, DOREWIND);
561		break;
562	}
563	sc_link->flags &= ~SDEV_OPEN;
564	timeout_del(&st->sc_timeout);
565	scsi_xsh_del(&st->sc_xsh);
566
567done:
568	device_unref(&st->sc_dev);
569	return (error);
570}
571
572/*
573 * Start a new mount session.
574 * Copy in all the default parameters from the selected device mode.
575 * and try guess any that seem to be defaulted.
576 */
577int
578st_mount_tape(dev_t dev, int flags)
579{
580	struct st_softc *st;
581	struct scsi_link *sc_link;
582	int error = 0;
583
584	st = stlookup(STUNIT(dev));
585	if (st == NULL)
586		return (ENXIO);
587	if (st->flags & ST_DYING) {
588		error = ENXIO;
589		goto done;
590	}
591	sc_link = st->sc_link;
592
593	SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n"));
594
595	if (st->flags & ST_MOUNTED)
596		goto done;
597
598	st->quirks = st->drive_quirks | st->modes.quirks;
599	/*
600	 * If the media is new, then make sure we give it a chance to
601	 * to do a 'load' instruction.  (We assume it is new.)
602	 */
603	if ((error = st_load(st, LD_LOAD, 0)) != 0)
604		goto done;
605
606	/*
607	 * Throw another dummy instruction to catch
608	 * 'Unit attention' errors. Some drives appear to give
609	 * these after doing a Load instruction.
610	 * (noteably some DAT drives)
611	 */
612	/* XXX */
613	scsi_test_unit_ready(sc_link, TEST_READY_RETRIES, SCSI_SILENT);
614
615	/*
616	 * Some devices can't tell you much until they have been
617	 * asked to look at the media. This quirk does this.
618	 */
619	if (st->quirks & ST_Q_SENSE_HELP)
620		if ((error = st_touch_tape(st)) != 0)
621			return error;
622	/*
623	 * Load the physical device parameters
624	 * loads: blkmin, blkmax
625	 */
626	if (!(sc_link->flags & SDEV_ATAPI) &&
627	    (error = st_read_block_limits(st, 0)) != 0)
628		goto done;
629
630	/*
631	 * Load the media dependent parameters
632	 * includes: media_blksize,media_density,numblks
633	 * As we have a tape in, it should be reflected here.
634	 * If not you may need the "quirk" above.
635	 */
636	if ((error = st_mode_sense(st, 0)) != 0)
637		goto done;
638
639	/*
640	 * If we have gained a permanent density from somewhere,
641	 * then use it in preference to the one supplied by
642	 * default by the driver.
643	 */
644	if (st->modeflags & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
645		st->density = st->modes.density;
646	else
647		st->density = st->media_density;
648	/*
649	 * If we have gained a permanent blocksize
650	 * then use it in preference to the one supplied by
651	 * default by the driver.
652	 */
653	st->flags &= ~ST_FIXEDBLOCKS;
654	if (st->modeflags & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
655		st->blksize = st->modes.blksize;
656		if (st->blksize)
657			st->flags |= ST_FIXEDBLOCKS;
658	} else {
659		if ((error = st_decide_mode(st, FALSE)) != 0)
660			goto done;
661	}
662	if ((error = st_mode_select(st, 0)) != 0) {
663		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
664		goto done;
665	}
666	scsi_prevent(sc_link, PR_PREVENT,
667	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
668	st->flags |= ST_MOUNTED;
669	sc_link->flags |= SDEV_MEDIA_LOADED;	/* move earlier? */
670
671done:
672	device_unref(&st->sc_dev);
673	return (error);
674}
675
676/*
677 * End the present mount session.
678 * Rewind, and optionally eject the tape.
679 * Reset various flags to indicate that all new
680 * operations require another mount operation
681 */
682void
683st_unmount(struct st_softc *st, int eject, int rewind)
684{
685	struct scsi_link *sc_link = st->sc_link;
686	int nmarks;
687
688	st->media_fileno = -1;
689	st->media_blkno = -1;
690
691	if (!(st->flags & ST_MOUNTED))
692		return;
693	SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
694	st_check_eod(st, FALSE, &nmarks, SCSI_IGNORE_NOT_READY);
695	if (rewind)
696		st_rewind(st, 0, SCSI_IGNORE_NOT_READY);
697	scsi_prevent(sc_link, PR_ALLOW,
698	    SCSI_IGNORE_ILLEGAL_REQUEST | SCSI_IGNORE_NOT_READY);
699	if (eject)
700		st_load(st, LD_UNLOAD, SCSI_IGNORE_NOT_READY);
701	st->flags &= ~ST_MOUNTED;
702	sc_link->flags &= ~SDEV_MEDIA_LOADED;
703}
704
705/*
706 * Given all we know about the device, media, mode, 'quirks' and
707 * initial operation, make a decision as to how we should be set
708 * to run (regarding blocking and EOD marks)
709 */
710int
711st_decide_mode(struct st_softc *st, int first_read)
712{
713	struct scsi_link *sc_link = st->sc_link;
714
715	SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
716
717	/* ATAPI tapes are always fixed blocksize. */
718	if (sc_link->flags & SDEV_ATAPI) {
719		st->flags |= ST_FIXEDBLOCKS;
720		if (st->media_blksize > 0)
721			st->blksize = st->media_blksize;
722		else
723			st->blksize = DEF_FIXED_BSIZE;
724		goto done;
725	}
726
727	/*
728	 * If the drive can only handle fixed-length blocks and only at
729	 * one size, perhaps we should just do that.
730	 */
731	if (st->blkmin && (st->blkmin == st->blkmax)) {
732		st->flags |= ST_FIXEDBLOCKS;
733		st->blksize = st->blkmin;
734		SC_DEBUG(sc_link, SDEV_DB3,
735		    ("blkmin == blkmax of %d\n", st->blkmin));
736		goto done;
737	}
738	/*
739	 * If the tape density mandates (or even suggests) use of fixed
740	 * or variable-length blocks, comply.
741	 */
742	switch (st->density) {
743	case HALFINCH_800:
744	case HALFINCH_1600:
745	case HALFINCH_6250:
746	case DDS:
747		st->flags &= ~ST_FIXEDBLOCKS;
748		st->blksize = 0;
749		SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
750		goto done;
751	case QIC_11:
752	case QIC_24:
753	case QIC_120:
754	case QIC_150:
755	case QIC_525:
756	case QIC_1320:
757		st->flags |= ST_FIXEDBLOCKS;
758		if (st->media_blksize > 0)
759			st->blksize = st->media_blksize;
760		else
761			st->blksize = DEF_FIXED_BSIZE;
762		SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
763		goto done;
764	}
765	/*
766	 * If we're about to read the tape, perhaps we should choose
767	 * fixed or variable-length blocks and block size according to
768	 * what the drive found on the tape.
769	 */
770	if (first_read &&
771	    (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
772	    (st->media_blksize == DEF_FIXED_BSIZE) ||
773	    (st->media_blksize == 1024))) {
774		if (st->media_blksize > 0)
775			st->flags |= ST_FIXEDBLOCKS;
776		else
777			st->flags &= ~ST_FIXEDBLOCKS;
778		st->blksize = st->media_blksize;
779		SC_DEBUG(sc_link, SDEV_DB3,
780		    ("Used media_blksize of %d\n", st->media_blksize));
781		goto done;
782	}
783	/*
784	 * We're getting no hints from any direction.  Choose variable-
785	 * length blocks arbitrarily.
786	 */
787	st->flags &= ~ST_FIXEDBLOCKS;
788	st->blksize = 0;
789	SC_DEBUG(sc_link, SDEV_DB3,
790	    ("Give up and default to variable mode\n"));
791
792done:
793	/*
794	 * Decide whether or not to write two file marks to signify end-
795	 * of-data.  Make the decision as a function of density.  If
796	 * the decision is not to use a second file mark, the SCSI BLANK
797	 * CHECK condition code will be recognized as end-of-data when
798	 * first read.
799	 * (I think this should be a by-product of fixed/variable..julian)
800	 */
801	switch (st->density) {
802/*      case 8 mm:   What is the SCSI density code for 8 mm, anyway? */
803	case QIC_11:
804	case QIC_24:
805	case QIC_120:
806	case QIC_150:
807	case QIC_525:
808	case QIC_1320:
809		st->flags &= ~ST_2FM_AT_EOD;
810		break;
811	default:
812		st->flags |= ST_2FM_AT_EOD;
813	}
814	return 0;
815}
816
817/*
818 * Actually translate the requested transfer into
819 * one the physical driver can understand
820 * The transfer is described by a buf and will include
821 * only one physical transfer.
822 */
823void
824ststrategy(struct buf *bp)
825{
826	struct scsi_link *sc_link;
827	struct st_softc *st;
828	int s;
829
830	st = stlookup(STUNIT(bp->b_dev));
831	if (st == NULL) {
832		bp->b_error = ENXIO;
833		goto bad;
834	}
835	if (st->flags & ST_DYING) {
836		bp->b_error = ENXIO;
837		goto bad;
838	}
839	sc_link = st->sc_link;
840
841	SC_DEBUG(sc_link, SDEV_DB2, ("ststrategy: %ld bytes @ blk %d\n",
842	    bp->b_bcount, bp->b_blkno));
843
844	if (st->flags & ST_DYING) {
845		bp->b_error = ENXIO;
846		goto bad;
847	}
848	/*
849	 * If it's a null transfer, return immediately.
850	 */
851	if (bp->b_bcount == 0)
852		goto done;
853	/*
854	 * Odd sized request on fixed drives are verboten
855	 */
856	if (st->flags & ST_FIXEDBLOCKS) {
857		if (bp->b_bcount % st->blksize) {
858			printf("%s: bad request, must be multiple of %d\n",
859			    st->sc_dev.dv_xname, st->blksize);
860			bp->b_error = EIO;
861			goto bad;
862		}
863	}
864	/*
865	 * as are out-of-range requests on variable drives.
866	 */
867	else if (bp->b_bcount < st->blkmin ||
868		 (st->blkmax && bp->b_bcount > st->blkmax)) {
869		printf("%s: bad request, must be between %d and %d\n",
870		    st->sc_dev.dv_xname, st->blkmin, st->blkmax);
871		bp->b_error = EIO;
872		goto bad;
873	}
874
875	/*
876	 * Place it in the queue of activities for this tape
877	 * at the end (a bit silly because we only have on user..
878	 * (but it could fork()))
879	 */
880	BUFQ_QUEUE(st->sc_bufq, bp);
881
882	/*
883	 * Tell the device to get going on the transfer if it's
884	 * not doing anything, otherwise just wait for completion
885	 * (All a bit silly if we're only allowing 1 open but..)
886	 */
887	scsi_xsh_add(&st->sc_xsh);
888
889	device_unref(&st->sc_dev);
890	return;
891bad:
892	bp->b_flags |= B_ERROR;
893done:
894	/* Set b_resid to indicate no xfer was done. */
895	bp->b_resid = bp->b_bcount;
896	s = splbio();
897	biodone(bp);
898	splx(s);
899	if (st)
900		device_unref(&st->sc_dev);
901}
902
903void
904ststart(struct scsi_xfer *xs)
905{
906	struct scsi_link *sc_link = xs->sc_link;
907	struct st_softc *st = sc_link->device_softc;
908	struct buf *bp;
909	struct scsi_rw_tape *cmd;
910	int s;
911
912	SC_DEBUG(sc_link, SDEV_DB2, ("ststart\n"));
913
914	if (st->flags & ST_DYING)
915		return;
916
917	/*
918	 * if the device has been unmounted by the user
919	 * then throw away all requests until done
920	 */
921	if (!(st->flags & ST_MOUNTED) ||
922	    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
923		/* make sure that one implies the other.. */
924		sc_link->flags &= ~SDEV_MEDIA_LOADED;
925		bufq_drain(st->sc_bufq);
926		scsi_xs_put(xs);
927		return;
928	}
929
930	for (;;) {
931		bp = BUFQ_DEQUEUE(st->sc_bufq);
932		if (bp == NULL) {
933			scsi_xs_put(xs);
934			return;
935		}
936
937		/*
938		 * only FIXEDBLOCK devices have pending operations
939		 */
940		if (st->flags & ST_FIXEDBLOCKS) {
941			/*
942			 * If we are at a filemark but have not reported it yet
943			 * then we should report it now
944			 */
945			if (st->flags & ST_AT_FILEMARK) {
946				if ((bp->b_flags & B_READ) == B_WRITE) {
947					/*
948					 * Handling of ST_AT_FILEMARK in
949					 * st_space will fill in the right file
950					 * mark count.
951					 * Back up over filemark
952					 */
953					if (st_space(st, 0, SP_FILEMARKS, 0)) {
954						bp->b_flags |= B_ERROR;
955						bp->b_resid = bp->b_bcount;
956						bp->b_error = EIO;
957						s = splbio();
958						biodone(bp);
959						splx(s);
960						continue;
961					}
962				} else {
963					bp->b_resid = bp->b_bcount;
964					bp->b_error = 0;
965					bp->b_flags &= ~B_ERROR;
966					st->flags &= ~ST_AT_FILEMARK;
967					s = splbio();
968					biodone(bp);
969					splx(s);
970					continue;	/* seek more work */
971				}
972			}
973			/*
974			 * If we are at EIO (e.g. EOM) but have not reported it
975			 * yet then we should report it now
976			 */
977			if (st->flags & ST_EIO_PENDING) {
978				bp->b_resid = bp->b_bcount;
979				bp->b_error = EIO;
980				bp->b_flags |= B_ERROR;
981				st->flags &= ~ST_EIO_PENDING;
982				s = splbio();
983				biodone(bp);
984				splx(s);
985				continue;	/* seek more work */
986			}
987		}
988
989		break;
990	}
991
992
993	/*
994	 *  Fill out the scsi command
995	 */
996	cmd = (struct scsi_rw_tape *)xs->cmd;
997	bzero(cmd, sizeof(*cmd));
998	if ((bp->b_flags & B_READ) == B_WRITE) {
999		cmd->opcode = WRITE;
1000		st->flags &= ~ST_FM_WRITTEN;
1001		st->flags |= ST_WRITTEN;
1002		xs->flags |= SCSI_DATA_OUT;
1003	} else {
1004		cmd->opcode = READ;
1005		xs->flags |= SCSI_DATA_IN;
1006	}
1007
1008	/*
1009	 * Handle "fixed-block-mode" tape drives by using the
1010	 * block count instead of the length.
1011	 */
1012	if (st->flags & ST_FIXEDBLOCKS) {
1013		cmd->byte2 |= SRW_FIXED;
1014		_lto3b(bp->b_bcount / st->blksize, cmd->len);
1015	} else
1016		_lto3b(bp->b_bcount, cmd->len);
1017
1018	if (st->media_blkno != -1) {
1019		/* Update block count now, errors will set it to -1. */
1020		if (st->flags & ST_FIXEDBLOCKS)
1021			st->media_blkno += _3btol(cmd->len);
1022		else if (cmd->len != 0)
1023			st->media_blkno++;
1024	}
1025
1026	xs->cmdlen = sizeof(*cmd);
1027	xs->timeout = ST_IO_TIME;
1028	xs->data = bp->b_data;
1029	xs->datalen = bp->b_bcount;
1030	xs->done = st_buf_done;
1031	xs->cookie = bp;
1032	xs->bp = bp;
1033
1034	/*
1035	 * go ask the adapter to do all this for us
1036	 */
1037	scsi_xs_exec(xs);
1038
1039	/*
1040	 * should we try do more work now?
1041	 */
1042	if (ISSET(st->flags, ST_WAITING))
1043		CLR(st->flags, ST_WAITING);
1044	else if (BUFQ_PEEK(st->sc_bufq))
1045		scsi_xsh_add(&st->sc_xsh);
1046}
1047
1048void
1049st_buf_done(struct scsi_xfer *xs)
1050{
1051	struct st_softc *st = xs->sc_link->device_softc;
1052	struct buf *bp = xs->cookie;
1053	int error, s;
1054
1055	switch (xs->error) {
1056	case XS_NOERROR:
1057		bp->b_error = 0;
1058		bp->b_resid = xs->resid;
1059		break;
1060
1061	case XS_NO_CCB:
1062		/* The adapter is busy, requeue the buf and try it later. */
1063		BUFQ_REQUEUE(st->sc_bufq, bp);
1064		scsi_xs_put(xs);
1065		SET(st->flags, ST_WAITING); /* dont let ststart xsh_add */
1066		timeout_add(&st->sc_timeout, 1);
1067		return;
1068
1069	case XS_SENSE:
1070	case XS_SHORTSENSE:
1071#ifdef SCSIDEBUG
1072		scsi_sense_print_debug(xs);
1073#endif
1074		error = st_interpret_sense(xs);
1075		if (error == 0) {
1076			bp->b_error = 0;
1077			bp->b_resid = xs->resid;
1078			break;
1079		}
1080		if (error != ERESTART)
1081			xs->retries = 0;
1082		goto retry;
1083
1084	case XS_BUSY:
1085		if (xs->retries) {
1086			if (scsi_delay(xs, 1) != ERESTART)
1087				xs->retries = 0;
1088		}
1089		goto retry;
1090
1091	case XS_TIMEOUT:
1092retry:
1093		if (xs->retries--) {
1094			scsi_xs_exec(xs);
1095			return;
1096		}
1097		/* FALLTHROUGH */
1098
1099	default:
1100		bp->b_error = EIO;
1101		bp->b_flags |= B_ERROR;
1102		bp->b_resid = bp->b_bcount;
1103		break;
1104	}
1105
1106	s = splbio();
1107	biodone(bp);
1108	splx(s);
1109	scsi_xs_put(xs);
1110}
1111
1112void
1113stminphys(struct buf *bp)
1114{
1115	struct st_softc *st;
1116
1117	st = stlookup(STUNIT(bp->b_dev));
1118	if (st == NULL)
1119		return;  /* can't happen */
1120
1121	(*st->sc_link->adapter->scsi_minphys)(bp, st->sc_link);
1122
1123	device_unref(&st->sc_dev);
1124}
1125
1126int
1127stread(dev_t dev, struct uio *uio, int iomode)
1128{
1129	struct st_softc *st;
1130
1131	st = stlookup(STUNIT(dev));
1132	if (st == NULL)
1133		return (ENXIO);
1134
1135	if (st->flags & ST_DYING) {
1136		device_unref(&st->sc_dev);
1137		return (ENXIO);
1138	}
1139
1140	return (physio(ststrategy, NULL, dev, B_READ, stminphys, uio));
1141}
1142
1143int
1144stwrite(dev_t dev, struct uio *uio, int iomode)
1145{
1146	struct st_softc *st;
1147
1148	st = stlookup(STUNIT(dev));
1149	if (st == NULL)
1150		return (ENXIO);
1151
1152	if (st->flags & ST_DYING) {
1153		device_unref(&st->sc_dev);
1154		return (ENXIO);
1155	}
1156
1157	return (physio(ststrategy, NULL, dev, B_WRITE, stminphys, uio));
1158}
1159
1160/*
1161 * Perform special action on behalf of the user;
1162 * knows about the internals of this device
1163 */
1164int
1165stioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
1166{
1167	int error = 0;
1168	int nmarks;
1169	int flags = 0;
1170	struct st_softc *st;
1171	int hold_blksize;
1172	u_int8_t hold_density;
1173	struct mtop *mt = (struct mtop *) arg;
1174	int number;
1175
1176	/*
1177	 * Find the device that the user is talking about
1178	 */
1179	st = stlookup(STUNIT(dev));
1180	if (st == NULL)
1181		return (ENXIO);
1182
1183	if (st->flags & ST_DYING) {
1184		error = ENXIO;
1185		goto done;
1186	}
1187
1188	hold_blksize = st->blksize;
1189	hold_density = st->density;
1190
1191	switch (cmd) {
1192
1193	case MTIOCGET: {
1194		struct mtget *g = (struct mtget *) arg;
1195
1196		/*
1197		 * (to get the current state of READONLY)
1198		 */
1199		error = st_mode_sense(st, SCSI_SILENT);
1200		if (error)
1201			break;
1202
1203		SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
1204		bzero(g, sizeof(struct mtget));
1205		g->mt_type = 0x7;	/* Ultrix compat *//*? */
1206		g->mt_blksiz = st->blksize;
1207		g->mt_density = st->density;
1208 		g->mt_mblksiz = st->modes.blksize;
1209 		g->mt_mdensity = st->modes.density;
1210		if (st->flags & ST_READONLY)
1211			g->mt_dsreg |= MT_DS_RDONLY;
1212		if (st->flags & ST_MOUNTED)
1213			g->mt_dsreg |= MT_DS_MOUNTED;
1214		g->mt_resid = st->mt_resid;
1215		g->mt_erreg = st->mt_erreg;
1216		g->mt_fileno = st->media_fileno;
1217		g->mt_blkno = st->media_blkno;
1218		/*
1219		 * clear latched errors.
1220		 */
1221		st->mt_resid = 0;
1222		st->mt_erreg = 0;
1223		break;
1224	}
1225	case MTIOCTOP: {
1226
1227		SC_DEBUG(st->sc_link, SDEV_DB1,
1228		    ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op, mt->mt_count));
1229
1230		number = mt->mt_count;
1231		switch (mt->mt_op) {
1232		case MTWEOF:	/* write an end-of-file record */
1233			error = st_write_filemarks(st, number, flags);
1234			break;
1235		case MTBSF:	/* backward space file */
1236			number = -number;
1237		case MTFSF:	/* forward space file */
1238			error = st_check_eod(st, FALSE, &nmarks, flags);
1239			if (!error)
1240				error = st_space(st, number - nmarks,
1241				    SP_FILEMARKS, flags);
1242			break;
1243		case MTBSR:	/* backward space record */
1244			number = -number;
1245		case MTFSR:	/* forward space record */
1246			error = st_check_eod(st, TRUE, &nmarks, flags);
1247			if (!error)
1248				error = st_space(st, number, SP_BLKS, flags);
1249			break;
1250		case MTREW:	/* rewind */
1251			error = st_rewind(st, 0, flags);
1252			break;
1253		case MTOFFL:	/* rewind and put the drive offline */
1254			st_unmount(st, EJECT, DOREWIND);
1255			break;
1256		case MTNOP:	/* no operation, sets status only */
1257			break;
1258		case MTRETEN:	/* retension the tape */
1259			error = st_load(st, LD_RETENSION, flags);
1260			if (!error)
1261				error = st_load(st, LD_LOAD, flags);
1262			break;
1263		case MTEOM:	/* forward space to end of media */
1264			error = st_check_eod(st, FALSE, &nmarks, flags);
1265			if (!error)
1266				error = st_space(st, 1, SP_EOM, flags);
1267			break;
1268		case MTCACHE:	/* enable controller cache */
1269			st->flags &= ~ST_DONTBUFFER;
1270			goto try_new_value;
1271		case MTNOCACHE:	/* disable controller cache */
1272			st->flags |= ST_DONTBUFFER;
1273			goto try_new_value;
1274		case MTERASE:	/* erase volume */
1275			error = st_erase(st, number, flags);
1276			break;
1277		case MTSETBSIZ:	/* Set block size for device */
1278			if (number == 0) {
1279				st->flags &= ~ST_FIXEDBLOCKS;
1280			} else {
1281				if ((st->blkmin || st->blkmax) &&
1282				    (number < st->blkmin ||
1283				    number > st->blkmax)) {
1284					error = EINVAL;
1285					break;
1286				}
1287				st->flags |= ST_FIXEDBLOCKS;
1288			}
1289			st->blksize = number;
1290			st->flags |= ST_BLOCK_SET;	/*XXX */
1291			goto try_new_value;
1292
1293		case MTSETDNSTY:	/* Set density for device and mode */
1294			if (number < 0 || number > SCSI_MAX_DENSITY_CODE) {
1295				error = EINVAL;
1296				break;
1297			} else
1298				st->density = number;
1299			goto try_new_value;
1300
1301		default:
1302			error = EINVAL;
1303		}
1304		break;
1305	}
1306	case MTIOCIEOT:
1307	case MTIOCEEOT:
1308		break;
1309
1310#if 0
1311	case MTIOCRDSPOS:
1312		error = st_rdpos(st, 0, (u_int32_t *) arg);
1313		break;
1314
1315	case MTIOCRDHPOS:
1316		error = st_rdpos(st, 1, (u_int32_t *) arg);
1317		break;
1318
1319	case MTIOCSLOCATE:
1320		error = st_setpos(st, 0, (u_int32_t *) arg);
1321		break;
1322
1323	case MTIOCHLOCATE:
1324		error = st_setpos(st, 1, (u_int32_t *) arg);
1325		break;
1326#endif
1327
1328	default:
1329		error = scsi_do_ioctl(st->sc_link, cmd, arg, flag);
1330		break;
1331	}
1332	goto done;
1333
1334try_new_value:
1335	/*
1336	 * Check that the mode being asked for is aggreeable to the
1337	 * drive. If not, put it back the way it was.
1338	 */
1339	if ((error = st_mode_select(st, 0)) != 0) {/* put it back as it was */
1340		printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1341		st->density = hold_density;
1342		st->blksize = hold_blksize;
1343		if (st->blksize)
1344			st->flags |= ST_FIXEDBLOCKS;
1345		else
1346			st->flags &= ~ST_FIXEDBLOCKS;
1347		goto done;
1348	}
1349	/*
1350	 * As the drive liked it, if we are setting a new default,
1351	 * set it into the structures as such.
1352	 */
1353	switch (mt->mt_op) {
1354	case MTSETBSIZ:
1355		st->modes.blksize = st->blksize;
1356		st->modeflags |= BLKSIZE_SET_BY_USER;
1357		break;
1358	case MTSETDNSTY:
1359		st->modes.density = st->density;
1360		st->modeflags |= DENSITY_SET_BY_USER;
1361		break;
1362	}
1363
1364done:
1365	device_unref(&st->sc_dev);
1366	return (error);
1367}
1368
1369/*
1370 * Do a synchronous read.
1371 */
1372int
1373st_read(struct st_softc *st, char *buf, int size, int flags)
1374{
1375	struct scsi_rw_tape *cmd;
1376	struct scsi_xfer *xs;
1377	int error;
1378
1379	if (size == 0)
1380		return 0;
1381
1382	xs = scsi_xs_get(st->sc_link, flags | SCSI_DATA_IN);
1383	if (xs == NULL)
1384		return (ENOMEM);
1385	xs->cmd->opcode = READ;
1386	xs->cmdlen = sizeof(*cmd);
1387	xs->data = buf;
1388	xs->datalen = size;
1389	xs->retries = 0;
1390	xs->timeout = ST_IO_TIME;
1391
1392	cmd = (struct scsi_rw_tape *)xs->cmd;
1393	if (st->flags & ST_FIXEDBLOCKS) {
1394		cmd->byte2 |= SRW_FIXED;
1395		_lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
1396		    cmd->len);
1397	} else
1398		_lto3b(size, cmd->len);
1399
1400	error = scsi_xs_sync(xs);
1401	scsi_xs_put(xs);
1402
1403	return (error);
1404}
1405
1406/*
1407 * Ask the drive what its min and max blk sizes are.
1408 */
1409int
1410st_read_block_limits(struct st_softc *st, int flags)
1411{
1412	struct scsi_block_limits_data block_limits;
1413	struct scsi_block_limits *cmd;
1414	struct scsi_link *sc_link = st->sc_link;
1415	struct scsi_xfer *xs;
1416	int error;
1417
1418	if ((sc_link->flags & SDEV_MEDIA_LOADED))
1419		return (0);
1420
1421	xs = scsi_xs_get(sc_link, flags | SCSI_DATA_IN);
1422	if (xs == NULL)
1423		return (ENOMEM);
1424	xs->cmd->opcode = READ_BLOCK_LIMITS;
1425	xs->cmdlen = sizeof(*cmd);
1426	xs->data = (void *)&block_limits;
1427	xs->datalen = sizeof(block_limits);
1428	xs->timeout = ST_CTL_TIME;
1429
1430	error = scsi_xs_sync(xs);
1431	scsi_xs_put(xs);
1432
1433	if (error == 0) {
1434		st->blkmin = _2btol(block_limits.min_length);
1435		st->blkmax = _3btol(block_limits.max_length);
1436		SC_DEBUG(sc_link, SDEV_DB3,
1437		    ("(%d <= blksize <= %d)\n", st->blkmin, st->blkmax));
1438	}
1439
1440	return (error);
1441}
1442
1443/*
1444 * Get the scsi driver to send a full inquiry to the
1445 * device and use the results to fill out the global
1446 * parameter structure.
1447 *
1448 * called from:
1449 * attach
1450 * open
1451 * ioctl (to reset original blksize)
1452 */
1453int
1454st_mode_sense(struct st_softc *st, int flags)
1455{
1456	union scsi_mode_sense_buf *data;
1457	struct scsi_link *sc_link = st->sc_link;
1458	u_int64_t block_count;
1459	u_int32_t density, block_size;
1460	u_char *page0 = NULL;
1461	u_int8_t dev_spec;
1462	int error, big;
1463
1464	data = malloc(sizeof(*data), M_TEMP, M_NOWAIT);
1465	if (data == NULL)
1466		return (ENOMEM);
1467
1468	/*
1469	 * Ask for page 0 (vendor specific) mode sense data.
1470	 */
1471	error = scsi_do_mode_sense(sc_link, 0, data, (void **)&page0,
1472	    &density, &block_count, &block_size, 1, flags | SCSI_SILENT, &big);
1473	if (error != 0) {
1474		free(data, M_TEMP);
1475		return (error);
1476	}
1477
1478	/* It is valid for no page0 to be available. */
1479
1480	if (big)
1481		dev_spec = data->hdr_big.dev_spec;
1482	else
1483		dev_spec = data->hdr.dev_spec;
1484
1485	if (dev_spec & SMH_DSP_WRITE_PROT)
1486		st->flags |= ST_READONLY;
1487	else
1488		st->flags &= ~ST_READONLY;
1489
1490	st->numblks = block_count;
1491	st->media_blksize = block_size;
1492	st->media_density = density;
1493
1494	SC_DEBUG(sc_link, SDEV_DB3,
1495	    ("density code 0x%x, %d-byte blocks, write-%s, ",
1496	    st->media_density, st->media_blksize,
1497	    st->flags & ST_READONLY ? "protected" : "enabled"));
1498	SC_DEBUGN(sc_link, SDEV_DB3,
1499	    ("%sbuffered\n", dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
1500
1501	sc_link->flags |= SDEV_MEDIA_LOADED;
1502
1503	free(data, M_TEMP);
1504	return (0);
1505}
1506
1507/*
1508 * Send a filled out parameter structure to the drive to
1509 * set it into the desire modes etc.
1510 */
1511int
1512st_mode_select(struct st_softc *st, int flags)
1513{
1514	union scsi_mode_sense_buf *inbuf, *outbuf;
1515	struct scsi_blk_desc general;
1516	struct scsi_link *sc_link = st->sc_link;
1517	u_int8_t *page0 = NULL;
1518	int error, big, page0_size;
1519
1520	inbuf = malloc(sizeof(*inbuf), M_TEMP, M_NOWAIT);
1521	if (inbuf == NULL)
1522		return (ENOMEM);
1523	outbuf = malloc(sizeof(*outbuf), M_TEMP, M_NOWAIT | M_ZERO);
1524	if (outbuf == NULL) {
1525		free(inbuf, M_TEMP);
1526		return (ENOMEM);
1527	}
1528
1529	/*
1530	 * This quirk deals with drives that have only one valid mode and think
1531	 * this gives them license to reject all mode selects, even if the
1532	 * selected mode is the one that is supported.
1533	 */
1534	if (st->quirks & ST_Q_UNIMODAL) {
1535		SC_DEBUG(sc_link, SDEV_DB3,
1536		    ("not setting density 0x%x blksize 0x%x\n",
1537		    st->density, st->blksize));
1538		free(inbuf, M_TEMP);
1539		free(outbuf, M_TEMP);
1540		return (0);
1541	}
1542
1543	if (sc_link->flags & SDEV_ATAPI) {
1544		free(inbuf, M_TEMP);
1545		free(outbuf, M_TEMP);
1546		return (0);
1547	}
1548
1549	bzero(&general, sizeof(general));
1550
1551	general.density = st->density;
1552	if (st->flags & ST_FIXEDBLOCKS)
1553		_lto3b(st->blksize, general.blklen);
1554
1555	/*
1556	 * Ask for page 0 (vendor specific) mode sense data.
1557	 */
1558	error = scsi_do_mode_sense(sc_link, 0, inbuf, (void **)&page0, NULL,
1559	    NULL, NULL, 1, flags | SCSI_SILENT, &big);
1560	if (error != 0) {
1561		free(inbuf, M_TEMP);
1562		free(outbuf, M_TEMP);
1563		return (error);
1564	}
1565
1566	if (page0 == NULL) {
1567		page0_size = 0;
1568	} else if (big == 0) {
1569		page0_size = inbuf->hdr.data_length +
1570		    sizeof(inbuf->hdr.data_length) - sizeof(inbuf->hdr) -
1571		    inbuf->hdr.blk_desc_len;
1572		memcpy(&outbuf->buf[sizeof(outbuf->hdr)+ sizeof(general)],
1573		    page0, page0_size);
1574	} else {
1575		page0_size = _2btol(inbuf->hdr_big.data_length) +
1576		    sizeof(inbuf->hdr_big.data_length) -
1577		    sizeof(inbuf->hdr_big) -
1578		   _2btol(inbuf->hdr_big.blk_desc_len);
1579		memcpy(&outbuf->buf[sizeof(outbuf->hdr_big) + sizeof(general)],
1580		    page0, page0_size);
1581	}
1582
1583	/*
1584	 * Set up for a mode select.
1585	 */
1586	if (big == 0) {
1587		outbuf->hdr.data_length = sizeof(outbuf->hdr) +
1588		    sizeof(general) + page0_size -
1589		    sizeof(outbuf->hdr.data_length);
1590		if ((st->flags & ST_DONTBUFFER) == 0)
1591			outbuf->hdr.dev_spec = SMH_DSP_BUFF_MODE_ON;
1592		outbuf->hdr.blk_desc_len = sizeof(general);
1593		memcpy(&outbuf->buf[sizeof(outbuf->hdr)],
1594		    &general, sizeof(general));
1595		error = scsi_mode_select(st->sc_link, 0, &outbuf->hdr,
1596		    flags, ST_CTL_TIME);
1597		free(inbuf, M_TEMP);
1598		free(outbuf, M_TEMP);
1599		return (error);
1600	}
1601
1602	/* MODE SENSE (10) header was returned, so use MODE SELECT (10). */
1603	_lto2b((sizeof(outbuf->hdr_big) + sizeof(general) + page0_size -
1604	    sizeof(outbuf->hdr_big.data_length)), outbuf->hdr_big.data_length);
1605	if ((st->flags & ST_DONTBUFFER) == 0)
1606		outbuf->hdr_big.dev_spec = SMH_DSP_BUFF_MODE_ON;
1607	_lto2b(sizeof(general), outbuf->hdr_big.blk_desc_len);
1608	memcpy(&outbuf->buf[sizeof(outbuf->hdr_big)], &general,
1609	    sizeof(general));
1610
1611	error = scsi_mode_select_big(st->sc_link, 0, &outbuf->hdr_big,
1612	    flags, ST_CTL_TIME);
1613	free(inbuf, M_TEMP);
1614	free(outbuf, M_TEMP);
1615	return (error);
1616}
1617
1618/*
1619 * issue an erase command
1620 */
1621int
1622st_erase(struct st_softc *st, int full, int flags)
1623{
1624	struct scsi_erase *cmd;
1625	struct scsi_xfer *xs;
1626	int error;
1627
1628	xs = scsi_xs_get(st->sc_link, flags);
1629	if (xs == NULL)
1630		return (ENOMEM);
1631	xs->cmd->opcode = ERASE;
1632	xs->cmdlen = sizeof(*cmd);
1633
1634	/*
1635	 * Full erase means set LONG bit in erase command, which asks
1636	 * the drive to erase the entire unit.  Without this bit, we're
1637	 * asking the drive to write an erase gap.
1638	 */
1639	cmd = (struct scsi_erase *)xs->cmd;
1640	if (full) {
1641		cmd->byte2 = SE_IMMED|SE_LONG;
1642		xs->timeout = ST_SPC_TIME;
1643	} else {
1644		cmd->byte2 = SE_IMMED;
1645		xs->timeout = ST_IO_TIME;
1646	}
1647
1648	/*
1649	 * XXX We always do this asynchronously, for now.  How long should
1650	 * we wait if we want to (eventually) to it synchronously?
1651	 */
1652	error = scsi_xs_sync(xs);
1653	scsi_xs_put(xs);
1654
1655	return (error);
1656}
1657
1658/*
1659 * skip N blocks/filemarks/seq filemarks/eom
1660 */
1661int
1662st_space(struct st_softc *st, int number, u_int what, int flags)
1663{
1664	struct scsi_space *cmd;
1665	struct scsi_xfer *xs;
1666	int error;
1667
1668	switch (what) {
1669	case SP_BLKS:
1670		if (st->flags & ST_PER_ACTION) {
1671			if (number > 0) {
1672				st->flags &= ~ST_PER_ACTION;
1673				return EIO;
1674			} else if (number < 0) {
1675				if (st->flags & ST_AT_FILEMARK) {
1676					/*
1677					 * Handling of ST_AT_FILEMARK
1678					 * in st_space will fill in the
1679					 * right file mark count.
1680					 */
1681					error = st_space(st, 0, SP_FILEMARKS,
1682						flags);
1683					if (error)
1684						return error;
1685				}
1686				if (st->flags & ST_BLANK_READ) {
1687					st->flags &= ~ST_BLANK_READ;
1688					return EIO;
1689				}
1690				st->flags &= ~ST_EIO_PENDING;
1691			}
1692		}
1693		break;
1694	case SP_FILEMARKS:
1695		if (st->flags & ST_EIO_PENDING) {
1696			if (number > 0) {
1697				/* pretend we just discovered the error */
1698				st->flags &= ~ST_EIO_PENDING;
1699				return EIO;
1700			} else if (number < 0) {
1701				/* back away from the error */
1702				st->flags &= ~ST_EIO_PENDING;
1703			}
1704		}
1705		if (st->flags & ST_AT_FILEMARK) {
1706			st->flags &= ~ST_AT_FILEMARK;
1707			number--;
1708		}
1709		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1710			/* back away from unwritten tape */
1711			st->flags &= ~ST_BLANK_READ;
1712			number++;	/* XXX dubious */
1713		}
1714		break;
1715	case SP_EOM:
1716		if (st->flags & ST_EIO_PENDING) {
1717			/* pretend we just discovered the error */
1718			st->flags &= ~ST_EIO_PENDING;
1719			return EIO;
1720		}
1721		if (st->flags & ST_AT_FILEMARK)
1722			st->flags &= ~ST_AT_FILEMARK;
1723		break;
1724	}
1725	if (number == 0)
1726		return 0;
1727
1728	xs = scsi_xs_get(st->sc_link, flags);
1729	if (xs == NULL) {
1730		st->media_fileno = -1;
1731		st->media_blkno = -1;
1732		return (ENOMEM);
1733	}
1734	xs->cmd->opcode = SPACE;
1735
1736	cmd = (struct scsi_space *)xs->cmd;
1737	cmd->byte2 = what;
1738	_lto3b(number, cmd->number);
1739	xs->cmdlen = sizeof(*cmd);
1740	xs->timeout = ST_SPC_TIME;
1741
1742	error = scsi_xs_sync(xs);
1743	scsi_xs_put(xs);
1744
1745	if (error != 0) {
1746		st->media_fileno = -1;
1747		st->media_blkno = -1;
1748	} else {
1749		switch (what) {
1750		case SP_BLKS:
1751			if (st->media_blkno != -1) {
1752				st->media_blkno += number;
1753				if (st->media_blkno < 0)
1754					st->media_blkno = -1;
1755			}
1756			break;
1757		case SP_FILEMARKS:
1758			if (st->media_fileno != -1) {
1759				st->media_fileno += number;
1760				st->media_blkno = 0;
1761			}
1762			break;
1763		default:
1764			st->media_fileno = -1;
1765			st->media_blkno = -1;
1766			break;
1767		}
1768	}
1769
1770	return (error);
1771}
1772
1773/*
1774 * write N filemarks
1775 */
1776int
1777st_write_filemarks(struct st_softc *st, int number, int flags)
1778{
1779	struct scsi_write_filemarks *cmd;
1780	struct scsi_xfer *xs;
1781	int error;
1782
1783	if (number < 0)
1784		return (EINVAL);
1785
1786	xs = scsi_xs_get(st->sc_link, flags);
1787	if (xs == NULL) {
1788		st->media_fileno = -1;
1789		st->media_blkno = -1;
1790		return (ENOMEM);
1791	}
1792	xs->cmd->opcode = WRITE_FILEMARKS;
1793	xs->cmdlen = sizeof(*cmd);
1794	xs->timeout = ST_IO_TIME * 4;
1795
1796	switch (number) {
1797	case 0:		/* really a command to sync the drive's buffers */
1798		break;
1799	case 1:
1800		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
1801			st->flags &= ~ST_WRITTEN;
1802		else
1803			st->flags |= ST_FM_WRITTEN;
1804		st->flags &= ~ST_PER_ACTION;
1805		break;
1806	default:
1807		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1808		break;
1809	}
1810
1811	cmd = (struct scsi_write_filemarks *)xs->cmd;
1812	_lto3b(number, cmd->number);
1813
1814	error = scsi_xs_sync(xs);
1815	scsi_xs_put(xs);
1816
1817	if (error != 0) {
1818		st->media_fileno = -1;
1819		st->media_blkno = -1;
1820	} else if (st->media_fileno != -1) {
1821		st->media_fileno += number;
1822		st->media_blkno = 0;
1823	}
1824
1825	return (error);
1826}
1827
1828/*
1829 * Make sure the right number of file marks is on tape if the
1830 * tape has been written.  If the position argument is true,
1831 * leave the tape positioned where it was originally.
1832 *
1833 * nmarks returns the number of marks to skip (or, if position
1834 * true, which were skipped) to get back original position.
1835 */
1836int
1837st_check_eod(struct st_softc *st, int position, int *nmarks, int flags)
1838{
1839	int error;
1840
1841	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1842	default:
1843		*nmarks = 0;
1844		return 0;
1845	case ST_WRITTEN:
1846	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1847		*nmarks = 1;
1848		break;
1849	case ST_WRITTEN | ST_2FM_AT_EOD:
1850		*nmarks = 2;
1851	}
1852	error = st_write_filemarks(st, *nmarks, flags);
1853	if (position && !error)
1854		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1855	return error;
1856}
1857
1858/*
1859 * load/unload/retension
1860 */
1861int
1862st_load(struct st_softc *st, u_int type, int flags)
1863{
1864	struct scsi_load *cmd;
1865	struct scsi_xfer *xs;
1866	int error, nmarks;
1867
1868	st->media_fileno = -1;
1869	st->media_blkno = -1;
1870
1871	if (type != LD_LOAD) {
1872		error = st_check_eod(st, FALSE, &nmarks, flags);
1873		if (error)
1874			return (error);
1875	}
1876
1877	if (st->quirks & ST_Q_IGNORE_LOADS) {
1878		if (type == LD_LOAD) {
1879			/*
1880			 * If we ignore loads, at least we should try a rewind.
1881			 */
1882			return (st_rewind(st, 0, flags));
1883		}
1884		return (0);
1885	}
1886
1887
1888	xs = scsi_xs_get(st->sc_link, flags);
1889	if (xs == NULL)
1890		return (ENOMEM);
1891	xs->cmd->opcode = LOAD;
1892	xs->cmdlen = sizeof(*cmd);
1893	xs->timeout = ST_SPC_TIME;
1894
1895	cmd = (struct scsi_load *)xs->cmd;
1896	cmd->how = type;
1897
1898	error = scsi_xs_sync(xs);
1899	scsi_xs_put(xs);
1900
1901	return (error);
1902}
1903
1904/*
1905 *  Rewind the device
1906 */
1907int
1908st_rewind(struct st_softc *st, u_int immediate, int flags)
1909{
1910	struct scsi_rewind *cmd;
1911	struct scsi_xfer *xs;
1912	int error, nmarks;
1913
1914	error = st_check_eod(st, FALSE, &nmarks, flags);
1915	if (error)
1916		return (error);
1917	st->flags &= ~ST_PER_ACTION;
1918
1919	xs = scsi_xs_get(st->sc_link, flags);
1920	if (xs == NULL)
1921		return (ENOMEM);
1922	xs->cmd->opcode = REWIND;
1923	xs->cmdlen = sizeof(*cmd);
1924	xs->timeout = immediate ? ST_CTL_TIME : ST_SPC_TIME;
1925
1926	cmd = (struct scsi_rewind *)xs->cmd;
1927	cmd->byte2 = immediate;
1928
1929	error = scsi_xs_sync(xs);
1930	scsi_xs_put(xs);
1931
1932	if (error == 0) {
1933		st->media_fileno = 0;
1934		st->media_blkno = 0;
1935	}
1936
1937	return (error);
1938}
1939
1940/*
1941 * Look at the returned sense and act on the error and detirmine
1942 * The unix error number to pass back... (0 = report no error)
1943 *                            (-1 = continue processing)
1944 */
1945int
1946st_interpret_sense(struct scsi_xfer *xs)
1947{
1948	struct scsi_sense_data *sense = &xs->sense;
1949	struct scsi_link *sc_link = xs->sc_link;
1950	struct st_softc *st = sc_link->device_softc;
1951	u_int8_t serr = sense->error_code & SSD_ERRCODE;
1952	u_int8_t skey = sense->flags & SSD_KEY;
1953	int32_t info;
1954
1955	if (((sc_link->flags & SDEV_OPEN) == 0) ||
1956	    (serr != SSD_ERRCODE_CURRENT && serr != SSD_ERRCODE_DEFERRED))
1957		return (scsi_interpret_sense(xs));
1958
1959	switch (skey) {
1960
1961	/*
1962	 * We do custom processing in st for the unit becoming ready case.
1963	 * in this case we do not allow xs->retries to be decremented
1964	 * only on the "Unit Becoming Ready" case. This is because tape
1965	 * drives report "Unit Becoming Ready" when loading media, etc.
1966	 * and can take a long time.  Rather than having a massive timeout
1967	 * for all operations (which would cause other problems) we allow
1968	 * operations to wait (but be interruptable with Ctrl-C) forever
1969	 * as long as the drive is reporting that it is becoming ready.
1970	 * all other cases are handled as per the default.
1971	 */
1972
1973	case SKEY_NOT_READY:
1974		if ((xs->flags & SCSI_IGNORE_NOT_READY) != 0)
1975			return (0);
1976		switch (ASC_ASCQ(sense)) {
1977		case SENSE_NOT_READY_BECOMING_READY:
1978			SC_DEBUG(sc_link, SDEV_DB1, ("not ready: busy (%#x)\n",
1979			    sense->add_sense_code_qual));
1980			/* don't count this as a retry */
1981			xs->retries++;
1982			return (scsi_delay(xs, 1));
1983		default:
1984			return (scsi_interpret_sense(xs));
1985	}
1986	case SKEY_NO_SENSE:
1987	case SKEY_RECOVERED_ERROR:
1988	case SKEY_MEDIUM_ERROR:
1989	case SKEY_VOLUME_OVERFLOW:
1990	case SKEY_BLANK_CHECK:
1991		break;
1992	default:
1993		return (scsi_interpret_sense(xs));
1994	}
1995
1996	/*
1997	 * Get the sense fields and work out what code
1998	 */
1999	if (sense->error_code & SSD_ERRCODE_VALID)
2000		info = _4btol(sense->info);
2001	else
2002		info = xs->datalen;	/* bad choice if fixed blocks */
2003	if (st->flags & ST_FIXEDBLOCKS) {
2004		xs->resid = info * st->blksize;
2005		if (sense->flags & SSD_EOM) {
2006			st->flags |= ST_EIO_PENDING;
2007		}
2008		if (sense->flags & SSD_FILEMARK) {
2009			st->flags |= ST_AT_FILEMARK;
2010			if (st->media_fileno != -1) {
2011				st->media_fileno++;
2012				st->media_blkno = 0;
2013			}
2014		}
2015		if (sense->flags & SSD_ILI) {
2016			st->flags |= ST_EIO_PENDING;
2017			if (sense->error_code & SSD_ERRCODE_VALID &&
2018			    (xs->flags & SCSI_SILENT) == 0)
2019				printf("%s: block wrong size, %d blocks residual\n",
2020				    st->sc_dev.dv_xname, info);
2021
2022			/*
2023			 * This quirk code helps the drive read
2024			 * the first tape block, regardless of
2025			 * format.  That is required for these
2026			 * drives to return proper MODE SENSE
2027			 * information.
2028			 */
2029			if ((st->quirks & ST_Q_SENSE_HELP) &&
2030			    !(sc_link->flags & SDEV_MEDIA_LOADED))
2031				st->blksize -= 512;
2032		}
2033		/*
2034		 * If no data was transferred, return immediately
2035		 */
2036		if (xs->resid >= xs->datalen) {
2037			if (st->flags & ST_EIO_PENDING)
2038				return EIO;
2039			if (st->flags & ST_AT_FILEMARK) {
2040				return 0;
2041			}
2042		}
2043	} else {		/* must be variable mode */
2044		xs->resid = xs->datalen;	/* to be sure */
2045		if (sense->flags & SSD_EOM)
2046			return EIO;
2047		if (sense->flags & SSD_FILEMARK) {
2048			if (st->media_fileno != -1) {
2049				st->media_fileno++;
2050				st->media_blkno = 0;
2051			}
2052			return 0;
2053		}
2054		if (sense->flags & SSD_ILI) {
2055			if (info < 0) {
2056				/*
2057				 * the record was bigger than the read
2058				 */
2059				if ((xs->flags & SCSI_SILENT) == 0)
2060					printf("%s: %d-byte record too big\n",
2061					    st->sc_dev.dv_xname,
2062					    xs->datalen - info);
2063				return (EIO);
2064			} else if (info > xs->datalen) {
2065				/*
2066				 * huh? the residual is bigger than the request
2067				 */
2068				if ((xs->flags & SCSI_SILENT) == 0)
2069					printf(
2070					    "%s: bad residual %d out of %d\n",
2071					    st->sc_dev.dv_xname, info,
2072					    xs->datalen);
2073				return (EIO);
2074			}
2075			xs->resid = info;
2076			return (0);
2077		}
2078	}
2079
2080	if (skey == SKEY_BLANK_CHECK) {
2081		/*
2082		 * This quirk code helps the drive read the first tape block,
2083		 * regardless of format.  That is required for these drives to
2084		 * return proper MODE SENSE information.
2085		 */
2086		if ((st->quirks & ST_Q_SENSE_HELP) &&
2087		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
2088			/* still starting */
2089			st->blksize -= 512;
2090		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
2091			st->flags |= ST_BLANK_READ;
2092			xs->resid = xs->datalen;
2093			return (0);
2094		}
2095	}
2096
2097	return (scsi_interpret_sense(xs));
2098}
2099
2100/*
2101 * The quirk here is that the drive returns some value to st_mode_sense
2102 * incorrectly until the tape has actually passed by the head.
2103 *
2104 * The method is to set the drive to large fixed-block state (user-specified
2105 * density and 1024-byte blocks), then read and rewind to get it to sense the
2106 * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
2107 * work, as a last resort, try variable- length blocks.  The result will be
2108 * the ability to do an accurate st_mode_sense.
2109 *
2110 * We know we can do a rewind because we just did a load, which implies rewind.
2111 * Rewind seems preferable to space backward if we have a virgin tape.
2112 *
2113 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
2114 * error processing, both part of st_interpret_sense.
2115 */
2116int
2117st_touch_tape(struct st_softc *st)
2118{
2119	char *buf;
2120	int readsize;
2121	int error;
2122
2123	buf = malloc(1024, M_TEMP, M_NOWAIT);
2124	if (!buf)
2125		return ENOMEM;
2126
2127	if ((error = st_mode_sense(st, 0)) != 0)
2128		goto bad;
2129	st->blksize = 1024;
2130	do {
2131		switch (st->blksize) {
2132		case 512:
2133		case 1024:
2134			readsize = st->blksize;
2135			st->flags |= ST_FIXEDBLOCKS;
2136			break;
2137		default:
2138			readsize = 1;
2139			st->flags &= ~ST_FIXEDBLOCKS;
2140		}
2141		if ((error = st_mode_select(st, 0)) != 0)
2142			goto bad;
2143		st_read(st, buf, readsize, SCSI_SILENT);	/* XXX */
2144		if ((error = st_rewind(st, 0, 0)) != 0) {
2145bad:			free(buf, M_TEMP);
2146			return error;
2147		}
2148	} while (readsize != 1 && readsize > st->blksize);
2149
2150	free(buf, M_TEMP);
2151	return 0;
2152}
2153
2154int
2155stdump(dev_t dev, daddr64_t blkno, caddr_t va, size_t size)
2156{
2157
2158	/* Not implemented. */
2159	return ENXIO;
2160}
2161