xref: /csrg-svn/sys/vax/if/if_pcl.c (revision 13064)
1 /*	if_pcl.c	4.2	83/06/13	*/
2 
3 #include "pcl.h"
4 #if NPCL > 0
5 /*
6  * DEC CSS PCL-11B Parallel Communications Interface
7  *
8  * Written by Mike Muuss.
9  */
10 #include "../machine/pte.h"
11 
12 #include "../h/param.h"
13 #include "../h/systm.h"
14 #include "../h/mbuf.h"
15 #include "../h/buf.h"
16 #include "../h/protosw.h"
17 #include "../h/socket.h"
18 #include "../h/vmmac.h"
19 #include "../h/ioctl.h"
20 #include "../h/errno.h"
21 
22 #include "../net/if.h"
23 #include "../net/netisr.h"
24 #include "../net/route.h"
25 #include "../netinet/in.h"
26 #include "../netinet/in_systm.h"
27 #include "../netinet/ip.h"
28 #include "../netinet/ip_var.h"
29 
30 #include "../vax/cpu.h"
31 #include "../vax/mtpr.h"
32 #include "../vaxif/if_pclreg.h"
33 #include "../vaxif/if_uba.h"
34 #include "../vaxuba/ubareg.h"
35 #include "../vaxuba/ubavar.h"
36 
37 /* The MTU has been carefully selected to prevent fragmentation <-> ArpaNet */
38 #define	PCLMTU	(1006)		/* Max transmission unit (bytes) */
39 
40 int	pclprobe(), pclattach(), pclrint(), pclxint();
41 int	pclinit(), pclioctl(), pcloutput(), pclreset();
42 
43 struct	uba_device	*pclinfo[NPCL];
44 u_short pclstd[] = { 0 };
45 #define	PCLUNIT(x)	minor(x)
46 struct	uba_driver pcldriver =
47 	{ pclprobe, 0, pclattach, 0, pclstd, "pcl", pclinfo };
48 
49 /*
50  * PCL software status per interface.
51  *
52  * Each interface is referenced by a network interface structure,
53  * sc_if, which the routing code uses to locate the interface.
54  * This structure contains the output queue for the interface, its address, ...
55  * We also have, for each interface, a UBA interface structure, which
56  * contains information about the UNIBUS resources held by the interface:
57  * map registers, buffered data paths, etc.  Information is cached in this
58  * structure for use by the if_uba.c routines in running the interface
59  * efficiently.
60  */
61 struct	pcl_softc {
62 	struct	ifnet sc_if;		/* network-visible interface */
63 	struct	ifuba sc_ifuba;		/* UNIBUS resources */
64 	short	sc_oactive;		/* is output active? */
65 	short	sc_olen;		/* length of last output */
66 	short	sc_lastdest;		/* previous destination */
67 	short	sc_odest;		/* current xmit destination */
68 	short	sc_pattern;		/* identification pattern */
69 } pcl_softc[NPCL];
70 
71 /*
72  * Structure of "local header", which only goes between
73  * pcloutput and pclstart.
74  */
75 struct pcl_header {
76 	short	pcl_dest;		/* Destination PCL station */
77 };
78 
79 /*
80  * Do non-DMA output of 1 word to determine presence of interface,
81  * and to find the interupt vector.  1 word messages are a special
82  * case in the receiver routine, and will be discarded.
83  */
84 pclprobe(reg)
85 	caddr_t reg;
86 {
87 	register int br, cvec;		/* r11, r10 value-result */
88 	register struct pcldevice *addr = (struct pcldevice *)reg;
89 
90 #ifdef lint
91 	br = 0; cvec = br; br = cvec;
92 	pclrint(0); pclxint(0);
93 #endif
94 	addr->pcl_rcr = PCL_RCINIT;
95 	addr->pcl_tcr = PCL_TXINIT;
96 	addr->pcl_tsba = 0xFFFE;
97 	/* going for 01777776 */
98 	addr->pcl_tsbc = -4;		/* really short */
99 	addr->pcl_tcr =
100 	 ((1 & 0xF) << 8) | PCL_TXNPR | PCL_SNDWD | PCL_STTXM | PCL_IE | 0x0030;
101 	DELAY(100000);
102 	addr->pcl_tcr = PCL_TXINIT;
103 	return (sizeof (struct pcldevice));
104 }
105 
106 /*
107  * Interface exists: make available by filling in network interface
108  * record.  System will initialize the interface when it is ready
109  * to accept packets.
110  */
111 pclattach(ui)
112 	struct uba_device *ui;
113 {
114 	register struct pcl_softc *sc = &pcl_softc[ui->ui_unit];
115 	register struct sockaddr_in *sin;
116 
117 	sc->sc_if.if_unit = ui->ui_unit;
118 	sc->sc_if.if_name = "pcl";
119 	sc->sc_if.if_mtu = PCLMTU;
120 	sc->sc_if.if_init = pclinit;
121 	sc->sc_if.if_output = pcloutput;
122 	sc->sc_if.if_ioctl = pclioctl;
123 	sc->sc_if.if_reset = pclreset;
124 	sc->sc_ifuba.ifu_flags = UBA_NEEDBDP;
125 	if_attach(&sc->sc_if);
126 }
127 
128 /*
129  * Reset of interface after UNIBUS reset.
130  * If interface is on specified uba, reset its state.
131  */
132 pclreset(unit, uban)
133 	int unit, uban;
134 {
135 	register struct uba_device *ui;
136 
137 	if (unit >= NPCL || (ui = pclinfo[unit]) == 0 || ui->ui_alive == 0 ||
138 	    ui->ui_ubanum != uban)
139 		return;
140 	printf(" pcl%d", unit);
141 	pclinit(unit);
142 }
143 
144 /*
145  * Initialization of interface; clear recorded pending
146  * operations, and reinitialize UNIBUS usage.
147  */
148 pclinit(unit)
149 	int unit;
150 {
151 	register struct pcl_softc *sc = &pcl_softc[unit];
152 	register struct uba_device *ui = pclinfo[unit];
153 	register struct pcldevice *addr;
154 	struct sockaddr_in *sin;
155 	int s;
156 
157 	sin = &sc->sc_if.if_addr;
158 	if (sin->sin_addr.s_addr == 0)
159 		return;
160 	if (if_ubainit(&sc->sc_ifuba, ui->ui_ubanum, 0,
161 	    (int)btoc(PCLMTU)) == 0) {
162 		printf("pcl%d: can't init\n", unit);
163 		sc->sc_if.if_flags &= ~IFF_UP;
164 		return;
165 	}
166 	addr = (struct pcldevice *)ui->ui_addr;
167 	addr->pcl_rcr = PCL_RCINIT;
168 	addr->pcl_tcr = PCL_TXINIT;
169 
170 	/*
171 	 * Hang a receive and start any
172 	 * pending writes by faking a transmit complete.
173 	 */
174 	s = splimp();
175 	addr->pcl_rdba = (short) sc->sc_ifuba.ifu_r.ifrw_info;
176 	addr->pcl_rdbc = -PCLMTU;
177 	addr->pcl_rcr = (((int)(sc->sc_ifuba.ifu_r.ifrw_info>>12))&0x0030) |
178 		PCL_RCNPR | PCL_RCVWD | PCL_RCVDAT | PCL_IE;
179 	sc->sc_oactive = 0;
180 	sc->sc_if.if_flags |= IFF_UP|IFF_RUNNING;
181 	pclstart(unit);
182 	splx(s);
183 	/* Set up routing table entry */
184 	if_rtinit(&sc->sc_if, RTF_UP);
185 }
186 
187 /*
188  * PCL output routine.
189  */
190 pcloutput(ifp, m, dst)
191 	struct ifnet *ifp;
192 	struct mbuf *m;
193 	struct sockaddr *dst;
194 {
195 	int type, dest, s, error;
196 	struct pcl_header *pclp;
197 	struct mbuf *m2;
198 
199 	switch (dst->sa_family) {
200 
201 #ifdef INET
202 	case AF_INET:
203 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
204 		dest = ntohl(dest);	/* ??? */
205 		dest = dest & 0xff;
206 		break;
207 #endif
208 	default:
209 		printf("pcl%d: can't handle af%d\n", ifp->if_unit,
210 			dst->sa_family);
211 		error = EAFNOSUPPORT;
212 		goto bad;
213 	}
214 
215 	/*
216 	 * Add pseudo local net header.
217 	 * Actually, it does not get transmitted, but merely stripped
218 	 * off and used by the START routine to route the packet.
219 	 * If no space in first mbuf, allocate another.
220 	 */
221 	if (m->m_off > MMAXOFF ||
222 	    MMINOFF + sizeof (struct pcl_header) > m->m_off) {
223 		m2 = m_get(M_DONTWAIT, MT_HEADER);
224 		if (m2 == 0) {
225 			error = ENOBUFS;
226 			goto bad;
227 		}
228 		m2->m_next = m;
229 		m2->m_off = MMINOFF;
230 		m2->m_len = sizeof (struct pcl_header);
231 		m = m2;
232 	} else {
233 		m->m_off -= sizeof (struct pcl_header);
234 		m->m_len += sizeof (struct pcl_header);
235 	}
236 	pclp = mtod(m, struct pcl_header *);
237 	pclp->pcl_dest = dest;
238 
239 	/*
240 	 * Queue message on interface, and start output if interface
241 	 * not yet active.
242 	 */
243 	s = splimp();
244 	if (IF_QFULL(&ifp->if_snd)) {
245 		IF_DROP(&ifp->if_snd);
246 		error = ENOBUFS;
247 		goto qfull;
248 	}
249 	IF_ENQUEUE(&ifp->if_snd, m);
250 	if (pcl_softc[ifp->if_unit].sc_oactive == 0)
251 		pclstart(ifp->if_unit);
252 	splx(s);
253 	return (0);
254 qfull:
255 	splx(s);
256 bad:
257 	m_freem(m);
258 	return (error);
259 }
260 
261 /*
262  * Start or restart output on interface.
263  * If interface is already active, then this is a retransmit.
264  * If interface is not already active, get another datagram
265  * to send off of the interface queue, and map it to the interface
266  * before starting the output.
267  */
268 pclstart(dev)
269 	dev_t dev;
270 {
271         int unit = PCLUNIT(dev);
272 	struct uba_device *ui = pclinfo[unit];
273 	register struct pcl_softc *sc = &pcl_softc[unit];
274 	register struct pcldevice *addr;
275 	struct mbuf *m;
276 
277 	if (sc->sc_oactive)
278 		goto restart;
279 
280 	/*
281 	 * Not already active: dequeue another request
282 	 * and map it to the UNIBUS.  If no more requests,
283 	 * just return.
284 	 */
285 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
286 	if (m == 0) {
287 		sc->sc_oactive = 0;
288 		return;
289 	}
290 
291 	/*
292 	 * Pull destination node out of pseudo-local net header.
293 	 * remove it from outbound data.
294 	 * Note that if_wubaput calls m_bcopy, which is prepared for
295 	 * m_len to be 0 in the first mbuf in the chain.
296 	 */
297 	sc->sc_odest = mtod(m, struct pcl_header *)->pcl_dest;
298 	m->m_off += sizeof (struct pcl_header);
299 	m->m_len -= sizeof (struct pcl_header);
300 
301 	/* Map out to the DMA area */
302 	sc->sc_olen = if_wubaput(&sc->sc_ifuba, m);
303 
304 restart:
305 	/*
306 	 * Have request mapped to UNIBUS for transmission.
307 	 * Purge any stale data from this BDP, and start the otput.
308 	 */
309 	if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP)
310 		UBAPURGE(sc->sc_ifuba.ifu_uba, sc->sc_ifuba.ifu_w.ifrw_bdp);
311 	addr = (struct pcldevice *)ui->ui_addr;
312 	addr->pcl_tcr = PCL_TXINIT;
313 	addr->pcl_tsba = (int)sc->sc_ifuba.ifu_w.ifrw_info;
314 	addr->pcl_tsbc = -sc->sc_olen;
315 
316 	/*
317 	 * RIB (retry if busy) is used on the second and subsequent packets
318 	 * to a single host, because TCP often wants to transmit multiple
319 	 * buffers in a row,
320 	 * and if they are all going to the same place, the second and
321 	 * subsequent ones may be lost due to receiver not ready again yet.
322 	 * This can cause serious problems, because the TCP will resend the
323 	 * whole window, which just repeats the problem.  The result is that
324 	 * a perfectly good link appears not to work unless we take steps here.
325 	 */
326 	addr->pcl_tcr = (((int)(sc->sc_ifuba.ifu_w.ifrw_info>>12))&0x0030) |
327 		((sc->sc_odest & 0xF)<<8) |
328 		PCL_TXNPR | PCL_SNDWD | PCL_STTXM | PCL_IE |
329 		(sc->sc_odest == sc->sc_lastdest ? PCL_RIB : 0);
330 	sc->sc_lastdest = sc->sc_odest;
331 	sc->sc_oactive = 1;
332 }
333 
334 /*
335  * PCL transmitter interrupt.
336  * Start another output if more data to send.
337  */
338 pclxint(unit)
339 	int unit;
340 {
341 	register struct uba_device *ui = pclinfo[unit];
342 	register struct pcl_softc *sc = &pcl_softc[unit];
343 	register struct pcldevice *addr = (struct pcldevice *)ui->ui_addr;
344 
345 	if (sc->sc_oactive == 0) {
346 		printf ("pcl%d: stray interrupt\n", unit);
347 		return;
348 	}
349 	if (addr->pcl_tsr & PCL_ERR) {
350 		sc->sc_lastdest = 0;		/* don't bother with RIB */
351 		if (addr->pcl_tsr & PCL_MSTDWN) {
352 			addr->pcl_tmmr = PCL_MASTER|PCL_AUTOADDR;
353 			pclstart(unit);	/* Retry */
354 			printf("pcl%d: master\n", unit );
355 			return;
356 		}
357 		if (addr->pcl_tsr & PCL_RESPB) {
358 			/* Log as an error */
359 			printf("pcl%d: send error, tcr=%b tsr=%b\n",
360 				unit, addr->pcl_tcr, PCL_TCSRBITS,
361 				addr->pcl_tsr, PCL_TERRBITS);
362 			sc->sc_if.if_oerrors++;
363 		}
364 	} else
365 		sc->sc_if.if_opackets++;
366 	sc->sc_oactive = 0;
367 	if (sc->sc_ifuba.ifu_xtofree) {
368 		m_freem(sc->sc_ifuba.ifu_xtofree);
369 		sc->sc_ifuba.ifu_xtofree = 0;
370 	}
371 	pclstart(unit);
372 }
373 
374 /*
375  * PCL interface receiver interrupt.
376  * If input error just drop packet.
377  */
378 pclrint(unit)
379 	int unit;
380 {
381 	register struct pcl_softc *sc = &pcl_softc[unit];
382 	struct pcldevice *addr = (struct pcldevice *)pclinfo[unit]->ui_addr;
383     	struct mbuf *m;
384 	int len, plen; short resid;
385 	register struct ifqueue *inq;
386 	int off;
387 
388 	sc->sc_if.if_ipackets++;
389 	/*
390 	 * Purge BDP; drop if input error indicated.
391 	 */
392 	if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP)
393 		UBAPURGE(sc->sc_ifuba.ifu_uba, sc->sc_ifuba.ifu_r.ifrw_bdp);
394 	if (addr->pcl_rsr & PCL_ERR) {
395 		printf("pcl%d: rcv error, rcr=%b rsr=%b\n",
396 			unit, addr->pcl_rcr, PCL_RCSRBITS,
397 			addr->pcl_rsr, PCL_RERRBITS);
398 		sc->sc_if.if_ierrors++;
399 		goto setup;
400 	}
401 	len = PCLMTU + addr->pcl_rdbc;
402 	if (len <= 0 || len > PCLMTU) {
403 		printf("pcl%d: bad len=%d.\n", unit, len);
404 		sc->sc_if.if_ierrors++;
405 		goto setup;
406 	}
407 
408 	/* Really short packets will be part of the startup sequence */
409 	if (len <= 4) {
410 		/* Later, do comming-up processing here */
411 		goto setup;	/* drop packet */
412 	}
413 
414 	/*
415 	 * Pull packet off interface.
416 	 */
417 	m = if_rubaget(&sc->sc_ifuba, len, 0);
418 	if (m == 0)
419 		goto setup;
420 
421 	schednetisr(NETISR_IP);
422 	inq = &ipintrq;
423 
424 	if (IF_QFULL(inq)) {
425 		IF_DROP(inq);
426 		m_freem(m);
427 	} else
428 		IF_ENQUEUE(inq, m);
429 setup:
430 	/*
431 	 * Reset for next packet.
432 	 */
433 	addr->pcl_rcr = PCL_RCINIT;
434 	addr->pcl_rdba = (int)sc->sc_ifuba.ifu_r.ifrw_info;
435 	addr->pcl_rdbc = -PCLMTU;
436 	addr->pcl_rcr = (((int)(sc->sc_ifuba.ifu_w.ifrw_info>>12))&0x0030) |
437 		PCL_RCNPR | PCL_RCVWD | PCL_RCVDAT | PCL_IE;
438 }
439 
440 /*
441  * Process an ioctl request.
442  */
443 pclioctl(ifp, cmd, data)
444 	register struct ifnet *ifp;
445 	int cmd;
446 	caddr_t data;
447 {
448 	struct ifreq *ifr = (struct ifreq *)data;
449 	struct sockaddr_in *sin;
450 	int s = splimp(), error = 0;
451 
452 	switch (cmd) {
453 
454 	case SIOCSIFADDR:
455 		if (ifp->if_flags & IFF_RUNNING)
456 			if_rtinit(ifp, -1);	/* delete previous route */
457 		sin = (struct sockaddr_in *)&ifr->ifr_addr;
458 		ifp->if_addr = *sin;
459 		ifp->if_net = in_netof(sin->sin_addr);
460 		ifp->if_host[0] = in_lnaof(sin->sin_addr);
461 		if (ifp->if_flags & IFF_RUNNING)
462 			if_rtinit(ifp, RTF_UP);
463 		else
464 			pclinit(ifp->if_unit);
465 		break;
466 
467 	default:
468 		error = EINVAL;
469 	}
470 	splx(s);
471 	return (error);
472 }
473 #endif
474