xref: /original-bsd/sys/vax/if/if_acc.c (revision 118cce18)
1 /*
2  * Copyright (c) 1982 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)if_acc.c	6.5 (Berkeley) 06/23/85
7  */
8 
9 #include "acc.h"
10 #if NACC > 0
11 
12 /*
13  * ACC LH/DH ARPAnet IMP interface driver.
14  */
15 #include "../machine/pte.h"
16 
17 #include "param.h"
18 #include "systm.h"
19 #include "mbuf.h"
20 #include "buf.h"
21 #include "protosw.h"
22 #include "socket.h"
23 #include "vmmac.h"
24 
25 #include "../net/if.h"
26 #include "../netimp/if_imp.h"
27 
28 #include "../vax/cpu.h"
29 #include "../vax/mtpr.h"
30 #include "if_accreg.h"
31 #include "if_uba.h"
32 #include "../vaxuba/ubareg.h"
33 #include "../vaxuba/ubavar.h"
34 
35 int     accprobe(), accattach(), accrint(), accxint();
36 struct  uba_device *accinfo[NACC];
37 u_short accstd[] = { 0 };
38 struct  uba_driver accdriver =
39 	{ accprobe, 0, accattach, 0, accstd, "acc", accinfo };
40 #define	ACCUNIT(x)	minor(x)
41 
42 int	accinit(), accstart(), accreset();
43 
44 /*
45  * "Lower half" of IMP interface driver.
46  *
47  * Each IMP interface is handled by a common module which handles
48  * the IMP-host protocol and a hardware driver which manages the
49  * hardware specific details of talking with the IMP.
50  *
51  * The hardware portion of the IMP driver handles DMA and related
52  * management of UNIBUS resources.  The IMP protocol module interprets
53  * contents of these messages and "controls" the actions of the
54  * hardware module during IMP resets, but not, for instance, during
55  * UNIBUS resets.
56  *
57  * The two modules are coupled at "attach time", and ever after,
58  * through the imp interface structure.  Higher level protocols,
59  * e.g. IP, interact with the IMP driver, rather than the ACC.
60  */
61 struct	acc_softc {
62 	struct	ifnet *acc_if;		/* pointer to IMP's ifnet struct */
63 	struct	impcb *acc_ic;		/* data structure shared with IMP */
64 	struct	ifuba acc_ifuba;	/* UNIBUS resources */
65 	struct	mbuf *acc_iq;		/* input reassembly queue */
66 	short	acc_olen;		/* size of last message sent */
67 	char	acc_flush;		/* flush remainder of message */
68 } acc_softc[NACC];
69 
70 /*
71  * Reset the IMP and cause a transmitter interrupt by
72  * performing a null DMA.
73  */
74 accprobe(reg)
75 	caddr_t reg;
76 {
77 	register int br, cvec;		/* r11, r10 value-result */
78 	register struct accdevice *addr = (struct accdevice *)reg;
79 
80 #ifdef lint
81 	br = 0; cvec = br; br = cvec;
82 	accrint(0); accxint(0);
83 #endif
84 	addr->icsr = ACC_RESET; DELAY(5000);
85 	addr->ocsr = ACC_RESET; DELAY(5000);
86 	addr->ocsr = OUT_BBACK; DELAY(5000);
87 	addr->owc = 0;
88 	addr->ocsr = ACC_IE | ACC_GO; DELAY(5000);
89 	addr->ocsr = 0;
90 	if (cvec && cvec != 0x200)	/* transmit -> receive */
91 		cvec -= 4;
92 	return (1);
93 }
94 
95 /*
96  * Call the IMP module to allow it to set up its internal
97  * state, then tie the two modules together by setting up
98  * the back pointers to common data structures.
99  */
100 accattach(ui)
101 	struct uba_device *ui;
102 {
103 	register struct acc_softc *sc = &acc_softc[ui->ui_unit];
104 	register struct impcb *ip;
105 	struct ifimpcb {
106 		struct	ifnet ifimp_if;
107 		struct	impcb ifimp_impcb;
108 	} *ifimp;
109 
110 	if ((ifimp = (struct ifimpcb *)impattach(ui, accreset)) == 0)
111 		panic("accattach");
112 	sc->acc_if = &ifimp->ifimp_if;
113 	ip = &ifimp->ifimp_impcb;
114 	sc->acc_ic = ip;
115 	ip->ic_init = accinit;
116 	ip->ic_start = accstart;
117 	sc->acc_ifuba.ifu_flags = UBA_CANTWAIT;
118 #ifdef notdef
119 	sc->acc_ifuba.ifu_flags |= UBA_NEEDBDP;
120 #endif
121 }
122 
123 /*
124  * Reset interface after UNIBUS reset.
125  * If interface is on specified uba, reset its state.
126  */
127 accreset(unit, uban)
128 	int unit, uban;
129 {
130 	register struct uba_device *ui;
131 	struct acc_softc *sc;
132 
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 	sc->acc_if->if_flags &= ~IFF_RUNNING;
139 	/* must go through IMP to allow it to set state */
140 	(*sc->acc_if->if_init)(unit);
141 }
142 
143 /*
144  * Initialize interface: clear recorded pending operations,
145  * and retrieve, and initialize UNIBUS resources.  Note
146  * return value is used by IMP init routine to mark IMP
147  * unavailable for outgoing traffic.
148  */
149 accinit(unit)
150 	int unit;
151 {
152 	register struct acc_softc *sc;
153 	register struct uba_device *ui;
154 	register struct accdevice *addr;
155 	int info;
156 
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 	sc->acc_if->if_flags |= IFF_RUNNING;
176 	addr = (struct accdevice *)ui->ui_addr;
177 
178 	/*
179 	 * Reset the imp interface;
180 	 * the delays are pure guesswork.
181 	 */
182         addr->ocsr = ACC_RESET; DELAY(5000);
183 	addr->ocsr = OUT_BBACK;	DELAY(5000);	/* reset host master ready */
184 	addr->ocsr = 0;
185 	if (accinputreset(addr, unit) == 0) {
186 		ui->ui_alive = 0;
187 		return (0);
188 	}
189 
190 	/*
191 	 * Put up a read.  We can't restart any outstanding writes
192 	 * until we're back in synch with the IMP (i.e. we've flushed
193 	 * the NOOPs it throws at us).
194 	 * Note: IMPMTU includes the leader.
195 	 */
196 	info = sc->acc_ifuba.ifu_r.ifrw_info;
197 	addr->iba = (u_short)info;
198 	addr->iwc = -(IMPMTU >> 1);
199 #ifdef LOOPBACK
200 	addr->ocsr |= OUT_BBACK;
201 #endif
202 	addr->icsr =
203 		IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
204 	return (1);
205 }
206 
207 accinputreset(addr, unit)
208 	register struct accdevice *addr;
209 	register int unit;
210 {
211 	register int i;
212 
213 	addr->icsr = ACC_RESET; DELAY(5000);
214 	addr->icsr = IN_MRDY | IN_WEN;		/* close the relay */
215 	DELAY(10000);
216 	/* YECH!!! */
217 	for (i = 0; i < 500; i++) {
218 		if ((addr->icsr & IN_HRDY) ||
219 		    (addr->icsr & (IN_RMR | IN_IMPBSY)) == 0)
220 			return (1);
221 		addr->icsr = IN_MRDY | IN_WEN; DELAY(10000);
222 		/* keep turning IN_RMR off */
223 	}
224 	printf("acc%d: imp doesn't respond, icsr=%b\n", unit,
225 		addr->icsr, ACC_INBITS);
226 	return (0);
227 }
228 
229 /*
230  * Start output on an interface.
231  */
232 accstart(dev)
233 	dev_t dev;
234 {
235 	int unit = ACCUNIT(dev), info;
236 	register struct acc_softc *sc = &acc_softc[unit];
237 	register struct accdevice *addr;
238 	struct mbuf *m;
239 	u_short cmd;
240 
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 	int unit;
280 {
281 	register struct acc_softc *sc = &acc_softc[unit];
282 	register struct accdevice *addr;
283 
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 	int unit;
310 {
311 	register struct acc_softc *sc = &acc_softc[unit];
312 	register struct accdevice *addr;
313     	struct mbuf *m;
314 	int len, info;
315 
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