net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.git] / drivers / net / ethernet / faraday / ftmac100.c
1 /*
2  * Faraday FTMAC100 10/100 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/init.h>
28 #include <linux/io.h>
29 #include <linux/mii.h>
30 #include <linux/module.h>
31 #include <linux/netdevice.h>
32 #include <linux/platform_device.h>
33
34 #include "ftmac100.h"
35
36 #define DRV_NAME        "ftmac100"
37 #define DRV_VERSION     "0.2"
38
39 #define RX_QUEUE_ENTRIES        128     /* must be power of 2 */
40 #define TX_QUEUE_ENTRIES        16      /* must be power of 2 */
41
42 #define MAX_PKT_SIZE            1518
43 #define RX_BUF_SIZE             2044    /* must be smaller than 0x7ff */
44
45 #if MAX_PKT_SIZE > 0x7ff
46 #error invalid MAX_PKT_SIZE
47 #endif
48
49 #if RX_BUF_SIZE > 0x7ff || RX_BUF_SIZE > PAGE_SIZE
50 #error invalid RX_BUF_SIZE
51 #endif
52
53 /******************************************************************************
54  * private data
55  *****************************************************************************/
56 struct ftmac100_descs {
57         struct ftmac100_rxdes rxdes[RX_QUEUE_ENTRIES];
58         struct ftmac100_txdes txdes[TX_QUEUE_ENTRIES];
59 };
60
61 struct ftmac100 {
62         struct resource *res;
63         void __iomem *base;
64         int irq;
65
66         struct ftmac100_descs *descs;
67         dma_addr_t descs_dma_addr;
68
69         unsigned int rx_pointer;
70         unsigned int tx_clean_pointer;
71         unsigned int tx_pointer;
72         unsigned int tx_pending;
73
74         spinlock_t tx_lock;
75
76         struct net_device *netdev;
77         struct device *dev;
78         struct napi_struct napi;
79
80         struct mii_if_info mii;
81 };
82
83 static int ftmac100_alloc_rx_page(struct ftmac100 *priv,
84                                   struct ftmac100_rxdes *rxdes, gfp_t gfp);
85
86 /******************************************************************************
87  * internal functions (hardware register access)
88  *****************************************************************************/
89 #define INT_MASK_ALL_ENABLED    (FTMAC100_INT_RPKT_FINISH       | \
90                                  FTMAC100_INT_NORXBUF           | \
91                                  FTMAC100_INT_XPKT_OK           | \
92                                  FTMAC100_INT_XPKT_LOST         | \
93                                  FTMAC100_INT_RPKT_LOST         | \
94                                  FTMAC100_INT_AHB_ERR           | \
95                                  FTMAC100_INT_PHYSTS_CHG)
96
97 #define INT_MASK_ALL_DISABLED   0
98
99 static void ftmac100_enable_all_int(struct ftmac100 *priv)
100 {
101         iowrite32(INT_MASK_ALL_ENABLED, priv->base + FTMAC100_OFFSET_IMR);
102 }
103
104 static void ftmac100_disable_all_int(struct ftmac100 *priv)
105 {
106         iowrite32(INT_MASK_ALL_DISABLED, priv->base + FTMAC100_OFFSET_IMR);
107 }
108
109 static void ftmac100_set_rx_ring_base(struct ftmac100 *priv, dma_addr_t addr)
110 {
111         iowrite32(addr, priv->base + FTMAC100_OFFSET_RXR_BADR);
112 }
113
114 static void ftmac100_set_tx_ring_base(struct ftmac100 *priv, dma_addr_t addr)
115 {
116         iowrite32(addr, priv->base + FTMAC100_OFFSET_TXR_BADR);
117 }
118
119 static void ftmac100_txdma_start_polling(struct ftmac100 *priv)
120 {
121         iowrite32(1, priv->base + FTMAC100_OFFSET_TXPD);
122 }
123
124 static int ftmac100_reset(struct ftmac100 *priv)
125 {
126         struct net_device *netdev = priv->netdev;
127         int i;
128
129         /* NOTE: reset clears all registers */
130         iowrite32(FTMAC100_MACCR_SW_RST, priv->base + FTMAC100_OFFSET_MACCR);
131
132         for (i = 0; i < 5; i++) {
133                 unsigned int maccr;
134
135                 maccr = ioread32(priv->base + FTMAC100_OFFSET_MACCR);
136                 if (!(maccr & FTMAC100_MACCR_SW_RST)) {
137                         /*
138                          * FTMAC100_MACCR_SW_RST cleared does not indicate
139                          * that hardware reset completed (what the f*ck).
140                          * We still need to wait for a while.
141                          */
142                         udelay(500);
143                         return 0;
144                 }
145
146                 udelay(1000);
147         }
148
149         netdev_err(netdev, "software reset failed\n");
150         return -EIO;
151 }
152
153 static void ftmac100_set_mac(struct ftmac100 *priv, const unsigned char *mac)
154 {
155         unsigned int maddr = mac[0] << 8 | mac[1];
156         unsigned int laddr = mac[2] << 24 | mac[3] << 16 | mac[4] << 8 | mac[5];
157
158         iowrite32(maddr, priv->base + FTMAC100_OFFSET_MAC_MADR);
159         iowrite32(laddr, priv->base + FTMAC100_OFFSET_MAC_LADR);
160 }
161
162 #define MACCR_ENABLE_ALL        (FTMAC100_MACCR_XMT_EN  | \
163                                  FTMAC100_MACCR_RCV_EN  | \
164                                  FTMAC100_MACCR_XDMA_EN | \
165                                  FTMAC100_MACCR_RDMA_EN | \
166                                  FTMAC100_MACCR_CRC_APD | \
167                                  FTMAC100_MACCR_FULLDUP | \
168                                  FTMAC100_MACCR_RX_RUNT | \
169                                  FTMAC100_MACCR_RX_BROADPKT)
170
171 static int ftmac100_start_hw(struct ftmac100 *priv)
172 {
173         struct net_device *netdev = priv->netdev;
174
175         if (ftmac100_reset(priv))
176                 return -EIO;
177
178         /* setup ring buffer base registers */
179         ftmac100_set_rx_ring_base(priv,
180                                   priv->descs_dma_addr +
181                                   offsetof(struct ftmac100_descs, rxdes));
182         ftmac100_set_tx_ring_base(priv,
183                                   priv->descs_dma_addr +
184                                   offsetof(struct ftmac100_descs, txdes));
185
186         iowrite32(FTMAC100_APTC_RXPOLL_CNT(1), priv->base + FTMAC100_OFFSET_APTC);
187
188         ftmac100_set_mac(priv, netdev->dev_addr);
189
190         iowrite32(MACCR_ENABLE_ALL, priv->base + FTMAC100_OFFSET_MACCR);
191         return 0;
192 }
193
194 static void ftmac100_stop_hw(struct ftmac100 *priv)
195 {
196         iowrite32(0, priv->base + FTMAC100_OFFSET_MACCR);
197 }
198
199 /******************************************************************************
200  * internal functions (receive descriptor)
201  *****************************************************************************/
202 static bool ftmac100_rxdes_first_segment(struct ftmac100_rxdes *rxdes)
203 {
204         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_FRS);
205 }
206
207 static bool ftmac100_rxdes_last_segment(struct ftmac100_rxdes *rxdes)
208 {
209         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_LRS);
210 }
211
212 static bool ftmac100_rxdes_owned_by_dma(struct ftmac100_rxdes *rxdes)
213 {
214         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RXDMA_OWN);
215 }
216
217 static void ftmac100_rxdes_set_dma_own(struct ftmac100_rxdes *rxdes)
218 {
219         /* clear status bits */
220         rxdes->rxdes0 = cpu_to_le32(FTMAC100_RXDES0_RXDMA_OWN);
221 }
222
223 static bool ftmac100_rxdes_rx_error(struct ftmac100_rxdes *rxdes)
224 {
225         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RX_ERR);
226 }
227
228 static bool ftmac100_rxdes_crc_error(struct ftmac100_rxdes *rxdes)
229 {
230         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_CRC_ERR);
231 }
232
233 static bool ftmac100_rxdes_frame_too_long(struct ftmac100_rxdes *rxdes)
234 {
235         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_FTL);
236 }
237
238 static bool ftmac100_rxdes_runt(struct ftmac100_rxdes *rxdes)
239 {
240         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RUNT);
241 }
242
243 static bool ftmac100_rxdes_odd_nibble(struct ftmac100_rxdes *rxdes)
244 {
245         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_RX_ODD_NB);
246 }
247
248 static unsigned int ftmac100_rxdes_frame_length(struct ftmac100_rxdes *rxdes)
249 {
250         return le32_to_cpu(rxdes->rxdes0) & FTMAC100_RXDES0_RFL;
251 }
252
253 static bool ftmac100_rxdes_multicast(struct ftmac100_rxdes *rxdes)
254 {
255         return rxdes->rxdes0 & cpu_to_le32(FTMAC100_RXDES0_MULTICAST);
256 }
257
258 static void ftmac100_rxdes_set_buffer_size(struct ftmac100_rxdes *rxdes,
259                                            unsigned int size)
260 {
261         rxdes->rxdes1 &= cpu_to_le32(FTMAC100_RXDES1_EDORR);
262         rxdes->rxdes1 |= cpu_to_le32(FTMAC100_RXDES1_RXBUF_SIZE(size));
263 }
264
265 static void ftmac100_rxdes_set_end_of_ring(struct ftmac100_rxdes *rxdes)
266 {
267         rxdes->rxdes1 |= cpu_to_le32(FTMAC100_RXDES1_EDORR);
268 }
269
270 static void ftmac100_rxdes_set_dma_addr(struct ftmac100_rxdes *rxdes,
271                                         dma_addr_t addr)
272 {
273         rxdes->rxdes2 = cpu_to_le32(addr);
274 }
275
276 static dma_addr_t ftmac100_rxdes_get_dma_addr(struct ftmac100_rxdes *rxdes)
277 {
278         return le32_to_cpu(rxdes->rxdes2);
279 }
280
281 /*
282  * rxdes3 is not used by hardware. We use it to keep track of page.
283  * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
284  */
285 static void ftmac100_rxdes_set_page(struct ftmac100_rxdes *rxdes, struct page *page)
286 {
287         rxdes->rxdes3 = (unsigned int)page;
288 }
289
290 static struct page *ftmac100_rxdes_get_page(struct ftmac100_rxdes *rxdes)
291 {
292         return (struct page *)rxdes->rxdes3;
293 }
294
295 /******************************************************************************
296  * internal functions (receive)
297  *****************************************************************************/
298 static int ftmac100_next_rx_pointer(int pointer)
299 {
300         return (pointer + 1) & (RX_QUEUE_ENTRIES - 1);
301 }
302
303 static void ftmac100_rx_pointer_advance(struct ftmac100 *priv)
304 {
305         priv->rx_pointer = ftmac100_next_rx_pointer(priv->rx_pointer);
306 }
307
308 static struct ftmac100_rxdes *ftmac100_current_rxdes(struct ftmac100 *priv)
309 {
310         return &priv->descs->rxdes[priv->rx_pointer];
311 }
312
313 static struct ftmac100_rxdes *
314 ftmac100_rx_locate_first_segment(struct ftmac100 *priv)
315 {
316         struct ftmac100_rxdes *rxdes = ftmac100_current_rxdes(priv);
317
318         while (!ftmac100_rxdes_owned_by_dma(rxdes)) {
319                 if (ftmac100_rxdes_first_segment(rxdes))
320                         return rxdes;
321
322                 ftmac100_rxdes_set_dma_own(rxdes);
323                 ftmac100_rx_pointer_advance(priv);
324                 rxdes = ftmac100_current_rxdes(priv);
325         }
326
327         return NULL;
328 }
329
330 static bool ftmac100_rx_packet_error(struct ftmac100 *priv,
331                                      struct ftmac100_rxdes *rxdes)
332 {
333         struct net_device *netdev = priv->netdev;
334         bool error = false;
335
336         if (unlikely(ftmac100_rxdes_rx_error(rxdes))) {
337                 if (net_ratelimit())
338                         netdev_info(netdev, "rx err\n");
339
340                 netdev->stats.rx_errors++;
341                 error = true;
342         }
343
344         if (unlikely(ftmac100_rxdes_crc_error(rxdes))) {
345                 if (net_ratelimit())
346                         netdev_info(netdev, "rx crc err\n");
347
348                 netdev->stats.rx_crc_errors++;
349                 error = true;
350         }
351
352         if (unlikely(ftmac100_rxdes_frame_too_long(rxdes))) {
353                 if (net_ratelimit())
354                         netdev_info(netdev, "rx frame too long\n");
355
356                 netdev->stats.rx_length_errors++;
357                 error = true;
358         } else if (unlikely(ftmac100_rxdes_runt(rxdes))) {
359                 if (net_ratelimit())
360                         netdev_info(netdev, "rx runt\n");
361
362                 netdev->stats.rx_length_errors++;
363                 error = true;
364         } else if (unlikely(ftmac100_rxdes_odd_nibble(rxdes))) {
365                 if (net_ratelimit())
366                         netdev_info(netdev, "rx odd nibble\n");
367
368                 netdev->stats.rx_length_errors++;
369                 error = true;
370         }
371
372         return error;
373 }
374
375 static void ftmac100_rx_drop_packet(struct ftmac100 *priv)
376 {
377         struct net_device *netdev = priv->netdev;
378         struct ftmac100_rxdes *rxdes = ftmac100_current_rxdes(priv);
379         bool done = false;
380
381         if (net_ratelimit())
382                 netdev_dbg(netdev, "drop packet %p\n", rxdes);
383
384         do {
385                 if (ftmac100_rxdes_last_segment(rxdes))
386                         done = true;
387
388                 ftmac100_rxdes_set_dma_own(rxdes);
389                 ftmac100_rx_pointer_advance(priv);
390                 rxdes = ftmac100_current_rxdes(priv);
391         } while (!done && !ftmac100_rxdes_owned_by_dma(rxdes));
392
393         netdev->stats.rx_dropped++;
394 }
395
396 static bool ftmac100_rx_packet(struct ftmac100 *priv, int *processed)
397 {
398         struct net_device *netdev = priv->netdev;
399         struct ftmac100_rxdes *rxdes;
400         struct sk_buff *skb;
401         struct page *page;
402         dma_addr_t map;
403         int length;
404
405         rxdes = ftmac100_rx_locate_first_segment(priv);
406         if (!rxdes)
407                 return false;
408
409         if (unlikely(ftmac100_rx_packet_error(priv, rxdes))) {
410                 ftmac100_rx_drop_packet(priv);
411                 return true;
412         }
413
414         /*
415          * It is impossible to get multi-segment packets
416          * because we always provide big enough receive buffers.
417          */
418         if (unlikely(!ftmac100_rxdes_last_segment(rxdes)))
419                 BUG();
420
421         /* start processing */
422         skb = netdev_alloc_skb_ip_align(netdev, 128);
423         if (unlikely(!skb)) {
424                 if (net_ratelimit())
425                         netdev_err(netdev, "rx skb alloc failed\n");
426
427                 ftmac100_rx_drop_packet(priv);
428                 return true;
429         }
430
431         if (unlikely(ftmac100_rxdes_multicast(rxdes)))
432                 netdev->stats.multicast++;
433
434         map = ftmac100_rxdes_get_dma_addr(rxdes);
435         dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
436
437         length = ftmac100_rxdes_frame_length(rxdes);
438         page = ftmac100_rxdes_get_page(rxdes);
439         skb_fill_page_desc(skb, 0, page, 0, length);
440         skb->len += length;
441         skb->data_len += length;
442
443         /* page might be freed in __pskb_pull_tail() */
444         if (length > 64)
445                 skb->truesize += PAGE_SIZE;
446         __pskb_pull_tail(skb, min(length, 64));
447
448         ftmac100_alloc_rx_page(priv, rxdes, GFP_ATOMIC);
449
450         ftmac100_rx_pointer_advance(priv);
451
452         skb->protocol = eth_type_trans(skb, netdev);
453
454         netdev->stats.rx_packets++;
455         netdev->stats.rx_bytes += skb->len;
456
457         /* push packet to protocol stack */
458         netif_receive_skb(skb);
459
460         (*processed)++;
461         return true;
462 }
463
464 /******************************************************************************
465  * internal functions (transmit descriptor)
466  *****************************************************************************/
467 static void ftmac100_txdes_reset(struct ftmac100_txdes *txdes)
468 {
469         /* clear all except end of ring bit */
470         txdes->txdes0 = 0;
471         txdes->txdes1 &= cpu_to_le32(FTMAC100_TXDES1_EDOTR);
472         txdes->txdes2 = 0;
473         txdes->txdes3 = 0;
474 }
475
476 static bool ftmac100_txdes_owned_by_dma(struct ftmac100_txdes *txdes)
477 {
478         return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXDMA_OWN);
479 }
480
481 static void ftmac100_txdes_set_dma_own(struct ftmac100_txdes *txdes)
482 {
483         /*
484          * Make sure dma own bit will not be set before any other
485          * descriptor fields.
486          */
487         wmb();
488         txdes->txdes0 |= cpu_to_le32(FTMAC100_TXDES0_TXDMA_OWN);
489 }
490
491 static bool ftmac100_txdes_excessive_collision(struct ftmac100_txdes *txdes)
492 {
493         return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXPKT_EXSCOL);
494 }
495
496 static bool ftmac100_txdes_late_collision(struct ftmac100_txdes *txdes)
497 {
498         return txdes->txdes0 & cpu_to_le32(FTMAC100_TXDES0_TXPKT_LATECOL);
499 }
500
501 static void ftmac100_txdes_set_end_of_ring(struct ftmac100_txdes *txdes)
502 {
503         txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_EDOTR);
504 }
505
506 static void ftmac100_txdes_set_first_segment(struct ftmac100_txdes *txdes)
507 {
508         txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_FTS);
509 }
510
511 static void ftmac100_txdes_set_last_segment(struct ftmac100_txdes *txdes)
512 {
513         txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_LTS);
514 }
515
516 static void ftmac100_txdes_set_txint(struct ftmac100_txdes *txdes)
517 {
518         txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_TXIC);
519 }
520
521 static void ftmac100_txdes_set_buffer_size(struct ftmac100_txdes *txdes,
522                                            unsigned int len)
523 {
524         txdes->txdes1 |= cpu_to_le32(FTMAC100_TXDES1_TXBUF_SIZE(len));
525 }
526
527 static void ftmac100_txdes_set_dma_addr(struct ftmac100_txdes *txdes,
528                                         dma_addr_t addr)
529 {
530         txdes->txdes2 = cpu_to_le32(addr);
531 }
532
533 static dma_addr_t ftmac100_txdes_get_dma_addr(struct ftmac100_txdes *txdes)
534 {
535         return le32_to_cpu(txdes->txdes2);
536 }
537
538 /*
539  * txdes3 is not used by hardware. We use it to keep track of socket buffer.
540  * Since hardware does not touch it, we can skip cpu_to_le32()/le32_to_cpu().
541  */
542 static void ftmac100_txdes_set_skb(struct ftmac100_txdes *txdes, struct sk_buff *skb)
543 {
544         txdes->txdes3 = (unsigned int)skb;
545 }
546
547 static struct sk_buff *ftmac100_txdes_get_skb(struct ftmac100_txdes *txdes)
548 {
549         return (struct sk_buff *)txdes->txdes3;
550 }
551
552 /******************************************************************************
553  * internal functions (transmit)
554  *****************************************************************************/
555 static int ftmac100_next_tx_pointer(int pointer)
556 {
557         return (pointer + 1) & (TX_QUEUE_ENTRIES - 1);
558 }
559
560 static void ftmac100_tx_pointer_advance(struct ftmac100 *priv)
561 {
562         priv->tx_pointer = ftmac100_next_tx_pointer(priv->tx_pointer);
563 }
564
565 static void ftmac100_tx_clean_pointer_advance(struct ftmac100 *priv)
566 {
567         priv->tx_clean_pointer = ftmac100_next_tx_pointer(priv->tx_clean_pointer);
568 }
569
570 static struct ftmac100_txdes *ftmac100_current_txdes(struct ftmac100 *priv)
571 {
572         return &priv->descs->txdes[priv->tx_pointer];
573 }
574
575 static struct ftmac100_txdes *ftmac100_current_clean_txdes(struct ftmac100 *priv)
576 {
577         return &priv->descs->txdes[priv->tx_clean_pointer];
578 }
579
580 static bool ftmac100_tx_complete_packet(struct ftmac100 *priv)
581 {
582         struct net_device *netdev = priv->netdev;
583         struct ftmac100_txdes *txdes;
584         struct sk_buff *skb;
585         dma_addr_t map;
586
587         if (priv->tx_pending == 0)
588                 return false;
589
590         txdes = ftmac100_current_clean_txdes(priv);
591
592         if (ftmac100_txdes_owned_by_dma(txdes))
593                 return false;
594
595         skb = ftmac100_txdes_get_skb(txdes);
596         map = ftmac100_txdes_get_dma_addr(txdes);
597
598         if (unlikely(ftmac100_txdes_excessive_collision(txdes) ||
599                      ftmac100_txdes_late_collision(txdes))) {
600                 /*
601                  * packet transmitted to ethernet lost due to late collision
602                  * or excessive collision
603                  */
604                 netdev->stats.tx_aborted_errors++;
605         } else {
606                 netdev->stats.tx_packets++;
607                 netdev->stats.tx_bytes += skb->len;
608         }
609
610         dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
611         dev_kfree_skb(skb);
612
613         ftmac100_txdes_reset(txdes);
614
615         ftmac100_tx_clean_pointer_advance(priv);
616
617         spin_lock(&priv->tx_lock);
618         priv->tx_pending--;
619         spin_unlock(&priv->tx_lock);
620         netif_wake_queue(netdev);
621
622         return true;
623 }
624
625 static void ftmac100_tx_complete(struct ftmac100 *priv)
626 {
627         while (ftmac100_tx_complete_packet(priv))
628                 ;
629 }
630
631 static int ftmac100_xmit(struct ftmac100 *priv, struct sk_buff *skb,
632                          dma_addr_t map)
633 {
634         struct net_device *netdev = priv->netdev;
635         struct ftmac100_txdes *txdes;
636         unsigned int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
637
638         txdes = ftmac100_current_txdes(priv);
639         ftmac100_tx_pointer_advance(priv);
640
641         /* setup TX descriptor */
642         ftmac100_txdes_set_skb(txdes, skb);
643         ftmac100_txdes_set_dma_addr(txdes, map);
644
645         ftmac100_txdes_set_first_segment(txdes);
646         ftmac100_txdes_set_last_segment(txdes);
647         ftmac100_txdes_set_txint(txdes);
648         ftmac100_txdes_set_buffer_size(txdes, len);
649
650         spin_lock(&priv->tx_lock);
651         priv->tx_pending++;
652         if (priv->tx_pending == TX_QUEUE_ENTRIES)
653                 netif_stop_queue(netdev);
654
655         /* start transmit */
656         ftmac100_txdes_set_dma_own(txdes);
657         spin_unlock(&priv->tx_lock);
658
659         ftmac100_txdma_start_polling(priv);
660         return NETDEV_TX_OK;
661 }
662
663 /******************************************************************************
664  * internal functions (buffer)
665  *****************************************************************************/
666 static int ftmac100_alloc_rx_page(struct ftmac100 *priv,
667                                   struct ftmac100_rxdes *rxdes, gfp_t gfp)
668 {
669         struct net_device *netdev = priv->netdev;
670         struct page *page;
671         dma_addr_t map;
672
673         page = alloc_page(gfp);
674         if (!page) {
675                 if (net_ratelimit())
676                         netdev_err(netdev, "failed to allocate rx page\n");
677                 return -ENOMEM;
678         }
679
680         map = dma_map_page(priv->dev, page, 0, RX_BUF_SIZE, DMA_FROM_DEVICE);
681         if (unlikely(dma_mapping_error(priv->dev, map))) {
682                 if (net_ratelimit())
683                         netdev_err(netdev, "failed to map rx page\n");
684                 __free_page(page);
685                 return -ENOMEM;
686         }
687
688         ftmac100_rxdes_set_page(rxdes, page);
689         ftmac100_rxdes_set_dma_addr(rxdes, map);
690         ftmac100_rxdes_set_buffer_size(rxdes, RX_BUF_SIZE);
691         ftmac100_rxdes_set_dma_own(rxdes);
692         return 0;
693 }
694
695 static void ftmac100_free_buffers(struct ftmac100 *priv)
696 {
697         int i;
698
699         for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
700                 struct ftmac100_rxdes *rxdes = &priv->descs->rxdes[i];
701                 struct page *page = ftmac100_rxdes_get_page(rxdes);
702                 dma_addr_t map = ftmac100_rxdes_get_dma_addr(rxdes);
703
704                 if (!page)
705                         continue;
706
707                 dma_unmap_page(priv->dev, map, RX_BUF_SIZE, DMA_FROM_DEVICE);
708                 __free_page(page);
709         }
710
711         for (i = 0; i < TX_QUEUE_ENTRIES; i++) {
712                 struct ftmac100_txdes *txdes = &priv->descs->txdes[i];
713                 struct sk_buff *skb = ftmac100_txdes_get_skb(txdes);
714                 dma_addr_t map = ftmac100_txdes_get_dma_addr(txdes);
715
716                 if (!skb)
717                         continue;
718
719                 dma_unmap_single(priv->dev, map, skb_headlen(skb), DMA_TO_DEVICE);
720                 dev_kfree_skb(skb);
721         }
722
723         dma_free_coherent(priv->dev, sizeof(struct ftmac100_descs),
724                           priv->descs, priv->descs_dma_addr);
725 }
726
727 static int ftmac100_alloc_buffers(struct ftmac100 *priv)
728 {
729         int i;
730
731         priv->descs = dma_alloc_coherent(priv->dev, sizeof(struct ftmac100_descs),
732                                          &priv->descs_dma_addr, GFP_KERNEL);
733         if (!priv->descs)
734                 return -ENOMEM;
735
736         memset(priv->descs, 0, sizeof(struct ftmac100_descs));
737
738         /* initialize RX ring */
739         ftmac100_rxdes_set_end_of_ring(&priv->descs->rxdes[RX_QUEUE_ENTRIES - 1]);
740
741         for (i = 0; i < RX_QUEUE_ENTRIES; i++) {
742                 struct ftmac100_rxdes *rxdes = &priv->descs->rxdes[i];
743
744                 if (ftmac100_alloc_rx_page(priv, rxdes, GFP_KERNEL))
745                         goto err;
746         }
747
748         /* initialize TX ring */
749         ftmac100_txdes_set_end_of_ring(&priv->descs->txdes[TX_QUEUE_ENTRIES - 1]);
750         return 0;
751
752 err:
753         ftmac100_free_buffers(priv);
754         return -ENOMEM;
755 }
756
757 /******************************************************************************
758  * struct mii_if_info functions
759  *****************************************************************************/
760 static int ftmac100_mdio_read(struct net_device *netdev, int phy_id, int reg)
761 {
762         struct ftmac100 *priv = netdev_priv(netdev);
763         unsigned int phycr;
764         int i;
765
766         phycr = FTMAC100_PHYCR_PHYAD(phy_id) |
767                 FTMAC100_PHYCR_REGAD(reg) |
768                 FTMAC100_PHYCR_MIIRD;
769
770         iowrite32(phycr, priv->base + FTMAC100_OFFSET_PHYCR);
771
772         for (i = 0; i < 10; i++) {
773                 phycr = ioread32(priv->base + FTMAC100_OFFSET_PHYCR);
774
775                 if ((phycr & FTMAC100_PHYCR_MIIRD) == 0)
776                         return phycr & FTMAC100_PHYCR_MIIRDATA;
777
778                 udelay(100);
779         }
780
781         netdev_err(netdev, "mdio read timed out\n");
782         return 0;
783 }
784
785 static void ftmac100_mdio_write(struct net_device *netdev, int phy_id, int reg,
786                                 int data)
787 {
788         struct ftmac100 *priv = netdev_priv(netdev);
789         unsigned int phycr;
790         int i;
791
792         phycr = FTMAC100_PHYCR_PHYAD(phy_id) |
793                 FTMAC100_PHYCR_REGAD(reg) |
794                 FTMAC100_PHYCR_MIIWR;
795
796         data = FTMAC100_PHYWDATA_MIIWDATA(data);
797
798         iowrite32(data, priv->base + FTMAC100_OFFSET_PHYWDATA);
799         iowrite32(phycr, priv->base + FTMAC100_OFFSET_PHYCR);
800
801         for (i = 0; i < 10; i++) {
802                 phycr = ioread32(priv->base + FTMAC100_OFFSET_PHYCR);
803
804                 if ((phycr & FTMAC100_PHYCR_MIIWR) == 0)
805                         return;
806
807                 udelay(100);
808         }
809
810         netdev_err(netdev, "mdio write timed out\n");
811 }
812
813 /******************************************************************************
814  * struct ethtool_ops functions
815  *****************************************************************************/
816 static void ftmac100_get_drvinfo(struct net_device *netdev,
817                                  struct ethtool_drvinfo *info)
818 {
819         strcpy(info->driver, DRV_NAME);
820         strcpy(info->version, DRV_VERSION);
821         strcpy(info->bus_info, dev_name(&netdev->dev));
822 }
823
824 static int ftmac100_get_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
825 {
826         struct ftmac100 *priv = netdev_priv(netdev);
827         return mii_ethtool_gset(&priv->mii, cmd);
828 }
829
830 static int ftmac100_set_settings(struct net_device *netdev, struct ethtool_cmd *cmd)
831 {
832         struct ftmac100 *priv = netdev_priv(netdev);
833         return mii_ethtool_sset(&priv->mii, cmd);
834 }
835
836 static int ftmac100_nway_reset(struct net_device *netdev)
837 {
838         struct ftmac100 *priv = netdev_priv(netdev);
839         return mii_nway_restart(&priv->mii);
840 }
841
842 static u32 ftmac100_get_link(struct net_device *netdev)
843 {
844         struct ftmac100 *priv = netdev_priv(netdev);
845         return mii_link_ok(&priv->mii);
846 }
847
848 static const struct ethtool_ops ftmac100_ethtool_ops = {
849         .set_settings           = ftmac100_set_settings,
850         .get_settings           = ftmac100_get_settings,
851         .get_drvinfo            = ftmac100_get_drvinfo,
852         .nway_reset             = ftmac100_nway_reset,
853         .get_link               = ftmac100_get_link,
854 };
855
856 /******************************************************************************
857  * interrupt handler
858  *****************************************************************************/
859 static irqreturn_t ftmac100_interrupt(int irq, void *dev_id)
860 {
861         struct net_device *netdev = dev_id;
862         struct ftmac100 *priv = netdev_priv(netdev);
863
864         if (likely(netif_running(netdev))) {
865                 /* Disable interrupts for polling */
866                 ftmac100_disable_all_int(priv);
867                 napi_schedule(&priv->napi);
868         }
869
870         return IRQ_HANDLED;
871 }
872
873 /******************************************************************************
874  * struct napi_struct functions
875  *****************************************************************************/
876 static int ftmac100_poll(struct napi_struct *napi, int budget)
877 {
878         struct ftmac100 *priv = container_of(napi, struct ftmac100, napi);
879         struct net_device *netdev = priv->netdev;
880         unsigned int status;
881         bool completed = true;
882         int rx = 0;
883
884         status = ioread32(priv->base + FTMAC100_OFFSET_ISR);
885
886         if (status & (FTMAC100_INT_RPKT_FINISH | FTMAC100_INT_NORXBUF)) {
887                 /*
888                  * FTMAC100_INT_RPKT_FINISH:
889                  *      RX DMA has received packets into RX buffer successfully
890                  *
891                  * FTMAC100_INT_NORXBUF:
892                  *      RX buffer unavailable
893                  */
894                 bool retry;
895
896                 do {
897                         retry = ftmac100_rx_packet(priv, &rx);
898                 } while (retry && rx < budget);
899
900                 if (retry && rx == budget)
901                         completed = false;
902         }
903
904         if (status & (FTMAC100_INT_XPKT_OK | FTMAC100_INT_XPKT_LOST)) {
905                 /*
906                  * FTMAC100_INT_XPKT_OK:
907                  *      packet transmitted to ethernet successfully
908                  *
909                  * FTMAC100_INT_XPKT_LOST:
910                  *      packet transmitted to ethernet lost due to late
911                  *      collision or excessive collision
912                  */
913                 ftmac100_tx_complete(priv);
914         }
915
916         if (status & (FTMAC100_INT_NORXBUF | FTMAC100_INT_RPKT_LOST |
917                       FTMAC100_INT_AHB_ERR | FTMAC100_INT_PHYSTS_CHG)) {
918                 if (net_ratelimit())
919                         netdev_info(netdev, "[ISR] = 0x%x: %s%s%s%s\n", status,
920                                     status & FTMAC100_INT_NORXBUF ? "NORXBUF " : "",
921                                     status & FTMAC100_INT_RPKT_LOST ? "RPKT_LOST " : "",
922                                     status & FTMAC100_INT_AHB_ERR ? "AHB_ERR " : "",
923                                     status & FTMAC100_INT_PHYSTS_CHG ? "PHYSTS_CHG" : "");
924
925                 if (status & FTMAC100_INT_NORXBUF) {
926                         /* RX buffer unavailable */
927                         netdev->stats.rx_over_errors++;
928                 }
929
930                 if (status & FTMAC100_INT_RPKT_LOST) {
931                         /* received packet lost due to RX FIFO full */
932                         netdev->stats.rx_fifo_errors++;
933                 }
934
935                 if (status & FTMAC100_INT_PHYSTS_CHG) {
936                         /* PHY link status change */
937                         mii_check_link(&priv->mii);
938                 }
939         }
940
941         if (completed) {
942                 /* stop polling */
943                 napi_complete(napi);
944                 ftmac100_enable_all_int(priv);
945         }
946
947         return rx;
948 }
949
950 /******************************************************************************
951  * struct net_device_ops functions
952  *****************************************************************************/
953 static int ftmac100_open(struct net_device *netdev)
954 {
955         struct ftmac100 *priv = netdev_priv(netdev);
956         int err;
957
958         err = ftmac100_alloc_buffers(priv);
959         if (err) {
960                 netdev_err(netdev, "failed to allocate buffers\n");
961                 goto err_alloc;
962         }
963
964         err = request_irq(priv->irq, ftmac100_interrupt, 0, netdev->name, netdev);
965         if (err) {
966                 netdev_err(netdev, "failed to request irq %d\n", priv->irq);
967                 goto err_irq;
968         }
969
970         priv->rx_pointer = 0;
971         priv->tx_clean_pointer = 0;
972         priv->tx_pointer = 0;
973         priv->tx_pending = 0;
974
975         err = ftmac100_start_hw(priv);
976         if (err)
977                 goto err_hw;
978
979         napi_enable(&priv->napi);
980         netif_start_queue(netdev);
981
982         ftmac100_enable_all_int(priv);
983
984         return 0;
985
986 err_hw:
987         free_irq(priv->irq, netdev);
988 err_irq:
989         ftmac100_free_buffers(priv);
990 err_alloc:
991         return err;
992 }
993
994 static int ftmac100_stop(struct net_device *netdev)
995 {
996         struct ftmac100 *priv = netdev_priv(netdev);
997
998         ftmac100_disable_all_int(priv);
999         netif_stop_queue(netdev);
1000         napi_disable(&priv->napi);
1001         ftmac100_stop_hw(priv);
1002         free_irq(priv->irq, netdev);
1003         ftmac100_free_buffers(priv);
1004
1005         return 0;
1006 }
1007
1008 static int ftmac100_hard_start_xmit(struct sk_buff *skb, struct net_device *netdev)
1009 {
1010         struct ftmac100 *priv = netdev_priv(netdev);
1011         dma_addr_t map;
1012
1013         if (unlikely(skb->len > MAX_PKT_SIZE)) {
1014                 if (net_ratelimit())
1015                         netdev_dbg(netdev, "tx packet too big\n");
1016
1017                 netdev->stats.tx_dropped++;
1018                 dev_kfree_skb(skb);
1019                 return NETDEV_TX_OK;
1020         }
1021
1022         map = dma_map_single(priv->dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
1023         if (unlikely(dma_mapping_error(priv->dev, map))) {
1024                 /* drop packet */
1025                 if (net_ratelimit())
1026                         netdev_err(netdev, "map socket buffer failed\n");
1027
1028                 netdev->stats.tx_dropped++;
1029                 dev_kfree_skb(skb);
1030                 return NETDEV_TX_OK;
1031         }
1032
1033         return ftmac100_xmit(priv, skb, map);
1034 }
1035
1036 /* optional */
1037 static int ftmac100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
1038 {
1039         struct ftmac100 *priv = netdev_priv(netdev);
1040         struct mii_ioctl_data *data = if_mii(ifr);
1041
1042         return generic_mii_ioctl(&priv->mii, data, cmd, NULL);
1043 }
1044
1045 static const struct net_device_ops ftmac100_netdev_ops = {
1046         .ndo_open               = ftmac100_open,
1047         .ndo_stop               = ftmac100_stop,
1048         .ndo_start_xmit         = ftmac100_hard_start_xmit,
1049         .ndo_set_mac_address    = eth_mac_addr,
1050         .ndo_validate_addr      = eth_validate_addr,
1051         .ndo_do_ioctl           = ftmac100_do_ioctl,
1052 };
1053
1054 /******************************************************************************
1055  * struct platform_driver functions
1056  *****************************************************************************/
1057 static int ftmac100_probe(struct platform_device *pdev)
1058 {
1059         struct resource *res;
1060         int irq;
1061         struct net_device *netdev;
1062         struct ftmac100 *priv;
1063         int err;
1064
1065         if (!pdev)
1066                 return -ENODEV;
1067
1068         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1069         if (!res)
1070                 return -ENXIO;
1071
1072         irq = platform_get_irq(pdev, 0);
1073         if (irq < 0)
1074                 return irq;
1075
1076         /* setup net_device */
1077         netdev = alloc_etherdev(sizeof(*priv));
1078         if (!netdev) {
1079                 err = -ENOMEM;
1080                 goto err_alloc_etherdev;
1081         }
1082
1083         SET_NETDEV_DEV(netdev, &pdev->dev);
1084         SET_ETHTOOL_OPS(netdev, &ftmac100_ethtool_ops);
1085         netdev->netdev_ops = &ftmac100_netdev_ops;
1086
1087         platform_set_drvdata(pdev, netdev);
1088
1089         /* setup private data */
1090         priv = netdev_priv(netdev);
1091         priv->netdev = netdev;
1092         priv->dev = &pdev->dev;
1093
1094         spin_lock_init(&priv->tx_lock);
1095
1096         /* initialize NAPI */
1097         netif_napi_add(netdev, &priv->napi, ftmac100_poll, 64);
1098
1099         /* map io memory */
1100         priv->res = request_mem_region(res->start, resource_size(res),
1101                                        dev_name(&pdev->dev));
1102         if (!priv->res) {
1103                 dev_err(&pdev->dev, "Could not reserve memory region\n");
1104                 err = -ENOMEM;
1105                 goto err_req_mem;
1106         }
1107
1108         priv->base = ioremap(res->start, resource_size(res));
1109         if (!priv->base) {
1110                 dev_err(&pdev->dev, "Failed to ioremap ethernet registers\n");
1111                 err = -EIO;
1112                 goto err_ioremap;
1113         }
1114
1115         priv->irq = irq;
1116
1117         /* initialize struct mii_if_info */
1118         priv->mii.phy_id        = 0;
1119         priv->mii.phy_id_mask   = 0x1f;
1120         priv->mii.reg_num_mask  = 0x1f;
1121         priv->mii.dev           = netdev;
1122         priv->mii.mdio_read     = ftmac100_mdio_read;
1123         priv->mii.mdio_write    = ftmac100_mdio_write;
1124
1125         /* register network device */
1126         err = register_netdev(netdev);
1127         if (err) {
1128                 dev_err(&pdev->dev, "Failed to register netdev\n");
1129                 goto err_register_netdev;
1130         }
1131
1132         netdev_info(netdev, "irq %d, mapped at %p\n", priv->irq, priv->base);
1133
1134         if (!is_valid_ether_addr(netdev->dev_addr)) {
1135                 random_ether_addr(netdev->dev_addr);
1136                 netdev_info(netdev, "generated random MAC address %pM\n",
1137                             netdev->dev_addr);
1138         }
1139
1140         return 0;
1141
1142 err_register_netdev:
1143         iounmap(priv->base);
1144 err_ioremap:
1145         release_resource(priv->res);
1146 err_req_mem:
1147         netif_napi_del(&priv->napi);
1148         platform_set_drvdata(pdev, NULL);
1149         free_netdev(netdev);
1150 err_alloc_etherdev:
1151         return err;
1152 }
1153
1154 static int __exit ftmac100_remove(struct platform_device *pdev)
1155 {
1156         struct net_device *netdev;
1157         struct ftmac100 *priv;
1158
1159         netdev = platform_get_drvdata(pdev);
1160         priv = netdev_priv(netdev);
1161
1162         unregister_netdev(netdev);
1163
1164         iounmap(priv->base);
1165         release_resource(priv->res);
1166
1167         netif_napi_del(&priv->napi);
1168         platform_set_drvdata(pdev, NULL);
1169         free_netdev(netdev);
1170         return 0;
1171 }
1172
1173 static struct platform_driver ftmac100_driver = {
1174         .probe          = ftmac100_probe,
1175         .remove         = __exit_p(ftmac100_remove),
1176         .driver         = {
1177                 .name   = DRV_NAME,
1178                 .owner  = THIS_MODULE,
1179         },
1180 };
1181
1182 /******************************************************************************
1183  * initialization / finalization
1184  *****************************************************************************/
1185 static int __init ftmac100_init(void)
1186 {
1187         pr_info("Loading version " DRV_VERSION " ...\n");
1188         return platform_driver_register(&ftmac100_driver);
1189 }
1190
1191 static void __exit ftmac100_exit(void)
1192 {
1193         platform_driver_unregister(&ftmac100_driver);
1194 }
1195
1196 module_init(ftmac100_init);
1197 module_exit(ftmac100_exit);
1198
1199 MODULE_AUTHOR("Po-Yu Chuang <ratbert@faraday-tech.com>");
1200 MODULE_DESCRIPTION("FTMAC100 driver");
1201 MODULE_LICENSE("GPL");