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