xref: /netbsd-src/sys/dev/mscp/mscp_tape.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: mscp_tape.c,v 1.41 2014/03/16 05:20:28 dholland 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.41 2014/03/16 05:20:28 dholland 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 	device_t 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(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 *);
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_NEW(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 	.d_open = mtopen,
128 	.d_close = mtclose,
129 	.d_strategy = mtstrategy,
130 	.d_ioctl = mtioctl,
131 	.d_dump = mtdump,
132 	.d_psize = nosize,
133 	.d_flag = D_TAPE
134 };
135 
136 const struct cdevsw mt_cdevsw = {
137 	.d_open = mtopen,
138 	.d_close = mtclose,
139 	.d_read = mtread,
140 	.d_write = mtwrite,
141 	.d_ioctl = mtioctl,
142 	.d_stop = nostop,
143 	.d_tty = notty,
144 	.d_poll = nopoll,
145 	.d_mmap = nommap,
146 	.d_kqfilter = nokqfilter,
147 	.d_flag = D_TAPE
148 };
149 
150 /*
151  * More driver definitions, for generic MSCP code.
152  */
153 
154 int
155 mtmatch(device_t parent, cfdata_t cf, void *aux)
156 {
157 	struct	drive_attach_args *da = aux;
158 	struct	mscp *mp = da->da_mp;
159 
160 	if ((da->da_typ & MSCPBUS_TAPE) == 0)
161 		return 0;
162 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
163 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
164 		return 0;
165 	return 1;
166 }
167 
168 /*
169  * The attach routine only checks and prints drive type.
170  */
171 void
172 mtattach(device_t parent, device_t self, void *aux)
173 {
174 	struct	mt_softc *mt = device_private(self);
175 	struct	drive_attach_args *da = aux;
176 	struct	mscp *mp = da->da_mp;
177 	struct	mscp_softc *mi = device_private(parent);
178 
179 	mt->mt_dev = self;
180 	mt->mt_hwunit = mp->mscp_unit;
181 	mi->mi_dp[mp->mscp_unit] = self;
182 
183 	disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
184 }
185 
186 /*
187  * (Try to) put the drive online. This is done the first time the
188  * drive is opened, or if it has fallen offline.
189  */
190 int
191 mt_putonline(struct mt_softc *mt)
192 {
193 	struct	mscp *mp;
194 	struct	mscp_softc *mi =
195 	    device_private(device_parent(mt->mt_dev));
196 
197 	((volatile struct mt_softc *) mt)->mt_state = MT_OFFLINE;
198 	mp = mscp_getcp(mi, MSCP_WAIT);
199 	mp->mscp_opcode = M_OP_ONLINE;
200 	mp->mscp_unit = mt->mt_hwunit;
201 	mp->mscp_cmdref = (long)&mt->mt_state;
202 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
203 
204 	/* Poll away */
205 	bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
206 	if (tsleep(&mt->mt_state, PRIBIO, "mtonline", 240 * hz))
207 		return MSCP_FAILED;
208 
209 	if ((volatile int)mt->mt_state != MT_ONLINE)
210 		return MSCP_FAILED;
211 
212 	return MSCP_DONE;
213 }
214 /*
215  * Open a drive.
216  */
217 /*ARGSUSED*/
218 int
219 mtopen(dev_t dev, int flag, int fmt, struct lwp *l)
220 {
221 	struct mt_softc *mt;
222 	int unit;
223 
224 	/*
225 	 * Make sure this is a reasonable open request.
226 	 */
227 	unit = mtunit(dev);
228 	mt = device_lookup_private(&mt_cd, unit);
229 	if (!mt)
230 		return ENXIO;
231 
232 	if (mt->mt_inuse)
233 			return EBUSY;
234 	mt->mt_inuse = 1;
235 
236 	if (mt_putonline(mt) == MSCP_FAILED) {
237 		mt->mt_inuse = 0;
238 		return EIO;
239 	}
240 
241 	return 0;
242 }
243 
244 /* ARGSUSED */
245 int
246 mtclose(dev_t dev, int flags, int fmt, struct lwp *l)
247 {
248 	int unit = mtunit(dev);
249 	struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
250 
251 	/*
252 	 * If we just have finished a writing, write EOT marks.
253 	 */
254 	if ((flags & FWRITE) && mt->mt_waswrite) {
255 		mtcmd(mt, MTWEOF, 0, 0);
256 		mtcmd(mt, MTWEOF, 0, 0);
257 		mtcmd(mt, MTBSR, 1, 0);
258 	}
259 	if (mtnorewind(dev) == 0)
260 		mtcmd(mt, MTREW, 0, 1);
261 	if (mt->mt_serex)
262 		mtcmd(mt, -1, 0, 0);
263 
264 	mt->mt_inuse = 0; /* Release the tape */
265 	return 0;
266 }
267 
268 void
269 mtstrategy(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 ((mt = device_lookup_private(&mt_cd, 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_t dev, struct uio *uio, int flag)
291 {
292 
293 	return (physio(mtstrategy, NULL, dev, B_READ, minphys, uio));
294 }
295 
296 int
297 mtwrite(dev_t dev, struct uio *uio, int flag)
298 {
299 
300 	return (physio(mtstrategy, NULL, dev, B_WRITE, minphys, uio));
301 }
302 
303 void
304 mtiodone(device_t usc, struct buf *bp)
305 {
306 
307 	biodone(bp);
308 }
309 
310 /*
311  * Fill in drive addresses in a mscp packet waiting for transfer.
312  */
313 void
314 mtfillin(struct buf *bp, struct mscp *mp)
315 {
316 	int unit = mtunit(bp->b_dev);
317 	struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
318 
319 	mp->mscp_unit = mt->mt_hwunit;
320 	if (mt->mt_serex == 2) {
321 		mp->mscp_modifier = M_MD_CLSEX;
322 		mt->mt_serex = 0;
323 	} else
324 		mp->mscp_modifier = 0;
325 
326 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
327 }
328 
329 /*
330  * Handle an error datagram.
331  */
332 void
333 mtdgram(device_t usc, struct mscp *mp, struct mscp_softc *mi)
334 {
335 	if (mscp_decodeerror(usc == NULL?"unconf mt" : device_xname(usc), mp, mi))
336 		return;
337 }
338 
339 /*
340  * A drive came on line, make sure it really _is_ on line before
341  * trying to use it.
342  */
343 int
344 mtonline(device_t usc, struct mscp *mp)
345 {
346 	struct mt_softc *mt = (void *)usc;
347 
348 	wakeup((void *)&mt->mt_state);
349 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
350 		mt->mt_state = MT_ONLINE;
351 
352 	return (MSCP_DONE);
353 }
354 
355 /*
356  * We got some (configured) unit's status.  Return DONE.
357  */
358 int
359 mtgotstatus(device_t usc, struct mscp *mp)
360 {
361 	return (MSCP_DONE);
362 }
363 
364 static const char *mt_ioerrs[] = {
365 	"invalid command",	/* 1 M_ST_INVALCMD */
366 	"command aborted",	/* 2 M_ST_ABORTED */
367 	"unit offline",		/* 3 M_ST_OFFLINE */
368 	"unknown",		/* 4 M_ST_AVAILABLE */
369 	"unknown",		/* 5 M_ST_MFMTERR */
370 	"unit write protected", /* 6 M_ST_WRPROT */
371 	"compare error",	/* 7 M_ST_COMPERR */
372 	"data error",		/* 8 M_ST_DATAERR */
373 	"host buffer access error",	/* 9 M_ST_HOSTBUFERR */
374 	"controller error",	/* 10 M_ST_CTLRERR */
375 	"drive error",		/* 11 M_ST_DRIVEERR */
376 	"formatter error",	/* 12 M_ST_FORMATTERR */
377 	"BOT encountered",	/* 13 M_ST_BOT */
378 	"tape mark encountered",/* 14 M_ST_TAPEMARK */
379 	"unknown",		/* 15 */
380 	"record data truncated",/* 16 M_ST_RDTRUNC */
381 };
382 
383 /*
384  * An I/O error, may be because of a tapemark encountered.
385  * Check that before failing.
386  */
387 /*ARGSUSED*/
388 int
389 mtioerror(device_t usc, struct mscp *mp, struct buf *bp)
390 {
391 	struct mt_softc *mt = (void *)usc;
392 	int st = mp->mscp_status & M_ST_MASK;
393 
394 	if (mp->mscp_flags & M_EF_SEREX)
395 		mt->mt_serex = 1;
396 	if (st == M_ST_TAPEMARK)
397 		mt->mt_serex = 2;
398 	else {
399 		if (st && st < 17)
400 			printf("%s: error %d (%s)\n", device_xname(mt->mt_dev), st,
401 			    mt_ioerrs[st-1]);
402 		else
403 			printf("%s: error %d\n", device_xname(mt->mt_dev), st);
404 		bp->b_error = EROFS;
405 	}
406 
407 	return (MSCP_DONE);
408 }
409 
410 /*
411  * I/O controls.
412  */
413 int
414 mtioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
415 {
416 	int unit = mtunit(dev);
417 	struct mt_softc *mt = device_lookup_private(&mt_cd, unit);
418 	struct mtop *mtop;
419 	int error = 0;
420 
421 	switch (cmd) {
422 
423 	case MTIOCTOP:
424 		mtop = (void *)data;
425 		if (mtop->mt_op == MTWEOF) {
426 			while (mtop->mt_count-- > 0)
427 				if ((error = mtcmd(mt, mtop->mt_op, 0, 0)))
428 					break;
429 		} else
430 			error = mtcmd(mt, mtop->mt_op, mtop->mt_count, 0);
431 
432 	case MTIOCGET:
433 		((struct mtget *)data)->mt_type = MT_ISTMSCP;
434 		/* XXX we need to fill in more fields here */
435 		break;
436 
437 	default:
438 		error = ENXIO;
439 		break;
440 	}
441 	return (error);
442 }
443 
444 /*
445  * No crash dump support...
446  */
447 int
448 mtdump(dev_t dev, daddr_t blkno, void *va, size_t size)
449 {
450 	return -1;
451 }
452 
453 /*
454  * Send a command to the tape drive. Wait until the command is
455  * finished before returning.
456  * This routine must only be called when there are no data transfer
457  * active on this device. Can we be sure of this? Or does the ctlr
458  * queue up all command packets and take them in sequential order?
459  * It sure would be nice if my manual stated this... /ragge
460  */
461 int
462 mtcmd(struct mt_softc *mt, int cmd, int count, int complete)
463 {
464 	struct mscp *mp;
465 	struct mscp_softc *mi = device_private(device_parent(mt->mt_dev));
466 
467 	mp = mscp_getcp(mi, MSCP_WAIT);
468 
469 	mt->mt_ioctlerr = 0;
470 	mp->mscp_unit = mt->mt_hwunit;
471 	mp->mscp_cmdref = -1;
472 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
473 
474 	switch (cmd) {
475 	case MTWEOF:
476 		mp->mscp_opcode = M_OP_WRITM;
477 		break;
478 
479 	case MTBSF:
480 		mp->mscp_modifier = M_MD_REVERSE;
481 	case MTFSF:
482 		mp->mscp_opcode = M_OP_POS;
483 		mp->mscp_seq.seq_buffer = count;
484 		break;
485 
486 	case MTBSR:
487 		mp->mscp_modifier = M_MD_REVERSE;
488 	case MTFSR:
489 		mp->mscp_opcode = M_OP_POS;
490 		mp->mscp_modifier |= M_MD_OBJCOUNT;
491 		mp->mscp_seq.seq_bytecount = count;
492 		break;
493 
494 	case MTREW:
495 		mp->mscp_opcode = M_OP_POS;
496 		mp->mscp_modifier = M_MD_REWIND | M_MD_CLSEX;
497 		if (complete)
498 			mp->mscp_modifier |= M_MD_IMMEDIATE;
499 		mt->mt_serex = 0;
500 		break;
501 
502 	case MTOFFL:
503 		mp->mscp_opcode = M_OP_AVAILABLE;
504 		mp->mscp_modifier = M_MD_UNLOAD | M_MD_CLSEX;
505 		mt->mt_serex = 0;
506 		break;
507 
508 	case MTNOP:
509 		mp->mscp_opcode = M_OP_GETUNITST;
510 		break;
511 
512 	case -1: /* Clear serious exception only */
513 		mp->mscp_opcode = M_OP_POS;
514 		mp->mscp_modifier = M_MD_CLSEX;
515 		mt->mt_serex = 0;
516 		break;
517 
518 	default:
519 		printf("Bad ioctl %x\n", cmd);
520 		mp->mscp_opcode = M_OP_POS;
521 		break;
522 	}
523 
524 	bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
525 	tsleep(&mt->mt_inuse, PRIBIO, "mtioctl", 0);
526 	return mt->mt_ioctlerr;
527 }
528 
529 /*
530  * Called from bus routines whenever a non-data transfer is finished.
531  */
532 void
533 mtcmddone(device_t usc, struct mscp *mp)
534 {
535 	struct mt_softc *mt = (void *)usc;
536 
537 	if (mp->mscp_status) {
538 		mt->mt_ioctlerr = EIO;
539 		printf("%s: bad status %x\n", device_xname(mt->mt_dev),
540 		    mp->mscp_status);
541 	}
542 	wakeup(&mt->mt_inuse);
543 }
544