]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mlx5/mlx5_core/mlx5_mr.c
MFV r329807:
[FreeBSD/FreeBSD.git] / sys / dev / mlx5 / mlx5_core / mlx5_mr.c
1 /*-
2  * Copyright (c) 2013-2015, 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 void mlx5_init_mr_table(struct mlx5_core_dev *dev)
34 {
35         struct mlx5_mr_table *table = &dev->priv.mr_table;
36
37         spin_lock_init(&table->lock);
38         INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
39 }
40
41 void mlx5_cleanup_mr_table(struct mlx5_core_dev *dev)
42 {
43 }
44
45 int mlx5_core_create_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr,
46                           struct mlx5_create_mkey_mbox_in *in, int inlen,
47                           mlx5_cmd_cbk_t callback, void *context,
48                           struct mlx5_create_mkey_mbox_out *out)
49 {
50         struct mlx5_mr_table *table = &dev->priv.mr_table;
51         struct mlx5_create_mkey_mbox_out lout;
52         unsigned long flags;
53         int err;
54         u8 key;
55
56         memset(&lout, 0, sizeof(lout));
57         spin_lock_irq(&dev->priv.mkey_lock);
58         key = dev->priv.mkey_key++;
59         spin_unlock_irq(&dev->priv.mkey_lock);
60         in->seg.qpn_mkey7_0 |= cpu_to_be32(key);
61         in->hdr.opcode = cpu_to_be16(MLX5_CMD_OP_CREATE_MKEY);
62         if (callback) {
63                 err = mlx5_cmd_exec_cb(dev, in, inlen, out, sizeof(*out),
64                                        callback, context);
65                 return err;
66         } else {
67                 err = mlx5_cmd_exec(dev, in, inlen, &lout, sizeof(lout));
68         }
69
70         if (err) {
71                 mlx5_core_dbg(dev, "cmd exec failed %d\n", err);
72                 return err;
73         }
74
75         if (lout.hdr.status) {
76                 mlx5_core_dbg(dev, "status %d\n", lout.hdr.status);
77                 return mlx5_cmd_status_to_err(&lout.hdr);
78         }
79
80         mr->iova = be64_to_cpu(in->seg.start_addr);
81         mr->size = be64_to_cpu(in->seg.len);
82         mr->key = mlx5_idx_to_mkey(be32_to_cpu(lout.mkey) & 0xffffff) | key;
83         mr->pd = be32_to_cpu(in->seg.flags_pd) & 0xffffff;
84
85         mlx5_core_dbg(dev, "out 0x%x, key 0x%x, mkey 0x%x\n",
86                       be32_to_cpu(lout.mkey), key, mr->key);
87
88         /* connect to MR tree */
89         spin_lock_irqsave(&table->lock, flags);
90         err = radix_tree_insert(&table->tree, mlx5_mkey_to_idx(mr->key), mr);
91         spin_unlock_irqrestore(&table->lock, flags);
92         if (err) {
93                 mlx5_core_warn(dev, "failed radix tree insert of mr 0x%x, %d\n",
94                                mr->key, err);
95                 mlx5_core_destroy_mkey(dev, mr);
96         }
97
98         return err;
99 }
100 EXPORT_SYMBOL(mlx5_core_create_mkey);
101
102 int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr)
103 {
104         struct mlx5_mr_table *table = &dev->priv.mr_table;
105         u32 in[MLX5_ST_SZ_DW(destroy_mkey_in)];
106         u32 out[MLX5_ST_SZ_DW(destroy_mkey_out)];
107         struct mlx5_core_mr *deleted_mr;
108         unsigned long flags;
109
110         memset(in, 0, sizeof(in));
111
112         spin_lock_irqsave(&table->lock, flags);
113         deleted_mr = radix_tree_delete(&table->tree, mlx5_mkey_to_idx(mr->key));
114         spin_unlock_irqrestore(&table->lock, flags);
115         if (!deleted_mr) {
116                 mlx5_core_warn(dev, "failed radix tree delete of mr 0x%x\n", mr->key);
117                 return -ENOENT;
118         }
119
120         MLX5_SET(destroy_mkey_in, in, opcode, MLX5_CMD_OP_DESTROY_MKEY);
121         MLX5_SET(destroy_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mr->key));
122
123         memset(out, 0, sizeof(out));
124         return mlx5_cmd_exec_check_status(dev, in,  sizeof(in),
125                                                out, sizeof(out));
126 }
127 EXPORT_SYMBOL(mlx5_core_destroy_mkey);
128
129 int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr,
130                          struct mlx5_query_mkey_mbox_out *out, int outlen)
131 {
132         struct mlx5_query_mkey_mbox_in in;
133         int err;
134
135         memset(&in, 0, sizeof(in));
136         memset(out, 0, outlen);
137
138         in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_MKEY);
139         in.mkey = cpu_to_be32(mlx5_mkey_to_idx(mr->key));
140         err = mlx5_cmd_exec(dev, &in, sizeof(in), out, outlen);
141         if (err)
142                 return err;
143
144         if (out->hdr.status)
145                 return mlx5_cmd_status_to_err(&out->hdr);
146
147         return err;
148 }
149 EXPORT_SYMBOL(mlx5_core_query_mkey);
150
151 int mlx5_core_dump_fill_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mr *mr,
152                              u32 *mkey)
153 {
154         struct mlx5_query_special_ctxs_mbox_in in;
155         struct mlx5_query_special_ctxs_mbox_out out;
156         int err;
157
158         memset(&in, 0, sizeof(in));
159         memset(&out, 0, sizeof(out));
160
161         in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_QUERY_SPECIAL_CONTEXTS);
162         err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
163         if (err)
164                 return err;
165
166         if (out.hdr.status)
167                 return mlx5_cmd_status_to_err(&out.hdr);
168
169         *mkey = be32_to_cpu(out.dump_fill_mkey);
170
171         return err;
172 }
173 EXPORT_SYMBOL(mlx5_core_dump_fill_mkey);
174
175 int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
176                          int npsvs, u32 *sig_index)
177 {
178         struct mlx5_allocate_psv_in in;
179         struct mlx5_allocate_psv_out out;
180         int i, err;
181
182         if (npsvs > MLX5_MAX_PSVS)
183                 return -EINVAL;
184
185         memset(&in, 0, sizeof(in));
186         memset(&out, 0, sizeof(out));
187
188         in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_CREATE_PSV);
189         in.npsv_pd = cpu_to_be32((npsvs << 28) | pdn);
190         err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
191         if (err) {
192                 mlx5_core_err(dev, "cmd exec failed %d\n", err);
193                 return err;
194         }
195
196         if (out.hdr.status) {
197                 mlx5_core_err(dev, "create_psv bad status %d\n",
198                               out.hdr.status);
199                 return mlx5_cmd_status_to_err(&out.hdr);
200         }
201
202         for (i = 0; i < npsvs; i++)
203                 sig_index[i] = be32_to_cpu(out.psv_idx[i]) & 0xffffff;
204
205         return err;
206 }
207 EXPORT_SYMBOL(mlx5_core_create_psv);
208
209 int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num)
210 {
211         struct mlx5_destroy_psv_in in;
212         struct mlx5_destroy_psv_out out;
213         int err;
214
215         memset(&in, 0, sizeof(in));
216         memset(&out, 0, sizeof(out));
217
218         in.psv_number = cpu_to_be32(psv_num);
219         in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_DESTROY_PSV);
220         err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
221         if (err) {
222                 mlx5_core_err(dev, "destroy_psv cmd exec failed %d\n", err);
223                 goto out;
224         }
225
226         if (out.hdr.status) {
227                 mlx5_core_err(dev, "destroy_psv bad status %d\n",
228                               out.hdr.status);
229                 err = mlx5_cmd_status_to_err(&out.hdr);
230                 goto out;
231         }
232
233 out:
234         return err;
235 }
236 EXPORT_SYMBOL(mlx5_core_destroy_psv);