net/mlx5_core: Break down the vport mac address query function
[deliverable/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / vport.c
CommitLineData
afb736e9
AV
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>
d18a9470 36#include <linux/mlx5/vport.h>
afb736e9
AV
37#include "mlx5_core.h"
38
39u8 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}
d18a9470 58EXPORT_SYMBOL(mlx5_query_vport_state);
afb736e9 59
e5f6175c
AS
60static int mlx5_query_nic_vport_context(struct mlx5_core_dev *mdev, u32 *out,
61 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 return mlx5_cmd_exec_check_status(mdev, in, sizeof(in), out, outlen);
71}
72
d18a9470 73void mlx5_query_nic_vport_mac_address(struct mlx5_core_dev *mdev, u8 *addr)
afb736e9 74{
afb736e9
AV
75 u32 *out;
76 int outlen = MLX5_ST_SZ_BYTES(query_nic_vport_context_out);
77 u8 *out_addr;
e5f6175c 78 int err;
afb736e9
AV
79
80 out = mlx5_vzalloc(outlen);
81 if (!out)
82 return;
83
84 out_addr = MLX5_ADDR_OF(query_nic_vport_context_out, out,
85 nic_vport_context.permanent_address);
86
e5f6175c
AS
87 err = mlx5_query_nic_vport_context(mdev, out, outlen);
88 if (!err)
89 ether_addr_copy(addr, &out_addr[2]);
afb736e9
AV
90
91 kvfree(out);
92}
d18a9470 93EXPORT_SYMBOL(mlx5_query_nic_vport_mac_address);
707c4602
MD
94
95int mlx5_query_hca_vport_gid(struct mlx5_core_dev *dev, u8 other_vport,
96 u8 port_num, u16 vf_num, u16 gid_index,
97 union ib_gid *gid)
98{
99 int in_sz = MLX5_ST_SZ_BYTES(query_hca_vport_gid_in);
100 int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_gid_out);
101 int is_group_manager;
102 void *out = NULL;
103 void *in = NULL;
104 union ib_gid *tmp;
105 int tbsz;
106 int nout;
107 int err;
108
109 is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
110 tbsz = mlx5_get_gid_table_len(MLX5_CAP_GEN(dev, gid_table_size));
111 mlx5_core_dbg(dev, "vf_num %d, index %d, gid_table_size %d\n",
112 vf_num, gid_index, tbsz);
113
114 if (gid_index > tbsz && gid_index != 0xffff)
115 return -EINVAL;
116
117 if (gid_index == 0xffff)
118 nout = tbsz;
119 else
120 nout = 1;
121
122 out_sz += nout * sizeof(*gid);
123
124 in = kzalloc(in_sz, GFP_KERNEL);
125 out = kzalloc(out_sz, GFP_KERNEL);
126 if (!in || !out) {
127 err = -ENOMEM;
128 goto out;
129 }
130
131 MLX5_SET(query_hca_vport_gid_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_GID);
132 if (other_vport) {
133 if (is_group_manager) {
134 MLX5_SET(query_hca_vport_gid_in, in, vport_number, vf_num);
135 MLX5_SET(query_hca_vport_gid_in, in, other_vport, 1);
136 } else {
137 err = -EPERM;
138 goto out;
139 }
140 }
141 MLX5_SET(query_hca_vport_gid_in, in, gid_index, gid_index);
142
143 if (MLX5_CAP_GEN(dev, num_ports) == 2)
144 MLX5_SET(query_hca_vport_gid_in, in, port_num, port_num);
145
146 err = mlx5_cmd_exec(dev, in, in_sz, out, out_sz);
147 if (err)
148 goto out;
149
150 err = mlx5_cmd_status_to_err_v2(out);
151 if (err)
152 goto out;
153
154 tmp = out + MLX5_ST_SZ_BYTES(query_hca_vport_gid_out);
155 gid->global.subnet_prefix = tmp->global.subnet_prefix;
156 gid->global.interface_id = tmp->global.interface_id;
157
158out:
159 kfree(in);
160 kfree(out);
161 return err;
162}
163EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_gid);
164
165int mlx5_query_hca_vport_pkey(struct mlx5_core_dev *dev, u8 other_vport,
166 u8 port_num, u16 vf_num, u16 pkey_index,
167 u16 *pkey)
168{
169 int in_sz = MLX5_ST_SZ_BYTES(query_hca_vport_pkey_in);
170 int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_pkey_out);
171 int is_group_manager;
172 void *out = NULL;
173 void *in = NULL;
174 void *pkarr;
175 int nout;
176 int tbsz;
177 int err;
178 int i;
179
180 is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
181
182 tbsz = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(dev, pkey_table_size));
183 if (pkey_index > tbsz && pkey_index != 0xffff)
184 return -EINVAL;
185
186 if (pkey_index == 0xffff)
187 nout = tbsz;
188 else
189 nout = 1;
190
191 out_sz += nout * MLX5_ST_SZ_BYTES(pkey);
192
193 in = kzalloc(in_sz, GFP_KERNEL);
194 out = kzalloc(out_sz, GFP_KERNEL);
195 if (!in || !out) {
196 err = -ENOMEM;
197 goto out;
198 }
199
200 MLX5_SET(query_hca_vport_pkey_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_PKEY);
201 if (other_vport) {
202 if (is_group_manager) {
203 MLX5_SET(query_hca_vport_pkey_in, in, vport_number, vf_num);
204 MLX5_SET(query_hca_vport_pkey_in, in, other_vport, 1);
205 } else {
206 err = -EPERM;
207 goto out;
208 }
209 }
210 MLX5_SET(query_hca_vport_pkey_in, in, pkey_index, pkey_index);
211
212 if (MLX5_CAP_GEN(dev, num_ports) == 2)
213 MLX5_SET(query_hca_vport_pkey_in, in, port_num, port_num);
214
215 err = mlx5_cmd_exec(dev, in, in_sz, out, out_sz);
216 if (err)
217 goto out;
218
219 err = mlx5_cmd_status_to_err_v2(out);
220 if (err)
221 goto out;
222
223 pkarr = MLX5_ADDR_OF(query_hca_vport_pkey_out, out, pkey);
224 for (i = 0; i < nout; i++, pkey++, pkarr += MLX5_ST_SZ_BYTES(pkey))
225 *pkey = MLX5_GET_PR(pkey, pkarr, pkey);
226
227out:
228 kfree(in);
229 kfree(out);
230 return err;
231}
232EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_pkey);
233
234int mlx5_query_hca_vport_context(struct mlx5_core_dev *dev,
235 u8 other_vport, u8 port_num,
236 u16 vf_num,
237 struct mlx5_hca_vport_context *rep)
238{
239 int out_sz = MLX5_ST_SZ_BYTES(query_hca_vport_context_out);
240 int in[MLX5_ST_SZ_DW(query_hca_vport_context_in)];
241 int is_group_manager;
242 void *out;
243 void *ctx;
244 int err;
245
246 is_group_manager = MLX5_CAP_GEN(dev, vport_group_manager);
247
248 memset(in, 0, sizeof(in));
249 out = kzalloc(out_sz, GFP_KERNEL);
250 if (!out)
251 return -ENOMEM;
252
253 MLX5_SET(query_hca_vport_context_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_VPORT_CONTEXT);
254
255 if (other_vport) {
256 if (is_group_manager) {
257 MLX5_SET(query_hca_vport_context_in, in, other_vport, 1);
258 MLX5_SET(query_hca_vport_context_in, in, vport_number, vf_num);
259 } else {
260 err = -EPERM;
261 goto ex;
262 }
263 }
264
265 if (MLX5_CAP_GEN(dev, num_ports) == 2)
266 MLX5_SET(query_hca_vport_context_in, in, port_num, port_num);
267
268 err = mlx5_cmd_exec(dev, in, sizeof(in), out, out_sz);
269 if (err)
270 goto ex;
271 err = mlx5_cmd_status_to_err_v2(out);
272 if (err)
273 goto ex;
274
275 ctx = MLX5_ADDR_OF(query_hca_vport_context_out, out, hca_vport_context);
276 rep->field_select = MLX5_GET_PR(hca_vport_context, ctx, field_select);
277 rep->sm_virt_aware = MLX5_GET_PR(hca_vport_context, ctx, sm_virt_aware);
278 rep->has_smi = MLX5_GET_PR(hca_vport_context, ctx, has_smi);
279 rep->has_raw = MLX5_GET_PR(hca_vport_context, ctx, has_raw);
280 rep->policy = MLX5_GET_PR(hca_vport_context, ctx, vport_state_policy);
281 rep->phys_state = MLX5_GET_PR(hca_vport_context, ctx,
282 port_physical_state);
283 rep->vport_state = MLX5_GET_PR(hca_vport_context, ctx, vport_state);
284 rep->port_physical_state = MLX5_GET_PR(hca_vport_context, ctx,
285 port_physical_state);
286 rep->port_guid = MLX5_GET64_PR(hca_vport_context, ctx, port_guid);
287 rep->node_guid = MLX5_GET64_PR(hca_vport_context, ctx, node_guid);
288 rep->cap_mask1 = MLX5_GET_PR(hca_vport_context, ctx, cap_mask1);
289 rep->cap_mask1_perm = MLX5_GET_PR(hca_vport_context, ctx,
290 cap_mask1_field_select);
291 rep->cap_mask2 = MLX5_GET_PR(hca_vport_context, ctx, cap_mask2);
292 rep->cap_mask2_perm = MLX5_GET_PR(hca_vport_context, ctx,
293 cap_mask2_field_select);
294 rep->lid = MLX5_GET_PR(hca_vport_context, ctx, lid);
295 rep->init_type_reply = MLX5_GET_PR(hca_vport_context, ctx,
296 init_type_reply);
297 rep->lmc = MLX5_GET_PR(hca_vport_context, ctx, lmc);
298 rep->subnet_timeout = MLX5_GET_PR(hca_vport_context, ctx,
299 subnet_timeout);
300 rep->sm_lid = MLX5_GET_PR(hca_vport_context, ctx, sm_lid);
301 rep->sm_sl = MLX5_GET_PR(hca_vport_context, ctx, sm_sl);
302 rep->qkey_violation_counter = MLX5_GET_PR(hca_vport_context, ctx,
303 qkey_violation_counter);
304 rep->pkey_violation_counter = MLX5_GET_PR(hca_vport_context, ctx,
305 pkey_violation_counter);
306 rep->grh_required = MLX5_GET_PR(hca_vport_context, ctx, grh_required);
307 rep->sys_image_guid = MLX5_GET64_PR(hca_vport_context, ctx,
308 system_image_guid);
309
310ex:
311 kfree(out);
312 return err;
313}
314EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_context);
315
316int mlx5_query_hca_vport_system_image_guid(struct mlx5_core_dev *dev,
7cf7fa52 317 u64 *sys_image_guid)
707c4602
MD
318{
319 struct mlx5_hca_vport_context *rep;
320 int err;
321
322 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
323 if (!rep)
324 return -ENOMEM;
325
326 err = mlx5_query_hca_vport_context(dev, 0, 1, 0, rep);
327 if (!err)
328 *sys_image_guid = rep->sys_image_guid;
329
330 kfree(rep);
331 return err;
332}
333EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_system_image_guid);
334
335int mlx5_query_hca_vport_node_guid(struct mlx5_core_dev *dev,
336 u64 *node_guid)
337{
338 struct mlx5_hca_vport_context *rep;
339 int err;
340
341 rep = kzalloc(sizeof(*rep), GFP_KERNEL);
342 if (!rep)
343 return -ENOMEM;
344
345 err = mlx5_query_hca_vport_context(dev, 0, 1, 0, rep);
346 if (!err)
347 *node_guid = rep->node_guid;
348
349 kfree(rep);
350 return err;
351}
352EXPORT_SYMBOL_GPL(mlx5_query_hca_vport_node_guid);
This page took 0.075778 seconds and 5 git commands to generate.