blob: 1f95afda68410ad9794c0425366b6dd9c6aa7663 [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>
35
36#include <linux/mlx4/cmd.h>
37
38#include "mlx4.h"
39
40#define MLX4_MAC_VALID (1ull << 63)
41#define MLX4_MAC_MASK 0xffffffffffffULL
42
43#define MLX4_VLAN_VALID (1u << 31)
44#define MLX4_VLAN_MASK 0xfff
45
46void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
47{
48 int i;
49
50 mutex_init(&table->mutex);
51 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
52 table->entries[i] = 0;
53 table->refs[i] = 0;
54 }
55 table->max = 1 << dev->caps.log_num_macs;
56 table->total = 0;
57}
58
59void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
60{
61 int i;
62
63 mutex_init(&table->mutex);
64 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
65 table->entries[i] = 0;
66 table->refs[i] = 0;
67 }
68 table->max = 1 << dev->caps.log_num_vlans;
69 table->total = 0;
70}
71
72static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
73 __be64 *entries)
74{
75 struct mlx4_cmd_mailbox *mailbox;
76 u32 in_mod;
77 int err;
78
79 mailbox = mlx4_alloc_cmd_mailbox(dev);
80 if (IS_ERR(mailbox))
81 return PTR_ERR(mailbox);
82
83 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
84
85 in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
86 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
87 MLX4_CMD_TIME_CLASS_B);
88
89 mlx4_free_cmd_mailbox(dev, mailbox);
90 return err;
91}
92
Yevgeny Petrilin16792002011-03-22 22:38:31 +000093static int mlx4_uc_steer_add(struct mlx4_dev *dev, u8 port,
94 u64 mac, int *qpn, u8 reserve)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070095{
Yevgeny Petrilin16792002011-03-22 22:38:31 +000096 struct mlx4_qp qp;
97 u8 gid[16] = {0};
98 int err;
99
100 if (reserve) {
101 err = mlx4_qp_reserve_range(dev, 1, 1, qpn);
102 if (err) {
103 mlx4_err(dev, "Failed to reserve qp for mac registration\n");
104 return err;
105 }
106 }
107 qp.qpn = *qpn;
108
109 mac &= 0xffffffffffffULL;
110 mac = cpu_to_be64(mac << 16);
111 memcpy(&gid[10], &mac, ETH_ALEN);
112 gid[5] = port;
113 gid[7] = MLX4_UC_STEER << 1;
114
115 err = mlx4_qp_attach_common(dev, &qp, gid, 0,
116 MLX4_PROT_ETH, MLX4_UC_STEER);
117 if (err && reserve)
118 mlx4_qp_release_range(dev, *qpn, 1);
119
120 return err;
121}
122
123static void mlx4_uc_steer_release(struct mlx4_dev *dev, u8 port,
124 u64 mac, int qpn, u8 free)
125{
126 struct mlx4_qp qp;
127 u8 gid[16] = {0};
128
129 qp.qpn = qpn;
130 mac &= 0xffffffffffffULL;
131 mac = cpu_to_be64(mac << 16);
132 memcpy(&gid[10], &mac, ETH_ALEN);
133 gid[5] = port;
134 gid[7] = MLX4_UC_STEER << 1;
135
136 mlx4_qp_detach_common(dev, &qp, gid, MLX4_PROT_ETH, MLX4_UC_STEER);
137 if (free)
138 mlx4_qp_release_range(dev, qpn, 1);
139}
140
141int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *qpn, u8 wrap)
142{
143 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
144 struct mlx4_mac_table *table = &info->mac_table;
145 struct mlx4_mac_entry *entry;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700146 int i, err = 0;
147 int free = -1;
148
Or Gerlitzccf86322011-07-07 19:19:29 +0000149 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000150 err = mlx4_uc_steer_add(dev, port, mac, qpn, 1);
151 if (!err) {
152 entry = kmalloc(sizeof *entry, GFP_KERNEL);
153 if (!entry) {
154 mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
155 return -ENOMEM;
156 }
157 entry->mac = mac;
158 err = radix_tree_insert(&info->mac_tree, *qpn, entry);
159 if (err) {
160 mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
161 return err;
162 }
163 } else
164 return err;
165 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700166 mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
167 mutex_lock(&table->mutex);
168 for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
169 if (free < 0 && !table->refs[i]) {
170 free = i;
171 continue;
172 }
173
174 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300175 /* MAC already registered, increase references count */
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700176 ++table->refs[i];
177 goto out;
178 }
179 }
Eli Cohen0926f912010-10-25 02:56:47 +0000180
181 if (free < 0) {
182 err = -ENOMEM;
183 goto out;
184 }
185
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700186 mlx4_dbg(dev, "Free MAC index is %d\n", free);
187
188 if (table->total == table->max) {
189 /* No free mac entries */
190 err = -ENOSPC;
191 goto out;
192 }
193
194 /* Register new MAC */
195 table->refs[free] = 1;
196 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
197
198 err = mlx4_set_port_mac_table(dev, port, table->entries);
199 if (unlikely(err)) {
200 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
201 table->refs[free] = 0;
202 table->entries[free] = 0;
203 goto out;
204 }
205
Or Gerlitzccf86322011-07-07 19:19:29 +0000206 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000207 *qpn = info->base_qpn + free;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700208 ++table->total;
209out:
210 mutex_unlock(&table->mutex);
211 return err;
212}
213EXPORT_SYMBOL_GPL(mlx4_register_mac);
214
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000215static int validate_index(struct mlx4_dev *dev,
216 struct mlx4_mac_table *table, int index)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700217{
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000218 int err = 0;
219
220 if (index < 0 || index >= table->max || !table->entries[index]) {
221 mlx4_warn(dev, "No valid Mac entry for the given index\n");
222 err = -EINVAL;
223 }
224 return err;
225}
226
227static int find_index(struct mlx4_dev *dev,
228 struct mlx4_mac_table *table, u64 mac)
229{
230 int i;
231 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
232 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
233 return i;
234 }
235 /* Mac not found */
236 return -EINVAL;
237}
238
239void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
240{
241 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
242 struct mlx4_mac_table *table = &info->mac_table;
243 int index = qpn - info->base_qpn;
244 struct mlx4_mac_entry *entry;
245
Or Gerlitzccf86322011-07-07 19:19:29 +0000246 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000247 entry = radix_tree_lookup(&info->mac_tree, qpn);
248 if (entry) {
249 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1);
250 radix_tree_delete(&info->mac_tree, qpn);
251 index = find_index(dev, table, entry->mac);
252 kfree(entry);
253 }
254 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700255
256 mutex_lock(&table->mutex);
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000257
258 if (validate_index(dev, table, index))
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700259 goto out;
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000260
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700261 table->entries[index] = 0;
262 mlx4_set_port_mac_table(dev, port, table->entries);
263 --table->total;
264out:
265 mutex_unlock(&table->mutex);
266}
267EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
268
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000269int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
270{
271 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
272 struct mlx4_mac_table *table = &info->mac_table;
273 int index = qpn - info->base_qpn;
274 struct mlx4_mac_entry *entry;
275 int err;
276
Or Gerlitzccf86322011-07-07 19:19:29 +0000277 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000278 entry = radix_tree_lookup(&info->mac_tree, qpn);
279 if (!entry)
280 return -EINVAL;
281 index = find_index(dev, table, entry->mac);
282 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0);
283 entry->mac = new_mac;
284 err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0);
285 if (err || index < 0)
286 return err;
287 }
288
289 mutex_lock(&table->mutex);
290
291 err = validate_index(dev, table, index);
292 if (err)
293 goto out;
294
295 table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
296
297 err = mlx4_set_port_mac_table(dev, port, table->entries);
298 if (unlikely(err)) {
299 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac);
300 table->entries[index] = 0;
301 }
302out:
303 mutex_unlock(&table->mutex);
304 return err;
305}
306EXPORT_SYMBOL_GPL(mlx4_replace_mac);
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700307static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
308 __be32 *entries)
309{
310 struct mlx4_cmd_mailbox *mailbox;
311 u32 in_mod;
312 int err;
313
314 mailbox = mlx4_alloc_cmd_mailbox(dev);
315 if (IS_ERR(mailbox))
316 return PTR_ERR(mailbox);
317
318 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
319 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
320 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
321 MLX4_CMD_TIME_CLASS_B);
322
323 mlx4_free_cmd_mailbox(dev, mailbox);
324
325 return err;
326}
327
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300328int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
329{
330 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
331 int i;
332
333 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
334 if (table->refs[i] &&
335 (vid == (MLX4_VLAN_MASK &
336 be32_to_cpu(table->entries[i])))) {
337 /* VLAN already registered, increase reference count */
338 *idx = i;
339 return 0;
340 }
341 }
342
343 return -ENOENT;
344}
345EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
346
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700347int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
348{
349 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
350 int i, err = 0;
351 int free = -1;
352
353 mutex_lock(&table->mutex);
354 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
355 if (free < 0 && (table->refs[i] == 0)) {
356 free = i;
357 continue;
358 }
359
360 if (table->refs[i] &&
361 (vlan == (MLX4_VLAN_MASK &
362 be32_to_cpu(table->entries[i])))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300363 /* Vlan already registered, increase references count */
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700364 *index = i;
365 ++table->refs[i];
366 goto out;
367 }
368 }
369
Eli Cohen0926f912010-10-25 02:56:47 +0000370 if (free < 0) {
371 err = -ENOMEM;
372 goto out;
373 }
374
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700375 if (table->total == table->max) {
376 /* No free vlan entries */
377 err = -ENOSPC;
378 goto out;
379 }
380
381 /* Register new MAC */
382 table->refs[free] = 1;
383 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
384
385 err = mlx4_set_port_vlan_table(dev, port, table->entries);
386 if (unlikely(err)) {
387 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
388 table->refs[free] = 0;
389 table->entries[free] = 0;
390 goto out;
391 }
392
393 *index = free;
394 ++table->total;
395out:
396 mutex_unlock(&table->mutex);
397 return err;
398}
399EXPORT_SYMBOL_GPL(mlx4_register_vlan);
400
401void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
402{
403 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
404
405 if (index < MLX4_VLAN_REGULAR) {
406 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
407 return;
408 }
409
410 mutex_lock(&table->mutex);
411 if (!table->refs[index]) {
412 mlx4_warn(dev, "No vlan entry for index %d\n", index);
413 goto out;
414 }
415 if (--table->refs[index]) {
416 mlx4_dbg(dev, "Have more references for index %d,"
417 "no need to modify vlan table\n", index);
418 goto out;
419 }
420 table->entries[index] = 0;
421 mlx4_set_port_vlan_table(dev, port, table->entries);
422 --table->total;
423out:
424 mutex_unlock(&table->mutex);
425}
426EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700427
Jack Morgenstein9a5aa622008-11-28 21:29:46 -0800428int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
429{
430 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
431 u8 *inbuf, *outbuf;
432 int err;
433
434 inmailbox = mlx4_alloc_cmd_mailbox(dev);
435 if (IS_ERR(inmailbox))
436 return PTR_ERR(inmailbox);
437
438 outmailbox = mlx4_alloc_cmd_mailbox(dev);
439 if (IS_ERR(outmailbox)) {
440 mlx4_free_cmd_mailbox(dev, inmailbox);
441 return PTR_ERR(outmailbox);
442 }
443
444 inbuf = inmailbox->buf;
445 outbuf = outmailbox->buf;
446 memset(inbuf, 0, 256);
447 memset(outbuf, 0, 256);
448 inbuf[0] = 1;
449 inbuf[1] = 1;
450 inbuf[2] = 1;
451 inbuf[3] = 1;
452 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
453 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
454
455 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
456 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
457 if (!err)
458 *caps = *(__be32 *) (outbuf + 84);
459 mlx4_free_cmd_mailbox(dev, inmailbox);
460 mlx4_free_cmd_mailbox(dev, outmailbox);
461 return err;
462}
463
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700464int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
465{
466 struct mlx4_cmd_mailbox *mailbox;
467 int err;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700468
Roland Dreier352b09e2009-03-31 09:54:15 -0700469 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
470 return 0;
471
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700472 mailbox = mlx4_alloc_cmd_mailbox(dev);
473 if (IS_ERR(mailbox))
474 return PTR_ERR(mailbox);
475
476 memset(mailbox->buf, 0, 256);
Yevgeny Petrilin793730b2009-03-11 15:47:18 -0700477
478 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
479 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700480 MLX4_CMD_TIME_CLASS_B);
481
482 mlx4_free_cmd_mailbox(dev, mailbox);
483 return err;
484}