e5590a05eb361df48caa2c8d4feebfa37c09a719
[dpdk.git] / drivers / net / cnxk / cnxk_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4 #include <cnxk_ethdev.h>
5
6 static inline uint64_t
7 nix_get_rx_offload_capa(struct cnxk_eth_dev *dev)
8 {
9         uint64_t capa = CNXK_NIX_RX_OFFLOAD_CAPA;
10
11         if (roc_nix_is_vf_or_sdp(&dev->nix))
12                 capa &= ~DEV_RX_OFFLOAD_TIMESTAMP;
13
14         return capa;
15 }
16
17 static inline uint64_t
18 nix_get_tx_offload_capa(struct cnxk_eth_dev *dev)
19 {
20         RTE_SET_USED(dev);
21         return CNXK_NIX_TX_OFFLOAD_CAPA;
22 }
23
24 static inline uint32_t
25 nix_get_speed_capa(struct cnxk_eth_dev *dev)
26 {
27         uint32_t speed_capa;
28
29         /* Auto negotiation disabled */
30         speed_capa = ETH_LINK_SPEED_FIXED;
31         if (!roc_nix_is_vf_or_sdp(&dev->nix) && !roc_nix_is_lbk(&dev->nix)) {
32                 speed_capa |= ETH_LINK_SPEED_1G | ETH_LINK_SPEED_10G |
33                               ETH_LINK_SPEED_25G | ETH_LINK_SPEED_40G |
34                               ETH_LINK_SPEED_50G | ETH_LINK_SPEED_100G;
35         }
36
37         return speed_capa;
38 }
39
40 static void
41 nix_enable_mseg_on_jumbo(struct cnxk_eth_rxq_sp *rxq)
42 {
43         struct rte_pktmbuf_pool_private *mbp_priv;
44         struct rte_eth_dev *eth_dev;
45         struct cnxk_eth_dev *dev;
46         uint32_t buffsz;
47
48         dev = rxq->dev;
49         eth_dev = dev->eth_dev;
50
51         /* Get rx buffer size */
52         mbp_priv = rte_mempool_get_priv(rxq->qconf.mp);
53         buffsz = mbp_priv->mbuf_data_room_size - RTE_PKTMBUF_HEADROOM;
54
55         if (eth_dev->data->dev_conf.rxmode.max_rx_pkt_len > buffsz) {
56                 dev->rx_offloads |= DEV_RX_OFFLOAD_SCATTER;
57                 dev->tx_offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
58         }
59 }
60
61 static int
62 nix_recalc_mtu(struct rte_eth_dev *eth_dev)
63 {
64         struct rte_eth_dev_data *data = eth_dev->data;
65         struct cnxk_eth_rxq_sp *rxq;
66         uint16_t mtu;
67         int rc;
68
69         rxq = ((struct cnxk_eth_rxq_sp *)data->rx_queues[0]) - 1;
70         /* Setup scatter mode if needed by jumbo */
71         nix_enable_mseg_on_jumbo(rxq);
72
73         /* Setup MTU based on max_rx_pkt_len */
74         mtu = data->dev_conf.rxmode.max_rx_pkt_len - CNXK_NIX_L2_OVERHEAD +
75                                 CNXK_NIX_MAX_VTAG_ACT_SIZE;
76
77         rc = cnxk_nix_mtu_set(eth_dev, mtu);
78         if (rc)
79                 plt_err("Failed to set default MTU size, rc=%d", rc);
80
81         return rc;
82 }
83
84 uint64_t
85 cnxk_nix_rxq_mbuf_setup(struct cnxk_eth_dev *dev)
86 {
87         uint16_t port_id = dev->eth_dev->data->port_id;
88         struct rte_mbuf mb_def;
89         uint64_t *tmp;
90
91         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, data_off) % 8 != 0);
92         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, refcnt) -
93                                  offsetof(struct rte_mbuf, data_off) !=
94                          2);
95         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, nb_segs) -
96                                  offsetof(struct rte_mbuf, data_off) !=
97                          4);
98         RTE_BUILD_BUG_ON(offsetof(struct rte_mbuf, port) -
99                                  offsetof(struct rte_mbuf, data_off) !=
100                          6);
101         mb_def.nb_segs = 1;
102         mb_def.data_off = RTE_PKTMBUF_HEADROOM;
103         mb_def.port = port_id;
104         rte_mbuf_refcnt_set(&mb_def, 1);
105
106         /* Prevent compiler reordering: rearm_data covers previous fields */
107         rte_compiler_barrier();
108         tmp = (uint64_t *)&mb_def.rearm_data;
109
110         return *tmp;
111 }
112
113 static inline uint8_t
114 nix_sq_max_sqe_sz(struct cnxk_eth_dev *dev)
115 {
116         /*
117          * Maximum three segments can be supported with W8, Choose
118          * NIX_MAXSQESZ_W16 for multi segment offload.
119          */
120         if (dev->tx_offloads & DEV_TX_OFFLOAD_MULTI_SEGS)
121                 return NIX_MAXSQESZ_W16;
122         else
123                 return NIX_MAXSQESZ_W8;
124 }
125
126 int
127 cnxk_nix_tx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t qid,
128                         uint16_t nb_desc, uint16_t fp_tx_q_sz,
129                         const struct rte_eth_txconf *tx_conf)
130 {
131         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
132         const struct eth_dev_ops *dev_ops = eth_dev->dev_ops;
133         struct cnxk_eth_txq_sp *txq_sp;
134         struct roc_nix_sq *sq;
135         size_t txq_sz;
136         int rc;
137
138         /* Free memory prior to re-allocation if needed. */
139         if (eth_dev->data->tx_queues[qid] != NULL) {
140                 plt_nix_dbg("Freeing memory prior to re-allocation %d", qid);
141                 dev_ops->tx_queue_release(eth_dev->data->tx_queues[qid]);
142                 eth_dev->data->tx_queues[qid] = NULL;
143         }
144
145         /* Setup ROC SQ */
146         sq = &dev->sqs[qid];
147         sq->qid = qid;
148         sq->nb_desc = nb_desc;
149         sq->max_sqe_sz = nix_sq_max_sqe_sz(dev);
150
151         rc = roc_nix_sq_init(&dev->nix, sq);
152         if (rc) {
153                 plt_err("Failed to init sq=%d, rc=%d", qid, rc);
154                 return rc;
155         }
156
157         rc = -ENOMEM;
158         txq_sz = sizeof(struct cnxk_eth_txq_sp) + fp_tx_q_sz;
159         txq_sp = plt_zmalloc(txq_sz, PLT_CACHE_LINE_SIZE);
160         if (!txq_sp) {
161                 plt_err("Failed to alloc tx queue mem");
162                 rc |= roc_nix_sq_fini(sq);
163                 return rc;
164         }
165
166         txq_sp->dev = dev;
167         txq_sp->qid = qid;
168         txq_sp->qconf.conf.tx = *tx_conf;
169         txq_sp->qconf.nb_desc = nb_desc;
170
171         plt_nix_dbg("sq=%d fc=%p offload=0x%" PRIx64 " lmt_addr=%p"
172                     " nb_sqb_bufs=%d sqes_per_sqb_log2=%d",
173                     qid, sq->fc, dev->tx_offloads, sq->lmt_addr,
174                     sq->nb_sqb_bufs, sq->sqes_per_sqb_log2);
175
176         /* Store start of fast path area */
177         eth_dev->data->tx_queues[qid] = txq_sp + 1;
178         eth_dev->data->tx_queue_state[qid] = RTE_ETH_QUEUE_STATE_STOPPED;
179         return 0;
180 }
181
182 static void
183 cnxk_nix_tx_queue_release(void *txq)
184 {
185         struct cnxk_eth_txq_sp *txq_sp;
186         struct cnxk_eth_dev *dev;
187         struct roc_nix_sq *sq;
188         uint16_t qid;
189         int rc;
190
191         if (!txq)
192                 return;
193
194         txq_sp = cnxk_eth_txq_to_sp(txq);
195         dev = txq_sp->dev;
196         qid = txq_sp->qid;
197
198         plt_nix_dbg("Releasing txq %u", qid);
199
200         /* Cleanup ROC SQ */
201         sq = &dev->sqs[qid];
202         rc = roc_nix_sq_fini(sq);
203         if (rc)
204                 plt_err("Failed to cleanup sq, rc=%d", rc);
205
206         /* Finally free */
207         plt_free(txq_sp);
208 }
209
210 int
211 cnxk_nix_rx_queue_setup(struct rte_eth_dev *eth_dev, uint16_t qid,
212                         uint16_t nb_desc, uint16_t fp_rx_q_sz,
213                         const struct rte_eth_rxconf *rx_conf,
214                         struct rte_mempool *mp)
215 {
216         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
217         struct cnxk_eth_rxq_sp *rxq_sp;
218         struct rte_mempool_ops *ops;
219         const char *platform_ops;
220         struct roc_nix_rq *rq;
221         struct roc_nix_cq *cq;
222         uint16_t first_skip;
223         int rc = -EINVAL;
224         size_t rxq_sz;
225
226         /* Sanity checks */
227         if (rx_conf->rx_deferred_start == 1) {
228                 plt_err("Deferred Rx start is not supported");
229                 goto fail;
230         }
231
232         platform_ops = rte_mbuf_platform_mempool_ops();
233         /* This driver needs cnxk_npa mempool ops to work */
234         ops = rte_mempool_get_ops(mp->ops_index);
235         if (strncmp(ops->name, platform_ops, RTE_MEMPOOL_OPS_NAMESIZE)) {
236                 plt_err("mempool ops should be of cnxk_npa type");
237                 goto fail;
238         }
239
240         if (mp->pool_id == 0) {
241                 plt_err("Invalid pool_id");
242                 goto fail;
243         }
244
245         /* Free memory prior to re-allocation if needed */
246         if (eth_dev->data->rx_queues[qid] != NULL) {
247                 const struct eth_dev_ops *dev_ops = eth_dev->dev_ops;
248
249                 plt_nix_dbg("Freeing memory prior to re-allocation %d", qid);
250                 dev_ops->rx_queue_release(eth_dev->data->rx_queues[qid]);
251                 eth_dev->data->rx_queues[qid] = NULL;
252         }
253
254         /* Setup ROC CQ */
255         cq = &dev->cqs[qid];
256         cq->qid = qid;
257         cq->nb_desc = nb_desc;
258         rc = roc_nix_cq_init(&dev->nix, cq);
259         if (rc) {
260                 plt_err("Failed to init roc cq for rq=%d, rc=%d", qid, rc);
261                 goto fail;
262         }
263
264         /* Setup ROC RQ */
265         rq = &dev->rqs[qid];
266         rq->qid = qid;
267         rq->aura_handle = mp->pool_id;
268         rq->flow_tag_width = 32;
269         rq->sso_ena = false;
270
271         /* Calculate first mbuf skip */
272         first_skip = (sizeof(struct rte_mbuf));
273         first_skip += RTE_PKTMBUF_HEADROOM;
274         first_skip += rte_pktmbuf_priv_size(mp);
275         rq->first_skip = first_skip;
276         rq->later_skip = sizeof(struct rte_mbuf);
277         rq->lpb_size = mp->elt_size;
278
279         rc = roc_nix_rq_init(&dev->nix, rq, !!eth_dev->data->dev_started);
280         if (rc) {
281                 plt_err("Failed to init roc rq for rq=%d, rc=%d", qid, rc);
282                 goto cq_fini;
283         }
284
285         /* Allocate and setup fast path rx queue */
286         rc = -ENOMEM;
287         rxq_sz = sizeof(struct cnxk_eth_rxq_sp) + fp_rx_q_sz;
288         rxq_sp = plt_zmalloc(rxq_sz, PLT_CACHE_LINE_SIZE);
289         if (!rxq_sp) {
290                 plt_err("Failed to alloc rx queue for rq=%d", qid);
291                 goto rq_fini;
292         }
293
294         /* Setup slow path fields */
295         rxq_sp->dev = dev;
296         rxq_sp->qid = qid;
297         rxq_sp->qconf.conf.rx = *rx_conf;
298         rxq_sp->qconf.nb_desc = nb_desc;
299         rxq_sp->qconf.mp = mp;
300
301         plt_nix_dbg("rq=%d pool=%s nb_desc=%d->%d", qid, mp->name, nb_desc,
302                     cq->nb_desc);
303
304         /* Store start of fast path area */
305         eth_dev->data->rx_queues[qid] = rxq_sp + 1;
306         eth_dev->data->rx_queue_state[qid] = RTE_ETH_QUEUE_STATE_STOPPED;
307
308         return 0;
309 rq_fini:
310         rc |= roc_nix_rq_fini(rq);
311 cq_fini:
312         rc |= roc_nix_cq_fini(cq);
313 fail:
314         return rc;
315 }
316
317 static void
318 cnxk_nix_rx_queue_release(void *rxq)
319 {
320         struct cnxk_eth_rxq_sp *rxq_sp;
321         struct cnxk_eth_dev *dev;
322         struct roc_nix_rq *rq;
323         struct roc_nix_cq *cq;
324         uint16_t qid;
325         int rc;
326
327         if (!rxq)
328                 return;
329
330         rxq_sp = cnxk_eth_rxq_to_sp(rxq);
331         dev = rxq_sp->dev;
332         qid = rxq_sp->qid;
333
334         plt_nix_dbg("Releasing rxq %u", qid);
335
336         /* Cleanup ROC RQ */
337         rq = &dev->rqs[qid];
338         rc = roc_nix_rq_fini(rq);
339         if (rc)
340                 plt_err("Failed to cleanup rq, rc=%d", rc);
341
342         /* Cleanup ROC CQ */
343         cq = &dev->cqs[qid];
344         rc = roc_nix_cq_fini(cq);
345         if (rc)
346                 plt_err("Failed to cleanup cq, rc=%d", rc);
347
348         /* Finally free fast path area */
349         plt_free(rxq_sp);
350 }
351
352 uint32_t
353 cnxk_rss_ethdev_to_nix(struct cnxk_eth_dev *dev, uint64_t ethdev_rss,
354                        uint8_t rss_level)
355 {
356         uint32_t flow_key_type[RSS_MAX_LEVELS][6] = {
357                 {FLOW_KEY_TYPE_IPV4, FLOW_KEY_TYPE_IPV6, FLOW_KEY_TYPE_TCP,
358                  FLOW_KEY_TYPE_UDP, FLOW_KEY_TYPE_SCTP, FLOW_KEY_TYPE_ETH_DMAC},
359                 {FLOW_KEY_TYPE_INNR_IPV4, FLOW_KEY_TYPE_INNR_IPV6,
360                  FLOW_KEY_TYPE_INNR_TCP, FLOW_KEY_TYPE_INNR_UDP,
361                  FLOW_KEY_TYPE_INNR_SCTP, FLOW_KEY_TYPE_INNR_ETH_DMAC},
362                 {FLOW_KEY_TYPE_IPV4 | FLOW_KEY_TYPE_INNR_IPV4,
363                  FLOW_KEY_TYPE_IPV6 | FLOW_KEY_TYPE_INNR_IPV6,
364                  FLOW_KEY_TYPE_TCP | FLOW_KEY_TYPE_INNR_TCP,
365                  FLOW_KEY_TYPE_UDP | FLOW_KEY_TYPE_INNR_UDP,
366                  FLOW_KEY_TYPE_SCTP | FLOW_KEY_TYPE_INNR_SCTP,
367                  FLOW_KEY_TYPE_ETH_DMAC | FLOW_KEY_TYPE_INNR_ETH_DMAC}
368         };
369         uint32_t flowkey_cfg = 0;
370
371         dev->ethdev_rss_hf = ethdev_rss;
372
373         if (ethdev_rss & ETH_RSS_L2_PAYLOAD)
374                 flowkey_cfg |= FLOW_KEY_TYPE_CH_LEN_90B;
375
376         if (ethdev_rss & ETH_RSS_C_VLAN)
377                 flowkey_cfg |= FLOW_KEY_TYPE_VLAN;
378
379         if (ethdev_rss & ETH_RSS_L3_SRC_ONLY)
380                 flowkey_cfg |= FLOW_KEY_TYPE_L3_SRC;
381
382         if (ethdev_rss & ETH_RSS_L3_DST_ONLY)
383                 flowkey_cfg |= FLOW_KEY_TYPE_L3_DST;
384
385         if (ethdev_rss & ETH_RSS_L4_SRC_ONLY)
386                 flowkey_cfg |= FLOW_KEY_TYPE_L4_SRC;
387
388         if (ethdev_rss & ETH_RSS_L4_DST_ONLY)
389                 flowkey_cfg |= FLOW_KEY_TYPE_L4_DST;
390
391         if (ethdev_rss & RSS_IPV4_ENABLE)
392                 flowkey_cfg |= flow_key_type[rss_level][RSS_IPV4_INDEX];
393
394         if (ethdev_rss & RSS_IPV6_ENABLE)
395                 flowkey_cfg |= flow_key_type[rss_level][RSS_IPV6_INDEX];
396
397         if (ethdev_rss & ETH_RSS_TCP)
398                 flowkey_cfg |= flow_key_type[rss_level][RSS_TCP_INDEX];
399
400         if (ethdev_rss & ETH_RSS_UDP)
401                 flowkey_cfg |= flow_key_type[rss_level][RSS_UDP_INDEX];
402
403         if (ethdev_rss & ETH_RSS_SCTP)
404                 flowkey_cfg |= flow_key_type[rss_level][RSS_SCTP_INDEX];
405
406         if (ethdev_rss & ETH_RSS_L2_PAYLOAD)
407                 flowkey_cfg |= flow_key_type[rss_level][RSS_DMAC_INDEX];
408
409         if (ethdev_rss & RSS_IPV6_EX_ENABLE)
410                 flowkey_cfg |= FLOW_KEY_TYPE_IPV6_EXT;
411
412         if (ethdev_rss & ETH_RSS_PORT)
413                 flowkey_cfg |= FLOW_KEY_TYPE_PORT;
414
415         if (ethdev_rss & ETH_RSS_NVGRE)
416                 flowkey_cfg |= FLOW_KEY_TYPE_NVGRE;
417
418         if (ethdev_rss & ETH_RSS_VXLAN)
419                 flowkey_cfg |= FLOW_KEY_TYPE_VXLAN;
420
421         if (ethdev_rss & ETH_RSS_GENEVE)
422                 flowkey_cfg |= FLOW_KEY_TYPE_GENEVE;
423
424         if (ethdev_rss & ETH_RSS_GTPU)
425                 flowkey_cfg |= FLOW_KEY_TYPE_GTPU;
426
427         return flowkey_cfg;
428 }
429
430 static void
431 nix_free_queue_mem(struct cnxk_eth_dev *dev)
432 {
433         plt_free(dev->rqs);
434         plt_free(dev->cqs);
435         plt_free(dev->sqs);
436         dev->rqs = NULL;
437         dev->cqs = NULL;
438         dev->sqs = NULL;
439 }
440
441 static int
442 nix_rss_default_setup(struct cnxk_eth_dev *dev)
443 {
444         struct rte_eth_dev *eth_dev = dev->eth_dev;
445         uint8_t rss_hash_level;
446         uint32_t flowkey_cfg;
447         uint64_t rss_hf;
448
449         rss_hf = eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf;
450         rss_hash_level = ETH_RSS_LEVEL(rss_hf);
451         if (rss_hash_level)
452                 rss_hash_level -= 1;
453
454         flowkey_cfg = cnxk_rss_ethdev_to_nix(dev, rss_hf, rss_hash_level);
455         return roc_nix_rss_default_setup(&dev->nix, flowkey_cfg);
456 }
457
458 static int
459 nix_store_queue_cfg_and_then_release(struct rte_eth_dev *eth_dev)
460 {
461         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
462         const struct eth_dev_ops *dev_ops = eth_dev->dev_ops;
463         struct cnxk_eth_qconf *tx_qconf = NULL;
464         struct cnxk_eth_qconf *rx_qconf = NULL;
465         struct cnxk_eth_rxq_sp *rxq_sp;
466         struct cnxk_eth_txq_sp *txq_sp;
467         int i, nb_rxq, nb_txq;
468         void **txq, **rxq;
469
470         nb_rxq = RTE_MIN(dev->nb_rxq, eth_dev->data->nb_rx_queues);
471         nb_txq = RTE_MIN(dev->nb_txq, eth_dev->data->nb_tx_queues);
472
473         tx_qconf = malloc(nb_txq * sizeof(*tx_qconf));
474         if (tx_qconf == NULL) {
475                 plt_err("Failed to allocate memory for tx_qconf");
476                 goto fail;
477         }
478
479         rx_qconf = malloc(nb_rxq * sizeof(*rx_qconf));
480         if (rx_qconf == NULL) {
481                 plt_err("Failed to allocate memory for rx_qconf");
482                 goto fail;
483         }
484
485         txq = eth_dev->data->tx_queues;
486         for (i = 0; i < nb_txq; i++) {
487                 if (txq[i] == NULL) {
488                         tx_qconf[i].valid = false;
489                         plt_info("txq[%d] is already released", i);
490                         continue;
491                 }
492                 txq_sp = cnxk_eth_txq_to_sp(txq[i]);
493                 memcpy(&tx_qconf[i], &txq_sp->qconf, sizeof(*tx_qconf));
494                 tx_qconf[i].valid = true;
495                 dev_ops->tx_queue_release(txq[i]);
496                 eth_dev->data->tx_queues[i] = NULL;
497         }
498
499         rxq = eth_dev->data->rx_queues;
500         for (i = 0; i < nb_rxq; i++) {
501                 if (rxq[i] == NULL) {
502                         rx_qconf[i].valid = false;
503                         plt_info("rxq[%d] is already released", i);
504                         continue;
505                 }
506                 rxq_sp = cnxk_eth_rxq_to_sp(rxq[i]);
507                 memcpy(&rx_qconf[i], &rxq_sp->qconf, sizeof(*rx_qconf));
508                 rx_qconf[i].valid = true;
509                 dev_ops->rx_queue_release(rxq[i]);
510                 eth_dev->data->rx_queues[i] = NULL;
511         }
512
513         dev->tx_qconf = tx_qconf;
514         dev->rx_qconf = rx_qconf;
515         return 0;
516
517 fail:
518         free(tx_qconf);
519         free(rx_qconf);
520         return -ENOMEM;
521 }
522
523 static int
524 nix_restore_queue_cfg(struct rte_eth_dev *eth_dev)
525 {
526         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
527         const struct eth_dev_ops *dev_ops = eth_dev->dev_ops;
528         struct cnxk_eth_qconf *tx_qconf = dev->tx_qconf;
529         struct cnxk_eth_qconf *rx_qconf = dev->rx_qconf;
530         int rc, i, nb_rxq, nb_txq;
531         void **txq, **rxq;
532
533         nb_rxq = RTE_MIN(dev->nb_rxq, eth_dev->data->nb_rx_queues);
534         nb_txq = RTE_MIN(dev->nb_txq, eth_dev->data->nb_tx_queues);
535
536         rc = -ENOMEM;
537         /* Setup tx & rx queues with previous configuration so
538          * that the queues can be functional in cases like ports
539          * are started without re configuring queues.
540          *
541          * Usual re config sequence is like below:
542          * port_configure() {
543          *      if(reconfigure) {
544          *              queue_release()
545          *              queue_setup()
546          *      }
547          *      queue_configure() {
548          *              queue_release()
549          *              queue_setup()
550          *      }
551          * }
552          * port_start()
553          *
554          * In some application's control path, queue_configure() would
555          * NOT be invoked for TXQs/RXQs in port_configure().
556          * In such cases, queues can be functional after start as the
557          * queues are already setup in port_configure().
558          */
559         for (i = 0; i < nb_txq; i++) {
560                 if (!tx_qconf[i].valid)
561                         continue;
562                 rc = dev_ops->tx_queue_setup(eth_dev, i, tx_qconf[i].nb_desc, 0,
563                                              &tx_qconf[i].conf.tx);
564                 if (rc) {
565                         plt_err("Failed to setup tx queue rc=%d", rc);
566                         txq = eth_dev->data->tx_queues;
567                         for (i -= 1; i >= 0; i--)
568                                 dev_ops->tx_queue_release(txq[i]);
569                         goto fail;
570                 }
571         }
572
573         free(tx_qconf);
574         tx_qconf = NULL;
575
576         for (i = 0; i < nb_rxq; i++) {
577                 if (!rx_qconf[i].valid)
578                         continue;
579                 rc = dev_ops->rx_queue_setup(eth_dev, i, rx_qconf[i].nb_desc, 0,
580                                              &rx_qconf[i].conf.rx,
581                                              rx_qconf[i].mp);
582                 if (rc) {
583                         plt_err("Failed to setup rx queue rc=%d", rc);
584                         rxq = eth_dev->data->rx_queues;
585                         for (i -= 1; i >= 0; i--)
586                                 dev_ops->rx_queue_release(rxq[i]);
587                         goto tx_queue_release;
588                 }
589         }
590
591         free(rx_qconf);
592         rx_qconf = NULL;
593
594         return 0;
595
596 tx_queue_release:
597         txq = eth_dev->data->tx_queues;
598         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
599                 dev_ops->tx_queue_release(txq[i]);
600 fail:
601         if (tx_qconf)
602                 free(tx_qconf);
603         if (rx_qconf)
604                 free(rx_qconf);
605
606         return rc;
607 }
608
609 static uint16_t
610 nix_eth_nop_burst(void *queue, struct rte_mbuf **mbufs, uint16_t pkts)
611 {
612         RTE_SET_USED(queue);
613         RTE_SET_USED(mbufs);
614         RTE_SET_USED(pkts);
615
616         return 0;
617 }
618
619 static void
620 nix_set_nop_rxtx_function(struct rte_eth_dev *eth_dev)
621 {
622         /* These dummy functions are required for supporting
623          * some applications which reconfigure queues without
624          * stopping tx burst and rx burst threads(eg kni app)
625          * When the queues context is saved, txq/rxqs are released
626          * which caused app crash since rx/tx burst is still
627          * on different lcores
628          */
629         eth_dev->tx_pkt_burst = nix_eth_nop_burst;
630         eth_dev->rx_pkt_burst = nix_eth_nop_burst;
631         rte_mb();
632 }
633
634 static int
635 nix_lso_tun_fmt_update(struct cnxk_eth_dev *dev)
636 {
637         uint8_t udp_tun[ROC_NIX_LSO_TUN_MAX];
638         uint8_t tun[ROC_NIX_LSO_TUN_MAX];
639         struct roc_nix *nix = &dev->nix;
640         int rc;
641
642         rc = roc_nix_lso_fmt_get(nix, udp_tun, tun);
643         if (rc)
644                 return rc;
645
646         dev->lso_tun_fmt = ((uint64_t)tun[ROC_NIX_LSO_TUN_V4V4] |
647                             (uint64_t)tun[ROC_NIX_LSO_TUN_V4V6] << 8 |
648                             (uint64_t)tun[ROC_NIX_LSO_TUN_V6V4] << 16 |
649                             (uint64_t)tun[ROC_NIX_LSO_TUN_V6V6] << 24);
650
651         dev->lso_tun_fmt |= ((uint64_t)udp_tun[ROC_NIX_LSO_TUN_V4V4] << 32 |
652                              (uint64_t)udp_tun[ROC_NIX_LSO_TUN_V4V6] << 40 |
653                              (uint64_t)udp_tun[ROC_NIX_LSO_TUN_V6V4] << 48 |
654                              (uint64_t)udp_tun[ROC_NIX_LSO_TUN_V6V6] << 56);
655         return 0;
656 }
657
658 static int
659 nix_lso_fmt_setup(struct cnxk_eth_dev *dev)
660 {
661         struct roc_nix *nix = &dev->nix;
662         int rc;
663
664         /* Nothing much to do if offload is not enabled */
665         if (!(dev->tx_offloads &
666               (DEV_TX_OFFLOAD_TCP_TSO | DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
667                DEV_TX_OFFLOAD_GENEVE_TNL_TSO | DEV_TX_OFFLOAD_GRE_TNL_TSO)))
668                 return 0;
669
670         /* Setup LSO formats in AF. Its a no-op if other ethdev has
671          * already set it up
672          */
673         rc = roc_nix_lso_fmt_setup(nix);
674         if (rc)
675                 return rc;
676
677         return nix_lso_tun_fmt_update(dev);
678 }
679
680 int
681 cnxk_nix_configure(struct rte_eth_dev *eth_dev)
682 {
683         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
684         struct rte_eth_dev_data *data = eth_dev->data;
685         struct rte_eth_conf *conf = &data->dev_conf;
686         struct rte_eth_rxmode *rxmode = &conf->rxmode;
687         struct rte_eth_txmode *txmode = &conf->txmode;
688         char ea_fmt[RTE_ETHER_ADDR_FMT_SIZE];
689         struct roc_nix *nix = &dev->nix;
690         struct rte_ether_addr *ea;
691         uint8_t nb_rxq, nb_txq;
692         uint64_t rx_cfg;
693         void *qs;
694         int rc;
695
696         rc = -EINVAL;
697
698         /* Sanity checks */
699         if (rte_eal_has_hugepages() == 0) {
700                 plt_err("Huge page is not configured");
701                 goto fail_configure;
702         }
703
704         if (conf->dcb_capability_en == 1) {
705                 plt_err("dcb enable is not supported");
706                 goto fail_configure;
707         }
708
709         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
710                 plt_err("Flow director is not supported");
711                 goto fail_configure;
712         }
713
714         if (rxmode->mq_mode != ETH_MQ_RX_NONE &&
715             rxmode->mq_mode != ETH_MQ_RX_RSS) {
716                 plt_err("Unsupported mq rx mode %d", rxmode->mq_mode);
717                 goto fail_configure;
718         }
719
720         if (txmode->mq_mode != ETH_MQ_TX_NONE) {
721                 plt_err("Unsupported mq tx mode %d", txmode->mq_mode);
722                 goto fail_configure;
723         }
724
725         /* Free the resources allocated from the previous configure */
726         if (dev->configured == 1) {
727                 /* Unregister queue irq's */
728                 roc_nix_unregister_queue_irqs(nix);
729
730                 /* Unregister CQ irqs if present */
731                 if (eth_dev->data->dev_conf.intr_conf.rxq)
732                         roc_nix_unregister_cq_irqs(nix);
733
734                 /* Set no-op functions */
735                 nix_set_nop_rxtx_function(eth_dev);
736                 /* Store queue config for later */
737                 rc = nix_store_queue_cfg_and_then_release(eth_dev);
738                 if (rc)
739                         goto fail_configure;
740                 roc_nix_tm_fini(nix);
741                 roc_nix_lf_free(nix);
742         }
743
744         dev->rx_offloads = rxmode->offloads;
745         dev->tx_offloads = txmode->offloads;
746
747         /* Prepare rx cfg */
748         rx_cfg = ROC_NIX_LF_RX_CFG_DIS_APAD;
749         if (dev->rx_offloads &
750             (DEV_RX_OFFLOAD_TCP_CKSUM | DEV_RX_OFFLOAD_UDP_CKSUM)) {
751                 rx_cfg |= ROC_NIX_LF_RX_CFG_CSUM_OL4;
752                 rx_cfg |= ROC_NIX_LF_RX_CFG_CSUM_IL4;
753         }
754         rx_cfg |= (ROC_NIX_LF_RX_CFG_DROP_RE | ROC_NIX_LF_RX_CFG_L2_LEN_ERR |
755                    ROC_NIX_LF_RX_CFG_LEN_IL4 | ROC_NIX_LF_RX_CFG_LEN_IL3 |
756                    ROC_NIX_LF_RX_CFG_LEN_OL4 | ROC_NIX_LF_RX_CFG_LEN_OL3);
757
758         nb_rxq = RTE_MAX(data->nb_rx_queues, 1);
759         nb_txq = RTE_MAX(data->nb_tx_queues, 1);
760
761         /* Alloc a nix lf */
762         rc = roc_nix_lf_alloc(nix, nb_rxq, nb_txq, rx_cfg);
763         if (rc) {
764                 plt_err("Failed to init nix_lf rc=%d", rc);
765                 goto fail_configure;
766         }
767
768         nb_rxq = data->nb_rx_queues;
769         nb_txq = data->nb_tx_queues;
770         rc = -ENOMEM;
771         if (nb_rxq) {
772                 /* Allocate memory for roc rq's and cq's */
773                 qs = plt_zmalloc(sizeof(struct roc_nix_rq) * nb_rxq, 0);
774                 if (!qs) {
775                         plt_err("Failed to alloc rqs");
776                         goto free_nix_lf;
777                 }
778                 dev->rqs = qs;
779
780                 qs = plt_zmalloc(sizeof(struct roc_nix_cq) * nb_rxq, 0);
781                 if (!qs) {
782                         plt_err("Failed to alloc cqs");
783                         goto free_nix_lf;
784                 }
785                 dev->cqs = qs;
786         }
787
788         if (nb_txq) {
789                 /* Allocate memory for roc sq's */
790                 qs = plt_zmalloc(sizeof(struct roc_nix_sq) * nb_txq, 0);
791                 if (!qs) {
792                         plt_err("Failed to alloc sqs");
793                         goto free_nix_lf;
794                 }
795                 dev->sqs = qs;
796         }
797
798         /* Re-enable NIX LF error interrupts */
799         roc_nix_err_intr_ena_dis(nix, true);
800         roc_nix_ras_intr_ena_dis(nix, true);
801
802         if (nix->rx_ptp_ena) {
803                 plt_err("Both PTP and switch header enabled");
804                 goto free_nix_lf;
805         }
806
807         /* Setup LSO if needed */
808         rc = nix_lso_fmt_setup(dev);
809         if (rc) {
810                 plt_err("Failed to setup nix lso format fields, rc=%d", rc);
811                 goto free_nix_lf;
812         }
813
814         /* Configure RSS */
815         rc = nix_rss_default_setup(dev);
816         if (rc) {
817                 plt_err("Failed to configure rss rc=%d", rc);
818                 goto free_nix_lf;
819         }
820
821         /* Init the default TM scheduler hierarchy */
822         rc = roc_nix_tm_init(nix);
823         if (rc) {
824                 plt_err("Failed to init traffic manager, rc=%d", rc);
825                 goto free_nix_lf;
826         }
827
828         rc = roc_nix_tm_hierarchy_enable(nix, ROC_NIX_TM_DEFAULT, false);
829         if (rc) {
830                 plt_err("Failed to enable default tm hierarchy, rc=%d", rc);
831                 goto tm_fini;
832         }
833
834         /* Register queue IRQs */
835         rc = roc_nix_register_queue_irqs(nix);
836         if (rc) {
837                 plt_err("Failed to register queue interrupts rc=%d", rc);
838                 goto tm_fini;
839         }
840
841         /* Register cq IRQs */
842         if (eth_dev->data->dev_conf.intr_conf.rxq) {
843                 if (eth_dev->data->nb_rx_queues > dev->nix.cints) {
844                         plt_err("Rx interrupt cannot be enabled, rxq > %d",
845                                 dev->nix.cints);
846                         goto q_irq_fini;
847                 }
848                 /* Rx interrupt feature cannot work with vector mode because,
849                  * vector mode does not process packets unless min 4 pkts are
850                  * received, while cq interrupts are generated even for 1 pkt
851                  * in the CQ.
852                  */
853                 dev->scalar_ena = true;
854
855                 rc = roc_nix_register_cq_irqs(nix);
856                 if (rc) {
857                         plt_err("Failed to register CQ interrupts rc=%d", rc);
858                         goto q_irq_fini;
859                 }
860         }
861
862         /* Configure loop back mode */
863         rc = roc_nix_mac_loopback_enable(nix,
864                                          eth_dev->data->dev_conf.lpbk_mode);
865         if (rc) {
866                 plt_err("Failed to configure cgx loop back mode rc=%d", rc);
867                 goto cq_fini;
868         }
869
870         /*
871          * Restore queue config when reconfigure followed by
872          * reconfigure and no queue configure invoked from application case.
873          */
874         if (dev->configured == 1) {
875                 rc = nix_restore_queue_cfg(eth_dev);
876                 if (rc)
877                         goto cq_fini;
878         }
879
880         /* Update the mac address */
881         ea = eth_dev->data->mac_addrs;
882         memcpy(ea, dev->mac_addr, RTE_ETHER_ADDR_LEN);
883         if (rte_is_zero_ether_addr(ea))
884                 rte_eth_random_addr((uint8_t *)ea);
885
886         rte_ether_format_addr(ea_fmt, RTE_ETHER_ADDR_FMT_SIZE, ea);
887
888         plt_nix_dbg("Configured port%d mac=%s nb_rxq=%d nb_txq=%d"
889                     " rx_offloads=0x%" PRIx64 " tx_offloads=0x%" PRIx64 "",
890                     eth_dev->data->port_id, ea_fmt, nb_rxq, nb_txq,
891                     dev->rx_offloads, dev->tx_offloads);
892
893         /* All good */
894         dev->configured = 1;
895         dev->nb_rxq = data->nb_rx_queues;
896         dev->nb_txq = data->nb_tx_queues;
897         return 0;
898
899 cq_fini:
900         roc_nix_unregister_cq_irqs(nix);
901 q_irq_fini:
902         roc_nix_unregister_queue_irqs(nix);
903 tm_fini:
904         roc_nix_tm_fini(nix);
905 free_nix_lf:
906         nix_free_queue_mem(dev);
907         rc |= roc_nix_lf_free(nix);
908 fail_configure:
909         dev->configured = 0;
910         return rc;
911 }
912
913 static int
914 cnxk_nix_tx_queue_start(struct rte_eth_dev *eth_dev, uint16_t qid)
915 {
916         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
917         struct rte_eth_dev_data *data = eth_dev->data;
918         struct roc_nix_sq *sq = &dev->sqs[qid];
919         int rc = -EINVAL;
920
921         if (data->tx_queue_state[qid] == RTE_ETH_QUEUE_STATE_STARTED)
922                 return 0;
923
924         rc = roc_nix_tm_sq_aura_fc(sq, true);
925         if (rc) {
926                 plt_err("Failed to enable sq aura fc, txq=%u, rc=%d", qid, rc);
927                 goto done;
928         }
929
930         data->tx_queue_state[qid] = RTE_ETH_QUEUE_STATE_STARTED;
931 done:
932         return rc;
933 }
934
935 int
936 cnxk_nix_tx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t qid)
937 {
938         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
939         struct rte_eth_dev_data *data = eth_dev->data;
940         struct roc_nix_sq *sq = &dev->sqs[qid];
941         int rc;
942
943         if (data->tx_queue_state[qid] == RTE_ETH_QUEUE_STATE_STOPPED)
944                 return 0;
945
946         rc = roc_nix_tm_sq_aura_fc(sq, false);
947         if (rc) {
948                 plt_err("Failed to disable sqb aura fc, txq=%u, rc=%d", qid,
949                         rc);
950                 goto done;
951         }
952
953         data->tx_queue_state[qid] = RTE_ETH_QUEUE_STATE_STOPPED;
954 done:
955         return rc;
956 }
957
958 static int
959 cnxk_nix_rx_queue_start(struct rte_eth_dev *eth_dev, uint16_t qid)
960 {
961         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
962         struct rte_eth_dev_data *data = eth_dev->data;
963         struct roc_nix_rq *rq = &dev->rqs[qid];
964         int rc;
965
966         if (data->rx_queue_state[qid] == RTE_ETH_QUEUE_STATE_STARTED)
967                 return 0;
968
969         rc = roc_nix_rq_ena_dis(rq, true);
970         if (rc) {
971                 plt_err("Failed to enable rxq=%u, rc=%d", qid, rc);
972                 goto done;
973         }
974
975         data->rx_queue_state[qid] = RTE_ETH_QUEUE_STATE_STARTED;
976 done:
977         return rc;
978 }
979
980 static int
981 cnxk_nix_rx_queue_stop(struct rte_eth_dev *eth_dev, uint16_t qid)
982 {
983         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
984         struct rte_eth_dev_data *data = eth_dev->data;
985         struct roc_nix_rq *rq = &dev->rqs[qid];
986         int rc;
987
988         if (data->rx_queue_state[qid] == RTE_ETH_QUEUE_STATE_STOPPED)
989                 return 0;
990
991         rc = roc_nix_rq_ena_dis(rq, false);
992         if (rc) {
993                 plt_err("Failed to disable rxq=%u, rc=%d", qid, rc);
994                 goto done;
995         }
996
997         data->rx_queue_state[qid] = RTE_ETH_QUEUE_STATE_STOPPED;
998 done:
999         return rc;
1000 }
1001
1002 static int
1003 cnxk_nix_dev_stop(struct rte_eth_dev *eth_dev)
1004 {
1005         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
1006         const struct eth_dev_ops *dev_ops = eth_dev->dev_ops;
1007         struct rte_mbuf *rx_pkts[32];
1008         int count, i, j, rc;
1009         void *rxq;
1010
1011         /* Disable switch hdr pkind */
1012         roc_nix_switch_hdr_set(&dev->nix, 0);
1013
1014         /* Stop link change events */
1015         if (!roc_nix_is_vf_or_sdp(&dev->nix))
1016                 roc_nix_mac_link_event_start_stop(&dev->nix, false);
1017
1018         /* Disable Rx via NPC */
1019         roc_nix_npc_rx_ena_dis(&dev->nix, false);
1020
1021         /* Stop rx queues and free up pkts pending */
1022         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1023                 rc = dev_ops->rx_queue_stop(eth_dev, i);
1024                 if (rc)
1025                         continue;
1026
1027                 rxq = eth_dev->data->rx_queues[i];
1028                 count = dev->rx_pkt_burst_no_offload(rxq, rx_pkts, 32);
1029                 while (count) {
1030                         for (j = 0; j < count; j++)
1031                                 rte_pktmbuf_free(rx_pkts[j]);
1032                         count = dev->rx_pkt_burst_no_offload(rxq, rx_pkts, 32);
1033                 }
1034         }
1035
1036         /* Stop tx queues  */
1037         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
1038                 dev_ops->tx_queue_stop(eth_dev, i);
1039
1040         return 0;
1041 }
1042
1043 int
1044 cnxk_nix_dev_start(struct rte_eth_dev *eth_dev)
1045 {
1046         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
1047         int rc, i;
1048
1049         if (eth_dev->data->nb_rx_queues != 0) {
1050                 rc = nix_recalc_mtu(eth_dev);
1051                 if (rc)
1052                         return rc;
1053         }
1054
1055         /* Start rx queues */
1056         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1057                 rc = cnxk_nix_rx_queue_start(eth_dev, i);
1058                 if (rc)
1059                         return rc;
1060         }
1061
1062         /* Start tx queues  */
1063         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
1064                 rc = cnxk_nix_tx_queue_start(eth_dev, i);
1065                 if (rc)
1066                         return rc;
1067         }
1068
1069         /* Enable Rx in NPC */
1070         rc = roc_nix_npc_rx_ena_dis(&dev->nix, true);
1071         if (rc) {
1072                 plt_err("Failed to enable NPC rx %d", rc);
1073                 return rc;
1074         }
1075
1076         cnxk_nix_toggle_flag_link_cfg(dev, true);
1077
1078         /* Start link change events */
1079         if (!roc_nix_is_vf_or_sdp(&dev->nix)) {
1080                 rc = roc_nix_mac_link_event_start_stop(&dev->nix, true);
1081                 if (rc) {
1082                         plt_err("Failed to start cgx link event %d", rc);
1083                         goto rx_disable;
1084                 }
1085         }
1086
1087         cnxk_nix_toggle_flag_link_cfg(dev, false);
1088
1089         return 0;
1090
1091 rx_disable:
1092         roc_nix_npc_rx_ena_dis(&dev->nix, false);
1093         cnxk_nix_toggle_flag_link_cfg(dev, false);
1094         return rc;
1095 }
1096
1097 /* CNXK platform independent eth dev ops */
1098 struct eth_dev_ops cnxk_eth_dev_ops = {
1099         .mtu_set = cnxk_nix_mtu_set,
1100         .mac_addr_set = cnxk_nix_mac_addr_set,
1101         .dev_infos_get = cnxk_nix_info_get,
1102         .link_update = cnxk_nix_link_update,
1103         .tx_queue_release = cnxk_nix_tx_queue_release,
1104         .rx_queue_release = cnxk_nix_rx_queue_release,
1105         .dev_stop = cnxk_nix_dev_stop,
1106         .tx_queue_start = cnxk_nix_tx_queue_start,
1107         .rx_queue_start = cnxk_nix_rx_queue_start,
1108         .rx_queue_stop = cnxk_nix_rx_queue_stop,
1109         .dev_supported_ptypes_get = cnxk_nix_supported_ptypes_get,
1110         .promiscuous_enable = cnxk_nix_promisc_enable,
1111         .promiscuous_disable = cnxk_nix_promisc_disable,
1112 };
1113
1114 static int
1115 cnxk_eth_dev_init(struct rte_eth_dev *eth_dev)
1116 {
1117         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
1118         struct roc_nix *nix = &dev->nix;
1119         struct rte_pci_device *pci_dev;
1120         int rc, max_entries;
1121
1122         eth_dev->dev_ops = &cnxk_eth_dev_ops;
1123
1124         /* For secondary processes, the primary has done all the work */
1125         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1126                 return 0;
1127
1128         pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1129         rte_eth_copy_pci_info(eth_dev, pci_dev);
1130
1131         /* Parse devargs string */
1132         rc = cnxk_ethdev_parse_devargs(eth_dev->device->devargs, dev);
1133         if (rc) {
1134                 plt_err("Failed to parse devargs rc=%d", rc);
1135                 goto error;
1136         }
1137
1138         /* Initialize base roc nix */
1139         nix->pci_dev = pci_dev;
1140         rc = roc_nix_dev_init(nix);
1141         if (rc) {
1142                 plt_err("Failed to initialize roc nix rc=%d", rc);
1143                 goto error;
1144         }
1145
1146         /* Register up msg callbacks */
1147         roc_nix_mac_link_cb_register(nix, cnxk_eth_dev_link_status_cb);
1148
1149         dev->eth_dev = eth_dev;
1150         dev->configured = 0;
1151         dev->ptype_disable = 0;
1152
1153         /* For vfs, returned max_entries will be 0. but to keep default mac
1154          * address, one entry must be allocated. so setting up to 1.
1155          */
1156         if (roc_nix_is_vf_or_sdp(nix))
1157                 max_entries = 1;
1158         else
1159                 max_entries = roc_nix_mac_max_entries_get(nix);
1160
1161         if (max_entries <= 0) {
1162                 plt_err("Failed to get max entries for mac addr");
1163                 rc = -ENOTSUP;
1164                 goto dev_fini;
1165         }
1166
1167         eth_dev->data->mac_addrs =
1168                 rte_zmalloc("mac_addr", max_entries * RTE_ETHER_ADDR_LEN, 0);
1169         if (eth_dev->data->mac_addrs == NULL) {
1170                 plt_err("Failed to allocate memory for mac addr");
1171                 rc = -ENOMEM;
1172                 goto dev_fini;
1173         }
1174
1175         dev->max_mac_entries = max_entries;
1176
1177         /* Get mac address */
1178         rc = roc_nix_npc_mac_addr_get(nix, dev->mac_addr);
1179         if (rc) {
1180                 plt_err("Failed to get mac addr, rc=%d", rc);
1181                 goto free_mac_addrs;
1182         }
1183
1184         /* Update the mac address */
1185         memcpy(eth_dev->data->mac_addrs, dev->mac_addr, RTE_ETHER_ADDR_LEN);
1186
1187         if (!roc_nix_is_vf_or_sdp(nix)) {
1188                 /* Sync same MAC address to CGX/RPM table */
1189                 rc = roc_nix_mac_addr_set(nix, dev->mac_addr);
1190                 if (rc) {
1191                         plt_err("Failed to set mac addr, rc=%d", rc);
1192                         goto free_mac_addrs;
1193                 }
1194         }
1195
1196         /* Union of all capabilities supported by CNXK.
1197          * Platform specific capabilities will be
1198          * updated later.
1199          */
1200         dev->rx_offload_capa = nix_get_rx_offload_capa(dev);
1201         dev->tx_offload_capa = nix_get_tx_offload_capa(dev);
1202         dev->speed_capa = nix_get_speed_capa(dev);
1203
1204         /* Initialize roc npc */
1205         plt_nix_dbg("Port=%d pf=%d vf=%d ver=%s hwcap=0x%" PRIx64
1206                     " rxoffload_capa=0x%" PRIx64 " txoffload_capa=0x%" PRIx64,
1207                     eth_dev->data->port_id, roc_nix_get_pf(nix),
1208                     roc_nix_get_vf(nix), CNXK_ETH_DEV_PMD_VERSION, dev->hwcap,
1209                     dev->rx_offload_capa, dev->tx_offload_capa);
1210         return 0;
1211
1212 free_mac_addrs:
1213         rte_free(eth_dev->data->mac_addrs);
1214 dev_fini:
1215         roc_nix_dev_fini(nix);
1216 error:
1217         plt_err("Failed to init nix eth_dev rc=%d", rc);
1218         return rc;
1219 }
1220
1221 static int
1222 cnxk_eth_dev_uninit(struct rte_eth_dev *eth_dev, bool mbox_close)
1223 {
1224         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
1225         const struct eth_dev_ops *dev_ops = eth_dev->dev_ops;
1226         struct roc_nix *nix = &dev->nix;
1227         int rc, i;
1228
1229         /* Nothing to be done for secondary processes */
1230         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1231                 return 0;
1232
1233         /* Clear the flag since we are closing down */
1234         dev->configured = 0;
1235
1236         roc_nix_npc_rx_ena_dis(nix, false);
1237
1238         /* Disable link status events */
1239         roc_nix_mac_link_event_start_stop(nix, false);
1240
1241         /* Free up SQs */
1242         for (i = 0; i < eth_dev->data->nb_tx_queues; i++) {
1243                 dev_ops->tx_queue_release(eth_dev->data->tx_queues[i]);
1244                 eth_dev->data->tx_queues[i] = NULL;
1245         }
1246         eth_dev->data->nb_tx_queues = 0;
1247
1248         /* Free up RQ's and CQ's */
1249         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1250                 dev_ops->rx_queue_release(eth_dev->data->rx_queues[i]);
1251                 eth_dev->data->rx_queues[i] = NULL;
1252         }
1253         eth_dev->data->nb_rx_queues = 0;
1254
1255         /* Free tm resources */
1256         roc_nix_tm_fini(nix);
1257
1258         /* Unregister queue irqs */
1259         roc_nix_unregister_queue_irqs(nix);
1260
1261         /* Unregister cq irqs */
1262         if (eth_dev->data->dev_conf.intr_conf.rxq)
1263                 roc_nix_unregister_cq_irqs(nix);
1264
1265         /* Free ROC RQ's, SQ's and CQ's memory */
1266         nix_free_queue_mem(dev);
1267
1268         /* Free nix lf resources */
1269         rc = roc_nix_lf_free(nix);
1270         if (rc)
1271                 plt_err("Failed to free nix lf, rc=%d", rc);
1272
1273         rte_free(eth_dev->data->mac_addrs);
1274         eth_dev->data->mac_addrs = NULL;
1275
1276         /* Check if mbox close is needed */
1277         if (!mbox_close)
1278                 return 0;
1279
1280         rc = roc_nix_dev_fini(nix);
1281         /* Can be freed later by PMD if NPA LF is in use */
1282         if (rc == -EAGAIN) {
1283                 eth_dev->data->dev_private = NULL;
1284                 return 0;
1285         } else if (rc) {
1286                 plt_err("Failed in nix dev fini, rc=%d", rc);
1287         }
1288
1289         return rc;
1290 }
1291
1292 int
1293 cnxk_nix_remove(struct rte_pci_device *pci_dev)
1294 {
1295         struct rte_eth_dev *eth_dev;
1296         struct roc_nix *nix;
1297         int rc = -EINVAL;
1298
1299         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
1300         if (eth_dev) {
1301                 /* Cleanup eth dev */
1302                 rc = cnxk_eth_dev_uninit(eth_dev, true);
1303                 if (rc)
1304                         return rc;
1305
1306                 rte_eth_dev_release_port(eth_dev);
1307         }
1308
1309         /* Nothing to be done for secondary processes */
1310         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1311                 return 0;
1312
1313         /* Check if this device is hosting common resource */
1314         nix = roc_idev_npa_nix_get();
1315         if (nix->pci_dev != pci_dev)
1316                 return 0;
1317
1318         /* Try nix fini now */
1319         rc = roc_nix_dev_fini(nix);
1320         if (rc == -EAGAIN) {
1321                 plt_info("%s: common resource in use by other devices",
1322                          pci_dev->name);
1323                 goto exit;
1324         } else if (rc) {
1325                 plt_err("Failed in nix dev fini, rc=%d", rc);
1326                 goto exit;
1327         }
1328
1329         /* Free device pointer as rte_ethdev does not have it anymore */
1330         rte_free(nix);
1331 exit:
1332         return rc;
1333 }
1334
1335 int
1336 cnxk_nix_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
1337 {
1338         int rc;
1339
1340         RTE_SET_USED(pci_drv);
1341
1342         rc = rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct cnxk_eth_dev),
1343                                            cnxk_eth_dev_init);
1344
1345         /* On error on secondary, recheck if port exists in primary or
1346          * in mid of detach state.
1347          */
1348         if (rte_eal_process_type() != RTE_PROC_PRIMARY && rc)
1349                 if (!rte_eth_dev_allocated(pci_dev->device.name))
1350                         return 0;
1351         return rc;
1352 }