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