pasemi_mac: basic error checking
[linux-2.6/kvm.git] / drivers / net / pasemi_mac.c
blobb297a67d2fbee4b39ff8ccf65eece1c9b7b122d6
1 /*
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>
29 #include <linux/in.h>
30 #include <linux/skbuff.h>
32 #include <linux/ip.h>
33 #include <linux/tcp.h>
34 #include <net/checksum.h>
36 #include <asm/irq.h>
38 #include "pasemi_mac.h"
41 /* TODO list
43 * - Get rid of pci_{read,write}_config(), map registers with ioremap
44 * for performance
45 * - PHY support
46 * - Multicast support
47 * - Large MTU support
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 \
57 (NETIF_MSG_DRV | \
58 NETIF_MSG_PROBE | \
59 NETIF_MSG_LINK | \
60 NETIF_MSG_TIMER | \
61 NETIF_MSG_IFDOWN | \
62 NETIF_MSG_IFUP | \
63 NETIF_MSG_RX_ERR | \
64 NETIF_MSG_TX_ERR)
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 RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
73 & ((ring)->size - 1))
74 #define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
76 #define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
78 MODULE_LICENSE("GPL");
79 MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
80 MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
82 static int debug = -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
83 module_param(debug, int, 0);
84 MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
86 static struct pasdma_status *dma_status;
88 static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
89 unsigned int val)
91 out_le32(mac->iob_regs+reg, val);
94 static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
96 return in_le32(mac->regs+reg);
99 static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
100 unsigned int val)
102 out_le32(mac->regs+reg, val);
105 static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
107 return in_le32(mac->dma_regs+reg);
110 static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
111 unsigned int val)
113 out_le32(mac->dma_regs+reg, val);
116 static int pasemi_get_mac_addr(struct pasemi_mac *mac)
118 struct pci_dev *pdev = mac->pdev;
119 struct device_node *dn = pci_device_to_OF_node(pdev);
120 int len;
121 const u8 *maddr;
122 u8 addr[6];
124 if (!dn) {
125 dev_dbg(&pdev->dev,
126 "No device node for mac, not configuring\n");
127 return -ENOENT;
130 maddr = of_get_property(dn, "local-mac-address", &len);
132 if (maddr && len == 6) {
133 memcpy(mac->mac_addr, maddr, 6);
134 return 0;
137 /* Some old versions of firmware mistakenly uses mac-address
138 * (and as a string) instead of a byte array in local-mac-address.
141 if (maddr == NULL)
142 maddr = of_get_property(dn, "mac-address", NULL);
144 if (maddr == NULL) {
145 dev_warn(&pdev->dev,
146 "no mac address in device tree, not configuring\n");
147 return -ENOENT;
151 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
152 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
153 dev_warn(&pdev->dev,
154 "can't parse mac address, not configuring\n");
155 return -EINVAL;
158 memcpy(mac->mac_addr, addr, 6);
160 return 0;
163 static int pasemi_mac_setup_rx_resources(struct net_device *dev)
165 struct pasemi_mac_rxring *ring;
166 struct pasemi_mac *mac = netdev_priv(dev);
167 int chan_id = mac->dma_rxch;
169 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
171 if (!ring)
172 goto out_ring;
174 spin_lock_init(&ring->lock);
176 ring->size = RX_RING_SIZE;
177 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
178 RX_RING_SIZE, GFP_KERNEL);
180 if (!ring->desc_info)
181 goto out_desc_info;
183 /* Allocate descriptors */
184 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
185 RX_RING_SIZE *
186 sizeof(struct pas_dma_xct_descr),
187 &ring->dma, GFP_KERNEL);
189 if (!ring->desc)
190 goto out_desc;
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);
197 if (!ring->buffers)
198 goto out_buffers;
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),
225 "%s rx", dev->name);
226 mac->rx = ring;
228 return 0;
230 out_buffers:
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);
234 out_desc:
235 kfree(ring->desc_info);
236 out_desc_info:
237 kfree(ring);
238 out_ring:
239 return -ENOMEM;
243 static int pasemi_mac_setup_tx_resources(struct net_device *dev)
245 struct pasemi_mac *mac = netdev_priv(dev);
246 u32 val;
247 int chan_id = mac->dma_txch;
248 struct pasemi_mac_txring *ring;
250 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
251 if (!ring)
252 goto out_ring;
254 spin_lock_init(&ring->lock);
256 ring->size = TX_RING_SIZE;
257 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
258 TX_RING_SIZE, GFP_KERNEL);
259 if (!ring->desc_info)
260 goto out_desc_info;
262 /* Allocate descriptors */
263 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
264 TX_RING_SIZE *
265 sizeof(struct pas_dma_xct_descr),
266 &ring->dma, GFP_KERNEL);
267 if (!ring->desc)
268 goto out_desc;
270 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
272 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
273 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
274 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
275 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
277 write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
279 write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
280 PAS_DMA_TXCHAN_CFG_TY_IFACE |
281 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
282 PAS_DMA_TXCHAN_CFG_UP |
283 PAS_DMA_TXCHAN_CFG_WT(2));
285 ring->next_to_fill = 0;
286 ring->next_to_clean = 0;
288 snprintf(ring->irq_name, sizeof(ring->irq_name),
289 "%s tx", dev->name);
290 mac->tx = ring;
292 return 0;
294 out_desc:
295 kfree(ring->desc_info);
296 out_desc_info:
297 kfree(ring);
298 out_ring:
299 return -ENOMEM;
302 static void pasemi_mac_free_tx_resources(struct net_device *dev)
304 struct pasemi_mac *mac = netdev_priv(dev);
305 unsigned int i;
306 struct pasemi_mac_buffer *info;
307 struct pas_dma_xct_descr *dp;
309 for (i = 0; i < TX_RING_SIZE; i++) {
310 info = &TX_DESC_INFO(mac, i);
311 dp = &TX_DESC(mac, i);
312 if (info->dma) {
313 if (info->skb) {
314 pci_unmap_single(mac->dma_pdev,
315 info->dma,
316 info->skb->len,
317 PCI_DMA_TODEVICE);
318 dev_kfree_skb_any(info->skb);
320 info->dma = 0;
321 info->skb = NULL;
322 dp->mactx = 0;
323 dp->ptr = 0;
327 dma_free_coherent(&mac->dma_pdev->dev,
328 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
329 mac->tx->desc, mac->tx->dma);
331 kfree(mac->tx->desc_info);
332 kfree(mac->tx);
333 mac->tx = NULL;
336 static void pasemi_mac_free_rx_resources(struct net_device *dev)
338 struct pasemi_mac *mac = netdev_priv(dev);
339 unsigned int i;
340 struct pasemi_mac_buffer *info;
341 struct pas_dma_xct_descr *dp;
343 for (i = 0; i < RX_RING_SIZE; i++) {
344 info = &RX_DESC_INFO(mac, i);
345 dp = &RX_DESC(mac, i);
346 if (info->skb) {
347 if (info->dma) {
348 pci_unmap_single(mac->dma_pdev,
349 info->dma,
350 info->skb->len,
351 PCI_DMA_FROMDEVICE);
352 dev_kfree_skb_any(info->skb);
354 info->dma = 0;
355 info->skb = NULL;
356 dp->macrx = 0;
357 dp->ptr = 0;
361 dma_free_coherent(&mac->dma_pdev->dev,
362 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
363 mac->rx->desc, mac->rx->dma);
365 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
366 mac->rx->buffers, mac->rx->buf_dma);
368 kfree(mac->rx->desc_info);
369 kfree(mac->rx);
370 mac->rx = NULL;
373 static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
375 struct pasemi_mac *mac = netdev_priv(dev);
376 unsigned int i;
377 int start = mac->rx->next_to_fill;
378 int count;
380 if (limit <= 0)
381 return;
383 i = start;
384 for (count = 0; count < limit; count++) {
385 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
386 u64 *buff = &RX_BUFF(mac, i);
387 struct sk_buff *skb;
388 dma_addr_t dma;
390 /* skb might still be in there for recycle on short receives */
391 if (info->skb)
392 skb = info->skb;
393 else
394 skb = dev_alloc_skb(BUF_SIZE);
396 if (unlikely(!skb))
397 break;
399 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
400 PCI_DMA_FROMDEVICE);
402 if (unlikely(dma_mapping_error(dma))) {
403 dev_kfree_skb_irq(info->skb);
404 break;
407 info->skb = skb;
408 info->dma = dma;
409 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
410 i++;
413 wmb();
415 write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count);
416 write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
418 mac->rx->next_to_fill += count;
421 static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
423 unsigned int reg, pcnt;
424 /* Re-enable packet count interrupts: finally
425 * ack the packet count interrupt we got in rx_intr.
428 pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
430 reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
432 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
435 static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
437 unsigned int reg, pcnt;
439 /* Re-enable packet count interrupts */
440 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
442 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
444 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
448 static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
450 unsigned int rcmdsta, ccmdsta;
452 if (!netif_msg_rx_err(mac))
453 return;
455 rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
456 ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
458 printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
459 macrx, *mac->rx_status);
461 printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
462 rcmdsta, ccmdsta);
465 static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
467 unsigned int cmdsta;
469 if (!netif_msg_tx_err(mac))
470 return;
472 cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
474 printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
475 "tx status 0x%016lx\n", mactx, *mac->tx_status);
477 printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
480 static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
482 unsigned int n;
483 int count;
484 struct pas_dma_xct_descr *dp;
485 struct pasemi_mac_buffer *info;
486 struct sk_buff *skb;
487 unsigned int i, len;
488 u64 macrx;
489 dma_addr_t dma;
491 spin_lock(&mac->rx->lock);
493 n = mac->rx->next_to_clean;
495 for (count = limit; count; count--) {
497 rmb();
499 dp = &RX_DESC(mac, n);
500 prefetchw(dp);
501 macrx = dp->macrx;
503 if ((macrx & XCT_MACRX_E) ||
504 (*mac->rx_status & PAS_STATUS_ERROR))
505 pasemi_mac_rx_error(mac, macrx);
507 if (!(macrx & XCT_MACRX_O))
508 break;
510 info = NULL;
512 /* We have to scan for our skb since there's no way
513 * to back-map them from the descriptor, and if we
514 * have several receive channels then they might not
515 * show up in the same order as they were put on the
516 * interface ring.
519 dma = (dp->ptr & XCT_PTR_ADDR_M);
520 for (i = n; i < (n + RX_RING_SIZE); i++) {
521 info = &RX_DESC_INFO(mac, i);
522 if (info->dma == dma)
523 break;
525 prefetchw(info);
527 skb = info->skb;
528 prefetchw(skb);
529 info->dma = 0;
531 pci_unmap_single(mac->dma_pdev, dma, skb->len,
532 PCI_DMA_FROMDEVICE);
534 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
536 if (len < 256) {
537 struct sk_buff *new_skb =
538 netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
539 if (new_skb) {
540 skb_reserve(new_skb, NET_IP_ALIGN);
541 memcpy(new_skb->data, skb->data, len);
542 /* save the skb in buffer_info as good */
543 skb = new_skb;
545 /* else just continue with the old one */
546 } else
547 info->skb = NULL;
549 skb_put(skb, len);
551 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
552 skb->ip_summed = CHECKSUM_UNNECESSARY;
553 skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
554 XCT_MACRX_CSUM_S;
555 } else
556 skb->ip_summed = CHECKSUM_NONE;
558 mac->netdev->stats.rx_bytes += len;
559 mac->netdev->stats.rx_packets++;
561 skb->protocol = eth_type_trans(skb, mac->netdev);
562 netif_receive_skb(skb);
564 dp->ptr = 0;
565 dp->macrx = 0;
567 n++;
570 mac->rx->next_to_clean += limit - count;
571 pasemi_mac_replenish_rx_ring(mac->netdev, limit-count);
573 spin_unlock(&mac->rx->lock);
575 return count;
578 static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
580 int i;
581 struct pasemi_mac_buffer *info;
582 struct pas_dma_xct_descr *dp;
583 unsigned int start, count, limit;
584 unsigned int total_count;
585 unsigned long flags;
586 struct sk_buff *skbs[32];
587 dma_addr_t dmas[32];
589 total_count = 0;
590 restart:
591 spin_lock_irqsave(&mac->tx->lock, flags);
593 start = mac->tx->next_to_clean;
594 limit = min(mac->tx->next_to_fill, start+32);
596 count = 0;
598 for (i = start; i < limit; i++) {
599 dp = &TX_DESC(mac, i);
601 if ((dp->mactx & XCT_MACTX_E) ||
602 (*mac->tx_status & PAS_STATUS_ERROR))
603 pasemi_mac_tx_error(mac, dp->mactx);
605 if (unlikely(dp->mactx & XCT_MACTX_O))
606 /* Not yet transmitted */
607 break;
609 info = &TX_DESC_INFO(mac, i);
610 skbs[count] = info->skb;
611 dmas[count] = info->dma;
613 info->skb = NULL;
614 info->dma = 0;
615 dp->mactx = 0;
616 dp->ptr = 0;
618 count++;
620 mac->tx->next_to_clean += count;
621 spin_unlock_irqrestore(&mac->tx->lock, flags);
622 netif_wake_queue(mac->netdev);
624 for (i = 0; i < count; i++) {
625 pci_unmap_single(mac->dma_pdev, dmas[i],
626 skbs[i]->len, PCI_DMA_TODEVICE);
627 dev_kfree_skb_irq(skbs[i]);
630 total_count += count;
632 /* If the batch was full, try to clean more */
633 if (count == 32)
634 goto restart;
636 return total_count;
640 static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
642 struct net_device *dev = data;
643 struct pasemi_mac *mac = netdev_priv(dev);
644 unsigned int reg;
646 if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
647 return IRQ_NONE;
649 /* Don't reset packet count so it won't fire again but clear
650 * all others.
653 reg = 0;
654 if (*mac->rx_status & PAS_STATUS_SOFT)
655 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
656 if (*mac->rx_status & PAS_STATUS_ERROR)
657 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
658 if (*mac->rx_status & PAS_STATUS_TIMER)
659 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
661 netif_rx_schedule(dev, &mac->napi);
663 write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
665 return IRQ_HANDLED;
668 static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
670 struct net_device *dev = data;
671 struct pasemi_mac *mac = netdev_priv(dev);
672 unsigned int reg, pcnt;
674 if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
675 return IRQ_NONE;
677 pasemi_mac_clean_tx(mac);
679 pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
681 reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
683 if (*mac->tx_status & PAS_STATUS_SOFT)
684 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
685 if (*mac->tx_status & PAS_STATUS_ERROR)
686 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
688 write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
690 return IRQ_HANDLED;
693 static void pasemi_adjust_link(struct net_device *dev)
695 struct pasemi_mac *mac = netdev_priv(dev);
696 int msg;
697 unsigned int flags;
698 unsigned int new_flags;
700 if (!mac->phydev->link) {
701 /* If no link, MAC speed settings don't matter. Just report
702 * link down and return.
704 if (mac->link && netif_msg_link(mac))
705 printk(KERN_INFO "%s: Link is down.\n", dev->name);
707 netif_carrier_off(dev);
708 mac->link = 0;
710 return;
711 } else
712 netif_carrier_on(dev);
714 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
715 new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
716 PAS_MAC_CFG_PCFG_TSR_M);
718 if (!mac->phydev->duplex)
719 new_flags |= PAS_MAC_CFG_PCFG_HD;
721 switch (mac->phydev->speed) {
722 case 1000:
723 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
724 PAS_MAC_CFG_PCFG_TSR_1G;
725 break;
726 case 100:
727 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
728 PAS_MAC_CFG_PCFG_TSR_100M;
729 break;
730 case 10:
731 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
732 PAS_MAC_CFG_PCFG_TSR_10M;
733 break;
734 default:
735 printk("Unsupported speed %d\n", mac->phydev->speed);
738 /* Print on link or speed/duplex change */
739 msg = mac->link != mac->phydev->link || flags != new_flags;
741 mac->duplex = mac->phydev->duplex;
742 mac->speed = mac->phydev->speed;
743 mac->link = mac->phydev->link;
745 if (new_flags != flags)
746 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
748 if (msg && netif_msg_link(mac))
749 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
750 dev->name, mac->speed, mac->duplex ? "full" : "half");
753 static int pasemi_mac_phy_init(struct net_device *dev)
755 struct pasemi_mac *mac = netdev_priv(dev);
756 struct device_node *dn, *phy_dn;
757 struct phy_device *phydev;
758 unsigned int phy_id;
759 const phandle *ph;
760 const unsigned int *prop;
761 struct resource r;
762 int ret;
764 dn = pci_device_to_OF_node(mac->pdev);
765 ph = of_get_property(dn, "phy-handle", NULL);
766 if (!ph)
767 return -ENODEV;
768 phy_dn = of_find_node_by_phandle(*ph);
770 prop = of_get_property(phy_dn, "reg", NULL);
771 ret = of_address_to_resource(phy_dn->parent, 0, &r);
772 if (ret)
773 goto err;
775 phy_id = *prop;
776 snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
778 of_node_put(phy_dn);
780 mac->link = 0;
781 mac->speed = 0;
782 mac->duplex = -1;
784 phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
786 if (IS_ERR(phydev)) {
787 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
788 return PTR_ERR(phydev);
791 mac->phydev = phydev;
793 return 0;
795 err:
796 of_node_put(phy_dn);
797 return -ENODEV;
801 static int pasemi_mac_open(struct net_device *dev)
803 struct pasemi_mac *mac = netdev_priv(dev);
804 int base_irq;
805 unsigned int flags;
806 int ret;
808 /* enable rx section */
809 write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
811 /* enable tx section */
812 write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
814 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
815 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
816 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
818 write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
820 write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
821 PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
823 write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
824 PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
826 /* Clear out any residual packet count state from firmware */
827 pasemi_mac_restart_rx_intr(mac);
828 pasemi_mac_restart_tx_intr(mac);
830 /* 0xffffff is max value, about 16ms */
831 write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
832 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
834 ret = pasemi_mac_setup_rx_resources(dev);
835 if (ret)
836 goto out_rx_resources;
838 ret = pasemi_mac_setup_tx_resources(dev);
839 if (ret)
840 goto out_tx_resources;
842 write_mac_reg(mac, PAS_MAC_IPC_CHNL,
843 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
844 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
846 /* enable rx if */
847 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
848 PAS_DMA_RXINT_RCMDSTA_EN);
850 /* enable rx channel */
851 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
852 PAS_DMA_RXCHAN_CCMDSTA_EN |
853 PAS_DMA_RXCHAN_CCMDSTA_DU);
855 /* enable tx channel */
856 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
857 PAS_DMA_TXCHAN_TCMDSTA_EN);
859 pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
861 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
862 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
864 if (mac->type == MAC_TYPE_GMAC)
865 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
866 else
867 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
869 /* Enable interface in MAC */
870 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
872 ret = pasemi_mac_phy_init(dev);
873 /* Some configs don't have PHYs (XAUI etc), so don't complain about
874 * failed init due to -ENODEV.
876 if (ret && ret != -ENODEV)
877 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
879 netif_start_queue(dev);
880 napi_enable(&mac->napi);
882 /* Interrupts are a bit different for our DMA controller: While
883 * it's got one a regular PCI device header, the interrupt there
884 * is really the base of the range it's using. Each tx and rx
885 * channel has it's own interrupt source.
888 base_irq = virq_to_hw(mac->dma_pdev->irq);
890 mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
891 mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
893 ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
894 mac->tx->irq_name, dev);
895 if (ret) {
896 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
897 base_irq + mac->dma_txch, ret);
898 goto out_tx_int;
901 ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
902 mac->rx->irq_name, dev);
903 if (ret) {
904 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
905 base_irq + 20 + mac->dma_rxch, ret);
906 goto out_rx_int;
909 if (mac->phydev)
910 phy_start(mac->phydev);
912 return 0;
914 out_rx_int:
915 free_irq(mac->tx_irq, dev);
916 out_tx_int:
917 napi_disable(&mac->napi);
918 netif_stop_queue(dev);
919 pasemi_mac_free_tx_resources(dev);
920 out_tx_resources:
921 pasemi_mac_free_rx_resources(dev);
922 out_rx_resources:
924 return ret;
927 #define MAX_RETRIES 5000
929 static int pasemi_mac_close(struct net_device *dev)
931 struct pasemi_mac *mac = netdev_priv(dev);
932 unsigned int stat;
933 int retries;
935 if (mac->phydev) {
936 phy_stop(mac->phydev);
937 phy_disconnect(mac->phydev);
940 netif_stop_queue(dev);
941 napi_disable(&mac->napi);
943 /* Clean out any pending buffers */
944 pasemi_mac_clean_tx(mac);
945 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
947 /* Disable interface */
948 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
949 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
950 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
952 for (retries = 0; retries < MAX_RETRIES; retries++) {
953 stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
954 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
955 break;
956 cond_resched();
959 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
960 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
962 for (retries = 0; retries < MAX_RETRIES; retries++) {
963 stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
964 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
965 break;
966 cond_resched();
969 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
970 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
972 for (retries = 0; retries < MAX_RETRIES; retries++) {
973 stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
974 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
975 break;
976 cond_resched();
979 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
980 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
982 /* Then, disable the channel. This must be done separately from
983 * stopping, since you can't disable when active.
986 write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
987 write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
988 write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
990 free_irq(mac->tx_irq, dev);
991 free_irq(mac->rx_irq, dev);
993 /* Free resources */
994 pasemi_mac_free_rx_resources(dev);
995 pasemi_mac_free_tx_resources(dev);
997 return 0;
1000 static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1002 struct pasemi_mac *mac = netdev_priv(dev);
1003 struct pasemi_mac_txring *txring;
1004 struct pasemi_mac_buffer *info;
1005 struct pas_dma_xct_descr *dp;
1006 u64 dflags, mactx, ptr;
1007 dma_addr_t map;
1008 unsigned long flags;
1010 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
1012 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1013 const unsigned char *nh = skb_network_header(skb);
1015 switch (ip_hdr(skb)->protocol) {
1016 case IPPROTO_TCP:
1017 dflags |= XCT_MACTX_CSUM_TCP;
1018 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
1019 dflags |= XCT_MACTX_IPO(nh - skb->data);
1020 break;
1021 case IPPROTO_UDP:
1022 dflags |= XCT_MACTX_CSUM_UDP;
1023 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
1024 dflags |= XCT_MACTX_IPO(nh - skb->data);
1025 break;
1029 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1031 if (dma_mapping_error(map))
1032 return NETDEV_TX_BUSY;
1034 mactx = dflags | XCT_MACTX_LLEN(skb->len);
1035 ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
1037 txring = mac->tx;
1039 spin_lock_irqsave(&txring->lock, flags);
1041 if (RING_AVAIL(txring) <= 1) {
1042 spin_unlock_irqrestore(&txring->lock, flags);
1043 pasemi_mac_clean_tx(mac);
1044 pasemi_mac_restart_tx_intr(mac);
1045 spin_lock_irqsave(&txring->lock, flags);
1047 if (RING_AVAIL(txring) <= 1) {
1048 /* Still no room -- stop the queue and wait for tx
1049 * intr when there's room.
1051 netif_stop_queue(dev);
1052 goto out_err;
1056 dp = &TX_DESC(mac, txring->next_to_fill);
1057 info = &TX_DESC_INFO(mac, txring->next_to_fill);
1059 dp->mactx = mactx;
1060 dp->ptr = ptr;
1061 info->dma = map;
1062 info->skb = skb;
1064 txring->next_to_fill++;
1065 dev->stats.tx_packets++;
1066 dev->stats.tx_bytes += skb->len;
1068 spin_unlock_irqrestore(&txring->lock, flags);
1070 write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
1072 return NETDEV_TX_OK;
1074 out_err:
1075 spin_unlock_irqrestore(&txring->lock, flags);
1076 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1077 return NETDEV_TX_BUSY;
1080 static void pasemi_mac_set_rx_mode(struct net_device *dev)
1082 struct pasemi_mac *mac = netdev_priv(dev);
1083 unsigned int flags;
1085 flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
1087 /* Set promiscuous */
1088 if (dev->flags & IFF_PROMISC)
1089 flags |= PAS_MAC_CFG_PCFG_PR;
1090 else
1091 flags &= ~PAS_MAC_CFG_PCFG_PR;
1093 write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1097 static int pasemi_mac_poll(struct napi_struct *napi, int budget)
1099 struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1100 struct net_device *dev = mac->netdev;
1101 int pkts;
1103 pasemi_mac_clean_tx(mac);
1104 pkts = pasemi_mac_clean_rx(mac, budget);
1105 if (pkts < budget) {
1106 /* all done, no more packets present */
1107 netif_rx_complete(dev, napi);
1109 pasemi_mac_restart_rx_intr(mac);
1111 return pkts;
1114 static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1116 struct device_node *dn;
1117 void __iomem *ret;
1119 dn = pci_device_to_OF_node(p);
1120 if (!dn)
1121 goto fallback;
1123 ret = of_iomap(dn, index);
1124 if (!ret)
1125 goto fallback;
1127 return ret;
1128 fallback:
1129 /* This is hardcoded and ugly, but we have some firmware versions
1130 * that don't provide the register space in the device tree. Luckily
1131 * they are at well-known locations so we can just do the math here.
1133 return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1136 static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1138 struct resource res;
1139 struct device_node *dn;
1140 int err;
1142 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1143 if (!mac->dma_pdev) {
1144 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1145 return -ENODEV;
1148 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1149 if (!mac->iob_pdev) {
1150 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1151 return -ENODEV;
1154 mac->regs = map_onedev(mac->pdev, 0);
1155 mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1156 mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1158 if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1159 dev_err(&mac->pdev->dev, "Can't map registers\n");
1160 return -ENODEV;
1163 /* The dma status structure is located in the I/O bridge, and
1164 * is cache coherent.
1166 if (!dma_status) {
1167 dn = pci_device_to_OF_node(mac->iob_pdev);
1168 if (dn)
1169 err = of_address_to_resource(dn, 1, &res);
1170 if (!dn || err) {
1171 /* Fallback for old firmware */
1172 res.start = 0xfd800000;
1173 res.end = res.start + 0x1000;
1175 dma_status = __ioremap(res.start, res.end-res.start, 0);
1178 return 0;
1181 static int __devinit
1182 pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1184 static int index = 0;
1185 struct net_device *dev;
1186 struct pasemi_mac *mac;
1187 int err;
1188 DECLARE_MAC_BUF(mac_buf);
1190 err = pci_enable_device(pdev);
1191 if (err)
1192 return err;
1194 dev = alloc_etherdev(sizeof(struct pasemi_mac));
1195 if (dev == NULL) {
1196 dev_err(&pdev->dev,
1197 "pasemi_mac: Could not allocate ethernet device.\n");
1198 err = -ENOMEM;
1199 goto out_disable_device;
1202 pci_set_drvdata(pdev, dev);
1203 SET_NETDEV_DEV(dev, &pdev->dev);
1205 mac = netdev_priv(dev);
1207 mac->pdev = pdev;
1208 mac->netdev = dev;
1210 netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1212 dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX;
1214 /* These should come out of the device tree eventually */
1215 mac->dma_txch = index;
1216 mac->dma_rxch = index;
1218 /* We probe GMAC before XAUI, but the DMA interfaces are
1219 * in XAUI, GMAC order.
1221 if (index < 4)
1222 mac->dma_if = index + 2;
1223 else
1224 mac->dma_if = index - 4;
1225 index++;
1227 switch (pdev->device) {
1228 case 0xa005:
1229 mac->type = MAC_TYPE_GMAC;
1230 break;
1231 case 0xa006:
1232 mac->type = MAC_TYPE_XAUI;
1233 break;
1234 default:
1235 err = -ENODEV;
1236 goto out;
1239 /* get mac addr from device tree */
1240 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1241 err = -ENODEV;
1242 goto out;
1244 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1246 dev->open = pasemi_mac_open;
1247 dev->stop = pasemi_mac_close;
1248 dev->hard_start_xmit = pasemi_mac_start_tx;
1249 dev->set_multicast_list = pasemi_mac_set_rx_mode;
1251 err = pasemi_mac_map_regs(mac);
1252 if (err)
1253 goto out;
1255 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1256 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1258 mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1260 /* Enable most messages by default */
1261 mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1263 err = register_netdev(dev);
1265 if (err) {
1266 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1267 err);
1268 goto out;
1269 } else if netif_msg_probe(mac)
1270 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1271 "hw addr %s\n",
1272 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1273 mac->dma_if, mac->dma_txch, mac->dma_rxch,
1274 print_mac(mac_buf, dev->dev_addr));
1276 return err;
1278 out:
1279 if (mac->iob_pdev)
1280 pci_dev_put(mac->iob_pdev);
1281 if (mac->dma_pdev)
1282 pci_dev_put(mac->dma_pdev);
1283 if (mac->dma_regs)
1284 iounmap(mac->dma_regs);
1285 if (mac->iob_regs)
1286 iounmap(mac->iob_regs);
1287 if (mac->regs)
1288 iounmap(mac->regs);
1290 free_netdev(dev);
1291 out_disable_device:
1292 pci_disable_device(pdev);
1293 return err;
1297 static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1299 struct net_device *netdev = pci_get_drvdata(pdev);
1300 struct pasemi_mac *mac;
1302 if (!netdev)
1303 return;
1305 mac = netdev_priv(netdev);
1307 unregister_netdev(netdev);
1309 pci_disable_device(pdev);
1310 pci_dev_put(mac->dma_pdev);
1311 pci_dev_put(mac->iob_pdev);
1313 iounmap(mac->regs);
1314 iounmap(mac->dma_regs);
1315 iounmap(mac->iob_regs);
1317 pci_set_drvdata(pdev, NULL);
1318 free_netdev(netdev);
1321 static struct pci_device_id pasemi_mac_pci_tbl[] = {
1322 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1323 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
1324 { },
1327 MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1329 static struct pci_driver pasemi_mac_driver = {
1330 .name = "pasemi_mac",
1331 .id_table = pasemi_mac_pci_tbl,
1332 .probe = pasemi_mac_probe,
1333 .remove = __devexit_p(pasemi_mac_remove),
1336 static void __exit pasemi_mac_cleanup_module(void)
1338 pci_unregister_driver(&pasemi_mac_driver);
1339 __iounmap(dma_status);
1340 dma_status = NULL;
1343 int pasemi_mac_init_module(void)
1345 return pci_register_driver(&pasemi_mac_driver);
1348 module_init(pasemi_mac_init_module);
1349 module_exit(pasemi_mac_cleanup_module);