2 * Copyright (C) 2006-2007 PA Semi, Inc
4 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
23 #include <linux/interrupt.h>
24 #include <linux/dmaengine.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <asm/dma-mapping.h>
30 #include <linux/skbuff.h>
33 #include <linux/tcp.h>
34 #include <net/checksum.h>
38 #include "pasemi_mac.h"
43 * - Get rid of pci_{read,write}_config(), map registers with ioremap
48 * - Other performance improvements
52 /* Must be a power of two */
53 #define RX_RING_SIZE 512
54 #define TX_RING_SIZE 512
56 #define DEFAULT_MSG_ENABLE \
66 #define TX_DESC(mac, num) ((mac)->tx->desc[(num) & (TX_RING_SIZE-1)])
67 #define TX_DESC_INFO(mac, num) ((mac)->tx->desc_info[(num) & (TX_RING_SIZE-1)])
68 #define RX_DESC(mac, num) ((mac)->rx->desc[(num) & (RX_RING_SIZE-1)])
69 #define RX_DESC_INFO(mac, num) ((mac)->rx->desc_info[(num) & (RX_RING_SIZE-1)])
70 #define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
72 #define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
74 MODULE_LICENSE("GPL");
75 MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
76 MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
78 static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
79 module_param(debug, int, 0);
80 MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
82 static struct pasdma_status *dma_status;
84 static unsigned int read_iob_reg(struct pasemi_mac *mac, unsigned int reg)
86 return in_le32(mac->iob_regs+reg);
89 static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
92 out_le32(mac->iob_regs+reg, val);
95 static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
97 return in_le32(mac->regs+reg);
100 static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
103 out_le32(mac->regs+reg, val);
106 static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
108 return in_le32(mac->dma_regs+reg);
111 static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
114 out_le32(mac->dma_regs+reg, val);
117 static int pasemi_get_mac_addr(struct pasemi_mac *mac)
119 struct pci_dev *pdev = mac->pdev;
120 struct device_node *dn = pci_device_to_OF_node(pdev);
127 "No device node for mac, not configuring\n");
131 maddr = of_get_property(dn, "local-mac-address", &len);
133 if (maddr && len == 6) {
134 memcpy(mac->mac_addr, maddr, 6);
138 /* Some old versions of firmware mistakenly uses mac-address
139 * (and as a string) instead of a byte array in local-mac-address.
143 maddr = of_get_property(dn, "mac-address", NULL);
147 "no mac address in device tree, not configuring\n");
152 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
153 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
155 "can't parse mac address, not configuring\n");
159 memcpy(mac->mac_addr, addr, 6);
164 static int pasemi_mac_setup_rx_resources(struct net_device *dev)
166 struct pasemi_mac_rxring *ring;
167 struct pasemi_mac *mac = netdev_priv(dev);
168 int chan_id = mac->dma_rxch;
170 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
175 spin_lock_init(&ring->lock);
177 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
178 RX_RING_SIZE, GFP_KERNEL);
180 if (!ring->desc_info)
183 /* Allocate descriptors */
184 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
186 sizeof(struct pas_dma_xct_descr),
187 &ring->dma, GFP_KERNEL);
192 memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
194 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
195 RX_RING_SIZE * sizeof(u64),
196 &ring->buf_dma, GFP_KERNEL);
200 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
202 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
204 write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
205 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
206 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
208 write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
209 PAS_DMA_RXCHAN_CFG_HBU(2));
211 write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
212 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
214 write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
215 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
216 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
218 write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if),
219 PAS_DMA_RXINT_CFG_DHL(2));
221 ring->next_to_fill = 0;
222 ring->next_to_clean = 0;
224 snprintf(ring->irq_name, sizeof(ring->irq_name),
231 dma_free_coherent(&mac->dma_pdev->dev,
232 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
233 mac->rx->desc, mac->rx->dma);
235 kfree(ring->desc_info);
243 static int pasemi_mac_setup_tx_resources(struct net_device *dev)
245 struct pasemi_mac *mac = netdev_priv(dev);
247 int chan_id = mac->dma_txch;
248 struct pasemi_mac_txring *ring;
250 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
254 spin_lock_init(&ring->lock);
256 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
257 TX_RING_SIZE, GFP_KERNEL);
258 if (!ring->desc_info)
261 /* Allocate descriptors */
262 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
264 sizeof(struct pas_dma_xct_descr),
265 &ring->dma, GFP_KERNEL);
269 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
271 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
272 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
273 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
274 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
276 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
278 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
279 PAS_DMA_TXCHAN_CFG_TY_IFACE |
280 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
281 PAS_DMA_TXCHAN_CFG_UP |
282 PAS_DMA_TXCHAN_CFG_WT(2));
284 ring->next_to_use = 0;
285 ring->next_to_clean = 0;
287 snprintf(ring->irq_name, sizeof(ring->irq_name),
294 kfree(ring->desc_info);
301 static void pasemi_mac_free_tx_resources(struct net_device *dev)
303 struct pasemi_mac *mac = netdev_priv(dev);
305 struct pasemi_mac_buffer *info;
306 struct pas_dma_xct_descr *dp;
308 for (i = 0; i < TX_RING_SIZE; i++) {
309 info = &TX_DESC_INFO(mac, i);
310 dp = &TX_DESC(mac, i);
313 pci_unmap_single(mac->dma_pdev,
317 dev_kfree_skb_any(info->skb);
326 dma_free_coherent(&mac->dma_pdev->dev,
327 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
328 mac->tx->desc, mac->tx->dma);
330 kfree(mac->tx->desc_info);
335 static void pasemi_mac_free_rx_resources(struct net_device *dev)
337 struct pasemi_mac *mac = netdev_priv(dev);
339 struct pasemi_mac_buffer *info;
340 struct pas_dma_xct_descr *dp;
342 for (i = 0; i < RX_RING_SIZE; i++) {
343 info = &RX_DESC_INFO(mac, i);
344 dp = &RX_DESC(mac, i);
347 pci_unmap_single(mac->dma_pdev,
351 dev_kfree_skb_any(info->skb);
360 dma_free_coherent(&mac->dma_pdev->dev,
361 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
362 mac->rx->desc, mac->rx->dma);
364 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
365 mac->rx->buffers, mac->rx->buf_dma);
367 kfree(mac->rx->desc_info);
372 static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
374 struct pasemi_mac *mac = netdev_priv(dev);
376 int start = mac->rx->next_to_fill;
377 unsigned int limit, count;
379 limit = (mac->rx->next_to_clean + RX_RING_SIZE -
380 mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
382 /* Check to see if we're doing first-time setup */
383 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
384 limit = RX_RING_SIZE;
390 for (count = limit; count; count--) {
391 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
392 u64 *buff = &RX_BUFF(mac, i);
396 /* skb might still be in there for recycle on short receives */
400 skb = dev_alloc_skb(BUF_SIZE);
405 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
408 if (unlikely(dma_mapping_error(dma))) {
409 dev_kfree_skb_irq(info->skb);
415 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
421 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), limit - count);
422 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), limit - count);
424 mac->rx->next_to_fill += limit - count;
427 static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
429 unsigned int reg, pcnt;
430 /* Re-enable packet count interrupts: finally
431 * ack the packet count interrupt we got in rx_intr.
434 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
436 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
438 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
441 static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
443 unsigned int reg, pcnt;
445 /* Re-enable packet count interrupts */
446 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
448 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
450 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
454 static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
458 struct pas_dma_xct_descr *dp;
459 struct pasemi_mac_buffer *info;
465 spin_lock(&mac->rx->lock);
467 n = mac->rx->next_to_clean;
469 for (count = limit; count; count--) {
473 dp = &RX_DESC(mac, n);
476 if (!(macrx & XCT_MACRX_O))
482 /* We have to scan for our skb since there's no way
483 * to back-map them from the descriptor, and if we
484 * have several receive channels then they might not
485 * show up in the same order as they were put on the
489 dma = (dp->ptr & XCT_PTR_ADDR_M);
490 for (i = n; i < (n + RX_RING_SIZE); i++) {
491 info = &RX_DESC_INFO(mac, i);
492 if (info->dma == dma)
499 pci_unmap_single(mac->dma_pdev, dma, skb->len,
502 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
505 struct sk_buff *new_skb =
506 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
508 skb_reserve(new_skb, NET_IP_ALIGN);
509 memcpy(new_skb->data - NET_IP_ALIGN,
510 skb->data - NET_IP_ALIGN,
512 /* save the skb in buffer_info as good */
515 /* else just continue with the old one */
521 skb->protocol = eth_type_trans(skb, mac->netdev);
523 if ((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
524 skb->ip_summed = CHECKSUM_COMPLETE;
525 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
528 skb->ip_summed = CHECKSUM_NONE;
530 mac->stats.rx_bytes += len;
531 mac->stats.rx_packets++;
533 netif_receive_skb(skb);
541 mac->rx->next_to_clean += limit - count;
542 pasemi_mac_replenish_rx_ring(mac->netdev);
544 spin_unlock(&mac->rx->lock);
549 static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
552 struct pasemi_mac_buffer *info;
553 struct pas_dma_xct_descr *dp;
557 spin_lock_irqsave(&mac->tx->lock, flags);
559 start = mac->tx->next_to_clean;
562 for (i = start; i < mac->tx->next_to_use; i++) {
563 dp = &TX_DESC(mac, i);
564 if (!dp || (dp->mactx & XCT_MACTX_O))
569 info = &TX_DESC_INFO(mac, i);
571 pci_unmap_single(mac->dma_pdev, info->dma,
572 info->skb->len, PCI_DMA_TODEVICE);
573 dev_kfree_skb_irq(info->skb);
580 mac->tx->next_to_clean += count;
581 spin_unlock_irqrestore(&mac->tx->lock, flags);
582 netif_wake_queue(mac->netdev);
588 static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
590 struct net_device *dev = data;
591 struct pasemi_mac *mac = netdev_priv(dev);
594 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
597 if (*mac->rx_status & PAS_STATUS_ERROR)
598 printk("rx_status reported error\n");
600 /* Don't reset packet count so it won't fire again but clear
605 if (*mac->rx_status & PAS_STATUS_SOFT)
606 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
607 if (*mac->rx_status & PAS_STATUS_ERROR)
608 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
609 if (*mac->rx_status & PAS_STATUS_TIMER)
610 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
612 netif_rx_schedule(dev, &mac->napi);
614 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
619 static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
621 struct net_device *dev = data;
622 struct pasemi_mac *mac = netdev_priv(dev);
623 unsigned int reg, pcnt;
625 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
628 pasemi_mac_clean_tx(mac);
630 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
632 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
634 if (*mac->tx_status & PAS_STATUS_SOFT)
635 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
636 if (*mac->tx_status & PAS_STATUS_ERROR)
637 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
639 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
644 static void pasemi_adjust_link(struct net_device *dev)
646 struct pasemi_mac *mac = netdev_priv(dev);
649 unsigned int new_flags;
651 if (!mac->phydev->link) {
652 /* If no link, MAC speed settings don't matter. Just report
653 * link down and return.
655 if (mac->link && netif_msg_link(mac))
656 printk(KERN_INFO "%s: Link is down.\n", dev->name);
658 netif_carrier_off(dev);
663 netif_carrier_on(dev);
665 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
666 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
667 PAS_MAC_CFG_PCFG_TSR_M);
669 if (!mac->phydev->duplex)
670 new_flags |= PAS_MAC_CFG_PCFG_HD;
672 switch (mac->phydev->speed) {
674 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
675 PAS_MAC_CFG_PCFG_TSR_1G;
678 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
679 PAS_MAC_CFG_PCFG_TSR_100M;
682 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
683 PAS_MAC_CFG_PCFG_TSR_10M;
686 printk("Unsupported speed %d\n", mac->phydev->speed);
689 /* Print on link or speed/duplex change */
690 msg = mac->link != mac->phydev->link || flags != new_flags;
692 mac->duplex = mac->phydev->duplex;
693 mac->speed = mac->phydev->speed;
694 mac->link = mac->phydev->link;
696 if (new_flags != flags)
697 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
699 if (msg && netif_msg_link(mac))
700 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
701 dev->name, mac->speed, mac->duplex ? "full" : "half");
704 static int pasemi_mac_phy_init(struct net_device *dev)
706 struct pasemi_mac *mac = netdev_priv(dev);
707 struct device_node *dn, *phy_dn;
708 struct phy_device *phydev;
711 const unsigned int *prop;
715 dn = pci_device_to_OF_node(mac->pdev);
716 ph = of_get_property(dn, "phy-handle", NULL);
719 phy_dn = of_find_node_by_phandle(*ph);
721 prop = of_get_property(phy_dn, "reg", NULL);
722 ret = of_address_to_resource(phy_dn->parent, 0, &r);
727 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
735 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
737 if (IS_ERR(phydev)) {
738 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
739 return PTR_ERR(phydev);
742 mac->phydev = phydev;
752 static int pasemi_mac_open(struct net_device *dev)
754 struct pasemi_mac *mac = netdev_priv(dev);
759 /* enable rx section */
760 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
762 /* enable tx section */
763 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
765 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
766 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
767 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
769 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
771 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
772 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
774 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
776 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
777 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
779 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
780 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
782 /* Clear out any residual packet count state from firmware */
783 pasemi_mac_restart_rx_intr(mac);
784 pasemi_mac_restart_tx_intr(mac);
786 /* 0xffffff is max value, about 16ms */
787 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
788 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
790 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
792 ret = pasemi_mac_setup_rx_resources(dev);
794 goto out_rx_resources;
796 ret = pasemi_mac_setup_tx_resources(dev);
798 goto out_tx_resources;
800 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
801 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
802 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
805 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
806 PAS_DMA_RXINT_RCMDSTA_EN);
808 /* enable rx channel */
809 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
810 PAS_DMA_RXCHAN_CCMDSTA_EN |
811 PAS_DMA_RXCHAN_CCMDSTA_DU);
813 /* enable tx channel */
814 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
815 PAS_DMA_TXCHAN_TCMDSTA_EN);
817 pasemi_mac_replenish_rx_ring(dev);
819 ret = pasemi_mac_phy_init(dev);
820 /* Some configs don't have PHYs (XAUI etc), so don't complain about
821 * failed init due to -ENODEV.
823 if (ret && ret != -ENODEV)
824 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
826 netif_start_queue(dev);
827 napi_enable(&mac->napi);
829 /* Interrupts are a bit different for our DMA controller: While
830 * it's got one a regular PCI device header, the interrupt there
831 * is really the base of the range it's using. Each tx and rx
832 * channel has it's own interrupt source.
835 base_irq = virq_to_hw(mac->dma_pdev->irq);
837 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
838 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
840 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
841 mac->tx->irq_name, dev);
843 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
844 base_irq + mac->dma_txch, ret);
848 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
849 mac->rx->irq_name, dev);
851 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
852 base_irq + 20 + mac->dma_rxch, ret);
857 phy_start(mac->phydev);
862 free_irq(mac->tx_irq, dev);
864 napi_disable(&mac->napi);
865 netif_stop_queue(dev);
866 pasemi_mac_free_tx_resources(dev);
868 pasemi_mac_free_rx_resources(dev);
874 #define MAX_RETRIES 5000
876 static int pasemi_mac_close(struct net_device *dev)
878 struct pasemi_mac *mac = netdev_priv(dev);
883 phy_stop(mac->phydev);
884 phy_disconnect(mac->phydev);
887 netif_stop_queue(dev);
888 napi_disable(&mac->napi);
890 /* Clean out any pending buffers */
891 pasemi_mac_clean_tx(mac);
892 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
894 /* Disable interface */
895 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
896 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
897 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
899 for (retries = 0; retries < MAX_RETRIES; retries++) {
900 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
901 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
906 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
907 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
909 for (retries = 0; retries < MAX_RETRIES; retries++) {
910 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
911 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
916 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
917 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
919 for (retries = 0; retries < MAX_RETRIES; retries++) {
920 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
921 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
926 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
927 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
929 /* Then, disable the channel. This must be done separately from
930 * stopping, since you can't disable when active.
933 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
934 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
935 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
937 free_irq(mac->tx_irq, dev);
938 free_irq(mac->rx_irq, dev);
941 pasemi_mac_free_rx_resources(dev);
942 pasemi_mac_free_tx_resources(dev);
947 static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
949 struct pasemi_mac *mac = netdev_priv(dev);
950 struct pasemi_mac_txring *txring;
951 struct pasemi_mac_buffer *info;
952 struct pas_dma_xct_descr *dp;
957 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
959 if (skb->ip_summed == CHECKSUM_PARTIAL) {
960 const unsigned char *nh = skb_network_header(skb);
962 switch (ip_hdr(skb)->protocol) {
964 dflags |= XCT_MACTX_CSUM_TCP;
965 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
966 dflags |= XCT_MACTX_IPO(nh - skb->data);
969 dflags |= XCT_MACTX_CSUM_UDP;
970 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
971 dflags |= XCT_MACTX_IPO(nh - skb->data);
976 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
978 if (dma_mapping_error(map))
979 return NETDEV_TX_BUSY;
983 spin_lock_irqsave(&txring->lock, flags);
985 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
986 spin_unlock_irqrestore(&txring->lock, flags);
987 pasemi_mac_clean_tx(mac);
988 pasemi_mac_restart_tx_intr(mac);
989 spin_lock_irqsave(&txring->lock, flags);
991 if (txring->next_to_clean - txring->next_to_use ==
993 /* Still no room -- stop the queue and wait for tx
994 * intr when there's room.
996 netif_stop_queue(dev);
1002 dp = &TX_DESC(mac, txring->next_to_use);
1003 info = &TX_DESC_INFO(mac, txring->next_to_use);
1005 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
1006 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
1010 txring->next_to_use++;
1011 mac->stats.tx_packets++;
1012 mac->stats.tx_bytes += skb->len;
1014 spin_unlock_irqrestore(&txring->lock, flags);
1016 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
1018 return NETDEV_TX_OK;
1021 spin_unlock_irqrestore(&txring->lock, flags);
1022 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1023 return NETDEV_TX_BUSY;
1026 static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
1028 struct pasemi_mac *mac = netdev_priv(dev);
1034 static void pasemi_mac_set_rx_mode(struct net_device *dev)
1036 struct pasemi_mac *mac = netdev_priv(dev);
1039 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
1041 /* Set promiscuous */
1042 if (dev->flags & IFF_PROMISC)
1043 flags |= PAS_MAC_CFG_PCFG_PR;
1045 flags &= ~PAS_MAC_CFG_PCFG_PR;
1047 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1051 static int pasemi_mac_poll(struct napi_struct *napi, int budget)
1053 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1054 struct net_device *dev = mac->netdev;
1057 pkts = pasemi_mac_clean_rx(mac, budget);
1058 if (pkts < budget) {
1059 /* all done, no more packets present */
1060 netif_rx_complete(dev, napi);
1062 pasemi_mac_restart_rx_intr(mac);
1067 static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1069 struct device_node *dn;
1072 dn = pci_device_to_OF_node(p);
1076 ret = of_iomap(dn, index);
1082 /* This is hardcoded and ugly, but we have some firmware versions
1083 * that don't provide the register space in the device tree. Luckily
1084 * they are at well-known locations so we can just do the math here.
1086 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1089 static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1091 struct resource res;
1092 struct device_node *dn;
1095 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1096 if (!mac->dma_pdev) {
1097 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1101 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1102 if (!mac->iob_pdev) {
1103 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1107 mac->regs = map_onedev(mac->pdev, 0);
1108 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1109 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1111 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1112 dev_err(&mac->pdev->dev, "Can't map registers\n");
1116 /* The dma status structure is located in the I/O bridge, and
1117 * is cache coherent.
1120 dn = pci_device_to_OF_node(mac->iob_pdev);
1122 err = of_address_to_resource(dn, 1, &res);
1124 /* Fallback for old firmware */
1125 res.start = 0xfd800000;
1126 res.end = res.start + 0x1000;
1128 dma_status = __ioremap(res.start, res.end-res.start, 0);
1134 static int __devinit
1135 pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1137 static int index = 0;
1138 struct net_device *dev;
1139 struct pasemi_mac *mac;
1142 err = pci_enable_device(pdev);
1146 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1149 "pasemi_mac: Could not allocate ethernet device.\n");
1151 goto out_disable_device;
1154 SET_MODULE_OWNER(dev);
1155 pci_set_drvdata(pdev, dev);
1156 SET_NETDEV_DEV(dev, &pdev->dev);
1158 mac = netdev_priv(dev);
1163 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1165 dev->features = NETIF_F_HW_CSUM;
1167 /* These should come out of the device tree eventually */
1168 mac->dma_txch = index;
1169 mac->dma_rxch = index;
1171 /* We probe GMAC before XAUI, but the DMA interfaces are
1172 * in XAUI, GMAC order.
1175 mac->dma_if = index + 2;
1177 mac->dma_if = index - 4;
1180 switch (pdev->device) {
1182 mac->type = MAC_TYPE_GMAC;
1185 mac->type = MAC_TYPE_XAUI;
1192 /* get mac addr from device tree */
1193 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1197 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1199 dev->open = pasemi_mac_open;
1200 dev->stop = pasemi_mac_close;
1201 dev->hard_start_xmit = pasemi_mac_start_tx;
1202 dev->get_stats = pasemi_mac_get_stats;
1203 dev->set_multicast_list = pasemi_mac_set_rx_mode;
1205 err = pasemi_mac_map_regs(mac);
1209 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1210 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1212 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1214 /* Enable most messages by default */
1215 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1217 err = register_netdev(dev);
1220 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1224 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1225 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1226 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1227 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1228 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1229 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1235 pci_dev_put(mac->iob_pdev);
1237 pci_dev_put(mac->dma_pdev);
1239 iounmap(mac->dma_regs);
1241 iounmap(mac->iob_regs);
1247 pci_disable_device(pdev);
1252 static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1254 struct net_device *netdev = pci_get_drvdata(pdev);
1255 struct pasemi_mac *mac;
1260 mac = netdev_priv(netdev);
1262 unregister_netdev(netdev);
1264 pci_disable_device(pdev);
1265 pci_dev_put(mac->dma_pdev);
1266 pci_dev_put(mac->iob_pdev);
1269 iounmap(mac->dma_regs);
1270 iounmap(mac->iob_regs);
1272 pci_set_drvdata(pdev, NULL);
1273 free_netdev(netdev);
1276 static struct pci_device_id pasemi_mac_pci_tbl[] = {
1277 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1278 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
1282 MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1284 static struct pci_driver pasemi_mac_driver = {
1285 .name = "pasemi_mac",
1286 .id_table = pasemi_mac_pci_tbl,
1287 .probe = pasemi_mac_probe,
1288 .remove = __devexit_p(pasemi_mac_remove),
1291 static void __exit pasemi_mac_cleanup_module(void)
1293 pci_unregister_driver(&pasemi_mac_driver);
1294 __iounmap(dma_status);
1298 int pasemi_mac_init_module(void)
1300 return pci_register_driver(&pasemi_mac_driver);
1303 module_init(pasemi_mac_init_module);
1304 module_exit(pasemi_mac_cleanup_module);