Merge tag 'tegra-for-5.5-memory-fixes' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / mr.c
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
39 int mlx5_core_create_mkey_cb(struct mlx5_core_dev *dev,
40                              struct mlx5_core_mkey *mkey,
41                              struct mlx5_async_ctx *async_ctx, u32 *in,
42                              int inlen, u32 *out, int outlen,
43                              mlx5_async_cbk_t callback,
44                              struct mlx5_async_work *context)
45 {
46         u32 lout[MLX5_ST_SZ_DW(create_mkey_out)] = {0};
47         u32 mkey_index;
48         void *mkc;
49         int err;
50         u8 key;
51
52         spin_lock_irq(&dev->priv.mkey_lock);
53         key = dev->priv.mkey_key++;
54         spin_unlock_irq(&dev->priv.mkey_lock);
55         mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
56
57         MLX5_SET(create_mkey_in, in, opcode, MLX5_CMD_OP_CREATE_MKEY);
58         MLX5_SET(mkc, mkc, mkey_7_0, key);
59
60         if (callback)
61                 return mlx5_cmd_exec_cb(async_ctx, in, inlen, out, outlen,
62                                         callback, context);
63
64         err = mlx5_cmd_exec(dev, in, inlen, lout, sizeof(lout));
65         if (err)
66                 return err;
67
68         mkey_index = MLX5_GET(create_mkey_out, lout, mkey_index);
69         mkey->iova = MLX5_GET64(mkc, mkc, start_addr);
70         mkey->size = MLX5_GET64(mkc, mkc, len);
71         mkey->key = mlx5_idx_to_mkey(mkey_index) | key;
72         mkey->pd = MLX5_GET(mkc, mkc, pd);
73
74         mlx5_core_dbg(dev, "out 0x%x, key 0x%x, mkey 0x%x\n",
75                       mkey_index, key, mkey->key);
76         return 0;
77 }
78 EXPORT_SYMBOL(mlx5_core_create_mkey_cb);
79
80 int mlx5_core_create_mkey(struct mlx5_core_dev *dev,
81                           struct mlx5_core_mkey *mkey,
82                           u32 *in, int inlen)
83 {
84         return mlx5_core_create_mkey_cb(dev, mkey, NULL, in, inlen,
85                                         NULL, 0, NULL, NULL);
86 }
87 EXPORT_SYMBOL(mlx5_core_create_mkey);
88
89 int mlx5_core_destroy_mkey(struct mlx5_core_dev *dev,
90                            struct mlx5_core_mkey *mkey)
91 {
92         u32 out[MLX5_ST_SZ_DW(destroy_mkey_out)] = {0};
93         u32 in[MLX5_ST_SZ_DW(destroy_mkey_in)]   = {0};
94
95         MLX5_SET(destroy_mkey_in, in, opcode, MLX5_CMD_OP_DESTROY_MKEY);
96         MLX5_SET(destroy_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key));
97         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
98 }
99 EXPORT_SYMBOL(mlx5_core_destroy_mkey);
100
101 int mlx5_core_query_mkey(struct mlx5_core_dev *dev, struct mlx5_core_mkey *mkey,
102                          u32 *out, int outlen)
103 {
104         u32 in[MLX5_ST_SZ_DW(query_mkey_in)] = {0};
105
106         memset(out, 0, outlen);
107         MLX5_SET(query_mkey_in, in, opcode, MLX5_CMD_OP_QUERY_MKEY);
108         MLX5_SET(query_mkey_in, in, mkey_index, mlx5_mkey_to_idx(mkey->key));
109         return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
110 }
111 EXPORT_SYMBOL(mlx5_core_query_mkey);
112
113 static inline u32 mlx5_get_psv(u32 *out, int psv_index)
114 {
115         switch (psv_index) {
116         case 1: return MLX5_GET(create_psv_out, out, psv1_index);
117         case 2: return MLX5_GET(create_psv_out, out, psv2_index);
118         case 3: return MLX5_GET(create_psv_out, out, psv3_index);
119         default: return MLX5_GET(create_psv_out, out, psv0_index);
120         }
121 }
122
123 int mlx5_core_create_psv(struct mlx5_core_dev *dev, u32 pdn,
124                          int npsvs, u32 *sig_index)
125 {
126         u32 out[MLX5_ST_SZ_DW(create_psv_out)] = {0};
127         u32 in[MLX5_ST_SZ_DW(create_psv_in)]   = {0};
128         int i, err;
129
130         if (npsvs > MLX5_MAX_PSVS)
131                 return -EINVAL;
132
133         MLX5_SET(create_psv_in, in, opcode, MLX5_CMD_OP_CREATE_PSV);
134         MLX5_SET(create_psv_in, in, pd, pdn);
135         MLX5_SET(create_psv_in, in, num_psv, npsvs);
136
137         err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
138         if (err)
139                 return err;
140
141         for (i = 0; i < npsvs; i++)
142                 sig_index[i] = mlx5_get_psv(out, i);
143
144         return err;
145 }
146 EXPORT_SYMBOL(mlx5_core_create_psv);
147
148 int mlx5_core_destroy_psv(struct mlx5_core_dev *dev, int psv_num)
149 {
150         u32 out[MLX5_ST_SZ_DW(destroy_psv_out)] = {0};
151         u32 in[MLX5_ST_SZ_DW(destroy_psv_in)]   = {0};
152
153         MLX5_SET(destroy_psv_in, in, opcode, MLX5_CMD_OP_DESTROY_PSV);
154         MLX5_SET(destroy_psv_in, in, psvn, psv_num);
155         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
156 }
157 EXPORT_SYMBOL(mlx5_core_destroy_psv);