if_bm.c revision 1.1 1 /* $NetBSD: if_bm.c,v 1.1 1999/01/01 01:27:52 tsubai Exp $ */
2
3 /*-
4 * Copyright (C) 1998, 1999 Tsubai Masanari. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "opt_inet.h"
30 #include "opt_ns.h"
31 #include "bpfilter.h"
32
33 #include <sys/param.h>
34 #include <sys/device.h>
35 #include <sys/ioctl.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/systm.h>
39
40 #include <net/if.h>
41 #include <net/if_ether.h>
42 #include <net/if_media.h>
43
44 #if NBPFILTER > 0
45 #include <net/bpf.h>
46 #include <net/bpfdesc.h>
47 #endif
48
49 #include <vm/vm.h>
50
51 #include <dev/ofw/openfirm.h>
52
53 #include <machine/autoconf.h>
54 #include <machine/pio.h>
55
56 #include <macppc/dev/dbdma.h>
57 #include <macppc/dev/if_bmreg.h>
58
59 #define BMAC_TXBUFS 2
60 #define BMAC_RXBUFS 16
61 #define BMAC_BUFLEN 2048
62
63 struct bmac_softc {
64 struct device sc_dev;
65 struct ethercom sc_ethercom;
66 #define sc_if sc_ethercom.ec_if
67 struct ifmedia sc_media;
68 vaddr_t sc_regs;
69 dbdma_regmap_t *sc_txdma;
70 dbdma_regmap_t *sc_rxdma;
71 dbdma_command_t *sc_txcmd;
72 dbdma_command_t *sc_rxcmd;
73 caddr_t sc_txbuf;
74 caddr_t sc_rxbuf;
75 int sc_rxlast;
76 int sc_flags;
77 int sc_debug;
78 u_char sc_enaddr[6];
79 };
80
81 #define BMAC_BMACPLUS 0x01
82
83 extern u_int *heathrow_FCR;
84
85 static __inline int bmac_read_reg __P((struct bmac_softc *, int));
86 static __inline void bmac_write_reg __P((struct bmac_softc *, int, int));
87 static __inline void bmac_set_bits __P((struct bmac_softc *, int, int));
88 static __inline void bmac_reset_bits __P((struct bmac_softc *, int, int));
89
90 static int bmac_match __P((struct device *, struct cfdata *, void *));
91 static void bmac_attach __P((struct device *, struct device *, void *));
92 static void bmac_reset_chip __P((struct bmac_softc *));
93 static void bmac_init __P((struct bmac_softc *));
94 static void bmac_init_dma __P((struct bmac_softc *));
95 static int bmac_intr __P((void *));
96 static int bmac_rint __P((void *));
97 static void bmac_reset __P((struct bmac_softc *));
98 static void bmac_stop __P((struct bmac_softc *));
99 static void bmac_start __P((struct ifnet *));
100 static void bmac_transmit_packet __P((struct bmac_softc *, void *, int));
101 static int bmac_put __P((struct bmac_softc *, caddr_t, struct mbuf *));
102 static struct mbuf *bmac_get __P((struct bmac_softc *, caddr_t, int));
103 static void bmac_watchdog __P((struct ifnet *));
104 static int bmac_ioctl __P((struct ifnet *, u_long, caddr_t));
105 static int bmac_mediachange __P((struct ifnet *));
106 static void bmac_mediastatus __P((struct ifnet *, struct ifmediareq *));
107 static void bmac_setladrf __P((struct bmac_softc *));
108
109 struct cfattach bm_ca = {
110 sizeof(struct bmac_softc), bmac_match, bmac_attach
111 };
112
113 int
114 bmac_read_reg(sc, off)
115 struct bmac_softc *sc;
116 int off;
117 {
118 return in16rb(sc->sc_regs + off);
119 }
120
121 void
122 bmac_write_reg(sc, off, val)
123 struct bmac_softc *sc;
124 int off, val;
125 {
126 out16rb(sc->sc_regs + off, val);
127 }
128
129 void
130 bmac_set_bits(sc, off, val)
131 struct bmac_softc *sc;
132 int off, val;
133 {
134 val |= bmac_read_reg(sc, off);
135 bmac_write_reg(sc, off, val);
136 }
137
138 void
139 bmac_reset_bits(sc, off, val)
140 struct bmac_softc *sc;
141 int off, val;
142 {
143 bmac_write_reg(sc, off, bmac_read_reg(sc, off) & ~val);
144 }
145
146 int
147 bmac_match(parent, cf, aux)
148 struct device *parent;
149 struct cfdata *cf;
150 void *aux;
151 {
152 struct confargs *ca = aux;
153
154 if (ca->ca_nreg < 24 || ca->ca_nintr < 12)
155 return 0;
156
157 if (strcmp(ca->ca_name, "bmac") == 0) /* bmac */
158 return 1;
159 if (strcmp(ca->ca_name, "ethernet") == 0) /* bmac+ */
160 return 1;
161
162 return 0;
163 }
164
165 void
166 bmac_attach(parent, self, aux)
167 struct device *parent, *self;
168 void *aux;
169 {
170 struct confargs *ca = aux;
171 struct bmac_softc *sc = (void *)self;
172 struct ifnet *ifp = &sc->sc_if;
173 u_char laddr[6];
174 int i;
175
176 sc->sc_flags =0;
177 if (strcmp(ca->ca_name, "ethernet") == 0)
178 sc->sc_flags |= BMAC_BMACPLUS;
179
180 ca->ca_reg[0] += ca->ca_baseaddr;
181 ca->ca_reg[2] += ca->ca_baseaddr;
182 ca->ca_reg[4] += ca->ca_baseaddr;
183
184 sc->sc_regs = (vaddr_t)mapiodev(ca->ca_reg[0], NBPG);
185
186 bmac_write_reg(sc, INTDISABLE, NoEventsMask);
187
188 if (OF_getprop(ca->ca_node, "local-mac-address", laddr, 6) == -1 &&
189 OF_getprop(ca->ca_node, "mac-address", laddr, 6) == -1) {
190 printf(": cannot get mac-address\n");
191 return;
192 }
193 bcopy(laddr, sc->sc_enaddr, 6);
194
195 sc->sc_txdma = mapiodev(ca->ca_reg[2], NBPG);
196 sc->sc_rxdma = mapiodev(ca->ca_reg[4], NBPG);
197 sc->sc_txcmd = dbdma_alloc(BMAC_TXBUFS * sizeof(dbdma_command_t));
198 sc->sc_rxcmd = dbdma_alloc((BMAC_RXBUFS + 1) * sizeof(dbdma_command_t));
199 sc->sc_txbuf = malloc(BMAC_BUFLEN * BMAC_TXBUFS, M_DEVBUF, M_NOWAIT);
200 sc->sc_rxbuf = malloc(BMAC_BUFLEN * BMAC_RXBUFS, M_DEVBUF, M_NOWAIT);
201 if (sc->sc_txbuf == NULL || sc->sc_rxbuf == NULL ||
202 sc->sc_txcmd == NULL || sc->sc_rxcmd == NULL) {
203 printf("cannot allocate memory\n");
204 return;
205 }
206
207 printf(" irq %d,%d: address %s\n", ca->ca_intr[0], ca->ca_intr[2],
208 ether_sprintf(laddr));
209
210 intr_establish(ca->ca_intr[0], IST_LEVEL, IPL_NET, bmac_intr, sc);
211 intr_establish(ca->ca_intr[2], IST_LEVEL, IPL_NET, bmac_rint, sc);
212
213 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
214 ifp->if_softc = sc;
215 ifp->if_ioctl = bmac_ioctl;
216 ifp->if_start = bmac_start;
217 ifp->if_flags =
218 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
219 ifp->if_watchdog = bmac_watchdog;
220
221 ifmedia_init(&sc->sc_media, 0, bmac_mediachange, bmac_mediastatus);
222 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_T, 0, NULL);
223 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_T);
224
225 bmac_reset_chip(sc);
226
227 if_attach(ifp);
228 ether_ifattach(ifp, sc->sc_enaddr);
229
230 #if NBPFILTER > 0
231 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
232 #endif
233 }
234
235 /*
236 * Reset and enable bmac by heathrow FCR.
237 */
238 void
239 bmac_reset_chip(sc)
240 struct bmac_softc *sc;
241 {
242 u_int v;
243
244 dbdma_reset(sc->sc_txdma);
245 dbdma_reset(sc->sc_rxdma);
246
247 v = in32rb(heathrow_FCR);
248
249 v |= EnetEnable;
250 out32rb(heathrow_FCR, v);
251 delay(50000);
252
253 v |= ResetEnetCell;
254 out32rb(heathrow_FCR, v);
255 delay(50000);
256
257 v &= ~ResetEnetCell;
258 out32rb(heathrow_FCR, v);
259 delay(50000);
260
261 out32rb(heathrow_FCR, v);
262 }
263
264 void
265 bmac_init(sc)
266 struct bmac_softc *sc;
267 {
268 struct ifnet *ifp = &sc->sc_if;
269 struct ether_header *eh;
270 caddr_t data;
271 int i, tb;
272 u_short *p;
273
274 bmac_reset_chip(sc);
275
276 bmac_write_reg(sc, RXRST, RxResetValue);
277 bmac_write_reg(sc, TXRST, TxResetBit);
278
279 /* Wait for reset completion. */
280 while (bmac_read_reg(sc, TXRST) & TxResetBit);
281
282 if (! (sc->sc_flags & BMAC_BMACPLUS))
283 bmac_set_bits(sc, XCVRIF, ClkBit|SerialMode|COLActiveLow);
284
285 __asm __volatile ("mftb %0" : "=r"(tb));
286 bmac_write_reg(sc, RSEED, tb);
287 bmac_set_bits(sc, XIFC, TxOutputEnable);
288 bmac_read_reg(sc, PAREG);
289
290 /* Reset various counters. */
291 bmac_write_reg(sc, NCCNT, 0);
292 bmac_write_reg(sc, NTCNT, 0);
293 bmac_write_reg(sc, EXCNT, 0);
294 bmac_write_reg(sc, LTCNT, 0);
295 bmac_write_reg(sc, FRCNT, 0);
296 bmac_write_reg(sc, LECNT, 0);
297 bmac_write_reg(sc, AECNT, 0);
298 bmac_write_reg(sc, FECNT, 0);
299 bmac_write_reg(sc, RXCV, 0);
300
301 /* Set tx fifo information. */
302 bmac_write_reg(sc, TXTH, 4); /* 4 octets before tx starts */
303
304 bmac_write_reg(sc, TXFIFOCSR, 0);
305 bmac_write_reg(sc, TXFIFOCSR, TxFIFOEnable);
306
307 /* Set rx fifo information. */
308 bmac_write_reg(sc, RXFIFOCSR, 0);
309 bmac_write_reg(sc, RXFIFOCSR, RxFIFOEnable);
310
311 /* Clear status register. */
312 bmac_read_reg(sc, STATUS);
313
314 bmac_write_reg(sc, HASH3, 0);
315 bmac_write_reg(sc, HASH2, 0);
316 bmac_write_reg(sc, HASH1, 0);
317 bmac_write_reg(sc, HASH0, 0);
318
319 /* Set MAC address. */
320 p = (u_short *)sc->sc_enaddr;
321 bmac_write_reg(sc, MADD0, *p++);
322 bmac_write_reg(sc, MADD1, *p++);
323 bmac_write_reg(sc, MADD2, *p);
324
325 bmac_write_reg(sc, RXCFG,
326 RxCRCEnable | RxHashFilterEnable | RxRejectOwnPackets);
327
328 if (ifp->if_flags & IFF_PROMISC)
329 bmac_set_bits(sc, RXCFG, RxPromiscEnable);
330
331 bmac_init_dma(sc);
332
333 /* Enable TX/RX */
334 bmac_set_bits(sc, RXCFG, RxMACEnable);
335 bmac_set_bits(sc, TXCFG, TxMACEnable);
336
337 bmac_write_reg(sc, INTDISABLE, NormalIntEvents);
338
339 ifp->if_flags |= IFF_RUNNING;
340 ifp->if_flags &= ~IFF_OACTIVE;
341 ifp->if_timer = 0;
342
343 data = sc->sc_txbuf;
344 eh = (struct ether_header *)data;
345
346 bzero(data, sizeof(eh) + ETHERMIN);
347 bcopy(sc->sc_enaddr, eh->ether_dhost, ETHER_ADDR_LEN);
348 bcopy(sc->sc_enaddr, eh->ether_shost, ETHER_ADDR_LEN);
349 bmac_transmit_packet(sc, data, sizeof(eh) + ETHERMIN);
350
351 bmac_start(ifp);
352 }
353
354 void
355 bmac_init_dma(sc)
356 struct bmac_softc *sc;
357 {
358 dbdma_command_t *cmd = sc->sc_rxcmd;
359 int i;
360
361 dbdma_reset(sc->sc_txdma);
362 dbdma_reset(sc->sc_rxdma);
363
364 bzero(sc->sc_txcmd, BMAC_TXBUFS * sizeof(dbdma_command_t));
365 bzero(sc->sc_rxcmd, (BMAC_RXBUFS + 1) * sizeof(dbdma_command_t));
366
367 for (i = 0; i < BMAC_RXBUFS; i++) {
368 DBDMA_BUILD(cmd, DBDMA_CMD_IN_LAST, 0, BMAC_BUFLEN,
369 vtophys(sc->sc_rxbuf + BMAC_BUFLEN * i),
370 DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
371 cmd++;
372 }
373 DBDMA_BUILD(cmd, DBDMA_CMD_NOP, 0, 0, 0,
374 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_ALWAYS);
375 dbdma_st32(&cmd->d_cmddep, vtophys(sc->sc_rxcmd));
376
377 sc->sc_rxlast = 0;
378
379 dbdma_start(sc->sc_rxdma, sc->sc_rxcmd);
380 }
381
382 int
383 bmac_intr(v)
384 void *v;
385 {
386 struct bmac_softc *sc = v;
387 int stat;
388
389 stat = bmac_read_reg(sc, STATUS);
390 if (stat == 0)
391 return 0;
392
393 #ifdef BMAC_DEBUG
394 printf("bmac_intr status = 0x%x\n", stat);
395 #endif
396
397 if (stat & IntFrameSent) {
398 sc->sc_if.if_flags &= ~IFF_OACTIVE;
399 sc->sc_if.if_timer = 0;
400 sc->sc_if.if_opackets++;
401 bmac_start(&sc->sc_if);
402 }
403
404 /* XXX should do more! */
405
406 return 1;
407 }
408
409 int
410 bmac_rint(v)
411 void *v;
412 {
413 struct bmac_softc *sc = v;
414 struct ifnet *ifp = &sc->sc_if;
415 struct mbuf *m;
416 dbdma_command_t *cmd;
417 int status, resid, count, datalen;
418 int i, n;
419 void *data;
420
421 i = sc->sc_rxlast;
422 for (n = 0; n < BMAC_RXBUFS; n++, i++) {
423 if (i == BMAC_RXBUFS)
424 i = 0;
425 cmd = &sc->sc_rxcmd[i];
426 status = dbdma_ld16(&cmd->d_status);
427 resid = dbdma_ld16(&cmd->d_resid);
428
429 #ifdef BMAC_DEBUG
430 if (status != 0 && status != 0x8440 && status != 0x9440)
431 printf("bmac_rint status = 0x%x\n", status);
432 #endif
433
434 if ((status & DBDMA_CNTRL_ACTIVE) == 0) /* 0x9440 | 0x8440 */
435 continue;
436 count = dbdma_ld16(&cmd->d_count);
437 datalen = count - resid;
438 if (datalen < sizeof(struct ether_header)) {
439 printf("%s: short packet len = %d\n",
440 ifp->if_xname, datalen);
441 goto next;
442 }
443 DBDMA_BUILD_CMD(cmd, DBDMA_CMD_STOP, 0, 0, 0, 0);
444 data = sc->sc_rxbuf + BMAC_BUFLEN * i;
445 m = bmac_get(sc, data, datalen);
446
447 if (m == NULL) {
448 ifp->if_ierrors++;
449 goto next;
450 }
451
452 #if NBPFILTER > 0
453 /*
454 * Check if there's a BPF listener on this interface.
455 * If so, hand off the raw packet to BPF.
456 */
457 if (ifp->if_bpf)
458 bpf_mtap(ifp->if_bpf, m);
459 #endif
460 m_adj(m, sizeof(struct ether_header));
461 ether_input(ifp, data, m);
462 ifp->if_ipackets++;
463
464 next:
465 DBDMA_BUILD_CMD(cmd, DBDMA_CMD_IN_LAST, 0, DBDMA_INT_ALWAYS,
466 DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
467
468 cmd->d_status = 0;
469 cmd->d_resid = 0;
470 sc->sc_rxlast = i + 1;
471 }
472 dbdma_continue(sc->sc_rxdma);
473
474 return 1;
475 }
476
477 void
478 bmac_reset(sc)
479 struct bmac_softc *sc;
480 {
481 int s;
482
483 s = splnet();
484 bmac_init(sc);
485 splx(s);
486 }
487
488 void
489 bmac_stop(sc)
490 struct bmac_softc *sc;
491 {
492 struct ifnet *ifp = &sc->sc_if;
493 int s;
494
495 s = splnet();
496
497 /* Disable TX/RX. */
498 bmac_reset_bits(sc, TXCFG, TxMACEnable);
499 bmac_reset_bits(sc, RXCFG, RxMACEnable);
500
501 /* Disable all interrupts. */
502 bmac_write_reg(sc, INTDISABLE, NoEventsMask);
503
504 dbdma_stop(sc->sc_txdma);
505 dbdma_stop(sc->sc_rxdma);
506
507 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING);
508 ifp->if_timer = 0;
509
510 splx(s);
511 }
512
513 void
514 bmac_start(ifp)
515 struct ifnet *ifp;
516 {
517 struct bmac_softc *sc = ifp->if_softc;
518 struct mbuf *m;
519 int tlen;
520
521 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
522 return;
523
524 while (1) {
525 if (ifp->if_flags & IFF_OACTIVE)
526 return;
527
528 IF_DEQUEUE(&ifp->if_snd, m);
529 if (m == 0)
530 break;
531 #if NBPFILTER > 0
532 /*
533 * If BPF is listening on this interface, let it see the
534 * packet before we commit it to the wire.
535 */
536 if (ifp->if_bpf)
537 bpf_mtap(ifp->if_bpf, m);
538 #endif
539
540 ifp->if_flags |= IFF_OACTIVE;
541 tlen = bmac_put(sc, sc->sc_txbuf, m);
542
543 /* 5 seconds to watch for failing to transmit */
544 ifp->if_timer = 5;
545 ifp->if_opackets++; /* # of pkts */
546
547 bmac_transmit_packet(sc, sc->sc_txbuf, tlen);
548 }
549 }
550
551 void
552 bmac_transmit_packet(sc, buff, len)
553 struct bmac_softc *sc;
554 void *buff;
555 int len;
556 {
557 dbdma_command_t *cmd = sc->sc_txcmd;
558 vaddr_t va = (vaddr_t)buff;
559
560 #ifdef BMAC_DEBUG
561 if (vtophys(va) + len - 1 != vtophys(va + len - 1))
562 panic("bmac_transmit_packet");
563 #endif
564
565 DBDMA_BUILD(cmd, DBDMA_CMD_OUT_LAST, 0, len, vtophys(va),
566 DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
567 cmd++;
568 DBDMA_BUILD(cmd, DBDMA_CMD_STOP, 0, 0, 0,
569 DBDMA_INT_ALWAYS, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
570
571 dbdma_start(sc->sc_txdma, sc->sc_txcmd);
572 }
573
574 int
575 bmac_put(sc, buff, m)
576 struct bmac_softc *sc;
577 caddr_t buff;
578 struct mbuf *m;
579 {
580 struct mbuf *n;
581 int len, tlen = 0;
582
583 for (; m; m = n) {
584 len = m->m_len;
585 if (len == 0) {
586 MFREE(m, n);
587 continue;
588 }
589 bcopy(mtod(m, caddr_t), buff, len);
590 buff += len;
591 tlen += len;
592 MFREE(m, n);
593 }
594 if (tlen > NBPG)
595 panic("%s: putpacket packet overflow", sc->sc_dev.dv_xname);
596
597 return tlen;
598 }
599
600 struct mbuf *
601 bmac_get(sc, pkt, totlen)
602 struct bmac_softc *sc;
603 caddr_t pkt;
604 int totlen;
605 {
606 struct mbuf *m;
607 struct mbuf *top, **mp;
608 int len;
609
610 MGETHDR(m, M_DONTWAIT, MT_DATA);
611 if (m == 0)
612 return 0;
613 m->m_pkthdr.rcvif = &sc->sc_if;
614 m->m_pkthdr.len = totlen;
615 len = MHLEN;
616 top = 0;
617 mp = ⊤
618
619 while (totlen > 0) {
620 if (top) {
621 MGET(m, M_DONTWAIT, MT_DATA);
622 if (m == 0) {
623 m_freem(top);
624 return 0;
625 }
626 len = MLEN;
627 }
628 if (totlen >= MINCLSIZE) {
629 MCLGET(m, M_DONTWAIT);
630 if ((m->m_flags & M_EXT) == 0) {
631 m_free(m);
632 m_freem(top);
633 return 0;
634 }
635 len = MCLBYTES;
636 }
637 m->m_len = len = min(totlen, len);
638 bcopy(pkt, mtod(m, caddr_t), len);
639 pkt += len;
640 totlen -= len;
641 *mp = m;
642 mp = &m->m_next;
643 }
644
645 return top;
646 }
647
648 void
649 bmac_watchdog(ifp)
650 struct ifnet *ifp;
651 {
652 struct bmac_softc *sc = ifp->if_softc;
653
654 bmac_reset_bits(sc, RXCFG, RxMACEnable);
655 bmac_reset_bits(sc, TXCFG, TxMACEnable);
656
657 printf("%s: device timeout\n", ifp->if_xname);
658 ifp->if_oerrors++;
659
660 bmac_reset(sc);
661 }
662
663 int
664 bmac_ioctl(ifp, cmd, data)
665 struct ifnet *ifp;
666 u_long cmd;
667 caddr_t data;
668 {
669 struct bmac_softc *sc = ifp->if_softc;
670 struct ifaddr *ifa = (struct ifaddr *)data;
671 struct ifreq *ifr = (struct ifreq *)data;
672 int s, error = 0;
673 int temp;
674
675 s = splnet();
676
677 switch (cmd) {
678
679 case SIOCSIFADDR:
680 ifp->if_flags |= IFF_UP;
681
682 switch (ifa->ifa_addr->sa_family) {
683 #ifdef INET
684 case AF_INET:
685 bmac_init(sc);
686 arp_ifinit(ifp, ifa);
687 break;
688 #endif
689 #ifdef NS
690 case AF_NS:
691 {
692 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
693
694 if (ns_nullhost(*ina))
695 ina->x_host =
696 *(union ns_host *)LLADDR(ifp->if_sadl);
697 else {
698 bcopy(ina->x_host.c_host,
699 LLADDR(ifp->if_sadl),
700 sizeof(sc->sc_enaddr));
701 }
702 /* Set new address. */
703 bmac_init(sc);
704 break;
705 }
706 #endif
707 default:
708 bmac_init(sc);
709 break;
710 }
711 break;
712
713 case SIOCSIFFLAGS:
714 if ((ifp->if_flags & IFF_UP) == 0 &&
715 (ifp->if_flags & IFF_RUNNING) != 0) {
716 /*
717 * If interface is marked down and it is running, then
718 * stop it.
719 */
720 bmac_stop(sc);
721 ifp->if_flags &= ~IFF_RUNNING;
722 } else if ((ifp->if_flags & IFF_UP) != 0 &&
723 (ifp->if_flags & IFF_RUNNING) == 0) {
724 /*
725 * If interface is marked up and it is stopped, then
726 * start it.
727 */
728 bmac_init(sc);
729 } else {
730 /*
731 * Reset the interface to pick up changes in any other
732 * flags that affect hardware registers.
733 */
734 /*bmac_stop(sc);*/
735 bmac_init(sc);
736 }
737 #ifdef BMAC_DEBUG
738 if (ifp->if_flags & IFF_DEBUG)
739 sc->sc_debug = 1;
740 else
741 sc->sc_debug = 0;
742 #endif
743 break;
744
745 case SIOCADDMULTI:
746 case SIOCDELMULTI:
747 error = (cmd == SIOCADDMULTI) ?
748 ether_addmulti(ifr, &sc->sc_ethercom) :
749 ether_delmulti(ifr, &sc->sc_ethercom);
750
751 if (error == ENETRESET) {
752 /*
753 * Multicast list has changed; set the hardware filter
754 * accordingly.
755 */
756 bmac_init(sc);
757 bmac_setladrf(sc);
758 error = 0;
759 }
760 break;
761
762 case SIOCGIFMEDIA:
763 case SIOCSIFMEDIA:
764 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
765 break;
766
767 default:
768 error = EINVAL;
769 }
770
771 splx(s);
772 return error;
773 }
774
775 int
776 bmac_mediachange(ifp)
777 struct ifnet *ifp;
778 {
779 return EINVAL;
780 }
781
782 void
783 bmac_mediastatus(ifp, ifmr)
784 struct ifnet *ifp;
785 struct ifmediareq *ifmr;
786 {
787 if ((ifp->if_flags & IFF_UP) == 0)
788 return;
789
790 ifmr->ifm_status = IFM_AVALID;
791 ifmr->ifm_status |= IFM_ACTIVE;
792 }
793
794 #define MC_POLY_BE 0x04c11db7UL /* mcast crc, big endian */
795 #define MC_POLY_LE 0xedb88320UL /* mcast crc, little endian */
796
797 /*
798 * Set up the logical address filter.
799 */
800 void
801 bmac_setladrf(sc)
802 struct bmac_softc *sc;
803 {
804 struct ifnet *ifp = &sc->sc_if;
805 struct ether_multi *enm;
806 struct ether_multistep step;
807 int i, j;
808 u_int32_t crc;
809 u_int16_t hash[4];
810 u_int8_t octet;
811
812 /*
813 * Set up multicast address filter by passing all multicast addresses
814 * through a crc generator, and then using the high order 6 bits as an
815 * index into the 64 bit logical address filter. The high order bit
816 * selects the word, while the rest of the bits select the bit within
817 * the word.
818 */
819
820 if (ifp->if_flags & IFF_ALLMULTI)
821 goto allmulti;
822
823 if (ifp->if_flags & IFF_PROMISC) {
824 bmac_set_bits(sc, RXCFG, RxPromiscEnable);
825 goto allmulti;
826 }
827
828 hash[3] = hash[2] = hash[1] = hash[0] = 0;
829 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
830 while (enm != NULL) {
831 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
832 /*
833 * We must listen to a range of multicast addresses.
834 * For now, just accept all multicasts, rather than
835 * trying to set only those filter bits needed to match
836 * the range. (At this time, the only use of address
837 * ranges is for IP multicast routing, for which the
838 * range is big enough to require all bits set.)
839 */
840 goto allmulti;
841 }
842
843 crc = 0xffffffff;
844 for (i = 0; i < ETHER_ADDR_LEN; i++) {
845 octet = enm->enm_addrlo[i];
846
847 for (j = 0; j < 8; j++) {
848 if ((crc & 1) ^ (octet & 1)) {
849 crc >>= 1;
850 crc ^= MC_POLY_LE;
851 }
852 else
853 crc >>= 1;
854 octet >>= 1;
855 }
856 }
857
858 /* Just want the 6 most significant bits. */
859 crc >>= 26;
860
861 /* Set the corresponding bit in the filter. */
862 hash[crc >> 4] |= 1 << (crc & 0xf);
863
864 ETHER_NEXT_MULTI(step, enm);
865 }
866 bmac_write_reg(sc, HASH3, hash[3]);
867 bmac_write_reg(sc, HASH2, hash[2]);
868 bmac_write_reg(sc, HASH1, hash[1]);
869 bmac_write_reg(sc, HASH0, hash[0]);
870 ifp->if_flags &= ~IFF_ALLMULTI;
871 return;
872
873 allmulti:
874 ifp->if_flags |= IFF_ALLMULTI;
875 bmac_write_reg(sc, HASH3, 0xffff);
876 bmac_write_reg(sc, HASH2, 0xffff);
877 bmac_write_reg(sc, HASH1, 0xffff);
878 bmac_write_reg(sc, HASH0, 0xffff);
879 }
880