123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
#include <linux/etherdevice.h>
#include <linux/mlx5/driver.h>
#include <linux/mlx5/mlx5_ifc.h>
#include <linux/mlx5/mpfs.h>
#include <linux/mlx5/eswitch.h>
#include "mlx5_core.h"
#include "lib/mpfs.h"
static int set_l2table_entry_cmd(struct mlx5_core_dev *dev, u32 index, u8 *mac)
{
u32 in[MLX5_ST_SZ_DW(set_l2_table_entry_in)] = {};
u8 *in_mac_addr;
MLX5_SET(set_l2_table_entry_in, in, opcode, MLX5_CMD_OP_SET_L2_TABLE_ENTRY);
MLX5_SET(set_l2_table_entry_in, in, table_index, index);
in_mac_addr = MLX5_ADDR_OF(set_l2_table_entry_in, in, mac_address);
ether_addr_copy(&in_mac_addr[2], mac);
return mlx5_cmd_exec_in(dev, set_l2_table_entry, in);
}
static int del_l2table_entry_cmd(struct mlx5_core_dev *dev, u32 index)
{
u32 in[MLX5_ST_SZ_DW(delete_l2_table_entry_in)] = {};
MLX5_SET(delete_l2_table_entry_in, in, opcode, MLX5_CMD_OP_DELETE_L2_TABLE_ENTRY);
MLX5_SET(delete_l2_table_entry_in, in, table_index, index);
return mlx5_cmd_exec_in(dev, delete_l2_table_entry, in);
}
struct l2table_node {
struct l2addr_node node;
int index;
int ref_count;
};
struct mlx5_mpfs {
struct hlist_head hash[MLX5_L2_ADDR_HASH_SIZE];
struct mutex lock;
bool enabled;
u32 size;
unsigned long *bitmap;
};
static int alloc_l2table_index(struct mlx5_mpfs *l2table, u32 *ix)
{
int err = 0;
*ix = find_first_zero_bit(l2table->bitmap, l2table->size);
if (*ix >= l2table->size)
err = -ENOSPC;
else
__set_bit(*ix, l2table->bitmap);
return err;
}
static void free_l2table_index(struct mlx5_mpfs *l2table, u32 ix)
{
__clear_bit(ix, l2table->bitmap);
}
int mlx5_mpfs_init(struct mlx5_core_dev *dev)
{
int l2table_size = 1 << MLX5_CAP_GEN(dev, log_max_l2_table);
struct mlx5_mpfs *mpfs;
if (!MLX5_ESWITCH_MANAGER(dev) || l2table_size == 1)
return 0;
mpfs = kzalloc_obj(*mpfs);
if (!mpfs)
return -ENOMEM;
mutex_init(&mpfs->lock);
mpfs->size = l2table_size;
mpfs->bitmap = bitmap_zalloc(l2table_size, GFP_KERNEL);
if (!mpfs->bitmap) {
kfree(mpfs);
return -ENOMEM;
}
mpfs->enabled = true;
dev->priv.mpfs = mpfs;
return 0;
}
void mlx5_mpfs_cleanup(struct mlx5_core_dev *dev)
{
struct mlx5_mpfs *mpfs = dev->priv.mpfs;
if (!mpfs)
return;
WARN_ON(!hlist_empty(mpfs->hash));
bitmap_free(mpfs->bitmap);
kfree(mpfs);
}
int mlx5_mpfs_add_mac(struct mlx5_core_dev *dev, u8 *mac)
{
struct mlx5_mpfs *mpfs = dev->priv.mpfs;
struct l2table_node *l2addr;
int err = 0;
int index;
if (!mpfs)
return 0;
mutex_lock(&mpfs->lock);
l2addr = l2addr_hash_find(mpfs->hash, mac, struct l2table_node);
if (l2addr) {
l2addr->ref_count++;
goto out;
}
l2addr = l2addr_hash_add(mpfs->hash, mac, struct l2table_node, GFP_KERNEL);
if (!l2addr) {
err = -ENOMEM;
goto out;
}
index = -1;
if (mpfs->enabled) {
err = alloc_l2table_index(mpfs, &index);
if (err)
goto hash_del;
err = set_l2table_entry_cmd(dev, index, mac);
if (err)
goto free_l2table_index;
mlx5_core_dbg(dev, "MPFS entry %pM, set @index (%d)\n",
l2addr->node.addr, index);
}
l2addr->index = index;
l2addr->ref_count = 1;
mlx5_core_dbg(dev, "MPFS mac added %pM, index (%d)\n", mac, index);
goto out;
free_l2table_index:
free_l2table_index(mpfs, index);
hash_del:
l2addr_hash_del(l2addr);
out:
mutex_unlock(&mpfs->lock);
return err;
}
EXPORT_SYMBOL(mlx5_mpfs_add_mac);
int mlx5_mpfs_del_mac(struct mlx5_core_dev *dev, u8 *mac)
{
struct mlx5_mpfs *mpfs = dev->priv.mpfs;
struct l2table_node *l2addr;
int err = 0;
int index;
if (!mpfs)
return 0;
mutex_lock(&mpfs->lock);
l2addr = l2addr_hash_find(mpfs->hash, mac, struct l2table_node);
if (!l2addr) {
err = -ENOENT;
goto unlock;
}
if (--l2addr->ref_count > 0)
goto unlock;
index = l2addr->index;
if (index >= 0) {
del_l2table_entry_cmd(dev, index);
free_l2table_index(mpfs, index);
mlx5_core_dbg(dev, "MPFS entry %pM, deleted @index (%d)\n",
mac, index);
}
l2addr_hash_del(l2addr);
mlx5_core_dbg(dev, "MPFS mac deleted %pM, index (%d)\n", mac, index);
unlock:
mutex_unlock(&mpfs->lock);
return err;
}
EXPORT_SYMBOL(mlx5_mpfs_del_mac);
int mlx5_mpfs_enable(struct mlx5_core_dev *dev)
{
struct mlx5_mpfs *mpfs = dev->priv.mpfs;
struct l2table_node *l2addr;
struct hlist_node *n;
int err = 0, i;
if (!mpfs)
return -ENODEV;
mutex_lock(&mpfs->lock);
if (mpfs->enabled)
goto out;
mpfs->enabled = true;
mlx5_core_dbg(dev, "MPFS enabling mpfs\n");
mlx5_mpfs_foreach(l2addr, n, mpfs, i) {
u32 index;
err = alloc_l2table_index(mpfs, &index);
if (err) {
mlx5_core_err(dev, "Failed to allocated MPFS index for %pM, err(%d)\n",
l2addr->node.addr, err);
goto out;
}
err = set_l2table_entry_cmd(dev, index, l2addr->node.addr);
if (err) {
mlx5_core_err(dev, "Failed to set MPFS l2table entry for %pM index=%d, err(%d)\n",
l2addr->node.addr, index, err);
free_l2table_index(mpfs, index);
goto out;
}
l2addr->index = index;
mlx5_core_dbg(dev, "MPFS entry %pM, set @index (%d)\n",
l2addr->node.addr, l2addr->index);
}
out:
mutex_unlock(&mpfs->lock);
return err;
}
void mlx5_mpfs_disable(struct mlx5_core_dev *dev)
{
struct mlx5_mpfs *mpfs = dev->priv.mpfs;
struct l2table_node *l2addr;
struct hlist_node *n;
int i;
if (!mpfs)
return;
mutex_lock(&mpfs->lock);
if (!mpfs->enabled)
goto unlock;
mlx5_mpfs_foreach(l2addr, n, mpfs, i) {
if (l2addr->index < 0)
continue;
del_l2table_entry_cmd(dev, l2addr->index);
free_l2table_index(mpfs, l2addr->index);
mlx5_core_dbg(dev, "MPFS entry %pM, deleted @index (%d)\n",
l2addr->node.addr, l2addr->index);
l2addr->index = -1;
}
mpfs->enabled = false;
mlx5_core_dbg(dev, "MPFS disabled\n");
unlock:
mutex_unlock(&mpfs->lock);
}