xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/lib/mpfs.h (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
1 /*
2  * Copyright (c) 2017, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #ifndef __MLX5_MPFS_H__
34 #define __MLX5_MPFS_H__
35 
36 #include <linux/if_ether.h>
37 #include <linux/mlx5/device.h>
38 
39 /* L2 -mac address based- hash helpers */
40 #define MLX5_L2_ADDR_HASH_SIZE (BIT(BITS_PER_BYTE))
41 #define MLX5_L2_ADDR_HASH(addr) (addr[5])
42 
43 struct l2addr_node {
44 	struct hlist_node hlist;
45 	u8                addr[ETH_ALEN];
46 };
47 
48 #define for_each_l2hash_node(hn, tmp, hash, i) \
49 	for (i = 0; i < MLX5_L2_ADDR_HASH_SIZE; i++) \
50 		hlist_for_each_entry_safe(hn, tmp, &(hash)[i], hlist)
51 
52 #define l2addr_hash_find(hash, mac, type) ({                \
53 	int ix = MLX5_L2_ADDR_HASH(mac);                    \
54 	bool found = false;                                 \
55 	type *ptr = NULL;                                   \
56 							    \
57 	hlist_for_each_entry(ptr, &(hash)[ix], node.hlist)  \
58 		if (ether_addr_equal(ptr->node.addr, mac)) {\
59 			found = true;                       \
60 			break;                              \
61 		}                                           \
62 	if (!found)                                         \
63 		ptr = NULL;                                 \
64 	ptr;                                                \
65 })
66 
67 #define l2addr_hash_add(hash, mac, type, gfp) ({            \
68 	int ix = MLX5_L2_ADDR_HASH(mac);                    \
69 	type *ptr = NULL;                                   \
70 							    \
71 	ptr = kzalloc(sizeof(type), gfp);                   \
72 	if (ptr) {                                          \
73 		ether_addr_copy(ptr->node.addr, mac);       \
74 		hlist_add_head(&ptr->node.hlist, &(hash)[ix]);\
75 	}                                                   \
76 	ptr;                                                \
77 })
78 
79 #define l2addr_hash_del(ptr) ({                             \
80 	hlist_del(&(ptr)->node.hlist);                      \
81 	kfree(ptr);                                         \
82 })
83 
84 #ifdef CONFIG_MLX5_MPFS
85 int  mlx5_mpfs_init(struct mlx5_core_dev *dev);
86 void mlx5_mpfs_cleanup(struct mlx5_core_dev *dev);
87 #else /* #ifndef CONFIG_MLX5_MPFS */
mlx5_mpfs_init(struct mlx5_core_dev * dev)88 static inline int  mlx5_mpfs_init(struct mlx5_core_dev *dev) { return 0; }
mlx5_mpfs_cleanup(struct mlx5_core_dev * dev)89 static inline void mlx5_mpfs_cleanup(struct mlx5_core_dev *dev) {}
90 #endif
91 
92 #endif
93