ocelot_vcap.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /* Microsemi Ocelot Switch driver
  3. * Copyright (c) 2019 Microsemi Corporation
  4. */
  5. #include <linux/iopoll.h>
  6. #include <linux/proc_fs.h>
  7. #include <soc/mscc/ocelot_vcap.h>
  8. #include "ocelot_police.h"
  9. #include "ocelot_vcap.h"
  10. #define ENTRY_WIDTH 32
  11. enum vcap_sel {
  12. VCAP_SEL_ENTRY = 0x1,
  13. VCAP_SEL_ACTION = 0x2,
  14. VCAP_SEL_COUNTER = 0x4,
  15. VCAP_SEL_ALL = 0x7,
  16. };
  17. enum vcap_cmd {
  18. VCAP_CMD_WRITE = 0, /* Copy from Cache to TCAM */
  19. VCAP_CMD_READ = 1, /* Copy from TCAM to Cache */
  20. VCAP_CMD_MOVE_UP = 2, /* Move <count> up */
  21. VCAP_CMD_MOVE_DOWN = 3, /* Move <count> down */
  22. VCAP_CMD_INITIALIZE = 4, /* Write all (from cache) */
  23. };
  24. #define VCAP_ENTRY_WIDTH 12 /* Max entry width (32bit words) */
  25. #define VCAP_COUNTER_WIDTH 4 /* Max counter width (32bit words) */
  26. struct vcap_data {
  27. u32 entry[VCAP_ENTRY_WIDTH]; /* ENTRY_DAT */
  28. u32 mask[VCAP_ENTRY_WIDTH]; /* MASK_DAT */
  29. u32 action[VCAP_ENTRY_WIDTH]; /* ACTION_DAT */
  30. u32 counter[VCAP_COUNTER_WIDTH]; /* CNT_DAT */
  31. u32 tg; /* TG_DAT */
  32. u32 type; /* Action type */
  33. u32 tg_sw; /* Current type-group */
  34. u32 cnt; /* Current counter */
  35. u32 key_offset; /* Current entry offset */
  36. u32 action_offset; /* Current action offset */
  37. u32 counter_offset; /* Current counter offset */
  38. u32 tg_value; /* Current type-group value */
  39. u32 tg_mask; /* Current type-group mask */
  40. };
  41. static u32 vcap_read_update_ctrl(struct ocelot *ocelot,
  42. const struct vcap_props *vcap)
  43. {
  44. return ocelot_target_read(ocelot, vcap->target, VCAP_CORE_UPDATE_CTRL);
  45. }
  46. static void vcap_cmd(struct ocelot *ocelot, const struct vcap_props *vcap,
  47. u16 ix, int cmd, int sel)
  48. {
  49. u32 value = (VCAP_CORE_UPDATE_CTRL_UPDATE_CMD(cmd) |
  50. VCAP_CORE_UPDATE_CTRL_UPDATE_ADDR(ix) |
  51. VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT);
  52. if ((sel & VCAP_SEL_ENTRY) && ix >= vcap->entry_count)
  53. return;
  54. if (!(sel & VCAP_SEL_ENTRY))
  55. value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ENTRY_DIS;
  56. if (!(sel & VCAP_SEL_ACTION))
  57. value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ACTION_DIS;
  58. if (!(sel & VCAP_SEL_COUNTER))
  59. value |= VCAP_CORE_UPDATE_CTRL_UPDATE_CNT_DIS;
  60. ocelot_target_write(ocelot, vcap->target, value, VCAP_CORE_UPDATE_CTRL);
  61. read_poll_timeout(vcap_read_update_ctrl, value,
  62. (value & VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT) == 0,
  63. 10, 100000, false, ocelot, vcap);
  64. }
  65. /* Convert from 0-based row to VCAP entry row and run command */
  66. static void vcap_row_cmd(struct ocelot *ocelot, const struct vcap_props *vcap,
  67. u32 row, int cmd, int sel)
  68. {
  69. vcap_cmd(ocelot, vcap, vcap->entry_count - row - 1, cmd, sel);
  70. }
  71. static void vcap_entry2cache(struct ocelot *ocelot,
  72. const struct vcap_props *vcap,
  73. struct vcap_data *data)
  74. {
  75. u32 entry_words, i;
  76. entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH);
  77. for (i = 0; i < entry_words; i++) {
  78. ocelot_target_write_rix(ocelot, vcap->target, data->entry[i],
  79. VCAP_CACHE_ENTRY_DAT, i);
  80. ocelot_target_write_rix(ocelot, vcap->target, ~data->mask[i],
  81. VCAP_CACHE_MASK_DAT, i);
  82. }
  83. ocelot_target_write(ocelot, vcap->target, data->tg, VCAP_CACHE_TG_DAT);
  84. }
  85. static void vcap_cache2entry(struct ocelot *ocelot,
  86. const struct vcap_props *vcap,
  87. struct vcap_data *data)
  88. {
  89. u32 entry_words, i;
  90. entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH);
  91. for (i = 0; i < entry_words; i++) {
  92. data->entry[i] = ocelot_target_read_rix(ocelot, vcap->target,
  93. VCAP_CACHE_ENTRY_DAT, i);
  94. // Invert mask
  95. data->mask[i] = ~ocelot_target_read_rix(ocelot, vcap->target,
  96. VCAP_CACHE_MASK_DAT, i);
  97. }
  98. data->tg = ocelot_target_read(ocelot, vcap->target, VCAP_CACHE_TG_DAT);
  99. }
  100. static void vcap_action2cache(struct ocelot *ocelot,
  101. const struct vcap_props *vcap,
  102. struct vcap_data *data)
  103. {
  104. u32 action_words, mask;
  105. int i, width;
  106. /* Encode action type */
  107. width = vcap->action_type_width;
  108. if (width) {
  109. mask = GENMASK(width, 0);
  110. data->action[0] = ((data->action[0] & ~mask) | data->type);
  111. }
  112. action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH);
  113. for (i = 0; i < action_words; i++)
  114. ocelot_target_write_rix(ocelot, vcap->target, data->action[i],
  115. VCAP_CACHE_ACTION_DAT, i);
  116. for (i = 0; i < vcap->counter_words; i++)
  117. ocelot_target_write_rix(ocelot, vcap->target, data->counter[i],
  118. VCAP_CACHE_CNT_DAT, i);
  119. }
  120. static void vcap_cache2action(struct ocelot *ocelot,
  121. const struct vcap_props *vcap,
  122. struct vcap_data *data)
  123. {
  124. u32 action_words;
  125. int i, width;
  126. action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH);
  127. for (i = 0; i < action_words; i++)
  128. data->action[i] = ocelot_target_read_rix(ocelot, vcap->target,
  129. VCAP_CACHE_ACTION_DAT,
  130. i);
  131. for (i = 0; i < vcap->counter_words; i++)
  132. data->counter[i] = ocelot_target_read_rix(ocelot, vcap->target,
  133. VCAP_CACHE_CNT_DAT,
  134. i);
  135. /* Extract action type */
  136. width = vcap->action_type_width;
  137. data->type = (width ? (data->action[0] & GENMASK(width, 0)) : 0);
  138. }
  139. /* Calculate offsets for entry */
  140. static void vcap_data_offset_get(const struct vcap_props *vcap,
  141. struct vcap_data *data, int ix)
  142. {
  143. int num_subwords_per_entry, num_subwords_per_action;
  144. int i, col, offset, num_entries_per_row, base;
  145. u32 width = vcap->tg_width;
  146. switch (data->tg_sw) {
  147. case VCAP_TG_FULL:
  148. num_entries_per_row = 1;
  149. break;
  150. case VCAP_TG_HALF:
  151. num_entries_per_row = 2;
  152. break;
  153. case VCAP_TG_QUARTER:
  154. num_entries_per_row = 4;
  155. break;
  156. default:
  157. return;
  158. }
  159. col = (ix % num_entries_per_row);
  160. num_subwords_per_entry = (vcap->sw_count / num_entries_per_row);
  161. base = (vcap->sw_count - col * num_subwords_per_entry -
  162. num_subwords_per_entry);
  163. data->tg_value = 0;
  164. data->tg_mask = 0;
  165. for (i = 0; i < num_subwords_per_entry; i++) {
  166. offset = ((base + i) * width);
  167. data->tg_value |= (data->tg_sw << offset);
  168. data->tg_mask |= GENMASK(offset + width - 1, offset);
  169. }
  170. /* Calculate key/action/counter offsets */
  171. col = (num_entries_per_row - col - 1);
  172. data->key_offset = (base * vcap->entry_width) / vcap->sw_count;
  173. data->counter_offset = (num_subwords_per_entry * col *
  174. vcap->counter_width);
  175. i = data->type;
  176. width = vcap->action_table[i].width;
  177. num_subwords_per_action = vcap->action_table[i].count;
  178. data->action_offset = ((num_subwords_per_action * col * width) /
  179. num_entries_per_row);
  180. data->action_offset += vcap->action_type_width;
  181. }
  182. static void vcap_data_set(u32 *data, u32 offset, u32 len, u32 value)
  183. {
  184. u32 i, v, m;
  185. for (i = 0; i < len; i++, offset++) {
  186. v = data[offset / ENTRY_WIDTH];
  187. m = (1 << (offset % ENTRY_WIDTH));
  188. if (value & (1 << i))
  189. v |= m;
  190. else
  191. v &= ~m;
  192. data[offset / ENTRY_WIDTH] = v;
  193. }
  194. }
  195. static u32 vcap_data_get(u32 *data, u32 offset, u32 len)
  196. {
  197. u32 i, v, m, value = 0;
  198. for (i = 0; i < len; i++, offset++) {
  199. v = data[offset / ENTRY_WIDTH];
  200. m = (1 << (offset % ENTRY_WIDTH));
  201. if (v & m)
  202. value |= (1 << i);
  203. }
  204. return value;
  205. }
  206. static void vcap_key_field_set(struct vcap_data *data, u32 offset, u32 width,
  207. u32 value, u32 mask)
  208. {
  209. vcap_data_set(data->entry, offset + data->key_offset, width, value);
  210. vcap_data_set(data->mask, offset + data->key_offset, width, mask);
  211. }
  212. static void vcap_key_set(const struct vcap_props *vcap, struct vcap_data *data,
  213. int field, u32 value, u32 mask)
  214. {
  215. u32 offset = vcap->keys[field].offset;
  216. u32 length = vcap->keys[field].length;
  217. vcap_key_field_set(data, offset, length, value, mask);
  218. }
  219. static void vcap_key_bytes_set(const struct vcap_props *vcap,
  220. struct vcap_data *data, int field,
  221. u8 *val, u8 *msk)
  222. {
  223. u32 offset = vcap->keys[field].offset;
  224. u32 count = vcap->keys[field].length;
  225. u32 i, j, n = 0, value = 0, mask = 0;
  226. WARN_ON(count % 8);
  227. /* Data wider than 32 bits are split up in chunks of maximum 32 bits.
  228. * The 32 LSB of the data are written to the 32 MSB of the TCAM.
  229. */
  230. offset += count;
  231. count /= 8;
  232. for (i = 0; i < count; i++) {
  233. j = (count - i - 1);
  234. value += (val[j] << n);
  235. mask += (msk[j] << n);
  236. n += 8;
  237. if (n == ENTRY_WIDTH || (i + 1) == count) {
  238. offset -= n;
  239. vcap_key_field_set(data, offset, n, value, mask);
  240. n = 0;
  241. value = 0;
  242. mask = 0;
  243. }
  244. }
  245. }
  246. static void vcap_key_l4_port_set(const struct vcap_props *vcap,
  247. struct vcap_data *data, int field,
  248. struct ocelot_vcap_udp_tcp *port)
  249. {
  250. u32 offset = vcap->keys[field].offset;
  251. u32 length = vcap->keys[field].length;
  252. WARN_ON(length != 16);
  253. vcap_key_field_set(data, offset, length, port->value, port->mask);
  254. }
  255. static void vcap_key_bit_set(const struct vcap_props *vcap,
  256. struct vcap_data *data, int field,
  257. enum ocelot_vcap_bit val)
  258. {
  259. u32 value = (val == OCELOT_VCAP_BIT_1 ? 1 : 0);
  260. u32 msk = (val == OCELOT_VCAP_BIT_ANY ? 0 : 1);
  261. u32 offset = vcap->keys[field].offset;
  262. u32 length = vcap->keys[field].length;
  263. WARN_ON(length != 1);
  264. vcap_key_field_set(data, offset, length, value, msk);
  265. }
  266. static void vcap_action_set(const struct vcap_props *vcap,
  267. struct vcap_data *data, int field, u32 value)
  268. {
  269. int offset = vcap->actions[field].offset;
  270. int length = vcap->actions[field].length;
  271. vcap_data_set(data->action, offset + data->action_offset, length,
  272. value);
  273. }
  274. static void is2_action_set(struct ocelot *ocelot, struct vcap_data *data,
  275. struct ocelot_vcap_filter *filter)
  276. {
  277. const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
  278. struct ocelot_vcap_action *a = &filter->action;
  279. vcap_action_set(vcap, data, VCAP_IS2_ACT_MASK_MODE, a->mask_mode);
  280. vcap_action_set(vcap, data, VCAP_IS2_ACT_PORT_MASK, a->port_mask);
  281. vcap_action_set(vcap, data, VCAP_IS2_ACT_MIRROR_ENA, a->mirror_ena);
  282. vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_ENA, a->police_ena);
  283. vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_IDX, a->pol_ix);
  284. vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_QU_NUM, a->cpu_qu_num);
  285. vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_COPY_ENA, a->cpu_copy_ena);
  286. }
  287. static void is2_entry_set(struct ocelot *ocelot, int ix,
  288. struct ocelot_vcap_filter *filter)
  289. {
  290. const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2];
  291. struct ocelot_vcap_key_vlan *tag = &filter->vlan;
  292. u32 val, msk, type, type_mask = 0xf, i, count;
  293. struct ocelot_vcap_u64 payload;
  294. struct vcap_data data;
  295. int row = (ix / 2);
  296. memset(&payload, 0, sizeof(payload));
  297. memset(&data, 0, sizeof(data));
  298. /* Read row */
  299. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
  300. vcap_cache2entry(ocelot, vcap, &data);
  301. vcap_cache2action(ocelot, vcap, &data);
  302. data.tg_sw = VCAP_TG_HALF;
  303. vcap_data_offset_get(vcap, &data, ix);
  304. data.tg = (data.tg & ~data.tg_mask);
  305. if (filter->prio != 0)
  306. data.tg |= data.tg_value;
  307. data.type = IS2_ACTION_TYPE_NORMAL;
  308. vcap_key_set(vcap, &data, VCAP_IS2_HK_PAG, filter->pag, 0xff);
  309. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST,
  310. (filter->lookup == 0) ? OCELOT_VCAP_BIT_1 :
  311. OCELOT_VCAP_BIT_0);
  312. vcap_key_set(vcap, &data, VCAP_IS2_HK_IGR_PORT_MASK, 0,
  313. ~filter->ingress_port_mask);
  314. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_HOST_MATCH,
  315. OCELOT_VCAP_BIT_ANY);
  316. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_MC, filter->dmac_mc);
  317. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_BC, filter->dmac_bc);
  318. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_VLAN_TAGGED, tag->tagged);
  319. vcap_key_set(vcap, &data, VCAP_IS2_HK_VID,
  320. tag->vid.value, tag->vid.mask);
  321. vcap_key_set(vcap, &data, VCAP_IS2_HK_PCP,
  322. tag->pcp.value[0], tag->pcp.mask[0]);
  323. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DEI, tag->dei);
  324. switch (filter->key_type) {
  325. case OCELOT_VCAP_KEY_ETYPE: {
  326. struct ocelot_vcap_key_etype *etype = &filter->key.etype;
  327. type = IS2_TYPE_ETYPE;
  328. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
  329. etype->dmac.value, etype->dmac.mask);
  330. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
  331. etype->smac.value, etype->smac.mask);
  332. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_ETYPE,
  333. etype->etype.value, etype->etype.mask);
  334. /* Clear unused bits */
  335. vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
  336. 0, 0);
  337. vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1,
  338. 0, 0);
  339. vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2,
  340. 0, 0);
  341. vcap_key_bytes_set(vcap, &data,
  342. VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0,
  343. etype->data.value, etype->data.mask);
  344. break;
  345. }
  346. case OCELOT_VCAP_KEY_LLC: {
  347. struct ocelot_vcap_key_llc *llc = &filter->key.llc;
  348. type = IS2_TYPE_LLC;
  349. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
  350. llc->dmac.value, llc->dmac.mask);
  351. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
  352. llc->smac.value, llc->smac.mask);
  353. for (i = 0; i < 4; i++) {
  354. payload.value[i] = llc->llc.value[i];
  355. payload.mask[i] = llc->llc.mask[i];
  356. }
  357. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_LLC_L2_LLC,
  358. payload.value, payload.mask);
  359. break;
  360. }
  361. case OCELOT_VCAP_KEY_SNAP: {
  362. struct ocelot_vcap_key_snap *snap = &filter->key.snap;
  363. type = IS2_TYPE_SNAP;
  364. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC,
  365. snap->dmac.value, snap->dmac.mask);
  366. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC,
  367. snap->smac.value, snap->smac.mask);
  368. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_SNAP_L2_SNAP,
  369. filter->key.snap.snap.value,
  370. filter->key.snap.snap.mask);
  371. break;
  372. }
  373. case OCELOT_VCAP_KEY_ARP: {
  374. struct ocelot_vcap_key_arp *arp = &filter->key.arp;
  375. type = IS2_TYPE_ARP;
  376. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_SMAC,
  377. arp->smac.value, arp->smac.mask);
  378. vcap_key_bit_set(vcap, &data,
  379. VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK,
  380. arp->ethernet);
  381. vcap_key_bit_set(vcap, &data,
  382. VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK,
  383. arp->ip);
  384. vcap_key_bit_set(vcap, &data,
  385. VCAP_IS2_HK_MAC_ARP_LEN_OK,
  386. arp->length);
  387. vcap_key_bit_set(vcap, &data,
  388. VCAP_IS2_HK_MAC_ARP_TARGET_MATCH,
  389. arp->dmac_match);
  390. vcap_key_bit_set(vcap, &data,
  391. VCAP_IS2_HK_MAC_ARP_SENDER_MATCH,
  392. arp->smac_match);
  393. vcap_key_bit_set(vcap, &data,
  394. VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN,
  395. arp->unknown);
  396. /* OPCODE is inverse, bit 0 is reply flag, bit 1 is RARP flag */
  397. val = ((arp->req == OCELOT_VCAP_BIT_0 ? 1 : 0) |
  398. (arp->arp == OCELOT_VCAP_BIT_0 ? 2 : 0));
  399. msk = ((arp->req == OCELOT_VCAP_BIT_ANY ? 0 : 1) |
  400. (arp->arp == OCELOT_VCAP_BIT_ANY ? 0 : 2));
  401. vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_OPCODE,
  402. val, msk);
  403. vcap_key_bytes_set(vcap, &data,
  404. VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP,
  405. arp->dip.value.addr, arp->dip.mask.addr);
  406. vcap_key_bytes_set(vcap, &data,
  407. VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP,
  408. arp->sip.value.addr, arp->sip.mask.addr);
  409. vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP,
  410. 0, 0);
  411. break;
  412. }
  413. case OCELOT_VCAP_KEY_IPV4:
  414. case OCELOT_VCAP_KEY_IPV6: {
  415. enum ocelot_vcap_bit sip_eq_dip, sport_eq_dport, seq_zero, tcp;
  416. enum ocelot_vcap_bit ttl, fragment, options, tcp_ack, tcp_urg;
  417. enum ocelot_vcap_bit tcp_fin, tcp_syn, tcp_rst, tcp_psh;
  418. struct ocelot_vcap_key_ipv4 *ipv4 = NULL;
  419. struct ocelot_vcap_key_ipv6 *ipv6 = NULL;
  420. struct ocelot_vcap_udp_tcp *sport, *dport;
  421. struct ocelot_vcap_ipv4 sip, dip;
  422. struct ocelot_vcap_u8 proto, ds;
  423. struct ocelot_vcap_u48 *ip_data;
  424. if (filter->key_type == OCELOT_VCAP_KEY_IPV4) {
  425. ipv4 = &filter->key.ipv4;
  426. ttl = ipv4->ttl;
  427. fragment = ipv4->fragment;
  428. options = ipv4->options;
  429. proto = ipv4->proto;
  430. ds = ipv4->ds;
  431. ip_data = &ipv4->data;
  432. sip = ipv4->sip;
  433. dip = ipv4->dip;
  434. sport = &ipv4->sport;
  435. dport = &ipv4->dport;
  436. tcp_fin = ipv4->tcp_fin;
  437. tcp_syn = ipv4->tcp_syn;
  438. tcp_rst = ipv4->tcp_rst;
  439. tcp_psh = ipv4->tcp_psh;
  440. tcp_ack = ipv4->tcp_ack;
  441. tcp_urg = ipv4->tcp_urg;
  442. sip_eq_dip = ipv4->sip_eq_dip;
  443. sport_eq_dport = ipv4->sport_eq_dport;
  444. seq_zero = ipv4->seq_zero;
  445. } else {
  446. ipv6 = &filter->key.ipv6;
  447. ttl = ipv6->ttl;
  448. fragment = OCELOT_VCAP_BIT_ANY;
  449. options = OCELOT_VCAP_BIT_ANY;
  450. proto = ipv6->proto;
  451. ds = ipv6->ds;
  452. ip_data = &ipv6->data;
  453. for (i = 0; i < 8; i++) {
  454. val = ipv6->sip.value[i + 8];
  455. msk = ipv6->sip.mask[i + 8];
  456. if (i < 4) {
  457. dip.value.addr[i] = val;
  458. dip.mask.addr[i] = msk;
  459. } else {
  460. sip.value.addr[i - 4] = val;
  461. sip.mask.addr[i - 4] = msk;
  462. }
  463. }
  464. sport = &ipv6->sport;
  465. dport = &ipv6->dport;
  466. tcp_fin = ipv6->tcp_fin;
  467. tcp_syn = ipv6->tcp_syn;
  468. tcp_rst = ipv6->tcp_rst;
  469. tcp_psh = ipv6->tcp_psh;
  470. tcp_ack = ipv6->tcp_ack;
  471. tcp_urg = ipv6->tcp_urg;
  472. sip_eq_dip = ipv6->sip_eq_dip;
  473. sport_eq_dport = ipv6->sport_eq_dport;
  474. seq_zero = ipv6->seq_zero;
  475. }
  476. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4,
  477. ipv4 ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
  478. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_FRAGMENT,
  479. fragment);
  480. vcap_key_set(vcap, &data, VCAP_IS2_HK_L3_FRAG_OFS_GT0, 0, 0);
  481. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_OPTIONS,
  482. options);
  483. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4_L3_TTL_GT0,
  484. ttl);
  485. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_TOS,
  486. ds.value, ds.mask);
  487. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_DIP,
  488. dip.value.addr, dip.mask.addr);
  489. vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_SIP,
  490. sip.value.addr, sip.mask.addr);
  491. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DIP_EQ_SIP,
  492. sip_eq_dip);
  493. val = proto.value[0];
  494. msk = proto.mask[0];
  495. type = IS2_TYPE_IP_UDP_TCP;
  496. if (msk == 0xff && (val == IPPROTO_TCP || val == IPPROTO_UDP)) {
  497. /* UDP/TCP protocol match */
  498. tcp = (val == IPPROTO_TCP ?
  499. OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
  500. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_TCP, tcp);
  501. vcap_key_l4_port_set(vcap, &data,
  502. VCAP_IS2_HK_L4_DPORT, dport);
  503. vcap_key_l4_port_set(vcap, &data,
  504. VCAP_IS2_HK_L4_SPORT, sport);
  505. vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_RNG, 0, 0);
  506. vcap_key_bit_set(vcap, &data,
  507. VCAP_IS2_HK_L4_SPORT_EQ_DPORT,
  508. sport_eq_dport);
  509. vcap_key_bit_set(vcap, &data,
  510. VCAP_IS2_HK_L4_SEQUENCE_EQ0,
  511. seq_zero);
  512. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_FIN,
  513. tcp_fin);
  514. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_SYN,
  515. tcp_syn);
  516. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_RST,
  517. tcp_rst);
  518. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_PSH,
  519. tcp_psh);
  520. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_ACK,
  521. tcp_ack);
  522. vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_URG,
  523. tcp_urg);
  524. vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_DOM,
  525. 0, 0);
  526. vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_VER,
  527. 0, 0);
  528. } else {
  529. if (msk == 0) {
  530. /* Any IP protocol match */
  531. type_mask = IS2_TYPE_MASK_IP_ANY;
  532. } else {
  533. /* Non-UDP/TCP protocol match */
  534. type = IS2_TYPE_IP_OTHER;
  535. for (i = 0; i < 6; i++) {
  536. payload.value[i] = ip_data->value[i];
  537. payload.mask[i] = ip_data->mask[i];
  538. }
  539. }
  540. vcap_key_bytes_set(vcap, &data,
  541. VCAP_IS2_HK_IP4_L3_PROTO,
  542. proto.value, proto.mask);
  543. vcap_key_bytes_set(vcap, &data,
  544. VCAP_IS2_HK_L3_PAYLOAD,
  545. payload.value, payload.mask);
  546. }
  547. break;
  548. }
  549. case OCELOT_VCAP_KEY_ANY:
  550. default:
  551. type = 0;
  552. type_mask = 0;
  553. count = vcap->entry_width / 2;
  554. /* Iterate over the non-common part of the key and
  555. * clear entry data
  556. */
  557. for (i = vcap->keys[VCAP_IS2_HK_L2_DMAC].offset;
  558. i < count; i += ENTRY_WIDTH) {
  559. vcap_key_field_set(&data, i, min(32u, count - i), 0, 0);
  560. }
  561. break;
  562. }
  563. vcap_key_set(vcap, &data, VCAP_IS2_TYPE, type, type_mask);
  564. is2_action_set(ocelot, &data, filter);
  565. vcap_data_set(data.counter, data.counter_offset,
  566. vcap->counter_width, filter->stats.pkts);
  567. /* Write row */
  568. vcap_entry2cache(ocelot, vcap, &data);
  569. vcap_action2cache(ocelot, vcap, &data);
  570. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
  571. }
  572. static void is1_action_set(struct ocelot *ocelot, struct vcap_data *data,
  573. const struct ocelot_vcap_filter *filter)
  574. {
  575. const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
  576. const struct ocelot_vcap_action *a = &filter->action;
  577. vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_REPLACE_ENA,
  578. a->vid_replace_ena);
  579. vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_ADD_VAL, a->vid);
  580. vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT_ENA,
  581. a->vlan_pop_cnt_ena);
  582. vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT,
  583. a->vlan_pop_cnt);
  584. vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_DEI_ENA, a->pcp_dei_ena);
  585. vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_VAL, a->pcp);
  586. vcap_action_set(vcap, data, VCAP_IS1_ACT_DEI_VAL, a->dei);
  587. vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_ENA, a->qos_ena);
  588. vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_VAL, a->qos_val);
  589. vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_OVERRIDE_MASK,
  590. a->pag_override_mask);
  591. vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_VAL, a->pag_val);
  592. }
  593. static void is1_entry_set(struct ocelot *ocelot, int ix,
  594. struct ocelot_vcap_filter *filter)
  595. {
  596. const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1];
  597. struct ocelot_vcap_key_vlan *tag = &filter->vlan;
  598. struct vcap_data data;
  599. int row = ix / 2;
  600. u32 type;
  601. memset(&data, 0, sizeof(data));
  602. /* Read row */
  603. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
  604. vcap_cache2entry(ocelot, vcap, &data);
  605. vcap_cache2action(ocelot, vcap, &data);
  606. data.tg_sw = VCAP_TG_HALF;
  607. data.type = IS1_ACTION_TYPE_NORMAL;
  608. vcap_data_offset_get(vcap, &data, ix);
  609. data.tg = (data.tg & ~data.tg_mask);
  610. if (filter->prio != 0)
  611. data.tg |= data.tg_value;
  612. vcap_key_set(vcap, &data, VCAP_IS1_HK_LOOKUP, filter->lookup, 0x3);
  613. vcap_key_set(vcap, &data, VCAP_IS1_HK_IGR_PORT_MASK, 0,
  614. ~filter->ingress_port_mask);
  615. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_MC, filter->dmac_mc);
  616. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_BC, filter->dmac_bc);
  617. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_VLAN_TAGGED, tag->tagged);
  618. vcap_key_set(vcap, &data, VCAP_IS1_HK_VID,
  619. tag->vid.value, tag->vid.mask);
  620. vcap_key_set(vcap, &data, VCAP_IS1_HK_PCP,
  621. tag->pcp.value[0], tag->pcp.mask[0]);
  622. type = IS1_TYPE_S1_NORMAL;
  623. switch (filter->key_type) {
  624. case OCELOT_VCAP_KEY_ETYPE: {
  625. struct ocelot_vcap_key_etype *etype = &filter->key.etype;
  626. vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L2_SMAC,
  627. etype->smac.value, etype->smac.mask);
  628. vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
  629. etype->etype.value, etype->etype.mask);
  630. break;
  631. }
  632. case OCELOT_VCAP_KEY_IPV4: {
  633. struct ocelot_vcap_key_ipv4 *ipv4 = &filter->key.ipv4;
  634. struct ocelot_vcap_udp_tcp *sport = &ipv4->sport;
  635. struct ocelot_vcap_udp_tcp *dport = &ipv4->dport;
  636. enum ocelot_vcap_bit tcp_udp = OCELOT_VCAP_BIT_0;
  637. struct ocelot_vcap_u8 proto = ipv4->proto;
  638. struct ocelot_vcap_ipv4 sip = ipv4->sip;
  639. u32 val, msk;
  640. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP_SNAP,
  641. OCELOT_VCAP_BIT_1);
  642. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP4,
  643. OCELOT_VCAP_BIT_1);
  644. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_ETYPE_LEN,
  645. OCELOT_VCAP_BIT_1);
  646. vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L3_IP4_SIP,
  647. sip.value.addr, sip.mask.addr);
  648. val = proto.value[0];
  649. msk = proto.mask[0];
  650. if ((val == NEXTHDR_TCP || val == NEXTHDR_UDP) && msk == 0xff)
  651. tcp_udp = OCELOT_VCAP_BIT_1;
  652. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP_UDP, tcp_udp);
  653. if (tcp_udp) {
  654. enum ocelot_vcap_bit tcp = OCELOT_VCAP_BIT_0;
  655. if (val == NEXTHDR_TCP)
  656. tcp = OCELOT_VCAP_BIT_1;
  657. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP, tcp);
  658. vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_L4_SPORT,
  659. sport);
  660. /* Overloaded field */
  661. vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_ETYPE,
  662. dport);
  663. } else {
  664. /* IPv4 "other" frame */
  665. struct ocelot_vcap_u16 etype = {0};
  666. /* Overloaded field */
  667. etype.value[0] = proto.value[0];
  668. etype.mask[0] = proto.mask[0];
  669. vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
  670. etype.value, etype.mask);
  671. }
  672. break;
  673. }
  674. default:
  675. break;
  676. }
  677. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TYPE,
  678. type ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
  679. is1_action_set(ocelot, &data, filter);
  680. vcap_data_set(data.counter, data.counter_offset,
  681. vcap->counter_width, filter->stats.pkts);
  682. /* Write row */
  683. vcap_entry2cache(ocelot, vcap, &data);
  684. vcap_action2cache(ocelot, vcap, &data);
  685. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
  686. }
  687. static void es0_action_set(struct ocelot *ocelot, struct vcap_data *data,
  688. const struct ocelot_vcap_filter *filter)
  689. {
  690. const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
  691. const struct ocelot_vcap_action *a = &filter->action;
  692. vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_OUTER_TAG,
  693. a->push_outer_tag);
  694. vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_INNER_TAG,
  695. a->push_inner_tag);
  696. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_TPID_SEL,
  697. a->tag_a_tpid_sel);
  698. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_VID_SEL,
  699. a->tag_a_vid_sel);
  700. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_PCP_SEL,
  701. a->tag_a_pcp_sel);
  702. vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_A_VAL, a->vid_a_val);
  703. vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_A_VAL, a->pcp_a_val);
  704. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_TPID_SEL,
  705. a->tag_b_tpid_sel);
  706. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_VID_SEL,
  707. a->tag_b_vid_sel);
  708. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_PCP_SEL,
  709. a->tag_b_pcp_sel);
  710. vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_B_VAL, a->vid_b_val);
  711. vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_B_VAL, a->pcp_b_val);
  712. }
  713. static void es0_entry_set(struct ocelot *ocelot, int ix,
  714. struct ocelot_vcap_filter *filter)
  715. {
  716. const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
  717. struct ocelot_vcap_key_vlan *tag = &filter->vlan;
  718. struct vcap_data data;
  719. int row = ix;
  720. memset(&data, 0, sizeof(data));
  721. /* Read row */
  722. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
  723. vcap_cache2entry(ocelot, vcap, &data);
  724. vcap_cache2action(ocelot, vcap, &data);
  725. data.tg_sw = VCAP_TG_FULL;
  726. data.type = ES0_ACTION_TYPE_NORMAL;
  727. vcap_data_offset_get(vcap, &data, ix);
  728. data.tg = (data.tg & ~data.tg_mask);
  729. if (filter->prio != 0)
  730. data.tg |= data.tg_value;
  731. vcap_key_set(vcap, &data, VCAP_ES0_IGR_PORT, filter->ingress_port.value,
  732. filter->ingress_port.mask);
  733. vcap_key_set(vcap, &data, VCAP_ES0_EGR_PORT, filter->egress_port.value,
  734. filter->egress_port.mask);
  735. vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_MC, filter->dmac_mc);
  736. vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_BC, filter->dmac_bc);
  737. vcap_key_set(vcap, &data, VCAP_ES0_VID,
  738. tag->vid.value, tag->vid.mask);
  739. vcap_key_set(vcap, &data, VCAP_ES0_PCP,
  740. tag->pcp.value[0], tag->pcp.mask[0]);
  741. es0_action_set(ocelot, &data, filter);
  742. vcap_data_set(data.counter, data.counter_offset,
  743. vcap->counter_width, filter->stats.pkts);
  744. /* Write row */
  745. vcap_entry2cache(ocelot, vcap, &data);
  746. vcap_action2cache(ocelot, vcap, &data);
  747. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
  748. }
  749. static void vcap_entry_get(struct ocelot *ocelot, int ix,
  750. struct ocelot_vcap_filter *filter)
  751. {
  752. const struct vcap_props *vcap = &ocelot->vcap[filter->block_id];
  753. struct vcap_data data;
  754. int row, count;
  755. u32 cnt;
  756. if (filter->block_id == VCAP_ES0)
  757. data.tg_sw = VCAP_TG_FULL;
  758. else
  759. data.tg_sw = VCAP_TG_HALF;
  760. count = (1 << (data.tg_sw - 1));
  761. row = (ix / count);
  762. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER);
  763. vcap_cache2action(ocelot, vcap, &data);
  764. vcap_data_offset_get(vcap, &data, ix);
  765. cnt = vcap_data_get(data.counter, data.counter_offset,
  766. vcap->counter_width);
  767. filter->stats.pkts = cnt;
  768. }
  769. static void vcap_entry_set(struct ocelot *ocelot, int ix,
  770. struct ocelot_vcap_filter *filter)
  771. {
  772. if (filter->block_id == VCAP_IS1)
  773. return is1_entry_set(ocelot, ix, filter);
  774. if (filter->block_id == VCAP_IS2)
  775. return is2_entry_set(ocelot, ix, filter);
  776. if (filter->block_id == VCAP_ES0)
  777. return es0_entry_set(ocelot, ix, filter);
  778. }
  779. struct vcap_policer_entry {
  780. struct list_head list;
  781. refcount_t refcount;
  782. u32 pol_ix;
  783. };
  784. int ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix,
  785. struct ocelot_policer *pol)
  786. {
  787. struct qos_policer_conf pp = { 0 };
  788. struct vcap_policer_entry *tmp;
  789. int ret;
  790. if (!pol)
  791. return -EINVAL;
  792. pp.mode = MSCC_QOS_RATE_MODE_DATA;
  793. pp.pir = pol->rate;
  794. pp.pbs = pol->burst;
  795. list_for_each_entry(tmp, &ocelot->vcap_pol.pol_list, list)
  796. if (tmp->pol_ix == pol_ix) {
  797. refcount_inc(&tmp->refcount);
  798. return 0;
  799. }
  800. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  801. if (!tmp)
  802. return -ENOMEM;
  803. ret = qos_policer_conf_set(ocelot, pol_ix, &pp);
  804. if (ret) {
  805. kfree(tmp);
  806. return ret;
  807. }
  808. tmp->pol_ix = pol_ix;
  809. refcount_set(&tmp->refcount, 1);
  810. list_add_tail(&tmp->list, &ocelot->vcap_pol.pol_list);
  811. return 0;
  812. }
  813. EXPORT_SYMBOL(ocelot_vcap_policer_add);
  814. int ocelot_vcap_policer_del(struct ocelot *ocelot, u32 pol_ix)
  815. {
  816. struct qos_policer_conf pp = {0};
  817. struct vcap_policer_entry *tmp, *n;
  818. u8 z = 0;
  819. list_for_each_entry_safe(tmp, n, &ocelot->vcap_pol.pol_list, list)
  820. if (tmp->pol_ix == pol_ix) {
  821. z = refcount_dec_and_test(&tmp->refcount);
  822. if (z) {
  823. list_del(&tmp->list);
  824. kfree(tmp);
  825. }
  826. }
  827. if (z) {
  828. pp.mode = MSCC_QOS_RATE_MODE_DISABLED;
  829. return qos_policer_conf_set(ocelot, pol_ix, &pp);
  830. }
  831. return 0;
  832. }
  833. EXPORT_SYMBOL(ocelot_vcap_policer_del);
  834. static int
  835. ocelot_vcap_filter_add_aux_resources(struct ocelot *ocelot,
  836. struct ocelot_vcap_filter *filter,
  837. struct netlink_ext_ack *extack)
  838. {
  839. struct ocelot_mirror *m;
  840. int ret;
  841. if (filter->block_id == VCAP_IS2 && filter->action.mirror_ena) {
  842. m = ocelot_mirror_get(ocelot, filter->egress_port.value,
  843. extack);
  844. if (IS_ERR(m))
  845. return PTR_ERR(m);
  846. }
  847. if (filter->block_id == VCAP_IS2 && filter->action.police_ena) {
  848. ret = ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
  849. &filter->action.pol);
  850. if (ret)
  851. return ret;
  852. }
  853. return 0;
  854. }
  855. static void
  856. ocelot_vcap_filter_del_aux_resources(struct ocelot *ocelot,
  857. struct ocelot_vcap_filter *filter)
  858. {
  859. if (filter->block_id == VCAP_IS2 && filter->action.police_ena)
  860. ocelot_vcap_policer_del(ocelot, filter->action.pol_ix);
  861. if (filter->block_id == VCAP_IS2 && filter->action.mirror_ena)
  862. ocelot_mirror_put(ocelot);
  863. }
  864. static int ocelot_vcap_filter_add_to_block(struct ocelot *ocelot,
  865. struct ocelot_vcap_block *block,
  866. struct ocelot_vcap_filter *filter,
  867. struct netlink_ext_ack *extack)
  868. {
  869. struct list_head *pos = &block->rules;
  870. struct ocelot_vcap_filter *tmp;
  871. int ret;
  872. ret = ocelot_vcap_filter_add_aux_resources(ocelot, filter, extack);
  873. if (ret)
  874. return ret;
  875. block->count++;
  876. list_for_each_entry(tmp, &block->rules, list) {
  877. if (filter->prio < tmp->prio) {
  878. pos = &tmp->list;
  879. break;
  880. }
  881. }
  882. list_add_tail(&filter->list, pos);
  883. return 0;
  884. }
  885. static bool ocelot_vcap_filter_equal(const struct ocelot_vcap_filter *a,
  886. const struct ocelot_vcap_filter *b)
  887. {
  888. return !memcmp(&a->id, &b->id, sizeof(struct ocelot_vcap_id));
  889. }
  890. static int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block,
  891. struct ocelot_vcap_filter *filter)
  892. {
  893. struct ocelot_vcap_filter *tmp;
  894. int index = 0;
  895. list_for_each_entry(tmp, &block->rules, list) {
  896. if (ocelot_vcap_filter_equal(filter, tmp))
  897. return index;
  898. index++;
  899. }
  900. return -ENOENT;
  901. }
  902. static struct ocelot_vcap_filter*
  903. ocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block,
  904. int index)
  905. {
  906. struct ocelot_vcap_filter *tmp;
  907. int i = 0;
  908. list_for_each_entry(tmp, &block->rules, list) {
  909. if (i == index)
  910. return tmp;
  911. ++i;
  912. }
  913. return NULL;
  914. }
  915. struct ocelot_vcap_filter *
  916. ocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block,
  917. unsigned long cookie, bool tc_offload)
  918. {
  919. struct ocelot_vcap_filter *filter;
  920. list_for_each_entry(filter, &block->rules, list)
  921. if (filter->id.tc_offload == tc_offload &&
  922. filter->id.cookie == cookie)
  923. return filter;
  924. return NULL;
  925. }
  926. EXPORT_SYMBOL(ocelot_vcap_block_find_filter_by_id);
  927. /* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based
  928. * on destination and source MAC addresses, but only on higher-level protocol
  929. * information. The only frame types to match on keys containing MAC addresses
  930. * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames.
  931. *
  932. * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match
  933. * on MAC_ETYPE keys such as destination and source MAC on this ingress port.
  934. * However the setting has the side effect of making these frames not matching
  935. * on any _other_ keys than MAC_ETYPE ones.
  936. */
  937. static void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port,
  938. int lookup, bool on)
  939. {
  940. u32 val = 0;
  941. if (on)
  942. val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) |
  943. ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) |
  944. ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) |
  945. ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) |
  946. ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup));
  947. ocelot_rmw_gix(ocelot, val,
  948. ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) |
  949. ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) |
  950. ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) |
  951. ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) |
  952. ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup)),
  953. ANA_PORT_VCAP_S2_CFG, port);
  954. }
  955. static bool
  956. ocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter)
  957. {
  958. u16 proto, mask;
  959. if (filter->key_type != OCELOT_VCAP_KEY_ETYPE)
  960. return false;
  961. proto = ntohs(*(__be16 *)filter->key.etype.etype.value);
  962. mask = ntohs(*(__be16 *)filter->key.etype.etype.mask);
  963. /* ETH_P_ALL match, so all protocols below are included */
  964. if (mask == 0)
  965. return true;
  966. if (proto == ETH_P_ARP)
  967. return true;
  968. if (proto == ETH_P_IP)
  969. return true;
  970. if (proto == ETH_P_IPV6)
  971. return true;
  972. return false;
  973. }
  974. static bool
  975. ocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter)
  976. {
  977. if (filter->key_type == OCELOT_VCAP_KEY_SNAP)
  978. return true;
  979. if (filter->key_type == OCELOT_VCAP_KEY_ARP)
  980. return true;
  981. if (filter->key_type == OCELOT_VCAP_KEY_IPV4)
  982. return true;
  983. if (filter->key_type == OCELOT_VCAP_KEY_IPV6)
  984. return true;
  985. return false;
  986. }
  987. static bool
  988. ocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot,
  989. struct ocelot_vcap_filter *filter)
  990. {
  991. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  992. struct ocelot_vcap_filter *tmp;
  993. unsigned long port;
  994. int i;
  995. /* We only have the S2_IP_TCPUDP_DIS set of knobs for VCAP IS2 */
  996. if (filter->block_id != VCAP_IS2)
  997. return true;
  998. if (ocelot_vcap_is_problematic_mac_etype(filter)) {
  999. /* Search for any non-MAC_ETYPE rules on the port */
  1000. for (i = 0; i < block->count; i++) {
  1001. tmp = ocelot_vcap_block_find_filter_by_index(block, i);
  1002. if (tmp->ingress_port_mask & filter->ingress_port_mask &&
  1003. tmp->lookup == filter->lookup &&
  1004. ocelot_vcap_is_problematic_non_mac_etype(tmp))
  1005. return false;
  1006. }
  1007. for_each_set_bit(port, &filter->ingress_port_mask,
  1008. ocelot->num_phys_ports)
  1009. ocelot_match_all_as_mac_etype(ocelot, port,
  1010. filter->lookup, true);
  1011. } else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) {
  1012. /* Search for any MAC_ETYPE rules on the port */
  1013. for (i = 0; i < block->count; i++) {
  1014. tmp = ocelot_vcap_block_find_filter_by_index(block, i);
  1015. if (tmp->ingress_port_mask & filter->ingress_port_mask &&
  1016. tmp->lookup == filter->lookup &&
  1017. ocelot_vcap_is_problematic_mac_etype(tmp))
  1018. return false;
  1019. }
  1020. for_each_set_bit(port, &filter->ingress_port_mask,
  1021. ocelot->num_phys_ports)
  1022. ocelot_match_all_as_mac_etype(ocelot, port,
  1023. filter->lookup, false);
  1024. }
  1025. return true;
  1026. }
  1027. int ocelot_vcap_filter_add(struct ocelot *ocelot,
  1028. struct ocelot_vcap_filter *filter,
  1029. struct netlink_ext_ack *extack)
  1030. {
  1031. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  1032. int i, index, ret;
  1033. if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) {
  1034. NL_SET_ERR_MSG_MOD(extack,
  1035. "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules, use the other IS2 lookup");
  1036. return -EBUSY;
  1037. }
  1038. /* Add filter to the linked list */
  1039. ret = ocelot_vcap_filter_add_to_block(ocelot, block, filter, extack);
  1040. if (ret)
  1041. return ret;
  1042. /* Get the index of the inserted filter */
  1043. index = ocelot_vcap_block_get_filter_index(block, filter);
  1044. if (index < 0)
  1045. return index;
  1046. /* Move down the rules to make place for the new filter */
  1047. for (i = block->count - 1; i > index; i--) {
  1048. struct ocelot_vcap_filter *tmp;
  1049. tmp = ocelot_vcap_block_find_filter_by_index(block, i);
  1050. /* Read back the filter's counters before moving it */
  1051. vcap_entry_get(ocelot, i - 1, tmp);
  1052. vcap_entry_set(ocelot, i, tmp);
  1053. }
  1054. /* Now insert the new filter */
  1055. vcap_entry_set(ocelot, index, filter);
  1056. return 0;
  1057. }
  1058. EXPORT_SYMBOL(ocelot_vcap_filter_add);
  1059. static void ocelot_vcap_block_remove_filter(struct ocelot *ocelot,
  1060. struct ocelot_vcap_block *block,
  1061. struct ocelot_vcap_filter *filter)
  1062. {
  1063. struct ocelot_vcap_filter *tmp, *n;
  1064. list_for_each_entry_safe(tmp, n, &block->rules, list) {
  1065. if (ocelot_vcap_filter_equal(filter, tmp)) {
  1066. ocelot_vcap_filter_del_aux_resources(ocelot, tmp);
  1067. list_del(&tmp->list);
  1068. kfree(tmp);
  1069. }
  1070. }
  1071. block->count--;
  1072. }
  1073. int ocelot_vcap_filter_del(struct ocelot *ocelot,
  1074. struct ocelot_vcap_filter *filter)
  1075. {
  1076. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  1077. struct ocelot_vcap_filter del_filter;
  1078. int i, index;
  1079. /* Need to inherit the block_id so that vcap_entry_set()
  1080. * does not get confused and knows where to install it.
  1081. */
  1082. memset(&del_filter, 0, sizeof(del_filter));
  1083. del_filter.block_id = filter->block_id;
  1084. /* Gets index of the filter */
  1085. index = ocelot_vcap_block_get_filter_index(block, filter);
  1086. if (index < 0)
  1087. return index;
  1088. /* Delete filter */
  1089. ocelot_vcap_block_remove_filter(ocelot, block, filter);
  1090. /* Move up all the blocks over the deleted filter */
  1091. for (i = index; i < block->count; i++) {
  1092. struct ocelot_vcap_filter *tmp;
  1093. tmp = ocelot_vcap_block_find_filter_by_index(block, i);
  1094. /* Read back the filter's counters before moving it */
  1095. vcap_entry_get(ocelot, i + 1, tmp);
  1096. vcap_entry_set(ocelot, i, tmp);
  1097. }
  1098. /* Now delete the last filter, because it is duplicated */
  1099. vcap_entry_set(ocelot, block->count, &del_filter);
  1100. return 0;
  1101. }
  1102. EXPORT_SYMBOL(ocelot_vcap_filter_del);
  1103. int ocelot_vcap_filter_replace(struct ocelot *ocelot,
  1104. struct ocelot_vcap_filter *filter)
  1105. {
  1106. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  1107. int index;
  1108. index = ocelot_vcap_block_get_filter_index(block, filter);
  1109. if (index < 0)
  1110. return index;
  1111. vcap_entry_set(ocelot, index, filter);
  1112. return 0;
  1113. }
  1114. EXPORT_SYMBOL(ocelot_vcap_filter_replace);
  1115. int ocelot_vcap_filter_stats_update(struct ocelot *ocelot,
  1116. struct ocelot_vcap_filter *filter)
  1117. {
  1118. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  1119. struct ocelot_vcap_filter tmp;
  1120. int index;
  1121. index = ocelot_vcap_block_get_filter_index(block, filter);
  1122. if (index < 0)
  1123. return index;
  1124. vcap_entry_get(ocelot, index, filter);
  1125. /* After we get the result we need to clear the counters */
  1126. tmp = *filter;
  1127. tmp.stats.pkts = 0;
  1128. vcap_entry_set(ocelot, index, &tmp);
  1129. return 0;
  1130. }
  1131. static void ocelot_vcap_init_one(struct ocelot *ocelot,
  1132. const struct vcap_props *vcap)
  1133. {
  1134. struct vcap_data data;
  1135. memset(&data, 0, sizeof(data));
  1136. vcap_entry2cache(ocelot, vcap, &data);
  1137. ocelot_target_write(ocelot, vcap->target, vcap->entry_count,
  1138. VCAP_CORE_MV_CFG);
  1139. vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY);
  1140. vcap_action2cache(ocelot, vcap, &data);
  1141. ocelot_target_write(ocelot, vcap->target, vcap->action_count,
  1142. VCAP_CORE_MV_CFG);
  1143. vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE,
  1144. VCAP_SEL_ACTION | VCAP_SEL_COUNTER);
  1145. }
  1146. static void ocelot_vcap_detect_constants(struct ocelot *ocelot,
  1147. struct vcap_props *vcap)
  1148. {
  1149. int counter_memory_width;
  1150. int num_default_actions;
  1151. int version;
  1152. version = ocelot_target_read(ocelot, vcap->target,
  1153. VCAP_CONST_VCAP_VER);
  1154. /* Only version 0 VCAP supported for now */
  1155. if (WARN_ON(version != 0))
  1156. return;
  1157. /* Width in bits of type-group field */
  1158. vcap->tg_width = ocelot_target_read(ocelot, vcap->target,
  1159. VCAP_CONST_ENTRY_TG_WIDTH);
  1160. /* Number of subwords per TCAM row */
  1161. vcap->sw_count = ocelot_target_read(ocelot, vcap->target,
  1162. VCAP_CONST_ENTRY_SWCNT);
  1163. /* Number of rows in TCAM. There can be this many full keys, or double
  1164. * this number half keys, or 4 times this number quarter keys.
  1165. */
  1166. vcap->entry_count = ocelot_target_read(ocelot, vcap->target,
  1167. VCAP_CONST_ENTRY_CNT);
  1168. /* Assuming there are 4 subwords per TCAM row, their layout in the
  1169. * actual TCAM (not in the cache) would be:
  1170. *
  1171. * | SW 3 | TG 3 | SW 2 | TG 2 | SW 1 | TG 1 | SW 0 | TG 0 |
  1172. *
  1173. * (where SW=subword and TG=Type-Group).
  1174. *
  1175. * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM
  1176. * row. But when software accesses the TCAM through the cache
  1177. * registers, the Type-Group values are written through another set of
  1178. * registers VCAP_TG_DAT, and therefore, it appears as though the 4
  1179. * subwords are contiguous in the cache memory.
  1180. * Important mention: regardless of the number of key entries per row
  1181. * (and therefore of key size: 1 full key or 2 half keys or 4 quarter
  1182. * keys), software always has to configure 4 Type-Group values. For
  1183. * example, in the case of 1 full key, the driver needs to set all 4
  1184. * Type-Group to be full key.
  1185. *
  1186. * For this reason, we need to fix up the value that the hardware is
  1187. * giving us. We don't actually care about the width of the entry in
  1188. * the TCAM. What we care about is the width of the entry in the cache
  1189. * registers, which is how we get to interact with it. And since the
  1190. * VCAP_ENTRY_DAT cache registers access only the subwords and not the
  1191. * Type-Groups, this means we need to subtract the width of the
  1192. * Type-Groups when packing and unpacking key entry data in a TCAM row.
  1193. */
  1194. vcap->entry_width = ocelot_target_read(ocelot, vcap->target,
  1195. VCAP_CONST_ENTRY_WIDTH);
  1196. vcap->entry_width -= vcap->tg_width * vcap->sw_count;
  1197. num_default_actions = ocelot_target_read(ocelot, vcap->target,
  1198. VCAP_CONST_ACTION_DEF_CNT);
  1199. vcap->action_count = vcap->entry_count + num_default_actions;
  1200. vcap->action_width = ocelot_target_read(ocelot, vcap->target,
  1201. VCAP_CONST_ACTION_WIDTH);
  1202. /* The width of the counter memory, this is the complete width of all
  1203. * counter-fields associated with one full-word entry. There is one
  1204. * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of
  1205. * subwords.)
  1206. */
  1207. vcap->counter_words = vcap->sw_count;
  1208. counter_memory_width = ocelot_target_read(ocelot, vcap->target,
  1209. VCAP_CONST_CNT_WIDTH);
  1210. vcap->counter_width = counter_memory_width / vcap->counter_words;
  1211. }
  1212. int ocelot_vcap_init(struct ocelot *ocelot)
  1213. {
  1214. struct qos_policer_conf cpu_drop = {
  1215. .mode = MSCC_QOS_RATE_MODE_DATA,
  1216. };
  1217. int ret, i;
  1218. /* Create a policer that will drop the frames for the cpu.
  1219. * This policer will be used as action in the acl rules to drop
  1220. * frames.
  1221. */
  1222. ret = qos_policer_conf_set(ocelot, OCELOT_POLICER_DISCARD, &cpu_drop);
  1223. if (ret)
  1224. return ret;
  1225. for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) {
  1226. struct ocelot_vcap_block *block = &ocelot->block[i];
  1227. struct vcap_props *vcap = &ocelot->vcap[i];
  1228. INIT_LIST_HEAD(&block->rules);
  1229. ocelot_vcap_detect_constants(ocelot, vcap);
  1230. ocelot_vcap_init_one(ocelot, vcap);
  1231. }
  1232. INIT_LIST_HEAD(&ocelot->dummy_rules);
  1233. INIT_LIST_HEAD(&ocelot->traps);
  1234. INIT_LIST_HEAD(&ocelot->vcap_pol.pol_list);
  1235. return 0;
  1236. }