xref: /csrg-svn/sys/vax/if/if_acc.c (revision 5647)
1 /*	if_acc.c	4.2	82/02/01	*/
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/accreg.h"
20 #include "../h/cpu.h"
21 #include "../h/mtpr.h"
22 #include "../h/vmmac.h"
23 #include "../net/in.h"
24 #include "../net/in_systm.h"
25 #include "../net/if.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->acc_icsr = ACC_RESET;
80 	DELAY(500000);
81 	addr->acc_ocsr = ACC_RESET;
82 	DELAY(500000);
83 
84 	addr->acc_ocsr = OUT_BBACK;
85 	DELAY(500000);
86 	addr->acc_owc = 0;
87 	addr->acc_ocsr = ACC_IE | ACC_GO;
88 	DELAY(500000);
89 	addr->acc_ocsr = 0;
90 	/* interrupt was for transmit, push back to receive vector */
91 	if (cvec && cvec != 0x200)
92 		cvec -= 4;
93 	return (1);
94 }
95 
96 /*
97  * Call the IMP module to allow it to set up its internal
98  * state, then tie the two modules together by setting up
99  * the back pointers to common data structures.
100  */
101 accattach(ui)
102 	struct uba_device *ui;
103 {
104 	register struct acc_softc *sc = &acc_softc[ui->ui_unit];
105 	register struct impcb *ip;
106 	struct ifimpcb {
107 		struct	ifnet ifimp_if;
108 		struct	impcb ifimp_impcb;
109 	} *ifimp;
110 
111 COUNT(ACCATTACH);
112 	if ((ifimp = (struct ifimpcb *)impattach(ui)) == 0)
113 		panic("accattach");		/* XXX */
114 	sc->acc_if = &ifimp->ifimp_if;
115 	ip = &ifimp->ifimp_impcb;
116 	sc->acc_ic = ip;
117 	ip->ic_init = accinit;
118 	ip->ic_start = accstart;
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 reinitialize UNIBUS resources.
144  */
145 accinit(unit)
146 	int unit;
147 {
148 	register struct acc_softc *sc = &acc_softc[unit];
149 	register struct uba_device *ui = accinfo[unit];
150 	register struct accdevice *addr;
151 	int x, info;
152 
153 COUNT(ACCINIT);
154 	if (if_ubainit(&sc->acc_ifuba, ui->ui_ubanum,
155 	      sizeof(struct imp_leader), (int)btop(IMP_MTU)) == 0) {
156 		printf("acc%d: can't initialize\n", unit);
157 		return;
158 	}
159 	addr = (struct accdevice *)ui->ui_addr;
160 
161 	/*
162 	 * Reset the imp interface.
163 	 * the delays are totally guesses
164 	 */
165 	x = spl5();
166 	addr->acc_icsr = ACC_RESET;
167 	DELAY(100);
168         addr->acc_ocsr = ACC_RESET;
169 	DELAY(1000);
170 	addr->acc_ocsr = OUT_BBACK;	/* reset host master ready */
171 	DELAY(1000);
172 	addr->acc_ocsr = 0;
173 	addr->acc_icsr = IN_MRDY;	/* close the relay */
174 	splx(x);
175 
176 	/* YECH!!! */
177 	while ((addr->acc_icsr & IN_HRDY) == 0 ||
178 	       (addr->acc_icsr & (IN_RMR | IN_IMPBSY))) {
179 		/* keep turning IN_RMR off */
180 		addr->acc_icsr = IN_MRDY;
181 		sleep((caddr_t)&lbolt, PZERO);	/* ??? */
182 	}
183 
184 	/*
185 	 * Put up a read.  We can't restart any outstanding writes
186 	 * until we're back in synch with the IMP (i.e. we've flushed
187 	 * the NOOPs it throws at us).
188 	 */
189 	x = spl5();
190 	info = sc->acc_ifuba.ifu_r.ifrw_info;
191 	addr->acc_iba = (u_short)info;
192 	addr->acc_iwc = -(sizeof(struct imp_leader) + IMP_MTU) >> 1;
193 	addr->acc_icsr =
194 		IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
195 	splx(x);
196 }
197 
198 /*
199  * Start output on an interface.
200  */
201 accstart(dev)
202 	dev_t dev;
203 {
204 	int unit = ACCUNIT(dev), info;
205 	struct uba_device *ui = accinfo[unit];
206 	register struct acc_softc *sc = &acc_softc[unit];
207 	register struct accdevice *addr;
208 	struct mbuf *m;
209 	u_short cmd;
210 
211 COUNT(ACCSTART);
212 	if (sc->acc_ic->ic_oactive)
213 		goto restart;
214 
215 	/*
216 	 * Not already active, deqeue a request and
217 	 * map it onto the UNIBUS.  If no more
218 	 * requeusts, just return.
219 	 */
220 	IF_DEQUEUE(&sc->acc_if->if_snd, m);
221 	if (m == 0) {
222 		sc->acc_ic->ic_oactive = 0;
223 		return;
224 	}
225 	sc->acc_olen = if_wubaput(&sc->acc_ifuba, m);
226 
227 restart:
228 	/*
229 	 * Have request mapped to UNIBUS for transmission.
230 	 * Purge any stale data from the BDP, and start the output.
231 	 */
232 	UBAPURGE(sc->acc_ifuba.ifu_uba, sc->acc_ifuba.ifu_w.ifrw_bdp);
233 	addr = (struct accdevice *)ui->ui_addr;
234 	info = sc->acc_ifuba.ifu_w.ifrw_info;
235 	addr->acc_oba = (u_short)info;
236 	addr->acc_owc = -((sc->acc_olen + 1) >> 1);
237 	cmd = ACC_IE | OUT_ENLB | ((info & 0x30000) >> 12) | ACC_GO;
238 	addr->acc_ocsr = cmd;
239 	sc->acc_ic->ic_oactive = 1;
240 }
241 
242 /*
243  * Output interrupt handler.
244  */
245 accxint(unit)
246 {
247 	register struct uba_device *ui = accinfo[unit];
248 	register struct acc_softc *sc = &acc_softc[unit];
249 	register struct accdevice *addr;
250 
251 COUNT(ACCXINT);
252 	if (sc->acc_ic->ic_oactive == 0) {
253 		printf("acc%d: stray send interrupt\n", unit);
254 		return;
255 	}
256 	addr = (struct accdevice *)ui->ui_addr;
257 	sc->acc_if->if_opackets++;
258 	sc->acc_ic->ic_oactive = 0;
259 	if (addr->acc_ocsr & ACC_ERR) {
260 		printf("acc%d: send error, csr=%b\n", unit,
261 			addr->acc_ocsr, ACC_OUTBITS);
262 		sc->acc_if->if_oerrors++;
263 	}
264 	if (sc->acc_if->if_snd.ifq_head == 0) {
265 		if (sc->acc_ifuba.ifu_xtofree) {
266 			m_freem(sc->acc_ifuba.ifu_xtofree);
267 			sc->acc_ifuba.ifu_xtofree = 0;
268 		}
269 		return;
270 	}
271 	accstart(unit);
272 }
273 
274 /*
275  * Input interrupt handler
276  */
277 accrint(unit)
278 {
279 	register struct acc_softc *sc = &acc_softc[unit];
280 	register struct accdevice *addr;
281 	register struct ifqueue *inq;
282     	struct mbuf *m;
283 	int len, info;
284 
285 COUNT(ACCRINT);
286 	sc->acc_if->if_ipackets++;
287 
288 	/*
289 	 * Purge BDP; flush message if error indicated.
290 	 */
291 	UBAPURGE(sc->acc_ifuba.ifu_uba, sc->acc_ifuba.ifu_r.ifrw_bdp);
292 	addr = (struct accdevice *)accinfo[unit]->ui_addr;
293 	if (addr->acc_icsr & ACC_ERR) {
294 		printf("acc%d: recv error, csr=%b\n", unit,
295 		    addr->acc_icsr, ACC_INBITS);
296 		sc->acc_if->if_ierrors++;
297 		sc->acc_flush = 1;
298 	}
299 
300 	if (sc->acc_flush) {
301 		if (addr->acc_icsr & IN_EOM)
302 			sc->acc_flush = 0;
303 		goto setup;
304 	}
305 	len = sizeof(struct imp_leader) + (addr->acc_iwc << 1);
306 
307 	/*
308 	 * The last parameter is always 0 since using
309 	 * trailers on the ARPAnet is insane.
310 	 */
311 	m = if_rubaget(&sc->acc_ifuba, len, 0);
312 	if (m == 0)
313 		goto setup;
314 	if ((addr->acc_icsr & IN_EOM) == 0) {
315 		if (sc->acc_iq)
316 			m_cat(sc->acc_iq, m);
317 		else
318 			sc->acc_iq = m;
319 		goto setup;
320 	}
321 	/* adjust message length for padding. */
322 	m->m_len -= 2;
323 	if (sc->acc_iq) {
324 		m_cat(sc->acc_iq, m);
325 		m = sc->acc_iq;
326 		sc->acc_iq = 0;
327 	}
328 	impinput(unit, m);
329 
330 setup:
331 	/*
332 	 * Setup for next message.
333 	 */
334 	info = sc->acc_ifuba.ifu_r.ifrw_info;
335 	addr->acc_iba = (u_short)info;
336 	addr->acc_iwc = - (sizeof(struct imp_leader) + IMP_MTU) >> 1;
337 	addr->acc_icsr =
338 		IN_MRDY | ACC_IE | IN_WEN | ((info & 0x30000) >> 12) | ACC_GO;
339 }
340 #endif
341