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