st.c revision 1.16.2.10 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.10 1994/02/06 08:15:47 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 wakeup((caddr_t)sc_link);
932 return;
933 }
934
935 bp = st->buf_queue.b_actf;
936 if (!bp)
937 return; /* no work to bother with */
938 if (dp = bp->b_actf)
939 dp->b_actb = bp->b_actb;
940 else
941 st->buf_queue.b_actb = bp->b_actb;
942 *bp->b_actb = dp;
943
944 /*
945 * if the device has been unmounted byt the user
946 * then throw away all requests until done
947 */
948 if ((!(st->flags & ST_MOUNTED))
949 || (!(sc_link->flags & SDEV_MEDIA_LOADED))) {
950 /* make sure that one implies the other.. */
951 sc_link->flags &= ~SDEV_MEDIA_LOADED;
952 goto badnews;
953 }
954 /*
955 * only FIXEDBLOCK devices have pending operations
956 */
957 if (st->flags & ST_FIXEDBLOCKS) {
958 /*
959 * If we are at a filemark but have not reported it yet
960 * then we should report it now
961 */
962 if (st->flags & ST_AT_FILEMARK) {
963 if ((bp->b_flags & B_READ) == B_WRITE) {
964 /*
965 * Handling of ST_AT_FILEMARK in
966 * st_space will fill in the right file
967 * mark count.
968 * Back up over filemark
969 */
970 if (st_space(st, 0, SP_FILEMARKS, 0))
971 goto badnews;
972 } else {
973 bp->b_resid = bp->b_bcount;
974 bp->b_error = 0;
975 bp->b_flags &= ~B_ERROR;
976 st->flags &= ~ST_AT_FILEMARK;
977 biodone(bp);
978 continue; /* seek more work */
979 }
980 }
981 /*
982 * If we are at EIO (e.g. EOM) but have not reported it
983 * yet then we should report it now
984 */
985 if (st->flags & ST_EIO_PENDING) {
986 bp->b_resid = bp->b_bcount;
987 bp->b_error = EIO;
988 bp->b_flags |= B_ERROR;
989 st->flags &= ~ST_EIO_PENDING;
990 biodone(bp);
991 continue; /* seek more work */
992 }
993 }
994 /*
995 * Fill out the scsi command
996 */
997 bzero(&cmd, sizeof(cmd));
998 if ((bp->b_flags & B_READ) == B_WRITE) {
999 cmd.op_code = WRITE_COMMAND_TAPE;
1000 st->flags &= ~ST_FM_WRITTEN;
1001 st->flags |= ST_WRITTEN;
1002 flags = SCSI_DATA_OUT;
1003 } else {
1004 cmd.op_code = READ_COMMAND_TAPE;
1005 flags = SCSI_DATA_IN;
1006 }
1007 /*
1008 * Handle "fixed-block-mode" tape drives by using the
1009 * block count instead of the length.
1010 */
1011 if (st->flags & ST_FIXEDBLOCKS) {
1012 cmd.byte2 |= SRWT_FIXED;
1013 lto3b(bp->b_bcount / st->blksiz, cmd.len);
1014 } else {
1015 lto3b(bp->b_bcount, cmd.len);
1016 }
1017 /*
1018 * go ask the adapter to do all this for us
1019 */
1020 if (scsi_scsi_cmd(sc_link, (struct scsi_generic *) &cmd,
1021 sizeof(cmd), (u_char *) bp->b_un.b_addr,
1022 bp->b_bcount, 0, 100000, bp,
1023 flags | SCSI_NOSLEEP)
1024 != SUCCESSFULLY_QUEUED) {
1025 badnews:
1026 printf("%s: not queued\n", st->sc_dev.dv_xname);
1027 bp->b_flags |= B_ERROR;
1028 bp->b_error = EIO;
1029 biodone(bp);
1030 }
1031 } /* go back and see if we can cram more work in.. */
1032 }
1033
1034 /*
1035 * Perform special action on behalf of the user;
1036 * knows about the internals of this device
1037 */
1038 int
1039 stioctl(dev, cmd, arg, flag)
1040 dev_t dev;
1041 int cmd;
1042 caddr_t arg;
1043 int flag;
1044 {
1045 int error = 0;
1046 int unit;
1047 int number, nmarks, dsty;
1048 u_int flags;
1049 struct st_data *st;
1050 u_int hold_blksiz;
1051 u_int hold_density;
1052 struct mtop *mt = (struct mtop *) arg;
1053
1054 /*
1055 * Find the device that the user is talking about
1056 */
1057 flags = 0; /* give error messages, act on errors etc. */
1058 unit = STUNIT(dev);
1059 dsty = STDSTY(dev);
1060 st = stcd.cd_devs[unit];
1061 hold_blksiz = st->blksiz;
1062 hold_density = st->density;
1063
1064 switch (cmd) {
1065
1066 case MTIOCGET: {
1067 struct mtget *g = (struct mtget *) arg;
1068
1069 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: get status]\n"));
1070 bzero(g, sizeof(struct mtget));
1071 g->mt_type = 0x7; /* Ultrix compat *//*? */
1072 g->mt_blksiz = st->blksiz;
1073 g->mt_density = st->density;
1074 g->mt_mblksiz[0] = st->modes[0].blksiz;
1075 g->mt_mblksiz[1] = st->modes[1].blksiz;
1076 g->mt_mblksiz[2] = st->modes[2].blksiz;
1077 g->mt_mblksiz[3] = st->modes[3].blksiz;
1078 g->mt_mdensity[0] = st->modes[0].density;
1079 g->mt_mdensity[1] = st->modes[1].density;
1080 g->mt_mdensity[2] = st->modes[2].density;
1081 g->mt_mdensity[3] = st->modes[3].density;
1082 break;
1083 }
1084 case MTIOCTOP: {
1085
1086 SC_DEBUG(st->sc_link, SDEV_DB1, ("[ioctl: op=0x%x count=0x%x]\n",
1087 mt->mt_op, mt->mt_count));
1088
1089 /* compat: in U*x it is a short */
1090 number = mt->mt_count;
1091 switch ((short) (mt->mt_op)) {
1092 case MTWEOF: /* write an end-of-file record */
1093 error = st_write_filemarks(st, number, flags);
1094 break;
1095 case MTBSF: /* backward space file */
1096 number = -number;
1097 case MTFSF: /* forward space file */
1098 error = st_chkeod(st, FALSE, &nmarks, flags);
1099 if (!error)
1100 error = st_space(st, number - nmarks,
1101 SP_FILEMARKS, flags);
1102 break;
1103 case MTBSR: /* backward space record */
1104 number = -number;
1105 case MTFSR: /* forward space record */
1106 error = st_chkeod(st, TRUE, &nmarks, flags);
1107 if (!error)
1108 error = st_space(st, number, SP_BLKS, flags);
1109 break;
1110 case MTREW: /* rewind */
1111 error = st_rewind(st, FALSE, flags);
1112 break;
1113 case MTOFFL: /* rewind and put the drive offline */
1114 st_unmount(st, EJECT);
1115 break;
1116 case MTNOP: /* no operation, sets status only */
1117 case MTCACHE: /* enable controller cache */
1118 case MTNOCACHE: /* disable controller cache */
1119 break;
1120 case MTSETBSIZ: /* Set block size for device */
1121 #ifdef NOTYET
1122 if (!(st->flags & ST_NEW_MOUNT)) {
1123 uprintf("re-mount tape before changing blocksize");
1124 error = EINVAL;
1125 break;
1126 }
1127 #endif
1128 if (number == 0) {
1129 st->flags &= ~ST_FIXEDBLOCKS;
1130 } else {
1131 if ((st->blkmin || st->blkmax) /* they exist */
1132 &&((number < st->blkmin
1133 || number > st->blkmax))) {
1134 error = EINVAL;
1135 break;
1136 }
1137 st->flags |= ST_FIXEDBLOCKS;
1138 }
1139 st->blksiz = number;
1140 st->flags |= ST_BLOCK_SET; /*XXX */
1141 goto try_new_value;
1142
1143 case MTSETDNSTY: /* Set density for device and mode */
1144 if (number > SCSI_2_MAX_DENSITY_CODE)
1145 error = EINVAL;
1146 else
1147 st->density = number;
1148 goto try_new_value;
1149
1150 default:
1151 error = EINVAL;
1152 }
1153 break;
1154 }
1155 case MTIOCIEOT:
1156 case MTIOCEEOT:
1157 break;
1158 default:
1159 if (STMODE(dev) == CTLMODE)
1160 error = scsi_do_ioctl(st->sc_link,cmd,arg,flag);
1161 else
1162 error = ENOTTY;
1163 break;
1164 }
1165 return error;
1166 /*-----------------------------*/
1167 try_new_value:
1168 /*
1169 * Check that the mode being asked for is aggreeable to the
1170 * drive. If not, put it back the way it was.
1171 */
1172 if (error = st_mode_select(st, 0)) { /* put it back as it was */
1173 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1174 st->density = hold_density;
1175 st->blksiz = hold_blksiz;
1176 if (st->blksiz)
1177 st->flags |= ST_FIXEDBLOCKS;
1178 else
1179 st->flags &= ~ST_FIXEDBLOCKS;
1180 return error;
1181 }
1182 /*
1183 * As the drive liked it, if we are setting a new default,
1184 * set it into the structures as such.
1185 *
1186 * The means for deciding this are not finalised yet
1187 */
1188 if (STMODE(dev) == 0x03) {
1189 /* special mode */
1190 /* XXX */
1191 switch ((short) (mt->mt_op)) {
1192 case MTSETBSIZ:
1193 st->modes[dsty].blksiz = st->blksiz;
1194 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1195 break;
1196 case MTSETDNSTY:
1197 st->modes[dsty].density = st->density;
1198 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1199 break;
1200 }
1201 }
1202 return 0;
1203 }
1204
1205 /*
1206 * Do a synchronous read.
1207 */
1208 int
1209 st_read(st, buf, size, flags)
1210 struct st_data *st;
1211 u_int size;
1212 u_int flags;
1213 char *buf;
1214 {
1215 struct scsi_rw_tape scsi_cmd;
1216
1217 /*
1218 * If it's a null transfer, return immediatly
1219 */
1220 if (size == 0)
1221 return 0;
1222 bzero(&scsi_cmd, sizeof(scsi_cmd));
1223 scsi_cmd.op_code = READ_COMMAND_TAPE;
1224 if (st->flags & ST_FIXEDBLOCKS) {
1225 scsi_cmd.byte2 |= SRWT_FIXED;
1226 lto3b(size / (st->blksiz ? st->blksiz : DEF_FIXED_BSIZE),
1227 scsi_cmd.len);
1228 } else {
1229 lto3b(size, scsi_cmd.len);
1230 }
1231 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1232 sizeof(scsi_cmd), (u_char *) buf, size, 0, 100000,
1233 NULL, flags | SCSI_DATA_IN);
1234 }
1235
1236 #ifdef __STDC__
1237 #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0)
1238 #else
1239 #define b2tol(a) (((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0)
1240 #endif
1241
1242 /*
1243 * Ask the drive what it's min and max blk sizes are.
1244 */
1245 int
1246 st_rd_blk_lim(st, flags)
1247 struct st_data *st;
1248 u_int flags;
1249 {
1250 struct scsi_blk_limits scsi_cmd;
1251 struct scsi_blk_limits_data scsi_blkl;
1252 int error;
1253 struct scsi_link *sc_link = st->sc_link;
1254
1255 /*
1256 * First check if we have it all loaded
1257 */
1258 if ((sc_link->flags & SDEV_MEDIA_LOADED))
1259 return 0;
1260
1261 /*
1262 * do a 'Read Block Limits'
1263 */
1264 bzero(&scsi_cmd, sizeof(scsi_cmd));
1265 scsi_cmd.op_code = READ_BLK_LIMITS;
1266
1267 /*
1268 * do the command, update the global values
1269 */
1270 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
1271 sizeof(scsi_cmd), (u_char *) & scsi_blkl,
1272 sizeof(scsi_blkl), ST_RETRIES, 5000, NULL,
1273 flags | SCSI_DATA_IN))
1274 return error;
1275
1276 st->blkmin = b2tol(scsi_blkl.min_length);
1277 st->blkmax = _3btol(&scsi_blkl.max_length_2);
1278
1279 SC_DEBUG(sc_link, SDEV_DB3,
1280 ("(%d <= blksiz <= %d)\n", st->blkmin, st->blkmax));
1281 return 0;
1282 }
1283
1284 /*
1285 * Get the scsi driver to send a full inquiry to the
1286 * device and use the results to fill out the global
1287 * parameter structure.
1288 *
1289 * called from:
1290 * attach
1291 * open
1292 * ioctl (to reset original blksize)
1293 */
1294 int
1295 st_mode_sense(st, flags)
1296 struct st_data *st;
1297 u_int flags;
1298 {
1299 u_int scsi_sense_len;
1300 int error;
1301 char *scsi_sense_ptr;
1302 struct scsi_mode_sense scsi_cmd;
1303 struct scsi_sense {
1304 struct scsi_mode_header header;
1305 struct blk_desc blk_desc;
1306 } scsi_sense;
1307
1308 struct scsi_sense_page_0 {
1309 struct scsi_mode_header header;
1310 struct blk_desc blk_desc;
1311 unsigned char sense_data[PAGE_0_SENSE_DATA_SIZE];
1312 /* Tandberg tape drives returns page 00
1313 * with the sense data, whether or not
1314 * you want it (ie the don't like you
1315 * saying you want anything less!!!!!)
1316 * They also expect page 00
1317 * back when you issue a mode select
1318 */
1319 } scsi_sense_page_0;
1320 struct scsi_link *sc_link = st->sc_link;
1321
1322 /*
1323 * Define what sort of structure we're working with
1324 */
1325 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1326 scsi_sense_len = sizeof(scsi_sense_page_0);
1327 scsi_sense_ptr = (char *) &scsi_sense_page_0;
1328 } else {
1329 scsi_sense_len = sizeof(scsi_sense);
1330 scsi_sense_ptr = (char *) &scsi_sense;
1331 }
1332 /*
1333 * Set up a mode sense
1334 */
1335 bzero(&scsi_cmd, sizeof(scsi_cmd));
1336 scsi_cmd.op_code = MODE_SENSE;
1337 scsi_cmd.length = scsi_sense_len;
1338
1339 /*
1340 * do the command, but we don't need the results
1341 * just print them for our interest's sake, if asked,
1342 * or if we need it as a template for the mode select
1343 * store it away.
1344 */
1345 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
1346 sizeof(scsi_cmd), (u_char *) scsi_sense_ptr,
1347 scsi_sense_len, ST_RETRIES, 5000, NULL,
1348 flags | SCSI_DATA_IN))
1349 return error;
1350
1351 st->numblks = _3btol(((struct scsi_sense *)scsi_sense_ptr)->blk_desc.nblocks);
1352 st->media_blksiz = _3btol(((struct scsi_sense *)scsi_sense_ptr)->blk_desc.blklen);
1353 st->media_density = ((struct scsi_sense *) scsi_sense_ptr)->blk_desc.density;
1354 if (((struct scsi_sense *) scsi_sense_ptr)->header.dev_spec &
1355 SMH_DSP_WRITE_PROT) {
1356 st->flags |= ST_READONLY;
1357 }
1358 SC_DEBUG(sc_link, SDEV_DB3,
1359 ("density code 0x%x, %d-byte blocks, write-%s, ",
1360 st->media_density, st->media_blksiz,
1361 st->flags & ST_READONLY ? "protected" : "enabled"));
1362 SC_DEBUG(sc_link, SDEV_DB3,
1363 ("%sbuffered\n",
1364 ((struct scsi_sense *) scsi_sense_ptr)->header.dev_spec
1365 & SMH_DSP_BUFF_MODE ? "" : "un"));
1366 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1367 bcopy(((struct scsi_sense_page_0 *) scsi_sense_ptr)->sense_data,
1368 st->sense_data,
1369 sizeof(((struct scsi_sense_page_0 *) scsi_sense_ptr)->sense_data));
1370 }
1371 sc_link->flags |= SDEV_MEDIA_LOADED;
1372 return 0;
1373 }
1374
1375 /*
1376 * Send a filled out parameter structure to the drive to
1377 * set it into the desire modes etc.
1378 */
1379 int
1380 st_mode_select(st, flags)
1381 struct st_data *st;
1382 u_int flags;
1383 {
1384 u_int dat_len;
1385 char *dat_ptr;
1386 struct scsi_mode_select scsi_cmd;
1387 struct dat {
1388 struct scsi_mode_header header;
1389 struct blk_desc blk_desc;
1390 } dat;
1391 struct dat_page_0 {
1392 struct scsi_mode_header header;
1393 struct blk_desc blk_desc;
1394 unsigned char sense_data[PAGE_0_SENSE_DATA_SIZE];
1395 } dat_page_0;
1396
1397 /*
1398 * Define what sort of structure we're working with
1399 */
1400 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1401 dat_len = sizeof(dat_page_0);
1402 dat_ptr = (char *) &dat_page_0;
1403 } else {
1404 dat_len = sizeof(dat);
1405 dat_ptr = (char *) &dat;
1406 }
1407 /*
1408 * Set up for a mode select
1409 */
1410 bzero(dat_ptr, dat_len);
1411 bzero(&scsi_cmd, sizeof(scsi_cmd));
1412 scsi_cmd.op_code = MODE_SELECT;
1413 scsi_cmd.length = dat_len;
1414 ((struct dat *) dat_ptr)->header.blk_desc_len = sizeof(struct blk_desc);
1415 ((struct dat *) dat_ptr)->header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
1416 ((struct dat *) dat_ptr)->blk_desc.density = st->density;
1417 if (st->flags & ST_FIXEDBLOCKS)
1418 lto3b(st->blksiz, ((struct dat *) dat_ptr)->blk_desc.blklen);
1419 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1420 bcopy(st->sense_data, ((struct dat_page_0 *) dat_ptr)->sense_data,
1421 sizeof(((struct dat_page_0 *) dat_ptr)->sense_data));
1422 /* the Tandberg tapes need the block size to */
1423 /* be set on each mode sense/select. */
1424 }
1425 /*
1426 * do the command
1427 */
1428 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1429 sizeof(scsi_cmd), (u_char *) dat_ptr, dat_len,
1430 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_OUT);
1431 }
1432
1433 /*
1434 * skip N blocks/filemarks/seq filemarks/eom
1435 */
1436 int
1437 st_space(st, number, what, flags)
1438 struct st_data *st;
1439 u_int what;
1440 u_int flags;
1441 int number;
1442 {
1443 int error;
1444 struct scsi_space scsi_cmd;
1445
1446 switch (what) {
1447 case SP_BLKS:
1448 if (st->flags & ST_PER_ACTION) {
1449 if (number > 0) {
1450 st->flags &= ~ST_PER_ACTION;
1451 return EIO;
1452 } else if (number < 0) {
1453 if (st->flags & ST_AT_FILEMARK) {
1454 /*
1455 * Handling of ST_AT_FILEMARK
1456 * in st_space will fill in the
1457 * right file mark count.
1458 */
1459 error = st_space(st, 0, SP_FILEMARKS,
1460 flags);
1461 if (error)
1462 return error;
1463 }
1464 if (st->flags & ST_BLANK_READ) {
1465 st->flags &= ~ST_BLANK_READ;
1466 return EIO;
1467 }
1468 st->flags &= ~ST_EIO_PENDING;
1469 }
1470 }
1471 break;
1472 case SP_FILEMARKS:
1473 if (st->flags & ST_EIO_PENDING) {
1474 if (number > 0) { /* pretend we just discover the error */
1475 st->flags &= ~ST_EIO_PENDING;
1476 return EIO;
1477 } else if (number < 0) { /* back away from the error */
1478 st->flags &= ~ST_EIO_PENDING;
1479 }
1480 }
1481 if (st->flags & ST_AT_FILEMARK) {
1482 st->flags &= ~ST_AT_FILEMARK;
1483 number--;
1484 }
1485 if ((st->flags & ST_BLANK_READ) && (number < 0)) { /* back away from unwritten tape */
1486 st->flags &= ~ST_BLANK_READ;
1487 number++; /* dubious */
1488 }
1489 }
1490 if (number == 0)
1491 return 0;
1492 bzero(&scsi_cmd, sizeof(scsi_cmd));
1493 scsi_cmd.op_code = SPACE;
1494 scsi_cmd.byte2 = what & SS_CODE;
1495 lto3b(number, scsi_cmd.number);
1496 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1497 sizeof(scsi_cmd), 0, 0, 0, 600000, NULL, flags);
1498 }
1499
1500 /*
1501 * write N filemarks
1502 */
1503 int
1504 st_write_filemarks(st, number, flags)
1505 struct st_data *st;
1506 u_int flags;
1507 int number;
1508 {
1509 struct scsi_write_filemarks scsi_cmd;
1510
1511 /*
1512 * It's hard to write a negative number of file marks.
1513 * Don't try.
1514 */
1515 if (number < 0)
1516 return EINVAL;
1517 switch (number) {
1518 case 0: /* really a command to sync the drive's buffers */
1519 break;
1520 case 1:
1521 if (st->flags & ST_FM_WRITTEN) /* already have one down */
1522 st->flags &= ~ST_WRITTEN;
1523 else
1524 st->flags |= ST_FM_WRITTEN;
1525 st->flags &= ~ST_PER_ACTION;
1526 break;
1527 default:
1528 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1529 }
1530 bzero(&scsi_cmd, sizeof(scsi_cmd));
1531 scsi_cmd.op_code = WRITE_FILEMARKS;
1532 lto3b(number, scsi_cmd.number);
1533 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1534 sizeof(scsi_cmd), 0, 0, 0, 100000, NULL, flags);
1535 }
1536
1537 /*
1538 * Make sure the right number of file marks is on tape if the
1539 * tape has been written. If the position argument is true,
1540 * leave the tape positioned where it was originally.
1541 *
1542 * nmarks returns the number of marks to skip (or, if position
1543 * true, which were skipped) to get back original position.
1544 */
1545 int
1546 st_chkeod(st, position, nmarks, flags)
1547 struct st_data *st;
1548 boolean position;
1549 int *nmarks;
1550 u_int flags;
1551 {
1552 int error;
1553
1554 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1555 default:
1556 *nmarks = 0;
1557 return 0;
1558 case ST_WRITTEN:
1559 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1560 *nmarks = 1;
1561 break;
1562 case ST_WRITTEN | ST_2FM_AT_EOD:
1563 *nmarks = 2;
1564 }
1565 error = st_write_filemarks(st, *nmarks, flags);
1566 if (position && !error)
1567 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1568 return error;
1569 }
1570
1571 /*
1572 * load/unload (with retension if true)
1573 */
1574 int
1575 st_load(st, type, flags)
1576 struct st_data *st;
1577 u_int type;
1578 u_int flags;
1579 {
1580 struct scsi_load scsi_cmd;
1581 struct scsi_link *sc_link = st->sc_link;
1582
1583 bzero(&scsi_cmd, sizeof(scsi_cmd));
1584 if (type != LD_LOAD) {
1585 int error;
1586 int nmarks;
1587
1588 error = st_chkeod(st, FALSE, &nmarks, flags);
1589 if (error)
1590 return error;
1591 sc_link->flags &= ~SDEV_MEDIA_LOADED;
1592 }
1593 if (st->quirks & ST_Q_IGNORE_LOADS)
1594 return 0;
1595 scsi_cmd.op_code = LOAD_UNLOAD;
1596 scsi_cmd.how |= type;
1597 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1598 sizeof(scsi_cmd), 0, 0, ST_RETRIES, 300000, NULL,
1599 flags);
1600 }
1601
1602 /*
1603 * Rewind the device
1604 */
1605 int
1606 st_rewind(st, immed, flags)
1607 struct st_data *st;
1608 u_int flags;
1609 boolean immed;
1610 {
1611 struct scsi_rewind scsi_cmd;
1612 int error;
1613 int nmarks;
1614
1615 error = st_chkeod(st, FALSE, &nmarks, flags);
1616 if (error)
1617 return error;
1618 st->flags &= ~ST_PER_ACTION;
1619 bzero(&scsi_cmd, sizeof(scsi_cmd));
1620 scsi_cmd.op_code = REWIND;
1621 scsi_cmd.byte2 = immed ? SR_IMMED : 0;
1622 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1623 sizeof(scsi_cmd), 0, 0, ST_RETRIES,
1624 immed ? 5000 : 300000, NULL, flags);
1625 }
1626
1627 #ifdef __NetBSD__
1628 #define SIGNAL_SHORT_READ
1629 #else
1630 #define SIGNAL_SHORT_READ bp->b_flags |= B_ERROR;
1631 #endif
1632
1633 /*
1634 * Look at the returned sense and act on the error and detirmine
1635 * The unix error number to pass back... (0 = report no error)
1636 * (-1 = continue processing)
1637 */
1638 int
1639 st_interpret_sense(xs)
1640 struct scsi_xfer *xs;
1641 {
1642 struct scsi_link *sc_link = xs->sc_link;
1643 struct scsi_sense_data *sense = &(xs->sense);
1644 boolean silent = xs->flags & SCSI_SILENT;
1645 struct buf *bp = xs->bp;
1646 int unit = sc_link->dev_unit;
1647 struct st_data *st = stcd.cd_devs[unit];
1648 u_int key;
1649 int info;
1650
1651 /*
1652 * Get the sense fields and work out what code
1653 */
1654 if (sense->error_code & SSD_ERRCODE_VALID)
1655 info = ntohl(*((int *) sense->ext.extended.info));
1656 else
1657 info = xs->datalen; /* bad choice if fixed blocks */
1658 if ((sense->error_code & SSD_ERRCODE) != 0x70)
1659 return -1; /* let the generic code handle it */
1660 if (st->flags & ST_FIXEDBLOCKS) {
1661 xs->resid = info * st->blksiz;
1662 if (sense->ext.extended.flags & SSD_EOM) {
1663 st->flags |= ST_EIO_PENDING;
1664 if (bp) {
1665 bp->b_resid = xs->resid;
1666 SIGNAL_SHORT_READ
1667 }
1668 }
1669 if (sense->ext.extended.flags & SSD_FILEMARK) {
1670 st->flags |= ST_AT_FILEMARK;
1671 if (bp) {
1672 bp->b_resid = xs->resid;
1673 SIGNAL_SHORT_READ
1674 }
1675 }
1676 if (sense->ext.extended.flags & SSD_ILI) {
1677 st->flags |= ST_EIO_PENDING;
1678 if (bp) {
1679 bp->b_resid = xs->resid;
1680 SIGNAL_SHORT_READ
1681 }
1682 if (sense->error_code & SSD_ERRCODE_VALID &&
1683 !silent)
1684 printf("%s: block wrong size"
1685 ", %d blocks residual\n",
1686 st->sc_dev.dv_xname, info);
1687
1688 /*
1689 * This quirk code helps the drive read
1690 * the first tape block, regardless of
1691 * format. That is required for these
1692 * drives to return proper MODE SENSE
1693 * information.
1694 */
1695 if ((st->quirks & ST_Q_SNS_HLP) &&
1696 !(sc_link->flags & SDEV_MEDIA_LOADED))
1697 st->blksiz -= 512;
1698 }
1699 /*
1700 * If no data was tranfered, do it immediatly
1701 */
1702 if (xs->resid >= xs->datalen) {
1703 if (st->flags & ST_EIO_PENDING)
1704 return EIO;
1705 if (st->flags & ST_AT_FILEMARK) {
1706 if (bp) {
1707 bp->b_resid = xs->resid;
1708 SIGNAL_SHORT_READ
1709 }
1710 return 0;
1711 }
1712 }
1713 } else { /* must be variable mode */
1714 xs->resid = xs->datalen; /* to be sure */
1715 if (sense->ext.extended.flags & SSD_EOM)
1716 return EIO;
1717 if (sense->ext.extended.flags & SSD_FILEMARK) {
1718 if (bp)
1719 bp->b_resid = bp->b_bcount;
1720 return 0;
1721 }
1722 if (sense->ext.extended.flags & SSD_ILI) {
1723 if (info < 0) {
1724 /*
1725 * the record was bigger than the read
1726 */
1727 if (!silent)
1728 printf("%s: %d-byte record too big\n",
1729 st->sc_dev.dv_xname,
1730 xs->datalen - info);
1731 return EIO;
1732 }
1733 xs->resid = info;
1734 if (bp) {
1735 bp->b_resid = info;
1736 SIGNAL_SHORT_READ
1737 }
1738 }
1739 }
1740 key = sense->ext.extended.flags & SSD_KEY;
1741
1742 if (key == 0x8) {
1743 /*
1744 * This quirk code helps the drive read the
1745 * first tape block, regardless of format. That
1746 * is required for these drives to return proper
1747 * MODE SENSE information.
1748 */
1749 if ((st->quirks & ST_Q_SNS_HLP) &&
1750 !(sc_link->flags & SDEV_MEDIA_LOADED)) { /* still starting */
1751 st->blksiz -= 512;
1752 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
1753 st->flags |= ST_BLANK_READ;
1754 xs->resid = xs->datalen;
1755 if (bp) {
1756 bp->b_resid = xs->resid;
1757 /* return an EOF */
1758 }
1759 return 0;
1760 }
1761 }
1762 return -1; /* let the default/generic handler handle it */
1763 }
1764
1765 /*
1766 * The quirk here is that the drive returns some value to st_mode_sense
1767 * incorrectly until the tape has actually passed by the head.
1768 *
1769 * The method is to set the drive to large fixed-block state (user-specified
1770 * density and 1024-byte blocks), then read and rewind to get it to sense the
1771 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
1772 * work, as a last resort, try variable- length blocks. The result will be
1773 * the ability to do an accurate st_mode_sense.
1774 *
1775 * We know we can do a rewind because we just did a load, which implies rewind.
1776 * Rewind seems preferable to space backward if we have a virgin tape.
1777 *
1778 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
1779 * error processing, both part of st_interpret_sense.
1780 */
1781 int
1782 st_touch_tape(st)
1783 struct st_data *st;
1784 {
1785 char *buf;
1786 u_int readsiz;
1787 int error;
1788
1789 buf = malloc(1024, M_TEMP, M_NOWAIT);
1790 if (!buf)
1791 return ENOMEM;
1792
1793 if (error = st_mode_sense(st, 0))
1794 goto bad;
1795 st->blksiz = 1024;
1796 do {
1797 switch (st->blksiz) {
1798 case 512:
1799 case 1024:
1800 readsiz = st->blksiz;
1801 st->flags |= ST_FIXEDBLOCKS;
1802 break;
1803 default:
1804 readsiz = 1;
1805 st->flags &= ~ST_FIXEDBLOCKS;
1806 }
1807 if (error = st_mode_select(st, 0))
1808 goto bad;
1809 st_read(st, buf, readsiz, SCSI_SILENT);
1810 if (error = st_rewind(st, FALSE, 0)) {
1811 bad: free(buf, M_TEMP);
1812 return error;
1813 }
1814 } while (readsiz != 1 && readsiz > st->blksiz);
1815 free(buf, M_TEMP);
1816 return 0;
1817 }
1818