cpsw_ale.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 void cpsw_ale_flush_ucast(struct cpsw_ale *ale, u32 *ale_entry,
  226. int port_mask)
  227. {
  228. int port;
  229. port = cpsw_ale_get_port_num(ale_entry);
  230. if ((BIT(port) & port_mask) == 0)
  231. return; /* ports dont intersect, not interested */
  232. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  233. }
  234. int cpsw_ale_flush(struct cpsw_ale *ale, int port_mask)
  235. {
  236. u32 ale_entry[ALE_ENTRY_WORDS];
  237. int ret, idx;
  238. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  239. cpsw_ale_read(ale, idx, ale_entry);
  240. ret = cpsw_ale_get_entry_type(ale_entry);
  241. if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
  242. continue;
  243. if (cpsw_ale_get_mcast(ale_entry))
  244. cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
  245. else
  246. cpsw_ale_flush_ucast(ale, ale_entry, port_mask);
  247. cpsw_ale_write(ale, idx, ale_entry);
  248. }
  249. return 0;
  250. }
  251. EXPORT_SYMBOL_GPL(cpsw_ale_flush);
  252. static inline void cpsw_ale_set_vlan_entry_type(u32 *ale_entry,
  253. int flags, u16 vid)
  254. {
  255. if (flags & ALE_VLAN) {
  256. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN_ADDR);
  257. cpsw_ale_set_vlan_id(ale_entry, vid);
  258. } else {
  259. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
  260. }
  261. }
  262. int cpsw_ale_add_ucast(struct cpsw_ale *ale, u8 *addr, int port,
  263. int flags, u16 vid)
  264. {
  265. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  266. int idx;
  267. cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
  268. cpsw_ale_set_addr(ale_entry, addr);
  269. cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
  270. cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
  271. cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
  272. cpsw_ale_set_port_num(ale_entry, port);
  273. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  274. if (idx < 0)
  275. idx = cpsw_ale_match_free(ale);
  276. if (idx < 0)
  277. idx = cpsw_ale_find_ageable(ale);
  278. if (idx < 0)
  279. return -ENOMEM;
  280. cpsw_ale_write(ale, idx, ale_entry);
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(cpsw_ale_add_ucast);
  284. int cpsw_ale_del_ucast(struct cpsw_ale *ale, u8 *addr, int port,
  285. int flags, u16 vid)
  286. {
  287. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  288. int idx;
  289. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  290. if (idx < 0)
  291. return -ENOENT;
  292. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  293. cpsw_ale_write(ale, idx, ale_entry);
  294. return 0;
  295. }
  296. EXPORT_SYMBOL_GPL(cpsw_ale_del_ucast);
  297. int cpsw_ale_add_mcast(struct cpsw_ale *ale, u8 *addr, int port_mask,
  298. int flags, u16 vid, int mcast_state)
  299. {
  300. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  301. int idx, mask;
  302. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  303. if (idx >= 0)
  304. cpsw_ale_read(ale, idx, ale_entry);
  305. cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
  306. cpsw_ale_set_addr(ale_entry, addr);
  307. cpsw_ale_set_super(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
  308. cpsw_ale_set_mcast_state(ale_entry, mcast_state);
  309. mask = cpsw_ale_get_port_mask(ale_entry);
  310. port_mask |= mask;
  311. cpsw_ale_set_port_mask(ale_entry, port_mask);
  312. if (idx < 0)
  313. idx = cpsw_ale_match_free(ale);
  314. if (idx < 0)
  315. idx = cpsw_ale_find_ageable(ale);
  316. if (idx < 0)
  317. return -ENOMEM;
  318. cpsw_ale_write(ale, idx, ale_entry);
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(cpsw_ale_add_mcast);
  322. int cpsw_ale_del_mcast(struct cpsw_ale *ale, u8 *addr, int port_mask,
  323. int flags, u16 vid)
  324. {
  325. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  326. int idx;
  327. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  328. if (idx < 0)
  329. return -EINVAL;
  330. cpsw_ale_read(ale, idx, ale_entry);
  331. if (port_mask)
  332. cpsw_ale_set_port_mask(ale_entry, port_mask);
  333. else
  334. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  335. cpsw_ale_write(ale, idx, ale_entry);
  336. return 0;
  337. }
  338. EXPORT_SYMBOL_GPL(cpsw_ale_del_mcast);
  339. int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port, int untag,
  340. int reg_mcast, int unreg_mcast)
  341. {
  342. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  343. int idx;
  344. idx = cpsw_ale_match_vlan(ale, vid);
  345. if (idx >= 0)
  346. cpsw_ale_read(ale, idx, ale_entry);
  347. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN);
  348. cpsw_ale_set_vlan_id(ale_entry, vid);
  349. cpsw_ale_set_vlan_untag_force(ale_entry, untag);
  350. cpsw_ale_set_vlan_reg_mcast(ale_entry, reg_mcast);
  351. cpsw_ale_set_vlan_unreg_mcast(ale_entry, unreg_mcast);
  352. cpsw_ale_set_vlan_member_list(ale_entry, port);
  353. if (idx < 0)
  354. idx = cpsw_ale_match_free(ale);
  355. if (idx < 0)
  356. idx = cpsw_ale_find_ageable(ale);
  357. if (idx < 0)
  358. return -ENOMEM;
  359. cpsw_ale_write(ale, idx, ale_entry);
  360. return 0;
  361. }
  362. EXPORT_SYMBOL_GPL(cpsw_ale_add_vlan);
  363. int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask)
  364. {
  365. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  366. int idx;
  367. idx = cpsw_ale_match_vlan(ale, vid);
  368. if (idx < 0)
  369. return -ENOENT;
  370. cpsw_ale_read(ale, idx, ale_entry);
  371. if (port_mask)
  372. cpsw_ale_set_vlan_member_list(ale_entry, port_mask);
  373. else
  374. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  375. cpsw_ale_write(ale, idx, ale_entry);
  376. return 0;
  377. }
  378. EXPORT_SYMBOL_GPL(cpsw_ale_del_vlan);
  379. void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti)
  380. {
  381. u32 ale_entry[ALE_ENTRY_WORDS];
  382. int type, idx;
  383. int unreg_mcast = 0;
  384. /* Only bother doing the work if the setting is actually changing */
  385. if (ale->allmulti == allmulti)
  386. return;
  387. /* Remember the new setting to check against next time */
  388. ale->allmulti = allmulti;
  389. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  390. cpsw_ale_read(ale, idx, ale_entry);
  391. type = cpsw_ale_get_entry_type(ale_entry);
  392. if (type != ALE_TYPE_VLAN)
  393. continue;
  394. unreg_mcast = cpsw_ale_get_vlan_unreg_mcast(ale_entry);
  395. if (allmulti)
  396. unreg_mcast |= 1;
  397. else
  398. unreg_mcast &= ~1;
  399. cpsw_ale_set_vlan_unreg_mcast(ale_entry, unreg_mcast);
  400. cpsw_ale_write(ale, idx, ale_entry);
  401. }
  402. }
  403. EXPORT_SYMBOL_GPL(cpsw_ale_set_allmulti);
  404. struct ale_control_info {
  405. const char *name;
  406. int offset, port_offset;
  407. int shift, port_shift;
  408. int bits;
  409. };
  410. static const struct ale_control_info ale_controls[ALE_NUM_CONTROLS] = {
  411. [ALE_ENABLE] = {
  412. .name = "enable",
  413. .offset = ALE_CONTROL,
  414. .port_offset = 0,
  415. .shift = 31,
  416. .port_shift = 0,
  417. .bits = 1,
  418. },
  419. [ALE_CLEAR] = {
  420. .name = "clear",
  421. .offset = ALE_CONTROL,
  422. .port_offset = 0,
  423. .shift = 30,
  424. .port_shift = 0,
  425. .bits = 1,
  426. },
  427. [ALE_AGEOUT] = {
  428. .name = "ageout",
  429. .offset = ALE_CONTROL,
  430. .port_offset = 0,
  431. .shift = 29,
  432. .port_shift = 0,
  433. .bits = 1,
  434. },
  435. [ALE_P0_UNI_FLOOD] = {
  436. .name = "port0_unicast_flood",
  437. .offset = ALE_CONTROL,
  438. .port_offset = 0,
  439. .shift = 8,
  440. .port_shift = 0,
  441. .bits = 1,
  442. },
  443. [ALE_VLAN_NOLEARN] = {
  444. .name = "vlan_nolearn",
  445. .offset = ALE_CONTROL,
  446. .port_offset = 0,
  447. .shift = 7,
  448. .port_shift = 0,
  449. .bits = 1,
  450. },
  451. [ALE_NO_PORT_VLAN] = {
  452. .name = "no_port_vlan",
  453. .offset = ALE_CONTROL,
  454. .port_offset = 0,
  455. .shift = 6,
  456. .port_shift = 0,
  457. .bits = 1,
  458. },
  459. [ALE_OUI_DENY] = {
  460. .name = "oui_deny",
  461. .offset = ALE_CONTROL,
  462. .port_offset = 0,
  463. .shift = 5,
  464. .port_shift = 0,
  465. .bits = 1,
  466. },
  467. [ALE_BYPASS] = {
  468. .name = "bypass",
  469. .offset = ALE_CONTROL,
  470. .port_offset = 0,
  471. .shift = 4,
  472. .port_shift = 0,
  473. .bits = 1,
  474. },
  475. [ALE_RATE_LIMIT_TX] = {
  476. .name = "rate_limit_tx",
  477. .offset = ALE_CONTROL,
  478. .port_offset = 0,
  479. .shift = 3,
  480. .port_shift = 0,
  481. .bits = 1,
  482. },
  483. [ALE_VLAN_AWARE] = {
  484. .name = "vlan_aware",
  485. .offset = ALE_CONTROL,
  486. .port_offset = 0,
  487. .shift = 2,
  488. .port_shift = 0,
  489. .bits = 1,
  490. },
  491. [ALE_AUTH_ENABLE] = {
  492. .name = "auth_enable",
  493. .offset = ALE_CONTROL,
  494. .port_offset = 0,
  495. .shift = 1,
  496. .port_shift = 0,
  497. .bits = 1,
  498. },
  499. [ALE_RATE_LIMIT] = {
  500. .name = "rate_limit",
  501. .offset = ALE_CONTROL,
  502. .port_offset = 0,
  503. .shift = 0,
  504. .port_shift = 0,
  505. .bits = 1,
  506. },
  507. [ALE_PORT_STATE] = {
  508. .name = "port_state",
  509. .offset = ALE_PORTCTL,
  510. .port_offset = 4,
  511. .shift = 0,
  512. .port_shift = 0,
  513. .bits = 2,
  514. },
  515. [ALE_PORT_DROP_UNTAGGED] = {
  516. .name = "drop_untagged",
  517. .offset = ALE_PORTCTL,
  518. .port_offset = 4,
  519. .shift = 2,
  520. .port_shift = 0,
  521. .bits = 1,
  522. },
  523. [ALE_PORT_DROP_UNKNOWN_VLAN] = {
  524. .name = "drop_unknown",
  525. .offset = ALE_PORTCTL,
  526. .port_offset = 4,
  527. .shift = 3,
  528. .port_shift = 0,
  529. .bits = 1,
  530. },
  531. [ALE_PORT_NOLEARN] = {
  532. .name = "nolearn",
  533. .offset = ALE_PORTCTL,
  534. .port_offset = 4,
  535. .shift = 4,
  536. .port_shift = 0,
  537. .bits = 1,
  538. },
  539. [ALE_PORT_NO_SA_UPDATE] = {
  540. .name = "no_source_update",
  541. .offset = ALE_PORTCTL,
  542. .port_offset = 4,
  543. .shift = 5,
  544. .port_shift = 0,
  545. .bits = 1,
  546. },
  547. [ALE_PORT_MCAST_LIMIT] = {
  548. .name = "mcast_limit",
  549. .offset = ALE_PORTCTL,
  550. .port_offset = 4,
  551. .shift = 16,
  552. .port_shift = 0,
  553. .bits = 8,
  554. },
  555. [ALE_PORT_BCAST_LIMIT] = {
  556. .name = "bcast_limit",
  557. .offset = ALE_PORTCTL,
  558. .port_offset = 4,
  559. .shift = 24,
  560. .port_shift = 0,
  561. .bits = 8,
  562. },
  563. [ALE_PORT_UNKNOWN_VLAN_MEMBER] = {
  564. .name = "unknown_vlan_member",
  565. .offset = ALE_UNKNOWNVLAN,
  566. .port_offset = 0,
  567. .shift = 0,
  568. .port_shift = 0,
  569. .bits = 6,
  570. },
  571. [ALE_PORT_UNKNOWN_MCAST_FLOOD] = {
  572. .name = "unknown_mcast_flood",
  573. .offset = ALE_UNKNOWNVLAN,
  574. .port_offset = 0,
  575. .shift = 8,
  576. .port_shift = 0,
  577. .bits = 6,
  578. },
  579. [ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = {
  580. .name = "unknown_reg_flood",
  581. .offset = ALE_UNKNOWNVLAN,
  582. .port_offset = 0,
  583. .shift = 16,
  584. .port_shift = 0,
  585. .bits = 6,
  586. },
  587. [ALE_PORT_UNTAGGED_EGRESS] = {
  588. .name = "untagged_egress",
  589. .offset = ALE_UNKNOWNVLAN,
  590. .port_offset = 0,
  591. .shift = 24,
  592. .port_shift = 0,
  593. .bits = 6,
  594. },
  595. };
  596. int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
  597. int value)
  598. {
  599. const struct ale_control_info *info;
  600. int offset, shift;
  601. u32 tmp, mask;
  602. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  603. return -EINVAL;
  604. info = &ale_controls[control];
  605. if (info->port_offset == 0 && info->port_shift == 0)
  606. port = 0; /* global, port is a dont care */
  607. if (port < 0 || port > ale->params.ale_ports)
  608. return -EINVAL;
  609. mask = BITMASK(info->bits);
  610. if (value & ~mask)
  611. return -EINVAL;
  612. offset = info->offset + (port * info->port_offset);
  613. shift = info->shift + (port * info->port_shift);
  614. tmp = __raw_readl(ale->params.ale_regs + offset);
  615. tmp = (tmp & ~(mask << shift)) | (value << shift);
  616. __raw_writel(tmp, ale->params.ale_regs + offset);
  617. return 0;
  618. }
  619. EXPORT_SYMBOL_GPL(cpsw_ale_control_set);
  620. int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
  621. {
  622. const struct ale_control_info *info;
  623. int offset, shift;
  624. u32 tmp;
  625. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  626. return -EINVAL;
  627. info = &ale_controls[control];
  628. if (info->port_offset == 0 && info->port_shift == 0)
  629. port = 0; /* global, port is a dont care */
  630. if (port < 0 || port > ale->params.ale_ports)
  631. return -EINVAL;
  632. offset = info->offset + (port * info->port_offset);
  633. shift = info->shift + (port * info->port_shift);
  634. tmp = __raw_readl(ale->params.ale_regs + offset) >> shift;
  635. return tmp & BITMASK(info->bits);
  636. }
  637. EXPORT_SYMBOL_GPL(cpsw_ale_control_get);
  638. static void cpsw_ale_timer(unsigned long arg)
  639. {
  640. struct cpsw_ale *ale = (struct cpsw_ale *)arg;
  641. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  642. if (ale->ageout) {
  643. ale->timer.expires = jiffies + ale->ageout;
  644. add_timer(&ale->timer);
  645. }
  646. }
  647. int cpsw_ale_set_ageout(struct cpsw_ale *ale, int ageout)
  648. {
  649. del_timer_sync(&ale->timer);
  650. ale->ageout = ageout * HZ;
  651. if (ale->ageout) {
  652. ale->timer.expires = jiffies + ale->ageout;
  653. add_timer(&ale->timer);
  654. }
  655. return 0;
  656. }
  657. EXPORT_SYMBOL_GPL(cpsw_ale_set_ageout);
  658. void cpsw_ale_start(struct cpsw_ale *ale)
  659. {
  660. u32 rev;
  661. rev = __raw_readl(ale->params.ale_regs + ALE_IDVER);
  662. dev_dbg(ale->params.dev, "initialized cpsw ale revision %d.%d\n",
  663. ALE_VERSION_MAJOR(rev), ALE_VERSION_MINOR(rev));
  664. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
  665. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  666. init_timer(&ale->timer);
  667. ale->timer.data = (unsigned long)ale;
  668. ale->timer.function = cpsw_ale_timer;
  669. if (ale->ageout) {
  670. ale->timer.expires = jiffies + ale->ageout;
  671. add_timer(&ale->timer);
  672. }
  673. }
  674. EXPORT_SYMBOL_GPL(cpsw_ale_start);
  675. void cpsw_ale_stop(struct cpsw_ale *ale)
  676. {
  677. del_timer_sync(&ale->timer);
  678. }
  679. EXPORT_SYMBOL_GPL(cpsw_ale_stop);
  680. struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
  681. {
  682. struct cpsw_ale *ale;
  683. ale = kzalloc(sizeof(*ale), GFP_KERNEL);
  684. if (!ale)
  685. return NULL;
  686. ale->params = *params;
  687. ale->ageout = ale->params.ale_ageout * HZ;
  688. return ale;
  689. }
  690. EXPORT_SYMBOL_GPL(cpsw_ale_create);
  691. int cpsw_ale_destroy(struct cpsw_ale *ale)
  692. {
  693. if (!ale)
  694. return -EINVAL;
  695. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
  696. kfree(ale);
  697. return 0;
  698. }
  699. EXPORT_SYMBOL_GPL(cpsw_ale_destroy);
  700. void cpsw_ale_dump(struct cpsw_ale *ale, u32 *data)
  701. {
  702. int i;
  703. for (i = 0; i < ale->params.ale_entries; i++) {
  704. cpsw_ale_read(ale, i, data);
  705. data += ALE_ENTRY_WORDS;
  706. }
  707. }
  708. EXPORT_SYMBOL_GPL(cpsw_ale_dump);
  709. MODULE_LICENSE("GPL v2");
  710. MODULE_DESCRIPTION("TI CPSW ALE driver");
  711. MODULE_AUTHOR("Texas Instruments");