print only occasional error messages
[unix-history] / usr / src / sys / vax / if / if_en.c
CommitLineData
4020bf24 1/* if_en.c 4.41 82/03/15 */
11720282
BJ
2
3#include "en.h"
f1b2fa5b 4
11720282 5/*
f1b2fa5b 6 * Xerox prototype (3 Mb) Ethernet interface driver.
11720282
BJ
7 */
8
9#include "../h/param.h"
10#include "../h/systm.h"
11#include "../h/mbuf.h"
11720282
BJ
12#include "../h/pte.h"
13#include "../h/buf.h"
8a13b737
BJ
14#include "../h/protosw.h"
15#include "../h/socket.h"
11720282
BJ
16#include "../h/ubareg.h"
17#include "../h/ubavar.h"
11720282 18#include "../h/enreg.h"
11720282 19#include "../h/cpu.h"
8a13b737
BJ
20#include "../h/mtpr.h"
21#include "../h/vmmac.h"
22#include "../net/in.h"
23#include "../net/in_systm.h"
24#include "../net/if.h"
25#include "../net/if_en.h"
26#include "../net/if_uba.h"
27#include "../net/ip.h"
28#include "../net/ip_var.h"
31c2345c 29#include "../net/pup.h"
8a13b737 30
a453e1b5 31#define ENMTU (1024+512)
11720282 32
11720282
BJ
33int enprobe(), enattach(), enrint(), enxint(), encollide();
34struct uba_device *eninfo[NEN];
35u_short enstd[] = { 0 };
36struct uba_driver endriver =
b454c3ea 37 { enprobe, 0, enattach, 0, enstd, "en", eninfo };
11720282
BJ
38#define ENUNIT(x) minor(x)
39
f1b2fa5b
BJ
40int eninit(),enoutput(),enreset();
41
42/*
43 * Ethernet software status per interface.
44 *
45 * Each interface is referenced by a network interface structure,
46 * es_if, which the routing code uses to locate the interface.
47 * This structure contains the output queue for the interface, its address, ...
48 * We also have, for each interface, a UBA interface structure, which
49 * contains information about the UNIBUS resources held by the interface:
50 * map registers, buffered data paths, etc. Information is cached in this
51 * structure for use by the if_uba.c routines in running the interface
52 * efficiently.
53 */
8a13b737 54struct en_softc {
f1b2fa5b
BJ
55 struct ifnet es_if; /* network-visible interface */
56 struct ifuba es_ifuba; /* UNIBUS resources */
57 short es_delay; /* current output delay */
58 short es_mask; /* mask for current output delay */
59 u_char es_lastx; /* host last transmitted to */
60 short es_oactive; /* is output active? */
61 short es_olen; /* length of last output */
8a13b737 62} en_softc[NEN];
11720282 63
f1b2fa5b
BJ
64/*
65 * Do output DMA to determine interface presence and
66 * interrupt vector. DMA is too short to disturb other hosts.
67 */
11720282
BJ
68enprobe(reg)
69 caddr_t reg;
70{
b454c3ea 71 register int br, cvec; /* r11, r10 value-result */
11720282
BJ
72 register struct endevice *addr = (struct endevice *)reg;
73
8a13b737 74COUNT(ENPROBE);
11720282
BJ
75#ifdef lint
76 br = 0; cvec = br; br = cvec;
2b4b57cd 77 enrint(0); enxint(0); encollide(0);
11720282 78#endif
11720282 79 addr->en_istat = 0;
11720282
BJ
80 addr->en_owc = -1;
81 addr->en_oba = 0;
941ee7ef 82 addr->en_ostat = EN_IEN|EN_GO;
11720282
BJ
83 DELAY(100000);
84 addr->en_ostat = 0;
11720282
BJ
85 return (1);
86}
87
f1b2fa5b
BJ
88/*
89 * Interface exists: make available by filling in network interface
90 * record. System will initialize the interface when it is ready
91 * to accept packets.
92 */
11720282
BJ
93enattach(ui)
94 struct uba_device *ui;
95{
f1b2fa5b 96 register struct en_softc *es = &en_softc[ui->ui_unit];
8a13b737 97COUNT(ENATTACH);
f1b2fa5b
BJ
98
99 es->es_if.if_unit = ui->ui_unit;
b454c3ea 100 es->es_if.if_name = "en";
f1b2fa5b
BJ
101 es->es_if.if_mtu = ENMTU;
102 es->es_if.if_net = ui->ui_flags;
103 es->es_if.if_host[0] =
a453e1b5 104 (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff;
f7bbb8eb
BJ
105#ifdef ENKLUDGE
106 if (es->es_if.if_net == 10) {
107 es->es_if.if_host[0] <<= 16;
108 es->es_if.if_host[0] |= 0x4e;
109 }
110#endif
f1b2fa5b
BJ
111 es->es_if.if_addr =
112 if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
1dd55890
BJ
113 es->es_if.if_broadaddr =
114 if_makeaddr(es->es_if.if_net, 0);
f1b2fa5b 115 es->es_if.if_init = eninit;
b454c3ea 116 es->es_if.if_output = enoutput;
f1b2fa5b 117 es->es_if.if_ubareset = enreset;
7a66118b 118 es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16;
405c9168 119 if_attach(&es->es_if);
11720282
BJ
120}
121
f1b2fa5b
BJ
122/*
123 * Reset of interface after UNIBUS reset.
124 * If interface is on specified uba, reset its state.
125 */
126enreset(unit, uban)
127 int unit, uban;
8a13b737 128{
11720282 129 register struct uba_device *ui;
f1b2fa5b 130COUNT(ENRESET);
11720282 131
b454c3ea
BJ
132 if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
133 ui->ui_ubanum != uban)
f1b2fa5b 134 return;
b454c3ea 135 printf(" en%d", unit);
f1b2fa5b
BJ
136 eninit(unit);
137}
138
139/*
140 * Initialization of interface; clear recorded pending
141 * operations, and reinitialize UNIBUS usage.
142 */
143eninit(unit)
144 int unit;
145{
b454c3ea
BJ
146 register struct en_softc *es = &en_softc[unit];
147 register struct uba_device *ui = eninfo[unit];
f1b2fa5b 148 register struct endevice *addr;
f1b2fa5b
BJ
149 int s;
150
f1b2fa5b 151 if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
81950b45 152 sizeof (struct en_header), (int)btoc(ENMTU)) == 0) {
b454c3ea 153 printf("en%d: can't initialize\n", unit);
8a13b737 154 return;
11720282 155 }
11720282 156 addr = (struct endevice *)ui->ui_addr;
8a13b737 157 addr->en_istat = addr->en_ostat = 0;
11720282 158
f1b2fa5b 159 /*
b454c3ea
BJ
160 * Hang a receive and start any
161 * pending writes by faking a transmit complete.
f1b2fa5b
BJ
162 */
163 s = splimp();
b454c3ea
BJ
164 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
165 addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
166 addr->en_istat = EN_IEN|EN_GO;
167 es->es_oactive = 1;
f1b2fa5b
BJ
168 enxint(unit);
169 splx(s);
11720282
BJ
170}
171
3552a746 172int enlastdel = 25;
8a13b737 173
f1b2fa5b
BJ
174/*
175 * Start or restart output on interface.
176 * If interface is already active, then this is a retransmit
177 * after a collision, and just restuff registers and delay.
178 * If interface is not already active, get another datagram
179 * to send off of the interface queue, and map it to the interface
180 * before starting the output.
181 */
ae348eea 182enstart(dev)
11720282
BJ
183 dev_t dev;
184{
b454c3ea
BJ
185 int unit = ENUNIT(dev);
186 struct uba_device *ui = eninfo[unit];
187 register struct en_softc *es = &en_softc[unit];
8a13b737 188 register struct endevice *addr;
8a13b737
BJ
189 struct mbuf *m;
190 int dest;
ae348eea 191COUNT(ENSTART);
11720282 192
8a13b737
BJ
193 if (es->es_oactive)
194 goto restart;
f1b2fa5b
BJ
195
196 /*
197 * Not already active: dequeue another request
198 * and map it to the UNIBUS. If no more requests,
199 * just return.
200 */
201 IF_DEQUEUE(&es->es_if.if_snd, m);
8a13b737
BJ
202 if (m == 0) {
203 es->es_oactive = 0;
11720282
BJ
204 return;
205 }
a453e1b5 206 dest = mtod(m, struct en_header *)->en_dhost;
8a13b737 207 es->es_olen = if_wubaput(&es->es_ifuba, m);
f1b2fa5b
BJ
208
209 /*
210 * Ethernet cannot take back-to-back packets (no
211 * buffering in interface. To avoid overrunning
212 * receiver, enforce a small delay (about 1ms) in interface
213 * on successive packets sent to same host.
214 */
8a13b737
BJ
215 if (es->es_lastx && es->es_lastx == dest)
216 es->es_delay = enlastdel;
217 else
218 es->es_lastx = dest;
f1b2fa5b 219
8a13b737 220restart:
f1b2fa5b
BJ
221 /*
222 * Have request mapped to UNIBUS for transmission.
223 * Purge any stale data from this BDP, and start the otput.
224 */
225 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
11720282 226 addr = (struct endevice *)ui->ui_addr;
405c9168 227 addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
8a13b737
BJ
228 addr->en_odelay = es->es_delay;
229 addr->en_owc = -((es->es_olen + 1) >> 1);
941ee7ef 230 addr->en_ostat = EN_IEN|EN_GO;
8a13b737 231 es->es_oactive = 1;
11720282
BJ
232}
233
f1b2fa5b
BJ
234/*
235 * Ethernet interface transmitter interrupt.
236 * Start another output if more data to send.
237 */
11720282
BJ
238enxint(unit)
239 int unit;
240{
b454c3ea
BJ
241 register struct uba_device *ui = eninfo[unit];
242 register struct en_softc *es = &en_softc[unit];
11720282 243 register struct endevice *addr;
11720282
BJ
244COUNT(ENXINT);
245
8a13b737
BJ
246 if (es->es_oactive == 0)
247 return;
11720282 248 addr = (struct endevice *)ui->ui_addr;
b454c3ea 249 es->es_if.if_opackets++;
8a13b737
BJ
250 es->es_oactive = 0;
251 es->es_delay = 0;
252 es->es_mask = ~0;
89413846
BJ
253 if (addr->en_ostat&EN_OERROR) {
254 es->es_if.if_oerrors++;
4020bf24
BJ
255 if (es->es_if.if_oerrors % 100 == 0)
256 printf("en%d: += 100 output errors\n", unit);
89413846 257 }
7a66118b
BJ
258 if (es->es_ifuba.ifu_xtofree) {
259 m_freem(es->es_ifuba.ifu_xtofree);
260 es->es_ifuba.ifu_xtofree = 0;
261 }
f1b2fa5b 262 if (es->es_if.if_snd.ifq_head == 0) {
8a13b737 263 es->es_lastx = 0;
11720282
BJ
264 return;
265 }
8a13b737 266 enstart(unit);
11720282
BJ
267}
268
f1b2fa5b
BJ
269/*
270 * Collision on ethernet interface. Do exponential
271 * backoff, and retransmit. If have backed off all
272 * the way printing warning diagnostic, and drop packet.
273 */
11720282
BJ
274encollide(unit)
275 int unit;
276{
b454c3ea 277 register struct en_softc *es = &en_softc[unit];
11720282
BJ
278COUNT(ENCOLLIDE);
279
f1b2fa5b 280 es->es_if.if_collisions++;
8a13b737 281 if (es->es_oactive == 0)
11720282 282 return;
b454c3ea
BJ
283 /*
284 * Es_mask is a 16 bit number with n low zero bits, with
285 * n the number of backoffs. When es_mask is 0 we have
286 * backed off 16 times, and give up.
287 */
8a13b737 288 if (es->es_mask == 0) {
a453e1b5 289 printf("en%d: send error\n", unit);
8a13b737 290 enxint(unit);
b454c3ea 291 return;
11720282 292 }
b454c3ea
BJ
293 /*
294 * Another backoff. Restart with delay based on n low bits
295 * of the interval timer.
296 */
297 es->es_mask <<= 1;
298 es->es_delay = mfpr(ICR) &~ es->es_mask;
299 enstart(unit);
11720282
BJ
300}
301
31c2345c
SL
302struct sockaddr_pup pupsrc = { AF_PUP };
303struct sockaddr_pup pupdst = { AF_PUP };
304struct sockproto pupproto = { PF_PUP };
f1b2fa5b
BJ
305/*
306 * Ethernet interface receiver interrupt.
307 * If input error just drop packet.
308 * Otherwise purge input buffered data path and examine
309 * packet to determine type. If can't determine length
310 * from type, then have to drop packet. Othewise decapsulate
311 * packet based on type and pass to type specific higher-level
312 * input routine.
313 */
11720282
BJ
314enrint(unit)
315 int unit;
316{
b454c3ea
BJ
317 register struct en_softc *es = &en_softc[unit];
318 struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
319 register struct en_header *en;
8a13b737 320 struct mbuf *m;
b454c3ea
BJ
321 int len;
322 register struct ifqueue *inq;
8a13b737 323 int off;
11720282
BJ
324COUNT(ENRINT);
325
b454c3ea 326 es->es_if.if_ipackets++;
f1b2fa5b
BJ
327
328 /*
b454c3ea 329 * Purge BDP; drop if input error indicated.
f1b2fa5b
BJ
330 */
331 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
941ee7ef 332 if (addr->en_istat&EN_IERROR) {
f1b2fa5b 333 es->es_if.if_ierrors++;
4020bf24
BJ
334 if (es->es_if.if_ierrors % 100 == 0)
335 printf("en%d: += 100 input errors\n", unit);
8a13b737 336 goto setup;
11720282 337 }
f1b2fa5b
BJ
338
339 /*
340 * Get pointer to ethernet header (in input buffer).
341 * Deal with trailer protocol: if type is PUP trailer
342 * get true type from first 16-bit word past data.
343 * Remember that type was trailer by setting off.
344 */
345 en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
8a13b737
BJ
346#define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off))))
347 if (en->en_type >= ENPUP_TRAIL &&
348 en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
349 off = (en->en_type - ENPUP_TRAIL) * 512;
b454c3ea
BJ
350 if (off >= ENMTU)
351 goto setup; /* sanity */
8a13b737 352 en->en_type = *endataaddr(en, off, u_short *);
8a13b737
BJ
353 } else
354 off = 0;
f1b2fa5b
BJ
355
356 /*
357 * Attempt to infer packet length from type;
358 * can't deal with packet if can't infer length.
359 */
8a13b737
BJ
360 switch (en->en_type) {
361
362#ifdef INET
363 case ENPUP_IPTYPE:
6255745d
BJ
364 len = htons((u_short)endataaddr(en, off ? off+2 : 0, struct ip *)->ip_len);
365 if (off)
366 len += 2;
11720282
BJ
367 break;
368#endif
31c2345c
SL
369#ifdef PUP
370 case ENPUP_PUPTYPE:
371 len = endataaddr(en, off, struct pup_header *)->pup_length;
372 if (off)
373 len -= 2;
374 break;
375#endif
376
8a13b737 377 default:
5d855bdf 378 printf("en%d: unknown pkt type 0x%x\n", unit, en->en_type);
8a13b737 379 goto setup;
11720282 380 }
8a13b737
BJ
381 if (len == 0)
382 goto setup;
f1b2fa5b
BJ
383
384 /*
385 * Pull packet off interface. Off is nonzero if packet
386 * has trailing header; if_rubaget will then force this header
387 * information to be at the front, but we still have to drop
b454c3ea 388 * the two-byte type which is at the front of any trailer data.
f1b2fa5b
BJ
389 */
390 m = if_rubaget(&es->es_ifuba, len, off);
a453e1b5
BJ
391 if (m == 0)
392 goto setup;
f1b2fa5b
BJ
393 if (off) {
394 m->m_off += 2;
395 m->m_len -= 2;
396 }
31c2345c
SL
397 switch (en->en_type) {
398
399#ifdef INET
400 case ENPUP_IPTYPE:
401 setipintr();
402 inq = &ipintrq;
403 break;
404#endif
405 case ENPUP_PUPTYPE: {
406 struct pup_header *pup = mtod(m, struct pup_header *);
407
408 pupproto.sp_protocol = pup->pup_type;
409 pupdst.spup_addr = pup->pup_dport;
410 pupsrc.spup_addr = pup->pup_sport;
668cc26d
SL
411 raw_input(m, &pupproto, (struct sockaddr *)&pupdst,
412 (struct sockaddr *)&pupsrc);
31c2345c
SL
413 goto setup;
414 }
415 }
1e977657
BJ
416 if (IF_QFULL(inq)) {
417 IF_DROP(inq);
418 (void) m_freem(m);
419 } else
420 IF_ENQUEUE(inq, m);
f1b2fa5b 421
ae348eea 422setup:
f1b2fa5b
BJ
423 /*
424 * Reset for next packet.
425 */
426 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
8a13b737 427 addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
941ee7ef 428 addr->en_istat = EN_IEN|EN_GO;
ae348eea 429}
11720282 430
8a13b737
BJ
431/*
432 * Ethernet output routine.
433 * Encapsulate a packet of type family for the local net.
f1b2fa5b
BJ
434 * Use trailer local net encapsulation if enough data in first
435 * packet leaves a multiple of 512 bytes of data in remainder.
8a13b737
BJ
436 */
437enoutput(ifp, m0, pf)
438 struct ifnet *ifp;
439 struct mbuf *m0;
440 int pf;
11720282 441{
9979b4f2 442 int type, dest, s, off;
f1b2fa5b 443 register struct mbuf *m = m0;
8a13b737 444 register struct en_header *en;
8a13b737 445
9d1b03e0 446COUNT(ENOUTPUT);
8a13b737 447 switch (pf) {
11720282 448
8a13b737
BJ
449#ifdef INET
450 case PF_INET: {
451 register struct ip *ip = mtod(m0, struct ip *);
8a13b737 452
f7bbb8eb 453#ifndef ENKLUDGE
f1b2fa5b 454 dest = ip->ip_dst.s_addr >> 24;
f7bbb8eb
BJ
455#else
456 dest = (ip->ip_dst.s_addr >> 8) & 0xff;
457#endif
6255745d 458 off = ntohs((u_short)ip->ip_len) - m->m_len;
86532ef6 459#ifndef ENKLUDGE
b454c3ea 460 if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) {
8a13b737 461 type = ENPUP_TRAIL + (off>>9);
f1b2fa5b
BJ
462 m->m_off -= 2;
463 m->m_len += 2;
464 *mtod(m, u_short *) = ENPUP_IPTYPE;
465 goto gottrailertype;
8a13b737 466 }
86532ef6 467#endif
f1b2fa5b
BJ
468 type = ENPUP_IPTYPE;
469 off = 0;
470 goto gottype;
8a13b737 471 }
8a13b737 472#endif
31c2345c
SL
473#ifdef PUP
474 case PF_PUP: {
475 register struct pup_header *pup = mtod(m, struct pup_header *);
476
477 dest = pup->pup_dhost;
478 off = pup->pup_length - m->m_len;
479 if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) {
480 type = ENPUP_TRAIL + (off>>9);
481 m->m_off -= 2;
482 m->m_len += 2;
483 *mtod(m, u_short *) = ENPUP_PUPTYPE;
484 goto gottrailertype;
485 }
486 type = ENPUP_PUPTYPE;
487 off = 0;
488 goto gottype;
489 }
490#endif
8a13b737
BJ
491
492 default:
493 printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
494 m_freem(m0);
495 return (0);
496 }
f1b2fa5b 497
b454c3ea 498gottrailertype:
f1b2fa5b
BJ
499 /*
500 * Packet to be sent as trailer: move first packet
501 * (control information) to end of chain.
502 */
f1b2fa5b
BJ
503 while (m->m_next)
504 m = m->m_next;
505 m->m_next = m0;
506 m = m0->m_next;
507 m0->m_next = 0;
b454c3ea 508 m0 = m;
f1b2fa5b 509
b454c3ea 510gottype:
f1b2fa5b
BJ
511 /*
512 * Add local net header. If no space in first mbuf,
513 * allocate another.
514 */
a453e1b5
BJ
515 if (m->m_off > MMAXOFF ||
516 MMINOFF + sizeof (struct en_header) > m->m_off) {
ef9b4258 517 m = m_get(M_DONTWAIT);
8a13b737
BJ
518 if (m == 0) {
519 m_freem(m0);
520 return (0);
521 }
522 m->m_next = m0;
523 m->m_off = MMINOFF;
524 m->m_len = sizeof (struct en_header);
525 } else {
8a13b737
BJ
526 m->m_off -= sizeof (struct en_header);
527 m->m_len += sizeof (struct en_header);
11720282 528 }
8a13b737
BJ
529 en = mtod(m, struct en_header *);
530 en->en_shost = ifp->if_host[0];
531 en->en_dhost = dest;
532 en->en_type = type;
f1b2fa5b
BJ
533
534 /*
535 * Queue message on interface, and start output if interface
536 * not yet active.
537 */
8a13b737 538 s = splimp();
1e977657
BJ
539 if (IF_QFULL(&ifp->if_snd)) {
540 IF_DROP(&ifp->if_snd);
541 m_freem(m);
542 splx(s);
543 return (0);
544 }
8a13b737 545 IF_ENQUEUE(&ifp->if_snd, m);
8a13b737
BJ
546 if (en_softc[ifp->if_unit].es_oactive == 0)
547 enstart(ifp->if_unit);
89413846 548 splx(s);
f1b2fa5b 549 return (1);
11720282 550}