xref: /csrg-svn/sys/vax/uba/uda.c (revision 38979)
123353Smckusick /*
235039Sbostic  * Copyright (c) 1988 Regents of the University of California.
335039Sbostic  * All rights reserved.
432523Sbostic  *
535039Sbostic  * This code is derived from software contributed to Berkeley by
635039Sbostic  * Chris Torek.
732523Sbostic  *
835039Sbostic  * Redistribution and use in source and binary forms are permitted
935039Sbostic  * provided that the above copyright notice and this paragraph are
1035039Sbostic  * duplicated in all such forms and that any documentation,
1135039Sbostic  * advertising materials, and other materials related to such
1235039Sbostic  * distribution and use acknowledge that the software was developed
1335039Sbostic  * by the University of California, Berkeley.  The name of the
1435039Sbostic  * University may not be used to endorse or promote products derived
1535039Sbostic  * from this software without specific prior written permission.
1635039Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1735039Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1835039Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1935039Sbostic  *
20*38979Skarels  *	@(#)uda.c	7.24 (Berkeley) 09/04/89
2123353Smckusick  */
2223353Smckusick 
2332523Sbostic /*
2432523Sbostic  * UDA50/MSCP device driver
2517553Skarels  */
2617553Skarels 
2732523Sbostic #define	POLLSTATS
2832523Sbostic 
2932523Sbostic /*
3032523Sbostic  * TODO
3132523Sbostic  *	write bad block forwarding code
3232523Sbostic  */
3332523Sbostic 
344743Swnj #include "ra.h"
3532523Sbostic 
3617642Skarels #if NUDA > 0
3732523Sbostic 
384743Swnj /*
3932523Sbostic  * CONFIGURATION OPTIONS.  The next three defines are tunable -- tune away!
404743Swnj  *
4132523Sbostic  * COMPAT_42 enables 4.2/4.3 compatibility (label mapping)
4232523Sbostic  *
4332523Sbostic  * NRSPL2 and NCMDL2 control the number of response and command
4432523Sbostic  * packets respectively.  They may be any value from 0 to 7, though
4532523Sbostic  * setting them higher than 5 is unlikely to be of any value.
4632523Sbostic  * If you get warnings about your command ring being too small,
4732523Sbostic  * try increasing the values by one.
4832523Sbostic  *
4932523Sbostic  * MAXUNIT controls the maximum unit number (number of drives per
5032523Sbostic  * controller) we are prepared to handle.
5132523Sbostic  *
5232523Sbostic  * DEFAULT_BURST must be at least 1.
534743Swnj  */
5432523Sbostic #define	COMPAT_42
5532523Sbostic 
5632523Sbostic #define	NRSPL2	5		/* log2 number of response packets */
5732523Sbostic #define NCMDL2	5		/* log2 number of command packets */
5832523Sbostic #define	MAXUNIT	8		/* maximum allowed unit number */
5932523Sbostic #define	DEFAULT_BURST	4	/* default DMA burst size */
6032523Sbostic 
6117553Skarels #include "param.h"
6217553Skarels #include "systm.h"
6317553Skarels #include "buf.h"
6417553Skarels #include "conf.h"
6517553Skarels #include "dir.h"
6630536Skarels #include "file.h"
6730536Skarels #include "ioctl.h"
6817553Skarels #include "user.h"
6917553Skarels #include "map.h"
7017553Skarels #include "vm.h"
7130536Skarels #include "dkstat.h"
7217553Skarels #include "cmap.h"
7330536Skarels #include "disklabel.h"
7430536Skarels #include "syslog.h"
7530773Skarels #include "stat.h"
764743Swnj 
7737512Smckusick #include "machine/pte.h"
7834283Skarels 
798482Sroot #include "../vax/cpu.h"
8017553Skarels #include "ubareg.h"
8117553Skarels #include "ubavar.h"
828613Sroot 
8332523Sbostic #define	NRSP	(1 << NRSPL2)
8432523Sbostic #define	NCMD	(1 << NCMDL2)
858613Sroot 
8632523Sbostic #include "udareg.h"
878482Sroot #include "../vax/mscp.h"
8832523Sbostic #include "../vax/mscpvar.h"
8932523Sbostic #include "../vax/mtpr.h"
904743Swnj 
9132523Sbostic /*
9232523Sbostic  * UDA communications area and MSCP packet pools, per controller.
9332523Sbostic  */
9432523Sbostic struct	uda {
9532523Sbostic 	struct	udaca uda_ca;		/* communications area */
9632523Sbostic 	struct	mscp uda_rsp[NRSP];	/* response packets */
9732523Sbostic 	struct	mscp uda_cmd[NCMD];	/* command packets */
984743Swnj } uda[NUDA];
994743Swnj 
10032523Sbostic /*
10132523Sbostic  * Software status, per controller.
10232523Sbostic  */
10332523Sbostic struct	uda_softc {
10432523Sbostic 	struct	uda *sc_uda;	/* Unibus address of uda struct */
10532523Sbostic 	short	sc_state;	/* UDA50 state; see below */
10632523Sbostic 	short	sc_flags;	/* flags; see below */
10732523Sbostic 	int	sc_micro;	/* microcode revision */
10832523Sbostic 	int	sc_ivec;	/* interrupt vector address */
10936036Skarels 	short	sc_ipl;		/* interrupt priority, Q-bus */
11032523Sbostic 	struct	mscp_info sc_mi;/* MSCP info (per mscpvar.h) */
11132523Sbostic #ifndef POLLSTATS
11232523Sbostic 	int	sc_wticks;	/* watchdog timer ticks */
11332523Sbostic #else
11432523Sbostic 	short	sc_wticks;
11532523Sbostic 	short	sc_ncmd;
11632523Sbostic #endif
11732523Sbostic } uda_softc[NUDA];
11824742Sbloom 
11932523Sbostic #ifdef POLLSTATS
12032523Sbostic struct udastats {
12132523Sbostic 	int	ncmd;
12232523Sbostic 	int	cmd[NCMD + 1];
12332523Sbostic } udastats = { NCMD + 1 };
12432523Sbostic #endif
12517553Skarels 
12632523Sbostic /*
12732523Sbostic  * Controller states
12832523Sbostic  */
12932523Sbostic #define	ST_IDLE		0	/* uninitialised */
13032523Sbostic #define	ST_STEP1	1	/* in `STEP 1' */
13132523Sbostic #define	ST_STEP2	2	/* in `STEP 2' */
13232523Sbostic #define	ST_STEP3	3	/* in `STEP 3' */
13332523Sbostic #define	ST_SETCHAR	4	/* in `Set Controller Characteristics' */
13432523Sbostic #define	ST_RUN		5	/* up and running */
1354743Swnj 
13632523Sbostic /*
13732523Sbostic  * Flags
13832523Sbostic  */
13932523Sbostic #define	SC_MAPPED	0x01	/* mapped in Unibus I/O space */
14032523Sbostic #define	SC_INSTART	0x02	/* inside udastart() */
14132523Sbostic #define	SC_GRIPED	0x04	/* griped about cmd ring too small */
14232523Sbostic #define	SC_INSLAVE	0x08	/* inside udaslave() */
14332523Sbostic #define	SC_DOWAKE	0x10	/* wakeup when ctlr init done */
14432523Sbostic #define	SC_STARTPOLL	0x20	/* need to initiate polling */
14512421Ssam 
14632523Sbostic /*
14732523Sbostic  * Device to unit number and partition and back
14832523Sbostic  */
14932523Sbostic #define	UNITSHIFT	3
15032523Sbostic #define	UNITMASK	7
15132523Sbostic #define	udaunit(dev)	(minor(dev) >> UNITSHIFT)
15232523Sbostic #define	udapart(dev)	(minor(dev) & UNITMASK)
15332523Sbostic #define	udaminor(u, p)	(((u) << UNITSHIFT) | (p))
1544743Swnj 
15517553Skarels /*
15632523Sbostic  * Drive status, per drive
15717553Skarels  */
15832523Sbostic struct ra_info {
15932523Sbostic 	daddr_t	ra_dsize;	/* size in sectors */
16033444Sbostic /*	u_long	ra_type;	/* drive type */
16132523Sbostic 	u_long	ra_mediaid;	/* media id */
16232523Sbostic 	int	ra_state;	/* open/closed state */
16332523Sbostic 	struct	ra_geom {	/* geometry information */
16432523Sbostic 		u_short	rg_nsectors;	/* sectors/track */
16532523Sbostic 		u_short	rg_ngroups;	/* track groups */
16632523Sbostic 		u_short	rg_ngpc;	/* groups/cylinder */
16732523Sbostic 		u_short	rg_ntracks;	/* ngroups*ngpc */
16832523Sbostic 		u_short	rg_ncyl;	/* ra_dsize/ntracks/nsectors */
16932523Sbostic #ifdef notyet
17032523Sbostic 		u_short	rg_rctsize;	/* size of rct */
17132523Sbostic 		u_short	rg_rbns;	/* replacement blocks per track */
17232523Sbostic 		u_short	rg_nrct;	/* number of rct copies */
17332523Sbostic #endif
17432523Sbostic 	} ra_geom;
17533443Skarels 	int	ra_wlabel;	/* label sector is currently writable */
17632523Sbostic 	u_long	ra_openpart;	/* partitions open */
17732523Sbostic 	u_long	ra_bopenpart;	/* block partitions open */
17832523Sbostic 	u_long	ra_copenpart;	/* character partitions open */
17932523Sbostic } ra_info[NRA];
18017553Skarels 
18130536Skarels /*
18230536Skarels  * Software state, per drive
18330536Skarels  */
18430536Skarels #define	CLOSED		0
18530536Skarels #define	WANTOPEN	1
18630536Skarels #define	RDLABEL		2
18730536Skarels #define	OPEN		3
18830536Skarels #define	OPENRAW		4
18917553Skarels 
19032523Sbostic /*
19132523Sbostic  * Definition of the driver for autoconf.
19232523Sbostic  */
19332523Sbostic int	udaprobe(), udaslave(), udaattach(), udadgo(), udaintr();
19432523Sbostic struct	uba_ctlr *udaminfo[NUDA];
19532523Sbostic struct	uba_device *udadinfo[NRA];
19632523Sbostic struct	disklabel udalabel[NRA];
19717553Skarels 
19832523Sbostic u_short	udastd[] = { 0772150, 0772550, 0777550, 0 };
19932523Sbostic struct	uba_driver udadriver =
20032523Sbostic  { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda",
20132523Sbostic    udaminfo };
20217553Skarels 
20332523Sbostic /*
20432523Sbostic  * More driver definitions, for generic MSCP code.
20532523Sbostic  */
20632523Sbostic int	udadgram(), udactlrdone(), udaunconf(), udaiodone();
20732523Sbostic int	udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb();
2084743Swnj 
20932523Sbostic struct	buf udautab[NRA];	/* per drive transfer queue */
2104743Swnj 
21132523Sbostic struct	mscp_driver udamscpdriver =
21234524Skarels  { MAXUNIT, NRA, UNITSHIFT, udautab, udalabel, udadinfo,
21332523Sbostic    udadgram, udactlrdone, udaunconf, udaiodone,
21432523Sbostic    udaonline, udagotstatus, udareplace, udaioerror, udabb,
21532523Sbostic    "uda", "ra" };
21632523Sbostic 
21732523Sbostic /*
21832523Sbostic  * Miscellaneous private variables.
21932523Sbostic  */
22032523Sbostic char	udasr_bits[] = UDASR_BITS;
22132523Sbostic 
22232523Sbostic struct	uba_device *udaip[NUDA][MAXUNIT];
22332523Sbostic 				/* inverting pointers: ctlr & unit => Unibus
22432523Sbostic 				   device pointer */
22532523Sbostic 
22632523Sbostic int	udaburst[NUDA] = { 0 };	/* burst size, per UDA50, zero => default;
22732523Sbostic 				   in data space so patchable via adb */
22832523Sbostic 
22932523Sbostic struct	mscp udaslavereply;	/* get unit status response packet, set
23032523Sbostic 				   for udaslave by udaunconf, via udaintr */
23132523Sbostic 
23232523Sbostic static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr
23332523Sbostic 				   info to slave routine; instead, we remember
23432523Sbostic 				   the last ctlr argument to probe */
23532523Sbostic 
23632523Sbostic int	udawstart, udawatch();	/* watchdog timer */
23732523Sbostic 
23832523Sbostic /*
23932523Sbostic  * Externals
24032523Sbostic  */
24132523Sbostic int	wakeup();
24232523Sbostic int	hz;
24332523Sbostic 
24432523Sbostic /*
24532523Sbostic  * Poke at a supposed UDA50 to see if it is there.
24632523Sbostic  * This routine duplicates some of the code in udainit() only
24732523Sbostic  * because autoconf has not set up the right information yet.
24832523Sbostic  * We have to do everything `by hand'.
24932523Sbostic  */
25032523Sbostic udaprobe(reg, ctlr, um)
2514743Swnj 	caddr_t reg;
2524743Swnj 	int ctlr;
25332523Sbostic 	struct uba_ctlr *um;
2544743Swnj {
2554743Swnj 	register int br, cvec;
25632523Sbostic 	register struct uda_softc *sc;
25732523Sbostic 	register struct udadevice *udaddr;
25832523Sbostic 	register struct mscp_info *mi;
25936036Skarels 	int timeout, tries, s;
2604743Swnj 
26132523Sbostic #ifdef VAX750
26232523Sbostic 	/*
26332523Sbostic 	 * The UDA50 wants to share BDPs on 750s, but not on 780s or
26432523Sbostic 	 * 8600s.  (730s have no BDPs anyway.)  Toward this end, we
26532523Sbostic 	 * here set the `keep bdp' flag in the per-driver information
26632523Sbostic 	 * if this is a 750.  (We just need to do it once, but it is
26732523Sbostic 	 * easiest to do it now, for each UDA50.)
26832523Sbostic 	 */
26932523Sbostic 	if (cpu == VAX_750)
27032523Sbostic 		udadriver.ud_keepbdp = 1;
27132523Sbostic #endif
27217553Skarels 
27332523Sbostic 	probeum = um;			/* remember for udaslave() */
2744743Swnj #ifdef lint
27532523Sbostic 	br = 0; cvec = br; br = cvec; udaintr(0);
2764743Swnj #endif
27732523Sbostic 	/*
27832523Sbostic 	 * Set up the controller-specific generic MSCP driver info.
27932523Sbostic 	 * Note that this should really be done in the (nonexistent)
28032523Sbostic 	 * controller attach routine.
28132523Sbostic 	 */
28232523Sbostic 	sc = &uda_softc[ctlr];
28332523Sbostic 	mi = &sc->sc_mi;
28432523Sbostic 	mi->mi_md = &udamscpdriver;
28532523Sbostic 	mi->mi_ctlr = um->um_ctlr;
28632523Sbostic 	mi->mi_tab = &um->um_tab;
28732523Sbostic 	mi->mi_ip = udaip[ctlr];
28832523Sbostic 	mi->mi_cmd.mri_size = NCMD;
28932523Sbostic 	mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc;
29032523Sbostic 	mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd;
29132523Sbostic 	mi->mi_rsp.mri_size = NRSP;
29232523Sbostic 	mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc;
29332523Sbostic 	mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp;
29432523Sbostic 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
29532523Sbostic 
29632523Sbostic 	/*
29732523Sbostic 	 * More controller specific variables.  Again, this should
29832523Sbostic 	 * be in the controller attach routine.
29932523Sbostic 	 */
30032523Sbostic 	if (udaburst[ctlr] == 0)
30132523Sbostic 		udaburst[ctlr] = DEFAULT_BURST;
30232523Sbostic 
30332523Sbostic 	/*
30432523Sbostic 	 * Get an interrupt vector.  Note that even if the controller
30532523Sbostic 	 * does not respond, we keep the vector.  This is not a serious
30632523Sbostic 	 * problem; but it would be easily fixed if we had a controller
30732523Sbostic 	 * attach routine.  Sigh.
30832523Sbostic 	 */
30932523Sbostic 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
31017553Skarels 	udaddr = (struct udadevice *) reg;
31117553Skarels 
31232523Sbostic 	/*
31332523Sbostic 	 * Initialise the controller (partially).  The UDA50 programmer's
31432523Sbostic 	 * manual states that if initialisation fails, it should be retried
31532523Sbostic 	 * at least once, but after a second failure the port should be
31632523Sbostic 	 * considered `down'; it also mentions that the controller should
31732523Sbostic 	 * initialise within ten seconds.  Or so I hear; I have not seen
31832523Sbostic 	 * this manual myself.
31932523Sbostic 	 */
32036036Skarels #ifdef QBA
32136036Skarels 	s = spl6();
32236036Skarels #endif
32332523Sbostic 	tries = 0;
32432523Sbostic again:
32532523Sbostic 	udaddr->udaip = 0;		/* start initialisation */
32632523Sbostic 	timeout = todr() + 1000;	/* timeout in 10 seconds */
32732523Sbostic 	while ((udaddr->udasa & UDA_STEP1) == 0)
32832523Sbostic 		if (todr() > timeout)
32932523Sbostic 			goto bad;
33032523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
33132523Sbostic 		(sc->sc_ivec >> 2);
33232523Sbostic 	while ((udaddr->udasa & UDA_STEP2) == 0)
33332523Sbostic 		if (todr() > timeout)
33432523Sbostic 			goto bad;
33532523Sbostic 
33632523Sbostic 	/* should have interrupted by now */
33735401Stef #ifdef QBA
33836036Skarels 	sc->sc_ipl = br = qbgetpri();
33927254Skridle #endif
34032523Sbostic 	return (sizeof (struct udadevice));
34132523Sbostic bad:
34232523Sbostic 	if (++tries < 2)
34332523Sbostic 		goto again;
34436036Skarels 	splx(s);
34532523Sbostic 	return (0);
3464743Swnj }
3474743Swnj 
34832523Sbostic /*
34932523Sbostic  * Find a slave.  We allow wildcard slave numbers (something autoconf
35032523Sbostic  * is not really prepared to deal with); and we need to know the
35132523Sbostic  * controller number to talk to the UDA.  For the latter, we keep
35232523Sbostic  * track of the last controller probed, since a controller probe
35332523Sbostic  * immediately precedes all slave probes for that controller.  For the
35432523Sbostic  * former, we simply put the unit number into ui->ui_slave after we
35532523Sbostic  * have found one.
35632523Sbostic  *
35732523Sbostic  * Note that by the time udaslave is called, the interrupt vector
35832523Sbostic  * for the UDA50 has been set up (so that udaunconf() will be called).
35932523Sbostic  */
36032523Sbostic udaslave(ui, reg)
36132523Sbostic 	register struct uba_device *ui;
3624743Swnj 	caddr_t reg;
3634743Swnj {
36432523Sbostic 	register struct uba_ctlr *um = probeum;
36532523Sbostic 	register struct mscp *mp;
36632523Sbostic 	register struct uda_softc *sc;
36733443Skarels 	int next = 0, timeout, tries, i;
36817553Skarels 
36932523Sbostic #ifdef lint
37032523Sbostic 	i = 0; i = i;
37132523Sbostic #endif
37232523Sbostic 	/*
37332523Sbostic 	 * Make sure the controller is fully initialised, by waiting
37432523Sbostic 	 * for it if necessary.
37532523Sbostic 	 */
37632523Sbostic 	sc = &uda_softc[um->um_ctlr];
37732523Sbostic 	if (sc->sc_state == ST_RUN)
37832523Sbostic 		goto findunit;
37932523Sbostic 	tries = 0;
38032523Sbostic again:
38132523Sbostic 	if (udainit(ui->ui_ctlr))
38232523Sbostic 		return (0);
38332523Sbostic 	timeout = todr() + 1000;		/* 10 seconds */
38432523Sbostic 	while (todr() < timeout)
38532523Sbostic 		if (sc->sc_state == ST_RUN)	/* made it */
38632523Sbostic 			goto findunit;
38732523Sbostic 	if (++tries < 2)
38832523Sbostic 		goto again;
38932523Sbostic 	printf("uda%d: controller hung\n", um->um_ctlr);
39032523Sbostic 	return (0);
39117553Skarels 
39232523Sbostic 	/*
39332523Sbostic 	 * The controller is all set; go find the unit.  Grab an
39432523Sbostic 	 * MSCP packet and send out a Get Unit Status command, with
39532523Sbostic 	 * the `next unit' modifier if we are looking for a generic
39632523Sbostic 	 * unit.  We set the `in slave' flag so that udaunconf()
39732523Sbostic 	 * knows to copy the response to `udaslavereply'.
39832523Sbostic 	 */
39932523Sbostic findunit:
40032523Sbostic 	udaslavereply.mscp_opcode = 0;
40132523Sbostic 	sc->sc_flags |= SC_INSLAVE;
40232523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL)
40332523Sbostic 		panic("udaslave");		/* `cannot happen' */
40432523Sbostic 	mp->mscp_opcode = M_OP_GETUNITST;
40532523Sbostic 	if (ui->ui_slave == '?') {
40632523Sbostic 		mp->mscp_unit = next;
40732523Sbostic 		mp->mscp_modifier = M_GUM_NEXTUNIT;
40832523Sbostic 	} else {
40932523Sbostic 		mp->mscp_unit = ui->ui_slave;
41032523Sbostic 		mp->mscp_modifier = 0;
41117553Skarels 	}
41232523Sbostic 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
41332523Sbostic 	i = ((struct udadevice *) reg)->udaip;	/* initiate polling */
41432523Sbostic 	mp = &udaslavereply;
41532523Sbostic 	timeout = todr() + 1000;
41632523Sbostic 	while (todr() < timeout)
41732523Sbostic 		if (mp->mscp_opcode)
41832523Sbostic 			goto gotit;
41932523Sbostic 	printf("uda%d: no response to Get Unit Status request\n",
42032523Sbostic 		um->um_ctlr);
42132523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
42232523Sbostic 	return (0);
42332523Sbostic 
42432523Sbostic gotit:
42532523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
42632523Sbostic 
42732523Sbostic 	/*
42832523Sbostic 	 * Got a slave response.  If the unit is there, use it.
42932523Sbostic 	 */
43032523Sbostic 	switch (mp->mscp_status & M_ST_MASK) {
43132523Sbostic 
43232523Sbostic 	case M_ST_SUCCESS:	/* worked */
43332523Sbostic 	case M_ST_AVAILABLE:	/* found another drive */
43432523Sbostic 		break;		/* use it */
43532523Sbostic 
43632523Sbostic 	case M_ST_OFFLINE:
43732523Sbostic 		/*
43832523Sbostic 		 * Figure out why it is off line.  It may be because
43932523Sbostic 		 * it is nonexistent, or because it is spun down, or
44032523Sbostic 		 * for some other reason.
44132523Sbostic 		 */
44232523Sbostic 		switch (mp->mscp_status & ~M_ST_MASK) {
44332523Sbostic 
44432523Sbostic 		case M_OFFLINE_UNKNOWN:
44532523Sbostic 			/*
44632523Sbostic 			 * No such drive, and there are none with
44732523Sbostic 			 * higher unit numbers either, if we are
44832523Sbostic 			 * using M_GUM_NEXTUNIT.
44932523Sbostic 			 */
45032523Sbostic 			return (0);
45132523Sbostic 
45232523Sbostic 		case M_OFFLINE_UNMOUNTED:
45332523Sbostic 			/*
45432523Sbostic 			 * The drive is not spun up.  Use it anyway.
45532523Sbostic 			 *
45632523Sbostic 			 * N.B.: this seems to be a common occurrance
45732523Sbostic 			 * after a power failure.  The first attempt
45832523Sbostic 			 * to bring it on line seems to spin it up
45932523Sbostic 			 * (and thus takes several minutes).  Perhaps
46032523Sbostic 			 * we should note here that the on-line may
46132523Sbostic 			 * take longer than usual.
46232523Sbostic 			 */
46332523Sbostic 			break;
46432523Sbostic 
46532523Sbostic 		default:
46632523Sbostic 			/*
46732523Sbostic 			 * In service, or something else equally unusable.
46832523Sbostic 			 */
46932523Sbostic 			printf("uda%d: unit %d off line: ", um->um_ctlr,
47032523Sbostic 				mp->mscp_unit);
47132523Sbostic 			mscp_printevent(mp);
47232523Sbostic 			goto try_another;
47332523Sbostic 		}
47432523Sbostic 		break;
47532523Sbostic 
47632523Sbostic 	default:
47732523Sbostic 		printf("uda%d: unable to get unit status: ", um->um_ctlr);
47832523Sbostic 		mscp_printevent(mp);
47932523Sbostic 		return (0);
48017553Skarels 	}
48132523Sbostic 
48232523Sbostic 	/*
48332523Sbostic 	 * Does this ever happen?  What (if anything) does it mean?
48432523Sbostic 	 */
48532523Sbostic 	if (mp->mscp_unit < next) {
48632523Sbostic 		printf("uda%d: unit %d, next %d\n",
48732523Sbostic 			um->um_ctlr, mp->mscp_unit, next);
48832523Sbostic 		return (0);
48917553Skarels 	}
49032523Sbostic 
49132523Sbostic 	if (mp->mscp_unit >= MAXUNIT) {
49232523Sbostic 		printf("uda%d: cannot handle unit number %d (max is %d)\n",
49332523Sbostic 			um->um_ctlr, mp->mscp_unit, MAXUNIT - 1);
49432523Sbostic 		return (0);
49532523Sbostic 	}
49632523Sbostic 
49732523Sbostic 	/*
49832523Sbostic 	 * See if we already handle this drive.
49932523Sbostic 	 * (Only likely if ui->ui_slave=='?'.)
50032523Sbostic 	 */
50132523Sbostic 	if (udaip[um->um_ctlr][mp->mscp_unit] != NULL) {
50232523Sbostic try_another:
50332523Sbostic 		if (ui->ui_slave != '?')
50432523Sbostic 			return (0);
50532523Sbostic 		next = mp->mscp_unit + 1;
50632523Sbostic 		goto findunit;
50732523Sbostic 	}
50832523Sbostic 
50932523Sbostic 	/*
51032523Sbostic 	 * Voila!
51132523Sbostic 	 */
51232523Sbostic 	uda_rasave(ui->ui_unit, mp, 0);
51332523Sbostic 	ui->ui_flags = 0;	/* not on line, nor anything else */
51432523Sbostic 	ui->ui_slave = mp->mscp_unit;
51532523Sbostic 	return (1);
5164743Swnj }
5174743Swnj 
51832523Sbostic /*
51932523Sbostic  * Attach a found slave.  Make sure the watchdog timer is running.
52038176Smckusick  * If this disk is being profiled, fill in the `wpms' value (used by
52132523Sbostic  * what?).  Set up the inverting pointer, and attempt to bring the
52232523Sbostic  * drive on line and read its label.
52332523Sbostic  */
52432523Sbostic udaattach(ui)
5254743Swnj 	register struct uba_device *ui;
5264743Swnj {
52732523Sbostic 	register int unit = ui->ui_unit;
52832523Sbostic 
52932523Sbostic 	if (udawstart == 0) {
53032523Sbostic 		timeout(udawatch, (caddr_t) 0, hz);
53132523Sbostic 		udawstart++;
53232523Sbostic 	}
53333444Sbostic 
53433444Sbostic 	/*
53533444Sbostic 	 * Floppies cannot be brought on line unless there is
53633444Sbostic 	 * a disk in the drive.  Since an ONLINE while cold
53733444Sbostic 	 * takes ten seconds to fail, and (when notyet becomes now)
53833444Sbostic 	 * no sensible person will swap to one, we just
53933444Sbostic 	 * defer the ONLINE until someone tries to use the drive.
54033444Sbostic 	 *
54133444Sbostic 	 * THIS ASSUMES THAT DRIVE TYPES ?X? ARE FLOPPIES
54233444Sbostic 	 */
54333444Sbostic 	if (MSCP_MID_ECH(1, ra_info[unit].ra_mediaid) == 'X' - '@') {
54434283Skarels 		printf(": floppy");
54533444Sbostic 		return;
54633444Sbostic 	}
54734649Skarels 	if (ui->ui_dk >= 0)
54838176Smckusick 		dk_wpms[ui->ui_dk] = (60 * 31 * 256);	/* approx */
54932523Sbostic 	udaip[ui->ui_ctlr][ui->ui_slave] = ui;
55033195Sbostic 
55132523Sbostic 	if (uda_rainit(ui, 0))
55234283Skarels 		printf(": offline");
55336036Skarels 	else if (ra_info[unit].ra_state == OPEN) {
55434283Skarels 		printf(": %s, size = %d sectors",
55534283Skarels 		    udalabel[unit].d_typename, ra_info[unit].ra_dsize);
55632523Sbostic #ifdef notyet
55732523Sbostic 		addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]);
55826295Skarels #endif
55930536Skarels 	}
56032523Sbostic }
56132523Sbostic 
56232523Sbostic /*
56332523Sbostic  * Initialise a UDA50.  Return true iff something goes wrong.
56432523Sbostic  */
56532523Sbostic udainit(ctlr)
56632523Sbostic 	int ctlr;
56732523Sbostic {
56832523Sbostic 	register struct uda_softc *sc;
56932523Sbostic 	register struct udadevice *udaddr;
57032523Sbostic 	struct uba_ctlr *um;
57132523Sbostic 	int timo, ubinfo;
57232523Sbostic 
57332523Sbostic 	sc = &uda_softc[ctlr];
57432523Sbostic 	um = udaminfo[ctlr];
57532523Sbostic 	if ((sc->sc_flags & SC_MAPPED) == 0) {
57632523Sbostic 		/*
57732523Sbostic 		 * Map the communication area and command and
57832523Sbostic 		 * response packets into Unibus space.
57932523Sbostic 		 */
58032523Sbostic 		ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr],
58132523Sbostic 			sizeof (struct uda), UBA_CANTWAIT);
58232523Sbostic 		if (ubinfo == 0) {
58332523Sbostic 			printf("uda%d: uballoc map failed\n", ctlr);
58432523Sbostic 			return (-1);
58532523Sbostic 		}
58636036Skarels 		sc->sc_uda = (struct uda *) UBAI_ADDR(ubinfo);
58732523Sbostic 		sc->sc_flags |= SC_MAPPED;
58832523Sbostic 	}
58932523Sbostic 
59030536Skarels 	/*
59132523Sbostic 	 * While we are thinking about it, reset the next command
59232523Sbostic 	 * and response indicies.
59330536Skarels 	 */
59432523Sbostic 	sc->sc_mi.mi_cmd.mri_next = 0;
59532523Sbostic 	sc->sc_mi.mi_rsp.mri_next = 0;
59632523Sbostic 
59732523Sbostic 	/*
59832523Sbostic 	 * Start up the hardware initialisation sequence.
59932523Sbostic 	 */
60032523Sbostic #define	STEP0MASK	(UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \
60132523Sbostic 			 UDA_STEP1 | UDA_NV)
60232523Sbostic 
60332523Sbostic 	sc->sc_state = ST_IDLE;	/* in case init fails */
60433444Sbostic 	udaddr = (struct udadevice *)um->um_addr;
60532523Sbostic 	udaddr->udaip = 0;
60632523Sbostic 	timo = todr() + 1000;
60732523Sbostic 	while ((udaddr->udasa & STEP0MASK) == 0) {
60832523Sbostic 		if (todr() > timo) {
60932523Sbostic 			printf("uda%d: timeout during init\n", ctlr);
61032523Sbostic 			return (-1);
61132523Sbostic 		}
61232523Sbostic 	}
61332523Sbostic 	if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) {
61432523Sbostic 		printf("uda%d: init failed, sa=%b\n", ctlr,
61532523Sbostic 			udaddr->udasa, udasr_bits);
61633444Sbostic 		udasaerror(um, 0);
61732523Sbostic 		return (-1);
61832523Sbostic 	}
61932523Sbostic 
62032523Sbostic 	/*
62132523Sbostic 	 * Success!  Record new state, and start step 1 initialisation.
62232523Sbostic 	 * The rest is done in the interrupt handler.
62332523Sbostic 	 */
62432523Sbostic 	sc->sc_state = ST_STEP1;
62532523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
62632523Sbostic 	    (sc->sc_ivec >> 2);
62732523Sbostic 	return (0);
6284743Swnj }
6294743Swnj 
6304743Swnj /*
63132523Sbostic  * Open a drive.
6324743Swnj  */
63332523Sbostic /*ARGSUSED*/
63432523Sbostic udaopen(dev, flag, fmt)
6354743Swnj 	dev_t dev;
63630773Skarels 	int flag, fmt;
6374743Swnj {
63832523Sbostic 	register int unit;
6394743Swnj 	register struct uba_device *ui;
6404743Swnj 	register struct uda_softc *sc;
64130536Skarels 	register struct disklabel *lp;
64230536Skarels 	register struct partition *pp;
64330916Skarels 	register struct ra_info *ra;
64432523Sbostic 	int s, i, part, mask, error = 0;
64530536Skarels 	daddr_t start, end;
64630536Skarels 
64732523Sbostic 	/*
64832523Sbostic 	 * Make sure this is a reasonable open request.
64932523Sbostic 	 */
65032523Sbostic 	unit = udaunit(dev);
65132523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0)
6528576Sroot 		return (ENXIO);
65332523Sbostic 
65432523Sbostic 	/*
65532523Sbostic 	 * Make sure the controller is running, by (re)initialising it if
65632523Sbostic 	 * necessary.
65732523Sbostic 	 */
6584743Swnj 	sc = &uda_softc[ui->ui_ctlr];
6595434Sroot 	s = spl5();
66032523Sbostic 	if (sc->sc_state != ST_RUN) {
66132523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) {
66232523Sbostic 			splx(s);
6638576Sroot 			return (EIO);
66417553Skarels 		}
66532523Sbostic 		/*
66632523Sbostic 		 * In case it does not come up, make sure we will be
66732523Sbostic 		 * restarted in 10 seconds.  This corresponds to the
66832523Sbostic 		 * 10 second timeouts in udaprobe() and udaslave().
66932523Sbostic 		 */
67032523Sbostic 		sc->sc_flags |= SC_DOWAKE;
67132523Sbostic 		timeout(wakeup, (caddr_t) sc, 10 * hz);
67232523Sbostic 		sleep((caddr_t) sc, PRIBIO);
67332523Sbostic 		if (sc->sc_state != ST_RUN) {
67432523Sbostic 			splx(s);
67532523Sbostic 			printf("uda%d: controller hung\n", ui->ui_ctlr);
67632523Sbostic 			return (EIO);
67732523Sbostic 		}
67832523Sbostic 		untimeout(wakeup, (caddr_t) sc);
6794743Swnj 	}
68032523Sbostic 
68132523Sbostic 	/*
68232523Sbostic 	 * Wait for the state to settle
68332523Sbostic 	 */
68432523Sbostic 	ra = &ra_info[unit];
68532523Sbostic 	while (ra->ra_state != OPEN && ra->ra_state != OPENRAW &&
68632523Sbostic 	    ra->ra_state != CLOSED)
68732523Sbostic 		sleep((caddr_t)ra, PZERO + 1);
68832523Sbostic 
68932523Sbostic 	/*
69032523Sbostic 	 * If not on line, or we are not sure of the label, reinitialise
69132523Sbostic 	 * the drive.
69232523Sbostic 	 */
69332523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0 ||
69432523Sbostic 	    (ra->ra_state != OPEN && ra->ra_state != OPENRAW))
69532523Sbostic 		error = uda_rainit(ui, flag);
69630916Skarels 	splx(s);
69732523Sbostic 	if (error)
69832523Sbostic 		return (error);
69930536Skarels 
70032523Sbostic 	part = udapart(dev);
70132523Sbostic 	lp = &udalabel[unit];
70230536Skarels 	if (part >= lp->d_npartitions)
70330536Skarels 		return (ENXIO);
70430536Skarels 	/*
70532523Sbostic 	 * Warn if a partition is opened that overlaps another
70632523Sbostic 	 * already open, unless either is the `raw' partition
70732523Sbostic 	 * (whole disk).
70830536Skarels 	 */
70932523Sbostic #define	RAWPART		2	/* 'c' partition */	/* XXX */
71032523Sbostic 	mask = 1 << part;
71132523Sbostic 	if ((ra->ra_openpart & mask) == 0 && part != RAWPART) {
71230536Skarels 		pp = &lp->d_partitions[part];
71330536Skarels 		start = pp->p_offset;
71430536Skarels 		end = pp->p_offset + pp->p_size;
71532523Sbostic 		for (pp = lp->d_partitions, i = 0;
71632523Sbostic 		     i < lp->d_npartitions; pp++, i++) {
71730536Skarels 			if (pp->p_offset + pp->p_size <= start ||
71832523Sbostic 			    pp->p_offset >= end || i == RAWPART)
71930536Skarels 				continue;
72032523Sbostic 			if (ra->ra_openpart & (1 << i))
72130536Skarels 				log(LOG_WARNING,
72230536Skarels 				    "ra%d%c: overlaps open partition (%c)\n",
72332523Sbostic 				    unit, part + 'a', i + 'a');
72417553Skarels 		}
72517553Skarels 	}
72630773Skarels 	switch (fmt) {
72730773Skarels 	case S_IFCHR:
72832523Sbostic 		ra->ra_copenpart |= mask;
72930773Skarels 		break;
73030773Skarels 	case S_IFBLK:
73132523Sbostic 		ra->ra_bopenpart |= mask;
73230773Skarels 		break;
73330773Skarels 	}
73432523Sbostic 	ra->ra_openpart |= mask;
7358576Sroot 	return (0);
7364743Swnj }
7374743Swnj 
73833444Sbostic /* ARGSUSED */
73932523Sbostic udaclose(dev, flags, fmt)
74030536Skarels 	dev_t dev;
74130773Skarels 	int flags, fmt;
74230536Skarels {
74332523Sbostic 	register int unit = udaunit(dev);
74430773Skarels 	register struct ra_info *ra = &ra_info[unit];
74532523Sbostic 	int s, mask = (1 << udapart(dev));
74630536Skarels 
74730773Skarels 	switch (fmt) {
74830773Skarels 	case S_IFCHR:
74932523Sbostic 		ra->ra_copenpart &= ~mask;
75030773Skarels 		break;
75130773Skarels 	case S_IFBLK:
75232523Sbostic 		ra->ra_bopenpart &= ~mask;
75330773Skarels 		break;
75430773Skarels 	}
75532523Sbostic 	ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
75632523Sbostic 
75730536Skarels 	/*
75832523Sbostic 	 * Should wait for I/O to complete on this partition even if
75932523Sbostic 	 * others are open, but wait for work on blkflush().
76030536Skarels 	 */
76132523Sbostic 	if (ra->ra_openpart == 0) {
76230536Skarels 		s = spl5();
76332523Sbostic 		while (udautab[unit].b_actf)
76432523Sbostic 			sleep((caddr_t)&udautab[unit], PZERO - 1);
76530536Skarels 		splx(s);
76632523Sbostic 		ra->ra_state = CLOSED;
76733443Skarels 		ra->ra_wlabel = 0;
76830536Skarels 	}
76930773Skarels 	return (0);
77030536Skarels }
77130536Skarels 
7724743Swnj /*
77332523Sbostic  * Initialise a drive.  If it is not already, bring it on line,
77432523Sbostic  * and set a timeout on it in case it fails to respond.
77532523Sbostic  * When on line, read in the pack label.
7764743Swnj  */
77732523Sbostic uda_rainit(ui, flags)
77830536Skarels 	register struct uba_device *ui;
77932523Sbostic 	int flags;
78030536Skarels {
78132523Sbostic 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
78234283Skarels 	register struct disklabel *lp;
78330536Skarels 	register struct mscp *mp;
78432523Sbostic 	register int unit = ui->ui_unit;
78532523Sbostic 	register struct ra_info *ra;
78630773Skarels 	char *msg, *readdisklabel();
78732523Sbostic 	int s, i, udastrategy();
78830536Skarels 	extern int cold;
78930536Skarels 
79032523Sbostic 	ra = &ra_info[unit];
79132523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
79232523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT);
79332523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
79432523Sbostic 		mp->mscp_unit = ui->ui_slave;
79532523Sbostic 		mp->mscp_cmdref = (long)&ui->ui_flags;
79632523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
79732523Sbostic 		ra->ra_state = WANTOPEN;
79832523Sbostic 		if (!cold)
79932523Sbostic 			s = spl5();
80032523Sbostic 		i = ((struct udadevice *)ui->ui_addr)->udaip;
80130536Skarels 
80230916Skarels 		if (cold) {
80332523Sbostic 			i = todr() + 1000;
80432523Sbostic 			while ((ui->ui_flags & UNIT_ONLINE) == 0)
80532523Sbostic 				if (todr() > i)
80632523Sbostic 					break;
80730916Skarels 		} else {
80832523Sbostic 			timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
80932523Sbostic 			sleep((caddr_t)&ui->ui_flags, PSWP + 1);
81032523Sbostic 			splx(s);
81132523Sbostic 			untimeout(wakeup, (caddr_t)&ui->ui_flags);
81230916Skarels 		}
81332523Sbostic 		if (ra->ra_state != OPENRAW) {
81432523Sbostic 			ra->ra_state = CLOSED;
81532523Sbostic 			wakeup((caddr_t)ra);
81630916Skarels 			return (EIO);
81730916Skarels 		}
81830536Skarels 	}
81930536Skarels 
82032523Sbostic 	lp = &udalabel[unit];
82130916Skarels 	lp->d_secsize = DEV_BSIZE;
82232523Sbostic 	lp->d_secperunit = ra->ra_dsize;
82330916Skarels 
82430536Skarels 	if (flags & O_NDELAY)
82530536Skarels 		return (0);
82632523Sbostic 	ra->ra_state = RDLABEL;
82730536Skarels 	/*
82832523Sbostic 	 * Set up default sizes until we have the label, or longer
82932523Sbostic 	 * if there is none.  Set secpercyl, as readdisklabel wants
830*38979Skarels 	 * to compute b_cylin (although we do not need it), and set
831*38979Skarels 	 * nsectors in case diskerr is called.
83230536Skarels 	 */
83330916Skarels 	lp->d_secpercyl = 1;
83430536Skarels 	lp->d_npartitions = 1;
83536036Skarels 	lp->d_secsize = 512;
83636036Skarels 	lp->d_secperunit = ra->ra_dsize;
837*38979Skarels 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
83830536Skarels 	lp->d_partitions[0].p_size = lp->d_secperunit;
83930536Skarels 	lp->d_partitions[0].p_offset = 0;
84032523Sbostic 
84130536Skarels 	/*
84230536Skarels 	 * Read pack label.
84330536Skarels 	 */
84432523Sbostic 	if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) {
84534283Skarels 		if (cold)
84634283Skarels 			printf(": %s", msg);
84734283Skarels 		else
84836036Skarels 			log(LOG_ERR, "ra%d: %s", unit, msg);
84930536Skarels #ifdef COMPAT_42
85032523Sbostic 		if (udamaptype(unit, lp))
85132523Sbostic 			ra->ra_state = OPEN;
85230536Skarels 		else
85332523Sbostic 			ra->ra_state = OPENRAW;
85431022Skarels #else
85532523Sbostic 		ra->ra_state = OPENRAW;
85636036Skarels 		uda_makefakelabel(ra, lp);
85730536Skarels #endif
85830773Skarels 	} else
85932523Sbostic 		ra->ra_state = OPEN;
86030916Skarels 	wakeup((caddr_t)ra);
86130916Skarels 	return (0);
86230536Skarels }
86330536Skarels 
86432523Sbostic /*
86532523Sbostic  * Copy the geometry information for the given ra from a
86632523Sbostic  * GET UNIT STATUS response.  If check, see if it changed.
86732523Sbostic  */
86832523Sbostic uda_rasave(unit, mp, check)
86932523Sbostic 	int unit;
87032523Sbostic 	register struct mscp *mp;
87132523Sbostic 	int check;
87232523Sbostic {
87332523Sbostic 	register struct ra_info *ra = &ra_info[unit];
87432523Sbostic 
87534283Skarels 	if (check && ra->ra_mediaid != mp->mscp_guse.guse_mediaid) {
87633443Skarels 		printf("ra%d: changed types! was %d now %d\n", unit,
87734283Skarels 			ra->ra_mediaid, mp->mscp_guse.guse_mediaid);
87832523Sbostic 		ra->ra_state = CLOSED;	/* ??? */
87932523Sbostic 	}
88033444Sbostic 	/* ra->ra_type = mp->mscp_guse.guse_drivetype; */
88132523Sbostic 	ra->ra_mediaid = mp->mscp_guse.guse_mediaid;
88232523Sbostic 	ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt;
88332523Sbostic 	ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group;
88432523Sbostic 	ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc;
88532523Sbostic 	ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc;
88632523Sbostic 	/* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */
88732523Sbostic #ifdef notyet
88832523Sbostic 	ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize;
88932523Sbostic 	ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt;
89032523Sbostic 	ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct;
89132523Sbostic #endif
89232523Sbostic }
89332523Sbostic 
89432523Sbostic /*
89532523Sbostic  * Queue a transfer request, and if possible, hand it to the controller.
89632523Sbostic  *
89732523Sbostic  * This routine is broken into two so that the internal version
89832523Sbostic  * udastrat1() can be called by the (nonexistent, as yet) bad block
89932523Sbostic  * revectoring routine.
90032523Sbostic  */
90132523Sbostic udastrategy(bp)
9024743Swnj 	register struct buf *bp;
9034743Swnj {
90432523Sbostic 	register int unit;
9054743Swnj 	register struct uba_device *ui;
90632523Sbostic 	register struct ra_info *ra;
90732523Sbostic 	struct partition *pp;
90832523Sbostic 	int p;
9094743Swnj 	daddr_t sz, maxsz;
9104743Swnj 
91132523Sbostic 	/*
91232523Sbostic 	 * Make sure this is a reasonable drive to use.
91332523Sbostic 	 */
91432523Sbostic 	if ((unit = udaunit(bp->b_dev)) >= NRA ||
91532523Sbostic 	    (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 ||
91632523Sbostic 	    (ra = &ra_info[unit])->ra_state == CLOSED) {
91724742Sbloom 		bp->b_error = ENXIO;
9184743Swnj 		goto bad;
91924742Sbloom 	}
92032523Sbostic 
92132523Sbostic 	/*
92232523Sbostic 	 * If drive is open `raw' or reading label, let it at it.
92332523Sbostic 	 */
92432523Sbostic 	if (ra->ra_state < OPEN) {
92532523Sbostic 		udastrat1(bp);
92632523Sbostic 		return;
92724742Sbloom 	}
92832523Sbostic 	p = udapart(bp->b_dev);
92933443Skarels 	if ((ra->ra_openpart & (1 << p)) == 0) {
93033443Skarels 		bp->b_error = ENODEV;
93133443Skarels 		goto bad;
93233443Skarels 	}
93332523Sbostic 
93432523Sbostic 	/*
93532523Sbostic 	 * Determine the size of the transfer, and make sure it is
93632523Sbostic 	 * within the boundaries of the partition.
93732523Sbostic 	 */
93832523Sbostic 	pp = &udalabel[unit].d_partitions[p];
93932523Sbostic 	maxsz = pp->p_size;
94032523Sbostic 	if (pp->p_offset + pp->p_size > ra->ra_dsize)
94132523Sbostic 		maxsz = ra->ra_dsize - pp->p_offset;
94230536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
94333443Skarels 	if (bp->b_blkno + pp->p_offset <= LABELSECTOR &&
94433443Skarels #if LABELSECTOR != 0
94533443Skarels 	    bp->b_blkno + pp->p_offset + sz > LABELSECTOR &&
94633443Skarels #endif
94733443Skarels 	    (bp->b_flags & B_READ) == 0 && ra->ra_wlabel == 0) {
94833443Skarels 		bp->b_error = EROFS;
94933443Skarels 		goto bad;
95033443Skarels 	}
95130536Skarels 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
95232523Sbostic 		/* if exactly at end of disk, return an EOF */
95324787Skarels 		if (bp->b_blkno == maxsz) {
95424787Skarels 			bp->b_resid = bp->b_bcount;
95532523Sbostic 			biodone(bp);
95632523Sbostic 			return;
95724787Skarels 		}
95832523Sbostic 		/* or truncate if part of it fits */
95930536Skarels 		sz = maxsz - bp->b_blkno;
96030536Skarels 		if (sz <= 0) {
96132523Sbostic 			bp->b_error = EINVAL;	/* or hang it up */
96230536Skarels 			goto bad;
96330536Skarels 		}
96430536Skarels 		bp->b_bcount = sz << DEV_BSHIFT;
96524742Sbloom 	}
96632523Sbostic 	udastrat1(bp);
96732523Sbostic 	return;
96832523Sbostic bad:
96932523Sbostic 	bp->b_flags |= B_ERROR;
97032523Sbostic 	biodone(bp);
97132523Sbostic }
97232523Sbostic 
97332523Sbostic /*
97432523Sbostic  * Work routine for udastrategy.
97532523Sbostic  */
97632523Sbostic udastrat1(bp)
97732523Sbostic 	register struct buf *bp;
97832523Sbostic {
97932523Sbostic 	register int unit = udaunit(bp->b_dev);
98032523Sbostic 	register struct uba_ctlr *um;
98132523Sbostic 	register struct buf *dp;
98232523Sbostic 	struct uba_device *ui;
98332523Sbostic 	int s = spl5();
98432523Sbostic 
9854743Swnj 	/*
98632523Sbostic 	 * Append the buffer to the drive queue, and if it is not
98732523Sbostic 	 * already there, the drive to the controller queue.  (However,
98832523Sbostic 	 * if the drive queue is marked to be requeued, we must be
98932523Sbostic 	 * awaiting an on line or get unit status command; in this
99032523Sbostic 	 * case, leave it off the controller queue.)
9914743Swnj 	 */
99232523Sbostic 	um = (ui = udadinfo[unit])->ui_mi;
99332523Sbostic 	dp = &udautab[unit];
99432523Sbostic 	APPEND(bp, dp, av_forw);
99532523Sbostic 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
99632523Sbostic 		APPEND(dp, &um->um_tab, b_forw);
99732523Sbostic 		dp->b_active++;
99832523Sbostic 	}
99932523Sbostic 
10004743Swnj 	/*
100132523Sbostic 	 * Start activity on the controller.  Note that unlike other
100232523Sbostic 	 * Unibus drivers, we must always do this, not just when the
100332523Sbostic 	 * controller is not active.
10044743Swnj 	 */
100532523Sbostic 	udastart(um);
10065434Sroot 	splx(s);
10074743Swnj }
10084743Swnj 
100932523Sbostic /*
101032523Sbostic  * Start up whatever transfers we can find.
101132523Sbostic  * Note that udastart() must be called at spl5().
101232523Sbostic  */
101332523Sbostic udastart(um)
10144743Swnj 	register struct uba_ctlr *um;
10154743Swnj {
101632523Sbostic 	register struct uda_softc *sc = &uda_softc[um->um_ctlr];
10174743Swnj 	register struct buf *bp, *dp;
10184743Swnj 	register struct mscp *mp;
101932523Sbostic 	struct uba_device *ui;
10204743Swnj 	struct udadevice *udaddr;
102132523Sbostic 	struct partition *pp;
102232523Sbostic 	int i, sz;
10234743Swnj 
102432523Sbostic #ifdef lint
102532523Sbostic 	i = 0; i = i;
102632523Sbostic #endif
102732523Sbostic 	/*
102832523Sbostic 	 * If it is not running, try (again and again...) to initialise
102932523Sbostic 	 * it.  If it is currently initialising just ignore it for now.
103032523Sbostic 	 */
103132523Sbostic 	if (sc->sc_state != ST_RUN) {
103232523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr))
103332523Sbostic 			printf("uda%d: still hung\n", um->um_ctlr);
103432523Sbostic 		return;
103532523Sbostic 	}
103632523Sbostic 
103732523Sbostic 	/*
103832523Sbostic 	 * If um_cmd is nonzero, this controller is on the Unibus
103932523Sbostic 	 * resource wait queue.  It will not help to try more requests;
104032523Sbostic 	 * instead, when the Unibus unblocks and calls udadgo(), we
104132523Sbostic 	 * will call udastart() again.
104232523Sbostic 	 */
104332523Sbostic 	if (um->um_cmd)
104432523Sbostic 		return;
104532523Sbostic 
104632523Sbostic 	sc->sc_flags |= SC_INSTART;
104732523Sbostic 	udaddr = (struct udadevice *) um->um_addr;
104832523Sbostic 
10494743Swnj loop:
105032523Sbostic 	/*
105132523Sbostic 	 * Service the drive at the head of the queue.  It may not
105232523Sbostic 	 * need anything, in which case it might be shutting down
105332523Sbostic 	 * in udaclose().
105432523Sbostic 	 */
105532523Sbostic 	if ((dp = um->um_tab.b_actf) == NULL)
105632523Sbostic 		goto out;
10574743Swnj 	if ((bp = dp->b_actf) == NULL) {
10584743Swnj 		dp->b_active = 0;
10594743Swnj 		um->um_tab.b_actf = dp->b_forw;
106032523Sbostic 		if (ra_info[dp - udautab].ra_openpart == 0)
106132523Sbostic 			wakeup((caddr_t)dp); /* finish close protocol */
106232523Sbostic 		goto loop;
10634743Swnj 	}
106432523Sbostic 
106532523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
106633444Sbostic 		udasaerror(um, 1);
106732523Sbostic 		goto out;
10684743Swnj 	}
106932523Sbostic 
107032523Sbostic 	/*
107132523Sbostic 	 * Get an MSCP packet, then figure out what to do.  If
107232523Sbostic 	 * we cannot get a command packet, the command ring may
107332523Sbostic 	 * be too small:  We should have at least as many command
107432523Sbostic 	 * packets as credits, for best performance.
107532523Sbostic 	 */
107632523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) {
107732523Sbostic 		if (sc->sc_mi.mi_credits > MSCP_MINCREDITS &&
107832523Sbostic 		    (sc->sc_flags & SC_GRIPED) == 0) {
107932523Sbostic 			log(LOG_NOTICE, "uda%d: command ring too small\n",
108032523Sbostic 				um->um_ctlr);
108132523Sbostic 			sc->sc_flags |= SC_GRIPED;/* complain only once */
108217553Skarels 		}
108332523Sbostic 		goto out;
10844743Swnj 	}
10854743Swnj 
108632523Sbostic 	/*
108732523Sbostic 	 * Bring the drive on line if it is not already.  Get its status
108832523Sbostic 	 * if we do not already have it.  Otherwise just start the transfer.
108932523Sbostic 	 */
109032523Sbostic 	ui = udadinfo[udaunit(bp->b_dev)];
109132523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
109232523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
109332523Sbostic 		goto common;
10944743Swnj 	}
109532523Sbostic 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
109632523Sbostic 		mp->mscp_opcode = M_OP_GETUNITST;
109732523Sbostic common:
109832523Sbostic if (ui->ui_flags & UNIT_REQUEUE) panic("udastart");
109932523Sbostic 		/*
110032523Sbostic 		 * Take the drive off the controller queue.  When the
110132523Sbostic 		 * command finishes, make sure the drive is requeued.
110232523Sbostic 		 */
110332523Sbostic 		um->um_tab.b_actf = dp->b_forw;
110432523Sbostic 		dp->b_active = 0;
110532523Sbostic 		ui->ui_flags |= UNIT_REQUEUE;
110632523Sbostic 		mp->mscp_unit = ui->ui_slave;
110732523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
110832523Sbostic 		sc->sc_flags |= SC_STARTPOLL;
110932523Sbostic #ifdef POLLSTATS
111032523Sbostic 		sc->sc_ncmd++;
111125653Skarels #endif
111232523Sbostic 		goto loop;
111317553Skarels 	}
111432523Sbostic 
111532523Sbostic 	pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)];
111632523Sbostic 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
11174743Swnj 	mp->mscp_unit = ui->ui_slave;
111832523Sbostic 	mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset;
111930536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
112032523Sbostic 	mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ?
112132523Sbostic 		(pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount;
112232523Sbostic 	/* mscp_cmdref is filled in by mscp_go() */
11234743Swnj 
11244743Swnj 	/*
112532523Sbostic 	 * Drop the packet pointer into the `command' field so udadgo()
112632523Sbostic 	 * can tell what to start.  If ubago returns 1, we can do another
112732523Sbostic 	 * transfer.  If not, um_cmd will still point at mp, so we will
112832523Sbostic 	 * know that we are waiting for resources.
11294743Swnj 	 */
113032523Sbostic 	um->um_cmd = (int)mp;
113132523Sbostic 	if (ubago(ui))
113232523Sbostic 		goto loop;
113332523Sbostic 
113432523Sbostic 	/*
113532523Sbostic 	 * All done, or blocked in ubago().  If we managed to
113632523Sbostic 	 * issue some commands, start up the beast.
113732523Sbostic 	 */
113832523Sbostic out:
113932523Sbostic 	if (sc->sc_flags & SC_STARTPOLL) {
114032523Sbostic #ifdef POLLSTATS
114132523Sbostic 		udastats.cmd[sc->sc_ncmd]++;
114232523Sbostic 		sc->sc_ncmd = 0;
114332523Sbostic #endif
114433444Sbostic 		i = ((struct udadevice *)um->um_addr)->udaip;
11454743Swnj 	}
114632523Sbostic 	sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL);
114732523Sbostic }
114832523Sbostic 
114932523Sbostic /*
115032523Sbostic  * Start a transfer.
115132523Sbostic  *
115232523Sbostic  * If we are not called from within udastart(), we must have been
115332523Sbostic  * blocked, so call udastart to do more requests (if any).  If
115432523Sbostic  * this calls us again immediately we will not recurse, because
115532523Sbostic  * that time we will be in udastart().  Clever....
115632523Sbostic  */
115732523Sbostic udadgo(um)
115832523Sbostic 	register struct uba_ctlr *um;
115932523Sbostic {
116032523Sbostic 	struct uda_softc *sc = &uda_softc[um->um_ctlr];
116132523Sbostic 	struct mscp *mp = (struct mscp *)um->um_cmd;
116232523Sbostic 
116332523Sbostic 	um->um_tab.b_active++;	/* another transfer going */
116432523Sbostic 
11654743Swnj 	/*
116632523Sbostic 	 * Fill in the MSCP packet and move the buffer to the
116732523Sbostic 	 * I/O wait queue.  Mark the controller as no longer on
116832523Sbostic 	 * the resource queue, and remember to initiate polling.
11694743Swnj 	 */
117036036Skarels 	mp->mscp_seq.seq_buffer = UBAI_ADDR(um->um_ubinfo) |
117132523Sbostic 		(UBAI_BDP(um->um_ubinfo) << 24);
117232523Sbostic 	mscp_go(&sc->sc_mi, mp, um->um_ubinfo);
117332523Sbostic 	um->um_cmd = 0;
117432523Sbostic 	um->um_ubinfo = 0;	/* tyke it awye */
117532523Sbostic 	sc->sc_flags |= SC_STARTPOLL;
117632523Sbostic #ifdef POLLSTATS
117732523Sbostic 	sc->sc_ncmd++;
117832523Sbostic #endif
117932523Sbostic 	if ((sc->sc_flags & SC_INSTART) == 0)
118032523Sbostic 		udastart(um);
11814743Swnj }
11824743Swnj 
118332523Sbostic udaiodone(mi, bp, info)
118432523Sbostic 	register struct mscp_info *mi;
118532523Sbostic 	struct buf *bp;
118632523Sbostic 	int info;
118732523Sbostic {
118832523Sbostic 	register struct uba_ctlr *um = udaminfo[mi->mi_ctlr];
118932523Sbostic 
119032523Sbostic 	um->um_ubinfo = info;
119132523Sbostic 	ubadone(um);
119232523Sbostic 	biodone(bp);
119332523Sbostic 	if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab)
119432523Sbostic 		ubarelse(um->um_ubanum, &um->um_bdp);
119532523Sbostic 	um->um_tab.b_active--;	/* another transfer done */
119632523Sbostic }
119732523Sbostic 
119833444Sbostic static struct saerr {
119933444Sbostic 	int	code;		/* error code (including UDA_ERR) */
120033444Sbostic 	char	*desc;		/* what it means: Efoo => foo error */
120133444Sbostic } saerr[] = {
120233444Sbostic 	{ 0100001, "Eunibus packet read" },
120333444Sbostic 	{ 0100002, "Eunibus packet write" },
120433444Sbostic 	{ 0100003, "EUDA ROM and RAM parity" },
120533444Sbostic 	{ 0100004, "EUDA RAM parity" },
120633444Sbostic 	{ 0100005, "EUDA ROM parity" },
120733444Sbostic 	{ 0100006, "Eunibus ring read" },
120833444Sbostic 	{ 0100007, "Eunibus ring write" },
120933444Sbostic 	{ 0100010, " unibus interrupt master failure" },
121033444Sbostic 	{ 0100011, "Ehost access timeout" },
121133444Sbostic 	{ 0100012, " host exceeded command limit" },
121233444Sbostic 	{ 0100013, " unibus bus master failure" },
121333444Sbostic 	{ 0100014, " DM XFC fatal error" },
121433444Sbostic 	{ 0100015, " hardware timeout of instruction loop" },
121533444Sbostic 	{ 0100016, " invalid virtual circuit id" },
121633444Sbostic 	{ 0100017, "Eunibus interrupt write" },
121733444Sbostic 	{ 0104000, "Efatal sequence" },
121833444Sbostic 	{ 0104040, " D proc ALU" },
121933444Sbostic 	{ 0104041, "ED proc control ROM parity" },
122033444Sbostic 	{ 0105102, "ED proc w/no BD#2 or RAM parity" },
122133444Sbostic 	{ 0105105, "ED proc RAM buffer" },
122233444Sbostic 	{ 0105152, "ED proc SDI" },
122333444Sbostic 	{ 0105153, "ED proc write mode wrap serdes" },
122433444Sbostic 	{ 0105154, "ED proc read mode serdes, RSGEN & ECC" },
122533444Sbostic 	{ 0106040, "EU proc ALU" },
122633444Sbostic 	{ 0106041, "EU proc control reg" },
122733444Sbostic 	{ 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" },
122833444Sbostic 	{ 0106047, " U proc const PROM err w/D proc running SDI test" },
122933444Sbostic 	{ 0106055, " unexpected trap" },
123033444Sbostic 	{ 0106071, "EU proc const PROM" },
123133444Sbostic 	{ 0106072, "EU proc control ROM parity" },
123233444Sbostic 	{ 0106200, "Estep 1 data" },
123333444Sbostic 	{ 0107103, "EU proc RAM parity" },
123433444Sbostic 	{ 0107107, "EU proc RAM buffer" },
123533444Sbostic 	{ 0107115, " test count wrong (BD 12)" },
123633444Sbostic 	{ 0112300, "Estep 2" },
123733444Sbostic 	{ 0122240, "ENPR" },
123833444Sbostic 	{ 0122300, "Estep 3" },
123933444Sbostic 	{ 0142300, "Estep 4" },
124033444Sbostic 	{ 0, " unknown error code" }
124133444Sbostic };
124233444Sbostic 
12434743Swnj /*
124433444Sbostic  * If the error bit was set in the controller status register, gripe,
124533444Sbostic  * then (optionally) reset the controller and requeue pending transfers.
12464743Swnj  */
124733444Sbostic udasaerror(um, doreset)
124832523Sbostic 	register struct uba_ctlr *um;
124933444Sbostic 	int doreset;
12504743Swnj {
125133444Sbostic 	register int code = ((struct udadevice *)um->um_addr)->udasa;
125233444Sbostic 	register struct saerr *e;
125332523Sbostic 
125433444Sbostic 	if ((code & UDA_ERR) == 0)
125533444Sbostic 		return;
125633444Sbostic 	for (e = saerr; e->code; e++)
125733444Sbostic 		if (e->code == code)
125833444Sbostic 			break;
125933444Sbostic 	printf("uda%d: controller error, sa=0%o (%s%s)\n",
126033444Sbostic 		um->um_ctlr, code, e->desc + 1,
126133444Sbostic 		*e->desc == 'E' ? " error" : "");
126233444Sbostic 	if (doreset) {
126333444Sbostic 		mscp_requeue(&uda_softc[um->um_ctlr].sc_mi);
126433444Sbostic 		(void) udainit(um->um_ctlr);
126533444Sbostic 	}
126632523Sbostic }
126732523Sbostic 
126832523Sbostic /*
126932523Sbostic  * Interrupt routine.  Depending on the state of the controller,
127032523Sbostic  * continue initialisation, or acknowledge command and response
127132523Sbostic  * interrupts, and process responses.
127232523Sbostic  */
127332523Sbostic udaintr(ctlr)
127432523Sbostic 	int ctlr;
127532523Sbostic {
127632523Sbostic 	register struct uba_ctlr *um = udaminfo[ctlr];
127732523Sbostic 	register struct uda_softc *sc = &uda_softc[ctlr];
127833444Sbostic 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
127932523Sbostic 	register struct uda *ud;
128032523Sbostic 	register struct mscp *mp;
12814743Swnj 	register int i;
12824743Swnj 
128335401Stef #ifdef QBA
128436036Skarels 	splx(sc->sc_ipl);	/* Qbus interrupt protocol is odd */
128527254Skridle #endif
128632523Sbostic 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
128732523Sbostic 
128832523Sbostic 	/*
128932523Sbostic 	 * Combinations during steps 1, 2, and 3: STEPnMASK
129032523Sbostic 	 * corresponds to which bits should be tested;
129132523Sbostic 	 * STEPnGOOD corresponds to the pattern that should
129232523Sbostic 	 * appear after the interrupt from STEPn initialisation.
129332523Sbostic 	 * All steps test the bits in ALLSTEPS.
129432523Sbostic 	 */
129532523Sbostic #define	ALLSTEPS	(UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1)
129632523Sbostic 
129732523Sbostic #define	STEP1MASK	(ALLSTEPS | UDA_IE | UDA_NCNRMASK)
129832523Sbostic #define	STEP1GOOD	(UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2)
129932523Sbostic 
130032523Sbostic #define	STEP2MASK	(ALLSTEPS | UDA_IE | UDA_IVECMASK)
130132523Sbostic #define	STEP2GOOD	(UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2))
130232523Sbostic 
130332523Sbostic #define	STEP3MASK	ALLSTEPS
130432523Sbostic #define	STEP3GOOD	UDA_STEP4
130532523Sbostic 
13064743Swnj 	switch (sc->sc_state) {
130732523Sbostic 
130832523Sbostic 	case ST_IDLE:
130932523Sbostic 		/*
131032523Sbostic 		 * Ignore unsolicited interrupts.
131132523Sbostic 		 */
131232523Sbostic 		log(LOG_WARNING, "uda%d: stray intr\n", ctlr);
13134743Swnj 		return;
13144743Swnj 
131532523Sbostic 	case ST_STEP1:
131632523Sbostic 		/*
131732523Sbostic 		 * Begin step two initialisation.
131832523Sbostic 		 */
131932523Sbostic 		if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) {
132032523Sbostic 			i = 1;
132132523Sbostic initfailed:
132232523Sbostic 			printf("uda%d: init step %d failed, sa=%b\n",
132332523Sbostic 				ctlr, i, udaddr->udasa, udasr_bits);
132433444Sbostic 			udasaerror(um, 0);
132532523Sbostic 			sc->sc_state = ST_IDLE;
132632523Sbostic 			if (sc->sc_flags & SC_DOWAKE) {
132732523Sbostic 				sc->sc_flags &= ~SC_DOWAKE;
132833444Sbostic 				wakeup((caddr_t)sc);
132932523Sbostic 			}
13304743Swnj 			return;
13314743Swnj 		}
133233444Sbostic 		udaddr->udasa = (int)&sc->sc_uda->uda_ca.ca_rspdsc[0] |
133332523Sbostic 			(cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0);
133432523Sbostic 		sc->sc_state = ST_STEP2;
13354743Swnj 		return;
13364743Swnj 
133732523Sbostic 	case ST_STEP2:
133832523Sbostic 		/*
133932523Sbostic 		 * Begin step 3 initialisation.
134032523Sbostic 		 */
134132523Sbostic 		if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) {
134232523Sbostic 			i = 2;
134332523Sbostic 			goto initfailed;
13444743Swnj 		}
134533444Sbostic 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16;
134632523Sbostic 		sc->sc_state = ST_STEP3;
13474743Swnj 		return;
13484743Swnj 
134932523Sbostic 	case ST_STEP3:
135032523Sbostic 		/*
135132523Sbostic 		 * Set controller characteristics (finish initialisation).
135232523Sbostic 		 */
135332523Sbostic 		if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) {
135432523Sbostic 			i = 3;
135532523Sbostic 			goto initfailed;
13564743Swnj 		}
135732523Sbostic 		i = udaddr->udasa & 0xff;
135832523Sbostic 		if (i != sc->sc_micro) {
135932523Sbostic 			sc->sc_micro = i;
136032523Sbostic 			printf("uda%d: version %d model %d\n",
136132523Sbostic 				ctlr, i & 0xf, i >> 4);
136232523Sbostic 		}
136332523Sbostic 
136417553Skarels 		/*
136532523Sbostic 		 * Present the burst size, then remove it.  Why this
136632523Sbostic 		 * should be done this way, I have no idea.
136732523Sbostic 		 *
136832523Sbostic 		 * Note that this assumes udaburst[ctlr] > 0.
136917553Skarels 		 */
137032523Sbostic 		udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2;
13714743Swnj 		udaddr->udasa = UDA_GO;
137232523Sbostic 		printf("uda%d: DMA burst size set to %d\n",
137332523Sbostic 			ctlr, udaburst[ctlr]);
13744743Swnj 
137532523Sbostic 		udainitds(ctlr);	/* initialise data structures */
137632523Sbostic 
13774743Swnj 		/*
137832523Sbostic 		 * Before we can get a command packet, we need some
137932523Sbostic 		 * credits.  Fake some up to keep mscp_getcp() happy,
138032523Sbostic 		 * get a packet, and cancel all credits (the right
138132523Sbostic 		 * number should come back in the response to the
138232523Sbostic 		 * SCC packet).
13834743Swnj 		 */
138432523Sbostic 		sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1;
138532523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT);
138632523Sbostic 		if (mp == NULL)	/* `cannot happen' */
138732523Sbostic 			panic("udaintr");
138832523Sbostic 		sc->sc_mi.mi_credits = 0;
138932523Sbostic 		mp->mscp_opcode = M_OP_SETCTLRC;
139032523Sbostic 		mp->mscp_unit = 0;
139132523Sbostic 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
139232523Sbostic 			M_CF_THIS;
139332523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
139432523Sbostic 		i = udaddr->udaip;
139532523Sbostic 		sc->sc_state = ST_SETCHAR;
13964743Swnj 		return;
13974743Swnj 
139832523Sbostic 	case ST_SETCHAR:
139932523Sbostic 	case ST_RUN:
140032523Sbostic 		/*
140132523Sbostic 		 * Handle Set Ctlr Characteristics responses and operational
140232523Sbostic 		 * responses (via mscp_dorsp).
140332523Sbostic 		 */
14044743Swnj 		break;
14054743Swnj 
14064743Swnj 	default:
140732523Sbostic 		printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state);
140832523Sbostic 		panic("udastate");
14094743Swnj 	}
14104743Swnj 
141132523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
141233444Sbostic 		udasaerror(um, 1);
141332523Sbostic 		return;
14144743Swnj 	}
14154743Swnj 
141632523Sbostic 	ud = &uda[ctlr];
141732523Sbostic 
14184743Swnj 	/*
141932523Sbostic 	 * Handle buffer purge requests.
14204743Swnj 	 */
14214743Swnj 	if (ud->uda_ca.ca_bdp) {
142226372Skarels 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
14234743Swnj 		ud->uda_ca.ca_bdp = 0;
142432523Sbostic 		udaddr->udasa = 0;	/* signal purge complete */
14254743Swnj 	}
14264743Swnj 
14274743Swnj 	/*
142832523Sbostic 	 * Check for response and command ring transitions.
14294743Swnj 	 */
14304743Swnj 	if (ud->uda_ca.ca_rspint) {
14314743Swnj 		ud->uda_ca.ca_rspint = 0;
143232523Sbostic 		mscp_dorsp(&sc->sc_mi);
14334743Swnj 	}
14344743Swnj 	if (ud->uda_ca.ca_cmdint) {
14354743Swnj 		ud->uda_ca.ca_cmdint = 0;
143632523Sbostic 		MSCP_DOCMD(&sc->sc_mi);
14374743Swnj 	}
143832523Sbostic 	udastart(um);
14394743Swnj }
14404743Swnj 
14414743Swnj /*
144232523Sbostic  * Initialise the various data structures that control the UDA50.
144317553Skarels  */
144432523Sbostic udainitds(ctlr)
144532523Sbostic 	int ctlr;
144632523Sbostic {
144732523Sbostic 	register struct uda *ud = &uda[ctlr];
144832523Sbostic 	register struct uda *uud = uda_softc[ctlr].sc_uda;
144932523Sbostic 	register struct mscp *mp;
145032523Sbostic 	register int i;
14514743Swnj 
145232523Sbostic 	for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) {
145332523Sbostic 		ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
145432523Sbostic 			(long)&uud->uda_rsp[i].mscp_cmdref;
145532523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i];
145632523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
14574743Swnj 	}
145832523Sbostic 	for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) {
145932523Sbostic 		ud->uda_ca.ca_cmddsc[i] = MSCP_INT |
146032523Sbostic 			(long)&uud->uda_cmd[i].mscp_cmdref;
146132523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i];
146232523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
146332523Sbostic 	}
14644743Swnj }
14654743Swnj 
14664743Swnj /*
146733444Sbostic  * Handle an error datagram.
14684743Swnj  */
146932523Sbostic udadgram(mi, mp)
147032523Sbostic 	struct mscp_info *mi;
147132523Sbostic 	struct mscp *mp;
14724743Swnj {
147317553Skarels 
147432523Sbostic 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
147533444Sbostic 	/*
147633444Sbostic 	 * SDI status information bytes 10 and 11 are the microprocessor
147733444Sbostic 	 * error code and front panel code respectively.  These vary per
147833444Sbostic 	 * drive type and are printed purely for field service information.
147933444Sbostic 	 */
148033444Sbostic 	if (mp->mscp_format == M_FM_SDI)
148133444Sbostic 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
148233444Sbostic 			mp->mscp_erd.erd_sdistat[10],
148333444Sbostic 			mp->mscp_erd.erd_sdistat[11]);
148432523Sbostic }
148517553Skarels 
148632523Sbostic /*
148732523Sbostic  * The Set Controller Characteristics command finished.
148832523Sbostic  * Record the new state of the controller.
148932523Sbostic  */
149032523Sbostic udactlrdone(mi, mp)
149132523Sbostic 	register struct mscp_info *mi;
149232523Sbostic 	struct mscp *mp;
149332523Sbostic {
149432523Sbostic 	register struct uda_softc *sc = &uda_softc[mi->mi_ctlr];
149517553Skarels 
149632523Sbostic 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
149732523Sbostic 		sc->sc_state = ST_RUN;
149832523Sbostic 	else {
149932523Sbostic 		printf("uda%d: SETCTLRC failed: ",
150032523Sbostic 			mi->mi_ctlr, mp->mscp_status);
150132523Sbostic 		mscp_printevent(mp);
150232523Sbostic 		sc->sc_state = ST_IDLE;
15034743Swnj 	}
150432523Sbostic 	if (sc->sc_flags & SC_DOWAKE) {
150532523Sbostic 		sc->sc_flags &= ~SC_DOWAKE;
150632523Sbostic 		wakeup((caddr_t)sc);
15076964Ssam 	}
15084743Swnj }
15094743Swnj 
15104743Swnj /*
151132523Sbostic  * Received a response from an as-yet unconfigured drive.  Configure it
151232523Sbostic  * in, if possible.
15134743Swnj  */
151432523Sbostic udaunconf(mi, mp)
151532523Sbostic 	struct mscp_info *mi;
151632523Sbostic 	register struct mscp *mp;
15174743Swnj {
15184743Swnj 
151917553Skarels 	/*
152032523Sbostic 	 * If it is a slave response, copy it to udaslavereply for
152132523Sbostic 	 * udaslave() to look at.
152217553Skarels 	 */
152332523Sbostic 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
152432523Sbostic 	    (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) {
152532523Sbostic 		udaslavereply = *mp;
152632523Sbostic 		return (MSCP_DONE);
15274743Swnj 	}
152832523Sbostic 
152932523Sbostic 	/*
153032523Sbostic 	 * Otherwise, it had better be an available attention response.
153132523Sbostic 	 */
153232523Sbostic 	if (mp->mscp_opcode != M_OP_AVAILATTN)
153332523Sbostic 		return (MSCP_FAILED);
153432523Sbostic 
153532523Sbostic 	/* do what autoconf does */
153632523Sbostic 	return (MSCP_FAILED);	/* not yet, arwhite, not yet */
15374743Swnj }
15384743Swnj 
153932523Sbostic /*
154032523Sbostic  * A drive came on line.  Check its type and size.  Return DONE if
154132523Sbostic  * we think the drive is truly on line.  In any case, awaken anyone
154232523Sbostic  * sleeping on the drive on-line-ness.
154332523Sbostic  */
154432523Sbostic udaonline(ui, mp)
154532523Sbostic 	register struct uba_device *ui;
154632523Sbostic 	struct mscp *mp;
15474743Swnj {
154832523Sbostic 	register struct ra_info *ra = &ra_info[ui->ui_unit];
15494743Swnj 
155032523Sbostic 	wakeup((caddr_t)&ui->ui_flags);
155132523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
155236036Skarels 		if (!cold)
155336036Skarels 			printf("uda%d: ra%d", ui->ui_ctlr, ui->ui_unit);
155436036Skarels 		printf(": attempt to bring on line failed: ");
155532523Sbostic 		mscp_printevent(mp);
155632523Sbostic 		ra->ra_state = CLOSED;
155732523Sbostic 		return (MSCP_FAILED);
155832523Sbostic 	}
155932523Sbostic 
156032523Sbostic 	ra->ra_state = OPENRAW;
156132523Sbostic 	ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize;
156234283Skarels 	if (!cold)
156334283Skarels 		printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit,
156434283Skarels 		    ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize);
156532523Sbostic 	/* can now compute ncyl */
156632523Sbostic 	ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks /
156732523Sbostic 		ra->ra_geom.rg_nsectors;
156832523Sbostic 	return (MSCP_DONE);
15694743Swnj }
15704743Swnj 
157132523Sbostic /*
157232523Sbostic  * We got some (configured) unit's status.  Return DONE if it succeeded.
157332523Sbostic  */
157432523Sbostic udagotstatus(ui, mp)
157532523Sbostic 	register struct uba_device *ui;
157632523Sbostic 	register struct mscp *mp;
15774743Swnj {
15784743Swnj 
157932523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
158032523Sbostic 		printf("uda%d: attempt to get status for ra%d failed: ",
158132523Sbostic 			ui->ui_ctlr, ui->ui_unit);
158232523Sbostic 		mscp_printevent(mp);
158332523Sbostic 		return (MSCP_FAILED);
158432523Sbostic 	}
158532523Sbostic 	/* record for (future) bad block forwarding and whatever else */
158632523Sbostic 	uda_rasave(ui->ui_unit, mp, 1);
158732523Sbostic 	return (MSCP_DONE);
15884743Swnj }
15894743Swnj 
159032523Sbostic /*
159132523Sbostic  * A transfer failed.  We get a chance to fix or restart it.
159232523Sbostic  * Need to write the bad block forwaring code first....
159332523Sbostic  */
159432523Sbostic /*ARGSUSED*/
159532523Sbostic udaioerror(ui, mp, bp)
159632523Sbostic 	register struct uba_device *ui;
159732523Sbostic 	register struct mscp *mp;
159832523Sbostic 	struct buf *bp;
15994743Swnj {
16004743Swnj 
160132523Sbostic 	if (mp->mscp_flags & M_EF_BBLKR) {
160232523Sbostic 		/*
160332523Sbostic 		 * A bad block report.  Eventually we will
160432523Sbostic 		 * restart this transfer, but for now, just
160532523Sbostic 		 * log it and give up.
160632523Sbostic 		 */
160732523Sbostic 		log(LOG_ERR, "ra%d: bad block report: %d%s\n",
160832523Sbostic 			ui->ui_unit, mp->mscp_seq.seq_lbn,
160932523Sbostic 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
161032523Sbostic 	} else {
161132523Sbostic 		/*
161232523Sbostic 		 * What the heck IS a `serious exception' anyway?
161332523Sbostic 		 * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION
161432523Sbostic 		 * FOR THEIR OWN CONTROLLERS.
161532523Sbostic 		 */
161632523Sbostic 		if (mp->mscp_flags & M_EF_SEREX)
161732523Sbostic 			log(LOG_ERR, "ra%d: serious exception reported\n",
161832523Sbostic 				ui->ui_unit);
16194743Swnj 	}
162032523Sbostic 	return (MSCP_FAILED);
16214743Swnj }
16224743Swnj 
162332523Sbostic /*
162432523Sbostic  * A replace operation finished.
162532523Sbostic  */
162632523Sbostic /*ARGSUSED*/
162732523Sbostic udareplace(ui, mp)
162832523Sbostic 	struct uba_device *ui;
162932523Sbostic 	struct mscp *mp;
16304743Swnj {
163117553Skarels 
163232523Sbostic 	panic("udareplace");
16334743Swnj }
163417553Skarels 
163532523Sbostic /*
163632523Sbostic  * A bad block related operation finished.
163732523Sbostic  */
163832523Sbostic /*ARGSUSED*/
163932523Sbostic udabb(ui, mp, bp)
164032523Sbostic 	struct uba_device *ui;
164132523Sbostic 	struct mscp *mp;
164232523Sbostic 	struct buf *bp;
164317553Skarels {
164417553Skarels 
164532523Sbostic 	panic("udabb");
164617553Skarels }
164717553Skarels 
164832523Sbostic 
164932523Sbostic /*
165032523Sbostic  * I/O controls.
165132523Sbostic  */
165232523Sbostic udaioctl(dev, cmd, data, flag)
165312511Ssam 	dev_t dev;
165430536Skarels 	int cmd;
165530536Skarels 	caddr_t data;
165630536Skarels 	int flag;
165712511Ssam {
165832523Sbostic 	register int unit = udaunit(dev);
165930536Skarels 	register struct disklabel *lp;
166033443Skarels 	register struct ra_info *ra = &ra_info[unit];
166134638Skarels 	int error = 0;
166212511Ssam 
166332523Sbostic 	lp = &udalabel[unit];
166430536Skarels 
166530536Skarels 	switch (cmd) {
166630536Skarels 
166730536Skarels 	case DIOCGDINFO:
166830536Skarels 		*(struct disklabel *)data = *lp;
166930536Skarels 		break;
167030536Skarels 
167130773Skarels 	case DIOCGPART:
167230773Skarels 		((struct partinfo *)data)->disklab = lp;
167330773Skarels 		((struct partinfo *)data)->part =
167432523Sbostic 		    &lp->d_partitions[udapart(dev)];
167530536Skarels 		break;
167630536Skarels 
167730536Skarels 	case DIOCSDINFO:
167830536Skarels 		if ((flag & FWRITE) == 0)
167930536Skarels 			error = EBADF;
168030536Skarels 		else
168132574Skarels 			error = setdisklabel(lp, (struct disklabel *)data,
168234283Skarels 			    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart);
168330536Skarels 		break;
168430536Skarels 
168533443Skarels 	case DIOCWLABEL:
168633443Skarels 		if ((flag & FWRITE) == 0)
168733443Skarels 			error = EBADF;
168833443Skarels 		else
168933443Skarels 			ra->ra_wlabel = *(int *)data;
169033443Skarels 		break;
169133443Skarels 
169232574Skarels 	case DIOCWDINFO:
169332574Skarels 		if ((flag & FWRITE) == 0)
169432523Sbostic 			error = EBADF;
169532574Skarels 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
169634283Skarels 		    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart)) == 0) {
169734638Skarels 			int wlab;
169834638Skarels 
169934283Skarels 			ra->ra_state = OPEN;
170034638Skarels 			/* simulate opening partition 0 so write succeeds */
170134638Skarels 			ra->ra_openpart |= (1 << 0);		/* XXX */
170234638Skarels 			wlab = ra->ra_wlabel;
170334638Skarels 			ra->ra_wlabel = 1;
170432574Skarels 			error = writedisklabel(dev, udastrategy, lp);
170534638Skarels 			ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
170634638Skarels 			ra->ra_wlabel = wlab;
170734283Skarels 		}
170830536Skarels 		break;
170930536Skarels 
171032523Sbostic #ifdef notyet
171132523Sbostic 	case UDAIOCREPLACE:
171232523Sbostic 		/*
171332523Sbostic 		 * Initiate bad block replacement for the given LBN.
171432523Sbostic 		 * (Should we allow modifiers?)
171532523Sbostic 		 */
171632523Sbostic 		error = EOPNOTSUPP;
171732523Sbostic 		break;
171832523Sbostic 
171932523Sbostic 	case UDAIOCGMICRO:
172032523Sbostic 		/*
172132523Sbostic 		 * Return the microcode revision for the UDA50 running
172232523Sbostic 		 * this drive.
172332523Sbostic 		 */
172433444Sbostic 		*(int *)data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro;
172532523Sbostic 		break;
172632523Sbostic #endif
172732523Sbostic 
172830536Skarels 	default:
172930536Skarels 		error = ENOTTY;
173030536Skarels 		break;
173130536Skarels 	}
173232523Sbostic 	return (error);
173332523Sbostic }
173432523Sbostic 
173532523Sbostic /*
173632523Sbostic  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
173732523Sbostic  * on that Unibus, and requeue outstanding I/O.
173832523Sbostic  */
173932523Sbostic udareset(uban)
174032523Sbostic 	int uban;
174132523Sbostic {
174232523Sbostic 	register struct uba_ctlr *um;
174332523Sbostic 	register struct uda_softc *sc;
174432523Sbostic 	register int ctlr;
174532523Sbostic 
174632523Sbostic 	for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) {
174732523Sbostic 		if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban ||
174832523Sbostic 		    um->um_alive == 0)
174932523Sbostic 			continue;
175032523Sbostic 		printf(" uda%d", ctlr);
175132523Sbostic 
175232523Sbostic 		/*
175332523Sbostic 		 * Our BDP (if any) is gone; our command (if any) is
175432523Sbostic 		 * flushed; the device is no longer mapped; and the
175532523Sbostic 		 * UDA50 is not yet initialised.
175632523Sbostic 		 */
175732523Sbostic 		if (um->um_bdp) {
175832523Sbostic 			printf("<%d>", UBAI_BDP(um->um_bdp));
175932523Sbostic 			um->um_bdp = 0;
176032523Sbostic 		}
176132523Sbostic 		um->um_ubinfo = 0;
176232523Sbostic 		um->um_cmd = 0;
176332523Sbostic 		sc->sc_flags &= ~SC_MAPPED;
176432523Sbostic 		sc->sc_state = ST_IDLE;
176532523Sbostic 
176632523Sbostic 		/* reset queues and requeue pending transfers */
176732523Sbostic 		mscp_requeue(&sc->sc_mi);
176832523Sbostic 
176932523Sbostic 		/*
177032523Sbostic 		 * If it fails to initialise we will notice later and
177132523Sbostic 		 * try again (and again...).  Do not call udastart()
177232523Sbostic 		 * here; it will be done after the controller finishes
177332523Sbostic 		 * initialisation.
177432523Sbostic 		 */
177532523Sbostic 		if (udainit(ctlr))
177632523Sbostic 			printf(" (hung)");
177732523Sbostic 	}
177832523Sbostic }
177932523Sbostic 
178032523Sbostic /*
178132523Sbostic  * Watchdog timer:  If the controller is active, and no interrupts
178232523Sbostic  * have occurred for 30 seconds, assume it has gone away.
178332523Sbostic  */
178432523Sbostic udawatch()
178532523Sbostic {
178632523Sbostic 	register int i;
178732523Sbostic 	register struct uba_ctlr *um;
178832523Sbostic 	register struct uda_softc *sc;
178932523Sbostic 
179032523Sbostic 	timeout(udawatch, (caddr_t) 0, hz);	/* every second */
179132523Sbostic 	for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) {
179232523Sbostic 		if ((um = udaminfo[i]) == 0 || !um->um_alive)
179332523Sbostic 			continue;
179432523Sbostic 		if (sc->sc_state == ST_IDLE)
179532523Sbostic 			continue;
179632523Sbostic 		if (sc->sc_state == ST_RUN && !um->um_tab.b_active)
179732523Sbostic 			sc->sc_wticks = 0;
179832523Sbostic 		else if (++sc->sc_wticks >= 30) {
179932523Sbostic 			sc->sc_wticks = 0;
180032523Sbostic 			printf("uda%d: lost interrupt\n", i);
180132523Sbostic 			ubareset(um->um_ubanum);
180232523Sbostic 		}
180332523Sbostic 	}
180432523Sbostic }
180532523Sbostic 
180632523Sbostic /*
180732523Sbostic  * Do a panic dump.  We set up the controller for one command packet
180832523Sbostic  * and one response packet, for which we use `struct uda1'.
180932523Sbostic  */
181032523Sbostic struct	uda1 {
181132523Sbostic 	struct	uda1ca uda1_ca;	/* communications area */
181232523Sbostic 	struct	mscp uda1_rsp;	/* response packet */
181332523Sbostic 	struct	mscp uda1_cmd;	/* command packet */
181432523Sbostic } uda1;
181532523Sbostic 
181632523Sbostic #define	DBSIZE	32		/* dump 16K at a time */
181732523Sbostic 
181832523Sbostic udadump(dev)
181932523Sbostic 	dev_t dev;
182032523Sbostic {
182132523Sbostic 	struct udadevice *udaddr;
182232523Sbostic 	struct uda1 *ud_ubaddr;
182332523Sbostic 	char *start;
182432523Sbostic 	int num, blk, unit, maxsz, blkoff, reg;
182532523Sbostic 	struct partition *pp;
182632523Sbostic 	register struct uba_regs *uba;
182732523Sbostic 	register struct uba_device *ui;
182832523Sbostic 	register struct uda1 *ud;
182932523Sbostic 	register struct pte *io;
183032523Sbostic 	register int i;
183132523Sbostic 
183232523Sbostic 	/*
183332523Sbostic 	 * Make sure the device is a reasonable place on which to dump.
183432523Sbostic 	 */
183532523Sbostic 	unit = udaunit(dev);
183632523Sbostic 	if (unit >= NRA)
183732523Sbostic 		return (ENXIO);
183833444Sbostic #define	phys(cast, addr)	((cast) ((int)addr & 0x7fffffff))
183932523Sbostic 	ui = phys(struct uba_device *, udadinfo[unit]);
184032523Sbostic 	if (ui == NULL || ui->ui_alive == 0)
184132523Sbostic 		return (ENXIO);
184232523Sbostic 
184332523Sbostic 	/*
184432523Sbostic 	 * Find and initialise the UBA; get the physical address of the
184532523Sbostic 	 * device registers, and of communications area and command and
184632523Sbostic 	 * response packet.
184732523Sbostic 	 */
184832523Sbostic 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
184932523Sbostic 	ubainit(uba);
185032523Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
185132523Sbostic 	ud = phys(struct uda1 *, &uda1);
185232523Sbostic 
185332523Sbostic 	/*
185432523Sbostic 	 * Map the ca+packets into Unibus I/O space so the UDA50 can get
185532523Sbostic 	 * at them.  Use the registers at the end of the Unibus map (since
185632523Sbostic 	 * we will use the registers at the beginning to map the memory
185732523Sbostic 	 * we are dumping).
185832523Sbostic 	 */
185932523Sbostic 	num = btoc(sizeof(struct uda1)) + 1;
186032523Sbostic 	reg = NUBMREG - num;
186132523Sbostic 	io = &uba->uba_map[reg];
186232523Sbostic 	for (i = 0; i < num; i++)
186332523Sbostic 		*(int *)io++ = UBAMR_MRV | (btop(ud) + i);
186432523Sbostic 	ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9));
186532523Sbostic 
186632523Sbostic 	/*
186732523Sbostic 	 * Initialise the controller, with one command and one response
186832523Sbostic 	 * packet.
186932523Sbostic 	 */
187032523Sbostic 	udaddr->udaip = 0;
187132523Sbostic 	if (udadumpwait(udaddr, UDA_STEP1))
187232523Sbostic 		return (EFAULT);
187332523Sbostic 	udaddr->udasa = UDA_ERR;
187432523Sbostic 	if (udadumpwait(udaddr, UDA_STEP2))
187532523Sbostic 		return (EFAULT);
187632523Sbostic 	udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc;
187732523Sbostic 	if (udadumpwait(udaddr, UDA_STEP3))
187832523Sbostic 		return (EFAULT);
187932523Sbostic 	udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16;
188032523Sbostic 	if (udadumpwait(udaddr, UDA_STEP4))
188132523Sbostic 		return (EFAULT);
188232523Sbostic 	uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff;
188332523Sbostic 	udaddr->udasa = UDA_GO;
188432523Sbostic 
188532523Sbostic 	/*
188632523Sbostic 	 * Set up the command and response descriptor, then set the
188732523Sbostic 	 * controller characteristics and bring the drive on line.
188832523Sbostic 	 * Note that all uninitialised locations in uda1_cmd are zero.
188932523Sbostic 	 */
189032523Sbostic 	ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref;
189132523Sbostic 	ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref;
189232523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */
189332523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */
189432523Sbostic 	if (udadumpcmd(M_OP_SETCTLRC, ud, ui))
189532523Sbostic 		return (EFAULT);
189632523Sbostic 	ud->uda1_cmd.mscp_unit = ui->ui_slave;
189732523Sbostic 	if (udadumpcmd(M_OP_ONLINE, ud, ui))
189832523Sbostic 		return (EFAULT);
189932523Sbostic 
190032523Sbostic 	pp = phys(struct partition *,
190132523Sbostic 	    &udalabel[unit].d_partitions[udapart(dev)]);
190232523Sbostic 	maxsz = pp->p_size;
190332523Sbostic 	blkoff = pp->p_offset;
190432523Sbostic 
190532523Sbostic 	/*
190632523Sbostic 	 * Dump all of physical memory, or as much as will fit in the
190732523Sbostic 	 * space provided.
190832523Sbostic 	 */
190932523Sbostic 	start = 0;
191032523Sbostic 	num = maxfree;
191132523Sbostic 	if (dumplo + num >= maxsz)
191232523Sbostic 		num = maxsz - dumplo;
191332523Sbostic 	blkoff += dumplo;
191432523Sbostic 
191532523Sbostic 	/*
191632523Sbostic 	 * Write out memory, DBSIZE pages at a time.
191732523Sbostic 	 * N.B.: this code depends on the fact that the sector
191832523Sbostic 	 * size == the page size.
191932523Sbostic 	 */
192032523Sbostic 	while (num > 0) {
192132523Sbostic 		blk = num > DBSIZE ? DBSIZE : num;
192232523Sbostic 		io = uba->uba_map;
192332523Sbostic 		/*
192432523Sbostic 		 * Map in the pages to write, leaving an invalid entry
192532523Sbostic 		 * at the end to guard against wild Unibus transfers.
192632523Sbostic 		 * Then do the write.
192732523Sbostic 		 */
192832523Sbostic 		for (i = 0; i < blk; i++)
192933444Sbostic 			*(int *)io++ = UBAMR_MRV | (btop(start) + i);
193033444Sbostic 		*(int *)io = 0;
193132523Sbostic 		ud->uda1_cmd.mscp_unit = ui->ui_slave;
193232523Sbostic 		ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
193332523Sbostic 		ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
193432523Sbostic 		if (udadumpcmd(M_OP_WRITE, ud, ui))
193532523Sbostic 			return (EIO);
193632523Sbostic 		start += blk << PGSHIFT;
193732523Sbostic 		num -= blk;
193832523Sbostic 	}
193932523Sbostic 	return (0);		/* made it! */
194032523Sbostic }
194132523Sbostic 
194232523Sbostic /*
194332523Sbostic  * Wait for some of the bits in `bits' to come on.  If the error bit
194432523Sbostic  * comes on, or ten seconds pass without response, return true (error).
194532523Sbostic  */
194632523Sbostic udadumpwait(udaddr, bits)
194732523Sbostic 	register struct udadevice *udaddr;
194832523Sbostic 	register int bits;
194932523Sbostic {
195032523Sbostic 	register int timo = todr() + 1000;
195132523Sbostic 
195232523Sbostic 	while ((udaddr->udasa & bits) == 0) {
195332523Sbostic 		if (udaddr->udasa & UDA_ERR) {
195432523Sbostic 			printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
195532523Sbostic 			return (1);
195632523Sbostic 		}
195732523Sbostic 		if (todr() >= timo) {
195832523Sbostic 			printf("timeout\ndump ");
195932523Sbostic 			return (1);
196032523Sbostic 		}
196132523Sbostic 	}
196230536Skarels 	return (0);
196330536Skarels }
196430536Skarels 
196532523Sbostic /*
196632523Sbostic  * Feed a command to the UDA50, wait for its response, and return
196732523Sbostic  * true iff something went wrong.
196832523Sbostic  */
196932523Sbostic udadumpcmd(op, ud, ui)
197032523Sbostic 	int op;
197132523Sbostic 	register struct uda1 *ud;
197232523Sbostic 	struct uba_device *ui;
197332523Sbostic {
197432523Sbostic 	register struct udadevice *udaddr;
197532523Sbostic 	register int n;
197632523Sbostic #define mp (&ud->uda1_rsp)
197732523Sbostic 
197833444Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
197932523Sbostic 	ud->uda1_cmd.mscp_opcode = op;
198032523Sbostic 	ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN;
198132523Sbostic 	ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN;
198232523Sbostic 	ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
198332523Sbostic 	ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
198432523Sbostic 	if (udaddr->udasa & UDA_ERR) {
198532523Sbostic 		printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
198632523Sbostic 		return (1);
198732523Sbostic 	}
198832523Sbostic 	n = udaddr->udaip;
198932523Sbostic 	n = todr() + 1000;
199032523Sbostic 	for (;;) {
199132523Sbostic 		if (todr() > n) {
199232523Sbostic 			printf("timeout\ndump ");
199332523Sbostic 			return (1);
199432523Sbostic 		}
199532523Sbostic 		if (ud->uda1_ca.ca_cmdint)
199632523Sbostic 			ud->uda1_ca.ca_cmdint = 0;
199732523Sbostic 		if (ud->uda1_ca.ca_rspint == 0)
199832523Sbostic 			continue;
199932523Sbostic 		ud->uda1_ca.ca_rspint = 0;
200032523Sbostic 		if (mp->mscp_opcode == (op | M_OP_END))
200132523Sbostic 			break;
200232523Sbostic 		printf("\n");
200332523Sbostic 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
200432523Sbostic 
200532523Sbostic 		case MSCPT_SEQ:
200632523Sbostic 			printf("sequential");
200732523Sbostic 			break;
200832523Sbostic 
200932523Sbostic 		case MSCPT_DATAGRAM:
201032523Sbostic 			mscp_decodeerror("uda", ui->ui_ctlr, mp);
201132523Sbostic 			printf("datagram");
201232523Sbostic 			break;
201332523Sbostic 
201432523Sbostic 		case MSCPT_CREDITS:
201532523Sbostic 			printf("credits");
201632523Sbostic 			break;
201732523Sbostic 
201832523Sbostic 		case MSCPT_MAINTENANCE:
201932523Sbostic 			printf("maintenance");
202032523Sbostic 			break;
202132523Sbostic 
202232523Sbostic 		default:
202332523Sbostic 			printf("unknown (type 0x%x)",
202432523Sbostic 				MSCP_MSGTYPE(mp->mscp_msgtc));
202532523Sbostic 			break;
202632523Sbostic 		}
202732523Sbostic 		printf(" ignored\ndump ");
202832523Sbostic 		ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
202932523Sbostic 	}
203032523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
203132523Sbostic 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
203232523Sbostic 			mp->mscp_opcode, mp->mscp_status);
203332523Sbostic 		return (1);
203432523Sbostic 	}
203532523Sbostic 	return (0);
203632523Sbostic #undef mp
203732523Sbostic }
203832523Sbostic 
203932523Sbostic /*
204032523Sbostic  * Return the size of a partition, if known, or -1 if not.
204132523Sbostic  */
204232523Sbostic udasize(dev)
204330536Skarels 	dev_t dev;
204430536Skarels {
204532523Sbostic 	register int unit = udaunit(dev);
204630536Skarels 	register struct uba_device *ui;
204730536Skarels 
204832523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == NULL ||
204932523Sbostic 	    ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 ||
205032523Sbostic 	    ra_info[unit].ra_state != OPEN)
205112511Ssam 		return (-1);
205232523Sbostic 	return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size);
205312511Ssam }
205417553Skarels 
205530536Skarels #ifdef COMPAT_42
205632523Sbostic /*
205732523Sbostic  * Tables mapping unlabelled drives.
205832523Sbostic  */
205930536Skarels struct size {
206030536Skarels 	daddr_t nblocks;
206130536Skarels 	daddr_t blkoff;
206233444Sbostic } ra60_sizes[8] = {
206330536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
206430536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
206530536Skarels 	400176,	0,		/* C=sectors 0 thru 400175 */
206630536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
206730536Skarels 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
206830536Skarels 	350852,	49324,		/* F=sectors 49324 thru 400175 */
206930536Skarels 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
207030536Skarels 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
207133444Sbostic }, ra70_sizes[8] = {
207233444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
207333444Sbostic 	33440,	15972,		/* B=blk 15972 thru 49323 */
207433444Sbostic 	-1,	0,		/* C=blk 0 thru end */
207533444Sbostic 	15884,	341220,		/* D=blk 341220 thru 357103 */
207633444Sbostic 	55936,	357192,		/* E=blk 357192 thru 413127 */
207733444Sbostic 	-1,	413457,		/* F=blk 413457 thru end */
207833444Sbostic 	-1,	341220,		/* G=blk 341220 thru end */
207933444Sbostic 	291346,	49731,		/* H=blk 49731 thru 341076 */
208030536Skarels }, ra80_sizes[8] = {
208130536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
208230536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
208330536Skarels 	242606,	0,		/* C=sectors 0 thru 242605 */
208430536Skarels 	0,	0,		/* D=unused */
208530536Skarels 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
208630536Skarels 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
208730536Skarels 	192696,	49910,		/* G=sectors 49910 thru 242605 */
208830536Skarels 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
208930536Skarels }, ra81_sizes[8] ={
209030536Skarels /*
209130536Skarels  * These are the new standard partition sizes for ra81's.
209230536Skarels  * An RA_COMPAT system is compiled with D, E, and F corresponding
209330536Skarels  * to the 4.2 partitions for G, H, and F respectively.
209430536Skarels  */
209530536Skarels #ifndef	UCBRA
209630536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
209730536Skarels 	66880,	16422,		/* B=sectors 16422 thru 83301 */
209830536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
209930536Skarels #ifdef RA_COMPAT
210030536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
210130536Skarels 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
210230536Skarels 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
210330536Skarels #else
210430536Skarels 	15884,	375564,		/* D=sectors 375564 thru 391447 */
210530536Skarels 	307200,	391986,		/* E=sectors 391986 thru 699185 */
210630536Skarels 	191352,	699720,		/* F=sectors 699720 thru 891071 */
210730536Skarels #endif RA_COMPAT
210830536Skarels 	515508,	375564,		/* G=sectors 375564 thru 891071 */
210930536Skarels 	291346,	83538,		/* H=sectors 83538 thru 374883 */
211030536Skarels 
211130536Skarels /*
211230536Skarels  * These partitions correspond to the sizes used by sites at Berkeley,
211330536Skarels  * and by those sites that have received copies of the Berkeley driver
211430536Skarels  * with deltas 6.2 or greater (11/15/83).
211530536Skarels  */
211630536Skarels #else UCBRA
211730536Skarels 
211830536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
211930536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
212030536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
212130536Skarels 	15884,	242606,		/* D=sectors 242606 thru 258489 */
212230536Skarels 	307200,	258490,		/* E=sectors 258490 thru 565689 */
212330536Skarels 	325382,	565690,		/* F=sectors 565690 thru 891071 */
212430536Skarels 	648466,	242606,		/* G=sectors 242606 thru 891071 */
212530536Skarels 	193282,	49324,		/* H=sectors 49324 thru 242605 */
212630536Skarels 
212730536Skarels #endif UCBRA
212833444Sbostic }, ra82_sizes[8] = {
212933444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
213033444Sbostic 	66880,	16245,		/* B=blk 16245 thru 83124 */
213133444Sbostic 	-1,	0,		/* C=blk 0 thru end */
213233444Sbostic 	15884,	375345,		/* D=blk 375345 thru 391228 */
213333444Sbostic 	307200,	391590,		/* E=blk 391590 thru 698789 */
213433444Sbostic 	-1,	699390,		/* F=blk 699390 thru end */
213533444Sbostic 	-1,	375345,		/* G=blk 375345 thru end */
213633444Sbostic 	291346,	83790,		/* H=blk 83790 thru 375135 */
213733444Sbostic }, rc25_sizes[8] = {
213833444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
213933444Sbostic 	10032,	15884,		/* B=blk 15884 thru 49323 */
214033444Sbostic 	-1,	0,		/* C=blk 0 thru end */
214133444Sbostic 	0,	0,		/* D=blk 340670 thru 356553 */
214233444Sbostic 	0,	0,		/* E=blk 356554 thru 412489 */
214333444Sbostic 	0,	0,		/* F=blk 412490 thru end */
214433444Sbostic 	-1,	25916,		/* G=blk 49324 thru 131403 */
214533444Sbostic 	0,	0,		/* H=blk 131404 thru end */
214633444Sbostic }, rd52_sizes[8] = {
214733444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
214833444Sbostic 	9766,	15884,		/* B=blk 15884 thru 25649 */
214933444Sbostic 	-1,	0,		/* C=blk 0 thru end */
215033444Sbostic 	0,	0,		/* D=unused */
215133444Sbostic 	0,	0,		/* E=unused */
215233444Sbostic 	0,	0,		/* F=unused */
215333444Sbostic 	-1,	25650,		/* G=blk 25650 thru end */
215433444Sbostic 	0,	0,		/* H=unused */
215533444Sbostic }, rd53_sizes[8] = {
215633444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
215733444Sbostic 	33440,	15884,		/* B=blk 15884 thru 49323 */
215833444Sbostic 	-1,	0,		/* C=blk 0 thru end */
215933444Sbostic 	0,	0,		/* D=unused */
216033444Sbostic 	33440,	0,		/* E=blk 0 thru 33439 */
216133444Sbostic 	-1,	33440,		/* F=blk 33440 thru end */
216233444Sbostic 	-1,	49324,		/* G=blk 49324 thru end */
216333444Sbostic 	-1,	15884,		/* H=blk 15884 thru end */
216433444Sbostic }, rx50_sizes[8] = {
216533444Sbostic 	800,	0,		/* A=blk 0 thru 799 */
216633444Sbostic 	0,	0,
216733444Sbostic 	-1,	0,		/* C=blk 0 thru end */
216833444Sbostic 	0,	0,
216933444Sbostic 	0,	0,
217033444Sbostic 	0,	0,
217133444Sbostic 	0,	0,
217233444Sbostic 	0,	0,
217330536Skarels };
217430536Skarels 
217532523Sbostic /*
217633444Sbostic  * Media ID decoding table.
217732523Sbostic  */
217832523Sbostic struct	udatypes {
217933444Sbostic 	u_long	ut_id;		/* media drive ID */
218032523Sbostic 	char	*ut_name;	/* drive type name */
218132523Sbostic 	struct	size *ut_sizes;	/* partition tables */
218232523Sbostic 	int	ut_nsectors, ut_ntracks, ut_ncylinders;
218332523Sbostic } udatypes[] = {
218433444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 60), "ra60", ra60_sizes, 42, 4, 2382 },
218533444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 70), "ra70", ra70_sizes, 33, 11, 1507 },
218633444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 80), "ra80", ra80_sizes, 31, 14, 559 },
218733444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 81), "ra81", ra81_sizes, 51, 14, 1248 },
218833444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 82), "ra82", ra82_sizes, 57, 14, 1423 },
218933444Sbostic 	{ MSCP_MKDRIVE2('R', 'C', 25), "rc25-removable",
219033444Sbostic 						rc25_sizes, 42, 4, 302 },
219133444Sbostic 	{ MSCP_MKDRIVE3('R', 'C', 'F', 25), "rc25-fixed",
219233444Sbostic 						rc25_sizes, 42, 4, 302 },
219333444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 52), "rd52", rd52_sizes, 18, 7, 480 },
219433444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 53), "rd53", rd53_sizes, 18, 8, 963 },
219533444Sbostic 	{ MSCP_MKDRIVE2('R', 'X', 50), "rx50", rx50_sizes, 10, 1, 80 },
219633444Sbostic 	0
219732523Sbostic };
219832523Sbostic 
219932523Sbostic #define NTYPES (sizeof(udatypes) / sizeof(*udatypes))
220032523Sbostic 
220132523Sbostic udamaptype(unit, lp)
220232523Sbostic 	int unit;
220330536Skarels 	register struct disklabel *lp;
220430536Skarels {
220532523Sbostic 	register struct udatypes *ut;
220632523Sbostic 	register struct size *sz;
220730536Skarels 	register struct partition *pp;
220832523Sbostic 	register char *p;
220932523Sbostic 	register int i;
221032523Sbostic 	register struct ra_info *ra = &ra_info[unit];
221130536Skarels 
221233444Sbostic 	i = MSCP_MEDIA_DRIVE(ra->ra_mediaid);
221333444Sbostic 	for (ut = udatypes; ut->ut_id; ut++)
221436036Skarels 		if (ut->ut_id == i &&
221536036Skarels 		    ut->ut_nsectors == ra->ra_geom.rg_nsectors &&
221636036Skarels 		    ut->ut_ntracks == ra->ra_geom.rg_ntracks &&
221736036Skarels 		    ut->ut_ncylinders == ra->ra_geom.rg_ncyl)
221833444Sbostic 			goto found;
221933444Sbostic 
222033444Sbostic 	/* not one we know; fake up a label for the whole drive */
222136036Skarels 	uda_makefakelabel(ra, lp);
222233444Sbostic 	i = ra->ra_mediaid;	/* print the port type too */
222336036Skarels 	addlog(": no partition table for %c%c %c%c%c%d, size %d;\n\
222434283Skarels using (s,t,c)=(%d,%d,%d)",
222534283Skarels 		MSCP_MID_CHAR(4, i), MSCP_MID_CHAR(3, i),
222633444Sbostic 		MSCP_MID_CHAR(2, i), MSCP_MID_CHAR(1, i),
222736036Skarels 		MSCP_MID_CHAR(0, i), MSCP_MID_NUM(i), lp->d_secperunit,
222836036Skarels 		lp->d_nsectors, lp->d_ntracks, lp->d_ncylinders);
222934283Skarels 	if (!cold)
223034283Skarels 		addlog("\n");
223133444Sbostic 	return (0);
223233444Sbostic found:
223332523Sbostic 	p = ut->ut_name;
223432523Sbostic 	for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++)
223532523Sbostic 		lp->d_typename[i] = *p++;
223632523Sbostic 	lp->d_typename[i] = 0;
223732523Sbostic 	sz = ut->ut_sizes;
223832523Sbostic 	lp->d_nsectors = ut->ut_nsectors;
223932523Sbostic 	lp->d_ntracks = ut->ut_ntracks;
224032523Sbostic 	lp->d_ncylinders = ut->ut_ncylinders;
224130536Skarels 	lp->d_npartitions = 8;
224230536Skarels 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
224332523Sbostic 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) {
224432523Sbostic 		pp->p_offset = sz->blkoff;
224532523Sbostic 		if ((pp->p_size = sz->nblocks) == (u_long)-1)
224632523Sbostic 			pp->p_size = ra->ra_dsize - sz->blkoff;
224730536Skarels 	}
224830536Skarels 	return (1);
224930536Skarels }
225032523Sbostic #endif /* COMPAT_42 */
225136036Skarels 
225236036Skarels /*
225336036Skarels  * Construct a label for a drive from geometry information
225436036Skarels  * if we have no better information.
225536036Skarels  */
225636036Skarels uda_makefakelabel(ra, lp)
225736036Skarels 	register struct ra_info *ra;
225836036Skarels 	register struct disklabel *lp;
225936036Skarels {
226036036Skarels 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
226136036Skarels 	lp->d_ntracks = ra->ra_geom.rg_ntracks;
226236036Skarels 	lp->d_ncylinders = ra->ra_geom.rg_ncyl;
226336036Skarels 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
226436036Skarels 	bcopy("ra??", lp->d_typename, sizeof("ra??"));
226536036Skarels 	lp->d_npartitions = 1;
226636036Skarels 	lp->d_partitions[0].p_offset = 0;
226736036Skarels 	lp->d_partitions[0].p_size = lp->d_secperunit;
226836036Skarels }
226932523Sbostic #endif /* NUDA > 0 */
2270