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