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