net/mlx5: allow meta modifications in legacy mode
[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         PMD_DRV_LOG(INFO, "calling bnxt_print_link_info\n");
214         bnxt_print_link_info(eth_dev);
215
216         PMD_DRV_LOG(INFO,
217                     "Switch domain id %d: Representor Device %d init done\n",
218                     vf_rep_bp->switch_domain_id, vf_rep_bp->vf_id);
219
220         if (BNXT_REP_BASED_PF(vf_rep_bp)) {
221                 vf_rep_bp->fw_fid = vf_rep_bp->rep_based_pf + 1;
222                 vf_rep_bp->parent_pf_idx = vf_rep_bp->rep_based_pf;
223                 if (!(BNXT_REP_PF(vf_rep_bp))) {
224                         /* VF representor for the remote PF,get first_vf_id */
225                         rc = bnxt_hwrm_first_vf_id_query(parent_bp,
226                                                          vf_rep_bp->fw_fid,
227                                                          &first_vf_id);
228                         if (rc)
229                                 return rc;
230                         if (first_vf_id == 0xffff) {
231                                 PMD_DRV_LOG(ERR,
232                                             "Invalid first_vf_id fid:%x\n",
233                                             vf_rep_bp->fw_fid);
234                                 return -EINVAL;
235                         }
236                         PMD_DRV_LOG(INFO, "first_vf_id = %x parent_fid:%x\n",
237                                     first_vf_id, vf_rep_bp->fw_fid);
238                         vf_rep_bp->fw_fid = rep_params->vf_id + first_vf_id;
239                 }
240         }  else {
241                 vf_rep_bp->fw_fid = rep_params->vf_id + parent_bp->first_vf_id;
242                 if (BNXT_VF_IS_TRUSTED(parent_bp))
243                         vf_rep_bp->parent_pf_idx = parent_bp->parent->fid - 1;
244                 else
245                         vf_rep_bp->parent_pf_idx = parent_bp->fw_fid - 1;
246         }
247
248         PMD_DRV_LOG(INFO, "vf_rep->fw_fid = %d\n", vf_rep_bp->fw_fid);
249
250         return 0;
251 }
252
253 int bnxt_representor_uninit(struct rte_eth_dev *eth_dev)
254 {
255         struct bnxt *parent_bp;
256         struct bnxt_representor *rep =
257                 (struct bnxt_representor *)eth_dev->data->dev_private;
258         uint16_t vf_id;
259
260         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
261                 return 0;
262
263         PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR uninit\n", eth_dev->data->port_id);
264         eth_dev->data->mac_addrs = NULL;
265
266         parent_bp = rep->parent_dev->data->dev_private;
267         if (!parent_bp) {
268                 PMD_DRV_LOG(DEBUG, "BNXT Port:%d already freed\n",
269                             eth_dev->data->port_id);
270                 return 0;
271         }
272
273         parent_bp->num_reps--;
274         vf_id = rep->vf_id;
275         if (parent_bp->rep_info)
276                 memset(&parent_bp->rep_info[vf_id], 0,
277                        sizeof(parent_bp->rep_info[vf_id]));
278                 /* mark that this representor has been freed */
279         return 0;
280 }
281
282 int bnxt_rep_link_update_op(struct rte_eth_dev *eth_dev, int wait_to_compl)
283 {
284         struct bnxt *parent_bp;
285         struct bnxt_representor *rep =
286                 (struct bnxt_representor *)eth_dev->data->dev_private;
287         struct rte_eth_link *link;
288         int rc;
289
290         parent_bp = rep->parent_dev->data->dev_private;
291         if (!parent_bp)
292                 return 0;
293
294         rc = bnxt_link_update_op(parent_bp->eth_dev, wait_to_compl);
295
296         /* Link state. Inherited from PF or trusted VF */
297         link = &parent_bp->eth_dev->data->dev_link;
298
299         eth_dev->data->dev_link.link_speed = link->link_speed;
300         eth_dev->data->dev_link.link_duplex = link->link_duplex;
301         eth_dev->data->dev_link.link_status = link->link_status;
302         eth_dev->data->dev_link.link_autoneg = link->link_autoneg;
303         bnxt_print_link_info(eth_dev);
304
305         return rc;
306 }
307
308 static int bnxt_tf_vfr_alloc(struct rte_eth_dev *vfr_ethdev)
309 {
310         int rc;
311         struct bnxt_representor *vfr = vfr_ethdev->data->dev_private;
312         struct rte_eth_dev *parent_dev = vfr->parent_dev;
313         struct bnxt *parent_bp = parent_dev->data->dev_private;
314
315         if (!parent_bp || !parent_bp->ulp_ctx) {
316                 BNXT_TF_DBG(ERR, "Invalid arguments\n");
317                 return 0;
318         }
319
320         /* Update the ULP portdata base with the new VFR interface */
321         rc = ulp_port_db_dev_port_intf_update(parent_bp->ulp_ctx, vfr_ethdev);
322         if (rc) {
323                 BNXT_TF_DBG(ERR, "Failed to update ulp port details vfr:%u\n",
324                             vfr->vf_id);
325                 return rc;
326         }
327
328         /* Create the default rules for the VFR */
329         rc = bnxt_ulp_create_vfr_default_rules(vfr_ethdev);
330         if (rc) {
331                 BNXT_TF_DBG(ERR, "Failed to create VFR default rules vfr:%u\n",
332                             vfr->vf_id);
333                 return rc;
334         }
335         /* update the port id so you can backtrack to ethdev */
336         vfr->dpdk_port_id = vfr_ethdev->data->port_id;
337
338         rc = bnxt_hwrm_cfa_pair_alloc(parent_bp, vfr);
339         if (rc) {
340                 BNXT_TF_DBG(ERR, "Failed in hwrm vfr alloc vfr:%u rc=%d\n",
341                             vfr->vf_id, rc);
342                 (void)bnxt_ulp_delete_vfr_default_rules(vfr);
343         }
344         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR created and initialized\n",
345                     vfr->dpdk_port_id);
346         return rc;
347 }
348
349 static int bnxt_vfr_alloc(struct rte_eth_dev *vfr_ethdev)
350 {
351         int rc = 0;
352         struct bnxt_representor *vfr = vfr_ethdev->data->dev_private;
353         struct bnxt *parent_bp;
354
355         if (!vfr || !vfr->parent_dev) {
356                 PMD_DRV_LOG(ERR,
357                                 "No memory allocated for representor\n");
358                 return -ENOMEM;
359         }
360
361         parent_bp = vfr->parent_dev->data->dev_private;
362         if (parent_bp && !parent_bp->ulp_ctx) {
363                 PMD_DRV_LOG(ERR,
364                             "ulp context not allocated for parent\n");
365                 return -EIO;
366         }
367
368         /* Check if representor has been already allocated in FW */
369         if (vfr->vfr_tx_cfa_action)
370                 return 0;
371
372         /*
373          * Alloc VF rep rules in CFA after default VNIC is created.
374          * Otherwise the FW will create the VF-rep rules with
375          * default drop action.
376          */
377         rc = bnxt_tf_vfr_alloc(vfr_ethdev);
378         if (!rc)
379                 PMD_DRV_LOG(DEBUG, "allocated representor %d in FW\n",
380                             vfr->vf_id);
381         else
382                 PMD_DRV_LOG(ERR,
383                             "Failed to alloc representor %d in FW\n",
384                             vfr->vf_id);
385
386         return rc;
387 }
388
389 static void bnxt_vfr_rx_queue_release_mbufs(struct bnxt_rx_queue *rxq)
390 {
391         struct rte_mbuf **sw_ring;
392         unsigned int i;
393
394         if (!rxq || !rxq->rx_ring)
395                 return;
396
397         sw_ring = rxq->rx_ring->rx_buf_ring;
398         if (sw_ring) {
399                 for (i = 0; i < rxq->rx_ring->rx_ring_struct->ring_size; i++) {
400                         if (sw_ring[i]) {
401                                 if (sw_ring[i] != &rxq->fake_mbuf)
402                                         rte_pktmbuf_free_seg(sw_ring[i]);
403                                 sw_ring[i] = NULL;
404                         }
405                 }
406         }
407 }
408
409 static void bnxt_rep_free_rx_mbufs(struct bnxt_representor *rep_bp)
410 {
411         struct bnxt_rx_queue *rxq;
412         unsigned int i;
413
414         for (i = 0; i < rep_bp->rx_nr_rings; i++) {
415                 rxq = rep_bp->rx_queues[i];
416                 bnxt_vfr_rx_queue_release_mbufs(rxq);
417         }
418 }
419
420 int bnxt_rep_dev_start_op(struct rte_eth_dev *eth_dev)
421 {
422         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
423         struct bnxt_rep_info *rep_info;
424         struct bnxt *parent_bp;
425         int rc;
426
427         parent_bp = rep_bp->parent_dev->data->dev_private;
428         rep_info = &parent_bp->rep_info[rep_bp->vf_id];
429
430         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR start\n", eth_dev->data->port_id);
431         pthread_mutex_lock(&rep_info->vfr_start_lock);
432         if (!rep_info->conduit_valid) {
433                 rc = bnxt_get_dflt_vnic_svif(parent_bp, rep_bp);
434                 if (rc || !rep_info->conduit_valid) {
435                         pthread_mutex_unlock(&rep_info->vfr_start_lock);
436                         return rc;
437                 }
438         }
439         pthread_mutex_unlock(&rep_info->vfr_start_lock);
440
441         rc = bnxt_vfr_alloc(eth_dev);
442         if (rc) {
443                 eth_dev->data->dev_link.link_status = 0;
444                 bnxt_rep_free_rx_mbufs(rep_bp);
445                 return rc;
446         }
447         eth_dev->rx_pkt_burst = &bnxt_rep_rx_burst;
448         eth_dev->tx_pkt_burst = &bnxt_rep_tx_burst;
449         bnxt_rep_link_update_op(eth_dev, 1);
450
451         return 0;
452 }
453
454 static int bnxt_tf_vfr_free(struct bnxt_representor *vfr)
455 {
456         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR ulp free\n", vfr->dpdk_port_id);
457         return bnxt_ulp_delete_vfr_default_rules(vfr);
458 }
459
460 static int bnxt_vfr_free(struct bnxt_representor *vfr)
461 {
462         int rc = 0;
463         struct bnxt *parent_bp;
464
465         if (!vfr || !vfr->parent_dev) {
466                 PMD_DRV_LOG(ERR,
467                             "No memory allocated for representor\n");
468                 return -ENOMEM;
469         }
470
471         parent_bp = vfr->parent_dev->data->dev_private;
472         if (!parent_bp) {
473                 PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR already freed\n",
474                             vfr->dpdk_port_id);
475                 return 0;
476         }
477
478         /* Check if representor has been already freed in FW */
479         if (!vfr->vfr_tx_cfa_action)
480                 return 0;
481
482         rc = bnxt_tf_vfr_free(vfr);
483         if (rc) {
484                 PMD_DRV_LOG(ERR,
485                             "Failed to free representor %d in FW\n",
486                             vfr->vf_id);
487         }
488
489         PMD_DRV_LOG(DEBUG, "freed representor %d in FW\n",
490                     vfr->vf_id);
491         vfr->vfr_tx_cfa_action = 0;
492
493         rc = bnxt_hwrm_cfa_pair_free(parent_bp, vfr);
494
495         return rc;
496 }
497
498 int bnxt_rep_dev_stop_op(struct rte_eth_dev *eth_dev)
499 {
500         struct bnxt_representor *vfr_bp = eth_dev->data->dev_private;
501
502         /* Avoid crashes as we are about to free queues */
503         eth_dev->rx_pkt_burst = &bnxt_dummy_recv_pkts;
504         eth_dev->tx_pkt_burst = &bnxt_dummy_xmit_pkts;
505
506         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR stop\n", eth_dev->data->port_id);
507
508         bnxt_vfr_free(vfr_bp);
509
510         if (eth_dev->data->dev_started)
511                 eth_dev->data->dev_link.link_status = 0;
512
513         bnxt_rep_free_rx_mbufs(vfr_bp);
514
515         return 0;
516 }
517
518 int bnxt_rep_dev_close_op(struct rte_eth_dev *eth_dev)
519 {
520         BNXT_TF_DBG(DEBUG, "BNXT Port:%d VFR close\n", eth_dev->data->port_id);
521         bnxt_representor_uninit(eth_dev);
522         return 0;
523 }
524
525 int bnxt_rep_dev_info_get_op(struct rte_eth_dev *eth_dev,
526                                 struct rte_eth_dev_info *dev_info)
527 {
528         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
529         struct bnxt *parent_bp;
530         unsigned int max_rx_rings;
531         int rc = 0;
532
533         /* MAC Specifics */
534         parent_bp = rep_bp->parent_dev->data->dev_private;
535         if (!parent_bp) {
536                 PMD_DRV_LOG(ERR, "Rep parent NULL!\n");
537                 return rc;
538         }
539         PMD_DRV_LOG(DEBUG, "Representor dev_info_get_op\n");
540         dev_info->max_mac_addrs = parent_bp->max_l2_ctx;
541         dev_info->max_hash_mac_addrs = 0;
542
543         max_rx_rings = BNXT_MAX_VF_REP_RINGS;
544         /* For the sake of symmetry, max_rx_queues = max_tx_queues */
545         dev_info->max_rx_queues = max_rx_rings;
546         dev_info->max_tx_queues = max_rx_rings;
547         dev_info->reta_size = bnxt_rss_hash_tbl_size(parent_bp);
548         dev_info->hash_key_size = 40;
549
550         /* MTU specifics */
551         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
552         dev_info->max_mtu = BNXT_MAX_MTU;
553
554         /* Fast path specifics */
555         dev_info->min_rx_bufsize = 1;
556         dev_info->max_rx_pktlen = BNXT_MAX_PKT_LEN;
557
558         dev_info->rx_offload_capa = BNXT_DEV_RX_OFFLOAD_SUPPORT;
559         if (parent_bp->flags & BNXT_FLAG_PTP_SUPPORTED)
560                 dev_info->rx_offload_capa |= RTE_ETH_RX_OFFLOAD_TIMESTAMP;
561         dev_info->tx_offload_capa = BNXT_DEV_TX_OFFLOAD_SUPPORT;
562         dev_info->flow_type_rss_offloads = BNXT_ETH_RSS_SUPPORT;
563
564         dev_info->switch_info.name = eth_dev->device->name;
565         dev_info->switch_info.domain_id = rep_bp->switch_domain_id;
566         dev_info->switch_info.port_id =
567                         rep_bp->vf_id & BNXT_SWITCH_PORT_ID_VF_MASK;
568
569         return 0;
570 }
571
572 int bnxt_rep_dev_configure_op(struct rte_eth_dev *eth_dev)
573 {
574         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
575
576         PMD_DRV_LOG(DEBUG, "Representor dev_configure_op\n");
577         rep_bp->rx_queues = (void *)eth_dev->data->rx_queues;
578         rep_bp->tx_nr_rings = eth_dev->data->nb_tx_queues;
579         rep_bp->rx_nr_rings = eth_dev->data->nb_rx_queues;
580
581         return 0;
582 }
583
584 static int bnxt_init_rep_rx_ring(struct bnxt_rx_queue *rxq,
585                                  unsigned int socket_id)
586 {
587         struct bnxt_rx_ring_info *rxr;
588         struct bnxt_ring *ring;
589
590         rxr = rte_zmalloc_socket("bnxt_rep_rx_ring",
591                                  sizeof(struct bnxt_rx_ring_info),
592                                  RTE_CACHE_LINE_SIZE, socket_id);
593         if (rxr == NULL)
594                 return -ENOMEM;
595         rxq->rx_ring = rxr;
596
597         ring = rte_zmalloc_socket("bnxt_rep_rx_ring_struct",
598                                   sizeof(struct bnxt_ring),
599                                   RTE_CACHE_LINE_SIZE, socket_id);
600         if (ring == NULL)
601                 return -ENOMEM;
602         rxr->rx_ring_struct = ring;
603         ring->ring_size = rte_align32pow2(rxq->nb_rx_desc);
604         ring->ring_mask = ring->ring_size - 1;
605
606         return 0;
607 }
608
609 int bnxt_rep_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
610                                uint16_t queue_idx,
611                                uint16_t nb_desc,
612                                unsigned int socket_id,
613                                __rte_unused const struct rte_eth_rxconf *rx_conf,
614                                __rte_unused struct rte_mempool *mp)
615 {
616         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
617         struct bnxt *parent_bp = rep_bp->parent_dev->data->dev_private;
618         struct bnxt_rx_queue *parent_rxq;
619         struct bnxt_rx_queue *rxq;
620         struct rte_mbuf **buf_ring;
621         int rc = 0;
622
623         if (queue_idx >= BNXT_MAX_VF_REP_RINGS) {
624                 PMD_DRV_LOG(ERR,
625                             "Cannot create Rx ring %d. %d rings available\n",
626                             queue_idx, BNXT_MAX_VF_REP_RINGS);
627                 return -EINVAL;
628         }
629
630         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
631                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
632                 return -EINVAL;
633         }
634
635         if (!parent_bp->rx_queues) {
636                 PMD_DRV_LOG(ERR, "Parent Rx qs not configured yet\n");
637                 return -EINVAL;
638         }
639
640         parent_rxq = parent_bp->rx_queues[queue_idx];
641         if (!parent_rxq) {
642                 PMD_DRV_LOG(ERR, "Parent RxQ has not been configured yet\n");
643                 return -EINVAL;
644         }
645
646         if (nb_desc != parent_rxq->nb_rx_desc) {
647                 PMD_DRV_LOG(ERR, "nb_desc %d do not match parent rxq", nb_desc);
648                 return -EINVAL;
649         }
650
651         if (eth_dev->data->rx_queues) {
652                 rxq = eth_dev->data->rx_queues[queue_idx];
653                 if (rxq)
654                         bnxt_rx_queue_release_op(eth_dev, queue_idx);
655         }
656
657         rxq = rte_zmalloc_socket("bnxt_vfr_rx_queue",
658                                  sizeof(struct bnxt_rx_queue),
659                                  RTE_CACHE_LINE_SIZE, socket_id);
660         if (!rxq) {
661                 PMD_DRV_LOG(ERR, "bnxt_vfr_rx_queue allocation failed!\n");
662                 return -ENOMEM;
663         }
664
665         eth_dev->data->rx_queues[queue_idx] = rxq;
666
667         rxq->nb_rx_desc = nb_desc;
668
669         rc = bnxt_init_rep_rx_ring(rxq, socket_id);
670         if (rc)
671                 goto out;
672
673         buf_ring = rte_zmalloc_socket("bnxt_rx_vfr_buf_ring",
674                                       sizeof(struct rte_mbuf *) *
675                                       rxq->rx_ring->rx_ring_struct->ring_size,
676                                       RTE_CACHE_LINE_SIZE, socket_id);
677         if (!buf_ring) {
678                 PMD_DRV_LOG(ERR, "bnxt_rx_vfr_buf_ring allocation failed!\n");
679                 rc = -ENOMEM;
680                 goto out;
681         }
682
683         rxq->rx_ring->rx_buf_ring = buf_ring;
684         rxq->queue_id = queue_idx;
685         rxq->port_id = eth_dev->data->port_id;
686
687         return 0;
688
689 out:
690         if (rxq)
691                 bnxt_rep_rx_queue_release_op(eth_dev, queue_idx);
692
693         return rc;
694 }
695
696 void bnxt_rep_rx_queue_release_op(struct rte_eth_dev *dev, uint16_t queue_idx)
697 {
698         struct bnxt_rx_queue *rxq = dev->data->rx_queues[queue_idx];
699
700         if (!rxq)
701                 return;
702
703         bnxt_rx_queue_release_mbufs(rxq);
704
705         bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
706         rte_free(rxq->rx_ring->rx_ring_struct);
707         rte_free(rxq->rx_ring);
708
709         rte_free(rxq);
710 }
711
712 int bnxt_rep_tx_queue_setup_op(struct rte_eth_dev *eth_dev,
713                                uint16_t queue_idx,
714                                uint16_t nb_desc,
715                                unsigned int socket_id,
716                                __rte_unused const struct rte_eth_txconf *tx_conf)
717 {
718         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
719         struct bnxt *parent_bp = rep_bp->parent_dev->data->dev_private;
720         struct bnxt_tx_queue *parent_txq, *txq;
721         struct bnxt_vf_rep_tx_queue *vfr_txq;
722
723         if (queue_idx >= BNXT_MAX_VF_REP_RINGS) {
724                 PMD_DRV_LOG(ERR,
725                             "Cannot create Tx rings %d. %d rings available\n",
726                             queue_idx, BNXT_MAX_VF_REP_RINGS);
727                 return -EINVAL;
728         }
729
730         if (!nb_desc || nb_desc > MAX_TX_DESC_CNT) {
731                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid", nb_desc);
732                 return -EINVAL;
733         }
734
735         if (!parent_bp->tx_queues) {
736                 PMD_DRV_LOG(ERR, "Parent Tx qs not configured yet\n");
737                 return -EINVAL;
738         }
739
740         parent_txq = parent_bp->tx_queues[queue_idx];
741         if (!parent_txq) {
742                 PMD_DRV_LOG(ERR, "Parent TxQ has not been configured yet\n");
743                 return -EINVAL;
744         }
745
746         if (nb_desc != parent_txq->nb_tx_desc) {
747                 PMD_DRV_LOG(ERR, "nb_desc %d do not match parent txq", nb_desc);
748                 return -EINVAL;
749         }
750
751         if (eth_dev->data->tx_queues) {
752                 vfr_txq = eth_dev->data->tx_queues[queue_idx];
753                 if (vfr_txq != NULL)
754                         bnxt_rep_tx_queue_release_op(eth_dev, queue_idx);
755         }
756
757         vfr_txq = rte_zmalloc_socket("bnxt_vfr_tx_queue",
758                                      sizeof(struct bnxt_vf_rep_tx_queue),
759                                      RTE_CACHE_LINE_SIZE, socket_id);
760         if (!vfr_txq) {
761                 PMD_DRV_LOG(ERR, "bnxt_vfr_tx_queue allocation failed!");
762                 return -ENOMEM;
763         }
764         txq = rte_zmalloc_socket("bnxt_tx_queue",
765                                  sizeof(struct bnxt_tx_queue),
766                                  RTE_CACHE_LINE_SIZE, socket_id);
767         if (!txq) {
768                 PMD_DRV_LOG(ERR, "bnxt_tx_queue allocation failed!");
769                 rte_free(vfr_txq);
770                 return -ENOMEM;
771         }
772
773         txq->nb_tx_desc = nb_desc;
774         txq->queue_id = queue_idx;
775         txq->port_id = eth_dev->data->port_id;
776         vfr_txq->txq = txq;
777         vfr_txq->bp = rep_bp;
778         eth_dev->data->tx_queues[queue_idx] = vfr_txq;
779
780         return 0;
781 }
782
783 void bnxt_rep_tx_queue_release_op(struct rte_eth_dev *dev, uint16_t queue_idx)
784 {
785         struct bnxt_vf_rep_tx_queue *vfr_txq = dev->data->tx_queues[queue_idx];
786
787         if (!vfr_txq)
788                 return;
789
790         rte_free(vfr_txq->txq);
791         rte_free(vfr_txq);
792         dev->data->tx_queues[queue_idx] = NULL;
793 }
794
795 int bnxt_rep_stats_get_op(struct rte_eth_dev *eth_dev,
796                              struct rte_eth_stats *stats)
797 {
798         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
799         int i;
800
801         memset(stats, 0, sizeof(*stats));
802         for (i = 0; i < BNXT_MAX_VF_REP_RINGS; i++) {
803                 stats->obytes += rep_bp->tx_bytes[i];
804                 stats->opackets += rep_bp->tx_pkts[i];
805                 stats->ibytes += rep_bp->rx_bytes[i];
806                 stats->ipackets += rep_bp->rx_pkts[i];
807                 stats->imissed += rep_bp->rx_drop_pkts[i];
808
809                 stats->q_ipackets[i] = rep_bp->rx_pkts[i];
810                 stats->q_ibytes[i] = rep_bp->rx_bytes[i];
811                 stats->q_opackets[i] = rep_bp->tx_pkts[i];
812                 stats->q_obytes[i] = rep_bp->tx_bytes[i];
813                 stats->q_errors[i] = rep_bp->rx_drop_pkts[i];
814         }
815
816         return 0;
817 }
818
819 int bnxt_rep_stats_reset_op(struct rte_eth_dev *eth_dev)
820 {
821         struct bnxt_representor *rep_bp = eth_dev->data->dev_private;
822         int i;
823
824         for (i = 0; i < BNXT_MAX_VF_REP_RINGS; i++) {
825                 rep_bp->tx_pkts[i] = 0;
826                 rep_bp->tx_bytes[i] = 0;
827                 rep_bp->rx_pkts[i] = 0;
828                 rep_bp->rx_bytes[i] = 0;
829                 rep_bp->rx_drop_pkts[i] = 0;
830         }
831         return 0;
832 }
833
834 int bnxt_rep_stop_all(struct bnxt *bp)
835 {
836         uint16_t vf_id;
837         struct rte_eth_dev *rep_eth_dev;
838         int ret;
839
840         /* No vfrep ports just exit */
841         if (!bp->rep_info)
842                 return 0;
843
844         for (vf_id = 0; vf_id < BNXT_MAX_VF_REPS; vf_id++) {
845                 rep_eth_dev = bp->rep_info[vf_id].vfr_eth_dev;
846                 if (!rep_eth_dev)
847                         continue;
848                 ret = bnxt_rep_dev_stop_op(rep_eth_dev);
849                 if (ret != 0)
850                         return ret;
851         }
852
853         return 0;
854 }