Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / mscp / mscp_tape.c
blobc09aa018022fc3b89c5d3fee875edf6f494c4b85
1 /* $NetBSD: mscp_tape.c,v 1.37 2009/05/12 13:16:17 cegger Exp $ */
2 /*
3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed at Ludd, University of
17 * Lule}, Sweden and its contributors.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 * MSCP tape device driver
39 * TODO
40 * Write status handling code.
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: mscp_tape.c,v 1.37 2009/05/12 13:16:17 cegger Exp $");
46 #include <sys/param.h>
47 #include <sys/device.h>
48 #include <sys/kernel.h>
49 #include <sys/buf.h>
50 #include <sys/bufq.h>
51 #include <sys/ioccom.h>
52 #include <sys/mtio.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
55 #include <sys/systm.h>
56 #include <sys/proc.h>
57 #include <sys/conf.h>
59 #include <sys/bus.h>
60 #include <sys/cpu.h>
62 #include <dev/mscp/mscp.h>
63 #include <dev/mscp/mscpreg.h>
64 #include <dev/mscp/mscpvar.h>
66 #include "locators.h"
69 * Drive status, per drive
71 struct mt_softc {
72 struct device mt_dev; /* Autoconf struct */
73 int mt_state; /* open/closed state */
74 int mt_hwunit; /* Hardware unit number */
75 int mt_inuse; /* Locks the tape drive for others */
76 int mt_waswrite; /* Last operation was a write op */
77 int mt_serex; /* Got serious exception */
78 int mt_ioctlerr; /* Error after last ioctl */
81 #define MT_OFFLINE 0
82 #define MT_ONLINE 1
84 int mtmatch(device_t, cfdata_t, void *);
85 void mtattach(device_t, device_t, void *);
86 void mtdgram(device_t, struct mscp *, struct mscp_softc *);
87 void mtiodone(device_t, struct buf *);
88 int mtonline(device_t, struct mscp *);
89 int mtgotstatus(device_t, struct mscp *);
90 int mtioerror(device_t, struct mscp *, struct buf *);
91 void mtfillin(struct buf *, struct mscp *);
92 int mtcmd(struct mt_softc *, int, int, int);
93 void mtcmddone(device_t, struct mscp *);
94 int mt_putonline(struct mt_softc *);
96 struct mscp_device mt_device = {
97 mtdgram,
98 mtiodone,
99 mtonline,
100 mtgotstatus,
102 mtioerror,
104 mtfillin,
105 mtcmddone,
108 /* This is not good, should allow more than 4 tapes/device type */
109 #define mtunit(dev) (minor(dev) & T_UNIT)
110 #define mtnorewind(dev) (dev & T_NOREWIND)
111 #define mthdensity(dev) (dev & T_1600BPI)
113 CFATTACH_DECL(mt, sizeof(struct mt_softc),
114 mtmatch, mtattach, NULL, NULL);
116 extern struct cfdriver mt_cd;
118 dev_type_open(mtopen);
119 dev_type_close(mtclose);
120 dev_type_read(mtread);
121 dev_type_write(mtwrite);
122 dev_type_ioctl(mtioctl);
123 dev_type_strategy(mtstrategy);
124 dev_type_dump(mtdump);
126 const struct bdevsw mt_bdevsw = {
127 mtopen, mtclose, mtstrategy, mtioctl, mtdump, nosize, D_TAPE
130 const struct cdevsw mt_cdevsw = {
131 mtopen, mtclose, mtread, mtwrite, mtioctl,
132 nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
136 * More driver definitions, for generic MSCP code.
140 mtmatch(device_t parent, cfdata_t cf, void *aux)
142 struct drive_attach_args *da = aux;
143 struct mscp *mp = da->da_mp;
145 if ((da->da_typ & MSCPBUS_TAPE) == 0)
146 return 0;
147 if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
148 cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
149 return 0;
150 return 1;
154 * The attach routine only checks and prints drive type.
156 void
157 mtattach(device_t parent, device_t self, void *aux)
159 struct mt_softc *mt = device_private(self);
160 struct drive_attach_args *da = aux;
161 struct mscp *mp = da->da_mp;
162 struct mscp_softc *mi = (void *)parent;
164 mt->mt_hwunit = mp->mscp_unit;
165 mi->mi_dp[mp->mscp_unit] = self;
167 disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
171 * (Try to) put the drive online. This is done the first time the
172 * drive is opened, or if it has fallen offline.
175 mt_putonline(struct mt_softc *mt)
177 struct mscp *mp;
178 struct mscp_softc *mi =
179 (struct mscp_softc *)device_parent(&mt->mt_dev);
180 volatile int i;
182 ((volatile struct mt_softc *) mt)->mt_state = MT_OFFLINE;
183 mp = mscp_getcp(mi, MSCP_WAIT);
184 mp->mscp_opcode = M_OP_ONLINE;
185 mp->mscp_unit = mt->mt_hwunit;
186 mp->mscp_cmdref = (long)&mt->mt_state;
187 *mp->mscp_addr |= MSCP_OWN | MSCP_INT;
189 /* Poll away */
190 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
191 if (tsleep(&mt->mt_state, PRIBIO, "mtonline", 240 * hz))
192 return MSCP_FAILED;
194 if ((volatile int)mt->mt_state != MT_ONLINE)
195 return MSCP_FAILED;
197 return MSCP_DONE;
200 * Open a drive.
202 /*ARGSUSED*/
204 mtopen(dev_t dev, int flag, int fmt, struct lwp *l)
206 struct mt_softc *mt;
207 int unit;
210 * Make sure this is a reasonable open request.
212 unit = mtunit(dev);
213 mt = device_lookup_private(&mt_cd, unit);
214 if (!mt)
215 return ENXIO;
217 if (mt->mt_inuse)
218 return EBUSY;
219 mt->mt_inuse = 1;
221 if (mt_putonline(mt) == MSCP_FAILED) {
222 mt->mt_inuse = 0;
223 return EIO;
226 return 0;
229 /* ARGSUSED */
231 mtclose(dev_t dev, int flags, int fmt, struct lwp *l)
233 int unit = mtunit(dev);
234 struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
237 * If we just have finished a writing, write EOT marks.
239 if ((flags & FWRITE) && mt->mt_waswrite) {
240 mtcmd(mt, MTWEOF, 0, 0);
241 mtcmd(mt, MTWEOF, 0, 0);
242 mtcmd(mt, MTBSR, 1, 0);
244 if (mtnorewind(dev) == 0)
245 mtcmd(mt, MTREW, 0, 1);
246 if (mt->mt_serex)
247 mtcmd(mt, -1, 0, 0);
249 mt->mt_inuse = 0; /* Release the tape */
250 return 0;
253 void
254 mtstrategy(struct buf *bp)
256 int unit;
257 struct mt_softc *mt;
260 * Make sure this is a reasonable drive to use.
262 unit = mtunit(bp->b_dev);
263 if ((mt = device_lookup_private(&mt_cd, unit)) == NULL) {
264 bp->b_error = ENXIO;
265 biodone(bp);
266 return;
269 mt->mt_waswrite = bp->b_flags & B_READ ? 0 : 1;
270 mscp_strategy(bp, device_parent(&mt->mt_dev));
271 return;
275 mtread(dev_t dev, struct uio *uio, int flag)
278 return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio));
282 mtwrite(dev_t dev, struct uio *uio, int flag)
285 return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio));
288 void
289 mtiodone(device_t usc, struct buf *bp)
292 biodone(bp);
296 * Fill in drive addresses in a mscp packet waiting for transfer.
298 void
299 mtfillin(struct buf *bp, struct mscp *mp)
301 int unit = mtunit(bp->b_dev);
302 struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
304 mp->mscp_unit = mt->mt_hwunit;
305 if (mt->mt_serex == 2) {
306 mp->mscp_modifier = M_MD_CLSEX;
307 mt->mt_serex = 0;
308 } else
309 mp->mscp_modifier = 0;
311 mp->mscp_seq.seq_bytecount = bp->b_bcount;
315 * Handle an error datagram.
317 void
318 mtdgram(device_t usc, struct mscp *mp, struct mscp_softc *mi)
320 if (mscp_decodeerror(usc == NULL?"unconf mt" : device_xname(usc), mp, mi))
321 return;
325 * A drive came on line, make sure it really _is_ on line before
326 * trying to use it.
329 mtonline(device_t usc, struct mscp *mp)
331 struct mt_softc *mt = (void *)usc;
333 wakeup((void *)&mt->mt_state);
334 if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
335 mt->mt_state = MT_ONLINE;
337 return (MSCP_DONE);
341 * We got some (configured) unit's status. Return DONE.
344 mtgotstatus(device_t usc, struct mscp *mp)
346 return (MSCP_DONE);
349 static const char *mt_ioerrs[] = {
350 "invalid command", /* 1 M_ST_INVALCMD */
351 "command aborted", /* 2 M_ST_ABORTED */
352 "unit offline", /* 3 M_ST_OFFLINE */
353 "unknown", /* 4 M_ST_AVAILABLE */
354 "unknown", /* 5 M_ST_MFMTERR */
355 "unit write protected", /* 6 M_ST_WRPROT */
356 "compare error", /* 7 M_ST_COMPERR */
357 "data error", /* 8 M_ST_DATAERR */
358 "host buffer access error", /* 9 M_ST_HOSTBUFERR */
359 "controller error", /* 10 M_ST_CTLRERR */
360 "drive error", /* 11 M_ST_DRIVEERR */
361 "formatter error", /* 12 M_ST_FORMATTERR */
362 "BOT encountered", /* 13 M_ST_BOT */
363 "tape mark encountered",/* 14 M_ST_TAPEMARK */
364 "unknown", /* 15 */
365 "record data truncated",/* 16 M_ST_RDTRUNC */
369 * An I/O error, may be because of a tapemark encountered.
370 * Check that before failing.
372 /*ARGSUSED*/
374 mtioerror(device_t usc, struct mscp *mp, struct buf *bp)
376 struct mt_softc *mt = (void *)usc;
377 int st = mp->mscp_status & M_ST_MASK;
379 if (mp->mscp_flags & M_EF_SEREX)
380 mt->mt_serex = 1;
381 if (st == M_ST_TAPEMARK)
382 mt->mt_serex = 2;
383 else {
384 if (st && st < 17)
385 printf("%s: error %d (%s)\n", device_xname(&mt->mt_dev), st,
386 mt_ioerrs[st-1]);
387 else
388 printf("%s: error %d\n", device_xname(&mt->mt_dev), st);
389 bp->b_error = EROFS;
392 return (MSCP_DONE);
396 * I/O controls.
399 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
401 int unit = mtunit(dev);
402 struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
403 struct mtop *mtop;
404 int error = 0;
406 switch (cmd) {
408 case MTIOCTOP:
409 mtop = (void *)data;
410 if (mtop->mt_op == MTWEOF) {
411 while (mtop->mt_count-- > 0)
412 if ((error = mtcmd(mt, mtop->mt_op, 0, 0)))
413 break;
414 } else
415 error = mtcmd(mt, mtop->mt_op, mtop->mt_count, 0);
417 case MTIOCGET:
418 ((struct mtget *)data)->mt_type = MT_ISTMSCP;
419 /* XXX we need to fill in more fields here */
420 break;
422 default:
423 error = ENXIO;
424 break;
426 return (error);
430 * No crash dump support...
433 mtdump(dev_t dev, daddr_t blkno, void *va, size_t size)
435 return -1;
439 * Send a command to the tape drive. Wait until the command is
440 * finished before returning.
441 * This routine must only be called when there are no data transfer
442 * active on this device. Can we be sure of this? Or does the ctlr
443 * queue up all command packets and take them in sequential order?
444 * It sure would be nice if my manual stated this... /ragge
447 mtcmd(struct mt_softc *mt, int cmd, int count, int complete)
449 struct mscp *mp;
450 struct mscp_softc *mi = (void *)device_parent(&mt->mt_dev);
451 volatile int i;
453 mp = mscp_getcp(mi, MSCP_WAIT);
455 mt->mt_ioctlerr = 0;
456 mp->mscp_unit = mt->mt_hwunit;
457 mp->mscp_cmdref = -1;
458 *mp->mscp_addr |= MSCP_OWN | MSCP_INT;
460 switch (cmd) {
461 case MTWEOF:
462 mp->mscp_opcode = M_OP_WRITM;
463 break;
465 case MTBSF:
466 mp->mscp_modifier = M_MD_REVERSE;
467 case MTFSF:
468 mp->mscp_opcode = M_OP_POS;
469 mp->mscp_seq.seq_buffer = count;
470 break;
472 case MTBSR:
473 mp->mscp_modifier = M_MD_REVERSE;
474 case MTFSR:
475 mp->mscp_opcode = M_OP_POS;
476 mp->mscp_modifier |= M_MD_OBJCOUNT;
477 mp->mscp_seq.seq_bytecount = count;
478 break;
480 case MTREW:
481 mp->mscp_opcode = M_OP_POS;
482 mp->mscp_modifier = M_MD_REWIND | M_MD_CLSEX;
483 if (complete)
484 mp->mscp_modifier |= M_MD_IMMEDIATE;
485 mt->mt_serex = 0;
486 break;
488 case MTOFFL:
489 mp->mscp_opcode = M_OP_AVAILABLE;
490 mp->mscp_modifier = M_MD_UNLOAD | M_MD_CLSEX;
491 mt->mt_serex = 0;
492 break;
494 case MTNOP:
495 mp->mscp_opcode = M_OP_GETUNITST;
496 break;
498 case -1: /* Clear serious exception only */
499 mp->mscp_opcode = M_OP_POS;
500 mp->mscp_modifier = M_MD_CLSEX;
501 mt->mt_serex = 0;
502 break;
504 default:
505 printf("Bad ioctl %x\n", cmd);
506 mp->mscp_opcode = M_OP_POS;
507 break;
510 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
511 tsleep(&mt->mt_inuse, PRIBIO, "mtioctl", 0);
512 return mt->mt_ioctlerr;
516 * Called from bus routines whenever a non-data transfer is finished.
518 void
519 mtcmddone(device_t usc, struct mscp *mp)
521 struct mt_softc *mt = (void *)usc;
523 if (mp->mscp_status) {
524 mt->mt_ioctlerr = EIO;
525 printf("%s: bad status %x\n", device_xname(&mt->mt_dev),
526 mp->mscp_status);
528 wakeup(&mt->mt_inuse);