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