lemac.c revision 1.19 1 /* $NetBSD: lemac.c,v 1.19 2001/04/30 03:53:19 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1994, 1995, 1997 Matt Thomas <matt (at) 3am-software.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software withough specific prior written permission
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*
28 * DEC EtherWORKS 3 Ethernet Controllers
29 *
30 * Written by Matt Thomas
31 * BPF support code stolen directly from if_ec.c
32 *
33 * This driver supports the LEMAC DE203/204/205 cards.
34 */
35
36 #include "opt_inet.h"
37 #include "opt_ns.h"
38 #include "rnd.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/protosw.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/errno.h>
47 #include <sys/malloc.h>
48 #include <sys/device.h>
49 #if NRND > 0
50 #include <sys/rnd.h>
51 #endif
52
53 #include <net/if.h>
54 #include <net/if_types.h>
55 #include <net/if_dl.h>
56 #include <net/route.h>
57 #include <net/if_ether.h>
58 #include <net/if_media.h>
59
60 #ifdef INET
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_var.h>
64 #include <netinet/ip.h>
65 #include <netinet/if_inarp.h>
66 #endif
67
68 #ifdef NS
69 #include <netns/ns.h>
70 #include <netns/ns_if.h>
71 #endif
72
73 #include <machine/bus.h>
74
75 #include <dev/ic/lemacreg.h>
76 #include <dev/ic/lemacvar.h>
77 #if 0
78 #include <i386/isa/decether.h>
79 #endif
80
81 #include <uvm/uvm_extern.h>
82
83 #include "bpfilter.h"
84 #if NBPFILTER > 0
85 #include <net/bpf.h>
86 #endif
87
88 static void lemac_init(lemac_softc_t *sc);
89 static void lemac_ifstart(struct ifnet *ifp);
90 static void lemac_reset(lemac_softc_t *sc);
91 static void lemac_rne_intr(lemac_softc_t *sc);
92 static void lemac_tne_intr(lemac_softc_t *sc);
93 static void lemac_txd_intr(lemac_softc_t *sc, unsigned cs_value);
94 static void lemac_rxd_intr(lemac_softc_t *sc, unsigned cs_value);
95 static int lemac_read_eeprom(lemac_softc_t *sc);
96 static void lemac_init_adapmem(lemac_softc_t *sc);
97
98 static const u_int16_t lemac_allmulti_mctbl[16] = {
99 0xFFFFU, 0xFFFFU, 0xFFFFU, 0xFFFFU,
100 0xFFFFU, 0xFFFFU, 0xFFFFU, 0xFFFFU,
101 0xFFFFU, 0xFFFFU, 0xFFFFU, 0xFFFFU,
102 0xFFFFU, 0xFFFFU, 0xFFFFU, 0xFFFFU,
103 };
104
105 /*
106 * Some tuning/monitoring variables.
107 */
108 unsigned lemac_txmax = 16;
109
110 static void
112 lemac_rxd_intr(
113 lemac_softc_t *sc,
114 unsigned cs_value)
115 {
116 /*
117 * Handle CS_RXD (Receiver disabled) here.
118 *
119 * Check Free Memory Queue Count. If not equal to zero
120 * then just turn Receiver back on. If it is equal to
121 * zero then check to see if transmitter is disabled.
122 * Process transmit TXD loop once more. If all else
123 * fails then do software init (0xC0 to EEPROM Init)
124 * and rebuild Free Memory Queue.
125 */
126
127 sc->sc_cntrs.cntr_rxd_intrs++;
128
129 /*
130 * Re-enable Receiver.
131 */
132
133 cs_value &= ~LEMAC_CS_RXD;
134 LEMAC_OUTB(sc, LEMAC_REG_CS, cs_value);
135
136 if (LEMAC_INB(sc, LEMAC_REG_FMC) > 0)
137 return;
138
139 if (cs_value & LEMAC_CS_TXD)
140 lemac_txd_intr(sc, cs_value);
141
142 if ((LEMAC_INB(sc, LEMAC_REG_CS) & LEMAC_CS_RXD) == 0)
143 return;
144
145 printf("%s: fatal RXD error, attempting recovery\n", sc->sc_if.if_xname);
146
147 lemac_reset(sc);
148 if (sc->sc_if.if_flags & IFF_UP) {
149 lemac_init(sc);
150 return;
151 }
152
153 /*
154 * Error during initializion. Mark card as disabled.
155 */
156 printf("%s: recovery failed -- board disabled\n", sc->sc_if.if_xname);
157 }
158
159 static void
161 lemac_tne_intr(
162 lemac_softc_t *sc)
163 {
164 unsigned txcount = LEMAC_INB(sc, LEMAC_REG_TDC);
165
166 sc->sc_cntrs.cntr_tne_intrs++;
167 while (txcount-- > 0) {
168 unsigned txsts = LEMAC_INB(sc, LEMAC_REG_TDQ);
169 sc->sc_if.if_opackets++; /* another one done */
170 if ((txsts & (LEMAC_TDQ_LCL|LEMAC_TDQ_NCL))
171 || (txsts & LEMAC_TDQ_COL) == LEMAC_TDQ_EXCCOL) {
172 if (txsts & LEMAC_TDQ_NCL)
173 sc->sc_flags &= ~LEMAC_LINKUP;
174 sc->sc_if.if_oerrors++;
175 } else {
176 sc->sc_flags |= LEMAC_LINKUP;
177 if ((txsts & LEMAC_TDQ_COL) != LEMAC_TDQ_NOCOL)
178 sc->sc_if.if_collisions++;
179 }
180 }
181 sc->sc_if.if_flags &= ~IFF_OACTIVE;
182 lemac_ifstart(&sc->sc_if);
183 }
184
185 static void
186 lemac_txd_intr(
187 lemac_softc_t *sc,
188 unsigned cs_value)
189 {
190 /*
191 * Read transmit status, remove transmit buffer from
192 * transmit queue and place on free memory queue,
193 * then reset transmitter.
194 * Increment appropriate counters.
195 */
196
197 sc->sc_cntrs.cntr_txd_intrs++;
198 if (sc->sc_txctl & LEMAC_TX_STP) {
199 sc->sc_if.if_oerrors++;
200 /* return page to free queue */
201 LEMAC_OUTB(sc, LEMAC_REG_FMQ, LEMAC_INB(sc, LEMAC_REG_TDQ));
202 }
203
204 /* Turn back on transmitter if disabled */
205 LEMAC_OUTB(sc, LEMAC_REG_CS, cs_value & ~LEMAC_CS_TXD);
206 sc->sc_if.if_flags &= ~IFF_OACTIVE;
207 }
208
209 static int
211 lemac_read_eeprom(
212 lemac_softc_t *sc)
213 {
214 int word_off, cksum;
215
216 u_char *ep;
217
218 cksum = 0;
219 ep = sc->sc_eeprom;
220 for (word_off = 0; word_off < LEMAC_EEP_SIZE / 2; word_off++) {
221 LEMAC_OUTB(sc, LEMAC_REG_PI1, word_off);
222 LEMAC_OUTB(sc, LEMAC_REG_IOP, LEMAC_IOP_EEREAD);
223
224 DELAY(LEMAC_EEP_DELAY);
225
226 *ep = LEMAC_INB(sc, LEMAC_REG_EE1); cksum += *ep++;
227 *ep = LEMAC_INB(sc, LEMAC_REG_EE2); cksum += *ep++;
228 }
229
230 /*
231 * Set up Transmit Control Byte for use later during transmit.
232 */
233
234 sc->sc_txctl |= LEMAC_TX_FLAGS;
235
236 if ((sc->sc_eeprom[LEMAC_EEP_SWFLAGS] & LEMAC_EEP_SW_SQE) == 0)
237 sc->sc_txctl &= ~LEMAC_TX_SQE;
238
239 if (sc->sc_eeprom[LEMAC_EEP_SWFLAGS] & LEMAC_EEP_SW_LAB)
240 sc->sc_txctl |= LEMAC_TX_LAB;
241
242 bcopy(&sc->sc_eeprom[LEMAC_EEP_PRDNM], sc->sc_prodname, LEMAC_EEP_PRDNMSZ);
243 sc->sc_prodname[LEMAC_EEP_PRDNMSZ] = '\0';
244
245 return cksum % 256;
246 }
247
248 static void
250 lemac_init_adapmem(
251 lemac_softc_t *sc)
252 {
253 int pg, conf;
254
255 conf = LEMAC_INB(sc, LEMAC_REG_CNF);
256
257 if ((sc->sc_eeprom[LEMAC_EEP_SETUP] & LEMAC_EEP_ST_DRAM) == 0) {
258 sc->sc_lastpage = 63;
259 conf &= ~LEMAC_CNF_DRAM;
260 } else {
261 sc->sc_lastpage = 127;
262 conf |= LEMAC_CNF_DRAM;
263 }
264
265 LEMAC_OUTB(sc, LEMAC_REG_CNF, conf);
266
267 for (pg = 1; pg <= sc->sc_lastpage; pg++)
268 LEMAC_OUTB(sc, LEMAC_REG_FMQ, pg);
269 }
270
271 static void
273 lemac_input(
274 lemac_softc_t *sc,
275 bus_addr_t offset,
276 size_t length)
277 {
278 struct ether_header eh;
279 struct mbuf *m;
280
281 if (length - sizeof(eh) > ETHERMTU
282 || length - sizeof(eh) < ETHERMIN) {
283 sc->sc_if.if_ierrors++;
284 return;
285 }
286 if (LEMAC_USE_PIO_MODE(sc)) {
287 LEMAC_INSB(sc, LEMAC_REG_DAT, sizeof(eh), (void *) &eh);
288 } else {
289 LEMAC_GETBUF16(sc, offset, sizeof(eh) / 2, (void *) &eh);
290 }
291
292 MGETHDR(m, M_DONTWAIT, MT_DATA);
293 if (m == NULL) {
294 sc->sc_if.if_ierrors++;
295 return;
296 }
297 if (length + 2 > MHLEN) {
298 MCLGET(m, M_DONTWAIT);
299 if ((m->m_flags & M_EXT) == 0) {
300 m_free(m);
301 sc->sc_if.if_ierrors++;
302 return;
303 }
304 }
305 m->m_data += 2;
306 bcopy((caddr_t)&eh, m->m_data, sizeof(eh));
307 if (LEMAC_USE_PIO_MODE(sc)) {
308 LEMAC_INSB(sc, LEMAC_REG_DAT, length - sizeof(eh),
309 mtod(m, caddr_t) + sizeof(eh));
310 } else {
311 LEMAC_GETBUF16(sc, offset + sizeof(eh), (length - sizeof(eh)) / 2,
312 (void *) (mtod(m, caddr_t) + sizeof(eh)));
313 if (length & 1)
314 m->m_data[length - 1] = LEMAC_GET8(sc, offset + length - 1);
315 }
316 #if NBPFILTER > 0
317 if (sc->sc_if.if_bpf != NULL) {
318 m->m_pkthdr.len = m->m_len = length;
319 bpf_mtap(sc->sc_if.if_bpf, m);
320 }
321 /*
322 * If this is single cast but not to us
323 * drop it!
324 */
325 if ((eh.ether_dhost[0] & 1) == 0
326 && !LEMAC_ADDREQUAL(eh.ether_dhost, sc->sc_enaddr)) {
327 m_freem(m);
328 return;
329 }
330 #endif
331 m->m_pkthdr.len = m->m_len = length;
332 m->m_pkthdr.rcvif = &sc->sc_if;
333 (*sc->sc_if.if_input)(&sc->sc_if, m);
334 }
335
336 static void
338 lemac_rne_intr(
339 lemac_softc_t *sc)
340 {
341 int rxcount;
342
343 sc->sc_cntrs.cntr_rne_intrs++;
344 rxcount = LEMAC_INB(sc, LEMAC_REG_RQC);
345 while (rxcount--) {
346 unsigned rxpg = LEMAC_INB(sc, LEMAC_REG_RQ);
347 u_int32_t rxlen;
348
349 sc->sc_if.if_ipackets++;
350 if (LEMAC_USE_PIO_MODE(sc)) {
351 LEMAC_OUTB(sc, LEMAC_REG_IOP, rxpg);
352 LEMAC_OUTB(sc, LEMAC_REG_PI1, 0);
353 LEMAC_OUTB(sc, LEMAC_REG_PI2, 0);
354 LEMAC_INSB(sc, LEMAC_REG_DAT, sizeof(rxlen), (void *) &rxlen);
355 } else {
356 LEMAC_OUTB(sc, LEMAC_REG_MPN, rxpg);
357 rxlen = LEMAC_GET32(sc, 0);
358 }
359 if (rxlen & LEMAC_RX_OK) {
360 sc->sc_flags |= LEMAC_LINKUP;
361 /*
362 * Get receive length - subtract out checksum.
363 */
364 rxlen = ((rxlen >> 8) & 0x7FF) - 4;
365 lemac_input(sc, sizeof(rxlen), rxlen);
366 } else {
367 sc->sc_if.if_ierrors++;
368 }
369 LEMAC_OUTB(sc, LEMAC_REG_FMQ, rxpg); /* Return this page to Free Memory Queue */
370 } /* end while (recv_count--) */
371
372 return;
373 }
374
375 /*
377 * This is the standard method of reading the DEC Address ROMS.
378 * I don't understand it but it does work.
379 */
380 static int
381 lemac_read_macaddr(
382 unsigned char *hwaddr,
383 const bus_space_tag_t iot,
384 const bus_space_handle_t ioh,
385 const bus_addr_t ioreg,
386 int skippat)
387 {
388 int cksum, rom_cksum;
389 unsigned char addrbuf[6];
390
391 if (!skippat) {
392 int idx, idx2, found, octet;
393 static u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
394 idx2 = found = 0;
395
396 for (idx = 0; idx < 32; idx++) {
397 octet = bus_space_read_1(iot, ioh, ioreg);
398
399 if (octet == testpat[idx2]) {
400 if (++idx2 == sizeof(testpat)) {
401 ++found;
402 break;
403 }
404 } else {
405 idx2 = 0;
406 }
407 }
408
409 if (!found)
410 return -1;
411 }
412
413 if (hwaddr == NULL)
414 hwaddr = addrbuf;
415
416 cksum = 0;
417 hwaddr[0] = bus_space_read_1(iot, ioh, ioreg);
418 hwaddr[1] = bus_space_read_1(iot, ioh, ioreg);
419
420 /* hardware adddress can't be multicast */
421 if (hwaddr[0] & 1)
422 return -1;
423
424 cksum = *(u_short *) &hwaddr[0];
425
426 hwaddr[2] = bus_space_read_1(iot, ioh, ioreg);
427 hwaddr[3] = bus_space_read_1(iot, ioh, ioreg);
428 cksum *= 2;
429 if (cksum > 65535) cksum -= 65535;
430 cksum += *(u_short *) &hwaddr[2];
431 if (cksum > 65535) cksum -= 65535;
432
433 hwaddr[4] = bus_space_read_1(iot, ioh, ioreg);
434 hwaddr[5] = bus_space_read_1(iot, ioh, ioreg);
435 cksum *= 2;
436 if (cksum > 65535) cksum -= 65535;
437 cksum += *(u_short *) &hwaddr[4];
438 if (cksum >= 65535) cksum -= 65535;
439
440 /* 00-00-00 is an illegal OUI */
441 if (hwaddr[0] == 0 && hwaddr[1] == 0 && hwaddr[2] == 0)
442 return -1;
443
444 rom_cksum = bus_space_read_1(iot, ioh, ioreg);
445 rom_cksum |= bus_space_read_1(iot, ioh, ioreg) << 8;
446
447 if (cksum != rom_cksum)
448 return -1;
449 return 0;
450 }
451
452 static void
454 lemac_multicast_op(
455 u_int16_t *mctbl,
456 const u_char *mca,
457 int enable)
458 {
459 u_int idx, bit, crc;
460
461 crc = ether_crc32_le(mca, ETHER_ADDR_LEN);
462
463 /*
464 * The following two lines convert the N bit index into a longword index
465 * and a longword mask.
466 */
467 #if LEMAC_MCTBL_BITS < 0
468 crc >>= (32 + LEMAC_MCTBL_BITS);
469 crc &= (1 << -LEMAC_MCTBL_BITS) - 1;
470 #else
471 crc &= (1 << LEMAC_MCTBL_BITS) - 1;
472 #endif
473 bit = 1 << (crc & 0x0F);
474 idx = crc >> 4;
475
476 /*
477 * Set or clear hash filter bit in our table.
478 */
479 if (enable) {
480 mctbl[idx] |= bit; /* Set Bit */
481 } else {
482 mctbl[idx] &= ~bit; /* Clear Bit */
483 }
484 }
485
486 static void
488 lemac_multicast_filter(
489 lemac_softc_t *sc)
490 {
491 struct ether_multistep step;
492 struct ether_multi *enm;
493
494 bzero(sc->sc_mctbl, LEMAC_MCTBL_BITS / 8);
495
496 lemac_multicast_op(sc->sc_mctbl, etherbroadcastaddr, TRUE);
497
498 ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
499 while (enm != NULL) {
500 if (!LEMAC_ADDREQUAL(enm->enm_addrlo, enm->enm_addrhi)) {
501 sc->sc_flags |= LEMAC_ALLMULTI;
502 sc->sc_if.if_flags |= IFF_ALLMULTI;
503 return;
504 }
505 lemac_multicast_op(sc->sc_mctbl, enm->enm_addrlo, TRUE);
506 ETHER_NEXT_MULTI(step, enm);
507 }
508 sc->sc_flags &= ~LEMAC_ALLMULTI;
509 sc->sc_if.if_flags &= ~IFF_ALLMULTI;
510 }
511
512 /*
514 * Do a hard reset of the board;
515 */
516 static void
517 lemac_reset(
518 lemac_softc_t * const sc)
519 {
520 unsigned data;
521
522 /*
523 * Initialize board..
524 */
525 sc->sc_flags &= ~LEMAC_LINKUP;
526 sc->sc_if.if_flags &= ~IFF_OACTIVE;
527 LEMAC_INTR_DISABLE(sc);
528
529 LEMAC_OUTB(sc, LEMAC_REG_IOP, LEMAC_IOP_EEINIT);
530 DELAY(LEMAC_EEP_DELAY);
531
532 /*
533 * Read EEPROM information. NOTE - the placement of this function
534 * is important because functions hereafter may rely on information
535 * read from the EEPROM.
536 */
537 if ((data = lemac_read_eeprom(sc)) != LEMAC_EEP_CKSUM) {
538 printf("%s: reset: EEPROM checksum failed (0x%x)\n",
539 sc->sc_if.if_xname, data);
540 return;
541 }
542
543 /*
544 * Update the control register to reflect the media choice
545 */
546 data = LEMAC_INB(sc, LEMAC_REG_CTL);
547 if ((data & (LEMAC_CTL_APD|LEMAC_CTL_PSL)) != sc->sc_ctlmode) {
548 data &= ~(LEMAC_CTL_APD|LEMAC_CTL_PSL);
549 data |= sc->sc_ctlmode;
550 LEMAC_OUTB(sc, LEMAC_REG_CTL, data);
551 }
552
553 /*
554 * Force to 2K mode if not already configured.
555 */
556
557 data = LEMAC_INB(sc, LEMAC_REG_MBR);
558 if (LEMAC_IS_2K_MODE(data)) {
559 sc->sc_flags |= LEMAC_2K_MODE;
560 } else if (LEMAC_IS_64K_MODE(data)) {
561 data = (((data * 2) & 0xF) << 4);
562 sc->sc_flags |= LEMAC_WAS_64K_MODE;
563 LEMAC_OUTB(sc, LEMAC_REG_MBR, data);
564 } else if (LEMAC_IS_32K_MODE(data)) {
565 data = ((data & 0xF) << 4);
566 sc->sc_flags |= LEMAC_WAS_32K_MODE;
567 LEMAC_OUTB(sc, LEMAC_REG_MBR, data);
568 } else {
569 sc->sc_flags |= LEMAC_PIO_MODE;
570 /* PIO mode */
571 }
572
573 /*
574 * Initialize Free Memory Queue, Init mcast table with broadcast.
575 */
576
577 lemac_init_adapmem(sc);
578 sc->sc_flags |= LEMAC_ALIVE;
579 }
580
581 static void
583 lemac_init(
584 lemac_softc_t * const sc)
585 {
586 if ((sc->sc_flags & LEMAC_ALIVE) == 0)
587 return;
588
589 /*
590 * If the interface has the up flag
591 */
592 if (sc->sc_if.if_flags & IFF_UP) {
593 int saved_cs = LEMAC_INB(sc, LEMAC_REG_CS);
594 LEMAC_OUTB(sc, LEMAC_REG_CS, saved_cs | (LEMAC_CS_TXD | LEMAC_CS_RXD));
595 LEMAC_OUTB(sc, LEMAC_REG_PA0, sc->sc_enaddr[0]);
596 LEMAC_OUTB(sc, LEMAC_REG_PA1, sc->sc_enaddr[1]);
597 LEMAC_OUTB(sc, LEMAC_REG_PA2, sc->sc_enaddr[2]);
598 LEMAC_OUTB(sc, LEMAC_REG_PA3, sc->sc_enaddr[3]);
599 LEMAC_OUTB(sc, LEMAC_REG_PA4, sc->sc_enaddr[4]);
600 LEMAC_OUTB(sc, LEMAC_REG_PA5, sc->sc_enaddr[5]);
601
602 LEMAC_OUTB(sc, LEMAC_REG_IC, LEMAC_INB(sc, LEMAC_REG_IC) | LEMAC_IC_IE);
603
604 if (sc->sc_if.if_flags & IFF_PROMISC) {
605 LEMAC_OUTB(sc, LEMAC_REG_CS, LEMAC_CS_MCE | LEMAC_CS_PME);
606 } else {
607 LEMAC_INTR_DISABLE(sc);
608 lemac_multicast_filter(sc);
609 if (sc->sc_flags & LEMAC_ALLMULTI)
610 bcopy(lemac_allmulti_mctbl, sc->sc_mctbl, sizeof(sc->sc_mctbl));
611 if (LEMAC_USE_PIO_MODE(sc)) {
612 LEMAC_OUTB(sc, LEMAC_REG_IOP, 0);
613 LEMAC_OUTB(sc, LEMAC_REG_PI1, LEMAC_MCTBL_OFF & 0xFF);
614 LEMAC_OUTB(sc, LEMAC_REG_PI2, LEMAC_MCTBL_OFF >> 8);
615 LEMAC_OUTSB(sc, LEMAC_REG_DAT, sizeof(sc->sc_mctbl), (void *) sc->sc_mctbl);
616 } else {
617 LEMAC_OUTB(sc, LEMAC_REG_MPN, 0);
618 LEMAC_PUTBUF8(sc, LEMAC_MCTBL_OFF, sizeof(sc->sc_mctbl), (void *) sc->sc_mctbl);
619 }
620
621 LEMAC_OUTB(sc, LEMAC_REG_CS, LEMAC_CS_MCE);
622 }
623
624 LEMAC_OUTB(sc, LEMAC_REG_CTL, LEMAC_INB(sc, LEMAC_REG_CTL) ^ LEMAC_CTL_LED);
625
626 LEMAC_INTR_ENABLE(sc);
627 sc->sc_if.if_flags |= IFF_RUNNING;
628 lemac_ifstart(&sc->sc_if);
629 } else {
630 LEMAC_OUTB(sc, LEMAC_REG_CS, LEMAC_CS_RXD|LEMAC_CS_TXD);
631
632 LEMAC_INTR_DISABLE(sc);
633 sc->sc_if.if_flags &= ~IFF_RUNNING;
634 }
635 }
636
637 static void
639 lemac_ifstart(
640 struct ifnet *ifp)
641 {
642 lemac_softc_t * const sc = LEMAC_IFP_TO_SOFTC(ifp);
643
644 if ((ifp->if_flags & IFF_RUNNING) == 0)
645 return;
646
647 LEMAC_INTR_DISABLE(sc);
648
649 for (;;) {
650 struct mbuf *m;
651 struct mbuf *m0;
652 int tx_pg;
653
654 IFQ_POLL(&ifp->if_snd, m);
655 if (m == NULL)
656 break;
657
658 if ((sc->sc_csr.csr_tqc = LEMAC_INB(sc, LEMAC_REG_TQC)) >= lemac_txmax) {
659 sc->sc_cntrs.cntr_txfull++;
660 ifp->if_flags |= IFF_OACTIVE;
661 break;
662 }
663
664 /*
665 * get free memory page
666 */
667 tx_pg = sc->sc_csr.csr_fmq = LEMAC_INB(sc, LEMAC_REG_FMQ);
668 /*
669 * Check for good transmit page.
670 */
671 if (tx_pg == 0 || tx_pg > sc->sc_lastpage) {
672 sc->sc_cntrs.cntr_txnospc++;
673 ifp->if_flags |= IFF_OACTIVE;
674 break;
675 }
676
677 IFQ_DEQUEUE(&ifp->if_snd, m);
678
679 /*
680 * The first four bytes of each transmit buffer are for
681 * control information. The first byte is the control
682 * byte, then the length (why not word aligned?), then
683 * the offset to the buffer.
684 */
685
686 if (LEMAC_USE_PIO_MODE(sc)) {
687 LEMAC_OUTB(sc, LEMAC_REG_IOP, tx_pg); /* Shift 2K window. */
688 LEMAC_OUTB(sc, LEMAC_REG_PI1, 0);
689 LEMAC_OUTB(sc, LEMAC_REG_PI2, 0);
690 LEMAC_OUTB(sc, LEMAC_REG_DAT, sc->sc_txctl);
691 LEMAC_OUTB(sc, LEMAC_REG_DAT, (m->m_pkthdr.len >> 0) & 0xFF);
692 LEMAC_OUTB(sc, LEMAC_REG_DAT, (m->m_pkthdr.len >> 8) & 0xFF);
693 LEMAC_OUTB(sc, LEMAC_REG_DAT, LEMAC_TX_HDRSZ);
694 for (m0 = m; m0 != NULL; m0 = m0->m_next)
695 LEMAC_OUTSB(sc, LEMAC_REG_DAT, m0->m_len, m0->m_data);
696 } else {
697 bus_size_t txoff = /* (mtod(m, u_int32_t) & (sizeof(u_int32_t) - 1)) + */ LEMAC_TX_HDRSZ;
698 LEMAC_OUTB(sc, LEMAC_REG_MPN, tx_pg); /* Shift 2K window. */
699 LEMAC_PUT8(sc, 0, sc->sc_txctl);
700 LEMAC_PUT8(sc, 1, (m->m_pkthdr.len >> 0) & 0xFF);
701 LEMAC_PUT8(sc, 2, (m->m_pkthdr.len >> 8) & 0xFF);
702 LEMAC_PUT8(sc, 3, txoff);
703
704 /*
705 * Copy the packet to the board
706 */
707 for (m0 = m; m0 != NULL; m0 = m0->m_next) {
708 #if 0
709 LEMAC_PUTBUF8(sc, txoff, m0->m_len, m0->m_data);
710 txoff += m0->m_len;
711 #else
712 const u_int8_t *cp = m0->m_data;
713 int len = m0->m_len;
714 #if 0
715 if ((txoff & 3) == (((long)cp) & 3) && len >= 4) {
716 if (txoff & 3) {
717 int alen = (~txoff & 3);
718 LEMAC_PUTBUF8(sc, txoff, alen, cp);
719 cp += alen; txoff += alen; len -= alen;
720 }
721 if (len >= 4) {
722 LEMAC_PUTBUF32(sc, txoff, len / 4, cp);
723 cp += len & ~3; txoff += len & ~3; len &= 3;
724 }
725 }
726 #endif
727 if ((txoff & 1) == (((long)cp) & 1) && len >= 2) {
728 if (txoff & 1) {
729 int alen = (~txoff & 1);
730 LEMAC_PUTBUF8(sc, txoff, alen, cp);
731 cp += alen; txoff += alen; len -= alen;
732 }
733 if (len >= 2) {
734 LEMAC_PUTBUF16(sc, txoff, len / 2, (void *) cp);
735 cp += len & ~1; txoff += len & ~1; len &= 1;
736 }
737 }
738 if (len > 0) {
739 LEMAC_PUTBUF8(sc, txoff, len, cp);
740 txoff += len;
741 }
742 #endif
743 }
744 }
745
746 LEMAC_OUTB(sc, LEMAC_REG_TQ, tx_pg); /* tell chip to transmit this packet */
747 #if NBPFILTER > 0
748 if (sc->sc_if.if_bpf != NULL)
749 bpf_mtap(sc->sc_if.if_bpf, m);
750 #endif
751 m_freem(m); /* free the mbuf */
752 }
753 LEMAC_INTR_ENABLE(sc);
754 }
755
756 static int
758 lemac_ifioctl(
759 struct ifnet *ifp,
760 u_long cmd,
761 caddr_t data)
762 {
763 lemac_softc_t * const sc = LEMAC_IFP_TO_SOFTC(ifp);
764 int s;
765 int error = 0;
766
767 s = splnet();
768
769 switch (cmd) {
770 case SIOCSIFADDR: {
771 struct ifaddr *ifa = (struct ifaddr *)data;
772
773 ifp->if_flags |= IFF_UP;
774 lemac_init(sc);
775 switch (ifa->ifa_addr->sa_family) {
776 #ifdef INET
777 case AF_INET: {
778 arp_ifinit(&sc->sc_if, ifa);
779 break;
780 }
781 #endif /* INET */
782
783 #ifdef NS
784 /* This magic copied from if_is.c; I don't use XNS,
785 * so I have no way of telling if this actually
786 * works or not.
787 */
788 case AF_NS: {
789 struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
790 if (ns_nullhost(*ina)) {
791 ina->x_host = *(union ns_host *)sc->sc_enaddr;
792 } else {
793 bcopy((caddr_t)ina->x_host.c_host, sc->sc_enaddr,
794 ifp->if_addrlen);
795 }
796 break;
797 }
798 #endif /* NS */
799
800 default: {
801 break;
802 }
803 }
804 break;
805 }
806
807 case SIOCSIFFLAGS: {
808 lemac_init(sc);
809 break;
810 }
811
812 case SIOCADDMULTI:
813 case SIOCDELMULTI: {
814 /*
815 * Update multicast listeners
816 */
817 if (cmd == SIOCADDMULTI)
818 error = ether_addmulti((struct ifreq *)data, &sc->sc_ec);
819 else
820 error = ether_delmulti((struct ifreq *)data, &sc->sc_ec);
821
822 if (error == ENETRESET) {
823
824 /* reset multicast filtering */
825 lemac_init(sc);
826 error = 0;
827 }
828 break;
829 }
830
831 case SIOCSIFMEDIA:
832 case SIOCGIFMEDIA: {
833 error = ifmedia_ioctl(ifp, (struct ifreq *)data,
834 &sc->sc_ifmedia, cmd);
835 break;
836 }
837
838 default: {
839 error = EINVAL;
840 break;
841 }
842 }
843
844 splx(s);
845 return error;
846 }
847
848 static int
850 lemac_ifmedia_change(
851 struct ifnet * const ifp)
852 {
853 lemac_softc_t * const sc = LEMAC_IFP_TO_SOFTC(ifp);
854 unsigned new_ctl;
855
856 switch (IFM_SUBTYPE(sc->sc_ifmedia.ifm_media)) {
857 case IFM_10_T: new_ctl = LEMAC_CTL_APD; break;
858 case IFM_10_2:
859 case IFM_10_5: new_ctl = LEMAC_CTL_APD|LEMAC_CTL_PSL; break;
860 case IFM_AUTO: new_ctl = 0; break;
861 default: return EINVAL;
862 }
863 if (sc->sc_ctlmode != new_ctl) {
864 sc->sc_ctlmode = new_ctl;
865 lemac_reset(sc);
866 if (sc->sc_if.if_flags & IFF_UP)
867 lemac_init(sc);
868 }
869 return 0;
870 }
871
872 /*
873 * Media status callback
874 */
875 static void
876 lemac_ifmedia_status(
877 struct ifnet * const ifp,
878 struct ifmediareq *req)
879 {
880 lemac_softc_t *sc = LEMAC_IFP_TO_SOFTC(ifp);
881 unsigned data = LEMAC_INB(sc, LEMAC_REG_CNF);
882
883 req->ifm_status = IFM_AVALID;
884 if (sc->sc_flags & LEMAC_LINKUP)
885 req->ifm_status |= IFM_ACTIVE;
886
887 if (sc->sc_ctlmode & LEMAC_CTL_APD) {
888 if (sc->sc_ctlmode & LEMAC_CTL_PSL) {
889 req->ifm_active = IFM_10_5;
890 } else {
891 req->ifm_active = IFM_10_T;
892 }
893 } else {
894 /*
895 * The link bit of the configuration register reflects the
896 * current media choice when auto-port is enabled.
897 */
898 if (data & LEMAC_CNF_NOLINK) {
899 req->ifm_active = IFM_10_5;
900 } else {
901 req->ifm_active = IFM_10_T;
902 }
903 }
904
905 req->ifm_active |= IFM_ETHER;
906 }
907
908 int
910 lemac_port_check(
911 const bus_space_tag_t iot,
912 const bus_space_handle_t ioh)
913 {
914 unsigned char hwaddr[6];
915
916 if (lemac_read_macaddr(hwaddr, iot, ioh, LEMAC_REG_APD, 0) == 0)
917 return 1;
918 if (lemac_read_macaddr(hwaddr, iot, ioh, LEMAC_REG_APD, 1) == 0)
919 return 1;
920 return 0;
921 }
922
923 void
925 lemac_info_get(
926 const bus_space_tag_t iot,
927 const bus_space_handle_t ioh,
928 bus_addr_t *maddr_p,
929 bus_size_t *msize_p,
930 int *irq_p)
931 {
932 unsigned data;
933
934 *irq_p = LEMAC_DECODEIRQ(bus_space_read_1(iot, ioh, LEMAC_REG_IC) & LEMAC_IC_IRQMSK);
935
936 data = bus_space_read_1(iot, ioh, LEMAC_REG_MBR);
937 if (LEMAC_IS_2K_MODE(data)) {
938 *maddr_p = data * (2 * 1024) + (512 * 1024);
939 *msize_p = 2 * 1024;
940 } else if (LEMAC_IS_64K_MODE(data)) {
941 *maddr_p = data * 64 * 1024;
942 *msize_p = 64 * 1024;
943 } else if (LEMAC_IS_32K_MODE(data)) {
944 *maddr_p = data * 32 * 1024;
945 *msize_p = 32* 1024;
946 } else {
947 *maddr_p = 0;
948 *msize_p = 0;
949 }
950 }
951
952 /*
954 * What to do upon receipt of an interrupt.
955 */
956 int
957 lemac_intr(
958 void *arg)
959 {
960 lemac_softc_t * const sc = arg;
961 int cs_value;
962
963 LEMAC_INTR_DISABLE(sc); /* Mask interrupts */
964
965 /*
966 * Determine cause of interrupt. Receive events take
967 * priority over Transmit.
968 */
969
970 cs_value = LEMAC_INB(sc, LEMAC_REG_CS);
971
972 /*
973 * Check for Receive Queue not being empty.
974 * Check for Transmit Done Queue not being empty.
975 */
976
977 if (cs_value & LEMAC_CS_RNE)
978 lemac_rne_intr(sc);
979 if (cs_value & LEMAC_CS_TNE)
980 lemac_tne_intr(sc);
981
982 /*
983 * Check for Transmitter Disabled.
984 * Check for Receiver Disabled.
985 */
986
987 if (cs_value & LEMAC_CS_TXD)
988 lemac_txd_intr(sc, cs_value);
989 if (cs_value & LEMAC_CS_RXD)
990 lemac_rxd_intr(sc, cs_value);
991
992 /*
993 * Toggle LED and unmask interrupts.
994 */
995
996 sc->sc_csr.csr_cs = LEMAC_INB(sc, LEMAC_REG_CS);
997
998 LEMAC_OUTB(sc, LEMAC_REG_CTL, LEMAC_INB(sc, LEMAC_REG_CTL) ^ LEMAC_CTL_LED);
999 LEMAC_INTR_ENABLE(sc); /* Unmask interrupts */
1000
1001 #if NRND > 0
1002 if (cs_value)
1003 rnd_add_uint32(&sc->rnd_source, cs_value);
1004 #endif
1005
1006 return 1;
1007 }
1008
1009 void
1010 lemac_shutdown(
1011 void *arg)
1012 {
1013 lemac_reset((lemac_softc_t *) arg);
1014 }
1015
1016 static const char * const lemac_modes[4] = {
1018 "PIO mode (internal 2KB window)",
1019 "2KB window",
1020 "changed 32KB window to 2KB",
1021 "changed 64KB window to 2KB",
1022 };
1023
1024 void
1025 lemac_ifattach(
1026 lemac_softc_t *sc)
1027 {
1028 struct ifnet * const ifp = &sc->sc_if;
1029
1030 bcopy(sc->sc_dv.dv_xname, ifp->if_xname, IFNAMSIZ);
1031
1032 lemac_reset(sc);
1033
1034 (void) lemac_read_macaddr(sc->sc_enaddr, sc->sc_iot, sc->sc_ioh,
1035 LEMAC_REG_APD, 0);
1036
1037 printf(": %s\n", sc->sc_prodname);
1038
1039 printf("%s: address %s, %dKB RAM, %s\n",
1040 ifp->if_xname,
1041 ether_sprintf(sc->sc_enaddr),
1042 sc->sc_lastpage * 2 + 2,
1043 lemac_modes[sc->sc_flags & LEMAC_MODE_MASK]);
1044
1045 ifp->if_softc = (void *) sc;
1046 ifp->if_start = lemac_ifstart;
1047 ifp->if_ioctl = lemac_ifioctl;
1048
1049 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX
1050 #ifdef IFF_NOTRAILERS
1051 | IFF_NOTRAILERS
1052 #endif
1053 | IFF_MULTICAST;
1054
1055 if (sc->sc_flags & LEMAC_ALIVE) {
1056 int media;
1057
1058 IFQ_SET_READY(&ifp->if_snd);
1059
1060 if_attach(ifp);
1061 ether_ifattach(ifp, sc->sc_enaddr);
1062
1063 #if NRND > 0
1064 rnd_attach_source(&sc->rnd_source, sc->sc_dv.dv_xname,
1065 RND_TYPE_NET, 0);
1066 #endif
1067
1068 ifmedia_init(&sc->sc_ifmedia, 0,
1069 lemac_ifmedia_change,
1070 lemac_ifmedia_status);
1071 if (sc->sc_prodname[4] == '5') /* DE205 is UTP/AUI */
1072 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_AUTO, 0, 0);
1073 if (sc->sc_prodname[4] != '3') /* DE204 & 205 have UTP */
1074 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_10_T, 0, 0);
1075 if (sc->sc_prodname[4] != '4') /* DE203 & 205 have BNC */
1076 ifmedia_add(&sc->sc_ifmedia, IFM_ETHER | IFM_10_5, 0, 0);
1077 switch (sc->sc_prodname[4]) {
1078 case '3': media = IFM_10_5; break;
1079 case '4': media = IFM_10_T; break;
1080 default: media = IFM_AUTO; break;
1081 }
1082 ifmedia_set(&sc->sc_ifmedia, IFM_ETHER | media);
1083 } else {
1084 printf("%s: disabled due to error\n", ifp->if_xname);
1085 }
1086 }
1087