Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx4 / port.c
1 /*
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
46 void 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
59 void 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) - MLX4_VLAN_REGULAR;
69         table->total = 0;
70 }
71
72 static 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
93 static int mlx4_uc_steer_add(struct mlx4_dev *dev, u8 port,
94                              u64 mac, int *qpn, u8 reserve)
95 {
96         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
123 static 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
141 int 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;
146         int i, err = 0;
147         int free = -1;
148
149         if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
150                 err = mlx4_uc_steer_add(dev, port, mac, qpn, 1);
151                 if (err)
152                         return err;
153
154                 entry = kmalloc(sizeof *entry, GFP_KERNEL);
155                 if (!entry) {
156                         mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
157                         return -ENOMEM;
158                 }
159
160                 entry->mac = mac;
161                 err = radix_tree_insert(&info->mac_tree, *qpn, entry);
162                 if (err) {
163                         kfree(entry);
164                         mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
165                         return err;
166                 }
167         }
168
169         mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
170
171         mutex_lock(&table->mutex);
172         for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
173                 if (free < 0 && !table->refs[i]) {
174                         free = i;
175                         continue;
176                 }
177
178                 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
179                         /* MAC already registered, increase references count */
180                         ++table->refs[i];
181                         goto out;
182                 }
183         }
184
185         if (free < 0) {
186                 err = -ENOMEM;
187                 goto out;
188         }
189
190         mlx4_dbg(dev, "Free MAC index is %d\n", free);
191
192         if (table->total == table->max) {
193                 /* No free mac entries */
194                 err = -ENOSPC;
195                 goto out;
196         }
197
198         /* Register new MAC */
199         table->refs[free] = 1;
200         table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
201
202         err = mlx4_set_port_mac_table(dev, port, table->entries);
203         if (unlikely(err)) {
204                 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
205                 table->refs[free] = 0;
206                 table->entries[free] = 0;
207                 goto out;
208         }
209
210         if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
211                 *qpn = info->base_qpn + free;
212         ++table->total;
213 out:
214         mutex_unlock(&table->mutex);
215         return err;
216 }
217 EXPORT_SYMBOL_GPL(mlx4_register_mac);
218
219 static int validate_index(struct mlx4_dev *dev,
220                           struct mlx4_mac_table *table, int index)
221 {
222         int err = 0;
223
224         if (index < 0 || index >= table->max || !table->entries[index]) {
225                 mlx4_warn(dev, "No valid Mac entry for the given index\n");
226                 err = -EINVAL;
227         }
228         return err;
229 }
230
231 static int find_index(struct mlx4_dev *dev,
232                       struct mlx4_mac_table *table, u64 mac)
233 {
234         int i;
235         for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
236                 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
237                         return i;
238         }
239         /* Mac not found */
240         return -EINVAL;
241 }
242
243 void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
244 {
245         struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
246         struct mlx4_mac_table *table = &info->mac_table;
247         int index = qpn - info->base_qpn;
248         struct mlx4_mac_entry *entry;
249
250         if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
251                 entry = radix_tree_lookup(&info->mac_tree, qpn);
252                 if (entry) {
253                         mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1);
254                         radix_tree_delete(&info->mac_tree, qpn);
255                         index = find_index(dev, table, entry->mac);
256                         kfree(entry);
257                 }
258         }
259
260         mutex_lock(&table->mutex);
261
262         if (validate_index(dev, table, index))
263                 goto out;
264
265         /* Check whether this address has reference count */
266         if (!(--table->refs[index])) {
267                 table->entries[index] = 0;
268                 mlx4_set_port_mac_table(dev, port, table->entries);
269                 --table->total;
270         }
271 out:
272         mutex_unlock(&table->mutex);
273 }
274 EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
275
276 int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
277 {
278         struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
279         struct mlx4_mac_table *table = &info->mac_table;
280         int index = qpn - info->base_qpn;
281         struct mlx4_mac_entry *entry;
282         int err;
283
284         if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
285                 entry = radix_tree_lookup(&info->mac_tree, qpn);
286                 if (!entry)
287                         return -EINVAL;
288                 index = find_index(dev, table, entry->mac);
289                 mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0);
290                 entry->mac = new_mac;
291                 err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0);
292                 if (err || index < 0)
293                         return err;
294         }
295
296         mutex_lock(&table->mutex);
297
298         err = validate_index(dev, table, index);
299         if (err)
300                 goto out;
301
302         table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
303
304         err = mlx4_set_port_mac_table(dev, port, table->entries);
305         if (unlikely(err)) {
306                 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac);
307                 table->entries[index] = 0;
308         }
309 out:
310         mutex_unlock(&table->mutex);
311         return err;
312 }
313 EXPORT_SYMBOL_GPL(mlx4_replace_mac);
314 static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
315                                     __be32 *entries)
316 {
317         struct mlx4_cmd_mailbox *mailbox;
318         u32 in_mod;
319         int err;
320
321         mailbox = mlx4_alloc_cmd_mailbox(dev);
322         if (IS_ERR(mailbox))
323                 return PTR_ERR(mailbox);
324
325         memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
326         in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
327         err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
328                        MLX4_CMD_TIME_CLASS_B);
329
330         mlx4_free_cmd_mailbox(dev, mailbox);
331
332         return err;
333 }
334
335 int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
336 {
337         struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
338         int i;
339
340         for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
341                 if (table->refs[i] &&
342                     (vid == (MLX4_VLAN_MASK &
343                               be32_to_cpu(table->entries[i])))) {
344                         /* VLAN already registered, increase reference count */
345                         *idx = i;
346                         return 0;
347                 }
348         }
349
350         return -ENOENT;
351 }
352 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
353
354 int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
355 {
356         struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
357         int i, err = 0;
358         int free = -1;
359
360         mutex_lock(&table->mutex);
361
362         if (table->total == table->max) {
363                 /* No free vlan entries */
364                 err = -ENOSPC;
365                 goto out;
366         }
367
368         for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
369                 if (free < 0 && (table->refs[i] == 0)) {
370                         free = i;
371                         continue;
372                 }
373
374                 if (table->refs[i] &&
375                     (vlan == (MLX4_VLAN_MASK &
376                               be32_to_cpu(table->entries[i])))) {
377                         /* Vlan already registered, increase references count */
378                         *index = i;
379                         ++table->refs[i];
380                         goto out;
381                 }
382         }
383
384         if (free < 0) {
385                 err = -ENOMEM;
386                 goto out;
387         }
388
389         /* Register new MAC */
390         table->refs[free] = 1;
391         table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
392
393         err = mlx4_set_port_vlan_table(dev, port, table->entries);
394         if (unlikely(err)) {
395                 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
396                 table->refs[free] = 0;
397                 table->entries[free] = 0;
398                 goto out;
399         }
400
401         *index = free;
402         ++table->total;
403 out:
404         mutex_unlock(&table->mutex);
405         return err;
406 }
407 EXPORT_SYMBOL_GPL(mlx4_register_vlan);
408
409 void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
410 {
411         struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
412
413         if (index < MLX4_VLAN_REGULAR) {
414                 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
415                 return;
416         }
417
418         mutex_lock(&table->mutex);
419         if (!table->refs[index]) {
420                 mlx4_warn(dev, "No vlan entry for index %d\n", index);
421                 goto out;
422         }
423         if (--table->refs[index]) {
424                 mlx4_dbg(dev, "Have more references for index %d,"
425                          "no need to modify vlan table\n", index);
426                 goto out;
427         }
428         table->entries[index] = 0;
429         mlx4_set_port_vlan_table(dev, port, table->entries);
430         --table->total;
431 out:
432         mutex_unlock(&table->mutex);
433 }
434 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
435
436 int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
437 {
438         struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
439         u8 *inbuf, *outbuf;
440         int err;
441
442         inmailbox = mlx4_alloc_cmd_mailbox(dev);
443         if (IS_ERR(inmailbox))
444                 return PTR_ERR(inmailbox);
445
446         outmailbox = mlx4_alloc_cmd_mailbox(dev);
447         if (IS_ERR(outmailbox)) {
448                 mlx4_free_cmd_mailbox(dev, inmailbox);
449                 return PTR_ERR(outmailbox);
450         }
451
452         inbuf = inmailbox->buf;
453         outbuf = outmailbox->buf;
454         memset(inbuf, 0, 256);
455         memset(outbuf, 0, 256);
456         inbuf[0] = 1;
457         inbuf[1] = 1;
458         inbuf[2] = 1;
459         inbuf[3] = 1;
460         *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
461         *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
462
463         err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
464                            MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
465         if (!err)
466                 *caps = *(__be32 *) (outbuf + 84);
467         mlx4_free_cmd_mailbox(dev, inmailbox);
468         mlx4_free_cmd_mailbox(dev, outmailbox);
469         return err;
470 }
471
472 int mlx4_check_ext_port_caps(struct mlx4_dev *dev, u8 port)
473 {
474         struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
475         u8 *inbuf, *outbuf;
476         int err, packet_error;
477
478         inmailbox = mlx4_alloc_cmd_mailbox(dev);
479         if (IS_ERR(inmailbox))
480                 return PTR_ERR(inmailbox);
481
482         outmailbox = mlx4_alloc_cmd_mailbox(dev);
483         if (IS_ERR(outmailbox)) {
484                 mlx4_free_cmd_mailbox(dev, inmailbox);
485                 return PTR_ERR(outmailbox);
486         }
487
488         inbuf = inmailbox->buf;
489         outbuf = outmailbox->buf;
490         memset(inbuf, 0, 256);
491         memset(outbuf, 0, 256);
492         inbuf[0] = 1;
493         inbuf[1] = 1;
494         inbuf[2] = 1;
495         inbuf[3] = 1;
496
497         *(__be16 *) (&inbuf[16]) = MLX4_ATTR_EXTENDED_PORT_INFO;
498         *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
499
500         err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
501                            MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
502
503         packet_error = be16_to_cpu(*(__be16 *) (outbuf + 4));
504
505         dev->caps.ext_port_cap[port] = (!err && !packet_error) ?
506                                        MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO
507                                        : 0;
508
509         mlx4_free_cmd_mailbox(dev, inmailbox);
510         mlx4_free_cmd_mailbox(dev, outmailbox);
511         return err;
512 }
513
514 int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
515 {
516         struct mlx4_cmd_mailbox *mailbox;
517         int err;
518
519         if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
520                 return 0;
521
522         mailbox = mlx4_alloc_cmd_mailbox(dev);
523         if (IS_ERR(mailbox))
524                 return PTR_ERR(mailbox);
525
526         memset(mailbox->buf, 0, 256);
527
528         ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
529         err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
530                        MLX4_CMD_TIME_CLASS_B);
531
532         mlx4_free_cmd_mailbox(dev, mailbox);
533         return err;
534 }