Merge branch 'linux-2.6'
[linux-2.6] / drivers / net / pasemi_mac.c
1 /*
2  * Copyright (C) 2006-2007 PA Semi, Inc
3  *
4  * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
5  *
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.
9  *
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.
14  *
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
18  */
19
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>
31
32 #include <linux/ip.h>
33 #include <linux/tcp.h>
34 #include <net/checksum.h>
35
36 #include <asm/irq.h>
37
38 #include "pasemi_mac.h"
39
40
41 /* TODO list
42  *
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
49  */
50
51
52 /* Must be a power of two */
53 #define RX_RING_SIZE 512
54 #define TX_RING_SIZE 512
55
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)
65
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)])
71
72 #define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
73
74 MODULE_LICENSE("GPL");
75 MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
76 MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
77
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");
81
82 static struct pasdma_status *dma_status;
83
84 static int pasemi_get_mac_addr(struct pasemi_mac *mac)
85 {
86         struct pci_dev *pdev = mac->pdev;
87         struct device_node *dn = pci_device_to_OF_node(pdev);
88         const u8 *maddr;
89         u8 addr[6];
90
91         if (!dn) {
92                 dev_dbg(&pdev->dev,
93                           "No device node for mac, not configuring\n");
94                 return -ENOENT;
95         }
96
97         maddr = of_get_property(dn, "local-mac-address", NULL);
98
99         /* Fall back to mac-address for older firmware */
100         if (maddr == NULL)
101                 maddr = of_get_property(dn, "mac-address", NULL);
102
103         if (maddr == NULL) {
104                 dev_warn(&pdev->dev,
105                          "no mac address in device tree, not configuring\n");
106                 return -ENOENT;
107         }
108
109         if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
110                    &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
111                 dev_warn(&pdev->dev,
112                          "can't parse mac address, not configuring\n");
113                 return -EINVAL;
114         }
115
116         memcpy(mac->mac_addr, addr, sizeof(addr));
117         return 0;
118 }
119
120 static int pasemi_mac_setup_rx_resources(struct net_device *dev)
121 {
122         struct pasemi_mac_rxring *ring;
123         struct pasemi_mac *mac = netdev_priv(dev);
124         int chan_id = mac->dma_rxch;
125
126         ring = kzalloc(sizeof(*ring), GFP_KERNEL);
127
128         if (!ring)
129                 goto out_ring;
130
131         spin_lock_init(&ring->lock);
132
133         ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
134                                   RX_RING_SIZE, GFP_KERNEL);
135
136         if (!ring->desc_info)
137                 goto out_desc_info;
138
139         /* Allocate descriptors */
140         ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
141                                         RX_RING_SIZE *
142                                         sizeof(struct pas_dma_xct_descr),
143                                         &ring->dma, GFP_KERNEL);
144
145         if (!ring->desc)
146                 goto out_desc;
147
148         memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
149
150         ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
151                                            RX_RING_SIZE * sizeof(u64),
152                                            &ring->buf_dma, GFP_KERNEL);
153         if (!ring->buffers)
154                 goto out_buffers;
155
156         memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
157
158         pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEL(chan_id),
159                                PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
160
161         pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEU(chan_id),
162                                PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
163                                PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
164
165         pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_CFG(chan_id),
166                                PAS_DMA_RXCHAN_CFG_HBU(1));
167
168         pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEL(mac->dma_if),
169                                PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
170
171         pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEU(mac->dma_if),
172                                PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
173                                PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
174
175         ring->next_to_fill = 0;
176         ring->next_to_clean = 0;
177
178         snprintf(ring->irq_name, sizeof(ring->irq_name),
179                  "%s rx", dev->name);
180         mac->rx = ring;
181
182         return 0;
183
184 out_buffers:
185         dma_free_coherent(&mac->dma_pdev->dev,
186                           RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
187                           mac->rx->desc, mac->rx->dma);
188 out_desc:
189         kfree(ring->desc_info);
190 out_desc_info:
191         kfree(ring);
192 out_ring:
193         return -ENOMEM;
194 }
195
196
197 static int pasemi_mac_setup_tx_resources(struct net_device *dev)
198 {
199         struct pasemi_mac *mac = netdev_priv(dev);
200         u32 val;
201         int chan_id = mac->dma_txch;
202         struct pasemi_mac_txring *ring;
203
204         ring = kzalloc(sizeof(*ring), GFP_KERNEL);
205         if (!ring)
206                 goto out_ring;
207
208         spin_lock_init(&ring->lock);
209
210         ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
211                                   TX_RING_SIZE, GFP_KERNEL);
212         if (!ring->desc_info)
213                 goto out_desc_info;
214
215         /* Allocate descriptors */
216         ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
217                                         TX_RING_SIZE *
218                                         sizeof(struct pas_dma_xct_descr),
219                                         &ring->dma, GFP_KERNEL);
220         if (!ring->desc)
221                 goto out_desc;
222
223         memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
224
225         pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEL(chan_id),
226                                PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
227         val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
228         val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
229
230         pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEU(chan_id), val);
231
232         pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_CFG(chan_id),
233                                PAS_DMA_TXCHAN_CFG_TY_IFACE |
234                                PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
235                                PAS_DMA_TXCHAN_CFG_UP |
236                                PAS_DMA_TXCHAN_CFG_WT(2));
237
238         ring->next_to_use = 0;
239         ring->next_to_clean = 0;
240
241         snprintf(ring->irq_name, sizeof(ring->irq_name),
242                  "%s tx", dev->name);
243         mac->tx = ring;
244
245         return 0;
246
247 out_desc:
248         kfree(ring->desc_info);
249 out_desc_info:
250         kfree(ring);
251 out_ring:
252         return -ENOMEM;
253 }
254
255 static void pasemi_mac_free_tx_resources(struct net_device *dev)
256 {
257         struct pasemi_mac *mac = netdev_priv(dev);
258         unsigned int i;
259         struct pasemi_mac_buffer *info;
260         struct pas_dma_xct_descr *dp;
261
262         for (i = 0; i < TX_RING_SIZE; i++) {
263                 info = &TX_DESC_INFO(mac, i);
264                 dp = &TX_DESC(mac, i);
265                 if (info->dma) {
266                         if (info->skb) {
267                                 pci_unmap_single(mac->dma_pdev,
268                                                  info->dma,
269                                                  info->skb->len,
270                                                  PCI_DMA_TODEVICE);
271                                 dev_kfree_skb_any(info->skb);
272                         }
273                         info->dma = 0;
274                         info->skb = NULL;
275                         dp->mactx = 0;
276                         dp->ptr = 0;
277                 }
278         }
279
280         dma_free_coherent(&mac->dma_pdev->dev,
281                           TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
282                           mac->tx->desc, mac->tx->dma);
283
284         kfree(mac->tx->desc_info);
285         kfree(mac->tx);
286         mac->tx = NULL;
287 }
288
289 static void pasemi_mac_free_rx_resources(struct net_device *dev)
290 {
291         struct pasemi_mac *mac = netdev_priv(dev);
292         unsigned int i;
293         struct pasemi_mac_buffer *info;
294         struct pas_dma_xct_descr *dp;
295
296         for (i = 0; i < RX_RING_SIZE; i++) {
297                 info = &RX_DESC_INFO(mac, i);
298                 dp = &RX_DESC(mac, i);
299                 if (info->skb) {
300                         if (info->dma) {
301                                 pci_unmap_single(mac->dma_pdev,
302                                                  info->dma,
303                                                  info->skb->len,
304                                                  PCI_DMA_FROMDEVICE);
305                                 dev_kfree_skb_any(info->skb);
306                         }
307                         info->dma = 0;
308                         info->skb = NULL;
309                         dp->macrx = 0;
310                         dp->ptr = 0;
311                 }
312         }
313
314         dma_free_coherent(&mac->dma_pdev->dev,
315                           RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
316                           mac->rx->desc, mac->rx->dma);
317
318         dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
319                           mac->rx->buffers, mac->rx->buf_dma);
320
321         kfree(mac->rx->desc_info);
322         kfree(mac->rx);
323         mac->rx = NULL;
324 }
325
326 static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
327 {
328         struct pasemi_mac *mac = netdev_priv(dev);
329         unsigned int i;
330         int start = mac->rx->next_to_fill;
331         unsigned int limit, count;
332
333         limit = (mac->rx->next_to_clean + RX_RING_SIZE -
334                  mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
335
336         /* Check to see if we're doing first-time setup */
337         if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
338                 limit = RX_RING_SIZE;
339
340         if (limit <= 0)
341                 return;
342
343         i = start;
344         for (count = limit; count; count--) {
345                 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
346                 u64 *buff = &RX_BUFF(mac, i);
347                 struct sk_buff *skb;
348                 dma_addr_t dma;
349
350                 /* skb might still be in there for recycle on short receives */
351                 if (info->skb)
352                         skb = info->skb;
353                 else
354                         skb = dev_alloc_skb(BUF_SIZE);
355
356                 if (unlikely(!skb))
357                         break;
358
359                 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
360                                      PCI_DMA_FROMDEVICE);
361
362                 if (unlikely(dma_mapping_error(dma))) {
363                         dev_kfree_skb_irq(info->skb);
364                         break;
365                 }
366
367                 info->skb = skb;
368                 info->dma = dma;
369                 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
370                 i++;
371         }
372
373         wmb();
374
375         pci_write_config_dword(mac->dma_pdev,
376                                PAS_DMA_RXCHAN_INCR(mac->dma_rxch),
377                                limit - count);
378         pci_write_config_dword(mac->dma_pdev,
379                                PAS_DMA_RXINT_INCR(mac->dma_if),
380                                limit - count);
381
382         mac->rx->next_to_fill += limit - count;
383 }
384
385 static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
386 {
387         unsigned int reg, stat;
388         /* Re-enable packet count interrupts: finally
389          * ack the packet count interrupt we got in rx_intr.
390          */
391
392         pci_read_config_dword(mac->iob_pdev,
393                               PAS_IOB_DMA_RXCH_STAT(mac->dma_rxch),
394                               &stat);
395
396         reg = PAS_IOB_DMA_RXCH_RESET_PCNT(stat & PAS_IOB_DMA_RXCH_STAT_CNTDEL_M)
397                 | PAS_IOB_DMA_RXCH_RESET_PINTC;
398
399         pci_write_config_dword(mac->iob_pdev,
400                                PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch),
401                                reg);
402 }
403
404 static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
405 {
406         unsigned int reg, stat;
407
408         /* Re-enable packet count interrupts */
409         pci_read_config_dword(mac->iob_pdev,
410                               PAS_IOB_DMA_TXCH_STAT(mac->dma_txch), &stat);
411
412         reg = PAS_IOB_DMA_TXCH_RESET_PCNT(stat & PAS_IOB_DMA_TXCH_STAT_CNTDEL_M)
413                 | PAS_IOB_DMA_TXCH_RESET_PINTC;
414
415         pci_write_config_dword(mac->iob_pdev,
416                                PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
417 }
418
419
420 static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
421 {
422         unsigned int n;
423         int count;
424         struct pas_dma_xct_descr *dp;
425         struct pasemi_mac_buffer *info;
426         struct sk_buff *skb;
427         unsigned int i, len;
428         u64 macrx;
429         dma_addr_t dma;
430
431         spin_lock(&mac->rx->lock);
432
433         n = mac->rx->next_to_clean;
434
435         for (count = limit; count; count--) {
436
437                 rmb();
438
439                 dp = &RX_DESC(mac, n);
440                 macrx = dp->macrx;
441
442                 if (!(macrx & XCT_MACRX_O))
443                         break;
444
445
446                 info = NULL;
447
448                 /* We have to scan for our skb since there's no way
449                  * to back-map them from the descriptor, and if we
450                  * have several receive channels then they might not
451                  * show up in the same order as they were put on the
452                  * interface ring.
453                  */
454
455                 dma = (dp->ptr & XCT_PTR_ADDR_M);
456                 for (i = n; i < (n + RX_RING_SIZE); i++) {
457                         info = &RX_DESC_INFO(mac, i);
458                         if (info->dma == dma)
459                                 break;
460                 }
461
462                 skb = info->skb;
463                 info->dma = 0;
464
465                 pci_unmap_single(mac->dma_pdev, dma, skb->len,
466                                  PCI_DMA_FROMDEVICE);
467
468                 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
469
470                 if (len < 256) {
471                         struct sk_buff *new_skb =
472                             netdev_alloc_skb(mac->netdev, len + NET_IP_ALIGN);
473                         if (new_skb) {
474                                 skb_reserve(new_skb, NET_IP_ALIGN);
475                                 memcpy(new_skb->data - NET_IP_ALIGN,
476                                         skb->data - NET_IP_ALIGN,
477                                         len + NET_IP_ALIGN);
478                                 /* save the skb in buffer_info as good */
479                                 skb = new_skb;
480                         }
481                         /* else just continue with the old one */
482                 } else
483                         info->skb = NULL;
484
485                 skb_put(skb, len);
486
487                 skb->protocol = eth_type_trans(skb, mac->netdev);
488
489                 if ((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
490                         skb->ip_summed = CHECKSUM_COMPLETE;
491                         skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
492                                            XCT_MACRX_CSUM_S;
493                 } else
494                         skb->ip_summed = CHECKSUM_NONE;
495
496                 mac->stats.rx_bytes += len;
497                 mac->stats.rx_packets++;
498
499                 netif_receive_skb(skb);
500
501                 dp->ptr = 0;
502                 dp->macrx = 0;
503
504                 n++;
505         }
506
507         mac->rx->next_to_clean += limit - count;
508         pasemi_mac_replenish_rx_ring(mac->netdev);
509
510         spin_unlock(&mac->rx->lock);
511
512         return count;
513 }
514
515 static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
516 {
517         int i;
518         struct pasemi_mac_buffer *info;
519         struct pas_dma_xct_descr *dp;
520         int start, count;
521         int flags;
522
523         spin_lock_irqsave(&mac->tx->lock, flags);
524
525         start = mac->tx->next_to_clean;
526         count = 0;
527
528         for (i = start; i < mac->tx->next_to_use; i++) {
529                 dp = &TX_DESC(mac, i);
530                 if (!dp || (dp->mactx & XCT_MACTX_O))
531                         break;
532
533                 count++;
534
535                 info = &TX_DESC_INFO(mac, i);
536
537                 pci_unmap_single(mac->dma_pdev, info->dma,
538                                  info->skb->len, PCI_DMA_TODEVICE);
539                 dev_kfree_skb_irq(info->skb);
540
541                 info->skb = NULL;
542                 info->dma = 0;
543                 dp->mactx = 0;
544                 dp->ptr = 0;
545         }
546         mac->tx->next_to_clean += count;
547         spin_unlock_irqrestore(&mac->tx->lock, flags);
548
549         netif_wake_queue(mac->netdev);
550
551         return count;
552 }
553
554
555 static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
556 {
557         struct net_device *dev = data;
558         struct pasemi_mac *mac = netdev_priv(dev);
559         unsigned int reg;
560
561         if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
562                 return IRQ_NONE;
563
564         if (*mac->rx_status & PAS_STATUS_ERROR)
565                 printk("rx_status reported error\n");
566
567         /* Don't reset packet count so it won't fire again but clear
568          * all others.
569          */
570
571         pci_read_config_dword(mac->dma_pdev, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), &reg);
572
573         reg = 0;
574         if (*mac->rx_status & PAS_STATUS_SOFT)
575                 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
576         if (*mac->rx_status & PAS_STATUS_ERROR)
577                 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
578         if (*mac->rx_status & PAS_STATUS_TIMER)
579                 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
580
581         netif_rx_schedule(dev);
582
583         pci_write_config_dword(mac->iob_pdev,
584                                PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
585
586
587         return IRQ_HANDLED;
588 }
589
590 static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
591 {
592         struct net_device *dev = data;
593         struct pasemi_mac *mac = netdev_priv(dev);
594         unsigned int reg;
595
596         if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
597                 return IRQ_NONE;
598
599         pasemi_mac_clean_tx(mac);
600
601         reg = PAS_IOB_DMA_TXCH_RESET_PINTC;
602
603         if (*mac->tx_status & PAS_STATUS_SOFT)
604                 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
605         if (*mac->tx_status & PAS_STATUS_ERROR)
606                 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
607
608         pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
609                                reg);
610
611         return IRQ_HANDLED;
612 }
613
614 static void pasemi_adjust_link(struct net_device *dev)
615 {
616         struct pasemi_mac *mac = netdev_priv(dev);
617         int msg;
618         unsigned int flags;
619         unsigned int new_flags;
620
621         if (!mac->phydev->link) {
622                 /* If no link, MAC speed settings don't matter. Just report
623                  * link down and return.
624                  */
625                 if (mac->link && netif_msg_link(mac))
626                         printk(KERN_INFO "%s: Link is down.\n", dev->name);
627
628                 netif_carrier_off(dev);
629                 mac->link = 0;
630
631                 return;
632         } else
633                 netif_carrier_on(dev);
634
635         pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
636         new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
637                               PAS_MAC_CFG_PCFG_TSR_M);
638
639         if (!mac->phydev->duplex)
640                 new_flags |= PAS_MAC_CFG_PCFG_HD;
641
642         switch (mac->phydev->speed) {
643         case 1000:
644                 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
645                              PAS_MAC_CFG_PCFG_TSR_1G;
646                 break;
647         case 100:
648                 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
649                              PAS_MAC_CFG_PCFG_TSR_100M;
650                 break;
651         case 10:
652                 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
653                              PAS_MAC_CFG_PCFG_TSR_10M;
654                 break;
655         default:
656                 printk("Unsupported speed %d\n", mac->phydev->speed);
657         }
658
659         /* Print on link or speed/duplex change */
660         msg = mac->link != mac->phydev->link || flags != new_flags;
661
662         mac->duplex = mac->phydev->duplex;
663         mac->speed = mac->phydev->speed;
664         mac->link = mac->phydev->link;
665
666         if (new_flags != flags)
667                 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, new_flags);
668
669         if (msg && netif_msg_link(mac))
670                 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
671                        dev->name, mac->speed, mac->duplex ? "full" : "half");
672 }
673
674 static int pasemi_mac_phy_init(struct net_device *dev)
675 {
676         struct pasemi_mac *mac = netdev_priv(dev);
677         struct device_node *dn, *phy_dn;
678         struct phy_device *phydev;
679         unsigned int phy_id;
680         const phandle *ph;
681         const unsigned int *prop;
682         struct resource r;
683         int ret;
684
685         dn = pci_device_to_OF_node(mac->pdev);
686         ph = of_get_property(dn, "phy-handle", NULL);
687         if (!ph)
688                 return -ENODEV;
689         phy_dn = of_find_node_by_phandle(*ph);
690
691         prop = of_get_property(phy_dn, "reg", NULL);
692         ret = of_address_to_resource(phy_dn->parent, 0, &r);
693         if (ret)
694                 goto err;
695
696         phy_id = *prop;
697         snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
698
699         of_node_put(phy_dn);
700
701         mac->link = 0;
702         mac->speed = 0;
703         mac->duplex = -1;
704
705         phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
706
707         if (IS_ERR(phydev)) {
708                 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
709                 return PTR_ERR(phydev);
710         }
711
712         mac->phydev = phydev;
713
714         return 0;
715
716 err:
717         of_node_put(phy_dn);
718         return -ENODEV;
719 }
720
721
722 static int pasemi_mac_open(struct net_device *dev)
723 {
724         struct pasemi_mac *mac = netdev_priv(dev);
725         int base_irq;
726         unsigned int flags;
727         int ret;
728
729         /* enable rx section */
730         pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_RXCMD,
731                                PAS_DMA_COM_RXCMD_EN);
732
733         /* enable tx section */
734         pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_TXCMD,
735                                PAS_DMA_COM_TXCMD_EN);
736
737         flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
738                 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
739                 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
740
741         pci_write_config_dword(mac->pdev, PAS_MAC_CFG_TXP, flags);
742
743         flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
744                 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
745
746         flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
747
748         pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
749                                PAS_IOB_DMA_RXCH_CFG_CNTTH(1));
750
751         pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
752                                PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
753
754         /* Clear out any residual packet count state from firmware */
755         pasemi_mac_restart_rx_intr(mac);
756         pasemi_mac_restart_tx_intr(mac);
757
758         /* 0xffffff is max value, about 16ms */
759         pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
760                                PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
761
762         pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
763
764         ret = pasemi_mac_setup_rx_resources(dev);
765         if (ret)
766                 goto out_rx_resources;
767
768         ret = pasemi_mac_setup_tx_resources(dev);
769         if (ret)
770                 goto out_tx_resources;
771
772         pci_write_config_dword(mac->pdev, PAS_MAC_IPC_CHNL,
773                                PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
774                                PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
775
776         /* enable rx if */
777         pci_write_config_dword(mac->dma_pdev,
778                                PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
779                                PAS_DMA_RXINT_RCMDSTA_EN);
780
781         /* enable rx channel */
782         pci_write_config_dword(mac->dma_pdev,
783                                PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
784                                PAS_DMA_RXCHAN_CCMDSTA_EN |
785                                PAS_DMA_RXCHAN_CCMDSTA_DU);
786
787         /* enable tx channel */
788         pci_write_config_dword(mac->dma_pdev,
789                                PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
790                                PAS_DMA_TXCHAN_TCMDSTA_EN);
791
792         pasemi_mac_replenish_rx_ring(dev);
793
794         ret = pasemi_mac_phy_init(dev);
795         /* Some configs don't have PHYs (XAUI etc), so don't complain about
796          * failed init due to -ENODEV.
797          */
798         if (ret && ret != -ENODEV)
799                 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
800
801         netif_start_queue(dev);
802         netif_poll_enable(dev);
803
804         /* Interrupts are a bit different for our DMA controller: While
805          * it's got one a regular PCI device header, the interrupt there
806          * is really the base of the range it's using. Each tx and rx
807          * channel has it's own interrupt source.
808          */
809
810         base_irq = virq_to_hw(mac->dma_pdev->irq);
811
812         mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
813         mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);
814
815         ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
816                           mac->tx->irq_name, dev);
817         if (ret) {
818                 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
819                         base_irq + mac->dma_txch, ret);
820                 goto out_tx_int;
821         }
822
823         ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
824                           mac->rx->irq_name, dev);
825         if (ret) {
826                 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
827                         base_irq + 20 + mac->dma_rxch, ret);
828                 goto out_rx_int;
829         }
830
831         if (mac->phydev)
832                 phy_start(mac->phydev);
833
834         return 0;
835
836 out_rx_int:
837         free_irq(mac->tx_irq, dev);
838 out_tx_int:
839         netif_poll_disable(dev);
840         netif_stop_queue(dev);
841         pasemi_mac_free_tx_resources(dev);
842 out_tx_resources:
843         pasemi_mac_free_rx_resources(dev);
844 out_rx_resources:
845
846         return ret;
847 }
848
849 #define MAX_RETRIES 5000
850
851 static int pasemi_mac_close(struct net_device *dev)
852 {
853         struct pasemi_mac *mac = netdev_priv(dev);
854         unsigned int stat;
855         int retries;
856
857         if (mac->phydev) {
858                 phy_stop(mac->phydev);
859                 phy_disconnect(mac->phydev);
860         }
861
862         netif_stop_queue(dev);
863
864         /* Clean out any pending buffers */
865         pasemi_mac_clean_tx(mac);
866         pasemi_mac_clean_rx(mac, RX_RING_SIZE);
867
868         /* Disable interface */
869         pci_write_config_dword(mac->dma_pdev,
870                                PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
871                                PAS_DMA_TXCHAN_TCMDSTA_ST);
872         pci_write_config_dword(mac->dma_pdev,
873                       PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
874                       PAS_DMA_RXINT_RCMDSTA_ST);
875         pci_write_config_dword(mac->dma_pdev,
876                       PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
877                       PAS_DMA_RXCHAN_CCMDSTA_ST);
878
879         for (retries = 0; retries < MAX_RETRIES; retries++) {
880                 pci_read_config_dword(mac->dma_pdev,
881                                       PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
882                                       &stat);
883                 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
884                         break;
885                 cond_resched();
886         }
887
888         if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
889                 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
890
891         for (retries = 0; retries < MAX_RETRIES; retries++) {
892                 pci_read_config_dword(mac->dma_pdev,
893                                       PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
894                                       &stat);
895                 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
896                         break;
897                 cond_resched();
898         }
899
900         if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
901                 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
902
903         for (retries = 0; retries < MAX_RETRIES; retries++) {
904                 pci_read_config_dword(mac->dma_pdev,
905                                       PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
906                                       &stat);
907                 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
908                         break;
909                 cond_resched();
910         }
911
912         if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
913                 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
914
915         /* Then, disable the channel. This must be done separately from
916          * stopping, since you can't disable when active.
917          */
918
919         pci_write_config_dword(mac->dma_pdev,
920                                PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
921         pci_write_config_dword(mac->dma_pdev,
922                                PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
923         pci_write_config_dword(mac->dma_pdev,
924                                PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
925
926         free_irq(mac->tx_irq, dev);
927         free_irq(mac->rx_irq, dev);
928
929         /* Free resources */
930         pasemi_mac_free_rx_resources(dev);
931         pasemi_mac_free_tx_resources(dev);
932
933         return 0;
934 }
935
936 static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
937 {
938         struct pasemi_mac *mac = netdev_priv(dev);
939         struct pasemi_mac_txring *txring;
940         struct pasemi_mac_buffer *info;
941         struct pas_dma_xct_descr *dp;
942         u64 dflags;
943         dma_addr_t map;
944         int flags;
945
946         dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
947
948         if (skb->ip_summed == CHECKSUM_PARTIAL) {
949                 const unsigned char *nh = skb_network_header(skb);
950
951                 switch (ip_hdr(skb)->protocol) {
952                 case IPPROTO_TCP:
953                         dflags |= XCT_MACTX_CSUM_TCP;
954                         dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
955                         dflags |= XCT_MACTX_IPO(nh - skb->data);
956                         break;
957                 case IPPROTO_UDP:
958                         dflags |= XCT_MACTX_CSUM_UDP;
959                         dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
960                         dflags |= XCT_MACTX_IPO(nh - skb->data);
961                         break;
962                 }
963         }
964
965         map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
966
967         if (dma_mapping_error(map))
968                 return NETDEV_TX_BUSY;
969
970         txring = mac->tx;
971
972         spin_lock_irqsave(&txring->lock, flags);
973
974         if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
975                 spin_unlock_irqrestore(&txring->lock, flags);
976                 pasemi_mac_clean_tx(mac);
977                 spin_lock_irqsave(&txring->lock, flags);
978
979                 if (txring->next_to_clean - txring->next_to_use ==
980                     TX_RING_SIZE) {
981                         /* Still no room -- stop the queue and wait for tx
982                          * intr when there's room.
983                          */
984                         netif_stop_queue(dev);
985                         goto out_err;
986                 }
987         }
988
989
990         dp = &TX_DESC(mac, txring->next_to_use);
991         info = &TX_DESC_INFO(mac, txring->next_to_use);
992
993         dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
994         dp->ptr   = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
995         info->dma = map;
996         info->skb = skb;
997
998         txring->next_to_use++;
999         mac->stats.tx_packets++;
1000         mac->stats.tx_bytes += skb->len;
1001
1002         spin_unlock_irqrestore(&txring->lock, flags);
1003
1004         pci_write_config_dword(mac->dma_pdev,
1005                                PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
1006
1007         return NETDEV_TX_OK;
1008
1009 out_err:
1010         spin_unlock_irqrestore(&txring->lock, flags);
1011         pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
1012         return NETDEV_TX_BUSY;
1013 }
1014
1015 static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
1016 {
1017         struct pasemi_mac *mac = netdev_priv(dev);
1018
1019         return &mac->stats;
1020 }
1021
1022
1023 static void pasemi_mac_set_rx_mode(struct net_device *dev)
1024 {
1025         struct pasemi_mac *mac = netdev_priv(dev);
1026         unsigned int flags;
1027
1028         pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
1029
1030         /* Set promiscuous */
1031         if (dev->flags & IFF_PROMISC)
1032                 flags |= PAS_MAC_CFG_PCFG_PR;
1033         else
1034                 flags &= ~PAS_MAC_CFG_PCFG_PR;
1035
1036         pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
1037 }
1038
1039
1040 static int pasemi_mac_poll(struct net_device *dev, int *budget)
1041 {
1042         int pkts, limit = min(*budget, dev->quota);
1043         struct pasemi_mac *mac = netdev_priv(dev);
1044
1045         pkts = pasemi_mac_clean_rx(mac, limit);
1046
1047         dev->quota -= pkts;
1048         *budget -= pkts;
1049
1050         if (pkts < limit) {
1051                 /* all done, no more packets present */
1052                 netif_rx_complete(dev);
1053
1054                 pasemi_mac_restart_rx_intr(mac);
1055                 return 0;
1056         } else {
1057                 /* used up our quantum, so reschedule */
1058                 return 1;
1059         }
1060 }
1061
1062 static int __devinit
1063 pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1064 {
1065         static int index = 0;
1066         struct net_device *dev;
1067         struct pasemi_mac *mac;
1068         int err;
1069
1070         err = pci_enable_device(pdev);
1071         if (err)
1072                 return err;
1073
1074         dev = alloc_etherdev(sizeof(struct pasemi_mac));
1075         if (dev == NULL) {
1076                 dev_err(&pdev->dev,
1077                         "pasemi_mac: Could not allocate ethernet device.\n");
1078                 err = -ENOMEM;
1079                 goto out_disable_device;
1080         }
1081
1082         SET_MODULE_OWNER(dev);
1083         pci_set_drvdata(pdev, dev);
1084         SET_NETDEV_DEV(dev, &pdev->dev);
1085
1086         mac = netdev_priv(dev);
1087
1088         mac->pdev = pdev;
1089         mac->netdev = dev;
1090         mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1091
1092         if (!mac->dma_pdev) {
1093                 dev_err(&pdev->dev, "Can't find DMA Controller\n");
1094                 err = -ENODEV;
1095                 goto out_free_netdev;
1096         }
1097
1098         mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1099
1100         if (!mac->iob_pdev) {
1101                 dev_err(&pdev->dev, "Can't find I/O Bridge\n");
1102                 err = -ENODEV;
1103                 goto out_put_dma_pdev;
1104         }
1105
1106         /* These should come out of the device tree eventually */
1107         mac->dma_txch = index;
1108         mac->dma_rxch = index;
1109
1110         /* We probe GMAC before XAUI, but the DMA interfaces are
1111          * in XAUI, GMAC order.
1112          */
1113         if (index < 4)
1114                 mac->dma_if = index + 2;
1115         else
1116                 mac->dma_if = index - 4;
1117         index++;
1118
1119         switch (pdev->device) {
1120         case 0xa005:
1121                 mac->type = MAC_TYPE_GMAC;
1122                 break;
1123         case 0xa006:
1124                 mac->type = MAC_TYPE_XAUI;
1125                 break;
1126         default:
1127                 err = -ENODEV;
1128                 goto out;
1129         }
1130
1131         /* get mac addr from device tree */
1132         if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1133                 err = -ENODEV;
1134                 goto out;
1135         }
1136         memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1137
1138         dev->open = pasemi_mac_open;
1139         dev->stop = pasemi_mac_close;
1140         dev->hard_start_xmit = pasemi_mac_start_tx;
1141         dev->get_stats = pasemi_mac_get_stats;
1142         dev->set_multicast_list = pasemi_mac_set_rx_mode;
1143         dev->weight = 64;
1144         dev->poll = pasemi_mac_poll;
1145         dev->features = NETIF_F_HW_CSUM;
1146
1147         /* The dma status structure is located in the I/O bridge, and
1148          * is cache coherent.
1149          */
1150         if (!dma_status)
1151                 /* XXXOJN This should come from the device tree */
1152                 dma_status = __ioremap(0xfd800000, 0x1000, 0);
1153
1154         mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
1155         mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
1156
1157         mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1158
1159         /* Enable most messages by default */
1160         mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1161
1162         err = register_netdev(dev);
1163
1164         if (err) {
1165                 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1166                         err);
1167                 goto out;
1168         } else
1169                 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1170                        "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
1171                        dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1172                        mac->dma_if, mac->dma_txch, mac->dma_rxch,
1173                        dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1174                        dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1175
1176         return err;
1177
1178 out:
1179         pci_dev_put(mac->iob_pdev);
1180 out_put_dma_pdev:
1181         pci_dev_put(mac->dma_pdev);
1182 out_free_netdev:
1183         free_netdev(dev);
1184 out_disable_device:
1185         pci_disable_device(pdev);
1186         return err;
1187
1188 }
1189
1190 static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1191 {
1192         struct net_device *netdev = pci_get_drvdata(pdev);
1193         struct pasemi_mac *mac;
1194
1195         if (!netdev)
1196                 return;
1197
1198         mac = netdev_priv(netdev);
1199
1200         unregister_netdev(netdev);
1201
1202         pci_disable_device(pdev);
1203         pci_dev_put(mac->dma_pdev);
1204         pci_dev_put(mac->iob_pdev);
1205
1206         pci_set_drvdata(pdev, NULL);
1207         free_netdev(netdev);
1208 }
1209
1210 static struct pci_device_id pasemi_mac_pci_tbl[] = {
1211         { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1212         { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
1213 };
1214
1215 MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1216
1217 static struct pci_driver pasemi_mac_driver = {
1218         .name           = "pasemi_mac",
1219         .id_table       = pasemi_mac_pci_tbl,
1220         .probe          = pasemi_mac_probe,
1221         .remove         = __devexit_p(pasemi_mac_remove),
1222 };
1223
1224 static void __exit pasemi_mac_cleanup_module(void)
1225 {
1226         pci_unregister_driver(&pasemi_mac_driver);
1227         __iounmap(dma_status);
1228         dma_status = NULL;
1229 }
1230
1231 int pasemi_mac_init_module(void)
1232 {
1233         return pci_register_driver(&pasemi_mac_driver);
1234 }
1235
1236 module_init(pasemi_mac_init_module);
1237 module_exit(pasemi_mac_cleanup_module);