blob: 4e36e287d60567920866a2027a748f1c46fa110d [file] [log] [blame]
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -07001/*
2 * Copyright (c) 2007 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/errno.h>
34#include <linux/if_ether.h>
Eugenia Emantayevc59fec22013-04-11 01:56:42 +000035#include <linux/if_vlan.h>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040036#include <linux/export.h>
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070037
38#include <linux/mlx4/cmd.h>
39
40#include "mlx4.h"
Eran Ben Elishab4b6e842015-03-30 17:45:21 +030041#include "mlx4_stats.h"
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070042
43#define MLX4_MAC_VALID (1ull << 63)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070044
45#define MLX4_VLAN_VALID (1u << 31)
46#define MLX4_VLAN_MASK 0xfff
47
Muhammad Mahajna78500b82015-04-02 16:31:22 +030048#define MLX4_STATS_TRAFFIC_COUNTERS_MASK 0xfULL
49#define MLX4_STATS_TRAFFIC_DROPS_MASK 0xc0ULL
50#define MLX4_STATS_ERROR_COUNTERS_MASK 0x1ffc30ULL
51#define MLX4_STATS_PORT_COUNTERS_MASK 0x1fe00000ULL
52
Shaker Daibes40fb4fc2017-01-29 18:56:18 +020053#define MLX4_FLAG2_V_IGNORE_FCS_MASK BIT(1)
54#define MLX4_FLAG2_V_USER_MTU_MASK BIT(5)
Shaker Daibesbf1f9392017-01-29 18:56:19 +020055#define MLX4_FLAG_V_MTU_MASK BIT(0)
Shaker Daibes1f8176f2017-01-29 18:56:20 +020056#define MLX4_FLAG_V_PPRX_MASK BIT(1)
57#define MLX4_FLAG_V_PPTX_MASK BIT(2)
Muhammad Mahajna78500b82015-04-02 16:31:22 +030058#define MLX4_IGNORE_FCS_MASK 0x1
Tariq Toukan564ed9b2016-09-11 10:56:19 +030059#define MLX4_TC_MAX_NUMBER 8
Muhammad Mahajna78500b82015-04-02 16:31:22 +030060
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070061void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
62{
63 int i;
64
65 mutex_init(&table->mutex);
66 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
67 table->entries[i] = 0;
68 table->refs[i] = 0;
Moni Shoua5f613852015-12-06 18:07:41 +020069 table->is_dup[i] = false;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070070 }
71 table->max = 1 << dev->caps.log_num_macs;
72 table->total = 0;
73}
74
75void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
76{
77 int i;
78
79 mutex_init(&table->mutex);
80 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
81 table->entries[i] = 0;
82 table->refs[i] = 0;
Moni Shoua5f613852015-12-06 18:07:41 +020083 table->is_dup[i] = false;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070084 }
Yevgeny Petriline72ebf52011-10-18 01:50:29 +000085 table->max = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070086 table->total = 0;
87}
88
Jack Morgenstein111c6092014-05-27 09:26:38 +030089void mlx4_init_roce_gid_table(struct mlx4_dev *dev,
90 struct mlx4_roce_gid_table *table)
91{
92 int i;
93
94 mutex_init(&table->mutex);
95 for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++)
96 memset(table->roce_gids[i].raw, 0, MLX4_ROCE_GID_ENTRY_SIZE);
97}
98
Yevgeny Petrilin16792002011-03-22 22:38:31 +000099static int validate_index(struct mlx4_dev *dev,
100 struct mlx4_mac_table *table, int index)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700101{
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000102 int err = 0;
103
104 if (index < 0 || index >= table->max || !table->entries[index]) {
105 mlx4_warn(dev, "No valid Mac entry for the given index\n");
106 err = -EINVAL;
107 }
108 return err;
109}
110
111static int find_index(struct mlx4_dev *dev,
112 struct mlx4_mac_table *table, u64 mac)
113{
114 int i;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000115
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000116 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
Jack Morgensteinf4fd40b2014-09-11 14:11:16 +0300117 if (table->refs[i] &&
118 (MLX4_MAC_MASK & mac) ==
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000119 (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000120 return i;
121 }
122 /* Mac not found */
123 return -EINVAL;
124}
125
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000126static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
127 __be64 *entries)
128{
129 struct mlx4_cmd_mailbox *mailbox;
130 u32 in_mod;
131 int err;
132
133 mailbox = mlx4_alloc_cmd_mailbox(dev);
134 if (IS_ERR(mailbox))
135 return PTR_ERR(mailbox);
136
137 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
138
139 in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
140
Ido Shamaya130b592015-04-02 16:31:19 +0300141 err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
142 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
143 MLX4_CMD_NATIVE);
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000144
145 mlx4_free_cmd_mailbox(dev, mailbox);
146 return err;
147}
148
Moni Shoua297e0da2013-12-12 18:03:14 +0200149int mlx4_find_cached_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *idx)
150{
151 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
152 struct mlx4_mac_table *table = &info->mac_table;
153 int i;
154
155 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
156 if (!table->refs[i])
157 continue;
158
159 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
160 *idx = i;
161 return 0;
162 }
163 }
164
165 return -ENOENT;
166}
167EXPORT_SYMBOL_GPL(mlx4_find_cached_mac);
168
Moni Shoua5f613852015-12-06 18:07:41 +0200169static bool mlx4_need_mf_bond(struct mlx4_dev *dev)
170{
171 int i, num_eth_ports = 0;
172
173 if (!mlx4_is_mfunc(dev))
174 return false;
175 mlx4_foreach_port(i, dev, MLX4_PORT_TYPE_ETH)
176 ++num_eth_ports;
177
178 return (num_eth_ports == 2) ? true : false;
179}
180
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000181int __mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
182{
183 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
184 struct mlx4_mac_table *table = &info->mac_table;
185 int i, err = 0;
186 int free = -1;
Moni Shoua5f613852015-12-06 18:07:41 +0200187 int free_for_dup = -1;
188 bool dup = mlx4_is_mf_bonded(dev);
189 u8 dup_port = (port == 1) ? 2 : 1;
190 struct mlx4_mac_table *dup_table = &mlx4_priv(dev)->port[dup_port].mac_table;
191 bool need_mf_bond = mlx4_need_mf_bond(dev);
192 bool can_mf_bond = true;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000193
Moni Shoua5f613852015-12-06 18:07:41 +0200194 mlx4_dbg(dev, "Registering MAC: 0x%llx for port %d %s duplicate\n",
195 (unsigned long long)mac, port,
196 dup ? "with" : "without");
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000197
Moni Shoua5f613852015-12-06 18:07:41 +0200198 if (need_mf_bond) {
199 if (port == 1) {
200 mutex_lock(&table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200201 mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200202 } else {
203 mutex_lock(&dup_table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200204 mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200205 }
206 } else {
207 mutex_lock(&table->mutex);
208 }
209
210 if (need_mf_bond) {
211 int index_at_port = -1;
212 int index_at_dup_port = -1;
213
214 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
215 if (((MLX4_MAC_MASK & mac) == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))))
216 index_at_port = i;
217 if (((MLX4_MAC_MASK & mac) == (MLX4_MAC_MASK & be64_to_cpu(dup_table->entries[i]))))
218 index_at_dup_port = i;
219 }
220
221 /* check that same mac is not in the tables at different indices */
222 if ((index_at_port != index_at_dup_port) &&
223 (index_at_port >= 0) &&
224 (index_at_dup_port >= 0))
225 can_mf_bond = false;
226
227 /* If the mac is already in the primary table, the slot must be
228 * available in the duplicate table as well.
229 */
230 if (index_at_port >= 0 && index_at_dup_port < 0 &&
231 dup_table->refs[index_at_port]) {
232 can_mf_bond = false;
233 }
234 /* If the mac is already in the duplicate table, check that the
235 * corresponding index is not occupied in the primary table, or
236 * the primary table already contains the mac at the same index.
237 * Otherwise, you cannot bond (primary contains a different mac
238 * at that index).
239 */
240 if (index_at_dup_port >= 0) {
241 if (!table->refs[index_at_dup_port] ||
242 ((MLX4_MAC_MASK & mac) == (MLX4_MAC_MASK & be64_to_cpu(table->entries[index_at_dup_port]))))
243 free_for_dup = index_at_dup_port;
244 else
245 can_mf_bond = false;
246 }
247 }
248
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000249 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
Jack Morgensteinf4fd40b2014-09-11 14:11:16 +0300250 if (!table->refs[i]) {
251 if (free < 0)
252 free = i;
Moni Shoua5f613852015-12-06 18:07:41 +0200253 if (free_for_dup < 0 && need_mf_bond && can_mf_bond) {
254 if (!dup_table->refs[i])
255 free_for_dup = i;
256 }
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000257 continue;
258 }
259
Jack Morgensteinf4fd40b2014-09-11 14:11:16 +0300260 if ((MLX4_MAC_MASK & mac) ==
261 (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
Rony Efraim6ce71ac2013-04-25 05:22:25 +0000262 /* MAC already registered, increment ref count */
263 err = i;
264 ++table->refs[i];
Moni Shoua5f613852015-12-06 18:07:41 +0200265 if (dup) {
266 u64 dup_mac = MLX4_MAC_MASK & be64_to_cpu(dup_table->entries[i]);
267
268 if (dup_mac != mac || !dup_table->is_dup[i]) {
269 mlx4_warn(dev, "register mac: expect duplicate mac 0x%llx on port %d index %d\n",
270 mac, dup_port, i);
271 }
272 }
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000273 goto out;
274 }
275 }
276
Moni Shoua5f613852015-12-06 18:07:41 +0200277 if (need_mf_bond && (free_for_dup < 0)) {
278 if (dup) {
279 mlx4_warn(dev, "Fail to allocate duplicate MAC table entry\n");
280 mlx4_warn(dev, "High Availability for virtual functions may not work as expected\n");
281 dup = false;
282 }
283 can_mf_bond = false;
284 }
285
286 if (need_mf_bond && can_mf_bond)
287 free = free_for_dup;
288
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000289 mlx4_dbg(dev, "Free MAC index is %d\n", free);
290
291 if (table->total == table->max) {
292 /* No free mac entries */
293 err = -ENOSPC;
294 goto out;
295 }
296
297 /* Register new MAC */
298 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
299
300 err = mlx4_set_port_mac_table(dev, port, table->entries);
301 if (unlikely(err)) {
302 mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
303 (unsigned long long) mac);
304 table->entries[free] = 0;
305 goto out;
306 }
Rony Efraim6ce71ac2013-04-25 05:22:25 +0000307 table->refs[free] = 1;
Moni Shoua5f613852015-12-06 18:07:41 +0200308 table->is_dup[free] = false;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000309 ++table->total;
Moni Shoua5f613852015-12-06 18:07:41 +0200310 if (dup) {
311 dup_table->refs[free] = 0;
312 dup_table->is_dup[free] = true;
313 dup_table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
314
315 err = mlx4_set_port_mac_table(dev, dup_port, dup_table->entries);
316 if (unlikely(err)) {
317 mlx4_warn(dev, "Failed adding duplicate mac: 0x%llx\n", mac);
318 dup_table->is_dup[free] = false;
319 dup_table->entries[free] = 0;
320 goto out;
321 }
322 ++dup_table->total;
323 }
324 err = free;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000325out:
Moni Shoua5f613852015-12-06 18:07:41 +0200326 if (need_mf_bond) {
327 if (port == 2) {
328 mutex_unlock(&table->mutex);
329 mutex_unlock(&dup_table->mutex);
330 } else {
331 mutex_unlock(&dup_table->mutex);
332 mutex_unlock(&table->mutex);
333 }
334 } else {
335 mutex_unlock(&table->mutex);
336 }
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000337 return err;
338}
339EXPORT_SYMBOL_GPL(__mlx4_register_mac);
340
341int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac)
342{
Jack Morgensteine7dbeba2013-03-07 03:46:54 +0000343 u64 out_param = 0;
Jack Morgensteinacddd5d2013-11-03 10:03:18 +0200344 int err = -EINVAL;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000345
346 if (mlx4_is_mfunc(dev)) {
Jack Morgensteinacddd5d2013-11-03 10:03:18 +0200347 if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
348 err = mlx4_cmd_imm(dev, mac, &out_param,
349 ((u32) port) << 8 | (u32) RES_MAC,
350 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
351 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
352 }
353 if (err && err == -EINVAL && mlx4_is_slave(dev)) {
354 /* retry using old REG_MAC format */
355 set_param_l(&out_param, port);
356 err = mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
357 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
358 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
359 if (!err)
360 dev->flags |= MLX4_FLAG_OLD_REG_MAC;
361 }
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000362 if (err)
363 return err;
364
365 return get_param_l(&out_param);
366 }
367 return __mlx4_register_mac(dev, port, mac);
368}
369EXPORT_SYMBOL_GPL(mlx4_register_mac);
370
Yan Burman16a10ff2013-02-07 02:25:22 +0000371int mlx4_get_base_qpn(struct mlx4_dev *dev, u8 port)
372{
373 return dev->caps.reserved_qps_base[MLX4_QP_REGION_ETH_ADDR] +
374 (port - 1) * (1 << dev->caps.log_num_macs);
375}
376EXPORT_SYMBOL_GPL(mlx4_get_base_qpn);
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000377
378void __mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
379{
Eugenia Emantayev143b3efb2014-07-08 11:25:21 +0300380 struct mlx4_port_info *info;
381 struct mlx4_mac_table *table;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000382 int index;
Moni Shoua5f613852015-12-06 18:07:41 +0200383 bool dup = mlx4_is_mf_bonded(dev);
384 u8 dup_port = (port == 1) ? 2 : 1;
385 struct mlx4_mac_table *dup_table = &mlx4_priv(dev)->port[dup_port].mac_table;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000386
Eugenia Emantayev143b3efb2014-07-08 11:25:21 +0300387 if (port < 1 || port > dev->caps.num_ports) {
388 mlx4_warn(dev, "invalid port number (%d), aborting...\n", port);
389 return;
390 }
391 info = &mlx4_priv(dev)->port[port];
392 table = &info->mac_table;
Moni Shoua5f613852015-12-06 18:07:41 +0200393
394 if (dup) {
395 if (port == 1) {
396 mutex_lock(&table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200397 mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200398 } else {
399 mutex_lock(&dup_table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200400 mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200401 }
402 } else {
403 mutex_lock(&table->mutex);
404 }
405
Rony Efraim6ce71ac2013-04-25 05:22:25 +0000406 index = find_index(dev, table, mac);
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000407
408 if (validate_index(dev, table, index))
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700409 goto out;
Moni Shoua5f613852015-12-06 18:07:41 +0200410
411 if (--table->refs[index] || table->is_dup[index]) {
Joe Perches1a91de22014-05-07 12:52:57 -0700412 mlx4_dbg(dev, "Have more references for index %d, no need to modify mac table\n",
413 index);
Moni Shoua5f613852015-12-06 18:07:41 +0200414 if (!table->refs[index])
415 dup_table->is_dup[index] = false;
Rony Efraim6ce71ac2013-04-25 05:22:25 +0000416 goto out;
417 }
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000418
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000419 table->entries[index] = 0;
Moni Shoua5f613852015-12-06 18:07:41 +0200420 if (mlx4_set_port_mac_table(dev, port, table->entries))
421 mlx4_warn(dev, "Fail to set mac in port %d during unregister\n", port);
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000422 --table->total;
Moni Shoua5f613852015-12-06 18:07:41 +0200423
424 if (dup) {
425 dup_table->is_dup[index] = false;
426 if (dup_table->refs[index])
427 goto out;
428 dup_table->entries[index] = 0;
429 if (mlx4_set_port_mac_table(dev, dup_port, dup_table->entries))
430 mlx4_warn(dev, "Fail to set mac in duplicate port %d during unregister\n", dup_port);
431
432 --table->total;
433 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700434out:
Moni Shoua5f613852015-12-06 18:07:41 +0200435 if (dup) {
436 if (port == 2) {
437 mutex_unlock(&table->mutex);
438 mutex_unlock(&dup_table->mutex);
439 } else {
440 mutex_unlock(&dup_table->mutex);
441 mutex_unlock(&table->mutex);
442 }
443 } else {
444 mutex_unlock(&table->mutex);
445 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700446}
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000447EXPORT_SYMBOL_GPL(__mlx4_unregister_mac);
448
449void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, u64 mac)
450{
Jack Morgensteine7dbeba2013-03-07 03:46:54 +0000451 u64 out_param = 0;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000452
453 if (mlx4_is_mfunc(dev)) {
Jack Morgensteinacddd5d2013-11-03 10:03:18 +0200454 if (!(dev->flags & MLX4_FLAG_OLD_REG_MAC)) {
455 (void) mlx4_cmd_imm(dev, mac, &out_param,
456 ((u32) port) << 8 | (u32) RES_MAC,
457 RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
458 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
459 } else {
460 /* use old unregister mac format */
461 set_param_l(&out_param, port);
462 (void) mlx4_cmd_imm(dev, mac, &out_param, RES_MAC,
463 RES_OP_RESERVE_AND_MAP, MLX4_CMD_FREE_RES,
464 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
465 }
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000466 return;
467 }
468 __mlx4_unregister_mac(dev, port, mac);
469 return;
470}
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700471EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
472
Yan Burman16a10ff2013-02-07 02:25:22 +0000473int __mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac)
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000474{
475 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
476 struct mlx4_mac_table *table = &info->mac_table;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000477 int index = qpn - info->base_qpn;
478 int err = 0;
Moni Shoua5f613852015-12-06 18:07:41 +0200479 bool dup = mlx4_is_mf_bonded(dev);
480 u8 dup_port = (port == 1) ? 2 : 1;
481 struct mlx4_mac_table *dup_table = &mlx4_priv(dev)->port[dup_port].mac_table;
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000482
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000483 /* CX1 doesn't support multi-functions */
Moni Shoua5f613852015-12-06 18:07:41 +0200484 if (dup) {
485 if (port == 1) {
486 mutex_lock(&table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200487 mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200488 } else {
489 mutex_lock(&dup_table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200490 mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200491 }
492 } else {
493 mutex_lock(&table->mutex);
494 }
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000495
496 err = validate_index(dev, table, index);
497 if (err)
498 goto out;
499
500 table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
501
502 err = mlx4_set_port_mac_table(dev, port, table->entries);
503 if (unlikely(err)) {
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000504 mlx4_err(dev, "Failed adding MAC: 0x%llx\n",
505 (unsigned long long) new_mac);
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000506 table->entries[index] = 0;
Moni Shoua5f613852015-12-06 18:07:41 +0200507 } else {
508 if (dup) {
509 dup_table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
510
511 err = mlx4_set_port_mac_table(dev, dup_port, dup_table->entries);
512 if (unlikely(err)) {
513 mlx4_err(dev, "Failed adding duplicate MAC: 0x%llx\n",
514 (unsigned long long)new_mac);
515 dup_table->entries[index] = 0;
516 }
517 }
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000518 }
519out:
Moni Shoua5f613852015-12-06 18:07:41 +0200520 if (dup) {
521 if (port == 2) {
522 mutex_unlock(&table->mutex);
523 mutex_unlock(&dup_table->mutex);
524 } else {
525 mutex_unlock(&dup_table->mutex);
526 mutex_unlock(&table->mutex);
527 }
528 } else {
529 mutex_unlock(&table->mutex);
530 }
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000531 return err;
532}
Yan Burman16a10ff2013-02-07 02:25:22 +0000533EXPORT_SYMBOL_GPL(__mlx4_replace_mac);
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000534
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700535static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
536 __be32 *entries)
537{
538 struct mlx4_cmd_mailbox *mailbox;
539 u32 in_mod;
540 int err;
541
542 mailbox = mlx4_alloc_cmd_mailbox(dev);
543 if (IS_ERR(mailbox))
544 return PTR_ERR(mailbox);
545
546 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
547 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
Ido Shamaya130b592015-04-02 16:31:19 +0300548 err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
549 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
550 MLX4_CMD_NATIVE);
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700551
552 mlx4_free_cmd_mailbox(dev, mailbox);
553
554 return err;
555}
556
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300557int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
558{
559 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
560 int i;
561
562 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
563 if (table->refs[i] &&
564 (vid == (MLX4_VLAN_MASK &
565 be32_to_cpu(table->entries[i])))) {
566 /* VLAN already registered, increase reference count */
567 *idx = i;
568 return 0;
569 }
570 }
571
572 return -ENOENT;
573}
574EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
575
Rony Efraim3f7fb022013-04-25 05:22:28 +0000576int __mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan,
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000577 int *index)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700578{
579 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
580 int i, err = 0;
581 int free = -1;
Moni Shoua5f613852015-12-06 18:07:41 +0200582 int free_for_dup = -1;
583 bool dup = mlx4_is_mf_bonded(dev);
584 u8 dup_port = (port == 1) ? 2 : 1;
585 struct mlx4_vlan_table *dup_table = &mlx4_priv(dev)->port[dup_port].vlan_table;
586 bool need_mf_bond = mlx4_need_mf_bond(dev);
587 bool can_mf_bond = true;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700588
Moni Shoua5f613852015-12-06 18:07:41 +0200589 mlx4_dbg(dev, "Registering VLAN: %d for port %d %s duplicate\n",
590 vlan, port,
591 dup ? "with" : "without");
592
593 if (need_mf_bond) {
594 if (port == 1) {
595 mutex_lock(&table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200596 mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200597 } else {
598 mutex_lock(&dup_table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200599 mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200600 }
601 } else {
602 mutex_lock(&table->mutex);
603 }
Yevgeny Petriline72ebf52011-10-18 01:50:29 +0000604
605 if (table->total == table->max) {
606 /* No free vlan entries */
607 err = -ENOSPC;
608 goto out;
609 }
610
Moni Shoua5f613852015-12-06 18:07:41 +0200611 if (need_mf_bond) {
612 int index_at_port = -1;
613 int index_at_dup_port = -1;
614
615 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
616 if ((vlan == (MLX4_VLAN_MASK & be32_to_cpu(table->entries[i]))))
617 index_at_port = i;
618 if ((vlan == (MLX4_VLAN_MASK & be32_to_cpu(dup_table->entries[i]))))
619 index_at_dup_port = i;
620 }
621 /* check that same vlan is not in the tables at different indices */
622 if ((index_at_port != index_at_dup_port) &&
623 (index_at_port >= 0) &&
624 (index_at_dup_port >= 0))
625 can_mf_bond = false;
626
627 /* If the vlan is already in the primary table, the slot must be
628 * available in the duplicate table as well.
629 */
630 if (index_at_port >= 0 && index_at_dup_port < 0 &&
631 dup_table->refs[index_at_port]) {
632 can_mf_bond = false;
633 }
634 /* If the vlan is already in the duplicate table, check that the
635 * corresponding index is not occupied in the primary table, or
636 * the primary table already contains the vlan at the same index.
637 * Otherwise, you cannot bond (primary contains a different vlan
638 * at that index).
639 */
640 if (index_at_dup_port >= 0) {
641 if (!table->refs[index_at_dup_port] ||
642 (vlan == (MLX4_VLAN_MASK & be32_to_cpu(dup_table->entries[index_at_dup_port]))))
643 free_for_dup = index_at_dup_port;
644 else
645 can_mf_bond = false;
646 }
647 }
648
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700649 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
Moni Shoua5f613852015-12-06 18:07:41 +0200650 if (!table->refs[i]) {
651 if (free < 0)
652 free = i;
653 if (free_for_dup < 0 && need_mf_bond && can_mf_bond) {
654 if (!dup_table->refs[i])
655 free_for_dup = i;
656 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700657 }
658
Moni Shoua5f613852015-12-06 18:07:41 +0200659 if ((table->refs[i] || table->is_dup[i]) &&
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700660 (vlan == (MLX4_VLAN_MASK &
661 be32_to_cpu(table->entries[i])))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300662 /* Vlan already registered, increase references count */
Moni Shoua5f613852015-12-06 18:07:41 +0200663 mlx4_dbg(dev, "vlan %u is already registered.\n", vlan);
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700664 *index = i;
665 ++table->refs[i];
Moni Shoua5f613852015-12-06 18:07:41 +0200666 if (dup) {
667 u16 dup_vlan = MLX4_VLAN_MASK & be32_to_cpu(dup_table->entries[i]);
668
669 if (dup_vlan != vlan || !dup_table->is_dup[i]) {
670 mlx4_warn(dev, "register vlan: expected duplicate vlan %u on port %d index %d\n",
671 vlan, dup_port, i);
672 }
673 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700674 goto out;
675 }
676 }
677
Moni Shoua5f613852015-12-06 18:07:41 +0200678 if (need_mf_bond && (free_for_dup < 0)) {
679 if (dup) {
680 mlx4_warn(dev, "Fail to allocate duplicate VLAN table entry\n");
681 mlx4_warn(dev, "High Availability for virtual functions may not work as expected\n");
682 dup = false;
683 }
684 can_mf_bond = false;
685 }
686
687 if (need_mf_bond && can_mf_bond)
688 free = free_for_dup;
689
Eli Cohen0926f912010-10-25 02:56:47 +0000690 if (free < 0) {
691 err = -ENOMEM;
692 goto out;
693 }
694
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000695 /* Register new VLAN */
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700696 table->refs[free] = 1;
Moni Shoua5f613852015-12-06 18:07:41 +0200697 table->is_dup[free] = false;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700698 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
699
700 err = mlx4_set_port_vlan_table(dev, port, table->entries);
701 if (unlikely(err)) {
702 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
703 table->refs[free] = 0;
704 table->entries[free] = 0;
705 goto out;
706 }
Moni Shoua5f613852015-12-06 18:07:41 +0200707 ++table->total;
708 if (dup) {
709 dup_table->refs[free] = 0;
710 dup_table->is_dup[free] = true;
711 dup_table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
712
713 err = mlx4_set_port_vlan_table(dev, dup_port, dup_table->entries);
714 if (unlikely(err)) {
715 mlx4_warn(dev, "Failed adding duplicate vlan: %u\n", vlan);
716 dup_table->is_dup[free] = false;
717 dup_table->entries[free] = 0;
718 goto out;
719 }
720 ++dup_table->total;
721 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700722
723 *index = free;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700724out:
Moni Shoua5f613852015-12-06 18:07:41 +0200725 if (need_mf_bond) {
726 if (port == 2) {
727 mutex_unlock(&table->mutex);
728 mutex_unlock(&dup_table->mutex);
729 } else {
730 mutex_unlock(&dup_table->mutex);
731 mutex_unlock(&table->mutex);
732 }
733 } else {
734 mutex_unlock(&table->mutex);
735 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700736 return err;
737}
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000738
739int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
740{
Jack Morgensteine7dbeba2013-03-07 03:46:54 +0000741 u64 out_param = 0;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000742 int err;
743
Jack Morgenstein162226a2013-11-03 10:03:17 +0200744 if (vlan > 4095)
745 return -EINVAL;
746
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000747 if (mlx4_is_mfunc(dev)) {
Jack Morgensteinacddd5d2013-11-03 10:03:18 +0200748 err = mlx4_cmd_imm(dev, vlan, &out_param,
749 ((u32) port) << 8 | (u32) RES_VLAN,
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000750 RES_OP_RESERVE_AND_MAP, MLX4_CMD_ALLOC_RES,
751 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
752 if (!err)
753 *index = get_param_l(&out_param);
754
755 return err;
756 }
757 return __mlx4_register_vlan(dev, port, vlan, index);
758}
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700759EXPORT_SYMBOL_GPL(mlx4_register_vlan);
760
Jack Morgenstein2009d002013-11-03 10:03:19 +0200761void __mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700762{
763 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
Jack Morgenstein2009d002013-11-03 10:03:19 +0200764 int index;
Moni Shoua5f613852015-12-06 18:07:41 +0200765 bool dup = mlx4_is_mf_bonded(dev);
766 u8 dup_port = (port == 1) ? 2 : 1;
767 struct mlx4_vlan_table *dup_table = &mlx4_priv(dev)->port[dup_port].vlan_table;
Jack Morgenstein2009d002013-11-03 10:03:19 +0200768
Moni Shoua5f613852015-12-06 18:07:41 +0200769 if (dup) {
770 if (port == 1) {
771 mutex_lock(&table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200772 mutex_lock_nested(&dup_table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200773 } else {
774 mutex_lock(&dup_table->mutex);
Jack Morgenstein03a79f32016-03-02 17:47:44 +0200775 mutex_lock_nested(&table->mutex, SINGLE_DEPTH_NESTING);
Moni Shoua5f613852015-12-06 18:07:41 +0200776 }
777 } else {
778 mutex_lock(&table->mutex);
779 }
780
Jack Morgenstein2009d002013-11-03 10:03:19 +0200781 if (mlx4_find_cached_vlan(dev, port, vlan, &index)) {
782 mlx4_warn(dev, "vlan 0x%x is not in the vlan table\n", vlan);
783 goto out;
784 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700785
786 if (index < MLX4_VLAN_REGULAR) {
787 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700788 goto out;
789 }
Jack Morgenstein2009d002013-11-03 10:03:19 +0200790
Moni Shoua5f613852015-12-06 18:07:41 +0200791 if (--table->refs[index] || table->is_dup[index]) {
Joe Perches1a91de22014-05-07 12:52:57 -0700792 mlx4_dbg(dev, "Have %d more references for index %d, no need to modify vlan table\n",
793 table->refs[index], index);
Moni Shoua5f613852015-12-06 18:07:41 +0200794 if (!table->refs[index])
795 dup_table->is_dup[index] = false;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700796 goto out;
797 }
798 table->entries[index] = 0;
Moni Shoua5f613852015-12-06 18:07:41 +0200799 if (mlx4_set_port_vlan_table(dev, port, table->entries))
800 mlx4_warn(dev, "Fail to set vlan in port %d during unregister\n", port);
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700801 --table->total;
Moni Shoua5f613852015-12-06 18:07:41 +0200802 if (dup) {
803 dup_table->is_dup[index] = false;
804 if (dup_table->refs[index])
805 goto out;
806 dup_table->entries[index] = 0;
807 if (mlx4_set_port_vlan_table(dev, dup_port, dup_table->entries))
808 mlx4_warn(dev, "Fail to set vlan in duplicate port %d during unregister\n", dup_port);
809 --dup_table->total;
810 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700811out:
Moni Shoua5f613852015-12-06 18:07:41 +0200812 if (dup) {
813 if (port == 2) {
814 mutex_unlock(&table->mutex);
815 mutex_unlock(&dup_table->mutex);
816 } else {
817 mutex_unlock(&dup_table->mutex);
818 mutex_unlock(&table->mutex);
819 }
820 } else {
821 mutex_unlock(&table->mutex);
822 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700823}
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000824
Jack Morgenstein2009d002013-11-03 10:03:19 +0200825void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, u16 vlan)
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000826{
Jack Morgenstein162226a2013-11-03 10:03:17 +0200827 u64 out_param = 0;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000828
829 if (mlx4_is_mfunc(dev)) {
Jack Morgenstein2009d002013-11-03 10:03:19 +0200830 (void) mlx4_cmd_imm(dev, vlan, &out_param,
Jack Morgensteinacddd5d2013-11-03 10:03:18 +0200831 ((u32) port) << 8 | (u32) RES_VLAN,
Jack Morgenstein162226a2013-11-03 10:03:17 +0200832 RES_OP_RESERVE_AND_MAP,
833 MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
834 MLX4_CMD_WRAPPED);
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000835 return;
836 }
Jack Morgenstein2009d002013-11-03 10:03:19 +0200837 __mlx4_unregister_vlan(dev, port, vlan);
Eugenia Emantayevffe455a2011-12-13 04:16:21 +0000838}
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700839EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700840
Moni Shoua5f613852015-12-06 18:07:41 +0200841int mlx4_bond_mac_table(struct mlx4_dev *dev)
842{
843 struct mlx4_mac_table *t1 = &mlx4_priv(dev)->port[1].mac_table;
844 struct mlx4_mac_table *t2 = &mlx4_priv(dev)->port[2].mac_table;
845 int ret = 0;
846 int i;
847 bool update1 = false;
848 bool update2 = false;
849
850 mutex_lock(&t1->mutex);
851 mutex_lock(&t2->mutex);
852 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
853 if ((t1->entries[i] != t2->entries[i]) &&
854 t1->entries[i] && t2->entries[i]) {
855 mlx4_warn(dev, "can't duplicate entry %d in mac table\n", i);
856 ret = -EINVAL;
857 goto unlock;
858 }
859 }
860
861 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
862 if (t1->entries[i] && !t2->entries[i]) {
863 t2->entries[i] = t1->entries[i];
864 t2->is_dup[i] = true;
865 update2 = true;
866 } else if (!t1->entries[i] && t2->entries[i]) {
867 t1->entries[i] = t2->entries[i];
868 t1->is_dup[i] = true;
869 update1 = true;
870 } else if (t1->entries[i] && t2->entries[i]) {
871 t1->is_dup[i] = true;
872 t2->is_dup[i] = true;
873 }
874 }
875
876 if (update1) {
877 ret = mlx4_set_port_mac_table(dev, 1, t1->entries);
878 if (ret)
879 mlx4_warn(dev, "failed to set MAC table for port 1 (%d)\n", ret);
880 }
881 if (!ret && update2) {
882 ret = mlx4_set_port_mac_table(dev, 2, t2->entries);
883 if (ret)
884 mlx4_warn(dev, "failed to set MAC table for port 2 (%d)\n", ret);
885 }
886
887 if (ret)
888 mlx4_warn(dev, "failed to create mirror MAC tables\n");
889unlock:
890 mutex_unlock(&t2->mutex);
891 mutex_unlock(&t1->mutex);
892 return ret;
893}
894
895int mlx4_unbond_mac_table(struct mlx4_dev *dev)
896{
897 struct mlx4_mac_table *t1 = &mlx4_priv(dev)->port[1].mac_table;
898 struct mlx4_mac_table *t2 = &mlx4_priv(dev)->port[2].mac_table;
899 int ret = 0;
900 int ret1;
901 int i;
902 bool update1 = false;
903 bool update2 = false;
904
905 mutex_lock(&t1->mutex);
906 mutex_lock(&t2->mutex);
907 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
908 if (t1->entries[i] != t2->entries[i]) {
909 mlx4_warn(dev, "mac table is in an unexpected state when trying to unbond\n");
910 ret = -EINVAL;
911 goto unlock;
912 }
913 }
914
915 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
916 if (!t1->entries[i])
917 continue;
918 t1->is_dup[i] = false;
919 if (!t1->refs[i]) {
920 t1->entries[i] = 0;
921 update1 = true;
922 }
923 t2->is_dup[i] = false;
924 if (!t2->refs[i]) {
925 t2->entries[i] = 0;
926 update2 = true;
927 }
928 }
929
930 if (update1) {
931 ret = mlx4_set_port_mac_table(dev, 1, t1->entries);
932 if (ret)
933 mlx4_warn(dev, "failed to unmirror MAC tables for port 1(%d)\n", ret);
934 }
935 if (update2) {
936 ret1 = mlx4_set_port_mac_table(dev, 2, t2->entries);
937 if (ret1) {
938 mlx4_warn(dev, "failed to unmirror MAC tables for port 2(%d)\n", ret1);
939 ret = ret1;
940 }
941 }
942unlock:
943 mutex_unlock(&t2->mutex);
944 mutex_unlock(&t1->mutex);
945 return ret;
946}
947
948int mlx4_bond_vlan_table(struct mlx4_dev *dev)
949{
950 struct mlx4_vlan_table *t1 = &mlx4_priv(dev)->port[1].vlan_table;
951 struct mlx4_vlan_table *t2 = &mlx4_priv(dev)->port[2].vlan_table;
952 int ret = 0;
953 int i;
954 bool update1 = false;
955 bool update2 = false;
956
957 mutex_lock(&t1->mutex);
958 mutex_lock(&t2->mutex);
959 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
960 if ((t1->entries[i] != t2->entries[i]) &&
961 t1->entries[i] && t2->entries[i]) {
962 mlx4_warn(dev, "can't duplicate entry %d in vlan table\n", i);
963 ret = -EINVAL;
964 goto unlock;
965 }
966 }
967
968 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
969 if (t1->entries[i] && !t2->entries[i]) {
970 t2->entries[i] = t1->entries[i];
971 t2->is_dup[i] = true;
972 update2 = true;
973 } else if (!t1->entries[i] && t2->entries[i]) {
974 t1->entries[i] = t2->entries[i];
975 t1->is_dup[i] = true;
976 update1 = true;
977 } else if (t1->entries[i] && t2->entries[i]) {
978 t1->is_dup[i] = true;
979 t2->is_dup[i] = true;
980 }
981 }
982
983 if (update1) {
984 ret = mlx4_set_port_vlan_table(dev, 1, t1->entries);
985 if (ret)
986 mlx4_warn(dev, "failed to set VLAN table for port 1 (%d)\n", ret);
987 }
988 if (!ret && update2) {
989 ret = mlx4_set_port_vlan_table(dev, 2, t2->entries);
990 if (ret)
991 mlx4_warn(dev, "failed to set VLAN table for port 2 (%d)\n", ret);
992 }
993
994 if (ret)
995 mlx4_warn(dev, "failed to create mirror VLAN tables\n");
996unlock:
997 mutex_unlock(&t2->mutex);
998 mutex_unlock(&t1->mutex);
999 return ret;
1000}
1001
1002int mlx4_unbond_vlan_table(struct mlx4_dev *dev)
1003{
1004 struct mlx4_vlan_table *t1 = &mlx4_priv(dev)->port[1].vlan_table;
1005 struct mlx4_vlan_table *t2 = &mlx4_priv(dev)->port[2].vlan_table;
1006 int ret = 0;
1007 int ret1;
1008 int i;
1009 bool update1 = false;
1010 bool update2 = false;
1011
1012 mutex_lock(&t1->mutex);
1013 mutex_lock(&t2->mutex);
1014 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
1015 if (t1->entries[i] != t2->entries[i]) {
1016 mlx4_warn(dev, "vlan table is in an unexpected state when trying to unbond\n");
1017 ret = -EINVAL;
1018 goto unlock;
1019 }
1020 }
1021
1022 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
1023 if (!t1->entries[i])
1024 continue;
1025 t1->is_dup[i] = false;
1026 if (!t1->refs[i]) {
1027 t1->entries[i] = 0;
1028 update1 = true;
1029 }
1030 t2->is_dup[i] = false;
1031 if (!t2->refs[i]) {
1032 t2->entries[i] = 0;
1033 update2 = true;
1034 }
1035 }
1036
1037 if (update1) {
1038 ret = mlx4_set_port_vlan_table(dev, 1, t1->entries);
1039 if (ret)
1040 mlx4_warn(dev, "failed to unmirror VLAN tables for port 1(%d)\n", ret);
1041 }
1042 if (update2) {
1043 ret1 = mlx4_set_port_vlan_table(dev, 2, t2->entries);
1044 if (ret1) {
1045 mlx4_warn(dev, "failed to unmirror VLAN tables for port 2(%d)\n", ret1);
1046 ret = ret1;
1047 }
1048 }
1049unlock:
1050 mutex_unlock(&t2->mutex);
1051 mutex_unlock(&t1->mutex);
1052 return ret;
1053}
1054
Jack Morgenstein9a5aa622008-11-28 21:29:46 -08001055int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
1056{
1057 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
1058 u8 *inbuf, *outbuf;
1059 int err;
1060
1061 inmailbox = mlx4_alloc_cmd_mailbox(dev);
1062 if (IS_ERR(inmailbox))
1063 return PTR_ERR(inmailbox);
1064
1065 outmailbox = mlx4_alloc_cmd_mailbox(dev);
1066 if (IS_ERR(outmailbox)) {
1067 mlx4_free_cmd_mailbox(dev, inmailbox);
1068 return PTR_ERR(outmailbox);
1069 }
1070
1071 inbuf = inmailbox->buf;
1072 outbuf = outmailbox->buf;
Jack Morgenstein9a5aa622008-11-28 21:29:46 -08001073 inbuf[0] = 1;
1074 inbuf[1] = 1;
1075 inbuf[2] = 1;
1076 inbuf[3] = 1;
1077 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
1078 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
1079
1080 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
Jack Morgensteinf9baff52011-12-13 04:10:51 +00001081 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
1082 MLX4_CMD_NATIVE);
Jack Morgenstein9a5aa622008-11-28 21:29:46 -08001083 if (!err)
1084 *caps = *(__be32 *) (outbuf + 84);
1085 mlx4_free_cmd_mailbox(dev, inmailbox);
1086 mlx4_free_cmd_mailbox(dev, outmailbox);
1087 return err;
1088}
Jack Morgenstein9cd59352014-03-12 12:00:38 +02001089static struct mlx4_roce_gid_entry zgid_entry;
Jack Morgenstein9a5aa622008-11-28 21:29:46 -08001090
Matan Barak449fc482014-03-19 18:11:52 +02001091int mlx4_get_slave_num_gids(struct mlx4_dev *dev, int slave, int port)
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001092{
Matan Barak449fc482014-03-19 18:11:52 +02001093 int vfs;
1094 int slave_gid = slave;
1095 unsigned i;
1096 struct mlx4_slaves_pport slaves_pport;
1097 struct mlx4_active_ports actv_ports;
1098 unsigned max_port_p_one;
1099
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001100 if (slave == 0)
1101 return MLX4_ROCE_PF_GIDS;
Matan Barak449fc482014-03-19 18:11:52 +02001102
1103 /* Slave is a VF */
1104 slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
1105 actv_ports = mlx4_get_active_ports(dev, slave);
1106 max_port_p_one = find_first_bit(actv_ports.ports, dev->caps.num_ports) +
1107 bitmap_weight(actv_ports.ports, dev->caps.num_ports) + 1;
1108
1109 for (i = 1; i < max_port_p_one; i++) {
1110 struct mlx4_active_ports exclusive_ports;
1111 struct mlx4_slaves_pport slaves_pport_actv;
1112 bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
1113 set_bit(i - 1, exclusive_ports.ports);
1114 if (i == port)
1115 continue;
1116 slaves_pport_actv = mlx4_phys_to_slaves_pport_actv(
1117 dev, &exclusive_ports);
1118 slave_gid -= bitmap_weight(slaves_pport_actv.slaves,
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001119 dev->persist->num_vfs + 1);
Matan Barak449fc482014-03-19 18:11:52 +02001120 }
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001121 vfs = bitmap_weight(slaves_pport.slaves, dev->persist->num_vfs + 1) - 1;
Matan Barak449fc482014-03-19 18:11:52 +02001122 if (slave_gid <= ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) % vfs))
1123 return ((MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / vfs) + 1;
1124 return (MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS) / vfs;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001125}
1126
Matan Barak449fc482014-03-19 18:11:52 +02001127int mlx4_get_base_gid_ix(struct mlx4_dev *dev, int slave, int port)
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001128{
1129 int gids;
Matan Barak449fc482014-03-19 18:11:52 +02001130 unsigned i;
1131 int slave_gid = slave;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001132 int vfs;
1133
Matan Barak449fc482014-03-19 18:11:52 +02001134 struct mlx4_slaves_pport slaves_pport;
1135 struct mlx4_active_ports actv_ports;
1136 unsigned max_port_p_one;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001137
1138 if (slave == 0)
1139 return 0;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001140
Matan Barak449fc482014-03-19 18:11:52 +02001141 slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
1142 actv_ports = mlx4_get_active_ports(dev, slave);
1143 max_port_p_one = find_first_bit(actv_ports.ports, dev->caps.num_ports) +
1144 bitmap_weight(actv_ports.ports, dev->caps.num_ports) + 1;
1145
1146 for (i = 1; i < max_port_p_one; i++) {
1147 struct mlx4_active_ports exclusive_ports;
1148 struct mlx4_slaves_pport slaves_pport_actv;
1149 bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
1150 set_bit(i - 1, exclusive_ports.ports);
1151 if (i == port)
1152 continue;
1153 slaves_pport_actv = mlx4_phys_to_slaves_pport_actv(
1154 dev, &exclusive_ports);
1155 slave_gid -= bitmap_weight(slaves_pport_actv.slaves,
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001156 dev->persist->num_vfs + 1);
Matan Barak449fc482014-03-19 18:11:52 +02001157 }
1158 gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001159 vfs = bitmap_weight(slaves_pport.slaves, dev->persist->num_vfs + 1) - 1;
Matan Barak449fc482014-03-19 18:11:52 +02001160 if (slave_gid <= gids % vfs)
1161 return MLX4_ROCE_PF_GIDS + ((gids / vfs) + 1) * (slave_gid - 1);
1162
1163 return MLX4_ROCE_PF_GIDS + (gids % vfs) +
1164 ((gids / vfs) * (slave_gid - 1));
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001165}
Matan Barak449fc482014-03-19 18:11:52 +02001166EXPORT_SYMBOL_GPL(mlx4_get_base_gid_ix);
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001167
Jack Morgenstein111c6092014-05-27 09:26:38 +03001168static int mlx4_reset_roce_port_gids(struct mlx4_dev *dev, int slave,
1169 int port, struct mlx4_cmd_mailbox *mailbox)
1170{
1171 struct mlx4_roce_gid_entry *gid_entry_mbox;
1172 struct mlx4_priv *priv = mlx4_priv(dev);
1173 int num_gids, base, offset;
1174 int i, err;
1175
1176 num_gids = mlx4_get_slave_num_gids(dev, slave, port);
1177 base = mlx4_get_base_gid_ix(dev, slave, port);
1178
1179 memset(mailbox->buf, 0, MLX4_MAILBOX_SIZE);
1180
1181 mutex_lock(&(priv->port[port].gid_table.mutex));
1182 /* Zero-out gids belonging to that slave in the port GID table */
1183 for (i = 0, offset = base; i < num_gids; offset++, i++)
1184 memcpy(priv->port[port].gid_table.roce_gids[offset].raw,
1185 zgid_entry.raw, MLX4_ROCE_GID_ENTRY_SIZE);
1186
1187 /* Now, copy roce port gids table to mailbox for passing to FW */
1188 gid_entry_mbox = (struct mlx4_roce_gid_entry *)mailbox->buf;
1189 for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
1190 memcpy(gid_entry_mbox->raw,
1191 priv->port[port].gid_table.roce_gids[i].raw,
1192 MLX4_ROCE_GID_ENTRY_SIZE);
1193
1194 err = mlx4_cmd(dev, mailbox->dma,
Ido Shamaya130b592015-04-02 16:31:19 +03001195 ((u32)port) | (MLX4_SET_PORT_GID_TABLE << 8),
1196 MLX4_SET_PORT_ETH_OPCODE, MLX4_CMD_SET_PORT,
1197 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
Jack Morgenstein111c6092014-05-27 09:26:38 +03001198 mutex_unlock(&(priv->port[port].gid_table.mutex));
1199 return err;
1200}
1201
1202
1203void mlx4_reset_roce_gids(struct mlx4_dev *dev, int slave)
1204{
1205 struct mlx4_active_ports actv_ports;
1206 struct mlx4_cmd_mailbox *mailbox;
1207 int num_eth_ports, err;
1208 int i;
1209
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001210 if (slave < 0 || slave > dev->persist->num_vfs)
Jack Morgenstein111c6092014-05-27 09:26:38 +03001211 return;
1212
1213 actv_ports = mlx4_get_active_ports(dev, slave);
1214
1215 for (i = 0, num_eth_ports = 0; i < dev->caps.num_ports; i++) {
1216 if (test_bit(i, actv_ports.ports)) {
1217 if (dev->caps.port_type[i + 1] != MLX4_PORT_TYPE_ETH)
1218 continue;
1219 num_eth_ports++;
1220 }
1221 }
1222
1223 if (!num_eth_ports)
1224 return;
1225
1226 /* have ETH ports. Alloc mailbox for SET_PORT command */
1227 mailbox = mlx4_alloc_cmd_mailbox(dev);
1228 if (IS_ERR(mailbox))
1229 return;
1230
1231 for (i = 0; i < dev->caps.num_ports; i++) {
1232 if (test_bit(i, actv_ports.ports)) {
1233 if (dev->caps.port_type[i + 1] != MLX4_PORT_TYPE_ETH)
1234 continue;
1235 err = mlx4_reset_roce_port_gids(dev, slave, i + 1, mailbox);
1236 if (err)
1237 mlx4_warn(dev, "Could not reset ETH port GID table for slave %d, port %d (%d)\n",
1238 slave, i + 1, err);
1239 }
1240 }
1241
1242 mlx4_free_cmd_mailbox(dev, mailbox);
1243 return;
1244}
1245
Shaker Daibes40fb4fc2017-01-29 18:56:18 +02001246static void
Shaker Daibesbf1f9392017-01-29 18:56:19 +02001247mlx4_en_set_port_mtu(struct mlx4_dev *dev, int slave, int port,
1248 struct mlx4_set_port_general_context *gen_context)
1249{
1250 struct mlx4_priv *priv = mlx4_priv(dev);
1251 struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
1252 struct mlx4_slave_state *slave_st = &master->slave_state[slave];
1253 u16 mtu, prev_mtu;
1254
1255 /* Mtu is configured as the max USER_MTU among all
1256 * the functions on the port.
1257 */
1258 mtu = be16_to_cpu(gen_context->mtu);
1259 mtu = min_t(int, mtu, dev->caps.eth_mtu_cap[port] +
1260 ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN);
1261 prev_mtu = slave_st->mtu[port];
1262 slave_st->mtu[port] = mtu;
1263 if (mtu > master->max_mtu[port])
1264 master->max_mtu[port] = mtu;
1265 if (mtu < prev_mtu && prev_mtu == master->max_mtu[port]) {
1266 int i;
1267
1268 slave_st->mtu[port] = mtu;
1269 master->max_mtu[port] = mtu;
1270 for (i = 0; i < dev->num_slaves; i++)
1271 master->max_mtu[port] =
1272 max_t(u16, master->max_mtu[port],
1273 master->slave_state[i].mtu[port]);
1274 }
1275 gen_context->mtu = cpu_to_be16(master->max_mtu[port]);
1276}
1277
1278static void
Shaker Daibes40fb4fc2017-01-29 18:56:18 +02001279mlx4_en_set_port_user_mtu(struct mlx4_dev *dev, int slave, int port,
1280 struct mlx4_set_port_general_context *gen_context)
1281{
1282 struct mlx4_priv *priv = mlx4_priv(dev);
1283 struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
1284 struct mlx4_slave_state *slave_st = &master->slave_state[slave];
1285 u16 user_mtu, prev_user_mtu;
1286
1287 /* User Mtu is configured as the max USER_MTU among all
1288 * the functions on the port.
1289 */
1290 user_mtu = be16_to_cpu(gen_context->user_mtu);
1291 user_mtu = min_t(int, user_mtu, dev->caps.eth_mtu_cap[port]);
1292 prev_user_mtu = slave_st->user_mtu[port];
1293 slave_st->user_mtu[port] = user_mtu;
1294 if (user_mtu > master->max_user_mtu[port])
1295 master->max_user_mtu[port] = user_mtu;
1296 if (user_mtu < prev_user_mtu &&
1297 prev_user_mtu == master->max_user_mtu[port]) {
1298 int i;
1299
1300 slave_st->user_mtu[port] = user_mtu;
1301 master->max_user_mtu[port] = user_mtu;
1302 for (i = 0; i < dev->num_slaves; i++)
1303 master->max_user_mtu[port] =
1304 max_t(u16, master->max_user_mtu[port],
1305 master->slave_state[i].user_mtu[port]);
1306 }
1307 gen_context->user_mtu = cpu_to_be16(master->max_user_mtu[port]);
1308}
1309
Shaker Daibes1f8176f2017-01-29 18:56:20 +02001310static void
1311mlx4_en_set_port_global_pause(struct mlx4_dev *dev, int slave,
1312 struct mlx4_set_port_general_context *gen_context)
1313{
1314 struct mlx4_priv *priv = mlx4_priv(dev);
1315 struct mlx4_mfunc_master_ctx *master = &priv->mfunc.master;
1316
1317 /* Slave cannot change Global Pause configuration */
1318 if (slave != mlx4_master_func_num(dev) &&
1319 (gen_context->pptx != master->pptx ||
1320 gen_context->pprx != master->pprx)) {
1321 gen_context->pptx = master->pptx;
1322 gen_context->pprx = master->pprx;
1323 mlx4_warn(dev, "denying Global Pause change for slave:%d\n",
1324 slave);
1325 } else {
1326 master->pptx = gen_context->pptx;
1327 master->pprx = gen_context->pprx;
1328 }
1329}
1330
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001331static int mlx4_common_set_port(struct mlx4_dev *dev, int slave, u32 in_mod,
1332 u8 op_mod, struct mlx4_cmd_mailbox *inbox)
1333{
1334 struct mlx4_priv *priv = mlx4_priv(dev);
1335 struct mlx4_port_info *port_info;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001336 struct mlx4_set_port_rqp_calc_context *qpn_context;
1337 struct mlx4_set_port_general_context *gen_context;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001338 struct mlx4_roce_gid_entry *gid_entry_tbl, *gid_entry_mbox, *gid_entry_mb1;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001339 int reset_qkey_viols;
1340 int port;
1341 int is_eth;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001342 int num_gids;
1343 int base;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001344 u32 in_modifier;
1345 u32 promisc;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001346 int err;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001347 int i, j;
1348 int offset;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001349 __be32 agg_cap_mask;
1350 __be32 slave_cap_mask;
1351 __be32 new_cap_mask;
1352
1353 port = in_mod & 0xff;
1354 in_modifier = in_mod >> 8;
1355 is_eth = op_mod;
1356 port_info = &priv->port[port];
1357
Shaker Daibes40fb4fc2017-01-29 18:56:18 +02001358 /* Slaves cannot perform SET_PORT operations,
1359 * except for changing MTU and USER_MTU.
1360 */
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001361 if (is_eth) {
1362 if (slave != dev->caps.function &&
Jack Morgenstein9cd59352014-03-12 12:00:38 +02001363 in_modifier != MLX4_SET_PORT_GENERAL &&
1364 in_modifier != MLX4_SET_PORT_GID_TABLE) {
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001365 mlx4_warn(dev, "denying SET_PORT for slave:%d\n",
1366 slave);
1367 return -EINVAL;
1368 }
1369 switch (in_modifier) {
1370 case MLX4_SET_PORT_RQP_CALC:
1371 qpn_context = inbox->buf;
1372 qpn_context->base_qpn =
1373 cpu_to_be32(port_info->base_qpn);
1374 qpn_context->n_mac = 0x7;
1375 promisc = be32_to_cpu(qpn_context->promisc) >>
1376 SET_PORT_PROMISC_SHIFT;
1377 qpn_context->promisc = cpu_to_be32(
1378 promisc << SET_PORT_PROMISC_SHIFT |
1379 port_info->base_qpn);
1380 promisc = be32_to_cpu(qpn_context->mcast) >>
1381 SET_PORT_MC_PROMISC_SHIFT;
1382 qpn_context->mcast = cpu_to_be32(
1383 promisc << SET_PORT_MC_PROMISC_SHIFT |
1384 port_info->base_qpn);
1385 break;
1386 case MLX4_SET_PORT_GENERAL:
1387 gen_context = inbox->buf;
Shaker Daibesbf1f9392017-01-29 18:56:19 +02001388
1389 if (gen_context->flags & MLX4_FLAG_V_MTU_MASK)
1390 mlx4_en_set_port_mtu(dev, slave, port,
1391 gen_context);
Shaker Daibes40fb4fc2017-01-29 18:56:18 +02001392
1393 if (gen_context->flags2 & MLX4_FLAG2_V_USER_MTU_MASK)
1394 mlx4_en_set_port_user_mtu(dev, slave, port,
1395 gen_context);
1396
Shaker Daibes1f8176f2017-01-29 18:56:20 +02001397 if (gen_context->flags &
Dan Carpenter73cfb2a2017-02-03 12:54:59 +03001398 (MLX4_FLAG_V_PPRX_MASK | MLX4_FLAG_V_PPTX_MASK))
Shaker Daibes1f8176f2017-01-29 18:56:20 +02001399 mlx4_en_set_port_global_pause(dev, slave,
1400 gen_context);
1401
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001402 break;
Jack Morgenstein9cd59352014-03-12 12:00:38 +02001403 case MLX4_SET_PORT_GID_TABLE:
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001404 /* change to MULTIPLE entries: number of guest's gids
1405 * need a FOR-loop here over number of gids the guest has.
1406 * 1. Check no duplicates in gids passed by slave
1407 */
Matan Barak449fc482014-03-19 18:11:52 +02001408 num_gids = mlx4_get_slave_num_gids(dev, slave, port);
1409 base = mlx4_get_base_gid_ix(dev, slave, port);
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001410 gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
1411 for (i = 0; i < num_gids; gid_entry_mbox++, i++) {
1412 if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
1413 sizeof(zgid_entry)))
1414 continue;
1415 gid_entry_mb1 = gid_entry_mbox + 1;
1416 for (j = i + 1; j < num_gids; gid_entry_mb1++, j++) {
1417 if (!memcmp(gid_entry_mb1->raw,
1418 zgid_entry.raw, sizeof(zgid_entry)))
1419 continue;
1420 if (!memcmp(gid_entry_mb1->raw, gid_entry_mbox->raw,
1421 sizeof(gid_entry_mbox->raw))) {
1422 /* found duplicate */
1423 return -EINVAL;
Jack Morgenstein9cd59352014-03-12 12:00:38 +02001424 }
1425 }
1426 }
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001427
1428 /* 2. Check that do not have duplicates in OTHER
1429 * entries in the port GID table
1430 */
Jack Morgenstein111c6092014-05-27 09:26:38 +03001431
1432 mutex_lock(&(priv->port[port].gid_table.mutex));
Jack Morgenstein9cd59352014-03-12 12:00:38 +02001433 for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001434 if (i >= base && i < base + num_gids)
1435 continue; /* don't compare to slave's current gids */
Jack Morgenstein111c6092014-05-27 09:26:38 +03001436 gid_entry_tbl = &priv->port[port].gid_table.roce_gids[i];
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001437 if (!memcmp(gid_entry_tbl->raw, zgid_entry.raw, sizeof(zgid_entry)))
1438 continue;
1439 gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
1440 for (j = 0; j < num_gids; gid_entry_mbox++, j++) {
1441 if (!memcmp(gid_entry_mbox->raw, zgid_entry.raw,
1442 sizeof(zgid_entry)))
1443 continue;
1444 if (!memcmp(gid_entry_mbox->raw, gid_entry_tbl->raw,
1445 sizeof(gid_entry_tbl->raw))) {
1446 /* found duplicate */
Joe Perches1a91de22014-05-07 12:52:57 -07001447 mlx4_warn(dev, "requested gid entry for slave:%d is a duplicate of gid at index %d\n",
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001448 slave, i);
Jack Morgenstein111c6092014-05-27 09:26:38 +03001449 mutex_unlock(&(priv->port[port].gid_table.mutex));
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001450 return -EINVAL;
1451 }
1452 }
Jack Morgenstein9cd59352014-03-12 12:00:38 +02001453 }
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001454
1455 /* insert slave GIDs with memcpy, starting at slave's base index */
1456 gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
1457 for (i = 0, offset = base; i < num_gids; gid_entry_mbox++, offset++, i++)
Jack Morgenstein111c6092014-05-27 09:26:38 +03001458 memcpy(priv->port[port].gid_table.roce_gids[offset].raw,
1459 gid_entry_mbox->raw, MLX4_ROCE_GID_ENTRY_SIZE);
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001460
1461 /* Now, copy roce port gids table to current mailbox for passing to FW */
1462 gid_entry_mbox = (struct mlx4_roce_gid_entry *)(inbox->buf);
1463 for (i = 0; i < MLX4_ROCE_MAX_GIDS; gid_entry_mbox++, i++)
Jack Morgenstein111c6092014-05-27 09:26:38 +03001464 memcpy(gid_entry_mbox->raw,
1465 priv->port[port].gid_table.roce_gids[i].raw,
1466 MLX4_ROCE_GID_ENTRY_SIZE);
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001467
Jack Morgenstein111c6092014-05-27 09:26:38 +03001468 err = mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
1469 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1470 MLX4_CMD_NATIVE);
1471 mutex_unlock(&(priv->port[port].gid_table.mutex));
1472 return err;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001473 }
Jack Morgenstein111c6092014-05-27 09:26:38 +03001474
1475 return mlx4_cmd(dev, inbox->dma, in_mod & 0xffff, op_mod,
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001476 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1477 MLX4_CMD_NATIVE);
1478 }
1479
Ido Shamay51af33c2015-04-02 16:31:20 +03001480 /* Slaves are not allowed to SET_PORT beacon (LED) blink */
1481 if (op_mod == MLX4_SET_PORT_BEACON_OPCODE) {
1482 mlx4_warn(dev, "denying SET_PORT Beacon slave:%d\n", slave);
1483 return -EPERM;
1484 }
1485
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001486 /* For IB, we only consider:
1487 * - The capability mask, which is set to the aggregate of all
1488 * slave function capabilities
1489 * - The QKey violatin counter - reset according to each request.
1490 */
1491
1492 if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
1493 reset_qkey_viols = (*(u8 *) inbox->buf) & 0x40;
1494 new_cap_mask = ((__be32 *) inbox->buf)[2];
1495 } else {
1496 reset_qkey_viols = ((u8 *) inbox->buf)[3] & 0x1;
1497 new_cap_mask = ((__be32 *) inbox->buf)[1];
1498 }
1499
Jack Morgensteinefcd2352012-08-03 08:40:52 +00001500 /* slave may not set the IS_SM capability for the port */
1501 if (slave != mlx4_master_func_num(dev) &&
1502 (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_IS_SM))
1503 return -EINVAL;
1504
1505 /* No DEV_MGMT in multifunc mode */
1506 if (mlx4_is_mfunc(dev) &&
1507 (be32_to_cpu(new_cap_mask) & MLX4_PORT_CAP_DEV_MGMT_SUP))
1508 return -EINVAL;
1509
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001510 agg_cap_mask = 0;
1511 slave_cap_mask =
1512 priv->mfunc.master.slave_state[slave].ib_cap_mask[port];
1513 priv->mfunc.master.slave_state[slave].ib_cap_mask[port] = new_cap_mask;
1514 for (i = 0; i < dev->num_slaves; i++)
1515 agg_cap_mask |=
1516 priv->mfunc.master.slave_state[i].ib_cap_mask[port];
1517
1518 /* only clear mailbox for guests. Master may be setting
1519 * MTU or PKEY table size
1520 */
1521 if (slave != dev->caps.function)
1522 memset(inbox->buf, 0, 256);
1523 if (dev->flags & MLX4_FLAG_OLD_PORT_CMDS) {
Jack Morgensteinedc4a672012-05-24 16:08:09 +03001524 *(u8 *) inbox->buf |= !!reset_qkey_viols << 6;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001525 ((__be32 *) inbox->buf)[2] = agg_cap_mask;
1526 } else {
Jack Morgensteinedc4a672012-05-24 16:08:09 +03001527 ((u8 *) inbox->buf)[3] |= !!reset_qkey_viols;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001528 ((__be32 *) inbox->buf)[1] = agg_cap_mask;
1529 }
1530
1531 err = mlx4_cmd(dev, inbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
1532 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
1533 if (err)
1534 priv->mfunc.master.slave_state[slave].ib_cap_mask[port] =
1535 slave_cap_mask;
1536 return err;
1537}
1538
1539int mlx4_SET_PORT_wrapper(struct mlx4_dev *dev, int slave,
1540 struct mlx4_vhcr *vhcr,
1541 struct mlx4_cmd_mailbox *inbox,
1542 struct mlx4_cmd_mailbox *outbox,
1543 struct mlx4_cmd_info *cmd)
1544{
Matan Barak449fc482014-03-19 18:11:52 +02001545 int port = mlx4_slave_convert_port(
1546 dev, slave, vhcr->in_modifier & 0xFF);
1547
1548 if (port < 0)
1549 return -EINVAL;
1550
1551 vhcr->in_modifier = (vhcr->in_modifier & ~0xFF) |
1552 (port & 0xFF);
1553
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001554 return mlx4_common_set_port(dev, slave, vhcr->in_modifier,
1555 vhcr->op_modifier, inbox);
1556}
1557
Or Gerlitz096335b2012-01-11 19:02:17 +02001558/* bit locations for set port command with zero op modifier */
1559enum {
1560 MLX4_SET_PORT_VL_CAP = 4, /* bits 7:4 */
1561 MLX4_SET_PORT_MTU_CAP = 12, /* bits 15:12 */
Jack Morgenstein66349612012-06-19 11:21:44 +03001562 MLX4_CHANGE_PORT_PKEY_TBL_SZ = 20,
Or Gerlitz096335b2012-01-11 19:02:17 +02001563 MLX4_CHANGE_PORT_VL_CAP = 21,
1564 MLX4_CHANGE_PORT_MTU_CAP = 22,
1565};
1566
Jack Morgenstein66349612012-06-19 11:21:44 +03001567int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port, int pkey_tbl_sz)
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001568{
1569 struct mlx4_cmd_mailbox *mailbox;
Jack Morgenstein66349612012-06-19 11:21:44 +03001570 int err, vl_cap, pkey_tbl_flag = 0;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001571
Roland Dreier352b09e2009-03-31 09:54:15 -07001572 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
1573 return 0;
1574
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001575 mailbox = mlx4_alloc_cmd_mailbox(dev);
1576 if (IS_ERR(mailbox))
1577 return PTR_ERR(mailbox);
1578
Yevgeny Petrilin793730b2009-03-11 15:47:18 -07001579 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
Or Gerlitz096335b2012-01-11 19:02:17 +02001580
Jack Morgenstein66349612012-06-19 11:21:44 +03001581 if (pkey_tbl_sz >= 0 && mlx4_is_master(dev)) {
1582 pkey_tbl_flag = 1;
1583 ((__be16 *) mailbox->buf)[20] = cpu_to_be16(pkey_tbl_sz);
1584 }
1585
Or Gerlitz096335b2012-01-11 19:02:17 +02001586 /* IB VL CAP enum isn't used by the firmware, just numerical values */
1587 for (vl_cap = 8; vl_cap >= 1; vl_cap >>= 1) {
1588 ((__be32 *) mailbox->buf)[0] = cpu_to_be32(
1589 (1 << MLX4_CHANGE_PORT_MTU_CAP) |
1590 (1 << MLX4_CHANGE_PORT_VL_CAP) |
Jack Morgenstein66349612012-06-19 11:21:44 +03001591 (pkey_tbl_flag << MLX4_CHANGE_PORT_PKEY_TBL_SZ) |
Or Gerlitz096335b2012-01-11 19:02:17 +02001592 (dev->caps.port_ib_mtu[port] << MLX4_SET_PORT_MTU_CAP) |
1593 (vl_cap << MLX4_SET_PORT_VL_CAP));
Ido Shamaya130b592015-04-02 16:31:19 +03001594 err = mlx4_cmd(dev, mailbox->dma, port,
1595 MLX4_SET_PORT_IB_OPCODE, MLX4_CMD_SET_PORT,
1596 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
Or Gerlitz096335b2012-01-11 19:02:17 +02001597 if (err != -ENOMEM)
1598 break;
1599 }
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -07001600
1601 mlx4_free_cmd_mailbox(dev, mailbox);
1602 return err;
1603}
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001604
Moni Shoua1da494c2016-01-14 17:50:34 +02001605#define SET_PORT_ROCE_2_FLAGS 0x10
1606#define MLX4_SET_PORT_ROCE_V1_V2 0x2
Joerg Roedelcb9ffb72011-12-15 06:48:37 +00001607int mlx4_SET_PORT_general(struct mlx4_dev *dev, u8 port, int mtu,
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001608 u8 pptx, u8 pfctx, u8 pprx, u8 pfcrx)
1609{
1610 struct mlx4_cmd_mailbox *mailbox;
1611 struct mlx4_set_port_general_context *context;
1612 int err;
1613 u32 in_mod;
1614
1615 mailbox = mlx4_alloc_cmd_mailbox(dev);
1616 if (IS_ERR(mailbox))
1617 return PTR_ERR(mailbox);
1618 context = mailbox->buf;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001619 context->flags = SET_PORT_GEN_ALL_VALID;
1620 context->mtu = cpu_to_be16(mtu);
1621 context->pptx = (pptx * (!pfctx)) << 7;
1622 context->pfctx = pfctx;
1623 context->pprx = (pprx * (!pfcrx)) << 7;
1624 context->pfcrx = pfcrx;
1625
Moni Shoua1da494c2016-01-14 17:50:34 +02001626 if (dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
1627 context->flags |= SET_PORT_ROCE_2_FLAGS;
1628 context->roce_mode |=
1629 MLX4_SET_PORT_ROCE_V1_V2 << 4;
1630 }
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001631 in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
Ido Shamaya130b592015-04-02 16:31:19 +03001632 err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
1633 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1634 MLX4_CMD_WRAPPED);
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001635
1636 mlx4_free_cmd_mailbox(dev, mailbox);
1637 return err;
1638}
1639EXPORT_SYMBOL(mlx4_SET_PORT_general);
1640
Joerg Roedelcb9ffb72011-12-15 06:48:37 +00001641int mlx4_SET_PORT_qpn_calc(struct mlx4_dev *dev, u8 port, u32 base_qpn,
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001642 u8 promisc)
1643{
1644 struct mlx4_cmd_mailbox *mailbox;
1645 struct mlx4_set_port_rqp_calc_context *context;
1646 int err;
1647 u32 in_mod;
1648 u32 m_promisc = (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_MC_STEER) ?
1649 MCAST_DIRECT : MCAST_DEFAULT;
1650
Hadar Hen Zionc96d97f2012-07-05 04:03:44 +00001651 if (dev->caps.steering_mode != MLX4_STEERING_MODE_A0)
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001652 return 0;
1653
1654 mailbox = mlx4_alloc_cmd_mailbox(dev);
1655 if (IS_ERR(mailbox))
1656 return PTR_ERR(mailbox);
1657 context = mailbox->buf;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001658 context->base_qpn = cpu_to_be32(base_qpn);
1659 context->n_mac = dev->caps.log_num_macs;
1660 context->promisc = cpu_to_be32(promisc << SET_PORT_PROMISC_SHIFT |
1661 base_qpn);
1662 context->mcast = cpu_to_be32(m_promisc << SET_PORT_MC_PROMISC_SHIFT |
1663 base_qpn);
1664 context->intra_no_vlan = 0;
1665 context->no_vlan = MLX4_NO_VLAN_IDX;
1666 context->intra_vlan_miss = 0;
1667 context->vlan_miss = MLX4_VLAN_MISS_IDX;
1668
1669 in_mod = MLX4_SET_PORT_RQP_CALC << 8 | port;
Ido Shamaya130b592015-04-02 16:31:19 +03001670 err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
1671 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1672 MLX4_CMD_WRAPPED);
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001673
1674 mlx4_free_cmd_mailbox(dev, mailbox);
1675 return err;
1676}
1677EXPORT_SYMBOL(mlx4_SET_PORT_qpn_calc);
1678
Shaker Daibes40fb4fc2017-01-29 18:56:18 +02001679int mlx4_SET_PORT_user_mtu(struct mlx4_dev *dev, u8 port, u16 user_mtu)
1680{
1681 struct mlx4_cmd_mailbox *mailbox;
1682 struct mlx4_set_port_general_context *context;
1683 u32 in_mod;
1684 int err;
1685
1686 mailbox = mlx4_alloc_cmd_mailbox(dev);
1687 if (IS_ERR(mailbox))
1688 return PTR_ERR(mailbox);
1689 context = mailbox->buf;
1690 context->flags2 |= MLX4_FLAG2_V_USER_MTU_MASK;
1691 context->user_mtu = cpu_to_be16(user_mtu);
1692
1693 in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
1694 err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
1695 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1696 MLX4_CMD_WRAPPED);
1697
1698 mlx4_free_cmd_mailbox(dev, mailbox);
1699 return err;
1700}
1701EXPORT_SYMBOL(mlx4_SET_PORT_user_mtu);
1702
Muhammad Mahajna78500b82015-04-02 16:31:22 +03001703int mlx4_SET_PORT_fcs_check(struct mlx4_dev *dev, u8 port, u8 ignore_fcs_value)
1704{
1705 struct mlx4_cmd_mailbox *mailbox;
1706 struct mlx4_set_port_general_context *context;
1707 u32 in_mod;
1708 int err;
1709
1710 mailbox = mlx4_alloc_cmd_mailbox(dev);
1711 if (IS_ERR(mailbox))
1712 return PTR_ERR(mailbox);
1713 context = mailbox->buf;
Shaker Daibes40fb4fc2017-01-29 18:56:18 +02001714 context->flags2 |= MLX4_FLAG2_V_IGNORE_FCS_MASK;
Muhammad Mahajna78500b82015-04-02 16:31:22 +03001715 if (ignore_fcs_value)
1716 context->ignore_fcs |= MLX4_IGNORE_FCS_MASK;
1717 else
1718 context->ignore_fcs &= ~MLX4_IGNORE_FCS_MASK;
1719
1720 in_mod = MLX4_SET_PORT_GENERAL << 8 | port;
1721 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
1722 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
1723
1724 mlx4_free_cmd_mailbox(dev, mailbox);
1725 return err;
1726}
1727EXPORT_SYMBOL(mlx4_SET_PORT_fcs_check);
1728
Or Gerlitz7ffdf722013-12-23 16:09:43 +02001729enum {
1730 VXLAN_ENABLE_MODIFY = 1 << 7,
1731 VXLAN_STEERING_MODIFY = 1 << 6,
1732
1733 VXLAN_ENABLE = 1 << 7,
1734};
1735
1736struct mlx4_set_port_vxlan_context {
1737 u32 reserved1;
1738 u8 modify_flags;
1739 u8 reserved2;
1740 u8 enable_flags;
1741 u8 steering;
1742};
1743
Or Gerlitz1b136de2014-03-27 14:02:04 +02001744int mlx4_SET_PORT_VXLAN(struct mlx4_dev *dev, u8 port, u8 steering, int enable)
Or Gerlitz7ffdf722013-12-23 16:09:43 +02001745{
1746 int err;
1747 u32 in_mod;
1748 struct mlx4_cmd_mailbox *mailbox;
1749 struct mlx4_set_port_vxlan_context *context;
1750
1751 mailbox = mlx4_alloc_cmd_mailbox(dev);
1752 if (IS_ERR(mailbox))
1753 return PTR_ERR(mailbox);
1754 context = mailbox->buf;
1755 memset(context, 0, sizeof(*context));
1756
1757 context->modify_flags = VXLAN_ENABLE_MODIFY | VXLAN_STEERING_MODIFY;
Or Gerlitz1b136de2014-03-27 14:02:04 +02001758 if (enable)
1759 context->enable_flags = VXLAN_ENABLE;
Or Gerlitz7ffdf722013-12-23 16:09:43 +02001760 context->steering = steering;
1761
1762 in_mod = MLX4_SET_PORT_VXLAN << 8 | port;
Ido Shamaya130b592015-04-02 16:31:19 +03001763 err = mlx4_cmd(dev, mailbox->dma, in_mod, MLX4_SET_PORT_ETH_OPCODE,
1764 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1765 MLX4_CMD_NATIVE);
Or Gerlitz7ffdf722013-12-23 16:09:43 +02001766
1767 mlx4_free_cmd_mailbox(dev, mailbox);
1768 return err;
1769}
1770EXPORT_SYMBOL(mlx4_SET_PORT_VXLAN);
1771
Ido Shamay51af33c2015-04-02 16:31:20 +03001772int mlx4_SET_PORT_BEACON(struct mlx4_dev *dev, u8 port, u16 time)
1773{
1774 int err;
1775 struct mlx4_cmd_mailbox *mailbox;
1776
1777 mailbox = mlx4_alloc_cmd_mailbox(dev);
1778 if (IS_ERR(mailbox))
1779 return PTR_ERR(mailbox);
1780
1781 *((__be32 *)mailbox->buf) = cpu_to_be32(time);
1782
1783 err = mlx4_cmd(dev, mailbox->dma, port, MLX4_SET_PORT_BEACON_OPCODE,
1784 MLX4_CMD_SET_PORT, MLX4_CMD_TIME_CLASS_B,
1785 MLX4_CMD_NATIVE);
1786
1787 mlx4_free_cmd_mailbox(dev, mailbox);
1788 return err;
1789}
1790EXPORT_SYMBOL(mlx4_SET_PORT_BEACON);
1791
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001792int mlx4_SET_MCAST_FLTR_wrapper(struct mlx4_dev *dev, int slave,
1793 struct mlx4_vhcr *vhcr,
1794 struct mlx4_cmd_mailbox *inbox,
1795 struct mlx4_cmd_mailbox *outbox,
1796 struct mlx4_cmd_info *cmd)
1797{
1798 int err = 0;
1799
1800 return err;
1801}
1802
1803int mlx4_SET_MCAST_FLTR(struct mlx4_dev *dev, u8 port,
1804 u64 mac, u64 clear, u8 mode)
1805{
1806 return mlx4_cmd(dev, (mac | (clear << 63)), port, mode,
1807 MLX4_CMD_SET_MCAST_FLTR, MLX4_CMD_TIME_CLASS_B,
1808 MLX4_CMD_WRAPPED);
1809}
1810EXPORT_SYMBOL(mlx4_SET_MCAST_FLTR);
1811
1812int mlx4_SET_VLAN_FLTR_wrapper(struct mlx4_dev *dev, int slave,
1813 struct mlx4_vhcr *vhcr,
1814 struct mlx4_cmd_mailbox *inbox,
1815 struct mlx4_cmd_mailbox *outbox,
1816 struct mlx4_cmd_info *cmd)
1817{
1818 int err = 0;
1819
1820 return err;
1821}
1822
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001823int mlx4_DUMP_ETH_STATS_wrapper(struct mlx4_dev *dev, int slave,
1824 struct mlx4_vhcr *vhcr,
1825 struct mlx4_cmd_mailbox *inbox,
1826 struct mlx4_cmd_mailbox *outbox,
1827 struct mlx4_cmd_info *cmd)
1828{
Jack Morgensteind2582a02016-10-27 16:27:21 +03001829 return 0;
Eugenia Emantayevffe455a2011-12-13 04:16:21 +00001830}
Eugenia Emantayev93ece0c2012-01-19 09:45:05 +00001831
Jack Morgenstein9cd59352014-03-12 12:00:38 +02001832int mlx4_get_slave_from_roce_gid(struct mlx4_dev *dev, int port, u8 *gid,
1833 int *slave_id)
Jack Morgenstein6ee51a42014-03-12 12:00:37 +02001834{
1835 struct mlx4_priv *priv = mlx4_priv(dev);
1836 int i, found_ix = -1;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001837 int vf_gids = MLX4_ROCE_MAX_GIDS - MLX4_ROCE_PF_GIDS;
Matan Barak449fc482014-03-19 18:11:52 +02001838 struct mlx4_slaves_pport slaves_pport;
1839 unsigned num_vfs;
1840 int slave_gid;
Jack Morgenstein6ee51a42014-03-12 12:00:37 +02001841
1842 if (!mlx4_is_mfunc(dev))
1843 return -EINVAL;
1844
Matan Barak449fc482014-03-19 18:11:52 +02001845 slaves_pport = mlx4_phys_to_slaves_pport(dev, port);
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001846 num_vfs = bitmap_weight(slaves_pport.slaves,
1847 dev->persist->num_vfs + 1) - 1;
Matan Barak449fc482014-03-19 18:11:52 +02001848
Jack Morgenstein6ee51a42014-03-12 12:00:37 +02001849 for (i = 0; i < MLX4_ROCE_MAX_GIDS; i++) {
Jack Morgenstein111c6092014-05-27 09:26:38 +03001850 if (!memcmp(priv->port[port].gid_table.roce_gids[i].raw, gid,
1851 MLX4_ROCE_GID_ENTRY_SIZE)) {
Jack Morgenstein6ee51a42014-03-12 12:00:37 +02001852 found_ix = i;
1853 break;
1854 }
1855 }
1856
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001857 if (found_ix >= 0) {
Matan Barak0254bc82014-05-04 17:07:23 +03001858 /* Calculate a slave_gid which is the slave number in the gid
1859 * table and not a globally unique slave number.
1860 */
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001861 if (found_ix < MLX4_ROCE_PF_GIDS)
Matan Barak449fc482014-03-19 18:11:52 +02001862 slave_gid = 0;
1863 else if (found_ix < MLX4_ROCE_PF_GIDS + (vf_gids % num_vfs) *
1864 (vf_gids / num_vfs + 1))
1865 slave_gid = ((found_ix - MLX4_ROCE_PF_GIDS) /
1866 (vf_gids / num_vfs + 1)) + 1;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001867 else
Matan Barak449fc482014-03-19 18:11:52 +02001868 slave_gid =
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001869 ((found_ix - MLX4_ROCE_PF_GIDS -
Matan Barak449fc482014-03-19 18:11:52 +02001870 ((vf_gids % num_vfs) * ((vf_gids / num_vfs + 1)))) /
1871 (vf_gids / num_vfs)) + vf_gids % num_vfs + 1;
1872
Matan Barak0254bc82014-05-04 17:07:23 +03001873 /* Calculate the globally unique slave id */
Matan Barak449fc482014-03-19 18:11:52 +02001874 if (slave_gid) {
1875 struct mlx4_active_ports exclusive_ports;
1876 struct mlx4_active_ports actv_ports;
1877 struct mlx4_slaves_pport slaves_pport_actv;
1878 unsigned max_port_p_one;
Matan Barak0254bc82014-05-04 17:07:23 +03001879 int num_vfs_before = 0;
1880 int candidate_slave_gid;
Matan Barak449fc482014-03-19 18:11:52 +02001881
Matan Barak0254bc82014-05-04 17:07:23 +03001882 /* Calculate how many VFs are on the previous port, if exists */
Matan Barak449fc482014-03-19 18:11:52 +02001883 for (i = 1; i < port; i++) {
1884 bitmap_zero(exclusive_ports.ports, dev->caps.num_ports);
Matan Barak0254bc82014-05-04 17:07:23 +03001885 set_bit(i - 1, exclusive_ports.ports);
Matan Barak449fc482014-03-19 18:11:52 +02001886 slaves_pport_actv =
1887 mlx4_phys_to_slaves_pport_actv(
1888 dev, &exclusive_ports);
Matan Barak0254bc82014-05-04 17:07:23 +03001889 num_vfs_before += bitmap_weight(
Matan Barak449fc482014-03-19 18:11:52 +02001890 slaves_pport_actv.slaves,
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001891 dev->persist->num_vfs + 1);
Matan Barak449fc482014-03-19 18:11:52 +02001892 }
1893
Matan Barak0254bc82014-05-04 17:07:23 +03001894 /* candidate_slave_gid isn't necessarily the correct slave, but
1895 * it has the same number of ports and is assigned to the same
1896 * ports as the real slave we're looking for. On dual port VF,
1897 * slave_gid = [single port VFs on port <port>] +
1898 * [offset of the current slave from the first dual port VF] +
1899 * 1 (for the PF).
1900 */
1901 candidate_slave_gid = slave_gid + num_vfs_before;
1902
1903 actv_ports = mlx4_get_active_ports(dev, candidate_slave_gid);
Matan Barak449fc482014-03-19 18:11:52 +02001904 max_port_p_one = find_first_bit(
1905 actv_ports.ports, dev->caps.num_ports) +
1906 bitmap_weight(actv_ports.ports,
1907 dev->caps.num_ports) + 1;
1908
Matan Barak0254bc82014-05-04 17:07:23 +03001909 /* Calculate the real slave number */
Matan Barak449fc482014-03-19 18:11:52 +02001910 for (i = 1; i < max_port_p_one; i++) {
1911 if (i == port)
1912 continue;
1913 bitmap_zero(exclusive_ports.ports,
1914 dev->caps.num_ports);
1915 set_bit(i - 1, exclusive_ports.ports);
1916 slaves_pport_actv =
1917 mlx4_phys_to_slaves_pport_actv(
1918 dev, &exclusive_ports);
1919 slave_gid += bitmap_weight(
1920 slaves_pport_actv.slaves,
Yishai Hadas872bf2f2015-01-25 16:59:35 +02001921 dev->persist->num_vfs + 1);
Matan Barak449fc482014-03-19 18:11:52 +02001922 }
1923 }
1924 *slave_id = slave_gid;
Jack Morgensteinb6ffaef2014-03-12 12:00:39 +02001925 }
Jack Morgenstein6ee51a42014-03-12 12:00:37 +02001926
1927 return (found_ix >= 0) ? 0 : -EINVAL;
1928}
1929EXPORT_SYMBOL(mlx4_get_slave_from_roce_gid);
1930
Jack Morgenstein9cd59352014-03-12 12:00:38 +02001931int mlx4_get_roce_gid_from_slave(struct mlx4_dev *dev, int port, int slave_id,
1932 u8 *gid)
Jack Morgenstein6ee51a42014-03-12 12:00:37 +02001933{
1934 struct mlx4_priv *priv = mlx4_priv(dev);
1935
1936 if (!mlx4_is_master(dev))
1937 return -EINVAL;
1938
Jack Morgenstein111c6092014-05-27 09:26:38 +03001939 memcpy(gid, priv->port[port].gid_table.roce_gids[slave_id].raw,
1940 MLX4_ROCE_GID_ENTRY_SIZE);
Jack Morgenstein6ee51a42014-03-12 12:00:37 +02001941 return 0;
1942}
1943EXPORT_SYMBOL(mlx4_get_roce_gid_from_slave);
Saeed Mahameed32a173c2014-10-27 11:37:35 +02001944
1945/* Cable Module Info */
1946#define MODULE_INFO_MAX_READ 48
1947
1948#define I2C_ADDR_LOW 0x50
1949#define I2C_ADDR_HIGH 0x51
1950#define I2C_PAGE_SIZE 256
1951
1952/* Module Info Data */
1953struct mlx4_cable_info {
1954 u8 i2c_addr;
1955 u8 page_num;
1956 __be16 dev_mem_address;
1957 __be16 reserved1;
1958 __be16 size;
1959 __be32 reserved2[2];
1960 u8 data[MODULE_INFO_MAX_READ];
1961};
1962
1963enum cable_info_err {
1964 CABLE_INF_INV_PORT = 0x1,
1965 CABLE_INF_OP_NOSUP = 0x2,
1966 CABLE_INF_NOT_CONN = 0x3,
1967 CABLE_INF_NO_EEPRM = 0x4,
1968 CABLE_INF_PAGE_ERR = 0x5,
1969 CABLE_INF_INV_ADDR = 0x6,
1970 CABLE_INF_I2C_ADDR = 0x7,
1971 CABLE_INF_QSFP_VIO = 0x8,
1972 CABLE_INF_I2C_BUSY = 0x9,
1973};
1974
1975#define MAD_STATUS_2_CABLE_ERR(mad_status) ((mad_status >> 8) & 0xFF)
1976
1977static inline const char *cable_info_mad_err_str(u16 mad_status)
1978{
1979 u8 err = MAD_STATUS_2_CABLE_ERR(mad_status);
1980
1981 switch (err) {
1982 case CABLE_INF_INV_PORT:
1983 return "invalid port selected";
1984 case CABLE_INF_OP_NOSUP:
1985 return "operation not supported for this port (the port is of type CX4 or internal)";
1986 case CABLE_INF_NOT_CONN:
1987 return "cable is not connected";
1988 case CABLE_INF_NO_EEPRM:
1989 return "the connected cable has no EPROM (passive copper cable)";
1990 case CABLE_INF_PAGE_ERR:
1991 return "page number is greater than 15";
1992 case CABLE_INF_INV_ADDR:
1993 return "invalid device_address or size (that is, size equals 0 or address+size is greater than 256)";
1994 case CABLE_INF_I2C_ADDR:
1995 return "invalid I2C slave address";
1996 case CABLE_INF_QSFP_VIO:
1997 return "at least one cable violates the QSFP specification and ignores the modsel signal";
1998 case CABLE_INF_I2C_BUSY:
1999 return "I2C bus is constantly busy";
2000 }
2001 return "Unknown Error";
2002}
2003
2004/**
2005 * mlx4_get_module_info - Read cable module eeprom data
2006 * @dev: mlx4_dev.
2007 * @port: port number.
2008 * @offset: byte offset in eeprom to start reading data from.
2009 * @size: num of bytes to read.
2010 * @data: output buffer to put the requested data into.
2011 *
2012 * Reads cable module eeprom data, puts the outcome data into
2013 * data pointer paramer.
2014 * Returns num of read bytes on success or a negative error
2015 * code.
2016 */
2017int mlx4_get_module_info(struct mlx4_dev *dev, u8 port,
2018 u16 offset, u16 size, u8 *data)
2019{
2020 struct mlx4_cmd_mailbox *inbox, *outbox;
2021 struct mlx4_mad_ifc *inmad, *outmad;
2022 struct mlx4_cable_info *cable_info;
2023 u16 i2c_addr;
2024 int ret;
2025
2026 if (size > MODULE_INFO_MAX_READ)
2027 size = MODULE_INFO_MAX_READ;
2028
2029 inbox = mlx4_alloc_cmd_mailbox(dev);
2030 if (IS_ERR(inbox))
2031 return PTR_ERR(inbox);
2032
2033 outbox = mlx4_alloc_cmd_mailbox(dev);
2034 if (IS_ERR(outbox)) {
2035 mlx4_free_cmd_mailbox(dev, inbox);
2036 return PTR_ERR(outbox);
2037 }
2038
2039 inmad = (struct mlx4_mad_ifc *)(inbox->buf);
2040 outmad = (struct mlx4_mad_ifc *)(outbox->buf);
2041
2042 inmad->method = 0x1; /* Get */
2043 inmad->class_version = 0x1;
2044 inmad->mgmt_class = 0x1;
2045 inmad->base_version = 0x1;
2046 inmad->attr_id = cpu_to_be16(0xFF60); /* Module Info */
2047
2048 if (offset < I2C_PAGE_SIZE && offset + size > I2C_PAGE_SIZE)
2049 /* Cross pages reads are not allowed
2050 * read until offset 256 in low page
2051 */
2052 size -= offset + size - I2C_PAGE_SIZE;
2053
2054 i2c_addr = I2C_ADDR_LOW;
2055 if (offset >= I2C_PAGE_SIZE) {
2056 /* Reset offset to high page */
2057 i2c_addr = I2C_ADDR_HIGH;
2058 offset -= I2C_PAGE_SIZE;
2059 }
2060
2061 cable_info = (struct mlx4_cable_info *)inmad->data;
2062 cable_info->dev_mem_address = cpu_to_be16(offset);
2063 cable_info->page_num = 0;
2064 cable_info->i2c_addr = i2c_addr;
2065 cable_info->size = cpu_to_be16(size);
2066
2067 ret = mlx4_cmd_box(dev, inbox->dma, outbox->dma, port, 3,
2068 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C,
2069 MLX4_CMD_NATIVE);
2070 if (ret)
2071 goto out;
2072
2073 if (be16_to_cpu(outmad->status)) {
2074 /* Mad returned with bad status */
2075 ret = be16_to_cpu(outmad->status);
2076 mlx4_warn(dev,
2077 "MLX4_CMD_MAD_IFC Get Module info attr(%x) port(%d) i2c_addr(%x) offset(%d) size(%d): Response Mad Status(%x) - %s\n",
2078 0xFF60, port, i2c_addr, offset, size,
2079 ret, cable_info_mad_err_str(ret));
2080
2081 if (i2c_addr == I2C_ADDR_HIGH &&
2082 MAD_STATUS_2_CABLE_ERR(ret) == CABLE_INF_I2C_ADDR)
2083 /* Some SFP cables do not support i2c slave
2084 * address 0x51 (high page), abort silently.
2085 */
2086 ret = 0;
2087 else
2088 ret = -ret;
2089 goto out;
2090 }
2091 cable_info = (struct mlx4_cable_info *)outmad->data;
2092 memcpy(data, cable_info->data, size);
2093 ret = size;
2094out:
2095 mlx4_free_cmd_mailbox(dev, inbox);
2096 mlx4_free_cmd_mailbox(dev, outbox);
2097 return ret;
2098}
2099EXPORT_SYMBOL(mlx4_get_module_info);
Rana Shahoutaf7d5182016-06-21 12:43:59 +03002100
2101int mlx4_max_tc(struct mlx4_dev *dev)
2102{
2103 u8 num_tc = dev->caps.max_tc_eth;
2104
2105 if (!num_tc)
Tariq Toukan564ed9b2016-09-11 10:56:19 +03002106 num_tc = MLX4_TC_MAX_NUMBER;
Rana Shahoutaf7d5182016-06-21 12:43:59 +03002107
2108 return num_tc;
2109}
2110EXPORT_SYMBOL(mlx4_max_tc);