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