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