1 /*- 2 * Copyright (c) 2013-2018, Mellanox Technologies, Ltd. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 * $FreeBSD$ 26 */ 27 28 #include <linux/kernel.h> 29 #include <linux/module.h> 30 #include <dev/mlx5/driver.h> 31 #include "mlx5_core.h" 32 33 static int mlx5_relaxed_ordering_write; 34 SYSCTL_INT(_hw_mlx5, OID_AUTO, relaxed_ordering_write, CTLFLAG_RWTUN, 35 &mlx5_relaxed_ordering_write, 0, 36 "Set to enable relaxed ordering for PCIe writes"); 37 38 void mlx5_init_mr_table(struct mlx5_core_dev *dev) 39 { 40 struct mlx5_mr_table *table = &dev->priv.mr_table; 41 42 memset(table, 0, sizeof(*table)); 43 spin_lock_init(&table->lock); 44 INIT_RADIX_TREE(&table->tree, GFP_ATOMIC); 45 } 46 47 void mlx5_cleanup_mr_table(struct mlx5_core_dev *dev) 48 { 49 } 50 51 int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev, 52 struct mlx5_core_mr *mkey, 53 u32 *in, int inlen, 54 u32 *out, int outlen, 55 mlx5_cmd_cbk_t callback, void *context) 56 { 57 struct mlx5_mr_table *table = &dev->priv.mr_table; 58 u32 lout[MLX5_ST_SZ_DW(create_mkey_out)] = {0}; 59 u32 mkey_index; 60 void *mkc; 61 unsigned long flags; 62 int err; 63 u8 key; 64 65 spin_lock_irq(&dev->priv.mkey_lock); 66 key = dev->priv.mkey_key++; 67 spin_unlock_irq(&dev->priv.mkey_lock); 68 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry); 69 MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY); 70 MLX5_SET(mkc, mkc, mkey_7_0, key); 71 72 if (mlx5_relaxed_ordering_write != 0) { 73 if (MLX5_CAP_GEN(dev, relaxed_ordering_write)) 74 MLX5_SET(mkc, mkc, relaxed_ordering_write, 1); 75 else 76 return (-EPROTONOSUPPORT); 77 } 78 79 if (callback) 80 return mlx5_cmd_exec_cb(dev, in, inlen, out, outlen, 81 callback, context); 82 83 err = mlx5_cmd_exec(dev, in, inlen, lout, sizeof(lout)); 84 if (err) { 85 mlx5_core_dbg(dev, "cmd exec failed %d\n", err); 86 return err; 87 } 88 89 mkey_index = MLX5_GET(create_mkey_out, lout, mkey_index); 90 mkey->iova = MLX5_GET64(mkc, mkc, start_addr); 91 mkey->size = MLX5_GET64(mkc, mkc, len); 92 mkey->key = mlx5_idx_to_mkey(mkey_index) | key; 93 mkey->pd = MLX5_GET(mkc, mkc, pd); 94 95 mlx5_core_dbg(dev, "out 0x%x, key 0x%x, mkey 0x%x\n", 96 mkey_index, key, mkey->key); 97 98 /* connect to MR tree */ 99 spin_lock_irqsave(&table->lock, flags); 100 err = radix_tree_insert(&table->tree, mlx5_mkey_to_idx(mkey->key), mkey); 101 spin_unlock_irqrestore(&table->lock, flags); 102 if (err) { 103 mlx5_core_warn(dev, "failed radix tree insert of mr 0x%x, %d\n", 104 mkey->key, err); 105 mlx5_core_destroy_mkey(dev, mkey); 106 } 107 108 return err; 109 } 110 EXPORT_SYMBOL(mlx5_core_create_mkey_cb); 111 112 int mlx5_core_create_mkey(struct mlx5_core_dev *dev, 113 struct mlx5_core_mr *mkey, 114 u32 *in, int inlen) 115 { 116 return mlx5_core_create_mkey_cb(dev, mkey, in, inlen, 117 NULL, 0, NULL, NULL); 118 } 119 EXPORT_SYMBOL(mlx5_core_create_mkey); 120 121 int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mkey) 122 { 123 struct mlx5_mr_table *table = &dev->priv.mr_table; 124 u32 out[MLX5_ST_SZ_DW(destroy_mkey_out)] = {0}; 125 u32 in[MLX5_ST_SZ_DW(destroy_mkey_in)] = {0}; 126 struct mlx5_core_mr *deleted_mr; 127 unsigned long flags; 128 129 spin_lock_irqsave(&table->lock, flags); 130 deleted_mr = radix_tree_delete(&table->tree, mlx5_mkey_to_idx(mkey->key)); 131 spin_unlock_irqrestore(&table->lock, flags); 132 if (!deleted_mr) { 133 mlx5_core_warn(dev, "failed radix tree delete of mr 0x%x\n", mkey->key); 134 return -ENOENT; 135 } 136 137 MLX5_SET(destroy_mkey_in, in, opcode, MLX5_CMD_OP_DESTROY_MKEY); 138 MLX5_SET(destroy_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key)); 139 140 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); 141 } 142 EXPORT_SYMBOL(mlx5_core_destroy_mkey); 143 144 int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mkey, 145 u32 *out, int outlen) 146 { 147 u32 in[MLX5_ST_SZ_DW(query_mkey_in)] = {0}; 148 149 memset(out, 0, outlen); 150 MLX5_SET(query_mkey_in, in, opcode, MLX5_CMD_OP_QUERY_MKEY); 151 MLX5_SET(query_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key)); 152 153 return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen); 154 } 155 EXPORT_SYMBOL(mlx5_core_query_mkey); 156 157 int mlx5_core_dump_fill_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *_mkey, 158 u32 *mkey) 159 { 160 u32 out[MLX5_ST_SZ_DW(query_special_contexts_out)] = {0}; 161 u32 in[MLX5_ST_SZ_DW(query_special_contexts_in)] = {0}; 162 int err; 163 164 MLX5_SET(query_special_contexts_in, in, opcode, 165 MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS); 166 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); 167 if (!err) 168 *mkey = MLX5_GET(query_special_contexts_out, out, dump_fill_mkey); 169 170 return err; 171 } 172 EXPORT_SYMBOL(mlx5_core_dump_fill_mkey); 173 174 static inline u32 mlx5_get_psv(u32 *out, int psv_index) 175 { 176 switch (psv_index) { 177 case 1: return MLX5_GET(create_psv_out, out, psv1_index); 178 case 2: return MLX5_GET(create_psv_out, out, psv2_index); 179 case 3: return MLX5_GET(create_psv_out, out, psv3_index); 180 default: return MLX5_GET(create_psv_out, out, psv0_index); 181 } 182 } 183 184 int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn, 185 int npsvs, u32 *sig_index) 186 { 187 u32 out[MLX5_ST_SZ_DW(create_psv_out)] = {0}; 188 u32 in[MLX5_ST_SZ_DW(create_psv_in)] = {0}; 189 int i, err; 190 191 if (npsvs > MLX5_MAX_PSVS) 192 return -EINVAL; 193 194 MLX5_SET(create_psv_in, in, opcode, MLX5_CMD_OP_CREATE_PSV); 195 MLX5_SET(create_psv_in, in, pd, pdn); 196 MLX5_SET(create_psv_in, in, num_psv, npsvs); 197 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); 198 if (err) { 199 mlx5_core_err(dev, "create_psv cmd exec failed %d\n", err); 200 return err; 201 } 202 203 for (i = 0; i < npsvs; i++) 204 sig_index[i] = mlx5_get_psv(out, i); 205 206 return err; 207 } 208 EXPORT_SYMBOL(mlx5_core_create_psv); 209 210 int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num) 211 { 212 u32 out[MLX5_ST_SZ_DW(destroy_psv_out)] = {0}; 213 u32 in[MLX5_ST_SZ_DW(destroy_psv_in)] = {0}; 214 215 MLX5_SET(destroy_psv_in, in, opcode, MLX5_CMD_OP_DESTROY_PSV); 216 MLX5_SET(destroy_psv_in, in, psvn, psv_num); 217 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); 218 } 219 EXPORT_SYMBOL(mlx5_core_destroy_psv); 220