]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/net/rtl8169.c
net: rtl8169: Build warning fixes for 64-bit
[karo-tx-uboot.git] / drivers / net / rtl8169.c
index c3ce17516c7407b0f189084bda4cbecb515122ac..958488c19a1c56d7a2f5353a544ca3e1a2d8b561 100644 (file)
@@ -41,6 +41,7 @@
  * Modified to use le32_to_cpu and cpu_to_le32 properly
  */
 #include <common.h>
+#include <errno.h>
 #include <malloc.h>
 #include <net.h>
 #include <netdev.h>
@@ -54,7 +55,7 @@
 #define drv_version "v1.5"
 #define drv_date "01-17-2004"
 
-static u32 ioaddr;
+static unsigned long ioaddr;
 
 /* Condensed operations for readability. */
 #define currticks()    get_timer(0)
@@ -79,7 +80,11 @@ static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
 #define InterFrameGap  0x03    /* 3 means InterFrameGap = the shortest one */
 
 #define NUM_TX_DESC    1       /* Number of Tx descriptor registers */
-#define NUM_RX_DESC    4       /* Number of Rx descriptor registers */
+#ifdef CONFIG_SYS_RX_ETH_BUFFER
+  #define NUM_RX_DESC  CONFIG_SYS_RX_ETH_BUFFER
+#else
+  #define NUM_RX_DESC  4       /* Number of Rx descriptor registers */
+#endif
 #define RX_BUF_SIZE    1536    /* Rx Buffer size */
 #define RX_BUF_LEN     8192
 
@@ -87,19 +92,21 @@ static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
 #define TX_TIMEOUT  (6*HZ)
 
 /* write/read MMIO register. Notice: {read,write}[wl] do the necessary swapping */
-#define RTL_W8(reg, val8)      writeb ((val8), ioaddr + (reg))
-#define RTL_W16(reg, val16)    writew ((val16), ioaddr + (reg))
-#define RTL_W32(reg, val32)    writel ((val32), ioaddr + (reg))
-#define RTL_R8(reg)            readb (ioaddr + (reg))
-#define RTL_R16(reg)           readw (ioaddr + (reg))
-#define RTL_R32(reg)           ((unsigned long) readl (ioaddr + (reg)))
+#define RTL_W8(reg, val8)      writeb((val8), ioaddr + (reg))
+#define RTL_W16(reg, val16)    writew((val16), ioaddr + (reg))
+#define RTL_W32(reg, val32)    writel((val32), ioaddr + (reg))
+#define RTL_R8(reg)            readb(ioaddr + (reg))
+#define RTL_R16(reg)           readw(ioaddr + (reg))
+#define RTL_R32(reg)           readl(ioaddr + (reg))
 
 #define ETH_FRAME_LEN  MAX_ETH_FRAME_SIZE
 #define ETH_ALEN       MAC_ADDR_LEN
 #define ETH_ZLEN       60
 
-#define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, (pci_addr_t)a)
-#define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, (phys_addr_t)a)
+#define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)(unsigned long)dev->priv, \
+       (pci_addr_t)(unsigned long)a)
+#define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)(unsigned long)dev->priv, \
+       (phys_addr_t)a)
 
 enum RTL8169_registers {
        MAC0 = 0,               /* Ethernet hardware address. */
@@ -248,6 +255,7 @@ static struct {
        {"RTL-8168b/8111sb",    0x38, 0xff7e1880,},
        {"RTL-8168d/8111d",     0x28, 0xff7e1880,},
        {"RTL-8168evl/8111evl", 0x2e, 0xff7e1880,},
+       {"RTL-8168/8111g",      0x4c, 0xff7e1880,},
        {"RTL-8101e",           0x34, 0xff7e1880,},
        {"RTL-8100e",           0x32, 0xff7e1880,},
 };
@@ -273,23 +281,40 @@ struct RxDesc {
        u32 buf_Haddr;
 };
 
-/* Define the TX Descriptor */
-static u8 tx_ring[NUM_TX_DESC * sizeof(struct TxDesc) + 256];
-/*     __attribute__ ((aligned(256))); */
+#define RTL8169_DESC_SIZE 16
 
-/* Create a static buffer of size RX_BUF_SZ for each
-TX Descriptor. All descriptors point to a
-part of this buffer */
-static unsigned char txb[NUM_TX_DESC * RX_BUF_SIZE];
+#if ARCH_DMA_MINALIGN > 256
+#  define RTL8169_ALIGN ARCH_DMA_MINALIGN
+#else
+#  define RTL8169_ALIGN 256
+#endif
 
-/* Define the RX Descriptor */
-static u8 rx_ring[NUM_RX_DESC * sizeof(struct TxDesc) + 256];
-  /*  __attribute__ ((aligned(256))); */
+/*
+ * Warn if the cache-line size is larger than the descriptor size. In such
+ * cases the driver will likely fail because the CPU needs to flush the cache
+ * when requeuing RX buffers, therefore descriptors written by the hardware
+ * may be discarded.
+ *
+ * This can be fixed by defining CONFIG_SYS_NONCACHED_MEMORY which will cause
+ * the driver to allocate descriptors from a pool of non-cached memory.
+ */
+#if RTL8169_DESC_SIZE < ARCH_DMA_MINALIGN
+#if !defined(CONFIG_SYS_NONCACHED_MEMORY) && !defined(CONFIG_SYS_DCACHE_OFF)
+#warning cache-line size is larger than descriptor size
+#endif
+#endif
 
-/* Create a static buffer of size RX_BUF_SZ for each
-RX Descriptor  All descriptors point to a
-part of this buffer */
-static unsigned char rxb[NUM_RX_DESC * RX_BUF_SIZE];
+/*
+ * Create a static buffer of size RX_BUF_SZ for each TX Descriptor. All
+ * descriptors point to a part of this buffer.
+ */
+DEFINE_ALIGN_BUFFER(u8, txb, NUM_TX_DESC * RX_BUF_SIZE, RTL8169_ALIGN);
+
+/*
+ * Create a static buffer of size RX_BUF_SZ for each RX Descriptor. All
+ * descriptors point to a part of this buffer.
+ */
+DEFINE_ALIGN_BUFFER(u8, rxb, NUM_RX_DESC * RX_BUF_SIZE, RTL8169_ALIGN);
 
 struct rtl8169_private {
        void *mmio_addr;        /* memory map physical address */
@@ -297,8 +322,6 @@ struct rtl8169_private {
        unsigned long cur_rx;   /* Index into the Rx descriptor buffer of next Rx pkt. */
        unsigned long cur_tx;   /* Index into the Tx descriptor buffer of next Rx pkt. */
        unsigned long dirty_tx;
-       unsigned char *TxDescArrays;    /* Index of Tx Descriptor buffer */
-       unsigned char *RxDescArrays;    /* Index of Rx Descriptor buffer */
        struct TxDesc *TxDescArray;     /* Index of 256-alignment Tx Descriptor buffer */
        struct RxDesc *RxDescArray;     /* Index of 256-alignment Rx Descriptor buffer */
        unsigned char *RxBufferRings;   /* Index of Rx Buffer  */
@@ -397,6 +420,35 @@ match:
        return 0;
 }
 
+/*
+ * TX and RX descriptors are 16 bytes. This causes problems with the cache
+ * maintenance on CPUs where the cache-line size exceeds the size of these
+ * descriptors. What will happen is that when the driver receives a packet
+ * it will be immediately requeued for the hardware to reuse. The CPU will
+ * therefore need to flush the cache-line containing the descriptor, which
+ * will cause all other descriptors in the same cache-line to be flushed
+ * along with it. If one of those descriptors had been written to by the
+ * device those changes (and the associated packet) will be lost.
+ *
+ * To work around this, we make use of non-cached memory if available. If
+ * descriptors are mapped uncached there's no need to manually flush them
+ * or invalidate them.
+ *
+ * Note that this only applies to descriptors. The packet data buffers do
+ * not have the same constraints since they are 1536 bytes large, so they
+ * are unlikely to share cache-lines.
+ */
+static void *rtl_alloc_descs(unsigned int num)
+{
+       size_t size = num * RTL8169_DESC_SIZE;
+
+#ifdef CONFIG_SYS_NONCACHED_MEMORY
+       return (void *)noncached_alloc(size, RTL8169_ALIGN);
+#else
+       return memalign(RTL8169_ALIGN, size);
+#endif
+}
+
 /*
  * Cache maintenance functions. These are simple wrappers around the more
  * general purpose flush_cache() and invalidate_dcache_range() functions.
@@ -404,28 +456,36 @@ match:
 
 static void rtl_inval_rx_desc(struct RxDesc *desc)
 {
+#ifndef CONFIG_SYS_NONCACHED_MEMORY
        unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
        unsigned long end = ALIGN(start + sizeof(*desc), ARCH_DMA_MINALIGN);
 
        invalidate_dcache_range(start, end);
+#endif
 }
 
 static void rtl_flush_rx_desc(struct RxDesc *desc)
 {
+#ifndef CONFIG_SYS_NONCACHED_MEMORY
        flush_cache((unsigned long)desc, sizeof(*desc));
+#endif
 }
 
 static void rtl_inval_tx_desc(struct TxDesc *desc)
 {
+#ifndef CONFIG_SYS_NONCACHED_MEMORY
        unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
        unsigned long end = ALIGN(start + sizeof(*desc), ARCH_DMA_MINALIGN);
 
        invalidate_dcache_range(start, end);
+#endif
 }
 
 static void rtl_flush_tx_desc(struct TxDesc *desc)
 {
+#ifndef CONFIG_SYS_NONCACHED_MEMORY
        flush_cache((unsigned long)desc, sizeof(*desc));
+#endif
 }
 
 static void rtl_inval_buffer(void *buf, size_t size)
@@ -480,7 +540,7 @@ static int rtl_recv(struct eth_device *dev)
                                cpu_to_le32(bus_to_phys(tpc->RxBufferRing[cur_rx]));
                        rtl_flush_rx_desc(&tpc->RxDescArray[cur_rx]);
 
-                       NetReceive(rxdata, length);
+                       net_process_received_packet(rxdata, length);
                } else {
                        puts("Error Rx");
                }
@@ -707,16 +767,6 @@ static int rtl_reset(struct eth_device *dev, bd_t *bis)
        printf ("%s\n", __FUNCTION__);
 #endif
 
-       tpc->TxDescArrays = tx_ring;
-       /* Tx Desscriptor needs 256 bytes alignment; */
-       tpc->TxDescArray = (struct TxDesc *) ((unsigned long)(tpc->TxDescArrays +
-                                                             255) & ~255);
-
-       tpc->RxDescArrays = rx_ring;
-       /* Rx Desscriptor needs 256 bytes alignment; */
-       tpc->RxDescArray = (struct RxDesc *) ((unsigned long)(tpc->RxDescArrays +
-                                                             255) & ~255);
-
        rtl8169_init_ring(dev);
        rtl8169_hw_start(dev);
        /* Construct a perfect filter frame with the mac address as first match
@@ -758,10 +808,6 @@ static void rtl_halt(struct eth_device *dev)
 
        RTL_W32(RxMissed, 0);
 
-       tpc->TxDescArrays = NULL;
-       tpc->RxDescArrays = NULL;
-       tpc->TxDescArray = NULL;
-       tpc->RxDescArray = NULL;
        for (i = 0; i < NUM_RX_DESC; i++) {
                tpc->RxBufferRing[i] = NULL;
        }
@@ -808,7 +854,7 @@ static int rtl_init(struct eth_device *dev, bd_t *bis)
 
 #ifdef DEBUG_RTL8169
        /* Print out some hardware info */
-       printf("%s: at ioaddr 0x%x\n", dev->name, ioaddr);
+       printf("%s: at ioaddr 0x%lx\n", dev->name, ioaddr);
 #endif
 
        /* if TBI is not endbled */
@@ -906,7 +952,16 @@ static int rtl_init(struct eth_device *dev, bd_t *bis)
 #endif
        }
 
-       return 1;
+
+       tpc->RxDescArray = rtl_alloc_descs(NUM_RX_DESC);
+       if (!tpc->RxDescArray)
+               return -ENOMEM;
+
+       tpc->TxDescArray = rtl_alloc_descs(NUM_TX_DESC);
+       if (!tpc->TxDescArray)
+               return -ENOMEM;
+
+       return 0;
 }
 
 int rtl8169_initialize(bd_t *bis)
@@ -920,6 +975,7 @@ int rtl8169_initialize(bd_t *bis)
        while(1){
                unsigned int region;
                u16 device;
+               int err;
 
                /* Find RTL8169 */
                if ((devno = pci_find_devices(supported, idx++)) < 0)
@@ -950,7 +1006,7 @@ int rtl8169_initialize(bd_t *bis)
                memset(dev, 0, sizeof(*dev));
                sprintf (dev->name, "RTL8169#%d", card_number);
 
-               dev->priv = (void *) devno;
+               dev->priv = (void *)(unsigned long)devno;
                dev->iobase = (int)pci_mem_to_phys(devno, iobase);
 
                dev->init = rtl_reset;
@@ -958,9 +1014,14 @@ int rtl8169_initialize(bd_t *bis)
                dev->send = rtl_send;
                dev->recv = rtl_recv;
 
-               eth_register (dev);
+               err = rtl_init(dev, bis);
+               if (err < 0) {
+                       printf(pr_fmt("failed to initialize card: %d\n"), err);
+                       free(dev);
+                       continue;
+               }
 
-               rtl_init(dev, bis);
+               eth_register (dev);
 
                card_number++;
        }