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