sun merge
[unix-history] / usr / src / sys / vax / if / if_en.c
CommitLineData
961945a8 1/* if_en.c 4.75 82/12/17 */
11720282
BJ
2
3#include "en.h"
f1b2fa5b 4
11720282 5/*
f1b2fa5b 6 * Xerox prototype (3 Mb) Ethernet interface driver.
11720282 7 */
961945a8 8#include "../machine/pte.h"
11720282
BJ
9
10#include "../h/param.h"
11#include "../h/systm.h"
12#include "../h/mbuf.h"
11720282 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;
b4e3e570
SL
124#if defined(VAX750)
125 /* don't chew up 750 bdp's */
126 if (cpu == VAX_750 && ui->ui_unit > 0)
127 es->es_ifuba.ifu_flags &= ~UBA_NEEDBDP;
128#endif
405c9168 129 if_attach(&es->es_if);
11720282
BJ
130}
131
f1b2fa5b
BJ
132/*
133 * Reset of interface after UNIBUS reset.
134 * If interface is on specified uba, reset its state.
135 */
136enreset(unit, uban)
137 int unit, uban;
8a13b737 138{
11720282 139 register struct uba_device *ui;
11720282 140
b454c3ea
BJ
141 if (unit >= NEN || (ui = eninfo[unit]) == 0 || ui->ui_alive == 0 ||
142 ui->ui_ubanum != uban)
f1b2fa5b 143 return;
b454c3ea 144 printf(" en%d", unit);
f1b2fa5b
BJ
145 eninit(unit);
146}
147
148/*
149 * Initialization of interface; clear recorded pending
150 * operations, and reinitialize UNIBUS usage.
151 */
152eninit(unit)
153 int unit;
154{
b454c3ea
BJ
155 register struct en_softc *es = &en_softc[unit];
156 register struct uba_device *ui = eninfo[unit];
f1b2fa5b 157 register struct endevice *addr;
f1b2fa5b
BJ
158 int s;
159
f1b2fa5b 160 if (if_ubainit(&es->es_ifuba, ui->ui_ubanum,
dc39362e 161 sizeof (struct en_header), (int)btoc(ENMRU)) == 0) {
b454c3ea 162 printf("en%d: can't initialize\n", unit);
ee787340 163 es->es_if.if_flags &= ~IFF_UP;
8a13b737 164 return;
11720282 165 }
11720282 166 addr = (struct endevice *)ui->ui_addr;
8a13b737 167 addr->en_istat = addr->en_ostat = 0;
11720282 168
f1b2fa5b 169 /*
b454c3ea
BJ
170 * Hang a receive and start any
171 * pending writes by faking a transmit complete.
f1b2fa5b
BJ
172 */
173 s = splimp();
b454c3ea 174 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
dc39362e 175 addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
b454c3ea
BJ
176 addr->en_istat = EN_IEN|EN_GO;
177 es->es_oactive = 1;
ee787340 178 es->es_if.if_flags |= IFF_UP;
f1b2fa5b
BJ
179 enxint(unit);
180 splx(s);
a13c006d 181 if_rtinit(&es->es_if, RTF_UP);
11720282
BJ
182}
183
0658a645 184int enalldelay = 0;
4c073c48 185int enlastdel = 50;
0658a645 186int enlastmask = (~0) << 5;
8a13b737 187
f1b2fa5b
BJ
188/*
189 * Start or restart output on interface.
190 * If interface is already active, then this is a retransmit
191 * after a collision, and just restuff registers and delay.
192 * If interface is not already active, get another datagram
193 * to send off of the interface queue, and map it to the interface
194 * before starting the output.
195 */
ae348eea 196enstart(dev)
11720282
BJ
197 dev_t dev;
198{
b454c3ea
BJ
199 int unit = ENUNIT(dev);
200 struct uba_device *ui = eninfo[unit];
201 register struct en_softc *es = &en_softc[unit];
8a13b737 202 register struct endevice *addr;
8a13b737
BJ
203 struct mbuf *m;
204 int dest;
11720282 205
8a13b737
BJ
206 if (es->es_oactive)
207 goto restart;
f1b2fa5b
BJ
208
209 /*
210 * Not already active: dequeue another request
211 * and map it to the UNIBUS. If no more requests,
212 * just return.
213 */
214 IF_DEQUEUE(&es->es_if.if_snd, m);
8a13b737
BJ
215 if (m == 0) {
216 es->es_oactive = 0;
11720282
BJ
217 return;
218 }
a453e1b5 219 dest = mtod(m, struct en_header *)->en_dhost;
8a13b737 220 es->es_olen = if_wubaput(&es->es_ifuba, m);
f1b2fa5b
BJ
221
222 /*
223 * Ethernet cannot take back-to-back packets (no
0658a645
BJ
224 * buffering in interface. To help avoid overrunning
225 * receivers, enforce a small delay (about 1ms) in interface:
226 * * between all packets when enalldelay
227 * * whenever last packet was broadcast
228 * * whenever this packet is to same host as last packet
f1b2fa5b 229 */
0658a645 230 if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) {
8a13b737 231 es->es_delay = enlastdel;
0658a645
BJ
232 es->es_mask = enlastmask;
233 }
234 es->es_lastx = dest;
f1b2fa5b 235
8a13b737 236restart:
f1b2fa5b
BJ
237 /*
238 * Have request mapped to UNIBUS for transmission.
239 * Purge any stale data from this BDP, and start the otput.
240 */
ee787340
SL
241 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
242 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
11720282 243 addr = (struct endevice *)ui->ui_addr;
405c9168 244 addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
8a13b737
BJ
245 addr->en_odelay = es->es_delay;
246 addr->en_owc = -((es->es_olen + 1) >> 1);
941ee7ef 247 addr->en_ostat = EN_IEN|EN_GO;
8a13b737 248 es->es_oactive = 1;
11720282
BJ
249}
250
f1b2fa5b
BJ
251/*
252 * Ethernet interface transmitter interrupt.
253 * Start another output if more data to send.
254 */
11720282
BJ
255enxint(unit)
256 int unit;
257{
b454c3ea
BJ
258 register struct uba_device *ui = eninfo[unit];
259 register struct en_softc *es = &en_softc[unit];
519a1ecf 260 register struct endevice *addr = (struct endevice *)ui->ui_addr;
11720282 261
8a13b737
BJ
262 if (es->es_oactive == 0)
263 return;
519a1ecf 264 if (es->es_mask && (addr->en_ostat&EN_OERROR)) {
89413846 265 es->es_if.if_oerrors++;
519a1ecf
BJ
266 endocoll(unit);
267 return;
89413846 268 }
519a1ecf
BJ
269 es->es_if.if_opackets++;
270 es->es_oactive = 0;
271 es->es_delay = 0;
272 es->es_mask = ~0;
7a66118b
BJ
273 if (es->es_ifuba.ifu_xtofree) {
274 m_freem(es->es_ifuba.ifu_xtofree);
275 es->es_ifuba.ifu_xtofree = 0;
276 }
f1b2fa5b 277 if (es->es_if.if_snd.ifq_head == 0) {
0658a645 278 es->es_lastx = 256; /* putatively illegal */
11720282
BJ
279 return;
280 }
8a13b737 281 enstart(unit);
11720282
BJ
282}
283
f1b2fa5b
BJ
284/*
285 * Collision on ethernet interface. Do exponential
286 * backoff, and retransmit. If have backed off all
ee787340 287 * the way print warning diagnostic, and drop packet.
f1b2fa5b 288 */
11720282
BJ
289encollide(unit)
290 int unit;
291{
519a1ecf 292 struct en_softc *es = &en_softc[unit];
11720282 293
f1b2fa5b 294 es->es_if.if_collisions++;
8a13b737 295 if (es->es_oactive == 0)
11720282 296 return;
519a1ecf
BJ
297 endocoll(unit);
298}
299
300endocoll(unit)
301 int unit;
302{
303 register struct en_softc *es = &en_softc[unit];
304
b454c3ea
BJ
305 /*
306 * Es_mask is a 16 bit number with n low zero bits, with
307 * n the number of backoffs. When es_mask is 0 we have
308 * backed off 16 times, and give up.
309 */
8a13b737 310 if (es->es_mask == 0) {
a453e1b5 311 printf("en%d: send error\n", unit);
8a13b737 312 enxint(unit);
b454c3ea 313 return;
11720282 314 }
b454c3ea
BJ
315 /*
316 * Another backoff. Restart with delay based on n low bits
317 * of the interval timer.
318 */
319 es->es_mask <<= 1;
320 es->es_delay = mfpr(ICR) &~ es->es_mask;
321 enstart(unit);
11720282
BJ
322}
323
31c2345c
SL
324struct sockaddr_pup pupsrc = { AF_PUP };
325struct sockaddr_pup pupdst = { AF_PUP };
326struct sockproto pupproto = { PF_PUP };
f1b2fa5b
BJ
327/*
328 * Ethernet interface receiver interrupt.
329 * If input error just drop packet.
330 * Otherwise purge input buffered data path and examine
331 * packet to determine type. If can't determine length
332 * from type, then have to drop packet. Othewise decapsulate
333 * packet based on type and pass to type specific higher-level
334 * input routine.
335 */
11720282
BJ
336enrint(unit)
337 int unit;
338{
b454c3ea
BJ
339 register struct en_softc *es = &en_softc[unit];
340 struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
341 register struct en_header *en;
8a13b737 342 struct mbuf *m;
038ee561 343 int len; short resid;
b454c3ea 344 register struct ifqueue *inq;
8a13b737 345 int off;
11720282 346
b454c3ea 347 es->es_if.if_ipackets++;
f1b2fa5b
BJ
348
349 /*
b454c3ea 350 * Purge BDP; drop if input error indicated.
f1b2fa5b 351 */
ee787340
SL
352 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
353 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
941ee7ef 354 if (addr->en_istat&EN_IERROR) {
f1b2fa5b 355 es->es_if.if_ierrors++;
8a13b737 356 goto setup;
11720282 357 }
f1b2fa5b
BJ
358
359 /*
0658a645 360 * Calculate input data length.
f1b2fa5b
BJ
361 * Get pointer to ethernet header (in input buffer).
362 * Deal with trailer protocol: if type is PUP trailer
363 * get true type from first 16-bit word past data.
364 * Remember that type was trailer by setting off.
365 */
0658a645
BJ
366 resid = addr->en_iwc;
367 if (resid)
368 resid |= 0176000;
dc39362e 369 len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1;
0658a645 370 len -= sizeof (struct en_header);
dc39362e 371 if (len > ENMRU)
0658a645 372 goto setup; /* sanity */
f1b2fa5b 373 en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
8a13b737
BJ
374#define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off))))
375 if (en->en_type >= ENPUP_TRAIL &&
376 en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
377 off = (en->en_type - ENPUP_TRAIL) * 512;
dc39362e 378 if (off > ENMTU)
b454c3ea 379 goto setup; /* sanity */
8a13b737 380 en->en_type = *endataaddr(en, off, u_short *);
0658a645
BJ
381 resid = *(endataaddr(en, off+2, u_short *));
382 if (off + resid > len)
383 goto setup; /* sanity */
384 len = off + resid;
8a13b737
BJ
385 } else
386 off = 0;
8a13b737
BJ
387 if (len == 0)
388 goto setup;
f1b2fa5b
BJ
389 /*
390 * Pull packet off interface. Off is nonzero if packet
391 * has trailing header; if_rubaget will then force this header
392 * information to be at the front, but we still have to drop
0658a645 393 * the type and length which are at the front of any trailer data.
f1b2fa5b
BJ
394 */
395 m = if_rubaget(&es->es_ifuba, len, off);
a453e1b5
BJ
396 if (m == 0)
397 goto setup;
f1b2fa5b 398 if (off) {
0658a645
BJ
399 m->m_off += 2 * sizeof (u_short);
400 m->m_len -= 2 * sizeof (u_short);
f1b2fa5b 401 }
31c2345c
SL
402 switch (en->en_type) {
403
404#ifdef INET
405 case ENPUP_IPTYPE:
9c8692e9 406 schednetisr(NETISR_IP);
31c2345c
SL
407 inq = &ipintrq;
408 break;
409#endif
ee787340 410#ifdef PUP
31c2345c
SL
411 case ENPUP_PUPTYPE: {
412 struct pup_header *pup = mtod(m, struct pup_header *);
413
414 pupproto.sp_protocol = pup->pup_type;
415 pupdst.spup_addr = pup->pup_dport;
416 pupsrc.spup_addr = pup->pup_sport;
57aa3090
SL
417 raw_input(m, &pupproto, (struct sockaddr *)&pupsrc,
418 (struct sockaddr *)&pupdst);
31c2345c 419 goto setup;
31c2345c 420 }
ee787340 421#endif
c8f8184f
BJ
422 default:
423 m_freem(m);
424 goto setup;
ee787340
SL
425 }
426
1e977657
BJ
427 if (IF_QFULL(inq)) {
428 IF_DROP(inq);
ee787340 429 m_freem(m);
1e977657
BJ
430 } else
431 IF_ENQUEUE(inq, m);
f1b2fa5b 432
ae348eea 433setup:
f1b2fa5b
BJ
434 /*
435 * Reset for next packet.
436 */
437 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
dc39362e 438 addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
941ee7ef 439 addr->en_istat = EN_IEN|EN_GO;
ae348eea 440}
11720282 441
8a13b737
BJ
442/*
443 * Ethernet output routine.
444 * Encapsulate a packet of type family for the local net.
f1b2fa5b
BJ
445 * Use trailer local net encapsulation if enough data in first
446 * packet leaves a multiple of 512 bytes of data in remainder.
8a13b737 447 */
ee787340 448enoutput(ifp, m0, dst)
8a13b737
BJ
449 struct ifnet *ifp;
450 struct mbuf *m0;
ee787340 451 struct sockaddr *dst;
11720282 452{
8a2f82db 453 int type, dest, s, error;
f1b2fa5b 454 register struct mbuf *m = m0;
8a13b737 455 register struct en_header *en;
ee787340 456 register int off;
8a13b737 457
ee787340 458 switch (dst->sa_family) {
11720282 459
8a13b737 460#ifdef INET
ee787340 461 case AF_INET:
4e818526 462 dest = ((struct sockaddr_in *)dst)->sin_addr.s_addr;
4fce3bf9 463 if (in_lnaof(*((struct in_addr *)&dest)) >= 0x100) {
8a2f82db 464 error = EPERM; /* ??? */
3734056d 465 goto bad;
8a2f82db 466 }
4e818526 467 dest = (dest >> 24) & 0xff;
ee787340
SL
468 off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
469 if (off > 0 && (off & 0x1ff) == 0 &&
0658a645 470 m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
8a13b737 471 type = ENPUP_TRAIL + (off>>9);
0658a645
BJ
472 m->m_off -= 2 * sizeof (u_short);
473 m->m_len += 2 * sizeof (u_short);
f1b2fa5b 474 *mtod(m, u_short *) = ENPUP_IPTYPE;
0658a645 475 *(mtod(m, u_short *) + 1) = m->m_len;
f1b2fa5b 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;
31c2345c
SL
485 type = ENPUP_PUPTYPE;
486 off = 0;
487 goto gottype;
31c2345c 488#endif
8a13b737
BJ
489
490 default:
ee787340
SL
491 printf("en%d: can't handle af%d\n", ifp->if_unit,
492 dst->sa_family);
8a2f82db
SL
493 error = EAFNOSUPPORT;
494 goto bad;
8a13b737 495 }
f1b2fa5b 496
b454c3ea 497gottrailertype:
f1b2fa5b
BJ
498 /*
499 * Packet to be sent as trailer: move first packet
500 * (control information) to end of chain.
501 */
f1b2fa5b
BJ
502 while (m->m_next)
503 m = m->m_next;
504 m->m_next = m0;
505 m = m0->m_next;
506 m0->m_next = 0;
b454c3ea 507 m0 = m;
f1b2fa5b 508
b454c3ea 509gottype:
f1b2fa5b
BJ
510 /*
511 * Add local net header. If no space in first mbuf,
512 * allocate another.
513 */
a453e1b5
BJ
514 if (m->m_off > MMAXOFF ||
515 MMINOFF + sizeof (struct en_header) > m->m_off) {
cce93e4b 516 m = m_get(M_DONTWAIT, MT_HEADER);
8a13b737 517 if (m == 0) {
8a2f82db
SL
518 error = ENOBUFS;
519 goto bad;
8a13b737
BJ
520 }
521 m->m_next = m0;
522 m->m_off = MMINOFF;
523 m->m_len = sizeof (struct en_header);
524 } else {
8a13b737
BJ
525 m->m_off -= sizeof (struct en_header);
526 m->m_len += sizeof (struct en_header);
11720282 527 }
8a13b737
BJ
528 en = mtod(m, struct en_header *);
529 en->en_shost = ifp->if_host[0];
530 en->en_dhost = dest;
531 en->en_type = type;
f1b2fa5b
BJ
532
533 /*
534 * Queue message on interface, and start output if interface
535 * not yet active.
536 */
8a13b737 537 s = splimp();
1e977657
BJ
538 if (IF_QFULL(&ifp->if_snd)) {
539 IF_DROP(&ifp->if_snd);
8a2f82db
SL
540 error = ENOBUFS;
541 goto qfull;
1e977657 542 }
8a13b737 543 IF_ENQUEUE(&ifp->if_snd, m);
8a13b737
BJ
544 if (en_softc[ifp->if_unit].es_oactive == 0)
545 enstart(ifp->if_unit);
89413846 546 splx(s);
4e818526 547 return (0);
8a2f82db
SL
548qfull:
549 m0 = m;
550 splx(s);
551bad:
552 m_freem(m0);
553 return (error);
11720282 554}