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