cpsw_ale.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Texas Instruments N-Port Ethernet Switch Address Lookup Engine
  4. *
  5. * Copyright (C) 2012 Texas Instruments
  6. *
  7. */
  8. #include <linux/bitmap.h>
  9. #include <linux/if_vlan.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <linux/err.h>
  16. #include <linux/io.h>
  17. #include <linux/stat.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/etherdevice.h>
  20. #include "cpsw_ale.h"
  21. #define BITMASK(bits) (BIT(bits) - 1)
  22. #define ALE_VERSION_MAJOR(rev, mask) (((rev) >> 8) & (mask))
  23. #define ALE_VERSION_MINOR(rev) (rev & 0xff)
  24. #define ALE_VERSION_1R3 0x0103
  25. #define ALE_VERSION_1R4 0x0104
  26. /* ALE Registers */
  27. #define ALE_IDVER 0x00
  28. #define ALE_STATUS 0x04
  29. #define ALE_CONTROL 0x08
  30. #define ALE_PRESCALE 0x10
  31. #define ALE_AGING_TIMER 0x14
  32. #define ALE_UNKNOWNVLAN 0x18
  33. #define ALE_TABLE_CONTROL 0x20
  34. #define ALE_TABLE 0x34
  35. #define ALE_PORTCTL 0x40
  36. /* ALE NetCP NU switch specific Registers */
  37. #define ALE_UNKNOWNVLAN_MEMBER 0x90
  38. #define ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD 0x94
  39. #define ALE_UNKNOWNVLAN_REG_MCAST_FLOOD 0x98
  40. #define ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS 0x9C
  41. #define ALE_VLAN_MASK_MUX(reg) (0xc0 + (0x4 * (reg)))
  42. #define AM65_CPSW_ALE_THREAD_DEF_REG 0x134
  43. /* ALE_AGING_TIMER */
  44. #define ALE_AGING_TIMER_MASK GENMASK(23, 0)
  45. #define ALE_RATE_LIMIT_MIN_PPS 1000
  46. /**
  47. * struct ale_entry_fld - The ALE tbl entry field description
  48. * @start_bit: field start bit
  49. * @num_bits: field bit length
  50. * @flags: field flags
  51. */
  52. struct ale_entry_fld {
  53. u8 start_bit;
  54. u8 num_bits;
  55. u8 flags;
  56. };
  57. enum {
  58. CPSW_ALE_F_STATUS_REG = BIT(0), /* Status register present */
  59. CPSW_ALE_F_HW_AUTOAGING = BIT(1), /* HW auto aging */
  60. CPSW_ALE_F_COUNT
  61. };
  62. /**
  63. * struct cpsw_ale_dev_id - The ALE version/SoC specific configuration
  64. * @dev_id: ALE version/SoC id
  65. * @features: features supported by ALE
  66. * @tbl_entries: number of ALE entries
  67. * @major_ver_mask: mask of ALE Major Version Value in ALE_IDVER reg.
  68. * @nu_switch_ale: NU Switch ALE
  69. * @vlan_entry_tbl: ALE vlan entry fields description tbl
  70. */
  71. struct cpsw_ale_dev_id {
  72. const char *dev_id;
  73. u32 features;
  74. u32 tbl_entries;
  75. u32 major_ver_mask;
  76. bool nu_switch_ale;
  77. const struct ale_entry_fld *vlan_entry_tbl;
  78. };
  79. #define ALE_TABLE_WRITE BIT(31)
  80. #define ALE_TYPE_FREE 0
  81. #define ALE_TYPE_ADDR 1
  82. #define ALE_TYPE_VLAN 2
  83. #define ALE_TYPE_VLAN_ADDR 3
  84. #define ALE_UCAST_PERSISTANT 0
  85. #define ALE_UCAST_UNTOUCHED 1
  86. #define ALE_UCAST_OUI 2
  87. #define ALE_UCAST_TOUCHED 3
  88. #define ALE_TABLE_SIZE_MULTIPLIER 1024
  89. #define ALE_STATUS_SIZE_MASK 0x1f
  90. static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
  91. {
  92. int idx, idx2;
  93. u32 hi_val = 0;
  94. idx = start / 32;
  95. idx2 = (start + bits - 1) / 32;
  96. /* Check if bits to be fetched exceed a word */
  97. if (idx != idx2) {
  98. idx2 = 2 - idx2; /* flip */
  99. hi_val = ale_entry[idx2] << ((idx2 * 32) - start);
  100. }
  101. start -= idx * 32;
  102. idx = 2 - idx; /* flip */
  103. return (hi_val + (ale_entry[idx] >> start)) & BITMASK(bits);
  104. }
  105. static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
  106. u32 value)
  107. {
  108. int idx, idx2;
  109. value &= BITMASK(bits);
  110. idx = start / 32;
  111. idx2 = (start + bits - 1) / 32;
  112. /* Check if bits to be set exceed a word */
  113. if (idx != idx2) {
  114. idx2 = 2 - idx2; /* flip */
  115. ale_entry[idx2] &= ~(BITMASK(bits + start - (idx2 * 32)));
  116. ale_entry[idx2] |= (value >> ((idx2 * 32) - start));
  117. }
  118. start -= idx * 32;
  119. idx = 2 - idx; /* flip */
  120. ale_entry[idx] &= ~(BITMASK(bits) << start);
  121. ale_entry[idx] |= (value << start);
  122. }
  123. #define DEFINE_ALE_FIELD(name, start, bits) \
  124. static inline int cpsw_ale_get_##name(u32 *ale_entry) \
  125. { \
  126. return cpsw_ale_get_field(ale_entry, start, bits); \
  127. } \
  128. static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
  129. { \
  130. cpsw_ale_set_field(ale_entry, start, bits, value); \
  131. }
  132. #define DEFINE_ALE_FIELD1(name, start) \
  133. static inline int cpsw_ale_get_##name(u32 *ale_entry, u32 bits) \
  134. { \
  135. return cpsw_ale_get_field(ale_entry, start, bits); \
  136. } \
  137. static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value, \
  138. u32 bits) \
  139. { \
  140. cpsw_ale_set_field(ale_entry, start, bits, value); \
  141. }
  142. enum {
  143. ALE_ENT_VID_MEMBER_LIST = 0,
  144. ALE_ENT_VID_UNREG_MCAST_MSK,
  145. ALE_ENT_VID_REG_MCAST_MSK,
  146. ALE_ENT_VID_FORCE_UNTAGGED_MSK,
  147. ALE_ENT_VID_UNREG_MCAST_IDX,
  148. ALE_ENT_VID_REG_MCAST_IDX,
  149. ALE_ENT_VID_LAST,
  150. };
  151. #define ALE_FLD_ALLOWED BIT(0)
  152. #define ALE_FLD_SIZE_PORT_MASK_BITS BIT(1)
  153. #define ALE_FLD_SIZE_PORT_NUM_BITS BIT(2)
  154. #define ALE_ENTRY_FLD(id, start, bits) \
  155. [id] = { \
  156. .start_bit = start, \
  157. .num_bits = bits, \
  158. .flags = ALE_FLD_ALLOWED, \
  159. }
  160. #define ALE_ENTRY_FLD_DYN_MSK_SIZE(id, start) \
  161. [id] = { \
  162. .start_bit = start, \
  163. .num_bits = 0, \
  164. .flags = ALE_FLD_ALLOWED | \
  165. ALE_FLD_SIZE_PORT_MASK_BITS, \
  166. }
  167. /* dm814x, am3/am4/am5, k2hk */
  168. static const struct ale_entry_fld vlan_entry_cpsw[ALE_ENT_VID_LAST] = {
  169. ALE_ENTRY_FLD(ALE_ENT_VID_MEMBER_LIST, 0, 3),
  170. ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_MSK, 8, 3),
  171. ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_MSK, 16, 3),
  172. ALE_ENTRY_FLD(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24, 3),
  173. };
  174. /* k2e/k2l, k3 am65/j721e cpsw2g */
  175. static const struct ale_entry_fld vlan_entry_nu[ALE_ENT_VID_LAST] = {
  176. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
  177. ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_IDX, 20, 3),
  178. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
  179. ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_IDX, 44, 3),
  180. };
  181. /* K3 j721e/j7200 cpsw9g/5g, am64x cpsw3g */
  182. static const struct ale_entry_fld vlan_entry_k3_cpswxg[] = {
  183. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
  184. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_UNREG_MCAST_MSK, 12),
  185. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
  186. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_REG_MCAST_MSK, 36),
  187. };
  188. DEFINE_ALE_FIELD(entry_type, 60, 2)
  189. DEFINE_ALE_FIELD(vlan_id, 48, 12)
  190. DEFINE_ALE_FIELD(mcast_state, 62, 2)
  191. DEFINE_ALE_FIELD1(port_mask, 66)
  192. DEFINE_ALE_FIELD(super, 65, 1)
  193. DEFINE_ALE_FIELD(ucast_type, 62, 2)
  194. DEFINE_ALE_FIELD1(port_num, 66)
  195. DEFINE_ALE_FIELD(blocked, 65, 1)
  196. DEFINE_ALE_FIELD(secure, 64, 1)
  197. DEFINE_ALE_FIELD(mcast, 40, 1)
  198. #define NU_VLAN_UNREG_MCAST_IDX 1
  199. static int cpsw_ale_entry_get_fld(struct cpsw_ale *ale,
  200. u32 *ale_entry,
  201. const struct ale_entry_fld *entry_tbl,
  202. int fld_id)
  203. {
  204. const struct ale_entry_fld *entry_fld;
  205. u32 bits;
  206. if (!ale || !ale_entry)
  207. return -EINVAL;
  208. entry_fld = &entry_tbl[fld_id];
  209. if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
  210. dev_err(ale->params.dev, "get: wrong ale fld id %d\n", fld_id);
  211. return -ENOENT;
  212. }
  213. bits = entry_fld->num_bits;
  214. if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
  215. bits = ale->port_mask_bits;
  216. return cpsw_ale_get_field(ale_entry, entry_fld->start_bit, bits);
  217. }
  218. static void cpsw_ale_entry_set_fld(struct cpsw_ale *ale,
  219. u32 *ale_entry,
  220. const struct ale_entry_fld *entry_tbl,
  221. int fld_id,
  222. u32 value)
  223. {
  224. const struct ale_entry_fld *entry_fld;
  225. u32 bits;
  226. if (!ale || !ale_entry)
  227. return;
  228. entry_fld = &entry_tbl[fld_id];
  229. if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
  230. dev_err(ale->params.dev, "set: wrong ale fld id %d\n", fld_id);
  231. return;
  232. }
  233. bits = entry_fld->num_bits;
  234. if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
  235. bits = ale->port_mask_bits;
  236. cpsw_ale_set_field(ale_entry, entry_fld->start_bit, bits, value);
  237. }
  238. static int cpsw_ale_vlan_get_fld(struct cpsw_ale *ale,
  239. u32 *ale_entry,
  240. int fld_id)
  241. {
  242. return cpsw_ale_entry_get_fld(ale, ale_entry,
  243. ale->vlan_entry_tbl, fld_id);
  244. }
  245. static void cpsw_ale_vlan_set_fld(struct cpsw_ale *ale,
  246. u32 *ale_entry,
  247. int fld_id,
  248. u32 value)
  249. {
  250. cpsw_ale_entry_set_fld(ale, ale_entry,
  251. ale->vlan_entry_tbl, fld_id, value);
  252. }
  253. /* The MAC address field in the ALE entry cannot be macroized as above */
  254. static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
  255. {
  256. int i;
  257. for (i = 0; i < 6; i++)
  258. addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
  259. }
  260. static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
  261. {
  262. int i;
  263. for (i = 0; i < 6; i++)
  264. cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
  265. }
  266. static int cpsw_ale_read(struct cpsw_ale *ale, int idx, u32 *ale_entry)
  267. {
  268. int i;
  269. WARN_ON(idx > ale->params.ale_entries);
  270. writel_relaxed(idx, ale->params.ale_regs + ALE_TABLE_CONTROL);
  271. for (i = 0; i < ALE_ENTRY_WORDS; i++)
  272. ale_entry[i] = readl_relaxed(ale->params.ale_regs +
  273. ALE_TABLE + 4 * i);
  274. return idx;
  275. }
  276. static int cpsw_ale_write(struct cpsw_ale *ale, int idx, u32 *ale_entry)
  277. {
  278. int i;
  279. WARN_ON(idx > ale->params.ale_entries);
  280. for (i = 0; i < ALE_ENTRY_WORDS; i++)
  281. writel_relaxed(ale_entry[i], ale->params.ale_regs +
  282. ALE_TABLE + 4 * i);
  283. writel_relaxed(idx | ALE_TABLE_WRITE, ale->params.ale_regs +
  284. ALE_TABLE_CONTROL);
  285. return idx;
  286. }
  287. static int cpsw_ale_match_addr(struct cpsw_ale *ale, const u8 *addr, u16 vid)
  288. {
  289. u32 ale_entry[ALE_ENTRY_WORDS];
  290. int type, idx;
  291. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  292. u8 entry_addr[6];
  293. cpsw_ale_read(ale, idx, ale_entry);
  294. type = cpsw_ale_get_entry_type(ale_entry);
  295. if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
  296. continue;
  297. if (cpsw_ale_get_vlan_id(ale_entry) != vid)
  298. continue;
  299. cpsw_ale_get_addr(ale_entry, entry_addr);
  300. if (ether_addr_equal(entry_addr, addr))
  301. return idx;
  302. }
  303. return -ENOENT;
  304. }
  305. static int cpsw_ale_match_vlan(struct cpsw_ale *ale, u16 vid)
  306. {
  307. u32 ale_entry[ALE_ENTRY_WORDS];
  308. int type, idx;
  309. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  310. cpsw_ale_read(ale, idx, ale_entry);
  311. type = cpsw_ale_get_entry_type(ale_entry);
  312. if (type != ALE_TYPE_VLAN)
  313. continue;
  314. if (cpsw_ale_get_vlan_id(ale_entry) == vid)
  315. return idx;
  316. }
  317. return -ENOENT;
  318. }
  319. static int cpsw_ale_match_free(struct cpsw_ale *ale)
  320. {
  321. u32 ale_entry[ALE_ENTRY_WORDS];
  322. int type, idx;
  323. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  324. cpsw_ale_read(ale, idx, ale_entry);
  325. type = cpsw_ale_get_entry_type(ale_entry);
  326. if (type == ALE_TYPE_FREE)
  327. return idx;
  328. }
  329. return -ENOENT;
  330. }
  331. static int cpsw_ale_find_ageable(struct cpsw_ale *ale)
  332. {
  333. u32 ale_entry[ALE_ENTRY_WORDS];
  334. int type, idx;
  335. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  336. cpsw_ale_read(ale, idx, ale_entry);
  337. type = cpsw_ale_get_entry_type(ale_entry);
  338. if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
  339. continue;
  340. if (cpsw_ale_get_mcast(ale_entry))
  341. continue;
  342. type = cpsw_ale_get_ucast_type(ale_entry);
  343. if (type != ALE_UCAST_PERSISTANT &&
  344. type != ALE_UCAST_OUI)
  345. return idx;
  346. }
  347. return -ENOENT;
  348. }
  349. static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  350. int port_mask)
  351. {
  352. int mask;
  353. mask = cpsw_ale_get_port_mask(ale_entry,
  354. ale->port_mask_bits);
  355. if ((mask & port_mask) == 0)
  356. return; /* ports dont intersect, not interested */
  357. mask &= ~port_mask;
  358. /* free if only remaining port is host port */
  359. if (mask)
  360. cpsw_ale_set_port_mask(ale_entry, mask,
  361. ale->port_mask_bits);
  362. else
  363. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  364. }
  365. int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask, int vid)
  366. {
  367. u32 ale_entry[ALE_ENTRY_WORDS];
  368. int ret, idx;
  369. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  370. cpsw_ale_read(ale, idx, ale_entry);
  371. ret = cpsw_ale_get_entry_type(ale_entry);
  372. if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
  373. continue;
  374. /* if vid passed is -1 then remove all multicast entry from
  375. * the table irrespective of vlan id, if a valid vlan id is
  376. * passed then remove only multicast added to that vlan id.
  377. * if vlan id doesn't match then move on to next entry.
  378. */
  379. if (vid != -1 && cpsw_ale_get_vlan_id(ale_entry) != vid)
  380. continue;
  381. if (cpsw_ale_get_mcast(ale_entry)) {
  382. u8 addr[6];
  383. if (cpsw_ale_get_super(ale_entry))
  384. continue;
  385. cpsw_ale_get_addr(ale_entry, addr);
  386. if (!is_broadcast_ether_addr(addr))
  387. cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
  388. }
  389. cpsw_ale_write(ale, idx, ale_entry);
  390. }
  391. return 0;
  392. }
  393. static inline void cpsw_ale_set_vlan_entry_type(u32 *ale_entry,
  394. int flags, u16 vid)
  395. {
  396. if (flags & ALE_VLAN) {
  397. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN_ADDR);
  398. cpsw_ale_set_vlan_id(ale_entry, vid);
  399. } else {
  400. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
  401. }
  402. }
  403. int cpsw_ale_add_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
  404. int flags, u16 vid)
  405. {
  406. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  407. int idx;
  408. cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
  409. cpsw_ale_set_addr(ale_entry, addr);
  410. cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
  411. cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
  412. cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
  413. cpsw_ale_set_port_num(ale_entry, port, ale->port_num_bits);
  414. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  415. if (idx < 0)
  416. idx = cpsw_ale_match_free(ale);
  417. if (idx < 0)
  418. idx = cpsw_ale_find_ageable(ale);
  419. if (idx < 0)
  420. return -ENOMEM;
  421. cpsw_ale_write(ale, idx, ale_entry);
  422. return 0;
  423. }
  424. int cpsw_ale_del_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
  425. int flags, u16 vid)
  426. {
  427. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  428. int idx;
  429. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  430. if (idx < 0)
  431. return -ENOENT;
  432. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  433. cpsw_ale_write(ale, idx, ale_entry);
  434. return 0;
  435. }
  436. int cpsw_ale_add_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
  437. int flags, u16 vid, int mcast_state)
  438. {
  439. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  440. int idx, mask;
  441. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  442. if (idx >= 0)
  443. cpsw_ale_read(ale, idx, ale_entry);
  444. cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
  445. cpsw_ale_set_addr(ale_entry, addr);
  446. cpsw_ale_set_super(ale_entry, (flags & ALE_SUPER) ? 1 : 0);
  447. cpsw_ale_set_mcast_state(ale_entry, mcast_state);
  448. mask = cpsw_ale_get_port_mask(ale_entry,
  449. ale->port_mask_bits);
  450. port_mask |= mask;
  451. cpsw_ale_set_port_mask(ale_entry, port_mask,
  452. ale->port_mask_bits);
  453. if (idx < 0)
  454. idx = cpsw_ale_match_free(ale);
  455. if (idx < 0)
  456. idx = cpsw_ale_find_ageable(ale);
  457. if (idx < 0)
  458. return -ENOMEM;
  459. cpsw_ale_write(ale, idx, ale_entry);
  460. return 0;
  461. }
  462. int cpsw_ale_del_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
  463. int flags, u16 vid)
  464. {
  465. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  466. int mcast_members = 0;
  467. int idx;
  468. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  469. if (idx < 0)
  470. return -ENOENT;
  471. cpsw_ale_read(ale, idx, ale_entry);
  472. if (port_mask) {
  473. mcast_members = cpsw_ale_get_port_mask(ale_entry,
  474. ale->port_mask_bits);
  475. mcast_members &= ~port_mask;
  476. }
  477. if (mcast_members)
  478. cpsw_ale_set_port_mask(ale_entry, mcast_members,
  479. ale->port_mask_bits);
  480. else
  481. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  482. cpsw_ale_write(ale, idx, ale_entry);
  483. return 0;
  484. }
  485. /* ALE NetCP NU switch specific vlan functions */
  486. static void cpsw_ale_set_vlan_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  487. int reg_mcast, int unreg_mcast)
  488. {
  489. int idx;
  490. /* Set VLAN registered multicast flood mask */
  491. idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
  492. ALE_ENT_VID_REG_MCAST_IDX);
  493. writel(reg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  494. /* Set VLAN unregistered multicast flood mask */
  495. idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
  496. ALE_ENT_VID_UNREG_MCAST_IDX);
  497. writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  498. }
  499. static void cpsw_ale_set_vlan_untag(struct cpsw_ale *ale, u32 *ale_entry,
  500. u16 vid, int untag_mask)
  501. {
  502. cpsw_ale_vlan_set_fld(ale, ale_entry,
  503. ALE_ENT_VID_FORCE_UNTAGGED_MSK,
  504. untag_mask);
  505. if (untag_mask & ALE_PORT_HOST)
  506. bitmap_set(ale->p0_untag_vid_mask, vid, 1);
  507. else
  508. bitmap_clear(ale->p0_untag_vid_mask, vid, 1);
  509. }
  510. int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, int untag,
  511. int reg_mcast, int unreg_mcast)
  512. {
  513. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  514. int idx;
  515. idx = cpsw_ale_match_vlan(ale, vid);
  516. if (idx >= 0)
  517. cpsw_ale_read(ale, idx, ale_entry);
  518. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN);
  519. cpsw_ale_set_vlan_id(ale_entry, vid);
  520. cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
  521. if (!ale->params.nu_switch_ale) {
  522. cpsw_ale_vlan_set_fld(ale, ale_entry,
  523. ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
  524. cpsw_ale_vlan_set_fld(ale, ale_entry,
  525. ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
  526. } else {
  527. cpsw_ale_vlan_set_fld(ale, ale_entry,
  528. ALE_ENT_VID_UNREG_MCAST_IDX,
  529. NU_VLAN_UNREG_MCAST_IDX);
  530. cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast, unreg_mcast);
  531. }
  532. cpsw_ale_vlan_set_fld(ale, ale_entry,
  533. ALE_ENT_VID_MEMBER_LIST, port_mask);
  534. if (idx < 0)
  535. idx = cpsw_ale_match_free(ale);
  536. if (idx < 0)
  537. idx = cpsw_ale_find_ageable(ale);
  538. if (idx < 0)
  539. return -ENOMEM;
  540. cpsw_ale_write(ale, idx, ale_entry);
  541. return 0;
  542. }
  543. static void cpsw_ale_vlan_del_modify_int(struct cpsw_ale *ale, u32 *ale_entry,
  544. u16 vid, int port_mask)
  545. {
  546. int reg_mcast, unreg_mcast;
  547. int members, untag;
  548. members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  549. ALE_ENT_VID_MEMBER_LIST);
  550. members &= ~port_mask;
  551. if (!members) {
  552. cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
  553. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  554. return;
  555. }
  556. untag = cpsw_ale_vlan_get_fld(ale, ale_entry,
  557. ALE_ENT_VID_FORCE_UNTAGGED_MSK);
  558. reg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
  559. ALE_ENT_VID_REG_MCAST_MSK);
  560. unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
  561. ALE_ENT_VID_UNREG_MCAST_MSK);
  562. untag &= members;
  563. reg_mcast &= members;
  564. unreg_mcast &= members;
  565. cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
  566. if (!ale->params.nu_switch_ale) {
  567. cpsw_ale_vlan_set_fld(ale, ale_entry,
  568. ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
  569. cpsw_ale_vlan_set_fld(ale, ale_entry,
  570. ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
  571. } else {
  572. cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast,
  573. unreg_mcast);
  574. }
  575. cpsw_ale_vlan_set_fld(ale, ale_entry,
  576. ALE_ENT_VID_MEMBER_LIST, members);
  577. }
  578. int cpsw_ale_vlan_del_modify(struct cpsw_ale *ale, u16 vid, int port_mask)
  579. {
  580. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  581. int idx;
  582. idx = cpsw_ale_match_vlan(ale, vid);
  583. if (idx < 0)
  584. return -ENOENT;
  585. cpsw_ale_read(ale, idx, ale_entry);
  586. cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
  587. cpsw_ale_write(ale, idx, ale_entry);
  588. return 0;
  589. }
  590. int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask)
  591. {
  592. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  593. int members, idx;
  594. idx = cpsw_ale_match_vlan(ale, vid);
  595. if (idx < 0)
  596. return -ENOENT;
  597. cpsw_ale_read(ale, idx, ale_entry);
  598. /* if !port_mask - force remove VLAN (legacy).
  599. * Check if there are other VLAN members ports
  600. * if no - remove VLAN.
  601. * if yes it means same VLAN was added to >1 port in multi port mode, so
  602. * remove port_mask ports from VLAN ALE entry excluding Host port.
  603. */
  604. members = cpsw_ale_vlan_get_fld(ale, ale_entry, ALE_ENT_VID_MEMBER_LIST);
  605. members &= ~port_mask;
  606. if (!port_mask || !members) {
  607. /* last port or force remove - remove VLAN */
  608. cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
  609. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  610. } else {
  611. port_mask &= ~ALE_PORT_HOST;
  612. cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
  613. }
  614. cpsw_ale_write(ale, idx, ale_entry);
  615. return 0;
  616. }
  617. int cpsw_ale_vlan_add_modify(struct cpsw_ale *ale, u16 vid, int port_mask,
  618. int untag_mask, int reg_mask, int unreg_mask)
  619. {
  620. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  621. int reg_mcast_members, unreg_mcast_members;
  622. int vlan_members, untag_members;
  623. int idx, ret = 0;
  624. idx = cpsw_ale_match_vlan(ale, vid);
  625. if (idx >= 0)
  626. cpsw_ale_read(ale, idx, ale_entry);
  627. vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  628. ALE_ENT_VID_MEMBER_LIST);
  629. reg_mcast_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  630. ALE_ENT_VID_REG_MCAST_MSK);
  631. unreg_mcast_members =
  632. cpsw_ale_vlan_get_fld(ale, ale_entry,
  633. ALE_ENT_VID_UNREG_MCAST_MSK);
  634. untag_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  635. ALE_ENT_VID_FORCE_UNTAGGED_MSK);
  636. vlan_members |= port_mask;
  637. untag_members = (untag_members & ~port_mask) | untag_mask;
  638. reg_mcast_members = (reg_mcast_members & ~port_mask) | reg_mask;
  639. unreg_mcast_members = (unreg_mcast_members & ~port_mask) | unreg_mask;
  640. ret = cpsw_ale_add_vlan(ale, vid, vlan_members, untag_members,
  641. reg_mcast_members, unreg_mcast_members);
  642. if (ret) {
  643. dev_err(ale->params.dev, "Unable to add vlan\n");
  644. return ret;
  645. }
  646. dev_dbg(ale->params.dev, "port mask 0x%x untag 0x%x\n", vlan_members,
  647. untag_mask);
  648. return ret;
  649. }
  650. void cpsw_ale_set_unreg_mcast(struct cpsw_ale *ale, int unreg_mcast_mask,
  651. bool add)
  652. {
  653. u32 ale_entry[ALE_ENTRY_WORDS];
  654. int unreg_members = 0;
  655. int type, idx;
  656. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  657. cpsw_ale_read(ale, idx, ale_entry);
  658. type = cpsw_ale_get_entry_type(ale_entry);
  659. if (type != ALE_TYPE_VLAN)
  660. continue;
  661. unreg_members =
  662. cpsw_ale_vlan_get_fld(ale, ale_entry,
  663. ALE_ENT_VID_UNREG_MCAST_MSK);
  664. if (add)
  665. unreg_members |= unreg_mcast_mask;
  666. else
  667. unreg_members &= ~unreg_mcast_mask;
  668. cpsw_ale_vlan_set_fld(ale, ale_entry,
  669. ALE_ENT_VID_UNREG_MCAST_MSK,
  670. unreg_members);
  671. cpsw_ale_write(ale, idx, ale_entry);
  672. }
  673. }
  674. static void cpsw_ale_vlan_set_unreg_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  675. int allmulti)
  676. {
  677. int unreg_mcast;
  678. unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
  679. ALE_ENT_VID_UNREG_MCAST_MSK);
  680. if (allmulti)
  681. unreg_mcast |= ALE_PORT_HOST;
  682. else
  683. unreg_mcast &= ~ALE_PORT_HOST;
  684. cpsw_ale_vlan_set_fld(ale, ale_entry,
  685. ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
  686. }
  687. static void
  688. cpsw_ale_vlan_set_unreg_mcast_idx(struct cpsw_ale *ale, u32 *ale_entry,
  689. int allmulti)
  690. {
  691. int unreg_mcast;
  692. int idx;
  693. idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
  694. ALE_ENT_VID_UNREG_MCAST_IDX);
  695. unreg_mcast = readl(ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  696. if (allmulti)
  697. unreg_mcast |= ALE_PORT_HOST;
  698. else
  699. unreg_mcast &= ~ALE_PORT_HOST;
  700. writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  701. }
  702. void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti, int port)
  703. {
  704. u32 ale_entry[ALE_ENTRY_WORDS];
  705. int type, idx;
  706. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  707. int vlan_members;
  708. cpsw_ale_read(ale, idx, ale_entry);
  709. type = cpsw_ale_get_entry_type(ale_entry);
  710. if (type != ALE_TYPE_VLAN)
  711. continue;
  712. vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  713. ALE_ENT_VID_MEMBER_LIST);
  714. if (port != -1 && !(vlan_members & BIT(port)))
  715. continue;
  716. if (!ale->params.nu_switch_ale)
  717. cpsw_ale_vlan_set_unreg_mcast(ale, ale_entry, allmulti);
  718. else
  719. cpsw_ale_vlan_set_unreg_mcast_idx(ale, ale_entry,
  720. allmulti);
  721. cpsw_ale_write(ale, idx, ale_entry);
  722. }
  723. }
  724. struct ale_control_info {
  725. const char *name;
  726. int offset, port_offset;
  727. int shift, port_shift;
  728. int bits;
  729. };
  730. static struct ale_control_info ale_controls[ALE_NUM_CONTROLS] = {
  731. [ALE_ENABLE] = {
  732. .name = "enable",
  733. .offset = ALE_CONTROL,
  734. .port_offset = 0,
  735. .shift = 31,
  736. .port_shift = 0,
  737. .bits = 1,
  738. },
  739. [ALE_CLEAR] = {
  740. .name = "clear",
  741. .offset = ALE_CONTROL,
  742. .port_offset = 0,
  743. .shift = 30,
  744. .port_shift = 0,
  745. .bits = 1,
  746. },
  747. [ALE_AGEOUT] = {
  748. .name = "ageout",
  749. .offset = ALE_CONTROL,
  750. .port_offset = 0,
  751. .shift = 29,
  752. .port_shift = 0,
  753. .bits = 1,
  754. },
  755. [ALE_P0_UNI_FLOOD] = {
  756. .name = "port0_unicast_flood",
  757. .offset = ALE_CONTROL,
  758. .port_offset = 0,
  759. .shift = 8,
  760. .port_shift = 0,
  761. .bits = 1,
  762. },
  763. [ALE_VLAN_NOLEARN] = {
  764. .name = "vlan_nolearn",
  765. .offset = ALE_CONTROL,
  766. .port_offset = 0,
  767. .shift = 7,
  768. .port_shift = 0,
  769. .bits = 1,
  770. },
  771. [ALE_NO_PORT_VLAN] = {
  772. .name = "no_port_vlan",
  773. .offset = ALE_CONTROL,
  774. .port_offset = 0,
  775. .shift = 6,
  776. .port_shift = 0,
  777. .bits = 1,
  778. },
  779. [ALE_OUI_DENY] = {
  780. .name = "oui_deny",
  781. .offset = ALE_CONTROL,
  782. .port_offset = 0,
  783. .shift = 5,
  784. .port_shift = 0,
  785. .bits = 1,
  786. },
  787. [ALE_BYPASS] = {
  788. .name = "bypass",
  789. .offset = ALE_CONTROL,
  790. .port_offset = 0,
  791. .shift = 4,
  792. .port_shift = 0,
  793. .bits = 1,
  794. },
  795. [ALE_RATE_LIMIT_TX] = {
  796. .name = "rate_limit_tx",
  797. .offset = ALE_CONTROL,
  798. .port_offset = 0,
  799. .shift = 3,
  800. .port_shift = 0,
  801. .bits = 1,
  802. },
  803. [ALE_VLAN_AWARE] = {
  804. .name = "vlan_aware",
  805. .offset = ALE_CONTROL,
  806. .port_offset = 0,
  807. .shift = 2,
  808. .port_shift = 0,
  809. .bits = 1,
  810. },
  811. [ALE_AUTH_ENABLE] = {
  812. .name = "auth_enable",
  813. .offset = ALE_CONTROL,
  814. .port_offset = 0,
  815. .shift = 1,
  816. .port_shift = 0,
  817. .bits = 1,
  818. },
  819. [ALE_RATE_LIMIT] = {
  820. .name = "rate_limit",
  821. .offset = ALE_CONTROL,
  822. .port_offset = 0,
  823. .shift = 0,
  824. .port_shift = 0,
  825. .bits = 1,
  826. },
  827. [ALE_PORT_STATE] = {
  828. .name = "port_state",
  829. .offset = ALE_PORTCTL,
  830. .port_offset = 4,
  831. .shift = 0,
  832. .port_shift = 0,
  833. .bits = 2,
  834. },
  835. [ALE_PORT_DROP_UNTAGGED] = {
  836. .name = "drop_untagged",
  837. .offset = ALE_PORTCTL,
  838. .port_offset = 4,
  839. .shift = 2,
  840. .port_shift = 0,
  841. .bits = 1,
  842. },
  843. [ALE_PORT_DROP_UNKNOWN_VLAN] = {
  844. .name = "drop_unknown",
  845. .offset = ALE_PORTCTL,
  846. .port_offset = 4,
  847. .shift = 3,
  848. .port_shift = 0,
  849. .bits = 1,
  850. },
  851. [ALE_PORT_NOLEARN] = {
  852. .name = "nolearn",
  853. .offset = ALE_PORTCTL,
  854. .port_offset = 4,
  855. .shift = 4,
  856. .port_shift = 0,
  857. .bits = 1,
  858. },
  859. [ALE_PORT_NO_SA_UPDATE] = {
  860. .name = "no_source_update",
  861. .offset = ALE_PORTCTL,
  862. .port_offset = 4,
  863. .shift = 5,
  864. .port_shift = 0,
  865. .bits = 1,
  866. },
  867. [ALE_PORT_MACONLY] = {
  868. .name = "mac_only_port_mode",
  869. .offset = ALE_PORTCTL,
  870. .port_offset = 4,
  871. .shift = 11,
  872. .port_shift = 0,
  873. .bits = 1,
  874. },
  875. [ALE_PORT_MACONLY_CAF] = {
  876. .name = "mac_only_port_caf",
  877. .offset = ALE_PORTCTL,
  878. .port_offset = 4,
  879. .shift = 13,
  880. .port_shift = 0,
  881. .bits = 1,
  882. },
  883. [ALE_PORT_MCAST_LIMIT] = {
  884. .name = "mcast_limit",
  885. .offset = ALE_PORTCTL,
  886. .port_offset = 4,
  887. .shift = 16,
  888. .port_shift = 0,
  889. .bits = 8,
  890. },
  891. [ALE_PORT_BCAST_LIMIT] = {
  892. .name = "bcast_limit",
  893. .offset = ALE_PORTCTL,
  894. .port_offset = 4,
  895. .shift = 24,
  896. .port_shift = 0,
  897. .bits = 8,
  898. },
  899. [ALE_PORT_UNKNOWN_VLAN_MEMBER] = {
  900. .name = "unknown_vlan_member",
  901. .offset = ALE_UNKNOWNVLAN,
  902. .port_offset = 0,
  903. .shift = 0,
  904. .port_shift = 0,
  905. .bits = 6,
  906. },
  907. [ALE_PORT_UNKNOWN_MCAST_FLOOD] = {
  908. .name = "unknown_mcast_flood",
  909. .offset = ALE_UNKNOWNVLAN,
  910. .port_offset = 0,
  911. .shift = 8,
  912. .port_shift = 0,
  913. .bits = 6,
  914. },
  915. [ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = {
  916. .name = "unknown_reg_flood",
  917. .offset = ALE_UNKNOWNVLAN,
  918. .port_offset = 0,
  919. .shift = 16,
  920. .port_shift = 0,
  921. .bits = 6,
  922. },
  923. [ALE_PORT_UNTAGGED_EGRESS] = {
  924. .name = "untagged_egress",
  925. .offset = ALE_UNKNOWNVLAN,
  926. .port_offset = 0,
  927. .shift = 24,
  928. .port_shift = 0,
  929. .bits = 6,
  930. },
  931. [ALE_DEFAULT_THREAD_ID] = {
  932. .name = "default_thread_id",
  933. .offset = AM65_CPSW_ALE_THREAD_DEF_REG,
  934. .port_offset = 0,
  935. .shift = 0,
  936. .port_shift = 0,
  937. .bits = 6,
  938. },
  939. [ALE_DEFAULT_THREAD_ENABLE] = {
  940. .name = "default_thread_id_enable",
  941. .offset = AM65_CPSW_ALE_THREAD_DEF_REG,
  942. .port_offset = 0,
  943. .shift = 15,
  944. .port_shift = 0,
  945. .bits = 1,
  946. },
  947. };
  948. int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
  949. int value)
  950. {
  951. const struct ale_control_info *info;
  952. int offset, shift;
  953. u32 tmp, mask;
  954. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  955. return -EINVAL;
  956. info = &ale_controls[control];
  957. if (info->port_offset == 0 && info->port_shift == 0)
  958. port = 0; /* global, port is a dont care */
  959. if (port < 0 || port >= ale->params.ale_ports)
  960. return -EINVAL;
  961. mask = BITMASK(info->bits);
  962. if (value & ~mask)
  963. return -EINVAL;
  964. offset = info->offset + (port * info->port_offset);
  965. shift = info->shift + (port * info->port_shift);
  966. tmp = readl_relaxed(ale->params.ale_regs + offset);
  967. tmp = (tmp & ~(mask << shift)) | (value << shift);
  968. writel_relaxed(tmp, ale->params.ale_regs + offset);
  969. return 0;
  970. }
  971. int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
  972. {
  973. const struct ale_control_info *info;
  974. int offset, shift;
  975. u32 tmp;
  976. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  977. return -EINVAL;
  978. info = &ale_controls[control];
  979. if (info->port_offset == 0 && info->port_shift == 0)
  980. port = 0; /* global, port is a dont care */
  981. if (port < 0 || port >= ale->params.ale_ports)
  982. return -EINVAL;
  983. offset = info->offset + (port * info->port_offset);
  984. shift = info->shift + (port * info->port_shift);
  985. tmp = readl_relaxed(ale->params.ale_regs + offset) >> shift;
  986. return tmp & BITMASK(info->bits);
  987. }
  988. int cpsw_ale_rx_ratelimit_mc(struct cpsw_ale *ale, int port, unsigned int ratelimit_pps)
  989. {
  990. int val = ratelimit_pps / ALE_RATE_LIMIT_MIN_PPS;
  991. u32 remainder = ratelimit_pps % ALE_RATE_LIMIT_MIN_PPS;
  992. if (ratelimit_pps && !val) {
  993. dev_err(ale->params.dev, "ALE MC port:%d ratelimit min value 1000pps\n", port);
  994. return -EINVAL;
  995. }
  996. if (remainder)
  997. dev_info(ale->params.dev, "ALE port:%d MC ratelimit set to %dpps (requested %d)\n",
  998. port, ratelimit_pps - remainder, ratelimit_pps);
  999. cpsw_ale_control_set(ale, port, ALE_PORT_MCAST_LIMIT, val);
  1000. dev_dbg(ale->params.dev, "ALE port:%d MC ratelimit set %d\n",
  1001. port, val * ALE_RATE_LIMIT_MIN_PPS);
  1002. return 0;
  1003. }
  1004. int cpsw_ale_rx_ratelimit_bc(struct cpsw_ale *ale, int port, unsigned int ratelimit_pps)
  1005. {
  1006. int val = ratelimit_pps / ALE_RATE_LIMIT_MIN_PPS;
  1007. u32 remainder = ratelimit_pps % ALE_RATE_LIMIT_MIN_PPS;
  1008. if (ratelimit_pps && !val) {
  1009. dev_err(ale->params.dev, "ALE port:%d BC ratelimit min value 1000pps\n", port);
  1010. return -EINVAL;
  1011. }
  1012. if (remainder)
  1013. dev_info(ale->params.dev, "ALE port:%d BC ratelimit set to %dpps (requested %d)\n",
  1014. port, ratelimit_pps - remainder, ratelimit_pps);
  1015. cpsw_ale_control_set(ale, port, ALE_PORT_BCAST_LIMIT, val);
  1016. dev_dbg(ale->params.dev, "ALE port:%d BC ratelimit set %d\n",
  1017. port, val * ALE_RATE_LIMIT_MIN_PPS);
  1018. return 0;
  1019. }
  1020. static void cpsw_ale_timer(struct timer_list *t)
  1021. {
  1022. struct cpsw_ale *ale = from_timer(ale, t, timer);
  1023. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  1024. if (ale->ageout) {
  1025. ale->timer.expires = jiffies + ale->ageout;
  1026. add_timer(&ale->timer);
  1027. }
  1028. }
  1029. static void cpsw_ale_hw_aging_timer_start(struct cpsw_ale *ale)
  1030. {
  1031. u32 aging_timer;
  1032. aging_timer = ale->params.bus_freq / 1000000;
  1033. aging_timer *= ale->params.ale_ageout;
  1034. if (aging_timer & ~ALE_AGING_TIMER_MASK) {
  1035. aging_timer = ALE_AGING_TIMER_MASK;
  1036. dev_warn(ale->params.dev,
  1037. "ALE aging timer overflow, set to max\n");
  1038. }
  1039. writel(aging_timer, ale->params.ale_regs + ALE_AGING_TIMER);
  1040. }
  1041. static void cpsw_ale_hw_aging_timer_stop(struct cpsw_ale *ale)
  1042. {
  1043. writel(0, ale->params.ale_regs + ALE_AGING_TIMER);
  1044. }
  1045. static void cpsw_ale_aging_start(struct cpsw_ale *ale)
  1046. {
  1047. if (!ale->params.ale_ageout)
  1048. return;
  1049. if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
  1050. cpsw_ale_hw_aging_timer_start(ale);
  1051. return;
  1052. }
  1053. timer_setup(&ale->timer, cpsw_ale_timer, 0);
  1054. ale->timer.expires = jiffies + ale->ageout;
  1055. add_timer(&ale->timer);
  1056. }
  1057. static void cpsw_ale_aging_stop(struct cpsw_ale *ale)
  1058. {
  1059. if (!ale->params.ale_ageout)
  1060. return;
  1061. if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
  1062. cpsw_ale_hw_aging_timer_stop(ale);
  1063. return;
  1064. }
  1065. del_timer_sync(&ale->timer);
  1066. }
  1067. void cpsw_ale_start(struct cpsw_ale *ale)
  1068. {
  1069. unsigned long ale_prescale;
  1070. /* configure Broadcast and Multicast Rate Limit
  1071. * number_of_packets = (Fclk / ALE_PRESCALE) * port.BCAST/MCAST_LIMIT
  1072. * ALE_PRESCALE width is 19bit and min value 0x10
  1073. * port.BCAST/MCAST_LIMIT is 8bit
  1074. *
  1075. * For multi port configuration support the ALE_PRESCALE is configured to 1ms interval,
  1076. * which allows to configure port.BCAST/MCAST_LIMIT per port and achieve:
  1077. * min number_of_packets = 1000 when port.BCAST/MCAST_LIMIT = 1
  1078. * max number_of_packets = 1000 * 255 = 255000 when port.BCAST/MCAST_LIMIT = 0xFF
  1079. */
  1080. ale_prescale = ale->params.bus_freq / ALE_RATE_LIMIT_MIN_PPS;
  1081. writel((u32)ale_prescale, ale->params.ale_regs + ALE_PRESCALE);
  1082. /* Allow MC/BC rate limiting globally.
  1083. * The actual Rate Limit cfg enabled per-port by port.BCAST/MCAST_LIMIT
  1084. */
  1085. cpsw_ale_control_set(ale, 0, ALE_RATE_LIMIT, 1);
  1086. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
  1087. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  1088. cpsw_ale_aging_start(ale);
  1089. }
  1090. void cpsw_ale_stop(struct cpsw_ale *ale)
  1091. {
  1092. cpsw_ale_aging_stop(ale);
  1093. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  1094. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
  1095. }
  1096. static const struct cpsw_ale_dev_id cpsw_ale_id_match[] = {
  1097. {
  1098. /* am3/4/5, dra7. dm814x, 66ak2hk-gbe */
  1099. .dev_id = "cpsw",
  1100. .tbl_entries = 1024,
  1101. .major_ver_mask = 0xff,
  1102. .vlan_entry_tbl = vlan_entry_cpsw,
  1103. },
  1104. {
  1105. /* 66ak2h_xgbe */
  1106. .dev_id = "66ak2h-xgbe",
  1107. .tbl_entries = 2048,
  1108. .major_ver_mask = 0xff,
  1109. .vlan_entry_tbl = vlan_entry_cpsw,
  1110. },
  1111. {
  1112. .dev_id = "66ak2el",
  1113. .features = CPSW_ALE_F_STATUS_REG,
  1114. .major_ver_mask = 0x7,
  1115. .nu_switch_ale = true,
  1116. .vlan_entry_tbl = vlan_entry_nu,
  1117. },
  1118. {
  1119. .dev_id = "66ak2g",
  1120. .features = CPSW_ALE_F_STATUS_REG,
  1121. .tbl_entries = 64,
  1122. .major_ver_mask = 0x7,
  1123. .nu_switch_ale = true,
  1124. .vlan_entry_tbl = vlan_entry_nu,
  1125. },
  1126. {
  1127. .dev_id = "am65x-cpsw2g",
  1128. .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
  1129. .tbl_entries = 64,
  1130. .major_ver_mask = 0x7,
  1131. .nu_switch_ale = true,
  1132. .vlan_entry_tbl = vlan_entry_nu,
  1133. },
  1134. {
  1135. .dev_id = "j721e-cpswxg",
  1136. .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
  1137. .major_ver_mask = 0x7,
  1138. .vlan_entry_tbl = vlan_entry_k3_cpswxg,
  1139. },
  1140. {
  1141. .dev_id = "am64-cpswxg",
  1142. .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
  1143. .major_ver_mask = 0x7,
  1144. .vlan_entry_tbl = vlan_entry_k3_cpswxg,
  1145. .tbl_entries = 512,
  1146. },
  1147. { },
  1148. };
  1149. static const struct
  1150. cpsw_ale_dev_id *cpsw_ale_match_id(const struct cpsw_ale_dev_id *id,
  1151. const char *dev_id)
  1152. {
  1153. if (!dev_id)
  1154. return NULL;
  1155. while (id->dev_id) {
  1156. if (strcmp(dev_id, id->dev_id) == 0)
  1157. return id;
  1158. id++;
  1159. }
  1160. return NULL;
  1161. }
  1162. struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
  1163. {
  1164. const struct cpsw_ale_dev_id *ale_dev_id;
  1165. struct cpsw_ale *ale;
  1166. u32 rev, ale_entries;
  1167. ale_dev_id = cpsw_ale_match_id(cpsw_ale_id_match, params->dev_id);
  1168. if (!ale_dev_id)
  1169. return ERR_PTR(-EINVAL);
  1170. params->ale_entries = ale_dev_id->tbl_entries;
  1171. params->major_ver_mask = ale_dev_id->major_ver_mask;
  1172. params->nu_switch_ale = ale_dev_id->nu_switch_ale;
  1173. ale = devm_kzalloc(params->dev, sizeof(*ale), GFP_KERNEL);
  1174. if (!ale)
  1175. return ERR_PTR(-ENOMEM);
  1176. ale->p0_untag_vid_mask = devm_bitmap_zalloc(params->dev, VLAN_N_VID,
  1177. GFP_KERNEL);
  1178. if (!ale->p0_untag_vid_mask)
  1179. return ERR_PTR(-ENOMEM);
  1180. ale->params = *params;
  1181. ale->ageout = ale->params.ale_ageout * HZ;
  1182. ale->features = ale_dev_id->features;
  1183. ale->vlan_entry_tbl = ale_dev_id->vlan_entry_tbl;
  1184. rev = readl_relaxed(ale->params.ale_regs + ALE_IDVER);
  1185. ale->version =
  1186. (ALE_VERSION_MAJOR(rev, ale->params.major_ver_mask) << 8) |
  1187. ALE_VERSION_MINOR(rev);
  1188. dev_info(ale->params.dev, "initialized cpsw ale version %d.%d\n",
  1189. ALE_VERSION_MAJOR(rev, ale->params.major_ver_mask),
  1190. ALE_VERSION_MINOR(rev));
  1191. if (ale->features & CPSW_ALE_F_STATUS_REG &&
  1192. !ale->params.ale_entries) {
  1193. ale_entries =
  1194. readl_relaxed(ale->params.ale_regs + ALE_STATUS) &
  1195. ALE_STATUS_SIZE_MASK;
  1196. /* ALE available on newer NetCP switches has introduced
  1197. * a register, ALE_STATUS, to indicate the size of ALE
  1198. * table which shows the size as a multiple of 1024 entries.
  1199. * For these, params.ale_entries will be set to zero. So
  1200. * read the register and update the value of ale_entries.
  1201. * return error if ale_entries is zero in ALE_STATUS.
  1202. */
  1203. if (!ale_entries)
  1204. return ERR_PTR(-EINVAL);
  1205. ale_entries *= ALE_TABLE_SIZE_MULTIPLIER;
  1206. ale->params.ale_entries = ale_entries;
  1207. }
  1208. dev_info(ale->params.dev,
  1209. "ALE Table size %ld\n", ale->params.ale_entries);
  1210. /* set default bits for existing h/w */
  1211. ale->port_mask_bits = ale->params.ale_ports;
  1212. ale->port_num_bits = order_base_2(ale->params.ale_ports);
  1213. ale->vlan_field_bits = ale->params.ale_ports;
  1214. /* Set defaults override for ALE on NetCP NU switch and for version
  1215. * 1R3
  1216. */
  1217. if (ale->params.nu_switch_ale) {
  1218. /* Separate registers for unknown vlan configuration.
  1219. * Also there are N bits, where N is number of ale
  1220. * ports and shift value should be 0
  1221. */
  1222. ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].bits =
  1223. ale->params.ale_ports;
  1224. ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].offset =
  1225. ALE_UNKNOWNVLAN_MEMBER;
  1226. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].bits =
  1227. ale->params.ale_ports;
  1228. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].shift = 0;
  1229. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].offset =
  1230. ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD;
  1231. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].bits =
  1232. ale->params.ale_ports;
  1233. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].shift = 0;
  1234. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].offset =
  1235. ALE_UNKNOWNVLAN_REG_MCAST_FLOOD;
  1236. ale_controls[ALE_PORT_UNTAGGED_EGRESS].bits =
  1237. ale->params.ale_ports;
  1238. ale_controls[ALE_PORT_UNTAGGED_EGRESS].shift = 0;
  1239. ale_controls[ALE_PORT_UNTAGGED_EGRESS].offset =
  1240. ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS;
  1241. }
  1242. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  1243. return ale;
  1244. }
  1245. void cpsw_ale_dump(struct cpsw_ale *ale, u32 *data)
  1246. {
  1247. int i;
  1248. for (i = 0; i < ale->params.ale_entries; i++) {
  1249. cpsw_ale_read(ale, i, data);
  1250. data += ALE_ENTRY_WORDS;
  1251. }
  1252. }
  1253. u32 cpsw_ale_get_num_entries(struct cpsw_ale *ale)
  1254. {
  1255. return ale ? ale->params.ale_entries : 0;
  1256. }