345c24404de4fd58fc73a31794002ce27a4ecdb7
[dpdk.git] / drivers / net / mvpp2 / mrvl_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Marvell International Ltd.
3  * Copyright(c) 2017 Semihalf.
4  * All rights reserved.
5  */
6
7 #include <rte_string_fns.h>
8 #include <rte_ethdev_driver.h>
9 #include <rte_kvargs.h>
10 #include <rte_log.h>
11 #include <rte_malloc.h>
12 #include <rte_bus_vdev.h>
13
14 #include <fcntl.h>
15 #include <linux/ethtool.h>
16 #include <linux/sockios.h>
17 #include <net/if.h>
18 #include <net/if_arp.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #include <rte_mvep_common.h>
25 #include "mrvl_ethdev.h"
26 #include "mrvl_qos.h"
27 #include "mrvl_flow.h"
28 #include "mrvl_mtr.h"
29 #include "mrvl_tm.h"
30
31 /* bitmask with reserved hifs */
32 #define MRVL_MUSDK_HIFS_RESERVED 0x0F
33 /* bitmask with reserved bpools */
34 #define MRVL_MUSDK_BPOOLS_RESERVED 0x07
35 /* bitmask with reserved kernel RSS tables */
36 #define MRVL_MUSDK_RSS_RESERVED 0x01
37 /* maximum number of available hifs */
38 #define MRVL_MUSDK_HIFS_MAX 9
39
40 /* prefetch shift */
41 #define MRVL_MUSDK_PREFETCH_SHIFT 2
42
43 /* TCAM has 25 entries reserved for uc/mc filter entries */
44 #define MRVL_MAC_ADDRS_MAX 25
45 #define MRVL_MATCH_LEN 16
46 #define MRVL_PKT_EFFEC_OFFS (MRVL_PKT_OFFS + MV_MH_SIZE)
47 /* Maximum allowable packet size */
48 #define MRVL_PKT_SIZE_MAX (10240 - MV_MH_SIZE)
49
50 #define MRVL_IFACE_NAME_ARG "iface"
51 #define MRVL_CFG_ARG "cfg"
52
53 #define MRVL_BURST_SIZE 64
54
55 #define MRVL_ARP_LENGTH 28
56
57 #define MRVL_COOKIE_ADDR_INVALID ~0ULL
58 #define MRVL_COOKIE_HIGH_ADDR_MASK 0xffffff0000000000
59
60 /** Port Rx offload capabilities */
61 #define MRVL_RX_OFFLOADS (DEV_RX_OFFLOAD_VLAN_FILTER | \
62                           DEV_RX_OFFLOAD_JUMBO_FRAME | \
63                           DEV_RX_OFFLOAD_CHECKSUM)
64
65 /** Port Tx offloads capabilities */
66 #define MRVL_TX_OFFLOADS (DEV_TX_OFFLOAD_IPV4_CKSUM | \
67                           DEV_TX_OFFLOAD_UDP_CKSUM | \
68                           DEV_TX_OFFLOAD_TCP_CKSUM | \
69                           DEV_TX_OFFLOAD_MULTI_SEGS)
70
71 static const char * const valid_args[] = {
72         MRVL_IFACE_NAME_ARG,
73         MRVL_CFG_ARG,
74         NULL
75 };
76
77 static int used_hifs = MRVL_MUSDK_HIFS_RESERVED;
78 static struct pp2_hif *hifs[RTE_MAX_LCORE];
79 static int used_bpools[PP2_NUM_PKT_PROC] = {
80         [0 ... PP2_NUM_PKT_PROC - 1] = MRVL_MUSDK_BPOOLS_RESERVED
81 };
82
83 static struct pp2_bpool *mrvl_port_to_bpool_lookup[RTE_MAX_ETHPORTS];
84 static int mrvl_port_bpool_size[PP2_NUM_PKT_PROC][PP2_BPOOL_NUM_POOLS][RTE_MAX_LCORE];
85 static uint64_t cookie_addr_high = MRVL_COOKIE_ADDR_INVALID;
86
87 int mrvl_logtype;
88
89 struct mrvl_ifnames {
90         const char *names[PP2_NUM_ETH_PPIO * PP2_NUM_PKT_PROC];
91         int idx;
92 };
93
94 /*
95  * To use buffer harvesting based on loopback port shadow queue structure
96  * was introduced for buffers information bookkeeping.
97  *
98  * Before sending the packet, related buffer information (pp2_buff_inf) is
99  * stored in shadow queue. After packet is transmitted no longer used
100  * packet buffer is released back to it's original hardware pool,
101  * on condition it originated from interface.
102  * In case it  was generated by application itself i.e: mbuf->port field is
103  * 0xff then its released to software mempool.
104  */
105 struct mrvl_shadow_txq {
106         int head;           /* write index - used when sending buffers */
107         int tail;           /* read index - used when releasing buffers */
108         u16 size;           /* queue occupied size */
109         u16 num_to_release; /* number of descriptors sent, that can be
110                              * released
111                              */
112         struct buff_release_entry ent[MRVL_PP2_TX_SHADOWQ_SIZE]; /* q entries */
113 };
114
115 struct mrvl_rxq {
116         struct mrvl_priv *priv;
117         struct rte_mempool *mp;
118         int queue_id;
119         int port_id;
120         int cksum_enabled;
121         uint64_t bytes_recv;
122         uint64_t drop_mac;
123 };
124
125 struct mrvl_txq {
126         struct mrvl_priv *priv;
127         int queue_id;
128         int port_id;
129         uint64_t bytes_sent;
130         struct mrvl_shadow_txq shadow_txqs[RTE_MAX_LCORE];
131         int tx_deferred_start;
132 };
133
134 static int mrvl_lcore_first;
135 static int mrvl_lcore_last;
136 static int mrvl_dev_num;
137
138 static int mrvl_fill_bpool(struct mrvl_rxq *rxq, int num);
139 static inline void mrvl_free_sent_buffers(struct pp2_ppio *ppio,
140                         struct pp2_hif *hif, unsigned int core_id,
141                         struct mrvl_shadow_txq *sq, int qid, int force);
142
143 static uint16_t mrvl_tx_pkt_burst(void *txq, struct rte_mbuf **tx_pkts,
144                                   uint16_t nb_pkts);
145 static uint16_t mrvl_tx_sg_pkt_burst(void *txq, struct rte_mbuf **tx_pkts,
146                                      uint16_t nb_pkts);
147 static int rte_pmd_mrvl_remove(struct rte_vdev_device *vdev);
148 static void mrvl_deinit_pp2(void);
149 static void mrvl_deinit_hifs(void);
150
151
152 #define MRVL_XSTATS_TBL_ENTRY(name) { \
153         #name, offsetof(struct pp2_ppio_statistics, name),      \
154         sizeof(((struct pp2_ppio_statistics *)0)->name)         \
155 }
156
157 /* Table with xstats data */
158 static struct {
159         const char *name;
160         unsigned int offset;
161         unsigned int size;
162 } mrvl_xstats_tbl[] = {
163         MRVL_XSTATS_TBL_ENTRY(rx_bytes),
164         MRVL_XSTATS_TBL_ENTRY(rx_packets),
165         MRVL_XSTATS_TBL_ENTRY(rx_unicast_packets),
166         MRVL_XSTATS_TBL_ENTRY(rx_errors),
167         MRVL_XSTATS_TBL_ENTRY(rx_fullq_dropped),
168         MRVL_XSTATS_TBL_ENTRY(rx_bm_dropped),
169         MRVL_XSTATS_TBL_ENTRY(rx_early_dropped),
170         MRVL_XSTATS_TBL_ENTRY(rx_fifo_dropped),
171         MRVL_XSTATS_TBL_ENTRY(rx_cls_dropped),
172         MRVL_XSTATS_TBL_ENTRY(tx_bytes),
173         MRVL_XSTATS_TBL_ENTRY(tx_packets),
174         MRVL_XSTATS_TBL_ENTRY(tx_unicast_packets),
175         MRVL_XSTATS_TBL_ENTRY(tx_errors)
176 };
177
178 static inline void
179 mrvl_fill_shadowq(struct mrvl_shadow_txq *sq, struct rte_mbuf *buf)
180 {
181         sq->ent[sq->head].buff.cookie = (uint64_t)buf;
182         sq->ent[sq->head].buff.addr = buf ?
183                 rte_mbuf_data_iova_default(buf) : 0;
184
185         sq->ent[sq->head].bpool =
186                 (unlikely(!buf || buf->port >= RTE_MAX_ETHPORTS ||
187                  buf->refcnt > 1)) ? NULL :
188                  mrvl_port_to_bpool_lookup[buf->port];
189
190         sq->head = (sq->head + 1) & MRVL_PP2_TX_SHADOWQ_MASK;
191         sq->size++;
192 }
193
194 static inline void
195 mrvl_fill_desc(struct pp2_ppio_desc *desc, struct rte_mbuf *buf)
196 {
197         pp2_ppio_outq_desc_reset(desc);
198         pp2_ppio_outq_desc_set_phys_addr(desc, rte_pktmbuf_iova(buf));
199         pp2_ppio_outq_desc_set_pkt_offset(desc, 0);
200         pp2_ppio_outq_desc_set_pkt_len(desc, rte_pktmbuf_data_len(buf));
201 }
202
203 static inline int
204 mrvl_get_bpool_size(int pp2_id, int pool_id)
205 {
206         int i;
207         int size = 0;
208
209         for (i = mrvl_lcore_first; i <= mrvl_lcore_last; i++)
210                 size += mrvl_port_bpool_size[pp2_id][pool_id][i];
211
212         return size;
213 }
214
215 static inline int
216 mrvl_reserve_bit(int *bitmap, int max)
217 {
218         int n = sizeof(*bitmap) * 8 - __builtin_clz(*bitmap);
219
220         if (n >= max)
221                 return -1;
222
223         *bitmap |= 1 << n;
224
225         return n;
226 }
227
228 static int
229 mrvl_init_hif(int core_id)
230 {
231         struct pp2_hif_params params;
232         char match[MRVL_MATCH_LEN];
233         int ret;
234
235         ret = mrvl_reserve_bit(&used_hifs, MRVL_MUSDK_HIFS_MAX);
236         if (ret < 0) {
237                 MRVL_LOG(ERR, "Failed to allocate hif %d", core_id);
238                 return ret;
239         }
240
241         snprintf(match, sizeof(match), "hif-%d", ret);
242         memset(&params, 0, sizeof(params));
243         params.match = match;
244         params.out_size = MRVL_PP2_AGGR_TXQD_MAX;
245         ret = pp2_hif_init(&params, &hifs[core_id]);
246         if (ret) {
247                 MRVL_LOG(ERR, "Failed to initialize hif %d", core_id);
248                 return ret;
249         }
250
251         return 0;
252 }
253
254 static inline struct pp2_hif*
255 mrvl_get_hif(struct mrvl_priv *priv, int core_id)
256 {
257         int ret;
258
259         if (likely(hifs[core_id] != NULL))
260                 return hifs[core_id];
261
262         rte_spinlock_lock(&priv->lock);
263
264         ret = mrvl_init_hif(core_id);
265         if (ret < 0) {
266                 MRVL_LOG(ERR, "Failed to allocate hif %d", core_id);
267                 goto out;
268         }
269
270         if (core_id < mrvl_lcore_first)
271                 mrvl_lcore_first = core_id;
272
273         if (core_id > mrvl_lcore_last)
274                 mrvl_lcore_last = core_id;
275 out:
276         rte_spinlock_unlock(&priv->lock);
277
278         return hifs[core_id];
279 }
280
281 /**
282  * Set tx burst function according to offload flag
283  *
284  * @param dev
285  *   Pointer to Ethernet device structure.
286  */
287 static void
288 mrvl_set_tx_function(struct rte_eth_dev *dev)
289 {
290         struct mrvl_priv *priv = dev->data->dev_private;
291
292         /* Use a simple Tx queue (no offloads, no multi segs) if possible */
293         if (priv->multiseg) {
294                 RTE_LOG(INFO, PMD, "Using multi-segment tx callback\n");
295                 dev->tx_pkt_burst = mrvl_tx_sg_pkt_burst;
296         } else {
297                 RTE_LOG(INFO, PMD, "Using single-segment tx callback\n");
298                 dev->tx_pkt_burst = mrvl_tx_pkt_burst;
299         }
300 }
301
302 /**
303  * Configure rss based on dpdk rss configuration.
304  *
305  * @param priv
306  *   Pointer to private structure.
307  * @param rss_conf
308  *   Pointer to RSS configuration.
309  *
310  * @return
311  *   0 on success, negative error value otherwise.
312  */
313 static int
314 mrvl_configure_rss(struct mrvl_priv *priv, struct rte_eth_rss_conf *rss_conf)
315 {
316         if (rss_conf->rss_key)
317                 MRVL_LOG(WARNING, "Changing hash key is not supported");
318
319         if (rss_conf->rss_hf == 0) {
320                 priv->ppio_params.inqs_params.hash_type = PP2_PPIO_HASH_T_NONE;
321         } else if (rss_conf->rss_hf & ETH_RSS_IPV4) {
322                 priv->ppio_params.inqs_params.hash_type =
323                         PP2_PPIO_HASH_T_2_TUPLE;
324         } else if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP) {
325                 priv->ppio_params.inqs_params.hash_type =
326                         PP2_PPIO_HASH_T_5_TUPLE;
327                 priv->rss_hf_tcp = 1;
328         } else if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_UDP) {
329                 priv->ppio_params.inqs_params.hash_type =
330                         PP2_PPIO_HASH_T_5_TUPLE;
331                 priv->rss_hf_tcp = 0;
332         } else {
333                 return -EINVAL;
334         }
335
336         return 0;
337 }
338
339 /**
340  * Ethernet device configuration.
341  *
342  * Prepare the driver for a given number of TX and RX queues and
343  * configure RSS.
344  *
345  * @param dev
346  *   Pointer to Ethernet device structure.
347  *
348  * @return
349  *   0 on success, negative error value otherwise.
350  */
351 static int
352 mrvl_dev_configure(struct rte_eth_dev *dev)
353 {
354         struct mrvl_priv *priv = dev->data->dev_private;
355         int ret;
356
357         if (priv->ppio) {
358                 MRVL_LOG(INFO, "Device reconfiguration is not supported");
359                 return -EINVAL;
360         }
361
362         if (dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_NONE &&
363             dev->data->dev_conf.rxmode.mq_mode != ETH_MQ_RX_RSS) {
364                 MRVL_LOG(INFO, "Unsupported rx multi queue mode %d",
365                         dev->data->dev_conf.rxmode.mq_mode);
366                 return -EINVAL;
367         }
368
369         if (dev->data->dev_conf.rxmode.split_hdr_size) {
370                 MRVL_LOG(INFO, "Split headers not supported");
371                 return -EINVAL;
372         }
373
374         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
375                 dev->data->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len -
376                                  MRVL_PP2_ETH_HDRS_LEN;
377
378         if (dev->data->dev_conf.txmode.offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
379                 priv->multiseg = 1;
380
381         ret = mrvl_configure_rxqs(priv, dev->data->port_id,
382                                   dev->data->nb_rx_queues);
383         if (ret < 0)
384                 return ret;
385
386         ret = mrvl_configure_txqs(priv, dev->data->port_id,
387                                   dev->data->nb_tx_queues);
388         if (ret < 0)
389                 return ret;
390
391         priv->ppio_params.outqs_params.num_outqs = dev->data->nb_tx_queues;
392         priv->ppio_params.maintain_stats = 1;
393         priv->nb_rx_queues = dev->data->nb_rx_queues;
394
395         ret = mrvl_tm_init(dev);
396         if (ret < 0)
397                 return ret;
398
399         if (dev->data->nb_rx_queues == 1 &&
400             dev->data->dev_conf.rxmode.mq_mode == ETH_MQ_RX_RSS) {
401                 MRVL_LOG(WARNING, "Disabling hash for 1 rx queue");
402                 priv->ppio_params.inqs_params.hash_type = PP2_PPIO_HASH_T_NONE;
403
404                 return 0;
405         }
406
407         return mrvl_configure_rss(priv,
408                                   &dev->data->dev_conf.rx_adv_conf.rss_conf);
409 }
410
411 /**
412  * DPDK callback to change the MTU.
413  *
414  * Setting the MTU affects hardware MRU (packets larger than the MRU
415  * will be dropped).
416  *
417  * @param dev
418  *   Pointer to Ethernet device structure.
419  * @param mtu
420  *   New MTU.
421  *
422  * @return
423  *   0 on success, negative error value otherwise.
424  */
425 static int
426 mrvl_mtu_set(struct rte_eth_dev *dev, uint16_t mtu)
427 {
428         struct mrvl_priv *priv = dev->data->dev_private;
429         uint16_t mru;
430         uint16_t mbuf_data_size = 0; /* SW buffer size */
431         int ret;
432
433         mru = MRVL_PP2_MTU_TO_MRU(mtu);
434         /*
435          * min_rx_buf_size is equal to mbuf data size
436          * if pmd didn't set it differently
437          */
438         mbuf_data_size = dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM;
439         /* Prevent PMD from:
440          * - setting mru greater than the mbuf size resulting in
441          * hw and sw buffer size mismatch
442          * - setting mtu that requires the support of scattered packets
443          * when this feature has not been enabled/supported so far
444          * (TODO check scattered_rx flag here once scattered RX is supported).
445          */
446         if (mru + MRVL_PKT_OFFS > mbuf_data_size) {
447                 mru = mbuf_data_size - MRVL_PKT_OFFS;
448                 mtu = MRVL_PP2_MRU_TO_MTU(mru);
449                 MRVL_LOG(WARNING, "MTU too big, max MTU possible limitted "
450                         "by current mbuf size: %u. Set MTU to %u, MRU to %u",
451                         mbuf_data_size, mtu, mru);
452         }
453
454         if (mtu < RTE_ETHER_MIN_MTU || mru > MRVL_PKT_SIZE_MAX) {
455                 MRVL_LOG(ERR, "Invalid MTU [%u] or MRU [%u]", mtu, mru);
456                 return -EINVAL;
457         }
458
459         dev->data->mtu = mtu;
460         dev->data->dev_conf.rxmode.max_rx_pkt_len = mru - MV_MH_SIZE;
461
462         if (!priv->ppio)
463                 return 0;
464
465         ret = pp2_ppio_set_mru(priv->ppio, mru);
466         if (ret) {
467                 MRVL_LOG(ERR, "Failed to change MRU");
468                 return ret;
469         }
470
471         ret = pp2_ppio_set_mtu(priv->ppio, mtu);
472         if (ret) {
473                 MRVL_LOG(ERR, "Failed to change MTU");
474                 return ret;
475         }
476
477         return 0;
478 }
479
480 /**
481  * DPDK callback to bring the link up.
482  *
483  * @param dev
484  *   Pointer to Ethernet device structure.
485  *
486  * @return
487  *   0 on success, negative error value otherwise.
488  */
489 static int
490 mrvl_dev_set_link_up(struct rte_eth_dev *dev)
491 {
492         struct mrvl_priv *priv = dev->data->dev_private;
493         int ret;
494
495         if (!priv->ppio)
496                 return -EPERM;
497
498         ret = pp2_ppio_enable(priv->ppio);
499         if (ret)
500                 return ret;
501
502         /*
503          * mtu/mru can be updated if pp2_ppio_enable() was called at least once
504          * as pp2_ppio_enable() changes port->t_mode from default 0 to
505          * PP2_TRAFFIC_INGRESS_EGRESS.
506          *
507          * Set mtu to default DPDK value here.
508          */
509         ret = mrvl_mtu_set(dev, dev->data->mtu);
510         if (ret)
511                 pp2_ppio_disable(priv->ppio);
512
513         return ret;
514 }
515
516 /**
517  * DPDK callback to bring the link down.
518  *
519  * @param dev
520  *   Pointer to Ethernet device structure.
521  *
522  * @return
523  *   0 on success, negative error value otherwise.
524  */
525 static int
526 mrvl_dev_set_link_down(struct rte_eth_dev *dev)
527 {
528         struct mrvl_priv *priv = dev->data->dev_private;
529
530         if (!priv->ppio)
531                 return -EPERM;
532
533         return pp2_ppio_disable(priv->ppio);
534 }
535
536 /**
537  * DPDK callback to start tx queue.
538  *
539  * @param dev
540  *   Pointer to Ethernet device structure.
541  * @param queue_id
542  *   Transmit queue index.
543  *
544  * @return
545  *   0 on success, negative error value otherwise.
546  */
547 static int
548 mrvl_tx_queue_start(struct rte_eth_dev *dev, uint16_t queue_id)
549 {
550         struct mrvl_priv *priv = dev->data->dev_private;
551         int ret;
552
553         if (!priv)
554                 return -EPERM;
555
556         /* passing 1 enables given tx queue */
557         ret = pp2_ppio_set_outq_state(priv->ppio, queue_id, 1);
558         if (ret) {
559                 MRVL_LOG(ERR, "Failed to start txq %d", queue_id);
560                 return ret;
561         }
562
563         dev->data->tx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
564
565         return 0;
566 }
567
568 /**
569  * DPDK callback to stop tx queue.
570  *
571  * @param dev
572  *   Pointer to Ethernet device structure.
573  * @param queue_id
574  *   Transmit queue index.
575  *
576  * @return
577  *   0 on success, negative error value otherwise.
578  */
579 static int
580 mrvl_tx_queue_stop(struct rte_eth_dev *dev, uint16_t queue_id)
581 {
582         struct mrvl_priv *priv = dev->data->dev_private;
583         int ret;
584
585         if (!priv->ppio)
586                 return -EPERM;
587
588         /* passing 0 disables given tx queue */
589         ret = pp2_ppio_set_outq_state(priv->ppio, queue_id, 0);
590         if (ret) {
591                 MRVL_LOG(ERR, "Failed to stop txq %d", queue_id);
592                 return ret;
593         }
594
595         dev->data->tx_queue_state[queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
596
597         return 0;
598 }
599
600 /**
601  * DPDK callback to start the device.
602  *
603  * @param dev
604  *   Pointer to Ethernet device structure.
605  *
606  * @return
607  *   0 on success, negative errno value on failure.
608  */
609 static int
610 mrvl_dev_start(struct rte_eth_dev *dev)
611 {
612         struct mrvl_priv *priv = dev->data->dev_private;
613         char match[MRVL_MATCH_LEN];
614         int ret = 0, i, def_init_size;
615
616         if (priv->ppio)
617                 return mrvl_dev_set_link_up(dev);
618
619         snprintf(match, sizeof(match), "ppio-%d:%d",
620                  priv->pp_id, priv->ppio_id);
621         priv->ppio_params.match = match;
622
623         /*
624          * Calculate the minimum bpool size for refill feature as follows:
625          * 2 default burst sizes multiply by number of rx queues.
626          * If the bpool size will be below this value, new buffers will
627          * be added to the pool.
628          */
629         priv->bpool_min_size = priv->nb_rx_queues * MRVL_BURST_SIZE * 2;
630
631         /* In case initial bpool size configured in queues setup is
632          * smaller than minimum size add more buffers
633          */
634         def_init_size = priv->bpool_min_size + MRVL_BURST_SIZE * 2;
635         if (priv->bpool_init_size < def_init_size) {
636                 int buffs_to_add = def_init_size - priv->bpool_init_size;
637
638                 priv->bpool_init_size += buffs_to_add;
639                 ret = mrvl_fill_bpool(dev->data->rx_queues[0], buffs_to_add);
640                 if (ret)
641                         MRVL_LOG(ERR, "Failed to add buffers to bpool");
642         }
643
644         /*
645          * Calculate the maximum bpool size for refill feature as follows:
646          * maximum number of descriptors in rx queue multiply by number
647          * of rx queues plus minimum bpool size.
648          * In case the bpool size will exceed this value, superfluous buffers
649          * will be removed
650          */
651         priv->bpool_max_size = (priv->nb_rx_queues * MRVL_PP2_RXD_MAX) +
652                                 priv->bpool_min_size;
653
654         ret = pp2_ppio_init(&priv->ppio_params, &priv->ppio);
655         if (ret) {
656                 MRVL_LOG(ERR, "Failed to init ppio");
657                 return ret;
658         }
659
660         /*
661          * In case there are some some stale uc/mc mac addresses flush them
662          * here. It cannot be done during mrvl_dev_close() as port information
663          * is already gone at that point (due to pp2_ppio_deinit() in
664          * mrvl_dev_stop()).
665          */
666         if (!priv->uc_mc_flushed) {
667                 ret = pp2_ppio_flush_mac_addrs(priv->ppio, 1, 1);
668                 if (ret) {
669                         MRVL_LOG(ERR,
670                                 "Failed to flush uc/mc filter list");
671                         goto out;
672                 }
673                 priv->uc_mc_flushed = 1;
674         }
675
676         if (!priv->vlan_flushed) {
677                 ret = pp2_ppio_flush_vlan(priv->ppio);
678                 if (ret) {
679                         MRVL_LOG(ERR, "Failed to flush vlan list");
680                         /*
681                          * TODO
682                          * once pp2_ppio_flush_vlan() is supported jump to out
683                          * goto out;
684                          */
685                 }
686                 priv->vlan_flushed = 1;
687         }
688         ret = mrvl_mtu_set(dev, dev->data->mtu);
689         if (ret)
690                 MRVL_LOG(ERR, "Failed to set MTU to %d", dev->data->mtu);
691
692         /* For default QoS config, don't start classifier. */
693         if (mrvl_qos_cfg  &&
694             mrvl_qos_cfg->port[dev->data->port_id].use_global_defaults == 0) {
695                 ret = mrvl_start_qos_mapping(priv);
696                 if (ret) {
697                         MRVL_LOG(ERR, "Failed to setup QoS mapping");
698                         goto out;
699                 }
700         }
701
702         ret = mrvl_dev_set_link_up(dev);
703         if (ret) {
704                 MRVL_LOG(ERR, "Failed to set link up");
705                 goto out;
706         }
707
708         /* start tx queues */
709         for (i = 0; i < dev->data->nb_tx_queues; i++) {
710                 struct mrvl_txq *txq = dev->data->tx_queues[i];
711
712                 dev->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STARTED;
713
714                 if (!txq->tx_deferred_start)
715                         continue;
716
717                 /*
718                  * All txqs are started by default. Stop them
719                  * so that tx_deferred_start works as expected.
720                  */
721                 ret = mrvl_tx_queue_stop(dev, i);
722                 if (ret)
723                         goto out;
724         }
725
726         mrvl_flow_init(dev);
727         mrvl_mtr_init(dev);
728         mrvl_set_tx_function(dev);
729
730         return 0;
731 out:
732         MRVL_LOG(ERR, "Failed to start device");
733         pp2_ppio_deinit(priv->ppio);
734         return ret;
735 }
736
737 /**
738  * Flush receive queues.
739  *
740  * @param dev
741  *   Pointer to Ethernet device structure.
742  */
743 static void
744 mrvl_flush_rx_queues(struct rte_eth_dev *dev)
745 {
746         int i;
747
748         MRVL_LOG(INFO, "Flushing rx queues");
749         for (i = 0; i < dev->data->nb_rx_queues; i++) {
750                 int ret, num;
751
752                 do {
753                         struct mrvl_rxq *q = dev->data->rx_queues[i];
754                         struct pp2_ppio_desc descs[MRVL_PP2_RXD_MAX];
755
756                         num = MRVL_PP2_RXD_MAX;
757                         ret = pp2_ppio_recv(q->priv->ppio,
758                                             q->priv->rxq_map[q->queue_id].tc,
759                                             q->priv->rxq_map[q->queue_id].inq,
760                                             descs, (uint16_t *)&num);
761                 } while (ret == 0 && num);
762         }
763 }
764
765 /**
766  * Flush transmit shadow queues.
767  *
768  * @param dev
769  *   Pointer to Ethernet device structure.
770  */
771 static void
772 mrvl_flush_tx_shadow_queues(struct rte_eth_dev *dev)
773 {
774         int i, j;
775         struct mrvl_txq *txq;
776
777         MRVL_LOG(INFO, "Flushing tx shadow queues");
778         for (i = 0; i < dev->data->nb_tx_queues; i++) {
779                 txq = (struct mrvl_txq *)dev->data->tx_queues[i];
780
781                 for (j = 0; j < RTE_MAX_LCORE; j++) {
782                         struct mrvl_shadow_txq *sq;
783
784                         if (!hifs[j])
785                                 continue;
786
787                         sq = &txq->shadow_txqs[j];
788                         mrvl_free_sent_buffers(txq->priv->ppio,
789                                 hifs[j], j, sq, txq->queue_id, 1);
790                         while (sq->tail != sq->head) {
791                                 uint64_t addr = cookie_addr_high |
792                                         sq->ent[sq->tail].buff.cookie;
793                                 rte_pktmbuf_free(
794                                         (struct rte_mbuf *)addr);
795                                 sq->tail = (sq->tail + 1) &
796                                             MRVL_PP2_TX_SHADOWQ_MASK;
797                         }
798                         memset(sq, 0, sizeof(*sq));
799                 }
800         }
801 }
802
803 /**
804  * Flush hardware bpool (buffer-pool).
805  *
806  * @param dev
807  *   Pointer to Ethernet device structure.
808  */
809 static void
810 mrvl_flush_bpool(struct rte_eth_dev *dev)
811 {
812         struct mrvl_priv *priv = dev->data->dev_private;
813         struct pp2_hif *hif;
814         uint32_t num;
815         int ret;
816         unsigned int core_id = rte_lcore_id();
817
818         if (core_id == LCORE_ID_ANY)
819                 core_id = 0;
820
821         hif = mrvl_get_hif(priv, core_id);
822
823         ret = pp2_bpool_get_num_buffs(priv->bpool, &num);
824         if (ret) {
825                 MRVL_LOG(ERR, "Failed to get bpool buffers number");
826                 return;
827         }
828
829         while (num--) {
830                 struct pp2_buff_inf inf;
831                 uint64_t addr;
832
833                 ret = pp2_bpool_get_buff(hif, priv->bpool, &inf);
834                 if (ret)
835                         break;
836
837                 addr = cookie_addr_high | inf.cookie;
838                 rte_pktmbuf_free((struct rte_mbuf *)addr);
839         }
840 }
841
842 /**
843  * DPDK callback to stop the device.
844  *
845  * @param dev
846  *   Pointer to Ethernet device structure.
847  */
848 static void
849 mrvl_dev_stop(struct rte_eth_dev *dev)
850 {
851         mrvl_dev_set_link_down(dev);
852 }
853
854 /**
855  * DPDK callback to close the device.
856  *
857  * @param dev
858  *   Pointer to Ethernet device structure.
859  */
860 static void
861 mrvl_dev_close(struct rte_eth_dev *dev)
862 {
863         struct mrvl_priv *priv = dev->data->dev_private;
864         size_t i;
865
866         mrvl_flush_rx_queues(dev);
867         mrvl_flush_tx_shadow_queues(dev);
868         mrvl_flow_deinit(dev);
869         mrvl_mtr_deinit(dev);
870
871         for (i = 0; i < priv->ppio_params.inqs_params.num_tcs; ++i) {
872                 struct pp2_ppio_tc_params *tc_params =
873                         &priv->ppio_params.inqs_params.tcs_params[i];
874
875                 if (tc_params->inqs_params) {
876                         rte_free(tc_params->inqs_params);
877                         tc_params->inqs_params = NULL;
878                 }
879         }
880
881         if (priv->cls_tbl) {
882                 pp2_cls_tbl_deinit(priv->cls_tbl);
883                 priv->cls_tbl = NULL;
884         }
885
886         if (priv->qos_tbl) {
887                 pp2_cls_qos_tbl_deinit(priv->qos_tbl);
888                 priv->qos_tbl = NULL;
889         }
890
891         mrvl_flush_bpool(dev);
892         mrvl_tm_deinit(dev);
893
894         if (priv->ppio) {
895                 pp2_ppio_deinit(priv->ppio);
896                 priv->ppio = NULL;
897         }
898
899         /* policer must be released after ppio deinitialization */
900         if (priv->default_policer) {
901                 pp2_cls_plcr_deinit(priv->default_policer);
902                 priv->default_policer = NULL;
903         }
904
905
906         if (priv->bpool) {
907                 pp2_bpool_deinit(priv->bpool);
908                 used_bpools[priv->pp_id] &= ~(1 << priv->bpool_bit);
909                 priv->bpool = NULL;
910         }
911
912         mrvl_dev_num--;
913
914         if (mrvl_dev_num == 0) {
915                 MRVL_LOG(INFO, "Perform MUSDK deinit");
916                 mrvl_deinit_hifs();
917                 mrvl_deinit_pp2();
918                 rte_mvep_deinit(MVEP_MOD_T_PP2);
919         }
920 }
921
922 /**
923  * DPDK callback to retrieve physical link information.
924  *
925  * @param dev
926  *   Pointer to Ethernet device structure.
927  * @param wait_to_complete
928  *   Wait for request completion (ignored).
929  *
930  * @return
931  *   0 on success, negative error value otherwise.
932  */
933 static int
934 mrvl_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
935 {
936         /*
937          * TODO
938          * once MUSDK provides necessary API use it here
939          */
940         struct mrvl_priv *priv = dev->data->dev_private;
941         struct ethtool_cmd edata;
942         struct ifreq req;
943         int ret, fd, link_up;
944
945         if (!priv->ppio)
946                 return -EPERM;
947
948         edata.cmd = ETHTOOL_GSET;
949
950         strcpy(req.ifr_name, dev->data->name);
951         req.ifr_data = (void *)&edata;
952
953         fd = socket(AF_INET, SOCK_DGRAM, 0);
954         if (fd == -1)
955                 return -EFAULT;
956
957         ret = ioctl(fd, SIOCETHTOOL, &req);
958         if (ret == -1) {
959                 close(fd);
960                 return -EFAULT;
961         }
962
963         close(fd);
964
965         switch (ethtool_cmd_speed(&edata)) {
966         case SPEED_10:
967                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_10M;
968                 break;
969         case SPEED_100:
970                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_100M;
971                 break;
972         case SPEED_1000:
973                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_1G;
974                 break;
975         case SPEED_10000:
976                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_10G;
977                 break;
978         default:
979                 dev->data->dev_link.link_speed = ETH_SPEED_NUM_NONE;
980         }
981
982         dev->data->dev_link.link_duplex = edata.duplex ? ETH_LINK_FULL_DUPLEX :
983                                                          ETH_LINK_HALF_DUPLEX;
984         dev->data->dev_link.link_autoneg = edata.autoneg ? ETH_LINK_AUTONEG :
985                                                            ETH_LINK_FIXED;
986         pp2_ppio_get_link_state(priv->ppio, &link_up);
987         dev->data->dev_link.link_status = link_up ? ETH_LINK_UP : ETH_LINK_DOWN;
988
989         return 0;
990 }
991
992 /**
993  * DPDK callback to enable promiscuous mode.
994  *
995  * @param dev
996  *   Pointer to Ethernet device structure.
997  */
998 static void
999 mrvl_promiscuous_enable(struct rte_eth_dev *dev)
1000 {
1001         struct mrvl_priv *priv = dev->data->dev_private;
1002         int ret;
1003
1004         if (!priv->ppio)
1005                 return;
1006
1007         if (priv->isolated)
1008                 return;
1009
1010         ret = pp2_ppio_set_promisc(priv->ppio, 1);
1011         if (ret)
1012                 MRVL_LOG(ERR, "Failed to enable promiscuous mode");
1013 }
1014
1015 /**
1016  * DPDK callback to enable allmulti mode.
1017  *
1018  * @param dev
1019  *   Pointer to Ethernet device structure.
1020  */
1021 static void
1022 mrvl_allmulticast_enable(struct rte_eth_dev *dev)
1023 {
1024         struct mrvl_priv *priv = dev->data->dev_private;
1025         int ret;
1026
1027         if (!priv->ppio)
1028                 return;
1029
1030         if (priv->isolated)
1031                 return;
1032
1033         ret = pp2_ppio_set_mc_promisc(priv->ppio, 1);
1034         if (ret)
1035                 MRVL_LOG(ERR, "Failed enable all-multicast mode");
1036 }
1037
1038 /**
1039  * DPDK callback to disable promiscuous mode.
1040  *
1041  * @param dev
1042  *   Pointer to Ethernet device structure.
1043  */
1044 static void
1045 mrvl_promiscuous_disable(struct rte_eth_dev *dev)
1046 {
1047         struct mrvl_priv *priv = dev->data->dev_private;
1048         int ret;
1049
1050         if (!priv->ppio)
1051                 return;
1052
1053         ret = pp2_ppio_set_promisc(priv->ppio, 0);
1054         if (ret)
1055                 MRVL_LOG(ERR, "Failed to disable promiscuous mode");
1056 }
1057
1058 /**
1059  * DPDK callback to disable allmulticast mode.
1060  *
1061  * @param dev
1062  *   Pointer to Ethernet device structure.
1063  */
1064 static void
1065 mrvl_allmulticast_disable(struct rte_eth_dev *dev)
1066 {
1067         struct mrvl_priv *priv = dev->data->dev_private;
1068         int ret;
1069
1070         if (!priv->ppio)
1071                 return;
1072
1073         ret = pp2_ppio_set_mc_promisc(priv->ppio, 0);
1074         if (ret)
1075                 MRVL_LOG(ERR, "Failed to disable all-multicast mode");
1076 }
1077
1078 /**
1079  * DPDK callback to remove a MAC address.
1080  *
1081  * @param dev
1082  *   Pointer to Ethernet device structure.
1083  * @param index
1084  *   MAC address index.
1085  */
1086 static void
1087 mrvl_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
1088 {
1089         struct mrvl_priv *priv = dev->data->dev_private;
1090         char buf[RTE_ETHER_ADDR_FMT_SIZE];
1091         int ret;
1092
1093         if (!priv->ppio)
1094                 return;
1095
1096         if (priv->isolated)
1097                 return;
1098
1099         ret = pp2_ppio_remove_mac_addr(priv->ppio,
1100                                        dev->data->mac_addrs[index].addr_bytes);
1101         if (ret) {
1102                 rte_ether_format_addr(buf, sizeof(buf),
1103                                   &dev->data->mac_addrs[index]);
1104                 MRVL_LOG(ERR, "Failed to remove mac %s", buf);
1105         }
1106 }
1107
1108 /**
1109  * DPDK callback to add a MAC address.
1110  *
1111  * @param dev
1112  *   Pointer to Ethernet device structure.
1113  * @param mac_addr
1114  *   MAC address to register.
1115  * @param index
1116  *   MAC address index.
1117  * @param vmdq
1118  *   VMDq pool index to associate address with (unused).
1119  *
1120  * @return
1121  *   0 on success, negative error value otherwise.
1122  */
1123 static int
1124 mrvl_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
1125                   uint32_t index, uint32_t vmdq __rte_unused)
1126 {
1127         struct mrvl_priv *priv = dev->data->dev_private;
1128         char buf[RTE_ETHER_ADDR_FMT_SIZE];
1129         int ret;
1130
1131         if (priv->isolated)
1132                 return -ENOTSUP;
1133
1134         if (index == 0)
1135                 /* For setting index 0, mrvl_mac_addr_set() should be used.*/
1136                 return -1;
1137
1138         if (!priv->ppio)
1139                 return 0;
1140
1141         /*
1142          * Maximum number of uc addresses can be tuned via kernel module mvpp2x
1143          * parameter uc_filter_max. Maximum number of mc addresses is then
1144          * MRVL_MAC_ADDRS_MAX - uc_filter_max. Currently it defaults to 4 and
1145          * 21 respectively.
1146          *
1147          * If more than uc_filter_max uc addresses were added to filter list
1148          * then NIC will switch to promiscuous mode automatically.
1149          *
1150          * If more than MRVL_MAC_ADDRS_MAX - uc_filter_max number mc addresses
1151          * were added to filter list then NIC will switch to all-multicast mode
1152          * automatically.
1153          */
1154         ret = pp2_ppio_add_mac_addr(priv->ppio, mac_addr->addr_bytes);
1155         if (ret) {
1156                 rte_ether_format_addr(buf, sizeof(buf), mac_addr);
1157                 MRVL_LOG(ERR, "Failed to add mac %s", buf);
1158                 return -1;
1159         }
1160
1161         return 0;
1162 }
1163
1164 /**
1165  * DPDK callback to set the primary MAC address.
1166  *
1167  * @param dev
1168  *   Pointer to Ethernet device structure.
1169  * @param mac_addr
1170  *   MAC address to register.
1171  *
1172  * @return
1173  *   0 on success, negative error value otherwise.
1174  */
1175 static int
1176 mrvl_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1177 {
1178         struct mrvl_priv *priv = dev->data->dev_private;
1179         int ret;
1180
1181         if (!priv->ppio)
1182                 return 0;
1183
1184         if (priv->isolated)
1185                 return -ENOTSUP;
1186
1187         ret = pp2_ppio_set_mac_addr(priv->ppio, mac_addr->addr_bytes);
1188         if (ret) {
1189                 char buf[RTE_ETHER_ADDR_FMT_SIZE];
1190                 rte_ether_format_addr(buf, sizeof(buf), mac_addr);
1191                 MRVL_LOG(ERR, "Failed to set mac to %s", buf);
1192         }
1193
1194         return ret;
1195 }
1196
1197 /**
1198  * DPDK callback to get device statistics.
1199  *
1200  * @param dev
1201  *   Pointer to Ethernet device structure.
1202  * @param stats
1203  *   Stats structure output buffer.
1204  *
1205  * @return
1206  *   0 on success, negative error value otherwise.
1207  */
1208 static int
1209 mrvl_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
1210 {
1211         struct mrvl_priv *priv = dev->data->dev_private;
1212         struct pp2_ppio_statistics ppio_stats;
1213         uint64_t drop_mac = 0;
1214         unsigned int i, idx, ret;
1215
1216         if (!priv->ppio)
1217                 return -EPERM;
1218
1219         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1220                 struct mrvl_rxq *rxq = dev->data->rx_queues[i];
1221                 struct pp2_ppio_inq_statistics rx_stats;
1222
1223                 if (!rxq)
1224                         continue;
1225
1226                 idx = rxq->queue_id;
1227                 if (unlikely(idx >= RTE_ETHDEV_QUEUE_STAT_CNTRS)) {
1228                         MRVL_LOG(ERR,
1229                                 "rx queue %d stats out of range (0 - %d)",
1230                                 idx, RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
1231                         continue;
1232                 }
1233
1234                 ret = pp2_ppio_inq_get_statistics(priv->ppio,
1235                                                   priv->rxq_map[idx].tc,
1236                                                   priv->rxq_map[idx].inq,
1237                                                   &rx_stats, 0);
1238                 if (unlikely(ret)) {
1239                         MRVL_LOG(ERR,
1240                                 "Failed to update rx queue %d stats", idx);
1241                         break;
1242                 }
1243
1244                 stats->q_ibytes[idx] = rxq->bytes_recv;
1245                 stats->q_ipackets[idx] = rx_stats.enq_desc - rxq->drop_mac;
1246                 stats->q_errors[idx] = rx_stats.drop_early +
1247                                        rx_stats.drop_fullq +
1248                                        rx_stats.drop_bm +
1249                                        rxq->drop_mac;
1250                 stats->ibytes += rxq->bytes_recv;
1251                 drop_mac += rxq->drop_mac;
1252         }
1253
1254         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1255                 struct mrvl_txq *txq = dev->data->tx_queues[i];
1256                 struct pp2_ppio_outq_statistics tx_stats;
1257
1258                 if (!txq)
1259                         continue;
1260
1261                 idx = txq->queue_id;
1262                 if (unlikely(idx >= RTE_ETHDEV_QUEUE_STAT_CNTRS)) {
1263                         MRVL_LOG(ERR,
1264                                 "tx queue %d stats out of range (0 - %d)",
1265                                 idx, RTE_ETHDEV_QUEUE_STAT_CNTRS - 1);
1266                 }
1267
1268                 ret = pp2_ppio_outq_get_statistics(priv->ppio, idx,
1269                                                    &tx_stats, 0);
1270                 if (unlikely(ret)) {
1271                         MRVL_LOG(ERR,
1272                                 "Failed to update tx queue %d stats", idx);
1273                         break;
1274                 }
1275
1276                 stats->q_opackets[idx] = tx_stats.deq_desc;
1277                 stats->q_obytes[idx] = txq->bytes_sent;
1278                 stats->obytes += txq->bytes_sent;
1279         }
1280
1281         ret = pp2_ppio_get_statistics(priv->ppio, &ppio_stats, 0);
1282         if (unlikely(ret)) {
1283                 MRVL_LOG(ERR, "Failed to update port statistics");
1284                 return ret;
1285         }
1286
1287         stats->ipackets += ppio_stats.rx_packets - drop_mac;
1288         stats->opackets += ppio_stats.tx_packets;
1289         stats->imissed += ppio_stats.rx_fullq_dropped +
1290                           ppio_stats.rx_bm_dropped +
1291                           ppio_stats.rx_early_dropped +
1292                           ppio_stats.rx_fifo_dropped +
1293                           ppio_stats.rx_cls_dropped;
1294         stats->ierrors = drop_mac;
1295
1296         return 0;
1297 }
1298
1299 /**
1300  * DPDK callback to clear device statistics.
1301  *
1302  * @param dev
1303  *   Pointer to Ethernet device structure.
1304  */
1305 static void
1306 mrvl_stats_reset(struct rte_eth_dev *dev)
1307 {
1308         struct mrvl_priv *priv = dev->data->dev_private;
1309         int i;
1310
1311         if (!priv->ppio)
1312                 return;
1313
1314         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1315                 struct mrvl_rxq *rxq = dev->data->rx_queues[i];
1316
1317                 pp2_ppio_inq_get_statistics(priv->ppio, priv->rxq_map[i].tc,
1318                                             priv->rxq_map[i].inq, NULL, 1);
1319                 rxq->bytes_recv = 0;
1320                 rxq->drop_mac = 0;
1321         }
1322
1323         for (i = 0; i < dev->data->nb_tx_queues; i++) {
1324                 struct mrvl_txq *txq = dev->data->tx_queues[i];
1325
1326                 pp2_ppio_outq_get_statistics(priv->ppio, i, NULL, 1);
1327                 txq->bytes_sent = 0;
1328         }
1329
1330         pp2_ppio_get_statistics(priv->ppio, NULL, 1);
1331 }
1332
1333 /**
1334  * DPDK callback to get extended statistics.
1335  *
1336  * @param dev
1337  *   Pointer to Ethernet device structure.
1338  * @param stats
1339  *   Pointer to xstats table.
1340  * @param n
1341  *   Number of entries in xstats table.
1342  * @return
1343  *   Negative value on error, number of read xstats otherwise.
1344  */
1345 static int
1346 mrvl_xstats_get(struct rte_eth_dev *dev,
1347                 struct rte_eth_xstat *stats, unsigned int n)
1348 {
1349         struct mrvl_priv *priv = dev->data->dev_private;
1350         struct pp2_ppio_statistics ppio_stats;
1351         unsigned int i;
1352
1353         if (!stats)
1354                 return 0;
1355
1356         pp2_ppio_get_statistics(priv->ppio, &ppio_stats, 0);
1357         for (i = 0; i < n && i < RTE_DIM(mrvl_xstats_tbl); i++) {
1358                 uint64_t val;
1359
1360                 if (mrvl_xstats_tbl[i].size == sizeof(uint32_t))
1361                         val = *(uint32_t *)((uint8_t *)&ppio_stats +
1362                                             mrvl_xstats_tbl[i].offset);
1363                 else if (mrvl_xstats_tbl[i].size == sizeof(uint64_t))
1364                         val = *(uint64_t *)((uint8_t *)&ppio_stats +
1365                                             mrvl_xstats_tbl[i].offset);
1366                 else
1367                         return -EINVAL;
1368
1369                 stats[i].id = i;
1370                 stats[i].value = val;
1371         }
1372
1373         return n;
1374 }
1375
1376 /**
1377  * DPDK callback to reset extended statistics.
1378  *
1379  * @param dev
1380  *   Pointer to Ethernet device structure.
1381  */
1382 static void
1383 mrvl_xstats_reset(struct rte_eth_dev *dev)
1384 {
1385         mrvl_stats_reset(dev);
1386 }
1387
1388 /**
1389  * DPDK callback to get extended statistics names.
1390  *
1391  * @param dev (unused)
1392  *   Pointer to Ethernet device structure.
1393  * @param xstats_names
1394  *   Pointer to xstats names table.
1395  * @param size
1396  *   Size of the xstats names table.
1397  * @return
1398  *   Number of read names.
1399  */
1400 static int
1401 mrvl_xstats_get_names(struct rte_eth_dev *dev __rte_unused,
1402                       struct rte_eth_xstat_name *xstats_names,
1403                       unsigned int size)
1404 {
1405         unsigned int i;
1406
1407         if (!xstats_names)
1408                 return RTE_DIM(mrvl_xstats_tbl);
1409
1410         for (i = 0; i < size && i < RTE_DIM(mrvl_xstats_tbl); i++)
1411                 strlcpy(xstats_names[i].name, mrvl_xstats_tbl[i].name,
1412                         RTE_ETH_XSTATS_NAME_SIZE);
1413
1414         return size;
1415 }
1416
1417 /**
1418  * DPDK callback to get information about the device.
1419  *
1420  * @param dev
1421  *   Pointer to Ethernet device structure (unused).
1422  * @param info
1423  *   Info structure output buffer.
1424  */
1425 static int
1426 mrvl_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
1427                    struct rte_eth_dev_info *info)
1428 {
1429         info->speed_capa = ETH_LINK_SPEED_10M |
1430                            ETH_LINK_SPEED_100M |
1431                            ETH_LINK_SPEED_1G |
1432                            ETH_LINK_SPEED_10G;
1433
1434         info->max_rx_queues = MRVL_PP2_RXQ_MAX;
1435         info->max_tx_queues = MRVL_PP2_TXQ_MAX;
1436         info->max_mac_addrs = MRVL_MAC_ADDRS_MAX;
1437
1438         info->rx_desc_lim.nb_max = MRVL_PP2_RXD_MAX;
1439         info->rx_desc_lim.nb_min = MRVL_PP2_RXD_MIN;
1440         info->rx_desc_lim.nb_align = MRVL_PP2_RXD_ALIGN;
1441
1442         info->tx_desc_lim.nb_max = MRVL_PP2_TXD_MAX;
1443         info->tx_desc_lim.nb_min = MRVL_PP2_TXD_MIN;
1444         info->tx_desc_lim.nb_align = MRVL_PP2_TXD_ALIGN;
1445
1446         info->rx_offload_capa = MRVL_RX_OFFLOADS;
1447         info->rx_queue_offload_capa = MRVL_RX_OFFLOADS;
1448
1449         info->tx_offload_capa = MRVL_TX_OFFLOADS;
1450         info->tx_queue_offload_capa = MRVL_TX_OFFLOADS;
1451
1452         info->flow_type_rss_offloads = ETH_RSS_IPV4 |
1453                                        ETH_RSS_NONFRAG_IPV4_TCP |
1454                                        ETH_RSS_NONFRAG_IPV4_UDP;
1455
1456         /* By default packets are dropped if no descriptors are available */
1457         info->default_rxconf.rx_drop_en = 1;
1458
1459         info->max_rx_pktlen = MRVL_PKT_SIZE_MAX;
1460
1461         return 0;
1462 }
1463
1464 /**
1465  * Return supported packet types.
1466  *
1467  * @param dev
1468  *   Pointer to Ethernet device structure (unused).
1469  *
1470  * @return
1471  *   Const pointer to the table with supported packet types.
1472  */
1473 static const uint32_t *
1474 mrvl_dev_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
1475 {
1476         static const uint32_t ptypes[] = {
1477                 RTE_PTYPE_L2_ETHER,
1478                 RTE_PTYPE_L2_ETHER_VLAN,
1479                 RTE_PTYPE_L2_ETHER_QINQ,
1480                 RTE_PTYPE_L3_IPV4,
1481                 RTE_PTYPE_L3_IPV4_EXT,
1482                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
1483                 RTE_PTYPE_L3_IPV6,
1484                 RTE_PTYPE_L3_IPV6_EXT,
1485                 RTE_PTYPE_L2_ETHER_ARP,
1486                 RTE_PTYPE_L4_TCP,
1487                 RTE_PTYPE_L4_UDP
1488         };
1489
1490         return ptypes;
1491 }
1492
1493 /**
1494  * DPDK callback to get information about specific receive queue.
1495  *
1496  * @param dev
1497  *   Pointer to Ethernet device structure.
1498  * @param rx_queue_id
1499  *   Receive queue index.
1500  * @param qinfo
1501  *   Receive queue information structure.
1502  */
1503 static void mrvl_rxq_info_get(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1504                               struct rte_eth_rxq_info *qinfo)
1505 {
1506         struct mrvl_rxq *q = dev->data->rx_queues[rx_queue_id];
1507         struct mrvl_priv *priv = dev->data->dev_private;
1508         int inq = priv->rxq_map[rx_queue_id].inq;
1509         int tc = priv->rxq_map[rx_queue_id].tc;
1510         struct pp2_ppio_tc_params *tc_params =
1511                 &priv->ppio_params.inqs_params.tcs_params[tc];
1512
1513         qinfo->mp = q->mp;
1514         qinfo->nb_desc = tc_params->inqs_params[inq].size;
1515 }
1516
1517 /**
1518  * DPDK callback to get information about specific transmit queue.
1519  *
1520  * @param dev
1521  *   Pointer to Ethernet device structure.
1522  * @param tx_queue_id
1523  *   Transmit queue index.
1524  * @param qinfo
1525  *   Transmit queue information structure.
1526  */
1527 static void mrvl_txq_info_get(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1528                               struct rte_eth_txq_info *qinfo)
1529 {
1530         struct mrvl_priv *priv = dev->data->dev_private;
1531         struct mrvl_txq *txq = dev->data->tx_queues[tx_queue_id];
1532
1533         qinfo->nb_desc =
1534                 priv->ppio_params.outqs_params.outqs_params[tx_queue_id].size;
1535         qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
1536 }
1537
1538 /**
1539  * DPDK callback to Configure a VLAN filter.
1540  *
1541  * @param dev
1542  *   Pointer to Ethernet device structure.
1543  * @param vlan_id
1544  *   VLAN ID to filter.
1545  * @param on
1546  *   Toggle filter.
1547  *
1548  * @return
1549  *   0 on success, negative error value otherwise.
1550  */
1551 static int
1552 mrvl_vlan_filter_set(struct rte_eth_dev *dev, uint16_t vlan_id, int on)
1553 {
1554         struct mrvl_priv *priv = dev->data->dev_private;
1555
1556         if (!priv->ppio)
1557                 return -EPERM;
1558
1559         if (priv->isolated)
1560                 return -ENOTSUP;
1561
1562         return on ? pp2_ppio_add_vlan(priv->ppio, vlan_id) :
1563                     pp2_ppio_remove_vlan(priv->ppio, vlan_id);
1564 }
1565
1566 /**
1567  * Release buffers to hardware bpool (buffer-pool)
1568  *
1569  * @param rxq
1570  *   Receive queue pointer.
1571  * @param num
1572  *   Number of buffers to release to bpool.
1573  *
1574  * @return
1575  *   0 on success, negative error value otherwise.
1576  */
1577 static int
1578 mrvl_fill_bpool(struct mrvl_rxq *rxq, int num)
1579 {
1580         struct buff_release_entry entries[MRVL_PP2_RXD_MAX];
1581         struct rte_mbuf *mbufs[MRVL_PP2_RXD_MAX];
1582         int i, ret;
1583         unsigned int core_id;
1584         struct pp2_hif *hif;
1585         struct pp2_bpool *bpool;
1586
1587         core_id = rte_lcore_id();
1588         if (core_id == LCORE_ID_ANY)
1589                 core_id = 0;
1590
1591         hif = mrvl_get_hif(rxq->priv, core_id);
1592         if (!hif)
1593                 return -1;
1594
1595         bpool = rxq->priv->bpool;
1596
1597         ret = rte_pktmbuf_alloc_bulk(rxq->mp, mbufs, num);
1598         if (ret)
1599                 return ret;
1600
1601         if (cookie_addr_high == MRVL_COOKIE_ADDR_INVALID)
1602                 cookie_addr_high =
1603                         (uint64_t)mbufs[0] & MRVL_COOKIE_HIGH_ADDR_MASK;
1604
1605         for (i = 0; i < num; i++) {
1606                 if (((uint64_t)mbufs[i] & MRVL_COOKIE_HIGH_ADDR_MASK)
1607                         != cookie_addr_high) {
1608                         MRVL_LOG(ERR,
1609                                 "mbuf virtual addr high 0x%lx out of range",
1610                                 (uint64_t)mbufs[i] >> 32);
1611                         goto out;
1612                 }
1613
1614                 entries[i].buff.addr =
1615                         rte_mbuf_data_iova_default(mbufs[i]);
1616                 entries[i].buff.cookie = (uint64_t)mbufs[i];
1617                 entries[i].bpool = bpool;
1618         }
1619
1620         pp2_bpool_put_buffs(hif, entries, (uint16_t *)&i);
1621         mrvl_port_bpool_size[bpool->pp2_id][bpool->id][core_id] += i;
1622
1623         if (i != num)
1624                 goto out;
1625
1626         return 0;
1627 out:
1628         for (; i < num; i++)
1629                 rte_pktmbuf_free(mbufs[i]);
1630
1631         return -1;
1632 }
1633
1634 /**
1635  * DPDK callback to configure the receive queue.
1636  *
1637  * @param dev
1638  *   Pointer to Ethernet device structure.
1639  * @param idx
1640  *   RX queue index.
1641  * @param desc
1642  *   Number of descriptors to configure in queue.
1643  * @param socket
1644  *   NUMA socket on which memory must be allocated.
1645  * @param conf
1646  *   Thresholds parameters.
1647  * @param mp
1648  *   Memory pool for buffer allocations.
1649  *
1650  * @return
1651  *   0 on success, negative error value otherwise.
1652  */
1653 static int
1654 mrvl_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1655                     unsigned int socket,
1656                     const struct rte_eth_rxconf *conf,
1657                     struct rte_mempool *mp)
1658 {
1659         struct mrvl_priv *priv = dev->data->dev_private;
1660         struct mrvl_rxq *rxq;
1661         uint32_t frame_size, buf_size = rte_pktmbuf_data_room_size(mp);
1662         uint32_t max_rx_pkt_len = dev->data->dev_conf.rxmode.max_rx_pkt_len;
1663         int ret, tc, inq;
1664         uint64_t offloads;
1665
1666         offloads = conf->offloads | dev->data->dev_conf.rxmode.offloads;
1667
1668         if (priv->rxq_map[idx].tc == MRVL_UNKNOWN_TC) {
1669                 /*
1670                  * Unknown TC mapping, mapping will not have a correct queue.
1671                  */
1672                 MRVL_LOG(ERR, "Unknown TC mapping for queue %hu eth%hhu",
1673                         idx, priv->ppio_id);
1674                 return -EFAULT;
1675         }
1676
1677         frame_size = buf_size - RTE_PKTMBUF_HEADROOM - MRVL_PKT_EFFEC_OFFS;
1678         if (frame_size < max_rx_pkt_len) {
1679                 MRVL_LOG(WARNING,
1680                         "Mbuf size must be increased to %u bytes to hold up "
1681                         "to %u bytes of data.",
1682                         buf_size + max_rx_pkt_len - frame_size,
1683                         max_rx_pkt_len);
1684                 dev->data->dev_conf.rxmode.max_rx_pkt_len = frame_size;
1685                 MRVL_LOG(INFO, "Setting max rx pkt len to %u",
1686                         dev->data->dev_conf.rxmode.max_rx_pkt_len);
1687         }
1688
1689         if (dev->data->rx_queues[idx]) {
1690                 rte_free(dev->data->rx_queues[idx]);
1691                 dev->data->rx_queues[idx] = NULL;
1692         }
1693
1694         rxq = rte_zmalloc_socket("rxq", sizeof(*rxq), 0, socket);
1695         if (!rxq)
1696                 return -ENOMEM;
1697
1698         rxq->priv = priv;
1699         rxq->mp = mp;
1700         rxq->cksum_enabled = offloads & DEV_RX_OFFLOAD_IPV4_CKSUM;
1701         rxq->queue_id = idx;
1702         rxq->port_id = dev->data->port_id;
1703         mrvl_port_to_bpool_lookup[rxq->port_id] = priv->bpool;
1704
1705         tc = priv->rxq_map[rxq->queue_id].tc,
1706         inq = priv->rxq_map[rxq->queue_id].inq;
1707         priv->ppio_params.inqs_params.tcs_params[tc].inqs_params[inq].size =
1708                 desc;
1709
1710         ret = mrvl_fill_bpool(rxq, desc);
1711         if (ret) {
1712                 rte_free(rxq);
1713                 return ret;
1714         }
1715
1716         priv->bpool_init_size += desc;
1717
1718         dev->data->rx_queues[idx] = rxq;
1719
1720         return 0;
1721 }
1722
1723 /**
1724  * DPDK callback to release the receive queue.
1725  *
1726  * @param rxq
1727  *   Generic receive queue pointer.
1728  */
1729 static void
1730 mrvl_rx_queue_release(void *rxq)
1731 {
1732         struct mrvl_rxq *q = rxq;
1733         struct pp2_ppio_tc_params *tc_params;
1734         int i, num, tc, inq;
1735         struct pp2_hif *hif;
1736         unsigned int core_id = rte_lcore_id();
1737
1738         if (core_id == LCORE_ID_ANY)
1739                 core_id = 0;
1740
1741         if (!q)
1742                 return;
1743
1744         hif = mrvl_get_hif(q->priv, core_id);
1745
1746         if (!hif)
1747                 return;
1748
1749         tc = q->priv->rxq_map[q->queue_id].tc;
1750         inq = q->priv->rxq_map[q->queue_id].inq;
1751         tc_params = &q->priv->ppio_params.inqs_params.tcs_params[tc];
1752         num = tc_params->inqs_params[inq].size;
1753         for (i = 0; i < num; i++) {
1754                 struct pp2_buff_inf inf;
1755                 uint64_t addr;
1756
1757                 pp2_bpool_get_buff(hif, q->priv->bpool, &inf);
1758                 addr = cookie_addr_high | inf.cookie;
1759                 rte_pktmbuf_free((struct rte_mbuf *)addr);
1760         }
1761
1762         rte_free(q);
1763 }
1764
1765 /**
1766  * DPDK callback to configure the transmit queue.
1767  *
1768  * @param dev
1769  *   Pointer to Ethernet device structure.
1770  * @param idx
1771  *   Transmit queue index.
1772  * @param desc
1773  *   Number of descriptors to configure in the queue.
1774  * @param socket
1775  *   NUMA socket on which memory must be allocated.
1776  * @param conf
1777  *   Tx queue configuration parameters.
1778  *
1779  * @return
1780  *   0 on success, negative error value otherwise.
1781  */
1782 static int
1783 mrvl_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t desc,
1784                     unsigned int socket,
1785                     const struct rte_eth_txconf *conf)
1786 {
1787         struct mrvl_priv *priv = dev->data->dev_private;
1788         struct mrvl_txq *txq;
1789
1790         if (dev->data->tx_queues[idx]) {
1791                 rte_free(dev->data->tx_queues[idx]);
1792                 dev->data->tx_queues[idx] = NULL;
1793         }
1794
1795         txq = rte_zmalloc_socket("txq", sizeof(*txq), 0, socket);
1796         if (!txq)
1797                 return -ENOMEM;
1798
1799         txq->priv = priv;
1800         txq->queue_id = idx;
1801         txq->port_id = dev->data->port_id;
1802         txq->tx_deferred_start = conf->tx_deferred_start;
1803         dev->data->tx_queues[idx] = txq;
1804
1805         priv->ppio_params.outqs_params.outqs_params[idx].size = desc;
1806
1807         return 0;
1808 }
1809
1810 /**
1811  * DPDK callback to release the transmit queue.
1812  *
1813  * @param txq
1814  *   Generic transmit queue pointer.
1815  */
1816 static void
1817 mrvl_tx_queue_release(void *txq)
1818 {
1819         struct mrvl_txq *q = txq;
1820
1821         if (!q)
1822                 return;
1823
1824         rte_free(q);
1825 }
1826
1827 /**
1828  * DPDK callback to get flow control configuration.
1829  *
1830  * @param dev
1831  *  Pointer to Ethernet device structure.
1832  * @param fc_conf
1833  *  Pointer to the flow control configuration.
1834  *
1835  * @return
1836  *  0 on success, negative error value otherwise.
1837  */
1838 static int
1839 mrvl_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1840 {
1841         struct mrvl_priv *priv = dev->data->dev_private;
1842         int ret, en;
1843
1844         if (!priv)
1845                 return -EPERM;
1846
1847         ret = pp2_ppio_get_rx_pause(priv->ppio, &en);
1848         if (ret) {
1849                 MRVL_LOG(ERR, "Failed to read rx pause state");
1850                 return ret;
1851         }
1852
1853         fc_conf->mode = en ? RTE_FC_RX_PAUSE : RTE_FC_NONE;
1854
1855         return 0;
1856 }
1857
1858 /**
1859  * DPDK callback to set flow control configuration.
1860  *
1861  * @param dev
1862  *  Pointer to Ethernet device structure.
1863  * @param fc_conf
1864  *  Pointer to the flow control configuration.
1865  *
1866  * @return
1867  *  0 on success, negative error value otherwise.
1868  */
1869 static int
1870 mrvl_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1871 {
1872         struct mrvl_priv *priv = dev->data->dev_private;
1873
1874         if (!priv)
1875                 return -EPERM;
1876
1877         if (fc_conf->high_water ||
1878             fc_conf->low_water ||
1879             fc_conf->pause_time ||
1880             fc_conf->mac_ctrl_frame_fwd ||
1881             fc_conf->autoneg) {
1882                 MRVL_LOG(ERR, "Flowctrl parameter is not supported");
1883
1884                 return -EINVAL;
1885         }
1886
1887         if (fc_conf->mode == RTE_FC_NONE ||
1888             fc_conf->mode == RTE_FC_RX_PAUSE) {
1889                 int ret, en;
1890
1891                 en = fc_conf->mode == RTE_FC_NONE ? 0 : 1;
1892                 ret = pp2_ppio_set_rx_pause(priv->ppio, en);
1893                 if (ret)
1894                         MRVL_LOG(ERR,
1895                                 "Failed to change flowctrl on RX side");
1896
1897                 return ret;
1898         }
1899
1900         return 0;
1901 }
1902
1903 /**
1904  * Update RSS hash configuration
1905  *
1906  * @param dev
1907  *   Pointer to Ethernet device structure.
1908  * @param rss_conf
1909  *   Pointer to RSS configuration.
1910  *
1911  * @return
1912  *   0 on success, negative error value otherwise.
1913  */
1914 static int
1915 mrvl_rss_hash_update(struct rte_eth_dev *dev,
1916                      struct rte_eth_rss_conf *rss_conf)
1917 {
1918         struct mrvl_priv *priv = dev->data->dev_private;
1919
1920         if (priv->isolated)
1921                 return -ENOTSUP;
1922
1923         return mrvl_configure_rss(priv, rss_conf);
1924 }
1925
1926 /**
1927  * DPDK callback to get RSS hash configuration.
1928  *
1929  * @param dev
1930  *   Pointer to Ethernet device structure.
1931  * @rss_conf
1932  *   Pointer to RSS configuration.
1933  *
1934  * @return
1935  *   Always 0.
1936  */
1937 static int
1938 mrvl_rss_hash_conf_get(struct rte_eth_dev *dev,
1939                        struct rte_eth_rss_conf *rss_conf)
1940 {
1941         struct mrvl_priv *priv = dev->data->dev_private;
1942         enum pp2_ppio_hash_type hash_type =
1943                 priv->ppio_params.inqs_params.hash_type;
1944
1945         rss_conf->rss_key = NULL;
1946
1947         if (hash_type == PP2_PPIO_HASH_T_NONE)
1948                 rss_conf->rss_hf = 0;
1949         else if (hash_type == PP2_PPIO_HASH_T_2_TUPLE)
1950                 rss_conf->rss_hf = ETH_RSS_IPV4;
1951         else if (hash_type == PP2_PPIO_HASH_T_5_TUPLE && priv->rss_hf_tcp)
1952                 rss_conf->rss_hf = ETH_RSS_NONFRAG_IPV4_TCP;
1953         else if (hash_type == PP2_PPIO_HASH_T_5_TUPLE && !priv->rss_hf_tcp)
1954                 rss_conf->rss_hf = ETH_RSS_NONFRAG_IPV4_UDP;
1955
1956         return 0;
1957 }
1958
1959 /**
1960  * DPDK callback to get rte_flow callbacks.
1961  *
1962  * @param dev
1963  *   Pointer to the device structure.
1964  * @param filer_type
1965  *   Flow filter type.
1966  * @param filter_op
1967  *   Flow filter operation.
1968  * @param arg
1969  *   Pointer to pass the flow ops.
1970  *
1971  * @return
1972  *   0 on success, negative error value otherwise.
1973  */
1974 static int
1975 mrvl_eth_filter_ctrl(struct rte_eth_dev *dev __rte_unused,
1976                      enum rte_filter_type filter_type,
1977                      enum rte_filter_op filter_op, void *arg)
1978 {
1979         switch (filter_type) {
1980         case RTE_ETH_FILTER_GENERIC:
1981                 if (filter_op != RTE_ETH_FILTER_GET)
1982                         return -EINVAL;
1983                 *(const void **)arg = &mrvl_flow_ops;
1984                 return 0;
1985         default:
1986                 MRVL_LOG(WARNING, "Filter type (%d) not supported",
1987                                 filter_type);
1988                 return -EINVAL;
1989         }
1990 }
1991
1992 /**
1993  * DPDK callback to get rte_mtr callbacks.
1994  *
1995  * @param dev
1996  *   Pointer to the device structure.
1997  * @param ops
1998  *   Pointer to pass the mtr ops.
1999  *
2000  * @return
2001  *   Always 0.
2002  */
2003 static int
2004 mrvl_mtr_ops_get(struct rte_eth_dev *dev __rte_unused, void *ops)
2005 {
2006         *(const void **)ops = &mrvl_mtr_ops;
2007
2008         return 0;
2009 }
2010
2011 /**
2012  * DPDK callback to get rte_tm callbacks.
2013  *
2014  * @param dev
2015  *   Pointer to the device structure.
2016  * @param ops
2017  *   Pointer to pass the tm ops.
2018  *
2019  * @return
2020  *   Always 0.
2021  */
2022 static int
2023 mrvl_tm_ops_get(struct rte_eth_dev *dev __rte_unused, void *ops)
2024 {
2025         *(const void **)ops = &mrvl_tm_ops;
2026
2027         return 0;
2028 }
2029
2030 static const struct eth_dev_ops mrvl_ops = {
2031         .dev_configure = mrvl_dev_configure,
2032         .dev_start = mrvl_dev_start,
2033         .dev_stop = mrvl_dev_stop,
2034         .dev_set_link_up = mrvl_dev_set_link_up,
2035         .dev_set_link_down = mrvl_dev_set_link_down,
2036         .dev_close = mrvl_dev_close,
2037         .link_update = mrvl_link_update,
2038         .promiscuous_enable = mrvl_promiscuous_enable,
2039         .allmulticast_enable = mrvl_allmulticast_enable,
2040         .promiscuous_disable = mrvl_promiscuous_disable,
2041         .allmulticast_disable = mrvl_allmulticast_disable,
2042         .mac_addr_remove = mrvl_mac_addr_remove,
2043         .mac_addr_add = mrvl_mac_addr_add,
2044         .mac_addr_set = mrvl_mac_addr_set,
2045         .mtu_set = mrvl_mtu_set,
2046         .stats_get = mrvl_stats_get,
2047         .stats_reset = mrvl_stats_reset,
2048         .xstats_get = mrvl_xstats_get,
2049         .xstats_reset = mrvl_xstats_reset,
2050         .xstats_get_names = mrvl_xstats_get_names,
2051         .dev_infos_get = mrvl_dev_infos_get,
2052         .dev_supported_ptypes_get = mrvl_dev_supported_ptypes_get,
2053         .rxq_info_get = mrvl_rxq_info_get,
2054         .txq_info_get = mrvl_txq_info_get,
2055         .vlan_filter_set = mrvl_vlan_filter_set,
2056         .tx_queue_start = mrvl_tx_queue_start,
2057         .tx_queue_stop = mrvl_tx_queue_stop,
2058         .rx_queue_setup = mrvl_rx_queue_setup,
2059         .rx_queue_release = mrvl_rx_queue_release,
2060         .tx_queue_setup = mrvl_tx_queue_setup,
2061         .tx_queue_release = mrvl_tx_queue_release,
2062         .flow_ctrl_get = mrvl_flow_ctrl_get,
2063         .flow_ctrl_set = mrvl_flow_ctrl_set,
2064         .rss_hash_update = mrvl_rss_hash_update,
2065         .rss_hash_conf_get = mrvl_rss_hash_conf_get,
2066         .filter_ctrl = mrvl_eth_filter_ctrl,
2067         .mtr_ops_get = mrvl_mtr_ops_get,
2068         .tm_ops_get = mrvl_tm_ops_get,
2069 };
2070
2071 /**
2072  * Return packet type information and l3/l4 offsets.
2073  *
2074  * @param desc
2075  *   Pointer to the received packet descriptor.
2076  * @param l3_offset
2077  *   l3 packet offset.
2078  * @param l4_offset
2079  *   l4 packet offset.
2080  *
2081  * @return
2082  *   Packet type information.
2083  */
2084 static inline uint64_t
2085 mrvl_desc_to_packet_type_and_offset(struct pp2_ppio_desc *desc,
2086                                     uint8_t *l3_offset, uint8_t *l4_offset)
2087 {
2088         enum pp2_inq_l3_type l3_type;
2089         enum pp2_inq_l4_type l4_type;
2090         enum pp2_inq_vlan_tag vlan_tag;
2091         uint64_t packet_type;
2092
2093         pp2_ppio_inq_desc_get_l3_info(desc, &l3_type, l3_offset);
2094         pp2_ppio_inq_desc_get_l4_info(desc, &l4_type, l4_offset);
2095         pp2_ppio_inq_desc_get_vlan_tag(desc, &vlan_tag);
2096
2097         packet_type = RTE_PTYPE_L2_ETHER;
2098
2099         switch (vlan_tag) {
2100         case PP2_INQ_VLAN_TAG_SINGLE:
2101                 packet_type |= RTE_PTYPE_L2_ETHER_VLAN;
2102                 break;
2103         case PP2_INQ_VLAN_TAG_DOUBLE:
2104         case PP2_INQ_VLAN_TAG_TRIPLE:
2105                 packet_type |= RTE_PTYPE_L2_ETHER_QINQ;
2106                 break;
2107         default:
2108                 break;
2109         }
2110
2111         switch (l3_type) {
2112         case PP2_INQ_L3_TYPE_IPV4_NO_OPTS:
2113                 packet_type |= RTE_PTYPE_L3_IPV4;
2114                 break;
2115         case PP2_INQ_L3_TYPE_IPV4_OK:
2116                 packet_type |= RTE_PTYPE_L3_IPV4_EXT;
2117                 break;
2118         case PP2_INQ_L3_TYPE_IPV4_TTL_ZERO:
2119                 packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
2120                 break;
2121         case PP2_INQ_L3_TYPE_IPV6_NO_EXT:
2122                 packet_type |= RTE_PTYPE_L3_IPV6;
2123                 break;
2124         case PP2_INQ_L3_TYPE_IPV6_EXT:
2125                 packet_type |= RTE_PTYPE_L3_IPV6_EXT;
2126                 break;
2127         case PP2_INQ_L3_TYPE_ARP:
2128                 packet_type |= RTE_PTYPE_L2_ETHER_ARP;
2129                 /*
2130                  * In case of ARP l4_offset is set to wrong value.
2131                  * Set it to proper one so that later on mbuf->l3_len can be
2132                  * calculated subtracting l4_offset and l3_offset.
2133                  */
2134                 *l4_offset = *l3_offset + MRVL_ARP_LENGTH;
2135                 break;
2136         default:
2137                 MRVL_LOG(DEBUG, "Failed to recognise l3 packet type");
2138                 break;
2139         }
2140
2141         switch (l4_type) {
2142         case PP2_INQ_L4_TYPE_TCP:
2143                 packet_type |= RTE_PTYPE_L4_TCP;
2144                 break;
2145         case PP2_INQ_L4_TYPE_UDP:
2146                 packet_type |= RTE_PTYPE_L4_UDP;
2147                 break;
2148         default:
2149                 MRVL_LOG(DEBUG, "Failed to recognise l4 packet type");
2150                 break;
2151         }
2152
2153         return packet_type;
2154 }
2155
2156 /**
2157  * Get offload information from the received packet descriptor.
2158  *
2159  * @param desc
2160  *   Pointer to the received packet descriptor.
2161  *
2162  * @return
2163  *   Mbuf offload flags.
2164  */
2165 static inline uint64_t
2166 mrvl_desc_to_ol_flags(struct pp2_ppio_desc *desc)
2167 {
2168         uint64_t flags;
2169         enum pp2_inq_desc_status status;
2170
2171         status = pp2_ppio_inq_desc_get_l3_pkt_error(desc);
2172         if (unlikely(status != PP2_DESC_ERR_OK))
2173                 flags = PKT_RX_IP_CKSUM_BAD;
2174         else
2175                 flags = PKT_RX_IP_CKSUM_GOOD;
2176
2177         status = pp2_ppio_inq_desc_get_l4_pkt_error(desc);
2178         if (unlikely(status != PP2_DESC_ERR_OK))
2179                 flags |= PKT_RX_L4_CKSUM_BAD;
2180         else
2181                 flags |= PKT_RX_L4_CKSUM_GOOD;
2182
2183         return flags;
2184 }
2185
2186 /**
2187  * DPDK callback for receive.
2188  *
2189  * @param rxq
2190  *   Generic pointer to the receive queue.
2191  * @param rx_pkts
2192  *   Array to store received packets.
2193  * @param nb_pkts
2194  *   Maximum number of packets in array.
2195  *
2196  * @return
2197  *   Number of packets successfully received.
2198  */
2199 static uint16_t
2200 mrvl_rx_pkt_burst(void *rxq, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
2201 {
2202         struct mrvl_rxq *q = rxq;
2203         struct pp2_ppio_desc descs[nb_pkts];
2204         struct pp2_bpool *bpool;
2205         int i, ret, rx_done = 0;
2206         int num;
2207         struct pp2_hif *hif;
2208         unsigned int core_id = rte_lcore_id();
2209
2210         hif = mrvl_get_hif(q->priv, core_id);
2211
2212         if (unlikely(!q->priv->ppio || !hif))
2213                 return 0;
2214
2215         bpool = q->priv->bpool;
2216
2217         ret = pp2_ppio_recv(q->priv->ppio, q->priv->rxq_map[q->queue_id].tc,
2218                             q->priv->rxq_map[q->queue_id].inq, descs, &nb_pkts);
2219         if (unlikely(ret < 0)) {
2220                 MRVL_LOG(ERR, "Failed to receive packets");
2221                 return 0;
2222         }
2223         mrvl_port_bpool_size[bpool->pp2_id][bpool->id][core_id] -= nb_pkts;
2224
2225         for (i = 0; i < nb_pkts; i++) {
2226                 struct rte_mbuf *mbuf;
2227                 uint8_t l3_offset, l4_offset;
2228                 enum pp2_inq_desc_status status;
2229                 uint64_t addr;
2230
2231                 if (likely(nb_pkts - i > MRVL_MUSDK_PREFETCH_SHIFT)) {
2232                         struct pp2_ppio_desc *pref_desc;
2233                         u64 pref_addr;
2234
2235                         pref_desc = &descs[i + MRVL_MUSDK_PREFETCH_SHIFT];
2236                         pref_addr = cookie_addr_high |
2237                                     pp2_ppio_inq_desc_get_cookie(pref_desc);
2238                         rte_mbuf_prefetch_part1((struct rte_mbuf *)(pref_addr));
2239                         rte_mbuf_prefetch_part2((struct rte_mbuf *)(pref_addr));
2240                 }
2241
2242                 addr = cookie_addr_high |
2243                        pp2_ppio_inq_desc_get_cookie(&descs[i]);
2244                 mbuf = (struct rte_mbuf *)addr;
2245                 rte_pktmbuf_reset(mbuf);
2246
2247                 /* drop packet in case of mac, overrun or resource error */
2248                 status = pp2_ppio_inq_desc_get_l2_pkt_error(&descs[i]);
2249                 if (unlikely(status != PP2_DESC_ERR_OK)) {
2250                         struct pp2_buff_inf binf = {
2251                                 .addr = rte_mbuf_data_iova_default(mbuf),
2252                                 .cookie = (uint64_t)mbuf,
2253                         };
2254
2255                         pp2_bpool_put_buff(hif, bpool, &binf);
2256                         mrvl_port_bpool_size
2257                                 [bpool->pp2_id][bpool->id][core_id]++;
2258                         q->drop_mac++;
2259                         continue;
2260                 }
2261
2262                 mbuf->data_off += MRVL_PKT_EFFEC_OFFS;
2263                 mbuf->pkt_len = pp2_ppio_inq_desc_get_pkt_len(&descs[i]);
2264                 mbuf->data_len = mbuf->pkt_len;
2265                 mbuf->port = q->port_id;
2266                 mbuf->packet_type =
2267                         mrvl_desc_to_packet_type_and_offset(&descs[i],
2268                                                             &l3_offset,
2269                                                             &l4_offset);
2270                 mbuf->l2_len = l3_offset;
2271                 mbuf->l3_len = l4_offset - l3_offset;
2272
2273                 if (likely(q->cksum_enabled))
2274                         mbuf->ol_flags = mrvl_desc_to_ol_flags(&descs[i]);
2275
2276                 rx_pkts[rx_done++] = mbuf;
2277                 q->bytes_recv += mbuf->pkt_len;
2278         }
2279
2280         if (rte_spinlock_trylock(&q->priv->lock) == 1) {
2281                 num = mrvl_get_bpool_size(bpool->pp2_id, bpool->id);
2282
2283                 if (unlikely(num <= q->priv->bpool_min_size ||
2284                              (!rx_done && num < q->priv->bpool_init_size))) {
2285                         ret = mrvl_fill_bpool(q, MRVL_BURST_SIZE);
2286                         if (ret)
2287                                 MRVL_LOG(ERR, "Failed to fill bpool");
2288                 } else if (unlikely(num > q->priv->bpool_max_size)) {
2289                         int i;
2290                         int pkt_to_remove = num - q->priv->bpool_init_size;
2291                         struct rte_mbuf *mbuf;
2292                         struct pp2_buff_inf buff;
2293
2294                         MRVL_LOG(DEBUG,
2295                                 "port-%d:%d: bpool %d oversize - remove %d buffers (pool size: %d -> %d)",
2296                                 bpool->pp2_id, q->priv->ppio->port_id,
2297                                 bpool->id, pkt_to_remove, num,
2298                                 q->priv->bpool_init_size);
2299
2300                         for (i = 0; i < pkt_to_remove; i++) {
2301                                 ret = pp2_bpool_get_buff(hif, bpool, &buff);
2302                                 if (ret)
2303                                         break;
2304                                 mbuf = (struct rte_mbuf *)
2305                                         (cookie_addr_high | buff.cookie);
2306                                 rte_pktmbuf_free(mbuf);
2307                         }
2308                         mrvl_port_bpool_size
2309                                 [bpool->pp2_id][bpool->id][core_id] -= i;
2310                 }
2311                 rte_spinlock_unlock(&q->priv->lock);
2312         }
2313
2314         return rx_done;
2315 }
2316
2317 /**
2318  * Prepare offload information.
2319  *
2320  * @param ol_flags
2321  *   Offload flags.
2322  * @param packet_type
2323  *   Packet type bitfield.
2324  * @param l3_type
2325  *   Pointer to the pp2_ouq_l3_type structure.
2326  * @param l4_type
2327  *   Pointer to the pp2_outq_l4_type structure.
2328  * @param gen_l3_cksum
2329  *   Will be set to 1 in case l3 checksum is computed.
2330  * @param l4_cksum
2331  *   Will be set to 1 in case l4 checksum is computed.
2332  *
2333  * @return
2334  *   0 on success, negative error value otherwise.
2335  */
2336 static inline int
2337 mrvl_prepare_proto_info(uint64_t ol_flags, uint32_t packet_type,
2338                         enum pp2_outq_l3_type *l3_type,
2339                         enum pp2_outq_l4_type *l4_type,
2340                         int *gen_l3_cksum,
2341                         int *gen_l4_cksum)
2342 {
2343         /*
2344          * Based on ol_flags prepare information
2345          * for pp2_ppio_outq_desc_set_proto_info() which setups descriptor
2346          * for offloading.
2347          */
2348         if (ol_flags & PKT_TX_IPV4) {
2349                 *l3_type = PP2_OUTQ_L3_TYPE_IPV4;
2350                 *gen_l3_cksum = ol_flags & PKT_TX_IP_CKSUM ? 1 : 0;
2351         } else if (ol_flags & PKT_TX_IPV6) {
2352                 *l3_type = PP2_OUTQ_L3_TYPE_IPV6;
2353                 /* no checksum for ipv6 header */
2354                 *gen_l3_cksum = 0;
2355         } else {
2356                 /* if something different then stop processing */
2357                 return -1;
2358         }
2359
2360         ol_flags &= PKT_TX_L4_MASK;
2361         if ((packet_type & RTE_PTYPE_L4_TCP) &&
2362             ol_flags == PKT_TX_TCP_CKSUM) {
2363                 *l4_type = PP2_OUTQ_L4_TYPE_TCP;
2364                 *gen_l4_cksum = 1;
2365         } else if ((packet_type & RTE_PTYPE_L4_UDP) &&
2366                    ol_flags == PKT_TX_UDP_CKSUM) {
2367                 *l4_type = PP2_OUTQ_L4_TYPE_UDP;
2368                 *gen_l4_cksum = 1;
2369         } else {
2370                 *l4_type = PP2_OUTQ_L4_TYPE_OTHER;
2371                 /* no checksum for other type */
2372                 *gen_l4_cksum = 0;
2373         }
2374
2375         return 0;
2376 }
2377
2378 /**
2379  * Release already sent buffers to bpool (buffer-pool).
2380  *
2381  * @param ppio
2382  *   Pointer to the port structure.
2383  * @param hif
2384  *   Pointer to the MUSDK hardware interface.
2385  * @param sq
2386  *   Pointer to the shadow queue.
2387  * @param qid
2388  *   Queue id number.
2389  * @param force
2390  *   Force releasing packets.
2391  */
2392 static inline void
2393 mrvl_free_sent_buffers(struct pp2_ppio *ppio, struct pp2_hif *hif,
2394                        unsigned int core_id, struct mrvl_shadow_txq *sq,
2395                        int qid, int force)
2396 {
2397         struct buff_release_entry *entry;
2398         uint16_t nb_done = 0, num = 0, skip_bufs = 0;
2399         int i;
2400
2401         pp2_ppio_get_num_outq_done(ppio, hif, qid, &nb_done);
2402
2403         sq->num_to_release += nb_done;
2404
2405         if (likely(!force &&
2406                    sq->num_to_release < MRVL_PP2_BUF_RELEASE_BURST_SIZE))
2407                 return;
2408
2409         nb_done = sq->num_to_release;
2410         sq->num_to_release = 0;
2411
2412         for (i = 0; i < nb_done; i++) {
2413                 entry = &sq->ent[sq->tail + num];
2414                 if (unlikely(!entry->buff.addr)) {
2415                         MRVL_LOG(ERR,
2416                                 "Shadow memory @%d: cookie(%lx), pa(%lx)!",
2417                                 sq->tail, (u64)entry->buff.cookie,
2418                                 (u64)entry->buff.addr);
2419                         skip_bufs = 1;
2420                         goto skip;
2421                 }
2422
2423                 if (unlikely(!entry->bpool)) {
2424                         struct rte_mbuf *mbuf;
2425
2426                         mbuf = (struct rte_mbuf *)
2427                                (cookie_addr_high | entry->buff.cookie);
2428                         rte_pktmbuf_free(mbuf);
2429                         skip_bufs = 1;
2430                         goto skip;
2431                 }
2432
2433                 mrvl_port_bpool_size
2434                         [entry->bpool->pp2_id][entry->bpool->id][core_id]++;
2435                 num++;
2436                 if (unlikely(sq->tail + num == MRVL_PP2_TX_SHADOWQ_SIZE))
2437                         goto skip;
2438                 continue;
2439 skip:
2440                 if (likely(num))
2441                         pp2_bpool_put_buffs(hif, &sq->ent[sq->tail], &num);
2442                 num += skip_bufs;
2443                 sq->tail = (sq->tail + num) & MRVL_PP2_TX_SHADOWQ_MASK;
2444                 sq->size -= num;
2445                 num = 0;
2446                 skip_bufs = 0;
2447         }
2448
2449         if (likely(num)) {
2450                 pp2_bpool_put_buffs(hif, &sq->ent[sq->tail], &num);
2451                 sq->tail = (sq->tail + num) & MRVL_PP2_TX_SHADOWQ_MASK;
2452                 sq->size -= num;
2453         }
2454 }
2455
2456 /**
2457  * DPDK callback for transmit.
2458  *
2459  * @param txq
2460  *   Generic pointer transmit queue.
2461  * @param tx_pkts
2462  *   Packets to transmit.
2463  * @param nb_pkts
2464  *   Number of packets in array.
2465  *
2466  * @return
2467  *   Number of packets successfully transmitted.
2468  */
2469 static uint16_t
2470 mrvl_tx_pkt_burst(void *txq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2471 {
2472         struct mrvl_txq *q = txq;
2473         struct mrvl_shadow_txq *sq;
2474         struct pp2_hif *hif;
2475         struct pp2_ppio_desc descs[nb_pkts];
2476         unsigned int core_id = rte_lcore_id();
2477         int i, ret, bytes_sent = 0;
2478         uint16_t num, sq_free_size;
2479         uint64_t addr;
2480
2481         hif = mrvl_get_hif(q->priv, core_id);
2482         sq = &q->shadow_txqs[core_id];
2483
2484         if (unlikely(!q->priv->ppio || !hif))
2485                 return 0;
2486
2487         if (sq->size)
2488                 mrvl_free_sent_buffers(q->priv->ppio, hif, core_id,
2489                                        sq, q->queue_id, 0);
2490
2491         sq_free_size = MRVL_PP2_TX_SHADOWQ_SIZE - sq->size - 1;
2492         if (unlikely(nb_pkts > sq_free_size)) {
2493                 MRVL_LOG(DEBUG,
2494                         "No room in shadow queue for %d packets! %d packets will be sent.",
2495                         nb_pkts, sq_free_size);
2496                 nb_pkts = sq_free_size;
2497         }
2498
2499         for (i = 0; i < nb_pkts; i++) {
2500                 struct rte_mbuf *mbuf = tx_pkts[i];
2501                 int gen_l3_cksum, gen_l4_cksum;
2502                 enum pp2_outq_l3_type l3_type;
2503                 enum pp2_outq_l4_type l4_type;
2504
2505                 if (likely(nb_pkts - i > MRVL_MUSDK_PREFETCH_SHIFT)) {
2506                         struct rte_mbuf *pref_pkt_hdr;
2507
2508                         pref_pkt_hdr = tx_pkts[i + MRVL_MUSDK_PREFETCH_SHIFT];
2509                         rte_mbuf_prefetch_part1(pref_pkt_hdr);
2510                         rte_mbuf_prefetch_part2(pref_pkt_hdr);
2511                 }
2512
2513                 mrvl_fill_shadowq(sq, mbuf);
2514                 mrvl_fill_desc(&descs[i], mbuf);
2515
2516                 bytes_sent += rte_pktmbuf_pkt_len(mbuf);
2517                 /*
2518                  * in case unsupported ol_flags were passed
2519                  * do not update descriptor offload information
2520                  */
2521                 ret = mrvl_prepare_proto_info(mbuf->ol_flags, mbuf->packet_type,
2522                                               &l3_type, &l4_type, &gen_l3_cksum,
2523                                               &gen_l4_cksum);
2524                 if (unlikely(ret))
2525                         continue;
2526
2527                 pp2_ppio_outq_desc_set_proto_info(&descs[i], l3_type, l4_type,
2528                                                   mbuf->l2_len,
2529                                                   mbuf->l2_len + mbuf->l3_len,
2530                                                   gen_l3_cksum, gen_l4_cksum);
2531         }
2532
2533         num = nb_pkts;
2534         pp2_ppio_send(q->priv->ppio, hif, q->queue_id, descs, &nb_pkts);
2535         /* number of packets that were not sent */
2536         if (unlikely(num > nb_pkts)) {
2537                 for (i = nb_pkts; i < num; i++) {
2538                         sq->head = (MRVL_PP2_TX_SHADOWQ_SIZE + sq->head - 1) &
2539                                 MRVL_PP2_TX_SHADOWQ_MASK;
2540                         addr = cookie_addr_high | sq->ent[sq->head].buff.cookie;
2541                         bytes_sent -=
2542                                 rte_pktmbuf_pkt_len((struct rte_mbuf *)addr);
2543                 }
2544                 sq->size -= num - nb_pkts;
2545         }
2546
2547         q->bytes_sent += bytes_sent;
2548
2549         return nb_pkts;
2550 }
2551
2552 /** DPDK callback for S/G transmit.
2553  *
2554  * @param txq
2555  *   Generic pointer transmit queue.
2556  * @param tx_pkts
2557  *   Packets to transmit.
2558  * @param nb_pkts
2559  *   Number of packets in array.
2560  *
2561  * @return
2562  *   Number of packets successfully transmitted.
2563  */
2564 static uint16_t
2565 mrvl_tx_sg_pkt_burst(void *txq, struct rte_mbuf **tx_pkts,
2566                      uint16_t nb_pkts)
2567 {
2568         struct mrvl_txq *q = txq;
2569         struct mrvl_shadow_txq *sq;
2570         struct pp2_hif *hif;
2571         struct pp2_ppio_desc descs[nb_pkts * PP2_PPIO_DESC_NUM_FRAGS];
2572         struct pp2_ppio_sg_pkts pkts;
2573         uint8_t frags[nb_pkts];
2574         unsigned int core_id = rte_lcore_id();
2575         int i, j, ret, bytes_sent = 0;
2576         int tail, tail_first;
2577         uint16_t num, sq_free_size;
2578         uint16_t nb_segs, total_descs = 0;
2579         uint64_t addr;
2580
2581         hif = mrvl_get_hif(q->priv, core_id);
2582         sq = &q->shadow_txqs[core_id];
2583         pkts.frags = frags;
2584         pkts.num = 0;
2585
2586         if (unlikely(!q->priv->ppio || !hif))
2587                 return 0;
2588
2589         if (sq->size)
2590                 mrvl_free_sent_buffers(q->priv->ppio, hif, core_id,
2591                                        sq, q->queue_id, 0);
2592
2593         /* Save shadow queue free size */
2594         sq_free_size = MRVL_PP2_TX_SHADOWQ_SIZE - sq->size - 1;
2595
2596         tail = 0;
2597         for (i = 0; i < nb_pkts; i++) {
2598                 struct rte_mbuf *mbuf = tx_pkts[i];
2599                 struct rte_mbuf *seg = NULL;
2600                 int gen_l3_cksum, gen_l4_cksum;
2601                 enum pp2_outq_l3_type l3_type;
2602                 enum pp2_outq_l4_type l4_type;
2603
2604                 nb_segs = mbuf->nb_segs;
2605                 tail_first = tail;
2606                 total_descs += nb_segs;
2607
2608                 /*
2609                  * Check if total_descs does not exceed
2610                  * shadow queue free size
2611                  */
2612                 if (unlikely(total_descs > sq_free_size)) {
2613                         total_descs -= nb_segs;
2614                         RTE_LOG(DEBUG, PMD,
2615                                 "No room in shadow queue for %d packets! "
2616                                 "%d packets will be sent.\n",
2617                                 nb_pkts, i);
2618                         break;
2619                 }
2620
2621                 /* Check if nb_segs does not exceed the max nb of desc per
2622                  * fragmented packet
2623                  */
2624                 if (nb_segs > PP2_PPIO_DESC_NUM_FRAGS) {
2625                         total_descs -= nb_segs;
2626                         RTE_LOG(ERR, PMD,
2627                                 "Too many segments. Packet won't be sent.\n");
2628                         break;
2629                 }
2630
2631                 if (likely(nb_pkts - i > MRVL_MUSDK_PREFETCH_SHIFT)) {
2632                         struct rte_mbuf *pref_pkt_hdr;
2633
2634                         pref_pkt_hdr = tx_pkts[i + MRVL_MUSDK_PREFETCH_SHIFT];
2635                         rte_mbuf_prefetch_part1(pref_pkt_hdr);
2636                         rte_mbuf_prefetch_part2(pref_pkt_hdr);
2637                 }
2638
2639                 pkts.frags[pkts.num] = nb_segs;
2640                 pkts.num++;
2641
2642                 seg = mbuf;
2643                 for (j = 0; j < nb_segs - 1; j++) {
2644                         /* For the subsequent segments, set shadow queue
2645                          * buffer to NULL
2646                          */
2647                         mrvl_fill_shadowq(sq, NULL);
2648                         mrvl_fill_desc(&descs[tail], seg);
2649
2650                         tail++;
2651                         seg = seg->next;
2652                 }
2653                 /* Put first mbuf info in last shadow queue entry */
2654                 mrvl_fill_shadowq(sq, mbuf);
2655                 /* Update descriptor with last segment */
2656                 mrvl_fill_desc(&descs[tail++], seg);
2657
2658                 bytes_sent += rte_pktmbuf_pkt_len(mbuf);
2659                 /* In case unsupported ol_flags were passed
2660                  * do not update descriptor offload information
2661                  */
2662                 ret = mrvl_prepare_proto_info(mbuf->ol_flags, mbuf->packet_type,
2663                                               &l3_type, &l4_type, &gen_l3_cksum,
2664                                               &gen_l4_cksum);
2665                 if (unlikely(ret))
2666                         continue;
2667
2668                 pp2_ppio_outq_desc_set_proto_info(&descs[tail_first], l3_type,
2669                                                   l4_type, mbuf->l2_len,
2670                                                   mbuf->l2_len + mbuf->l3_len,
2671                                                   gen_l3_cksum, gen_l4_cksum);
2672         }
2673
2674         num = total_descs;
2675         pp2_ppio_send_sg(q->priv->ppio, hif, q->queue_id, descs,
2676                          &total_descs, &pkts);
2677         /* number of packets that were not sent */
2678         if (unlikely(num > total_descs)) {
2679                 for (i = total_descs; i < num; i++) {
2680                         sq->head = (MRVL_PP2_TX_SHADOWQ_SIZE + sq->head - 1) &
2681                                 MRVL_PP2_TX_SHADOWQ_MASK;
2682
2683                         addr = sq->ent[sq->head].buff.cookie;
2684                         if (addr)
2685                                 bytes_sent -=
2686                                         rte_pktmbuf_pkt_len((struct rte_mbuf *)
2687                                                 (cookie_addr_high | addr));
2688                 }
2689                 sq->size -= num - total_descs;
2690                 nb_pkts = pkts.num;
2691         }
2692
2693         q->bytes_sent += bytes_sent;
2694
2695         return nb_pkts;
2696 }
2697
2698 /**
2699  * Initialize packet processor.
2700  *
2701  * @return
2702  *   0 on success, negative error value otherwise.
2703  */
2704 static int
2705 mrvl_init_pp2(void)
2706 {
2707         struct pp2_init_params init_params;
2708
2709         memset(&init_params, 0, sizeof(init_params));
2710         init_params.hif_reserved_map = MRVL_MUSDK_HIFS_RESERVED;
2711         init_params.bm_pool_reserved_map = MRVL_MUSDK_BPOOLS_RESERVED;
2712         init_params.rss_tbl_reserved_map = MRVL_MUSDK_RSS_RESERVED;
2713
2714         return pp2_init(&init_params);
2715 }
2716
2717 /**
2718  * Deinitialize packet processor.
2719  *
2720  * @return
2721  *   0 on success, negative error value otherwise.
2722  */
2723 static void
2724 mrvl_deinit_pp2(void)
2725 {
2726         pp2_deinit();
2727 }
2728
2729 /**
2730  * Create private device structure.
2731  *
2732  * @param dev_name
2733  *   Pointer to the port name passed in the initialization parameters.
2734  *
2735  * @return
2736  *   Pointer to the newly allocated private device structure.
2737  */
2738 static struct mrvl_priv *
2739 mrvl_priv_create(const char *dev_name)
2740 {
2741         struct pp2_bpool_params bpool_params;
2742         char match[MRVL_MATCH_LEN];
2743         struct mrvl_priv *priv;
2744         int ret, bpool_bit;
2745
2746         priv = rte_zmalloc_socket(dev_name, sizeof(*priv), 0, rte_socket_id());
2747         if (!priv)
2748                 return NULL;
2749
2750         ret = pp2_netdev_get_ppio_info((char *)(uintptr_t)dev_name,
2751                                        &priv->pp_id, &priv->ppio_id);
2752         if (ret)
2753                 goto out_free_priv;
2754
2755         bpool_bit = mrvl_reserve_bit(&used_bpools[priv->pp_id],
2756                                      PP2_BPOOL_NUM_POOLS);
2757         if (bpool_bit < 0)
2758                 goto out_free_priv;
2759         priv->bpool_bit = bpool_bit;
2760
2761         snprintf(match, sizeof(match), "pool-%d:%d", priv->pp_id,
2762                  priv->bpool_bit);
2763         memset(&bpool_params, 0, sizeof(bpool_params));
2764         bpool_params.match = match;
2765         bpool_params.buff_len = MRVL_PKT_SIZE_MAX + MRVL_PKT_EFFEC_OFFS;
2766         ret = pp2_bpool_init(&bpool_params, &priv->bpool);
2767         if (ret)
2768                 goto out_clear_bpool_bit;
2769
2770         priv->ppio_params.type = PP2_PPIO_T_NIC;
2771         rte_spinlock_init(&priv->lock);
2772
2773         return priv;
2774 out_clear_bpool_bit:
2775         used_bpools[priv->pp_id] &= ~(1 << priv->bpool_bit);
2776 out_free_priv:
2777         rte_free(priv);
2778         return NULL;
2779 }
2780
2781 /**
2782  * Create device representing Ethernet port.
2783  *
2784  * @param name
2785  *   Pointer to the port's name.
2786  *
2787  * @return
2788  *   0 on success, negative error value otherwise.
2789  */
2790 static int
2791 mrvl_eth_dev_create(struct rte_vdev_device *vdev, const char *name)
2792 {
2793         int ret, fd = socket(AF_INET, SOCK_DGRAM, 0);
2794         struct rte_eth_dev *eth_dev;
2795         struct mrvl_priv *priv;
2796         struct ifreq req;
2797
2798         eth_dev = rte_eth_dev_allocate(name);
2799         if (!eth_dev)
2800                 return -ENOMEM;
2801
2802         priv = mrvl_priv_create(name);
2803         if (!priv) {
2804                 ret = -ENOMEM;
2805                 goto out_free;
2806         }
2807         eth_dev->data->dev_private = priv;
2808
2809         eth_dev->data->mac_addrs =
2810                 rte_zmalloc("mac_addrs",
2811                             RTE_ETHER_ADDR_LEN * MRVL_MAC_ADDRS_MAX, 0);
2812         if (!eth_dev->data->mac_addrs) {
2813                 MRVL_LOG(ERR, "Failed to allocate space for eth addrs");
2814                 ret = -ENOMEM;
2815                 goto out_free;
2816         }
2817
2818         memset(&req, 0, sizeof(req));
2819         strcpy(req.ifr_name, name);
2820         ret = ioctl(fd, SIOCGIFHWADDR, &req);
2821         if (ret)
2822                 goto out_free;
2823
2824         memcpy(eth_dev->data->mac_addrs[0].addr_bytes,
2825                req.ifr_addr.sa_data, RTE_ETHER_ADDR_LEN);
2826
2827         eth_dev->data->kdrv = RTE_KDRV_NONE;
2828         eth_dev->device = &vdev->device;
2829         eth_dev->rx_pkt_burst = mrvl_rx_pkt_burst;
2830         mrvl_set_tx_function(eth_dev);
2831         eth_dev->dev_ops = &mrvl_ops;
2832
2833         /* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
2834         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
2835
2836         rte_eth_dev_probing_finish(eth_dev);
2837         return 0;
2838 out_free:
2839         rte_eth_dev_release_port(eth_dev);
2840
2841         return ret;
2842 }
2843
2844 /**
2845  * Callback used by rte_kvargs_process() during argument parsing.
2846  *
2847  * @param key
2848  *   Pointer to the parsed key (unused).
2849  * @param value
2850  *   Pointer to the parsed value.
2851  * @param extra_args
2852  *   Pointer to the extra arguments which contains address of the
2853  *   table of pointers to parsed interface names.
2854  *
2855  * @return
2856  *   Always 0.
2857  */
2858 static int
2859 mrvl_get_ifnames(const char *key __rte_unused, const char *value,
2860                  void *extra_args)
2861 {
2862         struct mrvl_ifnames *ifnames = extra_args;
2863
2864         ifnames->names[ifnames->idx++] = value;
2865
2866         return 0;
2867 }
2868
2869 /**
2870  * Deinitialize per-lcore MUSDK hardware interfaces (hifs).
2871  */
2872 static void
2873 mrvl_deinit_hifs(void)
2874 {
2875         int i;
2876
2877         for (i = mrvl_lcore_first; i <= mrvl_lcore_last; i++) {
2878                 if (hifs[i])
2879                         pp2_hif_deinit(hifs[i]);
2880         }
2881         used_hifs = MRVL_MUSDK_HIFS_RESERVED;
2882         memset(hifs, 0, sizeof(hifs));
2883 }
2884
2885 /**
2886  * DPDK callback to register the virtual device.
2887  *
2888  * @param vdev
2889  *   Pointer to the virtual device.
2890  *
2891  * @return
2892  *   0 on success, negative error value otherwise.
2893  */
2894 static int
2895 rte_pmd_mrvl_probe(struct rte_vdev_device *vdev)
2896 {
2897         struct rte_kvargs *kvlist;
2898         struct mrvl_ifnames ifnames;
2899         int ret = -EINVAL;
2900         uint32_t i, ifnum, cfgnum;
2901         const char *params;
2902
2903         params = rte_vdev_device_args(vdev);
2904         if (!params)
2905                 return -EINVAL;
2906
2907         kvlist = rte_kvargs_parse(params, valid_args);
2908         if (!kvlist)
2909                 return -EINVAL;
2910
2911         ifnum = rte_kvargs_count(kvlist, MRVL_IFACE_NAME_ARG);
2912         if (ifnum > RTE_DIM(ifnames.names))
2913                 goto out_free_kvlist;
2914
2915         ifnames.idx = 0;
2916         rte_kvargs_process(kvlist, MRVL_IFACE_NAME_ARG,
2917                            mrvl_get_ifnames, &ifnames);
2918
2919
2920         /*
2921          * The below system initialization should be done only once,
2922          * on the first provided configuration file
2923          */
2924         if (!mrvl_qos_cfg) {
2925                 cfgnum = rte_kvargs_count(kvlist, MRVL_CFG_ARG);
2926                 MRVL_LOG(INFO, "Parsing config file!");
2927                 if (cfgnum > 1) {
2928                         MRVL_LOG(ERR, "Cannot handle more than one config file!");
2929                         goto out_free_kvlist;
2930                 } else if (cfgnum == 1) {
2931                         rte_kvargs_process(kvlist, MRVL_CFG_ARG,
2932                                            mrvl_get_qoscfg, &mrvl_qos_cfg);
2933                 }
2934         }
2935
2936         if (mrvl_dev_num)
2937                 goto init_devices;
2938
2939         MRVL_LOG(INFO, "Perform MUSDK initializations");
2940
2941         ret = rte_mvep_init(MVEP_MOD_T_PP2, kvlist);
2942         if (ret)
2943                 goto out_free_kvlist;
2944
2945         ret = mrvl_init_pp2();
2946         if (ret) {
2947                 MRVL_LOG(ERR, "Failed to init PP!");
2948                 rte_mvep_deinit(MVEP_MOD_T_PP2);
2949                 goto out_free_kvlist;
2950         }
2951
2952         memset(mrvl_port_bpool_size, 0, sizeof(mrvl_port_bpool_size));
2953         memset(mrvl_port_to_bpool_lookup, 0, sizeof(mrvl_port_to_bpool_lookup));
2954
2955         mrvl_lcore_first = RTE_MAX_LCORE;
2956         mrvl_lcore_last = 0;
2957
2958 init_devices:
2959         for (i = 0; i < ifnum; i++) {
2960                 MRVL_LOG(INFO, "Creating %s", ifnames.names[i]);
2961                 ret = mrvl_eth_dev_create(vdev, ifnames.names[i]);
2962                 if (ret)
2963                         goto out_cleanup;
2964                 mrvl_dev_num++;
2965         }
2966
2967         rte_kvargs_free(kvlist);
2968
2969         return 0;
2970 out_cleanup:
2971         rte_pmd_mrvl_remove(vdev);
2972
2973 out_free_kvlist:
2974         rte_kvargs_free(kvlist);
2975
2976         return ret;
2977 }
2978
2979 /**
2980  * DPDK callback to remove virtual device.
2981  *
2982  * @param vdev
2983  *   Pointer to the removed virtual device.
2984  *
2985  * @return
2986  *   0 on success, negative error value otherwise.
2987  */
2988 static int
2989 rte_pmd_mrvl_remove(struct rte_vdev_device *vdev)
2990 {
2991         uint16_t port_id;
2992
2993         RTE_ETH_FOREACH_DEV(port_id) {
2994                 if (rte_eth_devices[port_id].device != &vdev->device)
2995                         continue;
2996                 rte_eth_dev_close(port_id);
2997         }
2998
2999         return 0;
3000 }
3001
3002 static struct rte_vdev_driver pmd_mrvl_drv = {
3003         .probe = rte_pmd_mrvl_probe,
3004         .remove = rte_pmd_mrvl_remove,
3005 };
3006
3007 RTE_PMD_REGISTER_VDEV(net_mvpp2, pmd_mrvl_drv);
3008 RTE_PMD_REGISTER_ALIAS(net_mvpp2, eth_mvpp2);
3009
3010 RTE_INIT(mrvl_init_log)
3011 {
3012         mrvl_logtype = rte_log_register("pmd.net.mvpp2");
3013         if (mrvl_logtype >= 0)
3014                 rte_log_set_level(mrvl_logtype, RTE_LOG_NOTICE);
3015 }