ocelot_vcap.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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_bit_set(vcap, &data, VCAP_IS1_HK_TPID, tag->tpid);
  619. vcap_key_set(vcap, &data, VCAP_IS1_HK_VID,
  620. tag->vid.value, tag->vid.mask);
  621. vcap_key_set(vcap, &data, VCAP_IS1_HK_PCP,
  622. tag->pcp.value[0], tag->pcp.mask[0]);
  623. type = IS1_TYPE_S1_NORMAL;
  624. switch (filter->key_type) {
  625. case OCELOT_VCAP_KEY_ETYPE: {
  626. struct ocelot_vcap_key_etype *etype = &filter->key.etype;
  627. vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L2_SMAC,
  628. etype->smac.value, etype->smac.mask);
  629. vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
  630. etype->etype.value, etype->etype.mask);
  631. break;
  632. }
  633. case OCELOT_VCAP_KEY_IPV4: {
  634. struct ocelot_vcap_key_ipv4 *ipv4 = &filter->key.ipv4;
  635. struct ocelot_vcap_udp_tcp *sport = &ipv4->sport;
  636. struct ocelot_vcap_udp_tcp *dport = &ipv4->dport;
  637. enum ocelot_vcap_bit tcp_udp = OCELOT_VCAP_BIT_0;
  638. struct ocelot_vcap_u8 proto = ipv4->proto;
  639. struct ocelot_vcap_ipv4 sip = ipv4->sip;
  640. u32 val, msk;
  641. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP_SNAP,
  642. OCELOT_VCAP_BIT_1);
  643. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP4,
  644. OCELOT_VCAP_BIT_1);
  645. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_ETYPE_LEN,
  646. OCELOT_VCAP_BIT_1);
  647. vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L3_IP4_SIP,
  648. sip.value.addr, sip.mask.addr);
  649. val = proto.value[0];
  650. msk = proto.mask[0];
  651. if ((val == NEXTHDR_TCP || val == NEXTHDR_UDP) && msk == 0xff)
  652. tcp_udp = OCELOT_VCAP_BIT_1;
  653. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP_UDP, tcp_udp);
  654. if (tcp_udp) {
  655. enum ocelot_vcap_bit tcp = OCELOT_VCAP_BIT_0;
  656. if (val == NEXTHDR_TCP)
  657. tcp = OCELOT_VCAP_BIT_1;
  658. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP, tcp);
  659. vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_L4_SPORT,
  660. sport);
  661. /* Overloaded field */
  662. vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_ETYPE,
  663. dport);
  664. } else {
  665. /* IPv4 "other" frame */
  666. struct ocelot_vcap_u16 etype = {0};
  667. /* Overloaded field */
  668. etype.value[0] = proto.value[0];
  669. etype.mask[0] = proto.mask[0];
  670. vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE,
  671. etype.value, etype.mask);
  672. }
  673. break;
  674. }
  675. default:
  676. break;
  677. }
  678. vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TYPE,
  679. type ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0);
  680. is1_action_set(ocelot, &data, filter);
  681. vcap_data_set(data.counter, data.counter_offset,
  682. vcap->counter_width, filter->stats.pkts);
  683. /* Write row */
  684. vcap_entry2cache(ocelot, vcap, &data);
  685. vcap_action2cache(ocelot, vcap, &data);
  686. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
  687. }
  688. static void es0_action_set(struct ocelot *ocelot, struct vcap_data *data,
  689. const struct ocelot_vcap_filter *filter)
  690. {
  691. const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
  692. const struct ocelot_vcap_action *a = &filter->action;
  693. vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_OUTER_TAG,
  694. a->push_outer_tag);
  695. vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_INNER_TAG,
  696. a->push_inner_tag);
  697. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_TPID_SEL,
  698. a->tag_a_tpid_sel);
  699. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_VID_SEL,
  700. a->tag_a_vid_sel);
  701. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_PCP_SEL,
  702. a->tag_a_pcp_sel);
  703. vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_A_VAL, a->vid_a_val);
  704. vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_A_VAL, a->pcp_a_val);
  705. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_TPID_SEL,
  706. a->tag_b_tpid_sel);
  707. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_VID_SEL,
  708. a->tag_b_vid_sel);
  709. vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_PCP_SEL,
  710. a->tag_b_pcp_sel);
  711. vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_B_VAL, a->vid_b_val);
  712. vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_B_VAL, a->pcp_b_val);
  713. }
  714. static void es0_entry_set(struct ocelot *ocelot, int ix,
  715. struct ocelot_vcap_filter *filter)
  716. {
  717. const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0];
  718. struct ocelot_vcap_key_vlan *tag = &filter->vlan;
  719. struct vcap_data data;
  720. int row = ix;
  721. memset(&data, 0, sizeof(data));
  722. /* Read row */
  723. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL);
  724. vcap_cache2entry(ocelot, vcap, &data);
  725. vcap_cache2action(ocelot, vcap, &data);
  726. data.tg_sw = VCAP_TG_FULL;
  727. data.type = ES0_ACTION_TYPE_NORMAL;
  728. vcap_data_offset_get(vcap, &data, ix);
  729. data.tg = (data.tg & ~data.tg_mask);
  730. if (filter->prio != 0)
  731. data.tg |= data.tg_value;
  732. vcap_key_set(vcap, &data, VCAP_ES0_IGR_PORT, filter->ingress_port.value,
  733. filter->ingress_port.mask);
  734. vcap_key_set(vcap, &data, VCAP_ES0_EGR_PORT, filter->egress_port.value,
  735. filter->egress_port.mask);
  736. vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_MC, filter->dmac_mc);
  737. vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_BC, filter->dmac_bc);
  738. vcap_key_set(vcap, &data, VCAP_ES0_VID,
  739. tag->vid.value, tag->vid.mask);
  740. vcap_key_set(vcap, &data, VCAP_ES0_PCP,
  741. tag->pcp.value[0], tag->pcp.mask[0]);
  742. es0_action_set(ocelot, &data, filter);
  743. vcap_data_set(data.counter, data.counter_offset,
  744. vcap->counter_width, filter->stats.pkts);
  745. /* Write row */
  746. vcap_entry2cache(ocelot, vcap, &data);
  747. vcap_action2cache(ocelot, vcap, &data);
  748. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL);
  749. }
  750. static void vcap_entry_get(struct ocelot *ocelot, int ix,
  751. struct ocelot_vcap_filter *filter)
  752. {
  753. const struct vcap_props *vcap = &ocelot->vcap[filter->block_id];
  754. struct vcap_data data;
  755. int row, count;
  756. u32 cnt;
  757. if (filter->block_id == VCAP_ES0)
  758. data.tg_sw = VCAP_TG_FULL;
  759. else
  760. data.tg_sw = VCAP_TG_HALF;
  761. count = (1 << (data.tg_sw - 1));
  762. row = (ix / count);
  763. vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER);
  764. vcap_cache2action(ocelot, vcap, &data);
  765. vcap_data_offset_get(vcap, &data, ix);
  766. cnt = vcap_data_get(data.counter, data.counter_offset,
  767. vcap->counter_width);
  768. filter->stats.pkts = cnt;
  769. }
  770. static void vcap_entry_set(struct ocelot *ocelot, int ix,
  771. struct ocelot_vcap_filter *filter)
  772. {
  773. if (filter->block_id == VCAP_IS1)
  774. return is1_entry_set(ocelot, ix, filter);
  775. if (filter->block_id == VCAP_IS2)
  776. return is2_entry_set(ocelot, ix, filter);
  777. if (filter->block_id == VCAP_ES0)
  778. return es0_entry_set(ocelot, ix, filter);
  779. }
  780. struct vcap_policer_entry {
  781. struct list_head list;
  782. refcount_t refcount;
  783. u32 pol_ix;
  784. };
  785. int ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix,
  786. struct ocelot_policer *pol)
  787. {
  788. struct qos_policer_conf pp = { 0 };
  789. struct vcap_policer_entry *tmp;
  790. int ret;
  791. if (!pol)
  792. return -EINVAL;
  793. pp.mode = MSCC_QOS_RATE_MODE_DATA;
  794. pp.pir = pol->rate;
  795. pp.pbs = pol->burst;
  796. list_for_each_entry(tmp, &ocelot->vcap_pol.pol_list, list)
  797. if (tmp->pol_ix == pol_ix) {
  798. refcount_inc(&tmp->refcount);
  799. return 0;
  800. }
  801. tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
  802. if (!tmp)
  803. return -ENOMEM;
  804. ret = qos_policer_conf_set(ocelot, pol_ix, &pp);
  805. if (ret) {
  806. kfree(tmp);
  807. return ret;
  808. }
  809. tmp->pol_ix = pol_ix;
  810. refcount_set(&tmp->refcount, 1);
  811. list_add_tail(&tmp->list, &ocelot->vcap_pol.pol_list);
  812. return 0;
  813. }
  814. EXPORT_SYMBOL(ocelot_vcap_policer_add);
  815. int ocelot_vcap_policer_del(struct ocelot *ocelot, u32 pol_ix)
  816. {
  817. struct qos_policer_conf pp = {0};
  818. struct vcap_policer_entry *tmp, *n;
  819. u8 z = 0;
  820. list_for_each_entry_safe(tmp, n, &ocelot->vcap_pol.pol_list, list)
  821. if (tmp->pol_ix == pol_ix) {
  822. z = refcount_dec_and_test(&tmp->refcount);
  823. if (z) {
  824. list_del(&tmp->list);
  825. kfree(tmp);
  826. }
  827. }
  828. if (z) {
  829. pp.mode = MSCC_QOS_RATE_MODE_DISABLED;
  830. return qos_policer_conf_set(ocelot, pol_ix, &pp);
  831. }
  832. return 0;
  833. }
  834. EXPORT_SYMBOL(ocelot_vcap_policer_del);
  835. static int
  836. ocelot_vcap_filter_add_aux_resources(struct ocelot *ocelot,
  837. struct ocelot_vcap_filter *filter,
  838. struct netlink_ext_ack *extack)
  839. {
  840. struct ocelot_mirror *m;
  841. int ret;
  842. if (filter->block_id == VCAP_IS2 && filter->action.mirror_ena) {
  843. m = ocelot_mirror_get(ocelot, filter->egress_port.value,
  844. extack);
  845. if (IS_ERR(m))
  846. return PTR_ERR(m);
  847. }
  848. if (filter->block_id == VCAP_IS2 && filter->action.police_ena) {
  849. ret = ocelot_vcap_policer_add(ocelot, filter->action.pol_ix,
  850. &filter->action.pol);
  851. if (ret)
  852. return ret;
  853. }
  854. return 0;
  855. }
  856. static void
  857. ocelot_vcap_filter_del_aux_resources(struct ocelot *ocelot,
  858. struct ocelot_vcap_filter *filter)
  859. {
  860. if (filter->block_id == VCAP_IS2 && filter->action.police_ena)
  861. ocelot_vcap_policer_del(ocelot, filter->action.pol_ix);
  862. if (filter->block_id == VCAP_IS2 && filter->action.mirror_ena)
  863. ocelot_mirror_put(ocelot);
  864. }
  865. static int ocelot_vcap_filter_add_to_block(struct ocelot *ocelot,
  866. struct ocelot_vcap_block *block,
  867. struct ocelot_vcap_filter *filter,
  868. struct netlink_ext_ack *extack)
  869. {
  870. struct list_head *pos = &block->rules;
  871. struct ocelot_vcap_filter *tmp;
  872. int ret;
  873. ret = ocelot_vcap_filter_add_aux_resources(ocelot, filter, extack);
  874. if (ret)
  875. return ret;
  876. block->count++;
  877. list_for_each_entry(tmp, &block->rules, list) {
  878. if (filter->prio < tmp->prio) {
  879. pos = &tmp->list;
  880. break;
  881. }
  882. }
  883. list_add_tail(&filter->list, pos);
  884. return 0;
  885. }
  886. static bool ocelot_vcap_filter_equal(const struct ocelot_vcap_filter *a,
  887. const struct ocelot_vcap_filter *b)
  888. {
  889. return !memcmp(&a->id, &b->id, sizeof(struct ocelot_vcap_id));
  890. }
  891. static int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block,
  892. struct ocelot_vcap_filter *filter)
  893. {
  894. struct ocelot_vcap_filter *tmp;
  895. int index = 0;
  896. list_for_each_entry(tmp, &block->rules, list) {
  897. if (ocelot_vcap_filter_equal(filter, tmp))
  898. return index;
  899. index++;
  900. }
  901. return -ENOENT;
  902. }
  903. static struct ocelot_vcap_filter*
  904. ocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block,
  905. int index)
  906. {
  907. struct ocelot_vcap_filter *tmp;
  908. int i = 0;
  909. list_for_each_entry(tmp, &block->rules, list) {
  910. if (i == index)
  911. return tmp;
  912. ++i;
  913. }
  914. return NULL;
  915. }
  916. struct ocelot_vcap_filter *
  917. ocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block,
  918. unsigned long cookie, bool tc_offload)
  919. {
  920. struct ocelot_vcap_filter *filter;
  921. list_for_each_entry(filter, &block->rules, list)
  922. if (filter->id.tc_offload == tc_offload &&
  923. filter->id.cookie == cookie)
  924. return filter;
  925. return NULL;
  926. }
  927. EXPORT_SYMBOL(ocelot_vcap_block_find_filter_by_id);
  928. /* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based
  929. * on destination and source MAC addresses, but only on higher-level protocol
  930. * information. The only frame types to match on keys containing MAC addresses
  931. * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames.
  932. *
  933. * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match
  934. * on MAC_ETYPE keys such as destination and source MAC on this ingress port.
  935. * However the setting has the side effect of making these frames not matching
  936. * on any _other_ keys than MAC_ETYPE ones.
  937. */
  938. static void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port,
  939. int lookup, bool on)
  940. {
  941. u32 val = 0;
  942. if (on)
  943. val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) |
  944. ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) |
  945. ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) |
  946. ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) |
  947. ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup));
  948. ocelot_rmw_gix(ocelot, val,
  949. ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) |
  950. ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) |
  951. ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) |
  952. ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) |
  953. ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup)),
  954. ANA_PORT_VCAP_S2_CFG, port);
  955. }
  956. static bool
  957. ocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter)
  958. {
  959. u16 proto, mask;
  960. if (filter->key_type != OCELOT_VCAP_KEY_ETYPE)
  961. return false;
  962. proto = ntohs(*(__be16 *)filter->key.etype.etype.value);
  963. mask = ntohs(*(__be16 *)filter->key.etype.etype.mask);
  964. /* ETH_P_ALL match, so all protocols below are included */
  965. if (mask == 0)
  966. return true;
  967. if (proto == ETH_P_ARP)
  968. return true;
  969. if (proto == ETH_P_IP)
  970. return true;
  971. if (proto == ETH_P_IPV6)
  972. return true;
  973. return false;
  974. }
  975. static bool
  976. ocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter)
  977. {
  978. if (filter->key_type == OCELOT_VCAP_KEY_SNAP)
  979. return true;
  980. if (filter->key_type == OCELOT_VCAP_KEY_ARP)
  981. return true;
  982. if (filter->key_type == OCELOT_VCAP_KEY_IPV4)
  983. return true;
  984. if (filter->key_type == OCELOT_VCAP_KEY_IPV6)
  985. return true;
  986. return false;
  987. }
  988. static bool
  989. ocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot,
  990. struct ocelot_vcap_filter *filter)
  991. {
  992. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  993. struct ocelot_vcap_filter *tmp;
  994. unsigned long port;
  995. int i;
  996. /* We only have the S2_IP_TCPUDP_DIS set of knobs for VCAP IS2 */
  997. if (filter->block_id != VCAP_IS2)
  998. return true;
  999. if (ocelot_vcap_is_problematic_mac_etype(filter)) {
  1000. /* Search for any non-MAC_ETYPE rules on the port */
  1001. for (i = 0; i < block->count; i++) {
  1002. tmp = ocelot_vcap_block_find_filter_by_index(block, i);
  1003. if (tmp->ingress_port_mask & filter->ingress_port_mask &&
  1004. tmp->lookup == filter->lookup &&
  1005. ocelot_vcap_is_problematic_non_mac_etype(tmp))
  1006. return false;
  1007. }
  1008. for_each_set_bit(port, &filter->ingress_port_mask,
  1009. ocelot->num_phys_ports)
  1010. ocelot_match_all_as_mac_etype(ocelot, port,
  1011. filter->lookup, true);
  1012. } else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) {
  1013. /* Search for any MAC_ETYPE rules on the port */
  1014. for (i = 0; i < block->count; i++) {
  1015. tmp = ocelot_vcap_block_find_filter_by_index(block, i);
  1016. if (tmp->ingress_port_mask & filter->ingress_port_mask &&
  1017. tmp->lookup == filter->lookup &&
  1018. ocelot_vcap_is_problematic_mac_etype(tmp))
  1019. return false;
  1020. }
  1021. for_each_set_bit(port, &filter->ingress_port_mask,
  1022. ocelot->num_phys_ports)
  1023. ocelot_match_all_as_mac_etype(ocelot, port,
  1024. filter->lookup, false);
  1025. }
  1026. return true;
  1027. }
  1028. int ocelot_vcap_filter_add(struct ocelot *ocelot,
  1029. struct ocelot_vcap_filter *filter,
  1030. struct netlink_ext_ack *extack)
  1031. {
  1032. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  1033. int i, index, ret;
  1034. if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) {
  1035. NL_SET_ERR_MSG_MOD(extack,
  1036. "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules, use the other IS2 lookup");
  1037. return -EBUSY;
  1038. }
  1039. /* Add filter to the linked list */
  1040. ret = ocelot_vcap_filter_add_to_block(ocelot, block, filter, extack);
  1041. if (ret)
  1042. return ret;
  1043. /* Get the index of the inserted filter */
  1044. index = ocelot_vcap_block_get_filter_index(block, filter);
  1045. if (index < 0)
  1046. return index;
  1047. /* Move down the rules to make place for the new filter */
  1048. for (i = block->count - 1; i > index; i--) {
  1049. struct ocelot_vcap_filter *tmp;
  1050. tmp = ocelot_vcap_block_find_filter_by_index(block, i);
  1051. /* Read back the filter's counters before moving it */
  1052. vcap_entry_get(ocelot, i - 1, tmp);
  1053. vcap_entry_set(ocelot, i, tmp);
  1054. }
  1055. /* Now insert the new filter */
  1056. vcap_entry_set(ocelot, index, filter);
  1057. return 0;
  1058. }
  1059. EXPORT_SYMBOL(ocelot_vcap_filter_add);
  1060. static void ocelot_vcap_block_remove_filter(struct ocelot *ocelot,
  1061. struct ocelot_vcap_block *block,
  1062. struct ocelot_vcap_filter *filter)
  1063. {
  1064. struct ocelot_vcap_filter *tmp, *n;
  1065. list_for_each_entry_safe(tmp, n, &block->rules, list) {
  1066. if (ocelot_vcap_filter_equal(filter, tmp)) {
  1067. ocelot_vcap_filter_del_aux_resources(ocelot, tmp);
  1068. list_del(&tmp->list);
  1069. kfree(tmp);
  1070. }
  1071. }
  1072. block->count--;
  1073. }
  1074. int ocelot_vcap_filter_del(struct ocelot *ocelot,
  1075. struct ocelot_vcap_filter *filter)
  1076. {
  1077. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  1078. struct ocelot_vcap_filter del_filter;
  1079. int i, index;
  1080. /* Need to inherit the block_id so that vcap_entry_set()
  1081. * does not get confused and knows where to install it.
  1082. */
  1083. memset(&del_filter, 0, sizeof(del_filter));
  1084. del_filter.block_id = filter->block_id;
  1085. /* Gets index of the filter */
  1086. index = ocelot_vcap_block_get_filter_index(block, filter);
  1087. if (index < 0)
  1088. return index;
  1089. /* Delete filter */
  1090. ocelot_vcap_block_remove_filter(ocelot, block, filter);
  1091. /* Move up all the blocks over the deleted filter */
  1092. for (i = index; i < block->count; i++) {
  1093. struct ocelot_vcap_filter *tmp;
  1094. tmp = ocelot_vcap_block_find_filter_by_index(block, i);
  1095. /* Read back the filter's counters before moving it */
  1096. vcap_entry_get(ocelot, i + 1, tmp);
  1097. vcap_entry_set(ocelot, i, tmp);
  1098. }
  1099. /* Now delete the last filter, because it is duplicated */
  1100. vcap_entry_set(ocelot, block->count, &del_filter);
  1101. return 0;
  1102. }
  1103. EXPORT_SYMBOL(ocelot_vcap_filter_del);
  1104. int ocelot_vcap_filter_replace(struct ocelot *ocelot,
  1105. struct ocelot_vcap_filter *filter)
  1106. {
  1107. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  1108. int index;
  1109. index = ocelot_vcap_block_get_filter_index(block, filter);
  1110. if (index < 0)
  1111. return index;
  1112. vcap_entry_set(ocelot, index, filter);
  1113. return 0;
  1114. }
  1115. EXPORT_SYMBOL(ocelot_vcap_filter_replace);
  1116. int ocelot_vcap_filter_stats_update(struct ocelot *ocelot,
  1117. struct ocelot_vcap_filter *filter)
  1118. {
  1119. struct ocelot_vcap_block *block = &ocelot->block[filter->block_id];
  1120. struct ocelot_vcap_filter tmp;
  1121. int index;
  1122. index = ocelot_vcap_block_get_filter_index(block, filter);
  1123. if (index < 0)
  1124. return index;
  1125. vcap_entry_get(ocelot, index, filter);
  1126. /* After we get the result we need to clear the counters */
  1127. tmp = *filter;
  1128. tmp.stats.pkts = 0;
  1129. vcap_entry_set(ocelot, index, &tmp);
  1130. return 0;
  1131. }
  1132. static void ocelot_vcap_init_one(struct ocelot *ocelot,
  1133. const struct vcap_props *vcap)
  1134. {
  1135. struct vcap_data data;
  1136. memset(&data, 0, sizeof(data));
  1137. vcap_entry2cache(ocelot, vcap, &data);
  1138. ocelot_target_write(ocelot, vcap->target, vcap->entry_count,
  1139. VCAP_CORE_MV_CFG);
  1140. vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY);
  1141. vcap_action2cache(ocelot, vcap, &data);
  1142. ocelot_target_write(ocelot, vcap->target, vcap->action_count,
  1143. VCAP_CORE_MV_CFG);
  1144. vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE,
  1145. VCAP_SEL_ACTION | VCAP_SEL_COUNTER);
  1146. }
  1147. static void ocelot_vcap_detect_constants(struct ocelot *ocelot,
  1148. struct vcap_props *vcap)
  1149. {
  1150. int counter_memory_width;
  1151. int num_default_actions;
  1152. int version;
  1153. version = ocelot_target_read(ocelot, vcap->target,
  1154. VCAP_CONST_VCAP_VER);
  1155. /* Only version 0 VCAP supported for now */
  1156. if (WARN_ON(version != 0))
  1157. return;
  1158. /* Width in bits of type-group field */
  1159. vcap->tg_width = ocelot_target_read(ocelot, vcap->target,
  1160. VCAP_CONST_ENTRY_TG_WIDTH);
  1161. /* Number of subwords per TCAM row */
  1162. vcap->sw_count = ocelot_target_read(ocelot, vcap->target,
  1163. VCAP_CONST_ENTRY_SWCNT);
  1164. /* Number of rows in TCAM. There can be this many full keys, or double
  1165. * this number half keys, or 4 times this number quarter keys.
  1166. */
  1167. vcap->entry_count = ocelot_target_read(ocelot, vcap->target,
  1168. VCAP_CONST_ENTRY_CNT);
  1169. /* Assuming there are 4 subwords per TCAM row, their layout in the
  1170. * actual TCAM (not in the cache) would be:
  1171. *
  1172. * | SW 3 | TG 3 | SW 2 | TG 2 | SW 1 | TG 1 | SW 0 | TG 0 |
  1173. *
  1174. * (where SW=subword and TG=Type-Group).
  1175. *
  1176. * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM
  1177. * row. But when software accesses the TCAM through the cache
  1178. * registers, the Type-Group values are written through another set of
  1179. * registers VCAP_TG_DAT, and therefore, it appears as though the 4
  1180. * subwords are contiguous in the cache memory.
  1181. * Important mention: regardless of the number of key entries per row
  1182. * (and therefore of key size: 1 full key or 2 half keys or 4 quarter
  1183. * keys), software always has to configure 4 Type-Group values. For
  1184. * example, in the case of 1 full key, the driver needs to set all 4
  1185. * Type-Group to be full key.
  1186. *
  1187. * For this reason, we need to fix up the value that the hardware is
  1188. * giving us. We don't actually care about the width of the entry in
  1189. * the TCAM. What we care about is the width of the entry in the cache
  1190. * registers, which is how we get to interact with it. And since the
  1191. * VCAP_ENTRY_DAT cache registers access only the subwords and not the
  1192. * Type-Groups, this means we need to subtract the width of the
  1193. * Type-Groups when packing and unpacking key entry data in a TCAM row.
  1194. */
  1195. vcap->entry_width = ocelot_target_read(ocelot, vcap->target,
  1196. VCAP_CONST_ENTRY_WIDTH);
  1197. vcap->entry_width -= vcap->tg_width * vcap->sw_count;
  1198. num_default_actions = ocelot_target_read(ocelot, vcap->target,
  1199. VCAP_CONST_ACTION_DEF_CNT);
  1200. vcap->action_count = vcap->entry_count + num_default_actions;
  1201. vcap->action_width = ocelot_target_read(ocelot, vcap->target,
  1202. VCAP_CONST_ACTION_WIDTH);
  1203. /* The width of the counter memory, this is the complete width of all
  1204. * counter-fields associated with one full-word entry. There is one
  1205. * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of
  1206. * subwords.)
  1207. */
  1208. vcap->counter_words = vcap->sw_count;
  1209. counter_memory_width = ocelot_target_read(ocelot, vcap->target,
  1210. VCAP_CONST_CNT_WIDTH);
  1211. vcap->counter_width = counter_memory_width / vcap->counter_words;
  1212. }
  1213. int ocelot_vcap_init(struct ocelot *ocelot)
  1214. {
  1215. struct qos_policer_conf cpu_drop = {
  1216. .mode = MSCC_QOS_RATE_MODE_DATA,
  1217. };
  1218. int ret, i;
  1219. /* Create a policer that will drop the frames for the cpu.
  1220. * This policer will be used as action in the acl rules to drop
  1221. * frames.
  1222. */
  1223. ret = qos_policer_conf_set(ocelot, OCELOT_POLICER_DISCARD, &cpu_drop);
  1224. if (ret)
  1225. return ret;
  1226. for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) {
  1227. struct ocelot_vcap_block *block = &ocelot->block[i];
  1228. struct vcap_props *vcap = &ocelot->vcap[i];
  1229. INIT_LIST_HEAD(&block->rules);
  1230. ocelot_vcap_detect_constants(ocelot, vcap);
  1231. ocelot_vcap_init_one(ocelot, vcap);
  1232. }
  1233. INIT_LIST_HEAD(&ocelot->dummy_rules);
  1234. INIT_LIST_HEAD(&ocelot->traps);
  1235. INIT_LIST_HEAD(&ocelot->vcap_pol.pol_list);
  1236. return 0;
  1237. }