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