xref: /csrg-svn/sys/vax/if/if_pcl.c (revision 13089)
1 /*	if_pcl.c	4.3	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 
116 	sc->sc_if.if_unit = ui->ui_unit;
117 	sc->sc_if.if_name = "pcl";
118 	sc->sc_if.if_mtu = PCLMTU;
119 	sc->sc_if.if_init = pclinit;
120 	sc->sc_if.if_output = pcloutput;
121 	sc->sc_if.if_ioctl = pclioctl;
122 	sc->sc_if.if_reset = pclreset;
123 	sc->sc_ifuba.ifu_flags = UBA_NEEDBDP;
124 	if_attach(&sc->sc_if);
125 }
126 
127 /*
128  * Reset of interface after UNIBUS reset.
129  * If interface is on specified uba, reset its state.
130  */
131 pclreset(unit, uban)
132 	int unit, uban;
133 {
134 	register struct uba_device *ui;
135 
136 	if (unit >= NPCL || (ui = pclinfo[unit]) == 0 || ui->ui_alive == 0 ||
137 	    ui->ui_ubanum != uban)
138 		return;
139 	printf(" pcl%d", unit);
140 	pclinit(unit);
141 }
142 
143 /*
144  * Initialization of interface; clear recorded pending
145  * operations, and reinitialize UNIBUS usage.
146  */
147 pclinit(unit)
148 	int unit;
149 {
150 	register struct pcl_softc *sc = &pcl_softc[unit];
151 	register struct uba_device *ui = pclinfo[unit];
152 	register struct pcldevice *addr;
153 	struct sockaddr_in *sin;
154 	int s;
155 
156 	sin = (struct sockaddr_in *)&sc->sc_if.if_addr;
157 	if (sin->sin_addr.s_addr == 0)
158 		return;
159 	if (if_ubainit(&sc->sc_ifuba, ui->ui_ubanum, 0,
160 	    (int)btoc(PCLMTU)) == 0) {
161 		printf("pcl%d: can't init\n", unit);
162 		sc->sc_if.if_flags &= ~IFF_UP;
163 		return;
164 	}
165 	addr = (struct pcldevice *)ui->ui_addr;
166 	addr->pcl_rcr = PCL_RCINIT;
167 	addr->pcl_tcr = PCL_TXINIT;
168 
169 	/*
170 	 * Hang a receive and start any
171 	 * pending writes by faking a transmit complete.
172 	 */
173 	s = splimp();
174 	addr->pcl_rdba = (short) sc->sc_ifuba.ifu_r.ifrw_info;
175 	addr->pcl_rdbc = -PCLMTU;
176 	addr->pcl_rcr = (((int)(sc->sc_ifuba.ifu_r.ifrw_info>>12))&0x0030) |
177 		PCL_RCNPR | PCL_RCVWD | PCL_RCVDAT | PCL_IE;
178 	sc->sc_oactive = 0;
179 	sc->sc_if.if_flags |= IFF_UP|IFF_RUNNING;
180 	pclstart(unit);
181 	splx(s);
182 	/* Set up routing table entry */
183 	if_rtinit(&sc->sc_if, RTF_UP);
184 }
185 
186 /*
187  * PCL output routine.
188  */
189 pcloutput(ifp, m, dst)
190 	struct ifnet *ifp;
191 	struct mbuf *m;
192 	struct sockaddr *dst;
193 {
194 	int dest, s, error;
195 	struct pcl_header *pclp;
196 	struct mbuf *m2;
197 
198 	switch (dst->sa_family) {
199 
200 #ifdef INET
201 	case AF_INET:
202 		dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
203 		dest = ntohl((u_long)dest);	/* ??? */
204 		dest = dest & 0xff;
205 		break;
206 #endif
207 	default:
208 		printf("pcl%d: can't handle af%d\n", ifp->if_unit,
209 			dst->sa_family);
210 		error = EAFNOSUPPORT;
211 		goto bad;
212 	}
213 
214 	/*
215 	 * Add pseudo local net header.
216 	 * Actually, it does not get transmitted, but merely stripped
217 	 * off and used by the START routine to route the packet.
218 	 * If no space in first mbuf, allocate another.
219 	 */
220 	if (m->m_off > MMAXOFF ||
221 	    MMINOFF + sizeof (struct pcl_header) > m->m_off) {
222 		m2 = m_get(M_DONTWAIT, MT_HEADER);
223 		if (m2 == 0) {
224 			error = ENOBUFS;
225 			goto bad;
226 		}
227 		m2->m_next = m;
228 		m2->m_off = MMINOFF;
229 		m2->m_len = sizeof (struct pcl_header);
230 		m = m2;
231 	} else {
232 		m->m_off -= sizeof (struct pcl_header);
233 		m->m_len += sizeof (struct pcl_header);
234 	}
235 	pclp = mtod(m, struct pcl_header *);
236 	pclp->pcl_dest = dest;
237 
238 	/*
239 	 * Queue message on interface, and start output if interface
240 	 * not yet active.
241 	 */
242 	s = splimp();
243 	if (IF_QFULL(&ifp->if_snd)) {
244 		IF_DROP(&ifp->if_snd);
245 		error = ENOBUFS;
246 		goto qfull;
247 	}
248 	IF_ENQUEUE(&ifp->if_snd, m);
249 	if (pcl_softc[ifp->if_unit].sc_oactive == 0)
250 		pclstart(ifp->if_unit);
251 	splx(s);
252 	return (0);
253 qfull:
254 	splx(s);
255 bad:
256 	m_freem(m);
257 	return (error);
258 }
259 
260 /*
261  * Start or restart output on interface.
262  * If interface is already active, then this is a retransmit.
263  * If interface is not already active, get another datagram
264  * to send off of the interface queue, and map it to the interface
265  * before starting the output.
266  */
267 pclstart(dev)
268 	dev_t dev;
269 {
270         int unit = PCLUNIT(dev);
271 	struct uba_device *ui = pclinfo[unit];
272 	register struct pcl_softc *sc = &pcl_softc[unit];
273 	register struct pcldevice *addr;
274 	struct mbuf *m;
275 
276 	if (sc->sc_oactive)
277 		goto restart;
278 
279 	/*
280 	 * Not already active: dequeue another request
281 	 * and map it to the UNIBUS.  If no more requests,
282 	 * just return.
283 	 */
284 	IF_DEQUEUE(&sc->sc_if.if_snd, m);
285 	if (m == 0) {
286 		sc->sc_oactive = 0;
287 		return;
288 	}
289 
290 	/*
291 	 * Pull destination node out of pseudo-local net header.
292 	 * remove it from outbound data.
293 	 * Note that if_wubaput calls m_bcopy, which is prepared for
294 	 * m_len to be 0 in the first mbuf in the chain.
295 	 */
296 	sc->sc_odest = mtod(m, struct pcl_header *)->pcl_dest;
297 	m->m_off += sizeof (struct pcl_header);
298 	m->m_len -= sizeof (struct pcl_header);
299 
300 	/* Map out to the DMA area */
301 	sc->sc_olen = if_wubaput(&sc->sc_ifuba, m);
302 
303 restart:
304 	/*
305 	 * Have request mapped to UNIBUS for transmission.
306 	 * Purge any stale data from this BDP, and start the otput.
307 	 */
308 	if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP)
309 		UBAPURGE(sc->sc_ifuba.ifu_uba, sc->sc_ifuba.ifu_w.ifrw_bdp);
310 	addr = (struct pcldevice *)ui->ui_addr;
311 	addr->pcl_tcr = PCL_TXINIT;
312 	addr->pcl_tsba = (int)sc->sc_ifuba.ifu_w.ifrw_info;
313 	addr->pcl_tsbc = -sc->sc_olen;
314 
315 	/*
316 	 * RIB (retry if busy) is used on the second and subsequent packets
317 	 * to a single host, because TCP often wants to transmit multiple
318 	 * buffers in a row,
319 	 * and if they are all going to the same place, the second and
320 	 * subsequent ones may be lost due to receiver not ready again yet.
321 	 * This can cause serious problems, because the TCP will resend the
322 	 * whole window, which just repeats the problem.  The result is that
323 	 * a perfectly good link appears not to work unless we take steps here.
324 	 */
325 	addr->pcl_tcr = (((int)(sc->sc_ifuba.ifu_w.ifrw_info>>12))&0x0030) |
326 		((sc->sc_odest & 0xF)<<8) |
327 		PCL_TXNPR | PCL_SNDWD | PCL_STTXM | PCL_IE |
328 		(sc->sc_odest == sc->sc_lastdest ? PCL_RIB : 0);
329 	sc->sc_lastdest = sc->sc_odest;
330 	sc->sc_oactive = 1;
331 }
332 
333 /*
334  * PCL transmitter interrupt.
335  * Start another output if more data to send.
336  */
337 pclxint(unit)
338 	int unit;
339 {
340 	register struct uba_device *ui = pclinfo[unit];
341 	register struct pcl_softc *sc = &pcl_softc[unit];
342 	register struct pcldevice *addr = (struct pcldevice *)ui->ui_addr;
343 
344 	if (sc->sc_oactive == 0) {
345 		printf ("pcl%d: stray interrupt\n", unit);
346 		return;
347 	}
348 	if (addr->pcl_tsr & PCL_ERR) {
349 		sc->sc_lastdest = 0;		/* don't bother with RIB */
350 		if (addr->pcl_tsr & PCL_MSTDWN) {
351 			addr->pcl_tmmr = PCL_MASTER|PCL_AUTOADDR;
352 			pclstart(unit);	/* Retry */
353 			printf("pcl%d: master\n", unit );
354 			return;
355 		}
356 		if (addr->pcl_tsr & PCL_RESPB) {
357 			/* Log as an error */
358 			printf("pcl%d: send error, tcr=%b tsr=%b\n",
359 				unit, addr->pcl_tcr, PCL_TCSRBITS,
360 				addr->pcl_tsr, PCL_TERRBITS);
361 			sc->sc_if.if_oerrors++;
362 		}
363 	} else
364 		sc->sc_if.if_opackets++;
365 	sc->sc_oactive = 0;
366 	if (sc->sc_ifuba.ifu_xtofree) {
367 		m_freem(sc->sc_ifuba.ifu_xtofree);
368 		sc->sc_ifuba.ifu_xtofree = 0;
369 	}
370 	pclstart(unit);
371 }
372 
373 /*
374  * PCL interface receiver interrupt.
375  * If input error just drop packet.
376  */
377 pclrint(unit)
378 	int unit;
379 {
380 	register struct pcl_softc *sc = &pcl_softc[unit];
381 	struct pcldevice *addr = (struct pcldevice *)pclinfo[unit]->ui_addr;
382     	struct mbuf *m;
383 	int len;
384 	register struct ifqueue *inq;
385 
386 	sc->sc_if.if_ipackets++;
387 	/*
388 	 * Purge BDP; drop if input error indicated.
389 	 */
390 	if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP)
391 		UBAPURGE(sc->sc_ifuba.ifu_uba, sc->sc_ifuba.ifu_r.ifrw_bdp);
392 	if (addr->pcl_rsr & PCL_ERR) {
393 		printf("pcl%d: rcv error, rcr=%b rsr=%b\n",
394 			unit, addr->pcl_rcr, PCL_RCSRBITS,
395 			addr->pcl_rsr, PCL_RERRBITS);
396 		sc->sc_if.if_ierrors++;
397 		goto setup;
398 	}
399 	len = PCLMTU + addr->pcl_rdbc;
400 	if (len <= 0 || len > PCLMTU) {
401 		printf("pcl%d: bad len=%d.\n", unit, len);
402 		sc->sc_if.if_ierrors++;
403 		goto setup;
404 	}
405 
406 	/* Really short packets will be part of the startup sequence */
407 	if (len <= 4) {
408 		/* Later, do comming-up processing here */
409 		goto setup;	/* drop packet */
410 	}
411 
412 	/*
413 	 * Pull packet off interface.
414 	 */
415 	m = if_rubaget(&sc->sc_ifuba, len, 0);
416 	if (m == 0)
417 		goto setup;
418 
419 	schednetisr(NETISR_IP);
420 	inq = &ipintrq;
421 
422 	if (IF_QFULL(inq)) {
423 		IF_DROP(inq);
424 		m_freem(m);
425 	} else
426 		IF_ENQUEUE(inq, m);
427 setup:
428 	/*
429 	 * Reset for next packet.
430 	 */
431 	addr->pcl_rcr = PCL_RCINIT;
432 	addr->pcl_rdba = (int)sc->sc_ifuba.ifu_r.ifrw_info;
433 	addr->pcl_rdbc = -PCLMTU;
434 	addr->pcl_rcr = (((int)(sc->sc_ifuba.ifu_w.ifrw_info>>12))&0x0030) |
435 		PCL_RCNPR | PCL_RCVWD | PCL_RCVDAT | PCL_IE;
436 }
437 
438 /*
439  * Process an ioctl request.
440  */
441 pclioctl(ifp, cmd, data)
442 	register struct ifnet *ifp;
443 	int cmd;
444 	caddr_t data;
445 {
446 	struct ifreq *ifr = (struct ifreq *)data;
447 	struct sockaddr_in *sin;
448 	int s = splimp(), error = 0;
449 
450 	switch (cmd) {
451 
452 	case SIOCSIFADDR:
453 		if (ifp->if_flags & IFF_RUNNING)
454 			if_rtinit(ifp, -1);	/* delete previous route */
455 		sin = (struct sockaddr_in *)&ifr->ifr_addr;
456 		ifp->if_addr = *(struct sockaddr *)sin;
457 		ifp->if_net = in_netof(sin->sin_addr);
458 		ifp->if_host[0] = in_lnaof(sin->sin_addr);
459 		if (ifp->if_flags & IFF_RUNNING)
460 			if_rtinit(ifp, RTF_UP);
461 		else
462 			pclinit(ifp->if_unit);
463 		break;
464 
465 	default:
466 		error = EINVAL;
467 	}
468 	splx(s);
469 	return (error);
470 }
471 #endif
472