net/mlx5: Update access functions to Query/Modify vport MAC address
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / vport.c
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  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/export.h>
34 #include <linux/etherdevice.h>
35 #include <linux/mlx5/driver.h>
36 #include <linux/mlx5/vport.h>
37 #include "mlx5_core.h"
38
39 u8 mlx5_query_vport_state(struct mlx5_core_dev *mdev, u8 opmod)
40 {
41         u32 in[MLX5_ST_SZ_DW(query_vport_state_in)];
42         u32 out[MLX5_ST_SZ_DW(query_vport_state_out)];
43         int err;
44
45         memset(in, 0, sizeof(in));
46
47         MLX5_SET(query_vport_state_in, in, opcode,
48                  MLX5_CMD_OP_QUERY_VPORT_STATE);
49         MLX5_SET(query_vport_state_in, in, op_mod, opmod);
50
51         err = mlx5_cmd_exec_check_status(mdev, in, sizeof(in), out,
52                                          sizeof(out));
53         if (err)
54                 mlx5_core_warn(mdev, "MLX5_CMD_OP_QUERY_VPORT_STATE failed\n");
55
56         return MLX5_GET(query_vport_state_out, out, state);
57 }
58 EXPORT_SYMBOL(mlx5_query_vport_state);
59
60 static int mlx5_query_nic_vport_context(struct mlx5_core_dev *mdev, u16 vport,
61                                         u32 *out, int outlen)
62 {
63         u32 in[MLX5_ST_SZ_DW(query_nic_vport_context_in)];
64
65         memset(in, 0, sizeof(in));
66
67         MLX5_SET(query_nic_vport_context_in, in, opcode,
68                  MLX5_CMD_OP_QUERY_NIC_VPORT_CONTEXT);
69
70         MLX5_SET(query_nic_vport_context_in, in, vport_number, vport);
71         if (vport)
72                 MLX5_SET(query_nic_vport_context_in, in, other_vport, 1);
73
74         return mlx5_cmd_exec_check_status(mdev, in, sizeof(in), out, outlen);
75 }
76
77 static int mlx5_modify_nic_vport_context(struct mlx5_core_dev *mdev, void *in,
78                                          int inlen)
79 {
80         u32 out[MLX5_ST_SZ_DW(modify_nic_vport_context_out)];
81
82         MLX5_SET(modify_nic_vport_context_in, in, opcode,
83                  MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
84
85         memset(out, 0, sizeof(out));
86         return mlx5_cmd_exec_check_status(mdev, in, inlen, out, sizeof(out));
87 }
88
89 int mlx5_query_nic_vport_mac_address(struct mlx5_core_dev *mdev,
90                                      u16 vport, u8 *addr)
91 {
92         u32 *out;
93         int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
94         u8 *out_addr;
95         int err;
96
97         out = mlx5_vzalloc(outlen);
98         if (!out)
99                 return -ENOMEM;
100
101         out_addr = MLX5_ADDR_OF(query_nic_vport_context_out, out,
102                                 nic_vport_context.permanent_address);
103
104         err = mlx5_query_nic_vport_context(mdev, vport, out, outlen);
105         if (err)
106                 goto out;
107
108         ether_addr_copy(addr, &out_addr[2]);
109
110 out:
111         kvfree(out);
112         return err;
113 }
114 EXPORT_SYMBOL_GPL(mlx5_query_nic_vport_mac_address);
115
116 int mlx5_modify_nic_vport_mac_address(struct mlx5_core_dev *mdev,
117                                       u16 vport, u8 *addr)
118 {
119         void *in;
120         int inlen = MLX5_ST_SZ_BYTES(modify_nic_vport_context_in);
121         int err;
122         void *nic_vport_ctx;
123         u8 *perm_mac;
124
125         in = mlx5_vzalloc(inlen);
126         if (!in) {
127                 mlx5_core_warn(mdev, "failed to allocate inbox\n");
128                 return -ENOMEM;
129         }
130
131         MLX5_SET(modify_nic_vport_context_in, in,
132                  field_select.permanent_address, 1);
133         MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
134
135         if (vport)
136                 MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
137
138         nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
139                                      in, nic_vport_context);
140         perm_mac = MLX5_ADDR_OF(nic_vport_context, nic_vport_ctx,
141                                 permanent_address);
142
143         ether_addr_copy(&perm_mac[2], addr);
144
145         err = mlx5_modify_nic_vport_context(mdev, in, inlen);
146
147         kvfree(in);
148
149         return err;
150 }
151 EXPORT_SYMBOL(mlx5_modify_nic_vport_mac_address);
152
153 int mlx5_query_hca_vport_gid(struct mlx5_core_dev *dev, u8 other_vport,
154                              u8 port_num, u16  vf_num, u16 gid_index,
155                              union ib_gid *gid)
156 {
157         int in_sz = MLX5_ST_SZ_BYTES(query_hca_vport_gid_in);
158         int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_gid_out);
159         int is_group_manager;
160         void *out = NULL;
161         void *in = NULL;
162         union ib_gid *tmp;
163         int tbsz;
164         int nout;
165         int err;
166
167         is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
168         tbsz = mlx5_get_gid_table_len(MLX5_CAP_GEN(dev, gid_table_size));
169         mlx5_core_dbg(dev, "vf_num %d, index %d, gid_table_size %d\n",
170                       vf_num, gid_index, tbsz);
171
172         if (gid_index > tbsz && gid_index != 0xffff)
173                 return -EINVAL;
174
175         if (gid_index == 0xffff)
176                 nout = tbsz;
177         else
178                 nout = 1;
179
180         out_sz += nout * sizeof(*gid);
181
182         in = kzalloc(in_sz, GFP_KERNEL);
183         out = kzalloc(out_sz, GFP_KERNEL);
184         if (!in || !out) {
185                 err = -ENOMEM;
186                 goto out;
187         }
188
189         MLX5_SET(query_hca_vport_gid_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_GID);
190         if (other_vport) {
191                 if (is_group_manager) {
192                         MLX5_SET(query_hca_vport_gid_in, in, vport_number, vf_num);
193                         MLX5_SET(query_hca_vport_gid_in, in, other_vport, 1);
194                 } else {
195                         err = -EPERM;
196                         goto out;
197                 }
198         }
199         MLX5_SET(query_hca_vport_gid_in, in, gid_index, gid_index);
200
201         if (MLX5_CAP_GEN(dev, num_ports) == 2)
202                 MLX5_SET(query_hca_vport_gid_in, in, port_num, port_num);
203
204         err = mlx5_cmd_exec(dev, in, in_sz, out, out_sz);
205         if (err)
206                 goto out;
207
208         err = mlx5_cmd_status_to_err_v2(out);
209         if (err)
210                 goto out;
211
212         tmp = out + MLX5_ST_SZ_BYTES(query_hca_vport_gid_out);
213         gid->global.subnet_prefix = tmp->global.subnet_prefix;
214         gid->global.interface_id = tmp->global.interface_id;
215
216 out:
217         kfree(in);
218         kfree(out);
219         return err;
220 }
221 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_gid);
222
223 int mlx5_query_hca_vport_pkey(struct mlx5_core_dev *dev, u8 other_vport,
224                               u8 port_num, u16 vf_num, u16 pkey_index,
225                               u16 *pkey)
226 {
227         int in_sz = MLX5_ST_SZ_BYTES(query_hca_vport_pkey_in);
228         int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_pkey_out);
229         int is_group_manager;
230         void *out = NULL;
231         void *in = NULL;
232         void *pkarr;
233         int nout;
234         int tbsz;
235         int err;
236         int i;
237
238         is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
239
240         tbsz = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size));
241         if (pkey_index > tbsz && pkey_index != 0xffff)
242                 return -EINVAL;
243
244         if (pkey_index == 0xffff)
245                 nout = tbsz;
246         else
247                 nout = 1;
248
249         out_sz += nout * MLX5_ST_SZ_BYTES(pkey);
250
251         in = kzalloc(in_sz, GFP_KERNEL);
252         out = kzalloc(out_sz, GFP_KERNEL);
253         if (!in || !out) {
254                 err = -ENOMEM;
255                 goto out;
256         }
257
258         MLX5_SET(query_hca_vport_pkey_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_PKEY);
259         if (other_vport) {
260                 if (is_group_manager) {
261                         MLX5_SET(query_hca_vport_pkey_in, in, vport_number, vf_num);
262                         MLX5_SET(query_hca_vport_pkey_in, in, other_vport, 1);
263                 } else {
264                         err = -EPERM;
265                         goto out;
266                 }
267         }
268         MLX5_SET(query_hca_vport_pkey_in, in, pkey_index, pkey_index);
269
270         if (MLX5_CAP_GEN(dev, num_ports) == 2)
271                 MLX5_SET(query_hca_vport_pkey_in, in, port_num, port_num);
272
273         err = mlx5_cmd_exec(dev, in, in_sz, out, out_sz);
274         if (err)
275                 goto out;
276
277         err = mlx5_cmd_status_to_err_v2(out);
278         if (err)
279                 goto out;
280
281         pkarr = MLX5_ADDR_OF(query_hca_vport_pkey_out, out, pkey);
282         for (i = 0; i < nout; i++, pkey++, pkarr += MLX5_ST_SZ_BYTES(pkey))
283                 *pkey = MLX5_GET_PR(pkey, pkarr, pkey);
284
285 out:
286         kfree(in);
287         kfree(out);
288         return err;
289 }
290 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_pkey);
291
292 int mlx5_query_hca_vport_context(struct mlx5_core_dev *dev,
293                                  u8 other_vport, u8 port_num,
294                                  u16 vf_num,
295                                  struct mlx5_hca_vport_context *rep)
296 {
297         int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_context_out);
298         int in[MLX5_ST_SZ_DW(query_hca_vport_context_in)];
299         int is_group_manager;
300         void *out;
301         void *ctx;
302         int err;
303
304         is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
305
306         memset(in, 0, sizeof(in));
307         out = kzalloc(out_sz, GFP_KERNEL);
308         if (!out)
309                 return -ENOMEM;
310
311         MLX5_SET(query_hca_vport_context_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT);
312
313         if (other_vport) {
314                 if (is_group_manager) {
315                         MLX5_SET(query_hca_vport_context_in, in, other_vport, 1);
316                         MLX5_SET(query_hca_vport_context_in, in, vport_number, vf_num);
317                 } else {
318                         err = -EPERM;
319                         goto ex;
320                 }
321         }
322
323         if (MLX5_CAP_GEN(dev, num_ports) == 2)
324                 MLX5_SET(query_hca_vport_context_in, in, port_num, port_num);
325
326         err = mlx5_cmd_exec(dev, in, sizeof(in), out,  out_sz);
327         if (err)
328                 goto ex;
329         err = mlx5_cmd_status_to_err_v2(out);
330         if (err)
331                 goto ex;
332
333         ctx = MLX5_ADDR_OF(query_hca_vport_context_out, out, hca_vport_context);
334         rep->field_select = MLX5_GET_PR(hca_vport_context, ctx, field_select);
335         rep->sm_virt_aware = MLX5_GET_PR(hca_vport_context, ctx, sm_virt_aware);
336         rep->has_smi = MLX5_GET_PR(hca_vport_context, ctx, has_smi);
337         rep->has_raw = MLX5_GET_PR(hca_vport_context, ctx, has_raw);
338         rep->policy = MLX5_GET_PR(hca_vport_context, ctx, vport_state_policy);
339         rep->phys_state = MLX5_GET_PR(hca_vport_context, ctx,
340                                       port_physical_state);
341         rep->vport_state = MLX5_GET_PR(hca_vport_context, ctx, vport_state);
342         rep->port_physical_state = MLX5_GET_PR(hca_vport_context, ctx,
343                                                port_physical_state);
344         rep->port_guid = MLX5_GET64_PR(hca_vport_context, ctx, port_guid);
345         rep->node_guid = MLX5_GET64_PR(hca_vport_context, ctx, node_guid);
346         rep->cap_mask1 = MLX5_GET_PR(hca_vport_context, ctx, cap_mask1);
347         rep->cap_mask1_perm = MLX5_GET_PR(hca_vport_context, ctx,
348                                           cap_mask1_field_select);
349         rep->cap_mask2 = MLX5_GET_PR(hca_vport_context, ctx, cap_mask2);
350         rep->cap_mask2_perm = MLX5_GET_PR(hca_vport_context, ctx,
351                                           cap_mask2_field_select);
352         rep->lid = MLX5_GET_PR(hca_vport_context, ctx, lid);
353         rep->init_type_reply = MLX5_GET_PR(hca_vport_context, ctx,
354                                            init_type_reply);
355         rep->lmc = MLX5_GET_PR(hca_vport_context, ctx, lmc);
356         rep->subnet_timeout = MLX5_GET_PR(hca_vport_context, ctx,
357                                           subnet_timeout);
358         rep->sm_lid = MLX5_GET_PR(hca_vport_context, ctx, sm_lid);
359         rep->sm_sl = MLX5_GET_PR(hca_vport_context, ctx, sm_sl);
360         rep->qkey_violation_counter = MLX5_GET_PR(hca_vport_context, ctx,
361                                                   qkey_violation_counter);
362         rep->pkey_violation_counter = MLX5_GET_PR(hca_vport_context, ctx,
363                                                   pkey_violation_counter);
364         rep->grh_required = MLX5_GET_PR(hca_vport_context, ctx, grh_required);
365         rep->sys_image_guid = MLX5_GET64_PR(hca_vport_context, ctx,
366                                             system_image_guid);
367
368 ex:
369         kfree(out);
370         return err;
371 }
372 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_context);
373
374 int mlx5_query_hca_vport_system_image_guid(struct mlx5_core_dev *dev,
375                                            u64 *sys_image_guid)
376 {
377         struct mlx5_hca_vport_context *rep;
378         int err;
379
380         rep = kzalloc(sizeof(*rep), GFP_KERNEL);
381         if (!rep)
382                 return -ENOMEM;
383
384         err = mlx5_query_hca_vport_context(dev, 0, 1, 0, rep);
385         if (!err)
386                 *sys_image_guid = rep->sys_image_guid;
387
388         kfree(rep);
389         return err;
390 }
391 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_system_image_guid);
392
393 int mlx5_query_hca_vport_node_guid(struct mlx5_core_dev *dev,
394                                    u64 *node_guid)
395 {
396         struct mlx5_hca_vport_context *rep;
397         int err;
398
399         rep = kzalloc(sizeof(*rep), GFP_KERNEL);
400         if (!rep)
401                 return -ENOMEM;
402
403         err = mlx5_query_hca_vport_context(dev, 0, 1, 0, rep);
404         if (!err)
405                 *node_guid = rep->node_guid;
406
407         kfree(rep);
408         return err;
409 }
410 EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_node_guid);