mscp_tape.c revision 1.32 1 /* $NetBSD: mscp_tape.c,v 1.32 2007/10/19 12:00:36 ad 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.32 2007/10/19 12:00:36 ad 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 <sys/bus.h>
60 #include <sys/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 = device_private(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 biodone(bp);
281 return;
282 }
283
284 mt->mt_waswrite = bp->b_flags & B_READ ? 0 : 1;
285 mscp_strategy(bp, device_parent(&mt->mt_dev));
286 return;
287 }
288
289 int
290 mtread(dev, uio, flag)
291 dev_t dev;
292 struct uio *uio;
293 int flag;
294 {
295
296 return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio));
297 }
298
299 int
300 mtwrite(dev, uio, flag)
301 dev_t dev;
302 struct uio *uio;
303 int flag;
304 {
305
306 return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio));
307 }
308
309 void
310 mtiodone(usc, bp)
311 struct device *usc;
312 struct buf *bp;
313 {
314
315 biodone(bp);
316 }
317
318 /*
319 * Fill in drive addresses in a mscp packet waiting for transfer.
320 */
321 void
322 mtfillin(bp, mp)
323 struct buf *bp;
324 struct mscp *mp;
325 {
326 int unit = mtunit(bp->b_dev);
327 struct mt_softc *mt = mt_cd.cd_devs[unit];
328
329 mp->mscp_unit = mt->mt_hwunit;
330 if (mt->mt_serex == 2) {
331 mp->mscp_modifier = M_MD_CLSEX;
332 mt->mt_serex = 0;
333 } else
334 mp->mscp_modifier = 0;
335
336 mp->mscp_seq.seq_bytecount = bp->b_bcount;
337 }
338
339 /*
340 * Handle an error datagram.
341 */
342 void
343 mtdgram(usc, mp, mi)
344 struct device *usc;
345 struct mscp *mp;
346 struct mscp_softc *mi;
347 {
348 if (mscp_decodeerror(usc == NULL?"unconf mt" : usc->dv_xname, mp, mi))
349 return;
350 }
351
352 /*
353 * A drive came on line, make sure it really _is_ on line before
354 * trying to use it.
355 */
356 int
357 mtonline(usc, mp)
358 struct device *usc;
359 struct mscp *mp;
360 {
361 struct mt_softc *mt = (void *)usc;
362
363 wakeup((void *)&mt->mt_state);
364 if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
365 mt->mt_state = MT_ONLINE;
366
367 return (MSCP_DONE);
368 }
369
370 /*
371 * We got some (configured) unit's status. Return DONE.
372 */
373 int
374 mtgotstatus(usc, mp)
375 struct device *usc;
376 struct mscp *mp;
377 {
378 return (MSCP_DONE);
379 }
380
381 static const char *mt_ioerrs[] = {
382 "invalid command", /* 1 M_ST_INVALCMD */
383 "command aborted", /* 2 M_ST_ABORTED */
384 "unit offline", /* 3 M_ST_OFFLINE */
385 "unknown", /* 4 M_ST_AVAILABLE */
386 "unknown", /* 5 M_ST_MFMTERR */
387 "unit write protected", /* 6 M_ST_WRPROT */
388 "compare error", /* 7 M_ST_COMPERR */
389 "data error", /* 8 M_ST_DATAERR */
390 "host buffer access error", /* 9 M_ST_HOSTBUFERR */
391 "controller error", /* 10 M_ST_CTLRERR */
392 "drive error", /* 11 M_ST_DRIVEERR */
393 "formatter error", /* 12 M_ST_FORMATTERR */
394 "BOT encountered", /* 13 M_ST_BOT */
395 "tape mark encountered",/* 14 M_ST_TAPEMARK */
396 "unknown", /* 15 */
397 "record data truncated",/* 16 M_ST_RDTRUNC */
398 };
399
400 /*
401 * An I/O error, may be because of a tapemark encountered.
402 * Check that before failing.
403 */
404 /*ARGSUSED*/
405 int
406 mtioerror(usc, mp, bp)
407 struct device *usc;
408 struct mscp *mp;
409 struct buf *bp;
410 {
411 struct mt_softc *mt = (void *)usc;
412 int st = mp->mscp_status & M_ST_MASK;
413
414 if (mp->mscp_flags & M_EF_SEREX)
415 mt->mt_serex = 1;
416 if (st == M_ST_TAPEMARK)
417 mt->mt_serex = 2;
418 else {
419 if (st && st < 17)
420 printf("%s: error %d (%s)\n", mt->mt_dev.dv_xname, st,
421 mt_ioerrs[st-1]);
422 else
423 printf("%s: error %d\n", mt->mt_dev.dv_xname, st);
424 bp->b_error = EROFS;
425 }
426
427 return (MSCP_DONE);
428 }
429
430 /*
431 * I/O controls.
432 */
433 int
434 mtioctl(dev, cmd, data, flag, l)
435 dev_t dev;
436 u_long cmd;
437 void *data;
438 int flag;
439 struct lwp *l;
440 {
441 int unit = mtunit(dev);
442 struct mt_softc *mt = mt_cd.cd_devs[unit];
443 struct mtop *mtop;
444 int error = 0;
445
446 switch (cmd) {
447
448 case MTIOCTOP:
449 mtop = (void *)data;
450 if (mtop->mt_op == MTWEOF) {
451 while (mtop->mt_count-- > 0)
452 if ((error = mtcmd(mt, mtop->mt_op, 0, 0)))
453 break;
454 } else
455 error = mtcmd(mt, mtop->mt_op, mtop->mt_count, 0);
456
457 case MTIOCGET:
458 ((struct mtget *)data)->mt_type = MT_ISTMSCP;
459 /* XXX we need to fill in more fields here */
460 break;
461
462 default:
463 error = ENXIO;
464 break;
465 }
466 return (error);
467 }
468
469 /*
470 * No crash dump support...
471 */
472 int
473 mtdump(dev, blkno, va, size)
474 dev_t dev;
475 daddr_t blkno;
476 void *va;
477 size_t size;
478 {
479 return -1;
480 }
481
482 /*
483 * Send a command to the tape drive. Wait until the command is
484 * finished before returning.
485 * This routine must only be called when there are no data transfer
486 * active on this device. Can we be sure of this? Or does the ctlr
487 * queue up all command packets and take them in sequential order?
488 * It sure would be nice if my manual stated this... /ragge
489 */
490 int
491 mtcmd(mt, cmd, count, complete)
492 struct mt_softc *mt;
493 int cmd, count, complete;
494 {
495 struct mscp *mp;
496 struct mscp_softc *mi = (void *)device_parent(&mt->mt_dev);
497 volatile int i;
498
499 mp = mscp_getcp(mi, MSCP_WAIT);
500
501 mt->mt_ioctlerr = 0;
502 mp->mscp_unit = mt->mt_hwunit;
503 mp->mscp_cmdref = -1;
504 *mp->mscp_addr |= MSCP_OWN | MSCP_INT;
505
506 switch (cmd) {
507 case MTWEOF:
508 mp->mscp_opcode = M_OP_WRITM;
509 break;
510
511 case MTBSF:
512 mp->mscp_modifier = M_MD_REVERSE;
513 case MTFSF:
514 mp->mscp_opcode = M_OP_POS;
515 mp->mscp_seq.seq_buffer = count;
516 break;
517
518 case MTBSR:
519 mp->mscp_modifier = M_MD_REVERSE;
520 case MTFSR:
521 mp->mscp_opcode = M_OP_POS;
522 mp->mscp_modifier |= M_MD_OBJCOUNT;
523 mp->mscp_seq.seq_bytecount = count;
524 break;
525
526 case MTREW:
527 mp->mscp_opcode = M_OP_POS;
528 mp->mscp_modifier = M_MD_REWIND | M_MD_CLSEX;
529 if (complete)
530 mp->mscp_modifier |= M_MD_IMMEDIATE;
531 mt->mt_serex = 0;
532 break;
533
534 case MTOFFL:
535 mp->mscp_opcode = M_OP_AVAILABLE;
536 mp->mscp_modifier = M_MD_UNLOAD | M_MD_CLSEX;
537 mt->mt_serex = 0;
538 break;
539
540 case MTNOP:
541 mp->mscp_opcode = M_OP_GETUNITST;
542 break;
543
544 case -1: /* Clear serious exception only */
545 mp->mscp_opcode = M_OP_POS;
546 mp->mscp_modifier = M_MD_CLSEX;
547 mt->mt_serex = 0;
548 break;
549
550 default:
551 printf("Bad ioctl %x\n", cmd);
552 mp->mscp_opcode = M_OP_POS;
553 break;
554 }
555
556 i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
557 tsleep(&mt->mt_inuse, PRIBIO, "mtioctl", 0);
558 return mt->mt_ioctlerr;
559 }
560
561 /*
562 * Called from bus routines whenever a non-data transfer is finished.
563 */
564 void
565 mtcmddone(usc, mp)
566 struct device *usc;
567 struct mscp *mp;
568 {
569 struct mt_softc *mt = (void *)usc;
570
571 if (mp->mscp_status) {
572 mt->mt_ioctlerr = EIO;
573 printf("%s: bad status %x\n", mt->mt_dev.dv_xname,
574 mp->mscp_status);
575 }
576 wakeup(&mt->mt_inuse);
577 }
578