xref: /csrg-svn/sys/vax/uba/uda.c (revision 40045)
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*40045Smarc  *	@(#)uda.c	7.25 (Berkeley) 02/08/90
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"
76*40045Smarc #include "tsleep.h"
774743Swnj 
7837512Smckusick #include "machine/pte.h"
7934283Skarels 
808482Sroot #include "../vax/cpu.h"
8117553Skarels #include "ubareg.h"
8217553Skarels #include "ubavar.h"
838613Sroot 
8432523Sbostic #define	NRSP	(1 << NRSPL2)
8532523Sbostic #define	NCMD	(1 << NCMDL2)
868613Sroot 
8732523Sbostic #include "udareg.h"
888482Sroot #include "../vax/mscp.h"
8932523Sbostic #include "../vax/mscpvar.h"
9032523Sbostic #include "../vax/mtpr.h"
914743Swnj 
9232523Sbostic /*
9332523Sbostic  * UDA communications area and MSCP packet pools, per controller.
9432523Sbostic  */
9532523Sbostic struct	uda {
9632523Sbostic 	struct	udaca uda_ca;		/* communications area */
9732523Sbostic 	struct	mscp uda_rsp[NRSP];	/* response packets */
9832523Sbostic 	struct	mscp uda_cmd[NCMD];	/* command packets */
994743Swnj } uda[NUDA];
1004743Swnj 
10132523Sbostic /*
10232523Sbostic  * Software status, per controller.
10332523Sbostic  */
10432523Sbostic struct	uda_softc {
10532523Sbostic 	struct	uda *sc_uda;	/* Unibus address of uda struct */
10632523Sbostic 	short	sc_state;	/* UDA50 state; see below */
10732523Sbostic 	short	sc_flags;	/* flags; see below */
10832523Sbostic 	int	sc_micro;	/* microcode revision */
10932523Sbostic 	int	sc_ivec;	/* interrupt vector address */
11036036Skarels 	short	sc_ipl;		/* interrupt priority, Q-bus */
11132523Sbostic 	struct	mscp_info sc_mi;/* MSCP info (per mscpvar.h) */
11232523Sbostic #ifndef POLLSTATS
11332523Sbostic 	int	sc_wticks;	/* watchdog timer ticks */
11432523Sbostic #else
11532523Sbostic 	short	sc_wticks;
11632523Sbostic 	short	sc_ncmd;
11732523Sbostic #endif
11832523Sbostic } uda_softc[NUDA];
11924742Sbloom 
12032523Sbostic #ifdef POLLSTATS
12132523Sbostic struct udastats {
12232523Sbostic 	int	ncmd;
12332523Sbostic 	int	cmd[NCMD + 1];
12432523Sbostic } udastats = { NCMD + 1 };
12532523Sbostic #endif
12617553Skarels 
12732523Sbostic /*
12832523Sbostic  * Controller states
12932523Sbostic  */
13032523Sbostic #define	ST_IDLE		0	/* uninitialised */
13132523Sbostic #define	ST_STEP1	1	/* in `STEP 1' */
13232523Sbostic #define	ST_STEP2	2	/* in `STEP 2' */
13332523Sbostic #define	ST_STEP3	3	/* in `STEP 3' */
13432523Sbostic #define	ST_SETCHAR	4	/* in `Set Controller Characteristics' */
13532523Sbostic #define	ST_RUN		5	/* up and running */
1364743Swnj 
13732523Sbostic /*
13832523Sbostic  * Flags
13932523Sbostic  */
14032523Sbostic #define	SC_MAPPED	0x01	/* mapped in Unibus I/O space */
14132523Sbostic #define	SC_INSTART	0x02	/* inside udastart() */
14232523Sbostic #define	SC_GRIPED	0x04	/* griped about cmd ring too small */
14332523Sbostic #define	SC_INSLAVE	0x08	/* inside udaslave() */
14432523Sbostic #define	SC_DOWAKE	0x10	/* wakeup when ctlr init done */
14532523Sbostic #define	SC_STARTPOLL	0x20	/* need to initiate polling */
14612421Ssam 
14732523Sbostic /*
14832523Sbostic  * Device to unit number and partition and back
14932523Sbostic  */
15032523Sbostic #define	UNITSHIFT	3
15132523Sbostic #define	UNITMASK	7
15232523Sbostic #define	udaunit(dev)	(minor(dev) >> UNITSHIFT)
15332523Sbostic #define	udapart(dev)	(minor(dev) & UNITMASK)
15432523Sbostic #define	udaminor(u, p)	(((u) << UNITSHIFT) | (p))
1554743Swnj 
15617553Skarels /*
15732523Sbostic  * Drive status, per drive
15817553Skarels  */
15932523Sbostic struct ra_info {
16032523Sbostic 	daddr_t	ra_dsize;	/* size in sectors */
16133444Sbostic /*	u_long	ra_type;	/* drive type */
16232523Sbostic 	u_long	ra_mediaid;	/* media id */
16332523Sbostic 	int	ra_state;	/* open/closed state */
16432523Sbostic 	struct	ra_geom {	/* geometry information */
16532523Sbostic 		u_short	rg_nsectors;	/* sectors/track */
16632523Sbostic 		u_short	rg_ngroups;	/* track groups */
16732523Sbostic 		u_short	rg_ngpc;	/* groups/cylinder */
16832523Sbostic 		u_short	rg_ntracks;	/* ngroups*ngpc */
16932523Sbostic 		u_short	rg_ncyl;	/* ra_dsize/ntracks/nsectors */
17032523Sbostic #ifdef notyet
17132523Sbostic 		u_short	rg_rctsize;	/* size of rct */
17232523Sbostic 		u_short	rg_rbns;	/* replacement blocks per track */
17332523Sbostic 		u_short	rg_nrct;	/* number of rct copies */
17432523Sbostic #endif
17532523Sbostic 	} ra_geom;
17633443Skarels 	int	ra_wlabel;	/* label sector is currently writable */
17732523Sbostic 	u_long	ra_openpart;	/* partitions open */
17832523Sbostic 	u_long	ra_bopenpart;	/* block partitions open */
17932523Sbostic 	u_long	ra_copenpart;	/* character partitions open */
18032523Sbostic } ra_info[NRA];
18117553Skarels 
18230536Skarels /*
18330536Skarels  * Software state, per drive
18430536Skarels  */
18530536Skarels #define	CLOSED		0
18630536Skarels #define	WANTOPEN	1
18730536Skarels #define	RDLABEL		2
18830536Skarels #define	OPEN		3
18930536Skarels #define	OPENRAW		4
19017553Skarels 
19132523Sbostic /*
19232523Sbostic  * Definition of the driver for autoconf.
19332523Sbostic  */
19432523Sbostic int	udaprobe(), udaslave(), udaattach(), udadgo(), udaintr();
19532523Sbostic struct	uba_ctlr *udaminfo[NUDA];
19632523Sbostic struct	uba_device *udadinfo[NRA];
19732523Sbostic struct	disklabel udalabel[NRA];
19817553Skarels 
19932523Sbostic u_short	udastd[] = { 0772150, 0772550, 0777550, 0 };
20032523Sbostic struct	uba_driver udadriver =
20132523Sbostic  { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda",
20232523Sbostic    udaminfo };
20317553Skarels 
20432523Sbostic /*
20532523Sbostic  * More driver definitions, for generic MSCP code.
20632523Sbostic  */
20732523Sbostic int	udadgram(), udactlrdone(), udaunconf(), udaiodone();
20832523Sbostic int	udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb();
2094743Swnj 
21032523Sbostic struct	buf udautab[NRA];	/* per drive transfer queue */
2114743Swnj 
21232523Sbostic struct	mscp_driver udamscpdriver =
21334524Skarels  { MAXUNIT, NRA, UNITSHIFT, udautab, udalabel, udadinfo,
21432523Sbostic    udadgram, udactlrdone, udaunconf, udaiodone,
21532523Sbostic    udaonline, udagotstatus, udareplace, udaioerror, udabb,
21632523Sbostic    "uda", "ra" };
21732523Sbostic 
21832523Sbostic /*
21932523Sbostic  * Miscellaneous private variables.
22032523Sbostic  */
22132523Sbostic char	udasr_bits[] = UDASR_BITS;
22232523Sbostic 
22332523Sbostic struct	uba_device *udaip[NUDA][MAXUNIT];
22432523Sbostic 				/* inverting pointers: ctlr & unit => Unibus
22532523Sbostic 				   device pointer */
22632523Sbostic 
22732523Sbostic int	udaburst[NUDA] = { 0 };	/* burst size, per UDA50, zero => default;
22832523Sbostic 				   in data space so patchable via adb */
22932523Sbostic 
23032523Sbostic struct	mscp udaslavereply;	/* get unit status response packet, set
23132523Sbostic 				   for udaslave by udaunconf, via udaintr */
23232523Sbostic 
23332523Sbostic static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr
23432523Sbostic 				   info to slave routine; instead, we remember
23532523Sbostic 				   the last ctlr argument to probe */
23632523Sbostic 
23732523Sbostic int	udawstart, udawatch();	/* watchdog timer */
23832523Sbostic 
23932523Sbostic /*
24032523Sbostic  * Externals
24132523Sbostic  */
24232523Sbostic int	wakeup();
24332523Sbostic int	hz;
24432523Sbostic 
24532523Sbostic /*
24632523Sbostic  * Poke at a supposed UDA50 to see if it is there.
24732523Sbostic  * This routine duplicates some of the code in udainit() only
24832523Sbostic  * because autoconf has not set up the right information yet.
24932523Sbostic  * We have to do everything `by hand'.
25032523Sbostic  */
25132523Sbostic udaprobe(reg, ctlr, um)
2524743Swnj 	caddr_t reg;
2534743Swnj 	int ctlr;
25432523Sbostic 	struct uba_ctlr *um;
2554743Swnj {
2564743Swnj 	register int br, cvec;
25732523Sbostic 	register struct uda_softc *sc;
25832523Sbostic 	register struct udadevice *udaddr;
25932523Sbostic 	register struct mscp_info *mi;
26036036Skarels 	int timeout, tries, s;
2614743Swnj 
26232523Sbostic #ifdef VAX750
26332523Sbostic 	/*
26432523Sbostic 	 * The UDA50 wants to share BDPs on 750s, but not on 780s or
26532523Sbostic 	 * 8600s.  (730s have no BDPs anyway.)  Toward this end, we
26632523Sbostic 	 * here set the `keep bdp' flag in the per-driver information
26732523Sbostic 	 * if this is a 750.  (We just need to do it once, but it is
26832523Sbostic 	 * easiest to do it now, for each UDA50.)
26932523Sbostic 	 */
27032523Sbostic 	if (cpu == VAX_750)
27132523Sbostic 		udadriver.ud_keepbdp = 1;
27232523Sbostic #endif
27317553Skarels 
27432523Sbostic 	probeum = um;			/* remember for udaslave() */
2754743Swnj #ifdef lint
27632523Sbostic 	br = 0; cvec = br; br = cvec; udaintr(0);
2774743Swnj #endif
27832523Sbostic 	/*
27932523Sbostic 	 * Set up the controller-specific generic MSCP driver info.
28032523Sbostic 	 * Note that this should really be done in the (nonexistent)
28132523Sbostic 	 * controller attach routine.
28232523Sbostic 	 */
28332523Sbostic 	sc = &uda_softc[ctlr];
28432523Sbostic 	mi = &sc->sc_mi;
28532523Sbostic 	mi->mi_md = &udamscpdriver;
28632523Sbostic 	mi->mi_ctlr = um->um_ctlr;
28732523Sbostic 	mi->mi_tab = &um->um_tab;
28832523Sbostic 	mi->mi_ip = udaip[ctlr];
28932523Sbostic 	mi->mi_cmd.mri_size = NCMD;
29032523Sbostic 	mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc;
29132523Sbostic 	mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd;
29232523Sbostic 	mi->mi_rsp.mri_size = NRSP;
29332523Sbostic 	mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc;
29432523Sbostic 	mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp;
29532523Sbostic 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
29632523Sbostic 
29732523Sbostic 	/*
29832523Sbostic 	 * More controller specific variables.  Again, this should
29932523Sbostic 	 * be in the controller attach routine.
30032523Sbostic 	 */
30132523Sbostic 	if (udaburst[ctlr] == 0)
30232523Sbostic 		udaburst[ctlr] = DEFAULT_BURST;
30332523Sbostic 
30432523Sbostic 	/*
30532523Sbostic 	 * Get an interrupt vector.  Note that even if the controller
30632523Sbostic 	 * does not respond, we keep the vector.  This is not a serious
30732523Sbostic 	 * problem; but it would be easily fixed if we had a controller
30832523Sbostic 	 * attach routine.  Sigh.
30932523Sbostic 	 */
31032523Sbostic 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
31117553Skarels 	udaddr = (struct udadevice *) reg;
31217553Skarels 
31332523Sbostic 	/*
31432523Sbostic 	 * Initialise the controller (partially).  The UDA50 programmer's
31532523Sbostic 	 * manual states that if initialisation fails, it should be retried
31632523Sbostic 	 * at least once, but after a second failure the port should be
31732523Sbostic 	 * considered `down'; it also mentions that the controller should
31832523Sbostic 	 * initialise within ten seconds.  Or so I hear; I have not seen
31932523Sbostic 	 * this manual myself.
32032523Sbostic 	 */
32136036Skarels #ifdef QBA
32236036Skarels 	s = spl6();
32336036Skarels #endif
32432523Sbostic 	tries = 0;
32532523Sbostic again:
32632523Sbostic 	udaddr->udaip = 0;		/* start initialisation */
32732523Sbostic 	timeout = todr() + 1000;	/* timeout in 10 seconds */
32832523Sbostic 	while ((udaddr->udasa & UDA_STEP1) == 0)
32932523Sbostic 		if (todr() > timeout)
33032523Sbostic 			goto bad;
33132523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
33232523Sbostic 		(sc->sc_ivec >> 2);
33332523Sbostic 	while ((udaddr->udasa & UDA_STEP2) == 0)
33432523Sbostic 		if (todr() > timeout)
33532523Sbostic 			goto bad;
33632523Sbostic 
33732523Sbostic 	/* should have interrupted by now */
33835401Stef #ifdef QBA
33936036Skarels 	sc->sc_ipl = br = qbgetpri();
34027254Skridle #endif
34132523Sbostic 	return (sizeof (struct udadevice));
34232523Sbostic bad:
34332523Sbostic 	if (++tries < 2)
34432523Sbostic 		goto again;
34536036Skarels 	splx(s);
34632523Sbostic 	return (0);
3474743Swnj }
3484743Swnj 
34932523Sbostic /*
35032523Sbostic  * Find a slave.  We allow wildcard slave numbers (something autoconf
35132523Sbostic  * is not really prepared to deal with); and we need to know the
35232523Sbostic  * controller number to talk to the UDA.  For the latter, we keep
35332523Sbostic  * track of the last controller probed, since a controller probe
35432523Sbostic  * immediately precedes all slave probes for that controller.  For the
35532523Sbostic  * former, we simply put the unit number into ui->ui_slave after we
35632523Sbostic  * have found one.
35732523Sbostic  *
35832523Sbostic  * Note that by the time udaslave is called, the interrupt vector
35932523Sbostic  * for the UDA50 has been set up (so that udaunconf() will be called).
36032523Sbostic  */
36132523Sbostic udaslave(ui, reg)
36232523Sbostic 	register struct uba_device *ui;
3634743Swnj 	caddr_t reg;
3644743Swnj {
36532523Sbostic 	register struct uba_ctlr *um = probeum;
36632523Sbostic 	register struct mscp *mp;
36732523Sbostic 	register struct uda_softc *sc;
36833443Skarels 	int next = 0, timeout, tries, i;
36917553Skarels 
37032523Sbostic #ifdef lint
37132523Sbostic 	i = 0; i = i;
37232523Sbostic #endif
37332523Sbostic 	/*
37432523Sbostic 	 * Make sure the controller is fully initialised, by waiting
37532523Sbostic 	 * for it if necessary.
37632523Sbostic 	 */
37732523Sbostic 	sc = &uda_softc[um->um_ctlr];
37832523Sbostic 	if (sc->sc_state == ST_RUN)
37932523Sbostic 		goto findunit;
38032523Sbostic 	tries = 0;
38132523Sbostic again:
38232523Sbostic 	if (udainit(ui->ui_ctlr))
38332523Sbostic 		return (0);
38432523Sbostic 	timeout = todr() + 1000;		/* 10 seconds */
38532523Sbostic 	while (todr() < timeout)
38632523Sbostic 		if (sc->sc_state == ST_RUN)	/* made it */
38732523Sbostic 			goto findunit;
38832523Sbostic 	if (++tries < 2)
38932523Sbostic 		goto again;
39032523Sbostic 	printf("uda%d: controller hung\n", um->um_ctlr);
39132523Sbostic 	return (0);
39217553Skarels 
39332523Sbostic 	/*
39432523Sbostic 	 * The controller is all set; go find the unit.  Grab an
39532523Sbostic 	 * MSCP packet and send out a Get Unit Status command, with
39632523Sbostic 	 * the `next unit' modifier if we are looking for a generic
39732523Sbostic 	 * unit.  We set the `in slave' flag so that udaunconf()
39832523Sbostic 	 * knows to copy the response to `udaslavereply'.
39932523Sbostic 	 */
40032523Sbostic findunit:
40132523Sbostic 	udaslavereply.mscp_opcode = 0;
40232523Sbostic 	sc->sc_flags |= SC_INSLAVE;
40332523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL)
40432523Sbostic 		panic("udaslave");		/* `cannot happen' */
40532523Sbostic 	mp->mscp_opcode = M_OP_GETUNITST;
40632523Sbostic 	if (ui->ui_slave == '?') {
40732523Sbostic 		mp->mscp_unit = next;
40832523Sbostic 		mp->mscp_modifier = M_GUM_NEXTUNIT;
40932523Sbostic 	} else {
41032523Sbostic 		mp->mscp_unit = ui->ui_slave;
41132523Sbostic 		mp->mscp_modifier = 0;
41217553Skarels 	}
41332523Sbostic 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
41432523Sbostic 	i = ((struct udadevice *) reg)->udaip;	/* initiate polling */
41532523Sbostic 	mp = &udaslavereply;
41632523Sbostic 	timeout = todr() + 1000;
41732523Sbostic 	while (todr() < timeout)
41832523Sbostic 		if (mp->mscp_opcode)
41932523Sbostic 			goto gotit;
42032523Sbostic 	printf("uda%d: no response to Get Unit Status request\n",
42132523Sbostic 		um->um_ctlr);
42232523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
42332523Sbostic 	return (0);
42432523Sbostic 
42532523Sbostic gotit:
42632523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
42732523Sbostic 
42832523Sbostic 	/*
42932523Sbostic 	 * Got a slave response.  If the unit is there, use it.
43032523Sbostic 	 */
43132523Sbostic 	switch (mp->mscp_status & M_ST_MASK) {
43232523Sbostic 
43332523Sbostic 	case M_ST_SUCCESS:	/* worked */
43432523Sbostic 	case M_ST_AVAILABLE:	/* found another drive */
43532523Sbostic 		break;		/* use it */
43632523Sbostic 
43732523Sbostic 	case M_ST_OFFLINE:
43832523Sbostic 		/*
43932523Sbostic 		 * Figure out why it is off line.  It may be because
44032523Sbostic 		 * it is nonexistent, or because it is spun down, or
44132523Sbostic 		 * for some other reason.
44232523Sbostic 		 */
44332523Sbostic 		switch (mp->mscp_status & ~M_ST_MASK) {
44432523Sbostic 
44532523Sbostic 		case M_OFFLINE_UNKNOWN:
44632523Sbostic 			/*
44732523Sbostic 			 * No such drive, and there are none with
44832523Sbostic 			 * higher unit numbers either, if we are
44932523Sbostic 			 * using M_GUM_NEXTUNIT.
45032523Sbostic 			 */
45132523Sbostic 			return (0);
45232523Sbostic 
45332523Sbostic 		case M_OFFLINE_UNMOUNTED:
45432523Sbostic 			/*
45532523Sbostic 			 * The drive is not spun up.  Use it anyway.
45632523Sbostic 			 *
45732523Sbostic 			 * N.B.: this seems to be a common occurrance
45832523Sbostic 			 * after a power failure.  The first attempt
45932523Sbostic 			 * to bring it on line seems to spin it up
46032523Sbostic 			 * (and thus takes several minutes).  Perhaps
46132523Sbostic 			 * we should note here that the on-line may
46232523Sbostic 			 * take longer than usual.
46332523Sbostic 			 */
46432523Sbostic 			break;
46532523Sbostic 
46632523Sbostic 		default:
46732523Sbostic 			/*
46832523Sbostic 			 * In service, or something else equally unusable.
46932523Sbostic 			 */
47032523Sbostic 			printf("uda%d: unit %d off line: ", um->um_ctlr,
47132523Sbostic 				mp->mscp_unit);
47232523Sbostic 			mscp_printevent(mp);
47332523Sbostic 			goto try_another;
47432523Sbostic 		}
47532523Sbostic 		break;
47632523Sbostic 
47732523Sbostic 	default:
47832523Sbostic 		printf("uda%d: unable to get unit status: ", um->um_ctlr);
47932523Sbostic 		mscp_printevent(mp);
48032523Sbostic 		return (0);
48117553Skarels 	}
48232523Sbostic 
48332523Sbostic 	/*
48432523Sbostic 	 * Does this ever happen?  What (if anything) does it mean?
48532523Sbostic 	 */
48632523Sbostic 	if (mp->mscp_unit < next) {
48732523Sbostic 		printf("uda%d: unit %d, next %d\n",
48832523Sbostic 			um->um_ctlr, mp->mscp_unit, next);
48932523Sbostic 		return (0);
49017553Skarels 	}
49132523Sbostic 
49232523Sbostic 	if (mp->mscp_unit >= MAXUNIT) {
49332523Sbostic 		printf("uda%d: cannot handle unit number %d (max is %d)\n",
49432523Sbostic 			um->um_ctlr, mp->mscp_unit, MAXUNIT - 1);
49532523Sbostic 		return (0);
49632523Sbostic 	}
49732523Sbostic 
49832523Sbostic 	/*
49932523Sbostic 	 * See if we already handle this drive.
50032523Sbostic 	 * (Only likely if ui->ui_slave=='?'.)
50132523Sbostic 	 */
50232523Sbostic 	if (udaip[um->um_ctlr][mp->mscp_unit] != NULL) {
50332523Sbostic try_another:
50432523Sbostic 		if (ui->ui_slave != '?')
50532523Sbostic 			return (0);
50632523Sbostic 		next = mp->mscp_unit + 1;
50732523Sbostic 		goto findunit;
50832523Sbostic 	}
50932523Sbostic 
51032523Sbostic 	/*
51132523Sbostic 	 * Voila!
51232523Sbostic 	 */
51332523Sbostic 	uda_rasave(ui->ui_unit, mp, 0);
51432523Sbostic 	ui->ui_flags = 0;	/* not on line, nor anything else */
51532523Sbostic 	ui->ui_slave = mp->mscp_unit;
51632523Sbostic 	return (1);
5174743Swnj }
5184743Swnj 
51932523Sbostic /*
52032523Sbostic  * Attach a found slave.  Make sure the watchdog timer is running.
521*40045Smarc  * If this disk is being profiled, fill in the `wpms' value (used by
52232523Sbostic  * what?).  Set up the inverting pointer, and attempt to bring the
52332523Sbostic  * drive on line and read its label.
52432523Sbostic  */
52532523Sbostic udaattach(ui)
5264743Swnj 	register struct uba_device *ui;
5274743Swnj {
52832523Sbostic 	register int unit = ui->ui_unit;
52932523Sbostic 
53032523Sbostic 	if (udawstart == 0) {
53132523Sbostic 		timeout(udawatch, (caddr_t) 0, hz);
53232523Sbostic 		udawstart++;
53332523Sbostic 	}
53433444Sbostic 
53533444Sbostic 	/*
53633444Sbostic 	 * Floppies cannot be brought on line unless there is
53733444Sbostic 	 * a disk in the drive.  Since an ONLINE while cold
53833444Sbostic 	 * takes ten seconds to fail, and (when notyet becomes now)
53933444Sbostic 	 * no sensible person will swap to one, we just
54033444Sbostic 	 * defer the ONLINE until someone tries to use the drive.
54133444Sbostic 	 *
54233444Sbostic 	 * THIS ASSUMES THAT DRIVE TYPES ?X? ARE FLOPPIES
54333444Sbostic 	 */
54433444Sbostic 	if (MSCP_MID_ECH(1, ra_info[unit].ra_mediaid) == 'X' - '@') {
54534283Skarels 		printf(": floppy");
54633444Sbostic 		return;
54733444Sbostic 	}
54834649Skarels 	if (ui->ui_dk >= 0)
549*40045Smarc 		dk_wpms[ui->ui_dk] = (60 * 31 * 256);	/* approx */
55032523Sbostic 	udaip[ui->ui_ctlr][ui->ui_slave] = ui;
55133195Sbostic 
55232523Sbostic 	if (uda_rainit(ui, 0))
55334283Skarels 		printf(": offline");
55436036Skarels 	else if (ra_info[unit].ra_state == OPEN) {
55534283Skarels 		printf(": %s, size = %d sectors",
55634283Skarels 		    udalabel[unit].d_typename, ra_info[unit].ra_dsize);
55732523Sbostic #ifdef notyet
55832523Sbostic 		addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]);
55926295Skarels #endif
56030536Skarels 	}
56132523Sbostic }
56232523Sbostic 
56332523Sbostic /*
56432523Sbostic  * Initialise a UDA50.  Return true iff something goes wrong.
56532523Sbostic  */
56632523Sbostic udainit(ctlr)
56732523Sbostic 	int ctlr;
56832523Sbostic {
56932523Sbostic 	register struct uda_softc *sc;
57032523Sbostic 	register struct udadevice *udaddr;
57132523Sbostic 	struct uba_ctlr *um;
57232523Sbostic 	int timo, ubinfo;
57332523Sbostic 
57432523Sbostic 	sc = &uda_softc[ctlr];
57532523Sbostic 	um = udaminfo[ctlr];
57632523Sbostic 	if ((sc->sc_flags & SC_MAPPED) == 0) {
57732523Sbostic 		/*
57832523Sbostic 		 * Map the communication area and command and
57932523Sbostic 		 * response packets into Unibus space.
58032523Sbostic 		 */
58132523Sbostic 		ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr],
58232523Sbostic 			sizeof (struct uda), UBA_CANTWAIT);
58332523Sbostic 		if (ubinfo == 0) {
58432523Sbostic 			printf("uda%d: uballoc map failed\n", ctlr);
58532523Sbostic 			return (-1);
58632523Sbostic 		}
58736036Skarels 		sc->sc_uda = (struct uda *) UBAI_ADDR(ubinfo);
58832523Sbostic 		sc->sc_flags |= SC_MAPPED;
58932523Sbostic 	}
59032523Sbostic 
59130536Skarels 	/*
59232523Sbostic 	 * While we are thinking about it, reset the next command
59332523Sbostic 	 * and response indicies.
59430536Skarels 	 */
59532523Sbostic 	sc->sc_mi.mi_cmd.mri_next = 0;
59632523Sbostic 	sc->sc_mi.mi_rsp.mri_next = 0;
59732523Sbostic 
59832523Sbostic 	/*
59932523Sbostic 	 * Start up the hardware initialisation sequence.
60032523Sbostic 	 */
60132523Sbostic #define	STEP0MASK	(UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \
60232523Sbostic 			 UDA_STEP1 | UDA_NV)
60332523Sbostic 
60432523Sbostic 	sc->sc_state = ST_IDLE;	/* in case init fails */
60533444Sbostic 	udaddr = (struct udadevice *)um->um_addr;
60632523Sbostic 	udaddr->udaip = 0;
60732523Sbostic 	timo = todr() + 1000;
60832523Sbostic 	while ((udaddr->udasa & STEP0MASK) == 0) {
60932523Sbostic 		if (todr() > timo) {
61032523Sbostic 			printf("uda%d: timeout during init\n", ctlr);
61132523Sbostic 			return (-1);
61232523Sbostic 		}
61332523Sbostic 	}
61432523Sbostic 	if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) {
61532523Sbostic 		printf("uda%d: init failed, sa=%b\n", ctlr,
61632523Sbostic 			udaddr->udasa, udasr_bits);
61733444Sbostic 		udasaerror(um, 0);
61832523Sbostic 		return (-1);
61932523Sbostic 	}
62032523Sbostic 
62132523Sbostic 	/*
62232523Sbostic 	 * Success!  Record new state, and start step 1 initialisation.
62332523Sbostic 	 * The rest is done in the interrupt handler.
62432523Sbostic 	 */
62532523Sbostic 	sc->sc_state = ST_STEP1;
62632523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
62732523Sbostic 	    (sc->sc_ivec >> 2);
62832523Sbostic 	return (0);
6294743Swnj }
6304743Swnj 
6314743Swnj /*
63232523Sbostic  * Open a drive.
6334743Swnj  */
63432523Sbostic /*ARGSUSED*/
63532523Sbostic udaopen(dev, flag, fmt)
6364743Swnj 	dev_t dev;
63730773Skarels 	int flag, fmt;
6384743Swnj {
63932523Sbostic 	register int unit;
6404743Swnj 	register struct uba_device *ui;
6414743Swnj 	register struct uda_softc *sc;
64230536Skarels 	register struct disklabel *lp;
64330536Skarels 	register struct partition *pp;
64430916Skarels 	register struct ra_info *ra;
64532523Sbostic 	int s, i, part, mask, error = 0;
64630536Skarels 	daddr_t start, end;
64730536Skarels 
64832523Sbostic 	/*
64932523Sbostic 	 * Make sure this is a reasonable open request.
65032523Sbostic 	 */
65132523Sbostic 	unit = udaunit(dev);
65232523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0)
6538576Sroot 		return (ENXIO);
65432523Sbostic 
65532523Sbostic 	/*
65632523Sbostic 	 * Make sure the controller is running, by (re)initialising it if
65732523Sbostic 	 * necessary.
65832523Sbostic 	 */
6594743Swnj 	sc = &uda_softc[ui->ui_ctlr];
6605434Sroot 	s = spl5();
66132523Sbostic 	if (sc->sc_state != ST_RUN) {
66232523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) {
66332523Sbostic 			splx(s);
6648576Sroot 			return (EIO);
66517553Skarels 		}
66632523Sbostic 		/*
66732523Sbostic 		 * In case it does not come up, make sure we will be
66832523Sbostic 		 * restarted in 10 seconds.  This corresponds to the
66932523Sbostic 		 * 10 second timeouts in udaprobe() and udaslave().
67032523Sbostic 		 */
67132523Sbostic 		sc->sc_flags |= SC_DOWAKE;
67232523Sbostic 		timeout(wakeup, (caddr_t) sc, 10 * hz);
67332523Sbostic 		sleep((caddr_t) sc, PRIBIO);
67432523Sbostic 		if (sc->sc_state != ST_RUN) {
67532523Sbostic 			splx(s);
67632523Sbostic 			printf("uda%d: controller hung\n", ui->ui_ctlr);
67732523Sbostic 			return (EIO);
67832523Sbostic 		}
67932523Sbostic 		untimeout(wakeup, (caddr_t) sc);
6804743Swnj 	}
68132523Sbostic 
68232523Sbostic 	/*
68332523Sbostic 	 * Wait for the state to settle
68432523Sbostic 	 */
68532523Sbostic 	ra = &ra_info[unit];
68632523Sbostic 	while (ra->ra_state != OPEN && ra->ra_state != OPENRAW &&
68732523Sbostic 	    ra->ra_state != CLOSED)
688*40045Smarc 		tsleep((caddr_t)ra, PZERO + 1, SLP_UDA_OPN, 0);
68932523Sbostic 
69032523Sbostic 	/*
69132523Sbostic 	 * If not on line, or we are not sure of the label, reinitialise
69232523Sbostic 	 * the drive.
69332523Sbostic 	 */
69432523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0 ||
69532523Sbostic 	    (ra->ra_state != OPEN && ra->ra_state != OPENRAW))
69632523Sbostic 		error = uda_rainit(ui, flag);
69730916Skarels 	splx(s);
69832523Sbostic 	if (error)
69932523Sbostic 		return (error);
70030536Skarels 
70132523Sbostic 	part = udapart(dev);
70232523Sbostic 	lp = &udalabel[unit];
70330536Skarels 	if (part >= lp->d_npartitions)
70430536Skarels 		return (ENXIO);
70530536Skarels 	/*
70632523Sbostic 	 * Warn if a partition is opened that overlaps another
70732523Sbostic 	 * already open, unless either is the `raw' partition
70832523Sbostic 	 * (whole disk).
70930536Skarels 	 */
71032523Sbostic #define	RAWPART		2	/* 'c' partition */	/* XXX */
71132523Sbostic 	mask = 1 << part;
71232523Sbostic 	if ((ra->ra_openpart & mask) == 0 && part != RAWPART) {
71330536Skarels 		pp = &lp->d_partitions[part];
71430536Skarels 		start = pp->p_offset;
71530536Skarels 		end = pp->p_offset + pp->p_size;
71632523Sbostic 		for (pp = lp->d_partitions, i = 0;
71732523Sbostic 		     i < lp->d_npartitions; pp++, i++) {
71830536Skarels 			if (pp->p_offset + pp->p_size <= start ||
71932523Sbostic 			    pp->p_offset >= end || i == RAWPART)
72030536Skarels 				continue;
72132523Sbostic 			if (ra->ra_openpart & (1 << i))
72230536Skarels 				log(LOG_WARNING,
72330536Skarels 				    "ra%d%c: overlaps open partition (%c)\n",
72432523Sbostic 				    unit, part + 'a', i + 'a');
72517553Skarels 		}
72617553Skarels 	}
72730773Skarels 	switch (fmt) {
72830773Skarels 	case S_IFCHR:
72932523Sbostic 		ra->ra_copenpart |= mask;
73030773Skarels 		break;
73130773Skarels 	case S_IFBLK:
73232523Sbostic 		ra->ra_bopenpart |= mask;
73330773Skarels 		break;
73430773Skarels 	}
73532523Sbostic 	ra->ra_openpart |= mask;
7368576Sroot 	return (0);
7374743Swnj }
7384743Swnj 
73933444Sbostic /* ARGSUSED */
74032523Sbostic udaclose(dev, flags, fmt)
74130536Skarels 	dev_t dev;
74230773Skarels 	int flags, fmt;
74330536Skarels {
74432523Sbostic 	register int unit = udaunit(dev);
74530773Skarels 	register struct ra_info *ra = &ra_info[unit];
74632523Sbostic 	int s, mask = (1 << udapart(dev));
74730536Skarels 
74830773Skarels 	switch (fmt) {
74930773Skarels 	case S_IFCHR:
75032523Sbostic 		ra->ra_copenpart &= ~mask;
75130773Skarels 		break;
75230773Skarels 	case S_IFBLK:
75332523Sbostic 		ra->ra_bopenpart &= ~mask;
75430773Skarels 		break;
75530773Skarels 	}
75632523Sbostic 	ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
75732523Sbostic 
75830536Skarels 	/*
75932523Sbostic 	 * Should wait for I/O to complete on this partition even if
76032523Sbostic 	 * others are open, but wait for work on blkflush().
76130536Skarels 	 */
76232523Sbostic 	if (ra->ra_openpart == 0) {
76330536Skarels 		s = spl5();
76432523Sbostic 		while (udautab[unit].b_actf)
76532523Sbostic 			sleep((caddr_t)&udautab[unit], PZERO - 1);
76630536Skarels 		splx(s);
76732523Sbostic 		ra->ra_state = CLOSED;
76833443Skarels 		ra->ra_wlabel = 0;
76930536Skarels 	}
77030773Skarels 	return (0);
77130536Skarels }
77230536Skarels 
7734743Swnj /*
77432523Sbostic  * Initialise a drive.  If it is not already, bring it on line,
77532523Sbostic  * and set a timeout on it in case it fails to respond.
77632523Sbostic  * When on line, read in the pack label.
7774743Swnj  */
77832523Sbostic uda_rainit(ui, flags)
77930536Skarels 	register struct uba_device *ui;
78032523Sbostic 	int flags;
78130536Skarels {
78232523Sbostic 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
78334283Skarels 	register struct disklabel *lp;
78430536Skarels 	register struct mscp *mp;
78532523Sbostic 	register int unit = ui->ui_unit;
78632523Sbostic 	register struct ra_info *ra;
78730773Skarels 	char *msg, *readdisklabel();
78832523Sbostic 	int s, i, udastrategy();
78930536Skarels 	extern int cold;
79030536Skarels 
79132523Sbostic 	ra = &ra_info[unit];
79232523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
79332523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT);
79432523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
79532523Sbostic 		mp->mscp_unit = ui->ui_slave;
79632523Sbostic 		mp->mscp_cmdref = (long)&ui->ui_flags;
79732523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
79832523Sbostic 		ra->ra_state = WANTOPEN;
79932523Sbostic 		if (!cold)
80032523Sbostic 			s = spl5();
80132523Sbostic 		i = ((struct udadevice *)ui->ui_addr)->udaip;
80230536Skarels 
80330916Skarels 		if (cold) {
80432523Sbostic 			i = todr() + 1000;
80532523Sbostic 			while ((ui->ui_flags & UNIT_ONLINE) == 0)
80632523Sbostic 				if (todr() > i)
80732523Sbostic 					break;
80830916Skarels 		} else {
80932523Sbostic 			timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
81032523Sbostic 			sleep((caddr_t)&ui->ui_flags, PSWP + 1);
81132523Sbostic 			splx(s);
81232523Sbostic 			untimeout(wakeup, (caddr_t)&ui->ui_flags);
81330916Skarels 		}
81432523Sbostic 		if (ra->ra_state != OPENRAW) {
81532523Sbostic 			ra->ra_state = CLOSED;
81632523Sbostic 			wakeup((caddr_t)ra);
81730916Skarels 			return (EIO);
81830916Skarels 		}
81930536Skarels 	}
82030536Skarels 
82132523Sbostic 	lp = &udalabel[unit];
82230916Skarels 	lp->d_secsize = DEV_BSIZE;
82332523Sbostic 	lp->d_secperunit = ra->ra_dsize;
82430916Skarels 
82530536Skarels 	if (flags & O_NDELAY)
82630536Skarels 		return (0);
82732523Sbostic 	ra->ra_state = RDLABEL;
82830536Skarels 	/*
82932523Sbostic 	 * Set up default sizes until we have the label, or longer
83032523Sbostic 	 * if there is none.  Set secpercyl, as readdisklabel wants
83138979Skarels 	 * to compute b_cylin (although we do not need it), and set
83238979Skarels 	 * nsectors in case diskerr is called.
83330536Skarels 	 */
83430916Skarels 	lp->d_secpercyl = 1;
83530536Skarels 	lp->d_npartitions = 1;
83636036Skarels 	lp->d_secsize = 512;
83736036Skarels 	lp->d_secperunit = ra->ra_dsize;
83838979Skarels 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
83930536Skarels 	lp->d_partitions[0].p_size = lp->d_secperunit;
84030536Skarels 	lp->d_partitions[0].p_offset = 0;
84132523Sbostic 
84230536Skarels 	/*
84330536Skarels 	 * Read pack label.
84430536Skarels 	 */
84532523Sbostic 	if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) {
84634283Skarels 		if (cold)
84734283Skarels 			printf(": %s", msg);
84834283Skarels 		else
84936036Skarels 			log(LOG_ERR, "ra%d: %s", unit, msg);
85030536Skarels #ifdef COMPAT_42
85132523Sbostic 		if (udamaptype(unit, lp))
85232523Sbostic 			ra->ra_state = OPEN;
85330536Skarels 		else
85432523Sbostic 			ra->ra_state = OPENRAW;
85531022Skarels #else
85632523Sbostic 		ra->ra_state = OPENRAW;
85736036Skarels 		uda_makefakelabel(ra, lp);
85830536Skarels #endif
85930773Skarels 	} else
86032523Sbostic 		ra->ra_state = OPEN;
86130916Skarels 	wakeup((caddr_t)ra);
86230916Skarels 	return (0);
86330536Skarels }
86430536Skarels 
86532523Sbostic /*
86632523Sbostic  * Copy the geometry information for the given ra from a
86732523Sbostic  * GET UNIT STATUS response.  If check, see if it changed.
86832523Sbostic  */
86932523Sbostic uda_rasave(unit, mp, check)
87032523Sbostic 	int unit;
87132523Sbostic 	register struct mscp *mp;
87232523Sbostic 	int check;
87332523Sbostic {
87432523Sbostic 	register struct ra_info *ra = &ra_info[unit];
87532523Sbostic 
87634283Skarels 	if (check && ra->ra_mediaid != mp->mscp_guse.guse_mediaid) {
87733443Skarels 		printf("ra%d: changed types! was %d now %d\n", unit,
87834283Skarels 			ra->ra_mediaid, mp->mscp_guse.guse_mediaid);
87932523Sbostic 		ra->ra_state = CLOSED;	/* ??? */
88032523Sbostic 	}
88133444Sbostic 	/* ra->ra_type = mp->mscp_guse.guse_drivetype; */
88232523Sbostic 	ra->ra_mediaid = mp->mscp_guse.guse_mediaid;
88332523Sbostic 	ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt;
88432523Sbostic 	ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group;
88532523Sbostic 	ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc;
88632523Sbostic 	ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc;
88732523Sbostic 	/* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */
88832523Sbostic #ifdef notyet
88932523Sbostic 	ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize;
89032523Sbostic 	ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt;
89132523Sbostic 	ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct;
89232523Sbostic #endif
89332523Sbostic }
89432523Sbostic 
89532523Sbostic /*
89632523Sbostic  * Queue a transfer request, and if possible, hand it to the controller.
89732523Sbostic  *
89832523Sbostic  * This routine is broken into two so that the internal version
89932523Sbostic  * udastrat1() can be called by the (nonexistent, as yet) bad block
90032523Sbostic  * revectoring routine.
90132523Sbostic  */
90232523Sbostic udastrategy(bp)
9034743Swnj 	register struct buf *bp;
9044743Swnj {
90532523Sbostic 	register int unit;
9064743Swnj 	register struct uba_device *ui;
90732523Sbostic 	register struct ra_info *ra;
90832523Sbostic 	struct partition *pp;
90932523Sbostic 	int p;
9104743Swnj 	daddr_t sz, maxsz;
9114743Swnj 
91232523Sbostic 	/*
91332523Sbostic 	 * Make sure this is a reasonable drive to use.
91432523Sbostic 	 */
91532523Sbostic 	if ((unit = udaunit(bp->b_dev)) >= NRA ||
91632523Sbostic 	    (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 ||
91732523Sbostic 	    (ra = &ra_info[unit])->ra_state == CLOSED) {
91824742Sbloom 		bp->b_error = ENXIO;
9194743Swnj 		goto bad;
92024742Sbloom 	}
92132523Sbostic 
92232523Sbostic 	/*
92332523Sbostic 	 * If drive is open `raw' or reading label, let it at it.
92432523Sbostic 	 */
92532523Sbostic 	if (ra->ra_state < OPEN) {
92632523Sbostic 		udastrat1(bp);
92732523Sbostic 		return;
92824742Sbloom 	}
92932523Sbostic 	p = udapart(bp->b_dev);
93033443Skarels 	if ((ra->ra_openpart & (1 << p)) == 0) {
93133443Skarels 		bp->b_error = ENODEV;
93233443Skarels 		goto bad;
93333443Skarels 	}
93432523Sbostic 
93532523Sbostic 	/*
93632523Sbostic 	 * Determine the size of the transfer, and make sure it is
93732523Sbostic 	 * within the boundaries of the partition.
93832523Sbostic 	 */
93932523Sbostic 	pp = &udalabel[unit].d_partitions[p];
94032523Sbostic 	maxsz = pp->p_size;
94132523Sbostic 	if (pp->p_offset + pp->p_size > ra->ra_dsize)
94232523Sbostic 		maxsz = ra->ra_dsize - pp->p_offset;
94330536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
94433443Skarels 	if (bp->b_blkno + pp->p_offset <= LABELSECTOR &&
94533443Skarels #if LABELSECTOR != 0
94633443Skarels 	    bp->b_blkno + pp->p_offset + sz > LABELSECTOR &&
94733443Skarels #endif
94833443Skarels 	    (bp->b_flags & B_READ) == 0 && ra->ra_wlabel == 0) {
94933443Skarels 		bp->b_error = EROFS;
95033443Skarels 		goto bad;
95133443Skarels 	}
95230536Skarels 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
95332523Sbostic 		/* if exactly at end of disk, return an EOF */
95424787Skarels 		if (bp->b_blkno == maxsz) {
95524787Skarels 			bp->b_resid = bp->b_bcount;
95632523Sbostic 			biodone(bp);
95732523Sbostic 			return;
95824787Skarels 		}
95932523Sbostic 		/* or truncate if part of it fits */
96030536Skarels 		sz = maxsz - bp->b_blkno;
96130536Skarels 		if (sz <= 0) {
96232523Sbostic 			bp->b_error = EINVAL;	/* or hang it up */
96330536Skarels 			goto bad;
96430536Skarels 		}
96530536Skarels 		bp->b_bcount = sz << DEV_BSHIFT;
96624742Sbloom 	}
96732523Sbostic 	udastrat1(bp);
96832523Sbostic 	return;
96932523Sbostic bad:
97032523Sbostic 	bp->b_flags |= B_ERROR;
97132523Sbostic 	biodone(bp);
97232523Sbostic }
97332523Sbostic 
97432523Sbostic /*
97532523Sbostic  * Work routine for udastrategy.
97632523Sbostic  */
97732523Sbostic udastrat1(bp)
97832523Sbostic 	register struct buf *bp;
97932523Sbostic {
98032523Sbostic 	register int unit = udaunit(bp->b_dev);
98132523Sbostic 	register struct uba_ctlr *um;
98232523Sbostic 	register struct buf *dp;
98332523Sbostic 	struct uba_device *ui;
98432523Sbostic 	int s = spl5();
98532523Sbostic 
9864743Swnj 	/*
98732523Sbostic 	 * Append the buffer to the drive queue, and if it is not
98832523Sbostic 	 * already there, the drive to the controller queue.  (However,
98932523Sbostic 	 * if the drive queue is marked to be requeued, we must be
99032523Sbostic 	 * awaiting an on line or get unit status command; in this
99132523Sbostic 	 * case, leave it off the controller queue.)
9924743Swnj 	 */
99332523Sbostic 	um = (ui = udadinfo[unit])->ui_mi;
99432523Sbostic 	dp = &udautab[unit];
99532523Sbostic 	APPEND(bp, dp, av_forw);
99632523Sbostic 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
99732523Sbostic 		APPEND(dp, &um->um_tab, b_forw);
99832523Sbostic 		dp->b_active++;
99932523Sbostic 	}
100032523Sbostic 
10014743Swnj 	/*
100232523Sbostic 	 * Start activity on the controller.  Note that unlike other
100332523Sbostic 	 * Unibus drivers, we must always do this, not just when the
100432523Sbostic 	 * controller is not active.
10054743Swnj 	 */
100632523Sbostic 	udastart(um);
10075434Sroot 	splx(s);
10084743Swnj }
10094743Swnj 
101032523Sbostic /*
101132523Sbostic  * Start up whatever transfers we can find.
101232523Sbostic  * Note that udastart() must be called at spl5().
101332523Sbostic  */
101432523Sbostic udastart(um)
10154743Swnj 	register struct uba_ctlr *um;
10164743Swnj {
101732523Sbostic 	register struct uda_softc *sc = &uda_softc[um->um_ctlr];
10184743Swnj 	register struct buf *bp, *dp;
10194743Swnj 	register struct mscp *mp;
102032523Sbostic 	struct uba_device *ui;
10214743Swnj 	struct udadevice *udaddr;
102232523Sbostic 	struct partition *pp;
102332523Sbostic 	int i, sz;
10244743Swnj 
102532523Sbostic #ifdef lint
102632523Sbostic 	i = 0; i = i;
102732523Sbostic #endif
102832523Sbostic 	/*
102932523Sbostic 	 * If it is not running, try (again and again...) to initialise
103032523Sbostic 	 * it.  If it is currently initialising just ignore it for now.
103132523Sbostic 	 */
103232523Sbostic 	if (sc->sc_state != ST_RUN) {
103332523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr))
103432523Sbostic 			printf("uda%d: still hung\n", um->um_ctlr);
103532523Sbostic 		return;
103632523Sbostic 	}
103732523Sbostic 
103832523Sbostic 	/*
103932523Sbostic 	 * If um_cmd is nonzero, this controller is on the Unibus
104032523Sbostic 	 * resource wait queue.  It will not help to try more requests;
104132523Sbostic 	 * instead, when the Unibus unblocks and calls udadgo(), we
104232523Sbostic 	 * will call udastart() again.
104332523Sbostic 	 */
104432523Sbostic 	if (um->um_cmd)
104532523Sbostic 		return;
104632523Sbostic 
104732523Sbostic 	sc->sc_flags |= SC_INSTART;
104832523Sbostic 	udaddr = (struct udadevice *) um->um_addr;
104932523Sbostic 
10504743Swnj loop:
105132523Sbostic 	/*
105232523Sbostic 	 * Service the drive at the head of the queue.  It may not
105332523Sbostic 	 * need anything, in which case it might be shutting down
105432523Sbostic 	 * in udaclose().
105532523Sbostic 	 */
105632523Sbostic 	if ((dp = um->um_tab.b_actf) == NULL)
105732523Sbostic 		goto out;
10584743Swnj 	if ((bp = dp->b_actf) == NULL) {
10594743Swnj 		dp->b_active = 0;
10604743Swnj 		um->um_tab.b_actf = dp->b_forw;
106132523Sbostic 		if (ra_info[dp - udautab].ra_openpart == 0)
106232523Sbostic 			wakeup((caddr_t)dp); /* finish close protocol */
106332523Sbostic 		goto loop;
10644743Swnj 	}
106532523Sbostic 
106632523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
106733444Sbostic 		udasaerror(um, 1);
106832523Sbostic 		goto out;
10694743Swnj 	}
107032523Sbostic 
107132523Sbostic 	/*
107232523Sbostic 	 * Get an MSCP packet, then figure out what to do.  If
107332523Sbostic 	 * we cannot get a command packet, the command ring may
107432523Sbostic 	 * be too small:  We should have at least as many command
107532523Sbostic 	 * packets as credits, for best performance.
107632523Sbostic 	 */
107732523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) {
107832523Sbostic 		if (sc->sc_mi.mi_credits > MSCP_MINCREDITS &&
107932523Sbostic 		    (sc->sc_flags & SC_GRIPED) == 0) {
108032523Sbostic 			log(LOG_NOTICE, "uda%d: command ring too small\n",
108132523Sbostic 				um->um_ctlr);
108232523Sbostic 			sc->sc_flags |= SC_GRIPED;/* complain only once */
108317553Skarels 		}
108432523Sbostic 		goto out;
10854743Swnj 	}
10864743Swnj 
108732523Sbostic 	/*
108832523Sbostic 	 * Bring the drive on line if it is not already.  Get its status
108932523Sbostic 	 * if we do not already have it.  Otherwise just start the transfer.
109032523Sbostic 	 */
109132523Sbostic 	ui = udadinfo[udaunit(bp->b_dev)];
109232523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
109332523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
109432523Sbostic 		goto common;
10954743Swnj 	}
109632523Sbostic 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
109732523Sbostic 		mp->mscp_opcode = M_OP_GETUNITST;
109832523Sbostic common:
109932523Sbostic if (ui->ui_flags & UNIT_REQUEUE) panic("udastart");
110032523Sbostic 		/*
110132523Sbostic 		 * Take the drive off the controller queue.  When the
110232523Sbostic 		 * command finishes, make sure the drive is requeued.
110332523Sbostic 		 */
110432523Sbostic 		um->um_tab.b_actf = dp->b_forw;
110532523Sbostic 		dp->b_active = 0;
110632523Sbostic 		ui->ui_flags |= UNIT_REQUEUE;
110732523Sbostic 		mp->mscp_unit = ui->ui_slave;
110832523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
110932523Sbostic 		sc->sc_flags |= SC_STARTPOLL;
111032523Sbostic #ifdef POLLSTATS
111132523Sbostic 		sc->sc_ncmd++;
111225653Skarels #endif
111332523Sbostic 		goto loop;
111417553Skarels 	}
111532523Sbostic 
111632523Sbostic 	pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)];
111732523Sbostic 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
11184743Swnj 	mp->mscp_unit = ui->ui_slave;
111932523Sbostic 	mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset;
112030536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
112132523Sbostic 	mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ?
112232523Sbostic 		(pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount;
112332523Sbostic 	/* mscp_cmdref is filled in by mscp_go() */
11244743Swnj 
11254743Swnj 	/*
112632523Sbostic 	 * Drop the packet pointer into the `command' field so udadgo()
112732523Sbostic 	 * can tell what to start.  If ubago returns 1, we can do another
112832523Sbostic 	 * transfer.  If not, um_cmd will still point at mp, so we will
112932523Sbostic 	 * know that we are waiting for resources.
11304743Swnj 	 */
113132523Sbostic 	um->um_cmd = (int)mp;
113232523Sbostic 	if (ubago(ui))
113332523Sbostic 		goto loop;
113432523Sbostic 
113532523Sbostic 	/*
113632523Sbostic 	 * All done, or blocked in ubago().  If we managed to
113732523Sbostic 	 * issue some commands, start up the beast.
113832523Sbostic 	 */
113932523Sbostic out:
114032523Sbostic 	if (sc->sc_flags & SC_STARTPOLL) {
114132523Sbostic #ifdef POLLSTATS
114232523Sbostic 		udastats.cmd[sc->sc_ncmd]++;
114332523Sbostic 		sc->sc_ncmd = 0;
114432523Sbostic #endif
114533444Sbostic 		i = ((struct udadevice *)um->um_addr)->udaip;
11464743Swnj 	}
114732523Sbostic 	sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL);
114832523Sbostic }
114932523Sbostic 
115032523Sbostic /*
115132523Sbostic  * Start a transfer.
115232523Sbostic  *
115332523Sbostic  * If we are not called from within udastart(), we must have been
115432523Sbostic  * blocked, so call udastart to do more requests (if any).  If
115532523Sbostic  * this calls us again immediately we will not recurse, because
115632523Sbostic  * that time we will be in udastart().  Clever....
115732523Sbostic  */
115832523Sbostic udadgo(um)
115932523Sbostic 	register struct uba_ctlr *um;
116032523Sbostic {
116132523Sbostic 	struct uda_softc *sc = &uda_softc[um->um_ctlr];
116232523Sbostic 	struct mscp *mp = (struct mscp *)um->um_cmd;
116332523Sbostic 
116432523Sbostic 	um->um_tab.b_active++;	/* another transfer going */
116532523Sbostic 
11664743Swnj 	/*
116732523Sbostic 	 * Fill in the MSCP packet and move the buffer to the
116832523Sbostic 	 * I/O wait queue.  Mark the controller as no longer on
116932523Sbostic 	 * the resource queue, and remember to initiate polling.
11704743Swnj 	 */
117136036Skarels 	mp->mscp_seq.seq_buffer = UBAI_ADDR(um->um_ubinfo) |
117232523Sbostic 		(UBAI_BDP(um->um_ubinfo) << 24);
117332523Sbostic 	mscp_go(&sc->sc_mi, mp, um->um_ubinfo);
117432523Sbostic 	um->um_cmd = 0;
117532523Sbostic 	um->um_ubinfo = 0;	/* tyke it awye */
117632523Sbostic 	sc->sc_flags |= SC_STARTPOLL;
117732523Sbostic #ifdef POLLSTATS
117832523Sbostic 	sc->sc_ncmd++;
117932523Sbostic #endif
118032523Sbostic 	if ((sc->sc_flags & SC_INSTART) == 0)
118132523Sbostic 		udastart(um);
11824743Swnj }
11834743Swnj 
118432523Sbostic udaiodone(mi, bp, info)
118532523Sbostic 	register struct mscp_info *mi;
118632523Sbostic 	struct buf *bp;
118732523Sbostic 	int info;
118832523Sbostic {
118932523Sbostic 	register struct uba_ctlr *um = udaminfo[mi->mi_ctlr];
119032523Sbostic 
119132523Sbostic 	um->um_ubinfo = info;
119232523Sbostic 	ubadone(um);
119332523Sbostic 	biodone(bp);
119432523Sbostic 	if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab)
119532523Sbostic 		ubarelse(um->um_ubanum, &um->um_bdp);
119632523Sbostic 	um->um_tab.b_active--;	/* another transfer done */
119732523Sbostic }
119832523Sbostic 
119933444Sbostic static struct saerr {
120033444Sbostic 	int	code;		/* error code (including UDA_ERR) */
120133444Sbostic 	char	*desc;		/* what it means: Efoo => foo error */
120233444Sbostic } saerr[] = {
120333444Sbostic 	{ 0100001, "Eunibus packet read" },
120433444Sbostic 	{ 0100002, "Eunibus packet write" },
120533444Sbostic 	{ 0100003, "EUDA ROM and RAM parity" },
120633444Sbostic 	{ 0100004, "EUDA RAM parity" },
120733444Sbostic 	{ 0100005, "EUDA ROM parity" },
120833444Sbostic 	{ 0100006, "Eunibus ring read" },
120933444Sbostic 	{ 0100007, "Eunibus ring write" },
121033444Sbostic 	{ 0100010, " unibus interrupt master failure" },
121133444Sbostic 	{ 0100011, "Ehost access timeout" },
121233444Sbostic 	{ 0100012, " host exceeded command limit" },
121333444Sbostic 	{ 0100013, " unibus bus master failure" },
121433444Sbostic 	{ 0100014, " DM XFC fatal error" },
121533444Sbostic 	{ 0100015, " hardware timeout of instruction loop" },
121633444Sbostic 	{ 0100016, " invalid virtual circuit id" },
121733444Sbostic 	{ 0100017, "Eunibus interrupt write" },
121833444Sbostic 	{ 0104000, "Efatal sequence" },
121933444Sbostic 	{ 0104040, " D proc ALU" },
122033444Sbostic 	{ 0104041, "ED proc control ROM parity" },
122133444Sbostic 	{ 0105102, "ED proc w/no BD#2 or RAM parity" },
122233444Sbostic 	{ 0105105, "ED proc RAM buffer" },
122333444Sbostic 	{ 0105152, "ED proc SDI" },
122433444Sbostic 	{ 0105153, "ED proc write mode wrap serdes" },
122533444Sbostic 	{ 0105154, "ED proc read mode serdes, RSGEN & ECC" },
122633444Sbostic 	{ 0106040, "EU proc ALU" },
122733444Sbostic 	{ 0106041, "EU proc control reg" },
122833444Sbostic 	{ 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" },
122933444Sbostic 	{ 0106047, " U proc const PROM err w/D proc running SDI test" },
123033444Sbostic 	{ 0106055, " unexpected trap" },
123133444Sbostic 	{ 0106071, "EU proc const PROM" },
123233444Sbostic 	{ 0106072, "EU proc control ROM parity" },
123333444Sbostic 	{ 0106200, "Estep 1 data" },
123433444Sbostic 	{ 0107103, "EU proc RAM parity" },
123533444Sbostic 	{ 0107107, "EU proc RAM buffer" },
123633444Sbostic 	{ 0107115, " test count wrong (BD 12)" },
123733444Sbostic 	{ 0112300, "Estep 2" },
123833444Sbostic 	{ 0122240, "ENPR" },
123933444Sbostic 	{ 0122300, "Estep 3" },
124033444Sbostic 	{ 0142300, "Estep 4" },
124133444Sbostic 	{ 0, " unknown error code" }
124233444Sbostic };
124333444Sbostic 
12444743Swnj /*
124533444Sbostic  * If the error bit was set in the controller status register, gripe,
124633444Sbostic  * then (optionally) reset the controller and requeue pending transfers.
12474743Swnj  */
124833444Sbostic udasaerror(um, doreset)
124932523Sbostic 	register struct uba_ctlr *um;
125033444Sbostic 	int doreset;
12514743Swnj {
125233444Sbostic 	register int code = ((struct udadevice *)um->um_addr)->udasa;
125333444Sbostic 	register struct saerr *e;
125432523Sbostic 
125533444Sbostic 	if ((code & UDA_ERR) == 0)
125633444Sbostic 		return;
125733444Sbostic 	for (e = saerr; e->code; e++)
125833444Sbostic 		if (e->code == code)
125933444Sbostic 			break;
126033444Sbostic 	printf("uda%d: controller error, sa=0%o (%s%s)\n",
126133444Sbostic 		um->um_ctlr, code, e->desc + 1,
126233444Sbostic 		*e->desc == 'E' ? " error" : "");
126333444Sbostic 	if (doreset) {
126433444Sbostic 		mscp_requeue(&uda_softc[um->um_ctlr].sc_mi);
126533444Sbostic 		(void) udainit(um->um_ctlr);
126633444Sbostic 	}
126732523Sbostic }
126832523Sbostic 
126932523Sbostic /*
127032523Sbostic  * Interrupt routine.  Depending on the state of the controller,
127132523Sbostic  * continue initialisation, or acknowledge command and response
127232523Sbostic  * interrupts, and process responses.
127332523Sbostic  */
127432523Sbostic udaintr(ctlr)
127532523Sbostic 	int ctlr;
127632523Sbostic {
127732523Sbostic 	register struct uba_ctlr *um = udaminfo[ctlr];
127832523Sbostic 	register struct uda_softc *sc = &uda_softc[ctlr];
127933444Sbostic 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
128032523Sbostic 	register struct uda *ud;
128132523Sbostic 	register struct mscp *mp;
12824743Swnj 	register int i;
12834743Swnj 
128435401Stef #ifdef QBA
128536036Skarels 	splx(sc->sc_ipl);	/* Qbus interrupt protocol is odd */
128627254Skridle #endif
128732523Sbostic 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
128832523Sbostic 
128932523Sbostic 	/*
129032523Sbostic 	 * Combinations during steps 1, 2, and 3: STEPnMASK
129132523Sbostic 	 * corresponds to which bits should be tested;
129232523Sbostic 	 * STEPnGOOD corresponds to the pattern that should
129332523Sbostic 	 * appear after the interrupt from STEPn initialisation.
129432523Sbostic 	 * All steps test the bits in ALLSTEPS.
129532523Sbostic 	 */
129632523Sbostic #define	ALLSTEPS	(UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1)
129732523Sbostic 
129832523Sbostic #define	STEP1MASK	(ALLSTEPS | UDA_IE | UDA_NCNRMASK)
129932523Sbostic #define	STEP1GOOD	(UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2)
130032523Sbostic 
130132523Sbostic #define	STEP2MASK	(ALLSTEPS | UDA_IE | UDA_IVECMASK)
130232523Sbostic #define	STEP2GOOD	(UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2))
130332523Sbostic 
130432523Sbostic #define	STEP3MASK	ALLSTEPS
130532523Sbostic #define	STEP3GOOD	UDA_STEP4
130632523Sbostic 
13074743Swnj 	switch (sc->sc_state) {
130832523Sbostic 
130932523Sbostic 	case ST_IDLE:
131032523Sbostic 		/*
131132523Sbostic 		 * Ignore unsolicited interrupts.
131232523Sbostic 		 */
131332523Sbostic 		log(LOG_WARNING, "uda%d: stray intr\n", ctlr);
13144743Swnj 		return;
13154743Swnj 
131632523Sbostic 	case ST_STEP1:
131732523Sbostic 		/*
131832523Sbostic 		 * Begin step two initialisation.
131932523Sbostic 		 */
132032523Sbostic 		if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) {
132132523Sbostic 			i = 1;
132232523Sbostic initfailed:
132332523Sbostic 			printf("uda%d: init step %d failed, sa=%b\n",
132432523Sbostic 				ctlr, i, udaddr->udasa, udasr_bits);
132533444Sbostic 			udasaerror(um, 0);
132632523Sbostic 			sc->sc_state = ST_IDLE;
132732523Sbostic 			if (sc->sc_flags & SC_DOWAKE) {
132832523Sbostic 				sc->sc_flags &= ~SC_DOWAKE;
132933444Sbostic 				wakeup((caddr_t)sc);
133032523Sbostic 			}
13314743Swnj 			return;
13324743Swnj 		}
133333444Sbostic 		udaddr->udasa = (int)&sc->sc_uda->uda_ca.ca_rspdsc[0] |
133432523Sbostic 			(cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0);
133532523Sbostic 		sc->sc_state = ST_STEP2;
13364743Swnj 		return;
13374743Swnj 
133832523Sbostic 	case ST_STEP2:
133932523Sbostic 		/*
134032523Sbostic 		 * Begin step 3 initialisation.
134132523Sbostic 		 */
134232523Sbostic 		if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) {
134332523Sbostic 			i = 2;
134432523Sbostic 			goto initfailed;
13454743Swnj 		}
134633444Sbostic 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16;
134732523Sbostic 		sc->sc_state = ST_STEP3;
13484743Swnj 		return;
13494743Swnj 
135032523Sbostic 	case ST_STEP3:
135132523Sbostic 		/*
135232523Sbostic 		 * Set controller characteristics (finish initialisation).
135332523Sbostic 		 */
135432523Sbostic 		if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) {
135532523Sbostic 			i = 3;
135632523Sbostic 			goto initfailed;
13574743Swnj 		}
135832523Sbostic 		i = udaddr->udasa & 0xff;
135932523Sbostic 		if (i != sc->sc_micro) {
136032523Sbostic 			sc->sc_micro = i;
136132523Sbostic 			printf("uda%d: version %d model %d\n",
136232523Sbostic 				ctlr, i & 0xf, i >> 4);
136332523Sbostic 		}
136432523Sbostic 
136517553Skarels 		/*
136632523Sbostic 		 * Present the burst size, then remove it.  Why this
136732523Sbostic 		 * should be done this way, I have no idea.
136832523Sbostic 		 *
136932523Sbostic 		 * Note that this assumes udaburst[ctlr] > 0.
137017553Skarels 		 */
137132523Sbostic 		udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2;
13724743Swnj 		udaddr->udasa = UDA_GO;
137332523Sbostic 		printf("uda%d: DMA burst size set to %d\n",
137432523Sbostic 			ctlr, udaburst[ctlr]);
13754743Swnj 
137632523Sbostic 		udainitds(ctlr);	/* initialise data structures */
137732523Sbostic 
13784743Swnj 		/*
137932523Sbostic 		 * Before we can get a command packet, we need some
138032523Sbostic 		 * credits.  Fake some up to keep mscp_getcp() happy,
138132523Sbostic 		 * get a packet, and cancel all credits (the right
138232523Sbostic 		 * number should come back in the response to the
138332523Sbostic 		 * SCC packet).
13844743Swnj 		 */
138532523Sbostic 		sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1;
138632523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT);
138732523Sbostic 		if (mp == NULL)	/* `cannot happen' */
138832523Sbostic 			panic("udaintr");
138932523Sbostic 		sc->sc_mi.mi_credits = 0;
139032523Sbostic 		mp->mscp_opcode = M_OP_SETCTLRC;
139132523Sbostic 		mp->mscp_unit = 0;
139232523Sbostic 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
139332523Sbostic 			M_CF_THIS;
139432523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
139532523Sbostic 		i = udaddr->udaip;
139632523Sbostic 		sc->sc_state = ST_SETCHAR;
13974743Swnj 		return;
13984743Swnj 
139932523Sbostic 	case ST_SETCHAR:
140032523Sbostic 	case ST_RUN:
140132523Sbostic 		/*
140232523Sbostic 		 * Handle Set Ctlr Characteristics responses and operational
140332523Sbostic 		 * responses (via mscp_dorsp).
140432523Sbostic 		 */
14054743Swnj 		break;
14064743Swnj 
14074743Swnj 	default:
140832523Sbostic 		printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state);
140932523Sbostic 		panic("udastate");
14104743Swnj 	}
14114743Swnj 
141232523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
141333444Sbostic 		udasaerror(um, 1);
141432523Sbostic 		return;
14154743Swnj 	}
14164743Swnj 
141732523Sbostic 	ud = &uda[ctlr];
141832523Sbostic 
14194743Swnj 	/*
142032523Sbostic 	 * Handle buffer purge requests.
14214743Swnj 	 */
14224743Swnj 	if (ud->uda_ca.ca_bdp) {
142326372Skarels 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
14244743Swnj 		ud->uda_ca.ca_bdp = 0;
142532523Sbostic 		udaddr->udasa = 0;	/* signal purge complete */
14264743Swnj 	}
14274743Swnj 
14284743Swnj 	/*
142932523Sbostic 	 * Check for response and command ring transitions.
14304743Swnj 	 */
14314743Swnj 	if (ud->uda_ca.ca_rspint) {
14324743Swnj 		ud->uda_ca.ca_rspint = 0;
143332523Sbostic 		mscp_dorsp(&sc->sc_mi);
14344743Swnj 	}
14354743Swnj 	if (ud->uda_ca.ca_cmdint) {
14364743Swnj 		ud->uda_ca.ca_cmdint = 0;
143732523Sbostic 		MSCP_DOCMD(&sc->sc_mi);
14384743Swnj 	}
143932523Sbostic 	udastart(um);
14404743Swnj }
14414743Swnj 
14424743Swnj /*
144332523Sbostic  * Initialise the various data structures that control the UDA50.
144417553Skarels  */
144532523Sbostic udainitds(ctlr)
144632523Sbostic 	int ctlr;
144732523Sbostic {
144832523Sbostic 	register struct uda *ud = &uda[ctlr];
144932523Sbostic 	register struct uda *uud = uda_softc[ctlr].sc_uda;
145032523Sbostic 	register struct mscp *mp;
145132523Sbostic 	register int i;
14524743Swnj 
145332523Sbostic 	for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) {
145432523Sbostic 		ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
145532523Sbostic 			(long)&uud->uda_rsp[i].mscp_cmdref;
145632523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i];
145732523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
14584743Swnj 	}
145932523Sbostic 	for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) {
146032523Sbostic 		ud->uda_ca.ca_cmddsc[i] = MSCP_INT |
146132523Sbostic 			(long)&uud->uda_cmd[i].mscp_cmdref;
146232523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i];
146332523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
146432523Sbostic 	}
14654743Swnj }
14664743Swnj 
14674743Swnj /*
146833444Sbostic  * Handle an error datagram.
14694743Swnj  */
147032523Sbostic udadgram(mi, mp)
147132523Sbostic 	struct mscp_info *mi;
147232523Sbostic 	struct mscp *mp;
14734743Swnj {
147417553Skarels 
147532523Sbostic 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
147633444Sbostic 	/*
147733444Sbostic 	 * SDI status information bytes 10 and 11 are the microprocessor
147833444Sbostic 	 * error code and front panel code respectively.  These vary per
147933444Sbostic 	 * drive type and are printed purely for field service information.
148033444Sbostic 	 */
148133444Sbostic 	if (mp->mscp_format == M_FM_SDI)
148233444Sbostic 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
148333444Sbostic 			mp->mscp_erd.erd_sdistat[10],
148433444Sbostic 			mp->mscp_erd.erd_sdistat[11]);
148532523Sbostic }
148617553Skarels 
148732523Sbostic /*
148832523Sbostic  * The Set Controller Characteristics command finished.
148932523Sbostic  * Record the new state of the controller.
149032523Sbostic  */
149132523Sbostic udactlrdone(mi, mp)
149232523Sbostic 	register struct mscp_info *mi;
149332523Sbostic 	struct mscp *mp;
149432523Sbostic {
149532523Sbostic 	register struct uda_softc *sc = &uda_softc[mi->mi_ctlr];
149617553Skarels 
149732523Sbostic 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
149832523Sbostic 		sc->sc_state = ST_RUN;
149932523Sbostic 	else {
150032523Sbostic 		printf("uda%d: SETCTLRC failed: ",
150132523Sbostic 			mi->mi_ctlr, mp->mscp_status);
150232523Sbostic 		mscp_printevent(mp);
150332523Sbostic 		sc->sc_state = ST_IDLE;
15044743Swnj 	}
150532523Sbostic 	if (sc->sc_flags & SC_DOWAKE) {
150632523Sbostic 		sc->sc_flags &= ~SC_DOWAKE;
150732523Sbostic 		wakeup((caddr_t)sc);
15086964Ssam 	}
15094743Swnj }
15104743Swnj 
15114743Swnj /*
151232523Sbostic  * Received a response from an as-yet unconfigured drive.  Configure it
151332523Sbostic  * in, if possible.
15144743Swnj  */
151532523Sbostic udaunconf(mi, mp)
151632523Sbostic 	struct mscp_info *mi;
151732523Sbostic 	register struct mscp *mp;
15184743Swnj {
15194743Swnj 
152017553Skarels 	/*
152132523Sbostic 	 * If it is a slave response, copy it to udaslavereply for
152232523Sbostic 	 * udaslave() to look at.
152317553Skarels 	 */
152432523Sbostic 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
152532523Sbostic 	    (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) {
152632523Sbostic 		udaslavereply = *mp;
152732523Sbostic 		return (MSCP_DONE);
15284743Swnj 	}
152932523Sbostic 
153032523Sbostic 	/*
153132523Sbostic 	 * Otherwise, it had better be an available attention response.
153232523Sbostic 	 */
153332523Sbostic 	if (mp->mscp_opcode != M_OP_AVAILATTN)
153432523Sbostic 		return (MSCP_FAILED);
153532523Sbostic 
153632523Sbostic 	/* do what autoconf does */
153732523Sbostic 	return (MSCP_FAILED);	/* not yet, arwhite, not yet */
15384743Swnj }
15394743Swnj 
154032523Sbostic /*
154132523Sbostic  * A drive came on line.  Check its type and size.  Return DONE if
154232523Sbostic  * we think the drive is truly on line.  In any case, awaken anyone
154332523Sbostic  * sleeping on the drive on-line-ness.
154432523Sbostic  */
154532523Sbostic udaonline(ui, mp)
154632523Sbostic 	register struct uba_device *ui;
154732523Sbostic 	struct mscp *mp;
15484743Swnj {
154932523Sbostic 	register struct ra_info *ra = &ra_info[ui->ui_unit];
15504743Swnj 
155132523Sbostic 	wakeup((caddr_t)&ui->ui_flags);
155232523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
155336036Skarels 		if (!cold)
155436036Skarels 			printf("uda%d: ra%d", ui->ui_ctlr, ui->ui_unit);
155536036Skarels 		printf(": attempt to bring on line failed: ");
155632523Sbostic 		mscp_printevent(mp);
155732523Sbostic 		ra->ra_state = CLOSED;
155832523Sbostic 		return (MSCP_FAILED);
155932523Sbostic 	}
156032523Sbostic 
156132523Sbostic 	ra->ra_state = OPENRAW;
156232523Sbostic 	ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize;
156334283Skarels 	if (!cold)
156434283Skarels 		printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit,
156534283Skarels 		    ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize);
156632523Sbostic 	/* can now compute ncyl */
156732523Sbostic 	ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks /
156832523Sbostic 		ra->ra_geom.rg_nsectors;
156932523Sbostic 	return (MSCP_DONE);
15704743Swnj }
15714743Swnj 
157232523Sbostic /*
157332523Sbostic  * We got some (configured) unit's status.  Return DONE if it succeeded.
157432523Sbostic  */
157532523Sbostic udagotstatus(ui, mp)
157632523Sbostic 	register struct uba_device *ui;
157732523Sbostic 	register struct mscp *mp;
15784743Swnj {
15794743Swnj 
158032523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
158132523Sbostic 		printf("uda%d: attempt to get status for ra%d failed: ",
158232523Sbostic 			ui->ui_ctlr, ui->ui_unit);
158332523Sbostic 		mscp_printevent(mp);
158432523Sbostic 		return (MSCP_FAILED);
158532523Sbostic 	}
158632523Sbostic 	/* record for (future) bad block forwarding and whatever else */
158732523Sbostic 	uda_rasave(ui->ui_unit, mp, 1);
158832523Sbostic 	return (MSCP_DONE);
15894743Swnj }
15904743Swnj 
159132523Sbostic /*
159232523Sbostic  * A transfer failed.  We get a chance to fix or restart it.
159332523Sbostic  * Need to write the bad block forwaring code first....
159432523Sbostic  */
159532523Sbostic /*ARGSUSED*/
159632523Sbostic udaioerror(ui, mp, bp)
159732523Sbostic 	register struct uba_device *ui;
159832523Sbostic 	register struct mscp *mp;
159932523Sbostic 	struct buf *bp;
16004743Swnj {
16014743Swnj 
160232523Sbostic 	if (mp->mscp_flags & M_EF_BBLKR) {
160332523Sbostic 		/*
160432523Sbostic 		 * A bad block report.  Eventually we will
160532523Sbostic 		 * restart this transfer, but for now, just
160632523Sbostic 		 * log it and give up.
160732523Sbostic 		 */
160832523Sbostic 		log(LOG_ERR, "ra%d: bad block report: %d%s\n",
160932523Sbostic 			ui->ui_unit, mp->mscp_seq.seq_lbn,
161032523Sbostic 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
161132523Sbostic 	} else {
161232523Sbostic 		/*
161332523Sbostic 		 * What the heck IS a `serious exception' anyway?
161432523Sbostic 		 * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION
161532523Sbostic 		 * FOR THEIR OWN CONTROLLERS.
161632523Sbostic 		 */
161732523Sbostic 		if (mp->mscp_flags & M_EF_SEREX)
161832523Sbostic 			log(LOG_ERR, "ra%d: serious exception reported\n",
161932523Sbostic 				ui->ui_unit);
16204743Swnj 	}
162132523Sbostic 	return (MSCP_FAILED);
16224743Swnj }
16234743Swnj 
162432523Sbostic /*
162532523Sbostic  * A replace operation finished.
162632523Sbostic  */
162732523Sbostic /*ARGSUSED*/
162832523Sbostic udareplace(ui, mp)
162932523Sbostic 	struct uba_device *ui;
163032523Sbostic 	struct mscp *mp;
16314743Swnj {
163217553Skarels 
163332523Sbostic 	panic("udareplace");
16344743Swnj }
163517553Skarels 
163632523Sbostic /*
163732523Sbostic  * A bad block related operation finished.
163832523Sbostic  */
163932523Sbostic /*ARGSUSED*/
164032523Sbostic udabb(ui, mp, bp)
164132523Sbostic 	struct uba_device *ui;
164232523Sbostic 	struct mscp *mp;
164332523Sbostic 	struct buf *bp;
164417553Skarels {
164517553Skarels 
164632523Sbostic 	panic("udabb");
164717553Skarels }
164817553Skarels 
164932523Sbostic 
165032523Sbostic /*
165132523Sbostic  * I/O controls.
165232523Sbostic  */
165332523Sbostic udaioctl(dev, cmd, data, flag)
165412511Ssam 	dev_t dev;
165530536Skarels 	int cmd;
165630536Skarels 	caddr_t data;
165730536Skarels 	int flag;
165812511Ssam {
165932523Sbostic 	register int unit = udaunit(dev);
166030536Skarels 	register struct disklabel *lp;
166133443Skarels 	register struct ra_info *ra = &ra_info[unit];
166234638Skarels 	int error = 0;
166312511Ssam 
166432523Sbostic 	lp = &udalabel[unit];
166530536Skarels 
166630536Skarels 	switch (cmd) {
166730536Skarels 
166830536Skarels 	case DIOCGDINFO:
166930536Skarels 		*(struct disklabel *)data = *lp;
167030536Skarels 		break;
167130536Skarels 
167230773Skarels 	case DIOCGPART:
167330773Skarels 		((struct partinfo *)data)->disklab = lp;
167430773Skarels 		((struct partinfo *)data)->part =
167532523Sbostic 		    &lp->d_partitions[udapart(dev)];
167630536Skarels 		break;
167730536Skarels 
167830536Skarels 	case DIOCSDINFO:
167930536Skarels 		if ((flag & FWRITE) == 0)
168030536Skarels 			error = EBADF;
168130536Skarels 		else
168232574Skarels 			error = setdisklabel(lp, (struct disklabel *)data,
168334283Skarels 			    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart);
168430536Skarels 		break;
168530536Skarels 
168633443Skarels 	case DIOCWLABEL:
168733443Skarels 		if ((flag & FWRITE) == 0)
168833443Skarels 			error = EBADF;
168933443Skarels 		else
169033443Skarels 			ra->ra_wlabel = *(int *)data;
169133443Skarels 		break;
169233443Skarels 
169332574Skarels 	case DIOCWDINFO:
169432574Skarels 		if ((flag & FWRITE) == 0)
169532523Sbostic 			error = EBADF;
169632574Skarels 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
169734283Skarels 		    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart)) == 0) {
169834638Skarels 			int wlab;
169934638Skarels 
170034283Skarels 			ra->ra_state = OPEN;
170134638Skarels 			/* simulate opening partition 0 so write succeeds */
170234638Skarels 			ra->ra_openpart |= (1 << 0);		/* XXX */
170334638Skarels 			wlab = ra->ra_wlabel;
170434638Skarels 			ra->ra_wlabel = 1;
170532574Skarels 			error = writedisklabel(dev, udastrategy, lp);
170634638Skarels 			ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
170734638Skarels 			ra->ra_wlabel = wlab;
170834283Skarels 		}
170930536Skarels 		break;
171030536Skarels 
171132523Sbostic #ifdef notyet
171232523Sbostic 	case UDAIOCREPLACE:
171332523Sbostic 		/*
171432523Sbostic 		 * Initiate bad block replacement for the given LBN.
171532523Sbostic 		 * (Should we allow modifiers?)
171632523Sbostic 		 */
171732523Sbostic 		error = EOPNOTSUPP;
171832523Sbostic 		break;
171932523Sbostic 
172032523Sbostic 	case UDAIOCGMICRO:
172132523Sbostic 		/*
172232523Sbostic 		 * Return the microcode revision for the UDA50 running
172332523Sbostic 		 * this drive.
172432523Sbostic 		 */
172533444Sbostic 		*(int *)data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro;
172632523Sbostic 		break;
172732523Sbostic #endif
172832523Sbostic 
172930536Skarels 	default:
173030536Skarels 		error = ENOTTY;
173130536Skarels 		break;
173230536Skarels 	}
173332523Sbostic 	return (error);
173432523Sbostic }
173532523Sbostic 
173632523Sbostic /*
173732523Sbostic  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
173832523Sbostic  * on that Unibus, and requeue outstanding I/O.
173932523Sbostic  */
174032523Sbostic udareset(uban)
174132523Sbostic 	int uban;
174232523Sbostic {
174332523Sbostic 	register struct uba_ctlr *um;
174432523Sbostic 	register struct uda_softc *sc;
174532523Sbostic 	register int ctlr;
174632523Sbostic 
174732523Sbostic 	for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) {
174832523Sbostic 		if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban ||
174932523Sbostic 		    um->um_alive == 0)
175032523Sbostic 			continue;
175132523Sbostic 		printf(" uda%d", ctlr);
175232523Sbostic 
175332523Sbostic 		/*
175432523Sbostic 		 * Our BDP (if any) is gone; our command (if any) is
175532523Sbostic 		 * flushed; the device is no longer mapped; and the
175632523Sbostic 		 * UDA50 is not yet initialised.
175732523Sbostic 		 */
175832523Sbostic 		if (um->um_bdp) {
175932523Sbostic 			printf("<%d>", UBAI_BDP(um->um_bdp));
176032523Sbostic 			um->um_bdp = 0;
176132523Sbostic 		}
176232523Sbostic 		um->um_ubinfo = 0;
176332523Sbostic 		um->um_cmd = 0;
176432523Sbostic 		sc->sc_flags &= ~SC_MAPPED;
176532523Sbostic 		sc->sc_state = ST_IDLE;
176632523Sbostic 
176732523Sbostic 		/* reset queues and requeue pending transfers */
176832523Sbostic 		mscp_requeue(&sc->sc_mi);
176932523Sbostic 
177032523Sbostic 		/*
177132523Sbostic 		 * If it fails to initialise we will notice later and
177232523Sbostic 		 * try again (and again...).  Do not call udastart()
177332523Sbostic 		 * here; it will be done after the controller finishes
177432523Sbostic 		 * initialisation.
177532523Sbostic 		 */
177632523Sbostic 		if (udainit(ctlr))
177732523Sbostic 			printf(" (hung)");
177832523Sbostic 	}
177932523Sbostic }
178032523Sbostic 
178132523Sbostic /*
178232523Sbostic  * Watchdog timer:  If the controller is active, and no interrupts
178332523Sbostic  * have occurred for 30 seconds, assume it has gone away.
178432523Sbostic  */
178532523Sbostic udawatch()
178632523Sbostic {
178732523Sbostic 	register int i;
178832523Sbostic 	register struct uba_ctlr *um;
178932523Sbostic 	register struct uda_softc *sc;
179032523Sbostic 
179132523Sbostic 	timeout(udawatch, (caddr_t) 0, hz);	/* every second */
179232523Sbostic 	for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) {
179332523Sbostic 		if ((um = udaminfo[i]) == 0 || !um->um_alive)
179432523Sbostic 			continue;
179532523Sbostic 		if (sc->sc_state == ST_IDLE)
179632523Sbostic 			continue;
179732523Sbostic 		if (sc->sc_state == ST_RUN && !um->um_tab.b_active)
179832523Sbostic 			sc->sc_wticks = 0;
179932523Sbostic 		else if (++sc->sc_wticks >= 30) {
180032523Sbostic 			sc->sc_wticks = 0;
180132523Sbostic 			printf("uda%d: lost interrupt\n", i);
180232523Sbostic 			ubareset(um->um_ubanum);
180332523Sbostic 		}
180432523Sbostic 	}
180532523Sbostic }
180632523Sbostic 
180732523Sbostic /*
180832523Sbostic  * Do a panic dump.  We set up the controller for one command packet
180932523Sbostic  * and one response packet, for which we use `struct uda1'.
181032523Sbostic  */
181132523Sbostic struct	uda1 {
181232523Sbostic 	struct	uda1ca uda1_ca;	/* communications area */
181332523Sbostic 	struct	mscp uda1_rsp;	/* response packet */
181432523Sbostic 	struct	mscp uda1_cmd;	/* command packet */
181532523Sbostic } uda1;
181632523Sbostic 
181732523Sbostic #define	DBSIZE	32		/* dump 16K at a time */
181832523Sbostic 
181932523Sbostic udadump(dev)
182032523Sbostic 	dev_t dev;
182132523Sbostic {
182232523Sbostic 	struct udadevice *udaddr;
182332523Sbostic 	struct uda1 *ud_ubaddr;
182432523Sbostic 	char *start;
182532523Sbostic 	int num, blk, unit, maxsz, blkoff, reg;
182632523Sbostic 	struct partition *pp;
182732523Sbostic 	register struct uba_regs *uba;
182832523Sbostic 	register struct uba_device *ui;
182932523Sbostic 	register struct uda1 *ud;
183032523Sbostic 	register struct pte *io;
183132523Sbostic 	register int i;
183232523Sbostic 
183332523Sbostic 	/*
183432523Sbostic 	 * Make sure the device is a reasonable place on which to dump.
183532523Sbostic 	 */
183632523Sbostic 	unit = udaunit(dev);
183732523Sbostic 	if (unit >= NRA)
183832523Sbostic 		return (ENXIO);
183933444Sbostic #define	phys(cast, addr)	((cast) ((int)addr & 0x7fffffff))
184032523Sbostic 	ui = phys(struct uba_device *, udadinfo[unit]);
184132523Sbostic 	if (ui == NULL || ui->ui_alive == 0)
184232523Sbostic 		return (ENXIO);
184332523Sbostic 
184432523Sbostic 	/*
184532523Sbostic 	 * Find and initialise the UBA; get the physical address of the
184632523Sbostic 	 * device registers, and of communications area and command and
184732523Sbostic 	 * response packet.
184832523Sbostic 	 */
184932523Sbostic 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
185032523Sbostic 	ubainit(uba);
185132523Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
185232523Sbostic 	ud = phys(struct uda1 *, &uda1);
185332523Sbostic 
185432523Sbostic 	/*
185532523Sbostic 	 * Map the ca+packets into Unibus I/O space so the UDA50 can get
185632523Sbostic 	 * at them.  Use the registers at the end of the Unibus map (since
185732523Sbostic 	 * we will use the registers at the beginning to map the memory
185832523Sbostic 	 * we are dumping).
185932523Sbostic 	 */
186032523Sbostic 	num = btoc(sizeof(struct uda1)) + 1;
186132523Sbostic 	reg = NUBMREG - num;
186232523Sbostic 	io = &uba->uba_map[reg];
186332523Sbostic 	for (i = 0; i < num; i++)
186432523Sbostic 		*(int *)io++ = UBAMR_MRV | (btop(ud) + i);
186532523Sbostic 	ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9));
186632523Sbostic 
186732523Sbostic 	/*
186832523Sbostic 	 * Initialise the controller, with one command and one response
186932523Sbostic 	 * packet.
187032523Sbostic 	 */
187132523Sbostic 	udaddr->udaip = 0;
187232523Sbostic 	if (udadumpwait(udaddr, UDA_STEP1))
187332523Sbostic 		return (EFAULT);
187432523Sbostic 	udaddr->udasa = UDA_ERR;
187532523Sbostic 	if (udadumpwait(udaddr, UDA_STEP2))
187632523Sbostic 		return (EFAULT);
187732523Sbostic 	udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc;
187832523Sbostic 	if (udadumpwait(udaddr, UDA_STEP3))
187932523Sbostic 		return (EFAULT);
188032523Sbostic 	udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16;
188132523Sbostic 	if (udadumpwait(udaddr, UDA_STEP4))
188232523Sbostic 		return (EFAULT);
188332523Sbostic 	uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff;
188432523Sbostic 	udaddr->udasa = UDA_GO;
188532523Sbostic 
188632523Sbostic 	/*
188732523Sbostic 	 * Set up the command and response descriptor, then set the
188832523Sbostic 	 * controller characteristics and bring the drive on line.
188932523Sbostic 	 * Note that all uninitialised locations in uda1_cmd are zero.
189032523Sbostic 	 */
189132523Sbostic 	ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref;
189232523Sbostic 	ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref;
189332523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */
189432523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */
189532523Sbostic 	if (udadumpcmd(M_OP_SETCTLRC, ud, ui))
189632523Sbostic 		return (EFAULT);
189732523Sbostic 	ud->uda1_cmd.mscp_unit = ui->ui_slave;
189832523Sbostic 	if (udadumpcmd(M_OP_ONLINE, ud, ui))
189932523Sbostic 		return (EFAULT);
190032523Sbostic 
190132523Sbostic 	pp = phys(struct partition *,
190232523Sbostic 	    &udalabel[unit].d_partitions[udapart(dev)]);
190332523Sbostic 	maxsz = pp->p_size;
190432523Sbostic 	blkoff = pp->p_offset;
190532523Sbostic 
190632523Sbostic 	/*
190732523Sbostic 	 * Dump all of physical memory, or as much as will fit in the
190832523Sbostic 	 * space provided.
190932523Sbostic 	 */
191032523Sbostic 	start = 0;
191132523Sbostic 	num = maxfree;
191232523Sbostic 	if (dumplo + num >= maxsz)
191332523Sbostic 		num = maxsz - dumplo;
191432523Sbostic 	blkoff += dumplo;
191532523Sbostic 
191632523Sbostic 	/*
191732523Sbostic 	 * Write out memory, DBSIZE pages at a time.
191832523Sbostic 	 * N.B.: this code depends on the fact that the sector
191932523Sbostic 	 * size == the page size.
192032523Sbostic 	 */
192132523Sbostic 	while (num > 0) {
192232523Sbostic 		blk = num > DBSIZE ? DBSIZE : num;
192332523Sbostic 		io = uba->uba_map;
192432523Sbostic 		/*
192532523Sbostic 		 * Map in the pages to write, leaving an invalid entry
192632523Sbostic 		 * at the end to guard against wild Unibus transfers.
192732523Sbostic 		 * Then do the write.
192832523Sbostic 		 */
192932523Sbostic 		for (i = 0; i < blk; i++)
193033444Sbostic 			*(int *)io++ = UBAMR_MRV | (btop(start) + i);
193133444Sbostic 		*(int *)io = 0;
193232523Sbostic 		ud->uda1_cmd.mscp_unit = ui->ui_slave;
193332523Sbostic 		ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
193432523Sbostic 		ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
193532523Sbostic 		if (udadumpcmd(M_OP_WRITE, ud, ui))
193632523Sbostic 			return (EIO);
193732523Sbostic 		start += blk << PGSHIFT;
193832523Sbostic 		num -= blk;
193932523Sbostic 	}
194032523Sbostic 	return (0);		/* made it! */
194132523Sbostic }
194232523Sbostic 
194332523Sbostic /*
194432523Sbostic  * Wait for some of the bits in `bits' to come on.  If the error bit
194532523Sbostic  * comes on, or ten seconds pass without response, return true (error).
194632523Sbostic  */
194732523Sbostic udadumpwait(udaddr, bits)
194832523Sbostic 	register struct udadevice *udaddr;
194932523Sbostic 	register int bits;
195032523Sbostic {
195132523Sbostic 	register int timo = todr() + 1000;
195232523Sbostic 
195332523Sbostic 	while ((udaddr->udasa & bits) == 0) {
195432523Sbostic 		if (udaddr->udasa & UDA_ERR) {
195532523Sbostic 			printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
195632523Sbostic 			return (1);
195732523Sbostic 		}
195832523Sbostic 		if (todr() >= timo) {
195932523Sbostic 			printf("timeout\ndump ");
196032523Sbostic 			return (1);
196132523Sbostic 		}
196232523Sbostic 	}
196330536Skarels 	return (0);
196430536Skarels }
196530536Skarels 
196632523Sbostic /*
196732523Sbostic  * Feed a command to the UDA50, wait for its response, and return
196832523Sbostic  * true iff something went wrong.
196932523Sbostic  */
197032523Sbostic udadumpcmd(op, ud, ui)
197132523Sbostic 	int op;
197232523Sbostic 	register struct uda1 *ud;
197332523Sbostic 	struct uba_device *ui;
197432523Sbostic {
197532523Sbostic 	register struct udadevice *udaddr;
197632523Sbostic 	register int n;
197732523Sbostic #define mp (&ud->uda1_rsp)
197832523Sbostic 
197933444Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
198032523Sbostic 	ud->uda1_cmd.mscp_opcode = op;
198132523Sbostic 	ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN;
198232523Sbostic 	ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN;
198332523Sbostic 	ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
198432523Sbostic 	ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
198532523Sbostic 	if (udaddr->udasa & UDA_ERR) {
198632523Sbostic 		printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
198732523Sbostic 		return (1);
198832523Sbostic 	}
198932523Sbostic 	n = udaddr->udaip;
199032523Sbostic 	n = todr() + 1000;
199132523Sbostic 	for (;;) {
199232523Sbostic 		if (todr() > n) {
199332523Sbostic 			printf("timeout\ndump ");
199432523Sbostic 			return (1);
199532523Sbostic 		}
199632523Sbostic 		if (ud->uda1_ca.ca_cmdint)
199732523Sbostic 			ud->uda1_ca.ca_cmdint = 0;
199832523Sbostic 		if (ud->uda1_ca.ca_rspint == 0)
199932523Sbostic 			continue;
200032523Sbostic 		ud->uda1_ca.ca_rspint = 0;
200132523Sbostic 		if (mp->mscp_opcode == (op | M_OP_END))
200232523Sbostic 			break;
200332523Sbostic 		printf("\n");
200432523Sbostic 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
200532523Sbostic 
200632523Sbostic 		case MSCPT_SEQ:
200732523Sbostic 			printf("sequential");
200832523Sbostic 			break;
200932523Sbostic 
201032523Sbostic 		case MSCPT_DATAGRAM:
201132523Sbostic 			mscp_decodeerror("uda", ui->ui_ctlr, mp);
201232523Sbostic 			printf("datagram");
201332523Sbostic 			break;
201432523Sbostic 
201532523Sbostic 		case MSCPT_CREDITS:
201632523Sbostic 			printf("credits");
201732523Sbostic 			break;
201832523Sbostic 
201932523Sbostic 		case MSCPT_MAINTENANCE:
202032523Sbostic 			printf("maintenance");
202132523Sbostic 			break;
202232523Sbostic 
202332523Sbostic 		default:
202432523Sbostic 			printf("unknown (type 0x%x)",
202532523Sbostic 				MSCP_MSGTYPE(mp->mscp_msgtc));
202632523Sbostic 			break;
202732523Sbostic 		}
202832523Sbostic 		printf(" ignored\ndump ");
202932523Sbostic 		ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
203032523Sbostic 	}
203132523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
203232523Sbostic 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
203332523Sbostic 			mp->mscp_opcode, mp->mscp_status);
203432523Sbostic 		return (1);
203532523Sbostic 	}
203632523Sbostic 	return (0);
203732523Sbostic #undef mp
203832523Sbostic }
203932523Sbostic 
204032523Sbostic /*
204132523Sbostic  * Return the size of a partition, if known, or -1 if not.
204232523Sbostic  */
204332523Sbostic udasize(dev)
204430536Skarels 	dev_t dev;
204530536Skarels {
204632523Sbostic 	register int unit = udaunit(dev);
204730536Skarels 	register struct uba_device *ui;
204830536Skarels 
204932523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == NULL ||
205032523Sbostic 	    ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 ||
205132523Sbostic 	    ra_info[unit].ra_state != OPEN)
205212511Ssam 		return (-1);
205332523Sbostic 	return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size);
205412511Ssam }
205517553Skarels 
205630536Skarels #ifdef COMPAT_42
205732523Sbostic /*
205832523Sbostic  * Tables mapping unlabelled drives.
205932523Sbostic  */
206030536Skarels struct size {
206130536Skarels 	daddr_t nblocks;
206230536Skarels 	daddr_t blkoff;
206333444Sbostic } ra60_sizes[8] = {
206430536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
206530536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
206630536Skarels 	400176,	0,		/* C=sectors 0 thru 400175 */
206730536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
206830536Skarels 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
206930536Skarels 	350852,	49324,		/* F=sectors 49324 thru 400175 */
207030536Skarels 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
207130536Skarels 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
207233444Sbostic }, ra70_sizes[8] = {
207333444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
207433444Sbostic 	33440,	15972,		/* B=blk 15972 thru 49323 */
207533444Sbostic 	-1,	0,		/* C=blk 0 thru end */
207633444Sbostic 	15884,	341220,		/* D=blk 341220 thru 357103 */
207733444Sbostic 	55936,	357192,		/* E=blk 357192 thru 413127 */
207833444Sbostic 	-1,	413457,		/* F=blk 413457 thru end */
207933444Sbostic 	-1,	341220,		/* G=blk 341220 thru end */
208033444Sbostic 	291346,	49731,		/* H=blk 49731 thru 341076 */
208130536Skarels }, ra80_sizes[8] = {
208230536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
208330536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
208430536Skarels 	242606,	0,		/* C=sectors 0 thru 242605 */
208530536Skarels 	0,	0,		/* D=unused */
208630536Skarels 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
208730536Skarels 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
208830536Skarels 	192696,	49910,		/* G=sectors 49910 thru 242605 */
208930536Skarels 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
209030536Skarels }, ra81_sizes[8] ={
209130536Skarels /*
209230536Skarels  * These are the new standard partition sizes for ra81's.
209330536Skarels  * An RA_COMPAT system is compiled with D, E, and F corresponding
209430536Skarels  * to the 4.2 partitions for G, H, and F respectively.
209530536Skarels  */
209630536Skarels #ifndef	UCBRA
209730536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
209830536Skarels 	66880,	16422,		/* B=sectors 16422 thru 83301 */
209930536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
210030536Skarels #ifdef RA_COMPAT
210130536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
210230536Skarels 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
210330536Skarels 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
210430536Skarels #else
210530536Skarels 	15884,	375564,		/* D=sectors 375564 thru 391447 */
210630536Skarels 	307200,	391986,		/* E=sectors 391986 thru 699185 */
210730536Skarels 	191352,	699720,		/* F=sectors 699720 thru 891071 */
210830536Skarels #endif RA_COMPAT
210930536Skarels 	515508,	375564,		/* G=sectors 375564 thru 891071 */
211030536Skarels 	291346,	83538,		/* H=sectors 83538 thru 374883 */
211130536Skarels 
211230536Skarels /*
211330536Skarels  * These partitions correspond to the sizes used by sites at Berkeley,
211430536Skarels  * and by those sites that have received copies of the Berkeley driver
211530536Skarels  * with deltas 6.2 or greater (11/15/83).
211630536Skarels  */
211730536Skarels #else UCBRA
211830536Skarels 
211930536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
212030536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
212130536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
212230536Skarels 	15884,	242606,		/* D=sectors 242606 thru 258489 */
212330536Skarels 	307200,	258490,		/* E=sectors 258490 thru 565689 */
212430536Skarels 	325382,	565690,		/* F=sectors 565690 thru 891071 */
212530536Skarels 	648466,	242606,		/* G=sectors 242606 thru 891071 */
212630536Skarels 	193282,	49324,		/* H=sectors 49324 thru 242605 */
212730536Skarels 
212830536Skarels #endif UCBRA
212933444Sbostic }, ra82_sizes[8] = {
213033444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
213133444Sbostic 	66880,	16245,		/* B=blk 16245 thru 83124 */
213233444Sbostic 	-1,	0,		/* C=blk 0 thru end */
213333444Sbostic 	15884,	375345,		/* D=blk 375345 thru 391228 */
213433444Sbostic 	307200,	391590,		/* E=blk 391590 thru 698789 */
213533444Sbostic 	-1,	699390,		/* F=blk 699390 thru end */
213633444Sbostic 	-1,	375345,		/* G=blk 375345 thru end */
213733444Sbostic 	291346,	83790,		/* H=blk 83790 thru 375135 */
213833444Sbostic }, rc25_sizes[8] = {
213933444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
214033444Sbostic 	10032,	15884,		/* B=blk 15884 thru 49323 */
214133444Sbostic 	-1,	0,		/* C=blk 0 thru end */
214233444Sbostic 	0,	0,		/* D=blk 340670 thru 356553 */
214333444Sbostic 	0,	0,		/* E=blk 356554 thru 412489 */
214433444Sbostic 	0,	0,		/* F=blk 412490 thru end */
214533444Sbostic 	-1,	25916,		/* G=blk 49324 thru 131403 */
214633444Sbostic 	0,	0,		/* H=blk 131404 thru end */
214733444Sbostic }, rd52_sizes[8] = {
214833444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
214933444Sbostic 	9766,	15884,		/* B=blk 15884 thru 25649 */
215033444Sbostic 	-1,	0,		/* C=blk 0 thru end */
215133444Sbostic 	0,	0,		/* D=unused */
215233444Sbostic 	0,	0,		/* E=unused */
215333444Sbostic 	0,	0,		/* F=unused */
215433444Sbostic 	-1,	25650,		/* G=blk 25650 thru end */
215533444Sbostic 	0,	0,		/* H=unused */
215633444Sbostic }, rd53_sizes[8] = {
215733444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
215833444Sbostic 	33440,	15884,		/* B=blk 15884 thru 49323 */
215933444Sbostic 	-1,	0,		/* C=blk 0 thru end */
216033444Sbostic 	0,	0,		/* D=unused */
216133444Sbostic 	33440,	0,		/* E=blk 0 thru 33439 */
216233444Sbostic 	-1,	33440,		/* F=blk 33440 thru end */
216333444Sbostic 	-1,	49324,		/* G=blk 49324 thru end */
216433444Sbostic 	-1,	15884,		/* H=blk 15884 thru end */
216533444Sbostic }, rx50_sizes[8] = {
216633444Sbostic 	800,	0,		/* A=blk 0 thru 799 */
216733444Sbostic 	0,	0,
216833444Sbostic 	-1,	0,		/* C=blk 0 thru end */
216933444Sbostic 	0,	0,
217033444Sbostic 	0,	0,
217133444Sbostic 	0,	0,
217233444Sbostic 	0,	0,
217333444Sbostic 	0,	0,
217430536Skarels };
217530536Skarels 
217632523Sbostic /*
217733444Sbostic  * Media ID decoding table.
217832523Sbostic  */
217932523Sbostic struct	udatypes {
218033444Sbostic 	u_long	ut_id;		/* media drive ID */
218132523Sbostic 	char	*ut_name;	/* drive type name */
218232523Sbostic 	struct	size *ut_sizes;	/* partition tables */
218332523Sbostic 	int	ut_nsectors, ut_ntracks, ut_ncylinders;
218432523Sbostic } udatypes[] = {
218533444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 60), "ra60", ra60_sizes, 42, 4, 2382 },
218633444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 70), "ra70", ra70_sizes, 33, 11, 1507 },
218733444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 80), "ra80", ra80_sizes, 31, 14, 559 },
218833444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 81), "ra81", ra81_sizes, 51, 14, 1248 },
218933444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 82), "ra82", ra82_sizes, 57, 14, 1423 },
219033444Sbostic 	{ MSCP_MKDRIVE2('R', 'C', 25), "rc25-removable",
219133444Sbostic 						rc25_sizes, 42, 4, 302 },
219233444Sbostic 	{ MSCP_MKDRIVE3('R', 'C', 'F', 25), "rc25-fixed",
219333444Sbostic 						rc25_sizes, 42, 4, 302 },
219433444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 52), "rd52", rd52_sizes, 18, 7, 480 },
219533444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 53), "rd53", rd53_sizes, 18, 8, 963 },
219633444Sbostic 	{ MSCP_MKDRIVE2('R', 'X', 50), "rx50", rx50_sizes, 10, 1, 80 },
219733444Sbostic 	0
219832523Sbostic };
219932523Sbostic 
220032523Sbostic #define NTYPES (sizeof(udatypes) / sizeof(*udatypes))
220132523Sbostic 
220232523Sbostic udamaptype(unit, lp)
220332523Sbostic 	int unit;
220430536Skarels 	register struct disklabel *lp;
220530536Skarels {
220632523Sbostic 	register struct udatypes *ut;
220732523Sbostic 	register struct size *sz;
220830536Skarels 	register struct partition *pp;
220932523Sbostic 	register char *p;
221032523Sbostic 	register int i;
221132523Sbostic 	register struct ra_info *ra = &ra_info[unit];
221230536Skarels 
221333444Sbostic 	i = MSCP_MEDIA_DRIVE(ra->ra_mediaid);
221433444Sbostic 	for (ut = udatypes; ut->ut_id; ut++)
221536036Skarels 		if (ut->ut_id == i &&
221636036Skarels 		    ut->ut_nsectors == ra->ra_geom.rg_nsectors &&
221736036Skarels 		    ut->ut_ntracks == ra->ra_geom.rg_ntracks &&
221836036Skarels 		    ut->ut_ncylinders == ra->ra_geom.rg_ncyl)
221933444Sbostic 			goto found;
222033444Sbostic 
222133444Sbostic 	/* not one we know; fake up a label for the whole drive */
222236036Skarels 	uda_makefakelabel(ra, lp);
222333444Sbostic 	i = ra->ra_mediaid;	/* print the port type too */
222436036Skarels 	addlog(": no partition table for %c%c %c%c%c%d, size %d;\n\
222534283Skarels using (s,t,c)=(%d,%d,%d)",
222634283Skarels 		MSCP_MID_CHAR(4, i), MSCP_MID_CHAR(3, i),
222733444Sbostic 		MSCP_MID_CHAR(2, i), MSCP_MID_CHAR(1, i),
222836036Skarels 		MSCP_MID_CHAR(0, i), MSCP_MID_NUM(i), lp->d_secperunit,
222936036Skarels 		lp->d_nsectors, lp->d_ntracks, lp->d_ncylinders);
223034283Skarels 	if (!cold)
223134283Skarels 		addlog("\n");
223233444Sbostic 	return (0);
223333444Sbostic found:
223432523Sbostic 	p = ut->ut_name;
223532523Sbostic 	for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++)
223632523Sbostic 		lp->d_typename[i] = *p++;
223732523Sbostic 	lp->d_typename[i] = 0;
223832523Sbostic 	sz = ut->ut_sizes;
223932523Sbostic 	lp->d_nsectors = ut->ut_nsectors;
224032523Sbostic 	lp->d_ntracks = ut->ut_ntracks;
224132523Sbostic 	lp->d_ncylinders = ut->ut_ncylinders;
224230536Skarels 	lp->d_npartitions = 8;
224330536Skarels 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
224432523Sbostic 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) {
224532523Sbostic 		pp->p_offset = sz->blkoff;
224632523Sbostic 		if ((pp->p_size = sz->nblocks) == (u_long)-1)
224732523Sbostic 			pp->p_size = ra->ra_dsize - sz->blkoff;
224830536Skarels 	}
224930536Skarels 	return (1);
225030536Skarels }
225132523Sbostic #endif /* COMPAT_42 */
225236036Skarels 
225336036Skarels /*
225436036Skarels  * Construct a label for a drive from geometry information
225536036Skarels  * if we have no better information.
225636036Skarels  */
225736036Skarels uda_makefakelabel(ra, lp)
225836036Skarels 	register struct ra_info *ra;
225936036Skarels 	register struct disklabel *lp;
226036036Skarels {
226136036Skarels 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
226236036Skarels 	lp->d_ntracks = ra->ra_geom.rg_ntracks;
226336036Skarels 	lp->d_ncylinders = ra->ra_geom.rg_ncyl;
226436036Skarels 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
226536036Skarels 	bcopy("ra??", lp->d_typename, sizeof("ra??"));
226636036Skarels 	lp->d_npartitions = 1;
226736036Skarels 	lp->d_partitions[0].p_offset = 0;
226836036Skarels 	lp->d_partitions[0].p_size = lp->d_secperunit;
226936036Skarels }
227032523Sbostic #endif /* NUDA > 0 */
2271