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