1 /*	$OpenBSD: st.c,v 1.48 2005/06/24 20:48:25 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  */
97 struct modes {
98 	u_int quirks;			/* same definitions as in quirkdata */
99 	int blksize;
100 	u_int8_t density;
101 };
102 
103 struct 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 
113 struct st_quirk_inquiry_pattern {
114 	struct scsi_inquiry_pattern pattern;
115 	struct quirkdata quirkdata;
116 };
117 
118 const 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 
257 struct 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 
291 int	stmatch(struct device *, void *, void *);
292 void	stattach(struct device *, struct device *, void *);
293 void	st_identify_drive(struct st_softc *, struct scsi_inquiry_data *);
294 void	st_loadquirks(struct st_softc *);
295 int	st_mount_tape(dev_t, int);
296 void	st_unmount(struct st_softc *, boolean, boolean);
297 int	st_decide_mode(struct st_softc *, boolean);
298 void	ststart(void *);
299 int	st_read(struct st_softc *, char *, int, int);
300 int	st_read_block_limits(struct st_softc *, int);
301 int	st_mode_sense(struct st_softc *, int);
302 int	st_mode_select(struct st_softc *, int);
303 int	st_space(struct st_softc *, daddr_t, u_int, int);
304 int	st_write_filemarks(struct st_softc *, daddr_t, int);
305 int	st_check_eod(struct st_softc *, boolean, int *, int);
306 int	st_load(struct st_softc *, u_int, int);
307 int	st_rewind(struct st_softc *, u_int, int);
308 int	st_interpret_sense(struct scsi_xfer *);
309 int	st_touch_tape(struct st_softc *);
310 int	st_erase(struct st_softc *, daddr_t, int);
311 
312 struct cfattach st_ca = {
313 	sizeof(struct st_softc), stmatch, stattach
314 };
315 
316 struct cfdriver st_cd = {
317 	NULL, "st", DV_TAPE
318 };
319 
320 struct 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 
350 const struct scsi_inquiry_pattern st_patterns[] = {
351 	{T_SEQUENTIAL, T_REMOV,
352 	 "",         "",                 ""},
353 };
354 
355 int
stmatch(parent,match,aux)356 stmatch(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  */
373 void
stattach(parent,self,aux)374 stattach(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  */
439 void
st_identify_drive(st,inqbuf)440 st_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  */
464 void
st_loadquirks(st)465 st_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  */
495 int
stopen(dev,flags,mode,p)496 stopen(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 
577 bad:
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  */
587 int
stclose(dev,flags,mode,p)588 stclose(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  */
625 int
st_mount_tape(dev,flags)626 st_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  */
730 void
st_unmount(st,eject,rewind)731 st_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  */
757 int
st_decide_mode(st,first_read)758 st_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 
841 done:
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  */
872 void
ststrategy(bp)873 ststrategy(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;
930 bad:
931 	bp->b_flags |= B_ERROR;
932 done:
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  */
956 void
ststart(v)957 ststart(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 
1085 int
stread(dev,uio,iomode)1086 stread(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 
1097 int
stwrite(dev,uio,iomode)1098 stwrite(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  */
1113 int
stioctl(dev,cmd,arg,flag,p)1114 stioctl(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 /*-----------------------------*/
1299 try_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  */
1340 int
st_read(st,buf,size,flags)1341 st_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  */
1370 int
st_read_block_limits(st,flags)1371 st_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  */
1419 int
st_mode_sense(st,flags)1420 st_mode_sense(st, flags)
1421 	struct st_softc *st;
1422 	int flags;
1423 {
1424 	struct 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 	/*
1433 	 * Ask for page 0 (vendor specific) mode sense data.
1434 	 */
1435 	error = scsi_do_mode_sense(sc_link, 0, &data, (void **)&page0,
1436 	    &density, &block_count, &block_size, 1, flags | SCSI_SILENT, &big);
1437 	if (error != 0)
1438 		return (error);
1439 
1440 	/* It is valid for no page0 to be available. */
1441 
1442 	if (big)
1443 		dev_spec = data.headers.hdr_big.dev_spec;
1444 	else
1445 		dev_spec = data.headers.hdr.dev_spec;
1446 
1447 	if (dev_spec & SMH_DSP_WRITE_PROT)
1448 		st->flags |= ST_READONLY;
1449 	else
1450 		st->flags &= ~ST_READONLY;
1451 
1452 	st->numblks = block_count;
1453 	st->media_blksize = block_size;
1454 	st->media_density = density;
1455 
1456 	SC_DEBUG(sc_link, SDEV_DB3,
1457 	    ("density code 0x%x, %d-byte blocks, write-%s, ",
1458 	    st->media_density, st->media_blksize,
1459 	    st->flags & ST_READONLY ? "protected" : "enabled"));
1460 	SC_DEBUGN(sc_link, SDEV_DB3,
1461 	    ("%sbuffered\n", dev_spec & SMH_DSP_BUFF_MODE ? "" : "un"));
1462 
1463 	sc_link->flags |= SDEV_MEDIA_LOADED;
1464 
1465 	return 0;
1466 }
1467 
1468 /*
1469  * Send a filled out parameter structure to the drive to
1470  * set it into the desire modes etc.
1471  */
1472 int
st_mode_select(st,flags)1473 st_mode_select(st, flags)
1474 	struct st_softc *st;
1475 	int flags;
1476 {
1477 	struct scsi_mode_sense_buf inbuf, outbuf;
1478 	struct scsi_blk_desc general;
1479 	struct scsi_link *sc_link = st->sc_link;
1480 	u_int8_t *page0 = NULL;
1481 	int error, big, page0_size;
1482 
1483 	/*
1484 	 * This quirk deals with drives that have only one valid mode and think
1485 	 * this gives them license to reject all mode selects, even if the
1486 	 * selected mode is the one that is supported.
1487 	 */
1488 	if (st->quirks & ST_Q_UNIMODAL) {
1489 		SC_DEBUG(sc_link, SDEV_DB3,
1490 		    ("not setting density 0x%x blksize 0x%x\n",
1491 		    st->density, st->blksize));
1492 		return 0;
1493 	}
1494 
1495 	if (sc_link->flags & SDEV_ATAPI)
1496 		return 0;
1497 
1498 	bzero(&outbuf, sizeof(outbuf));
1499 	bzero(&general, sizeof(general));
1500 
1501 	general.density = st->density;
1502 	if (st->flags & ST_FIXEDBLOCKS)
1503 		_lto3b(st->blksize, general.blklen);
1504 
1505 	/*
1506 	 * Ask for page 0 (vendor specific) mode sense data.
1507 	 */
1508 	error = scsi_do_mode_sense(sc_link, 0, &inbuf, (void **)&page0, NULL,
1509 	    NULL, NULL, 1, flags | SCSI_SILENT, &big);
1510 	if (error != 0)
1511 		return (error);
1512 
1513 	if (page0 == NULL) {
1514 		page0_size = 0;
1515 	} else if (big == 0) {
1516 		page0_size = inbuf.headers.hdr.data_length +
1517 		    sizeof(inbuf.headers.hdr.data_length) -
1518 		    sizeof(inbuf.headers.hdr) - inbuf.headers.hdr.blk_desc_len;
1519 		memcpy(&outbuf.headers.buf[sizeof(outbuf.headers.hdr)+
1520 		    sizeof(general)], page0, page0_size);
1521 	} else {
1522 		page0_size = _2btol(inbuf.headers.hdr_big.data_length) +
1523 		    sizeof(inbuf.headers.hdr_big.data_length) -
1524 		    sizeof(inbuf.headers.hdr_big) -
1525 		    _2btol(inbuf.headers.hdr_big.blk_desc_len);
1526 		memcpy(&outbuf.headers.buf[sizeof(outbuf.headers.hdr_big) +
1527 		    sizeof(general)], page0, page0_size);
1528 	}
1529 
1530 	/*
1531 	 * Set up for a mode select.
1532 	 */
1533 	if (big == 0) {
1534 		outbuf.headers.hdr.data_length = sizeof(outbuf.headers.hdr) +
1535 		    sizeof(general) + page0_size -
1536 		    sizeof(outbuf.headers.hdr.data_length);
1537 		if ((st->flags & ST_DONTBUFFER) == 0)
1538 			outbuf.headers.hdr.dev_spec = SMH_DSP_BUFF_MODE_ON;
1539 		outbuf.headers.hdr.blk_desc_len = sizeof(general);
1540 		memcpy(&outbuf.headers.buf[sizeof(outbuf.headers.hdr)],
1541 		    &general, sizeof(general));
1542 		return (scsi_mode_select(st->sc_link, 0, &outbuf.headers.hdr,
1543 		    flags, ST_CTL_TIME));
1544 	}
1545 
1546 	/* MODE SENSE (10) header was returned, so use MODE SELECT (10). */
1547 	_lto2b((sizeof(outbuf.headers.hdr_big) + sizeof(general) + page0_size -
1548 	    sizeof(outbuf.headers.hdr_big.data_length)),
1549 	    outbuf.headers.hdr_big.data_length);
1550 	if ((st->flags & ST_DONTBUFFER) == 0)
1551 		outbuf.headers.hdr_big.dev_spec = SMH_DSP_BUFF_MODE_ON;
1552 	_lto2b(sizeof(general), outbuf.headers.hdr_big.blk_desc_len);
1553 	memcpy(&outbuf.headers.buf[sizeof(outbuf.headers.hdr_big)], &general,
1554 	    sizeof(general));
1555 	return (scsi_mode_select_big(st->sc_link, 0, &outbuf.headers.hdr_big,
1556 	    flags, ST_CTL_TIME));
1557 }
1558 
1559 /*
1560  * issue an erase command
1561  */
1562 int
st_erase(st,full,flags)1563 st_erase(st, full, flags)
1564 	struct st_softc *st;
1565 	daddr_t full;
1566 	int flags;
1567 {
1568 	struct scsi_erase cmd;
1569 	int tmo;
1570 
1571 	/*
1572 	 * Full erase means set LONG bit in erase command, which asks
1573 	 * the drive to erase the entire unit.  Without this bit, we're
1574 	 * asking the drive to write an erase gap.
1575 	 */
1576 	bzero(&cmd, sizeof(cmd));
1577 	cmd.opcode = ERASE;
1578 	if (full) {
1579 		cmd.byte2 = SE_IMMED|SE_LONG;
1580 		tmo = ST_SPC_TIME;
1581 	} else {
1582 		cmd.byte2 = SE_IMMED;
1583 		tmo = ST_IO_TIME;
1584 	}
1585 
1586 	/*
1587 	 * XXX We always do this asynchronously, for now.  How long should
1588 	 * we wait if we want to (eventually) to it synchronously?
1589 	 */
1590 	return (scsi_scsi_cmd(st->sc_link, (struct scsi_generic *)&cmd,
1591 	    sizeof(cmd), 0, 0, ST_RETRIES, tmo, NULL, flags));
1592 }
1593 
1594 /*
1595  * skip N blocks/filemarks/seq filemarks/eom
1596  */
1597 int
st_space(st,number,what,flags)1598 st_space(st, number, what, flags)
1599 	struct st_softc *st;
1600 	u_int what;
1601 	int flags;
1602 	daddr_t number;
1603 {
1604 	struct scsi_space cmd;
1605 	int error;
1606 
1607 	switch (what) {
1608 	case SP_BLKS:
1609 		if (st->flags & ST_PER_ACTION) {
1610 			if (number > 0) {
1611 				st->flags &= ~ST_PER_ACTION;
1612 				return EIO;
1613 			} else if (number < 0) {
1614 				if (st->flags & ST_AT_FILEMARK) {
1615 					/*
1616 					 * Handling of ST_AT_FILEMARK
1617 					 * in st_space will fill in the
1618 					 * right file mark count.
1619 					 */
1620 					error = st_space(st, 0, SP_FILEMARKS,
1621 						flags);
1622 					if (error)
1623 						return error;
1624 				}
1625 				if (st->flags & ST_BLANK_READ) {
1626 					st->flags &= ~ST_BLANK_READ;
1627 					return EIO;
1628 				}
1629 				st->flags &= ~ST_EIO_PENDING;
1630 			}
1631 		}
1632 		break;
1633 	case SP_FILEMARKS:
1634 		if (st->flags & ST_EIO_PENDING) {
1635 			if (number > 0) {
1636 				/* pretend we just discovered the error */
1637 				st->flags &= ~ST_EIO_PENDING;
1638 				return EIO;
1639 			} else if (number < 0) {
1640 				/* back away from the error */
1641 				st->flags &= ~ST_EIO_PENDING;
1642 			}
1643 		}
1644 		if (st->flags & ST_AT_FILEMARK) {
1645 			st->flags &= ~ST_AT_FILEMARK;
1646 			number--;
1647 		}
1648 		if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1649 			/* back away from unwritten tape */
1650 			st->flags &= ~ST_BLANK_READ;
1651 			number++;	/* XXX dubious */
1652 		}
1653 		break;
1654 	case SP_EOM:
1655 		if (st->flags & ST_EIO_PENDING) {
1656 			/* pretend we just discovered the error */
1657 			st->flags &= ~ST_EIO_PENDING;
1658 			return EIO;
1659 		}
1660 		if (st->flags & ST_AT_FILEMARK)
1661 			st->flags &= ~ST_AT_FILEMARK;
1662 		break;
1663 	}
1664 	if (number == 0)
1665 		return 0;
1666 
1667 	bzero(&cmd, sizeof(cmd));
1668 	cmd.opcode = SPACE;
1669 	cmd.byte2 = what;
1670 	_lto3b(number, cmd.number);
1671 
1672 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1673 	    sizeof(cmd), 0, 0, 0, ST_SPC_TIME, NULL, flags);
1674 }
1675 
1676 /*
1677  * write N filemarks
1678  */
1679 int
st_write_filemarks(st,number,flags)1680 st_write_filemarks(st, number, flags)
1681 	struct st_softc *st;
1682 	int flags;
1683 	daddr_t number;
1684 {
1685 	struct scsi_write_filemarks cmd;
1686 
1687 	/*
1688 	 * It's hard to write a negative number of file marks.
1689 	 * Don't try.
1690 	 */
1691 	if (number < 0)
1692 		return EINVAL;
1693 	switch (number) {
1694 	case 0:		/* really a command to sync the drive's buffers */
1695 		break;
1696 	case 1:
1697 		if (st->flags & ST_FM_WRITTEN)	/* already have one down */
1698 			st->flags &= ~ST_WRITTEN;
1699 		else
1700 			st->flags |= ST_FM_WRITTEN;
1701 		st->flags &= ~ST_PER_ACTION;
1702 		break;
1703 	default:
1704 		st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1705 	}
1706 
1707 	bzero(&cmd, sizeof(cmd));
1708 	cmd.opcode = WRITE_FILEMARKS;
1709 	_lto3b(number, cmd.number);
1710 
1711 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1712 	    sizeof(cmd), 0, 0, 0, ST_IO_TIME * 4, NULL, flags);
1713 }
1714 
1715 /*
1716  * Make sure the right number of file marks is on tape if the
1717  * tape has been written.  If the position argument is true,
1718  * leave the tape positioned where it was originally.
1719  *
1720  * nmarks returns the number of marks to skip (or, if position
1721  * true, which were skipped) to get back original position.
1722  */
1723 int
st_check_eod(st,position,nmarks,flags)1724 st_check_eod(st, position, nmarks, flags)
1725 	struct st_softc *st;
1726 	boolean position;
1727 	int *nmarks;
1728 	int flags;
1729 {
1730 	int error;
1731 
1732 	switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1733 	default:
1734 		*nmarks = 0;
1735 		return 0;
1736 	case ST_WRITTEN:
1737 	case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1738 		*nmarks = 1;
1739 		break;
1740 	case ST_WRITTEN | ST_2FM_AT_EOD:
1741 		*nmarks = 2;
1742 	}
1743 	error = st_write_filemarks(st, *nmarks, flags);
1744 	if (position && !error)
1745 		error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1746 	return error;
1747 }
1748 
1749 /*
1750  * load/unload/retension
1751  */
1752 int
st_load(st,type,flags)1753 st_load(st, type, flags)
1754 	struct st_softc *st;
1755 	u_int type;
1756 	int flags;
1757 {
1758 	struct scsi_load cmd;
1759 
1760 	if (type != LD_LOAD) {
1761 		int error;
1762 		int nmarks;
1763 
1764 		error = st_check_eod(st, FALSE, &nmarks, flags);
1765 		if (error)
1766 			return error;
1767 	}
1768 	if (st->quirks & ST_Q_IGNORE_LOADS) {
1769 		if (type == LD_LOAD) {
1770 			/*
1771 			 * If we ignore loads, at least we should try a rewind.
1772 			 */
1773 			return st_rewind(st, 0, flags);
1774 		}
1775 		return (0);
1776 	}
1777 
1778 
1779 	bzero(&cmd, sizeof(cmd));
1780 	cmd.opcode = LOAD;
1781 	cmd.how = type;
1782 
1783 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1784 	    sizeof(cmd), 0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags);
1785 }
1786 
1787 /*
1788  *  Rewind the device
1789  */
1790 int
st_rewind(st,immediate,flags)1791 st_rewind(st, immediate, flags)
1792 	struct st_softc *st;
1793 	u_int immediate;
1794 	int flags;
1795 {
1796 	struct scsi_rewind cmd;
1797 	int error;
1798 	int nmarks;
1799 
1800 	error = st_check_eod(st, FALSE, &nmarks, flags);
1801 	if (error)
1802 		return error;
1803 	st->flags &= ~ST_PER_ACTION;
1804 
1805 	bzero(&cmd, sizeof(cmd));
1806 	cmd.opcode = REWIND;
1807 	cmd.byte2 = immediate;
1808 
1809 	return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &cmd,
1810 	    sizeof(cmd), 0, 0, ST_RETRIES,
1811 	    immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags);
1812 }
1813 
1814 /*
1815  * Look at the returned sense and act on the error and detirmine
1816  * The unix error number to pass back... (0 = report no error)
1817  *                            (-1 = continue processing)
1818  */
1819 int
st_interpret_sense(xs)1820 st_interpret_sense(xs)
1821 	struct scsi_xfer *xs;
1822 {
1823 	struct scsi_sense_data *sense = &xs->sense;
1824 	struct scsi_link *sc_link = xs->sc_link;
1825 	struct st_softc *st = sc_link->device_softc;
1826 	struct buf *bp = xs->bp;
1827 	u_int8_t serr = sense->error_code & SSD_ERRCODE;
1828 	u_int8_t skey = sense->flags & SSD_KEY;
1829 	int32_t info;
1830 
1831 	if (((sc_link->flags & SDEV_OPEN) == 0) ||
1832 	    (serr != 0x70 && serr != 0x71))
1833 		return (EJUSTRETURN); /* let the generic code handle it */
1834 
1835 	switch (skey) {
1836 	case SKEY_NO_SENSE:
1837 	case SKEY_RECOVERED_ERROR:
1838 	case SKEY_MEDIUM_ERROR:
1839 	case SKEY_VOLUME_OVERFLOW:
1840 	case SKEY_BLANK_CHECK:
1841 		break;
1842 	default:
1843 		return (EJUSTRETURN);
1844 	}
1845 
1846 	/*
1847 	 * Get the sense fields and work out what code
1848 	 */
1849 	if (sense->error_code & SSD_ERRCODE_VALID)
1850 		info = _4btol(sense->info);
1851 	else
1852 		info = xs->datalen;	/* bad choice if fixed blocks */
1853 	if (st->flags & ST_FIXEDBLOCKS) {
1854 		xs->resid = info * st->blksize;
1855 		if (sense->flags & SSD_EOM) {
1856 			st->flags |= ST_EIO_PENDING;
1857 			if (bp)
1858 				bp->b_resid = xs->resid;
1859 		}
1860 		if (sense->flags & SSD_FILEMARK) {
1861 			st->flags |= ST_AT_FILEMARK;
1862 			if (bp)
1863 				bp->b_resid = xs->resid;
1864 		}
1865 		if (sense->flags & SSD_ILI) {
1866 			st->flags |= ST_EIO_PENDING;
1867 			if (bp)
1868 				bp->b_resid = xs->resid;
1869 			if (sense->error_code & SSD_ERRCODE_VALID &&
1870 			    (xs->flags & SCSI_SILENT) == 0)
1871 				printf("%s: block wrong size, %d blocks residual\n",
1872 				    st->sc_dev.dv_xname, info);
1873 
1874 			/*
1875 			 * This quirk code helps the drive read
1876 			 * the first tape block, regardless of
1877 			 * format.  That is required for these
1878 			 * drives to return proper MODE SENSE
1879 			 * information.
1880 			 */
1881 			if ((st->quirks & ST_Q_SENSE_HELP) &&
1882 			    !(sc_link->flags & SDEV_MEDIA_LOADED))
1883 				st->blksize -= 512;
1884 		}
1885 		/*
1886 		 * If no data was tranfered, do it immediatly
1887 		 */
1888 		if (xs->resid >= xs->datalen) {
1889 			if (st->flags & ST_EIO_PENDING)
1890 				return EIO;
1891 			if (st->flags & ST_AT_FILEMARK) {
1892 				if (bp)
1893 					bp->b_resid = xs->resid;
1894 				return 0;
1895 			}
1896 		}
1897 	} else {		/* must be variable mode */
1898 		xs->resid = xs->datalen;	/* to be sure */
1899 		if (sense->flags & SSD_EOM)
1900 			return EIO;
1901 		if (sense->flags & SSD_FILEMARK) {
1902 			if (bp)
1903 				bp->b_resid = bp->b_bcount;
1904 			return 0;
1905 		}
1906 		if (sense->flags & SSD_ILI) {
1907 			if (info < 0) {
1908 				/*
1909 				 * the record was bigger than the read
1910 				 */
1911 				if ((xs->flags & SCSI_SILENT) == 0)
1912 					printf("%s: %d-byte record too big\n",
1913 					    st->sc_dev.dv_xname,
1914 					    xs->datalen - info);
1915 				return (EIO);
1916 			} else if (info > xs->datalen) {
1917 				/*
1918 				 * huh? the residual is bigger than the request
1919 				 */
1920 				if ((xs->flags & SCSI_SILENT) == 0) {
1921 					printf(
1922 					    "%s: bad residual %d out of %d\n",
1923 					    st->sc_dev.dv_xname, info,
1924 					    xs->datalen);
1925 					return (EIO);
1926 				}
1927 			}
1928 			xs->resid = info;
1929 			if (bp)
1930 				bp->b_resid = info;
1931 			return (0);
1932 		}
1933 	}
1934 
1935 	if (skey == SKEY_BLANK_CHECK) {
1936 		/*
1937 		 * This quirk code helps the drive read the first tape block,
1938 		 * regardless of format.  That is required for these drives to
1939 		 * return proper MODE SENSE information.
1940 		 */
1941 		if ((st->quirks & ST_Q_SENSE_HELP) &&
1942 		    !(sc_link->flags & SDEV_MEDIA_LOADED)) {
1943 			/* still starting */
1944 			st->blksize -= 512;
1945 		} else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
1946 			st->flags |= ST_BLANK_READ;
1947 			xs->resid = xs->datalen;
1948 			if (bp) {
1949 				bp->b_resid = xs->resid;
1950 				/* return an EOF */
1951 			}
1952 			return (0);
1953 		}
1954 	}
1955 
1956 	return (EJUSTRETURN);
1957 }
1958 
1959 /*
1960  * The quirk here is that the drive returns some value to st_mode_sense
1961  * incorrectly until the tape has actually passed by the head.
1962  *
1963  * The method is to set the drive to large fixed-block state (user-specified
1964  * density and 1024-byte blocks), then read and rewind to get it to sense the
1965  * tape.  If that doesn't work, try 512-byte fixed blocks.  If that doesn't
1966  * work, as a last resort, try variable- length blocks.  The result will be
1967  * the ability to do an accurate st_mode_sense.
1968  *
1969  * We know we can do a rewind because we just did a load, which implies rewind.
1970  * Rewind seems preferable to space backward if we have a virgin tape.
1971  *
1972  * The rest of the code for this quirk is in ILI processing and BLANK CHECK
1973  * error processing, both part of st_interpret_sense.
1974  */
1975 int
st_touch_tape(st)1976 st_touch_tape(st)
1977 	struct st_softc *st;
1978 {
1979 	char *buf;
1980 	int readsize;
1981 	int error;
1982 
1983 	buf = malloc(1024, M_TEMP, M_NOWAIT);
1984 	if (!buf)
1985 		return ENOMEM;
1986 
1987 	if ((error = st_mode_sense(st, 0)) != 0)
1988 		goto bad;
1989 	st->blksize = 1024;
1990 	do {
1991 		switch (st->blksize) {
1992 		case 512:
1993 		case 1024:
1994 			readsize = st->blksize;
1995 			st->flags |= ST_FIXEDBLOCKS;
1996 			break;
1997 		default:
1998 			readsize = 1;
1999 			st->flags &= ~ST_FIXEDBLOCKS;
2000 		}
2001 		if ((error = st_mode_select(st, 0)) != 0)
2002 			goto bad;
2003 		st_read(st, buf, readsize, SCSI_SILENT);	/* XXX */
2004 		if ((error = st_rewind(st, 0, 0)) != 0) {
2005 bad:			free(buf, M_TEMP);
2006 			return error;
2007 		}
2008 	} while (readsize != 1 && readsize > st->blksize);
2009 
2010 	free(buf, M_TEMP);
2011 	return 0;
2012 }
2013 
2014 int
stdump(dev,blkno,va,size)2015 stdump(dev, blkno, va, size)
2016 	dev_t dev;
2017 	daddr_t blkno;
2018 	caddr_t va;
2019 	size_t size;
2020 {
2021 
2022 	/* Not implemented. */
2023 	return ENXIO;
2024 }
2025