xref: /csrg-svn/sys/vax/if/if_acc.c (revision 7198)
1 /*	if_acc.c	4.18	82/06/15	*/
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"
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"
25 #include "../net/if_acc.h"
26 #include "../net/if_imp.h"
27 #include "../net/if_uba.h"
28 
29 int     accprobe(), accattach(), accrint(), accxint();
30 struct  uba_device *accinfo[NACC];
31 u_short accstd[] = { 0 };
32 struct  uba_driver accdriver =
33 	{ accprobe, 0, accattach, 0, accstd, "acc", accinfo };
34 #define	ACCUNIT(x)	minor(x)
35 
36 int	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  */
55 struct	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 */
62 } acc_softc[NACC];
63 
64 /*
65  * Reset the IMP and cause a transmitter interrupt by
66  * performing a null DMA.
67  */
68 accprobe(reg)
69 	caddr_t reg;
70 {
71 	register int br, cvec;		/* r11, r10 value-result */
72 	register struct accdevice *addr = (struct accdevice *)reg;
73 
74 COUNT(ACCPROBE);
75 #ifdef lint
76 	br = 0; cvec = br; br = cvec;
77 	accrint(0); accxint(0);
78 #endif
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);
84 	addr->ocsr = 0;
85 	if (cvec && cvec != 0x200)	/* transmit -> receive */
86 		cvec -= 4;
87 #ifdef ECHACK
88 	br = 0x16;
89 #endif
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  */
98 accattach(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 
108 COUNT(ACCATTACH);
109 	if ((ifimp = (struct ifimpcb *)impattach(ui)) == 0)
110 		panic("accattach");
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;
116 	sc->acc_ifuba.ifu_flags = UBA_CANTWAIT;
117 #ifdef notdef
118 	sc->acc_ifuba.ifu_flags |= UBA_NEEDBDP;
119 #endif
120 }
121 
122 /*
123  * Reset interface after UNIBUS reset.
124  * If interface is on specified uba, reset its state.
125  */
126 accreset(unit, uban)
127 	int unit, uban;
128 {
129 	register struct uba_device *ui;
130 	struct acc_softc *sc;
131 
132 COUNT(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,
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.
147  */
148 accinit(unit)
149 	int unit;
150 {
151 	register struct acc_softc *sc;
152 	register struct uba_device *ui;
153 	register struct accdevice *addr;
154 	int info, i;
155 
156 COUNT(ACCINIT);
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 	 */
169 	if (if_ubainit(&sc->acc_ifuba, ui->ui_ubanum, 0,
170 	     (int)btoc(IMPMTU)) == 0) {
171 		printf("acc%d: can't initialize\n", unit);
172 		ui->ui_alive = 0;
173 		return (0);
174 	}
175 	addr = (struct accdevice *)ui->ui_addr;
176 
177 	/*
178 	 * Reset the imp interface;
179 	 * the delays are pure guesswork.
180 	 */
181         addr->ocsr = ACC_RESET; DELAY(5000);
182 	addr->ocsr = OUT_BBACK;	DELAY(5000);	/* reset host master ready */
183 	addr->ocsr = 0;
184 	if (accinputreset(addr, unit) == 0) {
185 		ui->ui_alive = 0;
186 		return (0);
187 	}
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).
193 	 * Note: IMPMTU includes the leader.
194 	 */
195 	info = sc->acc_ifuba.ifu_r.ifrw_info;
196 	addr->iba = (u_short)info;
197 	addr->iwc = -(IMPMTU >> 1);
198 #ifdef LOOPBACK
199 	addr->ocsr |= OUT_BBACK;
200 #endif
201 	addr->icsr =
202 		IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
203 	return (1);
204 }
205 
206 accinputreset(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 
228 /*
229  * Start output on an interface.
230  */
231 accstart(dev)
232 	dev_t dev;
233 {
234 	int unit = ACCUNIT(dev), info;
235 	register struct acc_softc *sc = &acc_softc[unit];
236 	register struct accdevice *addr;
237 	struct mbuf *m;
238 	u_short cmd;
239 
240 COUNT(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 
256 restart:
257 	/*
258 	 * Have request mapped to UNIBUS for
259 	 * transmission; start the output.
260 	 */
261 	if (sc->acc_ifuba.ifu_flags & UBA_NEEDBDP)
262 		UBAPURGE(sc->acc_ifuba.ifu_uba, sc->acc_ifuba.ifu_w.ifrw_bdp);
263 	addr = (struct accdevice *)accinfo[unit]->ui_addr;
264 	info = sc->acc_ifuba.ifu_w.ifrw_info;
265 	addr->oba = (u_short)info;
266 	addr->owc = -((sc->acc_olen + 1) >> 1);
267 	cmd = ACC_IE | OUT_ENLB | ((info & 0x30000) >> 12) | ACC_GO;
268 #ifdef LOOPBACK
269 	cmd |= OUT_BBACK;
270 #endif
271 	addr->ocsr = cmd;
272 	sc->acc_ic->ic_oactive = 1;
273 }
274 
275 /*
276  * Output interrupt handler.
277  */
278 accxint(unit)
279 {
280 	register struct acc_softc *sc = &acc_softc[unit];
281 	register struct accdevice *addr;
282 
283 COUNT(ACCXINT);
284 	addr = (struct accdevice *)accinfo[unit]->ui_addr;
285 	if (sc->acc_ic->ic_oactive == 0) {
286 		printf("acc%d: stray xmit interrupt, csr=%b\n", unit,
287 			addr->ocsr, ACC_OUTBITS);
288 		return;
289 	}
290 	sc->acc_if->if_opackets++;
291 	sc->acc_ic->ic_oactive = 0;
292 	if (addr->ocsr & ACC_ERR) {
293 		printf("acc%d: output error, ocsr=%b, icsr=%b\n", unit,
294 			addr->ocsr, ACC_OUTBITS, addr->icsr, ACC_INBITS);
295 		sc->acc_if->if_oerrors++;
296 	}
297 	if (sc->acc_ifuba.ifu_xtofree) {
298 		m_freem(sc->acc_ifuba.ifu_xtofree);
299 		sc->acc_ifuba.ifu_xtofree = 0;
300 	}
301 	if (sc->acc_if->if_snd.ifq_head)
302 		accstart(unit);
303 }
304 
305 /*
306  * Input interrupt handler
307  */
308 accrint(unit)
309 {
310 	register struct acc_softc *sc = &acc_softc[unit];
311 	register struct accdevice *addr;
312     	struct mbuf *m;
313 	int len, info;
314 
315 COUNT(ACCRINT);
316 	addr = (struct accdevice *)accinfo[unit]->ui_addr;
317 	sc->acc_if->if_ipackets++;
318 
319 	/*
320 	 * Purge BDP; flush message if error indicated.
321 	 */
322 	if (sc->acc_ifuba.ifu_flags & UBA_NEEDBDP)
323 		UBAPURGE(sc->acc_ifuba.ifu_uba, sc->acc_ifuba.ifu_r.ifrw_bdp);
324 	if (addr->icsr & ACC_ERR) {
325 		printf("acc%d: input error, csr=%b\n", unit,
326 		    addr->icsr, ACC_INBITS);
327 		sc->acc_if->if_ierrors++;
328 		sc->acc_flush = 1;
329 	}
330 
331 	if (sc->acc_flush) {
332 		if (addr->icsr & IN_EOM)
333 			sc->acc_flush = 0;
334 		goto setup;
335 	}
336 	len = IMPMTU + (addr->iwc << 1);
337 	if (len < 0 || len > IMPMTU) {
338 		printf("acc%d: bad length=%d\n", len);
339 		sc->acc_if->if_ierrors++;
340 		goto setup;
341 	}
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;
350 	if ((addr->icsr & IN_EOM) == 0) {
351 		if (sc->acc_iq)
352 			m_cat(sc->acc_iq, m);
353 		else
354 			sc->acc_iq = m;
355 		goto setup;
356 	}
357 	if (sc->acc_iq) {
358 		m_cat(sc->acc_iq, m);
359 		m = sc->acc_iq;
360 		sc->acc_iq = 0;
361 	}
362 	impinput(unit, m);
363 
364 setup:
365 	/*
366 	 * Setup for next message.
367 	 */
368 	info = sc->acc_ifuba.ifu_r.ifrw_info;
369 	addr->iba = (u_short)info;
370 	addr->iwc = -(IMPMTU >> 1);
371 	addr->icsr =
372 		IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
373 }
374 #endif
375