blob: b7c4c083dffc7ae396d78df65fabf42baacd1f7a [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>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040035#include <linux/export.h>
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070036
37#include <linux/mlx4/cmd.h>
38
39#include "mlx4.h"
40
41#define MLX4_MAC_VALID (1ull << 63)
42#define MLX4_MAC_MASK 0xffffffffffffULL
43
44#define MLX4_VLAN_VALID (1u << 31)
45#define MLX4_VLAN_MASK 0xfff
46
47void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
48{
49 int i;
50
51 mutex_init(&table->mutex);
52 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
53 table->entries[i] = 0;
54 table->refs[i] = 0;
55 }
56 table->max = 1 << dev->caps.log_num_macs;
57 table->total = 0;
58}
59
60void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
61{
62 int i;
63
64 mutex_init(&table->mutex);
65 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
66 table->entries[i] = 0;
67 table->refs[i] = 0;
68 }
Yevgeny Petriline72ebf52011-10-18 01:50:29 +000069 table->max = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070070 table->total = 0;
71}
72
73static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
74 __be64 *entries)
75{
76 struct mlx4_cmd_mailbox *mailbox;
77 u32 in_mod;
78 int err;
79
80 mailbox = mlx4_alloc_cmd_mailbox(dev);
81 if (IS_ERR(mailbox))
82 return PTR_ERR(mailbox);
83
84 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
85
86 in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
87 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
88 MLX4_CMD_TIME_CLASS_B);
89
90 mlx4_free_cmd_mailbox(dev, mailbox);
91 return err;
92}
93
Yevgeny Petrilin16792002011-03-22 22:38:31 +000094static int mlx4_uc_steer_add(struct mlx4_dev *dev, u8 port,
95 u64 mac, int *qpn, u8 reserve)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -070096{
Yevgeny Petrilin16792002011-03-22 22:38:31 +000097 struct mlx4_qp qp;
98 u8 gid[16] = {0};
99 int err;
100
101 if (reserve) {
102 err = mlx4_qp_reserve_range(dev, 1, 1, qpn);
103 if (err) {
104 mlx4_err(dev, "Failed to reserve qp for mac registration\n");
105 return err;
106 }
107 }
108 qp.qpn = *qpn;
109
110 mac &= 0xffffffffffffULL;
111 mac = cpu_to_be64(mac << 16);
112 memcpy(&gid[10], &mac, ETH_ALEN);
113 gid[5] = port;
114 gid[7] = MLX4_UC_STEER << 1;
115
116 err = mlx4_qp_attach_common(dev, &qp, gid, 0,
117 MLX4_PROT_ETH, MLX4_UC_STEER);
118 if (err && reserve)
119 mlx4_qp_release_range(dev, *qpn, 1);
120
121 return err;
122}
123
124static void mlx4_uc_steer_release(struct mlx4_dev *dev, u8 port,
125 u64 mac, int qpn, u8 free)
126{
127 struct mlx4_qp qp;
128 u8 gid[16] = {0};
129
130 qp.qpn = qpn;
131 mac &= 0xffffffffffffULL;
132 mac = cpu_to_be64(mac << 16);
133 memcpy(&gid[10], &mac, ETH_ALEN);
134 gid[5] = port;
135 gid[7] = MLX4_UC_STEER << 1;
136
137 mlx4_qp_detach_common(dev, &qp, gid, MLX4_PROT_ETH, MLX4_UC_STEER);
138 if (free)
139 mlx4_qp_release_range(dev, qpn, 1);
140}
141
142int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *qpn, u8 wrap)
143{
144 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
145 struct mlx4_mac_table *table = &info->mac_table;
146 struct mlx4_mac_entry *entry;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700147 int i, err = 0;
148 int free = -1;
149
Or Gerlitzccf86322011-07-07 19:19:29 +0000150 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000151 err = mlx4_uc_steer_add(dev, port, mac, qpn, 1);
152 if (!err) {
153 entry = kmalloc(sizeof *entry, GFP_KERNEL);
154 if (!entry) {
155 mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
156 return -ENOMEM;
157 }
158 entry->mac = mac;
159 err = radix_tree_insert(&info->mac_tree, *qpn, entry);
160 if (err) {
161 mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
162 return err;
163 }
164 } else
165 return err;
166 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700167 mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
168 mutex_lock(&table->mutex);
169 for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
170 if (free < 0 && !table->refs[i]) {
171 free = i;
172 continue;
173 }
174
175 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300176 /* MAC already registered, increase references count */
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700177 ++table->refs[i];
178 goto out;
179 }
180 }
Eli Cohen0926f912010-10-25 02:56:47 +0000181
182 if (free < 0) {
183 err = -ENOMEM;
184 goto out;
185 }
186
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700187 mlx4_dbg(dev, "Free MAC index is %d\n", free);
188
189 if (table->total == table->max) {
190 /* No free mac entries */
191 err = -ENOSPC;
192 goto out;
193 }
194
195 /* Register new MAC */
196 table->refs[free] = 1;
197 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
198
199 err = mlx4_set_port_mac_table(dev, port, table->entries);
200 if (unlikely(err)) {
201 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
202 table->refs[free] = 0;
203 table->entries[free] = 0;
204 goto out;
205 }
206
Or Gerlitzccf86322011-07-07 19:19:29 +0000207 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000208 *qpn = info->base_qpn + free;
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700209 ++table->total;
210out:
211 mutex_unlock(&table->mutex);
212 return err;
213}
214EXPORT_SYMBOL_GPL(mlx4_register_mac);
215
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000216static int validate_index(struct mlx4_dev *dev,
217 struct mlx4_mac_table *table, int index)
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700218{
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000219 int err = 0;
220
221 if (index < 0 || index >= table->max || !table->entries[index]) {
222 mlx4_warn(dev, "No valid Mac entry for the given index\n");
223 err = -EINVAL;
224 }
225 return err;
226}
227
228static int find_index(struct mlx4_dev *dev,
229 struct mlx4_mac_table *table, u64 mac)
230{
231 int i;
232 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
233 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
234 return i;
235 }
236 /* Mac not found */
237 return -EINVAL;
238}
239
240void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
241{
242 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
243 struct mlx4_mac_table *table = &info->mac_table;
244 int index = qpn - info->base_qpn;
245 struct mlx4_mac_entry *entry;
246
Or Gerlitzccf86322011-07-07 19:19:29 +0000247 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000248 entry = radix_tree_lookup(&info->mac_tree, qpn);
249 if (entry) {
250 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1);
251 radix_tree_delete(&info->mac_tree, qpn);
252 index = find_index(dev, table, entry->mac);
253 kfree(entry);
254 }
255 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700256
257 mutex_lock(&table->mutex);
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000258
259 if (validate_index(dev, table, index))
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700260 goto out;
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000261
Yevgeny Petrilin20e72a42011-08-04 01:05:12 +0000262 /* Check whether this address has reference count */
263 if (!(--table->refs[index])) {
264 table->entries[index] = 0;
265 mlx4_set_port_mac_table(dev, port, table->entries);
266 --table->total;
267 }
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700268out:
269 mutex_unlock(&table->mutex);
270}
271EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
272
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000273int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
274{
275 struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
276 struct mlx4_mac_table *table = &info->mac_table;
277 int index = qpn - info->base_qpn;
278 struct mlx4_mac_entry *entry;
279 int err;
280
Or Gerlitzccf86322011-07-07 19:19:29 +0000281 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
Yevgeny Petrilin16792002011-03-22 22:38:31 +0000282 entry = radix_tree_lookup(&info->mac_tree, qpn);
283 if (!entry)
284 return -EINVAL;
285 index = find_index(dev, table, entry->mac);
286 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0);
287 entry->mac = new_mac;
288 err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0);
289 if (err || index < 0)
290 return err;
291 }
292
293 mutex_lock(&table->mutex);
294
295 err = validate_index(dev, table, index);
296 if (err)
297 goto out;
298
299 table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
300
301 err = mlx4_set_port_mac_table(dev, port, table->entries);
302 if (unlikely(err)) {
303 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac);
304 table->entries[index] = 0;
305 }
306out:
307 mutex_unlock(&table->mutex);
308 return err;
309}
310EXPORT_SYMBOL_GPL(mlx4_replace_mac);
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700311static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
312 __be32 *entries)
313{
314 struct mlx4_cmd_mailbox *mailbox;
315 u32 in_mod;
316 int err;
317
318 mailbox = mlx4_alloc_cmd_mailbox(dev);
319 if (IS_ERR(mailbox))
320 return PTR_ERR(mailbox);
321
322 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
323 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
324 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
325 MLX4_CMD_TIME_CLASS_B);
326
327 mlx4_free_cmd_mailbox(dev, mailbox);
328
329 return err;
330}
331
Eli Cohen4c3eb3c2010-08-26 17:19:22 +0300332int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
333{
334 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
335 int i;
336
337 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
338 if (table->refs[i] &&
339 (vid == (MLX4_VLAN_MASK &
340 be32_to_cpu(table->entries[i])))) {
341 /* VLAN already registered, increase reference count */
342 *idx = i;
343 return 0;
344 }
345 }
346
347 return -ENOENT;
348}
349EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
350
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700351int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
352{
353 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
354 int i, err = 0;
355 int free = -1;
356
357 mutex_lock(&table->mutex);
Yevgeny Petriline72ebf52011-10-18 01:50:29 +0000358
359 if (table->total == table->max) {
360 /* No free vlan entries */
361 err = -ENOSPC;
362 goto out;
363 }
364
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700365 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
366 if (free < 0 && (table->refs[i] == 0)) {
367 free = i;
368 continue;
369 }
370
371 if (table->refs[i] &&
372 (vlan == (MLX4_VLAN_MASK &
373 be32_to_cpu(table->entries[i])))) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300374 /* Vlan already registered, increase references count */
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700375 *index = i;
376 ++table->refs[i];
377 goto out;
378 }
379 }
380
Eli Cohen0926f912010-10-25 02:56:47 +0000381 if (free < 0) {
382 err = -ENOMEM;
383 goto out;
384 }
385
Yevgeny Petrilin2a2336f2008-10-22 11:44:46 -0700386 /* Register new MAC */
387 table->refs[free] = 1;
388 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
389
390 err = mlx4_set_port_vlan_table(dev, port, table->entries);
391 if (unlikely(err)) {
392 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
393 table->refs[free] = 0;
394 table->entries[free] = 0;
395 goto out;
396 }
397
398 *index = free;
399 ++table->total;
400out:
401 mutex_unlock(&table->mutex);
402 return err;
403}
404EXPORT_SYMBOL_GPL(mlx4_register_vlan);
405
406void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
407{
408 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
409
410 if (index < MLX4_VLAN_REGULAR) {
411 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
412 return;
413 }
414
415 mutex_lock(&table->mutex);
416 if (!table->refs[index]) {
417 mlx4_warn(dev, "No vlan entry for index %d\n", index);
418 goto out;
419 }
420 if (--table->refs[index]) {
421 mlx4_dbg(dev, "Have more references for index %d,"
422 "no need to modify vlan table\n", index);
423 goto out;
424 }
425 table->entries[index] = 0;
426 mlx4_set_port_vlan_table(dev, port, table->entries);
427 --table->total;
428out:
429 mutex_unlock(&table->mutex);
430}
431EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700432
Jack Morgenstein9a5aa622008-11-28 21:29:46 -0800433int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
434{
435 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
436 u8 *inbuf, *outbuf;
437 int err;
438
439 inmailbox = mlx4_alloc_cmd_mailbox(dev);
440 if (IS_ERR(inmailbox))
441 return PTR_ERR(inmailbox);
442
443 outmailbox = mlx4_alloc_cmd_mailbox(dev);
444 if (IS_ERR(outmailbox)) {
445 mlx4_free_cmd_mailbox(dev, inmailbox);
446 return PTR_ERR(outmailbox);
447 }
448
449 inbuf = inmailbox->buf;
450 outbuf = outmailbox->buf;
451 memset(inbuf, 0, 256);
452 memset(outbuf, 0, 256);
453 inbuf[0] = 1;
454 inbuf[1] = 1;
455 inbuf[2] = 1;
456 inbuf[3] = 1;
457 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
458 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
459
460 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
461 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
462 if (!err)
463 *caps = *(__be32 *) (outbuf + 84);
464 mlx4_free_cmd_mailbox(dev, inmailbox);
465 mlx4_free_cmd_mailbox(dev, outmailbox);
466 return err;
467}
468
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700469int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
470{
471 struct mlx4_cmd_mailbox *mailbox;
472 int err;
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700473
Roland Dreier352b09e2009-03-31 09:54:15 -0700474 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
475 return 0;
476
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700477 mailbox = mlx4_alloc_cmd_mailbox(dev);
478 if (IS_ERR(mailbox))
479 return PTR_ERR(mailbox);
480
481 memset(mailbox->buf, 0, 256);
Yevgeny Petrilin793730b2009-03-11 15:47:18 -0700482
483 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
484 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
Yevgeny Petrilin7ff93f82008-10-22 15:38:42 -0700485 MLX4_CMD_TIME_CLASS_B);
486
487 mlx4_free_cmd_mailbox(dev, mailbox);
488 return err;
489}