xref: /csrg-svn/sys/vax/if/if_css.c (revision 24800)
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_css.c	6.5 (Berkeley) 09/16/85
7  */
8 
9 #include "css.h"
10 
11 /*
12  * DEC/CSS IMP11-A ARPAnet IMP interface driver.
13  * Since "imp11a" is such a mouthful, it is called
14  * "css" after the LH/DH being called "acc".
15  *
16  * Configuration notes:
17  *
18  * As delivered from DEC/CSS, it
19  * is addressed and vectored as two DR11-B's.  This makes
20  * Autoconfig almost IMPOSSIBLE.  To make it work, the
21  * interrupt vectors must be restrapped to make the vectors
22  * consecutive.  The 020 hole between the CSR addresses is
23  * tolerated, althought that could be cleaned-up also.
24  *
25  * Additionally, the TRANSMIT side of the IMP11-A has the
26  * lower address of the two subunits, so the vector ordering
27  * in the CONFIG file is reversed from most other devices.
28  * It should be:
29  *
30  * device css0 ....  cssxint cssrint
31  *
32  * If you get it wrong, it will still autoconfig, but will just
33  * sit there with RECEIVE IDLE indicated on the front panel.
34  */
35 #include "../machine/pte.h"
36 
37 #include "param.h"
38 #include "systm.h"
39 #include "mbuf.h"
40 #include "buf.h"
41 #include "protosw.h"
42 #include "socket.h"
43 #include "vmmac.h"
44 
45 #include "../net/if.h"
46 #include "../netimp/if_imp.h"
47 
48 #include "../vax/cpu.h"
49 #include "../vax/mtpr.h"
50 #include "if_cssreg.h"
51 #include "if_uba.h"
52 #include "../vaxuba/ubareg.h"
53 #include "../vaxuba/ubavar.h"
54 
55 int     cssprobe(), cssattach(), cssrint(), cssxint();
56 struct  uba_device *cssinfo[NCSS];
57 u_short cssstd[] = { 0 };
58 struct  uba_driver cssdriver =
59         { cssprobe, 0, cssattach, 0, cssstd, "css", cssinfo };
60 #define CSSUNIT(x)      minor(x)
61 
62 int     cssinit(), cssstart(), cssreset();
63 
64 /*
65  * "Lower half" of IMP interface driver.
66  *
67  * Each IMP interface is handled by a common module which handles
68  * the IMP-host protocol and a hardware driver which manages the
69  * hardware specific details of talking with the IMP.
70  *
71  * The hardware portion of the IMP driver handles DMA and related
72  * management of UNIBUS resources.  The IMP protocol module interprets
73  * contents of these messages and "controls" the actions of the
74  * hardware module during IMP resets, but not, for instance, during
75  * UNIBUS resets.
76  *
77  * The two modules are coupled at "attach time", and ever after,
78  * through the imp interface structure.  Higher level protocols,
79  * e.g. IP, interact with the IMP driver, rather than the CSS.
80  */
81 struct  css_softc {
82         struct  ifnet *css_if;          /* pointer to IMP's ifnet struct */
83         struct  impcb *css_ic;          /* data structure shared with IMP */
84         struct  ifuba css_ifuba;        /* UNIBUS resources */
85         struct  mbuf *css_iq;           /* input reassembly queue */
86         short   css_olen;               /* size of last message sent */
87         char    css_flush;              /* flush remainder of message */
88 } css_softc[NCSS];
89 
90 /*
91  * Reset the IMP and cause a transmitter interrupt by
92  * performing a null DMA.
93  */
94 cssprobe(reg)
95         caddr_t reg;
96 {
97         register int br, cvec;          /* r11, r10 value-result */
98         register struct cssdevice *addr = (struct cssdevice *)reg;
99 
100 #ifdef lint
101         br = 0; cvec = br; br = cvec;
102         cssrint(0); cssxint(0);
103 #endif
104 
105 
106         addr->css_icsr = CSS_CLR;
107         addr->css_ocsr = CSS_CLR;
108         DELAY(50000);
109 	addr->css_icsr = 0;
110 	addr->css_ocsr = 0;
111         DELAY(50000);
112 
113 	addr->css_oba = 0;
114 	addr->css_owc = -1;
115         addr->css_ocsr = CSS_IE | CSS_GO;	/* enable interrupts */
116         DELAY(50000);
117         addr->css_ocsr = 0;
118 
119         return (1);
120 }
121 
122 /*
123  * Call the IMP module to allow it to set up its internal
124  * state, then tie the two modules together by setting up
125  * the back pointers to common data structures.
126  */
127 cssattach(ui)
128         struct uba_device *ui;
129 {
130         register struct css_softc *sc = &css_softc[ui->ui_unit];
131         register struct impcb *ip;
132         struct ifimpcb {
133                 struct  ifnet ifimp_if;
134                 struct  impcb ifimp_impcb;
135         } *ifimp;
136 
137         if ((ifimp = (struct ifimpcb *)impattach(ui, cssreset)) == 0)
138                 return;
139         sc->css_if = &ifimp->ifimp_if;
140         ip = &ifimp->ifimp_impcb;
141         sc->css_ic = ip;
142         ip->ic_init = cssinit;
143         ip->ic_start = cssstart;
144 	sc->css_ifuba.ifu_flags = UBA_CANTWAIT | UBA_NEED16;
145 #ifdef notdef
146 	sc->css_ifuba.ifu_flags =| UBA_NEEDBDP;
147 #endif
148 }
149 
150 /*
151  * Reset interface after UNIBUS reset.
152  * If interface is on specified uba, reset its state.
153  */
154 cssreset(unit, uban)
155         int unit, uban;
156 {
157         register struct uba_device *ui;
158         struct css_softc *sc;
159 
160         if (unit >= NCSS || (ui = cssinfo[unit]) == 0 || ui->ui_alive == 0 ||
161             ui->ui_ubanum != uban)
162                 return;
163         printf(" css%d", unit);
164         sc = &css_softc[unit];
165 	sc->css_if->if_flags &= ~IFF_RUNNING;
166         /* must go through IMP to allow it to set state */
167         (*sc->css_if->if_init)(unit);
168 }
169 
170 /*
171  * Initialize interface: clear recorded pending operations,
172  * and retrieve, and reinitialize UNIBUS resources.
173  */
174 cssinit(unit)
175         int unit;
176 {
177         register struct css_softc *sc;
178         register struct uba_device *ui;
179         register struct cssdevice *addr;
180         int x, info;
181 
182 	if (unit >= NCSS || (ui = cssinfo[unit]) == 0 || ui->ui_alive == 0) {
183 		printf("css%d: not alive\n", unit);
184 		return(0);
185 	}
186 	sc = &css_softc[unit];
187 
188 	/*
189 	 * Header length is 0 to if_ubainit since we have to pass
190 	 * the IMP leader up to the protocol interpretaion
191 	 * routines.  If we had the deader length as
192 	 * sizeof(struct imp_leader), then the if_ routines
193 	 * would assume we handle it on input and output.
194 	 */
195 
196         if (if_ubainit(&sc->css_ifuba, ui->ui_ubanum, 0,(int)btoc(IMPMTU)) == 0) {
197                 printf("css%d: can't initialize\n", unit);
198 		ui->ui_alive = 0;
199 		sc->css_if->if_flags &= ~(IFF_UP | IFF_RUNNING);
200 		return(0);
201         }
202 	sc->css_if->if_flags |= IFF_RUNNING;
203         addr = (struct cssdevice *)ui->ui_addr;
204 
205         /* reset the imp interface. */
206         x = spl5();
207         addr->css_icsr = CSS_CLR;
208         addr->css_ocsr = CSS_CLR;
209 	DELAY(100);
210 	addr->css_icsr = 0;
211 	addr->css_ocsr = 0;
212         addr->css_icsr = IN_HRDY;       /* close the relay */
213 	DELAY(5000);
214         splx(x);
215 
216         /*
217 	 * This may hang if the imp isn't really there.
218 	 * Will test and verify safe operation.
219 	 */
220 
221 	x = 500;
222 	while (x-- > 0) {
223 		if ((addr->css_icsr & (IN_HRDY|IN_IMPNR)) == IN_HRDY)
224 			break;
225                 addr->css_icsr = IN_HRDY;	/* close the relay */
226                 DELAY(5000);
227         }
228 
229 	if (x <= 0) {
230 		printf("css%d: imp doesn't respond, icsr=%b\n", unit,
231 			CSS_INBITS, addr->css_icsr);
232 		goto down;
233 	}
234 
235         /*
236          * Put up a read.  We can't restart any outstanding writes
237          * until we're back in synch with the IMP (i.e. we've flushed
238          * the NOOPs it throws at us).
239 	 * Note: IMPMTU includes the leader.
240          */
241 
242         x = spl5();
243         info = sc->css_ifuba.ifu_r.ifrw_info;
244         addr->css_iba = (u_short)info;
245         addr->css_iwc = -(IMPMTU >> 1);
246         addr->css_icsr =
247                 IN_HRDY | CSS_IE | IN_WEN | ((info & 0x30000) >> 12) | CSS_GO;
248         splx(x);
249 	return(1);
250 
251 down:
252 	ui->ui_alive = 0;
253 	return(0);
254 }
255 
256 /*
257  * Start output on an interface.
258  */
259 cssstart(dev)
260         dev_t dev;
261 {
262         int unit = CSSUNIT(dev), info;
263         struct uba_device *ui = cssinfo[unit];
264         register struct css_softc *sc = &css_softc[unit];
265         register struct cssdevice *addr;
266         struct mbuf *m;
267         u_short cmd;
268 
269         if (sc->css_ic->ic_oactive)
270                 goto restart;
271 
272         /*
273          * Not already active, deqeue a request and
274          * map it onto the UNIBUS.  If no more
275          * requeusts, just return.
276          */
277         IF_DEQUEUE(&sc->css_if->if_snd, m);
278         if (m == 0) {
279                 sc->css_ic->ic_oactive = 0;
280                 return;
281         }
282         sc->css_olen = if_wubaput(&sc->css_ifuba, m);
283 
284 restart:
285         /*
286          * Have request mapped to UNIBUS for transmission.
287          * Purge any stale data from the BDP, and start the output.
288          */
289 	if (sc->css_ifuba.ifu_flags & UBA_NEEDBDP)
290 		UBAPURGE(sc->css_ifuba.ifu_uba, sc->css_ifuba.ifu_w.ifrw_bdp);
291         addr = (struct cssdevice *)ui->ui_addr;
292         info = sc->css_ifuba.ifu_w.ifrw_info;
293         addr->css_oba = (u_short)info;
294         addr->css_owc = -((sc->css_olen + 1) >> 1);
295         cmd = CSS_IE | OUT_ENLB | ((info & 0x30000) >> 12) | CSS_GO;
296         addr->css_ocsr = cmd;
297         sc->css_ic->ic_oactive = 1;
298 }
299 
300 /*
301  * Output interrupt handler.
302  */
303 cssxint(unit)
304 {
305         register struct uba_device *ui = cssinfo[unit];
306         register struct css_softc *sc = &css_softc[unit];
307         register struct cssdevice *addr;
308 
309         addr = (struct cssdevice *)ui->ui_addr;
310         if (sc->css_ic->ic_oactive == 0) {
311                 printf("css%d: stray output interrupt csr=%b\n",
312 			unit, addr->css_ocsr, CSS_OUTBITS);
313                 return;
314         }
315         sc->css_if->if_opackets++;
316         sc->css_ic->ic_oactive = 0;
317         if (addr->css_ocsr & CSS_ERR){
318                 sc->css_if->if_oerrors++;
319                 printf("css%d: output error, ocsr=%b icsr=%b\n", unit,
320                         addr->css_ocsr, CSS_OUTBITS,
321 			addr->css_icsr, CSS_INBITS);
322 	}
323 	if (sc->css_ifuba.ifu_xtofree) {
324 		m_freem(sc->css_ifuba.ifu_xtofree);
325 		sc->css_ifuba.ifu_xtofree = 0;
326 	}
327 	if (sc->css_if->if_snd.ifq_head)
328 		cssstart(unit);
329 }
330 
331 /*
332  * Input interrupt handler
333  */
334 cssrint(unit)
335 {
336         register struct css_softc *sc = &css_softc[unit];
337         register struct cssdevice *addr;
338         struct mbuf *m;
339         int len, info;
340 
341         sc->css_if->if_ipackets++;
342 
343         /*
344          * Purge BDP; flush message if error indicated.
345          */
346 
347         addr = (struct cssdevice *)cssinfo[unit]->ui_addr;
348 	if (sc->css_ifuba.ifu_flags & UBA_NEEDBDP)
349 		UBAPURGE(sc->css_ifuba.ifu_uba, sc->css_ifuba.ifu_r.ifrw_bdp);
350         if (addr->css_icsr & CSS_ERR) {
351                 printf("css%d: recv error, csr=%b\n", unit,
352                     addr->css_icsr, CSS_INBITS);
353                 sc->css_if->if_ierrors++;
354                 sc->css_flush = 1;
355         }
356 
357         if (sc->css_flush) {
358                 if (addr->css_icsr & IN_EOM)
359                         sc->css_flush = 0;
360                 goto setup;
361         }
362 
363         len = IMPMTU + (addr->css_iwc << 1);
364 	if (len < 0 || len > IMPMTU) {
365 		printf("css%d: bad length=%d\n", len);
366 		sc->css_if->if_ierrors++;
367 		goto setup;
368 	}
369 
370         /*
371          * The offset parameter is always 0 since using
372          * trailers on the ARPAnet is insane.
373          */
374         m = if_rubaget(&sc->css_ifuba, len, 0, &sc->css_if);
375         if (m == 0)
376                 goto setup;
377         if ((addr->css_icsr & IN_EOM) == 0) {
378 		if (sc->css_iq)
379 			m_cat(sc->css_iq, m);
380 		else
381 			sc->css_iq = m;
382 		goto setup;
383 	}
384 	if (sc->css_iq) {
385 		m_cat(sc->css_iq, m);
386 		m = sc->css_iq;
387 		sc->css_iq = 0;
388         }
389         impinput(unit, m);
390 
391 setup:
392         /*
393          * Setup for next message.
394          */
395         info = sc->css_ifuba.ifu_r.ifrw_info;
396         addr->css_iba = (u_short)info;
397         addr->css_iwc = - (IMPMTU >> 1);
398         addr->css_icsr =
399                 IN_HRDY | CSS_IE | IN_WEN | ((info & 0x30000) >> 12) | CSS_GO;
400 }
401