xref: /freebsd/sys/dev/mlx5/mlx5_core/mlx5_mpfs.c (revision d0b2dbfa)
1 /*-
2  * Copyright (c) 2019, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include "opt_rss.h"
27 #include "opt_ratelimit.h"
28 
29 #include <linux/types.h>
30 #include <linux/etherdevice.h>
31 
32 #include <dev/mlx5/mlx5_ifc.h>
33 #include <dev/mlx5/device.h>
34 #include <dev/mlx5/mpfs.h>
35 #include <dev/mlx5/driver.h>
36 
37 #include <dev/mlx5/mlx5_core/mlx5_core.h>
38 
39 #define	MPFS_LOCK(dev) spin_lock(&(dev)->mpfs.spinlock)
40 #define	MPFS_UNLOCK(dev) spin_unlock(&(dev)->mpfs.spinlock)
41 
42 int
43 mlx5_mpfs_add_mac(struct mlx5_core_dev *dev, u32 *p_index, const u8 *mac,
44     u8 vlan_valid, u16 vlan)
45 {
46 	const u32 l2table_size = MIN(1U << MLX5_CAP_GEN(dev, log_max_l2_table),
47 	    MLX5_MPFS_TABLE_MAX);
48 	u32 in[MLX5_ST_SZ_DW(set_l2_table_entry_in)] = {};
49 	u32 out[MLX5_ST_SZ_DW(set_l2_table_entry_out)] = {};
50 	u8 *in_mac_addr;
51 	u32 index;
52 	int err;
53 
54 	if (!MLX5_CAP_GEN(dev, eswitch_flow_table)) {
55 		*p_index = 0;
56 		return (0);
57 	}
58 
59 	MPFS_LOCK(dev);
60 	index = find_first_zero_bit(dev->mpfs.bitmap, l2table_size);
61 	if (index < l2table_size)
62 		set_bit(index, dev->mpfs.bitmap);
63 	MPFS_UNLOCK(dev);
64 
65 	if (index >= l2table_size)
66 		return (-ENOMEM);
67 
68 	MLX5_SET(set_l2_table_entry_in, in, opcode, MLX5_CMD_OP_SET_L2_TABLE_ENTRY);
69 	MLX5_SET(set_l2_table_entry_in, in, table_index, index);
70 	MLX5_SET(set_l2_table_entry_in, in, vlan_valid, vlan_valid);
71 	MLX5_SET(set_l2_table_entry_in, in, vlan, vlan);
72 
73 	in_mac_addr = MLX5_ADDR_OF(set_l2_table_entry_in, in, mac_address);
74 	ether_addr_copy(&in_mac_addr[2], mac);
75 
76 	err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
77 	if (err != 0) {
78 		MPFS_LOCK(dev);
79 		clear_bit(index, dev->mpfs.bitmap);
80 		MPFS_UNLOCK(dev);
81 	} else {
82 		*p_index = index;
83 	}
84 	return (err);
85 }
86 
87 int
88 mlx5_mpfs_del_mac(struct mlx5_core_dev *dev, u32 index)
89 {
90 	u32 in[MLX5_ST_SZ_DW(delete_l2_table_entry_in)] = {};
91 	u32 out[MLX5_ST_SZ_DW(delete_l2_table_entry_out)] = {};
92 	int err;
93 
94 	if (!MLX5_CAP_GEN(dev, eswitch_flow_table)) {
95 		if (index != 0)
96 			return (-EINVAL);
97 		return (0);
98 	}
99 
100 	MLX5_SET(delete_l2_table_entry_in, in, opcode, MLX5_CMD_OP_DELETE_L2_TABLE_ENTRY);
101 	MLX5_SET(delete_l2_table_entry_in, in, table_index, index);
102 
103 	err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
104 	if (err == 0) {
105 		MPFS_LOCK(dev);
106 		clear_bit(index, dev->mpfs.bitmap);
107 		MPFS_UNLOCK(dev);
108 	}
109 	return (err);
110 }
111 
112 int
113 mlx5_mpfs_init(struct mlx5_core_dev *dev)
114 {
115 
116 	spin_lock_init(&dev->mpfs.spinlock);
117 	bitmap_zero(dev->mpfs.bitmap, MLX5_MPFS_TABLE_MAX);
118 	return (0);
119 }
120 
121 void
122 mlx5_mpfs_destroy(struct mlx5_core_dev *dev)
123 {
124 	u32 num;
125 
126 	num = bitmap_weight(dev->mpfs.bitmap, MLX5_MPFS_TABLE_MAX);
127 	if (num != 0)
128 		mlx5_core_err(dev, "Leaking %u MPFS MAC table entries\n", num);
129 
130 	spin_lock_destroy(&dev->mpfs.spinlock);
131 }
132