Patch from Julian. Commit message by me.
[unix-history] / sys / scsi / st.c
CommitLineData
15637ed4 1/*
a0becc21 2 * Written by Julian Elischer (julian@tfs.com)(now julian@DIALix.oz.au)
15637ed4
RG
3 * for TRW Financial Systems for use under the MACH(2.5) operating system.
4 *
5 * TRW Financial Systems, in accordance with their agreement with Carnegie
6 * Mellon University, makes this software available to CMU to distribute
7 * or use in any manner that they see fit as long as this message is kept with
8 * the software. For this reason TFS also grants any other persons or
9 * organisations permission to use or modify this software.
10 *
11 * TFS supplies this software to be publicly redistributed
12 * on the understanding that TFS is not responsible for the correct
13 * functioning of this software in any circumstances.
14 *
519fb2b7
RG
15 *
16 * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
17 * -------------------- ----- ----------------------
18 * CURRENT PATCH LEVEL: 1 00098
19 * -------------------- ----- ----------------------
20 *
21 * 16 Feb 93 Julian Elischer ADDED for SCSI system
22 * 1.15 is the last version to support MACH and OSF/1
23 */
46d39670 24/* $Revision: 1.14 $ */
519fb2b7
RG
25
26/*
15637ed4 27 * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
a0becc21 28 * major changes by Julian Elischer (julian@jules.dialix.oz.au) May 1993
98639498 29 *
46d39670 30 * $Id: st.c,v 1.14 1993/12/19 00:54:59 wollman Exp $
15637ed4
RG
31 */
32
15637ed4
RG
33/*
34 * To do:
35 * work out some better way of guessing what a good timeout is going
36 * to be depending on whether we expect to retension or not.
37 *
38 */
39
40#include <sys/types.h>
41#include <st.h>
42
43#include <sys/param.h>
44#include <sys/systm.h>
45
fca1f689 46#include <fcntl.h>
15637ed4
RG
47#include <sys/errno.h>
48#include <sys/ioctl.h>
d4284689 49#include <sys/malloc.h>
15637ed4
RG
50#include <sys/buf.h>
51#include <sys/proc.h>
52#include <sys/user.h>
53#include <sys/mtio.h>
54
15637ed4
RG
55#include <scsi/scsi_all.h>
56#include <scsi/scsi_tape.h>
57#include <scsi/scsiconf.h>
58
519fb2b7 59u_int32 ststrats, stqueues;
15637ed4 60
a0becc21
NW
61/* Defines for device specific stuff */
62#define PAGE_0_SENSE_DATA_SIZE 12
15637ed4 63#define PAGESIZ 4096
a0becc21 64#define DEF_FIXED_BSIZE 512
519fb2b7 65#define ST_RETRIES 4 /* only on non IO commands */
15637ed4
RG
66
67#define MODE(z) ( (minor(z) & 0x03) )
68#define DSTY(z) ( ((minor(z) >> 2) & 0x03) )
69#define UNIT(z) ( (minor(z) >> 4) )
519fb2b7 70#define CTLMODE 3
15637ed4 71
a0becc21 72#define SCSI_2_MAX_DENSITY_CODE 0x17 /* maximum density code specified
519fb2b7
RG
73 * in SCSI II spec. */
74/*
75 * Define various devices that we know mis-behave in some way,
76 * and note how they are bad, so we can correct for them
77 */
78struct modes {
79 u_int32 blksiz;
80 u_int32 quirks; /* same definitions as in rogues */
81 char density;
82 char spare[3];
a0becc21 83};
519fb2b7
RG
84
85struct rogues {
86 char *name;
87 char *manu;
88 char *model;
89 char *version;
90 u_int32 quirks; /* valid for all modes */
91 struct modes modes[4];
a0becc21
NW
92};
93
94/* define behaviour codes (quirks) */
95#define ST_Q_NEEDS_PAGE_0 0x00001
96#define ST_Q_FORCE_FIXED_MODE 0x00002
97#define ST_Q_FORCE_VAR_MODE 0x00004
519fb2b7 98#define ST_Q_SNS_HLP 0x00008 /* must do READ for good MODE SENSE */
0269d546 99#define ST_Q_IGNORE_LOADS 0x00010
519fb2b7 100#define ST_Q_BLKSIZ 0x00020 /* variable-block media_blksiz > 0 */
a0becc21 101
519fb2b7 102static struct rogues gallery[] = /* ends with an all-null entry */
a0becc21 103{
519fb2b7
RG
104 {"Such an old device ", "pre-scsi", " unknown model ", "????",
105 0,
106 {
107 {512, ST_Q_FORCE_FIXED_MODE, 0}, /* minor 0,1,2,3 */
108 {512, ST_Q_FORCE_FIXED_MODE, QIC_24}, /* minor 4,5,6,7 */
109 {0, ST_Q_FORCE_VAR_MODE, HALFINCH_1600}, /* minor 8,9,10,11 */
110 {0, ST_Q_FORCE_VAR_MODE, HALFINCH_6250} /* minor 12,13,14,15 */
111 }
112 },
113 {"Tandberg tdc3600", "TANDBERG", " TDC 3600", "????",
114 ST_Q_NEEDS_PAGE_0,
115 {
116 {0, 0, 0}, /* minor 0,1,2,3 */
117 {0, ST_Q_FORCE_VAR_MODE, QIC_525}, /* minor 4,5,6,7 */
118 {0, 0, QIC_150}, /* minor 8,9,10,11 */
119 {0, 0, QIC_120} /* minor 12,13,14,15 */
120 }
121 },
122 {"Rev 5 of the Archive 2525", "ARCHIVE ", "VIPER 2525 25462", "-005",
123 0,
124 {
125 {0, ST_Q_SNS_HLP, 0}, /* minor 0,1,2,3 */
126 {0, ST_Q_SNS_HLP, QIC_525}, /* minor 4,5,6,7 */
127 {0, 0, QIC_150}, /* minor 8,9,10,11 */
128 {0, 0, QIC_120} /* minor 12,13,14,15 */
129 }
130 },
131 {"Archive Viper 150", "ARCHIVE ", "VIPER 150", "????",
132 ST_Q_NEEDS_PAGE_0,
133 {
134 {0, 0, 0}, /* minor 0,1,2,3 */
135 {0, 0, QIC_150}, /* minor 4,5,6,7 */
136 {0, 0, QIC_120}, /* minor 8,9,10,11 */
137 {0, 0, QIC_24} /* minor 12,13,14,15 */
138 }
139 },
140 {"Wangtek 5525ES", "WANGTEK ", "5525ES SCSI REV7", "????",
141 0,
142 {
143 {0, 0, 0}, /* minor 0,1,2,3 */
144 {0, ST_Q_BLKSIZ, QIC_525}, /* minor 4,5,6,7 */
145 {0, 0, QIC_150}, /* minor 8,9,10,11 */
146 {0, 0, QIC_120} /* minor 12,13,14,15 */
147 }
148 },
149 {"WangDAT model 1300", "WangDAT ", "Model 1300", "????",
150 0,
151 {
152 {0, 0, 0}, /* minor 0,1,2,3 */
153 {512, ST_Q_FORCE_FIXED_MODE, 0x13}, /* minor 4,5,6,7 */
154 {1024, ST_Q_FORCE_FIXED_MODE, 0x13}, /* minor 8,9,10,11 */
155 {0, ST_Q_FORCE_VAR_MODE, 0x13} /* minor 12,13,14,15 */
156 }
157 },
158 {(char *) 0}
a0becc21 159};
15637ed4 160
519fb2b7
RG
161errval st_space __P((u_int32 unit, int32 number, u_int32 what, u_int32 flags));
162errval st_rewind __P((u_int32 unit, boolean immed, u_int32 flags));
163errval st_mode_sense __P((u_int32 unit, u_int32 flags));
164errval st_decide_mode __P((u_int32 unit, boolean first_read));
165errval st_rd_blk_lim __P((u_int32 unit, u_int32 flags));
166errval st_touch_tape __P((u_int32 unit));
167errval st_write_filemarks __P((u_int32 unit, int32 number, u_int32 flags));
168errval st_load __P((u_int32 unit, u_int32 type, u_int32 flags));
169errval st_mode_select __P((u_int32 unit, u_int32 flags));
170void ststrategy();
171void stminphys();
172int32 st_chkeod();
173errval stattach();
174void ststart();
175void st_unmount();
176errval st_mount_tape();
177void st_loadquirks();
178void st_identify_drive();
179errval st_interpret_sense();
15637ed4 180
15637ed4 181#define ESUCCESS 0
519fb2b7
RG
182#define NOEJECT 0
183#define EJECT 1
15637ed4 184
519fb2b7 185struct scsi_device st_switch =
15637ed4 186{
519fb2b7
RG
187 st_interpret_sense, /* check errors with us first */
188 ststart, /* we have a queue, and this is how we service it */
189 NULL,
190 NULL, /* use the default 'done' routine */
191 "st",
192 0,
193 { 0, 0 }
194};
195
196struct st_data {
a0becc21 197/*--------------------present operating parameters, flags etc.----------------*/
519fb2b7
RG
198 u_int32 flags; /* see below */
199 u_int32 blksiz; /* blksiz we are using */
200 u_int32 density; /* present density */
201 u_int32 quirks; /* quirks for the open mode */
202 u_int32 last_dsty; /* last density openned */
a0becc21 203/*--------------------device/scsi parameters----------------------------------*/
519fb2b7 204 struct scsi_link *sc_link; /* our link to the adpter etc. */
a0becc21 205/*--------------------parameters reported by the device ----------------------*/
519fb2b7
RG
206 u_int32 blkmin; /* min blk size */
207 u_int32 blkmax; /* max blk size */
208 struct rogues *rogues; /* if we have a rogue entry */
a0becc21 209/*--------------------parameters reported by the device for this media--------*/
519fb2b7
RG
210 u_int32 numblks; /* nominal blocks capacity */
211 u_int32 media_blksiz; /* 0 if not ST_FIXEDBLOCKS */
212 u_int32 media_density; /* this is what it said when asked */
a0becc21 213/*--------------------quirks for the whole drive------------------------------*/
519fb2b7 214 u_int32 drive_quirks; /* quirks of this drive */
a0becc21 215/*--------------------How we should set up when openning each minor device----*/
519fb2b7
RG
216 struct modes modes[4]; /* plus more for each mode */
217 u_int8 modeflags[4]; /* flags for the modes */
218#define DENSITY_SET_BY_USER 0x01
219#define DENSITY_SET_BY_QUIRK 0x02
220#define BLKSIZE_SET_BY_USER 0x04
221#define BLKSIZE_SET_BY_QUIRK 0x08
a0becc21 222/*--------------------storage for sense data returned by the drive------------*/
519fb2b7
RG
223 unsigned char sense_data[12]; /*
224 * additional sense data needed
225 * for mode sense/select.
226 */
227 struct buf *buf_queue; /* the queue of pending IO operations */
228 struct scsi_xfer scsi_xfer; /* scsi xfer struct for this drive */
229 u_int32 xfer_block_wait; /* is a process waiting? */
230} *st_data[NST];
231
a0becc21
NW
232#define ST_INITIALIZED 0x01
233#define ST_INFO_VALID 0x02
234#define ST_OPEN 0x04
519fb2b7
RG
235#define ST_BLOCK_SET 0x08 /* block size, mode set by ioctl */
236#define ST_WRITTEN 0x10 /* data have been written, EOD needed */
a0becc21
NW
237#define ST_FIXEDBLOCKS 0x20
238#define ST_AT_FILEMARK 0x40
519fb2b7
RG
239#define ST_EIO_PENDING 0x80 /* we couldn't report it then (had data) */
240#define ST_NEW_MOUNT 0x100 /* still need to decide mode */
241#define ST_READONLY 0x200 /* st_mode_sense says write protected */
242#define ST_FM_WRITTEN 0x400 /*
243 * EOF file mark written -- used with
244 * ~ST_WRITTEN to indicate that multiple file
245 * marks have been written
246 */
247#define ST_BLANK_READ 0x800 /* BLANK CHECK encountered already */
248#define ST_2FM_AT_EOD 0x1000 /* write 2 file marks at EOD */
249#define ST_MOUNTED 0x2000 /* Device is presently mounted */
fca1f689
RG
250
251#define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
519fb2b7
RG
252#define ST_PER_MOUNT (ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
253 ST_FIXEDBLOCKS | ST_READONLY | \
fca1f689 254 ST_FM_WRITTEN | ST_2FM_AT_EOD | ST_PER_ACTION)
15637ed4 255
519fb2b7 256static u_int32 next_st_unit = 0;
15637ed4 257
519fb2b7
RG
258/*
259 * The routine called by the low level scsi routine when it discovers
260 * A device suitable for this driver
261 */
262
263errval
264stattach(sc_link)
265 struct scsi_link *sc_link;
15637ed4 266{
519fb2b7 267 u_int32 unit;
15637ed4
RG
268 struct st_data *st;
269
519fb2b7
RG
270 SC_DEBUG(sc_link, SDEV_DB2, ("stattach: "));
271 /*
272 * Check we have the resources for another drive
273 */
15637ed4 274 unit = next_st_unit++;
519fb2b7
RG
275
276 if (unit >= NST) {
869c4419 277 printf("Too many scsi tapes..(%d > %d) reconfigure kernel\n",
519fb2b7
RG
278 (unit + 1), NST);
279 return 0;
15637ed4 280 }
519fb2b7
RG
281 if (st_data[unit]) {
282 printf("st%d: Already has storage!\n", unit);
283 return 0;
d4284689 284 }
519fb2b7
RG
285 sc_link->device = &st_switch;
286 sc_link->dev_unit = unit;
287 st = st_data[unit] = malloc(sizeof(struct st_data), M_DEVBUF, M_NOWAIT);
288 if (!st) {
289 printf("st%d: malloc failed in st.c\n", unit);
290 return 0;
d4284689 291 }
519fb2b7
RG
292 bzero(st, sizeof(struct st_data));
293
294 /*
295 * Store information needed to contact our base driver
296 */
297 st->sc_link = sc_link;
298
299 /*
300 * Check if the drive is a known criminal and take
301 * Any steps needed to bring it into line
302 */
a0becc21 303 st_identify_drive(unit);
15637ed4 304
519fb2b7
RG
305 /*
306 * Use the subdriver to request information regarding
307 * the drive. We cannot use interrupts yet, so the
308 * request must specify this.
309 */
310 if (st_mode_sense(unit, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT)) {
d4284689 311 printf("st%d: drive offline\n", unit);
519fb2b7
RG
312 } else {
313 printf("st%d: density code 0x%x, ", unit, st->media_density);
314 if (!scsi_test_unit_ready(sc_link, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT)) {
315 if (st->media_blksiz) {
d4284689 316 printf("%d-byte", st->media_blksiz);
519fb2b7 317 } else {
d4284689
RG
318 printf("variable");
319 }
320 printf(" blocks, write-%s\n",
519fb2b7
RG
321 (st->flags & ST_READONLY) ? "protected" : "enabled");
322 } else {
323 printf(" drive empty\n");
a0becc21 324 }
15637ed4 325 }
519fb2b7
RG
326 /*
327 * Set up the buf queue for this device
328 */
329 st->buf_queue = 0;
a0becc21 330 st->flags |= ST_INITIALIZED;
519fb2b7 331 return 0;
15637ed4
RG
332}
333
519fb2b7
RG
334/*
335 * Use the inquiry routine in 'scsi_base' to get drive info so we can
336 * Further tailor our behaviour.
337 */
338void
a0becc21 339st_identify_drive(unit)
519fb2b7 340 u_int32 unit;
a0becc21 341{
519fb2b7
RG
342 struct st_data *st = st_data[unit];
343 struct scsi_inquiry_data inqbuf;
344 struct rogues *finger;
345 char manu[32];
346 char model[32];
347 char model2[32];
348 char version[32];
349 u_int32 model_len;
350
351 /*
352 * Get the device type information
353 */
354 if (scsi_inquire(st->sc_link, &inqbuf,
355 SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT) != 0) {
98639498 356 printf("st%d: couldn't get device type, using default\n", unit);
a0becc21
NW
357 return;
358 }
519fb2b7
RG
359 if ((inqbuf.version & SID_ANSII) == 0) {
360 /*
361 * If not advanced enough, use default values
362 */
363 strncpy(manu, "pre-scsi", 8);
364 manu[8] = 0;
365 strncpy(model, " unknown model ", 16);
366 model[16] = 0;
367 strncpy(version, "????", 4);
368 version[4] = 0;
369 } else {
370 strncpy(manu, inqbuf.vendor, 8);
371 manu[8] = 0;
372 strncpy(model, inqbuf.product, 16);
373 model[16] = 0;
374 strncpy(version, inqbuf.revision, 4);
375 version[4] = 0;
a0becc21 376 }
519fb2b7
RG
377
378 /*
379 * Load the parameters for this kind of device, so we
380 * treat it as appropriate for each operating mode.
381 * Only check the number of characters in the array's
382 * model entry, not the entire model string returned.
383 */
a0becc21 384 finger = gallery;
519fb2b7 385 while (finger->name) {
a0becc21 386 model_len = 0;
519fb2b7 387 while (finger->model[model_len] && (model_len < 32)) {
a0becc21
NW
388 model2[model_len] = model[model_len];
389 model_len++;
390 }
391 model2[model_len] = 0;
519fb2b7
RG
392 if ((strcmp(manu, finger->manu) == 0)
393 && (strcmp(model2, finger->model) == 0 ||
394 strcmp("????????????????", finger->model) == 0)
395 && (strcmp(version, finger->version) == 0 ||
396 strcmp("????", finger->version) == 0)) {
397 printf("st%d: %s is a known rogue\n", unit, finger->name);
398 st->rogues = finger;
399 st->drive_quirks = finger->quirks;
400 st->quirks = finger->quirks; /*start value */
401 st_loadquirks(st);
a0becc21 402 break;
519fb2b7 403 } else {
a0becc21
NW
404 finger++; /* go to next suspect */
405 }
406 }
407}
15637ed4 408
519fb2b7
RG
409/*
410 * initialise the subdevices to the default (QUIRK) state.
411 * this will remove any setting made by the system operator or previous
412 * operations.
413 */
414void
415st_loadquirks(st)
416 struct st_data *st;
417{
418 int i;
419 struct modes *mode;
420 struct modes *mode2;
421
422 if (!st->rogues)
423 return;
424 mode = st->rogues->modes;
425 mode2 = st->modes;
426 for (i = 0; i < 4; i++) {
427 bzero(mode2, sizeof(struct modes));
428 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK
429 | DENSITY_SET_BY_QUIRK
430 | BLKSIZE_SET_BY_USER
431 | DENSITY_SET_BY_USER);
432 if (mode->blksiz && ((mode->quirks | st->drive_quirks)
433 & (ST_Q_FORCE_FIXED_MODE))) {
434 mode2->blksiz = mode->blksiz;
435 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
436 } else {
437 if ((mode->quirks | st->drive_quirks)
438 & ST_Q_FORCE_VAR_MODE) {
439 mode2->blksiz = 0;
440 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
441 }
442 }
443 if (mode->density) {
444 mode2->density = mode->density;
445 st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
446 }
447 mode++;
448 mode2++;
449 }
450}
451
452/*
453 * open the device.
454 */
455errval
fca1f689 456stopen(dev, flags)
519fb2b7
RG
457 dev_t dev;
458 u_int32 flags;
15637ed4 459{
519fb2b7
RG
460 u_int32 unit, mode, dsty;
461 errval errno = 0;
15637ed4 462 struct st_data *st;
519fb2b7 463 struct scsi_link *sc_link;
15637ed4
RG
464 unit = UNIT(dev);
465 mode = MODE(dev);
466 dsty = DSTY(dev);
15637ed4 467
519fb2b7
RG
468 /*
469 * Check the unit is legal
470 */
471 if (unit >= NST) {
472 return (ENXIO);
15637ed4 473 }
d4284689 474 st = st_data[unit];
519fb2b7
RG
475 /*
476 * Make sure the device has been initialised
477 */
d4284689 478 if ((st == NULL) || (!(st->flags & ST_INITIALIZED)))
519fb2b7
RG
479 return (ENXIO);
480
481 sc_link = st->sc_link;
482 SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n"
483 ,dev, unit, NST));
484 /*
485 * Only allow one at a time
486 */
487 if (st->flags & ST_OPEN) {
46d39670 488 return (EBUSY);
a0becc21 489 }
519fb2b7
RG
490 /*
491 * Throw out a dummy instruction to catch 'Unit attention
492 * errors (the error handling will invalidate all our
493 * device info if we get one, but otherwise, ignore it)
494 */
495 scsi_test_unit_ready(sc_link, SCSI_SILENT);
496
497 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */
498 /*
499 * If the mode is 3 (e.g. minor = 3,7,11,15)
500 * then the device has been openned to set defaults
501 * This mode does NOT ALLOW I/O, only ioctls
502 */
503 if (mode == CTLMODE)
504 return 0;
505
506 /*
507 * Check that the device is ready to use (media loaded?)
508 * This time take notice of the return result
509 */
510 if (errno = (scsi_test_unit_ready(sc_link, 0))) {
511 printf("st%d: not ready\n", unit);
512 st_unmount(unit, NOEJECT);
513 return (errno);
15637ed4 514 }
519fb2b7
RG
515 /*
516 * if it's a different mode, or if the media has been
517 * invalidated, unmount the tape from the previous
518 * session but continue with open processing
519 */
520 if ((st->last_dsty != dsty)
521 || (!(sc_link->flags & SDEV_MEDIA_LOADED))) {
522 st_unmount(unit, NOEJECT);
0269d546 523 }
519fb2b7
RG
524 /*
525 * If we are not mounted, then we should start a new
526 * mount session.
527 */
528 if (!(st->flags & ST_MOUNTED)) {
529 st_mount_tape(dev, flags);
530 st->last_dsty = dsty;
fca1f689 531 }
519fb2b7
RG
532 /*
533 * Make sure that a tape opened in write-only mode will have
534 * file marks written on it when closed, even if not written to.
535 * This is for SUN compatibility
536 */
fca1f689
RG
537 if ((flags & O_ACCMODE) == FWRITE)
538 st->flags |= ST_WRITTEN;
539
519fb2b7 540 SC_DEBUG(sc_link, SDEV_DB2, ("Open complete\n"));
15637ed4
RG
541
542 st->flags |= ST_OPEN;
519fb2b7 543 return (0);
15637ed4
RG
544}
545
519fb2b7
RG
546/*
547 * close the device.. only called if we are the LAST
548 * occurence of an open device
549 */
550errval
15637ed4 551stclose(dev)
519fb2b7 552 dev_t dev;
15637ed4 553{
519fb2b7
RG
554 unsigned char unit, mode;
555 struct st_data *st;
556 struct scsi_link *sc_link;
15637ed4
RG
557
558 unit = UNIT(dev);
559 mode = MODE(dev);
d4284689 560 st = st_data[unit];
519fb2b7
RG
561 sc_link = st->sc_link;
562
563 SC_DEBUG(sc_link, SDEV_DB1, ("closing\n"));
564 if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
565 st_write_filemarks(unit, 1, 0);
566 switch (mode & 0x3) {
567 case 0:
568 case 3: /* for now */
569 st_unmount(unit, NOEJECT);
15637ed4 570 break;
519fb2b7
RG
571 case 1: /*leave mounted unless media seems to have been removed */
572 if (!(sc_link->flags & SDEV_MEDIA_LOADED)) {
573 st_unmount(unit, NOEJECT);
574 }
15637ed4 575 break;
519fb2b7
RG
576 case 2:
577 st_unmount(unit, EJECT);
15637ed4 578 break;
15637ed4 579 }
519fb2b7
RG
580 sc_link->flags &= ~SDEV_OPEN;
581 st->flags &= ~ST_OPEN;
582 return (0);
15637ed4
RG
583}
584
519fb2b7
RG
585/*
586 * Start a new mount session.
587 * Copy in all the default parameters from the selected device mode.
588 * and try guess any that seem to be defaulted.
589 */
590errval
591st_mount_tape(dev, flags)
592 dev_t dev;
593 u_int32 flags;
a0becc21 594{
519fb2b7
RG
595 u_int32 unit, mode, dsty;
596 struct st_data *st;
597 struct scsi_link *sc_link;
598 errval errno = 0;
a0becc21 599
519fb2b7
RG
600 unit = UNIT(dev);
601 mode = MODE(dev);
602 dsty = DSTY(dev);
603 st = st_data[unit];
604 sc_link = st->sc_link;
605
606 if (st->flags & ST_MOUNTED)
607 return 0;
608
609 SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
610 st->flags |= ST_NEW_MOUNT;
611 st->quirks = st->drive_quirks | st->modes[dsty].quirks;
612 /*
613 * If the media is new, then make sure we give it a chance to
614 * to do a 'load' instruction. ( We assume it is new)
615 */
616 if (errno = st_load(unit, LD_LOAD, 0)) {
617 return (errno);
d4284689 618 }
519fb2b7
RG
619 /*
620 * Throw another dummy instruction to catch
621 * 'Unit attention' errors. Some drives appear to give
622 * these after doing a Load instruction.
623 * (noteably some DAT drives)
624 */
625 scsi_test_unit_ready(sc_link, SCSI_SILENT);
626
627 /*
628 * Some devices can't tell you much until they have been
629 * asked to look at the media. This quirk does this.
630 */
631 if (st->quirks & ST_Q_SNS_HLP) {
632 if (errno = st_touch_tape(unit))
633 return errno;
634 }
635 /*
636 * Load the physical device parameters
637 * loads: blkmin, blkmax
638 */
639 if (errno = st_rd_blk_lim(unit, 0)) {
640 return errno;
641 }
642 /*
643 * Load the media dependent parameters
644 * includes: media_blksiz,media_density,numblks
645 * As we have a tape in, it should be reflected here.
646 * If not you may need the "quirk" above.
647 */
648 if (errno = st_mode_sense(unit, 0)) {
649 return errno;
650 }
651 /*
652 * If we have gained a permanent density from somewhere,
653 * then use it in preference to the one supplied by
654 * default by the driver.
655 */
656 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER)) {
657 st->density = st->modes[dsty].density;
658 } else {
659 st->density = st->media_density;
660 }
661 /*
662 * If we have gained a permanent blocksize
663 * then use it in preference to the one supplied by
664 * default by the driver.
665 */
666 st->flags &= ~ST_FIXEDBLOCKS;
667 if (st->modeflags[dsty] & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
668 st->blksiz = st->modes[dsty].blksiz;
669 if (st->blksiz) {
670 st->flags |= ST_FIXEDBLOCKS;
671 }
672 } else {
673 if (errno = st_decide_mode(unit, FALSE)) {
674 return errno;
675 }
d4284689 676 }
519fb2b7
RG
677 if (errno = st_mode_select(unit, 0)) {
678 printf("st%d: Cannot set selected mode", unit);
679 return errno;
680 }
681 scsi_prevent(sc_link, PR_PREVENT, 0); /* who cares if it fails? */
682 st->flags &= ~ST_NEW_MOUNT;
683 st->flags |= ST_MOUNTED;
684 sc_link->flags |= SDEV_MEDIA_LOADED; /* move earlier? */
a0becc21 685
519fb2b7
RG
686 return 0;
687}
688
689/*
690 * End the present mount session.
691 * Rewind, and optionally eject the tape.
692 * Reset various flags to indicate that all new
693 * operations require another mount operation
694 */
695void
696st_unmount(int unit, boolean eject)
697{
698 struct st_data *st = st_data[unit];
699 struct scsi_link *sc_link = st->sc_link;
700 int32 nmarks;
701
702 if (!(st->flags & ST_MOUNTED))
703 return;
704 SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
705 st_chkeod(unit, FALSE, &nmarks, SCSI_SILENT);
706 st_rewind(unit, FALSE, SCSI_SILENT);
707 scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT);
708 if (eject) {
709 st_load(unit, LD_UNLOAD, SCSI_SILENT);
710 }
711 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
712 sc_link->flags &= ~SDEV_MEDIA_LOADED;
713}
714
715/*
716 * Given all we know about the device, media, mode, 'quirks' and
717 * initial operation, make a decision as to how we should be set
718 * to run (regarding blocking and EOD marks)
719 */
720errval
721st_decide_mode(unit, first_read)
722 u_int32 unit;
723 boolean first_read;
724{
725 struct st_data *st = st_data[unit];
726#ifdef SCSIDEBUG
727 struct scsi_link *sc_link = st->sc_link;
728#endif
729
730 SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
731
732 /*
733 * If the user hasn't already specified fixed or variable-length
734 * blocks and the block size (zero if variable-length), we'll
735 * have to try to figure them out ourselves.
736 *
737 * Our first shot at a method is, "The quirks made me do it!"
738 */
fde1aeb2 739 switch ((int)(st->quirks & (ST_Q_FORCE_FIXED_MODE | ST_Q_FORCE_VAR_MODE))) {
519fb2b7
RG
740 case (ST_Q_FORCE_FIXED_MODE | ST_Q_FORCE_VAR_MODE):
741 printf("st%d: bad quirks\n", unit);
d4284689 742 return (EINVAL);
519fb2b7 743 case ST_Q_FORCE_FIXED_MODE: /*specified fixed, but not what size */
a0becc21 744 st->flags |= ST_FIXEDBLOCKS;
0269d546 745 if (st->blkmin && (st->blkmin == st->blkmax))
d4284689 746 st->blksiz = st->blkmin;
519fb2b7 747 else if (st->media_blksiz > 0)
a0becc21 748 st->blksiz = st->media_blksiz;
a0becc21 749 else
d4284689 750 st->blksiz = DEF_FIXED_BSIZE;
519fb2b7
RG
751 SC_DEBUG(sc_link, SDEV_DB3, ("Quirks force fixed mode(%d)\n",
752 st->blksiz));
d4284689 753 goto done;
519fb2b7 754 case ST_Q_FORCE_VAR_MODE:
a0becc21
NW
755 st->flags &= ~ST_FIXEDBLOCKS;
756 st->blksiz = 0;
519fb2b7 757 SC_DEBUG(sc_link, SDEV_DB3, ("Quirks force variable mode\n"));
d4284689 758 goto done;
a0becc21 759 }
519fb2b7
RG
760 /*
761 * If the drive can only handle fixed-length blocks and only at
762 * one size, perhaps we should just do that.
763 */
764 if (st->blkmin && (st->blkmin == st->blkmax)) {
d4284689
RG
765 st->flags |= ST_FIXEDBLOCKS;
766 st->blksiz = st->blkmin;
519fb2b7
RG
767 SC_DEBUG(sc_link, SDEV_DB3,
768 ("blkmin == blkmax of %d\n", st->blkmin));
d4284689
RG
769 goto done;
770 }
519fb2b7
RG
771 /*
772 * If the tape density mandates (or even suggests) use of fixed
773 * or variable-length blocks, comply.
774 */
fde1aeb2 775 switch ((int)st->density) {
519fb2b7
RG
776 case HALFINCH_800:
777 case HALFINCH_1600:
778 case HALFINCH_6250:
779 case DDS:
d4284689
RG
780 st->flags &= ~ST_FIXEDBLOCKS;
781 st->blksiz = 0;
519fb2b7 782 SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
d4284689 783 goto done;
519fb2b7
RG
784 case QIC_11:
785 case QIC_24:
786 case QIC_120:
787 case QIC_150:
788 case QIC_525:
789 case QIC_1320:
d4284689 790 st->flags |= ST_FIXEDBLOCKS;
519fb2b7 791 if (st->media_blksiz > 0) {
d4284689 792 st->blksiz = st->media_blksiz;
519fb2b7 793 } else {
d4284689 794 st->blksiz = DEF_FIXED_BSIZE;
519fb2b7
RG
795 }
796 SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
d4284689
RG
797 goto done;
798 }
519fb2b7
RG
799 /*
800 * If we're about to read the tape, perhaps we should choose
801 * fixed or variable-length blocks and block size according to
802 * what the drive found on the tape.
803 */
804 if (first_read
805 && (!(st->quirks & ST_Q_BLKSIZ)
806 || (st->media_blksiz == 0)
807 || (st->media_blksiz == DEF_FIXED_BSIZE)
808 || (st->media_blksiz == 1024))) {
809 if (st->media_blksiz == 0) {
d4284689 810 st->flags &= ~ST_FIXEDBLOCKS;
519fb2b7 811 } else {
d4284689 812 st->flags |= ST_FIXEDBLOCKS;
519fb2b7 813 }
d4284689 814 st->blksiz = st->media_blksiz;
519fb2b7
RG
815 SC_DEBUG(sc_link, SDEV_DB3,
816 ("Used media_blksiz of %d\n", st->media_blksiz));
d4284689
RG
817 goto done;
818 }
519fb2b7
RG
819 /*
820 * We're getting no hints from any direction. Choose variable-
821 * length blocks arbitrarily.
822 */
0269d546
RG
823 st->flags &= ~ST_FIXEDBLOCKS;
824 st->blksiz = 0;
519fb2b7 825 SC_DEBUG(sc_link, SDEV_DB3, ("Give up and default to variable mode\n"));
d4284689 826done:
519fb2b7
RG
827
828 /*
829 * Decide whether or not to write two file marks to signify end-
830 * of-data. Make the decision as a function of density. If
831 * the decision is not to use a second file mark, the SCSI BLANK
832 * CHECK condition code will be recognized as end-of-data when
833 * first read.
834 * (I think this should be a by-product of fixed/variable..julian)
835 */
fde1aeb2 836 switch ((int)st->density) {
519fb2b7
RG
837/* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
838 case QIC_11:
839 case QIC_24:
840 case QIC_120:
841 case QIC_150:
842 case QIC_525:
843 case QIC_1320:
844 st->flags &= ~ST_2FM_AT_EOD;
845 break;
846 default:
847 st->flags |= ST_2FM_AT_EOD;
848 }
849 return 0;
d4284689 850}
15637ed4 851
519fb2b7
RG
852/*
853 * trim the size of the transfer if needed,
854 * called by physio
855 * basically the smaller of our min and the scsi driver's
856 * minphys
857 */
858void
859stminphys(bp)
860 struct buf *bp;
15637ed4 861{
519fb2b7 862 (*(st_data[UNIT(bp->b_dev)]->sc_link->adapter->scsi_minphys)) (bp);
15637ed4
RG
863}
864
519fb2b7
RG
865/*
866 * Actually translate the requested transfer into
867 * one the physical driver can understand
868 * The transfer is described by a buf and will include
869 * only one physical transfer.
870 */
871void
872ststrategy(bp)
873 struct buf *bp;
15637ed4 874{
519fb2b7 875 struct buf **dp;
15637ed4 876 unsigned char unit;
519fb2b7 877 u_int32 opri;
d4284689 878 struct st_data *st;
15637ed4
RG
879
880 ststrats++;
881 unit = UNIT((bp->b_dev));
d4284689 882 st = st_data[unit];
519fb2b7
RG
883 SC_DEBUG(st->sc_link, SDEV_DB1,
884 (" strategy: %d bytes @ blk%d\n", bp->b_bcount, bp->b_blkno));
885 /*
886 * If it's a null transfer, return immediatly
887 */
888 if (bp->b_bcount == 0) {
15637ed4
RG
889 goto done;
890 }
519fb2b7
RG
891 /*
892 * Odd sized request on fixed drives are verboten
893 */
894 if (st->flags & ST_FIXEDBLOCKS) {
895 if (bp->b_bcount % st->blksiz) {
a0becc21 896 printf("st%d: bad request, must be multiple of %d\n",
519fb2b7 897 unit, st->blksiz);
a0becc21
NW
898 bp->b_error = EIO;
899 goto bad;
900 }
901 }
519fb2b7
RG
902 /*
903 * as are out-of-range requests on variable drives.
904 */
905 else if (bp->b_bcount < st->blkmin || bp->b_bcount > st->blkmax) {
d4284689 906 printf("st%d: bad request, must be between %d and %d\n",
519fb2b7 907 unit, st->blkmin, st->blkmax);
15637ed4
RG
908 bp->b_error = EIO;
909 goto bad;
910 }
15637ed4 911 stminphys(bp);
15637ed4 912 opri = splbio();
15637ed4 913
519fb2b7
RG
914 /*
915 * Place it in the queue of activities for this tape
916 * at the end (a bit silly because we only have on user..
917 * (but it could fork() ))
918 */
d4284689 919 dp = &(st->buf_queue);
519fb2b7 920 while (*dp) {
d4284689 921 dp = &((*dp)->b_actf);
519fb2b7 922 }
d4284689 923 *dp = bp;
2aecffae 924 bp->b_actf = NULL;
15637ed4 925
519fb2b7
RG
926 /*
927 * Tell the device to get going on the transfer if it's
928 * not doing anything, otherwise just wait for completion
929 * (All a bit silly if we're only allowing 1 open but..)
930 */
15637ed4
RG
931 ststart(unit);
932
933 splx(opri);
934 return;
935bad:
936 bp->b_flags |= B_ERROR;
937done:
519fb2b7
RG
938 /*
939 * Correctly set the buf to indicate a completed xfer
940 */
15637ed4
RG
941 iodone(bp);
942 return;
943}
944
519fb2b7
RG
945/*
946 * ststart looks to see if there is a buf waiting for the device
947 * and that the device is not already busy. If both are true,
948 * It dequeues the buf and creates a scsi command to perform the
949 * transfer required. The transfer request will call scsi_done
950 * on completion, which will in turn call this routine again
951 * so that the next queued transfer is performed.
952 * The bufs are queued by the strategy routine (ststrategy)
953 *
954 * This routine is also called after other non-queued requests
955 * have been made of the scsi driver, to ensure that the queue
956 * continues to be drained.
957 * ststart() is called at splbio
958 */
959void
960ststart(unit)
961 u_int32 unit;
962{
963 struct st_data *st = st_data[unit];
964 struct scsi_link *sc_link = st->sc_link;
965 register struct buf *bp = 0;
966 struct scsi_rw_tape cmd;
967 u_int32 flags;
968
969 SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
970 /*
971 * See if there is a buf to do and we are not already
972 * doing one
973 */
974 while (sc_link->opennings != 0) {
975
976 /* if a special awaits, let it proceed first */
977 if (sc_link->flags & SDEV_WAITING) {
46d39670 978 sc_link->flags &= ~SDEV_WAITING;
519fb2b7
RG
979 wakeup((caddr_t)sc_link);
980 return;
981 }
982 if ((bp = st->buf_queue) == NULL) {
983 return; /* no work to bother with */
984 }
985 st->buf_queue = bp->b_actf;
986
987 /*
988 * if the device has been unmounted byt the user
989 * then throw away all requests until done
990 */
991 if ((!(st->flags & ST_MOUNTED))
992 || (!(sc_link->flags & SDEV_MEDIA_LOADED))) {
993 /* make sure that one implies the other.. */
994 sc_link->flags &= ~SDEV_MEDIA_LOADED;
995 goto badnews;
996 }
997 /*
998 * only FIXEDBLOCK devices have pending operations
999 */
1000 if (st->flags & ST_FIXEDBLOCKS) {
1001 /*
1002 * If we are at a filemark but have not reported it yet
1003 * then we should report it now
1004 */
1005 if (st->flags & ST_AT_FILEMARK) {
1006 if ((bp->b_flags & B_READ) == B_WRITE) {
1007 /*
1008 * Handling of ST_AT_FILEMARK in
1009 * st_space will fill in the right file
1010 * mark count.
1011 * Back up over filemark
1012 */
1013 if (st_space(unit, 0, SP_FILEMARKS, 0) !=
1014 ESUCCESS)
1015 goto badnews;
1016 } else {
1017 bp->b_resid = bp->b_bcount;
1018 bp->b_error = 0;
1019 bp->b_flags &= ~B_ERROR;
1020 st->flags &= ~ST_AT_FILEMARK;
1021 biodone(bp);
1022 continue; /* seek more work */
1023 }
1024 }
1025 /*
1026 * If we are at EIO (e.g. EOM) but have not reported it
1027 * yet then we should report it now
1028 */
1029 if (st->flags & ST_EIO_PENDING) {
1030 bp->b_resid = bp->b_bcount;
1031 bp->b_error = EIO;
1032 bp->b_flags |= B_ERROR;
1033 st->flags &= ~ST_EIO_PENDING;
1034 biodone(bp);
1035 continue; /* seek more work */
1036 }
1037 }
1038 /*
1039 * Fill out the scsi command
1040 */
1041 bzero(&cmd, sizeof(cmd));
1042 if ((bp->b_flags & B_READ) == B_WRITE) {
1043 cmd.op_code = WRITE_COMMAND_TAPE;
1044 st->flags &= ~ST_FM_WRITTEN;
1045 st->flags |= ST_WRITTEN;
1046 flags = SCSI_DATA_OUT;
1047 } else {
1048 cmd.op_code = READ_COMMAND_TAPE;
1049 flags = SCSI_DATA_IN;
1050 }
1051 /*
1052 * Handle "fixed-block-mode" tape drives by using the
1053 * block count instead of the length.
1054 */
1055 if (st->flags & ST_FIXEDBLOCKS) {
1056 cmd.byte2 |= SRWT_FIXED;
1057 lto3b(bp->b_bcount / st->blksiz, cmd.len);
1058 } else {
1059 lto3b(bp->b_bcount, cmd.len);
1060 }
1061 /*
1062 * go ask the adapter to do all this for us
1063 */
1064 if (scsi_scsi_cmd(sc_link,
1065 (struct scsi_generic *) &cmd,
1066 sizeof(cmd),
1067 (u_char *) bp->b_un.b_addr,
1068 bp->b_bcount,
1069 0, /* can't retry a read on a tape really */
1070 100000,
1071 bp,
1072 flags | SCSI_NOSLEEP) == SUCCESSFULLY_QUEUED) {
1073 stqueues++;
1074 } else {
1075badnews:
1076 printf("st%d: oops not queued\n", unit);
1077 bp->b_flags |= B_ERROR;
1078 bp->b_error = EIO;
1079 biodone(bp);
1080 }
1081 } /* go back and see if we can cram more work in.. */
1082}
15637ed4 1083
519fb2b7
RG
1084/*
1085 * Perform special action on behalf of the user;
1086 * knows about the internals of this device
1087 */
1088errval
1089stioctl(dev, cmd, arg, flag)
1090 dev_t dev;
1091 int cmd;
1092 caddr_t arg;
1093 int flag;
d4284689 1094{
519fb2b7 1095 errval errcode = 0;
d4284689 1096 unsigned char unit;
519fb2b7
RG
1097 u_int32 number, flags, dsty;
1098 struct st_data *st;
1099 u_int32 hold_blksiz;
1100 u_int32 hold_density;
1101 int32 nmarks;
1102 struct mtop *mt = (struct mtop *) arg;
1103
1104 /*
1105 * Find the device that the user is talking about
1106 */
1107 flags = 0; /* give error messages, act on errors etc. */
15637ed4 1108 unit = UNIT(dev);
0269d546 1109 dsty = DSTY(dev);
519fb2b7
RG
1110 st = st_data[unit];
1111 hold_blksiz = st->blksiz;
1112 hold_density = st->density;
15637ed4 1113
519fb2b7 1114 switch (cmd) {
15637ed4
RG
1115
1116 case MTIOCGET:
519fb2b7
RG
1117 {
1118 struct mtget *g = (struct mtget *) arg;
1119
1120 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
1121 bzero(g, sizeof(struct mtget));
1122 g->mt_type = 0x7; /* Ultrix compat *//*? */
1123 g->mt_density = st->density;
1124 g->mt_blksiz = st->blksiz;
1125 g->mt_density0 = st->modes[0].density;
1126 g->mt_density1 = st->modes[1].density;
1127 g->mt_density2 = st->modes[2].density;
1128 g->mt_density3 = st->modes[3].density;
1129 g->mt_blksiz0 = st->modes[0].blksiz;
1130 g->mt_blksiz1 = st->modes[1].blksiz;
1131 g->mt_blksiz2 = st->modes[2].blksiz;
1132 g->mt_blksiz3 = st->modes[3].blksiz;
1133 break;
1134 }
15637ed4 1135 case MTIOCTOP:
519fb2b7 1136 {
15637ed4 1137
519fb2b7
RG
1138 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: op=0x%x count=0x%x]\n",
1139 mt->mt_op, mt->mt_count));
15637ed4 1140
519fb2b7
RG
1141 /* compat: in U*x it is a short */
1142 number = mt->mt_count;
1143 switch ((short) (mt->mt_op)) {
1144 case MTWEOF: /* write an end-of-file record */
1145 errcode = st_write_filemarks(unit, number, flags);
d4284689 1146 break;
519fb2b7
RG
1147 case MTBSF: /* backward space file */
1148 number = -number;
1149 case MTFSF: /* forward space file */
1150 errcode = st_chkeod(unit, FALSE, &nmarks, flags);
1151 if (errcode == ESUCCESS)
1152 errcode = st_space(unit, number - nmarks,
1153 SP_FILEMARKS, flags);
1154 break;
1155 case MTBSR: /* backward space record */
1156 number = -number;
1157 case MTFSR: /* forward space record */
1158 errcode = st_chkeod(unit, TRUE, &nmarks, flags);
1159 if (errcode == ESUCCESS)
1160 errcode = st_space(unit, number, SP_BLKS, flags);
1161 break;
1162 case MTREW: /* rewind */
1163 errcode = st_rewind(unit, FALSE, flags);
1164 break;
1165 case MTOFFL: /* rewind and put the drive offline */
1166 st_unmount(unit, EJECT);
1167 break;
1168 case MTNOP: /* no operation, sets status only */
1169 case MTCACHE: /* enable controller cache */
1170 case MTNOCACHE: /* disable controller cache */
1171 break;
1172 case MTSETBSIZ: /* Set block size for device */
1173#ifdef NOTYET
1174 if (!(st->flags & ST_NEW_MOUNT)) {
1175 uprintf("re-mount tape before changing blocksize");
0269d546
RG
1176 errcode = EINVAL;
1177 break;
1178 }
519fb2b7
RG
1179#endif
1180 if (number == 0) {
1181 st->flags &= ~ST_FIXEDBLOCKS;
1182 } else {
1183 if ((st->blkmin || st->blkmax) /* they exist */
1184 &&((number < st->blkmin
1185 || number > st->blkmax))) {
1186 errcode = EINVAL;
1187 break;
1188 }
1189 st->flags |= ST_FIXEDBLOCKS;
1190 }
1191 st->blksiz = number;
1192 st->flags |= ST_BLOCK_SET; /*XXX */
1193 goto try_new_value;
a0becc21 1194
519fb2b7
RG
1195 case MTSETDNSTY: /* Set density for device and mode */
1196 if (number > SCSI_2_MAX_DENSITY_CODE) {
1197 errcode = EINVAL;
1198 } else {
1199 st->density = number;
1200 }
1201 goto try_new_value;
a0becc21 1202
519fb2b7 1203 default:
869c4419 1204 errcode = EINVAL;
a0becc21 1205 }
a0becc21 1206 break;
15637ed4 1207 }
15637ed4
RG
1208 case MTIOCIEOT:
1209 case MTIOCEEOT:
15637ed4 1210 break;
869c4419 1211 default:
519fb2b7
RG
1212 if(MODE(dev) == CTLMODE)
1213 errcode = scsi_do_ioctl(st->sc_link,cmd,arg,flag);
1214 else
1215 errcode = ENOTTY;
1216 break;
15637ed4 1217 }
869c4419 1218 return errcode;
519fb2b7
RG
1219/*-----------------------------*/
1220try_new_value:
1221 /*
1222 * Check that the mode being asked for is aggreeable to the
1223 * drive. If not, put it back the way it was.
1224 */
1225 if (errcode = st_mode_select(unit, 0)) { /* put it back as it was */
1226 printf("st%d: Cannot set selected mode", unit);
1227 st->density = hold_density;
1228 st->blksiz = hold_blksiz;
1229 if (st->blksiz) {
1230 st->flags |= ST_FIXEDBLOCKS;
1231 } else {
1232 st->flags &= ~ST_FIXEDBLOCKS;
1233 }
1234 return (errcode);
1235 }
1236 /*
1237 * As the drive liked it, if we are setting a new default,
1238 * set it into the structures as such.
1239 *
1240 * The means for deciding this are not finalised yet
1241 */
1242 if (MODE(dev) == 0x03) {
1243 /* special mode */
1244 /* XXX */
1245 switch ((short) (mt->mt_op)) {
1246 case MTSETBSIZ:
1247 st->modes[dsty].blksiz = st->blksiz;
1248 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1249 break;
1250 case MTSETDNSTY:
1251 st->modes[dsty].density = st->density;
1252 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1253 break;
1254 }
1255 }
1256 return 0;
15637ed4
RG
1257}
1258
519fb2b7
RG
1259/*
1260 * Do a synchronous read.
1261 */
1262errval
1263st_read(unit, buf, size, flags)
1264 u_int32 unit, size, flags;
1265 char *buf;
15637ed4 1266{
d4284689
RG
1267 struct scsi_rw_tape scsi_cmd;
1268 struct st_data *st = st_data[unit];
15637ed4 1269
519fb2b7
RG
1270 /*
1271 * If it's a null transfer, return immediatly
1272 */
1273 if (size == 0) {
1274 return (ESUCCESS);
d4284689 1275 }
d4284689
RG
1276 bzero(&scsi_cmd, sizeof(scsi_cmd));
1277 scsi_cmd.op_code = READ_COMMAND_TAPE;
519fb2b7
RG
1278 if (st->flags & ST_FIXEDBLOCKS) {
1279 scsi_cmd.byte2 |= SRWT_FIXED;
1280 lto3b(size / (st->blksiz ? st->blksiz : DEF_FIXED_BSIZE),
1281 scsi_cmd.len);
1282 } else {
1283 lto3b(size, scsi_cmd.len);
1284 }
1285 return (scsi_scsi_cmd(st->sc_link,
1286 (struct scsi_generic *) &scsi_cmd,
1287 sizeof(scsi_cmd),
1288 (u_char *) buf,
1289 size,
1290 0, /* not on io commands */
1291 100000,
1292 NULL,
1293 flags | SCSI_DATA_IN));
15637ed4 1294}
15637ed4
RG
1295#ifdef __STDC__
1296#define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0 )
1297#else
1298#define b2tol(a) (((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0 )
1299#endif
1300
519fb2b7
RG
1301/*
1302 * Ask the drive what it's min and max blk sizes are.
1303 */
1304errval
15637ed4 1305st_rd_blk_lim(unit, flags)
519fb2b7 1306 u_int32 unit, flags;
15637ed4 1307{
519fb2b7 1308 struct scsi_blk_limits scsi_cmd;
15637ed4 1309 struct scsi_blk_limits_data scsi_blkl;
d4284689 1310 struct st_data *st = st_data[unit];
519fb2b7
RG
1311 errval errno;
1312 struct scsi_link *sc_link = st->sc_link;
1313
1314 /*
1315 * First check if we have it all loaded
1316 */
1317 if ((sc_link->flags & SDEV_MEDIA_LOADED))
1318 return 0;
1319
1320 /*
1321 * do a 'Read Block Limits'
1322 */
15637ed4
RG
1323 bzero(&scsi_cmd, sizeof(scsi_cmd));
1324 scsi_cmd.op_code = READ_BLK_LIMITS;
1325
519fb2b7
RG
1326 /*
1327 * do the command, update the global values
1328 */
1329 if (errno = scsi_scsi_cmd(sc_link,
1330 (struct scsi_generic *) &scsi_cmd,
1331 sizeof(scsi_cmd),
1332 (u_char *) & scsi_blkl,
1333 sizeof(scsi_blkl),
1334 ST_RETRIES,
1335 5000,
1336 NULL,
1337 flags | SCSI_DATA_IN)) {
d4284689 1338 return errno;
519fb2b7 1339 }
15637ed4
RG
1340 st->blkmin = b2tol(scsi_blkl.min_length);
1341 st->blkmax = _3btol(&scsi_blkl.max_length_2);
1342
519fb2b7
RG
1343 SC_DEBUG(sc_link, SDEV_DB3,
1344 ("(%d <= blksiz <= %d)\n", st->blkmin, st->blkmax));
d4284689 1345 return 0;
15637ed4 1346}
a0becc21 1347
519fb2b7
RG
1348/*
1349 * Get the scsi driver to send a full inquiry to the
1350 * device and use the results to fill out the global
1351 * parameter structure.
1352 *
1353 * called from:
1354 * attach
1355 * open
1356 * ioctl (to reset original blksize)
1357 */
1358errval
15637ed4 1359st_mode_sense(unit, flags)
519fb2b7 1360 u_int32 unit, flags;
15637ed4 1361{
519fb2b7
RG
1362 u_int32 scsi_sense_len;
1363 errval errno;
1364 char *scsi_sense_ptr;
1365 struct scsi_mode_sense scsi_cmd;
1366 struct scsi_sense {
1367 struct scsi_mode_header header;
1368 struct blk_desc blk_desc;
1369 } scsi_sense;
1370
1371 struct scsi_sense_page_0 {
1372 struct scsi_mode_header header;
1373 struct blk_desc blk_desc;
1374 unsigned char sense_data[PAGE_0_SENSE_DATA_SIZE];
1375 /* Tandberg tape drives returns page 00
1376 * with the sense data, whether or not
1377 * you want it( ie the don't like you
1378 * saying you want anything less!!!!!
1379 * They also expect page 00
1380 * back when you issue a mode select
1381 */
1382 } scsi_sense_page_0;
1383 struct st_data *st = st_data[unit];
1384 struct scsi_link *sc_link = st->sc_link;
15637ed4 1385
519fb2b7
RG
1386 /*
1387 * Define what sort of structure we're working with
1388 */
1389 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
a0becc21
NW
1390 scsi_sense_len = sizeof(scsi_sense_page_0);
1391 scsi_sense_ptr = (char *) &scsi_sense_page_0;
519fb2b7 1392 } else {
a0becc21
NW
1393 scsi_sense_len = sizeof(scsi_sense);
1394 scsi_sense_ptr = (char *) &scsi_sense;
1395 }
519fb2b7
RG
1396 /*
1397 * Set up a mode sense
1398 */
15637ed4
RG
1399 bzero(&scsi_cmd, sizeof(scsi_cmd));
1400 scsi_cmd.op_code = MODE_SENSE;
a0becc21
NW
1401 scsi_cmd.length = scsi_sense_len;
1402
519fb2b7
RG
1403 /*
1404 * do the command, but we don't need the results
1405 * just print them for our interest's sake, if asked,
1406 * or if we need it as a template for the mode select
1407 * store it away.
1408 */
1409 if (errno = scsi_scsi_cmd(sc_link,
1410 (struct scsi_generic *) &scsi_cmd,
1411 sizeof(scsi_cmd),
1412 (u_char *) scsi_sense_ptr,
1413 scsi_sense_len,
1414 ST_RETRIES,
1415 5000,
1416 NULL,
1417 flags | SCSI_DATA_IN)) {
d4284689 1418 return errno;
519fb2b7
RG
1419 }
1420 st->numblks = _3btol(((struct scsi_sense *)scsi_sense_ptr)->blk_desc.nblocks);
1421 st->media_blksiz = _3btol(((struct scsi_sense *)scsi_sense_ptr)->blk_desc.blklen);
1422 st->media_density = ((struct scsi_sense *) scsi_sense_ptr)->blk_desc.density;
1423 if (((struct scsi_sense *) scsi_sense_ptr)->header.dev_spec &
1424 SMH_DSP_WRITE_PROT) {
d4284689 1425 st->flags |= ST_READONLY;
15637ed4 1426 }
519fb2b7
RG
1427 SC_DEBUG(sc_link, SDEV_DB3,
1428 ("density code 0x%x, %d-byte blocks, write-%s, ",
1429 st->media_density, st->media_blksiz,
1430 st->flags & ST_READONLY ? "protected" : "enabled"));
1431 SC_DEBUG(sc_link, SDEV_DB3,
1432 ("%sbuffered\n",
1433 ((struct scsi_sense *) scsi_sense_ptr)->header.dev_spec
1434 & SMH_DSP_BUFF_MODE ? "" : "un"));
1435 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1436 bcopy(((struct scsi_sense_page_0 *) scsi_sense_ptr)->sense_data,
1437 st->sense_data,
1438 sizeof(((struct scsi_sense_page_0 *) scsi_sense_ptr)->sense_data));
a0becc21 1439 }
519fb2b7 1440 sc_link->flags |= SDEV_MEDIA_LOADED;
d4284689 1441 return 0;
15637ed4
RG
1442}
1443
519fb2b7
RG
1444/*
1445 * Send a filled out parameter structure to the drive to
1446 * set it into the desire modes etc.
1447 */
1448errval
d4284689 1449st_mode_select(unit, flags)
519fb2b7 1450 u_int32 unit, flags;
15637ed4 1451{
519fb2b7
RG
1452 u_int32 dat_len;
1453 char *dat_ptr;
15637ed4 1454 struct scsi_mode_select scsi_cmd;
519fb2b7
RG
1455 struct dat {
1456 struct scsi_mode_header header;
1457 struct blk_desc blk_desc;
1458 } dat;
1459 struct dat_page_0 {
1460 struct scsi_mode_header header;
1461 struct blk_desc blk_desc;
1462 unsigned char sense_data[PAGE_0_SENSE_DATA_SIZE];
1463 } dat_page_0;
1464 struct st_data *st = st_data[unit];
1465
1466 /*
1467 * Define what sort of structure we're working with
1468 */
1469 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
a0becc21
NW
1470 dat_len = sizeof(dat_page_0);
1471 dat_ptr = (char *) &dat_page_0;
519fb2b7 1472 } else {
a0becc21
NW
1473 dat_len = sizeof(dat);
1474 dat_ptr = (char *) &dat;
1475 }
519fb2b7
RG
1476 /*
1477 * Set up for a mode select
1478 */
a0becc21 1479 bzero(dat_ptr, dat_len);
15637ed4
RG
1480 bzero(&scsi_cmd, sizeof(scsi_cmd));
1481 scsi_cmd.op_code = MODE_SELECT;
a0becc21 1482 scsi_cmd.length = dat_len;
519fb2b7
RG
1483 ((struct dat *) dat_ptr)->header.blk_desc_len = sizeof(struct blk_desc);
1484 ((struct dat *) dat_ptr)->header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
1485 ((struct dat *) dat_ptr)->blk_desc.density = st->density;
1486 if (st->flags & ST_FIXEDBLOCKS) {
1487 lto3b(st->blksiz, ((struct dat *) dat_ptr)->blk_desc.blklen);
a0becc21 1488 }
519fb2b7
RG
1489 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1490 bcopy(st->sense_data, ((struct dat_page_0 *) dat_ptr)->sense_data,
1491 sizeof(((struct dat_page_0 *) dat_ptr)->sense_data));
1492 /* the Tandberg tapes need the block size to */
1493 /* be set on each mode sense/select. */
15637ed4 1494 }
519fb2b7
RG
1495 /*
1496 * do the command
1497 */
1498 return (scsi_scsi_cmd(st->sc_link,
1499 (struct scsi_generic *) &scsi_cmd,
1500 sizeof(scsi_cmd),
1501 (u_char *) dat_ptr,
1502 dat_len,
1503 ST_RETRIES,
1504 5000,
1505 NULL,
1506 flags | SCSI_DATA_OUT));
15637ed4
RG
1507}
1508
519fb2b7
RG
1509/*
1510 * skip N blocks/filemarks/seq filemarks/eom
1511 */
1512errval
1513st_space(unit, number, what, flags)
1514 u_int32 unit, what, flags;
1515 int32 number;
15637ed4 1516{
519fb2b7 1517 errval error;
15637ed4 1518 struct scsi_space scsi_cmd;
519fb2b7 1519 struct st_data *st = st_data[unit];
15637ed4 1520
fde1aeb2 1521 switch ((int)what) {
519fb2b7
RG
1522 case SP_BLKS:
1523 if (st->flags & ST_PER_ACTION) {
1524 if (number > 0) {
fca1f689 1525 st->flags &= ~ST_PER_ACTION;
519fb2b7
RG
1526 return (EIO);
1527 } else if (number < 0) {
1528 if (st->flags & ST_AT_FILEMARK) {
1529 /*
1530 * Handling of ST_AT_FILEMARK
1531 * in st_space will fill in the
1532 * right file mark count.
1533 */
fca1f689
RG
1534 error = st_space(unit, 0, SP_FILEMARKS,
1535 flags);
519fb2b7
RG
1536 if (error)
1537 return (error);
fca1f689 1538 }
519fb2b7 1539 if (st->flags & ST_BLANK_READ) {
fca1f689 1540 st->flags &= ~ST_BLANK_READ;
519fb2b7 1541 return (EIO);
fca1f689
RG
1542 }
1543 st->flags &= ~ST_EIO_PENDING;
1544 }
1545 }
1546 break;
519fb2b7
RG
1547 case SP_FILEMARKS:
1548 if (st->flags & ST_EIO_PENDING) {
1549 if (number > 0) { /* pretend we just discover the error */
fca1f689 1550 st->flags &= ~ST_EIO_PENDING;
519fb2b7
RG
1551 return (EIO);
1552 } else if (number < 0) { /* back away from the error */
fca1f689 1553 st->flags &= ~ST_EIO_PENDING;
519fb2b7 1554 }
fca1f689 1555 }
519fb2b7 1556 if (st->flags & ST_AT_FILEMARK) {
fca1f689
RG
1557 st->flags &= ~ST_AT_FILEMARK;
1558 number--;
1559 }
519fb2b7 1560 if ((st->flags & ST_BLANK_READ) && (number < 0)) { /* back away from unwritten tape */
fca1f689 1561 st->flags &= ~ST_BLANK_READ;
519fb2b7 1562 number++; /* dubious */
fca1f689
RG
1563 }
1564 }
519fb2b7
RG
1565 if (number == 0) {
1566 return (ESUCCESS);
1567 }
15637ed4
RG
1568 bzero(&scsi_cmd, sizeof(scsi_cmd));
1569 scsi_cmd.op_code = SPACE;
869c4419 1570 scsi_cmd.byte2 = what & SS_CODE;
519fb2b7
RG
1571 lto3b(number, scsi_cmd.number);
1572 return (scsi_scsi_cmd(st->sc_link,
1573 (struct scsi_generic *) &scsi_cmd,
1574 sizeof(scsi_cmd),
1575 0,
1576 0,
1577 0, /* no retries please , just fail */
1578 600000, /* 10 mins enough? */
1579 NULL,
1580 flags));
15637ed4 1581}
519fb2b7
RG
1582
1583/*
1584 * write N filemarks
1585 */
1586errval
1587st_write_filemarks(unit, number, flags)
1588 u_int32 unit, flags;
1589 int32 number;
15637ed4
RG
1590{
1591 struct scsi_write_filemarks scsi_cmd;
519fb2b7 1592 struct st_data *st = st_data[unit];
15637ed4 1593
519fb2b7
RG
1594 /*
1595 * It's hard to write a negative number of file marks.
1596 * Don't try.
1597 */
1598 if (number < 0) {
1599 return EINVAL;
1600 }
fde1aeb2 1601 switch ((int)number) {
519fb2b7 1602 case 0: /* really a command to sync the drive's buffers */
fca1f689 1603 break;
519fb2b7
RG
1604 case 1:
1605 if (st->flags & ST_FM_WRITTEN) { /* already have one down */
fca1f689 1606 st->flags &= ~ST_WRITTEN;
519fb2b7 1607 } else {
fca1f689 1608 st->flags |= ST_FM_WRITTEN;
519fb2b7 1609 }
fca1f689
RG
1610 st->flags &= ~ST_PER_ACTION;
1611 break;
1612 default:
1613 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1614 }
15637ed4
RG
1615 bzero(&scsi_cmd, sizeof(scsi_cmd));
1616 scsi_cmd.op_code = WRITE_FILEMARKS;
519fb2b7
RG
1617 lto3b(number, scsi_cmd.number);
1618 return scsi_scsi_cmd(st->sc_link,
1619 (struct scsi_generic *) &scsi_cmd,
1620 sizeof(scsi_cmd),
1621 0,
1622 0,
1623 0, /* no retries, just fail */
1624 100000, /* 10 secs.. (may need to repos head ) */
1625 NULL,
1626 flags);
15637ed4 1627}
fca1f689 1628
519fb2b7
RG
1629/*
1630 * Make sure the right number of file marks is on tape if the
1631 * tape has been written. If the position argument is true,
1632 * leave the tape positioned where it was originally.
1633 *
1634 * nmarks returns the number of marks to skip (or, if position
1635 * true, which were skipped) to get back original position.
1636 */
1637int32
fca1f689 1638st_chkeod(unit, position, nmarks, flags)
519fb2b7
RG
1639 u_int32 unit;
1640 boolean position;
1641 int32 *nmarks;
1642 u_int32 flags;
fca1f689 1643{
519fb2b7
RG
1644 errval error;
1645 struct st_data *st = st_data[unit];
fca1f689 1646
fde1aeb2 1647 switch ((int)(st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD))) {
fca1f689
RG
1648 default:
1649 *nmarks = 0;
519fb2b7
RG
1650 return (ESUCCESS);
1651 case ST_WRITTEN:
1652 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
fca1f689
RG
1653 *nmarks = 1;
1654 break;
519fb2b7 1655 case ST_WRITTEN | ST_2FM_AT_EOD:
fca1f689
RG
1656 *nmarks = 2;
1657 }
1658 error = st_write_filemarks(unit, *nmarks, flags);
519fb2b7 1659 if (position && (error == ESUCCESS))
fca1f689
RG
1660 error = st_space(unit, -*nmarks, SP_FILEMARKS, flags);
1661 return (error);
1662}
1663
519fb2b7
RG
1664/*
1665 * load/unload (with retension if true)
1666 */
1667errval
1668st_load(unit, type, flags)
1669 u_int32 unit, type, flags;
15637ed4 1670{
519fb2b7
RG
1671 struct scsi_load scsi_cmd;
1672 struct st_data *st = st_data[unit];
1673 struct scsi_link *sc_link = st->sc_link;
15637ed4 1674
15637ed4 1675 bzero(&scsi_cmd, sizeof(scsi_cmd));
519fb2b7
RG
1676 if (type != LD_LOAD) {
1677 errval error;
1678 int32 nmarks;
fca1f689
RG
1679
1680 error = st_chkeod(unit, FALSE, &nmarks, flags);
519fb2b7
RG
1681 if (error != ESUCCESS)
1682 return (error);
1683 sc_link->flags &= ~SDEV_MEDIA_LOADED;
fca1f689 1684 }
519fb2b7
RG
1685 if (st->quirks & ST_Q_IGNORE_LOADS)
1686 return (0);
fca1f689
RG
1687 scsi_cmd.op_code = LOAD_UNLOAD;
1688 scsi_cmd.how |= type;
519fb2b7
RG
1689 return (scsi_scsi_cmd(st->sc_link,
1690 (struct scsi_generic *) &scsi_cmd,
1691 sizeof(scsi_cmd),
1692 0,
1693 0,
1694 ST_RETRIES,
1695 300000, /* 5 min */
1696 NULL,
1697 flags));
15637ed4 1698}
fca1f689 1699
519fb2b7
RG
1700/*
1701 * Rewind the device
1702 */
1703errval
1704st_rewind(unit, immed, flags)
1705 u_int32 unit, flags;
1706 boolean immed;
15637ed4 1707{
519fb2b7
RG
1708 struct scsi_rewind scsi_cmd;
1709 struct st_data *st = st_data[unit];
1710 errval error;
1711 int32 nmarks;
15637ed4 1712
fca1f689 1713 error = st_chkeod(unit, FALSE, &nmarks, flags);
519fb2b7
RG
1714 if (error != ESUCCESS)
1715 return (error);
d4284689 1716 st->flags &= ~ST_PER_ACTION;
15637ed4
RG
1717 bzero(&scsi_cmd, sizeof(scsi_cmd));
1718 scsi_cmd.op_code = REWIND;
d4284689 1719 scsi_cmd.byte2 = immed ? SR_IMMED : 0;
519fb2b7
RG
1720 return (scsi_scsi_cmd(st->sc_link,
1721 (struct scsi_generic *) &scsi_cmd,
1722 sizeof(scsi_cmd),
1723 0,
1724 0,
1725 ST_RETRIES,
1726 immed ? 5000 : 300000, /* 5 sec or 5 min */
1727 NULL,
1728 flags));
d4284689
RG
1729}
1730
519fb2b7
RG
1731#ifdef NETBSD
1732#define SIGNAL_SHORT_READ
d4284689 1733#else
519fb2b7
RG
1734#define SIGNAL_SHORT_READ bp->b_flags |= B_ERROR;
1735#endif
d4284689 1736
519fb2b7
RG
1737/*
1738 * Look at the returned sense and act on the error and detirmine
1739 * The unix error number to pass back... (0 = report no error)
1740 * (-1 = continue processing)
1741 */
1742errval
1743st_interpret_sense(xs)
1744 struct scsi_xfer *xs;
d4284689 1745{
519fb2b7
RG
1746 struct scsi_link *sc_link = xs->sc_link;
1747 struct scsi_sense_data *sense = &(xs->sense);
1748 boolean silent = xs->flags & SCSI_SILENT;
1749 struct buf *bp = xs->bp;
1750 u_int32 unit = sc_link->dev_unit;
1751 struct st_data *st = st_data[unit];
1752 u_int32 key;
1753 int32 info;
1754
1755 /*
1756 * Get the sense fields and work out what code
1757 */
1758 if (sense->error_code & SSD_ERRCODE_VALID) {
1759 info = ntohl(*((int32 *) sense->ext.extended.info));
1760 } else {
1761 info = xs->datalen; /* bad choice if fixed blocks */
d4284689 1762 }
519fb2b7
RG
1763 if ((sense->error_code & SSD_ERRCODE) != 0x70) {
1764 return (-1); /* let the generic code handle it */
d4284689 1765 }
519fb2b7
RG
1766 if (st->flags & ST_FIXEDBLOCKS) {
1767 xs->resid = info * st->blksiz;
1768 if (sense->ext.extended.flags & SSD_EOM) {
1769 st->flags |= ST_EIO_PENDING;
1770 if (bp) {
1771 bp->b_resid = xs->resid;
1772 SIGNAL_SHORT_READ
d4284689 1773 }
15637ed4 1774 }
519fb2b7
RG
1775 if (sense->ext.extended.flags & SSD_FILEMARK) {
1776 st->flags |= ST_AT_FILEMARK;
1777 if (bp) {
1778 bp->b_resid = xs->resid;
1779 SIGNAL_SHORT_READ
1780 }
15637ed4 1781 }
519fb2b7
RG
1782 if (sense->ext.extended.flags & SSD_ILI) {
1783 st->flags |= ST_EIO_PENDING;
1784 if (bp) {
1785 bp->b_resid = xs->resid;
1786 SIGNAL_SHORT_READ
d4284689 1787 }
519fb2b7
RG
1788 if (sense->error_code & SSD_ERRCODE_VALID &&
1789 !silent)
1790 printf("st%d: block wrong size"
1791 ", %d blocks residual\n", unit
1792 ,info);
1793
1794 /*
1795 * This quirk code helps the drive read
1796 * the first tape block, regardless of
1797 * format. That is required for these
1798 * drives to return proper MODE SENSE
1799 * information.
1800 */
1801 if ((st->quirks & ST_Q_SNS_HLP) &&
1802 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
1803 st->blksiz -= 512;
d4284689 1804 }
519fb2b7
RG
1805 }
1806 /*
1807 * If no data was tranfered, do it immediatly
1808 */
1809 if (xs->resid >= xs->datalen) {
1810 if (st->flags & ST_EIO_PENDING) {
1811 return EIO;
d4284689 1812 }
519fb2b7
RG
1813 if (st->flags & ST_AT_FILEMARK) {
1814 if (bp) {
1815 bp->b_resid = xs->resid;
1816 SIGNAL_SHORT_READ
d4284689 1817 }
519fb2b7 1818 return 0;
d4284689 1819 }
15637ed4 1820 }
519fb2b7
RG
1821 } else { /* must be variable mode */
1822 xs->resid = xs->datalen; /* to be sure */
1823 if (sense->ext.extended.flags & SSD_EOM) {
1824 return (EIO);
1825 }
1826 if (sense->ext.extended.flags & SSD_FILEMARK) {
1827 if (bp)
1828 bp->b_resid = bp->b_bcount;
1829 return 0;
1830 }
1831 if (sense->ext.extended.flags & SSD_ILI) {
1832 if (info < 0) {
1833 /*
1834 * the record was bigger than the read
1835 */
1836 if (!silent)
1837 printf("st%d: %d-byte record "
1838 "too big\n", unit,
1839 xs->datalen - info);
1840 return (EIO);
15637ed4 1841 }
519fb2b7
RG
1842 xs->resid = info;
1843 if (bp) {
1844 bp->b_resid = info;
1845 SIGNAL_SHORT_READ
d4284689 1846 }
519fb2b7
RG
1847 }
1848 }
1849 key = sense->ext.extended.flags & SSD_KEY;
1850
1851 if (key == 0x8) {
1852 /*
1853 * This quirk code helps the drive read the
1854 * first tape block, regardless of format. That
1855 * is required for these drives to return proper
1856 * MODE SENSE information.
1857 */
1858 if ((st->quirks & ST_Q_SNS_HLP) &&
1859 !(sc_link->flags & SDEV_MEDIA_LOADED)) { /* still starting */
1860 st->blksiz -= 512;
1861 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
1862 st->flags |= ST_BLANK_READ;
1863 xs->resid = xs->datalen;
1864 if (bp) {
1865 bp->b_resid = xs->resid;
1866 /*return an EOF */
15637ed4 1867 }
519fb2b7
RG
1868 return (ESUCCESS);
1869 }
1870 }
1871 return (-1); /* let the default/generic handler handle it */
1872}
15637ed4 1873
519fb2b7
RG
1874/*
1875 * The quirk here is that the drive returns some value to st_mode_sense
1876 * incorrectly until the tape has actually passed by the head.
1877 *
1878 * The method is to set the drive to large fixed-block state (user-specified
1879 * density and 1024-byte blocks), then read and rewind to get it to sense the
1880 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
1881 * work, as a last resort, try variable- length blocks. The result will be
1882 * the ability to do an accurate st_mode_sense.
1883 *
1884 * We know we can do a rewind because we just did a load, which implies rewind.
1885 * Rewind seems preferable to space backward if we have a virgin tape.
1886 *
1887 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
1888 * error processing, both part of st_interpret_sense.
1889 */
1890errval
1891st_touch_tape(unit)
1892 u_int32 unit;
1893{
1894 struct st_data *st = st_data[unit];
1895 char *buf;
1896 u_int32 readsiz;
1897 errval errno;
0269d546 1898
519fb2b7
RG
1899 buf = malloc(1024, M_TEMP, M_NOWAIT);
1900 if (!buf)
1901 return (ENOMEM);
0269d546 1902
519fb2b7
RG
1903 if (errno = st_mode_sense(unit, 0)) {
1904 goto bad;
1905 }
1906 st->blksiz = 1024;
1907 do {
fde1aeb2 1908 switch ((int)st->blksiz) {
519fb2b7
RG
1909 case 512:
1910 case 1024:
1911 readsiz = st->blksiz;
1912 st->flags |= ST_FIXEDBLOCKS;
1913 break;
0269d546 1914 default:
519fb2b7
RG
1915 readsiz = 1;
1916 st->flags &= ~ST_FIXEDBLOCKS;
1917 } if (errno = st_mode_select(unit, 0)) {
1918 goto bad;
15637ed4 1919 }
519fb2b7
RG
1920 st_read(unit, buf, readsiz, SCSI_SILENT);
1921 if (errno = st_rewind(unit, FALSE, 0)) {
1922bad: free(buf, M_TEMP);
1923 return (errno);
15637ed4 1924 }
519fb2b7
RG
1925 } while (readsiz != 1 && readsiz > st->blksiz);
1926 free(buf, M_TEMP);
1927 return 0;
15637ed4 1928}