GNU Linux-libre 4.9.318-gnu1
[releases.git] / drivers / net / ethernet / hisilicon / hip04_eth.c
1
2 /* Copyright (c) 2014 Linaro Ltd.
3  * Copyright (c) 2014 Hisilicon Limited.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  */
10
11 #include <linux/module.h>
12 #include <linux/etherdevice.h>
13 #include <linux/platform_device.h>
14 #include <linux/interrupt.h>
15 #include <linux/ktime.h>
16 #include <linux/of_address.h>
17 #include <linux/phy.h>
18 #include <linux/of_mdio.h>
19 #include <linux/of_net.h>
20 #include <linux/mfd/syscon.h>
21 #include <linux/regmap.h>
22
23 #define PPE_CFG_RX_ADDR                 0x100
24 #define PPE_CFG_POOL_GRP                0x300
25 #define PPE_CFG_RX_BUF_SIZE             0x400
26 #define PPE_CFG_RX_FIFO_SIZE            0x500
27 #define PPE_CURR_BUF_CNT                0xa200
28
29 #define GE_DUPLEX_TYPE                  0x08
30 #define GE_MAX_FRM_SIZE_REG             0x3c
31 #define GE_PORT_MODE                    0x40
32 #define GE_PORT_EN                      0x44
33 #define GE_SHORT_RUNTS_THR_REG          0x50
34 #define GE_TX_LOCAL_PAGE_REG            0x5c
35 #define GE_TRANSMIT_CONTROL_REG         0x60
36 #define GE_CF_CRC_STRIP_REG             0x1b0
37 #define GE_MODE_CHANGE_REG              0x1b4
38 #define GE_RECV_CONTROL_REG             0x1e0
39 #define GE_STATION_MAC_ADDRESS          0x210
40 #define PPE_CFG_CPU_ADD_ADDR            0x580
41 #define PPE_CFG_MAX_FRAME_LEN_REG       0x408
42 #define PPE_CFG_BUS_CTRL_REG            0x424
43 #define PPE_CFG_RX_CTRL_REG             0x428
44 #define PPE_CFG_RX_PKT_MODE_REG         0x438
45 #define PPE_CFG_QOS_VMID_GEN            0x500
46 #define PPE_CFG_RX_PKT_INT              0x538
47 #define PPE_INTEN                       0x600
48 #define PPE_INTSTS                      0x608
49 #define PPE_RINT                        0x604
50 #define PPE_CFG_STS_MODE                0x700
51 #define PPE_HIS_RX_PKT_CNT              0x804
52
53 /* REG_INTERRUPT */
54 #define RCV_INT                         BIT(10)
55 #define RCV_NOBUF                       BIT(8)
56 #define RCV_DROP                        BIT(7)
57 #define TX_DROP                         BIT(6)
58 #define DEF_INT_ERR                     (RCV_NOBUF | RCV_DROP | TX_DROP)
59 #define DEF_INT_MASK                    (RCV_INT | DEF_INT_ERR)
60
61 /* TX descriptor config */
62 #define TX_FREE_MEM                     BIT(0)
63 #define TX_READ_ALLOC_L3                BIT(1)
64 #define TX_FINISH_CACHE_INV             BIT(2)
65 #define TX_CLEAR_WB                     BIT(4)
66 #define TX_L3_CHECKSUM                  BIT(5)
67 #define TX_LOOP_BACK                    BIT(11)
68
69 /* RX error */
70 #define RX_PKT_DROP                     BIT(0)
71 #define RX_L2_ERR                       BIT(1)
72 #define RX_PKT_ERR                      (RX_PKT_DROP | RX_L2_ERR)
73
74 #define SGMII_SPEED_1000                0x08
75 #define SGMII_SPEED_100                 0x07
76 #define SGMII_SPEED_10                  0x06
77 #define MII_SPEED_100                   0x01
78 #define MII_SPEED_10                    0x00
79
80 #define GE_DUPLEX_FULL                  BIT(0)
81 #define GE_DUPLEX_HALF                  0x00
82 #define GE_MODE_CHANGE_EN               BIT(0)
83
84 #define GE_TX_AUTO_NEG                  BIT(5)
85 #define GE_TX_ADD_CRC                   BIT(6)
86 #define GE_TX_SHORT_PAD_THROUGH         BIT(7)
87
88 #define GE_RX_STRIP_CRC                 BIT(0)
89 #define GE_RX_STRIP_PAD                 BIT(3)
90 #define GE_RX_PAD_EN                    BIT(4)
91
92 #define GE_AUTO_NEG_CTL                 BIT(0)
93
94 #define GE_RX_INT_THRESHOLD             BIT(6)
95 #define GE_RX_TIMEOUT                   0x04
96
97 #define GE_RX_PORT_EN                   BIT(1)
98 #define GE_TX_PORT_EN                   BIT(2)
99
100 #define PPE_CFG_STS_RX_PKT_CNT_RC       BIT(12)
101
102 #define PPE_CFG_RX_PKT_ALIGN            BIT(18)
103 #define PPE_CFG_QOS_VMID_MODE           BIT(14)
104 #define PPE_CFG_QOS_VMID_GRP_SHIFT      8
105
106 #define PPE_CFG_RX_FIFO_FSFU            BIT(11)
107 #define PPE_CFG_RX_DEPTH_SHIFT          16
108 #define PPE_CFG_RX_START_SHIFT          0
109 #define PPE_CFG_RX_CTRL_ALIGN_SHIFT     11
110
111 #define PPE_CFG_BUS_LOCAL_REL           BIT(14)
112 #define PPE_CFG_BUS_BIG_ENDIEN          BIT(0)
113
114 #define RX_DESC_NUM                     128
115 #define TX_DESC_NUM                     256
116 #define TX_NEXT(N)                      (((N) + 1) & (TX_DESC_NUM-1))
117 #define RX_NEXT(N)                      (((N) + 1) & (RX_DESC_NUM-1))
118
119 #define GMAC_PPE_RX_PKT_MAX_LEN         379
120 #define GMAC_MAX_PKT_LEN                1516
121 #define GMAC_MIN_PKT_LEN                31
122 #define RX_BUF_SIZE                     1600
123 #define RESET_TIMEOUT                   1000
124 #define TX_TIMEOUT                      (6 * HZ)
125
126 #define DRV_NAME                        "hip04-ether"
127 #define DRV_VERSION                     "v1.0"
128
129 #define HIP04_MAX_TX_COALESCE_USECS     200
130 #define HIP04_MIN_TX_COALESCE_USECS     100
131 #define HIP04_MAX_TX_COALESCE_FRAMES    200
132 #define HIP04_MIN_TX_COALESCE_FRAMES    100
133
134 struct tx_desc {
135         u32 send_addr;
136         u32 send_size;
137         u32 next_addr;
138         u32 cfg;
139         u32 wb_addr;
140 } __aligned(64);
141
142 struct rx_desc {
143         u16 reserved_16;
144         u16 pkt_len;
145         u32 reserve1[3];
146         u32 pkt_err;
147         u32 reserve2[4];
148 };
149
150 struct hip04_priv {
151         void __iomem *base;
152         int phy_mode;
153         int chan;
154         unsigned int port;
155         unsigned int speed;
156         unsigned int duplex;
157         unsigned int reg_inten;
158
159         struct napi_struct napi;
160         struct device *dev;
161         struct net_device *ndev;
162
163         struct tx_desc *tx_desc;
164         dma_addr_t tx_desc_dma;
165         struct sk_buff *tx_skb[TX_DESC_NUM];
166         dma_addr_t tx_phys[TX_DESC_NUM];
167         unsigned int tx_head;
168
169         int tx_coalesce_frames;
170         int tx_coalesce_usecs;
171         struct hrtimer tx_coalesce_timer;
172
173         unsigned char *rx_buf[RX_DESC_NUM];
174         dma_addr_t rx_phys[RX_DESC_NUM];
175         unsigned int rx_head;
176         unsigned int rx_buf_size;
177         unsigned int rx_cnt_remaining;
178
179         struct device_node *phy_node;
180         struct phy_device *phy;
181         struct regmap *map;
182         struct work_struct tx_timeout_task;
183
184         /* written only by tx cleanup */
185         unsigned int tx_tail ____cacheline_aligned_in_smp;
186 };
187
188 static inline unsigned int tx_count(unsigned int head, unsigned int tail)
189 {
190         return (head - tail) % TX_DESC_NUM;
191 }
192
193 static void hip04_config_port(struct net_device *ndev, u32 speed, u32 duplex)
194 {
195         struct hip04_priv *priv = netdev_priv(ndev);
196         u32 val;
197
198         priv->speed = speed;
199         priv->duplex = duplex;
200
201         switch (priv->phy_mode) {
202         case PHY_INTERFACE_MODE_SGMII:
203                 if (speed == SPEED_1000)
204                         val = SGMII_SPEED_1000;
205                 else if (speed == SPEED_100)
206                         val = SGMII_SPEED_100;
207                 else
208                         val = SGMII_SPEED_10;
209                 break;
210         case PHY_INTERFACE_MODE_MII:
211                 if (speed == SPEED_100)
212                         val = MII_SPEED_100;
213                 else
214                         val = MII_SPEED_10;
215                 break;
216         default:
217                 netdev_warn(ndev, "not supported mode\n");
218                 val = MII_SPEED_10;
219                 break;
220         }
221         writel_relaxed(val, priv->base + GE_PORT_MODE);
222
223         val = duplex ? GE_DUPLEX_FULL : GE_DUPLEX_HALF;
224         writel_relaxed(val, priv->base + GE_DUPLEX_TYPE);
225
226         val = GE_MODE_CHANGE_EN;
227         writel_relaxed(val, priv->base + GE_MODE_CHANGE_REG);
228 }
229
230 static void hip04_reset_ppe(struct hip04_priv *priv)
231 {
232         u32 val, tmp, timeout = 0;
233
234         do {
235                 regmap_read(priv->map, priv->port * 4 + PPE_CURR_BUF_CNT, &val);
236                 regmap_read(priv->map, priv->port * 4 + PPE_CFG_RX_ADDR, &tmp);
237                 if (timeout++ > RESET_TIMEOUT)
238                         break;
239         } while (val & 0xfff);
240 }
241
242 static void hip04_config_fifo(struct hip04_priv *priv)
243 {
244         u32 val;
245
246         val = readl_relaxed(priv->base + PPE_CFG_STS_MODE);
247         val |= PPE_CFG_STS_RX_PKT_CNT_RC;
248         writel_relaxed(val, priv->base + PPE_CFG_STS_MODE);
249
250         val = BIT(priv->port);
251         regmap_write(priv->map, priv->port * 4 + PPE_CFG_POOL_GRP, val);
252
253         val = priv->port << PPE_CFG_QOS_VMID_GRP_SHIFT;
254         val |= PPE_CFG_QOS_VMID_MODE;
255         writel_relaxed(val, priv->base + PPE_CFG_QOS_VMID_GEN);
256
257         val = RX_BUF_SIZE;
258         regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_BUF_SIZE, val);
259
260         val = RX_DESC_NUM << PPE_CFG_RX_DEPTH_SHIFT;
261         val |= PPE_CFG_RX_FIFO_FSFU;
262         val |= priv->chan << PPE_CFG_RX_START_SHIFT;
263         regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_FIFO_SIZE, val);
264
265         val = NET_IP_ALIGN << PPE_CFG_RX_CTRL_ALIGN_SHIFT;
266         writel_relaxed(val, priv->base + PPE_CFG_RX_CTRL_REG);
267
268         val = PPE_CFG_RX_PKT_ALIGN;
269         writel_relaxed(val, priv->base + PPE_CFG_RX_PKT_MODE_REG);
270
271         val = PPE_CFG_BUS_LOCAL_REL | PPE_CFG_BUS_BIG_ENDIEN;
272         writel_relaxed(val, priv->base + PPE_CFG_BUS_CTRL_REG);
273
274         val = GMAC_PPE_RX_PKT_MAX_LEN;
275         writel_relaxed(val, priv->base + PPE_CFG_MAX_FRAME_LEN_REG);
276
277         val = GMAC_MAX_PKT_LEN;
278         writel_relaxed(val, priv->base + GE_MAX_FRM_SIZE_REG);
279
280         val = GMAC_MIN_PKT_LEN;
281         writel_relaxed(val, priv->base + GE_SHORT_RUNTS_THR_REG);
282
283         val = readl_relaxed(priv->base + GE_TRANSMIT_CONTROL_REG);
284         val |= GE_TX_AUTO_NEG | GE_TX_ADD_CRC | GE_TX_SHORT_PAD_THROUGH;
285         writel_relaxed(val, priv->base + GE_TRANSMIT_CONTROL_REG);
286
287         val = GE_RX_STRIP_CRC;
288         writel_relaxed(val, priv->base + GE_CF_CRC_STRIP_REG);
289
290         val = readl_relaxed(priv->base + GE_RECV_CONTROL_REG);
291         val |= GE_RX_STRIP_PAD | GE_RX_PAD_EN;
292         writel_relaxed(val, priv->base + GE_RECV_CONTROL_REG);
293
294         val = GE_AUTO_NEG_CTL;
295         writel_relaxed(val, priv->base + GE_TX_LOCAL_PAGE_REG);
296 }
297
298 static void hip04_mac_enable(struct net_device *ndev)
299 {
300         struct hip04_priv *priv = netdev_priv(ndev);
301         u32 val;
302
303         /* enable tx & rx */
304         val = readl_relaxed(priv->base + GE_PORT_EN);
305         val |= GE_RX_PORT_EN | GE_TX_PORT_EN;
306         writel_relaxed(val, priv->base + GE_PORT_EN);
307
308         /* clear rx int */
309         val = RCV_INT;
310         writel_relaxed(val, priv->base + PPE_RINT);
311
312         /* config recv int */
313         val = GE_RX_INT_THRESHOLD | GE_RX_TIMEOUT;
314         writel_relaxed(val, priv->base + PPE_CFG_RX_PKT_INT);
315
316         /* enable interrupt */
317         priv->reg_inten = DEF_INT_MASK;
318         writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
319 }
320
321 static void hip04_mac_disable(struct net_device *ndev)
322 {
323         struct hip04_priv *priv = netdev_priv(ndev);
324         u32 val;
325
326         /* disable int */
327         priv->reg_inten &= ~(DEF_INT_MASK);
328         writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
329
330         /* disable tx & rx */
331         val = readl_relaxed(priv->base + GE_PORT_EN);
332         val &= ~(GE_RX_PORT_EN | GE_TX_PORT_EN);
333         writel_relaxed(val, priv->base + GE_PORT_EN);
334 }
335
336 static void hip04_set_xmit_desc(struct hip04_priv *priv, dma_addr_t phys)
337 {
338         writel(phys, priv->base + PPE_CFG_CPU_ADD_ADDR);
339 }
340
341 static void hip04_set_recv_desc(struct hip04_priv *priv, dma_addr_t phys)
342 {
343         regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_ADDR, phys);
344 }
345
346 static u32 hip04_recv_cnt(struct hip04_priv *priv)
347 {
348         return readl(priv->base + PPE_HIS_RX_PKT_CNT);
349 }
350
351 static void hip04_update_mac_address(struct net_device *ndev)
352 {
353         struct hip04_priv *priv = netdev_priv(ndev);
354
355         writel_relaxed(((ndev->dev_addr[0] << 8) | (ndev->dev_addr[1])),
356                        priv->base + GE_STATION_MAC_ADDRESS);
357         writel_relaxed(((ndev->dev_addr[2] << 24) | (ndev->dev_addr[3] << 16) |
358                         (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5])),
359                        priv->base + GE_STATION_MAC_ADDRESS + 4);
360 }
361
362 static int hip04_set_mac_address(struct net_device *ndev, void *addr)
363 {
364         eth_mac_addr(ndev, addr);
365         hip04_update_mac_address(ndev);
366         return 0;
367 }
368
369 static int hip04_tx_reclaim(struct net_device *ndev, bool force)
370 {
371         struct hip04_priv *priv = netdev_priv(ndev);
372         unsigned tx_tail = priv->tx_tail;
373         struct tx_desc *desc;
374         unsigned int bytes_compl = 0, pkts_compl = 0;
375         unsigned int count;
376
377         smp_rmb();
378         count = tx_count(ACCESS_ONCE(priv->tx_head), tx_tail);
379         if (count == 0)
380                 goto out;
381
382         while (count) {
383                 desc = &priv->tx_desc[tx_tail];
384                 if (desc->send_addr != 0) {
385                         if (force)
386                                 desc->send_addr = 0;
387                         else
388                                 break;
389                 }
390
391                 if (priv->tx_phys[tx_tail]) {
392                         dma_unmap_single(priv->dev, priv->tx_phys[tx_tail],
393                                          priv->tx_skb[tx_tail]->len,
394                                          DMA_TO_DEVICE);
395                         priv->tx_phys[tx_tail] = 0;
396                 }
397                 pkts_compl++;
398                 bytes_compl += priv->tx_skb[tx_tail]->len;
399                 dev_kfree_skb(priv->tx_skb[tx_tail]);
400                 priv->tx_skb[tx_tail] = NULL;
401                 tx_tail = TX_NEXT(tx_tail);
402                 count--;
403         }
404
405         priv->tx_tail = tx_tail;
406         smp_wmb(); /* Ensure tx_tail visible to xmit */
407
408 out:
409         if (pkts_compl || bytes_compl)
410                 netdev_completed_queue(ndev, pkts_compl, bytes_compl);
411
412         if (unlikely(netif_queue_stopped(ndev)) && (count < (TX_DESC_NUM - 1)))
413                 netif_wake_queue(ndev);
414
415         return count;
416 }
417
418 static void hip04_start_tx_timer(struct hip04_priv *priv)
419 {
420         unsigned long ns = priv->tx_coalesce_usecs * NSEC_PER_USEC / 2;
421
422         /* allow timer to fire after half the time at the earliest */
423         hrtimer_start_range_ns(&priv->tx_coalesce_timer, ns_to_ktime(ns),
424                                ns, HRTIMER_MODE_REL);
425 }
426
427 static int hip04_mac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
428 {
429         struct hip04_priv *priv = netdev_priv(ndev);
430         struct net_device_stats *stats = &ndev->stats;
431         unsigned int tx_head = priv->tx_head, count;
432         struct tx_desc *desc = &priv->tx_desc[tx_head];
433         dma_addr_t phys;
434
435         smp_rmb();
436         count = tx_count(tx_head, ACCESS_ONCE(priv->tx_tail));
437         if (count == (TX_DESC_NUM - 1)) {
438                 netif_stop_queue(ndev);
439                 return NETDEV_TX_BUSY;
440         }
441
442         phys = dma_map_single(priv->dev, skb->data, skb->len, DMA_TO_DEVICE);
443         if (dma_mapping_error(priv->dev, phys)) {
444                 dev_kfree_skb(skb);
445                 return NETDEV_TX_OK;
446         }
447
448         priv->tx_skb[tx_head] = skb;
449         priv->tx_phys[tx_head] = phys;
450         desc->send_addr = cpu_to_be32(phys);
451         desc->send_size = cpu_to_be32(skb->len);
452         desc->cfg = cpu_to_be32(TX_CLEAR_WB | TX_FINISH_CACHE_INV);
453         phys = priv->tx_desc_dma + tx_head * sizeof(struct tx_desc);
454         desc->wb_addr = cpu_to_be32(phys);
455         skb_tx_timestamp(skb);
456
457         hip04_set_xmit_desc(priv, phys);
458         count++;
459         netdev_sent_queue(ndev, skb->len);
460         priv->tx_head = TX_NEXT(tx_head);
461
462         stats->tx_bytes += skb->len;
463         stats->tx_packets++;
464
465         /* Ensure tx_head update visible to tx reclaim */
466         smp_wmb();
467
468         /* queue is getting full, better start cleaning up now */
469         if (count >= priv->tx_coalesce_frames) {
470                 if (napi_schedule_prep(&priv->napi)) {
471                         /* disable rx interrupt and timer */
472                         priv->reg_inten &= ~(RCV_INT);
473                         writel_relaxed(DEF_INT_MASK & ~RCV_INT,
474                                        priv->base + PPE_INTEN);
475                         hrtimer_cancel(&priv->tx_coalesce_timer);
476                         __napi_schedule(&priv->napi);
477                 }
478         } else if (!hrtimer_is_queued(&priv->tx_coalesce_timer)) {
479                 /* cleanup not pending yet, start a new timer */
480                 hip04_start_tx_timer(priv);
481         }
482
483         return NETDEV_TX_OK;
484 }
485
486 static int hip04_rx_poll(struct napi_struct *napi, int budget)
487 {
488         struct hip04_priv *priv = container_of(napi, struct hip04_priv, napi);
489         struct net_device *ndev = priv->ndev;
490         struct net_device_stats *stats = &ndev->stats;
491         struct rx_desc *desc;
492         struct sk_buff *skb;
493         unsigned char *buf;
494         bool last = false;
495         dma_addr_t phys;
496         int rx = 0;
497         int tx_remaining;
498         u16 len;
499         u32 err;
500
501         /* clean up tx descriptors */
502         tx_remaining = hip04_tx_reclaim(ndev, false);
503         priv->rx_cnt_remaining += hip04_recv_cnt(priv);
504         while (priv->rx_cnt_remaining && !last) {
505                 buf = priv->rx_buf[priv->rx_head];
506                 skb = build_skb(buf, priv->rx_buf_size);
507                 if (unlikely(!skb)) {
508                         net_dbg_ratelimited("build_skb failed\n");
509                         goto refill;
510                 }
511
512                 dma_unmap_single(priv->dev, priv->rx_phys[priv->rx_head],
513                                  RX_BUF_SIZE, DMA_FROM_DEVICE);
514                 priv->rx_phys[priv->rx_head] = 0;
515
516                 desc = (struct rx_desc *)skb->data;
517                 len = be16_to_cpu(desc->pkt_len);
518                 err = be32_to_cpu(desc->pkt_err);
519
520                 if (0 == len) {
521                         dev_kfree_skb_any(skb);
522                         last = true;
523                 } else if ((err & RX_PKT_ERR) || (len >= GMAC_MAX_PKT_LEN)) {
524                         dev_kfree_skb_any(skb);
525                         stats->rx_dropped++;
526                         stats->rx_errors++;
527                 } else {
528                         skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
529                         skb_put(skb, len);
530                         skb->protocol = eth_type_trans(skb, ndev);
531                         napi_gro_receive(&priv->napi, skb);
532                         stats->rx_packets++;
533                         stats->rx_bytes += len;
534                         rx++;
535                 }
536
537 refill:
538                 buf = netdev_alloc_frag(priv->rx_buf_size);
539                 if (!buf)
540                         goto done;
541                 phys = dma_map_single(priv->dev, buf,
542                                       RX_BUF_SIZE, DMA_FROM_DEVICE);
543                 if (dma_mapping_error(priv->dev, phys))
544                         goto done;
545                 priv->rx_buf[priv->rx_head] = buf;
546                 priv->rx_phys[priv->rx_head] = phys;
547                 hip04_set_recv_desc(priv, phys);
548
549                 priv->rx_head = RX_NEXT(priv->rx_head);
550                 if (rx >= budget) {
551                         --priv->rx_cnt_remaining;
552                         goto done;
553                 }
554
555                 if (--priv->rx_cnt_remaining == 0)
556                         priv->rx_cnt_remaining += hip04_recv_cnt(priv);
557         }
558
559         if (!(priv->reg_inten & RCV_INT)) {
560                 /* enable rx interrupt */
561                 priv->reg_inten |= RCV_INT;
562                 writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
563         }
564         napi_complete(napi);
565 done:
566         /* start a new timer if necessary */
567         if (rx < budget && tx_remaining)
568                 hip04_start_tx_timer(priv);
569
570         return rx;
571 }
572
573 static irqreturn_t hip04_mac_interrupt(int irq, void *dev_id)
574 {
575         struct net_device *ndev = (struct net_device *)dev_id;
576         struct hip04_priv *priv = netdev_priv(ndev);
577         struct net_device_stats *stats = &ndev->stats;
578         u32 ists = readl_relaxed(priv->base + PPE_INTSTS);
579
580         if (!ists)
581                 return IRQ_NONE;
582
583         writel_relaxed(DEF_INT_MASK, priv->base + PPE_RINT);
584
585         if (unlikely(ists & DEF_INT_ERR)) {
586                 if (ists & (RCV_NOBUF | RCV_DROP)) {
587                         stats->rx_errors++;
588                         stats->rx_dropped++;
589                         netdev_err(ndev, "rx drop\n");
590                 }
591                 if (ists & TX_DROP) {
592                         stats->tx_dropped++;
593                         netdev_err(ndev, "tx drop\n");
594                 }
595         }
596
597         if (ists & RCV_INT && napi_schedule_prep(&priv->napi)) {
598                 /* disable rx interrupt */
599                 priv->reg_inten &= ~(RCV_INT);
600                 writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
601                 hrtimer_cancel(&priv->tx_coalesce_timer);
602                 __napi_schedule(&priv->napi);
603         }
604
605         return IRQ_HANDLED;
606 }
607
608 static enum hrtimer_restart tx_done(struct hrtimer *hrtimer)
609 {
610         struct hip04_priv *priv;
611
612         priv = container_of(hrtimer, struct hip04_priv, tx_coalesce_timer);
613
614         if (napi_schedule_prep(&priv->napi)) {
615                 /* disable rx interrupt */
616                 priv->reg_inten &= ~(RCV_INT);
617                 writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
618                 __napi_schedule(&priv->napi);
619         }
620
621         return HRTIMER_NORESTART;
622 }
623
624 static void hip04_adjust_link(struct net_device *ndev)
625 {
626         struct hip04_priv *priv = netdev_priv(ndev);
627         struct phy_device *phy = priv->phy;
628
629         if ((priv->speed != phy->speed) || (priv->duplex != phy->duplex)) {
630                 hip04_config_port(ndev, phy->speed, phy->duplex);
631                 phy_print_status(phy);
632         }
633 }
634
635 static int hip04_mac_open(struct net_device *ndev)
636 {
637         struct hip04_priv *priv = netdev_priv(ndev);
638         int i;
639
640         priv->rx_head = 0;
641         priv->rx_cnt_remaining = 0;
642         priv->tx_head = 0;
643         priv->tx_tail = 0;
644         hip04_reset_ppe(priv);
645
646         for (i = 0; i < RX_DESC_NUM; i++) {
647                 dma_addr_t phys;
648
649                 phys = dma_map_single(priv->dev, priv->rx_buf[i],
650                                       RX_BUF_SIZE, DMA_FROM_DEVICE);
651                 if (dma_mapping_error(priv->dev, phys))
652                         return -EIO;
653
654                 priv->rx_phys[i] = phys;
655                 hip04_set_recv_desc(priv, phys);
656         }
657
658         if (priv->phy)
659                 phy_start(priv->phy);
660
661         netdev_reset_queue(ndev);
662         netif_start_queue(ndev);
663         hip04_mac_enable(ndev);
664         napi_enable(&priv->napi);
665
666         return 0;
667 }
668
669 static int hip04_mac_stop(struct net_device *ndev)
670 {
671         struct hip04_priv *priv = netdev_priv(ndev);
672         int i;
673
674         napi_disable(&priv->napi);
675         netif_stop_queue(ndev);
676         hip04_mac_disable(ndev);
677         hip04_tx_reclaim(ndev, true);
678         hip04_reset_ppe(priv);
679
680         if (priv->phy)
681                 phy_stop(priv->phy);
682
683         for (i = 0; i < RX_DESC_NUM; i++) {
684                 if (priv->rx_phys[i]) {
685                         dma_unmap_single(priv->dev, priv->rx_phys[i],
686                                          RX_BUF_SIZE, DMA_FROM_DEVICE);
687                         priv->rx_phys[i] = 0;
688                 }
689         }
690
691         return 0;
692 }
693
694 static void hip04_timeout(struct net_device *ndev)
695 {
696         struct hip04_priv *priv = netdev_priv(ndev);
697
698         schedule_work(&priv->tx_timeout_task);
699 }
700
701 static void hip04_tx_timeout_task(struct work_struct *work)
702 {
703         struct hip04_priv *priv;
704
705         priv = container_of(work, struct hip04_priv, tx_timeout_task);
706         hip04_mac_stop(priv->ndev);
707         hip04_mac_open(priv->ndev);
708 }
709
710 static struct net_device_stats *hip04_get_stats(struct net_device *ndev)
711 {
712         return &ndev->stats;
713 }
714
715 static int hip04_get_coalesce(struct net_device *netdev,
716                               struct ethtool_coalesce *ec)
717 {
718         struct hip04_priv *priv = netdev_priv(netdev);
719
720         ec->tx_coalesce_usecs = priv->tx_coalesce_usecs;
721         ec->tx_max_coalesced_frames = priv->tx_coalesce_frames;
722
723         return 0;
724 }
725
726 static int hip04_set_coalesce(struct net_device *netdev,
727                               struct ethtool_coalesce *ec)
728 {
729         struct hip04_priv *priv = netdev_priv(netdev);
730
731         /* Check not supported parameters  */
732         if ((ec->rx_max_coalesced_frames) || (ec->rx_coalesce_usecs_irq) ||
733             (ec->rx_max_coalesced_frames_irq) || (ec->tx_coalesce_usecs_irq) ||
734             (ec->use_adaptive_rx_coalesce) || (ec->use_adaptive_tx_coalesce) ||
735             (ec->pkt_rate_low) || (ec->rx_coalesce_usecs_low) ||
736             (ec->rx_max_coalesced_frames_low) || (ec->tx_coalesce_usecs_high) ||
737             (ec->tx_max_coalesced_frames_low) || (ec->pkt_rate_high) ||
738             (ec->tx_coalesce_usecs_low) || (ec->rx_coalesce_usecs_high) ||
739             (ec->rx_max_coalesced_frames_high) || (ec->rx_coalesce_usecs) ||
740             (ec->tx_max_coalesced_frames_irq) ||
741             (ec->stats_block_coalesce_usecs) ||
742             (ec->tx_max_coalesced_frames_high) || (ec->rate_sample_interval))
743                 return -EOPNOTSUPP;
744
745         if ((ec->tx_coalesce_usecs > HIP04_MAX_TX_COALESCE_USECS ||
746              ec->tx_coalesce_usecs < HIP04_MIN_TX_COALESCE_USECS) ||
747             (ec->tx_max_coalesced_frames > HIP04_MAX_TX_COALESCE_FRAMES ||
748              ec->tx_max_coalesced_frames < HIP04_MIN_TX_COALESCE_FRAMES))
749                 return -EINVAL;
750
751         priv->tx_coalesce_usecs = ec->tx_coalesce_usecs;
752         priv->tx_coalesce_frames = ec->tx_max_coalesced_frames;
753
754         return 0;
755 }
756
757 static void hip04_get_drvinfo(struct net_device *netdev,
758                               struct ethtool_drvinfo *drvinfo)
759 {
760         strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
761         strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
762 }
763
764 static const struct ethtool_ops hip04_ethtool_ops = {
765         .get_coalesce           = hip04_get_coalesce,
766         .set_coalesce           = hip04_set_coalesce,
767         .get_drvinfo            = hip04_get_drvinfo,
768 };
769
770 static const struct net_device_ops hip04_netdev_ops = {
771         .ndo_open               = hip04_mac_open,
772         .ndo_stop               = hip04_mac_stop,
773         .ndo_get_stats          = hip04_get_stats,
774         .ndo_start_xmit         = hip04_mac_start_xmit,
775         .ndo_set_mac_address    = hip04_set_mac_address,
776         .ndo_tx_timeout         = hip04_timeout,
777         .ndo_validate_addr      = eth_validate_addr,
778         .ndo_change_mtu         = eth_change_mtu,
779 };
780
781 static int hip04_alloc_ring(struct net_device *ndev, struct device *d)
782 {
783         struct hip04_priv *priv = netdev_priv(ndev);
784         int i;
785
786         priv->tx_desc = dma_alloc_coherent(d,
787                                            TX_DESC_NUM * sizeof(struct tx_desc),
788                                            &priv->tx_desc_dma, GFP_KERNEL);
789         if (!priv->tx_desc)
790                 return -ENOMEM;
791
792         priv->rx_buf_size = RX_BUF_SIZE +
793                             SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
794         for (i = 0; i < RX_DESC_NUM; i++) {
795                 priv->rx_buf[i] = netdev_alloc_frag(priv->rx_buf_size);
796                 if (!priv->rx_buf[i])
797                         return -ENOMEM;
798         }
799
800         return 0;
801 }
802
803 static void hip04_free_ring(struct net_device *ndev, struct device *d)
804 {
805         struct hip04_priv *priv = netdev_priv(ndev);
806         int i;
807
808         for (i = 0; i < RX_DESC_NUM; i++)
809                 if (priv->rx_buf[i])
810                         skb_free_frag(priv->rx_buf[i]);
811
812         for (i = 0; i < TX_DESC_NUM; i++)
813                 if (priv->tx_skb[i])
814                         dev_kfree_skb_any(priv->tx_skb[i]);
815
816         dma_free_coherent(d, TX_DESC_NUM * sizeof(struct tx_desc),
817                           priv->tx_desc, priv->tx_desc_dma);
818 }
819
820 static int hip04_mac_probe(struct platform_device *pdev)
821 {
822         struct device *d = &pdev->dev;
823         struct device_node *node = d->of_node;
824         struct of_phandle_args arg;
825         struct net_device *ndev;
826         struct hip04_priv *priv;
827         struct resource *res;
828         int irq;
829         int ret;
830
831         ndev = alloc_etherdev(sizeof(struct hip04_priv));
832         if (!ndev)
833                 return -ENOMEM;
834
835         priv = netdev_priv(ndev);
836         priv->dev = d;
837         priv->ndev = ndev;
838         platform_set_drvdata(pdev, ndev);
839
840         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
841         priv->base = devm_ioremap_resource(d, res);
842         if (IS_ERR(priv->base)) {
843                 ret = PTR_ERR(priv->base);
844                 goto init_fail;
845         }
846
847         ret = of_parse_phandle_with_fixed_args(node, "port-handle", 2, 0, &arg);
848         if (ret < 0) {
849                 dev_warn(d, "no port-handle\n");
850                 goto init_fail;
851         }
852
853         priv->port = arg.args[0];
854         priv->chan = arg.args[1] * RX_DESC_NUM;
855
856         hrtimer_init(&priv->tx_coalesce_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
857
858         /* BQL will try to keep the TX queue as short as possible, but it can't
859          * be faster than tx_coalesce_usecs, so we need a fast timeout here,
860          * but also long enough to gather up enough frames to ensure we don't
861          * get more interrupts than necessary.
862          * 200us is enough for 16 frames of 1500 bytes at gigabit ethernet rate
863          */
864         priv->tx_coalesce_frames = TX_DESC_NUM * 3 / 4;
865         priv->tx_coalesce_usecs = 200;
866         priv->tx_coalesce_timer.function = tx_done;
867
868         priv->map = syscon_node_to_regmap(arg.np);
869         if (IS_ERR(priv->map)) {
870                 dev_warn(d, "no syscon hisilicon,hip04-ppe\n");
871                 ret = PTR_ERR(priv->map);
872                 goto init_fail;
873         }
874
875         priv->phy_mode = of_get_phy_mode(node);
876         if (priv->phy_mode < 0) {
877                 dev_warn(d, "not find phy-mode\n");
878                 ret = -EINVAL;
879                 goto init_fail;
880         }
881
882         irq = platform_get_irq(pdev, 0);
883         if (irq <= 0) {
884                 ret = -EINVAL;
885                 goto init_fail;
886         }
887
888         ret = devm_request_irq(d, irq, hip04_mac_interrupt,
889                                0, pdev->name, ndev);
890         if (ret) {
891                 netdev_err(ndev, "devm_request_irq failed\n");
892                 goto init_fail;
893         }
894
895         priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
896         if (priv->phy_node) {
897                 priv->phy = of_phy_connect(ndev, priv->phy_node,
898                                            &hip04_adjust_link,
899                                            0, priv->phy_mode);
900                 if (!priv->phy) {
901                         ret = -EPROBE_DEFER;
902                         goto init_fail;
903                 }
904         }
905
906         INIT_WORK(&priv->tx_timeout_task, hip04_tx_timeout_task);
907
908         ether_setup(ndev);
909         ndev->netdev_ops = &hip04_netdev_ops;
910         ndev->ethtool_ops = &hip04_ethtool_ops;
911         ndev->watchdog_timeo = TX_TIMEOUT;
912         ndev->priv_flags |= IFF_UNICAST_FLT;
913         ndev->irq = irq;
914         netif_napi_add(ndev, &priv->napi, hip04_rx_poll, NAPI_POLL_WEIGHT);
915         SET_NETDEV_DEV(ndev, &pdev->dev);
916
917         hip04_reset_ppe(priv);
918         if (priv->phy_mode == PHY_INTERFACE_MODE_MII)
919                 hip04_config_port(ndev, SPEED_100, DUPLEX_FULL);
920
921         hip04_config_fifo(priv);
922         random_ether_addr(ndev->dev_addr);
923         hip04_update_mac_address(ndev);
924
925         ret = hip04_alloc_ring(ndev, d);
926         if (ret) {
927                 netdev_err(ndev, "alloc ring fail\n");
928                 goto alloc_fail;
929         }
930
931         ret = register_netdev(ndev);
932         if (ret)
933                 goto alloc_fail;
934
935         return 0;
936
937 alloc_fail:
938         hip04_free_ring(ndev, d);
939 init_fail:
940         of_node_put(priv->phy_node);
941         free_netdev(ndev);
942         return ret;
943 }
944
945 static int hip04_remove(struct platform_device *pdev)
946 {
947         struct net_device *ndev = platform_get_drvdata(pdev);
948         struct hip04_priv *priv = netdev_priv(ndev);
949         struct device *d = &pdev->dev;
950
951         if (priv->phy)
952                 phy_disconnect(priv->phy);
953
954         hip04_free_ring(ndev, d);
955         unregister_netdev(ndev);
956         of_node_put(priv->phy_node);
957         cancel_work_sync(&priv->tx_timeout_task);
958         free_netdev(ndev);
959
960         return 0;
961 }
962
963 static const struct of_device_id hip04_mac_match[] = {
964         { .compatible = "hisilicon,hip04-mac" },
965         { }
966 };
967
968 MODULE_DEVICE_TABLE(of, hip04_mac_match);
969
970 static struct platform_driver hip04_mac_driver = {
971         .probe  = hip04_mac_probe,
972         .remove = hip04_remove,
973         .driver = {
974                 .name           = DRV_NAME,
975                 .of_match_table = hip04_mac_match,
976         },
977 };
978 module_platform_driver(hip04_mac_driver);
979
980 MODULE_DESCRIPTION("HISILICON P04 Ethernet driver");
981 MODULE_LICENSE("GPL");