xref: /csrg-svn/sys/tahoe/vba/cy.c (revision 34285)
1 /*	cy.c	1.15	88/05/14	*/
2 
3 #include "yc.h"
4 #if NCY > 0
5 /*
6  * Cipher Tapemaster driver.
7  */
8 #define CYDEBUG
9 #ifdef	CYDEBUG
10 int	cydebug = 0;
11 #define	dlog(params)	if (cydebug) log params
12 #else
13 #define dlog(params)	/* */
14 #endif
15 
16 #include "param.h"
17 #include "systm.h"
18 #include "vm.h"
19 #include "buf.h"
20 #include "file.h"
21 #include "dir.h"
22 #include "user.h"
23 #include "proc.h"
24 #include "signal.h"
25 #include "uio.h"
26 #include "ioctl.h"
27 #include "mtio.h"
28 #include "errno.h"
29 #include "cmap.h"
30 #include "kernel.h"
31 #include "syslog.h"
32 #include "tty.h"
33 
34 #include "../tahoe/cpu.h"
35 #include "../tahoe/mtpr.h"
36 #include "../tahoe/pte.h"
37 
38 #include "../tahoevba/vbavar.h"
39 #define	CYERROR
40 #include "../tahoevba/cyreg.h"
41 
42 /*
43  * There is a ccybuf per tape controller.
44  * It is used as the token to pass to the internal routines
45  * to execute tape ioctls, and also acts as a lock on the slaves
46  * on the controller, since there is only one per controller.
47  * In particular, when the tape is rewinding on close we release
48  * the user process but any further attempts to use the tape drive
49  * before the rewind completes will hang waiting for ccybuf.
50  */
51 struct	buf ccybuf[NCY];
52 
53 /*
54  * Raw tape operations use rcybuf.  The driver notices when
55  * rcybuf is being used and allows the user program to contine
56  * after errors and read records not of the standard length.
57  */
58 struct	buf rcybuf[NCY];
59 
60 int	cyprobe(), cyslave(), cyattach();
61 struct	buf ycutab[NYC];
62 short	yctocy[NYC];
63 struct	vba_ctlr *cyminfo[NCY];
64 struct	vba_device *ycdinfo[NYC];
65 long	cystd[] = { 0 };
66 struct	vba_driver cydriver =
67    { cyprobe, cyslave, cyattach, 0, cystd, "yc", ycdinfo, "cy", cyminfo };
68 
69 /* bits in minor device */
70 #define	YCUNIT(dev)	(minor(dev)&03)
71 #define	CYUNIT(dev)	(yctocy[YCUNIT(dev)])
72 #define	T_NOREWIND	0x04
73 #define	T_1600BPI	0x00		/* pseudo */
74 #define	T_3200BPI	0x08		/* unused */
75 
76 #define	INF	1000000L		/* close to infinity */
77 
78 /*
79  * Software state and shared command areas per controller.
80  *
81  * The i/o intermediate buffer must be allocated in startup()
82  * so its address will fit in 20-bits (YECH!!!!!!!!!!!!!!).
83  */
84 struct cy_softc {
85 	int	cy_bs;		/* controller's buffer size */
86 	struct	cyscp *cy_scp;	/* system configuration block address */
87 	struct	cyccb cy_ccb;	/* channel control block */
88 	struct	cyscb cy_scb;	/* system configuration block */
89 	struct	cytpb cy_tpb;	/* tape parameter block */
90 	struct	cytpb cy_nop;	/* nop parameter block for cyintr */
91 	struct	vb_buf cy_rbuf;	/* vba resources */
92 } cy_softc[NCY];
93 
94 /*
95  * Software state per tape transport.
96  */
97 struct	yc_softc {
98 	char	yc_openf;	/* lock against multiple opens */
99 	char	yc_lastiow;	/* last operation was a write */
100 	short	yc_tact;	/* timeout is active */
101 	long	yc_timo;	/* time until timeout expires */
102 	u_short	yc_control;	/* copy of last tpcb.tpcontrol */
103 	u_short	yc_status;	/* copy of last tpcb.tpstatus */
104 	u_short	yc_resid;	/* copy of last bc */
105 	u_short	yc_dens;	/* prototype control word with density info */
106 	struct	tty *yc_ttyp;	/* user's tty for errors */
107 	daddr_t	yc_blkno;	/* block number, for block device tape */
108 	daddr_t	yc_nxrec;	/* position of end of tape, if known */
109 	int	yc_blksize;	/* current tape blocksize estimate */
110 	int	yc_blks;	/* number of I/O operations since open */
111 	int	yc_softerrs;	/* number of soft I/O errors since open */
112 } yc_softc[NYC];
113 
114 /*
115  * States for vm->um_tab.b_active, the per controller state flag.
116  * This is used to sequence control in the driver.
117  */
118 #define	SSEEK	1		/* seeking */
119 #define	SIO	2		/* doing seq i/o */
120 #define	SCOM	3		/* sending control command */
121 #define	SREW	4		/* sending a rewind */
122 #define	SERASE	5		/* erase inter-record gap */
123 #define	SERASED	6		/* erased inter-record gap */
124 
125 /* there's no way to figure these out dynamically? -- yech */
126 struct	cyscp *cyscp[] =
127     { (struct cyscp *)0xc0000c06, (struct cyscp *)0xc0000c16 };
128 #define	NCYSCP	(sizeof (cyscp) / sizeof (cyscp[0]))
129 
130 cyprobe(reg, vm)
131 	caddr_t reg;
132 	struct vba_ctlr *vm;
133 {
134 	register br, cvec;			/* must be r12, r11 */
135 	register struct cy_softc *cy;
136 	int ctlr = vm->um_ctlr;
137 
138 #ifdef lint
139 	br = 0; cvec = br; br = cvec;
140 	cyintr(0);
141 #endif
142 	if (badcyaddr(reg+1))
143 		return (0);
144 	if (ctlr > NCYSCP || cyscp[ctlr] == 0)		/* XXX */
145 		return (0);
146 	cy = &cy_softc[ctlr];
147 	cy->cy_scp = cyscp[ctlr];			/* XXX */
148 	/*
149 	 * Tapemaster controller must have interrupt handler
150 	 * disable interrupt, so we'll just kludge things
151 	 * (stupid multibus non-vectored interrupt crud).
152 	 */
153 	if (cyinit(ctlr, reg)) {
154 		uncache(&cy->cy_tpb.tpcount);
155 		cy->cy_bs = htoms(cy->cy_tpb.tpcount);
156 		/*
157 		 * Setup nop parameter block for clearing interrupts.
158 		 */
159 		cy->cy_nop.tpcmd = CY_NOP;
160 		cy->cy_nop.tpcontrol = 0;
161 		/*
162 		 * Allocate page tables.
163 		 */
164 		if (cybuf == 0) {
165 			printf("no cy buffer!!!\n");
166 			return (0);
167 		}
168 		cy->cy_rbuf.vb_rawbuf = cybuf + ctlr * CYMAXIO;
169 		if (vbainit(&cy->cy_rbuf, CYMAXIO, VB_20BIT) == 0) {
170 			printf("cy%d: vbainit failed\n", ctlr);
171 			return (0);
172 		}
173 
174 		br = 0x13, cvec = 0x80;			/* XXX */
175 		return (sizeof (struct cyccb));
176 	} else
177 		return (0);
178 }
179 
180 /*
181  * Check to see if a drive is attached to a controller.
182  * Since we can only tell that a drive is there if a tape is loaded and
183  * the drive is placed online, we always indicate the slave is present.
184  */
185 cyslave(vi, addr)
186 	struct vba_device *vi;
187 	caddr_t addr;
188 {
189 
190 #ifdef lint
191 	vi = vi; addr = addr;
192 #endif
193 	return (1);
194 }
195 
196 cyattach(vi)
197 	struct vba_device *vi;
198 {
199 	register struct cy_softc *cy;
200 	int ctlr = vi->ui_mi->um_ctlr;
201 
202 	yctocy[vi->ui_unit] = ctlr;
203 	cy = &cy_softc[ctlr];
204 	if (vi->ui_slave == 0 && cy->cy_bs)
205 		printf("; %dkb buffer", cy->cy_bs/1024);
206 }
207 
208 /*
209  * Initialize the controller after a controller reset or
210  * during autoconfigure.  All of the system control blocks
211  * are initialized and the controller is asked to configure
212  * itself for later use.
213  */
214 cyinit(ctlr, addr)
215 	int ctlr;
216 	register caddr_t addr;
217 {
218 	register struct cy_softc *cy = &cy_softc[ctlr];
219 	register int *pte;
220 
221 	/*
222 	 * Initialize the system configuration pointer.
223 	 */
224 	/* make kernel writable */
225 	pte = (int *)&Sysmap[btop((int)cy->cy_scp &~ KERNBASE)];
226 	*pte &= ~PG_PROT; *pte |= PG_KW;
227 	mtpr(TBIS, cy->cy_scp);
228 	/* load the correct values in the scp */
229 	cy->cy_scp->csp_buswidth = CSP_16BITS;
230 	cyldmba(cy->cy_scp->csp_scb, (caddr_t)&cy->cy_scb);
231 	/* put it back to read-only */
232 	*pte &= ~PG_PROT; *pte |= PG_KR;
233 	mtpr(TBIS, cy->cy_scp);
234 
235 	/*
236 	 * Init system configuration block.
237 	 */
238 	cy->cy_scb.csb_fixed = CSB_FIXED;
239 	/* set pointer to the channel control block */
240 	cyldmba(cy->cy_scb.csb_ccb, (caddr_t)&cy->cy_ccb);
241 
242 	/*
243 	 * Initialize the chanel control block.
244 	 */
245 	cy->cy_ccb.cbcw = CBCW_CLRINT;
246 	cy->cy_ccb.cbgate = GATE_OPEN;
247 	/* set pointer to the tape parameter block */
248 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
249 
250 	/*
251 	 * Issue a nop cmd and get the internal buffer size for buffered i/o.
252 	 */
253 	cy->cy_tpb.tpcmd = CY_NOP;
254 	cy->cy_tpb.tpcontrol = CYCW_16BITS;
255 	cy->cy_ccb.cbgate = GATE_CLOSED;
256 	CY_GO(addr);
257 	if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) {
258 		uncache(&cy->cy_tpb.tpstatus);
259 		printf("cy%d: timeout or err during init, status=%b\n", ctlr,
260 		    cy->cy_tpb.tpstatus, CYS_BITS);
261 		return (0);
262 	}
263 	cy->cy_tpb.tpcmd = CY_CONFIG;
264 	cy->cy_tpb.tpcontrol = CYCW_16BITS;
265 	cy->cy_ccb.cbgate = GATE_CLOSED;
266 	CY_GO(addr);
267 	if (cywait(&cy->cy_ccb) || (cy->cy_tpb.tpstatus&CYS_ERR)) {
268 		uncache(&cy->cy_tpb.tpstatus);
269 		printf("cy%d: configuration failure, status=%b\n", ctlr,
270 		    cy->cy_tpb.tpstatus, CYS_BITS);
271 		return (0);
272 	}
273 	return (1);
274 }
275 
276 int	cytimer();
277 /*
278  * Open the device.  Tapes are unique open
279  * devices, so we refuse if it is already open.
280  * We also check that a tape is available, and
281  * don't block waiting here; if you want to wait
282  * for a tape you should timeout in user code.
283  */
284 cyopen(dev, flag)
285 	dev_t dev;
286 	register int flag;
287 {
288 	register int ycunit;
289 	register struct vba_device *vi;
290 	register struct yc_softc *yc;
291 	int s;
292 
293 	ycunit = YCUNIT(dev);
294 	if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0)
295 		return (ENXIO);
296 	if ((yc = &yc_softc[ycunit])->yc_openf)
297 		return (EBUSY);
298 	yc->yc_openf = 1;
299 #define	PACKUNIT(vi) \
300     (((vi->ui_slave&1)<<11)|((vi->ui_slave&2)<<9)|((vi->ui_slave&4)>>2))
301 	/* no way to select density */
302 	yc->yc_dens = PACKUNIT(vi)|CYCW_IE|CYCW_16BITS;
303 	if (yc->yc_tact == 0) {
304 		yc->yc_timo = INF;
305 		yc->yc_tact = 1;
306 		timeout(cytimer, (caddr_t)dev, 5*hz);
307 	}
308 	cycommand(dev, CY_SENSE, 1);
309 	if ((yc->yc_status&CYS_OL) == 0) {	/* not on-line */
310 		uprintf("cy%d: not online\n", ycunit);
311 		yc->yc_openf = 0;
312 		return (EIO);
313 	}
314 	if ((flag&FWRITE) && (yc->yc_status&CYS_WP)) {
315 		uprintf("cy%d: no write ring\n", ycunit);
316 		yc->yc_openf = 0;
317 		return (EIO);
318 	}
319 	yc->yc_blkno = (daddr_t)0;
320 	yc->yc_nxrec = INF;
321 	yc->yc_lastiow = 0;
322 	yc->yc_blksize = CYMAXIO;		/* guess > 0 */
323 	yc->yc_blks = 0;
324 	yc->yc_softerrs = 0;
325 	yc->yc_ttyp = u.u_ttyp;
326 	return (0);
327 }
328 
329 /*
330  * Close tape device.
331  *
332  * If tape was open for writing or last operation was a write,
333  * then write two EOF's and backspace over the last one.
334  * Unless this is a non-rewinding special file, rewind the tape.
335  * Make the tape available to others.
336  */
337 cyclose(dev, flag)
338 	dev_t dev;
339 	int flag;
340 {
341 	struct yc_softc *yc = &yc_softc[YCUNIT(dev)];
342 
343 	if (flag == FWRITE || (flag&FWRITE) && yc->yc_lastiow) {
344 		cycommand(dev, CY_WEOF, 1);	/* can't use count with WEOF */
345 		cycommand(dev, CY_WEOF, 1);
346 		cycommand(dev, CY_SREV, 1);
347 	}
348 	if ((minor(dev)&T_NOREWIND) == 0)
349 		/*
350 		 * 0 count means don't hang waiting for rewind complete
351 		 * rather ccybuf stays busy until the operation completes
352 		 * preventing further opens from completing by preventing
353 		 * a CY_SENSE from completing.
354 		 */
355 		cycommand(dev, CY_REW, 0);
356 	if (yc->yc_blks > 10 && yc->yc_softerrs > yc->yc_blks / 10)
357 		log(LOG_INFO, "yc%d: %d soft errors in %d blocks\n",
358 		    YCUNIT(dev), yc->yc_softerrs, yc->yc_blks);
359 	dlog((LOG_INFO, "%d soft errors in %d blocks\n",
360 	    yc->yc_softerrs, yc->yc_blks));
361 	yc->yc_openf = 0;
362 	return (0);
363 }
364 
365 /*
366  * Execute a command on the tape drive a specified number of times.
367  */
368 cycommand(dev, com, count)
369 	dev_t dev;
370 	int com, count;
371 {
372 	register struct buf *bp;
373 	int s;
374 
375 	bp = &ccybuf[CYUNIT(dev)];
376 	s = spl3();
377 	dlog((LOG_INFO, "cycommand(%o, %x, %d), b_flags %x\n",
378 	    dev, com, count, bp->b_flags));
379 	while (bp->b_flags&B_BUSY) {
380 		/*
381 		 * This special check is because B_BUSY never
382 		 * gets cleared in the non-waiting rewind case.
383 		 */
384 		if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
385 			break;
386 		bp->b_flags |= B_WANTED;
387 		sleep((caddr_t)bp, PRIBIO);
388 	}
389 	bp->b_flags = B_BUSY|B_READ;
390 	splx(s);
391 	bp->b_dev = dev;
392 	bp->b_repcnt = count;
393 	bp->b_command = com;
394 	bp->b_blkno = 0;
395 	cystrategy(bp);
396 	/*
397 	 * In case of rewind from close; don't wait.
398 	 * This is the only case where count can be 0.
399 	 */
400 	if (count == 0)
401 		return;
402 	biowait(bp);
403 	if (bp->b_flags&B_WANTED)
404 		wakeup((caddr_t)bp);
405 	bp->b_flags &= B_ERROR;
406 }
407 
408 cystrategy(bp)
409 	register struct buf *bp;
410 {
411 	int ycunit = YCUNIT(bp->b_dev);
412 	register struct vba_ctlr *vm;
413 	register struct buf *dp;
414 	int s;
415 
416 	/*
417 	 * Put transfer at end of unit queue.
418 	 */
419 	dlog((LOG_INFO, "cystrategy(%o, %x)\n", bp->b_dev, bp->b_command));
420 	dp = &ycutab[ycunit];
421 	bp->av_forw = NULL;
422 	vm = ycdinfo[ycunit]->ui_mi;
423 	/* BEGIN GROT */
424 	if (bp == &rcybuf[CYUNIT(bp->b_dev)]) {
425 		if (bp->b_bcount >= CYMAXIO) {
426 			uprintf("cy%d: i/o size too large\n", vm->um_ctlr);
427 			bp->b_error = EINVAL;
428 			bp->b_resid = bp->b_bcount;
429 			bp->b_flags |= B_ERROR;
430 			biodone(bp);
431 			return;
432 		}
433 	}
434 	/* END GROT */
435 	s = spl3();
436 	if (dp->b_actf == NULL) {
437 		dp->b_actf = bp;
438 		/*
439 		 * Transport not already active...
440 		 * put at end of controller queue.
441 		 */
442 		 dp->b_forw = NULL;
443 		 if (vm->um_tab.b_actf == NULL)
444 			vm->um_tab.b_actf = dp;
445 		else
446 			vm->um_tab.b_actl->b_forw = dp;
447 	} else
448 		dp->b_actl->av_forw = bp;
449 	dp->b_actl = bp;
450 	/*
451 	 * If the controller is not busy, get it going.
452 	 */
453 	if (vm->um_tab.b_active == 0)
454 		cystart(vm);
455 	splx(s);
456 }
457 
458 /*
459  * Start activity on a cy controller.
460  */
461 cystart(vm)
462 	register struct vba_ctlr *vm;
463 {
464 	register struct buf *bp, *dp;
465 	register struct yc_softc *yc;
466 	register struct cy_softc *cy;
467 	int ycunit;
468 	daddr_t blkno;
469 
470 	dlog((LOG_INFO, "cystart()\n"));
471 	/*
472 	 * Look for an idle transport on the controller.
473 	 */
474 loop:
475 	if ((dp = vm->um_tab.b_actf) == NULL)
476 		return;
477 	if ((bp = dp->b_actf) == NULL) {
478 		vm->um_tab.b_actf = dp->b_forw;
479 		goto loop;
480 	}
481 	ycunit = YCUNIT(bp->b_dev);
482 	yc = &yc_softc[ycunit];
483 	cy = &cy_softc[CYUNIT(bp->b_dev)];
484 	/*
485 	 * Default is that last command was NOT a write command;
486 	 * if we do a write command we will notice this in cyintr().
487 	 */
488 	yc->yc_lastiow = 0;
489 	if (yc->yc_openf < 0 ||
490 	    (bp->b_command != CY_SENSE && (cy->cy_tpb.tpstatus&CYS_OL) == 0)) {
491 		/*
492 		 * Have had a hard error on a non-raw tape
493 		 * or the tape unit is now unavailable (e.g.
494 		 * taken off line).
495 		 */
496 		dlog((LOG_INFO, "openf %d command %x status %b\n",
497 		   yc->yc_openf, bp->b_command, cy->cy_tpb.tpstatus, CYS_BITS));
498 		bp->b_flags |= B_ERROR;
499 		goto next;
500 	}
501 	if (bp == &ccybuf[CYUNIT(bp->b_dev)]) {
502 		/*
503 		 * Execute control operation with the specified count.
504 		 *
505 		 * Set next state; give 5 minutes to complete
506 		 * rewind or file mark search, or 10 seconds per
507 		 * iteration (minimum 60 seconds and max 5 minutes)
508 		 * to complete other ops.
509 		 */
510 		if (bp->b_command == CY_REW) {
511 			vm->um_tab.b_active = SREW;
512 			yc->yc_timo = 5*60;
513 		} else if (bp->b_command == CY_FSF ||
514 		    bp->b_command == CY_BSF) {
515 			vm->um_tab.b_active = SCOM;
516 			yc->yc_timo = 5*60;
517 		} else {
518 			vm->um_tab.b_active = SCOM;
519 			yc->yc_timo = imin(imax(10*(int)bp->b_repcnt,60),5*60);
520 		}
521 		cy->cy_tpb.tprec = htoms(bp->b_repcnt);
522 		dlog((LOG_INFO, "bpcmd "));
523 		goto dobpcmd;
524 	}
525 	/*
526 	 * The following checks handle boundary cases for operation
527 	 * on no-raw tapes.  On raw tapes the initialization of
528 	 * yc->yc_nxrec by cyphys causes them to be skipped normally
529 	 * (except in the case of retries).
530 	 */
531 	if (bp->b_blkno > yc->yc_nxrec) {
532 		/*
533 		 * Can't read past known end-of-file.
534 		 */
535 		bp->b_flags |= B_ERROR;
536 		bp->b_error = ENXIO;
537 		goto next;
538 	}
539 	if (bp->b_blkno == yc->yc_nxrec && bp->b_flags&B_READ) {
540 		/*
541 		 * Reading at end of file returns 0 bytes.
542 		 */
543 		bp->b_resid = bp->b_bcount;
544 		clrbuf(bp);
545 		goto next;
546 	}
547 	if ((bp->b_flags&B_READ) == 0)
548 		/*
549 		 * Writing sets EOF.
550 		 */
551 		yc->yc_nxrec = bp->b_blkno + 1;
552 	if ((blkno = yc->yc_blkno) == bp->b_blkno) {
553 		caddr_t addr;
554 		int cmd;
555 
556 		/*
557 		 * Choose the appropriate i/o command based on the
558 		 * transfer size, the estimated block size,
559 		 * and the controller's internal buffer size.
560 		 * If the request length is longer than the tape
561 		 * block length, a buffered read will fail,
562 		 * thus, we request at most the size that we expect.
563 		 * We then check for larger records when the read completes.
564 		 * If we're retrying a read on a raw device because
565 		 * the original try was a buffer request which failed
566 		 * due to a record length error, then we force the use
567 		 * of the raw controller read (YECH!!!!).
568 		 */
569 		if (bp->b_flags&B_READ) {
570 			if (yc->yc_blksize <= cy->cy_bs &&
571 			    vm->um_tab.b_errcnt == 0)
572 				cmd = CY_BRCOM;
573 			else
574 				cmd = CY_RCOM;
575 		} else {
576 			/*
577 			 * On write error retries erase the
578 			 * inter-record gap before rewriting.
579 			 */
580 			if (vm->um_tab.b_errcnt &&
581 			    vm->um_tab.b_active != SERASED) {
582 				vm->um_tab.b_active = SERASE;
583 				bp->b_command = CY_ERASE;
584 				yc->yc_timo = 60;
585 				goto dobpcmd;
586 			}
587 			cmd = (bp->b_bcount > cy->cy_bs) ? CY_WCOM : CY_BWCOM;
588 		}
589 		vm->um_tab.b_active = SIO;
590 		addr = (caddr_t)vbasetup(bp, &cy->cy_rbuf, 1);
591 		cy->cy_tpb.tpcmd = cmd;
592 		cy->cy_tpb.tpcontrol = yc->yc_dens;
593 		if (cmd == CY_RCOM || cmd == CY_WCOM)
594 			cy->cy_tpb.tpcontrol |= CYCW_LOCK;
595 		cy->cy_tpb.tpstatus = 0;
596 		cy->cy_tpb.tpcount = 0;
597 		cyldmba(cy->cy_tpb.tpdata, (caddr_t)addr);
598 		cy->cy_tpb.tprec = 0;
599 		if (cmd == CY_BRCOM)
600 			cy->cy_tpb.tpsize = htoms(min(yc->yc_blksize,
601 			    bp->b_bcount));
602 		else
603 			cy->cy_tpb.tpsize = htoms(bp->b_bcount);
604 		cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
605 		do
606 			uncache(&cy->cy_ccb.cbgate);
607 		while (cy->cy_ccb.cbgate == GATE_CLOSED);
608 		cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
609 		cy->cy_ccb.cbcw = CBCW_IE;
610 		cy->cy_ccb.cbgate = GATE_CLOSED;
611 		dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x size %d\n",
612 		    vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol,
613 		    htoms(cy->cy_tpb.tpsize)));
614 		CY_GO(vm->um_addr);
615 		return;
616 	}
617 	/*
618 	 * Tape positioned incorrectly; set to seek forwards
619 	 * or backwards to the correct spot.  This happens
620 	 * for raw tapes only on error retries.
621 	 */
622 	vm->um_tab.b_active = SSEEK;
623 	if (blkno < bp->b_blkno) {
624 		bp->b_command = CY_SFORW;
625 		cy->cy_tpb.tprec = htoms(bp->b_blkno - blkno);
626 	} else {
627 		bp->b_command = CY_SREV;
628 		cy->cy_tpb.tprec = htoms(blkno - bp->b_blkno);
629 	}
630 	yc->yc_timo = imin(imax(10 * htoms(cy->cy_tpb.tprec), 60), 5*60);
631 dobpcmd:
632 	/*
633 	 * Do the command in bp.  Reverse direction commands
634 	 * are indicated by having CYCW_REV or'd into their
635 	 * value.  For these we must set the appropriate bit
636 	 * in the control field.
637 	 */
638 	if (bp->b_command&CYCW_REV) {
639 		cy->cy_tpb.tpcmd = bp->b_command &~ CYCW_REV;
640 		cy->cy_tpb.tpcontrol = yc->yc_dens | CYCW_REV;
641 dlog((LOG_INFO, "cmd %x control %x\n", cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol));
642 	} else {
643 		cy->cy_tpb.tpcmd = bp->b_command;
644 		cy->cy_tpb.tpcontrol = yc->yc_dens;
645 dlog((LOG_INFO, "cmd %x control %x\n", cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol));
646 	}
647 	cy->cy_tpb.tpstatus = 0;
648 	cy->cy_tpb.tpcount = 0;
649 	cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
650 	do
651 		uncache(&cy->cy_ccb.cbgate);
652 	while (cy->cy_ccb.cbgate == GATE_CLOSED);
653 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
654 	cy->cy_ccb.cbcw = CBCW_IE;
655 	cy->cy_ccb.cbgate = GATE_CLOSED;
656 	dlog((LOG_INFO, "CY_GO(%x) cmd %x control %x rec %d\n",
657 	    vm->um_addr, cy->cy_tpb.tpcmd, cy->cy_tpb.tpcontrol,
658 	    htoms(cy->cy_tpb.tprec)));
659 	CY_GO(vm->um_addr);
660 	return;
661 next:
662 	/*
663 	 * Done with this operation due to error or the
664 	 * fact that it doesn't do anything.
665 	 * Dequeue the transfer and continue
666 	 * processing this slave.
667 	 */
668 	vm->um_tab.b_errcnt = 0;
669 	dp->b_actf = bp->av_forw;
670 	biodone(bp);
671 	goto loop;
672 }
673 
674 /*
675  * Cy interrupt routine.
676  */
677 cyintr(cyunit)
678 	int cyunit;
679 {
680 	struct buf *dp;
681 	register struct buf *bp;
682 	register struct vba_ctlr *vm = cyminfo[cyunit];
683 	register struct cy_softc *cy;
684 	register struct yc_softc *yc;
685 	int err;
686 	register state;
687 
688 	dlog((LOG_INFO, "cyintr(%d)\n", cyunit));
689 	/*
690 	 * First, turn off the interrupt from the controller
691 	 * (device uses Multibus non-vectored interrupts...yech).
692 	 */
693 	cy = &cy_softc[vm->um_ctlr];
694 	cy->cy_ccb.cbcw = CBCW_CLRINT;
695 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_nop);
696 	cy->cy_ccb.cbgate = GATE_CLOSED;
697 	CY_GO(vm->um_addr);
698 	if ((dp = vm->um_tab.b_actf) == NULL) {
699 		dlog((LOG_ERR, "cy%d: stray interrupt", vm->um_ctlr));
700 		return;
701 	}
702 	bp = dp->b_actf;
703 	cy = &cy_softc[cyunit];
704 	cyuncachetpb(cy);
705 	yc = &yc_softc[YCUNIT(bp->b_dev)];
706 	/*
707 	 * If last command was a rewind and tape is
708 	 * still moving, wait for the operation to complete.
709 	 */
710 	if (vm->um_tab.b_active == SREW) {
711 		vm->um_tab.b_active = SCOM;
712 		if ((cy->cy_tpb.tpstatus&CYS_RDY) == 0) {
713 			yc->yc_timo = 5*60;	/* 5 minutes */
714 			return;
715 		}
716 	}
717 	/*
718 	 * An operation completed...record status.
719 	 */
720 	yc->yc_timo = INF;
721 	yc->yc_control = cy->cy_tpb.tpcontrol;
722 	yc->yc_status = cy->cy_tpb.tpstatus;
723 	yc->yc_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount);
724 	dlog((LOG_INFO, "cmd %x control %b status %b resid %d\n",
725 	    cy->cy_tpb.tpcmd, yc->yc_control, CYCW_BITS,
726 	    yc->yc_status, CYS_BITS, yc->yc_resid));
727 	if ((bp->b_flags&B_READ) == 0)
728 		yc->yc_lastiow = 1;
729 	state = vm->um_tab.b_active;
730 	vm->um_tab.b_active = 0;
731 	/*
732 	 * Check for errors.
733 	 */
734 	if (cy->cy_tpb.tpstatus&CYS_ERR) {
735 		err = cy->cy_tpb.tpstatus&CYS_ERR;
736 		dlog((LOG_INFO, "error %d\n", err));
737 		/*
738 		 * If we hit the end of tape file, update our position.
739 		 */
740 		if (err == CYER_FM) {
741 			yc->yc_status |= CYS_FM;
742 			state = SCOM;		/* force completion */
743 			cyseteof(bp);		/* set blkno and nxrec */
744 			goto opdone;
745 		}
746 		/*
747 		 * Fix up errors which occur due to backspacing over
748 		 * the beginning of the tape.
749 		 */
750 		if (err == CYER_BOT && cy->cy_tpb.tpcontrol&CYCW_REV) {
751 			yc->yc_status |= CYS_BOT;
752 			goto ignoreerr;
753 		}
754 		/*
755 		 * If we were reading raw tape and the only error was that the
756 		 * record was too long, then we don't consider this an error.
757 		 */
758 		if (bp == &rcybuf[cyunit] && (bp->b_flags&B_READ) &&
759 		    err == CYER_STROBE) {
760 			/*
761 			 * Retry reads with the command changed to
762 			 * a raw read if necessary.  Setting b_errcnt
763 			 * here causes cystart (above) to force a CY_RCOM.
764 			 */
765 			if (cy->cy_tpb.tpcmd == CY_BRCOM &&
766 			    vm->um_tab.b_errcnt++ == 0) {
767 				yc->yc_blkno++;
768 				goto opcont;
769 			} else
770 				goto ignoreerr;
771 		}
772 		/*
773 		 * If error is not hard, and this was an i/o operation
774 		 * retry up to 8 times.
775 		 */
776 		if (state == SIO && (CYMASK(err) &
777 		    ((bp->b_flags&B_READ) ? CYER_RSOFT : CYER_WSOFT))) {
778 			if (++vm->um_tab.b_errcnt < 7) {
779 				yc->yc_blkno++;
780 				goto opcont;
781 			}
782 		} else
783 			/*
784 			 * Hard or non-i/o errors on non-raw tape
785 			 * cause it to close.
786 			 */
787 			if (yc->yc_openf > 0 && bp != &rcybuf[cyunit])
788 				yc->yc_openf = -1;
789 		/*
790 		 * Couldn't recover from error.
791 		 */
792 		tprintf(yc->yc_ttyp,
793 		    "yc%d: hard error bn%d status=%b, %s\n", YCUNIT(bp->b_dev),
794 		    bp->b_blkno, yc->yc_status, CYS_BITS,
795 		    (err < NCYERROR) ? cyerror[err] : "");
796 		bp->b_flags |= B_ERROR;
797 		goto opdone;
798 	} else if (cy->cy_tpb.tpcmd == CY_BRCOM) {
799 		int reclen = htoms(cy->cy_tpb.tprec);
800 
801 		/*
802 		 * If we did a buffered read, check whether the read
803 		 * was long enough.  If we asked the controller for less
804 		 * than the user asked for because the previous record
805 		 * was shorter, update our notion of record size
806 		 * and retry.  If the record is longer than the buffer,
807 		 * bump the errcnt so the retry will use direct read.
808 		 */
809 		if (reclen > yc->yc_blksize && bp->b_bcount > yc->yc_blksize) {
810 			yc->yc_blksize = reclen;
811 			if (reclen > cy->cy_bs)
812 				vm->um_tab.b_errcnt++;
813 			yc->yc_blkno++;
814 			goto opcont;
815 		}
816 	}
817 	/*
818 	 * Advance tape control FSM.
819 	 */
820 ignoreerr:
821 	/*
822 	 * If we hit a tape mark update our position.
823 	 */
824 	if (yc->yc_status&CYS_FM && bp->b_flags&B_READ) {
825 		cyseteof(bp);
826 		goto opdone;
827 	}
828 	switch (state) {
829 
830 	case SIO:
831 		/*
832 		 * Read/write increments tape block number.
833 		 */
834 		yc->yc_blkno++;
835 		yc->yc_blks++;
836 		if (vm->um_tab.b_errcnt || yc->yc_status & CYS_CR)
837 			yc->yc_softerrs++;
838 		yc->yc_blksize = htoms(cy->cy_tpb.tpcount);
839 		dlog((LOG_ERR, "blocksize %d", yc->yc_blksize));
840 		goto opdone;
841 
842 	case SCOM:
843 		/*
844 		 * For forward/backward space record update current position.
845 		 */
846 		if (bp == &ccybuf[CYUNIT(bp->b_dev)])
847 			switch ((int)bp->b_command) {
848 
849 			case CY_SFORW:
850 				yc->yc_blkno -= bp->b_repcnt;
851 				break;
852 
853 			case CY_SREV:
854 				yc->yc_blkno += bp->b_repcnt;
855 				break;
856 			}
857 		goto opdone;
858 
859 	case SSEEK:
860 		yc->yc_blkno = bp->b_blkno;
861 		goto opcont;
862 
863 	case SERASE:
864 		/*
865 		 * Completed erase of the inter-record gap due to a
866 		 * write error; now retry the write operation.
867 		 */
868 		vm->um_tab.b_active = SERASED;
869 		goto opcont;
870 	}
871 
872 opdone:
873 	/*
874 	 * Reset error count and remove from device queue.
875 	 */
876 	vm->um_tab.b_errcnt = 0;
877 	dp->b_actf = bp->av_forw;
878 	/*
879 	 * Save resid and release resources.
880 	 */
881 	bp->b_resid = bp->b_bcount - htoms(cy->cy_tpb.tpcount);
882 	if (bp != &ccybuf[cyunit])
883 		vbadone(bp, &cy->cy_rbuf);
884 	biodone(bp);
885 	/*
886 	 * Circulate slave to end of controller
887 	 * queue to give other slaves a chance.
888 	 */
889 	vm->um_tab.b_actf = dp->b_forw;
890 	if (dp->b_actf) {
891 		dp->b_forw = NULL;
892 		if (vm->um_tab.b_actf == NULL)
893 			vm->um_tab.b_actf = dp;
894 		else
895 			vm->um_tab.b_actl->b_forw = dp;
896 	}
897 	if (vm->um_tab.b_actf == 0)
898 		return;
899 opcont:
900 	cystart(vm);
901 }
902 
903 cytimer(dev)
904 	int dev;
905 {
906 	register struct yc_softc *yc = &yc_softc[YCUNIT(dev)];
907 	int s;
908 
909 	if (yc->yc_openf == 0 && yc->yc_timo == INF) {
910 		yc->yc_tact = 0;
911 		return;
912 	}
913 	if (yc->yc_timo != INF && (yc->yc_timo -= 5) < 0) {
914 		printf("yc%d: lost interrupt\n", YCUNIT(dev));
915 		yc->yc_timo = INF;
916 		s = spl3();
917 		cyintr(CYUNIT(dev));
918 		splx(s);
919 	}
920 	timeout(cytimer, (caddr_t)dev, 5*hz);
921 }
922 
923 cyseteof(bp)
924 	register struct buf *bp;
925 {
926 	register int cyunit = CYUNIT(bp->b_dev);
927 	register struct cy_softc *cy = &cy_softc[cyunit];
928 	register struct yc_softc *yc = &yc_softc[YCUNIT(bp->b_dev)];
929 
930 	if (bp == &ccybuf[cyunit]) {
931 		if (yc->yc_blkno > bp->b_blkno) {
932 			/* reversing */
933 			yc->yc_nxrec = bp->b_blkno - htoms(cy->cy_tpb.tpcount);
934 			yc->yc_blkno = yc->yc_nxrec;
935 		} else {
936 			yc->yc_blkno = bp->b_blkno + htoms(cy->cy_tpb.tpcount);
937 			yc->yc_nxrec = yc->yc_blkno - 1;
938 		}
939 		return;
940 	}
941 	/* eof on read */
942 	yc->yc_nxrec = bp->b_blkno;
943 }
944 
945 cyread(dev, uio)
946 	dev_t dev;
947 	struct uio *uio;
948 {
949 	int errno;
950 
951 	errno = cyphys(dev, uio);
952 	if (errno)
953 		return (errno);
954 	return (physio(cystrategy, &rcybuf[CYUNIT(dev)], dev, B_READ, minphys, uio));
955 }
956 
957 cywrite(dev, uio)
958 	dev_t dev;
959 	struct uio *uio;
960 {
961 	int errno;
962 
963 	errno = cyphys(dev, uio);
964 	if (errno)
965 		return (errno);
966 	return (physio(cystrategy, &rcybuf[CYUNIT(dev)], dev, B_WRITE, minphys, uio));
967 }
968 
969 /*
970  * Check that a raw device exits.
971  * If it does, set up the yc_blkno and yc_nxrec
972  * so that the tape will appear positioned correctly.
973  */
974 cyphys(dev, uio)
975 	dev_t dev;
976 	struct uio *uio;
977 {
978 	register int ycunit = YCUNIT(dev);
979 	register daddr_t a;
980 	register struct yc_softc *yc;
981 	register struct vba_device *vi;
982 
983 	if (ycunit >= NYC || (vi = ycdinfo[ycunit]) == 0 || vi->ui_alive == 0)
984 		return (ENXIO);
985 	yc = &yc_softc[ycunit];
986 	a = uio->uio_offset >> DEV_BSHIFT;
987 	yc->yc_blkno = a;
988 	yc->yc_nxrec = a + 1;
989 	return (0);
990 }
991 
992 /*ARGSUSED*/
993 cyioctl(dev, cmd, data, flag)
994 	caddr_t data;
995 	dev_t dev;
996 {
997 	int ycunit = YCUNIT(dev);
998 	register struct yc_softc *yc = &yc_softc[ycunit];
999 	register struct buf *bp = &ccybuf[CYUNIT(dev)];
1000 	register callcount;
1001 	int fcount, op;
1002 	struct mtop *mtop;
1003 	struct mtget *mtget;
1004 	/* we depend of the values and order of the MT codes here */
1005 	static cyops[] =
1006 	{CY_WEOF,CY_FSF,CY_BSF,CY_SFORW,CY_SREV,CY_REW,CY_OFFL,CY_SENSE};
1007 
1008 	switch (cmd) {
1009 
1010 	case MTIOCTOP:	/* tape operation */
1011 		mtop = (struct mtop *)data;
1012 		switch (op = mtop->mt_op) {
1013 
1014 		case MTWEOF:
1015 			callcount = mtop->mt_count;
1016 			fcount = 1;
1017 			break;
1018 
1019 		case MTFSR: case MTBSR:
1020 			callcount = 1;
1021 			fcount = mtop->mt_count;
1022 			break;
1023 
1024 		case MTFSF: case MTBSF:
1025 			callcount = mtop->mt_count;
1026 			fcount = 1;
1027 			break;
1028 
1029 		case MTREW: case MTOFFL: case MTNOP:
1030 			callcount = 1;
1031 			fcount = 1;
1032 			break;
1033 
1034 		default:
1035 			return (ENXIO);
1036 		}
1037 		if (callcount <= 0 || fcount <= 0)
1038 			return (EINVAL);
1039 		while (--callcount >= 0) {
1040 #ifdef notdef
1041 			/*
1042 			 * Gagh, this controller is the pits...
1043 			 */
1044 			if (op == MTFSF || op == MTBSF) {
1045 				do
1046 					cycommand(dev, cyops[op], 1);
1047 				while ((bp->b_flags&B_ERROR) == 0 &&
1048 				 (yc->yc_status&(CYS_EOT|CYS_BOT|CYS_FM)) == 0);
1049 			} else
1050 #endif
1051 				cycommand(dev, cyops[op], fcount);
1052 			dlog((LOG_INFO,
1053 			    "cyioctl: status %x, b_flags %x, resid %d\n",
1054 			    yc->yc_status, bp->b_flags, bp->b_resid));
1055 			if ((bp->b_flags&B_ERROR) ||
1056 			    (yc->yc_status&(CYS_BOT|CYS_EOT)))
1057 				break;
1058 		}
1059 		bp->b_resid = callcount + 1;
1060 		return (geterror(bp));
1061 
1062 	case MTIOCGET:
1063 		cycommand(dev, CY_SENSE, 1);
1064 		mtget = (struct mtget *)data;
1065 		mtget->mt_dsreg = yc->yc_status;
1066 		mtget->mt_erreg = yc->yc_control;
1067 		mtget->mt_resid = yc->yc_resid;
1068 		mtget->mt_type = MT_ISCY;
1069 		break;
1070 
1071 	default:
1072 		return (ENXIO);
1073 	}
1074 	return (0);
1075 }
1076 
1077 /*
1078  * Poll until the controller is ready.
1079  */
1080 cywait(cp)
1081 	register struct cyccb *cp;
1082 {
1083 	register int i = 5000;
1084 
1085 	uncache(&cp->cbgate);
1086 	while (i-- > 0 && cp->cbgate == GATE_CLOSED) {
1087 		DELAY(1000);
1088 		uncache(&cp->cbgate);
1089 	}
1090 	return (i <= 0);
1091 }
1092 
1093 /*
1094  * Load a 20 bit pointer into a Tapemaster pointer.
1095  */
1096 cyldmba(reg, value)
1097 	register caddr_t reg;
1098 	caddr_t value;
1099 {
1100 	register int v = (int)value;
1101 
1102 	*reg++ = v;
1103 	*reg++ = v >> 8;
1104 	*reg++ = 0;
1105 	*reg = (v&0xf0000) >> 12;
1106 }
1107 
1108 /*
1109  * Unconditionally reset all controllers to their initial state.
1110  */
1111 cyreset(vba)
1112 	int vba;
1113 {
1114 	register caddr_t addr;
1115 	register int ctlr;
1116 
1117 	for (ctlr = 0; ctlr < NCY; ctlr++)
1118 		if (cyminfo[ctlr] && cyminfo[ctlr]->um_vbanum == vba) {
1119 			addr = cyminfo[ctlr]->um_addr;
1120 			CY_RESET(addr);
1121 			if (!cyinit(ctlr, addr)) {
1122 				printf("cy%d: reset failed\n", ctlr);
1123 				cyminfo[ctlr] = NULL;
1124 			}
1125 		}
1126 }
1127 
1128 cyuncachetpb(cy)
1129 	struct cy_softc *cy;
1130 {
1131 	register long *lp = (long *)&cy->cy_tpb;
1132 	register int i;
1133 
1134 	for (i = 0; i < howmany(sizeof (struct cytpb), sizeof (long)); i++)
1135 		uncache(lp++);
1136 }
1137 
1138 /*
1139  * Dump routine.
1140  */
1141 #define	DUMPREC	(32*1024)
1142 cydump(dev)
1143 	dev_t dev;
1144 {
1145 	register struct cy_softc *cy;
1146 	register int bs, num, start;
1147 	register caddr_t addr;
1148 	int unit = CYUNIT(dev), error;
1149 
1150 	if (unit >= NCY || cyminfo[unit] == 0 ||
1151 	    (cy = &cy_softc[unit])->cy_bs == 0 || YCUNIT(dev) >= NYC)
1152 		return (ENXIO);
1153 	if (cywait(&cy->cy_ccb))
1154 		return (EFAULT);
1155 #define	phys(a)	((caddr_t)((int)(a)&~0xc0000000))
1156 	addr = phys(cyminfo[unit]->um_addr);
1157 	num = maxfree, start = NBPG*2;
1158 	while (num > 0) {
1159 		bs = num > btoc(DUMPREC) ? btoc(DUMPREC) : num;
1160 		error = cydwrite(cy, start, bs, addr);
1161 		if (error)
1162 			return (error);
1163 		start += bs, num -= bs;
1164 	}
1165 	cyweof(cy, addr);
1166 	cyweof(cy, addr);
1167 	uncache(&cy->cy_tpb);
1168 	if (cy->cy_tpb.tpstatus&CYS_ERR)
1169 		return (EIO);
1170 	cyrewind(cy, addr);
1171 	return (0);
1172 }
1173 
1174 cydwrite(cy, pf, npf, addr)
1175 	register struct cy_softc *cy;
1176 	int pf, npf;
1177 	caddr_t addr;
1178 {
1179 
1180 	cy->cy_tpb.tpcmd = CY_WCOM;
1181 	cy->cy_tpb.tpcontrol = CYCW_LOCK|CYCW_25IPS|CYCW_16BITS;
1182 	cy->cy_tpb.tpstatus = 0;
1183 	cy->cy_tpb.tpsize = htoms(npf*NBPG);
1184 	cyldmba(cy->cy_tpb.tplink, (caddr_t)0);
1185 	cyldmba(cy->cy_tpb.tpdata, (caddr_t)(pf*NBPG));
1186 	cyldmba(cy->cy_ccb.cbtpb, (caddr_t)&cy->cy_tpb);
1187 	cy->cy_ccb.cbgate = GATE_CLOSED;
1188 	CY_GO(addr);
1189 	if (cywait(&cy->cy_ccb))
1190 		return (EFAULT);
1191 	uncache(&cy->cy_tpb);
1192 	if (cy->cy_tpb.tpstatus&CYS_ERR)
1193 		return (EIO);
1194 	return (0);
1195 }
1196 
1197 cyweof(cy, addr)
1198 	register struct cy_softc *cy;
1199 	caddr_t addr;
1200 {
1201 
1202 	cy->cy_tpb.tpcmd = CY_WEOF;
1203 	cy->cy_tpb.tpcount = htoms(1);
1204 	cy->cy_ccb.cbgate = GATE_CLOSED;
1205 	CY_GO(addr);
1206 	(void) cywait(&cy->cy_ccb);
1207 }
1208 
1209 cyrewind(cy, addr)
1210 	register struct cy_softc *cy;
1211 	caddr_t addr;
1212 {
1213 
1214 	cy->cy_tpb.tpcmd = CY_REW;
1215 	cy->cy_tpb.tpcount = htoms(1);
1216 	cy->cy_ccb.cbgate = GATE_CLOSED;
1217 	CY_GO(addr);
1218 	(void) cywait(&cy->cy_ccb);
1219 }
1220 #endif
1221