convert interfaces to sockaddr's and add hooks for routing
[unix-history] / usr / src / sys / vax / if / if_en.c
CommitLineData
ee787340 1/* if_en.c 4.44 82/03/28 */
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];
ee787340 97 register struct sockaddr_in *sin;
8a13b737 98COUNT(ENATTACH);
f1b2fa5b
BJ
99
100 es->es_if.if_unit = ui->ui_unit;
b454c3ea 101 es->es_if.if_name = "en";
f1b2fa5b
BJ
102 es->es_if.if_mtu = ENMTU;
103 es->es_if.if_net = ui->ui_flags;
104 es->es_if.if_host[0] =
ee787340
SL
105 (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff;
106 sin = (struct sockaddr_in *)&es->es_if.if_addr;
107 sin->sin_family = AF_INET;
108 sin->sin_addr = if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
109 sin = (struct sockaddr_in *)&es->es_if.if_broadaddr;
110 sin->sin_family = AF_INET;
111 sin->sin_addr = if_makeaddr(es->es_if.if_net, 0);
112 es->es_if.if_flags = IFF_BROADCAST;
f1b2fa5b 113 es->es_if.if_init = eninit;
b454c3ea 114 es->es_if.if_output = enoutput;
f1b2fa5b 115 es->es_if.if_ubareset = enreset;
7a66118b 116 es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16;
405c9168 117 if_attach(&es->es_if);
11720282
BJ
118}
119
f1b2fa5b
BJ
120/*
121 * Reset of interface after UNIBUS reset.
122 * If interface is on specified uba, reset its state.
123 */
124enreset(unit, uban)
125 int unit, uban;
8a13b737 126{
11720282 127 register struct uba_device *ui;
f1b2fa5b 128COUNT(ENRESET);
11720282 129
b454c3ea
BJ
130 if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
131 ui->ui_ubanum != uban)
f1b2fa5b 132 return;
b454c3ea 133 printf(" en%d", unit);
f1b2fa5b
BJ
134 eninit(unit);
135}
136
137/*
138 * Initialization of interface; clear recorded pending
139 * operations, and reinitialize UNIBUS usage.
140 */
141eninit(unit)
142 int unit;
143{
b454c3ea
BJ
144 register struct en_softc *es = &en_softc[unit];
145 register struct uba_device *ui = eninfo[unit];
f1b2fa5b 146 register struct endevice *addr;
f1b2fa5b
BJ
147 int s;
148
f1b2fa5b 149 if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
81950b45 150 sizeof (struct en_header), (int)btoc(ENMTU)) == 0) {
b454c3ea 151 printf("en%d: can't initialize\n", unit);
ee787340 152 es->es_if.if_flags &= ~IFF_UP;
8a13b737 153 return;
11720282 154 }
11720282 155 addr = (struct endevice *)ui->ui_addr;
8a13b737 156 addr->en_istat = addr->en_ostat = 0;
11720282 157
f1b2fa5b 158 /*
b454c3ea
BJ
159 * Hang a receive and start any
160 * pending writes by faking a transmit complete.
f1b2fa5b
BJ
161 */
162 s = splimp();
b454c3ea
BJ
163 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
164 addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
165 addr->en_istat = EN_IEN|EN_GO;
166 es->es_oactive = 1;
ee787340 167 es->es_if.if_flags |= IFF_UP;
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 */
ee787340
SL
225 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
226 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
11720282 227 addr = (struct endevice *)ui->ui_addr;
405c9168 228 addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
8a13b737
BJ
229 addr->en_odelay = es->es_delay;
230 addr->en_owc = -((es->es_olen + 1) >> 1);
941ee7ef 231 addr->en_ostat = EN_IEN|EN_GO;
8a13b737 232 es->es_oactive = 1;
11720282
BJ
233}
234
f1b2fa5b
BJ
235/*
236 * Ethernet interface transmitter interrupt.
237 * Start another output if more data to send.
238 */
11720282
BJ
239enxint(unit)
240 int unit;
241{
b454c3ea
BJ
242 register struct uba_device *ui = eninfo[unit];
243 register struct en_softc *es = &en_softc[unit];
519a1ecf 244 register struct endevice *addr = (struct endevice *)ui->ui_addr;
11720282
BJ
245COUNT(ENXINT);
246
8a13b737
BJ
247 if (es->es_oactive == 0)
248 return;
519a1ecf 249 if (es->es_mask && (addr->en_ostat&EN_OERROR)) {
89413846 250 es->es_if.if_oerrors++;
4020bf24
BJ
251 if (es->es_if.if_oerrors % 100 == 0)
252 printf("en%d: += 100 output errors\n", unit);
519a1ecf
BJ
253 endocoll(unit);
254 return;
89413846 255 }
519a1ecf
BJ
256 es->es_if.if_opackets++;
257 es->es_oactive = 0;
258 es->es_delay = 0;
259 es->es_mask = ~0;
7a66118b
BJ
260 if (es->es_ifuba.ifu_xtofree) {
261 m_freem(es->es_ifuba.ifu_xtofree);
262 es->es_ifuba.ifu_xtofree = 0;
263 }
f1b2fa5b 264 if (es->es_if.if_snd.ifq_head == 0) {
8a13b737 265 es->es_lastx = 0;
11720282
BJ
266 return;
267 }
8a13b737 268 enstart(unit);
11720282
BJ
269}
270
f1b2fa5b
BJ
271/*
272 * Collision on ethernet interface. Do exponential
273 * backoff, and retransmit. If have backed off all
ee787340 274 * the way print warning diagnostic, and drop packet.
f1b2fa5b 275 */
11720282
BJ
276encollide(unit)
277 int unit;
278{
519a1ecf 279 struct en_softc *es = &en_softc[unit];
11720282
BJ
280COUNT(ENCOLLIDE);
281
f1b2fa5b 282 es->es_if.if_collisions++;
8a13b737 283 if (es->es_oactive == 0)
11720282 284 return;
519a1ecf
BJ
285 endocoll(unit);
286}
287
288endocoll(unit)
289 int unit;
290{
291 register struct en_softc *es = &en_softc[unit];
292
b454c3ea
BJ
293 /*
294 * Es_mask is a 16 bit number with n low zero bits, with
295 * n the number of backoffs. When es_mask is 0 we have
296 * backed off 16 times, and give up.
297 */
8a13b737 298 if (es->es_mask == 0) {
a453e1b5 299 printf("en%d: send error\n", unit);
8a13b737 300 enxint(unit);
b454c3ea 301 return;
11720282 302 }
b454c3ea
BJ
303 /*
304 * Another backoff. Restart with delay based on n low bits
305 * of the interval timer.
306 */
307 es->es_mask <<= 1;
308 es->es_delay = mfpr(ICR) &~ es->es_mask;
309 enstart(unit);
11720282
BJ
310}
311
31c2345c
SL
312struct sockaddr_pup pupsrc = { AF_PUP };
313struct sockaddr_pup pupdst = { AF_PUP };
314struct sockproto pupproto = { PF_PUP };
f1b2fa5b
BJ
315/*
316 * Ethernet interface receiver interrupt.
317 * If input error just drop packet.
318 * Otherwise purge input buffered data path and examine
319 * packet to determine type. If can't determine length
320 * from type, then have to drop packet. Othewise decapsulate
321 * packet based on type and pass to type specific higher-level
322 * input routine.
323 */
11720282
BJ
324enrint(unit)
325 int unit;
326{
b454c3ea
BJ
327 register struct en_softc *es = &en_softc[unit];
328 struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
329 register struct en_header *en;
8a13b737 330 struct mbuf *m;
b454c3ea
BJ
331 int len;
332 register struct ifqueue *inq;
8a13b737 333 int off;
11720282
BJ
334COUNT(ENRINT);
335
b454c3ea 336 es->es_if.if_ipackets++;
f1b2fa5b
BJ
337
338 /*
b454c3ea 339 * Purge BDP; drop if input error indicated.
f1b2fa5b 340 */
ee787340
SL
341 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
342 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
941ee7ef 343 if (addr->en_istat&EN_IERROR) {
f1b2fa5b 344 es->es_if.if_ierrors++;
4020bf24
BJ
345 if (es->es_if.if_ierrors % 100 == 0)
346 printf("en%d: += 100 input errors\n", unit);
8a13b737 347 goto setup;
11720282 348 }
f1b2fa5b
BJ
349
350 /*
351 * Get pointer to ethernet header (in input buffer).
352 * Deal with trailer protocol: if type is PUP trailer
353 * get true type from first 16-bit word past data.
354 * Remember that type was trailer by setting off.
355 */
356 en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
8a13b737
BJ
357#define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off))))
358 if (en->en_type >= ENPUP_TRAIL &&
359 en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
360 off = (en->en_type - ENPUP_TRAIL) * 512;
b454c3ea
BJ
361 if (off >= ENMTU)
362 goto setup; /* sanity */
8a13b737 363 en->en_type = *endataaddr(en, off, u_short *);
8a13b737
BJ
364 } else
365 off = 0;
f1b2fa5b
BJ
366
367 /*
368 * Attempt to infer packet length from type;
369 * can't deal with packet if can't infer length.
370 */
8a13b737
BJ
371 switch (en->en_type) {
372
373#ifdef INET
374 case ENPUP_IPTYPE:
ee787340
SL
375 len = htons((u_short)endataaddr(en,
376 off ? off + sizeof (u_short) : 0, struct ip *)->ip_len);
6255745d 377 if (off)
ee787340 378 len += sizeof (u_short);
11720282
BJ
379 break;
380#endif
31c2345c
SL
381#ifdef PUP
382 case ENPUP_PUPTYPE:
ee787340
SL
383 len = endataaddr(en, off ? off + sizeof (u_short) : 0,
384 struct pup_header *)->pup_length;
31c2345c 385 if (off)
ee787340 386 len -= sizeof (u_short);
31c2345c
SL
387 break;
388#endif
389
8a13b737 390 default:
5d855bdf 391 printf("en%d: unknown pkt type 0x%x\n", unit, en->en_type);
8a13b737 392 goto setup;
11720282 393 }
8a13b737
BJ
394 if (len == 0)
395 goto setup;
f1b2fa5b
BJ
396
397 /*
398 * Pull packet off interface. Off is nonzero if packet
399 * has trailing header; if_rubaget will then force this header
400 * information to be at the front, but we still have to drop
b454c3ea 401 * the two-byte type which is at the front of any trailer data.
f1b2fa5b
BJ
402 */
403 m = if_rubaget(&es->es_ifuba, len, off);
a453e1b5
BJ
404 if (m == 0)
405 goto setup;
f1b2fa5b 406 if (off) {
ee787340
SL
407 m->m_off += sizeof (u_short);
408 m->m_len -= sizeof (u_short);
f1b2fa5b 409 }
31c2345c
SL
410 switch (en->en_type) {
411
412#ifdef INET
413 case ENPUP_IPTYPE:
9c8692e9 414 schednetisr(NETISR_IP);
31c2345c
SL
415 inq = &ipintrq;
416 break;
417#endif
ee787340 418#ifdef PUP
31c2345c
SL
419 case ENPUP_PUPTYPE: {
420 struct pup_header *pup = mtod(m, struct pup_header *);
421
422 pupproto.sp_protocol = pup->pup_type;
423 pupdst.spup_addr = pup->pup_dport;
424 pupsrc.spup_addr = pup->pup_sport;
668cc26d 425 raw_input(m, &pupproto, (struct sockaddr *)&pupdst,
ee787340 426 (struct sockaddr *)&pupsrc);
31c2345c 427 goto setup;
31c2345c 428 }
ee787340
SL
429#endif
430 }
431
1e977657
BJ
432 if (IF_QFULL(inq)) {
433 IF_DROP(inq);
ee787340 434 m_freem(m);
1e977657
BJ
435 } else
436 IF_ENQUEUE(inq, m);
f1b2fa5b 437
ae348eea 438setup:
f1b2fa5b
BJ
439 /*
440 * Reset for next packet.
441 */
442 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
8a13b737 443 addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
941ee7ef 444 addr->en_istat = EN_IEN|EN_GO;
ae348eea 445}
11720282 446
8a13b737
BJ
447/*
448 * Ethernet output routine.
449 * Encapsulate a packet of type family for the local net.
f1b2fa5b
BJ
450 * Use trailer local net encapsulation if enough data in first
451 * packet leaves a multiple of 512 bytes of data in remainder.
8a13b737 452 */
ee787340 453enoutput(ifp, m0, dst)
8a13b737
BJ
454 struct ifnet *ifp;
455 struct mbuf *m0;
ee787340 456 struct sockaddr *dst;
11720282 457{
ee787340 458 int type, dest, s;
f1b2fa5b 459 register struct mbuf *m = m0;
8a13b737 460 register struct en_header *en;
ee787340 461 register int off;
8a13b737 462
9d1b03e0 463COUNT(ENOUTPUT);
ee787340 464 switch (dst->sa_family) {
11720282 465
8a13b737 466#ifdef INET
ee787340
SL
467 case AF_INET:
468 dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr >> 24;
469 off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
470 if (off > 0 && (off & 0x1ff) == 0 &&
471 m->m_off >= MMINOFF + sizeof (u_short)) {
8a13b737 472 type = ENPUP_TRAIL + (off>>9);
ee787340
SL
473 m->m_off -= sizeof (u_short);
474 m->m_len += sizeof (u_short);
f1b2fa5b
BJ
475 *mtod(m, u_short *) = ENPUP_IPTYPE;
476 goto gottrailertype;
8a13b737 477 }
f1b2fa5b
BJ
478 type = ENPUP_IPTYPE;
479 off = 0;
480 goto gottype;
8a13b737 481#endif
31c2345c 482#ifdef PUP
ee787340
SL
483 case AF_PUP:
484 dest = ((struct sockaddr_pup *)dst)->spup_addr.pp_host;
485 off = mtod(m, struct pup_header *)->pup_length - m->m_len;
486 if (off > 0 && (off & 0x1ff) == 0 &&
487 m->m_off >= MMINOFF + sizeof (u_short)) {
31c2345c 488 type = ENPUP_TRAIL + (off>>9);
ee787340
SL
489 m->m_off -= sizeof (u_short);
490 m->m_len += sizeof (u_short);
31c2345c
SL
491 *mtod(m, u_short *) = ENPUP_PUPTYPE;
492 goto gottrailertype;
493 }
494 type = ENPUP_PUPTYPE;
495 off = 0;
496 goto gottype;
31c2345c 497#endif
8a13b737
BJ
498
499 default:
ee787340
SL
500 printf("en%d: can't handle af%d\n", ifp->if_unit,
501 dst->sa_family);
8a13b737
BJ
502 m_freem(m0);
503 return (0);
504 }
f1b2fa5b 505
b454c3ea 506gottrailertype:
f1b2fa5b
BJ
507 /*
508 * Packet to be sent as trailer: move first packet
509 * (control information) to end of chain.
510 */
f1b2fa5b
BJ
511 while (m->m_next)
512 m = m->m_next;
513 m->m_next = m0;
514 m = m0->m_next;
515 m0->m_next = 0;
b454c3ea 516 m0 = m;
f1b2fa5b 517
b454c3ea 518gottype:
f1b2fa5b
BJ
519 /*
520 * Add local net header. If no space in first mbuf,
521 * allocate another.
522 */
a453e1b5
BJ
523 if (m->m_off > MMAXOFF ||
524 MMINOFF + sizeof (struct en_header) > m->m_off) {
ef9b4258 525 m = m_get(M_DONTWAIT);
8a13b737
BJ
526 if (m == 0) {
527 m_freem(m0);
528 return (0);
529 }
530 m->m_next = m0;
531 m->m_off = MMINOFF;
532 m->m_len = sizeof (struct en_header);
533 } else {
8a13b737
BJ
534 m->m_off -= sizeof (struct en_header);
535 m->m_len += sizeof (struct en_header);
11720282 536 }
8a13b737
BJ
537 en = mtod(m, struct en_header *);
538 en->en_shost = ifp->if_host[0];
539 en->en_dhost = dest;
540 en->en_type = type;
f1b2fa5b
BJ
541
542 /*
543 * Queue message on interface, and start output if interface
544 * not yet active.
545 */
8a13b737 546 s = splimp();
1e977657
BJ
547 if (IF_QFULL(&ifp->if_snd)) {
548 IF_DROP(&ifp->if_snd);
549 m_freem(m);
550 splx(s);
551 return (0);
552 }
8a13b737 553 IF_ENQUEUE(&ifp->if_snd, m);
8a13b737
BJ
554 if (en_softc[ifp->if_unit].es_oactive == 0)
555 enstart(ifp->if_unit);
89413846 556 splx(s);
f1b2fa5b 557 return (1);
11720282 558}