]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/faraday/ftgmac100.c
ftgmac100: Cleanup rx checksum handling
[karo-tx-linux.git] / drivers / net / ethernet / faraday / ftgmac100.c
1 /*
2  * Faraday FTGMAC100 Gigabit Ethernet
3  *
4  * (C) Copyright 2009-2011 Faraday Technology
5  * Po-Yu Chuang <ratbert@faraday-tech.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
23
24 #include <linux/dma-mapping.h>
25 #include <linux/etherdevice.h>
26 #include <linux/ethtool.h>
27 #include <linux/interrupt.h>
28 #include <linux/io.h>
29 #include <linux/module.h>
30 #include <linux/netdevice.h>
31 #include <linux/of.h>
32 #include <linux/phy.h>
33 #include <linux/platform_device.h>
34 #include <linux/property.h>
35 #include <net/ip.h>
36 #include <net/ncsi.h>
37
38 #include "ftgmac100.h"
39
40 #define DRV_NAME        "ftgmac100"
41 #define DRV_VERSION     "0.7"
42
43 #define RX_QUEUE_ENTRIES        256     /* must be power of 2 */
44 #define TX_QUEUE_ENTRIES        512     /* must be power of 2 */
45
46 #define MAX_PKT_SIZE            1518
47 #define RX_BUF_SIZE             PAGE_SIZE       /* must be smaller than 0x3fff */
48
49 struct ftgmac100_descs {
50         struct ftgmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
51         struct ftgmac100_txdes txdes[TX_QUEUE_ENTRIES];
52 };
53
54 struct ftgmac100 {
55         /* Registers */
56         struct resource *res;
57         void __iomem *base;
58
59         struct ftgmac100_descs *descs;
60         dma_addr_t descs_dma_addr;
61
62         /* Rx ring */
63         struct page *rx_pages[RX_QUEUE_ENTRIES];
64         unsigned int rx_pointer;
65         u32 rxdes0_edorr_mask;
66
67         /* Tx ring */
68         unsigned int tx_clean_pointer;
69         unsigned int tx_pointer;
70         unsigned int tx_pending;
71         u32 txdes0_edotr_mask;
72         spinlock_t tx_lock;
73
74         /* Scratch page to use when rx skb alloc fails */
75         void *rx_scratch;
76         dma_addr_t rx_scratch_dma;
77
78         /* Component structures */
79         struct net_device *netdev;
80         struct device *dev;
81         struct ncsi_dev *ndev;
82         struct napi_struct napi;
83         struct work_struct reset_task;
84         struct mii_bus *mii_bus;
85
86         /* Link management */
87         int cur_speed;
88         int cur_duplex;
89         bool use_ncsi;
90
91         /* Misc */
92         bool need_mac_restart;
93 };
94
95 static void ftgmac100_set_rx_ring_base(struct ftgmac100 *priv, dma_addr_t addr)
96 {
97         iowrite32(addr, priv->base + FTGMAC100_OFFSET_RXR_BADR);
98 }
99
100 static void ftgmac100_set_rx_buffer_size(struct ftgmac100 *priv,
101                 unsigned int size)
102 {
103         size = FTGMAC100_RBSR_SIZE(size);
104         iowrite32(size, priv->base + FTGMAC100_OFFSET_RBSR);
105 }
106
107 static void ftgmac100_set_normal_prio_tx_ring_base(struct ftgmac100 *priv,
108                                                    dma_addr_t addr)
109 {
110         iowrite32(addr, priv->base + FTGMAC100_OFFSET_NPTXR_BADR);
111 }
112
113 static void ftgmac100_txdma_normal_prio_start_polling(struct ftgmac100 *priv)
114 {
115         iowrite32(1, priv->base + FTGMAC100_OFFSET_NPTXPD);
116 }
117
118 static int ftgmac100_reset_mac(struct ftgmac100 *priv, u32 maccr)
119 {
120         struct net_device *netdev = priv->netdev;
121         int i;
122
123         /* NOTE: reset clears all registers */
124         iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
125         iowrite32(maccr | FTGMAC100_MACCR_SW_RST,
126                   priv->base + FTGMAC100_OFFSET_MACCR);
127         for (i = 0; i < 50; i++) {
128                 unsigned int maccr;
129
130                 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
131                 if (!(maccr & FTGMAC100_MACCR_SW_RST))
132                         return 0;
133
134                 udelay(1);
135         }
136
137         netdev_err(netdev, "Hardware reset failed\n");
138         return -EIO;
139 }
140
141 static int ftgmac100_reset_and_config_mac(struct ftgmac100 *priv)
142 {
143         u32 maccr = 0;
144
145         switch (priv->cur_speed) {
146         case SPEED_10:
147         case 0: /* no link */
148                 break;
149
150         case SPEED_100:
151                 maccr |= FTGMAC100_MACCR_FAST_MODE;
152                 break;
153
154         case SPEED_1000:
155                 maccr |= FTGMAC100_MACCR_GIGA_MODE;
156                 break;
157         default:
158                 netdev_err(priv->netdev, "Unknown speed %d !\n",
159                            priv->cur_speed);
160                 break;
161         }
162
163         /* (Re)initialize the queue pointers */
164         priv->rx_pointer = 0;
165         priv->tx_clean_pointer = 0;
166         priv->tx_pointer = 0;
167         priv->tx_pending = 0;
168
169         /* The doc says reset twice with 10us interval */
170         if (ftgmac100_reset_mac(priv, maccr))
171                 return -EIO;
172         usleep_range(10, 1000);
173         return ftgmac100_reset_mac(priv, maccr);
174 }
175
176 static void ftgmac100_set_mac(struct ftgmac100 *priv, const unsigned char *mac)
177 {
178         unsigned int maddr = mac[0] << 8 | mac[1];
179         unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
180
181         iowrite32(maddr, priv->base + FTGMAC100_OFFSET_MAC_MADR);
182         iowrite32(laddr, priv->base + FTGMAC100_OFFSET_MAC_LADR);
183 }
184
185 static void ftgmac100_setup_mac(struct ftgmac100 *priv)
186 {
187         u8 mac[ETH_ALEN];
188         unsigned int m;
189         unsigned int l;
190         void *addr;
191
192         addr = device_get_mac_address(priv->dev, mac, ETH_ALEN);
193         if (addr) {
194                 ether_addr_copy(priv->netdev->dev_addr, mac);
195                 dev_info(priv->dev, "Read MAC address %pM from device tree\n",
196                          mac);
197                 return;
198         }
199
200         m = ioread32(priv->base + FTGMAC100_OFFSET_MAC_MADR);
201         l = ioread32(priv->base + FTGMAC100_OFFSET_MAC_LADR);
202
203         mac[0] = (m >> 8) & 0xff;
204         mac[1] = m & 0xff;
205         mac[2] = (l >> 24) & 0xff;
206         mac[3] = (l >> 16) & 0xff;
207         mac[4] = (l >> 8) & 0xff;
208         mac[5] = l & 0xff;
209
210         if (is_valid_ether_addr(mac)) {
211                 ether_addr_copy(priv->netdev->dev_addr, mac);
212                 dev_info(priv->dev, "Read MAC address %pM from chip\n", mac);
213         } else {
214                 eth_hw_addr_random(priv->netdev);
215                 dev_info(priv->dev, "Generated random MAC address %pM\n",
216                          priv->netdev->dev_addr);
217         }
218 }
219
220 static int ftgmac100_set_mac_addr(struct net_device *dev, void *p)
221 {
222         int ret;
223
224         ret = eth_prepare_mac_addr_change(dev, p);
225         if (ret < 0)
226                 return ret;
227
228         eth_commit_mac_addr_change(dev, p);
229         ftgmac100_set_mac(netdev_priv(dev), dev->dev_addr);
230
231         return 0;
232 }
233
234 static void ftgmac100_init_hw(struct ftgmac100 *priv)
235 {
236         /* setup ring buffer base registers */
237         ftgmac100_set_rx_ring_base(priv,
238                                    priv->descs_dma_addr +
239                                    offsetof(struct ftgmac100_descs, rxdes));
240         ftgmac100_set_normal_prio_tx_ring_base(priv,
241                                                priv->descs_dma_addr +
242                                                offsetof(struct ftgmac100_descs, txdes));
243
244         ftgmac100_set_rx_buffer_size(priv, RX_BUF_SIZE);
245
246         iowrite32(FTGMAC100_APTC_RXPOLL_CNT(1), priv->base + FTGMAC100_OFFSET_APTC);
247
248         ftgmac100_set_mac(priv, priv->netdev->dev_addr);
249 }
250
251 static void ftgmac100_start_hw(struct ftgmac100 *priv)
252 {
253         u32 maccr = ioread32(priv->base + FTGMAC100_OFFSET_MACCR);
254
255         /* Keep the original GMAC and FAST bits */
256         maccr &= (FTGMAC100_MACCR_FAST_MODE | FTGMAC100_MACCR_GIGA_MODE);
257
258         /* Add all the main enable bits */
259         maccr |= FTGMAC100_MACCR_TXDMA_EN       |
260                  FTGMAC100_MACCR_RXDMA_EN       |
261                  FTGMAC100_MACCR_TXMAC_EN       |
262                  FTGMAC100_MACCR_RXMAC_EN       |
263                  FTGMAC100_MACCR_CRC_APD        |
264                  FTGMAC100_MACCR_PHY_LINK_LEVEL |
265                  FTGMAC100_MACCR_RX_RUNT        |
266                  FTGMAC100_MACCR_RX_BROADPKT;
267
268         /* Add other bits as needed */
269         if (priv->cur_duplex == DUPLEX_FULL)
270                 maccr |= FTGMAC100_MACCR_FULLDUP;
271
272         /* Hit the HW */
273         iowrite32(maccr, priv->base + FTGMAC100_OFFSET_MACCR);
274 }
275
276 static void ftgmac100_stop_hw(struct ftgmac100 *priv)
277 {
278         iowrite32(0, priv->base + FTGMAC100_OFFSET_MACCR);
279 }
280
281 static bool ftgmac100_rxdes_first_segment(struct ftgmac100_rxdes *rxdes)
282 {
283         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_FRS);
284 }
285
286 static bool ftgmac100_rxdes_last_segment(struct ftgmac100_rxdes *rxdes)
287 {
288         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_LRS);
289 }
290
291 static bool ftgmac100_rxdes_packet_ready(struct ftgmac100_rxdes *rxdes)
292 {
293         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RXPKT_RDY);
294 }
295
296 static void ftgmac100_rxdes_set_dma_own(const struct ftgmac100 *priv,
297                                         struct ftgmac100_rxdes *rxdes)
298 {
299         /* clear status bits */
300         rxdes->rxdes0 &= cpu_to_le32(priv->rxdes0_edorr_mask);
301 }
302
303 static bool ftgmac100_rxdes_rx_error(struct ftgmac100_rxdes *rxdes)
304 {
305         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RX_ERR);
306 }
307
308 static bool ftgmac100_rxdes_crc_error(struct ftgmac100_rxdes *rxdes)
309 {
310         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_CRC_ERR);
311 }
312
313 static bool ftgmac100_rxdes_frame_too_long(struct ftgmac100_rxdes *rxdes)
314 {
315         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_FTL);
316 }
317
318 static bool ftgmac100_rxdes_runt(struct ftgmac100_rxdes *rxdes)
319 {
320         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RUNT);
321 }
322
323 static bool ftgmac100_rxdes_odd_nibble(struct ftgmac100_rxdes *rxdes)
324 {
325         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_RX_ODD_NB);
326 }
327
328 static unsigned int ftgmac100_rxdes_data_length(struct ftgmac100_rxdes *rxdes)
329 {
330         return le32_to_cpu(rxdes->rxdes0) & FTGMAC100_RXDES0_VDBC;
331 }
332
333 static bool ftgmac100_rxdes_multicast(struct ftgmac100_rxdes *rxdes)
334 {
335         return rxdes->rxdes0 & cpu_to_le32(FTGMAC100_RXDES0_MULTICAST);
336 }
337
338 static void ftgmac100_rxdes_set_end_of_ring(const struct ftgmac100 *priv,
339                                             struct ftgmac100_rxdes *rxdes)
340 {
341         rxdes->rxdes0 |= cpu_to_le32(priv->rxdes0_edorr_mask);
342 }
343
344 static void ftgmac100_rxdes_set_dma_addr(struct ftgmac100_rxdes *rxdes,
345                                          dma_addr_t addr)
346 {
347         rxdes->rxdes3 = cpu_to_le32(addr);
348 }
349
350 static dma_addr_t ftgmac100_rxdes_get_dma_addr(struct ftgmac100_rxdes *rxdes)
351 {
352         return le32_to_cpu(rxdes->rxdes3);
353 }
354
355 static inline bool ftgmac100_rxdes_csum_err(struct ftgmac100_rxdes *rxdes)
356 {
357         return !!(rxdes->rxdes1 & cpu_to_le32(FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
358                                               FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
359                                               FTGMAC100_RXDES1_IP_CHKSUM_ERR));
360 }
361
362 static inline struct page **ftgmac100_rxdes_page_slot(struct ftgmac100 *priv,
363                                                       struct ftgmac100_rxdes *rxdes)
364 {
365         return &priv->rx_pages[rxdes - priv->descs->rxdes];
366 }
367
368 /*
369  * rxdes2 is not used by hardware. We use it to keep track of page.
370  * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
371  */
372 static void ftgmac100_rxdes_set_page(struct ftgmac100 *priv,
373                                      struct ftgmac100_rxdes *rxdes,
374                                      struct page *page)
375 {
376         *ftgmac100_rxdes_page_slot(priv, rxdes) = page;
377 }
378
379 static struct page *ftgmac100_rxdes_get_page(struct ftgmac100 *priv,
380                                              struct ftgmac100_rxdes *rxdes)
381 {
382         return *ftgmac100_rxdes_page_slot(priv, rxdes);
383 }
384
385 static int ftgmac100_alloc_rx_page(struct ftgmac100 *priv,
386                                    struct ftgmac100_rxdes *rxdes, gfp_t gfp)
387 {
388         struct net_device *netdev = priv->netdev;
389         struct page *page;
390         dma_addr_t map;
391         int err;
392
393         page = alloc_page(gfp);
394         if (!page) {
395                 if (net_ratelimit())
396                         netdev_err(netdev, "failed to allocate rx page\n");
397                 err = -ENOMEM;
398                 map = priv->rx_scratch_dma;
399         }
400
401         map = dma_map_page(priv->dev, page, 0, RX_BUF_SIZE, DMA_FROM_DEVICE);
402         if (unlikely(dma_mapping_error(priv->dev, map))) {
403                 if (net_ratelimit())
404                         netdev_err(netdev, "failed to map rx page\n");
405                 __free_page(page);
406                 err = -ENOMEM;
407                 map = priv->rx_scratch_dma;
408                 page = NULL;
409         }
410
411         ftgmac100_rxdes_set_page(priv, rxdes, page);
412         ftgmac100_rxdes_set_dma_addr(rxdes, map);
413         ftgmac100_rxdes_set_dma_own(priv, rxdes);
414         return 0;
415 }
416
417 static int ftgmac100_next_rx_pointer(int pointer)
418 {
419         return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
420 }
421
422 static void ftgmac100_rx_pointer_advance(struct ftgmac100 *priv)
423 {
424         priv->rx_pointer = ftgmac100_next_rx_pointer(priv->rx_pointer);
425 }
426
427 static struct ftgmac100_rxdes *ftgmac100_current_rxdes(struct ftgmac100 *priv)
428 {
429         return &priv->descs->rxdes[priv->rx_pointer];
430 }
431
432 static struct ftgmac100_rxdes *
433 ftgmac100_rx_locate_first_segment(struct ftgmac100 *priv)
434 {
435         struct ftgmac100_rxdes *rxdes = ftgmac100_current_rxdes(priv);
436
437         while (ftgmac100_rxdes_packet_ready(rxdes)) {
438                 if (ftgmac100_rxdes_first_segment(rxdes))
439                         return rxdes;
440
441                 ftgmac100_rxdes_set_dma_own(priv, rxdes);
442                 ftgmac100_rx_pointer_advance(priv);
443                 rxdes = ftgmac100_current_rxdes(priv);
444         }
445
446         return NULL;
447 }
448
449 static bool ftgmac100_rx_packet_error(struct ftgmac100 *priv,
450                                       struct ftgmac100_rxdes *rxdes)
451 {
452         struct net_device *netdev = priv->netdev;
453         bool error = false;
454
455         if (unlikely(ftgmac100_rxdes_rx_error(rxdes))) {
456                 if (net_ratelimit())
457                         netdev_info(netdev, "rx err\n");
458
459                 netdev->stats.rx_errors++;
460                 error = true;
461         }
462
463         if (unlikely(ftgmac100_rxdes_crc_error(rxdes))) {
464                 if (net_ratelimit())
465                         netdev_info(netdev, "rx crc err\n");
466
467                 netdev->stats.rx_crc_errors++;
468                 error = true;
469         }
470
471         if (unlikely(ftgmac100_rxdes_frame_too_long(rxdes))) {
472                 if (net_ratelimit())
473                         netdev_info(netdev, "rx frame too long\n");
474
475                 netdev->stats.rx_length_errors++;
476                 error = true;
477         } else if (unlikely(ftgmac100_rxdes_runt(rxdes))) {
478                 if (net_ratelimit())
479                         netdev_info(netdev, "rx runt\n");
480
481                 netdev->stats.rx_length_errors++;
482                 error = true;
483         } else if (unlikely(ftgmac100_rxdes_odd_nibble(rxdes))) {
484                 if (net_ratelimit())
485                         netdev_info(netdev, "rx odd nibble\n");
486
487                 netdev->stats.rx_length_errors++;
488                 error = true;
489         }
490
491         return error;
492 }
493
494 static void ftgmac100_rx_drop_packet(struct ftgmac100 *priv)
495 {
496         struct net_device *netdev = priv->netdev;
497         struct ftgmac100_rxdes *rxdes = ftgmac100_current_rxdes(priv);
498         bool done = false;
499
500         if (net_ratelimit())
501                 netdev_dbg(netdev, "drop packet %p\n", rxdes);
502
503         do {
504                 if (ftgmac100_rxdes_last_segment(rxdes))
505                         done = true;
506
507                 ftgmac100_rxdes_set_dma_own(priv, rxdes);
508                 ftgmac100_rx_pointer_advance(priv);
509                 rxdes = ftgmac100_current_rxdes(priv);
510         } while (!done && ftgmac100_rxdes_packet_ready(rxdes));
511
512         netdev->stats.rx_dropped++;
513 }
514
515 static bool ftgmac100_rx_packet(struct ftgmac100 *priv, int *processed)
516 {
517         struct net_device *netdev = priv->netdev;
518         struct ftgmac100_rxdes *rxdes;
519         struct sk_buff *skb;
520         struct page *page;
521         unsigned int size;
522         dma_addr_t map;
523
524         rxdes = ftgmac100_rx_locate_first_segment(priv);
525         if (!rxdes)
526                 return false;
527
528         /* We don't support segmented rx frames, so drop these
529          * along with packets with errors.
530          */
531         if (unlikely(!ftgmac100_rxdes_last_segment(rxdes) ||
532                      ftgmac100_rx_packet_error(priv, rxdes))) {
533                 ftgmac100_rx_drop_packet(priv);
534                 return true;
535         }
536
537         /* If the packet had no buffer (failed to allocate earlier)
538          * then try to allocate one and skip
539          */
540         page = ftgmac100_rxdes_get_page(priv, rxdes);
541         if (!page) {
542                 ftgmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
543                 ftgmac100_rx_pointer_advance(priv);
544                 return true;
545         }
546
547         /* start processing */
548         skb = netdev_alloc_skb_ip_align(netdev, 128);
549         if (unlikely(!skb)) {
550                 if (net_ratelimit())
551                         netdev_err(netdev, "rx skb alloc failed\n");
552
553                 ftgmac100_rx_drop_packet(priv);
554                 return true;
555         }
556
557         if (unlikely(ftgmac100_rxdes_multicast(rxdes)))
558                 netdev->stats.multicast++;
559
560         /* If the HW found checksum errors, bounce it to software.
561          *
562          * If we didn't, we need to see if the packet was recognized
563          * by HW as one of the supported checksummed protocols before
564          * we accept the HW test results.
565          */
566         if (netdev->features & NETIF_F_RXCSUM) {
567                 __le32 csum_vlan = rxdes->rxdes1;
568                 __le32 err_bits = cpu_to_le32(FTGMAC100_RXDES1_TCP_CHKSUM_ERR |
569                                               FTGMAC100_RXDES1_UDP_CHKSUM_ERR |
570                                               FTGMAC100_RXDES1_IP_CHKSUM_ERR);
571                 if ((csum_vlan & err_bits) ||
572                     !(csum_vlan & cpu_to_le32(FTGMAC100_RXDES1_PROT_MASK)))
573                         skb->ip_summed = CHECKSUM_NONE;
574                 else
575                         skb->ip_summed = CHECKSUM_UNNECESSARY;
576         }
577
578         map = ftgmac100_rxdes_get_dma_addr(rxdes);
579
580         dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
581
582         size = ftgmac100_rxdes_data_length(rxdes);
583         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, 0, size);
584
585         skb->len += size;
586         skb->data_len += size;
587         skb->truesize += PAGE_SIZE;
588
589         ftgmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
590
591         ftgmac100_rx_pointer_advance(priv);
592         rxdes = ftgmac100_current_rxdes(priv);
593
594         /* Small frames are copied into linear part of skb to free one page */
595         if (skb->len <= 128) {
596                 skb->truesize -= PAGE_SIZE;
597                 __pskb_pull_tail(skb, skb->len);
598         } else {
599                 /* We pull the minimum amount into linear part */
600                 __pskb_pull_tail(skb, ETH_HLEN);
601         }
602         skb->protocol = eth_type_trans(skb, netdev);
603
604         netdev->stats.rx_packets++;
605         netdev->stats.rx_bytes += skb->len;
606
607         /* push packet to protocol stack */
608         if (skb->ip_summed == CHECKSUM_NONE)
609                 netif_receive_skb(skb);
610         else
611                 napi_gro_receive(&priv->napi, skb);
612
613         (*processed)++;
614         return true;
615 }
616
617 static void ftgmac100_txdes_reset(const struct ftgmac100 *priv,
618                                   struct ftgmac100_txdes *txdes)
619 {
620         /* clear all except end of ring bit */
621         txdes->txdes0 &= cpu_to_le32(priv->txdes0_edotr_mask);
622         txdes->txdes1 = 0;
623         txdes->txdes2 = 0;
624         txdes->txdes3 = 0;
625 }
626
627 static bool ftgmac100_txdes_owned_by_dma(struct ftgmac100_txdes *txdes)
628 {
629         return txdes->txdes0 & cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
630 }
631
632 static void ftgmac100_txdes_set_dma_own(struct ftgmac100_txdes *txdes)
633 {
634         /*
635          * Make sure dma own bit will not be set before any other
636          * descriptor fields.
637          */
638         wmb();
639         txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXDMA_OWN);
640 }
641
642 static void ftgmac100_txdes_set_end_of_ring(const struct ftgmac100 *priv,
643                                             struct ftgmac100_txdes *txdes)
644 {
645         txdes->txdes0 |= cpu_to_le32(priv->txdes0_edotr_mask);
646 }
647
648 static void ftgmac100_txdes_set_first_segment(struct ftgmac100_txdes *txdes)
649 {
650         txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_FTS);
651 }
652
653 static void ftgmac100_txdes_set_last_segment(struct ftgmac100_txdes *txdes)
654 {
655         txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_LTS);
656 }
657
658 static void ftgmac100_txdes_set_buffer_size(struct ftgmac100_txdes *txdes,
659                                             unsigned int len)
660 {
661         txdes->txdes0 |= cpu_to_le32(FTGMAC100_TXDES0_TXBUF_SIZE(len));
662 }
663
664 static void ftgmac100_txdes_set_txint(struct ftgmac100_txdes *txdes)
665 {
666         txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TXIC);
667 }
668
669 static void ftgmac100_txdes_set_tcpcs(struct ftgmac100_txdes *txdes)
670 {
671         txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_TCP_CHKSUM);
672 }
673
674 static void ftgmac100_txdes_set_udpcs(struct ftgmac100_txdes *txdes)
675 {
676         txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_UDP_CHKSUM);
677 }
678
679 static void ftgmac100_txdes_set_ipcs(struct ftgmac100_txdes *txdes)
680 {
681         txdes->txdes1 |= cpu_to_le32(FTGMAC100_TXDES1_IP_CHKSUM);
682 }
683
684 static void ftgmac100_txdes_set_dma_addr(struct ftgmac100_txdes *txdes,
685                                          dma_addr_t addr)
686 {
687         txdes->txdes3 = cpu_to_le32(addr);
688 }
689
690 static dma_addr_t ftgmac100_txdes_get_dma_addr(struct ftgmac100_txdes *txdes)
691 {
692         return le32_to_cpu(txdes->txdes3);
693 }
694
695 /*
696  * txdes2 is not used by hardware. We use it to keep track of socket buffer.
697  * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
698  */
699 static void ftgmac100_txdes_set_skb(struct ftgmac100_txdes *txdes,
700                                     struct sk_buff *skb)
701 {
702         txdes->txdes2 = (unsigned int)skb;
703 }
704
705 static struct sk_buff *ftgmac100_txdes_get_skb(struct ftgmac100_txdes *txdes)
706 {
707         return (struct sk_buff *)txdes->txdes2;
708 }
709
710 static int ftgmac100_next_tx_pointer(int pointer)
711 {
712         return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
713 }
714
715 static void ftgmac100_tx_pointer_advance(struct ftgmac100 *priv)
716 {
717         priv->tx_pointer = ftgmac100_next_tx_pointer(priv->tx_pointer);
718 }
719
720 static void ftgmac100_tx_clean_pointer_advance(struct ftgmac100 *priv)
721 {
722         priv->tx_clean_pointer = ftgmac100_next_tx_pointer(priv->tx_clean_pointer);
723 }
724
725 static struct ftgmac100_txdes *ftgmac100_current_txdes(struct ftgmac100 *priv)
726 {
727         return &priv->descs->txdes[priv->tx_pointer];
728 }
729
730 static struct ftgmac100_txdes *
731 ftgmac100_current_clean_txdes(struct ftgmac100 *priv)
732 {
733         return &priv->descs->txdes[priv->tx_clean_pointer];
734 }
735
736 static bool ftgmac100_tx_complete_packet(struct ftgmac100 *priv)
737 {
738         struct net_device *netdev = priv->netdev;
739         struct ftgmac100_txdes *txdes;
740         struct sk_buff *skb;
741         dma_addr_t map;
742
743         if (priv->tx_pending == 0)
744                 return false;
745
746         txdes = ftgmac100_current_clean_txdes(priv);
747
748         if (ftgmac100_txdes_owned_by_dma(txdes))
749                 return false;
750
751         skb = ftgmac100_txdes_get_skb(txdes);
752         map = ftgmac100_txdes_get_dma_addr(txdes);
753
754         netdev->stats.tx_packets++;
755         netdev->stats.tx_bytes += skb->len;
756
757         dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
758
759         dev_kfree_skb(skb);
760
761         ftgmac100_txdes_reset(priv, txdes);
762
763         ftgmac100_tx_clean_pointer_advance(priv);
764
765         spin_lock(&priv->tx_lock);
766         priv->tx_pending--;
767         spin_unlock(&priv->tx_lock);
768         netif_wake_queue(netdev);
769
770         return true;
771 }
772
773 static void ftgmac100_tx_complete(struct ftgmac100 *priv)
774 {
775         while (ftgmac100_tx_complete_packet(priv))
776                 ;
777 }
778
779 static int ftgmac100_xmit(struct ftgmac100 *priv, struct sk_buff *skb,
780                           dma_addr_t map)
781 {
782         struct net_device *netdev = priv->netdev;
783         struct ftgmac100_txdes *txdes;
784         unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
785
786         txdes = ftgmac100_current_txdes(priv);
787         ftgmac100_tx_pointer_advance(priv);
788
789         /* setup TX descriptor */
790         ftgmac100_txdes_set_skb(txdes, skb);
791         ftgmac100_txdes_set_dma_addr(txdes, map);
792         ftgmac100_txdes_set_buffer_size(txdes, len);
793
794         ftgmac100_txdes_set_first_segment(txdes);
795         ftgmac100_txdes_set_last_segment(txdes);
796         ftgmac100_txdes_set_txint(txdes);
797         if (skb->ip_summed == CHECKSUM_PARTIAL) {
798                 __be16 protocol = skb->protocol;
799
800                 if (protocol == cpu_to_be16(ETH_P_IP)) {
801                         u8 ip_proto = ip_hdr(skb)->protocol;
802
803                         ftgmac100_txdes_set_ipcs(txdes);
804                         if (ip_proto == IPPROTO_TCP)
805                                 ftgmac100_txdes_set_tcpcs(txdes);
806                         else if (ip_proto == IPPROTO_UDP)
807                                 ftgmac100_txdes_set_udpcs(txdes);
808                 }
809         }
810
811         spin_lock(&priv->tx_lock);
812         priv->tx_pending++;
813         if (priv->tx_pending == TX_QUEUE_ENTRIES)
814                 netif_stop_queue(netdev);
815
816         /* start transmit */
817         ftgmac100_txdes_set_dma_own(txdes);
818         spin_unlock(&priv->tx_lock);
819
820         ftgmac100_txdma_normal_prio_start_polling(priv);
821
822         return NETDEV_TX_OK;
823 }
824
825 static void ftgmac100_free_buffers(struct ftgmac100 *priv)
826 {
827         int i;
828
829         /* Free all RX buffers */
830         for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
831                 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
832                 struct page *page = ftgmac100_rxdes_get_page(priv, rxdes);
833                 dma_addr_t map = ftgmac100_rxdes_get_dma_addr(rxdes);
834
835                 if (!page)
836                         continue;
837
838                 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
839                 __free_page(page);
840         }
841
842         /* Free all TX buffers */
843         for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
844                 struct ftgmac100_txdes *txdes = &priv->descs->txdes[i];
845                 struct sk_buff *skb = ftgmac100_txdes_get_skb(txdes);
846                 dma_addr_t map = ftgmac100_txdes_get_dma_addr(txdes);
847
848                 if (!skb)
849                         continue;
850
851                 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
852                 kfree_skb(skb);
853         }
854 }
855
856 static void ftgmac100_free_rings(struct ftgmac100 *priv)
857 {
858         /* Free descriptors */
859         if (priv->descs)
860                 dma_free_coherent(priv->dev, sizeof(struct ftgmac100_descs),
861                                   priv->descs, priv->descs_dma_addr);
862
863         /* Free scratch packet buffer */
864         if (priv->rx_scratch)
865                 dma_free_coherent(priv->dev, RX_BUF_SIZE,
866                                   priv->rx_scratch, priv->rx_scratch_dma);
867 }
868
869 static int ftgmac100_alloc_rings(struct ftgmac100 *priv)
870 {
871         /* Allocate descriptors */
872         priv->descs = dma_zalloc_coherent(priv->dev,
873                                           sizeof(struct ftgmac100_descs),
874                                           &priv->descs_dma_addr, GFP_KERNEL);
875         if (!priv->descs)
876                 return -ENOMEM;
877
878         /* Allocate scratch packet buffer */
879         priv->rx_scratch = dma_alloc_coherent(priv->dev,
880                                               RX_BUF_SIZE,
881                                               &priv->rx_scratch_dma,
882                                               GFP_KERNEL);
883         if (!priv->rx_scratch)
884                 return -ENOMEM;
885
886         return 0;
887 }
888
889 static void ftgmac100_init_rings(struct ftgmac100 *priv)
890 {
891         int i;
892
893         /* Initialize RX ring */
894         for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
895                 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
896                 ftgmac100_rxdes_set_dma_addr(rxdes, priv->rx_scratch_dma);
897                 rxdes->rxdes0 = 0;
898         }
899         ftgmac100_rxdes_set_end_of_ring(priv, &priv->descs->rxdes[i - 1]);
900
901         /* Initialize TX ring */
902         for (i = 0; i < TX_QUEUE_ENTRIES; i++)
903                 priv->descs->txdes[i].txdes0 = 0;
904         ftgmac100_txdes_set_end_of_ring(priv, &priv->descs->txdes[i -1]);
905 }
906
907 static int ftgmac100_alloc_rx_buffers(struct ftgmac100 *priv)
908 {
909         int i;
910
911         for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
912                 struct ftgmac100_rxdes *rxdes = &priv->descs->rxdes[i];
913
914                 if (ftgmac100_alloc_rx_page(priv, rxdes, GFP_KERNEL))
915                         return -ENOMEM;
916         }
917         return 0;
918 }
919
920 static void ftgmac100_adjust_link(struct net_device *netdev)
921 {
922         struct ftgmac100 *priv = netdev_priv(netdev);
923         struct phy_device *phydev = netdev->phydev;
924         int new_speed;
925
926         /* We store "no link" as speed 0 */
927         if (!phydev->link)
928                 new_speed = 0;
929         else
930                 new_speed = phydev->speed;
931
932         if (phydev->speed == priv->cur_speed &&
933             phydev->duplex == priv->cur_duplex)
934                 return;
935
936         /* Print status if we have a link or we had one and just lost it,
937          * don't print otherwise.
938          */
939         if (new_speed || priv->cur_speed)
940                 phy_print_status(phydev);
941
942         priv->cur_speed = new_speed;
943         priv->cur_duplex = phydev->duplex;
944
945         /* Link is down, do nothing else */
946         if (!new_speed)
947                 return;
948
949         /* Disable all interrupts */
950         iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
951
952         /* Reset the adapter asynchronously */
953         schedule_work(&priv->reset_task);
954 }
955
956 static int ftgmac100_mii_probe(struct ftgmac100 *priv)
957 {
958         struct net_device *netdev = priv->netdev;
959         struct phy_device *phydev;
960
961         phydev = phy_find_first(priv->mii_bus);
962         if (!phydev) {
963                 netdev_info(netdev, "%s: no PHY found\n", netdev->name);
964                 return -ENODEV;
965         }
966
967         phydev = phy_connect(netdev, phydev_name(phydev),
968                              &ftgmac100_adjust_link, PHY_INTERFACE_MODE_GMII);
969
970         if (IS_ERR(phydev)) {
971                 netdev_err(netdev, "%s: Could not attach to PHY\n", netdev->name);
972                 return PTR_ERR(phydev);
973         }
974
975         return 0;
976 }
977
978 static int ftgmac100_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
979 {
980         struct net_device *netdev = bus->priv;
981         struct ftgmac100 *priv = netdev_priv(netdev);
982         unsigned int phycr;
983         int i;
984
985         phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
986
987         /* preserve MDC cycle threshold */
988         phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
989
990         phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
991                  FTGMAC100_PHYCR_REGAD(regnum) |
992                  FTGMAC100_PHYCR_MIIRD;
993
994         iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
995
996         for (i = 0; i < 10; i++) {
997                 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
998
999                 if ((phycr & FTGMAC100_PHYCR_MIIRD) == 0) {
1000                         int data;
1001
1002                         data = ioread32(priv->base + FTGMAC100_OFFSET_PHYDATA);
1003                         return FTGMAC100_PHYDATA_MIIRDATA(data);
1004                 }
1005
1006                 udelay(100);
1007         }
1008
1009         netdev_err(netdev, "mdio read timed out\n");
1010         return -EIO;
1011 }
1012
1013 static int ftgmac100_mdiobus_write(struct mii_bus *bus, int phy_addr,
1014                                    int regnum, u16 value)
1015 {
1016         struct net_device *netdev = bus->priv;
1017         struct ftgmac100 *priv = netdev_priv(netdev);
1018         unsigned int phycr;
1019         int data;
1020         int i;
1021
1022         phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1023
1024         /* preserve MDC cycle threshold */
1025         phycr &= FTGMAC100_PHYCR_MDC_CYCTHR_MASK;
1026
1027         phycr |= FTGMAC100_PHYCR_PHYAD(phy_addr) |
1028                  FTGMAC100_PHYCR_REGAD(regnum) |
1029                  FTGMAC100_PHYCR_MIIWR;
1030
1031         data = FTGMAC100_PHYDATA_MIIWDATA(value);
1032
1033         iowrite32(data, priv->base + FTGMAC100_OFFSET_PHYDATA);
1034         iowrite32(phycr, priv->base + FTGMAC100_OFFSET_PHYCR);
1035
1036         for (i = 0; i < 10; i++) {
1037                 phycr = ioread32(priv->base + FTGMAC100_OFFSET_PHYCR);
1038
1039                 if ((phycr & FTGMAC100_PHYCR_MIIWR) == 0)
1040                         return 0;
1041
1042                 udelay(100);
1043         }
1044
1045         netdev_err(netdev, "mdio write timed out\n");
1046         return -EIO;
1047 }
1048
1049 static void ftgmac100_get_drvinfo(struct net_device *netdev,
1050                                   struct ethtool_drvinfo *info)
1051 {
1052         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1053         strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1054         strlcpy(info->bus_info, dev_name(&netdev->dev), sizeof(info->bus_info));
1055 }
1056
1057 static const struct ethtool_ops ftgmac100_ethtool_ops = {
1058         .get_drvinfo            = ftgmac100_get_drvinfo,
1059         .get_link               = ethtool_op_get_link,
1060         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
1061         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
1062 };
1063
1064 static irqreturn_t ftgmac100_interrupt(int irq, void *dev_id)
1065 {
1066         struct net_device *netdev = dev_id;
1067         struct ftgmac100 *priv = netdev_priv(netdev);
1068         unsigned int status, new_mask = FTGMAC100_INT_BAD;
1069
1070         /* Fetch and clear interrupt bits, process abnormal ones */
1071         status = ioread32(priv->base + FTGMAC100_OFFSET_ISR);
1072         iowrite32(status, priv->base + FTGMAC100_OFFSET_ISR);
1073         if (unlikely(status & FTGMAC100_INT_BAD)) {
1074
1075                 /* RX buffer unavailable */
1076                 if (status & FTGMAC100_INT_NO_RXBUF)
1077                         netdev->stats.rx_over_errors++;
1078
1079                 /* received packet lost due to RX FIFO full */
1080                 if (status & FTGMAC100_INT_RPKT_LOST)
1081                         netdev->stats.rx_fifo_errors++;
1082
1083                 /* sent packet lost due to excessive TX collision */
1084                 if (status & FTGMAC100_INT_XPKT_LOST)
1085                         netdev->stats.tx_fifo_errors++;
1086
1087                 /* AHB error -> Reset the chip */
1088                 if (status & FTGMAC100_INT_AHB_ERR) {
1089                         if (net_ratelimit())
1090                                 netdev_warn(netdev,
1091                                            "AHB bus error ! Resetting chip.\n");
1092                         iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1093                         schedule_work(&priv->reset_task);
1094                         return IRQ_HANDLED;
1095                 }
1096
1097                 /* We may need to restart the MAC after such errors, delay
1098                  * this until after we have freed some Rx buffers though
1099                  */
1100                 priv->need_mac_restart = true;
1101
1102                 /* Disable those errors until we restart */
1103                 new_mask &= ~status;
1104         }
1105
1106         /* Only enable "bad" interrupts while NAPI is on */
1107         iowrite32(new_mask, priv->base + FTGMAC100_OFFSET_IER);
1108
1109         /* Schedule NAPI bh */
1110         napi_schedule_irqoff(&priv->napi);
1111
1112         return IRQ_HANDLED;
1113 }
1114
1115 static int ftgmac100_poll(struct napi_struct *napi, int budget)
1116 {
1117         struct ftgmac100 *priv = container_of(napi, struct ftgmac100, napi);
1118         bool more, completed = true;
1119         int rx = 0;
1120
1121         ftgmac100_tx_complete(priv);
1122
1123         do {
1124                 more = ftgmac100_rx_packet(priv, &rx);
1125         } while (more && rx < budget);
1126
1127         if (more && rx == budget)
1128                 completed = false;
1129
1130
1131         /* The interrupt is telling us to kick the MAC back to life
1132          * after an RX overflow
1133          */
1134         if (unlikely(priv->need_mac_restart)) {
1135                 ftgmac100_start_hw(priv);
1136
1137                 /* Re-enable "bad" interrupts */
1138                 iowrite32(FTGMAC100_INT_BAD,
1139                           priv->base + FTGMAC100_OFFSET_IER);
1140         }
1141
1142         /* Keep NAPI going if we have still packets to reclaim */
1143         if (priv->tx_pending)
1144                 return budget;
1145
1146         if (completed) {
1147                 /* We are about to re-enable all interrupts. However
1148                  * the HW has been latching RX/TX packet interrupts while
1149                  * they were masked. So we clear them first, then we need
1150                  * to re-check if there's something to process
1151                  */
1152                 iowrite32(FTGMAC100_INT_RXTX,
1153                           priv->base + FTGMAC100_OFFSET_ISR);
1154                 if (ftgmac100_rxdes_packet_ready
1155                     (ftgmac100_current_rxdes(priv)) || priv->tx_pending)
1156                         return budget;
1157
1158                 /* deschedule NAPI */
1159                 napi_complete(napi);
1160
1161                 /* enable all interrupts */
1162                 iowrite32(FTGMAC100_INT_ALL,
1163                           priv->base + FTGMAC100_OFFSET_IER);
1164         }
1165
1166         return rx;
1167 }
1168
1169 static int ftgmac100_init_all(struct ftgmac100 *priv, bool ignore_alloc_err)
1170 {
1171         int err = 0;
1172
1173         /* Re-init descriptors (adjust queue sizes) */
1174         ftgmac100_init_rings(priv);
1175
1176         /* Realloc rx descriptors */
1177         err = ftgmac100_alloc_rx_buffers(priv);
1178         if (err && !ignore_alloc_err)
1179                 return err;
1180
1181         /* Reinit and restart HW */
1182         ftgmac100_init_hw(priv);
1183         ftgmac100_start_hw(priv);
1184
1185         /* Re-enable the device */
1186         napi_enable(&priv->napi);
1187         netif_start_queue(priv->netdev);
1188
1189         /* Enable all interrupts */
1190         iowrite32(FTGMAC100_INT_ALL, priv->base + FTGMAC100_OFFSET_IER);
1191
1192         return err;
1193 }
1194
1195 static void ftgmac100_reset_task(struct work_struct *work)
1196 {
1197         struct ftgmac100 *priv = container_of(work, struct ftgmac100,
1198                                               reset_task);
1199         struct net_device *netdev = priv->netdev;
1200         int err;
1201
1202         netdev_dbg(netdev, "Resetting NIC...\n");
1203
1204         /* Lock the world */
1205         rtnl_lock();
1206         if (netdev->phydev)
1207                 mutex_lock(&netdev->phydev->lock);
1208         if (priv->mii_bus)
1209                 mutex_lock(&priv->mii_bus->mdio_lock);
1210
1211
1212         /* Check if the interface is still up */
1213         if (!netif_running(netdev))
1214                 goto bail;
1215
1216         /* Stop the network stack */
1217         netif_trans_update(netdev);
1218         napi_disable(&priv->napi);
1219         netif_tx_disable(netdev);
1220
1221         /* Stop and reset the MAC */
1222         ftgmac100_stop_hw(priv);
1223         err = ftgmac100_reset_and_config_mac(priv);
1224         if (err) {
1225                 /* Not much we can do ... it might come back... */
1226                 netdev_err(netdev, "attempting to continue...\n");
1227         }
1228
1229         /* Free all rx and tx buffers */
1230         ftgmac100_free_buffers(priv);
1231
1232         /* Setup everything again and restart chip */
1233         ftgmac100_init_all(priv, true);
1234
1235         netdev_dbg(netdev, "Reset done !\n");
1236  bail:
1237         if (priv->mii_bus)
1238                 mutex_unlock(&priv->mii_bus->mdio_lock);
1239         if (netdev->phydev)
1240                 mutex_unlock(&netdev->phydev->lock);
1241         rtnl_unlock();
1242 }
1243
1244 static int ftgmac100_open(struct net_device *netdev)
1245 {
1246         struct ftgmac100 *priv = netdev_priv(netdev);
1247         int err;
1248
1249         /* Allocate ring buffers  */
1250         err = ftgmac100_alloc_rings(priv);
1251         if (err) {
1252                 netdev_err(netdev, "Failed to allocate descriptors\n");
1253                 return err;
1254         }
1255
1256         /* When using NC-SI we force the speed to 100Mbit/s full duplex,
1257          *
1258          * Otherwise we leave it set to 0 (no link), the link
1259          * message from the PHY layer will handle setting it up to
1260          * something else if needed.
1261          */
1262         if (priv->use_ncsi) {
1263                 priv->cur_duplex = DUPLEX_FULL;
1264                 priv->cur_speed = SPEED_100;
1265         } else {
1266                 priv->cur_duplex = 0;
1267                 priv->cur_speed = 0;
1268         }
1269
1270         /* Reset the hardware */
1271         err = ftgmac100_reset_and_config_mac(priv);
1272         if (err)
1273                 goto err_hw;
1274
1275         /* Initialize NAPI */
1276         netif_napi_add(netdev, &priv->napi, ftgmac100_poll, 64);
1277
1278         /* Grab our interrupt */
1279         err = request_irq(netdev->irq, ftgmac100_interrupt, 0, netdev->name, netdev);
1280         if (err) {
1281                 netdev_err(netdev, "failed to request irq %d\n", netdev->irq);
1282                 goto err_irq;
1283         }
1284
1285         /* Start things up */
1286         err = ftgmac100_init_all(priv, false);
1287         if (err) {
1288                 netdev_err(netdev, "Failed to allocate packet buffers\n");
1289                 goto err_alloc;
1290         }
1291
1292         if (netdev->phydev) {
1293                 /* If we have a PHY, start polling */
1294                 phy_start(netdev->phydev);
1295         } else if (priv->use_ncsi) {
1296                 /* If using NC-SI, set our carrier on and start the stack */
1297                 netif_carrier_on(netdev);
1298
1299                 /* Start the NCSI device */
1300                 err = ncsi_start_dev(priv->ndev);
1301                 if (err)
1302                         goto err_ncsi;
1303         }
1304
1305         return 0;
1306
1307  err_ncsi:
1308         napi_disable(&priv->napi);
1309         netif_stop_queue(netdev);
1310  err_alloc:
1311         ftgmac100_free_buffers(priv);
1312         free_irq(netdev->irq, netdev);
1313  err_irq:
1314         netif_napi_del(&priv->napi);
1315  err_hw:
1316         iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1317         ftgmac100_free_rings(priv);
1318         return err;
1319 }
1320
1321 static int ftgmac100_stop(struct net_device *netdev)
1322 {
1323         struct ftgmac100 *priv = netdev_priv(netdev);
1324
1325         /* Note about the reset task: We are called with the rtnl lock
1326          * held, so we are synchronized against the core of the reset
1327          * task. We must not try to synchronously cancel it otherwise
1328          * we can deadlock. But since it will test for netif_running()
1329          * which has already been cleared by the net core, we don't
1330          * anything special to do.
1331          */
1332
1333         /* disable all interrupts */
1334         iowrite32(0, priv->base + FTGMAC100_OFFSET_IER);
1335
1336         netif_stop_queue(netdev);
1337         napi_disable(&priv->napi);
1338         netif_napi_del(&priv->napi);
1339         if (netdev->phydev)
1340                 phy_stop(netdev->phydev);
1341         else if (priv->use_ncsi)
1342                 ncsi_stop_dev(priv->ndev);
1343
1344         ftgmac100_stop_hw(priv);
1345         free_irq(netdev->irq, netdev);
1346         ftgmac100_free_buffers(priv);
1347         ftgmac100_free_rings(priv);
1348
1349         return 0;
1350 }
1351
1352 static int ftgmac100_hard_start_xmit(struct sk_buff *skb,
1353                                      struct net_device *netdev)
1354 {
1355         struct ftgmac100 *priv = netdev_priv(netdev);
1356         dma_addr_t map;
1357
1358         if (unlikely(skb->len > MAX_PKT_SIZE)) {
1359                 if (net_ratelimit())
1360                         netdev_dbg(netdev, "tx packet too big\n");
1361
1362                 netdev->stats.tx_dropped++;
1363                 kfree_skb(skb);
1364                 return NETDEV_TX_OK;
1365         }
1366
1367         map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
1368         if (unlikely(dma_mapping_error(priv->dev, map))) {
1369                 /* drop packet */
1370                 if (net_ratelimit())
1371                         netdev_err(netdev, "map socket buffer failed\n");
1372
1373                 netdev->stats.tx_dropped++;
1374                 kfree_skb(skb);
1375                 return NETDEV_TX_OK;
1376         }
1377
1378         return ftgmac100_xmit(priv, skb, map);
1379 }
1380
1381 /* optional */
1382 static int ftgmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1383 {
1384         if (!netdev->phydev)
1385                 return -ENXIO;
1386
1387         return phy_mii_ioctl(netdev->phydev, ifr, cmd);
1388 }
1389
1390 static const struct net_device_ops ftgmac100_netdev_ops = {
1391         .ndo_open               = ftgmac100_open,
1392         .ndo_stop               = ftgmac100_stop,
1393         .ndo_start_xmit         = ftgmac100_hard_start_xmit,
1394         .ndo_set_mac_address    = ftgmac100_set_mac_addr,
1395         .ndo_validate_addr      = eth_validate_addr,
1396         .ndo_do_ioctl           = ftgmac100_do_ioctl,
1397 };
1398
1399 static int ftgmac100_setup_mdio(struct net_device *netdev)
1400 {
1401         struct ftgmac100 *priv = netdev_priv(netdev);
1402         struct platform_device *pdev = to_platform_device(priv->dev);
1403         int i, err = 0;
1404         u32 reg;
1405
1406         /* initialize mdio bus */
1407         priv->mii_bus = mdiobus_alloc();
1408         if (!priv->mii_bus)
1409                 return -EIO;
1410
1411         if (of_machine_is_compatible("aspeed,ast2400") ||
1412             of_machine_is_compatible("aspeed,ast2500")) {
1413                 /* This driver supports the old MDIO interface */
1414                 reg = ioread32(priv->base + FTGMAC100_OFFSET_REVR);
1415                 reg &= ~FTGMAC100_REVR_NEW_MDIO_INTERFACE;
1416                 iowrite32(reg, priv->base + FTGMAC100_OFFSET_REVR);
1417         };
1418
1419         priv->mii_bus->name = "ftgmac100_mdio";
1420         snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%d",
1421                  pdev->name, pdev->id);
1422         priv->mii_bus->priv = priv->netdev;
1423         priv->mii_bus->read = ftgmac100_mdiobus_read;
1424         priv->mii_bus->write = ftgmac100_mdiobus_write;
1425
1426         for (i = 0; i < PHY_MAX_ADDR; i++)
1427                 priv->mii_bus->irq[i] = PHY_POLL;
1428
1429         err = mdiobus_register(priv->mii_bus);
1430         if (err) {
1431                 dev_err(priv->dev, "Cannot register MDIO bus!\n");
1432                 goto err_register_mdiobus;
1433         }
1434
1435         err = ftgmac100_mii_probe(priv);
1436         if (err) {
1437                 dev_err(priv->dev, "MII Probe failed!\n");
1438                 goto err_mii_probe;
1439         }
1440
1441         return 0;
1442
1443 err_mii_probe:
1444         mdiobus_unregister(priv->mii_bus);
1445 err_register_mdiobus:
1446         mdiobus_free(priv->mii_bus);
1447         return err;
1448 }
1449
1450 static void ftgmac100_destroy_mdio(struct net_device *netdev)
1451 {
1452         struct ftgmac100 *priv = netdev_priv(netdev);
1453
1454         if (!netdev->phydev)
1455                 return;
1456
1457         phy_disconnect(netdev->phydev);
1458         mdiobus_unregister(priv->mii_bus);
1459         mdiobus_free(priv->mii_bus);
1460 }
1461
1462 static void ftgmac100_ncsi_handler(struct ncsi_dev *nd)
1463 {
1464         if (unlikely(nd->state != ncsi_dev_state_functional))
1465                 return;
1466
1467         netdev_info(nd->dev, "NCSI interface %s\n",
1468                     nd->link_up ? "up" : "down");
1469 }
1470
1471 static int ftgmac100_probe(struct platform_device *pdev)
1472 {
1473         struct resource *res;
1474         int irq;
1475         struct net_device *netdev;
1476         struct ftgmac100 *priv;
1477         int err = 0;
1478
1479         if (!pdev)
1480                 return -ENODEV;
1481
1482         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1483         if (!res)
1484                 return -ENXIO;
1485
1486         irq = platform_get_irq(pdev, 0);
1487         if (irq < 0)
1488                 return irq;
1489
1490         /* setup net_device */
1491         netdev = alloc_etherdev(sizeof(*priv));
1492         if (!netdev) {
1493                 err = -ENOMEM;
1494                 goto err_alloc_etherdev;
1495         }
1496
1497         SET_NETDEV_DEV(netdev, &pdev->dev);
1498
1499         netdev->ethtool_ops = &ftgmac100_ethtool_ops;
1500         netdev->netdev_ops = &ftgmac100_netdev_ops;
1501
1502         platform_set_drvdata(pdev, netdev);
1503
1504         /* setup private data */
1505         priv = netdev_priv(netdev);
1506         priv->netdev = netdev;
1507         priv->dev = &pdev->dev;
1508         INIT_WORK(&priv->reset_task, ftgmac100_reset_task);
1509
1510         spin_lock_init(&priv->tx_lock);
1511
1512         /* map io memory */
1513         priv->res = request_mem_region(res->start, resource_size(res),
1514                                        dev_name(&pdev->dev));
1515         if (!priv->res) {
1516                 dev_err(&pdev->dev, "Could not reserve memory region\n");
1517                 err = -ENOMEM;
1518                 goto err_req_mem;
1519         }
1520
1521         priv->base = ioremap(res->start, resource_size(res));
1522         if (!priv->base) {
1523                 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1524                 err = -EIO;
1525                 goto err_ioremap;
1526         }
1527
1528         netdev->irq = irq;
1529
1530         /* MAC address from chip or random one */
1531         ftgmac100_setup_mac(priv);
1532
1533         if (of_machine_is_compatible("aspeed,ast2400") ||
1534             of_machine_is_compatible("aspeed,ast2500")) {
1535                 priv->rxdes0_edorr_mask = BIT(30);
1536                 priv->txdes0_edotr_mask = BIT(30);
1537         } else {
1538                 priv->rxdes0_edorr_mask = BIT(15);
1539                 priv->txdes0_edotr_mask = BIT(15);
1540         }
1541
1542         if (pdev->dev.of_node &&
1543             of_get_property(pdev->dev.of_node, "use-ncsi", NULL)) {
1544                 if (!IS_ENABLED(CONFIG_NET_NCSI)) {
1545                         dev_err(&pdev->dev, "NCSI stack not enabled\n");
1546                         goto err_ncsi_dev;
1547                 }
1548
1549                 dev_info(&pdev->dev, "Using NCSI interface\n");
1550                 priv->use_ncsi = true;
1551                 priv->ndev = ncsi_register_dev(netdev, ftgmac100_ncsi_handler);
1552                 if (!priv->ndev)
1553                         goto err_ncsi_dev;
1554         } else {
1555                 priv->use_ncsi = false;
1556                 err = ftgmac100_setup_mdio(netdev);
1557                 if (err)
1558                         goto err_setup_mdio;
1559         }
1560
1561         /* We have to disable on-chip IP checksum functionality
1562          * when NCSI is enabled on the interface. It doesn't work
1563          * in that case.
1564          */
1565         netdev->features = NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_GRO;
1566         if (priv->use_ncsi &&
1567             of_get_property(pdev->dev.of_node, "no-hw-checksum", NULL))
1568                 netdev->features &= ~NETIF_F_IP_CSUM;
1569
1570
1571         /* register network device */
1572         err = register_netdev(netdev);
1573         if (err) {
1574                 dev_err(&pdev->dev, "Failed to register netdev\n");
1575                 goto err_register_netdev;
1576         }
1577
1578         netdev_info(netdev, "irq %d, mapped at %p\n", netdev->irq, priv->base);
1579
1580         return 0;
1581
1582 err_ncsi_dev:
1583 err_register_netdev:
1584         ftgmac100_destroy_mdio(netdev);
1585 err_setup_mdio:
1586         iounmap(priv->base);
1587 err_ioremap:
1588         release_resource(priv->res);
1589 err_req_mem:
1590         netif_napi_del(&priv->napi);
1591         free_netdev(netdev);
1592 err_alloc_etherdev:
1593         return err;
1594 }
1595
1596 static int ftgmac100_remove(struct platform_device *pdev)
1597 {
1598         struct net_device *netdev;
1599         struct ftgmac100 *priv;
1600
1601         netdev = platform_get_drvdata(pdev);
1602         priv = netdev_priv(netdev);
1603
1604         unregister_netdev(netdev);
1605
1606         /* There's a small chance the reset task will have been re-queued,
1607          * during stop, make sure it's gone before we free the structure.
1608          */
1609         cancel_work_sync(&priv->reset_task);
1610
1611         ftgmac100_destroy_mdio(netdev);
1612
1613         iounmap(priv->base);
1614         release_resource(priv->res);
1615
1616         netif_napi_del(&priv->napi);
1617         free_netdev(netdev);
1618         return 0;
1619 }
1620
1621 static const struct of_device_id ftgmac100_of_match[] = {
1622         { .compatible = "faraday,ftgmac100" },
1623         { }
1624 };
1625 MODULE_DEVICE_TABLE(of, ftgmac100_of_match);
1626
1627 static struct platform_driver ftgmac100_driver = {
1628         .probe  = ftgmac100_probe,
1629         .remove = ftgmac100_remove,
1630         .driver = {
1631                 .name           = DRV_NAME,
1632                 .of_match_table = ftgmac100_of_match,
1633         },
1634 };
1635 module_platform_driver(ftgmac100_driver);
1636
1637 MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1638 MODULE_DESCRIPTION("FTGMAC100 driver");
1639 MODULE_LICENSE("GPL");