8319bc18bfa27deaeb27bf5c4a1fffe0f7d485fc
[linux-2.6.git] / 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 #include <asm/firmware.h>
38 #include <asm/pasemi_dma.h>
39
40 #include "pasemi_mac.h"
41
42 /* We have our own align, since ppc64 in general has it at 0 because
43  * of design flaws in some of the server bridge chips. However, for
44  * PWRficient doing the unaligned copies is more expensive than doing
45  * unaligned DMA, so make sure the data is aligned instead.
46  */
47 #define LOCAL_SKB_ALIGN 2
48
49 /* TODO list
50  *
51  * - Multicast support
52  * - Large MTU support
53  * - SW LRO
54  * - Multiqueue RX/TX
55  */
56
57
58 /* Must be a power of two */
59 #define RX_RING_SIZE 4096
60 #define TX_RING_SIZE 4096
61
62 #define DEFAULT_MSG_ENABLE        \
63         (NETIF_MSG_DRV          | \
64          NETIF_MSG_PROBE        | \
65          NETIF_MSG_LINK         | \
66          NETIF_MSG_TIMER        | \
67          NETIF_MSG_IFDOWN       | \
68          NETIF_MSG_IFUP         | \
69          NETIF_MSG_RX_ERR       | \
70          NETIF_MSG_TX_ERR)
71
72 #define TX_DESC(tx, num)        ((tx)->ring[(num) & (TX_RING_SIZE-1)])
73 #define TX_DESC_INFO(tx, num)   ((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
74 #define RX_DESC(rx, num)        ((rx)->ring[(num) & (RX_RING_SIZE-1)])
75 #define RX_DESC_INFO(rx, num)   ((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
76 #define RX_BUFF(rx, num)        ((rx)->buffers[(num) & (RX_RING_SIZE-1)])
77
78 #define RING_USED(ring)         (((ring)->next_to_fill - (ring)->next_to_clean) \
79                                  & ((ring)->size - 1))
80 #define RING_AVAIL(ring)        ((ring->size) - RING_USED(ring))
81
82 #define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
83
84 MODULE_LICENSE("GPL");
85 MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
86 MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
87
88 static int debug = -1;  /* -1 == use DEFAULT_MSG_ENABLE as value */
89 module_param(debug, int, 0);
90 MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
91
92 static struct pasdma_status *dma_status;
93
94 static int translation_enabled(void)
95 {
96 #if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
97         return 1;
98 #else
99         return firmware_has_feature(FW_FEATURE_LPAR);
100 #endif
101 }
102
103 static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
104                           unsigned int val)
105 {
106         out_le32(mac->iob_regs+reg, val);
107 }
108
109 static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
110 {
111         return in_le32(mac->regs+reg);
112 }
113
114 static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
115                           unsigned int val)
116 {
117         out_le32(mac->regs+reg, val);
118 }
119
120 static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
121 {
122         return in_le32(mac->dma_regs+reg);
123 }
124
125 static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
126                           unsigned int val)
127 {
128         out_le32(mac->dma_regs+reg, val);
129 }
130
131 static struct pasemi_mac_rxring *rx_ring(struct pasemi_mac *mac)
132 {
133         return mac->rx;
134 }
135
136 static struct pasemi_mac_txring *tx_ring(struct pasemi_mac *mac)
137 {
138         return mac->tx;
139 }
140
141 static int pasemi_get_mac_addr(struct pasemi_mac *mac)
142 {
143         struct pci_dev *pdev = mac->pdev;
144         struct device_node *dn = pci_device_to_OF_node(pdev);
145         int len;
146         const u8 *maddr;
147         u8 addr[6];
148
149         if (!dn) {
150                 dev_dbg(&pdev->dev,
151                           "No device node for mac, not configuring\n");
152                 return -ENOENT;
153         }
154
155         maddr = of_get_property(dn, "local-mac-address", &len);
156
157         if (maddr && len == 6) {
158                 memcpy(mac->mac_addr, maddr, 6);
159                 return 0;
160         }
161
162         /* Some old versions of firmware mistakenly uses mac-address
163          * (and as a string) instead of a byte array in local-mac-address.
164          */
165
166         if (maddr == NULL)
167                 maddr = of_get_property(dn, "mac-address", NULL);
168
169         if (maddr == NULL) {
170                 dev_warn(&pdev->dev,
171                          "no mac address in device tree, not configuring\n");
172                 return -ENOENT;
173         }
174
175
176         if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
177                    &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
178                 dev_warn(&pdev->dev,
179                          "can't parse mac address, not configuring\n");
180                 return -EINVAL;
181         }
182
183         memcpy(mac->mac_addr, addr, 6);
184
185         return 0;
186 }
187
188 static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
189                                     struct sk_buff *skb,
190                                     dma_addr_t *dmas)
191 {
192         int f;
193         int nfrags = skb_shinfo(skb)->nr_frags;
194
195         pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb),
196                          PCI_DMA_TODEVICE);
197
198         for (f = 0; f < nfrags; f++) {
199                 skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
200
201                 pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size,
202                                PCI_DMA_TODEVICE);
203         }
204         dev_kfree_skb_irq(skb);
205
206         /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
207          * aligned up to a power of 2
208          */
209         return (nfrags + 3) & ~1;
210 }
211
212 static int pasemi_mac_setup_rx_resources(struct net_device *dev)
213 {
214         struct pasemi_mac_rxring *ring;
215         struct pasemi_mac *mac = netdev_priv(dev);
216         int chan_id = mac->dma_rxch;
217         unsigned int cfg;
218
219         ring = kzalloc(sizeof(*ring), GFP_KERNEL);
220
221         if (!ring)
222                 goto out_ring;
223
224         spin_lock_init(&ring->lock);
225
226         ring->size = RX_RING_SIZE;
227         ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
228                                   RX_RING_SIZE, GFP_KERNEL);
229
230         if (!ring->ring_info)
231                 goto out_ring_info;
232
233         /* Allocate descriptors */
234         ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
235                                         RX_RING_SIZE * sizeof(u64),
236                                         &ring->dma, GFP_KERNEL);
237
238         if (!ring->ring)
239                 goto out_ring_desc;
240
241         memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64));
242
243         ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
244                                            RX_RING_SIZE * sizeof(u64),
245                                            &ring->buf_dma, GFP_KERNEL);
246         if (!ring->buffers)
247                 goto out_buffers;
248
249         memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
250
251         write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
252
253         write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
254                            PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
255                            PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
256
257         cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
258
259         if (translation_enabled())
260                 cfg |= PAS_DMA_RXCHAN_CFG_CTR;
261
262         write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id), cfg);
263
264         write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
265                            PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
266
267         write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
268                            PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
269                            PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
270
271         cfg = PAS_DMA_RXINT_CFG_DHL(3) | PAS_DMA_RXINT_CFG_L2 |
272               PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
273               PAS_DMA_RXINT_CFG_HEN;
274
275         if (translation_enabled())
276                 cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;
277
278         write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
279
280         ring->next_to_fill = 0;
281         ring->next_to_clean = 0;
282
283         ring->status = &dma_status->rx_sta[mac->dma_rxch];
284         ring->mac = mac;
285         mac->rx = ring;
286
287         return 0;
288
289 out_buffers:
290         dma_free_coherent(&mac->dma_pdev->dev,
291                           RX_RING_SIZE * sizeof(u64),
292                           rx_ring(mac)->ring, rx_ring(mac)->dma);
293 out_ring_desc:
294         kfree(ring->ring_info);
295 out_ring_info:
296         kfree(ring);
297 out_ring:
298         return -ENOMEM;
299 }
300
301 static struct pasemi_mac_txring *
302 pasemi_mac_setup_tx_resources(struct net_device *dev, int txch)
303 {
304         struct pasemi_mac *mac = netdev_priv(dev);
305         u32 val;
306         struct pasemi_mac_txring *ring;
307         unsigned int cfg;
308
309         ring = kzalloc(sizeof(*ring), GFP_KERNEL);
310         if (!ring)
311                 goto out_ring;
312
313         spin_lock_init(&ring->lock);
314
315         ring->size = TX_RING_SIZE;
316         ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
317                                   TX_RING_SIZE, GFP_KERNEL);
318         if (!ring->ring_info)
319                 goto out_ring_info;
320
321         /* Allocate descriptors */
322         ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
323                                         TX_RING_SIZE * sizeof(u64),
324                                         &ring->dma, GFP_KERNEL);
325         if (!ring->ring)
326                 goto out_ring_desc;
327
328         memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
329
330         write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(txch),
331                            PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
332         val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
333         val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
334
335         write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(txch), val);
336
337         cfg = PAS_DMA_TXCHAN_CFG_TY_IFACE |
338               PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
339               PAS_DMA_TXCHAN_CFG_UP |
340               PAS_DMA_TXCHAN_CFG_WT(2);
341
342         if (translation_enabled())
343                 cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;
344
345         write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(txch), cfg);
346
347         ring->next_to_fill = 0;
348         ring->next_to_clean = 0;
349         ring->status = &dma_status->tx_sta[txch];
350         ring->chan = txch;
351         ring->mac = mac;
352
353         return ring;
354
355 out_ring_desc:
356         kfree(ring->ring_info);
357 out_ring_info:
358         kfree(ring);
359 out_ring:
360         return NULL;
361 }
362
363 static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
364 {
365         struct pasemi_mac_txring *txring = tx_ring(mac);
366         unsigned int i, j;
367         struct pasemi_mac_buffer *info;
368         dma_addr_t dmas[MAX_SKB_FRAGS+1];
369         int freed;
370         int start, limit;
371
372         start = txring->next_to_clean;
373         limit = txring->next_to_fill;
374
375         /* Compensate for when fill has wrapped and clean has not */
376         if (start > limit)
377                 limit += TX_RING_SIZE;
378
379         for (i = start; i < limit; i += freed) {
380                 info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
381                 if (info->dma && info->skb) {
382                         for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
383                                 dmas[j] = txring->ring_info[(i+1+j) &
384                                                 (TX_RING_SIZE-1)].dma;
385                         freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
386                 } else
387                         freed = 2;
388         }
389
390         for (i = 0; i < TX_RING_SIZE; i++)
391                 txring->ring[i] = 0;
392
393         dma_free_coherent(&mac->dma_pdev->dev,
394                           TX_RING_SIZE * sizeof(u64),
395                           txring->ring, txring->dma);
396
397         kfree(txring->ring_info);
398         kfree(txring);
399 }
400
401 static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
402 {
403         struct pasemi_mac_rxring *rx = rx_ring(mac);
404         unsigned int i;
405         struct pasemi_mac_buffer *info;
406
407         for (i = 0; i < RX_RING_SIZE; i++) {
408                 info = &RX_DESC_INFO(rx, i);
409                 if (info->skb && info->dma) {
410                         pci_unmap_single(mac->dma_pdev,
411                                          info->dma,
412                                          info->skb->len,
413                                          PCI_DMA_FROMDEVICE);
414                         dev_kfree_skb_any(info->skb);
415                 }
416                 info->dma = 0;
417                 info->skb = NULL;
418         }
419
420         for (i = 0; i < RX_RING_SIZE; i++)
421                 RX_DESC(rx, i) = 0;
422
423         dma_free_coherent(&mac->dma_pdev->dev,
424                           RX_RING_SIZE * sizeof(u64),
425                           rx_ring(mac)->ring, rx_ring(mac)->dma);
426
427         dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
428                           rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
429
430         kfree(rx_ring(mac)->ring_info);
431         kfree(rx_ring(mac));
432         mac->rx = NULL;
433 }
434
435 static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
436 {
437         struct pasemi_mac *mac = netdev_priv(dev);
438         struct pasemi_mac_rxring *rx = rx_ring(mac);
439         int fill, count;
440
441         if (limit <= 0)
442                 return;
443
444         fill = rx_ring(mac)->next_to_fill;
445         for (count = 0; count < limit; count++) {
446                 struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
447                 u64 *buff = &RX_BUFF(rx, fill);
448                 struct sk_buff *skb;
449                 dma_addr_t dma;
450
451                 /* Entry in use? */
452                 WARN_ON(*buff);
453
454                 /* skb might still be in there for recycle on short receives */
455                 if (info->skb)
456                         skb = info->skb;
457                 else {
458                         skb = dev_alloc_skb(BUF_SIZE);
459                         skb_reserve(skb, LOCAL_SKB_ALIGN);
460                 }
461
462                 if (unlikely(!skb))
463                         break;
464
465                 dma = pci_map_single(mac->dma_pdev, skb->data,
466                                      BUF_SIZE - LOCAL_SKB_ALIGN,
467                                      PCI_DMA_FROMDEVICE);
468
469                 if (unlikely(dma_mapping_error(dma))) {
470                         dev_kfree_skb_irq(info->skb);
471                         break;
472                 }
473
474                 info->skb = skb;
475                 info->dma = dma;
476                 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
477                 fill++;
478         }
479
480         wmb();
481
482         write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
483
484         rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
485                                 (RX_RING_SIZE - 1);
486 }
487
488 static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
489 {
490         unsigned int reg, pcnt;
491         /* Re-enable packet count interrupts: finally
492          * ack the packet count interrupt we got in rx_intr.
493          */
494
495         pcnt = *rx_ring(mac)->status & PAS_STATUS_PCNT_M;
496
497         reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
498
499         write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
500 }
501
502 static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
503 {
504         unsigned int reg, pcnt;
505
506         /* Re-enable packet count interrupts */
507         pcnt = *tx_ring(mac)->status & PAS_STATUS_PCNT_M;
508
509         reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
510
511         write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan), reg);
512 }
513
514
515 static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
516 {
517         unsigned int rcmdsta, ccmdsta;
518
519         if (!netif_msg_rx_err(mac))
520                 return;
521
522         rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
523         ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
524
525         printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
526                 macrx, *rx_ring(mac)->status);
527
528         printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
529                 rcmdsta, ccmdsta);
530 }
531
532 static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
533 {
534         unsigned int cmdsta;
535
536         if (!netif_msg_tx_err(mac))
537                 return;
538
539         cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
540
541         printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
542                 "tx status 0x%016lx\n", mactx, *tx_ring(mac)->status);
543
544         printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
545 }
546
547 static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx, int limit)
548 {
549         struct pasemi_mac *mac = rx->mac;
550         unsigned int n;
551         int count;
552         struct pasemi_mac_buffer *info;
553         struct sk_buff *skb;
554         unsigned int len;
555         u64 macrx;
556         dma_addr_t dma;
557         int buf_index;
558         u64 eval;
559
560         spin_lock(&rx->lock);
561
562         n = rx->next_to_clean;
563
564         prefetch(&RX_DESC(rx, n));
565
566         for (count = 0; count < limit; count++) {
567                 macrx = RX_DESC(rx, n);
568
569                 if ((macrx & XCT_MACRX_E) ||
570                     (*rx_ring(mac)->status & PAS_STATUS_ERROR))
571                         pasemi_mac_rx_error(mac, macrx);
572
573                 if (!(macrx & XCT_MACRX_O))
574                         break;
575
576                 info = NULL;
577
578                 BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
579
580                 eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
581                         XCT_RXRES_8B_EVAL_S;
582                 buf_index = eval-1;
583
584                 dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
585                 info = &RX_DESC_INFO(rx, buf_index);
586
587                 skb = info->skb;
588
589                 prefetch(skb);
590                 prefetch(&skb->data_len);
591
592                 len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
593
594                 pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE);
595
596                 if (macrx & XCT_MACRX_CRC) {
597                         /* CRC error flagged */
598                         mac->netdev->stats.rx_errors++;
599                         mac->netdev->stats.rx_crc_errors++;
600                         /* No need to free skb, it'll be reused */
601                         goto next;
602                 }
603
604                 if (len < 256) {
605                         struct sk_buff *new_skb;
606
607                         new_skb = netdev_alloc_skb(mac->netdev,
608                                                    len + LOCAL_SKB_ALIGN);
609                         if (new_skb) {
610                                 skb_reserve(new_skb, LOCAL_SKB_ALIGN);
611                                 memcpy(new_skb->data, skb->data, len);
612                                 /* save the skb in buffer_info as good */
613                                 skb = new_skb;
614                         }
615                         /* else just continue with the old one */
616                 } else
617                         info->skb = NULL;
618
619                 info->dma = 0;
620
621                 /* Don't include CRC */
622                 skb_put(skb, len-4);
623
624                 if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
625                         skb->ip_summed = CHECKSUM_UNNECESSARY;
626                         skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
627                                            XCT_MACRX_CSUM_S;
628                 } else
629                         skb->ip_summed = CHECKSUM_NONE;
630
631                 mac->netdev->stats.rx_bytes += len;
632                 mac->netdev->stats.rx_packets++;
633
634                 skb->protocol = eth_type_trans(skb, mac->netdev);
635                 netif_receive_skb(skb);
636
637 next:
638                 RX_DESC(rx, n) = 0;
639                 RX_DESC(rx, n+1) = 0;
640
641                 /* Need to zero it out since hardware doesn't, since the
642                  * replenish loop uses it to tell when it's done.
643                  */
644                 RX_BUFF(rx, buf_index) = 0;
645
646                 n += 4;
647         }
648
649         if (n > RX_RING_SIZE) {
650                 /* Errata 5971 workaround: L2 target of headers */
651                 write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0);
652                 n &= (RX_RING_SIZE-1);
653         }
654
655         rx_ring(mac)->next_to_clean = n;
656
657         /* Increase is in number of 16-byte entries, and since each descriptor
658          * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
659          * count*2.
660          */
661         write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count << 1);
662
663         pasemi_mac_replenish_rx_ring(mac->netdev, count);
664
665         spin_unlock(&rx_ring(mac)->lock);
666
667         return count;
668 }
669
670 /* Can't make this too large or we blow the kernel stack limits */
671 #define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
672
673 static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
674 {
675         struct pasemi_mac *mac = txring->mac;
676         int i, j;
677         unsigned int start, descr_count, buf_count, batch_limit;
678         unsigned int ring_limit;
679         unsigned int total_count;
680         unsigned long flags;
681         struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
682         dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
683
684         total_count = 0;
685         batch_limit = TX_CLEAN_BATCHSIZE;
686 restart:
687         spin_lock_irqsave(&txring->lock, flags);
688
689         start = txring->next_to_clean;
690         ring_limit = txring->next_to_fill;
691
692         /* Compensate for when fill has wrapped but clean has not */
693         if (start > ring_limit)
694                 ring_limit += TX_RING_SIZE;
695
696         buf_count = 0;
697         descr_count = 0;
698
699         for (i = start;
700              descr_count < batch_limit && i < ring_limit;
701              i += buf_count) {
702                 u64 mactx = TX_DESC(txring, i);
703                 struct sk_buff *skb;
704
705                 if ((mactx  & XCT_MACTX_E) ||
706                     (*tx_ring(mac)->status & PAS_STATUS_ERROR))
707                         pasemi_mac_tx_error(mac, mactx);
708
709                 if (unlikely(mactx & XCT_MACTX_O))
710                         /* Not yet transmitted */
711                         break;
712
713                 skb = TX_DESC_INFO(txring, i+1).skb;
714                 skbs[descr_count] = skb;
715
716                 buf_count = 2 + skb_shinfo(skb)->nr_frags;
717                 for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
718                         dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
719
720                 TX_DESC(txring, i) = 0;
721                 TX_DESC(txring, i+1) = 0;
722
723                 /* Since we always fill with an even number of entries, make
724                  * sure we skip any unused one at the end as well.
725                  */
726                 if (buf_count & 1)
727                         buf_count++;
728                 descr_count++;
729         }
730         txring->next_to_clean = i & (TX_RING_SIZE-1);
731
732         spin_unlock_irqrestore(&txring->lock, flags);
733         netif_wake_queue(mac->netdev);
734
735         for (i = 0; i < descr_count; i++)
736                 pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
737
738         total_count += descr_count;
739
740         /* If the batch was full, try to clean more */
741         if (descr_count == batch_limit)
742                 goto restart;
743
744         return total_count;
745 }
746
747
748 static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
749 {
750         struct net_device *dev = data;
751         struct pasemi_mac *mac = netdev_priv(dev);
752         unsigned int reg;
753
754         if (!(*rx_ring(mac)->status & PAS_STATUS_CAUSE_M))
755                 return IRQ_NONE;
756
757         /* Don't reset packet count so it won't fire again but clear
758          * all others.
759          */
760
761         reg = 0;
762         if (*rx_ring(mac)->status & PAS_STATUS_SOFT)
763                 reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
764         if (*rx_ring(mac)->status & PAS_STATUS_ERROR)
765                 reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
766         if (*rx_ring(mac)->status & PAS_STATUS_TIMER)
767                 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
768
769         netif_rx_schedule(dev, &mac->napi);
770
771         write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
772
773         return IRQ_HANDLED;
774 }
775
776 static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
777 {
778         struct pasemi_mac_txring *txring = data;
779         struct pasemi_mac *mac = txring->mac;
780         unsigned int reg, pcnt;
781
782         if (!(*txring->status & PAS_STATUS_CAUSE_M))
783                 return IRQ_NONE;
784
785         pasemi_mac_clean_tx(txring);
786
787         pcnt = *txring->status & PAS_STATUS_PCNT_M;
788
789         reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
790
791         if (*txring->status & PAS_STATUS_SOFT)
792                 reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
793         if (*txring->status & PAS_STATUS_ERROR)
794                 reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
795
796         write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(txring->chan), reg);
797
798         return IRQ_HANDLED;
799 }
800
801 static void pasemi_adjust_link(struct net_device *dev)
802 {
803         struct pasemi_mac *mac = netdev_priv(dev);
804         int msg;
805         unsigned int flags;
806         unsigned int new_flags;
807
808         if (!mac->phydev->link) {
809                 /* If no link, MAC speed settings don't matter. Just report
810                  * link down and return.
811                  */
812                 if (mac->link && netif_msg_link(mac))
813                         printk(KERN_INFO "%s: Link is down.\n", dev->name);
814
815                 netif_carrier_off(dev);
816                 mac->link = 0;
817
818                 return;
819         } else
820                 netif_carrier_on(dev);
821
822         flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
823         new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
824                               PAS_MAC_CFG_PCFG_TSR_M);
825
826         if (!mac->phydev->duplex)
827                 new_flags |= PAS_MAC_CFG_PCFG_HD;
828
829         switch (mac->phydev->speed) {
830         case 1000:
831                 new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
832                              PAS_MAC_CFG_PCFG_TSR_1G;
833                 break;
834         case 100:
835                 new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
836                              PAS_MAC_CFG_PCFG_TSR_100M;
837                 break;
838         case 10:
839                 new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
840                              PAS_MAC_CFG_PCFG_TSR_10M;
841                 break;
842         default:
843                 printk("Unsupported speed %d\n", mac->phydev->speed);
844         }
845
846         /* Print on link or speed/duplex change */
847         msg = mac->link != mac->phydev->link || flags != new_flags;
848
849         mac->duplex = mac->phydev->duplex;
850         mac->speed = mac->phydev->speed;
851         mac->link = mac->phydev->link;
852
853         if (new_flags != flags)
854                 write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
855
856         if (msg && netif_msg_link(mac))
857                 printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
858                        dev->name, mac->speed, mac->duplex ? "full" : "half");
859 }
860
861 static int pasemi_mac_phy_init(struct net_device *dev)
862 {
863         struct pasemi_mac *mac = netdev_priv(dev);
864         struct device_node *dn, *phy_dn;
865         struct phy_device *phydev;
866         unsigned int phy_id;
867         const phandle *ph;
868         const unsigned int *prop;
869         struct resource r;
870         int ret;
871
872         dn = pci_device_to_OF_node(mac->pdev);
873         ph = of_get_property(dn, "phy-handle", NULL);
874         if (!ph)
875                 return -ENODEV;
876         phy_dn = of_find_node_by_phandle(*ph);
877
878         prop = of_get_property(phy_dn, "reg", NULL);
879         ret = of_address_to_resource(phy_dn->parent, 0, &r);
880         if (ret)
881                 goto err;
882
883         phy_id = *prop;
884         snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);
885
886         of_node_put(phy_dn);
887
888         mac->link = 0;
889         mac->speed = 0;
890         mac->duplex = -1;
891
892         phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);
893
894         if (IS_ERR(phydev)) {
895                 printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
896                 return PTR_ERR(phydev);
897         }
898
899         mac->phydev = phydev;
900
901         return 0;
902
903 err:
904         of_node_put(phy_dn);
905         return -ENODEV;
906 }
907
908
909 static int pasemi_mac_open(struct net_device *dev)
910 {
911         struct pasemi_mac *mac = netdev_priv(dev);
912         int base_irq;
913         unsigned int flags;
914         int ret;
915
916         /* enable rx section */
917         write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
918
919         /* enable tx section */
920         write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
921
922         flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
923                 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
924                 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
925
926         write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
927
928         write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
929                            PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
930
931         write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
932                            PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
933
934         /* 0xffffff is max value, about 16ms */
935         write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
936                            PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
937
938         ret = pasemi_mac_setup_rx_resources(dev);
939         if (ret)
940                 goto out_rx_resources;
941
942         mac->tx = pasemi_mac_setup_tx_resources(dev, mac->dma_txch);
943
944         if (!mac->tx)
945                 goto out_tx_ring;
946
947         write_mac_reg(mac, PAS_MAC_IPC_CHNL,
948                            PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
949                            PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
950
951         /* enable rx if */
952         write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
953                            PAS_DMA_RXINT_RCMDSTA_EN |
954                            PAS_DMA_RXINT_RCMDSTA_DROPS_M |
955                            PAS_DMA_RXINT_RCMDSTA_BP |
956                            PAS_DMA_RXINT_RCMDSTA_OO |
957                            PAS_DMA_RXINT_RCMDSTA_BT);
958
959         /* enable rx channel */
960         write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
961                            PAS_DMA_RXCHAN_CCMDSTA_EN |
962                            PAS_DMA_RXCHAN_CCMDSTA_DU |
963                            PAS_DMA_RXCHAN_CCMDSTA_OD |
964                            PAS_DMA_RXCHAN_CCMDSTA_FD |
965                            PAS_DMA_RXCHAN_CCMDSTA_DT);
966
967         /* enable tx channel */
968         write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
969                            PAS_DMA_TXCHAN_TCMDSTA_EN |
970                            PAS_DMA_TXCHAN_TCMDSTA_SZ |
971                            PAS_DMA_TXCHAN_TCMDSTA_DB |
972                            PAS_DMA_TXCHAN_TCMDSTA_DE |
973                            PAS_DMA_TXCHAN_TCMDSTA_DA);
974
975         pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
976
977         write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), RX_RING_SIZE>>1);
978
979         /* Clear out any residual packet count state from firmware */
980         pasemi_mac_restart_rx_intr(mac);
981         pasemi_mac_restart_tx_intr(mac);
982
983         flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
984                 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
985
986         if (mac->type == MAC_TYPE_GMAC)
987                 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
988         else
989                 flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;
990
991         /* Enable interface in MAC */
992         write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
993
994         ret = pasemi_mac_phy_init(dev);
995         /* Some configs don't have PHYs (XAUI etc), so don't complain about
996          * failed init due to -ENODEV.
997          */
998         if (ret && ret != -ENODEV)
999                 dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);
1000
1001         netif_start_queue(dev);
1002         napi_enable(&mac->napi);
1003
1004         /* Interrupts are a bit different for our DMA controller: While
1005          * it's got one a regular PCI device header, the interrupt there
1006          * is really the base of the range it's using. Each tx and rx
1007          * channel has it's own interrupt source.
1008          */
1009
1010         base_irq = virq_to_hw(mac->dma_pdev->irq);
1011
1012         mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
1013
1014         snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
1015                  dev->name);
1016
1017         ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
1018                           mac->tx_irq_name, mac->tx);
1019         if (ret) {
1020                 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
1021                         base_irq + mac->dma_txch, ret);
1022                 goto out_tx_int;
1023         }
1024
1025         mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_rxch);
1026
1027         snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
1028                  dev->name);
1029
1030         ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
1031                           mac->rx_irq_name, dev);
1032         if (ret) {
1033                 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
1034                         base_irq + 20 + mac->dma_rxch, ret);
1035                 goto out_rx_int;
1036         }
1037
1038         if (mac->phydev)
1039                 phy_start(mac->phydev);
1040
1041         return 0;
1042
1043 out_rx_int:
1044         free_irq(mac->tx_irq, mac->tx);
1045 out_tx_int:
1046         napi_disable(&mac->napi);
1047         netif_stop_queue(dev);
1048 out_tx_ring:
1049         if (mac->tx)
1050                 pasemi_mac_free_tx_resources(mac);
1051         pasemi_mac_free_rx_resources(mac);
1052 out_rx_resources:
1053
1054         return ret;
1055 }
1056
1057 #define MAX_RETRIES 5000
1058
1059 static int pasemi_mac_close(struct net_device *dev)
1060 {
1061         struct pasemi_mac *mac = netdev_priv(dev);
1062         unsigned int sta;
1063         int retries;
1064
1065         if (mac->phydev) {
1066                 phy_stop(mac->phydev);
1067                 phy_disconnect(mac->phydev);
1068         }
1069
1070         netif_stop_queue(dev);
1071         napi_disable(&mac->napi);
1072
1073         sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1074         if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
1075                       PAS_DMA_RXINT_RCMDSTA_OO |
1076                       PAS_DMA_RXINT_RCMDSTA_BT))
1077                 printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);
1078
1079         sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
1080         if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
1081                      PAS_DMA_RXCHAN_CCMDSTA_OD |
1082                      PAS_DMA_RXCHAN_CCMDSTA_FD |
1083                      PAS_DMA_RXCHAN_CCMDSTA_DT))
1084                 printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);
1085
1086         sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
1087         if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
1088                       PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
1089                 printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);
1090
1091         /* Clean out any pending buffers */
1092         pasemi_mac_clean_tx(tx_ring(mac));
1093         pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
1094
1095         /* Disable interface */
1096         write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
1097                       PAS_DMA_TXCHAN_TCMDSTA_ST);
1098         write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1099                       PAS_DMA_RXINT_RCMDSTA_ST);
1100         write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
1101                       PAS_DMA_RXCHAN_CCMDSTA_ST);
1102
1103         for (retries = 0; retries < MAX_RETRIES; retries++) {
1104                 sta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
1105                 if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
1106                         break;
1107                 cond_resched();
1108         }
1109
1110         if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
1111                 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel %d\n",
1112                         mac->dma_txch);
1113
1114         for (retries = 0; retries < MAX_RETRIES; retries++) {
1115                 sta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
1116                 if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
1117                         break;
1118                 cond_resched();
1119         }
1120
1121         if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
1122                 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
1123
1124         for (retries = 0; retries < MAX_RETRIES; retries++) {
1125                 sta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1126                 if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
1127                         break;
1128                 cond_resched();
1129         }
1130
1131         if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
1132                 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
1133
1134         /* Then, disable the channel. This must be done separately from
1135          * stopping, since you can't disable when active.
1136          */
1137
1138         write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
1139         write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
1140         write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
1141
1142         free_irq(mac->tx_irq, mac->tx);
1143         free_irq(mac->rx_irq, mac->rx);
1144
1145         /* Free resources */
1146         pasemi_mac_free_rx_resources(mac);
1147         pasemi_mac_free_tx_resources(mac);
1148
1149         return 0;
1150 }
1151
1152 static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
1153 {
1154         struct pasemi_mac *mac = netdev_priv(dev);
1155         struct pasemi_mac_txring *txring;
1156         u64 dflags, mactx;
1157         dma_addr_t map[MAX_SKB_FRAGS+1];
1158         unsigned int map_size[MAX_SKB_FRAGS+1];
1159         unsigned long flags;
1160         int i, nfrags;
1161
1162         dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
1163
1164         if (skb->ip_summed == CHECKSUM_PARTIAL) {
1165                 const unsigned char *nh = skb_network_header(skb);
1166
1167                 switch (ip_hdr(skb)->protocol) {
1168                 case IPPROTO_TCP:
1169                         dflags |= XCT_MACTX_CSUM_TCP;
1170                         dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
1171                         dflags |= XCT_MACTX_IPO(nh - skb->data);
1172                         break;
1173                 case IPPROTO_UDP:
1174                         dflags |= XCT_MACTX_CSUM_UDP;
1175                         dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
1176                         dflags |= XCT_MACTX_IPO(nh - skb->data);
1177                         break;
1178                 }
1179         }
1180
1181         nfrags = skb_shinfo(skb)->nr_frags;
1182
1183         map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
1184                                 PCI_DMA_TODEVICE);
1185         map_size[0] = skb_headlen(skb);
1186         if (dma_mapping_error(map[0]))
1187                 goto out_err_nolock;
1188
1189         for (i = 0; i < nfrags; i++) {
1190                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1191
1192                 map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
1193                                         frag->page_offset, frag->size,
1194                                         PCI_DMA_TODEVICE);
1195                 map_size[i+1] = frag->size;
1196                 if (dma_mapping_error(map[i+1])) {
1197                         nfrags = i;
1198                         goto out_err_nolock;
1199                 }
1200         }
1201
1202         mactx = dflags | XCT_MACTX_LLEN(skb->len);
1203
1204         txring = tx_ring(mac);
1205
1206         spin_lock_irqsave(&txring->lock, flags);
1207
1208         /* Avoid stepping on the same cache line that the DMA controller
1209          * is currently about to send, so leave at least 8 words available.
1210          * Total free space needed is mactx + fragments + 8
1211          */
1212         if (RING_AVAIL(txring) < nfrags + 10) {
1213                 /* no room -- stop the queue and wait for tx intr */
1214                 netif_stop_queue(dev);
1215                 goto out_err;
1216         }
1217
1218         TX_DESC(txring, txring->next_to_fill) = mactx;
1219         txring->next_to_fill++;
1220         TX_DESC_INFO(txring, txring->next_to_fill).skb = skb;
1221         for (i = 0; i <= nfrags; i++) {
1222                 TX_DESC(txring, txring->next_to_fill+i) =
1223                         XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
1224                 TX_DESC_INFO(txring, txring->next_to_fill+i).dma = map[i];
1225         }
1226
1227         /* We have to add an even number of 8-byte entries to the ring
1228          * even if the last one is unused. That means always an odd number
1229          * of pointers + one mactx descriptor.
1230          */
1231         if (nfrags & 1)
1232                 nfrags++;
1233
1234         txring->next_to_fill = (txring->next_to_fill + nfrags + 1) &
1235                                 (TX_RING_SIZE-1);
1236
1237         dev->stats.tx_packets++;
1238         dev->stats.tx_bytes += skb->len;
1239
1240         spin_unlock_irqrestore(&txring->lock, flags);
1241
1242         write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(txring->chan), (nfrags+2) >> 1);
1243
1244         return NETDEV_TX_OK;
1245
1246 out_err:
1247         spin_unlock_irqrestore(&txring->lock, flags);
1248 out_err_nolock:
1249         while (nfrags--)
1250                 pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
1251                                  PCI_DMA_TODEVICE);
1252
1253         return NETDEV_TX_BUSY;
1254 }
1255
1256 static void pasemi_mac_set_rx_mode(struct net_device *dev)
1257 {
1258         struct pasemi_mac *mac = netdev_priv(dev);
1259         unsigned int flags;
1260
1261         flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
1262
1263         /* Set promiscuous */
1264         if (dev->flags & IFF_PROMISC)
1265                 flags |= PAS_MAC_CFG_PCFG_PR;
1266         else
1267                 flags &= ~PAS_MAC_CFG_PCFG_PR;
1268
1269         write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1270 }
1271
1272
1273 static int pasemi_mac_poll(struct napi_struct *napi, int budget)
1274 {
1275         struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
1276         struct net_device *dev = mac->netdev;
1277         int pkts;
1278
1279         pasemi_mac_clean_tx(tx_ring(mac));
1280         pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
1281         if (pkts < budget) {
1282                 /* all done, no more packets present */
1283                 netif_rx_complete(dev, napi);
1284
1285                 pasemi_mac_restart_rx_intr(mac);
1286         }
1287         return pkts;
1288 }
1289
1290 static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
1291 {
1292         struct device_node *dn;
1293         void __iomem *ret;
1294
1295         dn = pci_device_to_OF_node(p);
1296         if (!dn)
1297                 goto fallback;
1298
1299         ret = of_iomap(dn, index);
1300         if (!ret)
1301                 goto fallback;
1302
1303         return ret;
1304 fallback:
1305         /* This is hardcoded and ugly, but we have some firmware versions
1306          * that don't provide the register space in the device tree. Luckily
1307          * they are at well-known locations so we can just do the math here.
1308          */
1309         return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
1310 }
1311
1312 static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
1313 {
1314         struct resource res;
1315         struct device_node *dn;
1316         int err;
1317
1318         mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
1319         if (!mac->dma_pdev) {
1320                 dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
1321                 return -ENODEV;
1322         }
1323
1324         mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
1325         if (!mac->iob_pdev) {
1326                 dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
1327                 return -ENODEV;
1328         }
1329
1330         mac->regs = map_onedev(mac->pdev, 0);
1331         mac->dma_regs = map_onedev(mac->dma_pdev, 0);
1332         mac->iob_regs = map_onedev(mac->iob_pdev, 0);
1333
1334         if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
1335                 dev_err(&mac->pdev->dev, "Can't map registers\n");
1336                 return -ENODEV;
1337         }
1338
1339         /* The dma status structure is located in the I/O bridge, and
1340          * is cache coherent.
1341          */
1342         if (!dma_status) {
1343                 dn = pci_device_to_OF_node(mac->iob_pdev);
1344                 if (dn)
1345                         err = of_address_to_resource(dn, 1, &res);
1346                 if (!dn || err) {
1347                         /* Fallback for old firmware */
1348                         res.start = 0xfd800000;
1349                         res.end = res.start + 0x1000;
1350                 }
1351                 dma_status = __ioremap(res.start, res.end-res.start, 0);
1352         }
1353
1354         return 0;
1355 }
1356
1357 static int __devinit
1358 pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1359 {
1360         static int index = 0;
1361         struct net_device *dev;
1362         struct pasemi_mac *mac;
1363         int err;
1364         DECLARE_MAC_BUF(mac_buf);
1365
1366         err = pci_enable_device(pdev);
1367         if (err)
1368                 return err;
1369
1370         dev = alloc_etherdev(sizeof(struct pasemi_mac));
1371         if (dev == NULL) {
1372                 dev_err(&pdev->dev,
1373                         "pasemi_mac: Could not allocate ethernet device.\n");
1374                 err = -ENOMEM;
1375                 goto out_disable_device;
1376         }
1377
1378         pci_set_drvdata(pdev, dev);
1379         SET_NETDEV_DEV(dev, &pdev->dev);
1380
1381         mac = netdev_priv(dev);
1382
1383         mac->pdev = pdev;
1384         mac->netdev = dev;
1385
1386         netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);
1387
1388         dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG;
1389
1390         /* These should come out of the device tree eventually */
1391         mac->dma_txch = index;
1392         mac->dma_rxch = index;
1393
1394         /* We probe GMAC before XAUI, but the DMA interfaces are
1395          * in XAUI, GMAC order.
1396          */
1397         if (index < 4)
1398                 mac->dma_if = index + 2;
1399         else
1400                 mac->dma_if = index - 4;
1401         index++;
1402
1403         switch (pdev->device) {
1404         case 0xa005:
1405                 mac->type = MAC_TYPE_GMAC;
1406                 break;
1407         case 0xa006:
1408                 mac->type = MAC_TYPE_XAUI;
1409                 break;
1410         default:
1411                 err = -ENODEV;
1412                 goto out;
1413         }
1414
1415         /* get mac addr from device tree */
1416         if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
1417                 err = -ENODEV;
1418                 goto out;
1419         }
1420         memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
1421
1422         dev->open = pasemi_mac_open;
1423         dev->stop = pasemi_mac_close;
1424         dev->hard_start_xmit = pasemi_mac_start_tx;
1425         dev->set_multicast_list = pasemi_mac_set_rx_mode;
1426
1427         err = pasemi_mac_map_regs(mac);
1428         if (err)
1429                 goto out;
1430
1431         mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
1432
1433         /* Enable most messages by default */
1434         mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
1435
1436         err = register_netdev(dev);
1437
1438         if (err) {
1439                 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
1440                         err);
1441                 goto out;
1442         } else if netif_msg_probe(mac)
1443                 printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
1444                        dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1445                        mac->dma_if, print_mac(mac_buf, dev->dev_addr));
1446
1447         return err;
1448
1449 out:
1450         if (mac->iob_pdev)
1451                 pci_dev_put(mac->iob_pdev);
1452         if (mac->dma_pdev)
1453                 pci_dev_put(mac->dma_pdev);
1454         if (mac->dma_regs)
1455                 iounmap(mac->dma_regs);
1456         if (mac->iob_regs)
1457                 iounmap(mac->iob_regs);
1458         if (mac->regs)
1459                 iounmap(mac->regs);
1460
1461         free_netdev(dev);
1462 out_disable_device:
1463         pci_disable_device(pdev);
1464         return err;
1465
1466 }
1467
1468 static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
1469 {
1470         struct net_device *netdev = pci_get_drvdata(pdev);
1471         struct pasemi_mac *mac;
1472
1473         if (!netdev)
1474                 return;
1475
1476         mac = netdev_priv(netdev);
1477
1478         unregister_netdev(netdev);
1479
1480         pci_disable_device(pdev);
1481         pci_dev_put(mac->dma_pdev);
1482         pci_dev_put(mac->iob_pdev);
1483
1484         iounmap(mac->regs);
1485         iounmap(mac->dma_regs);
1486         iounmap(mac->iob_regs);
1487
1488         pci_set_drvdata(pdev, NULL);
1489         free_netdev(netdev);
1490 }
1491
1492 static struct pci_device_id pasemi_mac_pci_tbl[] = {
1493         { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
1494         { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
1495         { },
1496 };
1497
1498 MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
1499
1500 static struct pci_driver pasemi_mac_driver = {
1501         .name           = "pasemi_mac",
1502         .id_table       = pasemi_mac_pci_tbl,
1503         .probe          = pasemi_mac_probe,
1504         .remove         = __devexit_p(pasemi_mac_remove),
1505 };
1506
1507 static void __exit pasemi_mac_cleanup_module(void)
1508 {
1509         pci_unregister_driver(&pasemi_mac_driver);
1510         __iounmap(dma_status);
1511         dma_status = NULL;
1512 }
1513
1514 int pasemi_mac_init_module(void)
1515 {
1516         return pci_register_driver(&pasemi_mac_driver);
1517 }
1518
1519 module_init(pasemi_mac_init_module);
1520 module_exit(pasemi_mac_cleanup_module);