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> 35ee40fa06SPaul Gortmaker #include <linux/export.h> 365a2cc190SJeff Kirsher 375a2cc190SJeff Kirsher #include <linux/mlx4/cmd.h> 385a2cc190SJeff Kirsher 395a2cc190SJeff Kirsher #include "mlx4.h" 405a2cc190SJeff Kirsher 415a2cc190SJeff Kirsher #define MLX4_MAC_VALID (1ull << 63) 425a2cc190SJeff Kirsher #define MLX4_MAC_MASK 0xffffffffffffULL 435a2cc190SJeff Kirsher 445a2cc190SJeff Kirsher #define MLX4_VLAN_VALID (1u << 31) 455a2cc190SJeff Kirsher #define MLX4_VLAN_MASK 0xfff 465a2cc190SJeff Kirsher 475a2cc190SJeff Kirsher void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table) 485a2cc190SJeff Kirsher { 495a2cc190SJeff Kirsher int i; 505a2cc190SJeff Kirsher 515a2cc190SJeff Kirsher mutex_init(&table->mutex); 525a2cc190SJeff Kirsher for (i = 0; i < MLX4_MAX_MAC_NUM; i++) { 535a2cc190SJeff Kirsher table->entries[i] = 0; 545a2cc190SJeff Kirsher table->refs[i] = 0; 555a2cc190SJeff Kirsher } 565a2cc190SJeff Kirsher table->max = 1 << dev->caps.log_num_macs; 575a2cc190SJeff Kirsher table->total = 0; 585a2cc190SJeff Kirsher } 595a2cc190SJeff Kirsher 605a2cc190SJeff Kirsher void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table) 615a2cc190SJeff Kirsher { 625a2cc190SJeff Kirsher int i; 635a2cc190SJeff Kirsher 645a2cc190SJeff Kirsher mutex_init(&table->mutex); 655a2cc190SJeff Kirsher for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) { 665a2cc190SJeff Kirsher table->entries[i] = 0; 675a2cc190SJeff Kirsher table->refs[i] = 0; 685a2cc190SJeff Kirsher } 69e72ebf5aSYevgeny Petrilin table->max = (1 << dev->caps.log_num_vlans) - MLX4_VLAN_REGULAR; 705a2cc190SJeff Kirsher table->total = 0; 715a2cc190SJeff Kirsher } 725a2cc190SJeff Kirsher 735a2cc190SJeff Kirsher static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port, 745a2cc190SJeff Kirsher __be64 *entries) 755a2cc190SJeff Kirsher { 765a2cc190SJeff Kirsher struct mlx4_cmd_mailbox *mailbox; 775a2cc190SJeff Kirsher u32 in_mod; 785a2cc190SJeff Kirsher int err; 795a2cc190SJeff Kirsher 805a2cc190SJeff Kirsher mailbox = mlx4_alloc_cmd_mailbox(dev); 815a2cc190SJeff Kirsher if (IS_ERR(mailbox)) 825a2cc190SJeff Kirsher return PTR_ERR(mailbox); 835a2cc190SJeff Kirsher 845a2cc190SJeff Kirsher memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE); 855a2cc190SJeff Kirsher 865a2cc190SJeff Kirsher in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port; 875a2cc190SJeff Kirsher err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT, 885a2cc190SJeff Kirsher MLX4_CMD_TIME_CLASS_B); 895a2cc190SJeff Kirsher 905a2cc190SJeff Kirsher mlx4_free_cmd_mailbox(dev, mailbox); 915a2cc190SJeff Kirsher return err; 925a2cc190SJeff Kirsher } 935a2cc190SJeff Kirsher 945a2cc190SJeff Kirsher static int mlx4_uc_steer_add(struct mlx4_dev *dev, u8 port, 955a2cc190SJeff Kirsher u64 mac, int *qpn, u8 reserve) 965a2cc190SJeff Kirsher { 975a2cc190SJeff Kirsher struct mlx4_qp qp; 985a2cc190SJeff Kirsher u8 gid[16] = {0}; 995a2cc190SJeff Kirsher int err; 1005a2cc190SJeff Kirsher 1015a2cc190SJeff Kirsher if (reserve) { 1025a2cc190SJeff Kirsher err = mlx4_qp_reserve_range(dev, 1, 1, qpn); 1035a2cc190SJeff Kirsher if (err) { 1045a2cc190SJeff Kirsher mlx4_err(dev, "Failed to reserve qp for mac registration\n"); 1055a2cc190SJeff Kirsher return err; 1065a2cc190SJeff Kirsher } 1075a2cc190SJeff Kirsher } 1085a2cc190SJeff Kirsher qp.qpn = *qpn; 1095a2cc190SJeff Kirsher 1105a2cc190SJeff Kirsher mac &= 0xffffffffffffULL; 1115a2cc190SJeff Kirsher mac = cpu_to_be64(mac << 16); 1125a2cc190SJeff Kirsher memcpy(&gid[10], &mac, ETH_ALEN); 1135a2cc190SJeff Kirsher gid[5] = port; 1145a2cc190SJeff Kirsher gid[7] = MLX4_UC_STEER << 1; 1155a2cc190SJeff Kirsher 1165a2cc190SJeff Kirsher err = mlx4_qp_attach_common(dev, &qp, gid, 0, 1175a2cc190SJeff Kirsher MLX4_PROT_ETH, MLX4_UC_STEER); 1185a2cc190SJeff Kirsher if (err && reserve) 1195a2cc190SJeff Kirsher mlx4_qp_release_range(dev, *qpn, 1); 1205a2cc190SJeff Kirsher 1215a2cc190SJeff Kirsher return err; 1225a2cc190SJeff Kirsher } 1235a2cc190SJeff Kirsher 1245a2cc190SJeff Kirsher static void mlx4_uc_steer_release(struct mlx4_dev *dev, u8 port, 1255a2cc190SJeff Kirsher u64 mac, int qpn, u8 free) 1265a2cc190SJeff Kirsher { 1275a2cc190SJeff Kirsher struct mlx4_qp qp; 1285a2cc190SJeff Kirsher u8 gid[16] = {0}; 1295a2cc190SJeff Kirsher 1305a2cc190SJeff Kirsher qp.qpn = qpn; 1315a2cc190SJeff Kirsher mac &= 0xffffffffffffULL; 1325a2cc190SJeff Kirsher mac = cpu_to_be64(mac << 16); 1335a2cc190SJeff Kirsher memcpy(&gid[10], &mac, ETH_ALEN); 1345a2cc190SJeff Kirsher gid[5] = port; 1355a2cc190SJeff Kirsher gid[7] = MLX4_UC_STEER << 1; 1365a2cc190SJeff Kirsher 1375a2cc190SJeff Kirsher mlx4_qp_detach_common(dev, &qp, gid, MLX4_PROT_ETH, MLX4_UC_STEER); 1385a2cc190SJeff Kirsher if (free) 1395a2cc190SJeff Kirsher mlx4_qp_release_range(dev, qpn, 1); 1405a2cc190SJeff Kirsher } 1415a2cc190SJeff Kirsher 1425a2cc190SJeff Kirsher int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *qpn, u8 wrap) 1435a2cc190SJeff Kirsher { 1445a2cc190SJeff Kirsher struct mlx4_port_info *info = &mlx4_priv(dev)->port[port]; 1455a2cc190SJeff Kirsher struct mlx4_mac_table *table = &info->mac_table; 1465a2cc190SJeff Kirsher struct mlx4_mac_entry *entry; 1475a2cc190SJeff Kirsher int i, err = 0; 1485a2cc190SJeff Kirsher int free = -1; 1495a2cc190SJeff Kirsher 1505a2cc190SJeff Kirsher if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) { 1515a2cc190SJeff Kirsher err = mlx4_uc_steer_add(dev, port, mac, qpn, 1); 1525a2cc190SJeff Kirsher if (!err) { 1535a2cc190SJeff Kirsher entry = kmalloc(sizeof *entry, GFP_KERNEL); 1545a2cc190SJeff Kirsher if (!entry) { 1555a2cc190SJeff Kirsher mlx4_uc_steer_release(dev, port, mac, *qpn, 1); 1565a2cc190SJeff Kirsher return -ENOMEM; 1575a2cc190SJeff Kirsher } 1585a2cc190SJeff Kirsher entry->mac = mac; 1595a2cc190SJeff Kirsher err = radix_tree_insert(&info->mac_tree, *qpn, entry); 1605a2cc190SJeff Kirsher if (err) { 1615a2cc190SJeff Kirsher mlx4_uc_steer_release(dev, port, mac, *qpn, 1); 1625a2cc190SJeff Kirsher return err; 1635a2cc190SJeff Kirsher } 1645a2cc190SJeff Kirsher } else 1655a2cc190SJeff Kirsher return err; 1665a2cc190SJeff Kirsher } 1675a2cc190SJeff Kirsher mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac); 1685a2cc190SJeff Kirsher mutex_lock(&table->mutex); 1695a2cc190SJeff Kirsher for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) { 1705a2cc190SJeff Kirsher if (free < 0 && !table->refs[i]) { 1715a2cc190SJeff Kirsher free = i; 1725a2cc190SJeff Kirsher continue; 1735a2cc190SJeff Kirsher } 1745a2cc190SJeff Kirsher 1755a2cc190SJeff Kirsher if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) { 1765a2cc190SJeff Kirsher /* MAC already registered, increase references count */ 1775a2cc190SJeff Kirsher ++table->refs[i]; 1785a2cc190SJeff Kirsher goto out; 1795a2cc190SJeff Kirsher } 1805a2cc190SJeff Kirsher } 1815a2cc190SJeff Kirsher 1825a2cc190SJeff Kirsher if (free < 0) { 1835a2cc190SJeff Kirsher err = -ENOMEM; 1845a2cc190SJeff Kirsher goto out; 1855a2cc190SJeff Kirsher } 1865a2cc190SJeff Kirsher 1875a2cc190SJeff Kirsher mlx4_dbg(dev, "Free MAC index is %d\n", free); 1885a2cc190SJeff Kirsher 1895a2cc190SJeff Kirsher if (table->total == table->max) { 1905a2cc190SJeff Kirsher /* No free mac entries */ 1915a2cc190SJeff Kirsher err = -ENOSPC; 1925a2cc190SJeff Kirsher goto out; 1935a2cc190SJeff Kirsher } 1945a2cc190SJeff Kirsher 1955a2cc190SJeff Kirsher /* Register new MAC */ 1965a2cc190SJeff Kirsher table->refs[free] = 1; 1975a2cc190SJeff Kirsher table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID); 1985a2cc190SJeff Kirsher 1995a2cc190SJeff Kirsher err = mlx4_set_port_mac_table(dev, port, table->entries); 2005a2cc190SJeff Kirsher if (unlikely(err)) { 2015a2cc190SJeff Kirsher mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac); 2025a2cc190SJeff Kirsher table->refs[free] = 0; 2035a2cc190SJeff Kirsher table->entries[free] = 0; 2045a2cc190SJeff Kirsher goto out; 2055a2cc190SJeff Kirsher } 2065a2cc190SJeff Kirsher 2075a2cc190SJeff Kirsher if (!(dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER)) 2085a2cc190SJeff Kirsher *qpn = info->base_qpn + free; 2095a2cc190SJeff Kirsher ++table->total; 2105a2cc190SJeff Kirsher out: 2115a2cc190SJeff Kirsher mutex_unlock(&table->mutex); 2125a2cc190SJeff Kirsher return err; 2135a2cc190SJeff Kirsher } 2145a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_register_mac); 2155a2cc190SJeff Kirsher 2165a2cc190SJeff Kirsher static int validate_index(struct mlx4_dev *dev, 2175a2cc190SJeff Kirsher struct mlx4_mac_table *table, int index) 2185a2cc190SJeff Kirsher { 2195a2cc190SJeff Kirsher int err = 0; 2205a2cc190SJeff Kirsher 2215a2cc190SJeff Kirsher if (index < 0 || index >= table->max || !table->entries[index]) { 2225a2cc190SJeff Kirsher mlx4_warn(dev, "No valid Mac entry for the given index\n"); 2235a2cc190SJeff Kirsher err = -EINVAL; 2245a2cc190SJeff Kirsher } 2255a2cc190SJeff Kirsher return err; 2265a2cc190SJeff Kirsher } 2275a2cc190SJeff Kirsher 2285a2cc190SJeff Kirsher static int find_index(struct mlx4_dev *dev, 2295a2cc190SJeff Kirsher struct mlx4_mac_table *table, u64 mac) 2305a2cc190SJeff Kirsher { 2315a2cc190SJeff Kirsher int i; 2325a2cc190SJeff Kirsher for (i = 0; i < MLX4_MAX_MAC_NUM; i++) { 2335a2cc190SJeff Kirsher if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) 2345a2cc190SJeff Kirsher return i; 2355a2cc190SJeff Kirsher } 2365a2cc190SJeff Kirsher /* Mac not found */ 2375a2cc190SJeff Kirsher return -EINVAL; 2385a2cc190SJeff Kirsher } 2395a2cc190SJeff Kirsher 2405a2cc190SJeff Kirsher void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int qpn) 2415a2cc190SJeff Kirsher { 2425a2cc190SJeff Kirsher struct mlx4_port_info *info = &mlx4_priv(dev)->port[port]; 2435a2cc190SJeff Kirsher struct mlx4_mac_table *table = &info->mac_table; 2445a2cc190SJeff Kirsher int index = qpn - info->base_qpn; 2455a2cc190SJeff Kirsher struct mlx4_mac_entry *entry; 2465a2cc190SJeff Kirsher 2475a2cc190SJeff Kirsher if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) { 2485a2cc190SJeff Kirsher entry = radix_tree_lookup(&info->mac_tree, qpn); 2495a2cc190SJeff Kirsher if (entry) { 2505a2cc190SJeff Kirsher mlx4_uc_steer_release(dev, port, entry->mac, qpn, 1); 2515a2cc190SJeff Kirsher radix_tree_delete(&info->mac_tree, qpn); 2525a2cc190SJeff Kirsher index = find_index(dev, table, entry->mac); 2535a2cc190SJeff Kirsher kfree(entry); 2545a2cc190SJeff Kirsher } 2555a2cc190SJeff Kirsher } 2565a2cc190SJeff Kirsher 2575a2cc190SJeff Kirsher mutex_lock(&table->mutex); 2585a2cc190SJeff Kirsher 2595a2cc190SJeff Kirsher if (validate_index(dev, table, index)) 2605a2cc190SJeff Kirsher goto out; 2615a2cc190SJeff Kirsher 2625a2cc190SJeff Kirsher /* Check whether this address has reference count */ 2635a2cc190SJeff Kirsher if (!(--table->refs[index])) { 2645a2cc190SJeff Kirsher table->entries[index] = 0; 2655a2cc190SJeff Kirsher mlx4_set_port_mac_table(dev, port, table->entries); 2665a2cc190SJeff Kirsher --table->total; 2675a2cc190SJeff Kirsher } 2685a2cc190SJeff Kirsher out: 2695a2cc190SJeff Kirsher mutex_unlock(&table->mutex); 2705a2cc190SJeff Kirsher } 2715a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_unregister_mac); 2725a2cc190SJeff Kirsher 2735a2cc190SJeff Kirsher int mlx4_replace_mac(struct mlx4_dev *dev, u8 port, int qpn, u64 new_mac, u8 wrap) 2745a2cc190SJeff Kirsher { 2755a2cc190SJeff Kirsher struct mlx4_port_info *info = &mlx4_priv(dev)->port[port]; 2765a2cc190SJeff Kirsher struct mlx4_mac_table *table = &info->mac_table; 2775a2cc190SJeff Kirsher int index = qpn - info->base_qpn; 2785a2cc190SJeff Kirsher struct mlx4_mac_entry *entry; 2795a2cc190SJeff Kirsher int err; 2805a2cc190SJeff Kirsher 2815a2cc190SJeff Kirsher if (dev->caps.flags & MLX4_DEV_CAP_FLAG_VEP_UC_STEER) { 2825a2cc190SJeff Kirsher entry = radix_tree_lookup(&info->mac_tree, qpn); 2835a2cc190SJeff Kirsher if (!entry) 2845a2cc190SJeff Kirsher return -EINVAL; 2855a2cc190SJeff Kirsher index = find_index(dev, table, entry->mac); 2865a2cc190SJeff Kirsher mlx4_uc_steer_release(dev, port, entry->mac, qpn, 0); 2875a2cc190SJeff Kirsher entry->mac = new_mac; 2885a2cc190SJeff Kirsher err = mlx4_uc_steer_add(dev, port, entry->mac, &qpn, 0); 2895a2cc190SJeff Kirsher if (err || index < 0) 2905a2cc190SJeff Kirsher return err; 2915a2cc190SJeff Kirsher } 2925a2cc190SJeff Kirsher 2935a2cc190SJeff Kirsher mutex_lock(&table->mutex); 2945a2cc190SJeff Kirsher 2955a2cc190SJeff Kirsher err = validate_index(dev, table, index); 2965a2cc190SJeff Kirsher if (err) 2975a2cc190SJeff Kirsher goto out; 2985a2cc190SJeff Kirsher 2995a2cc190SJeff Kirsher table->entries[index] = cpu_to_be64(new_mac | MLX4_MAC_VALID); 3005a2cc190SJeff Kirsher 3015a2cc190SJeff Kirsher err = mlx4_set_port_mac_table(dev, port, table->entries); 3025a2cc190SJeff Kirsher if (unlikely(err)) { 3035a2cc190SJeff Kirsher mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) new_mac); 3045a2cc190SJeff Kirsher table->entries[index] = 0; 3055a2cc190SJeff Kirsher } 3065a2cc190SJeff Kirsher out: 3075a2cc190SJeff Kirsher mutex_unlock(&table->mutex); 3085a2cc190SJeff Kirsher return err; 3095a2cc190SJeff Kirsher } 3105a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_replace_mac); 3115a2cc190SJeff Kirsher static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port, 3125a2cc190SJeff Kirsher __be32 *entries) 3135a2cc190SJeff Kirsher { 3145a2cc190SJeff Kirsher struct mlx4_cmd_mailbox *mailbox; 3155a2cc190SJeff Kirsher u32 in_mod; 3165a2cc190SJeff Kirsher int err; 3175a2cc190SJeff Kirsher 3185a2cc190SJeff Kirsher mailbox = mlx4_alloc_cmd_mailbox(dev); 3195a2cc190SJeff Kirsher if (IS_ERR(mailbox)) 3205a2cc190SJeff Kirsher return PTR_ERR(mailbox); 3215a2cc190SJeff Kirsher 3225a2cc190SJeff Kirsher memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE); 3235a2cc190SJeff Kirsher in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port; 3245a2cc190SJeff Kirsher err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT, 3255a2cc190SJeff Kirsher MLX4_CMD_TIME_CLASS_B); 3265a2cc190SJeff Kirsher 3275a2cc190SJeff Kirsher mlx4_free_cmd_mailbox(dev, mailbox); 3285a2cc190SJeff Kirsher 3295a2cc190SJeff Kirsher return err; 3305a2cc190SJeff Kirsher } 3315a2cc190SJeff Kirsher 3325a2cc190SJeff Kirsher int mlx4_find_cached_vlan(struct mlx4_dev *dev, u8 port, u16 vid, int *idx) 3335a2cc190SJeff Kirsher { 3345a2cc190SJeff Kirsher struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table; 3355a2cc190SJeff Kirsher int i; 3365a2cc190SJeff Kirsher 3375a2cc190SJeff Kirsher for (i = 0; i < MLX4_MAX_VLAN_NUM; ++i) { 3385a2cc190SJeff Kirsher if (table->refs[i] && 3395a2cc190SJeff Kirsher (vid == (MLX4_VLAN_MASK & 3405a2cc190SJeff Kirsher be32_to_cpu(table->entries[i])))) { 3415a2cc190SJeff Kirsher /* VLAN already registered, increase reference count */ 3425a2cc190SJeff Kirsher *idx = i; 3435a2cc190SJeff Kirsher return 0; 3445a2cc190SJeff Kirsher } 3455a2cc190SJeff Kirsher } 3465a2cc190SJeff Kirsher 3475a2cc190SJeff Kirsher return -ENOENT; 3485a2cc190SJeff Kirsher } 3495a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_find_cached_vlan); 3505a2cc190SJeff Kirsher 3515a2cc190SJeff Kirsher int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index) 3525a2cc190SJeff Kirsher { 3535a2cc190SJeff Kirsher struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table; 3545a2cc190SJeff Kirsher int i, err = 0; 3555a2cc190SJeff Kirsher int free = -1; 3565a2cc190SJeff Kirsher 3575a2cc190SJeff Kirsher mutex_lock(&table->mutex); 358e72ebf5aSYevgeny Petrilin 359e72ebf5aSYevgeny Petrilin if (table->total == table->max) { 360e72ebf5aSYevgeny Petrilin /* No free vlan entries */ 361e72ebf5aSYevgeny Petrilin err = -ENOSPC; 362e72ebf5aSYevgeny Petrilin goto out; 363e72ebf5aSYevgeny Petrilin } 364e72ebf5aSYevgeny Petrilin 3655a2cc190SJeff Kirsher for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) { 3665a2cc190SJeff Kirsher if (free < 0 && (table->refs[i] == 0)) { 3675a2cc190SJeff Kirsher free = i; 3685a2cc190SJeff Kirsher continue; 3695a2cc190SJeff Kirsher } 3705a2cc190SJeff Kirsher 3715a2cc190SJeff Kirsher if (table->refs[i] && 3725a2cc190SJeff Kirsher (vlan == (MLX4_VLAN_MASK & 3735a2cc190SJeff Kirsher be32_to_cpu(table->entries[i])))) { 3745a2cc190SJeff Kirsher /* Vlan already registered, increase references count */ 3755a2cc190SJeff Kirsher *index = i; 3765a2cc190SJeff Kirsher ++table->refs[i]; 3775a2cc190SJeff Kirsher goto out; 3785a2cc190SJeff Kirsher } 3795a2cc190SJeff Kirsher } 3805a2cc190SJeff Kirsher 3815a2cc190SJeff Kirsher if (free < 0) { 3825a2cc190SJeff Kirsher err = -ENOMEM; 3835a2cc190SJeff Kirsher goto out; 3845a2cc190SJeff Kirsher } 3855a2cc190SJeff Kirsher 3865a2cc190SJeff Kirsher /* Register new MAC */ 3875a2cc190SJeff Kirsher table->refs[free] = 1; 3885a2cc190SJeff Kirsher table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID); 3895a2cc190SJeff Kirsher 3905a2cc190SJeff Kirsher err = mlx4_set_port_vlan_table(dev, port, table->entries); 3915a2cc190SJeff Kirsher if (unlikely(err)) { 3925a2cc190SJeff Kirsher mlx4_warn(dev, "Failed adding vlan: %u\n", vlan); 3935a2cc190SJeff Kirsher table->refs[free] = 0; 3945a2cc190SJeff Kirsher table->entries[free] = 0; 3955a2cc190SJeff Kirsher goto out; 3965a2cc190SJeff Kirsher } 3975a2cc190SJeff Kirsher 3985a2cc190SJeff Kirsher *index = free; 3995a2cc190SJeff Kirsher ++table->total; 4005a2cc190SJeff Kirsher out: 4015a2cc190SJeff Kirsher mutex_unlock(&table->mutex); 4025a2cc190SJeff Kirsher return err; 4035a2cc190SJeff Kirsher } 4045a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_register_vlan); 4055a2cc190SJeff Kirsher 4065a2cc190SJeff Kirsher void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index) 4075a2cc190SJeff Kirsher { 4085a2cc190SJeff Kirsher struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table; 4095a2cc190SJeff Kirsher 4105a2cc190SJeff Kirsher if (index < MLX4_VLAN_REGULAR) { 4115a2cc190SJeff Kirsher mlx4_warn(dev, "Trying to free special vlan index %d\n", index); 4125a2cc190SJeff Kirsher return; 4135a2cc190SJeff Kirsher } 4145a2cc190SJeff Kirsher 4155a2cc190SJeff Kirsher mutex_lock(&table->mutex); 4165a2cc190SJeff Kirsher if (!table->refs[index]) { 4175a2cc190SJeff Kirsher mlx4_warn(dev, "No vlan entry for index %d\n", index); 4185a2cc190SJeff Kirsher goto out; 4195a2cc190SJeff Kirsher } 4205a2cc190SJeff Kirsher if (--table->refs[index]) { 4215a2cc190SJeff Kirsher mlx4_dbg(dev, "Have more references for index %d," 4225a2cc190SJeff Kirsher "no need to modify vlan table\n", index); 4235a2cc190SJeff Kirsher goto out; 4245a2cc190SJeff Kirsher } 4255a2cc190SJeff Kirsher table->entries[index] = 0; 4265a2cc190SJeff Kirsher mlx4_set_port_vlan_table(dev, port, table->entries); 4275a2cc190SJeff Kirsher --table->total; 4285a2cc190SJeff Kirsher out: 4295a2cc190SJeff Kirsher mutex_unlock(&table->mutex); 4305a2cc190SJeff Kirsher } 4315a2cc190SJeff Kirsher EXPORT_SYMBOL_GPL(mlx4_unregister_vlan); 4325a2cc190SJeff Kirsher 4335a2cc190SJeff Kirsher int mlx4_get_port_ib_caps(struct mlx4_dev *dev, u8 port, __be32 *caps) 4345a2cc190SJeff Kirsher { 4355a2cc190SJeff Kirsher struct mlx4_cmd_mailbox *inmailbox, *outmailbox; 4365a2cc190SJeff Kirsher u8 *inbuf, *outbuf; 4375a2cc190SJeff Kirsher int err; 4385a2cc190SJeff Kirsher 4395a2cc190SJeff Kirsher inmailbox = mlx4_alloc_cmd_mailbox(dev); 4405a2cc190SJeff Kirsher if (IS_ERR(inmailbox)) 4415a2cc190SJeff Kirsher return PTR_ERR(inmailbox); 4425a2cc190SJeff Kirsher 4435a2cc190SJeff Kirsher outmailbox = mlx4_alloc_cmd_mailbox(dev); 4445a2cc190SJeff Kirsher if (IS_ERR(outmailbox)) { 4455a2cc190SJeff Kirsher mlx4_free_cmd_mailbox(dev, inmailbox); 4465a2cc190SJeff Kirsher return PTR_ERR(outmailbox); 4475a2cc190SJeff Kirsher } 4485a2cc190SJeff Kirsher 4495a2cc190SJeff Kirsher inbuf = inmailbox->buf; 4505a2cc190SJeff Kirsher outbuf = outmailbox->buf; 4515a2cc190SJeff Kirsher memset(inbuf, 0, 256); 4525a2cc190SJeff Kirsher memset(outbuf, 0, 256); 4535a2cc190SJeff Kirsher inbuf[0] = 1; 4545a2cc190SJeff Kirsher inbuf[1] = 1; 4555a2cc190SJeff Kirsher inbuf[2] = 1; 4565a2cc190SJeff Kirsher inbuf[3] = 1; 4575a2cc190SJeff Kirsher *(__be16 *) (&inbuf[16]) = cpu_to_be16(0x0015); 4585a2cc190SJeff Kirsher *(__be32 *) (&inbuf[20]) = cpu_to_be32(port); 4595a2cc190SJeff Kirsher 4605a2cc190SJeff Kirsher err = mlx4_cmd_box(dev, inmailbox->dma, outmailbox->dma, port, 3, 4615a2cc190SJeff Kirsher MLX4_CMD_MAD_IFC, MLX4_CMD_TIME_CLASS_C); 4625a2cc190SJeff Kirsher if (!err) 4635a2cc190SJeff Kirsher *caps = *(__be32 *) (outbuf + 84); 4645a2cc190SJeff Kirsher mlx4_free_cmd_mailbox(dev, inmailbox); 4655a2cc190SJeff Kirsher mlx4_free_cmd_mailbox(dev, outmailbox); 4665a2cc190SJeff Kirsher return err; 4675a2cc190SJeff Kirsher } 4685a2cc190SJeff Kirsher 4695a2cc190SJeff Kirsher int mlx4_SET_PORT(struct mlx4_dev *dev, u8 port) 4705a2cc190SJeff Kirsher { 4715a2cc190SJeff Kirsher struct mlx4_cmd_mailbox *mailbox; 4725a2cc190SJeff Kirsher int err; 4735a2cc190SJeff Kirsher 4745a2cc190SJeff Kirsher if (dev->caps.port_type[port] == MLX4_PORT_TYPE_ETH) 4755a2cc190SJeff Kirsher return 0; 4765a2cc190SJeff Kirsher 4775a2cc190SJeff Kirsher mailbox = mlx4_alloc_cmd_mailbox(dev); 4785a2cc190SJeff Kirsher if (IS_ERR(mailbox)) 4795a2cc190SJeff Kirsher return PTR_ERR(mailbox); 4805a2cc190SJeff Kirsher 4815a2cc190SJeff Kirsher memset(mailbox->buf, 0, 256); 4825a2cc190SJeff Kirsher 4835a2cc190SJeff Kirsher ((__be32 *) mailbox->buf)[1] = dev->caps.ib_port_def_cap[port]; 4845a2cc190SJeff Kirsher err = mlx4_cmd(dev, mailbox->dma, port, 0, MLX4_CMD_SET_PORT, 4855a2cc190SJeff Kirsher MLX4_CMD_TIME_CLASS_B); 4865a2cc190SJeff Kirsher 4875a2cc190SJeff Kirsher mlx4_free_cmd_mailbox(dev, mailbox); 4885a2cc190SJeff Kirsher return err; 4895a2cc190SJeff Kirsher } 490