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