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