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