cpsw_ale.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * Texas Instruments 3-Port Ethernet Switch Address Lookup Engine
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/slab.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <linux/stat.h>
  22. #include <linux/sysfs.h>
  23. #include <linux/etherdevice.h>
  24. #include "cpsw_ale.h"
  25. #define BITMASK(bits) (BIT(bits) - 1)
  26. #define ALE_ENTRY_BITS 68
  27. #define ALE_ENTRY_WORDS DIV_ROUND_UP(ALE_ENTRY_BITS, 32)
  28. #define ALE_VERSION_MAJOR(rev) ((rev >> 8) & 0xff)
  29. #define ALE_VERSION_MINOR(rev) (rev & 0xff)
  30. /* ALE Registers */
  31. #define ALE_IDVER 0x00
  32. #define ALE_CONTROL 0x08
  33. #define ALE_PRESCALE 0x10
  34. #define ALE_UNKNOWNVLAN 0x18
  35. #define ALE_TABLE_CONTROL 0x20
  36. #define ALE_TABLE 0x34
  37. #define ALE_PORTCTL 0x40
  38. #define ALE_TABLE_WRITE BIT(31)
  39. #define ALE_TYPE_FREE 0
  40. #define ALE_TYPE_ADDR 1
  41. #define ALE_TYPE_VLAN 2
  42. #define ALE_TYPE_VLAN_ADDR 3
  43. #define ALE_UCAST_PERSISTANT 0
  44. #define ALE_UCAST_UNTOUCHED 1
  45. #define ALE_UCAST_OUI 2
  46. #define ALE_UCAST_TOUCHED 3
  47. static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
  48. {
  49. int idx;
  50. idx = start / 32;
  51. start -= idx * 32;
  52. idx = 2 - idx; /* flip */
  53. return (ale_entry[idx] >> start) & BITMASK(bits);
  54. }
  55. static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
  56. u32 value)
  57. {
  58. int idx;
  59. value &= BITMASK(bits);
  60. idx = start / 32;
  61. start -= idx * 32;
  62. idx = 2 - idx; /* flip */
  63. ale_entry[idx] &= ~(BITMASK(bits) << start);
  64. ale_entry[idx] |= (value << start);
  65. }
  66. #define DEFINE_ALE_FIELD(name, start, bits) \
  67. static inline int cpsw_ale_get_##name(u32 *ale_entry) \
  68. { \
  69. return cpsw_ale_get_field(ale_entry, start, bits); \
  70. } \
  71. static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
  72. { \
  73. cpsw_ale_set_field(ale_entry, start, bits, value); \
  74. }
  75. DEFINE_ALE_FIELD(entry_type, 60, 2)
  76. DEFINE_ALE_FIELD(vlan_id, 48, 12)
  77. DEFINE_ALE_FIELD(mcast_state, 62, 2)
  78. DEFINE_ALE_FIELD(port_mask, 66, 3)
  79. DEFINE_ALE_FIELD(super, 65, 1)
  80. DEFINE_ALE_FIELD(ucast_type, 62, 2)
  81. DEFINE_ALE_FIELD(port_num, 66, 2)
  82. DEFINE_ALE_FIELD(blocked, 65, 1)
  83. DEFINE_ALE_FIELD(secure, 64, 1)
  84. DEFINE_ALE_FIELD(vlan_untag_force, 24, 3)
  85. DEFINE_ALE_FIELD(vlan_reg_mcast, 16, 3)
  86. DEFINE_ALE_FIELD(vlan_unreg_mcast, 8, 3)
  87. DEFINE_ALE_FIELD(vlan_member_list, 0, 3)
  88. DEFINE_ALE_FIELD(mcast, 40, 1)
  89. /* The MAC address field in the ALE entry cannot be macroized as above */
  90. static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
  91. {
  92. int i;
  93. for (i = 0; i < 6; i++)
  94. addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
  95. }
  96. static inline void cpsw_ale_set_addr(u32 *ale_entry, u8 *addr)
  97. {
  98. int i;
  99. for (i = 0; i < 6; i++)
  100. cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
  101. }
  102. static int cpsw_ale_read(struct cpsw_ale *ale, int idx, u32 *ale_entry)
  103. {
  104. int i;
  105. WARN_ON(idx > ale->params.ale_entries);
  106. __raw_writel(idx, ale->params.ale_regs + ALE_TABLE_CONTROL);
  107. for (i = 0; i < ALE_ENTRY_WORDS; i++)
  108. ale_entry[i] = __raw_readl(ale->params.ale_regs +
  109. ALE_TABLE + 4 * i);
  110. return idx;
  111. }
  112. static int cpsw_ale_write(struct cpsw_ale *ale, int idx, u32 *ale_entry)
  113. {
  114. int i;
  115. WARN_ON(idx > ale->params.ale_entries);
  116. for (i = 0; i < ALE_ENTRY_WORDS; i++)
  117. __raw_writel(ale_entry[i], ale->params.ale_regs +
  118. ALE_TABLE + 4 * i);
  119. __raw_writel(idx | ALE_TABLE_WRITE, ale->params.ale_regs +
  120. ALE_TABLE_CONTROL);
  121. return idx;
  122. }
  123. int cpsw_ale_match_addr(struct cpsw_ale *ale, u8 *addr, u16 vid)
  124. {
  125. u32 ale_entry[ALE_ENTRY_WORDS];
  126. int type, idx;
  127. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  128. u8 entry_addr[6];
  129. cpsw_ale_read(ale, idx, ale_entry);
  130. type = cpsw_ale_get_entry_type(ale_entry);
  131. if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
  132. continue;
  133. if (cpsw_ale_get_vlan_id(ale_entry) != vid)
  134. continue;
  135. cpsw_ale_get_addr(ale_entry, entry_addr);
  136. if (memcmp(entry_addr, addr, 6) == 0)
  137. return idx;
  138. }
  139. return -ENOENT;
  140. }
  141. int cpsw_ale_match_vlan(struct cpsw_ale *ale, u16 vid)
  142. {
  143. u32 ale_entry[ALE_ENTRY_WORDS];
  144. int type, idx;
  145. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  146. cpsw_ale_read(ale, idx, ale_entry);
  147. type = cpsw_ale_get_entry_type(ale_entry);
  148. if (type != ALE_TYPE_VLAN)
  149. continue;
  150. if (cpsw_ale_get_vlan_id(ale_entry) == vid)
  151. return idx;
  152. }
  153. return -ENOENT;
  154. }
  155. static int cpsw_ale_match_free(struct cpsw_ale *ale)
  156. {
  157. u32 ale_entry[ALE_ENTRY_WORDS];
  158. int type, idx;
  159. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  160. cpsw_ale_read(ale, idx, ale_entry);
  161. type = cpsw_ale_get_entry_type(ale_entry);
  162. if (type == ALE_TYPE_FREE)
  163. return idx;
  164. }
  165. return -ENOENT;
  166. }
  167. static int cpsw_ale_find_ageable(struct cpsw_ale *ale)
  168. {
  169. u32 ale_entry[ALE_ENTRY_WORDS];
  170. int type, idx;
  171. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  172. cpsw_ale_read(ale, idx, ale_entry);
  173. type = cpsw_ale_get_entry_type(ale_entry);
  174. if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
  175. continue;
  176. if (cpsw_ale_get_mcast(ale_entry))
  177. continue;
  178. type = cpsw_ale_get_ucast_type(ale_entry);
  179. if (type != ALE_UCAST_PERSISTANT &&
  180. type != ALE_UCAST_OUI)
  181. return idx;
  182. }
  183. return -ENOENT;
  184. }
  185. static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  186. int port_mask)
  187. {
  188. int mask;
  189. mask = cpsw_ale_get_port_mask(ale_entry);
  190. if ((mask & port_mask) == 0)
  191. return; /* ports dont intersect, not interested */
  192. mask &= ~port_mask;
  193. /* free if only remaining port is host port */
  194. if (mask)
  195. cpsw_ale_set_port_mask(ale_entry, mask);
  196. else
  197. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  198. }
  199. int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask)
  200. {
  201. u32 ale_entry[ALE_ENTRY_WORDS];
  202. int ret, idx;
  203. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  204. cpsw_ale_read(ale, idx, ale_entry);
  205. ret = cpsw_ale_get_entry_type(ale_entry);
  206. if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
  207. continue;
  208. if (cpsw_ale_get_mcast(ale_entry)) {
  209. u8 addr[6];
  210. cpsw_ale_get_addr(ale_entry, addr);
  211. if (!is_broadcast_ether_addr(addr))
  212. cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
  213. }
  214. cpsw_ale_write(ale, idx, ale_entry);
  215. }
  216. return 0;
  217. }
  218. static void cpsw_ale_flush_ucast(struct cpsw_ale *ale, u32 *ale_entry,
  219. int port_mask)
  220. {
  221. int port;
  222. port = cpsw_ale_get_port_num(ale_entry);
  223. if ((BIT(port) & port_mask) == 0)
  224. return; /* ports dont intersect, not interested */
  225. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  226. }
  227. int cpsw_ale_flush(struct cpsw_ale *ale, int port_mask)
  228. {
  229. u32 ale_entry[ALE_ENTRY_WORDS];
  230. int ret, idx;
  231. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  232. cpsw_ale_read(ale, idx, ale_entry);
  233. ret = cpsw_ale_get_entry_type(ale_entry);
  234. if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
  235. continue;
  236. if (cpsw_ale_get_mcast(ale_entry))
  237. cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
  238. else
  239. cpsw_ale_flush_ucast(ale, ale_entry, port_mask);
  240. cpsw_ale_write(ale, idx, ale_entry);
  241. }
  242. return 0;
  243. }
  244. static inline void cpsw_ale_set_vlan_entry_type(u32 *ale_entry,
  245. int flags, u16 vid)
  246. {
  247. if (flags & ALE_VLAN) {
  248. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN_ADDR);
  249. cpsw_ale_set_vlan_id(ale_entry, vid);
  250. } else {
  251. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
  252. }
  253. }
  254. int cpsw_ale_add_ucast(struct cpsw_ale *ale, u8 *addr, int port,
  255. int flags, u16 vid)
  256. {
  257. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  258. int idx;
  259. cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
  260. cpsw_ale_set_addr(ale_entry, addr);
  261. cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
  262. cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
  263. cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
  264. cpsw_ale_set_port_num(ale_entry, port);
  265. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  266. if (idx < 0)
  267. idx = cpsw_ale_match_free(ale);
  268. if (idx < 0)
  269. idx = cpsw_ale_find_ageable(ale);
  270. if (idx < 0)
  271. return -ENOMEM;
  272. cpsw_ale_write(ale, idx, ale_entry);
  273. return 0;
  274. }
  275. int cpsw_ale_del_ucast(struct cpsw_ale *ale, u8 *addr, int port,
  276. int flags, u16 vid)
  277. {
  278. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  279. int idx;
  280. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  281. if (idx < 0)
  282. return -ENOENT;
  283. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  284. cpsw_ale_write(ale, idx, ale_entry);
  285. return 0;
  286. }
  287. int cpsw_ale_add_mcast(struct cpsw_ale *ale, u8 *addr, int port_mask,
  288. int flags, u16 vid, int mcast_state)
  289. {
  290. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  291. int idx, mask;
  292. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  293. if (idx >= 0)
  294. cpsw_ale_read(ale, idx, ale_entry);
  295. cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
  296. cpsw_ale_set_addr(ale_entry, addr);
  297. cpsw_ale_set_super(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
  298. cpsw_ale_set_mcast_state(ale_entry, mcast_state);
  299. mask = cpsw_ale_get_port_mask(ale_entry);
  300. port_mask |= mask;
  301. cpsw_ale_set_port_mask(ale_entry, port_mask);
  302. if (idx < 0)
  303. idx = cpsw_ale_match_free(ale);
  304. if (idx < 0)
  305. idx = cpsw_ale_find_ageable(ale);
  306. if (idx < 0)
  307. return -ENOMEM;
  308. cpsw_ale_write(ale, idx, ale_entry);
  309. return 0;
  310. }
  311. int cpsw_ale_del_mcast(struct cpsw_ale *ale, u8 *addr, int port_mask,
  312. int flags, u16 vid)
  313. {
  314. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  315. int idx;
  316. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  317. if (idx < 0)
  318. return -EINVAL;
  319. cpsw_ale_read(ale, idx, ale_entry);
  320. if (port_mask)
  321. cpsw_ale_set_port_mask(ale_entry, port_mask);
  322. else
  323. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  324. cpsw_ale_write(ale, idx, ale_entry);
  325. return 0;
  326. }
  327. int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port, int untag,
  328. int reg_mcast, int unreg_mcast)
  329. {
  330. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  331. int idx;
  332. idx = cpsw_ale_match_vlan(ale, vid);
  333. if (idx >= 0)
  334. cpsw_ale_read(ale, idx, ale_entry);
  335. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN);
  336. cpsw_ale_set_vlan_id(ale_entry, vid);
  337. cpsw_ale_set_vlan_untag_force(ale_entry, untag);
  338. cpsw_ale_set_vlan_reg_mcast(ale_entry, reg_mcast);
  339. cpsw_ale_set_vlan_unreg_mcast(ale_entry, unreg_mcast);
  340. cpsw_ale_set_vlan_member_list(ale_entry, port);
  341. if (idx < 0)
  342. idx = cpsw_ale_match_free(ale);
  343. if (idx < 0)
  344. idx = cpsw_ale_find_ageable(ale);
  345. if (idx < 0)
  346. return -ENOMEM;
  347. cpsw_ale_write(ale, idx, ale_entry);
  348. return 0;
  349. }
  350. int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask)
  351. {
  352. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  353. int idx;
  354. idx = cpsw_ale_match_vlan(ale, vid);
  355. if (idx < 0)
  356. return -ENOENT;
  357. cpsw_ale_read(ale, idx, ale_entry);
  358. if (port_mask)
  359. cpsw_ale_set_vlan_member_list(ale_entry, port_mask);
  360. else
  361. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  362. cpsw_ale_write(ale, idx, ale_entry);
  363. return 0;
  364. }
  365. struct ale_control_info {
  366. const char *name;
  367. int offset, port_offset;
  368. int shift, port_shift;
  369. int bits;
  370. };
  371. static const struct ale_control_info ale_controls[ALE_NUM_CONTROLS] = {
  372. [ALE_ENABLE] = {
  373. .name = "enable",
  374. .offset = ALE_CONTROL,
  375. .port_offset = 0,
  376. .shift = 31,
  377. .port_shift = 0,
  378. .bits = 1,
  379. },
  380. [ALE_CLEAR] = {
  381. .name = "clear",
  382. .offset = ALE_CONTROL,
  383. .port_offset = 0,
  384. .shift = 30,
  385. .port_shift = 0,
  386. .bits = 1,
  387. },
  388. [ALE_AGEOUT] = {
  389. .name = "ageout",
  390. .offset = ALE_CONTROL,
  391. .port_offset = 0,
  392. .shift = 29,
  393. .port_shift = 0,
  394. .bits = 1,
  395. },
  396. [ALE_VLAN_NOLEARN] = {
  397. .name = "vlan_nolearn",
  398. .offset = ALE_CONTROL,
  399. .port_offset = 0,
  400. .shift = 7,
  401. .port_shift = 0,
  402. .bits = 1,
  403. },
  404. [ALE_NO_PORT_VLAN] = {
  405. .name = "no_port_vlan",
  406. .offset = ALE_CONTROL,
  407. .port_offset = 0,
  408. .shift = 6,
  409. .port_shift = 0,
  410. .bits = 1,
  411. },
  412. [ALE_OUI_DENY] = {
  413. .name = "oui_deny",
  414. .offset = ALE_CONTROL,
  415. .port_offset = 0,
  416. .shift = 5,
  417. .port_shift = 0,
  418. .bits = 1,
  419. },
  420. [ALE_BYPASS] = {
  421. .name = "bypass",
  422. .offset = ALE_CONTROL,
  423. .port_offset = 0,
  424. .shift = 4,
  425. .port_shift = 0,
  426. .bits = 1,
  427. },
  428. [ALE_RATE_LIMIT_TX] = {
  429. .name = "rate_limit_tx",
  430. .offset = ALE_CONTROL,
  431. .port_offset = 0,
  432. .shift = 3,
  433. .port_shift = 0,
  434. .bits = 1,
  435. },
  436. [ALE_VLAN_AWARE] = {
  437. .name = "vlan_aware",
  438. .offset = ALE_CONTROL,
  439. .port_offset = 0,
  440. .shift = 2,
  441. .port_shift = 0,
  442. .bits = 1,
  443. },
  444. [ALE_AUTH_ENABLE] = {
  445. .name = "auth_enable",
  446. .offset = ALE_CONTROL,
  447. .port_offset = 0,
  448. .shift = 1,
  449. .port_shift = 0,
  450. .bits = 1,
  451. },
  452. [ALE_RATE_LIMIT] = {
  453. .name = "rate_limit",
  454. .offset = ALE_CONTROL,
  455. .port_offset = 0,
  456. .shift = 0,
  457. .port_shift = 0,
  458. .bits = 1,
  459. },
  460. [ALE_PORT_STATE] = {
  461. .name = "port_state",
  462. .offset = ALE_PORTCTL,
  463. .port_offset = 4,
  464. .shift = 0,
  465. .port_shift = 0,
  466. .bits = 2,
  467. },
  468. [ALE_PORT_DROP_UNTAGGED] = {
  469. .name = "drop_untagged",
  470. .offset = ALE_PORTCTL,
  471. .port_offset = 4,
  472. .shift = 2,
  473. .port_shift = 0,
  474. .bits = 1,
  475. },
  476. [ALE_PORT_DROP_UNKNOWN_VLAN] = {
  477. .name = "drop_unknown",
  478. .offset = ALE_PORTCTL,
  479. .port_offset = 4,
  480. .shift = 3,
  481. .port_shift = 0,
  482. .bits = 1,
  483. },
  484. [ALE_PORT_NOLEARN] = {
  485. .name = "nolearn",
  486. .offset = ALE_PORTCTL,
  487. .port_offset = 4,
  488. .shift = 4,
  489. .port_shift = 0,
  490. .bits = 1,
  491. },
  492. [ALE_PORT_MCAST_LIMIT] = {
  493. .name = "mcast_limit",
  494. .offset = ALE_PORTCTL,
  495. .port_offset = 4,
  496. .shift = 16,
  497. .port_shift = 0,
  498. .bits = 8,
  499. },
  500. [ALE_PORT_BCAST_LIMIT] = {
  501. .name = "bcast_limit",
  502. .offset = ALE_PORTCTL,
  503. .port_offset = 4,
  504. .shift = 24,
  505. .port_shift = 0,
  506. .bits = 8,
  507. },
  508. [ALE_PORT_UNKNOWN_VLAN_MEMBER] = {
  509. .name = "unknown_vlan_member",
  510. .offset = ALE_UNKNOWNVLAN,
  511. .port_offset = 0,
  512. .shift = 0,
  513. .port_shift = 0,
  514. .bits = 6,
  515. },
  516. [ALE_PORT_UNKNOWN_MCAST_FLOOD] = {
  517. .name = "unknown_mcast_flood",
  518. .offset = ALE_UNKNOWNVLAN,
  519. .port_offset = 0,
  520. .shift = 8,
  521. .port_shift = 0,
  522. .bits = 6,
  523. },
  524. [ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = {
  525. .name = "unknown_reg_flood",
  526. .offset = ALE_UNKNOWNVLAN,
  527. .port_offset = 0,
  528. .shift = 16,
  529. .port_shift = 0,
  530. .bits = 6,
  531. },
  532. [ALE_PORT_UNTAGGED_EGRESS] = {
  533. .name = "untagged_egress",
  534. .offset = ALE_UNKNOWNVLAN,
  535. .port_offset = 0,
  536. .shift = 24,
  537. .port_shift = 0,
  538. .bits = 6,
  539. },
  540. };
  541. int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
  542. int value)
  543. {
  544. const struct ale_control_info *info;
  545. int offset, shift;
  546. u32 tmp, mask;
  547. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  548. return -EINVAL;
  549. info = &ale_controls[control];
  550. if (info->port_offset == 0 && info->port_shift == 0)
  551. port = 0; /* global, port is a dont care */
  552. if (port < 0 || port > ale->params.ale_ports)
  553. return -EINVAL;
  554. mask = BITMASK(info->bits);
  555. if (value & ~mask)
  556. return -EINVAL;
  557. offset = info->offset + (port * info->port_offset);
  558. shift = info->shift + (port * info->port_shift);
  559. tmp = __raw_readl(ale->params.ale_regs + offset);
  560. tmp = (tmp & ~(mask << shift)) | (value << shift);
  561. __raw_writel(tmp, ale->params.ale_regs + offset);
  562. return 0;
  563. }
  564. int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
  565. {
  566. const struct ale_control_info *info;
  567. int offset, shift;
  568. u32 tmp;
  569. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  570. return -EINVAL;
  571. info = &ale_controls[control];
  572. if (info->port_offset == 0 && info->port_shift == 0)
  573. port = 0; /* global, port is a dont care */
  574. if (port < 0 || port > ale->params.ale_ports)
  575. return -EINVAL;
  576. offset = info->offset + (port * info->port_offset);
  577. shift = info->shift + (port * info->port_shift);
  578. tmp = __raw_readl(ale->params.ale_regs + offset) >> shift;
  579. return tmp & BITMASK(info->bits);
  580. }
  581. static void cpsw_ale_timer(unsigned long arg)
  582. {
  583. struct cpsw_ale *ale = (struct cpsw_ale *)arg;
  584. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  585. if (ale->ageout) {
  586. ale->timer.expires = jiffies + ale->ageout;
  587. add_timer(&ale->timer);
  588. }
  589. }
  590. int cpsw_ale_set_ageout(struct cpsw_ale *ale, int ageout)
  591. {
  592. del_timer_sync(&ale->timer);
  593. ale->ageout = ageout * HZ;
  594. if (ale->ageout) {
  595. ale->timer.expires = jiffies + ale->ageout;
  596. add_timer(&ale->timer);
  597. }
  598. return 0;
  599. }
  600. void cpsw_ale_start(struct cpsw_ale *ale)
  601. {
  602. u32 rev;
  603. rev = __raw_readl(ale->params.ale_regs + ALE_IDVER);
  604. dev_dbg(ale->params.dev, "initialized cpsw ale revision %d.%d\n",
  605. ALE_VERSION_MAJOR(rev), ALE_VERSION_MINOR(rev));
  606. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
  607. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  608. init_timer(&ale->timer);
  609. ale->timer.data = (unsigned long)ale;
  610. ale->timer.function = cpsw_ale_timer;
  611. if (ale->ageout) {
  612. ale->timer.expires = jiffies + ale->ageout;
  613. add_timer(&ale->timer);
  614. }
  615. }
  616. void cpsw_ale_stop(struct cpsw_ale *ale)
  617. {
  618. del_timer_sync(&ale->timer);
  619. }
  620. struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
  621. {
  622. struct cpsw_ale *ale;
  623. ale = kzalloc(sizeof(*ale), GFP_KERNEL);
  624. if (!ale)
  625. return NULL;
  626. ale->params = *params;
  627. ale->ageout = ale->params.ale_ageout * HZ;
  628. return ale;
  629. }
  630. int cpsw_ale_destroy(struct cpsw_ale *ale)
  631. {
  632. if (!ale)
  633. return -EINVAL;
  634. cpsw_ale_stop(ale);
  635. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
  636. kfree(ale);
  637. return 0;
  638. }