fix includes
[unix-history] / usr / src / sys / vax / if / if_il.c
CommitLineData
d2cc167c 1/* if_il.c 4.11 82/10/09 */
f0d51478
BF
2
3#include "il.h"
f0d51478
BF
4
5/*
6 * Interlan Ethernet Communications Controller interface
7 */
f0d51478
BF
8#include "../h/param.h"
9#include "../h/systm.h"
10#include "../h/mbuf.h"
11#include "../h/pte.h"
12#include "../h/buf.h"
13#include "../h/protosw.h"
14#include "../h/socket.h"
15#include "../h/ubareg.h"
16#include "../h/ubavar.h"
17#include "../h/ilreg.h"
18#include "../h/cpu.h"
19#include "../h/mtpr.h"
20#include "../h/vmmac.h"
d2cc167c
BJ
21#include "../netinet/in.h"
22#include "../netinet/in_systm.h"
f0d51478 23#include "../net/if.h"
d2cc167c
BJ
24#include "../vaxif/if_il.h"
25#include "../vaxif/if_uba.h"
26#include "../netinet/ip.h"
27#include "../netinet/ip_var.h"
28#include "../netpup/pup.h"
f0d51478
BF
29#include "../net/route.h"
30#include <errno.h>
31
32#define ILMTU 1500
33
34int ilprobe(), ilattach(), ilrint(), ilcint();
35struct uba_device *ilinfo[NIL];
36u_short ilstd[] = { 0 };
37struct uba_driver ildriver =
38 { ilprobe, 0, ilattach, 0, ilstd, "il", ilinfo };
f0d51478 39#define ILUNIT(x) minor(x)
8d38dbc2 40int ilinit(),iloutput(),ilreset(),ilwatch();
f0d51478 41
caf0e371
SL
42u_char il_ectop[3] = { 0x02, 0x60, 0x8c };
43u_char ilbroadcastaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
44
f0d51478
BF
45/*
46 * Ethernet software status per interface.
47 *
48 * Each interface is referenced by a network interface structure,
49 * is_if, which the routing code uses to locate the interface.
50 * This structure contains the output queue for the interface, its address, ...
51 * We also have, for each interface, a UBA interface structure, which
52 * contains information about the UNIBUS resources held by the interface:
53 * map registers, buffered data paths, etc. Information is cached in this
54 * structure for use by the if_uba.c routines in running the interface
55 * efficiently.
56 */
57struct il_softc {
58 struct ifnet is_if; /* network-visible interface */
59 struct ifuba is_ifuba; /* UNIBUS resources */
8d38dbc2
SL
60 int is_flags;
61#define ILF_OACTIVE 0x1 /* output is active */
62#define ILF_RCVPENDING 0x2 /* start rcv in ilcint */
63#define ILF_STATPENDING 0x4 /* stat cmd pending */
64 short is_lastcmd; /* can't read csr, so must save it */
65 short is_scaninterval; /* interval of stat collection */
66#define ILWATCHINTERVAL 60 /* once every 60 seconds */
67 struct il_stats is_stats; /* holds on-board statistics */
68 struct il_stats is_sum; /* summation over time */
69 int is_ubaddr; /* mapping registers of is_stats */
f0d51478
BF
70} il_softc[NIL];
71
f0d51478
BF
72ilprobe(reg)
73 caddr_t reg;
74{
75 register int br, cvec; /* r11, r10 value-result */
76 register struct ildevice *addr = (struct ildevice *)reg;
77 register i;
78
f0d51478
BF
79#ifdef lint
80 br = 0; cvec = br; br = cvec;
8d38dbc2 81 ilrint(0); ilcint(0); ilwatch(0);
f0d51478
BF
82#endif
83
84 addr->il_csr = ILC_OFFLINE|IL_CIE;
85 DELAY(100000);
8d38dbc2 86 i = addr->il_csr; /* clear CDONE */
f0d51478
BF
87 if (cvec > 0 && cvec != 0x200)
88 cvec -= 4;
89 return (1);
90}
91
f0d51478
BF
92/*
93 * Interface exists: make available by filling in network interface
94 * record. System will initialize the interface when it is ready
95 * to accept packets. A STATUS command is done to get the ethernet
96 * address and other interesting data.
97 */
98ilattach(ui)
99 struct uba_device *ui;
100{
101 register struct il_softc *is = &il_softc[ui->ui_unit];
caf0e371 102 register struct ifnet *ifp = &is->is_if;
f0d51478 103 register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
caf0e371 104 struct sockaddr_in *sin;
f0d51478 105
caf0e371
SL
106 ifp->if_unit = ui->ui_unit;
107 ifp->if_name = "il";
108 ifp->if_mtu = ILMTU;
109 ifp->if_net = ui->ui_flags;
f0d51478
BF
110
111 /*
8d38dbc2
SL
112 * Reset the board and map the statistics
113 * buffer onto the Unibus.
f0d51478 114 */
8d38dbc2
SL
115 addr->il_csr = ILC_RESET;
116 while ((addr->il_csr&IL_CDONE) == 0)
117 ;
118 if (addr->il_csr&IL_STATUS)
119 printf("il%d: reset failed, csr=%b\n", ui->ui_unit,
120 addr->il_csr, IL_BITS);
f0d51478 121
8d38dbc2
SL
122 is->is_ubaddr = uballoc(ui->ui_ubanum, &is->is_stats,
123 sizeof (struct il_stats), 0);
124 addr->il_bar = is->is_ubaddr & 0xffff;
125 addr->il_bcr = sizeof (struct il_stats);
126 addr->il_csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT;
127 while ((addr->il_csr&IL_CDONE) == 0)
128 ;
129 if (addr->il_csr&IL_STATUS)
130 printf("il%d: status failed, csr=%b\n", ui->ui_unit,
131 addr->il_csr, IL_BITS);
132 ubarelse(ui->ui_ubanum, &is->is_ubaddr);
f0d51478
BF
133 printf("il%d: addr=%x:%x:%x:%x:%x:%x module=%s firmware=%s\n",
134 ui->ui_unit,
8d38dbc2
SL
135 is->is_stats.ils_addr[0]&0xff, is->is_stats.ils_addr[1]&0xff,
136 is->is_stats.ils_addr[2]&0xff, is->is_stats.ils_addr[3]&0xff,
137 is->is_stats.ils_addr[4]&0xff, is->is_stats.ils_addr[5]&0xff,
138 is->is_stats.ils_module, is->is_stats.ils_firmware);
139 ifp->if_host[0] =
140 ((is->is_stats.ils_addr[3]&0xff)<<16) | 0x800000 |
141 ((is->is_stats.ils_addr[4]&0xff)<<8) |
142 (is->is_stats.ils_addr[5]&0xff);
caf0e371 143 sin = (struct sockaddr_in *)&ifp->if_addr;
f0d51478 144 sin->sin_family = AF_INET;
caf0e371 145 sin->sin_addr = if_makeaddr(ifp->if_net, ifp->if_host[0]);
f0d51478 146
caf0e371 147 sin = (struct sockaddr_in *)&ifp->if_broadaddr;
f0d51478 148 sin->sin_family = AF_INET;
caf0e371
SL
149 sin->sin_addr = if_makeaddr(ifp->if_net, INADDR_ANY);
150 ifp->if_flags = IFF_BROADCAST;
f0d51478 151
caf0e371
SL
152 ifp->if_init = ilinit;
153 ifp->if_output = iloutput;
154 ifp->if_ubareset = ilreset;
8d38dbc2
SL
155 ifp->if_watchdog = ilwatch;
156 is->is_scaninterval = ILWATCHINTERVAL;
157 ifp->if_timer = is->is_scaninterval;
f0d51478 158 is->is_ifuba.ifu_flags = UBA_CANTWAIT;
caf0e371
SL
159#ifdef notdef
160 is->is_ifuba.ifu_flags |= UBA_NEEDBDP;
161#endif
162 if_attach(ifp);
f0d51478
BF
163}
164
165/*
166 * Reset of interface after UNIBUS reset.
167 * If interface is on specified uba, reset its state.
168 */
169ilreset(unit, uban)
170 int unit, uban;
171{
172 register struct uba_device *ui;
f0d51478
BF
173
174 if (unit >= NIL || (ui = ilinfo[unit]) == 0 || ui->ui_alive == 0 ||
175 ui->ui_ubanum != uban)
176 return;
177 printf(" il%d", unit);
178 ilinit(unit);
179}
180
181/*
182 * Initialization of interface; clear recorded pending
183 * operations, and reinitialize UNIBUS usage.
184 */
185ilinit(unit)
186 int unit;
187{
188 register struct il_softc *is = &il_softc[unit];
189 register struct uba_device *ui = ilinfo[unit];
190 register struct ildevice *addr;
8d38dbc2 191 int s;
f0d51478
BF
192
193 if (if_ubainit(&is->is_ifuba, ui->ui_ubanum,
194 sizeof (struct il_rheader), (int)btoc(ILMTU)) == 0) {
195 printf("il%d: can't initialize\n", unit);
196 is->is_if.if_flags &= ~IFF_UP;
197 return;
198 }
8d38dbc2
SL
199 is->is_ubaddr = uballoc(ui->ui_ubanum, &is->is_stats,
200 sizeof (struct il_stats), 0);
f0d51478
BF
201 addr = (struct ildevice *)ui->ui_addr;
202
203 /*
204 * Set board online.
205 * Hang receive buffer and start any pending
206 * writes by faking a transmit complete.
207 * Receive bcr is not a muliple of 4 so buffer
208 * chaining can't happen.
209 */
210 s = splimp();
211 addr->il_csr = ILC_ONLINE;
caf0e371
SL
212 while ((addr->il_csr & IL_CDONE) == 0)
213 ;
f0d51478
BF
214 addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
215 addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
8d38dbc2
SL
216 addr->il_csr =
217 ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
caf0e371
SL
218 while ((addr->il_csr & IL_CDONE) == 0)
219 ;
8d38dbc2 220 is->is_flags = ILF_OACTIVE;
f0d51478 221 is->is_if.if_flags |= IFF_UP;
8d38dbc2 222 is->is_lastcmd = 0;
f0d51478
BF
223 ilcint(unit);
224 splx(s);
a13c006d 225 if_rtinit(&is->is_if, RTF_UP);
f0d51478
BF
226}
227
228/*
229 * Start output on interface.
230 * Get another datagram to send off of the interface queue,
231 * and map it to the interface before starting the output.
232 */
233ilstart(dev)
234 dev_t dev;
235{
caf0e371 236 int unit = ILUNIT(dev), dest, len;
f0d51478
BF
237 struct uba_device *ui = ilinfo[unit];
238 register struct il_softc *is = &il_softc[unit];
239 register struct ildevice *addr;
f0d51478 240 struct mbuf *m;
caf0e371 241 short csr;
f0d51478 242
f0d51478 243 IF_DEQUEUE(&is->is_if.if_snd, m);
8d38dbc2
SL
244 addr = (struct ildevice *)ui->ui_addr;
245 if (m == 0) {
246 if ((is->is_flags & ILF_STATPENDING) == 0)
247 return;
248 addr->il_bar = is->is_ubaddr & 0xfff;
249 addr->il_bcr = sizeof (struct il_stats);
250 csr = ((is->is_ubaddr >> 2) & IL_EUA)|ILC_STAT|IL_RIE|IL_CIE;
251 is->is_flags &= ~ILF_STATPENDING;
252 goto startcmd;
253 }
f0d51478 254 len = if_wubaput(&is->is_ifuba, m);
f0d51478
BF
255 if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
256 UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_w.ifrw_bdp);
f0d51478
BF
257 addr->il_bar = is->is_ifuba.ifu_w.ifrw_info & 0xffff;
258 addr->il_bcr = len;
8d38dbc2
SL
259 csr =
260 ((is->is_ifuba.ifu_w.ifrw_info >> 2) & IL_EUA)|ILC_XMIT|IL_CIE|IL_RIE;
261
262startcmd:
263 is->is_lastcmd = csr & IL_CMD;
caf0e371 264 addr->il_csr = csr;
8d38dbc2 265 is->is_flags |= ILF_OACTIVE;
f0d51478
BF
266}
267
268/*
269 * Command done interrupt.
f0d51478
BF
270 */
271ilcint(unit)
272 int unit;
273{
f0d51478 274 register struct il_softc *is = &il_softc[unit];
caf0e371 275 struct uba_device *ui = ilinfo[unit];
f0d51478 276 register struct ildevice *addr = (struct ildevice *)ui->ui_addr;
5565eee9 277 short csr;
f0d51478 278
8d38dbc2 279 if ((is->is_flags & ILF_OACTIVE) == 0) {
caf0e371 280 printf("il%d: stray xmit interrupt, csr=%b\n", unit,
8d38dbc2 281 addr->il_csr, IL_BITS);
f0d51478
BF
282 return;
283 }
caf0e371 284
5565eee9 285 csr = addr->il_csr;
f0d51478 286 /*
8d38dbc2
SL
287 * Hang receive buffer if it couldn't
288 * be done earlier (in ilrint).
f0d51478 289 */
8d38dbc2 290 if (is->is_flags & ILF_RCVPENDING) {
f0d51478
BF
291 addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
292 addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
8d38dbc2
SL
293 addr->il_csr =
294 ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
caf0e371
SL
295 while ((addr->il_csr & IL_CDONE) == 0)
296 ;
8d38dbc2
SL
297 is->is_flags &= ~ILF_RCVPENDING;
298 }
299 is->is_flags &= ~ILF_OACTIVE;
5565eee9 300 csr &= IL_STATUS;
8d38dbc2
SL
301 switch (is->is_lastcmd) {
302
303 case ILC_XMIT:
304 is->is_if.if_opackets++;
5565eee9 305 if (csr > ILERR_RETRIES)
8d38dbc2
SL
306 is->is_if.if_oerrors++;
307 break;
308
309 case ILC_STAT:
5565eee9 310 if (csr == ILERR_SUCCESS)
8d38dbc2
SL
311 iltotal(is);
312 break;
f0d51478
BF
313 }
314 if (is->is_ifuba.ifu_xtofree) {
315 m_freem(is->is_ifuba.ifu_xtofree);
316 is->is_ifuba.ifu_xtofree = 0;
317 }
8d38dbc2 318 ilstart(unit);
f0d51478
BF
319}
320
321/*
322 * Ethernet interface receiver interrupt.
323 * If input error just drop packet.
324 * Otherwise purge input buffered data path and examine
325 * packet to determine type. If can't determine length
326 * from type, then have to drop packet. Othewise decapsulate
327 * packet based on type and pass to type specific higher-level
328 * input routine.
329 */
330ilrint(unit)
331 int unit;
332{
333 register struct il_softc *is = &il_softc[unit];
334 struct ildevice *addr = (struct ildevice *)ilinfo[unit]->ui_addr;
335 register struct il_rheader *il;
336 struct mbuf *m;
337 int len, off, resid;
338 register struct ifqueue *inq;
339
f0d51478
BF
340 is->is_if.if_ipackets++;
341 if (is->is_ifuba.ifu_flags & UBA_NEEDBDP)
342 UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
343 il = (struct il_rheader *)(is->is_ifuba.ifu_r.ifrw_addr);
344 len = il->ilr_length - sizeof(struct il_rheader);
8d38dbc2 345 if ((il->ilr_status&(ILFSTAT_A|ILFSTAT_C)) || len < 46 || len > ILMTU) {
f0d51478
BF
346 is->is_if.if_ierrors++;
347#ifdef notdef
348 if (is->is_if.if_ierrors % 100 == 0)
349 printf("il%d: += 100 input errors\n", unit);
350#endif
f0d51478
BF
351 goto setup;
352 }
353
354 /*
355 * Deal with trailer protocol: if type is PUP trailer
356 * get true type from first 16-bit word past data.
357 * Remember that type was trailer by setting off.
358 */
359#define ildataaddr(il, off, type) ((type)(((caddr_t)((il)+1)+(off))))
360 if (il->ilr_type >= ILPUP_TRAIL &&
361 il->ilr_type < ILPUP_TRAIL+ILPUP_NTRAILER) {
362 off = (il->ilr_type - ILPUP_TRAIL) * 512;
363 if (off >= ILMTU)
364 goto setup; /* sanity */
365 il->ilr_type = *ildataaddr(il, off, u_short *);
366 resid = *(ildataaddr(il, off+2, u_short *));
367 if (off + resid > len)
368 goto setup; /* sanity */
369 len = off + resid;
370 } else
371 off = 0;
372 if (len == 0)
373 goto setup;
374
375 /*
376 * Pull packet off interface. Off is nonzero if packet
377 * has trailing header; ilget will then force this header
378 * information to be at the front, but we still have to drop
379 * the type and length which are at the front of any trailer data.
380 */
381 m = if_rubaget(&is->is_ifuba, len, off);
382 if (m == 0)
383 goto setup;
384 if (off) {
385 m->m_off += 2 * sizeof (u_short);
386 m->m_len -= 2 * sizeof (u_short);
387 }
388 switch (il->ilr_type) {
389
390#ifdef INET
391 case ILPUP_IPTYPE:
392 schednetisr(NETISR_IP);
393 inq = &ipintrq;
394 break;
395#endif
396 default:
397 m_freem(m);
398 goto setup;
399 }
400
401 if (IF_QFULL(inq)) {
402 IF_DROP(inq);
403 m_freem(m);
caf0e371
SL
404 goto setup;
405 }
406 IF_ENQUEUE(inq, m);
f0d51478
BF
407
408setup:
409 /*
410 * Reset for next packet if possible.
411 * If waiting for transmit command completion, set flag
412 * and wait until command completes.
413 */
8d38dbc2
SL
414 if (is->is_flags & ILF_OACTIVE) {
415 is->is_flags |= ILF_RCVPENDING;
caf0e371
SL
416 return;
417 }
418 addr->il_bar = is->is_ifuba.ifu_r.ifrw_info & 0xffff;
419 addr->il_bcr = sizeof(struct il_rheader) + ILMTU + 6;
8d38dbc2
SL
420 addr->il_csr =
421 ((is->is_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA)|ILC_RCV|IL_RIE;
caf0e371
SL
422 while ((addr->il_csr & IL_CDONE) == 0)
423 ;
f0d51478
BF
424}
425
426/*
427 * Ethernet output routine.
428 * Encapsulate a packet of type family for the local net.
429 * Use trailer local net encapsulation if enough data in first
430 * packet leaves a multiple of 512 bytes of data in remainder.
431 */
432iloutput(ifp, m0, dst)
433 struct ifnet *ifp;
434 struct mbuf *m0;
435 struct sockaddr *dst;
436{
437 int type, dest, s, error;
438 register struct il_softc *is = &il_softc[ifp->if_unit];
439 register struct mbuf *m = m0;
440 register struct il_xheader *il;
caf0e371 441 register int off, i;
f0d51478 442
f0d51478
BF
443 switch (dst->sa_family) {
444
445#ifdef INET
446 case AF_INET:
447 dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
448 off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
449 if (off > 0 && (off & 0x1ff) == 0 &&
450 m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
451 type = ILPUP_TRAIL + (off>>9);
452 m->m_off -= 2 * sizeof (u_short);
453 m->m_len += 2 * sizeof (u_short);
454 *mtod(m, u_short *) = ILPUP_IPTYPE;
455 *(mtod(m, u_short *) + 1) = m->m_len;
456 goto gottrailertype;
457 }
458 type = ILPUP_IPTYPE;
459 off = 0;
460 goto gottype;
461#endif
462
463 default:
464 printf("il%d: can't handle af%d\n", ifp->if_unit,
465 dst->sa_family);
466 error = EAFNOSUPPORT;
467 goto bad;
468 }
469
470gottrailertype:
471 /*
472 * Packet to be sent as trailer: move first packet
473 * (control information) to end of chain.
474 */
475 while (m->m_next)
476 m = m->m_next;
477 m->m_next = m0;
478 m = m0->m_next;
479 m0->m_next = 0;
480 m0 = m;
481
482gottype:
483 /*
484 * Add local net header. If no space in first mbuf,
485 * allocate another.
486 */
487 if (m->m_off > MMAXOFF ||
488 MMINOFF + sizeof (struct il_xheader) > m->m_off) {
489 m = m_get(M_DONTWAIT);
490 if (m == 0) {
491 error = ENOBUFS;
492 goto bad;
493 }
494 m->m_next = m0;
495 m->m_off = MMINOFF;
496 m->m_len = sizeof (struct il_xheader);
497 } else {
498 m->m_off -= sizeof (struct il_xheader);
499 m->m_len += sizeof (struct il_xheader);
500 }
501 il = mtod(m, struct il_xheader *);
502 if ((dest &~ 0xff) == 0)
caf0e371 503 bcopy(ilbroadcastaddr, il->ilx_dhost, 6);
f0d51478 504 else {
8d38dbc2 505 u_char *to = dest & 0x8000 ? is->is_stats.ils_addr : il_ectop;
caf0e371
SL
506
507 bcopy(to, il->ilx_dhost, 3);
f0d51478
BF
508 il->ilx_dhost[3] = (dest>>8) & 0x7f;
509 il->ilx_dhost[4] = (dest>>16) & 0xff;
510 il->ilx_dhost[5] = (dest>>24) & 0xff;
511 }
512 il->ilx_type = type;
513
514 /*
515 * Queue message on interface, and start output if interface
516 * not yet active.
517 */
518 s = splimp();
519 if (IF_QFULL(&ifp->if_snd)) {
520 IF_DROP(&ifp->if_snd);
caf0e371
SL
521 splx(s);
522 m_freem(m);
523 return (ENOBUFS);
f0d51478
BF
524 }
525 IF_ENQUEUE(&ifp->if_snd, m);
8d38dbc2 526 if ((is->is_flags & ILF_OACTIVE) == 0)
f0d51478
BF
527 ilstart(ifp->if_unit);
528 splx(s);
529 return (0);
caf0e371 530
f0d51478
BF
531bad:
532 m_freem(m0);
caf0e371 533 return (error);
f0d51478 534}
8d38dbc2
SL
535
536/*
537 * Watchdog routine, request statistics from board.
538 */
539ilwatch(unit)
540 int unit;
541{
542 register struct il_softc *is = &il_softc[unit];
543 register struct ifnet *ifp = &is->is_if;
544 int s;
545
546 if (is->is_flags & ILF_STATPENDING) {
547 ifp->if_timer = is->is_scaninterval;
548 return;
549 }
550 s = splimp();
551 is->is_flags |= ILF_STATPENDING;
552 if ((is->is_flags & ILF_OACTIVE) == 0)
553 ilstart(ifp->if_unit);
554 splx(s);
555 ifp->if_timer = is->is_scaninterval;
556}
557
558/*
559 * Total up the on-board statistics.
560 */
561iltotal(is)
562 register struct il_softc *is;
563{
564 register u_short *interval, *sum, *end;
565
566 interval = &is->is_stats.ils_frames;
567 sum = &is->is_sum.ils_frames;
568 end = is->is_sum.ils_fill2;
569 while (sum < end)
570 *sum++ += *interval++;
571 is->is_if.if_collisions = is->is_sum.ils_collis;
572}