st.c revision 1.25 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.25 1994/04/05 22:39:37 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 if (!error)
1165 error = st_load(st, LD_LOAD, flags);
1166 break;
1167 case MTEOM: /* forward space to end of media */
1168 error = st_chkeod(st, FALSE, &nmarks, flags);
1169 if (!error)
1170 error = st_space(st, 1, SP_EOM, flags);
1171 break;
1172 case MTCACHE: /* enable controller cache */
1173 case MTNOCACHE: /* disable controller cache */
1174 break;
1175 case MTSETBSIZ: /* Set block size for device */
1176 #ifdef NOTYET
1177 if (!(st->flags & ST_NEW_MOUNT)) {
1178 uprintf("re-mount tape before changing blocksize");
1179 error = EINVAL;
1180 break;
1181 }
1182 #endif
1183 if (number == 0) {
1184 st->flags &= ~ST_FIXEDBLOCKS;
1185 } else {
1186 if ((st->blkmin || st->blkmax) &&
1187 (number < st->blkmin ||
1188 number > st->blkmax)) {
1189 error = EINVAL;
1190 break;
1191 }
1192 st->flags |= ST_FIXEDBLOCKS;
1193 }
1194 st->blksiz = number;
1195 st->flags |= ST_BLOCK_SET; /*XXX */
1196 goto try_new_value;
1197
1198 case MTSETDNSTY: /* Set density for device and mode */
1199 if (number > SCSI_2_MAX_DENSITY_CODE)
1200 error = EINVAL;
1201 else
1202 st->density = number;
1203 goto try_new_value;
1204
1205 default:
1206 error = EINVAL;
1207 }
1208 break;
1209 }
1210 case MTIOCIEOT:
1211 case MTIOCEEOT:
1212 break;
1213 default:
1214 if (STMODE(dev) == CTLMODE)
1215 error = scsi_do_ioctl(st->sc_link,cmd,arg,flag);
1216 else
1217 error = ENOTTY;
1218 break;
1219 }
1220 return error;
1221 /*-----------------------------*/
1222 try_new_value:
1223 /*
1224 * Check that the mode being asked for is aggreeable to the
1225 * drive. If not, put it back the way it was.
1226 */
1227 if (error = st_mode_select(st, 0)) { /* put it back as it was */
1228 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1229 st->density = hold_density;
1230 st->blksiz = hold_blksiz;
1231 if (st->blksiz)
1232 st->flags |= ST_FIXEDBLOCKS;
1233 else
1234 st->flags &= ~ST_FIXEDBLOCKS;
1235 return error;
1236 }
1237 /*
1238 * As the drive liked it, if we are setting a new default,
1239 * set it into the structures as such.
1240 *
1241 * The means for deciding this are not finalised yet
1242 */
1243 if (STMODE(dev) == 0x03) {
1244 /* special mode */
1245 /* XXX */
1246 switch ((short) (mt->mt_op)) {
1247 case MTSETBSIZ:
1248 st->modes[dsty].blksiz = st->blksiz;
1249 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1250 break;
1251 case MTSETDNSTY:
1252 st->modes[dsty].density = st->density;
1253 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1254 break;
1255 }
1256 }
1257 return 0;
1258 }
1259
1260 /*
1261 * Do a synchronous read.
1262 */
1263 int
1264 st_read(st, buf, size, flags)
1265 struct st_data *st;
1266 u_int size;
1267 u_int flags;
1268 char *buf;
1269 {
1270 struct scsi_rw_tape scsi_cmd;
1271
1272 /*
1273 * If it's a null transfer, return immediatly
1274 */
1275 if (size == 0)
1276 return 0;
1277 bzero(&scsi_cmd, sizeof(scsi_cmd));
1278 scsi_cmd.op_code = READ_COMMAND_TAPE;
1279 if (st->flags & ST_FIXEDBLOCKS) {
1280 scsi_cmd.byte2 |= SRWT_FIXED;
1281 lto3b(size / (st->blksiz ? st->blksiz : DEF_FIXED_BSIZE),
1282 scsi_cmd.len);
1283 } else
1284 lto3b(size, scsi_cmd.len);
1285 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1286 sizeof(scsi_cmd), (u_char *) buf, size, 0, 100000, NULL,
1287 flags | SCSI_DATA_IN);
1288 }
1289
1290 #ifdef __STDC__
1291 #define b2tol(a) (((unsigned)(a##_1) << 8) + (unsigned)a##_0)
1292 #else
1293 #define b2tol(a) (((unsigned)(a/**/_1) << 8) + (unsigned)a/**/_0)
1294 #endif
1295
1296 /*
1297 * Ask the drive what it's min and max blk sizes are.
1298 */
1299 int
1300 st_rd_blk_lim(st, flags)
1301 struct st_data *st;
1302 u_int flags;
1303 {
1304 struct scsi_blk_limits scsi_cmd;
1305 struct scsi_blk_limits_data scsi_blkl;
1306 int error;
1307 struct scsi_link *sc_link = st->sc_link;
1308
1309 /*
1310 * First check if we have it all loaded
1311 */
1312 if ((sc_link->flags & SDEV_MEDIA_LOADED))
1313 return 0;
1314
1315 /*
1316 * do a 'Read Block Limits'
1317 */
1318 bzero(&scsi_cmd, sizeof(scsi_cmd));
1319 scsi_cmd.op_code = READ_BLK_LIMITS;
1320
1321 /*
1322 * do the command, update the global values
1323 */
1324 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
1325 sizeof(scsi_cmd), (u_char *) & scsi_blkl, sizeof(scsi_blkl),
1326 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
1327 return error;
1328
1329 st->blkmin = b2tol(scsi_blkl.min_length);
1330 st->blkmax = _3btol(&scsi_blkl.max_length_2);
1331
1332 SC_DEBUG(sc_link, SDEV_DB3,
1333 ("(%d <= blksiz <= %d)\n", st->blkmin, st->blkmax));
1334 return 0;
1335 }
1336
1337 /*
1338 * Get the scsi driver to send a full inquiry to the
1339 * device and use the results to fill out the global
1340 * parameter structure.
1341 *
1342 * called from:
1343 * attach
1344 * open
1345 * ioctl (to reset original blksize)
1346 */
1347 int
1348 st_mode_sense(st, flags)
1349 struct st_data *st;
1350 u_int flags;
1351 {
1352 u_int scsi_sense_len;
1353 int error;
1354 char *scsi_sense_ptr;
1355 struct scsi_mode_sense scsi_cmd;
1356 struct scsi_sense {
1357 struct scsi_mode_header header;
1358 struct blk_desc blk_desc;
1359 } scsi_sense;
1360
1361 struct scsi_sense_page_0 {
1362 struct scsi_mode_header header;
1363 struct blk_desc blk_desc;
1364 u_char sense_data[PAGE_0_SENSE_DATA_SIZE];
1365 /* Tandberg tape drives returns page 00
1366 * with the sense data, whether or not
1367 * you want it (ie the don't like you
1368 * saying you want anything less!!!!!)
1369 * They also expect page 00
1370 * back when you issue a mode select
1371 */
1372 } scsi_sense_page_0;
1373 struct scsi_link *sc_link = st->sc_link;
1374
1375 /*
1376 * Define what sort of structure we're working with
1377 */
1378 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1379 scsi_sense_len = sizeof(scsi_sense_page_0);
1380 scsi_sense_ptr = (char *) &scsi_sense_page_0;
1381 } else {
1382 scsi_sense_len = sizeof(scsi_sense);
1383 scsi_sense_ptr = (char *) &scsi_sense;
1384 }
1385 /*
1386 * Set up a mode sense
1387 */
1388 bzero(&scsi_cmd, sizeof(scsi_cmd));
1389 scsi_cmd.op_code = MODE_SENSE;
1390 scsi_cmd.length = scsi_sense_len;
1391
1392 /*
1393 * do the command, but we don't need the results
1394 * just print them for our interest's sake, if asked,
1395 * or if we need it as a template for the mode select
1396 * store it away.
1397 */
1398 if (error = scsi_scsi_cmd(sc_link, (struct scsi_generic *) &scsi_cmd,
1399 sizeof(scsi_cmd), (u_char *) scsi_sense_ptr, scsi_sense_len,
1400 ST_RETRIES, 5000, NULL, flags | SCSI_DATA_IN))
1401 return error;
1402
1403 st->numblks = _3btol(((struct scsi_sense *)scsi_sense_ptr)->blk_desc.nblocks);
1404 st->media_blksiz = _3btol(((struct scsi_sense *)scsi_sense_ptr)->blk_desc.blklen);
1405 st->media_density = ((struct scsi_sense *) scsi_sense_ptr)->blk_desc.density;
1406 if (((struct scsi_sense *) scsi_sense_ptr)->header.dev_spec &
1407 SMH_DSP_WRITE_PROT)
1408 st->flags |= ST_READONLY;
1409 SC_DEBUG(sc_link, SDEV_DB3,
1410 ("density code 0x%x, %d-byte blocks, write-%s, ",
1411 st->media_density, st->media_blksiz,
1412 st->flags & ST_READONLY ? "protected" : "enabled"));
1413 SC_DEBUG(sc_link, SDEV_DB3,
1414 ("%sbuffered\n",
1415 ((struct scsi_sense *) scsi_sense_ptr)->header.dev_spec &
1416 SMH_DSP_BUFF_MODE ? "" : "un"));
1417 if (st->quirks & ST_Q_NEEDS_PAGE_0)
1418 bcopy(((struct scsi_sense_page_0 *) scsi_sense_ptr)->sense_data,
1419 st->sense_data,
1420 sizeof(((struct scsi_sense_page_0 *) scsi_sense_ptr)->sense_data));
1421 sc_link->flags |= SDEV_MEDIA_LOADED;
1422 return 0;
1423 }
1424
1425 /*
1426 * Send a filled out parameter structure to the drive to
1427 * set it into the desire modes etc.
1428 */
1429 int
1430 st_mode_select(st, flags)
1431 struct st_data *st;
1432 u_int flags;
1433 {
1434 u_int dat_len;
1435 char *dat_ptr;
1436 struct scsi_mode_select scsi_cmd;
1437 struct dat {
1438 struct scsi_mode_header header;
1439 struct blk_desc blk_desc;
1440 } dat;
1441 struct dat_page_0 {
1442 struct scsi_mode_header header;
1443 struct blk_desc blk_desc;
1444 u_char sense_data[PAGE_0_SENSE_DATA_SIZE];
1445 } dat_page_0;
1446
1447 /*
1448 * Define what sort of structure we're working with
1449 */
1450 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1451 dat_len = sizeof(dat_page_0);
1452 dat_ptr = (char *) &dat_page_0;
1453 } else {
1454 dat_len = sizeof(dat);
1455 dat_ptr = (char *) &dat;
1456 }
1457 /*
1458 * Set up for a mode select
1459 */
1460 bzero(dat_ptr, dat_len);
1461 bzero(&scsi_cmd, sizeof(scsi_cmd));
1462 scsi_cmd.op_code = MODE_SELECT;
1463 scsi_cmd.length = dat_len;
1464 ((struct dat *) dat_ptr)->header.blk_desc_len = sizeof(struct blk_desc);
1465 ((struct dat *) dat_ptr)->header.dev_spec |= SMH_DSP_BUFF_MODE_ON;
1466 ((struct dat *) dat_ptr)->blk_desc.density = st->density;
1467 if (st->flags & ST_FIXEDBLOCKS)
1468 lto3b(st->blksiz, ((struct dat *) dat_ptr)->blk_desc.blklen);
1469 if (st->quirks & ST_Q_NEEDS_PAGE_0) {
1470 bcopy(st->sense_data,
1471 ((struct dat_page_0 *) dat_ptr)->sense_data,
1472 sizeof(((struct dat_page_0 *) dat_ptr)->sense_data));
1473 /* the Tandberg tapes need the block size to */
1474 /* be set on each mode sense/select. */
1475 }
1476 /*
1477 * do the command
1478 */
1479 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1480 sizeof(scsi_cmd), (u_char *) dat_ptr, dat_len, ST_RETRIES, 5000,
1481 NULL, flags | SCSI_DATA_OUT);
1482 }
1483
1484 /*
1485 * skip N blocks/filemarks/seq filemarks/eom
1486 */
1487 int
1488 st_space(st, number, what, flags)
1489 struct st_data *st;
1490 u_int what;
1491 u_int flags;
1492 int number;
1493 {
1494 int error;
1495 struct scsi_space scsi_cmd;
1496
1497 switch (what) {
1498 case SP_BLKS:
1499 if (st->flags & ST_PER_ACTION) {
1500 if (number > 0) {
1501 st->flags &= ~ST_PER_ACTION;
1502 return EIO;
1503 } else if (number < 0) {
1504 if (st->flags & ST_AT_FILEMARK) {
1505 /*
1506 * Handling of ST_AT_FILEMARK
1507 * in st_space will fill in the
1508 * right file mark count.
1509 */
1510 error = st_space(st, 0, SP_FILEMARKS,
1511 flags);
1512 if (error)
1513 return error;
1514 }
1515 if (st->flags & ST_BLANK_READ) {
1516 st->flags &= ~ST_BLANK_READ;
1517 return EIO;
1518 }
1519 st->flags &= ~ST_EIO_PENDING;
1520 }
1521 }
1522 break;
1523 case SP_FILEMARKS:
1524 if (st->flags & ST_EIO_PENDING) {
1525 if (number > 0) {
1526 /* pretend we just discovered the error */
1527 st->flags &= ~ST_EIO_PENDING;
1528 return EIO;
1529 } else if (number < 0) {
1530 /* back away from the error */
1531 st->flags &= ~ST_EIO_PENDING;
1532 }
1533 }
1534 if (st->flags & ST_AT_FILEMARK) {
1535 st->flags &= ~ST_AT_FILEMARK;
1536 number--;
1537 }
1538 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1539 /* back away from unwritten tape */
1540 st->flags &= ~ST_BLANK_READ;
1541 number++; /* XXX dubious */
1542 }
1543 break;
1544 case SP_EOM:
1545 if (st->flags & ST_EIO_PENDING) {
1546 /* pretend we just discovered the error */
1547 st->flags &= ~ST_EIO_PENDING;
1548 return EIO;
1549 }
1550 if (st->flags & ST_AT_FILEMARK)
1551 st->flags &= ~ST_AT_FILEMARK;
1552 break;
1553 }
1554 if (number == 0)
1555 return 0;
1556 bzero(&scsi_cmd, sizeof(scsi_cmd));
1557 scsi_cmd.op_code = SPACE;
1558 scsi_cmd.byte2 = what & SS_CODE;
1559 lto3b(number, scsi_cmd.number);
1560 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1561 sizeof(scsi_cmd), 0, 0, 0, 600000, NULL, flags);
1562 }
1563
1564 /*
1565 * write N filemarks
1566 */
1567 int
1568 st_write_filemarks(st, number, flags)
1569 struct st_data *st;
1570 u_int flags;
1571 int number;
1572 {
1573 struct scsi_write_filemarks scsi_cmd;
1574
1575 /*
1576 * It's hard to write a negative number of file marks.
1577 * Don't try.
1578 */
1579 if (number < 0)
1580 return EINVAL;
1581 switch (number) {
1582 case 0: /* really a command to sync the drive's buffers */
1583 break;
1584 case 1:
1585 if (st->flags & ST_FM_WRITTEN) /* already have one down */
1586 st->flags &= ~ST_WRITTEN;
1587 else
1588 st->flags |= ST_FM_WRITTEN;
1589 st->flags &= ~ST_PER_ACTION;
1590 break;
1591 default:
1592 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1593 }
1594 bzero(&scsi_cmd, sizeof(scsi_cmd));
1595 scsi_cmd.op_code = WRITE_FILEMARKS;
1596 lto3b(number, scsi_cmd.number);
1597 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1598 sizeof(scsi_cmd), 0, 0, 0, 100000, NULL, flags);
1599 }
1600
1601 /*
1602 * Make sure the right number of file marks is on tape if the
1603 * tape has been written. If the position argument is true,
1604 * leave the tape positioned where it was originally.
1605 *
1606 * nmarks returns the number of marks to skip (or, if position
1607 * true, which were skipped) to get back original position.
1608 */
1609 int
1610 st_chkeod(st, position, nmarks, flags)
1611 struct st_data *st;
1612 boolean position;
1613 int *nmarks;
1614 u_int flags;
1615 {
1616 int error;
1617
1618 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1619 default:
1620 *nmarks = 0;
1621 return 0;
1622 case ST_WRITTEN:
1623 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1624 *nmarks = 1;
1625 break;
1626 case ST_WRITTEN | ST_2FM_AT_EOD:
1627 *nmarks = 2;
1628 }
1629 error = st_write_filemarks(st, *nmarks, flags);
1630 if (position && !error)
1631 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1632 return error;
1633 }
1634
1635 /*
1636 * load/unload/retension
1637 */
1638 int
1639 st_load(st, type, flags)
1640 struct st_data *st;
1641 u_int type;
1642 u_int flags;
1643 {
1644 struct scsi_load scsi_cmd;
1645 struct scsi_link *sc_link = st->sc_link;
1646
1647 bzero(&scsi_cmd, sizeof(scsi_cmd));
1648 if (type != LD_LOAD) {
1649 int error;
1650 int nmarks;
1651
1652 error = st_chkeod(st, FALSE, &nmarks, flags);
1653 if (error)
1654 return error;
1655 }
1656 if (st->quirks & ST_Q_IGNORE_LOADS)
1657 return 0;
1658 scsi_cmd.op_code = LOAD_UNLOAD;
1659 scsi_cmd.how |= type;
1660 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1661 sizeof(scsi_cmd), 0, 0, ST_RETRIES, 300000, NULL, flags);
1662 }
1663
1664 /*
1665 * Rewind the device
1666 */
1667 int
1668 st_rewind(st, immed, flags)
1669 struct st_data *st;
1670 u_int flags;
1671 boolean immed;
1672 {
1673 struct scsi_rewind scsi_cmd;
1674 int error;
1675 int nmarks;
1676
1677 error = st_chkeod(st, FALSE, &nmarks, flags);
1678 if (error)
1679 return error;
1680 st->flags &= ~ST_PER_ACTION;
1681 bzero(&scsi_cmd, sizeof(scsi_cmd));
1682 scsi_cmd.op_code = REWIND;
1683 scsi_cmd.byte2 = immed ? SR_IMMED : 0;
1684 return scsi_scsi_cmd(st->sc_link, (struct scsi_generic *) &scsi_cmd,
1685 sizeof(scsi_cmd), 0, 0, ST_RETRIES, immed ? 5000 : 300000, NULL,
1686 flags);
1687 }
1688
1689 /*
1690 * Look at the returned sense and act on the error and detirmine
1691 * The unix error number to pass back... (0 = report no error)
1692 * (-1 = continue processing)
1693 */
1694 int
1695 st_interpret_sense(xs)
1696 struct scsi_xfer *xs;
1697 {
1698 struct scsi_link *sc_link = xs->sc_link;
1699 struct scsi_sense_data *sense = &xs->sense;
1700 boolean silent = xs->flags & SCSI_SILENT;
1701 struct buf *bp = xs->bp;
1702 int unit = sc_link->dev_unit;
1703 struct st_data *st = stcd.cd_devs[unit];
1704 u_int key;
1705 int info;
1706
1707 /*
1708 * Get the sense fields and work out what code
1709 */
1710 if (sense->error_code & SSD_ERRCODE_VALID)
1711 info = ntohl(*((int *) sense->ext.extended.info));
1712 else
1713 info = xs->datalen; /* bad choice if fixed blocks */
1714 if ((sense->error_code & SSD_ERRCODE) != 0x70)
1715 return -1; /* let the generic code handle it */
1716 if (st->flags & ST_FIXEDBLOCKS) {
1717 xs->resid = info * st->blksiz;
1718 if (sense->ext.extended.flags & SSD_EOM) {
1719 st->flags |= ST_EIO_PENDING;
1720 if (bp)
1721 bp->b_resid = xs->resid;
1722 }
1723 if (sense->ext.extended.flags & SSD_FILEMARK) {
1724 st->flags |= ST_AT_FILEMARK;
1725 if (bp)
1726 bp->b_resid = xs->resid;
1727 }
1728 if (sense->ext.extended.flags & SSD_ILI) {
1729 st->flags |= ST_EIO_PENDING;
1730 if (bp)
1731 bp->b_resid = xs->resid;
1732 if (sense->error_code & SSD_ERRCODE_VALID && !silent)
1733 printf("%s: block wrong size, %d blocks residual\n",
1734 st->sc_dev.dv_xname, info);
1735
1736 /*
1737 * This quirk code helps the drive read
1738 * the first tape block, regardless of
1739 * format. That is required for these
1740 * drives to return proper MODE SENSE
1741 * information.
1742 */
1743 if ((st->quirks & ST_Q_SNS_HLP) &&
1744 !(sc_link->flags & SDEV_MEDIA_LOADED))
1745 st->blksiz -= 512;
1746 }
1747 /*
1748 * If no data was tranfered, do it immediatly
1749 */
1750 if (xs->resid >= xs->datalen) {
1751 if (st->flags & ST_EIO_PENDING)
1752 return EIO;
1753 if (st->flags & ST_AT_FILEMARK) {
1754 if (bp)
1755 bp->b_resid = xs->resid;
1756 return 0;
1757 }
1758 }
1759 } else { /* must be variable mode */
1760 xs->resid = xs->datalen; /* to be sure */
1761 if (sense->ext.extended.flags & SSD_EOM)
1762 return EIO;
1763 if (sense->ext.extended.flags & SSD_FILEMARK) {
1764 if (bp)
1765 bp->b_resid = bp->b_bcount;
1766 return 0;
1767 }
1768 if (sense->ext.extended.flags & SSD_ILI) {
1769 if (info < 0) {
1770 /*
1771 * the record was bigger than the read
1772 */
1773 if (!silent)
1774 printf("%s: %d-byte record too big\n",
1775 st->sc_dev.dv_xname,
1776 xs->datalen - info);
1777 return EIO;
1778 }
1779 xs->resid = info;
1780 if (bp)
1781 bp->b_resid = info;
1782 }
1783 }
1784 key = sense->ext.extended.flags & SSD_KEY;
1785
1786 if (key == 0x8) {
1787 /*
1788 * This quirk code helps the drive read the
1789 * first tape block, regardless of format. That
1790 * is required for these drives to return proper
1791 * MODE SENSE information.
1792 */
1793 if ((st->quirks & ST_Q_SNS_HLP) &&
1794 !(sc_link->flags & SDEV_MEDIA_LOADED)) {
1795 /* still starting */
1796 st->blksiz -= 512;
1797 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
1798 st->flags |= ST_BLANK_READ;
1799 xs->resid = xs->datalen;
1800 if (bp) {
1801 bp->b_resid = xs->resid;
1802 /* return an EOF */
1803 }
1804 return 0;
1805 }
1806 }
1807 return -1; /* let the default/generic handler handle it */
1808 }
1809
1810 /*
1811 * The quirk here is that the drive returns some value to st_mode_sense
1812 * incorrectly until the tape has actually passed by the head.
1813 *
1814 * The method is to set the drive to large fixed-block state (user-specified
1815 * density and 1024-byte blocks), then read and rewind to get it to sense the
1816 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
1817 * work, as a last resort, try variable- length blocks. The result will be
1818 * the ability to do an accurate st_mode_sense.
1819 *
1820 * We know we can do a rewind because we just did a load, which implies rewind.
1821 * Rewind seems preferable to space backward if we have a virgin tape.
1822 *
1823 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
1824 * error processing, both part of st_interpret_sense.
1825 */
1826 int
1827 st_touch_tape(st)
1828 struct st_data *st;
1829 {
1830 char *buf;
1831 u_int readsiz;
1832 int error;
1833
1834 buf = malloc(1024, M_TEMP, M_NOWAIT);
1835 if (!buf)
1836 return ENOMEM;
1837
1838 if (error = st_mode_sense(st, 0))
1839 goto bad;
1840 st->blksiz = 1024;
1841 do {
1842 switch (st->blksiz) {
1843 case 512:
1844 case 1024:
1845 readsiz = st->blksiz;
1846 st->flags |= ST_FIXEDBLOCKS;
1847 break;
1848 default:
1849 readsiz = 1;
1850 st->flags &= ~ST_FIXEDBLOCKS;
1851 }
1852 if (error = st_mode_select(st, 0))
1853 goto bad;
1854 st_read(st, buf, readsiz, SCSI_SILENT);
1855 if (error = st_rewind(st, FALSE, 0)) {
1856 bad: free(buf, M_TEMP);
1857 return error;
1858 }
1859 } while (readsiz != 1 && readsiz > st->blksiz);
1860 free(buf, M_TEMP);
1861 return 0;
1862 }
1863