123353Smckusick /* 235039Sbostic * Copyright (c) 1988 Regents of the University of California. 335039Sbostic * All rights reserved. 432523Sbostic * 535039Sbostic * This code is derived from software contributed to Berkeley by 635039Sbostic * Chris Torek. 732523Sbostic * 835039Sbostic * Redistribution and use in source and binary forms are permitted 935039Sbostic * provided that the above copyright notice and this paragraph are 1035039Sbostic * duplicated in all such forms and that any documentation, 1135039Sbostic * advertising materials, and other materials related to such 1235039Sbostic * distribution and use acknowledge that the software was developed 1335039Sbostic * by the University of California, Berkeley. The name of the 1435039Sbostic * University may not be used to endorse or promote products derived 1535039Sbostic * from this software without specific prior written permission. 1635039Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1735039Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1835039Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1935039Sbostic * 20*40726Skarels * @(#)uda.c 7.27 (Berkeley) 04/03/90 2123353Smckusick */ 2223353Smckusick 2332523Sbostic /* 2432523Sbostic * UDA50/MSCP device driver 2517553Skarels */ 2617553Skarels 2732523Sbostic #define POLLSTATS 2832523Sbostic 2932523Sbostic /* 3032523Sbostic * TODO 3132523Sbostic * write bad block forwarding code 3232523Sbostic */ 3332523Sbostic 344743Swnj #include "ra.h" 3532523Sbostic 3617642Skarels #if NUDA > 0 3732523Sbostic 384743Swnj /* 3932523Sbostic * CONFIGURATION OPTIONS. The next three defines are tunable -- tune away! 404743Swnj * 4132523Sbostic * COMPAT_42 enables 4.2/4.3 compatibility (label mapping) 4232523Sbostic * 4332523Sbostic * NRSPL2 and NCMDL2 control the number of response and command 4432523Sbostic * packets respectively. They may be any value from 0 to 7, though 4532523Sbostic * setting them higher than 5 is unlikely to be of any value. 4632523Sbostic * If you get warnings about your command ring being too small, 4732523Sbostic * try increasing the values by one. 4832523Sbostic * 4932523Sbostic * MAXUNIT controls the maximum unit number (number of drives per 5032523Sbostic * controller) we are prepared to handle. 5132523Sbostic * 5232523Sbostic * DEFAULT_BURST must be at least 1. 534743Swnj */ 5432523Sbostic #define COMPAT_42 5532523Sbostic 5632523Sbostic #define NRSPL2 5 /* log2 number of response packets */ 5732523Sbostic #define NCMDL2 5 /* log2 number of command packets */ 5832523Sbostic #define MAXUNIT 8 /* maximum allowed unit number */ 5932523Sbostic #define DEFAULT_BURST 4 /* default DMA burst size */ 6032523Sbostic 6117553Skarels #include "param.h" 6217553Skarels #include "systm.h" 6317553Skarels #include "buf.h" 6417553Skarels #include "conf.h" 6530536Skarels #include "file.h" 6630536Skarels #include "ioctl.h" 6717553Skarels #include "user.h" 6817553Skarels #include "map.h" 6917553Skarels #include "vm.h" 7030536Skarels #include "dkstat.h" 7117553Skarels #include "cmap.h" 7230536Skarels #include "disklabel.h" 7330536Skarels #include "syslog.h" 7430773Skarels #include "stat.h" 754743Swnj 7637512Smckusick #include "machine/pte.h" 7734283Skarels 788482Sroot #include "../vax/cpu.h" 7917553Skarels #include "ubareg.h" 8017553Skarels #include "ubavar.h" 818613Sroot 8232523Sbostic #define NRSP (1 << NRSPL2) 8332523Sbostic #define NCMD (1 << NCMDL2) 848613Sroot 8532523Sbostic #include "udareg.h" 868482Sroot #include "../vax/mscp.h" 8732523Sbostic #include "../vax/mscpvar.h" 8832523Sbostic #include "../vax/mtpr.h" 894743Swnj 9032523Sbostic /* 9132523Sbostic * UDA communications area and MSCP packet pools, per controller. 9232523Sbostic */ 9332523Sbostic struct uda { 9432523Sbostic struct udaca uda_ca; /* communications area */ 9532523Sbostic struct mscp uda_rsp[NRSP]; /* response packets */ 9632523Sbostic struct mscp uda_cmd[NCMD]; /* command packets */ 974743Swnj } uda[NUDA]; 984743Swnj 9932523Sbostic /* 10032523Sbostic * Software status, per controller. 10132523Sbostic */ 10232523Sbostic struct uda_softc { 10332523Sbostic struct uda *sc_uda; /* Unibus address of uda struct */ 10432523Sbostic short sc_state; /* UDA50 state; see below */ 10532523Sbostic short sc_flags; /* flags; see below */ 10632523Sbostic int sc_micro; /* microcode revision */ 10732523Sbostic int sc_ivec; /* interrupt vector address */ 10836036Skarels short sc_ipl; /* interrupt priority, Q-bus */ 10932523Sbostic struct mscp_info sc_mi;/* MSCP info (per mscpvar.h) */ 11032523Sbostic #ifndef POLLSTATS 11132523Sbostic int sc_wticks; /* watchdog timer ticks */ 11232523Sbostic #else 11332523Sbostic short sc_wticks; 11432523Sbostic short sc_ncmd; 11532523Sbostic #endif 11632523Sbostic } uda_softc[NUDA]; 11724742Sbloom 11832523Sbostic #ifdef POLLSTATS 11932523Sbostic struct udastats { 12032523Sbostic int ncmd; 12132523Sbostic int cmd[NCMD + 1]; 12232523Sbostic } udastats = { NCMD + 1 }; 12332523Sbostic #endif 12417553Skarels 12532523Sbostic /* 12632523Sbostic * Controller states 12732523Sbostic */ 12832523Sbostic #define ST_IDLE 0 /* uninitialised */ 12932523Sbostic #define ST_STEP1 1 /* in `STEP 1' */ 13032523Sbostic #define ST_STEP2 2 /* in `STEP 2' */ 13132523Sbostic #define ST_STEP3 3 /* in `STEP 3' */ 13232523Sbostic #define ST_SETCHAR 4 /* in `Set Controller Characteristics' */ 13332523Sbostic #define ST_RUN 5 /* up and running */ 1344743Swnj 13532523Sbostic /* 13632523Sbostic * Flags 13732523Sbostic */ 13832523Sbostic #define SC_MAPPED 0x01 /* mapped in Unibus I/O space */ 13932523Sbostic #define SC_INSTART 0x02 /* inside udastart() */ 14032523Sbostic #define SC_GRIPED 0x04 /* griped about cmd ring too small */ 14132523Sbostic #define SC_INSLAVE 0x08 /* inside udaslave() */ 14232523Sbostic #define SC_DOWAKE 0x10 /* wakeup when ctlr init done */ 14332523Sbostic #define SC_STARTPOLL 0x20 /* need to initiate polling */ 14412421Ssam 14532523Sbostic /* 14632523Sbostic * Device to unit number and partition and back 14732523Sbostic */ 14832523Sbostic #define UNITSHIFT 3 14932523Sbostic #define UNITMASK 7 15032523Sbostic #define udaunit(dev) (minor(dev) >> UNITSHIFT) 15132523Sbostic #define udapart(dev) (minor(dev) & UNITMASK) 15232523Sbostic #define udaminor(u, p) (((u) << UNITSHIFT) | (p)) 1534743Swnj 15417553Skarels /* 15532523Sbostic * Drive status, per drive 15617553Skarels */ 15732523Sbostic struct ra_info { 15832523Sbostic daddr_t ra_dsize; /* size in sectors */ 15933444Sbostic /* u_long ra_type; /* drive type */ 16032523Sbostic u_long ra_mediaid; /* media id */ 16132523Sbostic int ra_state; /* open/closed state */ 16232523Sbostic struct ra_geom { /* geometry information */ 16332523Sbostic u_short rg_nsectors; /* sectors/track */ 16432523Sbostic u_short rg_ngroups; /* track groups */ 16532523Sbostic u_short rg_ngpc; /* groups/cylinder */ 16632523Sbostic u_short rg_ntracks; /* ngroups*ngpc */ 16732523Sbostic u_short rg_ncyl; /* ra_dsize/ntracks/nsectors */ 16832523Sbostic #ifdef notyet 16932523Sbostic u_short rg_rctsize; /* size of rct */ 17032523Sbostic u_short rg_rbns; /* replacement blocks per track */ 17132523Sbostic u_short rg_nrct; /* number of rct copies */ 17232523Sbostic #endif 17332523Sbostic } ra_geom; 17433443Skarels int ra_wlabel; /* label sector is currently writable */ 17532523Sbostic u_long ra_openpart; /* partitions open */ 17632523Sbostic u_long ra_bopenpart; /* block partitions open */ 17732523Sbostic u_long ra_copenpart; /* character partitions open */ 17832523Sbostic } ra_info[NRA]; 17917553Skarels 18030536Skarels /* 18130536Skarels * Software state, per drive 18230536Skarels */ 18330536Skarels #define CLOSED 0 18430536Skarels #define WANTOPEN 1 18530536Skarels #define RDLABEL 2 18630536Skarels #define OPEN 3 18730536Skarels #define OPENRAW 4 18817553Skarels 18932523Sbostic /* 19032523Sbostic * Definition of the driver for autoconf. 19132523Sbostic */ 19232523Sbostic int udaprobe(), udaslave(), udaattach(), udadgo(), udaintr(); 19332523Sbostic struct uba_ctlr *udaminfo[NUDA]; 19432523Sbostic struct uba_device *udadinfo[NRA]; 19532523Sbostic struct disklabel udalabel[NRA]; 19617553Skarels 19732523Sbostic u_short udastd[] = { 0772150, 0772550, 0777550, 0 }; 19832523Sbostic struct uba_driver udadriver = 19932523Sbostic { udaprobe, udaslave, udaattach, udadgo, udastd, "ra", udadinfo, "uda", 20032523Sbostic udaminfo }; 20117553Skarels 20232523Sbostic /* 20332523Sbostic * More driver definitions, for generic MSCP code. 20432523Sbostic */ 20532523Sbostic int udadgram(), udactlrdone(), udaunconf(), udaiodone(); 20632523Sbostic int udaonline(), udagotstatus(), udaioerror(), udareplace(), udabb(); 2074743Swnj 20832523Sbostic struct buf udautab[NRA]; /* per drive transfer queue */ 2094743Swnj 21032523Sbostic struct mscp_driver udamscpdriver = 21134524Skarels { MAXUNIT, NRA, UNITSHIFT, udautab, udalabel, udadinfo, 21232523Sbostic udadgram, udactlrdone, udaunconf, udaiodone, 21332523Sbostic udaonline, udagotstatus, udareplace, udaioerror, udabb, 21432523Sbostic "uda", "ra" }; 21532523Sbostic 21632523Sbostic /* 21732523Sbostic * Miscellaneous private variables. 21832523Sbostic */ 21932523Sbostic char udasr_bits[] = UDASR_BITS; 22032523Sbostic 22132523Sbostic struct uba_device *udaip[NUDA][MAXUNIT]; 22232523Sbostic /* inverting pointers: ctlr & unit => Unibus 22332523Sbostic device pointer */ 22432523Sbostic 22532523Sbostic int udaburst[NUDA] = { 0 }; /* burst size, per UDA50, zero => default; 22632523Sbostic in data space so patchable via adb */ 22732523Sbostic 22832523Sbostic struct mscp udaslavereply; /* get unit status response packet, set 22932523Sbostic for udaslave by udaunconf, via udaintr */ 23032523Sbostic 23132523Sbostic static struct uba_ctlr *probeum;/* this is a hack---autoconf should pass ctlr 23232523Sbostic info to slave routine; instead, we remember 23332523Sbostic the last ctlr argument to probe */ 23432523Sbostic 23532523Sbostic int udawstart, udawatch(); /* watchdog timer */ 23632523Sbostic 23732523Sbostic /* 23832523Sbostic * Externals 23932523Sbostic */ 24032523Sbostic int wakeup(); 24132523Sbostic int hz; 24232523Sbostic 24332523Sbostic /* 24432523Sbostic * Poke at a supposed UDA50 to see if it is there. 24532523Sbostic * This routine duplicates some of the code in udainit() only 24632523Sbostic * because autoconf has not set up the right information yet. 24732523Sbostic * We have to do everything `by hand'. 24832523Sbostic */ 24932523Sbostic udaprobe(reg, ctlr, um) 2504743Swnj caddr_t reg; 2514743Swnj int ctlr; 25232523Sbostic struct uba_ctlr *um; 2534743Swnj { 2544743Swnj register int br, cvec; 25532523Sbostic register struct uda_softc *sc; 25632523Sbostic register struct udadevice *udaddr; 25732523Sbostic register struct mscp_info *mi; 25836036Skarels int timeout, tries, s; 2594743Swnj 26032523Sbostic #ifdef VAX750 26132523Sbostic /* 26232523Sbostic * The UDA50 wants to share BDPs on 750s, but not on 780s or 26332523Sbostic * 8600s. (730s have no BDPs anyway.) Toward this end, we 26432523Sbostic * here set the `keep bdp' flag in the per-driver information 26532523Sbostic * if this is a 750. (We just need to do it once, but it is 26632523Sbostic * easiest to do it now, for each UDA50.) 26732523Sbostic */ 26832523Sbostic if (cpu == VAX_750) 26932523Sbostic udadriver.ud_keepbdp = 1; 27032523Sbostic #endif 27117553Skarels 27232523Sbostic probeum = um; /* remember for udaslave() */ 2734743Swnj #ifdef lint 27432523Sbostic br = 0; cvec = br; br = cvec; udaintr(0); 2754743Swnj #endif 27632523Sbostic /* 27732523Sbostic * Set up the controller-specific generic MSCP driver info. 27832523Sbostic * Note that this should really be done in the (nonexistent) 27932523Sbostic * controller attach routine. 28032523Sbostic */ 28132523Sbostic sc = &uda_softc[ctlr]; 28232523Sbostic mi = &sc->sc_mi; 28332523Sbostic mi->mi_md = &udamscpdriver; 28432523Sbostic mi->mi_ctlr = um->um_ctlr; 28532523Sbostic mi->mi_tab = &um->um_tab; 28632523Sbostic mi->mi_ip = udaip[ctlr]; 28732523Sbostic mi->mi_cmd.mri_size = NCMD; 28832523Sbostic mi->mi_cmd.mri_desc = uda[ctlr].uda_ca.ca_cmddsc; 28932523Sbostic mi->mi_cmd.mri_ring = uda[ctlr].uda_cmd; 29032523Sbostic mi->mi_rsp.mri_size = NRSP; 29132523Sbostic mi->mi_rsp.mri_desc = uda[ctlr].uda_ca.ca_rspdsc; 29232523Sbostic mi->mi_rsp.mri_ring = uda[ctlr].uda_rsp; 29332523Sbostic mi->mi_wtab.av_forw = mi->mi_wtab.av_back = &mi->mi_wtab; 29432523Sbostic 29532523Sbostic /* 29632523Sbostic * More controller specific variables. Again, this should 29732523Sbostic * be in the controller attach routine. 29832523Sbostic */ 29932523Sbostic if (udaburst[ctlr] == 0) 30032523Sbostic udaburst[ctlr] = DEFAULT_BURST; 30132523Sbostic 30232523Sbostic /* 30332523Sbostic * Get an interrupt vector. Note that even if the controller 30432523Sbostic * does not respond, we keep the vector. This is not a serious 30532523Sbostic * problem; but it would be easily fixed if we had a controller 30632523Sbostic * attach routine. Sigh. 30732523Sbostic */ 30832523Sbostic sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4); 30917553Skarels udaddr = (struct udadevice *) reg; 31017553Skarels 31132523Sbostic /* 31232523Sbostic * Initialise the controller (partially). The UDA50 programmer's 31332523Sbostic * manual states that if initialisation fails, it should be retried 31432523Sbostic * at least once, but after a second failure the port should be 31532523Sbostic * considered `down'; it also mentions that the controller should 31632523Sbostic * initialise within ten seconds. Or so I hear; I have not seen 31732523Sbostic * this manual myself. 31832523Sbostic */ 31936036Skarels #ifdef QBA 32036036Skarels s = spl6(); 32136036Skarels #endif 32232523Sbostic tries = 0; 32332523Sbostic again: 32432523Sbostic udaddr->udaip = 0; /* start initialisation */ 32532523Sbostic timeout = todr() + 1000; /* timeout in 10 seconds */ 32632523Sbostic while ((udaddr->udasa & UDA_STEP1) == 0) 32732523Sbostic if (todr() > timeout) 32832523Sbostic goto bad; 32932523Sbostic udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE | 33032523Sbostic (sc->sc_ivec >> 2); 33132523Sbostic while ((udaddr->udasa & UDA_STEP2) == 0) 33232523Sbostic if (todr() > timeout) 33332523Sbostic goto bad; 33432523Sbostic 33532523Sbostic /* should have interrupted by now */ 33635401Stef #ifdef QBA 33736036Skarels sc->sc_ipl = br = qbgetpri(); 33827254Skridle #endif 33932523Sbostic return (sizeof (struct udadevice)); 34032523Sbostic bad: 34132523Sbostic if (++tries < 2) 34232523Sbostic goto again; 34336036Skarels splx(s); 34432523Sbostic return (0); 3454743Swnj } 3464743Swnj 34732523Sbostic /* 34832523Sbostic * Find a slave. We allow wildcard slave numbers (something autoconf 34932523Sbostic * is not really prepared to deal with); and we need to know the 35032523Sbostic * controller number to talk to the UDA. For the latter, we keep 35132523Sbostic * track of the last controller probed, since a controller probe 35232523Sbostic * immediately precedes all slave probes for that controller. For the 35332523Sbostic * former, we simply put the unit number into ui->ui_slave after we 35432523Sbostic * have found one. 35532523Sbostic * 35632523Sbostic * Note that by the time udaslave is called, the interrupt vector 35732523Sbostic * for the UDA50 has been set up (so that udaunconf() will be called). 35832523Sbostic */ 35932523Sbostic udaslave(ui, reg) 36032523Sbostic register struct uba_device *ui; 3614743Swnj caddr_t reg; 3624743Swnj { 36332523Sbostic register struct uba_ctlr *um = probeum; 36432523Sbostic register struct mscp *mp; 36532523Sbostic register struct uda_softc *sc; 36633443Skarels int next = 0, timeout, tries, i; 36717553Skarels 36832523Sbostic #ifdef lint 36932523Sbostic i = 0; i = i; 37032523Sbostic #endif 37132523Sbostic /* 37232523Sbostic * Make sure the controller is fully initialised, by waiting 37332523Sbostic * for it if necessary. 37432523Sbostic */ 37532523Sbostic sc = &uda_softc[um->um_ctlr]; 37632523Sbostic if (sc->sc_state == ST_RUN) 37732523Sbostic goto findunit; 37832523Sbostic tries = 0; 37932523Sbostic again: 38032523Sbostic if (udainit(ui->ui_ctlr)) 38132523Sbostic return (0); 38232523Sbostic timeout = todr() + 1000; /* 10 seconds */ 38332523Sbostic while (todr() < timeout) 38432523Sbostic if (sc->sc_state == ST_RUN) /* made it */ 38532523Sbostic goto findunit; 38632523Sbostic if (++tries < 2) 38732523Sbostic goto again; 38832523Sbostic printf("uda%d: controller hung\n", um->um_ctlr); 38932523Sbostic return (0); 39017553Skarels 39132523Sbostic /* 39232523Sbostic * The controller is all set; go find the unit. Grab an 39332523Sbostic * MSCP packet and send out a Get Unit Status command, with 39432523Sbostic * the `next unit' modifier if we are looking for a generic 39532523Sbostic * unit. We set the `in slave' flag so that udaunconf() 39632523Sbostic * knows to copy the response to `udaslavereply'. 39732523Sbostic */ 39832523Sbostic findunit: 39932523Sbostic udaslavereply.mscp_opcode = 0; 40032523Sbostic sc->sc_flags |= SC_INSLAVE; 40132523Sbostic if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) 40232523Sbostic panic("udaslave"); /* `cannot happen' */ 40332523Sbostic mp->mscp_opcode = M_OP_GETUNITST; 40432523Sbostic if (ui->ui_slave == '?') { 40532523Sbostic mp->mscp_unit = next; 40632523Sbostic mp->mscp_modifier = M_GUM_NEXTUNIT; 40732523Sbostic } else { 40832523Sbostic mp->mscp_unit = ui->ui_slave; 40932523Sbostic mp->mscp_modifier = 0; 41017553Skarels } 41132523Sbostic *mp->mscp_addr |= MSCP_OWN | MSCP_INT; 41232523Sbostic i = ((struct udadevice *) reg)->udaip; /* initiate polling */ 41332523Sbostic mp = &udaslavereply; 41432523Sbostic timeout = todr() + 1000; 41532523Sbostic while (todr() < timeout) 41632523Sbostic if (mp->mscp_opcode) 41732523Sbostic goto gotit; 41832523Sbostic printf("uda%d: no response to Get Unit Status request\n", 41932523Sbostic um->um_ctlr); 42032523Sbostic sc->sc_flags &= ~SC_INSLAVE; 42132523Sbostic return (0); 42232523Sbostic 42332523Sbostic gotit: 42432523Sbostic sc->sc_flags &= ~SC_INSLAVE; 42532523Sbostic 42632523Sbostic /* 42732523Sbostic * Got a slave response. If the unit is there, use it. 42832523Sbostic */ 42932523Sbostic switch (mp->mscp_status & M_ST_MASK) { 43032523Sbostic 43132523Sbostic case M_ST_SUCCESS: /* worked */ 43232523Sbostic case M_ST_AVAILABLE: /* found another drive */ 43332523Sbostic break; /* use it */ 43432523Sbostic 43532523Sbostic case M_ST_OFFLINE: 43632523Sbostic /* 43732523Sbostic * Figure out why it is off line. It may be because 43832523Sbostic * it is nonexistent, or because it is spun down, or 43932523Sbostic * for some other reason. 44032523Sbostic */ 44132523Sbostic switch (mp->mscp_status & ~M_ST_MASK) { 44232523Sbostic 44332523Sbostic case M_OFFLINE_UNKNOWN: 44432523Sbostic /* 44532523Sbostic * No such drive, and there are none with 44632523Sbostic * higher unit numbers either, if we are 44732523Sbostic * using M_GUM_NEXTUNIT. 44832523Sbostic */ 44932523Sbostic return (0); 45032523Sbostic 45132523Sbostic case M_OFFLINE_UNMOUNTED: 45232523Sbostic /* 45332523Sbostic * The drive is not spun up. Use it anyway. 45432523Sbostic * 45532523Sbostic * N.B.: this seems to be a common occurrance 45632523Sbostic * after a power failure. The first attempt 45732523Sbostic * to bring it on line seems to spin it up 45832523Sbostic * (and thus takes several minutes). Perhaps 45932523Sbostic * we should note here that the on-line may 46032523Sbostic * take longer than usual. 46132523Sbostic */ 46232523Sbostic break; 46332523Sbostic 46432523Sbostic default: 46532523Sbostic /* 46632523Sbostic * In service, or something else equally unusable. 46732523Sbostic */ 46832523Sbostic printf("uda%d: unit %d off line: ", um->um_ctlr, 46932523Sbostic mp->mscp_unit); 47032523Sbostic mscp_printevent(mp); 47132523Sbostic goto try_another; 47232523Sbostic } 47332523Sbostic break; 47432523Sbostic 47532523Sbostic default: 47632523Sbostic printf("uda%d: unable to get unit status: ", um->um_ctlr); 47732523Sbostic mscp_printevent(mp); 47832523Sbostic return (0); 47917553Skarels } 48032523Sbostic 48132523Sbostic /* 48232523Sbostic * Does this ever happen? What (if anything) does it mean? 48332523Sbostic */ 48432523Sbostic if (mp->mscp_unit < next) { 48532523Sbostic printf("uda%d: unit %d, next %d\n", 48632523Sbostic um->um_ctlr, mp->mscp_unit, next); 48732523Sbostic return (0); 48817553Skarels } 48932523Sbostic 49032523Sbostic if (mp->mscp_unit >= MAXUNIT) { 49132523Sbostic printf("uda%d: cannot handle unit number %d (max is %d)\n", 49232523Sbostic um->um_ctlr, mp->mscp_unit, MAXUNIT - 1); 49332523Sbostic return (0); 49432523Sbostic } 49532523Sbostic 49632523Sbostic /* 49732523Sbostic * See if we already handle this drive. 49832523Sbostic * (Only likely if ui->ui_slave=='?'.) 49932523Sbostic */ 50032523Sbostic if (udaip[um->um_ctlr][mp->mscp_unit] != NULL) { 50132523Sbostic try_another: 50232523Sbostic if (ui->ui_slave != '?') 50332523Sbostic return (0); 50432523Sbostic next = mp->mscp_unit + 1; 50532523Sbostic goto findunit; 50632523Sbostic } 50732523Sbostic 50832523Sbostic /* 50932523Sbostic * Voila! 51032523Sbostic */ 51132523Sbostic uda_rasave(ui->ui_unit, mp, 0); 51232523Sbostic ui->ui_flags = 0; /* not on line, nor anything else */ 51332523Sbostic ui->ui_slave = mp->mscp_unit; 51432523Sbostic return (1); 5154743Swnj } 5164743Swnj 51732523Sbostic /* 51832523Sbostic * Attach a found slave. Make sure the watchdog timer is running. 51940045Smarc * If this disk is being profiled, fill in the `wpms' value (used by 52032523Sbostic * what?). Set up the inverting pointer, and attempt to bring the 52132523Sbostic * drive on line and read its label. 52232523Sbostic */ 52332523Sbostic udaattach(ui) 5244743Swnj register struct uba_device *ui; 5254743Swnj { 52632523Sbostic register int unit = ui->ui_unit; 52732523Sbostic 52832523Sbostic if (udawstart == 0) { 52932523Sbostic timeout(udawatch, (caddr_t) 0, hz); 53032523Sbostic udawstart++; 53132523Sbostic } 53233444Sbostic 53333444Sbostic /* 53433444Sbostic * Floppies cannot be brought on line unless there is 53533444Sbostic * a disk in the drive. Since an ONLINE while cold 53633444Sbostic * takes ten seconds to fail, and (when notyet becomes now) 53733444Sbostic * no sensible person will swap to one, we just 53833444Sbostic * defer the ONLINE until someone tries to use the drive. 53933444Sbostic * 54033444Sbostic * THIS ASSUMES THAT DRIVE TYPES ?X? ARE FLOPPIES 54133444Sbostic */ 54233444Sbostic if (MSCP_MID_ECH(1, ra_info[unit].ra_mediaid) == 'X' - '@') { 54334283Skarels printf(": floppy"); 54433444Sbostic return; 54533444Sbostic } 54634649Skarels if (ui->ui_dk >= 0) 54740045Smarc dk_wpms[ui->ui_dk] = (60 * 31 * 256); /* approx */ 54832523Sbostic udaip[ui->ui_ctlr][ui->ui_slave] = ui; 54933195Sbostic 55032523Sbostic if (uda_rainit(ui, 0)) 55134283Skarels printf(": offline"); 55236036Skarels else if (ra_info[unit].ra_state == OPEN) { 55334283Skarels printf(": %s, size = %d sectors", 55434283Skarels udalabel[unit].d_typename, ra_info[unit].ra_dsize); 55532523Sbostic #ifdef notyet 55632523Sbostic addswap(makedev(UDADEVNUM, udaminor(unit, 0)), &udalabel[unit]); 55726295Skarels #endif 55830536Skarels } 55932523Sbostic } 56032523Sbostic 56132523Sbostic /* 56232523Sbostic * Initialise a UDA50. Return true iff something goes wrong. 56332523Sbostic */ 56432523Sbostic udainit(ctlr) 56532523Sbostic int ctlr; 56632523Sbostic { 56732523Sbostic register struct uda_softc *sc; 56832523Sbostic register struct udadevice *udaddr; 56932523Sbostic struct uba_ctlr *um; 57032523Sbostic int timo, ubinfo; 57132523Sbostic 57232523Sbostic sc = &uda_softc[ctlr]; 57332523Sbostic um = udaminfo[ctlr]; 57432523Sbostic if ((sc->sc_flags & SC_MAPPED) == 0) { 57532523Sbostic /* 57632523Sbostic * Map the communication area and command and 57732523Sbostic * response packets into Unibus space. 57832523Sbostic */ 57932523Sbostic ubinfo = uballoc(um->um_ubanum, (caddr_t) &uda[ctlr], 58032523Sbostic sizeof (struct uda), UBA_CANTWAIT); 58132523Sbostic if (ubinfo == 0) { 58232523Sbostic printf("uda%d: uballoc map failed\n", ctlr); 58332523Sbostic return (-1); 58432523Sbostic } 58536036Skarels sc->sc_uda = (struct uda *) UBAI_ADDR(ubinfo); 58632523Sbostic sc->sc_flags |= SC_MAPPED; 58732523Sbostic } 58832523Sbostic 58930536Skarels /* 59032523Sbostic * While we are thinking about it, reset the next command 59132523Sbostic * and response indicies. 59230536Skarels */ 59332523Sbostic sc->sc_mi.mi_cmd.mri_next = 0; 59432523Sbostic sc->sc_mi.mi_rsp.mri_next = 0; 59532523Sbostic 59632523Sbostic /* 59732523Sbostic * Start up the hardware initialisation sequence. 59832523Sbostic */ 59932523Sbostic #define STEP0MASK (UDA_ERR | UDA_STEP4 | UDA_STEP3 | UDA_STEP2 | \ 60032523Sbostic UDA_STEP1 | UDA_NV) 60132523Sbostic 60232523Sbostic sc->sc_state = ST_IDLE; /* in case init fails */ 60333444Sbostic udaddr = (struct udadevice *)um->um_addr; 60432523Sbostic udaddr->udaip = 0; 60532523Sbostic timo = todr() + 1000; 60632523Sbostic while ((udaddr->udasa & STEP0MASK) == 0) { 60732523Sbostic if (todr() > timo) { 60832523Sbostic printf("uda%d: timeout during init\n", ctlr); 60932523Sbostic return (-1); 61032523Sbostic } 61132523Sbostic } 61232523Sbostic if ((udaddr->udasa & STEP0MASK) != UDA_STEP1) { 61332523Sbostic printf("uda%d: init failed, sa=%b\n", ctlr, 61432523Sbostic udaddr->udasa, udasr_bits); 61533444Sbostic udasaerror(um, 0); 61632523Sbostic return (-1); 61732523Sbostic } 61832523Sbostic 61932523Sbostic /* 62032523Sbostic * Success! Record new state, and start step 1 initialisation. 62132523Sbostic * The rest is done in the interrupt handler. 62232523Sbostic */ 62332523Sbostic sc->sc_state = ST_STEP1; 62432523Sbostic udaddr->udasa = UDA_ERR | (NCMDL2 << 11) | (NRSPL2 << 8) | UDA_IE | 62532523Sbostic (sc->sc_ivec >> 2); 62632523Sbostic return (0); 6274743Swnj } 6284743Swnj 6294743Swnj /* 63032523Sbostic * Open a drive. 6314743Swnj */ 63232523Sbostic /*ARGSUSED*/ 63332523Sbostic udaopen(dev, flag, fmt) 6344743Swnj dev_t dev; 63530773Skarels int flag, fmt; 6364743Swnj { 63732523Sbostic register int unit; 6384743Swnj register struct uba_device *ui; 6394743Swnj register struct uda_softc *sc; 64030536Skarels register struct disklabel *lp; 64130536Skarels register struct partition *pp; 64230916Skarels register struct ra_info *ra; 64332523Sbostic int s, i, part, mask, error = 0; 64430536Skarels daddr_t start, end; 64530536Skarels 64632523Sbostic /* 64732523Sbostic * Make sure this is a reasonable open request. 64832523Sbostic */ 64932523Sbostic unit = udaunit(dev); 65032523Sbostic if (unit >= NRA || (ui = udadinfo[unit]) == 0 || ui->ui_alive == 0) 6518576Sroot return (ENXIO); 65232523Sbostic 65332523Sbostic /* 65432523Sbostic * Make sure the controller is running, by (re)initialising it if 65532523Sbostic * necessary. 65632523Sbostic */ 6574743Swnj sc = &uda_softc[ui->ui_ctlr]; 6585434Sroot s = spl5(); 65932523Sbostic if (sc->sc_state != ST_RUN) { 66032523Sbostic if (sc->sc_state == ST_IDLE && udainit(ui->ui_ctlr)) { 66132523Sbostic splx(s); 6628576Sroot return (EIO); 66317553Skarels } 66432523Sbostic /* 66532523Sbostic * In case it does not come up, make sure we will be 66632523Sbostic * restarted in 10 seconds. This corresponds to the 66732523Sbostic * 10 second timeouts in udaprobe() and udaslave(). 66832523Sbostic */ 66932523Sbostic sc->sc_flags |= SC_DOWAKE; 67032523Sbostic timeout(wakeup, (caddr_t) sc, 10 * hz); 67132523Sbostic sleep((caddr_t) sc, PRIBIO); 67232523Sbostic if (sc->sc_state != ST_RUN) { 67332523Sbostic splx(s); 67432523Sbostic printf("uda%d: controller hung\n", ui->ui_ctlr); 67532523Sbostic return (EIO); 67632523Sbostic } 67732523Sbostic untimeout(wakeup, (caddr_t) sc); 6784743Swnj } 67932523Sbostic 68032523Sbostic /* 68132523Sbostic * Wait for the state to settle 68232523Sbostic */ 68332523Sbostic ra = &ra_info[unit]; 68432523Sbostic while (ra->ra_state != OPEN && ra->ra_state != OPENRAW && 68532523Sbostic ra->ra_state != CLOSED) 686*40726Skarels if (error = tsleep((caddr_t)ra, (PZERO + 1) | PCATCH, 687*40726Skarels devopn, 0)) { 688*40726Skarels splx(s); 689*40726Skarels return (error); 690*40726Skarels } 69132523Sbostic 69232523Sbostic /* 69332523Sbostic * If not on line, or we are not sure of the label, reinitialise 69432523Sbostic * the drive. 69532523Sbostic */ 69632523Sbostic if ((ui->ui_flags & UNIT_ONLINE) == 0 || 69732523Sbostic (ra->ra_state != OPEN && ra->ra_state != OPENRAW)) 69832523Sbostic error = uda_rainit(ui, flag); 69930916Skarels splx(s); 70032523Sbostic if (error) 70132523Sbostic return (error); 70230536Skarels 70332523Sbostic part = udapart(dev); 70432523Sbostic lp = &udalabel[unit]; 70530536Skarels if (part >= lp->d_npartitions) 70630536Skarels return (ENXIO); 70730536Skarels /* 70832523Sbostic * Warn if a partition is opened that overlaps another 70932523Sbostic * already open, unless either is the `raw' partition 71032523Sbostic * (whole disk). 71130536Skarels */ 71232523Sbostic #define RAWPART 2 /* 'c' partition */ /* XXX */ 71332523Sbostic mask = 1 << part; 71432523Sbostic if ((ra->ra_openpart & mask) == 0 && part != RAWPART) { 71530536Skarels pp = &lp->d_partitions[part]; 71630536Skarels start = pp->p_offset; 71730536Skarels end = pp->p_offset + pp->p_size; 71832523Sbostic for (pp = lp->d_partitions, i = 0; 71932523Sbostic i < lp->d_npartitions; pp++, i++) { 72030536Skarels if (pp->p_offset + pp->p_size <= start || 72132523Sbostic pp->p_offset >= end || i == RAWPART) 72230536Skarels continue; 72332523Sbostic if (ra->ra_openpart & (1 << i)) 72430536Skarels log(LOG_WARNING, 72530536Skarels "ra%d%c: overlaps open partition (%c)\n", 72632523Sbostic unit, part + 'a', i + 'a'); 72717553Skarels } 72817553Skarels } 72930773Skarels switch (fmt) { 73030773Skarels case S_IFCHR: 73132523Sbostic ra->ra_copenpart |= mask; 73230773Skarels break; 73330773Skarels case S_IFBLK: 73432523Sbostic ra->ra_bopenpart |= mask; 73530773Skarels break; 73630773Skarels } 73732523Sbostic ra->ra_openpart |= mask; 7388576Sroot return (0); 7394743Swnj } 7404743Swnj 74133444Sbostic /* ARGSUSED */ 74232523Sbostic udaclose(dev, flags, fmt) 74330536Skarels dev_t dev; 74430773Skarels int flags, fmt; 74530536Skarels { 74632523Sbostic register int unit = udaunit(dev); 74730773Skarels register struct ra_info *ra = &ra_info[unit]; 74832523Sbostic int s, mask = (1 << udapart(dev)); 74930536Skarels 75030773Skarels switch (fmt) { 75130773Skarels case S_IFCHR: 75232523Sbostic ra->ra_copenpart &= ~mask; 75330773Skarels break; 75430773Skarels case S_IFBLK: 75532523Sbostic ra->ra_bopenpart &= ~mask; 75630773Skarels break; 75730773Skarels } 75832523Sbostic ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart; 75932523Sbostic 76030536Skarels /* 76132523Sbostic * Should wait for I/O to complete on this partition even if 76232523Sbostic * others are open, but wait for work on blkflush(). 76330536Skarels */ 76432523Sbostic if (ra->ra_openpart == 0) { 76530536Skarels s = spl5(); 76632523Sbostic while (udautab[unit].b_actf) 76732523Sbostic sleep((caddr_t)&udautab[unit], PZERO - 1); 76830536Skarels splx(s); 76932523Sbostic ra->ra_state = CLOSED; 77033443Skarels ra->ra_wlabel = 0; 77130536Skarels } 77230773Skarels return (0); 77330536Skarels } 77430536Skarels 7754743Swnj /* 77632523Sbostic * Initialise a drive. If it is not already, bring it on line, 77732523Sbostic * and set a timeout on it in case it fails to respond. 77832523Sbostic * When on line, read in the pack label. 7794743Swnj */ 78032523Sbostic uda_rainit(ui, flags) 78130536Skarels register struct uba_device *ui; 78232523Sbostic int flags; 78330536Skarels { 78432523Sbostic register struct uda_softc *sc = &uda_softc[ui->ui_ctlr]; 78534283Skarels register struct disklabel *lp; 78630536Skarels register struct mscp *mp; 78732523Sbostic register int unit = ui->ui_unit; 78832523Sbostic register struct ra_info *ra; 78930773Skarels char *msg, *readdisklabel(); 79032523Sbostic int s, i, udastrategy(); 79130536Skarels extern int cold; 79230536Skarels 79332523Sbostic ra = &ra_info[unit]; 79432523Sbostic if ((ui->ui_flags & UNIT_ONLINE) == 0) { 79532523Sbostic mp = mscp_getcp(&sc->sc_mi, MSCP_WAIT); 79632523Sbostic mp->mscp_opcode = M_OP_ONLINE; 79732523Sbostic mp->mscp_unit = ui->ui_slave; 79832523Sbostic mp->mscp_cmdref = (long)&ui->ui_flags; 79932523Sbostic *mp->mscp_addr |= MSCP_OWN | MSCP_INT; 80032523Sbostic ra->ra_state = WANTOPEN; 80132523Sbostic if (!cold) 80232523Sbostic s = spl5(); 80332523Sbostic i = ((struct udadevice *)ui->ui_addr)->udaip; 80430536Skarels 80530916Skarels if (cold) { 80632523Sbostic i = todr() + 1000; 80732523Sbostic while ((ui->ui_flags & UNIT_ONLINE) == 0) 80832523Sbostic if (todr() > i) 80932523Sbostic break; 81030916Skarels } else { 81132523Sbostic timeout(wakeup, (caddr_t)&ui->ui_flags, 10 * hz); 81232523Sbostic sleep((caddr_t)&ui->ui_flags, PSWP + 1); 81332523Sbostic splx(s); 81432523Sbostic untimeout(wakeup, (caddr_t)&ui->ui_flags); 81530916Skarels } 81632523Sbostic if (ra->ra_state != OPENRAW) { 81732523Sbostic ra->ra_state = CLOSED; 81832523Sbostic wakeup((caddr_t)ra); 81930916Skarels return (EIO); 82030916Skarels } 82130536Skarels } 82230536Skarels 82332523Sbostic lp = &udalabel[unit]; 82430916Skarels lp->d_secsize = DEV_BSIZE; 82532523Sbostic lp->d_secperunit = ra->ra_dsize; 82630916Skarels 82730536Skarels if (flags & O_NDELAY) 82830536Skarels return (0); 82932523Sbostic ra->ra_state = RDLABEL; 83030536Skarels /* 83132523Sbostic * Set up default sizes until we have the label, or longer 83232523Sbostic * if there is none. Set secpercyl, as readdisklabel wants 83338979Skarels * to compute b_cylin (although we do not need it), and set 83438979Skarels * nsectors in case diskerr is called. 83530536Skarels */ 83630916Skarels lp->d_secpercyl = 1; 83730536Skarels lp->d_npartitions = 1; 83836036Skarels lp->d_secsize = 512; 83936036Skarels lp->d_secperunit = ra->ra_dsize; 84038979Skarels lp->d_nsectors = ra->ra_geom.rg_nsectors; 84130536Skarels lp->d_partitions[0].p_size = lp->d_secperunit; 84230536Skarels lp->d_partitions[0].p_offset = 0; 84332523Sbostic 84430536Skarels /* 84530536Skarels * Read pack label. 84630536Skarels */ 84732523Sbostic if ((msg = readdisklabel(udaminor(unit, 0), udastrategy, lp)) != NULL) { 84834283Skarels if (cold) 84934283Skarels printf(": %s", msg); 85034283Skarels else 85136036Skarels log(LOG_ERR, "ra%d: %s", unit, msg); 85230536Skarels #ifdef COMPAT_42 85332523Sbostic if (udamaptype(unit, lp)) 85432523Sbostic ra->ra_state = OPEN; 85530536Skarels else 85632523Sbostic ra->ra_state = OPENRAW; 85731022Skarels #else 85832523Sbostic ra->ra_state = OPENRAW; 85936036Skarels uda_makefakelabel(ra, lp); 86030536Skarels #endif 86130773Skarels } else 86232523Sbostic ra->ra_state = OPEN; 86330916Skarels wakeup((caddr_t)ra); 86430916Skarels return (0); 86530536Skarels } 86630536Skarels 86732523Sbostic /* 86832523Sbostic * Copy the geometry information for the given ra from a 86932523Sbostic * GET UNIT STATUS response. If check, see if it changed. 87032523Sbostic */ 87132523Sbostic uda_rasave(unit, mp, check) 87232523Sbostic int unit; 87332523Sbostic register struct mscp *mp; 87432523Sbostic int check; 87532523Sbostic { 87632523Sbostic register struct ra_info *ra = &ra_info[unit]; 87732523Sbostic 87834283Skarels if (check && ra->ra_mediaid != mp->mscp_guse.guse_mediaid) { 87933443Skarels printf("ra%d: changed types! was %d now %d\n", unit, 88034283Skarels ra->ra_mediaid, mp->mscp_guse.guse_mediaid); 88132523Sbostic ra->ra_state = CLOSED; /* ??? */ 88232523Sbostic } 88333444Sbostic /* ra->ra_type = mp->mscp_guse.guse_drivetype; */ 88432523Sbostic ra->ra_mediaid = mp->mscp_guse.guse_mediaid; 88532523Sbostic ra->ra_geom.rg_nsectors = mp->mscp_guse.guse_nspt; 88632523Sbostic ra->ra_geom.rg_ngroups = mp->mscp_guse.guse_group; 88732523Sbostic ra->ra_geom.rg_ngpc = mp->mscp_guse.guse_ngpc; 88832523Sbostic ra->ra_geom.rg_ntracks = ra->ra_geom.rg_ngroups * ra->ra_geom.rg_ngpc; 88932523Sbostic /* ra_geom.rg_ncyl cannot be computed until we have ra_dsize */ 89032523Sbostic #ifdef notyet 89132523Sbostic ra->ra_geom.rg_rctsize = mp->mscp_guse.guse_rctsize; 89232523Sbostic ra->ra_geom.rg_rbns = mp->mscp_guse.guse_nrpt; 89332523Sbostic ra->ra_geom.rg_nrct = mp->mscp_guse.guse_nrct; 89432523Sbostic #endif 89532523Sbostic } 89632523Sbostic 89732523Sbostic /* 89832523Sbostic * Queue a transfer request, and if possible, hand it to the controller. 89932523Sbostic * 90032523Sbostic * This routine is broken into two so that the internal version 90132523Sbostic * udastrat1() can be called by the (nonexistent, as yet) bad block 90232523Sbostic * revectoring routine. 90332523Sbostic */ 90432523Sbostic udastrategy(bp) 9054743Swnj register struct buf *bp; 9064743Swnj { 90732523Sbostic register int unit; 9084743Swnj register struct uba_device *ui; 90932523Sbostic register struct ra_info *ra; 91032523Sbostic struct partition *pp; 91132523Sbostic int p; 9124743Swnj daddr_t sz, maxsz; 9134743Swnj 91432523Sbostic /* 91532523Sbostic * Make sure this is a reasonable drive to use. 91632523Sbostic */ 91732523Sbostic if ((unit = udaunit(bp->b_dev)) >= NRA || 91832523Sbostic (ui = udadinfo[unit]) == NULL || ui->ui_alive == 0 || 91932523Sbostic (ra = &ra_info[unit])->ra_state == CLOSED) { 92024742Sbloom bp->b_error = ENXIO; 9214743Swnj goto bad; 92224742Sbloom } 92332523Sbostic 92432523Sbostic /* 92532523Sbostic * If drive is open `raw' or reading label, let it at it. 92632523Sbostic */ 92732523Sbostic if (ra->ra_state < OPEN) { 92832523Sbostic udastrat1(bp); 92932523Sbostic return; 93024742Sbloom } 93132523Sbostic p = udapart(bp->b_dev); 93233443Skarels if ((ra->ra_openpart & (1 << p)) == 0) { 93333443Skarels bp->b_error = ENODEV; 93433443Skarels goto bad; 93533443Skarels } 93632523Sbostic 93732523Sbostic /* 93832523Sbostic * Determine the size of the transfer, and make sure it is 93932523Sbostic * within the boundaries of the partition. 94032523Sbostic */ 94132523Sbostic pp = &udalabel[unit].d_partitions[p]; 94232523Sbostic maxsz = pp->p_size; 94332523Sbostic if (pp->p_offset + pp->p_size > ra->ra_dsize) 94432523Sbostic maxsz = ra->ra_dsize - pp->p_offset; 94530536Skarels sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 94633443Skarels if (bp->b_blkno + pp->p_offset <= LABELSECTOR && 94733443Skarels #if LABELSECTOR != 0 94833443Skarels bp->b_blkno + pp->p_offset + sz > LABELSECTOR && 94933443Skarels #endif 95033443Skarels (bp->b_flags & B_READ) == 0 && ra->ra_wlabel == 0) { 95133443Skarels bp->b_error = EROFS; 95233443Skarels goto bad; 95333443Skarels } 95430536Skarels if (bp->b_blkno < 0 || bp->b_blkno + sz > maxsz) { 95532523Sbostic /* if exactly at end of disk, return an EOF */ 95624787Skarels if (bp->b_blkno == maxsz) { 95724787Skarels bp->b_resid = bp->b_bcount; 95832523Sbostic biodone(bp); 95932523Sbostic return; 96024787Skarels } 96132523Sbostic /* or truncate if part of it fits */ 96230536Skarels sz = maxsz - bp->b_blkno; 96330536Skarels if (sz <= 0) { 96432523Sbostic bp->b_error = EINVAL; /* or hang it up */ 96530536Skarels goto bad; 96630536Skarels } 96730536Skarels bp->b_bcount = sz << DEV_BSHIFT; 96824742Sbloom } 96932523Sbostic udastrat1(bp); 97032523Sbostic return; 97132523Sbostic bad: 97232523Sbostic bp->b_flags |= B_ERROR; 97332523Sbostic biodone(bp); 97432523Sbostic } 97532523Sbostic 97632523Sbostic /* 97732523Sbostic * Work routine for udastrategy. 97832523Sbostic */ 97932523Sbostic udastrat1(bp) 98032523Sbostic register struct buf *bp; 98132523Sbostic { 98232523Sbostic register int unit = udaunit(bp->b_dev); 98332523Sbostic register struct uba_ctlr *um; 98432523Sbostic register struct buf *dp; 98532523Sbostic struct uba_device *ui; 98632523Sbostic int s = spl5(); 98732523Sbostic 9884743Swnj /* 98932523Sbostic * Append the buffer to the drive queue, and if it is not 99032523Sbostic * already there, the drive to the controller queue. (However, 99132523Sbostic * if the drive queue is marked to be requeued, we must be 99232523Sbostic * awaiting an on line or get unit status command; in this 99332523Sbostic * case, leave it off the controller queue.) 9944743Swnj */ 99532523Sbostic um = (ui = udadinfo[unit])->ui_mi; 99632523Sbostic dp = &udautab[unit]; 99732523Sbostic APPEND(bp, dp, av_forw); 99832523Sbostic if (dp->b_active == 0 && (ui->ui_flags & UNIT_REQUEUE) == 0) { 99932523Sbostic APPEND(dp, &um->um_tab, b_forw); 100032523Sbostic dp->b_active++; 100132523Sbostic } 100232523Sbostic 10034743Swnj /* 100432523Sbostic * Start activity on the controller. Note that unlike other 100532523Sbostic * Unibus drivers, we must always do this, not just when the 100632523Sbostic * controller is not active. 10074743Swnj */ 100832523Sbostic udastart(um); 10095434Sroot splx(s); 10104743Swnj } 10114743Swnj 101232523Sbostic /* 101332523Sbostic * Start up whatever transfers we can find. 101432523Sbostic * Note that udastart() must be called at spl5(). 101532523Sbostic */ 101632523Sbostic udastart(um) 10174743Swnj register struct uba_ctlr *um; 10184743Swnj { 101932523Sbostic register struct uda_softc *sc = &uda_softc[um->um_ctlr]; 10204743Swnj register struct buf *bp, *dp; 10214743Swnj register struct mscp *mp; 102232523Sbostic struct uba_device *ui; 10234743Swnj struct udadevice *udaddr; 102432523Sbostic struct partition *pp; 102532523Sbostic int i, sz; 10264743Swnj 102732523Sbostic #ifdef lint 102832523Sbostic i = 0; i = i; 102932523Sbostic #endif 103032523Sbostic /* 103132523Sbostic * If it is not running, try (again and again...) to initialise 103232523Sbostic * it. If it is currently initialising just ignore it for now. 103332523Sbostic */ 103432523Sbostic if (sc->sc_state != ST_RUN) { 103532523Sbostic if (sc->sc_state == ST_IDLE && udainit(um->um_ctlr)) 103632523Sbostic printf("uda%d: still hung\n", um->um_ctlr); 103732523Sbostic return; 103832523Sbostic } 103932523Sbostic 104032523Sbostic /* 104132523Sbostic * If um_cmd is nonzero, this controller is on the Unibus 104232523Sbostic * resource wait queue. It will not help to try more requests; 104332523Sbostic * instead, when the Unibus unblocks and calls udadgo(), we 104432523Sbostic * will call udastart() again. 104532523Sbostic */ 104632523Sbostic if (um->um_cmd) 104732523Sbostic return; 104832523Sbostic 104932523Sbostic sc->sc_flags |= SC_INSTART; 105032523Sbostic udaddr = (struct udadevice *) um->um_addr; 105132523Sbostic 10524743Swnj loop: 105332523Sbostic /* 105432523Sbostic * Service the drive at the head of the queue. It may not 105532523Sbostic * need anything, in which case it might be shutting down 105632523Sbostic * in udaclose(). 105732523Sbostic */ 105832523Sbostic if ((dp = um->um_tab.b_actf) == NULL) 105932523Sbostic goto out; 10604743Swnj if ((bp = dp->b_actf) == NULL) { 10614743Swnj dp->b_active = 0; 10624743Swnj um->um_tab.b_actf = dp->b_forw; 106332523Sbostic if (ra_info[dp - udautab].ra_openpart == 0) 106432523Sbostic wakeup((caddr_t)dp); /* finish close protocol */ 106532523Sbostic goto loop; 10664743Swnj } 106732523Sbostic 106832523Sbostic if (udaddr->udasa & UDA_ERR) { /* ctlr fatal error */ 106933444Sbostic udasaerror(um, 1); 107032523Sbostic goto out; 10714743Swnj } 107232523Sbostic 107332523Sbostic /* 107432523Sbostic * Get an MSCP packet, then figure out what to do. If 107532523Sbostic * we cannot get a command packet, the command ring may 107632523Sbostic * be too small: We should have at least as many command 107732523Sbostic * packets as credits, for best performance. 107832523Sbostic */ 107932523Sbostic if ((mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT)) == NULL) { 108032523Sbostic if (sc->sc_mi.mi_credits > MSCP_MINCREDITS && 108132523Sbostic (sc->sc_flags & SC_GRIPED) == 0) { 108232523Sbostic log(LOG_NOTICE, "uda%d: command ring too small\n", 108332523Sbostic um->um_ctlr); 108432523Sbostic sc->sc_flags |= SC_GRIPED;/* complain only once */ 108517553Skarels } 108632523Sbostic goto out; 10874743Swnj } 10884743Swnj 108932523Sbostic /* 109032523Sbostic * Bring the drive on line if it is not already. Get its status 109132523Sbostic * if we do not already have it. Otherwise just start the transfer. 109232523Sbostic */ 109332523Sbostic ui = udadinfo[udaunit(bp->b_dev)]; 109432523Sbostic if ((ui->ui_flags & UNIT_ONLINE) == 0) { 109532523Sbostic mp->mscp_opcode = M_OP_ONLINE; 109632523Sbostic goto common; 10974743Swnj } 109832523Sbostic if ((ui->ui_flags & UNIT_HAVESTATUS) == 0) { 109932523Sbostic mp->mscp_opcode = M_OP_GETUNITST; 110032523Sbostic common: 110132523Sbostic if (ui->ui_flags & UNIT_REQUEUE) panic("udastart"); 110232523Sbostic /* 110332523Sbostic * Take the drive off the controller queue. When the 110432523Sbostic * command finishes, make sure the drive is requeued. 110532523Sbostic */ 110632523Sbostic um->um_tab.b_actf = dp->b_forw; 110732523Sbostic dp->b_active = 0; 110832523Sbostic ui->ui_flags |= UNIT_REQUEUE; 110932523Sbostic mp->mscp_unit = ui->ui_slave; 111032523Sbostic *mp->mscp_addr |= MSCP_OWN | MSCP_INT; 111132523Sbostic sc->sc_flags |= SC_STARTPOLL; 111232523Sbostic #ifdef POLLSTATS 111332523Sbostic sc->sc_ncmd++; 111425653Skarels #endif 111532523Sbostic goto loop; 111617553Skarels } 111732523Sbostic 111832523Sbostic pp = &udalabel[ui->ui_unit].d_partitions[udapart(bp->b_dev)]; 111932523Sbostic mp->mscp_opcode = (bp->b_flags & B_READ) ? M_OP_READ : M_OP_WRITE; 11204743Swnj mp->mscp_unit = ui->ui_slave; 112132523Sbostic mp->mscp_seq.seq_lbn = bp->b_blkno + pp->p_offset; 112230536Skarels sz = (bp->b_bcount + DEV_BSIZE - 1) >> DEV_BSHIFT; 112332523Sbostic mp->mscp_seq.seq_bytecount = bp->b_blkno + sz > pp->p_size ? 112432523Sbostic (pp->p_size - bp->b_blkno) >> DEV_BSHIFT : bp->b_bcount; 112532523Sbostic /* mscp_cmdref is filled in by mscp_go() */ 11264743Swnj 11274743Swnj /* 112832523Sbostic * Drop the packet pointer into the `command' field so udadgo() 112932523Sbostic * can tell what to start. If ubago returns 1, we can do another 113032523Sbostic * transfer. If not, um_cmd will still point at mp, so we will 113132523Sbostic * know that we are waiting for resources. 11324743Swnj */ 113332523Sbostic um->um_cmd = (int)mp; 113432523Sbostic if (ubago(ui)) 113532523Sbostic goto loop; 113632523Sbostic 113732523Sbostic /* 113832523Sbostic * All done, or blocked in ubago(). If we managed to 113932523Sbostic * issue some commands, start up the beast. 114032523Sbostic */ 114132523Sbostic out: 114232523Sbostic if (sc->sc_flags & SC_STARTPOLL) { 114332523Sbostic #ifdef POLLSTATS 114432523Sbostic udastats.cmd[sc->sc_ncmd]++; 114532523Sbostic sc->sc_ncmd = 0; 114632523Sbostic #endif 114733444Sbostic i = ((struct udadevice *)um->um_addr)->udaip; 11484743Swnj } 114932523Sbostic sc->sc_flags &= ~(SC_INSTART | SC_STARTPOLL); 115032523Sbostic } 115132523Sbostic 115232523Sbostic /* 115332523Sbostic * Start a transfer. 115432523Sbostic * 115532523Sbostic * If we are not called from within udastart(), we must have been 115632523Sbostic * blocked, so call udastart to do more requests (if any). If 115732523Sbostic * this calls us again immediately we will not recurse, because 115832523Sbostic * that time we will be in udastart(). Clever.... 115932523Sbostic */ 116032523Sbostic udadgo(um) 116132523Sbostic register struct uba_ctlr *um; 116232523Sbostic { 116332523Sbostic struct uda_softc *sc = &uda_softc[um->um_ctlr]; 116432523Sbostic struct mscp *mp = (struct mscp *)um->um_cmd; 116532523Sbostic 116632523Sbostic um->um_tab.b_active++; /* another transfer going */ 116732523Sbostic 11684743Swnj /* 116932523Sbostic * Fill in the MSCP packet and move the buffer to the 117032523Sbostic * I/O wait queue. Mark the controller as no longer on 117132523Sbostic * the resource queue, and remember to initiate polling. 11724743Swnj */ 117336036Skarels mp->mscp_seq.seq_buffer = UBAI_ADDR(um->um_ubinfo) | 117432523Sbostic (UBAI_BDP(um->um_ubinfo) << 24); 117532523Sbostic mscp_go(&sc->sc_mi, mp, um->um_ubinfo); 117632523Sbostic um->um_cmd = 0; 117732523Sbostic um->um_ubinfo = 0; /* tyke it awye */ 117832523Sbostic sc->sc_flags |= SC_STARTPOLL; 117932523Sbostic #ifdef POLLSTATS 118032523Sbostic sc->sc_ncmd++; 118132523Sbostic #endif 118232523Sbostic if ((sc->sc_flags & SC_INSTART) == 0) 118332523Sbostic udastart(um); 11844743Swnj } 11854743Swnj 118632523Sbostic udaiodone(mi, bp, info) 118732523Sbostic register struct mscp_info *mi; 118832523Sbostic struct buf *bp; 118932523Sbostic int info; 119032523Sbostic { 119132523Sbostic register struct uba_ctlr *um = udaminfo[mi->mi_ctlr]; 119232523Sbostic 119332523Sbostic um->um_ubinfo = info; 119432523Sbostic ubadone(um); 119532523Sbostic biodone(bp); 119632523Sbostic if (um->um_bdp && mi->mi_wtab.av_forw == &mi->mi_wtab) 119732523Sbostic ubarelse(um->um_ubanum, &um->um_bdp); 119832523Sbostic um->um_tab.b_active--; /* another transfer done */ 119932523Sbostic } 120032523Sbostic 120133444Sbostic static struct saerr { 120233444Sbostic int code; /* error code (including UDA_ERR) */ 120333444Sbostic char *desc; /* what it means: Efoo => foo error */ 120433444Sbostic } saerr[] = { 120533444Sbostic { 0100001, "Eunibus packet read" }, 120633444Sbostic { 0100002, "Eunibus packet write" }, 120733444Sbostic { 0100003, "EUDA ROM and RAM parity" }, 120833444Sbostic { 0100004, "EUDA RAM parity" }, 120933444Sbostic { 0100005, "EUDA ROM parity" }, 121033444Sbostic { 0100006, "Eunibus ring read" }, 121133444Sbostic { 0100007, "Eunibus ring write" }, 121233444Sbostic { 0100010, " unibus interrupt master failure" }, 121333444Sbostic { 0100011, "Ehost access timeout" }, 121433444Sbostic { 0100012, " host exceeded command limit" }, 121533444Sbostic { 0100013, " unibus bus master failure" }, 121633444Sbostic { 0100014, " DM XFC fatal error" }, 121733444Sbostic { 0100015, " hardware timeout of instruction loop" }, 121833444Sbostic { 0100016, " invalid virtual circuit id" }, 121933444Sbostic { 0100017, "Eunibus interrupt write" }, 122033444Sbostic { 0104000, "Efatal sequence" }, 122133444Sbostic { 0104040, " D proc ALU" }, 122233444Sbostic { 0104041, "ED proc control ROM parity" }, 122333444Sbostic { 0105102, "ED proc w/no BD#2 or RAM parity" }, 122433444Sbostic { 0105105, "ED proc RAM buffer" }, 122533444Sbostic { 0105152, "ED proc SDI" }, 122633444Sbostic { 0105153, "ED proc write mode wrap serdes" }, 122733444Sbostic { 0105154, "ED proc read mode serdes, RSGEN & ECC" }, 122833444Sbostic { 0106040, "EU proc ALU" }, 122933444Sbostic { 0106041, "EU proc control reg" }, 123033444Sbostic { 0106042, " U proc DFAIL/cntl ROM parity/BD #1 test CNT" }, 123133444Sbostic { 0106047, " U proc const PROM err w/D proc running SDI test" }, 123233444Sbostic { 0106055, " unexpected trap" }, 123333444Sbostic { 0106071, "EU proc const PROM" }, 123433444Sbostic { 0106072, "EU proc control ROM parity" }, 123533444Sbostic { 0106200, "Estep 1 data" }, 123633444Sbostic { 0107103, "EU proc RAM parity" }, 123733444Sbostic { 0107107, "EU proc RAM buffer" }, 123833444Sbostic { 0107115, " test count wrong (BD 12)" }, 123933444Sbostic { 0112300, "Estep 2" }, 124033444Sbostic { 0122240, "ENPR" }, 124133444Sbostic { 0122300, "Estep 3" }, 124233444Sbostic { 0142300, "Estep 4" }, 124333444Sbostic { 0, " unknown error code" } 124433444Sbostic }; 124533444Sbostic 12464743Swnj /* 124733444Sbostic * If the error bit was set in the controller status register, gripe, 124833444Sbostic * then (optionally) reset the controller and requeue pending transfers. 12494743Swnj */ 125033444Sbostic udasaerror(um, doreset) 125132523Sbostic register struct uba_ctlr *um; 125233444Sbostic int doreset; 12534743Swnj { 125433444Sbostic register int code = ((struct udadevice *)um->um_addr)->udasa; 125533444Sbostic register struct saerr *e; 125632523Sbostic 125733444Sbostic if ((code & UDA_ERR) == 0) 125833444Sbostic return; 125933444Sbostic for (e = saerr; e->code; e++) 126033444Sbostic if (e->code == code) 126133444Sbostic break; 126233444Sbostic printf("uda%d: controller error, sa=0%o (%s%s)\n", 126333444Sbostic um->um_ctlr, code, e->desc + 1, 126433444Sbostic *e->desc == 'E' ? " error" : ""); 126533444Sbostic if (doreset) { 126633444Sbostic mscp_requeue(&uda_softc[um->um_ctlr].sc_mi); 126733444Sbostic (void) udainit(um->um_ctlr); 126833444Sbostic } 126932523Sbostic } 127032523Sbostic 127132523Sbostic /* 127232523Sbostic * Interrupt routine. Depending on the state of the controller, 127332523Sbostic * continue initialisation, or acknowledge command and response 127432523Sbostic * interrupts, and process responses. 127532523Sbostic */ 127632523Sbostic udaintr(ctlr) 127732523Sbostic int ctlr; 127832523Sbostic { 127932523Sbostic register struct uba_ctlr *um = udaminfo[ctlr]; 128032523Sbostic register struct uda_softc *sc = &uda_softc[ctlr]; 128133444Sbostic register struct udadevice *udaddr = (struct udadevice *)um->um_addr; 128232523Sbostic register struct uda *ud; 128332523Sbostic register struct mscp *mp; 12844743Swnj register int i; 12854743Swnj 128635401Stef #ifdef QBA 128736036Skarels splx(sc->sc_ipl); /* Qbus interrupt protocol is odd */ 128827254Skridle #endif 128932523Sbostic sc->sc_wticks = 0; /* reset interrupt watchdog */ 129032523Sbostic 129132523Sbostic /* 129232523Sbostic * Combinations during steps 1, 2, and 3: STEPnMASK 129332523Sbostic * corresponds to which bits should be tested; 129432523Sbostic * STEPnGOOD corresponds to the pattern that should 129532523Sbostic * appear after the interrupt from STEPn initialisation. 129632523Sbostic * All steps test the bits in ALLSTEPS. 129732523Sbostic */ 129832523Sbostic #define ALLSTEPS (UDA_ERR|UDA_STEP4|UDA_STEP3|UDA_STEP2|UDA_STEP1) 129932523Sbostic 130032523Sbostic #define STEP1MASK (ALLSTEPS | UDA_IE | UDA_NCNRMASK) 130132523Sbostic #define STEP1GOOD (UDA_STEP2 | UDA_IE | (NCMDL2 << 3) | NRSPL2) 130232523Sbostic 130332523Sbostic #define STEP2MASK (ALLSTEPS | UDA_IE | UDA_IVECMASK) 130432523Sbostic #define STEP2GOOD (UDA_STEP3 | UDA_IE | (sc->sc_ivec >> 2)) 130532523Sbostic 130632523Sbostic #define STEP3MASK ALLSTEPS 130732523Sbostic #define STEP3GOOD UDA_STEP4 130832523Sbostic 13094743Swnj switch (sc->sc_state) { 131032523Sbostic 131132523Sbostic case ST_IDLE: 131232523Sbostic /* 131332523Sbostic * Ignore unsolicited interrupts. 131432523Sbostic */ 131532523Sbostic log(LOG_WARNING, "uda%d: stray intr\n", ctlr); 13164743Swnj return; 13174743Swnj 131832523Sbostic case ST_STEP1: 131932523Sbostic /* 132032523Sbostic * Begin step two initialisation. 132132523Sbostic */ 132232523Sbostic if ((udaddr->udasa & STEP1MASK) != STEP1GOOD) { 132332523Sbostic i = 1; 132432523Sbostic initfailed: 132532523Sbostic printf("uda%d: init step %d failed, sa=%b\n", 132632523Sbostic ctlr, i, udaddr->udasa, udasr_bits); 132733444Sbostic udasaerror(um, 0); 132832523Sbostic sc->sc_state = ST_IDLE; 132932523Sbostic if (sc->sc_flags & SC_DOWAKE) { 133032523Sbostic sc->sc_flags &= ~SC_DOWAKE; 133133444Sbostic wakeup((caddr_t)sc); 133232523Sbostic } 13334743Swnj return; 13344743Swnj } 133533444Sbostic udaddr->udasa = (int)&sc->sc_uda->uda_ca.ca_rspdsc[0] | 133632523Sbostic (cpu == VAX_780 || cpu == VAX_8600 ? UDA_PI : 0); 133732523Sbostic sc->sc_state = ST_STEP2; 13384743Swnj return; 13394743Swnj 134032523Sbostic case ST_STEP2: 134132523Sbostic /* 134232523Sbostic * Begin step 3 initialisation. 134332523Sbostic */ 134432523Sbostic if ((udaddr->udasa & STEP2MASK) != STEP2GOOD) { 134532523Sbostic i = 2; 134632523Sbostic goto initfailed; 13474743Swnj } 134833444Sbostic udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_rspdsc[0]) >> 16; 134932523Sbostic sc->sc_state = ST_STEP3; 13504743Swnj return; 13514743Swnj 135232523Sbostic case ST_STEP3: 135332523Sbostic /* 135432523Sbostic * Set controller characteristics (finish initialisation). 135532523Sbostic */ 135632523Sbostic if ((udaddr->udasa & STEP3MASK) != STEP3GOOD) { 135732523Sbostic i = 3; 135832523Sbostic goto initfailed; 13594743Swnj } 136032523Sbostic i = udaddr->udasa & 0xff; 136132523Sbostic if (i != sc->sc_micro) { 136232523Sbostic sc->sc_micro = i; 136332523Sbostic printf("uda%d: version %d model %d\n", 136432523Sbostic ctlr, i & 0xf, i >> 4); 136532523Sbostic } 136632523Sbostic 136717553Skarels /* 136832523Sbostic * Present the burst size, then remove it. Why this 136932523Sbostic * should be done this way, I have no idea. 137032523Sbostic * 137132523Sbostic * Note that this assumes udaburst[ctlr] > 0. 137217553Skarels */ 137332523Sbostic udaddr->udasa = UDA_GO | (udaburst[ctlr] - 1) << 2; 13744743Swnj udaddr->udasa = UDA_GO; 137532523Sbostic printf("uda%d: DMA burst size set to %d\n", 137632523Sbostic ctlr, udaburst[ctlr]); 13774743Swnj 137832523Sbostic udainitds(ctlr); /* initialise data structures */ 137932523Sbostic 13804743Swnj /* 138132523Sbostic * Before we can get a command packet, we need some 138232523Sbostic * credits. Fake some up to keep mscp_getcp() happy, 138332523Sbostic * get a packet, and cancel all credits (the right 138432523Sbostic * number should come back in the response to the 138532523Sbostic * SCC packet). 13864743Swnj */ 138732523Sbostic sc->sc_mi.mi_credits = MSCP_MINCREDITS + 1; 138832523Sbostic mp = mscp_getcp(&sc->sc_mi, MSCP_DONTWAIT); 138932523Sbostic if (mp == NULL) /* `cannot happen' */ 139032523Sbostic panic("udaintr"); 139132523Sbostic sc->sc_mi.mi_credits = 0; 139232523Sbostic mp->mscp_opcode = M_OP_SETCTLRC; 139332523Sbostic mp->mscp_unit = 0; 139432523Sbostic mp->mscp_sccc.sccc_ctlrflags = M_CF_ATTN | M_CF_MISC | 139532523Sbostic M_CF_THIS; 139632523Sbostic *mp->mscp_addr |= MSCP_OWN | MSCP_INT; 139732523Sbostic i = udaddr->udaip; 139832523Sbostic sc->sc_state = ST_SETCHAR; 13994743Swnj return; 14004743Swnj 140132523Sbostic case ST_SETCHAR: 140232523Sbostic case ST_RUN: 140332523Sbostic /* 140432523Sbostic * Handle Set Ctlr Characteristics responses and operational 140532523Sbostic * responses (via mscp_dorsp). 140632523Sbostic */ 14074743Swnj break; 14084743Swnj 14094743Swnj default: 141032523Sbostic printf("uda%d: driver bug, state %d\n", ctlr, sc->sc_state); 141132523Sbostic panic("udastate"); 14124743Swnj } 14134743Swnj 141432523Sbostic if (udaddr->udasa & UDA_ERR) { /* ctlr fatal error */ 141533444Sbostic udasaerror(um, 1); 141632523Sbostic return; 14174743Swnj } 14184743Swnj 141932523Sbostic ud = &uda[ctlr]; 142032523Sbostic 14214743Swnj /* 142232523Sbostic * Handle buffer purge requests. 14234743Swnj */ 14244743Swnj if (ud->uda_ca.ca_bdp) { 142526372Skarels UBAPURGE(um->um_hd->uh_uba, ud->uda_ca.ca_bdp); 14264743Swnj ud->uda_ca.ca_bdp = 0; 142732523Sbostic udaddr->udasa = 0; /* signal purge complete */ 14284743Swnj } 14294743Swnj 14304743Swnj /* 143132523Sbostic * Check for response and command ring transitions. 14324743Swnj */ 14334743Swnj if (ud->uda_ca.ca_rspint) { 14344743Swnj ud->uda_ca.ca_rspint = 0; 143532523Sbostic mscp_dorsp(&sc->sc_mi); 14364743Swnj } 14374743Swnj if (ud->uda_ca.ca_cmdint) { 14384743Swnj ud->uda_ca.ca_cmdint = 0; 143932523Sbostic MSCP_DOCMD(&sc->sc_mi); 14404743Swnj } 144132523Sbostic udastart(um); 14424743Swnj } 14434743Swnj 14444743Swnj /* 144532523Sbostic * Initialise the various data structures that control the UDA50. 144617553Skarels */ 144732523Sbostic udainitds(ctlr) 144832523Sbostic int ctlr; 144932523Sbostic { 145032523Sbostic register struct uda *ud = &uda[ctlr]; 145132523Sbostic register struct uda *uud = uda_softc[ctlr].sc_uda; 145232523Sbostic register struct mscp *mp; 145332523Sbostic register int i; 14544743Swnj 145532523Sbostic for (i = 0, mp = ud->uda_rsp; i < NRSP; i++, mp++) { 145632523Sbostic ud->uda_ca.ca_rspdsc[i] = MSCP_OWN | MSCP_INT | 145732523Sbostic (long)&uud->uda_rsp[i].mscp_cmdref; 145832523Sbostic mp->mscp_addr = &ud->uda_ca.ca_rspdsc[i]; 145932523Sbostic mp->mscp_msglen = MSCP_MSGLEN; 14604743Swnj } 146132523Sbostic for (i = 0, mp = ud->uda_cmd; i < NCMD; i++, mp++) { 146232523Sbostic ud->uda_ca.ca_cmddsc[i] = MSCP_INT | 146332523Sbostic (long)&uud->uda_cmd[i].mscp_cmdref; 146432523Sbostic mp->mscp_addr = &ud->uda_ca.ca_cmddsc[i]; 146532523Sbostic mp->mscp_msglen = MSCP_MSGLEN; 146632523Sbostic } 14674743Swnj } 14684743Swnj 14694743Swnj /* 147033444Sbostic * Handle an error datagram. 14714743Swnj */ 147232523Sbostic udadgram(mi, mp) 147332523Sbostic struct mscp_info *mi; 147432523Sbostic struct mscp *mp; 14754743Swnj { 147617553Skarels 147732523Sbostic mscp_decodeerror(mi->mi_md->md_mname, mi->mi_ctlr, mp); 147833444Sbostic /* 147933444Sbostic * SDI status information bytes 10 and 11 are the microprocessor 148033444Sbostic * error code and front panel code respectively. These vary per 148133444Sbostic * drive type and are printed purely for field service information. 148233444Sbostic */ 148333444Sbostic if (mp->mscp_format == M_FM_SDI) 148433444Sbostic printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n", 148533444Sbostic mp->mscp_erd.erd_sdistat[10], 148633444Sbostic mp->mscp_erd.erd_sdistat[11]); 148732523Sbostic } 148817553Skarels 148932523Sbostic /* 149032523Sbostic * The Set Controller Characteristics command finished. 149132523Sbostic * Record the new state of the controller. 149232523Sbostic */ 149332523Sbostic udactlrdone(mi, mp) 149432523Sbostic register struct mscp_info *mi; 149532523Sbostic struct mscp *mp; 149632523Sbostic { 149732523Sbostic register struct uda_softc *sc = &uda_softc[mi->mi_ctlr]; 149817553Skarels 149932523Sbostic if ((mp->mscp_status & M_ST_MASK) == M_ST_SUCCESS) 150032523Sbostic sc->sc_state = ST_RUN; 150132523Sbostic else { 150232523Sbostic printf("uda%d: SETCTLRC failed: ", 150332523Sbostic mi->mi_ctlr, mp->mscp_status); 150432523Sbostic mscp_printevent(mp); 150532523Sbostic sc->sc_state = ST_IDLE; 15064743Swnj } 150732523Sbostic if (sc->sc_flags & SC_DOWAKE) { 150832523Sbostic sc->sc_flags &= ~SC_DOWAKE; 150932523Sbostic wakeup((caddr_t)sc); 15106964Ssam } 15114743Swnj } 15124743Swnj 15134743Swnj /* 151432523Sbostic * Received a response from an as-yet unconfigured drive. Configure it 151532523Sbostic * in, if possible. 15164743Swnj */ 151732523Sbostic udaunconf(mi, mp) 151832523Sbostic struct mscp_info *mi; 151932523Sbostic register struct mscp *mp; 15204743Swnj { 15214743Swnj 152217553Skarels /* 152332523Sbostic * If it is a slave response, copy it to udaslavereply for 152432523Sbostic * udaslave() to look at. 152517553Skarels */ 152632523Sbostic if (mp->mscp_opcode == (M_OP_GETUNITST | M_OP_END) && 152732523Sbostic (uda_softc[mi->mi_ctlr].sc_flags & SC_INSLAVE) != 0) { 152832523Sbostic udaslavereply = *mp; 152932523Sbostic return (MSCP_DONE); 15304743Swnj } 153132523Sbostic 153232523Sbostic /* 153332523Sbostic * Otherwise, it had better be an available attention response. 153432523Sbostic */ 153532523Sbostic if (mp->mscp_opcode != M_OP_AVAILATTN) 153632523Sbostic return (MSCP_FAILED); 153732523Sbostic 153832523Sbostic /* do what autoconf does */ 153932523Sbostic return (MSCP_FAILED); /* not yet, arwhite, not yet */ 15404743Swnj } 15414743Swnj 154232523Sbostic /* 154332523Sbostic * A drive came on line. Check its type and size. Return DONE if 154432523Sbostic * we think the drive is truly on line. In any case, awaken anyone 154532523Sbostic * sleeping on the drive on-line-ness. 154632523Sbostic */ 154732523Sbostic udaonline(ui, mp) 154832523Sbostic register struct uba_device *ui; 154932523Sbostic struct mscp *mp; 15504743Swnj { 155132523Sbostic register struct ra_info *ra = &ra_info[ui->ui_unit]; 15524743Swnj 155332523Sbostic wakeup((caddr_t)&ui->ui_flags); 155432523Sbostic if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) { 155536036Skarels if (!cold) 155636036Skarels printf("uda%d: ra%d", ui->ui_ctlr, ui->ui_unit); 155736036Skarels printf(": attempt to bring on line failed: "); 155832523Sbostic mscp_printevent(mp); 155932523Sbostic ra->ra_state = CLOSED; 156032523Sbostic return (MSCP_FAILED); 156132523Sbostic } 156232523Sbostic 156332523Sbostic ra->ra_state = OPENRAW; 156432523Sbostic ra->ra_dsize = (daddr_t)mp->mscp_onle.onle_unitsize; 156534283Skarels if (!cold) 156634283Skarels printf("ra%d: uda%d, unit %d, size = %d sectors\n", ui->ui_unit, 156734283Skarels ui->ui_ctlr, mp->mscp_unit, ra->ra_dsize); 156832523Sbostic /* can now compute ncyl */ 156932523Sbostic ra->ra_geom.rg_ncyl = ra->ra_dsize / ra->ra_geom.rg_ntracks / 157032523Sbostic ra->ra_geom.rg_nsectors; 157132523Sbostic return (MSCP_DONE); 15724743Swnj } 15734743Swnj 157432523Sbostic /* 157532523Sbostic * We got some (configured) unit's status. Return DONE if it succeeded. 157632523Sbostic */ 157732523Sbostic udagotstatus(ui, mp) 157832523Sbostic register struct uba_device *ui; 157932523Sbostic register struct mscp *mp; 15804743Swnj { 15814743Swnj 158232523Sbostic if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) { 158332523Sbostic printf("uda%d: attempt to get status for ra%d failed: ", 158432523Sbostic ui->ui_ctlr, ui->ui_unit); 158532523Sbostic mscp_printevent(mp); 158632523Sbostic return (MSCP_FAILED); 158732523Sbostic } 158832523Sbostic /* record for (future) bad block forwarding and whatever else */ 158932523Sbostic uda_rasave(ui->ui_unit, mp, 1); 159032523Sbostic return (MSCP_DONE); 15914743Swnj } 15924743Swnj 159332523Sbostic /* 159432523Sbostic * A transfer failed. We get a chance to fix or restart it. 159532523Sbostic * Need to write the bad block forwaring code first.... 159632523Sbostic */ 159732523Sbostic /*ARGSUSED*/ 159832523Sbostic udaioerror(ui, mp, bp) 159932523Sbostic register struct uba_device *ui; 160032523Sbostic register struct mscp *mp; 160132523Sbostic struct buf *bp; 16024743Swnj { 16034743Swnj 160432523Sbostic if (mp->mscp_flags & M_EF_BBLKR) { 160532523Sbostic /* 160632523Sbostic * A bad block report. Eventually we will 160732523Sbostic * restart this transfer, but for now, just 160832523Sbostic * log it and give up. 160932523Sbostic */ 161032523Sbostic log(LOG_ERR, "ra%d: bad block report: %d%s\n", 161132523Sbostic ui->ui_unit, mp->mscp_seq.seq_lbn, 161232523Sbostic mp->mscp_flags & M_EF_BBLKU ? " + others" : ""); 161332523Sbostic } else { 161432523Sbostic /* 161532523Sbostic * What the heck IS a `serious exception' anyway? 161632523Sbostic * IT SURE WOULD BE NICE IF DEC SOLD DOCUMENTATION 161732523Sbostic * FOR THEIR OWN CONTROLLERS. 161832523Sbostic */ 161932523Sbostic if (mp->mscp_flags & M_EF_SEREX) 162032523Sbostic log(LOG_ERR, "ra%d: serious exception reported\n", 162132523Sbostic ui->ui_unit); 16224743Swnj } 162332523Sbostic return (MSCP_FAILED); 16244743Swnj } 16254743Swnj 162632523Sbostic /* 162732523Sbostic * A replace operation finished. 162832523Sbostic */ 162932523Sbostic /*ARGSUSED*/ 163032523Sbostic udareplace(ui, mp) 163132523Sbostic struct uba_device *ui; 163232523Sbostic struct mscp *mp; 16334743Swnj { 163417553Skarels 163532523Sbostic panic("udareplace"); 16364743Swnj } 163717553Skarels 163832523Sbostic /* 163932523Sbostic * A bad block related operation finished. 164032523Sbostic */ 164132523Sbostic /*ARGSUSED*/ 164232523Sbostic udabb(ui, mp, bp) 164332523Sbostic struct uba_device *ui; 164432523Sbostic struct mscp *mp; 164532523Sbostic struct buf *bp; 164617553Skarels { 164717553Skarels 164832523Sbostic panic("udabb"); 164917553Skarels } 165017553Skarels 165132523Sbostic 165232523Sbostic /* 165332523Sbostic * I/O controls. 165432523Sbostic */ 165532523Sbostic udaioctl(dev, cmd, data, flag) 165612511Ssam dev_t dev; 165730536Skarels int cmd; 165830536Skarels caddr_t data; 165930536Skarels int flag; 166012511Ssam { 166132523Sbostic register int unit = udaunit(dev); 166230536Skarels register struct disklabel *lp; 166333443Skarels register struct ra_info *ra = &ra_info[unit]; 166434638Skarels int error = 0; 166512511Ssam 166632523Sbostic lp = &udalabel[unit]; 166730536Skarels 166830536Skarels switch (cmd) { 166930536Skarels 167030536Skarels case DIOCGDINFO: 167130536Skarels *(struct disklabel *)data = *lp; 167230536Skarels break; 167330536Skarels 167430773Skarels case DIOCGPART: 167530773Skarels ((struct partinfo *)data)->disklab = lp; 167630773Skarels ((struct partinfo *)data)->part = 167732523Sbostic &lp->d_partitions[udapart(dev)]; 167830536Skarels break; 167930536Skarels 168030536Skarels case DIOCSDINFO: 168130536Skarels if ((flag & FWRITE) == 0) 168230536Skarels error = EBADF; 168330536Skarels else 168432574Skarels error = setdisklabel(lp, (struct disklabel *)data, 168534283Skarels (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart); 168630536Skarels break; 168730536Skarels 168833443Skarels case DIOCWLABEL: 168933443Skarels if ((flag & FWRITE) == 0) 169033443Skarels error = EBADF; 169133443Skarels else 169233443Skarels ra->ra_wlabel = *(int *)data; 169333443Skarels break; 169433443Skarels 169532574Skarels case DIOCWDINFO: 169632574Skarels if ((flag & FWRITE) == 0) 169732523Sbostic error = EBADF; 169832574Skarels else if ((error = setdisklabel(lp, (struct disklabel *)data, 169934283Skarels (ra->ra_state == OPENRAW) ? 0 : ra->ra_openpart)) == 0) { 170034638Skarels int wlab; 170134638Skarels 170234283Skarels ra->ra_state = OPEN; 170334638Skarels /* simulate opening partition 0 so write succeeds */ 170434638Skarels ra->ra_openpart |= (1 << 0); /* XXX */ 170534638Skarels wlab = ra->ra_wlabel; 170634638Skarels ra->ra_wlabel = 1; 170732574Skarels error = writedisklabel(dev, udastrategy, lp); 170834638Skarels ra->ra_openpart = ra->ra_copenpart | ra->ra_bopenpart; 170934638Skarels ra->ra_wlabel = wlab; 171034283Skarels } 171130536Skarels break; 171230536Skarels 171332523Sbostic #ifdef notyet 171432523Sbostic case UDAIOCREPLACE: 171532523Sbostic /* 171632523Sbostic * Initiate bad block replacement for the given LBN. 171732523Sbostic * (Should we allow modifiers?) 171832523Sbostic */ 171932523Sbostic error = EOPNOTSUPP; 172032523Sbostic break; 172132523Sbostic 172232523Sbostic case UDAIOCGMICRO: 172332523Sbostic /* 172432523Sbostic * Return the microcode revision for the UDA50 running 172532523Sbostic * this drive. 172632523Sbostic */ 172733444Sbostic *(int *)data = uda_softc[uddinfo[unit]->ui_ctlr].sc_micro; 172832523Sbostic break; 172932523Sbostic #endif 173032523Sbostic 173130536Skarels default: 173230536Skarels error = ENOTTY; 173330536Skarels break; 173430536Skarels } 173532523Sbostic return (error); 173632523Sbostic } 173732523Sbostic 173832523Sbostic /* 173932523Sbostic * A Unibus reset has occurred on UBA uban. Reinitialise the controller(s) 174032523Sbostic * on that Unibus, and requeue outstanding I/O. 174132523Sbostic */ 174232523Sbostic udareset(uban) 174332523Sbostic int uban; 174432523Sbostic { 174532523Sbostic register struct uba_ctlr *um; 174632523Sbostic register struct uda_softc *sc; 174732523Sbostic register int ctlr; 174832523Sbostic 174932523Sbostic for (ctlr = 0, sc = uda_softc; ctlr < NUDA; ctlr++, sc++) { 175032523Sbostic if ((um = udaminfo[ctlr]) == NULL || um->um_ubanum != uban || 175132523Sbostic um->um_alive == 0) 175232523Sbostic continue; 175332523Sbostic printf(" uda%d", ctlr); 175432523Sbostic 175532523Sbostic /* 175632523Sbostic * Our BDP (if any) is gone; our command (if any) is 175732523Sbostic * flushed; the device is no longer mapped; and the 175832523Sbostic * UDA50 is not yet initialised. 175932523Sbostic */ 176032523Sbostic if (um->um_bdp) { 176132523Sbostic printf("<%d>", UBAI_BDP(um->um_bdp)); 176232523Sbostic um->um_bdp = 0; 176332523Sbostic } 176432523Sbostic um->um_ubinfo = 0; 176532523Sbostic um->um_cmd = 0; 176632523Sbostic sc->sc_flags &= ~SC_MAPPED; 176732523Sbostic sc->sc_state = ST_IDLE; 176832523Sbostic 176932523Sbostic /* reset queues and requeue pending transfers */ 177032523Sbostic mscp_requeue(&sc->sc_mi); 177132523Sbostic 177232523Sbostic /* 177332523Sbostic * If it fails to initialise we will notice later and 177432523Sbostic * try again (and again...). Do not call udastart() 177532523Sbostic * here; it will be done after the controller finishes 177632523Sbostic * initialisation. 177732523Sbostic */ 177832523Sbostic if (udainit(ctlr)) 177932523Sbostic printf(" (hung)"); 178032523Sbostic } 178132523Sbostic } 178232523Sbostic 178332523Sbostic /* 178432523Sbostic * Watchdog timer: If the controller is active, and no interrupts 178532523Sbostic * have occurred for 30 seconds, assume it has gone away. 178632523Sbostic */ 178732523Sbostic udawatch() 178832523Sbostic { 178932523Sbostic register int i; 179032523Sbostic register struct uba_ctlr *um; 179132523Sbostic register struct uda_softc *sc; 179232523Sbostic 179332523Sbostic timeout(udawatch, (caddr_t) 0, hz); /* every second */ 179432523Sbostic for (i = 0, sc = uda_softc; i < NUDA; i++, sc++) { 179532523Sbostic if ((um = udaminfo[i]) == 0 || !um->um_alive) 179632523Sbostic continue; 179732523Sbostic if (sc->sc_state == ST_IDLE) 179832523Sbostic continue; 179932523Sbostic if (sc->sc_state == ST_RUN && !um->um_tab.b_active) 180032523Sbostic sc->sc_wticks = 0; 180132523Sbostic else if (++sc->sc_wticks >= 30) { 180232523Sbostic sc->sc_wticks = 0; 180332523Sbostic printf("uda%d: lost interrupt\n", i); 180432523Sbostic ubareset(um->um_ubanum); 180532523Sbostic } 180632523Sbostic } 180732523Sbostic } 180832523Sbostic 180932523Sbostic /* 181032523Sbostic * Do a panic dump. We set up the controller for one command packet 181132523Sbostic * and one response packet, for which we use `struct uda1'. 181232523Sbostic */ 181332523Sbostic struct uda1 { 181432523Sbostic struct uda1ca uda1_ca; /* communications area */ 181532523Sbostic struct mscp uda1_rsp; /* response packet */ 181632523Sbostic struct mscp uda1_cmd; /* command packet */ 181732523Sbostic } uda1; 181832523Sbostic 181932523Sbostic #define DBSIZE 32 /* dump 16K at a time */ 182032523Sbostic 182132523Sbostic udadump(dev) 182232523Sbostic dev_t dev; 182332523Sbostic { 182432523Sbostic struct udadevice *udaddr; 182532523Sbostic struct uda1 *ud_ubaddr; 182632523Sbostic char *start; 182732523Sbostic int num, blk, unit, maxsz, blkoff, reg; 182832523Sbostic struct partition *pp; 182932523Sbostic register struct uba_regs *uba; 183032523Sbostic register struct uba_device *ui; 183132523Sbostic register struct uda1 *ud; 183232523Sbostic register struct pte *io; 183332523Sbostic register int i; 183432523Sbostic 183532523Sbostic /* 183632523Sbostic * Make sure the device is a reasonable place on which to dump. 183732523Sbostic */ 183832523Sbostic unit = udaunit(dev); 183932523Sbostic if (unit >= NRA) 184032523Sbostic return (ENXIO); 184133444Sbostic #define phys(cast, addr) ((cast) ((int)addr & 0x7fffffff)) 184232523Sbostic ui = phys(struct uba_device *, udadinfo[unit]); 184332523Sbostic if (ui == NULL || ui->ui_alive == 0) 184432523Sbostic return (ENXIO); 184532523Sbostic 184632523Sbostic /* 184732523Sbostic * Find and initialise the UBA; get the physical address of the 184832523Sbostic * device registers, and of communications area and command and 184932523Sbostic * response packet. 185032523Sbostic */ 185132523Sbostic uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba; 185232523Sbostic ubainit(uba); 185332523Sbostic udaddr = (struct udadevice *)ui->ui_physaddr; 185432523Sbostic ud = phys(struct uda1 *, &uda1); 185532523Sbostic 185632523Sbostic /* 185732523Sbostic * Map the ca+packets into Unibus I/O space so the UDA50 can get 185832523Sbostic * at them. Use the registers at the end of the Unibus map (since 185932523Sbostic * we will use the registers at the beginning to map the memory 186032523Sbostic * we are dumping). 186132523Sbostic */ 186232523Sbostic num = btoc(sizeof(struct uda1)) + 1; 186332523Sbostic reg = NUBMREG - num; 186432523Sbostic io = &uba->uba_map[reg]; 186532523Sbostic for (i = 0; i < num; i++) 186632523Sbostic *(int *)io++ = UBAMR_MRV | (btop(ud) + i); 186732523Sbostic ud_ubaddr = (struct uda1 *)(((int)ud & PGOFSET) | (reg << 9)); 186832523Sbostic 186932523Sbostic /* 187032523Sbostic * Initialise the controller, with one command and one response 187132523Sbostic * packet. 187232523Sbostic */ 187332523Sbostic udaddr->udaip = 0; 187432523Sbostic if (udadumpwait(udaddr, UDA_STEP1)) 187532523Sbostic return (EFAULT); 187632523Sbostic udaddr->udasa = UDA_ERR; 187732523Sbostic if (udadumpwait(udaddr, UDA_STEP2)) 187832523Sbostic return (EFAULT); 187932523Sbostic udaddr->udasa = (int)&ud_ubaddr->uda1_ca.ca_rspdsc; 188032523Sbostic if (udadumpwait(udaddr, UDA_STEP3)) 188132523Sbostic return (EFAULT); 188232523Sbostic udaddr->udasa = ((int)&ud_ubaddr->uda1_ca.ca_rspdsc) >> 16; 188332523Sbostic if (udadumpwait(udaddr, UDA_STEP4)) 188432523Sbostic return (EFAULT); 188532523Sbostic uda_softc[ui->ui_ctlr].sc_micro = udaddr->udasa & 0xff; 188632523Sbostic udaddr->udasa = UDA_GO; 188732523Sbostic 188832523Sbostic /* 188932523Sbostic * Set up the command and response descriptor, then set the 189032523Sbostic * controller characteristics and bring the drive on line. 189132523Sbostic * Note that all uninitialised locations in uda1_cmd are zero. 189232523Sbostic */ 189332523Sbostic ud->uda1_ca.ca_rspdsc = (long)&ud_ubaddr->uda1_rsp.mscp_cmdref; 189432523Sbostic ud->uda1_ca.ca_cmddsc = (long)&ud_ubaddr->uda1_cmd.mscp_cmdref; 189532523Sbostic /* ud->uda1_cmd.mscp_sccc.sccc_ctlrflags = 0; */ 189632523Sbostic /* ud->uda1_cmd.mscp_sccc.sccc_version = 0; */ 189732523Sbostic if (udadumpcmd(M_OP_SETCTLRC, ud, ui)) 189832523Sbostic return (EFAULT); 189932523Sbostic ud->uda1_cmd.mscp_unit = ui->ui_slave; 190032523Sbostic if (udadumpcmd(M_OP_ONLINE, ud, ui)) 190132523Sbostic return (EFAULT); 190232523Sbostic 190332523Sbostic pp = phys(struct partition *, 190432523Sbostic &udalabel[unit].d_partitions[udapart(dev)]); 190532523Sbostic maxsz = pp->p_size; 190632523Sbostic blkoff = pp->p_offset; 190732523Sbostic 190832523Sbostic /* 190932523Sbostic * Dump all of physical memory, or as much as will fit in the 191032523Sbostic * space provided. 191132523Sbostic */ 191232523Sbostic start = 0; 191332523Sbostic num = maxfree; 191432523Sbostic if (dumplo + num >= maxsz) 191532523Sbostic num = maxsz - dumplo; 191632523Sbostic blkoff += dumplo; 191732523Sbostic 191832523Sbostic /* 191932523Sbostic * Write out memory, DBSIZE pages at a time. 192032523Sbostic * N.B.: this code depends on the fact that the sector 192132523Sbostic * size == the page size. 192232523Sbostic */ 192332523Sbostic while (num > 0) { 192432523Sbostic blk = num > DBSIZE ? DBSIZE : num; 192532523Sbostic io = uba->uba_map; 192632523Sbostic /* 192732523Sbostic * Map in the pages to write, leaving an invalid entry 192832523Sbostic * at the end to guard against wild Unibus transfers. 192932523Sbostic * Then do the write. 193032523Sbostic */ 193132523Sbostic for (i = 0; i < blk; i++) 193233444Sbostic *(int *)io++ = UBAMR_MRV | (btop(start) + i); 193333444Sbostic *(int *)io = 0; 193432523Sbostic ud->uda1_cmd.mscp_unit = ui->ui_slave; 193532523Sbostic ud->uda1_cmd.mscp_seq.seq_lbn = btop(start) + blkoff; 193632523Sbostic ud->uda1_cmd.mscp_seq.seq_bytecount = blk << PGSHIFT; 193732523Sbostic if (udadumpcmd(M_OP_WRITE, ud, ui)) 193832523Sbostic return (EIO); 193932523Sbostic start += blk << PGSHIFT; 194032523Sbostic num -= blk; 194132523Sbostic } 194232523Sbostic return (0); /* made it! */ 194332523Sbostic } 194432523Sbostic 194532523Sbostic /* 194632523Sbostic * Wait for some of the bits in `bits' to come on. If the error bit 194732523Sbostic * comes on, or ten seconds pass without response, return true (error). 194832523Sbostic */ 194932523Sbostic udadumpwait(udaddr, bits) 195032523Sbostic register struct udadevice *udaddr; 195132523Sbostic register int bits; 195232523Sbostic { 195332523Sbostic register int timo = todr() + 1000; 195432523Sbostic 195532523Sbostic while ((udaddr->udasa & bits) == 0) { 195632523Sbostic if (udaddr->udasa & UDA_ERR) { 195732523Sbostic printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits); 195832523Sbostic return (1); 195932523Sbostic } 196032523Sbostic if (todr() >= timo) { 196132523Sbostic printf("timeout\ndump "); 196232523Sbostic return (1); 196332523Sbostic } 196432523Sbostic } 196530536Skarels return (0); 196630536Skarels } 196730536Skarels 196832523Sbostic /* 196932523Sbostic * Feed a command to the UDA50, wait for its response, and return 197032523Sbostic * true iff something went wrong. 197132523Sbostic */ 197232523Sbostic udadumpcmd(op, ud, ui) 197332523Sbostic int op; 197432523Sbostic register struct uda1 *ud; 197532523Sbostic struct uba_device *ui; 197632523Sbostic { 197732523Sbostic register struct udadevice *udaddr; 197832523Sbostic register int n; 197932523Sbostic #define mp (&ud->uda1_rsp) 198032523Sbostic 198133444Sbostic udaddr = (struct udadevice *)ui->ui_physaddr; 198232523Sbostic ud->uda1_cmd.mscp_opcode = op; 198332523Sbostic ud->uda1_cmd.mscp_msglen = MSCP_MSGLEN; 198432523Sbostic ud->uda1_rsp.mscp_msglen = MSCP_MSGLEN; 198532523Sbostic ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT; 198632523Sbostic ud->uda1_ca.ca_cmddsc |= MSCP_OWN | MSCP_INT; 198732523Sbostic if (udaddr->udasa & UDA_ERR) { 198832523Sbostic printf("udasa=%b\ndump ", udaddr->udasa, udasr_bits); 198932523Sbostic return (1); 199032523Sbostic } 199132523Sbostic n = udaddr->udaip; 199232523Sbostic n = todr() + 1000; 199332523Sbostic for (;;) { 199432523Sbostic if (todr() > n) { 199532523Sbostic printf("timeout\ndump "); 199632523Sbostic return (1); 199732523Sbostic } 199832523Sbostic if (ud->uda1_ca.ca_cmdint) 199932523Sbostic ud->uda1_ca.ca_cmdint = 0; 200032523Sbostic if (ud->uda1_ca.ca_rspint == 0) 200132523Sbostic continue; 200232523Sbostic ud->uda1_ca.ca_rspint = 0; 200332523Sbostic if (mp->mscp_opcode == (op | M_OP_END)) 200432523Sbostic break; 200532523Sbostic printf("\n"); 200632523Sbostic switch (MSCP_MSGTYPE(mp->mscp_msgtc)) { 200732523Sbostic 200832523Sbostic case MSCPT_SEQ: 200932523Sbostic printf("sequential"); 201032523Sbostic break; 201132523Sbostic 201232523Sbostic case MSCPT_DATAGRAM: 201332523Sbostic mscp_decodeerror("uda", ui->ui_ctlr, mp); 201432523Sbostic printf("datagram"); 201532523Sbostic break; 201632523Sbostic 201732523Sbostic case MSCPT_CREDITS: 201832523Sbostic printf("credits"); 201932523Sbostic break; 202032523Sbostic 202132523Sbostic case MSCPT_MAINTENANCE: 202232523Sbostic printf("maintenance"); 202332523Sbostic break; 202432523Sbostic 202532523Sbostic default: 202632523Sbostic printf("unknown (type 0x%x)", 202732523Sbostic MSCP_MSGTYPE(mp->mscp_msgtc)); 202832523Sbostic break; 202932523Sbostic } 203032523Sbostic printf(" ignored\ndump "); 203132523Sbostic ud->uda1_ca.ca_rspdsc |= MSCP_OWN | MSCP_INT; 203232523Sbostic } 203332523Sbostic if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) { 203432523Sbostic printf("error: op 0x%x => 0x%x status 0x%x\ndump ", op, 203532523Sbostic mp->mscp_opcode, mp->mscp_status); 203632523Sbostic return (1); 203732523Sbostic } 203832523Sbostic return (0); 203932523Sbostic #undef mp 204032523Sbostic } 204132523Sbostic 204232523Sbostic /* 204332523Sbostic * Return the size of a partition, if known, or -1 if not. 204432523Sbostic */ 204532523Sbostic udasize(dev) 204630536Skarels dev_t dev; 204730536Skarels { 204832523Sbostic register int unit = udaunit(dev); 204930536Skarels register struct uba_device *ui; 205030536Skarels 205132523Sbostic if (unit >= NRA || (ui = udadinfo[unit]) == NULL || 205232523Sbostic ui->ui_alive == 0 || (ui->ui_flags & UNIT_ONLINE) == 0 || 205332523Sbostic ra_info[unit].ra_state != OPEN) 205412511Ssam return (-1); 205532523Sbostic return ((int)udalabel[unit].d_partitions[udapart(dev)].p_size); 205612511Ssam } 205717553Skarels 205830536Skarels #ifdef COMPAT_42 205932523Sbostic /* 206032523Sbostic * Tables mapping unlabelled drives. 206132523Sbostic */ 206230536Skarels struct size { 206330536Skarels daddr_t nblocks; 206430536Skarels daddr_t blkoff; 206533444Sbostic } ra60_sizes[8] = { 206630536Skarels 15884, 0, /* A=sectors 0 thru 15883 */ 206730536Skarels 33440, 15884, /* B=sectors 15884 thru 49323 */ 206830536Skarels 400176, 0, /* C=sectors 0 thru 400175 */ 206930536Skarels 82080, 49324, /* 4.2 G => D=sectors 49324 thru 131403 */ 207030536Skarels 268772, 131404, /* 4.2 H => E=sectors 131404 thru 400175 */ 207130536Skarels 350852, 49324, /* F=sectors 49324 thru 400175 */ 207230536Skarels 157570, 242606, /* UCB G => G=sectors 242606 thru 400175 */ 207330536Skarels 193282, 49324, /* UCB H => H=sectors 49324 thru 242605 */ 207433444Sbostic }, ra70_sizes[8] = { 207533444Sbostic 15884, 0, /* A=blk 0 thru 15883 */ 207633444Sbostic 33440, 15972, /* B=blk 15972 thru 49323 */ 207733444Sbostic -1, 0, /* C=blk 0 thru end */ 207833444Sbostic 15884, 341220, /* D=blk 341220 thru 357103 */ 207933444Sbostic 55936, 357192, /* E=blk 357192 thru 413127 */ 208033444Sbostic -1, 413457, /* F=blk 413457 thru end */ 208133444Sbostic -1, 341220, /* G=blk 341220 thru end */ 208233444Sbostic 291346, 49731, /* H=blk 49731 thru 341076 */ 208330536Skarels }, ra80_sizes[8] = { 208430536Skarels 15884, 0, /* A=sectors 0 thru 15883 */ 208530536Skarels 33440, 15884, /* B=sectors 15884 thru 49323 */ 208630536Skarels 242606, 0, /* C=sectors 0 thru 242605 */ 208730536Skarels 0, 0, /* D=unused */ 208830536Skarels 193282, 49324, /* UCB H => E=sectors 49324 thru 242605 */ 208930536Skarels 82080, 49324, /* 4.2 G => F=sectors 49324 thru 131403 */ 209030536Skarels 192696, 49910, /* G=sectors 49910 thru 242605 */ 209130536Skarels 111202, 131404, /* 4.2 H => H=sectors 131404 thru 242605 */ 209230536Skarels }, ra81_sizes[8] ={ 209330536Skarels /* 209430536Skarels * These are the new standard partition sizes for ra81's. 209530536Skarels * An RA_COMPAT system is compiled with D, E, and F corresponding 209630536Skarels * to the 4.2 partitions for G, H, and F respectively. 209730536Skarels */ 209830536Skarels #ifndef UCBRA 209930536Skarels 15884, 0, /* A=sectors 0 thru 15883 */ 210030536Skarels 66880, 16422, /* B=sectors 16422 thru 83301 */ 210130536Skarels 891072, 0, /* C=sectors 0 thru 891071 */ 210230536Skarels #ifdef RA_COMPAT 210330536Skarels 82080, 49324, /* 4.2 G => D=sectors 49324 thru 131403 */ 210430536Skarels 759668, 131404, /* 4.2 H => E=sectors 131404 thru 891071 */ 210530536Skarels 478582, 412490, /* 4.2 F => F=sectors 412490 thru 891071 */ 210630536Skarels #else 210730536Skarels 15884, 375564, /* D=sectors 375564 thru 391447 */ 210830536Skarels 307200, 391986, /* E=sectors 391986 thru 699185 */ 210930536Skarels 191352, 699720, /* F=sectors 699720 thru 891071 */ 211030536Skarels #endif RA_COMPAT 211130536Skarels 515508, 375564, /* G=sectors 375564 thru 891071 */ 211230536Skarels 291346, 83538, /* H=sectors 83538 thru 374883 */ 211330536Skarels 211430536Skarels /* 211530536Skarels * These partitions correspond to the sizes used by sites at Berkeley, 211630536Skarels * and by those sites that have received copies of the Berkeley driver 211730536Skarels * with deltas 6.2 or greater (11/15/83). 211830536Skarels */ 211930536Skarels #else UCBRA 212030536Skarels 212130536Skarels 15884, 0, /* A=sectors 0 thru 15883 */ 212230536Skarels 33440, 15884, /* B=sectors 15884 thru 49323 */ 212330536Skarels 891072, 0, /* C=sectors 0 thru 891071 */ 212430536Skarels 15884, 242606, /* D=sectors 242606 thru 258489 */ 212530536Skarels 307200, 258490, /* E=sectors 258490 thru 565689 */ 212630536Skarels 325382, 565690, /* F=sectors 565690 thru 891071 */ 212730536Skarels 648466, 242606, /* G=sectors 242606 thru 891071 */ 212830536Skarels 193282, 49324, /* H=sectors 49324 thru 242605 */ 212930536Skarels 213030536Skarels #endif UCBRA 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 221533444Sbostic i = MSCP_MEDIA_DRIVE(ra->ra_mediaid); 221633444Sbostic for (ut = udatypes; ut->ut_id; ut++) 221736036Skarels if (ut->ut_id == i && 221836036Skarels ut->ut_nsectors == ra->ra_geom.rg_nsectors && 221936036Skarels ut->ut_ntracks == ra->ra_geom.rg_ntracks && 222036036Skarels ut->ut_ncylinders == ra->ra_geom.rg_ncyl) 222133444Sbostic goto found; 222233444Sbostic 222333444Sbostic /* not one we know; fake up a label for the whole drive */ 222436036Skarels uda_makefakelabel(ra, lp); 222533444Sbostic i = ra->ra_mediaid; /* print the port type too */ 222636036Skarels addlog(": no partition table for %c%c %c%c%c%d, size %d;\n\ 222734283Skarels using (s,t,c)=(%d,%d,%d)", 222834283Skarels MSCP_MID_CHAR(4, i), MSCP_MID_CHAR(3, i), 222933444Sbostic MSCP_MID_CHAR(2, i), MSCP_MID_CHAR(1, i), 223036036Skarels MSCP_MID_CHAR(0, i), MSCP_MID_NUM(i), lp->d_secperunit, 223136036Skarels lp->d_nsectors, lp->d_ntracks, lp->d_ncylinders); 223234283Skarels if (!cold) 223334283Skarels addlog("\n"); 223433444Sbostic return (0); 223533444Sbostic found: 223632523Sbostic p = ut->ut_name; 223732523Sbostic for (i = 0; i < sizeof(lp->d_typename) - 1 && *p; i++) 223832523Sbostic lp->d_typename[i] = *p++; 223932523Sbostic lp->d_typename[i] = 0; 224032523Sbostic sz = ut->ut_sizes; 224132523Sbostic lp->d_nsectors = ut->ut_nsectors; 224232523Sbostic lp->d_ntracks = ut->ut_ntracks; 224332523Sbostic lp->d_ncylinders = ut->ut_ncylinders; 224430536Skarels lp->d_npartitions = 8; 224530536Skarels lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 224632523Sbostic for (pp = lp->d_partitions; pp < &lp->d_partitions[8]; pp++, sz++) { 224732523Sbostic pp->p_offset = sz->blkoff; 224832523Sbostic if ((pp->p_size = sz->nblocks) == (u_long)-1) 224932523Sbostic pp->p_size = ra->ra_dsize - sz->blkoff; 225030536Skarels } 225130536Skarels return (1); 225230536Skarels } 225332523Sbostic #endif /* COMPAT_42 */ 225436036Skarels 225536036Skarels /* 225636036Skarels * Construct a label for a drive from geometry information 225736036Skarels * if we have no better information. 225836036Skarels */ 225936036Skarels uda_makefakelabel(ra, lp) 226036036Skarels register struct ra_info *ra; 226136036Skarels register struct disklabel *lp; 226236036Skarels { 226336036Skarels lp->d_nsectors = ra->ra_geom.rg_nsectors; 226436036Skarels lp->d_ntracks = ra->ra_geom.rg_ntracks; 226536036Skarels lp->d_ncylinders = ra->ra_geom.rg_ncyl; 226636036Skarels lp->d_secpercyl = lp->d_nsectors * lp->d_ntracks; 226736036Skarels bcopy("ra??", lp->d_typename, sizeof("ra??")); 226836036Skarels lp->d_npartitions = 1; 226936036Skarels lp->d_partitions[0].p_offset = 0; 227036036Skarels lp->d_partitions[0].p_size = lp->d_secperunit; 227136036Skarels } 227232523Sbostic #endif /* NUDA > 0 */ 2273