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