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 /* Verbs header. */ 14 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */ 15 #include "mlx5_autoconf.h" 16 #ifdef PEDANTIC 17 #pragma GCC diagnostic ignored "-Wpedantic" 18 #endif 19 #ifdef HAVE_INFINIBAND_VERBS_H 20 #include <infiniband/verbs.h> 21 #endif 22 #ifdef HAVE_INFINIBAND_MLX5DV_H 23 #include <infiniband/mlx5dv.h> 24 #endif 25 #ifdef PEDANTIC 26 #pragma GCC diagnostic error "-Wpedantic" 27 #endif 28 29 #include <mlx5_glue.h> 30 #include <mlx5_common.h> 31 #include <mlx5_common_mr.h> 32 33 /** 34 * Register mr. Given protection domain pointer, pointer to addr and length 35 * register the memory region. 36 * 37 * @param[in] pd 38 * Pointer to protection domain context. 39 * @param[in] addr 40 * Pointer to memory start address. 41 * @param[in] length 42 * Length of the memory to register. 43 * @param[out] pmd_mr 44 * pmd_mr struct set with lkey, address, length and pointer to mr object 45 * 46 * @return 47 * 0 on successful registration, -1 otherwise 48 */ 49 int 50 mlx5_common_verbs_reg_mr(void *pd, void *addr, size_t length, 51 struct mlx5_pmd_mr *pmd_mr) 52 { 53 struct ibv_mr *ibv_mr; 54 55 memset(pmd_mr, 0, sizeof(*pmd_mr)); 56 ibv_mr = mlx5_glue->reg_mr(pd, addr, length, 57 IBV_ACCESS_LOCAL_WRITE | 58 (haswell_broadwell_cpu ? 0 : 59 IBV_ACCESS_RELAXED_ORDERING)); 60 if (!ibv_mr) 61 return -1; 62 63 *pmd_mr = (struct mlx5_pmd_mr){ 64 .lkey = ibv_mr->lkey, 65 .addr = ibv_mr->addr, 66 .len = ibv_mr->length, 67 .obj = (void *)ibv_mr, 68 }; 69 return 0; 70 } 71 72 /** 73 * Deregister mr. Given the mlx5 pmd MR - deregister the MR 74 * 75 * @param[in] pmd_mr 76 * pmd_mr struct set with lkey, address, length and pointer to mr object 77 * 78 */ 79 void 80 mlx5_common_verbs_dereg_mr(struct mlx5_pmd_mr *pmd_mr) 81 { 82 if (pmd_mr && pmd_mr->obj != NULL) { 83 claim_zero(mlx5_glue->dereg_mr(pmd_mr->obj)); 84 memset(pmd_mr, 0, sizeof(*pmd_mr)); 85 } 86 } 87 88