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