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);
1515a2cc190SJeff Kirsher 		if (!err) {
1525a2cc190SJeff Kirsher 			entry = kmalloc(sizeof *entry, GFP_KERNEL);
1535a2cc190SJeff Kirsher 			if (!entry) {
1545a2cc190SJeff Kirsher 				mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
1555a2cc190SJeff Kirsher 				return -ENOMEM;
1565a2cc190SJeff Kirsher 			}
1575a2cc190SJeff Kirsher 			entry->mac = mac;
1585a2cc190SJeff Kirsher 			err = radix_tree_insert(&info->mac_tree, *qpn, entry);
1595a2cc190SJeff Kirsher 			if (err) {
1605a2cc190SJeff Kirsher 				mlx4_uc_steer_release(dev, port, mac, *qpn, 1);
1615a2cc190SJeff Kirsher 				return err;
1625a2cc190SJeff Kirsher 			}
1635a2cc190SJeff Kirsher 		} else
1645a2cc190SJeff Kirsher 			return err;
1655a2cc190SJeff Kirsher 	}
1665a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
1675a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
1685a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
1695a2cc190SJeff Kirsher 		if (free < 0 && !table->refs[i]) {
1705a2cc190SJeff Kirsher 			free = i;
1715a2cc190SJeff Kirsher 			continue;
1725a2cc190SJeff Kirsher 		}
1735a2cc190SJeff Kirsher 
1745a2cc190SJeff Kirsher 		if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
1755a2cc190SJeff Kirsher 			/* MAC already registered, increase references count */
1765a2cc190SJeff Kirsher 			++table->refs[i];
1775a2cc190SJeff Kirsher 			goto out;
1785a2cc190SJeff Kirsher 		}
1795a2cc190SJeff Kirsher 	}
1805a2cc190SJeff Kirsher 
1815a2cc190SJeff Kirsher 	if (free < 0) {
1825a2cc190SJeff Kirsher 		err = -ENOMEM;
1835a2cc190SJeff Kirsher 		goto out;
1845a2cc190SJeff Kirsher 	}
1855a2cc190SJeff Kirsher 
1865a2cc190SJeff Kirsher 	mlx4_dbg(dev, "Free MAC index is %d\n", free);
1875a2cc190SJeff Kirsher 
1885a2cc190SJeff Kirsher 	if (table->total == table->max) {
1895a2cc190SJeff Kirsher 		/* No free mac entries */
1905a2cc190SJeff Kirsher 		err = -ENOSPC;
1915a2cc190SJeff Kirsher 		goto out;
1925a2cc190SJeff Kirsher 	}
1935a2cc190SJeff Kirsher 
1945a2cc190SJeff Kirsher 	/* Register new MAC */
1955a2cc190SJeff Kirsher 	table->refs[free] = 1;
1965a2cc190SJeff Kirsher 	table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
1975a2cc190SJeff Kirsher 
1985a2cc190SJeff Kirsher 	err = mlx4_set_port_mac_table(dev, port, table->entries);
1995a2cc190SJeff Kirsher 	if (unlikely(err)) {
2005a2cc190SJeff Kirsher 		mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
2015a2cc190SJeff Kirsher 		table->refs[free] = 0;
2025a2cc190SJeff Kirsher 		table->entries[free] = 0;
2035a2cc190SJeff Kirsher 		goto out;
2045a2cc190SJeff Kirsher 	}
2055a2cc190SJeff Kirsher 
2065a2cc190SJeff Kirsher 	if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER))
2075a2cc190SJeff Kirsher 		*qpn = info->base_qpn + free;
2085a2cc190SJeff Kirsher 	++table->total;
2095a2cc190SJeff Kirsher out:
2105a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
2115a2cc190SJeff Kirsher 	return err;
2125a2cc190SJeff Kirsher }
2135a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_register_mac);
2145a2cc190SJeff Kirsher 
2155a2cc190SJeff Kirsher static int validate_index(struct mlx4_dev *dev,
2165a2cc190SJeff Kirsher 			  struct mlx4_mac_table *table, int index)
2175a2cc190SJeff Kirsher {
2185a2cc190SJeff Kirsher 	int err = 0;
2195a2cc190SJeff Kirsher 
2205a2cc190SJeff Kirsher 	if (index < 0 || index >= table->max || !table->entries[index]) {
2215a2cc190SJeff Kirsher 		mlx4_warn(dev, "No valid Mac entry for the given index\n");
2225a2cc190SJeff Kirsher 		err = -EINVAL;
2235a2cc190SJeff Kirsher 	}
2245a2cc190SJeff Kirsher 	return err;
2255a2cc190SJeff Kirsher }
2265a2cc190SJeff Kirsher 
2275a2cc190SJeff Kirsher static int find_index(struct mlx4_dev *dev,
2285a2cc190SJeff Kirsher 		      struct mlx4_mac_table *table, u64 mac)
2295a2cc190SJeff Kirsher {
2305a2cc190SJeff Kirsher 	int i;
2315a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
2325a2cc190SJeff Kirsher 		if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i])))
2335a2cc190SJeff Kirsher 			return i;
2345a2cc190SJeff Kirsher 	}
2355a2cc190SJeff Kirsher 	/* Mac not found */
2365a2cc190SJeff Kirsher 	return -EINVAL;
2375a2cc190SJeff Kirsher }
2385a2cc190SJeff Kirsher 
2395a2cc190SJeff Kirsher void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn)
2405a2cc190SJeff Kirsher {
2415a2cc190SJeff Kirsher 	struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
2425a2cc190SJeff Kirsher 	struct mlx4_mac_table *table = &info->mac_table;
2435a2cc190SJeff Kirsher 	int index = qpn - info->base_qpn;
2445a2cc190SJeff Kirsher 	struct mlx4_mac_entry *entry;
2455a2cc190SJeff Kirsher 
2465a2cc190SJeff Kirsher 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
2475a2cc190SJeff Kirsher 		entry = radix_tree_lookup(&info->mac_tree, qpn);
2485a2cc190SJeff Kirsher 		if (entry) {
2495a2cc190SJeff Kirsher 			mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1);
2505a2cc190SJeff Kirsher 			radix_tree_delete(&info->mac_tree, qpn);
2515a2cc190SJeff Kirsher 			index = find_index(dev, table, entry->mac);
2525a2cc190SJeff Kirsher 			kfree(entry);
2535a2cc190SJeff Kirsher 		}
2545a2cc190SJeff Kirsher 	}
2555a2cc190SJeff Kirsher 
2565a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
2575a2cc190SJeff Kirsher 
2585a2cc190SJeff Kirsher 	if (validate_index(dev, table, index))
2595a2cc190SJeff Kirsher 		goto out;
2605a2cc190SJeff Kirsher 
2615a2cc190SJeff Kirsher 	/* Check whether this address has reference count */
2625a2cc190SJeff Kirsher 	if (!(--table->refs[index])) {
2635a2cc190SJeff Kirsher 		table->entries[index] = 0;
2645a2cc190SJeff Kirsher 		mlx4_set_port_mac_table(dev, port, table->entries);
2655a2cc190SJeff Kirsher 		--table->total;
2665a2cc190SJeff Kirsher 	}
2675a2cc190SJeff Kirsher out:
2685a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
2695a2cc190SJeff Kirsher }
2705a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
2715a2cc190SJeff Kirsher 
2725a2cc190SJeff Kirsher int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap)
2735a2cc190SJeff Kirsher {
2745a2cc190SJeff Kirsher 	struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
2755a2cc190SJeff Kirsher 	struct mlx4_mac_table *table = &info->mac_table;
2765a2cc190SJeff Kirsher 	int index = qpn - info->base_qpn;
2775a2cc190SJeff Kirsher 	struct mlx4_mac_entry *entry;
2785a2cc190SJeff Kirsher 	int err;
2795a2cc190SJeff Kirsher 
2805a2cc190SJeff Kirsher 	if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) {
2815a2cc190SJeff Kirsher 		entry = radix_tree_lookup(&info->mac_tree, qpn);
2825a2cc190SJeff Kirsher 		if (!entry)
2835a2cc190SJeff Kirsher 			return -EINVAL;
2845a2cc190SJeff Kirsher 		index = find_index(dev, table, entry->mac);
2855a2cc190SJeff Kirsher 		mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0);
2865a2cc190SJeff Kirsher 		entry->mac = new_mac;
2875a2cc190SJeff Kirsher 		err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0);
2885a2cc190SJeff Kirsher 		if (err || index < 0)
2895a2cc190SJeff Kirsher 			return err;
2905a2cc190SJeff Kirsher 	}
2915a2cc190SJeff Kirsher 
2925a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
2935a2cc190SJeff Kirsher 
2945a2cc190SJeff Kirsher 	err = validate_index(dev, table, index);
2955a2cc190SJeff Kirsher 	if (err)
2965a2cc190SJeff Kirsher 		goto out;
2975a2cc190SJeff Kirsher 
2985a2cc190SJeff Kirsher 	table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID);
2995a2cc190SJeff Kirsher 
3005a2cc190SJeff Kirsher 	err = mlx4_set_port_mac_table(dev, port, table->entries);
3015a2cc190SJeff Kirsher 	if (unlikely(err)) {
3025a2cc190SJeff Kirsher 		mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac);
3035a2cc190SJeff Kirsher 		table->entries[index] = 0;
3045a2cc190SJeff Kirsher 	}
3055a2cc190SJeff Kirsher out:
3065a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
3075a2cc190SJeff Kirsher 	return err;
3085a2cc190SJeff Kirsher }
3095a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_replace_mac);
3105a2cc190SJeff Kirsher static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
3115a2cc190SJeff Kirsher 				    __be32 *entries)
3125a2cc190SJeff Kirsher {
3135a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
3145a2cc190SJeff Kirsher 	u32 in_mod;
3155a2cc190SJeff Kirsher 	int err;
3165a2cc190SJeff Kirsher 
3175a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
3185a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
3195a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
3205a2cc190SJeff Kirsher 
3215a2cc190SJeff Kirsher 	memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
3225a2cc190SJeff Kirsher 	in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
3235a2cc190SJeff Kirsher 	err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
3245a2cc190SJeff Kirsher 		       MLX4_CMD_TIME_CLASS_B);
3255a2cc190SJeff Kirsher 
3265a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
3275a2cc190SJeff Kirsher 
3285a2cc190SJeff Kirsher 	return err;
3295a2cc190SJeff Kirsher }
3305a2cc190SJeff Kirsher 
3315a2cc190SJeff Kirsher int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx)
3325a2cc190SJeff Kirsher {
3335a2cc190SJeff Kirsher 	struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
3345a2cc190SJeff Kirsher 	int i;
3355a2cc190SJeff Kirsher 
3365a2cc190SJeff Kirsher 	for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) {
3375a2cc190SJeff Kirsher 		if (table->refs[i] &&
3385a2cc190SJeff Kirsher 		    (vid == (MLX4_VLAN_MASK &
3395a2cc190SJeff Kirsher 			      be32_to_cpu(table->entries[i])))) {
3405a2cc190SJeff Kirsher 			/* VLAN already registered, increase reference count */
3415a2cc190SJeff Kirsher 			*idx = i;
3425a2cc190SJeff Kirsher 			return 0;
3435a2cc190SJeff Kirsher 		}
3445a2cc190SJeff Kirsher 	}
3455a2cc190SJeff Kirsher 
3465a2cc190SJeff Kirsher 	return -ENOENT;
3475a2cc190SJeff Kirsher }
3485a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan);
3495a2cc190SJeff Kirsher 
3505a2cc190SJeff Kirsher int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
3515a2cc190SJeff Kirsher {
3525a2cc190SJeff Kirsher 	struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
3535a2cc190SJeff Kirsher 	int i, err = 0;
3545a2cc190SJeff Kirsher 	int free = -1;
3555a2cc190SJeff Kirsher 
3565a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
357e72ebf5aSYevgeny Petrilin 
358e72ebf5aSYevgeny Petrilin 	if (table->total == table->max) {
359e72ebf5aSYevgeny Petrilin 		/* No free vlan entries */
360e72ebf5aSYevgeny Petrilin 		err = -ENOSPC;
361e72ebf5aSYevgeny Petrilin 		goto out;
362e72ebf5aSYevgeny Petrilin 	}
363e72ebf5aSYevgeny Petrilin 
3645a2cc190SJeff Kirsher 	for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
3655a2cc190SJeff Kirsher 		if (free < 0 && (table->refs[i] == 0)) {
3665a2cc190SJeff Kirsher 			free = i;
3675a2cc190SJeff Kirsher 			continue;
3685a2cc190SJeff Kirsher 		}
3695a2cc190SJeff Kirsher 
3705a2cc190SJeff Kirsher 		if (table->refs[i] &&
3715a2cc190SJeff Kirsher 		    (vlan == (MLX4_VLAN_MASK &
3725a2cc190SJeff Kirsher 			      be32_to_cpu(table->entries[i])))) {
3735a2cc190SJeff Kirsher 			/* Vlan already registered, increase references count */
3745a2cc190SJeff Kirsher 			*index = i;
3755a2cc190SJeff Kirsher 			++table->refs[i];
3765a2cc190SJeff Kirsher 			goto out;
3775a2cc190SJeff Kirsher 		}
3785a2cc190SJeff Kirsher 	}
3795a2cc190SJeff Kirsher 
3805a2cc190SJeff Kirsher 	if (free < 0) {
3815a2cc190SJeff Kirsher 		err = -ENOMEM;
3825a2cc190SJeff Kirsher 		goto out;
3835a2cc190SJeff Kirsher 	}
3845a2cc190SJeff Kirsher 
3855a2cc190SJeff Kirsher 	/* Register new MAC */
3865a2cc190SJeff Kirsher 	table->refs[free] = 1;
3875a2cc190SJeff Kirsher 	table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
3885a2cc190SJeff Kirsher 
3895a2cc190SJeff Kirsher 	err = mlx4_set_port_vlan_table(dev, port, table->entries);
3905a2cc190SJeff Kirsher 	if (unlikely(err)) {
3915a2cc190SJeff Kirsher 		mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
3925a2cc190SJeff Kirsher 		table->refs[free] = 0;
3935a2cc190SJeff Kirsher 		table->entries[free] = 0;
3945a2cc190SJeff Kirsher 		goto out;
3955a2cc190SJeff Kirsher 	}
3965a2cc190SJeff Kirsher 
3975a2cc190SJeff Kirsher 	*index = free;
3985a2cc190SJeff Kirsher 	++table->total;
3995a2cc190SJeff Kirsher out:
4005a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
4015a2cc190SJeff Kirsher 	return err;
4025a2cc190SJeff Kirsher }
4035a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_register_vlan);
4045a2cc190SJeff Kirsher 
4055a2cc190SJeff Kirsher void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
4065a2cc190SJeff Kirsher {
4075a2cc190SJeff Kirsher 	struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
4085a2cc190SJeff Kirsher 
4095a2cc190SJeff Kirsher 	if (index < MLX4_VLAN_REGULAR) {
4105a2cc190SJeff Kirsher 		mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
4115a2cc190SJeff Kirsher 		return;
4125a2cc190SJeff Kirsher 	}
4135a2cc190SJeff Kirsher 
4145a2cc190SJeff Kirsher 	mutex_lock(&table->mutex);
4155a2cc190SJeff Kirsher 	if (!table->refs[index]) {
4165a2cc190SJeff Kirsher 		mlx4_warn(dev, "No vlan entry for index %d\n", index);
4175a2cc190SJeff Kirsher 		goto out;
4185a2cc190SJeff Kirsher 	}
4195a2cc190SJeff Kirsher 	if (--table->refs[index]) {
4205a2cc190SJeff Kirsher 		mlx4_dbg(dev, "Have more references for index %d,"
4215a2cc190SJeff Kirsher 			 "no need to modify vlan table\n", index);
4225a2cc190SJeff Kirsher 		goto out;
4235a2cc190SJeff Kirsher 	}
4245a2cc190SJeff Kirsher 	table->entries[index] = 0;
4255a2cc190SJeff Kirsher 	mlx4_set_port_vlan_table(dev, port, table->entries);
4265a2cc190SJeff Kirsher 	--table->total;
4275a2cc190SJeff Kirsher out:
4285a2cc190SJeff Kirsher 	mutex_unlock(&table->mutex);
4295a2cc190SJeff Kirsher }
4305a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
4315a2cc190SJeff Kirsher 
4325a2cc190SJeff Kirsher int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps)
4335a2cc190SJeff Kirsher {
4345a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *inmailbox, *outmailbox;
4355a2cc190SJeff Kirsher 	u8 *inbuf, *outbuf;
4365a2cc190SJeff Kirsher 	int err;
4375a2cc190SJeff Kirsher 
4385a2cc190SJeff Kirsher 	inmailbox = mlx4_alloc_cmd_mailbox(dev);
4395a2cc190SJeff Kirsher 	if (IS_ERR(inmailbox))
4405a2cc190SJeff Kirsher 		return PTR_ERR(inmailbox);
4415a2cc190SJeff Kirsher 
4425a2cc190SJeff Kirsher 	outmailbox = mlx4_alloc_cmd_mailbox(dev);
4435a2cc190SJeff Kirsher 	if (IS_ERR(outmailbox)) {
4445a2cc190SJeff Kirsher 		mlx4_free_cmd_mailbox(dev, inmailbox);
4455a2cc190SJeff Kirsher 		return PTR_ERR(outmailbox);
4465a2cc190SJeff Kirsher 	}
4475a2cc190SJeff Kirsher 
4485a2cc190SJeff Kirsher 	inbuf = inmailbox->buf;
4495a2cc190SJeff Kirsher 	outbuf = outmailbox->buf;
4505a2cc190SJeff Kirsher 	memset(inbuf, 0, 256);
4515a2cc190SJeff Kirsher 	memset(outbuf, 0, 256);
4525a2cc190SJeff Kirsher 	inbuf[0] = 1;
4535a2cc190SJeff Kirsher 	inbuf[1] = 1;
4545a2cc190SJeff Kirsher 	inbuf[2] = 1;
4555a2cc190SJeff Kirsher 	inbuf[3] = 1;
4565a2cc190SJeff Kirsher 	*(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015);
4575a2cc190SJeff Kirsher 	*(__be32 *) (&inbuf[20]) = cpu_to_be32(port);
4585a2cc190SJeff Kirsher 
4595a2cc190SJeff Kirsher 	err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3,
4605a2cc190SJeff Kirsher 			   MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C);
4615a2cc190SJeff Kirsher 	if (!err)
4625a2cc190SJeff Kirsher 		*caps = *(__be32 *) (outbuf + 84);
4635a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, inmailbox);
4645a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, outmailbox);
4655a2cc190SJeff Kirsher 	return err;
4665a2cc190SJeff Kirsher }
4675a2cc190SJeff Kirsher 
4685a2cc190SJeff Kirsher int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port)
4695a2cc190SJeff Kirsher {
4705a2cc190SJeff Kirsher 	struct mlx4_cmd_mailbox *mailbox;
4715a2cc190SJeff Kirsher 	int err;
4725a2cc190SJeff Kirsher 
4735a2cc190SJeff Kirsher 	if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH)
4745a2cc190SJeff Kirsher 		return 0;
4755a2cc190SJeff Kirsher 
4765a2cc190SJeff Kirsher 	mailbox = mlx4_alloc_cmd_mailbox(dev);
4775a2cc190SJeff Kirsher 	if (IS_ERR(mailbox))
4785a2cc190SJeff Kirsher 		return PTR_ERR(mailbox);
4795a2cc190SJeff Kirsher 
4805a2cc190SJeff Kirsher 	memset(mailbox->buf, 0, 256);
4815a2cc190SJeff Kirsher 
4825a2cc190SJeff Kirsher 	((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port];
4835a2cc190SJeff Kirsher 	err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT,
4845a2cc190SJeff Kirsher 		       MLX4_CMD_TIME_CLASS_B);
4855a2cc190SJeff Kirsher 
4865a2cc190SJeff Kirsher 	mlx4_free_cmd_mailbox(dev, mailbox);
4875a2cc190SJeff Kirsher 	return err;
4885a2cc190SJeff Kirsher }
489