cpsw_ale.c 18 KB

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