cpsw_ale.c 15 KB

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