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