xref: /original-bsd/sys/vax/if/if_acc.c (revision 3f839ad3)
1 /*	if_acc.c	4.31	82/11/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/vmmac.h"
18 
19 #include "../net/if.h"
20 #include "../netimp/if_imp.h"
21 
22 #include "../vax/cpu.h"
23 #include "../vax/mtpr.h"
24 #include "../vaxif/if_accreg.h"
25 #include "../vaxif/if_uba.h"
26 #include "../vaxuba/ubareg.h"
27 #include "../vaxuba/ubavar.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 #ifdef lint
75 	br = 0; cvec = br; br = cvec;
76 	accrint(0); accxint(0);
77 #endif
78 	addr->icsr = ACC_RESET; DELAY(5000);
79 	addr->ocsr = ACC_RESET; DELAY(5000);
80 	addr->ocsr = OUT_BBACK; DELAY(5000);
81 	addr->owc = 0;
82 	addr->ocsr = ACC_IE | ACC_GO; DELAY(5000);
83 	addr->ocsr = 0;
84 	if (cvec && cvec != 0x200)	/* transmit -> receive */
85 		cvec -= 4;
86 #ifdef ECHACK
87 	br = 0x16;
88 #endif
89 	return (1);
90 }
91 
92 /*
93  * Call the IMP module to allow it to set up its internal
94  * state, then tie the two modules together by setting up
95  * the back pointers to common data structures.
96  */
97 accattach(ui)
98 	struct uba_device *ui;
99 {
100 	register struct acc_softc *sc = &acc_softc[ui->ui_unit];
101 	register struct impcb *ip;
102 	struct ifimpcb {
103 		struct	ifnet ifimp_if;
104 		struct	impcb ifimp_impcb;
105 	} *ifimp;
106 
107 	if ((ifimp = (struct ifimpcb *)impattach(ui, accreset)) == 0)
108 		panic("accattach");
109 	sc->acc_if = &ifimp->ifimp_if;
110 	ip = &ifimp->ifimp_impcb;
111 	sc->acc_ic = ip;
112 	ip->ic_init = accinit;
113 	ip->ic_start = accstart;
114 	sc->acc_ifuba.ifu_flags = UBA_CANTWAIT;
115 #ifdef notdef
116 	sc->acc_ifuba.ifu_flags |= UBA_NEEDBDP;
117 #endif
118 }
119 
120 /*
121  * Reset interface after UNIBUS reset.
122  * If interface is on specified uba, reset its state.
123  */
124 accreset(unit, uban)
125 	int unit, uban;
126 {
127 	register struct uba_device *ui;
128 	struct acc_softc *sc;
129 
130 	if (unit >= NACC || (ui = accinfo[unit]) == 0 || ui->ui_alive == 0 ||
131 	    ui->ui_ubanum != uban)
132 		return;
133 	printf(" acc%d", unit);
134 	sc = &acc_softc[unit];
135 	/* must go through IMP to allow it to set state */
136 	(*sc->acc_if->if_init)(unit);
137 }
138 
139 /*
140  * Initialize interface: clear recorded pending operations,
141  * and retrieve, and initialize UNIBUS resources.  Note
142  * return value is used by IMP init routine to mark IMP
143  * unavailable for outgoing traffic.
144  */
145 accinit(unit)
146 	int unit;
147 {
148 	register struct acc_softc *sc;
149 	register struct uba_device *ui;
150 	register struct accdevice *addr;
151 	int info;
152 
153 	if (unit >= NACC || (ui = accinfo[unit]) == 0 || ui->ui_alive == 0) {
154 		printf("acc%d: not alive\n", unit);
155 		return (0);
156 	}
157 	sc = &acc_softc[unit];
158 	/*
159 	 * Header length is 0 since we have to passs
160 	 * the IMP leader up to the protocol interpretation
161 	 * routines.  If we had the header length as
162 	 * sizeof(struct imp_leader), then the if_ routines
163 	 * would asssume we handle it on input and output.
164 	 */
165 	if (if_ubainit(&sc->acc_ifuba, ui->ui_ubanum, 0,
166 	     (int)btoc(IMPMTU)) == 0) {
167 		printf("acc%d: can't initialize\n", unit);
168 		ui->ui_alive = 0;
169 		return (0);
170 	}
171 	addr = (struct accdevice *)ui->ui_addr;
172 
173 	/*
174 	 * Reset the imp interface;
175 	 * the delays are pure guesswork.
176 	 */
177         addr->ocsr = ACC_RESET; DELAY(5000);
178 	addr->ocsr = OUT_BBACK;	DELAY(5000);	/* reset host master ready */
179 	addr->ocsr = 0;
180 	if (accinputreset(addr, unit) == 0) {
181 		ui->ui_alive = 0;
182 		return (0);
183 	}
184 
185 	/*
186 	 * Put up a read.  We can't restart any outstanding writes
187 	 * until we're back in synch with the IMP (i.e. we've flushed
188 	 * the NOOPs it throws at us).
189 	 * Note: IMPMTU includes the leader.
190 	 */
191 	info = sc->acc_ifuba.ifu_r.ifrw_info;
192 	addr->iba = (u_short)info;
193 	addr->iwc = -(IMPMTU >> 1);
194 #ifdef LOOPBACK
195 	addr->ocsr |= OUT_BBACK;
196 #endif
197 	addr->icsr =
198 		IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
199 	return (1);
200 }
201 
202 accinputreset(addr, unit)
203 	register struct accdevice *addr;
204 	register int unit;
205 {
206 	register int i;
207 
208 	addr->icsr = ACC_RESET; DELAY(5000);
209 	addr->icsr = IN_MRDY | IN_WEN;		/* close the relay */
210 	DELAY(10000);
211 	/* YECH!!! */
212 	for (i = 0; i < 500; i++) {
213 		if ((addr->icsr & IN_HRDY) ||
214 		    (addr->icsr & (IN_RMR | IN_IMPBSY)) == 0)
215 			return (1);
216 		addr->icsr = IN_MRDY | IN_WEN; DELAY(10000);
217 		/* keep turning IN_RMR off */
218 	}
219 	printf("acc%d: imp doesn't respond, icsr=%b\n", unit,
220 		addr->icsr, ACC_INBITS);
221 	return (0);
222 }
223 
224 /*
225  * Start output on an interface.
226  */
227 accstart(dev)
228 	dev_t dev;
229 {
230 	int unit = ACCUNIT(dev), info;
231 	register struct acc_softc *sc = &acc_softc[unit];
232 	register struct accdevice *addr;
233 	struct mbuf *m;
234 	u_short cmd;
235 
236 	if (sc->acc_ic->ic_oactive)
237 		goto restart;
238 
239 	/*
240 	 * Not already active, deqeue a request and
241 	 * map it onto the UNIBUS.  If no more
242 	 * requeusts, just return.
243 	 */
244 	IF_DEQUEUE(&sc->acc_if->if_snd, m);
245 	if (m == 0) {
246 		sc->acc_ic->ic_oactive = 0;
247 		return;
248 	}
249 	sc->acc_olen = if_wubaput(&sc->acc_ifuba, m);
250 
251 restart:
252 	/*
253 	 * Have request mapped to UNIBUS for
254 	 * transmission; start the output.
255 	 */
256 	if (sc->acc_ifuba.ifu_flags & UBA_NEEDBDP)
257 		UBAPURGE(sc->acc_ifuba.ifu_uba, sc->acc_ifuba.ifu_w.ifrw_bdp);
258 	addr = (struct accdevice *)accinfo[unit]->ui_addr;
259 	info = sc->acc_ifuba.ifu_w.ifrw_info;
260 	addr->oba = (u_short)info;
261 	addr->owc = -((sc->acc_olen + 1) >> 1);
262 	cmd = ACC_IE | OUT_ENLB | ((info & 0x30000) >> 12) | ACC_GO;
263 #ifdef LOOPBACK
264 	cmd |= OUT_BBACK;
265 #endif
266 	addr->ocsr = cmd;
267 	sc->acc_ic->ic_oactive = 1;
268 }
269 
270 /*
271  * Output interrupt handler.
272  */
273 accxint(unit)
274 	int unit;
275 {
276 	register struct acc_softc *sc = &acc_softc[unit];
277 	register struct accdevice *addr;
278 
279 	addr = (struct accdevice *)accinfo[unit]->ui_addr;
280 	if (sc->acc_ic->ic_oactive == 0) {
281 		printf("acc%d: stray xmit interrupt, csr=%b\n", unit,
282 			addr->ocsr, ACC_OUTBITS);
283 		return;
284 	}
285 	sc->acc_if->if_opackets++;
286 	sc->acc_ic->ic_oactive = 0;
287 	if (addr->ocsr & ACC_ERR) {
288 		printf("acc%d: output error, ocsr=%b, icsr=%b\n", unit,
289 			addr->ocsr, ACC_OUTBITS, addr->icsr, ACC_INBITS);
290 		sc->acc_if->if_oerrors++;
291 	}
292 	if (sc->acc_ifuba.ifu_xtofree) {
293 		m_freem(sc->acc_ifuba.ifu_xtofree);
294 		sc->acc_ifuba.ifu_xtofree = 0;
295 	}
296 	if (sc->acc_if->if_snd.ifq_head)
297 		accstart(unit);
298 }
299 
300 /*
301  * Input interrupt handler
302  */
303 accrint(unit)
304 	int unit;
305 {
306 	register struct acc_softc *sc = &acc_softc[unit];
307 	register struct accdevice *addr;
308     	struct mbuf *m;
309 	int len, info;
310 
311 	addr = (struct accdevice *)accinfo[unit]->ui_addr;
312 	sc->acc_if->if_ipackets++;
313 
314 	/*
315 	 * Purge BDP; flush message if error indicated.
316 	 */
317 	if (sc->acc_ifuba.ifu_flags & UBA_NEEDBDP)
318 		UBAPURGE(sc->acc_ifuba.ifu_uba, sc->acc_ifuba.ifu_r.ifrw_bdp);
319 	if (addr->icsr & ACC_ERR) {
320 		printf("acc%d: input error, csr=%b\n", unit,
321 		    addr->icsr, ACC_INBITS);
322 		sc->acc_if->if_ierrors++;
323 		sc->acc_flush = 1;
324 	}
325 
326 	if (sc->acc_flush) {
327 		if (addr->icsr & IN_EOM)
328 			sc->acc_flush = 0;
329 		goto setup;
330 	}
331 	len = IMPMTU + (addr->iwc << 1);
332 	if (len < 0 || len > IMPMTU) {
333 		printf("acc%d: bad length=%d\n", len);
334 		sc->acc_if->if_ierrors++;
335 		goto setup;
336 	}
337 
338 	/*
339 	 * The last parameter is always 0 since using
340 	 * trailers on the ARPAnet is insane.
341 	 */
342 	m = if_rubaget(&sc->acc_ifuba, len, 0);
343 	if (m == 0)
344 		goto setup;
345 	if ((addr->icsr & IN_EOM) == 0) {
346 		if (sc->acc_iq)
347 			m_cat(sc->acc_iq, m);
348 		else
349 			sc->acc_iq = m;
350 		goto setup;
351 	}
352 	if (sc->acc_iq) {
353 		m_cat(sc->acc_iq, m);
354 		m = sc->acc_iq;
355 		sc->acc_iq = 0;
356 	}
357 	impinput(unit, m);
358 
359 setup:
360 	/*
361 	 * Setup for next message.
362 	 */
363 	info = sc->acc_ifuba.ifu_r.ifrw_info;
364 	addr->iba = (u_short)info;
365 	addr->iwc = -(IMPMTU >> 1);
366 	addr->icsr =
367 		IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
368 }
369 #endif
370