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