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