15a2cc190SJeff Kirsher /*
25a2cc190SJeff Kirsher  * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
35a2cc190SJeff Kirsher  *
45a2cc190SJeff Kirsher  * This software is available to you under a choice of one of two
55a2cc190SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
65a2cc190SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
75a2cc190SJeff Kirsher  * COPYING in the main directory of this source tree, or the
85a2cc190SJeff Kirsher  * OpenIB.org BSD license below:
95a2cc190SJeff Kirsher  *
105a2cc190SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
115a2cc190SJeff Kirsher  *     without modification, are permitted provided that the following
125a2cc190SJeff Kirsher  *     conditions are met:
135a2cc190SJeff Kirsher  *
145a2cc190SJeff Kirsher  *      - Redistributions of source code must retain the above
155a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
165a2cc190SJeff Kirsher  *        disclaimer.
175a2cc190SJeff Kirsher  *
185a2cc190SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
195a2cc190SJeff Kirsher  *        copyright notice, this list of conditions and the following
205a2cc190SJeff Kirsher  *        disclaimer in the documentation and/or other materials
215a2cc190SJeff Kirsher  *        provided with the distribution.
225a2cc190SJeff Kirsher  *
235a2cc190SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
245a2cc190SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
255a2cc190SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
265a2cc190SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
275a2cc190SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
285a2cc190SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
295a2cc190SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
305a2cc190SJeff Kirsher  * SOFTWARE.
315a2cc190SJeff Kirsher  */
325a2cc190SJeff Kirsher 
335a2cc190SJeff Kirsher #include <linux/errno.h>
345a2cc190SJeff Kirsher #include <linux/if_ether.h>
355a2cc190SJeff Kirsher 
365a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h>
375a2cc190SJeff Kirsher 
385a2cc190SJeff Kirsher #include "mlx4.h"
395a2cc190SJeff Kirsher 
405a2cc190SJeff Kirsher #define MLX4_MAC_VALID		(1ull << 63)
415a2cc190SJeff Kirsher #define MLX4_MAC_MASK		0xffffffffffffULL
425a2cc190SJeff Kirsher 
435a2cc190SJeff Kirsher #define MLX4_VLAN_VALID		(1u << 31)
445a2cc190SJeff Kirsher #define MLX4_VLAN_MASK		0xfff
455a2cc190SJeff Kirsher 
465a2cc190SJeff Kirsher void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
475a2cc190SJeff Kirsher {
485a2cc190SJeff Kirsher 	int i;
495a2cc190SJeff Kirsher 
505a2cc190SJeff Kirsher 	mutex_init(&table->mutex);
515a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
525a2cc190SJeff Kirsher 		table->entries[i] = 0;
535a2cc190SJeff Kirsher 		table->refs[i]	 = 0;
545a2cc190SJeff Kirsher 	}
555a2cc190SJeff Kirsher 	table->max   = 1 << dev->caps.log_num_macs;
565a2cc190SJeff Kirsher 	table->total = 0;
575a2cc190SJeff Kirsher }
585a2cc190SJeff Kirsher 
595a2cc190SJeff Kirsher void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
605a2cc190SJeff Kirsher {
615a2cc190SJeff Kirsher 	int i;
625a2cc190SJeff Kirsher 
635a2cc190SJeff Kirsher 	mutex_init(&table->mutex);
645a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
655a2cc190SJeff Kirsher 		table->entries[i] = 0;
665a2cc190SJeff Kirsher 		table->refs[i]	 = 0;
675a2cc190SJeff Kirsher 	}
68e72ebf5aSYevgeny Petrilin 	table->max   = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR;
695a2cc190SJeff Kirsher 	table->total = 0;
705a2cc190SJeff Kirsher }
715a2cc190SJeff Kirsher 
725a2cc190SJeff Kirsher static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
735a2cc190SJeff Kirsher 				   __be64 *entries)
745a2cc190SJeff Kirsher {
755a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
765a2cc190SJeff Kirsher 	u32 in_mod;
775a2cc190SJeff Kirsher 	int err;
785a2cc190SJeff Kirsher 
795a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
805a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
815a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
825a2cc190SJeff Kirsher 
835a2cc190SJeff Kirsher 	memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
845a2cc190SJeff Kirsher 
855a2cc190SJeff Kirsher 	in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
865a2cc190SJeff Kirsher 	err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
875a2cc190SJeff Kirsher 		       MLX4_CMD_TIME_CLASS_B);
885a2cc190SJeff Kirsher 
895a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
905a2cc190SJeff Kirsher 	return err;
915a2cc190SJeff Kirsher }
925a2cc190SJeff Kirsher 
935a2cc190SJeff Kirsher static int mlx4_uc_steer_add(struct mlx4_dev *dev, u8 port,
945a2cc190SJeff Kirsher 			     u64 mac, int *qpn, u8 reserve)
955a2cc190SJeff Kirsher {
965a2cc190SJeff Kirsher 	struct mlx4_qp qp;
975a2cc190SJeff Kirsher 	u8 gid[16] = {0};
985a2cc190SJeff Kirsher 	int err;
995a2cc190SJeff Kirsher 
1005a2cc190SJeff Kirsher 	if (reserve) {
1015a2cc190SJeff Kirsher 		err = mlx4_qp_reserve_range(dev, 1, 1, qpn);
1025a2cc190SJeff Kirsher 		if (err) {
1035a2cc190SJeff Kirsher 			mlx4_err(dev, "Failed to reserve qp for mac registration\n");
1045a2cc190SJeff Kirsher 			return err;
1055a2cc190SJeff Kirsher 		}
1065a2cc190SJeff Kirsher 	}
1075a2cc190SJeff Kirsher 	qp.qpn = *qpn;
1085a2cc190SJeff Kirsher 
1095a2cc190SJeff Kirsher 	mac &= 0xffffffffffffULL;
1105a2cc190SJeff Kirsher 	mac = cpu_to_be64(mac << 16);
1115a2cc190SJeff Kirsher 	memcpy(&gid[10], &mac, ETH_ALEN);
1125a2cc190SJeff Kirsher 	gid[5] = port;
1135a2cc190SJeff Kirsher 	gid[7] = MLX4_UC_STEER << 1;
1145a2cc190SJeff Kirsher 
1155a2cc190SJeff Kirsher 	err = mlx4_qp_attach_common(dev, &qp, gid, 0,
1165a2cc190SJeff Kirsher 				    MLX4_PROT_ETH, MLX4_UC_STEER);
1175a2cc190SJeff Kirsher 	if (err && reserve)
1185a2cc190SJeff Kirsher 		mlx4_qp_release_range(dev, *qpn, 1);
1195a2cc190SJeff Kirsher 
1205a2cc190SJeff Kirsher 	return err;
1215a2cc190SJeff Kirsher }
1225a2cc190SJeff Kirsher 
1235a2cc190SJeff Kirsher static void mlx4_uc_steer_release(struct mlx4_dev *dev, u8 port,
1245a2cc190SJeff Kirsher 				  u64 mac, int qpn, u8 free)
1255a2cc190SJeff Kirsher {
1265a2cc190SJeff Kirsher 	struct mlx4_qp qp;
1275a2cc190SJeff Kirsher 	u8 gid[16] = {0};
1285a2cc190SJeff Kirsher 
1295a2cc190SJeff Kirsher 	qp.qpn = qpn;
1305a2cc190SJeff Kirsher 	mac &= 0xffffffffffffULL;
1315a2cc190SJeff Kirsher 	mac = cpu_to_be64(mac << 16);
1325a2cc190SJeff Kirsher 	memcpy(&gid[10], &mac, ETH_ALEN);
1335a2cc190SJeff Kirsher 	gid[5] = port;
1345a2cc190SJeff Kirsher 	gid[7] = MLX4_UC_STEER << 1;
1355a2cc190SJeff Kirsher 
1365a2cc190SJeff Kirsher 	mlx4_qp_detach_common(dev, &qp, gid, MLX4_PROT_ETH, MLX4_UC_STEER);
1375a2cc190SJeff Kirsher 	if (free)
1385a2cc190SJeff Kirsher 		mlx4_qp_release_range(dev, qpn, 1);
1395a2cc190SJeff Kirsher }
1405a2cc190SJeff Kirsher 
1415a2cc190SJeff Kirsher int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *qpn, u8 wrap)
1425a2cc190SJeff Kirsher {
1435a2cc190SJeff Kirsher 	struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
1445a2cc190SJeff Kirsher 	struct mlx4_mac_table *table = &info->mac_table;
1455a2cc190SJeff Kirsher 	struct mlx4_mac_entry *entry;
1465a2cc190SJeff Kirsher 	int i, err = 0;
1475a2cc190SJeff Kirsher 	int free = -1;
1485a2cc190SJeff Kirsher 
1495a2cc190SJeff Kirsher 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
1505a2cc190SJeff Kirsher 		err = mlx4_uc_steer_add(dev, port, mac, qpn, 1);
151f470f8d4SLinus Torvalds 		if (err)
152f470f8d4SLinus Torvalds 			return err;
153f470f8d4SLinus Torvalds 
1545a2cc190SJeff Kirsher 		entry = kmalloc(sizeof *entry, GFP_KERNEL);
1555a2cc190SJeff Kirsher 		if (!entry) {
1565a2cc190SJeff Kirsher 			mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
1575a2cc190SJeff Kirsher 			return -ENOMEM;
1585a2cc190SJeff Kirsher 		}
159f470f8d4SLinus Torvalds 
1605a2cc190SJeff Kirsher 		entry->mac = mac;
1615a2cc190SJeff Kirsher 		err = radix_tree_insert(&info->mac_tree, *qpn, entry);
1625a2cc190SJeff Kirsher 		if (err) {
163f470f8d4SLinus Torvalds 			kfree(entry);
1645a2cc190SJeff Kirsher 			mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
1655a2cc190SJeff Kirsher 			return err;
1665a2cc190SJeff Kirsher 		}
1675a2cc190SJeff Kirsher 	}
168f470f8d4SLinus Torvalds 
1695a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
170f470f8d4SLinus Torvalds 
1715a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
1725a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
1735a2cc190SJeff Kirsher 		if (free < 0 && !table->refs[i]) {
1745a2cc190SJeff Kirsher 			free = i;
1755a2cc190SJeff Kirsher 			continue;
1765a2cc190SJeff Kirsher 		}
1775a2cc190SJeff Kirsher 
1785a2cc190SJeff Kirsher 		if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
1795a2cc190SJeff Kirsher 			/* MAC already registered, increase references count */
1805a2cc190SJeff Kirsher 			++table->refs[i];
1815a2cc190SJeff Kirsher 			goto out;
1825a2cc190SJeff Kirsher 		}
1835a2cc190SJeff Kirsher 	}
1845a2cc190SJeff Kirsher 
1855a2cc190SJeff Kirsher 	if (free < 0) {
1865a2cc190SJeff Kirsher 		err = -ENOMEM;
1875a2cc190SJeff Kirsher 		goto out;
1885a2cc190SJeff Kirsher 	}
1895a2cc190SJeff Kirsher 
1905a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Free MAC index is %d\n", free);
1915a2cc190SJeff Kirsher 
1925a2cc190SJeff Kirsher 	if (table->total == table->max) {
1935a2cc190SJeff Kirsher 		/* No free mac entries */
1945a2cc190SJeff Kirsher 		err = -ENOSPC;
1955a2cc190SJeff Kirsher 		goto out;
1965a2cc190SJeff Kirsher 	}
1975a2cc190SJeff Kirsher 
1985a2cc190SJeff Kirsher 	/* Register new MAC */
1995a2cc190SJeff Kirsher 	table->refs[free] = 1;
2005a2cc190SJeff Kirsher 	table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
2015a2cc190SJeff Kirsher 
2025a2cc190SJeff Kirsher 	err = mlx4_set_port_mac_table(dev, port, table->entries);
2035a2cc190SJeff Kirsher 	if (unlikely(err)) {
2045a2cc190SJeff Kirsher 		mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
2055a2cc190SJeff Kirsher 		table->refs[free] = 0;
2065a2cc190SJeff Kirsher 		table->entries[free] = 0;
2075a2cc190SJeff Kirsher 		goto out;
2085a2cc190SJeff Kirsher 	}
2095a2cc190SJeff Kirsher 
2105a2cc190SJeff Kirsher 	if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
2115a2cc190SJeff Kirsher 		*qpn = info->base_qpn + free;
2125a2cc190SJeff Kirsher 	++table->total;
2135a2cc190SJeff Kirsher out:
2145a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
2155a2cc190SJeff Kirsher 	return err;
2165a2cc190SJeff Kirsher }
2175a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_register_mac);
2185a2cc190SJeff Kirsher 
2195a2cc190SJeff Kirsher static int validate_index(struct mlx4_dev *dev,
2205a2cc190SJeff Kirsher 			  struct mlx4_mac_table *table, int index)
2215a2cc190SJeff Kirsher {
2225a2cc190SJeff Kirsher 	int err = 0;
2235a2cc190SJeff Kirsher 
2245a2cc190SJeff Kirsher 	if (index < 0 || index >= table->max || !table->entries[index]) {
2255a2cc190SJeff Kirsher 		mlx4_warn(dev, "No valid Mac entry for the given index\n");
2265a2cc190SJeff Kirsher 		err = -EINVAL;
2275a2cc190SJeff Kirsher 	}
2285a2cc190SJeff Kirsher 	return err;
2295a2cc190SJeff Kirsher }
2305a2cc190SJeff Kirsher 
2315a2cc190SJeff Kirsher static int find_index(struct mlx4_dev *dev,
2325a2cc190SJeff Kirsher 		      struct mlx4_mac_table *table, u64 mac)
2335a2cc190SJeff Kirsher {
2345a2cc190SJeff Kirsher 	int i;
2355a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
2365a2cc190SJeff Kirsher 		if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
2375a2cc190SJeff Kirsher 			return i;
2385a2cc190SJeff Kirsher 	}
2395a2cc190SJeff Kirsher 	/* Mac not found */
2405a2cc190SJeff Kirsher 	return -EINVAL;
2415a2cc190SJeff Kirsher }
2425a2cc190SJeff Kirsher 
2435a2cc190SJeff Kirsher void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
2445a2cc190SJeff Kirsher {
2455a2cc190SJeff Kirsher 	struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
2465a2cc190SJeff Kirsher 	struct mlx4_mac_table *table = &info->mac_table;
2475a2cc190SJeff Kirsher 	int index = qpn - info->base_qpn;
2485a2cc190SJeff Kirsher 	struct mlx4_mac_entry *entry;
2495a2cc190SJeff Kirsher 
2505a2cc190SJeff Kirsher 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
2515a2cc190SJeff Kirsher 		entry = radix_tree_lookup(&info->mac_tree, qpn);
2525a2cc190SJeff Kirsher 		if (entry) {
2535a2cc190SJeff Kirsher 			mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1);
2545a2cc190SJeff Kirsher 			radix_tree_delete(&info->mac_tree, qpn);
2555a2cc190SJeff Kirsher 			index = find_index(dev, table, entry->mac);
2565a2cc190SJeff Kirsher 			kfree(entry);
2575a2cc190SJeff Kirsher 		}
2585a2cc190SJeff Kirsher 	}
2595a2cc190SJeff Kirsher 
2605a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
2615a2cc190SJeff Kirsher 
2625a2cc190SJeff Kirsher 	if (validate_index(dev, table, index))
2635a2cc190SJeff Kirsher 		goto out;
2645a2cc190SJeff Kirsher 
2655a2cc190SJeff Kirsher 	/* Check whether this address has reference count */
2665a2cc190SJeff Kirsher 	if (!(--table->refs[index])) {
2675a2cc190SJeff Kirsher 		table->entries[index] = 0;
2685a2cc190SJeff Kirsher 		mlx4_set_port_mac_table(dev, port, table->entries);
2695a2cc190SJeff Kirsher 		--table->total;
2705a2cc190SJeff Kirsher 	}
2715a2cc190SJeff Kirsher out:
2725a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
2735a2cc190SJeff Kirsher }
2745a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
2755a2cc190SJeff Kirsher 
2765a2cc190SJeff Kirsher int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
2775a2cc190SJeff Kirsher {
2785a2cc190SJeff Kirsher 	struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
2795a2cc190SJeff Kirsher 	struct mlx4_mac_table *table = &info->mac_table;
2805a2cc190SJeff Kirsher 	int index = qpn - info->base_qpn;
2815a2cc190SJeff Kirsher 	struct mlx4_mac_entry *entry;
2825a2cc190SJeff Kirsher 	int err;
2835a2cc190SJeff Kirsher 
2845a2cc190SJeff Kirsher 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
2855a2cc190SJeff Kirsher 		entry = radix_tree_lookup(&info->mac_tree, qpn);
2865a2cc190SJeff Kirsher 		if (!entry)
2875a2cc190SJeff Kirsher 			return -EINVAL;
2885a2cc190SJeff Kirsher 		index = find_index(dev, table, entry->mac);
2895a2cc190SJeff Kirsher 		mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0);
2905a2cc190SJeff Kirsher 		entry->mac = new_mac;
2915a2cc190SJeff Kirsher 		err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0);
2925a2cc190SJeff Kirsher 		if (err || index < 0)
2935a2cc190SJeff Kirsher 			return err;
2945a2cc190SJeff Kirsher 	}
2955a2cc190SJeff Kirsher 
2965a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
2975a2cc190SJeff Kirsher 
2985a2cc190SJeff Kirsher 	err = validate_index(dev, table, index);
2995a2cc190SJeff Kirsher 	if (err)
3005a2cc190SJeff Kirsher 		goto out;
3015a2cc190SJeff Kirsher 
3025a2cc190SJeff Kirsher 	table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
3035a2cc190SJeff Kirsher 
3045a2cc190SJeff Kirsher 	err = mlx4_set_port_mac_table(dev, port, table->entries);
3055a2cc190SJeff Kirsher 	if (unlikely(err)) {
3065a2cc190SJeff Kirsher 		mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac);
3075a2cc190SJeff Kirsher 		table->entries[index] = 0;
3085a2cc190SJeff Kirsher 	}
3095a2cc190SJeff Kirsher out:
3105a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
3115a2cc190SJeff Kirsher 	return err;
3125a2cc190SJeff Kirsher }
3135a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_replace_mac);
3145a2cc190SJeff Kirsher static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
3155a2cc190SJeff Kirsher 				    __be32 *entries)
3165a2cc190SJeff Kirsher {
3175a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
3185a2cc190SJeff Kirsher 	u32 in_mod;
3195a2cc190SJeff Kirsher 	int err;
3205a2cc190SJeff Kirsher 
3215a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
3225a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
3235a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
3245a2cc190SJeff Kirsher 
3255a2cc190SJeff Kirsher 	memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
3265a2cc190SJeff Kirsher 	in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
3275a2cc190SJeff Kirsher 	err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
3285a2cc190SJeff Kirsher 		       MLX4_CMD_TIME_CLASS_B);
3295a2cc190SJeff Kirsher 
3305a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
3315a2cc190SJeff Kirsher 
3325a2cc190SJeff Kirsher 	return err;
3335a2cc190SJeff Kirsher }
3345a2cc190SJeff Kirsher 
3355a2cc190SJeff Kirsher int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
3365a2cc190SJeff Kirsher {
3375a2cc190SJeff Kirsher 	struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
3385a2cc190SJeff Kirsher 	int i;
3395a2cc190SJeff Kirsher 
3405a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
3415a2cc190SJeff Kirsher 		if (table->refs[i] &&
3425a2cc190SJeff Kirsher 		    (vid == (MLX4_VLAN_MASK &
3435a2cc190SJeff Kirsher 			      be32_to_cpu(table->entries[i])))) {
3445a2cc190SJeff Kirsher 			/* VLAN already registered, increase reference count */
3455a2cc190SJeff Kirsher 			*idx = i;
3465a2cc190SJeff Kirsher 			return 0;
3475a2cc190SJeff Kirsher 		}
3485a2cc190SJeff Kirsher 	}
3495a2cc190SJeff Kirsher 
3505a2cc190SJeff Kirsher 	return -ENOENT;
3515a2cc190SJeff Kirsher }
3525a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
3535a2cc190SJeff Kirsher 
3545a2cc190SJeff Kirsher int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
3555a2cc190SJeff Kirsher {
3565a2cc190SJeff Kirsher 	struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
3575a2cc190SJeff Kirsher 	int i, err = 0;
3585a2cc190SJeff Kirsher 	int free = -1;
3595a2cc190SJeff Kirsher 
3605a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
361e72ebf5aSYevgeny Petrilin 
362e72ebf5aSYevgeny Petrilin 	if (table->total == table->max) {
363e72ebf5aSYevgeny Petrilin 		/* No free vlan entries */
364e72ebf5aSYevgeny Petrilin 		err = -ENOSPC;
365e72ebf5aSYevgeny Petrilin 		goto out;
366e72ebf5aSYevgeny Petrilin 	}
367e72ebf5aSYevgeny Petrilin 
3685a2cc190SJeff Kirsher 	for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
3695a2cc190SJeff Kirsher 		if (free < 0 && (table->refs[i] == 0)) {
3705a2cc190SJeff Kirsher 			free = i;
3715a2cc190SJeff Kirsher 			continue;
3725a2cc190SJeff Kirsher 		}
3735a2cc190SJeff Kirsher 
3745a2cc190SJeff Kirsher 		if (table->refs[i] &&
3755a2cc190SJeff Kirsher 		    (vlan == (MLX4_VLAN_MASK &
3765a2cc190SJeff Kirsher 			      be32_to_cpu(table->entries[i])))) {
3775a2cc190SJeff Kirsher 			/* Vlan already registered, increase references count */
3785a2cc190SJeff Kirsher 			*index = i;
3795a2cc190SJeff Kirsher 			++table->refs[i];
3805a2cc190SJeff Kirsher 			goto out;
3815a2cc190SJeff Kirsher 		}
3825a2cc190SJeff Kirsher 	}
3835a2cc190SJeff Kirsher 
3845a2cc190SJeff Kirsher 	if (free < 0) {
3855a2cc190SJeff Kirsher 		err = -ENOMEM;
3865a2cc190SJeff Kirsher 		goto out;
3875a2cc190SJeff Kirsher 	}
3885a2cc190SJeff Kirsher 
3895a2cc190SJeff Kirsher 	/* Register new MAC */
3905a2cc190SJeff Kirsher 	table->refs[free] = 1;
3915a2cc190SJeff Kirsher 	table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
3925a2cc190SJeff Kirsher 
3935a2cc190SJeff Kirsher 	err = mlx4_set_port_vlan_table(dev, port, table->entries);
3945a2cc190SJeff Kirsher 	if (unlikely(err)) {
3955a2cc190SJeff Kirsher 		mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
3965a2cc190SJeff Kirsher 		table->refs[free] = 0;
3975a2cc190SJeff Kirsher 		table->entries[free] = 0;
3985a2cc190SJeff Kirsher 		goto out;
3995a2cc190SJeff Kirsher 	}
4005a2cc190SJeff Kirsher 
4015a2cc190SJeff Kirsher 	*index = free;
4025a2cc190SJeff Kirsher 	++table->total;
4035a2cc190SJeff Kirsher out:
4045a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
4055a2cc190SJeff Kirsher 	return err;
4065a2cc190SJeff Kirsher }
4075a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_register_vlan);
4085a2cc190SJeff Kirsher 
4095a2cc190SJeff Kirsher void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
4105a2cc190SJeff Kirsher {
4115a2cc190SJeff Kirsher 	struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
4125a2cc190SJeff Kirsher 
4135a2cc190SJeff Kirsher 	if (index < MLX4_VLAN_REGULAR) {
4145a2cc190SJeff Kirsher 		mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
4155a2cc190SJeff Kirsher 		return;
4165a2cc190SJeff Kirsher 	}
4175a2cc190SJeff Kirsher 
4185a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
4195a2cc190SJeff Kirsher 	if (!table->refs[index]) {
4205a2cc190SJeff Kirsher 		mlx4_warn(dev, "No vlan entry for index %d\n", index);
4215a2cc190SJeff Kirsher 		goto out;
4225a2cc190SJeff Kirsher 	}
4235a2cc190SJeff Kirsher 	if (--table->refs[index]) {
4245a2cc190SJeff Kirsher 		mlx4_dbg(dev, "Have more references for index %d,"
4255a2cc190SJeff Kirsher 			 "no need to modify vlan table\n", index);
4265a2cc190SJeff Kirsher 		goto out;
4275a2cc190SJeff Kirsher 	}
4285a2cc190SJeff Kirsher 	table->entries[index] = 0;
4295a2cc190SJeff Kirsher 	mlx4_set_port_vlan_table(dev, port, table->entries);
4305a2cc190SJeff Kirsher 	--table->total;
4315a2cc190SJeff Kirsher out:
4325a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
4335a2cc190SJeff Kirsher }
4345a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
4355a2cc190SJeff Kirsher 
4365a2cc190SJeff Kirsher int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
4375a2cc190SJeff Kirsher {
4385a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
4395a2cc190SJeff Kirsher 	u8 *inbuf, *outbuf;
4405a2cc190SJeff Kirsher 	int err;
4415a2cc190SJeff Kirsher 
4425a2cc190SJeff Kirsher 	inmailbox = mlx4_alloc_cmd_mailbox(dev);
4435a2cc190SJeff Kirsher 	if (IS_ERR(inmailbox))
4445a2cc190SJeff Kirsher 		return PTR_ERR(inmailbox);
4455a2cc190SJeff Kirsher 
4465a2cc190SJeff Kirsher 	outmailbox = mlx4_alloc_cmd_mailbox(dev);
4475a2cc190SJeff Kirsher 	if (IS_ERR(outmailbox)) {
4485a2cc190SJeff Kirsher 		mlx4_free_cmd_mailbox(dev, inmailbox);
4495a2cc190SJeff Kirsher 		return PTR_ERR(outmailbox);
4505a2cc190SJeff Kirsher 	}
4515a2cc190SJeff Kirsher 
4525a2cc190SJeff Kirsher 	inbuf = inmailbox->buf;
4535a2cc190SJeff Kirsher 	outbuf = outmailbox->buf;
4545a2cc190SJeff Kirsher 	memset(inbuf, 0, 256);
4555a2cc190SJeff Kirsher 	memset(outbuf, 0, 256);
4565a2cc190SJeff Kirsher 	inbuf[0] = 1;
4575a2cc190SJeff Kirsher 	inbuf[1] = 1;
4585a2cc190SJeff Kirsher 	inbuf[2] = 1;
4595a2cc190SJeff Kirsher 	inbuf[3] = 1;
4605a2cc190SJeff Kirsher 	*(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
4615a2cc190SJeff Kirsher 	*(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
4625a2cc190SJeff Kirsher 
4635a2cc190SJeff Kirsher 	err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
4645a2cc190SJeff Kirsher 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
4655a2cc190SJeff Kirsher 	if (!err)
4665a2cc190SJeff Kirsher 		*caps = *(__be32 *) (outbuf + 84);
4675a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, inmailbox);
4685a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, outmailbox);
4695a2cc190SJeff Kirsher 	return err;
4705a2cc190SJeff Kirsher }
4715a2cc190SJeff Kirsher 
472f470f8d4SLinus Torvalds int mlx4_check_ext_port_caps(struct mlx4_dev *dev, u8 port)
473f470f8d4SLinus Torvalds {
474f470f8d4SLinus Torvalds 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
475f470f8d4SLinus Torvalds 	u8 *inbuf, *outbuf;
476f470f8d4SLinus Torvalds 	int err, packet_error;
477f470f8d4SLinus Torvalds 
478f470f8d4SLinus Torvalds 	inmailbox = mlx4_alloc_cmd_mailbox(dev);
479f470f8d4SLinus Torvalds 	if (IS_ERR(inmailbox))
480f470f8d4SLinus Torvalds 		return PTR_ERR(inmailbox);
481f470f8d4SLinus Torvalds 
482f470f8d4SLinus Torvalds 	outmailbox = mlx4_alloc_cmd_mailbox(dev);
483f470f8d4SLinus Torvalds 	if (IS_ERR(outmailbox)) {
484f470f8d4SLinus Torvalds 		mlx4_free_cmd_mailbox(dev, inmailbox);
485f470f8d4SLinus Torvalds 		return PTR_ERR(outmailbox);
486f470f8d4SLinus Torvalds 	}
487f470f8d4SLinus Torvalds 
488f470f8d4SLinus Torvalds 	inbuf = inmailbox->buf;
489f470f8d4SLinus Torvalds 	outbuf = outmailbox->buf;
490f470f8d4SLinus Torvalds 	memset(inbuf, 0, 256);
491f470f8d4SLinus Torvalds 	memset(outbuf, 0, 256);
492f470f8d4SLinus Torvalds 	inbuf[0] = 1;
493f470f8d4SLinus Torvalds 	inbuf[1] = 1;
494f470f8d4SLinus Torvalds 	inbuf[2] = 1;
495f470f8d4SLinus Torvalds 	inbuf[3] = 1;
496f470f8d4SLinus Torvalds 
497f470f8d4SLinus Torvalds 	*(__be16 *) (&inbuf[16]) = MLX4_ATTR_EXTENDED_PORT_INFO;
498f470f8d4SLinus Torvalds 	*(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
499f470f8d4SLinus Torvalds 
500f470f8d4SLinus Torvalds 	err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
501f470f8d4SLinus Torvalds 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
502f470f8d4SLinus Torvalds 
503f470f8d4SLinus Torvalds 	packet_error = be16_to_cpu(*(__be16 *) (outbuf + 4));
504f470f8d4SLinus Torvalds 
505f470f8d4SLinus Torvalds 	dev->caps.ext_port_cap[port] = (!err && !packet_error) ?
506f470f8d4SLinus Torvalds 				       MLX_EXT_PORT_CAP_FLAG_EXTENDED_PORT_INFO
507f470f8d4SLinus Torvalds 				       : 0;
508f470f8d4SLinus Torvalds 
509f470f8d4SLinus Torvalds 	mlx4_free_cmd_mailbox(dev, inmailbox);
510f470f8d4SLinus Torvalds 	mlx4_free_cmd_mailbox(dev, outmailbox);
511f470f8d4SLinus Torvalds 	return err;
512f470f8d4SLinus Torvalds }
513f470f8d4SLinus Torvalds 
5145a2cc190SJeff Kirsher int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
5155a2cc190SJeff Kirsher {
5165a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
5175a2cc190SJeff Kirsher 	int err;
5185a2cc190SJeff Kirsher 
5195a2cc190SJeff Kirsher 	if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
5205a2cc190SJeff Kirsher 		return 0;
5215a2cc190SJeff Kirsher 
5225a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
5235a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
5245a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
5255a2cc190SJeff Kirsher 
5265a2cc190SJeff Kirsher 	memset(mailbox->buf, 0, 256);
5275a2cc190SJeff Kirsher 
5285a2cc190SJeff Kirsher 	((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
5295a2cc190SJeff Kirsher 	err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
5305a2cc190SJeff Kirsher 		       MLX4_CMD_TIME_CLASS_B);
5315a2cc190SJeff Kirsher 
5325a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
5335a2cc190SJeff Kirsher 	return err;
5345a2cc190SJeff Kirsher }
535