ts.c revision 1.30 1 /* $NetBSD: ts.c,v 1.30 2014/07/25 08:02:20 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 1991 The Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)tmscp.c 7.16 (Berkeley) 5/9/91
32 */
33
34 /*
35 * sccsid = "@(#)tmscp.c 1.24 (ULTRIX) 1/21/86";
36 */
37
38 /************************************************************************
39 * *
40 * Licensed from Digital Equipment Corporation *
41 * Copyright (c) *
42 * Digital Equipment Corporation *
43 * Maynard, Massachusetts *
44 * 1985, 1986 *
45 * All rights reserved. *
46 * *
47 * The Information in this software is subject to change *
48 * without notice and should not be construed as a commitment *
49 * by Digital Equipment Corporation. Digital makes no *
50 * representations about the suitability of this software for *
51 * any purpose. It is supplied "As Is" without expressed or *
52 * implied warranty. *
53 * *
54 * If the Regents of the University of California or its *
55 * licensees modify the software in a manner creating *
56 * derivative copyright rights, appropriate copyright *
57 * legends may be placed on the derivative work in addition *
58 * to that set forth above. *
59 * *
60 ************************************************************************/
61
62 /*
63 * TSV05/TS05 device driver, written by Bertram Barth.
64 *
65 * should be TS11 compatible (untested)
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: ts.c,v 1.30 2014/07/25 08:02:20 dholland Exp $");
70
71 #undef TSDEBUG
72
73 /*
74 * TODO:
75 *
76 * Keep track of tape position so that lseek et al works.
77 * Use tprintf to inform the user, not the system console.
78 */
79
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/kernel.h>
84 #include <sys/buf.h>
85 #include <sys/bufq.h>
86 #include <sys/conf.h>
87 #include <sys/device.h>
88 #include <sys/errno.h>
89 #include <sys/file.h>
90 #include <sys/syslog.h>
91 #include <sys/ioctl.h>
92 #include <sys/mtio.h>
93 #include <sys/uio.h>
94 #include <sys/proc.h>
95
96 #include <sys/bus.h>
97
98 #include <dev/qbus/ubareg.h>
99 #include <dev/qbus/ubavar.h>
100
101 #include <dev/qbus/tsreg.h>
102
103 #include "ioconf.h"
104
105 struct ts {
106 struct cmd cmd; /* command packet */
107 struct chr chr; /* characteristics packet */
108 struct status status; /* status packet */
109 };
110
111 /*
112 * Software status, per controller.
113 * also status per tape-unit, since only one unit per controller
114 * (thus we have no struct ts_info)
115 */
116 struct ts_softc {
117 device_t sc_dev; /* Autoconf ... */
118 struct uba_softc *sc_uh; /* the parent UBA */
119 struct uba_unit sc_unit; /* Struct common for UBA to talk */
120 struct evcnt sc_intrcnt; /* Interrupt counting */
121 struct ubinfo sc_ui; /* mapping info for struct ts */
122 struct uba_unit sc_uu; /* Struct for UBA to communicate */
123 bus_space_tag_t sc_iot;
124 bus_addr_t sc_ioh;
125 bus_dma_tag_t sc_dmat;
126 bus_dmamap_t sc_dmam;
127 volatile struct ts *sc_vts; /* Memory address of ts struct */
128 struct ts *sc_bts; /* Unibus address of ts struct */
129 int sc_type; /* TS11 or TS05? */
130 short sc_waddr; /* Value to write to TSDB */
131 struct bufq_state *sc_bufq; /* pending I/O requests */
132
133 short sc_mapped; /* Unibus map allocated ? */
134 short sc_state; /* see below: ST_xxx */
135 short sc_rtc; /* retry count for lcmd */
136 short sc_openf; /* lock against multiple opens */
137 short sc_liowf; /* last operation was write */
138 struct buf ts_cbuf; /* internal cmd buffer (for ioctls) */
139 };
140
141 #define TS_WCSR(csr, val) \
142 bus_space_write_2(sc->sc_iot, sc->sc_ioh, csr, val)
143 #define TS_RCSR(csr) \
144 bus_space_read_2(sc->sc_iot, sc->sc_ioh, csr)
145
146 #define LOWORD(x) ((int)(x) & 0xffff)
147 #define HIWORD(x) (((int)(x) >> 16) & 0x3f)
148
149 #define TYPE_TS11 0
150 #define TYPE_TS05 1
151 #define TYPE_TU80 2
152
153 #define TS_INVALID 0
154 #define TS_INIT 1
155 #define TS_RUNNING 2
156 #define TS_FASTREPOS 3
157
158 static void tsintr(void *);
159 static void tsinit(struct ts_softc *);
160 static void tscommand(struct ts_softc *, dev_t, int, int);
161 static int tsstart(struct ts_softc *, int);
162 static void tswchar(struct ts_softc *);
163 static bool tsreset(struct ts_softc *);
164 static int tsmatch(device_t, cfdata_t, void *);
165 static void tsattach(device_t, device_t, void *);
166 static int tsready(struct uba_unit *);
167
168 CFATTACH_DECL_NEW(ts, sizeof(struct ts_softc),
169 tsmatch, tsattach, NULL, NULL);
170
171 dev_type_open(tsopen);
172 dev_type_close(tsclose);
173 dev_type_read(tsread);
174 dev_type_write(tswrite);
175 dev_type_ioctl(tsioctl);
176 dev_type_strategy(tsstrategy);
177 dev_type_dump(tsdump);
178
179 const struct bdevsw ts_bdevsw = {
180 .d_open = tsopen,
181 .d_close = tsclose,
182 .d_strategy = tsstrategy,
183 .d_ioctl = tsioctl,
184 .d_dump = tsdump,
185 .d_psize = nosize,
186 .d_discard = nodiscard,
187 .d_flag = D_TAPE
188 };
189
190 const struct cdevsw ts_cdevsw = {
191 .d_open = tsopen,
192 .d_close = tsclose,
193 .d_read = tsread,
194 .d_write = tswrite,
195 .d_ioctl = tsioctl,
196 .d_stop = nostop,
197 .d_tty = notty,
198 .d_poll = nopoll,
199 .d_mmap = nommap,
200 .d_kqfilter = nokqfilter,
201 .d_flag = D_TAPE
202 };
203
204 /* Bits in minor device */
205 #define TS_UNIT(dev) (minor(dev)&03)
206 #define TS_HIDENSITY 010
207
208 #define TS_PRI LOG_INFO
209
210
211 /*
212 * Probe for device. If found, try to raise an interrupt.
213 */
214 int
215 tsmatch(device_t parent, cfdata_t match, void *aux)
216 {
217 struct device tsdev;
218 struct ts_softc ssc;
219 struct ts_softc *sc = &ssc;
220 struct uba_attach_args *ua = aux;
221 int i;
222
223 sc->sc_iot = ua->ua_iot;
224 sc->sc_ioh = ua->ua_ioh;
225 sc->sc_mapped = 0;
226 sc->sc_dev = &tsdev;
227 sc->sc_uh = device_private(parent);
228 strcpy(sc->sc_dev->dv_xname, "ts");
229
230 /* Try to reset the device */
231 for (i = 0; i < 3; i++) {
232 if (tsreset(sc))
233 break;
234 }
235
236 if (i == 3)
237 return 0;
238
239 tsinit(sc);
240 tswchar(sc); /* write charact. to enable interrupts */
241 /* completion of this will raise the intr. */
242
243 DELAY(1000000); /* Wait for interrupt */
244 ubmemfree(sc->sc_uh, &sc->sc_ui);
245 return 1;
246 }
247
248 /*
249 */
250 void
251 tsattach(device_t parent, device_t self, void *aux)
252 {
253 struct ts_softc *sc = device_private(self);
254 struct uba_attach_args *ua = aux;
255 int error;
256 const char *t;
257
258 sc->sc_dev = self;
259 sc->sc_uh = device_private(parent);
260 sc->sc_iot = ua->ua_iot;
261 sc->sc_ioh = ua->ua_ioh;
262 sc->sc_dmat = ua->ua_dmat;
263
264 sc->sc_uu.uu_dev = self;
265 sc->sc_uu.uu_ready = tsready;
266
267 tsinit(sc); /* reset and map */
268
269 error = bus_dmamap_create(sc->sc_dmat, (64*1024), 16, (64*1024),
270 0, BUS_DMA_NOWAIT, &sc->sc_dmam);
271 if (error) {
272 aprint_error(": failed create DMA map %d\n", error);
273 return;
274 }
275
276 bufq_alloc(&sc->sc_bufq, "fcfs", 0);
277
278 /*
279 * write the characteristics (again)
280 */
281 sc->sc_state = TS_INIT; /* tsintr() checks this ... */
282 tswchar(sc);
283 if (tsleep(sc, PRIBIO, "tsattach", 100)) {
284 aprint_error(": failed SET CHARACTERISTICS\n");
285 return;
286 }
287
288 sc->sc_state = TS_RUNNING;
289 if (sc->sc_uh->uh_type == UBA_UBA) {
290 if (sc->sc_vts->status.xst2 & TS_SF_TU80) {
291 sc->sc_type = TYPE_TU80;
292 t = "TU80";
293 } else {
294 sc->sc_type = TYPE_TS11;
295 t = "TS11";
296 }
297 } else {
298 sc->sc_type = TYPE_TS05;
299 t = "TS05";
300 }
301
302 aprint_normal(": %s\n", t);
303 aprint_normal_dev(sc->sc_dev,
304 "rev %d, extended features %s, transport %s\n",
305 (sc->sc_vts->status.xst2 & TS_SF_MCRL) >> 2,
306 (sc->sc_vts->status.xst2 & TS_SF_EFES ? "enabled" : "disabled"),
307 (TS_RCSR(TSSR) & TS_OFL ? "offline" : "online"));
308
309 uba_intr_establish(ua->ua_icookie, ua->ua_cvec, tsintr,
310 sc, &sc->sc_intrcnt);
311 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, ua->ua_evcnt,
312 device_xname(sc->sc_dev), "intr");
313 }
314
315 /*
316 * Initialize a TS device. Set up UBA mapping registers,
317 * initialize data structures, what else ???
318 */
319 void
320 tsinit(struct ts_softc *sc)
321 {
322 if (sc->sc_mapped == 0) {
323
324 /*
325 * Map the communications area and command and message
326 * buffer into Unibus address space.
327 */
328 sc->sc_ui.ui_size = sizeof(struct ts);
329 if (ubmemalloc(sc->sc_uh, &sc->sc_ui, UBA_CANTWAIT))
330 return;
331 sc->sc_vts = (void *)sc->sc_ui.ui_vaddr;
332 sc->sc_bts = (void *)sc->sc_ui.ui_baddr;
333 sc->sc_waddr = sc->sc_ui.ui_baddr |
334 ((sc->sc_ui.ui_baddr >> 16) & 3);
335 sc->sc_mapped = 1;
336 }
337 tsreset(sc);
338 }
339
340 /*
341 * Execute a (ioctl) command on the tape drive a specified number of times.
342 * This routine sets up a buffer and calls the strategy routine which
343 * issues the command to the controller.
344 */
345 void
346 tscommand(struct ts_softc *sc, dev_t dev, int cmd, int count)
347 {
348 struct buf *bp;
349
350 #ifdef TSDEBUG
351 printf("tscommand (%x, %d)\n", cmd, count);
352 #endif
353
354 bp = &sc->ts_cbuf;
355
356 mutex_enter(&bufcache_lock);
357 while (bp->b_cflags & BC_BUSY) {
358 /*
359 * This special check is because BC_BUSY never
360 * gets cleared in the non-waiting rewind case. ???
361 */
362 if (bp->b_bcount == 0 && (bp->b_oflags & BO_DONE))
363 break;
364 if (bbusy(bp, false, 0, NULL) == 0)
365 break;
366 /* check MOT-flag !!! */
367 }
368 bp->b_flags = B_READ;
369 mutex_exit(&bufcache_lock);
370
371 /*
372 * Load the buffer. The b_count field gets used to hold the command
373 * count. the b_resid field gets used to hold the command mneumonic.
374 * These 2 fields are "known" to be "safe" to use for this purpose.
375 * (Most other drivers also use these fields in this way.)
376 */
377 bp->b_dev = dev;
378 bp->b_bcount = count;
379 bp->b_resid = cmd;
380 bp->b_blkno = 0;
381 bp->b_oflags = 0;
382 bp->b_objlock = &buffer_lock;
383 tsstrategy(bp);
384 /*
385 * In case of rewind from close, don't wait.
386 * This is the only case where count can be 0.
387 */
388 if (count == 0)
389 return;
390 biowait(bp);
391 mutex_enter(&bufcache_lock);
392 cv_broadcast(&bp->b_busy);
393 bp->b_cflags = 0;
394 mutex_exit(&bufcache_lock);
395 }
396
397 /*
398 * Start an I/O operation on TS05 controller
399 */
400 int
401 tsstart(struct ts_softc *sc, int isloaded)
402 {
403 struct buf *bp;
404 int cmd;
405
406 bp = bufq_peek(sc->sc_bufq);
407 if (bp == NULL) {
408 return 0;
409 }
410 #ifdef TSDEBUG
411 printf("buf: %p bcount %ld blkno %d\n", bp, bp->b_bcount, bp->b_blkno);
412 #endif
413 /*
414 * Check if command is an ioctl or not (ie. read or write).
415 * If it's an ioctl then just set the flags for later use;
416 * For other commands attempt to setup a buffer pointer.
417 */
418 if (bp == &sc->ts_cbuf) {
419 switch ((int)bp->b_resid) {
420 case MTWEOF:
421 cmd = TS_CMD_WTM;
422 break;
423 case MTFSF:
424 cmd = TS_CMD_STMF;
425 sc->sc_vts->cmd.cw1 = bp->b_bcount;
426 break;
427 case MTBSF:
428 cmd = TS_CMD_STMR;
429 sc->sc_vts->cmd.cw1 = bp->b_bcount;
430 break;
431 case MTFSR:
432 cmd = TS_CMD_SRF;
433 sc->sc_vts->cmd.cw1 = bp->b_bcount;
434 break;
435 case MTBSR:
436 cmd = TS_CMD_SRR;
437 sc->sc_vts->cmd.cw1 = bp->b_bcount;
438 break;
439 case MTREW:
440 cmd = TS_CMD_RWND;
441 break;
442 case MTOFFL:
443 cmd = TS_CMD_RWUL;
444 break;
445 case MTNOP:
446 cmd = TS_CMD_STAT;
447 break;
448 default:
449 aprint_error_dev(sc->sc_dev, "bad ioctl %d\n",
450 (int)bp->b_resid);
451 /* Need a no-op. get status */
452 cmd = TS_CMD_STAT;
453 } /* end switch (bp->b_resid) */
454 } else {
455 if (isloaded == 0) {
456 /*
457 * now we try to map the buffer into uba map space (???)
458 */
459 if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmam,
460 bp->b_data,
461 bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT)) {
462 uba_enqueue(&sc->sc_uu);
463 return 0;
464 }
465 sc->sc_rtc = 0;
466 }
467 sc->sc_vts->cmd.cw1 = LOWORD(sc->sc_dmam->dm_segs[0].ds_addr);
468 sc->sc_vts->cmd.cw2 = HIWORD(sc->sc_dmam->dm_segs[0].ds_addr);
469 sc->sc_vts->cmd.cw3 = bp->b_bcount;
470 bp->b_error = 0; /* Used for error count */
471 #ifdef TSDEBUG
472 printf("tsstart-1: err %d\n", bp->b_error);
473 #endif
474 if (bp->b_flags & B_READ)
475 cmd = TS_CMD_RNF;
476 else
477 cmd = TS_CMD_WD;
478 }
479
480 /*
481 * Now that the command-buffer is setup, give it to the controller
482 */
483 sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | cmd;
484 #ifdef TSDEBUG
485 printf("tsstart: sending cmdr %x\n", sc->sc_vts->cmd.cmdr);
486 #endif
487 TS_WCSR(TSDB, sc->sc_waddr);
488 return 1;
489 }
490
491 /*
492 * Called when there are free uba resources.
493 */
494 int
495 tsready(struct uba_unit *uu)
496 {
497 struct ts_softc *sc = device_private(uu->uu_dev);
498 struct buf *bp = bufq_peek(sc->sc_bufq);
499
500 if (bus_dmamap_load(sc->sc_dmat, sc->sc_dmam, bp->b_data,
501 bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT))
502 return 0;
503
504 tsstart(sc, 1);
505 return 1;
506 }
507
508 /*
509 * initialize the controller by sending WRITE CHARACTERISTICS command.
510 * contents of command- and message-buffer are assembled during this
511 * function.
512 */
513 void
514 tswchar(struct ts_softc *sc)
515 {
516 /*
517 * assemble and send "WRITE CHARACTERISTICS" command
518 */
519
520 sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | TS_CMD_WCHAR;
521 sc->sc_vts->cmd.cw1 = LOWORD(&sc->sc_bts->chr);
522 sc->sc_vts->cmd.cw2 = HIWORD(&sc->sc_bts->chr);
523 sc->sc_vts->cmd.cw3 = 010; /* size of charact.-data */
524
525 sc->sc_vts->chr.sadrl = LOWORD(&sc->sc_bts->status);
526 sc->sc_vts->chr.sadrh = HIWORD(&sc->sc_bts->status);
527 sc->sc_vts->chr.onesix = (sc->sc_type == TYPE_TS05 ? 020 : 016);
528 sc->sc_vts->chr.chrw = TS_WC_ESS;
529 sc->sc_vts->chr.xchrw = TS_WCX_HSP|TS_WCX_RBUF|TS_WCX_WBUF;
530
531 TS_WCSR(TSDB, sc->sc_waddr);
532 }
533
534 /*
535 * Reset the TS11. Return 1 if failed, 0 if succeeded.
536 */
537 bool
538 tsreset(struct ts_softc *sc)
539 {
540 int timeout;
541
542 /*
543 * reset ctlr by writing into TSSR, then write characteristics
544 */
545 timeout = 0; /* timeout in 10 seconds */
546 TS_WCSR(TSSR, 0); /* start initialization */
547 while ((TS_RCSR(TSSR) & TS_SSR) == 0) {
548 DELAY(10000);
549 if (timeout++ > 1000)
550 return false;
551 }
552 return true;
553 }
554
555 static void
556 prtstat(struct ts_softc *sc, int sr)
557 {
558 char buf[100];
559
560 snprintb(buf, sizeof(buf), TS_TSSR_BITS, sr);
561 aprint_normal_dev(sc->sc_dev, "TSSR=%s\n", buf);
562 snprintb(buf, sizeof(buf), TS_XST0_BITS, sc->sc_vts->status.xst0);
563 aprint_normal_dev(sc->sc_dev, "XST0=%s\n", buf);
564 }
565
566 /*
567 * TSV05/TS05 interrupt routine
568 */
569 static void
570 tsintr(void *arg)
571 {
572 struct ts_softc *sc = arg;
573 struct buf *bp;
574
575 unsigned short sr = TS_RCSR(TSSR); /* save TSSR */
576 unsigned short mh = sc->sc_vts->status.hdr; /* and msg-header */
577 /* clear the message header ??? */
578
579 short ccode = sc->sc_vts->cmd.cmdr & TS_CF_CCODE;
580
581 bp = bufq_peek(sc->sc_bufq);
582 #ifdef TSDEBUG
583 {
584 char buf[100];
585 snprintb(buf, sizeof(buf), TS_TSSR_BITS, sr);
586 printf("tsintr: sr %x mh %x\n", sr, mh);
587 printf("srbits: %s\n", buf);
588 }
589 #endif
590 /*
591 * There are two different things which can (and should) be checked:
592 * the actual (internal) state and the device's result (tssr/msg.hdr)
593 *
594 * For each state there's only one "normal" interrupt. Anything else
595 * has to be checked more intensively. Thus in a first run according
596 * to the internal state the expected interrupt is checked/handled.
597 *
598 * In a second run the remaining (not yet handled) interrupts are
599 * checked according to the drive's result.
600 */
601 switch (sc->sc_state) {
602
603 case TS_INVALID:
604 /*
605 * Ignore unsolicited interrupts.
606 */
607 log(LOG_WARNING, "%s: stray intr [%x,%x]\n",
608 device_xname(sc->sc_dev), sr, mh);
609 return;
610
611 case TS_INIT:
612 /*
613 * Init phase ready.
614 */
615 wakeup(sc);
616 return;
617
618 case TS_RUNNING:
619 case TS_FASTREPOS:
620 /*
621 * Here we expect interrupts indicating the end of
622 * commands or indicating problems.
623 */
624 /*
625 * Anything else is handled outside this switch ...
626 */
627 break;
628
629 default:
630 aprint_error_dev(sc->sc_dev,
631 "unexpected interrupt during state %d [%x,%x]\n",
632 sc->sc_state, sr, mh);
633 return;
634 }
635
636 /*
637 * now we check the termination class.
638 */
639 switch (sr & TS_TC) {
640
641 case TS_TC_NORM:
642 /*
643 * Normal termination -- The operation is completed
644 * witout incident.
645 */
646 if (sc->sc_state == TS_FASTREPOS) {
647 #ifdef TSDEBUG
648 printf("Fast repos interrupt\n");
649 #endif
650 /* Fast repos succeeded, start normal data xfer */
651 sc->sc_state = TS_RUNNING;
652 tsstart(sc, 1);
653 return;
654 }
655 sc->sc_liowf = (ccode == TS_CC_WRITE);
656 break;
657
658 case TS_TC_ATTN:
659 /*
660 * Attention condition -- this code indicates that the
661 * drive has undergone a status change, such as going
662 * off-line or coming on-line.
663 * (Without EAI enabled, no Attention interrupts occur.
664 * drive status changes are signaled by the VCK flag.)
665 */
666 return;
667
668 case TS_TC_TSA:
669 /*
670 * Tape Status Alert -- A status condition is encountered
671 * that may have significance to the program. Bits of
672 * interest in the extended status registers include
673 * TMK, EOT and RLL.
674 */
675 #ifdef TSDEBUG
676 {
677 char buf[100];
678 snprintb(buf, sizeof(buf),
679 TS_XST0_BITS, sc->sc_vts->status.xst0);
680 printf("TSA: sr %x bits %s\n",
681 sc->sc_vts->status.xst0, buf);
682 }
683 #endif
684 if (sc->sc_vts->status.xst0 & TS_SF_TMK) {
685 #ifdef TSDEBUG
686 printf(("Tape Mark detected"));
687 #endif
688 /* Read to end-of-file. No error. */
689 break;
690 }
691 if (sc->sc_vts->status.xst0 & TS_SF_EOT) {
692 /* End of tape. Bad. */
693 #ifdef TSDEBUG
694 printf("TS_TC_TSA: EOT\n");
695 #endif
696 if (bp != NULL)
697 bp->b_error = EIO;
698 break;
699 }
700 if (sc->sc_vts->status.xst0 & TS_SF_RLS)
701 break;
702 #ifndef TSDEBUG
703 {
704 char buf[100];
705 snprintb(buf, sizeof(buf),
706 TS_XST0_BITS, sc->sc_vts->status.xst0);
707 printf("TSA: sr %x bits %s\n",
708 sc->sc_vts->status.xst0, buf);
709 }
710 #endif
711 break;
712
713 case TS_TC_FR:
714 /*
715 * Function Reject -- The specified function was not
716 * initiated. Bits of interest include OFL, VCK, BOT,
717 * WLE, ILC and ILA.
718 */
719 if (sr & TS_OFL)
720 printf("tape is off-line.\n");
721 #ifdef TSDEBUG
722 {
723 char buf[100];
724 snprintb(buf, sizeof(buf),
725 TS_XST0_BITS, sc->sc_vts->status.xst0);
726 printf("FR: sr %x bits %s\n",
727 sc->sc_vts->status.xst0, buf);
728 }
729 #endif
730 if (sc->sc_vts->status.xst0 & TS_SF_VCK)
731 printf("Volume check\n");
732 if (sc->sc_vts->status.xst0 & TS_SF_BOT)
733 printf("Start of tape.\n");
734 if (sc->sc_vts->status.xst0 & TS_SF_WLE)
735 printf("Write Lock Error\n");
736 if (sc->sc_vts->status.xst0 & TS_SF_EOT)
737 printf("End of tape.\n");
738 break;
739
740 case TS_TC_TPD:
741 /*
742 * Recoverable Error -- Tape position is a record beyond
743 * what its position was when the function was initiated.
744 * Suggested recovery procedure is to log the error and
745 * issue the appropriate retry command.
746 *
747 * Do a fast repositioning one record back.
748 */
749 sc->sc_state = TS_FASTREPOS;
750 #ifdef TSDEBUG
751 printf("TS_TC_TPD: errcnt %d\n", sc->sc_rtc);
752 #endif
753 if (sc->sc_rtc++ == 8) {
754 aprint_error_dev(sc->sc_dev, "failed 8 retries\n");
755 prtstat(sc, sr);
756 if (bp != NULL)
757 bp->b_error = EIO;
758 break;
759 }
760 sc->sc_vts->cmd.cmdr = TS_CF_ACK | TS_CF_IE | TS_CMD_SRR;
761 sc->sc_vts->cmd.cw1 = 1;
762 TS_WCSR(TSDB, sc->sc_waddr);
763 return;
764
765 case TS_TC_TNM:
766 /*
767 * Recoverable Error -- Tape position has not changed.
768 * Suggested recovery procedure is to log the error and
769 * reissue the original command.
770 */
771 if (sc->sc_rtc++ == 8) {
772 aprint_error_dev(sc->sc_dev, "failed 8 retries\n");
773 prtstat(sc, sr);
774 if (bp != NULL)
775 bp->b_error = EIO;
776 break;
777 }
778 tsstart(sc, 1);
779 return;
780
781 case TS_TC_TPL:
782 /*
783 * Unrecoverable Error -- Tape position has been lost.
784 * No valid recovery procedures exist unless the tape
785 * has labels or sequence numbers.
786 */
787 aprint_error_dev(sc->sc_dev, "tape position lost\n");
788 if (bp != NULL)
789 bp->b_error = EIO;
790 break;
791
792 case TS_TC_FCE:
793 /*
794 * Fatal subsytem Error -- The subsytem is incapable
795 * of properly performing commands, or at least its
796 * integrity is seriously questionable. Refer to the
797 * fatal class code field in the TSSR register for
798 * additional information on the type of fatal error.
799 */
800 aprint_error_dev(sc->sc_dev, "fatal controller error\n");
801 prtstat(sc, sr);
802 break;
803
804 default:
805 aprint_error_dev(sc->sc_dev,
806 "error 0x%x, resetting controller\n", sr & TS_TC);
807 tsreset(sc);
808 }
809 if ((bp = bufq_get(sc->sc_bufq)) != NULL) {
810 #ifdef TSDEBUG
811 printf("tsintr2: que %p\n", bufq_peek(sc->sc_bufq));
812 #endif
813 if (bp != &sc->ts_cbuf) { /* no ioctl */
814 bus_dmamap_unload(sc->sc_dmat, sc->sc_dmam);
815 uba_done(sc->sc_uh);
816 }
817 bp->b_resid = sc->sc_vts->status.rbpcr;
818 biodone (bp);
819 }
820 tsstart(sc, 0);
821 }
822
823
824 /*
825 * Open a ts device and set the unit online. If the controller is not
826 * in the run state, call init to initialize the ts controller first.
827 */
828 int
829 tsopen(dev_t dev, int flag, int type, struct lwp *l)
830 {
831 struct ts_softc *sc = device_lookup_private(&ts_cd, TS_UNIT(dev));
832
833 if (sc == NULL)
834 return ENXIO;
835
836 if (sc->sc_state < TS_RUNNING)
837 return ENXIO;
838
839 if (sc->sc_openf)
840 return EBUSY;
841 sc->sc_openf = 1;
842
843 /*
844 * check if transport is really online.
845 * (without attention-interrupts enabled, we really don't know
846 * the actual state of the transport. Thus we call get-status
847 * (ie. MTNOP) once and check the actual status.)
848 */
849 if (TS_RCSR(TSSR) & TS_OFL) {
850 uprintf("%s: transport is offline.\n", device_xname(sc->sc_dev));
851 sc->sc_openf = 0;
852 return EIO; /* transport is offline */
853 }
854 tscommand(sc, dev, MTNOP, 1);
855 if ((flag & FWRITE) && (sc->sc_vts->status.xst0 & TS_SF_WLK)) {
856 uprintf("%s: no write ring.\n", device_xname(sc->sc_dev));
857 sc->sc_openf = 0;
858 return EROFS;
859 }
860 if (sc->sc_vts->status.xst0 & TS_SF_VCK) {
861 sc->sc_vts->cmd.cmdr = TS_CF_CVC|TS_CF_ACK;
862 TS_WCSR(TSDB, sc->sc_waddr);
863 }
864 tscommand(sc, dev, MTNOP, 1);
865 #ifdef TSDEBUG
866 {
867 char buf[100];
868 snprintb(buf, sizeof(buf),
869 TS_XST0_BITS, sc->sc_vts->status.xst0);
870 printf("tsopen: xst0 %s\n", buf);
871 }
872 #endif
873 sc->sc_liowf = 0;
874 return 0;
875 }
876
877
878 /*
879 * Close tape device.
880 *
881 * If tape was open for writing or last operation was
882 * a write, then write two EOF's and backspace over the last one.
883 * Unless this is a non-rewinding special file, rewind the tape.
884 *
885 * Make the tape available to others, by clearing openf flag.
886 */
887 int
888 tsclose(dev_t dev, int flag, int type, struct lwp *l)
889 {
890 struct ts_softc *sc = device_lookup_private(&ts_cd, TS_UNIT(dev));
891
892 if (flag == FWRITE || ((flag & FWRITE) && sc->sc_liowf)) {
893 /*
894 * We are writing two tape marks (EOT), but place the tape
895 * before the second one, so that another write operation
896 * will overwrite the second one and leave and EOF-mark.
897 */
898 tscommand(sc, dev, MTWEOF, 1); /* Write Tape Mark */
899 tscommand(sc, dev, MTWEOF, 1); /* Write Tape Mark */
900 tscommand(sc, dev, MTBSF, 1); /* Skip Tape Marks Reverse */
901 }
902
903 if ((dev & T_NOREWIND) == 0)
904 tscommand(sc, dev, MTREW, 0);
905
906 sc->sc_openf = 0;
907 sc->sc_liowf = 0;
908 return 0;
909 }
910
911
912 /*
913 * Manage buffers and perform block mode read and write operations.
914 */
915 void
916 tsstrategy(struct buf *bp)
917 {
918 struct ts_softc *sc = device_lookup_private(&ts_cd, TS_UNIT(bp->b_dev));
919 bool empty;
920 int s;
921
922 #ifdef TSDEBUG
923 printf("buf: %p bcount %ld blkno %d\n", bp, bp->b_bcount, bp->b_blkno);
924 #endif
925 s = splbio ();
926 empty = (bufq_peek(sc->sc_bufq) == NULL);
927 bufq_put(sc->sc_bufq, bp);
928 if (empty)
929 tsstart(sc, 0);
930 splx(s);
931 }
932
933
934 /*
935 * Catch ioctl commands, and call the "command" routine to do them.
936 */
937 int
938 tsioctl(dev_t dev,
939 u_long cmd,
940 void *data,
941 int flag,
942 struct lwp *l)
943 {
944 struct buf *bp;
945 struct ts_softc * const sc = device_lookup_private(&ts_cd, TS_UNIT(dev));
946 struct mtop *mtop; /* mag tape cmd op to perform */
947 struct mtget *mtget; /* mag tape struct to get info in */
948 int callcount; /* number of times to call routine */
949 int scount; /* number of files/records to space */
950 int spaceop = 0; /* flag for skip/space operation */
951 int error = 0;
952
953 #ifdef TSDEBUG
954 printf("tsioctl (%x, %lx, %p, %d)\n", dev, cmd, data, flag);
955 #endif
956
957 bp = &sc->ts_cbuf;
958
959 switch (cmd) {
960 case MTIOCTOP: /* do a mag tape op */
961 mtop = (struct mtop *)data;
962 switch (mtop->mt_op) {
963 case MTWEOF: /* write an end-of-file record */
964 callcount = mtop->mt_count;
965 scount = 1;
966 break;
967 case MTFSR: /* forward space record */
968 case MTBSR: /* backward space record */
969 spaceop = 1;
970 case MTFSF: /* forward space file */
971 case MTBSF: /* backward space file */
972 callcount = 1;
973 scount = mtop->mt_count;
974 break;
975 case MTREW: /* rewind */
976 case MTOFFL: /* rewind and put the drive offline */
977 case MTNOP: /* no operation, sets status only */
978 callcount = 1;
979 scount = 1; /* wait for this rewind */
980 break;
981 case MTRETEN: /* retension */
982 case MTERASE: /* erase entire tape */
983 case MTEOM: /* forward to end of media */
984 case MTNBSF: /* backward space to begin of file */
985 case MTCACHE: /* enable controller cache */
986 case MTNOCACHE: /* disable controller cache */
987 case MTSETBSIZ: /* set block size; 0 for variable */
988 case MTSETDNSTY: /* set density code for current mode */
989 printf("ioctl %d not implemented.\n", mtop->mt_op);
990 return (ENXIO);
991 default:
992 #ifdef TSDEBUG
993 printf("invalid ioctl %d\n", mtop->mt_op);
994 #endif
995 return (ENXIO);
996 } /* switch (mtop->mt_op) */
997
998 if (callcount <= 0 || scount <= 0) {
999 #ifdef TSDEBUG
1000 printf("invalid values %d/%d\n", callcount, scount);
1001 #endif
1002 return (EINVAL);
1003 }
1004 do {
1005 tscommand(sc, dev, mtop->mt_op, scount);
1006 if (spaceop && bp->b_resid) {
1007 #ifdef TSDEBUG
1008 printf(("spaceop didn't complete\n"));
1009 #endif
1010 return (EIO);
1011 }
1012 if (bp->b_error != 0) {
1013 #ifdef TSDEBUG
1014 printf("error in ioctl %d\n", mtop->mt_op);
1015 #endif
1016 break;
1017 }
1018 } while (--callcount > 0);
1019 if (bp->b_error != 0)
1020 error = bp->b_error;
1021 return (error);
1022
1023 case MTIOCGET: /* get tape status */
1024 mtget = (struct mtget *)data;
1025 mtget->mt_type = MT_ISTS;
1026 mtget->mt_dsreg = TS_RCSR(TSSR);
1027 mtget->mt_erreg = sc->sc_vts->status.xst0;
1028 mtget->mt_resid = 0; /* ??? */
1029 mtget->mt_density = 0; /* ??? */
1030 break;
1031
1032 case MTIOCIEOT: /* ignore EOT error */
1033 #ifdef TSDEBUG
1034 printf(("MTIOCIEOT not implemented.\n"));
1035 #endif
1036 return (ENXIO);
1037
1038 case MTIOCEEOT: /* enable EOT error */
1039 #ifdef TSDEBUG
1040 printf(("MTIOCEEOT not implemented.\n"));
1041 #endif
1042 return (ENXIO);
1043
1044 default:
1045 #ifdef TSDEBUG
1046 printf("invalid ioctl cmd 0x%lx\n", cmd);
1047 #endif
1048 return (ENXIO);
1049 }
1050
1051 return (0);
1052 }
1053
1054
1055 /*
1056 *
1057 */
1058 int
1059 tsread(dev_t dev, struct uio *uio, int flag)
1060 {
1061 return (physio (tsstrategy, NULL, dev, B_READ, minphys, uio));
1062 }
1063
1064 /*
1065 *
1066 */
1067 int
1068 tswrite(dev_t dev, struct uio *uio, int flag)
1069 {
1070 return (physio (tsstrategy, NULL, dev, B_WRITE, minphys, uio));
1071 }
1072
1073 /*
1074 *
1075 */
1076 int
1077 tsdump(dev_t dev, daddr_t blkno, void *va, size_t size)
1078 {
1079 return EIO;
1080 }
1081