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