1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2016 6WIND S.A.
3 * Copyright 2016 Mellanox Technologies, Ltd
6 #include <rte_eal_memconfig.h>
7 #include <rte_mempool.h>
8 #include <rte_malloc.h>
9 #include <rte_rwlock.h>
11 #include <mlx5_common_mp.h>
12 #include <mlx5_common_mr.h>
16 #include "mlx5_rxtx.h"
20 struct mr_find_contig_memsegs_data {
24 const struct rte_memseg_list *msl;
27 struct mr_update_mp_data {
28 struct rte_eth_dev *dev;
29 struct mlx5_mr_ctrl *mr_ctrl;
34 * Callback for memory event. This can be called from both primary and secondary
45 mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void *addr,
46 size_t len, void *arg __rte_unused)
48 struct mlx5_dev_ctx_shared *sh;
49 struct mlx5_dev_list *dev_list = &mlx5_shared_data->mem_event_cb_list;
51 /* Must be called from the primary process. */
52 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
54 case RTE_MEM_EVENT_FREE:
55 rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
56 /* Iterate all the existing mlx5 devices. */
57 LIST_FOREACH(sh, dev_list, mem_event_cb)
58 mlx5_free_mr_by_addr(&sh->share_cache,
59 sh->ibdev_name, addr, len);
60 rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
62 case RTE_MEM_EVENT_ALLOC:
69 * Bottom-half of LKey search on Rx.
72 * Pointer to Rx queue structure.
77 * Searched LKey on success, UINT32_MAX on no match.
80 mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, uintptr_t addr)
82 struct mlx5_rxq_ctrl *rxq_ctrl =
83 container_of(rxq, struct mlx5_rxq_ctrl, rxq);
84 struct mlx5_mr_ctrl *mr_ctrl = &rxq->mr_ctrl;
85 struct mlx5_priv *priv = rxq_ctrl->priv;
87 return mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id,
88 &priv->sh->share_cache, mr_ctrl, addr,
89 priv->config.mr_ext_memseg_en);
93 * Bottom-half of LKey search on Tx.
96 * Pointer to Tx queue structure.
101 * Searched LKey on success, UINT32_MAX on no match.
104 mlx5_tx_addr2mr_bh(struct mlx5_txq_data *txq, uintptr_t addr)
106 struct mlx5_txq_ctrl *txq_ctrl =
107 container_of(txq, struct mlx5_txq_ctrl, txq);
108 struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
109 struct mlx5_priv *priv = txq_ctrl->priv;
111 return mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id,
112 &priv->sh->share_cache, mr_ctrl, addr,
113 priv->config.mr_ext_memseg_en);
117 * Bottom-half of LKey search on Tx. If it can't be searched in the memseg
118 * list, register the mempool of the mbuf as externally allocated memory.
121 * Pointer to Tx queue structure.
126 * Searched LKey on success, UINT32_MAX on no match.
129 mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, struct rte_mbuf *mb)
131 uintptr_t addr = (uintptr_t)mb->buf_addr;
134 lkey = mlx5_tx_addr2mr_bh(txq, addr);
135 if (lkey == UINT32_MAX && rte_errno == ENXIO) {
136 /* Mempool may have externally allocated memory. */
137 return mlx5_tx_update_ext_mp(txq, addr, mlx5_mb2mp(mb));
143 * Called during rte_mempool_mem_iter() by mlx5_mr_update_ext_mp().
145 * Externally allocated chunk is registered and a MR is created for the chunk.
146 * The MR object is added to the global list. If memseg list of a MR object
147 * (mr->msl) is null, the MR object can be regarded as externally allocated
150 * Once external memory is registered, it should be static. If the memory is
151 * freed and the virtual address range has different physical memory mapped
152 * again, it may cause crash on device due to the wrong translation entry. PMD
153 * can't track the free event of the external memory for now.
156 mlx5_mr_update_ext_mp_cb(struct rte_mempool *mp, void *opaque,
157 struct rte_mempool_memhdr *memhdr,
158 unsigned mem_idx __rte_unused)
160 struct mr_update_mp_data *data = opaque;
161 struct rte_eth_dev *dev = data->dev;
162 struct mlx5_priv *priv = dev->data->dev_private;
163 struct mlx5_dev_ctx_shared *sh = priv->sh;
164 struct mlx5_mr_ctrl *mr_ctrl = data->mr_ctrl;
165 struct mlx5_mr *mr = NULL;
166 uintptr_t addr = (uintptr_t)memhdr->addr;
167 size_t len = memhdr->len;
168 struct mr_cache_entry entry;
171 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
172 /* If already registered, it should return. */
173 rte_rwlock_read_lock(&sh->share_cache.rwlock);
174 lkey = mlx5_mr_lookup_cache(&sh->share_cache, &entry, addr);
175 rte_rwlock_read_unlock(&sh->share_cache.rwlock);
176 if (lkey != UINT32_MAX)
178 DRV_LOG(DEBUG, "port %u register MR for chunk #%d of mempool (%s)",
179 dev->data->port_id, mem_idx, mp->name);
180 mr = mlx5_create_mr_ext(sh->pd, addr, len, mp->socket_id,
181 sh->share_cache.reg_mr_cb);
184 "port %u unable to allocate a new MR of"
186 dev->data->port_id, mp->name);
190 rte_rwlock_write_lock(&sh->share_cache.rwlock);
191 LIST_INSERT_HEAD(&sh->share_cache.mr_list, mr, mr);
192 /* Insert to the global cache table. */
193 mlx5_mr_insert_cache(&sh->share_cache, mr);
194 rte_rwlock_write_unlock(&sh->share_cache.rwlock);
195 /* Insert to the local cache table */
196 mlx5_mr_addr2mr_bh(sh->pd, &priv->mp_id, &sh->share_cache,
197 mr_ctrl, addr, priv->config.mr_ext_memseg_en);
201 * Finds the first ethdev that match the device.
202 * The existence of multiple ethdev per pci device is only with representors.
203 * On such case, it is enough to get only one of the ports as they all share
204 * the same ibv context.
207 * Pointer to the device.
210 * Pointer to the ethdev if found, NULL otherwise.
212 static struct rte_eth_dev *
213 dev_to_eth_dev(struct rte_device *dev)
217 port_id = rte_eth_find_next_of(0, dev);
218 if (port_id == RTE_MAX_ETHPORTS)
220 return &rte_eth_devices[port_id];
224 * Callback to DMA map external memory to a device.
227 * Pointer to the generic device.
229 * Starting virtual address of memory to be mapped.
231 * Starting IOVA address of memory to be mapped.
233 * Length of memory segment being mapped.
236 * 0 on success, negative value on error.
239 mlx5_net_dma_map(struct rte_device *rte_dev, void *addr,
240 uint64_t iova __rte_unused, size_t len)
242 struct rte_eth_dev *dev;
244 struct mlx5_priv *priv;
245 struct mlx5_dev_ctx_shared *sh;
247 dev = dev_to_eth_dev(rte_dev);
249 DRV_LOG(WARNING, "unable to find matching ethdev "
250 "to device %s", rte_dev->name);
254 priv = dev->data->dev_private;
256 mr = mlx5_create_mr_ext(sh->pd, (uintptr_t)addr, len, SOCKET_ID_ANY,
257 sh->share_cache.reg_mr_cb);
260 "port %u unable to dma map", dev->data->port_id);
264 rte_rwlock_write_lock(&sh->share_cache.rwlock);
265 LIST_INSERT_HEAD(&sh->share_cache.mr_list, mr, mr);
266 /* Insert to the global cache table. */
267 mlx5_mr_insert_cache(&sh->share_cache, mr);
268 rte_rwlock_write_unlock(&sh->share_cache.rwlock);
273 * Callback to DMA unmap external memory to a device.
276 * Pointer to the generic device.
278 * Starting virtual address of memory to be unmapped.
280 * Starting IOVA address of memory to be unmapped.
282 * Length of memory segment being unmapped.
285 * 0 on success, negative value on error.
288 mlx5_net_dma_unmap(struct rte_device *rte_dev, void *addr,
289 uint64_t iova __rte_unused, size_t len __rte_unused)
291 struct rte_eth_dev *dev;
292 struct mlx5_priv *priv;
293 struct mlx5_dev_ctx_shared *sh;
295 struct mr_cache_entry entry;
297 dev = dev_to_eth_dev(rte_dev);
299 DRV_LOG(WARNING, "unable to find matching ethdev to device %s",
304 priv = dev->data->dev_private;
306 rte_rwlock_write_lock(&sh->share_cache.rwlock);
307 mr = mlx5_mr_lookup_list(&sh->share_cache, &entry, (uintptr_t)addr);
309 rte_rwlock_write_unlock(&sh->share_cache.rwlock);
310 DRV_LOG(WARNING, "address 0x%" PRIxPTR " wasn't registered to device %s",
311 (uintptr_t)addr, rte_dev->name);
316 DRV_LOG(DEBUG, "port %u remove MR(%p) from list", dev->data->port_id,
318 mlx5_mr_free(mr, sh->share_cache.dereg_mr_cb);
319 mlx5_mr_rebuild_cache(&sh->share_cache);
321 * No explicit wmb is needed after updating dev_gen due to
322 * store-release ordering in unlock that provides the
323 * implicit barrier at the software visible level.
325 ++sh->share_cache.dev_gen;
326 DRV_LOG(DEBUG, "broadcasting local cache flush, gen=%d",
327 sh->share_cache.dev_gen);
328 rte_rwlock_write_unlock(&sh->share_cache.rwlock);
333 * Register MR for entire memory chunks in a Mempool having externally allocated
334 * memory and fill in local cache.
337 * Pointer to Ethernet device.
339 * Pointer to per-queue MR control structure.
341 * Pointer to registering Mempool.
344 * 0 on success, -1 on failure.
347 mlx5_mr_update_ext_mp(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl,
348 struct rte_mempool *mp)
350 struct mr_update_mp_data data = {
356 rte_mempool_mem_iter(mp, mlx5_mr_update_ext_mp_cb, &data);
361 * Register MR entire memory chunks in a Mempool having externally allocated
362 * memory and search LKey of the address to return.
365 * Pointer to Ethernet device.
369 * Pointer to registering Mempool where addr belongs.
372 * LKey for address on success, UINT32_MAX on failure.
375 mlx5_tx_update_ext_mp(struct mlx5_txq_data *txq, uintptr_t addr,
376 struct rte_mempool *mp)
378 struct mlx5_txq_ctrl *txq_ctrl =
379 container_of(txq, struct mlx5_txq_ctrl, txq);
380 struct mlx5_mr_ctrl *mr_ctrl = &txq->mr_ctrl;
381 struct mlx5_priv *priv = txq_ctrl->priv;
383 if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
385 "port %u using address (%p) from unregistered mempool"
386 " having externally allocated memory"
387 " in secondary process, please create mempool"
388 " prior to rte_eth_dev_start()",
389 PORT_ID(priv), (void *)addr);
392 mlx5_mr_update_ext_mp(ETH_DEV(priv), mr_ctrl, mp);
393 return mlx5_tx_addr2mr_bh(txq, addr);
396 /* Called during rte_mempool_mem_iter() by mlx5_mr_update_mp(). */
398 mlx5_mr_update_mp_cb(struct rte_mempool *mp __rte_unused, void *opaque,
399 struct rte_mempool_memhdr *memhdr,
400 unsigned mem_idx __rte_unused)
402 struct mr_update_mp_data *data = opaque;
403 struct rte_eth_dev *dev = data->dev;
404 struct mlx5_priv *priv = dev->data->dev_private;
408 /* Stop iteration if failed in the previous walk. */
411 /* Register address of the chunk and update local caches. */
412 lkey = mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id,
413 &priv->sh->share_cache, data->mr_ctrl,
414 (uintptr_t)memhdr->addr,
415 priv->config.mr_ext_memseg_en);
416 if (lkey == UINT32_MAX)
421 * Register entire memory chunks in a Mempool.
424 * Pointer to Ethernet device.
426 * Pointer to per-queue MR control structure.
428 * Pointer to registering Mempool.
431 * 0 on success, -1 on failure.
434 mlx5_mr_update_mp(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl,
435 struct rte_mempool *mp)
437 struct mr_update_mp_data data = {
442 uint32_t flags = rte_pktmbuf_priv_flags(mp);
444 if (flags & RTE_PKTMBUF_POOL_F_PINNED_EXT_BUF) {
446 * The pinned external buffer should be registered for DMA
447 * operations by application. The mem_list of the pool contains
448 * the list of chunks with mbuf structures w/o built-in data
449 * buffers and DMA actually does not happen there, no need
450 * to create MR for these chunks.
454 DRV_LOG(DEBUG, "Port %u Rx queue registering mp %s "
455 "having %u chunks.", dev->data->port_id,
456 mp->name, mp->nb_mem_chunks);
457 rte_mempool_mem_iter(mp, mlx5_mr_update_mp_cb, &data);
458 if (data.ret < 0 && rte_errno == ENXIO) {
459 /* Mempool may have externally allocated memory. */
460 return mlx5_mr_update_ext_mp(dev, mr_ctrl, mp);