1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ralph Campbell and Rick Macklem.
7 *
8 * %sccs.include.redist.c%
9 *
10 * @(#)if_le.c 8.4 (Berkeley) 10/09/94
11 */
12
13 #include <le.h>
14 #if NLE > 0
15
16 #include <bpfilter.h>
17
18 /*
19 * AMD 7990 LANCE
20 *
21 * This driver will generate and accept trailer encapsulated packets even
22 * though it buys us nothing. The motivation was to avoid incompatibilities
23 * with VAXen, SUNs, and others that handle and benefit from them.
24 * This reasoning is dubious.
25 */
26 #include <sys/param.h>
27 #include <sys/proc.h>
28 #include <sys/systm.h>
29 #include <sys/mbuf.h>
30 #include <sys/buf.h>
31 #include <sys/protosw.h>
32 #include <sys/socket.h>
33 #include <sys/syslog.h>
34 #include <sys/ioctl.h>
35 #include <sys/errno.h>
36
37 #include <net/if.h>
38 #include <net/netisr.h>
39 #include <net/route.h>
40
41 #ifdef INET
42 #include <netinet/in.h>
43 #include <netinet/in_systm.h>
44 #include <netinet/in_var.h>
45 #include <netinet/ip.h>
46 #include <netinet/if_ether.h>
47 #endif
48
49 #ifdef NS
50 #include <netns/ns.h>
51 #include <netns/ns_if.h>
52 #endif
53
54 #if defined (CCITT) && defined (LLC)
55 #include <sys/socketvar.h>
56 #include <netccitt/x25.h>
57 extern llc_ctlinput(), cons_rtrequest();
58 #endif
59
60 #include <machine/machConst.h>
61
62 #include <pmax/pmax/pmaxtype.h>
63 #include <pmax/pmax/kn01.h>
64 #include <pmax/pmax/kmin.h>
65 #include <pmax/pmax/asic.h>
66
67 #include <pmax/dev/device.h>
68 #include <pmax/dev/if_lereg.h>
69
70 #if NBPFILTER > 0
71 #include <net/bpf.h>
72 #include <net/bpfdesc.h>
73 #endif
74
75 int leprobe();
76 void leintr();
77 struct driver ledriver = {
78 "le", leprobe, 0, 0, leintr,
79 };
80
81 int ledebug = 0; /* console error messages */
82
83 /*
84 * Ethernet software status per interface.
85 *
86 * Each interface is referenced by a network interface structure,
87 * le_if, which the routing code uses to locate the interface.
88 * This structure contains the output queue for the interface, its address, ...
89 */
90 struct le_softc {
91 struct arpcom sc_ac; /* common Ethernet structures */
92 #define sc_if sc_ac.ac_if /* network-visible interface */
93 #define sc_addr sc_ac.ac_enaddr /* hardware Ethernet address */
94 volatile struct lereg1 *sc_r1; /* LANCE registers */
95 volatile void *sc_r2; /* dual-port RAM */
96 int sc_ler2pad; /* Do ring descriptors require short pads? */
97 void (*sc_copytobuf)(); /* Copy to buffer */
98 void (*sc_copyfrombuf)(); /* Copy from buffer */
99 void (*sc_zerobuf)(); /* and Zero bytes in buffer */
100 int sc_rmd; /* predicted next rmd to process */
101 int sc_tmd; /* last tmd processed */
102 int sc_tmdnext; /* next tmd to transmit with */
103 /* stats */
104 int sc_runt;
105 int sc_merr;
106 int sc_babl;
107 int sc_cerr;
108 int sc_miss;
109 int sc_rown;
110 int sc_xint;
111 int sc_uflo;
112 int sc_rxlen;
113 int sc_rxoff;
114 int sc_txoff;
115 int sc_busy;
116 short sc_iflags;
117 } le_softc[NLE];
118
119 /* access LANCE registers */
120 static void lewritereg();
121 #define LERDWR(cntl, src, dst) { (dst) = (src); DELAY(10); }
122 #define LEWREG(src, dst) lewritereg(&(dst), (src))
123
124 #define CPU_TO_CHIP_ADDR(cpu) \
125 ((unsigned)(&(((struct lereg2 *)0)->cpu)))
126
127 #define LE_OFFSET_RAM 0x0
128 #define LE_OFFSET_LANCE 0x100000
129 #define LE_OFFSET_ROM 0x1c0000
130
131 void copytobuf_contig(), copyfrombuf_contig(), bzerobuf_contig();
132 void copytobuf_gap2(), copyfrombuf_gap2(), bzerobuf_gap2();
133 void copytobuf_gap16(), copyfrombuf_gap16(), bzerobuf_gap16();
134
135 extern int pmax_boardtype;
136 extern u_long le_iomem;
137 extern u_long asic_base;
138
139 /*
140 * Test to see if device is present.
141 * Return true if found and initialized ok.
142 * If interface exists, make available by filling in network interface
143 * record. System will initialize the interface when it is ready
144 * to accept packets.
145 */
146 leprobe(dp)
147 struct pmax_ctlr *dp;
148 {
149 volatile struct lereg1 *ler1;
150 struct le_softc *le = &le_softc[dp->pmax_unit];
151 struct ifnet *ifp = &le->sc_if;
152 u_char *cp;
153 int i;
154 extern int leinit(), lereset(), leioctl(), lestart(), ether_output();
155
156 switch (pmax_boardtype) {
157 case DS_PMAX:
158 le->sc_r1 = ler1 = (volatile struct lereg1 *)dp->pmax_addr;
159 le->sc_r2 = (volatile void *)MACH_PHYS_TO_UNCACHED(0x19000000);
160 cp = (u_char *)(MACH_PHYS_TO_UNCACHED(KN01_SYS_CLOCK) + 1);
161 le->sc_ler2pad = 1;
162 le->sc_copytobuf = copytobuf_gap2;
163 le->sc_copyfrombuf = copyfrombuf_gap2;
164 le->sc_zerobuf = bzerobuf_gap2;
165 break;
166 case DS_3MIN:
167 case DS_MAXINE:
168 case DS_3MAXPLUS:
169 if (dp->pmax_unit == 0) {
170 volatile u_int *ssr, *ldp;
171
172 le->sc_r1 = ler1 = (volatile struct lereg1 *)
173 ASIC_SYS_LANCE(asic_base);
174 cp = (u_char *)ASIC_SYS_ETHER_ADDRESS(asic_base);
175 le->sc_r2 = (volatile void *)
176 MACH_PHYS_TO_UNCACHED(le_iomem);
177 le->sc_ler2pad = 1;
178 le->sc_copytobuf = copytobuf_gap16;
179 le->sc_copyfrombuf = copyfrombuf_gap16;
180 le->sc_zerobuf = bzerobuf_gap16;
181
182 /*
183 * And enable Lance dma through the asic.
184 */
185 ssr = (volatile u_int *)ASIC_REG_CSR(asic_base);
186 ldp = (volatile u_int *)
187 ASIC_REG_LANCE_DMAPTR(asic_base);
188 *ldp = (le_iomem << 3); /* phys addr << 3 */
189 *ssr |= ASIC_CSR_DMAEN_LANCE;
190 break;
191 }
192 /*
193 * Units other than 0 are turbochannel option boards and fall
194 * through to DS_3MAX.
195 */
196 case DS_3MAX:
197 le->sc_r1 = ler1 = (volatile struct lereg1 *)
198 (dp->pmax_addr + LE_OFFSET_LANCE);
199 le->sc_r2 = (volatile void *)(dp->pmax_addr + LE_OFFSET_RAM);
200 cp = (u_char *)(dp->pmax_addr + LE_OFFSET_ROM + 2);
201 le->sc_ler2pad = 0;
202 le->sc_copytobuf = copytobuf_contig;
203 le->sc_copyfrombuf = copyfrombuf_contig;
204 le->sc_zerobuf = bzerobuf_contig;
205 break;
206 default:
207 printf("Unknown CPU board type %d\n", pmax_boardtype);
208 return (0);
209 };
210
211 /*
212 * Get the ethernet address out of rom
213 */
214 for (i = 0; i < sizeof(le->sc_addr); i++) {
215 le->sc_addr[i] = *cp;
216 cp += 4;
217 }
218
219 /* make sure the chip is stopped */
220 LEWREG(LE_CSR0, ler1->ler1_rap);
221 LEWREG(LE_STOP, ler1->ler1_rdp);
222
223 ifp->if_unit = dp->pmax_unit;
224 ifp->if_name = "le";
225 ifp->if_mtu = ETHERMTU;
226 ifp->if_init = leinit;
227 ifp->if_reset = lereset;
228 ifp->if_ioctl = leioctl;
229 ifp->if_output = ether_output;
230 ifp->if_start = lestart;
231 #ifdef MULTICAST
232 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
233 #else
234 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
235 #endif
236 #if NBPFILTER > 0
237 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
238 #endif
239 if_attach(ifp);
240
241 printf("le%d at nexus0 csr 0x%x priority %d ethernet address %s\n",
242 dp->pmax_unit, dp->pmax_addr, dp->pmax_pri,
243 ether_sprintf(le->sc_addr));
244 return (1);
245 }
246
247 #ifdef MULTICAST
248 /*
249 * Setup the logical address filter
250 */
251 void
lesetladrf(le)252 lesetladrf(le)
253 register struct le_softc *le;
254 {
255 register volatile struct lereg2 *ler2 = le->sc_r2;
256 register struct ifnet *ifp = &le->sc_if;
257 register struct ether_multi *enm;
258 register u_char *cp;
259 register u_long crc;
260 register u_long c;
261 register int i, len;
262 struct ether_multistep step;
263
264 /*
265 * Set up multicast address filter by passing all multicast
266 * addresses through a crc generator, and then using the high
267 * order 6 bits as an index into the 64 bit logical address
268 * filter. The high order two bits select the word, while the
269 * rest of the bits select the bit within the word.
270 */
271
272 LER2_ladrf0(ler2, 0);
273 LER2_ladrf1(ler2, 0);
274 ifp->if_flags &= ~IFF_ALLMULTI;
275 ETHER_FIRST_MULTI(step, &le->sc_ac, enm);
276 while (enm != NULL) {
277 if (bcmp((caddr_t)&enm->enm_addrlo,
278 (caddr_t)&enm->enm_addrhi, sizeof(enm->enm_addrlo)) == 0) {
279 /*
280 * We must listen to a range of multicast
281 * addresses. For now, just accept all
282 * multicasts, rather than trying to set only
283 * those filter bits needed to match the range.
284 * (At this time, the only use of address
285 * ranges is for IP multicast routing, for
286 * which the range is big enough to require all
287 * bits set.)
288 */
289 LER2_ladrf0(ler2, 0xff);
290 LER2_ladrf1(ler2, 0xff);
291 LER2_ladrf2(ler2, 0xff);
292 LER2_ladrf3(ler2, 0xff);
293 ifp->if_flags |= IFF_ALLMULTI;
294 return;
295 }
296
297 cp = (unsigned char *)&enm->enm_addrlo;
298 c = *cp;
299 crc = 0xffffffff;
300 len = 6;
301 while (len-- > 0) {
302 c = *cp;
303 for (i = 0; i < 8; i++) {
304 if ((c & 0x01) ^ (crc & 0x01)) {
305 crc >>= 1;
306 crc = crc ^ 0xedb88320;
307 }
308 else
309 crc >>= 1;
310 c >>= 1;
311 }
312 cp++;
313 }
314 /* Just want the 6 most significant bits. */
315 crc = crc >> 26;
316
317 /* Turn on the corresponding bit in the filter. */
318 switch (crc >> 5) {
319 case 0:
320 LER2_ladrf0(ler2, 1 << (crc & 0x1f));
321 break;
322 case 1:
323 LER2_ladrf1(ler2, 1 << (crc & 0x1f));
324 break;
325 case 2:
326 LER2_ladrf2(ler2, 1 << (crc & 0x1f));
327 break;
328 case 3:
329 LER2_ladrf3(ler2, 1 << (crc & 0x1f));
330 }
331
332 ETHER_NEXT_MULTI(step, enm);
333 }
334 }
335 #endif
336
337 ledrinit(le)
338 struct le_softc *le;
339 {
340 register volatile void *rp;
341 register int i;
342
343 for (i = 0; i < LERBUF; i++) {
344 rp = LER2_RMDADDR(le->sc_r2, i);
345 LER2_rmd0(rp, CPU_TO_CHIP_ADDR(ler2_rbuf[i][0]));
346 LER2_rmd1(rp, LE_OWN);
347 LER2_rmd2(rp, -LEMTU);
348 LER2_rmd3(rp, 0);
349 }
350 for (i = 0; i < LETBUF; i++) {
351 rp = LER2_TMDADDR(le->sc_r2, i);
352 LER2_tmd0(rp, CPU_TO_CHIP_ADDR(ler2_tbuf[i][0]));
353 LER2_tmd1(rp, 0);
354 LER2_tmd2(rp, 0);
355 LER2_tmd3(rp, 0);
356 }
357 }
358
lereset(unit)359 lereset(unit)
360 register int unit;
361 {
362 register struct le_softc *le = &le_softc[unit];
363 register volatile struct lereg1 *ler1 = le->sc_r1;
364 register volatile void *ler2 = le->sc_r2;
365 register int timo = 100000;
366 register int stat;
367
368 #ifdef lint
369 stat = unit;
370 #endif
371 LEWREG(LE_CSR0, ler1->ler1_rap);
372 LEWREG(LE_STOP, ler1->ler1_rdp);
373
374 /*
375 * Setup for transmit/receive
376 */
377 #if NBPFILTER > 0
378 if (le->sc_if.if_flags & IFF_PROMISC)
379 /* set the promiscuous bit */
380 LER2_mode(ler2, LE_MODE | 0x8000);
381 else
382 #endif
383 LER2_mode(ler2, LE_MODE);
384 LER2_padr0(ler2, (le->sc_addr[1] << 8) | le->sc_addr[0]);
385 LER2_padr1(ler2, (le->sc_addr[3] << 8) | le->sc_addr[2]);
386 LER2_padr2(ler2, (le->sc_addr[5] << 8) | le->sc_addr[4]);
387 /* Setup the logical address filter */
388 #ifdef MULTICAST
389 lesetladrf(le);
390 #else
391 LER2_ladrf0(ler2, 0);
392 LER2_ladrf1(ler2, 0);
393 LER2_ladrf2(ler2, 0);
394 LER2_ladrf3(ler2, 0);
395 #endif
396 LER2_rlen(ler2, LE_RLEN);
397 LER2_rdra(ler2, CPU_TO_CHIP_ADDR(ler2_rmd[0]));
398 LER2_tlen(ler2, LE_TLEN);
399 LER2_tdra(ler2, CPU_TO_CHIP_ADDR(ler2_tmd[0]));
400 ledrinit(le);
401 le->sc_rmd = 0;
402 le->sc_tmd = LETBUF - 1;
403 le->sc_tmdnext = 0;
404
405 LEWREG(LE_CSR1, ler1->ler1_rap);
406 LEWREG(CPU_TO_CHIP_ADDR(ler2_mode), ler1->ler1_rdp);
407 LEWREG(LE_CSR2, ler1->ler1_rap);
408 LEWREG(0, ler1->ler1_rdp);
409 LEWREG(LE_CSR3, ler1->ler1_rap);
410 LEWREG(0, ler1->ler1_rdp);
411 LEWREG(LE_CSR0, ler1->ler1_rap);
412 LERDWR(ler0, LE_INIT, ler1->ler1_rdp);
413 do {
414 if (--timo == 0) {
415 printf("le%d: init timeout, stat = 0x%x\n",
416 unit, stat);
417 break;
418 }
419 stat = ler1->ler1_rdp;
420 } while ((stat & LE_IDON) == 0);
421 LERDWR(ler0, LE_IDON, ler1->ler1_rdp);
422 LERDWR(ler0, LE_STRT | LE_INEA, ler1->ler1_rdp);
423 le->sc_if.if_flags &= ~IFF_OACTIVE;
424 }
425
426 /*
427 * Initialization of interface
428 */
leinit(unit)429 leinit(unit)
430 int unit;
431 {
432 register struct ifnet *ifp = &le_softc[unit].sc_if;
433 register struct ifaddr *ifa;
434 int s;
435
436 /* not yet, if address still unknown */
437 if (ifp->if_addrlist == NULL)
438 return;
439 if ((ifp->if_flags & IFF_RUNNING) == 0) {
440 s = splnet();
441 ifp->if_flags |= IFF_RUNNING;
442 lereset(unit);
443 (void) lestart(ifp);
444 splx(s);
445 }
446 }
447
448 #define LENEXTTMP \
449 if (++bix == LETBUF) \
450 bix = 0; \
451 tmd = LER2_TMDADDR(le->sc_r2, bix)
452
453 /*
454 * Start output on interface. Get another datagram to send
455 * off of the interface queue, and copy it to the interface
456 * before starting the output.
457 */
458 lestart(ifp)
459 struct ifnet *ifp;
460 {
461 register struct le_softc *le = &le_softc[ifp->if_unit];
462 register int bix = le->sc_tmdnext;
463 register volatile void *tmd = LER2_TMDADDR(le->sc_r2, bix);
464 register struct mbuf *m;
465 int len = 0;
466
467 if ((le->sc_if.if_flags & IFF_RUNNING) == 0)
468 return (0);
469 while (bix != le->sc_tmd) {
470 if (LER2V_tmd1(tmd) & LE_OWN)
471 panic("lestart");
472 IF_DEQUEUE(&le->sc_if.if_snd, m);
473 if (m == 0)
474 break;
475 #if NBPFILTER > 0
476 /*
477 * If bpf is listening on this interface, let it
478 * see the packet before we commit it to the wire.
479 */
480 if (ifp->if_bpf)
481 bpf_mtap(ifp->if_bpf, m);
482 #endif
483 len = leput(le, LER2_TBUFADDR(le->sc_r2, bix), m);
484 LER2_tmd3(tmd, 0);
485 LER2_tmd2(tmd, -len);
486 LER2_tmd1(tmd, LE_OWN | LE_STP | LE_ENP);
487 LENEXTTMP;
488 }
489 if (len != 0) {
490 le->sc_if.if_flags |= IFF_OACTIVE;
491 LERDWR(ler0, LE_TDMD | LE_INEA, le->sc_r1->ler1_rdp);
492 }
493 le->sc_tmdnext = bix;
494 return (0);
495 }
496
497 /*
498 * Process interrupts from the 7990 chip.
499 */
500 void
leintr(unit)501 leintr(unit)
502 int unit;
503 {
504 register struct le_softc *le;
505 register volatile struct lereg1 *ler1;
506 register int stat;
507
508 le = &le_softc[unit];
509 ler1 = le->sc_r1;
510 stat = ler1->ler1_rdp;
511 if (!(stat & LE_INTR)) {
512 printf("le%d: spurrious interrupt\n", unit);
513 return;
514 }
515 if (stat & LE_SERR) {
516 leerror(unit, stat);
517 if (stat & LE_MERR) {
518 le->sc_merr++;
519 lereset(unit);
520 return;
521 }
522 if (stat & LE_BABL)
523 le->sc_babl++;
524 if (stat & LE_CERR)
525 le->sc_cerr++;
526 if (stat & LE_MISS)
527 le->sc_miss++;
528 LERDWR(ler0, LE_BABL|LE_CERR|LE_MISS|LE_INEA, ler1->ler1_rdp);
529 }
530 if ((stat & LE_RXON) == 0) {
531 le->sc_rxoff++;
532 lereset(unit);
533 return;
534 }
535 if ((stat & LE_TXON) == 0) {
536 le->sc_txoff++;
537 lereset(unit);
538 return;
539 }
540 if (stat & LE_RINT) {
541 /* interrupt is cleared in lerint */
542 lerint(unit);
543 }
544 if (stat & LE_TINT) {
545 LERDWR(ler0, LE_TINT|LE_INEA, ler1->ler1_rdp);
546 lexint(unit);
547 }
548 }
549
550 /*
551 * Ethernet interface transmitter interrupt.
552 * Start another output if more data to send.
553 */
lexint(unit)554 lexint(unit)
555 register int unit;
556 {
557 register struct le_softc *le = &le_softc[unit];
558 register int bix = le->sc_tmd;
559 register volatile void *tmd;
560
561 if ((le->sc_if.if_flags & IFF_OACTIVE) == 0) {
562 le->sc_xint++;
563 return;
564 }
565 LENEXTTMP;
566 while (bix != le->sc_tmdnext && (LER2V_tmd1(tmd) & LE_OWN) == 0) {
567 le->sc_tmd = bix;
568 if ((LER2V_tmd1(tmd) & LE_ERR) || (LER2V_tmd3(tmd) & LE_TBUFF)) {
569 lexerror(unit);
570 le->sc_if.if_oerrors++;
571 if (LER2V_tmd3(tmd) & (LE_TBUFF|LE_UFLO)) {
572 le->sc_uflo++;
573 lereset(unit);
574 break;
575 }
576 else if (LER2V_tmd3(tmd) & LE_LCOL)
577 le->sc_if.if_collisions++;
578 else if (LER2V_tmd3(tmd) & LE_RTRY)
579 le->sc_if.if_collisions += 16;
580 }
581 else if (LER2V_tmd1(tmd) & LE_ONE)
582 le->sc_if.if_collisions++;
583 else if (LER2V_tmd1(tmd) & LE_MORE)
584 /* what is the real number? */
585 le->sc_if.if_collisions += 2;
586 else
587 le->sc_if.if_opackets++;
588 LENEXTTMP;
589 }
590 if (bix == le->sc_tmdnext)
591 le->sc_if.if_flags &= ~IFF_OACTIVE;
592 (void) lestart(&le->sc_if);
593 }
594
595 #define LENEXTRMP \
596 if (++bix == LERBUF) \
597 bix = 0; \
598 rmd = LER2_RMDADDR(le->sc_r2, bix)
599
600 /*
601 * Ethernet interface receiver interrupt.
602 * If input error just drop packet.
603 * Decapsulate packet based on type and pass to type specific
604 * higher-level input routine.
605 */
lerint(unit)606 lerint(unit)
607 int unit;
608 {
609 register struct le_softc *le = &le_softc[unit];
610 register int bix = le->sc_rmd;
611 register volatile void *rmd = LER2_RMDADDR(le->sc_r2, bix);
612
613 /*
614 * Out of sync with hardware, should never happen?
615 */
616 if (LER2V_rmd1(rmd) & LE_OWN) {
617 le->sc_rown++;
618 LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
619 return;
620 }
621
622 /*
623 * Process all buffers with valid data
624 */
625 while ((LER2V_rmd1(rmd) & LE_OWN) == 0) {
626 int len = LER2V_rmd3(rmd);
627
628 /* Clear interrupt to avoid race condition */
629 LERDWR(le->sc_r0, LE_RINT|LE_INEA, le->sc_r1->ler1_rdp);
630
631 if (LER2V_rmd1(rmd) & LE_ERR) {
632 le->sc_rmd = bix;
633 lererror(unit, "bad packet");
634 le->sc_if.if_ierrors++;
635 } else if ((LER2V_rmd1(rmd) & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP)) {
636 /*
637 * Find the end of the packet so we can see how long
638 * it was. We still throw it away.
639 */
640 do {
641 LERDWR(le->sc_r0, LE_RINT|LE_INEA,
642 le->sc_r1->ler1_rdp);
643 LER2_rmd3(rmd, 0);
644 LER2_rmd1(rmd, LE_OWN);
645 LENEXTRMP;
646 } while (!(LER2V_rmd1(rmd) & (LE_OWN|LE_ERR|LE_STP|LE_ENP)));
647 le->sc_rmd = bix;
648 lererror(unit, "chained buffer");
649 le->sc_rxlen++;
650 /*
651 * If search terminated without successful completion
652 * we reset the hardware (conservative).
653 */
654 if ((LER2V_rmd1(rmd) & (LE_OWN|LE_ERR|LE_STP|LE_ENP)) !=
655 LE_ENP) {
656 lereset(unit);
657 return;
658 }
659 } else
660 leread(unit, LER2_RBUFADDR(le->sc_r2, bix), len);
661 LER2_rmd3(rmd, 0);
662 LER2_rmd1(rmd, LE_OWN);
663 LENEXTRMP;
664 }
665 MachEmptyWriteBuffer(); /* Paranoia */
666 le->sc_rmd = bix;
667 }
668
669 /*
670 * Look at the packet in network buffer memory so we can be smart about how
671 * we copy the data into mbufs.
672 * This needs work since we can't just read network buffer memory like
673 * regular memory.
674 */
leread(unit,buf,len)675 leread(unit, buf, len)
676 int unit;
677 volatile void *buf;
678 int len;
679 {
680 register struct le_softc *le = &le_softc[unit];
681 struct ether_header et;
682 struct mbuf *m;
683 int off, resid, flags;
684 u_short sbuf[2], eth_type;
685 extern struct mbuf *leget();
686
687 le->sc_if.if_ipackets++;
688 (*le->sc_copyfrombuf)(buf, 0, (char *)&et, sizeof (et));
689 eth_type = ntohs(et.ether_type);
690 /* adjust input length to account for header and CRC */
691 len = len - sizeof(struct ether_header) - 4;
692
693 if (eth_type >= ETHERTYPE_TRAIL &&
694 eth_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
695 off = (eth_type - ETHERTYPE_TRAIL) * 512;
696 if (off >= ETHERMTU)
697 return; /* sanity */
698 (*le->sc_copyfrombuf)(buf, sizeof (et) + off, (char *)sbuf,
699 sizeof (sbuf));
700 eth_type = ntohs(sbuf[0]);
701 resid = ntohs(sbuf[1]);
702 if (off + resid > len)
703 return; /* sanity */
704 len = off + resid;
705 } else
706 off = 0;
707
708 if (len <= 0) {
709 if (ledebug)
710 log(LOG_WARNING,
711 "le%d: ierror(runt packet): from %s: len=%d\n",
712 unit, ether_sprintf(et.ether_shost), len);
713 le->sc_runt++;
714 le->sc_if.if_ierrors++;
715 return;
716 }
717 flags = 0;
718 if (bcmp((caddr_t)etherbroadcastaddr,
719 (caddr_t)et.ether_dhost, sizeof(etherbroadcastaddr)) == 0)
720 flags |= M_BCAST;
721 if (et.ether_dhost[0] & 1)
722 flags |= M_MCAST;
723
724 /*
725 * Pull packet off interface. Off is nonzero if packet
726 * has trailing header; leget will then force this header
727 * information to be at the front, but we still have to drop
728 * the type and length which are at the front of any trailer data.
729 */
730 m = leget(le, buf, len, off, &le->sc_if);
731 if (m == 0)
732 return;
733 #if NBPFILTER > 0
734 /*
735 * Check if there's a bpf filter listening on this interface.
736 * If so, hand off the raw packet to enet.
737 */
738 if (le->sc_if.if_bpf) {
739 bpf_mtap(le->sc_if.if_bpf, m);
740
741 /*
742 * Keep the packet if it's a broadcast or has our
743 * physical ethernet address (or if we support
744 * multicast and it's one).
745 */
746 if (
747 #ifdef MULTICAST
748 (flags & (M_BCAST | M_MCAST)) == 0 &&
749 #else
750 (flags & M_BCAST) == 0 &&
751 #endif
752 bcmp(et.ether_dhost, le->sc_addr,
753 sizeof(et.ether_dhost)) != 0) {
754 m_freem(m);
755 return;
756 }
757 }
758 #endif
759 m->m_flags |= flags;
760 et.ether_type = eth_type;
761 ether_input(&le->sc_if, &et, m);
762 }
763
764 /*
765 * Routine to copy from mbuf chain to transmit buffer in
766 * network buffer memory.
767 */
768 leput(le, lebuf, m)
769 struct le_softc *le;
770 register volatile void *lebuf;
771 register struct mbuf *m;
772 {
773 register struct mbuf *mp;
774 register int len, tlen = 0;
775 register int boff = 0;
776
777 for (mp = m; mp; mp = mp->m_next) {
778 len = mp->m_len;
779 if (len == 0)
780 continue;
781 (*le->sc_copytobuf)(mtod(mp, char *), lebuf, boff, len);
782 tlen += len;
783 boff += len;
784 }
785 m_freem(m);
786 if (tlen < LEMINSIZE) {
787 (*le->sc_zerobuf)(lebuf, boff, LEMINSIZE - tlen);
788 tlen = LEMINSIZE;
789 }
790 return(tlen);
791 }
792
793 /*
794 * Routine to copy from network buffer memory into mbufs.
795 */
796 struct mbuf *
leget(le,lebuf,totlen,off,ifp)797 leget(le, lebuf, totlen, off, ifp)
798 struct le_softc *le;
799 volatile void *lebuf;
800 int totlen, off;
801 struct ifnet *ifp;
802 {
803 register struct mbuf *m;
804 struct mbuf *top = 0, **mp = ⊤
805 register int len, resid, boff;
806
807 /* NOTE: sizeof(struct ether_header) should be even */
808 boff = sizeof(struct ether_header);
809 if (off) {
810 /* NOTE: off should be even */
811 boff += off + 2 * sizeof(u_short);
812 totlen -= 2 * sizeof(u_short);
813 resid = totlen - off;
814 } else
815 resid = totlen;
816
817 MGETHDR(m, M_DONTWAIT, MT_DATA);
818 if (m == 0)
819 return (0);
820 m->m_pkthdr.rcvif = ifp;
821 m->m_pkthdr.len = totlen;
822 m->m_len = MHLEN;
823
824 while (totlen > 0) {
825 if (top) {
826 MGET(m, M_DONTWAIT, MT_DATA);
827 if (m == 0) {
828 m_freem(top);
829 return (0);
830 }
831 m->m_len = MLEN;
832 }
833
834 if (resid >= MINCLSIZE)
835 MCLGET(m, M_DONTWAIT);
836 if (m->m_flags & M_EXT)
837 m->m_len = min(resid, MCLBYTES);
838 else if (resid < m->m_len) {
839 /*
840 * Place initial small packet/header at end of mbuf.
841 */
842 if (top == 0 && resid + max_linkhdr <= m->m_len)
843 m->m_data += max_linkhdr;
844 m->m_len = resid;
845 }
846 len = m->m_len;
847 (*le->sc_copyfrombuf)(lebuf, boff, mtod(m, char *), len);
848 boff += len;
849 *mp = m;
850 mp = &m->m_next;
851 totlen -= len;
852 resid -= len;
853 if (resid == 0) {
854 boff = sizeof (struct ether_header);
855 resid = totlen;
856 }
857 }
858 return (top);
859 }
860
861 /*
862 * Process an ioctl request.
863 */
leioctl(ifp,cmd,data)864 leioctl(ifp, cmd, data)
865 register struct ifnet *ifp;
866 int cmd;
867 caddr_t data;
868 {
869 register struct ifaddr *ifa = (struct ifaddr *)data;
870 struct le_softc *le = &le_softc[ifp->if_unit];
871 volatile struct lereg1 *ler1 = le->sc_r1;
872 int s, error = 0;
873
874 s = splnet();
875 switch (cmd) {
876
877 case SIOCSIFADDR:
878 ifp->if_flags |= IFF_UP;
879 switch (ifa->ifa_addr->sa_family) {
880 #ifdef INET
881 case AF_INET:
882 leinit(ifp->if_unit); /* before arpwhohas */
883 ((struct arpcom *)ifp)->ac_ipaddr =
884 IA_SIN(ifa)->sin_addr;
885 arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
886 break;
887 #endif
888 #ifdef NS
889 case AF_NS:
890 {
891 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
892
893 if (ns_nullhost(*ina))
894 ina->x_host = *(union ns_host *)(le->sc_addr);
895 else {
896 /*
897 * The manual says we can't change the address
898 * while the receiver is armed,
899 * so reset everything
900 */
901 ifp->if_flags &= ~IFF_RUNNING;
902 LEWREG(LE_STOP, ler1->ler1_rdp);
903 bcopy((caddr_t)ina->x_host.c_host,
904 (caddr_t)le->sc_addr, sizeof(le->sc_addr));
905 }
906 leinit(ifp->if_unit); /* does le_setaddr() */
907 break;
908 }
909 #endif
910 default:
911 leinit(ifp->if_unit);
912 break;
913 }
914 break;
915
916 #if defined (CCITT) && defined (LLC)
917 case SIOCSIFCONF_X25:
918 ifp->if_flags |= IFF_UP;
919 ifa->ifa_rtrequest = cons_rtrequest;
920 error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
921 if (error == 0)
922 leinit(ifp->if_unit);
923 break;
924 #endif /* CCITT && LLC */
925
926 case SIOCSIFFLAGS:
927 if ((ifp->if_flags & IFF_UP) == 0 &&
928 ifp->if_flags & IFF_RUNNING) {
929 LEWREG(LE_STOP, ler1->ler1_rdp);
930 ifp->if_flags &= ~IFF_RUNNING;
931 } else if (ifp->if_flags & IFF_UP &&
932 (ifp->if_flags & IFF_RUNNING) == 0)
933 leinit(ifp->if_unit);
934 /*
935 * If the state of the promiscuous bit changes, the interface
936 * must be reset to effect the change.
937 */
938 if (((ifp->if_flags ^ le->sc_iflags) & IFF_PROMISC) &&
939 (ifp->if_flags & IFF_RUNNING)) {
940 le->sc_iflags = ifp->if_flags;
941 lereset(ifp->if_unit);
942 lestart(ifp);
943 }
944 break;
945
946 #ifdef MULTICAST
947 case SIOCADDMULTI:
948 case SIOCDELMULTI:
949 /* Update our multicast list */
950 error = (cmd == SIOCADDMULTI) ?
951 ether_addmulti((struct ifreq *)data, &le->sc_ac) :
952 ether_delmulti((struct ifreq *)data, &le->sc_ac);
953
954 if (error == ENETRESET) {
955 /*
956 * Multicast list has changed; set the hardware
957 * filter accordingly.
958 */
959 lereset(ifp->if_unit);
960 error = 0;
961 }
962 break;
963 #endif
964
965 default:
966 error = EINVAL;
967 }
968 splx(s);
969 return (error);
970 }
971
leerror(unit,stat)972 leerror(unit, stat)
973 int unit;
974 int stat;
975 {
976 if (!ledebug)
977 return;
978
979 /*
980 * Not all transceivers implement heartbeat
981 * so we only log CERR once.
982 */
983 if ((stat & LE_CERR) && le_softc[unit].sc_cerr)
984 return;
985 log(LOG_WARNING,
986 "le%d: error: stat=%b\n", unit,
987 stat,
988 "\20\20ERR\17BABL\16CERR\15MISS\14MERR\13RINT\12TINT\11IDON\10INTR\07INEA\06RXON\05TXON\04TDMD\03STOP\02STRT\01INIT");
989 }
990
lererror(unit,msg)991 lererror(unit, msg)
992 int unit;
993 char *msg;
994 {
995 register struct le_softc *le = &le_softc[unit];
996 register volatile void *rmd;
997 u_char eaddr[6];
998 int len;
999
1000 if (!ledebug)
1001 return;
1002
1003 rmd = LER2_RMDADDR(le->sc_r2, le->sc_rmd);
1004 len = LER2V_rmd3(rmd);
1005 if (len > 11)
1006 (*le->sc_copyfrombuf)(LER2_RBUFADDR(le->sc_r2, le->sc_rmd),
1007 6, eaddr, 6);
1008 log(LOG_WARNING,
1009 "le%d: ierror(%s): from %s: buf=%d, len=%d, rmd1=%b\n",
1010 unit, msg,
1011 len > 11 ? ether_sprintf(eaddr) : "unknown",
1012 le->sc_rmd, len,
1013 LER2V_rmd1(rmd),
1014 "\20\20OWN\17ERR\16FRAM\15OFLO\14CRC\13RBUF\12STP\11ENP");
1015 }
1016
lexerror(unit)1017 lexerror(unit)
1018 int unit;
1019 {
1020 register struct le_softc *le = &le_softc[unit];
1021 register volatile void *tmd;
1022 u_char eaddr[6];
1023 int len;
1024
1025 if (!ledebug)
1026 return;
1027
1028 tmd = LER2_TMDADDR(le->sc_r2, 0);
1029 len = -LER2V_tmd2(tmd);
1030 if (len > 5)
1031 (*le->sc_copyfrombuf)(LER2_TBUFADDR(le->sc_r2, 0), 0, eaddr, 6);
1032 log(LOG_WARNING,
1033 "le%d: oerror: to %s: buf=%d, len=%d, tmd1=%b, tmd3=%b\n",
1034 unit,
1035 len > 5 ? ether_sprintf(eaddr) : "unknown",
1036 0, len,
1037 LER2V_tmd1(tmd),
1038 "\20\20OWN\17ERR\16RES\15MORE\14ONE\13DEF\12STP\11ENP",
1039 LER2V_tmd3(tmd),
1040 "\20\20BUFF\17UFLO\16RES\15LCOL\14LCAR\13RTRY");
1041 }
1042
1043 /*
1044 * Write a lance register port, reading it back to ensure success. This seems
1045 * to be necessary during initialization, since the chip appears to be a bit
1046 * pokey sometimes.
1047 */
1048 static void
lewritereg(regptr,val)1049 lewritereg(regptr, val)
1050 register volatile u_short *regptr;
1051 register u_short val;
1052 {
1053 register int i = 0;
1054
1055 while (*regptr != val) {
1056 *regptr = val;
1057 MachEmptyWriteBuffer();
1058 if (++i > 10000) {
1059 printf("le: Reg did not settle (to x%x): x%x\n",
1060 val, *regptr);
1061 return;
1062 }
1063 DELAY(100);
1064 }
1065 }
1066
1067 /*
1068 * Routines for accessing the transmit and receive buffers. Unfortunately,
1069 * CPU addressing of these buffers is done in one of 3 ways:
1070 * - contiguous (for the 3max and turbochannel option card)
1071 * - gap2, which means shorts (2 bytes) interspersed with short (2 byte)
1072 * spaces (for the pmax)
1073 * - gap16, which means 16bytes interspersed with 16byte spaces
1074 * for buffers which must begin on a 32byte boundary (for 3min and maxine)
1075 * The buffer offset is the logical byte offset, assuming contiguous storage.
1076 */
1077 void
copytobuf_contig(from,lebuf,boff,len)1078 copytobuf_contig(from, lebuf, boff, len)
1079 char *from;
1080 volatile void *lebuf;
1081 int boff;
1082 int len;
1083 {
1084
1085 /*
1086 * Just call bcopy() to do the work.
1087 */
1088 bcopy(from, ((char *)lebuf) + boff, len);
1089 }
1090
1091 void
copyfrombuf_contig(lebuf,boff,to,len)1092 copyfrombuf_contig(lebuf, boff, to, len)
1093 volatile void *lebuf;
1094 int boff;
1095 char *to;
1096 int len;
1097 {
1098
1099 /*
1100 * Just call bcopy() to do the work.
1101 */
1102 bcopy(((char *)lebuf) + boff, to, len);
1103 }
1104
1105 void
bzerobuf_contig(lebuf,boff,len)1106 bzerobuf_contig(lebuf, boff, len)
1107 volatile void *lebuf;
1108 int boff;
1109 int len;
1110 {
1111
1112 /*
1113 * Just let bzero() do the work
1114 */
1115 bzero(((char *)lebuf) + boff, len);
1116 }
1117
1118 /*
1119 * For the pmax the buffer consists of shorts (2 bytes) interspersed with
1120 * short (2 byte) spaces and must be accessed with halfword load/stores.
1121 * (don't worry about doing an extra byte)
1122 */
1123 void
copytobuf_gap2(from,lebuf,boff,len)1124 copytobuf_gap2(from, lebuf, boff, len)
1125 register char *from;
1126 volatile void *lebuf;
1127 int boff;
1128 register int len;
1129 {
1130 register volatile u_short *bptr;
1131 register int xfer;
1132
1133 if (boff & 0x1) {
1134 /* handle unaligned first byte */
1135 bptr = ((volatile u_short *)lebuf) + (boff - 1);
1136 *bptr = (*from++ << 8) | (*bptr & 0xff);
1137 bptr += 2;
1138 len--;
1139 } else
1140 bptr = ((volatile u_short *)lebuf) + boff;
1141 if ((unsigned)from & 0x1) {
1142 while (len > 1) {
1143 *bptr = (from[1] << 8) | (from[0] & 0xff);
1144 bptr += 2;
1145 from += 2;
1146 len -= 2;
1147 }
1148 } else {
1149 /* optimize for aligned transfers */
1150 xfer = (int)((unsigned)len & ~0x1);
1151 CopyToBuffer((u_short *)from, bptr, xfer);
1152 bptr += xfer;
1153 from += xfer;
1154 len -= xfer;
1155 }
1156 if (len == 1)
1157 *bptr = (u_short)*from;
1158 }
1159
1160 void
copyfrombuf_gap2(lebuf,boff,to,len)1161 copyfrombuf_gap2(lebuf, boff, to, len)
1162 volatile void *lebuf;
1163 int boff;
1164 register char *to;
1165 register int len;
1166 {
1167 register volatile u_short *bptr;
1168 register u_short tmp;
1169 register int xfer;
1170
1171 if (boff & 0x1) {
1172 /* handle unaligned first byte */
1173 bptr = ((volatile u_short *)lebuf) + (boff - 1);
1174 *to++ = (*bptr >> 8) & 0xff;
1175 bptr += 2;
1176 len--;
1177 } else
1178 bptr = ((volatile u_short *)lebuf) + boff;
1179 if ((unsigned)to & 0x1) {
1180 while (len > 1) {
1181 tmp = *bptr;
1182 *to++ = tmp & 0xff;
1183 *to++ = (tmp >> 8) & 0xff;
1184 bptr += 2;
1185 len -= 2;
1186 }
1187 } else {
1188 /* optimize for aligned transfers */
1189 xfer = (int)((unsigned)len & ~0x1);
1190 CopyFromBuffer(bptr, to, xfer);
1191 bptr += xfer;
1192 to += xfer;
1193 len -= xfer;
1194 }
1195 if (len == 1)
1196 *to = *bptr & 0xff;
1197 }
1198
1199 void
bzerobuf_gap2(lebuf,boff,len)1200 bzerobuf_gap2(lebuf, boff, len)
1201 volatile void *lebuf;
1202 int boff;
1203 int len;
1204 {
1205 register volatile u_short *bptr;
1206
1207 if ((unsigned)boff & 0x1) {
1208 bptr = ((volatile u_short *)lebuf) + (boff - 1);
1209 *bptr &= 0xff;
1210 bptr += 2;
1211 len--;
1212 } else
1213 bptr = ((volatile u_short *)lebuf) + boff;
1214 while (len > 0) {
1215 *bptr = 0;
1216 bptr += 2;
1217 len -= 2;
1218 }
1219 }
1220
1221 /*
1222 * For the 3min and maxine, the buffers are in main memory filled in with
1223 * 16byte blocks interspersed with 16byte spaces.
1224 */
1225 void
copytobuf_gap16(from,lebuf,boff,len)1226 copytobuf_gap16(from, lebuf, boff, len)
1227 register char *from;
1228 volatile void *lebuf;
1229 int boff;
1230 register int len;
1231 {
1232 register char *bptr;
1233 register int xfer;
1234
1235 bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1236 boff &= 0xf;
1237 xfer = min(len, 16 - boff);
1238 while (len > 0) {
1239 bcopy(from, ((char *)bptr) + boff, xfer);
1240 from += xfer;
1241 bptr += 32;
1242 boff = 0;
1243 len -= xfer;
1244 xfer = min(len, 16);
1245 }
1246 }
1247
1248 void
copyfrombuf_gap16(lebuf,boff,to,len)1249 copyfrombuf_gap16(lebuf, boff, to, len)
1250 volatile void *lebuf;
1251 int boff;
1252 register char *to;
1253 register int len;
1254 {
1255 register char *bptr;
1256 register int xfer;
1257
1258 bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1259 boff &= 0xf;
1260 xfer = min(len, 16 - boff);
1261 while (len > 0) {
1262 bcopy(((char *)bptr) + boff, to, xfer);
1263 to += xfer;
1264 bptr += 32;
1265 boff = 0;
1266 len -= xfer;
1267 xfer = min(len, 16);
1268 }
1269 }
1270
1271 void
bzerobuf_gap16(lebuf,boff,len)1272 bzerobuf_gap16(lebuf, boff, len)
1273 volatile void *lebuf;
1274 int boff;
1275 register int len;
1276 {
1277 register char *bptr;
1278 register int xfer;
1279
1280 bptr = ((char *)lebuf) + ((boff << 1) & ~0x1f);
1281 boff &= 0xf;
1282 xfer = min(len, 16 - boff);
1283 while (len > 0) {
1284 bzero(((char *)bptr) + boff, xfer);
1285 bptr += 32;
1286 boff = 0;
1287 len -= xfer;
1288 xfer = min(len, 16);
1289 }
1290 }
1291 #endif /* NLE */
1292