wt.c revision 1.55 1 1.55 gehenna /* $NetBSD: wt.c,v 1.55 2002/09/06 13:18:43 gehenna Exp $ */
2 1.18 cgd
3 1.10 mycroft /*
4 1.10 mycroft * Streamer tape driver.
5 1.10 mycroft * Supports Archive and Wangtek compatible QIC-02/QIC-36 boards.
6 1.10 mycroft *
7 1.10 mycroft * Copyright (C) 1993 by:
8 1.10 mycroft * Sergey Ryzhkov <sir (at) kiae.su>
9 1.10 mycroft * Serge Vakulenko <vak (at) zebub.msk.su>
10 1.10 mycroft *
11 1.10 mycroft * This software is distributed with NO WARRANTIES, not even the implied
12 1.10 mycroft * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 1.1 cgd *
14 1.10 mycroft * Authors grant any other persons or organisations permission to use
15 1.10 mycroft * or modify this software as long as this message is kept with the software,
16 1.10 mycroft * all derivative works or modified versions.
17 1.1 cgd *
18 1.10 mycroft * This driver is derived from the old 386bsd Wangtek streamer tape driver,
19 1.10 mycroft * made by Robert Baron at CMU, based on Intel sources.
20 1.1 cgd */
21 1.1 cgd
22 1.1 cgd /*
23 1.1 cgd * Copyright (c) 1989 Carnegie-Mellon University.
24 1.1 cgd * All rights reserved.
25 1.1 cgd *
26 1.1 cgd * Authors: Robert Baron
27 1.1 cgd *
28 1.1 cgd * Permission to use, copy, modify and distribute this software and
29 1.1 cgd * its documentation is hereby granted, provided that both the copyright
30 1.1 cgd * notice and this permission notice appear in all copies of the
31 1.1 cgd * software, derivative works or modified versions, and any portions
32 1.1 cgd * thereof, and that both notices appear in supporting documentation.
33 1.1 cgd *
34 1.1 cgd * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
35 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
36 1.1 cgd * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
37 1.1 cgd *
38 1.1 cgd * Carnegie Mellon requests users of this software to return to
39 1.1 cgd *
40 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
41 1.1 cgd * School of Computer Science
42 1.1 cgd * Carnegie Mellon University
43 1.1 cgd * Pittsburgh PA 15213-3890
44 1.1 cgd *
45 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
46 1.1 cgd * rights to redistribute these changes.
47 1.1 cgd */
48 1.1 cgd
49 1.1 cgd /*
50 1.1 cgd * Copyright 1988, 1989 by Intel Corporation
51 1.1 cgd */
52 1.53 lukem
53 1.53 lukem #include <sys/cdefs.h>
54 1.55 gehenna __KERNEL_RCSID(0, "$NetBSD: wt.c,v 1.55 2002/09/06 13:18:43 gehenna Exp $");
55 1.1 cgd
56 1.6 mycroft #include <sys/param.h>
57 1.10 mycroft #include <sys/systm.h>
58 1.49 thorpej #include <sys/callout.h>
59 1.10 mycroft #include <sys/kernel.h>
60 1.6 mycroft #include <sys/buf.h>
61 1.10 mycroft #include <sys/fcntl.h>
62 1.10 mycroft #include <sys/malloc.h>
63 1.10 mycroft #include <sys/ioctl.h>
64 1.10 mycroft #include <sys/mtio.h>
65 1.10 mycroft #include <sys/device.h>
66 1.32 christos #include <sys/proc.h>
67 1.32 christos #include <sys/conf.h>
68 1.6 mycroft
69 1.33 mycroft #include <machine/intr.h>
70 1.37 thorpej #include <machine/bus.h>
71 1.6 mycroft #include <machine/pio.h>
72 1.6 mycroft
73 1.24 cgd #include <dev/isa/isavar.h>
74 1.24 cgd #include <dev/isa/isadmavar.h>
75 1.23 cgd #include <dev/isa/wtreg.h>
76 1.1 cgd
77 1.10 mycroft /*
78 1.10 mycroft * Uncomment this to enable internal device tracing.
79 1.10 mycroft */
80 1.36 christos #define WTDBPRINT(x) /* printf x */
81 1.1 cgd
82 1.10 mycroft #define WTPRI (PZERO+10) /* sleep priority */
83 1.1 cgd
84 1.10 mycroft #define WT_NPORT 2 /* 2 i/o ports */
85 1.10 mycroft #define AV_NPORT 4 /* 4 i/o ports */
86 1.10 mycroft
87 1.10 mycroft enum wttype {
88 1.10 mycroft UNKNOWN = 0, /* unknown type, driver disabled */
89 1.10 mycroft ARCHIVE, /* Archive Viper SC499, SC402 etc */
90 1.10 mycroft WANGTEK, /* Wangtek */
91 1.10 mycroft };
92 1.1 cgd
93 1.43 drochner static struct wtregs {
94 1.43 drochner /* controller ports */
95 1.43 drochner int DATAPORT, /* data, read only */
96 1.43 drochner CMDPORT, /* command, write only */
97 1.43 drochner STATPORT, /* status, read only */
98 1.43 drochner CTLPORT, /* control, write only */
99 1.43 drochner SDMAPORT, /* start dma */
100 1.43 drochner RDMAPORT; /* reset dma */
101 1.43 drochner /* status port bits */
102 1.43 drochner u_char BUSY, /* not ready bit define */
103 1.43 drochner NOEXCEP, /* no exception bit define */
104 1.43 drochner RESETMASK, /* to check after reset */
105 1.43 drochner RESETVAL, /* state after reset */
106 1.43 drochner /* control port bits */
107 1.43 drochner ONLINE, /* device selected */
108 1.43 drochner RESET, /* reset command */
109 1.43 drochner REQUEST, /* request command */
110 1.43 drochner IEN; /* enable interrupts */
111 1.43 drochner } wtregs = {
112 1.43 drochner 1, 1, 0, 0, 0, 0,
113 1.43 drochner 0x01, 0x02, 0x07, 0x05,
114 1.43 drochner 0x01, 0x02, 0x04, 0x08
115 1.43 drochner }, avregs = {
116 1.43 drochner 0, 0, 1, 1, 2, 3,
117 1.43 drochner 0x40, 0x20, 0xf8, 0x50,
118 1.43 drochner 0, 0x80, 0x40, 0x20
119 1.43 drochner };
120 1.43 drochner
121 1.10 mycroft struct wt_softc {
122 1.10 mycroft struct device sc_dev;
123 1.24 cgd void *sc_ih;
124 1.1 cgd
125 1.38 jonathan bus_space_tag_t sc_iot;
126 1.38 jonathan bus_space_handle_t sc_ioh;
127 1.44 thorpej isa_chipset_tag_t sc_ic;
128 1.38 jonathan
129 1.49 thorpej struct callout sc_timer_ch;
130 1.49 thorpej
131 1.10 mycroft enum wttype type; /* type of controller */
132 1.21 mycroft int chan; /* dma channel number, 1..3 */
133 1.10 mycroft int flags; /* state of tape drive */
134 1.10 mycroft unsigned dens; /* tape density */
135 1.10 mycroft int bsize; /* tape block size */
136 1.10 mycroft void *buf; /* internal i/o buffer */
137 1.10 mycroft
138 1.10 mycroft void *dmavaddr; /* virtual address of dma i/o buffer */
139 1.10 mycroft size_t dmatotal; /* size of i/o buffer */
140 1.29 mycroft int dmaflags; /* i/o direction */
141 1.10 mycroft size_t dmacount; /* resulting length of dma i/o */
142 1.10 mycroft
143 1.10 mycroft u_short error; /* code for error encountered */
144 1.10 mycroft u_short ercnt; /* number of error blocks */
145 1.10 mycroft u_short urcnt; /* number of underruns */
146 1.1 cgd
147 1.43 drochner struct wtregs regs;
148 1.10 mycroft };
149 1.1 cgd
150 1.55 gehenna dev_type_open(wtopen);
151 1.55 gehenna dev_type_close(wtclose);
152 1.55 gehenna dev_type_read(wtread);
153 1.55 gehenna dev_type_write(wtwrite);
154 1.55 gehenna dev_type_ioctl(wtioctl);
155 1.55 gehenna dev_type_strategy(wtstrategy);
156 1.55 gehenna dev_type_dump(wtdump);
157 1.55 gehenna dev_type_size(wtsize);
158 1.55 gehenna
159 1.55 gehenna const struct bdevsw wt_bdevsw = {
160 1.55 gehenna wtopen, wtclose, wtstrategy, wtioctl, wtdump, wtsize, D_TAPE
161 1.55 gehenna };
162 1.55 gehenna
163 1.55 gehenna const struct cdevsw wt_cdevsw = {
164 1.55 gehenna wtopen, wtclose, wtread, wtwrite, wtioctl,
165 1.55 gehenna nostop, notty, nopoll, nommap, D_TAPE
166 1.55 gehenna };
167 1.32 christos
168 1.10 mycroft int wtwait __P((struct wt_softc *sc, int catch, char *msg));
169 1.10 mycroft int wtcmd __P((struct wt_softc *sc, int cmd));
170 1.10 mycroft int wtstart __P((struct wt_softc *sc, int flag, void *vaddr, size_t len));
171 1.10 mycroft void wtdma __P((struct wt_softc *sc));
172 1.14 cgd void wttimer __P((void *arg));
173 1.10 mycroft void wtclock __P((struct wt_softc *sc));
174 1.43 drochner int wtreset __P((bus_space_tag_t, bus_space_handle_t, struct wtregs *));
175 1.10 mycroft int wtsense __P((struct wt_softc *sc, int verbose, int ignore));
176 1.10 mycroft int wtstatus __P((struct wt_softc *sc));
177 1.10 mycroft void wtrewind __P((struct wt_softc *sc));
178 1.10 mycroft int wtreadfm __P((struct wt_softc *sc));
179 1.10 mycroft int wtwritefm __P((struct wt_softc *sc));
180 1.34 mycroft u_char wtsoft __P((struct wt_softc *sc, int mask, int bits));
181 1.1 cgd
182 1.43 drochner int wtprobe __P((struct device *, struct cfdata *, void *));
183 1.20 mycroft void wtattach __P((struct device *, struct device *, void *));
184 1.24 cgd int wtintr __P((void *sc));
185 1.1 cgd
186 1.30 thorpej struct cfattach wt_ca = {
187 1.30 thorpej sizeof(struct wt_softc), wtprobe, wtattach
188 1.30 thorpej };
189 1.30 thorpej
190 1.42 thorpej extern struct cfdriver wt_cd;
191 1.1 cgd
192 1.10 mycroft /*
193 1.10 mycroft * Probe for the presence of the device.
194 1.10 mycroft */
195 1.10 mycroft int
196 1.20 mycroft wtprobe(parent, match, aux)
197 1.20 mycroft struct device *parent;
198 1.43 drochner struct cfdata *match;
199 1.43 drochner void *aux;
200 1.10 mycroft {
201 1.10 mycroft struct isa_attach_args *ia = aux;
202 1.38 jonathan bus_space_tag_t iot = ia->ia_iot;
203 1.38 jonathan bus_space_handle_t ioh;
204 1.54 thorpej int rv = 0, iosize;
205 1.38 jonathan
206 1.54 thorpej if (ia->ia_nio < 1)
207 1.54 thorpej return (0);
208 1.54 thorpej if (ia->ia_nirq < 1)
209 1.54 thorpej return (0);
210 1.54 thorpej if (ia->ia_ndrq < 1);
211 1.40 thorpej
212 1.40 thorpej /* Disallow wildcarded i/o address. */
213 1.54 thorpej if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
214 1.54 thorpej return (0);
215 1.54 thorpej if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT)
216 1.40 thorpej return (0);
217 1.38 jonathan
218 1.54 thorpej if (ia->ia_drq[0].ir_drq < 1 || ia->ia_drq[0].ir_drq > 3) {
219 1.54 thorpej printf("wtprobe: Bad drq=%d, should be 1..3\n",
220 1.54 thorpej ia->ia_drq[0].ir_drq);
221 1.43 drochner return (0);
222 1.43 drochner }
223 1.43 drochner
224 1.54 thorpej iosize = AV_NPORT;
225 1.54 thorpej
226 1.38 jonathan /* Map i/o space */
227 1.54 thorpej if (bus_space_map(iot, ia->ia_io[0].ir_addr, iosize, 0, &ioh))
228 1.38 jonathan return 0;
229 1.10 mycroft
230 1.10 mycroft /* Try Wangtek. */
231 1.43 drochner if (wtreset(iot, ioh, &wtregs)) {
232 1.54 thorpej iosize = WT_NPORT; /* XXX misleading */
233 1.38 jonathan rv = 1;
234 1.38 jonathan goto done;
235 1.12 mycroft }
236 1.10 mycroft
237 1.10 mycroft /* Try Archive. */
238 1.43 drochner if (wtreset(iot, ioh, &avregs)) {
239 1.54 thorpej iosize = AV_NPORT;
240 1.38 jonathan rv = 1;
241 1.38 jonathan goto done;
242 1.12 mycroft }
243 1.10 mycroft
244 1.38 jonathan done:
245 1.54 thorpej if (rv) {
246 1.54 thorpej ia->ia_nio = 1;
247 1.54 thorpej ia->ia_io[0].ir_size = iosize;
248 1.54 thorpej
249 1.54 thorpej ia->ia_nirq = 1;
250 1.54 thorpej ia->ia_ndrq = 1;
251 1.54 thorpej
252 1.54 thorpej ia->ia_niomem = 0;
253 1.54 thorpej }
254 1.38 jonathan bus_space_unmap(iot, ioh, AV_NPORT);
255 1.38 jonathan return rv;
256 1.1 cgd }
257 1.1 cgd
258 1.1 cgd /*
259 1.10 mycroft * Device is found, configure it.
260 1.1 cgd */
261 1.10 mycroft void
262 1.10 mycroft wtattach(parent, self, aux)
263 1.10 mycroft struct device *parent, *self;
264 1.10 mycroft void *aux;
265 1.10 mycroft {
266 1.10 mycroft struct wt_softc *sc = (void *)self;
267 1.11 mycroft struct isa_attach_args *ia = aux;
268 1.38 jonathan bus_space_tag_t iot = ia->ia_iot;
269 1.38 jonathan bus_space_handle_t ioh;
270 1.47 thorpej bus_size_t maxsize;
271 1.38 jonathan
272 1.38 jonathan /* Map i/o space */
273 1.54 thorpej if (bus_space_map(iot, ia->ia_io[0].ir_addr, AV_NPORT, 0, &ioh)) {
274 1.41 thorpej printf(": can't map i/o space\n");
275 1.41 thorpej return;
276 1.41 thorpej }
277 1.38 jonathan
278 1.38 jonathan sc->sc_iot = iot;
279 1.38 jonathan sc->sc_ioh = ioh;
280 1.44 thorpej sc->sc_ic = ia->ia_ic;
281 1.10 mycroft
282 1.49 thorpej callout_init(&sc->sc_timer_ch);
283 1.49 thorpej
284 1.43 drochner /* Try Wangtek. */
285 1.43 drochner if (wtreset(iot, ioh, &wtregs)) {
286 1.43 drochner sc->type = WANGTEK;
287 1.52 thorpej memcpy(&sc->regs, &wtregs, sizeof(sc->regs));
288 1.43 drochner printf(": type <Wangtek>\n");
289 1.43 drochner goto ok;
290 1.43 drochner }
291 1.43 drochner
292 1.43 drochner /* Try Archive. */
293 1.43 drochner if (wtreset(iot, ioh, &avregs)) {
294 1.43 drochner sc->type = ARCHIVE;
295 1.52 thorpej memcpy(&sc->regs, &avregs, sizeof(sc->regs));
296 1.36 christos printf(": type <Archive>\n");
297 1.10 mycroft /* Reset DMA. */
298 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.RDMAPORT, 0);
299 1.43 drochner goto ok;
300 1.43 drochner }
301 1.43 drochner
302 1.43 drochner /* what happened? */
303 1.43 drochner printf("%s: lost controller\n", self->dv_xname);
304 1.43 drochner return;
305 1.43 drochner
306 1.43 drochner ok:
307 1.10 mycroft sc->flags = TPSTART; /* tape is rewound */
308 1.10 mycroft sc->dens = -1; /* unknown density */
309 1.11 mycroft
310 1.54 thorpej sc->chan = ia->ia_drq[0].ir_drq;
311 1.47 thorpej
312 1.47 thorpej if ((maxsize = isa_dmamaxsize(sc->sc_ic, sc->chan)) < MAXPHYS) {
313 1.48 thorpej printf("%s: max DMA size %lu is less than required %d\n",
314 1.48 thorpej sc->sc_dev.dv_xname, (u_long)maxsize, MAXPHYS);
315 1.47 thorpej return;
316 1.47 thorpej }
317 1.43 drochner
318 1.44 thorpej if (isa_dmamap_create(sc->sc_ic, sc->chan, MAXPHYS,
319 1.37 thorpej BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
320 1.37 thorpej printf("%s: can't set up ISA DMA map\n",
321 1.37 thorpej sc->sc_dev.dv_xname);
322 1.37 thorpej return;
323 1.37 thorpej }
324 1.37 thorpej
325 1.54 thorpej sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
326 1.54 thorpej IST_EDGE, IPL_BIO, wtintr, sc);
327 1.10 mycroft }
328 1.10 mycroft
329 1.10 mycroft int
330 1.25 cgd wtdump(dev, blkno, va, size)
331 1.10 mycroft dev_t dev;
332 1.25 cgd daddr_t blkno;
333 1.25 cgd caddr_t va;
334 1.25 cgd size_t size;
335 1.1 cgd {
336 1.1 cgd
337 1.10 mycroft /* Not implemented. */
338 1.25 cgd return ENXIO;
339 1.1 cgd }
340 1.1 cgd
341 1.10 mycroft int
342 1.10 mycroft wtsize(dev)
343 1.10 mycroft dev_t dev;
344 1.1 cgd {
345 1.1 cgd
346 1.10 mycroft /* Not implemented. */
347 1.10 mycroft return -1;
348 1.1 cgd }
349 1.1 cgd
350 1.1 cgd /*
351 1.10 mycroft * Open routine, called on every device open.
352 1.1 cgd */
353 1.10 mycroft int
354 1.32 christos wtopen(dev, flag, mode, p)
355 1.10 mycroft dev_t dev;
356 1.10 mycroft int flag;
357 1.32 christos int mode;
358 1.32 christos struct proc *p;
359 1.1 cgd {
360 1.10 mycroft int unit = minor(dev) & T_UNIT;
361 1.10 mycroft struct wt_softc *sc;
362 1.10 mycroft int error;
363 1.10 mycroft
364 1.51 thorpej sc = device_lookup(&wt_cd, unit);
365 1.51 thorpej if (sc == NULL)
366 1.51 thorpej return (ENXIO);
367 1.10 mycroft
368 1.10 mycroft /* Check that device is not in use */
369 1.10 mycroft if (sc->flags & TPINUSE)
370 1.10 mycroft return EBUSY;
371 1.10 mycroft
372 1.10 mycroft /* If the tape is in rewound state, check the status and set density. */
373 1.10 mycroft if (sc->flags & TPSTART) {
374 1.10 mycroft /* If rewind is going on, wait */
375 1.32 christos if ((error = wtwait(sc, PCATCH, "wtrew")) != 0)
376 1.10 mycroft return error;
377 1.10 mycroft
378 1.10 mycroft /* Check the controller status */
379 1.10 mycroft if (!wtsense(sc, 0, (flag & FWRITE) ? 0 : TP_WRP)) {
380 1.10 mycroft /* Bad status, reset the controller. */
381 1.43 drochner if (!wtreset(sc->sc_iot, sc->sc_ioh, &sc->regs))
382 1.10 mycroft return EIO;
383 1.10 mycroft if (!wtsense(sc, 1, (flag & FWRITE) ? 0 : TP_WRP))
384 1.10 mycroft return EIO;
385 1.10 mycroft }
386 1.10 mycroft
387 1.10 mycroft /* Set up tape density. */
388 1.10 mycroft if (sc->dens != (minor(dev) & WT_DENSEL)) {
389 1.10 mycroft int d = 0;
390 1.10 mycroft
391 1.10 mycroft switch (minor(dev) & WT_DENSEL) {
392 1.10 mycroft case WT_DENSDFLT:
393 1.10 mycroft default:
394 1.10 mycroft break; /* default density */
395 1.10 mycroft case WT_QIC11:
396 1.10 mycroft d = QIC_FMT11; break; /* minor 010 */
397 1.10 mycroft case WT_QIC24:
398 1.10 mycroft d = QIC_FMT24; break; /* minor 020 */
399 1.10 mycroft case WT_QIC120:
400 1.10 mycroft d = QIC_FMT120; break; /* minor 030 */
401 1.10 mycroft case WT_QIC150:
402 1.10 mycroft d = QIC_FMT150; break; /* minor 040 */
403 1.10 mycroft case WT_QIC300:
404 1.10 mycroft d = QIC_FMT300; break; /* minor 050 */
405 1.10 mycroft case WT_QIC600:
406 1.10 mycroft d = QIC_FMT600; break; /* minor 060 */
407 1.10 mycroft }
408 1.10 mycroft if (d) {
409 1.10 mycroft /* Change tape density. */
410 1.10 mycroft if (!wtcmd(sc, d))
411 1.10 mycroft return EIO;
412 1.10 mycroft if (!wtsense(sc, 1, TP_WRP | TP_ILL))
413 1.10 mycroft return EIO;
414 1.10 mycroft
415 1.10 mycroft /* Check the status of the controller. */
416 1.10 mycroft if (sc->error & TP_ILL) {
417 1.36 christos printf("%s: invalid tape density\n",
418 1.10 mycroft sc->sc_dev.dv_xname);
419 1.10 mycroft return ENODEV;
420 1.10 mycroft }
421 1.10 mycroft }
422 1.10 mycroft sc->dens = minor(dev) & WT_DENSEL;
423 1.1 cgd }
424 1.10 mycroft sc->flags &= ~TPSTART;
425 1.10 mycroft } else if (sc->dens != (minor(dev) & WT_DENSEL))
426 1.10 mycroft return ENXIO;
427 1.10 mycroft
428 1.10 mycroft sc->bsize = (minor(dev) & WT_BSIZE) ? 1024 : 512;
429 1.10 mycroft sc->buf = malloc(sc->bsize, M_TEMP, M_WAITOK);
430 1.1 cgd
431 1.10 mycroft sc->flags = TPINUSE;
432 1.1 cgd if (flag & FREAD)
433 1.10 mycroft sc->flags |= TPREAD;
434 1.1 cgd if (flag & FWRITE)
435 1.10 mycroft sc->flags |= TPWRITE;
436 1.10 mycroft return 0;
437 1.1 cgd }
438 1.1 cgd
439 1.1 cgd /*
440 1.10 mycroft * Close routine, called on last device close.
441 1.1 cgd */
442 1.10 mycroft int
443 1.32 christos wtclose(dev, flags, mode, p)
444 1.10 mycroft dev_t dev;
445 1.32 christos int flags;
446 1.32 christos int mode;
447 1.32 christos struct proc *p;
448 1.1 cgd {
449 1.51 thorpej struct wt_softc *sc = device_lookup(&wt_cd, minor(dev) & T_UNIT);
450 1.1 cgd
451 1.10 mycroft /* If rewind is pending, do nothing */
452 1.10 mycroft if (sc->flags & TPREW)
453 1.10 mycroft goto done;
454 1.1 cgd
455 1.10 mycroft /* If seek forward is pending and no rewind on close, do nothing */
456 1.10 mycroft if (sc->flags & TPRMARK) {
457 1.10 mycroft if (minor(dev) & T_NOREWIND)
458 1.10 mycroft goto done;
459 1.1 cgd
460 1.10 mycroft /* If read file mark is going on, wait */
461 1.10 mycroft wtwait(sc, 0, "wtrfm");
462 1.1 cgd }
463 1.10 mycroft
464 1.10 mycroft if (sc->flags & TPWANY) {
465 1.10 mycroft /* Tape was written. Write file mark. */
466 1.10 mycroft wtwritefm(sc);
467 1.1 cgd }
468 1.1 cgd
469 1.10 mycroft if ((minor(dev) & T_NOREWIND) == 0) {
470 1.10 mycroft /* Rewind to beginning of tape. */
471 1.10 mycroft /* Don't wait until rewind, though. */
472 1.10 mycroft wtrewind(sc);
473 1.10 mycroft goto done;
474 1.1 cgd }
475 1.10 mycroft if ((sc->flags & TPRANY) && (sc->flags & (TPVOL | TPWANY)) == 0) {
476 1.10 mycroft /* Space forward to after next file mark if no writing done. */
477 1.10 mycroft /* Don't wait for completion. */
478 1.10 mycroft wtreadfm(sc);
479 1.1 cgd }
480 1.1 cgd
481 1.10 mycroft done:
482 1.10 mycroft sc->flags &= TPREW | TPRMARK | TPSTART | TPTIMER;
483 1.10 mycroft free(sc->buf, M_TEMP);
484 1.10 mycroft return 0;
485 1.1 cgd }
486 1.1 cgd
487 1.10 mycroft /*
488 1.10 mycroft * Ioctl routine. Compatible with BSD ioctls.
489 1.10 mycroft * Direct QIC-02 commands ERASE and RETENSION added.
490 1.10 mycroft * There are three possible ioctls:
491 1.10 mycroft * ioctl(int fd, MTIOCGET, struct mtget *buf) -- get status
492 1.10 mycroft * ioctl(int fd, MTIOCTOP, struct mtop *buf) -- do BSD-like op
493 1.10 mycroft * ioctl(int fd, WTQICMD, int qicop) -- do QIC op
494 1.10 mycroft */
495 1.10 mycroft int
496 1.32 christos wtioctl(dev, cmd, addr, flag, p)
497 1.10 mycroft dev_t dev;
498 1.19 cgd u_long cmd;
499 1.32 christos caddr_t addr;
500 1.10 mycroft int flag;
501 1.32 christos struct proc *p;
502 1.10 mycroft {
503 1.51 thorpej struct wt_softc *sc = device_lookup(&wt_cd, minor(dev) & T_UNIT);
504 1.10 mycroft int error, count, op;
505 1.10 mycroft
506 1.10 mycroft switch (cmd) {
507 1.10 mycroft default:
508 1.10 mycroft return EINVAL;
509 1.10 mycroft case WTQICMD: /* direct QIC command */
510 1.10 mycroft op = *(int *)addr;
511 1.10 mycroft switch (op) {
512 1.10 mycroft default:
513 1.10 mycroft return EINVAL;
514 1.10 mycroft case QIC_ERASE: /* erase the whole tape */
515 1.10 mycroft if ((sc->flags & TPWRITE) == 0 || (sc->flags & TPWP))
516 1.10 mycroft return EACCES;
517 1.32 christos if ((error = wtwait(sc, PCATCH, "wterase")) != 0)
518 1.10 mycroft return error;
519 1.1 cgd break;
520 1.10 mycroft case QIC_RETENS: /* retension the tape */
521 1.32 christos if ((error = wtwait(sc, PCATCH, "wtretens")) != 0)
522 1.10 mycroft return error;
523 1.1 cgd break;
524 1.10 mycroft }
525 1.10 mycroft /* Both ERASE and RETENS operations work like REWIND. */
526 1.10 mycroft /* Simulate the rewind operation here. */
527 1.10 mycroft sc->flags &= ~(TPRO | TPWO | TPVOL);
528 1.10 mycroft if (!wtcmd(sc, op))
529 1.10 mycroft return EIO;
530 1.10 mycroft sc->flags |= TPSTART | TPREW;
531 1.10 mycroft if (op == QIC_ERASE)
532 1.10 mycroft sc->flags |= TPWANY;
533 1.10 mycroft wtclock(sc);
534 1.10 mycroft return 0;
535 1.10 mycroft case MTIOCIEOT: /* ignore EOT errors */
536 1.10 mycroft case MTIOCEEOT: /* enable EOT errors */
537 1.10 mycroft return 0;
538 1.10 mycroft case MTIOCGET:
539 1.10 mycroft ((struct mtget*)addr)->mt_type =
540 1.10 mycroft sc->type == ARCHIVE ? MT_ISVIPER1 : 0x11;
541 1.10 mycroft ((struct mtget*)addr)->mt_dsreg = sc->flags; /* status */
542 1.10 mycroft ((struct mtget*)addr)->mt_erreg = sc->error; /* errors */
543 1.10 mycroft ((struct mtget*)addr)->mt_resid = 0;
544 1.10 mycroft ((struct mtget*)addr)->mt_fileno = 0; /* file */
545 1.10 mycroft ((struct mtget*)addr)->mt_blkno = 0; /* block */
546 1.10 mycroft return 0;
547 1.10 mycroft case MTIOCTOP:
548 1.10 mycroft break;
549 1.1 cgd }
550 1.1 cgd
551 1.10 mycroft switch ((short)((struct mtop*)addr)->mt_op) {
552 1.10 mycroft default:
553 1.10 mycroft #if 0
554 1.10 mycroft case MTFSR: /* forward space record */
555 1.10 mycroft case MTBSR: /* backward space record */
556 1.10 mycroft case MTBSF: /* backward space file */
557 1.10 mycroft #endif
558 1.10 mycroft return EINVAL;
559 1.10 mycroft case MTNOP: /* no operation, sets status only */
560 1.10 mycroft case MTCACHE: /* enable controller cache */
561 1.10 mycroft case MTNOCACHE: /* disable controller cache */
562 1.10 mycroft return 0;
563 1.10 mycroft case MTREW: /* rewind */
564 1.10 mycroft case MTOFFL: /* rewind and put the drive offline */
565 1.10 mycroft if (sc->flags & TPREW) /* rewind is running */
566 1.10 mycroft return 0;
567 1.32 christos if ((error = wtwait(sc, PCATCH, "wtorew")) != 0)
568 1.10 mycroft return error;
569 1.10 mycroft wtrewind(sc);
570 1.10 mycroft return 0;
571 1.10 mycroft case MTFSF: /* forward space file */
572 1.10 mycroft for (count = ((struct mtop*)addr)->mt_count; count > 0;
573 1.10 mycroft --count) {
574 1.32 christos if ((error = wtwait(sc, PCATCH, "wtorfm")) != 0)
575 1.10 mycroft return error;
576 1.32 christos if ((error = wtreadfm(sc)) != 0)
577 1.10 mycroft return error;
578 1.10 mycroft }
579 1.10 mycroft return 0;
580 1.10 mycroft case MTWEOF: /* write an end-of-file record */
581 1.10 mycroft if ((sc->flags & TPWRITE) == 0 || (sc->flags & TPWP))
582 1.10 mycroft return EACCES;
583 1.32 christos if ((error = wtwait(sc, PCATCH, "wtowfm")) != 0)
584 1.10 mycroft return error;
585 1.32 christos if ((error = wtwritefm(sc)) != 0)
586 1.10 mycroft return error;
587 1.10 mycroft return 0;
588 1.1 cgd }
589 1.10 mycroft
590 1.10 mycroft #ifdef DIAGNOSTIC
591 1.10 mycroft panic("wtioctl: impossible");
592 1.1 cgd #endif
593 1.1 cgd }
594 1.1 cgd
595 1.10 mycroft /*
596 1.10 mycroft * Strategy routine.
597 1.10 mycroft */
598 1.10 mycroft void
599 1.10 mycroft wtstrategy(bp)
600 1.10 mycroft struct buf *bp;
601 1.1 cgd {
602 1.51 thorpej struct wt_softc *sc = device_lookup(&wt_cd, minor(bp->b_dev) & T_UNIT);
603 1.10 mycroft int s;
604 1.1 cgd
605 1.10 mycroft bp->b_resid = bp->b_bcount;
606 1.1 cgd
607 1.10 mycroft /* at file marks and end of tape, we just return '0 bytes available' */
608 1.10 mycroft if (sc->flags & TPVOL)
609 1.10 mycroft goto xit;
610 1.1 cgd
611 1.10 mycroft if (bp->b_flags & B_READ) {
612 1.10 mycroft /* Check read access and no previous write to this tape. */
613 1.10 mycroft if ((sc->flags & TPREAD) == 0 || (sc->flags & TPWANY))
614 1.10 mycroft goto errxit;
615 1.1 cgd
616 1.10 mycroft /* For now, we assume that all data will be copied out */
617 1.10 mycroft /* If read command outstanding, just skip down */
618 1.10 mycroft if ((sc->flags & TPRO) == 0) {
619 1.10 mycroft if (!wtsense(sc, 1, TP_WRP)) {
620 1.10 mycroft /* Clear status. */
621 1.10 mycroft goto errxit;
622 1.10 mycroft }
623 1.10 mycroft if (!wtcmd(sc, QIC_RDDATA)) {
624 1.10 mycroft /* Set read mode. */
625 1.10 mycroft wtsense(sc, 1, TP_WRP);
626 1.10 mycroft goto errxit;
627 1.10 mycroft }
628 1.10 mycroft sc->flags |= TPRO | TPRANY;
629 1.10 mycroft }
630 1.10 mycroft } else {
631 1.10 mycroft /* Check write access and write protection. */
632 1.10 mycroft /* No previous read from this tape allowed. */
633 1.10 mycroft if ((sc->flags & TPWRITE) == 0 || (sc->flags & (TPWP | TPRANY)))
634 1.10 mycroft goto errxit;
635 1.1 cgd
636 1.10 mycroft /* If write command outstanding, just skip down */
637 1.10 mycroft if ((sc->flags & TPWO) == 0) {
638 1.10 mycroft if (!wtsense(sc, 1, 0)) {
639 1.10 mycroft /* Clear status. */
640 1.10 mycroft goto errxit;
641 1.10 mycroft }
642 1.10 mycroft if (!wtcmd(sc, QIC_WRTDATA)) {
643 1.10 mycroft /* Set write mode. */
644 1.10 mycroft wtsense(sc, 1, 0);
645 1.10 mycroft goto errxit;
646 1.10 mycroft }
647 1.10 mycroft sc->flags |= TPWO | TPWANY;
648 1.10 mycroft }
649 1.10 mycroft }
650 1.1 cgd
651 1.10 mycroft if (bp->b_bcount == 0)
652 1.10 mycroft goto xit;
653 1.1 cgd
654 1.10 mycroft sc->flags &= ~TPEXCEP;
655 1.10 mycroft s = splbio();
656 1.16 mycroft if (wtstart(sc, bp->b_flags, bp->b_data, bp->b_bcount)) {
657 1.10 mycroft wtwait(sc, 0, (bp->b_flags & B_READ) ? "wtread" : "wtwrite");
658 1.10 mycroft bp->b_resid -= sc->dmacount;
659 1.10 mycroft }
660 1.10 mycroft splx(s);
661 1.1 cgd
662 1.10 mycroft if (sc->flags & TPEXCEP) {
663 1.10 mycroft errxit:
664 1.10 mycroft bp->b_flags |= B_ERROR;
665 1.10 mycroft bp->b_error = EIO;
666 1.10 mycroft }
667 1.10 mycroft xit:
668 1.10 mycroft biodone(bp);
669 1.10 mycroft return;
670 1.26 mycroft }
671 1.26 mycroft
672 1.26 mycroft int
673 1.32 christos wtread(dev, uio, flags)
674 1.26 mycroft dev_t dev;
675 1.26 mycroft struct uio *uio;
676 1.32 christos int flags;
677 1.26 mycroft {
678 1.26 mycroft
679 1.26 mycroft return (physio(wtstrategy, NULL, dev, B_READ, minphys, uio));
680 1.26 mycroft }
681 1.26 mycroft
682 1.26 mycroft int
683 1.32 christos wtwrite(dev, uio, flags)
684 1.26 mycroft dev_t dev;
685 1.26 mycroft struct uio *uio;
686 1.32 christos int flags;
687 1.26 mycroft {
688 1.26 mycroft
689 1.26 mycroft return (physio(wtstrategy, NULL, dev, B_WRITE, minphys, uio));
690 1.1 cgd }
691 1.1 cgd
692 1.10 mycroft /*
693 1.10 mycroft * Interrupt routine.
694 1.10 mycroft */
695 1.10 mycroft int
696 1.24 cgd wtintr(arg)
697 1.24 cgd void *arg;
698 1.1 cgd {
699 1.24 cgd struct wt_softc *sc = arg;
700 1.17 mycroft u_char x;
701 1.10 mycroft
702 1.38 jonathan /* get status */
703 1.43 drochner x = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->regs.STATPORT);
704 1.28 thorpej WTDBPRINT(("wtintr() status=0x%x -- ", x));
705 1.43 drochner if ((x & (sc->regs.BUSY | sc->regs.NOEXCEP))
706 1.43 drochner == (sc->regs.BUSY | sc->regs.NOEXCEP)) {
707 1.28 thorpej WTDBPRINT(("busy\n"));
708 1.10 mycroft return 0; /* device is busy */
709 1.10 mycroft }
710 1.1 cgd
711 1.10 mycroft /*
712 1.10 mycroft * Check if rewind finished.
713 1.10 mycroft */
714 1.10 mycroft if (sc->flags & TPREW) {
715 1.43 drochner WTDBPRINT(((x & (sc->regs.BUSY | sc->regs.NOEXCEP))
716 1.43 drochner == (sc->regs.BUSY | sc->regs.NOEXCEP) ?
717 1.43 drochner "rewind busy?\n" : "rewind finished\n"));
718 1.10 mycroft sc->flags &= ~TPREW; /* rewind finished */
719 1.10 mycroft wtsense(sc, 1, TP_WRP);
720 1.10 mycroft wakeup((caddr_t)sc);
721 1.10 mycroft return 1;
722 1.10 mycroft }
723 1.1 cgd
724 1.10 mycroft /*
725 1.10 mycroft * Check if writing/reading of file mark finished.
726 1.10 mycroft */
727 1.10 mycroft if (sc->flags & (TPRMARK | TPWMARK)) {
728 1.43 drochner WTDBPRINT(((x & (sc->regs.BUSY | sc->regs.NOEXCEP))
729 1.43 drochner == (sc->regs.BUSY | sc->regs.NOEXCEP) ?
730 1.43 drochner "marker r/w busy?\n" : "marker r/w finished\n"));
731 1.43 drochner if ((x & sc->regs.NOEXCEP) == 0) /* operation failed */
732 1.10 mycroft wtsense(sc, 1, (sc->flags & TPRMARK) ? TP_WRP : 0);
733 1.10 mycroft sc->flags &= ~(TPRMARK | TPWMARK); /* operation finished */
734 1.10 mycroft wakeup((caddr_t)sc);
735 1.10 mycroft return 1;
736 1.10 mycroft }
737 1.1 cgd
738 1.10 mycroft /*
739 1.10 mycroft * Do we started any i/o? If no, just return.
740 1.10 mycroft */
741 1.10 mycroft if ((sc->flags & TPACTIVE) == 0) {
742 1.28 thorpej WTDBPRINT(("unexpected interrupt\n"));
743 1.10 mycroft return 0;
744 1.10 mycroft }
745 1.10 mycroft sc->flags &= ~TPACTIVE;
746 1.10 mycroft sc->dmacount += sc->bsize; /* increment counter */
747 1.1 cgd
748 1.10 mycroft /*
749 1.10 mycroft * Clean up dma.
750 1.10 mycroft */
751 1.29 mycroft if ((sc->dmaflags & DMAMODE_READ) &&
752 1.10 mycroft (sc->dmatotal - sc->dmacount) < sc->bsize) {
753 1.10 mycroft /* If reading short block, copy the internal buffer
754 1.10 mycroft * to the user memory. */
755 1.44 thorpej isa_dmadone(sc->sc_ic, sc->chan);
756 1.52 thorpej memcpy(sc->dmavaddr, sc->buf, sc->dmatotal - sc->dmacount);
757 1.10 mycroft } else
758 1.44 thorpej isa_dmadone(sc->sc_ic, sc->chan);
759 1.1 cgd
760 1.10 mycroft /*
761 1.10 mycroft * On exception, check for end of file and end of volume.
762 1.10 mycroft */
763 1.43 drochner if ((x & sc->regs.NOEXCEP) == 0) {
764 1.28 thorpej WTDBPRINT(("i/o exception\n"));
765 1.29 mycroft wtsense(sc, 1, (sc->dmaflags & DMAMODE_READ) ? TP_WRP : 0);
766 1.10 mycroft if (sc->error & (TP_EOM | TP_FIL))
767 1.10 mycroft sc->flags |= TPVOL; /* end of file */
768 1.10 mycroft else
769 1.10 mycroft sc->flags |= TPEXCEP; /* i/o error */
770 1.10 mycroft wakeup((caddr_t)sc);
771 1.10 mycroft return 1;
772 1.10 mycroft }
773 1.10 mycroft
774 1.10 mycroft if (sc->dmacount < sc->dmatotal) {
775 1.10 mycroft /* Continue I/O. */
776 1.46 augustss sc->dmavaddr = (char *)sc->dmavaddr + sc->bsize;
777 1.10 mycroft wtdma(sc);
778 1.28 thorpej WTDBPRINT(("continue i/o, %d\n", sc->dmacount));
779 1.10 mycroft return 1;
780 1.10 mycroft }
781 1.10 mycroft if (sc->dmacount > sc->dmatotal) /* short last block */
782 1.10 mycroft sc->dmacount = sc->dmatotal;
783 1.10 mycroft /* Wake up user level. */
784 1.10 mycroft wakeup((caddr_t)sc);
785 1.28 thorpej WTDBPRINT(("i/o finished, %d\n", sc->dmacount));
786 1.10 mycroft return 1;
787 1.10 mycroft }
788 1.10 mycroft
789 1.10 mycroft /* start the rewind operation */
790 1.10 mycroft void
791 1.10 mycroft wtrewind(sc)
792 1.10 mycroft struct wt_softc *sc;
793 1.10 mycroft {
794 1.10 mycroft int rwmode = sc->flags & (TPRO | TPWO);
795 1.10 mycroft
796 1.10 mycroft sc->flags &= ~(TPRO | TPWO | TPVOL);
797 1.10 mycroft /*
798 1.10 mycroft * Wangtek strictly follows QIC-02 standard:
799 1.10 mycroft * clearing ONLINE in read/write modes causes rewind.
800 1.10 mycroft * REWIND command is not allowed in read/write mode
801 1.10 mycroft * and gives `illegal command' error.
802 1.10 mycroft */
803 1.10 mycroft if (sc->type == WANGTEK && rwmode) {
804 1.43 drochner bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->regs.CTLPORT, 0);
805 1.10 mycroft } else if (!wtcmd(sc, QIC_REWIND))
806 1.10 mycroft return;
807 1.10 mycroft sc->flags |= TPSTART | TPREW;
808 1.10 mycroft wtclock(sc);
809 1.1 cgd }
810 1.1 cgd
811 1.10 mycroft /*
812 1.10 mycroft * Start the `read marker' operation.
813 1.10 mycroft */
814 1.10 mycroft int
815 1.10 mycroft wtreadfm(sc)
816 1.10 mycroft struct wt_softc *sc;
817 1.1 cgd {
818 1.1 cgd
819 1.10 mycroft sc->flags &= ~(TPRO | TPWO | TPVOL);
820 1.10 mycroft if (!wtcmd(sc, QIC_READFM)) {
821 1.10 mycroft wtsense(sc, 1, TP_WRP);
822 1.10 mycroft return EIO;
823 1.10 mycroft }
824 1.10 mycroft sc->flags |= TPRMARK | TPRANY;
825 1.10 mycroft wtclock(sc);
826 1.10 mycroft /* Don't wait for completion here. */
827 1.10 mycroft return 0;
828 1.1 cgd }
829 1.1 cgd
830 1.10 mycroft /*
831 1.10 mycroft * Write marker to the tape.
832 1.10 mycroft */
833 1.10 mycroft int
834 1.10 mycroft wtwritefm(sc)
835 1.10 mycroft struct wt_softc *sc;
836 1.1 cgd {
837 1.1 cgd
838 1.10 mycroft tsleep((caddr_t)wtwritefm, WTPRI, "wtwfm", hz);
839 1.10 mycroft sc->flags &= ~(TPRO | TPWO);
840 1.10 mycroft if (!wtcmd(sc, QIC_WRITEFM)) {
841 1.10 mycroft wtsense(sc, 1, 0);
842 1.10 mycroft return EIO;
843 1.10 mycroft }
844 1.10 mycroft sc->flags |= TPWMARK | TPWANY;
845 1.10 mycroft wtclock(sc);
846 1.10 mycroft return wtwait(sc, 0, "wtwfm");
847 1.10 mycroft }
848 1.1 cgd
849 1.10 mycroft /*
850 1.10 mycroft * While controller status & mask == bits continue waiting.
851 1.10 mycroft */
852 1.10 mycroft u_char
853 1.34 mycroft wtsoft(sc, mask, bits)
854 1.10 mycroft struct wt_softc *sc;
855 1.10 mycroft int mask, bits;
856 1.10 mycroft {
857 1.38 jonathan bus_space_tag_t iot = sc->sc_iot;
858 1.38 jonathan bus_space_handle_t ioh = sc->sc_ioh;
859 1.17 mycroft u_char x;
860 1.10 mycroft int i;
861 1.10 mycroft
862 1.38 jonathan
863 1.10 mycroft /* Poll status port, waiting for specified bits. */
864 1.10 mycroft for (i = 0; i < 1000; ++i) { /* up to 1 msec */
865 1.43 drochner x = bus_space_read_1(iot, ioh, sc->regs.STATPORT);
866 1.17 mycroft if ((x & mask) != bits)
867 1.17 mycroft return x;
868 1.10 mycroft delay(1);
869 1.10 mycroft }
870 1.10 mycroft for (i = 0; i < 100; ++i) { /* up to 10 msec */
871 1.43 drochner x = bus_space_read_1(iot, ioh, sc->regs.STATPORT);
872 1.17 mycroft if ((x & mask) != bits)
873 1.17 mycroft return x;
874 1.10 mycroft delay(100);
875 1.10 mycroft }
876 1.10 mycroft for (;;) { /* forever */
877 1.43 drochner x = bus_space_read_1(iot, ioh, sc->regs.STATPORT);
878 1.17 mycroft if ((x & mask) != bits)
879 1.17 mycroft return x;
880 1.34 mycroft tsleep((caddr_t)wtsoft, WTPRI, "wtsoft", 1);
881 1.10 mycroft }
882 1.1 cgd }
883 1.1 cgd
884 1.10 mycroft /*
885 1.10 mycroft * Execute QIC command.
886 1.10 mycroft */
887 1.10 mycroft int
888 1.10 mycroft wtcmd(sc, cmd)
889 1.10 mycroft struct wt_softc *sc;
890 1.10 mycroft int cmd;
891 1.10 mycroft {
892 1.38 jonathan bus_space_tag_t iot = sc->sc_iot;
893 1.38 jonathan bus_space_handle_t ioh = sc->sc_ioh;
894 1.17 mycroft u_char x;
895 1.17 mycroft int s;
896 1.10 mycroft
897 1.28 thorpej WTDBPRINT(("wtcmd() cmd=0x%x\n", cmd));
898 1.17 mycroft s = splbio();
899 1.43 drochner x = wtsoft(sc, sc->regs.BUSY | sc->regs.NOEXCEP,
900 1.43 drochner sc->regs.BUSY | sc->regs.NOEXCEP); /* ready? */
901 1.43 drochner if ((x & sc->regs.NOEXCEP) == 0) { /* error */
902 1.17 mycroft splx(s);
903 1.10 mycroft return 0;
904 1.17 mycroft }
905 1.10 mycroft
906 1.38 jonathan /* output the command */
907 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.CMDPORT, cmd);
908 1.38 jonathan
909 1.38 jonathan /* set request */
910 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.CTLPORT,
911 1.43 drochner sc->regs.REQUEST | sc->regs.ONLINE);
912 1.38 jonathan
913 1.38 jonathan /* wait for ready */
914 1.43 drochner wtsoft(sc, sc->regs.BUSY, sc->regs.BUSY);
915 1.1 cgd
916 1.38 jonathan /* reset request */
917 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.CTLPORT,
918 1.43 drochner sc->regs.IEN | sc->regs.ONLINE);
919 1.38 jonathan
920 1.38 jonathan /* wait for not ready */
921 1.43 drochner wtsoft(sc, sc->regs.BUSY, 0);
922 1.17 mycroft splx(s);
923 1.10 mycroft return 1;
924 1.1 cgd }
925 1.1 cgd
926 1.10 mycroft /* wait for the end of i/o, seeking marker or rewind operation */
927 1.10 mycroft int
928 1.10 mycroft wtwait(sc, catch, msg)
929 1.10 mycroft struct wt_softc *sc;
930 1.10 mycroft int catch;
931 1.10 mycroft char *msg;
932 1.1 cgd {
933 1.10 mycroft int error;
934 1.1 cgd
935 1.28 thorpej WTDBPRINT(("wtwait() `%s'\n", msg));
936 1.10 mycroft while (sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
937 1.32 christos if ((error = tsleep((caddr_t)sc, WTPRI | catch, msg, 0)) != 0)
938 1.10 mycroft return error;
939 1.10 mycroft return 0;
940 1.1 cgd }
941 1.1 cgd
942 1.10 mycroft /* initialize dma for the i/o operation */
943 1.10 mycroft void
944 1.10 mycroft wtdma(sc)
945 1.10 mycroft struct wt_softc *sc;
946 1.1 cgd {
947 1.38 jonathan bus_space_tag_t iot = sc->sc_iot;
948 1.38 jonathan bus_space_handle_t ioh = sc->sc_ioh;
949 1.1 cgd
950 1.10 mycroft sc->flags |= TPACTIVE;
951 1.10 mycroft wtclock(sc);
952 1.1 cgd
953 1.10 mycroft if (sc->type == ARCHIVE) {
954 1.10 mycroft /* Set DMA. */
955 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.SDMAPORT, 0);
956 1.1 cgd }
957 1.1 cgd
958 1.29 mycroft if ((sc->dmaflags & DMAMODE_READ) &&
959 1.10 mycroft (sc->dmatotal - sc->dmacount) < sc->bsize) {
960 1.10 mycroft /* Reading short block; do it through the internal buffer. */
961 1.44 thorpej isa_dmastart(sc->sc_ic, sc->chan, sc->buf,
962 1.37 thorpej sc->bsize, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
963 1.10 mycroft } else
964 1.44 thorpej isa_dmastart(sc->sc_ic, sc->chan, sc->dmavaddr,
965 1.37 thorpej sc->bsize, NULL, sc->dmaflags, BUS_DMA_NOWAIT);
966 1.1 cgd }
967 1.1 cgd
968 1.10 mycroft /* start i/o operation */
969 1.10 mycroft int
970 1.10 mycroft wtstart(sc, flag, vaddr, len)
971 1.10 mycroft struct wt_softc *sc;
972 1.10 mycroft int flag;
973 1.10 mycroft void *vaddr;
974 1.10 mycroft size_t len;
975 1.10 mycroft {
976 1.17 mycroft u_char x;
977 1.10 mycroft
978 1.28 thorpej WTDBPRINT(("wtstart()\n"));
979 1.43 drochner x = wtsoft(sc, sc->regs.BUSY | sc->regs.NOEXCEP,
980 1.43 drochner sc->regs.BUSY | sc->regs.NOEXCEP); /* ready? */
981 1.43 drochner if ((x & sc->regs.NOEXCEP) == 0) {
982 1.10 mycroft sc->flags |= TPEXCEP; /* error */
983 1.10 mycroft return 0;
984 1.10 mycroft }
985 1.10 mycroft sc->flags &= ~TPEXCEP; /* clear exception flag */
986 1.10 mycroft sc->dmavaddr = vaddr;
987 1.10 mycroft sc->dmatotal = len;
988 1.10 mycroft sc->dmacount = 0;
989 1.29 mycroft sc->dmaflags = flag & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
990 1.10 mycroft wtdma(sc);
991 1.10 mycroft return 1;
992 1.1 cgd }
993 1.1 cgd
994 1.10 mycroft /*
995 1.10 mycroft * Start timer.
996 1.10 mycroft */
997 1.10 mycroft void
998 1.10 mycroft wtclock(sc)
999 1.10 mycroft struct wt_softc *sc;
1000 1.1 cgd {
1001 1.1 cgd
1002 1.10 mycroft if (sc->flags & TPTIMER)
1003 1.10 mycroft return;
1004 1.10 mycroft sc->flags |= TPTIMER;
1005 1.10 mycroft /*
1006 1.10 mycroft * Some controllers seem to lose dma interrupts too often. To make the
1007 1.10 mycroft * tape stream we need 1 tick timeout.
1008 1.10 mycroft */
1009 1.49 thorpej callout_reset(&sc->sc_timer_ch, (sc->flags & TPACTIVE) ? 1 : hz,
1010 1.49 thorpej wttimer, sc);
1011 1.1 cgd }
1012 1.1 cgd
1013 1.10 mycroft /*
1014 1.10 mycroft * Simulate an interrupt periodically while i/o is going.
1015 1.10 mycroft * This is necessary in case interrupts get eaten due to
1016 1.10 mycroft * multiple devices on a single IRQ line.
1017 1.10 mycroft */
1018 1.10 mycroft void
1019 1.14 cgd wttimer(arg)
1020 1.14 cgd void *arg;
1021 1.1 cgd {
1022 1.38 jonathan register struct wt_softc *sc = (struct wt_softc *)arg;
1023 1.10 mycroft int s;
1024 1.38 jonathan u_char status;
1025 1.10 mycroft
1026 1.10 mycroft sc->flags &= ~TPTIMER;
1027 1.10 mycroft if ((sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)) == 0)
1028 1.10 mycroft return;
1029 1.1 cgd
1030 1.10 mycroft /* If i/o going, simulate interrupt. */
1031 1.10 mycroft s = splbio();
1032 1.43 drochner status = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->regs.STATPORT);
1033 1.43 drochner if ((status & (sc->regs.BUSY | sc->regs.NOEXCEP))
1034 1.43 drochner != (sc->regs.BUSY | sc->regs.NOEXCEP)) {
1035 1.28 thorpej WTDBPRINT(("wttimer() -- "));
1036 1.11 mycroft wtintr(sc);
1037 1.10 mycroft }
1038 1.10 mycroft splx(s);
1039 1.10 mycroft
1040 1.10 mycroft /* Restart timer if i/o pending. */
1041 1.10 mycroft if (sc->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
1042 1.10 mycroft wtclock(sc);
1043 1.1 cgd }
1044 1.1 cgd
1045 1.10 mycroft /*
1046 1.10 mycroft * Perform QIC-02 and QIC-36 compatible reset sequence.
1047 1.10 mycroft */
1048 1.10 mycroft int
1049 1.43 drochner wtreset(iot, ioh, regs)
1050 1.43 drochner bus_space_tag_t iot;
1051 1.43 drochner bus_space_handle_t ioh;
1052 1.43 drochner struct wtregs *regs;
1053 1.1 cgd {
1054 1.17 mycroft u_char x;
1055 1.10 mycroft int i;
1056 1.1 cgd
1057 1.38 jonathan /* send reset */
1058 1.43 drochner bus_space_write_1(iot, ioh, regs->CTLPORT, regs->RESET | regs->ONLINE);
1059 1.10 mycroft delay(30);
1060 1.38 jonathan /* turn off reset */
1061 1.43 drochner bus_space_write_1(iot, ioh, regs->CTLPORT, regs->ONLINE);
1062 1.10 mycroft delay(30);
1063 1.10 mycroft
1064 1.10 mycroft /* Read the controller status. */
1065 1.43 drochner x = bus_space_read_1(iot, ioh, regs->STATPORT);
1066 1.17 mycroft if (x == 0xff) /* no port at this address? */
1067 1.10 mycroft return 0;
1068 1.10 mycroft
1069 1.10 mycroft /* Wait 3 sec for reset to complete. Needed for QIC-36 boards? */
1070 1.10 mycroft for (i = 0; i < 3000; ++i) {
1071 1.43 drochner if ((x & regs->BUSY) == 0 || (x & regs->NOEXCEP) == 0)
1072 1.10 mycroft break;
1073 1.10 mycroft delay(1000);
1074 1.43 drochner x = bus_space_read_1(iot, ioh, regs->STATPORT);
1075 1.1 cgd }
1076 1.43 drochner return (x & regs->RESETMASK) == regs->RESETVAL;
1077 1.1 cgd }
1078 1.1 cgd
1079 1.10 mycroft /*
1080 1.10 mycroft * Get controller status information. Return 0 if user i/o request should
1081 1.10 mycroft * receive an i/o error code.
1082 1.10 mycroft */
1083 1.10 mycroft int
1084 1.10 mycroft wtsense(sc, verbose, ignore)
1085 1.10 mycroft struct wt_softc *sc;
1086 1.10 mycroft int verbose, ignore;
1087 1.10 mycroft {
1088 1.10 mycroft char *msg = 0;
1089 1.10 mycroft int error;
1090 1.10 mycroft
1091 1.28 thorpej WTDBPRINT(("wtsense() ignore=0x%x\n", ignore));
1092 1.10 mycroft sc->flags &= ~(TPRO | TPWO);
1093 1.10 mycroft if (!wtstatus(sc))
1094 1.10 mycroft return 0;
1095 1.10 mycroft if ((sc->error & TP_ST0) == 0)
1096 1.10 mycroft sc->error &= ~TP_ST0MASK;
1097 1.10 mycroft if ((sc->error & TP_ST1) == 0)
1098 1.10 mycroft sc->error &= ~TP_ST1MASK;
1099 1.10 mycroft sc->error &= ~ignore; /* ignore certain errors */
1100 1.10 mycroft error = sc->error & (TP_FIL | TP_BNL | TP_UDA | TP_EOM | TP_WRP |
1101 1.10 mycroft TP_USL | TP_CNI | TP_MBD | TP_NDT | TP_ILL);
1102 1.10 mycroft if (!error)
1103 1.10 mycroft return 1;
1104 1.10 mycroft if (!verbose)
1105 1.10 mycroft return 0;
1106 1.10 mycroft
1107 1.10 mycroft /* lifted from tdriver.c from Wangtek */
1108 1.10 mycroft if (error & TP_USL)
1109 1.10 mycroft msg = "Drive not online";
1110 1.10 mycroft else if (error & TP_CNI)
1111 1.10 mycroft msg = "No cartridge";
1112 1.10 mycroft else if ((error & TP_WRP) && (sc->flags & TPWP) == 0) {
1113 1.10 mycroft msg = "Tape is write protected";
1114 1.10 mycroft sc->flags |= TPWP;
1115 1.10 mycroft } else if (error & TP_FIL)
1116 1.10 mycroft msg = 0 /*"Filemark detected"*/;
1117 1.10 mycroft else if (error & TP_EOM)
1118 1.10 mycroft msg = 0 /*"End of tape"*/;
1119 1.10 mycroft else if (error & TP_BNL)
1120 1.10 mycroft msg = "Block not located";
1121 1.10 mycroft else if (error & TP_UDA)
1122 1.10 mycroft msg = "Unrecoverable data error";
1123 1.10 mycroft else if (error & TP_NDT)
1124 1.10 mycroft msg = "No data detected";
1125 1.10 mycroft else if (error & TP_ILL)
1126 1.10 mycroft msg = "Illegal command";
1127 1.10 mycroft if (msg)
1128 1.36 christos printf("%s: %s\n", sc->sc_dev.dv_xname, msg);
1129 1.10 mycroft return 0;
1130 1.1 cgd }
1131 1.1 cgd
1132 1.10 mycroft /*
1133 1.10 mycroft * Get controller status information.
1134 1.10 mycroft */
1135 1.10 mycroft int
1136 1.10 mycroft wtstatus(sc)
1137 1.10 mycroft struct wt_softc *sc;
1138 1.1 cgd {
1139 1.38 jonathan bus_space_tag_t iot = sc->sc_iot;
1140 1.38 jonathan bus_space_handle_t ioh = sc->sc_ioh;
1141 1.10 mycroft char *p;
1142 1.17 mycroft int s;
1143 1.1 cgd
1144 1.17 mycroft s = splbio();
1145 1.43 drochner wtsoft(sc, sc->regs.BUSY | sc->regs.NOEXCEP,
1146 1.43 drochner sc->regs.BUSY | sc->regs.NOEXCEP); /* ready? */
1147 1.38 jonathan /* send `read status' command */
1148 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.CMDPORT, QIC_RDSTAT);
1149 1.38 jonathan
1150 1.38 jonathan /* set request */
1151 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.CTLPORT,
1152 1.43 drochner sc->regs.REQUEST | sc->regs.ONLINE);
1153 1.38 jonathan
1154 1.38 jonathan /* wait for ready */
1155 1.43 drochner wtsoft(sc, sc->regs.BUSY, sc->regs.BUSY);
1156 1.38 jonathan /* reset request */
1157 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.CTLPORT, sc->regs.ONLINE);
1158 1.1 cgd
1159 1.38 jonathan /* wait for not ready */
1160 1.43 drochner wtsoft(sc, sc->regs.BUSY, 0);
1161 1.1 cgd
1162 1.10 mycroft p = (char *)&sc->error;
1163 1.10 mycroft while (p < (char *)&sc->error + 6) {
1164 1.43 drochner u_char x = wtsoft(sc, sc->regs.BUSY | sc->regs.NOEXCEP,
1165 1.43 drochner sc->regs.BUSY | sc->regs.NOEXCEP);
1166 1.38 jonathan
1167 1.43 drochner if ((x & sc->regs.NOEXCEP) == 0) { /* error */
1168 1.17 mycroft splx(s);
1169 1.10 mycroft return 0;
1170 1.17 mycroft }
1171 1.1 cgd
1172 1.38 jonathan /* read status byte */
1173 1.43 drochner *p++ = bus_space_read_1(iot, ioh, sc->regs.DATAPORT);
1174 1.38 jonathan
1175 1.38 jonathan /* set request */
1176 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.CTLPORT,
1177 1.43 drochner sc->regs.REQUEST | sc->regs.ONLINE);
1178 1.38 jonathan
1179 1.38 jonathan /* wait for not ready */
1180 1.43 drochner wtsoft(sc, sc->regs.BUSY, 0);
1181 1.1 cgd
1182 1.38 jonathan /* unset request */
1183 1.43 drochner bus_space_write_1(iot, ioh, sc->regs.CTLPORT, sc->regs.ONLINE);
1184 1.10 mycroft }
1185 1.17 mycroft splx(s);
1186 1.10 mycroft return 1;
1187 1.1 cgd }
1188