1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2020 Mellanox Technologies, Ltd 3 */ 4 5 #include <stddef.h> 6 #include <errno.h> 7 #include <string.h> 8 #include <stdint.h> 9 #include <unistd.h> 10 #include <sys/mman.h> 11 #include <inttypes.h> 12 13 #include "mlx5_autoconf.h" 14 #include <mlx5_glue.h> 15 #include <mlx5_common.h> 16 #include <mlx5_common_mr.h> 17 18 /** 19 * Register mr. Given protection domain pointer, pointer to addr and length 20 * register the memory region. 21 * 22 * @param[in] pd 23 * Pointer to protection domain context. 24 * @param[in] addr 25 * Pointer to memory start address. 26 * @param[in] length 27 * Length of the memory to register. 28 * @param[out] pmd_mr 29 * pmd_mr struct set with lkey, address, length and pointer to mr object 30 * 31 * @return 32 * 0 on successful registration, -1 otherwise 33 */ 34 int 35 mlx5_common_verbs_reg_mr(void *pd, void *addr, size_t length, 36 struct mlx5_pmd_mr *pmd_mr) 37 { 38 struct ibv_mr *ibv_mr; 39 40 ibv_mr = mlx5_glue->reg_mr(pd, addr, length, 41 IBV_ACCESS_LOCAL_WRITE | 42 (haswell_broadwell_cpu ? 0 : 43 IBV_ACCESS_RELAXED_ORDERING)); 44 if (!ibv_mr) 45 return -1; 46 47 *pmd_mr = (struct mlx5_pmd_mr){ 48 .lkey = ibv_mr->lkey, 49 .addr = ibv_mr->addr, 50 .len = ibv_mr->length, 51 .obj = (void *)ibv_mr, 52 }; 53 return 0; 54 } 55 56 /** 57 * Deregister mr. Given the mlx5 pmd MR - deregister the MR 58 * 59 * @param[in] pmd_mr 60 * pmd_mr struct set with lkey, address, length and pointer to mr object 61 * 62 */ 63 void 64 mlx5_common_verbs_dereg_mr(struct mlx5_pmd_mr *pmd_mr) 65 { 66 if (pmd_mr && pmd_mr->obj != NULL) { 67 claim_zero(mlx5_glue->dereg_mr(pmd_mr->obj)); 68 memset(pmd_mr, 0, sizeof(*pmd_mr)); 69 } 70 } 71 72