Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / net / ethernet / mellanox / mlx4 / port.c
blob34104ae233434cbaf4041736dc8854819ba479b9
1 /*
2 * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
33 #include <linux/errno.h>
34 #include <linux/if_ether.h>
35 #include <linux/export.h>
37 #include <linux/mlx4/cmd.h>
39 #include "mlx4.h"
41 #define MLX4_MAC_VALID (1ull << 63)
42 #define MLX4_MAC_MASK 0xffffffffffffULL
44 #define MLX4_VLAN_VALID (1u << 31)
45 #define MLX4_VLAN_MASK 0xfff
47 void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
49 int i;
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;
56 table->max = 1 << dev->caps.log_num_macs;
57 table->total = 0;
60 void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
62 int i;
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;
69 table->max = 1 << dev->caps.log_num_vlans;
70 table->total = 0;
73 static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
74 __be64 *entries)
76 struct mlx4_cmd_mailbox *mailbox;
77 u32 in_mod;
78 int err;
80 mailbox = mlx4_alloc_cmd_mailbox(dev);
81 if (IS_ERR(mailbox))
82 return PTR_ERR(mailbox);
84 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
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);
90 mlx4_free_cmd_mailbox(dev, mailbox);
91 return err;
94 static int mlx4_uc_steer_add(struct mlx4_dev *dev, u8 port,
95 u64 mac, int *qpn, u8 reserve)
97 struct mlx4_qp qp;
98 u8 gid[16] = {0};
99 int err;
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;
108 qp.qpn = *qpn;
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;
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);
121 return err;
124 static void mlx4_uc_steer_release(struct mlx4_dev *dev, u8 port,
125 u64 mac, int qpn, u8 free)
127 struct mlx4_qp qp;
128 u8 gid[16] = {0};
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;
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);
142 int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *qpn, u8 wrap)
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;
147 int i, err = 0;
148 int free = -1;
150 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
151 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;
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;
164 } else
165 return err;
167 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;
175 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
176 /* MAC already registered, increase references count */
177 ++table->refs[i];
178 goto out;
182 if (free < 0) {
183 err = -ENOMEM;
184 goto out;
187 mlx4_dbg(dev, "Free MAC index is %d\n", free);
189 if (table->total == table->max) {
190 /* No free mac entries */
191 err = -ENOSPC;
192 goto out;
195 /* Register new MAC */
196 table->refs[free] = 1;
197 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
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;
207 if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
208 *qpn = info->base_qpn + free;
209 ++table->total;
210 out:
211 mutex_unlock(&table->mutex);
212 return err;
214 EXPORT_SYMBOL_GPL(mlx4_register_mac);
216 static int validate_index(struct mlx4_dev *dev,
217 struct mlx4_mac_table *table, int index)
219 int err = 0;
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;
225 return err;
228 static int find_index(struct mlx4_dev *dev,
229 struct mlx4_mac_table *table, u64 mac)
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;
236 /* Mac not found */
237 return -EINVAL;
240 void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
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;
247 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
248 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);
257 mutex_lock(&table->mutex);
259 if (validate_index(dev, table, index))
260 goto out;
262 /* 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;
268 out:
269 mutex_unlock(&table->mutex);
271 EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
273 int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
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;
281 if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
282 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;
293 mutex_lock(&table->mutex);
295 err = validate_index(dev, table, index);
296 if (err)
297 goto out;
299 table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
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;
306 out:
307 mutex_unlock(&table->mutex);
308 return err;
310 EXPORT_SYMBOL_GPL(mlx4_replace_mac);
311 static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
312 __be32 *entries)
314 struct mlx4_cmd_mailbox *mailbox;
315 u32 in_mod;
316 int err;
318 mailbox = mlx4_alloc_cmd_mailbox(dev);
319 if (IS_ERR(mailbox))
320 return PTR_ERR(mailbox);
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);
327 mlx4_free_cmd_mailbox(dev, mailbox);
329 return err;
332 int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
334 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
335 int i;
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;
347 return -ENOENT;
349 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
351 int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
353 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
354 int i, err = 0;
355 int free = -1;
357 mutex_lock(&table->mutex);
358 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
359 if (free < 0 && (table->refs[i] == 0)) {
360 free = i;
361 continue;
364 if (table->refs[i] &&
365 (vlan == (MLX4_VLAN_MASK &
366 be32_to_cpu(table->entries[i])))) {
367 /* Vlan already registered, increase references count */
368 *index = i;
369 ++table->refs[i];
370 goto out;
374 if (free < 0) {
375 err = -ENOMEM;
376 goto out;
379 if (table->total == table->max) {
380 /* No free vlan entries */
381 err = -ENOSPC;
382 goto out;
385 /* Register new MAC */
386 table->refs[free] = 1;
387 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
389 err = mlx4_set_port_vlan_table(dev, port, table->entries);
390 if (unlikely(err)) {
391 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
392 table->refs[free] = 0;
393 table->entries[free] = 0;
394 goto out;
397 *index = free;
398 ++table->total;
399 out:
400 mutex_unlock(&table->mutex);
401 return err;
403 EXPORT_SYMBOL_GPL(mlx4_register_vlan);
405 void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
407 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
409 if (index < MLX4_VLAN_REGULAR) {
410 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
411 return;
414 mutex_lock(&table->mutex);
415 if (!table->refs[index]) {
416 mlx4_warn(dev, "No vlan entry for index %d\n", index);
417 goto out;
419 if (--table->refs[index]) {
420 mlx4_dbg(dev, "Have more references for index %d,"
421 "no need to modify vlan table\n", index);
422 goto out;
424 table->entries[index] = 0;
425 mlx4_set_port_vlan_table(dev, port, table->entries);
426 --table->total;
427 out:
428 mutex_unlock(&table->mutex);
430 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
432 int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
434 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
435 u8 *inbuf, *outbuf;
436 int err;
438 inmailbox = mlx4_alloc_cmd_mailbox(dev);
439 if (IS_ERR(inmailbox))
440 return PTR_ERR(inmailbox);
442 outmailbox = mlx4_alloc_cmd_mailbox(dev);
443 if (IS_ERR(outmailbox)) {
444 mlx4_free_cmd_mailbox(dev, inmailbox);
445 return PTR_ERR(outmailbox);
448 inbuf = inmailbox->buf;
449 outbuf = outmailbox->buf;
450 memset(inbuf, 0, 256);
451 memset(outbuf, 0, 256);
452 inbuf[0] = 1;
453 inbuf[1] = 1;
454 inbuf[2] = 1;
455 inbuf[3] = 1;
456 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
457 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
459 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
460 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
461 if (!err)
462 *caps = *(__be32 *) (outbuf + 84);
463 mlx4_free_cmd_mailbox(dev, inmailbox);
464 mlx4_free_cmd_mailbox(dev, outmailbox);
465 return err;
468 int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
470 struct mlx4_cmd_mailbox *mailbox;
471 int err;
473 if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
474 return 0;
476 mailbox = mlx4_alloc_cmd_mailbox(dev);
477 if (IS_ERR(mailbox))
478 return PTR_ERR(mailbox);
480 memset(mailbox->buf, 0, 256);
482 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
483 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
484 MLX4_CMD_TIME_CLASS_B);
486 mlx4_free_cmd_mailbox(dev, mailbox);
487 return err;