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