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