net: sparx5: Refactor mdb handling according to feedback
[linux-block.git] / drivers / net / ethernet / microchip / sparx5 / sparx5_pgid.c
CommitLineData
af9b45d0
CA
1// SPDX-License-Identifier: GPL-2.0+
2#include "sparx5_main.h"
3
4void sparx5_pgid_init(struct sparx5 *spx5)
5{
6 int i;
7
8 for (i = 0; i < PGID_TABLE_SIZE; i++)
9 spx5->pgid_map[i] = SPX5_PGID_FREE;
10
11 /* Reserved for unicast, flood control, broadcast, and CPU.
12 * These cannot be freed.
13 */
14 for (i = 0; i <= PGID_CPU; i++)
15 spx5->pgid_map[i] = SPX5_PGID_RESERVED;
16}
17
af9b45d0
CA
18int sparx5_pgid_alloc_mcast(struct sparx5 *spx5, u16 *idx)
19{
20 int i;
21
ad238fc6
CA
22 /* The multicast area starts at index 65, but the first 7
23 * are reserved for flood masks and CPU. Start alloc after that.
24 */
af9b45d0 25 for (i = PGID_MCAST_START; i < PGID_TABLE_SIZE; i++) {
af9b45d0
CA
26 if (spx5->pgid_map[i] == SPX5_PGID_FREE) {
27 spx5->pgid_map[i] = SPX5_PGID_MULTICAST;
28 *idx = i;
29 return 0;
30 }
31 }
32
33 return -EBUSY;
34}
35
36int sparx5_pgid_free(struct sparx5 *spx5, u16 idx)
37{
38 if (idx <= PGID_CPU || idx >= PGID_TABLE_SIZE)
39 return -EINVAL;
40
41 if (spx5->pgid_map[idx] == SPX5_PGID_FREE)
42 return -EINVAL;
43
44 spx5->pgid_map[idx] = SPX5_PGID_FREE;
45 return 0;
46}