clean up imp code after fixing problems
[unix-history] / usr / src / sys / vax / if / if_acc.c
CommitLineData
b690198c 1/* if_acc.c 4.18 82/06/15 */
28081cf8
SL
2
3#include "acc.h"
4#ifdef NACC > 0
5
6/*
7 * ACC LH/DH ARPAnet IMP interface driver.
8 */
9
10#include "../h/param.h"
11#include "../h/systm.h"
12#include "../h/mbuf.h"
13#include "../h/pte.h"
14#include "../h/buf.h"
15#include "../h/protosw.h"
16#include "../h/socket.h"
17#include "../h/ubareg.h"
18#include "../h/ubavar.h"
28081cf8
SL
19#include "../h/cpu.h"
20#include "../h/mtpr.h"
21#include "../h/vmmac.h"
22#include "../net/in.h"
23#include "../net/in_systm.h"
24#include "../net/if.h"
a2cd4df7 25#include "../net/if_acc.h"
28081cf8
SL
26#include "../net/if_imp.h"
27#include "../net/if_uba.h"
28
29int accprobe(), accattach(), accrint(), accxint();
30struct uba_device *accinfo[NACC];
31u_short accstd[] = { 0 };
32struct uba_driver accdriver =
33 { accprobe, 0, accattach, 0, accstd, "acc", accinfo };
34#define ACCUNIT(x) minor(x)
35
36int accinit(), accstart(), accreset();
37
38/*
39 * "Lower half" of IMP interface driver.
40 *
41 * Each IMP interface is handled by a common module which handles
42 * the IMP-host protocol and a hardware driver which manages the
43 * hardware specific details of talking with the IMP.
44 *
45 * The hardware portion of the IMP driver handles DMA and related
46 * management of UNIBUS resources. The IMP protocol module interprets
47 * contents of these messages and "controls" the actions of the
48 * hardware module during IMP resets, but not, for instance, during
49 * UNIBUS resets.
50 *
51 * The two modules are coupled at "attach time", and ever after,
52 * through the imp interface structure. Higher level protocols,
53 * e.g. IP, interact with the IMP driver, rather than the ACC.
54 */
55struct acc_softc {
56 struct ifnet *acc_if; /* pointer to IMP's ifnet struct */
57 struct impcb *acc_ic; /* data structure shared with IMP */
58 struct ifuba acc_ifuba; /* UNIBUS resources */
59 struct mbuf *acc_iq; /* input reassembly queue */
60 short acc_olen; /* size of last message sent */
61 char acc_flush; /* flush remainder of message */
28081cf8
SL
62} acc_softc[NACC];
63
64/*
65 * Reset the IMP and cause a transmitter interrupt by
66 * performing a null DMA.
67 */
68accprobe(reg)
69 caddr_t reg;
70{
71 register int br, cvec; /* r11, r10 value-result */
72 register struct accdevice *addr = (struct accdevice *)reg;
73
74COUNT(ACCPROBE);
75#ifdef lint
76 br = 0; cvec = br; br = cvec;
77 accrint(0); accxint(0);
78#endif
a2cd4df7
BJ
79 addr->icsr = ACC_RESET; DELAY(5000);
80 addr->ocsr = ACC_RESET; DELAY(5000);
81 addr->ocsr = OUT_BBACK; DELAY(5000);
82 addr->owc = 0;
83 addr->ocsr = ACC_IE | ACC_GO; DELAY(5000);
7dd4b7cb 84 addr->ocsr = 0;
a2cd4df7 85 if (cvec && cvec != 0x200) /* transmit -> receive */
28081cf8 86 cvec -= 4;
7d6f1cd5
SL
87#ifdef ECHACK
88 br = 0x16;
89#endif
28081cf8
SL
90 return (1);
91}
92
93/*
94 * Call the IMP module to allow it to set up its internal
95 * state, then tie the two modules together by setting up
96 * the back pointers to common data structures.
97 */
98accattach(ui)
99 struct uba_device *ui;
100{
101 register struct acc_softc *sc = &acc_softc[ui->ui_unit];
102 register struct impcb *ip;
103 struct ifimpcb {
104 struct ifnet ifimp_if;
105 struct impcb ifimp_impcb;
106 } *ifimp;
107
108COUNT(ACCATTACH);
109 if ((ifimp = (struct ifimpcb *)impattach(ui)) == 0)
a2cd4df7 110 panic("accattach");
28081cf8
SL
111 sc->acc_if = &ifimp->ifimp_if;
112 ip = &ifimp->ifimp_impcb;
113 sc->acc_ic = ip;
114 ip->ic_init = accinit;
115 ip->ic_start = accstart;
7dfb87a5 116 sc->acc_ifuba.ifu_flags = UBA_CANTWAIT;
a2cd4df7 117#ifdef notdef
7dfb87a5 118 sc->acc_ifuba.ifu_flags |= UBA_NEEDBDP;
a2cd4df7 119#endif
28081cf8
SL
120}
121
122/*
123 * Reset interface after UNIBUS reset.
124 * If interface is on specified uba, reset its state.
125 */
126accreset(unit, uban)
127 int unit, uban;
128{
129 register struct uba_device *ui;
130 struct acc_softc *sc;
131
132COUNT(ACCRESET);
133 if (unit >= NACC || (ui = accinfo[unit]) == 0 || ui->ui_alive == 0 ||
134 ui->ui_ubanum != uban)
135 return;
136 printf(" acc%d", unit);
137 sc = &acc_softc[unit];
138 /* must go through IMP to allow it to set state */
139 (*sc->acc_if->if_init)(unit);
140}
141
142/*
143 * Initialize interface: clear recorded pending operations,
a2cd4df7
BJ
144 * and retrieve, and initialize UNIBUS resources. Note
145 * return value is used by IMP init routine to mark IMP
146 * unavailable for outgoing traffic.
28081cf8
SL
147 */
148accinit(unit)
149 int unit;
150{
a2cd4df7
BJ
151 register struct acc_softc *sc;
152 register struct uba_device *ui;
28081cf8 153 register struct accdevice *addr;
521c9128 154 int info, i;
28081cf8
SL
155
156COUNT(ACCINIT);
a2cd4df7
BJ
157 if (unit >= NACC || (ui = accinfo[unit]) == 0 || ui->ui_alive == 0) {
158 printf("acc%d: not alive\n", unit);
159 return (0);
160 }
161 sc = &acc_softc[unit];
162 /*
163 * Header length is 0 since we have to passs
164 * the IMP leader up to the protocol interpretation
165 * routines. If we had the header length as
166 * sizeof(struct imp_leader), then the if_ routines
167 * would asssume we handle it on input and output.
168 */
668cc26d 169 if (if_ubainit(&sc->acc_ifuba, ui->ui_ubanum, 0,
e431883e 170 (int)btoc(IMPMTU)) == 0) {
28081cf8 171 printf("acc%d: can't initialize\n", unit);
7dfb87a5
SL
172 ui->ui_alive = 0;
173 return (0);
28081cf8
SL
174 }
175 addr = (struct accdevice *)ui->ui_addr;
176
a0b7c7fb 177 /*
a2cd4df7
BJ
178 * Reset the imp interface;
179 * the delays are pure guesswork.
a0b7c7fb 180 */
a2cd4df7 181 addr->ocsr = ACC_RESET; DELAY(5000);
8129679e 182 addr->ocsr = OUT_BBACK; DELAY(5000); /* reset host master ready */
a2cd4df7 183 addr->ocsr = 0;
7dfb87a5
SL
184 if (accinputreset(addr, unit) == 0) {
185 ui->ui_alive = 0;
186 return (0);
a2cd4df7 187 }
28081cf8
SL
188
189 /*
190 * Put up a read. We can't restart any outstanding writes
191 * until we're back in synch with the IMP (i.e. we've flushed
192 * the NOOPs it throws at us).
e431883e 193 * Note: IMPMTU includes the leader.
28081cf8 194 */
28081cf8 195 info = sc->acc_ifuba.ifu_r.ifrw_info;
a2cd4df7 196 addr->iba = (u_short)info;
e431883e 197 addr->iwc = -(IMPMTU >> 1);
a2cd4df7
BJ
198#ifdef LOOPBACK
199 addr->ocsr |= OUT_BBACK;
200#endif
201 addr->icsr =
28081cf8 202 IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
a2cd4df7 203 return (1);
28081cf8
SL
204}
205
7dfb87a5
SL
206accinputreset(addr, unit)
207 register struct accdevice *addr;
208 register int unit;
209{
210 register int i;
211
212 addr->icsr = ACC_RESET; DELAY(5000);
213 addr->icsr = IN_MRDY | IN_WEN; /* close the relay */
214 DELAY(10000);
215 /* YECH!!! */
216 for (i = 0; i < 500; i++) {
217 if ((addr->icsr & IN_HRDY) ||
218 (addr->icsr & (IN_RMR | IN_IMPBSY)) == 0)
219 return (1);
220 addr->icsr = IN_MRDY | IN_WEN; DELAY(10000);
221 /* keep turning IN_RMR off */
222 }
223 printf("acc%d: imp doesn't respond, icsr=%b\n", unit,
224 addr->icsr, ACC_INBITS);
225 return (0);
226}
227
28081cf8
SL
228/*
229 * Start output on an interface.
230 */
231accstart(dev)
232 dev_t dev;
233{
234 int unit = ACCUNIT(dev), info;
28081cf8
SL
235 register struct acc_softc *sc = &acc_softc[unit];
236 register struct accdevice *addr;
237 struct mbuf *m;
238 u_short cmd;
239
240COUNT(ACCSTART);
241 if (sc->acc_ic->ic_oactive)
242 goto restart;
243
244 /*
245 * Not already active, deqeue a request and
246 * map it onto the UNIBUS. If no more
247 * requeusts, just return.
248 */
249 IF_DEQUEUE(&sc->acc_if->if_snd, m);
250 if (m == 0) {
251 sc->acc_ic->ic_oactive = 0;
252 return;
253 }
254 sc->acc_olen = if_wubaput(&sc->acc_ifuba, m);
255
256restart:
257 /*
a2cd4df7
BJ
258 * Have request mapped to UNIBUS for
259 * transmission; start the output.
28081cf8 260 */
a2cd4df7
BJ
261 if (sc->acc_ifuba.ifu_flags & UBA_NEEDBDP)
262 UBAPURGE(sc->acc_ifuba.ifu_uba, sc->acc_ifuba.ifu_w.ifrw_bdp);
bcb5db7d 263 addr = (struct accdevice *)accinfo[unit]->ui_addr;
28081cf8 264 info = sc->acc_ifuba.ifu_w.ifrw_info;
a2cd4df7
BJ
265 addr->oba = (u_short)info;
266 addr->owc = -((sc->acc_olen + 1) >> 1);
28081cf8 267 cmd = ACC_IE | OUT_ENLB | ((info & 0x30000) >> 12) | ACC_GO;
a2cd4df7
BJ
268#ifdef LOOPBACK
269 cmd |= OUT_BBACK;
270#endif
271 addr->ocsr = cmd;
28081cf8
SL
272 sc->acc_ic->ic_oactive = 1;
273}
274
275/*
276 * Output interrupt handler.
277 */
278accxint(unit)
279{
28081cf8
SL
280 register struct acc_softc *sc = &acc_softc[unit];
281 register struct accdevice *addr;
282
283COUNT(ACCXINT);
7dfb87a5 284 addr = (struct accdevice *)accinfo[unit]->ui_addr;
28081cf8 285 if (sc->acc_ic->ic_oactive == 0) {
7dfb87a5
SL
286 printf("acc%d: stray xmit interrupt, csr=%b\n", unit,
287 addr->ocsr, ACC_OUTBITS);
28081cf8
SL
288 return;
289 }
28081cf8
SL
290 sc->acc_if->if_opackets++;
291 sc->acc_ic->ic_oactive = 0;
7dd4b7cb 292 if (addr->ocsr & ACC_ERR) {
7dfb87a5
SL
293 printf("acc%d: output error, ocsr=%b, icsr=%b\n", unit,
294 addr->ocsr, ACC_OUTBITS, addr->icsr, ACC_INBITS);
28081cf8 295 sc->acc_if->if_oerrors++;
a0b7c7fb 296 }
a2cd4df7
BJ
297 if (sc->acc_ifuba.ifu_xtofree) {
298 m_freem(sc->acc_ifuba.ifu_xtofree);
299 sc->acc_ifuba.ifu_xtofree = 0;
28081cf8 300 }
b690198c
SL
301 if (sc->acc_if->if_snd.ifq_head)
302 accstart(unit);
28081cf8
SL
303}
304
305/*
306 * Input interrupt handler
307 */
308accrint(unit)
309{
310 register struct acc_softc *sc = &acc_softc[unit];
311 register struct accdevice *addr;
28081cf8
SL
312 struct mbuf *m;
313 int len, info;
314
315COUNT(ACCRINT);
7dfb87a5 316 addr = (struct accdevice *)accinfo[unit]->ui_addr;
28081cf8
SL
317 sc->acc_if->if_ipackets++;
318
319 /*
320 * Purge BDP; flush message if error indicated.
321 */
a2cd4df7
BJ
322 if (sc->acc_ifuba.ifu_flags & UBA_NEEDBDP)
323 UBAPURGE(sc->acc_ifuba.ifu_uba, sc->acc_ifuba.ifu_r.ifrw_bdp);
7dd4b7cb 324 if (addr->icsr & ACC_ERR) {
b690198c
SL
325 printf("acc%d: input error, csr=%b\n", unit,
326 addr->icsr, ACC_INBITS);
28081cf8
SL
327 sc->acc_if->if_ierrors++;
328 sc->acc_flush = 1;
329 }
330
331 if (sc->acc_flush) {
a2cd4df7 332 if (addr->icsr & IN_EOM)
28081cf8
SL
333 sc->acc_flush = 0;
334 goto setup;
335 }
e431883e
BJ
336 len = IMPMTU + (addr->iwc << 1);
337 if (len < 0 || len > IMPMTU) {
a2cd4df7
BJ
338 printf("acc%d: bad length=%d\n", len);
339 sc->acc_if->if_ierrors++;
340 goto setup;
341 }
28081cf8
SL
342
343 /*
344 * The last parameter is always 0 since using
345 * trailers on the ARPAnet is insane.
346 */
347 m = if_rubaget(&sc->acc_ifuba, len, 0);
348 if (m == 0)
349 goto setup;
a2cd4df7 350 if ((addr->icsr & IN_EOM) == 0) {
a0b7c7fb 351 if (sc->acc_iq)
28081cf8 352 m_cat(sc->acc_iq, m);
a0b7c7fb 353 else
28081cf8 354 sc->acc_iq = m;
28081cf8
SL
355 goto setup;
356 }
a0b7c7fb 357 if (sc->acc_iq) {
28081cf8
SL
358 m_cat(sc->acc_iq, m);
359 m = sc->acc_iq;
360 sc->acc_iq = 0;
28081cf8
SL
361 }
362 impinput(unit, m);
363
364setup:
365 /*
366 * Setup for next message.
367 */
368 info = sc->acc_ifuba.ifu_r.ifrw_info;
a2cd4df7 369 addr->iba = (u_short)info;
e431883e 370 addr->iwc = -(IMPMTU >> 1);
a2cd4df7 371 addr->icsr =
28081cf8
SL
372 IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
373}
374#endif