if_bce.c revision 1.2 1 /* $NetBSD: if_bce.c,v 1.2 2003/09/28 01:03:07 mrg Exp $ */
2
3 /*
4 * Copyright (c) 2003 Clifford Wright. 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,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Broadcom BCM440x 10/100 ethernet (broadcom.com)
32 * SiliconBackplane is technology from Sonics, Inc.(sonicsinc.com)
33 *
34 * Cliff Wright cliff (at) snipe444.org
35 */
36
37 #include "bpfilter.h"
38 #include "vlan.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/callout.h>
43 #include <sys/sockio.h>
44 #include <sys/mbuf.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48 #include <sys/socket.h>
49
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_media.h>
53 #include <net/if_ether.h>
54
55 #if NBPFILTER > 0
56 #include <net/bpf.h>
57 #endif
58
59 #include <dev/pci/pcireg.h>
60 #include <dev/pci/pcivar.h>
61 #include <dev/pci/pcidevs.h>
62
63 #include <dev/mii/mii.h>
64 #include <dev/mii/miivar.h>
65 #include <dev/mii/miidevs.h>
66 #include <dev/mii/brgphyreg.h>
67
68 #include <dev/pci/if_bcereg.h>
69
70 #include <uvm/uvm_extern.h>
71
72 /* transmit buffer max frags allowed */
73 #define BCE_NTXFRAGS 16
74
75 /* ring descriptor */
76 struct bce_dma_slot {
77 u_int32_t ctrl;
78 u_int32_t addr;
79 };
80 #define CTRL_BC_MASK 0x1fff /* buffer byte count */
81 #define CTRL_EOT 0x10000000 /* end of descriptor table */
82 #define CTRL_IOC 0x20000000 /* interrupt on completion */
83 #define CTRL_EOF 0x40000000 /* end of frame */
84 #define CTRL_SOF 0x80000000 /* start of frame */
85
86 /* Packet status is returned in a pre-packet header */
87 struct rx_pph {
88 u_int16_t len;
89 u_int16_t flags;
90 u_int16_t pad[12];
91 };
92
93 /* packet status flags bits */
94 #define RXF_NO 0x8 /* odd number of nibbles */
95 #define RXF_RXER 0x4 /* receive symbol error */
96 #define RXF_CRC 0x2 /* crc error */
97 #define RXF_OV 0x1 /* fifo overflow */
98
99 /* number of descriptors used in a ring */
100 #define BCE_NRXDESC 128
101 #define BCE_NTXDESC 128
102
103 /*
104 * Mbuf pointers. We need these to keep track of the virtual addresses
105 * of our mbuf chains since we can only convert from physical to virtual,
106 * not the other way around.
107 */
108 struct bce_chain_data {
109 struct mbuf *bce_tx_chain[BCE_NTXDESC];
110 struct mbuf *bce_rx_chain[BCE_NRXDESC];
111 bus_dmamap_t bce_tx_map[BCE_NTXDESC];
112 bus_dmamap_t bce_rx_map[BCE_NRXDESC];
113 };
114
115 #define BCE_TIMEOUT 100 /* # 10us for mii read/write */
116
117 struct bce_softc {
118 struct device bce_dev;
119 bus_space_tag_t bce_btag;
120 bus_space_handle_t bce_bhandle;
121 bus_dma_tag_t bce_dmatag;
122 struct ethercom ethercom; /* interface info */
123 void *bce_intrhand;
124 struct pci_attach_args bce_pa;
125 struct mii_data bce_mii;
126 u_int32_t bce_phy; /* eeprom indicated phy */
127 struct ifmedia bce_ifmedia; /* media info *//* Check */
128 u_int8_t enaddr[ETHER_ADDR_LEN];
129 struct bce_dma_slot *bce_rx_ring; /* receive ring */
130 struct bce_dma_slot *bce_tx_ring; /* transmit ring */
131 struct bce_chain_data bce_cdata; /* mbufs */
132 bus_dmamap_t bce_ring_map;
133 u_int32_t bce_rxin; /* last rx descriptor seen */
134 u_int32_t bce_txin; /* last tx descriptor seen */
135 int bce_txsfree; /* no. tx slots available */
136 int bce_txsnext; /* next available tx slot */
137 struct callout bce_timeout;
138 };
139
140 /* for ring descriptors */
141 #define BCE_RXBUF_LEN (MCLBYTES - 4)
142 #define BCE_INIT_RXDESC(sc, x) \
143 do { \
144 struct bce_dma_slot *__bced = &sc->bce_rx_ring[x]; \
145 \
146 *mtod(sc->bce_cdata.bce_rx_chain[x], u_int32_t *) = 0; \
147 __bced->addr = \
148 htole32(sc->bce_cdata.bce_rx_map[x]->dm_segs[0].ds_addr \
149 + 0x40000000); \
150 if (x != (BCE_NRXDESC - 1)) \
151 __bced->ctrl = htole32(BCE_RXBUF_LEN); \
152 else \
153 __bced->ctrl = htole32(BCE_RXBUF_LEN | CTRL_EOT); \
154 bus_dmamap_sync(sc->bce_dmatag, sc->bce_ring_map, \
155 sizeof(struct bce_dma_slot) * x, \
156 sizeof(struct bce_dma_slot), \
157 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \
158 } while (/* CONSTCOND */ 0)
159
160 static int bce_probe(struct device *, struct cfdata *, void *);
161 static void bce_attach(struct device *, struct device *, void *);
162 static int bce_ioctl(struct ifnet *, u_long, caddr_t);
163 static void bce_start(struct ifnet *);
164 static void bce_watchdog(struct ifnet *);
165 static int bce_intr(void *);
166 static void bce_rxintr(struct bce_softc *);
167 static void bce_txintr(struct bce_softc *);
168 static int bce_init(struct ifnet *);
169 static void bce_add_mac(struct bce_softc *, u_int8_t *, unsigned long);
170 static int bce_add_rxbuf(struct bce_softc *, int);
171 static void bce_rxdrain(struct bce_softc *);
172 static void bce_stop(struct ifnet *, int);
173 static void bce_reset(struct bce_softc *);
174 static void bce_set_filter(struct ifnet *);
175 static int bce_mii_read(struct device *, int, int);
176 static void bce_mii_write(struct device *, int, int, int);
177 static void bce_statchg(struct device *);
178 static int bce_mediachange(struct ifnet *);
179 static void bce_mediastatus(struct ifnet *, struct ifmediareq *);
180 static void bce_tick(void *);
181
182 #define BCE_DEBUG
183 #ifdef BCE_DEBUG
184 #define DPRINTF(x) do { \
185 if (bcedebug) \
186 printf x; \
187 } while (/* CONSTCOND */ 0)
188 #define DPRINTFN(n,x) do { \
189 if (bcedebug >= (n)) \
190 printf x; \
191 } while (/* CONSTCOND */ 0)
192 int bcedebug = 0;
193 #else
194 #define DPRINTF(x)
195 #define DPRINTFN(n,x)
196 #endif
197
198 #ifdef OLDNETBSD
199 struct cfattach bce_ca = {
200 sizeof(struct bce_softc), bce_probe, bce_attach
201 };
202 #else
203 CFATTACH_DECL(bce, sizeof(struct bce_softc),
204 bce_probe, bce_attach, NULL, NULL);
205 #endif
206
207
208 static const struct bce_product {
209 pci_vendor_id_t bp_vendor;
210 pci_product_id_t bp_product;
211 const char *bp_name;
212 } bce_products[] = {
213 {
214 PCI_VENDOR_BROADCOM,
215 PCI_PRODUCT_BROADCOM_BCM4401,
216 "Broadcom BCM4401 10/100 Ethernet"
217 },
218 {
219 0,
220 0,
221 NULL
222 },
223 };
224
225 static const struct bce_product *
226 bce_lookup(const struct pci_attach_args * pa)
227 {
228 const struct bce_product *bp;
229
230 for (bp = bce_products; bp->bp_name != NULL; bp++) {
231 if (PCI_VENDOR(pa->pa_id) == bp->bp_vendor &&
232 PCI_PRODUCT(pa->pa_id) == bp->bp_product)
233 return (bp);
234 }
235
236 return (NULL);
237 }
238
239 /*
240 * Probe for a Broadcom chip. Check the PCI vendor and device IDs
241 * against drivers product list, and return its name if a match is found.
242 */
243 int
244 bce_probe(parent, match, aux)
245 struct device *parent;
246 struct cfdata *match;
247 void *aux;
248 {
249 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
250
251 if (bce_lookup(pa) != NULL)
252 return (1);
253
254 return (0);
255 }
256
257 void
258 bce_attach(parent, self, aux)
259 struct device *parent, *self;
260 void *aux;
261 {
262 struct bce_softc *sc = (struct bce_softc *) self;
263 struct pci_attach_args *pa = aux;
264 const struct bce_product *bp;
265 pci_chipset_tag_t pc = pa->pa_pc;
266 pci_intr_handle_t ih;
267 const char *intrstr = NULL;
268 caddr_t kva;
269 bus_dma_segment_t seg;
270 int rseg;
271 u_int32_t command;
272 struct ifnet *ifp;
273 pcireg_t memtype;
274 bus_addr_t memaddr;
275 bus_size_t memsize;
276 int pmreg;
277 pcireg_t pmode;
278 int error;
279 int i;
280
281 bp = bce_lookup(pa);
282 KASSERT(bp != NULL);
283
284 sc->bce_pa = *pa;
285 sc->bce_dmatag = pa->pa_dmat;
286
287 printf(": %s\n", bp->bp_name);
288 /*
289 * following is for new aprint_naive(": Ethernet controller\n");
290 * aprint_normal(": %s\n", bp->bp_name);
291 */
292
293 /*
294 * Map control/status registers.
295 */
296 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
297 command |= PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE;
298 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
299 command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
300
301 if (!(command & PCI_COMMAND_MEM_ENABLE)) {
302 printf("%s: failed to enable memory mapping!\n",
303 /*
304 * following line for new aprint_error("%s: failed to enable
305 * memory mapping!\n",
306 */
307 sc->bce_dev.dv_xname);
308 return;
309 }
310 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, BCE_PCI_BAR0);
311 switch (memtype) {
312 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
313 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
314 if (pci_mapreg_map(pa, BCE_PCI_BAR0, memtype, 0, &sc->bce_btag,
315 &sc->bce_bhandle, &memaddr, &memsize) == 0)
316 break;
317 default:
318 printf("%s: unable to find mem space\n",
319 /*
320 * following for new aprint_error("%s: unable to find mem
321 * space\n",
322 */
323 sc->bce_dev.dv_xname);
324 return;
325 }
326
327 /* Get it out of power save mode if needed. */
328 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
329 pmode = pci_conf_read(pc, pa->pa_tag, pmreg + 4) & 0x3;
330 if (pmode == 3) {
331 /*
332 * The card has lost all configuration data in
333 * this state, so punt.
334 */
335 printf("%s: unable to wake up from power state D3\n",
336 sc->bce_dev.dv_xname);
337 return;
338 }
339 if (pmode != 0) {
340 printf("%s: waking up from power state D%d\n",
341 sc->bce_dev.dv_xname, pmode);
342 pci_conf_write(pc, pa->pa_tag, pmreg + 4, 0);
343 }
344 }
345 if (pci_intr_map(pa, &ih)) {
346 printf("%s: couldn't map interrupt\n",
347 /*
348 * following for new aprint_error("%s: couldn't map
349 * interrupt\n",
350 */
351 sc->bce_dev.dv_xname);
352 return;
353 }
354 intrstr = pci_intr_string(pc, ih);
355
356 sc->bce_intrhand = pci_intr_establish(pc, ih, IPL_NET, bce_intr, sc);
357
358 if (sc->bce_intrhand == NULL) {
359 printf("%s: couldn't establish interrupt",
360 /*
361 * following for new aprint_error("%s: couldn't establish
362 * interrupt",
363 */
364 sc->bce_dev.dv_xname);
365 if (intrstr != NULL)
366 printf(" at %s", intrstr);
367 /*
368 * following for new aprint_normal(" at %s", intrstr);
369 */
370 printf("\n");
371 /*
372 * following for new aprint_normal("\n");
373 */
374 return;
375 }
376 printf("%s: interrupting at %s\n",
377 /*
378 * following for new aprint_normal("%s: interrupting at %s\n",
379 */
380 sc->bce_dev.dv_xname, intrstr);
381
382 /* reset the chip */
383 bce_reset(sc);
384
385 /*
386 * Allocate DMA-safe memory for ring descriptors.
387 * The receive, and transmit rings can not share the same
388 * 4k space, however both are allocated at once here.
389 */
390 /*
391 * XXX PAGE_SIZE is wasteful; we only need 1KB + 1KB, but
392 * due to the limition above.
393 */
394 if ((error = bus_dmamem_alloc(sc->bce_dmatag,
395 2 * PAGE_SIZE, PAGE_SIZE, 2 * PAGE_SIZE,
396 &seg, 1, &rseg, BUS_DMA_NOWAIT))) {
397 printf("%s: unable to alloc space for ring descriptors, "
398 "error = %d\n", sc->bce_dev.dv_xname, error);
399 return;
400 }
401 /* map ring space to kernel */
402 if ((error = bus_dmamem_map(sc->bce_dmatag, &seg, rseg,
403 2 * PAGE_SIZE, &kva, BUS_DMA_NOWAIT))) {
404 printf("%s: unable to map DMA buffers, error = %d\n",
405 sc->bce_dev.dv_xname, error);
406 bus_dmamem_free(sc->bce_dmatag, &seg, rseg);
407 return;
408 }
409 /* create a dma map for the ring */
410 if ((error = bus_dmamap_create(sc->bce_dmatag,
411 2 * PAGE_SIZE, 1, 2 * PAGE_SIZE, 0, BUS_DMA_NOWAIT,
412 &sc->bce_ring_map))) {
413 printf("%s: unable to create ring DMA map, error = %d\n",
414 sc->bce_dev.dv_xname, error);
415 bus_dmamem_unmap(sc->bce_dmatag, kva, 2 * PAGE_SIZE);
416 bus_dmamem_free(sc->bce_dmatag, &seg, rseg);
417 return;
418 }
419 /* connect the ring space to the dma map */
420 if (bus_dmamap_load(sc->bce_dmatag, sc->bce_ring_map, kva,
421 2 * PAGE_SIZE, NULL, BUS_DMA_NOWAIT)) {
422 bus_dmamap_destroy(sc->bce_dmatag, sc->bce_ring_map);
423 bus_dmamem_unmap(sc->bce_dmatag, kva, 2 * PAGE_SIZE);
424 bus_dmamem_free(sc->bce_dmatag, &seg, rseg);
425 return;
426 }
427 /* save the ring space in softc */
428 sc->bce_rx_ring = (struct bce_dma_slot *) kva;
429 sc->bce_tx_ring = (struct bce_dma_slot *) (kva + PAGE_SIZE);
430
431 /* Create the transmit buffer DMA maps. */
432 for (i = 0; i < BCE_NTXDESC; i++) {
433 if ((error = bus_dmamap_create(sc->bce_dmatag, MCLBYTES,
434 BCE_NTXFRAGS, MCLBYTES, 0, 0, &sc->bce_cdata.bce_tx_map[i])) != 0) {
435 printf("%s: unable to create tx DMA map, error = %d\n",
436 sc->bce_dev.dv_xname, error);
437 }
438 sc->bce_cdata.bce_tx_chain[i] = NULL;
439 }
440
441 /* Create the receive buffer DMA maps. */
442 for (i = 0; i < BCE_NRXDESC; i++) {
443 if ((error = bus_dmamap_create(sc->bce_dmatag, MCLBYTES, 1,
444 MCLBYTES, 0, 0, &sc->bce_cdata.bce_rx_map[i])) != 0) {
445 printf("%s: unable to create rx DMA map, error = %d\n",
446 sc->bce_dev.dv_xname, error);
447 }
448 sc->bce_cdata.bce_rx_chain[i] = NULL;
449 }
450
451 /* Set up ifnet structure */
452 ifp = &sc->ethercom.ec_if;
453 strcpy(ifp->if_xname, sc->bce_dev.dv_xname);
454 ifp->if_softc = sc;
455 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
456 ifp->if_ioctl = bce_ioctl;
457 ifp->if_start = bce_start;
458 ifp->if_watchdog = bce_watchdog;
459 ifp->if_init = bce_init;
460 ifp->if_stop = bce_stop;
461 IFQ_SET_READY(&ifp->if_snd);
462
463 /* Initialize our media structures and probe the MII. */
464
465 sc->bce_mii.mii_ifp = ifp;
466 sc->bce_mii.mii_readreg = bce_mii_read;
467 sc->bce_mii.mii_writereg = bce_mii_write;
468 sc->bce_mii.mii_statchg = bce_statchg;
469 ifmedia_init(&sc->bce_mii.mii_media, 0, bce_mediachange,
470 bce_mediastatus);
471 mii_attach(&sc->bce_dev, &sc->bce_mii, 0xffffffff, MII_PHY_ANY,
472 MII_OFFSET_ANY, 0);
473 if (LIST_FIRST(&sc->bce_mii.mii_phys) == NULL) {
474 ifmedia_add(&sc->bce_mii.mii_media, IFM_ETHER | IFM_NONE, 0, NULL);
475 ifmedia_set(&sc->bce_mii.mii_media, IFM_ETHER | IFM_NONE);
476 } else
477 ifmedia_set(&sc->bce_mii.mii_media, IFM_ETHER | IFM_AUTO);
478 /* get the phy */
479 sc->bce_phy =
480 bus_space_read_1(sc->bce_btag, sc->bce_bhandle, 4096 + 90) & 0x1f; /* MAGIC */
481 /*
482 * Enable activity led.
483 * XXX This should be in a phy driver, but not currently.
484 */
485 bce_mii_write((struct device *) sc, 1, 26, /* MAGIC */
486 bce_mii_read((struct device *) sc, 1, 26) & 0x7fff); /* MAGIC */
487 /* enable traffic meter led mode */
488 bce_mii_write((struct device *) sc, 1, 26, /* MAGIC */
489 bce_mii_read((struct device *) sc, 1, 27) | (1 << 6)); /* MAGIC */
490
491
492 /* Attach the interface */
493 if_attach(ifp);
494 sc->enaddr[0] =
495 bus_space_read_1(sc->bce_btag, sc->bce_bhandle, 4096 + 79); /* MAGIC */
496 sc->enaddr[1] =
497 bus_space_read_1(sc->bce_btag, sc->bce_bhandle, 4096 + 78); /* MAGIC */
498 sc->enaddr[2] =
499 bus_space_read_1(sc->bce_btag, sc->bce_bhandle, 4096 + 81); /* MAGIC */
500 sc->enaddr[3] =
501 bus_space_read_1(sc->bce_btag, sc->bce_bhandle, 4096 + 80); /* MAGIC */
502 sc->enaddr[4] =
503 bus_space_read_1(sc->bce_btag, sc->bce_bhandle, 4096 + 83); /* MAGIC */
504 sc->enaddr[5] =
505 bus_space_read_1(sc->bce_btag, sc->bce_bhandle, 4096 + 82); /* MAGIC */
506 printf("%s: Ethernet address %s\n", sc->bce_dev.dv_xname,
507 ether_sprintf(sc->enaddr));
508 ether_ifattach(ifp, sc->enaddr);
509 callout_init(&sc->bce_timeout);
510 }
511
512 /* handle media, and ethernet requests */
513 static int
514 bce_ioctl(ifp, cmd, data)
515 struct ifnet *ifp;
516 u_long cmd;
517 caddr_t data;
518 {
519 struct bce_softc *sc = ifp->if_softc;
520 struct ifreq *ifr = (struct ifreq *) data;
521 int s, error;
522
523 s = splnet();
524 switch (cmd) {
525 case SIOCSIFMEDIA:
526 case SIOCGIFMEDIA:
527 error = ifmedia_ioctl(ifp, ifr, &sc->bce_mii.mii_media, cmd);
528 break;
529 default:
530 error = ether_ioctl(ifp, cmd, data);
531 if (error == ENETRESET) {
532 /* change multicast list */
533 error = 0;
534 }
535 break;
536 }
537
538 /* Try to get more packets going. */
539 bce_start(ifp);
540
541 splx(s);
542 return error;
543 }
544
545 /* Start packet transmission on the interface. */
546 static void
547 bce_start(ifp)
548 struct ifnet *ifp;
549 {
550 struct bce_softc *sc = ifp->if_softc;
551 struct mbuf *m0;
552 bus_dmamap_t dmamap;
553 int txstart;
554 int txsfree;
555 int newpkts = 0;
556 int error;
557
558 /*
559 * do not start another if currently transmitting, and more
560 * descriptors(tx slots) are needed for next packet.
561 */
562 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
563 return;
564
565 /* determine number of descriptors available */
566 if (sc->bce_txsnext >= sc->bce_txin)
567 txsfree = BCE_NTXDESC - 1 + sc->bce_txin - sc->bce_txsnext;
568 else
569 txsfree = sc->bce_txin - sc->bce_txsnext - 1;
570
571 /*
572 * Loop through the send queue, setting up transmit descriptors
573 * until we drain the queue, or use up all available transmit
574 * descriptors.
575 */
576 while (txsfree > 0) {
577 int seg;
578
579 /* Grab a packet off the queue. */
580 IFQ_POLL(&ifp->if_snd, m0);
581 if (m0 == NULL)
582 break;
583
584 /* get the transmit slot dma map */
585 dmamap = sc->bce_cdata.bce_tx_map[sc->bce_txsnext];
586
587 /*
588 * Load the DMA map. If this fails, the packet either
589 * didn't fit in the alloted number of segments, or we
590 * were short on resources. If the packet will not fit,
591 * it will be dropped. If short on resources, it will
592 * be tried again later.
593 */
594 error = bus_dmamap_load_mbuf(sc->bce_dmatag, dmamap, m0,
595 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
596 if (error == EFBIG) {
597 printf("%s: Tx packet consumes too many DMA segments, "
598 "dropping...\n", sc->bce_dev.dv_xname);
599 IFQ_DEQUEUE(&ifp->if_snd, m0);
600 m_freem(m0);
601 ifp->if_oerrors++;
602 continue;
603 } else if (error) {
604 /* short on resources, come back later */
605 printf("%s: unable to load Tx buffer, error = %d\n",
606 sc->bce_dev.dv_xname, error);
607 break;
608 }
609 /* If not enough descriptors available, try again later */
610 if (dmamap->dm_nsegs > txsfree) {
611 ifp->if_flags |= IFF_OACTIVE;
612 bus_dmamap_unload(sc->bce_dmatag, dmamap);
613 break;
614 }
615 /* WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET. */
616
617 /* So take it off the queue */
618 IFQ_DEQUEUE(&ifp->if_snd, m0);
619
620 /* save the pointer so it can be freed later */
621 sc->bce_cdata.bce_tx_chain[sc->bce_txsnext] = m0;
622
623 /* Sync the data DMA map. */
624 bus_dmamap_sync(sc->bce_dmatag, dmamap, 0, dmamap->dm_mapsize,
625 BUS_DMASYNC_PREWRITE);
626
627 /* Initialize the transmit descriptor(s). */
628 txstart = sc->bce_txsnext;
629 for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
630 u_int32_t ctrl;
631
632 ctrl = dmamap->dm_segs[seg].ds_len & CTRL_BC_MASK;
633 if (seg == 0)
634 ctrl |= CTRL_SOF;
635 if (seg == dmamap->dm_nsegs - 1)
636 ctrl |= CTRL_EOF;
637 if (sc->bce_txsnext == BCE_NTXDESC - 1)
638 ctrl |= CTRL_EOT;
639 ctrl |= CTRL_IOC;
640 sc->bce_tx_ring[sc->bce_txsnext].ctrl = htole32(ctrl);
641 sc->bce_tx_ring[sc->bce_txsnext].addr =
642 htole32(dmamap->dm_segs[seg].ds_addr + 0x40000000); /* MAGIC */
643 if (sc->bce_txsnext + 1 > BCE_NTXDESC - 1)
644 sc->bce_txsnext = 0;
645 else
646 sc->bce_txsnext++;
647 txsfree--;
648 }
649 /* sync descriptors being used */
650 bus_dmamap_sync(sc->bce_dmatag, sc->bce_ring_map,
651 sizeof(struct bce_dma_slot) * txstart + PAGE_SIZE,
652 sizeof(struct bce_dma_slot) * dmamap->dm_nsegs,
653 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
654
655 /* Give the packet to the chip. */
656 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_DPTR,
657 sc->bce_txsnext * sizeof(struct bce_dma_slot));
658
659 newpkts++;
660
661 #if NBPFILTER > 0
662 /* Pass the packet to any BPF listeners. */
663 if (ifp->if_bpf)
664 bpf_mtap(ifp->if_bpf, m0);
665 #endif /* NBPFILTER > 0 */
666 }
667 if (txsfree == 0) {
668 /* No more slots left; notify upper layer. */
669 ifp->if_flags |= IFF_OACTIVE;
670 }
671 if (newpkts) {
672 /* Set a watchdog timer in case the chip flakes out. */
673 ifp->if_timer = 5;
674 }
675 }
676
677 /* Watchdog timer handler. */
678 static void
679 bce_watchdog(ifp)
680 struct ifnet *ifp;
681 {
682 struct bce_softc *sc = ifp->if_softc;
683
684 printf("%s: device timeout\n", sc->bce_dev.dv_xname);
685 ifp->if_oerrors++;
686
687 (void) bce_init(ifp);
688
689 /* Try to get more packets going. */
690 bce_start(ifp);
691 }
692
693 int
694 bce_intr(xsc)
695 void *xsc;
696 {
697 struct bce_softc *sc;
698 struct ifnet *ifp;
699 u_int32_t intstatus;
700 u_int32_t intmask;
701 int wantinit;
702 int handled = 0;
703
704 sc = xsc;
705 ifp = &sc->ethercom.ec_if;
706
707
708 for (wantinit = 0; wantinit == 0;) {
709 intstatus = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
710 BCE_INT_STS);
711 intmask = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
712 BCE_INT_MASK);
713
714 /* ignore if not ours, or unsolicited interrupts */
715 intstatus &= intmask;
716 if (intstatus == 0)
717 break;
718
719 handled = 1;
720
721 /* Ack interrupt */
722 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_INT_STS,
723 intstatus);
724
725 /* Receive interrupts. */
726 if (intstatus & I_RI)
727 bce_rxintr(sc);
728 /* Transmit interrupts. */
729 if (intstatus & I_XI)
730 bce_txintr(sc);
731 /* Error interrupts */
732 if (intstatus & ~(I_RI | I_XI)) {
733 if (intstatus & I_XU)
734 printf("%s: transmit fifo underflow\n",
735 sc->bce_dev.dv_xname);
736 if (intstatus & I_RO) {
737 printf("%s: receive fifo overflow\n",
738 sc->bce_dev.dv_xname);
739 ifp->if_ierrors++;
740 }
741 if (intstatus & I_RU)
742 printf("%s: receive descriptor underflow\n",
743 sc->bce_dev.dv_xname);
744 if (intstatus & I_DE)
745 printf("%s: descriptor protocol error\n",
746 sc->bce_dev.dv_xname);
747 if (intstatus & I_PD)
748 printf("%s: data error\n",
749 sc->bce_dev.dv_xname);
750 if (intstatus & I_PC)
751 printf("%s: descriptor error\n",
752 sc->bce_dev.dv_xname);
753 if (intstatus & I_TO)
754 printf("%s: general purpose timeout\n",
755 sc->bce_dev.dv_xname);
756 wantinit = 1;
757 }
758 }
759
760 if (handled) {
761 if (wantinit)
762 bce_init(ifp);
763 /* Try to get more packets going. */
764 bce_start(ifp);
765 }
766 return (handled);
767 }
768
769 /* Receive interrupt handler */
770 void
771 bce_rxintr(sc)
772 struct bce_softc *sc;
773 {
774 struct ifnet *ifp = &sc->ethercom.ec_if;
775 struct rx_pph *pph;
776 struct mbuf *m;
777 int curr;
778 int len;
779 int i;
780
781 /* get pointer to active receive slot */
782 curr = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXSTATUS)
783 & RS_CD_MASK;
784 curr = curr / sizeof(struct bce_dma_slot);
785 if (curr >= BCE_NRXDESC)
786 curr = BCE_NRXDESC - 1;
787
788 /* process packets up to but not current packet being worked on */
789 for (i = sc->bce_rxin; i != curr;
790 i + 1 > BCE_NRXDESC - 1 ? i = 0 : i++) {
791 /* complete any post dma memory ops on packet */
792 bus_dmamap_sync(sc->bce_dmatag, sc->bce_cdata.bce_rx_map[i], 0,
793 sc->bce_cdata.bce_rx_map[i]->dm_mapsize,
794 BUS_DMASYNC_POSTREAD);
795
796 /*
797 * If the packet had an error, simply recycle the buffer,
798 * resetting the len, and flags.
799 */
800 pph = mtod(sc->bce_cdata.bce_rx_chain[i], struct rx_pph *);
801 if (pph->flags & (RXF_NO | RXF_RXER | RXF_CRC | RXF_OV)) {
802 ifp->if_ierrors++;
803 pph->len = 0;
804 pph->flags = 0;
805 continue;
806 }
807 /* receive the packet */
808 len = pph->len;
809 if (len == 0)
810 continue; /* no packet if empty */
811 pph->len = 0;
812 pph->flags = 0;
813 /* bump past pre header to packet */
814 sc->bce_cdata.bce_rx_chain[i]->m_data += 30; /* MAGIC */
815
816 /*
817 * If the packet is small enough to fit in a
818 * single header mbuf, allocate one and copy
819 * the data into it. This greatly reduces
820 * memory consumption when receiving lots
821 * of small packets.
822 *
823 * Otherwise, add a new buffer to the receive
824 * chain. If this fails, drop the packet and
825 * recycle the old buffer.
826 */
827 if (len <= (MHLEN - 2)) {
828 MGETHDR(m, M_DONTWAIT, MT_DATA);
829 if (m == NULL)
830 goto dropit;
831 m->m_data += 2;
832 memcpy(mtod(m, caddr_t),
833 mtod(sc->bce_cdata.bce_rx_chain[i], caddr_t), len);
834 sc->bce_cdata.bce_rx_chain[i]->m_data -= 30; /* MAGIC */
835 } else {
836 m = sc->bce_cdata.bce_rx_chain[i];
837 if (bce_add_rxbuf(sc, i) != 0) {
838 dropit:
839 ifp->if_ierrors++;
840 /* continue to use old buffer */
841 sc->bce_cdata.bce_rx_chain[i]->m_data -= 30;
842 bus_dmamap_sync(sc->bce_dmatag,
843 sc->bce_cdata.bce_rx_map[i], 0,
844 sc->bce_cdata.bce_rx_map[i]->dm_mapsize,
845 BUS_DMASYNC_PREREAD);
846 continue;
847 }
848 }
849
850 m->m_flags |= M_HASFCS;
851 m->m_pkthdr.rcvif = ifp;
852 m->m_pkthdr.len = m->m_len = len;
853 ifp->if_ipackets++;
854
855 #if NBPFILTER > 0
856 /*
857 * Pass this up to any BPF listeners, but only
858 * pass it up the stack if it's for us.
859 */
860 if (ifp->if_bpf)
861 bpf_mtap(ifp->if_bpf, m);
862 #endif /* NBPFILTER > 0 */
863
864 /* Pass it on. */
865 (*ifp->if_input) (ifp, m);
866
867 /* re-check current in case it changed */
868 curr = (bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
869 BCE_DMA_RXSTATUS) & RS_CD_MASK) /
870 sizeof(struct bce_dma_slot);
871 if (curr >= BCE_NRXDESC)
872 curr = BCE_NRXDESC - 1;
873 }
874 sc->bce_rxin = curr;
875 }
876
877 /* Transmit interrupt handler */
878 void
879 bce_txintr(sc)
880 struct bce_softc *sc;
881 {
882 struct ifnet *ifp = &sc->ethercom.ec_if;
883 int curr;
884 int i;
885
886 ifp->if_flags &= ~IFF_OACTIVE;
887
888 /*
889 * Go through the Tx list and free mbufs for those
890 * frames which have been transmitted.
891 */
892 curr = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXSTATUS) &
893 RS_CD_MASK;
894 curr = curr / sizeof(struct bce_dma_slot);
895 if (curr >= BCE_NTXDESC)
896 curr = BCE_NTXDESC - 1;
897 for (i = sc->bce_txin; i != curr;
898 i + 1 > BCE_NTXDESC - 1 ? i = 0 : i++) {
899 /* do any post dma memory ops on transmit data */
900 if (sc->bce_cdata.bce_tx_chain[i] == NULL)
901 continue;
902 bus_dmamap_sync(sc->bce_dmatag, sc->bce_cdata.bce_tx_map[i], 0,
903 sc->bce_cdata.bce_tx_map[i]->dm_mapsize,
904 BUS_DMASYNC_POSTWRITE);
905 bus_dmamap_unload(sc->bce_dmatag, sc->bce_cdata.bce_tx_map[i]);
906 m_freem(sc->bce_cdata.bce_tx_chain[i]);
907 sc->bce_cdata.bce_tx_chain[i] = NULL;
908 ifp->if_opackets++;
909 }
910 sc->bce_txin = curr;
911
912 /*
913 * If there are no more pending transmissions, cancel the watchdog
914 * timer
915 */
916 if (sc->bce_txsnext == sc->bce_txin)
917 ifp->if_timer = 0;
918 }
919
920 /* initialize the interface */
921 static int
922 bce_init(ifp)
923 struct ifnet *ifp;
924 {
925 struct bce_softc *sc = ifp->if_softc;
926 u_int32_t reg_win;
927 int error;
928 int i;
929
930 /* Cancel any pending I/O. */
931 bce_stop(ifp, 0);
932
933 /* enable pci inerrupts, bursts, and prefetch */
934
935 /* remap the pci registers to the Sonics config registers */
936
937 /* save the current map, so it can be restored */
938 reg_win = pci_conf_read(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag,
939 BCE_REG_WIN);
940
941 /* set register window to Sonics registers */
942 pci_conf_write(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag, BCE_REG_WIN,
943 BCE_SONICS_WIN);
944
945 /* enable SB to PCI interrupt */
946 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBINTVEC,
947 bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBINTVEC) |
948 SBIV_ENET0);
949
950 /* enable prefetch and bursts for sonics-to-pci translation 2 */
951 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SPCI_TR2,
952 bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SPCI_TR2) |
953 SBTOPCI_PREF | SBTOPCI_BURST);
954
955 /* restore to ethernet register space */
956 pci_conf_write(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag, BCE_REG_WIN,
957 reg_win);
958
959 /* Reset the chip to a known state. */
960 bce_reset(sc);
961
962 /* Initialize transmit descriptors */
963 memset(sc->bce_tx_ring, 0, BCE_NTXDESC * sizeof(struct bce_dma_slot));
964 sc->bce_txsnext = 0;
965 sc->bce_txin = 0;
966
967 /* enable crc32 generation */
968 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MACCTL,
969 bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_MACCTL) |
970 BCE_EMC_CG);
971
972 /* setup DMA interrupt control */
973 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMAI_CTL, 1 << 24); /* MAGIC */
974
975 /* setup packet filter */
976 bce_set_filter(ifp);
977
978 /* set max frame length, account for possible vlan tag */
979 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_MAX,
980 ETHER_MAX_LEN + 32);
981 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_MAX,
982 ETHER_MAX_LEN + 32);
983
984 /* set tx watermark */
985 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_WATER, 56);
986
987 /* enable transmit */
988 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXCTL, XC_XE);
989 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXADDR,
990 sc->bce_ring_map->dm_segs[0].ds_addr + PAGE_SIZE + 0x40000000); /* MAGIC */
991
992 /*
993 * Give the receive ring to the chip, and
994 * start the receive DMA engine.
995 */
996 sc->bce_rxin = 0;
997
998 /* clear the rx descriptor ring */
999 memset(sc->bce_rx_ring, 0, BCE_NRXDESC * sizeof(struct bce_dma_slot));
1000 /* enable receive */
1001 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXCTL,
1002 30 << 1 | 1);
1003 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXADDR,
1004 sc->bce_ring_map->dm_segs[0].ds_addr + 0x40000000); /* MAGIC */
1005
1006 /* Initalize receive descriptors */
1007 for (i = 0; i < BCE_NRXDESC; i++) {
1008 if (sc->bce_cdata.bce_rx_chain[i] == NULL) {
1009 if ((error = bce_add_rxbuf(sc, i)) != 0) {
1010 printf("%s: unable to allocate or map rx(%d) "
1011 "mbuf, error = %d\n", sc->bce_dev.dv_xname,
1012 i, error);
1013 bce_rxdrain(sc);
1014 return (error);
1015 }
1016 } else
1017 BCE_INIT_RXDESC(sc, i);
1018 }
1019
1020 /* Enable interrupts */
1021 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_INT_MASK,
1022 I_XI | I_RI | I_XU | I_RO | I_RU | I_DE | I_PD | I_PC | I_TO);
1023
1024 /* start the receive dma */
1025 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXDPTR,
1026 BCE_NRXDESC * sizeof(struct bce_dma_slot));
1027
1028 /* set media */
1029 mii_mediachg(&sc->bce_mii);
1030
1031 /* turn on the ethernet mac */
1032 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL,
1033 bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1034 BCE_ENET_CTL) | EC_EE);
1035
1036 /* start timer */
1037 callout_reset(&sc->bce_timeout, hz, bce_tick, sc);
1038
1039 /* mark as running, and no outputs active */
1040 ifp->if_flags |= IFF_RUNNING;
1041 ifp->if_flags &= ~IFF_OACTIVE;
1042
1043 return 0;
1044 }
1045
1046 /* add a mac address to packet filter */
1047 void
1048 bce_add_mac(sc, mac, idx)
1049 struct bce_softc *sc;
1050 u_int8_t *mac;
1051 unsigned long idx;
1052 {
1053 int i;
1054 u_int32_t rval;
1055
1056 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_LOW,
1057 mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5]);
1058 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_HI,
1059 mac[0] << 8 | mac[1] | 0x10000); /* MAGIC */
1060 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_CTL,
1061 idx << 16 | 8); /* MAGIC */
1062 /* wait for write to complete */
1063 for (i = 0; i < 100; i++) {
1064 rval = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1065 BCE_FILT_CTL);
1066 if (!(rval & 0x80000000)) /* MAGIC */
1067 break;
1068 delay(10);
1069 }
1070 if (i == 100) {
1071 printf("%s: timed out writting pkt filter ctl\n",
1072 sc->bce_dev.dv_xname);
1073 }
1074 }
1075
1076 /* Add a receive buffer to the indiciated descriptor. */
1077 static int
1078 bce_add_rxbuf(sc, idx)
1079 struct bce_softc *sc;
1080 int idx;
1081 {
1082 struct mbuf *m;
1083 int error;
1084
1085 MGETHDR(m, M_DONTWAIT, MT_DATA);
1086 if (m == NULL)
1087 return (ENOBUFS);
1088
1089 MCLGET(m, M_DONTWAIT);
1090 if ((m->m_flags & M_EXT) == 0) {
1091 m_freem(m);
1092 return (ENOBUFS);
1093 }
1094 if (sc->bce_cdata.bce_rx_chain[idx] != NULL)
1095 bus_dmamap_unload(sc->bce_dmatag,
1096 sc->bce_cdata.bce_rx_map[idx]);
1097
1098 sc->bce_cdata.bce_rx_chain[idx] = m;
1099
1100 error = bus_dmamap_load(sc->bce_dmatag, sc->bce_cdata.bce_rx_map[idx],
1101 m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
1102 BUS_DMA_READ | BUS_DMA_NOWAIT);
1103 if (error)
1104 return (error);
1105
1106 bus_dmamap_sync(sc->bce_dmatag, sc->bce_cdata.bce_rx_map[idx], 0,
1107 sc->bce_cdata.bce_rx_map[idx]->dm_mapsize, BUS_DMASYNC_PREREAD);
1108
1109 BCE_INIT_RXDESC(sc, idx);
1110
1111 return (0);
1112
1113 }
1114
1115 /* Drain the receive queue. */
1116 static void
1117 bce_rxdrain(sc)
1118 struct bce_softc *sc;
1119 {
1120 int i;
1121
1122 for (i = 0; i < BCE_NRXDESC; i++) {
1123 if (sc->bce_cdata.bce_rx_chain[i] != NULL) {
1124 bus_dmamap_unload(sc->bce_dmatag,
1125 sc->bce_cdata.bce_rx_map[i]);
1126 m_freem(sc->bce_cdata.bce_rx_chain[i]);
1127 sc->bce_cdata.bce_rx_chain[i] = NULL;
1128 }
1129 }
1130 }
1131
1132 /* Stop transmission on the interface */
1133 static void
1134 bce_stop(ifp, disable)
1135 struct ifnet *ifp;
1136 int disable;
1137 {
1138 struct bce_softc *sc = ifp->if_softc;
1139 int i;
1140 u_int32_t val;
1141
1142 /* Stop the 1 second timer */
1143 callout_stop(&sc->bce_timeout);
1144
1145 /* Down the MII. */
1146 mii_down(&sc->bce_mii);
1147
1148 /* Disable interrupts. */
1149 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_INT_MASK, 0);
1150 bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_INT_MASK);
1151
1152 /* Disable emac */
1153 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL, EC_ED);
1154 for (i = 0; i < 200; i++) {
1155 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1156 BCE_ENET_CTL);
1157 if (!(val & EC_ED))
1158 break;
1159 delay(10);
1160 }
1161
1162 /* Stop the DMA */
1163 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXCTL, 0);
1164 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXCTL, 0);
1165 delay(10);
1166
1167 /* Release any queued transmit buffers. */
1168 for (i = 0; i < BCE_NTXDESC; i++) {
1169 if (sc->bce_cdata.bce_tx_chain[i] != NULL) {
1170 bus_dmamap_unload(sc->bce_dmatag,
1171 sc->bce_cdata.bce_tx_map[i]);
1172 m_freem(sc->bce_cdata.bce_tx_chain[i]);
1173 sc->bce_cdata.bce_tx_chain[i] = NULL;
1174 }
1175 }
1176
1177 /* drain receive queue */
1178 if (disable)
1179 bce_rxdrain(sc);
1180
1181 /* Mark the interface down and cancel the watchdog timer. */
1182 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1183 ifp->if_timer = 0;
1184 }
1185
1186 /* reset the chip */
1187 static void
1188 bce_reset(sc)
1189 struct bce_softc *sc;
1190 {
1191 u_int32_t val;
1192 u_int32_t sbval;
1193 int i;
1194
1195 /* if SB core is up */
1196 sbval = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1197 BCE_SBTMSTATELOW);
1198 if ((sbval & (SBTML_RESET | SBTML_REJ | SBTML_CLK)) == SBTML_CLK) {
1199 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMAI_CTL,
1200 0);
1201
1202 /* disable emac */
1203 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL,
1204 EC_ED);
1205 for (i = 0; i < 200; i++) {
1206 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1207 BCE_ENET_CTL);
1208 if (!(val & EC_ED))
1209 break;
1210 delay(10);
1211 }
1212 if (i == 200)
1213 printf("%s: timed out disabling ethernet mac\n",
1214 sc->bce_dev.dv_xname);
1215
1216 /* reset the dma engines */
1217 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_TXCTL, 0);
1218 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_DMA_RXSTATUS);
1219 /* if error on receive, wait to go idle */
1220 if (val & 0xf0000) { /* MAGIC */
1221 for (i = 0; i < 100; i++) {
1222 val = bus_space_read_4(sc->bce_btag,
1223 sc->bce_bhandle, BCE_DMA_RXSTATUS);
1224 if (val & 0x2000) /* MAGIC */
1225 break;
1226 delay(10);
1227 }
1228 if (i == 100)
1229 printf("%s: receive dma did not go idle after"
1230 " error\n", sc->bce_dev.dv_xname);
1231 }
1232 bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
1233 BCE_DMA_RXSTATUS, 0);
1234
1235 /* reset ethernet mac */
1236 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL,
1237 EC_ES);
1238 for (i = 0; i < 200; i++) {
1239 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1240 BCE_ENET_CTL);
1241 if (!(val & EC_ES))
1242 break;
1243 delay(10);
1244 }
1245 if (i == 200)
1246 printf("%s: timed out restting ethernet mac\n",
1247 sc->bce_dev.dv_xname);
1248 } else {
1249 u_int32_t reg_win;
1250
1251 /* remap the pci registers to the Sonics config registers */
1252
1253 /* save the current map, so it can be restored */
1254 reg_win = pci_conf_read(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag,
1255 BCE_REG_WIN);
1256 /* set register window to Sonics registers */
1257 pci_conf_write(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag,
1258 BCE_REG_WIN, BCE_SONICS_WIN);
1259
1260 /* enable SB to PCI interrupt */
1261 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBINTVEC,
1262 bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1263 BCE_SBINTVEC) |
1264 SBIV_ENET0);
1265
1266 /* enable prefetch and bursts for sonics-to-pci translation 2 */
1267 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SPCI_TR2,
1268 bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1269 BCE_SPCI_TR2) |
1270 SBTOPCI_PREF | SBTOPCI_BURST);
1271
1272 /* restore to ethernet register space */
1273 pci_conf_write(sc->bce_pa.pa_pc, sc->bce_pa.pa_tag, BCE_REG_WIN,
1274 reg_win);
1275 }
1276
1277 /* disable SB core if not in reset */
1278 if (!(sbval & SBTML_RESET)) {
1279
1280 /* set the reject bit */
1281 bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
1282 BCE_SBTMSTATELOW, SBTML_REJ | SBTML_CLK);
1283 for (i = 0; i < 200; i++) {
1284 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1285 BCE_SBTMSTATELOW);
1286 if (val & SBTML_REJ)
1287 break;
1288 delay(1);
1289 }
1290 if (i == 200)
1291 printf("%s: while restting core, reject did not set\n",
1292 sc->bce_dev.dv_xname);
1293 /* wait until busy is clear */
1294 for (i = 0; i < 200; i++) {
1295 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1296 BCE_SBTMSTATEHI);
1297 if (!(val & 0x4))
1298 break;
1299 delay(1);
1300 }
1301 if (i == 200)
1302 printf("%s: while restting core, busy did not clear\n",
1303 sc->bce_dev.dv_xname);
1304 /* set reset and reject while enabling the clocks */
1305 bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
1306 BCE_SBTMSTATELOW,
1307 SBTML_FGC | SBTML_CLK | SBTML_REJ | SBTML_RESET);
1308 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1309 BCE_SBTMSTATELOW);
1310 delay(10);
1311 bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
1312 BCE_SBTMSTATELOW, SBTML_REJ | SBTML_RESET);
1313 delay(1);
1314 }
1315 /* enable clock */
1316 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW,
1317 SBTML_FGC | SBTML_CLK | SBTML_RESET);
1318 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW);
1319 delay(1);
1320
1321 /* clear any error bits that may be on */
1322 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATEHI);
1323 if (val & 1)
1324 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATEHI,
1325 0);
1326 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBIMSTATE);
1327 if (val & 0x60000) /* MAGIC */
1328 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBIMSTATE,
1329 val & ~0x60000); /* MAGIC */
1330
1331 /* clear reset and allow it to propagate throughout the core */
1332 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW,
1333 SBTML_FGC | SBTML_CLK);
1334 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW);
1335 delay(1);
1336
1337 /* leave clock enabled */
1338 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW,
1339 SBTML_CLK);
1340 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_SBTMSTATELOW);
1341 delay(1);
1342
1343 /* initialize MDC preamble, frequency */
1344 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_CTL, 0x8d); /* MAGIC */
1345
1346 /* enable phy, differs for internal, and external */
1347 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_DEVCTL);
1348 if (!(val & BCE_DC_IP)) {
1349 /* select external phy */
1350 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_ENET_CTL, EC_EP);
1351 } else if (val & BCE_DC_ER) { /* internal, clear reset bit if on */
1352 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_DEVCTL,
1353 val & ~BCE_DC_ER);
1354 delay(100);
1355 }
1356 }
1357
1358 /* Set up the receive filter. */
1359 void
1360 bce_set_filter(ifp)
1361 struct ifnet *ifp;
1362 {
1363 struct bce_softc *sc = ifp->if_softc;
1364
1365 if (ifp->if_flags & IFF_PROMISC) {
1366 ifp->if_flags |= IFF_ALLMULTI;
1367 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL,
1368 bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL)
1369 | ERC_PE);
1370 } else {
1371 ifp->if_flags &= ~IFF_ALLMULTI;
1372
1373 /* turn off promiscuous */
1374 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL,
1375 bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1376 BCE_RX_CTL) & ~ERC_PE);
1377
1378 /* enable/disable broadcast */
1379 if (ifp->if_flags & IFF_BROADCAST)
1380 bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
1381 BCE_RX_CTL, bus_space_read_4(sc->bce_btag,
1382 sc->bce_bhandle, BCE_RX_CTL) & ~ERC_DB);
1383 else
1384 bus_space_write_4(sc->bce_btag, sc->bce_bhandle,
1385 BCE_RX_CTL, bus_space_read_4(sc->bce_btag,
1386 sc->bce_bhandle, BCE_RX_CTL) | ERC_DB);
1387
1388 /* disable the filter */
1389 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_CTL,
1390 0);
1391
1392 /* add our own address */
1393 bce_add_mac(sc, sc->enaddr, 0);
1394
1395 /* for now accept all multicast */
1396 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL,
1397 bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_RX_CTL) |
1398 ERC_AM);
1399 ifp->if_flags |= IFF_ALLMULTI;
1400
1401 /* enable the filter */
1402 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_FILT_CTL,
1403 bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1404 BCE_FILT_CTL) | 1);
1405 }
1406 }
1407
1408 /* Read a PHY register on the MII. */
1409 int
1410 bce_mii_read(self, phy, reg)
1411 struct device *self;
1412 int phy, reg;
1413 {
1414 struct bce_softc *sc = (struct bce_softc *) self;
1415 int i;
1416 u_int32_t val;
1417
1418 /* clear mii_int */
1419 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_STS, BCE_MIINTR);
1420
1421 /* Read the PHY register */
1422 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_COMM,
1423 (MII_COMMAND_READ << 28) | (MII_COMMAND_START << 30) | /* MAGIC */
1424 (MII_COMMAND_ACK << 16) | BCE_MIPHY(phy) | BCE_MIREG(reg)); /* MAGIC */
1425
1426 for (i = 0; i < BCE_TIMEOUT; i++) {
1427 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_STS);
1428 if (val & BCE_MIINTR)
1429 break;
1430 delay(10);
1431 }
1432 val = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_COMM);
1433 if (i == BCE_TIMEOUT) {
1434 printf("%s: PHY read timed out reading phy %d, reg %d, val = "
1435 "0x%08x\n", sc->bce_dev.dv_xname, phy, reg, val);
1436 return (0);
1437 }
1438 return (val & BCE_MICOMM_DATA);
1439 }
1440
1441 /* Write a PHY register on the MII */
1442 void
1443 bce_mii_write(self, phy, reg, val)
1444 struct device *self;
1445 int phy, reg, val;
1446 {
1447 struct bce_softc *sc = (struct bce_softc *) self;
1448 int i;
1449 u_int32_t rval;
1450
1451 /* clear mii_int */
1452 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_STS,
1453 BCE_MIINTR);
1454
1455 /* Write the PHY register */
1456 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_COMM,
1457 (MII_COMMAND_WRITE << 28) | (MII_COMMAND_START << 30) | /* MAGIC */
1458 (MII_COMMAND_ACK << 16) | (val & BCE_MICOMM_DATA) | /* MAGIC */
1459 BCE_MIPHY(phy) | BCE_MIREG(reg));
1460
1461 /* wait for write to complete */
1462 for (i = 0; i < BCE_TIMEOUT; i++) {
1463 rval = bus_space_read_4(sc->bce_btag, sc->bce_bhandle,
1464 BCE_MI_STS);
1465 if (rval & BCE_MIINTR)
1466 break;
1467 delay(10);
1468 }
1469 rval = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_MI_COMM);
1470 if (i == BCE_TIMEOUT) {
1471 printf("%s: PHY timed out writting phy %d, reg %d, val "
1472 "= 0x%08x\n", sc->bce_dev.dv_xname, phy, reg, val);
1473 }
1474 }
1475
1476 /* sync hardware duplex mode to software state */
1477 void
1478 bce_statchg(self)
1479 struct device *self;
1480 {
1481 struct bce_softc *sc = (struct bce_softc *) self;
1482 u_int32_t reg;
1483
1484 /* if needed, change register to match duplex mode */
1485 reg = bus_space_read_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_CTL);
1486 if (sc->bce_mii.mii_media_active & IFM_FDX && !(reg & EXC_FD))
1487 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_CTL,
1488 reg | EXC_FD);
1489 else if (!(sc->bce_mii.mii_media_active & IFM_FDX) && reg & EXC_FD)
1490 bus_space_write_4(sc->bce_btag, sc->bce_bhandle, BCE_TX_CTL,
1491 reg & ~EXC_FD);
1492
1493 /*
1494 * Enable activity led.
1495 * XXX This should be in a phy driver, but not currently.
1496 */
1497 bce_mii_write((struct device *) sc, 1, 26, /* MAGIC */
1498 bce_mii_read((struct device *) sc, 1, 26) & 0x7fff); /* MAGIC */
1499 /* enable traffic meter led mode */
1500 bce_mii_write((struct device *) sc, 1, 26, /* MAGIC */
1501 bce_mii_read((struct device *) sc, 1, 27) | (1 << 6)); /* MAGIC */
1502 }
1503
1504 /* Set hardware to newly-selected media */
1505 int
1506 bce_mediachange(ifp)
1507 struct ifnet *ifp;
1508 {
1509 struct bce_softc *sc = ifp->if_softc;
1510
1511 if (ifp->if_flags & IFF_UP)
1512 mii_mediachg(&sc->bce_mii);
1513 return (0);
1514 }
1515
1516 /* Get the current interface media status */
1517 static void
1518 bce_mediastatus(ifp, ifmr)
1519 struct ifnet *ifp;
1520 struct ifmediareq *ifmr;
1521 {
1522 struct bce_softc *sc = ifp->if_softc;
1523
1524 mii_pollstat(&sc->bce_mii);
1525 ifmr->ifm_active = sc->bce_mii.mii_media_active;
1526 ifmr->ifm_status = sc->bce_mii.mii_media_status;
1527 }
1528
1529 /* One second timer, checks link status */
1530 static void
1531 bce_tick(v)
1532 void *v;
1533 {
1534 struct bce_softc *sc = v;
1535
1536 /* Tick the MII. */
1537 mii_tick(&sc->bce_mii);
1538
1539 callout_reset(&sc->bce_timeout, hz, bce_tick, sc);
1540 }
1541