]> git.ipfire.org Git - thirdparty/u-boot.git/blame - drivers/net/npe/IxEthDBSearch.c
SPDX-License-Identifier: convert BSD-3-Clause files
[thirdparty/u-boot.git] / drivers / net / npe / IxEthDBSearch.c
CommitLineData
ba94a1bb
WD
1/**
2 * @file IxEthDBSearch.c
3 *
4 * @par
5 * IXP400 SW Release version 2.0
6 *
7 * -- Copyright Notice --
8 *
9 * @par
10 * Copyright 2001-2005, Intel Corporation.
11 * All rights reserved.
12 *
13 * @par
cb3761ea 14 * SPDX-License-Identifier: BSD-3-Clause
ba94a1bb
WD
15 * @par
16 * -- End of Copyright Notice --
17 */
18
19#include "IxEthDB_p.h"
20
21extern HashTable dbHashtable;
22
23/**
24 * @brief matches two database records based on their MAC addresses
25 *
26 * @param untypedReference record to match against
27 * @param untypedEntry record to match
28 *
472d5460 29 * @return true if the match is successful or false otherwise
ba94a1bb
WD
30 *
31 * @internal
32 */
33IX_ETH_DB_PUBLIC
34BOOL ixEthDBAddressRecordMatch(void *untypedReference, void *untypedEntry)
35{
36 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
37 MacDescriptor *reference = (MacDescriptor *) untypedReference;
38
39 /* check accepted record types */
472d5460 40 if ((entry->type & reference->type) == 0) return false;
ba94a1bb
WD
41
42 return (ixEthDBAddressCompare((UINT8 *) entry->macAddress, (UINT8 *) reference->macAddress) == 0);
43}
44
45/**
46 * @brief matches two database records based on their MAC addresses
47 * and VLAN IDs
48 *
49 * @param untypedReference record to match against
50 * @param untypedEntry record to match
51 *
472d5460 52 * @return true if the match is successful or false otherwise
ba94a1bb
WD
53 *
54 * @internal
55 */
56IX_ETH_DB_PUBLIC
57BOOL ixEthDBVlanRecordMatch(void *untypedReference, void *untypedEntry)
58{
59 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
60 MacDescriptor *reference = (MacDescriptor *) untypedReference;
61
62 /* check accepted record types */
472d5460 63 if ((entry->type & reference->type) == 0) return false;
ba94a1bb
WD
64
65 return (IX_ETH_DB_GET_VLAN_ID(entry->recordData.filteringVlanData.ieee802_1qTag) ==
66 IX_ETH_DB_GET_VLAN_ID(reference->recordData.filteringVlanData.ieee802_1qTag)) &&
67 (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
68}
69
70/**
71 * @brief matches two database records based on their MAC addresses
72 * and port IDs
73 *
74 * @param untypedReference record to match against
75 * @param untypedEntry record to match
76 *
472d5460 77 * @return true if the match is successful or false otherwise
ba94a1bb
WD
78 *
79 * @internal
80 */
81IX_ETH_DB_PUBLIC
82BOOL ixEthDBPortRecordMatch(void *untypedReference, void *untypedEntry)
83{
84 MacDescriptor *entry = (MacDescriptor *) untypedEntry;
85 MacDescriptor *reference = (MacDescriptor *) untypedReference;
86
87 /* check accepted record types */
472d5460 88 if ((entry->type & reference->type) == 0) return false;
ba94a1bb
WD
89
90 return (entry->portID == reference->portID) &&
91 (ixEthDBAddressCompare(entry->macAddress, reference->macAddress) == 0);
92}
93
94/**
95 * @brief dummy matching function, registered for safety
96 *
97 * @param reference record to match against (unused)
98 * @param entry record to match (unused)
99 *
100 * This function is registered in the matching functions
101 * array on invalid types. Calling it will display an
102 * error message, indicating an error in the component logic.
103 *
472d5460 104 * @return false
ba94a1bb
WD
105 *
106 * @internal
107 */
108IX_ETH_DB_PUBLIC
109BOOL ixEthDBNullMatch(void *reference, void *entry)
110{
111 /* display an error message */
112
113 ixOsalLog(IX_OSAL_LOG_LVL_WARNING, IX_OSAL_LOG_DEV_STDOUT, "DB: (Search) The NullMatch function was called, wrong key type?\n", 0, 0, 0, 0, 0, 0);
114
115
472d5460 116 return false;
ba94a1bb
WD
117}
118
119/**
120 * @brief registers hash matching methods
121 *
122 * @param matchFunctions table of match functions to be populated
123 *
124 * This function registers the available record matching functions
125 * by indexing them on record types into the given function array.
126 *
127 * Note that it is compulsory to call this in ixEthDBInit(),
128 * otherwise hashtable searching and removal will not work
129 *
130 * @return number of registered functions
131 *
132 * @internal
133 */
134IX_ETH_DB_PUBLIC
135UINT32 ixEthDBMatchMethodsRegister(MatchFunction *matchFunctions)
136{
137 UINT32 i;
138
139 /* safety first */
140 for ( i = 0 ; i < IX_ETH_DB_MAX_KEY_INDEX + 1 ; i++)
141 {
142 matchFunctions[i] = ixEthDBNullMatch;
143 }
144
145 /* register MAC search method */
146 matchFunctions[IX_ETH_DB_MAC_KEY] = ixEthDBAddressRecordMatch;
147
148 /* register MAC/PortID search method */
149 matchFunctions[IX_ETH_DB_MAC_PORT_KEY] = ixEthDBPortRecordMatch;
150
151 /* register MAC/VLAN ID search method */
152 matchFunctions[IX_ETH_DB_MAC_VLAN_KEY] = ixEthDBVlanRecordMatch;
153
154 return 3; /* three methods */
155}
156
157/**
158 * @brief search a record in the Ethernet datbase
159 *
160 * @param macAddress MAC address to perform the search on
161 * @param typeFilter type of records to consider for matching
162 *
163 * @warning if searching is successful an implicit write lock
164 * to the search result is granted, therefore unlock the
165 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
166 *
167 * @see ixEthDBReleaseHashNode()
168 *
169 * @return the search result, or NULL if a record with the given
170 * MAC address was not found
171 *
172 * @internal
173 */
174IX_ETH_DB_PUBLIC
175HashNode* ixEthDBSearch(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
176{
177 HashNode *searchResult = NULL;
178 MacDescriptor reference;
179
180 TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
181
182 if (macAddress == NULL)
183 {
184 return NULL;
185 }
186
187 /* fill search fields */
188 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
189
190 /* set acceptable record types */
191 reference.type = typeFilter;
192
193 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference, &searchResult));
194
195 return searchResult;
196}
197
198IX_ETH_DB_PUBLIC
199IxEthDBStatus ixEthDBPeek(IxEthDBMacAddr *macAddress, IxEthDBRecordType typeFilter)
200{
201 MacDescriptor reference;
202 IxEthDBStatus result;
203
204 TEST_FIXTURE_INCREMENT_DB_CORE_ACCESS_COUNTER;
205
206 if (macAddress == NULL)
207 {
208 return IX_ETH_DB_INVALID_ARG;
209 }
210
211 /* fill search fields */
212 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
213
214 /* set acceptable record types */
215 reference.type = typeFilter;
216
217 result = ixEthDBPeekHashEntry(&dbHashtable, IX_ETH_DB_MAC_KEY, &reference);
218
219 return result;
220}
221
222/**
223 * @brief search a record in the Ethernet datbase
224 *
225 * @param macAddress MAC address to perform the search on
226 * @param portID port ID to perform the search on
227 * @param typeFilter type of records to consider for matching
228 *
229 * @warning if searching is successful an implicit write lock
230 * to the search result is granted, therefore unlock the
231 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
232 *
233 * @see ixEthDBReleaseHashNode()
234 *
235 * @return the search result, or NULL if a record with the given
236 * MAC address/port ID combination was not found
237 *
238 * @internal
239 */
240IX_ETH_DB_PUBLIC
241HashNode* ixEthDBPortSearch(IxEthDBMacAddr *macAddress, IxEthDBPortId portID, IxEthDBRecordType typeFilter)
242{
243 HashNode *searchResult = NULL;
244 MacDescriptor reference;
245
246 if (macAddress == NULL)
247 {
248 return NULL;
249 }
250
251 /* fill search fields */
252 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
253 reference.portID = portID;
254
255 /* set acceptable record types */
256 reference.type = typeFilter;
257
258 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_PORT_KEY, &reference, &searchResult));
259
260 return searchResult;
261}
262
263/**
264 * @brief search a record in the Ethernet datbase
265 *
266 * @param macAddress MAC address to perform the search on
267 * @param vlanID VLAN ID to perform the search on
268 * @param typeFilter type of records to consider for matching
269 *
270 * @warning if searching is successful an implicit write lock
271 * to the search result is granted, therefore unlock the
272 * entry using @ref ixEthDBReleaseHashNode() as soon as possible.
273 *
274 * @see ixEthDBReleaseHashNode()
275 *
276 * @return the search result, or NULL if a record with the given
277 * MAC address/VLAN ID combination was not found
278 *
279 * @internal
280 */
281IX_ETH_DB_PUBLIC
282HashNode* ixEthDBVlanSearch(IxEthDBMacAddr *macAddress, IxEthDBVlanId vlanID, IxEthDBRecordType typeFilter)
283{
284 HashNode *searchResult = NULL;
285 MacDescriptor reference;
286
287 if (macAddress == NULL)
288 {
289 return NULL;
290 }
291
292 /* fill search fields */
293 memcpy(reference.macAddress, macAddress, sizeof (IxEthDBMacAddr));
294 reference.recordData.filteringVlanData.ieee802_1qTag =
295 IX_ETH_DB_SET_VLAN_ID(reference.recordData.filteringVlanData.ieee802_1qTag, vlanID);
296
297 /* set acceptable record types */
298 reference.type = typeFilter;
299
300 BUSY_RETRY(ixEthDBSearchHashEntry(&dbHashtable, IX_ETH_DB_MAC_VLAN_KEY, &reference, &searchResult));
301
302 return searchResult;
303}