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 #define MLX4_MAC_VALID (1ull << 63)
41 #define MLX4_MAC_MASK 0xffffffffffffULL
43 #define MLX4_VLAN_VALID (1u << 31)
44 #define MLX4_VLAN_MASK 0xfff
46 void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
50 mutex_init(&table->mutex);
51 for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
52 table->entries[i] = 0;
55 table->max = 1 << dev->caps.log_num_macs;
59 void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
63 mutex_init(&table->mutex);
64 for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
65 table->entries[i] = 0;
68 table->max = 1 << dev->caps.log_num_vlans;
72 static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
75 struct mlx4_cmd_mailbox *mailbox;
79 mailbox = mlx4_alloc_cmd_mailbox(dev);
81 return PTR_ERR(mailbox);
83 memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
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);
89 mlx4_free_cmd_mailbox(dev, mailbox);
93 int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *index)
95 struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
99 mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
100 mutex_lock(&table->mutex);
101 for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
102 if (free < 0 && !table->refs[i]) {
107 if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
108 /* MAC already registered, increase refernce count */
114 mlx4_dbg(dev, "Free MAC index is %d\n", free);
116 if (table->total == table->max) {
117 /* No free mac entries */
122 /* Register new MAC */
123 table->refs[free] = 1;
124 table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
126 err = mlx4_set_port_mac_table(dev, port, table->entries);
128 mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
129 table->refs[free] = 0;
130 table->entries[free] = 0;
137 mutex_unlock(&table->mutex);
140 EXPORT_SYMBOL_GPL(mlx4_register_mac);
142 void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int index)
144 struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
146 mutex_lock(&table->mutex);
147 if (!table->refs[index]) {
148 mlx4_warn(dev, "No MAC entry for index %d\n", index);
151 if (--table->refs[index]) {
152 mlx4_warn(dev, "Have more references for index %d,"
153 "no need to modify MAC table\n", index);
156 table->entries[index] = 0;
157 mlx4_set_port_mac_table(dev, port, table->entries);
160 mutex_unlock(&table->mutex);
162 EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
164 static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
167 struct mlx4_cmd_mailbox *mailbox;
171 mailbox = mlx4_alloc_cmd_mailbox(dev);
173 return PTR_ERR(mailbox);
175 memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
176 in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
177 err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
178 MLX4_CMD_TIME_CLASS_B);
180 mlx4_free_cmd_mailbox(dev, mailbox);
185 int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
187 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
191 mutex_lock(&table->mutex);
192 for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
193 if (free < 0 && (table->refs[i] == 0)) {
198 if (table->refs[i] &&
199 (vlan == (MLX4_VLAN_MASK &
200 be32_to_cpu(table->entries[i])))) {
201 /* Vlan already registered, increase refernce count */
208 if (table->total == table->max) {
209 /* No free vlan entries */
214 /* Register new MAC */
215 table->refs[free] = 1;
216 table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
218 err = mlx4_set_port_vlan_table(dev, port, table->entries);
220 mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
221 table->refs[free] = 0;
222 table->entries[free] = 0;
229 mutex_unlock(&table->mutex);
232 EXPORT_SYMBOL_GPL(mlx4_register_vlan);
234 void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
236 struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
238 if (index < MLX4_VLAN_REGULAR) {
239 mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
243 mutex_lock(&table->mutex);
244 if (!table->refs[index]) {
245 mlx4_warn(dev, "No vlan entry for index %d\n", index);
248 if (--table->refs[index]) {
249 mlx4_dbg(dev, "Have more references for index %d,"
250 "no need to modify vlan table\n", index);
253 table->entries[index] = 0;
254 mlx4_set_port_vlan_table(dev, port, table->entries);
257 mutex_unlock(&table->mutex);
259 EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
261 int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
263 struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
267 inmailbox = mlx4_alloc_cmd_mailbox(dev);
268 if (IS_ERR(inmailbox))
269 return PTR_ERR(inmailbox);
271 outmailbox = mlx4_alloc_cmd_mailbox(dev);
272 if (IS_ERR(outmailbox)) {
273 mlx4_free_cmd_mailbox(dev, inmailbox);
274 return PTR_ERR(outmailbox);
277 inbuf = inmailbox->buf;
278 outbuf = outmailbox->buf;
279 memset(inbuf, 0, 256);
280 memset(outbuf, 0, 256);
285 *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
286 *(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
288 err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
289 MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
291 *caps = *(__be32 *) (outbuf + 84);
292 mlx4_free_cmd_mailbox(dev, inmailbox);
293 mlx4_free_cmd_mailbox(dev, outmailbox);
297 int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
299 struct mlx4_cmd_mailbox *mailbox;
301 u8 is_eth = dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH;
303 mailbox = mlx4_alloc_cmd_mailbox(dev);
305 return PTR_ERR(mailbox);
307 memset(mailbox->buf, 0, 256);
309 ((u8 *) mailbox->buf)[3] = 6;
310 ((__be16 *) mailbox->buf)[4] = cpu_to_be16(1 << 15);
311 ((__be16 *) mailbox->buf)[6] = cpu_to_be16(1 << 15);
313 ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
314 err = mlx4_cmd(dev, mailbox->dma, port, is_eth, MLX4_CMD_SET_PORT,
315 MLX4_CMD_TIME_CLASS_B);
317 mlx4_free_cmd_mailbox(dev, mailbox);