blob: a91d49dd92ea6c32a308ea61d0e1a02785985aa1 [file] [log] [blame]
dingtianhonga41ea462015-01-14 14:34:14 +08001
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
134struct 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
142struct rx_desc {
143 u16 reserved_16;
144 u16 pkt_len;
145 u32 reserve1[3];
146 u32 pkt_err;
147 u32 reserve2[4];
148};
149
150struct 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;
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800160 struct device *dev;
dingtianhonga41ea462015-01-14 14:34:14 +0800161 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
178 struct device_node *phy_node;
179 struct phy_device *phy;
180 struct regmap *map;
181 struct work_struct tx_timeout_task;
182
183 /* written only by tx cleanup */
184 unsigned int tx_tail ____cacheline_aligned_in_smp;
185};
186
187static inline unsigned int tx_count(unsigned int head, unsigned int tail)
188{
Jiangfeng Xiao4ab30522019-08-03 20:31:40 +0800189 return (head - tail) % TX_DESC_NUM;
dingtianhonga41ea462015-01-14 14:34:14 +0800190}
191
192static void hip04_config_port(struct net_device *ndev, u32 speed, u32 duplex)
193{
194 struct hip04_priv *priv = netdev_priv(ndev);
195 u32 val;
196
197 priv->speed = speed;
198 priv->duplex = duplex;
199
200 switch (priv->phy_mode) {
201 case PHY_INTERFACE_MODE_SGMII:
202 if (speed == SPEED_1000)
203 val = SGMII_SPEED_1000;
204 else if (speed == SPEED_100)
205 val = SGMII_SPEED_100;
206 else
207 val = SGMII_SPEED_10;
208 break;
209 case PHY_INTERFACE_MODE_MII:
210 if (speed == SPEED_100)
211 val = MII_SPEED_100;
212 else
213 val = MII_SPEED_10;
214 break;
215 default:
216 netdev_warn(ndev, "not supported mode\n");
217 val = MII_SPEED_10;
218 break;
219 }
220 writel_relaxed(val, priv->base + GE_PORT_MODE);
221
222 val = duplex ? GE_DUPLEX_FULL : GE_DUPLEX_HALF;
223 writel_relaxed(val, priv->base + GE_DUPLEX_TYPE);
224
225 val = GE_MODE_CHANGE_EN;
226 writel_relaxed(val, priv->base + GE_MODE_CHANGE_REG);
227}
228
229static void hip04_reset_ppe(struct hip04_priv *priv)
230{
231 u32 val, tmp, timeout = 0;
232
233 do {
234 regmap_read(priv->map, priv->port * 4 + PPE_CURR_BUF_CNT, &val);
235 regmap_read(priv->map, priv->port * 4 + PPE_CFG_RX_ADDR, &tmp);
236 if (timeout++ > RESET_TIMEOUT)
237 break;
238 } while (val & 0xfff);
239}
240
241static void hip04_config_fifo(struct hip04_priv *priv)
242{
243 u32 val;
244
245 val = readl_relaxed(priv->base + PPE_CFG_STS_MODE);
246 val |= PPE_CFG_STS_RX_PKT_CNT_RC;
247 writel_relaxed(val, priv->base + PPE_CFG_STS_MODE);
248
249 val = BIT(priv->port);
250 regmap_write(priv->map, priv->port * 4 + PPE_CFG_POOL_GRP, val);
251
252 val = priv->port << PPE_CFG_QOS_VMID_GRP_SHIFT;
253 val |= PPE_CFG_QOS_VMID_MODE;
254 writel_relaxed(val, priv->base + PPE_CFG_QOS_VMID_GEN);
255
256 val = RX_BUF_SIZE;
257 regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_BUF_SIZE, val);
258
259 val = RX_DESC_NUM << PPE_CFG_RX_DEPTH_SHIFT;
260 val |= PPE_CFG_RX_FIFO_FSFU;
261 val |= priv->chan << PPE_CFG_RX_START_SHIFT;
262 regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_FIFO_SIZE, val);
263
264 val = NET_IP_ALIGN << PPE_CFG_RX_CTRL_ALIGN_SHIFT;
265 writel_relaxed(val, priv->base + PPE_CFG_RX_CTRL_REG);
266
267 val = PPE_CFG_RX_PKT_ALIGN;
268 writel_relaxed(val, priv->base + PPE_CFG_RX_PKT_MODE_REG);
269
270 val = PPE_CFG_BUS_LOCAL_REL | PPE_CFG_BUS_BIG_ENDIEN;
271 writel_relaxed(val, priv->base + PPE_CFG_BUS_CTRL_REG);
272
273 val = GMAC_PPE_RX_PKT_MAX_LEN;
274 writel_relaxed(val, priv->base + PPE_CFG_MAX_FRAME_LEN_REG);
275
276 val = GMAC_MAX_PKT_LEN;
277 writel_relaxed(val, priv->base + GE_MAX_FRM_SIZE_REG);
278
279 val = GMAC_MIN_PKT_LEN;
280 writel_relaxed(val, priv->base + GE_SHORT_RUNTS_THR_REG);
281
282 val = readl_relaxed(priv->base + GE_TRANSMIT_CONTROL_REG);
283 val |= GE_TX_AUTO_NEG | GE_TX_ADD_CRC | GE_TX_SHORT_PAD_THROUGH;
284 writel_relaxed(val, priv->base + GE_TRANSMIT_CONTROL_REG);
285
286 val = GE_RX_STRIP_CRC;
287 writel_relaxed(val, priv->base + GE_CF_CRC_STRIP_REG);
288
289 val = readl_relaxed(priv->base + GE_RECV_CONTROL_REG);
290 val |= GE_RX_STRIP_PAD | GE_RX_PAD_EN;
291 writel_relaxed(val, priv->base + GE_RECV_CONTROL_REG);
292
293 val = GE_AUTO_NEG_CTL;
294 writel_relaxed(val, priv->base + GE_TX_LOCAL_PAGE_REG);
295}
296
297static void hip04_mac_enable(struct net_device *ndev)
298{
299 struct hip04_priv *priv = netdev_priv(ndev);
300 u32 val;
301
302 /* enable tx & rx */
303 val = readl_relaxed(priv->base + GE_PORT_EN);
304 val |= GE_RX_PORT_EN | GE_TX_PORT_EN;
305 writel_relaxed(val, priv->base + GE_PORT_EN);
306
307 /* clear rx int */
308 val = RCV_INT;
309 writel_relaxed(val, priv->base + PPE_RINT);
310
311 /* config recv int */
312 val = GE_RX_INT_THRESHOLD | GE_RX_TIMEOUT;
313 writel_relaxed(val, priv->base + PPE_CFG_RX_PKT_INT);
314
315 /* enable interrupt */
316 priv->reg_inten = DEF_INT_MASK;
317 writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
318}
319
320static void hip04_mac_disable(struct net_device *ndev)
321{
322 struct hip04_priv *priv = netdev_priv(ndev);
323 u32 val;
324
325 /* disable int */
326 priv->reg_inten &= ~(DEF_INT_MASK);
327 writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
328
329 /* disable tx & rx */
330 val = readl_relaxed(priv->base + GE_PORT_EN);
331 val &= ~(GE_RX_PORT_EN | GE_TX_PORT_EN);
332 writel_relaxed(val, priv->base + GE_PORT_EN);
333}
334
335static void hip04_set_xmit_desc(struct hip04_priv *priv, dma_addr_t phys)
336{
337 writel(phys, priv->base + PPE_CFG_CPU_ADD_ADDR);
338}
339
340static void hip04_set_recv_desc(struct hip04_priv *priv, dma_addr_t phys)
341{
342 regmap_write(priv->map, priv->port * 4 + PPE_CFG_RX_ADDR, phys);
343}
344
345static u32 hip04_recv_cnt(struct hip04_priv *priv)
346{
347 return readl(priv->base + PPE_HIS_RX_PKT_CNT);
348}
349
350static void hip04_update_mac_address(struct net_device *ndev)
351{
352 struct hip04_priv *priv = netdev_priv(ndev);
353
354 writel_relaxed(((ndev->dev_addr[0] << 8) | (ndev->dev_addr[1])),
355 priv->base + GE_STATION_MAC_ADDRESS);
356 writel_relaxed(((ndev->dev_addr[2] << 24) | (ndev->dev_addr[3] << 16) |
357 (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5])),
358 priv->base + GE_STATION_MAC_ADDRESS + 4);
359}
360
361static int hip04_set_mac_address(struct net_device *ndev, void *addr)
362{
363 eth_mac_addr(ndev, addr);
364 hip04_update_mac_address(ndev);
365 return 0;
366}
367
368static int hip04_tx_reclaim(struct net_device *ndev, bool force)
369{
370 struct hip04_priv *priv = netdev_priv(ndev);
371 unsigned tx_tail = priv->tx_tail;
372 struct tx_desc *desc;
373 unsigned int bytes_compl = 0, pkts_compl = 0;
374 unsigned int count;
375
376 smp_rmb();
Mark Rutland6aa7de02017-10-23 14:07:29 -0700377 count = tx_count(READ_ONCE(priv->tx_head), tx_tail);
dingtianhonga41ea462015-01-14 14:34:14 +0800378 if (count == 0)
379 goto out;
380
381 while (count) {
382 desc = &priv->tx_desc[tx_tail];
383 if (desc->send_addr != 0) {
384 if (force)
385 desc->send_addr = 0;
386 else
387 break;
388 }
389
390 if (priv->tx_phys[tx_tail]) {
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800391 dma_unmap_single(priv->dev, priv->tx_phys[tx_tail],
dingtianhonga41ea462015-01-14 14:34:14 +0800392 priv->tx_skb[tx_tail]->len,
393 DMA_TO_DEVICE);
394 priv->tx_phys[tx_tail] = 0;
395 }
396 pkts_compl++;
397 bytes_compl += priv->tx_skb[tx_tail]->len;
398 dev_kfree_skb(priv->tx_skb[tx_tail]);
399 priv->tx_skb[tx_tail] = NULL;
400 tx_tail = TX_NEXT(tx_tail);
401 count--;
402 }
403
404 priv->tx_tail = tx_tail;
405 smp_wmb(); /* Ensure tx_tail visible to xmit */
406
407out:
408 if (pkts_compl || bytes_compl)
409 netdev_completed_queue(ndev, pkts_compl, bytes_compl);
410
411 if (unlikely(netif_queue_stopped(ndev)) && (count < (TX_DESC_NUM - 1)))
412 netif_wake_queue(ndev);
413
414 return count;
415}
416
Thomas Gleixner48b63772015-04-14 21:42:42 +0200417static void hip04_start_tx_timer(struct hip04_priv *priv)
418{
419 unsigned long ns = priv->tx_coalesce_usecs * NSEC_PER_USEC / 2;
420
421 /* allow timer to fire after half the time at the earliest */
422 hrtimer_start_range_ns(&priv->tx_coalesce_timer, ns_to_ktime(ns),
423 ns, HRTIMER_MODE_REL);
424}
425
dingtianhonga41ea462015-01-14 14:34:14 +0800426static int hip04_mac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
427{
428 struct hip04_priv *priv = netdev_priv(ndev);
429 struct net_device_stats *stats = &ndev->stats;
430 unsigned int tx_head = priv->tx_head, count;
431 struct tx_desc *desc = &priv->tx_desc[tx_head];
432 dma_addr_t phys;
433
434 smp_rmb();
Mark Rutland6aa7de02017-10-23 14:07:29 -0700435 count = tx_count(tx_head, READ_ONCE(priv->tx_tail));
dingtianhonga41ea462015-01-14 14:34:14 +0800436 if (count == (TX_DESC_NUM - 1)) {
437 netif_stop_queue(ndev);
438 return NETDEV_TX_BUSY;
439 }
440
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800441 phys = dma_map_single(priv->dev, skb->data, skb->len, DMA_TO_DEVICE);
442 if (dma_mapping_error(priv->dev, phys)) {
dingtianhonga41ea462015-01-14 14:34:14 +0800443 dev_kfree_skb(skb);
444 return NETDEV_TX_OK;
445 }
446
447 priv->tx_skb[tx_head] = skb;
448 priv->tx_phys[tx_head] = phys;
449 desc->send_addr = cpu_to_be32(phys);
450 desc->send_size = cpu_to_be32(skb->len);
451 desc->cfg = cpu_to_be32(TX_CLEAR_WB | TX_FINISH_CACHE_INV);
452 phys = priv->tx_desc_dma + tx_head * sizeof(struct tx_desc);
453 desc->wb_addr = cpu_to_be32(phys);
454 skb_tx_timestamp(skb);
455
456 hip04_set_xmit_desc(priv, phys);
457 priv->tx_head = TX_NEXT(tx_head);
458 count++;
459 netdev_sent_queue(ndev, skb->len);
460
461 stats->tx_bytes += skb->len;
462 stats->tx_packets++;
463
464 /* Ensure tx_head update visible to tx reclaim */
465 smp_wmb();
466
467 /* queue is getting full, better start cleaning up now */
468 if (count >= priv->tx_coalesce_frames) {
469 if (napi_schedule_prep(&priv->napi)) {
470 /* disable rx interrupt and timer */
471 priv->reg_inten &= ~(RCV_INT);
472 writel_relaxed(DEF_INT_MASK & ~RCV_INT,
473 priv->base + PPE_INTEN);
474 hrtimer_cancel(&priv->tx_coalesce_timer);
475 __napi_schedule(&priv->napi);
476 }
477 } else if (!hrtimer_is_queued(&priv->tx_coalesce_timer)) {
478 /* cleanup not pending yet, start a new timer */
Thomas Gleixner48b63772015-04-14 21:42:42 +0200479 hip04_start_tx_timer(priv);
dingtianhonga41ea462015-01-14 14:34:14 +0800480 }
481
482 return NETDEV_TX_OK;
483}
484
485static int hip04_rx_poll(struct napi_struct *napi, int budget)
486{
487 struct hip04_priv *priv = container_of(napi, struct hip04_priv, napi);
488 struct net_device *ndev = priv->ndev;
489 struct net_device_stats *stats = &ndev->stats;
490 unsigned int cnt = hip04_recv_cnt(priv);
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
Jiangfeng Xiao09ec5bf2019-08-03 20:31:39 +0800501 /* clean up tx descriptors */
502 tx_remaining = hip04_tx_reclaim(ndev, false);
503
dingtianhonga41ea462015-01-14 14:34:14 +0800504 while (cnt && !last) {
505 buf = priv->rx_buf[priv->rx_head];
506 skb = build_skb(buf, priv->rx_buf_size);
wangweidong701a0fd2016-01-13 11:11:09 +0800507 if (unlikely(!skb)) {
dingtianhonga41ea462015-01-14 14:34:14 +0800508 net_dbg_ratelimited("build_skb failed\n");
wangweidong701a0fd2016-01-13 11:11:09 +0800509 goto refill;
510 }
dingtianhonga41ea462015-01-14 14:34:14 +0800511
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800512 dma_unmap_single(priv->dev, priv->rx_phys[priv->rx_head],
dingtianhonga41ea462015-01-14 14:34:14 +0800513 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
wangweidong701a0fd2016-01-13 11:11:09 +0800537refill:
dingtianhonga41ea462015-01-14 14:34:14 +0800538 buf = netdev_alloc_frag(priv->rx_buf_size);
539 if (!buf)
540 goto done;
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800541 phys = dma_map_single(priv->dev, buf,
dingtianhonga41ea462015-01-14 14:34:14 +0800542 RX_BUF_SIZE, DMA_FROM_DEVICE);
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800543 if (dma_mapping_error(priv->dev, phys))
dingtianhonga41ea462015-01-14 14:34:14 +0800544 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 goto done;
552
553 if (--cnt == 0)
554 cnt = hip04_recv_cnt(priv);
555 }
556
557 if (!(priv->reg_inten & RCV_INT)) {
558 /* enable rx interrupt */
559 priv->reg_inten |= RCV_INT;
560 writel_relaxed(priv->reg_inten, priv->base + PPE_INTEN);
561 }
Eric Dumazet6ad20162017-01-30 08:22:01 -0800562 napi_complete_done(napi, rx);
dingtianhonga41ea462015-01-14 14:34:14 +0800563done:
Jiangfeng Xiao09ec5bf2019-08-03 20:31:39 +0800564 /* start a new timer if necessary */
dingtianhonga41ea462015-01-14 14:34:14 +0800565 if (rx < budget && tx_remaining)
Thomas Gleixner48b63772015-04-14 21:42:42 +0200566 hip04_start_tx_timer(priv);
dingtianhonga41ea462015-01-14 14:34:14 +0800567
568 return rx;
569}
570
571static irqreturn_t hip04_mac_interrupt(int irq, void *dev_id)
572{
573 struct net_device *ndev = (struct net_device *)dev_id;
574 struct hip04_priv *priv = netdev_priv(ndev);
575 struct net_device_stats *stats = &ndev->stats;
576 u32 ists = readl_relaxed(priv->base + PPE_INTSTS);
577
578 if (!ists)
579 return IRQ_NONE;
580
581 writel_relaxed(DEF_INT_MASK, priv->base + PPE_RINT);
582
583 if (unlikely(ists & DEF_INT_ERR)) {
Dan Carpentera154e6f2015-01-28 21:58:33 +0300584 if (ists & (RCV_NOBUF | RCV_DROP)) {
dingtianhonga41ea462015-01-14 14:34:14 +0800585 stats->rx_errors++;
586 stats->rx_dropped++;
587 netdev_err(ndev, "rx drop\n");
Dan Carpentera154e6f2015-01-28 21:58:33 +0300588 }
dingtianhonga41ea462015-01-14 14:34:14 +0800589 if (ists & TX_DROP) {
590 stats->tx_dropped++;
591 netdev_err(ndev, "tx drop\n");
592 }
593 }
594
595 if (ists & RCV_INT && napi_schedule_prep(&priv->napi)) {
596 /* disable rx interrupt */
597 priv->reg_inten &= ~(RCV_INT);
598 writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
599 hrtimer_cancel(&priv->tx_coalesce_timer);
600 __napi_schedule(&priv->napi);
601 }
602
603 return IRQ_HANDLED;
604}
605
Baoyou Xie49e3e6f2016-09-25 17:19:04 +0800606static enum hrtimer_restart tx_done(struct hrtimer *hrtimer)
dingtianhonga41ea462015-01-14 14:34:14 +0800607{
608 struct hip04_priv *priv;
609
610 priv = container_of(hrtimer, struct hip04_priv, tx_coalesce_timer);
611
612 if (napi_schedule_prep(&priv->napi)) {
613 /* disable rx interrupt */
614 priv->reg_inten &= ~(RCV_INT);
615 writel_relaxed(DEF_INT_MASK & ~RCV_INT, priv->base + PPE_INTEN);
616 __napi_schedule(&priv->napi);
617 }
618
619 return HRTIMER_NORESTART;
620}
621
622static void hip04_adjust_link(struct net_device *ndev)
623{
624 struct hip04_priv *priv = netdev_priv(ndev);
625 struct phy_device *phy = priv->phy;
626
627 if ((priv->speed != phy->speed) || (priv->duplex != phy->duplex)) {
628 hip04_config_port(ndev, phy->speed, phy->duplex);
629 phy_print_status(phy);
630 }
631}
632
633static int hip04_mac_open(struct net_device *ndev)
634{
635 struct hip04_priv *priv = netdev_priv(ndev);
636 int i;
637
638 priv->rx_head = 0;
639 priv->tx_head = 0;
640 priv->tx_tail = 0;
641 hip04_reset_ppe(priv);
642
643 for (i = 0; i < RX_DESC_NUM; i++) {
644 dma_addr_t phys;
645
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800646 phys = dma_map_single(priv->dev, priv->rx_buf[i],
dingtianhonga41ea462015-01-14 14:34:14 +0800647 RX_BUF_SIZE, DMA_FROM_DEVICE);
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800648 if (dma_mapping_error(priv->dev, phys))
dingtianhonga41ea462015-01-14 14:34:14 +0800649 return -EIO;
650
651 priv->rx_phys[i] = phys;
652 hip04_set_recv_desc(priv, phys);
653 }
654
655 if (priv->phy)
656 phy_start(priv->phy);
657
658 netdev_reset_queue(ndev);
659 netif_start_queue(ndev);
660 hip04_mac_enable(ndev);
661 napi_enable(&priv->napi);
662
663 return 0;
664}
665
666static int hip04_mac_stop(struct net_device *ndev)
667{
668 struct hip04_priv *priv = netdev_priv(ndev);
669 int i;
670
671 napi_disable(&priv->napi);
672 netif_stop_queue(ndev);
673 hip04_mac_disable(ndev);
674 hip04_tx_reclaim(ndev, true);
675 hip04_reset_ppe(priv);
676
677 if (priv->phy)
678 phy_stop(priv->phy);
679
680 for (i = 0; i < RX_DESC_NUM; i++) {
681 if (priv->rx_phys[i]) {
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800682 dma_unmap_single(priv->dev, priv->rx_phys[i],
dingtianhonga41ea462015-01-14 14:34:14 +0800683 RX_BUF_SIZE, DMA_FROM_DEVICE);
684 priv->rx_phys[i] = 0;
685 }
686 }
687
688 return 0;
689}
690
691static void hip04_timeout(struct net_device *ndev)
692{
693 struct hip04_priv *priv = netdev_priv(ndev);
694
695 schedule_work(&priv->tx_timeout_task);
696}
697
698static void hip04_tx_timeout_task(struct work_struct *work)
699{
700 struct hip04_priv *priv;
701
702 priv = container_of(work, struct hip04_priv, tx_timeout_task);
703 hip04_mac_stop(priv->ndev);
704 hip04_mac_open(priv->ndev);
705}
706
dingtianhonga41ea462015-01-14 14:34:14 +0800707static int hip04_get_coalesce(struct net_device *netdev,
708 struct ethtool_coalesce *ec)
709{
710 struct hip04_priv *priv = netdev_priv(netdev);
711
712 ec->tx_coalesce_usecs = priv->tx_coalesce_usecs;
713 ec->tx_max_coalesced_frames = priv->tx_coalesce_frames;
714
715 return 0;
716}
717
718static int hip04_set_coalesce(struct net_device *netdev,
719 struct ethtool_coalesce *ec)
720{
721 struct hip04_priv *priv = netdev_priv(netdev);
722
723 /* Check not supported parameters */
724 if ((ec->rx_max_coalesced_frames) || (ec->rx_coalesce_usecs_irq) ||
725 (ec->rx_max_coalesced_frames_irq) || (ec->tx_coalesce_usecs_irq) ||
726 (ec->use_adaptive_rx_coalesce) || (ec->use_adaptive_tx_coalesce) ||
727 (ec->pkt_rate_low) || (ec->rx_coalesce_usecs_low) ||
728 (ec->rx_max_coalesced_frames_low) || (ec->tx_coalesce_usecs_high) ||
729 (ec->tx_max_coalesced_frames_low) || (ec->pkt_rate_high) ||
730 (ec->tx_coalesce_usecs_low) || (ec->rx_coalesce_usecs_high) ||
731 (ec->rx_max_coalesced_frames_high) || (ec->rx_coalesce_usecs) ||
732 (ec->tx_max_coalesced_frames_irq) ||
733 (ec->stats_block_coalesce_usecs) ||
734 (ec->tx_max_coalesced_frames_high) || (ec->rate_sample_interval))
735 return -EOPNOTSUPP;
736
737 if ((ec->tx_coalesce_usecs > HIP04_MAX_TX_COALESCE_USECS ||
738 ec->tx_coalesce_usecs < HIP04_MIN_TX_COALESCE_USECS) ||
739 (ec->tx_max_coalesced_frames > HIP04_MAX_TX_COALESCE_FRAMES ||
740 ec->tx_max_coalesced_frames < HIP04_MIN_TX_COALESCE_FRAMES))
741 return -EINVAL;
742
743 priv->tx_coalesce_usecs = ec->tx_coalesce_usecs;
744 priv->tx_coalesce_frames = ec->tx_max_coalesced_frames;
745
746 return 0;
747}
748
749static void hip04_get_drvinfo(struct net_device *netdev,
750 struct ethtool_drvinfo *drvinfo)
751{
752 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
753 strlcpy(drvinfo->version, DRV_VERSION, sizeof(drvinfo->version));
754}
755
Julia Lawallbc6f0132016-08-31 09:30:46 +0200756static const struct ethtool_ops hip04_ethtool_ops = {
dingtianhonga41ea462015-01-14 14:34:14 +0800757 .get_coalesce = hip04_get_coalesce,
758 .set_coalesce = hip04_set_coalesce,
759 .get_drvinfo = hip04_get_drvinfo,
760};
761
Julia Lawall66f58ec2016-09-15 22:23:24 +0200762static const struct net_device_ops hip04_netdev_ops = {
dingtianhonga41ea462015-01-14 14:34:14 +0800763 .ndo_open = hip04_mac_open,
764 .ndo_stop = hip04_mac_stop,
dingtianhonga41ea462015-01-14 14:34:14 +0800765 .ndo_start_xmit = hip04_mac_start_xmit,
766 .ndo_set_mac_address = hip04_set_mac_address,
767 .ndo_tx_timeout = hip04_timeout,
768 .ndo_validate_addr = eth_validate_addr,
dingtianhonga41ea462015-01-14 14:34:14 +0800769};
770
771static int hip04_alloc_ring(struct net_device *ndev, struct device *d)
772{
773 struct hip04_priv *priv = netdev_priv(ndev);
774 int i;
775
776 priv->tx_desc = dma_alloc_coherent(d,
777 TX_DESC_NUM * sizeof(struct tx_desc),
778 &priv->tx_desc_dma, GFP_KERNEL);
779 if (!priv->tx_desc)
780 return -ENOMEM;
781
782 priv->rx_buf_size = RX_BUF_SIZE +
783 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
784 for (i = 0; i < RX_DESC_NUM; i++) {
785 priv->rx_buf[i] = netdev_alloc_frag(priv->rx_buf_size);
786 if (!priv->rx_buf[i])
787 return -ENOMEM;
788 }
789
790 return 0;
791}
792
793static void hip04_free_ring(struct net_device *ndev, struct device *d)
794{
795 struct hip04_priv *priv = netdev_priv(ndev);
796 int i;
797
798 for (i = 0; i < RX_DESC_NUM; i++)
799 if (priv->rx_buf[i])
Alexander Duyckedea5842015-05-06 21:12:25 -0700800 skb_free_frag(priv->rx_buf[i]);
dingtianhonga41ea462015-01-14 14:34:14 +0800801
802 for (i = 0; i < TX_DESC_NUM; i++)
803 if (priv->tx_skb[i])
804 dev_kfree_skb_any(priv->tx_skb[i]);
805
806 dma_free_coherent(d, TX_DESC_NUM * sizeof(struct tx_desc),
807 priv->tx_desc, priv->tx_desc_dma);
808}
809
810static int hip04_mac_probe(struct platform_device *pdev)
811{
812 struct device *d = &pdev->dev;
813 struct device_node *node = d->of_node;
814 struct of_phandle_args arg;
815 struct net_device *ndev;
816 struct hip04_priv *priv;
817 struct resource *res;
Andrzej Hajdaf26bf062015-09-24 16:00:15 +0200818 int irq;
dingtianhonga41ea462015-01-14 14:34:14 +0800819 int ret;
820
821 ndev = alloc_etherdev(sizeof(struct hip04_priv));
822 if (!ndev)
823 return -ENOMEM;
824
825 priv = netdev_priv(ndev);
Jiangfeng Xiaoe0c03022019-08-03 20:31:41 +0800826 priv->dev = d;
dingtianhonga41ea462015-01-14 14:34:14 +0800827 priv->ndev = ndev;
828 platform_set_drvdata(pdev, ndev);
Dongpo Li8cd1f702016-12-12 20:03:43 +0800829 SET_NETDEV_DEV(ndev, &pdev->dev);
dingtianhonga41ea462015-01-14 14:34:14 +0800830
831 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
832 priv->base = devm_ioremap_resource(d, res);
833 if (IS_ERR(priv->base)) {
834 ret = PTR_ERR(priv->base);
835 goto init_fail;
836 }
837
838 ret = of_parse_phandle_with_fixed_args(node, "port-handle", 2, 0, &arg);
839 if (ret < 0) {
840 dev_warn(d, "no port-handle\n");
841 goto init_fail;
842 }
843
844 priv->port = arg.args[0];
845 priv->chan = arg.args[1] * RX_DESC_NUM;
846
847 hrtimer_init(&priv->tx_coalesce_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
848
849 /* BQL will try to keep the TX queue as short as possible, but it can't
850 * be faster than tx_coalesce_usecs, so we need a fast timeout here,
851 * but also long enough to gather up enough frames to ensure we don't
852 * get more interrupts than necessary.
853 * 200us is enough for 16 frames of 1500 bytes at gigabit ethernet rate
854 */
855 priv->tx_coalesce_frames = TX_DESC_NUM * 3 / 4;
856 priv->tx_coalesce_usecs = 200;
dingtianhonga41ea462015-01-14 14:34:14 +0800857 priv->tx_coalesce_timer.function = tx_done;
858
859 priv->map = syscon_node_to_regmap(arg.np);
860 if (IS_ERR(priv->map)) {
861 dev_warn(d, "no syscon hisilicon,hip04-ppe\n");
862 ret = PTR_ERR(priv->map);
863 goto init_fail;
864 }
865
866 priv->phy_mode = of_get_phy_mode(node);
867 if (priv->phy_mode < 0) {
868 dev_warn(d, "not find phy-mode\n");
869 ret = -EINVAL;
870 goto init_fail;
871 }
872
873 irq = platform_get_irq(pdev, 0);
874 if (irq <= 0) {
875 ret = -EINVAL;
876 goto init_fail;
877 }
878
879 ret = devm_request_irq(d, irq, hip04_mac_interrupt,
880 0, pdev->name, ndev);
881 if (ret) {
882 netdev_err(ndev, "devm_request_irq failed\n");
883 goto init_fail;
884 }
885
886 priv->phy_node = of_parse_phandle(node, "phy-handle", 0);
887 if (priv->phy_node) {
888 priv->phy = of_phy_connect(ndev, priv->phy_node,
889 &hip04_adjust_link,
890 0, priv->phy_mode);
891 if (!priv->phy) {
892 ret = -EPROBE_DEFER;
893 goto init_fail;
894 }
895 }
896
897 INIT_WORK(&priv->tx_timeout_task, hip04_tx_timeout_task);
898
dingtianhonga41ea462015-01-14 14:34:14 +0800899 ndev->netdev_ops = &hip04_netdev_ops;
900 ndev->ethtool_ops = &hip04_ethtool_ops;
901 ndev->watchdog_timeo = TX_TIMEOUT;
902 ndev->priv_flags |= IFF_UNICAST_FLT;
903 ndev->irq = irq;
904 netif_napi_add(ndev, &priv->napi, hip04_rx_poll, NAPI_POLL_WEIGHT);
dingtianhonga41ea462015-01-14 14:34:14 +0800905
906 hip04_reset_ppe(priv);
907 if (priv->phy_mode == PHY_INTERFACE_MODE_MII)
908 hip04_config_port(ndev, SPEED_100, DUPLEX_FULL);
909
910 hip04_config_fifo(priv);
Joe Perches6c1f0a12018-06-22 10:51:00 -0700911 eth_random_addr(ndev->dev_addr);
dingtianhonga41ea462015-01-14 14:34:14 +0800912 hip04_update_mac_address(ndev);
913
914 ret = hip04_alloc_ring(ndev, d);
915 if (ret) {
916 netdev_err(ndev, "alloc ring fail\n");
917 goto alloc_fail;
918 }
919
920 ret = register_netdev(ndev);
Pan Bian3b545582018-11-28 15:30:24 +0800921 if (ret)
dingtianhonga41ea462015-01-14 14:34:14 +0800922 goto alloc_fail;
dingtianhonga41ea462015-01-14 14:34:14 +0800923
924 return 0;
925
926alloc_fail:
927 hip04_free_ring(ndev, d);
928init_fail:
929 of_node_put(priv->phy_node);
930 free_netdev(ndev);
931 return ret;
932}
933
934static int hip04_remove(struct platform_device *pdev)
935{
936 struct net_device *ndev = platform_get_drvdata(pdev);
937 struct hip04_priv *priv = netdev_priv(ndev);
938 struct device *d = &pdev->dev;
939
940 if (priv->phy)
941 phy_disconnect(priv->phy);
942
943 hip04_free_ring(ndev, d);
944 unregister_netdev(ndev);
945 free_irq(ndev->irq, ndev);
946 of_node_put(priv->phy_node);
947 cancel_work_sync(&priv->tx_timeout_task);
948 free_netdev(ndev);
949
950 return 0;
951}
952
953static const struct of_device_id hip04_mac_match[] = {
954 { .compatible = "hisilicon,hip04-mac" },
955 { }
956};
957
958MODULE_DEVICE_TABLE(of, hip04_mac_match);
959
960static struct platform_driver hip04_mac_driver = {
961 .probe = hip04_mac_probe,
962 .remove = hip04_remove,
963 .driver = {
964 .name = DRV_NAME,
dingtianhonga41ea462015-01-14 14:34:14 +0800965 .of_match_table = hip04_mac_match,
966 },
967};
968module_platform_driver(hip04_mac_driver);
969
970MODULE_DESCRIPTION("HISILICON P04 Ethernet driver");
Arnd Bergmann4c0c46b2015-01-30 22:57:01 +0100971MODULE_LICENSE("GPL");