1 // SPDX-License-Identifier: GPL-2.0+
3 #include <net/switchdev.h>
4 #include "lan966x_main.h"
6 #define LAN966X_MAC_COLUMNS 4
7 #define MACACCESS_CMD_IDLE 0
8 #define MACACCESS_CMD_LEARN 1
9 #define MACACCESS_CMD_FORGET 2
10 #define MACACCESS_CMD_AGE 3
11 #define MACACCESS_CMD_GET_NEXT 4
12 #define MACACCESS_CMD_INIT 5
13 #define MACACCESS_CMD_READ 6
14 #define MACACCESS_CMD_WRITE 7
15 #define MACACCESS_CMD_SYNC_GET_NEXT 8
17 #define LAN966X_MAC_INVALID_ROW -1
19 struct lan966x_mac_entry {
20 struct list_head list;
21 unsigned char mac[ETH_ALEN] __aligned(2);
27 struct lan966x_mac_raw_entry {
34 static int lan966x_mac_get_status(struct lan966x *lan966x)
36 return lan_rd(lan966x, ANA_MACACCESS);
39 static int lan966x_mac_wait_for_completion(struct lan966x *lan966x)
43 return readx_poll_timeout_atomic(lan966x_mac_get_status,
45 (ANA_MACACCESS_MAC_TABLE_CMD_GET(val)) ==
47 TABLE_UPDATE_SLEEP_US,
48 TABLE_UPDATE_TIMEOUT_US);
51 static void lan966x_mac_select(struct lan966x *lan966x,
52 const unsigned char mac[ETH_ALEN],
55 u32 macl = 0, mach = 0;
57 /* Set the MAC address to handle and the vlan associated in a format
58 * understood by the hardware.
68 lan_wr(macl, lan966x, ANA_MACLDATA);
69 lan_wr(mach, lan966x, ANA_MACHDATA);
72 static int __lan966x_mac_learn(struct lan966x *lan966x, int pgid,
74 const unsigned char mac[ETH_ALEN],
76 enum macaccess_entry_type type)
78 lan966x_mac_select(lan966x, mac, vid);
80 /* Issue a write command */
81 lan_wr(ANA_MACACCESS_VALID_SET(1) |
82 ANA_MACACCESS_CHANGE2SW_SET(0) |
83 ANA_MACACCESS_MAC_CPU_COPY_SET(cpu_copy) |
84 ANA_MACACCESS_DEST_IDX_SET(pgid) |
85 ANA_MACACCESS_ENTRYTYPE_SET(type) |
86 ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_LEARN),
87 lan966x, ANA_MACACCESS);
89 return lan966x_mac_wait_for_completion(lan966x);
92 /* The mask of the front ports is encoded inside the mac parameter via a call
93 * to lan966x_mdb_encode_mac().
95 int lan966x_mac_ip_learn(struct lan966x *lan966x,
97 const unsigned char mac[ETH_ALEN],
99 enum macaccess_entry_type type)
101 WARN_ON(type != ENTRYTYPE_MACV4 && type != ENTRYTYPE_MACV6);
103 return __lan966x_mac_learn(lan966x, 0, cpu_copy, mac, vid, type);
106 int lan966x_mac_learn(struct lan966x *lan966x, int port,
107 const unsigned char mac[ETH_ALEN],
109 enum macaccess_entry_type type)
111 WARN_ON(type != ENTRYTYPE_NORMAL && type != ENTRYTYPE_LOCKED);
113 return __lan966x_mac_learn(lan966x, port, false, mac, vid, type);
116 int lan966x_mac_forget(struct lan966x *lan966x,
117 const unsigned char mac[ETH_ALEN],
119 enum macaccess_entry_type type)
121 lan966x_mac_select(lan966x, mac, vid);
123 /* Issue a forget command */
124 lan_wr(ANA_MACACCESS_ENTRYTYPE_SET(type) |
125 ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_FORGET),
126 lan966x, ANA_MACACCESS);
128 return lan966x_mac_wait_for_completion(lan966x);
131 int lan966x_mac_cpu_learn(struct lan966x *lan966x, const char *addr, u16 vid)
133 return lan966x_mac_learn(lan966x, PGID_CPU, addr, vid, ENTRYTYPE_LOCKED);
136 int lan966x_mac_cpu_forget(struct lan966x *lan966x, const char *addr, u16 vid)
138 return lan966x_mac_forget(lan966x, addr, vid, ENTRYTYPE_LOCKED);
141 void lan966x_mac_set_ageing(struct lan966x *lan966x,
144 lan_rmw(ANA_AUTOAGE_AGE_PERIOD_SET(ageing / 2),
145 ANA_AUTOAGE_AGE_PERIOD,
146 lan966x, ANA_AUTOAGE);
149 void lan966x_mac_init(struct lan966x *lan966x)
151 /* Clear the MAC table */
152 lan_wr(MACACCESS_CMD_INIT, lan966x, ANA_MACACCESS);
153 lan966x_mac_wait_for_completion(lan966x);
155 spin_lock_init(&lan966x->mac_lock);
156 INIT_LIST_HEAD(&lan966x->mac_entries);
159 static struct lan966x_mac_entry *lan966x_mac_alloc_entry(const unsigned char *mac,
160 u16 vid, u16 port_index)
162 struct lan966x_mac_entry *mac_entry;
164 mac_entry = kzalloc(sizeof(*mac_entry), GFP_KERNEL);
168 memcpy(mac_entry->mac, mac, ETH_ALEN);
169 mac_entry->vid = vid;
170 mac_entry->port_index = port_index;
171 mac_entry->row = LAN966X_MAC_INVALID_ROW;
175 static struct lan966x_mac_entry *lan966x_mac_find_entry(struct lan966x *lan966x,
176 const unsigned char *mac,
177 u16 vid, u16 port_index)
179 struct lan966x_mac_entry *res = NULL;
180 struct lan966x_mac_entry *mac_entry;
182 spin_lock(&lan966x->mac_lock);
183 list_for_each_entry(mac_entry, &lan966x->mac_entries, list) {
184 if (mac_entry->vid == vid &&
185 ether_addr_equal(mac, mac_entry->mac) &&
186 mac_entry->port_index == port_index) {
191 spin_unlock(&lan966x->mac_lock);
196 static int lan966x_mac_lookup(struct lan966x *lan966x,
197 const unsigned char mac[ETH_ALEN],
198 unsigned int vid, enum macaccess_entry_type type)
202 lan966x_mac_select(lan966x, mac, vid);
204 /* Issue a read command */
205 lan_wr(ANA_MACACCESS_ENTRYTYPE_SET(type) |
206 ANA_MACACCESS_VALID_SET(1) |
207 ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_READ),
208 lan966x, ANA_MACACCESS);
210 ret = lan966x_mac_wait_for_completion(lan966x);
214 return ANA_MACACCESS_VALID_GET(lan_rd(lan966x, ANA_MACACCESS));
217 static void lan966x_fdb_call_notifiers(enum switchdev_notifier_type type,
218 const char *mac, u16 vid,
219 struct net_device *dev)
221 struct switchdev_notifier_fdb_info info = { 0 };
225 info.offloaded = true;
226 call_switchdev_notifiers(type, dev, &info.info, NULL);
229 int lan966x_mac_add_entry(struct lan966x *lan966x, struct lan966x_port *port,
230 const unsigned char *addr, u16 vid)
232 struct lan966x_mac_entry *mac_entry;
234 if (lan966x_mac_lookup(lan966x, addr, vid, ENTRYTYPE_NORMAL))
237 /* In case the entry already exists, don't add it again to SW,
238 * just update HW, but we need to look in the actual HW because
239 * it is possible for an entry to be learn by HW and before we
240 * get the interrupt the frame will reach CPU and the CPU will
241 * add the entry but without the extern_learn flag.
243 mac_entry = lan966x_mac_find_entry(lan966x, addr, vid, port->chip_port);
245 return lan966x_mac_learn(lan966x, port->chip_port,
246 addr, vid, ENTRYTYPE_LOCKED);
248 mac_entry = lan966x_mac_alloc_entry(addr, vid, port->chip_port);
252 spin_lock(&lan966x->mac_lock);
253 list_add_tail(&mac_entry->list, &lan966x->mac_entries);
254 spin_unlock(&lan966x->mac_lock);
256 lan966x_mac_learn(lan966x, port->chip_port, addr, vid, ENTRYTYPE_LOCKED);
257 lan966x_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, addr, vid, port->dev);
262 int lan966x_mac_del_entry(struct lan966x *lan966x, const unsigned char *addr,
265 struct lan966x_mac_entry *mac_entry, *tmp;
267 spin_lock(&lan966x->mac_lock);
268 list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries,
270 if (mac_entry->vid == vid &&
271 ether_addr_equal(addr, mac_entry->mac)) {
272 lan966x_mac_forget(lan966x, mac_entry->mac, mac_entry->vid,
275 list_del(&mac_entry->list);
279 spin_unlock(&lan966x->mac_lock);
284 void lan966x_mac_purge_entries(struct lan966x *lan966x)
286 struct lan966x_mac_entry *mac_entry, *tmp;
288 spin_lock(&lan966x->mac_lock);
289 list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries,
291 lan966x_mac_forget(lan966x, mac_entry->mac, mac_entry->vid,
294 list_del(&mac_entry->list);
297 spin_unlock(&lan966x->mac_lock);
300 static void lan966x_mac_notifiers(enum switchdev_notifier_type type,
301 unsigned char *mac, u32 vid,
302 struct net_device *dev)
305 lan966x_fdb_call_notifiers(type, mac, vid, dev);
309 static void lan966x_mac_process_raw_entry(struct lan966x_mac_raw_entry *raw_entry,
310 u8 *mac, u16 *vid, u32 *dest_idx)
312 mac[0] = (raw_entry->mach >> 8) & 0xff;
313 mac[1] = (raw_entry->mach >> 0) & 0xff;
314 mac[2] = (raw_entry->macl >> 24) & 0xff;
315 mac[3] = (raw_entry->macl >> 16) & 0xff;
316 mac[4] = (raw_entry->macl >> 8) & 0xff;
317 mac[5] = (raw_entry->macl >> 0) & 0xff;
319 *vid = (raw_entry->mach >> 16) & 0xfff;
320 *dest_idx = ANA_MACACCESS_DEST_IDX_GET(raw_entry->maca);
323 static void lan966x_mac_irq_process(struct lan966x *lan966x, u32 row,
324 struct lan966x_mac_raw_entry *raw_entries)
326 struct lan966x_mac_entry *mac_entry, *tmp;
327 unsigned char mac[ETH_ALEN] __aligned(2);
332 spin_lock(&lan966x->mac_lock);
333 list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries, list) {
336 if (mac_entry->row != row)
339 for (column = 0; column < LAN966X_MAC_COLUMNS; ++column) {
340 /* All the valid entries are at the start of the row,
341 * so when get one invalid entry it can just skip the
342 * rest of the columns
344 if (!ANA_MACACCESS_VALID_GET(raw_entries[column].maca))
347 lan966x_mac_process_raw_entry(&raw_entries[column],
348 mac, &vid, &dest_idx);
349 if (WARN_ON(dest_idx >= lan966x->num_phys_ports))
352 /* If the entry in SW is found, then there is nothing
355 if (mac_entry->vid == vid &&
356 ether_addr_equal(mac_entry->mac, mac) &&
357 mac_entry->port_index == dest_idx) {
358 raw_entries[column].processed = true;
365 /* Notify the bridge that the entry doesn't exist
366 * anymore in the HW and remove the entry from the SW
369 lan966x_mac_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
370 mac_entry->mac, mac_entry->vid,
371 lan966x->ports[mac_entry->port_index]->dev);
373 list_del(&mac_entry->list);
377 spin_unlock(&lan966x->mac_lock);
379 /* Now go to the list of columns and see if any entry was not in the SW
380 * list, then that means that the entry is new so it needs to notify the
383 for (column = 0; column < LAN966X_MAC_COLUMNS; ++column) {
384 /* All the valid entries are at the start of the row, so when
385 * get one invalid entry it can just skip the rest of the columns
387 if (!ANA_MACACCESS_VALID_GET(raw_entries[column].maca))
390 /* If the entry already exists then don't do anything */
391 if (raw_entries[column].processed)
394 lan966x_mac_process_raw_entry(&raw_entries[column],
395 mac, &vid, &dest_idx);
396 if (WARN_ON(dest_idx >= lan966x->num_phys_ports))
399 mac_entry = lan966x_mac_alloc_entry(mac, vid, dest_idx);
403 mac_entry->row = row;
405 spin_lock(&lan966x->mac_lock);
406 list_add_tail(&mac_entry->list, &lan966x->mac_entries);
407 spin_unlock(&lan966x->mac_lock);
409 lan966x_mac_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
410 mac, vid, lan966x->ports[dest_idx]->dev);
414 irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x)
416 struct lan966x_mac_raw_entry entry[LAN966X_MAC_COLUMNS] = { 0 };
421 /* Start the scan from 0, 0 */
422 lan_wr(ANA_MACTINDX_M_INDEX_SET(0) |
423 ANA_MACTINDX_BUCKET_SET(0),
424 lan966x, ANA_MACTINDX);
427 lan_rmw(ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_SYNC_GET_NEXT),
428 ANA_MACACCESS_MAC_TABLE_CMD,
429 lan966x, ANA_MACACCESS);
430 lan966x_mac_wait_for_completion(lan966x);
432 val = lan_rd(lan966x, ANA_MACTINDX);
433 index = ANA_MACTINDX_M_INDEX_GET(val);
434 column = ANA_MACTINDX_BUCKET_GET(val);
436 /* The SYNC-GET-NEXT returns all the entries(4) in a row in
437 * which is suffered a change. By change it means that new entry
438 * was added or an entry was removed because of ageing.
439 * It would return all the columns for that row. And after that
440 * it would return the next row The stop conditions of the
441 * SYNC-GET-NEXT is when it reaches 'directly' to row 0
442 * column 3. So if SYNC-GET-NEXT returns row 0 and column 0
443 * then it is required to continue to read more even if it
444 * reaches row 0 and column 3.
446 if (index == 0 && column == 0)
449 if (column == LAN966X_MAC_COLUMNS - 1 &&
453 entry[column].mach = lan_rd(lan966x, ANA_MACHDATA);
454 entry[column].macl = lan_rd(lan966x, ANA_MACLDATA);
455 entry[column].maca = lan_rd(lan966x, ANA_MACACCESS);
457 /* Once all the columns are read process them */
458 if (column == LAN966X_MAC_COLUMNS - 1) {
459 lan966x_mac_irq_process(lan966x, index, entry);
460 /* A row was processed so it is safe to assume that the
461 * next row/column can be the stop condition
467 lan_rmw(ANA_ANAINTR_INTR_SET(0),
469 lan966x, ANA_ANAINTR);