forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/net/ethernet/mellanox/mlx5/core/lib/mpfs.c
....@@ -33,6 +33,7 @@
3333 #include <linux/etherdevice.h>
3434 #include <linux/mlx5/driver.h>
3535 #include <linux/mlx5/mlx5_ifc.h>
36
+#include <linux/mlx5/mpfs.h>
3637 #include <linux/mlx5/eswitch.h>
3738 #include "mlx5_core.h"
3839 #include "lib/mpfs.h"
....@@ -40,8 +41,7 @@
4041 /* HW L2 Table (MPFS) management */
4142 static int set_l2table_entry_cmd(struct mlx5_core_dev *dev, u32 index, u8 *mac)
4243 {
43
- u32 in[MLX5_ST_SZ_DW(set_l2_table_entry_in)] = {0};
44
- u32 out[MLX5_ST_SZ_DW(set_l2_table_entry_out)] = {0};
44
+ u32 in[MLX5_ST_SZ_DW(set_l2_table_entry_in)] = {};
4545 u8 *in_mac_addr;
4646
4747 MLX5_SET(set_l2_table_entry_in, in, opcode, MLX5_CMD_OP_SET_L2_TABLE_ENTRY);
....@@ -50,23 +50,23 @@
5050 in_mac_addr = MLX5_ADDR_OF(set_l2_table_entry_in, in, mac_address);
5151 ether_addr_copy(&in_mac_addr[2], mac);
5252
53
- return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
53
+ return mlx5_cmd_exec_in(dev, set_l2_table_entry, in);
5454 }
5555
5656 static int del_l2table_entry_cmd(struct mlx5_core_dev *dev, u32 index)
5757 {
58
- u32 in[MLX5_ST_SZ_DW(delete_l2_table_entry_in)] = {0};
59
- u32 out[MLX5_ST_SZ_DW(delete_l2_table_entry_out)] = {0};
58
+ u32 in[MLX5_ST_SZ_DW(delete_l2_table_entry_in)] = {};
6059
6160 MLX5_SET(delete_l2_table_entry_in, in, opcode, MLX5_CMD_OP_DELETE_L2_TABLE_ENTRY);
6261 MLX5_SET(delete_l2_table_entry_in, in, table_index, index);
63
- return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
62
+ return mlx5_cmd_exec_in(dev, delete_l2_table_entry, in);
6463 }
6564
6665 /* UC L2 table hash node */
6766 struct l2table_node {
6867 struct l2addr_node node;
6968 u32 index; /* index in HW l2 table */
69
+ int ref_count;
7070 };
7171
7272 struct mlx5_mpfs {
....@@ -108,8 +108,7 @@
108108
109109 mutex_init(&mpfs->lock);
110110 mpfs->size = l2table_size;
111
- mpfs->bitmap = kcalloc(BITS_TO_LONGS(l2table_size),
112
- sizeof(uintptr_t), GFP_KERNEL);
111
+ mpfs->bitmap = bitmap_zalloc(l2table_size, GFP_KERNEL);
113112 if (!mpfs->bitmap) {
114113 kfree(mpfs);
115114 return -ENOMEM;
....@@ -123,11 +122,11 @@
123122 {
124123 struct mlx5_mpfs *mpfs = dev->priv.mpfs;
125124
126
- if (!MLX5_ESWITCH_MANAGER(dev))
125
+ if (!mpfs)
127126 return;
128127
129128 WARN_ON(!hlist_empty(mpfs->hash));
130
- kfree(mpfs->bitmap);
129
+ bitmap_free(mpfs->bitmap);
131130 kfree(mpfs);
132131 }
133132
....@@ -135,43 +134,49 @@
135134 {
136135 struct mlx5_mpfs *mpfs = dev->priv.mpfs;
137136 struct l2table_node *l2addr;
137
+ int err = 0;
138138 u32 index;
139
- int err;
140139
141
- if (!MLX5_ESWITCH_MANAGER(dev))
140
+ if (!mpfs)
142141 return 0;
143142
144143 mutex_lock(&mpfs->lock);
145144
146145 l2addr = l2addr_hash_find(mpfs->hash, mac, struct l2table_node);
147146 if (l2addr) {
148
- err = -EEXIST;
149
- goto abort;
147
+ l2addr->ref_count++;
148
+ goto out;
150149 }
151150
152151 err = alloc_l2table_index(mpfs, &index);
153152 if (err)
154
- goto abort;
153
+ goto out;
155154
156155 l2addr = l2addr_hash_add(mpfs->hash, mac, struct l2table_node, GFP_KERNEL);
157156 if (!l2addr) {
158
- free_l2table_index(mpfs, index);
159157 err = -ENOMEM;
160
- goto abort;
158
+ goto hash_add_err;
161159 }
160
+
161
+ err = set_l2table_entry_cmd(dev, index, mac);
162
+ if (err)
163
+ goto set_table_entry_err;
162164
163165 l2addr->index = index;
164
- err = set_l2table_entry_cmd(dev, index, mac);
165
- if (err) {
166
- l2addr_hash_del(l2addr);
167
- free_l2table_index(mpfs, index);
168
- }
166
+ l2addr->ref_count = 1;
169167
170168 mlx5_core_dbg(dev, "MPFS mac added %pM, index (%d)\n", mac, index);
171
-abort:
169
+ goto out;
170
+
171
+set_table_entry_err:
172
+ l2addr_hash_del(l2addr);
173
+hash_add_err:
174
+ free_l2table_index(mpfs, index);
175
+out:
172176 mutex_unlock(&mpfs->lock);
173177 return err;
174178 }
179
+EXPORT_SYMBOL(mlx5_mpfs_add_mac);
175180
176181 int mlx5_mpfs_del_mac(struct mlx5_core_dev *dev, u8 *mac)
177182 {
....@@ -180,7 +185,7 @@
180185 int err = 0;
181186 u32 index;
182187
183
- if (!MLX5_ESWITCH_MANAGER(dev))
188
+ if (!mpfs)
184189 return 0;
185190
186191 mutex_lock(&mpfs->lock);
....@@ -191,6 +196,9 @@
191196 goto unlock;
192197 }
193198
199
+ if (--l2addr->ref_count > 0)
200
+ goto unlock;
201
+
194202 index = l2addr->index;
195203 del_l2table_entry_cmd(dev, index);
196204 l2addr_hash_del(l2addr);
....@@ -200,3 +208,4 @@
200208 mutex_unlock(&mpfs->lock);
201209 return err;
202210 }
211
+EXPORT_SYMBOL(mlx5_mpfs_del_mac);