]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/commitdiff
mlx4_core: Ethernet MAC/VLAN management
authorYevgeny Petrilin <yevgenyp@mellanox.co.il>
Wed, 22 Oct 2008 18:44:46 +0000 (11:44 -0700)
committerRoland Dreier <rolandd@cisco.com>
Wed, 22 Oct 2008 18:44:46 +0000 (11:44 -0700)
Add support for managing MAC and VLAN filters for each port.

Signed-off-by: Yevgeny Petrilin <yevgenyp@mellanox.co.il>
Signed-off-by: Oren Duer <oren@mellanox.co.il>
Signed-off-by: Roland Dreier <rolandd@cisco.com>
drivers/net/mlx4/Makefile
drivers/net/mlx4/main.c
drivers/net/mlx4/mlx4.h
drivers/net/mlx4/port.c [new file with mode: 0644]
include/linux/mlx4/cmd.h
include/linux/mlx4/device.h

index 0952a6528f583edd0139965a6300c8f4836ff5fd..9f493666e27b3451503e09029716ef86dc966ee2 100644 (file)
@@ -1,4 +1,4 @@
 obj-$(CONFIG_MLX4_CORE)                += mlx4_core.o
 
 mlx4_core-y := alloc.o catas.o cmd.o cq.o eq.o fw.o icm.o intf.o main.o mcg.o \
-               mr.o pd.o profile.o qp.o reset.o srq.o
+               mr.o pd.o port.o profile.o qp.o reset.o srq.o
index 28f36b88de380f78a9fb2fbbdb7d10376a2c02ca..0a5c8bfb3f1fe0099320b4824ca825cdcf15fe3d 100644 (file)
@@ -780,11 +780,22 @@ no_msi:
                priv->eq_table.eq[i].irq = dev->pdev->irq;
 }
 
+static void mlx4_init_port_info(struct mlx4_dev *dev, int port)
+{
+       struct mlx4_port_info *info = &mlx4_priv(dev)->port[port];
+
+       info->dev = dev;
+       info->port = port;
+       mlx4_init_mac_table(dev, &info->mac_table);
+       mlx4_init_vlan_table(dev, &info->vlan_table);
+}
+
 static int __mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
 {
        struct mlx4_priv *priv;
        struct mlx4_dev *dev;
        int err;
+       int port;
 
        printk(KERN_INFO PFX "Initializing %s\n",
               pci_name(pdev));
@@ -894,6 +905,9 @@ static int __mlx4_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
        if (err)
                goto err_close;
 
+       for (port = 1; port <= dev->caps.num_ports; port++)
+               mlx4_init_port_info(dev, port);
+
        err = mlx4_register_device(dev);
        if (err)
                goto err_cleanup;
index 9e2f44c31810759316508dc7afe8e8ab2c55183b..23309f381ee362d15844e9ff4d63a2a6e653a991 100644 (file)
@@ -252,6 +252,35 @@ struct mlx4_catas_err {
        struct list_head        list;
 };
 
+#define MLX4_MAX_MAC_NUM       128
+#define MLX4_MAC_TABLE_SIZE    (MLX4_MAX_MAC_NUM << 3)
+
+struct mlx4_mac_table {
+       __be64                  entries[MLX4_MAX_MAC_NUM];
+       int                     refs[MLX4_MAX_MAC_NUM];
+       struct mutex            mutex;
+       int                     total;
+       int                     max;
+};
+
+#define MLX4_MAX_VLAN_NUM      128
+#define MLX4_VLAN_TABLE_SIZE   (MLX4_MAX_VLAN_NUM << 2)
+
+struct mlx4_vlan_table {
+       __be32                  entries[MLX4_MAX_VLAN_NUM];
+       int                     refs[MLX4_MAX_VLAN_NUM];
+       struct mutex            mutex;
+       int                     total;
+       int                     max;
+};
+
+struct mlx4_port_info {
+       struct mlx4_dev        *dev;
+       int                     port;
+       struct mlx4_mac_table   mac_table;
+       struct mlx4_vlan_table  vlan_table;
+};
+
 struct mlx4_priv {
        struct mlx4_dev         dev;
 
@@ -280,6 +309,7 @@ struct mlx4_priv {
 
        struct mlx4_uar         driver_uar;
        void __iomem           *kar;
+       struct mlx4_port_info   port[MLX4_MAX_PORTS + 1];
 };
 
 static inline struct mlx4_priv *mlx4_priv(struct mlx4_dev *dev)
@@ -350,4 +380,7 @@ void mlx4_srq_event(struct mlx4_dev *dev, u32 srqn, int event_type);
 
 void mlx4_handle_catas_err(struct mlx4_dev *dev);
 
+void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table);
+void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table);
+
 #endif /* MLX4_H */
diff --git a/drivers/net/mlx4/port.c b/drivers/net/mlx4/port.c
new file mode 100644 (file)
index 0000000..8644f3d
--- /dev/null
@@ -0,0 +1,259 @@
+/*
+ * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
+ *
+ * This software is available to you under a choice of one of two
+ * licenses.  You may choose to be licensed under the terms of the GNU
+ * General Public License (GPL) Version 2, available from the file
+ * COPYING in the main directory of this source tree, or the
+ * OpenIB.org BSD license below:
+ *
+ *     Redistribution and use in source and binary forms, with or
+ *     without modification, are permitted provided that the following
+ *     conditions are met:
+ *
+ *      - Redistributions of source code must retain the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer.
+ *
+ *      - Redistributions in binary form must reproduce the above
+ *        copyright notice, this list of conditions and the following
+ *        disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <linux/errno.h>
+#include <linux/if_ether.h>
+
+#include <linux/mlx4/cmd.h>
+
+#include "mlx4.h"
+
+#define MLX4_MAC_VALID         (1ull << 63)
+#define MLX4_MAC_MASK          0xffffffffffffULL
+
+#define MLX4_VLAN_VALID                (1u << 31)
+#define MLX4_VLAN_MASK         0xfff
+
+void mlx4_init_mac_table(struct mlx4_dev *dev, struct mlx4_mac_table *table)
+{
+       int i;
+
+       mutex_init(&table->mutex);
+       for (i = 0; i < MLX4_MAX_MAC_NUM; i++) {
+               table->entries[i] = 0;
+               table->refs[i]   = 0;
+       }
+       table->max   = 1 << dev->caps.log_num_macs;
+       table->total = 0;
+}
+
+void mlx4_init_vlan_table(struct mlx4_dev *dev, struct mlx4_vlan_table *table)
+{
+       int i;
+
+       mutex_init(&table->mutex);
+       for (i = 0; i < MLX4_MAX_VLAN_NUM; i++) {
+               table->entries[i] = 0;
+               table->refs[i]   = 0;
+       }
+       table->max   = 1 << dev->caps.log_num_vlans;
+       table->total = 0;
+}
+
+static int mlx4_set_port_mac_table(struct mlx4_dev *dev, u8 port,
+                                  __be64 *entries)
+{
+       struct mlx4_cmd_mailbox *mailbox;
+       u32 in_mod;
+       int err;
+
+       mailbox = mlx4_alloc_cmd_mailbox(dev);
+       if (IS_ERR(mailbox))
+               return PTR_ERR(mailbox);
+
+       memcpy(mailbox->buf, entries, MLX4_MAC_TABLE_SIZE);
+
+       in_mod = MLX4_SET_PORT_MAC_TABLE << 8 | port;
+       err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
+                      MLX4_CMD_TIME_CLASS_B);
+
+       mlx4_free_cmd_mailbox(dev, mailbox);
+       return err;
+}
+
+int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *index)
+{
+       struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
+       int i, err = 0;
+       int free = -1;
+
+       mlx4_dbg(dev, "Registering MAC: 0x%llx\n", (unsigned long long) mac);
+       mutex_lock(&table->mutex);
+       for (i = 0; i < MLX4_MAX_MAC_NUM - 1; i++) {
+               if (free < 0 && !table->refs[i]) {
+                       free = i;
+                       continue;
+               }
+
+               if (mac == (MLX4_MAC_MASK & be64_to_cpu(table->entries[i]))) {
+                       /* MAC already registered, increase refernce count */
+                       *index = i;
+                       ++table->refs[i];
+                       goto out;
+               }
+       }
+       mlx4_dbg(dev, "Free MAC index is %d\n", free);
+
+       if (table->total == table->max) {
+               /* No free mac entries */
+               err = -ENOSPC;
+               goto out;
+       }
+
+       /* Register new MAC */
+       table->refs[free] = 1;
+       table->entries[free] = cpu_to_be64(mac | MLX4_MAC_VALID);
+
+       err = mlx4_set_port_mac_table(dev, port, table->entries);
+       if (unlikely(err)) {
+               mlx4_err(dev, "Failed adding MAC: 0x%llx\n", (unsigned long long) mac);
+               table->refs[free] = 0;
+               table->entries[free] = 0;
+               goto out;
+       }
+
+       *index = free;
+       ++table->total;
+out:
+       mutex_unlock(&table->mutex);
+       return err;
+}
+EXPORT_SYMBOL_GPL(mlx4_register_mac);
+
+void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int index)
+{
+       struct mlx4_mac_table *table = &mlx4_priv(dev)->port[port].mac_table;
+
+       mutex_lock(&table->mutex);
+       if (!table->refs[index]) {
+               mlx4_warn(dev, "No MAC entry for index %d\n", index);
+               goto out;
+       }
+       if (--table->refs[index]) {
+               mlx4_warn(dev, "Have more references for index %d,"
+                         "no need to modify MAC table\n", index);
+               goto out;
+       }
+       table->entries[index] = 0;
+       mlx4_set_port_mac_table(dev, port, table->entries);
+       --table->total;
+out:
+       mutex_unlock(&table->mutex);
+}
+EXPORT_SYMBOL_GPL(mlx4_unregister_mac);
+
+static int mlx4_set_port_vlan_table(struct mlx4_dev *dev, u8 port,
+                                   __be32 *entries)
+{
+       struct mlx4_cmd_mailbox *mailbox;
+       u32 in_mod;
+       int err;
+
+       mailbox = mlx4_alloc_cmd_mailbox(dev);
+       if (IS_ERR(mailbox))
+               return PTR_ERR(mailbox);
+
+       memcpy(mailbox->buf, entries, MLX4_VLAN_TABLE_SIZE);
+       in_mod = MLX4_SET_PORT_VLAN_TABLE << 8 | port;
+       err = mlx4_cmd(dev, mailbox->dma, in_mod, 1, MLX4_CMD_SET_PORT,
+                      MLX4_CMD_TIME_CLASS_B);
+
+       mlx4_free_cmd_mailbox(dev, mailbox);
+
+       return err;
+}
+
+int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index)
+{
+       struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
+       int i, err = 0;
+       int free = -1;
+
+       mutex_lock(&table->mutex);
+       for (i = MLX4_VLAN_REGULAR; i < MLX4_MAX_VLAN_NUM; i++) {
+               if (free < 0 && (table->refs[i] == 0)) {
+                       free = i;
+                       continue;
+               }
+
+               if (table->refs[i] &&
+                   (vlan == (MLX4_VLAN_MASK &
+                             be32_to_cpu(table->entries[i])))) {
+                       /* Vlan already registered, increase refernce count */
+                       *index = i;
+                       ++table->refs[i];
+                       goto out;
+               }
+       }
+
+       if (table->total == table->max) {
+               /* No free vlan entries */
+               err = -ENOSPC;
+               goto out;
+       }
+
+       /* Register new MAC */
+       table->refs[free] = 1;
+       table->entries[free] = cpu_to_be32(vlan | MLX4_VLAN_VALID);
+
+       err = mlx4_set_port_vlan_table(dev, port, table->entries);
+       if (unlikely(err)) {
+               mlx4_warn(dev, "Failed adding vlan: %u\n", vlan);
+               table->refs[free] = 0;
+               table->entries[free] = 0;
+               goto out;
+       }
+
+       *index = free;
+       ++table->total;
+out:
+       mutex_unlock(&table->mutex);
+       return err;
+}
+EXPORT_SYMBOL_GPL(mlx4_register_vlan);
+
+void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index)
+{
+       struct mlx4_vlan_table *table = &mlx4_priv(dev)->port[port].vlan_table;
+
+       if (index < MLX4_VLAN_REGULAR) {
+               mlx4_warn(dev, "Trying to free special vlan index %d\n", index);
+               return;
+       }
+
+       mutex_lock(&table->mutex);
+       if (!table->refs[index]) {
+               mlx4_warn(dev, "No vlan entry for index %d\n", index);
+               goto out;
+       }
+       if (--table->refs[index]) {
+               mlx4_dbg(dev, "Have more references for index %d,"
+                        "no need to modify vlan table\n", index);
+               goto out;
+       }
+       table->entries[index] = 0;
+       mlx4_set_port_vlan_table(dev, port, table->entries);
+       --table->total;
+out:
+       mutex_unlock(&table->mutex);
+}
+EXPORT_SYMBOL_GPL(mlx4_unregister_vlan);
index 77323a72dd3cc0517bb167b0b25803704588eb7a..cf9c679ab38b59fe8cb9794e4837e50dd1c9445d 100644 (file)
@@ -132,6 +132,15 @@ enum {
        MLX4_MAILBOX_SIZE       =  4096
 };
 
+enum {
+       /* set port opcode modifiers */
+       MLX4_SET_PORT_GENERAL   = 0x0,
+       MLX4_SET_PORT_RQP_CALC  = 0x1,
+       MLX4_SET_PORT_MAC_TABLE = 0x2,
+       MLX4_SET_PORT_VLAN_TABLE = 0x3,
+       MLX4_SET_PORT_PRIO_MAP  = 0x4,
+};
+
 struct mlx4_dev;
 
 struct mlx4_cmd_mailbox {
index f9e73cfc540bc87f983af8d06c63293cbb19b1e9..1951fe70a251149d37d57f134ba3314f47b6024e 100644 (file)
@@ -153,6 +153,12 @@ enum mlx4_qp_region {
        MLX4_NUM_QP_REGION
 };
 
+enum mlx4_special_vlan_idx {
+       MLX4_NO_VLAN_IDX        = 0,
+       MLX4_VLAN_MISS_IDX,
+       MLX4_VLAN_REGULAR
+};
+
 enum {
        MLX4_NUM_FEXCH          = 64 * 1024,
 };
@@ -438,6 +444,12 @@ int mlx4_multicast_attach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16],
                          int block_mcast_loopback);
 int mlx4_multicast_detach(struct mlx4_dev *dev, struct mlx4_qp *qp, u8 gid[16]);
 
+int mlx4_register_mac(struct mlx4_dev *dev, u8 port, u64 mac, int *index);
+void mlx4_unregister_mac(struct mlx4_dev *dev, u8 port, int index);
+
+int mlx4_register_vlan(struct mlx4_dev *dev, u8 port, u16 vlan, int *index);
+void mlx4_unregister_vlan(struct mlx4_dev *dev, u8 port, int index);
+
 int mlx4_map_phys_fmr(struct mlx4_dev *dev, struct mlx4_fmr *fmr, u64 *page_list,
                      int npages, u64 iova, u32 *lkey, u32 *rkey);
 int mlx4_fmr_alloc(struct mlx4_dev *dev, u32 pd, u32 access, int max_pages,