cleanup raw pup interface
[unix-history] / usr / src / sys / vax / if / if_en.c
CommitLineData
31c2345c 1/* if_en.c 4.36 82/03/03 */
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];
8a13b737 97COUNT(ENATTACH);
f1b2fa5b
BJ
98
99 es->es_if.if_unit = ui->ui_unit;
b454c3ea 100 es->es_if.if_name = "en";
f1b2fa5b
BJ
101 es->es_if.if_mtu = ENMTU;
102 es->es_if.if_net = ui->ui_flags;
103 es->es_if.if_host[0] =
a453e1b5 104 (~(((struct endevice *)eninfo[ui->ui_unit]->ui_addr)->en_addr)) & 0xff;
f7bbb8eb
BJ
105#ifdef ENKLUDGE
106 if (es->es_if.if_net == 10) {
107 es->es_if.if_host[0] <<= 16;
108 es->es_if.if_host[0] |= 0x4e;
109 }
110#endif
f1b2fa5b
BJ
111 es->es_if.if_addr =
112 if_makeaddr(es->es_if.if_net, es->es_if.if_host[0]);
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);
8a13b737 152 return;
11720282 153 }
11720282 154 addr = (struct endevice *)ui->ui_addr;
8a13b737 155 addr->en_istat = addr->en_ostat = 0;
11720282 156
f1b2fa5b 157 /*
b454c3ea
BJ
158 * Hang a receive and start any
159 * pending writes by faking a transmit complete.
f1b2fa5b
BJ
160 */
161 s = splimp();
b454c3ea
BJ
162 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
163 addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
164 addr->en_istat = EN_IEN|EN_GO;
165 es->es_oactive = 1;
f1b2fa5b
BJ
166 enxint(unit);
167 splx(s);
11720282
BJ
168}
169
3552a746 170int enlastdel = 25;
8a13b737 171
f1b2fa5b
BJ
172/*
173 * Start or restart output on interface.
174 * If interface is already active, then this is a retransmit
175 * after a collision, and just restuff registers and delay.
176 * If interface is not already active, get another datagram
177 * to send off of the interface queue, and map it to the interface
178 * before starting the output.
179 */
ae348eea 180enstart(dev)
11720282
BJ
181 dev_t dev;
182{
b454c3ea
BJ
183 int unit = ENUNIT(dev);
184 struct uba_device *ui = eninfo[unit];
185 register struct en_softc *es = &en_softc[unit];
8a13b737 186 register struct endevice *addr;
8a13b737
BJ
187 struct mbuf *m;
188 int dest;
ae348eea 189COUNT(ENSTART);
11720282 190
8a13b737
BJ
191 if (es->es_oactive)
192 goto restart;
f1b2fa5b
BJ
193
194 /*
195 * Not already active: dequeue another request
196 * and map it to the UNIBUS. If no more requests,
197 * just return.
198 */
199 IF_DEQUEUE(&es->es_if.if_snd, m);
8a13b737
BJ
200 if (m == 0) {
201 es->es_oactive = 0;
11720282
BJ
202 return;
203 }
a453e1b5 204 dest = mtod(m, struct en_header *)->en_dhost;
8a13b737 205 es->es_olen = if_wubaput(&es->es_ifuba, m);
f1b2fa5b
BJ
206
207 /*
208 * Ethernet cannot take back-to-back packets (no
209 * buffering in interface. To avoid overrunning
210 * receiver, enforce a small delay (about 1ms) in interface
211 * on successive packets sent to same host.
212 */
8a13b737
BJ
213 if (es->es_lastx && es->es_lastx == dest)
214 es->es_delay = enlastdel;
215 else
216 es->es_lastx = dest;
f1b2fa5b 217
8a13b737 218restart:
f1b2fa5b
BJ
219 /*
220 * Have request mapped to UNIBUS for transmission.
221 * Purge any stale data from this BDP, and start the otput.
222 */
223 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_w.ifrw_bdp);
11720282 224 addr = (struct endevice *)ui->ui_addr;
405c9168 225 addr->en_oba = (int)es->es_ifuba.ifu_w.ifrw_info;
8a13b737
BJ
226 addr->en_odelay = es->es_delay;
227 addr->en_owc = -((es->es_olen + 1) >> 1);
941ee7ef 228 addr->en_ostat = EN_IEN|EN_GO;
8a13b737 229 es->es_oactive = 1;
11720282
BJ
230}
231
f1b2fa5b
BJ
232/*
233 * Ethernet interface transmitter interrupt.
234 * Start another output if more data to send.
235 */
11720282
BJ
236enxint(unit)
237 int unit;
238{
b454c3ea
BJ
239 register struct uba_device *ui = eninfo[unit];
240 register struct en_softc *es = &en_softc[unit];
11720282 241 register struct endevice *addr;
11720282
BJ
242COUNT(ENXINT);
243
8a13b737
BJ
244 if (es->es_oactive == 0)
245 return;
11720282 246 addr = (struct endevice *)ui->ui_addr;
b454c3ea 247 es->es_if.if_opackets++;
8a13b737
BJ
248 es->es_oactive = 0;
249 es->es_delay = 0;
250 es->es_mask = ~0;
89413846
BJ
251 if (addr->en_ostat&EN_OERROR) {
252 es->es_if.if_oerrors++;
a453e1b5 253 printf("en%d: output error\n", unit);
89413846 254 }
7a66118b
BJ
255 if (es->es_ifuba.ifu_xtofree) {
256 m_freem(es->es_ifuba.ifu_xtofree);
257 es->es_ifuba.ifu_xtofree = 0;
258 }
f1b2fa5b 259 if (es->es_if.if_snd.ifq_head == 0) {
8a13b737 260 es->es_lastx = 0;
11720282
BJ
261 return;
262 }
8a13b737 263 enstart(unit);
11720282
BJ
264}
265
f1b2fa5b
BJ
266/*
267 * Collision on ethernet interface. Do exponential
268 * backoff, and retransmit. If have backed off all
269 * the way printing warning diagnostic, and drop packet.
270 */
11720282
BJ
271encollide(unit)
272 int unit;
273{
b454c3ea 274 register struct en_softc *es = &en_softc[unit];
11720282
BJ
275COUNT(ENCOLLIDE);
276
f1b2fa5b 277 es->es_if.if_collisions++;
8a13b737 278 if (es->es_oactive == 0)
11720282 279 return;
b454c3ea
BJ
280 /*
281 * Es_mask is a 16 bit number with n low zero bits, with
282 * n the number of backoffs. When es_mask is 0 we have
283 * backed off 16 times, and give up.
284 */
8a13b737 285 if (es->es_mask == 0) {
a453e1b5 286 printf("en%d: send error\n", unit);
8a13b737 287 enxint(unit);
b454c3ea 288 return;
11720282 289 }
b454c3ea
BJ
290 /*
291 * Another backoff. Restart with delay based on n low bits
292 * of the interval timer.
293 */
294 es->es_mask <<= 1;
295 es->es_delay = mfpr(ICR) &~ es->es_mask;
296 enstart(unit);
11720282
BJ
297}
298
541c2984 299int enprintierrors;
31c2345c
SL
300struct sockaddr_pup pupsrc = { AF_PUP };
301struct sockaddr_pup pupdst = { AF_PUP };
302struct sockproto pupproto = { PF_PUP };
f1b2fa5b
BJ
303/*
304 * Ethernet interface receiver interrupt.
305 * If input error just drop packet.
306 * Otherwise purge input buffered data path and examine
307 * packet to determine type. If can't determine length
308 * from type, then have to drop packet. Othewise decapsulate
309 * packet based on type and pass to type specific higher-level
310 * input routine.
311 */
11720282
BJ
312enrint(unit)
313 int unit;
314{
b454c3ea
BJ
315 register struct en_softc *es = &en_softc[unit];
316 struct endevice *addr = (struct endevice *)eninfo[unit]->ui_addr;
317 register struct en_header *en;
8a13b737 318 struct mbuf *m;
b454c3ea
BJ
319 int len;
320 register struct ifqueue *inq;
8a13b737 321 int off;
11720282
BJ
322COUNT(ENRINT);
323
b454c3ea 324 es->es_if.if_ipackets++;
f1b2fa5b
BJ
325
326 /*
b454c3ea 327 * Purge BDP; drop if input error indicated.
f1b2fa5b
BJ
328 */
329 UBAPURGE(es->es_ifuba.ifu_uba, es->es_ifuba.ifu_r.ifrw_bdp);
941ee7ef 330 if (addr->en_istat&EN_IERROR) {
f1b2fa5b 331 es->es_if.if_ierrors++;
541c2984 332 if (enprintierrors)
a453e1b5 333 printf("en%d: input error\n", unit);
8a13b737 334 goto setup;
11720282 335 }
f1b2fa5b
BJ
336
337 /*
338 * Get pointer to ethernet header (in input buffer).
339 * Deal with trailer protocol: if type is PUP trailer
340 * get true type from first 16-bit word past data.
341 * Remember that type was trailer by setting off.
342 */
343 en = (struct en_header *)(es->es_ifuba.ifu_r.ifrw_addr);
8a13b737
BJ
344#define endataaddr(en, off, type) ((type)(((caddr_t)((en)+1)+(off))))
345 if (en->en_type >= ENPUP_TRAIL &&
346 en->en_type < ENPUP_TRAIL+ENPUP_NTRAILER) {
347 off = (en->en_type - ENPUP_TRAIL) * 512;
b454c3ea
BJ
348 if (off >= ENMTU)
349 goto setup; /* sanity */
8a13b737 350 en->en_type = *endataaddr(en, off, u_short *);
8a13b737
BJ
351 } else
352 off = 0;
f1b2fa5b
BJ
353
354 /*
355 * Attempt to infer packet length from type;
356 * can't deal with packet if can't infer length.
357 */
8a13b737
BJ
358 switch (en->en_type) {
359
360#ifdef INET
361 case ENPUP_IPTYPE:
6255745d
BJ
362 len = htons((u_short)endataaddr(en, off ? off+2 : 0, struct ip *)->ip_len);
363 if (off)
364 len += 2;
11720282
BJ
365 break;
366#endif
31c2345c
SL
367#ifdef PUP
368 case ENPUP_PUPTYPE:
369 len = endataaddr(en, off, struct pup_header *)->pup_length;
370 if (off)
371 len -= 2;
372 break;
373#endif
374
8a13b737 375 default:
5d855bdf 376 printf("en%d: unknown pkt type 0x%x\n", unit, en->en_type);
8a13b737 377 goto setup;
11720282 378 }
8a13b737
BJ
379 if (len == 0)
380 goto setup;
f1b2fa5b
BJ
381
382 /*
383 * Pull packet off interface. Off is nonzero if packet
384 * has trailing header; if_rubaget will then force this header
385 * information to be at the front, but we still have to drop
b454c3ea 386 * the two-byte type which is at the front of any trailer data.
f1b2fa5b
BJ
387 */
388 m = if_rubaget(&es->es_ifuba, len, off);
a453e1b5
BJ
389 if (m == 0)
390 goto setup;
f1b2fa5b
BJ
391 if (off) {
392 m->m_off += 2;
393 m->m_len -= 2;
394 }
31c2345c
SL
395 switch (en->en_type) {
396
397#ifdef INET
398 case ENPUP_IPTYPE:
399 setipintr();
400 inq = &ipintrq;
401 break;
402#endif
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;
409 raw_input(m, &pupproto, &pupdst, &pupsrc);
410 goto setup;
411 }
412 }
8a13b737 413 IF_ENQUEUE(inq, m);
f1b2fa5b 414
ae348eea 415setup:
f1b2fa5b
BJ
416 /*
417 * Reset for next packet.
418 */
419 addr->en_iba = es->es_ifuba.ifu_r.ifrw_info;
8a13b737 420 addr->en_iwc = -(sizeof (struct en_header) + ENMTU) >> 1;
941ee7ef 421 addr->en_istat = EN_IEN|EN_GO;
ae348eea 422}
11720282 423
8a13b737
BJ
424/*
425 * Ethernet output routine.
426 * Encapsulate a packet of type family for the local net.
f1b2fa5b
BJ
427 * Use trailer local net encapsulation if enough data in first
428 * packet leaves a multiple of 512 bytes of data in remainder.
8a13b737
BJ
429 */
430enoutput(ifp, m0, pf)
431 struct ifnet *ifp;
432 struct mbuf *m0;
433 int pf;
11720282 434{
8a13b737 435 int type, dest;
f1b2fa5b 436 register struct mbuf *m = m0;
8a13b737
BJ
437 register struct en_header *en;
438 int s;
439
9d1b03e0 440COUNT(ENOUTPUT);
8a13b737 441 switch (pf) {
11720282 442
8a13b737
BJ
443#ifdef INET
444 case PF_INET: {
445 register struct ip *ip = mtod(m0, struct ip *);
446 int off;
447
f7bbb8eb 448#ifndef ENKLUDGE
f1b2fa5b 449 dest = ip->ip_dst.s_addr >> 24;
f7bbb8eb
BJ
450#else
451 dest = (ip->ip_dst.s_addr >> 8) & 0xff;
452#endif
6255745d 453 off = ntohs((u_short)ip->ip_len) - m->m_len;
86532ef6 454#ifndef ENKLUDGE
b454c3ea 455 if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) {
8a13b737 456 type = ENPUP_TRAIL + (off>>9);
f1b2fa5b
BJ
457 m->m_off -= 2;
458 m->m_len += 2;
459 *mtod(m, u_short *) = ENPUP_IPTYPE;
460 goto gottrailertype;
8a13b737 461 }
86532ef6 462#endif
f1b2fa5b
BJ
463 type = ENPUP_IPTYPE;
464 off = 0;
465 goto gottype;
8a13b737 466 }
8a13b737 467#endif
31c2345c
SL
468#ifdef PUP
469 case PF_PUP: {
470 register struct pup_header *pup = mtod(m, struct pup_header *);
471
472 dest = pup->pup_dhost;
473 off = pup->pup_length - m->m_len;
474 if (off > 0 && (off & 0x1ff) == 0 && m->m_off >= MMINOFF + 2) {
475 type = ENPUP_TRAIL + (off>>9);
476 m->m_off -= 2;
477 m->m_len += 2;
478 *mtod(m, u_short *) = ENPUP_PUPTYPE;
479 goto gottrailertype;
480 }
481 type = ENPUP_PUPTYPE;
482 off = 0;
483 goto gottype;
484 }
485#endif
8a13b737
BJ
486
487 default:
488 printf("en%d: can't encapsulate pf%d\n", ifp->if_unit, pf);
489 m_freem(m0);
490 return (0);
491 }
f1b2fa5b 492
b454c3ea 493gottrailertype:
f1b2fa5b
BJ
494 /*
495 * Packet to be sent as trailer: move first packet
496 * (control information) to end of chain.
497 */
f1b2fa5b
BJ
498 while (m->m_next)
499 m = m->m_next;
500 m->m_next = m0;
501 m = m0->m_next;
502 m0->m_next = 0;
b454c3ea 503 m0 = m;
f1b2fa5b 504
b454c3ea 505gottype:
f1b2fa5b
BJ
506 /*
507 * Add local net header. If no space in first mbuf,
508 * allocate another.
509 */
a453e1b5
BJ
510 if (m->m_off > MMAXOFF ||
511 MMINOFF + sizeof (struct en_header) > m->m_off) {
ef9b4258 512 m = m_get(M_DONTWAIT);
8a13b737
BJ
513 if (m == 0) {
514 m_freem(m0);
515 return (0);
516 }
517 m->m_next = m0;
518 m->m_off = MMINOFF;
519 m->m_len = sizeof (struct en_header);
520 } else {
8a13b737
BJ
521 m->m_off -= sizeof (struct en_header);
522 m->m_len += sizeof (struct en_header);
11720282 523 }
8a13b737
BJ
524 en = mtod(m, struct en_header *);
525 en->en_shost = ifp->if_host[0];
526 en->en_dhost = dest;
527 en->en_type = type;
f1b2fa5b
BJ
528
529 /*
530 * Queue message on interface, and start output if interface
531 * not yet active.
532 */
8a13b737
BJ
533 s = splimp();
534 IF_ENQUEUE(&ifp->if_snd, m);
8a13b737
BJ
535 if (en_softc[ifp->if_unit].es_oactive == 0)
536 enstart(ifp->if_unit);
89413846 537 splx(s);
f1b2fa5b 538 return (1);
11720282 539}