lan966x_mac.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <net/switchdev.h>
  3. #include "lan966x_main.h"
  4. #define LAN966X_MAC_COLUMNS 4
  5. #define MACACCESS_CMD_IDLE 0
  6. #define MACACCESS_CMD_LEARN 1
  7. #define MACACCESS_CMD_FORGET 2
  8. #define MACACCESS_CMD_AGE 3
  9. #define MACACCESS_CMD_GET_NEXT 4
  10. #define MACACCESS_CMD_INIT 5
  11. #define MACACCESS_CMD_READ 6
  12. #define MACACCESS_CMD_WRITE 7
  13. #define MACACCESS_CMD_SYNC_GET_NEXT 8
  14. #define LAN966X_MAC_INVALID_ROW -1
  15. struct lan966x_mac_entry {
  16. struct list_head list;
  17. unsigned char mac[ETH_ALEN] __aligned(2);
  18. u16 vid;
  19. u16 port_index;
  20. int row;
  21. bool lag;
  22. };
  23. struct lan966x_mac_raw_entry {
  24. u32 mach;
  25. u32 macl;
  26. u32 maca;
  27. bool processed;
  28. };
  29. static int lan966x_mac_get_status(struct lan966x *lan966x)
  30. {
  31. return lan_rd(lan966x, ANA_MACACCESS);
  32. }
  33. static int lan966x_mac_wait_for_completion(struct lan966x *lan966x)
  34. {
  35. u32 val;
  36. return readx_poll_timeout_atomic(lan966x_mac_get_status,
  37. lan966x, val,
  38. (ANA_MACACCESS_MAC_TABLE_CMD_GET(val)) ==
  39. MACACCESS_CMD_IDLE,
  40. TABLE_UPDATE_SLEEP_US,
  41. TABLE_UPDATE_TIMEOUT_US);
  42. }
  43. static void lan966x_mac_select(struct lan966x *lan966x,
  44. const unsigned char mac[ETH_ALEN],
  45. unsigned int vid)
  46. {
  47. u32 macl = 0, mach = 0;
  48. /* Set the MAC address to handle and the vlan associated in a format
  49. * understood by the hardware.
  50. */
  51. mach |= vid << 16;
  52. mach |= mac[0] << 8;
  53. mach |= mac[1] << 0;
  54. macl |= mac[2] << 24;
  55. macl |= mac[3] << 16;
  56. macl |= mac[4] << 8;
  57. macl |= mac[5] << 0;
  58. lan_wr(macl, lan966x, ANA_MACLDATA);
  59. lan_wr(mach, lan966x, ANA_MACHDATA);
  60. }
  61. static int __lan966x_mac_learn_locked(struct lan966x *lan966x, int pgid,
  62. bool cpu_copy,
  63. const unsigned char mac[ETH_ALEN],
  64. unsigned int vid,
  65. enum macaccess_entry_type type)
  66. {
  67. lockdep_assert_held(&lan966x->mac_lock);
  68. lan966x_mac_select(lan966x, mac, vid);
  69. /* Issue a write command */
  70. lan_wr(ANA_MACACCESS_VALID_SET(1) |
  71. ANA_MACACCESS_CHANGE2SW_SET(0) |
  72. ANA_MACACCESS_MAC_CPU_COPY_SET(cpu_copy) |
  73. ANA_MACACCESS_DEST_IDX_SET(pgid) |
  74. ANA_MACACCESS_ENTRYTYPE_SET(type) |
  75. ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_LEARN),
  76. lan966x, ANA_MACACCESS);
  77. return lan966x_mac_wait_for_completion(lan966x);
  78. }
  79. static int __lan966x_mac_learn(struct lan966x *lan966x, int pgid,
  80. bool cpu_copy,
  81. const unsigned char mac[ETH_ALEN],
  82. unsigned int vid,
  83. enum macaccess_entry_type type)
  84. {
  85. int ret;
  86. spin_lock(&lan966x->mac_lock);
  87. ret = __lan966x_mac_learn_locked(lan966x, pgid, cpu_copy, mac, vid, type);
  88. spin_unlock(&lan966x->mac_lock);
  89. return ret;
  90. }
  91. /* The mask of the front ports is encoded inside the mac parameter via a call
  92. * to lan966x_mdb_encode_mac().
  93. */
  94. int lan966x_mac_ip_learn(struct lan966x *lan966x,
  95. bool cpu_copy,
  96. const unsigned char mac[ETH_ALEN],
  97. unsigned int vid,
  98. enum macaccess_entry_type type)
  99. {
  100. WARN_ON(type != ENTRYTYPE_MACV4 && type != ENTRYTYPE_MACV6);
  101. return __lan966x_mac_learn(lan966x, 0, cpu_copy, mac, vid, type);
  102. }
  103. int lan966x_mac_learn(struct lan966x *lan966x, int port,
  104. const unsigned char mac[ETH_ALEN],
  105. unsigned int vid,
  106. enum macaccess_entry_type type)
  107. {
  108. WARN_ON(type != ENTRYTYPE_NORMAL && type != ENTRYTYPE_LOCKED);
  109. return __lan966x_mac_learn(lan966x, port, false, mac, vid, type);
  110. }
  111. static int lan966x_mac_learn_locked(struct lan966x *lan966x, int port,
  112. const unsigned char mac[ETH_ALEN],
  113. unsigned int vid,
  114. enum macaccess_entry_type type)
  115. {
  116. WARN_ON(type != ENTRYTYPE_NORMAL && type != ENTRYTYPE_LOCKED);
  117. return __lan966x_mac_learn_locked(lan966x, port, false, mac, vid, type);
  118. }
  119. static int lan966x_mac_forget_locked(struct lan966x *lan966x,
  120. const unsigned char mac[ETH_ALEN],
  121. unsigned int vid,
  122. enum macaccess_entry_type type)
  123. {
  124. lockdep_assert_held(&lan966x->mac_lock);
  125. lan966x_mac_select(lan966x, mac, vid);
  126. /* Issue a forget command */
  127. lan_wr(ANA_MACACCESS_ENTRYTYPE_SET(type) |
  128. ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_FORGET),
  129. lan966x, ANA_MACACCESS);
  130. return lan966x_mac_wait_for_completion(lan966x);
  131. }
  132. int lan966x_mac_forget(struct lan966x *lan966x,
  133. const unsigned char mac[ETH_ALEN],
  134. unsigned int vid,
  135. enum macaccess_entry_type type)
  136. {
  137. int ret;
  138. spin_lock(&lan966x->mac_lock);
  139. ret = lan966x_mac_forget_locked(lan966x, mac, vid, type);
  140. spin_unlock(&lan966x->mac_lock);
  141. return ret;
  142. }
  143. int lan966x_mac_cpu_learn(struct lan966x *lan966x, const char *addr, u16 vid)
  144. {
  145. return lan966x_mac_learn(lan966x, PGID_CPU, addr, vid, ENTRYTYPE_LOCKED);
  146. }
  147. int lan966x_mac_cpu_forget(struct lan966x *lan966x, const char *addr, u16 vid)
  148. {
  149. return lan966x_mac_forget(lan966x, addr, vid, ENTRYTYPE_LOCKED);
  150. }
  151. void lan966x_mac_set_ageing(struct lan966x *lan966x,
  152. u32 ageing)
  153. {
  154. lan_rmw(ANA_AUTOAGE_AGE_PERIOD_SET(ageing / 2),
  155. ANA_AUTOAGE_AGE_PERIOD,
  156. lan966x, ANA_AUTOAGE);
  157. }
  158. void lan966x_mac_init(struct lan966x *lan966x)
  159. {
  160. /* Clear the MAC table */
  161. lan_wr(MACACCESS_CMD_INIT, lan966x, ANA_MACACCESS);
  162. lan966x_mac_wait_for_completion(lan966x);
  163. spin_lock_init(&lan966x->mac_lock);
  164. INIT_LIST_HEAD(&lan966x->mac_entries);
  165. }
  166. static struct lan966x_mac_entry *lan966x_mac_alloc_entry(struct lan966x_port *port,
  167. const unsigned char *mac,
  168. u16 vid)
  169. {
  170. struct lan966x_mac_entry *mac_entry;
  171. mac_entry = kzalloc_obj(*mac_entry, GFP_ATOMIC);
  172. if (!mac_entry)
  173. return NULL;
  174. memcpy(mac_entry->mac, mac, ETH_ALEN);
  175. mac_entry->vid = vid;
  176. mac_entry->port_index = port->chip_port;
  177. mac_entry->row = LAN966X_MAC_INVALID_ROW;
  178. mac_entry->lag = port->bond ? true : false;
  179. return mac_entry;
  180. }
  181. static struct lan966x_mac_entry *lan966x_mac_find_entry(struct lan966x *lan966x,
  182. const unsigned char *mac,
  183. u16 vid, u16 port_index)
  184. {
  185. struct lan966x_mac_entry *res = NULL;
  186. struct lan966x_mac_entry *mac_entry;
  187. list_for_each_entry(mac_entry, &lan966x->mac_entries, list) {
  188. if (mac_entry->vid == vid &&
  189. ether_addr_equal(mac, mac_entry->mac) &&
  190. mac_entry->port_index == port_index) {
  191. res = mac_entry;
  192. break;
  193. }
  194. }
  195. return res;
  196. }
  197. static int lan966x_mac_lookup(struct lan966x *lan966x,
  198. const unsigned char mac[ETH_ALEN],
  199. unsigned int vid, enum macaccess_entry_type type)
  200. {
  201. int ret;
  202. lan966x_mac_select(lan966x, mac, vid);
  203. /* Issue a read command */
  204. lan_wr(ANA_MACACCESS_ENTRYTYPE_SET(type) |
  205. ANA_MACACCESS_VALID_SET(1) |
  206. ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_READ),
  207. lan966x, ANA_MACACCESS);
  208. ret = lan966x_mac_wait_for_completion(lan966x);
  209. if (ret)
  210. return ret;
  211. return ANA_MACACCESS_VALID_GET(lan_rd(lan966x, ANA_MACACCESS));
  212. }
  213. static void lan966x_fdb_call_notifiers(enum switchdev_notifier_type type,
  214. const char *mac, u16 vid,
  215. struct net_device *dev)
  216. {
  217. struct switchdev_notifier_fdb_info info = { 0 };
  218. info.addr = mac;
  219. info.vid = vid;
  220. info.offloaded = true;
  221. call_switchdev_notifiers(type, dev, &info.info, NULL);
  222. }
  223. int lan966x_mac_add_entry(struct lan966x *lan966x, struct lan966x_port *port,
  224. const unsigned char *addr, u16 vid)
  225. {
  226. struct lan966x_mac_entry *mac_entry;
  227. spin_lock(&lan966x->mac_lock);
  228. if (lan966x_mac_lookup(lan966x, addr, vid, ENTRYTYPE_NORMAL)) {
  229. spin_unlock(&lan966x->mac_lock);
  230. return 0;
  231. }
  232. /* In case the entry already exists, don't add it again to SW,
  233. * just update HW, but we need to look in the actual HW because
  234. * it is possible for an entry to be learn by HW and before we
  235. * get the interrupt the frame will reach CPU and the CPU will
  236. * add the entry but without the extern_learn flag.
  237. */
  238. mac_entry = lan966x_mac_find_entry(lan966x, addr, vid, port->chip_port);
  239. if (mac_entry) {
  240. spin_unlock(&lan966x->mac_lock);
  241. goto mac_learn;
  242. }
  243. mac_entry = lan966x_mac_alloc_entry(port, addr, vid);
  244. if (!mac_entry) {
  245. spin_unlock(&lan966x->mac_lock);
  246. return -ENOMEM;
  247. }
  248. list_add_tail(&mac_entry->list, &lan966x->mac_entries);
  249. spin_unlock(&lan966x->mac_lock);
  250. lan966x_fdb_call_notifiers(SWITCHDEV_FDB_OFFLOADED, addr, vid,
  251. port->bond ?: port->dev);
  252. mac_learn:
  253. lan966x_mac_learn(lan966x, port->chip_port, addr, vid, ENTRYTYPE_LOCKED);
  254. return 0;
  255. }
  256. int lan966x_mac_del_entry(struct lan966x *lan966x, const unsigned char *addr,
  257. u16 vid)
  258. {
  259. struct lan966x_mac_entry *mac_entry, *tmp;
  260. spin_lock(&lan966x->mac_lock);
  261. list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries,
  262. list) {
  263. if (mac_entry->vid == vid &&
  264. ether_addr_equal(addr, mac_entry->mac)) {
  265. lan966x_mac_forget_locked(lan966x, mac_entry->mac,
  266. mac_entry->vid,
  267. ENTRYTYPE_LOCKED);
  268. list_del(&mac_entry->list);
  269. kfree(mac_entry);
  270. }
  271. }
  272. spin_unlock(&lan966x->mac_lock);
  273. return 0;
  274. }
  275. void lan966x_mac_lag_replace_port_entry(struct lan966x *lan966x,
  276. struct lan966x_port *src,
  277. struct lan966x_port *dst)
  278. {
  279. struct lan966x_mac_entry *mac_entry;
  280. spin_lock(&lan966x->mac_lock);
  281. list_for_each_entry(mac_entry, &lan966x->mac_entries, list) {
  282. if (mac_entry->port_index == src->chip_port &&
  283. mac_entry->lag) {
  284. lan966x_mac_forget_locked(lan966x, mac_entry->mac,
  285. mac_entry->vid,
  286. ENTRYTYPE_LOCKED);
  287. lan966x_mac_learn_locked(lan966x, dst->chip_port,
  288. mac_entry->mac, mac_entry->vid,
  289. ENTRYTYPE_LOCKED);
  290. mac_entry->port_index = dst->chip_port;
  291. }
  292. }
  293. spin_unlock(&lan966x->mac_lock);
  294. }
  295. void lan966x_mac_lag_remove_port_entry(struct lan966x *lan966x,
  296. struct lan966x_port *src)
  297. {
  298. struct lan966x_mac_entry *mac_entry, *tmp;
  299. spin_lock(&lan966x->mac_lock);
  300. list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries,
  301. list) {
  302. if (mac_entry->port_index == src->chip_port &&
  303. mac_entry->lag) {
  304. lan966x_mac_forget_locked(lan966x, mac_entry->mac,
  305. mac_entry->vid,
  306. ENTRYTYPE_LOCKED);
  307. list_del(&mac_entry->list);
  308. kfree(mac_entry);
  309. }
  310. }
  311. spin_unlock(&lan966x->mac_lock);
  312. }
  313. void lan966x_mac_purge_entries(struct lan966x *lan966x)
  314. {
  315. struct lan966x_mac_entry *mac_entry, *tmp;
  316. spin_lock(&lan966x->mac_lock);
  317. list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries,
  318. list) {
  319. lan966x_mac_forget_locked(lan966x, mac_entry->mac,
  320. mac_entry->vid, ENTRYTYPE_LOCKED);
  321. list_del(&mac_entry->list);
  322. kfree(mac_entry);
  323. }
  324. spin_unlock(&lan966x->mac_lock);
  325. }
  326. static void lan966x_mac_notifiers(enum switchdev_notifier_type type,
  327. unsigned char *mac, u32 vid,
  328. struct net_device *dev)
  329. {
  330. rtnl_lock();
  331. lan966x_fdb_call_notifiers(type, mac, vid, dev);
  332. rtnl_unlock();
  333. }
  334. static void lan966x_mac_process_raw_entry(struct lan966x_mac_raw_entry *raw_entry,
  335. u8 *mac, u16 *vid, u32 *dest_idx)
  336. {
  337. mac[0] = (raw_entry->mach >> 8) & 0xff;
  338. mac[1] = (raw_entry->mach >> 0) & 0xff;
  339. mac[2] = (raw_entry->macl >> 24) & 0xff;
  340. mac[3] = (raw_entry->macl >> 16) & 0xff;
  341. mac[4] = (raw_entry->macl >> 8) & 0xff;
  342. mac[5] = (raw_entry->macl >> 0) & 0xff;
  343. *vid = (raw_entry->mach >> 16) & 0xfff;
  344. *dest_idx = ANA_MACACCESS_DEST_IDX_GET(raw_entry->maca);
  345. }
  346. static void lan966x_mac_irq_process(struct lan966x *lan966x, u32 row,
  347. struct lan966x_mac_raw_entry *raw_entries)
  348. {
  349. struct lan966x_mac_entry *mac_entry, *tmp;
  350. unsigned char mac[ETH_ALEN] __aligned(2);
  351. struct list_head mac_deleted_entries;
  352. struct lan966x_port *port;
  353. u32 dest_idx;
  354. u32 column;
  355. u16 vid;
  356. INIT_LIST_HEAD(&mac_deleted_entries);
  357. spin_lock(&lan966x->mac_lock);
  358. list_for_each_entry_safe(mac_entry, tmp, &lan966x->mac_entries, list) {
  359. bool found = false;
  360. if (mac_entry->row != row)
  361. continue;
  362. for (column = 0; column < LAN966X_MAC_COLUMNS; ++column) {
  363. /* All the valid entries are at the start of the row,
  364. * so when get one invalid entry it can just skip the
  365. * rest of the columns
  366. */
  367. if (!ANA_MACACCESS_VALID_GET(raw_entries[column].maca))
  368. break;
  369. lan966x_mac_process_raw_entry(&raw_entries[column],
  370. mac, &vid, &dest_idx);
  371. if (WARN_ON(dest_idx >= lan966x->num_phys_ports))
  372. continue;
  373. /* If the entry in SW is found, then there is nothing
  374. * to do
  375. */
  376. if (mac_entry->vid == vid &&
  377. ether_addr_equal(mac_entry->mac, mac) &&
  378. mac_entry->port_index == dest_idx) {
  379. raw_entries[column].processed = true;
  380. found = true;
  381. break;
  382. }
  383. }
  384. if (!found) {
  385. list_del(&mac_entry->list);
  386. /* Move the entry from SW list to a tmp list such that
  387. * it would be deleted later
  388. */
  389. list_add_tail(&mac_entry->list, &mac_deleted_entries);
  390. }
  391. }
  392. spin_unlock(&lan966x->mac_lock);
  393. list_for_each_entry_safe(mac_entry, tmp, &mac_deleted_entries, list) {
  394. /* Notify the bridge that the entry doesn't exist
  395. * anymore in the HW
  396. */
  397. port = lan966x->ports[mac_entry->port_index];
  398. lan966x_mac_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
  399. mac_entry->mac, mac_entry->vid,
  400. port->bond ?: port->dev);
  401. list_del(&mac_entry->list);
  402. kfree(mac_entry);
  403. }
  404. /* Now go to the list of columns and see if any entry was not in the SW
  405. * list, then that means that the entry is new so it needs to notify the
  406. * bridge.
  407. */
  408. for (column = 0; column < LAN966X_MAC_COLUMNS; ++column) {
  409. /* All the valid entries are at the start of the row, so when
  410. * get one invalid entry it can just skip the rest of the columns
  411. */
  412. if (!ANA_MACACCESS_VALID_GET(raw_entries[column].maca))
  413. break;
  414. /* If the entry already exists then don't do anything */
  415. if (raw_entries[column].processed)
  416. continue;
  417. lan966x_mac_process_raw_entry(&raw_entries[column],
  418. mac, &vid, &dest_idx);
  419. if (WARN_ON(dest_idx >= lan966x->num_phys_ports))
  420. continue;
  421. spin_lock(&lan966x->mac_lock);
  422. mac_entry = lan966x_mac_find_entry(lan966x, mac, vid, dest_idx);
  423. if (mac_entry) {
  424. spin_unlock(&lan966x->mac_lock);
  425. continue;
  426. }
  427. port = lan966x->ports[dest_idx];
  428. mac_entry = lan966x_mac_alloc_entry(port, mac, vid);
  429. if (!mac_entry) {
  430. spin_unlock(&lan966x->mac_lock);
  431. return;
  432. }
  433. mac_entry->row = row;
  434. list_add_tail(&mac_entry->list, &lan966x->mac_entries);
  435. spin_unlock(&lan966x->mac_lock);
  436. lan966x_mac_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
  437. mac, vid, port->bond ?: port->dev);
  438. }
  439. }
  440. irqreturn_t lan966x_mac_irq_handler(struct lan966x *lan966x)
  441. {
  442. struct lan966x_mac_raw_entry entry[LAN966X_MAC_COLUMNS] = { 0 };
  443. u32 index, column;
  444. bool stop = true;
  445. u32 val;
  446. /* Start the scan from 0, 0 */
  447. lan_wr(ANA_MACTINDX_M_INDEX_SET(0) |
  448. ANA_MACTINDX_BUCKET_SET(0),
  449. lan966x, ANA_MACTINDX);
  450. while (1) {
  451. spin_lock(&lan966x->mac_lock);
  452. lan_rmw(ANA_MACACCESS_MAC_TABLE_CMD_SET(MACACCESS_CMD_SYNC_GET_NEXT),
  453. ANA_MACACCESS_MAC_TABLE_CMD,
  454. lan966x, ANA_MACACCESS);
  455. lan966x_mac_wait_for_completion(lan966x);
  456. val = lan_rd(lan966x, ANA_MACTINDX);
  457. index = ANA_MACTINDX_M_INDEX_GET(val);
  458. column = ANA_MACTINDX_BUCKET_GET(val);
  459. /* The SYNC-GET-NEXT returns all the entries(4) in a row in
  460. * which is suffered a change. By change it means that new entry
  461. * was added or an entry was removed because of ageing.
  462. * It would return all the columns for that row. And after that
  463. * it would return the next row The stop conditions of the
  464. * SYNC-GET-NEXT is when it reaches 'directly' to row 0
  465. * column 3. So if SYNC-GET-NEXT returns row 0 and column 0
  466. * then it is required to continue to read more even if it
  467. * reaches row 0 and column 3.
  468. */
  469. if (index == 0 && column == 0)
  470. stop = false;
  471. if (column == LAN966X_MAC_COLUMNS - 1 &&
  472. index == 0 && stop) {
  473. spin_unlock(&lan966x->mac_lock);
  474. break;
  475. }
  476. entry[column].mach = lan_rd(lan966x, ANA_MACHDATA);
  477. entry[column].macl = lan_rd(lan966x, ANA_MACLDATA);
  478. entry[column].maca = lan_rd(lan966x, ANA_MACACCESS);
  479. spin_unlock(&lan966x->mac_lock);
  480. /* Once all the columns are read process them */
  481. if (column == LAN966X_MAC_COLUMNS - 1) {
  482. lan966x_mac_irq_process(lan966x, index, entry);
  483. /* A row was processed so it is safe to assume that the
  484. * next row/column can be the stop condition
  485. */
  486. stop = true;
  487. }
  488. }
  489. lan_rmw(ANA_ANAINTR_INTR_SET(0),
  490. ANA_ANAINTR_INTR,
  491. lan966x, ANA_ANAINTR);
  492. return IRQ_HANDLED;
  493. }