xref: /csrg-svn/sys/vax/uba/uda.c (revision 46402)
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  *
844554Sbostic  * %sccs.include.redist.c%
935039Sbostic  *
10*46402Storek  *	@(#)uda.c	7.32 (Berkeley) 02/13/91
1123353Smckusick  */
1223353Smckusick 
1332523Sbostic /*
1432523Sbostic  * UDA50/MSCP device driver
1517553Skarels  */
1617553Skarels 
1732523Sbostic #define	POLLSTATS
1832523Sbostic 
1932523Sbostic /*
2032523Sbostic  * TODO
2132523Sbostic  *	write bad block forwarding code
2232523Sbostic  */
2332523Sbostic 
244743Swnj #include "ra.h"
2532523Sbostic 
2617642Skarels #if NUDA > 0
2732523Sbostic 
284743Swnj /*
2932523Sbostic  * CONFIGURATION OPTIONS.  The next three defines are tunable -- tune away!
304743Swnj  *
3132523Sbostic  * COMPAT_42 enables 4.2/4.3 compatibility (label mapping)
3232523Sbostic  *
3332523Sbostic  * NRSPL2 and NCMDL2 control the number of response and command
3432523Sbostic  * packets respectively.  They may be any value from 0 to 7, though
3532523Sbostic  * setting them higher than 5 is unlikely to be of any value.
3632523Sbostic  * If you get warnings about your command ring being too small,
3732523Sbostic  * try increasing the values by one.
3832523Sbostic  *
3932523Sbostic  * MAXUNIT controls the maximum unit number (number of drives per
4032523Sbostic  * controller) we are prepared to handle.
4132523Sbostic  *
4232523Sbostic  * DEFAULT_BURST must be at least 1.
434743Swnj  */
4432523Sbostic #define	COMPAT_42
4532523Sbostic 
4632523Sbostic #define	NRSPL2	5		/* log2 number of response packets */
4732523Sbostic #define NCMDL2	5		/* log2 number of command packets */
4832523Sbostic #define	MAXUNIT	8		/* maximum allowed unit number */
4932523Sbostic #define	DEFAULT_BURST	4	/* default DMA burst size */
5032523Sbostic 
5145808Sbostic #include "sys/param.h"
5245808Sbostic #include "sys/systm.h"
5345808Sbostic #include "sys/buf.h"
5445808Sbostic #include "sys/conf.h"
5545808Sbostic #include "sys/file.h"
5645808Sbostic #include "sys/ioctl.h"
5745808Sbostic #include "sys/user.h"
5845808Sbostic #include "sys/map.h"
5945808Sbostic #include "sys/vm.h"
6045808Sbostic #include "sys/dkstat.h"
6145808Sbostic #include "sys/cmap.h"
6245808Sbostic #include "sys/disklabel.h"
6345808Sbostic #include "sys/syslog.h"
6445808Sbostic #include "sys/stat.h"
654743Swnj 
6645808Sbostic #include "../include/pte.h"
6734283Skarels 
6845808Sbostic #include "../include/cpu.h"
6917553Skarels #include "ubareg.h"
7017553Skarels #include "ubavar.h"
718613Sroot 
7232523Sbostic #define	NRSP	(1 << NRSPL2)
7332523Sbostic #define	NCMD	(1 << NCMDL2)
748613Sroot 
7532523Sbostic #include "udareg.h"
768482Sroot #include "../vax/mscp.h"
7732523Sbostic #include "../vax/mscpvar.h"
7845808Sbostic #include "../include/mtpr.h"
794743Swnj 
8032523Sbostic /*
8132523Sbostic  * UDA communications area and MSCP packet pools, per controller.
8232523Sbostic  */
8332523Sbostic struct	uda {
8432523Sbostic 	struct	udaca uda_ca;		/* communications area */
8532523Sbostic 	struct	mscp uda_rsp[NRSP];	/* response packets */
8632523Sbostic 	struct	mscp uda_cmd[NCMD];	/* command packets */
874743Swnj } uda[NUDA];
884743Swnj 
8932523Sbostic /*
9032523Sbostic  * Software status, per controller.
9132523Sbostic  */
9232523Sbostic struct	uda_softc {
9332523Sbostic 	struct	uda *sc_uda;	/* Unibus address of uda struct */
9432523Sbostic 	short	sc_state;	/* UDA50 state; see below */
9532523Sbostic 	short	sc_flags;	/* flags; see below */
9632523Sbostic 	int	sc_micro;	/* microcode revision */
9732523Sbostic 	int	sc_ivec;	/* interrupt vector address */
9836036Skarels 	short	sc_ipl;		/* interrupt priority, Q-bus */
9932523Sbostic 	struct	mscp_info sc_mi;/* MSCP info (per mscpvar.h) */
10032523Sbostic #ifndef POLLSTATS
10132523Sbostic 	int	sc_wticks;	/* watchdog timer ticks */
10232523Sbostic #else
10332523Sbostic 	short	sc_wticks;
10432523Sbostic 	short	sc_ncmd;
10532523Sbostic #endif
10632523Sbostic } uda_softc[NUDA];
10724742Sbloom 
10832523Sbostic #ifdef POLLSTATS
10932523Sbostic struct udastats {
11032523Sbostic 	int	ncmd;
11132523Sbostic 	int	cmd[NCMD + 1];
11232523Sbostic } udastats = { NCMD + 1 };
11332523Sbostic #endif
11417553Skarels 
11532523Sbostic /*
11632523Sbostic  * Controller states
11732523Sbostic  */
11832523Sbostic #define	ST_IDLE		0	/* uninitialised */
11932523Sbostic #define	ST_STEP1	1	/* in `STEP 1' */
12032523Sbostic #define	ST_STEP2	2	/* in `STEP 2' */
12132523Sbostic #define	ST_STEP3	3	/* in `STEP 3' */
12232523Sbostic #define	ST_SETCHAR	4	/* in `Set Controller Characteristics' */
12332523Sbostic #define	ST_RUN		5	/* up and running */
1244743Swnj 
12532523Sbostic /*
12632523Sbostic  * Flags
12732523Sbostic  */
12832523Sbostic #define	SC_MAPPED	0x01	/* mapped in Unibus I/O space */
12932523Sbostic #define	SC_INSTART	0x02	/* inside udastart() */
13032523Sbostic #define	SC_GRIPED	0x04	/* griped about cmd ring too small */
13132523Sbostic #define	SC_INSLAVE	0x08	/* inside udaslave() */
13232523Sbostic #define	SC_DOWAKE	0x10	/* wakeup when ctlr init done */
13332523Sbostic #define	SC_STARTPOLL	0x20	/* need to initiate polling */
13412421Ssam 
13532523Sbostic /*
13632523Sbostic  * Device to unit number and partition and back
13732523Sbostic  */
13832523Sbostic #define	UNITSHIFT	3
13932523Sbostic #define	UNITMASK	7
14032523Sbostic #define	udaunit(dev)	(minor(dev) >> UNITSHIFT)
14132523Sbostic #define	udapart(dev)	(minor(dev) & UNITMASK)
14232523Sbostic #define	udaminor(u, p)	(((u) << UNITSHIFT) | (p))
1434743Swnj 
14417553Skarels /*
14532523Sbostic  * Drive status, per drive
14617553Skarels  */
14732523Sbostic struct ra_info {
14832523Sbostic 	daddr_t	ra_dsize;	/* size in sectors */
14933444Sbostic /*	u_long	ra_type;	/* drive type */
15032523Sbostic 	u_long	ra_mediaid;	/* media id */
15132523Sbostic 	int	ra_state;	/* open/closed state */
15232523Sbostic 	struct	ra_geom {	/* geometry information */
15332523Sbostic 		u_short	rg_nsectors;	/* sectors/track */
15432523Sbostic 		u_short	rg_ngroups;	/* track groups */
15532523Sbostic 		u_short	rg_ngpc;	/* groups/cylinder */
15632523Sbostic 		u_short	rg_ntracks;	/* ngroups*ngpc */
15732523Sbostic 		u_short	rg_ncyl;	/* ra_dsize/ntracks/nsectors */
15832523Sbostic #ifdef notyet
15932523Sbostic 		u_short	rg_rctsize;	/* size of rct */
16032523Sbostic 		u_short	rg_rbns;	/* replacement blocks per track */
16132523Sbostic 		u_short	rg_nrct;	/* number of rct copies */
16232523Sbostic #endif
16332523Sbostic 	} ra_geom;
16433443Skarels 	int	ra_wlabel;	/* label sector is currently writable */
16532523Sbostic 	u_long	ra_openpart;	/* partitions open */
16632523Sbostic 	u_long	ra_bopenpart;	/* block partitions open */
16732523Sbostic 	u_long	ra_copenpart;	/* character partitions open */
16832523Sbostic } ra_info[NRA];
16917553Skarels 
17030536Skarels /*
17130536Skarels  * Software state, per drive
17230536Skarels  */
17330536Skarels #define	CLOSED		0
17430536Skarels #define	WANTOPEN	1
17530536Skarels #define	RDLABEL		2
17630536Skarels #define	OPEN		3
17730536Skarels #define	OPENRAW		4
17817553Skarels 
17932523Sbostic /*
18032523Sbostic  * Definition of the driver for autoconf.
18132523Sbostic  */
18232523Sbostic int	udaprobe(), udaslave(), udaattach(), udadgo(), udaintr();
18332523Sbostic struct	uba_ctlr *udaminfo[NUDA];
18432523Sbostic struct	uba_device *udadinfo[NRA];
18532523Sbostic struct	disklabel udalabel[NRA];
18617553Skarels 
18732523Sbostic u_short	udastd[] = { 0772150, 0772550, 0777550, 0 };
18832523Sbostic struct	uba_driver udadriver =
18932523Sbostic  { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda",
19032523Sbostic    udaminfo };
19117553Skarels 
19232523Sbostic /*
19332523Sbostic  * More driver definitions, for generic MSCP code.
19432523Sbostic  */
19532523Sbostic int	udadgram(), udactlrdone(), udaunconf(), udaiodone();
19632523Sbostic int	udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb();
1974743Swnj 
19832523Sbostic struct	buf udautab[NRA];	/* per drive transfer queue */
1994743Swnj 
20032523Sbostic struct	mscp_driver udamscpdriver =
20134524Skarels  { MAXUNIT, NRA, UNITSHIFT, udautab, udalabel, udadinfo,
20232523Sbostic    udadgram, udactlrdone, udaunconf, udaiodone,
20332523Sbostic    udaonline, udagotstatus, udareplace, udaioerror, udabb,
20432523Sbostic    "uda", "ra" };
20532523Sbostic 
20632523Sbostic /*
20732523Sbostic  * Miscellaneous private variables.
20832523Sbostic  */
20932523Sbostic char	udasr_bits[] = UDASR_BITS;
21032523Sbostic 
21132523Sbostic struct	uba_device *udaip[NUDA][MAXUNIT];
21232523Sbostic 				/* inverting pointers: ctlr & unit => Unibus
21332523Sbostic 				   device pointer */
21432523Sbostic 
21532523Sbostic int	udaburst[NUDA] = { 0 };	/* burst size, per UDA50, zero => default;
21632523Sbostic 				   in data space so patchable via adb */
21732523Sbostic 
21832523Sbostic struct	mscp udaslavereply;	/* get unit status response packet, set
21932523Sbostic 				   for udaslave by udaunconf, via udaintr */
22032523Sbostic 
22132523Sbostic static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr
22232523Sbostic 				   info to slave routine; instead, we remember
22332523Sbostic 				   the last ctlr argument to probe */
22432523Sbostic 
22532523Sbostic int	udawstart, udawatch();	/* watchdog timer */
22632523Sbostic 
22732523Sbostic /*
22832523Sbostic  * Externals
22932523Sbostic  */
23032523Sbostic int	wakeup();
23132523Sbostic int	hz;
23232523Sbostic 
23332523Sbostic /*
23432523Sbostic  * Poke at a supposed UDA50 to see if it is there.
23532523Sbostic  * This routine duplicates some of the code in udainit() only
23632523Sbostic  * because autoconf has not set up the right information yet.
23732523Sbostic  * We have to do everything `by hand'.
23832523Sbostic  */
udaprobe(reg,ctlr,um)23932523Sbostic udaprobe(reg, ctlr, um)
2404743Swnj 	caddr_t reg;
2414743Swnj 	int ctlr;
24232523Sbostic 	struct uba_ctlr *um;
2434743Swnj {
2444743Swnj 	register int br, cvec;
24532523Sbostic 	register struct uda_softc *sc;
24632523Sbostic 	register struct udadevice *udaddr;
24732523Sbostic 	register struct mscp_info *mi;
24845577Storek 	int timeout, tries;
24945577Storek #ifdef QBA
25045577Storek 	int s;
25145577Storek #endif
2524743Swnj 
25332523Sbostic #ifdef VAX750
25432523Sbostic 	/*
25532523Sbostic 	 * The UDA50 wants to share BDPs on 750s, but not on 780s or
25632523Sbostic 	 * 8600s.  (730s have no BDPs anyway.)  Toward this end, we
25732523Sbostic 	 * here set the `keep bdp' flag in the per-driver information
25832523Sbostic 	 * if this is a 750.  (We just need to do it once, but it is
25932523Sbostic 	 * easiest to do it now, for each UDA50.)
26032523Sbostic 	 */
26132523Sbostic 	if (cpu == VAX_750)
26232523Sbostic 		udadriver.ud_keepbdp = 1;
26332523Sbostic #endif
26417553Skarels 
26532523Sbostic 	probeum = um;			/* remember for udaslave() */
2664743Swnj #ifdef lint
26732523Sbostic 	br = 0; cvec = br; br = cvec; udaintr(0);
2684743Swnj #endif
26932523Sbostic 	/*
27032523Sbostic 	 * Set up the controller-specific generic MSCP driver info.
27132523Sbostic 	 * Note that this should really be done in the (nonexistent)
27232523Sbostic 	 * controller attach routine.
27332523Sbostic 	 */
27432523Sbostic 	sc = &uda_softc[ctlr];
27532523Sbostic 	mi = &sc->sc_mi;
27632523Sbostic 	mi->mi_md = &udamscpdriver;
27732523Sbostic 	mi->mi_ctlr = um->um_ctlr;
27832523Sbostic 	mi->mi_tab = &um->um_tab;
27932523Sbostic 	mi->mi_ip = udaip[ctlr];
28032523Sbostic 	mi->mi_cmd.mri_size = NCMD;
28132523Sbostic 	mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc;
28232523Sbostic 	mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd;
28332523Sbostic 	mi->mi_rsp.mri_size = NRSP;
28432523Sbostic 	mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc;
28532523Sbostic 	mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp;
28632523Sbostic 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
28732523Sbostic 
28832523Sbostic 	/*
28932523Sbostic 	 * More controller specific variables.  Again, this should
29032523Sbostic 	 * be in the controller attach routine.
29132523Sbostic 	 */
29232523Sbostic 	if (udaburst[ctlr] == 0)
29332523Sbostic 		udaburst[ctlr] = DEFAULT_BURST;
29432523Sbostic 
29532523Sbostic 	/*
29632523Sbostic 	 * Get an interrupt vector.  Note that even if the controller
29732523Sbostic 	 * does not respond, we keep the vector.  This is not a serious
29832523Sbostic 	 * problem; but it would be easily fixed if we had a controller
29932523Sbostic 	 * attach routine.  Sigh.
30032523Sbostic 	 */
30132523Sbostic 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
30217553Skarels 	udaddr = (struct udadevice *) reg;
30317553Skarels 
30432523Sbostic 	/*
30532523Sbostic 	 * Initialise the controller (partially).  The UDA50 programmer's
30632523Sbostic 	 * manual states that if initialisation fails, it should be retried
30732523Sbostic 	 * at least once, but after a second failure the port should be
30832523Sbostic 	 * considered `down'; it also mentions that the controller should
30932523Sbostic 	 * initialise within ten seconds.  Or so I hear; I have not seen
31032523Sbostic 	 * this manual myself.
31132523Sbostic 	 */
31245651Smckusick #if defined(QBA) && !defined(GENERIC)
31336036Skarels 	s = spl6();
31436036Skarels #endif
31532523Sbostic 	tries = 0;
31632523Sbostic again:
31732523Sbostic 	udaddr->udaip = 0;		/* start initialisation */
31832523Sbostic 	timeout = todr() + 1000;	/* timeout in 10 seconds */
31932523Sbostic 	while ((udaddr->udasa & UDA_STEP1) == 0)
32032523Sbostic 		if (todr() > timeout)
32132523Sbostic 			goto bad;
32232523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
32332523Sbostic 		(sc->sc_ivec >> 2);
32432523Sbostic 	while ((udaddr->udasa & UDA_STEP2) == 0)
32532523Sbostic 		if (todr() > timeout)
32632523Sbostic 			goto bad;
32732523Sbostic 
32832523Sbostic 	/* should have interrupted by now */
32935401Stef #ifdef QBA
33045651Smckusick #ifndef GENERIC
33136036Skarels 	sc->sc_ipl = br = qbgetpri();
33245651Smckusick #else
33345651Smckusick 	sc->sc_ipl = br = 0x15;
33427254Skridle #endif
33545651Smckusick #endif
33632523Sbostic 	return (sizeof (struct udadevice));
33732523Sbostic bad:
33832523Sbostic 	if (++tries < 2)
33932523Sbostic 		goto again;
34045651Smckusick #if defined(QBA) && !defined(GENERIC)
34136036Skarels 	splx(s);
34245577Storek #endif
34332523Sbostic 	return (0);
3444743Swnj }
3454743Swnj 
34632523Sbostic /*
34732523Sbostic  * Find a slave.  We allow wildcard slave numbers (something autoconf
34832523Sbostic  * is not really prepared to deal with); and we need to know the
34932523Sbostic  * controller number to talk to the UDA.  For the latter, we keep
35032523Sbostic  * track of the last controller probed, since a controller probe
35132523Sbostic  * immediately precedes all slave probes for that controller.  For the
35232523Sbostic  * former, we simply put the unit number into ui->ui_slave after we
35332523Sbostic  * have found one.
35432523Sbostic  *
35532523Sbostic  * Note that by the time udaslave is called, the interrupt vector
35632523Sbostic  * for the UDA50 has been set up (so that udaunconf() will be called).
35732523Sbostic  */
udaslave(ui,reg)35832523Sbostic udaslave(ui, reg)
35932523Sbostic 	register struct uba_device *ui;
3604743Swnj 	caddr_t reg;
3614743Swnj {
36232523Sbostic 	register struct uba_ctlr *um = probeum;
36332523Sbostic 	register struct mscp *mp;
36432523Sbostic 	register struct uda_softc *sc;
36533443Skarels 	int next = 0, timeout, tries, i;
36617553Skarels 
36732523Sbostic #ifdef lint
36832523Sbostic 	i = 0; i = i;
36932523Sbostic #endif
37032523Sbostic 	/*
37132523Sbostic 	 * Make sure the controller is fully initialised, by waiting
37232523Sbostic 	 * for it if necessary.
37332523Sbostic 	 */
37432523Sbostic 	sc = &uda_softc[um->um_ctlr];
37532523Sbostic 	if (sc->sc_state == ST_RUN)
37632523Sbostic 		goto findunit;
37732523Sbostic 	tries = 0;
37832523Sbostic again:
37932523Sbostic 	if (udainit(ui->ui_ctlr))
38032523Sbostic 		return (0);
38132523Sbostic 	timeout = todr() + 1000;		/* 10 seconds */
38232523Sbostic 	while (todr() < timeout)
38332523Sbostic 		if (sc->sc_state == ST_RUN)	/* made it */
38432523Sbostic 			goto findunit;
38532523Sbostic 	if (++tries < 2)
38632523Sbostic 		goto again;
38732523Sbostic 	printf("uda%d: controller hung\n", um->um_ctlr);
38832523Sbostic 	return (0);
38917553Skarels 
39032523Sbostic 	/*
39132523Sbostic 	 * The controller is all set; go find the unit.  Grab an
39232523Sbostic 	 * MSCP packet and send out a Get Unit Status command, with
39332523Sbostic 	 * the `next unit' modifier if we are looking for a generic
39432523Sbostic 	 * unit.  We set the `in slave' flag so that udaunconf()
39532523Sbostic 	 * knows to copy the response to `udaslavereply'.
39632523Sbostic 	 */
39732523Sbostic findunit:
39832523Sbostic 	udaslavereply.mscp_opcode = 0;
39932523Sbostic 	sc->sc_flags |= SC_INSLAVE;
40032523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL)
40132523Sbostic 		panic("udaslave");		/* `cannot happen' */
40232523Sbostic 	mp->mscp_opcode = M_OP_GETUNITST;
40332523Sbostic 	if (ui->ui_slave == '?') {
40432523Sbostic 		mp->mscp_unit = next;
40532523Sbostic 		mp->mscp_modifier = M_GUM_NEXTUNIT;
40632523Sbostic 	} else {
40732523Sbostic 		mp->mscp_unit = ui->ui_slave;
40832523Sbostic 		mp->mscp_modifier = 0;
40917553Skarels 	}
41032523Sbostic 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
41132523Sbostic 	i = ((struct udadevice *) reg)->udaip;	/* initiate polling */
41232523Sbostic 	mp = &udaslavereply;
41332523Sbostic 	timeout = todr() + 1000;
41432523Sbostic 	while (todr() < timeout)
41532523Sbostic 		if (mp->mscp_opcode)
41632523Sbostic 			goto gotit;
41732523Sbostic 	printf("uda%d: no response to Get Unit Status request\n",
41832523Sbostic 		um->um_ctlr);
41932523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
42032523Sbostic 	return (0);
42132523Sbostic 
42232523Sbostic gotit:
42332523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
42432523Sbostic 
42532523Sbostic 	/*
42632523Sbostic 	 * Got a slave response.  If the unit is there, use it.
42732523Sbostic 	 */
42832523Sbostic 	switch (mp->mscp_status & M_ST_MASK) {
42932523Sbostic 
43032523Sbostic 	case M_ST_SUCCESS:	/* worked */
43132523Sbostic 	case M_ST_AVAILABLE:	/* found another drive */
43232523Sbostic 		break;		/* use it */
43332523Sbostic 
43432523Sbostic 	case M_ST_OFFLINE:
43532523Sbostic 		/*
43632523Sbostic 		 * Figure out why it is off line.  It may be because
43732523Sbostic 		 * it is nonexistent, or because it is spun down, or
43832523Sbostic 		 * for some other reason.
43932523Sbostic 		 */
44032523Sbostic 		switch (mp->mscp_status & ~M_ST_MASK) {
44132523Sbostic 
44232523Sbostic 		case M_OFFLINE_UNKNOWN:
44332523Sbostic 			/*
44432523Sbostic 			 * No such drive, and there are none with
44532523Sbostic 			 * higher unit numbers either, if we are
44632523Sbostic 			 * using M_GUM_NEXTUNIT.
44732523Sbostic 			 */
44832523Sbostic 			return (0);
44932523Sbostic 
45032523Sbostic 		case M_OFFLINE_UNMOUNTED:
45132523Sbostic 			/*
45232523Sbostic 			 * The drive is not spun up.  Use it anyway.
45332523Sbostic 			 *
45432523Sbostic 			 * N.B.: this seems to be a common occurrance
45532523Sbostic 			 * after a power failure.  The first attempt
45632523Sbostic 			 * to bring it on line seems to spin it up
45732523Sbostic 			 * (and thus takes several minutes).  Perhaps
45832523Sbostic 			 * we should note here that the on-line may
45932523Sbostic 			 * take longer than usual.
46032523Sbostic 			 */
46132523Sbostic 			break;
46232523Sbostic 
46332523Sbostic 		default:
46432523Sbostic 			/*
46532523Sbostic 			 * In service, or something else equally unusable.
46632523Sbostic 			 */
46732523Sbostic 			printf("uda%d: unit %d off line: ", um->um_ctlr,
46832523Sbostic 				mp->mscp_unit);
46932523Sbostic 			mscp_printevent(mp);
47032523Sbostic 			goto try_another;
47132523Sbostic 		}
47232523Sbostic 		break;
47332523Sbostic 
47432523Sbostic 	default:
47532523Sbostic 		printf("uda%d: unable to get unit status: ", um->um_ctlr);
47632523Sbostic 		mscp_printevent(mp);
47732523Sbostic 		return (0);
47817553Skarels 	}
47932523Sbostic 
48032523Sbostic 	/*
48132523Sbostic 	 * Does this ever happen?  What (if anything) does it mean?
48232523Sbostic 	 */
48332523Sbostic 	if (mp->mscp_unit < next) {
48432523Sbostic 		printf("uda%d: unit %d, next %d\n",
48532523Sbostic 			um->um_ctlr, mp->mscp_unit, next);
48632523Sbostic 		return (0);
48717553Skarels 	}
48832523Sbostic 
48932523Sbostic 	if (mp->mscp_unit >= MAXUNIT) {
49032523Sbostic 		printf("uda%d: cannot handle unit number %d (max is %d)\n",
49132523Sbostic 			um->um_ctlr, mp->mscp_unit, MAXUNIT - 1);
49232523Sbostic 		return (0);
49332523Sbostic 	}
49432523Sbostic 
49532523Sbostic 	/*
49632523Sbostic 	 * See if we already handle this drive.
49732523Sbostic 	 * (Only likely if ui->ui_slave=='?'.)
49832523Sbostic 	 */
49932523Sbostic 	if (udaip[um->um_ctlr][mp->mscp_unit] != NULL) {
50032523Sbostic try_another:
50132523Sbostic 		if (ui->ui_slave != '?')
50232523Sbostic 			return (0);
50332523Sbostic 		next = mp->mscp_unit + 1;
50432523Sbostic 		goto findunit;
50532523Sbostic 	}
50632523Sbostic 
50732523Sbostic 	/*
50832523Sbostic 	 * Voila!
50932523Sbostic 	 */
51032523Sbostic 	uda_rasave(ui->ui_unit, mp, 0);
51132523Sbostic 	ui->ui_flags = 0;	/* not on line, nor anything else */
51232523Sbostic 	ui->ui_slave = mp->mscp_unit;
51332523Sbostic 	return (1);
5144743Swnj }
5154743Swnj 
51632523Sbostic /*
51732523Sbostic  * Attach a found slave.  Make sure the watchdog timer is running.
51840045Smarc  * If this disk is being profiled, fill in the `wpms' value (used by
51932523Sbostic  * what?).  Set up the inverting pointer, and attempt to bring the
52032523Sbostic  * drive on line and read its label.
52132523Sbostic  */
udaattach(ui)52232523Sbostic udaattach(ui)
5234743Swnj 	register struct uba_device *ui;
5244743Swnj {
52532523Sbostic 	register int unit = ui->ui_unit;
52632523Sbostic 
52732523Sbostic 	if (udawstart == 0) {
52832523Sbostic 		timeout(udawatch, (caddr_t) 0, hz);
52932523Sbostic 		udawstart++;
53032523Sbostic 	}
53133444Sbostic 
53233444Sbostic 	/*
53333444Sbostic 	 * Floppies cannot be brought on line unless there is
53433444Sbostic 	 * a disk in the drive.  Since an ONLINE while cold
53533444Sbostic 	 * takes ten seconds to fail, and (when notyet becomes now)
53633444Sbostic 	 * no sensible person will swap to one, we just
53733444Sbostic 	 * defer the ONLINE until someone tries to use the drive.
53833444Sbostic 	 *
53933444Sbostic 	 * THIS ASSUMES THAT DRIVE TYPES ?X? ARE FLOPPIES
54033444Sbostic 	 */
54133444Sbostic 	if (MSCP_MID_ECH(1, ra_info[unit].ra_mediaid) == 'X' - '@') {
54234283Skarels 		printf(": floppy");
54333444Sbostic 		return;
54433444Sbostic 	}
54534649Skarels 	if (ui->ui_dk >= 0)
54640045Smarc 		dk_wpms[ui->ui_dk] = (60 * 31 * 256);	/* approx */
54732523Sbostic 	udaip[ui->ui_ctlr][ui->ui_slave] = ui;
54833195Sbostic 
54932523Sbostic 	if (uda_rainit(ui, 0))
55034283Skarels 		printf(": offline");
55136036Skarels 	else if (ra_info[unit].ra_state == OPEN) {
55234283Skarels 		printf(": %s, size = %d sectors",
55334283Skarels 		    udalabel[unit].d_typename, ra_info[unit].ra_dsize);
55432523Sbostic #ifdef notyet
55532523Sbostic 		addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]);
55626295Skarels #endif
55730536Skarels 	}
55832523Sbostic }
55932523Sbostic 
56032523Sbostic /*
56132523Sbostic  * Initialise a UDA50.  Return true iff something goes wrong.
56232523Sbostic  */
udainit(ctlr)56332523Sbostic udainit(ctlr)
56432523Sbostic 	int ctlr;
56532523Sbostic {
56632523Sbostic 	register struct uda_softc *sc;
56732523Sbostic 	register struct udadevice *udaddr;
56832523Sbostic 	struct uba_ctlr *um;
56932523Sbostic 	int timo, ubinfo;
57032523Sbostic 
57132523Sbostic 	sc = &uda_softc[ctlr];
57232523Sbostic 	um = udaminfo[ctlr];
57332523Sbostic 	if ((sc->sc_flags & SC_MAPPED) == 0) {
57432523Sbostic 		/*
57532523Sbostic 		 * Map the communication area and command and
57632523Sbostic 		 * response packets into Unibus space.
57732523Sbostic 		 */
57832523Sbostic 		ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr],
57932523Sbostic 			sizeof (struct uda), UBA_CANTWAIT);
58032523Sbostic 		if (ubinfo == 0) {
58132523Sbostic 			printf("uda%d: uballoc map failed\n", ctlr);
58232523Sbostic 			return (-1);
58332523Sbostic 		}
58436036Skarels 		sc->sc_uda = (struct uda *) UBAI_ADDR(ubinfo);
58532523Sbostic 		sc->sc_flags |= SC_MAPPED;
58632523Sbostic 	}
58732523Sbostic 
58830536Skarels 	/*
58932523Sbostic 	 * While we are thinking about it, reset the next command
59032523Sbostic 	 * and response indicies.
59130536Skarels 	 */
59232523Sbostic 	sc->sc_mi.mi_cmd.mri_next = 0;
59332523Sbostic 	sc->sc_mi.mi_rsp.mri_next = 0;
59432523Sbostic 
59532523Sbostic 	/*
59632523Sbostic 	 * Start up the hardware initialisation sequence.
59732523Sbostic 	 */
59832523Sbostic #define	STEP0MASK	(UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \
59932523Sbostic 			 UDA_STEP1 | UDA_NV)
60032523Sbostic 
60132523Sbostic 	sc->sc_state = ST_IDLE;	/* in case init fails */
60233444Sbostic 	udaddr = (struct udadevice *)um->um_addr;
60332523Sbostic 	udaddr->udaip = 0;
60432523Sbostic 	timo = todr() + 1000;
60532523Sbostic 	while ((udaddr->udasa & STEP0MASK) == 0) {
60632523Sbostic 		if (todr() > timo) {
60732523Sbostic 			printf("uda%d: timeout during init\n", ctlr);
60832523Sbostic 			return (-1);
60932523Sbostic 		}
61032523Sbostic 	}
61132523Sbostic 	if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) {
61232523Sbostic 		printf("uda%d: init failed, sa=%b\n", ctlr,
61332523Sbostic 			udaddr->udasa, udasr_bits);
61433444Sbostic 		udasaerror(um, 0);
61532523Sbostic 		return (-1);
61632523Sbostic 	}
61732523Sbostic 
61832523Sbostic 	/*
61932523Sbostic 	 * Success!  Record new state, and start step 1 initialisation.
62032523Sbostic 	 * The rest is done in the interrupt handler.
62132523Sbostic 	 */
62232523Sbostic 	sc->sc_state = ST_STEP1;
62332523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
62432523Sbostic 	    (sc->sc_ivec >> 2);
62532523Sbostic 	return (0);
6264743Swnj }
6274743Swnj 
6284743Swnj /*
62932523Sbostic  * Open a drive.
6304743Swnj  */
63132523Sbostic /*ARGSUSED*/
udaopen(dev,flag,fmt)63232523Sbostic udaopen(dev, flag, fmt)
6334743Swnj 	dev_t dev;
63430773Skarels 	int flag, fmt;
6354743Swnj {
63632523Sbostic 	register int unit;
6374743Swnj 	register struct uba_device *ui;
6384743Swnj 	register struct uda_softc *sc;
63930536Skarels 	register struct disklabel *lp;
64030536Skarels 	register struct partition *pp;
64130916Skarels 	register struct ra_info *ra;
64232523Sbostic 	int s, i, part, mask, error = 0;
64330536Skarels 	daddr_t start, end;
64430536Skarels 
64532523Sbostic 	/*
64632523Sbostic 	 * Make sure this is a reasonable open request.
64732523Sbostic 	 */
64832523Sbostic 	unit = udaunit(dev);
64932523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0)
6508576Sroot 		return (ENXIO);
65132523Sbostic 
65232523Sbostic 	/*
65332523Sbostic 	 * Make sure the controller is running, by (re)initialising it if
65432523Sbostic 	 * necessary.
65532523Sbostic 	 */
6564743Swnj 	sc = &uda_softc[ui->ui_ctlr];
6575434Sroot 	s = spl5();
65832523Sbostic 	if (sc->sc_state != ST_RUN) {
65932523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) {
66032523Sbostic 			splx(s);
6618576Sroot 			return (EIO);
66217553Skarels 		}
66332523Sbostic 		/*
66432523Sbostic 		 * In case it does not come up, make sure we will be
66532523Sbostic 		 * restarted in 10 seconds.  This corresponds to the
66632523Sbostic 		 * 10 second timeouts in udaprobe() and udaslave().
66732523Sbostic 		 */
66832523Sbostic 		sc->sc_flags |= SC_DOWAKE;
66932523Sbostic 		timeout(wakeup, (caddr_t) sc, 10 * hz);
67032523Sbostic 		sleep((caddr_t) sc, PRIBIO);
67132523Sbostic 		if (sc->sc_state != ST_RUN) {
67232523Sbostic 			splx(s);
67332523Sbostic 			printf("uda%d: controller hung\n", ui->ui_ctlr);
67432523Sbostic 			return (EIO);
67532523Sbostic 		}
67632523Sbostic 		untimeout(wakeup, (caddr_t) sc);
6774743Swnj 	}
67832523Sbostic 
67932523Sbostic 	/*
68032523Sbostic 	 * Wait for the state to settle
68132523Sbostic 	 */
68232523Sbostic 	ra = &ra_info[unit];
68332523Sbostic 	while (ra->ra_state != OPEN && ra->ra_state != OPENRAW &&
68432523Sbostic 	    ra->ra_state != CLOSED)
68540726Skarels 		if (error = tsleep((caddr_t)ra, (PZERO + 1) | PCATCH,
68640726Skarels 		    devopn, 0)) {
68740726Skarels 			splx(s);
68840726Skarels 			return (error);
68940726Skarels 		}
69032523Sbostic 
69132523Sbostic 	/*
69232523Sbostic 	 * If not on line, or we are not sure of the label, reinitialise
69332523Sbostic 	 * the drive.
69432523Sbostic 	 */
69532523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0 ||
69632523Sbostic 	    (ra->ra_state != OPEN && ra->ra_state != OPENRAW))
69732523Sbostic 		error = uda_rainit(ui, flag);
69830916Skarels 	splx(s);
69932523Sbostic 	if (error)
70032523Sbostic 		return (error);
70130536Skarels 
70232523Sbostic 	part = udapart(dev);
70332523Sbostic 	lp = &udalabel[unit];
70430536Skarels 	if (part >= lp->d_npartitions)
70530536Skarels 		return (ENXIO);
70630536Skarels 	/*
70732523Sbostic 	 * Warn if a partition is opened that overlaps another
70832523Sbostic 	 * already open, unless either is the `raw' partition
70932523Sbostic 	 * (whole disk).
71030536Skarels 	 */
71132523Sbostic #define	RAWPART		2	/* 'c' partition */	/* XXX */
71232523Sbostic 	mask = 1 << part;
71332523Sbostic 	if ((ra->ra_openpart & mask) == 0 && part != RAWPART) {
71430536Skarels 		pp = &lp->d_partitions[part];
71530536Skarels 		start = pp->p_offset;
71630536Skarels 		end = pp->p_offset + pp->p_size;
71732523Sbostic 		for (pp = lp->d_partitions, i = 0;
71832523Sbostic 		     i < lp->d_npartitions; pp++, i++) {
71930536Skarels 			if (pp->p_offset + pp->p_size <= start ||
72032523Sbostic 			    pp->p_offset >= end || i == RAWPART)
72130536Skarels 				continue;
72232523Sbostic 			if (ra->ra_openpart & (1 << i))
72330536Skarels 				log(LOG_WARNING,
72430536Skarels 				    "ra%d%c: overlaps open partition (%c)\n",
72532523Sbostic 				    unit, part + 'a', i + 'a');
72617553Skarels 		}
72717553Skarels 	}
72830773Skarels 	switch (fmt) {
72930773Skarels 	case S_IFCHR:
73032523Sbostic 		ra->ra_copenpart |= mask;
73130773Skarels 		break;
73230773Skarels 	case S_IFBLK:
73332523Sbostic 		ra->ra_bopenpart |= mask;
73430773Skarels 		break;
73530773Skarels 	}
73632523Sbostic 	ra->ra_openpart |= mask;
7378576Sroot 	return (0);
7384743Swnj }
7394743Swnj 
74033444Sbostic /* ARGSUSED */
udaclose(dev,flags,fmt)74132523Sbostic udaclose(dev, flags, fmt)
74230536Skarels 	dev_t dev;
74330773Skarels 	int flags, fmt;
74430536Skarels {
74532523Sbostic 	register int unit = udaunit(dev);
74630773Skarels 	register struct ra_info *ra = &ra_info[unit];
74732523Sbostic 	int s, mask = (1 << udapart(dev));
74830536Skarels 
74930773Skarels 	switch (fmt) {
75030773Skarels 	case S_IFCHR:
75132523Sbostic 		ra->ra_copenpart &= ~mask;
75230773Skarels 		break;
75330773Skarels 	case S_IFBLK:
75432523Sbostic 		ra->ra_bopenpart &= ~mask;
75530773Skarels 		break;
75630773Skarels 	}
75732523Sbostic 	ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
75832523Sbostic 
75930536Skarels 	/*
76032523Sbostic 	 * Should wait for I/O to complete on this partition even if
76132523Sbostic 	 * others are open, but wait for work on blkflush().
76230536Skarels 	 */
76332523Sbostic 	if (ra->ra_openpart == 0) {
76430536Skarels 		s = spl5();
76532523Sbostic 		while (udautab[unit].b_actf)
76632523Sbostic 			sleep((caddr_t)&udautab[unit], PZERO - 1);
76730536Skarels 		splx(s);
76832523Sbostic 		ra->ra_state = CLOSED;
76933443Skarels 		ra->ra_wlabel = 0;
77030536Skarels 	}
77130773Skarels 	return (0);
77230536Skarels }
77330536Skarels 
7744743Swnj /*
77532523Sbostic  * Initialise a drive.  If it is not already, bring it on line,
77632523Sbostic  * and set a timeout on it in case it fails to respond.
77732523Sbostic  * When on line, read in the pack label.
7784743Swnj  */
uda_rainit(ui,flags)77932523Sbostic uda_rainit(ui, flags)
78030536Skarels 	register struct uba_device *ui;
78132523Sbostic 	int flags;
78230536Skarels {
78332523Sbostic 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
78434283Skarels 	register struct disklabel *lp;
78530536Skarels 	register struct mscp *mp;
78632523Sbostic 	register int unit = ui->ui_unit;
78732523Sbostic 	register struct ra_info *ra;
78830773Skarels 	char *msg, *readdisklabel();
78932523Sbostic 	int s, i, udastrategy();
79030536Skarels 	extern int cold;
79130536Skarels 
79232523Sbostic 	ra = &ra_info[unit];
79332523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
79432523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT);
79532523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
79632523Sbostic 		mp->mscp_unit = ui->ui_slave;
79732523Sbostic 		mp->mscp_cmdref = (long)&ui->ui_flags;
79832523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
79932523Sbostic 		ra->ra_state = WANTOPEN;
80032523Sbostic 		if (!cold)
80132523Sbostic 			s = spl5();
80232523Sbostic 		i = ((struct udadevice *)ui->ui_addr)->udaip;
80330536Skarels 
80430916Skarels 		if (cold) {
80532523Sbostic 			i = todr() + 1000;
80632523Sbostic 			while ((ui->ui_flags & UNIT_ONLINE) == 0)
80732523Sbostic 				if (todr() > i)
80832523Sbostic 					break;
80930916Skarels 		} else {
81032523Sbostic 			timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
81132523Sbostic 			sleep((caddr_t)&ui->ui_flags, PSWP + 1);
81232523Sbostic 			splx(s);
81332523Sbostic 			untimeout(wakeup, (caddr_t)&ui->ui_flags);
81430916Skarels 		}
81532523Sbostic 		if (ra->ra_state != OPENRAW) {
81632523Sbostic 			ra->ra_state = CLOSED;
81732523Sbostic 			wakeup((caddr_t)ra);
81830916Skarels 			return (EIO);
81930916Skarels 		}
82030536Skarels 	}
82130536Skarels 
82232523Sbostic 	lp = &udalabel[unit];
82330916Skarels 	lp->d_secsize = DEV_BSIZE;
82432523Sbostic 	lp->d_secperunit = ra->ra_dsize;
82530916Skarels 
82630536Skarels 	if (flags & O_NDELAY)
82730536Skarels 		return (0);
82832523Sbostic 	ra->ra_state = RDLABEL;
82930536Skarels 	/*
83032523Sbostic 	 * Set up default sizes until we have the label, or longer
83132523Sbostic 	 * if there is none.  Set secpercyl, as readdisklabel wants
83238979Skarels 	 * to compute b_cylin (although we do not need it), and set
83338979Skarels 	 * nsectors in case diskerr is called.
83430536Skarels 	 */
83530916Skarels 	lp->d_secpercyl = 1;
83630536Skarels 	lp->d_npartitions = 1;
83736036Skarels 	lp->d_secsize = 512;
83836036Skarels 	lp->d_secperunit = ra->ra_dsize;
83938979Skarels 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
84030536Skarels 	lp->d_partitions[0].p_size = lp->d_secperunit;
84130536Skarels 	lp->d_partitions[0].p_offset = 0;
84232523Sbostic 
84330536Skarels 	/*
84430536Skarels 	 * Read pack label.
84530536Skarels 	 */
84632523Sbostic 	if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) {
84734283Skarels 		if (cold)
84834283Skarels 			printf(": %s", msg);
84934283Skarels 		else
85036036Skarels 			log(LOG_ERR, "ra%d: %s", unit, msg);
85130536Skarels #ifdef COMPAT_42
85232523Sbostic 		if (udamaptype(unit, lp))
85332523Sbostic 			ra->ra_state = OPEN;
85430536Skarels 		else
85532523Sbostic 			ra->ra_state = OPENRAW;
85631022Skarels #else
85732523Sbostic 		ra->ra_state = OPENRAW;
85836036Skarels 		uda_makefakelabel(ra, lp);
85930536Skarels #endif
86030773Skarels 	} else
86132523Sbostic 		ra->ra_state = OPEN;
86230916Skarels 	wakeup((caddr_t)ra);
86330916Skarels 	return (0);
86430536Skarels }
86530536Skarels 
86632523Sbostic /*
86732523Sbostic  * Copy the geometry information for the given ra from a
86832523Sbostic  * GET UNIT STATUS response.  If check, see if it changed.
86932523Sbostic  */
uda_rasave(unit,mp,check)87032523Sbostic uda_rasave(unit, mp, check)
87132523Sbostic 	int unit;
87232523Sbostic 	register struct mscp *mp;
87332523Sbostic 	int check;
87432523Sbostic {
87532523Sbostic 	register struct ra_info *ra = &ra_info[unit];
87632523Sbostic 
87734283Skarels 	if (check && ra->ra_mediaid != mp->mscp_guse.guse_mediaid) {
87833443Skarels 		printf("ra%d: changed types! was %d now %d\n", unit,
87934283Skarels 			ra->ra_mediaid, mp->mscp_guse.guse_mediaid);
88032523Sbostic 		ra->ra_state = CLOSED;	/* ??? */
88132523Sbostic 	}
88233444Sbostic 	/* ra->ra_type = mp->mscp_guse.guse_drivetype; */
88332523Sbostic 	ra->ra_mediaid = mp->mscp_guse.guse_mediaid;
88432523Sbostic 	ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt;
88532523Sbostic 	ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group;
88632523Sbostic 	ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc;
88732523Sbostic 	ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc;
88832523Sbostic 	/* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */
88932523Sbostic #ifdef notyet
89032523Sbostic 	ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize;
89132523Sbostic 	ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt;
89232523Sbostic 	ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct;
89332523Sbostic #endif
89432523Sbostic }
89532523Sbostic 
89632523Sbostic /*
89732523Sbostic  * Queue a transfer request, and if possible, hand it to the controller.
89832523Sbostic  *
89932523Sbostic  * This routine is broken into two so that the internal version
90032523Sbostic  * udastrat1() can be called by the (nonexistent, as yet) bad block
90132523Sbostic  * revectoring routine.
90232523Sbostic  */
udastrategy(bp)90332523Sbostic udastrategy(bp)
9044743Swnj 	register struct buf *bp;
9054743Swnj {
90632523Sbostic 	register int unit;
9074743Swnj 	register struct uba_device *ui;
90832523Sbostic 	register struct ra_info *ra;
90932523Sbostic 	struct partition *pp;
91032523Sbostic 	int p;
9114743Swnj 	daddr_t sz, maxsz;
9124743Swnj 
91332523Sbostic 	/*
91432523Sbostic 	 * Make sure this is a reasonable drive to use.
91532523Sbostic 	 */
91632523Sbostic 	if ((unit = udaunit(bp->b_dev)) >= NRA ||
91732523Sbostic 	    (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 ||
91832523Sbostic 	    (ra = &ra_info[unit])->ra_state == CLOSED) {
91924742Sbloom 		bp->b_error = ENXIO;
9204743Swnj 		goto bad;
92124742Sbloom 	}
92232523Sbostic 
92332523Sbostic 	/*
92432523Sbostic 	 * If drive is open `raw' or reading label, let it at it.
92532523Sbostic 	 */
92632523Sbostic 	if (ra->ra_state < OPEN) {
92732523Sbostic 		udastrat1(bp);
92832523Sbostic 		return;
92924742Sbloom 	}
93032523Sbostic 	p = udapart(bp->b_dev);
93133443Skarels 	if ((ra->ra_openpart & (1 << p)) == 0) {
93233443Skarels 		bp->b_error = ENODEV;
93333443Skarels 		goto bad;
93433443Skarels 	}
93532523Sbostic 
93632523Sbostic 	/*
93732523Sbostic 	 * Determine the size of the transfer, and make sure it is
93832523Sbostic 	 * within the boundaries of the partition.
93932523Sbostic 	 */
94032523Sbostic 	pp = &udalabel[unit].d_partitions[p];
94132523Sbostic 	maxsz = pp->p_size;
94232523Sbostic 	if (pp->p_offset + pp->p_size > ra->ra_dsize)
94332523Sbostic 		maxsz = ra->ra_dsize - pp->p_offset;
94430536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
94533443Skarels 	if (bp->b_blkno + pp->p_offset <= LABELSECTOR &&
94633443Skarels #if LABELSECTOR != 0
94733443Skarels 	    bp->b_blkno + pp->p_offset + sz > LABELSECTOR &&
94833443Skarels #endif
94933443Skarels 	    (bp->b_flags & B_READ) == 0 && ra->ra_wlabel == 0) {
95033443Skarels 		bp->b_error = EROFS;
95133443Skarels 		goto bad;
95233443Skarels 	}
95330536Skarels 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
95432523Sbostic 		/* if exactly at end of disk, return an EOF */
95524787Skarels 		if (bp->b_blkno == maxsz) {
95624787Skarels 			bp->b_resid = bp->b_bcount;
95732523Sbostic 			biodone(bp);
95832523Sbostic 			return;
95924787Skarels 		}
96032523Sbostic 		/* or truncate if part of it fits */
96130536Skarels 		sz = maxsz - bp->b_blkno;
96230536Skarels 		if (sz <= 0) {
96332523Sbostic 			bp->b_error = EINVAL;	/* or hang it up */
96430536Skarels 			goto bad;
96530536Skarels 		}
96630536Skarels 		bp->b_bcount = sz << DEV_BSHIFT;
96724742Sbloom 	}
96832523Sbostic 	udastrat1(bp);
96932523Sbostic 	return;
97032523Sbostic bad:
97132523Sbostic 	bp->b_flags |= B_ERROR;
97232523Sbostic 	biodone(bp);
97332523Sbostic }
97432523Sbostic 
97532523Sbostic /*
97632523Sbostic  * Work routine for udastrategy.
97732523Sbostic  */
udastrat1(bp)97832523Sbostic udastrat1(bp)
97932523Sbostic 	register struct buf *bp;
98032523Sbostic {
98132523Sbostic 	register int unit = udaunit(bp->b_dev);
98232523Sbostic 	register struct uba_ctlr *um;
98332523Sbostic 	register struct buf *dp;
98432523Sbostic 	struct uba_device *ui;
98532523Sbostic 	int s = spl5();
98632523Sbostic 
9874743Swnj 	/*
98832523Sbostic 	 * Append the buffer to the drive queue, and if it is not
98932523Sbostic 	 * already there, the drive to the controller queue.  (However,
99032523Sbostic 	 * if the drive queue is marked to be requeued, we must be
99132523Sbostic 	 * awaiting an on line or get unit status command; in this
99232523Sbostic 	 * case, leave it off the controller queue.)
9934743Swnj 	 */
99432523Sbostic 	um = (ui = udadinfo[unit])->ui_mi;
99532523Sbostic 	dp = &udautab[unit];
99632523Sbostic 	APPEND(bp, dp, av_forw);
99732523Sbostic 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
99832523Sbostic 		APPEND(dp, &um->um_tab, b_forw);
99932523Sbostic 		dp->b_active++;
100032523Sbostic 	}
100132523Sbostic 
10024743Swnj 	/*
100332523Sbostic 	 * Start activity on the controller.  Note that unlike other
100432523Sbostic 	 * Unibus drivers, we must always do this, not just when the
100532523Sbostic 	 * controller is not active.
10064743Swnj 	 */
100732523Sbostic 	udastart(um);
10085434Sroot 	splx(s);
10094743Swnj }
10104743Swnj 
101132523Sbostic /*
101232523Sbostic  * Start up whatever transfers we can find.
101332523Sbostic  * Note that udastart() must be called at spl5().
101432523Sbostic  */
udastart(um)101532523Sbostic udastart(um)
10164743Swnj 	register struct uba_ctlr *um;
10174743Swnj {
101832523Sbostic 	register struct uda_softc *sc = &uda_softc[um->um_ctlr];
10194743Swnj 	register struct buf *bp, *dp;
10204743Swnj 	register struct mscp *mp;
102132523Sbostic 	struct uba_device *ui;
10224743Swnj 	struct udadevice *udaddr;
102332523Sbostic 	struct partition *pp;
102432523Sbostic 	int i, sz;
10254743Swnj 
102632523Sbostic #ifdef lint
102732523Sbostic 	i = 0; i = i;
102832523Sbostic #endif
102932523Sbostic 	/*
103032523Sbostic 	 * If it is not running, try (again and again...) to initialise
103132523Sbostic 	 * it.  If it is currently initialising just ignore it for now.
103232523Sbostic 	 */
103332523Sbostic 	if (sc->sc_state != ST_RUN) {
103432523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr))
103532523Sbostic 			printf("uda%d: still hung\n", um->um_ctlr);
103632523Sbostic 		return;
103732523Sbostic 	}
103832523Sbostic 
103932523Sbostic 	/*
104032523Sbostic 	 * If um_cmd is nonzero, this controller is on the Unibus
104132523Sbostic 	 * resource wait queue.  It will not help to try more requests;
104232523Sbostic 	 * instead, when the Unibus unblocks and calls udadgo(), we
104332523Sbostic 	 * will call udastart() again.
104432523Sbostic 	 */
104532523Sbostic 	if (um->um_cmd)
104632523Sbostic 		return;
104732523Sbostic 
104832523Sbostic 	sc->sc_flags |= SC_INSTART;
104932523Sbostic 	udaddr = (struct udadevice *) um->um_addr;
105032523Sbostic 
10514743Swnj loop:
105232523Sbostic 	/*
105332523Sbostic 	 * Service the drive at the head of the queue.  It may not
105432523Sbostic 	 * need anything, in which case it might be shutting down
105532523Sbostic 	 * in udaclose().
105632523Sbostic 	 */
105732523Sbostic 	if ((dp = um->um_tab.b_actf) == NULL)
105832523Sbostic 		goto out;
10594743Swnj 	if ((bp = dp->b_actf) == NULL) {
10604743Swnj 		dp->b_active = 0;
10614743Swnj 		um->um_tab.b_actf = dp->b_forw;
106232523Sbostic 		if (ra_info[dp - udautab].ra_openpart == 0)
106332523Sbostic 			wakeup((caddr_t)dp); /* finish close protocol */
106432523Sbostic 		goto loop;
10654743Swnj 	}
106632523Sbostic 
106732523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
106833444Sbostic 		udasaerror(um, 1);
106932523Sbostic 		goto out;
10704743Swnj 	}
107132523Sbostic 
107232523Sbostic 	/*
107332523Sbostic 	 * Get an MSCP packet, then figure out what to do.  If
107432523Sbostic 	 * we cannot get a command packet, the command ring may
107532523Sbostic 	 * be too small:  We should have at least as many command
107632523Sbostic 	 * packets as credits, for best performance.
107732523Sbostic 	 */
107832523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) {
107932523Sbostic 		if (sc->sc_mi.mi_credits > MSCP_MINCREDITS &&
108032523Sbostic 		    (sc->sc_flags & SC_GRIPED) == 0) {
108132523Sbostic 			log(LOG_NOTICE, "uda%d: command ring too small\n",
108232523Sbostic 				um->um_ctlr);
108332523Sbostic 			sc->sc_flags |= SC_GRIPED;/* complain only once */
108417553Skarels 		}
108532523Sbostic 		goto out;
10864743Swnj 	}
10874743Swnj 
108832523Sbostic 	/*
108932523Sbostic 	 * Bring the drive on line if it is not already.  Get its status
109032523Sbostic 	 * if we do not already have it.  Otherwise just start the transfer.
109132523Sbostic 	 */
109232523Sbostic 	ui = udadinfo[udaunit(bp->b_dev)];
109332523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
109432523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
109532523Sbostic 		goto common;
10964743Swnj 	}
109732523Sbostic 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
109832523Sbostic 		mp->mscp_opcode = M_OP_GETUNITST;
109932523Sbostic common:
110032523Sbostic if (ui->ui_flags & UNIT_REQUEUE) panic("udastart");
110132523Sbostic 		/*
110232523Sbostic 		 * Take the drive off the controller queue.  When the
110332523Sbostic 		 * command finishes, make sure the drive is requeued.
110432523Sbostic 		 */
110532523Sbostic 		um->um_tab.b_actf = dp->b_forw;
110632523Sbostic 		dp->b_active = 0;
110732523Sbostic 		ui->ui_flags |= UNIT_REQUEUE;
110832523Sbostic 		mp->mscp_unit = ui->ui_slave;
110932523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
111032523Sbostic 		sc->sc_flags |= SC_STARTPOLL;
111132523Sbostic #ifdef POLLSTATS
111232523Sbostic 		sc->sc_ncmd++;
111325653Skarels #endif
111432523Sbostic 		goto loop;
111517553Skarels 	}
111632523Sbostic 
111732523Sbostic 	pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)];
111832523Sbostic 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
11194743Swnj 	mp->mscp_unit = ui->ui_slave;
112032523Sbostic 	mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset;
112130536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
112232523Sbostic 	mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ?
112332523Sbostic 		(pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount;
112432523Sbostic 	/* mscp_cmdref is filled in by mscp_go() */
11254743Swnj 
11264743Swnj 	/*
112732523Sbostic 	 * Drop the packet pointer into the `command' field so udadgo()
112832523Sbostic 	 * can tell what to start.  If ubago returns 1, we can do another
112932523Sbostic 	 * transfer.  If not, um_cmd will still point at mp, so we will
113032523Sbostic 	 * know that we are waiting for resources.
11314743Swnj 	 */
113232523Sbostic 	um->um_cmd = (int)mp;
113332523Sbostic 	if (ubago(ui))
113432523Sbostic 		goto loop;
113532523Sbostic 
113632523Sbostic 	/*
113732523Sbostic 	 * All done, or blocked in ubago().  If we managed to
113832523Sbostic 	 * issue some commands, start up the beast.
113932523Sbostic 	 */
114032523Sbostic out:
114132523Sbostic 	if (sc->sc_flags & SC_STARTPOLL) {
114232523Sbostic #ifdef POLLSTATS
114332523Sbostic 		udastats.cmd[sc->sc_ncmd]++;
114432523Sbostic 		sc->sc_ncmd = 0;
114532523Sbostic #endif
114633444Sbostic 		i = ((struct udadevice *)um->um_addr)->udaip;
11474743Swnj 	}
114832523Sbostic 	sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL);
114932523Sbostic }
115032523Sbostic 
115132523Sbostic /*
115232523Sbostic  * Start a transfer.
115332523Sbostic  *
115432523Sbostic  * If we are not called from within udastart(), we must have been
115532523Sbostic  * blocked, so call udastart to do more requests (if any).  If
115632523Sbostic  * this calls us again immediately we will not recurse, because
115732523Sbostic  * that time we will be in udastart().  Clever....
115832523Sbostic  */
udadgo(um)115932523Sbostic udadgo(um)
116032523Sbostic 	register struct uba_ctlr *um;
116132523Sbostic {
116232523Sbostic 	struct uda_softc *sc = &uda_softc[um->um_ctlr];
116332523Sbostic 	struct mscp *mp = (struct mscp *)um->um_cmd;
116432523Sbostic 
116532523Sbostic 	um->um_tab.b_active++;	/* another transfer going */
116632523Sbostic 
11674743Swnj 	/*
116832523Sbostic 	 * Fill in the MSCP packet and move the buffer to the
116932523Sbostic 	 * I/O wait queue.  Mark the controller as no longer on
117032523Sbostic 	 * the resource queue, and remember to initiate polling.
11714743Swnj 	 */
117236036Skarels 	mp->mscp_seq.seq_buffer = UBAI_ADDR(um->um_ubinfo) |
117332523Sbostic 		(UBAI_BDP(um->um_ubinfo) << 24);
117432523Sbostic 	mscp_go(&sc->sc_mi, mp, um->um_ubinfo);
117532523Sbostic 	um->um_cmd = 0;
117632523Sbostic 	um->um_ubinfo = 0;	/* tyke it awye */
117732523Sbostic 	sc->sc_flags |= SC_STARTPOLL;
117832523Sbostic #ifdef POLLSTATS
117932523Sbostic 	sc->sc_ncmd++;
118032523Sbostic #endif
118132523Sbostic 	if ((sc->sc_flags & SC_INSTART) == 0)
118232523Sbostic 		udastart(um);
11834743Swnj }
11844743Swnj 
udaiodone(mi,bp,info)118532523Sbostic udaiodone(mi, bp, info)
118632523Sbostic 	register struct mscp_info *mi;
118732523Sbostic 	struct buf *bp;
118832523Sbostic 	int info;
118932523Sbostic {
119032523Sbostic 	register struct uba_ctlr *um = udaminfo[mi->mi_ctlr];
119132523Sbostic 
119232523Sbostic 	um->um_ubinfo = info;
119332523Sbostic 	ubadone(um);
119432523Sbostic 	biodone(bp);
119532523Sbostic 	if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab)
119632523Sbostic 		ubarelse(um->um_ubanum, &um->um_bdp);
119732523Sbostic 	um->um_tab.b_active--;	/* another transfer done */
119832523Sbostic }
119932523Sbostic 
120033444Sbostic static struct saerr {
120133444Sbostic 	int	code;		/* error code (including UDA_ERR) */
120233444Sbostic 	char	*desc;		/* what it means: Efoo => foo error */
120333444Sbostic } saerr[] = {
120433444Sbostic 	{ 0100001, "Eunibus packet read" },
120533444Sbostic 	{ 0100002, "Eunibus packet write" },
120633444Sbostic 	{ 0100003, "EUDA ROM and RAM parity" },
120733444Sbostic 	{ 0100004, "EUDA RAM parity" },
120833444Sbostic 	{ 0100005, "EUDA ROM parity" },
120933444Sbostic 	{ 0100006, "Eunibus ring read" },
121033444Sbostic 	{ 0100007, "Eunibus ring write" },
121133444Sbostic 	{ 0100010, " unibus interrupt master failure" },
121233444Sbostic 	{ 0100011, "Ehost access timeout" },
121333444Sbostic 	{ 0100012, " host exceeded command limit" },
121433444Sbostic 	{ 0100013, " unibus bus master failure" },
121533444Sbostic 	{ 0100014, " DM XFC fatal error" },
121633444Sbostic 	{ 0100015, " hardware timeout of instruction loop" },
121733444Sbostic 	{ 0100016, " invalid virtual circuit id" },
121833444Sbostic 	{ 0100017, "Eunibus interrupt write" },
121933444Sbostic 	{ 0104000, "Efatal sequence" },
122033444Sbostic 	{ 0104040, " D proc ALU" },
122133444Sbostic 	{ 0104041, "ED proc control ROM parity" },
122233444Sbostic 	{ 0105102, "ED proc w/no BD#2 or RAM parity" },
122333444Sbostic 	{ 0105105, "ED proc RAM buffer" },
122433444Sbostic 	{ 0105152, "ED proc SDI" },
122533444Sbostic 	{ 0105153, "ED proc write mode wrap serdes" },
122633444Sbostic 	{ 0105154, "ED proc read mode serdes, RSGEN & ECC" },
122733444Sbostic 	{ 0106040, "EU proc ALU" },
122833444Sbostic 	{ 0106041, "EU proc control reg" },
122933444Sbostic 	{ 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" },
123033444Sbostic 	{ 0106047, " U proc const PROM err w/D proc running SDI test" },
123133444Sbostic 	{ 0106055, " unexpected trap" },
123233444Sbostic 	{ 0106071, "EU proc const PROM" },
123333444Sbostic 	{ 0106072, "EU proc control ROM parity" },
123433444Sbostic 	{ 0106200, "Estep 1 data" },
123533444Sbostic 	{ 0107103, "EU proc RAM parity" },
123633444Sbostic 	{ 0107107, "EU proc RAM buffer" },
123733444Sbostic 	{ 0107115, " test count wrong (BD 12)" },
123833444Sbostic 	{ 0112300, "Estep 2" },
123933444Sbostic 	{ 0122240, "ENPR" },
124033444Sbostic 	{ 0122300, "Estep 3" },
124133444Sbostic 	{ 0142300, "Estep 4" },
124233444Sbostic 	{ 0, " unknown error code" }
124333444Sbostic };
124433444Sbostic 
12454743Swnj /*
124633444Sbostic  * If the error bit was set in the controller status register, gripe,
124733444Sbostic  * then (optionally) reset the controller and requeue pending transfers.
12484743Swnj  */
udasaerror(um,doreset)124933444Sbostic udasaerror(um, doreset)
125032523Sbostic 	register struct uba_ctlr *um;
125133444Sbostic 	int doreset;
12524743Swnj {
125333444Sbostic 	register int code = ((struct udadevice *)um->um_addr)->udasa;
125433444Sbostic 	register struct saerr *e;
125532523Sbostic 
125633444Sbostic 	if ((code & UDA_ERR) == 0)
125733444Sbostic 		return;
125833444Sbostic 	for (e = saerr; e->code; e++)
125933444Sbostic 		if (e->code == code)
126033444Sbostic 			break;
126133444Sbostic 	printf("uda%d: controller error, sa=0%o (%s%s)\n",
126233444Sbostic 		um->um_ctlr, code, e->desc + 1,
126333444Sbostic 		*e->desc == 'E' ? " error" : "");
126433444Sbostic 	if (doreset) {
126533444Sbostic 		mscp_requeue(&uda_softc[um->um_ctlr].sc_mi);
126633444Sbostic 		(void) udainit(um->um_ctlr);
126733444Sbostic 	}
126832523Sbostic }
126932523Sbostic 
127032523Sbostic /*
127132523Sbostic  * Interrupt routine.  Depending on the state of the controller,
127232523Sbostic  * continue initialisation, or acknowledge command and response
127332523Sbostic  * interrupts, and process responses.
127432523Sbostic  */
udaintr(ctlr)127532523Sbostic udaintr(ctlr)
127632523Sbostic 	int ctlr;
127732523Sbostic {
127832523Sbostic 	register struct uba_ctlr *um = udaminfo[ctlr];
127932523Sbostic 	register struct uda_softc *sc = &uda_softc[ctlr];
128033444Sbostic 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
128132523Sbostic 	register struct uda *ud;
128232523Sbostic 	register struct mscp *mp;
12834743Swnj 	register int i;
12844743Swnj 
128535401Stef #ifdef QBA
128636036Skarels 	splx(sc->sc_ipl);	/* Qbus interrupt protocol is odd */
128727254Skridle #endif
128832523Sbostic 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
128932523Sbostic 
129032523Sbostic 	/*
129132523Sbostic 	 * Combinations during steps 1, 2, and 3: STEPnMASK
129232523Sbostic 	 * corresponds to which bits should be tested;
129332523Sbostic 	 * STEPnGOOD corresponds to the pattern that should
129432523Sbostic 	 * appear after the interrupt from STEPn initialisation.
129532523Sbostic 	 * All steps test the bits in ALLSTEPS.
129632523Sbostic 	 */
129732523Sbostic #define	ALLSTEPS	(UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1)
129832523Sbostic 
129932523Sbostic #define	STEP1MASK	(ALLSTEPS | UDA_IE | UDA_NCNRMASK)
130032523Sbostic #define	STEP1GOOD	(UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2)
130132523Sbostic 
130232523Sbostic #define	STEP2MASK	(ALLSTEPS | UDA_IE | UDA_IVECMASK)
130332523Sbostic #define	STEP2GOOD	(UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2))
130432523Sbostic 
130532523Sbostic #define	STEP3MASK	ALLSTEPS
130632523Sbostic #define	STEP3GOOD	UDA_STEP4
130732523Sbostic 
13084743Swnj 	switch (sc->sc_state) {
130932523Sbostic 
131032523Sbostic 	case ST_IDLE:
131132523Sbostic 		/*
131232523Sbostic 		 * Ignore unsolicited interrupts.
131332523Sbostic 		 */
131432523Sbostic 		log(LOG_WARNING, "uda%d: stray intr\n", ctlr);
13154743Swnj 		return;
13164743Swnj 
131732523Sbostic 	case ST_STEP1:
131832523Sbostic 		/*
131932523Sbostic 		 * Begin step two initialisation.
132032523Sbostic 		 */
132132523Sbostic 		if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) {
132232523Sbostic 			i = 1;
132332523Sbostic initfailed:
132432523Sbostic 			printf("uda%d: init step %d failed, sa=%b\n",
132532523Sbostic 				ctlr, i, udaddr->udasa, udasr_bits);
132633444Sbostic 			udasaerror(um, 0);
132732523Sbostic 			sc->sc_state = ST_IDLE;
132832523Sbostic 			if (sc->sc_flags & SC_DOWAKE) {
132932523Sbostic 				sc->sc_flags &= ~SC_DOWAKE;
133033444Sbostic 				wakeup((caddr_t)sc);
133132523Sbostic 			}
13324743Swnj 			return;
13334743Swnj 		}
133433444Sbostic 		udaddr->udasa = (int)&sc->sc_uda->uda_ca.ca_rspdsc[0] |
133532523Sbostic 			(cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0);
133632523Sbostic 		sc->sc_state = ST_STEP2;
13374743Swnj 		return;
13384743Swnj 
133932523Sbostic 	case ST_STEP2:
134032523Sbostic 		/*
134132523Sbostic 		 * Begin step 3 initialisation.
134232523Sbostic 		 */
134332523Sbostic 		if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) {
134432523Sbostic 			i = 2;
134532523Sbostic 			goto initfailed;
13464743Swnj 		}
134733444Sbostic 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16;
134832523Sbostic 		sc->sc_state = ST_STEP3;
13494743Swnj 		return;
13504743Swnj 
135132523Sbostic 	case ST_STEP3:
135232523Sbostic 		/*
135332523Sbostic 		 * Set controller characteristics (finish initialisation).
135432523Sbostic 		 */
135532523Sbostic 		if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) {
135632523Sbostic 			i = 3;
135732523Sbostic 			goto initfailed;
13584743Swnj 		}
135932523Sbostic 		i = udaddr->udasa & 0xff;
136032523Sbostic 		if (i != sc->sc_micro) {
136132523Sbostic 			sc->sc_micro = i;
136232523Sbostic 			printf("uda%d: version %d model %d\n",
136332523Sbostic 				ctlr, i & 0xf, i >> 4);
136432523Sbostic 		}
136532523Sbostic 
136617553Skarels 		/*
136732523Sbostic 		 * Present the burst size, then remove it.  Why this
136832523Sbostic 		 * should be done this way, I have no idea.
136932523Sbostic 		 *
137032523Sbostic 		 * Note that this assumes udaburst[ctlr] > 0.
137117553Skarels 		 */
137232523Sbostic 		udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2;
13734743Swnj 		udaddr->udasa = UDA_GO;
137432523Sbostic 		printf("uda%d: DMA burst size set to %d\n",
137532523Sbostic 			ctlr, udaburst[ctlr]);
13764743Swnj 
137732523Sbostic 		udainitds(ctlr);	/* initialise data structures */
137832523Sbostic 
13794743Swnj 		/*
138032523Sbostic 		 * Before we can get a command packet, we need some
138132523Sbostic 		 * credits.  Fake some up to keep mscp_getcp() happy,
138232523Sbostic 		 * get a packet, and cancel all credits (the right
138332523Sbostic 		 * number should come back in the response to the
138432523Sbostic 		 * SCC packet).
13854743Swnj 		 */
138632523Sbostic 		sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1;
138732523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT);
138832523Sbostic 		if (mp == NULL)	/* `cannot happen' */
138932523Sbostic 			panic("udaintr");
139032523Sbostic 		sc->sc_mi.mi_credits = 0;
139132523Sbostic 		mp->mscp_opcode = M_OP_SETCTLRC;
139232523Sbostic 		mp->mscp_unit = 0;
139332523Sbostic 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
139432523Sbostic 			M_CF_THIS;
139532523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
139632523Sbostic 		i = udaddr->udaip;
139732523Sbostic 		sc->sc_state = ST_SETCHAR;
13984743Swnj 		return;
13994743Swnj 
140032523Sbostic 	case ST_SETCHAR:
140132523Sbostic 	case ST_RUN:
140232523Sbostic 		/*
140332523Sbostic 		 * Handle Set Ctlr Characteristics responses and operational
140432523Sbostic 		 * responses (via mscp_dorsp).
140532523Sbostic 		 */
14064743Swnj 		break;
14074743Swnj 
14084743Swnj 	default:
140932523Sbostic 		printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state);
141032523Sbostic 		panic("udastate");
14114743Swnj 	}
14124743Swnj 
141332523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
141433444Sbostic 		udasaerror(um, 1);
141532523Sbostic 		return;
14164743Swnj 	}
14174743Swnj 
141832523Sbostic 	ud = &uda[ctlr];
141932523Sbostic 
14204743Swnj 	/*
142132523Sbostic 	 * Handle buffer purge requests.
14224743Swnj 	 */
14234743Swnj 	if (ud->uda_ca.ca_bdp) {
142426372Skarels 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
14254743Swnj 		ud->uda_ca.ca_bdp = 0;
142632523Sbostic 		udaddr->udasa = 0;	/* signal purge complete */
14274743Swnj 	}
14284743Swnj 
14294743Swnj 	/*
143032523Sbostic 	 * Check for response and command ring transitions.
14314743Swnj 	 */
14324743Swnj 	if (ud->uda_ca.ca_rspint) {
14334743Swnj 		ud->uda_ca.ca_rspint = 0;
143432523Sbostic 		mscp_dorsp(&sc->sc_mi);
14354743Swnj 	}
14364743Swnj 	if (ud->uda_ca.ca_cmdint) {
14374743Swnj 		ud->uda_ca.ca_cmdint = 0;
143832523Sbostic 		MSCP_DOCMD(&sc->sc_mi);
14394743Swnj 	}
144032523Sbostic 	udastart(um);
14414743Swnj }
14424743Swnj 
14434743Swnj /*
144432523Sbostic  * Initialise the various data structures that control the UDA50.
144517553Skarels  */
udainitds(ctlr)144632523Sbostic udainitds(ctlr)
144732523Sbostic 	int ctlr;
144832523Sbostic {
144932523Sbostic 	register struct uda *ud = &uda[ctlr];
145032523Sbostic 	register struct uda *uud = uda_softc[ctlr].sc_uda;
145132523Sbostic 	register struct mscp *mp;
145232523Sbostic 	register int i;
14534743Swnj 
145432523Sbostic 	for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) {
145532523Sbostic 		ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
145632523Sbostic 			(long)&uud->uda_rsp[i].mscp_cmdref;
145732523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i];
145832523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
14594743Swnj 	}
146032523Sbostic 	for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) {
146132523Sbostic 		ud->uda_ca.ca_cmddsc[i] = MSCP_INT |
146232523Sbostic 			(long)&uud->uda_cmd[i].mscp_cmdref;
146332523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i];
146432523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
146532523Sbostic 	}
14664743Swnj }
14674743Swnj 
14684743Swnj /*
146933444Sbostic  * Handle an error datagram.
14704743Swnj  */
147132523Sbostic udadgram(mi, mp)
147232523Sbostic 	struct mscp_info *mi;
147332523Sbostic 	struct mscp *mp;
14744743Swnj {
147517553Skarels 
147632523Sbostic 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
147733444Sbostic 	/*
147833444Sbostic 	 * SDI status information bytes 10 and 11 are the microprocessor
147933444Sbostic 	 * error code and front panel code respectively.  These vary per
148033444Sbostic 	 * drive type and are printed purely for field service information.
148133444Sbostic 	 */
148233444Sbostic 	if (mp->mscp_format == M_FM_SDI)
148333444Sbostic 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
148433444Sbostic 			mp->mscp_erd.erd_sdistat[10],
148533444Sbostic 			mp->mscp_erd.erd_sdistat[11]);
148632523Sbostic }
148717553Skarels 
148832523Sbostic /*
148932523Sbostic  * The Set Controller Characteristics command finished.
149032523Sbostic  * Record the new state of the controller.
149132523Sbostic  */
udactlrdone(mi,mp)149232523Sbostic udactlrdone(mi, mp)
149332523Sbostic 	register struct mscp_info *mi;
149432523Sbostic 	struct mscp *mp;
149532523Sbostic {
149632523Sbostic 	register struct uda_softc *sc = &uda_softc[mi->mi_ctlr];
149717553Skarels 
149832523Sbostic 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
149932523Sbostic 		sc->sc_state = ST_RUN;
150032523Sbostic 	else {
150132523Sbostic 		printf("uda%d: SETCTLRC failed: ",
150232523Sbostic 			mi->mi_ctlr, mp->mscp_status);
150332523Sbostic 		mscp_printevent(mp);
150432523Sbostic 		sc->sc_state = ST_IDLE;
15054743Swnj 	}
150632523Sbostic 	if (sc->sc_flags & SC_DOWAKE) {
150732523Sbostic 		sc->sc_flags &= ~SC_DOWAKE;
150832523Sbostic 		wakeup((caddr_t)sc);
15096964Ssam 	}
15104743Swnj }
15114743Swnj 
15124743Swnj /*
151332523Sbostic  * Received a response from an as-yet unconfigured drive.  Configure it
151432523Sbostic  * in, if possible.
15154743Swnj  */
151632523Sbostic udaunconf(mi, mp)
151732523Sbostic 	struct mscp_info *mi;
151832523Sbostic 	register struct mscp *mp;
15194743Swnj {
15204743Swnj 
152117553Skarels 	/*
152232523Sbostic 	 * If it is a slave response, copy it to udaslavereply for
152332523Sbostic 	 * udaslave() to look at.
152417553Skarels 	 */
152532523Sbostic 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
152632523Sbostic 	    (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) {
152732523Sbostic 		udaslavereply = *mp;
152832523Sbostic 		return (MSCP_DONE);
15294743Swnj 	}
153032523Sbostic 
153132523Sbostic 	/*
153232523Sbostic 	 * Otherwise, it had better be an available attention response.
153332523Sbostic 	 */
153432523Sbostic 	if (mp->mscp_opcode != M_OP_AVAILATTN)
153532523Sbostic 		return (MSCP_FAILED);
153632523Sbostic 
153732523Sbostic 	/* do what autoconf does */
153832523Sbostic 	return (MSCP_FAILED);	/* not yet, arwhite, not yet */
15394743Swnj }
15404743Swnj 
154132523Sbostic /*
154232523Sbostic  * A drive came on line.  Check its type and size.  Return DONE if
154332523Sbostic  * we think the drive is truly on line.  In any case, awaken anyone
154432523Sbostic  * sleeping on the drive on-line-ness.
154532523Sbostic  */
udaonline(ui,mp)154632523Sbostic udaonline(ui, mp)
154732523Sbostic 	register struct uba_device *ui;
154832523Sbostic 	struct mscp *mp;
15494743Swnj {
155032523Sbostic 	register struct ra_info *ra = &ra_info[ui->ui_unit];
15514743Swnj 
155232523Sbostic 	wakeup((caddr_t)&ui->ui_flags);
155332523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
155436036Skarels 		if (!cold)
155536036Skarels 			printf("uda%d: ra%d", ui->ui_ctlr, ui->ui_unit);
155636036Skarels 		printf(": attempt to bring on line failed: ");
155732523Sbostic 		mscp_printevent(mp);
155832523Sbostic 		ra->ra_state = CLOSED;
155932523Sbostic 		return (MSCP_FAILED);
156032523Sbostic 	}
156132523Sbostic 
156232523Sbostic 	ra->ra_state = OPENRAW;
156332523Sbostic 	ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize;
156434283Skarels 	if (!cold)
156534283Skarels 		printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit,
156634283Skarels 		    ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize);
156732523Sbostic 	/* can now compute ncyl */
156832523Sbostic 	ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks /
156932523Sbostic 		ra->ra_geom.rg_nsectors;
157032523Sbostic 	return (MSCP_DONE);
15714743Swnj }
15724743Swnj 
157332523Sbostic /*
157432523Sbostic  * We got some (configured) unit's status.  Return DONE if it succeeded.
157532523Sbostic  */
udagotstatus(ui,mp)157632523Sbostic udagotstatus(ui, mp)
157732523Sbostic 	register struct uba_device *ui;
157832523Sbostic 	register struct mscp *mp;
15794743Swnj {
15804743Swnj 
158132523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
158232523Sbostic 		printf("uda%d: attempt to get status for ra%d failed: ",
158332523Sbostic 			ui->ui_ctlr, ui->ui_unit);
158432523Sbostic 		mscp_printevent(mp);
158532523Sbostic 		return (MSCP_FAILED);
158632523Sbostic 	}
158732523Sbostic 	/* record for (future) bad block forwarding and whatever else */
158832523Sbostic 	uda_rasave(ui->ui_unit, mp, 1);
158932523Sbostic 	return (MSCP_DONE);
15904743Swnj }
15914743Swnj 
159232523Sbostic /*
159332523Sbostic  * A transfer failed.  We get a chance to fix or restart it.
159432523Sbostic  * Need to write the bad block forwaring code first....
159532523Sbostic  */
159632523Sbostic /*ARGSUSED*/
udaioerror(ui,mp,bp)159732523Sbostic udaioerror(ui, mp, bp)
159832523Sbostic 	register struct uba_device *ui;
159932523Sbostic 	register struct mscp *mp;
160032523Sbostic 	struct buf *bp;
16014743Swnj {
16024743Swnj 
160332523Sbostic 	if (mp->mscp_flags & M_EF_BBLKR) {
160432523Sbostic 		/*
160532523Sbostic 		 * A bad block report.  Eventually we will
160632523Sbostic 		 * restart this transfer, but for now, just
160732523Sbostic 		 * log it and give up.
160832523Sbostic 		 */
160932523Sbostic 		log(LOG_ERR, "ra%d: bad block report: %d%s\n",
161032523Sbostic 			ui->ui_unit, mp->mscp_seq.seq_lbn,
161132523Sbostic 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
161232523Sbostic 	} else {
161332523Sbostic 		/*
161432523Sbostic 		 * What the heck IS a `serious exception' anyway?
161532523Sbostic 		 * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION
161632523Sbostic 		 * FOR THEIR OWN CONTROLLERS.
161732523Sbostic 		 */
161832523Sbostic 		if (mp->mscp_flags & M_EF_SEREX)
161932523Sbostic 			log(LOG_ERR, "ra%d: serious exception reported\n",
162032523Sbostic 				ui->ui_unit);
16214743Swnj 	}
162232523Sbostic 	return (MSCP_FAILED);
16234743Swnj }
16244743Swnj 
162532523Sbostic /*
162632523Sbostic  * A replace operation finished.
162732523Sbostic  */
162832523Sbostic /*ARGSUSED*/
162932523Sbostic udareplace(ui, mp)
163032523Sbostic 	struct uba_device *ui;
163132523Sbostic 	struct mscp *mp;
16324743Swnj {
163317553Skarels 
163432523Sbostic 	panic("udareplace");
16354743Swnj }
163617553Skarels 
163732523Sbostic /*
163832523Sbostic  * A bad block related operation finished.
163932523Sbostic  */
164032523Sbostic /*ARGSUSED*/
164132523Sbostic udabb(ui, mp, bp)
164232523Sbostic 	struct uba_device *ui;
164332523Sbostic 	struct mscp *mp;
164432523Sbostic 	struct buf *bp;
164517553Skarels {
164617553Skarels 
164732523Sbostic 	panic("udabb");
164817553Skarels }
164917553Skarels 
165032523Sbostic 
165132523Sbostic /*
165232523Sbostic  * I/O controls.
165332523Sbostic  */
udaioctl(dev,cmd,data,flag)165432523Sbostic udaioctl(dev, cmd, data, flag)
165512511Ssam 	dev_t dev;
165630536Skarels 	int cmd;
165730536Skarels 	caddr_t data;
165830536Skarels 	int flag;
165912511Ssam {
166032523Sbostic 	register int unit = udaunit(dev);
166130536Skarels 	register struct disklabel *lp;
166233443Skarels 	register struct ra_info *ra = &ra_info[unit];
166334638Skarels 	int error = 0;
166412511Ssam 
166532523Sbostic 	lp = &udalabel[unit];
166630536Skarels 
166730536Skarels 	switch (cmd) {
166830536Skarels 
166930536Skarels 	case DIOCGDINFO:
167030536Skarels 		*(struct disklabel *)data = *lp;
167130536Skarels 		break;
167230536Skarels 
167330773Skarels 	case DIOCGPART:
167430773Skarels 		((struct partinfo *)data)->disklab = lp;
167530773Skarels 		((struct partinfo *)data)->part =
167632523Sbostic 		    &lp->d_partitions[udapart(dev)];
167730536Skarels 		break;
167830536Skarels 
167930536Skarels 	case DIOCSDINFO:
168030536Skarels 		if ((flag & FWRITE) == 0)
168130536Skarels 			error = EBADF;
168230536Skarels 		else
168332574Skarels 			error = setdisklabel(lp, (struct disklabel *)data,
168434283Skarels 			    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart);
168530536Skarels 		break;
168630536Skarels 
168733443Skarels 	case DIOCWLABEL:
168833443Skarels 		if ((flag & FWRITE) == 0)
168933443Skarels 			error = EBADF;
169033443Skarels 		else
169133443Skarels 			ra->ra_wlabel = *(int *)data;
169233443Skarels 		break;
169333443Skarels 
169432574Skarels 	case DIOCWDINFO:
169532574Skarels 		if ((flag & FWRITE) == 0)
169632523Sbostic 			error = EBADF;
169732574Skarels 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
169834283Skarels 		    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart)) == 0) {
169934638Skarels 			int wlab;
170034638Skarels 
170134283Skarels 			ra->ra_state = OPEN;
170234638Skarels 			/* simulate opening partition 0 so write succeeds */
170334638Skarels 			ra->ra_openpart |= (1 << 0);		/* XXX */
170434638Skarels 			wlab = ra->ra_wlabel;
170534638Skarels 			ra->ra_wlabel = 1;
170632574Skarels 			error = writedisklabel(dev, udastrategy, lp);
170734638Skarels 			ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
170834638Skarels 			ra->ra_wlabel = wlab;
170934283Skarels 		}
171030536Skarels 		break;
171130536Skarels 
171232523Sbostic #ifdef notyet
171332523Sbostic 	case UDAIOCREPLACE:
171432523Sbostic 		/*
171532523Sbostic 		 * Initiate bad block replacement for the given LBN.
171632523Sbostic 		 * (Should we allow modifiers?)
171732523Sbostic 		 */
171832523Sbostic 		error = EOPNOTSUPP;
171932523Sbostic 		break;
172032523Sbostic 
172132523Sbostic 	case UDAIOCGMICRO:
172232523Sbostic 		/*
172332523Sbostic 		 * Return the microcode revision for the UDA50 running
172432523Sbostic 		 * this drive.
172532523Sbostic 		 */
172633444Sbostic 		*(int *)data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro;
172732523Sbostic 		break;
172832523Sbostic #endif
172932523Sbostic 
173030536Skarels 	default:
173130536Skarels 		error = ENOTTY;
173230536Skarels 		break;
173330536Skarels 	}
173432523Sbostic 	return (error);
173532523Sbostic }
173632523Sbostic 
173732523Sbostic /*
173832523Sbostic  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
173932523Sbostic  * on that Unibus, and requeue outstanding I/O.
174032523Sbostic  */
udareset(uban)174132523Sbostic udareset(uban)
174232523Sbostic 	int uban;
174332523Sbostic {
174432523Sbostic 	register struct uba_ctlr *um;
174532523Sbostic 	register struct uda_softc *sc;
174632523Sbostic 	register int ctlr;
174732523Sbostic 
174832523Sbostic 	for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) {
174932523Sbostic 		if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban ||
175032523Sbostic 		    um->um_alive == 0)
175132523Sbostic 			continue;
175232523Sbostic 		printf(" uda%d", ctlr);
175332523Sbostic 
175432523Sbostic 		/*
175532523Sbostic 		 * Our BDP (if any) is gone; our command (if any) is
175632523Sbostic 		 * flushed; the device is no longer mapped; and the
175732523Sbostic 		 * UDA50 is not yet initialised.
175832523Sbostic 		 */
175932523Sbostic 		if (um->um_bdp) {
176032523Sbostic 			printf("<%d>", UBAI_BDP(um->um_bdp));
176132523Sbostic 			um->um_bdp = 0;
176232523Sbostic 		}
176332523Sbostic 		um->um_ubinfo = 0;
176432523Sbostic 		um->um_cmd = 0;
176532523Sbostic 		sc->sc_flags &= ~SC_MAPPED;
176632523Sbostic 		sc->sc_state = ST_IDLE;
176732523Sbostic 
176832523Sbostic 		/* reset queues and requeue pending transfers */
176932523Sbostic 		mscp_requeue(&sc->sc_mi);
177032523Sbostic 
177132523Sbostic 		/*
177232523Sbostic 		 * If it fails to initialise we will notice later and
177332523Sbostic 		 * try again (and again...).  Do not call udastart()
177432523Sbostic 		 * here; it will be done after the controller finishes
177532523Sbostic 		 * initialisation.
177632523Sbostic 		 */
177732523Sbostic 		if (udainit(ctlr))
177832523Sbostic 			printf(" (hung)");
177932523Sbostic 	}
178032523Sbostic }
178132523Sbostic 
178232523Sbostic /*
178332523Sbostic  * Watchdog timer:  If the controller is active, and no interrupts
178432523Sbostic  * have occurred for 30 seconds, assume it has gone away.
178532523Sbostic  */
udawatch()178632523Sbostic udawatch()
178732523Sbostic {
178832523Sbostic 	register int i;
178932523Sbostic 	register struct uba_ctlr *um;
179032523Sbostic 	register struct uda_softc *sc;
179132523Sbostic 
179232523Sbostic 	timeout(udawatch, (caddr_t) 0, hz);	/* every second */
179332523Sbostic 	for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) {
179432523Sbostic 		if ((um = udaminfo[i]) == 0 || !um->um_alive)
179532523Sbostic 			continue;
179632523Sbostic 		if (sc->sc_state == ST_IDLE)
179732523Sbostic 			continue;
179832523Sbostic 		if (sc->sc_state == ST_RUN && !um->um_tab.b_active)
179932523Sbostic 			sc->sc_wticks = 0;
180032523Sbostic 		else if (++sc->sc_wticks >= 30) {
180132523Sbostic 			sc->sc_wticks = 0;
180232523Sbostic 			printf("uda%d: lost interrupt\n", i);
180332523Sbostic 			ubareset(um->um_ubanum);
180432523Sbostic 		}
180532523Sbostic 	}
180632523Sbostic }
180732523Sbostic 
180832523Sbostic /*
180932523Sbostic  * Do a panic dump.  We set up the controller for one command packet
181032523Sbostic  * and one response packet, for which we use `struct uda1'.
181132523Sbostic  */
181232523Sbostic struct	uda1 {
181332523Sbostic 	struct	uda1ca uda1_ca;	/* communications area */
181432523Sbostic 	struct	mscp uda1_rsp;	/* response packet */
181532523Sbostic 	struct	mscp uda1_cmd;	/* command packet */
181632523Sbostic } uda1;
181732523Sbostic 
181832523Sbostic #define	DBSIZE	32		/* dump 16K at a time */
181932523Sbostic 
udadump(dev)182032523Sbostic udadump(dev)
182132523Sbostic 	dev_t dev;
182232523Sbostic {
182332523Sbostic 	struct udadevice *udaddr;
182432523Sbostic 	struct uda1 *ud_ubaddr;
182532523Sbostic 	char *start;
182632523Sbostic 	int num, blk, unit, maxsz, blkoff, reg;
182732523Sbostic 	struct partition *pp;
182832523Sbostic 	register struct uba_regs *uba;
182932523Sbostic 	register struct uba_device *ui;
183032523Sbostic 	register struct uda1 *ud;
183132523Sbostic 	register struct pte *io;
183232523Sbostic 	register int i;
183332523Sbostic 
183432523Sbostic 	/*
183532523Sbostic 	 * Make sure the device is a reasonable place on which to dump.
183632523Sbostic 	 */
183732523Sbostic 	unit = udaunit(dev);
183832523Sbostic 	if (unit >= NRA)
183932523Sbostic 		return (ENXIO);
184033444Sbostic #define	phys(cast, addr)	((cast) ((int)addr & 0x7fffffff))
184132523Sbostic 	ui = phys(struct uba_device *, udadinfo[unit]);
184232523Sbostic 	if (ui == NULL || ui->ui_alive == 0)
184332523Sbostic 		return (ENXIO);
184432523Sbostic 
184532523Sbostic 	/*
184632523Sbostic 	 * Find and initialise the UBA; get the physical address of the
184732523Sbostic 	 * device registers, and of communications area and command and
184832523Sbostic 	 * response packet.
184932523Sbostic 	 */
185032523Sbostic 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
185132523Sbostic 	ubainit(uba);
185232523Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
185332523Sbostic 	ud = phys(struct uda1 *, &uda1);
185432523Sbostic 
185532523Sbostic 	/*
185632523Sbostic 	 * Map the ca+packets into Unibus I/O space so the UDA50 can get
185732523Sbostic 	 * at them.  Use the registers at the end of the Unibus map (since
185832523Sbostic 	 * we will use the registers at the beginning to map the memory
185932523Sbostic 	 * we are dumping).
186032523Sbostic 	 */
186132523Sbostic 	num = btoc(sizeof(struct uda1)) + 1;
186232523Sbostic 	reg = NUBMREG - num;
186332523Sbostic 	io = &uba->uba_map[reg];
186432523Sbostic 	for (i = 0; i < num; i++)
186532523Sbostic 		*(int *)io++ = UBAMR_MRV | (btop(ud) + i);
186632523Sbostic 	ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9));
186732523Sbostic 
186832523Sbostic 	/*
186932523Sbostic 	 * Initialise the controller, with one command and one response
187032523Sbostic 	 * packet.
187132523Sbostic 	 */
187232523Sbostic 	udaddr->udaip = 0;
187332523Sbostic 	if (udadumpwait(udaddr, UDA_STEP1))
187432523Sbostic 		return (EFAULT);
187532523Sbostic 	udaddr->udasa = UDA_ERR;
187632523Sbostic 	if (udadumpwait(udaddr, UDA_STEP2))
187732523Sbostic 		return (EFAULT);
187832523Sbostic 	udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc;
187932523Sbostic 	if (udadumpwait(udaddr, UDA_STEP3))
188032523Sbostic 		return (EFAULT);
188132523Sbostic 	udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16;
188232523Sbostic 	if (udadumpwait(udaddr, UDA_STEP4))
188332523Sbostic 		return (EFAULT);
188432523Sbostic 	uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff;
188532523Sbostic 	udaddr->udasa = UDA_GO;
188632523Sbostic 
188732523Sbostic 	/*
188832523Sbostic 	 * Set up the command and response descriptor, then set the
188932523Sbostic 	 * controller characteristics and bring the drive on line.
189032523Sbostic 	 * Note that all uninitialised locations in uda1_cmd are zero.
189132523Sbostic 	 */
189232523Sbostic 	ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref;
189332523Sbostic 	ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref;
189432523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */
189532523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */
189632523Sbostic 	if (udadumpcmd(M_OP_SETCTLRC, ud, ui))
189732523Sbostic 		return (EFAULT);
189832523Sbostic 	ud->uda1_cmd.mscp_unit = ui->ui_slave;
189932523Sbostic 	if (udadumpcmd(M_OP_ONLINE, ud, ui))
190032523Sbostic 		return (EFAULT);
190132523Sbostic 
190232523Sbostic 	pp = phys(struct partition *,
190332523Sbostic 	    &udalabel[unit].d_partitions[udapart(dev)]);
190432523Sbostic 	maxsz = pp->p_size;
190532523Sbostic 	blkoff = pp->p_offset;
190632523Sbostic 
190732523Sbostic 	/*
190832523Sbostic 	 * Dump all of physical memory, or as much as will fit in the
190932523Sbostic 	 * space provided.
191032523Sbostic 	 */
191132523Sbostic 	start = 0;
191232523Sbostic 	num = maxfree;
191332523Sbostic 	if (dumplo + num >= maxsz)
191432523Sbostic 		num = maxsz - dumplo;
191532523Sbostic 	blkoff += dumplo;
191632523Sbostic 
191732523Sbostic 	/*
191832523Sbostic 	 * Write out memory, DBSIZE pages at a time.
191932523Sbostic 	 * N.B.: this code depends on the fact that the sector
192032523Sbostic 	 * size == the page size.
192132523Sbostic 	 */
192232523Sbostic 	while (num > 0) {
192332523Sbostic 		blk = num > DBSIZE ? DBSIZE : num;
192432523Sbostic 		io = uba->uba_map;
192532523Sbostic 		/*
192632523Sbostic 		 * Map in the pages to write, leaving an invalid entry
192732523Sbostic 		 * at the end to guard against wild Unibus transfers.
192832523Sbostic 		 * Then do the write.
192932523Sbostic 		 */
193032523Sbostic 		for (i = 0; i < blk; i++)
193133444Sbostic 			*(int *)io++ = UBAMR_MRV | (btop(start) + i);
193233444Sbostic 		*(int *)io = 0;
193332523Sbostic 		ud->uda1_cmd.mscp_unit = ui->ui_slave;
193432523Sbostic 		ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
193532523Sbostic 		ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
193632523Sbostic 		if (udadumpcmd(M_OP_WRITE, ud, ui))
193732523Sbostic 			return (EIO);
193832523Sbostic 		start += blk << PGSHIFT;
193932523Sbostic 		num -= blk;
194032523Sbostic 	}
194132523Sbostic 	return (0);		/* made it! */
194232523Sbostic }
194332523Sbostic 
194432523Sbostic /*
194532523Sbostic  * Wait for some of the bits in `bits' to come on.  If the error bit
194632523Sbostic  * comes on, or ten seconds pass without response, return true (error).
194732523Sbostic  */
udadumpwait(udaddr,bits)194832523Sbostic udadumpwait(udaddr, bits)
194932523Sbostic 	register struct udadevice *udaddr;
195032523Sbostic 	register int bits;
195132523Sbostic {
195232523Sbostic 	register int timo = todr() + 1000;
195332523Sbostic 
195432523Sbostic 	while ((udaddr->udasa & bits) == 0) {
195532523Sbostic 		if (udaddr->udasa & UDA_ERR) {
195632523Sbostic 			printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
195732523Sbostic 			return (1);
195832523Sbostic 		}
195932523Sbostic 		if (todr() >= timo) {
196032523Sbostic 			printf("timeout\ndump ");
196132523Sbostic 			return (1);
196232523Sbostic 		}
196332523Sbostic 	}
196430536Skarels 	return (0);
196530536Skarels }
196630536Skarels 
196732523Sbostic /*
196832523Sbostic  * Feed a command to the UDA50, wait for its response, and return
196932523Sbostic  * true iff something went wrong.
197032523Sbostic  */
udadumpcmd(op,ud,ui)197132523Sbostic udadumpcmd(op, ud, ui)
197232523Sbostic 	int op;
197332523Sbostic 	register struct uda1 *ud;
197432523Sbostic 	struct uba_device *ui;
197532523Sbostic {
197632523Sbostic 	register struct udadevice *udaddr;
197732523Sbostic 	register int n;
197832523Sbostic #define mp (&ud->uda1_rsp)
197932523Sbostic 
198033444Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
198132523Sbostic 	ud->uda1_cmd.mscp_opcode = op;
198232523Sbostic 	ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN;
198332523Sbostic 	ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN;
198432523Sbostic 	ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
198532523Sbostic 	ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
198632523Sbostic 	if (udaddr->udasa & UDA_ERR) {
198732523Sbostic 		printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
198832523Sbostic 		return (1);
198932523Sbostic 	}
199032523Sbostic 	n = udaddr->udaip;
199132523Sbostic 	n = todr() + 1000;
199232523Sbostic 	for (;;) {
199332523Sbostic 		if (todr() > n) {
199432523Sbostic 			printf("timeout\ndump ");
199532523Sbostic 			return (1);
199632523Sbostic 		}
199732523Sbostic 		if (ud->uda1_ca.ca_cmdint)
199832523Sbostic 			ud->uda1_ca.ca_cmdint = 0;
199932523Sbostic 		if (ud->uda1_ca.ca_rspint == 0)
200032523Sbostic 			continue;
200132523Sbostic 		ud->uda1_ca.ca_rspint = 0;
200232523Sbostic 		if (mp->mscp_opcode == (op | M_OP_END))
200332523Sbostic 			break;
200432523Sbostic 		printf("\n");
200532523Sbostic 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
200632523Sbostic 
200732523Sbostic 		case MSCPT_SEQ:
200832523Sbostic 			printf("sequential");
200932523Sbostic 			break;
201032523Sbostic 
201132523Sbostic 		case MSCPT_DATAGRAM:
201232523Sbostic 			mscp_decodeerror("uda", ui->ui_ctlr, mp);
201332523Sbostic 			printf("datagram");
201432523Sbostic 			break;
201532523Sbostic 
201632523Sbostic 		case MSCPT_CREDITS:
201732523Sbostic 			printf("credits");
201832523Sbostic 			break;
201932523Sbostic 
202032523Sbostic 		case MSCPT_MAINTENANCE:
202132523Sbostic 			printf("maintenance");
202232523Sbostic 			break;
202332523Sbostic 
202432523Sbostic 		default:
202532523Sbostic 			printf("unknown (type 0x%x)",
202632523Sbostic 				MSCP_MSGTYPE(mp->mscp_msgtc));
202732523Sbostic 			break;
202832523Sbostic 		}
202932523Sbostic 		printf(" ignored\ndump ");
203032523Sbostic 		ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
203132523Sbostic 	}
203232523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
203332523Sbostic 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
203432523Sbostic 			mp->mscp_opcode, mp->mscp_status);
203532523Sbostic 		return (1);
203632523Sbostic 	}
203732523Sbostic 	return (0);
203832523Sbostic #undef mp
203932523Sbostic }
204032523Sbostic 
204132523Sbostic /*
204232523Sbostic  * Return the size of a partition, if known, or -1 if not.
204332523Sbostic  */
udasize(dev)204432523Sbostic udasize(dev)
204530536Skarels 	dev_t dev;
204630536Skarels {
204732523Sbostic 	register int unit = udaunit(dev);
204830536Skarels 	register struct uba_device *ui;
204930536Skarels 
205032523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == NULL ||
205132523Sbostic 	    ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 ||
205232523Sbostic 	    ra_info[unit].ra_state != OPEN)
205312511Ssam 		return (-1);
205432523Sbostic 	return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size);
205512511Ssam }
205617553Skarels 
205730536Skarels #ifdef COMPAT_42
205832523Sbostic /*
205932523Sbostic  * Tables mapping unlabelled drives.
206032523Sbostic  */
206130536Skarels struct size {
206230536Skarels 	daddr_t nblocks;
206330536Skarels 	daddr_t blkoff;
206433444Sbostic } ra60_sizes[8] = {
206530536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
206630536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
206730536Skarels 	400176,	0,		/* C=sectors 0 thru 400175 */
206830536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
206930536Skarels 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
207030536Skarels 	350852,	49324,		/* F=sectors 49324 thru 400175 */
207130536Skarels 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
207230536Skarels 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
207333444Sbostic }, ra70_sizes[8] = {
207433444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
207533444Sbostic 	33440,	15972,		/* B=blk 15972 thru 49323 */
207633444Sbostic 	-1,	0,		/* C=blk 0 thru end */
207733444Sbostic 	15884,	341220,		/* D=blk 341220 thru 357103 */
207833444Sbostic 	55936,	357192,		/* E=blk 357192 thru 413127 */
207933444Sbostic 	-1,	413457,		/* F=blk 413457 thru end */
208033444Sbostic 	-1,	341220,		/* G=blk 341220 thru end */
208133444Sbostic 	291346,	49731,		/* H=blk 49731 thru 341076 */
208230536Skarels }, ra80_sizes[8] = {
208330536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
208430536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
208530536Skarels 	242606,	0,		/* C=sectors 0 thru 242605 */
208630536Skarels 	0,	0,		/* D=unused */
208730536Skarels 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
208830536Skarels 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
208930536Skarels 	192696,	49910,		/* G=sectors 49910 thru 242605 */
209030536Skarels 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
209130536Skarels }, ra81_sizes[8] ={
209230536Skarels /*
209330536Skarels  * These are the new standard partition sizes for ra81's.
209430536Skarels  * An RA_COMPAT system is compiled with D, E, and F corresponding
209530536Skarels  * to the 4.2 partitions for G, H, and F respectively.
209630536Skarels  */
209730536Skarels #ifndef	UCBRA
209830536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
209930536Skarels 	66880,	16422,		/* B=sectors 16422 thru 83301 */
210030536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
210130536Skarels #ifdef RA_COMPAT
210230536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
210330536Skarels 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
210430536Skarels 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
210530536Skarels #else
210630536Skarels 	15884,	375564,		/* D=sectors 375564 thru 391447 */
210730536Skarels 	307200,	391986,		/* E=sectors 391986 thru 699185 */
210830536Skarels 	191352,	699720,		/* F=sectors 699720 thru 891071 */
210930536Skarels #endif RA_COMPAT
211030536Skarels 	515508,	375564,		/* G=sectors 375564 thru 891071 */
211130536Skarels 	291346,	83538,		/* H=sectors 83538 thru 374883 */
211230536Skarels 
211330536Skarels /*
211430536Skarels  * These partitions correspond to the sizes used by sites at Berkeley,
211530536Skarels  * and by those sites that have received copies of the Berkeley driver
211630536Skarels  * with deltas 6.2 or greater (11/15/83).
211730536Skarels  */
211830536Skarels #else UCBRA
211930536Skarels 
212030536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
212130536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
212230536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
212330536Skarels 	15884,	242606,		/* D=sectors 242606 thru 258489 */
212430536Skarels 	307200,	258490,		/* E=sectors 258490 thru 565689 */
212530536Skarels 	325382,	565690,		/* F=sectors 565690 thru 891071 */
212630536Skarels 	648466,	242606,		/* G=sectors 242606 thru 891071 */
212730536Skarels 	193282,	49324,		/* H=sectors 49324 thru 242605 */
212830536Skarels 
212930536Skarels #endif UCBRA
213033444Sbostic }, ra82_sizes[8] = {
213133444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
213233444Sbostic 	66880,	16245,		/* B=blk 16245 thru 83124 */
213333444Sbostic 	-1,	0,		/* C=blk 0 thru end */
213433444Sbostic 	15884,	375345,		/* D=blk 375345 thru 391228 */
213533444Sbostic 	307200,	391590,		/* E=blk 391590 thru 698789 */
213633444Sbostic 	-1,	699390,		/* F=blk 699390 thru end */
213733444Sbostic 	-1,	375345,		/* G=blk 375345 thru end */
213833444Sbostic 	291346,	83790,		/* H=blk 83790 thru 375135 */
213933444Sbostic }, rc25_sizes[8] = {
214033444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
214133444Sbostic 	10032,	15884,		/* B=blk 15884 thru 49323 */
214233444Sbostic 	-1,	0,		/* C=blk 0 thru end */
214333444Sbostic 	0,	0,		/* D=blk 340670 thru 356553 */
214433444Sbostic 	0,	0,		/* E=blk 356554 thru 412489 */
214533444Sbostic 	0,	0,		/* F=blk 412490 thru end */
214633444Sbostic 	-1,	25916,		/* G=blk 49324 thru 131403 */
214733444Sbostic 	0,	0,		/* H=blk 131404 thru end */
214833444Sbostic }, rd52_sizes[8] = {
214933444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
215033444Sbostic 	9766,	15884,		/* B=blk 15884 thru 25649 */
215133444Sbostic 	-1,	0,		/* C=blk 0 thru end */
215233444Sbostic 	0,	0,		/* D=unused */
215333444Sbostic 	0,	0,		/* E=unused */
215433444Sbostic 	0,	0,		/* F=unused */
215533444Sbostic 	-1,	25650,		/* G=blk 25650 thru end */
215633444Sbostic 	0,	0,		/* H=unused */
215733444Sbostic }, rd53_sizes[8] = {
215833444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
215933444Sbostic 	33440,	15884,		/* B=blk 15884 thru 49323 */
216033444Sbostic 	-1,	0,		/* C=blk 0 thru end */
216133444Sbostic 	0,	0,		/* D=unused */
216233444Sbostic 	33440,	0,		/* E=blk 0 thru 33439 */
216333444Sbostic 	-1,	33440,		/* F=blk 33440 thru end */
216433444Sbostic 	-1,	49324,		/* G=blk 49324 thru end */
216533444Sbostic 	-1,	15884,		/* H=blk 15884 thru end */
216645577Storek }, rd54_sizes[8] = {
216745577Storek 	15884,	0,		/* A=blk 0 thru 15883 */
216845577Storek 	33440,	15884,		/* B=blk 15884 thru 49323 */
216945577Storek 	-1,	0,		/* C=blk 0 thru end */
217045577Storek 	130938,	49324,		/* D=blk 49324 thru 180261 */
217145577Storek 	130938,	180262,		/* E=blk 180262 thru 311199 (end) */
217245577Storek 	0,	0,		/* F=unused */
217345577Storek 	261876,	49324,		/* G=blk 49324 thru 311199 (end) */
217445577Storek 	0,	0,		/* H=unused */
217533444Sbostic }, rx50_sizes[8] = {
217633444Sbostic 	800,	0,		/* A=blk 0 thru 799 */
217733444Sbostic 	0,	0,
217833444Sbostic 	-1,	0,		/* C=blk 0 thru end */
217933444Sbostic 	0,	0,
218033444Sbostic 	0,	0,
218133444Sbostic 	0,	0,
218233444Sbostic 	0,	0,
218333444Sbostic 	0,	0,
218430536Skarels };
218530536Skarels 
218632523Sbostic /*
218733444Sbostic  * Media ID decoding table.
218832523Sbostic  */
218932523Sbostic struct	udatypes {
219033444Sbostic 	u_long	ut_id;		/* media drive ID */
219132523Sbostic 	char	*ut_name;	/* drive type name */
219232523Sbostic 	struct	size *ut_sizes;	/* partition tables */
219332523Sbostic 	int	ut_nsectors, ut_ntracks, ut_ncylinders;
219432523Sbostic } udatypes[] = {
219533444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 60), "ra60", ra60_sizes, 42, 4, 2382 },
219633444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 70), "ra70", ra70_sizes, 33, 11, 1507 },
219733444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 80), "ra80", ra80_sizes, 31, 14, 559 },
219833444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 81), "ra81", ra81_sizes, 51, 14, 1248 },
2199*46402Storek 	{ MSCP_MKDRIVE2('R', 'A', 82), "ra82", ra82_sizes, 57, 15, 1423 },
220033444Sbostic 	{ MSCP_MKDRIVE2('R', 'C', 25), "rc25-removable",
220133444Sbostic 						rc25_sizes, 42, 4, 302 },
220233444Sbostic 	{ MSCP_MKDRIVE3('R', 'C', 'F', 25), "rc25-fixed",
220333444Sbostic 						rc25_sizes, 42, 4, 302 },
220433444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 52), "rd52", rd52_sizes, 18, 7, 480 },
220533444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 53), "rd53", rd53_sizes, 18, 8, 963 },
220645577Storek 	{ MSCP_MKDRIVE2('R', 'D', 32), "rd54-from-rd32",
220745577Storek 						rd54_sizes, 17, 15, 1220 },
220845577Storek 	{ MSCP_MKDRIVE2('R', 'D', 54), "rd54", rd54_sizes, 17, 15, 1220 },
220933444Sbostic 	{ MSCP_MKDRIVE2('R', 'X', 50), "rx50", rx50_sizes, 10, 1, 80 },
221033444Sbostic 	0
221132523Sbostic };
221232523Sbostic 
221332523Sbostic #define NTYPES (sizeof(udatypes) / sizeof(*udatypes))
221432523Sbostic 
udamaptype(unit,lp)221532523Sbostic udamaptype(unit, lp)
221632523Sbostic 	int unit;
221730536Skarels 	register struct disklabel *lp;
221830536Skarels {
221932523Sbostic 	register struct udatypes *ut;
222032523Sbostic 	register struct size *sz;
222130536Skarels 	register struct partition *pp;
222232523Sbostic 	register char *p;
222332523Sbostic 	register int i;
222432523Sbostic 	register struct ra_info *ra = &ra_info[unit];
222530536Skarels 
222633444Sbostic 	i = MSCP_MEDIA_DRIVE(ra->ra_mediaid);
222733444Sbostic 	for (ut = udatypes; ut->ut_id; ut++)
222836036Skarels 		if (ut->ut_id == i &&
222936036Skarels 		    ut->ut_nsectors == ra->ra_geom.rg_nsectors &&
223036036Skarels 		    ut->ut_ntracks == ra->ra_geom.rg_ntracks &&
223136036Skarels 		    ut->ut_ncylinders == ra->ra_geom.rg_ncyl)
223233444Sbostic 			goto found;
223333444Sbostic 
223433444Sbostic 	/* not one we know; fake up a label for the whole drive */
223536036Skarels 	uda_makefakelabel(ra, lp);
223633444Sbostic 	i = ra->ra_mediaid;	/* print the port type too */
223736036Skarels 	addlog(": no partition table for %c%c %c%c%c%d, size %d;\n\
223834283Skarels using (s,t,c)=(%d,%d,%d)",
223934283Skarels 		MSCP_MID_CHAR(4, i), MSCP_MID_CHAR(3, i),
224033444Sbostic 		MSCP_MID_CHAR(2, i), MSCP_MID_CHAR(1, i),
224136036Skarels 		MSCP_MID_CHAR(0, i), MSCP_MID_NUM(i), lp->d_secperunit,
224236036Skarels 		lp->d_nsectors, lp->d_ntracks, lp->d_ncylinders);
224334283Skarels 	if (!cold)
224434283Skarels 		addlog("\n");
224533444Sbostic 	return (0);
224633444Sbostic found:
224732523Sbostic 	p = ut->ut_name;
224832523Sbostic 	for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++)
224932523Sbostic 		lp->d_typename[i] = *p++;
225032523Sbostic 	lp->d_typename[i] = 0;
225132523Sbostic 	sz = ut->ut_sizes;
225232523Sbostic 	lp->d_nsectors = ut->ut_nsectors;
225332523Sbostic 	lp->d_ntracks = ut->ut_ntracks;
225432523Sbostic 	lp->d_ncylinders = ut->ut_ncylinders;
225530536Skarels 	lp->d_npartitions = 8;
225630536Skarels 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
225732523Sbostic 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) {
225832523Sbostic 		pp->p_offset = sz->blkoff;
225932523Sbostic 		if ((pp->p_size = sz->nblocks) == (u_long)-1)
226032523Sbostic 			pp->p_size = ra->ra_dsize - sz->blkoff;
226130536Skarels 	}
226230536Skarels 	return (1);
226330536Skarels }
226432523Sbostic #endif /* COMPAT_42 */
226536036Skarels 
226636036Skarels /*
226736036Skarels  * Construct a label for a drive from geometry information
226836036Skarels  * if we have no better information.
226936036Skarels  */
uda_makefakelabel(ra,lp)227036036Skarels uda_makefakelabel(ra, lp)
227136036Skarels 	register struct ra_info *ra;
227236036Skarels 	register struct disklabel *lp;
227336036Skarels {
227436036Skarels 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
227536036Skarels 	lp->d_ntracks = ra->ra_geom.rg_ntracks;
227636036Skarels 	lp->d_ncylinders = ra->ra_geom.rg_ncyl;
227736036Skarels 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
227836036Skarels 	bcopy("ra??", lp->d_typename, sizeof("ra??"));
227936036Skarels 	lp->d_npartitions = 1;
228036036Skarels 	lp->d_partitions[0].p_offset = 0;
228136036Skarels 	lp->d_partitions[0].p_size = lp->d_secperunit;
228236036Skarels }
228332523Sbostic #endif /* NUDA > 0 */
2284