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