cpsw_ale.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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/regmap.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/slab.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/stat.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/etherdevice.h>
  21. #include "cpsw_ale.h"
  22. #define BITMASK(bits) (BIT(bits) - 1)
  23. /* ALE Registers */
  24. #define ALE_IDVER 0x00
  25. #define ALE_STATUS 0x04
  26. #define ALE_CONTROL 0x08
  27. #define ALE_PRESCALE 0x10
  28. #define ALE_AGING_TIMER 0x14
  29. #define ALE_UNKNOWNVLAN 0x18
  30. #define ALE_TABLE_CONTROL 0x20
  31. #define ALE_TABLE 0x34
  32. #define ALE_PORTCTL 0x40
  33. /* ALE NetCP NU switch specific Registers */
  34. #define ALE_UNKNOWNVLAN_MEMBER 0x90
  35. #define ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD 0x94
  36. #define ALE_UNKNOWNVLAN_REG_MCAST_FLOOD 0x98
  37. #define ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS 0x9C
  38. #define ALE_VLAN_MASK_MUX(reg) (0xc0 + (0x4 * (reg)))
  39. #define ALE_POLICER_PORT_OUI 0x100
  40. #define ALE_POLICER_DA_SA 0x104
  41. #define ALE_POLICER_VLAN 0x108
  42. #define ALE_POLICER_ETHERTYPE_IPSA 0x10c
  43. #define ALE_POLICER_IPDA 0x110
  44. #define ALE_POLICER_PIR 0x118
  45. #define ALE_POLICER_CIR 0x11c
  46. #define ALE_POLICER_TBL_CTL 0x120
  47. #define ALE_POLICER_CTL 0x124
  48. #define ALE_POLICER_TEST_CTL 0x128
  49. #define ALE_POLICER_HIT_STATUS 0x12c
  50. #define ALE_THREAD_DEF 0x134
  51. #define ALE_THREAD_CTL 0x138
  52. #define ALE_THREAD_VAL 0x13c
  53. #define ALE_POLICER_TBL_WRITE_ENABLE BIT(31)
  54. #define ALE_POLICER_TBL_INDEX_MASK GENMASK(4, 0)
  55. #define AM65_CPSW_ALE_THREAD_DEF_REG 0x134
  56. /* ALE_AGING_TIMER */
  57. #define ALE_AGING_TIMER_MASK GENMASK(23, 0)
  58. #define ALE_RATE_LIMIT_MIN_PPS 1000
  59. /**
  60. * struct ale_entry_fld - The ALE tbl entry field description
  61. * @start_bit: field start bit
  62. * @num_bits: field bit length
  63. * @flags: field flags
  64. */
  65. struct ale_entry_fld {
  66. u8 start_bit;
  67. u8 num_bits;
  68. u8 flags;
  69. };
  70. enum {
  71. CPSW_ALE_F_STATUS_REG = BIT(0), /* Status register present */
  72. CPSW_ALE_F_HW_AUTOAGING = BIT(1), /* HW auto aging */
  73. CPSW_ALE_F_COUNT
  74. };
  75. /**
  76. * struct cpsw_ale_dev_id - The ALE version/SoC specific configuration
  77. * @dev_id: ALE version/SoC id
  78. * @features: features supported by ALE
  79. * @tbl_entries: number of ALE entries
  80. * @reg_fields: pointer to array of register field configuration
  81. * @num_fields: number of fields in the reg_fields array
  82. * @nu_switch_ale: NU Switch ALE
  83. * @vlan_entry_tbl: ALE vlan entry fields description tbl
  84. */
  85. struct cpsw_ale_dev_id {
  86. const char *dev_id;
  87. u32 features;
  88. u32 tbl_entries;
  89. const struct reg_field *reg_fields;
  90. int num_fields;
  91. bool nu_switch_ale;
  92. const struct ale_entry_fld *vlan_entry_tbl;
  93. };
  94. #define ALE_TABLE_WRITE BIT(31)
  95. #define ALE_TYPE_FREE 0
  96. #define ALE_TYPE_ADDR 1
  97. #define ALE_TYPE_VLAN 2
  98. #define ALE_TYPE_VLAN_ADDR 3
  99. #define ALE_UCAST_PERSISTANT 0
  100. #define ALE_UCAST_UNTOUCHED 1
  101. #define ALE_UCAST_OUI 2
  102. #define ALE_UCAST_TOUCHED 3
  103. #define ALE_TABLE_SIZE_MULTIPLIER 1024
  104. #define ALE_POLICER_SIZE_MULTIPLIER 8
  105. static inline int cpsw_ale_get_field(u32 *ale_entry, u32 start, u32 bits)
  106. {
  107. int idx, idx2, index;
  108. u32 hi_val = 0;
  109. idx = start / 32;
  110. idx2 = (start + bits - 1) / 32;
  111. /* Check if bits to be fetched exceed a word */
  112. if (idx != idx2) {
  113. index = 2 - idx2; /* flip */
  114. hi_val = ale_entry[index] << ((idx2 * 32) - start);
  115. }
  116. start -= idx * 32;
  117. idx = 2 - idx; /* flip */
  118. return (hi_val + (ale_entry[idx] >> start)) & BITMASK(bits);
  119. }
  120. static inline void cpsw_ale_set_field(u32 *ale_entry, u32 start, u32 bits,
  121. u32 value)
  122. {
  123. int idx, idx2, index;
  124. value &= BITMASK(bits);
  125. idx = start / 32;
  126. idx2 = (start + bits - 1) / 32;
  127. /* Check if bits to be set exceed a word */
  128. if (idx != idx2) {
  129. index = 2 - idx2; /* flip */
  130. ale_entry[index] &= ~(BITMASK(bits + start - (idx2 * 32)));
  131. ale_entry[index] |= (value >> ((idx2 * 32) - start));
  132. }
  133. start -= idx * 32;
  134. idx = 2 - idx; /* flip */
  135. ale_entry[idx] &= ~(BITMASK(bits) << start);
  136. ale_entry[idx] |= (value << start);
  137. }
  138. #define DEFINE_ALE_FIELD_GET(name, start, bits) \
  139. static inline int cpsw_ale_get_##name(u32 *ale_entry) \
  140. { \
  141. return cpsw_ale_get_field(ale_entry, start, bits); \
  142. }
  143. #define DEFINE_ALE_FIELD_SET(name, start, bits) \
  144. static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value) \
  145. { \
  146. cpsw_ale_set_field(ale_entry, start, bits, value); \
  147. }
  148. #define DEFINE_ALE_FIELD(name, start, bits) \
  149. DEFINE_ALE_FIELD_GET(name, start, bits) \
  150. DEFINE_ALE_FIELD_SET(name, start, bits)
  151. #define DEFINE_ALE_FIELD1_GET(name, start) \
  152. static inline int cpsw_ale_get_##name(u32 *ale_entry, u32 bits) \
  153. { \
  154. return cpsw_ale_get_field(ale_entry, start, bits); \
  155. }
  156. #define DEFINE_ALE_FIELD1_SET(name, start) \
  157. static inline void cpsw_ale_set_##name(u32 *ale_entry, u32 value, \
  158. u32 bits) \
  159. { \
  160. cpsw_ale_set_field(ale_entry, start, bits, value); \
  161. }
  162. #define DEFINE_ALE_FIELD1(name, start) \
  163. DEFINE_ALE_FIELD1_GET(name, start) \
  164. DEFINE_ALE_FIELD1_SET(name, start)
  165. enum {
  166. ALE_ENT_VID_MEMBER_LIST = 0,
  167. ALE_ENT_VID_UNREG_MCAST_MSK,
  168. ALE_ENT_VID_REG_MCAST_MSK,
  169. ALE_ENT_VID_FORCE_UNTAGGED_MSK,
  170. ALE_ENT_VID_UNREG_MCAST_IDX,
  171. ALE_ENT_VID_REG_MCAST_IDX,
  172. ALE_ENT_VID_LAST,
  173. };
  174. #define ALE_FLD_ALLOWED BIT(0)
  175. #define ALE_FLD_SIZE_PORT_MASK_BITS BIT(1)
  176. #define ALE_FLD_SIZE_PORT_NUM_BITS BIT(2)
  177. #define ALE_ENTRY_FLD(id, start, bits) \
  178. [id] = { \
  179. .start_bit = start, \
  180. .num_bits = bits, \
  181. .flags = ALE_FLD_ALLOWED, \
  182. }
  183. #define ALE_ENTRY_FLD_DYN_MSK_SIZE(id, start) \
  184. [id] = { \
  185. .start_bit = start, \
  186. .num_bits = 0, \
  187. .flags = ALE_FLD_ALLOWED | \
  188. ALE_FLD_SIZE_PORT_MASK_BITS, \
  189. }
  190. /* dm814x, am3/am4/am5, k2hk */
  191. static const struct ale_entry_fld vlan_entry_cpsw[ALE_ENT_VID_LAST] = {
  192. ALE_ENTRY_FLD(ALE_ENT_VID_MEMBER_LIST, 0, 3),
  193. ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_MSK, 8, 3),
  194. ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_MSK, 16, 3),
  195. ALE_ENTRY_FLD(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24, 3),
  196. };
  197. /* k2e/k2l, k3 am65/j721e cpsw2g */
  198. static const struct ale_entry_fld vlan_entry_nu[ALE_ENT_VID_LAST] = {
  199. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
  200. ALE_ENTRY_FLD(ALE_ENT_VID_UNREG_MCAST_IDX, 20, 3),
  201. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
  202. ALE_ENTRY_FLD(ALE_ENT_VID_REG_MCAST_IDX, 44, 3),
  203. };
  204. /* K3 j721e/j7200 cpsw9g/5g, am64x cpsw3g */
  205. static const struct ale_entry_fld vlan_entry_k3_cpswxg[] = {
  206. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_MEMBER_LIST, 0),
  207. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_UNREG_MCAST_MSK, 12),
  208. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_FORCE_UNTAGGED_MSK, 24),
  209. ALE_ENTRY_FLD_DYN_MSK_SIZE(ALE_ENT_VID_REG_MCAST_MSK, 36),
  210. };
  211. DEFINE_ALE_FIELD(entry_type, 60, 2)
  212. DEFINE_ALE_FIELD(vlan_id, 48, 12)
  213. DEFINE_ALE_FIELD_SET(mcast_state, 62, 2)
  214. DEFINE_ALE_FIELD1(port_mask, 66)
  215. DEFINE_ALE_FIELD(super, 65, 1)
  216. DEFINE_ALE_FIELD(ucast_type, 62, 2)
  217. DEFINE_ALE_FIELD1_SET(port_num, 66)
  218. DEFINE_ALE_FIELD_SET(blocked, 65, 1)
  219. DEFINE_ALE_FIELD_SET(secure, 64, 1)
  220. DEFINE_ALE_FIELD_GET(mcast, 40, 1)
  221. #define NU_VLAN_UNREG_MCAST_IDX 1
  222. static int cpsw_ale_entry_get_fld(struct cpsw_ale *ale,
  223. u32 *ale_entry,
  224. const struct ale_entry_fld *entry_tbl,
  225. int fld_id)
  226. {
  227. const struct ale_entry_fld *entry_fld;
  228. u32 bits;
  229. if (!ale || !ale_entry)
  230. return -EINVAL;
  231. entry_fld = &entry_tbl[fld_id];
  232. if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
  233. dev_err(ale->params.dev, "get: wrong ale fld id %d\n", fld_id);
  234. return -ENOENT;
  235. }
  236. bits = entry_fld->num_bits;
  237. if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
  238. bits = ale->port_mask_bits;
  239. return cpsw_ale_get_field(ale_entry, entry_fld->start_bit, bits);
  240. }
  241. static void cpsw_ale_entry_set_fld(struct cpsw_ale *ale,
  242. u32 *ale_entry,
  243. const struct ale_entry_fld *entry_tbl,
  244. int fld_id,
  245. u32 value)
  246. {
  247. const struct ale_entry_fld *entry_fld;
  248. u32 bits;
  249. if (!ale || !ale_entry)
  250. return;
  251. entry_fld = &entry_tbl[fld_id];
  252. if (!(entry_fld->flags & ALE_FLD_ALLOWED)) {
  253. dev_err(ale->params.dev, "set: wrong ale fld id %d\n", fld_id);
  254. return;
  255. }
  256. bits = entry_fld->num_bits;
  257. if (entry_fld->flags & ALE_FLD_SIZE_PORT_MASK_BITS)
  258. bits = ale->port_mask_bits;
  259. cpsw_ale_set_field(ale_entry, entry_fld->start_bit, bits, value);
  260. }
  261. static int cpsw_ale_vlan_get_fld(struct cpsw_ale *ale,
  262. u32 *ale_entry,
  263. int fld_id)
  264. {
  265. return cpsw_ale_entry_get_fld(ale, ale_entry,
  266. ale->vlan_entry_tbl, fld_id);
  267. }
  268. static void cpsw_ale_vlan_set_fld(struct cpsw_ale *ale,
  269. u32 *ale_entry,
  270. int fld_id,
  271. u32 value)
  272. {
  273. cpsw_ale_entry_set_fld(ale, ale_entry,
  274. ale->vlan_entry_tbl, fld_id, value);
  275. }
  276. /* The MAC address field in the ALE entry cannot be macroized as above */
  277. static inline void cpsw_ale_get_addr(u32 *ale_entry, u8 *addr)
  278. {
  279. int i;
  280. for (i = 0; i < 6; i++)
  281. addr[i] = cpsw_ale_get_field(ale_entry, 40 - 8*i, 8);
  282. }
  283. static inline void cpsw_ale_set_addr(u32 *ale_entry, const u8 *addr)
  284. {
  285. int i;
  286. for (i = 0; i < 6; i++)
  287. cpsw_ale_set_field(ale_entry, 40 - 8*i, 8, addr[i]);
  288. }
  289. static int cpsw_ale_read(struct cpsw_ale *ale, int idx, u32 *ale_entry)
  290. {
  291. int i;
  292. WARN_ON(idx > ale->params.ale_entries);
  293. writel_relaxed(idx, ale->params.ale_regs + ALE_TABLE_CONTROL);
  294. for (i = 0; i < ALE_ENTRY_WORDS; i++)
  295. ale_entry[i] = readl_relaxed(ale->params.ale_regs +
  296. ALE_TABLE + 4 * i);
  297. return idx;
  298. }
  299. static int cpsw_ale_write(struct cpsw_ale *ale, int idx, u32 *ale_entry)
  300. {
  301. int i;
  302. WARN_ON(idx > ale->params.ale_entries);
  303. for (i = 0; i < ALE_ENTRY_WORDS; i++)
  304. writel_relaxed(ale_entry[i], ale->params.ale_regs +
  305. ALE_TABLE + 4 * i);
  306. writel_relaxed(idx | ALE_TABLE_WRITE, ale->params.ale_regs +
  307. ALE_TABLE_CONTROL);
  308. return idx;
  309. }
  310. static int cpsw_ale_match_addr(struct cpsw_ale *ale, const u8 *addr, u16 vid)
  311. {
  312. u32 ale_entry[ALE_ENTRY_WORDS];
  313. int type, idx;
  314. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  315. u8 entry_addr[6];
  316. cpsw_ale_read(ale, idx, ale_entry);
  317. type = cpsw_ale_get_entry_type(ale_entry);
  318. if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
  319. continue;
  320. if (cpsw_ale_get_vlan_id(ale_entry) != vid)
  321. continue;
  322. cpsw_ale_get_addr(ale_entry, entry_addr);
  323. if (ether_addr_equal(entry_addr, addr))
  324. return idx;
  325. }
  326. return -ENOENT;
  327. }
  328. static int cpsw_ale_match_vlan(struct cpsw_ale *ale, u16 vid)
  329. {
  330. u32 ale_entry[ALE_ENTRY_WORDS];
  331. int type, idx;
  332. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  333. cpsw_ale_read(ale, idx, ale_entry);
  334. type = cpsw_ale_get_entry_type(ale_entry);
  335. if (type != ALE_TYPE_VLAN)
  336. continue;
  337. if (cpsw_ale_get_vlan_id(ale_entry) == vid)
  338. return idx;
  339. }
  340. return -ENOENT;
  341. }
  342. static int cpsw_ale_match_free(struct cpsw_ale *ale)
  343. {
  344. u32 ale_entry[ALE_ENTRY_WORDS];
  345. int type, idx;
  346. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  347. cpsw_ale_read(ale, idx, ale_entry);
  348. type = cpsw_ale_get_entry_type(ale_entry);
  349. if (type == ALE_TYPE_FREE)
  350. return idx;
  351. }
  352. return -ENOENT;
  353. }
  354. static int cpsw_ale_find_ageable(struct cpsw_ale *ale)
  355. {
  356. u32 ale_entry[ALE_ENTRY_WORDS];
  357. int type, idx;
  358. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  359. cpsw_ale_read(ale, idx, ale_entry);
  360. type = cpsw_ale_get_entry_type(ale_entry);
  361. if (type != ALE_TYPE_ADDR && type != ALE_TYPE_VLAN_ADDR)
  362. continue;
  363. if (cpsw_ale_get_mcast(ale_entry))
  364. continue;
  365. type = cpsw_ale_get_ucast_type(ale_entry);
  366. if (type != ALE_UCAST_PERSISTANT &&
  367. type != ALE_UCAST_OUI)
  368. return idx;
  369. }
  370. return -ENOENT;
  371. }
  372. static void cpsw_ale_flush_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  373. int port_mask)
  374. {
  375. int mask;
  376. mask = cpsw_ale_get_port_mask(ale_entry,
  377. ale->port_mask_bits);
  378. if ((mask & port_mask) == 0)
  379. return; /* ports dont intersect, not interested */
  380. mask &= (~port_mask | ALE_PORT_HOST);
  381. if (mask == 0x0 || mask == ALE_PORT_HOST)
  382. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  383. else
  384. cpsw_ale_set_port_mask(ale_entry, mask,
  385. ale->port_mask_bits);
  386. }
  387. int cpsw_ale_flush_multicast(struct cpsw_ale *ale, int port_mask, int vid)
  388. {
  389. u32 ale_entry[ALE_ENTRY_WORDS];
  390. int ret, idx;
  391. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  392. cpsw_ale_read(ale, idx, ale_entry);
  393. ret = cpsw_ale_get_entry_type(ale_entry);
  394. if (ret != ALE_TYPE_ADDR && ret != ALE_TYPE_VLAN_ADDR)
  395. continue;
  396. /* if vid passed is -1 then remove all multicast entry from
  397. * the table irrespective of vlan id, if a valid vlan id is
  398. * passed then remove only multicast added to that vlan id.
  399. * if vlan id doesn't match then move on to next entry.
  400. */
  401. if (vid != -1 && cpsw_ale_get_vlan_id(ale_entry) != vid)
  402. continue;
  403. if (cpsw_ale_get_mcast(ale_entry)) {
  404. u8 addr[6];
  405. if (cpsw_ale_get_super(ale_entry))
  406. continue;
  407. cpsw_ale_get_addr(ale_entry, addr);
  408. if (!is_broadcast_ether_addr(addr))
  409. cpsw_ale_flush_mcast(ale, ale_entry, port_mask);
  410. }
  411. cpsw_ale_write(ale, idx, ale_entry);
  412. }
  413. return 0;
  414. }
  415. static inline void cpsw_ale_set_vlan_entry_type(u32 *ale_entry,
  416. int flags, u16 vid)
  417. {
  418. if (flags & ALE_VLAN) {
  419. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN_ADDR);
  420. cpsw_ale_set_vlan_id(ale_entry, vid);
  421. } else {
  422. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_ADDR);
  423. }
  424. }
  425. int cpsw_ale_add_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
  426. int flags, u16 vid)
  427. {
  428. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  429. int idx;
  430. cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
  431. cpsw_ale_set_addr(ale_entry, addr);
  432. cpsw_ale_set_ucast_type(ale_entry, ALE_UCAST_PERSISTANT);
  433. cpsw_ale_set_secure(ale_entry, (flags & ALE_SECURE) ? 1 : 0);
  434. cpsw_ale_set_blocked(ale_entry, (flags & ALE_BLOCKED) ? 1 : 0);
  435. cpsw_ale_set_port_num(ale_entry, port, ale->port_num_bits);
  436. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  437. if (idx < 0)
  438. idx = cpsw_ale_match_free(ale);
  439. if (idx < 0)
  440. idx = cpsw_ale_find_ageable(ale);
  441. if (idx < 0)
  442. return -ENOMEM;
  443. cpsw_ale_write(ale, idx, ale_entry);
  444. return 0;
  445. }
  446. int cpsw_ale_del_ucast(struct cpsw_ale *ale, const u8 *addr, int port,
  447. int flags, u16 vid)
  448. {
  449. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  450. int idx;
  451. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  452. if (idx < 0)
  453. return -ENOENT;
  454. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  455. cpsw_ale_write(ale, idx, ale_entry);
  456. return 0;
  457. }
  458. int cpsw_ale_add_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
  459. int flags, u16 vid, int mcast_state)
  460. {
  461. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  462. int idx, mask;
  463. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  464. if (idx >= 0)
  465. cpsw_ale_read(ale, idx, ale_entry);
  466. cpsw_ale_set_vlan_entry_type(ale_entry, flags, vid);
  467. cpsw_ale_set_addr(ale_entry, addr);
  468. cpsw_ale_set_super(ale_entry, (flags & ALE_SUPER) ? 1 : 0);
  469. cpsw_ale_set_mcast_state(ale_entry, mcast_state);
  470. mask = cpsw_ale_get_port_mask(ale_entry,
  471. ale->port_mask_bits);
  472. port_mask |= mask;
  473. cpsw_ale_set_port_mask(ale_entry, port_mask,
  474. ale->port_mask_bits);
  475. if (idx < 0)
  476. idx = cpsw_ale_match_free(ale);
  477. if (idx < 0)
  478. idx = cpsw_ale_find_ageable(ale);
  479. if (idx < 0)
  480. return -ENOMEM;
  481. cpsw_ale_write(ale, idx, ale_entry);
  482. return 0;
  483. }
  484. int cpsw_ale_del_mcast(struct cpsw_ale *ale, const u8 *addr, int port_mask,
  485. int flags, u16 vid)
  486. {
  487. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  488. int mcast_members = 0;
  489. int idx;
  490. idx = cpsw_ale_match_addr(ale, addr, (flags & ALE_VLAN) ? vid : 0);
  491. if (idx < 0)
  492. return -ENOENT;
  493. cpsw_ale_read(ale, idx, ale_entry);
  494. if (port_mask) {
  495. mcast_members = cpsw_ale_get_port_mask(ale_entry,
  496. ale->port_mask_bits);
  497. mcast_members &= ~port_mask;
  498. }
  499. if (mcast_members)
  500. cpsw_ale_set_port_mask(ale_entry, mcast_members,
  501. ale->port_mask_bits);
  502. else
  503. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  504. cpsw_ale_write(ale, idx, ale_entry);
  505. return 0;
  506. }
  507. /* ALE NetCP NU switch specific vlan functions */
  508. static void cpsw_ale_set_vlan_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  509. int reg_mcast, int unreg_mcast)
  510. {
  511. int idx;
  512. /* Set VLAN registered multicast flood mask */
  513. idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
  514. ALE_ENT_VID_REG_MCAST_IDX);
  515. writel(reg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  516. /* Set VLAN unregistered multicast flood mask */
  517. idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
  518. ALE_ENT_VID_UNREG_MCAST_IDX);
  519. writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  520. }
  521. static void cpsw_ale_set_vlan_untag(struct cpsw_ale *ale, u32 *ale_entry,
  522. u16 vid, int untag_mask)
  523. {
  524. cpsw_ale_vlan_set_fld(ale, ale_entry,
  525. ALE_ENT_VID_FORCE_UNTAGGED_MSK,
  526. untag_mask);
  527. if (untag_mask & ALE_PORT_HOST)
  528. bitmap_set(ale->p0_untag_vid_mask, vid, 1);
  529. else
  530. bitmap_clear(ale->p0_untag_vid_mask, vid, 1);
  531. }
  532. int cpsw_ale_add_vlan(struct cpsw_ale *ale, u16 vid, int port_mask, int untag,
  533. int reg_mcast, int unreg_mcast)
  534. {
  535. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  536. int idx;
  537. idx = cpsw_ale_match_vlan(ale, vid);
  538. if (idx >= 0)
  539. cpsw_ale_read(ale, idx, ale_entry);
  540. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_VLAN);
  541. cpsw_ale_set_vlan_id(ale_entry, vid);
  542. cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
  543. if (!ale->params.nu_switch_ale) {
  544. cpsw_ale_vlan_set_fld(ale, ale_entry,
  545. ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
  546. cpsw_ale_vlan_set_fld(ale, ale_entry,
  547. ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
  548. } else {
  549. cpsw_ale_vlan_set_fld(ale, ale_entry,
  550. ALE_ENT_VID_UNREG_MCAST_IDX,
  551. NU_VLAN_UNREG_MCAST_IDX);
  552. cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast, unreg_mcast);
  553. }
  554. cpsw_ale_vlan_set_fld(ale, ale_entry,
  555. ALE_ENT_VID_MEMBER_LIST, port_mask);
  556. if (idx < 0)
  557. idx = cpsw_ale_match_free(ale);
  558. if (idx < 0)
  559. idx = cpsw_ale_find_ageable(ale);
  560. if (idx < 0)
  561. return -ENOMEM;
  562. cpsw_ale_write(ale, idx, ale_entry);
  563. return 0;
  564. }
  565. static void cpsw_ale_vlan_del_modify_int(struct cpsw_ale *ale, u32 *ale_entry,
  566. u16 vid, int port_mask)
  567. {
  568. int reg_mcast, unreg_mcast;
  569. int members, untag;
  570. members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  571. ALE_ENT_VID_MEMBER_LIST);
  572. members &= ~port_mask;
  573. if (!members) {
  574. cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
  575. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  576. return;
  577. }
  578. untag = cpsw_ale_vlan_get_fld(ale, ale_entry,
  579. ALE_ENT_VID_FORCE_UNTAGGED_MSK);
  580. reg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
  581. ALE_ENT_VID_REG_MCAST_MSK);
  582. unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
  583. ALE_ENT_VID_UNREG_MCAST_MSK);
  584. untag &= members;
  585. reg_mcast &= members;
  586. unreg_mcast &= members;
  587. cpsw_ale_set_vlan_untag(ale, ale_entry, vid, untag);
  588. if (!ale->params.nu_switch_ale) {
  589. cpsw_ale_vlan_set_fld(ale, ale_entry,
  590. ALE_ENT_VID_REG_MCAST_MSK, reg_mcast);
  591. cpsw_ale_vlan_set_fld(ale, ale_entry,
  592. ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
  593. } else {
  594. cpsw_ale_set_vlan_mcast(ale, ale_entry, reg_mcast,
  595. unreg_mcast);
  596. }
  597. cpsw_ale_vlan_set_fld(ale, ale_entry,
  598. ALE_ENT_VID_MEMBER_LIST, members);
  599. }
  600. int cpsw_ale_vlan_del_modify(struct cpsw_ale *ale, u16 vid, int port_mask)
  601. {
  602. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  603. int idx;
  604. idx = cpsw_ale_match_vlan(ale, vid);
  605. if (idx < 0)
  606. return -ENOENT;
  607. cpsw_ale_read(ale, idx, ale_entry);
  608. cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
  609. cpsw_ale_write(ale, idx, ale_entry);
  610. return 0;
  611. }
  612. int cpsw_ale_del_vlan(struct cpsw_ale *ale, u16 vid, int port_mask)
  613. {
  614. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  615. int members, idx;
  616. idx = cpsw_ale_match_vlan(ale, vid);
  617. if (idx < 0)
  618. return -ENOENT;
  619. cpsw_ale_read(ale, idx, ale_entry);
  620. /* if !port_mask - force remove VLAN (legacy).
  621. * Check if there are other VLAN members ports
  622. * if no - remove VLAN.
  623. * if yes it means same VLAN was added to >1 port in multi port mode, so
  624. * remove port_mask ports from VLAN ALE entry excluding Host port.
  625. */
  626. members = cpsw_ale_vlan_get_fld(ale, ale_entry, ALE_ENT_VID_MEMBER_LIST);
  627. members &= ~port_mask;
  628. if (!port_mask || !members) {
  629. /* last port or force remove - remove VLAN */
  630. cpsw_ale_set_vlan_untag(ale, ale_entry, vid, 0);
  631. cpsw_ale_set_entry_type(ale_entry, ALE_TYPE_FREE);
  632. } else {
  633. port_mask &= ~ALE_PORT_HOST;
  634. cpsw_ale_vlan_del_modify_int(ale, ale_entry, vid, port_mask);
  635. }
  636. cpsw_ale_write(ale, idx, ale_entry);
  637. return 0;
  638. }
  639. int cpsw_ale_vlan_add_modify(struct cpsw_ale *ale, u16 vid, int port_mask,
  640. int untag_mask, int reg_mask, int unreg_mask)
  641. {
  642. u32 ale_entry[ALE_ENTRY_WORDS] = {0, 0, 0};
  643. int reg_mcast_members, unreg_mcast_members;
  644. int vlan_members, untag_members;
  645. int idx, ret = 0;
  646. idx = cpsw_ale_match_vlan(ale, vid);
  647. if (idx >= 0)
  648. cpsw_ale_read(ale, idx, ale_entry);
  649. vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  650. ALE_ENT_VID_MEMBER_LIST);
  651. reg_mcast_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  652. ALE_ENT_VID_REG_MCAST_MSK);
  653. unreg_mcast_members =
  654. cpsw_ale_vlan_get_fld(ale, ale_entry,
  655. ALE_ENT_VID_UNREG_MCAST_MSK);
  656. untag_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  657. ALE_ENT_VID_FORCE_UNTAGGED_MSK);
  658. vlan_members |= port_mask;
  659. untag_members = (untag_members & ~port_mask) | untag_mask;
  660. reg_mcast_members = (reg_mcast_members & ~port_mask) | reg_mask;
  661. unreg_mcast_members = (unreg_mcast_members & ~port_mask) | unreg_mask;
  662. ret = cpsw_ale_add_vlan(ale, vid, vlan_members, untag_members,
  663. reg_mcast_members, unreg_mcast_members);
  664. if (ret) {
  665. dev_err(ale->params.dev, "Unable to add vlan\n");
  666. return ret;
  667. }
  668. dev_dbg(ale->params.dev, "port mask 0x%x untag 0x%x\n", vlan_members,
  669. untag_mask);
  670. return ret;
  671. }
  672. void cpsw_ale_set_unreg_mcast(struct cpsw_ale *ale, int unreg_mcast_mask,
  673. bool add)
  674. {
  675. u32 ale_entry[ALE_ENTRY_WORDS];
  676. int unreg_members = 0;
  677. int type, idx;
  678. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  679. cpsw_ale_read(ale, idx, ale_entry);
  680. type = cpsw_ale_get_entry_type(ale_entry);
  681. if (type != ALE_TYPE_VLAN)
  682. continue;
  683. unreg_members =
  684. cpsw_ale_vlan_get_fld(ale, ale_entry,
  685. ALE_ENT_VID_UNREG_MCAST_MSK);
  686. if (add)
  687. unreg_members |= unreg_mcast_mask;
  688. else
  689. unreg_members &= ~unreg_mcast_mask;
  690. cpsw_ale_vlan_set_fld(ale, ale_entry,
  691. ALE_ENT_VID_UNREG_MCAST_MSK,
  692. unreg_members);
  693. cpsw_ale_write(ale, idx, ale_entry);
  694. }
  695. }
  696. static void cpsw_ale_vlan_set_unreg_mcast(struct cpsw_ale *ale, u32 *ale_entry,
  697. int allmulti)
  698. {
  699. int unreg_mcast;
  700. unreg_mcast = cpsw_ale_vlan_get_fld(ale, ale_entry,
  701. ALE_ENT_VID_UNREG_MCAST_MSK);
  702. if (allmulti)
  703. unreg_mcast |= ALE_PORT_HOST;
  704. else
  705. unreg_mcast &= ~ALE_PORT_HOST;
  706. cpsw_ale_vlan_set_fld(ale, ale_entry,
  707. ALE_ENT_VID_UNREG_MCAST_MSK, unreg_mcast);
  708. }
  709. static void
  710. cpsw_ale_vlan_set_unreg_mcast_idx(struct cpsw_ale *ale, u32 *ale_entry,
  711. int allmulti)
  712. {
  713. int unreg_mcast;
  714. int idx;
  715. idx = cpsw_ale_vlan_get_fld(ale, ale_entry,
  716. ALE_ENT_VID_UNREG_MCAST_IDX);
  717. unreg_mcast = readl(ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  718. if (allmulti)
  719. unreg_mcast |= ALE_PORT_HOST;
  720. else
  721. unreg_mcast &= ~ALE_PORT_HOST;
  722. writel(unreg_mcast, ale->params.ale_regs + ALE_VLAN_MASK_MUX(idx));
  723. }
  724. void cpsw_ale_set_allmulti(struct cpsw_ale *ale, int allmulti, int port)
  725. {
  726. u32 ale_entry[ALE_ENTRY_WORDS];
  727. int type, idx;
  728. for (idx = 0; idx < ale->params.ale_entries; idx++) {
  729. int vlan_members;
  730. cpsw_ale_read(ale, idx, ale_entry);
  731. type = cpsw_ale_get_entry_type(ale_entry);
  732. if (type != ALE_TYPE_VLAN)
  733. continue;
  734. vlan_members = cpsw_ale_vlan_get_fld(ale, ale_entry,
  735. ALE_ENT_VID_MEMBER_LIST);
  736. if (port != -1 && !(vlan_members & BIT(port)))
  737. continue;
  738. if (!ale->params.nu_switch_ale)
  739. cpsw_ale_vlan_set_unreg_mcast(ale, ale_entry, allmulti);
  740. else
  741. cpsw_ale_vlan_set_unreg_mcast_idx(ale, ale_entry,
  742. allmulti);
  743. cpsw_ale_write(ale, idx, ale_entry);
  744. }
  745. }
  746. struct ale_control_info {
  747. const char *name;
  748. int offset, port_offset;
  749. int shift, port_shift;
  750. int bits;
  751. };
  752. static struct ale_control_info ale_controls[ALE_NUM_CONTROLS] = {
  753. [ALE_ENABLE] = {
  754. .name = "enable",
  755. .offset = ALE_CONTROL,
  756. .port_offset = 0,
  757. .shift = 31,
  758. .port_shift = 0,
  759. .bits = 1,
  760. },
  761. [ALE_CLEAR] = {
  762. .name = "clear",
  763. .offset = ALE_CONTROL,
  764. .port_offset = 0,
  765. .shift = 30,
  766. .port_shift = 0,
  767. .bits = 1,
  768. },
  769. [ALE_AGEOUT] = {
  770. .name = "ageout",
  771. .offset = ALE_CONTROL,
  772. .port_offset = 0,
  773. .shift = 29,
  774. .port_shift = 0,
  775. .bits = 1,
  776. },
  777. [ALE_P0_UNI_FLOOD] = {
  778. .name = "port0_unicast_flood",
  779. .offset = ALE_CONTROL,
  780. .port_offset = 0,
  781. .shift = 8,
  782. .port_shift = 0,
  783. .bits = 1,
  784. },
  785. [ALE_VLAN_NOLEARN] = {
  786. .name = "vlan_nolearn",
  787. .offset = ALE_CONTROL,
  788. .port_offset = 0,
  789. .shift = 7,
  790. .port_shift = 0,
  791. .bits = 1,
  792. },
  793. [ALE_NO_PORT_VLAN] = {
  794. .name = "no_port_vlan",
  795. .offset = ALE_CONTROL,
  796. .port_offset = 0,
  797. .shift = 6,
  798. .port_shift = 0,
  799. .bits = 1,
  800. },
  801. [ALE_OUI_DENY] = {
  802. .name = "oui_deny",
  803. .offset = ALE_CONTROL,
  804. .port_offset = 0,
  805. .shift = 5,
  806. .port_shift = 0,
  807. .bits = 1,
  808. },
  809. [ALE_BYPASS] = {
  810. .name = "bypass",
  811. .offset = ALE_CONTROL,
  812. .port_offset = 0,
  813. .shift = 4,
  814. .port_shift = 0,
  815. .bits = 1,
  816. },
  817. [ALE_RATE_LIMIT_TX] = {
  818. .name = "rate_limit_tx",
  819. .offset = ALE_CONTROL,
  820. .port_offset = 0,
  821. .shift = 3,
  822. .port_shift = 0,
  823. .bits = 1,
  824. },
  825. [ALE_VLAN_AWARE] = {
  826. .name = "vlan_aware",
  827. .offset = ALE_CONTROL,
  828. .port_offset = 0,
  829. .shift = 2,
  830. .port_shift = 0,
  831. .bits = 1,
  832. },
  833. [ALE_AUTH_ENABLE] = {
  834. .name = "auth_enable",
  835. .offset = ALE_CONTROL,
  836. .port_offset = 0,
  837. .shift = 1,
  838. .port_shift = 0,
  839. .bits = 1,
  840. },
  841. [ALE_RATE_LIMIT] = {
  842. .name = "rate_limit",
  843. .offset = ALE_CONTROL,
  844. .port_offset = 0,
  845. .shift = 0,
  846. .port_shift = 0,
  847. .bits = 1,
  848. },
  849. [ALE_PORT_STATE] = {
  850. .name = "port_state",
  851. .offset = ALE_PORTCTL,
  852. .port_offset = 4,
  853. .shift = 0,
  854. .port_shift = 0,
  855. .bits = 2,
  856. },
  857. [ALE_PORT_DROP_UNTAGGED] = {
  858. .name = "drop_untagged",
  859. .offset = ALE_PORTCTL,
  860. .port_offset = 4,
  861. .shift = 2,
  862. .port_shift = 0,
  863. .bits = 1,
  864. },
  865. [ALE_PORT_DROP_UNKNOWN_VLAN] = {
  866. .name = "drop_unknown",
  867. .offset = ALE_PORTCTL,
  868. .port_offset = 4,
  869. .shift = 3,
  870. .port_shift = 0,
  871. .bits = 1,
  872. },
  873. [ALE_PORT_NOLEARN] = {
  874. .name = "nolearn",
  875. .offset = ALE_PORTCTL,
  876. .port_offset = 4,
  877. .shift = 4,
  878. .port_shift = 0,
  879. .bits = 1,
  880. },
  881. [ALE_PORT_NO_SA_UPDATE] = {
  882. .name = "no_source_update",
  883. .offset = ALE_PORTCTL,
  884. .port_offset = 4,
  885. .shift = 5,
  886. .port_shift = 0,
  887. .bits = 1,
  888. },
  889. [ALE_PORT_MACONLY] = {
  890. .name = "mac_only_port_mode",
  891. .offset = ALE_PORTCTL,
  892. .port_offset = 4,
  893. .shift = 11,
  894. .port_shift = 0,
  895. .bits = 1,
  896. },
  897. [ALE_PORT_MACONLY_CAF] = {
  898. .name = "mac_only_port_caf",
  899. .offset = ALE_PORTCTL,
  900. .port_offset = 4,
  901. .shift = 13,
  902. .port_shift = 0,
  903. .bits = 1,
  904. },
  905. [ALE_PORT_MCAST_LIMIT] = {
  906. .name = "mcast_limit",
  907. .offset = ALE_PORTCTL,
  908. .port_offset = 4,
  909. .shift = 16,
  910. .port_shift = 0,
  911. .bits = 8,
  912. },
  913. [ALE_PORT_BCAST_LIMIT] = {
  914. .name = "bcast_limit",
  915. .offset = ALE_PORTCTL,
  916. .port_offset = 4,
  917. .shift = 24,
  918. .port_shift = 0,
  919. .bits = 8,
  920. },
  921. [ALE_PORT_UNKNOWN_VLAN_MEMBER] = {
  922. .name = "unknown_vlan_member",
  923. .offset = ALE_UNKNOWNVLAN,
  924. .port_offset = 0,
  925. .shift = 0,
  926. .port_shift = 0,
  927. .bits = 6,
  928. },
  929. [ALE_PORT_UNKNOWN_MCAST_FLOOD] = {
  930. .name = "unknown_mcast_flood",
  931. .offset = ALE_UNKNOWNVLAN,
  932. .port_offset = 0,
  933. .shift = 8,
  934. .port_shift = 0,
  935. .bits = 6,
  936. },
  937. [ALE_PORT_UNKNOWN_REG_MCAST_FLOOD] = {
  938. .name = "unknown_reg_flood",
  939. .offset = ALE_UNKNOWNVLAN,
  940. .port_offset = 0,
  941. .shift = 16,
  942. .port_shift = 0,
  943. .bits = 6,
  944. },
  945. [ALE_PORT_UNTAGGED_EGRESS] = {
  946. .name = "untagged_egress",
  947. .offset = ALE_UNKNOWNVLAN,
  948. .port_offset = 0,
  949. .shift = 24,
  950. .port_shift = 0,
  951. .bits = 6,
  952. },
  953. [ALE_DEFAULT_THREAD_ID] = {
  954. .name = "default_thread_id",
  955. .offset = AM65_CPSW_ALE_THREAD_DEF_REG,
  956. .port_offset = 0,
  957. .shift = 0,
  958. .port_shift = 0,
  959. .bits = 6,
  960. },
  961. [ALE_DEFAULT_THREAD_ENABLE] = {
  962. .name = "default_thread_id_enable",
  963. .offset = AM65_CPSW_ALE_THREAD_DEF_REG,
  964. .port_offset = 0,
  965. .shift = 15,
  966. .port_shift = 0,
  967. .bits = 1,
  968. },
  969. };
  970. int cpsw_ale_control_set(struct cpsw_ale *ale, int port, int control,
  971. int value)
  972. {
  973. const struct ale_control_info *info;
  974. int offset, shift;
  975. u32 tmp, mask;
  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. mask = BITMASK(info->bits);
  984. if (value & ~mask)
  985. return -EINVAL;
  986. offset = info->offset + (port * info->port_offset);
  987. shift = info->shift + (port * info->port_shift);
  988. tmp = readl_relaxed(ale->params.ale_regs + offset);
  989. tmp = (tmp & ~(mask << shift)) | (value << shift);
  990. writel_relaxed(tmp, ale->params.ale_regs + offset);
  991. return 0;
  992. }
  993. int cpsw_ale_control_get(struct cpsw_ale *ale, int port, int control)
  994. {
  995. const struct ale_control_info *info;
  996. int offset, shift;
  997. u32 tmp;
  998. if (control < 0 || control >= ARRAY_SIZE(ale_controls))
  999. return -EINVAL;
  1000. info = &ale_controls[control];
  1001. if (info->port_offset == 0 && info->port_shift == 0)
  1002. port = 0; /* global, port is a dont care */
  1003. if (port < 0 || port >= ale->params.ale_ports)
  1004. return -EINVAL;
  1005. offset = info->offset + (port * info->port_offset);
  1006. shift = info->shift + (port * info->port_shift);
  1007. tmp = readl_relaxed(ale->params.ale_regs + offset) >> shift;
  1008. return tmp & BITMASK(info->bits);
  1009. }
  1010. int cpsw_ale_rx_ratelimit_mc(struct cpsw_ale *ale, int port, unsigned int ratelimit_pps)
  1011. {
  1012. int val = ratelimit_pps / ALE_RATE_LIMIT_MIN_PPS;
  1013. u32 remainder = ratelimit_pps % ALE_RATE_LIMIT_MIN_PPS;
  1014. if (ratelimit_pps && !val) {
  1015. dev_err(ale->params.dev, "ALE MC port:%d ratelimit min value 1000pps\n", port);
  1016. return -EINVAL;
  1017. }
  1018. if (remainder)
  1019. dev_info(ale->params.dev, "ALE port:%d MC ratelimit set to %dpps (requested %d)\n",
  1020. port, ratelimit_pps - remainder, ratelimit_pps);
  1021. cpsw_ale_control_set(ale, port, ALE_PORT_MCAST_LIMIT, val);
  1022. dev_dbg(ale->params.dev, "ALE port:%d MC ratelimit set %d\n",
  1023. port, val * ALE_RATE_LIMIT_MIN_PPS);
  1024. return 0;
  1025. }
  1026. int cpsw_ale_rx_ratelimit_bc(struct cpsw_ale *ale, int port, unsigned int ratelimit_pps)
  1027. {
  1028. int val = ratelimit_pps / ALE_RATE_LIMIT_MIN_PPS;
  1029. u32 remainder = ratelimit_pps % ALE_RATE_LIMIT_MIN_PPS;
  1030. if (ratelimit_pps && !val) {
  1031. dev_err(ale->params.dev, "ALE port:%d BC ratelimit min value 1000pps\n", port);
  1032. return -EINVAL;
  1033. }
  1034. if (remainder)
  1035. dev_info(ale->params.dev, "ALE port:%d BC ratelimit set to %dpps (requested %d)\n",
  1036. port, ratelimit_pps - remainder, ratelimit_pps);
  1037. cpsw_ale_control_set(ale, port, ALE_PORT_BCAST_LIMIT, val);
  1038. dev_dbg(ale->params.dev, "ALE port:%d BC ratelimit set %d\n",
  1039. port, val * ALE_RATE_LIMIT_MIN_PPS);
  1040. return 0;
  1041. }
  1042. static void cpsw_ale_timer(struct timer_list *t)
  1043. {
  1044. struct cpsw_ale *ale = timer_container_of(ale, t, timer);
  1045. cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
  1046. if (ale->ageout) {
  1047. ale->timer.expires = jiffies + ale->ageout;
  1048. add_timer(&ale->timer);
  1049. }
  1050. }
  1051. static void cpsw_ale_hw_aging_timer_start(struct cpsw_ale *ale)
  1052. {
  1053. u32 aging_timer;
  1054. aging_timer = ale->params.bus_freq / 1000000;
  1055. aging_timer *= ale->params.ale_ageout;
  1056. if (aging_timer & ~ALE_AGING_TIMER_MASK) {
  1057. aging_timer = ALE_AGING_TIMER_MASK;
  1058. dev_warn(ale->params.dev,
  1059. "ALE aging timer overflow, set to max\n");
  1060. }
  1061. writel(aging_timer, ale->params.ale_regs + ALE_AGING_TIMER);
  1062. }
  1063. static void cpsw_ale_hw_aging_timer_stop(struct cpsw_ale *ale)
  1064. {
  1065. writel(0, ale->params.ale_regs + ALE_AGING_TIMER);
  1066. }
  1067. static void cpsw_ale_aging_start(struct cpsw_ale *ale)
  1068. {
  1069. if (!ale->params.ale_ageout)
  1070. return;
  1071. if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
  1072. cpsw_ale_hw_aging_timer_start(ale);
  1073. return;
  1074. }
  1075. timer_setup(&ale->timer, cpsw_ale_timer, 0);
  1076. ale->timer.expires = jiffies + ale->ageout;
  1077. add_timer(&ale->timer);
  1078. }
  1079. static void cpsw_ale_aging_stop(struct cpsw_ale *ale)
  1080. {
  1081. if (!ale->params.ale_ageout)
  1082. return;
  1083. if (ale->features & CPSW_ALE_F_HW_AUTOAGING) {
  1084. cpsw_ale_hw_aging_timer_stop(ale);
  1085. return;
  1086. }
  1087. timer_delete_sync(&ale->timer);
  1088. }
  1089. void cpsw_ale_start(struct cpsw_ale *ale)
  1090. {
  1091. unsigned long ale_prescale;
  1092. /* configure Broadcast and Multicast Rate Limit
  1093. * number_of_packets = (Fclk / ALE_PRESCALE) * port.BCAST/MCAST_LIMIT
  1094. * ALE_PRESCALE width is 19bit and min value 0x10
  1095. * port.BCAST/MCAST_LIMIT is 8bit
  1096. *
  1097. * For multi port configuration support the ALE_PRESCALE is configured to 1ms interval,
  1098. * which allows to configure port.BCAST/MCAST_LIMIT per port and achieve:
  1099. * min number_of_packets = 1000 when port.BCAST/MCAST_LIMIT = 1
  1100. * max number_of_packets = 1000 * 255 = 255000 when port.BCAST/MCAST_LIMIT = 0xFF
  1101. */
  1102. ale_prescale = ale->params.bus_freq / ALE_RATE_LIMIT_MIN_PPS;
  1103. writel((u32)ale_prescale, ale->params.ale_regs + ALE_PRESCALE);
  1104. /* Allow MC/BC rate limiting globally.
  1105. * The actual Rate Limit cfg enabled per-port by port.BCAST/MCAST_LIMIT
  1106. */
  1107. cpsw_ale_control_set(ale, 0, ALE_RATE_LIMIT, 1);
  1108. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 1);
  1109. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  1110. cpsw_ale_aging_start(ale);
  1111. }
  1112. void cpsw_ale_stop(struct cpsw_ale *ale)
  1113. {
  1114. cpsw_ale_aging_stop(ale);
  1115. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  1116. cpsw_ale_control_set(ale, 0, ALE_ENABLE, 0);
  1117. }
  1118. static const struct reg_field ale_fields_cpsw[] = {
  1119. /* CPSW_ALE_IDVER_REG */
  1120. [MINOR_VER] = REG_FIELD(ALE_IDVER, 0, 7),
  1121. [MAJOR_VER] = REG_FIELD(ALE_IDVER, 8, 15),
  1122. };
  1123. static const struct reg_field ale_fields_cpsw_nu[] = {
  1124. /* CPSW_ALE_IDVER_REG */
  1125. [MINOR_VER] = REG_FIELD(ALE_IDVER, 0, 7),
  1126. [MAJOR_VER] = REG_FIELD(ALE_IDVER, 8, 10),
  1127. /* CPSW_ALE_STATUS_REG */
  1128. [ALE_ENTRIES] = REG_FIELD(ALE_STATUS, 0, 7),
  1129. [ALE_POLICERS] = REG_FIELD(ALE_STATUS, 8, 15),
  1130. /* CPSW_ALE_POLICER_PORT_OUI_REG */
  1131. [POL_PORT_MEN] = REG_FIELD(ALE_POLICER_PORT_OUI, 31, 31),
  1132. [POL_TRUNK_ID] = REG_FIELD(ALE_POLICER_PORT_OUI, 30, 30),
  1133. [POL_PORT_NUM] = REG_FIELD(ALE_POLICER_PORT_OUI, 25, 25),
  1134. [POL_PRI_MEN] = REG_FIELD(ALE_POLICER_PORT_OUI, 19, 19),
  1135. [POL_PRI_VAL] = REG_FIELD(ALE_POLICER_PORT_OUI, 16, 18),
  1136. [POL_OUI_MEN] = REG_FIELD(ALE_POLICER_PORT_OUI, 15, 15),
  1137. [POL_OUI_INDEX] = REG_FIELD(ALE_POLICER_PORT_OUI, 0, 5),
  1138. /* CPSW_ALE_POLICER_DA_SA_REG */
  1139. [POL_DST_MEN] = REG_FIELD(ALE_POLICER_DA_SA, 31, 31),
  1140. [POL_DST_INDEX] = REG_FIELD(ALE_POLICER_DA_SA, 16, 21),
  1141. [POL_SRC_MEN] = REG_FIELD(ALE_POLICER_DA_SA, 15, 15),
  1142. [POL_SRC_INDEX] = REG_FIELD(ALE_POLICER_DA_SA, 0, 5),
  1143. /* CPSW_ALE_POLICER_VLAN_REG */
  1144. [POL_OVLAN_MEN] = REG_FIELD(ALE_POLICER_VLAN, 31, 31),
  1145. [POL_OVLAN_INDEX] = REG_FIELD(ALE_POLICER_VLAN, 16, 21),
  1146. [POL_IVLAN_MEN] = REG_FIELD(ALE_POLICER_VLAN, 15, 15),
  1147. [POL_IVLAN_INDEX] = REG_FIELD(ALE_POLICER_VLAN, 0, 5),
  1148. /* CPSW_ALE_POLICER_ETHERTYPE_IPSA_REG */
  1149. [POL_ETHERTYPE_MEN] = REG_FIELD(ALE_POLICER_ETHERTYPE_IPSA, 31, 31),
  1150. [POL_ETHERTYPE_INDEX] = REG_FIELD(ALE_POLICER_ETHERTYPE_IPSA, 16, 21),
  1151. [POL_IPSRC_MEN] = REG_FIELD(ALE_POLICER_ETHERTYPE_IPSA, 15, 15),
  1152. [POL_IPSRC_INDEX] = REG_FIELD(ALE_POLICER_ETHERTYPE_IPSA, 0, 5),
  1153. /* CPSW_ALE_POLICER_IPDA_REG */
  1154. [POL_IPDST_MEN] = REG_FIELD(ALE_POLICER_IPDA, 31, 31),
  1155. [POL_IPDST_INDEX] = REG_FIELD(ALE_POLICER_IPDA, 16, 21),
  1156. /* CPSW_ALE_POLICER_TBL_CTL_REG */
  1157. /**
  1158. * REG_FIELDS not defined for this as fields cannot be correctly
  1159. * used independently
  1160. */
  1161. /* CPSW_ALE_POLICER_CTL_REG */
  1162. [POL_EN] = REG_FIELD(ALE_POLICER_CTL, 31, 31),
  1163. [POL_RED_DROP_EN] = REG_FIELD(ALE_POLICER_CTL, 29, 29),
  1164. [POL_YELLOW_DROP_EN] = REG_FIELD(ALE_POLICER_CTL, 28, 28),
  1165. [POL_YELLOW_THRESH] = REG_FIELD(ALE_POLICER_CTL, 24, 26),
  1166. [POL_POL_MATCH_MODE] = REG_FIELD(ALE_POLICER_CTL, 22, 23),
  1167. [POL_PRIORITY_THREAD_EN] = REG_FIELD(ALE_POLICER_CTL, 21, 21),
  1168. [POL_MAC_ONLY_DEF_DIS] = REG_FIELD(ALE_POLICER_CTL, 20, 20),
  1169. /* CPSW_ALE_POLICER_TEST_CTL_REG */
  1170. [POL_TEST_CLR] = REG_FIELD(ALE_POLICER_TEST_CTL, 31, 31),
  1171. [POL_TEST_CLR_RED] = REG_FIELD(ALE_POLICER_TEST_CTL, 30, 30),
  1172. [POL_TEST_CLR_YELLOW] = REG_FIELD(ALE_POLICER_TEST_CTL, 29, 29),
  1173. [POL_TEST_CLR_SELECTED] = REG_FIELD(ALE_POLICER_TEST_CTL, 28, 28),
  1174. [POL_TEST_ENTRY] = REG_FIELD(ALE_POLICER_TEST_CTL, 0, 4),
  1175. /* CPSW_ALE_POLICER_HIT_STATUS_REG */
  1176. [POL_STATUS_HIT] = REG_FIELD(ALE_POLICER_HIT_STATUS, 31, 31),
  1177. [POL_STATUS_HIT_RED] = REG_FIELD(ALE_POLICER_HIT_STATUS, 30, 30),
  1178. [POL_STATUS_HIT_YELLOW] = REG_FIELD(ALE_POLICER_HIT_STATUS, 29, 29),
  1179. /* CPSW_ALE_THREAD_DEF_REG */
  1180. [ALE_DEFAULT_THREAD_EN] = REG_FIELD(ALE_THREAD_DEF, 15, 15),
  1181. [ALE_DEFAULT_THREAD_VAL] = REG_FIELD(ALE_THREAD_DEF, 0, 5),
  1182. /* CPSW_ALE_THREAD_CTL_REG */
  1183. [ALE_THREAD_CLASS_INDEX] = REG_FIELD(ALE_THREAD_CTL, 0, 4),
  1184. /* CPSW_ALE_THREAD_VAL_REG */
  1185. [ALE_THREAD_ENABLE] = REG_FIELD(ALE_THREAD_VAL, 15, 15),
  1186. [ALE_THREAD_VALUE] = REG_FIELD(ALE_THREAD_VAL, 0, 5),
  1187. };
  1188. static const struct cpsw_ale_dev_id cpsw_ale_id_match[] = {
  1189. {
  1190. /* am3/4/5, dra7. dm814x, 66ak2hk-gbe */
  1191. .dev_id = "cpsw",
  1192. .tbl_entries = 1024,
  1193. .reg_fields = ale_fields_cpsw,
  1194. .num_fields = ARRAY_SIZE(ale_fields_cpsw),
  1195. .vlan_entry_tbl = vlan_entry_cpsw,
  1196. },
  1197. {
  1198. /* 66ak2h_xgbe */
  1199. .dev_id = "66ak2h-xgbe",
  1200. .tbl_entries = 2048,
  1201. .reg_fields = ale_fields_cpsw,
  1202. .num_fields = ARRAY_SIZE(ale_fields_cpsw),
  1203. .vlan_entry_tbl = vlan_entry_cpsw,
  1204. },
  1205. {
  1206. .dev_id = "66ak2el",
  1207. .features = CPSW_ALE_F_STATUS_REG,
  1208. .reg_fields = ale_fields_cpsw_nu,
  1209. .num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
  1210. .nu_switch_ale = true,
  1211. .vlan_entry_tbl = vlan_entry_nu,
  1212. },
  1213. {
  1214. .dev_id = "66ak2g",
  1215. .features = CPSW_ALE_F_STATUS_REG,
  1216. .tbl_entries = 64,
  1217. .reg_fields = ale_fields_cpsw_nu,
  1218. .num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
  1219. .nu_switch_ale = true,
  1220. .vlan_entry_tbl = vlan_entry_nu,
  1221. },
  1222. {
  1223. .dev_id = "am65x-cpsw2g",
  1224. .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
  1225. .tbl_entries = 64,
  1226. .reg_fields = ale_fields_cpsw_nu,
  1227. .num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
  1228. .nu_switch_ale = true,
  1229. .vlan_entry_tbl = vlan_entry_nu,
  1230. },
  1231. {
  1232. .dev_id = "j721e-cpswxg",
  1233. .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
  1234. .reg_fields = ale_fields_cpsw_nu,
  1235. .num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
  1236. .vlan_entry_tbl = vlan_entry_k3_cpswxg,
  1237. },
  1238. {
  1239. .dev_id = "am64-cpswxg",
  1240. .features = CPSW_ALE_F_STATUS_REG | CPSW_ALE_F_HW_AUTOAGING,
  1241. .reg_fields = ale_fields_cpsw_nu,
  1242. .num_fields = ARRAY_SIZE(ale_fields_cpsw_nu),
  1243. .vlan_entry_tbl = vlan_entry_k3_cpswxg,
  1244. .tbl_entries = 512,
  1245. },
  1246. { },
  1247. };
  1248. static const struct
  1249. cpsw_ale_dev_id *cpsw_ale_match_id(const struct cpsw_ale_dev_id *id,
  1250. const char *dev_id)
  1251. {
  1252. if (!dev_id)
  1253. return NULL;
  1254. while (id->dev_id) {
  1255. if (strcmp(dev_id, id->dev_id) == 0)
  1256. return id;
  1257. id++;
  1258. }
  1259. return NULL;
  1260. }
  1261. static const struct regmap_config ale_regmap_cfg = {
  1262. .reg_bits = 32,
  1263. .val_bits = 32,
  1264. .reg_stride = 4,
  1265. .name = "cpsw-ale",
  1266. };
  1267. static int cpsw_ale_regfield_init(struct cpsw_ale *ale)
  1268. {
  1269. const struct reg_field *reg_fields = ale->params.reg_fields;
  1270. struct device *dev = ale->params.dev;
  1271. struct regmap *regmap = ale->regmap;
  1272. int i;
  1273. for (i = 0; i < ale->params.num_fields; i++) {
  1274. ale->fields[i] = devm_regmap_field_alloc(dev, regmap,
  1275. reg_fields[i]);
  1276. if (IS_ERR(ale->fields[i])) {
  1277. dev_err(dev, "Unable to allocate regmap field %d\n", i);
  1278. return PTR_ERR(ale->fields[i]);
  1279. }
  1280. }
  1281. return 0;
  1282. }
  1283. struct cpsw_ale *cpsw_ale_create(struct cpsw_ale_params *params)
  1284. {
  1285. u32 ale_entries, rev_major, rev_minor, policers;
  1286. const struct cpsw_ale_dev_id *ale_dev_id;
  1287. struct cpsw_ale *ale;
  1288. int ret;
  1289. ale_dev_id = cpsw_ale_match_id(cpsw_ale_id_match, params->dev_id);
  1290. if (!ale_dev_id)
  1291. return ERR_PTR(-EINVAL);
  1292. params->ale_entries = ale_dev_id->tbl_entries;
  1293. params->nu_switch_ale = ale_dev_id->nu_switch_ale;
  1294. params->reg_fields = ale_dev_id->reg_fields;
  1295. params->num_fields = ale_dev_id->num_fields;
  1296. ale = devm_kzalloc(params->dev, sizeof(*ale), GFP_KERNEL);
  1297. if (!ale)
  1298. return ERR_PTR(-ENOMEM);
  1299. ale->regmap = devm_regmap_init_mmio(params->dev, params->ale_regs,
  1300. &ale_regmap_cfg);
  1301. if (IS_ERR(ale->regmap)) {
  1302. dev_err(params->dev, "Couldn't create CPSW ALE regmap\n");
  1303. return ERR_PTR(-ENOMEM);
  1304. }
  1305. ale->params = *params;
  1306. ret = cpsw_ale_regfield_init(ale);
  1307. if (ret)
  1308. return ERR_PTR(ret);
  1309. ale->p0_untag_vid_mask = devm_bitmap_zalloc(params->dev, VLAN_N_VID,
  1310. GFP_KERNEL);
  1311. if (!ale->p0_untag_vid_mask)
  1312. return ERR_PTR(-ENOMEM);
  1313. ale->ageout = ale->params.ale_ageout * HZ;
  1314. ale->features = ale_dev_id->features;
  1315. ale->vlan_entry_tbl = ale_dev_id->vlan_entry_tbl;
  1316. regmap_field_read(ale->fields[MINOR_VER], &rev_minor);
  1317. regmap_field_read(ale->fields[MAJOR_VER], &rev_major);
  1318. ale->version = rev_major << 8 | rev_minor;
  1319. dev_info(ale->params.dev, "initialized cpsw ale version %d.%d\n",
  1320. rev_major, rev_minor);
  1321. if (ale->features & CPSW_ALE_F_STATUS_REG &&
  1322. !ale->params.ale_entries) {
  1323. regmap_field_read(ale->fields[ALE_ENTRIES], &ale_entries);
  1324. /* ALE available on newer NetCP switches has introduced
  1325. * a register, ALE_STATUS, to indicate the size of ALE
  1326. * table which shows the size as a multiple of 1024 entries.
  1327. * For these, params.ale_entries will be set to zero. So
  1328. * read the register and update the value of ale_entries.
  1329. * return error if ale_entries is zero in ALE_STATUS.
  1330. */
  1331. if (!ale_entries)
  1332. return ERR_PTR(-EINVAL);
  1333. ale_entries *= ALE_TABLE_SIZE_MULTIPLIER;
  1334. ale->params.ale_entries = ale_entries;
  1335. }
  1336. if (ale->features & CPSW_ALE_F_STATUS_REG &&
  1337. !ale->params.num_policers) {
  1338. regmap_field_read(ale->fields[ALE_POLICERS], &policers);
  1339. if (!policers)
  1340. return ERR_PTR(-EINVAL);
  1341. policers *= ALE_POLICER_SIZE_MULTIPLIER;
  1342. ale->params.num_policers = policers;
  1343. }
  1344. dev_info(ale->params.dev,
  1345. "ALE Table size %ld, Policers %ld\n", ale->params.ale_entries,
  1346. ale->params.num_policers);
  1347. /* set default bits for existing h/w */
  1348. ale->port_mask_bits = ale->params.ale_ports;
  1349. ale->port_num_bits = order_base_2(ale->params.ale_ports);
  1350. ale->vlan_field_bits = ale->params.ale_ports;
  1351. /* Set defaults override for ALE on NetCP NU switch and for version
  1352. * 1R3
  1353. */
  1354. if (ale->params.nu_switch_ale) {
  1355. /* Separate registers for unknown vlan configuration.
  1356. * Also there are N bits, where N is number of ale
  1357. * ports and shift value should be 0
  1358. */
  1359. ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].bits =
  1360. ale->params.ale_ports;
  1361. ale_controls[ALE_PORT_UNKNOWN_VLAN_MEMBER].offset =
  1362. ALE_UNKNOWNVLAN_MEMBER;
  1363. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].bits =
  1364. ale->params.ale_ports;
  1365. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].shift = 0;
  1366. ale_controls[ALE_PORT_UNKNOWN_MCAST_FLOOD].offset =
  1367. ALE_UNKNOWNVLAN_UNREG_MCAST_FLOOD;
  1368. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].bits =
  1369. ale->params.ale_ports;
  1370. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].shift = 0;
  1371. ale_controls[ALE_PORT_UNKNOWN_REG_MCAST_FLOOD].offset =
  1372. ALE_UNKNOWNVLAN_REG_MCAST_FLOOD;
  1373. ale_controls[ALE_PORT_UNTAGGED_EGRESS].bits =
  1374. ale->params.ale_ports;
  1375. ale_controls[ALE_PORT_UNTAGGED_EGRESS].shift = 0;
  1376. ale_controls[ALE_PORT_UNTAGGED_EGRESS].offset =
  1377. ALE_UNKNOWNVLAN_FORCE_UNTAG_EGRESS;
  1378. }
  1379. cpsw_ale_control_set(ale, 0, ALE_CLEAR, 1);
  1380. return ale;
  1381. }
  1382. void cpsw_ale_dump(struct cpsw_ale *ale, u32 *data)
  1383. {
  1384. int i;
  1385. for (i = 0; i < ale->params.ale_entries; i++) {
  1386. cpsw_ale_read(ale, i, data);
  1387. data += ALE_ENTRY_WORDS;
  1388. }
  1389. }
  1390. void cpsw_ale_restore(struct cpsw_ale *ale, u32 *data)
  1391. {
  1392. int i;
  1393. for (i = 0; i < ale->params.ale_entries; i++) {
  1394. cpsw_ale_write(ale, i, data);
  1395. data += ALE_ENTRY_WORDS;
  1396. }
  1397. }
  1398. u32 cpsw_ale_get_num_entries(struct cpsw_ale *ale)
  1399. {
  1400. return ale ? ale->params.ale_entries : 0;
  1401. }
  1402. /* Reads the specified policer index into ALE POLICER registers */
  1403. static void cpsw_ale_policer_read_idx(struct cpsw_ale *ale, u32 idx)
  1404. {
  1405. idx &= ALE_POLICER_TBL_INDEX_MASK;
  1406. writel_relaxed(idx, ale->params.ale_regs + ALE_POLICER_TBL_CTL);
  1407. }
  1408. /* Writes the ALE POLICER registers into the specified policer index */
  1409. static void cpsw_ale_policer_write_idx(struct cpsw_ale *ale, u32 idx)
  1410. {
  1411. idx &= ALE_POLICER_TBL_INDEX_MASK;
  1412. idx |= ALE_POLICER_TBL_WRITE_ENABLE;
  1413. writel_relaxed(idx, ale->params.ale_regs + ALE_POLICER_TBL_CTL);
  1414. }
  1415. /* enables/disables the custom thread value for the specified policer index */
  1416. static void cpsw_ale_policer_thread_idx_enable(struct cpsw_ale *ale, u32 idx,
  1417. u32 thread_id, bool enable)
  1418. {
  1419. regmap_field_write(ale->fields[ALE_THREAD_CLASS_INDEX], idx);
  1420. regmap_field_write(ale->fields[ALE_THREAD_VALUE], thread_id);
  1421. regmap_field_write(ale->fields[ALE_THREAD_ENABLE], enable ? 1 : 0);
  1422. }
  1423. /* Disable all policer entries and thread mappings */
  1424. static void cpsw_ale_policer_reset(struct cpsw_ale *ale)
  1425. {
  1426. int i;
  1427. for (i = 0; i < ale->params.num_policers ; i++) {
  1428. cpsw_ale_policer_read_idx(ale, i);
  1429. regmap_field_write(ale->fields[POL_PORT_MEN], 0);
  1430. regmap_field_write(ale->fields[POL_PRI_MEN], 0);
  1431. regmap_field_write(ale->fields[POL_OUI_MEN], 0);
  1432. regmap_field_write(ale->fields[POL_DST_MEN], 0);
  1433. regmap_field_write(ale->fields[POL_SRC_MEN], 0);
  1434. regmap_field_write(ale->fields[POL_OVLAN_MEN], 0);
  1435. regmap_field_write(ale->fields[POL_IVLAN_MEN], 0);
  1436. regmap_field_write(ale->fields[POL_ETHERTYPE_MEN], 0);
  1437. regmap_field_write(ale->fields[POL_IPSRC_MEN], 0);
  1438. regmap_field_write(ale->fields[POL_IPDST_MEN], 0);
  1439. regmap_field_write(ale->fields[POL_EN], 0);
  1440. regmap_field_write(ale->fields[POL_RED_DROP_EN], 0);
  1441. regmap_field_write(ale->fields[POL_YELLOW_DROP_EN], 0);
  1442. regmap_field_write(ale->fields[POL_PRIORITY_THREAD_EN], 0);
  1443. cpsw_ale_policer_thread_idx_enable(ale, i, 0, 0);
  1444. }
  1445. }
  1446. /* Default classifier is to map 8 user priorities to N receive channels */
  1447. void cpsw_ale_classifier_setup_default(struct cpsw_ale *ale, int num_rx_ch)
  1448. {
  1449. int pri, idx;
  1450. /* Reference:
  1451. * IEEE802.1Q-2014, Standard for Local and metropolitan area networks
  1452. * Table I-2 - Traffic type acronyms
  1453. * Table I-3 - Defining traffic types
  1454. * Section I.4 Traffic types and priority values, states:
  1455. * "0 is thus used both for default priority and for Best Effort, and
  1456. * Background is associated with a priority value of 1. This means
  1457. * that the value 1 effectively communicates a lower priority than 0."
  1458. *
  1459. * In the table below, Priority Code Point (PCP) 0 is assigned
  1460. * to a higher priority thread than PCP 1 wherever possible.
  1461. * The table maps which thread the PCP traffic needs to be
  1462. * sent to for a given number of threads (RX channels). Upper threads
  1463. * have higher priority.
  1464. * e.g. if number of threads is 8 then user priority 0 will map to
  1465. * pri_thread_map[8-1][0] i.e. thread 1
  1466. */
  1467. int pri_thread_map[8][8] = { /* BK,BE,EE,CA,VI,VO,IC,NC */
  1468. { 0, 0, 0, 0, 0, 0, 0, 0, },
  1469. { 0, 0, 0, 0, 1, 1, 1, 1, },
  1470. { 0, 0, 0, 0, 1, 1, 2, 2, },
  1471. { 0, 0, 1, 1, 2, 2, 3, 3, },
  1472. { 0, 0, 1, 1, 2, 2, 3, 4, },
  1473. { 1, 0, 2, 2, 3, 3, 4, 5, },
  1474. { 1, 0, 2, 3, 4, 4, 5, 6, },
  1475. { 1, 0, 2, 3, 4, 5, 6, 7 } };
  1476. cpsw_ale_policer_reset(ale);
  1477. /* use first 8 classifiers to map 8 (DSCP/PCP) priorities to channels */
  1478. for (pri = 0; pri < 8; pri++) {
  1479. idx = pri;
  1480. /* Classifier 'idx' match on priority 'pri' */
  1481. cpsw_ale_policer_read_idx(ale, idx);
  1482. regmap_field_write(ale->fields[POL_PRI_VAL], pri);
  1483. regmap_field_write(ale->fields[POL_PRI_MEN], 1);
  1484. cpsw_ale_policer_write_idx(ale, idx);
  1485. /* Map Classifier 'idx' to thread provided by the map */
  1486. cpsw_ale_policer_thread_idx_enable(ale, idx,
  1487. pri_thread_map[num_rx_ch - 1][pri],
  1488. 1);
  1489. }
  1490. }