1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2020 Mellanox Technologies, Ltd
11 #include <sys/queue.h>
13 #include "mlx5_autoconf.h"
16 #include <rte_malloc.h>
17 #include <rte_ethdev_driver.h>
18 #include <rte_common.h>
20 #include <mlx5_glue.h>
21 #include <mlx5_common.h>
22 #include <mlx5_common_mr.h>
23 #include <mlx5_rxtx.h>
24 #include <mlx5_verbs.h>
25 #include <mlx5_utils.h>
26 #include <mlx5_malloc.h>
29 * Register mr. Given protection domain pointer, pointer to addr and length
30 * register the memory region.
33 * Pointer to protection domain context.
35 * Pointer to memory start address.
37 * Length of the memory to register.
39 * pmd_mr struct set with lkey, address, length and pointer to mr object
42 * 0 on successful registration, -1 otherwise
45 mlx5_reg_mr(void *pd, void *addr, size_t length,
46 struct mlx5_pmd_mr *pmd_mr)
48 return mlx5_common_verbs_reg_mr(pd, addr, length, pmd_mr);
52 * Deregister mr. Given the mlx5 pmd MR - deregister the MR
55 * pmd_mr struct set with lkey, address, length and pointer to mr object
59 mlx5_dereg_mr(struct mlx5_pmd_mr *pmd_mr)
61 mlx5_common_verbs_dereg_mr(pmd_mr);
64 /* verbs operations. */
65 const struct mlx5_verbs_ops mlx5_verbs_ops = {
66 .reg_mr = mlx5_reg_mr,
67 .dereg_mr = mlx5_dereg_mr,
71 * Modify Rx WQ vlan stripping offload
76 * @return 0 on success, non-0 otherwise
79 mlx5_rxq_obj_modify_wq_vlan_strip(struct mlx5_rxq_obj *rxq_obj, int on)
81 uint16_t vlan_offloads =
82 (on ? IBV_WQ_FLAGS_CVLAN_STRIPPING : 0) |
84 struct ibv_wq_attr mod;
85 mod = (struct ibv_wq_attr){
86 .attr_mask = IBV_WQ_ATTR_FLAGS,
87 .flags_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING,
88 .flags = vlan_offloads,
90 return mlx5_glue->modify_wq(rxq_obj->wq, &mod);
94 * Create a CQ Verbs object.
97 * Pointer to Ethernet device.
99 * Pointer to device private data.
101 * Pointer to Rx queue data.
103 * Number of CQEs in CQ.
105 * Pointer to Rx queue object data.
108 * The Verbs object initialized, NULL otherwise and rte_errno is set.
110 static struct ibv_cq *
111 mlx5_ibv_cq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
112 struct mlx5_rxq_data *rxq_data,
113 unsigned int cqe_n, struct mlx5_rxq_obj *rxq_obj)
116 struct ibv_cq_init_attr_ex ibv;
117 struct mlx5dv_cq_init_attr mlx5;
120 cq_attr.ibv = (struct ibv_cq_init_attr_ex){
122 .channel = rxq_obj->ibv_channel,
125 cq_attr.mlx5 = (struct mlx5dv_cq_init_attr){
128 if (priv->config.cqe_comp && !rxq_data->hw_timestamp) {
129 cq_attr.mlx5.comp_mask |=
130 MLX5DV_CQ_INIT_ATTR_MASK_COMPRESSED_CQE;
131 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
132 cq_attr.mlx5.cqe_comp_res_format =
133 mlx5_rxq_mprq_enabled(rxq_data) ?
134 MLX5DV_CQE_RES_FORMAT_CSUM_STRIDX :
135 MLX5DV_CQE_RES_FORMAT_HASH;
137 cq_attr.mlx5.cqe_comp_res_format = MLX5DV_CQE_RES_FORMAT_HASH;
140 * For vectorized Rx, it must not be doubled in order to
141 * make cq_ci and rq_ci aligned.
143 if (mlx5_rxq_check_vec_support(rxq_data) < 0)
144 cq_attr.ibv.cqe *= 2;
145 } else if (priv->config.cqe_comp && rxq_data->hw_timestamp) {
147 "Port %u Rx CQE compression is disabled for HW"
151 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
152 if (priv->config.cqe_pad) {
153 cq_attr.mlx5.comp_mask |= MLX5DV_CQ_INIT_ATTR_MASK_FLAGS;
154 cq_attr.mlx5.flags |= MLX5DV_CQ_INIT_ATTR_FLAGS_CQE_PAD;
157 return mlx5_glue->cq_ex_to_cq(mlx5_glue->dv_create_cq(priv->sh->ctx,
163 * Create a WQ Verbs object.
166 * Pointer to Ethernet device.
168 * Pointer to device private data.
170 * Pointer to Rx queue data.
172 * Queue index in DPDK Rx queue array.
174 * Number of WQEs in WQ.
176 * Pointer to Rx queue object data.
179 * The Verbs object initialized, NULL otherwise and rte_errno is set.
181 static struct ibv_wq *
182 mlx5_ibv_wq_new(struct rte_eth_dev *dev, struct mlx5_priv *priv,
183 struct mlx5_rxq_data *rxq_data, uint16_t idx,
184 unsigned int wqe_n, struct mlx5_rxq_obj *rxq_obj)
187 struct ibv_wq_init_attr ibv;
188 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
189 struct mlx5dv_wq_init_attr mlx5;
193 wq_attr.ibv = (struct ibv_wq_init_attr){
194 .wq_context = NULL, /* Could be useful in the future. */
195 .wq_type = IBV_WQT_RQ,
196 /* Max number of outstanding WRs. */
197 .max_wr = wqe_n >> rxq_data->sges_n,
198 /* Max number of scatter/gather elements in a WR. */
199 .max_sge = 1 << rxq_data->sges_n,
201 .cq = rxq_obj->ibv_cq,
202 .comp_mask = IBV_WQ_FLAGS_CVLAN_STRIPPING | 0,
203 .create_flags = (rxq_data->vlan_strip ?
204 IBV_WQ_FLAGS_CVLAN_STRIPPING : 0),
206 /* By default, FCS (CRC) is stripped by hardware. */
207 if (rxq_data->crc_present) {
208 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_SCATTER_FCS;
209 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
211 if (priv->config.hw_padding) {
212 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
213 wq_attr.ibv.create_flags |= IBV_WQ_FLAG_RX_END_PADDING;
214 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
215 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
216 wq_attr.ibv.create_flags |= IBV_WQ_FLAGS_PCI_WRITE_END_PADDING;
217 wq_attr.ibv.comp_mask |= IBV_WQ_INIT_ATTR_FLAGS;
220 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
221 wq_attr.mlx5 = (struct mlx5dv_wq_init_attr){
224 if (mlx5_rxq_mprq_enabled(rxq_data)) {
225 struct mlx5dv_striding_rq_init_attr *mprq_attr =
226 &wq_attr.mlx5.striding_rq_attrs;
228 wq_attr.mlx5.comp_mask |= MLX5DV_WQ_INIT_ATTR_MASK_STRIDING_RQ;
229 *mprq_attr = (struct mlx5dv_striding_rq_init_attr){
230 .single_stride_log_num_of_bytes = rxq_data->strd_sz_n,
231 .single_wqe_log_num_of_strides = rxq_data->strd_num_n,
232 .two_byte_shift_en = MLX5_MPRQ_TWO_BYTE_SHIFT,
235 rxq_obj->wq = mlx5_glue->dv_create_wq(priv->sh->ctx, &wq_attr.ibv,
238 rxq_obj->wq = mlx5_glue->create_wq(priv->sh->ctx, &wq_attr.ibv);
242 * Make sure number of WRs*SGEs match expectations since a queue
243 * cannot allocate more than "desc" buffers.
245 if (wq_attr.ibv.max_wr != (wqe_n >> rxq_data->sges_n) ||
246 wq_attr.ibv.max_sge != (1u << rxq_data->sges_n)) {
248 "Port %u Rx queue %u requested %u*%u but got"
250 dev->data->port_id, idx,
251 wqe_n >> rxq_data->sges_n,
252 (1 << rxq_data->sges_n),
253 wq_attr.ibv.max_wr, wq_attr.ibv.max_sge);
254 claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
263 * Create the Rx queue Verbs object.
266 * Pointer to Ethernet device.
268 * Queue index in DPDK Rx queue array.
271 * 0 on success, a negative errno value otherwise and rte_errno is set.
274 mlx5_rxq_ibv_obj_new(struct rte_eth_dev *dev, uint16_t idx)
276 struct mlx5_priv *priv = dev->data->dev_private;
277 struct mlx5_rxq_data *rxq_data = (*priv->rxqs)[idx];
278 struct mlx5_rxq_ctrl *rxq_ctrl =
279 container_of(rxq_data, struct mlx5_rxq_ctrl, rxq);
280 struct ibv_wq_attr mod;
282 unsigned int wqe_n = 1 << rxq_data->elts_n;
283 struct mlx5_rxq_obj *tmpl = rxq_ctrl->obj;
284 struct mlx5dv_cq cq_info;
285 struct mlx5dv_rwq rwq;
287 struct mlx5dv_obj obj;
289 MLX5_ASSERT(rxq_data);
291 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_RX_QUEUE;
292 priv->verbs_alloc_ctx.obj = rxq_ctrl;
293 tmpl->type = MLX5_RXQ_OBJ_TYPE_IBV;
294 tmpl->rxq_ctrl = rxq_ctrl;
297 mlx5_glue->create_comp_channel(priv->sh->ctx);
298 if (!tmpl->ibv_channel) {
299 DRV_LOG(ERR, "Port %u: comp channel creation failure.",
304 tmpl->fd = ((struct ibv_comp_channel *)(tmpl->ibv_channel))->fd;
306 if (mlx5_rxq_mprq_enabled(rxq_data))
307 cqe_n = wqe_n * (1 << rxq_data->strd_num_n) - 1;
310 /* Create CQ using Verbs API. */
311 tmpl->ibv_cq = mlx5_ibv_cq_new(dev, priv, rxq_data, cqe_n, tmpl);
313 DRV_LOG(ERR, "Port %u Rx queue %u CQ creation failure.",
314 dev->data->port_id, idx);
318 obj.cq.in = tmpl->ibv_cq;
319 obj.cq.out = &cq_info;
320 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_CQ);
325 if (cq_info.cqe_size != RTE_CACHE_LINE_SIZE) {
327 "Port %u wrong MLX5_CQE_SIZE environment "
328 "variable value: it should be set to %u.",
329 dev->data->port_id, RTE_CACHE_LINE_SIZE);
333 /* Fill the rings. */
334 rxq_data->cqe_n = log2above(cq_info.cqe_cnt);
335 rxq_data->cq_db = cq_info.dbrec;
336 rxq_data->cqes = (volatile struct mlx5_cqe (*)[])(uintptr_t)cq_info.buf;
337 rxq_data->cq_uar = cq_info.cq_uar;
338 rxq_data->cqn = cq_info.cqn;
339 /* Create WQ (RQ) using Verbs API. */
340 tmpl->wq = mlx5_ibv_wq_new(dev, priv, rxq_data, idx, wqe_n, tmpl);
342 DRV_LOG(ERR, "Port %u Rx queue %u WQ creation failure.",
343 dev->data->port_id, idx);
347 /* Change queue state to ready. */
348 mod = (struct ibv_wq_attr){
349 .attr_mask = IBV_WQ_ATTR_STATE,
350 .wq_state = IBV_WQS_RDY,
352 ret = mlx5_glue->modify_wq(tmpl->wq, &mod);
355 "Port %u Rx queue %u WQ state to IBV_WQS_RDY failed.",
356 dev->data->port_id, idx);
360 obj.rwq.in = tmpl->wq;
362 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_RWQ);
367 rxq_data->wqes = rwq.buf;
368 rxq_data->rq_db = rwq.dbrec;
369 rxq_data->cq_arm_sn = 0;
370 mlx5_rxq_initialize(rxq_data);
372 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
373 dev->data->rx_queue_state[idx] = RTE_ETH_QUEUE_STATE_STARTED;
374 rxq_ctrl->wqn = ((struct ibv_wq *)(tmpl->wq))->wq_num;
377 ret = rte_errno; /* Save rte_errno before cleanup. */
379 claim_zero(mlx5_glue->destroy_wq(tmpl->wq));
381 claim_zero(mlx5_glue->destroy_cq(tmpl->ibv_cq));
382 if (tmpl->ibv_channel)
383 claim_zero(mlx5_glue->destroy_comp_channel(tmpl->ibv_channel));
384 rte_errno = ret; /* Restore rte_errno. */
385 priv->verbs_alloc_ctx.type = MLX5_VERBS_ALLOC_TYPE_NONE;
390 * Release an Rx verbs queue object.
393 * Verbs Rx queue object.
396 mlx5_rxq_ibv_obj_release(struct mlx5_rxq_obj *rxq_obj)
398 MLX5_ASSERT(rxq_obj);
399 MLX5_ASSERT(rxq_obj->wq);
400 MLX5_ASSERT(rxq_obj->ibv_cq);
401 claim_zero(mlx5_glue->destroy_wq(rxq_obj->wq));
402 claim_zero(mlx5_glue->destroy_cq(rxq_obj->ibv_cq));
403 if (rxq_obj->ibv_channel)
404 claim_zero(mlx5_glue->destroy_comp_channel
405 (rxq_obj->ibv_channel));
409 * Get event for an Rx verbs queue object.
412 * Verbs Rx queue object.
415 * 0 on success, a negative errno value otherwise and rte_errno is set.
418 mlx5_rx_ibv_get_event(struct mlx5_rxq_obj *rxq_obj)
420 struct ibv_cq *ev_cq;
422 int ret = mlx5_glue->get_cq_event(rxq_obj->ibv_channel,
425 if (ret < 0 || ev_cq != rxq_obj->ibv_cq)
427 mlx5_glue->ack_cq_events(rxq_obj->ibv_cq, 1);
437 struct mlx5_obj_ops ibv_obj_ops = {
438 .rxq_obj_modify_vlan_strip = mlx5_rxq_obj_modify_wq_vlan_strip,
439 .rxq_obj_new = mlx5_rxq_ibv_obj_new,
440 .rxq_event_get = mlx5_rx_ibv_get_event,
441 .rxq_obj_release = mlx5_rxq_ibv_obj_release,