xref: /csrg-svn/sys/vax/uba/uda.c (revision 34649)
123353Smckusick /*
232523Sbostic  * Copyright (c) 1987 Regents of the University of California.
332523Sbostic  * All rights reserved.  The Berkeley software License Agreement
432523Sbostic  * specifies the terms and conditions for redistribution.
532523Sbostic  *
6*34649Skarels  *	@(#)uda.c	7.18 (Berkeley) 06/06/88
732523Sbostic  *
823353Smckusick  */
923353Smckusick 
1032523Sbostic /*
1132523Sbostic  * UDA50/MSCP device driver
1217553Skarels  */
1317553Skarels 
1432523Sbostic #define	POLLSTATS
1532523Sbostic 
1632523Sbostic /*
1732523Sbostic  * TODO
1832523Sbostic  *	write bad block forwarding code
1932523Sbostic  */
2032523Sbostic 
214743Swnj #include "ra.h"
2232523Sbostic 
2317642Skarels #if NUDA > 0
2432523Sbostic 
254743Swnj /*
2632523Sbostic  * CONFIGURATION OPTIONS.  The next three defines are tunable -- tune away!
274743Swnj  *
2832523Sbostic  * COMPAT_42 enables 4.2/4.3 compatibility (label mapping)
2932523Sbostic  *
3032523Sbostic  * NRSPL2 and NCMDL2 control the number of response and command
3132523Sbostic  * packets respectively.  They may be any value from 0 to 7, though
3232523Sbostic  * setting them higher than 5 is unlikely to be of any value.
3332523Sbostic  * If you get warnings about your command ring being too small,
3432523Sbostic  * try increasing the values by one.
3532523Sbostic  *
3632523Sbostic  * MAXUNIT controls the maximum unit number (number of drives per
3732523Sbostic  * controller) we are prepared to handle.
3832523Sbostic  *
3932523Sbostic  * DEFAULT_BURST must be at least 1.
404743Swnj  */
4132523Sbostic #define	COMPAT_42
4232523Sbostic 
4332523Sbostic #define	NRSPL2	5		/* log2 number of response packets */
4432523Sbostic #define NCMDL2	5		/* log2 number of command packets */
4532523Sbostic #define	MAXUNIT	8		/* maximum allowed unit number */
4632523Sbostic #define	DEFAULT_BURST	4	/* default DMA burst size */
4732523Sbostic 
4817553Skarels #include "param.h"
4917553Skarels #include "systm.h"
5017553Skarels #include "buf.h"
5117553Skarels #include "conf.h"
5217553Skarels #include "dir.h"
5330536Skarels #include "file.h"
5430536Skarels #include "ioctl.h"
5517553Skarels #include "user.h"
5617553Skarels #include "map.h"
5717553Skarels #include "vm.h"
5830536Skarels #include "dkstat.h"
5917553Skarels #include "cmap.h"
6030536Skarels #include "disklabel.h"
6130536Skarels #include "syslog.h"
6230773Skarels #include "stat.h"
634743Swnj 
6434283Skarels #include "../machine/pte.h"
6534283Skarels 
668482Sroot #include "../vax/cpu.h"
6717553Skarels #include "ubareg.h"
6817553Skarels #include "ubavar.h"
698613Sroot 
7032523Sbostic #define	NRSP	(1 << NRSPL2)
7132523Sbostic #define	NCMD	(1 << NCMDL2)
728613Sroot 
7332523Sbostic #include "udareg.h"
748482Sroot #include "../vax/mscp.h"
7532523Sbostic #include "../vax/mscpvar.h"
7632523Sbostic #include "../vax/mtpr.h"
774743Swnj 
7832523Sbostic /*
7932523Sbostic  * UDA communications area and MSCP packet pools, per controller.
8032523Sbostic  */
8132523Sbostic struct	uda {
8232523Sbostic 	struct	udaca uda_ca;		/* communications area */
8332523Sbostic 	struct	mscp uda_rsp[NRSP];	/* response packets */
8432523Sbostic 	struct	mscp uda_cmd[NCMD];	/* command packets */
854743Swnj } uda[NUDA];
864743Swnj 
8732523Sbostic /*
8832523Sbostic  * Software status, per controller.
8932523Sbostic  */
9032523Sbostic struct	uda_softc {
9132523Sbostic 	struct	uda *sc_uda;	/* Unibus address of uda struct */
9232523Sbostic 	short	sc_state;	/* UDA50 state; see below */
9332523Sbostic 	short	sc_flags;	/* flags; see below */
9432523Sbostic 	int	sc_micro;	/* microcode revision */
9532523Sbostic 	int	sc_ivec;	/* interrupt vector address */
9632523Sbostic 	struct	mscp_info sc_mi;/* MSCP info (per mscpvar.h) */
9732523Sbostic #ifndef POLLSTATS
9832523Sbostic 	int	sc_wticks;	/* watchdog timer ticks */
9932523Sbostic #else
10032523Sbostic 	short	sc_wticks;
10132523Sbostic 	short	sc_ncmd;
10232523Sbostic #endif
10332523Sbostic } uda_softc[NUDA];
10424742Sbloom 
10532523Sbostic #ifdef POLLSTATS
10632523Sbostic struct udastats {
10732523Sbostic 	int	ncmd;
10832523Sbostic 	int	cmd[NCMD + 1];
10932523Sbostic } udastats = { NCMD + 1 };
11032523Sbostic #endif
11117553Skarels 
11232523Sbostic /*
11332523Sbostic  * Controller states
11432523Sbostic  */
11532523Sbostic #define	ST_IDLE		0	/* uninitialised */
11632523Sbostic #define	ST_STEP1	1	/* in `STEP 1' */
11732523Sbostic #define	ST_STEP2	2	/* in `STEP 2' */
11832523Sbostic #define	ST_STEP3	3	/* in `STEP 3' */
11932523Sbostic #define	ST_SETCHAR	4	/* in `Set Controller Characteristics' */
12032523Sbostic #define	ST_RUN		5	/* up and running */
1214743Swnj 
12232523Sbostic /*
12332523Sbostic  * Flags
12432523Sbostic  */
12532523Sbostic #define	SC_MAPPED	0x01	/* mapped in Unibus I/O space */
12632523Sbostic #define	SC_INSTART	0x02	/* inside udastart() */
12732523Sbostic #define	SC_GRIPED	0x04	/* griped about cmd ring too small */
12832523Sbostic #define	SC_INSLAVE	0x08	/* inside udaslave() */
12932523Sbostic #define	SC_DOWAKE	0x10	/* wakeup when ctlr init done */
13032523Sbostic #define	SC_STARTPOLL	0x20	/* need to initiate polling */
13112421Ssam 
13232523Sbostic /*
13332523Sbostic  * Device to unit number and partition and back
13432523Sbostic  */
13532523Sbostic #define	UNITSHIFT	3
13632523Sbostic #define	UNITMASK	7
13732523Sbostic #define	udaunit(dev)	(minor(dev) >> UNITSHIFT)
13832523Sbostic #define	udapart(dev)	(minor(dev) & UNITMASK)
13932523Sbostic #define	udaminor(u, p)	(((u) << UNITSHIFT) | (p))
1404743Swnj 
14117553Skarels /*
14232523Sbostic  * Drive status, per drive
14317553Skarels  */
14432523Sbostic struct ra_info {
14532523Sbostic 	daddr_t	ra_dsize;	/* size in sectors */
14633444Sbostic /*	u_long	ra_type;	/* drive type */
14732523Sbostic 	u_long	ra_mediaid;	/* media id */
14832523Sbostic 	int	ra_state;	/* open/closed state */
14932523Sbostic 	struct	ra_geom {	/* geometry information */
15032523Sbostic 		u_short	rg_nsectors;	/* sectors/track */
15132523Sbostic 		u_short	rg_ngroups;	/* track groups */
15232523Sbostic 		u_short	rg_ngpc;	/* groups/cylinder */
15332523Sbostic 		u_short	rg_ntracks;	/* ngroups*ngpc */
15432523Sbostic 		u_short	rg_ncyl;	/* ra_dsize/ntracks/nsectors */
15532523Sbostic #ifdef notyet
15632523Sbostic 		u_short	rg_rctsize;	/* size of rct */
15732523Sbostic 		u_short	rg_rbns;	/* replacement blocks per track */
15832523Sbostic 		u_short	rg_nrct;	/* number of rct copies */
15932523Sbostic #endif
16032523Sbostic 	} ra_geom;
16133443Skarels 	int	ra_wlabel;	/* label sector is currently writable */
16232523Sbostic 	u_long	ra_openpart;	/* partitions open */
16332523Sbostic 	u_long	ra_bopenpart;	/* block partitions open */
16432523Sbostic 	u_long	ra_copenpart;	/* character partitions open */
16532523Sbostic } ra_info[NRA];
16617553Skarels 
16730536Skarels /*
16830536Skarels  * Software state, per drive
16930536Skarels  */
17030536Skarels #define	CLOSED		0
17130536Skarels #define	WANTOPEN	1
17230536Skarels #define	RDLABEL		2
17330536Skarels #define	OPEN		3
17430536Skarels #define	OPENRAW		4
17517553Skarels 
17632523Sbostic /*
17732523Sbostic  * Definition of the driver for autoconf.
17832523Sbostic  */
17932523Sbostic int	udaprobe(), udaslave(), udaattach(), udadgo(), udaintr();
18032523Sbostic struct	uba_ctlr *udaminfo[NUDA];
18132523Sbostic struct	uba_device *udadinfo[NRA];
18232523Sbostic struct	disklabel udalabel[NRA];
18317553Skarels 
18432523Sbostic u_short	udastd[] = { 0772150, 0772550, 0777550, 0 };
18532523Sbostic struct	uba_driver udadriver =
18632523Sbostic  { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda",
18732523Sbostic    udaminfo };
18817553Skarels 
18932523Sbostic /*
19032523Sbostic  * More driver definitions, for generic MSCP code.
19132523Sbostic  */
19232523Sbostic int	udadgram(), udactlrdone(), udaunconf(), udaiodone();
19332523Sbostic int	udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb();
1944743Swnj 
19532523Sbostic struct	buf udautab[NRA];	/* per drive transfer queue */
1964743Swnj 
19732523Sbostic struct	mscp_driver udamscpdriver =
19834524Skarels  { MAXUNIT, NRA, UNITSHIFT, udautab, udalabel, udadinfo,
19932523Sbostic    udadgram, udactlrdone, udaunconf, udaiodone,
20032523Sbostic    udaonline, udagotstatus, udareplace, udaioerror, udabb,
20132523Sbostic    "uda", "ra" };
20232523Sbostic 
20332523Sbostic /*
20432523Sbostic  * Miscellaneous private variables.
20532523Sbostic  */
20632523Sbostic char	udasr_bits[] = UDASR_BITS;
20732523Sbostic 
20832523Sbostic struct	uba_device *udaip[NUDA][MAXUNIT];
20932523Sbostic 				/* inverting pointers: ctlr & unit => Unibus
21032523Sbostic 				   device pointer */
21132523Sbostic 
21232523Sbostic int	udaburst[NUDA] = { 0 };	/* burst size, per UDA50, zero => default;
21332523Sbostic 				   in data space so patchable via adb */
21432523Sbostic 
21532523Sbostic struct	mscp udaslavereply;	/* get unit status response packet, set
21632523Sbostic 				   for udaslave by udaunconf, via udaintr */
21732523Sbostic 
21832523Sbostic static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr
21932523Sbostic 				   info to slave routine; instead, we remember
22032523Sbostic 				   the last ctlr argument to probe */
22132523Sbostic 
22232523Sbostic int	udawstart, udawatch();	/* watchdog timer */
22332523Sbostic 
22432523Sbostic /*
22532523Sbostic  * Externals
22632523Sbostic  */
22732523Sbostic int	wakeup();
22832523Sbostic int	hz;
22932523Sbostic 
23032523Sbostic /*
23132523Sbostic  * Poke at a supposed UDA50 to see if it is there.
23232523Sbostic  * This routine duplicates some of the code in udainit() only
23332523Sbostic  * because autoconf has not set up the right information yet.
23432523Sbostic  * We have to do everything `by hand'.
23532523Sbostic  */
23632523Sbostic udaprobe(reg, ctlr, um)
2374743Swnj 	caddr_t reg;
2384743Swnj 	int ctlr;
23932523Sbostic 	struct uba_ctlr *um;
2404743Swnj {
2414743Swnj 	register int br, cvec;
24232523Sbostic 	register struct uda_softc *sc;
24332523Sbostic 	register struct udadevice *udaddr;
24432523Sbostic 	register struct mscp_info *mi;
24532523Sbostic 	int timeout, tries;
2464743Swnj 
24732523Sbostic #ifdef VAX750
24832523Sbostic 	/*
24932523Sbostic 	 * The UDA50 wants to share BDPs on 750s, but not on 780s or
25032523Sbostic 	 * 8600s.  (730s have no BDPs anyway.)  Toward this end, we
25132523Sbostic 	 * here set the `keep bdp' flag in the per-driver information
25232523Sbostic 	 * if this is a 750.  (We just need to do it once, but it is
25332523Sbostic 	 * easiest to do it now, for each UDA50.)
25432523Sbostic 	 */
25532523Sbostic 	if (cpu == VAX_750)
25632523Sbostic 		udadriver.ud_keepbdp = 1;
25732523Sbostic #endif
25817553Skarels 
25932523Sbostic 	probeum = um;			/* remember for udaslave() */
2604743Swnj #ifdef lint
26132523Sbostic 	br = 0; cvec = br; br = cvec; udaintr(0);
2624743Swnj #endif
26332523Sbostic 	/*
26432523Sbostic 	 * Set up the controller-specific generic MSCP driver info.
26532523Sbostic 	 * Note that this should really be done in the (nonexistent)
26632523Sbostic 	 * controller attach routine.
26732523Sbostic 	 */
26832523Sbostic 	sc = &uda_softc[ctlr];
26932523Sbostic 	mi = &sc->sc_mi;
27032523Sbostic 	mi->mi_md = &udamscpdriver;
27132523Sbostic 	mi->mi_ctlr = um->um_ctlr;
27232523Sbostic 	mi->mi_tab = &um->um_tab;
27332523Sbostic 	mi->mi_ip = udaip[ctlr];
27432523Sbostic 	mi->mi_cmd.mri_size = NCMD;
27532523Sbostic 	mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc;
27632523Sbostic 	mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd;
27732523Sbostic 	mi->mi_rsp.mri_size = NRSP;
27832523Sbostic 	mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc;
27932523Sbostic 	mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp;
28032523Sbostic 	mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab;
28132523Sbostic 
28232523Sbostic 	/*
28332523Sbostic 	 * More controller specific variables.  Again, this should
28432523Sbostic 	 * be in the controller attach routine.
28532523Sbostic 	 */
28632523Sbostic 	if (udaburst[ctlr] == 0)
28732523Sbostic 		udaburst[ctlr] = DEFAULT_BURST;
28832523Sbostic 
28932523Sbostic 	/*
29032523Sbostic 	 * Get an interrupt vector.  Note that even if the controller
29132523Sbostic 	 * does not respond, we keep the vector.  This is not a serious
29232523Sbostic 	 * problem; but it would be easily fixed if we had a controller
29332523Sbostic 	 * attach routine.  Sigh.
29432523Sbostic 	 */
29532523Sbostic 	sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
29617553Skarels 	udaddr = (struct udadevice *) reg;
29717553Skarels 
29832523Sbostic 	/*
29932523Sbostic 	 * Initialise the controller (partially).  The UDA50 programmer's
30032523Sbostic 	 * manual states that if initialisation fails, it should be retried
30132523Sbostic 	 * at least once, but after a second failure the port should be
30232523Sbostic 	 * considered `down'; it also mentions that the controller should
30332523Sbostic 	 * initialise within ten seconds.  Or so I hear; I have not seen
30432523Sbostic 	 * this manual myself.
30532523Sbostic 	 */
30632523Sbostic 	tries = 0;
30732523Sbostic again:
30832523Sbostic 	udaddr->udaip = 0;		/* start initialisation */
30932523Sbostic 	timeout = todr() + 1000;	/* timeout in 10 seconds */
31032523Sbostic 	while ((udaddr->udasa & UDA_STEP1) == 0)
31132523Sbostic 		if (todr() > timeout)
31232523Sbostic 			goto bad;
31332523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
31432523Sbostic 		(sc->sc_ivec >> 2);
31532523Sbostic 	while ((udaddr->udasa & UDA_STEP2) == 0)
31632523Sbostic 		if (todr() > timeout)
31732523Sbostic 			goto bad;
31832523Sbostic 
31932523Sbostic 	/* should have interrupted by now */
32032523Sbostic #ifdef VAX630
32132523Sbostic 	if (cpu == VAX_630)
32232523Sbostic 		br = 0x15;	/* screwy interrupt structure */
32327254Skridle #endif
32432523Sbostic 	return (sizeof (struct udadevice));
32532523Sbostic bad:
32632523Sbostic 	if (++tries < 2)
32732523Sbostic 		goto again;
32832523Sbostic 	return (0);
3294743Swnj }
3304743Swnj 
33132523Sbostic /*
33232523Sbostic  * Find a slave.  We allow wildcard slave numbers (something autoconf
33332523Sbostic  * is not really prepared to deal with); and we need to know the
33432523Sbostic  * controller number to talk to the UDA.  For the latter, we keep
33532523Sbostic  * track of the last controller probed, since a controller probe
33632523Sbostic  * immediately precedes all slave probes for that controller.  For the
33732523Sbostic  * former, we simply put the unit number into ui->ui_slave after we
33832523Sbostic  * have found one.
33932523Sbostic  *
34032523Sbostic  * Note that by the time udaslave is called, the interrupt vector
34132523Sbostic  * for the UDA50 has been set up (so that udaunconf() will be called).
34232523Sbostic  */
34332523Sbostic udaslave(ui, reg)
34432523Sbostic 	register struct uba_device *ui;
3454743Swnj 	caddr_t reg;
3464743Swnj {
34732523Sbostic 	register struct uba_ctlr *um = probeum;
34832523Sbostic 	register struct mscp *mp;
34932523Sbostic 	register struct uda_softc *sc;
35033443Skarels 	int next = 0, timeout, tries, i;
35117553Skarels 
35232523Sbostic #ifdef lint
35332523Sbostic 	i = 0; i = i;
35432523Sbostic #endif
35532523Sbostic 	/*
35632523Sbostic 	 * Make sure the controller is fully initialised, by waiting
35732523Sbostic 	 * for it if necessary.
35832523Sbostic 	 */
35932523Sbostic 	sc = &uda_softc[um->um_ctlr];
36032523Sbostic 	if (sc->sc_state == ST_RUN)
36132523Sbostic 		goto findunit;
36232523Sbostic 	tries = 0;
36332523Sbostic again:
36432523Sbostic 	if (udainit(ui->ui_ctlr))
36532523Sbostic 		return (0);
36632523Sbostic 	timeout = todr() + 1000;		/* 10 seconds */
36732523Sbostic 	while (todr() < timeout)
36832523Sbostic 		if (sc->sc_state == ST_RUN)	/* made it */
36932523Sbostic 			goto findunit;
37032523Sbostic 	if (++tries < 2)
37132523Sbostic 		goto again;
37232523Sbostic 	printf("uda%d: controller hung\n", um->um_ctlr);
37332523Sbostic 	return (0);
37417553Skarels 
37532523Sbostic 	/*
37632523Sbostic 	 * The controller is all set; go find the unit.  Grab an
37732523Sbostic 	 * MSCP packet and send out a Get Unit Status command, with
37832523Sbostic 	 * the `next unit' modifier if we are looking for a generic
37932523Sbostic 	 * unit.  We set the `in slave' flag so that udaunconf()
38032523Sbostic 	 * knows to copy the response to `udaslavereply'.
38132523Sbostic 	 */
38232523Sbostic findunit:
38332523Sbostic 	udaslavereply.mscp_opcode = 0;
38432523Sbostic 	sc->sc_flags |= SC_INSLAVE;
38532523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL)
38632523Sbostic 		panic("udaslave");		/* `cannot happen' */
38732523Sbostic 	mp->mscp_opcode = M_OP_GETUNITST;
38832523Sbostic 	if (ui->ui_slave == '?') {
38932523Sbostic 		mp->mscp_unit = next;
39032523Sbostic 		mp->mscp_modifier = M_GUM_NEXTUNIT;
39132523Sbostic 	} else {
39232523Sbostic 		mp->mscp_unit = ui->ui_slave;
39332523Sbostic 		mp->mscp_modifier = 0;
39417553Skarels 	}
39532523Sbostic 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
39632523Sbostic 	i = ((struct udadevice *) reg)->udaip;	/* initiate polling */
39732523Sbostic 	mp = &udaslavereply;
39832523Sbostic 	timeout = todr() + 1000;
39932523Sbostic 	while (todr() < timeout)
40032523Sbostic 		if (mp->mscp_opcode)
40132523Sbostic 			goto gotit;
40232523Sbostic 	printf("uda%d: no response to Get Unit Status request\n",
40332523Sbostic 		um->um_ctlr);
40432523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
40532523Sbostic 	return (0);
40632523Sbostic 
40732523Sbostic gotit:
40832523Sbostic 	sc->sc_flags &= ~SC_INSLAVE;
40932523Sbostic 
41032523Sbostic 	/*
41132523Sbostic 	 * Got a slave response.  If the unit is there, use it.
41232523Sbostic 	 */
41332523Sbostic 	switch (mp->mscp_status & M_ST_MASK) {
41432523Sbostic 
41532523Sbostic 	case M_ST_SUCCESS:	/* worked */
41632523Sbostic 	case M_ST_AVAILABLE:	/* found another drive */
41732523Sbostic 		break;		/* use it */
41832523Sbostic 
41932523Sbostic 	case M_ST_OFFLINE:
42032523Sbostic 		/*
42132523Sbostic 		 * Figure out why it is off line.  It may be because
42232523Sbostic 		 * it is nonexistent, or because it is spun down, or
42332523Sbostic 		 * for some other reason.
42432523Sbostic 		 */
42532523Sbostic 		switch (mp->mscp_status & ~M_ST_MASK) {
42632523Sbostic 
42732523Sbostic 		case M_OFFLINE_UNKNOWN:
42832523Sbostic 			/*
42932523Sbostic 			 * No such drive, and there are none with
43032523Sbostic 			 * higher unit numbers either, if we are
43132523Sbostic 			 * using M_GUM_NEXTUNIT.
43232523Sbostic 			 */
43332523Sbostic 			return (0);
43432523Sbostic 
43532523Sbostic 		case M_OFFLINE_UNMOUNTED:
43632523Sbostic 			/*
43732523Sbostic 			 * The drive is not spun up.  Use it anyway.
43832523Sbostic 			 *
43932523Sbostic 			 * N.B.: this seems to be a common occurrance
44032523Sbostic 			 * after a power failure.  The first attempt
44132523Sbostic 			 * to bring it on line seems to spin it up
44232523Sbostic 			 * (and thus takes several minutes).  Perhaps
44332523Sbostic 			 * we should note here that the on-line may
44432523Sbostic 			 * take longer than usual.
44532523Sbostic 			 */
44632523Sbostic 			break;
44732523Sbostic 
44832523Sbostic 		default:
44932523Sbostic 			/*
45032523Sbostic 			 * In service, or something else equally unusable.
45132523Sbostic 			 */
45232523Sbostic 			printf("uda%d: unit %d off line: ", um->um_ctlr,
45332523Sbostic 				mp->mscp_unit);
45432523Sbostic 			mscp_printevent(mp);
45532523Sbostic 			goto try_another;
45632523Sbostic 		}
45732523Sbostic 		break;
45832523Sbostic 
45932523Sbostic 	default:
46032523Sbostic 		printf("uda%d: unable to get unit status: ", um->um_ctlr);
46132523Sbostic 		mscp_printevent(mp);
46232523Sbostic 		return (0);
46317553Skarels 	}
46432523Sbostic 
46532523Sbostic 	/*
46632523Sbostic 	 * Does this ever happen?  What (if anything) does it mean?
46732523Sbostic 	 */
46832523Sbostic 	if (mp->mscp_unit < next) {
46932523Sbostic 		printf("uda%d: unit %d, next %d\n",
47032523Sbostic 			um->um_ctlr, mp->mscp_unit, next);
47132523Sbostic 		return (0);
47217553Skarels 	}
47332523Sbostic 
47432523Sbostic 	if (mp->mscp_unit >= MAXUNIT) {
47532523Sbostic 		printf("uda%d: cannot handle unit number %d (max is %d)\n",
47632523Sbostic 			um->um_ctlr, mp->mscp_unit, MAXUNIT - 1);
47732523Sbostic 		return (0);
47832523Sbostic 	}
47932523Sbostic 
48032523Sbostic 	/*
48132523Sbostic 	 * See if we already handle this drive.
48232523Sbostic 	 * (Only likely if ui->ui_slave=='?'.)
48332523Sbostic 	 */
48432523Sbostic 	if (udaip[um->um_ctlr][mp->mscp_unit] != NULL) {
48532523Sbostic try_another:
48632523Sbostic 		if (ui->ui_slave != '?')
48732523Sbostic 			return (0);
48832523Sbostic 		next = mp->mscp_unit + 1;
48932523Sbostic 		goto findunit;
49032523Sbostic 	}
49132523Sbostic 
49232523Sbostic 	/*
49332523Sbostic 	 * Voila!
49432523Sbostic 	 */
49532523Sbostic 	uda_rasave(ui->ui_unit, mp, 0);
49632523Sbostic 	ui->ui_flags = 0;	/* not on line, nor anything else */
49732523Sbostic 	ui->ui_slave = mp->mscp_unit;
49832523Sbostic 	return (1);
4994743Swnj }
5004743Swnj 
50132523Sbostic /*
50232523Sbostic  * Attach a found slave.  Make sure the watchdog timer is running.
50332523Sbostic  * If this disk is being profiled, fill in the `mspw' value (used by
50432523Sbostic  * what?).  Set up the inverting pointer, and attempt to bring the
50532523Sbostic  * drive on line and read its label.
50632523Sbostic  */
50732523Sbostic udaattach(ui)
5084743Swnj 	register struct uba_device *ui;
5094743Swnj {
51032523Sbostic 	register int unit = ui->ui_unit;
51132523Sbostic 
51232523Sbostic 	if (udawstart == 0) {
51332523Sbostic 		timeout(udawatch, (caddr_t) 0, hz);
51432523Sbostic 		udawstart++;
51532523Sbostic 	}
51633444Sbostic 
51733444Sbostic 	/*
51833444Sbostic 	 * Floppies cannot be brought on line unless there is
51933444Sbostic 	 * a disk in the drive.  Since an ONLINE while cold
52033444Sbostic 	 * takes ten seconds to fail, and (when notyet becomes now)
52133444Sbostic 	 * no sensible person will swap to one, we just
52233444Sbostic 	 * defer the ONLINE until someone tries to use the drive.
52333444Sbostic 	 *
52433444Sbostic 	 * THIS ASSUMES THAT DRIVE TYPES ?X? ARE FLOPPIES
52533444Sbostic 	 */
52633444Sbostic 	if (MSCP_MID_ECH(1, ra_info[unit].ra_mediaid) == 'X' - '@') {
52734283Skarels 		printf(": floppy");
52833444Sbostic 		return;
52933444Sbostic 	}
530*34649Skarels 	if (ui->ui_dk >= 0)
53132523Sbostic 		dk_mspw[ui->ui_dk] = 1.0 / (60 * 31 * 256);	/* approx */
53232523Sbostic 	udaip[ui->ui_ctlr][ui->ui_slave] = ui;
53333195Sbostic 
53432523Sbostic 	if (uda_rainit(ui, 0))
53534283Skarels 		printf(": offline");
53632523Sbostic 	else {
53734283Skarels 		printf(": %s, size = %d sectors",
53834283Skarels 		    udalabel[unit].d_typename, ra_info[unit].ra_dsize);
53932523Sbostic #ifdef notyet
54032523Sbostic 		addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]);
54126295Skarels #endif
54230536Skarels 	}
54332523Sbostic }
54432523Sbostic 
54532523Sbostic /*
54632523Sbostic  * Initialise a UDA50.  Return true iff something goes wrong.
54732523Sbostic  */
54832523Sbostic udainit(ctlr)
54932523Sbostic 	int ctlr;
55032523Sbostic {
55132523Sbostic 	register struct uda_softc *sc;
55232523Sbostic 	register struct udadevice *udaddr;
55332523Sbostic 	struct uba_ctlr *um;
55432523Sbostic 	int timo, ubinfo;
55532523Sbostic 
55632523Sbostic 	sc = &uda_softc[ctlr];
55732523Sbostic 	um = udaminfo[ctlr];
55832523Sbostic 	if ((sc->sc_flags & SC_MAPPED) == 0) {
55932523Sbostic 		/*
56032523Sbostic 		 * Map the communication area and command and
56132523Sbostic 		 * response packets into Unibus space.
56232523Sbostic 		 */
56332523Sbostic 		ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr],
56432523Sbostic 			sizeof (struct uda), UBA_CANTWAIT);
56532523Sbostic 		if (ubinfo == 0) {
56632523Sbostic 			printf("uda%d: uballoc map failed\n", ctlr);
56732523Sbostic 			return (-1);
56832523Sbostic 		}
56932523Sbostic 		sc->sc_uda = (struct uda *) (ubinfo & 0x3ffff);
57032523Sbostic 		sc->sc_flags |= SC_MAPPED;
57132523Sbostic 	}
57232523Sbostic 
57330536Skarels 	/*
57432523Sbostic 	 * While we are thinking about it, reset the next command
57532523Sbostic 	 * and response indicies.
57630536Skarels 	 */
57732523Sbostic 	sc->sc_mi.mi_cmd.mri_next = 0;
57832523Sbostic 	sc->sc_mi.mi_rsp.mri_next = 0;
57932523Sbostic 
58032523Sbostic 	/*
58132523Sbostic 	 * Start up the hardware initialisation sequence.
58232523Sbostic 	 */
58332523Sbostic #define	STEP0MASK	(UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \
58432523Sbostic 			 UDA_STEP1 | UDA_NV)
58532523Sbostic 
58632523Sbostic 	sc->sc_state = ST_IDLE;	/* in case init fails */
58733444Sbostic 	udaddr = (struct udadevice *)um->um_addr;
58832523Sbostic 	udaddr->udaip = 0;
58932523Sbostic 	timo = todr() + 1000;
59032523Sbostic 	while ((udaddr->udasa & STEP0MASK) == 0) {
59132523Sbostic 		if (todr() > timo) {
59232523Sbostic 			printf("uda%d: timeout during init\n", ctlr);
59332523Sbostic 			return (-1);
59432523Sbostic 		}
59532523Sbostic 	}
59632523Sbostic 	if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) {
59732523Sbostic 		printf("uda%d: init failed, sa=%b\n", ctlr,
59832523Sbostic 			udaddr->udasa, udasr_bits);
59933444Sbostic 		udasaerror(um, 0);
60032523Sbostic 		return (-1);
60132523Sbostic 	}
60232523Sbostic 
60332523Sbostic 	/*
60432523Sbostic 	 * Success!  Record new state, and start step 1 initialisation.
60532523Sbostic 	 * The rest is done in the interrupt handler.
60632523Sbostic 	 */
60732523Sbostic 	sc->sc_state = ST_STEP1;
60832523Sbostic 	udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE |
60932523Sbostic 	    (sc->sc_ivec >> 2);
61032523Sbostic 	return (0);
6114743Swnj }
6124743Swnj 
6134743Swnj /*
61432523Sbostic  * Open a drive.
6154743Swnj  */
61632523Sbostic /*ARGSUSED*/
61732523Sbostic udaopen(dev, flag, fmt)
6184743Swnj 	dev_t dev;
61930773Skarels 	int flag, fmt;
6204743Swnj {
62132523Sbostic 	register int unit;
6224743Swnj 	register struct uba_device *ui;
6234743Swnj 	register struct uda_softc *sc;
62430536Skarels 	register struct disklabel *lp;
62530536Skarels 	register struct partition *pp;
62630916Skarels 	register struct ra_info *ra;
62732523Sbostic 	int s, i, part, mask, error = 0;
62830536Skarels 	daddr_t start, end;
62930536Skarels 
63032523Sbostic 	/*
63132523Sbostic 	 * Make sure this is a reasonable open request.
63232523Sbostic 	 */
63332523Sbostic 	unit = udaunit(dev);
63432523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0)
6358576Sroot 		return (ENXIO);
63632523Sbostic 
63732523Sbostic 	/*
63832523Sbostic 	 * Make sure the controller is running, by (re)initialising it if
63932523Sbostic 	 * necessary.
64032523Sbostic 	 */
6414743Swnj 	sc = &uda_softc[ui->ui_ctlr];
6425434Sroot 	s = spl5();
64332523Sbostic 	if (sc->sc_state != ST_RUN) {
64432523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) {
64532523Sbostic 			splx(s);
6468576Sroot 			return (EIO);
64717553Skarels 		}
64832523Sbostic 		/*
64932523Sbostic 		 * In case it does not come up, make sure we will be
65032523Sbostic 		 * restarted in 10 seconds.  This corresponds to the
65132523Sbostic 		 * 10 second timeouts in udaprobe() and udaslave().
65232523Sbostic 		 */
65332523Sbostic 		sc->sc_flags |= SC_DOWAKE;
65432523Sbostic 		timeout(wakeup, (caddr_t) sc, 10 * hz);
65532523Sbostic 		sleep((caddr_t) sc, PRIBIO);
65632523Sbostic 		if (sc->sc_state != ST_RUN) {
65732523Sbostic 			splx(s);
65832523Sbostic 			printf("uda%d: controller hung\n", ui->ui_ctlr);
65932523Sbostic 			return (EIO);
66032523Sbostic 		}
66132523Sbostic 		untimeout(wakeup, (caddr_t) sc);
6624743Swnj 	}
66332523Sbostic 
66432523Sbostic 	/*
66532523Sbostic 	 * Wait for the state to settle
66632523Sbostic 	 */
66732523Sbostic 	ra = &ra_info[unit];
66832523Sbostic 	while (ra->ra_state != OPEN && ra->ra_state != OPENRAW &&
66932523Sbostic 	    ra->ra_state != CLOSED)
67032523Sbostic 		sleep((caddr_t)ra, PZERO + 1);
67132523Sbostic 
67232523Sbostic 	/*
67332523Sbostic 	 * If not on line, or we are not sure of the label, reinitialise
67432523Sbostic 	 * the drive.
67532523Sbostic 	 */
67632523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0 ||
67732523Sbostic 	    (ra->ra_state != OPEN && ra->ra_state != OPENRAW))
67832523Sbostic 		error = uda_rainit(ui, flag);
67930916Skarels 	splx(s);
68032523Sbostic 	if (error)
68132523Sbostic 		return (error);
68230536Skarels 
68332523Sbostic 	part = udapart(dev);
68432523Sbostic 	lp = &udalabel[unit];
68530536Skarels 	if (part >= lp->d_npartitions)
68630536Skarels 		return (ENXIO);
68730536Skarels 	/*
68832523Sbostic 	 * Warn if a partition is opened that overlaps another
68932523Sbostic 	 * already open, unless either is the `raw' partition
69032523Sbostic 	 * (whole disk).
69130536Skarels 	 */
69232523Sbostic #define	RAWPART		2	/* 'c' partition */	/* XXX */
69332523Sbostic 	mask = 1 << part;
69432523Sbostic 	if ((ra->ra_openpart & mask) == 0 && part != RAWPART) {
69530536Skarels 		pp = &lp->d_partitions[part];
69630536Skarels 		start = pp->p_offset;
69730536Skarels 		end = pp->p_offset + pp->p_size;
69832523Sbostic 		for (pp = lp->d_partitions, i = 0;
69932523Sbostic 		     i < lp->d_npartitions; pp++, i++) {
70030536Skarels 			if (pp->p_offset + pp->p_size <= start ||
70132523Sbostic 			    pp->p_offset >= end || i == RAWPART)
70230536Skarels 				continue;
70332523Sbostic 			if (ra->ra_openpart & (1 << i))
70430536Skarels 				log(LOG_WARNING,
70530536Skarels 				    "ra%d%c: overlaps open partition (%c)\n",
70632523Sbostic 				    unit, part + 'a', i + 'a');
70717553Skarels 		}
70817553Skarels 	}
70930773Skarels 	switch (fmt) {
71030773Skarels 	case S_IFCHR:
71132523Sbostic 		ra->ra_copenpart |= mask;
71230773Skarels 		break;
71330773Skarels 	case S_IFBLK:
71432523Sbostic 		ra->ra_bopenpart |= mask;
71530773Skarels 		break;
71630773Skarels 	}
71732523Sbostic 	ra->ra_openpart |= mask;
7188576Sroot 	return (0);
7194743Swnj }
7204743Swnj 
72133444Sbostic /* ARGSUSED */
72232523Sbostic udaclose(dev, flags, fmt)
72330536Skarels 	dev_t dev;
72430773Skarels 	int flags, fmt;
72530536Skarels {
72632523Sbostic 	register int unit = udaunit(dev);
72730773Skarels 	register struct ra_info *ra = &ra_info[unit];
72832523Sbostic 	int s, mask = (1 << udapart(dev));
72930536Skarels 
73030773Skarels 	switch (fmt) {
73130773Skarels 	case S_IFCHR:
73232523Sbostic 		ra->ra_copenpart &= ~mask;
73330773Skarels 		break;
73430773Skarels 	case S_IFBLK:
73532523Sbostic 		ra->ra_bopenpart &= ~mask;
73630773Skarels 		break;
73730773Skarels 	}
73832523Sbostic 	ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
73932523Sbostic 
74030536Skarels 	/*
74132523Sbostic 	 * Should wait for I/O to complete on this partition even if
74232523Sbostic 	 * others are open, but wait for work on blkflush().
74330536Skarels 	 */
74432523Sbostic 	if (ra->ra_openpart == 0) {
74530536Skarels 		s = spl5();
74632523Sbostic 		while (udautab[unit].b_actf)
74732523Sbostic 			sleep((caddr_t)&udautab[unit], PZERO - 1);
74830536Skarels 		splx(s);
74932523Sbostic 		ra->ra_state = CLOSED;
75033443Skarels 		ra->ra_wlabel = 0;
75130536Skarels 	}
75230773Skarels 	return (0);
75330536Skarels }
75430536Skarels 
7554743Swnj /*
75632523Sbostic  * Initialise a drive.  If it is not already, bring it on line,
75732523Sbostic  * and set a timeout on it in case it fails to respond.
75832523Sbostic  * When on line, read in the pack label.
7594743Swnj  */
76032523Sbostic uda_rainit(ui, flags)
76130536Skarels 	register struct uba_device *ui;
76232523Sbostic 	int flags;
76330536Skarels {
76432523Sbostic 	register struct uda_softc *sc = &uda_softc[ui->ui_ctlr];
76534283Skarels 	register struct disklabel *lp;
76630536Skarels 	register struct mscp *mp;
76732523Sbostic 	register int unit = ui->ui_unit;
76832523Sbostic 	register struct ra_info *ra;
76930773Skarels 	char *msg, *readdisklabel();
77032523Sbostic 	int s, i, udastrategy();
77130536Skarels 	extern int cold;
77230536Skarels 
77332523Sbostic 	ra = &ra_info[unit];
77432523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
77532523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT);
77632523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
77732523Sbostic 		mp->mscp_unit = ui->ui_slave;
77832523Sbostic 		mp->mscp_cmdref = (long)&ui->ui_flags;
77932523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
78032523Sbostic 		ra->ra_state = WANTOPEN;
78132523Sbostic 		if (!cold)
78232523Sbostic 			s = spl5();
78332523Sbostic 		i = ((struct udadevice *)ui->ui_addr)->udaip;
78430536Skarels 
78530916Skarels 		if (cold) {
78632523Sbostic 			i = todr() + 1000;
78732523Sbostic 			while ((ui->ui_flags & UNIT_ONLINE) == 0)
78832523Sbostic 				if (todr() > i)
78932523Sbostic 					break;
79030916Skarels 		} else {
79132523Sbostic 			timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz);
79232523Sbostic 			sleep((caddr_t)&ui->ui_flags, PSWP + 1);
79332523Sbostic 			splx(s);
79432523Sbostic 			untimeout(wakeup, (caddr_t)&ui->ui_flags);
79530916Skarels 		}
79632523Sbostic 		if (ra->ra_state != OPENRAW) {
79732523Sbostic 			ra->ra_state = CLOSED;
79832523Sbostic 			wakeup((caddr_t)ra);
79930916Skarels 			return (EIO);
80030916Skarels 		}
80130536Skarels 	}
80230536Skarels 
80332523Sbostic 	lp = &udalabel[unit];
80430916Skarels 	lp->d_secsize = DEV_BSIZE;
80532523Sbostic 	lp->d_secperunit = ra->ra_dsize;
80630916Skarels 
80730536Skarels 	if (flags & O_NDELAY)
80830536Skarels 		return (0);
80932523Sbostic 	ra->ra_state = RDLABEL;
81030536Skarels 	/*
81132523Sbostic 	 * Set up default sizes until we have the label, or longer
81232523Sbostic 	 * if there is none.  Set secpercyl, as readdisklabel wants
81332523Sbostic 	 * to compute b_cylin (although we do not need it).
81430536Skarels 	 */
81530916Skarels 	lp->d_secpercyl = 1;
81630536Skarels 	lp->d_npartitions = 1;
81730536Skarels 	lp->d_partitions[0].p_size = lp->d_secperunit;
81830536Skarels 	lp->d_partitions[0].p_offset = 0;
81932523Sbostic 
82030536Skarels 	/*
82130536Skarels 	 * Read pack label.
82230536Skarels 	 */
82332523Sbostic 	if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) {
82434283Skarels 		if (cold)
82534283Skarels 			printf(": %s", msg);
82634283Skarels 		else
82734283Skarels 			log(LOG_ERR, "ra%d: %s\n", unit, msg);
82830536Skarels #ifdef COMPAT_42
82932523Sbostic 		if (udamaptype(unit, lp))
83032523Sbostic 			ra->ra_state = OPEN;
83130536Skarels 		else
83232523Sbostic 			ra->ra_state = OPENRAW;
83331022Skarels #else
83432523Sbostic 		ra->ra_state = OPENRAW;
83532523Sbostic 		/* uda_makefakelabel(ra, lp); */
83630536Skarels #endif
83730773Skarels 	} else
83832523Sbostic 		ra->ra_state = OPEN;
83930916Skarels 	wakeup((caddr_t)ra);
84030916Skarels 	return (0);
84130536Skarels }
84230536Skarels 
84332523Sbostic /*
84432523Sbostic  * Copy the geometry information for the given ra from a
84532523Sbostic  * GET UNIT STATUS response.  If check, see if it changed.
84632523Sbostic  */
84732523Sbostic uda_rasave(unit, mp, check)
84832523Sbostic 	int unit;
84932523Sbostic 	register struct mscp *mp;
85032523Sbostic 	int check;
85132523Sbostic {
85232523Sbostic 	register struct ra_info *ra = &ra_info[unit];
85332523Sbostic 
85434283Skarels 	if (check && ra->ra_mediaid != mp->mscp_guse.guse_mediaid) {
85533443Skarels 		printf("ra%d: changed types! was %d now %d\n", unit,
85634283Skarels 			ra->ra_mediaid, mp->mscp_guse.guse_mediaid);
85732523Sbostic 		ra->ra_state = CLOSED;	/* ??? */
85832523Sbostic 	}
85933444Sbostic 	/* ra->ra_type = mp->mscp_guse.guse_drivetype; */
86032523Sbostic 	ra->ra_mediaid = mp->mscp_guse.guse_mediaid;
86132523Sbostic 	ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt;
86232523Sbostic 	ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group;
86332523Sbostic 	ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc;
86432523Sbostic 	ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc;
86532523Sbostic 	/* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */
86632523Sbostic #ifdef notyet
86732523Sbostic 	ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize;
86832523Sbostic 	ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt;
86932523Sbostic 	ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct;
87032523Sbostic #endif
87132523Sbostic }
87232523Sbostic 
87332523Sbostic /*
87432523Sbostic  * Queue a transfer request, and if possible, hand it to the controller.
87532523Sbostic  *
87632523Sbostic  * This routine is broken into two so that the internal version
87732523Sbostic  * udastrat1() can be called by the (nonexistent, as yet) bad block
87832523Sbostic  * revectoring routine.
87932523Sbostic  */
88032523Sbostic udastrategy(bp)
8814743Swnj 	register struct buf *bp;
8824743Swnj {
88332523Sbostic 	register int unit;
8844743Swnj 	register struct uba_device *ui;
88532523Sbostic 	register struct ra_info *ra;
88632523Sbostic 	struct partition *pp;
88732523Sbostic 	int p;
8884743Swnj 	daddr_t sz, maxsz;
8894743Swnj 
89032523Sbostic 	/*
89132523Sbostic 	 * Make sure this is a reasonable drive to use.
89232523Sbostic 	 */
89332523Sbostic 	if ((unit = udaunit(bp->b_dev)) >= NRA ||
89432523Sbostic 	    (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 ||
89532523Sbostic 	    (ra = &ra_info[unit])->ra_state == CLOSED) {
89624742Sbloom 		bp->b_error = ENXIO;
8974743Swnj 		goto bad;
89824742Sbloom 	}
89932523Sbostic 
90032523Sbostic 	/*
90132523Sbostic 	 * If drive is open `raw' or reading label, let it at it.
90232523Sbostic 	 */
90332523Sbostic 	if (ra->ra_state < OPEN) {
90432523Sbostic 		udastrat1(bp);
90532523Sbostic 		return;
90624742Sbloom 	}
90732523Sbostic 	p = udapart(bp->b_dev);
90833443Skarels 	if ((ra->ra_openpart & (1 << p)) == 0) {
90933443Skarels 		bp->b_error = ENODEV;
91033443Skarels 		goto bad;
91133443Skarels 	}
91232523Sbostic 
91332523Sbostic 	/*
91432523Sbostic 	 * Determine the size of the transfer, and make sure it is
91532523Sbostic 	 * within the boundaries of the partition.
91632523Sbostic 	 */
91732523Sbostic 	pp = &udalabel[unit].d_partitions[p];
91832523Sbostic 	maxsz = pp->p_size;
91932523Sbostic 	if (pp->p_offset + pp->p_size > ra->ra_dsize)
92032523Sbostic 		maxsz = ra->ra_dsize - pp->p_offset;
92130536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
92233443Skarels 	if (bp->b_blkno + pp->p_offset <= LABELSECTOR &&
92333443Skarels #if LABELSECTOR != 0
92433443Skarels 	    bp->b_blkno + pp->p_offset + sz > LABELSECTOR &&
92533443Skarels #endif
92633443Skarels 	    (bp->b_flags & B_READ) == 0 && ra->ra_wlabel == 0) {
92733443Skarels 		bp->b_error = EROFS;
92833443Skarels 		goto bad;
92933443Skarels 	}
93030536Skarels 	if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) {
93132523Sbostic 		/* if exactly at end of disk, return an EOF */
93224787Skarels 		if (bp->b_blkno == maxsz) {
93324787Skarels 			bp->b_resid = bp->b_bcount;
93432523Sbostic 			biodone(bp);
93532523Sbostic 			return;
93624787Skarels 		}
93732523Sbostic 		/* or truncate if part of it fits */
93830536Skarels 		sz = maxsz - bp->b_blkno;
93930536Skarels 		if (sz <= 0) {
94032523Sbostic 			bp->b_error = EINVAL;	/* or hang it up */
94130536Skarels 			goto bad;
94230536Skarels 		}
94330536Skarels 		bp->b_bcount = sz << DEV_BSHIFT;
94424742Sbloom 	}
94532523Sbostic 	udastrat1(bp);
94632523Sbostic 	return;
94732523Sbostic bad:
94832523Sbostic 	bp->b_flags |= B_ERROR;
94932523Sbostic 	biodone(bp);
95032523Sbostic }
95132523Sbostic 
95232523Sbostic /*
95332523Sbostic  * Work routine for udastrategy.
95432523Sbostic  */
95532523Sbostic udastrat1(bp)
95632523Sbostic 	register struct buf *bp;
95732523Sbostic {
95832523Sbostic 	register int unit = udaunit(bp->b_dev);
95932523Sbostic 	register struct uba_ctlr *um;
96032523Sbostic 	register struct buf *dp;
96132523Sbostic 	struct uba_device *ui;
96232523Sbostic 	int s = spl5();
96332523Sbostic 
9644743Swnj 	/*
96532523Sbostic 	 * Append the buffer to the drive queue, and if it is not
96632523Sbostic 	 * already there, the drive to the controller queue.  (However,
96732523Sbostic 	 * if the drive queue is marked to be requeued, we must be
96832523Sbostic 	 * awaiting an on line or get unit status command; in this
96932523Sbostic 	 * case, leave it off the controller queue.)
9704743Swnj 	 */
97132523Sbostic 	um = (ui = udadinfo[unit])->ui_mi;
97232523Sbostic 	dp = &udautab[unit];
97332523Sbostic 	APPEND(bp, dp, av_forw);
97432523Sbostic 	if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) {
97532523Sbostic 		APPEND(dp, &um->um_tab, b_forw);
97632523Sbostic 		dp->b_active++;
97732523Sbostic 	}
97832523Sbostic 
9794743Swnj 	/*
98032523Sbostic 	 * Start activity on the controller.  Note that unlike other
98132523Sbostic 	 * Unibus drivers, we must always do this, not just when the
98232523Sbostic 	 * controller is not active.
9834743Swnj 	 */
98432523Sbostic 	udastart(um);
9855434Sroot 	splx(s);
9864743Swnj }
9874743Swnj 
98832523Sbostic /*
98932523Sbostic  * Start up whatever transfers we can find.
99032523Sbostic  * Note that udastart() must be called at spl5().
99132523Sbostic  */
99232523Sbostic udastart(um)
9934743Swnj 	register struct uba_ctlr *um;
9944743Swnj {
99532523Sbostic 	register struct uda_softc *sc = &uda_softc[um->um_ctlr];
9964743Swnj 	register struct buf *bp, *dp;
9974743Swnj 	register struct mscp *mp;
99832523Sbostic 	struct uba_device *ui;
9994743Swnj 	struct udadevice *udaddr;
100032523Sbostic 	struct partition *pp;
100132523Sbostic 	int i, sz;
10024743Swnj 
100332523Sbostic #ifdef lint
100432523Sbostic 	i = 0; i = i;
100532523Sbostic #endif
100632523Sbostic 	/*
100732523Sbostic 	 * If it is not running, try (again and again...) to initialise
100832523Sbostic 	 * it.  If it is currently initialising just ignore it for now.
100932523Sbostic 	 */
101032523Sbostic 	if (sc->sc_state != ST_RUN) {
101132523Sbostic 		if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr))
101232523Sbostic 			printf("uda%d: still hung\n", um->um_ctlr);
101332523Sbostic 		return;
101432523Sbostic 	}
101532523Sbostic 
101632523Sbostic 	/*
101732523Sbostic 	 * If um_cmd is nonzero, this controller is on the Unibus
101832523Sbostic 	 * resource wait queue.  It will not help to try more requests;
101932523Sbostic 	 * instead, when the Unibus unblocks and calls udadgo(), we
102032523Sbostic 	 * will call udastart() again.
102132523Sbostic 	 */
102232523Sbostic 	if (um->um_cmd)
102332523Sbostic 		return;
102432523Sbostic 
102532523Sbostic 	sc->sc_flags |= SC_INSTART;
102632523Sbostic 	udaddr = (struct udadevice *) um->um_addr;
102732523Sbostic 
10284743Swnj loop:
102932523Sbostic 	/*
103032523Sbostic 	 * Service the drive at the head of the queue.  It may not
103132523Sbostic 	 * need anything, in which case it might be shutting down
103232523Sbostic 	 * in udaclose().
103332523Sbostic 	 */
103432523Sbostic 	if ((dp = um->um_tab.b_actf) == NULL)
103532523Sbostic 		goto out;
10364743Swnj 	if ((bp = dp->b_actf) == NULL) {
10374743Swnj 		dp->b_active = 0;
10384743Swnj 		um->um_tab.b_actf = dp->b_forw;
103932523Sbostic 		if (ra_info[dp - udautab].ra_openpart == 0)
104032523Sbostic 			wakeup((caddr_t)dp); /* finish close protocol */
104132523Sbostic 		goto loop;
10424743Swnj 	}
104332523Sbostic 
104432523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
104533444Sbostic 		udasaerror(um, 1);
104632523Sbostic 		goto out;
10474743Swnj 	}
104832523Sbostic 
104932523Sbostic 	/*
105032523Sbostic 	 * Get an MSCP packet, then figure out what to do.  If
105132523Sbostic 	 * we cannot get a command packet, the command ring may
105232523Sbostic 	 * be too small:  We should have at least as many command
105332523Sbostic 	 * packets as credits, for best performance.
105432523Sbostic 	 */
105532523Sbostic 	if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) {
105632523Sbostic 		if (sc->sc_mi.mi_credits > MSCP_MINCREDITS &&
105732523Sbostic 		    (sc->sc_flags & SC_GRIPED) == 0) {
105832523Sbostic 			log(LOG_NOTICE, "uda%d: command ring too small\n",
105932523Sbostic 				um->um_ctlr);
106032523Sbostic 			sc->sc_flags |= SC_GRIPED;/* complain only once */
106117553Skarels 		}
106232523Sbostic 		goto out;
10634743Swnj 	}
10644743Swnj 
106532523Sbostic 	/*
106632523Sbostic 	 * Bring the drive on line if it is not already.  Get its status
106732523Sbostic 	 * if we do not already have it.  Otherwise just start the transfer.
106832523Sbostic 	 */
106932523Sbostic 	ui = udadinfo[udaunit(bp->b_dev)];
107032523Sbostic 	if ((ui->ui_flags & UNIT_ONLINE) == 0) {
107132523Sbostic 		mp->mscp_opcode = M_OP_ONLINE;
107232523Sbostic 		goto common;
10734743Swnj 	}
107432523Sbostic 	if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) {
107532523Sbostic 		mp->mscp_opcode = M_OP_GETUNITST;
107632523Sbostic common:
107732523Sbostic if (ui->ui_flags & UNIT_REQUEUE) panic("udastart");
107832523Sbostic 		/*
107932523Sbostic 		 * Take the drive off the controller queue.  When the
108032523Sbostic 		 * command finishes, make sure the drive is requeued.
108132523Sbostic 		 */
108232523Sbostic 		um->um_tab.b_actf = dp->b_forw;
108332523Sbostic 		dp->b_active = 0;
108432523Sbostic 		ui->ui_flags |= UNIT_REQUEUE;
108532523Sbostic 		mp->mscp_unit = ui->ui_slave;
108632523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
108732523Sbostic 		sc->sc_flags |= SC_STARTPOLL;
108832523Sbostic #ifdef POLLSTATS
108932523Sbostic 		sc->sc_ncmd++;
109025653Skarels #endif
109132523Sbostic 		goto loop;
109217553Skarels 	}
109332523Sbostic 
109432523Sbostic 	pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)];
109532523Sbostic 	mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE;
10964743Swnj 	mp->mscp_unit = ui->ui_slave;
109732523Sbostic 	mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset;
109830536Skarels 	sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT;
109932523Sbostic 	mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ?
110032523Sbostic 		(pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount;
110132523Sbostic 	/* mscp_cmdref is filled in by mscp_go() */
11024743Swnj 
11034743Swnj 	/*
110432523Sbostic 	 * Drop the packet pointer into the `command' field so udadgo()
110532523Sbostic 	 * can tell what to start.  If ubago returns 1, we can do another
110632523Sbostic 	 * transfer.  If not, um_cmd will still point at mp, so we will
110732523Sbostic 	 * know that we are waiting for resources.
11084743Swnj 	 */
110932523Sbostic 	um->um_cmd = (int)mp;
111032523Sbostic 	if (ubago(ui))
111132523Sbostic 		goto loop;
111232523Sbostic 
111332523Sbostic 	/*
111432523Sbostic 	 * All done, or blocked in ubago().  If we managed to
111532523Sbostic 	 * issue some commands, start up the beast.
111632523Sbostic 	 */
111732523Sbostic out:
111832523Sbostic 	if (sc->sc_flags & SC_STARTPOLL) {
111932523Sbostic #ifdef POLLSTATS
112032523Sbostic 		udastats.cmd[sc->sc_ncmd]++;
112132523Sbostic 		sc->sc_ncmd = 0;
112232523Sbostic #endif
112333444Sbostic 		i = ((struct udadevice *)um->um_addr)->udaip;
11244743Swnj 	}
112532523Sbostic 	sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL);
112632523Sbostic }
112732523Sbostic 
112832523Sbostic /*
112932523Sbostic  * Start a transfer.
113032523Sbostic  *
113132523Sbostic  * If we are not called from within udastart(), we must have been
113232523Sbostic  * blocked, so call udastart to do more requests (if any).  If
113332523Sbostic  * this calls us again immediately we will not recurse, because
113432523Sbostic  * that time we will be in udastart().  Clever....
113532523Sbostic  */
113632523Sbostic udadgo(um)
113732523Sbostic 	register struct uba_ctlr *um;
113832523Sbostic {
113932523Sbostic 	struct uda_softc *sc = &uda_softc[um->um_ctlr];
114032523Sbostic 	struct mscp *mp = (struct mscp *)um->um_cmd;
114132523Sbostic 
114232523Sbostic 	um->um_tab.b_active++;	/* another transfer going */
114332523Sbostic 
11444743Swnj 	/*
114532523Sbostic 	 * Fill in the MSCP packet and move the buffer to the
114632523Sbostic 	 * I/O wait queue.  Mark the controller as no longer on
114732523Sbostic 	 * the resource queue, and remember to initiate polling.
11484743Swnj 	 */
114932523Sbostic 	mp->mscp_seq.seq_buffer = (um->um_ubinfo & 0x3ffff) |
115032523Sbostic 		(UBAI_BDP(um->um_ubinfo) << 24);
115132523Sbostic 	mscp_go(&sc->sc_mi, mp, um->um_ubinfo);
115232523Sbostic 	um->um_cmd = 0;
115332523Sbostic 	um->um_ubinfo = 0;	/* tyke it awye */
115432523Sbostic 	sc->sc_flags |= SC_STARTPOLL;
115532523Sbostic #ifdef POLLSTATS
115632523Sbostic 	sc->sc_ncmd++;
115732523Sbostic #endif
115832523Sbostic 	if ((sc->sc_flags & SC_INSTART) == 0)
115932523Sbostic 		udastart(um);
11604743Swnj }
11614743Swnj 
116232523Sbostic udaiodone(mi, bp, info)
116332523Sbostic 	register struct mscp_info *mi;
116432523Sbostic 	struct buf *bp;
116532523Sbostic 	int info;
116632523Sbostic {
116732523Sbostic 	register struct uba_ctlr *um = udaminfo[mi->mi_ctlr];
116832523Sbostic 
116932523Sbostic 	um->um_ubinfo = info;
117032523Sbostic 	ubadone(um);
117132523Sbostic 	biodone(bp);
117232523Sbostic 	if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab)
117332523Sbostic 		ubarelse(um->um_ubanum, &um->um_bdp);
117432523Sbostic 	um->um_tab.b_active--;	/* another transfer done */
117532523Sbostic }
117632523Sbostic 
117733444Sbostic static struct saerr {
117833444Sbostic 	int	code;		/* error code (including UDA_ERR) */
117933444Sbostic 	char	*desc;		/* what it means: Efoo => foo error */
118033444Sbostic } saerr[] = {
118133444Sbostic 	{ 0100001, "Eunibus packet read" },
118233444Sbostic 	{ 0100002, "Eunibus packet write" },
118333444Sbostic 	{ 0100003, "EUDA ROM and RAM parity" },
118433444Sbostic 	{ 0100004, "EUDA RAM parity" },
118533444Sbostic 	{ 0100005, "EUDA ROM parity" },
118633444Sbostic 	{ 0100006, "Eunibus ring read" },
118733444Sbostic 	{ 0100007, "Eunibus ring write" },
118833444Sbostic 	{ 0100010, " unibus interrupt master failure" },
118933444Sbostic 	{ 0100011, "Ehost access timeout" },
119033444Sbostic 	{ 0100012, " host exceeded command limit" },
119133444Sbostic 	{ 0100013, " unibus bus master failure" },
119233444Sbostic 	{ 0100014, " DM XFC fatal error" },
119333444Sbostic 	{ 0100015, " hardware timeout of instruction loop" },
119433444Sbostic 	{ 0100016, " invalid virtual circuit id" },
119533444Sbostic 	{ 0100017, "Eunibus interrupt write" },
119633444Sbostic 	{ 0104000, "Efatal sequence" },
119733444Sbostic 	{ 0104040, " D proc ALU" },
119833444Sbostic 	{ 0104041, "ED proc control ROM parity" },
119933444Sbostic 	{ 0105102, "ED proc w/no BD#2 or RAM parity" },
120033444Sbostic 	{ 0105105, "ED proc RAM buffer" },
120133444Sbostic 	{ 0105152, "ED proc SDI" },
120233444Sbostic 	{ 0105153, "ED proc write mode wrap serdes" },
120333444Sbostic 	{ 0105154, "ED proc read mode serdes, RSGEN & ECC" },
120433444Sbostic 	{ 0106040, "EU proc ALU" },
120533444Sbostic 	{ 0106041, "EU proc control reg" },
120633444Sbostic 	{ 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" },
120733444Sbostic 	{ 0106047, " U proc const PROM err w/D proc running SDI test" },
120833444Sbostic 	{ 0106055, " unexpected trap" },
120933444Sbostic 	{ 0106071, "EU proc const PROM" },
121033444Sbostic 	{ 0106072, "EU proc control ROM parity" },
121133444Sbostic 	{ 0106200, "Estep 1 data" },
121233444Sbostic 	{ 0107103, "EU proc RAM parity" },
121333444Sbostic 	{ 0107107, "EU proc RAM buffer" },
121433444Sbostic 	{ 0107115, " test count wrong (BD 12)" },
121533444Sbostic 	{ 0112300, "Estep 2" },
121633444Sbostic 	{ 0122240, "ENPR" },
121733444Sbostic 	{ 0122300, "Estep 3" },
121833444Sbostic 	{ 0142300, "Estep 4" },
121933444Sbostic 	{ 0, " unknown error code" }
122033444Sbostic };
122133444Sbostic 
12224743Swnj /*
122333444Sbostic  * If the error bit was set in the controller status register, gripe,
122433444Sbostic  * then (optionally) reset the controller and requeue pending transfers.
12254743Swnj  */
122633444Sbostic udasaerror(um, doreset)
122732523Sbostic 	register struct uba_ctlr *um;
122833444Sbostic 	int doreset;
12294743Swnj {
123033444Sbostic 	register int code = ((struct udadevice *)um->um_addr)->udasa;
123133444Sbostic 	register struct saerr *e;
123232523Sbostic 
123333444Sbostic 	if ((code & UDA_ERR) == 0)
123433444Sbostic 		return;
123533444Sbostic 	for (e = saerr; e->code; e++)
123633444Sbostic 		if (e->code == code)
123733444Sbostic 			break;
123833444Sbostic 	printf("uda%d: controller error, sa=0%o (%s%s)\n",
123933444Sbostic 		um->um_ctlr, code, e->desc + 1,
124033444Sbostic 		*e->desc == 'E' ? " error" : "");
124133444Sbostic 	if (doreset) {
124233444Sbostic 		mscp_requeue(&uda_softc[um->um_ctlr].sc_mi);
124333444Sbostic 		(void) udainit(um->um_ctlr);
124433444Sbostic 	}
124532523Sbostic }
124632523Sbostic 
124732523Sbostic /*
124832523Sbostic  * Interrupt routine.  Depending on the state of the controller,
124932523Sbostic  * continue initialisation, or acknowledge command and response
125032523Sbostic  * interrupts, and process responses.
125132523Sbostic  */
125232523Sbostic udaintr(ctlr)
125332523Sbostic 	int ctlr;
125432523Sbostic {
125532523Sbostic 	register struct uba_ctlr *um = udaminfo[ctlr];
125632523Sbostic 	register struct uda_softc *sc = &uda_softc[ctlr];
125733444Sbostic 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
125832523Sbostic 	register struct uda *ud;
125932523Sbostic 	register struct mscp *mp;
12604743Swnj 	register int i;
12614743Swnj 
126227254Skridle #ifdef VAX630
126332523Sbostic 	(void) spl5();		/* Qbus interrupt protocol is odd */
126427254Skridle #endif
126532523Sbostic 	sc->sc_wticks = 0;	/* reset interrupt watchdog */
126632523Sbostic 
126732523Sbostic 	/*
126832523Sbostic 	 * Combinations during steps 1, 2, and 3: STEPnMASK
126932523Sbostic 	 * corresponds to which bits should be tested;
127032523Sbostic 	 * STEPnGOOD corresponds to the pattern that should
127132523Sbostic 	 * appear after the interrupt from STEPn initialisation.
127232523Sbostic 	 * All steps test the bits in ALLSTEPS.
127332523Sbostic 	 */
127432523Sbostic #define	ALLSTEPS	(UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1)
127532523Sbostic 
127632523Sbostic #define	STEP1MASK	(ALLSTEPS | UDA_IE | UDA_NCNRMASK)
127732523Sbostic #define	STEP1GOOD	(UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2)
127832523Sbostic 
127932523Sbostic #define	STEP2MASK	(ALLSTEPS | UDA_IE | UDA_IVECMASK)
128032523Sbostic #define	STEP2GOOD	(UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2))
128132523Sbostic 
128232523Sbostic #define	STEP3MASK	ALLSTEPS
128332523Sbostic #define	STEP3GOOD	UDA_STEP4
128432523Sbostic 
12854743Swnj 	switch (sc->sc_state) {
128632523Sbostic 
128732523Sbostic 	case ST_IDLE:
128832523Sbostic 		/*
128932523Sbostic 		 * Ignore unsolicited interrupts.
129032523Sbostic 		 */
129132523Sbostic 		log(LOG_WARNING, "uda%d: stray intr\n", ctlr);
12924743Swnj 		return;
12934743Swnj 
129432523Sbostic 	case ST_STEP1:
129532523Sbostic 		/*
129632523Sbostic 		 * Begin step two initialisation.
129732523Sbostic 		 */
129832523Sbostic 		if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) {
129932523Sbostic 			i = 1;
130032523Sbostic initfailed:
130132523Sbostic 			printf("uda%d: init step %d failed, sa=%b\n",
130232523Sbostic 				ctlr, i, udaddr->udasa, udasr_bits);
130333444Sbostic 			udasaerror(um, 0);
130432523Sbostic 			sc->sc_state = ST_IDLE;
130532523Sbostic 			if (sc->sc_flags & SC_DOWAKE) {
130632523Sbostic 				sc->sc_flags &= ~SC_DOWAKE;
130733444Sbostic 				wakeup((caddr_t)sc);
130832523Sbostic 			}
13094743Swnj 			return;
13104743Swnj 		}
131133444Sbostic 		udaddr->udasa = (int)&sc->sc_uda->uda_ca.ca_rspdsc[0] |
131232523Sbostic 			(cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0);
131332523Sbostic 		sc->sc_state = ST_STEP2;
13144743Swnj 		return;
13154743Swnj 
131632523Sbostic 	case ST_STEP2:
131732523Sbostic 		/*
131832523Sbostic 		 * Begin step 3 initialisation.
131932523Sbostic 		 */
132032523Sbostic 		if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) {
132132523Sbostic 			i = 2;
132232523Sbostic 			goto initfailed;
13234743Swnj 		}
132433444Sbostic 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16;
132532523Sbostic 		sc->sc_state = ST_STEP3;
13264743Swnj 		return;
13274743Swnj 
132832523Sbostic 	case ST_STEP3:
132932523Sbostic 		/*
133032523Sbostic 		 * Set controller characteristics (finish initialisation).
133132523Sbostic 		 */
133232523Sbostic 		if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) {
133332523Sbostic 			i = 3;
133432523Sbostic 			goto initfailed;
13354743Swnj 		}
133632523Sbostic 		i = udaddr->udasa & 0xff;
133732523Sbostic 		if (i != sc->sc_micro) {
133832523Sbostic 			sc->sc_micro = i;
133932523Sbostic 			printf("uda%d: version %d model %d\n",
134032523Sbostic 				ctlr, i & 0xf, i >> 4);
134132523Sbostic 		}
134232523Sbostic 
134317553Skarels 		/*
134432523Sbostic 		 * Present the burst size, then remove it.  Why this
134532523Sbostic 		 * should be done this way, I have no idea.
134632523Sbostic 		 *
134732523Sbostic 		 * Note that this assumes udaburst[ctlr] > 0.
134817553Skarels 		 */
134932523Sbostic 		udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2;
13504743Swnj 		udaddr->udasa = UDA_GO;
135132523Sbostic 		printf("uda%d: DMA burst size set to %d\n",
135232523Sbostic 			ctlr, udaburst[ctlr]);
13534743Swnj 
135432523Sbostic 		udainitds(ctlr);	/* initialise data structures */
135532523Sbostic 
13564743Swnj 		/*
135732523Sbostic 		 * Before we can get a command packet, we need some
135832523Sbostic 		 * credits.  Fake some up to keep mscp_getcp() happy,
135932523Sbostic 		 * get a packet, and cancel all credits (the right
136032523Sbostic 		 * number should come back in the response to the
136132523Sbostic 		 * SCC packet).
13624743Swnj 		 */
136332523Sbostic 		sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1;
136432523Sbostic 		mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT);
136532523Sbostic 		if (mp == NULL)	/* `cannot happen' */
136632523Sbostic 			panic("udaintr");
136732523Sbostic 		sc->sc_mi.mi_credits = 0;
136832523Sbostic 		mp->mscp_opcode = M_OP_SETCTLRC;
136932523Sbostic 		mp->mscp_unit = 0;
137032523Sbostic 		mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC |
137132523Sbostic 			M_CF_THIS;
137232523Sbostic 		*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
137332523Sbostic 		i = udaddr->udaip;
137432523Sbostic 		sc->sc_state = ST_SETCHAR;
13754743Swnj 		return;
13764743Swnj 
137732523Sbostic 	case ST_SETCHAR:
137832523Sbostic 	case ST_RUN:
137932523Sbostic 		/*
138032523Sbostic 		 * Handle Set Ctlr Characteristics responses and operational
138132523Sbostic 		 * responses (via mscp_dorsp).
138232523Sbostic 		 */
13834743Swnj 		break;
13844743Swnj 
13854743Swnj 	default:
138632523Sbostic 		printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state);
138732523Sbostic 		panic("udastate");
13884743Swnj 	}
13894743Swnj 
139032523Sbostic 	if (udaddr->udasa & UDA_ERR) {	/* ctlr fatal error */
139133444Sbostic 		udasaerror(um, 1);
139232523Sbostic 		return;
13934743Swnj 	}
13944743Swnj 
139532523Sbostic 	ud = &uda[ctlr];
139632523Sbostic 
13974743Swnj 	/*
139832523Sbostic 	 * Handle buffer purge requests.
13994743Swnj 	 */
14004743Swnj 	if (ud->uda_ca.ca_bdp) {
140126372Skarels 		UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp);
14024743Swnj 		ud->uda_ca.ca_bdp = 0;
140332523Sbostic 		udaddr->udasa = 0;	/* signal purge complete */
14044743Swnj 	}
14054743Swnj 
14064743Swnj 	/*
140732523Sbostic 	 * Check for response and command ring transitions.
14084743Swnj 	 */
14094743Swnj 	if (ud->uda_ca.ca_rspint) {
14104743Swnj 		ud->uda_ca.ca_rspint = 0;
141132523Sbostic 		mscp_dorsp(&sc->sc_mi);
14124743Swnj 	}
14134743Swnj 	if (ud->uda_ca.ca_cmdint) {
14144743Swnj 		ud->uda_ca.ca_cmdint = 0;
141532523Sbostic 		MSCP_DOCMD(&sc->sc_mi);
14164743Swnj 	}
141732523Sbostic 	udastart(um);
14184743Swnj }
14194743Swnj 
14204743Swnj /*
142132523Sbostic  * Initialise the various data structures that control the UDA50.
142217553Skarels  */
142332523Sbostic udainitds(ctlr)
142432523Sbostic 	int ctlr;
142532523Sbostic {
142632523Sbostic 	register struct uda *ud = &uda[ctlr];
142732523Sbostic 	register struct uda *uud = uda_softc[ctlr].sc_uda;
142832523Sbostic 	register struct mscp *mp;
142932523Sbostic 	register int i;
14304743Swnj 
143132523Sbostic 	for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) {
143232523Sbostic 		ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT |
143332523Sbostic 			(long)&uud->uda_rsp[i].mscp_cmdref;
143432523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i];
143532523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
14364743Swnj 	}
143732523Sbostic 	for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) {
143832523Sbostic 		ud->uda_ca.ca_cmddsc[i] = MSCP_INT |
143932523Sbostic 			(long)&uud->uda_cmd[i].mscp_cmdref;
144032523Sbostic 		mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i];
144132523Sbostic 		mp->mscp_msglen = MSCP_MSGLEN;
144232523Sbostic 	}
14434743Swnj }
14444743Swnj 
14454743Swnj /*
144633444Sbostic  * Handle an error datagram.
14474743Swnj  */
144832523Sbostic udadgram(mi, mp)
144932523Sbostic 	struct mscp_info *mi;
145032523Sbostic 	struct mscp *mp;
14514743Swnj {
145217553Skarels 
145332523Sbostic 	mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp);
145433444Sbostic 	/*
145533444Sbostic 	 * SDI status information bytes 10 and 11 are the microprocessor
145633444Sbostic 	 * error code and front panel code respectively.  These vary per
145733444Sbostic 	 * drive type and are printed purely for field service information.
145833444Sbostic 	 */
145933444Sbostic 	if (mp->mscp_format == M_FM_SDI)
146033444Sbostic 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
146133444Sbostic 			mp->mscp_erd.erd_sdistat[10],
146233444Sbostic 			mp->mscp_erd.erd_sdistat[11]);
146332523Sbostic }
146417553Skarels 
146532523Sbostic /*
146632523Sbostic  * The Set Controller Characteristics command finished.
146732523Sbostic  * Record the new state of the controller.
146832523Sbostic  */
146932523Sbostic udactlrdone(mi, mp)
147032523Sbostic 	register struct mscp_info *mi;
147132523Sbostic 	struct mscp *mp;
147232523Sbostic {
147332523Sbostic 	register struct uda_softc *sc = &uda_softc[mi->mi_ctlr];
147417553Skarels 
147532523Sbostic 	if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS)
147632523Sbostic 		sc->sc_state = ST_RUN;
147732523Sbostic 	else {
147832523Sbostic 		printf("uda%d: SETCTLRC failed: ",
147932523Sbostic 			mi->mi_ctlr, mp->mscp_status);
148032523Sbostic 		mscp_printevent(mp);
148132523Sbostic 		sc->sc_state = ST_IDLE;
14824743Swnj 	}
148332523Sbostic 	if (sc->sc_flags & SC_DOWAKE) {
148432523Sbostic 		sc->sc_flags &= ~SC_DOWAKE;
148532523Sbostic 		wakeup((caddr_t)sc);
14866964Ssam 	}
14874743Swnj }
14884743Swnj 
14894743Swnj /*
149032523Sbostic  * Received a response from an as-yet unconfigured drive.  Configure it
149132523Sbostic  * in, if possible.
14924743Swnj  */
149332523Sbostic udaunconf(mi, mp)
149432523Sbostic 	struct mscp_info *mi;
149532523Sbostic 	register struct mscp *mp;
14964743Swnj {
14974743Swnj 
149817553Skarels 	/*
149932523Sbostic 	 * If it is a slave response, copy it to udaslavereply for
150032523Sbostic 	 * udaslave() to look at.
150117553Skarels 	 */
150232523Sbostic 	if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) &&
150332523Sbostic 	    (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) {
150432523Sbostic 		udaslavereply = *mp;
150532523Sbostic 		return (MSCP_DONE);
15064743Swnj 	}
150732523Sbostic 
150832523Sbostic 	/*
150932523Sbostic 	 * Otherwise, it had better be an available attention response.
151032523Sbostic 	 */
151132523Sbostic 	if (mp->mscp_opcode != M_OP_AVAILATTN)
151232523Sbostic 		return (MSCP_FAILED);
151332523Sbostic 
151432523Sbostic 	/* do what autoconf does */
151532523Sbostic 	return (MSCP_FAILED);	/* not yet, arwhite, not yet */
15164743Swnj }
15174743Swnj 
151832523Sbostic /*
151932523Sbostic  * A drive came on line.  Check its type and size.  Return DONE if
152032523Sbostic  * we think the drive is truly on line.  In any case, awaken anyone
152132523Sbostic  * sleeping on the drive on-line-ness.
152232523Sbostic  */
152332523Sbostic udaonline(ui, mp)
152432523Sbostic 	register struct uba_device *ui;
152532523Sbostic 	struct mscp *mp;
15264743Swnj {
152732523Sbostic 	register struct ra_info *ra = &ra_info[ui->ui_unit];
15284743Swnj 
152932523Sbostic 	wakeup((caddr_t)&ui->ui_flags);
153032523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
153132523Sbostic 		printf("uda%d: attempt to bring ra%d on line failed: ",
153232523Sbostic 			ui->ui_ctlr, ui->ui_unit);
153332523Sbostic 		mscp_printevent(mp);
153432523Sbostic 		ra->ra_state = CLOSED;
153532523Sbostic 		return (MSCP_FAILED);
153632523Sbostic 	}
153732523Sbostic 
153832523Sbostic 	ra->ra_state = OPENRAW;
153932523Sbostic 	ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize;
154034283Skarels 	if (!cold)
154134283Skarels 		printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit,
154234283Skarels 		    ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize);
154332523Sbostic 	/* can now compute ncyl */
154432523Sbostic 	ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks /
154532523Sbostic 		ra->ra_geom.rg_nsectors;
154632523Sbostic 	return (MSCP_DONE);
15474743Swnj }
15484743Swnj 
154932523Sbostic /*
155032523Sbostic  * We got some (configured) unit's status.  Return DONE if it succeeded.
155132523Sbostic  */
155232523Sbostic udagotstatus(ui, mp)
155332523Sbostic 	register struct uba_device *ui;
155432523Sbostic 	register struct mscp *mp;
15554743Swnj {
15564743Swnj 
155732523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
155832523Sbostic 		printf("uda%d: attempt to get status for ra%d failed: ",
155932523Sbostic 			ui->ui_ctlr, ui->ui_unit);
156032523Sbostic 		mscp_printevent(mp);
156132523Sbostic 		return (MSCP_FAILED);
156232523Sbostic 	}
156332523Sbostic 	/* record for (future) bad block forwarding and whatever else */
156432523Sbostic 	uda_rasave(ui->ui_unit, mp, 1);
156532523Sbostic 	return (MSCP_DONE);
15664743Swnj }
15674743Swnj 
156832523Sbostic /*
156932523Sbostic  * A transfer failed.  We get a chance to fix or restart it.
157032523Sbostic  * Need to write the bad block forwaring code first....
157132523Sbostic  */
157232523Sbostic /*ARGSUSED*/
157332523Sbostic udaioerror(ui, mp, bp)
157432523Sbostic 	register struct uba_device *ui;
157532523Sbostic 	register struct mscp *mp;
157632523Sbostic 	struct buf *bp;
15774743Swnj {
15784743Swnj 
157932523Sbostic 	if (mp->mscp_flags & M_EF_BBLKR) {
158032523Sbostic 		/*
158132523Sbostic 		 * A bad block report.  Eventually we will
158232523Sbostic 		 * restart this transfer, but for now, just
158332523Sbostic 		 * log it and give up.
158432523Sbostic 		 */
158532523Sbostic 		log(LOG_ERR, "ra%d: bad block report: %d%s\n",
158632523Sbostic 			ui->ui_unit, mp->mscp_seq.seq_lbn,
158732523Sbostic 			mp->mscp_flags & M_EF_BBLKU ? " + others" : "");
158832523Sbostic 	} else {
158932523Sbostic 		/*
159032523Sbostic 		 * What the heck IS a `serious exception' anyway?
159132523Sbostic 		 * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION
159232523Sbostic 		 * FOR THEIR OWN CONTROLLERS.
159332523Sbostic 		 */
159432523Sbostic 		if (mp->mscp_flags & M_EF_SEREX)
159532523Sbostic 			log(LOG_ERR, "ra%d: serious exception reported\n",
159632523Sbostic 				ui->ui_unit);
15974743Swnj 	}
159832523Sbostic 	return (MSCP_FAILED);
15994743Swnj }
16004743Swnj 
160132523Sbostic /*
160232523Sbostic  * A replace operation finished.
160332523Sbostic  */
160432523Sbostic /*ARGSUSED*/
160532523Sbostic udareplace(ui, mp)
160632523Sbostic 	struct uba_device *ui;
160732523Sbostic 	struct mscp *mp;
16084743Swnj {
160917553Skarels 
161032523Sbostic 	panic("udareplace");
16114743Swnj }
161217553Skarels 
161332523Sbostic /*
161432523Sbostic  * A bad block related operation finished.
161532523Sbostic  */
161632523Sbostic /*ARGSUSED*/
161732523Sbostic udabb(ui, mp, bp)
161832523Sbostic 	struct uba_device *ui;
161932523Sbostic 	struct mscp *mp;
162032523Sbostic 	struct buf *bp;
162117553Skarels {
162217553Skarels 
162332523Sbostic 	panic("udabb");
162417553Skarels }
162517553Skarels 
162632523Sbostic 
162732523Sbostic /*
162832523Sbostic  * I/O controls.
162932523Sbostic  */
163032523Sbostic udaioctl(dev, cmd, data, flag)
163112511Ssam 	dev_t dev;
163230536Skarels 	int cmd;
163330536Skarels 	caddr_t data;
163430536Skarels 	int flag;
163512511Ssam {
163632523Sbostic 	register int unit = udaunit(dev);
163730536Skarels 	register struct disklabel *lp;
163833443Skarels 	register struct ra_info *ra = &ra_info[unit];
163934638Skarels 	int error = 0;
164012511Ssam 
164132523Sbostic 	lp = &udalabel[unit];
164230536Skarels 
164330536Skarels 	switch (cmd) {
164430536Skarels 
164530536Skarels 	case DIOCGDINFO:
164630536Skarels 		*(struct disklabel *)data = *lp;
164730536Skarels 		break;
164830536Skarels 
164930773Skarels 	case DIOCGPART:
165030773Skarels 		((struct partinfo *)data)->disklab = lp;
165130773Skarels 		((struct partinfo *)data)->part =
165232523Sbostic 		    &lp->d_partitions[udapart(dev)];
165330536Skarels 		break;
165430536Skarels 
165530536Skarels 	case DIOCSDINFO:
165630536Skarels 		if ((flag & FWRITE) == 0)
165730536Skarels 			error = EBADF;
165830536Skarels 		else
165932574Skarels 			error = setdisklabel(lp, (struct disklabel *)data,
166034283Skarels 			    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart);
166130536Skarels 		break;
166230536Skarels 
166333443Skarels 	case DIOCWLABEL:
166433443Skarels 		if ((flag & FWRITE) == 0)
166533443Skarels 			error = EBADF;
166633443Skarels 		else
166733443Skarels 			ra->ra_wlabel = *(int *)data;
166833443Skarels 		break;
166933443Skarels 
167032574Skarels 	case DIOCWDINFO:
167132574Skarels 		if ((flag & FWRITE) == 0)
167232523Sbostic 			error = EBADF;
167332574Skarels 		else if ((error = setdisklabel(lp, (struct disklabel *)data,
167434283Skarels 		    (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart)) == 0) {
167534638Skarels 			int wlab;
167634638Skarels 
167734283Skarels 			ra->ra_state = OPEN;
167834638Skarels 			/* simulate opening partition 0 so write succeeds */
167934638Skarels 			ra->ra_openpart |= (1 << 0);		/* XXX */
168034638Skarels 			wlab = ra->ra_wlabel;
168134638Skarels 			ra->ra_wlabel = 1;
168232574Skarels 			error = writedisklabel(dev, udastrategy, lp);
168334638Skarels 			ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart;
168434638Skarels 			ra->ra_wlabel = wlab;
168534283Skarels 		}
168630536Skarels 		break;
168730536Skarels 
168832523Sbostic #ifdef notyet
168932523Sbostic 	case UDAIOCREPLACE:
169032523Sbostic 		/*
169132523Sbostic 		 * Initiate bad block replacement for the given LBN.
169232523Sbostic 		 * (Should we allow modifiers?)
169332523Sbostic 		 */
169432523Sbostic 		error = EOPNOTSUPP;
169532523Sbostic 		break;
169632523Sbostic 
169732523Sbostic 	case UDAIOCGMICRO:
169832523Sbostic 		/*
169932523Sbostic 		 * Return the microcode revision for the UDA50 running
170032523Sbostic 		 * this drive.
170132523Sbostic 		 */
170233444Sbostic 		*(int *)data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro;
170332523Sbostic 		break;
170432523Sbostic #endif
170532523Sbostic 
170630536Skarels 	default:
170730536Skarels 		error = ENOTTY;
170830536Skarels 		break;
170930536Skarels 	}
171032523Sbostic 	return (error);
171132523Sbostic }
171232523Sbostic 
171332523Sbostic /*
171432523Sbostic  * A Unibus reset has occurred on UBA uban.  Reinitialise the controller(s)
171532523Sbostic  * on that Unibus, and requeue outstanding I/O.
171632523Sbostic  */
171732523Sbostic udareset(uban)
171832523Sbostic 	int uban;
171932523Sbostic {
172032523Sbostic 	register struct uba_ctlr *um;
172132523Sbostic 	register struct uda_softc *sc;
172232523Sbostic 	register int ctlr;
172332523Sbostic 
172432523Sbostic 	for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) {
172532523Sbostic 		if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban ||
172632523Sbostic 		    um->um_alive == 0)
172732523Sbostic 			continue;
172832523Sbostic 		printf(" uda%d", ctlr);
172932523Sbostic 
173032523Sbostic 		/*
173132523Sbostic 		 * Our BDP (if any) is gone; our command (if any) is
173232523Sbostic 		 * flushed; the device is no longer mapped; and the
173332523Sbostic 		 * UDA50 is not yet initialised.
173432523Sbostic 		 */
173532523Sbostic 		if (um->um_bdp) {
173632523Sbostic 			printf("<%d>", UBAI_BDP(um->um_bdp));
173732523Sbostic 			um->um_bdp = 0;
173832523Sbostic 		}
173932523Sbostic 		um->um_ubinfo = 0;
174032523Sbostic 		um->um_cmd = 0;
174132523Sbostic 		sc->sc_flags &= ~SC_MAPPED;
174232523Sbostic 		sc->sc_state = ST_IDLE;
174332523Sbostic 
174432523Sbostic 		/* reset queues and requeue pending transfers */
174532523Sbostic 		mscp_requeue(&sc->sc_mi);
174632523Sbostic 
174732523Sbostic 		/*
174832523Sbostic 		 * If it fails to initialise we will notice later and
174932523Sbostic 		 * try again (and again...).  Do not call udastart()
175032523Sbostic 		 * here; it will be done after the controller finishes
175132523Sbostic 		 * initialisation.
175232523Sbostic 		 */
175332523Sbostic 		if (udainit(ctlr))
175432523Sbostic 			printf(" (hung)");
175532523Sbostic 	}
175632523Sbostic }
175732523Sbostic 
175832523Sbostic /*
175932523Sbostic  * Watchdog timer:  If the controller is active, and no interrupts
176032523Sbostic  * have occurred for 30 seconds, assume it has gone away.
176132523Sbostic  */
176232523Sbostic udawatch()
176332523Sbostic {
176432523Sbostic 	register int i;
176532523Sbostic 	register struct uba_ctlr *um;
176632523Sbostic 	register struct uda_softc *sc;
176732523Sbostic 
176832523Sbostic 	timeout(udawatch, (caddr_t) 0, hz);	/* every second */
176932523Sbostic 	for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) {
177032523Sbostic 		if ((um = udaminfo[i]) == 0 || !um->um_alive)
177132523Sbostic 			continue;
177232523Sbostic 		if (sc->sc_state == ST_IDLE)
177332523Sbostic 			continue;
177432523Sbostic 		if (sc->sc_state == ST_RUN && !um->um_tab.b_active)
177532523Sbostic 			sc->sc_wticks = 0;
177632523Sbostic 		else if (++sc->sc_wticks >= 30) {
177732523Sbostic 			sc->sc_wticks = 0;
177832523Sbostic 			printf("uda%d: lost interrupt\n", i);
177932523Sbostic 			ubareset(um->um_ubanum);
178032523Sbostic 		}
178132523Sbostic 	}
178232523Sbostic }
178332523Sbostic 
178432523Sbostic /*
178532523Sbostic  * Do a panic dump.  We set up the controller for one command packet
178632523Sbostic  * and one response packet, for which we use `struct uda1'.
178732523Sbostic  */
178832523Sbostic struct	uda1 {
178932523Sbostic 	struct	uda1ca uda1_ca;	/* communications area */
179032523Sbostic 	struct	mscp uda1_rsp;	/* response packet */
179132523Sbostic 	struct	mscp uda1_cmd;	/* command packet */
179232523Sbostic } uda1;
179332523Sbostic 
179432523Sbostic #define	DBSIZE	32		/* dump 16K at a time */
179532523Sbostic 
179632523Sbostic udadump(dev)
179732523Sbostic 	dev_t dev;
179832523Sbostic {
179932523Sbostic 	struct udadevice *udaddr;
180032523Sbostic 	struct uda1 *ud_ubaddr;
180132523Sbostic 	char *start;
180232523Sbostic 	int num, blk, unit, maxsz, blkoff, reg;
180332523Sbostic 	struct partition *pp;
180432523Sbostic 	register struct uba_regs *uba;
180532523Sbostic 	register struct uba_device *ui;
180632523Sbostic 	register struct uda1 *ud;
180732523Sbostic 	register struct pte *io;
180832523Sbostic 	register int i;
180932523Sbostic 
181032523Sbostic 	/*
181132523Sbostic 	 * Make sure the device is a reasonable place on which to dump.
181232523Sbostic 	 */
181332523Sbostic 	unit = udaunit(dev);
181432523Sbostic 	if (unit >= NRA)
181532523Sbostic 		return (ENXIO);
181633444Sbostic #define	phys(cast, addr)	((cast) ((int)addr & 0x7fffffff))
181732523Sbostic 	ui = phys(struct uba_device *, udadinfo[unit]);
181832523Sbostic 	if (ui == NULL || ui->ui_alive == 0)
181932523Sbostic 		return (ENXIO);
182032523Sbostic 
182132523Sbostic 	/*
182232523Sbostic 	 * Find and initialise the UBA; get the physical address of the
182332523Sbostic 	 * device registers, and of communications area and command and
182432523Sbostic 	 * response packet.
182532523Sbostic 	 */
182632523Sbostic 	uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
182732523Sbostic 	ubainit(uba);
182832523Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
182932523Sbostic 	ud = phys(struct uda1 *, &uda1);
183032523Sbostic 
183132523Sbostic 	/*
183232523Sbostic 	 * Map the ca+packets into Unibus I/O space so the UDA50 can get
183332523Sbostic 	 * at them.  Use the registers at the end of the Unibus map (since
183432523Sbostic 	 * we will use the registers at the beginning to map the memory
183532523Sbostic 	 * we are dumping).
183632523Sbostic 	 */
183732523Sbostic 	num = btoc(sizeof(struct uda1)) + 1;
183832523Sbostic 	reg = NUBMREG - num;
183932523Sbostic 	io = &uba->uba_map[reg];
184032523Sbostic 	for (i = 0; i < num; i++)
184132523Sbostic 		*(int *)io++ = UBAMR_MRV | (btop(ud) + i);
184232523Sbostic 	ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9));
184332523Sbostic 
184432523Sbostic 	/*
184532523Sbostic 	 * Initialise the controller, with one command and one response
184632523Sbostic 	 * packet.
184732523Sbostic 	 */
184832523Sbostic 	udaddr->udaip = 0;
184932523Sbostic 	if (udadumpwait(udaddr, UDA_STEP1))
185032523Sbostic 		return (EFAULT);
185132523Sbostic 	udaddr->udasa = UDA_ERR;
185232523Sbostic 	if (udadumpwait(udaddr, UDA_STEP2))
185332523Sbostic 		return (EFAULT);
185432523Sbostic 	udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc;
185532523Sbostic 	if (udadumpwait(udaddr, UDA_STEP3))
185632523Sbostic 		return (EFAULT);
185732523Sbostic 	udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16;
185832523Sbostic 	if (udadumpwait(udaddr, UDA_STEP4))
185932523Sbostic 		return (EFAULT);
186032523Sbostic 	uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff;
186132523Sbostic 	udaddr->udasa = UDA_GO;
186232523Sbostic 
186332523Sbostic 	/*
186432523Sbostic 	 * Set up the command and response descriptor, then set the
186532523Sbostic 	 * controller characteristics and bring the drive on line.
186632523Sbostic 	 * Note that all uninitialised locations in uda1_cmd are zero.
186732523Sbostic 	 */
186832523Sbostic 	ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref;
186932523Sbostic 	ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref;
187032523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */
187132523Sbostic 	/* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */
187232523Sbostic 	if (udadumpcmd(M_OP_SETCTLRC, ud, ui))
187332523Sbostic 		return (EFAULT);
187432523Sbostic 	ud->uda1_cmd.mscp_unit = ui->ui_slave;
187532523Sbostic 	if (udadumpcmd(M_OP_ONLINE, ud, ui))
187632523Sbostic 		return (EFAULT);
187732523Sbostic 
187832523Sbostic 	pp = phys(struct partition *,
187932523Sbostic 	    &udalabel[unit].d_partitions[udapart(dev)]);
188032523Sbostic 	maxsz = pp->p_size;
188132523Sbostic 	blkoff = pp->p_offset;
188232523Sbostic 
188332523Sbostic 	/*
188432523Sbostic 	 * Dump all of physical memory, or as much as will fit in the
188532523Sbostic 	 * space provided.
188632523Sbostic 	 */
188732523Sbostic 	start = 0;
188832523Sbostic 	num = maxfree;
188932523Sbostic 	if (dumplo < 0)
189032523Sbostic 		return (EINVAL);
189132523Sbostic 	if (dumplo + num >= maxsz)
189232523Sbostic 		num = maxsz - dumplo;
189332523Sbostic 	blkoff += dumplo;
189432523Sbostic 
189532523Sbostic 	/*
189632523Sbostic 	 * Write out memory, DBSIZE pages at a time.
189732523Sbostic 	 * N.B.: this code depends on the fact that the sector
189832523Sbostic 	 * size == the page size.
189932523Sbostic 	 */
190032523Sbostic 	while (num > 0) {
190132523Sbostic 		blk = num > DBSIZE ? DBSIZE : num;
190232523Sbostic 		io = uba->uba_map;
190332523Sbostic 		/*
190432523Sbostic 		 * Map in the pages to write, leaving an invalid entry
190532523Sbostic 		 * at the end to guard against wild Unibus transfers.
190632523Sbostic 		 * Then do the write.
190732523Sbostic 		 */
190832523Sbostic 		for (i = 0; i < blk; i++)
190933444Sbostic 			*(int *)io++ = UBAMR_MRV | (btop(start) + i);
191033444Sbostic 		*(int *)io = 0;
191132523Sbostic 		ud->uda1_cmd.mscp_unit = ui->ui_slave;
191232523Sbostic 		ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff;
191332523Sbostic 		ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT;
191432523Sbostic 		if (udadumpcmd(M_OP_WRITE, ud, ui))
191532523Sbostic 			return (EIO);
191632523Sbostic 		start += blk << PGSHIFT;
191732523Sbostic 		num -= blk;
191832523Sbostic 	}
191932523Sbostic 	return (0);		/* made it! */
192032523Sbostic }
192132523Sbostic 
192232523Sbostic /*
192332523Sbostic  * Wait for some of the bits in `bits' to come on.  If the error bit
192432523Sbostic  * comes on, or ten seconds pass without response, return true (error).
192532523Sbostic  */
192632523Sbostic udadumpwait(udaddr, bits)
192732523Sbostic 	register struct udadevice *udaddr;
192832523Sbostic 	register int bits;
192932523Sbostic {
193032523Sbostic 	register int timo = todr() + 1000;
193132523Sbostic 
193232523Sbostic 	while ((udaddr->udasa & bits) == 0) {
193332523Sbostic 		if (udaddr->udasa & UDA_ERR) {
193432523Sbostic 			printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
193532523Sbostic 			return (1);
193632523Sbostic 		}
193732523Sbostic 		if (todr() >= timo) {
193832523Sbostic 			printf("timeout\ndump ");
193932523Sbostic 			return (1);
194032523Sbostic 		}
194132523Sbostic 	}
194230536Skarels 	return (0);
194330536Skarels }
194430536Skarels 
194532523Sbostic /*
194632523Sbostic  * Feed a command to the UDA50, wait for its response, and return
194732523Sbostic  * true iff something went wrong.
194832523Sbostic  */
194932523Sbostic udadumpcmd(op, ud, ui)
195032523Sbostic 	int op;
195132523Sbostic 	register struct uda1 *ud;
195232523Sbostic 	struct uba_device *ui;
195332523Sbostic {
195432523Sbostic 	register struct udadevice *udaddr;
195532523Sbostic 	register int n;
195632523Sbostic #define mp (&ud->uda1_rsp)
195732523Sbostic 
195833444Sbostic 	udaddr = (struct udadevice *)ui->ui_physaddr;
195932523Sbostic 	ud->uda1_cmd.mscp_opcode = op;
196032523Sbostic 	ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN;
196132523Sbostic 	ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN;
196232523Sbostic 	ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
196332523Sbostic 	ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT;
196432523Sbostic 	if (udaddr->udasa & UDA_ERR) {
196532523Sbostic 		printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits);
196632523Sbostic 		return (1);
196732523Sbostic 	}
196832523Sbostic 	n = udaddr->udaip;
196932523Sbostic 	n = todr() + 1000;
197032523Sbostic 	for (;;) {
197132523Sbostic 		if (todr() > n) {
197232523Sbostic 			printf("timeout\ndump ");
197332523Sbostic 			return (1);
197432523Sbostic 		}
197532523Sbostic 		if (ud->uda1_ca.ca_cmdint)
197632523Sbostic 			ud->uda1_ca.ca_cmdint = 0;
197732523Sbostic 		if (ud->uda1_ca.ca_rspint == 0)
197832523Sbostic 			continue;
197932523Sbostic 		ud->uda1_ca.ca_rspint = 0;
198032523Sbostic 		if (mp->mscp_opcode == (op | M_OP_END))
198132523Sbostic 			break;
198232523Sbostic 		printf("\n");
198332523Sbostic 		switch (MSCP_MSGTYPE(mp->mscp_msgtc)) {
198432523Sbostic 
198532523Sbostic 		case MSCPT_SEQ:
198632523Sbostic 			printf("sequential");
198732523Sbostic 			break;
198832523Sbostic 
198932523Sbostic 		case MSCPT_DATAGRAM:
199032523Sbostic 			mscp_decodeerror("uda", ui->ui_ctlr, mp);
199132523Sbostic 			printf("datagram");
199232523Sbostic 			break;
199332523Sbostic 
199432523Sbostic 		case MSCPT_CREDITS:
199532523Sbostic 			printf("credits");
199632523Sbostic 			break;
199732523Sbostic 
199832523Sbostic 		case MSCPT_MAINTENANCE:
199932523Sbostic 			printf("maintenance");
200032523Sbostic 			break;
200132523Sbostic 
200232523Sbostic 		default:
200332523Sbostic 			printf("unknown (type 0x%x)",
200432523Sbostic 				MSCP_MSGTYPE(mp->mscp_msgtc));
200532523Sbostic 			break;
200632523Sbostic 		}
200732523Sbostic 		printf(" ignored\ndump ");
200832523Sbostic 		ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT;
200932523Sbostic 	}
201032523Sbostic 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
201132523Sbostic 		printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op,
201232523Sbostic 			mp->mscp_opcode, mp->mscp_status);
201332523Sbostic 		return (1);
201432523Sbostic 	}
201532523Sbostic 	return (0);
201632523Sbostic #undef mp
201732523Sbostic }
201832523Sbostic 
201932523Sbostic /*
202032523Sbostic  * Return the size of a partition, if known, or -1 if not.
202132523Sbostic  */
202232523Sbostic udasize(dev)
202330536Skarels 	dev_t dev;
202430536Skarels {
202532523Sbostic 	register int unit = udaunit(dev);
202630536Skarels 	register struct uba_device *ui;
202730536Skarels 
202832523Sbostic 	if (unit >= NRA || (ui = udadinfo[unit]) == NULL ||
202932523Sbostic 	    ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 ||
203032523Sbostic 	    ra_info[unit].ra_state != OPEN)
203112511Ssam 		return (-1);
203232523Sbostic 	return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size);
203312511Ssam }
203417553Skarels 
203530536Skarels #ifdef COMPAT_42
203632523Sbostic /*
203732523Sbostic  * Tables mapping unlabelled drives.
203832523Sbostic  */
203930536Skarels struct size {
204030536Skarels 	daddr_t nblocks;
204130536Skarels 	daddr_t blkoff;
204233444Sbostic } ra60_sizes[8] = {
204330536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
204430536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
204530536Skarels 	400176,	0,		/* C=sectors 0 thru 400175 */
204630536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
204730536Skarels 	268772,	131404,		/* 4.2 H => E=sectors 131404 thru 400175 */
204830536Skarels 	350852,	49324,		/* F=sectors 49324 thru 400175 */
204930536Skarels 	157570,	242606,		/* UCB G => G=sectors 242606 thru 400175 */
205030536Skarels 	193282,	49324,		/* UCB H => H=sectors 49324 thru 242605 */
205133444Sbostic }, ra70_sizes[8] = {
205233444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
205333444Sbostic 	33440,	15972,		/* B=blk 15972 thru 49323 */
205433444Sbostic 	-1,	0,		/* C=blk 0 thru end */
205533444Sbostic 	15884,	341220,		/* D=blk 341220 thru 357103 */
205633444Sbostic 	55936,	357192,		/* E=blk 357192 thru 413127 */
205733444Sbostic 	-1,	413457,		/* F=blk 413457 thru end */
205833444Sbostic 	-1,	341220,		/* G=blk 341220 thru end */
205933444Sbostic 	291346,	49731,		/* H=blk 49731 thru 341076 */
206030536Skarels }, ra80_sizes[8] = {
206130536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
206230536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
206330536Skarels 	242606,	0,		/* C=sectors 0 thru 242605 */
206430536Skarels 	0,	0,		/* D=unused */
206530536Skarels 	193282,	49324,		/* UCB H => E=sectors 49324 thru 242605 */
206630536Skarels 	82080,	49324,		/* 4.2 G => F=sectors 49324 thru 131403 */
206730536Skarels 	192696,	49910,		/* G=sectors 49910 thru 242605 */
206830536Skarels 	111202,	131404,		/* 4.2 H => H=sectors 131404 thru 242605 */
206930536Skarels }, ra81_sizes[8] ={
207033444Sbostic #ifdef MARYLAND
207133444Sbostic #ifdef ENEEVAX
207233444Sbostic 	30706,	0,		/* A=cyl    0 thru   42 + 2 sectors */
207333444Sbostic 	40696,	30706,		/* B=cyl   43 thru   99 - 2 sectors */
207433444Sbostic 	-1,	0,		/* C=cyl    0 thru 1247 */
207533444Sbostic 	-1,	71400,		/* D=cyl  100 thru 1247 */
207633444Sbostic 
207733444Sbostic 	15884,	0,		/* E=blk      0 thru  15883 */
207833444Sbostic 	33440,	15884,		/* F=blk  15884 thru  49323 */
207933444Sbostic 	82080,	49324,		/* G=blk  49324 thru 131403 */
208033444Sbostic 	-1,	131404,		/* H=blk 131404 thru    end */
208133444Sbostic #else
208233444Sbostic 	67832,	0,		/* A=cyl    0 thru   94 + 2 sectors */
208333444Sbostic 	67828,	67832,		/* B=cyl   95 thru  189 - 2 sectors */
208433444Sbostic 	-1,	0,		/* C=cyl    0 thru 1247 */
208533444Sbostic 	-1,	135660,		/* D=cyl  190 thru 1247 */
208633444Sbostic 	0,	0,
208733444Sbostic 	0,	0,
208833444Sbostic 	0,	0,
208933444Sbostic 	0,	0,
209033444Sbostic #endif ENEEVAX
209133444Sbostic #else
209230536Skarels /*
209330536Skarels  * These are the new standard partition sizes for ra81's.
209430536Skarels  * An RA_COMPAT system is compiled with D, E, and F corresponding
209530536Skarels  * to the 4.2 partitions for G, H, and F respectively.
209630536Skarels  */
209730536Skarels #ifndef	UCBRA
209830536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
209930536Skarels 	66880,	16422,		/* B=sectors 16422 thru 83301 */
210030536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
210130536Skarels #ifdef RA_COMPAT
210230536Skarels 	82080,	49324,		/* 4.2 G => D=sectors 49324 thru 131403 */
210330536Skarels 	759668,	131404,		/* 4.2 H => E=sectors 131404 thru 891071 */
210430536Skarels 	478582,	412490,		/* 4.2 F => F=sectors 412490 thru 891071 */
210530536Skarels #else
210630536Skarels 	15884,	375564,		/* D=sectors 375564 thru 391447 */
210730536Skarels 	307200,	391986,		/* E=sectors 391986 thru 699185 */
210830536Skarels 	191352,	699720,		/* F=sectors 699720 thru 891071 */
210930536Skarels #endif RA_COMPAT
211030536Skarels 	515508,	375564,		/* G=sectors 375564 thru 891071 */
211130536Skarels 	291346,	83538,		/* H=sectors 83538 thru 374883 */
211230536Skarels 
211330536Skarels /*
211430536Skarels  * These partitions correspond to the sizes used by sites at Berkeley,
211530536Skarels  * and by those sites that have received copies of the Berkeley driver
211630536Skarels  * with deltas 6.2 or greater (11/15/83).
211730536Skarels  */
211830536Skarels #else UCBRA
211930536Skarels 
212030536Skarels 	15884,	0,		/* A=sectors 0 thru 15883 */
212130536Skarels 	33440,	15884,		/* B=sectors 15884 thru 49323 */
212230536Skarels 	891072,	0,		/* C=sectors 0 thru 891071 */
212330536Skarels 	15884,	242606,		/* D=sectors 242606 thru 258489 */
212430536Skarels 	307200,	258490,		/* E=sectors 258490 thru 565689 */
212530536Skarels 	325382,	565690,		/* F=sectors 565690 thru 891071 */
212630536Skarels 	648466,	242606,		/* G=sectors 242606 thru 891071 */
212730536Skarels 	193282,	49324,		/* H=sectors 49324 thru 242605 */
212830536Skarels 
212930536Skarels #endif UCBRA
213033444Sbostic #endif MARYLAND
213133444Sbostic }, ra82_sizes[8] = {
213233444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
213333444Sbostic 	66880,	16245,		/* B=blk 16245 thru 83124 */
213433444Sbostic 	-1,	0,		/* C=blk 0 thru end */
213533444Sbostic 	15884,	375345,		/* D=blk 375345 thru 391228 */
213633444Sbostic 	307200,	391590,		/* E=blk 391590 thru 698789 */
213733444Sbostic 	-1,	699390,		/* F=blk 699390 thru end */
213833444Sbostic 	-1,	375345,		/* G=blk 375345 thru end */
213933444Sbostic 	291346,	83790,		/* H=blk 83790 thru 375135 */
214033444Sbostic }, rc25_sizes[8] = {
214133444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
214233444Sbostic 	10032,	15884,		/* B=blk 15884 thru 49323 */
214333444Sbostic 	-1,	0,		/* C=blk 0 thru end */
214433444Sbostic 	0,	0,		/* D=blk 340670 thru 356553 */
214533444Sbostic 	0,	0,		/* E=blk 356554 thru 412489 */
214633444Sbostic 	0,	0,		/* F=blk 412490 thru end */
214733444Sbostic 	-1,	25916,		/* G=blk 49324 thru 131403 */
214833444Sbostic 	0,	0,		/* H=blk 131404 thru end */
214933444Sbostic }, rd52_sizes[8] = {
215033444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
215133444Sbostic 	9766,	15884,		/* B=blk 15884 thru 25649 */
215233444Sbostic 	-1,	0,		/* C=blk 0 thru end */
215333444Sbostic 	0,	0,		/* D=unused */
215433444Sbostic 	0,	0,		/* E=unused */
215533444Sbostic 	0,	0,		/* F=unused */
215633444Sbostic 	-1,	25650,		/* G=blk 25650 thru end */
215733444Sbostic 	0,	0,		/* H=unused */
215833444Sbostic }, rd53_sizes[8] = {
215933444Sbostic 	15884,	0,		/* A=blk 0 thru 15883 */
216033444Sbostic 	33440,	15884,		/* B=blk 15884 thru 49323 */
216133444Sbostic 	-1,	0,		/* C=blk 0 thru end */
216233444Sbostic 	0,	0,		/* D=unused */
216333444Sbostic 	33440,	0,		/* E=blk 0 thru 33439 */
216433444Sbostic 	-1,	33440,		/* F=blk 33440 thru end */
216533444Sbostic 	-1,	49324,		/* G=blk 49324 thru end */
216633444Sbostic 	-1,	15884,		/* H=blk 15884 thru end */
216733444Sbostic }, rx50_sizes[8] = {
216833444Sbostic 	800,	0,		/* A=blk 0 thru 799 */
216933444Sbostic 	0,	0,
217033444Sbostic 	-1,	0,		/* C=blk 0 thru end */
217133444Sbostic 	0,	0,
217233444Sbostic 	0,	0,
217333444Sbostic 	0,	0,
217433444Sbostic 	0,	0,
217533444Sbostic 	0,	0,
217630536Skarels };
217730536Skarels 
217832523Sbostic /*
217933444Sbostic  * Media ID decoding table.
218032523Sbostic  */
218132523Sbostic struct	udatypes {
218233444Sbostic 	u_long	ut_id;		/* media drive ID */
218332523Sbostic 	char	*ut_name;	/* drive type name */
218432523Sbostic 	struct	size *ut_sizes;	/* partition tables */
218532523Sbostic 	int	ut_nsectors, ut_ntracks, ut_ncylinders;
218632523Sbostic } udatypes[] = {
218733444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 60), "ra60", ra60_sizes, 42, 4, 2382 },
218833444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 70), "ra70", ra70_sizes, 33, 11, 1507 },
218933444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 80), "ra80", ra80_sizes, 31, 14, 559 },
219033444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 81), "ra81", ra81_sizes, 51, 14, 1248 },
219133444Sbostic 	{ MSCP_MKDRIVE2('R', 'A', 82), "ra82", ra82_sizes, 57, 14, 1423 },
219233444Sbostic 	{ MSCP_MKDRIVE2('R', 'C', 25), "rc25-removable",
219333444Sbostic 						rc25_sizes, 42, 4, 302 },
219433444Sbostic 	{ MSCP_MKDRIVE3('R', 'C', 'F', 25), "rc25-fixed",
219533444Sbostic 						rc25_sizes, 42, 4, 302 },
219633444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 52), "rd52", rd52_sizes, 18, 7, 480 },
219733444Sbostic 	{ MSCP_MKDRIVE2('R', 'D', 53), "rd53", rd53_sizes, 18, 8, 963 },
219833444Sbostic 	{ MSCP_MKDRIVE2('R', 'X', 50), "rx50", rx50_sizes, 10, 1, 80 },
219933444Sbostic 	0
220032523Sbostic };
220132523Sbostic 
220232523Sbostic #define NTYPES (sizeof(udatypes) / sizeof(*udatypes))
220332523Sbostic 
220432523Sbostic udamaptype(unit, lp)
220532523Sbostic 	int unit;
220630536Skarels 	register struct disklabel *lp;
220730536Skarels {
220832523Sbostic 	register struct udatypes *ut;
220932523Sbostic 	register struct size *sz;
221030536Skarels 	register struct partition *pp;
221132523Sbostic 	register char *p;
221232523Sbostic 	register int i;
221332523Sbostic 	register struct ra_info *ra = &ra_info[unit];
221430536Skarels 
221532523Sbostic 	lp->d_secsize = 512;
221632523Sbostic 	lp->d_secperunit = ra->ra_dsize;
221733444Sbostic 	i = MSCP_MEDIA_DRIVE(ra->ra_mediaid);
221833444Sbostic 	for (ut = udatypes; ut->ut_id; ut++)
221933444Sbostic 		if (ut->ut_id == i)
222033444Sbostic 			goto found;
222133444Sbostic 
222233444Sbostic 	/* not one we know; fake up a label for the whole drive */
222333444Sbostic 	lp->d_nsectors = ra->ra_geom.rg_nsectors;
222433444Sbostic 	lp->d_ntracks = ra->ra_geom.rg_ntracks;
222533444Sbostic 	lp->d_ncylinders = ra->ra_geom.rg_ncyl;
222633444Sbostic 	i = ra->ra_mediaid;	/* print the port type too */
222734283Skarels 	if (!cold)
222834283Skarels 		log(LOG_ERR, "ra%d", unit);
222934283Skarels 	addlog(": don't have a partition table for %c%c %c%c%c%d;\n\
223034283Skarels using (s,t,c)=(%d,%d,%d)",
223134283Skarels 		MSCP_MID_CHAR(4, i), MSCP_MID_CHAR(3, i),
223233444Sbostic 		MSCP_MID_CHAR(2, i), MSCP_MID_CHAR(1, i),
223333444Sbostic 		MSCP_MID_CHAR(0, i), MSCP_MID_CHAR(0, i),
223433444Sbostic 		MSCP_MID_NUM(i), lp->d_nsectors,
223533444Sbostic 		lp->d_ntracks, lp->d_ncylinders);
223634283Skarels 	if (!cold)
223734283Skarels 		addlog("\n");
223833444Sbostic 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
223933444Sbostic 	lp->d_typename[0] = 'r';
224033444Sbostic 	lp->d_typename[1] = 'a';
224133444Sbostic 	lp->d_typename[2] = '?';
224233444Sbostic 	lp->d_typename[3] = '?';
224333444Sbostic 	lp->d_typename[4] = 0;
224433444Sbostic 	lp->d_npartitions = 1;
224533444Sbostic 	lp->d_partitions[0].p_offset = 0;
224633444Sbostic 	lp->d_partitions[0].p_size = lp->d_secperunit;
224733444Sbostic 	return (0);
224833444Sbostic found:
224932523Sbostic 	p = ut->ut_name;
225032523Sbostic 	for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++)
225132523Sbostic 		lp->d_typename[i] = *p++;
225232523Sbostic 	lp->d_typename[i] = 0;
225332523Sbostic 	sz = ut->ut_sizes;
225432523Sbostic 	/* GET nsectors, ntracks, ncylinders FROM SAVED GEOMETRY? */
225532523Sbostic 	lp->d_nsectors = ut->ut_nsectors;
225632523Sbostic 	lp->d_ntracks = ut->ut_ntracks;
225732523Sbostic 	lp->d_ncylinders = ut->ut_ncylinders;
225830536Skarels 	lp->d_npartitions = 8;
225930536Skarels 	lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks;
226032523Sbostic 	for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) {
226132523Sbostic 		pp->p_offset = sz->blkoff;
226232523Sbostic 		if ((pp->p_size = sz->nblocks) == (u_long)-1)
226332523Sbostic 			pp->p_size = ra->ra_dsize - sz->blkoff;
226430536Skarels 	}
226530536Skarels 	return (1);
226630536Skarels }
226732523Sbostic #endif /* COMPAT_42 */
226832523Sbostic #endif /* NUDA > 0 */
2269