xref: /csrg-svn/sys/vax/uba/uda.c (revision 35039)
123353Smckusick /*
2*35039Sbostic  * Copyright (c) 1988 Regents of the University of California.
3*35039Sbostic  * All rights reserved.
432523Sbostic  *
5*35039Sbostic  * This code is derived from software contributed to Berkeley by
6*35039Sbostic  * Chris Torek.
732523Sbostic  *
8*35039Sbostic  * Redistribution and use in source and binary forms are permitted
9*35039Sbostic  * provided that the above copyright notice and this paragraph are
10*35039Sbostic  * duplicated in all such forms and that any documentation,
11*35039Sbostic  * advertising materials, and other materials related to such
12*35039Sbostic  * distribution and use acknowledge that the software was developed
13*35039Sbostic  * by the University of California, Berkeley.  The name of the
14*35039Sbostic  * University may not be used to endorse or promote products derived
15*35039Sbostic  * from this software without specific prior written permission.
16*35039Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17*35039Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18*35039Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19*35039Sbostic  *
20*35039Sbostic  *	@(#)uda.c	7.19 (Berkeley) 07/09/88
2123353Smckusick  */
2223353Smckusick 
2332523Sbostic /*
2432523Sbostic  * UDA50/MSCP device driver
2517553Skarels  */
2617553Skarels 
2732523Sbostic #define	POLLSTATS
2832523Sbostic 
2932523Sbostic /*
3032523Sbostic  * TODO
3132523Sbostic  *	write bad block forwarding code
3232523Sbostic  */
3332523Sbostic 
344743Swnj #include "ra.h"
3532523Sbostic 
3617642Skarels #if NUDA > 0
3732523Sbostic 
384743Swnj /*
3932523Sbostic  * CONFIGURATION OPTIONS.  The next three defines are tunable -- tune away!
404743Swnj  *
4132523Sbostic  * COMPAT_42 enables 4.2/4.3 compatibility (label mapping)
4232523Sbostic  *
4332523Sbostic  * NRSPL2 and NCMDL2 control the number of response and command
4432523Sbostic  * packets respectively.  They may be any value from 0 to 7, though
4532523Sbostic  * setting them higher than 5 is unlikely to be of any value.
4632523Sbostic  * If you get warnings about your command ring being too small,
4732523Sbostic  * try increasing the values by one.
4832523Sbostic  *
4932523Sbostic  * MAXUNIT controls the maximum unit number (number of drives per
5032523Sbostic  * controller) we are prepared to handle.
5132523Sbostic  *
5232523Sbostic  * DEFAULT_BURST must be at least 1.
534743Swnj  */
5432523Sbostic #define	COMPAT_42
5532523Sbostic 
5632523Sbostic #define	NRSPL2	5		/* log2 number of response packets */
5732523Sbostic #define NCMDL2	5		/* log2 number of command packets */
5832523Sbostic #define	MAXUNIT	8		/* maximum allowed unit number */
5932523Sbostic #define	DEFAULT_BURST	4	/* default DMA burst size */
6032523Sbostic 
6117553Skarels #include "param.h"
6217553Skarels #include "systm.h"
6317553Skarels #include "buf.h"
6417553Skarels #include "conf.h"
6517553Skarels #include "dir.h"
6630536Skarels #include "file.h"
6730536Skarels #include "ioctl.h"
6817553Skarels #include "user.h"
6917553Skarels #include "map.h"
7017553Skarels #include "vm.h"
7130536Skarels #include "dkstat.h"
7217553Skarels #include "cmap.h"
7330536Skarels #include "disklabel.h"
7430536Skarels #include "syslog.h"
7530773Skarels #include "stat.h"
764743Swnj 
7734283Skarels #include "../machine/pte.h"
7834283Skarels 
798482Sroot #include "../vax/cpu.h"
8017553Skarels #include "ubareg.h"
8117553Skarels #include "ubavar.h"
828613Sroot 
8332523Sbostic #define	NRSP	(1 << NRSPL2)
8432523Sbostic #define	NCMD	(1 << NCMDL2)
858613Sroot 
8632523Sbostic #include "udareg.h"
878482Sroot #include "../vax/mscp.h"
8832523Sbostic #include "../vax/mscpvar.h"
8932523Sbostic #include "../vax/mtpr.h"
904743Swnj 
9132523Sbostic /*
9232523Sbostic  * UDA communications area and MSCP packet pools, per controller.
9332523Sbostic  */
9432523Sbostic struct	uda {
9532523Sbostic 	struct	udaca uda_ca;		/* communications area */
9632523Sbostic 	struct	mscp uda_rsp[NRSP];	/* response packets */
9732523Sbostic 	struct	mscp uda_cmd[NCMD];	/* command packets */
984743Swnj } uda[NUDA];
994743Swnj 
10032523Sbostic /*
10132523Sbostic  * Software status, per controller.
10232523Sbostic  */
10332523Sbostic struct	uda_softc {
10432523Sbostic 	struct	uda *sc_uda;	/* Unibus address of uda struct */
10532523Sbostic 	short	sc_state;	/* UDA50 state; see below */
10632523Sbostic 	short	sc_flags;	/* flags; see below */
10732523Sbostic 	int	sc_micro;	/* microcode revision */
10832523Sbostic 	int	sc_ivec;	/* interrupt vector address */
10932523Sbostic 	struct	mscp_info sc_mi;/* MSCP info (per mscpvar.h) */
11032523Sbostic #ifndef POLLSTATS
11132523Sbostic 	int	sc_wticks;	/* watchdog timer ticks */
11232523Sbostic #else
11332523Sbostic 	short	sc_wticks;
11432523Sbostic 	short	sc_ncmd;
11532523Sbostic #endif
11632523Sbostic } uda_softc[NUDA];
11724742Sbloom 
11832523Sbostic #ifdef POLLSTATS
11932523Sbostic struct udastats {
12032523Sbostic 	int	ncmd;
12132523Sbostic 	int	cmd[NCMD + 1];
12232523Sbostic } udastats = { NCMD + 1 };
12332523Sbostic #endif
12417553Skarels 
12532523Sbostic /*
12632523Sbostic  * Controller states
12732523Sbostic  */
12832523Sbostic #define	ST_IDLE		0	/* uninitialised */
12932523Sbostic #define	ST_STEP1	1	/* in `STEP 1' */
13032523Sbostic #define	ST_STEP2	2	/* in `STEP 2' */
13132523Sbostic #define	ST_STEP3	3	/* in `STEP 3' */
13232523Sbostic #define	ST_SETCHAR	4	/* in `Set Controller Characteristics' */
13332523Sbostic #define	ST_RUN		5	/* up and running */
1344743Swnj 
13532523Sbostic /*
13632523Sbostic  * Flags
13732523Sbostic  */
13832523Sbostic #define	SC_MAPPED	0x01	/* mapped in Unibus I/O space */
13932523Sbostic #define	SC_INSTART	0x02	/* inside udastart() */
14032523Sbostic #define	SC_GRIPED	0x04	/* griped about cmd ring too small */
14132523Sbostic #define	SC_INSLAVE	0x08	/* inside udaslave() */
14232523Sbostic #define	SC_DOWAKE	0x10	/* wakeup when ctlr init done */
14332523Sbostic #define	SC_STARTPOLL	0x20	/* need to initiate polling */
14412421Ssam 
14532523Sbostic /*
14632523Sbostic  * Device to unit number and partition and back
14732523Sbostic  */
14832523Sbostic #define	UNITSHIFT	3
14932523Sbostic #define	UNITMASK	7
15032523Sbostic #define	udaunit(dev)	(minor(dev) >> UNITSHIFT)
15132523Sbostic #define	udapart(dev)	(minor(dev) & UNITMASK)
15232523Sbostic #define	udaminor(u, p)	(((u) << UNITSHIFT) | (p))
1534743Swnj 
15417553Skarels /*
15532523Sbostic  * Drive status, per drive
15617553Skarels  */
15732523Sbostic struct ra_info {
15832523Sbostic 	daddr_t	ra_dsize;	/* size in sectors */
15933444Sbostic /*	u_long	ra_type;	/* drive type */
16032523Sbostic 	u_long	ra_mediaid;	/* media id */
16132523Sbostic 	int	ra_state;	/* open/closed state */
16232523Sbostic 	struct	ra_geom {	/* geometry information */
16332523Sbostic 		u_short	rg_nsectors;	/* sectors/track */
16432523Sbostic 		u_short	rg_ngroups;	/* track groups */
16532523Sbostic 		u_short	rg_ngpc;	/* groups/cylinder */
16632523Sbostic 		u_short	rg_ntracks;	/* ngroups*ngpc */
16732523Sbostic 		u_short	rg_ncyl;	/* ra_dsize/ntracks/nsectors */
16832523Sbostic #ifdef notyet
16932523Sbostic 		u_short	rg_rctsize;	/* size of rct */
17032523Sbostic 		u_short	rg_rbns;	/* replacement blocks per track */
17132523Sbostic 		u_short	rg_nrct;	/* number of rct copies */
17232523Sbostic #endif
17332523Sbostic 	} ra_geom;
17433443Skarels 	int	ra_wlabel;	/* label sector is currently writable */
17532523Sbostic 	u_long	ra_openpart;	/* partitions open */
17632523Sbostic 	u_long	ra_bopenpart;	/* block partitions open */
17732523Sbostic 	u_long	ra_copenpart;	/* character partitions open */
17832523Sbostic } ra_info[NRA];
17917553Skarels 
18030536Skarels /*
18130536Skarels  * Software state, per drive
18230536Skarels  */
18330536Skarels #define	CLOSED		0
18430536Skarels #define	WANTOPEN	1
18530536Skarels #define	RDLABEL		2
18630536Skarels #define	OPEN		3
18730536Skarels #define	OPENRAW		4
18817553Skarels 
18932523Sbostic /*
19032523Sbostic  * Definition of the driver for autoconf.
19132523Sbostic  */
19232523Sbostic int	udaprobe(), udaslave(), udaattach(), udadgo(), udaintr();
19332523Sbostic struct	uba_ctlr *udaminfo[NUDA];
19432523Sbostic struct	uba_device *udadinfo[NRA];
19532523Sbostic struct	disklabel udalabel[NRA];
19617553Skarels 
19732523Sbostic u_short	udastd[] = { 0772150, 0772550, 0777550, 0 };
19832523Sbostic struct	uba_driver udadriver =
19932523Sbostic  { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda",
20032523Sbostic    udaminfo };
20117553Skarels 
20232523Sbostic /*
20332523Sbostic  * More driver definitions, for generic MSCP code.
20432523Sbostic  */
20532523Sbostic int	udadgram(), udactlrdone(), udaunconf(), udaiodone();
20632523Sbostic int	udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb();
2074743Swnj 
20832523Sbostic struct	buf udautab[NRA];	/* per drive transfer queue */
2094743Swnj 
21032523Sbostic struct	mscp_driver udamscpdriver =
21134524Skarels  { MAXUNIT, NRA, UNITSHIFT, udautab, udalabel, udadinfo,
21232523Sbostic    udadgram, udactlrdone, udaunconf, udaiodone,
21332523Sbostic    udaonline, udagotstatus, udareplace, udaioerror, udabb,
21432523Sbostic    "uda", "ra" };
21532523Sbostic 
21632523Sbostic /*
21732523Sbostic  * Miscellaneous private variables.
21832523Sbostic  */
21932523Sbostic char	udasr_bits[] = UDASR_BITS;
22032523Sbostic 
22132523Sbostic struct	uba_device *udaip[NUDA][MAXUNIT];
22232523Sbostic 				/* inverting pointers: ctlr & unit => Unibus
22332523Sbostic 				   device pointer */
22432523Sbostic 
22532523Sbostic int	udaburst[NUDA] = { 0 };	/* burst size, per UDA50, zero => default;
22632523Sbostic 				   in data space so patchable via adb */
22732523Sbostic 
22832523Sbostic struct	mscp udaslavereply;	/* get unit status response packet, set
22932523Sbostic 				   for udaslave by udaunconf, via udaintr */
23032523Sbostic 
23132523Sbostic static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr
23232523Sbostic 				   info to slave routine; instead, we remember
23332523Sbostic 				   the last ctlr argument to probe */
23432523Sbostic 
23532523Sbostic int	udawstart, udawatch();	/* watchdog timer */
23632523Sbostic 
23732523Sbostic /*
23832523Sbostic  * Externals
23932523Sbostic  */
24032523Sbostic int	wakeup();
24132523Sbostic int	hz;
24232523Sbostic 
24332523Sbostic /*
24432523Sbostic  * Poke at a supposed UDA50 to see if it is there.
24532523Sbostic  * This routine duplicates some of the code in udainit() only
24632523Sbostic  * because autoconf has not set up the right information yet.
24732523Sbostic  * We have to do everything `by hand'.
24832523Sbostic  */
24932523Sbostic udaprobe(reg, ctlr, um)
2504743Swnj 	caddr_t reg;
2514743Swnj 	int ctlr;
25232523Sbostic 	struct uba_ctlr *um;
2534743Swnj {
2544743Swnj 	register int br, cvec;
25532523Sbostic 	register struct uda_softc *sc;
25632523Sbostic 	register struct udadevice *udaddr;
25732523Sbostic 	register struct mscp_info *mi;
25832523Sbostic 	int timeout, tries;
2594743Swnj 
26032523Sbostic #ifdef VAX750
26132523Sbostic 	/*
26232523Sbostic 	 * The UDA50 wants to share BDPs on 750s, but not on 780s or
26332523Sbostic 	 * 8600s.  (730s have no BDPs anyway.)  Toward this end, we
26432523Sbostic 	 * here set the `keep bdp' flag in the per-driver information
26532523Sbostic 	 * if this is a 750.  (We just need to do it once, but it is
26632523Sbostic 	 * easiest to do it now, for each UDA50.)
26732523Sbostic 	 */
26832523Sbostic 	if (cpu == VAX_750)
26932523Sbostic 		udadriver.ud_keepbdp = 1;
27032523Sbostic #endif
27117553Skarels 
27232523Sbostic 	probeum = um;			/* remember for udaslave() */
2734743Swnj #ifdef lint
27432523Sbostic 	br = 0; cvec = br; br = cvec; udaintr(0);
2754743Swnj #endif
27632523Sbostic 	/*
27732523Sbostic 	 * Set up the controller-specific generic MSCP driver info.
27832523Sbostic 	 * Note that this should really be done in the (nonexistent)
27932523Sbostic 	 * controller attach routine.
28032523Sbostic 	 */
28132523Sbostic 	sc = &uda_softc[ctlr];
28232523Sbostic 	mi = &sc->sc_mi;
28332523Sbostic 	mi->mi_md = &udamscpdriver;
28432523Sbostic 	mi->mi_ctlr = um->um_ctlr;
28532523Sbostic 	mi->mi_tab = &um->um_tab;
28632523Sbostic 	mi->mi_ip = udaip[ctlr];
28732523Sbostic 	mi->mi_cmd.mri_size = NCMD;
28832523Sbostic 	mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc;
28932523Sbostic 	mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd;
29032523Sbostic 	mi->mi_rsp.mri_size = NRSP;
29132523Sbostic 	mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc;
29232523Sbostic 	mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp;
29332523Sbostic 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
29432523Sbostic 
29532523Sbostic 	/*
29632523Sbostic 	 * More controller specific variables.  Again, this should
29732523Sbostic 	 * be in the controller attach routine.
29832523Sbostic 	 */
29932523Sbostic 	if (udaburst[ctlr] == 0)
30032523Sbostic 		udaburst[ctlr] = DEFAULT_BURST;
30132523Sbostic 
30232523Sbostic 	/*
30332523Sbostic 	 * Get an interrupt vector.  Note that even if the controller
30432523Sbostic 	 * does not respond, we keep the vector.  This is not a serious
30532523Sbostic 	 * problem; but it would be easily fixed if we had a controller
30632523Sbostic 	 * attach routine.  Sigh.
30732523Sbostic 	 */
30832523Sbostic 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
30917553Skarels 	udaddr = (struct udadevice *) reg;
31017553Skarels 
31132523Sbostic 	/*
31232523Sbostic 	 * Initialise the controller (partially).  The UDA50 programmer's
31332523Sbostic 	 * manual states that if initialisation fails, it should be retried
31432523Sbostic 	 * at least once, but after a second failure the port should be
31532523Sbostic 	 * considered `down'; it also mentions that the controller should
31632523Sbostic 	 * initialise within ten seconds.  Or so I hear; I have not seen
31732523Sbostic 	 * this manual myself.
31832523Sbostic 	 */
31932523Sbostic 	tries = 0;
32032523Sbostic again:
32132523Sbostic 	udaddr->udaip = 0;		/* start initialisation */
32232523Sbostic 	timeout = todr() + 1000;	/* timeout in 10 seconds */
32332523Sbostic 	while ((udaddr->udasa & UDA_STEP1) == 0)
32432523Sbostic 		if (todr() > timeout)
32532523Sbostic 			goto bad;
32632523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
32732523Sbostic 		(sc->sc_ivec >> 2);
32832523Sbostic 	while ((udaddr->udasa & UDA_STEP2) == 0)
32932523Sbostic 		if (todr() > timeout)
33032523Sbostic 			goto bad;
33132523Sbostic 
33232523Sbostic 	/* should have interrupted by now */
33332523Sbostic #ifdef VAX630
33432523Sbostic 	if (cpu == VAX_630)
33532523Sbostic 		br = 0x15;	/* screwy interrupt structure */
33627254Skridle #endif
33732523Sbostic 	return (sizeof (struct udadevice));
33832523Sbostic bad:
33932523Sbostic 	if (++tries < 2)
34032523Sbostic 		goto again;
34132523Sbostic 	return (0);
3424743Swnj }
3434743Swnj 
34432523Sbostic /*
34532523Sbostic  * Find a slave.  We allow wildcard slave numbers (something autoconf
34632523Sbostic  * is not really prepared to deal with); and we need to know the
34732523Sbostic  * controller number to talk to the UDA.  For the latter, we keep
34832523Sbostic  * track of the last controller probed, since a controller probe
34932523Sbostic  * immediately precedes all slave probes for that controller.  For the
35032523Sbostic  * former, we simply put the unit number into ui->ui_slave after we
35132523Sbostic  * have found one.
35232523Sbostic  *
35332523Sbostic  * Note that by the time udaslave is called, the interrupt vector
35432523Sbostic  * for the UDA50 has been set up (so that udaunconf() will be called).
35532523Sbostic  */
35632523Sbostic udaslave(ui, reg)
35732523Sbostic 	register struct uba_device *ui;
3584743Swnj 	caddr_t reg;
3594743Swnj {
36032523Sbostic 	register struct uba_ctlr *um = probeum;
36132523Sbostic 	register struct mscp *mp;
36232523Sbostic 	register struct uda_softc *sc;
36333443Skarels 	int next = 0, timeout, tries, i;
36417553Skarels 
36532523Sbostic #ifdef lint
36632523Sbostic 	i = 0; i = i;
36732523Sbostic #endif
36832523Sbostic 	/*
36932523Sbostic 	 * Make sure the controller is fully initialised, by waiting
37032523Sbostic 	 * for it if necessary.
37132523Sbostic 	 */
37232523Sbostic 	sc = &uda_softc[um->um_ctlr];
37332523Sbostic 	if (sc->sc_state == ST_RUN)
37432523Sbostic 		goto findunit;
37532523Sbostic 	tries = 0;
37632523Sbostic again:
37732523Sbostic 	if (udainit(ui->ui_ctlr))
37832523Sbostic 		return (0);
37932523Sbostic 	timeout = todr() + 1000;		/* 10 seconds */
38032523Sbostic 	while (todr() < timeout)
38132523Sbostic 		if (sc->sc_state == ST_RUN)	/* made it */
38232523Sbostic 			goto findunit;
38332523Sbostic 	if (++tries < 2)
38432523Sbostic 		goto again;
38532523Sbostic 	printf("uda%d: controller hung\n", um->um_ctlr);
38632523Sbostic 	return (0);
38717553Skarels 
38832523Sbostic 	/*
38932523Sbostic 	 * The controller is all set; go find the unit.  Grab an
39032523Sbostic 	 * MSCP packet and send out a Get Unit Status command, with
39132523Sbostic 	 * the `next unit' modifier if we are looking for a generic
39232523Sbostic 	 * unit.  We set the `in slave' flag so that udaunconf()
39332523Sbostic 	 * knows to copy the response to `udaslavereply'.
39432523Sbostic 	 */
39532523Sbostic findunit:
39632523Sbostic 	udaslavereply.mscp_opcode = 0;
39732523Sbostic 	sc->sc_flags |= SC_INSLAVE;
39832523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL)
39932523Sbostic 		panic("udaslave");		/* `cannot happen' */
40032523Sbostic 	mp->mscp_opcode = M_OP_GETUNITST;
40132523Sbostic 	if (ui->ui_slave == '?') {
40232523Sbostic 		mp->mscp_unit = next;
40332523Sbostic 		mp->mscp_modifier = M_GUM_NEXTUNIT;
40432523Sbostic 	} else {
40532523Sbostic 		mp->mscp_unit = ui->ui_slave;
40632523Sbostic 		mp->mscp_modifier = 0;
40717553Skarels 	}
40832523Sbostic 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
40932523Sbostic 	i = ((struct udadevice *) reg)->udaip;	/* initiate polling */
41032523Sbostic 	mp = &udaslavereply;
41132523Sbostic 	timeout = todr() + 1000;
41232523Sbostic 	while (todr() < timeout)
41332523Sbostic 		if (mp->mscp_opcode)
41432523Sbostic 			goto gotit;
41532523Sbostic 	printf("uda%d: no response to Get Unit Status request\n",
41632523Sbostic 		um->um_ctlr);
41732523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
41832523Sbostic 	return (0);
41932523Sbostic 
42032523Sbostic gotit:
42132523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
42232523Sbostic 
42332523Sbostic 	/*
42432523Sbostic 	 * Got a slave response.  If the unit is there, use it.
42532523Sbostic 	 */
42632523Sbostic 	switch (mp->mscp_status & M_ST_MASK) {
42732523Sbostic 
42832523Sbostic 	case M_ST_SUCCESS:	/* worked */
42932523Sbostic 	case M_ST_AVAILABLE:	/* found another drive */
43032523Sbostic 		break;		/* use it */
43132523Sbostic 
43232523Sbostic 	case M_ST_OFFLINE:
43332523Sbostic 		/*
43432523Sbostic 		 * Figure out why it is off line.  It may be because
43532523Sbostic 		 * it is nonexistent, or because it is spun down, or
43632523Sbostic 		 * for some other reason.
43732523Sbostic 		 */
43832523Sbostic 		switch (mp->mscp_status & ~M_ST_MASK) {
43932523Sbostic 
44032523Sbostic 		case M_OFFLINE_UNKNOWN:
44132523Sbostic 			/*
44232523Sbostic 			 * No such drive, and there are none with
44332523Sbostic 			 * higher unit numbers either, if we are
44432523Sbostic 			 * using M_GUM_NEXTUNIT.
44532523Sbostic 			 */
44632523Sbostic 			return (0);
44732523Sbostic 
44832523Sbostic 		case M_OFFLINE_UNMOUNTED:
44932523Sbostic 			/*
45032523Sbostic 			 * The drive is not spun up.  Use it anyway.
45132523Sbostic 			 *
45232523Sbostic 			 * N.B.: this seems to be a common occurrance
45332523Sbostic 			 * after a power failure.  The first attempt
45432523Sbostic 			 * to bring it on line seems to spin it up
45532523Sbostic 			 * (and thus takes several minutes).  Perhaps
45632523Sbostic 			 * we should note here that the on-line may
45732523Sbostic 			 * take longer than usual.
45832523Sbostic 			 */
45932523Sbostic 			break;
46032523Sbostic 
46132523Sbostic 		default:
46232523Sbostic 			/*
46332523Sbostic 			 * In service, or something else equally unusable.
46432523Sbostic 			 */
46532523Sbostic 			printf("uda%d: unit %d off line: ", um->um_ctlr,
46632523Sbostic 				mp->mscp_unit);
46732523Sbostic 			mscp_printevent(mp);
46832523Sbostic 			goto try_another;
46932523Sbostic 		}
47032523Sbostic 		break;
47132523Sbostic 
47232523Sbostic 	default:
47332523Sbostic 		printf("uda%d: unable to get unit status: ", um->um_ctlr);
47432523Sbostic 		mscp_printevent(mp);
47532523Sbostic 		return (0);
47617553Skarels 	}
47732523Sbostic 
47832523Sbostic 	/*
47932523Sbostic 	 * Does this ever happen?  What (if anything) does it mean?
48032523Sbostic 	 */
48132523Sbostic 	if (mp->mscp_unit < next) {
48232523Sbostic 		printf("uda%d: unit %d, next %d\n",
48332523Sbostic 			um->um_ctlr, mp->mscp_unit, next);
48432523Sbostic 		return (0);
48517553Skarels 	}
48632523Sbostic 
48732523Sbostic 	if (mp->mscp_unit >= MAXUNIT) {
48832523Sbostic 		printf("uda%d: cannot handle unit number %d (max is %d)\n",
48932523Sbostic 			um->um_ctlr, mp->mscp_unit, MAXUNIT - 1);
49032523Sbostic 		return (0);
49132523Sbostic 	}
49232523Sbostic 
49332523Sbostic 	/*
49432523Sbostic 	 * See if we already handle this drive.
49532523Sbostic 	 * (Only likely if ui->ui_slave=='?'.)
49632523Sbostic 	 */
49732523Sbostic 	if (udaip[um->um_ctlr][mp->mscp_unit] != NULL) {
49832523Sbostic try_another:
49932523Sbostic 		if (ui->ui_slave != '?')
50032523Sbostic 			return (0);
50132523Sbostic 		next = mp->mscp_unit + 1;
50232523Sbostic 		goto findunit;
50332523Sbostic 	}
50432523Sbostic 
50532523Sbostic 	/*
50632523Sbostic 	 * Voila!
50732523Sbostic 	 */
50832523Sbostic 	uda_rasave(ui->ui_unit, mp, 0);
50932523Sbostic 	ui->ui_flags = 0;	/* not on line, nor anything else */
51032523Sbostic 	ui->ui_slave = mp->mscp_unit;
51132523Sbostic 	return (1);
5124743Swnj }
5134743Swnj 
51432523Sbostic /*
51532523Sbostic  * Attach a found slave.  Make sure the watchdog timer is running.
51632523Sbostic  * If this disk is being profiled, fill in the `mspw' value (used by
51732523Sbostic  * what?).  Set up the inverting pointer, and attempt to bring the
51832523Sbostic  * drive on line and read its label.
51932523Sbostic  */
52032523Sbostic udaattach(ui)
5214743Swnj 	register struct uba_device *ui;
5224743Swnj {
52332523Sbostic 	register int unit = ui->ui_unit;
52432523Sbostic 
52532523Sbostic 	if (udawstart == 0) {
52632523Sbostic 		timeout(udawatch, (caddr_t) 0, hz);
52732523Sbostic 		udawstart++;
52832523Sbostic 	}
52933444Sbostic 
53033444Sbostic 	/*
53133444Sbostic 	 * Floppies cannot be brought on line unless there is
53233444Sbostic 	 * a disk in the drive.  Since an ONLINE while cold
53333444Sbostic 	 * takes ten seconds to fail, and (when notyet becomes now)
53433444Sbostic 	 * no sensible person will swap to one, we just
53533444Sbostic 	 * defer the ONLINE until someone tries to use the drive.
53633444Sbostic 	 *
53733444Sbostic 	 * THIS ASSUMES THAT DRIVE TYPES ?X? ARE FLOPPIES
53833444Sbostic 	 */
53933444Sbostic 	if (MSCP_MID_ECH(1, ra_info[unit].ra_mediaid) == 'X' - '@') {
54034283Skarels 		printf(": floppy");
54133444Sbostic 		return;
54233444Sbostic 	}
54334649Skarels 	if (ui->ui_dk >= 0)
54432523Sbostic 		dk_mspw[ui->ui_dk] = 1.0 / (60 * 31 * 256);	/* approx */
54532523Sbostic 	udaip[ui->ui_ctlr][ui->ui_slave] = ui;
54633195Sbostic 
54732523Sbostic 	if (uda_rainit(ui, 0))
54834283Skarels 		printf(": offline");
54932523Sbostic 	else {
55034283Skarels 		printf(": %s, size = %d sectors",
55134283Skarels 		    udalabel[unit].d_typename, ra_info[unit].ra_dsize);
55232523Sbostic #ifdef notyet
55332523Sbostic 		addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]);
55426295Skarels #endif
55530536Skarels 	}
55632523Sbostic }
55732523Sbostic 
55832523Sbostic /*
55932523Sbostic  * Initialise a UDA50.  Return true iff something goes wrong.
56032523Sbostic  */
56132523Sbostic udainit(ctlr)
56232523Sbostic 	int ctlr;
56332523Sbostic {
56432523Sbostic 	register struct uda_softc *sc;
56532523Sbostic 	register struct udadevice *udaddr;
56632523Sbostic 	struct uba_ctlr *um;
56732523Sbostic 	int timo, ubinfo;
56832523Sbostic 
56932523Sbostic 	sc = &uda_softc[ctlr];
57032523Sbostic 	um = udaminfo[ctlr];
57132523Sbostic 	if ((sc->sc_flags & SC_MAPPED) == 0) {
57232523Sbostic 		/*
57332523Sbostic 		 * Map the communication area and command and
57432523Sbostic 		 * response packets into Unibus space.
57532523Sbostic 		 */
57632523Sbostic 		ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr],
57732523Sbostic 			sizeof (struct uda), UBA_CANTWAIT);
57832523Sbostic 		if (ubinfo == 0) {
57932523Sbostic 			printf("uda%d: uballoc map failed\n", ctlr);
58032523Sbostic 			return (-1);
58132523Sbostic 		}
58232523Sbostic 		sc->sc_uda = (struct uda *) (ubinfo & 0x3ffff);
58332523Sbostic 		sc->sc_flags |= SC_MAPPED;
58432523Sbostic 	}
58532523Sbostic 
58630536Skarels 	/*
58732523Sbostic 	 * While we are thinking about it, reset the next command
58832523Sbostic 	 * and response indicies.
58930536Skarels 	 */
59032523Sbostic 	sc->sc_mi.mi_cmd.mri_next = 0;
59132523Sbostic 	sc->sc_mi.mi_rsp.mri_next = 0;
59232523Sbostic 
59332523Sbostic 	/*
59432523Sbostic 	 * Start up the hardware initialisation sequence.
59532523Sbostic 	 */
59632523Sbostic #define	STEP0MASK	(UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \
59732523Sbostic 			 UDA_STEP1 | UDA_NV)
59832523Sbostic 
59932523Sbostic 	sc->sc_state = ST_IDLE;	/* in case init fails */
60033444Sbostic 	udaddr = (struct udadevice *)um->um_addr;
60132523Sbostic 	udaddr->udaip = 0;
60232523Sbostic 	timo = todr() + 1000;
60332523Sbostic 	while ((udaddr->udasa & STEP0MASK) == 0) {
60432523Sbostic 		if (todr() > timo) {
60532523Sbostic 			printf("uda%d: timeout during init\n", ctlr);
60632523Sbostic 			return (-1);
60732523Sbostic 		}
60832523Sbostic 	}
60932523Sbostic 	if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) {
61032523Sbostic 		printf("uda%d: init failed, sa=%b\n", ctlr,
61132523Sbostic 			udaddr->udasa, udasr_bits);
61233444Sbostic 		udasaerror(um, 0);
61332523Sbostic 		return (-1);
61432523Sbostic 	}
61532523Sbostic 
61632523Sbostic 	/*
61732523Sbostic 	 * Success!  Record new state, and start step 1 initialisation.
61832523Sbostic 	 * The rest is done in the interrupt handler.
61932523Sbostic 	 */
62032523Sbostic 	sc->sc_state = ST_STEP1;
62132523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
62232523Sbostic 	    (sc->sc_ivec >> 2);
62332523Sbostic 	return (0);
6244743Swnj }
6254743Swnj 
6264743Swnj /*
62732523Sbostic  * Open a drive.
6284743Swnj  */
62932523Sbostic /*ARGSUSED*/
63032523Sbostic udaopen(dev, flag, fmt)
6314743Swnj 	dev_t dev;
63230773Skarels 	int flag, fmt;
6334743Swnj {
63432523Sbostic 	register int unit;
6354743Swnj 	register struct uba_device *ui;
6364743Swnj 	register struct uda_softc *sc;
63730536Skarels 	register struct disklabel *lp;
63830536Skarels 	register struct partition *pp;
63930916Skarels 	register struct ra_info *ra;
64032523Sbostic 	int s, i, part, mask, error = 0;
64130536Skarels 	daddr_t start, end;
64230536Skarels 
64332523Sbostic 	/*
64432523Sbostic 	 * Make sure this is a reasonable open request.
64532523Sbostic 	 */
64632523Sbostic 	unit = udaunit(dev);
64732523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0)
6488576Sroot 		return (ENXIO);
64932523Sbostic 
65032523Sbostic 	/*
65132523Sbostic 	 * Make sure the controller is running, by (re)initialising it if
65232523Sbostic 	 * necessary.
65332523Sbostic 	 */
6544743Swnj 	sc = &uda_softc[ui->ui_ctlr];
6555434Sroot 	s = spl5();
65632523Sbostic 	if (sc->sc_state != ST_RUN) {
65732523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) {
65832523Sbostic 			splx(s);
6598576Sroot 			return (EIO);
66017553Skarels 		}
66132523Sbostic 		/*
66232523Sbostic 		 * In case it does not come up, make sure we will be
66332523Sbostic 		 * restarted in 10 seconds.  This corresponds to the
66432523Sbostic 		 * 10 second timeouts in udaprobe() and udaslave().
66532523Sbostic 		 */
66632523Sbostic 		sc->sc_flags |= SC_DOWAKE;
66732523Sbostic 		timeout(wakeup, (caddr_t) sc, 10 * hz);
66832523Sbostic 		sleep((caddr_t) sc, PRIBIO);
66932523Sbostic 		if (sc->sc_state != ST_RUN) {
67032523Sbostic 			splx(s);
67132523Sbostic 			printf("uda%d: controller hung\n", ui->ui_ctlr);
67232523Sbostic 			return (EIO);
67332523Sbostic 		}
67432523Sbostic 		untimeout(wakeup, (caddr_t) sc);
6754743Swnj 	}
67632523Sbostic 
67732523Sbostic 	/*
67832523Sbostic 	 * Wait for the state to settle
67932523Sbostic 	 */
68032523Sbostic 	ra = &ra_info[unit];
68132523Sbostic 	while (ra->ra_state != OPEN && ra->ra_state != OPENRAW &&
68232523Sbostic 	    ra->ra_state != CLOSED)
68332523Sbostic 		sleep((caddr_t)ra, PZERO + 1);
68432523Sbostic 
68532523Sbostic 	/*
68632523Sbostic 	 * If not on line, or we are not sure of the label, reinitialise
68732523Sbostic 	 * the drive.
68832523Sbostic 	 */
68932523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0 ||
69032523Sbostic 	    (ra->ra_state != OPEN && ra->ra_state != OPENRAW))
69132523Sbostic 		error = uda_rainit(ui, flag);
69230916Skarels 	splx(s);
69332523Sbostic 	if (error)
69432523Sbostic 		return (error);
69530536Skarels 
69632523Sbostic 	part = udapart(dev);
69732523Sbostic 	lp = &udalabel[unit];
69830536Skarels 	if (part >= lp->d_npartitions)
69930536Skarels 		return (ENXIO);
70030536Skarels 	/*
70132523Sbostic 	 * Warn if a partition is opened that overlaps another
70232523Sbostic 	 * already open, unless either is the `raw' partition
70332523Sbostic 	 * (whole disk).
70430536Skarels 	 */
70532523Sbostic #define	RAWPART		2	/* 'c' partition */	/* XXX */
70632523Sbostic 	mask = 1 << part;
70732523Sbostic 	if ((ra->ra_openpart & mask) == 0 && part != RAWPART) {
70830536Skarels 		pp = &lp->d_partitions[part];
70930536Skarels 		start = pp->p_offset;
71030536Skarels 		end = pp->p_offset + pp->p_size;
71132523Sbostic 		for (pp = lp->d_partitions, i = 0;
71232523Sbostic 		     i < lp->d_npartitions; pp++, i++) {
71330536Skarels 			if (pp->p_offset + pp->p_size <= start ||
71432523Sbostic 			    pp->p_offset >= end || i == RAWPART)
71530536Skarels 				continue;
71632523Sbostic 			if (ra->ra_openpart & (1 << i))
71730536Skarels 				log(LOG_WARNING,
71830536Skarels 				    "ra%d%c: overlaps open partition (%c)\n",
71932523Sbostic 				    unit, part + 'a', i + 'a');
72017553Skarels 		}
72117553Skarels 	}
72230773Skarels 	switch (fmt) {
72330773Skarels 	case S_IFCHR:
72432523Sbostic 		ra->ra_copenpart |= mask;
72530773Skarels 		break;
72630773Skarels 	case S_IFBLK:
72732523Sbostic 		ra->ra_bopenpart |= mask;
72830773Skarels 		break;
72930773Skarels 	}
73032523Sbostic 	ra->ra_openpart |= mask;
7318576Sroot 	return (0);
7324743Swnj }
7334743Swnj 
73433444Sbostic /* ARGSUSED */
73532523Sbostic udaclose(dev, flags, fmt)
73630536Skarels 	dev_t dev;
73730773Skarels 	int flags, fmt;
73830536Skarels {
73932523Sbostic 	register int unit = udaunit(dev);
74030773Skarels 	register struct ra_info *ra = &ra_info[unit];
74132523Sbostic 	int s, mask = (1 << udapart(dev));
74230536Skarels 
74330773Skarels 	switch (fmt) {
74430773Skarels 	case S_IFCHR:
74532523Sbostic 		ra->ra_copenpart &= ~mask;
74630773Skarels 		break;
74730773Skarels 	case S_IFBLK:
74832523Sbostic 		ra->ra_bopenpart &= ~mask;
74930773Skarels 		break;
75030773Skarels 	}
75132523Sbostic 	ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
75232523Sbostic 
75330536Skarels 	/*
75432523Sbostic 	 * Should wait for I/O to complete on this partition even if
75532523Sbostic 	 * others are open, but wait for work on blkflush().
75630536Skarels 	 */
75732523Sbostic 	if (ra->ra_openpart == 0) {
75830536Skarels 		s = spl5();
75932523Sbostic 		while (udautab[unit].b_actf)
76032523Sbostic 			sleep((caddr_t)&udautab[unit], PZERO - 1);
76130536Skarels 		splx(s);
76232523Sbostic 		ra->ra_state = CLOSED;
76333443Skarels 		ra->ra_wlabel = 0;
76430536Skarels 	}
76530773Skarels 	return (0);
76630536Skarels }
76730536Skarels 
7684743Swnj /*
76932523Sbostic  * Initialise a drive.  If it is not already, bring it on line,
77032523Sbostic  * and set a timeout on it in case it fails to respond.
77132523Sbostic  * When on line, read in the pack label.
7724743Swnj  */
77332523Sbostic uda_rainit(ui, flags)
77430536Skarels 	register struct uba_device *ui;
77532523Sbostic 	int flags;
77630536Skarels {
77732523Sbostic 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
77834283Skarels 	register struct disklabel *lp;
77930536Skarels 	register struct mscp *mp;
78032523Sbostic 	register int unit = ui->ui_unit;
78132523Sbostic 	register struct ra_info *ra;
78230773Skarels 	char *msg, *readdisklabel();
78332523Sbostic 	int s, i, udastrategy();
78430536Skarels 	extern int cold;
78530536Skarels 
78632523Sbostic 	ra = &ra_info[unit];
78732523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
78832523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT);
78932523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
79032523Sbostic 		mp->mscp_unit = ui->ui_slave;
79132523Sbostic 		mp->mscp_cmdref = (long)&ui->ui_flags;
79232523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
79332523Sbostic 		ra->ra_state = WANTOPEN;
79432523Sbostic 		if (!cold)
79532523Sbostic 			s = spl5();
79632523Sbostic 		i = ((struct udadevice *)ui->ui_addr)->udaip;
79730536Skarels 
79830916Skarels 		if (cold) {
79932523Sbostic 			i = todr() + 1000;
80032523Sbostic 			while ((ui->ui_flags & UNIT_ONLINE) == 0)
80132523Sbostic 				if (todr() > i)
80232523Sbostic 					break;
80330916Skarels 		} else {
80432523Sbostic 			timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
80532523Sbostic 			sleep((caddr_t)&ui->ui_flags, PSWP + 1);
80632523Sbostic 			splx(s);
80732523Sbostic 			untimeout(wakeup, (caddr_t)&ui->ui_flags);
80830916Skarels 		}
80932523Sbostic 		if (ra->ra_state != OPENRAW) {
81032523Sbostic 			ra->ra_state = CLOSED;
81132523Sbostic 			wakeup((caddr_t)ra);
81230916Skarels 			return (EIO);
81330916Skarels 		}
81430536Skarels 	}
81530536Skarels 
81632523Sbostic 	lp = &udalabel[unit];
81730916Skarels 	lp->d_secsize = DEV_BSIZE;
81832523Sbostic 	lp->d_secperunit = ra->ra_dsize;
81930916Skarels 
82030536Skarels 	if (flags & O_NDELAY)
82130536Skarels 		return (0);
82232523Sbostic 	ra->ra_state = RDLABEL;
82330536Skarels 	/*
82432523Sbostic 	 * Set up default sizes until we have the label, or longer
82532523Sbostic 	 * if there is none.  Set secpercyl, as readdisklabel wants
82632523Sbostic 	 * to compute b_cylin (although we do not need it).
82730536Skarels 	 */
82830916Skarels 	lp->d_secpercyl = 1;
82930536Skarels 	lp->d_npartitions = 1;
83030536Skarels 	lp->d_partitions[0].p_size = lp->d_secperunit;
83130536Skarels 	lp->d_partitions[0].p_offset = 0;
83232523Sbostic 
83330536Skarels 	/*
83430536Skarels 	 * Read pack label.
83530536Skarels 	 */
83632523Sbostic 	if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) {
83734283Skarels 		if (cold)
83834283Skarels 			printf(": %s", msg);
83934283Skarels 		else
84034283Skarels 			log(LOG_ERR, "ra%d: %s\n", unit, msg);
84130536Skarels #ifdef COMPAT_42
84232523Sbostic 		if (udamaptype(unit, lp))
84332523Sbostic 			ra->ra_state = OPEN;
84430536Skarels 		else
84532523Sbostic 			ra->ra_state = OPENRAW;
84631022Skarels #else
84732523Sbostic 		ra->ra_state = OPENRAW;
84832523Sbostic 		/* uda_makefakelabel(ra, lp); */
84930536Skarels #endif
85030773Skarels 	} else
85132523Sbostic 		ra->ra_state = OPEN;
85230916Skarels 	wakeup((caddr_t)ra);
85330916Skarels 	return (0);
85430536Skarels }
85530536Skarels 
85632523Sbostic /*
85732523Sbostic  * Copy the geometry information for the given ra from a
85832523Sbostic  * GET UNIT STATUS response.  If check, see if it changed.
85932523Sbostic  */
86032523Sbostic uda_rasave(unit, mp, check)
86132523Sbostic 	int unit;
86232523Sbostic 	register struct mscp *mp;
86332523Sbostic 	int check;
86432523Sbostic {
86532523Sbostic 	register struct ra_info *ra = &ra_info[unit];
86632523Sbostic 
86734283Skarels 	if (check && ra->ra_mediaid != mp->mscp_guse.guse_mediaid) {
86833443Skarels 		printf("ra%d: changed types! was %d now %d\n", unit,
86934283Skarels 			ra->ra_mediaid, mp->mscp_guse.guse_mediaid);
87032523Sbostic 		ra->ra_state = CLOSED;	/* ??? */
87132523Sbostic 	}
87233444Sbostic 	/* ra->ra_type = mp->mscp_guse.guse_drivetype; */
87332523Sbostic 	ra->ra_mediaid = mp->mscp_guse.guse_mediaid;
87432523Sbostic 	ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt;
87532523Sbostic 	ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group;
87632523Sbostic 	ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc;
87732523Sbostic 	ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc;
87832523Sbostic 	/* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */
87932523Sbostic #ifdef notyet
88032523Sbostic 	ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize;
88132523Sbostic 	ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt;
88232523Sbostic 	ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct;
88332523Sbostic #endif
88432523Sbostic }
88532523Sbostic 
88632523Sbostic /*
88732523Sbostic  * Queue a transfer request, and if possible, hand it to the controller.
88832523Sbostic  *
88932523Sbostic  * This routine is broken into two so that the internal version
89032523Sbostic  * udastrat1() can be called by the (nonexistent, as yet) bad block
89132523Sbostic  * revectoring routine.
89232523Sbostic  */
89332523Sbostic udastrategy(bp)
8944743Swnj 	register struct buf *bp;
8954743Swnj {
89632523Sbostic 	register int unit;
8974743Swnj 	register struct uba_device *ui;
89832523Sbostic 	register struct ra_info *ra;
89932523Sbostic 	struct partition *pp;
90032523Sbostic 	int p;
9014743Swnj 	daddr_t sz, maxsz;
9024743Swnj 
90332523Sbostic 	/*
90432523Sbostic 	 * Make sure this is a reasonable drive to use.
90532523Sbostic 	 */
90632523Sbostic 	if ((unit = udaunit(bp->b_dev)) >= NRA ||
90732523Sbostic 	    (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 ||
90832523Sbostic 	    (ra = &ra_info[unit])->ra_state == CLOSED) {
90924742Sbloom 		bp->b_error = ENXIO;
9104743Swnj 		goto bad;
91124742Sbloom 	}
91232523Sbostic 
91332523Sbostic 	/*
91432523Sbostic 	 * If drive is open `raw' or reading label, let it at it.
91532523Sbostic 	 */
91632523Sbostic 	if (ra->ra_state < OPEN) {
91732523Sbostic 		udastrat1(bp);
91832523Sbostic 		return;
91924742Sbloom 	}
92032523Sbostic 	p = udapart(bp->b_dev);
92133443Skarels 	if ((ra->ra_openpart & (1 << p)) == 0) {
92233443Skarels 		bp->b_error = ENODEV;
92333443Skarels 		goto bad;
92433443Skarels 	}
92532523Sbostic 
92632523Sbostic 	/*
92732523Sbostic 	 * Determine the size of the transfer, and make sure it is
92832523Sbostic 	 * within the boundaries of the partition.
92932523Sbostic 	 */
93032523Sbostic 	pp = &udalabel[unit].d_partitions[p];
93132523Sbostic 	maxsz = pp->p_size;
93232523Sbostic 	if (pp->p_offset + pp->p_size > ra->ra_dsize)
93332523Sbostic 		maxsz = ra->ra_dsize - pp->p_offset;
93430536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
93533443Skarels 	if (bp->b_blkno + pp->p_offset <= LABELSECTOR &&
93633443Skarels #if LABELSECTOR != 0
93733443Skarels 	    bp->b_blkno + pp->p_offset + sz > LABELSECTOR &&
93833443Skarels #endif
93933443Skarels 	    (bp->b_flags & B_READ) == 0 && ra->ra_wlabel == 0) {
94033443Skarels 		bp->b_error = EROFS;
94133443Skarels 		goto bad;
94233443Skarels 	}
94330536Skarels 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
94432523Sbostic 		/* if exactly at end of disk, return an EOF */
94524787Skarels 		if (bp->b_blkno == maxsz) {
94624787Skarels 			bp->b_resid = bp->b_bcount;
94732523Sbostic 			biodone(bp);
94832523Sbostic 			return;
94924787Skarels 		}
95032523Sbostic 		/* or truncate if part of it fits */
95130536Skarels 		sz = maxsz - bp->b_blkno;
95230536Skarels 		if (sz <= 0) {
95332523Sbostic 			bp->b_error = EINVAL;	/* or hang it up */
95430536Skarels 			goto bad;
95530536Skarels 		}
95630536Skarels 		bp->b_bcount = sz << DEV_BSHIFT;
95724742Sbloom 	}
95832523Sbostic 	udastrat1(bp);
95932523Sbostic 	return;
96032523Sbostic bad:
96132523Sbostic 	bp->b_flags |= B_ERROR;
96232523Sbostic 	biodone(bp);
96332523Sbostic }
96432523Sbostic 
96532523Sbostic /*
96632523Sbostic  * Work routine for udastrategy.
96732523Sbostic  */
96832523Sbostic udastrat1(bp)
96932523Sbostic 	register struct buf *bp;
97032523Sbostic {
97132523Sbostic 	register int unit = udaunit(bp->b_dev);
97232523Sbostic 	register struct uba_ctlr *um;
97332523Sbostic 	register struct buf *dp;
97432523Sbostic 	struct uba_device *ui;
97532523Sbostic 	int s = spl5();
97632523Sbostic 
9774743Swnj 	/*
97832523Sbostic 	 * Append the buffer to the drive queue, and if it is not
97932523Sbostic 	 * already there, the drive to the controller queue.  (However,
98032523Sbostic 	 * if the drive queue is marked to be requeued, we must be
98132523Sbostic 	 * awaiting an on line or get unit status command; in this
98232523Sbostic 	 * case, leave it off the controller queue.)
9834743Swnj 	 */
98432523Sbostic 	um = (ui = udadinfo[unit])->ui_mi;
98532523Sbostic 	dp = &udautab[unit];
98632523Sbostic 	APPEND(bp, dp, av_forw);
98732523Sbostic 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
98832523Sbostic 		APPEND(dp, &um->um_tab, b_forw);
98932523Sbostic 		dp->b_active++;
99032523Sbostic 	}
99132523Sbostic 
9924743Swnj 	/*
99332523Sbostic 	 * Start activity on the controller.  Note that unlike other
99432523Sbostic 	 * Unibus drivers, we must always do this, not just when the
99532523Sbostic 	 * controller is not active.
9964743Swnj 	 */
99732523Sbostic 	udastart(um);
9985434Sroot 	splx(s);
9994743Swnj }
10004743Swnj 
100132523Sbostic /*
100232523Sbostic  * Start up whatever transfers we can find.
100332523Sbostic  * Note that udastart() must be called at spl5().
100432523Sbostic  */
100532523Sbostic udastart(um)
10064743Swnj 	register struct uba_ctlr *um;
10074743Swnj {
100832523Sbostic 	register struct uda_softc *sc = &uda_softc[um->um_ctlr];
10094743Swnj 	register struct buf *bp, *dp;
10104743Swnj 	register struct mscp *mp;
101132523Sbostic 	struct uba_device *ui;
10124743Swnj 	struct udadevice *udaddr;
101332523Sbostic 	struct partition *pp;
101432523Sbostic 	int i, sz;
10154743Swnj 
101632523Sbostic #ifdef lint
101732523Sbostic 	i = 0; i = i;
101832523Sbostic #endif
101932523Sbostic 	/*
102032523Sbostic 	 * If it is not running, try (again and again...) to initialise
102132523Sbostic 	 * it.  If it is currently initialising just ignore it for now.
102232523Sbostic 	 */
102332523Sbostic 	if (sc->sc_state != ST_RUN) {
102432523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr))
102532523Sbostic 			printf("uda%d: still hung\n", um->um_ctlr);
102632523Sbostic 		return;
102732523Sbostic 	}
102832523Sbostic 
102932523Sbostic 	/*
103032523Sbostic 	 * If um_cmd is nonzero, this controller is on the Unibus
103132523Sbostic 	 * resource wait queue.  It will not help to try more requests;
103232523Sbostic 	 * instead, when the Unibus unblocks and calls udadgo(), we
103332523Sbostic 	 * will call udastart() again.
103432523Sbostic 	 */
103532523Sbostic 	if (um->um_cmd)
103632523Sbostic 		return;
103732523Sbostic 
103832523Sbostic 	sc->sc_flags |= SC_INSTART;
103932523Sbostic 	udaddr = (struct udadevice *) um->um_addr;
104032523Sbostic 
10414743Swnj loop:
104232523Sbostic 	/*
104332523Sbostic 	 * Service the drive at the head of the queue.  It may not
104432523Sbostic 	 * need anything, in which case it might be shutting down
104532523Sbostic 	 * in udaclose().
104632523Sbostic 	 */
104732523Sbostic 	if ((dp = um->um_tab.b_actf) == NULL)
104832523Sbostic 		goto out;
10494743Swnj 	if ((bp = dp->b_actf) == NULL) {
10504743Swnj 		dp->b_active = 0;
10514743Swnj 		um->um_tab.b_actf = dp->b_forw;
105232523Sbostic 		if (ra_info[dp - udautab].ra_openpart == 0)
105332523Sbostic 			wakeup((caddr_t)dp); /* finish close protocol */
105432523Sbostic 		goto loop;
10554743Swnj 	}
105632523Sbostic 
105732523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
105833444Sbostic 		udasaerror(um, 1);
105932523Sbostic 		goto out;
10604743Swnj 	}
106132523Sbostic 
106232523Sbostic 	/*
106332523Sbostic 	 * Get an MSCP packet, then figure out what to do.  If
106432523Sbostic 	 * we cannot get a command packet, the command ring may
106532523Sbostic 	 * be too small:  We should have at least as many command
106632523Sbostic 	 * packets as credits, for best performance.
106732523Sbostic 	 */
106832523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) {
106932523Sbostic 		if (sc->sc_mi.mi_credits > MSCP_MINCREDITS &&
107032523Sbostic 		    (sc->sc_flags & SC_GRIPED) == 0) {
107132523Sbostic 			log(LOG_NOTICE, "uda%d: command ring too small\n",
107232523Sbostic 				um->um_ctlr);
107332523Sbostic 			sc->sc_flags |= SC_GRIPED;/* complain only once */
107417553Skarels 		}
107532523Sbostic 		goto out;
10764743Swnj 	}
10774743Swnj 
107832523Sbostic 	/*
107932523Sbostic 	 * Bring the drive on line if it is not already.  Get its status
108032523Sbostic 	 * if we do not already have it.  Otherwise just start the transfer.
108132523Sbostic 	 */
108232523Sbostic 	ui = udadinfo[udaunit(bp->b_dev)];
108332523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
108432523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
108532523Sbostic 		goto common;
10864743Swnj 	}
108732523Sbostic 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
108832523Sbostic 		mp->mscp_opcode = M_OP_GETUNITST;
108932523Sbostic common:
109032523Sbostic if (ui->ui_flags & UNIT_REQUEUE) panic("udastart");
109132523Sbostic 		/*
109232523Sbostic 		 * Take the drive off the controller queue.  When the
109332523Sbostic 		 * command finishes, make sure the drive is requeued.
109432523Sbostic 		 */
109532523Sbostic 		um->um_tab.b_actf = dp->b_forw;
109632523Sbostic 		dp->b_active = 0;
109732523Sbostic 		ui->ui_flags |= UNIT_REQUEUE;
109832523Sbostic 		mp->mscp_unit = ui->ui_slave;
109932523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
110032523Sbostic 		sc->sc_flags |= SC_STARTPOLL;
110132523Sbostic #ifdef POLLSTATS
110232523Sbostic 		sc->sc_ncmd++;
110325653Skarels #endif
110432523Sbostic 		goto loop;
110517553Skarels 	}
110632523Sbostic 
110732523Sbostic 	pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)];
110832523Sbostic 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
11094743Swnj 	mp->mscp_unit = ui->ui_slave;
111032523Sbostic 	mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset;
111130536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
111232523Sbostic 	mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ?
111332523Sbostic 		(pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount;
111432523Sbostic 	/* mscp_cmdref is filled in by mscp_go() */
11154743Swnj 
11164743Swnj 	/*
111732523Sbostic 	 * Drop the packet pointer into the `command' field so udadgo()
111832523Sbostic 	 * can tell what to start.  If ubago returns 1, we can do another
111932523Sbostic 	 * transfer.  If not, um_cmd will still point at mp, so we will
112032523Sbostic 	 * know that we are waiting for resources.
11214743Swnj 	 */
112232523Sbostic 	um->um_cmd = (int)mp;
112332523Sbostic 	if (ubago(ui))
112432523Sbostic 		goto loop;
112532523Sbostic 
112632523Sbostic 	/*
112732523Sbostic 	 * All done, or blocked in ubago().  If we managed to
112832523Sbostic 	 * issue some commands, start up the beast.
112932523Sbostic 	 */
113032523Sbostic out:
113132523Sbostic 	if (sc->sc_flags & SC_STARTPOLL) {
113232523Sbostic #ifdef POLLSTATS
113332523Sbostic 		udastats.cmd[sc->sc_ncmd]++;
113432523Sbostic 		sc->sc_ncmd = 0;
113532523Sbostic #endif
113633444Sbostic 		i = ((struct udadevice *)um->um_addr)->udaip;
11374743Swnj 	}
113832523Sbostic 	sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL);
113932523Sbostic }
114032523Sbostic 
114132523Sbostic /*
114232523Sbostic  * Start a transfer.
114332523Sbostic  *
114432523Sbostic  * If we are not called from within udastart(), we must have been
114532523Sbostic  * blocked, so call udastart to do more requests (if any).  If
114632523Sbostic  * this calls us again immediately we will not recurse, because
114732523Sbostic  * that time we will be in udastart().  Clever....
114832523Sbostic  */
114932523Sbostic udadgo(um)
115032523Sbostic 	register struct uba_ctlr *um;
115132523Sbostic {
115232523Sbostic 	struct uda_softc *sc = &uda_softc[um->um_ctlr];
115332523Sbostic 	struct mscp *mp = (struct mscp *)um->um_cmd;
115432523Sbostic 
115532523Sbostic 	um->um_tab.b_active++;	/* another transfer going */
115632523Sbostic 
11574743Swnj 	/*
115832523Sbostic 	 * Fill in the MSCP packet and move the buffer to the
115932523Sbostic 	 * I/O wait queue.  Mark the controller as no longer on
116032523Sbostic 	 * the resource queue, and remember to initiate polling.
11614743Swnj 	 */
116232523Sbostic 	mp->mscp_seq.seq_buffer = (um->um_ubinfo & 0x3ffff) |
116332523Sbostic 		(UBAI_BDP(um->um_ubinfo) << 24);
116432523Sbostic 	mscp_go(&sc->sc_mi, mp, um->um_ubinfo);
116532523Sbostic 	um->um_cmd = 0;
116632523Sbostic 	um->um_ubinfo = 0;	/* tyke it awye */
116732523Sbostic 	sc->sc_flags |= SC_STARTPOLL;
116832523Sbostic #ifdef POLLSTATS
116932523Sbostic 	sc->sc_ncmd++;
117032523Sbostic #endif
117132523Sbostic 	if ((sc->sc_flags & SC_INSTART) == 0)
117232523Sbostic 		udastart(um);
11734743Swnj }
11744743Swnj 
117532523Sbostic udaiodone(mi, bp, info)
117632523Sbostic 	register struct mscp_info *mi;
117732523Sbostic 	struct buf *bp;
117832523Sbostic 	int info;
117932523Sbostic {
118032523Sbostic 	register struct uba_ctlr *um = udaminfo[mi->mi_ctlr];
118132523Sbostic 
118232523Sbostic 	um->um_ubinfo = info;
118332523Sbostic 	ubadone(um);
118432523Sbostic 	biodone(bp);
118532523Sbostic 	if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab)
118632523Sbostic 		ubarelse(um->um_ubanum, &um->um_bdp);
118732523Sbostic 	um->um_tab.b_active--;	/* another transfer done */
118832523Sbostic }
118932523Sbostic 
119033444Sbostic static struct saerr {
119133444Sbostic 	int	code;		/* error code (including UDA_ERR) */
119233444Sbostic 	char	*desc;		/* what it means: Efoo => foo error */
119333444Sbostic } saerr[] = {
119433444Sbostic 	{ 0100001, "Eunibus packet read" },
119533444Sbostic 	{ 0100002, "Eunibus packet write" },
119633444Sbostic 	{ 0100003, "EUDA ROM and RAM parity" },
119733444Sbostic 	{ 0100004, "EUDA RAM parity" },
119833444Sbostic 	{ 0100005, "EUDA ROM parity" },
119933444Sbostic 	{ 0100006, "Eunibus ring read" },
120033444Sbostic 	{ 0100007, "Eunibus ring write" },
120133444Sbostic 	{ 0100010, " unibus interrupt master failure" },
120233444Sbostic 	{ 0100011, "Ehost access timeout" },
120333444Sbostic 	{ 0100012, " host exceeded command limit" },
120433444Sbostic 	{ 0100013, " unibus bus master failure" },
120533444Sbostic 	{ 0100014, " DM XFC fatal error" },
120633444Sbostic 	{ 0100015, " hardware timeout of instruction loop" },
120733444Sbostic 	{ 0100016, " invalid virtual circuit id" },
120833444Sbostic 	{ 0100017, "Eunibus interrupt write" },
120933444Sbostic 	{ 0104000, "Efatal sequence" },
121033444Sbostic 	{ 0104040, " D proc ALU" },
121133444Sbostic 	{ 0104041, "ED proc control ROM parity" },
121233444Sbostic 	{ 0105102, "ED proc w/no BD#2 or RAM parity" },
121333444Sbostic 	{ 0105105, "ED proc RAM buffer" },
121433444Sbostic 	{ 0105152, "ED proc SDI" },
121533444Sbostic 	{ 0105153, "ED proc write mode wrap serdes" },
121633444Sbostic 	{ 0105154, "ED proc read mode serdes, RSGEN & ECC" },
121733444Sbostic 	{ 0106040, "EU proc ALU" },
121833444Sbostic 	{ 0106041, "EU proc control reg" },
121933444Sbostic 	{ 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" },
122033444Sbostic 	{ 0106047, " U proc const PROM err w/D proc running SDI test" },
122133444Sbostic 	{ 0106055, " unexpected trap" },
122233444Sbostic 	{ 0106071, "EU proc const PROM" },
122333444Sbostic 	{ 0106072, "EU proc control ROM parity" },
122433444Sbostic 	{ 0106200, "Estep 1 data" },
122533444Sbostic 	{ 0107103, "EU proc RAM parity" },
122633444Sbostic 	{ 0107107, "EU proc RAM buffer" },
122733444Sbostic 	{ 0107115, " test count wrong (BD 12)" },
122833444Sbostic 	{ 0112300, "Estep 2" },
122933444Sbostic 	{ 0122240, "ENPR" },
123033444Sbostic 	{ 0122300, "Estep 3" },
123133444Sbostic 	{ 0142300, "Estep 4" },
123233444Sbostic 	{ 0, " unknown error code" }
123333444Sbostic };
123433444Sbostic 
12354743Swnj /*
123633444Sbostic  * If the error bit was set in the controller status register, gripe,
123733444Sbostic  * then (optionally) reset the controller and requeue pending transfers.
12384743Swnj  */
123933444Sbostic udasaerror(um, doreset)
124032523Sbostic 	register struct uba_ctlr *um;
124133444Sbostic 	int doreset;
12424743Swnj {
124333444Sbostic 	register int code = ((struct udadevice *)um->um_addr)->udasa;
124433444Sbostic 	register struct saerr *e;
124532523Sbostic 
124633444Sbostic 	if ((code & UDA_ERR) == 0)
124733444Sbostic 		return;
124833444Sbostic 	for (e = saerr; e->code; e++)
124933444Sbostic 		if (e->code == code)
125033444Sbostic 			break;
125133444Sbostic 	printf("uda%d: controller error, sa=0%o (%s%s)\n",
125233444Sbostic 		um->um_ctlr, code, e->desc + 1,
125333444Sbostic 		*e->desc == 'E' ? " error" : "");
125433444Sbostic 	if (doreset) {
125533444Sbostic 		mscp_requeue(&uda_softc[um->um_ctlr].sc_mi);
125633444Sbostic 		(void) udainit(um->um_ctlr);
125733444Sbostic 	}
125832523Sbostic }
125932523Sbostic 
126032523Sbostic /*
126132523Sbostic  * Interrupt routine.  Depending on the state of the controller,
126232523Sbostic  * continue initialisation, or acknowledge command and response
126332523Sbostic  * interrupts, and process responses.
126432523Sbostic  */
126532523Sbostic udaintr(ctlr)
126632523Sbostic 	int ctlr;
126732523Sbostic {
126832523Sbostic 	register struct uba_ctlr *um = udaminfo[ctlr];
126932523Sbostic 	register struct uda_softc *sc = &uda_softc[ctlr];
127033444Sbostic 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
127132523Sbostic 	register struct uda *ud;
127232523Sbostic 	register struct mscp *mp;
12734743Swnj 	register int i;
12744743Swnj 
127527254Skridle #ifdef VAX630
127632523Sbostic 	(void) spl5();		/* Qbus interrupt protocol is odd */
127727254Skridle #endif
127832523Sbostic 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
127932523Sbostic 
128032523Sbostic 	/*
128132523Sbostic 	 * Combinations during steps 1, 2, and 3: STEPnMASK
128232523Sbostic 	 * corresponds to which bits should be tested;
128332523Sbostic 	 * STEPnGOOD corresponds to the pattern that should
128432523Sbostic 	 * appear after the interrupt from STEPn initialisation.
128532523Sbostic 	 * All steps test the bits in ALLSTEPS.
128632523Sbostic 	 */
128732523Sbostic #define	ALLSTEPS	(UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1)
128832523Sbostic 
128932523Sbostic #define	STEP1MASK	(ALLSTEPS | UDA_IE | UDA_NCNRMASK)
129032523Sbostic #define	STEP1GOOD	(UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2)
129132523Sbostic 
129232523Sbostic #define	STEP2MASK	(ALLSTEPS | UDA_IE | UDA_IVECMASK)
129332523Sbostic #define	STEP2GOOD	(UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2))
129432523Sbostic 
129532523Sbostic #define	STEP3MASK	ALLSTEPS
129632523Sbostic #define	STEP3GOOD	UDA_STEP4
129732523Sbostic 
12984743Swnj 	switch (sc->sc_state) {
129932523Sbostic 
130032523Sbostic 	case ST_IDLE:
130132523Sbostic 		/*
130232523Sbostic 		 * Ignore unsolicited interrupts.
130332523Sbostic 		 */
130432523Sbostic 		log(LOG_WARNING, "uda%d: stray intr\n", ctlr);
13054743Swnj 		return;
13064743Swnj 
130732523Sbostic 	case ST_STEP1:
130832523Sbostic 		/*
130932523Sbostic 		 * Begin step two initialisation.
131032523Sbostic 		 */
131132523Sbostic 		if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) {
131232523Sbostic 			i = 1;
131332523Sbostic initfailed:
131432523Sbostic 			printf("uda%d: init step %d failed, sa=%b\n",
131532523Sbostic 				ctlr, i, udaddr->udasa, udasr_bits);
131633444Sbostic 			udasaerror(um, 0);
131732523Sbostic 			sc->sc_state = ST_IDLE;
131832523Sbostic 			if (sc->sc_flags & SC_DOWAKE) {
131932523Sbostic 				sc->sc_flags &= ~SC_DOWAKE;
132033444Sbostic 				wakeup((caddr_t)sc);
132132523Sbostic 			}
13224743Swnj 			return;
13234743Swnj 		}
132433444Sbostic 		udaddr->udasa = (int)&sc->sc_uda->uda_ca.ca_rspdsc[0] |
132532523Sbostic 			(cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0);
132632523Sbostic 		sc->sc_state = ST_STEP2;
13274743Swnj 		return;
13284743Swnj 
132932523Sbostic 	case ST_STEP2:
133032523Sbostic 		/*
133132523Sbostic 		 * Begin step 3 initialisation.
133232523Sbostic 		 */
133332523Sbostic 		if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) {
133432523Sbostic 			i = 2;
133532523Sbostic 			goto initfailed;
13364743Swnj 		}
133733444Sbostic 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16;
133832523Sbostic 		sc->sc_state = ST_STEP3;
13394743Swnj 		return;
13404743Swnj 
134132523Sbostic 	case ST_STEP3:
134232523Sbostic 		/*
134332523Sbostic 		 * Set controller characteristics (finish initialisation).
134432523Sbostic 		 */
134532523Sbostic 		if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) {
134632523Sbostic 			i = 3;
134732523Sbostic 			goto initfailed;
13484743Swnj 		}
134932523Sbostic 		i = udaddr->udasa & 0xff;
135032523Sbostic 		if (i != sc->sc_micro) {
135132523Sbostic 			sc->sc_micro = i;
135232523Sbostic 			printf("uda%d: version %d model %d\n",
135332523Sbostic 				ctlr, i & 0xf, i >> 4);
135432523Sbostic 		}
135532523Sbostic 
135617553Skarels 		/*
135732523Sbostic 		 * Present the burst size, then remove it.  Why this
135832523Sbostic 		 * should be done this way, I have no idea.
135932523Sbostic 		 *
136032523Sbostic 		 * Note that this assumes udaburst[ctlr] > 0.
136117553Skarels 		 */
136232523Sbostic 		udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2;
13634743Swnj 		udaddr->udasa = UDA_GO;
136432523Sbostic 		printf("uda%d: DMA burst size set to %d\n",
136532523Sbostic 			ctlr, udaburst[ctlr]);
13664743Swnj 
136732523Sbostic 		udainitds(ctlr);	/* initialise data structures */
136832523Sbostic 
13694743Swnj 		/*
137032523Sbostic 		 * Before we can get a command packet, we need some
137132523Sbostic 		 * credits.  Fake some up to keep mscp_getcp() happy,
137232523Sbostic 		 * get a packet, and cancel all credits (the right
137332523Sbostic 		 * number should come back in the response to the
137432523Sbostic 		 * SCC packet).
13754743Swnj 		 */
137632523Sbostic 		sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1;
137732523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT);
137832523Sbostic 		if (mp == NULL)	/* `cannot happen' */
137932523Sbostic 			panic("udaintr");
138032523Sbostic 		sc->sc_mi.mi_credits = 0;
138132523Sbostic 		mp->mscp_opcode = M_OP_SETCTLRC;
138232523Sbostic 		mp->mscp_unit = 0;
138332523Sbostic 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
138432523Sbostic 			M_CF_THIS;
138532523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
138632523Sbostic 		i = udaddr->udaip;
138732523Sbostic 		sc->sc_state = ST_SETCHAR;
13884743Swnj 		return;
13894743Swnj 
139032523Sbostic 	case ST_SETCHAR:
139132523Sbostic 	case ST_RUN:
139232523Sbostic 		/*
139332523Sbostic 		 * Handle Set Ctlr Characteristics responses and operational
139432523Sbostic 		 * responses (via mscp_dorsp).
139532523Sbostic 		 */
13964743Swnj 		break;
13974743Swnj 
13984743Swnj 	default:
139932523Sbostic 		printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state);
140032523Sbostic 		panic("udastate");
14014743Swnj 	}
14024743Swnj 
140332523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
140433444Sbostic 		udasaerror(um, 1);
140532523Sbostic 		return;
14064743Swnj 	}
14074743Swnj 
140832523Sbostic 	ud = &uda[ctlr];
140932523Sbostic 
14104743Swnj 	/*
141132523Sbostic 	 * Handle buffer purge requests.
14124743Swnj 	 */
14134743Swnj 	if (ud->uda_ca.ca_bdp) {
141426372Skarels 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
14154743Swnj 		ud->uda_ca.ca_bdp = 0;
141632523Sbostic 		udaddr->udasa = 0;	/* signal purge complete */
14174743Swnj 	}
14184743Swnj 
14194743Swnj 	/*
142032523Sbostic 	 * Check for response and command ring transitions.
14214743Swnj 	 */
14224743Swnj 	if (ud->uda_ca.ca_rspint) {
14234743Swnj 		ud->uda_ca.ca_rspint = 0;
142432523Sbostic 		mscp_dorsp(&sc->sc_mi);
14254743Swnj 	}
14264743Swnj 	if (ud->uda_ca.ca_cmdint) {
14274743Swnj 		ud->uda_ca.ca_cmdint = 0;
142832523Sbostic 		MSCP_DOCMD(&sc->sc_mi);
14294743Swnj 	}
143032523Sbostic 	udastart(um);
14314743Swnj }
14324743Swnj 
14334743Swnj /*
143432523Sbostic  * Initialise the various data structures that control the UDA50.
143517553Skarels  */
143632523Sbostic udainitds(ctlr)
143732523Sbostic 	int ctlr;
143832523Sbostic {
143932523Sbostic 	register struct uda *ud = &uda[ctlr];
144032523Sbostic 	register struct uda *uud = uda_softc[ctlr].sc_uda;
144132523Sbostic 	register struct mscp *mp;
144232523Sbostic 	register int i;
14434743Swnj 
144432523Sbostic 	for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) {
144532523Sbostic 		ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
144632523Sbostic 			(long)&uud->uda_rsp[i].mscp_cmdref;
144732523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i];
144832523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
14494743Swnj 	}
145032523Sbostic 	for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) {
145132523Sbostic 		ud->uda_ca.ca_cmddsc[i] = MSCP_INT |
145232523Sbostic 			(long)&uud->uda_cmd[i].mscp_cmdref;
145332523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i];
145432523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
145532523Sbostic 	}
14564743Swnj }
14574743Swnj 
14584743Swnj /*
145933444Sbostic  * Handle an error datagram.
14604743Swnj  */
146132523Sbostic udadgram(mi, mp)
146232523Sbostic 	struct mscp_info *mi;
146332523Sbostic 	struct mscp *mp;
14644743Swnj {
146517553Skarels 
146632523Sbostic 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
146733444Sbostic 	/*
146833444Sbostic 	 * SDI status information bytes 10 and 11 are the microprocessor
146933444Sbostic 	 * error code and front panel code respectively.  These vary per
147033444Sbostic 	 * drive type and are printed purely for field service information.
147133444Sbostic 	 */
147233444Sbostic 	if (mp->mscp_format == M_FM_SDI)
147333444Sbostic 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
147433444Sbostic 			mp->mscp_erd.erd_sdistat[10],
147533444Sbostic 			mp->mscp_erd.erd_sdistat[11]);
147632523Sbostic }
147717553Skarels 
147832523Sbostic /*
147932523Sbostic  * The Set Controller Characteristics command finished.
148032523Sbostic  * Record the new state of the controller.
148132523Sbostic  */
148232523Sbostic udactlrdone(mi, mp)
148332523Sbostic 	register struct mscp_info *mi;
148432523Sbostic 	struct mscp *mp;
148532523Sbostic {
148632523Sbostic 	register struct uda_softc *sc = &uda_softc[mi->mi_ctlr];
148717553Skarels 
148832523Sbostic 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
148932523Sbostic 		sc->sc_state = ST_RUN;
149032523Sbostic 	else {
149132523Sbostic 		printf("uda%d: SETCTLRC failed: ",
149232523Sbostic 			mi->mi_ctlr, mp->mscp_status);
149332523Sbostic 		mscp_printevent(mp);
149432523Sbostic 		sc->sc_state = ST_IDLE;
14954743Swnj 	}
149632523Sbostic 	if (sc->sc_flags & SC_DOWAKE) {
149732523Sbostic 		sc->sc_flags &= ~SC_DOWAKE;
149832523Sbostic 		wakeup((caddr_t)sc);
14996964Ssam 	}
15004743Swnj }
15014743Swnj 
15024743Swnj /*
150332523Sbostic  * Received a response from an as-yet unconfigured drive.  Configure it
150432523Sbostic  * in, if possible.
15054743Swnj  */
150632523Sbostic udaunconf(mi, mp)
150732523Sbostic 	struct mscp_info *mi;
150832523Sbostic 	register struct mscp *mp;
15094743Swnj {
15104743Swnj 
151117553Skarels 	/*
151232523Sbostic 	 * If it is a slave response, copy it to udaslavereply for
151332523Sbostic 	 * udaslave() to look at.
151417553Skarels 	 */
151532523Sbostic 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
151632523Sbostic 	    (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) {
151732523Sbostic 		udaslavereply = *mp;
151832523Sbostic 		return (MSCP_DONE);
15194743Swnj 	}
152032523Sbostic 
152132523Sbostic 	/*
152232523Sbostic 	 * Otherwise, it had better be an available attention response.
152332523Sbostic 	 */
152432523Sbostic 	if (mp->mscp_opcode != M_OP_AVAILATTN)
152532523Sbostic 		return (MSCP_FAILED);
152632523Sbostic 
152732523Sbostic 	/* do what autoconf does */
152832523Sbostic 	return (MSCP_FAILED);	/* not yet, arwhite, not yet */
15294743Swnj }
15304743Swnj 
153132523Sbostic /*
153232523Sbostic  * A drive came on line.  Check its type and size.  Return DONE if
153332523Sbostic  * we think the drive is truly on line.  In any case, awaken anyone
153432523Sbostic  * sleeping on the drive on-line-ness.
153532523Sbostic  */
153632523Sbostic udaonline(ui, mp)
153732523Sbostic 	register struct uba_device *ui;
153832523Sbostic 	struct mscp *mp;
15394743Swnj {
154032523Sbostic 	register struct ra_info *ra = &ra_info[ui->ui_unit];
15414743Swnj 
154232523Sbostic 	wakeup((caddr_t)&ui->ui_flags);
154332523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
154432523Sbostic 		printf("uda%d: attempt to bring ra%d on line failed: ",
154532523Sbostic 			ui->ui_ctlr, ui->ui_unit);
154632523Sbostic 		mscp_printevent(mp);
154732523Sbostic 		ra->ra_state = CLOSED;
154832523Sbostic 		return (MSCP_FAILED);
154932523Sbostic 	}
155032523Sbostic 
155132523Sbostic 	ra->ra_state = OPENRAW;
155232523Sbostic 	ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize;
155334283Skarels 	if (!cold)
155434283Skarels 		printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit,
155534283Skarels 		    ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize);
155632523Sbostic 	/* can now compute ncyl */
155732523Sbostic 	ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks /
155832523Sbostic 		ra->ra_geom.rg_nsectors;
155932523Sbostic 	return (MSCP_DONE);
15604743Swnj }
15614743Swnj 
156232523Sbostic /*
156332523Sbostic  * We got some (configured) unit's status.  Return DONE if it succeeded.
156432523Sbostic  */
156532523Sbostic udagotstatus(ui, mp)
156632523Sbostic 	register struct uba_device *ui;
156732523Sbostic 	register struct mscp *mp;
15684743Swnj {
15694743Swnj 
157032523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
157132523Sbostic 		printf("uda%d: attempt to get status for ra%d failed: ",
157232523Sbostic 			ui->ui_ctlr, ui->ui_unit);
157332523Sbostic 		mscp_printevent(mp);
157432523Sbostic 		return (MSCP_FAILED);
157532523Sbostic 	}
157632523Sbostic 	/* record for (future) bad block forwarding and whatever else */
157732523Sbostic 	uda_rasave(ui->ui_unit, mp, 1);
157832523Sbostic 	return (MSCP_DONE);
15794743Swnj }
15804743Swnj 
158132523Sbostic /*
158232523Sbostic  * A transfer failed.  We get a chance to fix or restart it.
158332523Sbostic  * Need to write the bad block forwaring code first....
158432523Sbostic  */
158532523Sbostic /*ARGSUSED*/
158632523Sbostic udaioerror(ui, mp, bp)
158732523Sbostic 	register struct uba_device *ui;
158832523Sbostic 	register struct mscp *mp;
158932523Sbostic 	struct buf *bp;
15904743Swnj {
15914743Swnj 
159232523Sbostic 	if (mp->mscp_flags & M_EF_BBLKR) {
159332523Sbostic 		/*
159432523Sbostic 		 * A bad block report.  Eventually we will
159532523Sbostic 		 * restart this transfer, but for now, just
159632523Sbostic 		 * log it and give up.
159732523Sbostic 		 */
159832523Sbostic 		log(LOG_ERR, "ra%d: bad block report: %d%s\n",
159932523Sbostic 			ui->ui_unit, mp->mscp_seq.seq_lbn,
160032523Sbostic 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
160132523Sbostic 	} else {
160232523Sbostic 		/*
160332523Sbostic 		 * What the heck IS a `serious exception' anyway?
160432523Sbostic 		 * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION
160532523Sbostic 		 * FOR THEIR OWN CONTROLLERS.
160632523Sbostic 		 */
160732523Sbostic 		if (mp->mscp_flags & M_EF_SEREX)
160832523Sbostic 			log(LOG_ERR, "ra%d: serious exception reported\n",
160932523Sbostic 				ui->ui_unit);
16104743Swnj 	}
161132523Sbostic 	return (MSCP_FAILED);
16124743Swnj }
16134743Swnj 
161432523Sbostic /*
161532523Sbostic  * A replace operation finished.
161632523Sbostic  */
161732523Sbostic /*ARGSUSED*/
161832523Sbostic udareplace(ui, mp)
161932523Sbostic 	struct uba_device *ui;
162032523Sbostic 	struct mscp *mp;
16214743Swnj {
162217553Skarels 
162332523Sbostic 	panic("udareplace");
16244743Swnj }
162517553Skarels 
162632523Sbostic /*
162732523Sbostic  * A bad block related operation finished.
162832523Sbostic  */
162932523Sbostic /*ARGSUSED*/
163032523Sbostic udabb(ui, mp, bp)
163132523Sbostic 	struct uba_device *ui;
163232523Sbostic 	struct mscp *mp;
163332523Sbostic 	struct buf *bp;
163417553Skarels {
163517553Skarels 
163632523Sbostic 	panic("udabb");
163717553Skarels }
163817553Skarels 
163932523Sbostic 
164032523Sbostic /*
164132523Sbostic  * I/O controls.
164232523Sbostic  */
164332523Sbostic udaioctl(dev, cmd, data, flag)
164412511Ssam 	dev_t dev;
164530536Skarels 	int cmd;
164630536Skarels 	caddr_t data;
164730536Skarels 	int flag;
164812511Ssam {
164932523Sbostic 	register int unit = udaunit(dev);
165030536Skarels 	register struct disklabel *lp;
165133443Skarels 	register struct ra_info *ra = &ra_info[unit];
165234638Skarels 	int error = 0;
165312511Ssam 
165432523Sbostic 	lp = &udalabel[unit];
165530536Skarels 
165630536Skarels 	switch (cmd) {
165730536Skarels 
165830536Skarels 	case DIOCGDINFO:
165930536Skarels 		*(struct disklabel *)data = *lp;
166030536Skarels 		break;
166130536Skarels 
166230773Skarels 	case DIOCGPART:
166330773Skarels 		((struct partinfo *)data)->disklab = lp;
166430773Skarels 		((struct partinfo *)data)->part =
166532523Sbostic 		    &lp->d_partitions[udapart(dev)];
166630536Skarels 		break;
166730536Skarels 
166830536Skarels 	case DIOCSDINFO:
166930536Skarels 		if ((flag & FWRITE) == 0)
167030536Skarels 			error = EBADF;
167130536Skarels 		else
167232574Skarels 			error = setdisklabel(lp, (struct disklabel *)data,
167334283Skarels 			    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart);
167430536Skarels 		break;
167530536Skarels 
167633443Skarels 	case DIOCWLABEL:
167733443Skarels 		if ((flag & FWRITE) == 0)
167833443Skarels 			error = EBADF;
167933443Skarels 		else
168033443Skarels 			ra->ra_wlabel = *(int *)data;
168133443Skarels 		break;
168233443Skarels 
168332574Skarels 	case DIOCWDINFO:
168432574Skarels 		if ((flag & FWRITE) == 0)
168532523Sbostic 			error = EBADF;
168632574Skarels 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
168734283Skarels 		    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart)) == 0) {
168834638Skarels 			int wlab;
168934638Skarels 
169034283Skarels 			ra->ra_state = OPEN;
169134638Skarels 			/* simulate opening partition 0 so write succeeds */
169234638Skarels 			ra->ra_openpart |= (1 << 0);		/* XXX */
169334638Skarels 			wlab = ra->ra_wlabel;
169434638Skarels 			ra->ra_wlabel = 1;
169532574Skarels 			error = writedisklabel(dev, udastrategy, lp);
169634638Skarels 			ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
169734638Skarels 			ra->ra_wlabel = wlab;
169834283Skarels 		}
169930536Skarels 		break;
170030536Skarels 
170132523Sbostic #ifdef notyet
170232523Sbostic 	case UDAIOCREPLACE:
170332523Sbostic 		/*
170432523Sbostic 		 * Initiate bad block replacement for the given LBN.
170532523Sbostic 		 * (Should we allow modifiers?)
170632523Sbostic 		 */
170732523Sbostic 		error = EOPNOTSUPP;
170832523Sbostic 		break;
170932523Sbostic 
171032523Sbostic 	case UDAIOCGMICRO:
171132523Sbostic 		/*
171232523Sbostic 		 * Return the microcode revision for the UDA50 running
171332523Sbostic 		 * this drive.
171432523Sbostic 		 */
171533444Sbostic 		*(int *)data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro;
171632523Sbostic 		break;
171732523Sbostic #endif
171832523Sbostic 
171930536Skarels 	default:
172030536Skarels 		error = ENOTTY;
172130536Skarels 		break;
172230536Skarels 	}
172332523Sbostic 	return (error);
172432523Sbostic }
172532523Sbostic 
172632523Sbostic /*
172732523Sbostic  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
172832523Sbostic  * on that Unibus, and requeue outstanding I/O.
172932523Sbostic  */
173032523Sbostic udareset(uban)
173132523Sbostic 	int uban;
173232523Sbostic {
173332523Sbostic 	register struct uba_ctlr *um;
173432523Sbostic 	register struct uda_softc *sc;
173532523Sbostic 	register int ctlr;
173632523Sbostic 
173732523Sbostic 	for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) {
173832523Sbostic 		if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban ||
173932523Sbostic 		    um->um_alive == 0)
174032523Sbostic 			continue;
174132523Sbostic 		printf(" uda%d", ctlr);
174232523Sbostic 
174332523Sbostic 		/*
174432523Sbostic 		 * Our BDP (if any) is gone; our command (if any) is
174532523Sbostic 		 * flushed; the device is no longer mapped; and the
174632523Sbostic 		 * UDA50 is not yet initialised.
174732523Sbostic 		 */
174832523Sbostic 		if (um->um_bdp) {
174932523Sbostic 			printf("<%d>", UBAI_BDP(um->um_bdp));
175032523Sbostic 			um->um_bdp = 0;
175132523Sbostic 		}
175232523Sbostic 		um->um_ubinfo = 0;
175332523Sbostic 		um->um_cmd = 0;
175432523Sbostic 		sc->sc_flags &= ~SC_MAPPED;
175532523Sbostic 		sc->sc_state = ST_IDLE;
175632523Sbostic 
175732523Sbostic 		/* reset queues and requeue pending transfers */
175832523Sbostic 		mscp_requeue(&sc->sc_mi);
175932523Sbostic 
176032523Sbostic 		/*
176132523Sbostic 		 * If it fails to initialise we will notice later and
176232523Sbostic 		 * try again (and again...).  Do not call udastart()
176332523Sbostic 		 * here; it will be done after the controller finishes
176432523Sbostic 		 * initialisation.
176532523Sbostic 		 */
176632523Sbostic 		if (udainit(ctlr))
176732523Sbostic 			printf(" (hung)");
176832523Sbostic 	}
176932523Sbostic }
177032523Sbostic 
177132523Sbostic /*
177232523Sbostic  * Watchdog timer:  If the controller is active, and no interrupts
177332523Sbostic  * have occurred for 30 seconds, assume it has gone away.
177432523Sbostic  */
177532523Sbostic udawatch()
177632523Sbostic {
177732523Sbostic 	register int i;
177832523Sbostic 	register struct uba_ctlr *um;
177932523Sbostic 	register struct uda_softc *sc;
178032523Sbostic 
178132523Sbostic 	timeout(udawatch, (caddr_t) 0, hz);	/* every second */
178232523Sbostic 	for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) {
178332523Sbostic 		if ((um = udaminfo[i]) == 0 || !um->um_alive)
178432523Sbostic 			continue;
178532523Sbostic 		if (sc->sc_state == ST_IDLE)
178632523Sbostic 			continue;
178732523Sbostic 		if (sc->sc_state == ST_RUN && !um->um_tab.b_active)
178832523Sbostic 			sc->sc_wticks = 0;
178932523Sbostic 		else if (++sc->sc_wticks >= 30) {
179032523Sbostic 			sc->sc_wticks = 0;
179132523Sbostic 			printf("uda%d: lost interrupt\n", i);
179232523Sbostic 			ubareset(um->um_ubanum);
179332523Sbostic 		}
179432523Sbostic 	}
179532523Sbostic }
179632523Sbostic 
179732523Sbostic /*
179832523Sbostic  * Do a panic dump.  We set up the controller for one command packet
179932523Sbostic  * and one response packet, for which we use `struct uda1'.
180032523Sbostic  */
180132523Sbostic struct	uda1 {
180232523Sbostic 	struct	uda1ca uda1_ca;	/* communications area */
180332523Sbostic 	struct	mscp uda1_rsp;	/* response packet */
180432523Sbostic 	struct	mscp uda1_cmd;	/* command packet */
180532523Sbostic } uda1;
180632523Sbostic 
180732523Sbostic #define	DBSIZE	32		/* dump 16K at a time */
180832523Sbostic 
180932523Sbostic udadump(dev)
181032523Sbostic 	dev_t dev;
181132523Sbostic {
181232523Sbostic 	struct udadevice *udaddr;
181332523Sbostic 	struct uda1 *ud_ubaddr;
181432523Sbostic 	char *start;
181532523Sbostic 	int num, blk, unit, maxsz, blkoff, reg;
181632523Sbostic 	struct partition *pp;
181732523Sbostic 	register struct uba_regs *uba;
181832523Sbostic 	register struct uba_device *ui;
181932523Sbostic 	register struct uda1 *ud;
182032523Sbostic 	register struct pte *io;
182132523Sbostic 	register int i;
182232523Sbostic 
182332523Sbostic 	/*
182432523Sbostic 	 * Make sure the device is a reasonable place on which to dump.
182532523Sbostic 	 */
182632523Sbostic 	unit = udaunit(dev);
182732523Sbostic 	if (unit >= NRA)
182832523Sbostic 		return (ENXIO);
182933444Sbostic #define	phys(cast, addr)	((cast) ((int)addr & 0x7fffffff))
183032523Sbostic 	ui = phys(struct uba_device *, udadinfo[unit]);
183132523Sbostic 	if (ui == NULL || ui->ui_alive == 0)
183232523Sbostic 		return (ENXIO);
183332523Sbostic 
183432523Sbostic 	/*
183532523Sbostic 	 * Find and initialise the UBA; get the physical address of the
183632523Sbostic 	 * device registers, and of communications area and command and
183732523Sbostic 	 * response packet.
183832523Sbostic 	 */
183932523Sbostic 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
184032523Sbostic 	ubainit(uba);
184132523Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
184232523Sbostic 	ud = phys(struct uda1 *, &uda1);
184332523Sbostic 
184432523Sbostic 	/*
184532523Sbostic 	 * Map the ca+packets into Unibus I/O space so the UDA50 can get
184632523Sbostic 	 * at them.  Use the registers at the end of the Unibus map (since
184732523Sbostic 	 * we will use the registers at the beginning to map the memory
184832523Sbostic 	 * we are dumping).
184932523Sbostic 	 */
185032523Sbostic 	num = btoc(sizeof(struct uda1)) + 1;
185132523Sbostic 	reg = NUBMREG - num;
185232523Sbostic 	io = &uba->uba_map[reg];
185332523Sbostic 	for (i = 0; i < num; i++)
185432523Sbostic 		*(int *)io++ = UBAMR_MRV | (btop(ud) + i);
185532523Sbostic 	ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9));
185632523Sbostic 
185732523Sbostic 	/*
185832523Sbostic 	 * Initialise the controller, with one command and one response
185932523Sbostic 	 * packet.
186032523Sbostic 	 */
186132523Sbostic 	udaddr->udaip = 0;
186232523Sbostic 	if (udadumpwait(udaddr, UDA_STEP1))
186332523Sbostic 		return (EFAULT);
186432523Sbostic 	udaddr->udasa = UDA_ERR;
186532523Sbostic 	if (udadumpwait(udaddr, UDA_STEP2))
186632523Sbostic 		return (EFAULT);
186732523Sbostic 	udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc;
186832523Sbostic 	if (udadumpwait(udaddr, UDA_STEP3))
186932523Sbostic 		return (EFAULT);
187032523Sbostic 	udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16;
187132523Sbostic 	if (udadumpwait(udaddr, UDA_STEP4))
187232523Sbostic 		return (EFAULT);
187332523Sbostic 	uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff;
187432523Sbostic 	udaddr->udasa = UDA_GO;
187532523Sbostic 
187632523Sbostic 	/*
187732523Sbostic 	 * Set up the command and response descriptor, then set the
187832523Sbostic 	 * controller characteristics and bring the drive on line.
187932523Sbostic 	 * Note that all uninitialised locations in uda1_cmd are zero.
188032523Sbostic 	 */
188132523Sbostic 	ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref;
188232523Sbostic 	ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref;
188332523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */
188432523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */
188532523Sbostic 	if (udadumpcmd(M_OP_SETCTLRC, ud, ui))
188632523Sbostic 		return (EFAULT);
188732523Sbostic 	ud->uda1_cmd.mscp_unit = ui->ui_slave;
188832523Sbostic 	if (udadumpcmd(M_OP_ONLINE, ud, ui))
188932523Sbostic 		return (EFAULT);
189032523Sbostic 
189132523Sbostic 	pp = phys(struct partition *,
189232523Sbostic 	    &udalabel[unit].d_partitions[udapart(dev)]);
189332523Sbostic 	maxsz = pp->p_size;
189432523Sbostic 	blkoff = pp->p_offset;
189532523Sbostic 
189632523Sbostic 	/*
189732523Sbostic 	 * Dump all of physical memory, or as much as will fit in the
189832523Sbostic 	 * space provided.
189932523Sbostic 	 */
190032523Sbostic 	start = 0;
190132523Sbostic 	num = maxfree;
190232523Sbostic 	if (dumplo < 0)
190332523Sbostic 		return (EINVAL);
190432523Sbostic 	if (dumplo + num >= maxsz)
190532523Sbostic 		num = maxsz - dumplo;
190632523Sbostic 	blkoff += dumplo;
190732523Sbostic 
190832523Sbostic 	/*
190932523Sbostic 	 * Write out memory, DBSIZE pages at a time.
191032523Sbostic 	 * N.B.: this code depends on the fact that the sector
191132523Sbostic 	 * size == the page size.
191232523Sbostic 	 */
191332523Sbostic 	while (num > 0) {
191432523Sbostic 		blk = num > DBSIZE ? DBSIZE : num;
191532523Sbostic 		io = uba->uba_map;
191632523Sbostic 		/*
191732523Sbostic 		 * Map in the pages to write, leaving an invalid entry
191832523Sbostic 		 * at the end to guard against wild Unibus transfers.
191932523Sbostic 		 * Then do the write.
192032523Sbostic 		 */
192132523Sbostic 		for (i = 0; i < blk; i++)
192233444Sbostic 			*(int *)io++ = UBAMR_MRV | (btop(start) + i);
192333444Sbostic 		*(int *)io = 0;
192432523Sbostic 		ud->uda1_cmd.mscp_unit = ui->ui_slave;
192532523Sbostic 		ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
192632523Sbostic 		ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
192732523Sbostic 		if (udadumpcmd(M_OP_WRITE, ud, ui))
192832523Sbostic 			return (EIO);
192932523Sbostic 		start += blk << PGSHIFT;
193032523Sbostic 		num -= blk;
193132523Sbostic 	}
193232523Sbostic 	return (0);		/* made it! */
193332523Sbostic }
193432523Sbostic 
193532523Sbostic /*
193632523Sbostic  * Wait for some of the bits in `bits' to come on.  If the error bit
193732523Sbostic  * comes on, or ten seconds pass without response, return true (error).
193832523Sbostic  */
193932523Sbostic udadumpwait(udaddr, bits)
194032523Sbostic 	register struct udadevice *udaddr;
194132523Sbostic 	register int bits;
194232523Sbostic {
194332523Sbostic 	register int timo = todr() + 1000;
194432523Sbostic 
194532523Sbostic 	while ((udaddr->udasa & bits) == 0) {
194632523Sbostic 		if (udaddr->udasa & UDA_ERR) {
194732523Sbostic 			printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
194832523Sbostic 			return (1);
194932523Sbostic 		}
195032523Sbostic 		if (todr() >= timo) {
195132523Sbostic 			printf("timeout\ndump ");
195232523Sbostic 			return (1);
195332523Sbostic 		}
195432523Sbostic 	}
195530536Skarels 	return (0);
195630536Skarels }
195730536Skarels 
195832523Sbostic /*
195932523Sbostic  * Feed a command to the UDA50, wait for its response, and return
196032523Sbostic  * true iff something went wrong.
196132523Sbostic  */
196232523Sbostic udadumpcmd(op, ud, ui)
196332523Sbostic 	int op;
196432523Sbostic 	register struct uda1 *ud;
196532523Sbostic 	struct uba_device *ui;
196632523Sbostic {
196732523Sbostic 	register struct udadevice *udaddr;
196832523Sbostic 	register int n;
196932523Sbostic #define mp (&ud->uda1_rsp)
197032523Sbostic 
197133444Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
197232523Sbostic 	ud->uda1_cmd.mscp_opcode = op;
197332523Sbostic 	ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN;
197432523Sbostic 	ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN;
197532523Sbostic 	ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
197632523Sbostic 	ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
197732523Sbostic 	if (udaddr->udasa & UDA_ERR) {
197832523Sbostic 		printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
197932523Sbostic 		return (1);
198032523Sbostic 	}
198132523Sbostic 	n = udaddr->udaip;
198232523Sbostic 	n = todr() + 1000;
198332523Sbostic 	for (;;) {
198432523Sbostic 		if (todr() > n) {
198532523Sbostic 			printf("timeout\ndump ");
198632523Sbostic 			return (1);
198732523Sbostic 		}
198832523Sbostic 		if (ud->uda1_ca.ca_cmdint)
198932523Sbostic 			ud->uda1_ca.ca_cmdint = 0;
199032523Sbostic 		if (ud->uda1_ca.ca_rspint == 0)
199132523Sbostic 			continue;
199232523Sbostic 		ud->uda1_ca.ca_rspint = 0;
199332523Sbostic 		if (mp->mscp_opcode == (op | M_OP_END))
199432523Sbostic 			break;
199532523Sbostic 		printf("\n");
199632523Sbostic 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
199732523Sbostic 
199832523Sbostic 		case MSCPT_SEQ:
199932523Sbostic 			printf("sequential");
200032523Sbostic 			break;
200132523Sbostic 
200232523Sbostic 		case MSCPT_DATAGRAM:
200332523Sbostic 			mscp_decodeerror("uda", ui->ui_ctlr, mp);
200432523Sbostic 			printf("datagram");
200532523Sbostic 			break;
200632523Sbostic 
200732523Sbostic 		case MSCPT_CREDITS:
200832523Sbostic 			printf("credits");
200932523Sbostic 			break;
201032523Sbostic 
201132523Sbostic 		case MSCPT_MAINTENANCE:
201232523Sbostic 			printf("maintenance");
201332523Sbostic 			break;
201432523Sbostic 
201532523Sbostic 		default:
201632523Sbostic 			printf("unknown (type 0x%x)",
201732523Sbostic 				MSCP_MSGTYPE(mp->mscp_msgtc));
201832523Sbostic 			break;
201932523Sbostic 		}
202032523Sbostic 		printf(" ignored\ndump ");
202132523Sbostic 		ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
202232523Sbostic 	}
202332523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
202432523Sbostic 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
202532523Sbostic 			mp->mscp_opcode, mp->mscp_status);
202632523Sbostic 		return (1);
202732523Sbostic 	}
202832523Sbostic 	return (0);
202932523Sbostic #undef mp
203032523Sbostic }
203132523Sbostic 
203232523Sbostic /*
203332523Sbostic  * Return the size of a partition, if known, or -1 if not.
203432523Sbostic  */
203532523Sbostic udasize(dev)
203630536Skarels 	dev_t dev;
203730536Skarels {
203832523Sbostic 	register int unit = udaunit(dev);
203930536Skarels 	register struct uba_device *ui;
204030536Skarels 
204132523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == NULL ||
204232523Sbostic 	    ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 ||
204332523Sbostic 	    ra_info[unit].ra_state != OPEN)
204412511Ssam 		return (-1);
204532523Sbostic 	return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size);
204612511Ssam }
204717553Skarels 
204830536Skarels #ifdef COMPAT_42
204932523Sbostic /*
205032523Sbostic  * Tables mapping unlabelled drives.
205132523Sbostic  */
205230536Skarels struct size {
205330536Skarels 	daddr_t nblocks;
205430536Skarels 	daddr_t blkoff;
205533444Sbostic } ra60_sizes[8] = {
205630536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
205730536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
205830536Skarels 	400176,	0,		/* C=sectors 0 thru 400175 */
205930536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
206030536Skarels 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
206130536Skarels 	350852,	49324,		/* F=sectors 49324 thru 400175 */
206230536Skarels 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
206330536Skarels 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
206433444Sbostic }, ra70_sizes[8] = {
206533444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
206633444Sbostic 	33440,	15972,		/* B=blk 15972 thru 49323 */
206733444Sbostic 	-1,	0,		/* C=blk 0 thru end */
206833444Sbostic 	15884,	341220,		/* D=blk 341220 thru 357103 */
206933444Sbostic 	55936,	357192,		/* E=blk 357192 thru 413127 */
207033444Sbostic 	-1,	413457,		/* F=blk 413457 thru end */
207133444Sbostic 	-1,	341220,		/* G=blk 341220 thru end */
207233444Sbostic 	291346,	49731,		/* H=blk 49731 thru 341076 */
207330536Skarels }, ra80_sizes[8] = {
207430536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
207530536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
207630536Skarels 	242606,	0,		/* C=sectors 0 thru 242605 */
207730536Skarels 	0,	0,		/* D=unused */
207830536Skarels 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
207930536Skarels 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
208030536Skarels 	192696,	49910,		/* G=sectors 49910 thru 242605 */
208130536Skarels 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
208230536Skarels }, ra81_sizes[8] ={
208333444Sbostic #ifdef MARYLAND
208433444Sbostic #ifdef ENEEVAX
208533444Sbostic 	30706,	0,		/* A=cyl    0 thru   42 + 2 sectors */
208633444Sbostic 	40696,	30706,		/* B=cyl   43 thru   99 - 2 sectors */
208733444Sbostic 	-1,	0,		/* C=cyl    0 thru 1247 */
208833444Sbostic 	-1,	71400,		/* D=cyl  100 thru 1247 */
208933444Sbostic 
209033444Sbostic 	15884,	0,		/* E=blk      0 thru  15883 */
209133444Sbostic 	33440,	15884,		/* F=blk  15884 thru  49323 */
209233444Sbostic 	82080,	49324,		/* G=blk  49324 thru 131403 */
209333444Sbostic 	-1,	131404,		/* H=blk 131404 thru    end */
209433444Sbostic #else
209533444Sbostic 	67832,	0,		/* A=cyl    0 thru   94 + 2 sectors */
209633444Sbostic 	67828,	67832,		/* B=cyl   95 thru  189 - 2 sectors */
209733444Sbostic 	-1,	0,		/* C=cyl    0 thru 1247 */
209833444Sbostic 	-1,	135660,		/* D=cyl  190 thru 1247 */
209933444Sbostic 	0,	0,
210033444Sbostic 	0,	0,
210133444Sbostic 	0,	0,
210233444Sbostic 	0,	0,
210333444Sbostic #endif ENEEVAX
210433444Sbostic #else
210530536Skarels /*
210630536Skarels  * These are the new standard partition sizes for ra81's.
210730536Skarels  * An RA_COMPAT system is compiled with D, E, and F corresponding
210830536Skarels  * to the 4.2 partitions for G, H, and F respectively.
210930536Skarels  */
211030536Skarels #ifndef	UCBRA
211130536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
211230536Skarels 	66880,	16422,		/* B=sectors 16422 thru 83301 */
211330536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
211430536Skarels #ifdef RA_COMPAT
211530536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
211630536Skarels 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
211730536Skarels 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
211830536Skarels #else
211930536Skarels 	15884,	375564,		/* D=sectors 375564 thru 391447 */
212030536Skarels 	307200,	391986,		/* E=sectors 391986 thru 699185 */
212130536Skarels 	191352,	699720,		/* F=sectors 699720 thru 891071 */
212230536Skarels #endif RA_COMPAT
212330536Skarels 	515508,	375564,		/* G=sectors 375564 thru 891071 */
212430536Skarels 	291346,	83538,		/* H=sectors 83538 thru 374883 */
212530536Skarels 
212630536Skarels /*
212730536Skarels  * These partitions correspond to the sizes used by sites at Berkeley,
212830536Skarels  * and by those sites that have received copies of the Berkeley driver
212930536Skarels  * with deltas 6.2 or greater (11/15/83).
213030536Skarels  */
213130536Skarels #else UCBRA
213230536Skarels 
213330536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
213430536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
213530536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
213630536Skarels 	15884,	242606,		/* D=sectors 242606 thru 258489 */
213730536Skarels 	307200,	258490,		/* E=sectors 258490 thru 565689 */
213830536Skarels 	325382,	565690,		/* F=sectors 565690 thru 891071 */
213930536Skarels 	648466,	242606,		/* G=sectors 242606 thru 891071 */
214030536Skarels 	193282,	49324,		/* H=sectors 49324 thru 242605 */
214130536Skarels 
214230536Skarels #endif UCBRA
214333444Sbostic #endif MARYLAND
214433444Sbostic }, ra82_sizes[8] = {
214533444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
214633444Sbostic 	66880,	16245,		/* B=blk 16245 thru 83124 */
214733444Sbostic 	-1,	0,		/* C=blk 0 thru end */
214833444Sbostic 	15884,	375345,		/* D=blk 375345 thru 391228 */
214933444Sbostic 	307200,	391590,		/* E=blk 391590 thru 698789 */
215033444Sbostic 	-1,	699390,		/* F=blk 699390 thru end */
215133444Sbostic 	-1,	375345,		/* G=blk 375345 thru end */
215233444Sbostic 	291346,	83790,		/* H=blk 83790 thru 375135 */
215333444Sbostic }, rc25_sizes[8] = {
215433444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
215533444Sbostic 	10032,	15884,		/* B=blk 15884 thru 49323 */
215633444Sbostic 	-1,	0,		/* C=blk 0 thru end */
215733444Sbostic 	0,	0,		/* D=blk 340670 thru 356553 */
215833444Sbostic 	0,	0,		/* E=blk 356554 thru 412489 */
215933444Sbostic 	0,	0,		/* F=blk 412490 thru end */
216033444Sbostic 	-1,	25916,		/* G=blk 49324 thru 131403 */
216133444Sbostic 	0,	0,		/* H=blk 131404 thru end */
216233444Sbostic }, rd52_sizes[8] = {
216333444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
216433444Sbostic 	9766,	15884,		/* B=blk 15884 thru 25649 */
216533444Sbostic 	-1,	0,		/* C=blk 0 thru end */
216633444Sbostic 	0,	0,		/* D=unused */
216733444Sbostic 	0,	0,		/* E=unused */
216833444Sbostic 	0,	0,		/* F=unused */
216933444Sbostic 	-1,	25650,		/* G=blk 25650 thru end */
217033444Sbostic 	0,	0,		/* H=unused */
217133444Sbostic }, rd53_sizes[8] = {
217233444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
217333444Sbostic 	33440,	15884,		/* B=blk 15884 thru 49323 */
217433444Sbostic 	-1,	0,		/* C=blk 0 thru end */
217533444Sbostic 	0,	0,		/* D=unused */
217633444Sbostic 	33440,	0,		/* E=blk 0 thru 33439 */
217733444Sbostic 	-1,	33440,		/* F=blk 33440 thru end */
217833444Sbostic 	-1,	49324,		/* G=blk 49324 thru end */
217933444Sbostic 	-1,	15884,		/* H=blk 15884 thru end */
218033444Sbostic }, rx50_sizes[8] = {
218133444Sbostic 	800,	0,		/* A=blk 0 thru 799 */
218233444Sbostic 	0,	0,
218333444Sbostic 	-1,	0,		/* C=blk 0 thru end */
218433444Sbostic 	0,	0,
218533444Sbostic 	0,	0,
218633444Sbostic 	0,	0,
218733444Sbostic 	0,	0,
218833444Sbostic 	0,	0,
218930536Skarels };
219030536Skarels 
219132523Sbostic /*
219233444Sbostic  * Media ID decoding table.
219332523Sbostic  */
219432523Sbostic struct	udatypes {
219533444Sbostic 	u_long	ut_id;		/* media drive ID */
219632523Sbostic 	char	*ut_name;	/* drive type name */
219732523Sbostic 	struct	size *ut_sizes;	/* partition tables */
219832523Sbostic 	int	ut_nsectors, ut_ntracks, ut_ncylinders;
219932523Sbostic } udatypes[] = {
220033444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 60), "ra60", ra60_sizes, 42, 4, 2382 },
220133444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 70), "ra70", ra70_sizes, 33, 11, 1507 },
220233444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 80), "ra80", ra80_sizes, 31, 14, 559 },
220333444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 81), "ra81", ra81_sizes, 51, 14, 1248 },
220433444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 82), "ra82", ra82_sizes, 57, 14, 1423 },
220533444Sbostic 	{ MSCP_MKDRIVE2('R', 'C', 25), "rc25-removable",
220633444Sbostic 						rc25_sizes, 42, 4, 302 },
220733444Sbostic 	{ MSCP_MKDRIVE3('R', 'C', 'F', 25), "rc25-fixed",
220833444Sbostic 						rc25_sizes, 42, 4, 302 },
220933444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 52), "rd52", rd52_sizes, 18, 7, 480 },
221033444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 53), "rd53", rd53_sizes, 18, 8, 963 },
221133444Sbostic 	{ MSCP_MKDRIVE2('R', 'X', 50), "rx50", rx50_sizes, 10, 1, 80 },
221233444Sbostic 	0
221332523Sbostic };
221432523Sbostic 
221532523Sbostic #define NTYPES (sizeof(udatypes) / sizeof(*udatypes))
221632523Sbostic 
221732523Sbostic udamaptype(unit, lp)
221832523Sbostic 	int unit;
221930536Skarels 	register struct disklabel *lp;
222030536Skarels {
222132523Sbostic 	register struct udatypes *ut;
222232523Sbostic 	register struct size *sz;
222330536Skarels 	register struct partition *pp;
222432523Sbostic 	register char *p;
222532523Sbostic 	register int i;
222632523Sbostic 	register struct ra_info *ra = &ra_info[unit];
222730536Skarels 
222832523Sbostic 	lp->d_secsize = 512;
222932523Sbostic 	lp->d_secperunit = ra->ra_dsize;
223033444Sbostic 	i = MSCP_MEDIA_DRIVE(ra->ra_mediaid);
223133444Sbostic 	for (ut = udatypes; ut->ut_id; ut++)
223233444Sbostic 		if (ut->ut_id == i)
223333444Sbostic 			goto found;
223433444Sbostic 
223533444Sbostic 	/* not one we know; fake up a label for the whole drive */
223633444Sbostic 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
223733444Sbostic 	lp->d_ntracks = ra->ra_geom.rg_ntracks;
223833444Sbostic 	lp->d_ncylinders = ra->ra_geom.rg_ncyl;
223933444Sbostic 	i = ra->ra_mediaid;	/* print the port type too */
224034283Skarels 	if (!cold)
224134283Skarels 		log(LOG_ERR, "ra%d", unit);
224234283Skarels 	addlog(": don't have a partition table for %c%c %c%c%c%d;\n\
224334283Skarels using (s,t,c)=(%d,%d,%d)",
224434283Skarels 		MSCP_MID_CHAR(4, i), MSCP_MID_CHAR(3, i),
224533444Sbostic 		MSCP_MID_CHAR(2, i), MSCP_MID_CHAR(1, i),
224633444Sbostic 		MSCP_MID_CHAR(0, i), MSCP_MID_CHAR(0, i),
224733444Sbostic 		MSCP_MID_NUM(i), lp->d_nsectors,
224833444Sbostic 		lp->d_ntracks, lp->d_ncylinders);
224934283Skarels 	if (!cold)
225034283Skarels 		addlog("\n");
225133444Sbostic 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
225233444Sbostic 	lp->d_typename[0] = 'r';
225333444Sbostic 	lp->d_typename[1] = 'a';
225433444Sbostic 	lp->d_typename[2] = '?';
225533444Sbostic 	lp->d_typename[3] = '?';
225633444Sbostic 	lp->d_typename[4] = 0;
225733444Sbostic 	lp->d_npartitions = 1;
225833444Sbostic 	lp->d_partitions[0].p_offset = 0;
225933444Sbostic 	lp->d_partitions[0].p_size = lp->d_secperunit;
226033444Sbostic 	return (0);
226133444Sbostic found:
226232523Sbostic 	p = ut->ut_name;
226332523Sbostic 	for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++)
226432523Sbostic 		lp->d_typename[i] = *p++;
226532523Sbostic 	lp->d_typename[i] = 0;
226632523Sbostic 	sz = ut->ut_sizes;
226732523Sbostic 	/* GET nsectors, ntracks, ncylinders FROM SAVED GEOMETRY? */
226832523Sbostic 	lp->d_nsectors = ut->ut_nsectors;
226932523Sbostic 	lp->d_ntracks = ut->ut_ntracks;
227032523Sbostic 	lp->d_ncylinders = ut->ut_ncylinders;
227130536Skarels 	lp->d_npartitions = 8;
227230536Skarels 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
227332523Sbostic 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) {
227432523Sbostic 		pp->p_offset = sz->blkoff;
227532523Sbostic 		if ((pp->p_size = sz->nblocks) == (u_long)-1)
227632523Sbostic 			pp->p_size = ra->ra_dsize - sz->blkoff;
227730536Skarels 	}
227830536Skarels 	return (1);
227930536Skarels }
228032523Sbostic #endif /* COMPAT_42 */
228132523Sbostic #endif /* NUDA > 0 */
2282