xref: /original-bsd/sys/vax/uba/tmscp.c (revision b31c5a61)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)tmscp.c	7.16 (Berkeley) 05/09/91
8  */
9 
10 /*
11  * sccsid = "@(#)tmscp.c	1.24	(ULTRIX)	1/21/86";
12  */
13 
14 /************************************************************************
15  *									*
16  *        Licensed from Digital Equipment Corporation 			*
17  *                       Copyright (c) 					*
18  *               Digital Equipment Corporation				*
19  *                   Maynard, Massachusetts 				*
20  *                         1985, 1986 					*
21  *                    All rights reserved. 				*
22  *									*
23  *        The Information in this software is subject to change 	*
24  *   without notice and should not be construed as a commitment 	*
25  *   by  Digital  Equipment  Corporation.   Digital   makes  no 	*
26  *   representations about the suitability of this software for 	*
27  *   any purpose.  It is supplied "As Is" without expressed  or 	*
28  *   implied  warranty. 						*
29  *									*
30  *        If the Regents of the University of California or its 	*
31  *   licensees modify the software in a manner creating  		*
32  *   diriviative copyright rights, appropriate copyright  		*
33  *   legends may be placed on  the drivative work in addition  		*
34  *   to that set forth above. 						*
35  *									*
36  ************************************************************************
37  *
38  * tmscp.c - TMSCP (TK50/TU81) tape device driver
39  *
40  * Modification History:
41  *
42  * 06-Jan-86 - afd
43  *	Changed the probe routine to use DELAY (not TODR).  This now
44  *	works for MicroVAXen as well.  This eliminates the busy-wait
45  *	for MicroVAXen so a dead TK50 controller will not hang autoconf.
46  *
47  * 06-Dec-85 - afd
48  *	Fixed a bug in density selection.  The "set unit characteristics"
49  *	command to select density, was clearing the "unit flags" field
50  *	where the CACHE bit was for TU81-E.  Now the unit's "format" and
51  *	"unitflgs" are saved in tms_info struct.  And are used on STUNT
52  *	commands.
53  *
54  * 19-Oct-85 - afd
55  *	Added support to the open routine to allow drives to be opened
56  *	for low density (800 or 1600 bpi) use.  When the slave routine
57  *	initiates a "get-unit-char" cmd, the format menu for the unit
58  *	is saved in the tms_info structure. The format menu is used in the
59  *	start routine to select the proper low density.
60  *
61  * 02-Oct-85 - afd
62  *	When a tmscp-type controller is initializing, it is possible for
63  *	the sa reg to become 0 between states.  Thus the init code in
64  *	the interrupt routine had to be modified to reflect this.
65  *
66  * 21-Sep-85 - afd
67  *	The TK50 declares a serious exception when a tape mark is encountered.
68  *	This causes problems to dd (& other UN*X utilities).  So a flag
69  *	is set in the rsp() routine when a tape mark is encountered.  If
70  *	this flag is set, the start() routine appends the Clear Serious
71  *	Exception modifier to the next command.
72  *
73  * 03-Sep-85 -- jaw
74  *	messed up previous edit..
75  *
76  * 29-Aug-85 - jaw
77  *	fixed bugs in 8200 and 750 buffered datapath handling.
78  *
79  * 06-Aug-85 - afd
80  *   1. When repositioning records or files, the count of items skipped
81  *	does NOT HAVE to be returned by controllers (& the TU81 doesn't).
82  *	So tmscprsp() had to be modified to stop reporting
83  *	residual count errors on reposition commands.
84  *
85  *   2. Fixed bug in the open routine which allowed multiple opens.
86  *
87  * 18-Jul-85 - afd
88  *   1. Need to return status when mt status (or corresponding ioctl) is done.
89  *	Save resid, flags, endcode & status in tmscprsp() routine (except on
90  *	clear serious exception no-op).  Return these fields when status
91  *	ioctl is done (in tmscpcommand()).  How they are returned:
92  *		mt_resid = resid
93  *		mt_dsreg = flags|endcode
94  *		mt_erreg = status
95  *
96  *   2. Added latent support for enabling/disabling caching.  This is
97  *	handled along with all other ioctl commands.
98  *
99  *   3. Need to issue a no-op on unrecognized ioctl in tmscpstart(), since
100  *	we have already commited to issuing a command at that point.
101  *
102  *   4. In tmscprsp() routine if encode is 0200 (invalid command issued);
103  *	We need to: Unlink the buffer from the I/O wait queue,
104  *	and signal iodone, so the higher level command can exit!
105  *	Just as if it were a valid command.
106  *
107  * 11-jul-85 -- jaw
108  *	fix bua/bda map registers.
109  *
110  * 19-Jun-85 -- jaw
111  *	VAX8200 name change.
112  *
113  * 06-Jun-85 - jaw
114  *	fixes for 8200.
115  *
116  * 9-Apr-85 - afd
117  *	Added timeout code to the probe routine, so if the controller
118  *	fails to init in 10 seconds we return failed status.
119  *
120  * 13-Mar-85 -jaw
121  *	Changes for support of the VAX8200 were merged in.
122  *
123  * 27-Feb-85 -tresvik
124  *	Changes for support of the VAX8600 were merged in.
125  *
126  */
127 
128 #include "tms.h"
129 #if NTMSCP > 0
130 
131 #include "sys/param.h"
132 #include "sys/systm.h"
133 #include "sys/buf.h"
134 #include "sys/conf.h"
135 #include "sys/errno.h"
136 #include "sys/file.h"
137 #include "sys/map.h"
138 #include "sys/vm.h"
139 #include "sys/ioctl.h"
140 #include "sys/syslog.h"
141 #include "sys/mtio.h"
142 #include "sys/cmap.h"
143 #include "sys/uio.h"
144 #include "sys/tprintf.h"
145 
146 #include "../include/pte.h"
147 #include "../include/cpu.h"
148 #include "../include/mtpr.h"
149 #include "ubareg.h"
150 #include "ubavar.h"
151 
152 #define TENSEC	(1000)
153 #define	TMS_PRI	LOG_INFO
154 
155 #define NRSPL2  3               /* log2 number of response packets */
156 #define NCMDL2  3               /* log2 number of command packets */
157 #define NRSP    (1<<NRSPL2)
158 #define NCMD    (1<<NCMDL2)
159 
160 #include "tmscpreg.h"
161 #include "../vax/tmscp.h"
162 
163 /* Software state per controller */
164 
165 struct tmscp_softc {
166 	short   sc_state;       /* state of controller */
167 	short   sc_mapped;      /* Unibus map allocated for tmscp struct? */
168 	int     sc_ubainfo;     /* Unibus mapping info */
169 	struct	tmscp *sc_tmscp;   /* Unibus address of tmscp struct */
170 	int     sc_ivec;        /* interrupt vector address */
171 	short   sc_credits;     /* transfer credits */
172 	short   sc_lastcmd;     /* pointer into command ring */
173 	short   sc_lastrsp;     /* pointer into response ring */
174 	short   sc_ipl;		/* interrupt priority (Q-bus) */
175 } tmscp_softc[NTMSCP];
176 
177 struct tmscp {
178 	struct tmscpca	tmscp_ca;         /* communications area */
179 	struct mscp	tmscp_rsp[NRSP];  /* response packets */
180 	struct mscp	tmscp_cmd[NCMD];  /* command packets */
181 } tmscp[NTMSCP];
182 
183 /*
184  * Per drive-unit info
185  */
186 struct tms_info {
187 	daddr_t		tms_dsize;	/* Max user size from online pkt */
188 	unsigned	tms_type;	/* Drive type int field  */
189 	int		tms_resid;	/* residual from last xfer */
190 	u_char		tms_endcode;	/* last command endcode */
191 	u_char		tms_flags;	/* last command end flags */
192 	unsigned	tms_status;	/* Command status from last command */
193 	char		tms_openf;	/* lock against multiple opens */
194 	char		tms_lastiow;	/* last op was a write */
195 	char		tms_serex;	/* set when serious exception occurs */
196 	char		tms_clserex;	/* set when serex being cleared by no-op */
197 	short		tms_fmtmenu;	/* the unit's format (density) menu */
198 	short		tms_unitflgs;	/* unit flag parameters */
199 	short		tms_format;	/* the unit's current format (density) */
200 	tpr_t 		tms_tpr;	/* tprintf handle */
201 } tms_info[NTMS];
202 struct uba_ctlr *tmscpminfo[NTMSCP];
203 struct uba_device *tmsdinfo[NTMS];
204 /*
205  * ifdef other tmscp devices here if they allow more than 1 unit/controller
206  */
207 struct uba_device *tmscpip[NTMSCP][1];
208 struct buf ctmscpbuf[NTMSCP];		/* internal cmd buffer (for ioctls) */
209 struct buf tmsutab[NTMS];		/* Drive queue */
210 struct buf tmscpwtab[NTMSCP];		/* I/O wait queue, per controller */
211 int    tmscpmicro[NTMSCP];		/* to store microcode level */
212 short  utoctlr[NTMS];			/* Slave unit to controller mapping */
213 					/* filled in by the slave routine */
214 
215 /* Bits in minor device */
216 #define	TMSUNIT(dev)	(minor(dev)&03)
217 #define	T_NOREWIND	04
218 #define	T_HIDENSITY	010
219 
220 /* Slave unit to controller mapping */
221 #define TMSCPCTLR(dev)	(utoctlr[TMSUNIT(dev)])
222 
223 /*
224  * Internal (ioctl) command codes (these must also be declared in the
225  * tmscpioctl routine).  These correspond to ioctls in mtio.h
226  */
227 #define TMS_WRITM	0		/* write tape mark */
228 #define TMS_FSF		1		/* forward space file */
229 #define TMS_BSF		2		/* backward space file */
230 #define TMS_FSR		3		/* forward space record */
231 #define TMS_BSR		4		/* backward space record */
232 #define TMS_REW		5		/* rewind tape */
233 #define TMS_OFFL	6		/* rewind tape & mark unit offline */
234 #define TMS_SENSE	7		/* noop - do a get unit status */
235 #define TMS_CACHE	8		/* enable cache */
236 #define TMS_NOCACHE	9		/* disable cache */
237 /* These go last: after all real mt cmds, just bump the numbers up */
238 #define TMS_CSE		10		/* clear serious exception */
239 #define TMS_LOWDENSITY	11		/* set unit to low density */
240 #define TMS_HIDENSITY	12		/* set unit to high density */
241 
242 /*
243  * Controller states
244  */
245 #define S_IDLE  0               /* hasn't been initialized */
246 #define S_STEP1 1               /* doing step 1 init */
247 #define S_STEP2 2               /* doing step 2 init */
248 #define S_STEP3 3               /* doing step 3 init */
249 #define S_SCHAR 4               /* doing "set controller characteristics" */
250 #define S_RUN   5               /* running */
251 
252 int     tmscperror = 0;		/* causes hex dump of packets */
253 int	tmscp_cp_wait = 0;	/* Something to wait on for command */
254 				/* packets and or credits. */
255 int	wakeup();
256 extern	int	hz;		/* Should find the right include */
257 
258 #ifdef	DEBUG
259 #define printd if (tmscpdebug) printf
260 int tmscpdebug = 1;
261 #define	printd10 if(tmscpdebug >= 10) printf
262 #endif
263 
264 int     tmscpprobe(), tmscpslave(), tmscpattach(), tmscpintr();
265 struct  mscp *tmscpgetcp();
266 
267 #define DRVNAME "tms"
268 #define CTRLNAME "tmscp"
269 
270 u_short tmscpstd[] = { 0174500, 0 };
271 struct  uba_driver tmscpdriver =
272 { tmscpprobe, tmscpslave, tmscpattach, 0, tmscpstd, DRVNAME, tmsdinfo, CTRLNAME
273 , tmscpminfo, 0};
274 
275 #define b_qsize         b_resid         /* queue size per drive, in tmsutab */
276 #define b_ubinfo        b_resid         /* Unibus mapping info, per buffer */
277 
278 
279 /*************************************************************************/
280 
281 #define DELAYTEN 1000
282 
283 /*
284  * Unfortunately qbgetpri can't be used because the TK50 doesn't flip the
285  * TMSCP_STEP2 flag in the tmscpsa register until after the pending interrupt
286  * has been acknowledged by the cpu. If you are at spl6(), the TMSCP_STEP2
287  * flag never gets set and you return (0).
288  */
289 tmscpprobe(reg, ctlr)
290 	caddr_t reg;		/* address of the IP register */
291 	int ctlr;		/* index of controller in the tmscp_softc array */
292 {
293 	register int br, cvec;	/* MUST be 1st (r11 & r10): IPL and intr vec */
294 	register struct tmscp_softc *sc = &tmscp_softc[ctlr];
295 				/* ptr to software controller structure */
296 	struct tmscpdevice *tmscpaddr; /* ptr to tmscpdevice struct (IP & SA) */
297 	int count;		/* for probe delay time out */
298 
299 #	ifdef lint
300 	br = 0; cvec = br; br = cvec; reg = reg;
301 	tmscpreset(0); tmscpintr(0);
302 #	endif
303 
304 	tmscpaddr = (struct tmscpdevice *) reg;
305 	/*
306 	 * Set host-settable interrupt vector.
307 	 * Assign 0 to the ip register to start the tmscp-device initialization.
308 	 * The device is not really initialized at this point, this is just to
309 	 * find out if the device exists.
310 	 */
311 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
312 	tmscpaddr->tmscpip = 0;
313 
314 	count=0;
315 	while(count < DELAYTEN)
316 		{	/* wait for at most 10 secs */
317 		if((tmscpaddr->tmscpsa & TMSCP_STEP1) != 0)
318 			break;
319 		DELAY(10000);
320 		count=count+1;
321 		}
322 	if (count == DELAYTEN)
323 		return(0);
324 
325 	tmscpaddr->tmscpsa = TMSCP_ERR|(NCMDL2<<11)|(NRSPL2<<8)|TMSCP_IE|(sc->sc_ivec/4);
326 
327 	count=0;
328 	while(count < DELAYTEN)
329 		{
330 		if((tmscpaddr->tmscpsa & TMSCP_STEP2) != 0)
331 			break;
332 		DELAY(10000);
333 		count = count+1;
334 		}
335 	if (count == DELAYTEN)
336 		return(0);
337 
338 #ifdef QBA
339 	sc->sc_ipl = br = 0x15;
340 #endif
341 	return(sizeof (struct tmscpdevice));
342 }
343 
344 /*
345  * Try to find a slave (a drive) on the controller.
346  * If the controller is not in the run state, call init to initialize it.
347  */
348 tmscpslave (ui, reg)
349 	struct uba_device *ui;	/* ptr to the uba device structure */
350 	caddr_t reg;		/* addr of the device controller */
351 {
352 	register struct uba_ctlr *um = tmscpminfo[ui->ui_ctlr];
353 	register struct tmscp_softc *sc = &tmscp_softc[ui->ui_ctlr];
354 	register struct tms_info *tms = &tms_info[ui->ui_unit];
355 	struct   tmscpdevice *tmscpaddr;	/* ptr to IP & SA */
356 	struct   mscp *mp;
357 	int	 i;			/* Something to write into to start */
358 					/* the tmscp polling */
359 
360 #	ifdef lint
361 	reg = reg;
362 #	endif
363 	tmscpaddr = (struct tmscpdevice *)um->um_addr;
364 	/*
365 	 * If its not in the run state, start the initialization process
366 	 * (tmscpintr will complete it);  if the initialization doesn't start;
367 	 * then return.
368 	 */
369 	if(sc->sc_state != S_RUN)
370 		{
371 #		ifdef DEBUG
372 		printd("tmscpslave: ctlr not running: calling init \n");
373 #		endif
374 		if(!tmscpinit(ui->ui_ctlr))
375 	    		return(0);
376 		}
377 	/*
378 	 * Wait for the controller to come into the run state or go idle.
379 	 * If it goes idle return.
380 	 */
381 #	ifdef DEBUG
382 	i=1;
383 #	endif
384 	while(sc->sc_state != S_RUN && sc->sc_state != S_IDLE)
385 #		ifdef DEBUG
386 		if (tmscpaddr->tmscpsa & TMSCP_ERR && i)
387 	    		{
388 	    		printd("tmscp-device: fatal error (%o)\n", tmscpaddr->tmscpsa&0xffff);
389 	    		i=0;
390 	   		 }
391 #		endif
392 		;	/* wait */
393 	if(sc->sc_state == S_IDLE)
394 		{	/* The tmscp device failed to initialize */
395 		printf("tmscp controller failed to init\n");
396 		return(0);
397 		}
398 	/* The controller is up so see if the drive is there */
399 	if(0 == (mp = tmscpgetcp(um)))
400 		{
401 		printf("tmscp can't get command packet\n");
402 		return(0);
403 		}
404 	/* Need to determine the drive type for generic driver */
405 	mp->mscp_opcode = M_OP_GTUNT;	/* This should give us the device type */
406 	mp->mscp_unit = ui->ui_slave;
407 	mp->mscp_cmdref = (long) ui->ui_slave;
408 	tms->tms_status = 0;	/* set to zero */
409 	tmscpip[ui->ui_ctlr][ui->ui_slave] = ui;
410 	*((long *) mp->mscp_dscptr ) |= TMSCP_OWN | TMSCP_INT;/* maybe we should poll*/
411 	i = tmscpaddr->tmscpip;
412 #ifdef lint
413 	i = i;
414 #endif
415 	while(!tms->tms_status)
416 		;				/* Wait for some status */
417 #	ifdef DEBUG
418 	printd("tmscpslave: status = %o\n",tms->tms_status & M_ST_MASK);
419 #	endif
420 	tmscpip[ui->ui_ctlr][ui->ui_slave] = 0;
421 	if(!tms->tms_type)			/* packet from a GTUNT */
422 		return(0);			/* Failed No such drive */
423 	else
424 		return(1);			/* Got it and it is there */
425 }
426 
427 
428 /*
429  * Set ui flags to zero to show device is not online & set tmscpip.
430  * Unit to Controller mapping is set up here.
431  * Open routine will issue the online command, later.
432  */
433 tmscpattach (ui)
434 	register struct uba_device *ui;		/* ptr to unibus dev struct */
435 {
436 
437 	ui->ui_flags = 0;
438 	tmscpip[ui->ui_ctlr][ui->ui_slave] = ui;
439 #	ifdef DEBUG
440 	/*
441 	 * Check to see if the drive is available.
442 	 * If not then just print debug.
443 	 */
444 	if(tms_info[ui->ui_unit].tms_status != M_ST_AVLBL)
445 		printd("tmscpattach: unavailable \n");
446 #	endif
447 	utoctlr[ui->ui_unit] = ui->ui_ctlr;
448 }
449 
450 
451 /*
452  * TMSCP interrupt routine.
453  */
454 tmscpintr (d)
455 	int d;		/* index to the controller */
456 {
457 	register struct uba_ctlr *um = tmscpminfo[d];
458 	register struct tmscpdevice *tmscpaddr = (struct tmscpdevice *)um->um_addr;
459 	struct buf *bp;
460 	register int i;
461 	register struct tmscp_softc *sc = &tmscp_softc[d];
462 	register struct tmscp *tm = &tmscp[d];
463 	struct tmscp *ttm;
464 	struct mscp *mp;
465 
466 #	ifdef DEBUG
467 	printd10("tmscpintr: state %d, tmscpsa %o\n", sc->sc_state, tmscpaddr->tmscpsa);
468 #	endif
469 
470 #ifdef QBA
471 	splx(sc->sc_ipl);
472 #endif
473 	/*
474 	 * How the interrupt is handled depends on the state of the controller.
475 	 */
476 	switch (sc->sc_state) {
477 
478 	case S_IDLE:
479 		printf("tmscp%d: random interrupt ignored\n", d);
480 		return;
481 
482 	/* Controller was in step 1 last, see if its gone to step 2 */
483 	case S_STEP1:
484 #		define STEP1MASK 0174377
485 #		define STEP1GOOD (TMSCP_STEP2|TMSCP_IE|(NCMDL2<<3)|NRSPL2)
486 		for (i = 0; i < 150; i++)
487 			{
488 			if ((tmscpaddr->tmscpsa&STEP1MASK) != STEP1GOOD)
489 				{ /* still in step 1 (wait 1/100 sec) */
490 				DELAY(10000);
491 #				ifdef DEBUG
492 				printd("still in step 1, delaying\n");
493 #				endif DEBUG
494 				}
495 			else
496 				break;
497 			}
498 		if (i > 149)
499 			{
500 			sc->sc_state = S_IDLE;
501 			printf("failed to initialize, in step1: sa 0x%x", tmscpaddr->tmscpsa);
502 			wakeup((caddr_t)um);
503 			return;
504 			}
505 		tmscpaddr->tmscpsa = ((int)&sc->sc_tmscp->tmscp_ca.ca_ringbase)
506 			| ((cpu == VAX_780 || cpu == VAX_8600) ? TMSCP_PI : 0);
507 		sc->sc_state = S_STEP2;
508 		return;
509 
510 	/* Controller was in step 2 last, see if its gone to step 3 */
511 	case S_STEP2:
512 #		define STEP2MASK 0174377
513 #		define STEP2GOOD (TMSCP_STEP3|TMSCP_IE|(sc->sc_ivec/4))
514 		for (i = 0; i < 150; i++)
515 			{
516 			if ((tmscpaddr->tmscpsa&STEP2MASK) != STEP2GOOD)
517 				{ /* still in step 2 (wait 1/100 sec) */
518 				DELAY(10000);
519 #				ifdef DEBUG
520 				printd("still in step 2, delaying\n");
521 #				endif DEBUG
522 				}
523 			else
524 				break;
525 			}
526 		if (i > 149)
527 			{
528 			sc->sc_state = S_IDLE;
529 			printf("failed to initialize, in step2: sa 0x%x", tmscpaddr->tmscpsa);
530 			wakeup((caddr_t)um);
531 			return;
532 			}
533 		tmscpaddr->tmscpsa = ((int)&sc->sc_tmscp->tmscp_ca.ca_ringbase)>>16;
534 		sc->sc_state = S_STEP3;
535 		return;
536 
537 	/* Controller was in step 3 last, see if its gone to step 4 */
538 	case S_STEP3:
539 #		define STEP3MASK 0174000
540 #		define STEP3GOOD TMSCP_STEP4
541 		for (i = 0; i < 150; i++)
542 			{
543 			if ((tmscpaddr->tmscpsa&STEP3MASK) != STEP3GOOD)
544 				{ /* still in step 3 (wait 1/100 sec) */
545 				DELAY(10000);
546 #				ifdef DEBUG
547 				printd("still in step 3, delaying\n");
548 #				endif DEBUG
549 				}
550 			else
551 				break;
552 			}
553 		if (i > 149)
554 			{
555 			sc->sc_state = S_IDLE;
556 			printf("failed to initialize, in step3: sa 0x%x", tmscpaddr->tmscpsa);
557 			wakeup((caddr_t)um);
558 			return;
559 			}
560 		/*
561 		 * Get microcode version and model number of controller;
562 		 * Signal initialization complete (_GO) (to the controller);
563 		 *    ask for Last Fail response if tmscperror is set;
564 		 * Set state to "set controller characteristics".
565 		 */
566 		tmscpmicro[d] = tmscpaddr->tmscpsa;
567 		tmscpaddr->tmscpsa = TMSCP_GO | (tmscperror? TMSCP_LF : 0);
568 		sc->sc_state = S_SCHAR;
569 #		ifdef DEBUG
570 		printd("tmscpintr: completed state %d \n", sc->sc_state);
571 		printd("tmscp%d Version %d model %d\n",d,tmscpmicro[d]&0xF,
572 			(tmscpmicro[d]>>4) & 0xF);
573 #		endif
574 
575 	    /*
576 	     * Initialize the data structures (response and command queues).
577 	     */
578 	    ttm = sc->sc_tmscp;
579 	    for (i = 0; i < NRSP; i++)
580 		    {
581 		    tm->tmscp_ca.ca_rspdsc[i] = TMSCP_OWN | TMSCP_INT |
582 			   (long)&ttm->tmscp_rsp[i].mscp_cmdref;
583 		    tm->tmscp_rsp[i].mscp_dscptr = &tm->tmscp_ca.ca_rspdsc[i];
584 		    tm->tmscp_rsp[i].mscp_header.tmscp_msglen = mscp_msglen;
585 		    }
586 	    for (i = 0; i < NCMD; i++)
587 		    {
588 		    tm->tmscp_ca.ca_cmddsc[i] = TMSCP_INT |
589 					(long)&ttm->tmscp_cmd[i].mscp_cmdref;
590 		    tm->tmscp_cmd[i].mscp_dscptr = &tm->tmscp_ca.ca_cmddsc[i];
591 		    tm->tmscp_cmd[i].mscp_header.tmscp_msglen = mscp_msglen;
592 		    tm->tmscp_cmd[i].mscp_header.tmscp_vcid = 1;
593 		    }
594 	    bp = &tmscpwtab[d];
595 	    bp->av_forw = bp->av_back = bp;
596 	    sc->sc_lastcmd = 1;
597 	    sc->sc_lastrsp = 0;
598 	    mp = &tmscp[um->um_ctlr].tmscp_cmd[0];
599 	    mp->mscp_unit = mp->mscp_modifier = 0;
600 	    mp->mscp_flags = 0;
601 	    mp->mscp_version = 0;
602 	    mp->mscp_cntflgs = M_CF_ATTN|M_CF_MISC|M_CF_THIS;
603 	    /*
604 	     * A host time out value of 0 means that the controller will not
605 	     * time out.  This is ok for the TK50.
606 	     */
607 	    mp->mscp_hsttmo = 0;
608 	    mp->mscp_time.val[0] = 0;
609 	    mp->mscp_time.val[1] = 0;
610 	    mp->mscp_cntdep = 0;
611 	    mp->mscp_opcode = M_OP_STCON;
612 	    *((long *)mp->mscp_dscptr) |= TMSCP_OWN|TMSCP_INT;
613 	    i = tmscpaddr->tmscpip;      /* initiate polling */
614 	    return;
615 
616 	case S_SCHAR:
617 	case S_RUN:
618 		break;
619 
620 	default:
621 	    printf("tmscp%d: interrupt in unknown state %d ignored\n",d,sc->sc_state);
622 	    return;
623 	}	/* end switch */
624 
625 	/*
626 	 * The controller state is S_SCHAR or S_RUN
627 	 */
628 
629 	/*
630 	 * If the error bit is set in the SA register then print an error
631 	 * message and reinitialize the controller.
632 	 */
633 	if (tmscpaddr->tmscpsa&TMSCP_ERR)
634 		{
635 		printf("tmscp%d: fatal error (%o)\n", d, tmscpaddr->tmscpsa&0xffff);
636 		tmscpaddr->tmscpip = 0;
637 		wakeup((caddr_t)um);
638 		}
639 	/*
640 	 * Check for a buffer purge request. (Won't happen w/ TK50 on Q22 bus)
641 	 */
642 	if (tm->tmscp_ca.ca_bdp)
643 		{
644 		UBAPURGE(um->um_hd->uh_uba, tm->tmscp_ca.ca_bdp);
645 		tm->tmscp_ca.ca_bdp = 0;
646 		tmscpaddr->tmscpsa = 0;      /* signal purge complete */
647 		}
648 
649 	/*
650 	 * Check for response ring transition.
651 	 */
652 	if (tm->tmscp_ca.ca_rspint)
653 		{
654 		tm->tmscp_ca.ca_rspint = 0;
655 		for (i = sc->sc_lastrsp;; i++)
656 			{
657 			i %= NRSP;
658 			if (tm->tmscp_ca.ca_rspdsc[i]&TMSCP_OWN)
659 			    break;
660 			tmscprsp(um, tm, sc, i);
661 			tm->tmscp_ca.ca_rspdsc[i] |= TMSCP_OWN;
662 			}
663 		sc->sc_lastrsp = i;
664 		}
665 
666 	/*
667 	 * Check for command ring transition.
668 	 */
669 	if (tm->tmscp_ca.ca_cmdint)
670 		{
671 #		ifdef DEBUG
672 		printd("tmscpintr: command ring transition\n");
673 #		endif
674 		tm->tmscp_ca.ca_cmdint = 0;
675 		}
676     	if(tmscp_cp_wait)
677 		wakeup((caddr_t)&tmscp_cp_wait);
678     	(void) tmscpstart(um);
679 }
680 
681 
682 /*
683  * Open a tmscp device and set the unit online.  If the controller is not
684  * in the run state, call init to initialize the tmscp controller first.
685  */
686 
687 /* ARGSUSED */
688 tmscpopen(dev, flag)
689 	dev_t dev;
690 	int flag;
691 {
692 	register int unit;
693 	register struct uba_device *ui;
694 	register struct tmscp_softc *sc;
695 	register struct tms_info *tms;
696 	register struct mscp *mp;
697 	register struct uba_ctlr *um;
698 	struct tmscpdevice *tmscpaddr;
699 	int s,i;
700 
701 	unit = TMSUNIT(dev);
702 #	ifdef DEBUG
703 	printd("tmscpopen unit %d\n",unit);
704 	if(tmscpdebug)DELAY(10000);
705 #	endif
706 	if (unit >= NTMS || (ui = tmsdinfo[unit]) == 0 || ui->ui_alive == 0)
707 		return (ENXIO);
708 	tms = &tms_info[ui->ui_unit];
709 	if (tms->tms_openf)
710 		return (EBUSY);
711 	sc = &tmscp_softc[ui->ui_ctlr];
712 	tms->tms_openf = 1;
713 	tms->tms_tpr = tprintf_open();
714 	s = spl5();
715 	if (sc->sc_state != S_RUN)
716 		{
717 		if (sc->sc_state == S_IDLE)
718 			if(!tmscpinit(ui->ui_ctlr))
719 				{
720 				printf("tmscp controller failed to init\n");
721 				(void) splx(s);
722 				tms->tms_openf = 0;
723 				return(ENXIO);
724 				}
725 		/*
726 		 * Wait for initialization to complete
727 		 */
728 		timeout(wakeup,(caddr_t)ui->ui_mi,11*hz);	/* to be sure*/
729 		sleep((caddr_t)ui->ui_mi, 0);
730 		if (sc->sc_state != S_RUN)
731 			{
732 			(void) splx(s);
733 			tms->tms_openf = 0;
734 			return (EIO);
735 			}
736 		}
737 	/*
738 	 * Check to see if the device is really there.
739 	 * this code was taken from Fred Canters 11 driver
740 	 */
741 	um = ui->ui_mi;
742 	tmscpaddr = (struct tmscpdevice *) um->um_addr;
743 	(void) splx(s);
744 	if(ui->ui_flags == 0)
745 		{
746 		s = spl5();
747 		while(0 ==(mp = tmscpgetcp(um)))
748 			{
749 			tmscp_cp_wait++;
750 			sleep((caddr_t)&tmscp_cp_wait,PSWP+1);
751 			tmscp_cp_wait--;
752 			}
753 		(void) splx(s);
754 		mp->mscp_opcode = M_OP_ONLIN;
755 		mp->mscp_unit = ui->ui_slave;
756 		mp->mscp_cmdref = (long) & tms->tms_type;
757 					    /* need to sleep on something */
758 #		ifdef DEBUG
759 		printd("tmscpopen: bring unit %d online\n",ui->ui_unit);
760 #		endif
761 		*((long *) mp->mscp_dscptr ) |= TMSCP_OWN | TMSCP_INT;
762 		i = tmscpaddr->tmscpip;
763 #ifdef lint
764 		i = i;
765 #endif
766 		/*
767 		 * To make sure we wake up, timeout in 240 seconds.
768 		 * Wakeup in tmscprsp routine.
769 		 * 240 seconds (4 minutes) is necessary since a rewind
770 		 * can take a few minutes.
771 		 */
772 		timeout(wakeup,(caddr_t) mp->mscp_cmdref,240 * hz);
773 		sleep((caddr_t) mp->mscp_cmdref,PSWP+1);
774 		}
775 	if(ui->ui_flags == 0) {
776 		tms->tms_openf = 0;
777 		return(ENXIO);  /* Didn't go online */
778 	}
779 	tms->tms_lastiow = 0;
780 	/*
781 	 * If the high density device is not specified, set unit to low
782 	 * density.  This is done as an "internal" ioctl command so
783 	 * that the command setup and response handling
784 	 * is done thru "regular" command routines.
785 	 */
786 	if ((minor(dev) & T_HIDENSITY) == 0)
787 		tmscpcommand(dev, TMS_LOWDENSITY, 1);
788 	else
789 		tmscpcommand(dev, TMS_HIDENSITY, 1);
790 	return (0);
791 }
792 
793 
794 /*
795  * Close tape device.
796  *
797  * If tape was open for writing or last operation was
798  * a write, then write two EOF's and backspace over the last one.
799  * Unless this is a non-rewinding special file, rewind the tape.
800  *
801  * NOTE:
802  *	We want to be sure that any serious exception is cleared on the
803  *	close. A Clear Serious Exception (CSE) modifier is always done on
804  *	the rewind command.  For the non-rewind case we check to see if the
805  *	"serex" field is set in the softc struct; if it is then issue a noop
806  *	command with the CSE modifier.
807  * Make the tape available to others, by clearing openf flag.
808  */
809 tmscpclose(dev, flag)
810 	register dev_t dev;
811 	register flag;
812 {
813 	register struct tms_info *tms;
814 	register struct uba_device *ui;
815 
816 	ui = tmsdinfo[TMSUNIT(dev)];
817 #	ifdef DEBUG
818 	printd("tmscpclose: ctlr =  %d\n",TMSCPCTLR(dev));
819 	printd("tmscpclose: unit = %d\n",TMSUNIT(dev));
820 	if(tmscpdebug)DELAY(10000);
821 #	endif
822 	tms = &tms_info[ui->ui_unit];
823 	if (flag == FWRITE || (flag&FWRITE) && tms->tms_lastiow)
824 		{
825 		/*	   device, command, count */
826 		tmscpcommand (dev, TMS_WRITM, 1);
827 		tmscpcommand (dev, TMS_WRITM, 1);
828 		tmscpcommand (dev, TMS_BSR, 1);
829 		}
830 	if ((minor(dev)&T_NOREWIND) == 0)
831 		/*
832 		 * Don't hang waiting for rewind complete.
833 		 */
834 		tmscpcommand(dev, TMS_REW, 0);
835 	else
836 		if (tms->tms_serex)
837 			{
838 #			ifdef DEBUG
839 			printd("tmscpclose: clearing serex\n");
840 			if(tmscpdebug)DELAY(10000);
841 #			endif
842 			tmscpcommand(dev, TMS_CSE, 1);
843 			}
844 	tprintf_close(tms->tms_tpr);
845 	tms->tms_openf = 0;
846 	return (0);
847 }
848 
849 
850 /*
851  * Execute a command on the tape drive a specified number of times.
852  * This routine sets up a buffer and calls the strategy routine which
853  * links the buffer onto the drive's buffer queue.
854  * The start routine will take care of creating a tmscp command packet
855  * with the command.  The start routine is called by the strategy or the
856  * interrupt routine.
857  */
858 
859 tmscpcommand (dev, com, count)
860 	register dev_t dev;
861 	int com, count;
862 {
863 	register struct uba_device *ui;
864 	register struct buf *bp;
865 	register int s;
866 	int unit = TMSUNIT(dev);
867 
868 	ui = tmsdinfo[unit];
869 	bp = &ctmscpbuf[ui->ui_ctlr];
870 
871 	s = spl5();
872 	while (bp->b_flags&B_BUSY)
873 		{
874 		/*
875 		 * This special check is because B_BUSY never
876 		 * gets cleared in the non-waiting rewind case.
877 		 */
878 		if (bp->b_bcount == 0 && (bp->b_flags&B_DONE))
879 			break;
880 		bp->b_flags |= B_WANTED;
881 		sleep((caddr_t)bp, PRIBIO);
882 		}
883 	bp->b_flags = B_BUSY|B_READ;
884 	splx(s);
885 	/*
886 	 * Load the buffer.  The b_count field gets used to hold the command
887 	 * count.  the b_resid field gets used to hold the command mneumonic.
888 	 * These 2 fields are "known" to be "safe" to use for this purpose.
889 	 * (Most other drivers also use these fields in this way.)
890 	 */
891 	bp->b_dev = dev;
892 	bp->b_bcount = count;
893 	bp->b_resid = com;
894 	bp->b_blkno = 0;
895 	tmscpstrategy(bp);
896 	/*
897 	 * In case of rewind from close, don't wait.
898 	 * This is the only case where count can be 0.
899 	 */
900 	if (count == 0)
901 		return;
902 	iowait(bp);
903 	if (bp->b_flags&B_WANTED)
904 		wakeup((caddr_t)bp);
905 	bp->b_flags &= B_ERROR;
906 }
907 
908 /*
909  * Find an unused command packet
910  */
911 struct mscp *
912 tmscpgetcp(um)
913 	struct uba_ctlr *um;
914 {
915 	register struct mscp *mp;
916 	register struct tmscpca *cp;
917 	register struct tmscp_softc *sc;
918 	register int i;
919 	int	s;
920 
921 	s = spl5();
922 	cp = &tmscp[um->um_ctlr].tmscp_ca;
923 	sc = &tmscp_softc[um->um_ctlr];
924 	/*
925 	 * If no credits, can't issue any commands
926 	 * until some outstanding commands complete.
927 	 */
928 	i = sc->sc_lastcmd;
929 #  	ifdef DEBUG
930 	printd10("tmscpgetcp: %d credits remain\n", sc->sc_credits);
931 #	endif
932 	if(((cp->ca_cmddsc[i]&(TMSCP_OWN|TMSCP_INT))==TMSCP_INT) &&
933 	  (sc->sc_credits >= 2))
934 		{
935 		sc->sc_credits--;       /* This commits to issuing a command */
936 		cp->ca_cmddsc[i] &= ~TMSCP_INT;
937 		mp = &tmscp[um->um_ctlr].tmscp_cmd[i];
938 		mp->mscp_unit = mp->mscp_modifier = 0;
939 		mp->mscp_opcode = mp->mscp_flags = 0;
940 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
941 		sc->sc_lastcmd = (i + 1) % NCMD;
942 		(void) splx(s);
943 		return(mp);
944 		}
945 	(void) splx(s);
946 	return(NULL);
947 }
948 
949 
950 /*
951  * Initialize a TMSCP device.  Set up UBA mapping registers,
952  * initialize data structures, and start hardware
953  * initialization sequence.
954  */
955 tmscpinit (d)
956 	int d;			/* index to the controller */
957 {
958 	register struct tmscp_softc *sc;
959 	register struct tmscp *t;  /* communications area; cmd & resp packets */
960 	struct tmscpdevice *tmscpaddr;
961 	struct uba_ctlr *um;
962 
963 	sc = &tmscp_softc[d];
964 	um = tmscpminfo[d];
965 	um->um_tab.b_active++;
966 	t = &tmscp[d];
967 	tmscpaddr = (struct tmscpdevice *)um->um_addr;
968 	if (sc->sc_mapped == 0)
969 		{
970 		/*
971 		 * Map the communications area and command
972 		 * and response packets into Unibus address
973 		 * space.
974 		 */
975 		sc->sc_ubainfo = uballoc(um->um_ubanum, (caddr_t)t, sizeof (struct tmscp), 0);
976 		sc->sc_tmscp = (struct tmscp *)(UBAI_ADDR(sc->sc_ubainfo));
977 		sc->sc_mapped = 1;
978 		}
979 
980 	/*
981 	 * Start the hardware initialization sequence.
982 	 */
983 	tmscpaddr->tmscpip = 0;              /* start initialization */
984 
985 	while((tmscpaddr->tmscpsa & TMSCP_STEP1) == 0)
986 		{
987 #		ifdef DEBUG
988 		printd("tmscpinit: tmscpsa = 0%o\n",tmscpaddr->tmscpsa);
989 		DELAY(100000);
990 #		endif
991 		if(tmscpaddr->tmscpsa & TMSCP_ERR)
992 			return(0);	/* CHECK */
993 		}
994 	tmscpaddr->tmscpsa=TMSCP_ERR|(NCMDL2<<11)|(NRSPL2<<8)|TMSCP_IE|(sc->sc_ivec/4);
995 	/*
996 	 * Initialization continues in the interrupt routine.
997 	 */
998 	sc->sc_state = S_STEP1;
999 	sc->sc_credits = 0;
1000 	return(1);
1001 }
1002 
1003 
1004 /*
1005  * Start I/O operation
1006  * This code is convoluted.  The majority of it was copied from the uda driver.
1007  */
1008 
1009 tmscpstart(um)
1010 	register struct uba_ctlr *um;
1011 {
1012 	register struct buf *bp, *dp;
1013 	register struct mscp *mp;
1014 	register struct tmscp_softc *sc;
1015 	register struct tms_info *tms;
1016 	register struct uba_device *ui;
1017 	struct   tmscpdevice *tmscpaddr;
1018 	struct   tmscp *tm = &tmscp[um->um_ctlr];
1019 	int i,tempi;
1020 	char ioctl;		/* flag: set true if its an IOCTL command */
1021 
1022 	sc = &tmscp_softc[um->um_ctlr];
1023 
1024     for(;;)
1025 	{
1026 	if ((dp = um->um_tab.b_actf) == NULL)
1027 		{
1028 		/*
1029 		 * Release unneeded UBA resources and return
1030 		 * (drive was inactive)
1031 		 */
1032 		um->um_tab.b_active = 0;
1033 		break;
1034 		}
1035 	if ((bp = dp->b_actf) == NULL)
1036 		{
1037 		/*
1038 		 * No more requests for this drive, remove
1039 		 * from controller queue and look at next drive.
1040 		 * We know we're at the head of the controller queue.
1041 		 */
1042 		dp->b_active = 0;
1043 		um->um_tab.b_actf = dp->b_forw;
1044 		continue;		/* Need to check for loop */
1045 		}
1046 	um->um_tab.b_active++;
1047 	tmscpaddr = (struct tmscpdevice *)um->um_addr;
1048 	ui = tmsdinfo[(TMSUNIT(bp->b_dev))];
1049 	tms = &tms_info[ui->ui_unit];
1050 	if ((tmscpaddr->tmscpsa&TMSCP_ERR) || sc->sc_state != S_RUN)
1051 		{
1052 		tprintf(tms->tms_tpr,
1053 		    "tms%d: hard error bn%d\n",
1054 		    minor(bp->b_dev)&03, bp->b_blkno);
1055 		log(TMS_PRI, "tmscp%d: sa 0%o, state %d\n",um->um_ctlr,
1056 				tmscpaddr->tmscpsa&0xffff, sc->sc_state);
1057 		(void)tmscpinit(um->um_ctlr);
1058 		/* SHOULD REQUEUE OUTSTANDING REQUESTS, LIKE TMSCPRESET */
1059 		break;
1060 		}
1061 	/*
1062 	 * Default is that last command was NOT a write command;
1063 	 * if a write command is done it will be detected in tmscprsp.
1064 	 */
1065 	tms->tms_lastiow = 0;
1066 	if (ui->ui_flags == 0)
1067 		{        /* not online */
1068 		if ((mp = tmscpgetcp(um)) == NULL)
1069 			break;
1070 		mp->mscp_opcode = M_OP_ONLIN;
1071 		mp->mscp_unit = ui->ui_slave;
1072 		dp->b_active = 2;
1073 		um->um_tab.b_actf = dp->b_forw; /* remove from controller q */
1074 		*((long *)mp->mscp_dscptr) |= TMSCP_OWN|TMSCP_INT;
1075 		if (tmscpaddr->tmscpsa&TMSCP_ERR)
1076 			printf("tmscp%d fatal error (0%o)\n",um->um_ctlr,
1077 					tmscpaddr->tmscpsa&0xffff);
1078 		i = tmscpaddr->tmscpip;
1079 		continue;
1080 		}
1081 	switch (cpu) {
1082 
1083 	case VAX_8600:
1084 	case VAX_780:
1085 		i = UBA_NEEDBDP|UBA_CANTWAIT;
1086 		break;
1087 	case VAX_750:
1088 		i = um->um_ubinfo|UBA_HAVEBDP|UBA_CANTWAIT;
1089 		break;
1090 	case VAX_730:
1091 	case VAX_630:
1092 		i = UBA_CANTWAIT;
1093 		break;
1094 	}   /* end switch (cpu) */
1095 	/*
1096 	 * If command is an ioctl command then set the ioctl flag for later use.
1097 	 * If not (i.e. it is a read or write) then attempt
1098 	 * to set up a buffer pointer.
1099 	 */
1100 	ioctl = 0;
1101 	if (bp == &ctmscpbuf[um->um_ctlr])
1102 		ioctl = 1;
1103 	else
1104 		if ((i = ubasetup(um->um_ubanum, bp, i)) == 0)
1105 			{
1106 			if(dp->b_qsize != 0)
1107 				break; /* When a command completes and */
1108 				     /* frees a bdp tmscpstart will be called */
1109 			if ((mp = tmscpgetcp(um)) == NULL)
1110 				break;
1111 #			ifdef DEBUG
1112 			printd("tmscpstart: GTUNT %d ubasetup = %d\n",ui->ui_unit, i);
1113 			if(tmscpdebug)DELAY(10000);
1114 #			endif
1115 			mp->mscp_opcode = M_OP_GTUNT;
1116 			mp->mscp_unit = ui->ui_slave;
1117 			*((long *)mp->mscp_dscptr) |= TMSCP_OWN|TMSCP_INT;
1118 			if (tmscpaddr->tmscpsa&TMSCP_ERR)
1119 				printf("tmscp%d: fatal error (0%o)\n",um->um_ctlr,
1120 					    tmscpaddr->tmscpsa&0xffff);
1121 			i = tmscpaddr->tmscpip;	/* initiate polling */
1122 			break;
1123 			}
1124 #	if defined(VAX750)
1125 	if (cpu == VAX_750)
1126 		tempi = i & 0xfffffff;			/* mask off bdp */
1127 	else
1128 #	endif
1129 		tempi = i;
1130 	if ((mp = tmscpgetcp(um)) == NULL)
1131 		{
1132 		if (!ioctl)		/* only need to release if NOT ioctl */
1133 			ubarelse(um->um_ubanum,&tempi);
1134 		break;
1135 		}
1136 	mp->mscp_cmdref = (long)bp;		/* pointer to get back */
1137 	mp->mscp_unit = ui->ui_slave;
1138 	/*
1139 	 * If its an ioctl-type command then set up the appropriate
1140 	 * tmscp command;  by doing a switch on the "b_resid" field where
1141 	 * the command mneumonic is stored.
1142 	 */
1143 	if (ioctl)
1144 		{
1145 #		ifdef DEBUG
1146 		printd("tmscpstart: doing ioctl cmd %d\n", bp->b_resid);
1147 #		endif
1148 		/*
1149 		 * The reccnt and tmkcnt fields are set to zero by the getcp
1150 		 * routine (as bytecnt and buffer fields).  Thus reccnt and
1151 		 * tmkcnt are only modified here if they need to be set to
1152 		 * a non-zero value.
1153 		 */
1154 		switch ((int)bp->b_resid) {
1155 
1156 		case TMS_WRITM:
1157 			mp->mscp_opcode = M_OP_WRITM;
1158 			break;
1159 		case TMS_FSF:
1160 			mp->mscp_opcode = M_OP_REPOS;
1161 			mp->mscp_tmkcnt = bp->b_bcount;
1162 			break;
1163 		case TMS_BSF:
1164 			mp->mscp_opcode = M_OP_REPOS;
1165 			mp->mscp_modifier = M_MD_REVRS;
1166 			mp->mscp_tmkcnt = bp->b_bcount;
1167 			break;
1168 		case TMS_FSR:
1169 			mp->mscp_opcode = M_OP_REPOS;
1170 			mp->mscp_modifier = M_MD_OBJCT;
1171 			mp->mscp_reccnt = bp->b_bcount;
1172 			break;
1173 		case TMS_BSR:
1174 			mp->mscp_opcode = M_OP_REPOS;
1175 			mp->mscp_modifier = M_MD_REVRS | M_MD_OBJCT;
1176 			mp->mscp_reccnt = bp->b_bcount;
1177 			break;
1178 		/*
1179 		 * Clear serious exception is done for Rewind & Available cmds
1180 		 */
1181 		case TMS_REW:
1182 			mp->mscp_opcode = M_OP_REPOS;
1183 			mp->mscp_modifier = M_MD_REWND | M_MD_CLSEX;
1184 			if (bp->b_bcount == 0)
1185 				mp->mscp_modifier |= M_MD_IMMED;
1186 			tms->tms_serex = 0;
1187 			break;
1188 		case TMS_OFFL:
1189 			mp->mscp_opcode = M_OP_AVAIL;
1190 			mp->mscp_modifier = M_MD_UNLOD | M_MD_CLSEX;
1191 			tms->tms_serex = 0;
1192 			break;
1193 		case TMS_SENSE:
1194 			mp->mscp_opcode = M_OP_GTUNT;
1195 			break;
1196 		case TMS_CACHE:
1197 			mp->mscp_opcode = M_OP_STUNT;
1198 			tms->tms_unitflgs |= M_UF_WBKNV;
1199 			mp->mscp_unitflgs = tms->tms_unitflgs;
1200 			mp->mscp_format = tms->tms_format;
1201 			/* default device dependant parameters */
1202 			mp->mscp_mediaid = 0;
1203 			break;
1204 		case TMS_NOCACHE:
1205 			mp->mscp_opcode = M_OP_STUNT;
1206 			tms->tms_unitflgs &= ~(M_UF_WBKNV);
1207 			mp->mscp_unitflgs = tms->tms_unitflgs;
1208 			mp->mscp_format = tms->tms_format;
1209 			/* default device dependant parameters */
1210 			mp->mscp_mediaid = 0;
1211 			break;
1212 		case TMS_CSE:
1213 			/*
1214 			 * This is a no-op command. It performs a
1215 			 * clear serious exception only.  (Done on a
1216 			 * non-rewinding close after a serious exception.)
1217 			 */
1218 			mp->mscp_opcode = M_OP_REPOS;
1219 			mp->mscp_modifier = M_MD_CLSEX;
1220 			tms->tms_serex = 0;
1221 			tms->tms_clserex = 1;
1222 			break;
1223 		case TMS_LOWDENSITY:
1224 			/*
1225 			 * Set the unit to low density
1226 			 */
1227 			mp->mscp_opcode = M_OP_STUNT;
1228 			mp->mscp_unitflgs = tms->tms_unitflgs;
1229 			mp->mscp_mediaid = 0;	/* default device dependant parameters */
1230 			if ((tms->tms_fmtmenu & M_TF_800) != 0)
1231 				mp->mscp_format = M_TF_800;
1232 			else
1233 				mp->mscp_format = M_TF_PE & tms->tms_fmtmenu;
1234 			tms->tms_format = mp->mscp_format;
1235 			break;
1236 		case TMS_HIDENSITY:
1237 			/*
1238 			 * Set the unit to high density (format == 0)
1239 			 */
1240 			mp->mscp_opcode = M_OP_STUNT;
1241 			mp->mscp_unitflgs = tms->tms_unitflgs;
1242 			mp->mscp_mediaid = 0;	/* default device dependant parameters */
1243 			mp->mscp_format = 0;
1244 			tms->tms_format = 0;
1245 			break;
1246 		default:
1247 			printf("Bad ioctl on tms unit %d\n", ui->ui_unit);
1248 			/* Need a no-op. Reposition no amount */
1249 			mp->mscp_opcode = M_OP_REPOS;
1250 			break;
1251 		}   /* end switch (bp->b_resid) */
1252 		}
1253 	else    /* Its a read/write command (not an ioctl) */
1254 		{
1255 		mp->mscp_opcode = bp->b_flags&B_READ ? M_OP_READ : M_OP_WRITE;
1256 		mp->mscp_bytecnt = bp->b_bcount;
1257 		mp->mscp_buffer = UBAI_ADDR(i) | (UBAI_BDP(i) << 24);
1258 
1259 		bp->b_ubinfo = tempi;			/* save mapping info */
1260 		}
1261 	if (tms->tms_serex == 2)			/* if tape mark read */
1262 		{
1263 		mp->mscp_modifier |= M_MD_CLSEX;	/*  clear serious exc */
1264 		tms->tms_serex = 0;
1265 		}
1266 	*((long *)mp->mscp_dscptr) |= TMSCP_OWN|TMSCP_INT;
1267 #	ifdef DEBUG
1268 	printd("tmscpstart: opcode 0%o mod %o unit %d cnt %d\n",mp->mscp_opcode,mp->mscp_modifier,mp->mscp_unit,mp->mscp_bytecnt);
1269 	if(tmscpdebug)DELAY(100000);
1270 #	endif
1271 	i = tmscpaddr->tmscpip;              /* initiate polling */
1272 	dp->b_qsize++;
1273 	/*
1274 	 * Move drive to the end of the controller queue
1275 	 */
1276 	if (dp->b_forw != NULL)
1277 		{
1278 		um->um_tab.b_actf = dp->b_forw;
1279 		um->um_tab.b_actl->b_forw = dp;
1280 		um->um_tab.b_actl = dp;
1281 		dp->b_forw = NULL;
1282 		}
1283 	/*
1284 	 * Move buffer to I/O wait queue
1285 	 */
1286 	dp->b_actf = bp->av_forw;
1287 	dp = &tmscpwtab[um->um_ctlr];
1288 	bp->av_forw = dp;
1289 	bp->av_back = dp->av_back;
1290 	dp->av_back->av_forw = bp;
1291 	dp->av_back = bp;
1292 	if (tmscpaddr->tmscpsa&TMSCP_ERR)
1293 		{
1294 		printf("tmscp%d: fatal error (0%o)\n", um->um_ctlr, tmscpaddr->tmscpsa&0xffff);
1295 		(void)tmscpinit(um->um_ctlr);
1296 		break;
1297 		}
1298     }   /* end for */
1299     /*
1300      * Check for response ring transitions lost in the
1301      * Race condition
1302      */
1303     for (i = sc->sc_lastrsp;; i++)
1304 	    {
1305 	    i %= NRSP;
1306 	    if (tm->tmscp_ca.ca_rspdsc[i]&TMSCP_OWN)
1307 		    break;
1308 	    tmscprsp(um, tm, sc, i);
1309 	    tm->tmscp_ca.ca_rspdsc[i] |= TMSCP_OWN;
1310 	    }
1311     sc->sc_lastrsp = i;
1312 }
1313 
1314 
1315 /*
1316  * Process a response packet
1317  */
1318 tmscprsp(um, tm, sc, i)
1319 	register struct uba_ctlr *um;
1320 	register struct tmscp *tm;
1321 	register struct tmscp_softc *sc;
1322 	int i;
1323 {
1324 	register struct mscp *mp;
1325 	register struct tms_info *tms;
1326 	struct uba_device *ui;
1327 	struct buf *dp, *bp;
1328 	int st;
1329 
1330 	mp = &tm->tmscp_rsp[i];
1331 	mp->mscp_header.tmscp_msglen = mscp_msglen;
1332 	sc->sc_credits += mp->mscp_header.tmscp_credits & 0xf;  /* low 4 bits */
1333 	if ((mp->mscp_header.tmscp_credits & 0xf0) > 0x10)	/* Check */
1334 		return;
1335 #	ifdef DEBUG
1336 	printd("tmscprsp, opcode 0%o status 0%o\n",mp->mscp_opcode,mp->mscp_status&M_ST_MASK);
1337 #	endif
1338 	/*
1339 	 * If it's an error log message (datagram),
1340 	 * pass it on for more extensive processing.
1341 	 */
1342 	if ((mp->mscp_header.tmscp_credits & 0xf0) == 0x10)
1343 		{	/* check */
1344 		tmserror(um, (struct mslg *)mp);
1345 		return;
1346 		}
1347 	st = mp->mscp_status&M_ST_MASK;
1348 	/*
1349 	 * The controller interrupts as drive 0.
1350 	 * This means that you must check for controller interrupts
1351 	 * before you check to see if there is a drive 0.
1352 	 */
1353 	if((M_OP_STCON|M_OP_END) == mp->mscp_opcode)
1354 		{
1355 		if (st == M_ST_SUCC)
1356 			{
1357 #		  	ifdef DEBUG
1358 			printd("ctlr has %d credits\n", mp->mscp_header.tmscp_credits & 0xf);
1359 			printd("ctlr timeout = %d\n", mp->mscp_cnttmo);
1360 #			endif
1361 			sc->sc_state = S_RUN;
1362 			}
1363 		else
1364 			sc->sc_state = S_IDLE;
1365 		um->um_tab.b_active = 0;
1366 		wakeup((caddr_t)um);
1367 		return;
1368 		}
1369 	if (mp->mscp_unit >= NTMS)
1370 		return;
1371 	if ((ui = tmscpip[um->um_ctlr][mp->mscp_unit]) == 0)
1372 		return;
1373 	tms = &tms_info[ui->ui_unit];
1374 	/*
1375 	 * Save endcode, endflags, and status for mtioctl get unit status.
1376 	 * NOTE: Don't do this on Clear serious exception (reposition no-op);
1377 	 *    which is done on close since this would
1378 	 *    overwrite the real status we want.
1379 	 */
1380 	if (tms->tms_clserex != 1)
1381 		{
1382 		tms->tms_endcode = mp->mscp_opcode;
1383 		tms->tms_flags = mp->mscp_flags;
1384 		tms->tms_status = st;
1385 		}
1386 	else tms->tms_clserex = 0;
1387 
1388 	switch (mp->mscp_opcode) {
1389 	case M_OP_ONLIN|M_OP_END:
1390 		tms->tms_type = mp->mscp_mediaid;
1391 		dp = &tmsutab[ui->ui_unit];
1392 		if (st == M_ST_SUCC)
1393 			{
1394 			/*
1395 			 * Link the drive onto the controller queue
1396 			 */
1397 			dp->b_forw = NULL;
1398 			if (um->um_tab.b_actf == NULL)
1399 				um->um_tab.b_actf = dp;
1400 			else
1401 				um->um_tab.b_actl->b_forw = dp;
1402 			um->um_tab.b_actl = dp;
1403 			ui->ui_flags = 1;       /* mark it online */
1404 			tms->tms_dsize=(daddr_t)mp->mscp_maxwrt;
1405 #			ifdef DEBUG
1406 			printd("tmscprsp: unit %d online\n", mp->mscp_unit);
1407 #			endif
1408 			/*
1409 			 * This define decodes the Media type identifier
1410 			 */
1411 #	        	define F_to_C(x,i)     ( ((x)->mscp_mediaid) >> (i*5+7) & 0x1f ? ( ( (((x)->mscp_mediaid) >>( i*5 + 7)) & 0x1f) + 'A' - 1): ' ')
1412 #			ifdef DEBUG
1413 			printd("tmscprsp: unit %d online %x %c%c %c%c%c%d\n"
1414 				,mp->mscp_unit, mp->mscp_mediaid ,F_to_C(mp,4)
1415 				,F_to_C(mp,3), F_to_C(mp,2)
1416 				,F_to_C(mp,1), F_to_C(mp,0), mp->mscp_mediaid & 0x7f);
1417 #			endif
1418 			dp->b_active = 1;
1419 			}	/* end if st == M_ST_SUCC */
1420 		else
1421 			{
1422 			if (bp = dp->b_actf)
1423 				tprintf(tms->tms_tpr,
1424 				    "tms%d: hard error bn%d: OFFLINE\n",
1425 				    minor(bp->b_dev)&03, bp->b_blkno);
1426 			else
1427 				tprintf(tms->tms_tpr,
1428 				    "tms%d: hard error: OFFLINE\n",
1429 				    ui->ui_unit);
1430 			while (bp = dp->b_actf)
1431 				{
1432 				dp->b_actf = bp->av_forw;
1433 				bp->b_flags |= B_ERROR;
1434 				iodone(bp);
1435 				}
1436 			}
1437 		if(mp->mscp_cmdref!=NULL)
1438 			/* Seems to get lost sometimes in uda */
1439 			wakeup((caddr_t)mp->mscp_cmdref);
1440 		break;
1441 	/*
1442 	 * The AVAILABLE ATTENTION message occurs when the
1443 	 * unit becomes available after loading,
1444 	 * marking the unit offline (ui_flags = 0) will force an
1445 	 * online command prior to using the unit.
1446 	 */
1447 	case M_OP_AVATN:
1448 		ui->ui_flags = 0;
1449 		tms->tms_type = mp->mscp_mediaid;
1450 		break;
1451 	case M_OP_END:
1452 		/*
1453 		 * An endcode without an opcode (0200) is an invalid command.
1454 		 * The mscp specification states that this would be a protocol
1455 		 * type error, such as illegal opcodes. The mscp spec. also
1456 		 * states that parameter error type of invalid commands should
1457 		 * return the normal end message for the command. This does not appear
1458 		 * to be the case. An invalid logical block number returned an endcode
1459 		 * of 0200 instead of the 0241 (read) that was expected.
1460 		 */
1461 
1462 		printf("tmscp%d: invalid cmd, endcode = %o, status=%o\n",
1463 			um->um_ctlr, mp->mscp_opcode, st);
1464 		bp = (struct buf *)mp->mscp_cmdref;
1465 		/*
1466 		 * Unlink buffer from I/O wait queue.
1467 		 * And signal iodone, so the higher level command can exit!
1468 		 *
1469 		 */
1470 		bp->av_back->av_forw = bp->av_forw;
1471 		bp->av_forw->av_back = bp->av_back;
1472 		dp = &tmsutab[ui->ui_unit];
1473 		dp->b_qsize--;
1474 		iodone(bp);
1475 		break;
1476 	case M_OP_WRITE|M_OP_END:
1477 		/* mark the last io op as a write */
1478 		tms->tms_lastiow = 1;
1479 	case M_OP_READ|M_OP_END:
1480 	case M_OP_WRITM|M_OP_END:
1481 	case M_OP_REPOS|M_OP_END:
1482 	case M_OP_STUNT|M_OP_END:
1483 	/*
1484 	 * The AVAILABLE message occurs when the mt ioctl "rewoffl" is
1485 	 * issued.  For the ioctl, "rewoffl", a tmscp AVAILABLE command is
1486 	 * done with the UNLOAD modifier.  This performs a rewind, followed
1487 	 * by marking the unit offline.  So mark the unit offline
1488 	 * software wise as well (ui_flags = 0 and
1489 	 * tms->tms_openf = 0).
1490 	 */
1491 	case M_OP_AVAIL|M_OP_END:
1492 #		ifdef DEBUG
1493 		printd("tmscprsp: position = %d\n", mp->mscp_lbn);
1494 #		endif
1495 		bp = (struct buf *)mp->mscp_cmdref;
1496 		/*
1497 		 * Only need to release buffer if the command was read or write.
1498 		 * No ubasetup was done in "tmscpstart" if it was an ioctl cmd.
1499 		 */
1500 		if (mp->mscp_opcode == (M_OP_READ|M_OP_END) ||
1501 		    mp->mscp_opcode == (M_OP_WRITE|M_OP_END))
1502 			ubarelse(um->um_ubanum, (int *)&bp->b_ubinfo);
1503 		/*
1504 		 * Unlink buffer from I/O wait queue.
1505 		 */
1506 		bp->av_back->av_forw = bp->av_forw;
1507 		bp->av_forw->av_back = bp->av_back;
1508 #		if defined(VAX750)
1509 		if (cpu == VAX_750) {
1510 		    if ((tmscpwtab[um->um_ctlr].av_forw == &tmscpwtab[um->um_ctlr]) &&
1511 					(um->um_ubinfo != 0)) {
1512 			ubarelse(um->um_ubanum, &um->um_ubinfo);
1513 		    }
1514 		    else {
1515 			if (mp->mscp_opcode == (M_OP_READ|M_OP_END) ||
1516 		    	    mp->mscp_opcode == (M_OP_WRITE|M_OP_END))
1517 				UBAPURGE(uba_hd[um->um_ubanum].uh_uba,(um->um_ubinfo >>28) & 0x0f);
1518 		    }
1519 		}
1520 #		endif
1521 		dp = &tmsutab[ui->ui_unit];
1522 		dp->b_qsize--;
1523 		if (st == M_ST_OFFLN || st == M_ST_AVLBL)
1524 			{
1525 			ui->ui_flags = 0;       /* mark unit offline */
1526 			tms->tms_openf = 0;
1527 			tms->tms_type = mp->mscp_mediaid;
1528 			/*
1529 			 * Link the buffer onto the front of the drive queue
1530 			 */
1531 			if ((bp->av_forw = dp->b_actf) == 0)
1532 				dp->b_actl = bp;
1533 			dp->b_actf = bp;
1534 			/*
1535 			 * Link the drive onto the controller queue
1536 			 */
1537 			if (dp->b_active == 0)
1538 				{
1539 				dp->b_forw = NULL;
1540 				if (um->um_tab.b_actf == NULL)
1541 					um->um_tab.b_actf = dp;
1542 				else
1543 					um->um_tab.b_actl->b_forw = dp;
1544 				um->um_tab.b_actl = dp;
1545 				dp->b_active = 1;
1546 				}
1547 #			if defined(VAX750)
1548 			if (cpu == VAX_750 && um->um_ubinfo == 0)
1549 				um->um_ubinfo = uballoc(um->um_ubanum, (caddr_t)0, 0, UBA_NEEDBDP);
1550 #			endif
1551 			return;
1552 			}
1553 		if (st != M_ST_SUCC)
1554 			{
1555 			if (mp->mscp_flags & M_EF_SEREX)
1556 				tms->tms_serex = 1;
1557 			if (st != M_ST_TAPEM)
1558 				{
1559 				tprintf(tms->tms_tpr,
1560 				    "tms%d: hard error bn%d\n",
1561 				    minor(bp->b_dev)&03, bp->b_blkno);
1562 				errinfo(st);		/* produces more info */
1563 #				ifdef DEBUG
1564 				printd("tmscprsp: error; status sub-code = 0%o, flags = 0%o\n",
1565 					(mp->mscp_status & 177740)>>5, mp->mscp_flags);
1566 #				endif
1567 				bp->b_flags |= B_ERROR;
1568 				}
1569 			else
1570 				/* Hit a tape mark - Set serex flag to
1571 				 * a special value so we can clear the
1572 				 * serious exception on the next command.
1573 				 */
1574 				tms->tms_serex = 2;
1575 			}
1576 		/*
1577 		 * The tmscp spec states that controllers do not have to
1578 		 * report the number of records or files skipped.  So on
1579 		 * reposition commands we go strictly by cmd status.
1580 		 */
1581 		if (mp->mscp_opcode != (M_OP_REPOS|M_OP_END))
1582 			bp->b_resid = bp->b_bcount - mp->mscp_bytecnt;
1583 		else
1584 			bp->b_resid = 0;
1585 		tms->tms_resid = bp->b_resid;
1586 		iodone(bp);
1587 		break;
1588 
1589 	case M_OP_GTUNT|M_OP_END:
1590 #		ifdef DEBUG
1591 		printd("tmscprsp: GTUNT end packet status = 0%o\n",st);
1592 		printd("tmscprsp: unit %d mediaid %x %c%c %c%c%c%d %x %x t=%d\n"
1593 		    ,mp->mscp_unit, mp->mscp_mediaid
1594 		    ,F_to_C(mp,4),F_to_C(mp,3),F_to_C(mp,2)
1595 		    ,F_to_C(mp,1),F_to_C(mp,0)
1596 		    ,mp->mscp_mediaid & 0x7f
1597 		    ,mp->mscp_unitid.val[0]
1598 		    ,mp->mscp_unitid.val[1]
1599 		    ,mp->mscp_format);
1600 #		endif
1601 		tms->tms_type = mp->mscp_mediaid;
1602 		tms->tms_fmtmenu = mp->mscp_fmtmenu;
1603 		tms->tms_unitflgs = mp->mscp_unitflgs;
1604 		break;
1605 
1606 	default:
1607 		printf("tmscp unknown packet\n");
1608 		tmserror(um, (struct mslg *)mp);
1609 	}	/* end switch mp->mscp_opcode */
1610 }
1611 
1612 
1613 /*
1614  * Give a meaningful error when the mscp_status field returns an error code.
1615  */
1616 
1617 errinfo(st)
1618 	int st;			/* the status code */
1619 {
1620 	switch(st) {
1621 	case M_ST_ICMD:
1622 		printf("invalid command\n");
1623 		break;
1624 	case M_ST_ABRTD:
1625 		printf("command aborted\n");
1626 		break;
1627 	case M_ST_OFFLN:
1628 		printf("unit offline\n");
1629 		break;
1630 	case M_ST_WRTPR:
1631 		printf("unit write protected\n");
1632 		break;
1633 	case M_ST_COMP:
1634 		printf("compare error\n");
1635 		break;
1636 	case M_ST_DATA:
1637 		printf("data error\n");
1638 		break;
1639 	case M_ST_HSTBF:
1640 		printf("host buffer access error\n");
1641 		break;
1642 	case M_ST_CNTLR:
1643 		printf("controller error\n");
1644 		break;
1645 	case M_ST_DRIVE:
1646 		printf("drive error\n");
1647 		break;
1648 	case M_ST_FMTER:
1649 		printf("formatter error\n");
1650 		break;
1651 	case M_ST_BOT:
1652 		printf("BOT encountered\n");
1653 		break;
1654 	case M_ST_TAPEM:
1655 		printf("tape mark encountered\n");
1656 		break;
1657 	case M_ST_RDTRN:
1658 		printf("record data truncated\n");
1659 		break;
1660 	case M_ST_PLOST:
1661 		printf("position lost\n");
1662 		break;
1663 	case M_ST_SEX:
1664 		printf("serious exception\n");
1665 		break;
1666 	case M_ST_LED:
1667 		printf("LEOT detected\n");
1668 		break;
1669 	}
1670 }
1671 
1672 
1673 /*
1674  * Manage buffers and perform block mode read and write operations.
1675  */
1676 
1677 tmscpstrategy (bp)
1678 	register struct buf *bp;
1679 {
1680 	register struct uba_device *ui;
1681 	register struct uba_ctlr *um;
1682 	register struct buf *dp;
1683 	register int unit = TMSUNIT(bp->b_dev);
1684 	int s;
1685 
1686 	if (unit >= NTMS)
1687 		{
1688 #		ifdef DEBUG
1689 		printd ("tmscpstrategy: bad unit # %d\n",unit);
1690 #		endif
1691 		bp->b_flags |= B_ERROR;
1692 		iodone(bp);
1693 		return;
1694 		}
1695 	ui = tmsdinfo[unit];
1696 	um = ui->ui_mi;
1697 	if (ui == 0 || ui->ui_alive == 0)
1698 		{
1699 		bp->b_flags |= B_ERROR;
1700 		iodone(bp);
1701 		return;
1702 		}
1703 	s = spl5();
1704 	/*
1705 	 * Link the buffer onto the drive queue
1706 	 */
1707 	dp = &tmsutab[ui->ui_unit];
1708 	if (dp->b_actf == 0)
1709 		dp->b_actf = bp;
1710 	else
1711 		dp->b_actl->av_forw = bp;
1712 	dp->b_actl = bp;
1713 	bp->av_forw = 0;
1714 	/*
1715 	 * Link the drive onto the controller queue
1716 	 */
1717 	if (dp->b_active == 0)
1718 		{
1719 		dp->b_forw = NULL;
1720 		if (um->um_tab.b_actf == NULL)
1721 			um->um_tab.b_actf = dp;
1722 		else
1723 			um->um_tab.b_actl->b_forw = dp;
1724 		um->um_tab.b_actl = dp;
1725 		dp->b_active = 1;
1726 		}
1727 	/*
1728 	 * If the controller is not active, start it.
1729 	 */
1730 	if (um->um_tab.b_active == 0)
1731 		{
1732 #		if defined(VAX750)
1733 		if (cpu == VAX_750
1734 				 && tmscpwtab[um->um_ctlr].av_forw == &tmscpwtab[um->um_ctlr])
1735 			{
1736 			if (um->um_ubinfo != 0)
1737 				log(TMS_PRI, "tmscpstrategy: ubinfo 0x%x\n",
1738 				    um->um_ubinfo);
1739 			else
1740 				um->um_ubinfo = uballoc(um->um_ubanum, (caddr_t)0, 0, UBA_NEEDBDP);
1741 			}
1742 #		endif
1743 #		ifdef DEBUG
1744 		printd10("tmscpstrategy: Controller not active, starting it\n");
1745 #		endif
1746 		(void) tmscpstart(um);
1747 		}
1748 	splx(s);
1749 	return;
1750 }
1751 
1752 #define DBSIZE 32
1753 
1754 #define ca_Rspdsc       ca_rspdsc[0]
1755 #define ca_Cmddsc       ca_rspdsc[1]
1756 #define tmscp_Rsp       tmscp_rsp[0]
1757 #define tmscp_Cmd       tmscp_cmd[0]
1758 
1759 struct  tmscp     tmscpd[NTMSCP];
1760 
1761 tmscpdump(dev)
1762 	dev_t dev;
1763 {
1764 	struct tmscpdevice *tmscpaddr;
1765 	struct tmscp *tmscp_ubaddr;
1766 	char *start;
1767 	int num, blk, unit;
1768 	register struct uba_regs *uba;
1769 	register struct uba_device *ui;
1770 	register struct tmscp *tmscpp;
1771 	register struct pte *io;
1772 	register int i;
1773 
1774 	unit = minor(dev) & 03;
1775 	if (unit >= NTMS)
1776 		return (ENXIO);
1777 #	define phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
1778 	ui = phys(struct uba_device *, tmsdinfo[unit]);
1779 	if (ui->ui_alive == 0)
1780 		return (ENXIO);
1781 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
1782 	ubainit(uba);
1783 	tmscpaddr = (struct tmscpdevice *)ui->ui_physaddr;
1784 	DELAY(2000000);
1785 	tmscpp = phys(struct tmscp *, &tmscpd[ui->ui_ctlr]);
1786 
1787 	num = btoc(sizeof(struct tmscp)) + 1;
1788 	io = &uba->uba_map[NUBMREG-num];
1789 	for(i = 0; i<num; i++)
1790 		*(int *)io++ = UBAMR_MRV|(btop(tmscpp)+i);
1791 	tmscp_ubaddr = (struct tmscp *)(((int)tmscpp & PGOFSET)|((NUBMREG-num)<<9));
1792 
1793 	tmscpaddr->tmscpip = 0;
1794 	while ((tmscpaddr->tmscpsa & TMSCP_STEP1) == 0)
1795 		if(tmscpaddr->tmscpsa & TMSCP_ERR) return(EFAULT);
1796 	tmscpaddr->tmscpsa = TMSCP_ERR;
1797 	while ((tmscpaddr->tmscpsa & TMSCP_STEP2) == 0)
1798 		if(tmscpaddr->tmscpsa & TMSCP_ERR) return(EFAULT);
1799 	tmscpaddr->tmscpsa = (short)&tmscp_ubaddr->tmscp_ca.ca_ringbase;
1800 	while ((tmscpaddr->tmscpsa & TMSCP_STEP3) == 0)
1801 		if(tmscpaddr->tmscpsa & TMSCP_ERR) return(EFAULT);
1802 	tmscpaddr->tmscpsa = (short)(((int)&tmscp_ubaddr->tmscp_ca.ca_ringbase) >> 16);
1803 	while ((tmscpaddr->tmscpsa & TMSCP_STEP4) == 0)
1804 		if(tmscpaddr->tmscpsa & TMSCP_ERR) return(EFAULT);
1805 	tmscpaddr->tmscpsa = TMSCP_GO;
1806 	tmscpp->tmscp_ca.ca_Rspdsc = (long)&tmscp_ubaddr->tmscp_Rsp.mscp_cmdref;
1807 	tmscpp->tmscp_ca.ca_Cmddsc = (long)&tmscp_ubaddr->tmscp_Cmd.mscp_cmdref;
1808 	tmscpp->tmscp_Cmd.mscp_header.tmscp_vcid = 1;	/* for tape */
1809 	tmscpp->tmscp_Cmd.mscp_cntflgs = 0;
1810 	tmscpp->tmscp_Cmd.mscp_version = 0;
1811 	if (tmscpcmd(M_OP_STCON, tmscpp, tmscpaddr) == 0) {
1812 		return(EFAULT);
1813 	}
1814 	tmscpp->tmscp_Cmd.mscp_unit = ui->ui_slave;
1815 	if (tmscpcmd(M_OP_ONLIN, tmscpp, tmscpaddr) == 0) {
1816 		return(EFAULT);
1817 	}
1818 
1819 	num = maxfree;
1820 	start = 0;
1821 	while (num > 0)
1822 		{
1823 		blk = num > DBSIZE ? DBSIZE : num;
1824 		io = uba->uba_map;
1825 		for (i = 0; i < blk; i++)
1826 			*(int *)io++ = (btop(start)+i) | UBAMR_MRV;
1827 		*(int *)io = 0;
1828 		tmscpp->tmscp_Cmd.mscp_lbn = btop(start);
1829 		tmscpp->tmscp_Cmd.mscp_unit = ui->ui_slave;
1830 		tmscpp->tmscp_Cmd.mscp_bytecnt = blk*NBPG;
1831 #		ifdef	MVAX
1832 		if( cpu == MVAX_I )
1833 			tmscpp->tmscp_Cmd.mscp_buffer = (long) start;
1834 		else
1835 #		endif 	MVAX
1836 			tmscpp->tmscp_Cmd.mscp_buffer = 0;
1837 		if (tmscpcmd(M_OP_WRITE, tmscpp, tmscpaddr) == 0)
1838 			return(EIO);
1839 		start += blk*NBPG;
1840 		num -= blk;
1841 		}
1842 	return (0);
1843 }
1844 
1845 
1846 /*
1847  * Perform a standalone tmscp command.  This routine is only used by tmscpdump.
1848  */
1849 
1850 tmscpcmd(op, tmscpp, tmscpaddr)
1851 	int op;
1852 	register struct tmscp *tmscpp;
1853 	struct tmscpdevice *tmscpaddr;
1854 {
1855 	int i;
1856 
1857 
1858 	tmscpp->tmscp_Cmd.mscp_opcode = op;
1859 	tmscpp->tmscp_Rsp.mscp_header.tmscp_msglen = mscp_msglen;
1860 	tmscpp->tmscp_Cmd.mscp_header.tmscp_msglen = mscp_msglen;
1861 	tmscpp->tmscp_ca.ca_Rspdsc |= TMSCP_OWN|TMSCP_INT;
1862 	tmscpp->tmscp_ca.ca_Cmddsc |= TMSCP_OWN|TMSCP_INT;
1863 	if (tmscpaddr->tmscpsa&TMSCP_ERR)
1864 		printf("tmscp fatal error (0%o)\n", tmscpaddr->tmscpsa&0xffff);
1865 	i = tmscpaddr->tmscpip;
1866 #ifdef lint
1867 	i = i;
1868 #endif
1869 	for (;;)
1870 		{
1871 		if (tmscpp->tmscp_ca.ca_cmdint)
1872 			tmscpp->tmscp_ca.ca_cmdint = 0;
1873 		if (tmscpp->tmscp_ca.ca_rspint)
1874 			break;
1875 		}
1876 	tmscpp->tmscp_ca.ca_rspint = 0;
1877 	if (tmscpp->tmscp_Rsp.mscp_opcode != (op|M_OP_END) ||
1878 	    (tmscpp->tmscp_Rsp.mscp_status&M_ST_MASK) != M_ST_SUCC)
1879 		{
1880 		printf("error: com %d opc 0x%x stat 0x%x\ndump ", op,
1881 			tmscpp->tmscp_Rsp.mscp_opcode, tmscpp->tmscp_Rsp.mscp_status);
1882 		return(0);
1883 		}
1884 	return(1);
1885 }
1886 
1887 /*
1888  * Catch ioctl commands, and call the "command" routine to do them.
1889  */
1890 
1891 /* ARGSUSED */
1892 tmscpioctl(dev, cmd, data, flag)
1893 	dev_t dev;
1894 	int cmd;
1895 	caddr_t data;
1896 	int flag;
1897 {
1898 	register struct buf *bp = &ctmscpbuf[TMSCPCTLR(dev)];
1899 	register callcount;	/* number of times to call cmd routine */
1900 	register struct uba_device *ui;
1901 	register struct tms_info *tms;
1902 	int fcount;		/* number of files (or records) to space */
1903 	int error = 0;
1904 	register struct mtop *mtop;	/* mag tape cmd op to perform */
1905 	register struct mtget *mtget;	/* mag tape struct to get info in */
1906 
1907 	/* we depend of the values and order of the TMS ioctl codes here */
1908 	static tmsops[] =
1909 	 {TMS_WRITM,TMS_FSF,TMS_BSF,TMS_FSR,TMS_BSR,TMS_REW,TMS_OFFL,TMS_SENSE,
1910 	  TMS_CACHE,TMS_NOCACHE};
1911 
1912 	switch (cmd) {
1913 	case MTIOCTOP:	/* tape operation */
1914 		mtop = (struct mtop *)data;
1915 		switch (mtop->mt_op) {
1916 
1917 		case MTWEOF:
1918 			callcount = mtop->mt_count;
1919 			fcount = 1;
1920 			break;
1921 		case MTFSF: case MTBSF:
1922 		case MTFSR: case MTBSR:
1923 			callcount = 1;
1924 			fcount = mtop->mt_count;
1925 			break;
1926 		case MTREW: case MTOFFL: case MTNOP:
1927 		case MTCACHE: case MTNOCACHE:
1928 			callcount = 1;
1929 			fcount = 1;		/* wait for this rewind */
1930 			break;
1931 		default:
1932 			return (ENXIO);
1933 		}	/* end switch mtop->mt_op */
1934 
1935 		if (callcount <= 0 || fcount <= 0)
1936 			return (EINVAL);
1937 		while (--callcount >= 0)
1938 			{
1939 			tmscpcommand(dev, tmsops[mtop->mt_op], fcount);
1940 			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
1941 			    bp->b_resid)
1942 				return (EIO);
1943 			if (bp->b_flags & B_ERROR)	/* like hitting BOT */
1944 				break;
1945 			}
1946 		if (bp->b_flags&B_ERROR)
1947 			if ((error = bp->b_error)==0)
1948 				return (EIO);
1949 		return (error);
1950 
1951 	case MTIOCGET:
1952 		/*
1953 		 * Return status info associated with the particular UNIT.
1954 		 */
1955 		ui = tmsdinfo[TMSUNIT(dev)];
1956 		tms = &tms_info[ui->ui_unit];
1957 		mtget = (struct mtget *)data;
1958 		mtget->mt_type = MT_ISTMSCP;
1959 		mtget->mt_dsreg = tms->tms_flags << 8;
1960 		mtget->mt_dsreg |= tms->tms_endcode;
1961 		mtget->mt_erreg = tms->tms_status;
1962 		mtget->mt_resid = tms->tms_resid;
1963 		break;
1964 
1965 	default:
1966 		return (ENXIO);
1967 	}
1968 	return (0);
1969 }
1970 
1971 
1972 /*
1973  * Reset (for raw mode use only).
1974  */
1975 
1976 tmscpreset (uban)
1977 	int uban;
1978 {
1979 	register struct uba_ctlr *um;
1980 	register struct uba_device *ui;
1981 	register struct buf *bp, *dp;
1982 	register int unit;
1983 	struct buf *nbp;
1984 	int d;
1985 
1986 	for (d = 0; d < NTMSCP; d++)
1987 		{
1988 		if ((um = tmscpminfo[d]) == 0 || um->um_ubanum != uban ||
1989 		    um->um_alive == 0)
1990 			continue;
1991 		printf(" tmscp%d", d);
1992 		um->um_tab.b_active = 0;
1993 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
1994 		tmscp_softc[d].sc_state = S_IDLE;
1995 		tmscp_softc[d].sc_mapped = 0;
1996 		for (unit = 0; unit < NTMS; unit++)
1997 			{
1998 			if ((ui = tmsdinfo[unit]) == 0)
1999 				continue;
2000 			if (ui->ui_alive == 0 || ui->ui_mi != um)
2001 				continue;
2002 			tmsutab[unit].b_active = 0;
2003 			tmsutab[unit].b_qsize = 0;
2004 			}
2005 		for (bp = tmscpwtab[d].av_forw; bp != &tmscpwtab[d]; bp = nbp)
2006 			{
2007 			nbp = bp->av_forw;
2008 			bp->b_ubinfo = 0;
2009 			/*
2010 			 * Link the buffer onto the drive queue
2011 			 */
2012 			dp = &tmsutab[TMSUNIT(bp->b_dev)];
2013 			if (dp->b_actf == 0)
2014 				dp->b_actf = bp;
2015 			else
2016 				dp->b_actl->av_forw = bp;
2017 			dp->b_actl = bp;
2018 			bp->av_forw = 0;
2019 			/*
2020 			 * Link the drive onto the controller queue
2021 			 */
2022 			if (dp->b_active == 0)
2023 				{
2024 				dp->b_forw = NULL;
2025 				if (um->um_tab.b_actf == NULL)
2026 					um->um_tab.b_actf = dp;
2027 				else
2028 					um->um_tab.b_actl->b_forw = dp;
2029 				um->um_tab.b_actl = dp;
2030 				dp->b_active = 1;
2031 				}
2032 			}
2033 		(void)tmscpinit(d);
2034 		}
2035 }
2036 
2037 
2038 /*
2039  * Process an error log message
2040  *
2041  * Only minimal decoding is done, only "useful"
2042  * information is printed.  Eventually should
2043  * send message to an error logger.
2044  */
2045 
2046 tmserror(um, mp)
2047 	register struct uba_ctlr *um;
2048 	register struct mslg *mp;
2049 {
2050 	register i;
2051 
2052 #	ifdef DEBUG
2053 	printd("tmserror:\n");
2054 #	endif
2055 	if(!(mp->mslg_flags & (M_LF_SUCC | M_LF_CONT)))
2056 		log(TMS_PRI, "tmscp%d: %s error, ", um->um_ctlr,
2057 		mp->mslg_flags & ( M_LF_SUCC | M_LF_CONT ) ? "soft" : "hard");
2058 
2059 	switch (mp->mslg_format) {
2060 
2061 	case M_FM_CNTERR:
2062 		log(TMS_PRI, "controller error, event 0%o\n", mp->mslg_event);
2063 		break;
2064 	case M_FM_BUSADDR:
2065 		log(TMS_PRI, "host memory access error, event 0%o, addr 0%o\n",
2066 			mp->mslg_event, mp->mslg_busaddr);
2067 		break;
2068 	case M_FM_TAPETRN:
2069 		log(TMS_PRI, "tape transfer error, unit %d, grp 0x%x, event 0%o\n",
2070 			mp->mslg_unit, mp->mslg_group, mp->mslg_event);
2071 		break;
2072 	case M_FM_STIERR:
2073 		log(TMS_PRI, "STI error, unit %d, event 0%o\n",
2074 			mp->mslg_unit, mp->mslg_event);
2075 #ifdef notdef
2076 		/* too painful to do with log() */
2077 		for(i = 0; i < 62;i++)
2078 			mprintf("\t0x%x",mp->mslg_stiunsucc[i] & 0xff);
2079 		mprintf("\n");
2080 #endif
2081 		break;
2082 	case M_FM_STIDEL:
2083 		log(TMS_PRI, "STI Drive Error Log, unit %d, event 0%o\n",
2084 			mp->mslg_unit, mp->mslg_event);
2085 		break;
2086 	case M_FM_STIFEL:
2087 		log(TMS_PRI, "STI Formatter Error Log, unit %d, event 0%o\n",
2088 			mp->mslg_unit, mp->mslg_event);
2089 		break;
2090 	default:
2091 		log(TMS_PRI, "unknown error, unit %d, format 0%o, event 0%o\n",
2092 			mp->mslg_unit, mp->mslg_format, mp->mslg_event);
2093 	}
2094 
2095 	if (tmscperror)
2096 		{
2097 		register long *p = (long *)mp;
2098 
2099 		for (i = 0; i < mp->mslg_header.tmscp_msglen; i += sizeof(*p))
2100 			printf("%x ", *p++);
2101 		printf("\n");
2102 		}
2103 }
2104 #endif
2105