8e857eb58cee751bab108d0608463e60aa954e7f
[dragonfly.git] / sys / dev / netif / bfe / if_bfe.c
1 /*
2  * Copyright (c) 2003 Stuart Walsh<stu@ipng.org.uk>
3  * and Duncan Barclay<dmlb@dmlb.org>
4  * Modifications for FreeBSD-stable by Edwin Groothuis
5  * <edwin at mavetju.org
6  * < http://lists.freebsd.org/mailman/listinfo/freebsd-bugs>>
7  */
8
9 /*
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS 'AS IS' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $FreeBSD: src/sys/dev/bfe/if_bfe.c 1.4.4.7 2004/03/02 08:41:33 julian Exp  v
32  * $DragonFly: src/sys/dev/netif/bfe/if_bfe.c,v 1.40 2008/09/17 08:51:29 sephe Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sockio.h>
38 #include <sys/mbuf.h>
39 #include <sys/malloc.h>
40 #include <sys/interrupt.h>
41 #include <sys/kernel.h>
42 #include <sys/socket.h>
43 #include <sys/queue.h>
44 #include <sys/bus.h>
45 #include <sys/rman.h>
46 #include <sys/thread2.h>
47
48 #include <net/if.h>
49 #include <net/ifq_var.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54
55 #include <net/bpf.h>
56
57 #include <net/if_types.h>
58 #include <net/vlan/if_vlan_var.h>
59
60 #include <netinet/in_systm.h>
61 #include <netinet/in.h>
62 #include <netinet/ip.h>
63
64 #include <bus/pci/pcireg.h>
65 #include <bus/pci/pcivar.h>
66 #include <bus/pci/pcidevs.h>
67
68 #include <dev/netif/mii_layer/mii.h>
69 #include <dev/netif/mii_layer/miivar.h>
70
71 #include <dev/netif/bfe/if_bfereg.h>
72
73 MODULE_DEPEND(bfe, pci, 1, 1, 1);
74 MODULE_DEPEND(bfe, miibus, 1, 1, 1);
75
76 /* "controller miibus0" required.  See GENERIC if you get errors here. */
77 #include "miibus_if.h"
78
79 #define BFE_DEVDESC_MAX         64      /* Maximum device description length */
80
81 struct bfe_dmamap_ctx {
82         int                     nsegs;
83         bus_dma_segment_t       *segs;
84 };
85
86 static struct bfe_type bfe_devs[] = {
87         { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4401,
88             "Broadcom BCM4401 Fast Ethernet" },
89         { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4401B0,
90             "Broadcom BCM4401-B0 Fast Ethernet" },
91         { PCI_VENDOR_BROADCOM, PCI_PRODUCT_BROADCOM_BCM4402,
92             "Broadcom BCM4402 Fast Ethernet" },
93         { 0, 0, NULL }
94 };
95
96 static int      bfe_probe(device_t);
97 static int      bfe_attach(device_t);
98 static int      bfe_detach(device_t);
99 static void     bfe_intr(void *);
100 static void     bfe_start(struct ifnet *);
101 static int      bfe_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
102 static void     bfe_init(void *);
103 static void     bfe_stop(struct bfe_softc *);
104 static void     bfe_watchdog(struct ifnet *);
105 static void     bfe_shutdown(device_t);
106 static void     bfe_tick(void *);
107 static void     bfe_txeof(struct bfe_softc *);
108 static void     bfe_rxeof(struct bfe_softc *);
109 static void     bfe_set_rx_mode(struct bfe_softc *);
110 static int      bfe_list_rx_init(struct bfe_softc *);
111 static int      bfe_newbuf(struct bfe_softc *, int, int);
112 static void     bfe_setup_rxdesc(struct bfe_softc *, int);
113 static void     bfe_rx_ring_free(struct bfe_softc *);
114
115 static void     bfe_pci_setup(struct bfe_softc *, uint32_t);
116 static int      bfe_ifmedia_upd(struct ifnet *);
117 static void     bfe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
118 static int      bfe_miibus_readreg(device_t, int, int);
119 static int      bfe_miibus_writereg(device_t, int, int, int);
120 static int      bfe_wait_bit(struct bfe_softc *, uint32_t, uint32_t,
121                              u_long, const int);
122 static void     bfe_get_config(struct bfe_softc *sc);
123 static void     bfe_read_eeprom(struct bfe_softc *, uint8_t *);
124 static void     bfe_stats_update(struct bfe_softc *);
125 static void     bfe_clear_stats (struct bfe_softc *);
126 static int      bfe_readphy(struct bfe_softc *, uint32_t, uint32_t*);
127 static int      bfe_writephy(struct bfe_softc *, uint32_t, uint32_t);
128 static int      bfe_resetphy(struct bfe_softc *);
129 static int      bfe_setupphy(struct bfe_softc *);
130 static void     bfe_chip_reset(struct bfe_softc *);
131 static void     bfe_chip_halt(struct bfe_softc *);
132 static void     bfe_core_reset(struct bfe_softc *);
133 static void     bfe_core_disable(struct bfe_softc *);
134 static int      bfe_dma_alloc(device_t);
135 static void     bfe_dma_free(struct bfe_softc *);
136 static void     bfe_dma_map_desc(void *, bus_dma_segment_t *, int, int);
137 static void     bfe_dma_map(void *, bus_dma_segment_t *, int, int);
138 static void     bfe_cam_write(struct bfe_softc *, u_char *, int);
139 static void     bfe_dmamap_buf_cb(void *, bus_dma_segment_t *, int,
140                                   bus_size_t, int);
141
142 static device_method_t bfe_methods[] = {
143         /* Device interface */
144         DEVMETHOD(device_probe,         bfe_probe),
145         DEVMETHOD(device_attach,        bfe_attach),
146         DEVMETHOD(device_detach,        bfe_detach),
147         DEVMETHOD(device_shutdown,      bfe_shutdown),
148
149         /* bus interface */
150         DEVMETHOD(bus_print_child,      bus_generic_print_child),
151         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
152
153         /* MII interface */
154         DEVMETHOD(miibus_readreg,       bfe_miibus_readreg),
155         DEVMETHOD(miibus_writereg,      bfe_miibus_writereg),
156
157         { 0, 0 }
158 };
159
160 static driver_t bfe_driver = {
161         "bfe",
162         bfe_methods,
163         sizeof(struct bfe_softc)
164 };
165
166 static devclass_t bfe_devclass;
167
168 DRIVER_MODULE(bfe, pci, bfe_driver, bfe_devclass, 0, 0);
169 DRIVER_MODULE(miibus, bfe, miibus_driver, miibus_devclass, 0, 0);
170
171 /*
172  * Probe for a Broadcom 4401 chip. 
173  */
174 static int
175 bfe_probe(device_t dev)
176 {
177         struct bfe_type *t;
178         uint16_t vendor, product;
179
180         vendor = pci_get_vendor(dev);
181         product = pci_get_device(dev);
182
183         for (t = bfe_devs; t->bfe_name != NULL; t++) {
184                 if (vendor == t->bfe_vid && product == t->bfe_did) {
185                         device_set_desc(dev, t->bfe_name);
186                         return(0);
187                 }
188         }
189
190         return(ENXIO);
191 }
192
193 static int
194 bfe_dma_alloc(device_t dev)
195 {
196         struct bfe_softc *sc = device_get_softc(dev);
197         int error, i, tx_pos = 0, rx_pos = 0;
198
199         /*
200          * Parent tag.  Apparently the chip cannot handle any DMA address
201          * greater than BFE_BUS_SPACE_MAXADDR (1GB).
202          */
203         error = bus_dma_tag_create(NULL,          /* parent */
204                         1, 0,                     /* alignment, boundary */
205                         BFE_BUS_SPACE_MAXADDR,    /* lowaddr */
206                         BUS_SPACE_MAXADDR,        /* highaddr */
207                         NULL, NULL,               /* filter, filterarg */
208                         BUS_SPACE_MAXSIZE_32BIT,  /* maxsize */
209                         0,                        /* num of segments */
210                         BUS_SPACE_MAXSIZE_32BIT,  /* max segment size */
211                         0,                        /* flags */
212                         &sc->bfe_parent_tag);
213         if (error) {
214                 device_printf(dev, "could not allocate parent dma tag\n");
215                 return(error);
216         }
217
218         /* tag for TX ring */
219         error = bus_dma_tag_create(sc->bfe_parent_tag, PAGE_SIZE, 0,
220                                    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
221                                    NULL, NULL,
222                                    BFE_TX_LIST_SIZE, 1, BFE_TX_LIST_SIZE,
223                                    0, &sc->bfe_tx_tag);
224         if (error) {
225                 device_printf(dev, "could not allocate dma tag for TX list\n");
226                 return(error);
227         }
228
229         /* tag for RX ring */
230         error = bus_dma_tag_create(sc->bfe_parent_tag, PAGE_SIZE, 0,
231                                    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
232                                    NULL, NULL,
233                                    BFE_RX_LIST_SIZE, 1, BFE_RX_LIST_SIZE,
234                                    0, &sc->bfe_rx_tag);
235         if (error) {
236                 device_printf(dev, "could not allocate dma tag for RX list\n");
237                 return(error);
238         }
239
240         /* Tag for RX mbufs */
241         error = bus_dma_tag_create(sc->bfe_parent_tag, ETHER_ALIGN, 0,
242                                    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
243                                    NULL, NULL,
244                                    MCLBYTES, 1, MCLBYTES,
245                                    BUS_DMA_ALLOCNOW, &sc->bfe_rxbuf_tag);
246         if (error) {
247                 device_printf(dev, "could not allocate dma tag for RX mbufs\n");
248                 return(error);
249         }
250
251         error = bus_dmamap_create(sc->bfe_rxbuf_tag, 0, &sc->bfe_rx_tmpmap);
252         if (error) {
253                 device_printf(dev, "could not create RX mbuf tmp map\n");
254                 bus_dma_tag_destroy(sc->bfe_rxbuf_tag);
255                 sc->bfe_rxbuf_tag = NULL;
256                 return error;
257         }
258
259         /* Allocate dma maps for RX list */
260         for (i = 0; i < BFE_RX_LIST_CNT; i++) {
261                 error = bus_dmamap_create(sc->bfe_rxbuf_tag, 0,
262                                           &sc->bfe_rx_ring[i].bfe_map);
263                 if (error) {
264                         rx_pos = i;
265                         device_printf(dev, "cannot create DMA map for RX\n");
266                         goto ring_fail;
267                 }
268         }
269         rx_pos = BFE_RX_LIST_CNT;
270
271         /* Tag for TX mbufs */
272         error = bus_dma_tag_create(sc->bfe_parent_tag, ETHER_ALIGN, 0,
273                                    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
274                                    NULL, NULL,
275                                    MCLBYTES, 1, MCLBYTES,
276                                    BUS_DMA_ALLOCNOW, &sc->bfe_txbuf_tag);
277         if (error) {
278                 device_printf(dev, "could not allocate dma tag for TX mbufs\n");
279                 return(error);
280         }
281
282         /* Allocate dmamaps for TX list */
283         for (i = 0; i < BFE_TX_LIST_CNT; i++) {
284                 error = bus_dmamap_create(sc->bfe_txbuf_tag, 0,
285                                           &sc->bfe_tx_ring[i].bfe_map);
286                 if (error) {
287                         tx_pos = i;
288                         device_printf(dev, "cannot create DMA map for TX\n");
289                         goto ring_fail;
290                 }
291         }
292
293         /* Alloc dma for rx ring */
294         error = bus_dmamem_alloc(sc->bfe_rx_tag, (void *)&sc->bfe_rx_list,
295                                  BUS_DMA_WAITOK | BUS_DMA_ZERO,
296                                  &sc->bfe_rx_map);
297         if (error) {
298                 device_printf(dev, "cannot allocate DMA mem for RX\n");
299                 return(error);
300         }
301
302         error = bus_dmamap_load(sc->bfe_rx_tag, sc->bfe_rx_map,
303                                 sc->bfe_rx_list, sizeof(struct bfe_desc),
304                                 bfe_dma_map, &sc->bfe_rx_dma, BUS_DMA_WAITOK);
305         if (error) {
306                 device_printf(dev, "cannot load DMA map for RX\n");
307                 return(error);
308         }
309
310         /* Alloc dma for tx ring */
311         error = bus_dmamem_alloc(sc->bfe_tx_tag, (void *)&sc->bfe_tx_list,
312                                  BUS_DMA_WAITOK | BUS_DMA_ZERO,
313                                  &sc->bfe_tx_map);
314         if (error) {
315                 device_printf(dev, "cannot allocate DMA mem for TX\n");
316                 return(error);
317         }
318
319         error = bus_dmamap_load(sc->bfe_tx_tag, sc->bfe_tx_map,
320                                 sc->bfe_tx_list, sizeof(struct bfe_desc),
321                                 bfe_dma_map, &sc->bfe_tx_dma, BUS_DMA_WAITOK);
322         if (error) {
323                 device_printf(dev, "cannot load DMA map for TX\n");
324                 return(error);
325         }
326
327         return(0);
328
329 ring_fail:
330         if (sc->bfe_rxbuf_tag != NULL) {
331                 for (i = 0; i < rx_pos; ++i) {
332                         bus_dmamap_destroy(sc->bfe_rxbuf_tag,
333                                            sc->bfe_rx_ring[i].bfe_map);
334                 }
335                 bus_dmamap_destroy(sc->bfe_rxbuf_tag, sc->bfe_rx_tmpmap);
336                 bus_dma_tag_destroy(sc->bfe_rxbuf_tag);
337                 sc->bfe_rxbuf_tag = NULL;
338         }
339
340         if (sc->bfe_txbuf_tag != NULL) {
341                 for (i = 0; i < tx_pos; ++i) {
342                         bus_dmamap_destroy(sc->bfe_txbuf_tag,
343                                            sc->bfe_tx_ring[i].bfe_map);
344                 }
345                 bus_dma_tag_destroy(sc->bfe_txbuf_tag);
346                 sc->bfe_txbuf_tag = NULL;
347         }
348         return error;
349 }
350
351 static int
352 bfe_attach(device_t dev)
353 {
354         struct ifnet *ifp;
355         struct bfe_softc *sc;
356         int error = 0, rid;
357
358         sc = device_get_softc(dev);
359
360         sc->bfe_dev = dev;
361         callout_init(&sc->bfe_stat_timer);
362
363 #ifndef BURN_BRIDGES
364         /*
365          * Handle power management nonsense.
366          */
367         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
368                 uint32_t membase, irq;
369
370                 /* Save important PCI config data. */
371                 membase = pci_read_config(dev, BFE_PCI_MEMLO, 4);
372                 irq = pci_read_config(dev, BFE_PCI_INTLINE, 4);
373
374                 /* Reset the power state. */
375                 device_printf(dev, "chip is in D%d power mode"
376                               " -- setting to D0\n", pci_get_powerstate(dev));
377
378                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
379
380                 /* Restore PCI config data. */
381                 pci_write_config(dev, BFE_PCI_MEMLO, membase, 4);
382                 pci_write_config(dev, BFE_PCI_INTLINE, irq, 4);
383         }
384 #endif  /* !BURN_BRIDGE */
385
386         /*
387          * Map control/status registers.
388          */
389         pci_enable_busmaster(dev);
390
391         rid = BFE_PCI_MEMLO;
392         sc->bfe_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
393             RF_ACTIVE);
394         if (sc->bfe_res == NULL) {
395                 device_printf(dev, "couldn't map memory\n");
396                 return ENXIO;
397         }
398
399         sc->bfe_btag = rman_get_bustag(sc->bfe_res);
400         sc->bfe_bhandle = rman_get_bushandle(sc->bfe_res);
401
402         /* Allocate interrupt */
403         rid = 0;
404
405         sc->bfe_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
406             RF_SHAREABLE | RF_ACTIVE);
407         if (sc->bfe_irq == NULL) {
408                 device_printf(dev, "couldn't map interrupt\n");
409                 error = ENXIO;
410                 goto fail;
411         }
412
413         error = bfe_dma_alloc(dev);
414         if (error != 0) {
415                 device_printf(dev, "failed to allocate DMA resources\n");
416                 goto fail;
417         }
418
419         /* Set up ifnet structure */
420         ifp = &sc->arpcom.ac_if;
421         ifp->if_softc = sc;
422         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
423         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
424         ifp->if_ioctl = bfe_ioctl;
425         ifp->if_start = bfe_start;
426         ifp->if_watchdog = bfe_watchdog;
427         ifp->if_init = bfe_init;
428         ifp->if_mtu = ETHERMTU;
429         ifp->if_baudrate = 100000000;
430         ifp->if_capabilities |= IFCAP_VLAN_MTU;
431         ifp->if_capenable |= IFCAP_VLAN_MTU;
432         ifp->if_hdrlen = sizeof(struct ether_vlan_header);
433         ifq_set_maxlen(&ifp->if_snd, BFE_TX_QLEN);
434         ifq_set_ready(&ifp->if_snd);
435
436         bfe_get_config(sc);
437
438         /* Reset the chip and turn on the PHY */
439         bfe_chip_reset(sc);
440
441         if (mii_phy_probe(dev, &sc->bfe_miibus,
442                                 bfe_ifmedia_upd, bfe_ifmedia_sts)) {
443                 device_printf(dev, "MII without any PHY!\n");
444                 error = ENXIO;
445                 goto fail;
446         }
447
448         ether_ifattach(ifp, sc->arpcom.ac_enaddr, NULL);
449
450         /*
451          * Hook interrupt last to avoid having to lock softc
452          */
453         error = bus_setup_intr(dev, sc->bfe_irq, INTR_MPSAFE,
454                                bfe_intr, sc, &sc->bfe_intrhand, 
455                                sc->arpcom.ac_if.if_serializer);
456
457         if (error) {
458                 ether_ifdetach(ifp);
459                 device_printf(dev, "couldn't set up irq\n");
460                 goto fail;
461         }
462
463         ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->bfe_irq));
464         KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
465         return 0;
466 fail:
467         bfe_detach(dev);
468         return(error);
469 }
470
471 static int
472 bfe_detach(device_t dev)
473 {
474         struct bfe_softc *sc = device_get_softc(dev);
475         struct ifnet *ifp = &sc->arpcom.ac_if;
476
477         if (device_is_attached(dev)) {
478                 lwkt_serialize_enter(ifp->if_serializer);
479                 bfe_stop(sc);
480                 bfe_chip_reset(sc);
481                 bus_teardown_intr(dev, sc->bfe_irq, sc->bfe_intrhand);
482                 lwkt_serialize_exit(ifp->if_serializer);
483
484                 ether_ifdetach(ifp);
485         }
486         if (sc->bfe_miibus != NULL)
487                 device_delete_child(dev, sc->bfe_miibus);
488         bus_generic_detach(dev);
489
490         if (sc->bfe_irq != NULL)
491                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->bfe_irq);
492
493         if (sc->bfe_res != NULL) {
494                 bus_release_resource(dev, SYS_RES_MEMORY, BFE_PCI_MEMLO,
495                                      sc->bfe_res);
496         }
497         bfe_dma_free(sc);
498
499         return(0);
500 }
501
502 /*
503  * Stop all chip I/O so that the kernel's probe routines don't
504  * get confused by errant DMAs when rebooting.
505  */
506 static void
507 bfe_shutdown(device_t dev)
508 {
509         struct bfe_softc *sc = device_get_softc(dev);
510         struct ifnet *ifp = &sc->arpcom.ac_if;
511
512         lwkt_serialize_enter(ifp->if_serializer);
513         bfe_stop(sc); 
514         lwkt_serialize_exit(ifp->if_serializer);
515 }
516
517 static int
518 bfe_miibus_readreg(device_t dev, int phy, int reg)
519 {
520         struct bfe_softc *sc;
521         uint32_t ret;
522
523         sc = device_get_softc(dev);
524         if (phy != sc->bfe_phyaddr)
525                 return(0);
526         bfe_readphy(sc, reg, &ret);
527
528         return(ret);
529 }
530
531 static int
532 bfe_miibus_writereg(device_t dev, int phy, int reg, int val)
533 {
534         struct bfe_softc *sc;
535
536         sc = device_get_softc(dev);
537         if (phy != sc->bfe_phyaddr)
538                 return(0);
539         bfe_writephy(sc, reg, val); 
540
541         return(0);
542 }
543
544 static void
545 bfe_tx_ring_free(struct bfe_softc *sc)
546 {
547         int i;
548     
549         for (i = 0; i < BFE_TX_LIST_CNT; i++) {
550                 bus_dmamap_unload(sc->bfe_txbuf_tag,
551                                   sc->bfe_tx_ring[i].bfe_map);
552                 if (sc->bfe_tx_ring[i].bfe_mbuf != NULL) {
553                         m_freem(sc->bfe_tx_ring[i].bfe_mbuf);
554                         sc->bfe_tx_ring[i].bfe_mbuf = NULL;
555                 }
556         }
557         bzero(sc->bfe_tx_list, BFE_TX_LIST_SIZE);
558         bus_dmamap_sync(sc->bfe_tx_tag, sc->bfe_tx_map, BUS_DMASYNC_PREWRITE);
559 }
560
561 static void
562 bfe_rx_ring_free(struct bfe_softc *sc)
563 {
564         int i;
565
566         for (i = 0; i < BFE_RX_LIST_CNT; i++) {
567                 if (sc->bfe_rx_ring[i].bfe_mbuf != NULL) {
568                         bus_dmamap_unload(sc->bfe_rxbuf_tag,
569                                           sc->bfe_rx_ring[i].bfe_map);
570                         m_freem(sc->bfe_rx_ring[i].bfe_mbuf);
571                         sc->bfe_rx_ring[i].bfe_mbuf = NULL;
572                 }
573         }
574         bzero(sc->bfe_rx_list, BFE_RX_LIST_SIZE);
575         bus_dmamap_sync(sc->bfe_rx_tag, sc->bfe_rx_map, BUS_DMASYNC_PREWRITE);
576 }
577
578 static int 
579 bfe_list_rx_init(struct bfe_softc *sc)
580 {
581         int i, error;
582
583         for (i = 0; i < BFE_RX_LIST_CNT; i++) {
584                 error = bfe_newbuf(sc, i, 1);
585                 if (error)
586                         return(error);
587         }
588
589         bus_dmamap_sync(sc->bfe_rx_tag, sc->bfe_rx_map, BUS_DMASYNC_PREWRITE);
590         CSR_WRITE_4(sc, BFE_DMARX_PTR, (i * sizeof(struct bfe_desc)));
591
592         sc->bfe_rx_cons = 0;
593
594         return(0);
595 }
596
597 static int
598 bfe_newbuf(struct bfe_softc *sc, int c, int init)
599 {
600         struct bfe_data *r;
601         bus_dmamap_t map;
602         bus_dma_segment_t seg;
603         struct bfe_dmamap_ctx ctx;
604         struct mbuf *m;
605         int error;
606
607         m = m_getcl(init ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
608         if (m == NULL)
609                 return ENOBUFS;
610         m->m_len = m->m_pkthdr.len = MCLBYTES;
611
612         ctx.nsegs = 1;
613         ctx.segs = &seg;
614         error = bus_dmamap_load_mbuf(sc->bfe_rxbuf_tag,
615                                      sc->bfe_rx_tmpmap,
616                                      m, bfe_dmamap_buf_cb, &ctx,
617                                      BUS_DMA_NOWAIT);
618         if (error || ctx.nsegs == 0) {
619                 if (!error) {
620                         bus_dmamap_unload(sc->bfe_rxbuf_tag,
621                                           sc->bfe_rx_tmpmap);
622                         error = EFBIG;
623                         if_printf(&sc->arpcom.ac_if, "too many segments?!\n");
624                 }
625                 m_freem(m);
626
627                 if (init)
628                         if_printf(&sc->arpcom.ac_if, "can't load RX mbuf\n");
629                 return error;
630         }
631
632         KKASSERT(c >= 0 && c < BFE_RX_LIST_CNT);
633         r = &sc->bfe_rx_ring[c];
634
635         if (r->bfe_mbuf != NULL)
636                 bus_dmamap_unload(sc->bfe_rxbuf_tag, r->bfe_map);
637
638         map = r->bfe_map;
639         r->bfe_map = sc->bfe_rx_tmpmap;
640         sc->bfe_rx_tmpmap = map;
641
642         r->bfe_mbuf = m;
643         r->bfe_paddr = seg.ds_addr;
644
645         bfe_setup_rxdesc(sc, c);
646         return 0;
647 }
648
649 static void
650 bfe_setup_rxdesc(struct bfe_softc *sc, int c)
651 {
652         struct bfe_rxheader *rx_header;
653         struct mbuf *m;
654         struct bfe_desc *d;
655         struct bfe_data *r;
656         uint32_t ctrl;
657
658         KKASSERT(c >= 0 && c < BFE_RX_LIST_CNT);
659         r = &sc->bfe_rx_ring[c];
660         d = &sc->bfe_rx_list[c];
661
662         KKASSERT(r->bfe_mbuf != NULL && r->bfe_paddr != 0);
663
664         m = r->bfe_mbuf;
665         rx_header = mtod(m, struct bfe_rxheader *);
666         rx_header->len = 0;
667         rx_header->flags = 0;
668         bus_dmamap_sync(sc->bfe_rxbuf_tag, r->bfe_map, BUS_DMASYNC_PREWRITE);
669
670         ctrl = ETHER_MAX_LEN + 32;
671         if (c == BFE_RX_LIST_CNT - 1)
672                 ctrl |= BFE_DESC_EOT;
673
674         d->bfe_addr = r->bfe_paddr + BFE_PCI_DMA;
675         d->bfe_ctrl = ctrl;
676         bus_dmamap_sync(sc->bfe_rx_tag, sc->bfe_rx_map, BUS_DMASYNC_PREWRITE);
677 }
678
679 static void
680 bfe_get_config(struct bfe_softc *sc)
681 {
682         uint8_t eeprom[128];
683
684         bfe_read_eeprom(sc, eeprom);
685
686         sc->arpcom.ac_enaddr[0] = eeprom[79];
687         sc->arpcom.ac_enaddr[1] = eeprom[78];
688         sc->arpcom.ac_enaddr[2] = eeprom[81];
689         sc->arpcom.ac_enaddr[3] = eeprom[80];
690         sc->arpcom.ac_enaddr[4] = eeprom[83];
691         sc->arpcom.ac_enaddr[5] = eeprom[82];
692
693         sc->bfe_phyaddr = eeprom[90] & 0x1f;
694         sc->bfe_mdc_port = (eeprom[90] >> 14) & 0x1;
695
696         sc->bfe_core_unit = 0; 
697         sc->bfe_dma_offset = BFE_PCI_DMA;
698 }
699
700 static void
701 bfe_pci_setup(struct bfe_softc *sc, uint32_t cores)
702 {
703         uint32_t bar_orig, pci_rev, val;
704
705         bar_orig = pci_read_config(sc->bfe_dev, BFE_BAR0_WIN, 4);
706         pci_write_config(sc->bfe_dev, BFE_BAR0_WIN, BFE_REG_PCI, 4);
707         pci_rev = CSR_READ_4(sc, BFE_SBIDHIGH) & BFE_RC_MASK;
708
709         val = CSR_READ_4(sc, BFE_SBINTVEC);
710         val |= cores;
711         CSR_WRITE_4(sc, BFE_SBINTVEC, val);
712
713         val = CSR_READ_4(sc, BFE_SSB_PCI_TRANS_2);
714         val |= BFE_SSB_PCI_PREF | BFE_SSB_PCI_BURST;
715         CSR_WRITE_4(sc, BFE_SSB_PCI_TRANS_2, val);
716
717         pci_write_config(sc->bfe_dev, BFE_BAR0_WIN, bar_orig, 4);
718 }
719
720 static void 
721 bfe_clear_stats(struct bfe_softc *sc)
722 {
723         u_long reg;
724
725         CSR_WRITE_4(sc, BFE_MIB_CTRL, BFE_MIB_CLR_ON_READ);
726         for (reg = BFE_TX_GOOD_O; reg <= BFE_TX_PAUSE; reg += 4)
727                 CSR_READ_4(sc, reg);
728         for (reg = BFE_RX_GOOD_O; reg <= BFE_RX_NPAUSE; reg += 4)
729                 CSR_READ_4(sc, reg);
730 }
731
732 static int 
733 bfe_resetphy(struct bfe_softc *sc)
734 {
735         uint32_t val;
736
737         bfe_writephy(sc, 0, BMCR_RESET);
738         DELAY(100);
739         bfe_readphy(sc, 0, &val);
740         if (val & BMCR_RESET) {
741                 if_printf(&sc->arpcom.ac_if,
742                           "PHY Reset would not complete.\n");
743                 return(ENXIO);
744         }
745         return(0);
746 }
747
748 static void
749 bfe_chip_halt(struct bfe_softc *sc)
750 {
751         /* disable interrupts - not that it actually does..*/
752         CSR_WRITE_4(sc, BFE_IMASK, 0);
753         CSR_READ_4(sc, BFE_IMASK);
754
755         CSR_WRITE_4(sc, BFE_ENET_CTRL, BFE_ENET_DISABLE);
756         bfe_wait_bit(sc, BFE_ENET_CTRL, BFE_ENET_DISABLE, 200, 1);
757
758         CSR_WRITE_4(sc, BFE_DMARX_CTRL, 0);
759         CSR_WRITE_4(sc, BFE_DMATX_CTRL, 0);
760         DELAY(10);
761 }
762
763 static void
764 bfe_chip_reset(struct bfe_softc *sc)
765 {
766         uint32_t val;    
767
768         /* Set the interrupt vector for the enet core */
769         bfe_pci_setup(sc, BFE_INTVEC_ENET0);
770
771         /* is core up? */
772         val = CSR_READ_4(sc, BFE_SBTMSLOW) & (BFE_RESET | BFE_REJECT | BFE_CLOCK);
773         if (val == BFE_CLOCK) {
774                 /* It is, so shut it down */
775                 CSR_WRITE_4(sc, BFE_RCV_LAZY, 0);
776                 CSR_WRITE_4(sc, BFE_ENET_CTRL, BFE_ENET_DISABLE);
777                 bfe_wait_bit(sc, BFE_ENET_CTRL, BFE_ENET_DISABLE, 100, 1);
778                 CSR_WRITE_4(sc, BFE_DMATX_CTRL, 0);
779                 sc->bfe_tx_cnt = sc->bfe_tx_prod = sc->bfe_tx_cons = 0;
780                 if (CSR_READ_4(sc, BFE_DMARX_STAT) & BFE_STAT_EMASK) 
781                         bfe_wait_bit(sc, BFE_DMARX_STAT, BFE_STAT_SIDLE, 100, 0);
782                 CSR_WRITE_4(sc, BFE_DMARX_CTRL, 0);
783                 sc->bfe_rx_cons = 0;
784         }
785
786         bfe_core_reset(sc);
787         bfe_clear_stats(sc);
788
789         /*
790          * We want the phy registers to be accessible even when
791          * the driver is "downed" so initialize MDC preamble, frequency,
792          * and whether internal or external phy here.
793          */
794
795         /* 4402 has 62.5Mhz SB clock and internal phy */
796         CSR_WRITE_4(sc, BFE_MDIO_CTRL, 0x8d);
797
798         /* Internal or external PHY? */
799         val = CSR_READ_4(sc, BFE_DEVCTRL);
800         if (!(val & BFE_IPP)) 
801                 CSR_WRITE_4(sc, BFE_ENET_CTRL, BFE_ENET_EPSEL);
802         else if (CSR_READ_4(sc, BFE_DEVCTRL) & BFE_EPR) {
803                 BFE_AND(sc, BFE_DEVCTRL, ~BFE_EPR);
804                 DELAY(100);
805         }
806
807         /* Enable CRC32 generation and set proper LED modes */
808         BFE_OR(sc, BFE_MAC_CTRL, BFE_CTRL_CRC32_ENAB | BFE_CTRL_LED);
809
810         /* Reset or clear powerdown control bit  */
811         BFE_AND(sc, BFE_MAC_CTRL, ~BFE_CTRL_PDOWN);
812
813         CSR_WRITE_4(sc, BFE_RCV_LAZY, ((1 << BFE_LAZY_FC_SHIFT) & 
814                                 BFE_LAZY_FC_MASK));
815
816         /* 
817          * We don't want lazy interrupts, so just send them at the end of a
818          * frame, please 
819          */
820         BFE_OR(sc, BFE_RCV_LAZY, 0);
821
822         /* Set max lengths, accounting for VLAN tags */
823         CSR_WRITE_4(sc, BFE_RXMAXLEN, ETHER_MAX_LEN+32);
824         CSR_WRITE_4(sc, BFE_TXMAXLEN, ETHER_MAX_LEN+32);
825
826         /* Set watermark XXX - magic */
827         CSR_WRITE_4(sc, BFE_TX_WMARK, 56);
828
829         /* 
830          * Initialise DMA channels - not forgetting dma addresses need to be
831          * added to BFE_PCI_DMA 
832          */
833         CSR_WRITE_4(sc, BFE_DMATX_CTRL, BFE_TX_CTRL_ENABLE);
834         CSR_WRITE_4(sc, BFE_DMATX_ADDR, sc->bfe_tx_dma + BFE_PCI_DMA);
835
836         CSR_WRITE_4(sc, BFE_DMARX_CTRL, (BFE_RX_OFFSET << BFE_RX_CTRL_ROSHIFT) | 
837                         BFE_RX_CTRL_ENABLE);
838         CSR_WRITE_4(sc, BFE_DMARX_ADDR, sc->bfe_rx_dma + BFE_PCI_DMA);
839
840         bfe_resetphy(sc);
841         bfe_setupphy(sc);
842 }
843
844 static void
845 bfe_core_disable(struct bfe_softc *sc)
846 {
847         if ((CSR_READ_4(sc, BFE_SBTMSLOW)) & BFE_RESET)
848                 return;
849
850         /* 
851          * Set reject, wait for it set, then wait for the core to stop being busy
852          * Then set reset and reject and enable the clocks
853          */
854         CSR_WRITE_4(sc, BFE_SBTMSLOW, (BFE_REJECT | BFE_CLOCK));
855         bfe_wait_bit(sc, BFE_SBTMSLOW, BFE_REJECT, 1000, 0);
856         bfe_wait_bit(sc, BFE_SBTMSHIGH, BFE_BUSY, 1000, 1);
857         CSR_WRITE_4(sc, BFE_SBTMSLOW, (BFE_FGC | BFE_CLOCK | BFE_REJECT |
858                                 BFE_RESET));
859         CSR_READ_4(sc, BFE_SBTMSLOW);
860         DELAY(10);
861         /* Leave reset and reject set */
862         CSR_WRITE_4(sc, BFE_SBTMSLOW, (BFE_REJECT | BFE_RESET));
863         DELAY(10);
864 }
865
866 static void
867 bfe_core_reset(struct bfe_softc *sc)
868 {
869         uint32_t val;
870
871         /* Disable the core */
872         bfe_core_disable(sc);
873
874         /* and bring it back up */
875         CSR_WRITE_4(sc, BFE_SBTMSLOW, (BFE_RESET | BFE_CLOCK | BFE_FGC));
876         CSR_READ_4(sc, BFE_SBTMSLOW);
877         DELAY(10);
878
879         /* Chip bug, clear SERR, IB and TO if they are set. */
880         if (CSR_READ_4(sc, BFE_SBTMSHIGH) & BFE_SERR)
881                 CSR_WRITE_4(sc, BFE_SBTMSHIGH, 0);
882         val = CSR_READ_4(sc, BFE_SBIMSTATE);
883         if (val & (BFE_IBE | BFE_TO))
884                 CSR_WRITE_4(sc, BFE_SBIMSTATE, val & ~(BFE_IBE | BFE_TO));
885
886         /* Clear reset and allow it to move through the core */
887         CSR_WRITE_4(sc, BFE_SBTMSLOW, (BFE_CLOCK | BFE_FGC));
888         CSR_READ_4(sc, BFE_SBTMSLOW);
889         DELAY(10);
890
891         /* Leave the clock set */
892         CSR_WRITE_4(sc, BFE_SBTMSLOW, BFE_CLOCK);
893         CSR_READ_4(sc, BFE_SBTMSLOW);
894         DELAY(10);
895 }
896
897 static void 
898 bfe_cam_write(struct bfe_softc *sc, u_char *data, int index)
899 {
900         uint32_t val;
901
902         val  = ((uint32_t) data[2]) << 24;
903         val |= ((uint32_t) data[3]) << 16;
904         val |= ((uint32_t) data[4]) <<  8;
905         val |= ((uint32_t) data[5]);
906         CSR_WRITE_4(sc, BFE_CAM_DATA_LO, val);
907         val = (BFE_CAM_HI_VALID |
908                         (((uint32_t) data[0]) << 8) |
909                         (((uint32_t) data[1])));
910         CSR_WRITE_4(sc, BFE_CAM_DATA_HI, val);
911         CSR_WRITE_4(sc, BFE_CAM_CTRL, (BFE_CAM_WRITE |
912                     ((uint32_t)index << BFE_CAM_INDEX_SHIFT)));
913         bfe_wait_bit(sc, BFE_CAM_CTRL, BFE_CAM_BUSY, 10000, 1);
914 }
915
916 static void 
917 bfe_set_rx_mode(struct bfe_softc *sc)
918 {
919         struct ifnet *ifp = &sc->arpcom.ac_if;
920         struct ifmultiaddr  *ifma;
921         uint32_t val;
922         int i = 0;
923
924         val = CSR_READ_4(sc, BFE_RXCONF);
925
926         if (ifp->if_flags & IFF_PROMISC)
927                 val |= BFE_RXCONF_PROMISC;
928         else
929                 val &= ~BFE_RXCONF_PROMISC;
930
931         if (ifp->if_flags & IFF_BROADCAST)
932                 val &= ~BFE_RXCONF_DBCAST;
933         else
934                 val |= BFE_RXCONF_DBCAST;
935
936
937         CSR_WRITE_4(sc, BFE_CAM_CTRL, 0);
938         bfe_cam_write(sc, sc->arpcom.ac_enaddr, i++);
939
940         if (ifp->if_flags & IFF_ALLMULTI) {
941                 val |= BFE_RXCONF_ALLMULTI;
942         } else {
943                 val &= ~BFE_RXCONF_ALLMULTI;
944                 LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
945                         if (ifma->ifma_addr->sa_family != AF_LINK)
946                                 continue;
947                         bfe_cam_write(sc,
948                             LLADDR((struct sockaddr_dl *)ifma->ifma_addr), i++);
949                 }
950         }
951
952         CSR_WRITE_4(sc, BFE_RXCONF, val);
953         BFE_OR(sc, BFE_CAM_CTRL, BFE_CAM_ENABLE);
954 }
955
956 static void
957 bfe_dma_map(void *arg, bus_dma_segment_t *segs, int nseg, int error)
958 {
959         uint32_t *ptr;
960
961         ptr = arg;
962         *ptr = segs->ds_addr;
963 }
964
965 static void
966 bfe_dma_map_desc(void *arg, bus_dma_segment_t *segs, int nseg, int error)
967 {
968         struct bfe_desc *d;
969
970         d = arg;
971         /* The chip needs all addresses to be added to BFE_PCI_DMA */
972         d->bfe_addr = segs->ds_addr + BFE_PCI_DMA;
973 }
974
975 static void
976 bfe_dma_free(struct bfe_softc *sc)
977 {
978         int i;
979
980         if (sc->bfe_tx_tag != NULL) {
981                 bus_dmamap_unload(sc->bfe_tx_tag, sc->bfe_tx_map);
982                 if (sc->bfe_tx_list != NULL) {
983                         bus_dmamem_free(sc->bfe_tx_tag, sc->bfe_tx_list,
984                                         sc->bfe_tx_map);
985                         sc->bfe_tx_list = NULL;
986                 }
987                 bus_dma_tag_destroy(sc->bfe_tx_tag);
988                 sc->bfe_tx_tag = NULL;
989         }
990
991         if (sc->bfe_rx_tag != NULL) {
992                 bus_dmamap_unload(sc->bfe_rx_tag, sc->bfe_rx_map);
993                 if (sc->bfe_rx_list != NULL) {
994                         bus_dmamem_free(sc->bfe_rx_tag, sc->bfe_rx_list,
995                                         sc->bfe_rx_map);
996                         sc->bfe_rx_list = NULL;
997                 }
998                 bus_dma_tag_destroy(sc->bfe_rx_tag);
999                 sc->bfe_rx_tag = NULL;
1000         }
1001
1002         if (sc->bfe_txbuf_tag != NULL) {
1003                 for (i = 0; i < BFE_TX_LIST_CNT; i++) {
1004                         bus_dmamap_destroy(sc->bfe_txbuf_tag,
1005                                            sc->bfe_tx_ring[i].bfe_map);
1006                 }
1007                 bus_dma_tag_destroy(sc->bfe_txbuf_tag);
1008                 sc->bfe_txbuf_tag = NULL;
1009         }
1010
1011         if (sc->bfe_rxbuf_tag != NULL) {
1012                 for (i = 0; i < BFE_RX_LIST_CNT; i++) {
1013                         bus_dmamap_destroy(sc->bfe_rxbuf_tag,
1014                                            sc->bfe_rx_ring[i].bfe_map);
1015                 }
1016                 bus_dmamap_destroy(sc->bfe_rxbuf_tag, sc->bfe_rx_tmpmap);
1017                 bus_dma_tag_destroy(sc->bfe_rxbuf_tag);
1018                 sc->bfe_rxbuf_tag = NULL;
1019         }
1020
1021         if (sc->bfe_parent_tag != NULL) {
1022                 bus_dma_tag_destroy(sc->bfe_parent_tag);
1023                 sc->bfe_parent_tag = NULL;
1024         }
1025 }
1026
1027 static void
1028 bfe_read_eeprom(struct bfe_softc *sc, uint8_t *data)
1029 {
1030         long i;
1031         uint16_t *ptr = (uint16_t *)data;
1032
1033         for (i = 0; i < 128; i += 2)
1034                 ptr[i/2] = CSR_READ_4(sc, 4096 + i);
1035 }
1036
1037 static int
1038 bfe_wait_bit(struct bfe_softc *sc, uint32_t reg, uint32_t bit, 
1039              u_long timeout, const int clear)
1040 {
1041         u_long i;
1042
1043         for (i = 0; i < timeout; i++) {
1044                 uint32_t val = CSR_READ_4(sc, reg);
1045
1046                 if (clear && !(val & bit))
1047                         break;
1048                 if (!clear && (val & bit))
1049                         break;
1050                 DELAY(10);
1051         }
1052         if (i == timeout) {
1053                 if_printf(&sc->arpcom.ac_if,
1054                           "BUG!  Timeout waiting for bit %08x of register "
1055                           "%x to %s.\n", bit, reg, 
1056                           (clear ? "clear" : "set"));
1057                 return -1;
1058         }
1059         return 0;
1060 }
1061
1062 static int
1063 bfe_readphy(struct bfe_softc *sc, uint32_t reg, uint32_t *val)
1064 {
1065         int err; 
1066
1067         /* Clear MII ISR */
1068         CSR_WRITE_4(sc, BFE_EMAC_ISTAT, BFE_EMAC_INT_MII);
1069         CSR_WRITE_4(sc, BFE_MDIO_DATA, (BFE_MDIO_SB_START |
1070                                 (BFE_MDIO_OP_READ << BFE_MDIO_OP_SHIFT) |
1071                                 (sc->bfe_phyaddr << BFE_MDIO_PMD_SHIFT) |
1072                                 (reg << BFE_MDIO_RA_SHIFT) |
1073                                 (BFE_MDIO_TA_VALID << BFE_MDIO_TA_SHIFT)));
1074         err = bfe_wait_bit(sc, BFE_EMAC_ISTAT, BFE_EMAC_INT_MII, 100, 0);
1075         *val = CSR_READ_4(sc, BFE_MDIO_DATA) & BFE_MDIO_DATA_DATA;
1076         return(err);
1077 }
1078
1079 static int
1080 bfe_writephy(struct bfe_softc *sc, uint32_t reg, uint32_t val)
1081 {
1082         int status;
1083
1084         CSR_WRITE_4(sc, BFE_EMAC_ISTAT, BFE_EMAC_INT_MII);
1085         CSR_WRITE_4(sc, BFE_MDIO_DATA, (BFE_MDIO_SB_START |
1086                                 (BFE_MDIO_OP_WRITE << BFE_MDIO_OP_SHIFT) |
1087                                 (sc->bfe_phyaddr << BFE_MDIO_PMD_SHIFT) |
1088                                 (reg << BFE_MDIO_RA_SHIFT) |
1089                                 (BFE_MDIO_TA_VALID << BFE_MDIO_TA_SHIFT) |
1090                                 (val & BFE_MDIO_DATA_DATA)));
1091         status = bfe_wait_bit(sc, BFE_EMAC_ISTAT, BFE_EMAC_INT_MII, 100, 0);
1092
1093         return status;
1094 }
1095
1096 /* 
1097  * XXX - I think this is handled by the PHY driver, but it can't hurt to do it
1098  * twice
1099  */
1100 static int
1101 bfe_setupphy(struct bfe_softc *sc)
1102 {
1103         uint32_t val;
1104         
1105         /* Enable activity LED */
1106         bfe_readphy(sc, 26, &val);
1107         bfe_writephy(sc, 26, val & 0x7fff); 
1108         bfe_readphy(sc, 26, &val);
1109
1110         /* Enable traffic meter LED mode */
1111         bfe_readphy(sc, 27, &val);
1112         bfe_writephy(sc, 27, val | (1 << 6));
1113
1114         return(0);
1115 }
1116
1117 static void 
1118 bfe_stats_update(struct bfe_softc *sc)
1119 {
1120         u_long reg;
1121         uint32_t *val;
1122
1123         val = &sc->bfe_hwstats.tx_good_octets;
1124         for (reg = BFE_TX_GOOD_O; reg <= BFE_TX_PAUSE; reg += 4)
1125                 *val++ += CSR_READ_4(sc, reg);
1126         val = &sc->bfe_hwstats.rx_good_octets;
1127         for (reg = BFE_RX_GOOD_O; reg <= BFE_RX_NPAUSE; reg += 4)
1128                 *val++ += CSR_READ_4(sc, reg);
1129 }
1130
1131 static void
1132 bfe_txeof(struct bfe_softc *sc)
1133 {
1134         struct ifnet *ifp = &sc->arpcom.ac_if;
1135         uint32_t i, chipidx;
1136
1137         chipidx = CSR_READ_4(sc, BFE_DMATX_STAT) & BFE_STAT_CDMASK;
1138         chipidx /= sizeof(struct bfe_desc);
1139
1140         i = sc->bfe_tx_cons;
1141         /* Go through the mbufs and free those that have been transmitted */
1142         while (i != chipidx) {
1143                 struct bfe_data *r = &sc->bfe_tx_ring[i];
1144
1145                 bus_dmamap_unload(sc->bfe_txbuf_tag, r->bfe_map);
1146                 if (r->bfe_mbuf != NULL) {
1147                         ifp->if_opackets++;
1148                         m_freem(r->bfe_mbuf);
1149                         r->bfe_mbuf = NULL;
1150                 }
1151                 sc->bfe_tx_cnt--;
1152                 BFE_INC(i, BFE_TX_LIST_CNT);
1153         }
1154
1155         if (i != sc->bfe_tx_cons) {
1156                 /* we freed up some mbufs */
1157                 sc->bfe_tx_cons = i;
1158                 ifp->if_flags &= ~IFF_OACTIVE;
1159         }
1160         if (sc->bfe_tx_cnt == 0)
1161                 ifp->if_timer = 0;
1162         else
1163                 ifp->if_timer = 5;
1164 }
1165
1166 /* Pass a received packet up the stack */
1167 static void
1168 bfe_rxeof(struct bfe_softc *sc)
1169 {
1170         struct ifnet *ifp = &sc->arpcom.ac_if;
1171         struct mbuf *m;
1172         struct bfe_rxheader *rxheader;
1173         struct bfe_data *r;
1174         uint32_t cons, status, current, len, flags;
1175         struct mbuf_chain chain[MAXCPU];
1176
1177         cons = sc->bfe_rx_cons;
1178         status = CSR_READ_4(sc, BFE_DMARX_STAT);
1179         current = (status & BFE_STAT_CDMASK) / sizeof(struct bfe_desc);
1180
1181         ether_input_chain_init(chain);
1182
1183         while (current != cons) {
1184                 r = &sc->bfe_rx_ring[cons];
1185                 bus_dmamap_sync(sc->bfe_rxbuf_tag, r->bfe_map,
1186                                 BUS_DMASYNC_POSTREAD);
1187
1188                 KKASSERT(r->bfe_mbuf != NULL);
1189                 m = r->bfe_mbuf;
1190                 rxheader = mtod(m, struct bfe_rxheader*);
1191                 len = rxheader->len - ETHER_CRC_LEN;
1192                 flags = rxheader->flags;
1193
1194                 /* flag an error and try again */
1195                 if (len > ETHER_MAX_LEN + 32 || (flags & BFE_RX_FLAG_ERRORS)) {
1196                         ifp->if_ierrors++;
1197                         if (flags & BFE_RX_FLAG_SERR)
1198                                 ifp->if_collisions++;
1199
1200                         bfe_setup_rxdesc(sc, cons);
1201                         BFE_INC(cons, BFE_RX_LIST_CNT);
1202                         continue;
1203                 }
1204
1205                 /* Go past the rx header */
1206                 if (bfe_newbuf(sc, cons, 0) != 0) {
1207                         bfe_setup_rxdesc(sc, cons);
1208                         ifp->if_ierrors++;
1209                         BFE_INC(cons, BFE_RX_LIST_CNT);
1210                         continue;
1211                 }
1212
1213                 m_adj(m, BFE_RX_OFFSET);
1214                 m->m_len = m->m_pkthdr.len = len;
1215
1216                 ifp->if_ipackets++;
1217                 m->m_pkthdr.rcvif = ifp;
1218
1219                 ether_input_chain(ifp, m, chain);
1220                 BFE_INC(cons, BFE_RX_LIST_CNT);
1221         }
1222
1223         ether_input_dispatch(chain);
1224
1225         sc->bfe_rx_cons = cons;
1226 }
1227
1228 static void
1229 bfe_intr(void *xsc)
1230 {
1231         struct bfe_softc *sc = xsc;
1232         struct ifnet *ifp = &sc->arpcom.ac_if;
1233         uint32_t istat, imask, flag;
1234
1235         istat = CSR_READ_4(sc, BFE_ISTAT);
1236         imask = CSR_READ_4(sc, BFE_IMASK);
1237
1238         /* 
1239          * Defer unsolicited interrupts - This is necessary because setting the
1240          * chips interrupt mask register to 0 doesn't actually stop the
1241          * interrupts
1242          */
1243         istat &= imask;
1244         CSR_WRITE_4(sc, BFE_ISTAT, istat);
1245         CSR_READ_4(sc, BFE_ISTAT);
1246
1247         /* not expecting this interrupt, disregard it */
1248         if (istat == 0) {
1249                 return;
1250         }
1251
1252         if (istat & BFE_ISTAT_ERRORS) {
1253                 flag = CSR_READ_4(sc, BFE_DMATX_STAT);
1254                 if (flag & BFE_STAT_EMASK)
1255                         ifp->if_oerrors++;
1256
1257                 flag = CSR_READ_4(sc, BFE_DMARX_STAT);
1258                 if (flag & BFE_RX_FLAG_ERRORS)
1259                         ifp->if_ierrors++;
1260
1261                 ifp->if_flags &= ~IFF_RUNNING;
1262                 bfe_init(sc);
1263         }
1264
1265         /* A packet was received */
1266         if (istat & BFE_ISTAT_RX)
1267                 bfe_rxeof(sc);
1268
1269         /* A packet was sent */
1270         if (istat & BFE_ISTAT_TX)
1271                 bfe_txeof(sc);
1272
1273         /* We have packets pending, fire them out */ 
1274         if ((ifp->if_flags & IFF_RUNNING) && !ifq_is_empty(&ifp->if_snd))
1275                 if_devstart(ifp);
1276 }
1277
1278 static int
1279 bfe_encap(struct bfe_softc *sc, struct mbuf **m_head, uint32_t *txidx)
1280 {
1281         struct bfe_desc *d = NULL;
1282         struct bfe_data *r = NULL;
1283         struct mbuf *m;
1284         uint32_t frag, cur, cnt = 0;
1285         int error, chainlen = 0;
1286
1287         KKASSERT(BFE_TX_LIST_CNT >= (2 + sc->bfe_tx_cnt));
1288
1289         /*
1290          * Count the number of frags in this chain to see if
1291          * we need to m_defrag.  Since the descriptor list is shared
1292          * by all packets, we'll m_defrag long chains so that they
1293          * do not use up the entire list, even if they would fit.
1294          */
1295         for (m = *m_head; m != NULL; m = m->m_next)
1296                 chainlen++;
1297
1298         if (chainlen > (BFE_TX_LIST_CNT / 4) ||
1299             BFE_TX_LIST_CNT < (2 + chainlen + sc->bfe_tx_cnt)) {
1300                 m = m_defrag(*m_head, MB_DONTWAIT);
1301                 if (m == NULL) {
1302                         m_freem(*m_head);
1303                         return (ENOBUFS);
1304                 }
1305                 *m_head = m;
1306         }
1307
1308         /*
1309          * Start packing the mbufs in this chain into
1310          * the fragment pointers. Stop when we run out
1311          * of fragments or hit the end of the mbuf chain.
1312          */
1313         cur = frag = *txidx;
1314         cnt = 0;
1315
1316         for (m = *m_head; m != NULL; m = m->m_next) {
1317                 if (m->m_len != 0) {
1318                         KKASSERT(BFE_TX_LIST_CNT >= (2 + sc->bfe_tx_cnt + cnt));
1319
1320                         d = &sc->bfe_tx_list[cur];
1321                         r = &sc->bfe_tx_ring[cur];
1322                         d->bfe_ctrl = BFE_DESC_LEN & m->m_len;
1323                         /* always intterupt on completion */
1324                         d->bfe_ctrl |= BFE_DESC_IOC;
1325                         if (cnt == 0) {
1326                                 /* Set start of frame */
1327                                 d->bfe_ctrl |= BFE_DESC_SOF;
1328                         }
1329                         if (cur == BFE_TX_LIST_CNT - 1) {
1330                                 /*
1331                                  * Tell the chip to wrap to the start of the
1332                                  * descriptor list
1333                                  */
1334                                 d->bfe_ctrl |= BFE_DESC_EOT;
1335                         }
1336
1337                         error = bus_dmamap_load(sc->bfe_txbuf_tag, r->bfe_map,
1338                                                 mtod(m, void *), m->m_len,
1339                                                 bfe_dma_map_desc, d,
1340                                                 BUS_DMA_NOWAIT);
1341                         if (error) {
1342                                 /* XXX This should be a fatal error. */
1343                                 if_printf(&sc->arpcom.ac_if,
1344                                           "%s bus_dmamap_load failed: %d",
1345                                           __func__, error);
1346                                 m_freem(*m_head);
1347                                 return (ENOBUFS);
1348                         }
1349
1350                         bus_dmamap_sync(sc->bfe_txbuf_tag, r->bfe_map,
1351                                         BUS_DMASYNC_PREWRITE);
1352
1353                         frag = cur;
1354                         BFE_INC(cur, BFE_TX_LIST_CNT);
1355                         cnt++;
1356                 }
1357         }
1358
1359         sc->bfe_tx_list[frag].bfe_ctrl |= BFE_DESC_EOF;
1360         sc->bfe_tx_ring[frag].bfe_mbuf = *m_head;
1361         bus_dmamap_sync(sc->bfe_tx_tag, sc->bfe_tx_map, BUS_DMASYNC_PREWRITE);
1362
1363         *txidx = cur;
1364         sc->bfe_tx_cnt += cnt;
1365         return(0);
1366 }
1367
1368 /*
1369  * Set up to transmit a packet
1370  */
1371 static void
1372 bfe_start(struct ifnet *ifp)
1373 {
1374         struct bfe_softc *sc = ifp->if_softc;
1375         struct mbuf *m_head = NULL;
1376         int idx, need_trans;
1377
1378         ASSERT_SERIALIZED(ifp->if_serializer);
1379
1380         /* 
1381          * Not much point trying to send if the link is down
1382          * or we have nothing to send.
1383          */
1384         if (!sc->bfe_link) {
1385                 ifq_purge(&ifp->if_snd);
1386                 return;
1387         }
1388
1389         if (ifp->if_flags & IFF_OACTIVE)
1390                 return;
1391
1392         idx = sc->bfe_tx_prod;
1393
1394         need_trans = 0;
1395         while (sc->bfe_tx_ring[idx].bfe_mbuf == NULL) {
1396                 if (BFE_TX_LIST_CNT < (2 + sc->bfe_tx_cnt)) {
1397                         ifp->if_flags |= IFF_OACTIVE;
1398                         break;
1399                 }
1400
1401                 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1402                 if (m_head == NULL)
1403                         break;
1404
1405                 /* 
1406                  * Pack the data into the tx ring.  If we don't have
1407                  * enough room, let the chip drain the ring.
1408                  */
1409                 if (bfe_encap(sc, &m_head, &idx)) {
1410                         ifp->if_flags |= IFF_OACTIVE;
1411                         break;
1412                 }
1413                 need_trans = 1;
1414
1415                 /*
1416                  * If there's a BPF listener, bounce a copy of this frame
1417                  * to him.
1418                  */
1419                 BPF_MTAP(ifp, m_head);
1420         }
1421
1422         if (!need_trans)
1423                 return;
1424
1425         sc->bfe_tx_prod = idx;
1426         /* Transmit - twice due to apparent hardware bug */
1427         CSR_WRITE_4(sc, BFE_DMATX_PTR, idx * sizeof(struct bfe_desc));
1428         CSR_WRITE_4(sc, BFE_DMATX_PTR, idx * sizeof(struct bfe_desc));
1429
1430         /*
1431          * Set a timeout in case the chip goes out to lunch.
1432          */
1433         ifp->if_timer = 5;
1434 }
1435
1436 static void
1437 bfe_init(void *xsc)
1438 {
1439         struct bfe_softc *sc = (struct bfe_softc*)xsc;
1440         struct ifnet *ifp = &sc->arpcom.ac_if;
1441
1442         ASSERT_SERIALIZED(ifp->if_serializer);
1443
1444         if (ifp->if_flags & IFF_RUNNING)
1445                 return;
1446
1447         bfe_stop(sc);
1448         bfe_chip_reset(sc);
1449
1450         if (bfe_list_rx_init(sc) == ENOBUFS) {
1451                 if_printf(ifp, "bfe_init failed. "
1452                           " Not enough memory for list buffers\n");
1453                 bfe_stop(sc);
1454                 return;
1455         }
1456
1457         bfe_set_rx_mode(sc);
1458
1459         /* Enable the chip and core */
1460         BFE_OR(sc, BFE_ENET_CTRL, BFE_ENET_ENABLE);
1461         /* Enable interrupts */
1462         CSR_WRITE_4(sc, BFE_IMASK, BFE_IMASK_DEF);
1463
1464         bfe_ifmedia_upd(ifp);
1465         ifp->if_flags |= IFF_RUNNING;
1466         ifp->if_flags &= ~IFF_OACTIVE;
1467
1468         callout_reset(&sc->bfe_stat_timer, hz, bfe_tick, sc);
1469 }
1470
1471 /*
1472  * Set media options.
1473  */
1474 static int
1475 bfe_ifmedia_upd(struct ifnet *ifp)
1476 {
1477         struct bfe_softc *sc = ifp->if_softc;
1478         struct mii_data *mii;
1479
1480         ASSERT_SERIALIZED(ifp->if_serializer);
1481
1482         mii = device_get_softc(sc->bfe_miibus);
1483         sc->bfe_link = 0;
1484         if (mii->mii_instance) {
1485                 struct mii_softc *miisc;
1486                 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1487                                 miisc = LIST_NEXT(miisc, mii_list))
1488                         mii_phy_reset(miisc);
1489         }
1490         mii_mediachg(mii);
1491
1492         bfe_setupphy(sc);
1493
1494         return(0);
1495 }
1496
1497 /*
1498  * Report current media status.
1499  */
1500 static void
1501 bfe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1502 {
1503         struct bfe_softc *sc = ifp->if_softc;
1504         struct mii_data *mii;
1505
1506         ASSERT_SERIALIZED(ifp->if_serializer);
1507
1508         mii = device_get_softc(sc->bfe_miibus);
1509         mii_pollstat(mii);
1510         ifmr->ifm_active = mii->mii_media_active;
1511         ifmr->ifm_status = mii->mii_media_status;
1512 }
1513
1514 static int
1515 bfe_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
1516 {
1517         struct bfe_softc *sc = ifp->if_softc;
1518         struct ifreq *ifr = (struct ifreq *) data;
1519         struct mii_data *mii;
1520         int error = 0;
1521
1522         ASSERT_SERIALIZED(ifp->if_serializer);
1523
1524         switch (command) {
1525                 case SIOCSIFFLAGS:
1526                         if (ifp->if_flags & IFF_UP)
1527                                 if (ifp->if_flags & IFF_RUNNING)
1528                                         bfe_set_rx_mode(sc);
1529                                 else
1530                                         bfe_init(sc);
1531                         else if (ifp->if_flags & IFF_RUNNING)
1532                                 bfe_stop(sc);
1533                         break;
1534                 case SIOCADDMULTI:
1535                 case SIOCDELMULTI:
1536                         if (ifp->if_flags & IFF_RUNNING)
1537                                 bfe_set_rx_mode(sc);
1538                         break;
1539                 case SIOCGIFMEDIA:
1540                 case SIOCSIFMEDIA:
1541                         mii = device_get_softc(sc->bfe_miibus);
1542                         error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
1543                                               command);
1544                         break;
1545                 default:
1546                         error = ether_ioctl(ifp, command, data);
1547                         break;
1548         }
1549         return error;
1550 }
1551
1552 static void
1553 bfe_watchdog(struct ifnet *ifp)
1554 {
1555         struct bfe_softc *sc = ifp->if_softc;
1556
1557         ASSERT_SERIALIZED(ifp->if_serializer);
1558
1559         if_printf(ifp, "watchdog timeout -- resetting\n");
1560
1561         ifp->if_flags &= ~IFF_RUNNING;
1562         bfe_init(sc);
1563
1564         ifp->if_oerrors++;
1565 }
1566
1567 static void
1568 bfe_tick(void *xsc)
1569 {
1570         struct bfe_softc *sc = xsc;
1571         struct mii_data *mii;
1572         struct ifnet *ifp = &sc->arpcom.ac_if;
1573
1574         mii = device_get_softc(sc->bfe_miibus);
1575
1576         lwkt_serialize_enter(ifp->if_serializer);
1577
1578         bfe_stats_update(sc);
1579         callout_reset(&sc->bfe_stat_timer, hz, bfe_tick, sc);
1580
1581         if (sc->bfe_link == 0) {
1582                 mii_tick(mii);
1583                 if (!sc->bfe_link && mii->mii_media_status & IFM_ACTIVE &&
1584                     IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)  {
1585                         sc->bfe_link++;
1586                 }
1587                 if (!sc->bfe_link)
1588                         sc->bfe_link++;
1589         }
1590         lwkt_serialize_exit(ifp->if_serializer);
1591 }
1592
1593 /*
1594  * Stop the adapter and free any mbufs allocated to the
1595  * RX and TX lists.
1596  */
1597 static void
1598 bfe_stop(struct bfe_softc *sc)
1599 {
1600         struct ifnet *ifp = &sc->arpcom.ac_if;
1601
1602         ASSERT_SERIALIZED(ifp->if_serializer);
1603
1604         callout_stop(&sc->bfe_stat_timer);
1605
1606         bfe_chip_halt(sc);
1607         bfe_tx_ring_free(sc);
1608         bfe_rx_ring_free(sc);
1609
1610         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1611 }
1612
1613 static void
1614 bfe_dmamap_buf_cb(void *xctx, bus_dma_segment_t *segs, int nsegs,
1615                   bus_size_t mapsz __unused, int error)
1616 {
1617         struct bfe_dmamap_ctx *ctx = xctx;
1618         int i;
1619
1620         if (error)
1621                 return;
1622
1623         if (nsegs > ctx->nsegs) {
1624                 ctx->nsegs = 0;
1625                 return;
1626         }
1627
1628         ctx->nsegs = nsegs;
1629         for (i = 0; i < nsegs; ++i)
1630                 ctx->segs[i] = segs[i];
1631 }