GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / net / ethernet / renesas / ravb_main.c
1 /* Renesas Ethernet AVB device driver
2  *
3  * Copyright (C) 2014-2019 Renesas Electronics Corporation
4  * Copyright (C) 2015 Renesas Solutions Corp.
5  * Copyright (C) 2015-2016 Cogent Embedded, Inc. <source@cogentembedded.com>
6  *
7  * Based on the SuperH Ethernet driver
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms and conditions of the GNU General Public License version 2,
11  * as published by the Free Software Foundation.
12  */
13
14 #include <linux/cache.h>
15 #include <linux/clk.h>
16 #include <linux/delay.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/err.h>
19 #include <linux/etherdevice.h>
20 #include <linux/ethtool.h>
21 #include <linux/if_vlan.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/net_tstamp.h>
26 #include <linux/of.h>
27 #include <linux/of_device.h>
28 #include <linux/of_irq.h>
29 #include <linux/of_mdio.h>
30 #include <linux/of_net.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/sys_soc.h>
35
36 #include <asm/div64.h>
37
38 #include "ravb.h"
39
40 #define RAVB_DEF_MSG_ENABLE \
41                 (NETIF_MSG_LINK   | \
42                  NETIF_MSG_TIMER  | \
43                  NETIF_MSG_RX_ERR | \
44                  NETIF_MSG_TX_ERR)
45
46 static const char *ravb_rx_irqs[NUM_RX_QUEUE] = {
47         "ch0", /* RAVB_BE */
48         "ch1", /* RAVB_NC */
49 };
50
51 static const char *ravb_tx_irqs[NUM_TX_QUEUE] = {
52         "ch18", /* RAVB_BE */
53         "ch19", /* RAVB_NC */
54 };
55
56 void ravb_modify(struct net_device *ndev, enum ravb_reg reg, u32 clear,
57                  u32 set)
58 {
59         ravb_write(ndev, (ravb_read(ndev, reg) & ~clear) | set, reg);
60 }
61
62 int ravb_wait(struct net_device *ndev, enum ravb_reg reg, u32 mask, u32 value)
63 {
64         int i;
65
66         for (i = 0; i < 10000; i++) {
67                 if ((ravb_read(ndev, reg) & mask) == value)
68                         return 0;
69                 udelay(10);
70         }
71         return -ETIMEDOUT;
72 }
73
74 static int ravb_config(struct net_device *ndev)
75 {
76         int error;
77
78         /* Set config mode */
79         ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
80         /* Check if the operating mode is changed to the config mode */
81         error = ravb_wait(ndev, CSR, CSR_OPS, CSR_OPS_CONFIG);
82         if (error)
83                 netdev_err(ndev, "failed to switch device to config mode\n");
84
85         return error;
86 }
87
88 static void ravb_set_duplex(struct net_device *ndev)
89 {
90         struct ravb_private *priv = netdev_priv(ndev);
91
92         ravb_modify(ndev, ECMR, ECMR_DM, priv->duplex ? ECMR_DM : 0);
93 }
94
95 static void ravb_set_rate(struct net_device *ndev)
96 {
97         struct ravb_private *priv = netdev_priv(ndev);
98
99         switch (priv->speed) {
100         case 100:               /* 100BASE */
101                 ravb_write(ndev, GECMR_SPEED_100, GECMR);
102                 break;
103         case 1000:              /* 1000BASE */
104                 ravb_write(ndev, GECMR_SPEED_1000, GECMR);
105                 break;
106         }
107 }
108
109 static void ravb_set_buffer_align(struct sk_buff *skb)
110 {
111         u32 reserve = (unsigned long)skb->data & (RAVB_ALIGN - 1);
112
113         if (reserve)
114                 skb_reserve(skb, RAVB_ALIGN - reserve);
115 }
116
117 /* Get MAC address from the MAC address registers
118  *
119  * Ethernet AVB device doesn't have ROM for MAC address.
120  * This function gets the MAC address that was used by a bootloader.
121  */
122 static void ravb_read_mac_address(struct net_device *ndev, const u8 *mac)
123 {
124         if (mac) {
125                 ether_addr_copy(ndev->dev_addr, mac);
126         } else {
127                 u32 mahr = ravb_read(ndev, MAHR);
128                 u32 malr = ravb_read(ndev, MALR);
129
130                 ndev->dev_addr[0] = (mahr >> 24) & 0xFF;
131                 ndev->dev_addr[1] = (mahr >> 16) & 0xFF;
132                 ndev->dev_addr[2] = (mahr >>  8) & 0xFF;
133                 ndev->dev_addr[3] = (mahr >>  0) & 0xFF;
134                 ndev->dev_addr[4] = (malr >>  8) & 0xFF;
135                 ndev->dev_addr[5] = (malr >>  0) & 0xFF;
136         }
137 }
138
139 static void ravb_mdio_ctrl(struct mdiobb_ctrl *ctrl, u32 mask, int set)
140 {
141         struct ravb_private *priv = container_of(ctrl, struct ravb_private,
142                                                  mdiobb);
143
144         ravb_modify(priv->ndev, PIR, mask, set ? mask : 0);
145 }
146
147 /* MDC pin control */
148 static void ravb_set_mdc(struct mdiobb_ctrl *ctrl, int level)
149 {
150         ravb_mdio_ctrl(ctrl, PIR_MDC, level);
151 }
152
153 /* Data I/O pin control */
154 static void ravb_set_mdio_dir(struct mdiobb_ctrl *ctrl, int output)
155 {
156         ravb_mdio_ctrl(ctrl, PIR_MMD, output);
157 }
158
159 /* Set data bit */
160 static void ravb_set_mdio_data(struct mdiobb_ctrl *ctrl, int value)
161 {
162         ravb_mdio_ctrl(ctrl, PIR_MDO, value);
163 }
164
165 /* Get data bit */
166 static int ravb_get_mdio_data(struct mdiobb_ctrl *ctrl)
167 {
168         struct ravb_private *priv = container_of(ctrl, struct ravb_private,
169                                                  mdiobb);
170
171         return (ravb_read(priv->ndev, PIR) & PIR_MDI) != 0;
172 }
173
174 /* MDIO bus control struct */
175 static struct mdiobb_ops bb_ops = {
176         .owner = THIS_MODULE,
177         .set_mdc = ravb_set_mdc,
178         .set_mdio_dir = ravb_set_mdio_dir,
179         .set_mdio_data = ravb_set_mdio_data,
180         .get_mdio_data = ravb_get_mdio_data,
181 };
182
183 /* Free TX skb function for AVB-IP */
184 static int ravb_tx_free(struct net_device *ndev, int q, bool free_txed_only)
185 {
186         struct ravb_private *priv = netdev_priv(ndev);
187         struct net_device_stats *stats = &priv->stats[q];
188         struct ravb_tx_desc *desc;
189         int free_num = 0;
190         int entry;
191         u32 size;
192
193         for (; priv->cur_tx[q] - priv->dirty_tx[q] > 0; priv->dirty_tx[q]++) {
194                 bool txed;
195
196                 entry = priv->dirty_tx[q] % (priv->num_tx_ring[q] *
197                                              NUM_TX_DESC);
198                 desc = &priv->tx_ring[q][entry];
199                 txed = desc->die_dt == DT_FEMPTY;
200                 if (free_txed_only && !txed)
201                         break;
202                 /* Descriptor type must be checked before all other reads */
203                 dma_rmb();
204                 size = le16_to_cpu(desc->ds_tagl) & TX_DS;
205                 /* Free the original skb. */
206                 if (priv->tx_skb[q][entry / NUM_TX_DESC]) {
207                         dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
208                                          size, DMA_TO_DEVICE);
209                         /* Last packet descriptor? */
210                         if (entry % NUM_TX_DESC == NUM_TX_DESC - 1) {
211                                 entry /= NUM_TX_DESC;
212                                 dev_kfree_skb_any(priv->tx_skb[q][entry]);
213                                 priv->tx_skb[q][entry] = NULL;
214                                 if (txed)
215                                         stats->tx_packets++;
216                         }
217                         free_num++;
218                 }
219                 if (txed)
220                         stats->tx_bytes += size;
221                 desc->die_dt = DT_EEMPTY;
222         }
223         return free_num;
224 }
225
226 /* Free skb's and DMA buffers for Ethernet AVB */
227 static void ravb_ring_free(struct net_device *ndev, int q)
228 {
229         struct ravb_private *priv = netdev_priv(ndev);
230         int ring_size;
231         int i;
232
233         if (priv->rx_ring[q]) {
234                 for (i = 0; i < priv->num_rx_ring[q]; i++) {
235                         struct ravb_ex_rx_desc *desc = &priv->rx_ring[q][i];
236
237                         if (!dma_mapping_error(ndev->dev.parent,
238                                                le32_to_cpu(desc->dptr)))
239                                 dma_unmap_single(ndev->dev.parent,
240                                                  le32_to_cpu(desc->dptr),
241                                                  PKT_BUF_SZ,
242                                                  DMA_FROM_DEVICE);
243                 }
244                 ring_size = sizeof(struct ravb_ex_rx_desc) *
245                             (priv->num_rx_ring[q] + 1);
246                 dma_free_coherent(ndev->dev.parent, ring_size, priv->rx_ring[q],
247                                   priv->rx_desc_dma[q]);
248                 priv->rx_ring[q] = NULL;
249         }
250
251         if (priv->tx_ring[q]) {
252                 ravb_tx_free(ndev, q, false);
253
254                 ring_size = sizeof(struct ravb_tx_desc) *
255                             (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
256                 dma_free_coherent(ndev->dev.parent, ring_size, priv->tx_ring[q],
257                                   priv->tx_desc_dma[q]);
258                 priv->tx_ring[q] = NULL;
259         }
260
261         /* Free RX skb ringbuffer */
262         if (priv->rx_skb[q]) {
263                 for (i = 0; i < priv->num_rx_ring[q]; i++)
264                         dev_kfree_skb(priv->rx_skb[q][i]);
265         }
266         kfree(priv->rx_skb[q]);
267         priv->rx_skb[q] = NULL;
268
269         /* Free aligned TX buffers */
270         kfree(priv->tx_align[q]);
271         priv->tx_align[q] = NULL;
272
273         /* Free TX skb ringbuffer.
274          * SKBs are freed by ravb_tx_free() call above.
275          */
276         kfree(priv->tx_skb[q]);
277         priv->tx_skb[q] = NULL;
278 }
279
280 /* Format skb and descriptor buffer for Ethernet AVB */
281 static void ravb_ring_format(struct net_device *ndev, int q)
282 {
283         struct ravb_private *priv = netdev_priv(ndev);
284         struct ravb_ex_rx_desc *rx_desc;
285         struct ravb_tx_desc *tx_desc;
286         struct ravb_desc *desc;
287         int rx_ring_size = sizeof(*rx_desc) * priv->num_rx_ring[q];
288         int tx_ring_size = sizeof(*tx_desc) * priv->num_tx_ring[q] *
289                            NUM_TX_DESC;
290         dma_addr_t dma_addr;
291         int i;
292
293         priv->cur_rx[q] = 0;
294         priv->cur_tx[q] = 0;
295         priv->dirty_rx[q] = 0;
296         priv->dirty_tx[q] = 0;
297
298         memset(priv->rx_ring[q], 0, rx_ring_size);
299         /* Build RX ring buffer */
300         for (i = 0; i < priv->num_rx_ring[q]; i++) {
301                 /* RX descriptor */
302                 rx_desc = &priv->rx_ring[q][i];
303                 rx_desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
304                 dma_addr = dma_map_single(ndev->dev.parent, priv->rx_skb[q][i]->data,
305                                           PKT_BUF_SZ,
306                                           DMA_FROM_DEVICE);
307                 /* We just set the data size to 0 for a failed mapping which
308                  * should prevent DMA from happening...
309                  */
310                 if (dma_mapping_error(ndev->dev.parent, dma_addr))
311                         rx_desc->ds_cc = cpu_to_le16(0);
312                 rx_desc->dptr = cpu_to_le32(dma_addr);
313                 rx_desc->die_dt = DT_FEMPTY;
314         }
315         rx_desc = &priv->rx_ring[q][i];
316         rx_desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
317         rx_desc->die_dt = DT_LINKFIX; /* type */
318
319         memset(priv->tx_ring[q], 0, tx_ring_size);
320         /* Build TX ring buffer */
321         for (i = 0, tx_desc = priv->tx_ring[q]; i < priv->num_tx_ring[q];
322              i++, tx_desc++) {
323                 tx_desc->die_dt = DT_EEMPTY;
324                 tx_desc++;
325                 tx_desc->die_dt = DT_EEMPTY;
326         }
327         tx_desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
328         tx_desc->die_dt = DT_LINKFIX; /* type */
329
330         /* RX descriptor base address for best effort */
331         desc = &priv->desc_bat[RX_QUEUE_OFFSET + q];
332         desc->die_dt = DT_LINKFIX; /* type */
333         desc->dptr = cpu_to_le32((u32)priv->rx_desc_dma[q]);
334
335         /* TX descriptor base address for best effort */
336         desc = &priv->desc_bat[q];
337         desc->die_dt = DT_LINKFIX; /* type */
338         desc->dptr = cpu_to_le32((u32)priv->tx_desc_dma[q]);
339 }
340
341 /* Init skb and descriptor buffer for Ethernet AVB */
342 static int ravb_ring_init(struct net_device *ndev, int q)
343 {
344         struct ravb_private *priv = netdev_priv(ndev);
345         struct sk_buff *skb;
346         int ring_size;
347         int i;
348
349         /* Allocate RX and TX skb rings */
350         priv->rx_skb[q] = kcalloc(priv->num_rx_ring[q],
351                                   sizeof(*priv->rx_skb[q]), GFP_KERNEL);
352         priv->tx_skb[q] = kcalloc(priv->num_tx_ring[q],
353                                   sizeof(*priv->tx_skb[q]), GFP_KERNEL);
354         if (!priv->rx_skb[q] || !priv->tx_skb[q])
355                 goto error;
356
357         for (i = 0; i < priv->num_rx_ring[q]; i++) {
358                 skb = netdev_alloc_skb(ndev, PKT_BUF_SZ + RAVB_ALIGN - 1);
359                 if (!skb)
360                         goto error;
361                 ravb_set_buffer_align(skb);
362                 priv->rx_skb[q][i] = skb;
363         }
364
365         /* Allocate rings for the aligned buffers */
366         priv->tx_align[q] = kmalloc(DPTR_ALIGN * priv->num_tx_ring[q] +
367                                     DPTR_ALIGN - 1, GFP_KERNEL);
368         if (!priv->tx_align[q])
369                 goto error;
370
371         /* Allocate all RX descriptors. */
372         ring_size = sizeof(struct ravb_ex_rx_desc) * (priv->num_rx_ring[q] + 1);
373         priv->rx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
374                                               &priv->rx_desc_dma[q],
375                                               GFP_KERNEL);
376         if (!priv->rx_ring[q])
377                 goto error;
378
379         priv->dirty_rx[q] = 0;
380
381         /* Allocate all TX descriptors. */
382         ring_size = sizeof(struct ravb_tx_desc) *
383                     (priv->num_tx_ring[q] * NUM_TX_DESC + 1);
384         priv->tx_ring[q] = dma_alloc_coherent(ndev->dev.parent, ring_size,
385                                               &priv->tx_desc_dma[q],
386                                               GFP_KERNEL);
387         if (!priv->tx_ring[q])
388                 goto error;
389
390         return 0;
391
392 error:
393         ravb_ring_free(ndev, q);
394
395         return -ENOMEM;
396 }
397
398 /* E-MAC init function */
399 static void ravb_emac_init(struct net_device *ndev)
400 {
401         struct ravb_private *priv = netdev_priv(ndev);
402
403         /* Receive frame limit set register */
404         ravb_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN, RFLR);
405
406         /* PAUSE prohibition */
407         ravb_write(ndev, ECMR_ZPF | (priv->duplex ? ECMR_DM : 0) |
408                    ECMR_TE | ECMR_RE, ECMR);
409
410         ravb_set_rate(ndev);
411
412         /* Set MAC address */
413         ravb_write(ndev,
414                    (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
415                    (ndev->dev_addr[2] << 8)  | (ndev->dev_addr[3]), MAHR);
416         ravb_write(ndev,
417                    (ndev->dev_addr[4] << 8)  | (ndev->dev_addr[5]), MALR);
418
419         /* E-MAC status register clear */
420         ravb_write(ndev, ECSR_ICD | ECSR_MPD, ECSR);
421
422         /* E-MAC interrupt enable register */
423         ravb_write(ndev, ECSIPR_ICDIP | ECSIPR_MPDIP | ECSIPR_LCHNGIP, ECSIPR);
424 }
425
426 /* Device init function for Ethernet AVB */
427 static int ravb_dmac_init(struct net_device *ndev)
428 {
429         struct ravb_private *priv = netdev_priv(ndev);
430         int error;
431
432         /* Set CONFIG mode */
433         error = ravb_config(ndev);
434         if (error)
435                 return error;
436
437         error = ravb_ring_init(ndev, RAVB_BE);
438         if (error)
439                 return error;
440         error = ravb_ring_init(ndev, RAVB_NC);
441         if (error) {
442                 ravb_ring_free(ndev, RAVB_BE);
443                 return error;
444         }
445
446         /* Descriptor format */
447         ravb_ring_format(ndev, RAVB_BE);
448         ravb_ring_format(ndev, RAVB_NC);
449
450 #if defined(__LITTLE_ENDIAN)
451         ravb_modify(ndev, CCC, CCC_BOC, 0);
452 #else
453         ravb_modify(ndev, CCC, CCC_BOC, CCC_BOC);
454 #endif
455
456         /* Set AVB RX */
457         ravb_write(ndev,
458                    RCR_EFFS | RCR_ENCF | RCR_ETS0 | RCR_ESF | 0x18000000, RCR);
459
460         /* Set FIFO size */
461         ravb_write(ndev, TGC_TQP_AVBMODE1 | 0x00112200, TGC);
462
463         /* Timestamp enable */
464         ravb_write(ndev, TCCR_TFEN, TCCR);
465
466         /* Interrupt init: */
467         if (priv->chip_id == RCAR_GEN3) {
468                 /* Clear DIL.DPLx */
469                 ravb_write(ndev, 0, DIL);
470                 /* Set queue specific interrupt */
471                 ravb_write(ndev, CIE_CRIE | CIE_CTIE | CIE_CL0M, CIE);
472         }
473         /* Frame receive */
474         ravb_write(ndev, RIC0_FRE0 | RIC0_FRE1, RIC0);
475         /* Disable FIFO full warning */
476         ravb_write(ndev, 0, RIC1);
477         /* Receive FIFO full error, descriptor empty */
478         ravb_write(ndev, RIC2_QFE0 | RIC2_QFE1 | RIC2_RFFE, RIC2);
479         /* Frame transmitted, timestamp FIFO updated */
480         ravb_write(ndev, TIC_FTE0 | TIC_FTE1 | TIC_TFUE, TIC);
481
482         /* Setting the control will start the AVB-DMAC process. */
483         ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_OPERATION);
484
485         return 0;
486 }
487
488 static void ravb_get_tx_tstamp(struct net_device *ndev)
489 {
490         struct ravb_private *priv = netdev_priv(ndev);
491         struct ravb_tstamp_skb *ts_skb, *ts_skb2;
492         struct skb_shared_hwtstamps shhwtstamps;
493         struct sk_buff *skb;
494         struct timespec64 ts;
495         u16 tag, tfa_tag;
496         int count;
497         u32 tfa2;
498
499         count = (ravb_read(ndev, TSR) & TSR_TFFL) >> 8;
500         while (count--) {
501                 tfa2 = ravb_read(ndev, TFA2);
502                 tfa_tag = (tfa2 & TFA2_TST) >> 16;
503                 ts.tv_nsec = (u64)ravb_read(ndev, TFA0);
504                 ts.tv_sec = ((u64)(tfa2 & TFA2_TSV) << 32) |
505                             ravb_read(ndev, TFA1);
506                 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
507                 shhwtstamps.hwtstamp = timespec64_to_ktime(ts);
508                 list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list,
509                                          list) {
510                         skb = ts_skb->skb;
511                         tag = ts_skb->tag;
512                         list_del(&ts_skb->list);
513                         kfree(ts_skb);
514                         if (tag == tfa_tag) {
515                                 skb_tstamp_tx(skb, &shhwtstamps);
516                                 dev_consume_skb_any(skb);
517                                 break;
518                         } else {
519                                 dev_kfree_skb_any(skb);
520                         }
521                 }
522                 ravb_modify(ndev, TCCR, TCCR_TFR, TCCR_TFR);
523         }
524 }
525
526 /* Packet receive function for Ethernet AVB */
527 static bool ravb_rx(struct net_device *ndev, int *quota, int q)
528 {
529         struct ravb_private *priv = netdev_priv(ndev);
530         int entry = priv->cur_rx[q] % priv->num_rx_ring[q];
531         int boguscnt = (priv->dirty_rx[q] + priv->num_rx_ring[q]) -
532                         priv->cur_rx[q];
533         struct net_device_stats *stats = &priv->stats[q];
534         struct ravb_ex_rx_desc *desc;
535         struct sk_buff *skb;
536         dma_addr_t dma_addr;
537         struct timespec64 ts;
538         u8  desc_status;
539         u16 pkt_len;
540         int limit;
541
542         boguscnt = min(boguscnt, *quota);
543         limit = boguscnt;
544         desc = &priv->rx_ring[q][entry];
545         while (desc->die_dt != DT_FEMPTY) {
546                 /* Descriptor type must be checked before all other reads */
547                 dma_rmb();
548                 desc_status = desc->msc;
549                 pkt_len = le16_to_cpu(desc->ds_cc) & RX_DS;
550
551                 if (--boguscnt < 0)
552                         break;
553
554                 /* We use 0-byte descriptors to mark the DMA mapping errors */
555                 if (!pkt_len)
556                         continue;
557
558                 if (desc_status & MSC_MC)
559                         stats->multicast++;
560
561                 if (desc_status & (MSC_CRC | MSC_RFE | MSC_RTSF | MSC_RTLF |
562                                    MSC_CEEF)) {
563                         stats->rx_errors++;
564                         if (desc_status & MSC_CRC)
565                                 stats->rx_crc_errors++;
566                         if (desc_status & MSC_RFE)
567                                 stats->rx_frame_errors++;
568                         if (desc_status & (MSC_RTLF | MSC_RTSF))
569                                 stats->rx_length_errors++;
570                         if (desc_status & MSC_CEEF)
571                                 stats->rx_missed_errors++;
572                 } else {
573                         u32 get_ts = priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE;
574
575                         skb = priv->rx_skb[q][entry];
576                         priv->rx_skb[q][entry] = NULL;
577                         dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
578                                          PKT_BUF_SZ,
579                                          DMA_FROM_DEVICE);
580                         get_ts &= (q == RAVB_NC) ?
581                                         RAVB_RXTSTAMP_TYPE_V2_L2_EVENT :
582                                         ~RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
583                         if (get_ts) {
584                                 struct skb_shared_hwtstamps *shhwtstamps;
585
586                                 shhwtstamps = skb_hwtstamps(skb);
587                                 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
588                                 ts.tv_sec = ((u64) le16_to_cpu(desc->ts_sh) <<
589                                              32) | le32_to_cpu(desc->ts_sl);
590                                 ts.tv_nsec = le32_to_cpu(desc->ts_n);
591                                 shhwtstamps->hwtstamp = timespec64_to_ktime(ts);
592                         }
593                         skb_put(skb, pkt_len);
594                         skb->protocol = eth_type_trans(skb, ndev);
595                         napi_gro_receive(&priv->napi[q], skb);
596                         stats->rx_packets++;
597                         stats->rx_bytes += pkt_len;
598                 }
599
600                 entry = (++priv->cur_rx[q]) % priv->num_rx_ring[q];
601                 desc = &priv->rx_ring[q][entry];
602         }
603
604         /* Refill the RX ring buffers. */
605         for (; priv->cur_rx[q] - priv->dirty_rx[q] > 0; priv->dirty_rx[q]++) {
606                 entry = priv->dirty_rx[q] % priv->num_rx_ring[q];
607                 desc = &priv->rx_ring[q][entry];
608                 desc->ds_cc = cpu_to_le16(PKT_BUF_SZ);
609
610                 if (!priv->rx_skb[q][entry]) {
611                         skb = netdev_alloc_skb(ndev,
612                                                PKT_BUF_SZ + RAVB_ALIGN - 1);
613                         if (!skb)
614                                 break;  /* Better luck next round. */
615                         ravb_set_buffer_align(skb);
616                         dma_addr = dma_map_single(ndev->dev.parent, skb->data,
617                                                   le16_to_cpu(desc->ds_cc),
618                                                   DMA_FROM_DEVICE);
619                         skb_checksum_none_assert(skb);
620                         /* We just set the data size to 0 for a failed mapping
621                          * which should prevent DMA  from happening...
622                          */
623                         if (dma_mapping_error(ndev->dev.parent, dma_addr))
624                                 desc->ds_cc = cpu_to_le16(0);
625                         desc->dptr = cpu_to_le32(dma_addr);
626                         priv->rx_skb[q][entry] = skb;
627                 }
628                 /* Descriptor type must be set after all the above writes */
629                 dma_wmb();
630                 desc->die_dt = DT_FEMPTY;
631         }
632
633         *quota -= limit - (++boguscnt);
634
635         return boguscnt <= 0;
636 }
637
638 static void ravb_rcv_snd_disable(struct net_device *ndev)
639 {
640         /* Disable TX and RX */
641         ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, 0);
642 }
643
644 static void ravb_rcv_snd_enable(struct net_device *ndev)
645 {
646         /* Enable TX and RX */
647         ravb_modify(ndev, ECMR, ECMR_RE | ECMR_TE, ECMR_RE | ECMR_TE);
648 }
649
650 /* function for waiting dma process finished */
651 static int ravb_stop_dma(struct net_device *ndev)
652 {
653         int error;
654
655         /* Wait for stopping the hardware TX process */
656         error = ravb_wait(ndev, TCCR,
657                           TCCR_TSRQ0 | TCCR_TSRQ1 | TCCR_TSRQ2 | TCCR_TSRQ3, 0);
658         if (error)
659                 return error;
660
661         error = ravb_wait(ndev, CSR, CSR_TPO0 | CSR_TPO1 | CSR_TPO2 | CSR_TPO3,
662                           0);
663         if (error)
664                 return error;
665
666         /* Stop the E-MAC's RX/TX processes. */
667         ravb_rcv_snd_disable(ndev);
668
669         /* Wait for stopping the RX DMA process */
670         error = ravb_wait(ndev, CSR, CSR_RPO, 0);
671         if (error)
672                 return error;
673
674         /* Stop AVB-DMAC process */
675         return ravb_config(ndev);
676 }
677
678 /* E-MAC interrupt handler */
679 static void ravb_emac_interrupt_unlocked(struct net_device *ndev)
680 {
681         struct ravb_private *priv = netdev_priv(ndev);
682         u32 ecsr, psr;
683
684         ecsr = ravb_read(ndev, ECSR);
685         ravb_write(ndev, ecsr, ECSR);   /* clear interrupt */
686
687         if (ecsr & ECSR_MPD)
688                 pm_wakeup_event(&priv->pdev->dev, 0);
689         if (ecsr & ECSR_ICD)
690                 ndev->stats.tx_carrier_errors++;
691         if (ecsr & ECSR_LCHNG) {
692                 /* Link changed */
693                 if (priv->no_avb_link)
694                         return;
695                 psr = ravb_read(ndev, PSR);
696                 if (priv->avb_link_active_low)
697                         psr ^= PSR_LMON;
698                 if (!(psr & PSR_LMON)) {
699                         /* DIsable RX and TX */
700                         ravb_rcv_snd_disable(ndev);
701                 } else {
702                         /* Enable RX and TX */
703                         ravb_rcv_snd_enable(ndev);
704                 }
705         }
706 }
707
708 static irqreturn_t ravb_emac_interrupt(int irq, void *dev_id)
709 {
710         struct net_device *ndev = dev_id;
711         struct ravb_private *priv = netdev_priv(ndev);
712
713         spin_lock(&priv->lock);
714         ravb_emac_interrupt_unlocked(ndev);
715         mmiowb();
716         spin_unlock(&priv->lock);
717         return IRQ_HANDLED;
718 }
719
720 /* Error interrupt handler */
721 static void ravb_error_interrupt(struct net_device *ndev)
722 {
723         struct ravb_private *priv = netdev_priv(ndev);
724         u32 eis, ris2;
725
726         eis = ravb_read(ndev, EIS);
727         ravb_write(ndev, ~(EIS_QFS | EIS_RESERVED), EIS);
728         if (eis & EIS_QFS) {
729                 ris2 = ravb_read(ndev, RIS2);
730                 ravb_write(ndev, ~(RIS2_QFF0 | RIS2_RFFF | RIS2_RESERVED),
731                            RIS2);
732
733                 /* Receive Descriptor Empty int */
734                 if (ris2 & RIS2_QFF0)
735                         priv->stats[RAVB_BE].rx_over_errors++;
736
737                     /* Receive Descriptor Empty int */
738                 if (ris2 & RIS2_QFF1)
739                         priv->stats[RAVB_NC].rx_over_errors++;
740
741                 /* Receive FIFO Overflow int */
742                 if (ris2 & RIS2_RFFF)
743                         priv->rx_fifo_errors++;
744         }
745 }
746
747 static bool ravb_queue_interrupt(struct net_device *ndev, int q)
748 {
749         struct ravb_private *priv = netdev_priv(ndev);
750         u32 ris0 = ravb_read(ndev, RIS0);
751         u32 ric0 = ravb_read(ndev, RIC0);
752         u32 tis  = ravb_read(ndev, TIS);
753         u32 tic  = ravb_read(ndev, TIC);
754
755         if (((ris0 & ric0) & BIT(q)) || ((tis  & tic)  & BIT(q))) {
756                 if (napi_schedule_prep(&priv->napi[q])) {
757                         /* Mask RX and TX interrupts */
758                         if (priv->chip_id == RCAR_GEN2) {
759                                 ravb_write(ndev, ric0 & ~BIT(q), RIC0);
760                                 ravb_write(ndev, tic & ~BIT(q), TIC);
761                         } else {
762                                 ravb_write(ndev, BIT(q), RID0);
763                                 ravb_write(ndev, BIT(q), TID);
764                         }
765                         __napi_schedule(&priv->napi[q]);
766                 } else {
767                         netdev_warn(ndev,
768                                     "ignoring interrupt, rx status 0x%08x, rx mask 0x%08x,\n",
769                                     ris0, ric0);
770                         netdev_warn(ndev,
771                                     "                    tx status 0x%08x, tx mask 0x%08x.\n",
772                                     tis, tic);
773                 }
774                 return true;
775         }
776         return false;
777 }
778
779 static bool ravb_timestamp_interrupt(struct net_device *ndev)
780 {
781         u32 tis = ravb_read(ndev, TIS);
782
783         if (tis & TIS_TFUF) {
784                 ravb_write(ndev, ~(TIS_TFUF | TIS_RESERVED), TIS);
785                 ravb_get_tx_tstamp(ndev);
786                 return true;
787         }
788         return false;
789 }
790
791 static irqreturn_t ravb_interrupt(int irq, void *dev_id)
792 {
793         struct net_device *ndev = dev_id;
794         struct ravb_private *priv = netdev_priv(ndev);
795         irqreturn_t result = IRQ_NONE;
796         u32 iss;
797
798         spin_lock(&priv->lock);
799         /* Get interrupt status */
800         iss = ravb_read(ndev, ISS);
801
802         /* Received and transmitted interrupts */
803         if (iss & (ISS_FRS | ISS_FTS | ISS_TFUS)) {
804                 int q;
805
806                 /* Timestamp updated */
807                 if (ravb_timestamp_interrupt(ndev))
808                         result = IRQ_HANDLED;
809
810                 /* Network control and best effort queue RX/TX */
811                 for (q = RAVB_NC; q >= RAVB_BE; q--) {
812                         if (ravb_queue_interrupt(ndev, q))
813                                 result = IRQ_HANDLED;
814                 }
815         }
816
817         /* E-MAC status summary */
818         if (iss & ISS_MS) {
819                 ravb_emac_interrupt_unlocked(ndev);
820                 result = IRQ_HANDLED;
821         }
822
823         /* Error status summary */
824         if (iss & ISS_ES) {
825                 ravb_error_interrupt(ndev);
826                 result = IRQ_HANDLED;
827         }
828
829         /* gPTP interrupt status summary */
830         if (iss & ISS_CGIS) {
831                 ravb_ptp_interrupt(ndev);
832                 result = IRQ_HANDLED;
833         }
834
835         mmiowb();
836         spin_unlock(&priv->lock);
837         return result;
838 }
839
840 /* Timestamp/Error/gPTP interrupt handler */
841 static irqreturn_t ravb_multi_interrupt(int irq, void *dev_id)
842 {
843         struct net_device *ndev = dev_id;
844         struct ravb_private *priv = netdev_priv(ndev);
845         irqreturn_t result = IRQ_NONE;
846         u32 iss;
847
848         spin_lock(&priv->lock);
849         /* Get interrupt status */
850         iss = ravb_read(ndev, ISS);
851
852         /* Timestamp updated */
853         if ((iss & ISS_TFUS) && ravb_timestamp_interrupt(ndev))
854                 result = IRQ_HANDLED;
855
856         /* Error status summary */
857         if (iss & ISS_ES) {
858                 ravb_error_interrupt(ndev);
859                 result = IRQ_HANDLED;
860         }
861
862         /* gPTP interrupt status summary */
863         if (iss & ISS_CGIS) {
864                 ravb_ptp_interrupt(ndev);
865                 result = IRQ_HANDLED;
866         }
867
868         mmiowb();
869         spin_unlock(&priv->lock);
870         return result;
871 }
872
873 static irqreturn_t ravb_dma_interrupt(int irq, void *dev_id, int q)
874 {
875         struct net_device *ndev = dev_id;
876         struct ravb_private *priv = netdev_priv(ndev);
877         irqreturn_t result = IRQ_NONE;
878
879         spin_lock(&priv->lock);
880
881         /* Network control/Best effort queue RX/TX */
882         if (ravb_queue_interrupt(ndev, q))
883                 result = IRQ_HANDLED;
884
885         mmiowb();
886         spin_unlock(&priv->lock);
887         return result;
888 }
889
890 static irqreturn_t ravb_be_interrupt(int irq, void *dev_id)
891 {
892         return ravb_dma_interrupt(irq, dev_id, RAVB_BE);
893 }
894
895 static irqreturn_t ravb_nc_interrupt(int irq, void *dev_id)
896 {
897         return ravb_dma_interrupt(irq, dev_id, RAVB_NC);
898 }
899
900 static int ravb_poll(struct napi_struct *napi, int budget)
901 {
902         struct net_device *ndev = napi->dev;
903         struct ravb_private *priv = netdev_priv(ndev);
904         unsigned long flags;
905         int q = napi - priv->napi;
906         int mask = BIT(q);
907         int quota = budget;
908         u32 ris0, tis;
909
910         for (;;) {
911                 tis = ravb_read(ndev, TIS);
912                 ris0 = ravb_read(ndev, RIS0);
913                 if (!((ris0 & mask) || (tis & mask)))
914                         break;
915
916                 /* Processing RX Descriptor Ring */
917                 if (ris0 & mask) {
918                         /* Clear RX interrupt */
919                         ravb_write(ndev, ~(mask | RIS0_RESERVED), RIS0);
920                         if (ravb_rx(ndev, &quota, q))
921                                 goto out;
922                 }
923                 /* Processing TX Descriptor Ring */
924                 if (tis & mask) {
925                         spin_lock_irqsave(&priv->lock, flags);
926                         /* Clear TX interrupt */
927                         ravb_write(ndev, ~(mask | TIS_RESERVED), TIS);
928                         ravb_tx_free(ndev, q, true);
929                         netif_wake_subqueue(ndev, q);
930                         mmiowb();
931                         spin_unlock_irqrestore(&priv->lock, flags);
932                 }
933         }
934
935         napi_complete(napi);
936
937         /* Re-enable RX/TX interrupts */
938         spin_lock_irqsave(&priv->lock, flags);
939         if (priv->chip_id == RCAR_GEN2) {
940                 ravb_modify(ndev, RIC0, mask, mask);
941                 ravb_modify(ndev, TIC,  mask, mask);
942         } else {
943                 ravb_write(ndev, mask, RIE0);
944                 ravb_write(ndev, mask, TIE);
945         }
946         mmiowb();
947         spin_unlock_irqrestore(&priv->lock, flags);
948
949         /* Receive error message handling */
950         priv->rx_over_errors =  priv->stats[RAVB_BE].rx_over_errors;
951         priv->rx_over_errors += priv->stats[RAVB_NC].rx_over_errors;
952         if (priv->rx_over_errors != ndev->stats.rx_over_errors)
953                 ndev->stats.rx_over_errors = priv->rx_over_errors;
954         if (priv->rx_fifo_errors != ndev->stats.rx_fifo_errors)
955                 ndev->stats.rx_fifo_errors = priv->rx_fifo_errors;
956 out:
957         return budget - quota;
958 }
959
960 /* PHY state control function */
961 static void ravb_adjust_link(struct net_device *ndev)
962 {
963         struct ravb_private *priv = netdev_priv(ndev);
964         struct phy_device *phydev = ndev->phydev;
965         bool new_state = false;
966         unsigned long flags;
967
968         spin_lock_irqsave(&priv->lock, flags);
969
970         /* Disable TX and RX right over here, if E-MAC change is ignored */
971         if (priv->no_avb_link)
972                 ravb_rcv_snd_disable(ndev);
973
974         if (phydev->link) {
975                 if (phydev->duplex != priv->duplex) {
976                         new_state = true;
977                         priv->duplex = phydev->duplex;
978                         ravb_set_duplex(ndev);
979                 }
980
981                 if (phydev->speed != priv->speed) {
982                         new_state = true;
983                         priv->speed = phydev->speed;
984                         ravb_set_rate(ndev);
985                 }
986                 if (!priv->link) {
987                         ravb_modify(ndev, ECMR, ECMR_TXF, 0);
988                         new_state = true;
989                         priv->link = phydev->link;
990                 }
991         } else if (priv->link) {
992                 new_state = true;
993                 priv->link = 0;
994                 priv->speed = 0;
995                 priv->duplex = -1;
996         }
997
998         /* Enable TX and RX right over here, if E-MAC change is ignored */
999         if (priv->no_avb_link && phydev->link)
1000                 ravb_rcv_snd_enable(ndev);
1001
1002         mmiowb();
1003         spin_unlock_irqrestore(&priv->lock, flags);
1004
1005         if (new_state && netif_msg_link(priv))
1006                 phy_print_status(phydev);
1007 }
1008
1009 static const struct soc_device_attribute r8a7795es10[] = {
1010         { .soc_id = "r8a7795", .revision = "ES1.0", },
1011         { /* sentinel */ }
1012 };
1013
1014 /* PHY init function */
1015 static int ravb_phy_init(struct net_device *ndev)
1016 {
1017         struct device_node *np = ndev->dev.parent->of_node;
1018         struct ravb_private *priv = netdev_priv(ndev);
1019         struct phy_device *phydev;
1020         struct device_node *pn;
1021         int err;
1022
1023         priv->link = 0;
1024         priv->speed = 0;
1025         priv->duplex = -1;
1026
1027         /* Try connecting to PHY */
1028         pn = of_parse_phandle(np, "phy-handle", 0);
1029         if (!pn) {
1030                 /* In the case of a fixed PHY, the DT node associated
1031                  * to the PHY is the Ethernet MAC DT node.
1032                  */
1033                 if (of_phy_is_fixed_link(np)) {
1034                         err = of_phy_register_fixed_link(np);
1035                         if (err)
1036                                 return err;
1037                 }
1038                 pn = of_node_get(np);
1039         }
1040         phydev = of_phy_connect(ndev, pn, ravb_adjust_link, 0,
1041                                 priv->phy_interface);
1042         of_node_put(pn);
1043         if (!phydev) {
1044                 netdev_err(ndev, "failed to connect PHY\n");
1045                 err = -ENOENT;
1046                 goto err_deregister_fixed_link;
1047         }
1048
1049         /* This driver only support 10/100Mbit speeds on R-Car H3 ES1.0
1050          * at this time.
1051          */
1052         if (soc_device_match(r8a7795es10)) {
1053                 err = phy_set_max_speed(phydev, SPEED_100);
1054                 if (err) {
1055                         netdev_err(ndev, "failed to limit PHY to 100Mbit/s\n");
1056                         goto err_phy_disconnect;
1057                 }
1058
1059                 netdev_info(ndev, "limited PHY to 100Mbit/s\n");
1060         }
1061
1062         /* 10BASE is not supported */
1063         phydev->supported &= ~PHY_10BT_FEATURES;
1064
1065         phy_attached_info(phydev);
1066
1067         return 0;
1068
1069 err_phy_disconnect:
1070         phy_disconnect(phydev);
1071 err_deregister_fixed_link:
1072         if (of_phy_is_fixed_link(np))
1073                 of_phy_deregister_fixed_link(np);
1074
1075         return err;
1076 }
1077
1078 /* PHY control start function */
1079 static int ravb_phy_start(struct net_device *ndev)
1080 {
1081         int error;
1082
1083         error = ravb_phy_init(ndev);
1084         if (error)
1085                 return error;
1086
1087         phy_start(ndev->phydev);
1088
1089         return 0;
1090 }
1091
1092 static int ravb_get_link_ksettings(struct net_device *ndev,
1093                                    struct ethtool_link_ksettings *cmd)
1094 {
1095         struct ravb_private *priv = netdev_priv(ndev);
1096         unsigned long flags;
1097
1098         if (!ndev->phydev)
1099                 return -ENODEV;
1100
1101         spin_lock_irqsave(&priv->lock, flags);
1102         phy_ethtool_ksettings_get(ndev->phydev, cmd);
1103         spin_unlock_irqrestore(&priv->lock, flags);
1104
1105         return 0;
1106 }
1107
1108 static int ravb_set_link_ksettings(struct net_device *ndev,
1109                                    const struct ethtool_link_ksettings *cmd)
1110 {
1111         if (!ndev->phydev)
1112                 return -ENODEV;
1113
1114         return phy_ethtool_ksettings_set(ndev->phydev, cmd);
1115 }
1116
1117 static int ravb_nway_reset(struct net_device *ndev)
1118 {
1119         int error = -ENODEV;
1120
1121         if (ndev->phydev)
1122                 error = phy_start_aneg(ndev->phydev);
1123
1124         return error;
1125 }
1126
1127 static u32 ravb_get_msglevel(struct net_device *ndev)
1128 {
1129         struct ravb_private *priv = netdev_priv(ndev);
1130
1131         return priv->msg_enable;
1132 }
1133
1134 static void ravb_set_msglevel(struct net_device *ndev, u32 value)
1135 {
1136         struct ravb_private *priv = netdev_priv(ndev);
1137
1138         priv->msg_enable = value;
1139 }
1140
1141 static const char ravb_gstrings_stats[][ETH_GSTRING_LEN] = {
1142         "rx_queue_0_current",
1143         "tx_queue_0_current",
1144         "rx_queue_0_dirty",
1145         "tx_queue_0_dirty",
1146         "rx_queue_0_packets",
1147         "tx_queue_0_packets",
1148         "rx_queue_0_bytes",
1149         "tx_queue_0_bytes",
1150         "rx_queue_0_mcast_packets",
1151         "rx_queue_0_errors",
1152         "rx_queue_0_crc_errors",
1153         "rx_queue_0_frame_errors",
1154         "rx_queue_0_length_errors",
1155         "rx_queue_0_missed_errors",
1156         "rx_queue_0_over_errors",
1157
1158         "rx_queue_1_current",
1159         "tx_queue_1_current",
1160         "rx_queue_1_dirty",
1161         "tx_queue_1_dirty",
1162         "rx_queue_1_packets",
1163         "tx_queue_1_packets",
1164         "rx_queue_1_bytes",
1165         "tx_queue_1_bytes",
1166         "rx_queue_1_mcast_packets",
1167         "rx_queue_1_errors",
1168         "rx_queue_1_crc_errors",
1169         "rx_queue_1_frame_errors",
1170         "rx_queue_1_length_errors",
1171         "rx_queue_1_missed_errors",
1172         "rx_queue_1_over_errors",
1173 };
1174
1175 #define RAVB_STATS_LEN  ARRAY_SIZE(ravb_gstrings_stats)
1176
1177 static int ravb_get_sset_count(struct net_device *netdev, int sset)
1178 {
1179         switch (sset) {
1180         case ETH_SS_STATS:
1181                 return RAVB_STATS_LEN;
1182         default:
1183                 return -EOPNOTSUPP;
1184         }
1185 }
1186
1187 static void ravb_get_ethtool_stats(struct net_device *ndev,
1188                                    struct ethtool_stats *stats, u64 *data)
1189 {
1190         struct ravb_private *priv = netdev_priv(ndev);
1191         int i = 0;
1192         int q;
1193
1194         /* Device-specific stats */
1195         for (q = RAVB_BE; q < NUM_RX_QUEUE; q++) {
1196                 struct net_device_stats *stats = &priv->stats[q];
1197
1198                 data[i++] = priv->cur_rx[q];
1199                 data[i++] = priv->cur_tx[q];
1200                 data[i++] = priv->dirty_rx[q];
1201                 data[i++] = priv->dirty_tx[q];
1202                 data[i++] = stats->rx_packets;
1203                 data[i++] = stats->tx_packets;
1204                 data[i++] = stats->rx_bytes;
1205                 data[i++] = stats->tx_bytes;
1206                 data[i++] = stats->multicast;
1207                 data[i++] = stats->rx_errors;
1208                 data[i++] = stats->rx_crc_errors;
1209                 data[i++] = stats->rx_frame_errors;
1210                 data[i++] = stats->rx_length_errors;
1211                 data[i++] = stats->rx_missed_errors;
1212                 data[i++] = stats->rx_over_errors;
1213         }
1214 }
1215
1216 static void ravb_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1217 {
1218         switch (stringset) {
1219         case ETH_SS_STATS:
1220                 memcpy(data, *ravb_gstrings_stats, sizeof(ravb_gstrings_stats));
1221                 break;
1222         }
1223 }
1224
1225 static void ravb_get_ringparam(struct net_device *ndev,
1226                                struct ethtool_ringparam *ring)
1227 {
1228         struct ravb_private *priv = netdev_priv(ndev);
1229
1230         ring->rx_max_pending = BE_RX_RING_MAX;
1231         ring->tx_max_pending = BE_TX_RING_MAX;
1232         ring->rx_pending = priv->num_rx_ring[RAVB_BE];
1233         ring->tx_pending = priv->num_tx_ring[RAVB_BE];
1234 }
1235
1236 static int ravb_set_ringparam(struct net_device *ndev,
1237                               struct ethtool_ringparam *ring)
1238 {
1239         struct ravb_private *priv = netdev_priv(ndev);
1240         int error;
1241
1242         if (ring->tx_pending > BE_TX_RING_MAX ||
1243             ring->rx_pending > BE_RX_RING_MAX ||
1244             ring->tx_pending < BE_TX_RING_MIN ||
1245             ring->rx_pending < BE_RX_RING_MIN)
1246                 return -EINVAL;
1247         if (ring->rx_mini_pending || ring->rx_jumbo_pending)
1248                 return -EINVAL;
1249
1250         if (netif_running(ndev)) {
1251                 netif_device_detach(ndev);
1252                 /* Stop PTP Clock driver */
1253                 if (priv->chip_id == RCAR_GEN2)
1254                         ravb_ptp_stop(ndev);
1255                 /* Wait for DMA stopping */
1256                 error = ravb_stop_dma(ndev);
1257                 if (error) {
1258                         netdev_err(ndev,
1259                                    "cannot set ringparam! Any AVB processes are still running?\n");
1260                         return error;
1261                 }
1262                 synchronize_irq(ndev->irq);
1263
1264                 /* Free all the skb's in the RX queue and the DMA buffers. */
1265                 ravb_ring_free(ndev, RAVB_BE);
1266                 ravb_ring_free(ndev, RAVB_NC);
1267         }
1268
1269         /* Set new parameters */
1270         priv->num_rx_ring[RAVB_BE] = ring->rx_pending;
1271         priv->num_tx_ring[RAVB_BE] = ring->tx_pending;
1272
1273         if (netif_running(ndev)) {
1274                 error = ravb_dmac_init(ndev);
1275                 if (error) {
1276                         netdev_err(ndev,
1277                                    "%s: ravb_dmac_init() failed, error %d\n",
1278                                    __func__, error);
1279                         return error;
1280                 }
1281
1282                 ravb_emac_init(ndev);
1283
1284                 /* Initialise PTP Clock driver */
1285                 if (priv->chip_id == RCAR_GEN2)
1286                         ravb_ptp_init(ndev, priv->pdev);
1287
1288                 netif_device_attach(ndev);
1289         }
1290
1291         return 0;
1292 }
1293
1294 static int ravb_get_ts_info(struct net_device *ndev,
1295                             struct ethtool_ts_info *info)
1296 {
1297         struct ravb_private *priv = netdev_priv(ndev);
1298
1299         info->so_timestamping =
1300                 SOF_TIMESTAMPING_TX_SOFTWARE |
1301                 SOF_TIMESTAMPING_RX_SOFTWARE |
1302                 SOF_TIMESTAMPING_SOFTWARE |
1303                 SOF_TIMESTAMPING_TX_HARDWARE |
1304                 SOF_TIMESTAMPING_RX_HARDWARE |
1305                 SOF_TIMESTAMPING_RAW_HARDWARE;
1306         info->tx_types = (1 << HWTSTAMP_TX_OFF) | (1 << HWTSTAMP_TX_ON);
1307         info->rx_filters =
1308                 (1 << HWTSTAMP_FILTER_NONE) |
1309                 (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
1310                 (1 << HWTSTAMP_FILTER_ALL);
1311         info->phc_index = ptp_clock_index(priv->ptp.clock);
1312
1313         return 0;
1314 }
1315
1316 static void ravb_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1317 {
1318         struct ravb_private *priv = netdev_priv(ndev);
1319
1320         wol->supported = 0;
1321         wol->wolopts = 0;
1322
1323         if (priv->clk) {
1324                 wol->supported = WAKE_MAGIC;
1325                 wol->wolopts = priv->wol_enabled ? WAKE_MAGIC : 0;
1326         }
1327 }
1328
1329 static int ravb_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
1330 {
1331         struct ravb_private *priv = netdev_priv(ndev);
1332
1333         if (!priv->clk || wol->wolopts & ~WAKE_MAGIC)
1334                 return -EOPNOTSUPP;
1335
1336         priv->wol_enabled = !!(wol->wolopts & WAKE_MAGIC);
1337
1338         device_set_wakeup_enable(&priv->pdev->dev, priv->wol_enabled);
1339
1340         return 0;
1341 }
1342
1343 static const struct ethtool_ops ravb_ethtool_ops = {
1344         .nway_reset             = ravb_nway_reset,
1345         .get_msglevel           = ravb_get_msglevel,
1346         .set_msglevel           = ravb_set_msglevel,
1347         .get_link               = ethtool_op_get_link,
1348         .get_strings            = ravb_get_strings,
1349         .get_ethtool_stats      = ravb_get_ethtool_stats,
1350         .get_sset_count         = ravb_get_sset_count,
1351         .get_ringparam          = ravb_get_ringparam,
1352         .set_ringparam          = ravb_set_ringparam,
1353         .get_ts_info            = ravb_get_ts_info,
1354         .get_link_ksettings     = ravb_get_link_ksettings,
1355         .set_link_ksettings     = ravb_set_link_ksettings,
1356         .get_wol                = ravb_get_wol,
1357         .set_wol                = ravb_set_wol,
1358 };
1359
1360 static inline int ravb_hook_irq(unsigned int irq, irq_handler_t handler,
1361                                 struct net_device *ndev, struct device *dev,
1362                                 const char *ch)
1363 {
1364         char *name;
1365         int error;
1366
1367         name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", ndev->name, ch);
1368         if (!name)
1369                 return -ENOMEM;
1370         error = request_irq(irq, handler, 0, name, ndev);
1371         if (error)
1372                 netdev_err(ndev, "cannot request IRQ %s\n", name);
1373
1374         return error;
1375 }
1376
1377 /* Network device open function for Ethernet AVB */
1378 static int ravb_open(struct net_device *ndev)
1379 {
1380         struct ravb_private *priv = netdev_priv(ndev);
1381         struct platform_device *pdev = priv->pdev;
1382         struct device *dev = &pdev->dev;
1383         int error;
1384
1385         napi_enable(&priv->napi[RAVB_BE]);
1386         napi_enable(&priv->napi[RAVB_NC]);
1387
1388         if (priv->chip_id == RCAR_GEN2) {
1389                 error = request_irq(ndev->irq, ravb_interrupt, IRQF_SHARED,
1390                                     ndev->name, ndev);
1391                 if (error) {
1392                         netdev_err(ndev, "cannot request IRQ\n");
1393                         goto out_napi_off;
1394                 }
1395         } else {
1396                 error = ravb_hook_irq(ndev->irq, ravb_multi_interrupt, ndev,
1397                                       dev, "ch22:multi");
1398                 if (error)
1399                         goto out_napi_off;
1400                 error = ravb_hook_irq(priv->emac_irq, ravb_emac_interrupt, ndev,
1401                                       dev, "ch24:emac");
1402                 if (error)
1403                         goto out_free_irq;
1404                 error = ravb_hook_irq(priv->rx_irqs[RAVB_BE], ravb_be_interrupt,
1405                                       ndev, dev, "ch0:rx_be");
1406                 if (error)
1407                         goto out_free_irq_emac;
1408                 error = ravb_hook_irq(priv->tx_irqs[RAVB_BE], ravb_be_interrupt,
1409                                       ndev, dev, "ch18:tx_be");
1410                 if (error)
1411                         goto out_free_irq_be_rx;
1412                 error = ravb_hook_irq(priv->rx_irqs[RAVB_NC], ravb_nc_interrupt,
1413                                       ndev, dev, "ch1:rx_nc");
1414                 if (error)
1415                         goto out_free_irq_be_tx;
1416                 error = ravb_hook_irq(priv->tx_irqs[RAVB_NC], ravb_nc_interrupt,
1417                                       ndev, dev, "ch19:tx_nc");
1418                 if (error)
1419                         goto out_free_irq_nc_rx;
1420         }
1421
1422         /* Device init */
1423         error = ravb_dmac_init(ndev);
1424         if (error)
1425                 goto out_free_irq_nc_tx;
1426         ravb_emac_init(ndev);
1427
1428         /* Initialise PTP Clock driver */
1429         if (priv->chip_id == RCAR_GEN2)
1430                 ravb_ptp_init(ndev, priv->pdev);
1431
1432         netif_tx_start_all_queues(ndev);
1433
1434         /* PHY control start */
1435         error = ravb_phy_start(ndev);
1436         if (error)
1437                 goto out_ptp_stop;
1438
1439         return 0;
1440
1441 out_ptp_stop:
1442         /* Stop PTP Clock driver */
1443         if (priv->chip_id == RCAR_GEN2)
1444                 ravb_ptp_stop(ndev);
1445 out_free_irq_nc_tx:
1446         if (priv->chip_id == RCAR_GEN2)
1447                 goto out_free_irq;
1448         free_irq(priv->tx_irqs[RAVB_NC], ndev);
1449 out_free_irq_nc_rx:
1450         free_irq(priv->rx_irqs[RAVB_NC], ndev);
1451 out_free_irq_be_tx:
1452         free_irq(priv->tx_irqs[RAVB_BE], ndev);
1453 out_free_irq_be_rx:
1454         free_irq(priv->rx_irqs[RAVB_BE], ndev);
1455 out_free_irq_emac:
1456         free_irq(priv->emac_irq, ndev);
1457 out_free_irq:
1458         free_irq(ndev->irq, ndev);
1459 out_napi_off:
1460         napi_disable(&priv->napi[RAVB_NC]);
1461         napi_disable(&priv->napi[RAVB_BE]);
1462         return error;
1463 }
1464
1465 /* Timeout function for Ethernet AVB */
1466 static void ravb_tx_timeout(struct net_device *ndev)
1467 {
1468         struct ravb_private *priv = netdev_priv(ndev);
1469
1470         netif_err(priv, tx_err, ndev,
1471                   "transmit timed out, status %08x, resetting...\n",
1472                   ravb_read(ndev, ISS));
1473
1474         /* tx_errors count up */
1475         ndev->stats.tx_errors++;
1476
1477         schedule_work(&priv->work);
1478 }
1479
1480 static void ravb_tx_timeout_work(struct work_struct *work)
1481 {
1482         struct ravb_private *priv = container_of(work, struct ravb_private,
1483                                                  work);
1484         struct net_device *ndev = priv->ndev;
1485         int error;
1486
1487         netif_tx_stop_all_queues(ndev);
1488
1489         /* Stop PTP Clock driver */
1490         if (priv->chip_id == RCAR_GEN2)
1491                 ravb_ptp_stop(ndev);
1492
1493         /* Wait for DMA stopping */
1494         if (ravb_stop_dma(ndev)) {
1495                 /* If ravb_stop_dma() fails, the hardware is still operating
1496                  * for TX and/or RX. So, this should not call the following
1497                  * functions because ravb_dmac_init() is possible to fail too.
1498                  * Also, this should not retry ravb_stop_dma() again and again
1499                  * here because it's possible to wait forever. So, this just
1500                  * re-enables the TX and RX and skip the following
1501                  * re-initialization procedure.
1502                  */
1503                 ravb_rcv_snd_enable(ndev);
1504                 goto out;
1505         }
1506
1507         ravb_ring_free(ndev, RAVB_BE);
1508         ravb_ring_free(ndev, RAVB_NC);
1509
1510         /* Device init */
1511         error = ravb_dmac_init(ndev);
1512         if (error) {
1513                 /* If ravb_dmac_init() fails, descriptors are freed. So, this
1514                  * should return here to avoid re-enabling the TX and RX in
1515                  * ravb_emac_init().
1516                  */
1517                 netdev_err(ndev, "%s: ravb_dmac_init() failed, error %d\n",
1518                            __func__, error);
1519                 return;
1520         }
1521         ravb_emac_init(ndev);
1522
1523 out:
1524         /* Initialise PTP Clock driver */
1525         if (priv->chip_id == RCAR_GEN2)
1526                 ravb_ptp_init(ndev, priv->pdev);
1527
1528         netif_tx_start_all_queues(ndev);
1529 }
1530
1531 /* Packet transmit function for Ethernet AVB */
1532 static netdev_tx_t ravb_start_xmit(struct sk_buff *skb, struct net_device *ndev)
1533 {
1534         struct ravb_private *priv = netdev_priv(ndev);
1535         u16 q = skb_get_queue_mapping(skb);
1536         struct ravb_tstamp_skb *ts_skb;
1537         struct ravb_tx_desc *desc;
1538         unsigned long flags;
1539         u32 dma_addr;
1540         void *buffer;
1541         u32 entry;
1542         u32 len;
1543
1544         spin_lock_irqsave(&priv->lock, flags);
1545         if (priv->cur_tx[q] - priv->dirty_tx[q] > (priv->num_tx_ring[q] - 1) *
1546             NUM_TX_DESC) {
1547                 netif_err(priv, tx_queued, ndev,
1548                           "still transmitting with the full ring!\n");
1549                 netif_stop_subqueue(ndev, q);
1550                 spin_unlock_irqrestore(&priv->lock, flags);
1551                 return NETDEV_TX_BUSY;
1552         }
1553
1554         if (skb_put_padto(skb, ETH_ZLEN))
1555                 goto exit;
1556
1557         entry = priv->cur_tx[q] % (priv->num_tx_ring[q] * NUM_TX_DESC);
1558         priv->tx_skb[q][entry / NUM_TX_DESC] = skb;
1559
1560         buffer = PTR_ALIGN(priv->tx_align[q], DPTR_ALIGN) +
1561                  entry / NUM_TX_DESC * DPTR_ALIGN;
1562         len = PTR_ALIGN(skb->data, DPTR_ALIGN) - skb->data;
1563         /* Zero length DMA descriptors are problematic as they seem to
1564          * terminate DMA transfers. Avoid them by simply using a length of
1565          * DPTR_ALIGN (4) when skb data is aligned to DPTR_ALIGN.
1566          *
1567          * As skb is guaranteed to have at least ETH_ZLEN (60) bytes of
1568          * data by the call to skb_put_padto() above this is safe with
1569          * respect to both the length of the first DMA descriptor (len)
1570          * overflowing the available data and the length of the second DMA
1571          * descriptor (skb->len - len) being negative.
1572          */
1573         if (len == 0)
1574                 len = DPTR_ALIGN;
1575
1576         memcpy(buffer, skb->data, len);
1577         dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1578         if (dma_mapping_error(ndev->dev.parent, dma_addr))
1579                 goto drop;
1580
1581         desc = &priv->tx_ring[q][entry];
1582         desc->ds_tagl = cpu_to_le16(len);
1583         desc->dptr = cpu_to_le32(dma_addr);
1584
1585         buffer = skb->data + len;
1586         len = skb->len - len;
1587         dma_addr = dma_map_single(ndev->dev.parent, buffer, len, DMA_TO_DEVICE);
1588         if (dma_mapping_error(ndev->dev.parent, dma_addr))
1589                 goto unmap;
1590
1591         desc++;
1592         desc->ds_tagl = cpu_to_le16(len);
1593         desc->dptr = cpu_to_le32(dma_addr);
1594
1595         /* TX timestamp required */
1596         if (q == RAVB_NC) {
1597                 ts_skb = kmalloc(sizeof(*ts_skb), GFP_ATOMIC);
1598                 if (!ts_skb) {
1599                         desc--;
1600                         dma_unmap_single(ndev->dev.parent, dma_addr, len,
1601                                          DMA_TO_DEVICE);
1602                         goto unmap;
1603                 }
1604                 ts_skb->skb = skb_get(skb);
1605                 ts_skb->tag = priv->ts_skb_tag++;
1606                 priv->ts_skb_tag &= 0x3ff;
1607                 list_add_tail(&ts_skb->list, &priv->ts_skb_list);
1608
1609                 /* TAG and timestamp required flag */
1610                 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1611                 desc->tagh_tsr = (ts_skb->tag >> 4) | TX_TSR;
1612                 desc->ds_tagl |= le16_to_cpu(ts_skb->tag << 12);
1613         }
1614
1615         skb_tx_timestamp(skb);
1616         /* Descriptor type must be set after all the above writes */
1617         dma_wmb();
1618         desc->die_dt = DT_FEND;
1619         desc--;
1620         desc->die_dt = DT_FSTART;
1621
1622         ravb_modify(ndev, TCCR, TCCR_TSRQ0 << q, TCCR_TSRQ0 << q);
1623
1624         priv->cur_tx[q] += NUM_TX_DESC;
1625         if (priv->cur_tx[q] - priv->dirty_tx[q] >
1626             (priv->num_tx_ring[q] - 1) * NUM_TX_DESC &&
1627             !ravb_tx_free(ndev, q, true))
1628                 netif_stop_subqueue(ndev, q);
1629
1630 exit:
1631         mmiowb();
1632         spin_unlock_irqrestore(&priv->lock, flags);
1633         return NETDEV_TX_OK;
1634
1635 unmap:
1636         dma_unmap_single(ndev->dev.parent, le32_to_cpu(desc->dptr),
1637                          le16_to_cpu(desc->ds_tagl), DMA_TO_DEVICE);
1638 drop:
1639         dev_kfree_skb_any(skb);
1640         priv->tx_skb[q][entry / NUM_TX_DESC] = NULL;
1641         goto exit;
1642 }
1643
1644 static u16 ravb_select_queue(struct net_device *ndev, struct sk_buff *skb,
1645                              void *accel_priv, select_queue_fallback_t fallback)
1646 {
1647         /* If skb needs TX timestamp, it is handled in network control queue */
1648         return (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) ? RAVB_NC :
1649                                                                RAVB_BE;
1650
1651 }
1652
1653 static struct net_device_stats *ravb_get_stats(struct net_device *ndev)
1654 {
1655         struct ravb_private *priv = netdev_priv(ndev);
1656         struct net_device_stats *nstats, *stats0, *stats1;
1657
1658         nstats = &ndev->stats;
1659         stats0 = &priv->stats[RAVB_BE];
1660         stats1 = &priv->stats[RAVB_NC];
1661
1662         nstats->tx_dropped += ravb_read(ndev, TROCR);
1663         ravb_write(ndev, 0, TROCR);     /* (write clear) */
1664         nstats->collisions += ravb_read(ndev, CDCR);
1665         ravb_write(ndev, 0, CDCR);      /* (write clear) */
1666         nstats->tx_carrier_errors += ravb_read(ndev, LCCR);
1667         ravb_write(ndev, 0, LCCR);      /* (write clear) */
1668
1669         nstats->tx_carrier_errors += ravb_read(ndev, CERCR);
1670         ravb_write(ndev, 0, CERCR);     /* (write clear) */
1671         nstats->tx_carrier_errors += ravb_read(ndev, CEECR);
1672         ravb_write(ndev, 0, CEECR);     /* (write clear) */
1673
1674         nstats->rx_packets = stats0->rx_packets + stats1->rx_packets;
1675         nstats->tx_packets = stats0->tx_packets + stats1->tx_packets;
1676         nstats->rx_bytes = stats0->rx_bytes + stats1->rx_bytes;
1677         nstats->tx_bytes = stats0->tx_bytes + stats1->tx_bytes;
1678         nstats->multicast = stats0->multicast + stats1->multicast;
1679         nstats->rx_errors = stats0->rx_errors + stats1->rx_errors;
1680         nstats->rx_crc_errors = stats0->rx_crc_errors + stats1->rx_crc_errors;
1681         nstats->rx_frame_errors =
1682                 stats0->rx_frame_errors + stats1->rx_frame_errors;
1683         nstats->rx_length_errors =
1684                 stats0->rx_length_errors + stats1->rx_length_errors;
1685         nstats->rx_missed_errors =
1686                 stats0->rx_missed_errors + stats1->rx_missed_errors;
1687         nstats->rx_over_errors =
1688                 stats0->rx_over_errors + stats1->rx_over_errors;
1689
1690         return nstats;
1691 }
1692
1693 /* Update promiscuous bit */
1694 static void ravb_set_rx_mode(struct net_device *ndev)
1695 {
1696         struct ravb_private *priv = netdev_priv(ndev);
1697         unsigned long flags;
1698
1699         spin_lock_irqsave(&priv->lock, flags);
1700         ravb_modify(ndev, ECMR, ECMR_PRM,
1701                     ndev->flags & IFF_PROMISC ? ECMR_PRM : 0);
1702         mmiowb();
1703         spin_unlock_irqrestore(&priv->lock, flags);
1704 }
1705
1706 /* Device close function for Ethernet AVB */
1707 static int ravb_close(struct net_device *ndev)
1708 {
1709         struct device_node *np = ndev->dev.parent->of_node;
1710         struct ravb_private *priv = netdev_priv(ndev);
1711         struct ravb_tstamp_skb *ts_skb, *ts_skb2;
1712
1713         netif_tx_stop_all_queues(ndev);
1714
1715         /* Disable interrupts by clearing the interrupt masks. */
1716         ravb_write(ndev, 0, RIC0);
1717         ravb_write(ndev, 0, RIC2);
1718         ravb_write(ndev, 0, TIC);
1719
1720         /* Stop PTP Clock driver */
1721         if (priv->chip_id == RCAR_GEN2)
1722                 ravb_ptp_stop(ndev);
1723
1724         /* Set the config mode to stop the AVB-DMAC's processes */
1725         if (ravb_stop_dma(ndev) < 0)
1726                 netdev_err(ndev,
1727                            "device will be stopped after h/w processes are done.\n");
1728
1729         /* Clear the timestamp list */
1730         list_for_each_entry_safe(ts_skb, ts_skb2, &priv->ts_skb_list, list) {
1731                 list_del(&ts_skb->list);
1732                 kfree_skb(ts_skb->skb);
1733                 kfree(ts_skb);
1734         }
1735
1736         /* PHY disconnect */
1737         if (ndev->phydev) {
1738                 phy_stop(ndev->phydev);
1739                 phy_disconnect(ndev->phydev);
1740                 if (of_phy_is_fixed_link(np))
1741                         of_phy_deregister_fixed_link(np);
1742         }
1743
1744         if (priv->chip_id != RCAR_GEN2) {
1745                 free_irq(priv->tx_irqs[RAVB_NC], ndev);
1746                 free_irq(priv->rx_irqs[RAVB_NC], ndev);
1747                 free_irq(priv->tx_irqs[RAVB_BE], ndev);
1748                 free_irq(priv->rx_irqs[RAVB_BE], ndev);
1749                 free_irq(priv->emac_irq, ndev);
1750         }
1751         free_irq(ndev->irq, ndev);
1752
1753         napi_disable(&priv->napi[RAVB_NC]);
1754         napi_disable(&priv->napi[RAVB_BE]);
1755
1756         /* Free all the skb's in the RX queue and the DMA buffers. */
1757         ravb_ring_free(ndev, RAVB_BE);
1758         ravb_ring_free(ndev, RAVB_NC);
1759
1760         return 0;
1761 }
1762
1763 static int ravb_hwtstamp_get(struct net_device *ndev, struct ifreq *req)
1764 {
1765         struct ravb_private *priv = netdev_priv(ndev);
1766         struct hwtstamp_config config;
1767
1768         config.flags = 0;
1769         config.tx_type = priv->tstamp_tx_ctrl ? HWTSTAMP_TX_ON :
1770                                                 HWTSTAMP_TX_OFF;
1771         switch (priv->tstamp_rx_ctrl & RAVB_RXTSTAMP_TYPE) {
1772         case RAVB_RXTSTAMP_TYPE_V2_L2_EVENT:
1773                 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1774                 break;
1775         case RAVB_RXTSTAMP_TYPE_ALL:
1776                 config.rx_filter = HWTSTAMP_FILTER_ALL;
1777                 break;
1778         default:
1779                 config.rx_filter = HWTSTAMP_FILTER_NONE;
1780         }
1781
1782         return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1783                 -EFAULT : 0;
1784 }
1785
1786 /* Control hardware time stamping */
1787 static int ravb_hwtstamp_set(struct net_device *ndev, struct ifreq *req)
1788 {
1789         struct ravb_private *priv = netdev_priv(ndev);
1790         struct hwtstamp_config config;
1791         u32 tstamp_rx_ctrl = RAVB_RXTSTAMP_ENABLED;
1792         u32 tstamp_tx_ctrl;
1793
1794         if (copy_from_user(&config, req->ifr_data, sizeof(config)))
1795                 return -EFAULT;
1796
1797         /* Reserved for future extensions */
1798         if (config.flags)
1799                 return -EINVAL;
1800
1801         switch (config.tx_type) {
1802         case HWTSTAMP_TX_OFF:
1803                 tstamp_tx_ctrl = 0;
1804                 break;
1805         case HWTSTAMP_TX_ON:
1806                 tstamp_tx_ctrl = RAVB_TXTSTAMP_ENABLED;
1807                 break;
1808         default:
1809                 return -ERANGE;
1810         }
1811
1812         switch (config.rx_filter) {
1813         case HWTSTAMP_FILTER_NONE:
1814                 tstamp_rx_ctrl = 0;
1815                 break;
1816         case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
1817                 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_V2_L2_EVENT;
1818                 break;
1819         default:
1820                 config.rx_filter = HWTSTAMP_FILTER_ALL;
1821                 tstamp_rx_ctrl |= RAVB_RXTSTAMP_TYPE_ALL;
1822         }
1823
1824         priv->tstamp_tx_ctrl = tstamp_tx_ctrl;
1825         priv->tstamp_rx_ctrl = tstamp_rx_ctrl;
1826
1827         return copy_to_user(req->ifr_data, &config, sizeof(config)) ?
1828                 -EFAULT : 0;
1829 }
1830
1831 /* ioctl to device function */
1832 static int ravb_do_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
1833 {
1834         struct phy_device *phydev = ndev->phydev;
1835
1836         if (!netif_running(ndev))
1837                 return -EINVAL;
1838
1839         if (!phydev)
1840                 return -ENODEV;
1841
1842         switch (cmd) {
1843         case SIOCGHWTSTAMP:
1844                 return ravb_hwtstamp_get(ndev, req);
1845         case SIOCSHWTSTAMP:
1846                 return ravb_hwtstamp_set(ndev, req);
1847         }
1848
1849         return phy_mii_ioctl(phydev, req, cmd);
1850 }
1851
1852 static const struct net_device_ops ravb_netdev_ops = {
1853         .ndo_open               = ravb_open,
1854         .ndo_stop               = ravb_close,
1855         .ndo_start_xmit         = ravb_start_xmit,
1856         .ndo_select_queue       = ravb_select_queue,
1857         .ndo_get_stats          = ravb_get_stats,
1858         .ndo_set_rx_mode        = ravb_set_rx_mode,
1859         .ndo_tx_timeout         = ravb_tx_timeout,
1860         .ndo_do_ioctl           = ravb_do_ioctl,
1861         .ndo_validate_addr      = eth_validate_addr,
1862         .ndo_set_mac_address    = eth_mac_addr,
1863 };
1864
1865 /* MDIO bus init function */
1866 static int ravb_mdio_init(struct ravb_private *priv)
1867 {
1868         struct platform_device *pdev = priv->pdev;
1869         struct device *dev = &pdev->dev;
1870         int error;
1871
1872         /* Bitbang init */
1873         priv->mdiobb.ops = &bb_ops;
1874
1875         /* MII controller setting */
1876         priv->mii_bus = alloc_mdio_bitbang(&priv->mdiobb);
1877         if (!priv->mii_bus)
1878                 return -ENOMEM;
1879
1880         /* Hook up MII support for ethtool */
1881         priv->mii_bus->name = "ravb_mii";
1882         priv->mii_bus->parent = dev;
1883         snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
1884                  pdev->name, pdev->id);
1885
1886         /* Register MDIO bus */
1887         error = of_mdiobus_register(priv->mii_bus, dev->of_node);
1888         if (error)
1889                 goto out_free_bus;
1890
1891         return 0;
1892
1893 out_free_bus:
1894         free_mdio_bitbang(priv->mii_bus);
1895         return error;
1896 }
1897
1898 /* MDIO bus release function */
1899 static int ravb_mdio_release(struct ravb_private *priv)
1900 {
1901         /* Unregister mdio bus */
1902         mdiobus_unregister(priv->mii_bus);
1903
1904         /* Free bitbang info */
1905         free_mdio_bitbang(priv->mii_bus);
1906
1907         return 0;
1908 }
1909
1910 static const struct of_device_id ravb_match_table[] = {
1911         { .compatible = "renesas,etheravb-r8a7790", .data = (void *)RCAR_GEN2 },
1912         { .compatible = "renesas,etheravb-r8a7794", .data = (void *)RCAR_GEN2 },
1913         { .compatible = "renesas,etheravb-rcar-gen2", .data = (void *)RCAR_GEN2 },
1914         { .compatible = "renesas,etheravb-r8a7795", .data = (void *)RCAR_GEN3 },
1915         { .compatible = "renesas,etheravb-rcar-gen3", .data = (void *)RCAR_GEN3 },
1916         { }
1917 };
1918 MODULE_DEVICE_TABLE(of, ravb_match_table);
1919
1920 static int ravb_set_gti(struct net_device *ndev)
1921 {
1922
1923         struct device *dev = ndev->dev.parent;
1924         struct device_node *np = dev->of_node;
1925         unsigned long rate;
1926         struct clk *clk;
1927         uint64_t inc;
1928
1929         clk = of_clk_get(np, 0);
1930         if (IS_ERR(clk)) {
1931                 dev_err(dev, "could not get clock\n");
1932                 return PTR_ERR(clk);
1933         }
1934
1935         rate = clk_get_rate(clk);
1936         clk_put(clk);
1937
1938         if (!rate)
1939                 return -EINVAL;
1940
1941         inc = 1000000000ULL << 20;
1942         do_div(inc, rate);
1943
1944         if (inc < GTI_TIV_MIN || inc > GTI_TIV_MAX) {
1945                 dev_err(dev, "gti.tiv increment 0x%llx is outside the range 0x%x - 0x%x\n",
1946                         inc, GTI_TIV_MIN, GTI_TIV_MAX);
1947                 return -EINVAL;
1948         }
1949
1950         ravb_write(ndev, inc, GTI);
1951
1952         return 0;
1953 }
1954
1955 static void ravb_set_config_mode(struct net_device *ndev)
1956 {
1957         struct ravb_private *priv = netdev_priv(ndev);
1958
1959         if (priv->chip_id == RCAR_GEN2) {
1960                 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG);
1961                 /* Set CSEL value */
1962                 ravb_modify(ndev, CCC, CCC_CSEL, CCC_CSEL_HPB);
1963         } else {
1964                 ravb_modify(ndev, CCC, CCC_OPC, CCC_OPC_CONFIG |
1965                             CCC_GAC | CCC_CSEL_HPB);
1966         }
1967 }
1968
1969 /* Set tx and rx clock internal delay modes */
1970 static void ravb_set_delay_mode(struct net_device *ndev)
1971 {
1972         struct ravb_private *priv = netdev_priv(ndev);
1973         int set = 0;
1974
1975         if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1976             priv->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID)
1977                 set |= APSR_DM_RDM;
1978
1979         if (priv->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
1980             priv->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID)
1981                 set |= APSR_DM_TDM;
1982
1983         ravb_modify(ndev, APSR, APSR_DM, set);
1984 }
1985
1986 static int ravb_probe(struct platform_device *pdev)
1987 {
1988         struct device_node *np = pdev->dev.of_node;
1989         struct ravb_private *priv;
1990         enum ravb_chip_id chip_id;
1991         struct net_device *ndev;
1992         int error, irq, q;
1993         struct resource *res;
1994         int i;
1995
1996         if (!np) {
1997                 dev_err(&pdev->dev,
1998                         "this driver is required to be instantiated from device tree\n");
1999                 return -EINVAL;
2000         }
2001
2002         /* Get base address */
2003         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2004         if (!res) {
2005                 dev_err(&pdev->dev, "invalid resource\n");
2006                 return -EINVAL;
2007         }
2008
2009         ndev = alloc_etherdev_mqs(sizeof(struct ravb_private),
2010                                   NUM_TX_QUEUE, NUM_RX_QUEUE);
2011         if (!ndev)
2012                 return -ENOMEM;
2013
2014         pm_runtime_enable(&pdev->dev);
2015         pm_runtime_get_sync(&pdev->dev);
2016
2017         /* The Ether-specific entries in the device structure. */
2018         ndev->base_addr = res->start;
2019
2020         chip_id = (enum ravb_chip_id)of_device_get_match_data(&pdev->dev);
2021
2022         if (chip_id == RCAR_GEN3)
2023                 irq = platform_get_irq_byname(pdev, "ch22");
2024         else
2025                 irq = platform_get_irq(pdev, 0);
2026         if (irq < 0) {
2027                 error = irq;
2028                 goto out_release;
2029         }
2030         ndev->irq = irq;
2031
2032         SET_NETDEV_DEV(ndev, &pdev->dev);
2033
2034         priv = netdev_priv(ndev);
2035         priv->ndev = ndev;
2036         priv->pdev = pdev;
2037         priv->num_tx_ring[RAVB_BE] = BE_TX_RING_SIZE;
2038         priv->num_rx_ring[RAVB_BE] = BE_RX_RING_SIZE;
2039         priv->num_tx_ring[RAVB_NC] = NC_TX_RING_SIZE;
2040         priv->num_rx_ring[RAVB_NC] = NC_RX_RING_SIZE;
2041         priv->addr = devm_ioremap_resource(&pdev->dev, res);
2042         if (IS_ERR(priv->addr)) {
2043                 error = PTR_ERR(priv->addr);
2044                 goto out_release;
2045         }
2046
2047         spin_lock_init(&priv->lock);
2048         INIT_WORK(&priv->work, ravb_tx_timeout_work);
2049
2050         priv->phy_interface = of_get_phy_mode(np);
2051
2052         priv->no_avb_link = of_property_read_bool(np, "renesas,no-ether-link");
2053         priv->avb_link_active_low =
2054                 of_property_read_bool(np, "renesas,ether-link-active-low");
2055
2056         if (chip_id == RCAR_GEN3) {
2057                 irq = platform_get_irq_byname(pdev, "ch24");
2058                 if (irq < 0) {
2059                         error = irq;
2060                         goto out_release;
2061                 }
2062                 priv->emac_irq = irq;
2063                 for (i = 0; i < NUM_RX_QUEUE; i++) {
2064                         irq = platform_get_irq_byname(pdev, ravb_rx_irqs[i]);
2065                         if (irq < 0) {
2066                                 error = irq;
2067                                 goto out_release;
2068                         }
2069                         priv->rx_irqs[i] = irq;
2070                 }
2071                 for (i = 0; i < NUM_TX_QUEUE; i++) {
2072                         irq = platform_get_irq_byname(pdev, ravb_tx_irqs[i]);
2073                         if (irq < 0) {
2074                                 error = irq;
2075                                 goto out_release;
2076                         }
2077                         priv->tx_irqs[i] = irq;
2078                 }
2079         }
2080
2081         priv->chip_id = chip_id;
2082
2083         /* Get clock, if not found that's OK but Wake-On-Lan is unavailable */
2084         priv->clk = devm_clk_get(&pdev->dev, NULL);
2085         if (IS_ERR(priv->clk))
2086                 priv->clk = NULL;
2087
2088         /* Set function */
2089         ndev->netdev_ops = &ravb_netdev_ops;
2090         ndev->ethtool_ops = &ravb_ethtool_ops;
2091
2092         /* Set AVB config mode */
2093         ravb_set_config_mode(ndev);
2094
2095         /* Set GTI value */
2096         error = ravb_set_gti(ndev);
2097         if (error)
2098                 goto out_release;
2099
2100         /* Request GTI loading */
2101         ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2102
2103         if (priv->chip_id != RCAR_GEN2)
2104                 ravb_set_delay_mode(ndev);
2105
2106         /* Allocate descriptor base address table */
2107         priv->desc_bat_size = sizeof(struct ravb_desc) * DBAT_ENTRY_NUM;
2108         priv->desc_bat = dma_alloc_coherent(ndev->dev.parent, priv->desc_bat_size,
2109                                             &priv->desc_bat_dma, GFP_KERNEL);
2110         if (!priv->desc_bat) {
2111                 dev_err(&pdev->dev,
2112                         "Cannot allocate desc base address table (size %d bytes)\n",
2113                         priv->desc_bat_size);
2114                 error = -ENOMEM;
2115                 goto out_release;
2116         }
2117         for (q = RAVB_BE; q < DBAT_ENTRY_NUM; q++)
2118                 priv->desc_bat[q].die_dt = DT_EOS;
2119         ravb_write(ndev, priv->desc_bat_dma, DBAT);
2120
2121         /* Initialise HW timestamp list */
2122         INIT_LIST_HEAD(&priv->ts_skb_list);
2123
2124         /* Initialise PTP Clock driver */
2125         if (chip_id != RCAR_GEN2)
2126                 ravb_ptp_init(ndev, pdev);
2127
2128         /* Debug message level */
2129         priv->msg_enable = RAVB_DEF_MSG_ENABLE;
2130
2131         /* Read and set MAC address */
2132         ravb_read_mac_address(ndev, of_get_mac_address(np));
2133         if (!is_valid_ether_addr(ndev->dev_addr)) {
2134                 dev_warn(&pdev->dev,
2135                          "no valid MAC address supplied, using a random one\n");
2136                 eth_hw_addr_random(ndev);
2137         }
2138
2139         /* MDIO bus init */
2140         error = ravb_mdio_init(priv);
2141         if (error) {
2142                 dev_err(&pdev->dev, "failed to initialize MDIO\n");
2143                 goto out_dma_free;
2144         }
2145
2146         netif_napi_add(ndev, &priv->napi[RAVB_BE], ravb_poll, 64);
2147         netif_napi_add(ndev, &priv->napi[RAVB_NC], ravb_poll, 64);
2148
2149         /* Network device register */
2150         error = register_netdev(ndev);
2151         if (error)
2152                 goto out_napi_del;
2153
2154         if (priv->clk)
2155                 device_set_wakeup_capable(&pdev->dev, 1);
2156
2157         /* Print device information */
2158         netdev_info(ndev, "Base address at %#x, %pM, IRQ %d.\n",
2159                     (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
2160
2161         platform_set_drvdata(pdev, ndev);
2162
2163         return 0;
2164
2165 out_napi_del:
2166         netif_napi_del(&priv->napi[RAVB_NC]);
2167         netif_napi_del(&priv->napi[RAVB_BE]);
2168         ravb_mdio_release(priv);
2169 out_dma_free:
2170         dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
2171                           priv->desc_bat_dma);
2172
2173         /* Stop PTP Clock driver */
2174         if (chip_id != RCAR_GEN2)
2175                 ravb_ptp_stop(ndev);
2176 out_release:
2177         if (ndev)
2178                 free_netdev(ndev);
2179
2180         pm_runtime_put(&pdev->dev);
2181         pm_runtime_disable(&pdev->dev);
2182         return error;
2183 }
2184
2185 static int ravb_remove(struct platform_device *pdev)
2186 {
2187         struct net_device *ndev = platform_get_drvdata(pdev);
2188         struct ravb_private *priv = netdev_priv(ndev);
2189
2190         /* Stop PTP Clock driver */
2191         if (priv->chip_id != RCAR_GEN2)
2192                 ravb_ptp_stop(ndev);
2193
2194         dma_free_coherent(ndev->dev.parent, priv->desc_bat_size, priv->desc_bat,
2195                           priv->desc_bat_dma);
2196         /* Set reset mode */
2197         ravb_write(ndev, CCC_OPC_RESET, CCC);
2198         pm_runtime_put_sync(&pdev->dev);
2199         unregister_netdev(ndev);
2200         netif_napi_del(&priv->napi[RAVB_NC]);
2201         netif_napi_del(&priv->napi[RAVB_BE]);
2202         ravb_mdio_release(priv);
2203         pm_runtime_disable(&pdev->dev);
2204         free_netdev(ndev);
2205         platform_set_drvdata(pdev, NULL);
2206
2207         return 0;
2208 }
2209
2210 static int ravb_wol_setup(struct net_device *ndev)
2211 {
2212         struct ravb_private *priv = netdev_priv(ndev);
2213
2214         /* Disable interrupts by clearing the interrupt masks. */
2215         ravb_write(ndev, 0, RIC0);
2216         ravb_write(ndev, 0, RIC2);
2217         ravb_write(ndev, 0, TIC);
2218
2219         /* Only allow ECI interrupts */
2220         synchronize_irq(priv->emac_irq);
2221         napi_disable(&priv->napi[RAVB_NC]);
2222         napi_disable(&priv->napi[RAVB_BE]);
2223         ravb_write(ndev, ECSIPR_MPDIP, ECSIPR);
2224
2225         /* Enable MagicPacket */
2226         ravb_modify(ndev, ECMR, ECMR_MPDE, ECMR_MPDE);
2227
2228         /* Increased clock usage so device won't be suspended */
2229         clk_enable(priv->clk);
2230
2231         return enable_irq_wake(priv->emac_irq);
2232 }
2233
2234 static int ravb_wol_restore(struct net_device *ndev)
2235 {
2236         struct ravb_private *priv = netdev_priv(ndev);
2237         int ret;
2238
2239         napi_enable(&priv->napi[RAVB_NC]);
2240         napi_enable(&priv->napi[RAVB_BE]);
2241
2242         /* Disable MagicPacket */
2243         ravb_modify(ndev, ECMR, ECMR_MPDE, 0);
2244
2245         ret = ravb_close(ndev);
2246         if (ret < 0)
2247                 return ret;
2248
2249         /* Restore clock usage count */
2250         clk_disable(priv->clk);
2251
2252         return disable_irq_wake(priv->emac_irq);
2253 }
2254
2255 static int __maybe_unused ravb_suspend(struct device *dev)
2256 {
2257         struct net_device *ndev = dev_get_drvdata(dev);
2258         struct ravb_private *priv = netdev_priv(ndev);
2259         int ret;
2260
2261         if (!netif_running(ndev))
2262                 return 0;
2263
2264         netif_device_detach(ndev);
2265
2266         if (priv->wol_enabled)
2267                 ret = ravb_wol_setup(ndev);
2268         else
2269                 ret = ravb_close(ndev);
2270
2271         return ret;
2272 }
2273
2274 static int __maybe_unused ravb_resume(struct device *dev)
2275 {
2276         struct net_device *ndev = dev_get_drvdata(dev);
2277         struct ravb_private *priv = netdev_priv(ndev);
2278         int ret = 0;
2279
2280         if (priv->wol_enabled) {
2281                 /* Reduce the usecount of the clock to zero and then
2282                  * restore it to its original value. This is done to force
2283                  * the clock to be re-enabled which is a workaround
2284                  * for renesas-cpg-mssr driver which do not enable clocks
2285                  * when resuming from PSCI suspend/resume.
2286                  *
2287                  * Without this workaround the driver fails to communicate
2288                  * with the hardware if WoL was enabled when the system
2289                  * entered PSCI suspend. This is due to that if WoL is enabled
2290                  * we explicitly keep the clock from being turned off when
2291                  * suspending, but in PSCI sleep power is cut so the clock
2292                  * is disabled anyhow, the clock driver is not aware of this
2293                  * so the clock is not turned back on when resuming.
2294                  *
2295                  * TODO: once the renesas-cpg-mssr suspend/resume is working
2296                  *       this clock dance should be removed.
2297                  */
2298                 clk_disable(priv->clk);
2299                 clk_disable(priv->clk);
2300                 clk_enable(priv->clk);
2301                 clk_enable(priv->clk);
2302
2303                 /* Set reset mode to rearm the WoL logic */
2304                 ravb_write(ndev, CCC_OPC_RESET, CCC);
2305         }
2306
2307         /* All register have been reset to default values.
2308          * Restore all registers which where setup at probe time and
2309          * reopen device if it was running before system suspended.
2310          */
2311
2312         /* Set AVB config mode */
2313         ravb_set_config_mode(ndev);
2314
2315         /* Set GTI value */
2316         ret = ravb_set_gti(ndev);
2317         if (ret)
2318                 return ret;
2319
2320         /* Request GTI loading */
2321         ravb_modify(ndev, GCCR, GCCR_LTI, GCCR_LTI);
2322
2323         if (priv->chip_id != RCAR_GEN2)
2324                 ravb_set_delay_mode(ndev);
2325
2326         /* Restore descriptor base address table */
2327         ravb_write(ndev, priv->desc_bat_dma, DBAT);
2328
2329         if (netif_running(ndev)) {
2330                 if (priv->wol_enabled) {
2331                         ret = ravb_wol_restore(ndev);
2332                         if (ret)
2333                                 return ret;
2334                 }
2335                 ret = ravb_open(ndev);
2336                 if (ret < 0)
2337                         return ret;
2338                 netif_device_attach(ndev);
2339         }
2340
2341         return ret;
2342 }
2343
2344 static int __maybe_unused ravb_runtime_nop(struct device *dev)
2345 {
2346         /* Runtime PM callback shared between ->runtime_suspend()
2347          * and ->runtime_resume(). Simply returns success.
2348          *
2349          * This driver re-initializes all registers after
2350          * pm_runtime_get_sync() anyway so there is no need
2351          * to save and restore registers here.
2352          */
2353         return 0;
2354 }
2355
2356 static const struct dev_pm_ops ravb_dev_pm_ops = {
2357         SET_SYSTEM_SLEEP_PM_OPS(ravb_suspend, ravb_resume)
2358         SET_RUNTIME_PM_OPS(ravb_runtime_nop, ravb_runtime_nop, NULL)
2359 };
2360
2361 static struct platform_driver ravb_driver = {
2362         .probe          = ravb_probe,
2363         .remove         = ravb_remove,
2364         .driver = {
2365                 .name   = "ravb",
2366                 .pm     = &ravb_dev_pm_ops,
2367                 .of_match_table = ravb_match_table,
2368         },
2369 };
2370
2371 module_platform_driver(ravb_driver);
2372
2373 MODULE_AUTHOR("Mitsuhiro Kimura, Masaru Nagai");
2374 MODULE_DESCRIPTION("Renesas Ethernet AVB driver");
2375 MODULE_LICENSE("GPL v2");