cpsw_ale.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Texas Instruments N-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, mask) (((rev) >> 8) & (mask))
  28. #define ALE_VERSION_MINOR(rev) (rev & 0xff)
  29. #define ALE_VERSION_1R3 0x0103
  30. #define ALE_VERSION_1R4 0x0104
  31. /* ALE Registers */
  32. #define ALE_IDVER 0x00
  33. #define ALE_STATUS 0x04
  34. #define ALE_CONTROL 0x08
  35. #define ALE_PRESCALE 0x10
  36. #define ALE_UNKNOWNVLAN 0x18
  37. #define ALE_TABLE_CONTROL 0x20
  38. #define ALE_TABLE 0x34
  39. #define ALE_PORTCTL 0x40
  40. /* ALE NetCP NU switch specific Registers */
  41. #define ALE_UNKNOWNVLAN_MEMBER 0x90
  42. #define ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD 0x94
  43. #define ALE_UNKNOWNVLAN_REG_MCAST_FLOOD 0x98
  44. #define ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS 0x9C
  45. #define ALE_VLAN_MASK_MUX(reg) (0xc0 + (0x4 * (reg)))
  46. #define ALE_TABLE_WRITE BIT(31)
  47. #define ALE_TYPE_FREE 0
  48. #define ALE_TYPE_ADDR 1
  49. #define ALE_TYPE_VLAN 2
  50. #define ALE_TYPE_VLAN_ADDR 3
  51. #define ALE_UCAST_PERSISTANT 0
  52. #define ALE_UCAST_UNTOUCHED 1
  53. #define ALE_UCAST_OUI 2
  54. #define ALE_UCAST_TOUCHED 3
  55. #define ALE_TABLE_SIZE_MULTIPLIER 1024
  56. #define ALE_STATUS_SIZE_MASK 0x1f
  57. #define ALE_TABLE_SIZE_DEFAULT 64
  58. static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
  59. {
  60. int idx;
  61. idx = start / 32;
  62. start -= idx * 32;
  63. idx = 2 - idx; /* flip */
  64. return (ale_entry[idx] >> start) & BITMASK(bits);
  65. }
  66. static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
  67. u32 value)
  68. {
  69. int idx;
  70. value &= BITMASK(bits);
  71. idx = start / 32;
  72. start -= idx * 32;
  73. idx = 2 - idx; /* flip */
  74. ale_entry[idx] &= ~(BITMASK(bits) << start);
  75. ale_entry[idx] |= (value << start);
  76. }
  77. #define DEFINE_ALE_FIELD(name, start, bits) \
  78. static inline int cpsw_ale_get_##name(u32 *ale_entry) \
  79. { \
  80. return cpsw_ale_get_field(ale_entry, start, bits); \
  81. } \
  82. static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
  83. { \
  84. cpsw_ale_set_field(ale_entry, start, bits, value); \
  85. }
  86. #define DEFINE_ALE_FIELD1(name, start) \
  87. static inline int cpsw_ale_get_##name(u32 *ale_entry, u32 bits) \
  88. { \
  89. return cpsw_ale_get_field(ale_entry, start, bits); \
  90. } \
  91. static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value, \
  92. u32 bits) \
  93. { \
  94. cpsw_ale_set_field(ale_entry, start, bits, value); \
  95. }
  96. DEFINE_ALE_FIELD(entry_type, 60, 2)
  97. DEFINE_ALE_FIELD(vlan_id, 48, 12)
  98. DEFINE_ALE_FIELD(mcast_state, 62, 2)
  99. DEFINE_ALE_FIELD1(port_mask, 66)
  100. DEFINE_ALE_FIELD(super, 65, 1)
  101. DEFINE_ALE_FIELD(ucast_type, 62, 2)
  102. DEFINE_ALE_FIELD1(port_num, 66)
  103. DEFINE_ALE_FIELD(blocked, 65, 1)
  104. DEFINE_ALE_FIELD(secure, 64, 1)
  105. DEFINE_ALE_FIELD1(vlan_untag_force, 24)
  106. DEFINE_ALE_FIELD1(vlan_reg_mcast, 16)
  107. DEFINE_ALE_FIELD1(vlan_unreg_mcast, 8)
  108. DEFINE_ALE_FIELD1(vlan_member_list, 0)
  109. DEFINE_ALE_FIELD(mcast, 40, 1)
  110. /* ALE NetCP nu switch specific */
  111. DEFINE_ALE_FIELD(vlan_unreg_mcast_idx, 20, 3)
  112. DEFINE_ALE_FIELD(vlan_reg_mcast_idx, 44, 3)
  113. /* The MAC address field in the ALE entry cannot be macroized as above */
  114. static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
  115. {
  116. int i;
  117. for (i = 0; i < 6; i++)
  118. addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
  119. }
  120. static inline void cpsw_ale_set_addr(u32 *ale_entry, u8 *addr)
  121. {
  122. int i;
  123. for (i = 0; i < 6; i++)
  124. cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
  125. }
  126. static int cpsw_ale_read(struct cpsw_ale *ale, int idx, u32 *ale_entry)
  127. {
  128. int i;
  129. WARN_ON(idx > ale->params.ale_entries);
  130. writel_relaxed(idx, ale->params.ale_regs + ALE_TABLE_CONTROL);
  131. for (i = 0; i < ALE_ENTRY_WORDS; i++)
  132. ale_entry[i] = readl_relaxed(ale->params.ale_regs +
  133. ALE_TABLE + 4 * i);
  134. return idx;
  135. }
  136. static int cpsw_ale_write(struct cpsw_ale *ale, int idx, u32 *ale_entry)
  137. {
  138. int i;
  139. WARN_ON(idx > ale->params.ale_entries);
  140. for (i = 0; i < ALE_ENTRY_WORDS; i++)
  141. writel_relaxed(ale_entry[i], ale->params.ale_regs +
  142. ALE_TABLE + 4 * i);
  143. writel_relaxed(idx | ALE_TABLE_WRITE, ale->params.ale_regs +
  144. ALE_TABLE_CONTROL);
  145. return idx;
  146. }
  147. static int cpsw_ale_match_addr(struct cpsw_ale *ale, u8 *addr, u16 vid)
  148. {
  149. u32 ale_entry[ALE_ENTRY_WORDS];
  150. int type, idx;
  151. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  152. u8 entry_addr[6];
  153. cpsw_ale_read(ale, idx, ale_entry);
  154. type = cpsw_ale_get_entry_type(ale_entry);
  155. if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
  156. continue;
  157. if (cpsw_ale_get_vlan_id(ale_entry) != vid)
  158. continue;
  159. cpsw_ale_get_addr(ale_entry, entry_addr);
  160. if (ether_addr_equal(entry_addr, addr))
  161. return idx;
  162. }
  163. return -ENOENT;
  164. }
  165. static int cpsw_ale_match_vlan(struct cpsw_ale *ale, u16 vid)
  166. {
  167. u32 ale_entry[ALE_ENTRY_WORDS];
  168. int type, idx;
  169. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  170. cpsw_ale_read(ale, idx, ale_entry);
  171. type = cpsw_ale_get_entry_type(ale_entry);
  172. if (type != ALE_TYPE_VLAN)
  173. continue;
  174. if (cpsw_ale_get_vlan_id(ale_entry) == vid)
  175. return idx;
  176. }
  177. return -ENOENT;
  178. }
  179. static int cpsw_ale_match_free(struct cpsw_ale *ale)
  180. {
  181. u32 ale_entry[ALE_ENTRY_WORDS];
  182. int type, idx;
  183. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  184. cpsw_ale_read(ale, idx, ale_entry);
  185. type = cpsw_ale_get_entry_type(ale_entry);
  186. if (type == ALE_TYPE_FREE)
  187. return idx;
  188. }
  189. return -ENOENT;
  190. }
  191. static int cpsw_ale_find_ageable(struct cpsw_ale *ale)
  192. {
  193. u32 ale_entry[ALE_ENTRY_WORDS];
  194. int type, idx;
  195. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  196. cpsw_ale_read(ale, idx, ale_entry);
  197. type = cpsw_ale_get_entry_type(ale_entry);
  198. if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
  199. continue;
  200. if (cpsw_ale_get_mcast(ale_entry))
  201. continue;
  202. type = cpsw_ale_get_ucast_type(ale_entry);
  203. if (type != ALE_UCAST_PERSISTANT &&
  204. type != ALE_UCAST_OUI)
  205. return idx;
  206. }
  207. return -ENOENT;
  208. }
  209. static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  210. int port_mask)
  211. {
  212. int mask;
  213. mask = cpsw_ale_get_port_mask(ale_entry,
  214. ale->port_mask_bits);
  215. if ((mask & port_mask) == 0)
  216. return; /* ports dont intersect, not interested */
  217. mask &= ~port_mask;
  218. /* free if only remaining port is host port */
  219. if (mask)
  220. cpsw_ale_set_port_mask(ale_entry, mask,
  221. ale->port_mask_bits);
  222. else
  223. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  224. }
  225. int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask, int vid)
  226. {
  227. u32 ale_entry[ALE_ENTRY_WORDS];
  228. int ret, idx;
  229. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  230. cpsw_ale_read(ale, idx, ale_entry);
  231. ret = cpsw_ale_get_entry_type(ale_entry);
  232. if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
  233. continue;
  234. /* if vid passed is -1 then remove all multicast entry from
  235. * the table irrespective of vlan id, if a valid vlan id is
  236. * passed then remove only multicast added to that vlan id.
  237. * if vlan id doesn't match then move on to next entry.
  238. */
  239. if (vid != -1 && cpsw_ale_get_vlan_id(ale_entry) != vid)
  240. continue;
  241. if (cpsw_ale_get_mcast(ale_entry)) {
  242. u8 addr[6];
  243. cpsw_ale_get_addr(ale_entry, addr);
  244. if (!is_broadcast_ether_addr(addr))
  245. cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
  246. }
  247. cpsw_ale_write(ale, idx, ale_entry);
  248. }
  249. return 0;
  250. }
  251. EXPORT_SYMBOL_GPL(cpsw_ale_flush_multicast);
  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, ale->port_num_bits);
  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. ale->port_mask_bits);
  311. port_mask |= mask;
  312. cpsw_ale_set_port_mask(ale_entry, port_mask,
  313. ale->port_mask_bits);
  314. if (idx < 0)
  315. idx = cpsw_ale_match_free(ale);
  316. if (idx < 0)
  317. idx = cpsw_ale_find_ageable(ale);
  318. if (idx < 0)
  319. return -ENOMEM;
  320. cpsw_ale_write(ale, idx, ale_entry);
  321. return 0;
  322. }
  323. EXPORT_SYMBOL_GPL(cpsw_ale_add_mcast);
  324. int cpsw_ale_del_mcast(struct cpsw_ale *ale, u8 *addr, int port_mask,
  325. int flags, u16 vid)
  326. {
  327. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  328. int idx;
  329. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  330. if (idx < 0)
  331. return -ENOENT;
  332. cpsw_ale_read(ale, idx, ale_entry);
  333. if (port_mask)
  334. cpsw_ale_set_port_mask(ale_entry, port_mask,
  335. ale->port_mask_bits);
  336. else
  337. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  338. cpsw_ale_write(ale, idx, ale_entry);
  339. return 0;
  340. }
  341. EXPORT_SYMBOL_GPL(cpsw_ale_del_mcast);
  342. /* ALE NetCP NU switch specific vlan functions */
  343. static void cpsw_ale_set_vlan_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  344. int reg_mcast, int unreg_mcast)
  345. {
  346. int idx;
  347. /* Set VLAN registered multicast flood mask */
  348. idx = cpsw_ale_get_vlan_reg_mcast_idx(ale_entry);
  349. writel(reg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  350. /* Set VLAN unregistered multicast flood mask */
  351. idx = cpsw_ale_get_vlan_unreg_mcast_idx(ale_entry);
  352. writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  353. }
  354. int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port, int untag,
  355. int reg_mcast, int unreg_mcast)
  356. {
  357. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  358. int idx;
  359. idx = cpsw_ale_match_vlan(ale, vid);
  360. if (idx >= 0)
  361. cpsw_ale_read(ale, idx, ale_entry);
  362. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN);
  363. cpsw_ale_set_vlan_id(ale_entry, vid);
  364. cpsw_ale_set_vlan_untag_force(ale_entry, untag, ale->vlan_field_bits);
  365. if (!ale->params.nu_switch_ale) {
  366. cpsw_ale_set_vlan_reg_mcast(ale_entry, reg_mcast,
  367. ale->vlan_field_bits);
  368. cpsw_ale_set_vlan_unreg_mcast(ale_entry, unreg_mcast,
  369. ale->vlan_field_bits);
  370. } else {
  371. cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast, unreg_mcast);
  372. }
  373. cpsw_ale_set_vlan_member_list(ale_entry, port, ale->vlan_field_bits);
  374. if (idx < 0)
  375. idx = cpsw_ale_match_free(ale);
  376. if (idx < 0)
  377. idx = cpsw_ale_find_ageable(ale);
  378. if (idx < 0)
  379. return -ENOMEM;
  380. cpsw_ale_write(ale, idx, ale_entry);
  381. return 0;
  382. }
  383. EXPORT_SYMBOL_GPL(cpsw_ale_add_vlan);
  384. int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask)
  385. {
  386. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  387. int idx;
  388. idx = cpsw_ale_match_vlan(ale, vid);
  389. if (idx < 0)
  390. return -ENOENT;
  391. cpsw_ale_read(ale, idx, ale_entry);
  392. if (port_mask)
  393. cpsw_ale_set_vlan_member_list(ale_entry, port_mask,
  394. ale->vlan_field_bits);
  395. else
  396. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  397. cpsw_ale_write(ale, idx, ale_entry);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL_GPL(cpsw_ale_del_vlan);
  401. void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti)
  402. {
  403. u32 ale_entry[ALE_ENTRY_WORDS];
  404. int type, idx;
  405. int unreg_mcast = 0;
  406. /* Only bother doing the work if the setting is actually changing */
  407. if (ale->allmulti == allmulti)
  408. return;
  409. /* Remember the new setting to check against next time */
  410. ale->allmulti = allmulti;
  411. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  412. cpsw_ale_read(ale, idx, ale_entry);
  413. type = cpsw_ale_get_entry_type(ale_entry);
  414. if (type != ALE_TYPE_VLAN)
  415. continue;
  416. unreg_mcast =
  417. cpsw_ale_get_vlan_unreg_mcast(ale_entry,
  418. ale->vlan_field_bits);
  419. if (allmulti)
  420. unreg_mcast |= 1;
  421. else
  422. unreg_mcast &= ~1;
  423. cpsw_ale_set_vlan_unreg_mcast(ale_entry, unreg_mcast,
  424. ale->vlan_field_bits);
  425. cpsw_ale_write(ale, idx, ale_entry);
  426. }
  427. }
  428. EXPORT_SYMBOL_GPL(cpsw_ale_set_allmulti);
  429. struct ale_control_info {
  430. const char *name;
  431. int offset, port_offset;
  432. int shift, port_shift;
  433. int bits;
  434. };
  435. static struct ale_control_info ale_controls[ALE_NUM_CONTROLS] = {
  436. [ALE_ENABLE] = {
  437. .name = "enable",
  438. .offset = ALE_CONTROL,
  439. .port_offset = 0,
  440. .shift = 31,
  441. .port_shift = 0,
  442. .bits = 1,
  443. },
  444. [ALE_CLEAR] = {
  445. .name = "clear",
  446. .offset = ALE_CONTROL,
  447. .port_offset = 0,
  448. .shift = 30,
  449. .port_shift = 0,
  450. .bits = 1,
  451. },
  452. [ALE_AGEOUT] = {
  453. .name = "ageout",
  454. .offset = ALE_CONTROL,
  455. .port_offset = 0,
  456. .shift = 29,
  457. .port_shift = 0,
  458. .bits = 1,
  459. },
  460. [ALE_P0_UNI_FLOOD] = {
  461. .name = "port0_unicast_flood",
  462. .offset = ALE_CONTROL,
  463. .port_offset = 0,
  464. .shift = 8,
  465. .port_shift = 0,
  466. .bits = 1,
  467. },
  468. [ALE_VLAN_NOLEARN] = {
  469. .name = "vlan_nolearn",
  470. .offset = ALE_CONTROL,
  471. .port_offset = 0,
  472. .shift = 7,
  473. .port_shift = 0,
  474. .bits = 1,
  475. },
  476. [ALE_NO_PORT_VLAN] = {
  477. .name = "no_port_vlan",
  478. .offset = ALE_CONTROL,
  479. .port_offset = 0,
  480. .shift = 6,
  481. .port_shift = 0,
  482. .bits = 1,
  483. },
  484. [ALE_OUI_DENY] = {
  485. .name = "oui_deny",
  486. .offset = ALE_CONTROL,
  487. .port_offset = 0,
  488. .shift = 5,
  489. .port_shift = 0,
  490. .bits = 1,
  491. },
  492. [ALE_BYPASS] = {
  493. .name = "bypass",
  494. .offset = ALE_CONTROL,
  495. .port_offset = 0,
  496. .shift = 4,
  497. .port_shift = 0,
  498. .bits = 1,
  499. },
  500. [ALE_RATE_LIMIT_TX] = {
  501. .name = "rate_limit_tx",
  502. .offset = ALE_CONTROL,
  503. .port_offset = 0,
  504. .shift = 3,
  505. .port_shift = 0,
  506. .bits = 1,
  507. },
  508. [ALE_VLAN_AWARE] = {
  509. .name = "vlan_aware",
  510. .offset = ALE_CONTROL,
  511. .port_offset = 0,
  512. .shift = 2,
  513. .port_shift = 0,
  514. .bits = 1,
  515. },
  516. [ALE_AUTH_ENABLE] = {
  517. .name = "auth_enable",
  518. .offset = ALE_CONTROL,
  519. .port_offset = 0,
  520. .shift = 1,
  521. .port_shift = 0,
  522. .bits = 1,
  523. },
  524. [ALE_RATE_LIMIT] = {
  525. .name = "rate_limit",
  526. .offset = ALE_CONTROL,
  527. .port_offset = 0,
  528. .shift = 0,
  529. .port_shift = 0,
  530. .bits = 1,
  531. },
  532. [ALE_PORT_STATE] = {
  533. .name = "port_state",
  534. .offset = ALE_PORTCTL,
  535. .port_offset = 4,
  536. .shift = 0,
  537. .port_shift = 0,
  538. .bits = 2,
  539. },
  540. [ALE_PORT_DROP_UNTAGGED] = {
  541. .name = "drop_untagged",
  542. .offset = ALE_PORTCTL,
  543. .port_offset = 4,
  544. .shift = 2,
  545. .port_shift = 0,
  546. .bits = 1,
  547. },
  548. [ALE_PORT_DROP_UNKNOWN_VLAN] = {
  549. .name = "drop_unknown",
  550. .offset = ALE_PORTCTL,
  551. .port_offset = 4,
  552. .shift = 3,
  553. .port_shift = 0,
  554. .bits = 1,
  555. },
  556. [ALE_PORT_NOLEARN] = {
  557. .name = "nolearn",
  558. .offset = ALE_PORTCTL,
  559. .port_offset = 4,
  560. .shift = 4,
  561. .port_shift = 0,
  562. .bits = 1,
  563. },
  564. [ALE_PORT_NO_SA_UPDATE] = {
  565. .name = "no_source_update",
  566. .offset = ALE_PORTCTL,
  567. .port_offset = 4,
  568. .shift = 5,
  569. .port_shift = 0,
  570. .bits = 1,
  571. },
  572. [ALE_PORT_MCAST_LIMIT] = {
  573. .name = "mcast_limit",
  574. .offset = ALE_PORTCTL,
  575. .port_offset = 4,
  576. .shift = 16,
  577. .port_shift = 0,
  578. .bits = 8,
  579. },
  580. [ALE_PORT_BCAST_LIMIT] = {
  581. .name = "bcast_limit",
  582. .offset = ALE_PORTCTL,
  583. .port_offset = 4,
  584. .shift = 24,
  585. .port_shift = 0,
  586. .bits = 8,
  587. },
  588. [ALE_PORT_UNKNOWN_VLAN_MEMBER] = {
  589. .name = "unknown_vlan_member",
  590. .offset = ALE_UNKNOWNVLAN,
  591. .port_offset = 0,
  592. .shift = 0,
  593. .port_shift = 0,
  594. .bits = 6,
  595. },
  596. [ALE_PORT_UNKNOWN_MCAST_FLOOD] = {
  597. .name = "unknown_mcast_flood",
  598. .offset = ALE_UNKNOWNVLAN,
  599. .port_offset = 0,
  600. .shift = 8,
  601. .port_shift = 0,
  602. .bits = 6,
  603. },
  604. [ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = {
  605. .name = "unknown_reg_flood",
  606. .offset = ALE_UNKNOWNVLAN,
  607. .port_offset = 0,
  608. .shift = 16,
  609. .port_shift = 0,
  610. .bits = 6,
  611. },
  612. [ALE_PORT_UNTAGGED_EGRESS] = {
  613. .name = "untagged_egress",
  614. .offset = ALE_UNKNOWNVLAN,
  615. .port_offset = 0,
  616. .shift = 24,
  617. .port_shift = 0,
  618. .bits = 6,
  619. },
  620. };
  621. int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
  622. int value)
  623. {
  624. const struct ale_control_info *info;
  625. int offset, shift;
  626. u32 tmp, mask;
  627. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  628. return -EINVAL;
  629. info = &ale_controls[control];
  630. if (info->port_offset == 0 && info->port_shift == 0)
  631. port = 0; /* global, port is a dont care */
  632. if (port < 0 || port >= ale->params.ale_ports)
  633. return -EINVAL;
  634. mask = BITMASK(info->bits);
  635. if (value & ~mask)
  636. return -EINVAL;
  637. offset = info->offset + (port * info->port_offset);
  638. shift = info->shift + (port * info->port_shift);
  639. tmp = readl_relaxed(ale->params.ale_regs + offset);
  640. tmp = (tmp & ~(mask << shift)) | (value << shift);
  641. writel_relaxed(tmp, ale->params.ale_regs + offset);
  642. return 0;
  643. }
  644. EXPORT_SYMBOL_GPL(cpsw_ale_control_set);
  645. int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
  646. {
  647. const struct ale_control_info *info;
  648. int offset, shift;
  649. u32 tmp;
  650. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  651. return -EINVAL;
  652. info = &ale_controls[control];
  653. if (info->port_offset == 0 && info->port_shift == 0)
  654. port = 0; /* global, port is a dont care */
  655. if (port < 0 || port >= ale->params.ale_ports)
  656. return -EINVAL;
  657. offset = info->offset + (port * info->port_offset);
  658. shift = info->shift + (port * info->port_shift);
  659. tmp = readl_relaxed(ale->params.ale_regs + offset) >> shift;
  660. return tmp & BITMASK(info->bits);
  661. }
  662. EXPORT_SYMBOL_GPL(cpsw_ale_control_get);
  663. static void cpsw_ale_timer(struct timer_list *t)
  664. {
  665. struct cpsw_ale *ale = from_timer(ale, t, timer);
  666. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  667. if (ale->ageout) {
  668. ale->timer.expires = jiffies + ale->ageout;
  669. add_timer(&ale->timer);
  670. }
  671. }
  672. void cpsw_ale_start(struct cpsw_ale *ale)
  673. {
  674. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
  675. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  676. timer_setup(&ale->timer, cpsw_ale_timer, 0);
  677. if (ale->ageout) {
  678. ale->timer.expires = jiffies + ale->ageout;
  679. add_timer(&ale->timer);
  680. }
  681. }
  682. EXPORT_SYMBOL_GPL(cpsw_ale_start);
  683. void cpsw_ale_stop(struct cpsw_ale *ale)
  684. {
  685. del_timer_sync(&ale->timer);
  686. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  687. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
  688. }
  689. EXPORT_SYMBOL_GPL(cpsw_ale_stop);
  690. struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
  691. {
  692. struct cpsw_ale *ale;
  693. u32 rev, ale_entries;
  694. ale = devm_kzalloc(params->dev, sizeof(*ale), GFP_KERNEL);
  695. if (!ale)
  696. return NULL;
  697. ale->params = *params;
  698. ale->ageout = ale->params.ale_ageout * HZ;
  699. rev = readl_relaxed(ale->params.ale_regs + ALE_IDVER);
  700. if (!ale->params.major_ver_mask)
  701. ale->params.major_ver_mask = 0xff;
  702. ale->version =
  703. (ALE_VERSION_MAJOR(rev, ale->params.major_ver_mask) << 8) |
  704. ALE_VERSION_MINOR(rev);
  705. dev_info(ale->params.dev, "initialized cpsw ale version %d.%d\n",
  706. ALE_VERSION_MAJOR(rev, ale->params.major_ver_mask),
  707. ALE_VERSION_MINOR(rev));
  708. if (!ale->params.ale_entries) {
  709. ale_entries =
  710. readl_relaxed(ale->params.ale_regs + ALE_STATUS) &
  711. ALE_STATUS_SIZE_MASK;
  712. /* ALE available on newer NetCP switches has introduced
  713. * a register, ALE_STATUS, to indicate the size of ALE
  714. * table which shows the size as a multiple of 1024 entries.
  715. * For these, params.ale_entries will be set to zero. So
  716. * read the register and update the value of ale_entries.
  717. * ALE table on NetCP lite, is much smaller and is indicated
  718. * by a value of zero in ALE_STATUS. So use a default value
  719. * of ALE_TABLE_SIZE_DEFAULT for this. Caller is expected
  720. * to set the value of ale_entries for all other versions
  721. * of ALE.
  722. */
  723. if (!ale_entries)
  724. ale_entries = ALE_TABLE_SIZE_DEFAULT;
  725. else
  726. ale_entries *= ALE_TABLE_SIZE_MULTIPLIER;
  727. ale->params.ale_entries = ale_entries;
  728. }
  729. dev_info(ale->params.dev,
  730. "ALE Table size %ld\n", ale->params.ale_entries);
  731. /* set default bits for existing h/w */
  732. ale->port_mask_bits = ale->params.ale_ports;
  733. ale->port_num_bits = order_base_2(ale->params.ale_ports);
  734. ale->vlan_field_bits = ale->params.ale_ports;
  735. /* Set defaults override for ALE on NetCP NU switch and for version
  736. * 1R3
  737. */
  738. if (ale->params.nu_switch_ale) {
  739. /* Separate registers for unknown vlan configuration.
  740. * Also there are N bits, where N is number of ale
  741. * ports and shift value should be 0
  742. */
  743. ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].bits =
  744. ale->params.ale_ports;
  745. ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].offset =
  746. ALE_UNKNOWNVLAN_MEMBER;
  747. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].bits =
  748. ale->params.ale_ports;
  749. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].shift = 0;
  750. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].offset =
  751. ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD;
  752. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].bits =
  753. ale->params.ale_ports;
  754. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].shift = 0;
  755. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].offset =
  756. ALE_UNKNOWNVLAN_REG_MCAST_FLOOD;
  757. ale_controls[ALE_PORT_UNTAGGED_EGRESS].bits =
  758. ale->params.ale_ports;
  759. ale_controls[ALE_PORT_UNTAGGED_EGRESS].shift = 0;
  760. ale_controls[ALE_PORT_UNTAGGED_EGRESS].offset =
  761. ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS;
  762. }
  763. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  764. return ale;
  765. }
  766. EXPORT_SYMBOL_GPL(cpsw_ale_create);
  767. void cpsw_ale_dump(struct cpsw_ale *ale, u32 *data)
  768. {
  769. int i;
  770. for (i = 0; i < ale->params.ale_entries; i++) {
  771. cpsw_ale_read(ale, i, data);
  772. data += ALE_ENTRY_WORDS;
  773. }
  774. }
  775. EXPORT_SYMBOL_GPL(cpsw_ale_dump);
  776. MODULE_LICENSE("GPL v2");
  777. MODULE_DESCRIPTION("TI CPSW ALE driver");
  778. MODULE_AUTHOR("Texas Instruments");