net: dpaa: Fix uninitialized variable in dpaa_stop()
[platform/kernel/linux-starfive.git] / drivers / net / ethernet / freescale / dpaa / dpaa_eth.c
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
2 /*
3  * Copyright 2008 - 2016 Freescale Semiconductor Inc.
4  * Copyright 2020 NXP
5  */
6
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/of_mdio.h>
13 #include <linux/of_net.h>
14 #include <linux/io.h>
15 #include <linux/if_arp.h>
16 #include <linux/if_vlan.h>
17 #include <linux/icmp.h>
18 #include <linux/ip.h>
19 #include <linux/ipv6.h>
20 #include <linux/udp.h>
21 #include <linux/tcp.h>
22 #include <linux/net.h>
23 #include <linux/skbuff.h>
24 #include <linux/etherdevice.h>
25 #include <linux/if_ether.h>
26 #include <linux/highmem.h>
27 #include <linux/percpu.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/sort.h>
30 #include <linux/phy_fixed.h>
31 #include <linux/bpf.h>
32 #include <linux/bpf_trace.h>
33 #include <soc/fsl/bman.h>
34 #include <soc/fsl/qman.h>
35 #include "fman.h"
36 #include "fman_port.h"
37 #include "mac.h"
38 #include "dpaa_eth.h"
39
40 /* CREATE_TRACE_POINTS only needs to be defined once. Other dpaa files
41  * using trace events only need to #include <trace/events/sched.h>
42  */
43 #define CREATE_TRACE_POINTS
44 #include "dpaa_eth_trace.h"
45
46 static int debug = -1;
47 module_param(debug, int, 0444);
48 MODULE_PARM_DESC(debug, "Module/Driver verbosity level (0=none,...,16=all)");
49
50 static u16 tx_timeout = 1000;
51 module_param(tx_timeout, ushort, 0444);
52 MODULE_PARM_DESC(tx_timeout, "The Tx timeout in ms");
53
54 #define FM_FD_STAT_RX_ERRORS                                            \
55         (FM_FD_ERR_DMA | FM_FD_ERR_PHYSICAL     | \
56          FM_FD_ERR_SIZE | FM_FD_ERR_CLS_DISCARD | \
57          FM_FD_ERR_EXTRACTION | FM_FD_ERR_NO_SCHEME     | \
58          FM_FD_ERR_PRS_TIMEOUT | FM_FD_ERR_PRS_ILL_INSTRUCT | \
59          FM_FD_ERR_PRS_HDR_ERR)
60
61 #define FM_FD_STAT_TX_ERRORS \
62         (FM_FD_ERR_UNSUPPORTED_FORMAT | \
63          FM_FD_ERR_LENGTH | FM_FD_ERR_DMA)
64
65 #define DPAA_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
66                           NETIF_MSG_LINK | NETIF_MSG_IFUP | \
67                           NETIF_MSG_IFDOWN | NETIF_MSG_HW)
68
69 #define DPAA_INGRESS_CS_THRESHOLD 0x10000000
70 /* Ingress congestion threshold on FMan ports
71  * The size in bytes of the ingress tail-drop threshold on FMan ports.
72  * Traffic piling up above this value will be rejected by QMan and discarded
73  * by FMan.
74  */
75
76 /* Size in bytes of the FQ taildrop threshold */
77 #define DPAA_FQ_TD 0x200000
78
79 #define DPAA_CS_THRESHOLD_1G 0x06000000
80 /* Egress congestion threshold on 1G ports, range 0x1000 .. 0x10000000
81  * The size in bytes of the egress Congestion State notification threshold on
82  * 1G ports. The 1G dTSECs can quite easily be flooded by cores doing Tx in a
83  * tight loop (e.g. by sending UDP datagrams at "while(1) speed"),
84  * and the larger the frame size, the more acute the problem.
85  * So we have to find a balance between these factors:
86  * - avoiding the device staying congested for a prolonged time (risking
87  *   the netdev watchdog to fire - see also the tx_timeout module param);
88  * - affecting performance of protocols such as TCP, which otherwise
89  *   behave well under the congestion notification mechanism;
90  * - preventing the Tx cores from tightly-looping (as if the congestion
91  *   threshold was too low to be effective);
92  * - running out of memory if the CS threshold is set too high.
93  */
94
95 #define DPAA_CS_THRESHOLD_10G 0x10000000
96 /* The size in bytes of the egress Congestion State notification threshold on
97  * 10G ports, range 0x1000 .. 0x10000000
98  */
99
100 /* Largest value that the FQD's OAL field can hold */
101 #define FSL_QMAN_MAX_OAL        127
102
103 /* Default alignment for start of data in an Rx FD */
104 #ifdef CONFIG_DPAA_ERRATUM_A050385
105 /* aligning data start to 64 avoids DMA transaction splits, unless the buffer
106  * is crossing a 4k page boundary
107  */
108 #define DPAA_FD_DATA_ALIGNMENT  (fman_has_errata_a050385() ? 64 : 16)
109 /* aligning to 256 avoids DMA transaction splits caused by 4k page boundary
110  * crossings; also, all SG fragments except the last must have a size multiple
111  * of 256 to avoid DMA transaction splits
112  */
113 #define DPAA_A050385_ALIGN 256
114 #define DPAA_FD_RX_DATA_ALIGNMENT (fman_has_errata_a050385() ? \
115                                    DPAA_A050385_ALIGN : 16)
116 #else
117 #define DPAA_FD_DATA_ALIGNMENT  16
118 #define DPAA_FD_RX_DATA_ALIGNMENT DPAA_FD_DATA_ALIGNMENT
119 #endif
120
121 /* The DPAA requires 256 bytes reserved and mapped for the SGT */
122 #define DPAA_SGT_SIZE 256
123
124 /* Values for the L3R field of the FM Parse Results
125  */
126 /* L3 Type field: First IP Present IPv4 */
127 #define FM_L3_PARSE_RESULT_IPV4 0x8000
128 /* L3 Type field: First IP Present IPv6 */
129 #define FM_L3_PARSE_RESULT_IPV6 0x4000
130 /* Values for the L4R field of the FM Parse Results */
131 /* L4 Type field: UDP */
132 #define FM_L4_PARSE_RESULT_UDP  0x40
133 /* L4 Type field: TCP */
134 #define FM_L4_PARSE_RESULT_TCP  0x20
135
136 /* FD status field indicating whether the FM Parser has attempted to validate
137  * the L4 csum of the frame.
138  * Note that having this bit set doesn't necessarily imply that the checksum
139  * is valid. One would have to check the parse results to find that out.
140  */
141 #define FM_FD_STAT_L4CV         0x00000004
142
143 #define DPAA_SGT_MAX_ENTRIES 16 /* maximum number of entries in SG Table */
144 #define DPAA_BUFF_RELEASE_MAX 8 /* maximum number of buffers released at once */
145
146 #define FSL_DPAA_BPID_INV               0xff
147 #define FSL_DPAA_ETH_MAX_BUF_COUNT      128
148 #define FSL_DPAA_ETH_REFILL_THRESHOLD   80
149
150 #define DPAA_TX_PRIV_DATA_SIZE  16
151 #define DPAA_PARSE_RESULTS_SIZE sizeof(struct fman_prs_result)
152 #define DPAA_TIME_STAMP_SIZE 8
153 #define DPAA_HASH_RESULTS_SIZE 8
154 #define DPAA_HWA_SIZE (DPAA_PARSE_RESULTS_SIZE + DPAA_TIME_STAMP_SIZE \
155                        + DPAA_HASH_RESULTS_SIZE)
156 #define DPAA_RX_PRIV_DATA_DEFAULT_SIZE (DPAA_TX_PRIV_DATA_SIZE + \
157                                         XDP_PACKET_HEADROOM - DPAA_HWA_SIZE)
158 #ifdef CONFIG_DPAA_ERRATUM_A050385
159 #define DPAA_RX_PRIV_DATA_A050385_SIZE (DPAA_A050385_ALIGN - DPAA_HWA_SIZE)
160 #define DPAA_RX_PRIV_DATA_SIZE (fman_has_errata_a050385() ? \
161                                 DPAA_RX_PRIV_DATA_A050385_SIZE : \
162                                 DPAA_RX_PRIV_DATA_DEFAULT_SIZE)
163 #else
164 #define DPAA_RX_PRIV_DATA_SIZE DPAA_RX_PRIV_DATA_DEFAULT_SIZE
165 #endif
166
167 #define DPAA_ETH_PCD_RXQ_NUM    128
168
169 #define DPAA_ENQUEUE_RETRIES    100000
170
171 enum port_type {RX, TX};
172
173 struct fm_port_fqs {
174         struct dpaa_fq *tx_defq;
175         struct dpaa_fq *tx_errq;
176         struct dpaa_fq *rx_defq;
177         struct dpaa_fq *rx_errq;
178         struct dpaa_fq *rx_pcdq;
179 };
180
181 /* All the dpa bps in use at any moment */
182 static struct dpaa_bp *dpaa_bp_array[BM_MAX_NUM_OF_POOLS];
183
184 #define DPAA_BP_RAW_SIZE 4096
185
186 #ifdef CONFIG_DPAA_ERRATUM_A050385
187 #define dpaa_bp_size(raw_size) (SKB_WITH_OVERHEAD(raw_size) & \
188                                 ~(DPAA_A050385_ALIGN - 1))
189 #else
190 #define dpaa_bp_size(raw_size) SKB_WITH_OVERHEAD(raw_size)
191 #endif
192
193 static int dpaa_max_frm;
194
195 static int dpaa_rx_extra_headroom;
196
197 #define dpaa_get_max_mtu()      \
198         (dpaa_max_frm - (VLAN_ETH_HLEN + ETH_FCS_LEN))
199
200 static void dpaa_eth_cgr_set_speed(struct mac_device *mac_dev, int speed);
201
202 static int dpaa_netdev_init(struct net_device *net_dev,
203                             const struct net_device_ops *dpaa_ops,
204                             u16 tx_timeout)
205 {
206         struct dpaa_priv *priv = netdev_priv(net_dev);
207         struct device *dev = net_dev->dev.parent;
208         struct mac_device *mac_dev = priv->mac_dev;
209         struct dpaa_percpu_priv *percpu_priv;
210         const u8 *mac_addr;
211         int i, err;
212
213         /* Although we access another CPU's private data here
214          * we do it at initialization so it is safe
215          */
216         for_each_possible_cpu(i) {
217                 percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
218                 percpu_priv->net_dev = net_dev;
219         }
220
221         net_dev->netdev_ops = dpaa_ops;
222         mac_addr = mac_dev->addr;
223
224         net_dev->mem_start = (unsigned long)priv->mac_dev->res->start;
225         net_dev->mem_end = (unsigned long)priv->mac_dev->res->end;
226
227         net_dev->min_mtu = ETH_MIN_MTU;
228         net_dev->max_mtu = dpaa_get_max_mtu();
229
230         net_dev->hw_features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
231                                  NETIF_F_LLTX | NETIF_F_RXHASH);
232
233         net_dev->hw_features |= NETIF_F_SG | NETIF_F_HIGHDMA;
234         /* The kernels enables GSO automatically, if we declare NETIF_F_SG.
235          * For conformity, we'll still declare GSO explicitly.
236          */
237         net_dev->features |= NETIF_F_GSO;
238         net_dev->features |= NETIF_F_RXCSUM;
239
240         net_dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
241         /* we do not want shared skbs on TX */
242         net_dev->priv_flags &= ~IFF_TX_SKB_SHARING;
243
244         net_dev->features |= net_dev->hw_features;
245         net_dev->vlan_features = net_dev->features;
246
247         if (is_valid_ether_addr(mac_addr)) {
248                 memcpy(net_dev->perm_addr, mac_addr, net_dev->addr_len);
249                 eth_hw_addr_set(net_dev, mac_addr);
250         } else {
251                 eth_hw_addr_random(net_dev);
252                 err = mac_dev->change_addr(mac_dev->fman_mac,
253                         (const enet_addr_t *)net_dev->dev_addr);
254                 if (err) {
255                         dev_err(dev, "Failed to set random MAC address\n");
256                         return -EINVAL;
257                 }
258                 dev_info(dev, "Using random MAC address: %pM\n",
259                          net_dev->dev_addr);
260         }
261
262         net_dev->ethtool_ops = &dpaa_ethtool_ops;
263
264         net_dev->needed_headroom = priv->tx_headroom;
265         net_dev->watchdog_timeo = msecs_to_jiffies(tx_timeout);
266
267         mac_dev->net_dev = net_dev;
268         mac_dev->update_speed = dpaa_eth_cgr_set_speed;
269
270         /* start without the RUNNING flag, phylib controls it later */
271         netif_carrier_off(net_dev);
272
273         err = register_netdev(net_dev);
274         if (err < 0) {
275                 dev_err(dev, "register_netdev() = %d\n", err);
276                 return err;
277         }
278
279         return 0;
280 }
281
282 static int dpaa_stop(struct net_device *net_dev)
283 {
284         struct mac_device *mac_dev;
285         struct dpaa_priv *priv;
286         int i, error;
287         int err = 0;
288
289         priv = netdev_priv(net_dev);
290         mac_dev = priv->mac_dev;
291
292         netif_tx_stop_all_queues(net_dev);
293         /* Allow the Fman (Tx) port to process in-flight frames before we
294          * try switching it off.
295          */
296         msleep(200);
297
298         if (mac_dev->phy_dev)
299                 phy_stop(mac_dev->phy_dev);
300         mac_dev->disable(mac_dev->fman_mac);
301
302         for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
303                 error = fman_port_disable(mac_dev->port[i]);
304                 if (error)
305                         err = error;
306         }
307
308         if (net_dev->phydev)
309                 phy_disconnect(net_dev->phydev);
310         net_dev->phydev = NULL;
311
312         msleep(200);
313
314         return err;
315 }
316
317 static void dpaa_tx_timeout(struct net_device *net_dev, unsigned int txqueue)
318 {
319         struct dpaa_percpu_priv *percpu_priv;
320         const struct dpaa_priv  *priv;
321
322         priv = netdev_priv(net_dev);
323         percpu_priv = this_cpu_ptr(priv->percpu_priv);
324
325         netif_crit(priv, timer, net_dev, "Transmit timeout latency: %u ms\n",
326                    jiffies_to_msecs(jiffies - dev_trans_start(net_dev)));
327
328         percpu_priv->stats.tx_errors++;
329 }
330
331 /* Calculates the statistics for the given device by adding the statistics
332  * collected by each CPU.
333  */
334 static void dpaa_get_stats64(struct net_device *net_dev,
335                              struct rtnl_link_stats64 *s)
336 {
337         int numstats = sizeof(struct rtnl_link_stats64) / sizeof(u64);
338         struct dpaa_priv *priv = netdev_priv(net_dev);
339         struct dpaa_percpu_priv *percpu_priv;
340         u64 *netstats = (u64 *)s;
341         u64 *cpustats;
342         int i, j;
343
344         for_each_possible_cpu(i) {
345                 percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
346
347                 cpustats = (u64 *)&percpu_priv->stats;
348
349                 /* add stats from all CPUs */
350                 for (j = 0; j < numstats; j++)
351                         netstats[j] += cpustats[j];
352         }
353 }
354
355 static int dpaa_setup_tc(struct net_device *net_dev, enum tc_setup_type type,
356                          void *type_data)
357 {
358         struct dpaa_priv *priv = netdev_priv(net_dev);
359         struct tc_mqprio_qopt *mqprio = type_data;
360         u8 num_tc;
361         int i;
362
363         if (type != TC_SETUP_QDISC_MQPRIO)
364                 return -EOPNOTSUPP;
365
366         mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
367         num_tc = mqprio->num_tc;
368
369         if (num_tc == priv->num_tc)
370                 return 0;
371
372         if (!num_tc) {
373                 netdev_reset_tc(net_dev);
374                 goto out;
375         }
376
377         if (num_tc > DPAA_TC_NUM) {
378                 netdev_err(net_dev, "Too many traffic classes: max %d supported.\n",
379                            DPAA_TC_NUM);
380                 return -EINVAL;
381         }
382
383         netdev_set_num_tc(net_dev, num_tc);
384
385         for (i = 0; i < num_tc; i++)
386                 netdev_set_tc_queue(net_dev, i, DPAA_TC_TXQ_NUM,
387                                     i * DPAA_TC_TXQ_NUM);
388
389 out:
390         priv->num_tc = num_tc ? : 1;
391         netif_set_real_num_tx_queues(net_dev, priv->num_tc * DPAA_TC_TXQ_NUM);
392         return 0;
393 }
394
395 static struct mac_device *dpaa_mac_dev_get(struct platform_device *pdev)
396 {
397         struct dpaa_eth_data *eth_data;
398         struct device *dpaa_dev;
399         struct mac_device *mac_dev;
400
401         dpaa_dev = &pdev->dev;
402         eth_data = dpaa_dev->platform_data;
403         if (!eth_data) {
404                 dev_err(dpaa_dev, "eth_data missing\n");
405                 return ERR_PTR(-ENODEV);
406         }
407         mac_dev = eth_data->mac_dev;
408         if (!mac_dev) {
409                 dev_err(dpaa_dev, "mac_dev missing\n");
410                 return ERR_PTR(-EINVAL);
411         }
412
413         return mac_dev;
414 }
415
416 static int dpaa_set_mac_address(struct net_device *net_dev, void *addr)
417 {
418         const struct dpaa_priv *priv;
419         struct mac_device *mac_dev;
420         struct sockaddr old_addr;
421         int err;
422
423         priv = netdev_priv(net_dev);
424
425         memcpy(old_addr.sa_data, net_dev->dev_addr,  ETH_ALEN);
426
427         err = eth_mac_addr(net_dev, addr);
428         if (err < 0) {
429                 netif_err(priv, drv, net_dev, "eth_mac_addr() = %d\n", err);
430                 return err;
431         }
432
433         mac_dev = priv->mac_dev;
434
435         err = mac_dev->change_addr(mac_dev->fman_mac,
436                                    (const enet_addr_t *)net_dev->dev_addr);
437         if (err < 0) {
438                 netif_err(priv, drv, net_dev, "mac_dev->change_addr() = %d\n",
439                           err);
440                 /* reverting to previous address */
441                 eth_mac_addr(net_dev, &old_addr);
442
443                 return err;
444         }
445
446         return 0;
447 }
448
449 static void dpaa_set_rx_mode(struct net_device *net_dev)
450 {
451         const struct dpaa_priv  *priv;
452         int err;
453
454         priv = netdev_priv(net_dev);
455
456         if (!!(net_dev->flags & IFF_PROMISC) != priv->mac_dev->promisc) {
457                 priv->mac_dev->promisc = !priv->mac_dev->promisc;
458                 err = priv->mac_dev->set_promisc(priv->mac_dev->fman_mac,
459                                                  priv->mac_dev->promisc);
460                 if (err < 0)
461                         netif_err(priv, drv, net_dev,
462                                   "mac_dev->set_promisc() = %d\n",
463                                   err);
464         }
465
466         if (!!(net_dev->flags & IFF_ALLMULTI) != priv->mac_dev->allmulti) {
467                 priv->mac_dev->allmulti = !priv->mac_dev->allmulti;
468                 err = priv->mac_dev->set_allmulti(priv->mac_dev->fman_mac,
469                                                   priv->mac_dev->allmulti);
470                 if (err < 0)
471                         netif_err(priv, drv, net_dev,
472                                   "mac_dev->set_allmulti() = %d\n",
473                                   err);
474         }
475
476         err = priv->mac_dev->set_multi(net_dev, priv->mac_dev);
477         if (err < 0)
478                 netif_err(priv, drv, net_dev, "mac_dev->set_multi() = %d\n",
479                           err);
480 }
481
482 static struct dpaa_bp *dpaa_bpid2pool(int bpid)
483 {
484         if (WARN_ON(bpid < 0 || bpid >= BM_MAX_NUM_OF_POOLS))
485                 return NULL;
486
487         return dpaa_bp_array[bpid];
488 }
489
490 /* checks if this bpool is already allocated */
491 static bool dpaa_bpid2pool_use(int bpid)
492 {
493         if (dpaa_bpid2pool(bpid)) {
494                 refcount_inc(&dpaa_bp_array[bpid]->refs);
495                 return true;
496         }
497
498         return false;
499 }
500
501 /* called only once per bpid by dpaa_bp_alloc_pool() */
502 static void dpaa_bpid2pool_map(int bpid, struct dpaa_bp *dpaa_bp)
503 {
504         dpaa_bp_array[bpid] = dpaa_bp;
505         refcount_set(&dpaa_bp->refs, 1);
506 }
507
508 static int dpaa_bp_alloc_pool(struct dpaa_bp *dpaa_bp)
509 {
510         int err;
511
512         if (dpaa_bp->size == 0 || dpaa_bp->config_count == 0) {
513                 pr_err("%s: Buffer pool is not properly initialized! Missing size or initial number of buffers\n",
514                        __func__);
515                 return -EINVAL;
516         }
517
518         /* If the pool is already specified, we only create one per bpid */
519         if (dpaa_bp->bpid != FSL_DPAA_BPID_INV &&
520             dpaa_bpid2pool_use(dpaa_bp->bpid))
521                 return 0;
522
523         if (dpaa_bp->bpid == FSL_DPAA_BPID_INV) {
524                 dpaa_bp->pool = bman_new_pool();
525                 if (!dpaa_bp->pool) {
526                         pr_err("%s: bman_new_pool() failed\n",
527                                __func__);
528                         return -ENODEV;
529                 }
530
531                 dpaa_bp->bpid = (u8)bman_get_bpid(dpaa_bp->pool);
532         }
533
534         if (dpaa_bp->seed_cb) {
535                 err = dpaa_bp->seed_cb(dpaa_bp);
536                 if (err)
537                         goto pool_seed_failed;
538         }
539
540         dpaa_bpid2pool_map(dpaa_bp->bpid, dpaa_bp);
541
542         return 0;
543
544 pool_seed_failed:
545         pr_err("%s: pool seeding failed\n", __func__);
546         bman_free_pool(dpaa_bp->pool);
547
548         return err;
549 }
550
551 /* remove and free all the buffers from the given buffer pool */
552 static void dpaa_bp_drain(struct dpaa_bp *bp)
553 {
554         u8 num = 8;
555         int ret;
556
557         do {
558                 struct bm_buffer bmb[8];
559                 int i;
560
561                 ret = bman_acquire(bp->pool, bmb, num);
562                 if (ret < 0) {
563                         if (num == 8) {
564                                 /* we have less than 8 buffers left;
565                                  * drain them one by one
566                                  */
567                                 num = 1;
568                                 ret = 1;
569                                 continue;
570                         } else {
571                                 /* Pool is fully drained */
572                                 break;
573                         }
574                 }
575
576                 if (bp->free_buf_cb)
577                         for (i = 0; i < num; i++)
578                                 bp->free_buf_cb(bp, &bmb[i]);
579         } while (ret > 0);
580 }
581
582 static void dpaa_bp_free(struct dpaa_bp *dpaa_bp)
583 {
584         struct dpaa_bp *bp = dpaa_bpid2pool(dpaa_bp->bpid);
585
586         /* the mapping between bpid and dpaa_bp is done very late in the
587          * allocation procedure; if something failed before the mapping, the bp
588          * was not configured, therefore we don't need the below instructions
589          */
590         if (!bp)
591                 return;
592
593         if (!refcount_dec_and_test(&bp->refs))
594                 return;
595
596         if (bp->free_buf_cb)
597                 dpaa_bp_drain(bp);
598
599         dpaa_bp_array[bp->bpid] = NULL;
600         bman_free_pool(bp->pool);
601 }
602
603 static void dpaa_bps_free(struct dpaa_priv *priv)
604 {
605         dpaa_bp_free(priv->dpaa_bp);
606 }
607
608 /* Use multiple WQs for FQ assignment:
609  *      - Tx Confirmation queues go to WQ1.
610  *      - Rx Error and Tx Error queues go to WQ5 (giving them a better chance
611  *        to be scheduled, in case there are many more FQs in WQ6).
612  *      - Rx Default goes to WQ6.
613  *      - Tx queues go to different WQs depending on their priority. Equal
614  *        chunks of NR_CPUS queues go to WQ6 (lowest priority), WQ2, WQ1 and
615  *        WQ0 (highest priority).
616  * This ensures that Tx-confirmed buffers are timely released. In particular,
617  * it avoids congestion on the Tx Confirm FQs, which can pile up PFDRs if they
618  * are greatly outnumbered by other FQs in the system, while
619  * dequeue scheduling is round-robin.
620  */
621 static inline void dpaa_assign_wq(struct dpaa_fq *fq, int idx)
622 {
623         switch (fq->fq_type) {
624         case FQ_TYPE_TX_CONFIRM:
625         case FQ_TYPE_TX_CONF_MQ:
626                 fq->wq = 1;
627                 break;
628         case FQ_TYPE_RX_ERROR:
629         case FQ_TYPE_TX_ERROR:
630                 fq->wq = 5;
631                 break;
632         case FQ_TYPE_RX_DEFAULT:
633         case FQ_TYPE_RX_PCD:
634                 fq->wq = 6;
635                 break;
636         case FQ_TYPE_TX:
637                 switch (idx / DPAA_TC_TXQ_NUM) {
638                 case 0:
639                         /* Low priority (best effort) */
640                         fq->wq = 6;
641                         break;
642                 case 1:
643                         /* Medium priority */
644                         fq->wq = 2;
645                         break;
646                 case 2:
647                         /* High priority */
648                         fq->wq = 1;
649                         break;
650                 case 3:
651                         /* Very high priority */
652                         fq->wq = 0;
653                         break;
654                 default:
655                         WARN(1, "Too many TX FQs: more than %d!\n",
656                              DPAA_ETH_TXQ_NUM);
657                 }
658                 break;
659         default:
660                 WARN(1, "Invalid FQ type %d for FQID %d!\n",
661                      fq->fq_type, fq->fqid);
662         }
663 }
664
665 static struct dpaa_fq *dpaa_fq_alloc(struct device *dev,
666                                      u32 start, u32 count,
667                                      struct list_head *list,
668                                      enum dpaa_fq_type fq_type)
669 {
670         struct dpaa_fq *dpaa_fq;
671         int i;
672
673         dpaa_fq = devm_kcalloc(dev, count, sizeof(*dpaa_fq),
674                                GFP_KERNEL);
675         if (!dpaa_fq)
676                 return NULL;
677
678         for (i = 0; i < count; i++) {
679                 dpaa_fq[i].fq_type = fq_type;
680                 dpaa_fq[i].fqid = start ? start + i : 0;
681                 list_add_tail(&dpaa_fq[i].list, list);
682         }
683
684         for (i = 0; i < count; i++)
685                 dpaa_assign_wq(dpaa_fq + i, i);
686
687         return dpaa_fq;
688 }
689
690 static int dpaa_alloc_all_fqs(struct device *dev, struct list_head *list,
691                               struct fm_port_fqs *port_fqs)
692 {
693         struct dpaa_fq *dpaa_fq;
694         u32 fq_base, fq_base_aligned, i;
695
696         dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_ERROR);
697         if (!dpaa_fq)
698                 goto fq_alloc_failed;
699
700         port_fqs->rx_errq = &dpaa_fq[0];
701
702         dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_RX_DEFAULT);
703         if (!dpaa_fq)
704                 goto fq_alloc_failed;
705
706         port_fqs->rx_defq = &dpaa_fq[0];
707
708         /* the PCD FQIDs range needs to be aligned for correct operation */
709         if (qman_alloc_fqid_range(&fq_base, 2 * DPAA_ETH_PCD_RXQ_NUM))
710                 goto fq_alloc_failed;
711
712         fq_base_aligned = ALIGN(fq_base, DPAA_ETH_PCD_RXQ_NUM);
713
714         for (i = fq_base; i < fq_base_aligned; i++)
715                 qman_release_fqid(i);
716
717         for (i = fq_base_aligned + DPAA_ETH_PCD_RXQ_NUM;
718              i < (fq_base + 2 * DPAA_ETH_PCD_RXQ_NUM); i++)
719                 qman_release_fqid(i);
720
721         dpaa_fq = dpaa_fq_alloc(dev, fq_base_aligned, DPAA_ETH_PCD_RXQ_NUM,
722                                 list, FQ_TYPE_RX_PCD);
723         if (!dpaa_fq)
724                 goto fq_alloc_failed;
725
726         port_fqs->rx_pcdq = &dpaa_fq[0];
727
728         if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX_CONF_MQ))
729                 goto fq_alloc_failed;
730
731         dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_ERROR);
732         if (!dpaa_fq)
733                 goto fq_alloc_failed;
734
735         port_fqs->tx_errq = &dpaa_fq[0];
736
737         dpaa_fq = dpaa_fq_alloc(dev, 0, 1, list, FQ_TYPE_TX_CONFIRM);
738         if (!dpaa_fq)
739                 goto fq_alloc_failed;
740
741         port_fqs->tx_defq = &dpaa_fq[0];
742
743         if (!dpaa_fq_alloc(dev, 0, DPAA_ETH_TXQ_NUM, list, FQ_TYPE_TX))
744                 goto fq_alloc_failed;
745
746         return 0;
747
748 fq_alloc_failed:
749         dev_err(dev, "dpaa_fq_alloc() failed\n");
750         return -ENOMEM;
751 }
752
753 static u32 rx_pool_channel;
754 static DEFINE_SPINLOCK(rx_pool_channel_init);
755
756 static int dpaa_get_channel(void)
757 {
758         spin_lock(&rx_pool_channel_init);
759         if (!rx_pool_channel) {
760                 u32 pool;
761                 int ret;
762
763                 ret = qman_alloc_pool(&pool);
764
765                 if (!ret)
766                         rx_pool_channel = pool;
767         }
768         spin_unlock(&rx_pool_channel_init);
769         if (!rx_pool_channel)
770                 return -ENOMEM;
771         return rx_pool_channel;
772 }
773
774 static void dpaa_release_channel(void)
775 {
776         qman_release_pool(rx_pool_channel);
777 }
778
779 static void dpaa_eth_add_channel(u16 channel, struct device *dev)
780 {
781         u32 pool = QM_SDQCR_CHANNELS_POOL_CONV(channel);
782         const cpumask_t *cpus = qman_affine_cpus();
783         struct qman_portal *portal;
784         int cpu;
785
786         for_each_cpu_and(cpu, cpus, cpu_online_mask) {
787                 portal = qman_get_affine_portal(cpu);
788                 qman_p_static_dequeue_add(portal, pool);
789                 qman_start_using_portal(portal, dev);
790         }
791 }
792
793 /* Congestion group state change notification callback.
794  * Stops the device's egress queues while they are congested and
795  * wakes them upon exiting congested state.
796  * Also updates some CGR-related stats.
797  */
798 static void dpaa_eth_cgscn(struct qman_portal *qm, struct qman_cgr *cgr,
799                            int congested)
800 {
801         struct dpaa_priv *priv = (struct dpaa_priv *)container_of(cgr,
802                 struct dpaa_priv, cgr_data.cgr);
803
804         if (congested) {
805                 priv->cgr_data.congestion_start_jiffies = jiffies;
806                 netif_tx_stop_all_queues(priv->net_dev);
807                 priv->cgr_data.cgr_congested_count++;
808         } else {
809                 priv->cgr_data.congested_jiffies +=
810                         (jiffies - priv->cgr_data.congestion_start_jiffies);
811                 netif_tx_wake_all_queues(priv->net_dev);
812         }
813 }
814
815 static int dpaa_eth_cgr_init(struct dpaa_priv *priv)
816 {
817         struct qm_mcc_initcgr initcgr;
818         u32 cs_th;
819         int err;
820
821         err = qman_alloc_cgrid(&priv->cgr_data.cgr.cgrid);
822         if (err < 0) {
823                 if (netif_msg_drv(priv))
824                         pr_err("%s: Error %d allocating CGR ID\n",
825                                __func__, err);
826                 goto out_error;
827         }
828         priv->cgr_data.cgr.cb = dpaa_eth_cgscn;
829
830         /* Enable Congestion State Change Notifications and CS taildrop */
831         memset(&initcgr, 0, sizeof(initcgr));
832         initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CSCN_EN | QM_CGR_WE_CS_THRES);
833         initcgr.cgr.cscn_en = QM_CGR_EN;
834
835         /* Set different thresholds based on the configured MAC speed.
836          * This may turn suboptimal if the MAC is reconfigured at another
837          * speed, so MACs must call dpaa_eth_cgr_set_speed in their adjust_link
838          * callback.
839          */
840         if (priv->mac_dev->if_support & SUPPORTED_10000baseT_Full)
841                 cs_th = DPAA_CS_THRESHOLD_10G;
842         else
843                 cs_th = DPAA_CS_THRESHOLD_1G;
844         qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
845
846         initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
847         initcgr.cgr.cstd_en = QM_CGR_EN;
848
849         err = qman_create_cgr(&priv->cgr_data.cgr, QMAN_CGR_FLAG_USE_INIT,
850                               &initcgr);
851         if (err < 0) {
852                 if (netif_msg_drv(priv))
853                         pr_err("%s: Error %d creating CGR with ID %d\n",
854                                __func__, err, priv->cgr_data.cgr.cgrid);
855                 qman_release_cgrid(priv->cgr_data.cgr.cgrid);
856                 goto out_error;
857         }
858         if (netif_msg_drv(priv))
859                 pr_debug("Created CGR %d for netdev with hwaddr %pM on QMan channel %d\n",
860                          priv->cgr_data.cgr.cgrid, priv->mac_dev->addr,
861                          priv->cgr_data.cgr.chan);
862
863 out_error:
864         return err;
865 }
866
867 static void dpaa_eth_cgr_set_speed(struct mac_device *mac_dev, int speed)
868 {
869         struct net_device *net_dev = mac_dev->net_dev;
870         struct dpaa_priv *priv = netdev_priv(net_dev);
871         struct qm_mcc_initcgr opts = { };
872         u32 cs_th;
873         int err;
874
875         opts.we_mask = cpu_to_be16(QM_CGR_WE_CS_THRES);
876         switch (speed) {
877         case SPEED_10000:
878                 cs_th = DPAA_CS_THRESHOLD_10G;
879                 break;
880         case SPEED_1000:
881         default:
882                 cs_th = DPAA_CS_THRESHOLD_1G;
883                 break;
884         }
885         qm_cgr_cs_thres_set64(&opts.cgr.cs_thres, cs_th, 1);
886
887         err = qman_update_cgr_safe(&priv->cgr_data.cgr, &opts);
888         if (err)
889                 netdev_err(net_dev, "could not update speed: %d\n", err);
890 }
891
892 static inline void dpaa_setup_ingress(const struct dpaa_priv *priv,
893                                       struct dpaa_fq *fq,
894                                       const struct qman_fq *template)
895 {
896         fq->fq_base = *template;
897         fq->net_dev = priv->net_dev;
898
899         fq->flags = QMAN_FQ_FLAG_NO_ENQUEUE;
900         fq->channel = priv->channel;
901 }
902
903 static inline void dpaa_setup_egress(const struct dpaa_priv *priv,
904                                      struct dpaa_fq *fq,
905                                      struct fman_port *port,
906                                      const struct qman_fq *template)
907 {
908         fq->fq_base = *template;
909         fq->net_dev = priv->net_dev;
910
911         if (port) {
912                 fq->flags = QMAN_FQ_FLAG_TO_DCPORTAL;
913                 fq->channel = (u16)fman_port_get_qman_channel_id(port);
914         } else {
915                 fq->flags = QMAN_FQ_FLAG_NO_MODIFY;
916         }
917 }
918
919 static void dpaa_fq_setup(struct dpaa_priv *priv,
920                           const struct dpaa_fq_cbs *fq_cbs,
921                           struct fman_port *tx_port)
922 {
923         int egress_cnt = 0, conf_cnt = 0, num_portals = 0, portal_cnt = 0, cpu;
924         const cpumask_t *affine_cpus = qman_affine_cpus();
925         u16 channels[NR_CPUS];
926         struct dpaa_fq *fq;
927
928         for_each_cpu_and(cpu, affine_cpus, cpu_online_mask)
929                 channels[num_portals++] = qman_affine_channel(cpu);
930
931         if (num_portals == 0)
932                 dev_err(priv->net_dev->dev.parent,
933                         "No Qman software (affine) channels found\n");
934
935         /* Initialize each FQ in the list */
936         list_for_each_entry(fq, &priv->dpaa_fq_list, list) {
937                 switch (fq->fq_type) {
938                 case FQ_TYPE_RX_DEFAULT:
939                         dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq);
940                         break;
941                 case FQ_TYPE_RX_ERROR:
942                         dpaa_setup_ingress(priv, fq, &fq_cbs->rx_errq);
943                         break;
944                 case FQ_TYPE_RX_PCD:
945                         if (!num_portals)
946                                 continue;
947                         dpaa_setup_ingress(priv, fq, &fq_cbs->rx_defq);
948                         fq->channel = channels[portal_cnt++ % num_portals];
949                         break;
950                 case FQ_TYPE_TX:
951                         dpaa_setup_egress(priv, fq, tx_port,
952                                           &fq_cbs->egress_ern);
953                         /* If we have more Tx queues than the number of cores,
954                          * just ignore the extra ones.
955                          */
956                         if (egress_cnt < DPAA_ETH_TXQ_NUM)
957                                 priv->egress_fqs[egress_cnt++] = &fq->fq_base;
958                         break;
959                 case FQ_TYPE_TX_CONF_MQ:
960                         priv->conf_fqs[conf_cnt++] = &fq->fq_base;
961                         fallthrough;
962                 case FQ_TYPE_TX_CONFIRM:
963                         dpaa_setup_ingress(priv, fq, &fq_cbs->tx_defq);
964                         break;
965                 case FQ_TYPE_TX_ERROR:
966                         dpaa_setup_ingress(priv, fq, &fq_cbs->tx_errq);
967                         break;
968                 default:
969                         dev_warn(priv->net_dev->dev.parent,
970                                  "Unknown FQ type detected!\n");
971                         break;
972                 }
973         }
974
975          /* Make sure all CPUs receive a corresponding Tx queue. */
976         while (egress_cnt < DPAA_ETH_TXQ_NUM) {
977                 list_for_each_entry(fq, &priv->dpaa_fq_list, list) {
978                         if (fq->fq_type != FQ_TYPE_TX)
979                                 continue;
980                         priv->egress_fqs[egress_cnt++] = &fq->fq_base;
981                         if (egress_cnt == DPAA_ETH_TXQ_NUM)
982                                 break;
983                 }
984         }
985 }
986
987 static inline int dpaa_tx_fq_to_id(const struct dpaa_priv *priv,
988                                    struct qman_fq *tx_fq)
989 {
990         int i;
991
992         for (i = 0; i < DPAA_ETH_TXQ_NUM; i++)
993                 if (priv->egress_fqs[i] == tx_fq)
994                         return i;
995
996         return -EINVAL;
997 }
998
999 static int dpaa_fq_init(struct dpaa_fq *dpaa_fq, bool td_enable)
1000 {
1001         const struct dpaa_priv  *priv;
1002         struct qman_fq *confq = NULL;
1003         struct qm_mcc_initfq initfq;
1004         struct device *dev;
1005         struct qman_fq *fq;
1006         int queue_id;
1007         int err;
1008
1009         priv = netdev_priv(dpaa_fq->net_dev);
1010         dev = dpaa_fq->net_dev->dev.parent;
1011
1012         if (dpaa_fq->fqid == 0)
1013                 dpaa_fq->flags |= QMAN_FQ_FLAG_DYNAMIC_FQID;
1014
1015         dpaa_fq->init = !(dpaa_fq->flags & QMAN_FQ_FLAG_NO_MODIFY);
1016
1017         err = qman_create_fq(dpaa_fq->fqid, dpaa_fq->flags, &dpaa_fq->fq_base);
1018         if (err) {
1019                 dev_err(dev, "qman_create_fq() failed\n");
1020                 return err;
1021         }
1022         fq = &dpaa_fq->fq_base;
1023
1024         if (dpaa_fq->init) {
1025                 memset(&initfq, 0, sizeof(initfq));
1026
1027                 initfq.we_mask = cpu_to_be16(QM_INITFQ_WE_FQCTRL);
1028                 /* Note: we may get to keep an empty FQ in cache */
1029                 initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_PREFERINCACHE);
1030
1031                 /* Try to reduce the number of portal interrupts for
1032                  * Tx Confirmation FQs.
1033                  */
1034                 if (dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM)
1035                         initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_AVOIDBLOCK);
1036
1037                 /* FQ placement */
1038                 initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_DESTWQ);
1039
1040                 qm_fqd_set_destwq(&initfq.fqd, dpaa_fq->channel, dpaa_fq->wq);
1041
1042                 /* Put all egress queues in a congestion group of their own.
1043                  * Sensu stricto, the Tx confirmation queues are Rx FQs,
1044                  * rather than Tx - but they nonetheless account for the
1045                  * memory footprint on behalf of egress traffic. We therefore
1046                  * place them in the netdev's CGR, along with the Tx FQs.
1047                  */
1048                 if (dpaa_fq->fq_type == FQ_TYPE_TX ||
1049                     dpaa_fq->fq_type == FQ_TYPE_TX_CONFIRM ||
1050                     dpaa_fq->fq_type == FQ_TYPE_TX_CONF_MQ) {
1051                         initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID);
1052                         initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE);
1053                         initfq.fqd.cgid = (u8)priv->cgr_data.cgr.cgrid;
1054                         /* Set a fixed overhead accounting, in an attempt to
1055                          * reduce the impact of fixed-size skb shells and the
1056                          * driver's needed headroom on system memory. This is
1057                          * especially the case when the egress traffic is
1058                          * composed of small datagrams.
1059                          * Unfortunately, QMan's OAL value is capped to an
1060                          * insufficient value, but even that is better than
1061                          * no overhead accounting at all.
1062                          */
1063                         initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC);
1064                         qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG);
1065                         qm_fqd_set_oal(&initfq.fqd,
1066                                        min(sizeof(struct sk_buff) +
1067                                        priv->tx_headroom,
1068                                        (size_t)FSL_QMAN_MAX_OAL));
1069                 }
1070
1071                 if (td_enable) {
1072                         initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_TDTHRESH);
1073                         qm_fqd_set_taildrop(&initfq.fqd, DPAA_FQ_TD, 1);
1074                         initfq.fqd.fq_ctrl = cpu_to_be16(QM_FQCTRL_TDE);
1075                 }
1076
1077                 if (dpaa_fq->fq_type == FQ_TYPE_TX) {
1078                         queue_id = dpaa_tx_fq_to_id(priv, &dpaa_fq->fq_base);
1079                         if (queue_id >= 0)
1080                                 confq = priv->conf_fqs[queue_id];
1081                         if (confq) {
1082                                 initfq.we_mask |=
1083                                         cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
1084                         /* ContextA: OVOM=1(use contextA2 bits instead of ICAD)
1085                          *           A2V=1 (contextA A2 field is valid)
1086                          *           A0V=1 (contextA A0 field is valid)
1087                          *           B0V=1 (contextB field is valid)
1088                          * ContextA A2: EBD=1 (deallocate buffers inside FMan)
1089                          * ContextB B0(ASPID): 0 (absolute Virtual Storage ID)
1090                          */
1091                                 qm_fqd_context_a_set64(&initfq.fqd,
1092                                                        0x1e00000080000000ULL);
1093                         }
1094                 }
1095
1096                 /* Put all the ingress queues in our "ingress CGR". */
1097                 if (priv->use_ingress_cgr &&
1098                     (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1099                      dpaa_fq->fq_type == FQ_TYPE_RX_ERROR ||
1100                      dpaa_fq->fq_type == FQ_TYPE_RX_PCD)) {
1101                         initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CGID);
1102                         initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_CGE);
1103                         initfq.fqd.cgid = (u8)priv->ingress_cgr.cgrid;
1104                         /* Set a fixed overhead accounting, just like for the
1105                          * egress CGR.
1106                          */
1107                         initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_OAC);
1108                         qm_fqd_set_oac(&initfq.fqd, QM_OAC_CG);
1109                         qm_fqd_set_oal(&initfq.fqd,
1110                                        min(sizeof(struct sk_buff) +
1111                                        priv->tx_headroom,
1112                                        (size_t)FSL_QMAN_MAX_OAL));
1113                 }
1114
1115                 /* Initialization common to all ingress queues */
1116                 if (dpaa_fq->flags & QMAN_FQ_FLAG_NO_ENQUEUE) {
1117                         initfq.we_mask |= cpu_to_be16(QM_INITFQ_WE_CONTEXTA);
1118                         initfq.fqd.fq_ctrl |= cpu_to_be16(QM_FQCTRL_HOLDACTIVE |
1119                                                 QM_FQCTRL_CTXASTASHING);
1120                         initfq.fqd.context_a.stashing.exclusive =
1121                                 QM_STASHING_EXCL_DATA | QM_STASHING_EXCL_CTX |
1122                                 QM_STASHING_EXCL_ANNOTATION;
1123                         qm_fqd_set_stashing(&initfq.fqd, 1, 2,
1124                                             DIV_ROUND_UP(sizeof(struct qman_fq),
1125                                                          64));
1126                 }
1127
1128                 err = qman_init_fq(fq, QMAN_INITFQ_FLAG_SCHED, &initfq);
1129                 if (err < 0) {
1130                         dev_err(dev, "qman_init_fq(%u) = %d\n",
1131                                 qman_fq_fqid(fq), err);
1132                         qman_destroy_fq(fq);
1133                         return err;
1134                 }
1135         }
1136
1137         dpaa_fq->fqid = qman_fq_fqid(fq);
1138
1139         if (dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1140             dpaa_fq->fq_type == FQ_TYPE_RX_PCD) {
1141                 err = xdp_rxq_info_reg(&dpaa_fq->xdp_rxq, dpaa_fq->net_dev,
1142                                        dpaa_fq->fqid, 0);
1143                 if (err) {
1144                         dev_err(dev, "xdp_rxq_info_reg() = %d\n", err);
1145                         return err;
1146                 }
1147
1148                 err = xdp_rxq_info_reg_mem_model(&dpaa_fq->xdp_rxq,
1149                                                  MEM_TYPE_PAGE_ORDER0, NULL);
1150                 if (err) {
1151                         dev_err(dev, "xdp_rxq_info_reg_mem_model() = %d\n",
1152                                 err);
1153                         xdp_rxq_info_unreg(&dpaa_fq->xdp_rxq);
1154                         return err;
1155                 }
1156         }
1157
1158         return 0;
1159 }
1160
1161 static int dpaa_fq_free_entry(struct device *dev, struct qman_fq *fq)
1162 {
1163         const struct dpaa_priv  *priv;
1164         struct dpaa_fq *dpaa_fq;
1165         int err, error;
1166
1167         err = 0;
1168
1169         dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
1170         priv = netdev_priv(dpaa_fq->net_dev);
1171
1172         if (dpaa_fq->init) {
1173                 err = qman_retire_fq(fq, NULL);
1174                 if (err < 0 && netif_msg_drv(priv))
1175                         dev_err(dev, "qman_retire_fq(%u) = %d\n",
1176                                 qman_fq_fqid(fq), err);
1177
1178                 error = qman_oos_fq(fq);
1179                 if (error < 0 && netif_msg_drv(priv)) {
1180                         dev_err(dev, "qman_oos_fq(%u) = %d\n",
1181                                 qman_fq_fqid(fq), error);
1182                         if (err >= 0)
1183                                 err = error;
1184                 }
1185         }
1186
1187         if ((dpaa_fq->fq_type == FQ_TYPE_RX_DEFAULT ||
1188              dpaa_fq->fq_type == FQ_TYPE_RX_PCD) &&
1189             xdp_rxq_info_is_reg(&dpaa_fq->xdp_rxq))
1190                 xdp_rxq_info_unreg(&dpaa_fq->xdp_rxq);
1191
1192         qman_destroy_fq(fq);
1193         list_del(&dpaa_fq->list);
1194
1195         return err;
1196 }
1197
1198 static int dpaa_fq_free(struct device *dev, struct list_head *list)
1199 {
1200         struct dpaa_fq *dpaa_fq, *tmp;
1201         int err, error;
1202
1203         err = 0;
1204         list_for_each_entry_safe(dpaa_fq, tmp, list, list) {
1205                 error = dpaa_fq_free_entry(dev, (struct qman_fq *)dpaa_fq);
1206                 if (error < 0 && err >= 0)
1207                         err = error;
1208         }
1209
1210         return err;
1211 }
1212
1213 static int dpaa_eth_init_tx_port(struct fman_port *port, struct dpaa_fq *errq,
1214                                  struct dpaa_fq *defq,
1215                                  struct dpaa_buffer_layout *buf_layout)
1216 {
1217         struct fman_buffer_prefix_content buf_prefix_content;
1218         struct fman_port_params params;
1219         int err;
1220
1221         memset(&params, 0, sizeof(params));
1222         memset(&buf_prefix_content, 0, sizeof(buf_prefix_content));
1223
1224         buf_prefix_content.priv_data_size = buf_layout->priv_data_size;
1225         buf_prefix_content.pass_prs_result = true;
1226         buf_prefix_content.pass_hash_result = true;
1227         buf_prefix_content.pass_time_stamp = true;
1228         buf_prefix_content.data_align = DPAA_FD_DATA_ALIGNMENT;
1229
1230         params.specific_params.non_rx_params.err_fqid = errq->fqid;
1231         params.specific_params.non_rx_params.dflt_fqid = defq->fqid;
1232
1233         err = fman_port_config(port, &params);
1234         if (err) {
1235                 pr_err("%s: fman_port_config failed\n", __func__);
1236                 return err;
1237         }
1238
1239         err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content);
1240         if (err) {
1241                 pr_err("%s: fman_port_cfg_buf_prefix_content failed\n",
1242                        __func__);
1243                 return err;
1244         }
1245
1246         err = fman_port_init(port);
1247         if (err)
1248                 pr_err("%s: fm_port_init failed\n", __func__);
1249
1250         return err;
1251 }
1252
1253 static int dpaa_eth_init_rx_port(struct fman_port *port, struct dpaa_bp *bp,
1254                                  struct dpaa_fq *errq,
1255                                  struct dpaa_fq *defq, struct dpaa_fq *pcdq,
1256                                  struct dpaa_buffer_layout *buf_layout)
1257 {
1258         struct fman_buffer_prefix_content buf_prefix_content;
1259         struct fman_port_rx_params *rx_p;
1260         struct fman_port_params params;
1261         int err;
1262
1263         memset(&params, 0, sizeof(params));
1264         memset(&buf_prefix_content, 0, sizeof(buf_prefix_content));
1265
1266         buf_prefix_content.priv_data_size = buf_layout->priv_data_size;
1267         buf_prefix_content.pass_prs_result = true;
1268         buf_prefix_content.pass_hash_result = true;
1269         buf_prefix_content.pass_time_stamp = true;
1270         buf_prefix_content.data_align = DPAA_FD_RX_DATA_ALIGNMENT;
1271
1272         rx_p = &params.specific_params.rx_params;
1273         rx_p->err_fqid = errq->fqid;
1274         rx_p->dflt_fqid = defq->fqid;
1275         if (pcdq) {
1276                 rx_p->pcd_base_fqid = pcdq->fqid;
1277                 rx_p->pcd_fqs_count = DPAA_ETH_PCD_RXQ_NUM;
1278         }
1279
1280         rx_p->ext_buf_pools.num_of_pools_used = 1;
1281         rx_p->ext_buf_pools.ext_buf_pool[0].id =  bp->bpid;
1282         rx_p->ext_buf_pools.ext_buf_pool[0].size = (u16)bp->size;
1283
1284         err = fman_port_config(port, &params);
1285         if (err) {
1286                 pr_err("%s: fman_port_config failed\n", __func__);
1287                 return err;
1288         }
1289
1290         err = fman_port_cfg_buf_prefix_content(port, &buf_prefix_content);
1291         if (err) {
1292                 pr_err("%s: fman_port_cfg_buf_prefix_content failed\n",
1293                        __func__);
1294                 return err;
1295         }
1296
1297         err = fman_port_init(port);
1298         if (err)
1299                 pr_err("%s: fm_port_init failed\n", __func__);
1300
1301         return err;
1302 }
1303
1304 static int dpaa_eth_init_ports(struct mac_device *mac_dev,
1305                                struct dpaa_bp *bp,
1306                                struct fm_port_fqs *port_fqs,
1307                                struct dpaa_buffer_layout *buf_layout,
1308                                struct device *dev)
1309 {
1310         struct fman_port *rxport = mac_dev->port[RX];
1311         struct fman_port *txport = mac_dev->port[TX];
1312         int err;
1313
1314         err = dpaa_eth_init_tx_port(txport, port_fqs->tx_errq,
1315                                     port_fqs->tx_defq, &buf_layout[TX]);
1316         if (err)
1317                 return err;
1318
1319         err = dpaa_eth_init_rx_port(rxport, bp, port_fqs->rx_errq,
1320                                     port_fqs->rx_defq, port_fqs->rx_pcdq,
1321                                     &buf_layout[RX]);
1322
1323         return err;
1324 }
1325
1326 static int dpaa_bman_release(const struct dpaa_bp *dpaa_bp,
1327                              struct bm_buffer *bmb, int cnt)
1328 {
1329         int err;
1330
1331         err = bman_release(dpaa_bp->pool, bmb, cnt);
1332         /* Should never occur, address anyway to avoid leaking the buffers */
1333         if (WARN_ON(err) && dpaa_bp->free_buf_cb)
1334                 while (cnt-- > 0)
1335                         dpaa_bp->free_buf_cb(dpaa_bp, &bmb[cnt]);
1336
1337         return cnt;
1338 }
1339
1340 static void dpaa_release_sgt_members(struct qm_sg_entry *sgt)
1341 {
1342         struct bm_buffer bmb[DPAA_BUFF_RELEASE_MAX];
1343         struct dpaa_bp *dpaa_bp;
1344         int i = 0, j;
1345
1346         memset(bmb, 0, sizeof(bmb));
1347
1348         do {
1349                 dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
1350                 if (!dpaa_bp)
1351                         return;
1352
1353                 j = 0;
1354                 do {
1355                         WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1356
1357                         bm_buffer_set64(&bmb[j], qm_sg_entry_get64(&sgt[i]));
1358
1359                         j++; i++;
1360                 } while (j < ARRAY_SIZE(bmb) &&
1361                                 !qm_sg_entry_is_final(&sgt[i - 1]) &&
1362                                 sgt[i - 1].bpid == sgt[i].bpid);
1363
1364                 dpaa_bman_release(dpaa_bp, bmb, j);
1365         } while (!qm_sg_entry_is_final(&sgt[i - 1]));
1366 }
1367
1368 static void dpaa_fd_release(const struct net_device *net_dev,
1369                             const struct qm_fd *fd)
1370 {
1371         struct qm_sg_entry *sgt;
1372         struct dpaa_bp *dpaa_bp;
1373         struct bm_buffer bmb;
1374         dma_addr_t addr;
1375         void *vaddr;
1376
1377         bmb.data = 0;
1378         bm_buffer_set64(&bmb, qm_fd_addr(fd));
1379
1380         dpaa_bp = dpaa_bpid2pool(fd->bpid);
1381         if (!dpaa_bp)
1382                 return;
1383
1384         if (qm_fd_get_format(fd) == qm_fd_sg) {
1385                 vaddr = phys_to_virt(qm_fd_addr(fd));
1386                 sgt = vaddr + qm_fd_get_offset(fd);
1387
1388                 dma_unmap_page(dpaa_bp->priv->rx_dma_dev, qm_fd_addr(fd),
1389                                DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1390
1391                 dpaa_release_sgt_members(sgt);
1392
1393                 addr = dma_map_page(dpaa_bp->priv->rx_dma_dev,
1394                                     virt_to_page(vaddr), 0, DPAA_BP_RAW_SIZE,
1395                                     DMA_FROM_DEVICE);
1396                 if (dma_mapping_error(dpaa_bp->priv->rx_dma_dev, addr)) {
1397                         netdev_err(net_dev, "DMA mapping failed\n");
1398                         return;
1399                 }
1400                 bm_buffer_set64(&bmb, addr);
1401         }
1402
1403         dpaa_bman_release(dpaa_bp, &bmb, 1);
1404 }
1405
1406 static void count_ern(struct dpaa_percpu_priv *percpu_priv,
1407                       const union qm_mr_entry *msg)
1408 {
1409         switch (msg->ern.rc & QM_MR_RC_MASK) {
1410         case QM_MR_RC_CGR_TAILDROP:
1411                 percpu_priv->ern_cnt.cg_tdrop++;
1412                 break;
1413         case QM_MR_RC_WRED:
1414                 percpu_priv->ern_cnt.wred++;
1415                 break;
1416         case QM_MR_RC_ERROR:
1417                 percpu_priv->ern_cnt.err_cond++;
1418                 break;
1419         case QM_MR_RC_ORPWINDOW_EARLY:
1420                 percpu_priv->ern_cnt.early_window++;
1421                 break;
1422         case QM_MR_RC_ORPWINDOW_LATE:
1423                 percpu_priv->ern_cnt.late_window++;
1424                 break;
1425         case QM_MR_RC_FQ_TAILDROP:
1426                 percpu_priv->ern_cnt.fq_tdrop++;
1427                 break;
1428         case QM_MR_RC_ORPWINDOW_RETIRED:
1429                 percpu_priv->ern_cnt.fq_retired++;
1430                 break;
1431         case QM_MR_RC_ORP_ZERO:
1432                 percpu_priv->ern_cnt.orp_zero++;
1433                 break;
1434         }
1435 }
1436
1437 /* Turn on HW checksum computation for this outgoing frame.
1438  * If the current protocol is not something we support in this regard
1439  * (or if the stack has already computed the SW checksum), we do nothing.
1440  *
1441  * Returns 0 if all goes well (or HW csum doesn't apply), and a negative value
1442  * otherwise.
1443  *
1444  * Note that this function may modify the fd->cmd field and the skb data buffer
1445  * (the Parse Results area).
1446  */
1447 static int dpaa_enable_tx_csum(struct dpaa_priv *priv,
1448                                struct sk_buff *skb,
1449                                struct qm_fd *fd,
1450                                void *parse_results)
1451 {
1452         struct fman_prs_result *parse_result;
1453         u16 ethertype = ntohs(skb->protocol);
1454         struct ipv6hdr *ipv6h = NULL;
1455         struct iphdr *iph;
1456         int retval = 0;
1457         u8 l4_proto;
1458
1459         if (skb->ip_summed != CHECKSUM_PARTIAL)
1460                 return 0;
1461
1462         /* Note: L3 csum seems to be already computed in sw, but we can't choose
1463          * L4 alone from the FM configuration anyway.
1464          */
1465
1466         /* Fill in some fields of the Parse Results array, so the FMan
1467          * can find them as if they came from the FMan Parser.
1468          */
1469         parse_result = (struct fman_prs_result *)parse_results;
1470
1471         /* If we're dealing with VLAN, get the real Ethernet type */
1472         if (ethertype == ETH_P_8021Q) {
1473                 /* We can't always assume the MAC header is set correctly
1474                  * by the stack, so reset to beginning of skb->data
1475                  */
1476                 skb_reset_mac_header(skb);
1477                 ethertype = ntohs(vlan_eth_hdr(skb)->h_vlan_encapsulated_proto);
1478         }
1479
1480         /* Fill in the relevant L3 parse result fields
1481          * and read the L4 protocol type
1482          */
1483         switch (ethertype) {
1484         case ETH_P_IP:
1485                 parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV4);
1486                 iph = ip_hdr(skb);
1487                 WARN_ON(!iph);
1488                 l4_proto = iph->protocol;
1489                 break;
1490         case ETH_P_IPV6:
1491                 parse_result->l3r = cpu_to_be16(FM_L3_PARSE_RESULT_IPV6);
1492                 ipv6h = ipv6_hdr(skb);
1493                 WARN_ON(!ipv6h);
1494                 l4_proto = ipv6h->nexthdr;
1495                 break;
1496         default:
1497                 /* We shouldn't even be here */
1498                 if (net_ratelimit())
1499                         netif_alert(priv, tx_err, priv->net_dev,
1500                                     "Can't compute HW csum for L3 proto 0x%x\n",
1501                                     ntohs(skb->protocol));
1502                 retval = -EIO;
1503                 goto return_error;
1504         }
1505
1506         /* Fill in the relevant L4 parse result fields */
1507         switch (l4_proto) {
1508         case IPPROTO_UDP:
1509                 parse_result->l4r = FM_L4_PARSE_RESULT_UDP;
1510                 break;
1511         case IPPROTO_TCP:
1512                 parse_result->l4r = FM_L4_PARSE_RESULT_TCP;
1513                 break;
1514         default:
1515                 if (net_ratelimit())
1516                         netif_alert(priv, tx_err, priv->net_dev,
1517                                     "Can't compute HW csum for L4 proto 0x%x\n",
1518                                     l4_proto);
1519                 retval = -EIO;
1520                 goto return_error;
1521         }
1522
1523         /* At index 0 is IPOffset_1 as defined in the Parse Results */
1524         parse_result->ip_off[0] = (u8)skb_network_offset(skb);
1525         parse_result->l4_off = (u8)skb_transport_offset(skb);
1526
1527         /* Enable L3 (and L4, if TCP or UDP) HW checksum. */
1528         fd->cmd |= cpu_to_be32(FM_FD_CMD_RPD | FM_FD_CMD_DTC);
1529
1530         /* On P1023 and similar platforms fd->cmd interpretation could
1531          * be disabled by setting CONTEXT_A bit ICMD; currently this bit
1532          * is not set so we do not need to check; in the future, if/when
1533          * using context_a we need to check this bit
1534          */
1535
1536 return_error:
1537         return retval;
1538 }
1539
1540 static int dpaa_bp_add_8_bufs(const struct dpaa_bp *dpaa_bp)
1541 {
1542         struct net_device *net_dev = dpaa_bp->priv->net_dev;
1543         struct bm_buffer bmb[8];
1544         dma_addr_t addr;
1545         struct page *p;
1546         u8 i;
1547
1548         for (i = 0; i < 8; i++) {
1549                 p = dev_alloc_pages(0);
1550                 if (unlikely(!p)) {
1551                         netdev_err(net_dev, "dev_alloc_pages() failed\n");
1552                         goto release_previous_buffs;
1553                 }
1554
1555                 addr = dma_map_page(dpaa_bp->priv->rx_dma_dev, p, 0,
1556                                     DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1557                 if (unlikely(dma_mapping_error(dpaa_bp->priv->rx_dma_dev,
1558                                                addr))) {
1559                         netdev_err(net_dev, "DMA map failed\n");
1560                         goto release_previous_buffs;
1561                 }
1562
1563                 bmb[i].data = 0;
1564                 bm_buffer_set64(&bmb[i], addr);
1565         }
1566
1567 release_bufs:
1568         return dpaa_bman_release(dpaa_bp, bmb, i);
1569
1570 release_previous_buffs:
1571         WARN_ONCE(1, "dpaa_eth: failed to add buffers on Rx\n");
1572
1573         bm_buffer_set64(&bmb[i], 0);
1574         /* Avoid releasing a completely null buffer; bman_release() requires
1575          * at least one buffer.
1576          */
1577         if (likely(i))
1578                 goto release_bufs;
1579
1580         return 0;
1581 }
1582
1583 static int dpaa_bp_seed(struct dpaa_bp *dpaa_bp)
1584 {
1585         int i;
1586
1587         /* Give each CPU an allotment of "config_count" buffers */
1588         for_each_possible_cpu(i) {
1589                 int *count_ptr = per_cpu_ptr(dpaa_bp->percpu_count, i);
1590                 int j;
1591
1592                 /* Although we access another CPU's counters here
1593                  * we do it at boot time so it is safe
1594                  */
1595                 for (j = 0; j < dpaa_bp->config_count; j += 8)
1596                         *count_ptr += dpaa_bp_add_8_bufs(dpaa_bp);
1597         }
1598         return 0;
1599 }
1600
1601 /* Add buffers/(pages) for Rx processing whenever bpool count falls below
1602  * REFILL_THRESHOLD.
1603  */
1604 static int dpaa_eth_refill_bpool(struct dpaa_bp *dpaa_bp, int *countptr)
1605 {
1606         int count = *countptr;
1607         int new_bufs;
1608
1609         if (unlikely(count < FSL_DPAA_ETH_REFILL_THRESHOLD)) {
1610                 do {
1611                         new_bufs = dpaa_bp_add_8_bufs(dpaa_bp);
1612                         if (unlikely(!new_bufs)) {
1613                                 /* Avoid looping forever if we've temporarily
1614                                  * run out of memory. We'll try again at the
1615                                  * next NAPI cycle.
1616                                  */
1617                                 break;
1618                         }
1619                         count += new_bufs;
1620                 } while (count < FSL_DPAA_ETH_MAX_BUF_COUNT);
1621
1622                 *countptr = count;
1623                 if (unlikely(count < FSL_DPAA_ETH_MAX_BUF_COUNT))
1624                         return -ENOMEM;
1625         }
1626
1627         return 0;
1628 }
1629
1630 static int dpaa_eth_refill_bpools(struct dpaa_priv *priv)
1631 {
1632         struct dpaa_bp *dpaa_bp;
1633         int *countptr;
1634
1635         dpaa_bp = priv->dpaa_bp;
1636         if (!dpaa_bp)
1637                 return -EINVAL;
1638         countptr = this_cpu_ptr(dpaa_bp->percpu_count);
1639
1640         return dpaa_eth_refill_bpool(dpaa_bp, countptr);
1641 }
1642
1643 /* Cleanup function for outgoing frame descriptors that were built on Tx path,
1644  * either contiguous frames or scatter/gather ones.
1645  * Skb freeing is not handled here.
1646  *
1647  * This function may be called on error paths in the Tx function, so guard
1648  * against cases when not all fd relevant fields were filled in. To avoid
1649  * reading the invalid transmission timestamp for the error paths set ts to
1650  * false.
1651  *
1652  * Return the skb backpointer, since for S/G frames the buffer containing it
1653  * gets freed here.
1654  *
1655  * No skb backpointer is set when transmitting XDP frames. Cleanup the buffer
1656  * and return NULL in this case.
1657  */
1658 static struct sk_buff *dpaa_cleanup_tx_fd(const struct dpaa_priv *priv,
1659                                           const struct qm_fd *fd, bool ts)
1660 {
1661         const enum dma_data_direction dma_dir = DMA_TO_DEVICE;
1662         struct device *dev = priv->net_dev->dev.parent;
1663         struct skb_shared_hwtstamps shhwtstamps;
1664         dma_addr_t addr = qm_fd_addr(fd);
1665         void *vaddr = phys_to_virt(addr);
1666         const struct qm_sg_entry *sgt;
1667         struct dpaa_eth_swbp *swbp;
1668         struct sk_buff *skb;
1669         u64 ns;
1670         int i;
1671
1672         if (unlikely(qm_fd_get_format(fd) == qm_fd_sg)) {
1673                 dma_unmap_page(priv->tx_dma_dev, addr,
1674                                qm_fd_get_offset(fd) + DPAA_SGT_SIZE,
1675                                dma_dir);
1676
1677                 /* The sgt buffer has been allocated with netdev_alloc_frag(),
1678                  * it's from lowmem.
1679                  */
1680                 sgt = vaddr + qm_fd_get_offset(fd);
1681
1682                 /* sgt[0] is from lowmem, was dma_map_single()-ed */
1683                 dma_unmap_single(priv->tx_dma_dev, qm_sg_addr(&sgt[0]),
1684                                  qm_sg_entry_get_len(&sgt[0]), dma_dir);
1685
1686                 /* remaining pages were mapped with skb_frag_dma_map() */
1687                 for (i = 1; (i < DPAA_SGT_MAX_ENTRIES) &&
1688                      !qm_sg_entry_is_final(&sgt[i - 1]); i++) {
1689                         WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1690
1691                         dma_unmap_page(priv->tx_dma_dev, qm_sg_addr(&sgt[i]),
1692                                        qm_sg_entry_get_len(&sgt[i]), dma_dir);
1693                 }
1694         } else {
1695                 dma_unmap_single(priv->tx_dma_dev, addr,
1696                                  qm_fd_get_offset(fd) + qm_fd_get_length(fd),
1697                                  dma_dir);
1698         }
1699
1700         swbp = (struct dpaa_eth_swbp *)vaddr;
1701         skb = swbp->skb;
1702
1703         /* No skb backpointer is set when running XDP. An xdp_frame
1704          * backpointer is saved instead.
1705          */
1706         if (!skb) {
1707                 xdp_return_frame(swbp->xdpf);
1708                 return NULL;
1709         }
1710
1711         /* DMA unmapping is required before accessing the HW provided info */
1712         if (ts && priv->tx_tstamp &&
1713             skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
1714                 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
1715
1716                 if (!fman_port_get_tstamp(priv->mac_dev->port[TX], vaddr,
1717                                           &ns)) {
1718                         shhwtstamps.hwtstamp = ns_to_ktime(ns);
1719                         skb_tstamp_tx(skb, &shhwtstamps);
1720                 } else {
1721                         dev_warn(dev, "fman_port_get_tstamp failed!\n");
1722                 }
1723         }
1724
1725         if (qm_fd_get_format(fd) == qm_fd_sg)
1726                 /* Free the page that we allocated on Tx for the SGT */
1727                 free_pages((unsigned long)vaddr, 0);
1728
1729         return skb;
1730 }
1731
1732 static u8 rx_csum_offload(const struct dpaa_priv *priv, const struct qm_fd *fd)
1733 {
1734         /* The parser has run and performed L4 checksum validation.
1735          * We know there were no parser errors (and implicitly no
1736          * L4 csum error), otherwise we wouldn't be here.
1737          */
1738         if ((priv->net_dev->features & NETIF_F_RXCSUM) &&
1739             (be32_to_cpu(fd->status) & FM_FD_STAT_L4CV))
1740                 return CHECKSUM_UNNECESSARY;
1741
1742         /* We're here because either the parser didn't run or the L4 checksum
1743          * was not verified. This may include the case of a UDP frame with
1744          * checksum zero or an L4 proto other than TCP/UDP
1745          */
1746         return CHECKSUM_NONE;
1747 }
1748
1749 #define PTR_IS_ALIGNED(x, a) (IS_ALIGNED((unsigned long)(x), (a)))
1750
1751 /* Build a linear skb around the received buffer.
1752  * We are guaranteed there is enough room at the end of the data buffer to
1753  * accommodate the shared info area of the skb.
1754  */
1755 static struct sk_buff *contig_fd_to_skb(const struct dpaa_priv *priv,
1756                                         const struct qm_fd *fd)
1757 {
1758         ssize_t fd_off = qm_fd_get_offset(fd);
1759         dma_addr_t addr = qm_fd_addr(fd);
1760         struct dpaa_bp *dpaa_bp;
1761         struct sk_buff *skb;
1762         void *vaddr;
1763
1764         vaddr = phys_to_virt(addr);
1765         WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES));
1766
1767         dpaa_bp = dpaa_bpid2pool(fd->bpid);
1768         if (!dpaa_bp)
1769                 goto free_buffer;
1770
1771         skb = build_skb(vaddr, dpaa_bp->size +
1772                         SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
1773         if (WARN_ONCE(!skb, "Build skb failure on Rx\n"))
1774                 goto free_buffer;
1775         skb_reserve(skb, fd_off);
1776         skb_put(skb, qm_fd_get_length(fd));
1777
1778         skb->ip_summed = rx_csum_offload(priv, fd);
1779
1780         return skb;
1781
1782 free_buffer:
1783         free_pages((unsigned long)vaddr, 0);
1784         return NULL;
1785 }
1786
1787 /* Build an skb with the data of the first S/G entry in the linear portion and
1788  * the rest of the frame as skb fragments.
1789  *
1790  * The page fragment holding the S/G Table is recycled here.
1791  */
1792 static struct sk_buff *sg_fd_to_skb(const struct dpaa_priv *priv,
1793                                     const struct qm_fd *fd)
1794 {
1795         ssize_t fd_off = qm_fd_get_offset(fd);
1796         dma_addr_t addr = qm_fd_addr(fd);
1797         const struct qm_sg_entry *sgt;
1798         struct page *page, *head_page;
1799         struct dpaa_bp *dpaa_bp;
1800         void *vaddr, *sg_vaddr;
1801         int frag_off, frag_len;
1802         struct sk_buff *skb;
1803         dma_addr_t sg_addr;
1804         int page_offset;
1805         unsigned int sz;
1806         int *count_ptr;
1807         int i, j;
1808
1809         vaddr = phys_to_virt(addr);
1810         WARN_ON(!IS_ALIGNED((unsigned long)vaddr, SMP_CACHE_BYTES));
1811
1812         /* Iterate through the SGT entries and add data buffers to the skb */
1813         sgt = vaddr + fd_off;
1814         skb = NULL;
1815         for (i = 0; i < DPAA_SGT_MAX_ENTRIES; i++) {
1816                 /* Extension bit is not supported */
1817                 WARN_ON(qm_sg_entry_is_ext(&sgt[i]));
1818
1819                 sg_addr = qm_sg_addr(&sgt[i]);
1820                 sg_vaddr = phys_to_virt(sg_addr);
1821                 WARN_ON(!PTR_IS_ALIGNED(sg_vaddr, SMP_CACHE_BYTES));
1822
1823                 dma_unmap_page(priv->rx_dma_dev, sg_addr,
1824                                DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1825
1826                 /* We may use multiple Rx pools */
1827                 dpaa_bp = dpaa_bpid2pool(sgt[i].bpid);
1828                 if (!dpaa_bp)
1829                         goto free_buffers;
1830
1831                 if (!skb) {
1832                         sz = dpaa_bp->size +
1833                                 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1834                         skb = build_skb(sg_vaddr, sz);
1835                         if (WARN_ON(!skb))
1836                                 goto free_buffers;
1837
1838                         skb->ip_summed = rx_csum_offload(priv, fd);
1839
1840                         /* Make sure forwarded skbs will have enough space
1841                          * on Tx, if extra headers are added.
1842                          */
1843                         WARN_ON(fd_off != priv->rx_headroom);
1844                         skb_reserve(skb, fd_off);
1845                         skb_put(skb, qm_sg_entry_get_len(&sgt[i]));
1846                 } else {
1847                         /* Not the first S/G entry; all data from buffer will
1848                          * be added in an skb fragment; fragment index is offset
1849                          * by one since first S/G entry was incorporated in the
1850                          * linear part of the skb.
1851                          *
1852                          * Caution: 'page' may be a tail page.
1853                          */
1854                         page = virt_to_page(sg_vaddr);
1855                         head_page = virt_to_head_page(sg_vaddr);
1856
1857                         /* Compute offset in (possibly tail) page */
1858                         page_offset = ((unsigned long)sg_vaddr &
1859                                         (PAGE_SIZE - 1)) +
1860                                 (page_address(page) - page_address(head_page));
1861                         /* page_offset only refers to the beginning of sgt[i];
1862                          * but the buffer itself may have an internal offset.
1863                          */
1864                         frag_off = qm_sg_entry_get_off(&sgt[i]) + page_offset;
1865                         frag_len = qm_sg_entry_get_len(&sgt[i]);
1866                         /* skb_add_rx_frag() does no checking on the page; if
1867                          * we pass it a tail page, we'll end up with
1868                          * bad page accounting and eventually with segafults.
1869                          */
1870                         skb_add_rx_frag(skb, i - 1, head_page, frag_off,
1871                                         frag_len, dpaa_bp->size);
1872                 }
1873
1874                 /* Update the pool count for the current {cpu x bpool} */
1875                 count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
1876                 (*count_ptr)--;
1877
1878                 if (qm_sg_entry_is_final(&sgt[i]))
1879                         break;
1880         }
1881         WARN_ONCE(i == DPAA_SGT_MAX_ENTRIES, "No final bit on SGT\n");
1882
1883         /* free the SG table buffer */
1884         free_pages((unsigned long)vaddr, 0);
1885
1886         return skb;
1887
1888 free_buffers:
1889         /* free all the SG entries */
1890         for (j = 0; j < DPAA_SGT_MAX_ENTRIES ; j++) {
1891                 sg_addr = qm_sg_addr(&sgt[j]);
1892                 sg_vaddr = phys_to_virt(sg_addr);
1893                 /* all pages 0..i were unmaped */
1894                 if (j > i)
1895                         dma_unmap_page(priv->rx_dma_dev, qm_sg_addr(&sgt[j]),
1896                                        DPAA_BP_RAW_SIZE, DMA_FROM_DEVICE);
1897                 free_pages((unsigned long)sg_vaddr, 0);
1898                 /* counters 0..i-1 were decremented */
1899                 if (j >= i) {
1900                         dpaa_bp = dpaa_bpid2pool(sgt[j].bpid);
1901                         if (dpaa_bp) {
1902                                 count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
1903                                 (*count_ptr)--;
1904                         }
1905                 }
1906
1907                 if (qm_sg_entry_is_final(&sgt[j]))
1908                         break;
1909         }
1910         /* free the SGT fragment */
1911         free_pages((unsigned long)vaddr, 0);
1912
1913         return NULL;
1914 }
1915
1916 static int skb_to_contig_fd(struct dpaa_priv *priv,
1917                             struct sk_buff *skb, struct qm_fd *fd,
1918                             int *offset)
1919 {
1920         struct net_device *net_dev = priv->net_dev;
1921         enum dma_data_direction dma_dir;
1922         struct dpaa_eth_swbp *swbp;
1923         unsigned char *buff_start;
1924         dma_addr_t addr;
1925         int err;
1926
1927         /* We are guaranteed to have at least tx_headroom bytes
1928          * available, so just use that for offset.
1929          */
1930         fd->bpid = FSL_DPAA_BPID_INV;
1931         buff_start = skb->data - priv->tx_headroom;
1932         dma_dir = DMA_TO_DEVICE;
1933
1934         swbp = (struct dpaa_eth_swbp *)buff_start;
1935         swbp->skb = skb;
1936
1937         /* Enable L3/L4 hardware checksum computation.
1938          *
1939          * We must do this before dma_map_single(DMA_TO_DEVICE), because we may
1940          * need to write into the skb.
1941          */
1942         err = dpaa_enable_tx_csum(priv, skb, fd,
1943                                   buff_start + DPAA_TX_PRIV_DATA_SIZE);
1944         if (unlikely(err < 0)) {
1945                 if (net_ratelimit())
1946                         netif_err(priv, tx_err, net_dev, "HW csum error: %d\n",
1947                                   err);
1948                 return err;
1949         }
1950
1951         /* Fill in the rest of the FD fields */
1952         qm_fd_set_contig(fd, priv->tx_headroom, skb->len);
1953         fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO);
1954
1955         /* Map the entire buffer size that may be seen by FMan, but no more */
1956         addr = dma_map_single(priv->tx_dma_dev, buff_start,
1957                               priv->tx_headroom + skb->len, dma_dir);
1958         if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
1959                 if (net_ratelimit())
1960                         netif_err(priv, tx_err, net_dev, "dma_map_single() failed\n");
1961                 return -EINVAL;
1962         }
1963         qm_fd_addr_set64(fd, addr);
1964
1965         return 0;
1966 }
1967
1968 static int skb_to_sg_fd(struct dpaa_priv *priv,
1969                         struct sk_buff *skb, struct qm_fd *fd)
1970 {
1971         const enum dma_data_direction dma_dir = DMA_TO_DEVICE;
1972         const int nr_frags = skb_shinfo(skb)->nr_frags;
1973         struct net_device *net_dev = priv->net_dev;
1974         struct dpaa_eth_swbp *swbp;
1975         struct qm_sg_entry *sgt;
1976         void *buff_start;
1977         skb_frag_t *frag;
1978         dma_addr_t addr;
1979         size_t frag_len;
1980         struct page *p;
1981         int i, j, err;
1982
1983         /* get a page to store the SGTable */
1984         p = dev_alloc_pages(0);
1985         if (unlikely(!p)) {
1986                 netdev_err(net_dev, "dev_alloc_pages() failed\n");
1987                 return -ENOMEM;
1988         }
1989         buff_start = page_address(p);
1990
1991         /* Enable L3/L4 hardware checksum computation.
1992          *
1993          * We must do this before dma_map_single(DMA_TO_DEVICE), because we may
1994          * need to write into the skb.
1995          */
1996         err = dpaa_enable_tx_csum(priv, skb, fd,
1997                                   buff_start + DPAA_TX_PRIV_DATA_SIZE);
1998         if (unlikely(err < 0)) {
1999                 if (net_ratelimit())
2000                         netif_err(priv, tx_err, net_dev, "HW csum error: %d\n",
2001                                   err);
2002                 goto csum_failed;
2003         }
2004
2005         /* SGT[0] is used by the linear part */
2006         sgt = (struct qm_sg_entry *)(buff_start + priv->tx_headroom);
2007         frag_len = skb_headlen(skb);
2008         qm_sg_entry_set_len(&sgt[0], frag_len);
2009         sgt[0].bpid = FSL_DPAA_BPID_INV;
2010         sgt[0].offset = 0;
2011         addr = dma_map_single(priv->tx_dma_dev, skb->data,
2012                               skb_headlen(skb), dma_dir);
2013         if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2014                 netdev_err(priv->net_dev, "DMA mapping failed\n");
2015                 err = -EINVAL;
2016                 goto sg0_map_failed;
2017         }
2018         qm_sg_entry_set64(&sgt[0], addr);
2019
2020         /* populate the rest of SGT entries */
2021         for (i = 0; i < nr_frags; i++) {
2022                 frag = &skb_shinfo(skb)->frags[i];
2023                 frag_len = skb_frag_size(frag);
2024                 WARN_ON(!skb_frag_page(frag));
2025                 addr = skb_frag_dma_map(priv->tx_dma_dev, frag, 0,
2026                                         frag_len, dma_dir);
2027                 if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2028                         netdev_err(priv->net_dev, "DMA mapping failed\n");
2029                         err = -EINVAL;
2030                         goto sg_map_failed;
2031                 }
2032
2033                 qm_sg_entry_set_len(&sgt[i + 1], frag_len);
2034                 sgt[i + 1].bpid = FSL_DPAA_BPID_INV;
2035                 sgt[i + 1].offset = 0;
2036
2037                 /* keep the offset in the address */
2038                 qm_sg_entry_set64(&sgt[i + 1], addr);
2039         }
2040
2041         /* Set the final bit in the last used entry of the SGT */
2042         qm_sg_entry_set_f(&sgt[nr_frags], frag_len);
2043
2044         /* set fd offset to priv->tx_headroom */
2045         qm_fd_set_sg(fd, priv->tx_headroom, skb->len);
2046
2047         /* DMA map the SGT page */
2048         swbp = (struct dpaa_eth_swbp *)buff_start;
2049         swbp->skb = skb;
2050
2051         addr = dma_map_page(priv->tx_dma_dev, p, 0,
2052                             priv->tx_headroom + DPAA_SGT_SIZE, dma_dir);
2053         if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2054                 netdev_err(priv->net_dev, "DMA mapping failed\n");
2055                 err = -EINVAL;
2056                 goto sgt_map_failed;
2057         }
2058
2059         fd->bpid = FSL_DPAA_BPID_INV;
2060         fd->cmd |= cpu_to_be32(FM_FD_CMD_FCO);
2061         qm_fd_addr_set64(fd, addr);
2062
2063         return 0;
2064
2065 sgt_map_failed:
2066 sg_map_failed:
2067         for (j = 0; j < i; j++)
2068                 dma_unmap_page(priv->tx_dma_dev, qm_sg_addr(&sgt[j]),
2069                                qm_sg_entry_get_len(&sgt[j]), dma_dir);
2070 sg0_map_failed:
2071 csum_failed:
2072         free_pages((unsigned long)buff_start, 0);
2073
2074         return err;
2075 }
2076
2077 static inline int dpaa_xmit(struct dpaa_priv *priv,
2078                             struct rtnl_link_stats64 *percpu_stats,
2079                             int queue,
2080                             struct qm_fd *fd)
2081 {
2082         struct qman_fq *egress_fq;
2083         int err, i;
2084
2085         egress_fq = priv->egress_fqs[queue];
2086         if (fd->bpid == FSL_DPAA_BPID_INV)
2087                 fd->cmd |= cpu_to_be32(qman_fq_fqid(priv->conf_fqs[queue]));
2088
2089         /* Trace this Tx fd */
2090         trace_dpaa_tx_fd(priv->net_dev, egress_fq, fd);
2091
2092         for (i = 0; i < DPAA_ENQUEUE_RETRIES; i++) {
2093                 err = qman_enqueue(egress_fq, fd);
2094                 if (err != -EBUSY)
2095                         break;
2096         }
2097
2098         if (unlikely(err < 0)) {
2099                 percpu_stats->tx_fifo_errors++;
2100                 return err;
2101         }
2102
2103         percpu_stats->tx_packets++;
2104         percpu_stats->tx_bytes += qm_fd_get_length(fd);
2105
2106         return 0;
2107 }
2108
2109 #ifdef CONFIG_DPAA_ERRATUM_A050385
2110 static int dpaa_a050385_wa_skb(struct net_device *net_dev, struct sk_buff **s)
2111 {
2112         struct dpaa_priv *priv = netdev_priv(net_dev);
2113         struct sk_buff *new_skb, *skb = *s;
2114         unsigned char *start, i;
2115
2116         /* check linear buffer alignment */
2117         if (!PTR_IS_ALIGNED(skb->data, DPAA_A050385_ALIGN))
2118                 goto workaround;
2119
2120         /* linear buffers just need to have an aligned start */
2121         if (!skb_is_nonlinear(skb))
2122                 return 0;
2123
2124         /* linear data size for nonlinear skbs needs to be aligned */
2125         if (!IS_ALIGNED(skb_headlen(skb), DPAA_A050385_ALIGN))
2126                 goto workaround;
2127
2128         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2129                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2130
2131                 /* all fragments need to have aligned start addresses */
2132                 if (!IS_ALIGNED(skb_frag_off(frag), DPAA_A050385_ALIGN))
2133                         goto workaround;
2134
2135                 /* all but last fragment need to have aligned sizes */
2136                 if (!IS_ALIGNED(skb_frag_size(frag), DPAA_A050385_ALIGN) &&
2137                     (i < skb_shinfo(skb)->nr_frags - 1))
2138                         goto workaround;
2139         }
2140
2141         return 0;
2142
2143 workaround:
2144         /* copy all the skb content into a new linear buffer */
2145         new_skb = netdev_alloc_skb(net_dev, skb->len + DPAA_A050385_ALIGN - 1 +
2146                                                 priv->tx_headroom);
2147         if (!new_skb)
2148                 return -ENOMEM;
2149
2150         /* NET_SKB_PAD bytes already reserved, adding up to tx_headroom */
2151         skb_reserve(new_skb, priv->tx_headroom - NET_SKB_PAD);
2152
2153         /* Workaround for DPAA_A050385 requires data start to be aligned */
2154         start = PTR_ALIGN(new_skb->data, DPAA_A050385_ALIGN);
2155         if (start - new_skb->data)
2156                 skb_reserve(new_skb, start - new_skb->data);
2157
2158         skb_put(new_skb, skb->len);
2159         skb_copy_bits(skb, 0, new_skb->data, skb->len);
2160         skb_copy_header(new_skb, skb);
2161         new_skb->dev = skb->dev;
2162
2163         /* Copy relevant timestamp info from the old skb to the new */
2164         if (priv->tx_tstamp) {
2165                 skb_shinfo(new_skb)->tx_flags = skb_shinfo(skb)->tx_flags;
2166                 skb_shinfo(new_skb)->hwtstamps = skb_shinfo(skb)->hwtstamps;
2167                 skb_shinfo(new_skb)->tskey = skb_shinfo(skb)->tskey;
2168                 if (skb->sk)
2169                         skb_set_owner_w(new_skb, skb->sk);
2170         }
2171
2172         /* We move the headroom when we align it so we have to reset the
2173          * network and transport header offsets relative to the new data
2174          * pointer. The checksum offload relies on these offsets.
2175          */
2176         skb_set_network_header(new_skb, skb_network_offset(skb));
2177         skb_set_transport_header(new_skb, skb_transport_offset(skb));
2178
2179         dev_kfree_skb(skb);
2180         *s = new_skb;
2181
2182         return 0;
2183 }
2184
2185 static int dpaa_a050385_wa_xdpf(struct dpaa_priv *priv,
2186                                 struct xdp_frame **init_xdpf)
2187 {
2188         struct xdp_frame *new_xdpf, *xdpf = *init_xdpf;
2189         void *new_buff, *aligned_data;
2190         struct page *p;
2191         u32 data_shift;
2192         int headroom;
2193
2194         /* Check the data alignment and make sure the headroom is large
2195          * enough to store the xdpf backpointer. Use an aligned headroom
2196          * value.
2197          *
2198          * Due to alignment constraints, we give XDP access to the full 256
2199          * byte frame headroom. If the XDP program uses all of it, copy the
2200          * data to a new buffer and make room for storing the backpointer.
2201          */
2202         if (PTR_IS_ALIGNED(xdpf->data, DPAA_FD_DATA_ALIGNMENT) &&
2203             xdpf->headroom >= priv->tx_headroom) {
2204                 xdpf->headroom = priv->tx_headroom;
2205                 return 0;
2206         }
2207
2208         /* Try to move the data inside the buffer just enough to align it and
2209          * store the xdpf backpointer. If the available headroom isn't large
2210          * enough, resort to allocating a new buffer and copying the data.
2211          */
2212         aligned_data = PTR_ALIGN_DOWN(xdpf->data, DPAA_FD_DATA_ALIGNMENT);
2213         data_shift = xdpf->data - aligned_data;
2214
2215         /* The XDP frame's headroom needs to be large enough to accommodate
2216          * shifting the data as well as storing the xdpf backpointer.
2217          */
2218         if (xdpf->headroom  >= data_shift + priv->tx_headroom) {
2219                 memmove(aligned_data, xdpf->data, xdpf->len);
2220                 xdpf->data = aligned_data;
2221                 xdpf->headroom = priv->tx_headroom;
2222                 return 0;
2223         }
2224
2225         /* The new xdp_frame is stored in the new buffer. Reserve enough space
2226          * in the headroom for storing it along with the driver's private
2227          * info. The headroom needs to be aligned to DPAA_FD_DATA_ALIGNMENT to
2228          * guarantee the data's alignment in the buffer.
2229          */
2230         headroom = ALIGN(sizeof(*new_xdpf) + priv->tx_headroom,
2231                          DPAA_FD_DATA_ALIGNMENT);
2232
2233         /* Assure the extended headroom and data don't overflow the buffer,
2234          * while maintaining the mandatory tailroom.
2235          */
2236         if (headroom + xdpf->len > DPAA_BP_RAW_SIZE -
2237                         SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
2238                 return -ENOMEM;
2239
2240         p = dev_alloc_pages(0);
2241         if (unlikely(!p))
2242                 return -ENOMEM;
2243
2244         /* Copy the data to the new buffer at a properly aligned offset */
2245         new_buff = page_address(p);
2246         memcpy(new_buff + headroom, xdpf->data, xdpf->len);
2247
2248         /* Create an XDP frame around the new buffer in a similar fashion
2249          * to xdp_convert_buff_to_frame.
2250          */
2251         new_xdpf = new_buff;
2252         new_xdpf->data = new_buff + headroom;
2253         new_xdpf->len = xdpf->len;
2254         new_xdpf->headroom = priv->tx_headroom;
2255         new_xdpf->frame_sz = DPAA_BP_RAW_SIZE;
2256         new_xdpf->mem.type = MEM_TYPE_PAGE_ORDER0;
2257
2258         /* Release the initial buffer */
2259         xdp_return_frame_rx_napi(xdpf);
2260
2261         *init_xdpf = new_xdpf;
2262         return 0;
2263 }
2264 #endif
2265
2266 static netdev_tx_t
2267 dpaa_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
2268 {
2269         const int queue_mapping = skb_get_queue_mapping(skb);
2270         bool nonlinear = skb_is_nonlinear(skb);
2271         struct rtnl_link_stats64 *percpu_stats;
2272         struct dpaa_percpu_priv *percpu_priv;
2273         struct netdev_queue *txq;
2274         struct dpaa_priv *priv;
2275         struct qm_fd fd;
2276         int offset = 0;
2277         int err = 0;
2278
2279         priv = netdev_priv(net_dev);
2280         percpu_priv = this_cpu_ptr(priv->percpu_priv);
2281         percpu_stats = &percpu_priv->stats;
2282
2283         qm_fd_clear_fd(&fd);
2284
2285         if (!nonlinear) {
2286                 /* We're going to store the skb backpointer at the beginning
2287                  * of the data buffer, so we need a privately owned skb
2288                  *
2289                  * We've made sure skb is not shared in dev->priv_flags,
2290                  * we need to verify the skb head is not cloned
2291                  */
2292                 if (skb_cow_head(skb, priv->tx_headroom))
2293                         goto enomem;
2294
2295                 WARN_ON(skb_is_nonlinear(skb));
2296         }
2297
2298         /* MAX_SKB_FRAGS is equal or larger than our dpaa_SGT_MAX_ENTRIES;
2299          * make sure we don't feed FMan with more fragments than it supports.
2300          */
2301         if (unlikely(nonlinear &&
2302                      (skb_shinfo(skb)->nr_frags >= DPAA_SGT_MAX_ENTRIES))) {
2303                 /* If the egress skb contains more fragments than we support
2304                  * we have no choice but to linearize it ourselves.
2305                  */
2306                 if (__skb_linearize(skb))
2307                         goto enomem;
2308
2309                 nonlinear = skb_is_nonlinear(skb);
2310         }
2311
2312 #ifdef CONFIG_DPAA_ERRATUM_A050385
2313         if (unlikely(fman_has_errata_a050385())) {
2314                 if (dpaa_a050385_wa_skb(net_dev, &skb))
2315                         goto enomem;
2316                 nonlinear = skb_is_nonlinear(skb);
2317         }
2318 #endif
2319
2320         if (nonlinear) {
2321                 /* Just create a S/G fd based on the skb */
2322                 err = skb_to_sg_fd(priv, skb, &fd);
2323                 percpu_priv->tx_frag_skbuffs++;
2324         } else {
2325                 /* Create a contig FD from this skb */
2326                 err = skb_to_contig_fd(priv, skb, &fd, &offset);
2327         }
2328         if (unlikely(err < 0))
2329                 goto skb_to_fd_failed;
2330
2331         txq = netdev_get_tx_queue(net_dev, queue_mapping);
2332
2333         /* LLTX requires to do our own update of trans_start */
2334         txq_trans_cond_update(txq);
2335
2336         if (priv->tx_tstamp && skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) {
2337                 fd.cmd |= cpu_to_be32(FM_FD_CMD_UPD);
2338                 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2339         }
2340
2341         if (likely(dpaa_xmit(priv, percpu_stats, queue_mapping, &fd) == 0))
2342                 return NETDEV_TX_OK;
2343
2344         dpaa_cleanup_tx_fd(priv, &fd, false);
2345 skb_to_fd_failed:
2346 enomem:
2347         percpu_stats->tx_errors++;
2348         dev_kfree_skb(skb);
2349         return NETDEV_TX_OK;
2350 }
2351
2352 static void dpaa_rx_error(struct net_device *net_dev,
2353                           const struct dpaa_priv *priv,
2354                           struct dpaa_percpu_priv *percpu_priv,
2355                           const struct qm_fd *fd,
2356                           u32 fqid)
2357 {
2358         if (net_ratelimit())
2359                 netif_err(priv, hw, net_dev, "Err FD status = 0x%08x\n",
2360                           be32_to_cpu(fd->status) & FM_FD_STAT_RX_ERRORS);
2361
2362         percpu_priv->stats.rx_errors++;
2363
2364         if (be32_to_cpu(fd->status) & FM_FD_ERR_DMA)
2365                 percpu_priv->rx_errors.dme++;
2366         if (be32_to_cpu(fd->status) & FM_FD_ERR_PHYSICAL)
2367                 percpu_priv->rx_errors.fpe++;
2368         if (be32_to_cpu(fd->status) & FM_FD_ERR_SIZE)
2369                 percpu_priv->rx_errors.fse++;
2370         if (be32_to_cpu(fd->status) & FM_FD_ERR_PRS_HDR_ERR)
2371                 percpu_priv->rx_errors.phe++;
2372
2373         dpaa_fd_release(net_dev, fd);
2374 }
2375
2376 static void dpaa_tx_error(struct net_device *net_dev,
2377                           const struct dpaa_priv *priv,
2378                           struct dpaa_percpu_priv *percpu_priv,
2379                           const struct qm_fd *fd,
2380                           u32 fqid)
2381 {
2382         struct sk_buff *skb;
2383
2384         if (net_ratelimit())
2385                 netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2386                            be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS);
2387
2388         percpu_priv->stats.tx_errors++;
2389
2390         skb = dpaa_cleanup_tx_fd(priv, fd, false);
2391         dev_kfree_skb(skb);
2392 }
2393
2394 static int dpaa_eth_poll(struct napi_struct *napi, int budget)
2395 {
2396         struct dpaa_napi_portal *np =
2397                         container_of(napi, struct dpaa_napi_portal, napi);
2398         int cleaned;
2399
2400         np->xdp_act = 0;
2401
2402         cleaned = qman_p_poll_dqrr(np->p, budget);
2403
2404         if (np->xdp_act & XDP_REDIRECT)
2405                 xdp_do_flush();
2406
2407         if (cleaned < budget) {
2408                 napi_complete_done(napi, cleaned);
2409                 qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
2410         } else if (np->down) {
2411                 qman_p_irqsource_add(np->p, QM_PIRQ_DQRI);
2412         }
2413
2414         return cleaned;
2415 }
2416
2417 static void dpaa_tx_conf(struct net_device *net_dev,
2418                          const struct dpaa_priv *priv,
2419                          struct dpaa_percpu_priv *percpu_priv,
2420                          const struct qm_fd *fd,
2421                          u32 fqid)
2422 {
2423         struct sk_buff  *skb;
2424
2425         if (unlikely(be32_to_cpu(fd->status) & FM_FD_STAT_TX_ERRORS)) {
2426                 if (net_ratelimit())
2427                         netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2428                                    be32_to_cpu(fd->status) &
2429                                    FM_FD_STAT_TX_ERRORS);
2430
2431                 percpu_priv->stats.tx_errors++;
2432         }
2433
2434         percpu_priv->tx_confirm++;
2435
2436         skb = dpaa_cleanup_tx_fd(priv, fd, true);
2437
2438         consume_skb(skb);
2439 }
2440
2441 static inline int dpaa_eth_napi_schedule(struct dpaa_percpu_priv *percpu_priv,
2442                                          struct qman_portal *portal, bool sched_napi)
2443 {
2444         if (sched_napi) {
2445                 /* Disable QMan IRQ and invoke NAPI */
2446                 qman_p_irqsource_remove(portal, QM_PIRQ_DQRI);
2447
2448                 percpu_priv->np.p = portal;
2449                 napi_schedule(&percpu_priv->np.napi);
2450                 percpu_priv->in_interrupt++;
2451                 return 1;
2452         }
2453         return 0;
2454 }
2455
2456 static enum qman_cb_dqrr_result rx_error_dqrr(struct qman_portal *portal,
2457                                               struct qman_fq *fq,
2458                                               const struct qm_dqrr_entry *dq,
2459                                               bool sched_napi)
2460 {
2461         struct dpaa_fq *dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
2462         struct dpaa_percpu_priv *percpu_priv;
2463         struct net_device *net_dev;
2464         struct dpaa_bp *dpaa_bp;
2465         struct dpaa_priv *priv;
2466
2467         net_dev = dpaa_fq->net_dev;
2468         priv = netdev_priv(net_dev);
2469         dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
2470         if (!dpaa_bp)
2471                 return qman_cb_dqrr_consume;
2472
2473         percpu_priv = this_cpu_ptr(priv->percpu_priv);
2474
2475         if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2476                 return qman_cb_dqrr_stop;
2477
2478         dpaa_eth_refill_bpools(priv);
2479         dpaa_rx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2480
2481         return qman_cb_dqrr_consume;
2482 }
2483
2484 static int dpaa_xdp_xmit_frame(struct net_device *net_dev,
2485                                struct xdp_frame *xdpf)
2486 {
2487         struct dpaa_priv *priv = netdev_priv(net_dev);
2488         struct rtnl_link_stats64 *percpu_stats;
2489         struct dpaa_percpu_priv *percpu_priv;
2490         struct dpaa_eth_swbp *swbp;
2491         struct netdev_queue *txq;
2492         void *buff_start;
2493         struct qm_fd fd;
2494         dma_addr_t addr;
2495         int err;
2496
2497         percpu_priv = this_cpu_ptr(priv->percpu_priv);
2498         percpu_stats = &percpu_priv->stats;
2499
2500 #ifdef CONFIG_DPAA_ERRATUM_A050385
2501         if (unlikely(fman_has_errata_a050385())) {
2502                 if (dpaa_a050385_wa_xdpf(priv, &xdpf)) {
2503                         err = -ENOMEM;
2504                         goto out_error;
2505                 }
2506         }
2507 #endif
2508
2509         if (xdpf->headroom < DPAA_TX_PRIV_DATA_SIZE) {
2510                 err = -EINVAL;
2511                 goto out_error;
2512         }
2513
2514         buff_start = xdpf->data - xdpf->headroom;
2515
2516         /* Leave empty the skb backpointer at the start of the buffer.
2517          * Save the XDP frame for easy cleanup on confirmation.
2518          */
2519         swbp = (struct dpaa_eth_swbp *)buff_start;
2520         swbp->skb = NULL;
2521         swbp->xdpf = xdpf;
2522
2523         qm_fd_clear_fd(&fd);
2524         fd.bpid = FSL_DPAA_BPID_INV;
2525         fd.cmd |= cpu_to_be32(FM_FD_CMD_FCO);
2526         qm_fd_set_contig(&fd, xdpf->headroom, xdpf->len);
2527
2528         addr = dma_map_single(priv->tx_dma_dev, buff_start,
2529                               xdpf->headroom + xdpf->len,
2530                               DMA_TO_DEVICE);
2531         if (unlikely(dma_mapping_error(priv->tx_dma_dev, addr))) {
2532                 err = -EINVAL;
2533                 goto out_error;
2534         }
2535
2536         qm_fd_addr_set64(&fd, addr);
2537
2538         /* Bump the trans_start */
2539         txq = netdev_get_tx_queue(net_dev, smp_processor_id());
2540         txq_trans_cond_update(txq);
2541
2542         err = dpaa_xmit(priv, percpu_stats, smp_processor_id(), &fd);
2543         if (err) {
2544                 dma_unmap_single(priv->tx_dma_dev, addr,
2545                                  qm_fd_get_offset(&fd) + qm_fd_get_length(&fd),
2546                                  DMA_TO_DEVICE);
2547                 goto out_error;
2548         }
2549
2550         return 0;
2551
2552 out_error:
2553         percpu_stats->tx_errors++;
2554         return err;
2555 }
2556
2557 static u32 dpaa_run_xdp(struct dpaa_priv *priv, struct qm_fd *fd, void *vaddr,
2558                         struct dpaa_fq *dpaa_fq, unsigned int *xdp_meta_len)
2559 {
2560         ssize_t fd_off = qm_fd_get_offset(fd);
2561         struct bpf_prog *xdp_prog;
2562         struct xdp_frame *xdpf;
2563         struct xdp_buff xdp;
2564         u32 xdp_act;
2565         int err;
2566
2567         xdp_prog = READ_ONCE(priv->xdp_prog);
2568         if (!xdp_prog)
2569                 return XDP_PASS;
2570
2571         xdp_init_buff(&xdp, DPAA_BP_RAW_SIZE - DPAA_TX_PRIV_DATA_SIZE,
2572                       &dpaa_fq->xdp_rxq);
2573         xdp_prepare_buff(&xdp, vaddr + fd_off - XDP_PACKET_HEADROOM,
2574                          XDP_PACKET_HEADROOM, qm_fd_get_length(fd), true);
2575
2576         /* We reserve a fixed headroom of 256 bytes under the erratum and we
2577          * offer it all to XDP programs to use. If no room is left for the
2578          * xdpf backpointer on TX, we will need to copy the data.
2579          * Disable metadata support since data realignments might be required
2580          * and the information can be lost.
2581          */
2582 #ifdef CONFIG_DPAA_ERRATUM_A050385
2583         if (unlikely(fman_has_errata_a050385())) {
2584                 xdp_set_data_meta_invalid(&xdp);
2585                 xdp.data_hard_start = vaddr;
2586                 xdp.frame_sz = DPAA_BP_RAW_SIZE;
2587         }
2588 #endif
2589
2590         xdp_act = bpf_prog_run_xdp(xdp_prog, &xdp);
2591
2592         /* Update the length and the offset of the FD */
2593         qm_fd_set_contig(fd, xdp.data - vaddr, xdp.data_end - xdp.data);
2594
2595         switch (xdp_act) {
2596         case XDP_PASS:
2597 #ifdef CONFIG_DPAA_ERRATUM_A050385
2598                 *xdp_meta_len = xdp_data_meta_unsupported(&xdp) ? 0 :
2599                                 xdp.data - xdp.data_meta;
2600 #else
2601                 *xdp_meta_len = xdp.data - xdp.data_meta;
2602 #endif
2603                 break;
2604         case XDP_TX:
2605                 /* We can access the full headroom when sending the frame
2606                  * back out
2607                  */
2608                 xdp.data_hard_start = vaddr;
2609                 xdp.frame_sz = DPAA_BP_RAW_SIZE;
2610                 xdpf = xdp_convert_buff_to_frame(&xdp);
2611                 if (unlikely(!xdpf)) {
2612                         free_pages((unsigned long)vaddr, 0);
2613                         break;
2614                 }
2615
2616                 if (dpaa_xdp_xmit_frame(priv->net_dev, xdpf))
2617                         xdp_return_frame_rx_napi(xdpf);
2618
2619                 break;
2620         case XDP_REDIRECT:
2621                 /* Allow redirect to use the full headroom */
2622                 xdp.data_hard_start = vaddr;
2623                 xdp.frame_sz = DPAA_BP_RAW_SIZE;
2624
2625                 err = xdp_do_redirect(priv->net_dev, &xdp, xdp_prog);
2626                 if (err) {
2627                         trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
2628                         free_pages((unsigned long)vaddr, 0);
2629                 }
2630                 break;
2631         default:
2632                 bpf_warn_invalid_xdp_action(priv->net_dev, xdp_prog, xdp_act);
2633                 fallthrough;
2634         case XDP_ABORTED:
2635                 trace_xdp_exception(priv->net_dev, xdp_prog, xdp_act);
2636                 fallthrough;
2637         case XDP_DROP:
2638                 /* Free the buffer */
2639                 free_pages((unsigned long)vaddr, 0);
2640                 break;
2641         }
2642
2643         return xdp_act;
2644 }
2645
2646 static enum qman_cb_dqrr_result rx_default_dqrr(struct qman_portal *portal,
2647                                                 struct qman_fq *fq,
2648                                                 const struct qm_dqrr_entry *dq,
2649                                                 bool sched_napi)
2650 {
2651         bool ts_valid = false, hash_valid = false;
2652         struct skb_shared_hwtstamps *shhwtstamps;
2653         unsigned int skb_len, xdp_meta_len = 0;
2654         struct rtnl_link_stats64 *percpu_stats;
2655         struct dpaa_percpu_priv *percpu_priv;
2656         const struct qm_fd *fd = &dq->fd;
2657         dma_addr_t addr = qm_fd_addr(fd);
2658         struct dpaa_napi_portal *np;
2659         enum qm_fd_format fd_format;
2660         struct net_device *net_dev;
2661         u32 fd_status, hash_offset;
2662         struct qm_sg_entry *sgt;
2663         struct dpaa_bp *dpaa_bp;
2664         struct dpaa_fq *dpaa_fq;
2665         struct dpaa_priv *priv;
2666         struct sk_buff *skb;
2667         int *count_ptr;
2668         u32 xdp_act;
2669         void *vaddr;
2670         u32 hash;
2671         u64 ns;
2672
2673         dpaa_fq = container_of(fq, struct dpaa_fq, fq_base);
2674         fd_status = be32_to_cpu(fd->status);
2675         fd_format = qm_fd_get_format(fd);
2676         net_dev = dpaa_fq->net_dev;
2677         priv = netdev_priv(net_dev);
2678         dpaa_bp = dpaa_bpid2pool(dq->fd.bpid);
2679         if (!dpaa_bp)
2680                 return qman_cb_dqrr_consume;
2681
2682         /* Trace the Rx fd */
2683         trace_dpaa_rx_fd(net_dev, fq, &dq->fd);
2684
2685         percpu_priv = this_cpu_ptr(priv->percpu_priv);
2686         percpu_stats = &percpu_priv->stats;
2687         np = &percpu_priv->np;
2688
2689         if (unlikely(dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi)))
2690                 return qman_cb_dqrr_stop;
2691
2692         /* Make sure we didn't run out of buffers */
2693         if (unlikely(dpaa_eth_refill_bpools(priv))) {
2694                 /* Unable to refill the buffer pool due to insufficient
2695                  * system memory. Just release the frame back into the pool,
2696                  * otherwise we'll soon end up with an empty buffer pool.
2697                  */
2698                 dpaa_fd_release(net_dev, &dq->fd);
2699                 return qman_cb_dqrr_consume;
2700         }
2701
2702         if (unlikely(fd_status & FM_FD_STAT_RX_ERRORS) != 0) {
2703                 if (net_ratelimit())
2704                         netif_warn(priv, hw, net_dev, "FD status = 0x%08x\n",
2705                                    fd_status & FM_FD_STAT_RX_ERRORS);
2706
2707                 percpu_stats->rx_errors++;
2708                 dpaa_fd_release(net_dev, fd);
2709                 return qman_cb_dqrr_consume;
2710         }
2711
2712         dma_unmap_page(dpaa_bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE,
2713                        DMA_FROM_DEVICE);
2714
2715         /* prefetch the first 64 bytes of the frame or the SGT start */
2716         vaddr = phys_to_virt(addr);
2717         prefetch(vaddr + qm_fd_get_offset(fd));
2718
2719         /* The only FD types that we may receive are contig and S/G */
2720         WARN_ON((fd_format != qm_fd_contig) && (fd_format != qm_fd_sg));
2721
2722         /* Account for either the contig buffer or the SGT buffer (depending on
2723          * which case we were in) having been removed from the pool.
2724          */
2725         count_ptr = this_cpu_ptr(dpaa_bp->percpu_count);
2726         (*count_ptr)--;
2727
2728         /* Extract the timestamp stored in the headroom before running XDP */
2729         if (priv->rx_tstamp) {
2730                 if (!fman_port_get_tstamp(priv->mac_dev->port[RX], vaddr, &ns))
2731                         ts_valid = true;
2732                 else
2733                         WARN_ONCE(1, "fman_port_get_tstamp failed!\n");
2734         }
2735
2736         /* Extract the hash stored in the headroom before running XDP */
2737         if (net_dev->features & NETIF_F_RXHASH && priv->keygen_in_use &&
2738             !fman_port_get_hash_result_offset(priv->mac_dev->port[RX],
2739                                               &hash_offset)) {
2740                 hash = be32_to_cpu(*(u32 *)(vaddr + hash_offset));
2741                 hash_valid = true;
2742         }
2743
2744         if (likely(fd_format == qm_fd_contig)) {
2745                 xdp_act = dpaa_run_xdp(priv, (struct qm_fd *)fd, vaddr,
2746                                        dpaa_fq, &xdp_meta_len);
2747                 np->xdp_act |= xdp_act;
2748                 if (xdp_act != XDP_PASS) {
2749                         percpu_stats->rx_packets++;
2750                         percpu_stats->rx_bytes += qm_fd_get_length(fd);
2751                         return qman_cb_dqrr_consume;
2752                 }
2753                 skb = contig_fd_to_skb(priv, fd);
2754         } else {
2755                 /* XDP doesn't support S/G frames. Return the fragments to the
2756                  * buffer pool and release the SGT.
2757                  */
2758                 if (READ_ONCE(priv->xdp_prog)) {
2759                         WARN_ONCE(1, "S/G frames not supported under XDP\n");
2760                         sgt = vaddr + qm_fd_get_offset(fd);
2761                         dpaa_release_sgt_members(sgt);
2762                         free_pages((unsigned long)vaddr, 0);
2763                         return qman_cb_dqrr_consume;
2764                 }
2765                 skb = sg_fd_to_skb(priv, fd);
2766         }
2767         if (!skb)
2768                 return qman_cb_dqrr_consume;
2769
2770         if (xdp_meta_len)
2771                 skb_metadata_set(skb, xdp_meta_len);
2772
2773         /* Set the previously extracted timestamp */
2774         if (ts_valid) {
2775                 shhwtstamps = skb_hwtstamps(skb);
2776                 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2777                 shhwtstamps->hwtstamp = ns_to_ktime(ns);
2778         }
2779
2780         skb->protocol = eth_type_trans(skb, net_dev);
2781
2782         /* Set the previously extracted hash */
2783         if (hash_valid) {
2784                 enum pkt_hash_types type;
2785
2786                 /* if L4 exists, it was used in the hash generation */
2787                 type = be32_to_cpu(fd->status) & FM_FD_STAT_L4CV ?
2788                         PKT_HASH_TYPE_L4 : PKT_HASH_TYPE_L3;
2789                 skb_set_hash(skb, hash, type);
2790         }
2791
2792         skb_len = skb->len;
2793
2794         if (unlikely(netif_receive_skb(skb) == NET_RX_DROP)) {
2795                 percpu_stats->rx_dropped++;
2796                 return qman_cb_dqrr_consume;
2797         }
2798
2799         percpu_stats->rx_packets++;
2800         percpu_stats->rx_bytes += skb_len;
2801
2802         return qman_cb_dqrr_consume;
2803 }
2804
2805 static enum qman_cb_dqrr_result conf_error_dqrr(struct qman_portal *portal,
2806                                                 struct qman_fq *fq,
2807                                                 const struct qm_dqrr_entry *dq,
2808                                                 bool sched_napi)
2809 {
2810         struct dpaa_percpu_priv *percpu_priv;
2811         struct net_device *net_dev;
2812         struct dpaa_priv *priv;
2813
2814         net_dev = ((struct dpaa_fq *)fq)->net_dev;
2815         priv = netdev_priv(net_dev);
2816
2817         percpu_priv = this_cpu_ptr(priv->percpu_priv);
2818
2819         if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2820                 return qman_cb_dqrr_stop;
2821
2822         dpaa_tx_error(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2823
2824         return qman_cb_dqrr_consume;
2825 }
2826
2827 static enum qman_cb_dqrr_result conf_dflt_dqrr(struct qman_portal *portal,
2828                                                struct qman_fq *fq,
2829                                                const struct qm_dqrr_entry *dq,
2830                                                bool sched_napi)
2831 {
2832         struct dpaa_percpu_priv *percpu_priv;
2833         struct net_device *net_dev;
2834         struct dpaa_priv *priv;
2835
2836         net_dev = ((struct dpaa_fq *)fq)->net_dev;
2837         priv = netdev_priv(net_dev);
2838
2839         /* Trace the fd */
2840         trace_dpaa_tx_conf_fd(net_dev, fq, &dq->fd);
2841
2842         percpu_priv = this_cpu_ptr(priv->percpu_priv);
2843
2844         if (dpaa_eth_napi_schedule(percpu_priv, portal, sched_napi))
2845                 return qman_cb_dqrr_stop;
2846
2847         dpaa_tx_conf(net_dev, priv, percpu_priv, &dq->fd, fq->fqid);
2848
2849         return qman_cb_dqrr_consume;
2850 }
2851
2852 static void egress_ern(struct qman_portal *portal,
2853                        struct qman_fq *fq,
2854                        const union qm_mr_entry *msg)
2855 {
2856         const struct qm_fd *fd = &msg->ern.fd;
2857         struct dpaa_percpu_priv *percpu_priv;
2858         const struct dpaa_priv *priv;
2859         struct net_device *net_dev;
2860         struct sk_buff *skb;
2861
2862         net_dev = ((struct dpaa_fq *)fq)->net_dev;
2863         priv = netdev_priv(net_dev);
2864         percpu_priv = this_cpu_ptr(priv->percpu_priv);
2865
2866         percpu_priv->stats.tx_dropped++;
2867         percpu_priv->stats.tx_fifo_errors++;
2868         count_ern(percpu_priv, msg);
2869
2870         skb = dpaa_cleanup_tx_fd(priv, fd, false);
2871         dev_kfree_skb_any(skb);
2872 }
2873
2874 static const struct dpaa_fq_cbs dpaa_fq_cbs = {
2875         .rx_defq = { .cb = { .dqrr = rx_default_dqrr } },
2876         .tx_defq = { .cb = { .dqrr = conf_dflt_dqrr } },
2877         .rx_errq = { .cb = { .dqrr = rx_error_dqrr } },
2878         .tx_errq = { .cb = { .dqrr = conf_error_dqrr } },
2879         .egress_ern = { .cb = { .ern = egress_ern } }
2880 };
2881
2882 static void dpaa_eth_napi_enable(struct dpaa_priv *priv)
2883 {
2884         struct dpaa_percpu_priv *percpu_priv;
2885         int i;
2886
2887         for_each_online_cpu(i) {
2888                 percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
2889
2890                 percpu_priv->np.down = false;
2891                 napi_enable(&percpu_priv->np.napi);
2892         }
2893 }
2894
2895 static void dpaa_eth_napi_disable(struct dpaa_priv *priv)
2896 {
2897         struct dpaa_percpu_priv *percpu_priv;
2898         int i;
2899
2900         for_each_online_cpu(i) {
2901                 percpu_priv = per_cpu_ptr(priv->percpu_priv, i);
2902
2903                 percpu_priv->np.down = true;
2904                 napi_disable(&percpu_priv->np.napi);
2905         }
2906 }
2907
2908 static void dpaa_adjust_link(struct net_device *net_dev)
2909 {
2910         struct mac_device *mac_dev;
2911         struct dpaa_priv *priv;
2912
2913         priv = netdev_priv(net_dev);
2914         mac_dev = priv->mac_dev;
2915         mac_dev->adjust_link(mac_dev);
2916 }
2917
2918 /* The Aquantia PHYs are capable of performing rate adaptation */
2919 #define PHY_VEND_AQUANTIA       0x03a1b400
2920 #define PHY_VEND_AQUANTIA2      0x31c31c00
2921
2922 static int dpaa_phy_init(struct net_device *net_dev)
2923 {
2924         __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
2925         struct mac_device *mac_dev;
2926         struct phy_device *phy_dev;
2927         struct dpaa_priv *priv;
2928         u32 phy_vendor;
2929
2930         priv = netdev_priv(net_dev);
2931         mac_dev = priv->mac_dev;
2932
2933         phy_dev = of_phy_connect(net_dev, mac_dev->phy_node,
2934                                  &dpaa_adjust_link, 0,
2935                                  mac_dev->phy_if);
2936         if (!phy_dev) {
2937                 netif_err(priv, ifup, net_dev, "init_phy() failed\n");
2938                 return -ENODEV;
2939         }
2940
2941         phy_vendor = phy_dev->drv->phy_id & GENMASK(31, 10);
2942         /* Unless the PHY is capable of rate adaptation */
2943         if (mac_dev->phy_if != PHY_INTERFACE_MODE_XGMII ||
2944             (phy_vendor != PHY_VEND_AQUANTIA &&
2945              phy_vendor != PHY_VEND_AQUANTIA2)) {
2946                 /* remove any features not supported by the controller */
2947                 ethtool_convert_legacy_u32_to_link_mode(mask,
2948                                                         mac_dev->if_support);
2949                 linkmode_and(phy_dev->supported, phy_dev->supported, mask);
2950         }
2951
2952         phy_support_asym_pause(phy_dev);
2953
2954         mac_dev->phy_dev = phy_dev;
2955         net_dev->phydev = phy_dev;
2956
2957         return 0;
2958 }
2959
2960 static int dpaa_open(struct net_device *net_dev)
2961 {
2962         struct mac_device *mac_dev;
2963         struct dpaa_priv *priv;
2964         int err, i;
2965
2966         priv = netdev_priv(net_dev);
2967         mac_dev = priv->mac_dev;
2968         dpaa_eth_napi_enable(priv);
2969
2970         err = dpaa_phy_init(net_dev);
2971         if (err)
2972                 goto phy_init_failed;
2973
2974         for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
2975                 err = fman_port_enable(mac_dev->port[i]);
2976                 if (err)
2977                         goto mac_start_failed;
2978         }
2979
2980         err = priv->mac_dev->enable(mac_dev->fman_mac);
2981         if (err < 0) {
2982                 netif_err(priv, ifup, net_dev, "mac_dev->enable() = %d\n", err);
2983                 goto mac_start_failed;
2984         }
2985         phy_start(priv->mac_dev->phy_dev);
2986
2987         netif_tx_start_all_queues(net_dev);
2988
2989         return 0;
2990
2991 mac_start_failed:
2992         for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++)
2993                 fman_port_disable(mac_dev->port[i]);
2994
2995 phy_init_failed:
2996         dpaa_eth_napi_disable(priv);
2997
2998         return err;
2999 }
3000
3001 static int dpaa_eth_stop(struct net_device *net_dev)
3002 {
3003         struct dpaa_priv *priv;
3004         int err;
3005
3006         err = dpaa_stop(net_dev);
3007
3008         priv = netdev_priv(net_dev);
3009         dpaa_eth_napi_disable(priv);
3010
3011         return err;
3012 }
3013
3014 static bool xdp_validate_mtu(struct dpaa_priv *priv, int mtu)
3015 {
3016         int max_contig_data = priv->dpaa_bp->size - priv->rx_headroom;
3017
3018         /* We do not support S/G fragments when XDP is enabled.
3019          * Limit the MTU in relation to the buffer size.
3020          */
3021         if (mtu + VLAN_ETH_HLEN + ETH_FCS_LEN > max_contig_data) {
3022                 dev_warn(priv->net_dev->dev.parent,
3023                          "The maximum MTU for XDP is %d\n",
3024                          max_contig_data - VLAN_ETH_HLEN - ETH_FCS_LEN);
3025                 return false;
3026         }
3027
3028         return true;
3029 }
3030
3031 static int dpaa_change_mtu(struct net_device *net_dev, int new_mtu)
3032 {
3033         struct dpaa_priv *priv = netdev_priv(net_dev);
3034
3035         if (priv->xdp_prog && !xdp_validate_mtu(priv, new_mtu))
3036                 return -EINVAL;
3037
3038         net_dev->mtu = new_mtu;
3039         return 0;
3040 }
3041
3042 static int dpaa_setup_xdp(struct net_device *net_dev, struct netdev_bpf *bpf)
3043 {
3044         struct dpaa_priv *priv = netdev_priv(net_dev);
3045         struct bpf_prog *old_prog;
3046         int err;
3047         bool up;
3048
3049         /* S/G fragments are not supported in XDP-mode */
3050         if (bpf->prog && !xdp_validate_mtu(priv, net_dev->mtu)) {
3051                 NL_SET_ERR_MSG_MOD(bpf->extack, "MTU too large for XDP");
3052                 return -EINVAL;
3053         }
3054
3055         up = netif_running(net_dev);
3056
3057         if (up)
3058                 dpaa_eth_stop(net_dev);
3059
3060         old_prog = xchg(&priv->xdp_prog, bpf->prog);
3061         if (old_prog)
3062                 bpf_prog_put(old_prog);
3063
3064         if (up) {
3065                 err = dpaa_open(net_dev);
3066                 if (err) {
3067                         NL_SET_ERR_MSG_MOD(bpf->extack, "dpaa_open() failed");
3068                         return err;
3069                 }
3070         }
3071
3072         return 0;
3073 }
3074
3075 static int dpaa_xdp(struct net_device *net_dev, struct netdev_bpf *xdp)
3076 {
3077         switch (xdp->command) {
3078         case XDP_SETUP_PROG:
3079                 return dpaa_setup_xdp(net_dev, xdp);
3080         default:
3081                 return -EINVAL;
3082         }
3083 }
3084
3085 static int dpaa_xdp_xmit(struct net_device *net_dev, int n,
3086                          struct xdp_frame **frames, u32 flags)
3087 {
3088         struct xdp_frame *xdpf;
3089         int i, nxmit = 0;
3090
3091         if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
3092                 return -EINVAL;
3093
3094         if (!netif_running(net_dev))
3095                 return -ENETDOWN;
3096
3097         for (i = 0; i < n; i++) {
3098                 xdpf = frames[i];
3099                 if (dpaa_xdp_xmit_frame(net_dev, xdpf))
3100                         break;
3101                 nxmit++;
3102         }
3103
3104         return nxmit;
3105 }
3106
3107 static int dpaa_ts_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3108 {
3109         struct dpaa_priv *priv = netdev_priv(dev);
3110         struct hwtstamp_config config;
3111
3112         if (copy_from_user(&config, rq->ifr_data, sizeof(config)))
3113                 return -EFAULT;
3114
3115         switch (config.tx_type) {
3116         case HWTSTAMP_TX_OFF:
3117                 /* Couldn't disable rx/tx timestamping separately.
3118                  * Do nothing here.
3119                  */
3120                 priv->tx_tstamp = false;
3121                 break;
3122         case HWTSTAMP_TX_ON:
3123                 priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true);
3124                 priv->tx_tstamp = true;
3125                 break;
3126         default:
3127                 return -ERANGE;
3128         }
3129
3130         if (config.rx_filter == HWTSTAMP_FILTER_NONE) {
3131                 /* Couldn't disable rx/tx timestamping separately.
3132                  * Do nothing here.
3133                  */
3134                 priv->rx_tstamp = false;
3135         } else {
3136                 priv->mac_dev->set_tstamp(priv->mac_dev->fman_mac, true);
3137                 priv->rx_tstamp = true;
3138                 /* TS is set for all frame types, not only those requested */
3139                 config.rx_filter = HWTSTAMP_FILTER_ALL;
3140         }
3141
3142         return copy_to_user(rq->ifr_data, &config, sizeof(config)) ?
3143                         -EFAULT : 0;
3144 }
3145
3146 static int dpaa_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
3147 {
3148         int ret = -EINVAL;
3149
3150         if (cmd == SIOCGMIIREG) {
3151                 if (net_dev->phydev)
3152                         return phy_mii_ioctl(net_dev->phydev, rq, cmd);
3153         }
3154
3155         if (cmd == SIOCSHWTSTAMP)
3156                 return dpaa_ts_ioctl(net_dev, rq, cmd);
3157
3158         return ret;
3159 }
3160
3161 static const struct net_device_ops dpaa_ops = {
3162         .ndo_open = dpaa_open,
3163         .ndo_start_xmit = dpaa_start_xmit,
3164         .ndo_stop = dpaa_eth_stop,
3165         .ndo_tx_timeout = dpaa_tx_timeout,
3166         .ndo_get_stats64 = dpaa_get_stats64,
3167         .ndo_change_carrier = fixed_phy_change_carrier,
3168         .ndo_set_mac_address = dpaa_set_mac_address,
3169         .ndo_validate_addr = eth_validate_addr,
3170         .ndo_set_rx_mode = dpaa_set_rx_mode,
3171         .ndo_eth_ioctl = dpaa_ioctl,
3172         .ndo_setup_tc = dpaa_setup_tc,
3173         .ndo_change_mtu = dpaa_change_mtu,
3174         .ndo_bpf = dpaa_xdp,
3175         .ndo_xdp_xmit = dpaa_xdp_xmit,
3176 };
3177
3178 static int dpaa_napi_add(struct net_device *net_dev)
3179 {
3180         struct dpaa_priv *priv = netdev_priv(net_dev);
3181         struct dpaa_percpu_priv *percpu_priv;
3182         int cpu;
3183
3184         for_each_possible_cpu(cpu) {
3185                 percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
3186
3187                 netif_napi_add(net_dev, &percpu_priv->np.napi, dpaa_eth_poll);
3188         }
3189
3190         return 0;
3191 }
3192
3193 static void dpaa_napi_del(struct net_device *net_dev)
3194 {
3195         struct dpaa_priv *priv = netdev_priv(net_dev);
3196         struct dpaa_percpu_priv *percpu_priv;
3197         int cpu;
3198
3199         for_each_possible_cpu(cpu) {
3200                 percpu_priv = per_cpu_ptr(priv->percpu_priv, cpu);
3201
3202                 netif_napi_del(&percpu_priv->np.napi);
3203         }
3204 }
3205
3206 static inline void dpaa_bp_free_pf(const struct dpaa_bp *bp,
3207                                    struct bm_buffer *bmb)
3208 {
3209         dma_addr_t addr = bm_buf_addr(bmb);
3210
3211         dma_unmap_page(bp->priv->rx_dma_dev, addr, DPAA_BP_RAW_SIZE,
3212                        DMA_FROM_DEVICE);
3213
3214         skb_free_frag(phys_to_virt(addr));
3215 }
3216
3217 /* Alloc the dpaa_bp struct and configure default values */
3218 static struct dpaa_bp *dpaa_bp_alloc(struct device *dev)
3219 {
3220         struct dpaa_bp *dpaa_bp;
3221
3222         dpaa_bp = devm_kzalloc(dev, sizeof(*dpaa_bp), GFP_KERNEL);
3223         if (!dpaa_bp)
3224                 return ERR_PTR(-ENOMEM);
3225
3226         dpaa_bp->bpid = FSL_DPAA_BPID_INV;
3227         dpaa_bp->percpu_count = devm_alloc_percpu(dev, *dpaa_bp->percpu_count);
3228         if (!dpaa_bp->percpu_count)
3229                 return ERR_PTR(-ENOMEM);
3230
3231         dpaa_bp->config_count = FSL_DPAA_ETH_MAX_BUF_COUNT;
3232
3233         dpaa_bp->seed_cb = dpaa_bp_seed;
3234         dpaa_bp->free_buf_cb = dpaa_bp_free_pf;
3235
3236         return dpaa_bp;
3237 }
3238
3239 /* Place all ingress FQs (Rx Default, Rx Error) in a dedicated CGR.
3240  * We won't be sending congestion notifications to FMan; for now, we just use
3241  * this CGR to generate enqueue rejections to FMan in order to drop the frames
3242  * before they reach our ingress queues and eat up memory.
3243  */
3244 static int dpaa_ingress_cgr_init(struct dpaa_priv *priv)
3245 {
3246         struct qm_mcc_initcgr initcgr;
3247         u32 cs_th;
3248         int err;
3249
3250         err = qman_alloc_cgrid(&priv->ingress_cgr.cgrid);
3251         if (err < 0) {
3252                 if (netif_msg_drv(priv))
3253                         pr_err("Error %d allocating CGR ID\n", err);
3254                 goto out_error;
3255         }
3256
3257         /* Enable CS TD, but disable Congestion State Change Notifications. */
3258         memset(&initcgr, 0, sizeof(initcgr));
3259         initcgr.we_mask = cpu_to_be16(QM_CGR_WE_CS_THRES);
3260         initcgr.cgr.cscn_en = QM_CGR_EN;
3261         cs_th = DPAA_INGRESS_CS_THRESHOLD;
3262         qm_cgr_cs_thres_set64(&initcgr.cgr.cs_thres, cs_th, 1);
3263
3264         initcgr.we_mask |= cpu_to_be16(QM_CGR_WE_CSTD_EN);
3265         initcgr.cgr.cstd_en = QM_CGR_EN;
3266
3267         /* This CGR will be associated with the SWP affined to the current CPU.
3268          * However, we'll place all our ingress FQs in it.
3269          */
3270         err = qman_create_cgr(&priv->ingress_cgr, QMAN_CGR_FLAG_USE_INIT,
3271                               &initcgr);
3272         if (err < 0) {
3273                 if (netif_msg_drv(priv))
3274                         pr_err("Error %d creating ingress CGR with ID %d\n",
3275                                err, priv->ingress_cgr.cgrid);
3276                 qman_release_cgrid(priv->ingress_cgr.cgrid);
3277                 goto out_error;
3278         }
3279         if (netif_msg_drv(priv))
3280                 pr_debug("Created ingress CGR %d for netdev with hwaddr %pM\n",
3281                          priv->ingress_cgr.cgrid, priv->mac_dev->addr);
3282
3283         priv->use_ingress_cgr = true;
3284
3285 out_error:
3286         return err;
3287 }
3288
3289 static u16 dpaa_get_headroom(struct dpaa_buffer_layout *bl,
3290                              enum port_type port)
3291 {
3292         u16 headroom;
3293
3294         /* The frame headroom must accommodate:
3295          * - the driver private data area
3296          * - parse results, hash results, timestamp if selected
3297          * If either hash results or time stamp are selected, both will
3298          * be copied to/from the frame headroom, as TS is located between PR and
3299          * HR in the IC and IC copy size has a granularity of 16bytes
3300          * (see description of FMBM_RICP and FMBM_TICP registers in DPAARM)
3301          *
3302          * Also make sure the headroom is a multiple of data_align bytes
3303          */
3304         headroom = (u16)(bl[port].priv_data_size + DPAA_HWA_SIZE);
3305
3306         if (port == RX) {
3307 #ifdef CONFIG_DPAA_ERRATUM_A050385
3308                 if (unlikely(fman_has_errata_a050385()))
3309                         headroom = XDP_PACKET_HEADROOM;
3310 #endif
3311
3312                 return ALIGN(headroom, DPAA_FD_RX_DATA_ALIGNMENT);
3313         } else {
3314                 return ALIGN(headroom, DPAA_FD_DATA_ALIGNMENT);
3315         }
3316 }
3317
3318 static int dpaa_eth_probe(struct platform_device *pdev)
3319 {
3320         struct net_device *net_dev = NULL;
3321         struct dpaa_bp *dpaa_bp = NULL;
3322         struct dpaa_fq *dpaa_fq, *tmp;
3323         struct dpaa_priv *priv = NULL;
3324         struct fm_port_fqs port_fqs;
3325         struct mac_device *mac_dev;
3326         int err = 0, channel;
3327         struct device *dev;
3328
3329         dev = &pdev->dev;
3330
3331         err = bman_is_probed();
3332         if (!err)
3333                 return -EPROBE_DEFER;
3334         if (err < 0) {
3335                 dev_err(dev, "failing probe due to bman probe error\n");
3336                 return -ENODEV;
3337         }
3338         err = qman_is_probed();
3339         if (!err)
3340                 return -EPROBE_DEFER;
3341         if (err < 0) {
3342                 dev_err(dev, "failing probe due to qman probe error\n");
3343                 return -ENODEV;
3344         }
3345         err = bman_portals_probed();
3346         if (!err)
3347                 return -EPROBE_DEFER;
3348         if (err < 0) {
3349                 dev_err(dev,
3350                         "failing probe due to bman portals probe error\n");
3351                 return -ENODEV;
3352         }
3353         err = qman_portals_probed();
3354         if (!err)
3355                 return -EPROBE_DEFER;
3356         if (err < 0) {
3357                 dev_err(dev,
3358                         "failing probe due to qman portals probe error\n");
3359                 return -ENODEV;
3360         }
3361
3362         /* Allocate this early, so we can store relevant information in
3363          * the private area
3364          */
3365         net_dev = alloc_etherdev_mq(sizeof(*priv), DPAA_ETH_TXQ_NUM);
3366         if (!net_dev) {
3367                 dev_err(dev, "alloc_etherdev_mq() failed\n");
3368                 return -ENOMEM;
3369         }
3370
3371         /* Do this here, so we can be verbose early */
3372         SET_NETDEV_DEV(net_dev, dev->parent);
3373         dev_set_drvdata(dev, net_dev);
3374
3375         priv = netdev_priv(net_dev);
3376         priv->net_dev = net_dev;
3377
3378         priv->msg_enable = netif_msg_init(debug, DPAA_MSG_DEFAULT);
3379
3380         mac_dev = dpaa_mac_dev_get(pdev);
3381         if (IS_ERR(mac_dev)) {
3382                 netdev_err(net_dev, "dpaa_mac_dev_get() failed\n");
3383                 err = PTR_ERR(mac_dev);
3384                 goto free_netdev;
3385         }
3386
3387         /* Devices used for DMA mapping */
3388         priv->rx_dma_dev = fman_port_get_device(mac_dev->port[RX]);
3389         priv->tx_dma_dev = fman_port_get_device(mac_dev->port[TX]);
3390         err = dma_coerce_mask_and_coherent(priv->rx_dma_dev, DMA_BIT_MASK(40));
3391         if (!err)
3392                 err = dma_coerce_mask_and_coherent(priv->tx_dma_dev,
3393                                                    DMA_BIT_MASK(40));
3394         if (err) {
3395                 netdev_err(net_dev, "dma_coerce_mask_and_coherent() failed\n");
3396                 goto free_netdev;
3397         }
3398
3399         /* If fsl_fm_max_frm is set to a higher value than the all-common 1500,
3400          * we choose conservatively and let the user explicitly set a higher
3401          * MTU via ifconfig. Otherwise, the user may end up with different MTUs
3402          * in the same LAN.
3403          * If on the other hand fsl_fm_max_frm has been chosen below 1500,
3404          * start with the maximum allowed.
3405          */
3406         net_dev->mtu = min(dpaa_get_max_mtu(), ETH_DATA_LEN);
3407
3408         netdev_dbg(net_dev, "Setting initial MTU on net device: %d\n",
3409                    net_dev->mtu);
3410
3411         priv->buf_layout[RX].priv_data_size = DPAA_RX_PRIV_DATA_SIZE; /* Rx */
3412         priv->buf_layout[TX].priv_data_size = DPAA_TX_PRIV_DATA_SIZE; /* Tx */
3413
3414         /* bp init */
3415         dpaa_bp = dpaa_bp_alloc(dev);
3416         if (IS_ERR(dpaa_bp)) {
3417                 err = PTR_ERR(dpaa_bp);
3418                 goto free_dpaa_bps;
3419         }
3420         /* the raw size of the buffers used for reception */
3421         dpaa_bp->raw_size = DPAA_BP_RAW_SIZE;
3422         /* avoid runtime computations by keeping the usable size here */
3423         dpaa_bp->size = dpaa_bp_size(dpaa_bp->raw_size);
3424         dpaa_bp->priv = priv;
3425
3426         err = dpaa_bp_alloc_pool(dpaa_bp);
3427         if (err < 0)
3428                 goto free_dpaa_bps;
3429         priv->dpaa_bp = dpaa_bp;
3430
3431         INIT_LIST_HEAD(&priv->dpaa_fq_list);
3432
3433         memset(&port_fqs, 0, sizeof(port_fqs));
3434
3435         err = dpaa_alloc_all_fqs(dev, &priv->dpaa_fq_list, &port_fqs);
3436         if (err < 0) {
3437                 dev_err(dev, "dpaa_alloc_all_fqs() failed\n");
3438                 goto free_dpaa_bps;
3439         }
3440
3441         priv->mac_dev = mac_dev;
3442
3443         channel = dpaa_get_channel();
3444         if (channel < 0) {
3445                 dev_err(dev, "dpaa_get_channel() failed\n");
3446                 err = channel;
3447                 goto free_dpaa_bps;
3448         }
3449
3450         priv->channel = (u16)channel;
3451
3452         /* Walk the CPUs with affine portals
3453          * and add this pool channel to each's dequeue mask.
3454          */
3455         dpaa_eth_add_channel(priv->channel, &pdev->dev);
3456
3457         dpaa_fq_setup(priv, &dpaa_fq_cbs, priv->mac_dev->port[TX]);
3458
3459         /* Create a congestion group for this netdev, with
3460          * dynamically-allocated CGR ID.
3461          * Must be executed after probing the MAC, but before
3462          * assigning the egress FQs to the CGRs.
3463          */
3464         err = dpaa_eth_cgr_init(priv);
3465         if (err < 0) {
3466                 dev_err(dev, "Error initializing CGR\n");
3467                 goto free_dpaa_bps;
3468         }
3469
3470         err = dpaa_ingress_cgr_init(priv);
3471         if (err < 0) {
3472                 dev_err(dev, "Error initializing ingress CGR\n");
3473                 goto delete_egress_cgr;
3474         }
3475
3476         /* Add the FQs to the interface, and make them active */
3477         list_for_each_entry_safe(dpaa_fq, tmp, &priv->dpaa_fq_list, list) {
3478                 err = dpaa_fq_init(dpaa_fq, false);
3479                 if (err < 0)
3480                         goto free_dpaa_fqs;
3481         }
3482
3483         priv->tx_headroom = dpaa_get_headroom(priv->buf_layout, TX);
3484         priv->rx_headroom = dpaa_get_headroom(priv->buf_layout, RX);
3485
3486         /* All real interfaces need their ports initialized */
3487         err = dpaa_eth_init_ports(mac_dev, dpaa_bp, &port_fqs,
3488                                   &priv->buf_layout[0], dev);
3489         if (err)
3490                 goto free_dpaa_fqs;
3491
3492         /* Rx traffic distribution based on keygen hashing defaults to on */
3493         priv->keygen_in_use = true;
3494
3495         priv->percpu_priv = devm_alloc_percpu(dev, *priv->percpu_priv);
3496         if (!priv->percpu_priv) {
3497                 dev_err(dev, "devm_alloc_percpu() failed\n");
3498                 err = -ENOMEM;
3499                 goto free_dpaa_fqs;
3500         }
3501
3502         priv->num_tc = 1;
3503         netif_set_real_num_tx_queues(net_dev, priv->num_tc * DPAA_TC_TXQ_NUM);
3504
3505         /* Initialize NAPI */
3506         err = dpaa_napi_add(net_dev);
3507         if (err < 0)
3508                 goto delete_dpaa_napi;
3509
3510         err = dpaa_netdev_init(net_dev, &dpaa_ops, tx_timeout);
3511         if (err < 0)
3512                 goto delete_dpaa_napi;
3513
3514         dpaa_eth_sysfs_init(&net_dev->dev);
3515
3516         netif_info(priv, probe, net_dev, "Probed interface %s\n",
3517                    net_dev->name);
3518
3519         return 0;
3520
3521 delete_dpaa_napi:
3522         dpaa_napi_del(net_dev);
3523 free_dpaa_fqs:
3524         dpaa_fq_free(dev, &priv->dpaa_fq_list);
3525         qman_delete_cgr_safe(&priv->ingress_cgr);
3526         qman_release_cgrid(priv->ingress_cgr.cgrid);
3527 delete_egress_cgr:
3528         qman_delete_cgr_safe(&priv->cgr_data.cgr);
3529         qman_release_cgrid(priv->cgr_data.cgr.cgrid);
3530 free_dpaa_bps:
3531         dpaa_bps_free(priv);
3532 free_netdev:
3533         dev_set_drvdata(dev, NULL);
3534         free_netdev(net_dev);
3535
3536         return err;
3537 }
3538
3539 static int dpaa_remove(struct platform_device *pdev)
3540 {
3541         struct net_device *net_dev;
3542         struct dpaa_priv *priv;
3543         struct device *dev;
3544         int err;
3545
3546         dev = &pdev->dev;
3547         net_dev = dev_get_drvdata(dev);
3548
3549         priv = netdev_priv(net_dev);
3550
3551         dpaa_eth_sysfs_remove(dev);
3552
3553         dev_set_drvdata(dev, NULL);
3554         unregister_netdev(net_dev);
3555
3556         err = dpaa_fq_free(dev, &priv->dpaa_fq_list);
3557
3558         qman_delete_cgr_safe(&priv->ingress_cgr);
3559         qman_release_cgrid(priv->ingress_cgr.cgrid);
3560         qman_delete_cgr_safe(&priv->cgr_data.cgr);
3561         qman_release_cgrid(priv->cgr_data.cgr.cgrid);
3562
3563         dpaa_napi_del(net_dev);
3564
3565         dpaa_bps_free(priv);
3566
3567         free_netdev(net_dev);
3568
3569         return err;
3570 }
3571
3572 static const struct platform_device_id dpaa_devtype[] = {
3573         {
3574                 .name = "dpaa-ethernet",
3575                 .driver_data = 0,
3576         }, {
3577         }
3578 };
3579 MODULE_DEVICE_TABLE(platform, dpaa_devtype);
3580
3581 static struct platform_driver dpaa_driver = {
3582         .driver = {
3583                 .name = KBUILD_MODNAME,
3584         },
3585         .id_table = dpaa_devtype,
3586         .probe = dpaa_eth_probe,
3587         .remove = dpaa_remove
3588 };
3589
3590 static int __init dpaa_load(void)
3591 {
3592         int err;
3593
3594         pr_debug("FSL DPAA Ethernet driver\n");
3595
3596         /* initialize dpaa_eth mirror values */
3597         dpaa_rx_extra_headroom = fman_get_rx_extra_headroom();
3598         dpaa_max_frm = fman_get_max_frm();
3599
3600         err = platform_driver_register(&dpaa_driver);
3601         if (err < 0)
3602                 pr_err("Error, platform_driver_register() = %d\n", err);
3603
3604         return err;
3605 }
3606 module_init(dpaa_load);
3607
3608 static void __exit dpaa_unload(void)
3609 {
3610         platform_driver_unregister(&dpaa_driver);
3611
3612         /* Only one channel is used and needs to be released after all
3613          * interfaces are removed
3614          */
3615         dpaa_release_channel();
3616 }
3617 module_exit(dpaa_unload);
3618
3619 MODULE_LICENSE("Dual BSD/GPL");
3620 MODULE_DESCRIPTION("FSL DPAA Ethernet driver");