st.c revision 1.155.2.1 1 /* $NetBSD: st.c,v 1.155.2.1 2002/05/16 11:40:54 gehenna Exp $ */
2 /*-
3 * Copyright (c) 1998 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Charles M. Hannum.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the NetBSD
20 * Foundation, Inc. and its contributors.
21 * 4. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 /*
39 * Originally written by Julian Elischer (julian (at) tfs.com)
40 * for TRW Financial Systems for use under the MACH(2.5) operating system.
41 *
42 * TRW Financial Systems, in accordance with their agreement with Carnegie
43 * Mellon University, makes this software available to CMU to distribute
44 * or use in any manner that they see fit as long as this message is kept with
45 * the software. For this reason TFS also grants any other persons or
46 * organisations permission to use or modify this software.
47 *
48 * TFS supplies this software to be publicly redistributed
49 * on the understanding that TFS is not responsible for the correct
50 * functioning of this software in any circumstances.
51 *
52 * Ported to run under 386BSD by Julian Elischer (julian (at) tfs.com) Sept 1992
53 * major changes by Julian Elischer (julian (at) jules.dialix.oz.au) May 1993
54 *
55 * A lot of rewhacking done by mjacob (mjacob (at) nas.nasa.gov).
56 */
57
58 #include <sys/cdefs.h>
59 __KERNEL_RCSID(0, "$NetBSD: st.c,v 1.155.2.1 2002/05/16 11:40:54 gehenna Exp $");
60
61 #include "opt_scsi.h"
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/fcntl.h>
66 #include <sys/errno.h>
67 #include <sys/ioctl.h>
68 #include <sys/malloc.h>
69 #include <sys/buf.h>
70 #include <sys/proc.h>
71 #include <sys/user.h>
72 #include <sys/mtio.h>
73 #include <sys/device.h>
74 #include <sys/conf.h>
75 #include <sys/kernel.h>
76 #include <sys/vnode.h>
77
78 #include <dev/scsipi/scsi_all.h>
79 #include <dev/scsipi/scsi_tape.h>
80 #include <dev/scsipi/stvar.h>
81
82 /* Defines for device specific stuff */
83 #define DEF_FIXED_BSIZE 512
84
85 #define STMODE(z) ( minor(z) & 0x03)
86 #define STDSTY(z) ((minor(z) >> 2) & 0x03)
87 #define STUNIT(z) ((minor(z) >> 4) )
88 #define STNMINOR 16
89
90 #define NORMAL_MODE 0
91 #define NOREW_MODE 1
92 #define EJECT_MODE 2
93 #define CTRL_MODE 3
94
95 #define FALSE 0
96 #define TRUE 1
97
98 #ifndef ST_MOUNT_DELAY
99 #define ST_MOUNT_DELAY 0
100 #endif
101
102 dev_type_open(stopen);
103 dev_type_close(stclose);
104 dev_type_read(stread);
105 dev_type_write(stwrite);
106 dev_type_ioctl(stioctl);
107 dev_type_strategy(ststrategy);
108 dev_type_dump(stdump);
109
110 const struct bdevsw st_bdevsw = {
111 stopen, stclose, ststrategy, stioctl, stdump, nosize, D_TAPE
112 };
113
114 const struct cdevsw st_cdevsw = {
115 stopen, stclose, stread, stwrite, stioctl,
116 nostop, notty, nopoll, nommap, D_TAPE
117 };
118
119 /*
120 * Define various devices that we know mis-behave in some way,
121 * and note how they are bad, so we can correct for them
122 */
123
124 const struct st_quirk_inquiry_pattern st_quirk_patterns[] = {
125 {{T_SEQUENTIAL, T_REMOV,
126 " ", " ", " "}, {0, 0, {
127 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
128 {ST_Q_FORCE_BLKSIZE, 512, QIC_24}, /* minor 4-7 */
129 {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_1600}, /* minor 8-11 */
130 {ST_Q_FORCE_BLKSIZE, 0, HALFINCH_6250} /* minor 12-15 */
131 }}},
132 {{T_SEQUENTIAL, T_REMOV,
133 "TANDBERG", " TDC 3600 ", ""}, {0, 12, {
134 {0, 0, 0}, /* minor 0-3 */
135 {ST_Q_FORCE_BLKSIZE, 0, QIC_525}, /* minor 4-7 */
136 {0, 0, QIC_150}, /* minor 8-11 */
137 {0, 0, QIC_120} /* minor 12-15 */
138 }}},
139 {{T_SEQUENTIAL, T_REMOV,
140 "TANDBERG", " TDC 3800 ", ""}, {0, 0, {
141 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
142 {0, 0, QIC_525}, /* minor 4-7 */
143 {0, 0, QIC_150}, /* minor 8-11 */
144 {0, 0, QIC_120} /* minor 12-15 */
145 }}},
146 /*
147 * lacking a manual for the 4200, it's not clear what the
148 * specific density codes should be- the device is a 2.5GB
149 * capable QIC drive, those density codes aren't readily
150 * availabel. The 'default' will just have to do.
151 */
152 {{T_SEQUENTIAL, T_REMOV,
153 "TANDBERG", " TDC 4200 ", ""}, {0, 0, {
154 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
155 {0, 0, QIC_525}, /* minor 4-7 */
156 {0, 0, QIC_150}, /* minor 8-11 */
157 {0, 0, QIC_120} /* minor 12-15 */
158 }}},
159 /*
160 * At least -005 and -007 need this. I'll assume they all do unless I
161 * hear otherwise. - mycroft, 31MAR1994
162 */
163 {{T_SEQUENTIAL, T_REMOV,
164 "ARCHIVE ", "VIPER 2525 25462", ""}, {0, 0, {
165 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */
166 {ST_Q_SENSE_HELP, 0, QIC_525}, /* minor 4-7 */
167 {0, 0, QIC_150}, /* minor 8-11 */
168 {0, 0, QIC_120} /* minor 12-15 */
169 }}},
170 /*
171 * One user reports that this works for his tape drive. It probably
172 * needs more work. - mycroft, 09APR1994
173 */
174 {{T_SEQUENTIAL, T_REMOV,
175 "SANKYO ", "CP525 ", ""}, {0, 0, {
176 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
177 {ST_Q_FORCE_BLKSIZE, 512, QIC_525}, /* minor 4-7 */
178 {0, 0, QIC_150}, /* minor 8-11 */
179 {0, 0, QIC_120} /* minor 12-15 */
180 }}},
181 {{T_SEQUENTIAL, T_REMOV,
182 "ANRITSU ", "DMT780 ", ""}, {0, 0, {
183 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
184 {ST_Q_FORCE_BLKSIZE, 512, QIC_525}, /* minor 4-7 */
185 {0, 0, QIC_150}, /* minor 8-11 */
186 {0, 0, QIC_120} /* minor 12-15 */
187 }}},
188 {{T_SEQUENTIAL, T_REMOV,
189 "ARCHIVE ", "VIPER 150 21247", ""}, {ST_Q_ERASE_NOIMM, 12, {
190 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */
191 {0, 0, QIC_150}, /* minor 4-7 */
192 {0, 0, QIC_120}, /* minor 8-11 */
193 {0, 0, QIC_24} /* minor 12-15 */
194 }}},
195 {{T_SEQUENTIAL, T_REMOV,
196 "ARCHIVE ", "VIPER 150 21531", ""}, {ST_Q_ERASE_NOIMM, 12, {
197 {ST_Q_SENSE_HELP, 0, 0}, /* minor 0-3 */
198 {0, 0, QIC_150}, /* minor 4-7 */
199 {0, 0, QIC_120}, /* minor 8-11 */
200 {0, 0, QIC_24} /* minor 12-15 */
201 }}},
202 {{T_SEQUENTIAL, T_REMOV,
203 "WANGTEK ", "5099ES SCSI", ""}, {0, 0, {
204 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
205 {0, 0, QIC_11}, /* minor 4-7 */
206 {0, 0, QIC_24}, /* minor 8-11 */
207 {0, 0, QIC_24} /* minor 12-15 */
208 }}},
209 {{T_SEQUENTIAL, T_REMOV,
210 "WANGTEK ", "5150ES SCSI", ""}, {0, 0, {
211 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
212 {0, 0, QIC_24}, /* minor 4-7 */
213 {0, 0, QIC_120}, /* minor 8-11 */
214 {0, 0, QIC_150} /* minor 12-15 */
215 }}},
216 {{T_SEQUENTIAL, T_REMOV,
217 "WANGTEK ", "5525ES SCSI REV7", ""}, {0, 0, {
218 {0, 0, 0}, /* minor 0-3 */
219 {ST_Q_BLKSIZE, 0, QIC_525}, /* minor 4-7 */
220 {0, 0, QIC_150}, /* minor 8-11 */
221 {0, 0, QIC_120} /* minor 12-15 */
222 }}},
223 {{T_SEQUENTIAL, T_REMOV,
224 "WangDAT ", "Model 1300 ", ""}, {0, 0, {
225 {0, 0, 0}, /* minor 0-3 */
226 {ST_Q_FORCE_BLKSIZE, 512, DDS}, /* minor 4-7 */
227 {ST_Q_FORCE_BLKSIZE, 1024, DDS}, /* minor 8-11 */
228 {ST_Q_FORCE_BLKSIZE, 0, DDS} /* minor 12-15 */
229 }}},
230 {{T_SEQUENTIAL, T_REMOV,
231 "EXABYTE ", "EXB-8200 ", "263H"}, {0, 5, {
232 {0, 0, 0}, /* minor 0-3 */
233 {0, 0, 0}, /* minor 4-7 */
234 {0, 0, 0}, /* minor 8-11 */
235 {0, 0, 0} /* minor 12-15 */
236 }}},
237 {{T_SEQUENTIAL, T_REMOV,
238 "STK", "9490", ""},
239 {ST_Q_FORCE_BLKSIZE, 0, {
240 {0, 0, 0}, /* minor 0-3 */
241 {0, 0, 0}, /* minor 4-7 */
242 {0, 0, 0}, /* minor 8-11 */
243 {0, 0, 0} /* minor 12-15 */
244 }}},
245 {{T_SEQUENTIAL, T_REMOV,
246 "STK", "SD-3", ""},
247 {ST_Q_FORCE_BLKSIZE, 0, {
248 {0, 0, 0}, /* minor 0-3 */
249 {0, 0, 0}, /* minor 4-7 */
250 {0, 0, 0}, /* minor 8-11 */
251 {0, 0, 0} /* minor 12-15 */
252 }}},
253 {{T_SEQUENTIAL, T_REMOV,
254 "IBM", "03590", ""}, {ST_Q_IGNORE_LOADS, 0, {
255 {0, 0, 0}, /* minor 0-3 */
256 {0, 0, 0}, /* minor 4-7 */
257 {0, 0, 0}, /* minor 8-11 */
258 {0, 0, 0} /* minor 12-15 */
259 }}},
260 {{T_SEQUENTIAL, T_REMOV,
261 "HP ", "T4000s ", ""}, {ST_Q_UNIMODAL, 0, {
262 {0, 0, QIC_3095}, /* minor 0-3 */
263 {0, 0, QIC_3095}, /* minor 4-7 */
264 {0, 0, QIC_3095}, /* minor 8-11 */
265 {0, 0, QIC_3095}, /* minor 12-15 */
266 }}},
267 #if 0
268 {{T_SEQUENTIAL, T_REMOV,
269 "EXABYTE ", "EXB-8200 ", ""}, {0, 12, {
270 {0, 0, 0}, /* minor 0-3 */
271 {0, 0, 0}, /* minor 4-7 */
272 {0, 0, 0}, /* minor 8-11 */
273 {0, 0, 0} /* minor 12-15 */
274 }}},
275 #endif
276 {{T_SEQUENTIAL, T_REMOV,
277 "TEAC ", "MT-2ST/N50 ", ""}, {ST_Q_IGNORE_LOADS, 0, {
278 {0, 0, 0}, /* minor 0-3 */
279 {0, 0, 0}, /* minor 4-7 */
280 {0, 0, 0}, /* minor 8-11 */
281 {0, 0, 0} /* minor 12-15 */
282 }}},
283 {{T_SEQUENTIAL, T_REMOV,
284 "OnStream", "ADR50 Drive", ""}, {ST_Q_UNIMODAL, 0, {
285 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 0-3 */
286 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 4-7 */
287 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 8-11 */
288 {ST_Q_FORCE_BLKSIZE, 512, 0}, /* minor 12-15 */
289 }}},
290 {{T_SEQUENTIAL, T_REMOV,
291 "OnStream DI-30", "", "1.0"}, {ST_Q_NOFILEMARKS, 0, {
292 {0, 0, 0}, /* minor 0-3 */
293 {0, 0, 0}, /* minor 4-7 */
294 {0, 0, 0}, /* minor 8-11 */
295 {0, 0, 0} /* minor 12-15 */
296 }}},
297 {{T_SEQUENTIAL, T_REMOV,
298 "NCR H621", "0-STD-03-46F880 ", ""}, {ST_Q_NOPREVENT, 0, {
299 {0, 0, 0}, /* minor 0-3 */
300 {0, 0, 0}, /* minor 4-7 */
301 {0, 0, 0}, /* minor 8-11 */
302 {0, 0, 0} /* minor 12-15 */
303 }}},
304 };
305
306 #define NOEJECT 0
307 #define EJECT 1
308
309 void st_identify_drive __P((struct st_softc *,
310 struct scsipi_inquiry_pattern *));
311 void st_loadquirks __P((struct st_softc *));
312 int st_mount_tape __P((dev_t, int));
313 void st_unmount __P((struct st_softc *, boolean));
314 int st_decide_mode __P((struct st_softc *, boolean));
315 void ststart __P((struct scsipi_periph *));
316 void stdone __P((struct scsipi_xfer *));
317 int st_read __P((struct st_softc *, char *, int, int));
318 int st_space __P((struct st_softc *, int, u_int, int));
319 int st_write_filemarks __P((struct st_softc *, int, int));
320 int st_check_eod __P((struct st_softc *, boolean, int *, int));
321 int st_load __P((struct st_softc *, u_int, int));
322 int st_rewind __P((struct st_softc *, u_int, int));
323 int st_interpret_sense __P((struct scsipi_xfer *));
324 int st_touch_tape __P((struct st_softc *));
325 int st_erase __P((struct st_softc *, int full, int flags));
326 int st_rdpos __P((struct st_softc *, int, u_int32_t *));
327 int st_setpos __P((struct st_softc *, int, u_int32_t *));
328
329 const struct scsipi_periphsw st_switch = {
330 st_interpret_sense,
331 ststart,
332 NULL,
333 stdone
334 };
335
336 #if defined(ST_ENABLE_EARLYWARN)
337 #define ST_INIT_FLAGS ST_EARLYWARN
338 #else
339 #define ST_INIT_FLAGS 0
340 #endif
341
342 /*
343 * The routine called by the low level scsi routine when it discovers
344 * A device suitable for this driver
345 */
346 void
347 stattach(parent, st, aux)
348 struct device *parent;
349 struct st_softc *st;
350 void *aux;
351 {
352 struct scsipibus_attach_args *sa = aux;
353 struct scsipi_periph *periph = sa->sa_periph;
354
355 SC_DEBUG(periph, SCSIPI_DB2, ("stattach: "));
356
357 /*
358 * Store information needed to contact our base driver
359 */
360 st->sc_periph = periph;
361 periph->periph_dev = &st->sc_dev;
362 periph->periph_switch = &st_switch;
363
364 /*
365 * Set initial flags
366 */
367
368 st->flags = ST_INIT_FLAGS;
369
370 /*
371 * Check if the drive is a known criminal and take
372 * Any steps needed to bring it into line
373 */
374 st_identify_drive(st, &sa->sa_inqbuf);
375 /*
376 * Use the subdriver to request information regarding the drive.
377 */
378 printf("\n");
379 printf("%s: %s", st->sc_dev.dv_xname, st->quirkdata ? "rogue, " : "");
380 if (scsipi_test_unit_ready(periph,
381 XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE) ||
382 st->ops(st, ST_OPS_MODESENSE,
383 XS_CTL_DISCOVERY | XS_CTL_SILENT | XS_CTL_IGNORE_MEDIA_CHANGE))
384 printf("drive empty\n");
385 else {
386 printf("density code %d, ", st->media_density);
387 if (st->media_blksize > 0)
388 printf("%d-byte", st->media_blksize);
389 else
390 printf("variable");
391 printf(" blocks, write-%s\n",
392 (st->flags & ST_READONLY) ? "protected" : "enabled");
393 }
394
395 /*
396 * Set up the buf queue for this device
397 */
398 BUFQ_INIT(&st->buf_queue);
399
400 #if NRND > 0
401 rnd_attach_source(&st->rnd_source, st->sc_dev.dv_xname,
402 RND_TYPE_TAPE, 0);
403 #endif
404 }
405
406 int
407 stactivate(self, act)
408 struct device *self;
409 enum devact act;
410 {
411 int rv = 0;
412
413 switch (act) {
414 case DVACT_ACTIVATE:
415 rv = EOPNOTSUPP;
416 break;
417
418 case DVACT_DEACTIVATE:
419 /*
420 * Nothing to do; we key off the device's DVF_ACTIVE.
421 */
422 break;
423 }
424 return (rv);
425 }
426
427 int
428 stdetach(self, flags)
429 struct device *self;
430 int flags;
431 {
432 struct st_softc *st = (struct st_softc *)self;
433 struct buf *bp;
434 int s, bmaj, cmaj, mn;
435
436 /* locate the major number */
437 bmaj = bdevsw_lookup_major(&st_bdevsw);
438 cmaj = cdevsw_lookup_major(&st_cdevsw);
439
440 s = splbio();
441
442 /* Kill off any queued buffers. */
443 while ((bp = BUFQ_FIRST(&st->buf_queue)) != NULL) {
444 BUFQ_REMOVE(&st->buf_queue, bp);
445 bp->b_error = EIO;
446 bp->b_flags |= B_ERROR;
447 bp->b_resid = bp->b_bcount;
448 biodone(bp);
449 }
450
451 /* Kill off any pending commands. */
452 scsipi_kill_pending(st->sc_periph);
453
454 splx(s);
455
456 /* Nuke the vnodes for any open instances */
457 mn = STUNIT(self->dv_unit);
458 vdevgone(bmaj, mn, mn+STNMINOR-1, VBLK);
459 vdevgone(cmaj, mn, mn+STNMINOR-1, VCHR);
460
461
462 #if NRND > 0
463 /* Unhook the entropy source. */
464 rnd_detach_source(&st->rnd_source);
465 #endif
466
467 return (0);
468 }
469
470 /*
471 * Use the inquiry routine in 'scsi_base' to get drive info so we can
472 * Further tailor our behaviour.
473 */
474 void
475 st_identify_drive(st, inqbuf)
476 struct st_softc *st;
477 struct scsipi_inquiry_pattern *inqbuf;
478 {
479 struct st_quirk_inquiry_pattern *finger;
480 int priority;
481
482 finger = (struct st_quirk_inquiry_pattern *)scsipi_inqmatch(inqbuf,
483 (caddr_t)st_quirk_patterns,
484 sizeof(st_quirk_patterns) / sizeof(st_quirk_patterns[0]),
485 sizeof(st_quirk_patterns[0]), &priority);
486 if (priority != 0) {
487 st->quirkdata = &finger->quirkdata;
488 st->drive_quirks = finger->quirkdata.quirks;
489 st->quirks = finger->quirkdata.quirks; /* start value */
490 st->page_0_size = finger->quirkdata.page_0_size;
491 st_loadquirks(st);
492 }
493 }
494
495 /*
496 * initialise the subdevices to the default (QUIRK) state.
497 * this will remove any setting made by the system operator or previous
498 * operations.
499 */
500 void
501 st_loadquirks(st)
502 struct st_softc *st;
503 {
504 int i;
505 struct modes *mode;
506 struct modes *mode2;
507
508 mode = st->quirkdata->modes;
509 mode2 = st->modes;
510 for (i = 0; i < 4; i++) {
511 memset(mode2, 0, sizeof(struct modes));
512 st->modeflags[i] &= ~(BLKSIZE_SET_BY_QUIRK |
513 DENSITY_SET_BY_QUIRK | BLKSIZE_SET_BY_USER |
514 DENSITY_SET_BY_USER);
515 if ((mode->quirks | st->drive_quirks) & ST_Q_FORCE_BLKSIZE) {
516 mode2->blksize = mode->blksize;
517 st->modeflags[i] |= BLKSIZE_SET_BY_QUIRK;
518 }
519 if (mode->density) {
520 mode2->density = mode->density;
521 st->modeflags[i] |= DENSITY_SET_BY_QUIRK;
522 }
523 mode2->quirks |= mode->quirks;
524 mode++;
525 mode2++;
526 }
527 }
528
529 /*
530 * open the device.
531 */
532 int
533 stopen(dev, flags, mode, p)
534 dev_t dev;
535 int flags;
536 int mode;
537 struct proc *p;
538 {
539 u_int stmode, dsty;
540 int error, sflags, unit, tries, ntries;
541 struct st_softc *st;
542 struct scsipi_periph *periph;
543 struct scsipi_adapter *adapt;
544
545 unit = STUNIT(dev);
546 if (unit >= st_cd.cd_ndevs)
547 return (ENXIO);
548 st = st_cd.cd_devs[unit];
549 if (st == NULL)
550 return (ENXIO);
551
552 stmode = STMODE(dev);
553 dsty = STDSTY(dev);
554
555 periph = st->sc_periph;
556 adapt = periph->periph_channel->chan_adapter;
557
558 SC_DEBUG(periph, SCSIPI_DB1, ("open: dev=0x%x (unit %d (of %d))\n", dev,
559 unit, st_cd.cd_ndevs));
560
561
562 /*
563 * Only allow one at a time
564 */
565 if (periph->periph_flags & PERIPH_OPEN) {
566 printf("%s: already open\n", st->sc_dev.dv_xname);
567 return (EBUSY);
568 }
569
570 if ((error = scsipi_adapter_addref(adapt)) != 0)
571 return (error);
572
573 /*
574 * clear any latched errors.
575 */
576 st->mt_resid = 0;
577 st->mt_erreg = 0;
578 st->asc = 0;
579 st->ascq = 0;
580
581 /*
582 * Catch any unit attention errors. Be silent about this
583 * unless we're already mounted. We ignore media change
584 * if we're in control mode or not mounted yet.
585 */
586 if ((st->flags & ST_MOUNTED) == 0 || stmode == CTRL_MODE) {
587 #ifdef SCSIDEBUG
588 sflags = XS_CTL_IGNORE_MEDIA_CHANGE;
589 #else
590 sflags = XS_CTL_SILENT|XS_CTL_IGNORE_MEDIA_CHANGE;
591 #endif
592 } else
593 sflags = 0;
594
595 /*
596 * If we're already mounted or we aren't configured for
597 * a mount delay, only try a test unit ready once. Otherwise,
598 * try up to ST_MOUNT_DELAY times with a rest interval of
599 * one second between each try.
600 */
601
602 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0) {
603 ntries = 1;
604 } else {
605 ntries = ST_MOUNT_DELAY;
606 }
607
608 for (error = tries = 0; tries < ntries; tries++) {
609 int slpintr, oflags;
610
611 /*
612 * If we had no error, or we're opening the control mode
613 * device, we jump out right away.
614 */
615
616 error = scsipi_test_unit_ready(periph, sflags);
617 if (error == 0 || stmode == CTRL_MODE) {
618 break;
619 }
620
621 /*
622 * We had an error.
623 *
624 * If we're already mounted or we aren't configured for
625 * a mount delay, or the error isn't a NOT READY error,
626 * skip to the error exit now.
627 */
628 if ((st->flags & ST_MOUNTED) || ST_MOUNT_DELAY == 0 ||
629 (st->mt_key != SKEY_NOT_READY)) {
630 goto bad;
631 }
632
633 /*
634 * clear any latched errors.
635 */
636 st->mt_resid = 0;
637 st->mt_erreg = 0;
638 st->asc = 0;
639 st->ascq = 0;
640
641 /*
642 * Fake that we have the device open so
643 * we block other apps from getting in.
644 */
645
646 oflags = periph->periph_flags;
647 periph->periph_flags |= PERIPH_OPEN;
648
649 slpintr = tsleep(&lbolt, PUSER|PCATCH, "stload", 0);
650
651 periph->periph_flags = oflags; /* restore flags */
652 if (slpintr) {
653 goto bad;
654 }
655 }
656
657
658 /*
659 * If the mode is 3 (e.g. minor = 3,7,11,15) then the device has
660 * been opened to set defaults and perform other, usually non-I/O
661 * related, operations. In this case, do a quick check to see
662 * whether the unit actually had a tape loaded (this will be known
663 * as to whether or not we got a NOT READY for the above
664 * unit attention). If a tape is there, go do a mount sequence.
665 */
666 if (stmode == CTRL_MODE && st->mt_key == SKEY_NOT_READY) {
667 periph->periph_flags |= PERIPH_OPEN;
668 return (0);
669 }
670
671 /*
672 * If we get this far and had an error set, that means we failed
673 * to pass the 'test unit ready' test for the non-controlmode device,
674 * so we bounce the open.
675 */
676
677 if (error)
678 return (error);
679
680 /*
681 * Else, we're now committed to saying we're open.
682 */
683
684 periph->periph_flags |= PERIPH_OPEN; /* unit attn are now errors */
685
686 /*
687 * If it's a different mode, or if the media has been
688 * invalidated, unmount the tape from the previous
689 * session but continue with open processing
690 */
691 if (st->last_dsty != dsty ||
692 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
693 st_unmount(st, NOEJECT);
694
695 /*
696 * If we are not mounted, then we should start a new
697 * mount session.
698 */
699 if (!(st->flags & ST_MOUNTED)) {
700 if ((error = st_mount_tape(dev, flags)) != 0)
701 goto bad;
702 st->last_dsty = dsty;
703 }
704 if (!(st->quirks & ST_Q_NOPREVENT)) {
705 scsipi_prevent(periph, PR_PREVENT,
706 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
707 }
708
709 SC_DEBUG(periph, SCSIPI_DB2, ("open complete\n"));
710 return (0);
711
712 bad:
713 st_unmount(st, NOEJECT);
714 scsipi_adapter_delref(adapt);
715 periph->periph_flags &= ~PERIPH_OPEN;
716 return (error);
717 }
718
719 /*
720 * close the device.. only called if we are the LAST
721 * occurence of an open device
722 */
723 int
724 stclose(dev, flags, mode, p)
725 dev_t dev;
726 int flags;
727 int mode;
728 struct proc *p;
729 {
730 int stxx, error = 0;
731 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
732 struct scsipi_periph *periph = st->sc_periph;
733 struct scsipi_adapter *adapt = periph->periph_channel->chan_adapter;
734
735 SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("closing\n"));
736
737 /*
738 * Make sure that a tape opened in write-only mode will have
739 * file marks written on it when closed, even if not written to.
740 *
741 * This is for SUN compatibility. Actually, the Sun way of
742 * things is to:
743 *
744 * only write filemarks if there are fmks to be written and
745 * - open for write (possibly read/write)
746 * - the last operation was a write
747 * or:
748 * - opened for wronly
749 * - no data was written (including filemarks)
750 */
751
752 stxx = st->flags & (ST_WRITTEN | ST_FM_WRITTEN);
753 if (((flags & FWRITE) && stxx == ST_WRITTEN) ||
754 ((flags & O_ACCMODE) == FWRITE && stxx == 0)) {
755 int nm;
756 error = st_check_eod(st, FALSE, &nm, 0);
757 }
758
759 /*
760 * Allow robots to eject tape if needed.
761 */
762 scsipi_prevent(periph, PR_ALLOW,
763 XS_CTL_IGNORE_ILLEGAL_REQUEST | XS_CTL_IGNORE_NOT_READY);
764
765 switch (STMODE(dev)) {
766 case NORMAL_MODE:
767 st_unmount(st, NOEJECT);
768 break;
769 case NOREW_MODE:
770 case CTRL_MODE:
771 /*
772 * Leave mounted unless media seems to have been removed.
773 *
774 * Otherwise, if we're to terminate a tape with more than one
775 * filemark [ and because we're not rewinding here ], backspace
776 * one filemark so that later appends will see an unbroken
777 * sequence of:
778 *
779 * file - FMK - file - FMK ... file - FMK FMK (EOM)
780 */
781 if ((periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
782 st_unmount(st, NOEJECT);
783 } else if (error == 0) {
784 /*
785 * ST_WRITTEN was preserved from above.
786 *
787 * All we need to know here is:
788 *
789 * Were we writing this tape and was the last
790 * operation a write?
791 *
792 * Are there supposed to be 2FM at EOD?
793 *
794 * If both statements are true, then we backspace
795 * one filemark.
796 */
797 stxx |= (st->flags & ST_2FM_AT_EOD);
798 if ((flags & FWRITE) != 0 &&
799 (stxx == (ST_2FM_AT_EOD|ST_WRITTEN))) {
800 error = st_space(st, -1, SP_FILEMARKS, 0);
801 }
802 }
803 break;
804 case EJECT_MODE:
805 st_unmount(st, EJECT);
806 break;
807 }
808
809 scsipi_wait_drain(periph);
810
811 scsipi_adapter_delref(adapt);
812 periph->periph_flags &= ~PERIPH_OPEN;
813
814 return (error);
815 }
816
817 /*
818 * Start a new mount session.
819 * Copy in all the default parameters from the selected device mode.
820 * and try guess any that seem to be defaulted.
821 */
822 int
823 st_mount_tape(dev, flags)
824 dev_t dev;
825 int flags;
826 {
827 int unit;
828 u_int dsty;
829 struct st_softc *st;
830 struct scsipi_periph *periph;
831 int error = 0;
832
833 unit = STUNIT(dev);
834 dsty = STDSTY(dev);
835 st = st_cd.cd_devs[unit];
836 periph = st->sc_periph;
837
838 if (st->flags & ST_MOUNTED)
839 return (0);
840
841 SC_DEBUG(periph, SCSIPI_DB1, ("mounting\n "));
842 st->flags |= ST_NEW_MOUNT;
843 st->quirks = st->drive_quirks | st->modes[dsty].quirks;
844 /*
845 * If the media is new, then make sure we give it a chance to
846 * to do a 'load' instruction. (We assume it is new.)
847 */
848 if ((error = st_load(st, LD_LOAD, XS_CTL_SILENT)) != 0)
849 return (error);
850 /*
851 * Throw another dummy instruction to catch
852 * 'Unit attention' errors. Many drives give
853 * these after doing a Load instruction (with
854 * the MEDIUM MAY HAVE CHANGED asc/ascq).
855 */
856 scsipi_test_unit_ready(periph, XS_CTL_SILENT); /* XXX */
857
858 /*
859 * Some devices can't tell you much until they have been
860 * asked to look at the media. This quirk does this.
861 */
862 if (st->quirks & ST_Q_SENSE_HELP)
863 if ((error = st_touch_tape(st)) != 0)
864 return (error);
865 /*
866 * Load the physical device parameters
867 * loads: blkmin, blkmax
868 */
869 if ((error = st->ops(st, ST_OPS_RBL, 0)) != 0)
870 return (error);
871 /*
872 * Load the media dependent parameters
873 * includes: media_blksize,media_density,numblks
874 * As we have a tape in, it should be reflected here.
875 * If not you may need the "quirk" above.
876 */
877 if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
878 return (error);
879 /*
880 * If we have gained a permanent density from somewhere,
881 * then use it in preference to the one supplied by
882 * default by the driver.
883 */
884 if (st->modeflags[dsty] & (DENSITY_SET_BY_QUIRK | DENSITY_SET_BY_USER))
885 st->density = st->modes[dsty].density;
886 else
887 st->density = st->media_density;
888 /*
889 * If we have gained a permanent blocksize
890 * then use it in preference to the one supplied by
891 * default by the driver.
892 */
893 st->flags &= ~ST_FIXEDBLOCKS;
894 if (st->modeflags[dsty] &
895 (BLKSIZE_SET_BY_QUIRK | BLKSIZE_SET_BY_USER)) {
896 st->blksize = st->modes[dsty].blksize;
897 if (st->blksize)
898 st->flags |= ST_FIXEDBLOCKS;
899 } else {
900 if ((error = st_decide_mode(st, FALSE)) != 0)
901 return (error);
902 }
903 if ((error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
904 /* ATAPI will return ENODEV for this, and this may be OK */
905 if (error != ENODEV) {
906 printf("%s: cannot set selected mode\n",
907 st->sc_dev.dv_xname);
908 return (error);
909 }
910 }
911 st->flags &= ~ST_NEW_MOUNT;
912 st->flags |= ST_MOUNTED;
913 periph->periph_flags |= PERIPH_MEDIA_LOADED; /* move earlier? */
914 st->blkno = st->fileno = (daddr_t) 0;
915 return (0);
916 }
917
918 /*
919 * End the present mount session.
920 * Rewind, and optionally eject the tape.
921 * Reset various flags to indicate that all new
922 * operations require another mount operation
923 */
924 void
925 st_unmount(st, eject)
926 struct st_softc *st;
927 boolean eject;
928 {
929 struct scsipi_periph *periph = st->sc_periph;
930 int nmarks;
931
932 if ((st->flags & ST_MOUNTED) == 0)
933 return;
934 SC_DEBUG(periph, SCSIPI_DB1, ("unmounting\n"));
935 st_check_eod(st, FALSE, &nmarks, XS_CTL_IGNORE_NOT_READY);
936 st_rewind(st, 0, XS_CTL_IGNORE_NOT_READY);
937
938 /*
939 * Section 9.3.3 of the SCSI specs states that a device shall return
940 * the density value specified in the last succesfull MODE SELECT
941 * after an unload operation, in case it is not able to
942 * automatically determine the density of the new medium.
943 *
944 * So we instruct the device to use the default density, which will
945 * prevent the use of stale density values (in particular,
946 * in st_touch_tape().
947 */
948 st->density = 0;
949 if (st->ops(st, ST_OPS_MODESELECT, 0) != 0) {
950 printf("%s: WARNING: cannot revert to default density\n",
951 st->sc_dev.dv_xname);
952 }
953
954 if (eject) {
955 if (!(st->quirks & ST_Q_NOPREVENT)) {
956 scsipi_prevent(periph, PR_ALLOW,
957 XS_CTL_IGNORE_ILLEGAL_REQUEST |
958 XS_CTL_IGNORE_NOT_READY);
959 }
960 st_load(st, LD_UNLOAD, XS_CTL_IGNORE_NOT_READY);
961 st->blkno = st->fileno = (daddr_t) -1;
962 } else {
963 st->blkno = st->fileno = (daddr_t) 0;
964 }
965 st->flags &= ~(ST_MOUNTED | ST_NEW_MOUNT);
966 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
967 }
968
969 /*
970 * Given all we know about the device, media, mode, 'quirks' and
971 * initial operation, make a decision as to how we should be set
972 * to run (regarding blocking and EOD marks)
973 */
974 int
975 st_decide_mode(st, first_read)
976 struct st_softc *st;
977 boolean first_read;
978 {
979
980 SC_DEBUG(st->sc_periph, SCSIPI_DB2, ("starting block mode decision\n"));
981
982 /*
983 * If the drive can only handle fixed-length blocks and only at
984 * one size, perhaps we should just do that.
985 */
986 if (st->blkmin && (st->blkmin == st->blkmax)) {
987 st->flags |= ST_FIXEDBLOCKS;
988 st->blksize = st->blkmin;
989 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
990 ("blkmin == blkmax of %d\n", st->blkmin));
991 goto done;
992 }
993 /*
994 * If the tape density mandates (or even suggests) use of fixed
995 * or variable-length blocks, comply.
996 */
997 switch (st->density) {
998 case HALFINCH_800:
999 case HALFINCH_1600:
1000 case HALFINCH_6250:
1001 case DDS:
1002 st->flags &= ~ST_FIXEDBLOCKS;
1003 st->blksize = 0;
1004 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1005 ("density specified variable\n"));
1006 goto done;
1007 case QIC_11:
1008 case QIC_24:
1009 case QIC_120:
1010 case QIC_150:
1011 case QIC_525:
1012 case QIC_1320:
1013 case QIC_3095:
1014 case QIC_3220:
1015 st->flags |= ST_FIXEDBLOCKS;
1016 if (st->media_blksize > 0)
1017 st->blksize = st->media_blksize;
1018 else
1019 st->blksize = DEF_FIXED_BSIZE;
1020 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1021 ("density specified fixed\n"));
1022 goto done;
1023 }
1024 /*
1025 * If we're about to read the tape, perhaps we should choose
1026 * fixed or variable-length blocks and block size according to
1027 * what the drive found on the tape.
1028 */
1029 if (first_read &&
1030 (!(st->quirks & ST_Q_BLKSIZE) || (st->media_blksize == 0) ||
1031 (st->media_blksize == DEF_FIXED_BSIZE) ||
1032 (st->media_blksize == 1024))) {
1033 if (st->media_blksize > 0)
1034 st->flags |= ST_FIXEDBLOCKS;
1035 else
1036 st->flags &= ~ST_FIXEDBLOCKS;
1037 st->blksize = st->media_blksize;
1038 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1039 ("Used media_blksize of %d\n", st->media_blksize));
1040 goto done;
1041 }
1042 /*
1043 * We're getting no hints from any direction. Choose variable-
1044 * length blocks arbitrarily.
1045 */
1046 st->flags &= ~ST_FIXEDBLOCKS;
1047 st->blksize = 0;
1048 SC_DEBUG(st->sc_periph, SCSIPI_DB3,
1049 ("Give up and default to variable mode\n"));
1050
1051 done:
1052 /*
1053 * Decide whether or not to write two file marks to signify end-
1054 * of-data. Make the decision as a function of density. If
1055 * the decision is not to use a second file mark, the SCSI BLANK
1056 * CHECK condition code will be recognized as end-of-data when
1057 * first read.
1058 * (I think this should be a by-product of fixed/variable..julian)
1059 */
1060 switch (st->density) {
1061 /* case 8 mm: What is the SCSI density code for 8 mm, anyway? */
1062 case QIC_11:
1063 case QIC_24:
1064 case QIC_120:
1065 case QIC_150:
1066 case QIC_525:
1067 case QIC_1320:
1068 case QIC_3095:
1069 case QIC_3220:
1070 st->flags &= ~ST_2FM_AT_EOD;
1071 break;
1072 default:
1073 st->flags |= ST_2FM_AT_EOD;
1074 }
1075 return (0);
1076 }
1077
1078 /*
1079 * Actually translate the requested transfer into
1080 * one the physical driver can understand
1081 * The transfer is described by a buf and will include
1082 * only one physical transfer.
1083 */
1084 void
1085 ststrategy(bp)
1086 struct buf *bp;
1087 {
1088 struct st_softc *st = st_cd.cd_devs[STUNIT(bp->b_dev)];
1089 int s;
1090
1091 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
1092 ("ststrategy %ld bytes @ blk %d\n", bp->b_bcount, bp->b_blkno));
1093 /*
1094 * If it's a null transfer, return immediatly
1095 */
1096 if (bp->b_bcount == 0)
1097 goto done;
1098
1099 /* If offset is negative, error */
1100 if (bp->b_blkno < 0) {
1101 bp->b_error = EINVAL;
1102 goto bad;
1103 }
1104
1105 /*
1106 * Odd sized request on fixed drives are verboten
1107 */
1108 if (st->flags & ST_FIXEDBLOCKS) {
1109 if (bp->b_bcount % st->blksize) {
1110 printf("%s: bad request, must be multiple of %d\n",
1111 st->sc_dev.dv_xname, st->blksize);
1112 bp->b_error = EIO;
1113 goto bad;
1114 }
1115 }
1116 /*
1117 * as are out-of-range requests on variable drives.
1118 */
1119 else if (bp->b_bcount < st->blkmin ||
1120 (st->blkmax && bp->b_bcount > st->blkmax)) {
1121 printf("%s: bad request, must be between %d and %d\n",
1122 st->sc_dev.dv_xname, st->blkmin, st->blkmax);
1123 bp->b_error = EIO;
1124 goto bad;
1125 }
1126 s = splbio();
1127
1128 /*
1129 * Place it in the queue of activities for this tape
1130 * at the end (a bit silly because we only have on user..
1131 * (but it could fork()))
1132 */
1133 BUFQ_INSERT_TAIL(&st->buf_queue, bp);
1134
1135 /*
1136 * Tell the device to get going on the transfer if it's
1137 * not doing anything, otherwise just wait for completion
1138 * (All a bit silly if we're only allowing 1 open but..)
1139 */
1140 ststart(st->sc_periph);
1141
1142 splx(s);
1143 return;
1144 bad:
1145 bp->b_flags |= B_ERROR;
1146 done:
1147 /*
1148 * Correctly set the buf to indicate a completed xfer
1149 */
1150 bp->b_resid = bp->b_bcount;
1151 biodone(bp);
1152 return;
1153 }
1154
1155 /*
1156 * ststart looks to see if there is a buf waiting for the device
1157 * and that the device is not already busy. If both are true,
1158 * It dequeues the buf and creates a scsi command to perform the
1159 * transfer required. The transfer request will call scsipi_done
1160 * on completion, which will in turn call this routine again
1161 * so that the next queued transfer is performed.
1162 * The bufs are queued by the strategy routine (ststrategy)
1163 *
1164 * This routine is also called after other non-queued requests
1165 * have been made of the scsi driver, to ensure that the queue
1166 * continues to be drained.
1167 * ststart() is called at splbio
1168 */
1169 void
1170 ststart(periph)
1171 struct scsipi_periph *periph;
1172 {
1173 struct st_softc *st = (void *)periph->periph_dev;
1174 struct buf *bp;
1175 struct scsi_rw_tape cmd;
1176 int flags, error;
1177
1178 SC_DEBUG(periph, SCSIPI_DB2, ("ststart "));
1179 /*
1180 * See if there is a buf to do and we are not already
1181 * doing one
1182 */
1183 while (periph->periph_active < periph->periph_openings) {
1184 /* if a special awaits, let it proceed first */
1185 if (periph->periph_flags & PERIPH_WAITING) {
1186 periph->periph_flags &= ~PERIPH_WAITING;
1187 wakeup((caddr_t)periph);
1188 return;
1189 }
1190
1191 if ((bp = BUFQ_FIRST(&st->buf_queue)) == NULL)
1192 return;
1193 BUFQ_REMOVE(&st->buf_queue, bp);
1194
1195 /*
1196 * If the device has been unmounted by the user
1197 * then throw away all requests until done.
1198 */
1199 if ((st->flags & ST_MOUNTED) == 0 ||
1200 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
1201 /* make sure that one implies the other.. */
1202 periph->periph_flags &= ~PERIPH_MEDIA_LOADED;
1203 bp->b_flags |= B_ERROR;
1204 bp->b_error = EIO;
1205 bp->b_resid = bp->b_bcount;
1206 biodone(bp);
1207 continue;
1208 }
1209 /*
1210 * only FIXEDBLOCK devices have pending I/O or space operations.
1211 */
1212 if (st->flags & ST_FIXEDBLOCKS) {
1213 /*
1214 * If we are at a filemark but have not reported it yet
1215 * then we should report it now
1216 */
1217 if (st->flags & ST_AT_FILEMARK) {
1218 if ((bp->b_flags & B_READ) == B_WRITE) {
1219 /*
1220 * Handling of ST_AT_FILEMARK in
1221 * st_space will fill in the right file
1222 * mark count.
1223 * Back up over filemark
1224 */
1225 if (st_space(st, 0, SP_FILEMARKS, 0)) {
1226 bp->b_flags |= B_ERROR;
1227 bp->b_error = EIO;
1228 biodone(bp);
1229 continue;
1230 }
1231 } else {
1232 bp->b_resid = bp->b_bcount;
1233 bp->b_error = 0;
1234 bp->b_flags &= ~B_ERROR;
1235 st->flags &= ~ST_AT_FILEMARK;
1236 biodone(bp);
1237 continue; /* seek more work */
1238 }
1239 }
1240 }
1241 /*
1242 * If we are at EOM but have not reported it
1243 * yet then we should report it now.
1244 */
1245 if (st->flags & (ST_EOM_PENDING|ST_EIO_PENDING)) {
1246 bp->b_resid = bp->b_bcount;
1247 if (st->flags & ST_EIO_PENDING) {
1248 bp->b_error = EIO;
1249 bp->b_flags |= B_ERROR;
1250 }
1251 st->flags &= ~(ST_EOM_PENDING|ST_EIO_PENDING);
1252 biodone(bp);
1253 continue; /* seek more work */
1254 }
1255
1256 /*
1257 * Fill out the scsi command
1258 */
1259 memset(&cmd, 0, sizeof(cmd));
1260 flags = XS_CTL_NOSLEEP | XS_CTL_ASYNC;
1261 if ((bp->b_flags & B_READ) == B_WRITE) {
1262 cmd.opcode = WRITE;
1263 st->flags &= ~ST_FM_WRITTEN;
1264 flags |= XS_CTL_DATA_OUT;
1265 } else {
1266 cmd.opcode = READ;
1267 flags |= XS_CTL_DATA_IN;
1268 }
1269
1270 /*
1271 * Handle "fixed-block-mode" tape drives by using the
1272 * block count instead of the length.
1273 */
1274 if (st->flags & ST_FIXEDBLOCKS) {
1275 cmd.byte2 |= SRW_FIXED;
1276 _lto3b(bp->b_bcount / st->blksize, cmd.len);
1277 } else
1278 _lto3b(bp->b_bcount, cmd.len);
1279
1280 /*
1281 * Clear 'position updated' indicator
1282 */
1283 st->flags &= ~ST_POSUPDATED;
1284
1285 /*
1286 * go ask the adapter to do all this for us
1287 */
1288 error = scsipi_command(periph,
1289 (struct scsipi_generic *)&cmd, sizeof(cmd),
1290 (u_char *)bp->b_data, bp->b_bcount,
1291 0, ST_IO_TIME, bp, flags);
1292 if (error) {
1293 printf("%s: not queued, error %d\n",
1294 st->sc_dev.dv_xname, error);
1295 }
1296 } /* go back and see if we can cram more work in.. */
1297 }
1298
1299 void
1300 stdone(xs)
1301 struct scsipi_xfer *xs;
1302 {
1303 struct st_softc *st = (void *)xs->xs_periph->periph_dev;
1304
1305 if (xs->bp != NULL) {
1306 if ((xs->bp->b_flags & B_READ) == B_WRITE) {
1307 st->flags |= ST_WRITTEN;
1308 } else {
1309 st->flags &= ~ST_WRITTEN;
1310 }
1311 #if NRND > 0
1312 rnd_add_uint32(&st->rnd_source, xs->bp->b_blkno);
1313 #endif
1314
1315 if ((st->flags & ST_POSUPDATED) == 0) {
1316 if (xs->bp->b_flags & B_ERROR) {
1317 st->fileno = st->blkno = -1;
1318 } else if (st->blkno != -1) {
1319 if (st->flags & ST_FIXEDBLOCKS) {
1320 st->blkno +=
1321 (xs->bp->b_bcount / st->blksize);
1322 } else {
1323 st->blkno++;
1324 }
1325 }
1326 }
1327 }
1328 }
1329
1330 int
1331 stread(dev, uio, iomode)
1332 dev_t dev;
1333 struct uio *uio;
1334 int iomode;
1335 {
1336 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1337
1338 return (physio(ststrategy, NULL, dev, B_READ,
1339 st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
1340 }
1341
1342 int
1343 stwrite(dev, uio, iomode)
1344 dev_t dev;
1345 struct uio *uio;
1346 int iomode;
1347 {
1348 struct st_softc *st = st_cd.cd_devs[STUNIT(dev)];
1349
1350 return (physio(ststrategy, NULL, dev, B_WRITE,
1351 st->sc_periph->periph_channel->chan_adapter->adapt_minphys, uio));
1352 }
1353
1354 /*
1355 * Perform special action on behalf of the user;
1356 * knows about the internals of this device
1357 */
1358 int
1359 stioctl(dev, cmd, arg, flag, p)
1360 dev_t dev;
1361 u_long cmd;
1362 caddr_t arg;
1363 int flag;
1364 struct proc *p;
1365 {
1366 int error = 0;
1367 int unit;
1368 int number, nmarks, dsty;
1369 int flags;
1370 struct st_softc *st;
1371 int hold_blksize;
1372 u_int8_t hold_density;
1373 struct mtop *mt = (struct mtop *) arg;
1374
1375 /*
1376 * Find the device that the user is talking about
1377 */
1378 flags = 0; /* give error messages, act on errors etc. */
1379 unit = STUNIT(dev);
1380 dsty = STDSTY(dev);
1381 st = st_cd.cd_devs[unit];
1382 hold_blksize = st->blksize;
1383 hold_density = st->density;
1384
1385 switch ((u_int) cmd) {
1386
1387 case MTIOCGET: {
1388 struct mtget *g = (struct mtget *) arg;
1389 /*
1390 * (to get the current state of READONLY)
1391 */
1392 error = st->ops(st, ST_OPS_MODESENSE, XS_CTL_SILENT);
1393 if (error) {
1394 /*
1395 * Ignore the error if in control mode;
1396 * this is mandated by st(4).
1397 */
1398 if (STMODE(dev) != CTRL_MODE)
1399 break;
1400 error = 0;
1401 }
1402 SC_DEBUG(st->sc_periph, SCSIPI_DB1, ("[ioctl: get status]\n"));
1403 memset(g, 0, sizeof(struct mtget));
1404 g->mt_type = 0x7; /* Ultrix compat *//*? */
1405 g->mt_blksiz = st->blksize;
1406 g->mt_density = st->density;
1407 g->mt_mblksiz[0] = st->modes[0].blksize;
1408 g->mt_mblksiz[1] = st->modes[1].blksize;
1409 g->mt_mblksiz[2] = st->modes[2].blksize;
1410 g->mt_mblksiz[3] = st->modes[3].blksize;
1411 g->mt_mdensity[0] = st->modes[0].density;
1412 g->mt_mdensity[1] = st->modes[1].density;
1413 g->mt_mdensity[2] = st->modes[2].density;
1414 g->mt_mdensity[3] = st->modes[3].density;
1415 g->mt_fileno = st->fileno;
1416 g->mt_blkno = st->blkno;
1417 if (st->flags & ST_READONLY)
1418 g->mt_dsreg |= MT_DS_RDONLY;
1419 if (st->flags & ST_MOUNTED)
1420 g->mt_dsreg |= MT_DS_MOUNTED;
1421 g->mt_resid = st->mt_resid;
1422 g->mt_erreg = st->mt_erreg;
1423 /*
1424 * clear latched errors.
1425 */
1426 st->mt_resid = 0;
1427 st->mt_erreg = 0;
1428 st->asc = 0;
1429 st->ascq = 0;
1430 break;
1431 }
1432 case MTIOCTOP: {
1433
1434 SC_DEBUG(st->sc_periph, SCSIPI_DB1,
1435 ("[ioctl: op=0x%x count=0x%x]\n", mt->mt_op,
1436 mt->mt_count));
1437
1438 /* compat: in U*x it is a short */
1439 number = mt->mt_count;
1440 switch ((short) (mt->mt_op)) {
1441 case MTWEOF: /* write an end-of-file record */
1442 error = st_write_filemarks(st, number, flags);
1443 break;
1444 case MTBSF: /* backward space file */
1445 number = -number;
1446 case MTFSF: /* forward space file */
1447 error = st_check_eod(st, FALSE, &nmarks, flags);
1448 if (!error)
1449 error = st_space(st, number - nmarks,
1450 SP_FILEMARKS, flags);
1451 break;
1452 case MTBSR: /* backward space record */
1453 number = -number;
1454 case MTFSR: /* forward space record */
1455 error = st_check_eod(st, TRUE, &nmarks, flags);
1456 if (!error)
1457 error = st_space(st, number, SP_BLKS, flags);
1458 break;
1459 case MTREW: /* rewind */
1460 error = st_rewind(st, 0, flags);
1461 break;
1462 case MTOFFL: /* rewind and put the drive offline */
1463 st_unmount(st, EJECT);
1464 break;
1465 case MTNOP: /* no operation, sets status only */
1466 break;
1467 case MTRETEN: /* retension the tape */
1468 error = st_load(st, LD_RETENSION, flags);
1469 if (!error)
1470 error = st_load(st, LD_LOAD, flags);
1471 break;
1472 case MTEOM: /* forward space to end of media */
1473 error = st_check_eod(st, FALSE, &nmarks, flags);
1474 if (!error)
1475 error = st_space(st, 1, SP_EOM, flags);
1476 break;
1477 case MTCACHE: /* enable controller cache */
1478 st->flags &= ~ST_DONTBUFFER;
1479 goto try_new_value;
1480 case MTNOCACHE: /* disable controller cache */
1481 st->flags |= ST_DONTBUFFER;
1482 goto try_new_value;
1483 case MTERASE: /* erase volume */
1484 error = st_erase(st, number, flags);
1485 break;
1486 case MTSETBSIZ: /* Set block size for device */
1487 #ifdef NOTYET
1488 if (!(st->flags & ST_NEW_MOUNT)) {
1489 uprintf("re-mount tape before changing blocksize");
1490 error = EINVAL;
1491 break;
1492 }
1493 #endif
1494 if (number == 0)
1495 st->flags &= ~ST_FIXEDBLOCKS;
1496 else {
1497 if ((st->blkmin || st->blkmax) &&
1498 (number < st->blkmin ||
1499 number > st->blkmax)) {
1500 error = EINVAL;
1501 break;
1502 }
1503 st->flags |= ST_FIXEDBLOCKS;
1504 }
1505 st->blksize = number;
1506 st->flags |= ST_BLOCK_SET; /*XXX */
1507 goto try_new_value;
1508
1509 case MTSETDNSTY: /* Set density for device and mode */
1510 /*
1511 * Any number >= 0 and <= 0xff is legal. Numbers
1512 * above 0x80 are 'vendor unique'.
1513 */
1514 if (number < 0 || number > 255) {
1515 error = EINVAL;
1516 break;
1517 } else
1518 st->density = number;
1519 goto try_new_value;
1520
1521 case MTCMPRESS:
1522 error = st->ops(st, (number == 0) ?
1523 ST_OPS_CMPRSS_OFF : ST_OPS_CMPRSS_ON,
1524 XS_CTL_SILENT);
1525 break;
1526
1527 case MTEWARN:
1528 if (number)
1529 st->flags |= ST_EARLYWARN;
1530 else
1531 st->flags &= ~ST_EARLYWARN;
1532 break;
1533
1534 default:
1535 error = EINVAL;
1536 }
1537 break;
1538 }
1539 case MTIOCIEOT:
1540 case MTIOCEEOT:
1541 break;
1542
1543 case MTIOCRDSPOS:
1544 error = st_rdpos(st, 0, (u_int32_t *) arg);
1545 break;
1546
1547 case MTIOCRDHPOS:
1548 error = st_rdpos(st, 1, (u_int32_t *) arg);
1549 break;
1550
1551 case MTIOCSLOCATE:
1552 error = st_setpos(st, 0, (u_int32_t *) arg);
1553 break;
1554
1555 case MTIOCHLOCATE:
1556 error = st_setpos(st, 1, (u_int32_t *) arg);
1557 break;
1558
1559
1560 default:
1561 error = scsipi_do_ioctl(st->sc_periph, dev, cmd, arg,
1562 flag, p);
1563 break;
1564 }
1565 return (error);
1566 /*-----------------------------*/
1567 try_new_value:
1568 /*
1569 * Check that the mode being asked for is aggreeable to the
1570 * drive. If not, put it back the way it was.
1571 *
1572 * If in control mode, we can make (persistent) mode changes
1573 * even if no medium is loaded (see st(4)).
1574 */
1575 if ((STMODE(dev) != CTRL_MODE || (st->flags & ST_MOUNTED) != 0) &&
1576 (error = st->ops(st, ST_OPS_MODESELECT, 0)) != 0) {
1577 /* put it back as it was */
1578 printf("%s: cannot set selected mode\n", st->sc_dev.dv_xname);
1579 st->density = hold_density;
1580 st->blksize = hold_blksize;
1581 if (st->blksize)
1582 st->flags |= ST_FIXEDBLOCKS;
1583 else
1584 st->flags &= ~ST_FIXEDBLOCKS;
1585 return (error);
1586 }
1587 /*
1588 * As the drive liked it, if we are setting a new default,
1589 * set it into the structures as such.
1590 *
1591 * The means for deciding this are not finalised yet- but
1592 * if the device was opened in Control Mode, the values
1593 * are persistent now across mounts.
1594 */
1595 if (STMODE(dev) == CTRL_MODE) {
1596 switch ((short) (mt->mt_op)) {
1597 case MTSETBSIZ:
1598 st->modes[dsty].blksize = st->blksize;
1599 st->modeflags[dsty] |= BLKSIZE_SET_BY_USER;
1600 break;
1601 case MTSETDNSTY:
1602 st->modes[dsty].density = st->density;
1603 st->modeflags[dsty] |= DENSITY_SET_BY_USER;
1604 break;
1605 }
1606 }
1607 return (0);
1608 }
1609
1610 /*
1611 * Do a synchronous read.
1612 */
1613 int
1614 st_read(st, buf, size, flags)
1615 struct st_softc *st;
1616 int size;
1617 int flags;
1618 char *buf;
1619 {
1620 struct scsi_rw_tape cmd;
1621
1622 /*
1623 * If it's a null transfer, return immediatly
1624 */
1625 if (size == 0)
1626 return (0);
1627 memset(&cmd, 0, sizeof(cmd));
1628 cmd.opcode = READ;
1629 if (st->flags & ST_FIXEDBLOCKS) {
1630 cmd.byte2 |= SRW_FIXED;
1631 _lto3b(size / (st->blksize ? st->blksize : DEF_FIXED_BSIZE),
1632 cmd.len);
1633 } else
1634 _lto3b(size, cmd.len);
1635 return (scsipi_command(st->sc_periph,
1636 (struct scsipi_generic *)&cmd, sizeof(cmd),
1637 (u_char *)buf, size, 0, ST_IO_TIME, NULL, flags | XS_CTL_DATA_IN));
1638 }
1639
1640 /*
1641 * issue an erase command
1642 */
1643 int
1644 st_erase(st, full, flags)
1645 struct st_softc *st;
1646 int full, flags;
1647 {
1648 int tmo;
1649 struct scsi_erase cmd;
1650
1651 /*
1652 * Full erase means set LONG bit in erase command, which asks
1653 * the drive to erase the entire unit. Without this bit, we're
1654 * asking the drive to write an erase gap.
1655 */
1656 memset(&cmd, 0, sizeof(cmd));
1657 cmd.opcode = ERASE;
1658 if (full) {
1659 cmd.byte2 = SE_LONG;
1660 tmo = ST_SPC_TIME;
1661 } else {
1662 tmo = ST_IO_TIME;
1663 }
1664
1665 /*
1666 * XXX We always do this asynchronously, for now, unless the device
1667 * has the ST_Q_ERASE_NOIMM quirk. How long should we wait if we
1668 * want to (eventually) to it synchronously?
1669 */
1670 if ((st->quirks & ST_Q_ERASE_NOIMM) == 0)
1671 cmd.byte2 |= SE_IMMED;
1672
1673 return (scsipi_command(st->sc_periph,
1674 (struct scsipi_generic *)&cmd, sizeof(cmd),
1675 0, 0, ST_RETRIES, tmo, NULL, flags));
1676 }
1677
1678 /*
1679 * skip N blocks/filemarks/seq filemarks/eom
1680 */
1681 int
1682 st_space(st, number, what, flags)
1683 struct st_softc *st;
1684 u_int what;
1685 int flags;
1686 int number;
1687 {
1688 struct scsi_space cmd;
1689 int error;
1690
1691 switch (what) {
1692 case SP_BLKS:
1693 if (st->flags & ST_PER_ACTION) {
1694 if (number > 0) {
1695 st->flags &= ~ST_PER_ACTION;
1696 return (EIO);
1697 } else if (number < 0) {
1698 if (st->flags & ST_AT_FILEMARK) {
1699 /*
1700 * Handling of ST_AT_FILEMARK
1701 * in st_space will fill in the
1702 * right file mark count.
1703 */
1704 error = st_space(st, 0, SP_FILEMARKS,
1705 flags);
1706 if (error)
1707 return (error);
1708 }
1709 if (st->flags & ST_BLANK_READ) {
1710 st->flags &= ~ST_BLANK_READ;
1711 return (EIO);
1712 }
1713 st->flags &= ~(ST_EIO_PENDING|ST_EOM_PENDING);
1714 }
1715 }
1716 break;
1717 case SP_FILEMARKS:
1718 if (st->flags & ST_EIO_PENDING) {
1719 if (number > 0) {
1720 /* pretend we just discovered the error */
1721 st->flags &= ~ST_EIO_PENDING;
1722 return (EIO);
1723 } else if (number < 0) {
1724 /* back away from the error */
1725 st->flags &= ~ST_EIO_PENDING;
1726 }
1727 }
1728 if (st->flags & ST_AT_FILEMARK) {
1729 st->flags &= ~ST_AT_FILEMARK;
1730 number--;
1731 }
1732 if ((st->flags & ST_BLANK_READ) && (number < 0)) {
1733 /* back away from unwritten tape */
1734 st->flags &= ~ST_BLANK_READ;
1735 number++; /* XXX dubious */
1736 }
1737 break;
1738 case SP_EOM:
1739 if (st->flags & ST_EOM_PENDING) {
1740 /* we're already there */
1741 st->flags &= ~ST_EOM_PENDING;
1742 return (0);
1743 }
1744 if (st->flags & ST_EIO_PENDING) {
1745 /* pretend we just discovered the error */
1746 st->flags &= ~ST_EIO_PENDING;
1747 return (EIO);
1748 }
1749 if (st->flags & ST_AT_FILEMARK)
1750 st->flags &= ~ST_AT_FILEMARK;
1751 break;
1752 }
1753 if (number == 0)
1754 return (0);
1755
1756 memset(&cmd, 0, sizeof(cmd));
1757 cmd.opcode = SPACE;
1758 cmd.byte2 = what;
1759 _lto3b(number, cmd.number);
1760
1761 st->flags &= ~ST_POSUPDATED;
1762 st->last_ctl_resid = 0;
1763 error = scsipi_command(st->sc_periph,
1764 (struct scsipi_generic *)&cmd, sizeof(cmd),
1765 0, 0, 0, ST_SPC_TIME, NULL, flags);
1766
1767 if (error == 0 && (st->flags & ST_POSUPDATED) == 0) {
1768 number = number - st->last_ctl_resid;
1769 if (what == SP_BLKS) {
1770 if (st->blkno != -1) {
1771 st->blkno += number;
1772 }
1773 } else if (what == SP_FILEMARKS) {
1774 if (st->fileno != -1) {
1775 st->fileno += number;
1776 if (number > 0) {
1777 st->blkno = 0;
1778 } else if (number < 0) {
1779 st->blkno = -1;
1780 }
1781 }
1782 } else if (what == SP_EOM) {
1783 /*
1784 * This loses us relative position.
1785 */
1786 st->fileno = st->blkno = -1;
1787 }
1788 }
1789 return (error);
1790 }
1791
1792 /*
1793 * write N filemarks
1794 */
1795 int
1796 st_write_filemarks(st, number, flags)
1797 struct st_softc *st;
1798 int flags;
1799 int number;
1800 {
1801 int error;
1802 struct scsi_write_filemarks cmd;
1803
1804 /*
1805 * It's hard to write a negative number of file marks.
1806 * Don't try.
1807 */
1808 if (number < 0)
1809 return (EINVAL);
1810 switch (number) {
1811 case 0: /* really a command to sync the drive's buffers */
1812 break;
1813 case 1:
1814 if (st->flags & ST_FM_WRITTEN) /* already have one down */
1815 st->flags &= ~ST_WRITTEN;
1816 else
1817 st->flags |= ST_FM_WRITTEN;
1818 st->flags &= ~ST_PER_ACTION;
1819 break;
1820 default:
1821 st->flags &= ~(ST_PER_ACTION | ST_WRITTEN);
1822 }
1823
1824 memset(&cmd, 0, sizeof(cmd));
1825 cmd.opcode = WRITE_FILEMARKS;
1826 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1827 cmd.byte2 = SR_IMMED;
1828 /*
1829 * The ATAPI Onstream DI-30 doesn't support writing filemarks, but
1830 * WRITE_FILEMARKS is still used to flush the buffer
1831 */
1832 if ((st->quirks & ST_Q_NOFILEMARKS) == 0)
1833 _lto3b(number, cmd.number);
1834
1835 /* XXX WE NEED TO BE ABLE TO GET A RESIDIUAL XXX */
1836 error = scsipi_command(st->sc_periph,
1837 (struct scsipi_generic *)&cmd, sizeof(cmd),
1838 0, 0, 0, ST_IO_TIME * 4, NULL, flags);
1839 if (error == 0 && st->fileno != -1) {
1840 st->fileno += number;
1841 }
1842 return (error);
1843 }
1844
1845 /*
1846 * Make sure the right number of file marks is on tape if the
1847 * tape has been written. If the position argument is true,
1848 * leave the tape positioned where it was originally.
1849 *
1850 * nmarks returns the number of marks to skip (or, if position
1851 * true, which were skipped) to get back original position.
1852 */
1853 int
1854 st_check_eod(st, position, nmarks, flags)
1855 struct st_softc *st;
1856 boolean position;
1857 int *nmarks;
1858 int flags;
1859 {
1860 int error;
1861
1862 switch (st->flags & (ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD)) {
1863 default:
1864 *nmarks = 0;
1865 return (0);
1866 case ST_WRITTEN:
1867 case ST_WRITTEN | ST_FM_WRITTEN | ST_2FM_AT_EOD:
1868 *nmarks = 1;
1869 break;
1870 case ST_WRITTEN | ST_2FM_AT_EOD:
1871 *nmarks = 2;
1872 }
1873 error = st_write_filemarks(st, *nmarks, flags);
1874 if (position && !error)
1875 error = st_space(st, -*nmarks, SP_FILEMARKS, flags);
1876 return (error);
1877 }
1878
1879 /*
1880 * load/unload/retension
1881 */
1882 int
1883 st_load(st, type, flags)
1884 struct st_softc *st;
1885 u_int type;
1886 int flags;
1887 {
1888 int error;
1889 struct scsi_load cmd;
1890
1891 if (type != LD_LOAD) {
1892 int nmarks;
1893
1894 error = st_check_eod(st, FALSE, &nmarks, flags);
1895 if (error) {
1896 printf("%s: failed to write closing filemarks at "
1897 "unload, errno=%d\n", st->sc_dev.dv_xname, error);
1898 return (error);
1899 }
1900 }
1901 if (st->quirks & ST_Q_IGNORE_LOADS) {
1902 if (type == LD_LOAD) {
1903 /*
1904 * If we ignore loads, at least we should try a rewind.
1905 */
1906 return st_rewind(st, 0, flags);
1907 }
1908 /* otherwise, we should do what's asked of us */
1909 }
1910
1911 memset(&cmd, 0, sizeof(cmd));
1912 cmd.opcode = LOAD;
1913 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1914 cmd.byte2 = SR_IMMED;
1915 cmd.how = type;
1916
1917 error = scsipi_command(st->sc_periph,
1918 (struct scsipi_generic *)&cmd, sizeof(cmd),
1919 0, 0, ST_RETRIES, ST_SPC_TIME, NULL, flags);
1920 if (error) {
1921 printf("%s: error %d in st_load (op %d)\n",
1922 st->sc_dev.dv_xname, error, type);
1923 }
1924 return (error);
1925 }
1926
1927 /*
1928 * Rewind the device
1929 */
1930 int
1931 st_rewind(st, immediate, flags)
1932 struct st_softc *st;
1933 u_int immediate;
1934 int flags;
1935 {
1936 struct scsi_rewind cmd;
1937 int error;
1938 int nmarks;
1939
1940 error = st_check_eod(st, FALSE, &nmarks, flags);
1941 if (error) {
1942 printf("%s: failed to write closing filemarks at "
1943 "rewind, errno=%d\n", st->sc_dev.dv_xname, error);
1944 return (error);
1945 }
1946 st->flags &= ~ST_PER_ACTION;
1947
1948 /*
1949 * ATAPI tapes always need immediate to be set
1950 */
1951 if (scsipi_periph_bustype(st->sc_periph) == SCSIPI_BUSTYPE_ATAPI)
1952 immediate = SR_IMMED;
1953
1954 memset(&cmd, 0, sizeof(cmd));
1955 cmd.opcode = REWIND;
1956 cmd.byte2 = immediate;
1957
1958 error = scsipi_command(st->sc_periph,
1959 (struct scsipi_generic *)&cmd, sizeof(cmd), 0, 0, ST_RETRIES,
1960 immediate ? ST_CTL_TIME: ST_SPC_TIME, NULL, flags);
1961 if (error) {
1962 printf("%s: error %d trying to rewind\n",
1963 st->sc_dev.dv_xname, error);
1964 /* lost position */
1965 st->fileno = st->blkno = -1;
1966 } else {
1967 st->fileno = st->blkno = 0;
1968 }
1969 return (error);
1970 }
1971
1972 int
1973 st_rdpos(st, hard, blkptr)
1974 struct st_softc *st;
1975 int hard;
1976 u_int32_t *blkptr;
1977 {
1978 int error;
1979 u_int8_t posdata[20];
1980 struct scsi_tape_read_position cmd;
1981
1982 /*
1983 * We try and flush any buffered writes here if we were writing
1984 * and we're trying to get hardware block position. It eats
1985 * up performance substantially, but I'm wary of drive firmware.
1986 *
1987 * I think that *logical* block position is probably okay-
1988 * but hardware block position might have to wait for data
1989 * to hit media to be valid. Caveat Emptor.
1990 */
1991
1992 if (hard && (st->flags & ST_WRITTEN)) {
1993 /*
1994 * First flush any pending writes...
1995 */
1996 error = st_write_filemarks(st, 0, XS_CTL_SILENT);
1997 /*
1998 * The latter case is for 'write protected' tapes
1999 * which are too stupid to recognize a zero count
2000 * for writing filemarks as a no-op.
2001 */
2002 if (error != 0 && error != EACCES && error != EROFS)
2003 return (error);
2004 }
2005
2006 memset(&cmd, 0, sizeof(cmd));
2007 memset(&posdata, 0, sizeof(posdata));
2008 cmd.opcode = READ_POSITION;
2009 if (hard)
2010 cmd.byte1 = 1;
2011
2012 error = scsipi_command(st->sc_periph,
2013 (struct scsipi_generic *)&cmd, sizeof(cmd), (u_char *)&posdata,
2014 sizeof(posdata), ST_RETRIES, ST_CTL_TIME, NULL,
2015 XS_CTL_SILENT | XS_CTL_DATA_IN | XS_CTL_DATA_ONSTACK);
2016
2017 if (error == 0) {
2018 #if 0
2019 printf("posdata:");
2020 for (hard = 0; hard < sizeof(posdata); hard++)
2021 printf("%02x ", posdata[hard] & 0xff);
2022 printf("\n");
2023 #endif
2024 if (posdata[0] & 0x4) /* Block Position Unknown */
2025 error = EINVAL;
2026 else
2027 *blkptr = _4btol(&posdata[4]);
2028 }
2029 return (error);
2030 }
2031
2032 int
2033 st_setpos(st, hard, blkptr)
2034 struct st_softc *st;
2035 int hard;
2036 u_int32_t *blkptr;
2037 {
2038 int error;
2039 struct scsi_tape_locate cmd;
2040
2041 /*
2042 * We used to try and flush any buffered writes here.
2043 * Now we push this onto user applications to either
2044 * flush the pending writes themselves (via a zero count
2045 * WRITE FILEMARKS command) or they can trust their tape
2046 * drive to do this correctly for them.
2047 *
2048 * There are very ugly performance limitations otherwise.
2049 */
2050
2051 memset(&cmd, 0, sizeof(cmd));
2052 cmd.opcode = LOCATE;
2053 if (hard)
2054 cmd.byte2 = 1 << 2;
2055 _lto4b(*blkptr, cmd.blkaddr);
2056 error = scsipi_command(st->sc_periph,
2057 (struct scsipi_generic *)&cmd, sizeof(cmd),
2058 NULL, 0, ST_RETRIES, ST_SPC_TIME, NULL, 0);
2059 /*
2060 * Note file && block number position now unknown (if
2061 * these things ever start being maintained in this driver)
2062 */
2063 st->fileno = st->blkno = -1;
2064 return (error);
2065 }
2066
2067
2068 /*
2069 * Look at the returned sense and act on the error and determine
2070 * the unix error number to pass back..., 0 (== report no error),
2071 * -1 = retry the operation, -2 continue error processing.
2072 */
2073 int
2074 st_interpret_sense(xs)
2075 struct scsipi_xfer *xs;
2076 {
2077 struct scsipi_periph *periph = xs->xs_periph;
2078 struct scsipi_sense_data *sense = &xs->sense.scsi_sense;
2079 struct buf *bp = xs->bp;
2080 struct st_softc *st = (void *)periph->periph_dev;
2081 int retval = EJUSTRETURN;
2082 int doprint = ((xs->xs_control & XS_CTL_SILENT) == 0);
2083 u_int8_t key;
2084 int32_t info;
2085
2086 /*
2087 * If it isn't a extended or extended/deferred error, let
2088 * the generic code handle it.
2089 */
2090 if ((sense->error_code & SSD_ERRCODE) != 0x70 &&
2091 (sense->error_code & SSD_ERRCODE) != 0x71) { /* DEFFERRED */
2092 return (retval);
2093 }
2094
2095 if (sense->error_code & SSD_ERRCODE_VALID)
2096 info = _4btol(sense->info);
2097 else
2098 info = (st->flags & ST_FIXEDBLOCKS) ?
2099 xs->datalen / st->blksize : xs->datalen;
2100 key = sense->flags & SSD_KEY;
2101 st->mt_erreg = key;
2102 st->asc = sense->add_sense_code;
2103 st->ascq = sense->add_sense_code_qual;
2104 st->mt_resid = (short) info;
2105
2106 if (key == SKEY_NOT_READY && st->asc == 0x4 && st->ascq == 0x1) {
2107 /* Not Ready, Logical Unit Is in Process Of Becoming Ready */
2108 if (!callout_active(&periph->periph_callout))
2109 scsipi_periph_freeze(periph, 1);
2110 callout_reset(&periph->periph_callout,
2111 hz, scsipi_periph_timed_thaw, periph);
2112 return (ERESTART);
2113 }
2114
2115 /*
2116 * If the device is not open yet, let generic handle
2117 */
2118 if ((periph->periph_flags & PERIPH_OPEN) == 0) {
2119 return (retval);
2120 }
2121
2122 xs->resid = info;
2123 if (st->flags & ST_FIXEDBLOCKS) {
2124 if (bp) {
2125 xs->resid *= st->blksize;
2126 st->last_io_resid = xs->resid;
2127 } else {
2128 st->last_ctl_resid = xs->resid;
2129 }
2130 if (key == SKEY_VOLUME_OVERFLOW) {
2131 st->flags |= ST_EIO_PENDING;
2132 if (bp)
2133 bp->b_resid = xs->resid;
2134 } else if (sense->flags & SSD_EOM) {
2135 if ((st->flags & ST_EARLYWARN) == 0)
2136 st->flags |= ST_EIO_PENDING;
2137 st->flags |= ST_EOM_PENDING;
2138 if (bp) {
2139 #if 0
2140 bp->b_resid = xs->resid;
2141 #else
2142 /*
2143 * Grotesque as it seems, the few times
2144 * I've actually seen a non-zero resid,
2145 * the tape drive actually lied and had
2146 * written all the data!
2147 */
2148 bp->b_resid = 0;
2149 #endif
2150 }
2151 }
2152 if (sense->flags & SSD_FILEMARK) {
2153 st->flags |= ST_AT_FILEMARK;
2154 if (bp)
2155 bp->b_resid = xs->resid;
2156 if (st->fileno != (daddr_t) -1) {
2157 st->fileno++;
2158 st->blkno = 0;
2159 st->flags |= ST_POSUPDATED;
2160 }
2161 }
2162 if (sense->flags & SSD_ILI) {
2163 st->flags |= ST_EIO_PENDING;
2164 if (bp)
2165 bp->b_resid = xs->resid;
2166 if (sense->error_code & SSD_ERRCODE_VALID &&
2167 (xs->xs_control & XS_CTL_SILENT) == 0)
2168 printf("%s: block wrong size, %d blocks "
2169 "residual\n", st->sc_dev.dv_xname, info);
2170
2171 /*
2172 * This quirk code helps the drive read
2173 * the first tape block, regardless of
2174 * format. That is required for these
2175 * drives to return proper MODE SENSE
2176 * information.
2177 */
2178 if ((st->quirks & ST_Q_SENSE_HELP) &&
2179 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0)
2180 st->blksize -= 512;
2181 else if ((st->flags & ST_POSUPDATED) == 0) {
2182 if (st->blkno != (daddr_t) -1) {
2183 st->blkno +=
2184 (xs->datalen / st->blksize);
2185 st->flags |= ST_POSUPDATED;
2186 }
2187 }
2188 }
2189 /*
2190 * If data wanted and no data was transferred, do it immediately
2191 */
2192 if (xs->datalen && xs->resid >= xs->datalen) {
2193 if (st->flags & ST_EIO_PENDING)
2194 return (EIO);
2195 if (st->flags & ST_AT_FILEMARK) {
2196 if (bp)
2197 bp->b_resid = xs->resid;
2198 return (0);
2199 }
2200 }
2201 } else { /* must be variable mode */
2202 if (bp) {
2203 st->last_io_resid = xs->resid;
2204 } else {
2205 st->last_ctl_resid = xs->resid;
2206 }
2207 if (sense->flags & SSD_EOM) {
2208 /*
2209 * The current semantics of this
2210 * driver requires EOM detection
2211 * to return EIO unless early
2212 * warning detection is enabled
2213 * for variable mode (this is always
2214 * on for fixed block mode).
2215 */
2216 if (st->flags & ST_EARLYWARN) {
2217 st->flags |= ST_EOM_PENDING;
2218 retval = 0;
2219 } else {
2220 retval = EIO;
2221 }
2222
2223 /*
2224 * If it's an unadorned EOM detection,
2225 * suppress printing an error.
2226 */
2227 if (key == SKEY_NO_SENSE) {
2228 doprint = 0;
2229 }
2230 } else if (sense->flags & SSD_FILEMARK) {
2231 retval = 0;
2232 if (st->fileno != (daddr_t) -1) {
2233 st->fileno++;
2234 st->blkno = 0;
2235 st->flags |= ST_POSUPDATED;
2236 }
2237 } else if (sense->flags & SSD_ILI) {
2238 if (info < 0) {
2239 /*
2240 * The tape record was bigger than the read
2241 * we issued.
2242 */
2243 if ((xs->xs_control & XS_CTL_SILENT) == 0) {
2244 printf("%s: %d-byte tape record too big"
2245 " for %d-byte user buffer\n",
2246 st->sc_dev.dv_xname,
2247 xs->datalen - info, xs->datalen);
2248 }
2249 retval = EIO;
2250 } else {
2251 retval = 0;
2252 if (st->blkno != (daddr_t) -1) {
2253 st->blkno++;
2254 st->flags |= ST_POSUPDATED;
2255 }
2256 }
2257 }
2258 if (bp)
2259 bp->b_resid = info;
2260 }
2261
2262 #ifndef SCSIPI_DEBUG
2263 if (retval == 0 && key == SKEY_NO_SENSE)
2264 doprint = 0;
2265 #endif
2266 if (key == SKEY_BLANK_CHECK) {
2267 /*
2268 * This quirk code helps the drive read the
2269 * first tape block, regardless of format. That
2270 * is required for these drives to return proper
2271 * MODE SENSE information.
2272 */
2273 if ((st->quirks & ST_Q_SENSE_HELP) &&
2274 (periph->periph_flags & PERIPH_MEDIA_LOADED) == 0) {
2275 /* still starting */
2276 st->blksize -= 512;
2277 } else if (!(st->flags & (ST_2FM_AT_EOD | ST_BLANK_READ))) {
2278 st->flags |= ST_BLANK_READ;
2279 xs->resid = xs->datalen;
2280 if (bp) {
2281 bp->b_resid = xs->resid;
2282 /* return an EOF */
2283 }
2284 retval = 0;
2285 /* lost position */
2286 st->fileno = st->blkno = -1;
2287 }
2288 }
2289
2290 /*
2291 * If generic sense processing will continue, we should not
2292 * print sense info here.
2293 */
2294 if (retval == EJUSTRETURN)
2295 doprint = 0;
2296
2297 if (doprint) {
2298 #ifdef SCSIVERBOSE
2299 scsipi_print_sense(xs, 0);
2300 #else
2301 scsipi_printaddr(periph);
2302 printf("Sense Key 0x%02x", key);
2303 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2304 switch (key) {
2305 case SKEY_NOT_READY:
2306 case SKEY_ILLEGAL_REQUEST:
2307 case SKEY_UNIT_ATTENTION:
2308 case SKEY_WRITE_PROTECT:
2309 break;
2310 case SKEY_VOLUME_OVERFLOW:
2311 case SKEY_BLANK_CHECK:
2312 printf(", requested size: %d (decimal)", info);
2313 break;
2314 case SKEY_ABORTED_COMMAND:
2315 if (xs->xs_retries)
2316 printf(", retrying");
2317 printf(", cmd 0x%x, info 0x%x",
2318 xs->cmd->opcode, info);
2319 break;
2320 default:
2321 printf(", info = %d (decimal)", info);
2322 }
2323 }
2324 if (sense->extra_len != 0) {
2325 int n;
2326 printf(", data =");
2327 for (n = 0; n < sense->extra_len; n++)
2328 printf(" %02x", sense->cmd_spec_info[n]);
2329 }
2330 printf("\n");
2331 #endif
2332 }
2333 return (retval);
2334 }
2335
2336 /*
2337 * The quirk here is that the drive returns some value to st_mode_sense
2338 * incorrectly until the tape has actually passed by the head.
2339 *
2340 * The method is to set the drive to large fixed-block state (user-specified
2341 * density and 1024-byte blocks), then read and rewind to get it to sense the
2342 * tape. If that doesn't work, try 512-byte fixed blocks. If that doesn't
2343 * work, as a last resort, try variable- length blocks. The result will be
2344 * the ability to do an accurate st_mode_sense.
2345 *
2346 * We know we can do a rewind because we just did a load, which implies rewind.
2347 * Rewind seems preferable to space backward if we have a virgin tape.
2348 *
2349 * The rest of the code for this quirk is in ILI processing and BLANK CHECK
2350 * error processing, both part of st_interpret_sense.
2351 */
2352 int
2353 st_touch_tape(st)
2354 struct st_softc *st;
2355 {
2356 char *buf;
2357 int readsize;
2358 int error;
2359
2360 buf = malloc(1024, M_TEMP, M_NOWAIT);
2361 if (buf == NULL)
2362 return (ENOMEM);
2363
2364 if ((error = st->ops(st, ST_OPS_MODESENSE, 0)) != 0)
2365 goto bad;
2366
2367 /*
2368 * If the block size is already known from the
2369 * sense data, use it. Else start probing at 1024.
2370 */
2371 if (st->media_blksize > 0)
2372 st->blksize = st->media_blksize;
2373 else
2374 st->blksize = 1024;
2375
2376 do {
2377 switch (st->blksize) {
2378 case 512:
2379 case 1024:
2380 readsize = st->blksize;
2381 st->flags |= ST_FIXEDBLOCKS;
2382 break;
2383 default:
2384 readsize = 1;
2385 st->flags &= ~ST_FIXEDBLOCKS;
2386 }
2387 if ((error = st->ops(st, ST_OPS_MODESELECT, XS_CTL_SILENT))
2388 != 0) {
2389 /*
2390 * The device did not agree with the proposed
2391 * block size. If we exhausted our options,
2392 * return failure, else try another.
2393 */
2394 if (readsize == 1)
2395 goto bad;
2396 st->blksize -= 512;
2397 continue;
2398 }
2399 st_read(st, buf, readsize, XS_CTL_SILENT); /* XXX */
2400 if ((error = st_rewind(st, 0, 0)) != 0) {
2401 bad: free(buf, M_TEMP);
2402 return (error);
2403 }
2404 } while (readsize != 1 && readsize > st->blksize);
2405
2406 free(buf, M_TEMP);
2407 return (0);
2408 }
2409
2410 int
2411 stdump(dev, blkno, va, size)
2412 dev_t dev;
2413 daddr_t blkno;
2414 caddr_t va;
2415 size_t size;
2416 {
2417
2418 /* Not implemented. */
2419 return (ENXIO);
2420 }
2421