xref: /freebsd/sys/dev/mlx5/mlx5_core/mlx5_mr.c (revision 1323ec57)
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 "opt_rss.h"
29 #include "opt_ratelimit.h"
30 
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <dev/mlx5/driver.h>
34 #include <dev/mlx5/mlx5_core/mlx5_core.h>
35 
36 static int mlx5_relaxed_ordering_write;
37 SYSCTL_INT(_hw_mlx5, OID_AUTO, relaxed_ordering_write, CTLFLAG_RWTUN,
38     &mlx5_relaxed_ordering_write, 0,
39     "Set to enable relaxed ordering for PCIe writes");
40 
41 void mlx5_init_mr_table(struct mlx5_core_dev *dev)
42 {
43 	struct mlx5_mr_table *table = &dev->priv.mr_table;
44 
45 	memset(table, 0, sizeof(*table));
46 	spin_lock_init(&table->lock);
47 	INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
48 }
49 
50 void mlx5_cleanup_mr_table(struct mlx5_core_dev *dev)
51 {
52 }
53 
54 int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
55 			     struct mlx5_core_mkey *mkey,
56 			     struct mlx5_async_ctx *async_ctx, u32 *in,
57 			     int inlen, u32 *out, int outlen,
58 			     mlx5_async_cbk_t callback,
59 			     struct mlx5_async_work *context)
60 {
61 	struct mlx5_mr_table *table = &dev->priv.mr_table;
62 	u32 lout[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
63 	u32 mkey_index;
64 	void *mkc;
65 	unsigned long flags;
66 	int err;
67 	u8 key;
68 
69 	spin_lock_irq(&dev->priv.mkey_lock);
70 	key = dev->priv.mkey_key++;
71 	spin_unlock_irq(&dev->priv.mkey_lock);
72 	mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
73 	MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
74 	MLX5_SET(mkc, mkc, mkey_7_0, key);
75 
76 	if (mlx5_relaxed_ordering_write != 0) {
77 		if (MLX5_CAP_GEN(dev, relaxed_ordering_write))
78 			MLX5_SET(mkc, mkc, relaxed_ordering_write, 1);
79 		else
80 			return (-EPROTONOSUPPORT);
81 	}
82 
83 	if (callback)
84 		return mlx5_cmd_exec_cb(async_ctx, in, inlen, out, outlen,
85 					callback, context);
86 
87 	err = mlx5_cmd_exec(dev, in, inlen, lout, sizeof(lout));
88 	if (err) {
89 		mlx5_core_dbg(dev, "cmd exec failed %d\n", err);
90 		return err;
91 	}
92 
93 	mkey_index = MLX5_GET(create_mkey_out, lout, mkey_index);
94 	mkey->iova = MLX5_GET64(mkc, mkc, start_addr);
95 	mkey->size = MLX5_GET64(mkc, mkc, len);
96 	mkey->key = mlx5_idx_to_mkey(mkey_index) | key;
97 	mkey->pd = MLX5_GET(mkc, mkc, pd);
98 
99 	mlx5_core_dbg(dev, "out 0x%x, key 0x%x, mkey 0x%x\n",
100 		      mkey_index, key, mkey->key);
101 
102 	/* connect to MR tree */
103 	spin_lock_irqsave(&table->lock, flags);
104 	err = radix_tree_insert(&table->tree, mlx5_mkey_to_idx(mkey->key), mkey);
105 	spin_unlock_irqrestore(&table->lock, flags);
106 	if (err) {
107 		mlx5_core_warn(dev, "failed radix tree insert of mr 0x%x, %d\n",
108 			       mkey->key, err);
109 		mlx5_core_destroy_mkey(dev, mkey);
110 	}
111 
112 	return err;
113 }
114 EXPORT_SYMBOL(mlx5_core_create_mkey_cb);
115 
116 int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
117 			  struct mlx5_core_mkey *mkey,
118 			  u32 *in, int inlen)
119 {
120 	return mlx5_core_create_mkey_cb(dev, mkey, NULL, in, inlen,
121 					NULL, 0, NULL, NULL);
122 }
123 EXPORT_SYMBOL(mlx5_core_create_mkey);
124 
125 int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *mkey)
126 {
127 	struct mlx5_mr_table *table = &dev->priv.mr_table;
128 	u32 out[MLX5_ST_SZ_DW(destroy_mkey_out)] = {0};
129 	u32 in[MLX5_ST_SZ_DW(destroy_mkey_in)] = {0};
130 	struct mlx5_core_mkey *deleted_mr;
131 	unsigned long flags;
132 
133 	spin_lock_irqsave(&table->lock, flags);
134 	deleted_mr = radix_tree_delete(&table->tree, mlx5_mkey_to_idx(mkey->key));
135 	spin_unlock_irqrestore(&table->lock, flags);
136 	if (!deleted_mr) {
137 		mlx5_core_warn(dev, "failed radix tree delete of mr 0x%x\n", mkey->key);
138 		return -ENOENT;
139 	}
140 
141 	MLX5_SET(destroy_mkey_in, in, opcode, MLX5_CMD_OP_DESTROY_MKEY);
142 	MLX5_SET(destroy_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key));
143 
144 	return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
145 }
146 EXPORT_SYMBOL(mlx5_core_destroy_mkey);
147 
148 int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *mkey,
149 			 u32 *out, int outlen)
150 {
151 	u32 in[MLX5_ST_SZ_DW(query_mkey_in)] = {0};
152 
153 	memset(out, 0, outlen);
154 	MLX5_SET(query_mkey_in, in, opcode, MLX5_CMD_OP_QUERY_MKEY);
155 	MLX5_SET(query_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key));
156 
157 	return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
158 }
159 EXPORT_SYMBOL(mlx5_core_query_mkey);
160 
161 int mlx5_core_dump_fill_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *_mkey,
162 			     u32 *mkey)
163 {
164 	u32 out[MLX5_ST_SZ_DW(query_special_contexts_out)] = {0};
165 	u32 in[MLX5_ST_SZ_DW(query_special_contexts_in)]   = {0};
166 	int err;
167 
168 	MLX5_SET(query_special_contexts_in, in, opcode,
169 		 MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS);
170 	err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
171 	if (!err)
172 		*mkey = MLX5_GET(query_special_contexts_out, out, dump_fill_mkey);
173 
174 	return err;
175 }
176 EXPORT_SYMBOL(mlx5_core_dump_fill_mkey);
177 
178 static inline u32 mlx5_get_psv(u32 *out, int psv_index)
179 {
180 	switch (psv_index) {
181 	case 1: return MLX5_GET(create_psv_out, out, psv1_index);
182 	case 2: return MLX5_GET(create_psv_out, out, psv2_index);
183 	case 3: return MLX5_GET(create_psv_out, out, psv3_index);
184 	default: return MLX5_GET(create_psv_out, out, psv0_index);
185 	}
186 }
187 
188 int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
189 			 int npsvs, u32 *sig_index)
190 {
191 	u32 out[MLX5_ST_SZ_DW(create_psv_out)] = {0};
192 	u32 in[MLX5_ST_SZ_DW(create_psv_in)]   = {0};
193 	int i, err;
194 
195 	if (npsvs > MLX5_MAX_PSVS)
196 		return -EINVAL;
197 
198 	MLX5_SET(create_psv_in, in, opcode, MLX5_CMD_OP_CREATE_PSV);
199 	MLX5_SET(create_psv_in, in, pd, pdn);
200 	MLX5_SET(create_psv_in, in, num_psv, npsvs);
201 	err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
202 	if (err) {
203 		mlx5_core_err(dev, "create_psv cmd exec failed %d\n", err);
204 		return err;
205 	}
206 
207 	for (i = 0; i < npsvs; i++)
208 		sig_index[i] = mlx5_get_psv(out, i);
209 
210 	return err;
211 }
212 EXPORT_SYMBOL(mlx5_core_create_psv);
213 
214 int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num)
215 {
216 	u32 out[MLX5_ST_SZ_DW(destroy_psv_out)] = {0};
217 	u32 in[MLX5_ST_SZ_DW(destroy_psv_in)]	= {0};
218 
219 	MLX5_SET(destroy_psv_in, in, opcode, MLX5_CMD_OP_DESTROY_PSV);
220 	MLX5_SET(destroy_psv_in, in, psvn, psv_num);
221 	return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
222 }
223 EXPORT_SYMBOL(mlx5_core_destroy_psv);
224