net: ravb: Always process TX descriptor ring
[sfrench/cifs-2.6.git] / drivers / net / ethernet / renesas / ravb_main.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Renesas Ethernet AVB device driver
3  *
4  * Copyright (C) 2014-2019 Renesas Electronics Corporation
5  * Copyright (C) 2015 Renesas Solutions Corp.
6  * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
7  *
8  * Based on the SuperH Ethernet driver
9  */
10
11 #include <linux/cache.h>
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/err.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/if_vlan.h>
19 #include <linux/kernel.h>
20 #include <linux/list.h>
21 #include <linux/module.h>
22 #include <linux/net_tstamp.h>
23 #include <linux/of.h>
24 #include <linux/of_mdio.h>
25 #include <linux/of_net.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/slab.h>
29 #include <linux/spinlock.h>
30 #include <linux/reset.h>
31 #include <linux/math64.h>
32 #include <net/ip.h>
33
34 #include "ravb.h"
35
36 #define RAVB_DEF_MSG_ENABLE \
37                 (NETIF_MSG_LINK   | \
38                  NETIF_MSG_TIMER  | \
39                  NETIF_MSG_RX_ERR | \
40                  NETIF_MSG_TX_ERR)
41
42 void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
43                  u32 set)
44 {
45         ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
46 }
47
48 int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
49 {
50         int i;
51
52         for (i = 0; i < 10000; i++) {
53                 if ((ravb_read(ndev, reg) & mask) == value)
54                         return 0;
55                 udelay(10);
56         }
57         return -ETIMEDOUT;
58 }
59
60 static int ravb_set_opmode(struct net_device *ndev, u32 opmode)
61 {
62         u32 csr_ops = 1U << (opmode & CCC_OPC);
63         u32 ccc_mask = CCC_OPC;
64         int error;
65
66         /* If gPTP active in config mode is supported it needs to be configured
67          * along with CSEL and operating mode in the same access. This is a
68          * hardware limitation.
69          */
70         if (opmode & CCC_GAC)
71                 ccc_mask |= CCC_GAC | CCC_CSEL;
72
73         /* Set operating mode */
74         ravb_modify(ndev, CCC, ccc_mask, opmode);
75         /* Check if the operating mode is changed to the requested one */
76         error = ravb_wait(ndev, CSR, CSR_OPS, csr_ops);
77         if (error) {
78                 netdev_err(ndev, "failed to switch device to requested mode (%u)\n",
79                            opmode & CCC_OPC);
80         }
81
82         return error;
83 }
84
85 static void ravb_set_rate_gbeth(struct net_device *ndev)
86 {
87         struct ravb_private *priv = netdev_priv(ndev);
88
89         switch (priv->speed) {
90         case 10:                /* 10BASE */
91                 ravb_write(ndev, GBETH_GECMR_SPEED_10, GECMR);
92                 break;
93         case 100:               /* 100BASE */
94                 ravb_write(ndev, GBETH_GECMR_SPEED_100, GECMR);
95                 break;
96         case 1000:              /* 1000BASE */
97                 ravb_write(ndev, GBETH_GECMR_SPEED_1000, GECMR);
98                 break;
99         }
100 }
101
102 static void ravb_set_rate_rcar(struct net_device *ndev)
103 {
104         struct ravb_private *priv = netdev_priv(ndev);
105
106         switch (priv->speed) {
107         case 100:               /* 100BASE */
108                 ravb_write(ndev, GECMR_SPEED_100, GECMR);
109                 break;
110         case 1000:              /* 1000BASE */
111                 ravb_write(ndev, GECMR_SPEED_1000, GECMR);
112                 break;
113         }
114 }
115
116 static struct sk_buff *
117 ravb_alloc_skb(struct net_device *ndev, const struct ravb_hw_info *info,
118                gfp_t gfp_mask)
119 {
120         struct sk_buff *skb;
121         u32 reserve;
122
123         skb = __netdev_alloc_skb(ndev, info->rx_max_frame_size + RAVB_ALIGN - 1,
124                                  gfp_mask);
125         if (!skb)
126                 return NULL;
127
128         reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
129         if (reserve)
130                 skb_reserve(skb, RAVB_ALIGN - reserve);
131
132         return skb;
133 }
134
135 /* Get MAC address from the MAC address registers
136  *
137  * Ethernet AVB device doesn't have ROM for MAC address.
138  * This function gets the MAC address that was used by a bootloader.
139  */
140 static void ravb_read_mac_address(struct device_node *np,
141                                   struct net_device *ndev)
142 {
143         int ret;
144
145         ret = of_get_ethdev_address(np, ndev);
146         if (ret) {
147                 u32 mahr = ravb_read(ndev, MAHR);
148                 u32 malr = ravb_read(ndev, MALR);
149                 u8 addr[ETH_ALEN];
150
151                 addr[0] = (mahr >> 24) & 0xFF;
152                 addr[1] = (mahr >> 16) & 0xFF;
153                 addr[2] = (mahr >>  8) & 0xFF;
154                 addr[3] = (mahr >>  0) & 0xFF;
155                 addr[4] = (malr >>  8) & 0xFF;
156                 addr[5] = (malr >>  0) & 0xFF;
157                 eth_hw_addr_set(ndev, addr);
158         }
159 }
160
161 static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
162 {
163         struct ravb_private *priv = container_of(ctrl, struct ravb_private,
164                                                  mdiobb);
165
166         ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
167 }
168
169 /* MDC pin control */
170 static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
171 {
172         ravb_mdio_ctrl(ctrl, PIR_MDC, level);
173 }
174
175 /* Data I/O pin control */
176 static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
177 {
178         ravb_mdio_ctrl(ctrl, PIR_MMD, output);
179 }
180
181 /* Set data bit */
182 static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
183 {
184         ravb_mdio_ctrl(ctrl, PIR_MDO, value);
185 }
186
187 /* Get data bit */
188 static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
189 {
190         struct ravb_private *priv = container_of(ctrl, struct ravb_private,
191                                                  mdiobb);
192
193         return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
194 }
195
196 /* MDIO bus control struct */
197 static const struct mdiobb_ops bb_ops = {
198         .owner = THIS_MODULE,
199         .set_mdc = ravb_set_mdc,
200         .set_mdio_dir = ravb_set_mdio_dir,
201         .set_mdio_data = ravb_set_mdio_data,
202         .get_mdio_data = ravb_get_mdio_data,
203 };
204
205 static struct ravb_rx_desc *
206 ravb_rx_get_desc(struct ravb_private *priv, unsigned int q,
207                  unsigned int i)
208 {
209         return priv->rx_ring[q].raw + priv->info->rx_desc_size * i;
210 }
211
212 /* Free TX skb function for AVB-IP */
213 static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only)
214 {
215         struct ravb_private *priv = netdev_priv(ndev);
216         struct net_device_stats *stats = &priv->stats[q];
217         unsigned int num_tx_desc = priv->num_tx_desc;
218         struct ravb_tx_desc *desc;
219         unsigned int entry;
220         int free_num = 0;
221         u32 size;
222
223         for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
224                 bool txed;
225
226                 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
227                                              num_tx_desc);
228                 desc = &priv->tx_ring[q][entry];
229                 txed = desc->die_dt == DT_FEMPTY;
230                 if (free_txed_only && !txed)
231                         break;
232                 /* Descriptor type must be checked before all other reads */
233                 dma_rmb();
234                 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
235                 /* Free the original skb. */
236                 if (priv->tx_skb[q][entry / num_tx_desc]) {
237                         dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
238                                          size, DMA_TO_DEVICE);
239                         /* Last packet descriptor? */
240                         if (entry % num_tx_desc == num_tx_desc - 1) {
241                                 entry /= num_tx_desc;
242                                 dev_kfree_skb_any(priv->tx_skb[q][entry]);
243                                 priv->tx_skb[q][entry] = NULL;
244                                 if (txed)
245                                         stats->tx_packets++;
246                         }
247                         free_num++;
248                 }
249                 if (txed)
250                         stats->tx_bytes += size;
251                 desc->die_dt = DT_EEMPTY;
252         }
253         return free_num;
254 }
255
256 static void ravb_rx_ring_free(struct net_device *ndev, int q)
257 {
258         struct ravb_private *priv = netdev_priv(ndev);
259         unsigned int ring_size;
260         unsigned int i;
261
262         if (!priv->rx_ring[q].raw)
263                 return;
264
265         for (i = 0; i < priv->num_rx_ring[q]; i++) {
266                 struct ravb_rx_desc *desc = ravb_rx_get_desc(priv, q, i);
267
268                 if (!dma_mapping_error(ndev->dev.parent,
269                                        le32_to_cpu(desc->dptr)))
270                         dma_unmap_single(ndev->dev.parent,
271                                          le32_to_cpu(desc->dptr),
272                                          priv->info->rx_max_frame_size,
273                                          DMA_FROM_DEVICE);
274         }
275         ring_size = priv->info->rx_desc_size * (priv->num_rx_ring[q] + 1);
276         dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q].raw,
277                           priv->rx_desc_dma[q]);
278         priv->rx_ring[q].raw = NULL;
279 }
280
281 /* Free skb's and DMA buffers for Ethernet AVB */
282 static void ravb_ring_free(struct net_device *ndev, int q)
283 {
284         struct ravb_private *priv = netdev_priv(ndev);
285         unsigned int num_tx_desc = priv->num_tx_desc;
286         unsigned int ring_size;
287         unsigned int i;
288
289         ravb_rx_ring_free(ndev, q);
290
291         if (priv->tx_ring[q]) {
292                 ravb_tx_free(ndev, q, false);
293
294                 ring_size = sizeof(struct ravb_tx_desc) *
295                             (priv->num_tx_ring[q] * num_tx_desc + 1);
296                 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
297                                   priv->tx_desc_dma[q]);
298                 priv->tx_ring[q] = NULL;
299         }
300
301         /* Free RX skb ringbuffer */
302         if (priv->rx_skb[q]) {
303                 for (i = 0; i < priv->num_rx_ring[q]; i++)
304                         dev_kfree_skb(priv->rx_skb[q][i]);
305         }
306         kfree(priv->rx_skb[q]);
307         priv->rx_skb[q] = NULL;
308
309         /* Free aligned TX buffers */
310         kfree(priv->tx_align[q]);
311         priv->tx_align[q] = NULL;
312
313         /* Free TX skb ringbuffer.
314          * SKBs are freed by ravb_tx_free() call above.
315          */
316         kfree(priv->tx_skb[q]);
317         priv->tx_skb[q] = NULL;
318 }
319
320 static void ravb_rx_ring_format(struct net_device *ndev, int q)
321 {
322         struct ravb_private *priv = netdev_priv(ndev);
323         struct ravb_rx_desc *rx_desc;
324         unsigned int rx_ring_size;
325         dma_addr_t dma_addr;
326         unsigned int i;
327
328         rx_ring_size = priv->info->rx_desc_size * priv->num_rx_ring[q];
329         memset(priv->rx_ring[q].raw, 0, rx_ring_size);
330         /* Build RX ring buffer */
331         for (i = 0; i < priv->num_rx_ring[q]; i++) {
332                 /* RX descriptor */
333                 rx_desc = ravb_rx_get_desc(priv, q, i);
334                 rx_desc->ds_cc = cpu_to_le16(priv->info->rx_max_desc_use);
335                 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
336                                           priv->info->rx_max_frame_size,
337                                           DMA_FROM_DEVICE);
338                 /* We just set the data size to 0 for a failed mapping which
339                  * should prevent DMA from happening...
340                  */
341                 if (dma_mapping_error(ndev->dev.parent, dma_addr))
342                         rx_desc->ds_cc = cpu_to_le16(0);
343                 rx_desc->dptr = cpu_to_le32(dma_addr);
344                 rx_desc->die_dt = DT_FEMPTY;
345         }
346         rx_desc = ravb_rx_get_desc(priv, q, i);
347         rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
348         rx_desc->die_dt = DT_LINKFIX; /* type */
349 }
350
351 /* Format skb and descriptor buffer for Ethernet AVB */
352 static void ravb_ring_format(struct net_device *ndev, int q)
353 {
354         struct ravb_private *priv = netdev_priv(ndev);
355         unsigned int num_tx_desc = priv->num_tx_desc;
356         struct ravb_tx_desc *tx_desc;
357         struct ravb_desc *desc;
358         unsigned int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
359                                     num_tx_desc;
360         unsigned int i;
361
362         priv->cur_rx[q] = 0;
363         priv->cur_tx[q] = 0;
364         priv->dirty_rx[q] = 0;
365         priv->dirty_tx[q] = 0;
366
367         ravb_rx_ring_format(ndev, q);
368
369         memset(priv->tx_ring[q], 0, tx_ring_size);
370         /* Build TX ring buffer */
371         for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
372              i++, tx_desc++) {
373                 tx_desc->die_dt = DT_EEMPTY;
374                 if (num_tx_desc > 1) {
375                         tx_desc++;
376                         tx_desc->die_dt = DT_EEMPTY;
377                 }
378         }
379         tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
380         tx_desc->die_dt = DT_LINKFIX; /* type */
381
382         /* RX descriptor base address for best effort */
383         desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
384         desc->die_dt = DT_LINKFIX; /* type */
385         desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
386
387         /* TX descriptor base address for best effort */
388         desc = &priv->desc_bat[q];
389         desc->die_dt = DT_LINKFIX; /* type */
390         desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
391 }
392
393 static void *ravb_alloc_rx_desc(struct net_device *ndev, int q)
394 {
395         struct ravb_private *priv = netdev_priv(ndev);
396         unsigned int ring_size;
397
398         ring_size = priv->info->rx_desc_size * (priv->num_rx_ring[q] + 1);
399
400         priv->rx_ring[q].raw = dma_alloc_coherent(ndev->dev.parent, ring_size,
401                                                   &priv->rx_desc_dma[q],
402                                                   GFP_KERNEL);
403
404         return priv->rx_ring[q].raw;
405 }
406
407 /* Init skb and descriptor buffer for Ethernet AVB */
408 static int ravb_ring_init(struct net_device *ndev, int q)
409 {
410         struct ravb_private *priv = netdev_priv(ndev);
411         const struct ravb_hw_info *info = priv->info;
412         unsigned int num_tx_desc = priv->num_tx_desc;
413         unsigned int ring_size;
414         struct sk_buff *skb;
415         unsigned int i;
416
417         /* Allocate RX and TX skb rings */
418         priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
419                                   sizeof(*priv->rx_skb[q]), GFP_KERNEL);
420         priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
421                                   sizeof(*priv->tx_skb[q]), GFP_KERNEL);
422         if (!priv->rx_skb[q] || !priv->tx_skb[q])
423                 goto error;
424
425         for (i = 0; i < priv->num_rx_ring[q]; i++) {
426                 skb = ravb_alloc_skb(ndev, info, GFP_KERNEL);
427                 if (!skb)
428                         goto error;
429                 priv->rx_skb[q][i] = skb;
430         }
431
432         if (num_tx_desc > 1) {
433                 /* Allocate rings for the aligned buffers */
434                 priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
435                                             DPTR_ALIGN - 1, GFP_KERNEL);
436                 if (!priv->tx_align[q])
437                         goto error;
438         }
439
440         /* Allocate all RX descriptors. */
441         if (!ravb_alloc_rx_desc(ndev, q))
442                 goto error;
443
444         priv->dirty_rx[q] = 0;
445
446         /* Allocate all TX descriptors. */
447         ring_size = sizeof(struct ravb_tx_desc) *
448                     (priv->num_tx_ring[q] * num_tx_desc + 1);
449         priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
450                                               &priv->tx_desc_dma[q],
451                                               GFP_KERNEL);
452         if (!priv->tx_ring[q])
453                 goto error;
454
455         return 0;
456
457 error:
458         ravb_ring_free(ndev, q);
459
460         return -ENOMEM;
461 }
462
463 static void ravb_csum_init_gbeth(struct net_device *ndev)
464 {
465         bool tx_enable = ndev->features & NETIF_F_HW_CSUM;
466         bool rx_enable = ndev->features & NETIF_F_RXCSUM;
467
468         if (!(tx_enable || rx_enable))
469                 goto done;
470
471         ravb_write(ndev, 0, CSR0);
472         if (ravb_wait(ndev, CSR0, CSR0_TPE | CSR0_RPE, 0)) {
473                 netdev_err(ndev, "Timeout enabling hardware checksum\n");
474
475                 if (tx_enable)
476                         ndev->features &= ~NETIF_F_HW_CSUM;
477
478                 if (rx_enable)
479                         ndev->features &= ~NETIF_F_RXCSUM;
480         } else {
481                 if (tx_enable)
482                         ravb_write(ndev, CSR1_TIP4 | CSR1_TTCP4 | CSR1_TUDP4, CSR1);
483
484                 if (rx_enable)
485                         ravb_write(ndev, CSR2_RIP4 | CSR2_RTCP4 | CSR2_RUDP4 | CSR2_RICMP4,
486                                    CSR2);
487         }
488
489 done:
490         ravb_write(ndev, CSR0_TPE | CSR0_RPE, CSR0);
491 }
492
493 static void ravb_emac_init_gbeth(struct net_device *ndev)
494 {
495         struct ravb_private *priv = netdev_priv(ndev);
496
497         if (priv->phy_interface == PHY_INTERFACE_MODE_MII) {
498                 ravb_write(ndev, (1000 << 16) | CXR35_SEL_XMII_MII, CXR35);
499                 ravb_modify(ndev, CXR31, CXR31_SEL_LINK0 | CXR31_SEL_LINK1, 0);
500         } else {
501                 ravb_write(ndev, (1000 << 16) | CXR35_SEL_XMII_RGMII, CXR35);
502                 ravb_modify(ndev, CXR31, CXR31_SEL_LINK0 | CXR31_SEL_LINK1,
503                             CXR31_SEL_LINK0);
504         }
505
506         /* Receive frame limit set register */
507         ravb_write(ndev, priv->info->rx_max_frame_size + ETH_FCS_LEN, RFLR);
508
509         /* EMAC Mode: PAUSE prohibition; Duplex; TX; RX; CRC Pass Through */
510         ravb_write(ndev, ECMR_ZPF | ((priv->duplex > 0) ? ECMR_DM : 0) |
511                          ECMR_TE | ECMR_RE | ECMR_RCPT |
512                          ECMR_TXF | ECMR_RXF, ECMR);
513
514         ravb_set_rate_gbeth(ndev);
515
516         /* Set MAC address */
517         ravb_write(ndev,
518                    (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
519                    (ndev->dev_addr[2] << 8)  | (ndev->dev_addr[3]), MAHR);
520         ravb_write(ndev, (ndev->dev_addr[4] << 8)  | (ndev->dev_addr[5]), MALR);
521
522         /* E-MAC status register clear */
523         ravb_write(ndev, ECSR_ICD | ECSR_LCHNG | ECSR_PFRI, ECSR);
524
525         ravb_csum_init_gbeth(ndev);
526
527         /* E-MAC interrupt enable register */
528         ravb_write(ndev, ECSIPR_ICDIP, ECSIPR);
529 }
530
531 static void ravb_emac_init_rcar(struct net_device *ndev)
532 {
533         /* Receive frame limit set register */
534         ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
535
536         /* EMAC Mode: PAUSE prohibition; Duplex; RX Checksum; TX; RX */
537         ravb_write(ndev, ECMR_ZPF | ECMR_DM |
538                    (ndev->features & NETIF_F_RXCSUM ? ECMR_RCSC : 0) |
539                    ECMR_TE | ECMR_RE, ECMR);
540
541         ravb_set_rate_rcar(ndev);
542
543         /* Set MAC address */
544         ravb_write(ndev,
545                    (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
546                    (ndev->dev_addr[2] << 8)  | (ndev->dev_addr[3]), MAHR);
547         ravb_write(ndev,
548                    (ndev->dev_addr[4] << 8)  | (ndev->dev_addr[5]), MALR);
549
550         /* E-MAC status register clear */
551         ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
552
553         /* E-MAC interrupt enable register */
554         ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
555 }
556
557 /* E-MAC init function */
558 static void ravb_emac_init(struct net_device *ndev)
559 {
560         struct ravb_private *priv = netdev_priv(ndev);
561         const struct ravb_hw_info *info = priv->info;
562
563         info->emac_init(ndev);
564 }
565
566 static int ravb_dmac_init_gbeth(struct net_device *ndev)
567 {
568         struct ravb_private *priv = netdev_priv(ndev);
569         int error;
570
571         error = ravb_ring_init(ndev, RAVB_BE);
572         if (error)
573                 return error;
574
575         /* Descriptor format */
576         ravb_ring_format(ndev, RAVB_BE);
577
578         /* Set DMAC RX */
579         ravb_write(ndev, 0x60000000, RCR);
580
581         /* Set Max Frame Length (RTC) */
582         ravb_write(ndev, 0x7ffc0000 | priv->info->rx_max_frame_size, RTC);
583
584         /* Set FIFO size */
585         ravb_write(ndev, 0x00222200, TGC);
586
587         ravb_write(ndev, 0, TCCR);
588
589         /* Frame receive */
590         ravb_write(ndev, RIC0_FRE0, RIC0);
591         /* Disable FIFO full warning */
592         ravb_write(ndev, 0x0, RIC1);
593         /* Receive FIFO full error, descriptor empty */
594         ravb_write(ndev, RIC2_QFE0 | RIC2_RFFE, RIC2);
595
596         ravb_write(ndev, TIC_FTE0, TIC);
597
598         return 0;
599 }
600
601 static int ravb_dmac_init_rcar(struct net_device *ndev)
602 {
603         struct ravb_private *priv = netdev_priv(ndev);
604         const struct ravb_hw_info *info = priv->info;
605         int error;
606
607         error = ravb_ring_init(ndev, RAVB_BE);
608         if (error)
609                 return error;
610         error = ravb_ring_init(ndev, RAVB_NC);
611         if (error) {
612                 ravb_ring_free(ndev, RAVB_BE);
613                 return error;
614         }
615
616         /* Descriptor format */
617         ravb_ring_format(ndev, RAVB_BE);
618         ravb_ring_format(ndev, RAVB_NC);
619
620         /* Set AVB RX */
621         ravb_write(ndev,
622                    RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
623
624         /* Set FIFO size */
625         ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00112200, TGC);
626
627         /* Timestamp enable */
628         ravb_write(ndev, TCCR_TFEN, TCCR);
629
630         /* Interrupt init: */
631         if (info->multi_irqs) {
632                 /* Clear DIL.DPLx */
633                 ravb_write(ndev, 0, DIL);
634                 /* Set queue specific interrupt */
635                 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
636         }
637         /* Frame receive */
638         ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
639         /* Disable FIFO full warning */
640         ravb_write(ndev, 0, RIC1);
641         /* Receive FIFO full error, descriptor empty */
642         ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
643         /* Frame transmitted, timestamp FIFO updated */
644         ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
645
646         return 0;
647 }
648
649 /* Device init function for Ethernet AVB */
650 static int ravb_dmac_init(struct net_device *ndev)
651 {
652         struct ravb_private *priv = netdev_priv(ndev);
653         const struct ravb_hw_info *info = priv->info;
654         int error;
655
656         /* Set CONFIG mode */
657         error = ravb_set_opmode(ndev, CCC_OPC_CONFIG);
658         if (error)
659                 return error;
660
661         error = info->dmac_init(ndev);
662         if (error)
663                 return error;
664
665         /* Setting the control will start the AVB-DMAC process. */
666         return ravb_set_opmode(ndev, CCC_OPC_OPERATION);
667 }
668
669 static void ravb_get_tx_tstamp(struct net_device *ndev)
670 {
671         struct ravb_private *priv = netdev_priv(ndev);
672         struct ravb_tstamp_skb *ts_skb, *ts_skb2;
673         struct skb_shared_hwtstamps shhwtstamps;
674         struct sk_buff *skb;
675         struct timespec64 ts;
676         u16 tag, tfa_tag;
677         int count;
678         u32 tfa2;
679
680         count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
681         while (count--) {
682                 tfa2 = ravb_read(ndev, TFA2);
683                 tfa_tag = (tfa2 & TFA2_TST) >> 16;
684                 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
685                 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
686                             ravb_read(ndev, TFA1);
687                 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
688                 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
689                 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
690                                          list) {
691                         skb = ts_skb->skb;
692                         tag = ts_skb->tag;
693                         list_del(&ts_skb->list);
694                         kfree(ts_skb);
695                         if (tag == tfa_tag) {
696                                 skb_tstamp_tx(skb, &shhwtstamps);
697                                 dev_consume_skb_any(skb);
698                                 break;
699                         } else {
700                                 dev_kfree_skb_any(skb);
701                         }
702                 }
703                 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
704         }
705 }
706
707 static void ravb_rx_csum_gbeth(struct sk_buff *skb)
708 {
709         __wsum csum_ip_hdr, csum_proto;
710         u8 *hw_csum;
711
712         /* The hardware checksum status is contained in sizeof(__sum16) * 2 = 4
713          * bytes appended to packet data. First 2 bytes is ip header checksum
714          * and last 2 bytes is protocol checksum.
715          */
716         if (unlikely(skb->len < sizeof(__sum16) * 2))
717                 return;
718
719         hw_csum = skb_tail_pointer(skb) - sizeof(__sum16);
720         csum_proto = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
721
722         hw_csum -= sizeof(__sum16);
723         csum_ip_hdr = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
724         skb_trim(skb, skb->len - 2 * sizeof(__sum16));
725
726         /* TODO: IPV6 Rx checksum */
727         if (skb->protocol == htons(ETH_P_IP) && !csum_ip_hdr && !csum_proto)
728                 skb->ip_summed = CHECKSUM_UNNECESSARY;
729 }
730
731 static void ravb_rx_csum(struct sk_buff *skb)
732 {
733         u8 *hw_csum;
734
735         /* The hardware checksum is contained in sizeof(__sum16) (2) bytes
736          * appended to packet data
737          */
738         if (unlikely(skb->len < sizeof(__sum16)))
739                 return;
740         hw_csum = skb_tail_pointer(skb) - sizeof(__sum16);
741         skb->csum = csum_unfold((__force __sum16)get_unaligned_le16(hw_csum));
742         skb->ip_summed = CHECKSUM_COMPLETE;
743         skb_trim(skb, skb->len - sizeof(__sum16));
744 }
745
746 static struct sk_buff *ravb_get_skb_gbeth(struct net_device *ndev, int entry,
747                                           struct ravb_rx_desc *desc)
748 {
749         struct ravb_private *priv = netdev_priv(ndev);
750         struct sk_buff *skb;
751
752         skb = priv->rx_skb[RAVB_BE][entry];
753         priv->rx_skb[RAVB_BE][entry] = NULL;
754         dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
755                          ALIGN(priv->info->rx_max_frame_size, 16),
756                          DMA_FROM_DEVICE);
757
758         return skb;
759 }
760
761 /* Packet receive function for Gigabit Ethernet */
762 static bool ravb_rx_gbeth(struct net_device *ndev, int *quota, int q)
763 {
764         struct ravb_private *priv = netdev_priv(ndev);
765         const struct ravb_hw_info *info = priv->info;
766         struct net_device_stats *stats;
767         struct ravb_rx_desc *desc;
768         struct sk_buff *skb;
769         dma_addr_t dma_addr;
770         int rx_packets = 0;
771         u8  desc_status;
772         u16 pkt_len;
773         u8  die_dt;
774         int entry;
775         int limit;
776         int i;
777
778         entry = priv->cur_rx[q] % priv->num_rx_ring[q];
779         limit = priv->dirty_rx[q] + priv->num_rx_ring[q] - priv->cur_rx[q];
780         stats = &priv->stats[q];
781
782         desc = &priv->rx_ring[q].desc[entry];
783         for (i = 0; i < limit && rx_packets < *quota && desc->die_dt != DT_FEMPTY; i++) {
784                 /* Descriptor type must be checked before all other reads */
785                 dma_rmb();
786                 desc_status = desc->msc;
787                 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
788
789                 /* We use 0-byte descriptors to mark the DMA mapping errors */
790                 if (!pkt_len)
791                         continue;
792
793                 if (desc_status & MSC_MC)
794                         stats->multicast++;
795
796                 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF | MSC_CEEF)) {
797                         stats->rx_errors++;
798                         if (desc_status & MSC_CRC)
799                                 stats->rx_crc_errors++;
800                         if (desc_status & MSC_RFE)
801                                 stats->rx_frame_errors++;
802                         if (desc_status & (MSC_RTLF | MSC_RTSF))
803                                 stats->rx_length_errors++;
804                         if (desc_status & MSC_CEEF)
805                                 stats->rx_missed_errors++;
806                 } else {
807                         die_dt = desc->die_dt & 0xF0;
808                         switch (die_dt) {
809                         case DT_FSINGLE:
810                                 skb = ravb_get_skb_gbeth(ndev, entry, desc);
811                                 skb_put(skb, pkt_len);
812                                 skb->protocol = eth_type_trans(skb, ndev);
813                                 if (ndev->features & NETIF_F_RXCSUM)
814                                         ravb_rx_csum_gbeth(skb);
815                                 napi_gro_receive(&priv->napi[q], skb);
816                                 rx_packets++;
817                                 stats->rx_bytes += pkt_len;
818                                 break;
819                         case DT_FSTART:
820                                 priv->rx_1st_skb = ravb_get_skb_gbeth(ndev, entry, desc);
821                                 skb_put(priv->rx_1st_skb, pkt_len);
822                                 break;
823                         case DT_FMID:
824                                 skb = ravb_get_skb_gbeth(ndev, entry, desc);
825                                 skb_copy_to_linear_data_offset(priv->rx_1st_skb,
826                                                                priv->rx_1st_skb->len,
827                                                                skb->data,
828                                                                pkt_len);
829                                 skb_put(priv->rx_1st_skb, pkt_len);
830                                 dev_kfree_skb(skb);
831                                 break;
832                         case DT_FEND:
833                                 skb = ravb_get_skb_gbeth(ndev, entry, desc);
834                                 skb_copy_to_linear_data_offset(priv->rx_1st_skb,
835                                                                priv->rx_1st_skb->len,
836                                                                skb->data,
837                                                                pkt_len);
838                                 skb_put(priv->rx_1st_skb, pkt_len);
839                                 dev_kfree_skb(skb);
840                                 priv->rx_1st_skb->protocol =
841                                         eth_type_trans(priv->rx_1st_skb, ndev);
842                                 if (ndev->features & NETIF_F_RXCSUM)
843                                         ravb_rx_csum_gbeth(skb);
844                                 napi_gro_receive(&priv->napi[q],
845                                                  priv->rx_1st_skb);
846                                 rx_packets++;
847                                 stats->rx_bytes += pkt_len;
848                                 break;
849                         }
850                 }
851
852                 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
853                 desc = &priv->rx_ring[q].desc[entry];
854         }
855
856         /* Refill the RX ring buffers. */
857         for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
858                 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
859                 desc = &priv->rx_ring[q].desc[entry];
860                 desc->ds_cc = cpu_to_le16(priv->info->rx_max_desc_use);
861
862                 if (!priv->rx_skb[q][entry]) {
863                         skb = ravb_alloc_skb(ndev, info, GFP_ATOMIC);
864                         if (!skb)
865                                 break;
866                         dma_addr = dma_map_single(ndev->dev.parent,
867                                                   skb->data,
868                                                   priv->info->rx_max_frame_size,
869                                                   DMA_FROM_DEVICE);
870                         skb_checksum_none_assert(skb);
871                         /* We just set the data size to 0 for a failed mapping
872                          * which should prevent DMA  from happening...
873                          */
874                         if (dma_mapping_error(ndev->dev.parent, dma_addr))
875                                 desc->ds_cc = cpu_to_le16(0);
876                         desc->dptr = cpu_to_le32(dma_addr);
877                         priv->rx_skb[q][entry] = skb;
878                 }
879                 /* Descriptor type must be set after all the above writes */
880                 dma_wmb();
881                 desc->die_dt = DT_FEMPTY;
882         }
883
884         stats->rx_packets += rx_packets;
885         *quota -= rx_packets;
886         return *quota == 0;
887 }
888
889 /* Packet receive function for Ethernet AVB */
890 static bool ravb_rx_rcar(struct net_device *ndev, int *quota, int q)
891 {
892         struct ravb_private *priv = netdev_priv(ndev);
893         const struct ravb_hw_info *info = priv->info;
894         int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
895         int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
896                         priv->cur_rx[q];
897         struct net_device_stats *stats = &priv->stats[q];
898         struct ravb_ex_rx_desc *desc;
899         struct sk_buff *skb;
900         dma_addr_t dma_addr;
901         struct timespec64 ts;
902         u8  desc_status;
903         u16 pkt_len;
904         int limit;
905
906         boguscnt = min(boguscnt, *quota);
907         limit = boguscnt;
908         desc = &priv->rx_ring[q].ex_desc[entry];
909         while (desc->die_dt != DT_FEMPTY) {
910                 /* Descriptor type must be checked before all other reads */
911                 dma_rmb();
912                 desc_status = desc->msc;
913                 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
914
915                 if (--boguscnt < 0)
916                         break;
917
918                 /* We use 0-byte descriptors to mark the DMA mapping errors */
919                 if (!pkt_len)
920                         continue;
921
922                 if (desc_status & MSC_MC)
923                         stats->multicast++;
924
925                 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
926                                    MSC_CEEF)) {
927                         stats->rx_errors++;
928                         if (desc_status & MSC_CRC)
929                                 stats->rx_crc_errors++;
930                         if (desc_status & MSC_RFE)
931                                 stats->rx_frame_errors++;
932                         if (desc_status & (MSC_RTLF | MSC_RTSF))
933                                 stats->rx_length_errors++;
934                         if (desc_status & MSC_CEEF)
935                                 stats->rx_missed_errors++;
936                 } else {
937                         u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
938
939                         skb = priv->rx_skb[q][entry];
940                         priv->rx_skb[q][entry] = NULL;
941                         dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
942                                          priv->info->rx_max_frame_size,
943                                          DMA_FROM_DEVICE);
944                         get_ts &= (q == RAVB_NC) ?
945                                         RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
946                                         ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
947                         if (get_ts) {
948                                 struct skb_shared_hwtstamps *shhwtstamps;
949
950                                 shhwtstamps = skb_hwtstamps(skb);
951                                 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
952                                 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
953                                              32) | le32_to_cpu(desc->ts_sl);
954                                 ts.tv_nsec = le32_to_cpu(desc->ts_n);
955                                 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
956                         }
957
958                         skb_put(skb, pkt_len);
959                         skb->protocol = eth_type_trans(skb, ndev);
960                         if (ndev->features & NETIF_F_RXCSUM)
961                                 ravb_rx_csum(skb);
962                         napi_gro_receive(&priv->napi[q], skb);
963                         stats->rx_packets++;
964                         stats->rx_bytes += pkt_len;
965                 }
966
967                 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
968                 desc = &priv->rx_ring[q].ex_desc[entry];
969         }
970
971         /* Refill the RX ring buffers. */
972         for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
973                 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
974                 desc = &priv->rx_ring[q].ex_desc[entry];
975                 desc->ds_cc = cpu_to_le16(priv->info->rx_max_desc_use);
976
977                 if (!priv->rx_skb[q][entry]) {
978                         skb = ravb_alloc_skb(ndev, info, GFP_ATOMIC);
979                         if (!skb)
980                                 break;  /* Better luck next round. */
981                         dma_addr = dma_map_single(ndev->dev.parent, skb->data,
982                                                   priv->info->rx_max_frame_size,
983                                                   DMA_FROM_DEVICE);
984                         skb_checksum_none_assert(skb);
985                         /* We just set the data size to 0 for a failed mapping
986                          * which should prevent DMA  from happening...
987                          */
988                         if (dma_mapping_error(ndev->dev.parent, dma_addr))
989                                 desc->ds_cc = cpu_to_le16(0);
990                         desc->dptr = cpu_to_le32(dma_addr);
991                         priv->rx_skb[q][entry] = skb;
992                 }
993                 /* Descriptor type must be set after all the above writes */
994                 dma_wmb();
995                 desc->die_dt = DT_FEMPTY;
996         }
997
998         *quota -= limit - (++boguscnt);
999
1000         return boguscnt <= 0;
1001 }
1002
1003 /* Packet receive function for Ethernet AVB */
1004 static bool ravb_rx(struct net_device *ndev, int *quota, int q)
1005 {
1006         struct ravb_private *priv = netdev_priv(ndev);
1007         const struct ravb_hw_info *info = priv->info;
1008
1009         return info->receive(ndev, quota, q);
1010 }
1011
1012 static void ravb_rcv_snd_disable(struct net_device *ndev)
1013 {
1014         /* Disable TX and RX */
1015         ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
1016 }
1017
1018 static void ravb_rcv_snd_enable(struct net_device *ndev)
1019 {
1020         /* Enable TX and RX */
1021         ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
1022 }
1023
1024 /* function for waiting dma process finished */
1025 static int ravb_stop_dma(struct net_device *ndev)
1026 {
1027         struct ravb_private *priv = netdev_priv(ndev);
1028         const struct ravb_hw_info *info = priv->info;
1029         int error;
1030
1031         /* Wait for stopping the hardware TX process */
1032         error = ravb_wait(ndev, TCCR, info->tccr_mask, 0);
1033
1034         if (error)
1035                 return error;
1036
1037         error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
1038                           0);
1039         if (error)
1040                 return error;
1041
1042         /* Stop the E-MAC's RX/TX processes. */
1043         ravb_rcv_snd_disable(ndev);
1044
1045         /* Wait for stopping the RX DMA process */
1046         error = ravb_wait(ndev, CSR, CSR_RPO, 0);
1047         if (error)
1048                 return error;
1049
1050         /* Stop AVB-DMAC process */
1051         return ravb_set_opmode(ndev, CCC_OPC_CONFIG);
1052 }
1053
1054 /* E-MAC interrupt handler */
1055 static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
1056 {
1057         struct ravb_private *priv = netdev_priv(ndev);
1058         u32 ecsr, psr;
1059
1060         ecsr = ravb_read(ndev, ECSR);
1061         ravb_write(ndev, ecsr, ECSR);   /* clear interrupt */
1062
1063         if (ecsr & ECSR_MPD)
1064                 pm_wakeup_event(&priv->pdev->dev, 0);
1065         if (ecsr & ECSR_ICD)
1066                 ndev->stats.tx_carrier_errors++;
1067         if (ecsr & ECSR_LCHNG) {
1068                 /* Link changed */
1069                 if (priv->no_avb_link)
1070                         return;
1071                 psr = ravb_read(ndev, PSR);
1072                 if (priv->avb_link_active_low)
1073                         psr ^= PSR_LMON;
1074                 if (!(psr & PSR_LMON)) {
1075                         /* DIsable RX and TX */
1076                         ravb_rcv_snd_disable(ndev);
1077                 } else {
1078                         /* Enable RX and TX */
1079                         ravb_rcv_snd_enable(ndev);
1080                 }
1081         }
1082 }
1083
1084 static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
1085 {
1086         struct net_device *ndev = dev_id;
1087         struct ravb_private *priv = netdev_priv(ndev);
1088         struct device *dev = &priv->pdev->dev;
1089         irqreturn_t result = IRQ_HANDLED;
1090
1091         pm_runtime_get_noresume(dev);
1092
1093         if (unlikely(!pm_runtime_active(dev))) {
1094                 result = IRQ_NONE;
1095                 goto out_rpm_put;
1096         }
1097
1098         spin_lock(&priv->lock);
1099         ravb_emac_interrupt_unlocked(ndev);
1100         spin_unlock(&priv->lock);
1101
1102 out_rpm_put:
1103         pm_runtime_put_noidle(dev);
1104         return result;
1105 }
1106
1107 /* Error interrupt handler */
1108 static void ravb_error_interrupt(struct net_device *ndev)
1109 {
1110         struct ravb_private *priv = netdev_priv(ndev);
1111         u32 eis, ris2;
1112
1113         eis = ravb_read(ndev, EIS);
1114         ravb_write(ndev, ~(EIS_QFS | EIS_RESERVED), EIS);
1115         if (eis & EIS_QFS) {
1116                 ris2 = ravb_read(ndev, RIS2);
1117                 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_QFF1 | RIS2_RFFF | RIS2_RESERVED),
1118                            RIS2);
1119
1120                 /* Receive Descriptor Empty int */
1121                 if (ris2 & RIS2_QFF0)
1122                         priv->stats[RAVB_BE].rx_over_errors++;
1123
1124                 /* Receive Descriptor Empty int */
1125                 if (ris2 & RIS2_QFF1)
1126                         priv->stats[RAVB_NC].rx_over_errors++;
1127
1128                 /* Receive FIFO Overflow int */
1129                 if (ris2 & RIS2_RFFF)
1130                         priv->rx_fifo_errors++;
1131         }
1132 }
1133
1134 static bool ravb_queue_interrupt(struct net_device *ndev, int q)
1135 {
1136         struct ravb_private *priv = netdev_priv(ndev);
1137         const struct ravb_hw_info *info = priv->info;
1138         u32 ris0 = ravb_read(ndev, RIS0);
1139         u32 ric0 = ravb_read(ndev, RIC0);
1140         u32 tis  = ravb_read(ndev, TIS);
1141         u32 tic  = ravb_read(ndev, TIC);
1142
1143         if (((ris0 & ric0) & BIT(q)) || ((tis  & tic)  & BIT(q))) {
1144                 if (napi_schedule_prep(&priv->napi[q])) {
1145                         /* Mask RX and TX interrupts */
1146                         if (!info->irq_en_dis) {
1147                                 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
1148                                 ravb_write(ndev, tic & ~BIT(q), TIC);
1149                         } else {
1150                                 ravb_write(ndev, BIT(q), RID0);
1151                                 ravb_write(ndev, BIT(q), TID);
1152                         }
1153                         __napi_schedule(&priv->napi[q]);
1154                 } else {
1155                         netdev_warn(ndev,
1156                                     "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
1157                                     ris0, ric0);
1158                         netdev_warn(ndev,
1159                                     "                    tx status 0x%08x, tx mask 0x%08x.\n",
1160                                     tis, tic);
1161                 }
1162                 return true;
1163         }
1164         return false;
1165 }
1166
1167 static bool ravb_timestamp_interrupt(struct net_device *ndev)
1168 {
1169         u32 tis = ravb_read(ndev, TIS);
1170
1171         if (tis & TIS_TFUF) {
1172                 ravb_write(ndev, ~(TIS_TFUF | TIS_RESERVED), TIS);
1173                 ravb_get_tx_tstamp(ndev);
1174                 return true;
1175         }
1176         return false;
1177 }
1178
1179 static irqreturn_t ravb_interrupt(int irq, void *dev_id)
1180 {
1181         struct net_device *ndev = dev_id;
1182         struct ravb_private *priv = netdev_priv(ndev);
1183         const struct ravb_hw_info *info = priv->info;
1184         struct device *dev = &priv->pdev->dev;
1185         irqreturn_t result = IRQ_NONE;
1186         u32 iss;
1187
1188         pm_runtime_get_noresume(dev);
1189
1190         if (unlikely(!pm_runtime_active(dev)))
1191                 goto out_rpm_put;
1192
1193         spin_lock(&priv->lock);
1194         /* Get interrupt status */
1195         iss = ravb_read(ndev, ISS);
1196
1197         /* Received and transmitted interrupts */
1198         if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
1199                 int q;
1200
1201                 /* Timestamp updated */
1202                 if (ravb_timestamp_interrupt(ndev))
1203                         result = IRQ_HANDLED;
1204
1205                 /* Network control and best effort queue RX/TX */
1206                 if (info->nc_queues) {
1207                         for (q = RAVB_NC; q >= RAVB_BE; q--) {
1208                                 if (ravb_queue_interrupt(ndev, q))
1209                                         result = IRQ_HANDLED;
1210                         }
1211                 } else {
1212                         if (ravb_queue_interrupt(ndev, RAVB_BE))
1213                                 result = IRQ_HANDLED;
1214                 }
1215         }
1216
1217         /* E-MAC status summary */
1218         if (iss & ISS_MS) {
1219                 ravb_emac_interrupt_unlocked(ndev);
1220                 result = IRQ_HANDLED;
1221         }
1222
1223         /* Error status summary */
1224         if (iss & ISS_ES) {
1225                 ravb_error_interrupt(ndev);
1226                 result = IRQ_HANDLED;
1227         }
1228
1229         /* gPTP interrupt status summary */
1230         if (iss & ISS_CGIS) {
1231                 ravb_ptp_interrupt(ndev);
1232                 result = IRQ_HANDLED;
1233         }
1234
1235         spin_unlock(&priv->lock);
1236
1237 out_rpm_put:
1238         pm_runtime_put_noidle(dev);
1239         return result;
1240 }
1241
1242 /* Timestamp/Error/gPTP interrupt handler */
1243 static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
1244 {
1245         struct net_device *ndev = dev_id;
1246         struct ravb_private *priv = netdev_priv(ndev);
1247         struct device *dev = &priv->pdev->dev;
1248         irqreturn_t result = IRQ_NONE;
1249         u32 iss;
1250
1251         pm_runtime_get_noresume(dev);
1252
1253         if (unlikely(!pm_runtime_active(dev)))
1254                 goto out_rpm_put;
1255
1256         spin_lock(&priv->lock);
1257         /* Get interrupt status */
1258         iss = ravb_read(ndev, ISS);
1259
1260         /* Timestamp updated */
1261         if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
1262                 result = IRQ_HANDLED;
1263
1264         /* Error status summary */
1265         if (iss & ISS_ES) {
1266                 ravb_error_interrupt(ndev);
1267                 result = IRQ_HANDLED;
1268         }
1269
1270         /* gPTP interrupt status summary */
1271         if (iss & ISS_CGIS) {
1272                 ravb_ptp_interrupt(ndev);
1273                 result = IRQ_HANDLED;
1274         }
1275
1276         spin_unlock(&priv->lock);
1277
1278 out_rpm_put:
1279         pm_runtime_put_noidle(dev);
1280         return result;
1281 }
1282
1283 static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
1284 {
1285         struct net_device *ndev = dev_id;
1286         struct ravb_private *priv = netdev_priv(ndev);
1287         struct device *dev = &priv->pdev->dev;
1288         irqreturn_t result = IRQ_NONE;
1289
1290         pm_runtime_get_noresume(dev);
1291
1292         if (unlikely(!pm_runtime_active(dev)))
1293                 goto out_rpm_put;
1294
1295         spin_lock(&priv->lock);
1296
1297         /* Network control/Best effort queue RX/TX */
1298         if (ravb_queue_interrupt(ndev, q))
1299                 result = IRQ_HANDLED;
1300
1301         spin_unlock(&priv->lock);
1302
1303 out_rpm_put:
1304         pm_runtime_put_noidle(dev);
1305         return result;
1306 }
1307
1308 static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
1309 {
1310         return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
1311 }
1312
1313 static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
1314 {
1315         return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
1316 }
1317
1318 static int ravb_poll(struct napi_struct *napi, int budget)
1319 {
1320         struct net_device *ndev = napi->dev;
1321         struct ravb_private *priv = netdev_priv(ndev);
1322         const struct ravb_hw_info *info = priv->info;
1323         unsigned long flags;
1324         int q = napi - priv->napi;
1325         int mask = BIT(q);
1326         int quota = budget;
1327         bool unmask;
1328
1329         /* Processing RX Descriptor Ring */
1330         /* Clear RX interrupt */
1331         ravb_write(ndev, ~(mask | RIS0_RESERVED), RIS0);
1332         unmask = !ravb_rx(ndev, &quota, q);
1333
1334         /* Processing TX Descriptor Ring */
1335         spin_lock_irqsave(&priv->lock, flags);
1336         /* Clear TX interrupt */
1337         ravb_write(ndev, ~(mask | TIS_RESERVED), TIS);
1338         ravb_tx_free(ndev, q, true);
1339         netif_wake_subqueue(ndev, q);
1340         spin_unlock_irqrestore(&priv->lock, flags);
1341
1342         if (!unmask)
1343                 goto out;
1344
1345         napi_complete(napi);
1346
1347         /* Re-enable RX/TX interrupts */
1348         spin_lock_irqsave(&priv->lock, flags);
1349         if (!info->irq_en_dis) {
1350                 ravb_modify(ndev, RIC0, mask, mask);
1351                 ravb_modify(ndev, TIC,  mask, mask);
1352         } else {
1353                 ravb_write(ndev, mask, RIE0);
1354                 ravb_write(ndev, mask, TIE);
1355         }
1356         spin_unlock_irqrestore(&priv->lock, flags);
1357
1358         /* Receive error message handling */
1359         priv->rx_over_errors =  priv->stats[RAVB_BE].rx_over_errors;
1360         if (info->nc_queues)
1361                 priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
1362         if (priv->rx_over_errors != ndev->stats.rx_over_errors)
1363                 ndev->stats.rx_over_errors = priv->rx_over_errors;
1364         if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
1365                 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
1366 out:
1367         return budget - quota;
1368 }
1369
1370 static void ravb_set_duplex_gbeth(struct net_device *ndev)
1371 {
1372         struct ravb_private *priv = netdev_priv(ndev);
1373
1374         ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex > 0 ? ECMR_DM : 0);
1375 }
1376
1377 /* PHY state control function */
1378 static void ravb_adjust_link(struct net_device *ndev)
1379 {
1380         struct ravb_private *priv = netdev_priv(ndev);
1381         const struct ravb_hw_info *info = priv->info;
1382         struct phy_device *phydev = ndev->phydev;
1383         bool new_state = false;
1384         unsigned long flags;
1385
1386         spin_lock_irqsave(&priv->lock, flags);
1387
1388         /* Disable TX and RX right over here, if E-MAC change is ignored */
1389         if (priv->no_avb_link)
1390                 ravb_rcv_snd_disable(ndev);
1391
1392         if (phydev->link) {
1393                 if (info->half_duplex && phydev->duplex != priv->duplex) {
1394                         new_state = true;
1395                         priv->duplex = phydev->duplex;
1396                         ravb_set_duplex_gbeth(ndev);
1397                 }
1398
1399                 if (phydev->speed != priv->speed) {
1400                         new_state = true;
1401                         priv->speed = phydev->speed;
1402                         info->set_rate(ndev);
1403                 }
1404                 if (!priv->link) {
1405                         ravb_modify(ndev, ECMR, ECMR_TXF, 0);
1406                         new_state = true;
1407                         priv->link = phydev->link;
1408                 }
1409         } else if (priv->link) {
1410                 new_state = true;
1411                 priv->link = 0;
1412                 priv->speed = 0;
1413                 if (info->half_duplex)
1414                         priv->duplex = -1;
1415         }
1416
1417         /* Enable TX and RX right over here, if E-MAC change is ignored */
1418         if (priv->no_avb_link && phydev->link)
1419                 ravb_rcv_snd_enable(ndev);
1420
1421         spin_unlock_irqrestore(&priv->lock, flags);
1422
1423         if (new_state && netif_msg_link(priv))
1424                 phy_print_status(phydev);
1425 }
1426
1427 /* PHY init function */
1428 static int ravb_phy_init(struct net_device *ndev)
1429 {
1430         struct device_node *np = ndev->dev.parent->of_node;
1431         struct ravb_private *priv = netdev_priv(ndev);
1432         const struct ravb_hw_info *info = priv->info;
1433         struct phy_device *phydev;
1434         struct device_node *pn;
1435         phy_interface_t iface;
1436         int err;
1437
1438         priv->link = 0;
1439         priv->speed = 0;
1440         priv->duplex = -1;
1441
1442         /* Try connecting to PHY */
1443         pn = of_parse_phandle(np, "phy-handle", 0);
1444         if (!pn) {
1445                 /* In the case of a fixed PHY, the DT node associated
1446                  * to the PHY is the Ethernet MAC DT node.
1447                  */
1448                 if (of_phy_is_fixed_link(np)) {
1449                         err = of_phy_register_fixed_link(np);
1450                         if (err)
1451                                 return err;
1452                 }
1453                 pn = of_node_get(np);
1454         }
1455
1456         iface = priv->rgmii_override ? PHY_INTERFACE_MODE_RGMII
1457                                      : priv->phy_interface;
1458         phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0, iface);
1459         of_node_put(pn);
1460         if (!phydev) {
1461                 netdev_err(ndev, "failed to connect PHY\n");
1462                 err = -ENOENT;
1463                 goto err_deregister_fixed_link;
1464         }
1465
1466         if (!info->half_duplex) {
1467                 /* 10BASE, Pause and Asym Pause is not supported */
1468                 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT);
1469                 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT);
1470                 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_Pause_BIT);
1471                 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_Asym_Pause_BIT);
1472
1473                 /* Half Duplex is not supported */
1474                 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1475                 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT);
1476         }
1477
1478         phy_attached_info(phydev);
1479
1480         return 0;
1481
1482 err_deregister_fixed_link:
1483         if (of_phy_is_fixed_link(np))
1484                 of_phy_deregister_fixed_link(np);
1485
1486         return err;
1487 }
1488
1489 /* PHY control start function */
1490 static int ravb_phy_start(struct net_device *ndev)
1491 {
1492         int error;
1493
1494         error = ravb_phy_init(ndev);
1495         if (error)
1496                 return error;
1497
1498         phy_start(ndev->phydev);
1499
1500         return 0;
1501 }
1502
1503 static u32 ravb_get_msglevel(struct net_device *ndev)
1504 {
1505         struct ravb_private *priv = netdev_priv(ndev);
1506
1507         return priv->msg_enable;
1508 }
1509
1510 static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1511 {
1512         struct ravb_private *priv = netdev_priv(ndev);
1513
1514         priv->msg_enable = value;
1515 }
1516
1517 static const char ravb_gstrings_stats_gbeth[][ETH_GSTRING_LEN] = {
1518         "rx_queue_0_current",
1519         "tx_queue_0_current",
1520         "rx_queue_0_dirty",
1521         "tx_queue_0_dirty",
1522         "rx_queue_0_packets",
1523         "tx_queue_0_packets",
1524         "rx_queue_0_bytes",
1525         "tx_queue_0_bytes",
1526         "rx_queue_0_mcast_packets",
1527         "rx_queue_0_errors",
1528         "rx_queue_0_crc_errors",
1529         "rx_queue_0_frame_errors",
1530         "rx_queue_0_length_errors",
1531         "rx_queue_0_csum_offload_errors",
1532         "rx_queue_0_over_errors",
1533 };
1534
1535 static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1536         "rx_queue_0_current",
1537         "tx_queue_0_current",
1538         "rx_queue_0_dirty",
1539         "tx_queue_0_dirty",
1540         "rx_queue_0_packets",
1541         "tx_queue_0_packets",
1542         "rx_queue_0_bytes",
1543         "tx_queue_0_bytes",
1544         "rx_queue_0_mcast_packets",
1545         "rx_queue_0_errors",
1546         "rx_queue_0_crc_errors",
1547         "rx_queue_0_frame_errors",
1548         "rx_queue_0_length_errors",
1549         "rx_queue_0_missed_errors",
1550         "rx_queue_0_over_errors",
1551
1552         "rx_queue_1_current",
1553         "tx_queue_1_current",
1554         "rx_queue_1_dirty",
1555         "tx_queue_1_dirty",
1556         "rx_queue_1_packets",
1557         "tx_queue_1_packets",
1558         "rx_queue_1_bytes",
1559         "tx_queue_1_bytes",
1560         "rx_queue_1_mcast_packets",
1561         "rx_queue_1_errors",
1562         "rx_queue_1_crc_errors",
1563         "rx_queue_1_frame_errors",
1564         "rx_queue_1_length_errors",
1565         "rx_queue_1_missed_errors",
1566         "rx_queue_1_over_errors",
1567 };
1568
1569 static int ravb_get_sset_count(struct net_device *netdev, int sset)
1570 {
1571         struct ravb_private *priv = netdev_priv(netdev);
1572         const struct ravb_hw_info *info = priv->info;
1573
1574         switch (sset) {
1575         case ETH_SS_STATS:
1576                 return info->stats_len;
1577         default:
1578                 return -EOPNOTSUPP;
1579         }
1580 }
1581
1582 static void ravb_get_ethtool_stats(struct net_device *ndev,
1583                                    struct ethtool_stats *estats, u64 *data)
1584 {
1585         struct ravb_private *priv = netdev_priv(ndev);
1586         const struct ravb_hw_info *info = priv->info;
1587         int num_rx_q;
1588         int i = 0;
1589         int q;
1590
1591         num_rx_q = info->nc_queues ? NUM_RX_QUEUE : 1;
1592         /* Device-specific stats */
1593         for (q = RAVB_BE; q < num_rx_q; q++) {
1594                 struct net_device_stats *stats = &priv->stats[q];
1595
1596                 data[i++] = priv->cur_rx[q];
1597                 data[i++] = priv->cur_tx[q];
1598                 data[i++] = priv->dirty_rx[q];
1599                 data[i++] = priv->dirty_tx[q];
1600                 data[i++] = stats->rx_packets;
1601                 data[i++] = stats->tx_packets;
1602                 data[i++] = stats->rx_bytes;
1603                 data[i++] = stats->tx_bytes;
1604                 data[i++] = stats->multicast;
1605                 data[i++] = stats->rx_errors;
1606                 data[i++] = stats->rx_crc_errors;
1607                 data[i++] = stats->rx_frame_errors;
1608                 data[i++] = stats->rx_length_errors;
1609                 data[i++] = stats->rx_missed_errors;
1610                 data[i++] = stats->rx_over_errors;
1611         }
1612 }
1613
1614 static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1615 {
1616         struct ravb_private *priv = netdev_priv(ndev);
1617         const struct ravb_hw_info *info = priv->info;
1618
1619         switch (stringset) {
1620         case ETH_SS_STATS:
1621                 memcpy(data, info->gstrings_stats, info->gstrings_size);
1622                 break;
1623         }
1624 }
1625
1626 static void ravb_get_ringparam(struct net_device *ndev,
1627                                struct ethtool_ringparam *ring,
1628                                struct kernel_ethtool_ringparam *kernel_ring,
1629                                struct netlink_ext_ack *extack)
1630 {
1631         struct ravb_private *priv = netdev_priv(ndev);
1632
1633         ring->rx_max_pending = BE_RX_RING_MAX;
1634         ring->tx_max_pending = BE_TX_RING_MAX;
1635         ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1636         ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1637 }
1638
1639 static int ravb_set_ringparam(struct net_device *ndev,
1640                               struct ethtool_ringparam *ring,
1641                               struct kernel_ethtool_ringparam *kernel_ring,
1642                               struct netlink_ext_ack *extack)
1643 {
1644         struct ravb_private *priv = netdev_priv(ndev);
1645         const struct ravb_hw_info *info = priv->info;
1646         int error;
1647
1648         if (ring->tx_pending > BE_TX_RING_MAX ||
1649             ring->rx_pending > BE_RX_RING_MAX ||
1650             ring->tx_pending < BE_TX_RING_MIN ||
1651             ring->rx_pending < BE_RX_RING_MIN)
1652                 return -EINVAL;
1653         if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1654                 return -EINVAL;
1655
1656         if (netif_running(ndev)) {
1657                 netif_device_detach(ndev);
1658                 /* Stop PTP Clock driver */
1659                 if (info->gptp)
1660                         ravb_ptp_stop(ndev);
1661                 /* Wait for DMA stopping */
1662                 error = ravb_stop_dma(ndev);
1663                 if (error) {
1664                         netdev_err(ndev,
1665                                    "cannot set ringparam! Any AVB processes are still running?\n");
1666                         return error;
1667                 }
1668                 synchronize_irq(ndev->irq);
1669
1670                 /* Free all the skb's in the RX queue and the DMA buffers. */
1671                 ravb_ring_free(ndev, RAVB_BE);
1672                 if (info->nc_queues)
1673                         ravb_ring_free(ndev, RAVB_NC);
1674         }
1675
1676         /* Set new parameters */
1677         priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1678         priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1679
1680         if (netif_running(ndev)) {
1681                 error = ravb_dmac_init(ndev);
1682                 if (error) {
1683                         netdev_err(ndev,
1684                                    "%s: ravb_dmac_init() failed, error %d\n",
1685                                    __func__, error);
1686                         return error;
1687                 }
1688
1689                 ravb_emac_init(ndev);
1690
1691                 /* Initialise PTP Clock driver */
1692                 if (info->gptp)
1693                         ravb_ptp_init(ndev, priv->pdev);
1694
1695                 netif_device_attach(ndev);
1696         }
1697
1698         return 0;
1699 }
1700
1701 static int ravb_get_ts_info(struct net_device *ndev,
1702                             struct ethtool_ts_info *info)
1703 {
1704         struct ravb_private *priv = netdev_priv(ndev);
1705         const struct ravb_hw_info *hw_info = priv->info;
1706
1707         info->so_timestamping =
1708                 SOF_TIMESTAMPING_TX_SOFTWARE |
1709                 SOF_TIMESTAMPING_RX_SOFTWARE |
1710                 SOF_TIMESTAMPING_SOFTWARE |
1711                 SOF_TIMESTAMPING_TX_HARDWARE |
1712                 SOF_TIMESTAMPING_RX_HARDWARE |
1713                 SOF_TIMESTAMPING_RAW_HARDWARE;
1714         info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1715         info->rx_filters =
1716                 (1 << HWTSTAMP_FILTER_NONE) |
1717                 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1718                 (1 << HWTSTAMP_FILTER_ALL);
1719         if (hw_info->gptp || hw_info->ccc_gac)
1720                 info->phc_index = ptp_clock_index(priv->ptp.clock);
1721
1722         return 0;
1723 }
1724
1725 static void ravb_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1726 {
1727         struct ravb_private *priv = netdev_priv(ndev);
1728
1729         wol->supported = WAKE_MAGIC;
1730         wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0;
1731 }
1732
1733 static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1734 {
1735         struct ravb_private *priv = netdev_priv(ndev);
1736         const struct ravb_hw_info *info = priv->info;
1737
1738         if (!info->magic_pkt || (wol->wolopts & ~WAKE_MAGIC))
1739                 return -EOPNOTSUPP;
1740
1741         priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
1742
1743         device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled);
1744
1745         return 0;
1746 }
1747
1748 static const struct ethtool_ops ravb_ethtool_ops = {
1749         .nway_reset             = phy_ethtool_nway_reset,
1750         .get_msglevel           = ravb_get_msglevel,
1751         .set_msglevel           = ravb_set_msglevel,
1752         .get_link               = ethtool_op_get_link,
1753         .get_strings            = ravb_get_strings,
1754         .get_ethtool_stats      = ravb_get_ethtool_stats,
1755         .get_sset_count         = ravb_get_sset_count,
1756         .get_ringparam          = ravb_get_ringparam,
1757         .set_ringparam          = ravb_set_ringparam,
1758         .get_ts_info            = ravb_get_ts_info,
1759         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
1760         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
1761         .get_wol                = ravb_get_wol,
1762         .set_wol                = ravb_set_wol,
1763 };
1764
1765 static int ravb_set_config_mode(struct net_device *ndev)
1766 {
1767         struct ravb_private *priv = netdev_priv(ndev);
1768         const struct ravb_hw_info *info = priv->info;
1769         int error;
1770
1771         if (info->gptp) {
1772                 error = ravb_set_opmode(ndev, CCC_OPC_CONFIG);
1773                 if (error)
1774                         return error;
1775                 /* Set CSEL value */
1776                 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
1777         } else if (info->ccc_gac) {
1778                 error = ravb_set_opmode(ndev, CCC_OPC_CONFIG | CCC_GAC | CCC_CSEL_HPB);
1779         } else {
1780                 error = ravb_set_opmode(ndev, CCC_OPC_CONFIG);
1781         }
1782
1783         return error;
1784 }
1785
1786 static void ravb_set_gti(struct net_device *ndev)
1787 {
1788         struct ravb_private *priv = netdev_priv(ndev);
1789         const struct ravb_hw_info *info = priv->info;
1790
1791         if (!(info->gptp || info->ccc_gac))
1792                 return;
1793
1794         ravb_write(ndev, priv->gti_tiv, GTI);
1795
1796         /* Request GTI loading */
1797         ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
1798 }
1799
1800 static int ravb_compute_gti(struct net_device *ndev)
1801 {
1802         struct ravb_private *priv = netdev_priv(ndev);
1803         const struct ravb_hw_info *info = priv->info;
1804         struct device *dev = ndev->dev.parent;
1805         unsigned long rate;
1806         u64 inc;
1807
1808         if (!(info->gptp || info->ccc_gac))
1809                 return 0;
1810
1811         if (info->gptp_ref_clk)
1812                 rate = clk_get_rate(priv->gptp_clk);
1813         else
1814                 rate = clk_get_rate(priv->clk);
1815         if (!rate)
1816                 return -EINVAL;
1817
1818         inc = div64_ul(1000000000ULL << 20, rate);
1819
1820         if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1821                 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1822                         inc, GTI_TIV_MIN, GTI_TIV_MAX);
1823                 return -EINVAL;
1824         }
1825         priv->gti_tiv = inc;
1826
1827         return 0;
1828 }
1829
1830 /* Set tx and rx clock internal delay modes */
1831 static void ravb_parse_delay_mode(struct device_node *np, struct net_device *ndev)
1832 {
1833         struct ravb_private *priv = netdev_priv(ndev);
1834         bool explicit_delay = false;
1835         u32 delay;
1836
1837         if (!priv->info->internal_delay)
1838                 return;
1839
1840         if (!of_property_read_u32(np, "rx-internal-delay-ps", &delay)) {
1841                 /* Valid values are 0 and 1800, according to DT bindings */
1842                 priv->rxcidm = !!delay;
1843                 explicit_delay = true;
1844         }
1845         if (!of_property_read_u32(np, "tx-internal-delay-ps", &delay)) {
1846                 /* Valid values are 0 and 2000, according to DT bindings */
1847                 priv->txcidm = !!delay;
1848                 explicit_delay = true;
1849         }
1850
1851         if (explicit_delay)
1852                 return;
1853
1854         /* Fall back to legacy rgmii-*id behavior */
1855         if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1856             priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) {
1857                 priv->rxcidm = 1;
1858                 priv->rgmii_override = 1;
1859         }
1860
1861         if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1862             priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) {
1863                 priv->txcidm = 1;
1864                 priv->rgmii_override = 1;
1865         }
1866 }
1867
1868 static void ravb_set_delay_mode(struct net_device *ndev)
1869 {
1870         struct ravb_private *priv = netdev_priv(ndev);
1871         u32 set = 0;
1872
1873         if (!priv->info->internal_delay)
1874                 return;
1875
1876         if (priv->rxcidm)
1877                 set |= APSR_RDM;
1878         if (priv->txcidm)
1879                 set |= APSR_TDM;
1880         ravb_modify(ndev, APSR, APSR_RDM | APSR_TDM, set);
1881 }
1882
1883 /* Network device open function for Ethernet AVB */
1884 static int ravb_open(struct net_device *ndev)
1885 {
1886         struct ravb_private *priv = netdev_priv(ndev);
1887         const struct ravb_hw_info *info = priv->info;
1888         struct device *dev = &priv->pdev->dev;
1889         int error;
1890
1891         napi_enable(&priv->napi[RAVB_BE]);
1892         if (info->nc_queues)
1893                 napi_enable(&priv->napi[RAVB_NC]);
1894
1895         error = pm_runtime_resume_and_get(dev);
1896         if (error < 0)
1897                 goto out_napi_off;
1898
1899         /* Set AVB config mode */
1900         error = ravb_set_config_mode(ndev);
1901         if (error)
1902                 goto out_rpm_put;
1903
1904         ravb_set_delay_mode(ndev);
1905         ravb_write(ndev, priv->desc_bat_dma, DBAT);
1906
1907         /* Device init */
1908         error = ravb_dmac_init(ndev);
1909         if (error)
1910                 goto out_set_reset;
1911
1912         ravb_emac_init(ndev);
1913
1914         ravb_set_gti(ndev);
1915
1916         /* Initialise PTP Clock driver */
1917         if (info->gptp || info->ccc_gac)
1918                 ravb_ptp_init(ndev, priv->pdev);
1919
1920         /* PHY control start */
1921         error = ravb_phy_start(ndev);
1922         if (error)
1923                 goto out_ptp_stop;
1924
1925         netif_tx_start_all_queues(ndev);
1926
1927         return 0;
1928
1929 out_ptp_stop:
1930         /* Stop PTP Clock driver */
1931         if (info->gptp || info->ccc_gac)
1932                 ravb_ptp_stop(ndev);
1933         ravb_stop_dma(ndev);
1934 out_set_reset:
1935         ravb_set_opmode(ndev, CCC_OPC_RESET);
1936 out_rpm_put:
1937         pm_runtime_mark_last_busy(dev);
1938         pm_runtime_put_autosuspend(dev);
1939 out_napi_off:
1940         if (info->nc_queues)
1941                 napi_disable(&priv->napi[RAVB_NC]);
1942         napi_disable(&priv->napi[RAVB_BE]);
1943         return error;
1944 }
1945
1946 /* Timeout function for Ethernet AVB */
1947 static void ravb_tx_timeout(struct net_device *ndev, unsigned int txqueue)
1948 {
1949         struct ravb_private *priv = netdev_priv(ndev);
1950
1951         netif_err(priv, tx_err, ndev,
1952                   "transmit timed out, status %08x, resetting...\n",
1953                   ravb_read(ndev, ISS));
1954
1955         /* tx_errors count up */
1956         ndev->stats.tx_errors++;
1957
1958         schedule_work(&priv->work);
1959 }
1960
1961 static void ravb_tx_timeout_work(struct work_struct *work)
1962 {
1963         struct ravb_private *priv = container_of(work, struct ravb_private,
1964                                                  work);
1965         const struct ravb_hw_info *info = priv->info;
1966         struct net_device *ndev = priv->ndev;
1967         int error;
1968
1969         if (!rtnl_trylock()) {
1970                 usleep_range(1000, 2000);
1971                 schedule_work(&priv->work);
1972                 return;
1973         }
1974
1975         netif_tx_stop_all_queues(ndev);
1976
1977         /* Stop PTP Clock driver */
1978         if (info->gptp)
1979                 ravb_ptp_stop(ndev);
1980
1981         /* Wait for DMA stopping */
1982         if (ravb_stop_dma(ndev)) {
1983                 /* If ravb_stop_dma() fails, the hardware is still operating
1984                  * for TX and/or RX. So, this should not call the following
1985                  * functions because ravb_dmac_init() is possible to fail too.
1986                  * Also, this should not retry ravb_stop_dma() again and again
1987                  * here because it's possible to wait forever. So, this just
1988                  * re-enables the TX and RX and skip the following
1989                  * re-initialization procedure.
1990                  */
1991                 ravb_rcv_snd_enable(ndev);
1992                 goto out;
1993         }
1994
1995         ravb_ring_free(ndev, RAVB_BE);
1996         if (info->nc_queues)
1997                 ravb_ring_free(ndev, RAVB_NC);
1998
1999         /* Device init */
2000         error = ravb_dmac_init(ndev);
2001         if (error) {
2002                 /* If ravb_dmac_init() fails, descriptors are freed. So, this
2003                  * should return here to avoid re-enabling the TX and RX in
2004                  * ravb_emac_init().
2005                  */
2006                 netdev_err(ndev, "%s: ravb_dmac_init() failed, error %d\n",
2007                            __func__, error);
2008                 goto out_unlock;
2009         }
2010         ravb_emac_init(ndev);
2011
2012 out:
2013         /* Initialise PTP Clock driver */
2014         if (info->gptp)
2015                 ravb_ptp_init(ndev, priv->pdev);
2016
2017         netif_tx_start_all_queues(ndev);
2018
2019 out_unlock:
2020         rtnl_unlock();
2021 }
2022
2023 static bool ravb_can_tx_csum_gbeth(struct sk_buff *skb)
2024 {
2025         struct iphdr *ip = ip_hdr(skb);
2026
2027         /* TODO: Need to add support for VLAN tag 802.1Q */
2028         if (skb_vlan_tag_present(skb))
2029                 return false;
2030
2031         /* TODO: Need to add hardware checksum for IPv6 */
2032         if (skb->protocol != htons(ETH_P_IP))
2033                 return false;
2034
2035         switch (ip->protocol) {
2036         case IPPROTO_TCP:
2037                 break;
2038         case IPPROTO_UDP:
2039                 /* If the checksum value in the UDP header field is 0, TOE does
2040                  * not calculate checksum for UDP part of this frame as it is
2041                  * optional function as per standards.
2042                  */
2043                 if (udp_hdr(skb)->check == 0)
2044                         return false;
2045                 break;
2046         default:
2047                 return false;
2048         }
2049
2050         return true;
2051 }
2052
2053 /* Packet transmit function for Ethernet AVB */
2054 static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
2055 {
2056         struct ravb_private *priv = netdev_priv(ndev);
2057         const struct ravb_hw_info *info = priv->info;
2058         unsigned int num_tx_desc = priv->num_tx_desc;
2059         u16 q = skb_get_queue_mapping(skb);
2060         struct ravb_tstamp_skb *ts_skb;
2061         struct ravb_tx_desc *desc;
2062         unsigned long flags;
2063         dma_addr_t dma_addr;
2064         void *buffer;
2065         u32 entry;
2066         u32 len;
2067
2068         if (skb->ip_summed == CHECKSUM_PARTIAL && !ravb_can_tx_csum_gbeth(skb))
2069                 skb_checksum_help(skb);
2070
2071         spin_lock_irqsave(&priv->lock, flags);
2072         if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
2073             num_tx_desc) {
2074                 netif_err(priv, tx_queued, ndev,
2075                           "still transmitting with the full ring!\n");
2076                 netif_stop_subqueue(ndev, q);
2077                 spin_unlock_irqrestore(&priv->lock, flags);
2078                 return NETDEV_TX_BUSY;
2079         }
2080
2081         if (skb_put_padto(skb, ETH_ZLEN))
2082                 goto exit;
2083
2084         entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * num_tx_desc);
2085         priv->tx_skb[q][entry / num_tx_desc] = skb;
2086
2087         if (num_tx_desc > 1) {
2088                 buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
2089                          entry / num_tx_desc * DPTR_ALIGN;
2090                 len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
2091
2092                 /* Zero length DMA descriptors are problematic as they seem
2093                  * to terminate DMA transfers. Avoid them by simply using a
2094                  * length of DPTR_ALIGN (4) when skb data is aligned to
2095                  * DPTR_ALIGN.
2096                  *
2097                  * As skb is guaranteed to have at least ETH_ZLEN (60)
2098                  * bytes of data by the call to skb_put_padto() above this
2099                  * is safe with respect to both the length of the first DMA
2100                  * descriptor (len) overflowing the available data and the
2101                  * length of the second DMA descriptor (skb->len - len)
2102                  * being negative.
2103                  */
2104                 if (len == 0)
2105                         len = DPTR_ALIGN;
2106
2107                 memcpy(buffer, skb->data, len);
2108                 dma_addr = dma_map_single(ndev->dev.parent, buffer, len,
2109                                           DMA_TO_DEVICE);
2110                 if (dma_mapping_error(ndev->dev.parent, dma_addr))
2111                         goto drop;
2112
2113                 desc = &priv->tx_ring[q][entry];
2114                 desc->ds_tagl = cpu_to_le16(len);
2115                 desc->dptr = cpu_to_le32(dma_addr);
2116
2117                 buffer = skb->data + len;
2118                 len = skb->len - len;
2119                 dma_addr = dma_map_single(ndev->dev.parent, buffer, len,
2120                                           DMA_TO_DEVICE);
2121                 if (dma_mapping_error(ndev->dev.parent, dma_addr))
2122                         goto unmap;
2123
2124                 desc++;
2125         } else {
2126                 desc = &priv->tx_ring[q][entry];
2127                 len = skb->len;
2128                 dma_addr = dma_map_single(ndev->dev.parent, skb->data, skb->len,
2129                                           DMA_TO_DEVICE);
2130                 if (dma_mapping_error(ndev->dev.parent, dma_addr))
2131                         goto drop;
2132         }
2133         desc->ds_tagl = cpu_to_le16(len);
2134         desc->dptr = cpu_to_le32(dma_addr);
2135
2136         /* TX timestamp required */
2137         if (info->gptp || info->ccc_gac) {
2138                 if (q == RAVB_NC) {
2139                         ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
2140                         if (!ts_skb) {
2141                                 if (num_tx_desc > 1) {
2142                                         desc--;
2143                                         dma_unmap_single(ndev->dev.parent, dma_addr,
2144                                                          len, DMA_TO_DEVICE);
2145                                 }
2146                                 goto unmap;
2147                         }
2148                         ts_skb->skb = skb_get(skb);
2149                         ts_skb->tag = priv->ts_skb_tag++;
2150                         priv->ts_skb_tag &= 0x3ff;
2151                         list_add_tail(&ts_skb->list, &priv->ts_skb_list);
2152
2153                         /* TAG and timestamp required flag */
2154                         skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2155                         desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
2156                         desc->ds_tagl |= cpu_to_le16(ts_skb->tag << 12);
2157                 }
2158
2159                 skb_tx_timestamp(skb);
2160         }
2161         /* Descriptor type must be set after all the above writes */
2162         dma_wmb();
2163         if (num_tx_desc > 1) {
2164                 desc->die_dt = DT_FEND;
2165                 desc--;
2166                 desc->die_dt = DT_FSTART;
2167         } else {
2168                 desc->die_dt = DT_FSINGLE;
2169         }
2170         ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
2171
2172         priv->cur_tx[q] += num_tx_desc;
2173         if (priv->cur_tx[q] - priv->dirty_tx[q] >
2174             (priv->num_tx_ring[q] - 1) * num_tx_desc &&
2175             !ravb_tx_free(ndev, q, true))
2176                 netif_stop_subqueue(ndev, q);
2177
2178 exit:
2179         spin_unlock_irqrestore(&priv->lock, flags);
2180         return NETDEV_TX_OK;
2181
2182 unmap:
2183         dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
2184                          le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
2185 drop:
2186         dev_kfree_skb_any(skb);
2187         priv->tx_skb[q][entry / num_tx_desc] = NULL;
2188         goto exit;
2189 }
2190
2191 static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
2192                              struct net_device *sb_dev)
2193 {
2194         /* If skb needs TX timestamp, it is handled in network control queue */
2195         return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
2196                                                                RAVB_BE;
2197
2198 }
2199
2200 static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
2201 {
2202         struct ravb_private *priv = netdev_priv(ndev);
2203         const struct ravb_hw_info *info = priv->info;
2204         struct net_device_stats *nstats, *stats0, *stats1;
2205         struct device *dev = &priv->pdev->dev;
2206
2207         nstats = &ndev->stats;
2208
2209         pm_runtime_get_noresume(dev);
2210
2211         if (!pm_runtime_active(dev))
2212                 goto out_rpm_put;
2213
2214         stats0 = &priv->stats[RAVB_BE];
2215
2216         if (info->tx_counters) {
2217                 nstats->tx_dropped += ravb_read(ndev, TROCR);
2218                 ravb_write(ndev, 0, TROCR);     /* (write clear) */
2219         }
2220
2221         if (info->carrier_counters) {
2222                 nstats->collisions += ravb_read(ndev, CXR41);
2223                 ravb_write(ndev, 0, CXR41);     /* (write clear) */
2224                 nstats->tx_carrier_errors += ravb_read(ndev, CXR42);
2225                 ravb_write(ndev, 0, CXR42);     /* (write clear) */
2226         }
2227
2228         nstats->rx_packets = stats0->rx_packets;
2229         nstats->tx_packets = stats0->tx_packets;
2230         nstats->rx_bytes = stats0->rx_bytes;
2231         nstats->tx_bytes = stats0->tx_bytes;
2232         nstats->multicast = stats0->multicast;
2233         nstats->rx_errors = stats0->rx_errors;
2234         nstats->rx_crc_errors = stats0->rx_crc_errors;
2235         nstats->rx_frame_errors = stats0->rx_frame_errors;
2236         nstats->rx_length_errors = stats0->rx_length_errors;
2237         nstats->rx_missed_errors = stats0->rx_missed_errors;
2238         nstats->rx_over_errors = stats0->rx_over_errors;
2239         if (info->nc_queues) {
2240                 stats1 = &priv->stats[RAVB_NC];
2241
2242                 nstats->rx_packets += stats1->rx_packets;
2243                 nstats->tx_packets += stats1->tx_packets;
2244                 nstats->rx_bytes += stats1->rx_bytes;
2245                 nstats->tx_bytes += stats1->tx_bytes;
2246                 nstats->multicast += stats1->multicast;
2247                 nstats->rx_errors += stats1->rx_errors;
2248                 nstats->rx_crc_errors += stats1->rx_crc_errors;
2249                 nstats->rx_frame_errors += stats1->rx_frame_errors;
2250                 nstats->rx_length_errors += stats1->rx_length_errors;
2251                 nstats->rx_missed_errors += stats1->rx_missed_errors;
2252                 nstats->rx_over_errors += stats1->rx_over_errors;
2253         }
2254
2255 out_rpm_put:
2256         pm_runtime_put_noidle(dev);
2257         return nstats;
2258 }
2259
2260 /* Update promiscuous bit */
2261 static void ravb_set_rx_mode(struct net_device *ndev)
2262 {
2263         struct ravb_private *priv = netdev_priv(ndev);
2264         unsigned long flags;
2265
2266         spin_lock_irqsave(&priv->lock, flags);
2267         ravb_modify(ndev, ECMR, ECMR_PRM,
2268                     ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
2269         spin_unlock_irqrestore(&priv->lock, flags);
2270 }
2271
2272 /* Device close function for Ethernet AVB */
2273 static int ravb_close(struct net_device *ndev)
2274 {
2275         struct device_node *np = ndev->dev.parent->of_node;
2276         struct ravb_private *priv = netdev_priv(ndev);
2277         const struct ravb_hw_info *info = priv->info;
2278         struct ravb_tstamp_skb *ts_skb, *ts_skb2;
2279         struct device *dev = &priv->pdev->dev;
2280         int error;
2281
2282         netif_tx_stop_all_queues(ndev);
2283
2284         /* Disable interrupts by clearing the interrupt masks. */
2285         ravb_write(ndev, 0, RIC0);
2286         ravb_write(ndev, 0, RIC2);
2287         ravb_write(ndev, 0, TIC);
2288
2289         /* PHY disconnect */
2290         if (ndev->phydev) {
2291                 phy_stop(ndev->phydev);
2292                 phy_disconnect(ndev->phydev);
2293                 if (of_phy_is_fixed_link(np))
2294                         of_phy_deregister_fixed_link(np);
2295         }
2296
2297         /* Stop PTP Clock driver */
2298         if (info->gptp || info->ccc_gac)
2299                 ravb_ptp_stop(ndev);
2300
2301         /* Set the config mode to stop the AVB-DMAC's processes */
2302         if (ravb_stop_dma(ndev) < 0)
2303                 netdev_err(ndev,
2304                            "device will be stopped after h/w processes are done.\n");
2305
2306         /* Clear the timestamp list */
2307         if (info->gptp || info->ccc_gac) {
2308                 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
2309                         list_del(&ts_skb->list);
2310                         kfree_skb(ts_skb->skb);
2311                         kfree(ts_skb);
2312                 }
2313         }
2314
2315         cancel_work_sync(&priv->work);
2316
2317         if (info->nc_queues)
2318                 napi_disable(&priv->napi[RAVB_NC]);
2319         napi_disable(&priv->napi[RAVB_BE]);
2320
2321         /* Free all the skb's in the RX queue and the DMA buffers. */
2322         ravb_ring_free(ndev, RAVB_BE);
2323         if (info->nc_queues)
2324                 ravb_ring_free(ndev, RAVB_NC);
2325
2326         /* Update statistics. */
2327         ravb_get_stats(ndev);
2328
2329         /* Set reset mode. */
2330         error = ravb_set_opmode(ndev, CCC_OPC_RESET);
2331         if (error)
2332                 return error;
2333
2334         pm_runtime_mark_last_busy(dev);
2335         pm_runtime_put_autosuspend(dev);
2336
2337         return 0;
2338 }
2339
2340 static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
2341 {
2342         struct ravb_private *priv = netdev_priv(ndev);
2343         struct hwtstamp_config config;
2344
2345         config.flags = 0;
2346         config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
2347                                                 HWTSTAMP_TX_OFF;
2348         switch (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE) {
2349         case RAVB_RXTSTAMP_TYPE_V2_L2_EVENT:
2350                 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
2351                 break;
2352         case RAVB_RXTSTAMP_TYPE_ALL:
2353                 config.rx_filter = HWTSTAMP_FILTER_ALL;
2354                 break;
2355         default:
2356                 config.rx_filter = HWTSTAMP_FILTER_NONE;
2357         }
2358
2359         return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
2360                 -EFAULT : 0;
2361 }
2362
2363 /* Control hardware time stamping */
2364 static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
2365 {
2366         struct ravb_private *priv = netdev_priv(ndev);
2367         struct hwtstamp_config config;
2368         u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
2369         u32 tstamp_tx_ctrl;
2370
2371         if (copy_from_user(&config, req->ifr_data, sizeof(config)))
2372                 return -EFAULT;
2373
2374         switch (config.tx_type) {
2375         case HWTSTAMP_TX_OFF:
2376                 tstamp_tx_ctrl = 0;
2377                 break;
2378         case HWTSTAMP_TX_ON:
2379                 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
2380                 break;
2381         default:
2382                 return -ERANGE;
2383         }
2384
2385         switch (config.rx_filter) {
2386         case HWTSTAMP_FILTER_NONE:
2387                 tstamp_rx_ctrl = 0;
2388                 break;
2389         case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2390                 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
2391                 break;
2392         default:
2393                 config.rx_filter = HWTSTAMP_FILTER_ALL;
2394                 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
2395         }
2396
2397         priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
2398         priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
2399
2400         return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
2401                 -EFAULT : 0;
2402 }
2403
2404 /* ioctl to device function */
2405 static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
2406 {
2407         struct phy_device *phydev = ndev->phydev;
2408
2409         if (!netif_running(ndev))
2410                 return -EINVAL;
2411
2412         if (!phydev)
2413                 return -ENODEV;
2414
2415         switch (cmd) {
2416         case SIOCGHWTSTAMP:
2417                 return ravb_hwtstamp_get(ndev, req);
2418         case SIOCSHWTSTAMP:
2419                 return ravb_hwtstamp_set(ndev, req);
2420         }
2421
2422         return phy_mii_ioctl(phydev, req, cmd);
2423 }
2424
2425 static int ravb_change_mtu(struct net_device *ndev, int new_mtu)
2426 {
2427         struct ravb_private *priv = netdev_priv(ndev);
2428
2429         ndev->mtu = new_mtu;
2430
2431         if (netif_running(ndev)) {
2432                 synchronize_irq(priv->emac_irq);
2433                 ravb_emac_init(ndev);
2434         }
2435
2436         netdev_update_features(ndev);
2437
2438         return 0;
2439 }
2440
2441 static void ravb_set_rx_csum(struct net_device *ndev, bool enable)
2442 {
2443         struct ravb_private *priv = netdev_priv(ndev);
2444         unsigned long flags;
2445
2446         spin_lock_irqsave(&priv->lock, flags);
2447
2448         /* Disable TX and RX */
2449         ravb_rcv_snd_disable(ndev);
2450
2451         /* Modify RX Checksum setting */
2452         ravb_modify(ndev, ECMR, ECMR_RCSC, enable ? ECMR_RCSC : 0);
2453
2454         /* Enable TX and RX */
2455         ravb_rcv_snd_enable(ndev);
2456
2457         spin_unlock_irqrestore(&priv->lock, flags);
2458 }
2459
2460 static int ravb_endisable_csum_gbeth(struct net_device *ndev, enum ravb_reg reg,
2461                                      u32 val, u32 mask)
2462 {
2463         u32 csr0 = CSR0_TPE | CSR0_RPE;
2464         int ret;
2465
2466         ravb_write(ndev, csr0 & ~mask, CSR0);
2467         ret = ravb_wait(ndev, CSR0, mask, 0);
2468         if (!ret)
2469                 ravb_write(ndev, val, reg);
2470
2471         ravb_write(ndev, csr0, CSR0);
2472
2473         return ret;
2474 }
2475
2476 static int ravb_set_features_gbeth(struct net_device *ndev,
2477                                    netdev_features_t features)
2478 {
2479         netdev_features_t changed = ndev->features ^ features;
2480         struct ravb_private *priv = netdev_priv(ndev);
2481         unsigned long flags;
2482         int ret = 0;
2483         u32 val;
2484
2485         spin_lock_irqsave(&priv->lock, flags);
2486         if (changed & NETIF_F_RXCSUM) {
2487                 if (features & NETIF_F_RXCSUM)
2488                         val = CSR2_RIP4 | CSR2_RTCP4 | CSR2_RUDP4 | CSR2_RICMP4;
2489                 else
2490                         val = 0;
2491
2492                 ret = ravb_endisable_csum_gbeth(ndev, CSR2, val, CSR0_RPE);
2493                 if (ret)
2494                         goto done;
2495         }
2496
2497         if (changed & NETIF_F_HW_CSUM) {
2498                 if (features & NETIF_F_HW_CSUM)
2499                         val = CSR1_TIP4 | CSR1_TTCP4 | CSR1_TUDP4;
2500                 else
2501                         val = 0;
2502
2503                 ret = ravb_endisable_csum_gbeth(ndev, CSR1, val, CSR0_TPE);
2504                 if (ret)
2505                         goto done;
2506         }
2507
2508 done:
2509         spin_unlock_irqrestore(&priv->lock, flags);
2510
2511         return ret;
2512 }
2513
2514 static int ravb_set_features_rcar(struct net_device *ndev,
2515                                   netdev_features_t features)
2516 {
2517         netdev_features_t changed = ndev->features ^ features;
2518
2519         if (changed & NETIF_F_RXCSUM)
2520                 ravb_set_rx_csum(ndev, features & NETIF_F_RXCSUM);
2521
2522         return 0;
2523 }
2524
2525 static int ravb_set_features(struct net_device *ndev,
2526                              netdev_features_t features)
2527 {
2528         struct ravb_private *priv = netdev_priv(ndev);
2529         const struct ravb_hw_info *info = priv->info;
2530         struct device *dev = &priv->pdev->dev;
2531         int ret;
2532
2533         pm_runtime_get_noresume(dev);
2534
2535         if (pm_runtime_active(dev))
2536                 ret = info->set_feature(ndev, features);
2537         else
2538                 ret = 0;
2539
2540         pm_runtime_put_noidle(dev);
2541
2542         if (ret)
2543                 return ret;
2544
2545         ndev->features = features;
2546
2547         return 0;
2548 }
2549
2550 static const struct net_device_ops ravb_netdev_ops = {
2551         .ndo_open               = ravb_open,
2552         .ndo_stop               = ravb_close,
2553         .ndo_start_xmit         = ravb_start_xmit,
2554         .ndo_select_queue       = ravb_select_queue,
2555         .ndo_get_stats          = ravb_get_stats,
2556         .ndo_set_rx_mode        = ravb_set_rx_mode,
2557         .ndo_tx_timeout         = ravb_tx_timeout,
2558         .ndo_eth_ioctl          = ravb_do_ioctl,
2559         .ndo_change_mtu         = ravb_change_mtu,
2560         .ndo_validate_addr      = eth_validate_addr,
2561         .ndo_set_mac_address    = eth_mac_addr,
2562         .ndo_set_features       = ravb_set_features,
2563 };
2564
2565 /* MDIO bus init function */
2566 static int ravb_mdio_init(struct ravb_private *priv)
2567 {
2568         struct platform_device *pdev = priv->pdev;
2569         struct device *dev = &pdev->dev;
2570         struct phy_device *phydev;
2571         struct device_node *pn;
2572         int error;
2573
2574         /* Bitbang init */
2575         priv->mdiobb.ops = &bb_ops;
2576
2577         /* MII controller setting */
2578         priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
2579         if (!priv->mii_bus)
2580                 return -ENOMEM;
2581
2582         /* Hook up MII support for ethtool */
2583         priv->mii_bus->name = "ravb_mii";
2584         priv->mii_bus->parent = dev;
2585         snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
2586                  pdev->name, pdev->id);
2587
2588         /* Register MDIO bus */
2589         error = of_mdiobus_register(priv->mii_bus, dev->of_node);
2590         if (error)
2591                 goto out_free_bus;
2592
2593         pn = of_parse_phandle(dev->of_node, "phy-handle", 0);
2594         phydev = of_phy_find_device(pn);
2595         if (phydev) {
2596                 phydev->mac_managed_pm = true;
2597                 put_device(&phydev->mdio.dev);
2598         }
2599         of_node_put(pn);
2600
2601         return 0;
2602
2603 out_free_bus:
2604         free_mdio_bitbang(priv->mii_bus);
2605         return error;
2606 }
2607
2608 /* MDIO bus release function */
2609 static int ravb_mdio_release(struct ravb_private *priv)
2610 {
2611         /* Unregister mdio bus */
2612         mdiobus_unregister(priv->mii_bus);
2613
2614         /* Free bitbang info */
2615         free_mdio_bitbang(priv->mii_bus);
2616
2617         return 0;
2618 }
2619
2620 static const struct ravb_hw_info ravb_gen3_hw_info = {
2621         .receive = ravb_rx_rcar,
2622         .set_rate = ravb_set_rate_rcar,
2623         .set_feature = ravb_set_features_rcar,
2624         .dmac_init = ravb_dmac_init_rcar,
2625         .emac_init = ravb_emac_init_rcar,
2626         .gstrings_stats = ravb_gstrings_stats,
2627         .gstrings_size = sizeof(ravb_gstrings_stats),
2628         .net_hw_features = NETIF_F_RXCSUM,
2629         .net_features = NETIF_F_RXCSUM,
2630         .stats_len = ARRAY_SIZE(ravb_gstrings_stats),
2631         .tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3,
2632         .rx_max_frame_size = SZ_2K,
2633         .rx_max_desc_use = SZ_2K - ETH_FCS_LEN + sizeof(__sum16),
2634         .rx_desc_size = sizeof(struct ravb_ex_rx_desc),
2635         .internal_delay = 1,
2636         .tx_counters = 1,
2637         .multi_irqs = 1,
2638         .irq_en_dis = 1,
2639         .ccc_gac = 1,
2640         .nc_queues = 1,
2641         .magic_pkt = 1,
2642 };
2643
2644 static const struct ravb_hw_info ravb_gen2_hw_info = {
2645         .receive = ravb_rx_rcar,
2646         .set_rate = ravb_set_rate_rcar,
2647         .set_feature = ravb_set_features_rcar,
2648         .dmac_init = ravb_dmac_init_rcar,
2649         .emac_init = ravb_emac_init_rcar,
2650         .gstrings_stats = ravb_gstrings_stats,
2651         .gstrings_size = sizeof(ravb_gstrings_stats),
2652         .net_hw_features = NETIF_F_RXCSUM,
2653         .net_features = NETIF_F_RXCSUM,
2654         .stats_len = ARRAY_SIZE(ravb_gstrings_stats),
2655         .tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3,
2656         .rx_max_frame_size = SZ_2K,
2657         .rx_max_desc_use = SZ_2K - ETH_FCS_LEN + sizeof(__sum16),
2658         .rx_desc_size = sizeof(struct ravb_ex_rx_desc),
2659         .aligned_tx = 1,
2660         .gptp = 1,
2661         .nc_queues = 1,
2662         .magic_pkt = 1,
2663 };
2664
2665 static const struct ravb_hw_info ravb_rzv2m_hw_info = {
2666         .receive = ravb_rx_rcar,
2667         .set_rate = ravb_set_rate_rcar,
2668         .set_feature = ravb_set_features_rcar,
2669         .dmac_init = ravb_dmac_init_rcar,
2670         .emac_init = ravb_emac_init_rcar,
2671         .gstrings_stats = ravb_gstrings_stats,
2672         .gstrings_size = sizeof(ravb_gstrings_stats),
2673         .net_hw_features = NETIF_F_RXCSUM,
2674         .net_features = NETIF_F_RXCSUM,
2675         .stats_len = ARRAY_SIZE(ravb_gstrings_stats),
2676         .tccr_mask = TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3,
2677         .rx_max_frame_size = SZ_2K,
2678         .rx_max_desc_use = SZ_2K - ETH_FCS_LEN + sizeof(__sum16),
2679         .rx_desc_size = sizeof(struct ravb_ex_rx_desc),
2680         .multi_irqs = 1,
2681         .err_mgmt_irqs = 1,
2682         .gptp = 1,
2683         .gptp_ref_clk = 1,
2684         .nc_queues = 1,
2685         .magic_pkt = 1,
2686 };
2687
2688 static const struct ravb_hw_info gbeth_hw_info = {
2689         .receive = ravb_rx_gbeth,
2690         .set_rate = ravb_set_rate_gbeth,
2691         .set_feature = ravb_set_features_gbeth,
2692         .dmac_init = ravb_dmac_init_gbeth,
2693         .emac_init = ravb_emac_init_gbeth,
2694         .gstrings_stats = ravb_gstrings_stats_gbeth,
2695         .gstrings_size = sizeof(ravb_gstrings_stats_gbeth),
2696         .net_hw_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM,
2697         .net_features = NETIF_F_RXCSUM | NETIF_F_HW_CSUM,
2698         .stats_len = ARRAY_SIZE(ravb_gstrings_stats_gbeth),
2699         .tccr_mask = TCCR_TSRQ0,
2700         .rx_max_frame_size = SZ_8K,
2701         .rx_max_desc_use = 4080,
2702         .rx_desc_size = sizeof(struct ravb_rx_desc),
2703         .aligned_tx = 1,
2704         .tx_counters = 1,
2705         .carrier_counters = 1,
2706         .half_duplex = 1,
2707 };
2708
2709 static const struct of_device_id ravb_match_table[] = {
2710         { .compatible = "renesas,etheravb-r8a7790", .data = &ravb_gen2_hw_info },
2711         { .compatible = "renesas,etheravb-r8a7794", .data = &ravb_gen2_hw_info },
2712         { .compatible = "renesas,etheravb-rcar-gen2", .data = &ravb_gen2_hw_info },
2713         { .compatible = "renesas,etheravb-r8a7795", .data = &ravb_gen3_hw_info },
2714         { .compatible = "renesas,etheravb-rcar-gen3", .data = &ravb_gen3_hw_info },
2715         { .compatible = "renesas,etheravb-rcar-gen4", .data = &ravb_gen3_hw_info },
2716         { .compatible = "renesas,etheravb-rzv2m", .data = &ravb_rzv2m_hw_info },
2717         { .compatible = "renesas,rzg2l-gbeth", .data = &gbeth_hw_info },
2718         { }
2719 };
2720 MODULE_DEVICE_TABLE(of, ravb_match_table);
2721
2722 static int ravb_setup_irq(struct ravb_private *priv, const char *irq_name,
2723                           const char *ch, int *irq, irq_handler_t handler)
2724 {
2725         struct platform_device *pdev = priv->pdev;
2726         struct net_device *ndev = priv->ndev;
2727         struct device *dev = &pdev->dev;
2728         const char *dev_name;
2729         unsigned long flags;
2730         int error, irq_num;
2731
2732         if (irq_name) {
2733                 dev_name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
2734                 if (!dev_name)
2735                         return -ENOMEM;
2736
2737                 irq_num = platform_get_irq_byname(pdev, irq_name);
2738                 flags = 0;
2739         } else {
2740                 dev_name = ndev->name;
2741                 irq_num = platform_get_irq(pdev, 0);
2742                 flags = IRQF_SHARED;
2743         }
2744         if (irq_num < 0)
2745                 return irq_num;
2746
2747         if (irq)
2748                 *irq = irq_num;
2749
2750         error = devm_request_irq(dev, irq_num, handler, flags, dev_name, ndev);
2751         if (error)
2752                 netdev_err(ndev, "cannot request IRQ %s\n", dev_name);
2753
2754         return error;
2755 }
2756
2757 static int ravb_setup_irqs(struct ravb_private *priv)
2758 {
2759         const struct ravb_hw_info *info = priv->info;
2760         struct net_device *ndev = priv->ndev;
2761         const char *irq_name, *emac_irq_name;
2762         int error;
2763
2764         if (!info->multi_irqs)
2765                 return ravb_setup_irq(priv, NULL, NULL, &ndev->irq, ravb_interrupt);
2766
2767         if (info->err_mgmt_irqs) {
2768                 irq_name = "dia";
2769                 emac_irq_name = "line3";
2770         } else {
2771                 irq_name = "ch22";
2772                 emac_irq_name = "ch24";
2773         }
2774
2775         error = ravb_setup_irq(priv, irq_name, "ch22:multi", &ndev->irq, ravb_multi_interrupt);
2776         if (error)
2777                 return error;
2778
2779         error = ravb_setup_irq(priv, emac_irq_name, "ch24:emac", &priv->emac_irq,
2780                                ravb_emac_interrupt);
2781         if (error)
2782                 return error;
2783
2784         if (info->err_mgmt_irqs) {
2785                 error = ravb_setup_irq(priv, "err_a", "err_a", NULL, ravb_multi_interrupt);
2786                 if (error)
2787                         return error;
2788
2789                 error = ravb_setup_irq(priv, "mgmt_a", "mgmt_a", NULL, ravb_multi_interrupt);
2790                 if (error)
2791                         return error;
2792         }
2793
2794         error = ravb_setup_irq(priv, "ch0", "ch0:rx_be", NULL, ravb_be_interrupt);
2795         if (error)
2796                 return error;
2797
2798         error = ravb_setup_irq(priv, "ch1", "ch1:rx_nc", NULL, ravb_nc_interrupt);
2799         if (error)
2800                 return error;
2801
2802         error = ravb_setup_irq(priv, "ch18", "ch18:tx_be", NULL, ravb_be_interrupt);
2803         if (error)
2804                 return error;
2805
2806         return ravb_setup_irq(priv, "ch19", "ch19:tx_nc", NULL, ravb_nc_interrupt);
2807 }
2808
2809 static int ravb_probe(struct platform_device *pdev)
2810 {
2811         struct device_node *np = pdev->dev.of_node;
2812         const struct ravb_hw_info *info;
2813         struct reset_control *rstc;
2814         struct ravb_private *priv;
2815         struct net_device *ndev;
2816         struct resource *res;
2817         int error, q;
2818
2819         if (!np) {
2820                 dev_err(&pdev->dev,
2821                         "this driver is required to be instantiated from device tree\n");
2822                 return -EINVAL;
2823         }
2824
2825         rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
2826         if (IS_ERR(rstc))
2827                 return dev_err_probe(&pdev->dev, PTR_ERR(rstc),
2828                                      "failed to get cpg reset\n");
2829
2830         ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
2831                                   NUM_TX_QUEUE, NUM_RX_QUEUE);
2832         if (!ndev)
2833                 return -ENOMEM;
2834
2835         info = of_device_get_match_data(&pdev->dev);
2836
2837         ndev->features = info->net_features;
2838         ndev->hw_features = info->net_hw_features;
2839
2840         error = reset_control_deassert(rstc);
2841         if (error)
2842                 goto out_free_netdev;
2843
2844         SET_NETDEV_DEV(ndev, &pdev->dev);
2845
2846         priv = netdev_priv(ndev);
2847         priv->info = info;
2848         priv->rstc = rstc;
2849         priv->ndev = ndev;
2850         priv->pdev = pdev;
2851         priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
2852         priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
2853         if (info->nc_queues) {
2854                 priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
2855                 priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
2856         }
2857
2858         error = ravb_setup_irqs(priv);
2859         if (error)
2860                 goto out_reset_assert;
2861
2862         priv->clk = devm_clk_get(&pdev->dev, NULL);
2863         if (IS_ERR(priv->clk)) {
2864                 error = PTR_ERR(priv->clk);
2865                 goto out_reset_assert;
2866         }
2867
2868         if (info->gptp_ref_clk) {
2869                 priv->gptp_clk = devm_clk_get(&pdev->dev, "gptp");
2870                 if (IS_ERR(priv->gptp_clk)) {
2871                         error = PTR_ERR(priv->gptp_clk);
2872                         goto out_reset_assert;
2873                 }
2874         }
2875
2876         priv->refclk = devm_clk_get_optional(&pdev->dev, "refclk");
2877         if (IS_ERR(priv->refclk)) {
2878                 error = PTR_ERR(priv->refclk);
2879                 goto out_reset_assert;
2880         }
2881         clk_prepare(priv->refclk);
2882
2883         platform_set_drvdata(pdev, ndev);
2884         pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
2885         pm_runtime_use_autosuspend(&pdev->dev);
2886         pm_runtime_enable(&pdev->dev);
2887         error = pm_runtime_resume_and_get(&pdev->dev);
2888         if (error < 0)
2889                 goto out_rpm_disable;
2890
2891         priv->addr = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
2892         if (IS_ERR(priv->addr)) {
2893                 error = PTR_ERR(priv->addr);
2894                 goto out_rpm_put;
2895         }
2896
2897         /* The Ether-specific entries in the device structure. */
2898         ndev->base_addr = res->start;
2899
2900         spin_lock_init(&priv->lock);
2901         INIT_WORK(&priv->work, ravb_tx_timeout_work);
2902
2903         error = of_get_phy_mode(np, &priv->phy_interface);
2904         if (error && error != -ENODEV)
2905                 goto out_rpm_put;
2906
2907         priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2908         priv->avb_link_active_low =
2909                 of_property_read_bool(np, "renesas,ether-link-active-low");
2910
2911         ndev->max_mtu = info->rx_max_frame_size -
2912                 (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
2913         ndev->min_mtu = ETH_MIN_MTU;
2914
2915         /* FIXME: R-Car Gen2 has 4byte alignment restriction for tx buffer
2916          * Use two descriptor to handle such situation. First descriptor to
2917          * handle aligned data buffer and second descriptor to handle the
2918          * overflow data because of alignment.
2919          */
2920         priv->num_tx_desc = info->aligned_tx ? 2 : 1;
2921
2922         /* Set function */
2923         ndev->netdev_ops = &ravb_netdev_ops;
2924         ndev->ethtool_ops = &ravb_ethtool_ops;
2925
2926         error = ravb_compute_gti(ndev);
2927         if (error)
2928                 goto out_rpm_put;
2929
2930         ravb_parse_delay_mode(np, ndev);
2931
2932         /* Allocate descriptor base address table */
2933         priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
2934         priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
2935                                             &priv->desc_bat_dma, GFP_KERNEL);
2936         if (!priv->desc_bat) {
2937                 dev_err(&pdev->dev,
2938                         "Cannot allocate desc base address table (size %d bytes)\n",
2939                         priv->desc_bat_size);
2940                 error = -ENOMEM;
2941                 goto out_rpm_put;
2942         }
2943         for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2944                 priv->desc_bat[q].die_dt = DT_EOS;
2945
2946         /* Initialise HW timestamp list */
2947         INIT_LIST_HEAD(&priv->ts_skb_list);
2948
2949         /* Debug message level */
2950         priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2951
2952         /* Set config mode as this is needed for PHY initialization. */
2953         error = ravb_set_opmode(ndev, CCC_OPC_CONFIG);
2954         if (error)
2955                 goto out_rpm_put;
2956
2957         /* Read and set MAC address */
2958         ravb_read_mac_address(np, ndev);
2959         if (!is_valid_ether_addr(ndev->dev_addr)) {
2960                 dev_warn(&pdev->dev,
2961                          "no valid MAC address supplied, using a random one\n");
2962                 eth_hw_addr_random(ndev);
2963         }
2964
2965         /* MDIO bus init */
2966         error = ravb_mdio_init(priv);
2967         if (error) {
2968                 dev_err(&pdev->dev, "failed to initialize MDIO\n");
2969                 goto out_reset_mode;
2970         }
2971
2972         /* Undo previous switch to config opmode. */
2973         error = ravb_set_opmode(ndev, CCC_OPC_RESET);
2974         if (error)
2975                 goto out_mdio_release;
2976
2977         netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll);
2978         if (info->nc_queues)
2979                 netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll);
2980
2981         /* Network device register */
2982         error = register_netdev(ndev);
2983         if (error)
2984                 goto out_napi_del;
2985
2986         device_set_wakeup_capable(&pdev->dev, 1);
2987
2988         /* Print device information */
2989         netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2990                     (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2991
2992         pm_runtime_mark_last_busy(&pdev->dev);
2993         pm_runtime_put_autosuspend(&pdev->dev);
2994
2995         return 0;
2996
2997 out_napi_del:
2998         if (info->nc_queues)
2999                 netif_napi_del(&priv->napi[RAVB_NC]);
3000
3001         netif_napi_del(&priv->napi[RAVB_BE]);
3002 out_mdio_release:
3003         ravb_mdio_release(priv);
3004 out_reset_mode:
3005         ravb_set_opmode(ndev, CCC_OPC_RESET);
3006         dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
3007                           priv->desc_bat_dma);
3008 out_rpm_put:
3009         pm_runtime_put(&pdev->dev);
3010 out_rpm_disable:
3011         pm_runtime_disable(&pdev->dev);
3012         pm_runtime_dont_use_autosuspend(&pdev->dev);
3013         clk_unprepare(priv->refclk);
3014 out_reset_assert:
3015         reset_control_assert(rstc);
3016 out_free_netdev:
3017         free_netdev(ndev);
3018         return error;
3019 }
3020
3021 static void ravb_remove(struct platform_device *pdev)
3022 {
3023         struct net_device *ndev = platform_get_drvdata(pdev);
3024         struct ravb_private *priv = netdev_priv(ndev);
3025         const struct ravb_hw_info *info = priv->info;
3026         struct device *dev = &priv->pdev->dev;
3027         int error;
3028
3029         error = pm_runtime_resume_and_get(dev);
3030         if (error < 0)
3031                 return;
3032
3033         unregister_netdev(ndev);
3034         if (info->nc_queues)
3035                 netif_napi_del(&priv->napi[RAVB_NC]);
3036         netif_napi_del(&priv->napi[RAVB_BE]);
3037
3038         ravb_mdio_release(priv);
3039
3040         dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
3041                           priv->desc_bat_dma);
3042
3043         pm_runtime_put_sync_suspend(&pdev->dev);
3044         pm_runtime_disable(&pdev->dev);
3045         pm_runtime_dont_use_autosuspend(dev);
3046         clk_unprepare(priv->refclk);
3047         reset_control_assert(priv->rstc);
3048         free_netdev(ndev);
3049         platform_set_drvdata(pdev, NULL);
3050 }
3051
3052 static int ravb_wol_setup(struct net_device *ndev)
3053 {
3054         struct ravb_private *priv = netdev_priv(ndev);
3055         const struct ravb_hw_info *info = priv->info;
3056
3057         /* Disable interrupts by clearing the interrupt masks. */
3058         ravb_write(ndev, 0, RIC0);
3059         ravb_write(ndev, 0, RIC2);
3060         ravb_write(ndev, 0, TIC);
3061
3062         /* Only allow ECI interrupts */
3063         synchronize_irq(priv->emac_irq);
3064         if (info->nc_queues)
3065                 napi_disable(&priv->napi[RAVB_NC]);
3066         napi_disable(&priv->napi[RAVB_BE]);
3067         ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
3068
3069         /* Enable MagicPacket */
3070         ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
3071
3072         if (priv->info->ccc_gac)
3073                 ravb_ptp_stop(ndev);
3074
3075         return enable_irq_wake(priv->emac_irq);
3076 }
3077
3078 static int ravb_wol_restore(struct net_device *ndev)
3079 {
3080         struct ravb_private *priv = netdev_priv(ndev);
3081         const struct ravb_hw_info *info = priv->info;
3082         int error;
3083
3084         /* Set reset mode to rearm the WoL logic. */
3085         error = ravb_set_opmode(ndev, CCC_OPC_RESET);
3086         if (error)
3087                 return error;
3088
3089         /* Set AVB config mode. */
3090         error = ravb_set_config_mode(ndev);
3091         if (error)
3092                 return error;
3093
3094         if (priv->info->ccc_gac)
3095                 ravb_ptp_init(ndev, priv->pdev);
3096
3097         if (info->nc_queues)
3098                 napi_enable(&priv->napi[RAVB_NC]);
3099         napi_enable(&priv->napi[RAVB_BE]);
3100
3101         /* Disable MagicPacket */
3102         ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
3103
3104         ravb_close(ndev);
3105
3106         return disable_irq_wake(priv->emac_irq);
3107 }
3108
3109 static int ravb_suspend(struct device *dev)
3110 {
3111         struct net_device *ndev = dev_get_drvdata(dev);
3112         struct ravb_private *priv = netdev_priv(ndev);
3113         int ret;
3114
3115         if (!netif_running(ndev))
3116                 goto reset_assert;
3117
3118         netif_device_detach(ndev);
3119
3120         if (priv->wol_enabled)
3121                 return ravb_wol_setup(ndev);
3122
3123         ret = ravb_close(ndev);
3124         if (ret)
3125                 return ret;
3126
3127         ret = pm_runtime_force_suspend(&priv->pdev->dev);
3128         if (ret)
3129                 return ret;
3130
3131 reset_assert:
3132         return reset_control_assert(priv->rstc);
3133 }
3134
3135 static int ravb_resume(struct device *dev)
3136 {
3137         struct net_device *ndev = dev_get_drvdata(dev);
3138         struct ravb_private *priv = netdev_priv(ndev);
3139         int ret;
3140
3141         ret = reset_control_deassert(priv->rstc);
3142         if (ret)
3143                 return ret;
3144
3145         if (!netif_running(ndev))
3146                 return 0;
3147
3148         /* If WoL is enabled restore the interface. */
3149         if (priv->wol_enabled) {
3150                 ret = ravb_wol_restore(ndev);
3151                 if (ret)
3152                         return ret;
3153         } else {
3154                 ret = pm_runtime_force_resume(dev);
3155                 if (ret)
3156                         return ret;
3157         }
3158
3159         /* Reopening the interface will restore the device to the working state. */
3160         ret = ravb_open(ndev);
3161         if (ret < 0)
3162                 goto out_rpm_put;
3163
3164         ravb_set_rx_mode(ndev);
3165         netif_device_attach(ndev);
3166
3167         return 0;
3168
3169 out_rpm_put:
3170         if (!priv->wol_enabled) {
3171                 pm_runtime_mark_last_busy(dev);
3172                 pm_runtime_put_autosuspend(dev);
3173         }
3174
3175         return ret;
3176 }
3177
3178 static int ravb_runtime_suspend(struct device *dev)
3179 {
3180         struct net_device *ndev = dev_get_drvdata(dev);
3181         struct ravb_private *priv = netdev_priv(ndev);
3182
3183         clk_disable(priv->refclk);
3184
3185         return 0;
3186 }
3187
3188 static int ravb_runtime_resume(struct device *dev)
3189 {
3190         struct net_device *ndev = dev_get_drvdata(dev);
3191         struct ravb_private *priv = netdev_priv(ndev);
3192
3193         return clk_enable(priv->refclk);
3194 }
3195
3196 static const struct dev_pm_ops ravb_dev_pm_ops = {
3197         SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
3198         RUNTIME_PM_OPS(ravb_runtime_suspend, ravb_runtime_resume, NULL)
3199 };
3200
3201 static struct platform_driver ravb_driver = {
3202         .probe          = ravb_probe,
3203         .remove_new     = ravb_remove,
3204         .driver = {
3205                 .name   = "ravb",
3206                 .pm     = pm_ptr(&ravb_dev_pm_ops),
3207                 .of_match_table = ravb_match_table,
3208         },
3209 };
3210
3211 module_platform_driver(ravb_driver);
3212
3213 MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
3214 MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
3215 MODULE_LICENSE("GPL v2");