tired of those old printfs
[unix-history] / usr / src / sys / vax / if / if_en.c
CommitLineData
00f2f0c1 1/* if_en.c 4.66 82/06/14 */
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"
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
8a13b737 77COUNT(ENPROBE);
11720282
BJ
78#ifdef lint
79 br = 0; cvec = br; br = cvec;
2b4b57cd 80 enrint(0); enxint(0); encollide(0);
11720282 81#endif
11720282 82 addr->en_istat = 0;
11720282
BJ
83 addr->en_owc = -1;
84 addr->en_oba = 0;
941ee7ef 85 addr->en_ostat = EN_IEN|EN_GO;
11720282
BJ
86 DELAY(100000);
87 addr->en_ostat = 0;
7d6f1cd5
SL
88#ifdef ECHACK
89 br = 0x16;
90#endif
11720282
BJ
91 return (1);
92}
93
f1b2fa5b
BJ
94/*
95 * Interface exists: make available by filling in network interface
96 * record. System will initialize the interface when it is ready
97 * to accept packets.
98 */
11720282
BJ
99enattach(ui)
100 struct uba_device *ui;
101{
f1b2fa5b 102 register struct en_softc *es = &en_softc[ui->ui_unit];
ee787340 103 register struct sockaddr_in *sin;
8a13b737 104COUNT(ENATTACH);
f1b2fa5b
BJ
105
106 es->es_if.if_unit = ui->ui_unit;
b454c3ea 107 es->es_if.if_name = "en";
f1b2fa5b 108 es->es_if.if_mtu = ENMTU;
6187f8f4 109 es->es_if.if_net = ui->ui_flags;
f1b2fa5b 110 es->es_if.if_host[0] =
ee787340
SL
111 (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff;
112 sin = (struct sockaddr_in *)&es->es_if.if_addr;
113 sin->sin_family = AF_INET;
114 sin->sin_addr = if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
115 sin = (struct sockaddr_in *)&es->es_if.if_broadaddr;
116 sin->sin_family = AF_INET;
117 sin->sin_addr = if_makeaddr(es->es_if.if_net, 0);
118 es->es_if.if_flags = IFF_BROADCAST;
f1b2fa5b 119 es->es_if.if_init = eninit;
b454c3ea 120 es->es_if.if_output = enoutput;
f1b2fa5b 121 es->es_if.if_ubareset = enreset;
d9c0644f 122 es->es_ifuba.ifu_flags = UBA_NEEDBDP | UBA_NEED16 | UBA_CANTWAIT;
405c9168 123 if_attach(&es->es_if);
11720282
BJ
124}
125
f1b2fa5b
BJ
126/*
127 * Reset of interface after UNIBUS reset.
128 * If interface is on specified uba, reset its state.
129 */
130enreset(unit, uban)
131 int unit, uban;
8a13b737 132{
11720282 133 register struct uba_device *ui;
f1b2fa5b 134COUNT(ENRESET);
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;
ae348eea 200COUNT(ENSTART);
11720282 201
8a13b737
BJ
202 if (es->es_oactive)
203 goto restart;
f1b2fa5b
BJ
204
205 /*
206 * Not already active: dequeue another request
207 * and map it to the UNIBUS. If no more requests,
208 * just return.
209 */
210 IF_DEQUEUE(&es->es_if.if_snd, m);
8a13b737
BJ
211 if (m == 0) {
212 es->es_oactive = 0;
11720282
BJ
213 return;
214 }
a453e1b5 215 dest = mtod(m, struct en_header *)->en_dhost;
8a13b737 216 es->es_olen = if_wubaput(&es->es_ifuba, m);
f1b2fa5b
BJ
217
218 /*
219 * Ethernet cannot take back-to-back packets (no
0658a645
BJ
220 * buffering in interface. To help avoid overrunning
221 * receivers, enforce a small delay (about 1ms) in interface:
222 * * between all packets when enalldelay
223 * * whenever last packet was broadcast
224 * * whenever this packet is to same host as last packet
f1b2fa5b 225 */
0658a645 226 if (enalldelay || es->es_lastx == 0 || es->es_lastx == dest) {
8a13b737 227 es->es_delay = enlastdel;
0658a645
BJ
228 es->es_mask = enlastmask;
229 }
230 es->es_lastx = dest;
f1b2fa5b 231
8a13b737 232restart:
f1b2fa5b
BJ
233 /*
234 * Have request mapped to UNIBUS for transmission.
235 * Purge any stale data from this BDP, and start the otput.
236 */
ee787340
SL
237 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
238 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
11720282 239 addr = (struct endevice *)ui->ui_addr;
405c9168 240 addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
8a13b737
BJ
241 addr->en_odelay = es->es_delay;
242 addr->en_owc = -((es->es_olen + 1) >> 1);
941ee7ef 243 addr->en_ostat = EN_IEN|EN_GO;
8a13b737 244 es->es_oactive = 1;
11720282
BJ
245}
246
f1b2fa5b
BJ
247/*
248 * Ethernet interface transmitter interrupt.
249 * Start another output if more data to send.
250 */
11720282
BJ
251enxint(unit)
252 int unit;
253{
b454c3ea
BJ
254 register struct uba_device *ui = eninfo[unit];
255 register struct en_softc *es = &en_softc[unit];
519a1ecf 256 register struct endevice *addr = (struct endevice *)ui->ui_addr;
11720282
BJ
257COUNT(ENXINT);
258
8a13b737
BJ
259 if (es->es_oactive == 0)
260 return;
519a1ecf 261 if (es->es_mask && (addr->en_ostat&EN_OERROR)) {
89413846 262 es->es_if.if_oerrors++;
519a1ecf
BJ
263 endocoll(unit);
264 return;
89413846 265 }
519a1ecf
BJ
266 es->es_if.if_opackets++;
267 es->es_oactive = 0;
268 es->es_delay = 0;
269 es->es_mask = ~0;
7a66118b
BJ
270 if (es->es_ifuba.ifu_xtofree) {
271 m_freem(es->es_ifuba.ifu_xtofree);
272 es->es_ifuba.ifu_xtofree = 0;
273 }
f1b2fa5b 274 if (es->es_if.if_snd.ifq_head == 0) {
0658a645 275 es->es_lastx = 256; /* putatively illegal */
11720282
BJ
276 return;
277 }
8a13b737 278 enstart(unit);
11720282
BJ
279}
280
f1b2fa5b
BJ
281/*
282 * Collision on ethernet interface. Do exponential
283 * backoff, and retransmit. If have backed off all
ee787340 284 * the way print warning diagnostic, and drop packet.
f1b2fa5b 285 */
11720282
BJ
286encollide(unit)
287 int unit;
288{
519a1ecf 289 struct en_softc *es = &en_softc[unit];
11720282
BJ
290COUNT(ENCOLLIDE);
291
f1b2fa5b 292 es->es_if.if_collisions++;
8a13b737 293 if (es->es_oactive == 0)
11720282 294 return;
519a1ecf
BJ
295 endocoll(unit);
296}
297
298endocoll(unit)
299 int unit;
300{
301 register struct en_softc *es = &en_softc[unit];
302
b454c3ea
BJ
303 /*
304 * Es_mask is a 16 bit number with n low zero bits, with
305 * n the number of backoffs. When es_mask is 0 we have
306 * backed off 16 times, and give up.
307 */
8a13b737 308 if (es->es_mask == 0) {
a453e1b5 309 printf("en%d: send error\n", unit);
8a13b737 310 enxint(unit);
b454c3ea 311 return;
11720282 312 }
b454c3ea
BJ
313 /*
314 * Another backoff. Restart with delay based on n low bits
315 * of the interval timer.
316 */
317 es->es_mask <<= 1;
318 es->es_delay = mfpr(ICR) &~ es->es_mask;
319 enstart(unit);
11720282
BJ
320}
321
31c2345c
SL
322struct sockaddr_pup pupsrc = { AF_PUP };
323struct sockaddr_pup pupdst = { AF_PUP };
324struct sockproto pupproto = { PF_PUP };
f1b2fa5b
BJ
325/*
326 * Ethernet interface receiver interrupt.
327 * If input error just drop packet.
328 * Otherwise purge input buffered data path and examine
329 * packet to determine type. If can't determine length
330 * from type, then have to drop packet. Othewise decapsulate
331 * packet based on type and pass to type specific higher-level
332 * input routine.
333 */
11720282
BJ
334enrint(unit)
335 int unit;
336{
b454c3ea
BJ
337 register struct en_softc *es = &en_softc[unit];
338 struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
339 register struct en_header *en;
8a13b737 340 struct mbuf *m;
0658a645 341 int len, plen; short resid;
b454c3ea 342 register struct ifqueue *inq;
8a13b737 343 int off;
11720282
BJ
344COUNT(ENRINT);
345
b454c3ea 346 es->es_if.if_ipackets++;
f1b2fa5b
BJ
347
348 /*
b454c3ea 349 * Purge BDP; drop if input error indicated.
f1b2fa5b 350 */
ee787340
SL
351 if (es->es_ifuba.ifu_flags & UBA_NEEDBDP)
352 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
941ee7ef 353 if (addr->en_istat&EN_IERROR) {
f1b2fa5b 354 es->es_if.if_ierrors++;
8a13b737 355 goto setup;
11720282 356 }
f1b2fa5b
BJ
357
358 /*
0658a645 359 * Calculate input data length.
f1b2fa5b
BJ
360 * Get pointer to ethernet header (in input buffer).
361 * Deal with trailer protocol: if type is PUP trailer
362 * get true type from first 16-bit word past data.
363 * Remember that type was trailer by setting off.
364 */
0658a645
BJ
365 resid = addr->en_iwc;
366 if (resid)
367 resid |= 0176000;
dc39362e 368 len = (((sizeof (struct en_header) + ENMRU) >> 1) + resid) << 1;
0658a645 369 len -= sizeof (struct en_header);
dc39362e 370 if (len > ENMRU)
0658a645 371 goto setup; /* sanity */
f1b2fa5b 372 en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
8a13b737
BJ
373#define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off))))
374 if (en->en_type >= ENPUP_TRAIL &&
375 en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
376 off = (en->en_type - ENPUP_TRAIL) * 512;
dc39362e 377 if (off > ENMTU)
b454c3ea 378 goto setup; /* sanity */
8a13b737 379 en->en_type = *endataaddr(en, off, u_short *);
0658a645
BJ
380 resid = *(endataaddr(en, off+2, u_short *));
381 if (off + resid > len)
382 goto setup; /* sanity */
383 len = off + resid;
8a13b737
BJ
384 } else
385 off = 0;
8a13b737
BJ
386 if (len == 0)
387 goto setup;
f1b2fa5b
BJ
388 /*
389 * Pull packet off interface. Off is nonzero if packet
390 * has trailing header; if_rubaget will then force this header
391 * information to be at the front, but we still have to drop
0658a645 392 * the type and length which are at the front of any trailer data.
f1b2fa5b
BJ
393 */
394 m = if_rubaget(&es->es_ifuba, len, off);
a453e1b5
BJ
395 if (m == 0)
396 goto setup;
f1b2fa5b 397 if (off) {
0658a645
BJ
398 m->m_off += 2 * sizeof (u_short);
399 m->m_len -= 2 * sizeof (u_short);
f1b2fa5b 400 }
31c2345c
SL
401 switch (en->en_type) {
402
403#ifdef INET
404 case ENPUP_IPTYPE:
9c8692e9 405 schednetisr(NETISR_IP);
31c2345c
SL
406 inq = &ipintrq;
407 break;
408#endif
ee787340 409#ifdef PUP
31c2345c
SL
410 case ENPUP_PUPTYPE: {
411 struct pup_header *pup = mtod(m, struct pup_header *);
412
413 pupproto.sp_protocol = pup->pup_type;
414 pupdst.spup_addr = pup->pup_dport;
415 pupsrc.spup_addr = pup->pup_sport;
57aa3090
SL
416 raw_input(m, &pupproto, (struct sockaddr *)&pupsrc,
417 (struct sockaddr *)&pupdst);
31c2345c 418 goto setup;
31c2345c 419 }
ee787340 420#endif
c8f8184f
BJ
421 default:
422 m_freem(m);
423 goto setup;
ee787340
SL
424 }
425
1e977657
BJ
426 if (IF_QFULL(inq)) {
427 IF_DROP(inq);
ee787340 428 m_freem(m);
1e977657
BJ
429 } else
430 IF_ENQUEUE(inq, m);
f1b2fa5b 431
ae348eea 432setup:
f1b2fa5b
BJ
433 /*
434 * Reset for next packet.
435 */
436 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
dc39362e 437 addr->en_iwc = -(sizeof (struct en_header) + ENMRU) >> 1;
941ee7ef 438 addr->en_istat = EN_IEN|EN_GO;
ae348eea 439}
11720282 440
8a13b737
BJ
441/*
442 * Ethernet output routine.
443 * Encapsulate a packet of type family for the local net.
f1b2fa5b
BJ
444 * Use trailer local net encapsulation if enough data in first
445 * packet leaves a multiple of 512 bytes of data in remainder.
8a13b737 446 */
ee787340 447enoutput(ifp, m0, dst)
8a13b737
BJ
448 struct ifnet *ifp;
449 struct mbuf *m0;
ee787340 450 struct sockaddr *dst;
11720282 451{
8a2f82db 452 int type, dest, s, error;
f1b2fa5b 453 register struct mbuf *m = m0;
8a13b737 454 register struct en_header *en;
ee787340 455 register int off;
8a13b737 456
9d1b03e0 457COUNT(ENOUTPUT);
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;
8a2f82db
SL
463 if (dest & 0x00ffff00) {
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) {
ef9b4258 516 m = m_get(M_DONTWAIT);
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}