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