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