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
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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
33 #include <linux/errno.h>
34 #include <linux/if_ether.h>
36 #include <linux/mlx4/cmd.h>
40 int mlx4_ib_set_4k_mtu = 0;
41 module_param_named(set_4k_mtu, mlx4_ib_set_4k_mtu, int, 0444);
42 MODULE_PARM_DESC(set_4k_mtu, "attempt to set 4K MTU to all ConnectX ports");
44 #define MLX4_MAC_VALID (1ull << 63)
45 #define MLX4_MAC_MASK 0xffffffffffffULL
47 #define MLX4_VLAN_VALID (1u << 31)
48 #define MLX4_VLAN_MASK 0xfff
50 void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
54 mutex_init(&table->mutex);
55 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
56 table->entries[i] = 0;
59 table->max = 1 << dev->caps.log_num_macs;
63 void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
67 mutex_init(&table->mutex);
68 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
69 table->entries[i] = 0;
72 table->max = 1 << dev->caps.log_num_vlans;
76 static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
79 struct mlx4_cmd_mailbox *mailbox;
83 mailbox = mlx4_alloc_cmd_mailbox(dev);
85 return PTR_ERR(mailbox);
87 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
89 in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
90 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
91 MLX4_CMD_TIME_CLASS_B);
93 mlx4_free_cmd_mailbox(dev, mailbox);
97 int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *index)
99 struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
103 mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
104 mutex_lock(&table->mutex);
105 for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
106 if (free < 0 && !table->refs[i]) {
111 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
112 /* MAC already registered, increase refernce count */
124 mlx4_dbg(dev, "Free MAC index is %d\n", free);
126 if (table->total == table->max) {
127 /* No free mac entries */
132 /* Register new MAC */
133 table->refs[free] = 1;
134 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
136 err = mlx4_set_port_mac_table(dev, port, table->entries);
138 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
139 table->refs[free] = 0;
140 table->entries[free] = 0;
147 mutex_unlock(&table->mutex);
150 EXPORT_SYMBOL_GPL(mlx4_register_mac);
152 void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int index)
154 struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
156 mutex_lock(&table->mutex);
157 if (!table->refs[index]) {
158 mlx4_warn(dev, "No MAC entry for index %d\n", index);
161 if (--table->refs[index]) {
162 mlx4_warn(dev, "Have more references for index %d,"
163 "no need to modify MAC table\n", index);
166 table->entries[index] = 0;
167 mlx4_set_port_mac_table(dev, port, table->entries);
170 mutex_unlock(&table->mutex);
172 EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
174 static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
177 struct mlx4_cmd_mailbox *mailbox;
181 mailbox = mlx4_alloc_cmd_mailbox(dev);
183 return PTR_ERR(mailbox);
185 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
186 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
187 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
188 MLX4_CMD_TIME_CLASS_B);
190 mlx4_free_cmd_mailbox(dev, mailbox);
195 int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
197 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
200 for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
201 if (table->refs[i] &&
202 (vid == (MLX4_VLAN_MASK &
203 be32_to_cpu(table->entries[i])))) {
204 /* Vlan already registered, increase refernce count */
212 EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
214 int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
216 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
220 mutex_lock(&table->mutex);
221 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
222 if (free < 0 && (table->refs[i] == 0)) {
227 if (table->refs[i] &&
228 (vlan == (MLX4_VLAN_MASK &
229 be32_to_cpu(table->entries[i])))) {
230 /* Vlan already registered, increase refernce count */
242 if (table->total == table->max) {
243 /* No free vlan entries */
248 /* Register new MAC */
249 table->refs[free] = 1;
250 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
252 err = mlx4_set_port_vlan_table(dev, port, table->entries);
254 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
255 table->refs[free] = 0;
256 table->entries[free] = 0;
263 mutex_unlock(&table->mutex);
266 EXPORT_SYMBOL_GPL(mlx4_register_vlan);
268 void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
270 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
272 if (index < MLX4_VLAN_REGULAR) {
273 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
277 mutex_lock(&table->mutex);
278 if (!table->refs[index]) {
279 mlx4_warn(dev, "No vlan entry for index %d\n", index);
282 if (--table->refs[index]) {
283 mlx4_dbg(dev, "Have more references for index %d,"
284 "no need to modify vlan table\n", index);
287 table->entries[index] = 0;
288 mlx4_set_port_vlan_table(dev, port, table->entries);
291 mutex_unlock(&table->mutex);
293 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
295 int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
297 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
301 inmailbox = mlx4_alloc_cmd_mailbox(dev);
302 if (IS_ERR(inmailbox))
303 return PTR_ERR(inmailbox);
305 outmailbox = mlx4_alloc_cmd_mailbox(dev);
306 if (IS_ERR(outmailbox)) {
307 mlx4_free_cmd_mailbox(dev, inmailbox);
308 return PTR_ERR(outmailbox);
311 inbuf = inmailbox->buf;
312 outbuf = outmailbox->buf;
313 memset(inbuf, 0, 256);
314 memset(outbuf, 0, 256);
319 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
320 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
322 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
323 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
325 *caps = *(__be32 *) (outbuf + 84);
326 mlx4_free_cmd_mailbox(dev, inmailbox);
327 mlx4_free_cmd_mailbox(dev, outmailbox);
331 int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
333 struct mlx4_cmd_mailbox *mailbox;
336 if (dev->caps.port_type[port] != MLX4_PORT_TYPE_IB)
339 mailbox = mlx4_alloc_cmd_mailbox(dev);
341 return PTR_ERR(mailbox);
343 memset(mailbox->buf, 0, 256);
345 if (mlx4_ib_set_4k_mtu)
346 ((__be32 *) mailbox->buf)[0] |= cpu_to_be32((1 << 22) | (1 << 21) | (5 << 12) | (2 << 4));
348 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
349 err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
350 MLX4_CMD_TIME_CLASS_B);
352 mlx4_free_cmd_mailbox(dev, mailbox);