Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[sfrench/cifs-2.6.git] / drivers / net / ll_temac_main.c
index dc318330ec79af1f9182d5bffbb7e7f72a0dc4c7..6474c4973d3ad977542ba53529ed9f0165cd21d6 100644 (file)
@@ -20,9 +20,6 @@
  *   or rx, so this should be okay.
  *
  * TODO:
- * - Fix driver to work on more than just Virtex5.  Right now the driver
- *   assumes that the locallink DMA registers are accessed via DCR
- *   instructions.
  * - Factor out locallink DMA code into separate driver
  * - Fix multicast assignment.
  * - Fix support for hardware checksumming.
@@ -116,16 +113,85 @@ void temac_indirect_out32(struct temac_local *lp, int reg, u32 value)
        temac_iow(lp, XTE_CTL0_OFFSET, CNTLREG_WRITE_ENABLE_MASK | reg);
 }
 
+/**
+ * temac_dma_in32 - Memory mapped DMA read, this function expects a
+ * register input that is based on DCR word addresses which
+ * are then converted to memory mapped byte addresses
+ */
 static u32 temac_dma_in32(struct temac_local *lp, int reg)
 {
-       return dcr_read(lp->sdma_dcrs, reg);
+       return in_be32((u32 *)(lp->sdma_regs + (reg << 2)));
 }
 
+/**
+ * temac_dma_out32 - Memory mapped DMA read, this function expects a
+ * register input that is based on DCR word addresses which
+ * are then converted to memory mapped byte addresses
+ */
 static void temac_dma_out32(struct temac_local *lp, int reg, u32 value)
+{
+       out_be32((u32 *)(lp->sdma_regs + (reg << 2)), value);
+}
+
+/* DMA register access functions can be DCR based or memory mapped.
+ * The PowerPC 440 is DCR based, the PowerPC 405 and MicroBlaze are both
+ * memory mapped.
+ */
+#ifdef CONFIG_PPC_DCR
+
+/**
+ * temac_dma_dcr_in32 - DCR based DMA read
+ */
+static u32 temac_dma_dcr_in(struct temac_local *lp, int reg)
+{
+       return dcr_read(lp->sdma_dcrs, reg);
+}
+
+/**
+ * temac_dma_dcr_out32 - DCR based DMA write
+ */
+static void temac_dma_dcr_out(struct temac_local *lp, int reg, u32 value)
 {
        dcr_write(lp->sdma_dcrs, reg, value);
 }
 
+/**
+ * temac_dcr_setup - If the DMA is DCR based, then setup the address and
+ * I/O  functions
+ */
+static int temac_dcr_setup(struct temac_local *lp, struct of_device *op,
+                               struct device_node *np)
+{
+       unsigned int dcrs;
+
+       /* setup the dcr address mapping if it's in the device tree */
+
+       dcrs = dcr_resource_start(np, 0);
+       if (dcrs != 0) {
+               lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
+               lp->dma_in = temac_dma_dcr_in;
+               lp->dma_out = temac_dma_dcr_out;
+               dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
+               return 0;
+       }
+       /* no DCR in the device tree, indicate a failure */
+       return -1;
+}
+
+#else
+
+/*
+ * temac_dcr_setup - This is a stub for when DCR is not supported,
+ * such as with MicroBlaze
+ */
+static int temac_dcr_setup(struct temac_local *lp, struct of_device *op,
+                               struct device_node *np)
+{
+       return -1;
+}
+
+#endif
+
 /**
  * temac_dma_bd_init - Setup buffer descriptor rings
  */
@@ -156,14 +222,14 @@ static int temac_dma_bd_init(struct net_device *ndev)
                lp->rx_bd_v[i].next = lp->rx_bd_p +
                                sizeof(*lp->rx_bd_v) * ((i + 1) % RX_BD_NUM);
 
-               skb = alloc_skb(XTE_MAX_JUMBO_FRAME_SIZE
-                               + XTE_ALIGN, GFP_ATOMIC);
+               skb = netdev_alloc_skb_ip_align(ndev,
+                                               XTE_MAX_JUMBO_FRAME_SIZE);
+
                if (skb == 0) {
                        dev_err(&ndev->dev, "alloc_skb error %d\n", i);
                        return -1;
                }
                lp->rx_skb[i] = skb;
-               skb_reserve(skb,  BUFFER_ALIGN(skb->data));
                /* returns physical address of skb->data */
                lp->rx_bd_v[i].phys = dma_map_single(ndev->dev.parent,
                                                     skb->data,
@@ -173,23 +239,23 @@ static int temac_dma_bd_init(struct net_device *ndev)
                lp->rx_bd_v[i].app0 = STS_CTRL_APP0_IRQONEND;
        }
 
-       temac_dma_out32(lp, TX_CHNL_CTRL, 0x10220400 |
+       lp->dma_out(lp, TX_CHNL_CTRL, 0x10220400 |
                                          CHNL_CTRL_IRQ_EN |
                                          CHNL_CTRL_IRQ_DLY_EN |
                                          CHNL_CTRL_IRQ_COAL_EN);
        /* 0x10220483 */
        /* 0x00100483 */
-       temac_dma_out32(lp, RX_CHNL_CTRL, 0xff010000 |
+       lp->dma_out(lp, RX_CHNL_CTRL, 0xff070000 |
                                          CHNL_CTRL_IRQ_EN |
                                          CHNL_CTRL_IRQ_DLY_EN |
                                          CHNL_CTRL_IRQ_COAL_EN |
                                          CHNL_CTRL_IRQ_IOE);
        /* 0xff010283 */
 
-       temac_dma_out32(lp, RX_CURDESC_PTR,  lp->rx_bd_p);
-       temac_dma_out32(lp, RX_TAILDESC_PTR,
+       lp->dma_out(lp, RX_CURDESC_PTR,  lp->rx_bd_p);
+       lp->dma_out(lp, RX_TAILDESC_PTR,
                       lp->rx_bd_p + (sizeof(*lp->rx_bd_v) * (RX_BD_NUM - 1)));
-       temac_dma_out32(lp, TX_CURDESC_PTR, lp->tx_bd_p);
+       lp->dma_out(lp, TX_CURDESC_PTR, lp->tx_bd_p);
 
        return 0;
 }
@@ -251,20 +317,20 @@ static void temac_set_multicast_list(struct net_device *ndev)
                temac_indirect_out32(lp, XTE_AFM_OFFSET, XTE_AFM_EPPRM_MASK);
                dev_info(&ndev->dev, "Promiscuous mode enabled.\n");
        } else if (!netdev_mc_empty(ndev)) {
-               struct dev_mc_list *mclist;
+               struct netdev_hw_addr *ha;
 
                i = 0;
-               netdev_for_each_mc_addr(mclist, ndev) {
+               netdev_for_each_mc_addr(ha, ndev) {
                        if (i >= MULTICAST_CAM_TABLE_NUM)
                                break;
-                       multi_addr_msw = ((mclist->dmi_addr[3] << 24) |
-                                         (mclist->dmi_addr[2] << 16) |
-                                         (mclist->dmi_addr[1] << 8) |
-                                         (mclist->dmi_addr[0]));
+                       multi_addr_msw = ((ha->addr[3] << 24) |
+                                         (ha->addr[2] << 16) |
+                                         (ha->addr[1] << 8) |
+                                         (ha->addr[0]));
                        temac_indirect_out32(lp, XTE_MAW0_OFFSET,
                                             multi_addr_msw);
-                       multi_addr_lsw = ((mclist->dmi_addr[5] << 8) |
-                                         (mclist->dmi_addr[4]) | (i << 16));
+                       multi_addr_lsw = ((ha->addr[5] << 8) |
+                                         (ha->addr[4]) | (i << 16));
                        temac_indirect_out32(lp, XTE_MAW1_OFFSET,
                                             multi_addr_lsw);
                        i++;
@@ -427,9 +493,9 @@ static void temac_device_reset(struct net_device *ndev)
        temac_indirect_out32(lp, XTE_RXC1_OFFSET, val & ~XTE_RXC1_RXEN_MASK);
 
        /* Reset Local Link (DMA) */
-       temac_dma_out32(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
+       lp->dma_out(lp, DMA_CONTROL_REG, DMA_CONTROL_RST);
        timeout = 1000;
-       while (temac_dma_in32(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
+       while (lp->dma_in(lp, DMA_CONTROL_REG) & DMA_CONTROL_RST) {
                udelay(1);
                if (--timeout == 0) {
                        dev_err(&ndev->dev,
@@ -437,7 +503,7 @@ static void temac_device_reset(struct net_device *ndev)
                        break;
                }
        }
-       temac_dma_out32(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
+       lp->dma_out(lp, DMA_CONTROL_REG, DMA_TAIL_ENABLE);
 
        temac_dma_bd_init(ndev);
 
@@ -461,7 +527,7 @@ static void temac_device_reset(struct net_device *ndev)
                dev_err(&ndev->dev, "Error setting TEMAC options\n");
 
        /* Init Driver variable */
-       ndev->trans_start = 0;
+       ndev->trans_start = jiffies; /* prevent tx timeout */
 }
 
 void temac_adjust_link(struct net_device *ndev)
@@ -508,6 +574,10 @@ static void temac_start_xmit_done(struct net_device *ndev)
                if (cur_p->app4)
                        dev_kfree_skb_irq((struct sk_buff *)cur_p->app4);
                cur_p->app0 = 0;
+               cur_p->app1 = 0;
+               cur_p->app2 = 0;
+               cur_p->app3 = 0;
+               cur_p->app4 = 0;
 
                ndev->stats.tx_packets++;
                ndev->stats.tx_bytes += cur_p->len;
@@ -523,6 +593,29 @@ static void temac_start_xmit_done(struct net_device *ndev)
        netif_wake_queue(ndev);
 }
 
+static inline int temac_check_tx_bd_space(struct temac_local *lp, int num_frag)
+{
+       struct cdmac_bd *cur_p;
+       int tail;
+
+       tail = lp->tx_bd_tail;
+       cur_p = &lp->tx_bd_v[tail];
+
+       do {
+               if (cur_p->app0)
+                       return NETDEV_TX_BUSY;
+
+               tail++;
+               if (tail >= TX_BD_NUM)
+                       tail = 0;
+
+               cur_p = &lp->tx_bd_v[tail];
+               num_frag--;
+       } while (num_frag >= 0);
+
+       return 0;
+}
+
 static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
 {
        struct temac_local *lp = netdev_priv(ndev);
@@ -537,7 +630,7 @@ static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
        start_p = lp->tx_bd_p + sizeof(*lp->tx_bd_v) * lp->tx_bd_tail;
        cur_p = &lp->tx_bd_v[lp->tx_bd_tail];
 
-       if (cur_p->app0 & STS_CTRL_APP0_CMPLT) {
+       if (temac_check_tx_bd_space(lp, num_frag)) {
                if (!netif_queue_stopped(ndev)) {
                        netif_stop_queue(ndev);
                        return NETDEV_TX_BUSY;
@@ -547,29 +640,14 @@ static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
 
        cur_p->app0 = 0;
        if (skb->ip_summed == CHECKSUM_PARTIAL) {
-               const struct iphdr *ip = ip_hdr(skb);
-               int length = 0, start = 0, insert = 0;
-
-               switch (ip->protocol) {
-               case IPPROTO_TCP:
-                       start = sizeof(struct iphdr) + ETH_HLEN;
-                       insert = sizeof(struct iphdr) + ETH_HLEN + 16;
-                       length = ip->tot_len - sizeof(struct iphdr);
-                       break;
-               case IPPROTO_UDP:
-                       start = sizeof(struct iphdr) + ETH_HLEN;
-                       insert = sizeof(struct iphdr) + ETH_HLEN + 6;
-                       length = ip->tot_len - sizeof(struct iphdr);
-                       break;
-               default:
-                       break;
-               }
-               cur_p->app1 = ((start << 16) | insert);
-               cur_p->app2 = csum_tcpudp_magic(ip->saddr, ip->daddr,
-                                               length, ip->protocol, 0);
-               skb->data[insert] = 0;
-               skb->data[insert + 1] = 0;
+               unsigned int csum_start_off = skb_transport_offset(skb);
+               unsigned int csum_index_off = csum_start_off + skb->csum_offset;
+
+               cur_p->app0 |= 1; /* TX Checksum Enabled */
+               cur_p->app1 = (csum_start_off << 16) | csum_index_off;
+               cur_p->app2 = 0;  /* initial checksum seed */
        }
+
        cur_p->app0 |= STS_CTRL_APP0_SOP;
        cur_p->len = skb_headlen(skb);
        cur_p->phys = dma_map_single(ndev->dev.parent, skb->data, skb->len,
@@ -598,7 +676,7 @@ static int temac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
                lp->tx_bd_tail = 0;
 
        /* Kick off the transfer */
-       temac_dma_out32(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
+       lp->dma_out(lp, TX_TAILDESC_PTR, tail_p); /* DMA start */
 
        return NETDEV_TX_OK;
 }
@@ -612,7 +690,6 @@ static void ll_temac_recv(struct net_device *ndev)
        struct cdmac_bd *cur_p;
        dma_addr_t tail_p;
        int length;
-       unsigned long skb_vaddr;
        unsigned long flags;
 
        spin_lock_irqsave(&lp->rx_lock, flags);
@@ -626,8 +703,7 @@ static void ll_temac_recv(struct net_device *ndev)
                skb = lp->rx_skb[lp->rx_bd_ci];
                length = cur_p->app4 & 0x3FFF;
 
-               skb_vaddr = virt_to_bus(skb->data);
-               dma_unmap_single(ndev->dev.parent, skb_vaddr, length,
+               dma_unmap_single(ndev->dev.parent, cur_p->phys, length,
                                 DMA_FROM_DEVICE);
 
                skb_put(skb, length);
@@ -635,21 +711,29 @@ static void ll_temac_recv(struct net_device *ndev)
                skb->protocol = eth_type_trans(skb, ndev);
                skb->ip_summed = CHECKSUM_NONE;
 
+               /* if we're doing rx csum offload, set it up */
+               if (((lp->temac_features & TEMAC_FEATURE_RX_CSUM) != 0) &&
+                       (skb->protocol == __constant_htons(ETH_P_IP)) &&
+                       (skb->len > 64)) {
+
+                       skb->csum = cur_p->app3 & 0xFFFF;
+                       skb->ip_summed = CHECKSUM_COMPLETE;
+               }
+
                netif_rx(skb);
 
                ndev->stats.rx_packets++;
                ndev->stats.rx_bytes += length;
 
-               new_skb = alloc_skb(XTE_MAX_JUMBO_FRAME_SIZE + XTE_ALIGN,
-                               GFP_ATOMIC);
+               new_skb = netdev_alloc_skb_ip_align(ndev,
+                                               XTE_MAX_JUMBO_FRAME_SIZE);
+
                if (new_skb == 0) {
                        dev_err(&ndev->dev, "no memory for new sk_buff\n");
                        spin_unlock_irqrestore(&lp->rx_lock, flags);
                        return;
                }
 
-               skb_reserve(new_skb, BUFFER_ALIGN(new_skb->data));
-
                cur_p->app0 = STS_CTRL_APP0_IRQONEND;
                cur_p->phys = dma_map_single(ndev->dev.parent, new_skb->data,
                                             XTE_MAX_JUMBO_FRAME_SIZE,
@@ -664,7 +748,7 @@ static void ll_temac_recv(struct net_device *ndev)
                cur_p = &lp->rx_bd_v[lp->rx_bd_ci];
                bdstat = cur_p->app0;
        }
-       temac_dma_out32(lp, RX_TAILDESC_PTR, tail_p);
+       lp->dma_out(lp, RX_TAILDESC_PTR, tail_p);
 
        spin_unlock_irqrestore(&lp->rx_lock, flags);
 }
@@ -675,8 +759,8 @@ static irqreturn_t ll_temac_tx_irq(int irq, void *_ndev)
        struct temac_local *lp = netdev_priv(ndev);
        unsigned int status;
 
-       status = temac_dma_in32(lp, TX_IRQ_REG);
-       temac_dma_out32(lp, TX_IRQ_REG, status);
+       status = lp->dma_in(lp, TX_IRQ_REG);
+       lp->dma_out(lp, TX_IRQ_REG, status);
 
        if (status & (IRQ_COAL | IRQ_DLY))
                temac_start_xmit_done(lp->ndev);
@@ -693,8 +777,8 @@ static irqreturn_t ll_temac_rx_irq(int irq, void *_ndev)
        unsigned int status;
 
        /* Read and clear the status registers */
-       status = temac_dma_in32(lp, RX_IRQ_REG);
-       temac_dma_out32(lp, RX_IRQ_REG, status);
+       status = lp->dma_in(lp, RX_IRQ_REG);
+       lp->dma_out(lp, RX_IRQ_REG, status);
 
        if (status & (IRQ_COAL | IRQ_DLY))
                ll_temac_recv(lp->ndev);
@@ -795,7 +879,7 @@ static ssize_t temac_show_llink_regs(struct device *dev,
        int i, len = 0;
 
        for (i = 0; i < 0x11; i++)
-               len += sprintf(buf + len, "%.8x%s", temac_dma_in32(lp, i),
+               len += sprintf(buf + len, "%.8x%s", lp->dma_in(lp, i),
                               (i % 8) == 7 ? "\n" : " ");
        len += sprintf(buf + len, "\n");
 
@@ -820,8 +904,8 @@ temac_of_probe(struct of_device *op, const struct of_device_id *match)
        struct temac_local *lp;
        struct net_device *ndev;
        const void *addr;
+       __be32 *p;
        int size, rc = 0;
-       unsigned int dcrs;
 
        /* Init network device structure */
        ndev = alloc_etherdev(sizeof(*lp));
@@ -864,27 +948,46 @@ temac_of_probe(struct of_device *op, const struct of_device_id *match)
                goto nodev;
        }
 
+       /* Setup checksum offload, but default to off if not specified */
+       lp->temac_features = 0;
+       p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,txcsum", NULL);
+       if (p && be32_to_cpu(*p)) {
+               lp->temac_features |= TEMAC_FEATURE_TX_CSUM;
+               /* Can checksum TCP/UDP over IPv4. */
+               ndev->features |= NETIF_F_IP_CSUM;
+       }
+       p = (__be32 *)of_get_property(op->dev.of_node, "xlnx,rxcsum", NULL);
+       if (p && be32_to_cpu(*p))
+               lp->temac_features |= TEMAC_FEATURE_RX_CSUM;
+
        /* Find the DMA node, map the DMA registers, and decode the DMA IRQs */
        np = of_parse_phandle(op->dev.of_node, "llink-connected", 0);
        if (!np) {
                dev_err(&op->dev, "could not find DMA node\n");
-               goto nodev;
+               goto err_iounmap;
        }
 
-       dcrs = dcr_resource_start(np, 0);
-       if (dcrs == 0) {
-               dev_err(&op->dev, "could not get DMA register address\n");
-               goto nodev;
+       /* Setup the DMA register accesses, could be DCR or memory mapped */
+       if (temac_dcr_setup(lp, op, np)) {
+
+               /* no DCR in the device tree, try non-DCR */
+               lp->sdma_regs = of_iomap(np, 0);
+               if (lp->sdma_regs) {
+                       lp->dma_in = temac_dma_in32;
+                       lp->dma_out = temac_dma_out32;
+                       dev_dbg(&op->dev, "MEM base: %p\n", lp->sdma_regs);
+               } else {
+                       dev_err(&op->dev, "unable to map DMA registers\n");
+                       goto err_iounmap;
+               }
        }
-       lp->sdma_dcrs = dcr_map(np, dcrs, dcr_resource_len(np, 0));
-       dev_dbg(&op->dev, "DCR base: %x\n", dcrs);
 
        lp->rx_irq = irq_of_parse_and_map(np, 0);
        lp->tx_irq = irq_of_parse_and_map(np, 1);
-       if (!lp->rx_irq || !lp->tx_irq) {
+       if ((lp->rx_irq == NO_IRQ) || (lp->tx_irq == NO_IRQ)) {
                dev_err(&op->dev, "could not determine irqs\n");
                rc = -ENOMEM;
-               goto nodev;
+               goto err_iounmap_2;
        }
 
        of_node_put(np); /* Finished with the DMA node; drop the reference */
@@ -894,7 +997,7 @@ temac_of_probe(struct of_device *op, const struct of_device_id *match)
        if ((!addr) || (size != 6)) {
                dev_err(&op->dev, "could not find MAC address\n");
                rc = -ENODEV;
-               goto nodev;
+               goto err_iounmap_2;
        }
        temac_set_mac_address(ndev, (void *)addr);
 
@@ -910,7 +1013,7 @@ temac_of_probe(struct of_device *op, const struct of_device_id *match)
        rc = sysfs_create_group(&lp->dev->kobj, &temac_attr_group);
        if (rc) {
                dev_err(lp->dev, "Error creating sysfs files\n");
-               goto nodev;
+               goto err_iounmap_2;
        }
 
        rc = register_netdev(lp->ndev);
@@ -923,6 +1026,11 @@ temac_of_probe(struct of_device *op, const struct of_device_id *match)
 
  err_register_ndev:
        sysfs_remove_group(&lp->dev->kobj, &temac_attr_group);
+ err_iounmap_2:
+       if (lp->sdma_regs)
+               iounmap(lp->sdma_regs);
+ err_iounmap:
+       iounmap(lp->regs);
  nodev:
        free_netdev(ndev);
        ndev = NULL;
@@ -941,6 +1049,9 @@ static int __devexit temac_of_remove(struct of_device *op)
                of_node_put(lp->phy_node);
        lp->phy_node = NULL;
        dev_set_drvdata(&op->dev, NULL);
+       iounmap(lp->regs);
+       if (lp->sdma_regs)
+               iounmap(lp->sdma_regs);
        free_netdev(ndev);
        return 0;
 }