net/bnxt: fix xstats query
[dpdk.git] / drivers / net / bnxt / bnxt_reps.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include "bnxt.h"
7 #include "bnxt_ring.h"
8 #include "bnxt_reps.h"
9 #include "bnxt_rxq.h"
10 #include "bnxt_rxr.h"
11 #include "bnxt_txq.h"
12 #include "bnxt_txr.h"
13 #include "bnxt_hwrm.h"
14 #include "hsi_struct_def_dpdk.h"
15 #include "bnxt_tf_common.h"
16 #include "ulp_port_db.h"
17 #include "ulp_flow_db.h"
18
19 static const struct eth_dev_ops bnxt_rep_dev_ops = {
20         .dev_infos_get = bnxt_rep_dev_info_get_op,
21         .dev_configure = bnxt_rep_dev_configure_op,
22         .dev_start = bnxt_rep_dev_start_op,
23         .rx_queue_setup = bnxt_rep_rx_queue_setup_op,
24         .rx_queue_release = bnxt_rep_rx_queue_release_op,
25         .tx_queue_setup = bnxt_rep_tx_queue_setup_op,
26         .tx_queue_release = bnxt_rep_tx_queue_release_op,
27         .link_update = bnxt_rep_link_update_op,
28         .dev_close = bnxt_rep_dev_close_op,
29         .dev_stop = bnxt_rep_dev_stop_op,
30         .stats_get = bnxt_rep_stats_get_op,
31         .stats_reset = bnxt_rep_stats_reset_op,
32         .flow_ops_get = bnxt_flow_ops_get_op
33 };
34
35 uint16_t
36 bnxt_vfr_recv(uint16_t port_id, uint16_t queue_id, struct rte_mbuf *mbuf)
37 {
38         struct rte_mbuf **prod_rx_buf;
39         struct bnxt_rx_ring_info *rep_rxr;
40         struct bnxt_rx_queue *rep_rxq;
41         struct rte_eth_dev *vfr_eth_dev;
42         struct bnxt_representor *vfr_bp;
43         uint16_t mask;
44         uint8_t que;
45
46         vfr_eth_dev = &rte_eth_devices[port_id];
47         vfr_bp = vfr_eth_dev->data->dev_private;
48         /* If rxq_id happens to be > nr_rings, use ring 0 */
49         que = queue_id < vfr_bp->rx_nr_rings ? queue_id : 0;
50         rep_rxq = vfr_bp->rx_queues[que];
51         /* Ideally should not happen now, paranoid check */
52         if (!rep_rxq)
53                 return 1;
54         rep_rxr = rep_rxq->rx_ring;
55         mask = rep_rxr->rx_ring_struct->ring_mask;
56
57         /* Put this mbuf on the RxQ of the Representor */
58         prod_rx_buf = &rep_rxr->rx_buf_ring[rep_rxr->rx_raw_prod & mask];
59         if (*prod_rx_buf == NULL) {
60                 *prod_rx_buf = mbuf;
61                 vfr_bp->rx_bytes[que] += mbuf->pkt_len;
62                 vfr_bp->rx_pkts[que]++;
63                 rep_rxr->rx_raw_prod++;
64         } else {
65                 /* Representor Rx ring full, drop pkt */
66                 vfr_bp->rx_drop_bytes[que] += mbuf->pkt_len;
67                 vfr_bp->rx_drop_pkts[que]++;
68                 rte_mbuf_raw_free(mbuf);
69         }
70
71         return 0;
72 }
73
74 static uint16_t
75 bnxt_rep_rx_burst(void *rx_queue,
76                      struct rte_mbuf **rx_pkts,
77                      uint16_t nb_pkts)
78 {
79         struct bnxt_rx_queue *rxq = rx_queue;
80         struct rte_mbuf **cons_rx_buf;
81         struct bnxt_rx_ring_info *rxr;
82         uint16_t nb_rx_pkts = 0;
83         uint16_t mask, i;
84
85         if (!rxq)
86                 return 0;
87
88         rxr = rxq->rx_ring;
89         mask = rxr->rx_ring_struct->ring_mask;
90         for (i = 0; i < nb_pkts; i++) {
91                 cons_rx_buf = &rxr->rx_buf_ring[rxr->rx_cons & mask];
92                 if (*cons_rx_buf == NULL)
93                         return nb_rx_pkts;
94                 rx_pkts[nb_rx_pkts] = *cons_rx_buf;
95                 rx_pkts[nb_rx_pkts]->port = rxq->port_id;
96                 *cons_rx_buf = NULL;
97                 nb_rx_pkts++;
98                 rxr->rx_cons++;
99         }
100
101         return nb_rx_pkts;
102 }
103
104 static uint16_t
105 bnxt_rep_tx_burst(void *tx_queue,
106                      struct rte_mbuf **tx_pkts,
107                      uint16_t nb_pkts)
108 {
109         struct bnxt_vf_rep_tx_queue *vfr_txq = tx_queue;
110         struct bnxt_tx_queue *ptxq;
111         struct bnxt *parent;
112         struct  bnxt_representor *vf_rep_bp;
113         int qid;
114         int rc;
115         int i;
116
117         if (!vfr_txq)
118                 return 0;
119
120         qid = vfr_txq->txq->queue_id;
121         vf_rep_bp = vfr_txq->bp;
122         parent = vf_rep_bp->parent_dev->data->dev_private;
123         pthread_mutex_lock(&parent->rep_info->vfr_lock);
124         ptxq = parent->tx_queues[qid];
125
126         ptxq->vfr_tx_cfa_action = vf_rep_bp->vfr_tx_cfa_action;
127
128         for (i = 0; i < nb_pkts; i++) {
129                 vf_rep_bp->tx_bytes[qid] += tx_pkts[i]->pkt_len;
130                 vf_rep_bp->tx_pkts[qid]++;
131         }
132
133         rc = bnxt_xmit_pkts(ptxq, tx_pkts, nb_pkts);
134         ptxq->vfr_tx_cfa_action = 0;
135         pthread_mutex_unlock(&parent->rep_info->vfr_lock);
136
137         return rc;
138 }
139
140 static int
141 bnxt_get_dflt_vnic_svif(struct bnxt *bp, struct bnxt_representor *vf_rep_bp)
142 {
143         struct bnxt_rep_info *rep_info;
144         int rc;
145
146         rc = bnxt_hwrm_get_dflt_vnic_svif(bp, vf_rep_bp->fw_fid,
147                                           &vf_rep_bp->dflt_vnic_id,
148                                           &vf_rep_bp->svif);
149         if (rc) {
150                 PMD_DRV_LOG(ERR, "Failed to get default vnic id of VF\n");
151                 vf_rep_bp->dflt_vnic_id = BNXT_DFLT_VNIC_ID_INVALID;
152                 vf_rep_bp->svif = BNXT_SVIF_INVALID;
153         } else {
154                 PMD_DRV_LOG(INFO, "vf_rep->dflt_vnic_id = %d\n",
155                                 vf_rep_bp->dflt_vnic_id);
156         }
157         if (vf_rep_bp->dflt_vnic_id != BNXT_DFLT_VNIC_ID_INVALID &&
158             vf_rep_bp->svif != BNXT_SVIF_INVALID) {
159                 rep_info = &bp->rep_info[vf_rep_bp->vf_id];
160                 rep_info->conduit_valid = true;
161         }
162
163         return rc;
164 }
165
166 int bnxt_representor_init(struct rte_eth_dev *eth_dev, void *params)
167 {
168         struct bnxt_representor *vf_rep_bp = eth_dev->data->dev_private;
169         struct bnxt_representor *rep_params =
170                                  (struct bnxt_representor *)params;
171         struct rte_eth_link *link;
172         struct bnxt *parent_bp;
173         uint16_t first_vf_id;
174         int rc = 0;
175
176         PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR init\n", eth_dev->data->port_id);
177         vf_rep_bp->vf_id = rep_params->vf_id;
178         vf_rep_bp->switch_domain_id = rep_params->switch_domain_id;
179         vf_rep_bp->parent_dev = rep_params->parent_dev;
180         vf_rep_bp->rep_based_pf = rep_params->rep_based_pf;
181         vf_rep_bp->flags = rep_params->flags;
182         vf_rep_bp->rep_q_r2f = rep_params->rep_q_r2f;
183         vf_rep_bp->rep_q_f2r = rep_params->rep_q_f2r;
184         vf_rep_bp->rep_fc_r2f = rep_params->rep_fc_r2f;
185         vf_rep_bp->rep_fc_f2r = rep_params->rep_fc_f2r;
186
187         eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR |
188                                         RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
189         eth_dev->data->representor_id = rep_params->vf_id;
190         eth_dev->data->backer_port_id = rep_params->parent_dev->data->port_id;
191
192         rte_eth_random_addr(vf_rep_bp->dflt_mac_addr);
193         memcpy(vf_rep_bp->mac_addr, vf_rep_bp->dflt_mac_addr,
194                sizeof(vf_rep_bp->mac_addr));
195         eth_dev->data->mac_addrs =
196                 (struct rte_ether_addr *)&vf_rep_bp->mac_addr;
197         eth_dev->dev_ops = &bnxt_rep_dev_ops;
198
199         /* No data-path, but need stub Rx/Tx functions to avoid crash
200          * when testing with ovs-dpdk
201          */
202         eth_dev->rx_pkt_burst = bnxt_rep_rx_burst;
203         eth_dev->tx_pkt_burst = bnxt_rep_tx_burst;
204         /* Link state. Inherited from PF or trusted VF */
205         parent_bp = vf_rep_bp->parent_dev->data->dev_private;
206         link = &parent_bp->eth_dev->data->dev_link;
207
208         eth_dev->data->dev_link.link_speed = link->link_speed;
209         eth_dev->data->dev_link.link_duplex = link->link_duplex;
210         eth_dev->data->dev_link.link_status = link->link_status;
211         eth_dev->data->dev_link.link_autoneg = link->link_autoneg;
212
213         bnxt_print_link_info(eth_dev);
214
215         PMD_DRV_LOG(INFO,
216                     "Switch domain id %d: Representor Device %d init done\n",
217                     vf_rep_bp->switch_domain_id, vf_rep_bp->vf_id);
218
219         if (BNXT_REP_BASED_PF(vf_rep_bp)) {
220                 vf_rep_bp->fw_fid = vf_rep_bp->rep_based_pf + 1;
221                 vf_rep_bp->parent_pf_idx = vf_rep_bp->rep_based_pf;
222                 if (!(BNXT_REP_PF(vf_rep_bp))) {
223                         /* VF representor for the remote PF,get first_vf_id */
224                         rc = bnxt_hwrm_first_vf_id_query(parent_bp,
225                                                          vf_rep_bp->fw_fid,
226                                                          &first_vf_id);
227                         if (rc)
228                                 return rc;
229                         if (first_vf_id == 0xffff) {
230                                 PMD_DRV_LOG(ERR,
231                                             "Invalid first_vf_id fid:%x\n",
232                                             vf_rep_bp->fw_fid);
233                                 return -EINVAL;
234                         }
235                         PMD_DRV_LOG(INFO, "first_vf_id = %x parent_fid:%x\n",
236                                     first_vf_id, vf_rep_bp->fw_fid);
237                         vf_rep_bp->fw_fid = rep_params->vf_id + first_vf_id;
238                 }
239         }  else {
240                 vf_rep_bp->fw_fid = rep_params->vf_id + parent_bp->first_vf_id;
241                 if (BNXT_VF_IS_TRUSTED(parent_bp))
242                         vf_rep_bp->parent_pf_idx = parent_bp->parent->fid - 1;
243                 else
244                         vf_rep_bp->parent_pf_idx = parent_bp->fw_fid - 1;
245         }
246
247         PMD_DRV_LOG(INFO, "vf_rep->fw_fid = %d\n", vf_rep_bp->fw_fid);
248
249         return 0;
250 }
251
252 int bnxt_representor_uninit(struct rte_eth_dev *eth_dev)
253 {
254         struct bnxt *parent_bp;
255         struct bnxt_representor *rep =
256                 (struct bnxt_representor *)eth_dev->data->dev_private;
257         uint16_t vf_id;
258
259         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
260                 return 0;
261
262         PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR uninit\n", eth_dev->data->port_id);
263         eth_dev->data->mac_addrs = NULL;
264
265         parent_bp = rep->parent_dev->data->dev_private;
266         if (!parent_bp) {
267                 PMD_DRV_LOG(DEBUG, "BNXT Port:%d already freed\n",
268                             eth_dev->data->port_id);
269                 return 0;
270         }
271
272         parent_bp->num_reps--;
273         vf_id = rep->vf_id;
274         if (parent_bp->rep_info)
275                 memset(&parent_bp->rep_info[vf_id], 0,
276                        sizeof(parent_bp->rep_info[vf_id]));
277                 /* mark that this representor has been freed */
278         return 0;
279 }
280
281 int bnxt_rep_link_update_op(struct rte_eth_dev *eth_dev, int wait_to_compl)
282 {
283         struct bnxt *parent_bp;
284         struct bnxt_representor *rep =
285                 (struct bnxt_representor *)eth_dev->data->dev_private;
286         struct rte_eth_link *link;
287         int rc;
288
289         parent_bp = rep->parent_dev->data->dev_private;
290         if (!parent_bp)
291                 return 0;
292
293         rc = bnxt_link_update_op(parent_bp->eth_dev, wait_to_compl);
294
295         /* Link state. Inherited from PF or trusted VF */
296         link = &parent_bp->eth_dev->data->dev_link;
297
298         eth_dev->data->dev_link.link_speed = link->link_speed;
299         eth_dev->data->dev_link.link_duplex = link->link_duplex;
300         eth_dev->data->dev_link.link_status = link->link_status;
301         eth_dev->data->dev_link.link_autoneg = link->link_autoneg;
302         bnxt_print_link_info(eth_dev);
303
304         return rc;
305 }
306
307 static int bnxt_tf_vfr_alloc(struct rte_eth_dev *vfr_ethdev)
308 {
309         int rc;
310         struct bnxt_representor *vfr = vfr_ethdev->data->dev_private;
311         struct rte_eth_dev *parent_dev = vfr->parent_dev;
312         struct bnxt *parent_bp = parent_dev->data->dev_private;
313
314         if (!parent_bp || !parent_bp->ulp_ctx) {
315                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
316                 return 0;
317         }
318         /* update the port id so you can backtrack to ethdev */
319         vfr->dpdk_port_id = vfr_ethdev->data->port_id;
320
321         /* If pair is present, then delete the pair */
322         if (bnxt_hwrm_cfa_pair_exists(parent_bp, vfr))
323                 (void)bnxt_hwrm_cfa_pair_free(parent_bp, vfr);
324
325         /* Update the ULP portdata base with the new VFR interface */
326         rc = ulp_port_db_dev_port_intf_update(parent_bp->ulp_ctx, vfr_ethdev);
327         if (rc) {
328                 BNXT_TF_DBG(ERR, "Failed to update ulp port details vfr:%u\n",
329                             vfr->vf_id);
330                 return rc;
331         }
332
333         /* Create the default rules for the VFR */
334         rc = bnxt_ulp_create_vfr_default_rules(vfr_ethdev);
335         if (rc) {
336                 BNXT_TF_DBG(ERR, "Failed to create VFR default rules vfr:%u\n",
337                             vfr->vf_id);
338                 return rc;
339         }
340         /* update the port id so you can backtrack to ethdev */
341         vfr->dpdk_port_id = vfr_ethdev->data->port_id;
342
343         rc = bnxt_hwrm_cfa_pair_alloc(parent_bp, vfr);
344         if (rc) {
345                 BNXT_TF_DBG(ERR, "Failed in hwrm vfr alloc vfr:%u rc=%d\n",
346                             vfr->vf_id, rc);
347                 (void)bnxt_ulp_delete_vfr_default_rules(vfr);
348         }
349         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR created and initialized\n",
350                     vfr->dpdk_port_id);
351         return rc;
352 }
353
354 static int bnxt_vfr_alloc(struct rte_eth_dev *vfr_ethdev)
355 {
356         int rc = 0;
357         struct bnxt_representor *vfr = vfr_ethdev->data->dev_private;
358         struct bnxt *parent_bp;
359
360         if (!vfr || !vfr->parent_dev) {
361                 PMD_DRV_LOG(ERR,
362                                 "No memory allocated for representor\n");
363                 return -ENOMEM;
364         }
365
366         parent_bp = vfr->parent_dev->data->dev_private;
367         if (parent_bp && !parent_bp->ulp_ctx) {
368                 PMD_DRV_LOG(ERR,
369                             "ulp context not allocated for parent\n");
370                 return -EIO;
371         }
372
373         /* Check if representor has been already allocated in FW */
374         if (vfr->vfr_tx_cfa_action)
375                 return 0;
376
377         /*
378          * Alloc VF rep rules in CFA after default VNIC is created.
379          * Otherwise the FW will create the VF-rep rules with
380          * default drop action.
381          */
382         rc = bnxt_tf_vfr_alloc(vfr_ethdev);
383         if (!rc)
384                 PMD_DRV_LOG(DEBUG, "allocated representor %d in FW\n",
385                             vfr->vf_id);
386         else
387                 PMD_DRV_LOG(ERR,
388                             "Failed to alloc representor %d in FW\n",
389                             vfr->vf_id);
390
391         return rc;
392 }
393
394 static void bnxt_vfr_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
395 {
396         struct rte_mbuf **sw_ring;
397         unsigned int i;
398
399         if (!rxq || !rxq->rx_ring)
400                 return;
401
402         sw_ring = rxq->rx_ring->rx_buf_ring;
403         if (sw_ring) {
404                 for (i = 0; i < rxq->rx_ring->rx_ring_struct->ring_size; i++) {
405                         if (sw_ring[i]) {
406                                 if (sw_ring[i] != &rxq->fake_mbuf)
407                                         rte_pktmbuf_free_seg(sw_ring[i]);
408                                 sw_ring[i] = NULL;
409                         }
410                 }
411         }
412 }
413
414 static void bnxt_rep_free_rx_mbufs(struct bnxt_representor *rep_bp)
415 {
416         struct bnxt_rx_queue *rxq;
417         unsigned int i;
418
419         for (i = 0; i < rep_bp->rx_nr_rings; i++) {
420                 rxq = rep_bp->rx_queues[i];
421                 bnxt_vfr_rx_queue_release_mbufs(rxq);
422         }
423 }
424
425 int bnxt_rep_dev_start_op(struct rte_eth_dev *eth_dev)
426 {
427         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
428         struct bnxt_rep_info *rep_info;
429         struct bnxt *parent_bp;
430         int rc;
431
432         parent_bp = rep_bp->parent_dev->data->dev_private;
433         rep_info = &parent_bp->rep_info[rep_bp->vf_id];
434
435         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR start\n", eth_dev->data->port_id);
436         pthread_mutex_lock(&rep_info->vfr_start_lock);
437         if (!rep_info->conduit_valid) {
438                 rc = bnxt_get_dflt_vnic_svif(parent_bp, rep_bp);
439                 if (rc || !rep_info->conduit_valid) {
440                         pthread_mutex_unlock(&rep_info->vfr_start_lock);
441                         return rc;
442                 }
443         }
444         pthread_mutex_unlock(&rep_info->vfr_start_lock);
445
446         rc = bnxt_vfr_alloc(eth_dev);
447         if (rc) {
448                 eth_dev->data->dev_link.link_status = 0;
449                 bnxt_rep_free_rx_mbufs(rep_bp);
450                 return rc;
451         }
452         eth_dev->rx_pkt_burst = &bnxt_rep_rx_burst;
453         eth_dev->tx_pkt_burst = &bnxt_rep_tx_burst;
454         bnxt_rep_link_update_op(eth_dev, 1);
455
456         return 0;
457 }
458
459 static int bnxt_tf_vfr_free(struct bnxt_representor *vfr)
460 {
461         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR ulp free\n", vfr->dpdk_port_id);
462         return bnxt_ulp_delete_vfr_default_rules(vfr);
463 }
464
465 static int bnxt_vfr_free(struct bnxt_representor *vfr)
466 {
467         int rc = 0;
468         struct bnxt *parent_bp;
469
470         if (!vfr || !vfr->parent_dev) {
471                 PMD_DRV_LOG(ERR,
472                             "No memory allocated for representor\n");
473                 return -ENOMEM;
474         }
475
476         parent_bp = vfr->parent_dev->data->dev_private;
477         if (!parent_bp) {
478                 PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR already freed\n",
479                             vfr->dpdk_port_id);
480                 return 0;
481         }
482
483         /* Check if representor has been already freed in FW */
484         if (!vfr->vfr_tx_cfa_action)
485                 return 0;
486
487         rc = bnxt_tf_vfr_free(vfr);
488         if (rc) {
489                 PMD_DRV_LOG(ERR,
490                             "Failed to free representor %d in FW\n",
491                             vfr->vf_id);
492         }
493
494         PMD_DRV_LOG(DEBUG, "freed representor %d in FW\n",
495                     vfr->vf_id);
496         vfr->vfr_tx_cfa_action = 0;
497
498         rc = bnxt_hwrm_cfa_pair_free(parent_bp, vfr);
499
500         return rc;
501 }
502
503 int bnxt_rep_dev_stop_op(struct rte_eth_dev *eth_dev)
504 {
505         struct bnxt_representor *vfr_bp = eth_dev->data->dev_private;
506
507         /* Avoid crashes as we are about to free queues */
508         bnxt_stop_rxtx(eth_dev);
509
510         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR stop\n", eth_dev->data->port_id);
511
512         bnxt_vfr_free(vfr_bp);
513
514         if (eth_dev->data->dev_started)
515                 eth_dev->data->dev_link.link_status = 0;
516
517         bnxt_rep_free_rx_mbufs(vfr_bp);
518
519         return 0;
520 }
521
522 int bnxt_rep_dev_close_op(struct rte_eth_dev *eth_dev)
523 {
524         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR close\n", eth_dev->data->port_id);
525         bnxt_representor_uninit(eth_dev);
526         return 0;
527 }
528
529 int bnxt_rep_dev_info_get_op(struct rte_eth_dev *eth_dev,
530                                 struct rte_eth_dev_info *dev_info)
531 {
532         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
533         struct bnxt *parent_bp;
534         unsigned int max_rx_rings;
535         int rc = 0;
536
537         /* MAC Specifics */
538         parent_bp = rep_bp->parent_dev->data->dev_private;
539         if (!parent_bp) {
540                 PMD_DRV_LOG(ERR, "Rep parent NULL!\n");
541                 return rc;
542         }
543         PMD_DRV_LOG(DEBUG, "Representor dev_info_get_op\n");
544         dev_info->max_mac_addrs = parent_bp->max_l2_ctx;
545         dev_info->max_hash_mac_addrs = 0;
546
547         max_rx_rings = BNXT_MAX_VF_REP_RINGS;
548         /* For the sake of symmetry, max_rx_queues = max_tx_queues */
549         dev_info->max_rx_queues = max_rx_rings;
550         dev_info->max_tx_queues = max_rx_rings;
551         dev_info->reta_size = bnxt_rss_hash_tbl_size(parent_bp);
552         dev_info->hash_key_size = 40;
553         dev_info->dev_capa &= ~RTE_ETH_DEV_CAPA_FLOW_RULE_KEEP;
554
555         /* MTU specifics */
556         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
557         dev_info->max_mtu = BNXT_MAX_MTU;
558
559         /* Fast path specifics */
560         dev_info->min_rx_bufsize = 1;
561         dev_info->max_rx_pktlen = BNXT_MAX_PKT_LEN;
562
563         dev_info->rx_offload_capa = BNXT_DEV_RX_OFFLOAD_SUPPORT;
564         if (parent_bp->flags & BNXT_FLAG_PTP_SUPPORTED)
565                 dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_TIMESTAMP;
566         dev_info->tx_offload_capa = BNXT_DEV_TX_OFFLOAD_SUPPORT;
567         dev_info->flow_type_rss_offloads = BNXT_ETH_RSS_SUPPORT;
568
569         dev_info->switch_info.name = eth_dev->device->name;
570         dev_info->switch_info.domain_id = rep_bp->switch_domain_id;
571         dev_info->switch_info.port_id =
572                         rep_bp->vf_id & BNXT_SWITCH_PORT_ID_VF_MASK;
573
574         return 0;
575 }
576
577 int bnxt_rep_dev_configure_op(struct rte_eth_dev *eth_dev)
578 {
579         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
580
581         PMD_DRV_LOG(DEBUG, "Representor dev_configure_op\n");
582         rep_bp->rx_queues = (void *)eth_dev->data->rx_queues;
583         rep_bp->tx_nr_rings = eth_dev->data->nb_tx_queues;
584         rep_bp->rx_nr_rings = eth_dev->data->nb_rx_queues;
585
586         return 0;
587 }
588
589 static int bnxt_init_rep_rx_ring(struct bnxt_rx_queue *rxq,
590                                  unsigned int socket_id)
591 {
592         struct bnxt_rx_ring_info *rxr;
593         struct bnxt_ring *ring;
594
595         rxr = rte_zmalloc_socket("bnxt_rep_rx_ring",
596                                  sizeof(struct bnxt_rx_ring_info),
597                                  RTE_CACHE_LINE_SIZE, socket_id);
598         if (rxr == NULL)
599                 return -ENOMEM;
600         rxq->rx_ring = rxr;
601
602         ring = rte_zmalloc_socket("bnxt_rep_rx_ring_struct",
603                                   sizeof(struct bnxt_ring),
604                                   RTE_CACHE_LINE_SIZE, socket_id);
605         if (ring == NULL)
606                 return -ENOMEM;
607         rxr->rx_ring_struct = ring;
608         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc);
609         ring->ring_mask = ring->ring_size - 1;
610
611         return 0;
612 }
613
614 int bnxt_rep_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
615                                uint16_t queue_idx,
616                                uint16_t nb_desc,
617                                unsigned int socket_id,
618                                __rte_unused const struct rte_eth_rxconf *rx_conf,
619                                __rte_unused struct rte_mempool *mp)
620 {
621         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
622         struct bnxt *parent_bp = rep_bp->parent_dev->data->dev_private;
623         struct bnxt_rx_queue *parent_rxq;
624         struct bnxt_rx_queue *rxq;
625         struct rte_mbuf **buf_ring;
626         int rc = 0;
627
628         if (queue_idx >= BNXT_MAX_VF_REP_RINGS) {
629                 PMD_DRV_LOG(ERR,
630                             "Cannot create Rx ring %d. %d rings available\n",
631                             queue_idx, BNXT_MAX_VF_REP_RINGS);
632                 return -EINVAL;
633         }
634
635         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
636                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
637                 return -EINVAL;
638         }
639
640         if (!parent_bp->rx_queues) {
641                 PMD_DRV_LOG(ERR, "Parent Rx qs not configured yet\n");
642                 return -EINVAL;
643         }
644
645         parent_rxq = parent_bp->rx_queues[queue_idx];
646         if (!parent_rxq) {
647                 PMD_DRV_LOG(ERR, "Parent RxQ has not been configured yet\n");
648                 return -EINVAL;
649         }
650
651         if (nb_desc != parent_rxq->nb_rx_desc) {
652                 PMD_DRV_LOG(ERR, "nb_desc %d do not match parent rxq", nb_desc);
653                 return -EINVAL;
654         }
655
656         if (eth_dev->data->rx_queues) {
657                 rxq = eth_dev->data->rx_queues[queue_idx];
658                 if (rxq)
659                         bnxt_rx_queue_release_op(eth_dev, queue_idx);
660         }
661
662         rxq = rte_zmalloc_socket("bnxt_vfr_rx_queue",
663                                  sizeof(struct bnxt_rx_queue),
664                                  RTE_CACHE_LINE_SIZE, socket_id);
665         if (!rxq) {
666                 PMD_DRV_LOG(ERR, "bnxt_vfr_rx_queue allocation failed!\n");
667                 return -ENOMEM;
668         }
669
670         eth_dev->data->rx_queues[queue_idx] = rxq;
671
672         rxq->nb_rx_desc = nb_desc;
673
674         rc = bnxt_init_rep_rx_ring(rxq, socket_id);
675         if (rc)
676                 goto out;
677
678         buf_ring = rte_zmalloc_socket("bnxt_rx_vfr_buf_ring",
679                                       sizeof(struct rte_mbuf *) *
680                                       rxq->rx_ring->rx_ring_struct->ring_size,
681                                       RTE_CACHE_LINE_SIZE, socket_id);
682         if (!buf_ring) {
683                 PMD_DRV_LOG(ERR, "bnxt_rx_vfr_buf_ring allocation failed!\n");
684                 rc = -ENOMEM;
685                 goto out;
686         }
687
688         rxq->rx_ring->rx_buf_ring = buf_ring;
689         rxq->queue_id = queue_idx;
690         rxq->port_id = eth_dev->data->port_id;
691
692         return 0;
693
694 out:
695         if (rxq)
696                 bnxt_rep_rx_queue_release_op(eth_dev, queue_idx);
697
698         return rc;
699 }
700
701 void bnxt_rep_rx_queue_release_op(struct rte_eth_dev *dev, uint16_t queue_idx)
702 {
703         struct bnxt_rx_queue *rxq = dev->data->rx_queues[queue_idx];
704
705         if (!rxq)
706                 return;
707
708         bnxt_rx_queue_release_mbufs(rxq);
709
710         bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
711         rte_free(rxq->rx_ring->rx_ring_struct);
712         rte_free(rxq->rx_ring);
713
714         rte_free(rxq);
715 }
716
717 int bnxt_rep_tx_queue_setup_op(struct rte_eth_dev *eth_dev,
718                                uint16_t queue_idx,
719                                uint16_t nb_desc,
720                                unsigned int socket_id,
721                                __rte_unused const struct rte_eth_txconf *tx_conf)
722 {
723         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
724         struct bnxt *parent_bp = rep_bp->parent_dev->data->dev_private;
725         struct bnxt_tx_queue *parent_txq, *txq;
726         struct bnxt_vf_rep_tx_queue *vfr_txq;
727
728         if (queue_idx >= BNXT_MAX_VF_REP_RINGS) {
729                 PMD_DRV_LOG(ERR,
730                             "Cannot create Tx rings %d. %d rings available\n",
731                             queue_idx, BNXT_MAX_VF_REP_RINGS);
732                 return -EINVAL;
733         }
734
735         if (!nb_desc || nb_desc > MAX_TX_DESC_CNT) {
736                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid", nb_desc);
737                 return -EINVAL;
738         }
739
740         if (!parent_bp->tx_queues) {
741                 PMD_DRV_LOG(ERR, "Parent Tx qs not configured yet\n");
742                 return -EINVAL;
743         }
744
745         parent_txq = parent_bp->tx_queues[queue_idx];
746         if (!parent_txq) {
747                 PMD_DRV_LOG(ERR, "Parent TxQ has not been configured yet\n");
748                 return -EINVAL;
749         }
750
751         if (nb_desc != parent_txq->nb_tx_desc) {
752                 PMD_DRV_LOG(ERR, "nb_desc %d do not match parent txq", nb_desc);
753                 return -EINVAL;
754         }
755
756         if (eth_dev->data->tx_queues) {
757                 vfr_txq = eth_dev->data->tx_queues[queue_idx];
758                 if (vfr_txq != NULL)
759                         bnxt_rep_tx_queue_release_op(eth_dev, queue_idx);
760         }
761
762         vfr_txq = rte_zmalloc_socket("bnxt_vfr_tx_queue",
763                                      sizeof(struct bnxt_vf_rep_tx_queue),
764                                      RTE_CACHE_LINE_SIZE, socket_id);
765         if (!vfr_txq) {
766                 PMD_DRV_LOG(ERR, "bnxt_vfr_tx_queue allocation failed!");
767                 return -ENOMEM;
768         }
769         txq = rte_zmalloc_socket("bnxt_tx_queue",
770                                  sizeof(struct bnxt_tx_queue),
771                                  RTE_CACHE_LINE_SIZE, socket_id);
772         if (!txq) {
773                 PMD_DRV_LOG(ERR, "bnxt_tx_queue allocation failed!");
774                 rte_free(vfr_txq);
775                 return -ENOMEM;
776         }
777
778         txq->nb_tx_desc = nb_desc;
779         txq->queue_id = queue_idx;
780         txq->port_id = eth_dev->data->port_id;
781         vfr_txq->txq = txq;
782         vfr_txq->bp = rep_bp;
783         eth_dev->data->tx_queues[queue_idx] = vfr_txq;
784
785         return 0;
786 }
787
788 void bnxt_rep_tx_queue_release_op(struct rte_eth_dev *dev, uint16_t queue_idx)
789 {
790         struct bnxt_vf_rep_tx_queue *vfr_txq = dev->data->tx_queues[queue_idx];
791
792         if (!vfr_txq)
793                 return;
794
795         rte_free(vfr_txq->txq);
796         rte_free(vfr_txq);
797         dev->data->tx_queues[queue_idx] = NULL;
798 }
799
800 int bnxt_rep_stats_get_op(struct rte_eth_dev *eth_dev,
801                              struct rte_eth_stats *stats)
802 {
803         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
804         int i;
805
806         memset(stats, 0, sizeof(*stats));
807         for (i = 0; i < BNXT_MAX_VF_REP_RINGS; i++) {
808                 stats->obytes += rep_bp->tx_bytes[i];
809                 stats->opackets += rep_bp->tx_pkts[i];
810                 stats->ibytes += rep_bp->rx_bytes[i];
811                 stats->ipackets += rep_bp->rx_pkts[i];
812                 stats->imissed += rep_bp->rx_drop_pkts[i];
813
814                 stats->q_ipackets[i] = rep_bp->rx_pkts[i];
815                 stats->q_ibytes[i] = rep_bp->rx_bytes[i];
816                 stats->q_opackets[i] = rep_bp->tx_pkts[i];
817                 stats->q_obytes[i] = rep_bp->tx_bytes[i];
818                 stats->q_errors[i] = rep_bp->rx_drop_pkts[i];
819         }
820
821         return 0;
822 }
823
824 int bnxt_rep_stats_reset_op(struct rte_eth_dev *eth_dev)
825 {
826         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
827         int i;
828
829         for (i = 0; i < BNXT_MAX_VF_REP_RINGS; i++) {
830                 rep_bp->tx_pkts[i] = 0;
831                 rep_bp->tx_bytes[i] = 0;
832                 rep_bp->rx_pkts[i] = 0;
833                 rep_bp->rx_bytes[i] = 0;
834                 rep_bp->rx_drop_pkts[i] = 0;
835         }
836         return 0;
837 }
838
839 int bnxt_rep_stop_all(struct bnxt *bp)
840 {
841         uint16_t vf_id;
842         struct rte_eth_dev *rep_eth_dev;
843         int ret;
844
845         /* No vfrep ports just exit */
846         if (!bp->rep_info)
847                 return 0;
848
849         for (vf_id = 0; vf_id < BNXT_MAX_VF_REPS(bp); vf_id++) {
850                 rep_eth_dev = bp->rep_info[vf_id].vfr_eth_dev;
851                 if (!rep_eth_dev)
852                         continue;
853                 ret = bnxt_rep_dev_stop_op(rep_eth_dev);
854                 if (ret != 0)
855                         return ret;
856         }
857
858         return 0;
859 }