st.c revision 1.26 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.26 1994/04/10 00:07:02 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_SNS_HLP 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_SNS_HLP, 0}, /* minor 0-3 */
140 {0, ST_Q_SNS_HLP, 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, 0x13}, /* minor 4-7 */
181 {1024, ST_Q_FORCE_FIXED_MODE, 0x13}, /* minor 8-11 */
182 {0, ST_Q_FORCE_VAR_MODE, 0x13} /* minor 12-15 */
183 }
184 },
185 {(char *) 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 u_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, u_int flags));
235 int st_rewind __P((struct st_data *, boolean immed, u_int flags));
236 int st_mode_sense __P((struct st_data *, u_int flags));
237 int st_decide_mode __P((struct st_data *, boolean first_read));
238 int st_rd_blk_lim __P((struct st_data *, u_int flags));
239 int st_touch_tape __P((struct st_data *));
240 int st_write_filemarks __P((struct st_data *, int number, u_int flags));
241 int st_load __P((struct st_data *, u_int type, u_int flags));
242 int st_mode_select __P((struct st_data *, u_int flags));
243 void ststrategy();
244 void stminphys();
245 int st_chkeod();
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_INITIALIZED 0x01
263 #define ST_INFO_VALID 0x02
264 #define ST_OPEN 0x04
265 #define ST_BLOCK_SET 0x08 /* block size, mode set by ioctl */
266 #define ST_WRITTEN 0x10 /* data have been written, EOD needed */
267 #define ST_FIXEDBLOCKS 0x20
268 #define ST_AT_FILEMARK 0x40
269 #define ST_EIO_PENDING 0x80 /* we couldn't report it then (had data) */
270 #define ST_NEW_MOUNT 0x100 /* still need to decide mode */
271 #define ST_READONLY 0x200 /* st_mode_sense says write protected */
272 #define ST_FM_WRITTEN 0x400 /*
273 * EOF file mark written -- used with
274 * ~ST_WRITTEN to indicate that multiple file
275 * marks have been written
276 */
277 #define ST_BLANK_READ 0x800 /* BLANK CHECK encountered already */
278 #define ST_2FM_AT_EOD 0x1000 /* write 2 file marks at EOD */
279 #define ST_MOUNTED 0x2000 /* Device is presently mounted */
280
281 #define ST_PER_ACTION (ST_AT_FILEMARK | ST_EIO_PENDING | ST_BLANK_READ)
282 #define ST_PER_MOUNT (ST_INFO_VALID | ST_BLOCK_SET | ST_WRITTEN | \
283 ST_FIXEDBLOCKS | ST_READONLY | \
284 ST_FM_WRITTEN | ST_2FM_AT_EOD | ST_PER_ACTION)
285
286 /*
287 * The routine called by the low level scsi routine when it discovers
288 * A device suitable for this driver
289 */
290 void
291 stattach(parent, self, aux)
292 struct device *parent, *self;
293 void *aux;
294 {
295 struct st_data *st = (void *)self;
296 struct scsi_link *sc_link = aux;
297
298 SC_DEBUG(sc_link, SDEV_DB2, ("stattach: "));
299
300 sc_link->device = &st_switch;
301 sc_link->dev_unit = st->sc_dev.dv_unit;
302
303 /*
304 * Store information needed to contact our base driver
305 */
306 st->sc_link = sc_link;
307
308 /*
309 * Check if the drive is a known criminal and take
310 * Any steps needed to bring it into line
311 */
312 st_identify_drive(st);
313
314 /*
315 * Use the subdriver to request information regarding
316 * the drive. We cannot use interrupts yet, so the
317 * request must specify this.
318 */
319 printf(": %s", st->rogues ? "rogue, " : "");
320 if (st_mode_sense(st, SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT))
321 printf("drive offline\n");
322 else if (scsi_test_unit_ready(sc_link,
323 SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT))
324 printf("drive empty\n");
325 else {
326 printf("density code 0x%x, ", st->media_density);
327 if (st->media_blksiz)
328 printf("%d-byte", st->media_blksiz);
329 else
330 printf("variable");
331 printf(" blocks, write-%s\n",
332 (st->flags & ST_READONLY) ? "protected" : "enabled");
333 }
334
335 /*
336 * Set up the buf queue for this device
337 */
338 st->buf_queue.b_active = 0;
339 st->buf_queue.b_actf = 0;
340 st->buf_queue.b_actb = &st->buf_queue.b_actf;
341 st->flags |= ST_INITIALIZED;
342 }
343
344 /*
345 * Use the inquiry routine in 'scsi_base' to get drive info so we can
346 * Further tailor our behaviour.
347 */
348 void
349 st_identify_drive(st)
350 struct st_data *st;
351 {
352 struct scsi_inquiry_data inqbuf;
353 struct rogues *finger;
354 char manu[32];
355 char model[32];
356 char model2[32];
357 char version[32];
358 u_int model_len;
359
360 /*
361 * Get the device type information
362 */
363 if (scsi_inquire(st->sc_link, &inqbuf,
364 SCSI_NOSLEEP | SCSI_NOMASK | SCSI_SILENT) != 0) {
365 printf("%s: couldn't get device type, using default\n",
366 st->sc_dev.dv_xname);
367 return;
368 }
369 if ((inqbuf.version & SID_ANSII) == 0) {
370 /*
371 * If not advanced enough, use default values
372 */
373 strncpy(manu, "pre-scsi", 8);
374 manu[8] = 0;
375 strncpy(model, " unknown model ", 16);
376 model[16] = 0;
377 strncpy(version, "????", 4);
378 version[4] = 0;
379 } else {
380 strncpy(manu, inqbuf.vendor, 8);
381 manu[8] = 0;
382 strncpy(model, inqbuf.product, 16);
383 model[16] = 0;
384 strncpy(version, inqbuf.revision, 4);
385 version[4] = 0;
386 }
387
388 /*
389 * Load the parameters for this kind of device, so we
390 * treat it as appropriate for each operating mode.
391 * Only check the number of characters in the array's
392 * model entry, not the entire model string returned.
393 */
394 finger = gallery;
395 while (finger->manu) {
396 model_len = 0;
397 while (finger->model[model_len] && (model_len < 32)) {
398 model2[model_len] = model[model_len];
399 model_len++;
400 }
401 model2[model_len] = 0;
402 if ((strcmp(manu, finger->manu) == 0) &&
403 (strcmp(model2, finger->model) == 0 ||
404 strcmp("????????????????", finger->model) == 0) &&
405 (strcmp(version, finger->version) == 0 ||
406 strcmp("????", finger->version) == 0)) {
407 st->rogues = finger;
408 st->drive_quirks = finger->quirks;
409 st->quirks = finger->quirks; /* start value */
410 st_loadquirks(st);
411 break;
412 } else
413 finger++; /* go to next suspect */
414 }
415 }
416
417 /*
418 * initialise the subdevices to the default (QUIRK) state.
419 * this will remove any setting made by the system operator or previous
420 * operations.
421 */
422 void
423 st_loadquirks(st)
424 struct st_data *st;
425 {
426 int i;
427 struct modes *mode;
428 struct modes *mode2;
429
430 if (!st->rogues)
431 return;
432 mode = st->rogues->modes;
433 mode2 = st->modes;
434 for (i = 0; i < 4; i++) {
435 bzero(mode2, sizeof(struct modes));
436 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
437 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
438 DENSITY_SET_BY_USER);
439 if (mode->blksiz && ((mode->quirks | st->drive_quirks) &
440 ST_Q_FORCE_FIXED_MODE)) {
441 mode2->blksiz = mode->blksiz;
442 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
443 } else if ((mode->quirks | st->drive_quirks) &
444 ST_Q_FORCE_VAR_MODE) {
445 mode2->blksiz = 0;
446 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
447 }
448 if (mode->density) {
449 mode2->density = mode->density;
450 st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
451 }
452 mode++;
453 mode2++;
454 }
455 }
456
457 /*
458 * open the device.
459 */
460 int
461 stopen(dev, flags)
462 dev_t dev;
463 u_int flags;
464 {
465 int unit;
466 u_int mode, dsty;
467 int error = 0;
468 struct st_data *st;
469 struct scsi_link *sc_link;
470
471 unit = STUNIT(dev);
472 mode = STMODE(dev);
473 dsty = STDSTY(dev);
474
475 if (unit >= stcd.cd_ndevs)
476 return ENXIO;
477 st = stcd.cd_devs[unit];
478 if (!st || !(st->flags & ST_INITIALIZED))
479 return ENXIO;
480
481 sc_link = st->sc_link;
482 SC_DEBUG(sc_link, SDEV_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
483 unit, stcd.cd_ndevs));
484
485 if (st->flags & ST_OPEN)
486 return EBUSY;
487
488 /*
489 * Throw out a dummy instruction to catch 'Unit attention
490 * errors (the error handling will invalidate all our
491 * device info if we get one, but otherwise, ignore it)
492 */
493 scsi_test_unit_ready(sc_link, SCSI_SILENT);
494
495 sc_link->flags |= SDEV_OPEN; /* unit attn are now errors */
496 /*
497 * If the mode is 3 (e.g. minor = 3,7,11,15)
498 * then the device has been openned to set defaults
499 * This mode does NOT ALLOW I/O, only ioctls
500 */
501 if (mode == CTLMODE)
502 return 0;
503
504 /*
505 * Check that the device is ready to use (media loaded?)
506 * This time take notice of the return result
507 */
508 if (error = (scsi_test_unit_ready(sc_link, 0))) {
509 printf("%s: not ready\n", st->sc_dev.dv_xname);
510 st_unmount(st, NOEJECT);
511 return error;
512 }
513
514 /*
515 * if it's a different mode, or if the media has been
516 * invalidated, unmount the tape from the previous
517 * session but continue with open processing
518 */
519 if (st->last_dsty != dsty || !(sc_link->flags & SDEV_MEDIA_LOADED))
520 st_unmount(st, NOEJECT);
521
522 /*
523 * If we are not mounted, then we should start a new
524 * mount session.
525 */
526 if (!(st->flags & ST_MOUNTED)) {
527 st_mount_tape(dev, flags);
528 st->last_dsty = dsty;
529 }
530
531 /*
532 * Make sure that a tape opened in write-only mode will have
533 * file marks written on it when closed, even if not written to.
534 * This is for SUN compatibility
535 */
536 if ((flags & O_ACCMODE) == FWRITE)
537 st->flags |= ST_WRITTEN;
538
539 SC_DEBUG(sc_link, SDEV_DB2, ("Open complete\n"));
540
541 st->flags |= ST_OPEN;
542 return 0;
543 }
544
545 /*
546 * close the device.. only called if we are the LAST
547 * occurence of an open device
548 */
549 int
550 stclose(dev)
551 dev_t dev;
552 {
553 int unit, mode;
554 struct st_data *st;
555 struct scsi_link *sc_link;
556
557 unit = STUNIT(dev);
558 mode = STMODE(dev);
559 st = stcd.cd_devs[unit];
560 sc_link = st->sc_link;
561
562 SC_DEBUG(sc_link, SDEV_DB1, ("closing\n"));
563 if ((st->flags & (ST_WRITTEN | ST_FM_WRITTEN)) == ST_WRITTEN)
564 st_write_filemarks(st, 1, 0);
565 switch (mode & 0x3) {
566 case 0:
567 case 3: /* for now */
568 st_unmount(st, NOEJECT);
569 break;
570 case 1:
571 /* leave mounted unless media seems to have been removed */
572 if (!(sc_link->flags & SDEV_MEDIA_LOADED))
573 st_unmount(st, NOEJECT);
574 break;
575 case 2:
576 st_unmount(st, EJECT);
577 break;
578 }
579 sc_link->flags &= ~SDEV_OPEN;
580 st->flags &= ~ST_OPEN;
581 return 0;
582 }
583
584 /*
585 * Start a new mount session.
586 * Copy in all the default parameters from the selected device mode.
587 * and try guess any that seem to be defaulted.
588 */
589 int
590 st_mount_tape(dev, flags)
591 dev_t dev;
592 u_int flags;
593 {
594 int unit;
595 u_int mode, dsty;
596 struct st_data *st;
597 struct scsi_link *sc_link;
598 int error = 0;
599
600 unit = STUNIT(dev);
601 mode = STMODE(dev);
602 dsty = STDSTY(dev);
603 st = stcd.cd_devs[unit];
604 sc_link = st->sc_link;
605
606 if (st->flags & ST_MOUNTED)
607 return 0;
608
609 SC_DEBUG(sc_link, SDEV_DB1, ("mounting\n "));
610 st->flags |= ST_NEW_MOUNT;
611 st->quirks = st->drive_quirks | st->modes[dsty].quirks;
612 /*
613 * If the media is new, then make sure we give it a chance to
614 * to do a 'load' instruction. (We assume it is new.)
615 */
616 if (error = st_load(st, LD_LOAD, 0))
617 return error;
618 /*
619 * Throw another dummy instruction to catch
620 * 'Unit attention' errors. Some drives appear to give
621 * these after doing a Load instruction.
622 * (noteably some DAT drives)
623 */
624 scsi_test_unit_ready(sc_link, SCSI_SILENT);
625
626 /*
627 * Some devices can't tell you much until they have been
628 * asked to look at the media. This quirk does this.
629 */
630 if (st->quirks & ST_Q_SNS_HLP)
631 if (error = st_touch_tape(st))
632 return error;
633 /*
634 * Load the physical device parameters
635 * loads: blkmin, blkmax
636 */
637 if (error = st_rd_blk_lim(st, 0))
638 return error;
639 /*
640 * Load the media dependent parameters
641 * includes: media_blksiz,media_density,numblks
642 * As we have a tape in, it should be reflected here.
643 * If not you may need the "quirk" above.
644 */
645 if (error = st_mode_sense(st, 0))
646 return error;
647 /*
648 * If we have gained a permanent density from somewhere,
649 * then use it in preference to the one supplied by
650 * default by the driver.
651 */
652 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
653 st->density = st->modes[dsty].density;
654 else
655 st->density = st->media_density;
656 /*
657 * If we have gained a permanent blocksize
658 * then use it in preference to the one supplied by
659 * default by the driver.
660 */
661 st->flags &= ~ST_FIXEDBLOCKS;
662 if (st->modeflags[dsty] & (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
663 st->blksiz = st->modes[dsty].blksiz;
664 if (st->blksiz)
665 st->flags |= ST_FIXEDBLOCKS;
666 } else {
667 if (error = st_decide_mode(st, FALSE))
668 return error;
669 }
670 if (error = st_mode_select(st, 0)) {
671 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
672 return error;
673 }
674 scsi_prevent(sc_link, PR_PREVENT, 0); /* who cares if it fails? */
675 st->flags &= ~ST_NEW_MOUNT;
676 st->flags |= ST_MOUNTED;
677 sc_link->flags |= SDEV_MEDIA_LOADED; /* move earlier? */
678
679 return 0;
680 }
681
682 /*
683 * End the present mount session.
684 * Rewind, and optionally eject the tape.
685 * Reset various flags to indicate that all new
686 * operations require another mount operation
687 */
688 void
689 st_unmount(st, eject)
690 struct st_data *st;
691 boolean eject;
692 {
693 struct scsi_link *sc_link = st->sc_link;
694 int nmarks;
695
696 if (!(st->flags & ST_MOUNTED))
697 return;
698 SC_DEBUG(sc_link, SDEV_DB1, ("unmounting\n"));
699 st_chkeod(st, FALSE, &nmarks, SCSI_SILENT);
700 st_rewind(st, FALSE, SCSI_SILENT);
701 scsi_prevent(sc_link, PR_ALLOW, SCSI_SILENT);
702 if (eject)
703 st_load(st, LD_UNLOAD, SCSI_SILENT);
704 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
705 sc_link->flags &= ~SDEV_MEDIA_LOADED;
706 }
707
708 /*
709 * Given all we know about the device, media, mode, 'quirks' and
710 * initial operation, make a decision as to how we should be set
711 * to run (regarding blocking and EOD marks)
712 */
713 int
714 st_decide_mode(st, first_read)
715 struct st_data *st;
716 boolean first_read;
717 {
718 #ifdef SCSIDEBUG
719 struct scsi_link *sc_link = st->sc_link;
720 #endif
721
722 SC_DEBUG(sc_link, SDEV_DB2, ("starting block mode decision\n"));
723
724 /*
725 * If the user hasn't already specified fixed or variable-length
726 * blocks and the block size (zero if variable-length), we'll
727 * have to try to figure them out ourselves.
728 *
729 * Our first shot at a method is, "The quirks made me do it!"
730 */
731 switch (st->quirks & (ST_Q_FORCE_FIXED_MODE | ST_Q_FORCE_VAR_MODE)) {
732 case (ST_Q_FORCE_FIXED_MODE | ST_Q_FORCE_VAR_MODE):
733 printf("%s: bad quirks\n", st->sc_dev.dv_xname);
734 return EINVAL;
735 case ST_Q_FORCE_FIXED_MODE: /*specified fixed, but not what size */
736 st->flags |= ST_FIXEDBLOCKS;
737 if (st->blkmin && (st->blkmin == st->blkmax))
738 st->blksiz = st->blkmin;
739 else if (st->media_blksiz > 0)
740 st->blksiz = st->media_blksiz;
741 else
742 st->blksiz = DEF_FIXED_BSIZE;
743 SC_DEBUG(sc_link, SDEV_DB3, ("Quirks force fixed mode(%d)\n",
744 st->blksiz));
745 goto done;
746 case ST_Q_FORCE_VAR_MODE:
747 st->flags &= ~ST_FIXEDBLOCKS;
748 st->blksiz = 0;
749 SC_DEBUG(sc_link, SDEV_DB3, ("Quirks force variable mode\n"));
750 goto done;
751 }
752
753 /*
754 * If the drive can only handle fixed-length blocks and only at
755 * one size, perhaps we should just do that.
756 */
757 if (st->blkmin && (st->blkmin == st->blkmax)) {
758 st->flags |= ST_FIXEDBLOCKS;
759 st->blksiz = st->blkmin;
760 SC_DEBUG(sc_link, SDEV_DB3,
761 ("blkmin == blkmax of %d\n", st->blkmin));
762 goto done;
763 }
764 /*
765 * If the tape density mandates (or even suggests) use of fixed
766 * or variable-length blocks, comply.
767 */
768 switch (st->density) {
769 case HALFINCH_800:
770 case HALFINCH_1600:
771 case HALFINCH_6250:
772 case DDS:
773 st->flags &= ~ST_FIXEDBLOCKS;
774 st->blksiz = 0;
775 SC_DEBUG(sc_link, SDEV_DB3, ("density specified variable\n"));
776 goto done;
777 case QIC_11:
778 case QIC_24:
779 case QIC_120:
780 case QIC_150:
781 case QIC_525:
782 case QIC_1320:
783 st->flags |= ST_FIXEDBLOCKS;
784 if (st->media_blksiz > 0)
785 st->blksiz = st->media_blksiz;
786 else
787 st->blksiz = DEF_FIXED_BSIZE;
788 SC_DEBUG(sc_link, SDEV_DB3, ("density specified fixed\n"));
789 goto done;
790 }
791 /*
792 * If we're about to read the tape, perhaps we should choose
793 * fixed or variable-length blocks and block size according to
794 * what the drive found on the tape.
795 */
796 if (first_read &&
797 (!(st->quirks & ST_Q_BLKSIZ) || (st->media_blksiz == 0) ||
798 (st->media_blksiz == DEF_FIXED_BSIZE) ||
799 (st->media_blksiz == 1024))) {
800 if (st->media_blksiz == 0)
801 st->flags &= ~ST_FIXEDBLOCKS;
802 else
803 st->flags |= ST_FIXEDBLOCKS;
804 st->blksiz = st->media_blksiz;
805 SC_DEBUG(sc_link, SDEV_DB3,
806 ("Used media_blksiz of %d\n", st->media_blksiz));
807 goto done;
808 }
809 /*
810 * We're getting no hints from any direction. Choose variable-
811 * length blocks arbitrarily.
812 */
813 st->flags &= ~ST_FIXEDBLOCKS;
814 st->blksiz = 0;
815 SC_DEBUG(sc_link, SDEV_DB3,
816 ("Give up and default to variable mode\n"));
817 done:
818
819 /*
820 * Decide whether or not to write two file marks to signify end-
821 * of-data. Make the decision as a function of density. If
822 * the decision is not to use a second file mark, the SCSI BLANK
823 * CHECK condition code will be recognized as end-of-data when
824 * first read.
825 * (I think this should be a by-product of fixed/variable..julian)
826 */
827 switch (st->density) {
828 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
829 case QIC_11:
830 case QIC_24:
831 case QIC_120:
832 case QIC_150:
833 case QIC_525:
834 case QIC_1320:
835 st->flags &= ~ST_2FM_AT_EOD;
836 break;
837 default:
838 st->flags |= ST_2FM_AT_EOD;
839 }
840 return 0;
841 }
842
843 /*
844 * trim the size of the transfer if needed,
845 * called by physio
846 * basically the smaller of our min and the scsi driver's
847 * minphys
848 */
849 void
850 stminphys(bp)
851 struct buf *bp;
852 {
853 register struct st_data *st = stcd.cd_devs[STUNIT(bp->b_dev)];
854
855 (st->sc_link->adapter->scsi_minphys) (bp);
856 }
857
858 /*
859 * Actually translate the requested transfer into
860 * one the physical driver can understand
861 * The transfer is described by a buf and will include
862 * only one physical transfer.
863 */
864 void
865 ststrategy(bp)
866 struct buf *bp;
867 {
868 struct buf *dp;
869 int unit;
870 int opri;
871 struct st_data *st;
872
873 unit = STUNIT(bp->b_dev);
874 st = stcd.cd_devs[unit];
875 SC_DEBUG(st->sc_link, SDEV_DB1,
876 ("ststrategy %d bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
877 /*
878 * If it's a null transfer, return immediatly
879 */
880 if (bp->b_bcount == 0) {
881 goto done;
882 }
883 /*
884 * Odd sized request on fixed drives are verboten
885 */
886 if (st->flags & ST_FIXEDBLOCKS) {
887 if (bp->b_bcount % st->blksiz) {
888 printf("%s: bad request, must be multiple of %d\n",
889 st->sc_dev.dv_xname, st->blksiz);
890 bp->b_error = EIO;
891 goto bad;
892 }
893 }
894 /*
895 * as are out-of-range requests on variable drives.
896 */
897 else if (bp->b_bcount < st->blkmin || bp->b_bcount > st->blkmax) {
898 printf("%s: bad request, must be between %d and %d\n",
899 st->sc_dev.dv_xname, st->blkmin, st->blkmax);
900 bp->b_error = EIO;
901 goto bad;
902 }
903 stminphys(bp);
904 opri = splbio();
905
906 /*
907 * Place it in the queue of activities for this tape
908 * at the end (a bit silly because we only have on user..
909 * (but it could fork()))
910 */
911 dp = &st->buf_queue;
912 bp->b_actf = NULL;
913 bp->b_actb = dp->b_actb;
914 *dp->b_actb = bp;
915 dp->b_actb = &bp->b_actf;
916
917 /*
918 * Tell the device to get going on the transfer if it's
919 * not doing anything, otherwise just wait for completion
920 * (All a bit silly if we're only allowing 1 open but..)
921 */
922 ststart(unit);
923
924 splx(opri);
925 return;
926 bad:
927 bp->b_flags |= B_ERROR;
928 done:
929 /*
930 * Correctly set the buf to indicate a completed xfer
931 */
932 iodone(bp);
933 return;
934 }
935
936 /*
937 * ststart looks to see if there is a buf waiting for the device
938 * and that the device is not already busy. If both are true,
939 * It dequeues the buf and creates a scsi command to perform the
940 * transfer required. The transfer request will call scsi_done
941 * on completion, which will in turn call this routine again
942 * so that the next queued transfer is performed.
943 * The bufs are queued by the strategy routine (ststrategy)
944 *
945 * This routine is also called after other non-queued requests
946 * have been made of the scsi driver, to ensure that the queue
947 * continues to be drained.
948 * ststart() is called at splbio
949 */
950 void
951 ststart(unit)
952 int unit;
953 {
954 struct st_data *st = stcd.cd_devs[unit];
955 struct scsi_link *sc_link = st->sc_link;
956 register struct buf *bp, *dp;
957 struct scsi_rw_tape cmd;
958 u_int flags;
959
960 SC_DEBUG(sc_link, SDEV_DB2, ("ststart "));
961 /*
962 * See if there is a buf to do and we are not already
963 * doing one
964 */
965 while (sc_link->opennings) {
966 /* if a special awaits, let it proceed first */
967 if (sc_link->flags & SDEV_WAITING) {
968 sc_link->flags &= ~SDEV_WAITING;
969 wakeup((caddr_t)sc_link);
970 return;
971 }
972
973 bp = st->buf_queue.b_actf;
974 if (!bp)
975 return; /* no work to bother with */
976 if (dp = bp->b_actf)
977 dp->b_actb = bp->b_actb;
978 else
979 st->buf_queue.b_actb = bp->b_actb;
980 *bp->b_actb = dp;
981
982 /*
983 * if the device has been unmounted byt the user
984 * then throw away all requests until done
985 */
986 if (!(st->flags & ST_MOUNTED) ||
987 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
988 /* make sure that one implies the other.. */
989 sc_link->flags &= ~SDEV_MEDIA_LOADED;
990 goto badnews;
991 }
992 /*
993 * only FIXEDBLOCK devices have pending operations
994 */
995 if (st->flags & ST_FIXEDBLOCKS) {
996 /*
997 * If we are at a filemark but have not reported it yet
998 * then we should report it now
999 */
1000 if (st->flags & ST_AT_FILEMARK) {
1001 if ((bp->b_flags & B_READ) == B_WRITE) {
1002 /*
1003 * Handling of ST_AT_FILEMARK in
1004 * st_space will fill in the right file
1005 * mark count.
1006 * Back up over filemark
1007 */
1008 if (st_space(st, 0, SP_FILEMARKS, 0))
1009 goto badnews;
1010 } else {
1011 bp->b_resid = bp->b_bcount;
1012 bp->b_error = 0;
1013 bp->b_flags &= ~B_ERROR;
1014 st->flags &= ~ST_AT_FILEMARK;
1015 biodone(bp);
1016 continue; /* seek more work */
1017 }
1018 }
1019 /*
1020 * If we are at EIO (e.g. EOM) but have not reported it
1021 * yet then we should report it now
1022 */
1023 if (st->flags & ST_EIO_PENDING) {
1024 bp->b_resid = bp->b_bcount;
1025 bp->b_error = EIO;
1026 bp->b_flags |= B_ERROR;
1027 st->flags &= ~ST_EIO_PENDING;
1028 biodone(bp);
1029 continue; /* seek more work */
1030 }
1031 }
1032 /*
1033 * Fill out the scsi command
1034 */
1035 bzero(&cmd, sizeof(cmd));
1036 if ((bp->b_flags & B_READ) == B_WRITE) {
1037 cmd.op_code = WRITE_COMMAND_TAPE;
1038 st->flags &= ~ST_FM_WRITTEN;
1039 st->flags |= ST_WRITTEN;
1040 flags = SCSI_DATA_OUT;
1041 } else {
1042 cmd.op_code = READ_COMMAND_TAPE;
1043 flags = SCSI_DATA_IN;
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 |= SRWT_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 u_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_chkeod(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_chkeod(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, FALSE, 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_RETEN, 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_chkeod(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 u_int flags;
1260 char *buf;
1261 {
1262 struct scsi_rw_tape scsi_cmd;
1263
1264 /*
1265 * If it's a null transfer, return immediatly
1266 */
1267 if (size == 0)
1268 return 0;
1269 bzero(&scsi_cmd, sizeof(scsi_cmd));
1270 scsi_cmd.op_code = READ_COMMAND_TAPE;
1271 if (st->flags & ST_FIXEDBLOCKS) {
1272 scsi_cmd.byte2 |= SRWT_FIXED;
1273 lto3b(size / (st->blksiz ? st->blksiz : DEF_FIXED_BSIZE),
1274 scsi_cmd.len);
1275 } else
1276 lto3b(size, scsi_cmd.len);
1277 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1278 sizeof(scsi_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_rd_blk_lim(st, flags)
1293 struct st_data *st;
1294 u_int flags;
1295 {
1296 struct scsi_blk_limits scsi_cmd;
1297 struct scsi_blk_limits_data scsi_blkl;
1298 int error;
1299 struct scsi_link *sc_link = st->sc_link;
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(&scsi_cmd, sizeof(scsi_cmd));
1311 scsi_cmd.op_code = READ_BLK_LIMITS;
1312
1313 /*
1314 * do the command, update the global values
1315 */
1316 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
1317 sizeof(scsi_cmd), (u_char *) & scsi_blkl, sizeof(scsi_blkl),
1318 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
1319 return error;
1320
1321 st->blkmin = b2tol(scsi_blkl.min_length);
1322 st->blkmax = _3btol(&scsi_blkl.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 u_int flags;
1343 {
1344 u_int scsi_sense_len;
1345 int error;
1346 char *scsi_sense_ptr;
1347 struct scsi_mode_sense scsi_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(&scsi_cmd, sizeof(scsi_cmd));
1381 scsi_cmd.op_code = MODE_SENSE;
1382 scsi_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 *) &scsi_cmd,
1391 sizeof(scsi_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 u_int flags;
1425 {
1426 u_int dat_len;
1427 char *dat_ptr;
1428 struct scsi_mode_select scsi_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(dat_ptr, dat_len);
1453 bzero(&scsi_cmd, sizeof(scsi_cmd));
1454 scsi_cmd.op_code = MODE_SELECT;
1455 scsi_cmd.length = dat_len;
1456 ((struct dat *) dat_ptr)->header.blk_desc_len = sizeof(struct blk_desc);
1457 ((struct dat *) dat_ptr)->header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
1458 ((struct dat *) dat_ptr)->blk_desc.density = st->density;
1459 if (st->flags & ST_FIXEDBLOCKS)
1460 lto3b(st->blksiz, ((struct dat *) dat_ptr)->blk_desc.blklen);
1461 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1462 bcopy(st->sense_data,
1463 ((struct dat_page_0 *) dat_ptr)->sense_data,
1464 sizeof(((struct dat_page_0 *) dat_ptr)->sense_data));
1465 /* the Tandberg tapes need the block size to */
1466 /* be set on each mode sense/select. */
1467 }
1468 /*
1469 * do the command
1470 */
1471 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1472 sizeof(scsi_cmd), (u_char *) dat_ptr, dat_len, ST_RETRIES, 5000,
1473 NULL, flags | SCSI_DATA_OUT);
1474 }
1475
1476 /*
1477 * skip N blocks/filemarks/seq filemarks/eom
1478 */
1479 int
1480 st_space(st, number, what, flags)
1481 struct st_data *st;
1482 u_int what;
1483 u_int flags;
1484 int number;
1485 {
1486 int error;
1487 struct scsi_space scsi_cmd;
1488
1489 switch (what) {
1490 case SP_BLKS:
1491 if (st->flags & ST_PER_ACTION) {
1492 if (number > 0) {
1493 st->flags &= ~ST_PER_ACTION;
1494 return EIO;
1495 } else if (number < 0) {
1496 if (st->flags & ST_AT_FILEMARK) {
1497 /*
1498 * Handling of ST_AT_FILEMARK
1499 * in st_space will fill in the
1500 * right file mark count.
1501 */
1502 error = st_space(st, 0, SP_FILEMARKS,
1503 flags);
1504 if (error)
1505 return error;
1506 }
1507 if (st->flags & ST_BLANK_READ) {
1508 st->flags &= ~ST_BLANK_READ;
1509 return EIO;
1510 }
1511 st->flags &= ~ST_EIO_PENDING;
1512 }
1513 }
1514 break;
1515 case SP_FILEMARKS:
1516 if (st->flags & ST_EIO_PENDING) {
1517 if (number > 0) {
1518 /* pretend we just discovered the error */
1519 st->flags &= ~ST_EIO_PENDING;
1520 return EIO;
1521 } else if (number < 0) {
1522 /* back away from the error */
1523 st->flags &= ~ST_EIO_PENDING;
1524 }
1525 }
1526 if (st->flags & ST_AT_FILEMARK) {
1527 st->flags &= ~ST_AT_FILEMARK;
1528 number--;
1529 }
1530 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1531 /* back away from unwritten tape */
1532 st->flags &= ~ST_BLANK_READ;
1533 number++; /* XXX dubious */
1534 }
1535 break;
1536 case SP_EOM:
1537 if (st->flags & ST_EIO_PENDING) {
1538 /* pretend we just discovered the error */
1539 st->flags &= ~ST_EIO_PENDING;
1540 return EIO;
1541 }
1542 if (st->flags & ST_AT_FILEMARK)
1543 st->flags &= ~ST_AT_FILEMARK;
1544 break;
1545 }
1546 if (number == 0)
1547 return 0;
1548 bzero(&scsi_cmd, sizeof(scsi_cmd));
1549 scsi_cmd.op_code = SPACE;
1550 scsi_cmd.byte2 = what & SS_CODE;
1551 lto3b(number, scsi_cmd.number);
1552 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1553 sizeof(scsi_cmd), 0, 0, 0, 600000, NULL, flags);
1554 }
1555
1556 /*
1557 * write N filemarks
1558 */
1559 int
1560 st_write_filemarks(st, number, flags)
1561 struct st_data *st;
1562 u_int flags;
1563 int number;
1564 {
1565 struct scsi_write_filemarks scsi_cmd;
1566
1567 /*
1568 * It's hard to write a negative number of file marks.
1569 * Don't try.
1570 */
1571 if (number < 0)
1572 return EINVAL;
1573 switch (number) {
1574 case 0: /* really a command to sync the drive's buffers */
1575 break;
1576 case 1:
1577 if (st->flags & ST_FM_WRITTEN) /* already have one down */
1578 st->flags &= ~ST_WRITTEN;
1579 else
1580 st->flags |= ST_FM_WRITTEN;
1581 st->flags &= ~ST_PER_ACTION;
1582 break;
1583 default:
1584 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1585 }
1586 bzero(&scsi_cmd, sizeof(scsi_cmd));
1587 scsi_cmd.op_code = WRITE_FILEMARKS;
1588 lto3b(number, scsi_cmd.number);
1589 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1590 sizeof(scsi_cmd), 0, 0, 0, 100000, NULL, flags);
1591 }
1592
1593 /*
1594 * Make sure the right number of file marks is on tape if the
1595 * tape has been written. If the position argument is true,
1596 * leave the tape positioned where it was originally.
1597 *
1598 * nmarks returns the number of marks to skip (or, if position
1599 * true, which were skipped) to get back original position.
1600 */
1601 int
1602 st_chkeod(st, position, nmarks, flags)
1603 struct st_data *st;
1604 boolean position;
1605 int *nmarks;
1606 u_int flags;
1607 {
1608 int error;
1609
1610 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1611 default:
1612 *nmarks = 0;
1613 return 0;
1614 case ST_WRITTEN:
1615 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1616 *nmarks = 1;
1617 break;
1618 case ST_WRITTEN | ST_2FM_AT_EOD:
1619 *nmarks = 2;
1620 }
1621 error = st_write_filemarks(st, *nmarks, flags);
1622 if (position && !error)
1623 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1624 return error;
1625 }
1626
1627 /*
1628 * load/unload/retension
1629 */
1630 int
1631 st_load(st, type, flags)
1632 struct st_data *st;
1633 u_int type;
1634 u_int flags;
1635 {
1636 struct scsi_load scsi_cmd;
1637 struct scsi_link *sc_link = st->sc_link;
1638
1639 bzero(&scsi_cmd, sizeof(scsi_cmd));
1640 if (type != LD_LOAD) {
1641 int error;
1642 int nmarks;
1643
1644 error = st_chkeod(st, FALSE, &nmarks, flags);
1645 if (error)
1646 return error;
1647 }
1648 if (st->quirks & ST_Q_IGNORE_LOADS)
1649 return 0;
1650 scsi_cmd.op_code = LOAD_UNLOAD;
1651 scsi_cmd.how |= type;
1652 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1653 sizeof(scsi_cmd), 0, 0, ST_RETRIES, 300000, NULL, flags);
1654 }
1655
1656 /*
1657 * Rewind the device
1658 */
1659 int
1660 st_rewind(st, immed, flags)
1661 struct st_data *st;
1662 u_int flags;
1663 boolean immed;
1664 {
1665 struct scsi_rewind scsi_cmd;
1666 int error;
1667 int nmarks;
1668
1669 error = st_chkeod(st, FALSE, &nmarks, flags);
1670 if (error)
1671 return error;
1672 st->flags &= ~ST_PER_ACTION;
1673 bzero(&scsi_cmd, sizeof(scsi_cmd));
1674 scsi_cmd.op_code = REWIND;
1675 scsi_cmd.byte2 = immed ? SR_IMMED : 0;
1676 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1677 sizeof(scsi_cmd), 0, 0, ST_RETRIES, immed ? 5000 : 300000, NULL,
1678 flags);
1679 }
1680
1681 /*
1682 * Look at the returned sense and act on the error and detirmine
1683 * The unix error number to pass back... (0 = report no error)
1684 * (-1 = continue processing)
1685 */
1686 int
1687 st_interpret_sense(xs)
1688 struct scsi_xfer *xs;
1689 {
1690 struct scsi_link *sc_link = xs->sc_link;
1691 struct scsi_sense_data *sense = &xs->sense;
1692 boolean silent = xs->flags & SCSI_SILENT;
1693 struct buf *bp = xs->bp;
1694 int unit = sc_link->dev_unit;
1695 struct st_data *st = stcd.cd_devs[unit];
1696 u_int key;
1697 int info;
1698
1699 /*
1700 * Get the sense fields and work out what code
1701 */
1702 if (sense->error_code & SSD_ERRCODE_VALID)
1703 info = ntohl(*((int *) sense->ext.extended.info));
1704 else
1705 info = xs->datalen; /* bad choice if fixed blocks */
1706 if ((sense->error_code & SSD_ERRCODE) != 0x70)
1707 return -1; /* let the generic code handle it */
1708 if (st->flags & ST_FIXEDBLOCKS) {
1709 xs->resid = info * st->blksiz;
1710 if (sense->ext.extended.flags & SSD_EOM) {
1711 st->flags |= ST_EIO_PENDING;
1712 if (bp)
1713 bp->b_resid = xs->resid;
1714 }
1715 if (sense->ext.extended.flags & SSD_FILEMARK) {
1716 st->flags |= ST_AT_FILEMARK;
1717 if (bp)
1718 bp->b_resid = xs->resid;
1719 }
1720 if (sense->ext.extended.flags & SSD_ILI) {
1721 st->flags |= ST_EIO_PENDING;
1722 if (bp)
1723 bp->b_resid = xs->resid;
1724 if (sense->error_code & SSD_ERRCODE_VALID && !silent)
1725 printf("%s: block wrong size, %d blocks residual\n",
1726 st->sc_dev.dv_xname, info);
1727
1728 /*
1729 * This quirk code helps the drive read
1730 * the first tape block, regardless of
1731 * format. That is required for these
1732 * drives to return proper MODE SENSE
1733 * information.
1734 */
1735 if ((st->quirks & ST_Q_SNS_HLP) &&
1736 !(sc_link->flags & SDEV_MEDIA_LOADED))
1737 st->blksiz -= 512;
1738 }
1739 /*
1740 * If no data was tranfered, do it immediatly
1741 */
1742 if (xs->resid >= xs->datalen) {
1743 if (st->flags & ST_EIO_PENDING)
1744 return EIO;
1745 if (st->flags & ST_AT_FILEMARK) {
1746 if (bp)
1747 bp->b_resid = xs->resid;
1748 return 0;
1749 }
1750 }
1751 } else { /* must be variable mode */
1752 xs->resid = xs->datalen; /* to be sure */
1753 if (sense->ext.extended.flags & SSD_EOM)
1754 return EIO;
1755 if (sense->ext.extended.flags & SSD_FILEMARK) {
1756 if (bp)
1757 bp->b_resid = bp->b_bcount;
1758 return 0;
1759 }
1760 if (sense->ext.extended.flags & SSD_ILI) {
1761 if (info < 0) {
1762 /*
1763 * the record was bigger than the read
1764 */
1765 if (!silent)
1766 printf("%s: %d-byte record too big\n",
1767 st->sc_dev.dv_xname,
1768 xs->datalen - info);
1769 return EIO;
1770 }
1771 xs->resid = info;
1772 if (bp)
1773 bp->b_resid = info;
1774 }
1775 }
1776 key = sense->ext.extended.flags & SSD_KEY;
1777
1778 if (key == 0x8) {
1779 /*
1780 * This quirk code helps the drive read the
1781 * first tape block, regardless of format. That
1782 * is required for these drives to return proper
1783 * MODE SENSE information.
1784 */
1785 if ((st->quirks & ST_Q_SNS_HLP) &&
1786 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
1787 /* still starting */
1788 st->blksiz -= 512;
1789 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
1790 st->flags |= ST_BLANK_READ;
1791 xs->resid = xs->datalen;
1792 if (bp) {
1793 bp->b_resid = xs->resid;
1794 /* return an EOF */
1795 }
1796 return 0;
1797 }
1798 }
1799 return -1; /* let the default/generic handler handle it */
1800 }
1801
1802 /*
1803 * The quirk here is that the drive returns some value to st_mode_sense
1804 * incorrectly until the tape has actually passed by the head.
1805 *
1806 * The method is to set the drive to large fixed-block state (user-specified
1807 * density and 1024-byte blocks), then read and rewind to get it to sense the
1808 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
1809 * work, as a last resort, try variable- length blocks. The result will be
1810 * the ability to do an accurate st_mode_sense.
1811 *
1812 * We know we can do a rewind because we just did a load, which implies rewind.
1813 * Rewind seems preferable to space backward if we have a virgin tape.
1814 *
1815 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
1816 * error processing, both part of st_interpret_sense.
1817 */
1818 int
1819 st_touch_tape(st)
1820 struct st_data *st;
1821 {
1822 char *buf;
1823 u_int readsiz;
1824 int error;
1825
1826 buf = malloc(1024, M_TEMP, M_NOWAIT);
1827 if (!buf)
1828 return ENOMEM;
1829
1830 if (error = st_mode_sense(st, 0))
1831 goto bad;
1832 st->blksiz = 1024;
1833 do {
1834 switch (st->blksiz) {
1835 case 512:
1836 case 1024:
1837 readsiz = st->blksiz;
1838 st->flags |= ST_FIXEDBLOCKS;
1839 break;
1840 default:
1841 readsiz = 1;
1842 st->flags &= ~ST_FIXEDBLOCKS;
1843 }
1844 if (error = st_mode_select(st, 0))
1845 goto bad;
1846 st_read(st, buf, readsiz, SCSI_SILENT);
1847 if (error = st_rewind(st, FALSE, 0)) {
1848 bad: free(buf, M_TEMP);
1849 return error;
1850 }
1851 } while (readsiz != 1 && readsiz > st->blksiz);
1852 free(buf, M_TEMP);
1853 return 0;
1854 }
1855