mscp_tape.c revision 1.28 1 /* $NetBSD: mscp_tape.c,v 1.28 2006/03/25 23:20:18 thorpej Exp $ */
2 /*
3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
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
20 *
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.
31 */
32
33
34 /*
35 * MSCP tape device driver
36 */
37
38 /*
39 * TODO
40 * Write status handling code.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: mscp_tape.c,v 1.28 2006/03/25 23:20:18 thorpej Exp $");
45
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>
58
59 #include <machine/bus.h>
60 #include <machine/cpu.h>
61
62 #include <dev/mscp/mscp.h>
63 #include <dev/mscp/mscpreg.h>
64 #include <dev/mscp/mscpvar.h>
65
66 #include "locators.h"
67
68 /*
69 * Drive status, per drive
70 */
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 */
79 };
80
81 #define MT_OFFLINE 0
82 #define MT_ONLINE 1
83
84 int mtmatch(struct device *, struct cfdata *, void *);
85 void mtattach(struct device *, struct device *, void *);
86 void mtdgram(struct device *, struct mscp *, struct mscp_softc *);
87 void mtiodone(struct device *, struct buf *);
88 int mtonline(struct device *, struct mscp *);
89 int mtgotstatus(struct device *, struct mscp *);
90 int mtioerror(struct device *, struct mscp *, struct buf *);
91 void mtfillin(struct buf *, struct mscp *);
92 int mtcmd(struct mt_softc *, int, int, int);
93 void mtcmddone(struct device *, struct mscp *);
94 int mt_putonline(struct mt_softc *);
95
96 struct mscp_device mt_device = {
97 mtdgram,
98 mtiodone,
99 mtonline,
100 mtgotstatus,
101 0,
102 mtioerror,
103 0,
104 mtfillin,
105 mtcmddone,
106 };
107
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)
112
113 CFATTACH_DECL(mt, sizeof(struct mt_softc),
114 mtmatch, mtattach, NULL, NULL);
115
116 extern struct cfdriver mt_cd;
117
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);
125
126 const struct bdevsw mt_bdevsw = {
127 mtopen, mtclose, mtstrategy, mtioctl, mtdump, nosize, D_TAPE
128 };
129
130 const struct cdevsw mt_cdevsw = {
131 mtopen, mtclose, mtread, mtwrite, mtioctl,
132 nostop, notty, nopoll, nommap, nokqfilter, D_TAPE
133 };
134
135 /*
136 * More driver definitions, for generic MSCP code.
137 */
138
139 int
140 mtmatch(parent, cf, aux)
141 struct device *parent;
142 struct cfdata *cf;
143 void *aux;
144 {
145 struct drive_attach_args *da = aux;
146 struct mscp *mp = da->da_mp;
147
148 if ((da->da_typ & MSCPBUS_TAPE) == 0)
149 return 0;
150 if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
151 cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
152 return 0;
153 return 1;
154 }
155
156 /*
157 * The attach routine only checks and prints drive type.
158 */
159 void
160 mtattach(parent, self, aux)
161 struct device *parent, *self;
162 void *aux;
163 {
164 struct mt_softc *mt = (void *)self;
165 struct drive_attach_args *da = aux;
166 struct mscp *mp = da->da_mp;
167 struct mscp_softc *mi = (void *)parent;
168
169 mt->mt_hwunit = mp->mscp_unit;
170 mi->mi_dp[mp->mscp_unit] = self;
171
172 disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
173 }
174
175 /*
176 * (Try to) put the drive online. This is done the first time the
177 * drive is opened, or if it has fallen offline.
178 */
179 int
180 mt_putonline(mt)
181 struct mt_softc *mt;
182 {
183 struct mscp *mp;
184 struct mscp_softc *mi =
185 (struct mscp_softc *)device_parent(&mt->mt_dev);
186 volatile int i;
187
188 ((volatile struct mt_softc *) mt)->mt_state = MT_OFFLINE;
189 mp = mscp_getcp(mi, MSCP_WAIT);
190 mp->mscp_opcode = M_OP_ONLINE;
191 mp->mscp_unit = mt->mt_hwunit;
192 mp->mscp_cmdref = (long)&mt->mt_state;
193 *mp->mscp_addr |= MSCP_OWN | MSCP_INT;
194
195 /* Poll away */
196 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
197 if (tsleep(&mt->mt_state, PRIBIO, "mtonline", 240 * hz))
198 return MSCP_FAILED;
199
200 if ((volatile int)mt->mt_state != MT_ONLINE)
201 return MSCP_FAILED;
202
203 return MSCP_DONE;
204 }
205 /*
206 * Open a drive.
207 */
208 /*ARGSUSED*/
209 int
210 mtopen(dev, flag, fmt, l)
211 dev_t dev;
212 int flag, fmt;
213 struct lwp *l;
214 {
215 struct mt_softc *mt;
216 int unit;
217
218 /*
219 * Make sure this is a reasonable open request.
220 */
221 unit = mtunit(dev);
222 if (unit >= mt_cd.cd_ndevs)
223 return ENXIO;
224 mt = mt_cd.cd_devs[unit];
225 if (mt == 0)
226 return ENXIO;
227
228 if (mt->mt_inuse)
229 return EBUSY;
230 mt->mt_inuse = 1;
231
232 if (mt_putonline(mt) == MSCP_FAILED) {
233 mt->mt_inuse = 0;
234 return EIO;
235 }
236
237 return 0;
238 }
239
240 /* ARGSUSED */
241 int
242 mtclose(dev, flags, fmt, l)
243 dev_t dev;
244 int flags, fmt;
245 struct lwp *l;
246 {
247 int unit = mtunit(dev);
248 struct mt_softc *mt = mt_cd.cd_devs[unit];
249
250 /*
251 * If we just have finished a writing, write EOT marks.
252 */
253 if ((flags & FWRITE) && mt->mt_waswrite) {
254 mtcmd(mt, MTWEOF, 0, 0);
255 mtcmd(mt, MTWEOF, 0, 0);
256 mtcmd(mt, MTBSR, 1, 0);
257 }
258 if (mtnorewind(dev) == 0)
259 mtcmd(mt, MTREW, 0, 1);
260 if (mt->mt_serex)
261 mtcmd(mt, -1, 0, 0);
262
263 mt->mt_inuse = 0; /* Release the tape */
264 return 0;
265 }
266
267 void
268 mtstrategy(bp)
269 struct buf *bp;
270 {
271 int unit;
272 struct mt_softc *mt;
273
274 /*
275 * Make sure this is a reasonable drive to use.
276 */
277 unit = mtunit(bp->b_dev);
278 if (unit > mt_cd.cd_ndevs || (mt = mt_cd.cd_devs[unit]) == NULL) {
279 bp->b_error = ENXIO;
280 goto bad;
281 }
282
283 mt->mt_waswrite = bp->b_flags & B_READ ? 0 : 1;
284 mscp_strategy(bp, device_parent(&mt->mt_dev));
285 return;
286
287 bad:
288 bp->b_flags |= B_ERROR;
289 biodone(bp);
290 }
291
292 int
293 mtread(dev, uio, flag)
294 dev_t dev;
295 struct uio *uio;
296 int flag;
297 {
298
299 return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio));
300 }
301
302 int
303 mtwrite(dev, uio, flag)
304 dev_t dev;
305 struct uio *uio;
306 int flag;
307 {
308
309 return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio));
310 }
311
312 void
313 mtiodone(usc, bp)
314 struct device *usc;
315 struct buf *bp;
316 {
317
318 biodone(bp);
319 }
320
321 /*
322 * Fill in drive addresses in a mscp packet waiting for transfer.
323 */
324 void
325 mtfillin(bp, mp)
326 struct buf *bp;
327 struct mscp *mp;
328 {
329 int unit = mtunit(bp->b_dev);
330 struct mt_softc *mt = mt_cd.cd_devs[unit];
331
332 mp->mscp_unit = mt->mt_hwunit;
333 if (mt->mt_serex == 2) {
334 mp->mscp_modifier = M_MD_CLSEX;
335 mt->mt_serex = 0;
336 } else
337 mp->mscp_modifier = 0;
338
339 mp->mscp_seq.seq_bytecount = bp->b_bcount;
340 }
341
342 /*
343 * Handle an error datagram.
344 */
345 void
346 mtdgram(usc, mp, mi)
347 struct device *usc;
348 struct mscp *mp;
349 struct mscp_softc *mi;
350 {
351 if (mscp_decodeerror(usc == NULL?"unconf mt" : usc->dv_xname, mp, mi))
352 return;
353 }
354
355 /*
356 * A drive came on line, make sure it really _is_ on line before
357 * trying to use it.
358 */
359 int
360 mtonline(usc, mp)
361 struct device *usc;
362 struct mscp *mp;
363 {
364 struct mt_softc *mt = (void *)usc;
365
366 wakeup((caddr_t)&mt->mt_state);
367 if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
368 mt->mt_state = MT_ONLINE;
369
370 return (MSCP_DONE);
371 }
372
373 /*
374 * We got some (configured) unit's status. Return DONE.
375 */
376 int
377 mtgotstatus(usc, mp)
378 struct device *usc;
379 struct mscp *mp;
380 {
381 return (MSCP_DONE);
382 }
383
384 static const char *mt_ioerrs[] = {
385 "invalid command", /* 1 M_ST_INVALCMD */
386 "command aborted", /* 2 M_ST_ABORTED */
387 "unit offline", /* 3 M_ST_OFFLINE */
388 "unknown", /* 4 M_ST_AVAILABLE */
389 "unknown", /* 5 M_ST_MFMTERR */
390 "unit write protected", /* 6 M_ST_WRPROT */
391 "compare error", /* 7 M_ST_COMPERR */
392 "data error", /* 8 M_ST_DATAERR */
393 "host buffer access error", /* 9 M_ST_HOSTBUFERR */
394 "controller error", /* 10 M_ST_CTLRERR */
395 "drive error", /* 11 M_ST_DRIVEERR */
396 "formatter error", /* 12 M_ST_FORMATTERR */
397 "BOT encountered", /* 13 M_ST_BOT */
398 "tape mark encountered",/* 14 M_ST_TAPEMARK */
399 "unknown", /* 15 */
400 "record data truncated",/* 16 M_ST_RDTRUNC */
401 };
402
403 /*
404 * An I/O error, may be because of a tapemark encountered.
405 * Check that before failing.
406 */
407 /*ARGSUSED*/
408 int
409 mtioerror(usc, mp, bp)
410 struct device *usc;
411 struct mscp *mp;
412 struct buf *bp;
413 {
414 struct mt_softc *mt = (void *)usc;
415 int st = mp->mscp_status & M_ST_MASK;
416
417 if (mp->mscp_flags & M_EF_SEREX)
418 mt->mt_serex = 1;
419 if (st == M_ST_TAPEMARK)
420 mt->mt_serex = 2;
421 else {
422 if (st && st < 17)
423 printf("%s: error %d (%s)\n", mt->mt_dev.dv_xname, st,
424 mt_ioerrs[st-1]);
425 else
426 printf("%s: error %d\n", mt->mt_dev.dv_xname, st);
427 bp->b_flags |= B_ERROR;
428 bp->b_error = EROFS;
429 }
430
431 return (MSCP_DONE);
432 }
433
434 /*
435 * I/O controls.
436 */
437 int
438 mtioctl(dev, cmd, data, flag, l)
439 dev_t dev;
440 u_long cmd;
441 caddr_t data;
442 int flag;
443 struct lwp *l;
444 {
445 int unit = mtunit(dev);
446 struct mt_softc *mt = mt_cd.cd_devs[unit];
447 struct mtop *mtop;
448 int error = 0;
449
450 switch (cmd) {
451
452 case MTIOCTOP:
453 mtop = (void *)data;
454 if (mtop->mt_op == MTWEOF) {
455 while (mtop->mt_count-- > 0)
456 if ((error = mtcmd(mt, mtop->mt_op, 0, 0)))
457 break;
458 } else
459 error = mtcmd(mt, mtop->mt_op, mtop->mt_count, 0);
460
461 case MTIOCGET:
462 ((struct mtget *)data)->mt_type = MT_ISTMSCP;
463 /* XXX we need to fill in more fields here */
464 break;
465
466 default:
467 error = ENXIO;
468 break;
469 }
470 return (error);
471 }
472
473 /*
474 * No crash dump support...
475 */
476 int
477 mtdump(dev, blkno, va, size)
478 dev_t dev;
479 daddr_t blkno;
480 caddr_t va;
481 size_t size;
482 {
483 return -1;
484 }
485
486 /*
487 * Send a command to the tape drive. Wait until the command is
488 * finished before returning.
489 * This routine must only be called when there are no data transfer
490 * active on this device. Can we be sure of this? Or does the ctlr
491 * queue up all command packets and take them in sequential order?
492 * It sure would be nice if my manual stated this... /ragge
493 */
494 int
495 mtcmd(mt, cmd, count, complete)
496 struct mt_softc *mt;
497 int cmd, count, complete;
498 {
499 struct mscp *mp;
500 struct mscp_softc *mi = (void *)device_parent(&mt->mt_dev);
501 volatile int i;
502
503 mp = mscp_getcp(mi, MSCP_WAIT);
504
505 mt->mt_ioctlerr = 0;
506 mp->mscp_unit = mt->mt_hwunit;
507 mp->mscp_cmdref = -1;
508 *mp->mscp_addr |= MSCP_OWN | MSCP_INT;
509
510 switch (cmd) {
511 case MTWEOF:
512 mp->mscp_opcode = M_OP_WRITM;
513 break;
514
515 case MTBSF:
516 mp->mscp_modifier = M_MD_REVERSE;
517 case MTFSF:
518 mp->mscp_opcode = M_OP_POS;
519 mp->mscp_seq.seq_buffer = count;
520 break;
521
522 case MTBSR:
523 mp->mscp_modifier = M_MD_REVERSE;
524 case MTFSR:
525 mp->mscp_opcode = M_OP_POS;
526 mp->mscp_modifier |= M_MD_OBJCOUNT;
527 mp->mscp_seq.seq_bytecount = count;
528 break;
529
530 case MTREW:
531 mp->mscp_opcode = M_OP_POS;
532 mp->mscp_modifier = M_MD_REWIND | M_MD_CLSEX;
533 if (complete)
534 mp->mscp_modifier |= M_MD_IMMEDIATE;
535 mt->mt_serex = 0;
536 break;
537
538 case MTOFFL:
539 mp->mscp_opcode = M_OP_AVAILABLE;
540 mp->mscp_modifier = M_MD_UNLOAD | M_MD_CLSEX;
541 mt->mt_serex = 0;
542 break;
543
544 case MTNOP:
545 mp->mscp_opcode = M_OP_GETUNITST;
546 break;
547
548 case -1: /* Clear serious exception only */
549 mp->mscp_opcode = M_OP_POS;
550 mp->mscp_modifier = M_MD_CLSEX;
551 mt->mt_serex = 0;
552 break;
553
554 default:
555 printf("Bad ioctl %x\n", cmd);
556 mp->mscp_opcode = M_OP_POS;
557 break;
558 }
559
560 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
561 tsleep(&mt->mt_inuse, PRIBIO, "mtioctl", 0);
562 return mt->mt_ioctlerr;
563 }
564
565 /*
566 * Called from bus routines whenever a non-data transfer is finished.
567 */
568 void
569 mtcmddone(usc, mp)
570 struct device *usc;
571 struct mscp *mp;
572 {
573 struct mt_softc *mt = (void *)usc;
574
575 if (mp->mscp_status) {
576 mt->mt_ioctlerr = EIO;
577 printf("%s: bad status %x\n", mt->mt_dev.dv_xname,
578 mp->mscp_status);
579 }
580 wakeup(&mt->mt_inuse);
581 }
582