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