Commit | Line | Data |
---|---|---|
e126ba97 | 1 | /* |
302bdf68 | 2 | * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved. |
e126ba97 EC |
3 | * |
4 | * This software is available to you under a choice of one of two | |
5 | * licenses. You may choose to be licensed under the terms of the GNU | |
6 | * General Public License (GPL) Version 2, available from the file | |
7 | * COPYING in the main directory of this source tree, or the | |
8 | * OpenIB.org BSD license below: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or | |
11 | * without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistributions of source code must retain the above | |
15 | * copyright notice, this list of conditions and the following | |
16 | * disclaimer. | |
17 | * | |
18 | * - Redistributions in binary form must reproduce the above | |
19 | * copyright notice, this list of conditions and the following | |
20 | * disclaimer in the documentation and/or other materials | |
21 | * provided with the distribution. | |
22 | * | |
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
30 | * SOFTWARE. | |
31 | */ | |
32 | ||
33 | #include <linux/kernel.h> | |
34 | #include <linux/module.h> | |
35 | #include <linux/mlx5/driver.h> | |
36 | #include <linux/mlx5/cmd.h> | |
37 | #include "mlx5_core.h" | |
38 | ||
a606b0f6 | 39 | void mlx5_init_mkey_table(struct mlx5_core_dev *dev) |
3bcdb17a | 40 | { |
a606b0f6 | 41 | struct mlx5_mkey_table *table = &dev->priv.mkey_table; |
3bcdb17a | 42 | |
a31208b1 | 43 | memset(table, 0, sizeof(*table)); |
3bcdb17a SG |
44 | rwlock_init(&table->lock); |
45 | INIT_RADIX_TREE(&table->tree, GFP_ATOMIC); | |
46 | } | |
47 | ||
a606b0f6 | 48 | void mlx5_cleanup_mkey_table(struct mlx5_core_dev *dev) |
3bcdb17a SG |
49 | { |
50 | } | |
51 | ||
ec22eb53 SM |
52 | int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev, |
53 | struct mlx5_core_mkey *mkey, | |
54 | u32 *in, int inlen, | |
55 | u32 *out, int outlen, | |
56 | mlx5_cmd_cbk_t callback, void *context) | |
e126ba97 | 57 | { |
a606b0f6 | 58 | struct mlx5_mkey_table *table = &dev->priv.mkey_table; |
ec22eb53 SM |
59 | u32 lout[MLX5_ST_SZ_DW(create_mkey_out)] = {0}; |
60 | u32 mkey_index; | |
61 | void *mkc; | |
e126ba97 EC |
62 | int err; |
63 | u8 key; | |
64 | ||
746b5583 | 65 | spin_lock_irq(&dev->priv.mkey_lock); |
e126ba97 | 66 | key = dev->priv.mkey_key++; |
746b5583 | 67 | spin_unlock_irq(&dev->priv.mkey_lock); |
ec22eb53 | 68 | mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry); |
746b5583 | 69 | |
ec22eb53 SM |
70 | MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY); |
71 | MLX5_SET(mkc, mkc, mkey_7_0, key); | |
e126ba97 | 72 | |
ec22eb53 SM |
73 | if (callback) |
74 | return mlx5_cmd_exec_cb(dev, in, inlen, out, outlen, | |
75 | callback, context); | |
76 | ||
77 | err = mlx5_cmd_exec(dev, in, inlen, lout, sizeof(lout)); | |
78 | err = err ? : mlx5_cmd_status_to_err_v2(lout); | |
79 | if (err) | |
80 | return err; | |
e126ba97 | 81 | |
ec22eb53 SM |
82 | mkey_index = MLX5_GET(create_mkey_out, lout, mkey_index); |
83 | mkey->iova = MLX5_GET64(mkc, mkc, start_addr); | |
84 | mkey->size = MLX5_GET64(mkc, mkc, len); | |
85 | mkey->key = mlx5_idx_to_mkey(mkey_index) | key; | |
86 | mkey->pd = MLX5_GET(mkc, mkc, pd); | |
b475598a | 87 | |
746b5583 | 88 | mlx5_core_dbg(dev, "out 0x%x, key 0x%x, mkey 0x%x\n", |
ec22eb53 | 89 | mkey_index, key, mkey->key); |
e126ba97 | 90 | |
a606b0f6 | 91 | /* connect to mkey tree */ |
3bcdb17a | 92 | write_lock_irq(&table->lock); |
a606b0f6 | 93 | err = radix_tree_insert(&table->tree, mlx5_base_mkey(mkey->key), mkey); |
3bcdb17a | 94 | write_unlock_irq(&table->lock); |
6ef07a9f | 95 | if (err) { |
a606b0f6 MB |
96 | mlx5_core_warn(dev, "failed radix tree insert of mkey 0x%x, %d\n", |
97 | mlx5_base_mkey(mkey->key), err); | |
98 | mlx5_core_destroy_mkey(dev, mkey); | |
6ef07a9f | 99 | } |
3bcdb17a | 100 | |
e126ba97 EC |
101 | return err; |
102 | } | |
ec22eb53 SM |
103 | EXPORT_SYMBOL(mlx5_core_create_mkey_cb); |
104 | ||
105 | int mlx5_core_create_mkey(struct mlx5_core_dev *dev, | |
106 | struct mlx5_core_mkey *mkey, | |
107 | u32 *in, int inlen) | |
108 | { | |
109 | return mlx5_core_create_mkey_cb(dev, mkey, in, inlen, | |
110 | NULL, 0, NULL, NULL); | |
111 | } | |
e126ba97 EC |
112 | EXPORT_SYMBOL(mlx5_core_create_mkey); |
113 | ||
a606b0f6 MB |
114 | int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev, |
115 | struct mlx5_core_mkey *mkey) | |
e126ba97 | 116 | { |
a606b0f6 | 117 | struct mlx5_mkey_table *table = &dev->priv.mkey_table; |
ec22eb53 SM |
118 | u32 out[MLX5_ST_SZ_DW(destroy_mkey_out)] = {0}; |
119 | u32 in[MLX5_ST_SZ_DW(destroy_mkey_in)] = {0}; | |
a606b0f6 | 120 | struct mlx5_core_mkey *deleted_mkey; |
3bcdb17a | 121 | unsigned long flags; |
e126ba97 EC |
122 | int err; |
123 | ||
6ef07a9f | 124 | write_lock_irqsave(&table->lock, flags); |
a606b0f6 | 125 | deleted_mkey = radix_tree_delete(&table->tree, mlx5_base_mkey(mkey->key)); |
6ef07a9f | 126 | write_unlock_irqrestore(&table->lock, flags); |
a606b0f6 MB |
127 | if (!deleted_mkey) { |
128 | mlx5_core_warn(dev, "failed radix tree delete of mkey 0x%x\n", | |
129 | mlx5_base_mkey(mkey->key)); | |
6ef07a9f SG |
130 | return -ENOENT; |
131 | } | |
132 | ||
ec22eb53 SM |
133 | MLX5_SET(destroy_mkey_in, in, opcode, MLX5_CMD_OP_DESTROY_MKEY); |
134 | MLX5_SET(destroy_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key)); | |
e126ba97 | 135 | |
ec22eb53 SM |
136 | err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); |
137 | return err ? : mlx5_cmd_status_to_err_v2(out); | |
e126ba97 EC |
138 | } |
139 | EXPORT_SYMBOL(mlx5_core_destroy_mkey); | |
140 | ||
a606b0f6 | 141 | int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *mkey, |
ec22eb53 | 142 | u32 *out, int outlen) |
e126ba97 | 143 | { |
ec22eb53 | 144 | u32 in[MLX5_ST_SZ_DW(query_mkey_in)] = {0}; |
e126ba97 EC |
145 | int err; |
146 | ||
e126ba97 | 147 | memset(out, 0, outlen); |
ec22eb53 SM |
148 | MLX5_SET(query_mkey_in, in, opcode, MLX5_CMD_OP_QUERY_MKEY); |
149 | MLX5_SET(query_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key)); | |
e126ba97 | 150 | |
ec22eb53 SM |
151 | err = mlx5_cmd_exec(dev, in, sizeof(in), out, outlen); |
152 | return err ? : mlx5_cmd_status_to_err_v2(out); | |
e126ba97 EC |
153 | } |
154 | EXPORT_SYMBOL(mlx5_core_query_mkey); | |
155 | ||
a606b0f6 | 156 | int mlx5_core_dump_fill_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *_mkey, |
e126ba97 EC |
157 | u32 *mkey) |
158 | { | |
ec22eb53 SM |
159 | u32 out[MLX5_ST_SZ_DW(query_special_contexts_out)] = {0}; |
160 | u32 in[MLX5_ST_SZ_DW(query_special_contexts_in)] = {0}; | |
e126ba97 EC |
161 | int err; |
162 | ||
ec22eb53 SM |
163 | MLX5_SET(query_special_contexts_in, in, opcode, |
164 | MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS); | |
165 | err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); | |
166 | err = err ? : mlx5_cmd_status_to_err_v2(out); | |
e126ba97 EC |
167 | if (err) |
168 | return err; | |
169 | ||
ec22eb53 | 170 | *mkey = MLX5_GET(query_special_contexts_out, out, dump_fill_mkey); |
e126ba97 EC |
171 | return err; |
172 | } | |
173 | EXPORT_SYMBOL(mlx5_core_dump_fill_mkey); | |
3121e3c4 | 174 | |
ec22eb53 SM |
175 | static inline u32 mlx5_get_psv(u32 *out, int psv_index) |
176 | { | |
177 | switch (psv_index) { | |
178 | case 1: return MLX5_GET(create_psv_out, out, psv1_index); | |
179 | case 2: return MLX5_GET(create_psv_out, out, psv2_index); | |
180 | case 3: return MLX5_GET(create_psv_out, out, psv3_index); | |
181 | default: return MLX5_GET(create_psv_out, out, psv0_index); | |
182 | } | |
183 | } | |
184 | ||
3121e3c4 SG |
185 | int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn, |
186 | int npsvs, u32 *sig_index) | |
187 | { | |
ec22eb53 SM |
188 | u32 out[MLX5_ST_SZ_DW(create_psv_out)] = {0}; |
189 | u32 in[MLX5_ST_SZ_DW(create_psv_in)] = {0}; | |
3121e3c4 SG |
190 | int i, err; |
191 | ||
192 | if (npsvs > MLX5_MAX_PSVS) | |
193 | return -EINVAL; | |
194 | ||
ec22eb53 SM |
195 | MLX5_SET(create_psv_in, in, opcode, MLX5_CMD_OP_CREATE_PSV); |
196 | MLX5_SET(create_psv_in, in, pd, pdn); | |
197 | MLX5_SET(create_psv_in, in, num_psv, npsvs); | |
3121e3c4 | 198 | |
ec22eb53 SM |
199 | err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); |
200 | err = err ? : mlx5_cmd_status_to_err_v2(out); | |
3121e3c4 | 201 | if (err) { |
ec22eb53 | 202 | mlx5_core_err(dev, "create_psv cmd exec failed %d\n", err); |
3121e3c4 SG |
203 | return err; |
204 | } | |
205 | ||
3121e3c4 | 206 | for (i = 0; i < npsvs; i++) |
ec22eb53 | 207 | sig_index[i] = mlx5_get_psv(out, i); |
3121e3c4 SG |
208 | |
209 | return err; | |
210 | } | |
211 | EXPORT_SYMBOL(mlx5_core_create_psv); | |
212 | ||
213 | int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num) | |
214 | { | |
ec22eb53 SM |
215 | u32 out[MLX5_ST_SZ_DW(destroy_psv_out)] = {0}; |
216 | u32 in[MLX5_ST_SZ_DW(destroy_psv_in)] = {0}; | |
3121e3c4 SG |
217 | int err; |
218 | ||
ec22eb53 SM |
219 | MLX5_SET(destroy_psv_in, in, opcode, MLX5_CMD_OP_DESTROY_PSV); |
220 | MLX5_SET(destroy_psv_in, in, psvn, psv_num); | |
221 | err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); | |
222 | return err ? : mlx5_cmd_status_to_err_v2(out); | |
3121e3c4 SG |
223 | } |
224 | EXPORT_SYMBOL(mlx5_core_destroy_psv); |