11f8c786bdf29a112db4ed90bd13fd62761f796e
[dpdk.git] / drivers / net / octeontx2 / otx2_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <inttypes.h>
6
7 #include <rte_ethdev_pci.h>
8 #include <rte_io.h>
9 #include <rte_malloc.h>
10 #include <rte_mbuf.h>
11 #include <rte_mbuf_pool_ops.h>
12 #include <rte_mempool.h>
13
14 #include "otx2_ethdev.h"
15
16 static inline uint64_t
17 nix_get_rx_offload_capa(struct otx2_eth_dev *dev)
18 {
19         uint64_t capa = NIX_RX_OFFLOAD_CAPA;
20
21         if (otx2_dev_is_vf(dev) ||
22             dev->npc_flow.switch_header_type == OTX2_PRIV_FLAGS_HIGIG)
23                 capa &= ~DEV_RX_OFFLOAD_TIMESTAMP;
24
25         return capa;
26 }
27
28 static inline uint64_t
29 nix_get_tx_offload_capa(struct otx2_eth_dev *dev)
30 {
31         uint64_t capa = NIX_TX_OFFLOAD_CAPA;
32
33         /* TSO not supported for earlier chip revisions */
34         if (otx2_dev_is_96xx_A0(dev) || otx2_dev_is_95xx_Ax(dev))
35                 capa &= ~(DEV_TX_OFFLOAD_TCP_TSO |
36                           DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
37                           DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
38                           DEV_TX_OFFLOAD_GRE_TNL_TSO);
39         return capa;
40 }
41
42 static const struct otx2_dev_ops otx2_dev_ops = {
43         .link_status_update = otx2_eth_dev_link_status_update,
44         .ptp_info_update = otx2_eth_dev_ptp_info_update
45 };
46
47 static int
48 nix_lf_alloc(struct otx2_eth_dev *dev, uint32_t nb_rxq, uint32_t nb_txq)
49 {
50         struct otx2_mbox *mbox = dev->mbox;
51         struct nix_lf_alloc_req *req;
52         struct nix_lf_alloc_rsp *rsp;
53         int rc;
54
55         req = otx2_mbox_alloc_msg_nix_lf_alloc(mbox);
56         req->rq_cnt = nb_rxq;
57         req->sq_cnt = nb_txq;
58         req->cq_cnt = nb_rxq;
59         /* XQE_SZ should be in Sync with NIX_CQ_ENTRY_SZ */
60         RTE_BUILD_BUG_ON(NIX_CQ_ENTRY_SZ != 128);
61         req->xqe_sz = NIX_XQESZ_W16;
62         req->rss_sz = dev->rss_info.rss_size;
63         req->rss_grps = NIX_RSS_GRPS;
64         req->npa_func = otx2_npa_pf_func_get();
65         req->sso_func = otx2_sso_pf_func_get();
66         req->rx_cfg = BIT_ULL(35 /* DIS_APAD */);
67         if (dev->rx_offloads & (DEV_RX_OFFLOAD_TCP_CKSUM |
68                          DEV_RX_OFFLOAD_UDP_CKSUM)) {
69                 req->rx_cfg |= BIT_ULL(37 /* CSUM_OL4 */);
70                 req->rx_cfg |= BIT_ULL(36 /* CSUM_IL4 */);
71         }
72         req->rx_cfg |= BIT_ULL(32 /* DROP_RE */);
73
74         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
75         if (rc)
76                 return rc;
77
78         dev->sqb_size = rsp->sqb_size;
79         dev->tx_chan_base = rsp->tx_chan_base;
80         dev->rx_chan_base = rsp->rx_chan_base;
81         dev->rx_chan_cnt = rsp->rx_chan_cnt;
82         dev->tx_chan_cnt = rsp->tx_chan_cnt;
83         dev->lso_tsov4_idx = rsp->lso_tsov4_idx;
84         dev->lso_tsov6_idx = rsp->lso_tsov6_idx;
85         dev->lf_tx_stats = rsp->lf_tx_stats;
86         dev->lf_rx_stats = rsp->lf_rx_stats;
87         dev->cints = rsp->cints;
88         dev->qints = rsp->qints;
89         dev->npc_flow.channel = dev->rx_chan_base;
90         dev->ptp_en = rsp->hw_rx_tstamp_en;
91
92         return 0;
93 }
94
95 static int
96 nix_lf_switch_header_type_enable(struct otx2_eth_dev *dev, bool enable)
97 {
98         struct otx2_mbox *mbox = dev->mbox;
99         struct npc_set_pkind *req;
100         struct msg_resp *rsp;
101         int rc;
102
103         if (dev->npc_flow.switch_header_type == 0)
104                 return 0;
105
106         /* Notify AF about higig2 config */
107         req = otx2_mbox_alloc_msg_npc_set_pkind(mbox);
108         req->mode = dev->npc_flow.switch_header_type;
109         if (enable == 0)
110                 req->mode = OTX2_PRIV_FLAGS_DEFAULT;
111         req->dir = PKIND_RX;
112         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
113         if (rc)
114                 return rc;
115         req = otx2_mbox_alloc_msg_npc_set_pkind(mbox);
116         req->mode = dev->npc_flow.switch_header_type;
117         if (enable == 0)
118                 req->mode = OTX2_PRIV_FLAGS_DEFAULT;
119         req->dir = PKIND_TX;
120         return otx2_mbox_process_msg(mbox, (void *)&rsp);
121 }
122
123 static int
124 nix_lf_free(struct otx2_eth_dev *dev)
125 {
126         struct otx2_mbox *mbox = dev->mbox;
127         struct nix_lf_free_req *req;
128         struct ndc_sync_op *ndc_req;
129         int rc;
130
131         /* Sync NDC-NIX for LF */
132         ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
133         ndc_req->nix_lf_tx_sync = 1;
134         ndc_req->nix_lf_rx_sync = 1;
135         rc = otx2_mbox_process(mbox);
136         if (rc)
137                 otx2_err("Error on NDC-NIX-[TX, RX] LF sync, rc %d", rc);
138
139         req = otx2_mbox_alloc_msg_nix_lf_free(mbox);
140         /* Let AF driver free all this nix lf's
141          * NPC entries allocated using NPC MBOX.
142          */
143         req->flags = 0;
144
145         return otx2_mbox_process(mbox);
146 }
147
148 int
149 otx2_cgx_rxtx_start(struct otx2_eth_dev *dev)
150 {
151         struct otx2_mbox *mbox = dev->mbox;
152
153         if (otx2_dev_is_vf_or_sdp(dev))
154                 return 0;
155
156         otx2_mbox_alloc_msg_cgx_start_rxtx(mbox);
157
158         return otx2_mbox_process(mbox);
159 }
160
161 int
162 otx2_cgx_rxtx_stop(struct otx2_eth_dev *dev)
163 {
164         struct otx2_mbox *mbox = dev->mbox;
165
166         if (otx2_dev_is_vf_or_sdp(dev))
167                 return 0;
168
169         otx2_mbox_alloc_msg_cgx_stop_rxtx(mbox);
170
171         return otx2_mbox_process(mbox);
172 }
173
174 static int
175 npc_rx_enable(struct otx2_eth_dev *dev)
176 {
177         struct otx2_mbox *mbox = dev->mbox;
178
179         otx2_mbox_alloc_msg_nix_lf_start_rx(mbox);
180
181         return otx2_mbox_process(mbox);
182 }
183
184 static int
185 npc_rx_disable(struct otx2_eth_dev *dev)
186 {
187         struct otx2_mbox *mbox = dev->mbox;
188
189         otx2_mbox_alloc_msg_nix_lf_stop_rx(mbox);
190
191         return otx2_mbox_process(mbox);
192 }
193
194 static int
195 nix_cgx_start_link_event(struct otx2_eth_dev *dev)
196 {
197         struct otx2_mbox *mbox = dev->mbox;
198
199         if (otx2_dev_is_vf_or_sdp(dev))
200                 return 0;
201
202         otx2_mbox_alloc_msg_cgx_start_linkevents(mbox);
203
204         return otx2_mbox_process(mbox);
205 }
206
207 static int
208 cgx_intlbk_enable(struct otx2_eth_dev *dev, bool en)
209 {
210         struct otx2_mbox *mbox = dev->mbox;
211
212         if (en && otx2_dev_is_vf_or_sdp(dev))
213                 return -ENOTSUP;
214
215         if (en)
216                 otx2_mbox_alloc_msg_cgx_intlbk_enable(mbox);
217         else
218                 otx2_mbox_alloc_msg_cgx_intlbk_disable(mbox);
219
220         return otx2_mbox_process(mbox);
221 }
222
223 static int
224 nix_cgx_stop_link_event(struct otx2_eth_dev *dev)
225 {
226         struct otx2_mbox *mbox = dev->mbox;
227
228         if (otx2_dev_is_vf_or_sdp(dev))
229                 return 0;
230
231         otx2_mbox_alloc_msg_cgx_stop_linkevents(mbox);
232
233         return otx2_mbox_process(mbox);
234 }
235
236 static inline void
237 nix_rx_queue_reset(struct otx2_eth_rxq *rxq)
238 {
239         rxq->head = 0;
240         rxq->available = 0;
241 }
242
243 static inline uint32_t
244 nix_qsize_to_val(enum nix_q_size_e qsize)
245 {
246         return (16UL << (qsize * 2));
247 }
248
249 static inline enum nix_q_size_e
250 nix_qsize_clampup_get(struct otx2_eth_dev *dev, uint32_t val)
251 {
252         int i;
253
254         if (otx2_ethdev_fixup_is_min_4k_q(dev))
255                 i = nix_q_size_4K;
256         else
257                 i = nix_q_size_16;
258
259         for (; i < nix_q_size_max; i++)
260                 if (val <= nix_qsize_to_val(i))
261                         break;
262
263         if (i >= nix_q_size_max)
264                 i = nix_q_size_max - 1;
265
266         return i;
267 }
268
269 static int
270 nix_cq_rq_init(struct rte_eth_dev *eth_dev, struct otx2_eth_dev *dev,
271                uint16_t qid, struct otx2_eth_rxq *rxq, struct rte_mempool *mp)
272 {
273         struct otx2_mbox *mbox = dev->mbox;
274         const struct rte_memzone *rz;
275         uint32_t ring_size, cq_size;
276         struct nix_aq_enq_req *aq;
277         uint16_t first_skip;
278         int rc;
279
280         cq_size = rxq->qlen;
281         ring_size = cq_size * NIX_CQ_ENTRY_SZ;
282         rz = rte_eth_dma_zone_reserve(eth_dev, "cq", qid, ring_size,
283                                       NIX_CQ_ALIGN, dev->node);
284         if (rz == NULL) {
285                 otx2_err("Failed to allocate mem for cq hw ring");
286                 rc = -ENOMEM;
287                 goto fail;
288         }
289         memset(rz->addr, 0, rz->len);
290         rxq->desc = (uintptr_t)rz->addr;
291         rxq->qmask = cq_size - 1;
292
293         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
294         aq->qidx = qid;
295         aq->ctype = NIX_AQ_CTYPE_CQ;
296         aq->op = NIX_AQ_INSTOP_INIT;
297
298         aq->cq.ena = 1;
299         aq->cq.caching = 1;
300         aq->cq.qsize = rxq->qsize;
301         aq->cq.base = rz->iova;
302         aq->cq.avg_level = 0xff;
303         aq->cq.cq_err_int_ena = BIT(NIX_CQERRINT_CQE_FAULT);
304         aq->cq.cq_err_int_ena |= BIT(NIX_CQERRINT_DOOR_ERR);
305
306         /* Many to one reduction */
307         aq->cq.qint_idx = qid % dev->qints;
308         /* Map CQ0 [RQ0] to CINT0 and so on till max 64 irqs */
309         aq->cq.cint_idx = qid;
310
311         if (otx2_ethdev_fixup_is_limit_cq_full(dev)) {
312                 const float rx_cq_skid = NIX_CQ_FULL_ERRATA_SKID;
313                 uint16_t min_rx_drop;
314
315                 min_rx_drop = ceil(rx_cq_skid / (float)cq_size);
316                 aq->cq.drop = min_rx_drop;
317                 aq->cq.drop_ena = 1;
318                 rxq->cq_drop = min_rx_drop;
319         } else {
320                 rxq->cq_drop = NIX_CQ_THRESH_LEVEL;
321                 aq->cq.drop = rxq->cq_drop;
322                 aq->cq.drop_ena = 1;
323         }
324
325         /* TX pause frames enable flowctrl on RX side */
326         if (dev->fc_info.tx_pause) {
327                 /* Single bpid is allocated for all rx channels for now */
328                 aq->cq.bpid = dev->fc_info.bpid[0];
329                 aq->cq.bp = rxq->cq_drop;
330                 aq->cq.bp_ena = 1;
331         }
332
333         rc = otx2_mbox_process(mbox);
334         if (rc) {
335                 otx2_err("Failed to init cq context");
336                 goto fail;
337         }
338
339         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
340         aq->qidx = qid;
341         aq->ctype = NIX_AQ_CTYPE_RQ;
342         aq->op = NIX_AQ_INSTOP_INIT;
343
344         aq->rq.sso_ena = 0;
345         aq->rq.cq = qid; /* RQ to CQ 1:1 mapped */
346         aq->rq.spb_ena = 0;
347         aq->rq.lpb_aura = npa_lf_aura_handle_to_aura(mp->pool_id);
348         first_skip = (sizeof(struct rte_mbuf));
349         first_skip += RTE_PKTMBUF_HEADROOM;
350         first_skip += rte_pktmbuf_priv_size(mp);
351         rxq->data_off = first_skip;
352
353         first_skip /= 8; /* Expressed in number of dwords */
354         aq->rq.first_skip = first_skip;
355         aq->rq.later_skip = (sizeof(struct rte_mbuf) / 8);
356         aq->rq.flow_tagw = 32; /* 32-bits */
357         aq->rq.lpb_sizem1 = rte_pktmbuf_data_room_size(mp);
358         aq->rq.lpb_sizem1 += rte_pktmbuf_priv_size(mp);
359         aq->rq.lpb_sizem1 += sizeof(struct rte_mbuf);
360         aq->rq.lpb_sizem1 /= 8;
361         aq->rq.lpb_sizem1 -= 1; /* Expressed in size minus one */
362         aq->rq.ena = 1;
363         aq->rq.pb_caching = 0x2; /* First cache aligned block to LLC */
364         aq->rq.xqe_imm_size = 0; /* No pkt data copy to CQE */
365         aq->rq.rq_int_ena = 0;
366         /* Many to one reduction */
367         aq->rq.qint_idx = qid % dev->qints;
368
369         aq->rq.xqe_drop_ena = 1;
370
371         rc = otx2_mbox_process(mbox);
372         if (rc) {
373                 otx2_err("Failed to init rq context");
374                 goto fail;
375         }
376
377         return 0;
378 fail:
379         return rc;
380 }
381
382 static int
383 nix_rq_enb_dis(struct rte_eth_dev *eth_dev,
384                struct otx2_eth_rxq *rxq, const bool enb)
385 {
386         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
387         struct otx2_mbox *mbox = dev->mbox;
388         struct nix_aq_enq_req *aq;
389
390         /* Pkts will be dropped silently if RQ is disabled */
391         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
392         aq->qidx = rxq->rq;
393         aq->ctype = NIX_AQ_CTYPE_RQ;
394         aq->op = NIX_AQ_INSTOP_WRITE;
395
396         aq->rq.ena = enb;
397         aq->rq_mask.ena = ~(aq->rq_mask.ena);
398
399         return otx2_mbox_process(mbox);
400 }
401
402 static int
403 nix_cq_rq_uninit(struct rte_eth_dev *eth_dev, struct otx2_eth_rxq *rxq)
404 {
405         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
406         struct otx2_mbox *mbox = dev->mbox;
407         struct nix_aq_enq_req *aq;
408         int rc;
409
410         /* RQ is already disabled */
411         /* Disable CQ */
412         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
413         aq->qidx = rxq->rq;
414         aq->ctype = NIX_AQ_CTYPE_CQ;
415         aq->op = NIX_AQ_INSTOP_WRITE;
416
417         aq->cq.ena = 0;
418         aq->cq_mask.ena = ~(aq->cq_mask.ena);
419
420         rc = otx2_mbox_process(mbox);
421         if (rc < 0) {
422                 otx2_err("Failed to disable cq context");
423                 return rc;
424         }
425
426         return 0;
427 }
428
429 static inline int
430 nix_get_data_off(struct otx2_eth_dev *dev)
431 {
432         return otx2_ethdev_is_ptp_en(dev) ? NIX_TIMESYNC_RX_OFFSET : 0;
433 }
434
435 uint64_t
436 otx2_nix_rxq_mbuf_setup(struct otx2_eth_dev *dev, uint16_t port_id)
437 {
438         struct rte_mbuf mb_def;
439         uint64_t *tmp;
440
441         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) % 8 != 0);
442         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, refcnt) -
443                                 offsetof(struct rte_mbuf, data_off) != 2);
444         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, nb_segs) -
445                                 offsetof(struct rte_mbuf, data_off) != 4);
446         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, port) -
447                                 offsetof(struct rte_mbuf, data_off) != 6);
448         mb_def.nb_segs = 1;
449         mb_def.data_off = RTE_PKTMBUF_HEADROOM + nix_get_data_off(dev);
450         mb_def.port = port_id;
451         rte_mbuf_refcnt_set(&mb_def, 1);
452
453         /* Prevent compiler reordering: rearm_data covers previous fields */
454         rte_compiler_barrier();
455         tmp = (uint64_t *)&mb_def.rearm_data;
456
457         return *tmp;
458 }
459
460 static void
461 otx2_nix_rx_queue_release(void *rx_queue)
462 {
463         struct otx2_eth_rxq *rxq = rx_queue;
464
465         if (!rxq)
466                 return;
467
468         otx2_nix_dbg("Releasing rxq %u", rxq->rq);
469         nix_cq_rq_uninit(rxq->eth_dev, rxq);
470         rte_free(rx_queue);
471 }
472
473 static int
474 otx2_nix_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t rq,
475                         uint16_t nb_desc, unsigned int socket,
476                         const struct rte_eth_rxconf *rx_conf,
477                         struct rte_mempool *mp)
478 {
479         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
480         struct rte_mempool_ops *ops;
481         struct otx2_eth_rxq *rxq;
482         const char *platform_ops;
483         enum nix_q_size_e qsize;
484         uint64_t offloads;
485         int rc;
486
487         rc = -EINVAL;
488
489         /* Compile time check to make sure all fast path elements in a CL */
490         RTE_BUILD_BUG_ON(offsetof(struct otx2_eth_rxq, slow_path_start) >= 128);
491
492         /* Sanity checks */
493         if (rx_conf->rx_deferred_start == 1) {
494                 otx2_err("Deferred Rx start is not supported");
495                 goto fail;
496         }
497
498         platform_ops = rte_mbuf_platform_mempool_ops();
499         /* This driver needs octeontx2_npa mempool ops to work */
500         ops = rte_mempool_get_ops(mp->ops_index);
501         if (strncmp(ops->name, platform_ops, RTE_MEMPOOL_OPS_NAMESIZE)) {
502                 otx2_err("mempool ops should be of octeontx2_npa type");
503                 goto fail;
504         }
505
506         if (mp->pool_id == 0) {
507                 otx2_err("Invalid pool_id");
508                 goto fail;
509         }
510
511         /* Free memory prior to re-allocation if needed */
512         if (eth_dev->data->rx_queues[rq] != NULL) {
513                 otx2_nix_dbg("Freeing memory prior to re-allocation %d", rq);
514                 otx2_nix_rx_queue_release(eth_dev->data->rx_queues[rq]);
515                 eth_dev->data->rx_queues[rq] = NULL;
516         }
517
518         offloads = rx_conf->offloads | eth_dev->data->dev_conf.rxmode.offloads;
519         dev->rx_offloads |= offloads;
520
521         /* Find the CQ queue size */
522         qsize = nix_qsize_clampup_get(dev, nb_desc);
523         /* Allocate rxq memory */
524         rxq = rte_zmalloc_socket("otx2 rxq", sizeof(*rxq), OTX2_ALIGN, socket);
525         if (rxq == NULL) {
526                 otx2_err("Failed to allocate rq=%d", rq);
527                 rc = -ENOMEM;
528                 goto fail;
529         }
530
531         rxq->eth_dev = eth_dev;
532         rxq->rq = rq;
533         rxq->cq_door = dev->base + NIX_LF_CQ_OP_DOOR;
534         rxq->cq_status = (int64_t *)(dev->base + NIX_LF_CQ_OP_STATUS);
535         rxq->wdata = (uint64_t)rq << 32;
536         rxq->aura = npa_lf_aura_handle_to_aura(mp->pool_id);
537         rxq->mbuf_initializer = otx2_nix_rxq_mbuf_setup(dev,
538                                                         eth_dev->data->port_id);
539         rxq->offloads = offloads;
540         rxq->pool = mp;
541         rxq->qlen = nix_qsize_to_val(qsize);
542         rxq->qsize = qsize;
543         rxq->lookup_mem = otx2_nix_fastpath_lookup_mem_get();
544         rxq->tstamp = &dev->tstamp;
545
546         /* Alloc completion queue */
547         rc = nix_cq_rq_init(eth_dev, dev, rq, rxq, mp);
548         if (rc) {
549                 otx2_err("Failed to allocate rxq=%u", rq);
550                 goto free_rxq;
551         }
552
553         rxq->qconf.socket_id = socket;
554         rxq->qconf.nb_desc = nb_desc;
555         rxq->qconf.mempool = mp;
556         memcpy(&rxq->qconf.conf.rx, rx_conf, sizeof(struct rte_eth_rxconf));
557
558         nix_rx_queue_reset(rxq);
559         otx2_nix_dbg("rq=%d pool=%s qsize=%d nb_desc=%d->%d",
560                      rq, mp->name, qsize, nb_desc, rxq->qlen);
561
562         eth_dev->data->rx_queues[rq] = rxq;
563         eth_dev->data->rx_queue_state[rq] = RTE_ETH_QUEUE_STATE_STOPPED;
564
565         /* Calculating delta and freq mult between PTP HI clock and tsc.
566          * These are needed in deriving raw clock value from tsc counter.
567          * read_clock eth op returns raw clock value.
568          */
569         if ((dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP) ||
570             otx2_ethdev_is_ptp_en(dev)) {
571                 rc = otx2_nix_raw_clock_tsc_conv(dev);
572                 if (rc) {
573                         otx2_err("Failed to calculate delta and freq mult");
574                         goto fail;
575                 }
576         }
577
578         return 0;
579
580 free_rxq:
581         otx2_nix_rx_queue_release(rxq);
582 fail:
583         return rc;
584 }
585
586 static inline uint8_t
587 nix_sq_max_sqe_sz(struct otx2_eth_txq *txq)
588 {
589         /*
590          * Maximum three segments can be supported with W8, Choose
591          * NIX_MAXSQESZ_W16 for multi segment offload.
592          */
593         if (txq->offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
594                 return NIX_MAXSQESZ_W16;
595         else
596                 return NIX_MAXSQESZ_W8;
597 }
598
599 static uint16_t
600 nix_rx_offload_flags(struct rte_eth_dev *eth_dev)
601 {
602         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
603         struct rte_eth_dev_data *data = eth_dev->data;
604         struct rte_eth_conf *conf = &data->dev_conf;
605         struct rte_eth_rxmode *rxmode = &conf->rxmode;
606         uint16_t flags = 0;
607
608         if (rxmode->mq_mode == ETH_MQ_RX_RSS &&
609                         (dev->rx_offloads & DEV_RX_OFFLOAD_RSS_HASH))
610                 flags |= NIX_RX_OFFLOAD_RSS_F;
611
612         if (dev->rx_offloads & (DEV_RX_OFFLOAD_TCP_CKSUM |
613                          DEV_RX_OFFLOAD_UDP_CKSUM))
614                 flags |= NIX_RX_OFFLOAD_CHECKSUM_F;
615
616         if (dev->rx_offloads & (DEV_RX_OFFLOAD_IPV4_CKSUM |
617                                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM))
618                 flags |= NIX_RX_OFFLOAD_CHECKSUM_F;
619
620         if (dev->rx_offloads & DEV_RX_OFFLOAD_SCATTER)
621                 flags |= NIX_RX_MULTI_SEG_F;
622
623         if (dev->rx_offloads & (DEV_RX_OFFLOAD_VLAN_STRIP |
624                                 DEV_RX_OFFLOAD_QINQ_STRIP))
625                 flags |= NIX_RX_OFFLOAD_VLAN_STRIP_F;
626
627         if ((dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP))
628                 flags |= NIX_RX_OFFLOAD_TSTAMP_F;
629
630         if (!dev->ptype_disable)
631                 flags |= NIX_RX_OFFLOAD_PTYPE_F;
632
633         return flags;
634 }
635
636 static uint16_t
637 nix_tx_offload_flags(struct rte_eth_dev *eth_dev)
638 {
639         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
640         uint64_t conf = dev->tx_offloads;
641         uint16_t flags = 0;
642
643         /* Fastpath is dependent on these enums */
644         RTE_BUILD_BUG_ON(PKT_TX_TCP_CKSUM != (1ULL << 52));
645         RTE_BUILD_BUG_ON(PKT_TX_SCTP_CKSUM != (2ULL << 52));
646         RTE_BUILD_BUG_ON(PKT_TX_UDP_CKSUM != (3ULL << 52));
647         RTE_BUILD_BUG_ON(PKT_TX_IP_CKSUM != (1ULL << 54));
648         RTE_BUILD_BUG_ON(PKT_TX_IPV4 != (1ULL << 55));
649         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IP_CKSUM != (1ULL << 58));
650         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IPV4 != (1ULL << 59));
651         RTE_BUILD_BUG_ON(PKT_TX_OUTER_IPV6 != (1ULL << 60));
652         RTE_BUILD_BUG_ON(PKT_TX_OUTER_UDP_CKSUM != (1ULL << 41));
653         RTE_BUILD_BUG_ON(RTE_MBUF_L2_LEN_BITS != 7);
654         RTE_BUILD_BUG_ON(RTE_MBUF_L3_LEN_BITS != 9);
655         RTE_BUILD_BUG_ON(RTE_MBUF_OUTL2_LEN_BITS != 7);
656         RTE_BUILD_BUG_ON(RTE_MBUF_OUTL3_LEN_BITS != 9);
657         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) !=
658                          offsetof(struct rte_mbuf, buf_iova) + 8);
659         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, ol_flags) !=
660                          offsetof(struct rte_mbuf, buf_iova) + 16);
661         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, pkt_len) !=
662                          offsetof(struct rte_mbuf, ol_flags) + 12);
663         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, tx_offload) !=
664                          offsetof(struct rte_mbuf, pool) + 2 * sizeof(void *));
665
666         if (conf & DEV_TX_OFFLOAD_VLAN_INSERT ||
667             conf & DEV_TX_OFFLOAD_QINQ_INSERT)
668                 flags |= NIX_TX_OFFLOAD_VLAN_QINQ_F;
669
670         if (conf & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM ||
671             conf & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM)
672                 flags |= NIX_TX_OFFLOAD_OL3_OL4_CSUM_F;
673
674         if (conf & DEV_TX_OFFLOAD_IPV4_CKSUM ||
675             conf & DEV_TX_OFFLOAD_TCP_CKSUM ||
676             conf & DEV_TX_OFFLOAD_UDP_CKSUM ||
677             conf & DEV_TX_OFFLOAD_SCTP_CKSUM)
678                 flags |= NIX_TX_OFFLOAD_L3_L4_CSUM_F;
679
680         if (!(conf & DEV_TX_OFFLOAD_MBUF_FAST_FREE))
681                 flags |= NIX_TX_OFFLOAD_MBUF_NOFF_F;
682
683         if (conf & DEV_TX_OFFLOAD_MULTI_SEGS)
684                 flags |= NIX_TX_MULTI_SEG_F;
685
686         /* Enable Inner checksum for TSO */
687         if (conf & DEV_TX_OFFLOAD_TCP_TSO)
688                 flags |= (NIX_TX_OFFLOAD_TSO_F |
689                           NIX_TX_OFFLOAD_L3_L4_CSUM_F);
690
691         /* Enable Inner and Outer checksum for Tunnel TSO */
692         if (conf & (DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
693                     DEV_TX_OFFLOAD_GENEVE_TNL_TSO |
694                     DEV_TX_OFFLOAD_GRE_TNL_TSO))
695                 flags |= (NIX_TX_OFFLOAD_TSO_F |
696                           NIX_TX_OFFLOAD_OL3_OL4_CSUM_F |
697                           NIX_TX_OFFLOAD_L3_L4_CSUM_F);
698
699         if ((dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP))
700                 flags |= NIX_TX_OFFLOAD_TSTAMP_F;
701
702         return flags;
703 }
704
705 static int
706 nix_sq_init(struct otx2_eth_txq *txq)
707 {
708         struct otx2_eth_dev *dev = txq->dev;
709         struct otx2_mbox *mbox = dev->mbox;
710         struct nix_aq_enq_req *sq;
711         uint32_t rr_quantum;
712         uint16_t smq;
713         int rc;
714
715         if (txq->sqb_pool->pool_id == 0)
716                 return -EINVAL;
717
718         rc = otx2_nix_tm_get_leaf_data(dev, txq->sq, &rr_quantum, &smq);
719         if (rc) {
720                 otx2_err("Failed to get sq->smq(leaf node), rc=%d", rc);
721                 return rc;
722         }
723
724         sq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
725         sq->qidx = txq->sq;
726         sq->ctype = NIX_AQ_CTYPE_SQ;
727         sq->op = NIX_AQ_INSTOP_INIT;
728         sq->sq.max_sqe_size = nix_sq_max_sqe_sz(txq);
729
730         sq->sq.smq = smq;
731         sq->sq.smq_rr_quantum = rr_quantum;
732         sq->sq.default_chan = dev->tx_chan_base;
733         sq->sq.sqe_stype = NIX_STYPE_STF;
734         sq->sq.ena = 1;
735         if (sq->sq.max_sqe_size == NIX_MAXSQESZ_W8)
736                 sq->sq.sqe_stype = NIX_STYPE_STP;
737         sq->sq.sqb_aura =
738                 npa_lf_aura_handle_to_aura(txq->sqb_pool->pool_id);
739         sq->sq.sq_int_ena = BIT(NIX_SQINT_LMT_ERR);
740         sq->sq.sq_int_ena |= BIT(NIX_SQINT_SQB_ALLOC_FAIL);
741         sq->sq.sq_int_ena |= BIT(NIX_SQINT_SEND_ERR);
742         sq->sq.sq_int_ena |= BIT(NIX_SQINT_MNQ_ERR);
743
744         /* Many to one reduction */
745         sq->sq.qint_idx = txq->sq % dev->qints;
746
747         return otx2_mbox_process(mbox);
748 }
749
750 static int
751 nix_sq_uninit(struct otx2_eth_txq *txq)
752 {
753         struct otx2_eth_dev *dev = txq->dev;
754         struct otx2_mbox *mbox = dev->mbox;
755         struct ndc_sync_op *ndc_req;
756         struct nix_aq_enq_rsp *rsp;
757         struct nix_aq_enq_req *aq;
758         uint16_t sqes_per_sqb;
759         void *sqb_buf;
760         int rc, count;
761
762         otx2_nix_dbg("Cleaning up sq %u", txq->sq);
763
764         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
765         aq->qidx = txq->sq;
766         aq->ctype = NIX_AQ_CTYPE_SQ;
767         aq->op = NIX_AQ_INSTOP_READ;
768
769         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
770         if (rc)
771                 return rc;
772
773         /* Check if sq is already cleaned up */
774         if (!rsp->sq.ena)
775                 return 0;
776
777         /* Disable sq */
778         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
779         aq->qidx = txq->sq;
780         aq->ctype = NIX_AQ_CTYPE_SQ;
781         aq->op = NIX_AQ_INSTOP_WRITE;
782
783         aq->sq_mask.ena = ~aq->sq_mask.ena;
784         aq->sq.ena = 0;
785
786         rc = otx2_mbox_process(mbox);
787         if (rc)
788                 return rc;
789
790         /* Read SQ and free sqb's */
791         aq = otx2_mbox_alloc_msg_nix_aq_enq(mbox);
792         aq->qidx = txq->sq;
793         aq->ctype = NIX_AQ_CTYPE_SQ;
794         aq->op = NIX_AQ_INSTOP_READ;
795
796         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
797         if (rc)
798                 return rc;
799
800         if (aq->sq.smq_pend)
801                 otx2_err("SQ has pending sqe's");
802
803         count = aq->sq.sqb_count;
804         sqes_per_sqb = 1 << txq->sqes_per_sqb_log2;
805         /* Free SQB's that are used */
806         sqb_buf = (void *)rsp->sq.head_sqb;
807         while (count) {
808                 void *next_sqb;
809
810                 next_sqb = *(void **)((uintptr_t)sqb_buf + (uint32_t)
811                                       ((sqes_per_sqb - 1) *
812                                       nix_sq_max_sqe_sz(txq)));
813                 npa_lf_aura_op_free(txq->sqb_pool->pool_id, 1,
814                                     (uint64_t)sqb_buf);
815                 sqb_buf = next_sqb;
816                 count--;
817         }
818
819         /* Free next to use sqb */
820         if (rsp->sq.next_sqb)
821                 npa_lf_aura_op_free(txq->sqb_pool->pool_id, 1,
822                                     rsp->sq.next_sqb);
823
824         /* Sync NDC-NIX-TX for LF */
825         ndc_req = otx2_mbox_alloc_msg_ndc_sync_op(mbox);
826         ndc_req->nix_lf_tx_sync = 1;
827         rc = otx2_mbox_process(mbox);
828         if (rc)
829                 otx2_err("Error on NDC-NIX-TX LF sync, rc %d", rc);
830
831         return rc;
832 }
833
834 static int
835 nix_sqb_aura_limit_cfg(struct rte_mempool *mp, uint16_t nb_sqb_bufs)
836 {
837         struct otx2_npa_lf *npa_lf = otx2_intra_dev_get_cfg()->npa_lf;
838         struct npa_aq_enq_req *aura_req;
839
840         aura_req = otx2_mbox_alloc_msg_npa_aq_enq(npa_lf->mbox);
841         aura_req->aura_id = npa_lf_aura_handle_to_aura(mp->pool_id);
842         aura_req->ctype = NPA_AQ_CTYPE_AURA;
843         aura_req->op = NPA_AQ_INSTOP_WRITE;
844
845         aura_req->aura.limit = nb_sqb_bufs;
846         aura_req->aura_mask.limit = ~(aura_req->aura_mask.limit);
847
848         return otx2_mbox_process(npa_lf->mbox);
849 }
850
851 static int
852 nix_alloc_sqb_pool(int port, struct otx2_eth_txq *txq, uint16_t nb_desc)
853 {
854         struct otx2_eth_dev *dev = txq->dev;
855         uint16_t sqes_per_sqb, nb_sqb_bufs;
856         char name[RTE_MEMPOOL_NAMESIZE];
857         struct rte_mempool_objsz sz;
858         struct npa_aura_s *aura;
859         uint32_t tmp, blk_sz;
860
861         aura = (struct npa_aura_s *)((uintptr_t)txq->fc_mem + OTX2_ALIGN);
862         snprintf(name, sizeof(name), "otx2_sqb_pool_%d_%d", port, txq->sq);
863         blk_sz = dev->sqb_size;
864
865         if (nix_sq_max_sqe_sz(txq) == NIX_MAXSQESZ_W16)
866                 sqes_per_sqb = (dev->sqb_size / 8) / 16;
867         else
868                 sqes_per_sqb = (dev->sqb_size / 8) / 8;
869
870         nb_sqb_bufs = nb_desc / sqes_per_sqb;
871         /* Clamp up to devarg passed SQB count */
872         nb_sqb_bufs =  RTE_MIN(dev->max_sqb_count, RTE_MAX(NIX_DEF_SQB,
873                               nb_sqb_bufs + NIX_SQB_LIST_SPACE));
874
875         txq->sqb_pool = rte_mempool_create_empty(name, NIX_MAX_SQB, blk_sz,
876                                                  0, 0, dev->node,
877                                                  MEMPOOL_F_NO_SPREAD);
878         txq->nb_sqb_bufs = nb_sqb_bufs;
879         txq->sqes_per_sqb_log2 = (uint16_t)rte_log2_u32(sqes_per_sqb);
880         txq->nb_sqb_bufs_adj = nb_sqb_bufs -
881                 RTE_ALIGN_MUL_CEIL(nb_sqb_bufs, sqes_per_sqb) / sqes_per_sqb;
882         txq->nb_sqb_bufs_adj =
883                 (NIX_SQB_LOWER_THRESH * txq->nb_sqb_bufs_adj) / 100;
884
885         if (txq->sqb_pool == NULL) {
886                 otx2_err("Failed to allocate sqe mempool");
887                 goto fail;
888         }
889
890         memset(aura, 0, sizeof(*aura));
891         aura->fc_ena = 1;
892         aura->fc_addr = txq->fc_iova;
893         aura->fc_hyst_bits = 0; /* Store count on all updates */
894         if (rte_mempool_set_ops_byname(txq->sqb_pool, "octeontx2_npa", aura)) {
895                 otx2_err("Failed to set ops for sqe mempool");
896                 goto fail;
897         }
898         if (rte_mempool_populate_default(txq->sqb_pool) < 0) {
899                 otx2_err("Failed to populate sqe mempool");
900                 goto fail;
901         }
902
903         tmp = rte_mempool_calc_obj_size(blk_sz, MEMPOOL_F_NO_SPREAD, &sz);
904         if (dev->sqb_size != sz.elt_size) {
905                 otx2_err("sqe pool block size is not expected %d != %d",
906                          dev->sqb_size, tmp);
907                 goto fail;
908         }
909
910         nix_sqb_aura_limit_cfg(txq->sqb_pool, txq->nb_sqb_bufs);
911
912         return 0;
913 fail:
914         return -ENOMEM;
915 }
916
917 void
918 otx2_nix_form_default_desc(struct otx2_eth_txq *txq)
919 {
920         struct nix_send_ext_s *send_hdr_ext;
921         struct nix_send_hdr_s *send_hdr;
922         struct nix_send_mem_s *send_mem;
923         union nix_send_sg_s *sg;
924
925         /* Initialize the fields based on basic single segment packet */
926         memset(&txq->cmd, 0, sizeof(txq->cmd));
927
928         if (txq->dev->tx_offload_flags & NIX_TX_NEED_EXT_HDR) {
929                 send_hdr = (struct nix_send_hdr_s *)&txq->cmd[0];
930                 /* 2(HDR) + 2(EXT_HDR) + 1(SG) + 1(IOVA) = 6/2 - 1 = 2 */
931                 send_hdr->w0.sizem1 = 2;
932
933                 send_hdr_ext = (struct nix_send_ext_s *)&txq->cmd[2];
934                 send_hdr_ext->w0.subdc = NIX_SUBDC_EXT;
935                 if (txq->dev->tx_offload_flags & NIX_TX_OFFLOAD_TSTAMP_F) {
936                         /* Default: one seg packet would have:
937                          * 2(HDR) + 2(EXT) + 1(SG) + 1(IOVA) + 2(MEM)
938                          * => 8/2 - 1 = 3
939                          */
940                         send_hdr->w0.sizem1 = 3;
941                         send_hdr_ext->w0.tstmp = 1;
942
943                         /* To calculate the offset for send_mem,
944                          * send_hdr->w0.sizem1 * 2
945                          */
946                         send_mem = (struct nix_send_mem_s *)(txq->cmd +
947                                                 (send_hdr->w0.sizem1 << 1));
948                         send_mem->subdc = NIX_SUBDC_MEM;
949                         send_mem->alg = NIX_SENDMEMALG_SETTSTMP;
950                         send_mem->addr = txq->dev->tstamp.tx_tstamp_iova;
951                 }
952                 sg = (union nix_send_sg_s *)&txq->cmd[4];
953         } else {
954                 send_hdr = (struct nix_send_hdr_s *)&txq->cmd[0];
955                 /* 2(HDR) + 1(SG) + 1(IOVA) = 4/2 - 1 = 1 */
956                 send_hdr->w0.sizem1 = 1;
957                 sg = (union nix_send_sg_s *)&txq->cmd[2];
958         }
959
960         send_hdr->w0.sq = txq->sq;
961         sg->subdc = NIX_SUBDC_SG;
962         sg->segs = 1;
963         sg->ld_type = NIX_SENDLDTYPE_LDD;
964
965         rte_smp_wmb();
966 }
967
968 static void
969 otx2_nix_tx_queue_release(void *_txq)
970 {
971         struct otx2_eth_txq *txq = _txq;
972         struct rte_eth_dev *eth_dev;
973
974         if (!txq)
975                 return;
976
977         eth_dev = txq->dev->eth_dev;
978
979         otx2_nix_dbg("Releasing txq %u", txq->sq);
980
981         /* Flush and disable tm */
982         otx2_nix_tm_sw_xoff(txq, eth_dev->data->dev_started);
983
984         /* Free sqb's and disable sq */
985         nix_sq_uninit(txq);
986
987         if (txq->sqb_pool) {
988                 rte_mempool_free(txq->sqb_pool);
989                 txq->sqb_pool = NULL;
990         }
991         rte_free(txq);
992 }
993
994
995 static int
996 otx2_nix_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t sq,
997                         uint16_t nb_desc, unsigned int socket_id,
998                         const struct rte_eth_txconf *tx_conf)
999 {
1000         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1001         const struct rte_memzone *fc;
1002         struct otx2_eth_txq *txq;
1003         uint64_t offloads;
1004         int rc;
1005
1006         rc = -EINVAL;
1007
1008         /* Compile time check to make sure all fast path elements in a CL */
1009         RTE_BUILD_BUG_ON(offsetof(struct otx2_eth_txq, slow_path_start) >= 128);
1010
1011         if (tx_conf->tx_deferred_start) {
1012                 otx2_err("Tx deferred start is not supported");
1013                 goto fail;
1014         }
1015
1016         /* Free memory prior to re-allocation if needed. */
1017         if (eth_dev->data->tx_queues[sq] != NULL) {
1018                 otx2_nix_dbg("Freeing memory prior to re-allocation %d", sq);
1019                 otx2_nix_tx_queue_release(eth_dev->data->tx_queues[sq]);
1020                 eth_dev->data->tx_queues[sq] = NULL;
1021         }
1022
1023         /* Find the expected offloads for this queue */
1024         offloads = tx_conf->offloads | eth_dev->data->dev_conf.txmode.offloads;
1025
1026         /* Allocating tx queue data structure */
1027         txq = rte_zmalloc_socket("otx2_ethdev TX queue", sizeof(*txq),
1028                                  OTX2_ALIGN, socket_id);
1029         if (txq == NULL) {
1030                 otx2_err("Failed to alloc txq=%d", sq);
1031                 rc = -ENOMEM;
1032                 goto fail;
1033         }
1034         txq->sq = sq;
1035         txq->dev = dev;
1036         txq->sqb_pool = NULL;
1037         txq->offloads = offloads;
1038         dev->tx_offloads |= offloads;
1039
1040         /*
1041          * Allocate memory for flow control updates from HW.
1042          * Alloc one cache line, so that fits all FC_STYPE modes.
1043          */
1044         fc = rte_eth_dma_zone_reserve(eth_dev, "fcmem", sq,
1045                                       OTX2_ALIGN + sizeof(struct npa_aura_s),
1046                                       OTX2_ALIGN, dev->node);
1047         if (fc == NULL) {
1048                 otx2_err("Failed to allocate mem for fcmem");
1049                 rc = -ENOMEM;
1050                 goto free_txq;
1051         }
1052         txq->fc_iova = fc->iova;
1053         txq->fc_mem = fc->addr;
1054
1055         /* Initialize the aura sqb pool */
1056         rc = nix_alloc_sqb_pool(eth_dev->data->port_id, txq, nb_desc);
1057         if (rc) {
1058                 otx2_err("Failed to alloc sqe pool rc=%d", rc);
1059                 goto free_txq;
1060         }
1061
1062         /* Initialize the SQ */
1063         rc = nix_sq_init(txq);
1064         if (rc) {
1065                 otx2_err("Failed to init sq=%d context", sq);
1066                 goto free_txq;
1067         }
1068
1069         txq->fc_cache_pkts = 0;
1070         txq->io_addr = dev->base + NIX_LF_OP_SENDX(0);
1071         /* Evenly distribute LMT slot for each sq */
1072         txq->lmt_addr = (void *)(dev->lmt_addr + ((sq & LMT_SLOT_MASK) << 12));
1073
1074         txq->qconf.socket_id = socket_id;
1075         txq->qconf.nb_desc = nb_desc;
1076         memcpy(&txq->qconf.conf.tx, tx_conf, sizeof(struct rte_eth_txconf));
1077
1078         otx2_nix_form_default_desc(txq);
1079
1080         otx2_nix_dbg("sq=%d fc=%p offload=0x%" PRIx64 " sqb=0x%" PRIx64 ""
1081                      " lmt_addr=%p nb_sqb_bufs=%d sqes_per_sqb_log2=%d", sq,
1082                      fc->addr, offloads, txq->sqb_pool->pool_id, txq->lmt_addr,
1083                      txq->nb_sqb_bufs, txq->sqes_per_sqb_log2);
1084         eth_dev->data->tx_queues[sq] = txq;
1085         eth_dev->data->tx_queue_state[sq] = RTE_ETH_QUEUE_STATE_STOPPED;
1086         return 0;
1087
1088 free_txq:
1089         otx2_nix_tx_queue_release(txq);
1090 fail:
1091         return rc;
1092 }
1093
1094 static int
1095 nix_store_queue_cfg_and_then_release(struct rte_eth_dev *eth_dev)
1096 {
1097         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1098         struct otx2_eth_qconf *tx_qconf = NULL;
1099         struct otx2_eth_qconf *rx_qconf = NULL;
1100         struct otx2_eth_txq **txq;
1101         struct otx2_eth_rxq **rxq;
1102         int i, nb_rxq, nb_txq;
1103
1104         nb_rxq = RTE_MIN(dev->configured_nb_rx_qs, eth_dev->data->nb_rx_queues);
1105         nb_txq = RTE_MIN(dev->configured_nb_tx_qs, eth_dev->data->nb_tx_queues);
1106
1107         tx_qconf = malloc(nb_txq * sizeof(*tx_qconf));
1108         if (tx_qconf == NULL) {
1109                 otx2_err("Failed to allocate memory for tx_qconf");
1110                 goto fail;
1111         }
1112
1113         rx_qconf = malloc(nb_rxq * sizeof(*rx_qconf));
1114         if (rx_qconf == NULL) {
1115                 otx2_err("Failed to allocate memory for rx_qconf");
1116                 goto fail;
1117         }
1118
1119         txq = (struct otx2_eth_txq **)eth_dev->data->tx_queues;
1120         for (i = 0; i < nb_txq; i++) {
1121                 if (txq[i] == NULL) {
1122                         otx2_err("txq[%d] is already released", i);
1123                         goto fail;
1124                 }
1125                 memcpy(&tx_qconf[i], &txq[i]->qconf, sizeof(*tx_qconf));
1126                 otx2_nix_tx_queue_release(txq[i]);
1127                 eth_dev->data->tx_queues[i] = NULL;
1128         }
1129
1130         rxq = (struct otx2_eth_rxq **)eth_dev->data->rx_queues;
1131         for (i = 0; i < nb_rxq; i++) {
1132                 if (rxq[i] == NULL) {
1133                         otx2_err("rxq[%d] is already released", i);
1134                         goto fail;
1135                 }
1136                 memcpy(&rx_qconf[i], &rxq[i]->qconf, sizeof(*rx_qconf));
1137                 otx2_nix_rx_queue_release(rxq[i]);
1138                 eth_dev->data->rx_queues[i] = NULL;
1139         }
1140
1141         dev->tx_qconf = tx_qconf;
1142         dev->rx_qconf = rx_qconf;
1143         return 0;
1144
1145 fail:
1146         if (tx_qconf)
1147                 free(tx_qconf);
1148         if (rx_qconf)
1149                 free(rx_qconf);
1150
1151         return -ENOMEM;
1152 }
1153
1154 static int
1155 nix_restore_queue_cfg(struct rte_eth_dev *eth_dev)
1156 {
1157         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1158         struct otx2_eth_qconf *tx_qconf = dev->tx_qconf;
1159         struct otx2_eth_qconf *rx_qconf = dev->rx_qconf;
1160         struct otx2_eth_txq **txq;
1161         struct otx2_eth_rxq **rxq;
1162         int rc, i, nb_rxq, nb_txq;
1163
1164         nb_rxq = RTE_MIN(dev->configured_nb_rx_qs, eth_dev->data->nb_rx_queues);
1165         nb_txq = RTE_MIN(dev->configured_nb_tx_qs, eth_dev->data->nb_tx_queues);
1166
1167         rc = -ENOMEM;
1168         /* Setup tx & rx queues with previous configuration so
1169          * that the queues can be functional in cases like ports
1170          * are started without re configuring queues.
1171          *
1172          * Usual re config sequence is like below:
1173          * port_configure() {
1174          *      if(reconfigure) {
1175          *              queue_release()
1176          *              queue_setup()
1177          *      }
1178          *      queue_configure() {
1179          *              queue_release()
1180          *              queue_setup()
1181          *      }
1182          * }
1183          * port_start()
1184          *
1185          * In some application's control path, queue_configure() would
1186          * NOT be invoked for TXQs/RXQs in port_configure().
1187          * In such cases, queues can be functional after start as the
1188          * queues are already setup in port_configure().
1189          */
1190         for (i = 0; i < nb_txq; i++) {
1191                 rc = otx2_nix_tx_queue_setup(eth_dev, i, tx_qconf[i].nb_desc,
1192                                              tx_qconf[i].socket_id,
1193                                              &tx_qconf[i].conf.tx);
1194                 if (rc) {
1195                         otx2_err("Failed to setup tx queue rc=%d", rc);
1196                         txq = (struct otx2_eth_txq **)eth_dev->data->tx_queues;
1197                         for (i -= 1; i >= 0; i--)
1198                                 otx2_nix_tx_queue_release(txq[i]);
1199                         goto fail;
1200                 }
1201         }
1202
1203         free(tx_qconf); tx_qconf = NULL;
1204
1205         for (i = 0; i < nb_rxq; i++) {
1206                 rc = otx2_nix_rx_queue_setup(eth_dev, i, rx_qconf[i].nb_desc,
1207                                              rx_qconf[i].socket_id,
1208                                              &rx_qconf[i].conf.rx,
1209                                              rx_qconf[i].mempool);
1210                 if (rc) {
1211                         otx2_err("Failed to setup rx queue rc=%d", rc);
1212                         rxq = (struct otx2_eth_rxq **)eth_dev->data->rx_queues;
1213                         for (i -= 1; i >= 0; i--)
1214                                 otx2_nix_rx_queue_release(rxq[i]);
1215                         goto release_tx_queues;
1216                 }
1217         }
1218
1219         free(rx_qconf); rx_qconf = NULL;
1220
1221         return 0;
1222
1223 release_tx_queues:
1224         txq = (struct otx2_eth_txq **)eth_dev->data->tx_queues;
1225         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1226                 otx2_nix_tx_queue_release(txq[i]);
1227 fail:
1228         if (tx_qconf)
1229                 free(tx_qconf);
1230         if (rx_qconf)
1231                 free(rx_qconf);
1232
1233         return rc;
1234 }
1235
1236 static uint16_t
1237 nix_eth_nop_burst(void *queue, struct rte_mbuf **mbufs, uint16_t pkts)
1238 {
1239         RTE_SET_USED(queue);
1240         RTE_SET_USED(mbufs);
1241         RTE_SET_USED(pkts);
1242
1243         return 0;
1244 }
1245
1246 static void
1247 nix_set_nop_rxtx_function(struct rte_eth_dev *eth_dev)
1248 {
1249         /* These dummy functions are required for supporting
1250          * some applications which reconfigure queues without
1251          * stopping tx burst and rx burst threads(eg kni app)
1252          * When the queues context is saved, txq/rxqs are released
1253          * which caused app crash since rx/tx burst is still
1254          * on different lcores
1255          */
1256         eth_dev->tx_pkt_burst = nix_eth_nop_burst;
1257         eth_dev->rx_pkt_burst = nix_eth_nop_burst;
1258         rte_mb();
1259 }
1260
1261 static void
1262 nix_lso_tcp(struct nix_lso_format_cfg *req, bool v4)
1263 {
1264         volatile struct nix_lso_format *field;
1265
1266         /* Format works only with TCP packet marked by OL3/OL4 */
1267         field = (volatile struct nix_lso_format *)&req->fields[0];
1268         req->field_mask = NIX_LSO_FIELD_MASK;
1269         /* Outer IPv4/IPv6 */
1270         field->layer = NIX_TXLAYER_OL3;
1271         field->offset = v4 ? 2 : 4;
1272         field->sizem1 = 1; /* 2B */
1273         field->alg = NIX_LSOALG_ADD_PAYLEN;
1274         field++;
1275         if (v4) {
1276                 /* IPID field */
1277                 field->layer = NIX_TXLAYER_OL3;
1278                 field->offset = 4;
1279                 field->sizem1 = 1;
1280                 /* Incremented linearly per segment */
1281                 field->alg = NIX_LSOALG_ADD_SEGNUM;
1282                 field++;
1283         }
1284
1285         /* TCP sequence number update */
1286         field->layer = NIX_TXLAYER_OL4;
1287         field->offset = 4;
1288         field->sizem1 = 3; /* 4 bytes */
1289         field->alg = NIX_LSOALG_ADD_OFFSET;
1290         field++;
1291         /* TCP flags field */
1292         field->layer = NIX_TXLAYER_OL4;
1293         field->offset = 12;
1294         field->sizem1 = 1;
1295         field->alg = NIX_LSOALG_TCP_FLAGS;
1296         field++;
1297 }
1298
1299 static void
1300 nix_lso_udp_tun_tcp(struct nix_lso_format_cfg *req,
1301                     bool outer_v4, bool inner_v4)
1302 {
1303         volatile struct nix_lso_format *field;
1304
1305         field = (volatile struct nix_lso_format *)&req->fields[0];
1306         req->field_mask = NIX_LSO_FIELD_MASK;
1307         /* Outer IPv4/IPv6 len */
1308         field->layer = NIX_TXLAYER_OL3;
1309         field->offset = outer_v4 ? 2 : 4;
1310         field->sizem1 = 1; /* 2B */
1311         field->alg = NIX_LSOALG_ADD_PAYLEN;
1312         field++;
1313         if (outer_v4) {
1314                 /* IPID */
1315                 field->layer = NIX_TXLAYER_OL3;
1316                 field->offset = 4;
1317                 field->sizem1 = 1;
1318                 /* Incremented linearly per segment */
1319                 field->alg = NIX_LSOALG_ADD_SEGNUM;
1320                 field++;
1321         }
1322
1323         /* Outer UDP length */
1324         field->layer = NIX_TXLAYER_OL4;
1325         field->offset = 4;
1326         field->sizem1 = 1;
1327         field->alg = NIX_LSOALG_ADD_PAYLEN;
1328         field++;
1329
1330         /* Inner IPv4/IPv6 */
1331         field->layer = NIX_TXLAYER_IL3;
1332         field->offset = inner_v4 ? 2 : 4;
1333         field->sizem1 = 1; /* 2B */
1334         field->alg = NIX_LSOALG_ADD_PAYLEN;
1335         field++;
1336         if (inner_v4) {
1337                 /* IPID field */
1338                 field->layer = NIX_TXLAYER_IL3;
1339                 field->offset = 4;
1340                 field->sizem1 = 1;
1341                 /* Incremented linearly per segment */
1342                 field->alg = NIX_LSOALG_ADD_SEGNUM;
1343                 field++;
1344         }
1345
1346         /* TCP sequence number update */
1347         field->layer = NIX_TXLAYER_IL4;
1348         field->offset = 4;
1349         field->sizem1 = 3; /* 4 bytes */
1350         field->alg = NIX_LSOALG_ADD_OFFSET;
1351         field++;
1352
1353         /* TCP flags field */
1354         field->layer = NIX_TXLAYER_IL4;
1355         field->offset = 12;
1356         field->sizem1 = 1;
1357         field->alg = NIX_LSOALG_TCP_FLAGS;
1358         field++;
1359 }
1360
1361 static void
1362 nix_lso_tun_tcp(struct nix_lso_format_cfg *req,
1363                 bool outer_v4, bool inner_v4)
1364 {
1365         volatile struct nix_lso_format *field;
1366
1367         field = (volatile struct nix_lso_format *)&req->fields[0];
1368         req->field_mask = NIX_LSO_FIELD_MASK;
1369         /* Outer IPv4/IPv6 len */
1370         field->layer = NIX_TXLAYER_OL3;
1371         field->offset = outer_v4 ? 2 : 4;
1372         field->sizem1 = 1; /* 2B */
1373         field->alg = NIX_LSOALG_ADD_PAYLEN;
1374         field++;
1375         if (outer_v4) {
1376                 /* IPID */
1377                 field->layer = NIX_TXLAYER_OL3;
1378                 field->offset = 4;
1379                 field->sizem1 = 1;
1380                 /* Incremented linearly per segment */
1381                 field->alg = NIX_LSOALG_ADD_SEGNUM;
1382                 field++;
1383         }
1384
1385         /* Inner IPv4/IPv6 */
1386         field->layer = NIX_TXLAYER_IL3;
1387         field->offset = inner_v4 ? 2 : 4;
1388         field->sizem1 = 1; /* 2B */
1389         field->alg = NIX_LSOALG_ADD_PAYLEN;
1390         field++;
1391         if (inner_v4) {
1392                 /* IPID field */
1393                 field->layer = NIX_TXLAYER_IL3;
1394                 field->offset = 4;
1395                 field->sizem1 = 1;
1396                 /* Incremented linearly per segment */
1397                 field->alg = NIX_LSOALG_ADD_SEGNUM;
1398                 field++;
1399         }
1400
1401         /* TCP sequence number update */
1402         field->layer = NIX_TXLAYER_IL4;
1403         field->offset = 4;
1404         field->sizem1 = 3; /* 4 bytes */
1405         field->alg = NIX_LSOALG_ADD_OFFSET;
1406         field++;
1407
1408         /* TCP flags field */
1409         field->layer = NIX_TXLAYER_IL4;
1410         field->offset = 12;
1411         field->sizem1 = 1;
1412         field->alg = NIX_LSOALG_TCP_FLAGS;
1413         field++;
1414 }
1415
1416 static int
1417 nix_setup_lso_formats(struct otx2_eth_dev *dev)
1418 {
1419         struct otx2_mbox *mbox = dev->mbox;
1420         struct nix_lso_format_cfg_rsp *rsp;
1421         struct nix_lso_format_cfg *req;
1422         uint8_t base;
1423         int rc;
1424
1425         /* Skip if TSO was not requested */
1426         if (!(dev->tx_offload_flags & NIX_TX_OFFLOAD_TSO_F))
1427                 return 0;
1428         /*
1429          * IPv4/TCP LSO
1430          */
1431         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1432         nix_lso_tcp(req, true);
1433         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1434         if (rc)
1435                 return rc;
1436
1437         base = rsp->lso_format_idx;
1438         if (base != NIX_LSO_FORMAT_IDX_TSOV4)
1439                 return -EFAULT;
1440         dev->lso_base_idx = base;
1441         otx2_nix_dbg("tcpv4 lso fmt=%u", base);
1442
1443
1444         /*
1445          * IPv6/TCP LSO
1446          */
1447         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1448         nix_lso_tcp(req, false);
1449         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1450         if (rc)
1451                 return rc;
1452
1453         if (rsp->lso_format_idx != base + 1)
1454                 return -EFAULT;
1455         otx2_nix_dbg("tcpv6 lso fmt=%u\n", base + 1);
1456
1457         /*
1458          * IPv4/UDP/TUN HDR/IPv4/TCP LSO
1459          */
1460         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1461         nix_lso_udp_tun_tcp(req, true, true);
1462         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1463         if (rc)
1464                 return rc;
1465
1466         if (rsp->lso_format_idx != base + 2)
1467                 return -EFAULT;
1468         otx2_nix_dbg("udp tun v4v4 fmt=%u\n", base + 2);
1469
1470         /*
1471          * IPv4/UDP/TUN HDR/IPv6/TCP LSO
1472          */
1473         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1474         nix_lso_udp_tun_tcp(req, true, false);
1475         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1476         if (rc)
1477                 return rc;
1478
1479         if (rsp->lso_format_idx != base + 3)
1480                 return -EFAULT;
1481         otx2_nix_dbg("udp tun v4v6 fmt=%u\n", base + 3);
1482
1483         /*
1484          * IPv6/UDP/TUN HDR/IPv4/TCP LSO
1485          */
1486         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1487         nix_lso_udp_tun_tcp(req, false, true);
1488         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1489         if (rc)
1490                 return rc;
1491
1492         if (rsp->lso_format_idx != base + 4)
1493                 return -EFAULT;
1494         otx2_nix_dbg("udp tun v6v4 fmt=%u\n", base + 4);
1495
1496         /*
1497          * IPv6/UDP/TUN HDR/IPv6/TCP LSO
1498          */
1499         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1500         nix_lso_udp_tun_tcp(req, false, false);
1501         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1502         if (rc)
1503                 return rc;
1504         if (rsp->lso_format_idx != base + 5)
1505                 return -EFAULT;
1506         otx2_nix_dbg("udp tun v6v6 fmt=%u\n", base + 5);
1507
1508         /*
1509          * IPv4/TUN HDR/IPv4/TCP LSO
1510          */
1511         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1512         nix_lso_tun_tcp(req, true, true);
1513         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1514         if (rc)
1515                 return rc;
1516
1517         if (rsp->lso_format_idx != base + 6)
1518                 return -EFAULT;
1519         otx2_nix_dbg("tun v4v4 fmt=%u\n", base + 6);
1520
1521         /*
1522          * IPv4/TUN HDR/IPv6/TCP LSO
1523          */
1524         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1525         nix_lso_tun_tcp(req, true, false);
1526         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1527         if (rc)
1528                 return rc;
1529
1530         if (rsp->lso_format_idx != base + 7)
1531                 return -EFAULT;
1532         otx2_nix_dbg("tun v4v6 fmt=%u\n", base + 7);
1533
1534         /*
1535          * IPv6/TUN HDR/IPv4/TCP LSO
1536          */
1537         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1538         nix_lso_tun_tcp(req, false, true);
1539         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1540         if (rc)
1541                 return rc;
1542
1543         if (rsp->lso_format_idx != base + 8)
1544                 return -EFAULT;
1545         otx2_nix_dbg("tun v6v4 fmt=%u\n", base + 8);
1546
1547         /*
1548          * IPv6/TUN HDR/IPv6/TCP LSO
1549          */
1550         req = otx2_mbox_alloc_msg_nix_lso_format_cfg(mbox);
1551         nix_lso_tun_tcp(req, false, false);
1552         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
1553         if (rc)
1554                 return rc;
1555         if (rsp->lso_format_idx != base + 9)
1556                 return -EFAULT;
1557         otx2_nix_dbg("tun v6v6 fmt=%u\n", base + 9);
1558         return 0;
1559 }
1560
1561 static int
1562 otx2_nix_configure(struct rte_eth_dev *eth_dev)
1563 {
1564         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1565         struct rte_eth_dev_data *data = eth_dev->data;
1566         struct rte_eth_conf *conf = &data->dev_conf;
1567         struct rte_eth_rxmode *rxmode = &conf->rxmode;
1568         struct rte_eth_txmode *txmode = &conf->txmode;
1569         char ea_fmt[RTE_ETHER_ADDR_FMT_SIZE];
1570         struct rte_ether_addr *ea;
1571         uint8_t nb_rxq, nb_txq;
1572         int rc;
1573
1574         rc = -EINVAL;
1575
1576         /* Sanity checks */
1577         if (rte_eal_has_hugepages() == 0) {
1578                 otx2_err("Huge page is not configured");
1579                 goto fail_configure;
1580         }
1581
1582         if (conf->link_speeds & ETH_LINK_SPEED_FIXED) {
1583                 otx2_err("Setting link speed/duplex not supported");
1584                 goto fail_configure;
1585         }
1586
1587         if (conf->dcb_capability_en == 1) {
1588                 otx2_err("dcb enable is not supported");
1589                 goto fail_configure;
1590         }
1591
1592         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
1593                 otx2_err("Flow director is not supported");
1594                 goto fail_configure;
1595         }
1596
1597         if (rxmode->mq_mode != ETH_MQ_RX_NONE &&
1598             rxmode->mq_mode != ETH_MQ_RX_RSS) {
1599                 otx2_err("Unsupported mq rx mode %d", rxmode->mq_mode);
1600                 goto fail_configure;
1601         }
1602
1603         if (txmode->mq_mode != ETH_MQ_TX_NONE) {
1604                 otx2_err("Unsupported mq tx mode %d", txmode->mq_mode);
1605                 goto fail_configure;
1606         }
1607
1608         if (otx2_dev_is_Ax(dev) &&
1609             (txmode->offloads & DEV_TX_OFFLOAD_SCTP_CKSUM) &&
1610             ((txmode->offloads & DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM) ||
1611             (txmode->offloads & DEV_TX_OFFLOAD_OUTER_UDP_CKSUM))) {
1612                 otx2_err("Outer IP and SCTP checksum unsupported");
1613                 goto fail_configure;
1614         }
1615
1616         /* Free the resources allocated from the previous configure */
1617         if (dev->configured == 1) {
1618                 otx2_nix_rxchan_bpid_cfg(eth_dev, false);
1619                 otx2_nix_vlan_fini(eth_dev);
1620                 otx2_nix_mc_addr_list_uninstall(eth_dev);
1621                 otx2_flow_free_all_resources(dev);
1622                 oxt2_nix_unregister_queue_irqs(eth_dev);
1623                 if (eth_dev->data->dev_conf.intr_conf.rxq)
1624                         oxt2_nix_unregister_cq_irqs(eth_dev);
1625                 nix_set_nop_rxtx_function(eth_dev);
1626                 rc = nix_store_queue_cfg_and_then_release(eth_dev);
1627                 if (rc)
1628                         goto fail_configure;
1629                 otx2_nix_tm_fini(eth_dev);
1630                 nix_lf_free(dev);
1631         }
1632
1633         dev->rx_offloads = rxmode->offloads;
1634         dev->tx_offloads = txmode->offloads;
1635         dev->rx_offload_flags |= nix_rx_offload_flags(eth_dev);
1636         dev->tx_offload_flags |= nix_tx_offload_flags(eth_dev);
1637         dev->rss_info.rss_grps = NIX_RSS_GRPS;
1638
1639         nb_rxq = RTE_MAX(data->nb_rx_queues, 1);
1640         nb_txq = RTE_MAX(data->nb_tx_queues, 1);
1641
1642         /* Alloc a nix lf */
1643         rc = nix_lf_alloc(dev, nb_rxq, nb_txq);
1644         if (rc) {
1645                 otx2_err("Failed to init nix_lf rc=%d", rc);
1646                 goto fail_offloads;
1647         }
1648
1649         if (dev->ptp_en &&
1650             dev->npc_flow.switch_header_type == OTX2_PRIV_FLAGS_HIGIG) {
1651                 otx2_err("Both PTP and switch header enabled");
1652                 goto free_nix_lf;
1653         }
1654
1655         rc = nix_lf_switch_header_type_enable(dev, true);
1656         if (rc) {
1657                 otx2_err("Failed to enable switch type nix_lf rc=%d", rc);
1658                 goto free_nix_lf;
1659         }
1660
1661         rc = nix_setup_lso_formats(dev);
1662         if (rc) {
1663                 otx2_err("failed to setup nix lso format fields, rc=%d", rc);
1664                 goto free_nix_lf;
1665         }
1666
1667         /* Configure RSS */
1668         rc = otx2_nix_rss_config(eth_dev);
1669         if (rc) {
1670                 otx2_err("Failed to configure rss rc=%d", rc);
1671                 goto free_nix_lf;
1672         }
1673
1674         /* Init the default TM scheduler hierarchy */
1675         rc = otx2_nix_tm_init_default(eth_dev);
1676         if (rc) {
1677                 otx2_err("Failed to init traffic manager rc=%d", rc);
1678                 goto free_nix_lf;
1679         }
1680
1681         rc = otx2_nix_vlan_offload_init(eth_dev);
1682         if (rc) {
1683                 otx2_err("Failed to init vlan offload rc=%d", rc);
1684                 goto tm_fini;
1685         }
1686
1687         /* Register queue IRQs */
1688         rc = oxt2_nix_register_queue_irqs(eth_dev);
1689         if (rc) {
1690                 otx2_err("Failed to register queue interrupts rc=%d", rc);
1691                 goto vlan_fini;
1692         }
1693
1694         /* Register cq IRQs */
1695         if (eth_dev->data->dev_conf.intr_conf.rxq) {
1696                 if (eth_dev->data->nb_rx_queues > dev->cints) {
1697                         otx2_err("Rx interrupt cannot be enabled, rxq > %d",
1698                                  dev->cints);
1699                         goto q_irq_fini;
1700                 }
1701                 /* Rx interrupt feature cannot work with vector mode because,
1702                  * vector mode doesn't process packets unless min 4 pkts are
1703                  * received, while cq interrupts are generated even for 1 pkt
1704                  * in the CQ.
1705                  */
1706                 dev->scalar_ena = true;
1707
1708                 rc = oxt2_nix_register_cq_irqs(eth_dev);
1709                 if (rc) {
1710                         otx2_err("Failed to register CQ interrupts rc=%d", rc);
1711                         goto q_irq_fini;
1712                 }
1713         }
1714
1715         /* Configure loop back mode */
1716         rc = cgx_intlbk_enable(dev, eth_dev->data->dev_conf.lpbk_mode);
1717         if (rc) {
1718                 otx2_err("Failed to configure cgx loop back mode rc=%d", rc);
1719                 goto cq_fini;
1720         }
1721
1722         rc = otx2_nix_rxchan_bpid_cfg(eth_dev, true);
1723         if (rc) {
1724                 otx2_err("Failed to configure nix rx chan bpid cfg rc=%d", rc);
1725                 goto cq_fini;
1726         }
1727
1728         rc = otx2_nix_mc_addr_list_install(eth_dev);
1729         if (rc < 0) {
1730                 otx2_err("Failed to install mc address list rc=%d", rc);
1731                 goto cq_fini;
1732         }
1733
1734         /*
1735          * Restore queue config when reconfigure followed by
1736          * reconfigure and no queue configure invoked from application case.
1737          */
1738         if (dev->configured == 1) {
1739                 rc = nix_restore_queue_cfg(eth_dev);
1740                 if (rc)
1741                         goto uninstall_mc_list;
1742         }
1743
1744         /* Update the mac address */
1745         ea = eth_dev->data->mac_addrs;
1746         memcpy(ea, dev->mac_addr, RTE_ETHER_ADDR_LEN);
1747         if (rte_is_zero_ether_addr(ea))
1748                 rte_eth_random_addr((uint8_t *)ea);
1749
1750         rte_ether_format_addr(ea_fmt, RTE_ETHER_ADDR_FMT_SIZE, ea);
1751
1752         otx2_nix_dbg("Configured port%d mac=%s nb_rxq=%d nb_txq=%d"
1753                 " rx_offloads=0x%" PRIx64 " tx_offloads=0x%" PRIx64 ""
1754                 " rx_flags=0x%x tx_flags=0x%x",
1755                 eth_dev->data->port_id, ea_fmt, nb_rxq,
1756                 nb_txq, dev->rx_offloads, dev->tx_offloads,
1757                 dev->rx_offload_flags, dev->tx_offload_flags);
1758
1759         /* All good */
1760         dev->configured = 1;
1761         dev->configured_nb_rx_qs = data->nb_rx_queues;
1762         dev->configured_nb_tx_qs = data->nb_tx_queues;
1763         return 0;
1764
1765 uninstall_mc_list:
1766         otx2_nix_mc_addr_list_uninstall(eth_dev);
1767 cq_fini:
1768         oxt2_nix_unregister_cq_irqs(eth_dev);
1769 q_irq_fini:
1770         oxt2_nix_unregister_queue_irqs(eth_dev);
1771 vlan_fini:
1772         otx2_nix_vlan_fini(eth_dev);
1773 tm_fini:
1774         otx2_nix_tm_fini(eth_dev);
1775 free_nix_lf:
1776         nix_lf_free(dev);
1777 fail_offloads:
1778         dev->rx_offload_flags &= ~nix_rx_offload_flags(eth_dev);
1779         dev->tx_offload_flags &= ~nix_tx_offload_flags(eth_dev);
1780 fail_configure:
1781         dev->configured = 0;
1782         return rc;
1783 }
1784
1785 int
1786 otx2_nix_tx_queue_start(struct rte_eth_dev *eth_dev, uint16_t qidx)
1787 {
1788         struct rte_eth_dev_data *data = eth_dev->data;
1789         struct otx2_eth_txq *txq;
1790         int rc = -EINVAL;
1791
1792         txq = eth_dev->data->tx_queues[qidx];
1793
1794         if (data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED)
1795                 return 0;
1796
1797         rc = otx2_nix_sq_sqb_aura_fc(txq, true);
1798         if (rc) {
1799                 otx2_err("Failed to enable sqb aura fc, txq=%u, rc=%d",
1800                          qidx, rc);
1801                 goto done;
1802         }
1803
1804         data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
1805
1806 done:
1807         return rc;
1808 }
1809
1810 int
1811 otx2_nix_tx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t qidx)
1812 {
1813         struct rte_eth_dev_data *data = eth_dev->data;
1814         struct otx2_eth_txq *txq;
1815         int rc;
1816
1817         txq = eth_dev->data->tx_queues[qidx];
1818
1819         if (data->tx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED)
1820                 return 0;
1821
1822         txq->fc_cache_pkts = 0;
1823
1824         rc = otx2_nix_sq_sqb_aura_fc(txq, false);
1825         if (rc) {
1826                 otx2_err("Failed to disable sqb aura fc, txq=%u, rc=%d",
1827                          qidx, rc);
1828                 goto done;
1829         }
1830
1831         data->tx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
1832
1833 done:
1834         return rc;
1835 }
1836
1837 static int
1838 otx2_nix_rx_queue_start(struct rte_eth_dev *eth_dev, uint16_t qidx)
1839 {
1840         struct otx2_eth_rxq *rxq = eth_dev->data->rx_queues[qidx];
1841         struct rte_eth_dev_data *data = eth_dev->data;
1842         int rc;
1843
1844         if (data->rx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STARTED)
1845                 return 0;
1846
1847         rc = nix_rq_enb_dis(rxq->eth_dev, rxq, true);
1848         if (rc) {
1849                 otx2_err("Failed to enable rxq=%u, rc=%d", qidx, rc);
1850                 goto done;
1851         }
1852
1853         data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STARTED;
1854
1855 done:
1856         return rc;
1857 }
1858
1859 static int
1860 otx2_nix_rx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t qidx)
1861 {
1862         struct otx2_eth_rxq *rxq = eth_dev->data->rx_queues[qidx];
1863         struct rte_eth_dev_data *data = eth_dev->data;
1864         int rc;
1865
1866         if (data->rx_queue_state[qidx] == RTE_ETH_QUEUE_STATE_STOPPED)
1867                 return 0;
1868
1869         rc = nix_rq_enb_dis(rxq->eth_dev, rxq, false);
1870         if (rc) {
1871                 otx2_err("Failed to disable rxq=%u, rc=%d", qidx, rc);
1872                 goto done;
1873         }
1874
1875         data->rx_queue_state[qidx] = RTE_ETH_QUEUE_STATE_STOPPED;
1876
1877 done:
1878         return rc;
1879 }
1880
1881 static void
1882 otx2_nix_dev_stop(struct rte_eth_dev *eth_dev)
1883 {
1884         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1885         struct rte_mbuf *rx_pkts[32];
1886         struct otx2_eth_rxq *rxq;
1887         int count, i, j, rc;
1888
1889         nix_lf_switch_header_type_enable(dev, false);
1890         nix_cgx_stop_link_event(dev);
1891         npc_rx_disable(dev);
1892
1893         /* Stop rx queues and free up pkts pending */
1894         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1895                 rc = otx2_nix_rx_queue_stop(eth_dev, i);
1896                 if (rc)
1897                         continue;
1898
1899                 rxq = eth_dev->data->rx_queues[i];
1900                 count = dev->rx_pkt_burst_no_offload(rxq, rx_pkts, 32);
1901                 while (count) {
1902                         for (j = 0; j < count; j++)
1903                                 rte_pktmbuf_free(rx_pkts[j]);
1904                         count = dev->rx_pkt_burst_no_offload(rxq, rx_pkts, 32);
1905                 }
1906         }
1907
1908         /* Stop tx queues  */
1909         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1910                 otx2_nix_tx_queue_stop(eth_dev, i);
1911 }
1912
1913 static int
1914 otx2_nix_dev_start(struct rte_eth_dev *eth_dev)
1915 {
1916         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
1917         int rc, i;
1918
1919         /* MTU recalculate should be avoided here if PTP is enabled by PF, as
1920          * otx2_nix_recalc_mtu would be invoked during otx2_nix_ptp_enable_vf
1921          * call below.
1922          */
1923         if (eth_dev->data->nb_rx_queues != 0 && !otx2_ethdev_is_ptp_en(dev)) {
1924                 rc = otx2_nix_recalc_mtu(eth_dev);
1925                 if (rc)
1926                         return rc;
1927         }
1928
1929         /* Start rx queues */
1930         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1931                 rc = otx2_nix_rx_queue_start(eth_dev, i);
1932                 if (rc)
1933                         return rc;
1934         }
1935
1936         /* Start tx queues  */
1937         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
1938                 rc = otx2_nix_tx_queue_start(eth_dev, i);
1939                 if (rc)
1940                         return rc;
1941         }
1942
1943         rc = otx2_nix_update_flow_ctrl_mode(eth_dev);
1944         if (rc) {
1945                 otx2_err("Failed to update flow ctrl mode %d", rc);
1946                 return rc;
1947         }
1948
1949         /* Enable PTP if it was requested by the app or if it is already
1950          * enabled in PF owning this VF
1951          */
1952         memset(&dev->tstamp, 0, sizeof(struct otx2_timesync_info));
1953         if ((dev->rx_offloads & DEV_RX_OFFLOAD_TIMESTAMP) ||
1954             otx2_ethdev_is_ptp_en(dev))
1955                 otx2_nix_timesync_enable(eth_dev);
1956         else
1957                 otx2_nix_timesync_disable(eth_dev);
1958
1959         /* Update VF about data off shifted by 8 bytes if PTP already
1960          * enabled in PF owning this VF
1961          */
1962         if (otx2_ethdev_is_ptp_en(dev) && otx2_dev_is_vf(dev))
1963                 otx2_nix_ptp_enable_vf(eth_dev);
1964
1965         rc = npc_rx_enable(dev);
1966         if (rc) {
1967                 otx2_err("Failed to enable NPC rx %d", rc);
1968                 return rc;
1969         }
1970
1971         otx2_nix_toggle_flag_link_cfg(dev, true);
1972
1973         rc = nix_cgx_start_link_event(dev);
1974         if (rc) {
1975                 otx2_err("Failed to start cgx link event %d", rc);
1976                 goto rx_disable;
1977         }
1978
1979         otx2_nix_toggle_flag_link_cfg(dev, false);
1980         otx2_eth_set_tx_function(eth_dev);
1981         otx2_eth_set_rx_function(eth_dev);
1982
1983         return 0;
1984
1985 rx_disable:
1986         npc_rx_disable(dev);
1987         otx2_nix_toggle_flag_link_cfg(dev, false);
1988         return rc;
1989 }
1990
1991 static int otx2_nix_dev_reset(struct rte_eth_dev *eth_dev);
1992 static void otx2_nix_dev_close(struct rte_eth_dev *eth_dev);
1993
1994 /* Initialize and register driver with DPDK Application */
1995 static const struct eth_dev_ops otx2_eth_dev_ops = {
1996         .dev_infos_get            = otx2_nix_info_get,
1997         .dev_configure            = otx2_nix_configure,
1998         .link_update              = otx2_nix_link_update,
1999         .tx_queue_setup           = otx2_nix_tx_queue_setup,
2000         .tx_queue_release         = otx2_nix_tx_queue_release,
2001         .rx_queue_setup           = otx2_nix_rx_queue_setup,
2002         .rx_queue_release         = otx2_nix_rx_queue_release,
2003         .dev_start                = otx2_nix_dev_start,
2004         .dev_stop                 = otx2_nix_dev_stop,
2005         .dev_close                = otx2_nix_dev_close,
2006         .tx_queue_start           = otx2_nix_tx_queue_start,
2007         .tx_queue_stop            = otx2_nix_tx_queue_stop,
2008         .rx_queue_start           = otx2_nix_rx_queue_start,
2009         .rx_queue_stop            = otx2_nix_rx_queue_stop,
2010         .dev_set_link_up          = otx2_nix_dev_set_link_up,
2011         .dev_set_link_down        = otx2_nix_dev_set_link_down,
2012         .dev_supported_ptypes_get = otx2_nix_supported_ptypes_get,
2013         .dev_ptypes_set           = otx2_nix_ptypes_set,
2014         .dev_reset                = otx2_nix_dev_reset,
2015         .stats_get                = otx2_nix_dev_stats_get,
2016         .stats_reset              = otx2_nix_dev_stats_reset,
2017         .get_reg                  = otx2_nix_dev_get_reg,
2018         .mtu_set                  = otx2_nix_mtu_set,
2019         .mac_addr_add             = otx2_nix_mac_addr_add,
2020         .mac_addr_remove          = otx2_nix_mac_addr_del,
2021         .mac_addr_set             = otx2_nix_mac_addr_set,
2022         .set_mc_addr_list         = otx2_nix_set_mc_addr_list,
2023         .promiscuous_enable       = otx2_nix_promisc_enable,
2024         .promiscuous_disable      = otx2_nix_promisc_disable,
2025         .allmulticast_enable      = otx2_nix_allmulticast_enable,
2026         .allmulticast_disable     = otx2_nix_allmulticast_disable,
2027         .queue_stats_mapping_set  = otx2_nix_queue_stats_mapping,
2028         .reta_update              = otx2_nix_dev_reta_update,
2029         .reta_query               = otx2_nix_dev_reta_query,
2030         .rss_hash_update          = otx2_nix_rss_hash_update,
2031         .rss_hash_conf_get        = otx2_nix_rss_hash_conf_get,
2032         .xstats_get               = otx2_nix_xstats_get,
2033         .xstats_get_names         = otx2_nix_xstats_get_names,
2034         .xstats_reset             = otx2_nix_xstats_reset,
2035         .xstats_get_by_id         = otx2_nix_xstats_get_by_id,
2036         .xstats_get_names_by_id   = otx2_nix_xstats_get_names_by_id,
2037         .rxq_info_get             = otx2_nix_rxq_info_get,
2038         .txq_info_get             = otx2_nix_txq_info_get,
2039         .rx_burst_mode_get        = otx2_rx_burst_mode_get,
2040         .tx_burst_mode_get        = otx2_tx_burst_mode_get,
2041         .rx_queue_count           = otx2_nix_rx_queue_count,
2042         .rx_descriptor_done       = otx2_nix_rx_descriptor_done,
2043         .rx_descriptor_status     = otx2_nix_rx_descriptor_status,
2044         .tx_descriptor_status     = otx2_nix_tx_descriptor_status,
2045         .tx_done_cleanup          = otx2_nix_tx_done_cleanup,
2046         .pool_ops_supported       = otx2_nix_pool_ops_supported,
2047         .filter_ctrl              = otx2_nix_dev_filter_ctrl,
2048         .get_module_info          = otx2_nix_get_module_info,
2049         .get_module_eeprom        = otx2_nix_get_module_eeprom,
2050         .fw_version_get           = otx2_nix_fw_version_get,
2051         .flow_ctrl_get            = otx2_nix_flow_ctrl_get,
2052         .flow_ctrl_set            = otx2_nix_flow_ctrl_set,
2053         .timesync_enable          = otx2_nix_timesync_enable,
2054         .timesync_disable         = otx2_nix_timesync_disable,
2055         .timesync_read_rx_timestamp = otx2_nix_timesync_read_rx_timestamp,
2056         .timesync_read_tx_timestamp = otx2_nix_timesync_read_tx_timestamp,
2057         .timesync_adjust_time     = otx2_nix_timesync_adjust_time,
2058         .timesync_read_time       = otx2_nix_timesync_read_time,
2059         .timesync_write_time      = otx2_nix_timesync_write_time,
2060         .vlan_offload_set         = otx2_nix_vlan_offload_set,
2061         .vlan_filter_set          = otx2_nix_vlan_filter_set,
2062         .vlan_strip_queue_set     = otx2_nix_vlan_strip_queue_set,
2063         .vlan_tpid_set            = otx2_nix_vlan_tpid_set,
2064         .vlan_pvid_set            = otx2_nix_vlan_pvid_set,
2065         .rx_queue_intr_enable     = otx2_nix_rx_queue_intr_enable,
2066         .rx_queue_intr_disable    = otx2_nix_rx_queue_intr_disable,
2067         .read_clock               = otx2_nix_read_clock,
2068 };
2069
2070 static inline int
2071 nix_lf_attach(struct otx2_eth_dev *dev)
2072 {
2073         struct otx2_mbox *mbox = dev->mbox;
2074         struct rsrc_attach_req *req;
2075
2076         /* Attach NIX(lf) */
2077         req = otx2_mbox_alloc_msg_attach_resources(mbox);
2078         req->modify = true;
2079         req->nixlf = true;
2080
2081         return otx2_mbox_process(mbox);
2082 }
2083
2084 static inline int
2085 nix_lf_get_msix_offset(struct otx2_eth_dev *dev)
2086 {
2087         struct otx2_mbox *mbox = dev->mbox;
2088         struct msix_offset_rsp *msix_rsp;
2089         int rc;
2090
2091         /* Get NPA and NIX MSIX vector offsets */
2092         otx2_mbox_alloc_msg_msix_offset(mbox);
2093
2094         rc = otx2_mbox_process_msg(mbox, (void *)&msix_rsp);
2095
2096         dev->nix_msixoff = msix_rsp->nix_msixoff;
2097
2098         return rc;
2099 }
2100
2101 static inline int
2102 otx2_eth_dev_lf_detach(struct otx2_mbox *mbox)
2103 {
2104         struct rsrc_detach_req *req;
2105
2106         req = otx2_mbox_alloc_msg_detach_resources(mbox);
2107
2108         /* Detach all except npa lf */
2109         req->partial = true;
2110         req->nixlf = true;
2111         req->sso = true;
2112         req->ssow = true;
2113         req->timlfs = true;
2114         req->cptlfs = true;
2115
2116         return otx2_mbox_process(mbox);
2117 }
2118
2119 static bool
2120 otx2_eth_dev_is_sdp(struct rte_pci_device *pci_dev)
2121 {
2122         if (pci_dev->id.device_id == PCI_DEVID_OCTEONTX2_RVU_SDP_PF ||
2123             pci_dev->id.device_id == PCI_DEVID_OCTEONTX2_RVU_SDP_VF)
2124                 return true;
2125         return false;
2126 }
2127
2128 static int
2129 otx2_eth_dev_init(struct rte_eth_dev *eth_dev)
2130 {
2131         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
2132         struct rte_pci_device *pci_dev;
2133         int rc, max_entries;
2134
2135         eth_dev->dev_ops = &otx2_eth_dev_ops;
2136
2137         /* For secondary processes, the primary has done all the work */
2138         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2139                 /* Setup callbacks for secondary process */
2140                 otx2_eth_set_tx_function(eth_dev);
2141                 otx2_eth_set_rx_function(eth_dev);
2142                 return 0;
2143         }
2144
2145         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2146
2147         rte_eth_copy_pci_info(eth_dev, pci_dev);
2148         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
2149
2150         /* Zero out everything after OTX2_DEV to allow proper dev_reset() */
2151         memset(&dev->otx2_eth_dev_data_start, 0, sizeof(*dev) -
2152                 offsetof(struct otx2_eth_dev, otx2_eth_dev_data_start));
2153
2154         /* Parse devargs string */
2155         rc = otx2_ethdev_parse_devargs(eth_dev->device->devargs, dev);
2156         if (rc) {
2157                 otx2_err("Failed to parse devargs rc=%d", rc);
2158                 goto error;
2159         }
2160
2161         if (!dev->mbox_active) {
2162                 /* Initialize the base otx2_dev object
2163                  * only if already present
2164                  */
2165                 rc = otx2_dev_init(pci_dev, dev);
2166                 if (rc) {
2167                         otx2_err("Failed to initialize otx2_dev rc=%d", rc);
2168                         goto error;
2169                 }
2170         }
2171         if (otx2_eth_dev_is_sdp(pci_dev))
2172                 dev->sdp_link = true;
2173         else
2174                 dev->sdp_link = false;
2175         /* Device generic callbacks */
2176         dev->ops = &otx2_dev_ops;
2177         dev->eth_dev = eth_dev;
2178
2179         /* Grab the NPA LF if required */
2180         rc = otx2_npa_lf_init(pci_dev, dev);
2181         if (rc)
2182                 goto otx2_dev_uninit;
2183
2184         dev->configured = 0;
2185         dev->drv_inited = true;
2186         dev->ptype_disable = 0;
2187         dev->base = dev->bar2 + (RVU_BLOCK_ADDR_NIX0 << 20);
2188         dev->lmt_addr = dev->bar2 + (RVU_BLOCK_ADDR_LMT << 20);
2189
2190         /* Attach NIX LF */
2191         rc = nix_lf_attach(dev);
2192         if (rc)
2193                 goto otx2_npa_uninit;
2194
2195         /* Get NIX MSIX offset */
2196         rc = nix_lf_get_msix_offset(dev);
2197         if (rc)
2198                 goto otx2_npa_uninit;
2199
2200         /* Register LF irq handlers */
2201         rc = otx2_nix_register_irqs(eth_dev);
2202         if (rc)
2203                 goto mbox_detach;
2204
2205         /* Get maximum number of supported MAC entries */
2206         max_entries = otx2_cgx_mac_max_entries_get(dev);
2207         if (max_entries < 0) {
2208                 otx2_err("Failed to get max entries for mac addr");
2209                 rc = -ENOTSUP;
2210                 goto unregister_irq;
2211         }
2212
2213         /* For VFs, returned max_entries will be 0. But to keep default MAC
2214          * address, one entry must be allocated. So setting up to 1.
2215          */
2216         if (max_entries == 0)
2217                 max_entries = 1;
2218
2219         eth_dev->data->mac_addrs = rte_zmalloc("mac_addr", max_entries *
2220                                                RTE_ETHER_ADDR_LEN, 0);
2221         if (eth_dev->data->mac_addrs == NULL) {
2222                 otx2_err("Failed to allocate memory for mac addr");
2223                 rc = -ENOMEM;
2224                 goto unregister_irq;
2225         }
2226
2227         dev->max_mac_entries = max_entries;
2228
2229         rc = otx2_nix_mac_addr_get(eth_dev, dev->mac_addr);
2230         if (rc)
2231                 goto free_mac_addrs;
2232
2233         /* Update the mac address */
2234         memcpy(eth_dev->data->mac_addrs, dev->mac_addr, RTE_ETHER_ADDR_LEN);
2235
2236         /* Also sync same MAC address to CGX table */
2237         otx2_cgx_mac_addr_set(eth_dev, &eth_dev->data->mac_addrs[0]);
2238
2239         /* Initialize the tm data structures */
2240         otx2_nix_tm_conf_init(eth_dev);
2241
2242         dev->tx_offload_capa = nix_get_tx_offload_capa(dev);
2243         dev->rx_offload_capa = nix_get_rx_offload_capa(dev);
2244
2245         if (otx2_dev_is_96xx_A0(dev) ||
2246             otx2_dev_is_95xx_Ax(dev)) {
2247                 dev->hwcap |= OTX2_FIXUP_F_MIN_4K_Q;
2248                 dev->hwcap |= OTX2_FIXUP_F_LIMIT_CQ_FULL;
2249         }
2250
2251         /* Initialize rte-flow */
2252         rc = otx2_flow_init(dev);
2253         if (rc)
2254                 goto free_mac_addrs;
2255
2256         otx2_nix_mc_filter_init(dev);
2257
2258         otx2_nix_dbg("Port=%d pf=%d vf=%d ver=%s msix_off=%d hwcap=0x%" PRIx64
2259                      " rxoffload_capa=0x%" PRIx64 " txoffload_capa=0x%" PRIx64,
2260                      eth_dev->data->port_id, dev->pf, dev->vf,
2261                      OTX2_ETH_DEV_PMD_VERSION, dev->nix_msixoff, dev->hwcap,
2262                      dev->rx_offload_capa, dev->tx_offload_capa);
2263         return 0;
2264
2265 free_mac_addrs:
2266         rte_free(eth_dev->data->mac_addrs);
2267 unregister_irq:
2268         otx2_nix_unregister_irqs(eth_dev);
2269 mbox_detach:
2270         otx2_eth_dev_lf_detach(dev->mbox);
2271 otx2_npa_uninit:
2272         otx2_npa_lf_fini();
2273 otx2_dev_uninit:
2274         otx2_dev_fini(pci_dev, dev);
2275 error:
2276         otx2_err("Failed to init nix eth_dev rc=%d", rc);
2277         return rc;
2278 }
2279
2280 static int
2281 otx2_eth_dev_uninit(struct rte_eth_dev *eth_dev, bool mbox_close)
2282 {
2283         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
2284         struct rte_pci_device *pci_dev;
2285         int rc, i;
2286
2287         /* Nothing to be done for secondary processes */
2288         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2289                 return 0;
2290
2291         /* Clear the flag since we are closing down */
2292         dev->configured = 0;
2293
2294         /* Disable nix bpid config */
2295         otx2_nix_rxchan_bpid_cfg(eth_dev, false);
2296
2297         npc_rx_disable(dev);
2298
2299         /* Disable vlan offloads */
2300         otx2_nix_vlan_fini(eth_dev);
2301
2302         /* Disable other rte_flow entries */
2303         otx2_flow_fini(dev);
2304
2305         /* Free multicast filter list */
2306         otx2_nix_mc_filter_fini(dev);
2307
2308         /* Disable PTP if already enabled */
2309         if (otx2_ethdev_is_ptp_en(dev))
2310                 otx2_nix_timesync_disable(eth_dev);
2311
2312         nix_cgx_stop_link_event(dev);
2313
2314         /* Free up SQs */
2315         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
2316                 otx2_nix_tx_queue_release(eth_dev->data->tx_queues[i]);
2317                 eth_dev->data->tx_queues[i] = NULL;
2318         }
2319         eth_dev->data->nb_tx_queues = 0;
2320
2321         /* Free up RQ's and CQ's */
2322         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
2323                 otx2_nix_rx_queue_release(eth_dev->data->rx_queues[i]);
2324                 eth_dev->data->rx_queues[i] = NULL;
2325         }
2326         eth_dev->data->nb_rx_queues = 0;
2327
2328         /* Free tm resources */
2329         rc = otx2_nix_tm_fini(eth_dev);
2330         if (rc)
2331                 otx2_err("Failed to cleanup tm, rc=%d", rc);
2332
2333         /* Unregister queue irqs */
2334         oxt2_nix_unregister_queue_irqs(eth_dev);
2335
2336         /* Unregister cq irqs */
2337         if (eth_dev->data->dev_conf.intr_conf.rxq)
2338                 oxt2_nix_unregister_cq_irqs(eth_dev);
2339
2340         rc = nix_lf_free(dev);
2341         if (rc)
2342                 otx2_err("Failed to free nix lf, rc=%d", rc);
2343
2344         rc = otx2_npa_lf_fini();
2345         if (rc)
2346                 otx2_err("Failed to cleanup npa lf, rc=%d", rc);
2347
2348         rte_free(eth_dev->data->mac_addrs);
2349         eth_dev->data->mac_addrs = NULL;
2350         dev->drv_inited = false;
2351
2352         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
2353         otx2_nix_unregister_irqs(eth_dev);
2354
2355         rc = otx2_eth_dev_lf_detach(dev->mbox);
2356         if (rc)
2357                 otx2_err("Failed to detach resources, rc=%d", rc);
2358
2359         /* Check if mbox close is needed */
2360         if (!mbox_close)
2361                 return 0;
2362
2363         if (otx2_npa_lf_active(dev) || otx2_dev_active_vfs(dev)) {
2364                 /* Will be freed later by PMD */
2365                 eth_dev->data->dev_private = NULL;
2366                 return 0;
2367         }
2368
2369         otx2_dev_fini(pci_dev, dev);
2370         return 0;
2371 }
2372
2373 static void
2374 otx2_nix_dev_close(struct rte_eth_dev *eth_dev)
2375 {
2376         otx2_eth_dev_uninit(eth_dev, true);
2377 }
2378
2379 static int
2380 otx2_nix_dev_reset(struct rte_eth_dev *eth_dev)
2381 {
2382         int rc;
2383
2384         rc = otx2_eth_dev_uninit(eth_dev, false);
2385         if (rc)
2386                 return rc;
2387
2388         return otx2_eth_dev_init(eth_dev);
2389 }
2390
2391 static int
2392 nix_remove(struct rte_pci_device *pci_dev)
2393 {
2394         struct rte_eth_dev *eth_dev;
2395         struct otx2_idev_cfg *idev;
2396         struct otx2_dev *otx2_dev;
2397         int rc;
2398
2399         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
2400         if (eth_dev) {
2401                 /* Cleanup eth dev */
2402                 rc = otx2_eth_dev_uninit(eth_dev, true);
2403                 if (rc)
2404                         return rc;
2405
2406                 rte_eth_dev_pci_release(eth_dev);
2407         }
2408
2409         /* Nothing to be done for secondary processes */
2410         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2411                 return 0;
2412
2413         /* Check for common resources */
2414         idev = otx2_intra_dev_get_cfg();
2415         if (!idev || !idev->npa_lf || idev->npa_lf->pci_dev != pci_dev)
2416                 return 0;
2417
2418         otx2_dev = container_of(idev->npa_lf, struct otx2_dev, npalf);
2419
2420         if (otx2_npa_lf_active(otx2_dev) || otx2_dev_active_vfs(otx2_dev))
2421                 goto exit;
2422
2423         /* Safe to cleanup mbox as no more users */
2424         otx2_dev_fini(pci_dev, otx2_dev);
2425         rte_free(otx2_dev);
2426         return 0;
2427
2428 exit:
2429         otx2_info("%s: common resource in use by other devices", pci_dev->name);
2430         return -EAGAIN;
2431 }
2432
2433 static int
2434 nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
2435 {
2436         int rc;
2437
2438         RTE_SET_USED(pci_drv);
2439
2440         rc = rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct otx2_eth_dev),
2441                                            otx2_eth_dev_init);
2442
2443         /* On error on secondary, recheck if port exists in primary or
2444          * in mid of detach state.
2445          */
2446         if (rte_eal_process_type() != RTE_PROC_PRIMARY && rc)
2447                 if (!rte_eth_dev_allocated(pci_dev->device.name))
2448                         return 0;
2449         return rc;
2450 }
2451
2452 static const struct rte_pci_id pci_nix_map[] = {
2453         {
2454                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_PF)
2455         },
2456         {
2457                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM, PCI_DEVID_OCTEONTX2_RVU_VF)
2458         },
2459         {
2460                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
2461                                PCI_DEVID_OCTEONTX2_RVU_AF_VF)
2462         },
2463         {
2464                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
2465                                PCI_DEVID_OCTEONTX2_RVU_SDP_PF)
2466         },
2467         {
2468                 RTE_PCI_DEVICE(PCI_VENDOR_ID_CAVIUM,
2469                                PCI_DEVID_OCTEONTX2_RVU_SDP_VF)
2470         },
2471         {
2472                 .vendor_id = 0,
2473         },
2474 };
2475
2476 static struct rte_pci_driver pci_nix = {
2477         .id_table = pci_nix_map,
2478         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_NEED_IOVA_AS_VA |
2479                         RTE_PCI_DRV_INTR_LSC,
2480         .probe = nix_probe,
2481         .remove = nix_remove,
2482 };
2483
2484 RTE_PMD_REGISTER_PCI(net_octeontx2, pci_nix);
2485 RTE_PMD_REGISTER_PCI_TABLE(net_octeontx2, pci_nix_map);
2486 RTE_PMD_REGISTER_KMOD_DEP(net_octeontx2, "vfio-pci");