cpsw_ale.c 18 KB

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