1 /*
2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
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
mlx5_init_mkey_table(struct mlx5_core_dev * dev)39 void mlx5_init_mkey_table(struct mlx5_core_dev *dev)
40 {
41 xa_init_flags(&dev->priv.mkey_table, XA_FLAGS_LOCK_IRQ);
42 }
43
mlx5_cleanup_mkey_table(struct mlx5_core_dev * dev)44 void mlx5_cleanup_mkey_table(struct mlx5_core_dev *dev)
45 {
46 WARN_ON(!xa_empty(&dev->priv.mkey_table));
47 }
48
mlx5_core_create_mkey_cb(struct mlx5_core_dev * dev,struct mlx5_core_mkey * mkey,struct mlx5_async_ctx * async_ctx,u32 * in,int inlen,u32 * out,int outlen,mlx5_async_cbk_t callback,struct mlx5_async_work * context)49 int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
50 struct mlx5_core_mkey *mkey,
51 struct mlx5_async_ctx *async_ctx, u32 *in,
52 int inlen, u32 *out, int outlen,
53 mlx5_async_cbk_t callback,
54 struct mlx5_async_work *context)
55 {
56 u32 lout[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
57 struct xarray *mkeys = &dev->priv.mkey_table;
58 u32 mkey_index;
59 void *mkc;
60 int err;
61 u8 key;
62
63 spin_lock_irq(&dev->priv.mkey_lock);
64 key = dev->priv.mkey_key++;
65 spin_unlock_irq(&dev->priv.mkey_lock);
66 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
67
68 MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
69 MLX5_SET(mkc, mkc, mkey_7_0, key);
70
71 if (callback)
72 return mlx5_cmd_exec_cb(async_ctx, in, inlen, out, outlen,
73 callback, context);
74
75 err = mlx5_cmd_exec(dev, in, inlen, lout, sizeof(lout));
76 if (err)
77 return err;
78
79 mkey_index = MLX5_GET(create_mkey_out, lout, mkey_index);
80 mkey->iova = MLX5_GET64(mkc, mkc, start_addr);
81 mkey->size = MLX5_GET64(mkc, mkc, len);
82 mkey->key = mlx5_idx_to_mkey(mkey_index) | key;
83 mkey->pd = MLX5_GET(mkc, mkc, pd);
84
85 mlx5_core_dbg(dev, "out 0x%x, key 0x%x, mkey 0x%x\n",
86 mkey_index, key, mkey->key);
87
88 err = xa_err(xa_store_irq(mkeys, mlx5_base_mkey(mkey->key), mkey,
89 GFP_KERNEL));
90 if (err) {
91 mlx5_core_warn(dev, "failed xarray insert of mkey 0x%x, %d\n",
92 mlx5_base_mkey(mkey->key), err);
93 mlx5_core_destroy_mkey(dev, mkey);
94 }
95
96 return err;
97 }
98 EXPORT_SYMBOL(mlx5_core_create_mkey_cb);
99
mlx5_core_create_mkey(struct mlx5_core_dev * dev,struct mlx5_core_mkey * mkey,u32 * in,int inlen)100 int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
101 struct mlx5_core_mkey *mkey,
102 u32 *in, int inlen)
103 {
104 return mlx5_core_create_mkey_cb(dev, mkey, NULL, in, inlen,
105 NULL, 0, NULL, NULL);
106 }
107 EXPORT_SYMBOL(mlx5_core_create_mkey);
108
mlx5_core_destroy_mkey(struct mlx5_core_dev * dev,struct mlx5_core_mkey * mkey)109 int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev,
110 struct mlx5_core_mkey *mkey)
111 {
112 u32 out[MLX5_ST_SZ_DW(destroy_mkey_out)] = {0};
113 u32 in[MLX5_ST_SZ_DW(destroy_mkey_in)] = {0};
114 struct xarray *mkeys = &dev->priv.mkey_table;
115 unsigned long flags;
116
117 xa_lock_irqsave(mkeys, flags);
118 __xa_erase(mkeys, mlx5_base_mkey(mkey->key));
119 xa_unlock_irqrestore(mkeys, flags);
120
121 MLX5_SET(destroy_mkey_in, in, opcode, MLX5_CMD_OP_DESTROY_MKEY);
122 MLX5_SET(destroy_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key));
123 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
124 }
125 EXPORT_SYMBOL(mlx5_core_destroy_mkey);
126
mlx5_core_query_mkey(struct mlx5_core_dev * dev,struct mlx5_core_mkey * mkey,u32 * out,int outlen)127 int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *mkey,
128 u32 *out, int outlen)
129 {
130 u32 in[MLX5_ST_SZ_DW(query_mkey_in)] = {0};
131
132 memset(out, 0, outlen);
133 MLX5_SET(query_mkey_in, in, opcode, MLX5_CMD_OP_QUERY_MKEY);
134 MLX5_SET(query_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key));
135 return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
136 }
137 EXPORT_SYMBOL(mlx5_core_query_mkey);
138
mlx5_get_psv(u32 * out,int psv_index)139 static inline u32 mlx5_get_psv(u32 *out, int psv_index)
140 {
141 switch (psv_index) {
142 case 1: return MLX5_GET(create_psv_out, out, psv1_index);
143 case 2: return MLX5_GET(create_psv_out, out, psv2_index);
144 case 3: return MLX5_GET(create_psv_out, out, psv3_index);
145 default: return MLX5_GET(create_psv_out, out, psv0_index);
146 }
147 }
148
mlx5_core_create_psv(struct mlx5_core_dev * dev,u32 pdn,int npsvs,u32 * sig_index)149 int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
150 int npsvs, u32 *sig_index)
151 {
152 u32 out[MLX5_ST_SZ_DW(create_psv_out)] = {0};
153 u32 in[MLX5_ST_SZ_DW(create_psv_in)] = {0};
154 int i, err;
155
156 if (npsvs > MLX5_MAX_PSVS)
157 return -EINVAL;
158
159 MLX5_SET(create_psv_in, in, opcode, MLX5_CMD_OP_CREATE_PSV);
160 MLX5_SET(create_psv_in, in, pd, pdn);
161 MLX5_SET(create_psv_in, in, num_psv, npsvs);
162
163 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
164 if (err)
165 return err;
166
167 for (i = 0; i < npsvs; i++)
168 sig_index[i] = mlx5_get_psv(out, i);
169
170 return err;
171 }
172 EXPORT_SYMBOL(mlx5_core_create_psv);
173
mlx5_core_destroy_psv(struct mlx5_core_dev * dev,int psv_num)174 int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num)
175 {
176 u32 out[MLX5_ST_SZ_DW(destroy_psv_out)] = {0};
177 u32 in[MLX5_ST_SZ_DW(destroy_psv_in)] = {0};
178
179 MLX5_SET(destroy_psv_in, in, opcode, MLX5_CMD_OP_DESTROY_PSV);
180 MLX5_SET(destroy_psv_in, in, psvn, psv_num);
181 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
182 }
183 EXPORT_SYMBOL(mlx5_core_destroy_psv);
184