xref: /linux/drivers/net/dsa/realtek/rtl8365mb_table.h (revision fbafdd3b224a03b7b335de144f44a600de937586)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Look-up table query interface for the rtl8365mb switch family
3  *
4  * Copyright (C) 2022 Alvin Šipraga <alsi@bang-olufsen.dk>
5  */
6 
7 #ifndef _REALTEK_RTL8365MB_TABLE_H
8 #define _REALTEK_RTL8365MB_TABLE_H
9 
10 #include <linux/if_ether.h>
11 #include <linux/types.h>
12 
13 #include "realtek.h"
14 
15 #define RTL8365MB_TABLE_ENTRY_MAX_SIZE			10
16 
17 /*
18  * enum rtl8365mb_table - available switch tables
19  * @RTL8365MB_TABLE_ACL_RULE: ACL rules
20  * @RTL8365MB_TABLE_ACL_ACTION: ACL actions
21  * @RTL8365MB_TABLE_CVLAN: VLAN4k configurations
22  * @RTL8365MB_TABLE_L2: filtering database (2K hash table)
23  * @RTL8365MB_TABLE_IGMP_GROUP: IGMP group database (readonly)
24  *
25  * NOTE: Don't change the enum values. They must concur with the field
26  * described by @RTL8365MB_TABLE_CTRL_TABLE_MASK.
27  */
28 enum rtl8365mb_table {
29 	RTL8365MB_TABLE_ACL_RULE = 1,
30 	RTL8365MB_TABLE_ACL_ACTION = 2,
31 	RTL8365MB_TABLE_CVLAN = 3,
32 	RTL8365MB_TABLE_L2 = 4,
33 	RTL8365MB_TABLE_IGMP_GROUP = 5,
34 };
35 
36 /*
37  * enum rtl8365mb_table_op - table query operation
38  * @RTL8365MB_TABLE_OP_READ: read an entry from the target table
39  * @RTL8365MB_TABLE_OP_WRITE: write an entry to the target table
40  *
41  * NOTE: Don't change the enum values. They must concur with the field
42  * described by @RTL8365MB_TABLE_CTRL_OP_MASK.
43  */
44 enum rtl8365mb_table_op {
45 	RTL8365MB_TABLE_OP_READ = 0,
46 	RTL8365MB_TABLE_OP_WRITE = 1,
47 };
48 
49 /*
50  * enum rtl8365mb_table_l2_method - look-up method for read queries of L2 table
51  * @RTL8365MB_TABLE_L2_METHOD_MAC: look-up by source MAC address and FID (or
52  *   VID)
53  * @RTL8365MB_TABLE_L2_METHOD_ADDR: look-up by entry address
54  * @RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT: look-up next entry starting from the
55  *   supplied address
56  * @RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC: same as ADDR_NEXT but search only
57  *   unicast addresses
58  * @RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC: same as ADDR_NEXT but search only
59  *   multicast addresses
60  * @RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT: same as ADDR_NEXT_UC but
61  *   search only entries with matching source port
62  *
63  * NOTE: Don't change the enum values. They must concur with the field
64  * described by @RTL8365MB_TABLE_CTRL_METHOD_MASK
65  */
66 enum rtl8365mb_table_l2_method {
67 	RTL8365MB_TABLE_L2_METHOD_MAC = 0,
68 	RTL8365MB_TABLE_L2_METHOD_ADDR = 1,
69 	RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT = 2,
70 	RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC = 3,
71 	RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC = 4,
72 	/*
73 	 * RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC_L3 = 5,
74 	 * RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_MC_L2L3 = 6,
75 	 */
76 	RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT = 7,
77 };
78 
79 /*
80  * rtl8365mb_table_query() - read from or write to a switch table
81  * @priv: driver context
82  * @table: target table, see &enum rtl8365mb_table
83  * @op: read or write operation, see &enum rtl8365mb_table_op
84  * @addr: table address. For indexed tables, this selects the entry to access.
85  *        For L2 read queries, it is ignored as input for MAC-based lookup
86  *        methods and used as input for address-based lookup methods. On
87  *        successful L2 queries, it is updated with the matched entry address.
88  * @method: L2 table lookup method, see &enum rtl8365mb_table_l2_method.
89  *	    Ignored for non-L2 tables.
90  * @port: for L2 read queries using method
91  *        %RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT, restrict the search
92  *        to entries associated with this source port. Ignored otherwise.
93  * @data: data buffer used to read from or write to the table. For L2 MAC
94  *        lookups, this buffer provides the lookup key and receives the
95  *        matched entry contents on success.
96  * @size: size of @data in 16-bit words. The caller must ensure that @size
97  *        matches the target table's entry size and does not exceed
98  *        RTL8365MB_TABLE_ENTRY_MAX_SIZE.
99  *
100  * This function provides unified access to the internal tables of the switch.
101  * All tables except the L2 table are simple indexed tables, where @addr
102  * selects the entry and @op determines whether the access is a read or a
103  * write operation.
104  *
105  * The content of @data is used as input when writing to tables or when
106  * specifying the lookup key for L2 MAC searches, and as output for all
107  * successful read operations. It remains unchanged during write operations or
108  * failed read operations that return %-ENOENT. For other errors during read
109  * operations, it is undefined.
110  *
111  * The L2 table is a hash table and supports multiple lookup methods. For
112  * %RTL8365MB_TABLE_L2_METHOD_MAC, an entry is searched based on the MAC
113  * address and FID/VID fields provided in @data, using the same format as
114  * an L2 table entry. Address-based methods either read a specific entry
115  * (%RTL8365MB_TABLE_L2_METHOD_ADDR) or iterate over valid entries starting
116  * from @addr (%RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT and variants). When using
117  * %RTL8365MB_TABLE_L2_METHOD_ADDR_NEXT_UC_PORT, only entries associated with
118  * the specified @port are considered.
119  *
120  * On successful L2 operations, @addr is updated with the matched table address
121  * or allocated entry address. If no matching entry is found, or if an L2 write
122  * operation fails (e.g., due to a full table during addition or a missing entry
123  * during deletion), %-ENOENT is returned and @addr remains unchanged. It is the
124  * caller's responsibility to map the returned error to the appropriate
125  * semantic error.
126  *
127  * @size must match the size of the target table entry, expressed in 16-bit
128  * words.
129  *
130  * Return: 0 on success, or a negative error code on failure.
131  */
132 int rtl8365mb_table_query(struct realtek_priv *priv,
133 			  enum rtl8365mb_table table,
134 			  enum rtl8365mb_table_op op, u16 *addr,
135 			  enum rtl8365mb_table_l2_method method,
136 			  u16 port, u16 *data, size_t size);
137 
138 #endif /* _REALTEK_RTL8365MB_TABLE_H */
139