Deleted Added
sdiff udiff text old ( 153234 ) new ( 153239 )
full compact
1/*-
2 * Copyright (c) 2001 Wind River Systems
3 * Copyright (c) 1997, 1998, 1999, 2001
4 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

--- 18 unchanged lines hidden (view full) ---

27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31 * THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/dev/bge/if_bge.c 153234 2005-12-08 13:31:52Z oleg $");
36
37/*
38 * Broadcom BCM570x family gigabit ethernet driver for FreeBSD.
39 *
40 * The Broadcom BCM5700 is based on technology originally developed by
41 * Alteon Networks as part of the Tigon I and Tigon II gigabit ethernet
42 * MAC chips. The BCM5700, sometimes refered to as the Tigon III, has
43 * two on-board MIPS R4000 CPUs and can have as much as 16MB of external

--- 30 unchanged lines hidden (view full) ---

74#include <sys/endian.h>
75#include <sys/systm.h>
76#include <sys/sockio.h>
77#include <sys/mbuf.h>
78#include <sys/malloc.h>
79#include <sys/kernel.h>
80#include <sys/module.h>
81#include <sys/socket.h>
82#include <sys/queue.h>
83
84#include <net/if.h>
85#include <net/if_arp.h>
86#include <net/ethernet.h>
87#include <net/if_dl.h>
88#include <net/if_media.h>
89
90#include <net/bpf.h>

--- 141 unchanged lines hidden (view full) ---

232static void bge_ifmedia_sts (struct ifnet *, struct ifmediareq *);
233
234static u_int8_t bge_eeprom_getbyte (struct bge_softc *, int, u_int8_t *);
235static int bge_read_eeprom (struct bge_softc *, caddr_t, int, int);
236
237static void bge_setmulti (struct bge_softc *);
238
239static void bge_handle_events (struct bge_softc *);
240static int bge_alloc_jumbo_mem (struct bge_softc *);
241static void bge_free_jumbo_mem (struct bge_softc *);
242static void *bge_jalloc (struct bge_softc *);
243static void bge_jfree (void *, void *);
244static int bge_newbuf_std (struct bge_softc *, int, struct mbuf *);
245static int bge_newbuf_jumbo (struct bge_softc *, int, struct mbuf *);
246static int bge_init_rx_ring_std (struct bge_softc *);
247static void bge_free_rx_ring_std (struct bge_softc *);
248static int bge_init_rx_ring_jumbo (struct bge_softc *);
249static void bge_free_rx_ring_jumbo (struct bge_softc *);
250static void bge_free_tx_ring (struct bge_softc *);
251static int bge_init_tx_ring (struct bge_softc *);

--- 191 unchanged lines hidden (view full) ---

443
444 d->bge_flags |= htole16(BGE_TXBDFLAG_END);
445 ctx->bge_maxsegs = nseg;
446 ctx->bge_idx = idx;
447
448 return;
449}
450
451
452#ifdef notdef
453static u_int8_t
454bge_vpd_readbyte(sc, addr)
455 struct bge_softc *sc;
456 int addr;
457{
458 int i;
459 device_t dev;

--- 282 unchanged lines hidden (view full) ---

742bge_handle_events(sc)
743 struct bge_softc *sc;
744{
745
746 return;
747}
748
749/*
750 * Memory management for jumbo frames.
751 */
752
753static int
754bge_alloc_jumbo_mem(sc)
755 struct bge_softc *sc;
756{
757 caddr_t ptr;
758 register int i, error;
759 struct bge_jpool_entry *entry;
760
761 /* Create tag for jumbo buffer block */
762
763 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
764 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
765 NULL, BGE_JMEM, 1, BGE_JMEM, 0, NULL, NULL,
766 &sc->bge_cdata.bge_jumbo_tag);
767
768 if (error) {
769 printf("bge%d: could not allocate jumbo dma tag\n",
770 sc->bge_unit);
771 return (ENOMEM);
772 }
773
774 /* Allocate DMA'able memory for jumbo buffer block */
775
776 error = bus_dmamem_alloc(sc->bge_cdata.bge_jumbo_tag,
777 (void **)&sc->bge_ldata.bge_jumbo_buf, BUS_DMA_NOWAIT,
778 &sc->bge_cdata.bge_jumbo_map);
779
780 if (error)
781 return (ENOMEM);
782
783 SLIST_INIT(&sc->bge_jfree_listhead);
784 SLIST_INIT(&sc->bge_jinuse_listhead);
785
786 /*
787 * Now divide it up into 9K pieces and save the addresses
788 * in an array.
789 */
790 ptr = sc->bge_ldata.bge_jumbo_buf;
791 for (i = 0; i < BGE_JSLOTS; i++) {
792 sc->bge_cdata.bge_jslots[i] = ptr;
793 ptr += BGE_JLEN;
794 entry = malloc(sizeof(struct bge_jpool_entry),
795 M_DEVBUF, M_NOWAIT);
796 if (entry == NULL) {
797 bge_free_jumbo_mem(sc);
798 sc->bge_ldata.bge_jumbo_buf = NULL;
799 printf("bge%d: no memory for jumbo "
800 "buffer queue!\n", sc->bge_unit);
801 return(ENOBUFS);
802 }
803 entry->slot = i;
804 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead,
805 entry, jpool_entries);
806 }
807
808 return(0);
809}
810
811static void
812bge_free_jumbo_mem(sc)
813 struct bge_softc *sc;
814{
815 int i;
816 struct bge_jpool_entry *entry;
817
818 for (i = 0; i < BGE_JSLOTS; i++) {
819 entry = SLIST_FIRST(&sc->bge_jfree_listhead);
820 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
821 free(entry, M_DEVBUF);
822 }
823
824 /* Destroy jumbo buffer block */
825
826 if (sc->bge_ldata.bge_rx_jumbo_ring)
827 bus_dmamem_free(sc->bge_cdata.bge_jumbo_tag,
828 sc->bge_ldata.bge_jumbo_buf,
829 sc->bge_cdata.bge_jumbo_map);
830
831 if (sc->bge_cdata.bge_rx_jumbo_ring_map)
832 bus_dmamap_destroy(sc->bge_cdata.bge_jumbo_tag,
833 sc->bge_cdata.bge_jumbo_map);
834
835 if (sc->bge_cdata.bge_jumbo_tag)
836 bus_dma_tag_destroy(sc->bge_cdata.bge_jumbo_tag);
837
838 return;
839}
840
841/*
842 * Allocate a jumbo buffer.
843 */
844static void *
845bge_jalloc(sc)
846 struct bge_softc *sc;
847{
848 struct bge_jpool_entry *entry;
849
850 entry = SLIST_FIRST(&sc->bge_jfree_listhead);
851
852 if (entry == NULL) {
853 printf("bge%d: no free jumbo buffers\n", sc->bge_unit);
854 return(NULL);
855 }
856
857 SLIST_REMOVE_HEAD(&sc->bge_jfree_listhead, jpool_entries);
858 SLIST_INSERT_HEAD(&sc->bge_jinuse_listhead, entry, jpool_entries);
859 return(sc->bge_cdata.bge_jslots[entry->slot]);
860}
861
862/*
863 * Release a jumbo buffer.
864 */
865static void
866bge_jfree(buf, args)
867 void *buf;
868 void *args;
869{
870 struct bge_jpool_entry *entry;
871 struct bge_softc *sc;
872 int i;
873
874 /* Extract the softc struct pointer. */
875 sc = (struct bge_softc *)args;
876
877 if (sc == NULL)
878 panic("bge_jfree: can't find softc pointer!");
879
880 /* calculate the slot this buffer belongs to */
881
882 i = ((vm_offset_t)buf
883 - (vm_offset_t)sc->bge_ldata.bge_jumbo_buf) / BGE_JLEN;
884
885 if ((i < 0) || (i >= BGE_JSLOTS))
886 panic("bge_jfree: asked to free buffer that we don't manage!");
887
888 entry = SLIST_FIRST(&sc->bge_jinuse_listhead);
889 if (entry == NULL)
890 panic("bge_jfree: buffer not in use!");
891 entry->slot = i;
892 SLIST_REMOVE_HEAD(&sc->bge_jinuse_listhead, jpool_entries);
893 SLIST_INSERT_HEAD(&sc->bge_jfree_listhead, entry, jpool_entries);
894
895 return;
896}
897
898
899/*
900 * Intialize a standard receive ring descriptor.
901 */
902static int
903bge_newbuf_std(sc, i, m)
904 struct bge_softc *sc;
905 int i;
906 struct mbuf *m;
907{

--- 54 unchanged lines hidden (view full) ---

962 * a jumbo buffer from the pool managed internally by the driver.
963 */
964static int
965bge_newbuf_jumbo(sc, i, m)
966 struct bge_softc *sc;
967 int i;
968 struct mbuf *m;
969{
970 struct mbuf *m_new = NULL;
971 struct bge_rx_bd *r;
972 struct bge_dmamap_arg ctx;
973 int error;
974
975 if (m == NULL) {
976 caddr_t *buf = NULL;
977
978 /* Allocate the mbuf. */
979 MGETHDR(m_new, M_DONTWAIT, MT_DATA);
980 if (m_new == NULL) {
981 return(ENOBUFS);
982 }
983
984 /* Allocate the jumbo buffer */
985 buf = bge_jalloc(sc);
986 if (buf == NULL) {
987 m_freem(m_new);
988 printf("bge%d: jumbo allocation failed "
989 "-- packet dropped!\n", sc->bge_unit);
990 return(ENOBUFS);
991 }
992
993 /* Attach the buffer to the mbuf. */
994 m_new->m_data = (void *) buf;
995 m_new->m_len = m_new->m_pkthdr.len = BGE_JUMBO_FRAMELEN;
996 MEXTADD(m_new, buf, BGE_JUMBO_FRAMELEN, bge_jfree,
997 (struct bge_softc *)sc, 0, EXT_NET_DRV);
998 } else {
999 m_new = m;
1000 m_new->m_data = m_new->m_ext.ext_buf;
1001 m_new->m_ext.ext_size = BGE_JUMBO_FRAMELEN;
1002 }
1003
1004 if (!sc->bge_rx_alignment_bug)
1005 m_adj(m_new, ETHER_ALIGN);
1006 /* Set up the descriptor. */
1007 sc->bge_cdata.bge_rx_jumbo_chain[i] = m_new;
1008 r = &sc->bge_ldata.bge_rx_jumbo_ring[i];
1009 ctx.bge_maxsegs = 1;
1010 ctx.sc = sc;
1011 error = bus_dmamap_load(sc->bge_cdata.bge_mtag_jumbo,
1012 sc->bge_cdata.bge_rx_jumbo_dmamap[i], mtod(m_new, void *),
1013 m_new->m_len, bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
1014 if (error || ctx.bge_maxsegs == 0) {
1015 if (m == NULL) {
1016 sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1017 m_freem(m_new);
1018 }
1019 return(ENOMEM);
1020 }
1021 r->bge_addr.bge_addr_lo = htole32(BGE_ADDR_LO(ctx.bge_busaddr));
1022 r->bge_addr.bge_addr_hi = htole32(BGE_ADDR_HI(ctx.bge_busaddr));
1023 r->bge_flags = htole16(BGE_RXBDFLAG_END|BGE_RXBDFLAG_JUMBO_RING);
1024 r->bge_len = htole16(m_new->m_len);
1025 r->bge_idx = htole16(i);
1026
1027 bus_dmamap_sync(sc->bge_cdata.bge_mtag,
1028 sc->bge_cdata.bge_rx_jumbo_dmamap[i],
1029 BUS_DMASYNC_PREREAD);
1030
1031 return(0);
1032}
1033
1034/*
1035 * The standard receive ring has 512 entries in it. At 2K per mbuf cluster,
1036 * that's 1MB or memory, which is a lot. For now, we fill only the first
1037 * 256 ring entries and hope that our CPU is fast enough to keep up with
1038 * the NIC.
1039 */

--- 37 unchanged lines hidden (view full) ---

1077
1078 return;
1079}
1080
1081static int
1082bge_init_rx_ring_jumbo(sc)
1083 struct bge_softc *sc;
1084{
1085 int i;
1086 struct bge_rcb *rcb;
1087
1088 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1089 if (bge_newbuf_jumbo(sc, i, NULL) == ENOBUFS)
1090 return(ENOBUFS);
1091 };
1092
1093 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1094 sc->bge_cdata.bge_rx_jumbo_ring_map,
1095 BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
1096
1097 sc->bge_jumbo = i - 1;
1098
1099 rcb = &sc->bge_ldata.bge_info.bge_jumbo_rx_rcb;
1100 rcb->bge_maxlen_flags = BGE_RCB_MAXLEN_FLAGS(0, 0);
1101 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_MAXLEN_FLAGS, rcb->bge_maxlen_flags);
1102
1103 CSR_WRITE_4(sc, BGE_MBX_RX_JUMBO_PROD_LO, sc->bge_jumbo);
1104
1105 return(0);
1106}
1107
1108static void

--- 5 unchanged lines hidden (view full) ---

1114 for (i = 0; i < BGE_JUMBO_RX_RING_CNT; i++) {
1115 if (sc->bge_cdata.bge_rx_jumbo_chain[i] != NULL) {
1116 m_freem(sc->bge_cdata.bge_rx_jumbo_chain[i]);
1117 sc->bge_cdata.bge_rx_jumbo_chain[i] = NULL;
1118 bus_dmamap_unload(sc->bge_cdata.bge_mtag_jumbo,
1119 sc->bge_cdata.bge_rx_jumbo_dmamap[i]);
1120 }
1121 bzero((char *)&sc->bge_ldata.bge_rx_jumbo_ring[i],
1122 sizeof(struct bge_rx_bd));
1123 }
1124
1125 return;
1126}
1127
1128static void
1129bge_free_tx_ring(sc)
1130 struct bge_softc *sc;

--- 334 unchanged lines hidden (view full) ---

1465
1466 rcb->bge_hostaddr.bge_addr_lo =
1467 BGE_ADDR_LO(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1468 rcb->bge_hostaddr.bge_addr_hi =
1469 BGE_ADDR_HI(sc->bge_ldata.bge_rx_jumbo_ring_paddr);
1470 bus_dmamap_sync(sc->bge_cdata.bge_rx_jumbo_ring_tag,
1471 sc->bge_cdata.bge_rx_jumbo_ring_map,
1472 BUS_DMASYNC_PREREAD);
1473 rcb->bge_maxlen_flags =
1474 BGE_RCB_MAXLEN_FLAGS(BGE_MAX_FRAMELEN,
1475 BGE_RCB_FLAG_RING_DISABLED);
1476 if (sc->bge_extram)
1477 rcb->bge_nicaddr = BGE_EXT_JUMBO_RX_RINGS;
1478 else
1479 rcb->bge_nicaddr = BGE_JUMBO_RX_RINGS;
1480 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_HI,
1481 rcb->bge_hostaddr.bge_addr_hi);
1482 CSR_WRITE_4(sc, BGE_RX_JUMBO_RCB_HADDR_LO,
1483 rcb->bge_hostaddr.bge_addr_lo);

--- 454 unchanged lines hidden (view full) ---

1938 return;
1939}
1940
1941static int
1942bge_dma_alloc(dev)
1943 device_t dev;
1944{
1945 struct bge_softc *sc;
1946 int nseg, i, error;
1947 struct bge_dmamap_arg ctx;
1948
1949 sc = device_get_softc(dev);
1950
1951 /*
1952 * Allocate the parent bus DMA tag appropriate for PCI.
1953 */
1954#define BGE_NSEG_NEW 32
1955 error = bus_dma_tag_create(NULL, /* parent */
1956 PAGE_SIZE, 0, /* alignment, boundary */
1957 BUS_SPACE_MAXADDR, /* lowaddr */
1958 BUS_SPACE_MAXADDR, /* highaddr */
1959 NULL, NULL, /* filter, filterarg */
1960 MAXBSIZE, BGE_NSEG_NEW, /* maxsize, nsegments */
1961 BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1962 0, /* flags */
1963 NULL, NULL, /* lockfunc, lockarg */
1964 &sc->bge_cdata.bge_parent_tag);
1965
1966 /*
1967 * Create tag for RX mbufs.
1968 */
1969 nseg = 32;
1970 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag, 1,
1971 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1972 NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL,
1973 &sc->bge_cdata.bge_mtag);
1974
1975 if (error) {
1976 device_printf(dev, "could not allocate dma tag\n");
1977 return (ENOMEM);
1978 }
1979
1980 /* Create DMA maps for RX buffers */
1981

--- 70 unchanged lines hidden (view full) ---

2052 * a single segment. I think eventually the driver should
2053 * be changed so that it uses ordinary mbufs and cluster
2054 * buffers, i.e. jumbo frames can span multiple DMA
2055 * descriptors. But that's a project for another day.
2056 */
2057
2058 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2059 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2060 NULL, MCLBYTES * nseg, nseg, BGE_JLEN, 0, NULL, NULL,
2061 &sc->bge_cdata.bge_mtag_jumbo);
2062
2063 if (error) {
2064 device_printf(dev, "could not allocate dma tag\n");
2065 return (ENOMEM);
2066 }
2067
2068 /* Create tag for jumbo RX ring */
2069
2070 error = bus_dma_tag_create(sc->bge_cdata.bge_parent_tag,
2071 PAGE_SIZE, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
2072 NULL, BGE_JUMBO_RX_RING_SZ, 1, BGE_JUMBO_RX_RING_SZ, 0,
2073 NULL, NULL, &sc->bge_cdata.bge_rx_jumbo_ring_tag);
2074
2075 if (error) {
2076 device_printf(dev, "could not allocate dma tag\n");
2077 return (ENOMEM);
2078 }
2079
2080 /* Allocate DMA'able memory for jumbo RX ring */
2081
2082 error = bus_dmamem_alloc(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2083 (void **)&sc->bge_ldata.bge_rx_jumbo_ring, BUS_DMA_NOWAIT,
2084 &sc->bge_cdata.bge_rx_jumbo_ring_map);
2085 if (error)
2086 return (ENOMEM);
2087
2088 bzero((char *)sc->bge_ldata.bge_rx_jumbo_ring,
2089 BGE_JUMBO_RX_RING_SZ);
2090
2091 /* Load the address of the jumbo RX ring */
2092
2093 ctx.bge_maxsegs = 1;
2094 ctx.sc = sc;
2095
2096 error = bus_dmamap_load(sc->bge_cdata.bge_rx_jumbo_ring_tag,
2097 sc->bge_cdata.bge_rx_jumbo_ring_map,
2098 sc->bge_ldata.bge_rx_jumbo_ring, BGE_JUMBO_RX_RING_SZ,
2099 bge_dma_map_addr, &ctx, BUS_DMA_NOWAIT);
2100

--- 287 unchanged lines hidden (view full) ---

2388 if (bge_dma_alloc(dev)) {
2389 printf ("bge%d: failed to allocate DMA resources\n",
2390 sc->bge_unit);
2391 bge_release_resources(sc);
2392 error = ENXIO;
2393 goto fail;
2394 }
2395
2396 /*
2397 * Try to allocate memory for jumbo buffers.
2398 * The 5705 does not appear to support jumbo frames.
2399 */
2400 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2401 sc->bge_asicrev != BGE_ASICREV_BCM5750) {
2402 if (bge_alloc_jumbo_mem(sc)) {
2403 printf("bge%d: jumbo buffer allocation "
2404 "failed\n", sc->bge_unit);
2405 bge_release_resources(sc);
2406 error = ENXIO;
2407 goto fail;
2408 }
2409 }
2410
2411 /* Set default tuneable values. */
2412 sc->bge_stat_ticks = BGE_TICKS_PER_SEC;
2413 sc->bge_rx_coal_ticks = 150;
2414 sc->bge_tx_coal_ticks = 150;
2415 sc->bge_rx_max_coal_bds = 64;
2416 sc->bge_tx_max_coal_bds = 128;
2417
2418 /* Set up ifnet structure */

--- 60 unchanged lines hidden (view full) ---

2479 } else {
2480 /*
2481 * Do transceiver setup.
2482 */
2483 if (mii_phy_probe(dev, &sc->bge_miibus,
2484 bge_ifmedia_upd, bge_ifmedia_sts)) {
2485 printf("bge%d: MII without any PHY!\n", sc->bge_unit);
2486 bge_release_resources(sc);
2487 bge_free_jumbo_mem(sc);
2488 error = ENXIO;
2489 goto fail;
2490 }
2491 }
2492
2493 /*
2494 * When using the BCM5701 in PCI-X mode, data corruption has
2495 * been observed in the first few bytes of some received packets.

--- 61 unchanged lines hidden (view full) ---

2557 if (sc->bge_tbi) {
2558 ifmedia_removeall(&sc->bge_ifmedia);
2559 } else {
2560 bus_generic_detach(dev);
2561 device_delete_child(dev, sc->bge_miibus);
2562 }
2563
2564 bge_release_resources(sc);
2565 if (sc->bge_asicrev != BGE_ASICREV_BCM5705 &&
2566 sc->bge_asicrev != BGE_ASICREV_BCM5750)
2567 bge_free_jumbo_mem(sc);
2568
2569 return(0);
2570}
2571
2572static void
2573bge_release_resources(sc)
2574 struct bge_softc *sc;
2575{

--- 158 unchanged lines hidden (view full) ---

2734 return;
2735}
2736
2737/*
2738 * Frame reception handling. This is called if there's a frame
2739 * on the receive return list.
2740 *
2741 * Note: we have to be able to handle two possibilities here:
2742 * 1) the frame is from the jumbo recieve ring
2743 * 2) the frame is from the standard receive ring
2744 */
2745
2746static void
2747bge_rxeof(sc)
2748 struct bge_softc *sc;
2749{
2750 struct ifnet *ifp;

--- 1204 unchanged lines hidden ---