net/bnxt: use appropriate type for Rx ring
[dpdk.git] / drivers / net / bnxt / bnxt_reps.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2020 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_vf_rep_dev_ops = {
20         .dev_infos_get = bnxt_vf_rep_dev_info_get_op,
21         .dev_configure = bnxt_vf_rep_dev_configure_op,
22         .dev_start = bnxt_vf_rep_dev_start_op,
23         .rx_queue_setup = bnxt_vf_rep_rx_queue_setup_op,
24         .rx_queue_release = bnxt_vf_rep_rx_queue_release_op,
25         .tx_queue_setup = bnxt_vf_rep_tx_queue_setup_op,
26         .tx_queue_release = bnxt_vf_rep_tx_queue_release_op,
27         .link_update = bnxt_vf_rep_link_update_op,
28         .dev_close = bnxt_vf_rep_dev_close_op,
29         .dev_stop = bnxt_vf_rep_dev_stop_op,
30         .stats_get = bnxt_vf_rep_stats_get_op,
31         .stats_reset = bnxt_vf_rep_stats_reset_op,
32         .filter_ctrl = bnxt_filter_ctrl_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_vf_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 > max rep_queue, use rxq0 */
49         que = queue_id < BNXT_MAX_VF_REP_RINGS ? queue_id : 0;
50         rep_rxq = vfr_bp->rx_queues[que];
51         rep_rxr = rep_rxq->rx_ring;
52         mask = rep_rxr->rx_ring_struct->ring_mask;
53
54         /* Put this mbuf on the RxQ of the Representor */
55         prod_rx_buf = &rep_rxr->rx_buf_ring[rep_rxr->rx_prod++ & mask];
56         if (!*prod_rx_buf) {
57                 *prod_rx_buf = mbuf;
58                 vfr_bp->rx_bytes[que] += mbuf->pkt_len;
59                 vfr_bp->rx_pkts[que]++;
60         } else {
61                 vfr_bp->rx_drop_bytes[que] += mbuf->pkt_len;
62                 vfr_bp->rx_drop_pkts[que]++;
63                 rte_pktmbuf_free(mbuf); /* Representor Rx ring full, drop pkt */
64         }
65
66         return 0;
67 }
68
69 static uint16_t
70 bnxt_vf_rep_rx_burst(void *rx_queue,
71                      struct rte_mbuf **rx_pkts,
72                      uint16_t nb_pkts)
73 {
74         struct bnxt_rx_queue *rxq = rx_queue;
75         struct rte_mbuf **cons_rx_buf;
76         struct bnxt_rx_ring_info *rxr;
77         uint16_t nb_rx_pkts = 0;
78         uint16_t mask, i;
79
80         if (!rxq)
81                 return 0;
82
83         rxr = rxq->rx_ring;
84         mask = rxr->rx_ring_struct->ring_mask;
85         for (i = 0; i < nb_pkts; i++) {
86                 cons_rx_buf = &rxr->rx_buf_ring[rxr->rx_cons & mask];
87                 if (*cons_rx_buf == NULL)
88                         return nb_rx_pkts;
89                 rx_pkts[nb_rx_pkts] = *cons_rx_buf;
90                 rx_pkts[nb_rx_pkts]->port = rxq->port_id;
91                 *cons_rx_buf = NULL;
92                 nb_rx_pkts++;
93                 rxr->rx_cons++;
94         }
95
96         return nb_rx_pkts;
97 }
98
99 static uint16_t
100 bnxt_vf_rep_tx_burst(void *tx_queue,
101                      struct rte_mbuf **tx_pkts,
102                      __rte_unused uint16_t nb_pkts)
103 {
104         struct bnxt_vf_rep_tx_queue *vfr_txq = tx_queue;
105         struct bnxt_tx_queue *ptxq;
106         struct bnxt *parent;
107         struct  bnxt_vf_representor *vf_rep_bp;
108         int qid;
109         int rc;
110         int i;
111
112         if (!vfr_txq)
113                 return 0;
114
115         qid = vfr_txq->txq->queue_id;
116         vf_rep_bp = vfr_txq->bp;
117         parent = vf_rep_bp->parent_dev->data->dev_private;
118         pthread_mutex_lock(&parent->rep_info->vfr_lock);
119         ptxq = parent->tx_queues[qid];
120
121         ptxq->vfr_tx_cfa_action = vf_rep_bp->vfr_tx_cfa_action;
122
123         for (i = 0; i < nb_pkts; i++) {
124                 vf_rep_bp->tx_bytes[qid] += tx_pkts[i]->pkt_len;
125                 vf_rep_bp->tx_pkts[qid]++;
126         }
127
128         rc = bnxt_xmit_pkts(ptxq, tx_pkts, nb_pkts);
129         ptxq->vfr_tx_cfa_action = 0;
130         pthread_mutex_unlock(&parent->rep_info->vfr_lock);
131
132         return rc;
133 }
134
135 static int
136 bnxt_get_dflt_vnic_svif(struct bnxt *bp, struct bnxt_vf_representor *vf_rep_bp)
137 {
138         struct bnxt_rep_info *rep_info;
139         int rc;
140
141         rc = bnxt_hwrm_get_dflt_vnic_svif(bp, vf_rep_bp->fw_fid,
142                                           &vf_rep_bp->dflt_vnic_id,
143                                           &vf_rep_bp->svif);
144         if (rc) {
145                 PMD_DRV_LOG(ERR, "Failed to get default vnic id of VF\n");
146                 vf_rep_bp->dflt_vnic_id = BNXT_DFLT_VNIC_ID_INVALID;
147                 vf_rep_bp->svif = BNXT_SVIF_INVALID;
148         } else {
149                 PMD_DRV_LOG(INFO, "vf_rep->dflt_vnic_id = %d\n",
150                                 vf_rep_bp->dflt_vnic_id);
151         }
152         if (vf_rep_bp->dflt_vnic_id != BNXT_DFLT_VNIC_ID_INVALID &&
153             vf_rep_bp->svif != BNXT_SVIF_INVALID) {
154                 rep_info = &bp->rep_info[vf_rep_bp->vf_id];
155                 rep_info->conduit_valid = true;
156         }
157
158         return rc;
159 }
160
161 int bnxt_vf_representor_init(struct rte_eth_dev *eth_dev, void *params)
162 {
163         struct bnxt_vf_representor *vf_rep_bp = eth_dev->data->dev_private;
164         struct bnxt_vf_representor *rep_params =
165                                  (struct bnxt_vf_representor *)params;
166         struct rte_eth_link *link;
167         struct bnxt *parent_bp;
168
169         vf_rep_bp->vf_id = rep_params->vf_id;
170         vf_rep_bp->switch_domain_id = rep_params->switch_domain_id;
171         vf_rep_bp->parent_dev = rep_params->parent_dev;
172
173         eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
174         eth_dev->data->representor_id = rep_params->vf_id;
175
176         rte_eth_random_addr(vf_rep_bp->dflt_mac_addr);
177         memcpy(vf_rep_bp->mac_addr, vf_rep_bp->dflt_mac_addr,
178                sizeof(vf_rep_bp->mac_addr));
179         eth_dev->data->mac_addrs =
180                 (struct rte_ether_addr *)&vf_rep_bp->mac_addr;
181         eth_dev->dev_ops = &bnxt_vf_rep_dev_ops;
182
183         /* No data-path, but need stub Rx/Tx functions to avoid crash
184          * when testing with ovs-dpdk
185          */
186         eth_dev->rx_pkt_burst = bnxt_vf_rep_rx_burst;
187         eth_dev->tx_pkt_burst = bnxt_vf_rep_tx_burst;
188         /* Link state. Inherited from PF or trusted VF */
189         parent_bp = vf_rep_bp->parent_dev->data->dev_private;
190         link = &parent_bp->eth_dev->data->dev_link;
191
192         eth_dev->data->dev_link.link_speed = link->link_speed;
193         eth_dev->data->dev_link.link_duplex = link->link_duplex;
194         eth_dev->data->dev_link.link_status = link->link_status;
195         eth_dev->data->dev_link.link_autoneg = link->link_autoneg;
196
197         PMD_DRV_LOG(INFO, "calling bnxt_print_link_info\n");
198         bnxt_print_link_info(eth_dev);
199
200         /* Pass the information to the rte_eth_dev_close() that it should also
201          * release the private port resources.
202          */
203         eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
204         PMD_DRV_LOG(INFO,
205                     "Switch domain id %d: Representor Device %d init done\n",
206                     vf_rep_bp->switch_domain_id, vf_rep_bp->vf_id);
207
208         vf_rep_bp->fw_fid = rep_params->vf_id + parent_bp->first_vf_id;
209         PMD_DRV_LOG(INFO, "vf_rep->fw_fid = %d\n", vf_rep_bp->fw_fid);
210
211         return 0;
212 }
213
214 int bnxt_vf_representor_uninit(struct rte_eth_dev *eth_dev)
215 {
216         struct bnxt *parent_bp;
217         struct bnxt_vf_representor *rep =
218                 (struct bnxt_vf_representor *)eth_dev->data->dev_private;
219
220         uint16_t vf_id;
221
222         eth_dev->data->mac_addrs = NULL;
223         eth_dev->dev_ops = NULL;
224
225         parent_bp = rep->parent_dev->data->dev_private;
226         if (!parent_bp)
227                 return 0;
228
229         parent_bp->num_reps--;
230         vf_id = rep->vf_id;
231         if (parent_bp->rep_info)
232                 memset(&parent_bp->rep_info[vf_id], 0,
233                        sizeof(parent_bp->rep_info[vf_id]));
234                 /* mark that this representor has been freed */
235         return 0;
236 }
237
238 int bnxt_vf_rep_link_update_op(struct rte_eth_dev *eth_dev, int wait_to_compl)
239 {
240         struct bnxt *parent_bp;
241         struct bnxt_vf_representor *rep =
242                 (struct bnxt_vf_representor *)eth_dev->data->dev_private;
243         struct rte_eth_link *link;
244         int rc;
245
246         parent_bp = rep->parent_dev->data->dev_private;
247         if (!parent_bp)
248                 return 0;
249
250         rc = bnxt_link_update_op(parent_bp->eth_dev, wait_to_compl);
251
252         /* Link state. Inherited from PF or trusted VF */
253         link = &parent_bp->eth_dev->data->dev_link;
254
255         eth_dev->data->dev_link.link_speed = link->link_speed;
256         eth_dev->data->dev_link.link_duplex = link->link_duplex;
257         eth_dev->data->dev_link.link_status = link->link_status;
258         eth_dev->data->dev_link.link_autoneg = link->link_autoneg;
259         bnxt_print_link_info(eth_dev);
260
261         return rc;
262 }
263
264 static int bnxt_tf_vfr_alloc(struct rte_eth_dev *vfr_ethdev)
265 {
266         int rc;
267         struct bnxt_vf_representor *vfr = vfr_ethdev->data->dev_private;
268         struct rte_eth_dev *parent_dev = vfr->parent_dev;
269         struct bnxt *parent_bp = parent_dev->data->dev_private;
270         uint16_t vfr_port_id = vfr_ethdev->data->port_id;
271         struct ulp_tlv_param param_list[] = {
272                 {
273                         .type = BNXT_ULP_DF_PARAM_TYPE_DEV_PORT_ID,
274                         .length = 2,
275                         .value = {(vfr_port_id >> 8) & 0xff, vfr_port_id & 0xff}
276                 },
277                 {
278                         .type = BNXT_ULP_DF_PARAM_TYPE_LAST,
279                         .length = 0,
280                         .value = {0}
281                 }
282         };
283
284         ulp_port_db_dev_port_intf_update(parent_bp->ulp_ctx, vfr_ethdev);
285
286         rc = ulp_default_flow_create(parent_dev, param_list,
287                                      BNXT_ULP_DF_TPL_VFREP_TO_VF,
288                                      &vfr->rep2vf_flow_id);
289         if (rc) {
290                 BNXT_TF_DBG(DEBUG,
291                             "Default flow rule creation for VFR->VF failed!\n");
292                 goto err;
293         }
294
295         BNXT_TF_DBG(DEBUG, "*** Default flow rule created for VFR->VF! ***\n");
296         BNXT_TF_DBG(DEBUG, "rep2vf_flow_id = %d\n", vfr->rep2vf_flow_id);
297         rc = ulp_default_flow_db_cfa_action_get(parent_bp->ulp_ctx,
298                                                 vfr->rep2vf_flow_id,
299                                                 &vfr->vfr_tx_cfa_action);
300         if (rc) {
301                 BNXT_TF_DBG(DEBUG,
302                             "Failed to get action_ptr for VFR->VF dflt rule\n");
303                 goto rep2vf_free;
304         }
305         BNXT_TF_DBG(DEBUG, "tx_cfa_action = %d\n", vfr->vfr_tx_cfa_action);
306         rc = ulp_default_flow_create(parent_dev, param_list,
307                                      BNXT_ULP_DF_TPL_VF_TO_VFREP,
308                                      &vfr->vf2rep_flow_id);
309         if (rc) {
310                 BNXT_TF_DBG(DEBUG,
311                             "Default flow rule creation for VF->VFR failed!\n");
312                 goto rep2vf_free;
313         }
314
315         BNXT_TF_DBG(DEBUG, "*** Default flow rule created for VF->VFR! ***\n");
316         BNXT_TF_DBG(DEBUG, "vfr2rep_flow_id = %d\n", vfr->vf2rep_flow_id);
317
318         rc = bnxt_hwrm_cfa_vfr_alloc(parent_bp, vfr->vf_id);
319         if (rc)
320                 goto vf2rep_free;
321
322         return 0;
323
324 vf2rep_free:
325         ulp_default_flow_destroy(vfr->parent_dev, vfr->vf2rep_flow_id);
326 rep2vf_free:
327         ulp_default_flow_destroy(vfr->parent_dev, vfr->rep2vf_flow_id);
328 err:
329         return -EIO;
330 }
331
332 static int bnxt_vfr_alloc(struct rte_eth_dev *vfr_ethdev)
333 {
334         int rc = 0;
335         struct bnxt_vf_representor *vfr = vfr_ethdev->data->dev_private;
336         struct bnxt *parent_bp;
337
338         if (!vfr || !vfr->parent_dev) {
339                 PMD_DRV_LOG(ERR,
340                             "No memory allocated for representor\n");
341                 return -ENOMEM;
342         }
343
344         parent_bp = vfr->parent_dev->data->dev_private;
345         if (parent_bp && !parent_bp->ulp_ctx) {
346                 PMD_DRV_LOG(ERR,
347                             "ulp context not allocated for parent\n");
348                 return -EIO;
349         }
350
351         /* Check if representor has been already allocated in FW */
352         if (vfr->vfr_tx_cfa_action)
353                 return 0;
354
355         /*
356          * Alloc VF rep rules in CFA after default VNIC is created.
357          * Otherwise the FW will create the VF-rep rules with
358          * default drop action.
359          */
360         rc = bnxt_tf_vfr_alloc(vfr_ethdev);
361         if (!rc)
362                 PMD_DRV_LOG(DEBUG, "allocated representor %d in FW\n",
363                             vfr->vf_id);
364         else
365                 PMD_DRV_LOG(ERR,
366                             "Failed to alloc representor %d in FW\n",
367                             vfr->vf_id);
368
369         return rc;
370 }
371
372 static void bnxt_vf_rep_free_rx_mbufs(struct bnxt_vf_representor *rep_bp)
373 {
374         struct bnxt_rx_queue *rxq;
375         unsigned int i;
376
377         for (i = 0; i < rep_bp->rx_nr_rings; i++) {
378                 rxq = rep_bp->rx_queues[i];
379                 bnxt_rx_queue_release_mbufs(rxq);
380         }
381 }
382
383 int bnxt_vf_rep_dev_start_op(struct rte_eth_dev *eth_dev)
384 {
385         struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
386         struct bnxt_rep_info *rep_info;
387         struct bnxt *parent_bp;
388         int rc;
389
390         parent_bp = rep_bp->parent_dev->data->dev_private;
391         rep_info = &parent_bp->rep_info[rep_bp->vf_id];
392
393         pthread_mutex_lock(&rep_info->vfr_start_lock);
394         if (rep_info->conduit_valid) {
395                 pthread_mutex_unlock(&rep_info->vfr_start_lock);
396                 return 0;
397         }
398         rc = bnxt_get_dflt_vnic_svif(parent_bp, rep_bp);
399         if (rc || !rep_info->conduit_valid) {
400                 pthread_mutex_unlock(&rep_info->vfr_start_lock);
401                 return rc;
402         }
403         pthread_mutex_unlock(&rep_info->vfr_start_lock);
404
405         rc = bnxt_vfr_alloc(eth_dev);
406         if (rc) {
407                 eth_dev->data->dev_link.link_status = 0;
408                 bnxt_vf_rep_free_rx_mbufs(rep_bp);
409                 return rc;
410         }
411         eth_dev->rx_pkt_burst = &bnxt_vf_rep_rx_burst;
412         eth_dev->tx_pkt_burst = &bnxt_vf_rep_tx_burst;
413         bnxt_vf_rep_link_update_op(eth_dev, 1);
414
415         return 0;
416 }
417
418 static int bnxt_tf_vfr_free(struct bnxt_vf_representor *vfr)
419 {
420         int rc = 0;
421
422         rc = ulp_default_flow_destroy(vfr->parent_dev,
423                                       vfr->rep2vf_flow_id);
424         if (rc)
425                 PMD_DRV_LOG(ERR,
426                             "default flow destroy failed rep2vf flowid: %d\n",
427                             vfr->rep2vf_flow_id);
428         rc = ulp_default_flow_destroy(vfr->parent_dev,
429                                       vfr->vf2rep_flow_id);
430         if (rc)
431                 PMD_DRV_LOG(ERR,
432                             "default flow destroy failed vf2rep flowid: %d\n",
433                             vfr->vf2rep_flow_id);
434         return 0;
435 }
436
437 static int bnxt_vfr_free(struct bnxt_vf_representor *vfr)
438 {
439         int rc = 0;
440         struct bnxt *parent_bp;
441
442         if (!vfr || !vfr->parent_dev) {
443                 PMD_DRV_LOG(ERR,
444                             "No memory allocated for representor\n");
445                 return -ENOMEM;
446         }
447
448         parent_bp = vfr->parent_dev->data->dev_private;
449         if (!parent_bp)
450                 return 0;
451
452         /* Check if representor has been already freed in FW */
453         if (!vfr->vfr_tx_cfa_action)
454                 return 0;
455
456         rc = bnxt_tf_vfr_free(vfr);
457         if (rc) {
458                 PMD_DRV_LOG(ERR,
459                             "Failed to free representor %d in FW\n",
460                             vfr->vf_id);
461                 return rc;
462         }
463
464         PMD_DRV_LOG(DEBUG, "freed representor %d in FW\n",
465                     vfr->vf_id);
466         vfr->vfr_tx_cfa_action = 0;
467
468         rc = bnxt_hwrm_cfa_vfr_free(parent_bp, vfr->vf_id);
469
470         return rc;
471 }
472
473 void bnxt_vf_rep_dev_stop_op(struct rte_eth_dev *eth_dev)
474 {
475         struct bnxt_vf_representor *vfr_bp = eth_dev->data->dev_private;
476
477         /* Avoid crashes as we are about to free queues */
478         eth_dev->rx_pkt_burst = &bnxt_dummy_recv_pkts;
479         eth_dev->tx_pkt_burst = &bnxt_dummy_xmit_pkts;
480
481         bnxt_vfr_free(vfr_bp);
482
483         if (eth_dev->data->dev_started)
484                 eth_dev->data->dev_link.link_status = 0;
485
486         bnxt_vf_rep_free_rx_mbufs(vfr_bp);
487 }
488
489 void bnxt_vf_rep_dev_close_op(struct rte_eth_dev *eth_dev)
490 {
491         bnxt_vf_representor_uninit(eth_dev);
492 }
493
494 int bnxt_vf_rep_dev_info_get_op(struct rte_eth_dev *eth_dev,
495                                 struct rte_eth_dev_info *dev_info)
496 {
497         struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
498         struct bnxt *parent_bp;
499         unsigned int max_rx_rings;
500         int rc = 0;
501
502         /* MAC Specifics */
503         parent_bp = rep_bp->parent_dev->data->dev_private;
504         if (!parent_bp) {
505                 PMD_DRV_LOG(ERR, "Rep parent NULL!\n");
506                 return rc;
507         }
508         PMD_DRV_LOG(DEBUG, "Representor dev_info_get_op\n");
509         dev_info->max_mac_addrs = parent_bp->max_l2_ctx;
510         dev_info->max_hash_mac_addrs = 0;
511
512         max_rx_rings = BNXT_MAX_VF_REP_RINGS;
513         /* For the sake of symmetry, max_rx_queues = max_tx_queues */
514         dev_info->max_rx_queues = max_rx_rings;
515         dev_info->max_tx_queues = max_rx_rings;
516         dev_info->reta_size = bnxt_rss_hash_tbl_size(parent_bp);
517         dev_info->hash_key_size = 40;
518
519         /* MTU specifics */
520         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
521         dev_info->max_mtu = BNXT_MAX_MTU;
522
523         /* Fast path specifics */
524         dev_info->min_rx_bufsize = 1;
525         dev_info->max_rx_pktlen = BNXT_MAX_PKT_LEN;
526
527         dev_info->rx_offload_capa = BNXT_DEV_RX_OFFLOAD_SUPPORT;
528         if (parent_bp->flags & BNXT_FLAG_PTP_SUPPORTED)
529                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TIMESTAMP;
530         dev_info->tx_offload_capa = BNXT_DEV_TX_OFFLOAD_SUPPORT;
531         dev_info->flow_type_rss_offloads = BNXT_ETH_RSS_SUPPORT;
532
533         return 0;
534 }
535
536 int bnxt_vf_rep_dev_configure_op(__rte_unused struct rte_eth_dev *eth_dev)
537 {
538         struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
539
540         PMD_DRV_LOG(DEBUG, "Representor dev_configure_op\n");
541         rep_bp->rx_queues = (void *)eth_dev->data->rx_queues;
542         rep_bp->tx_nr_rings = eth_dev->data->nb_tx_queues;
543         rep_bp->rx_nr_rings = eth_dev->data->nb_rx_queues;
544
545         return 0;
546 }
547
548 int bnxt_vf_rep_rx_queue_setup_op(struct rte_eth_dev *eth_dev,
549                           uint16_t queue_idx,
550                           uint16_t nb_desc,
551                           unsigned int socket_id,
552                           __rte_unused const struct rte_eth_rxconf *rx_conf,
553                           __rte_unused struct rte_mempool *mp)
554 {
555         struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
556         struct bnxt *parent_bp = rep_bp->parent_dev->data->dev_private;
557         struct bnxt_rx_queue *parent_rxq;
558         struct bnxt_rx_queue *rxq;
559         struct rte_mbuf **buf_ring;
560         int rc = 0;
561
562         if (queue_idx >= BNXT_MAX_VF_REP_RINGS) {
563                 PMD_DRV_LOG(ERR,
564                             "Cannot create Rx ring %d. %d rings available\n",
565                             queue_idx, BNXT_MAX_VF_REP_RINGS);
566                 return -EINVAL;
567         }
568
569         if (!nb_desc || nb_desc > MAX_RX_DESC_CNT) {
570                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid\n", nb_desc);
571                 return -EINVAL;
572         }
573
574         if (!parent_bp->rx_queues) {
575                 PMD_DRV_LOG(ERR, "Parent Rx qs not configured yet\n");
576                 return -EINVAL;
577         }
578
579         parent_rxq = parent_bp->rx_queues[queue_idx];
580         if (!parent_rxq) {
581                 PMD_DRV_LOG(ERR, "Parent RxQ has not been configured yet\n");
582                 return -EINVAL;
583         }
584
585         if (nb_desc != parent_rxq->nb_rx_desc) {
586                 PMD_DRV_LOG(ERR, "nb_desc %d do not match parent rxq", nb_desc);
587                 return -EINVAL;
588         }
589
590         if (eth_dev->data->rx_queues) {
591                 rxq = eth_dev->data->rx_queues[queue_idx];
592                 if (rxq)
593                         bnxt_rx_queue_release_op(rxq);
594         }
595
596         rxq = rte_zmalloc_socket("bnxt_vfr_rx_queue",
597                                  sizeof(struct bnxt_rx_queue),
598                                  RTE_CACHE_LINE_SIZE, socket_id);
599         if (!rxq) {
600                 PMD_DRV_LOG(ERR, "bnxt_vfr_rx_queue allocation failed!\n");
601                 return -ENOMEM;
602         }
603
604         rxq->nb_rx_desc = nb_desc;
605
606         rc = bnxt_init_rx_ring_struct(rxq, socket_id);
607         if (rc)
608                 goto out;
609
610         buf_ring = rte_zmalloc_socket("bnxt_rx_vfr_buf_ring",
611                                       sizeof(struct rte_mbuf *) *
612                                       rxq->rx_ring->rx_ring_struct->ring_size,
613                                       RTE_CACHE_LINE_SIZE, socket_id);
614         if (!buf_ring) {
615                 PMD_DRV_LOG(ERR, "bnxt_rx_vfr_buf_ring allocation failed!\n");
616                 rc = -ENOMEM;
617                 goto out;
618         }
619
620         rxq->rx_ring->rx_buf_ring = buf_ring;
621         rxq->queue_id = queue_idx;
622         rxq->port_id = eth_dev->data->port_id;
623         eth_dev->data->rx_queues[queue_idx] = rxq;
624
625         return 0;
626
627 out:
628         if (rxq)
629                 bnxt_rx_queue_release_op(rxq);
630
631         return rc;
632 }
633
634 void bnxt_vf_rep_rx_queue_release_op(void *rx_queue)
635 {
636         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
637
638         if (!rxq)
639                 return;
640
641         bnxt_rx_queue_release_mbufs(rxq);
642
643         bnxt_free_ring(rxq->rx_ring->rx_ring_struct);
644         bnxt_free_ring(rxq->rx_ring->ag_ring_struct);
645         bnxt_free_ring(rxq->cp_ring->cp_ring_struct);
646
647         rte_free(rxq);
648 }
649
650 int bnxt_vf_rep_tx_queue_setup_op(struct rte_eth_dev *eth_dev,
651                           uint16_t queue_idx,
652                           uint16_t nb_desc,
653                           unsigned int socket_id,
654                           __rte_unused const struct rte_eth_txconf *tx_conf)
655 {
656         struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
657         struct bnxt *parent_bp = rep_bp->parent_dev->data->dev_private;
658         struct bnxt_tx_queue *parent_txq, *txq;
659         struct bnxt_vf_rep_tx_queue *vfr_txq;
660
661         if (queue_idx >= BNXT_MAX_VF_REP_RINGS) {
662                 PMD_DRV_LOG(ERR,
663                             "Cannot create Tx rings %d. %d rings available\n",
664                             queue_idx, BNXT_MAX_VF_REP_RINGS);
665                 return -EINVAL;
666         }
667
668         if (!nb_desc || nb_desc > MAX_TX_DESC_CNT) {
669                 PMD_DRV_LOG(ERR, "nb_desc %d is invalid", nb_desc);
670                 return -EINVAL;
671         }
672
673         if (!parent_bp->tx_queues) {
674                 PMD_DRV_LOG(ERR, "Parent Tx qs not configured yet\n");
675                 return -EINVAL;
676         }
677
678         parent_txq = parent_bp->tx_queues[queue_idx];
679         if (!parent_txq) {
680                 PMD_DRV_LOG(ERR, "Parent TxQ has not been configured yet\n");
681                 return -EINVAL;
682         }
683
684         if (nb_desc != parent_txq->nb_tx_desc) {
685                 PMD_DRV_LOG(ERR, "nb_desc %d do not match parent txq", nb_desc);
686                 return -EINVAL;
687         }
688
689         if (eth_dev->data->tx_queues) {
690                 vfr_txq = eth_dev->data->tx_queues[queue_idx];
691                 bnxt_vf_rep_tx_queue_release_op(vfr_txq);
692                 vfr_txq = NULL;
693         }
694
695         vfr_txq = rte_zmalloc_socket("bnxt_vfr_tx_queue",
696                                      sizeof(struct bnxt_vf_rep_tx_queue),
697                                      RTE_CACHE_LINE_SIZE, socket_id);
698         if (!vfr_txq) {
699                 PMD_DRV_LOG(ERR, "bnxt_vfr_tx_queue allocation failed!");
700                 return -ENOMEM;
701         }
702         txq = rte_zmalloc_socket("bnxt_tx_queue",
703                                  sizeof(struct bnxt_tx_queue),
704                                  RTE_CACHE_LINE_SIZE, socket_id);
705         if (!txq) {
706                 PMD_DRV_LOG(ERR, "bnxt_tx_queue allocation failed!");
707                 rte_free(vfr_txq);
708                 return -ENOMEM;
709         }
710
711         txq->nb_tx_desc = nb_desc;
712         txq->queue_id = queue_idx;
713         txq->port_id = eth_dev->data->port_id;
714         vfr_txq->txq = txq;
715         vfr_txq->bp = rep_bp;
716         eth_dev->data->tx_queues[queue_idx] = vfr_txq;
717
718         return 0;
719 }
720
721 void bnxt_vf_rep_tx_queue_release_op(void *tx_queue)
722 {
723         struct bnxt_vf_rep_tx_queue *vfr_txq = tx_queue;
724
725         if (!vfr_txq)
726                 return;
727
728         rte_free(vfr_txq->txq);
729         rte_free(vfr_txq);
730 }
731
732 int bnxt_vf_rep_stats_get_op(struct rte_eth_dev *eth_dev,
733                              struct rte_eth_stats *stats)
734 {
735         struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
736         int i;
737
738         memset(stats, 0, sizeof(*stats));
739         for (i = 0; i < BNXT_MAX_VF_REP_RINGS; i++) {
740                 stats->obytes += rep_bp->tx_bytes[i];
741                 stats->opackets += rep_bp->tx_pkts[i];
742                 stats->ibytes += rep_bp->rx_bytes[i];
743                 stats->ipackets += rep_bp->rx_pkts[i];
744                 stats->imissed += rep_bp->rx_drop_pkts[i];
745
746                 stats->q_ipackets[i] = rep_bp->rx_pkts[i];
747                 stats->q_ibytes[i] = rep_bp->rx_bytes[i];
748                 stats->q_opackets[i] = rep_bp->tx_pkts[i];
749                 stats->q_obytes[i] = rep_bp->tx_bytes[i];
750                 stats->q_errors[i] = rep_bp->rx_drop_pkts[i];
751         }
752
753         return 0;
754 }
755
756 int bnxt_vf_rep_stats_reset_op(struct rte_eth_dev *eth_dev)
757 {
758         struct bnxt_vf_representor *rep_bp = eth_dev->data->dev_private;
759         int i;
760
761         for (i = 0; i < BNXT_MAX_VF_REP_RINGS; i++) {
762                 rep_bp->tx_pkts[i] = 0;
763                 rep_bp->tx_bytes[i] = 0;
764                 rep_bp->rx_pkts[i] = 0;
765                 rep_bp->rx_bytes[i] = 0;
766                 rep_bp->rx_drop_pkts[i] = 0;
767         }
768         return 0;
769 }