1 /* $NetBSD: mt.c,v 1.34 2019/12/01 16:22:10 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 1996-2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1988 University of Utah.
34 * Copyright (c) 1982, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * the Systems Programming Group of the University of Utah Computer
39 * Science Department.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * from: Utah $Hdr: rd.c 1.44 92/12/26$
66 *
67 * @(#)rd.c 8.2 (Berkeley) 5/19/94
68 */
69
70 /*
71 * Magnetic tape driver (HP7974a, HP7978a/b, HP7979a, HP7980a, HP7980xc)
72 * Original version contributed by Mt. Xinu.
73 * Modified for 4.4BSD by Mark Davies and Andrew Vignaux, Department of
74 * Computer Science, Victoria University of Wellington
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: mt.c,v 1.34 2019/12/01 16:22:10 riastradh Exp $");
79
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/callout.h>
83 #include <sys/buf.h>
84 #include <sys/bufq.h>
85 #include <sys/ioctl.h>
86 #include <sys/mtio.h>
87 #include <sys/file.h>
88 #include <sys/proc.h>
89 #include <sys/tty.h>
90 #include <sys/kernel.h>
91 #include <sys/tprintf.h>
92 #include <sys/device.h>
93 #include <sys/conf.h>
94
95 #include <dev/gpib/gpibvar.h>
96 #include <dev/gpib/cs80busvar.h>
97
98 #include <dev/gpib/mtreg.h>
99
100 #include "ioconf.h"
101
102 #ifdef DEBUG
103 int mtdebug = 0;
104 #define MDB_ANY 0xff
105 #define MDB_FOLLOW 0x01
106 #define DPRINTF(mask, str) if (mtdebug & (mask)) printf str
107 #else
108 #define DPRINTF(mask, str) /* nothing */
109 #endif
110
111 struct mt_softc {
112 device_t sc_dev;
113
114 gpib_chipset_tag_t sc_ic;
115 gpib_handle_t sc_hdl;
116
117 int sc_slave; /* GPIB slave address (0-6) */
118 short sc_flags; /* see below */
119 u_char sc_lastdsj; /* place for DSJ in mtreaddsj() */
120 u_char sc_lastecmd; /* place for End Command in mtreaddsj() */
121 short sc_recvtimeo; /* count of gpibsend timeouts to prevent hang */
122 short sc_statindex; /* index for next sc_stat when MTF_STATTIMEO */
123 struct mt_stat sc_stat;/* status bytes last read from device */
124 short sc_density; /* current density of tape (mtio.h format) */
125 short sc_type; /* tape drive model (hardware IDs) */
126 tpr_t sc_ttyp;
127 struct bufq_state *sc_tab;/* buf queue */
128 int sc_active;
129 struct buf sc_bufstore; /* XXX buffer storage */
130
131 struct callout sc_start_ch;
132 struct callout sc_intr_ch;
133 };
134
135 #define MTUNIT(x) (minor(x) & 0x03)
136
137 #define B_CMD B_DEVPRIVATE /* command buf instead of data */
138 #define b_cmd b_blkno /* blkno holds cmd when B_CMD */
139
140 int mtmatch(device_t, cfdata_t, void *);
141 void mtattach(device_t, device_t, void *);
142
143 CFATTACH_DECL_NEW(mt, sizeof(struct mt_softc),
144 mtmatch, mtattach, NULL, NULL);
145
146 int mtlookup(int, int, int);
147 void mtustart(struct mt_softc *);
148 int mtreaddsj(struct mt_softc *, int);
149 int mtcommand(dev_t, int, int);
150
151 void mtintr_callout(void *);
152 void mtstart_callout(void *);
153
154 void mtcallback(void *, int);
155 void mtstart(struct mt_softc *);
156 void mtintr(struct mt_softc *);
157
158 dev_type_open(mtopen);
159 dev_type_close(mtclose);
160 dev_type_read(mtread);
161 dev_type_write(mtwrite);
162 dev_type_ioctl(mtioctl);
163 dev_type_strategy(mtstrategy);
164
165 const struct bdevsw mt_bdevsw = {
166 .d_open = mtopen,
167 .d_close = mtclose,
168 .d_strategy = mtstrategy,
169 .d_ioctl = mtioctl,
170 .d_dump = nodump,
171 .d_psize = nosize,
172 .d_discard = nodiscard,
173 .d_flag = D_TAPE
174 };
175
176 const struct cdevsw mt_cdevsw = {
177 .d_open = mtopen,
178 .d_close = mtclose,
179 .d_read = mtread,
180 .d_write = mtwrite,
181 .d_ioctl = mtioctl,
182 .d_stop = nostop,
183 .d_tty = notty,
184 .d_poll = nopoll,
185 .d_mmap = nommap,
186 .d_kqfilter = nokqfilter,
187 .d_discard = nodiscard,
188 .d_flag = D_TAPE
189 };
190
191
192 struct mtinfo {
193 u_short hwid;
194 const char *desc;
195 } mtinfo[] = {
196 { MT7978ID, "7978" },
197 { MT7979AID, "7979A" },
198 { MT7980ID, "7980" },
199 { MT7974AID, "7974A" },
200 };
201 int nmtinfo = sizeof(mtinfo) / sizeof(mtinfo[0]);
202
203
204 int
mtlookup(int id,int slave,int punit)205 mtlookup(int id, int slave, int punit)
206 {
207 int i;
208
209 for (i = 0; i < nmtinfo; i++)
210 if (mtinfo[i].hwid == id)
211 break;
212 if (i == nmtinfo)
213 return (-1);
214 return (0);
215 }
216
217 int
mtmatch(device_t parent,cfdata_t match,void * aux)218 mtmatch(device_t parent, cfdata_t match, void *aux)
219 {
220 struct cs80bus_attach_args *ca = aux;
221
222 ca->ca_punit = 0;
223 return (mtlookup(ca->ca_id, ca->ca_slave, ca->ca_punit) == 0);
224 }
225
226 void
mtattach(device_t parent,device_t self,void * aux)227 mtattach(device_t parent, device_t self, void *aux)
228 {
229 struct mt_softc *sc = device_private(self);
230 struct cs80bus_attach_args *ca = aux;
231 int type;
232
233 sc->sc_dev = self;
234 sc->sc_ic = ca->ca_ic;
235 sc->sc_slave = ca->ca_slave;
236
237 if ((type = mtlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
238 return;
239
240 aprint_naive("\n");
241 aprint_normal(": %s tape\n", mtinfo[type].desc);
242
243 sc->sc_type = type;
244 sc->sc_flags = MTF_EXISTS;
245
246 bufq_alloc(&sc->sc_tab, "fcfs", 0);
247 callout_init(&sc->sc_start_ch, 0);
248 callout_init(&sc->sc_intr_ch, 0);
249
250 if (gpibregister(sc->sc_ic, sc->sc_slave, mtcallback, sc,
251 &sc->sc_hdl)) {
252 aprint_error_dev(sc->sc_dev, "can't register callback\n");
253 return;
254 }
255 }
256
257 /*
258 * Perform a read of "Device Status Jump" register and update the
259 * status if necessary. If status is read, the given "ecmd" is also
260 * performed, unless "ecmd" is zero. Returns DSJ value, -1 on failure
261 * and -2 on "temporary" failure.
262 */
263 int
mtreaddsj(struct mt_softc * sc,int ecmd)264 mtreaddsj(struct mt_softc *sc, int ecmd)
265 {
266 int retval;
267
268 if (sc->sc_flags & MTF_STATTIMEO)
269 goto getstats;
270 retval = gpibrecv(sc->sc_ic,
271 (sc->sc_flags & MTF_DSJTIMEO) ? -1 : sc->sc_slave,
272 MTT_DSJ, &(sc->sc_lastdsj), 1);
273 sc->sc_flags &= ~MTF_DSJTIMEO;
274 if (retval != 1) {
275 DPRINTF(MDB_ANY, ("%s can't gpibrecv DSJ",
276 device_xname(sc->sc_dev)));
277 if (sc->sc_recvtimeo == 0)
278 sc->sc_recvtimeo = hz;
279 if (--sc->sc_recvtimeo == 0)
280 return (-1);
281 if (retval == 0)
282 sc->sc_flags |= MTF_DSJTIMEO;
283 return (-2);
284 }
285 sc->sc_recvtimeo = 0;
286 sc->sc_statindex = 0;
287 DPRINTF(MDB_ANY, ("%s readdsj: 0x%x", device_xname(sc->sc_dev),
288 sc->sc_lastdsj));
289 sc->sc_lastecmd = ecmd;
290 switch (sc->sc_lastdsj) {
291 case 0:
292 if (ecmd & MTE_DSJ_FORCE)
293 break;
294 return (0);
295
296 case 2:
297 sc->sc_lastecmd = MTE_COMPLETE;
298 case 1:
299 break;
300
301 default:
302 printf("%s readdsj: DSJ 0x%x\n", device_xname(sc->sc_dev),
303 sc->sc_lastdsj);
304 return (-1);
305 }
306
307 getstats:
308 retval = gpibrecv(sc->sc_ic,
309 (sc->sc_flags & MTF_STATCONT) ? -1 : sc->sc_slave, MTT_STAT,
310 ((char *)&(sc->sc_stat)) + sc->sc_statindex,
311 sizeof(sc->sc_stat) - sc->sc_statindex);
312 sc->sc_flags &= ~(MTF_STATTIMEO | MTF_STATCONT);
313 if (retval != sizeof(sc->sc_stat) - sc->sc_statindex) {
314 if (sc->sc_recvtimeo == 0)
315 sc->sc_recvtimeo = hz;
316 if (--sc->sc_recvtimeo != 0) {
317 if (retval >= 0) {
318 sc->sc_statindex += retval;
319 sc->sc_flags |= MTF_STATCONT;
320 }
321 sc->sc_flags |= MTF_STATTIMEO;
322 return (-2);
323 }
324 printf("%s readdsj: can't read status",
325 device_xname(sc->sc_dev));
326 return (-1);
327 }
328 sc->sc_recvtimeo = 0;
329 sc->sc_statindex = 0;
330 DPRINTF(MDB_ANY, ("%s readdsj: status is %x %x %x %x %x %x",
331 device_xname(sc->sc_dev),
332 sc->sc_stat1, sc->sc_stat2, sc->sc_stat3,
333 sc->sc_stat4, sc->sc_stat5, sc->sc_stat6));
334 if (sc->sc_lastecmd)
335 (void) gpibsend(sc->sc_ic, sc->sc_slave,
336 MTL_ECMD, &(sc->sc_lastecmd), 1);
337 return ((int) sc->sc_lastdsj);
338 }
339
340 int
mtopen(dev_t dev,int flag,int mode,struct lwp * l)341 mtopen(dev_t dev, int flag, int mode, struct lwp *l)
342 {
343 struct mt_softc *sc;
344 int req_den;
345 int error;
346
347 sc = device_lookup_private(&mt_cd, MTUNIT(dev));
348 if (sc == NULL || (sc->sc_flags & MTF_EXISTS) == 0)
349 return (ENXIO);
350
351 if (sc->sc_flags & MTF_OPEN)
352 return (EBUSY);
353
354 DPRINTF(MDB_ANY, ("%s open: flags 0x%x", device_xname(sc->sc_dev),
355 sc->sc_flags));
356
357 sc->sc_flags |= MTF_OPEN;
358 sc->sc_ttyp = tprintf_open(l->l_proc);
359 if ((sc->sc_flags & MTF_ALIVE) == 0) {
360 error = mtcommand(dev, MTRESET, 0);
361 if (error != 0 || (sc->sc_flags & MTF_ALIVE) == 0)
362 goto errout;
363 if ((sc->sc_stat1 & (SR1_BOT | SR1_ONLINE)) == SR1_ONLINE)
364 (void) mtcommand(dev, MTREW, 0);
365 }
366 for (;;) {
367 if ((error = mtcommand(dev, MTNOP, 0)) != 0)
368 goto errout;
369 if (!(sc->sc_flags & MTF_REW))
370 break;
371 error = kpause("mt", true, hz, NULL);
372 if (error != 0 && error != EWOULDBLOCK) {
373 error = EINTR;
374 goto errout;
375 }
376 }
377 if ((flag & FWRITE) && (sc->sc_stat1 & SR1_RO)) {
378 error = EROFS;
379 goto errout;
380 }
381 if (!(sc->sc_stat1 & SR1_ONLINE)) {
382 uprintf("%s: not online\n", device_xname(sc->sc_dev));
383 error = EIO;
384 goto errout;
385 }
386 /*
387 * Select density:
388 * - find out what density the drive is set to
389 * (i.e. the density of the current tape)
390 * - if we are going to write
391 * - if we're not at the beginning of the tape
392 * - complain if we want to change densities
393 * - otherwise, select the mtcommand to set the density
394 *
395 * If the drive doesn't support it then don't change the recorded
396 * density.
397 *
398 * The original MOREbsd code had these additional conditions
399 * for the mid-tape change
400 *
401 * req_den != T_BADBPI &&
402 * sc->sc_density != T_6250BPI
403 *
404 * which suggests that it would be possible to write multiple
405 * densities if req_den == T_BAD_BPI or the current tape
406 * density was 6250. Testing of our 7980 suggests that the
407 * device cannot change densities mid-tape.
408 *
409 * ajv@comp.vuw.ac.nz
410 */
411 sc->sc_density = (sc->sc_stat2 & SR2_6250) ? T_6250BPI : (
412 (sc->sc_stat3 & SR3_1600) ? T_1600BPI : (
413 (sc->sc_stat3 & SR3_800) ? T_800BPI : -1));
414 req_den = (dev & T_DENSEL);
415
416 if (flag & FWRITE) {
417 if (!(sc->sc_stat1 & SR1_BOT)) {
418 if (sc->sc_density != req_den) {
419 uprintf("%s: can't change density mid-tape\n",
420 device_xname(sc->sc_dev));
421 error = EIO;
422 goto errout;
423 }
424 }
425 else {
426 int mtset_density =
427 (req_den == T_800BPI ? MTSET800BPI : (
428 req_den == T_1600BPI ? MTSET1600BPI : (
429 req_den == T_6250BPI ? MTSET6250BPI : (
430 sc->sc_type == MT7980ID
431 ? MTSET6250DC
432 : MTSET6250BPI))));
433 if (mtcommand(dev, mtset_density, 0) == 0)
434 sc->sc_density = req_den;
435 }
436 }
437 return (0);
438 errout:
439 sc->sc_flags &= ~MTF_OPEN;
440 return (error);
441 }
442
443 int
mtclose(dev_t dev,int flag,int fmt,struct lwp * l)444 mtclose(dev_t dev, int flag, int fmt, struct lwp *l)
445 {
446 struct mt_softc *sc;
447
448 sc = device_lookup_private(&mt_cd, MTUNIT(dev));
449 if (sc == NULL)
450 return (ENXIO);
451
452 if (sc->sc_flags & MTF_WRT) {
453 (void) mtcommand(dev, MTWEOF, 2);
454 (void) mtcommand(dev, MTBSF, 0);
455 }
456 if ((minor(dev) & T_NOREWIND) == 0)
457 (void) mtcommand(dev, MTREW, 0);
458 sc->sc_flags &= ~MTF_OPEN;
459 tprintf_close(sc->sc_ttyp);
460 return (0);
461 }
462
463 int
mtcommand(dev_t dev,int cmd,int cnt)464 mtcommand(dev_t dev, int cmd, int cnt)
465 {
466 struct mt_softc *sc;
467 struct buf *bp;
468 int error = 0;
469
470 sc = device_lookup_private(&mt_cd, MTUNIT(dev));
471 bp = &sc->sc_bufstore;
472
473 if (bp->b_cflags & BC_BUSY)
474 return (EBUSY);
475
476 bp->b_cmd = cmd;
477 bp->b_dev = dev;
478 bp->b_objlock = &buffer_lock;
479 do {
480 bp->b_cflags = BC_BUSY;
481 bp->b_flags = B_CMD;
482 bp->b_oflags = 0;
483 mtstrategy(bp);
484 biowait(bp);
485 if (bp->b_error != 0) {
486 error = (int) (unsigned) bp->b_error;
487 break;
488 }
489 } while (--cnt > 0);
490 #if 0
491 bp->b_cflags = 0 /*&= ~BC_BUSY*/;
492 #else
493 bp->b_cflags &= ~BC_BUSY;
494 #endif
495 return (error);
496 }
497
498 /*
499 * Only thing to check here is for legal record lengths (writes only).
500 */
501 void
mtstrategy(struct buf * bp)502 mtstrategy(struct buf *bp)
503 {
504 struct mt_softc *sc;
505 int s;
506
507 sc = device_lookup_private(&mt_cd, MTUNIT(bp->b_dev));
508
509 DPRINTF(MDB_ANY, ("%s strategy", device_xname(sc->sc_dev)));
510
511 if ((bp->b_flags & (B_CMD | B_READ)) == 0) {
512 #define WRITE_BITS_IGNORED 8
513 #if 0
514 if (bp->b_bcount & ((1 << WRITE_BITS_IGNORED) - 1)) {
515 tprintf(sc->sc_ttyp,
516 "%s: write record must be multiple of %d\n",
517 device_xname(sc->sc_dev), 1 << WRITE_BITS_IGNORED);
518 goto error;
519 }
520 #endif
521 s = 16 * 1024;
522 if (sc->sc_stat2 & SR2_LONGREC) {
523 switch (sc->sc_density) {
524 case T_1600BPI:
525 s = 32 * 1024;
526 break;
527
528 case T_6250BPI:
529 case T_BADBPI:
530 s = 60 * 1024;
531 break;
532 }
533 }
534 if (bp->b_bcount > s) {
535 tprintf(sc->sc_ttyp,
536 "%s: write record (%d) too big: limit (%d)\n",
537 device_xname(sc->sc_dev), bp->b_bcount, s);
538 #if 0 /* XXX see above */
539 error:
540 #endif
541 bp->b_error = EIO;
542 biodone(bp);
543 return;
544 }
545 }
546 s = splbio();
547 bufq_put(sc->sc_tab, bp);
548 if (sc->sc_active == 0) {
549 sc->sc_active = 1;
550 mtustart(sc);
551 }
552 splx(s);
553 }
554
555 void
mtustart(struct mt_softc * sc)556 mtustart(struct mt_softc *sc)
557 {
558
559 DPRINTF(MDB_ANY, ("%s ustart", device_xname(sc->sc_dev)));
560 if (gpibrequest(sc->sc_ic, sc->sc_hdl))
561 mtstart(sc);
562 }
563
564 void
mtcallback(void * v,int action)565 mtcallback(void *v, int action)
566 {
567 struct mt_softc *sc = v;
568
569 DPRINTF(MDB_FOLLOW, ("mtcallback: v=%p, action=%d\n", v, action));
570
571 switch (action) {
572 case GPIBCBF_START:
573 mtstart(sc);
574 break;
575 case GPIBCBF_INTR:
576 mtintr(sc);
577 break;
578 #ifdef DEBUG
579 default:
580 printf("mtcallback: unknown action %d\n", action);
581 break;
582 #endif
583 }
584 }
585
586 void
mtintr_callout(void * arg)587 mtintr_callout(void *arg)
588 {
589 struct mt_softc *sc = arg;
590 int s = splbio();
591
592 gpibppclear(sc->sc_ic);
593 mtintr(sc);
594 splx(s);
595 }
596
597 void
mtstart_callout(void * arg)598 mtstart_callout(void *arg)
599 {
600 int s = splbio();
601
602 mtstart((struct mt_softc *)arg);
603 splx(s);
604 }
605
606 void
mtstart(struct mt_softc * sc)607 mtstart(struct mt_softc *sc)
608 {
609 struct buf *bp;
610 short cmdcount = 1;
611 u_char cmdbuf[2];
612
613 DPRINTF(MDB_ANY, ("%s start", device_xname(sc->sc_dev)));
614 sc->sc_flags &= ~MTF_WRT;
615 bp = bufq_peek(sc->sc_tab);
616 if ((sc->sc_flags & MTF_ALIVE) == 0 &&
617 ((bp->b_flags & B_CMD) == 0 || bp->b_cmd != MTRESET))
618 goto fatalerror;
619
620 if (sc->sc_flags & MTF_REW) {
621 if (!gpibpptest(sc->sc_ic, sc->sc_slave))
622 goto stillrew;
623 switch (mtreaddsj(sc, MTE_DSJ_FORCE|MTE_COMPLETE|MTE_IDLE)) {
624 case 0:
625 case 1:
626 stillrew:
627 if ((sc->sc_stat1 & SR1_BOT) ||
628 !(sc->sc_stat1 & SR1_ONLINE)) {
629 sc->sc_flags &= ~MTF_REW;
630 break;
631 }
632 /* FALLTHROUGH */
633 case -2:
634 /*
635 * -2 means "timeout" reading DSJ, which is probably
636 * temporary. This is considered OK when doing a NOP,
637 * but not otherwise.
638 */
639 if (sc->sc_flags & (MTF_DSJTIMEO | MTF_STATTIMEO)) {
640 callout_reset(&sc->sc_start_ch, hz >> 5,
641 mtstart_callout, sc);
642 return;
643 }
644 /* FALLTHROUGH */
645 case 2:
646 if (bp->b_cmd != MTNOP || !(bp->b_flags & B_CMD)) {
647 bp->b_error = EBUSY;
648 goto done;
649 }
650 goto done;
651
652 default:
653 goto fatalerror;
654 }
655 }
656 if (bp->b_flags & B_CMD) {
657 if (sc->sc_flags & MTF_PASTEOT) {
658 switch(bp->b_cmd) {
659 case MTFSF:
660 case MTWEOF:
661 case MTFSR:
662 bp->b_error = ENOSPC;
663 goto done;
664
665 case MTBSF:
666 case MTOFFL:
667 case MTBSR:
668 case MTREW:
669 sc->sc_flags &= ~(MTF_PASTEOT | MTF_ATEOT);
670 break;
671 }
672 }
673 switch(bp->b_cmd) {
674 case MTFSF:
675 if (sc->sc_flags & MTF_HITEOF)
676 goto done;
677 cmdbuf[0] = MTTC_FSF;
678 break;
679
680 case MTBSF:
681 if (sc->sc_flags & MTF_HITBOF)
682 goto done;
683 cmdbuf[0] = MTTC_BSF;
684 break;
685
686 case MTOFFL:
687 sc->sc_flags |= MTF_REW;
688 cmdbuf[0] = MTTC_REWOFF;
689 break;
690
691 case MTWEOF:
692 cmdbuf[0] = MTTC_WFM;
693 break;
694
695 case MTBSR:
696 cmdbuf[0] = MTTC_BSR;
697 break;
698
699 case MTFSR:
700 cmdbuf[0] = MTTC_FSR;
701 break;
702
703 case MTREW:
704 sc->sc_flags |= MTF_REW;
705 cmdbuf[0] = MTTC_REW;
706 break;
707
708 case MTNOP:
709 /*
710 * NOP is supposed to set status bits.
711 * Force readdsj to do it.
712 */
713 switch (mtreaddsj(sc,
714 MTE_DSJ_FORCE | MTE_COMPLETE | MTE_IDLE)) {
715 default:
716 goto done;
717
718 case -1:
719 /*
720 * If this fails, perform a device clear
721 * to fix any protocol problems and (most
722 * likely) get the status.
723 */
724 bp->b_cmd = MTRESET;
725 break;
726
727 case -2:
728 callout_reset(&sc->sc_start_ch, hz >> 5,
729 mtstart_callout, sc);
730 return;
731 }
732 __unreachable();
733
734 case MTRESET:
735 /*
736 * 1) selected device clear (send with "-2" secondary)
737 * 2) set timeout, then wait for "service request"
738 * 3) interrupt will read DSJ (and END COMPLETE-IDLE)
739 */
740 if (gpibsend(sc->sc_ic, sc->sc_slave, -2, NULL, 0)){
741 aprint_error_dev(sc->sc_dev, "can't reset");
742 goto fatalerror;
743 }
744 callout_reset(&sc->sc_intr_ch, 4*hz, mtintr_callout,
745 sc);
746 gpibawait(sc->sc_ic);
747 return;
748
749 case MTSET800BPI:
750 cmdbuf[0] = MTTC_800;
751 break;
752
753 case MTSET1600BPI:
754 cmdbuf[0] = MTTC_1600;
755 break;
756
757 case MTSET6250BPI:
758 cmdbuf[0] = MTTC_6250;
759 break;
760
761 case MTSET6250DC:
762 cmdbuf[0] = MTTC_DC6250;
763 break;
764 }
765 } else {
766 if (sc->sc_flags & MTF_PASTEOT) {
767 bp->b_error = ENOSPC;
768 goto done;
769 }
770 if (bp->b_flags & B_READ) {
771 sc->sc_flags |= MTF_IO;
772 cmdbuf[0] = MTTC_READ;
773 } else {
774 sc->sc_flags |= MTF_WRT | MTF_IO;
775 cmdbuf[0] = MTTC_WRITE;
776 cmdbuf[1] = (bp->b_bcount +((1 << WRITE_BITS_IGNORED) - 1)) >> WRITE_BITS_IGNORED;
777 cmdcount = 2;
778 }
779 }
780 if (gpibsend(sc->sc_ic, sc->sc_slave, MTL_TCMD, cmdbuf, cmdcount)
781 == cmdcount) {
782 if (sc->sc_flags & MTF_REW)
783 goto done;
784 gpibawait(sc->sc_ic);
785 return;
786 }
787 fatalerror:
788 /*
789 * If anything fails, the drive is probably hosed, so mark it not
790 * "ALIVE" (but it EXISTS and is OPEN or we wouldn't be here, and
791 * if, last we heard, it was REWinding, remember that).
792 */
793 sc->sc_flags &= MTF_EXISTS | MTF_OPEN | MTF_REW;
794 bp->b_error = EIO;
795 done:
796 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
797 (void)bufq_get(sc->sc_tab);
798 biodone(bp);
799 gpibrelease(sc->sc_ic, sc->sc_hdl);
800 if ((bp = bufq_peek(sc->sc_tab)) == NULL)
801 sc->sc_active = 0;
802 else
803 mtustart(sc);
804 }
805
806 void
mtintr(struct mt_softc * sc)807 mtintr(struct mt_softc *sc)
808 {
809 struct buf *bp;
810 int slave, dir, i;
811 u_char cmdbuf[4];
812
813 slave = sc->sc_slave;
814
815 bp = bufq_peek(sc->sc_tab);
816 if (bp == NULL) {
817 printf("%s intr: bp == NULL", device_xname(sc->sc_dev));
818 return;
819 }
820
821 DPRINTF(MDB_ANY, ("%s intr", device_xname(sc->sc_dev)));
822
823 /*
824 * Some operation completed. Read status bytes and report errors.
825 * Clear EOF flags here `cause they're set once on specific conditions
826 * below when a command succeeds.
827 * A DSJ of 2 always means keep waiting. If the command was READ
828 * (and we're in data DMA phase) stop data transfer first.
829 */
830 sc->sc_flags &= ~(MTF_HITEOF | MTF_HITBOF);
831 if ((bp->b_flags & (B_CMD|B_READ)) == B_READ &&
832 !(sc->sc_flags & (MTF_IO | MTF_STATTIMEO | MTF_DSJTIMEO))){
833 cmdbuf[0] = MTE_STOP;
834 (void) gpibsend(sc->sc_ic, slave, MTL_ECMD,cmdbuf,1);
835 }
836 switch (mtreaddsj(sc, 0)) {
837 case 0:
838 break;
839
840 case 1:
841 /*
842 * If we're in the middle of a READ/WRITE and have yet to
843 * start the data transfer, a DSJ of one should terminate it.
844 */
845 sc->sc_flags &= ~MTF_IO;
846 break;
847
848 case 2:
849 (void) gpibawait(sc->sc_ic);
850 return;
851
852 case -2:
853 /*
854 * -2 means that the drive failed to respond quickly enough
855 * to the request for DSJ. It's probably just "busy" figuring
856 * it out and will know in a little bit...
857 */
858 callout_reset(&sc->sc_intr_ch, hz >> 5, mtintr_callout, sc);
859 return;
860
861 default:
862 printf("%s intr: can't get drive stat",
863 device_xname(sc->sc_dev));
864 goto error;
865 }
866 if (sc->sc_stat1 & (SR1_ERR | SR1_REJECT)) {
867 i = sc->sc_stat4 & SR4_ERCLMASK;
868 printf("%s: %s error, retry %d, SR2/3 %x/%x, code %d",
869 device_xname(sc->sc_dev), i == SR4_DEVICE ? "device" :
870 (i == SR4_PROTOCOL ? "protocol" :
871 (i == SR4_SELFTEST ? "selftest" : "unknown")),
872 sc->sc_stat4 & SR4_RETRYMASK, sc->sc_stat2,
873 sc->sc_stat3, sc->sc_stat5);
874
875 if ((bp->b_flags & B_CMD) && bp->b_cmd == MTRESET)
876 callout_stop(&sc->sc_intr_ch);
877 if (sc->sc_stat3 & SR3_POWERUP)
878 sc->sc_flags &= MTF_OPEN | MTF_EXISTS;
879 goto error;
880 }
881 /*
882 * Report and clear any soft errors.
883 */
884 if (sc->sc_stat1 & SR1_SOFTERR) {
885 printf("%s: soft error, retry %d\n", device_xname(sc->sc_dev),
886 sc->sc_stat4 & SR4_RETRYMASK);
887 sc->sc_stat1 &= ~SR1_SOFTERR;
888 }
889 /*
890 * We've initiated a read or write, but haven't actually started to
891 * DMA the data yet. At this point, the drive's ready.
892 */
893 if (sc->sc_flags & MTF_IO) {
894 sc->sc_flags &= ~MTF_IO;
895 dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
896 gpibxfer(sc->sc_ic, slave,
897 dir == GPIB_READ ? MTT_READ : MTL_WRITE,
898 bp->b_data, bp->b_bcount, dir, dir == GPIB_READ);
899 return;
900 }
901 /*
902 * Check for End Of Tape - we're allowed to hit EOT and then write (or
903 * read) one more record. If we get here and have not already hit EOT,
904 * return ENOSPC to inform the process that it's hit it. If we get
905 * here and HAVE already hit EOT, don't allow any more operations that
906 * move the tape forward.
907 */
908 if (sc->sc_stat1 & SR1_EOT) {
909 if (sc->sc_flags & MTF_ATEOT)
910 sc->sc_flags |= MTF_PASTEOT;
911 else {
912 bp->b_error = ENOSPC;
913 sc->sc_flags |= MTF_ATEOT;
914 }
915 }
916 /*
917 * If a motion command was being executed, check for Tape Marks.
918 * If we were doing data, make sure we got the right amount, and
919 * check for hitting tape marks on reads.
920 */
921 if (bp->b_flags & B_CMD) {
922 if (sc->sc_stat1 & SR1_EOF) {
923 if (bp->b_cmd == MTFSR)
924 sc->sc_flags |= MTF_HITEOF;
925 if (bp->b_cmd == MTBSR)
926 sc->sc_flags |= MTF_HITBOF;
927 }
928 if (bp->b_cmd == MTRESET) {
929 callout_stop(&sc->sc_intr_ch);
930 sc->sc_flags |= MTF_ALIVE;
931 }
932 } else {
933 i = gpibrecv(sc->sc_ic, slave, MTT_BCNT, cmdbuf, 2);
934 if (i != 2) {
935 aprint_error_dev(sc->sc_dev,
936 "intr: can't get xfer length\n");
937 goto error;
938 }
939 i = (int) *((u_short *) cmdbuf);
940 if (i <= bp->b_bcount) {
941 if (i == 0)
942 sc->sc_flags |= MTF_HITEOF;
943 bp->b_resid = bp->b_bcount - i;
944 DPRINTF(MDB_ANY, ("%s intr: bcount %d, resid %d",
945 device_xname(sc->sc_dev),
946 bp->b_bcount, bp->b_resid));
947 } else {
948 tprintf(sc->sc_ttyp,
949 "%s: record (%d) larger than wanted (%d)\n",
950 device_xname(sc->sc_dev), i, bp->b_bcount);
951 error:
952 sc->sc_flags &= ~MTF_IO;
953 bp->b_error = EIO;
954 }
955 }
956 /*
957 * The operation is completely done.
958 * Let the drive know with an END command.
959 */
960 cmdbuf[0] = MTE_COMPLETE | MTE_IDLE;
961 (void) gpibsend(sc->sc_ic, slave, MTL_ECMD, cmdbuf, 1);
962 bp->b_flags &= ~B_CMD;
963 (void)bufq_get(sc->sc_tab);
964 biodone(bp);
965 gpibrelease(sc->sc_ic, sc->sc_hdl);
966 if (bufq_peek(sc->sc_tab) == NULL)
967 sc->sc_active = 0;
968 else
969 mtustart(sc);
970 }
971
972 int
mtread(dev_t dev,struct uio * uio,int flags)973 mtread(dev_t dev, struct uio *uio, int flags)
974 {
975 return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio));
976 }
977
978 int
mtwrite(dev_t dev,struct uio * uio,int flags)979 mtwrite(dev_t dev, struct uio *uio, int flags)
980 {
981 return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio));
982 }
983
984 int
mtioctl(dev_t dev,u_long cmd,void * data,int flag,struct lwp * l)985 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
986 {
987 struct mtop *op;
988 int cnt;
989
990 switch (cmd) {
991 case MTIOCTOP:
992 op = (struct mtop *)data;
993 switch(op->mt_op) {
994 case MTWEOF:
995 case MTFSF:
996 case MTBSR:
997 case MTBSF:
998 case MTFSR:
999 cnt = op->mt_count;
1000 break;
1001
1002 case MTOFFL:
1003 case MTREW:
1004 case MTNOP:
1005 cnt = 0;
1006 break;
1007
1008 default:
1009 return (EINVAL);
1010 }
1011 return (mtcommand(dev, op->mt_op, cnt));
1012
1013 case MTIOCGET:
1014 break;
1015
1016 default:
1017 return (EINVAL);
1018 }
1019 return (0);
1020 }
1021