ipa_rt.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/bitops.h>
  6. #include <linux/idr.h>
  7. #include "ipa_i.h"
  8. #include "ipahal/ipahal.h"
  9. #include "ipahal/ipahal_fltrt.h"
  10. #define IPA_RT_INDEX_BITMAP_SIZE (32)
  11. #define IPA_RT_STATUS_OF_ADD_FAILED (-1)
  12. #define IPA_RT_STATUS_OF_DEL_FAILED (-1)
  13. #define IPA_RT_STATUS_OF_MDFY_FAILED (-1)
  14. #define IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC 5
  15. #define IPA_RT_GET_RULE_TYPE(__entry) \
  16. ( \
  17. ((__entry)->rule.hashable) ? \
  18. (IPA_RULE_HASHABLE) : (IPA_RULE_NON_HASHABLE) \
  19. )
  20. /**
  21. * ipa_generate_rt_hw_rule() - Generated the RT H/W single rule
  22. * This func will do the preparation core driver work and then calls
  23. * the HAL layer for the real work.
  24. * @ip: the ip address family type
  25. * @entry: routing entry
  26. * @buf: output buffer, buf == NULL means
  27. * caller wants to know the size of the rule as seen
  28. * by HW so they did not pass a valid buffer, we will use a
  29. * scratch buffer instead.
  30. * With this scheme we are going to
  31. * generate the rule twice, once to know size using scratch
  32. * buffer and second to write the rule to the actual caller
  33. * supplied buffer which is of required size
  34. *
  35. * Returns: 0 on success, negative on failure
  36. *
  37. * caller needs to hold any needed locks to ensure integrity
  38. */
  39. static int ipa_generate_rt_hw_rule(enum ipa_ip_type ip,
  40. struct ipa3_rt_entry *entry, u8 *buf)
  41. {
  42. struct ipahal_rt_rule_gen_params gen_params;
  43. struct ipa3_hdr_entry *hdr_entry;
  44. struct ipa3_hdr_proc_ctx_entry *hdr_proc_entry;
  45. int res = 0;
  46. memset(&gen_params, 0, sizeof(gen_params));
  47. if (entry->rule.hashable &&
  48. entry->rule.attrib.attrib_mask & IPA_FLT_IS_PURE_ACK) {
  49. IPAERR_RL("PURE_ACK rule atrb used with hash rule\n");
  50. WARN_ON_RATELIMIT_IPA(1);
  51. return -EPERM;
  52. }
  53. gen_params.ipt = ip;
  54. gen_params.dst_pipe_idx = ipa3_get_ep_mapping(entry->rule.dst);
  55. if (gen_params.dst_pipe_idx == -1) {
  56. IPAERR_RL("Wrong destination pipe specified in RT rule\n");
  57. WARN_ON_RATELIMIT_IPA(1);
  58. return -EPERM;
  59. }
  60. if (!IPA_CLIENT_IS_CONS(entry->rule.dst)) {
  61. IPAERR_RL("No RT rule on IPA_client_producer pipe.\n");
  62. IPAERR_RL("pipe_idx: %d dst_pipe: %d\n",
  63. gen_params.dst_pipe_idx, entry->rule.dst);
  64. WARN_ON_RATELIMIT_IPA(1);
  65. return -EPERM;
  66. }
  67. /* Adding check to confirm still
  68. * header entry present in header table or not
  69. */
  70. if (entry->hdr) {
  71. hdr_entry = ipa3_id_find(entry->rule.hdr_hdl);
  72. if (!hdr_entry || hdr_entry->cookie != IPA_HDR_COOKIE) {
  73. IPAERR_RL("Header entry already deleted\n");
  74. return -EPERM;
  75. }
  76. } else if (entry->proc_ctx) {
  77. hdr_proc_entry = ipa3_id_find(entry->rule.hdr_proc_ctx_hdl);
  78. if (!hdr_proc_entry ||
  79. hdr_proc_entry->cookie != IPA_PROC_HDR_COOKIE) {
  80. IPAERR_RL("Proc header entry already deleted\n");
  81. return -EPERM;
  82. }
  83. }
  84. if (entry->proc_ctx || (entry->hdr && entry->hdr->is_hdr_proc_ctx)) {
  85. struct ipa3_hdr_proc_ctx_entry *proc_ctx;
  86. proc_ctx = (entry->proc_ctx) ? : entry->hdr->proc_ctx;
  87. if ((proc_ctx == NULL) ||
  88. (proc_ctx->cookie != IPA_PROC_HDR_COOKIE)) {
  89. gen_params.hdr_type = IPAHAL_RT_RULE_HDR_NONE;
  90. gen_params.hdr_ofst = 0;
  91. } else {
  92. gen_params.hdr_lcl = ipa3_ctx->hdr_proc_ctx_tbl_lcl;
  93. gen_params.hdr_type = IPAHAL_RT_RULE_HDR_PROC_CTX;
  94. gen_params.hdr_ofst = proc_ctx->offset_entry->offset +
  95. ipa3_ctx->hdr_proc_ctx_tbl.start_offset;
  96. }
  97. } else if ((entry->hdr != NULL) &&
  98. (entry->hdr->cookie == IPA_HDR_COOKIE)) {
  99. gen_params.hdr_lcl = ipa3_ctx->hdr_tbl_lcl;
  100. gen_params.hdr_type = IPAHAL_RT_RULE_HDR_RAW;
  101. gen_params.hdr_ofst = entry->hdr->offset_entry->offset;
  102. } else {
  103. gen_params.hdr_type = IPAHAL_RT_RULE_HDR_NONE;
  104. gen_params.hdr_ofst = 0;
  105. }
  106. gen_params.priority = entry->prio;
  107. gen_params.id = entry->rule_id;
  108. gen_params.rule = (const struct ipa_rt_rule_i *)&entry->rule;
  109. gen_params.cnt_idx = entry->cnt_idx;
  110. res = ipahal_rt_generate_hw_rule(&gen_params, &entry->hw_len, buf);
  111. if (res)
  112. IPAERR("failed to generate rt h/w rule\n");
  113. return res;
  114. }
  115. /**
  116. * ipa_translate_rt_tbl_to_hw_fmt() - translate the routing driver structures
  117. * (rules and tables) to HW format and fill it in the given buffers
  118. * @ip: the ip address family type
  119. * @rlt: the type of the rules to translate (hashable or non-hashable)
  120. * @base: the rules body buffer to be filled
  121. * @hdr: the rules header (addresses/offsets) buffer to be filled
  122. * @body_ofst: the offset of the rules body from the rules header at
  123. * ipa sram (for local body usage)
  124. * @apps_start_idx: the first rt table index of apps tables
  125. *
  126. * Returns: 0 on success, negative on failure
  127. *
  128. * caller needs to hold any needed locks to ensure integrity
  129. *
  130. */
  131. static int ipa_translate_rt_tbl_to_hw_fmt(enum ipa_ip_type ip,
  132. enum ipa_rule_type rlt, u8 *base, u8 *hdr,
  133. u32 body_ofst, u32 apps_start_idx)
  134. {
  135. struct ipa3_rt_tbl_set *set;
  136. struct ipa3_rt_tbl *tbl;
  137. struct ipa_mem_buffer tbl_mem;
  138. u8 *tbl_mem_buf;
  139. struct ipa3_rt_entry *entry;
  140. int res;
  141. u64 offset;
  142. u8 *body_i;
  143. set = &ipa3_ctx->rt_tbl_set[ip];
  144. body_i = base;
  145. list_for_each_entry(tbl, &set->head_rt_tbl_list, link) {
  146. if (tbl->sz[rlt] == 0)
  147. continue;
  148. if (tbl->in_sys[rlt]) {
  149. /* only body (no header) */
  150. tbl_mem.size = tbl->sz[rlt] -
  151. ipahal_get_hw_tbl_hdr_width();
  152. if (ipahal_fltrt_allocate_hw_sys_tbl(&tbl_mem)) {
  153. IPAERR_RL("fail to alloc sys tbl of size %d\n",
  154. tbl_mem.size);
  155. goto err;
  156. }
  157. if (ipahal_fltrt_write_addr_to_hdr(tbl_mem.phys_base,
  158. hdr, tbl->idx - apps_start_idx, true)) {
  159. IPAERR_RL("fail to wrt sys tbl addr to hdr\n");
  160. goto hdr_update_fail;
  161. }
  162. tbl_mem_buf = tbl_mem.base;
  163. /* generate the rule-set */
  164. list_for_each_entry(entry, &tbl->head_rt_rule_list,
  165. link) {
  166. if (IPA_RT_GET_RULE_TYPE(entry) != rlt)
  167. continue;
  168. res = ipa_generate_rt_hw_rule(ip, entry,
  169. tbl_mem_buf);
  170. if (res) {
  171. IPAERR_RL("failed to gen HW RT rule\n");
  172. goto hdr_update_fail;
  173. }
  174. tbl_mem_buf += entry->hw_len;
  175. }
  176. if (tbl->curr_mem[rlt].phys_base) {
  177. WARN_ON(tbl->prev_mem[rlt].phys_base);
  178. tbl->prev_mem[rlt] = tbl->curr_mem[rlt];
  179. }
  180. tbl->curr_mem[rlt] = tbl_mem;
  181. } else {
  182. offset = body_i - base + body_ofst;
  183. /* update the hdr at the right index */
  184. if (ipahal_fltrt_write_addr_to_hdr(offset, hdr,
  185. tbl->idx - apps_start_idx, true)) {
  186. IPAERR_RL("fail to wrt lcl tbl ofst to hdr\n");
  187. goto hdr_update_fail;
  188. }
  189. /* generate the rule-set */
  190. list_for_each_entry(entry, &tbl->head_rt_rule_list,
  191. link) {
  192. if (IPA_RT_GET_RULE_TYPE(entry) != rlt)
  193. continue;
  194. res = ipa_generate_rt_hw_rule(ip, entry,
  195. body_i);
  196. if (res) {
  197. IPAERR_RL("failed to gen HW RT rule\n");
  198. goto err;
  199. }
  200. body_i += entry->hw_len;
  201. }
  202. /**
  203. * advance body_i to next table alignment as local
  204. * tables
  205. * are order back-to-back
  206. */
  207. body_i += ipahal_get_lcl_tbl_addr_alignment();
  208. body_i = (u8 *)((long)body_i &
  209. ~ipahal_get_lcl_tbl_addr_alignment());
  210. }
  211. }
  212. return 0;
  213. hdr_update_fail:
  214. ipahal_free_dma_mem(&tbl_mem);
  215. err:
  216. return -EPERM;
  217. }
  218. static void __ipa_reap_sys_rt_tbls(enum ipa_ip_type ip)
  219. {
  220. struct ipa3_rt_tbl *tbl;
  221. struct ipa3_rt_tbl *next;
  222. struct ipa3_rt_tbl_set *set;
  223. int i;
  224. set = &ipa3_ctx->rt_tbl_set[ip];
  225. list_for_each_entry(tbl, &set->head_rt_tbl_list, link) {
  226. for (i = 0; i < IPA_RULE_TYPE_MAX; i++) {
  227. if (tbl->prev_mem[i].phys_base) {
  228. IPADBG_LOW(
  229. "reaping sys rt tbl name=%s ip=%d rlt=%d\n",
  230. tbl->name, ip, i);
  231. ipahal_free_dma_mem(&tbl->prev_mem[i]);
  232. memset(&tbl->prev_mem[i], 0,
  233. sizeof(tbl->prev_mem[i]));
  234. }
  235. }
  236. }
  237. set = &ipa3_ctx->reap_rt_tbl_set[ip];
  238. list_for_each_entry_safe(tbl, next, &set->head_rt_tbl_list, link) {
  239. for (i = 0; i < IPA_RULE_TYPE_MAX; i++) {
  240. WARN_ON(tbl->prev_mem[i].phys_base != 0);
  241. if (tbl->curr_mem[i].phys_base) {
  242. IPADBG_LOW(
  243. "reaping sys rt tbl name=%s ip=%d rlt=%d\n",
  244. tbl->name, ip, i);
  245. ipahal_free_dma_mem(&tbl->curr_mem[i]);
  246. }
  247. }
  248. list_del(&tbl->link);
  249. kmem_cache_free(ipa3_ctx->rt_tbl_cache, tbl);
  250. }
  251. }
  252. /**
  253. * ipa_prep_rt_tbl_for_cmt() - preparing the rt table for commit
  254. * assign priorities to the rules, calculate their sizes and calculate
  255. * the overall table size
  256. * @ip: the ip address family type
  257. * @tbl: the rt tbl to be prepared
  258. *
  259. * Return: 0 on success, negative on failure
  260. */
  261. static int ipa_prep_rt_tbl_for_cmt(enum ipa_ip_type ip,
  262. struct ipa3_rt_tbl *tbl)
  263. {
  264. struct ipa3_rt_entry *entry;
  265. int prio_i;
  266. int res;
  267. int max_prio;
  268. u32 hdr_width;
  269. tbl->sz[IPA_RULE_HASHABLE] = 0;
  270. tbl->sz[IPA_RULE_NON_HASHABLE] = 0;
  271. max_prio = ipahal_get_rule_max_priority();
  272. prio_i = max_prio;
  273. list_for_each_entry(entry, &tbl->head_rt_rule_list, link) {
  274. if (entry->rule.max_prio) {
  275. entry->prio = max_prio;
  276. } else {
  277. if (ipahal_rule_decrease_priority(&prio_i)) {
  278. IPAERR("cannot rule decrease priority - %d\n",
  279. prio_i);
  280. return -EPERM;
  281. }
  282. entry->prio = prio_i;
  283. }
  284. res = ipa_generate_rt_hw_rule(ip, entry, NULL);
  285. if (res) {
  286. IPAERR_RL("failed to calculate HW RT rule size\n");
  287. return -EPERM;
  288. }
  289. IPADBG_LOW("RT rule id (handle) %d hw_len %u priority %u\n",
  290. entry->id, entry->hw_len, entry->prio);
  291. if (entry->rule.hashable)
  292. tbl->sz[IPA_RULE_HASHABLE] += entry->hw_len;
  293. else
  294. tbl->sz[IPA_RULE_NON_HASHABLE] += entry->hw_len;
  295. }
  296. if ((tbl->sz[IPA_RULE_HASHABLE] +
  297. tbl->sz[IPA_RULE_NON_HASHABLE]) == 0) {
  298. WARN_ON_RATELIMIT_IPA(1);
  299. IPAERR_RL("rt tbl %s is with zero total size\n", tbl->name);
  300. }
  301. hdr_width = ipahal_get_hw_tbl_hdr_width();
  302. if (tbl->sz[IPA_RULE_HASHABLE])
  303. tbl->sz[IPA_RULE_HASHABLE] += hdr_width;
  304. if (tbl->sz[IPA_RULE_NON_HASHABLE])
  305. tbl->sz[IPA_RULE_NON_HASHABLE] += hdr_width;
  306. IPADBG("RT tbl index %u hash_sz %u non-hash sz %u\n", tbl->idx,
  307. tbl->sz[IPA_RULE_HASHABLE], tbl->sz[IPA_RULE_NON_HASHABLE]);
  308. return 0;
  309. }
  310. /**
  311. * ipa_generate_rt_hw_tbl_img() - generates the rt hw tbls.
  312. * headers and bodies (sys bodies) are being created into buffers that will
  313. * be filled into the local memory (sram)
  314. * @ip: the ip address family type
  315. * @alloc_params: IN/OUT parameters to hold info regard the tables headers
  316. * and bodies on DDR (DMA buffers), and needed info for the allocation
  317. * that the HAL needs
  318. *
  319. * Return: 0 on success, negative on failure
  320. */
  321. static int ipa_generate_rt_hw_tbl_img(enum ipa_ip_type ip,
  322. struct ipahal_fltrt_alloc_imgs_params *alloc_params)
  323. {
  324. u32 hash_bdy_start_ofst, nhash_bdy_start_ofst;
  325. u32 apps_start_idx;
  326. int rc = 0;
  327. if (ip == IPA_IP_v4) {
  328. nhash_bdy_start_ofst = IPA_MEM_PART(apps_v4_rt_nhash_ofst) -
  329. IPA_MEM_PART(v4_rt_nhash_ofst);
  330. hash_bdy_start_ofst = IPA_MEM_PART(apps_v4_rt_hash_ofst) -
  331. IPA_MEM_PART(v4_rt_hash_ofst);
  332. apps_start_idx = IPA_MEM_PART(v4_apps_rt_index_lo);
  333. } else {
  334. nhash_bdy_start_ofst = IPA_MEM_PART(apps_v6_rt_nhash_ofst) -
  335. IPA_MEM_PART(v6_rt_nhash_ofst);
  336. hash_bdy_start_ofst = IPA_MEM_PART(apps_v6_rt_hash_ofst) -
  337. IPA_MEM_PART(v6_rt_hash_ofst);
  338. apps_start_idx = IPA_MEM_PART(v6_apps_rt_index_lo);
  339. }
  340. if (ipahal_fltrt_allocate_hw_tbl_imgs(alloc_params)) {
  341. IPAERR("fail to allocate RT HW TBL images. IP %d\n", ip);
  342. rc = -ENOMEM;
  343. goto allocate_fail;
  344. }
  345. if (ipa_translate_rt_tbl_to_hw_fmt(ip, IPA_RULE_HASHABLE,
  346. alloc_params->hash_bdy.base, alloc_params->hash_hdr.base,
  347. hash_bdy_start_ofst, apps_start_idx)) {
  348. IPAERR("fail to translate hashable rt tbls to hw format\n");
  349. rc = -EPERM;
  350. goto translate_fail;
  351. }
  352. if (ipa_translate_rt_tbl_to_hw_fmt(ip, IPA_RULE_NON_HASHABLE,
  353. alloc_params->nhash_bdy.base, alloc_params->nhash_hdr.base,
  354. nhash_bdy_start_ofst, apps_start_idx)) {
  355. IPAERR("fail to translate non-hashable rt tbls to hw format\n");
  356. rc = -EPERM;
  357. goto translate_fail;
  358. }
  359. return rc;
  360. translate_fail:
  361. if (alloc_params->hash_hdr.size)
  362. ipahal_free_dma_mem(&alloc_params->hash_hdr);
  363. ipahal_free_dma_mem(&alloc_params->nhash_hdr);
  364. if (alloc_params->hash_bdy.size)
  365. ipahal_free_dma_mem(&alloc_params->hash_bdy);
  366. if (alloc_params->nhash_bdy.size)
  367. ipahal_free_dma_mem(&alloc_params->nhash_bdy);
  368. allocate_fail:
  369. return rc;
  370. }
  371. /**
  372. * ipa_rt_valid_lcl_tbl_size() - validate if the space allocated for rt tbl
  373. * bodies at the sram is enough for the commit
  374. * @ipt: the ip address family type
  375. * @rlt: the rule type (hashable or non-hashable)
  376. *
  377. * Return: true if enough space available or false in other cases
  378. */
  379. static bool ipa_rt_valid_lcl_tbl_size(enum ipa_ip_type ipt,
  380. enum ipa_rule_type rlt, struct ipa_mem_buffer *bdy)
  381. {
  382. u16 avail;
  383. if (ipt == IPA_IP_v4)
  384. avail = (rlt == IPA_RULE_HASHABLE) ?
  385. IPA_MEM_PART(apps_v4_rt_hash_size) :
  386. IPA_MEM_PART(apps_v4_rt_nhash_size);
  387. else
  388. avail = (rlt == IPA_RULE_HASHABLE) ?
  389. IPA_MEM_PART(apps_v6_rt_hash_size) :
  390. IPA_MEM_PART(apps_v6_rt_nhash_size);
  391. if (bdy->size <= avail)
  392. return true;
  393. IPAERR("tbl too big, needed %d avail %d ipt %d rlt %d\n",
  394. bdy->size, avail, ipt, rlt);
  395. return false;
  396. }
  397. /**
  398. * __ipa_commit_rt_v3() - commit rt tables to the hw
  399. * commit the headers and the bodies if are local with internal cache flushing
  400. * @ipt: the ip address family type
  401. *
  402. * Return: 0 on success, negative on failure
  403. */
  404. int __ipa_commit_rt_v3(enum ipa_ip_type ip)
  405. {
  406. struct ipa3_desc desc[IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC];
  407. struct ipahal_imm_cmd_register_write reg_write_cmd = {0};
  408. struct ipahal_imm_cmd_dma_shared_mem mem_cmd = {0};
  409. struct ipahal_imm_cmd_pyld
  410. *cmd_pyld[IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC];
  411. int num_cmd = 0;
  412. struct ipahal_fltrt_alloc_imgs_params alloc_params;
  413. u32 num_modem_rt_index;
  414. int rc = 0;
  415. u32 lcl_hash_hdr, lcl_nhash_hdr;
  416. u32 lcl_hash_bdy, lcl_nhash_bdy;
  417. bool lcl_hash, lcl_nhash;
  418. struct ipahal_reg_fltrt_hash_flush flush;
  419. struct ipahal_reg_valmask valmask;
  420. int i;
  421. struct ipa3_rt_tbl_set *set;
  422. struct ipa3_rt_tbl *tbl;
  423. u32 tbl_hdr_width;
  424. tbl_hdr_width = ipahal_get_hw_tbl_hdr_width();
  425. memset(desc, 0, sizeof(desc));
  426. memset(cmd_pyld, 0, sizeof(cmd_pyld));
  427. memset(&alloc_params, 0, sizeof(alloc_params));
  428. alloc_params.ipt = ip;
  429. if (ip == IPA_IP_v4) {
  430. num_modem_rt_index =
  431. IPA_MEM_PART(v4_modem_rt_index_hi) -
  432. IPA_MEM_PART(v4_modem_rt_index_lo) + 1;
  433. lcl_hash_hdr = ipa3_ctx->smem_restricted_bytes +
  434. IPA_MEM_PART(v4_rt_hash_ofst) +
  435. num_modem_rt_index * tbl_hdr_width;
  436. lcl_nhash_hdr = ipa3_ctx->smem_restricted_bytes +
  437. IPA_MEM_PART(v4_rt_nhash_ofst) +
  438. num_modem_rt_index * tbl_hdr_width;
  439. lcl_hash_bdy = ipa3_ctx->smem_restricted_bytes +
  440. IPA_MEM_PART(apps_v4_rt_hash_ofst);
  441. lcl_nhash_bdy = ipa3_ctx->smem_restricted_bytes +
  442. IPA_MEM_PART(apps_v4_rt_nhash_ofst);
  443. lcl_hash = ipa3_ctx->ip4_rt_tbl_hash_lcl;
  444. lcl_nhash = ipa3_ctx->ip4_rt_tbl_nhash_lcl;
  445. alloc_params.tbls_num = IPA_MEM_PART(v4_apps_rt_index_hi) -
  446. IPA_MEM_PART(v4_apps_rt_index_lo) + 1;
  447. } else {
  448. num_modem_rt_index =
  449. IPA_MEM_PART(v6_modem_rt_index_hi) -
  450. IPA_MEM_PART(v6_modem_rt_index_lo) + 1;
  451. lcl_hash_hdr = ipa3_ctx->smem_restricted_bytes +
  452. IPA_MEM_PART(v6_rt_hash_ofst) +
  453. num_modem_rt_index * tbl_hdr_width;
  454. lcl_nhash_hdr = ipa3_ctx->smem_restricted_bytes +
  455. IPA_MEM_PART(v6_rt_nhash_ofst) +
  456. num_modem_rt_index * tbl_hdr_width;
  457. lcl_hash_bdy = ipa3_ctx->smem_restricted_bytes +
  458. IPA_MEM_PART(apps_v6_rt_hash_ofst);
  459. lcl_nhash_bdy = ipa3_ctx->smem_restricted_bytes +
  460. IPA_MEM_PART(apps_v6_rt_nhash_ofst);
  461. lcl_hash = ipa3_ctx->ip6_rt_tbl_hash_lcl;
  462. lcl_nhash = ipa3_ctx->ip6_rt_tbl_nhash_lcl;
  463. alloc_params.tbls_num = IPA_MEM_PART(v6_apps_rt_index_hi) -
  464. IPA_MEM_PART(v6_apps_rt_index_lo) + 1;
  465. }
  466. if (!ipa3_ctx->rt_idx_bitmap[ip]) {
  467. IPAERR("no rt tbls present\n");
  468. rc = -EPERM;
  469. goto no_rt_tbls;
  470. }
  471. set = &ipa3_ctx->rt_tbl_set[ip];
  472. list_for_each_entry(tbl, &set->head_rt_tbl_list, link) {
  473. if (ipa_prep_rt_tbl_for_cmt(ip, tbl)) {
  474. rc = -EPERM;
  475. goto no_rt_tbls;
  476. }
  477. if (!tbl->in_sys[IPA_RULE_HASHABLE] &&
  478. tbl->sz[IPA_RULE_HASHABLE]) {
  479. alloc_params.num_lcl_hash_tbls++;
  480. alloc_params.total_sz_lcl_hash_tbls +=
  481. tbl->sz[IPA_RULE_HASHABLE];
  482. alloc_params.total_sz_lcl_hash_tbls -= tbl_hdr_width;
  483. }
  484. if (!tbl->in_sys[IPA_RULE_NON_HASHABLE] &&
  485. tbl->sz[IPA_RULE_NON_HASHABLE]) {
  486. alloc_params.num_lcl_nhash_tbls++;
  487. alloc_params.total_sz_lcl_nhash_tbls +=
  488. tbl->sz[IPA_RULE_NON_HASHABLE];
  489. alloc_params.total_sz_lcl_nhash_tbls -= tbl_hdr_width;
  490. }
  491. }
  492. if (ipa_generate_rt_hw_tbl_img(ip, &alloc_params)) {
  493. IPAERR("fail to generate RT HW TBL images. IP %d\n", ip);
  494. rc = -EFAULT;
  495. goto no_rt_tbls;
  496. }
  497. if (!ipa_rt_valid_lcl_tbl_size(ip, IPA_RULE_HASHABLE,
  498. &alloc_params.hash_bdy)) {
  499. rc = -EFAULT;
  500. goto fail_size_valid;
  501. }
  502. if (!ipa_rt_valid_lcl_tbl_size(ip, IPA_RULE_NON_HASHABLE,
  503. &alloc_params.nhash_bdy)) {
  504. rc = -EFAULT;
  505. goto fail_size_valid;
  506. }
  507. /*
  508. * SRAM memory not allocated to hash tables. Sending
  509. * command to hash tables(filer/routing) operation not supported.
  510. */
  511. if (!ipa3_ctx->ipa_fltrt_not_hashable) {
  512. /* flushing ipa internal hashable rt rules cache */
  513. memset(&flush, 0, sizeof(flush));
  514. if (ip == IPA_IP_v4)
  515. flush.v4_rt = true;
  516. else
  517. flush.v6_rt = true;
  518. ipahal_get_fltrt_hash_flush_valmask(&flush, &valmask);
  519. reg_write_cmd.skip_pipeline_clear = false;
  520. reg_write_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  521. reg_write_cmd.offset = ipahal_get_reg_ofst(
  522. IPA_FILT_ROUT_HASH_FLUSH);
  523. reg_write_cmd.value = valmask.val;
  524. reg_write_cmd.value_mask = valmask.mask;
  525. cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
  526. IPA_IMM_CMD_REGISTER_WRITE, &reg_write_cmd,
  527. false);
  528. if (!cmd_pyld[num_cmd]) {
  529. IPAERR(
  530. "fail construct register_write imm cmd. IP %d\n", ip);
  531. goto fail_size_valid;
  532. }
  533. ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
  534. num_cmd++;
  535. }
  536. mem_cmd.is_read = false;
  537. mem_cmd.skip_pipeline_clear = false;
  538. mem_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  539. mem_cmd.size = alloc_params.nhash_hdr.size;
  540. mem_cmd.system_addr = alloc_params.nhash_hdr.phys_base;
  541. mem_cmd.local_addr = lcl_nhash_hdr;
  542. cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
  543. IPA_IMM_CMD_DMA_SHARED_MEM, &mem_cmd, false);
  544. if (!cmd_pyld[num_cmd]) {
  545. IPAERR("fail construct dma_shared_mem imm cmd. IP %d\n", ip);
  546. goto fail_imm_cmd_construct;
  547. }
  548. ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
  549. num_cmd++;
  550. /*
  551. * SRAM memory not allocated to hash tables. Sending
  552. * command to hash tables(filer/routing) operation not supported.
  553. */
  554. if (!ipa3_ctx->ipa_fltrt_not_hashable) {
  555. mem_cmd.is_read = false;
  556. mem_cmd.skip_pipeline_clear = false;
  557. mem_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  558. mem_cmd.size = alloc_params.hash_hdr.size;
  559. mem_cmd.system_addr = alloc_params.hash_hdr.phys_base;
  560. mem_cmd.local_addr = lcl_hash_hdr;
  561. cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
  562. IPA_IMM_CMD_DMA_SHARED_MEM, &mem_cmd, false);
  563. if (!cmd_pyld[num_cmd]) {
  564. IPAERR(
  565. "fail construct dma_shared_mem imm cmd. IP %d\n", ip);
  566. goto fail_imm_cmd_construct;
  567. }
  568. ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
  569. num_cmd++;
  570. }
  571. if (lcl_nhash) {
  572. if (num_cmd >= IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC) {
  573. IPAERR("number of commands is out of range: IP = %d\n",
  574. ip);
  575. rc = -ENOBUFS;
  576. goto fail_imm_cmd_construct;
  577. }
  578. mem_cmd.is_read = false;
  579. mem_cmd.skip_pipeline_clear = false;
  580. mem_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  581. mem_cmd.size = alloc_params.nhash_bdy.size;
  582. mem_cmd.system_addr = alloc_params.nhash_bdy.phys_base;
  583. mem_cmd.local_addr = lcl_nhash_bdy;
  584. cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
  585. IPA_IMM_CMD_DMA_SHARED_MEM, &mem_cmd, false);
  586. if (!cmd_pyld[num_cmd]) {
  587. IPAERR("fail construct dma_shared_mem cmd. IP %d\n",
  588. ip);
  589. goto fail_imm_cmd_construct;
  590. }
  591. ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
  592. num_cmd++;
  593. }
  594. if (lcl_hash) {
  595. if (num_cmd >= IPA_RT_MAX_NUM_OF_COMMIT_TABLES_CMD_DESC) {
  596. IPAERR("number of commands is out of range: IP = %d\n",
  597. ip);
  598. rc = -ENOBUFS;
  599. goto fail_imm_cmd_construct;
  600. }
  601. mem_cmd.is_read = false;
  602. mem_cmd.skip_pipeline_clear = false;
  603. mem_cmd.pipeline_clear_options = IPAHAL_HPS_CLEAR;
  604. mem_cmd.size = alloc_params.hash_bdy.size;
  605. mem_cmd.system_addr = alloc_params.hash_bdy.phys_base;
  606. mem_cmd.local_addr = lcl_hash_bdy;
  607. cmd_pyld[num_cmd] = ipahal_construct_imm_cmd(
  608. IPA_IMM_CMD_DMA_SHARED_MEM, &mem_cmd, false);
  609. if (!cmd_pyld[num_cmd]) {
  610. IPAERR("fail construct dma_shared_mem cmd. IP %d\n",
  611. ip);
  612. goto fail_imm_cmd_construct;
  613. }
  614. ipa3_init_imm_cmd_desc(&desc[num_cmd], cmd_pyld[num_cmd]);
  615. num_cmd++;
  616. }
  617. if (ipa3_send_cmd(num_cmd, desc)) {
  618. IPAERR_RL("fail to send immediate command\n");
  619. rc = -EFAULT;
  620. goto fail_imm_cmd_construct;
  621. }
  622. IPADBG_LOW("Hashable HEAD\n");
  623. IPA_DUMP_BUFF(alloc_params.hash_hdr.base,
  624. alloc_params.hash_hdr.phys_base, alloc_params.hash_hdr.size);
  625. IPADBG_LOW("Non-Hashable HEAD\n");
  626. IPA_DUMP_BUFF(alloc_params.nhash_hdr.base,
  627. alloc_params.nhash_hdr.phys_base, alloc_params.nhash_hdr.size);
  628. if (alloc_params.hash_bdy.size) {
  629. IPADBG_LOW("Hashable BODY\n");
  630. IPA_DUMP_BUFF(alloc_params.hash_bdy.base,
  631. alloc_params.hash_bdy.phys_base,
  632. alloc_params.hash_bdy.size);
  633. }
  634. if (alloc_params.nhash_bdy.size) {
  635. IPADBG_LOW("Non-Hashable BODY\n");
  636. IPA_DUMP_BUFF(alloc_params.nhash_bdy.base,
  637. alloc_params.nhash_bdy.phys_base,
  638. alloc_params.nhash_bdy.size);
  639. }
  640. __ipa_reap_sys_rt_tbls(ip);
  641. fail_imm_cmd_construct:
  642. for (i = 0 ; i < num_cmd ; i++)
  643. ipahal_destroy_imm_cmd(cmd_pyld[i]);
  644. fail_size_valid:
  645. if (alloc_params.hash_hdr.size)
  646. ipahal_free_dma_mem(&alloc_params.hash_hdr);
  647. ipahal_free_dma_mem(&alloc_params.nhash_hdr);
  648. if (alloc_params.hash_bdy.size)
  649. ipahal_free_dma_mem(&alloc_params.hash_bdy);
  650. if (alloc_params.nhash_bdy.size)
  651. ipahal_free_dma_mem(&alloc_params.nhash_bdy);
  652. no_rt_tbls:
  653. return rc;
  654. }
  655. /**
  656. * __ipa3_find_rt_tbl() - find the routing table
  657. * which name is given as parameter
  658. * @ip: [in] the ip address family type of the wanted routing table
  659. * @name: [in] the name of the wanted routing table
  660. *
  661. * Returns: the routing table which name is given as parameter, or NULL if it
  662. * doesn't exist
  663. */
  664. struct ipa3_rt_tbl *__ipa3_find_rt_tbl(enum ipa_ip_type ip, const char *name)
  665. {
  666. struct ipa3_rt_tbl *entry;
  667. struct ipa3_rt_tbl_set *set;
  668. if (strnlen(name, IPA_RESOURCE_NAME_MAX) == IPA_RESOURCE_NAME_MAX) {
  669. IPAERR_RL("Name too long: %s\n", name);
  670. return NULL;
  671. }
  672. set = &ipa3_ctx->rt_tbl_set[ip];
  673. list_for_each_entry(entry, &set->head_rt_tbl_list, link) {
  674. if (!strcmp(name, entry->name))
  675. return entry;
  676. }
  677. return NULL;
  678. }
  679. /**
  680. * ipa3_query_rt_index() - find the routing table index
  681. * which name and ip type are given as parameters
  682. * @in: [out] the index of the wanted routing table
  683. *
  684. * Returns: the routing table which name is given as parameter, or NULL if it
  685. * doesn't exist
  686. */
  687. int ipa3_query_rt_index(struct ipa_ioc_get_rt_tbl_indx *in)
  688. {
  689. struct ipa3_rt_tbl *entry;
  690. if (in->ip >= IPA_IP_MAX) {
  691. IPAERR_RL("bad param\n");
  692. return -EINVAL;
  693. }
  694. mutex_lock(&ipa3_ctx->lock);
  695. in->name[IPA_RESOURCE_NAME_MAX-1] = '\0';
  696. /* check if this table exists */
  697. entry = __ipa3_find_rt_tbl(in->ip, in->name);
  698. if (!entry) {
  699. mutex_unlock(&ipa3_ctx->lock);
  700. return -EFAULT;
  701. }
  702. in->idx = entry->idx;
  703. mutex_unlock(&ipa3_ctx->lock);
  704. return 0;
  705. }
  706. static struct ipa3_rt_tbl *__ipa_add_rt_tbl(enum ipa_ip_type ip,
  707. const char *name)
  708. {
  709. struct ipa3_rt_tbl *entry;
  710. struct ipa3_rt_tbl_set *set;
  711. int i;
  712. int id;
  713. int max_tbl_indx;
  714. if (name == NULL) {
  715. IPAERR_RL("no tbl name\n");
  716. goto error;
  717. }
  718. if (ip == IPA_IP_v4) {
  719. max_tbl_indx =
  720. max(IPA_MEM_PART(v4_modem_rt_index_hi),
  721. IPA_MEM_PART(v4_apps_rt_index_hi));
  722. } else if (ip == IPA_IP_v6) {
  723. max_tbl_indx =
  724. max(IPA_MEM_PART(v6_modem_rt_index_hi),
  725. IPA_MEM_PART(v6_apps_rt_index_hi));
  726. } else {
  727. IPAERR_RL("bad ip family type\n");
  728. goto error;
  729. }
  730. set = &ipa3_ctx->rt_tbl_set[ip];
  731. /* check if this table exists */
  732. entry = __ipa3_find_rt_tbl(ip, name);
  733. if (!entry) {
  734. entry = kmem_cache_zalloc(ipa3_ctx->rt_tbl_cache, GFP_KERNEL);
  735. if (!entry)
  736. goto error;
  737. /* find a routing tbl index */
  738. for (i = 0; i < IPA_RT_INDEX_BITMAP_SIZE; i++) {
  739. if (!test_bit(i, &ipa3_ctx->rt_idx_bitmap[ip])) {
  740. entry->idx = i;
  741. set_bit(i, &ipa3_ctx->rt_idx_bitmap[ip]);
  742. break;
  743. }
  744. }
  745. if (i == IPA_RT_INDEX_BITMAP_SIZE) {
  746. IPAERR("not free RT tbl indices left\n");
  747. goto fail_rt_idx_alloc;
  748. }
  749. if (i > max_tbl_indx) {
  750. IPAERR("rt tbl index is above max\n");
  751. goto fail_rt_idx_alloc;
  752. }
  753. INIT_LIST_HEAD(&entry->head_rt_rule_list);
  754. INIT_LIST_HEAD(&entry->link);
  755. strlcpy(entry->name, name, IPA_RESOURCE_NAME_MAX);
  756. entry->set = set;
  757. entry->cookie = IPA_RT_TBL_COOKIE;
  758. entry->in_sys[IPA_RULE_HASHABLE] = (ip == IPA_IP_v4) ?
  759. !ipa3_ctx->ip4_rt_tbl_hash_lcl :
  760. !ipa3_ctx->ip6_rt_tbl_hash_lcl;
  761. entry->in_sys[IPA_RULE_NON_HASHABLE] = (ip == IPA_IP_v4) ?
  762. !ipa3_ctx->ip4_rt_tbl_nhash_lcl :
  763. !ipa3_ctx->ip6_rt_tbl_nhash_lcl;
  764. set->tbl_cnt++;
  765. entry->rule_ids = &set->rule_ids;
  766. list_add(&entry->link, &set->head_rt_tbl_list);
  767. IPADBG("add rt tbl idx=%d tbl_cnt=%d ip=%d\n", entry->idx,
  768. set->tbl_cnt, ip);
  769. id = ipa3_id_alloc(entry);
  770. if (id < 0) {
  771. IPAERR_RL("failed to add to tree\n");
  772. WARN_ON_RATELIMIT_IPA(1);
  773. goto ipa_insert_failed;
  774. }
  775. entry->id = id;
  776. }
  777. return entry;
  778. ipa_insert_failed:
  779. set->tbl_cnt--;
  780. list_del(&entry->link);
  781. idr_destroy(entry->rule_ids);
  782. fail_rt_idx_alloc:
  783. entry->cookie = 0;
  784. kmem_cache_free(ipa3_ctx->rt_tbl_cache, entry);
  785. error:
  786. return NULL;
  787. }
  788. static int __ipa_del_rt_tbl(struct ipa3_rt_tbl *entry)
  789. {
  790. enum ipa_ip_type ip = IPA_IP_MAX;
  791. u32 id;
  792. struct ipa3_rt_tbl_set *rset;
  793. if (entry == NULL || (entry->cookie != IPA_RT_TBL_COOKIE)) {
  794. IPAERR_RL("bad params\n");
  795. return -EINVAL;
  796. }
  797. id = entry->id;
  798. if (ipa3_id_find(id) == NULL) {
  799. IPAERR_RL("lookup failed\n");
  800. return -EPERM;
  801. }
  802. if (entry->set == &ipa3_ctx->rt_tbl_set[IPA_IP_v4])
  803. ip = IPA_IP_v4;
  804. else if (entry->set == &ipa3_ctx->rt_tbl_set[IPA_IP_v6])
  805. ip = IPA_IP_v6;
  806. else {
  807. WARN_ON_RATELIMIT_IPA(1);
  808. return -EPERM;
  809. }
  810. rset = &ipa3_ctx->reap_rt_tbl_set[ip];
  811. entry->rule_ids = NULL;
  812. if (entry->in_sys[IPA_RULE_HASHABLE] ||
  813. entry->in_sys[IPA_RULE_NON_HASHABLE]) {
  814. list_move(&entry->link, &rset->head_rt_tbl_list);
  815. clear_bit(entry->idx, &ipa3_ctx->rt_idx_bitmap[ip]);
  816. entry->set->tbl_cnt--;
  817. IPADBG("del sys rt tbl_idx=%d tbl_cnt=%d ip=%d\n",
  818. entry->idx, entry->set->tbl_cnt, ip);
  819. } else {
  820. list_del(&entry->link);
  821. clear_bit(entry->idx, &ipa3_ctx->rt_idx_bitmap[ip]);
  822. entry->set->tbl_cnt--;
  823. IPADBG("del rt tbl_idx=%d tbl_cnt=%d ip=%d\n",
  824. entry->idx, entry->set->tbl_cnt, ip);
  825. kmem_cache_free(ipa3_ctx->rt_tbl_cache, entry);
  826. }
  827. /* remove the handle from the database */
  828. ipa3_id_remove(id);
  829. return 0;
  830. }
  831. static int __ipa_rt_validate_rule_id(u16 rule_id)
  832. {
  833. if (!rule_id)
  834. return 0;
  835. if ((rule_id < ipahal_get_rule_id_hi_bit()) ||
  836. (rule_id >= ((ipahal_get_rule_id_hi_bit()<<1)-1))) {
  837. IPAERR_RL("Invalid rule_id provided 0x%x\n",
  838. rule_id);
  839. return -EPERM;
  840. }
  841. return 0;
  842. }
  843. static int __ipa_rt_validate_hndls(const struct ipa_rt_rule_i *rule,
  844. struct ipa3_hdr_entry **hdr,
  845. struct ipa3_hdr_proc_ctx_entry **proc_ctx)
  846. {
  847. int index;
  848. if (rule->hdr_hdl && rule->hdr_proc_ctx_hdl) {
  849. IPAERR_RL("rule contains both hdr_hdl and hdr_proc_ctx_hdl\n");
  850. return -EPERM;
  851. }
  852. if (rule->hdr_hdl) {
  853. *hdr = ipa3_id_find(rule->hdr_hdl);
  854. if ((*hdr == NULL) || ((*hdr)->cookie != IPA_HDR_COOKIE)) {
  855. IPAERR_RL("rt rule does not point to valid hdr\n");
  856. return -EPERM;
  857. }
  858. } else if (rule->hdr_proc_ctx_hdl) {
  859. *proc_ctx = ipa3_id_find(rule->hdr_proc_ctx_hdl);
  860. if ((*proc_ctx == NULL) ||
  861. ((*proc_ctx)->cookie != IPA_PROC_HDR_COOKIE)) {
  862. IPAERR_RL("rt rule does not point to valid proc ctx\n");
  863. return -EPERM;
  864. }
  865. }
  866. if (ipa3_ctx->ipa_hw_type < IPA_HW_v4_5 && rule->coalesce) {
  867. IPAERR_RL("rt rule should not allow coalescing\n");
  868. return -EPERM;
  869. }
  870. if (ipa3_ctx->ipa_hw_type >= IPA_HW_v4_5) {
  871. if (rule->enable_stats && rule->cnt_idx) {
  872. if (!ipahal_is_rule_cnt_id_valid(rule->cnt_idx)) {
  873. IPAERR_RL(
  874. "invalid cnt_idx %hhu out of range\n",
  875. rule->cnt_idx);
  876. return -EPERM;
  877. }
  878. index = rule->cnt_idx - 1;
  879. if (!ipa3_ctx->flt_rt_counters.used_hw[index]) {
  880. IPAERR_RL(
  881. "invalid cnt_idx %hhu not alloc by driver\n",
  882. rule->cnt_idx);
  883. return -EPERM;
  884. }
  885. }
  886. } else {
  887. if (rule->enable_stats) {
  888. IPAERR_RL(
  889. "enable_stats won't support on ipa_hw_type %d\n",
  890. ipa3_ctx->ipa_hw_type);
  891. return -EPERM;
  892. }
  893. }
  894. return 0;
  895. }
  896. static int __ipa_create_rt_entry(struct ipa3_rt_entry **entry,
  897. const struct ipa_rt_rule_i *rule,
  898. struct ipa3_rt_tbl *tbl, struct ipa3_hdr_entry *hdr,
  899. struct ipa3_hdr_proc_ctx_entry *proc_ctx,
  900. u16 rule_id, bool user)
  901. {
  902. int id;
  903. *entry = kmem_cache_zalloc(ipa3_ctx->rt_rule_cache, GFP_KERNEL);
  904. if (!*entry)
  905. goto error;
  906. INIT_LIST_HEAD(&(*entry)->link);
  907. (*(entry))->cookie = IPA_RT_RULE_COOKIE;
  908. (*(entry))->rule = *rule;
  909. (*(entry))->tbl = tbl;
  910. (*(entry))->hdr = hdr;
  911. (*(entry))->proc_ctx = proc_ctx;
  912. if (rule_id) {
  913. id = rule_id;
  914. (*(entry))->rule_id_valid = 1;
  915. } else {
  916. id = ipa3_alloc_rule_id(tbl->rule_ids);
  917. if (id < 0) {
  918. IPAERR_RL("failed to allocate rule id\n");
  919. WARN_ON_RATELIMIT_IPA(1);
  920. goto alloc_rule_id_fail;
  921. }
  922. }
  923. (*(entry))->rule_id = id;
  924. (*(entry))->ipacm_installed = user;
  925. if ((*(entry))->rule.coalesce &&
  926. (*(entry))->rule.dst == IPA_CLIENT_APPS_WAN_CONS &&
  927. ipa3_get_ep_mapping(IPA_CLIENT_APPS_WAN_COAL_CONS) != -1)
  928. (*(entry))->rule.dst = IPA_CLIENT_APPS_WAN_COAL_CONS;
  929. if (rule->enable_stats)
  930. (*entry)->cnt_idx = rule->cnt_idx;
  931. else
  932. (*entry)->cnt_idx = 0;
  933. return 0;
  934. alloc_rule_id_fail:
  935. kmem_cache_free(ipa3_ctx->rt_rule_cache, *entry);
  936. error:
  937. return -EPERM;
  938. }
  939. static int __ipa_finish_rt_rule_add(struct ipa3_rt_entry *entry, u32 *rule_hdl,
  940. struct ipa3_rt_tbl *tbl)
  941. {
  942. int id;
  943. tbl->rule_cnt++;
  944. if (entry->hdr)
  945. entry->hdr->ref_cnt++;
  946. else if (entry->proc_ctx)
  947. entry->proc_ctx->ref_cnt++;
  948. id = ipa3_id_alloc(entry);
  949. if (id < 0) {
  950. IPAERR_RL("failed to add to tree\n");
  951. WARN_ON_RATELIMIT_IPA(1);
  952. goto ipa_insert_failed;
  953. }
  954. IPADBG("add rt rule tbl_idx=%d rule_cnt=%d rule_id=%d\n",
  955. tbl->idx, tbl->rule_cnt, entry->rule_id);
  956. *rule_hdl = id;
  957. entry->id = id;
  958. return 0;
  959. ipa_insert_failed:
  960. if (entry->hdr)
  961. entry->hdr->ref_cnt--;
  962. else if (entry->proc_ctx)
  963. entry->proc_ctx->ref_cnt--;
  964. idr_remove(tbl->rule_ids, entry->rule_id);
  965. list_del(&entry->link);
  966. kmem_cache_free(ipa3_ctx->rt_rule_cache, entry);
  967. return -EPERM;
  968. }
  969. static int __ipa_add_rt_rule(enum ipa_ip_type ip, const char *name,
  970. const struct ipa_rt_rule_i *rule, u8 at_rear, u32 *rule_hdl,
  971. u16 rule_id, bool user)
  972. {
  973. struct ipa3_rt_tbl *tbl;
  974. struct ipa3_rt_entry *entry;
  975. struct ipa3_hdr_entry *hdr = NULL;
  976. struct ipa3_hdr_proc_ctx_entry *proc_ctx = NULL;
  977. if (__ipa_rt_validate_hndls(rule, &hdr, &proc_ctx))
  978. goto error;
  979. if (__ipa_rt_validate_rule_id(rule_id))
  980. goto error;
  981. tbl = __ipa_add_rt_tbl(ip, name);
  982. if (tbl == NULL || (tbl->cookie != IPA_RT_TBL_COOKIE)) {
  983. IPAERR_RL("failed adding rt tbl name = %s\n",
  984. name ? name : "");
  985. goto error;
  986. }
  987. /*
  988. * do not allow any rule to be added at "default" routing
  989. * table
  990. */
  991. if (!strcmp(tbl->name, IPA_DFLT_RT_TBL_NAME) &&
  992. (tbl->rule_cnt > 0)) {
  993. IPAERR_RL("cannot add rules to default rt table\n");
  994. goto error;
  995. }
  996. if (__ipa_create_rt_entry(&entry, rule, tbl, hdr, proc_ctx,
  997. rule_id, user))
  998. goto error;
  999. if (at_rear)
  1000. list_add_tail(&entry->link, &tbl->head_rt_rule_list);
  1001. else
  1002. list_add(&entry->link, &tbl->head_rt_rule_list);
  1003. if (__ipa_finish_rt_rule_add(entry, rule_hdl, tbl))
  1004. goto error;
  1005. return 0;
  1006. error:
  1007. return -EPERM;
  1008. }
  1009. static int __ipa_add_rt_rule_after(struct ipa3_rt_tbl *tbl,
  1010. const struct ipa_rt_rule_i *rule, u32 *rule_hdl,
  1011. struct ipa3_rt_entry **add_after_entry)
  1012. {
  1013. struct ipa3_rt_entry *entry;
  1014. struct ipa3_hdr_entry *hdr = NULL;
  1015. struct ipa3_hdr_proc_ctx_entry *proc_ctx = NULL;
  1016. if (!*add_after_entry)
  1017. goto error;
  1018. if (__ipa_rt_validate_hndls(rule, &hdr, &proc_ctx))
  1019. goto error;
  1020. if (__ipa_create_rt_entry(&entry, rule, tbl, hdr, proc_ctx, 0, true))
  1021. goto error;
  1022. list_add(&entry->link, &((*add_after_entry)->link));
  1023. if (__ipa_finish_rt_rule_add(entry, rule_hdl, tbl))
  1024. goto error;
  1025. /*
  1026. * prepare for next insertion
  1027. */
  1028. *add_after_entry = entry;
  1029. return 0;
  1030. error:
  1031. *add_after_entry = NULL;
  1032. return -EPERM;
  1033. }
  1034. static void __ipa_convert_rt_rule_in(struct ipa_rt_rule rule_in,
  1035. struct ipa_rt_rule_i *rule_out)
  1036. {
  1037. if (unlikely(sizeof(struct ipa_rt_rule) >
  1038. sizeof(struct ipa_rt_rule_i))) {
  1039. IPAERR_RL("invalid size in: %d size out: %d\n",
  1040. sizeof(struct ipa_rt_rule),
  1041. sizeof(struct ipa_rt_rule_i));
  1042. return;
  1043. }
  1044. memset(rule_out, 0, sizeof(struct ipa_rt_rule_i));
  1045. memcpy(rule_out, &rule_in, sizeof(struct ipa_rt_rule));
  1046. }
  1047. static void __ipa_convert_rt_rule_out(struct ipa_rt_rule_i rule_in,
  1048. struct ipa_rt_rule *rule_out)
  1049. {
  1050. if (unlikely(sizeof(struct ipa_rt_rule) >
  1051. sizeof(struct ipa_rt_rule_i))) {
  1052. IPAERR_RL("invalid size in:%d size out:%d\n",
  1053. sizeof(struct ipa_rt_rule),
  1054. sizeof(struct ipa_rt_rule_i));
  1055. return;
  1056. }
  1057. memset(rule_out, 0, sizeof(struct ipa_rt_rule));
  1058. memcpy(rule_out, &rule_in, sizeof(struct ipa_rt_rule));
  1059. }
  1060. static void __ipa_convert_rt_mdfy_in(struct ipa_rt_rule_mdfy rule_in,
  1061. struct ipa_rt_rule_mdfy_i *rule_out)
  1062. {
  1063. if (unlikely(sizeof(struct ipa_rt_rule_mdfy) >
  1064. sizeof(struct ipa_rt_rule_mdfy_i))) {
  1065. IPAERR_RL("invalid size in:%d size out:%d\n",
  1066. sizeof(struct ipa_rt_rule_mdfy),
  1067. sizeof(struct ipa_rt_rule_mdfy_i));
  1068. return;
  1069. }
  1070. memset(rule_out, 0, sizeof(struct ipa_rt_rule_mdfy_i));
  1071. memcpy(&rule_out->rule, &rule_in.rule,
  1072. sizeof(struct ipa_rt_rule));
  1073. rule_out->rt_rule_hdl = rule_in.rt_rule_hdl;
  1074. rule_out->status = rule_in.status;
  1075. }
  1076. static void __ipa_convert_rt_mdfy_out(struct ipa_rt_rule_mdfy_i rule_in,
  1077. struct ipa_rt_rule_mdfy *rule_out)
  1078. {
  1079. if (unlikely(sizeof(struct ipa_rt_rule_mdfy) >
  1080. sizeof(struct ipa_rt_rule_mdfy_i))) {
  1081. IPAERR_RL("invalid size in:%d size out:%d\n",
  1082. sizeof(struct ipa_rt_rule_mdfy),
  1083. sizeof(struct ipa_rt_rule_mdfy_i));
  1084. return;
  1085. }
  1086. memset(rule_out, 0, sizeof(struct ipa_rt_rule_mdfy));
  1087. memcpy(&rule_out->rule, &rule_in.rule,
  1088. sizeof(struct ipa_rt_rule));
  1089. rule_out->rt_rule_hdl = rule_in.rt_rule_hdl;
  1090. rule_out->status = rule_in.status;
  1091. }
  1092. /**
  1093. * ipa3_add_rt_rule() - Add the specified routing rules to SW and optionally
  1094. * commit to IPA HW
  1095. * @rules: [inout] set of routing rules to add
  1096. *
  1097. * Returns: 0 on success, negative on failure
  1098. *
  1099. * Note: Should not be called from atomic context
  1100. */
  1101. int ipa3_add_rt_rule(struct ipa_ioc_add_rt_rule *rules)
  1102. {
  1103. return ipa3_add_rt_rule_usr(rules, false);
  1104. }
  1105. /**
  1106. * ipa3_add_rt_rule_v2() - Add the specified routing rules to SW
  1107. * and optionally commit to IPA HW
  1108. * @rules: [inout] set of routing rules to add
  1109. *
  1110. * Returns: 0 on success, negative on failure
  1111. *
  1112. * Note: Should not be called from atomic context
  1113. */
  1114. int ipa3_add_rt_rule_v2(struct ipa_ioc_add_rt_rule_v2 *rules)
  1115. {
  1116. return ipa3_add_rt_rule_usr_v2(rules, false);
  1117. }
  1118. /**
  1119. * ipa3_add_rt_rule_usr() - Add the specified routing rules to SW and optionally
  1120. * commit to IPA HW
  1121. * @rules: [inout] set of routing rules to add
  1122. * @user_only: [in] indicate installed by userspace module
  1123. *
  1124. * Returns: 0 on success, negative on failure
  1125. *
  1126. * Note: Should not be called from atomic context
  1127. */
  1128. int ipa3_add_rt_rule_usr(struct ipa_ioc_add_rt_rule *rules, bool user_only)
  1129. {
  1130. int i;
  1131. int ret;
  1132. struct ipa_rt_rule_i rule;
  1133. if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
  1134. IPAERR_RL("bad param\n");
  1135. return -EINVAL;
  1136. }
  1137. mutex_lock(&ipa3_ctx->lock);
  1138. for (i = 0; i < rules->num_rules; i++) {
  1139. rules->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
  1140. /* if hashing not supported, all tables are non-hash tables*/
  1141. if (ipa3_ctx->ipa_fltrt_not_hashable)
  1142. rules->rules[i].rule.hashable = false;
  1143. __ipa_convert_rt_rule_in(rules->rules[i].rule, &rule);
  1144. if (__ipa_add_rt_rule(rules->ip, rules->rt_tbl_name,
  1145. &rule,
  1146. rules->rules[i].at_rear,
  1147. &rules->rules[i].rt_rule_hdl,
  1148. 0,
  1149. user_only)) {
  1150. IPAERR_RL("failed to add rt rule %d\n", i);
  1151. rules->rules[i].status = IPA_RT_STATUS_OF_ADD_FAILED;
  1152. } else {
  1153. __ipa_convert_rt_rule_out(rule, &rules->rules[i].rule);
  1154. rules->rules[i].status = 0;
  1155. }
  1156. }
  1157. if (rules->commit)
  1158. if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
  1159. ret = -EPERM;
  1160. goto bail;
  1161. }
  1162. ret = 0;
  1163. bail:
  1164. mutex_unlock(&ipa3_ctx->lock);
  1165. return ret;
  1166. }
  1167. /**
  1168. * ipa3_add_rt_rule_usr_v2() - Add the specified routing rules
  1169. * to SW and optionally commit to IPA HW
  1170. * @rules: [inout] set of routing rules to add
  1171. * @user_only: [in] indicate installed by userspace module
  1172. *
  1173. * Returns: 0 on success, negative on failure
  1174. *
  1175. * Note: Should not be called from atomic context
  1176. */
  1177. int ipa3_add_rt_rule_usr_v2(struct ipa_ioc_add_rt_rule_v2 *rules,
  1178. bool user_only)
  1179. {
  1180. int i;
  1181. int ret;
  1182. if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
  1183. IPAERR_RL("bad param\n");
  1184. return -EINVAL;
  1185. }
  1186. mutex_lock(&ipa3_ctx->lock);
  1187. for (i = 0; i < rules->num_rules; i++) {
  1188. rules->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
  1189. /* if hashing not supported, all tables are non-hash tables*/
  1190. if (ipa3_ctx->ipa_fltrt_not_hashable)
  1191. ((struct ipa_rt_rule_add_i *)
  1192. rules->rules)[i].rule.hashable = false;
  1193. if (__ipa_add_rt_rule(rules->ip, rules->rt_tbl_name,
  1194. &(((struct ipa_rt_rule_add_i *)
  1195. rules->rules)[i].rule),
  1196. ((struct ipa_rt_rule_add_i *)
  1197. rules->rules)[i].at_rear,
  1198. &(((struct ipa_rt_rule_add_i *)
  1199. rules->rules)[i].rt_rule_hdl),
  1200. 0,
  1201. user_only)) {
  1202. IPAERR_RL("failed to add rt rule %d\n", i);
  1203. ((struct ipa_rt_rule_add_i *)rules->rules)[i].status
  1204. = IPA_RT_STATUS_OF_ADD_FAILED;
  1205. } else {
  1206. ((struct ipa_rt_rule_add_i *)
  1207. rules->rules)[i].status = 0;
  1208. }
  1209. }
  1210. if (rules->commit)
  1211. if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
  1212. ret = -EPERM;
  1213. goto bail;
  1214. }
  1215. ret = 0;
  1216. bail:
  1217. mutex_unlock(&ipa3_ctx->lock);
  1218. return ret;
  1219. }
  1220. /**
  1221. * ipa3_add_rt_rule_ext() - Add the specified routing rules to SW with rule id
  1222. * and optionally commit to IPA HW
  1223. * @rules: [inout] set of routing rules to add
  1224. *
  1225. * Returns: 0 on success, negative on failure
  1226. *
  1227. * Note: Should not be called from atomic context
  1228. */
  1229. int ipa3_add_rt_rule_ext(struct ipa_ioc_add_rt_rule_ext *rules)
  1230. {
  1231. int i;
  1232. int ret;
  1233. struct ipa_rt_rule_i rule;
  1234. if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
  1235. IPAERR_RL("bad param\n");
  1236. return -EINVAL;
  1237. }
  1238. mutex_lock(&ipa3_ctx->lock);
  1239. for (i = 0; i < rules->num_rules; i++) {
  1240. /* if hashing not supported, all tables are non-hash tables*/
  1241. if (ipa3_ctx->ipa_fltrt_not_hashable)
  1242. rules->rules[i].rule.hashable = false;
  1243. __ipa_convert_rt_rule_in(
  1244. rules->rules[i].rule, &rule);
  1245. if (__ipa_add_rt_rule(rules->ip, rules->rt_tbl_name,
  1246. &rule,
  1247. rules->rules[i].at_rear,
  1248. &rules->rules[i].rt_rule_hdl,
  1249. rules->rules[i].rule_id, true)) {
  1250. IPAERR_RL("failed to add rt rule %d\n", i);
  1251. rules->rules[i].status = IPA_RT_STATUS_OF_ADD_FAILED;
  1252. } else {
  1253. __ipa_convert_rt_rule_out(rule, &rules->rules[i].rule);
  1254. rules->rules[i].status = 0;
  1255. }
  1256. }
  1257. if (rules->commit)
  1258. if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
  1259. ret = -EPERM;
  1260. goto bail;
  1261. }
  1262. ret = 0;
  1263. bail:
  1264. mutex_unlock(&ipa3_ctx->lock);
  1265. return ret;
  1266. }
  1267. /**
  1268. * ipa3_add_rt_rule_ext_v2() - Add the specified routing rules
  1269. * to SW with rule id and optionally commit to IPA HW
  1270. * @rules: [inout] set of routing rules to add
  1271. *
  1272. * Returns: 0 on success, negative on failure
  1273. *
  1274. * Note: Should not be called from atomic context
  1275. */
  1276. int ipa3_add_rt_rule_ext_v2(struct ipa_ioc_add_rt_rule_ext_v2 *rules)
  1277. {
  1278. int i;
  1279. int ret;
  1280. if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
  1281. IPAERR_RL("bad param\n");
  1282. return -EINVAL;
  1283. }
  1284. mutex_lock(&ipa3_ctx->lock);
  1285. for (i = 0; i < rules->num_rules; i++) {
  1286. /* if hashing not supported, all tables are non-hash tables*/
  1287. if (ipa3_ctx->ipa_fltrt_not_hashable)
  1288. ((struct ipa_rt_rule_add_ext_i *)
  1289. rules->rules)[i].rule.hashable = false;
  1290. if (__ipa_add_rt_rule(rules->ip, rules->rt_tbl_name,
  1291. &(((struct ipa_rt_rule_add_ext_i *)
  1292. rules->rules)[i].rule),
  1293. ((struct ipa_rt_rule_add_ext_i *)
  1294. rules->rules)[i].at_rear,
  1295. &(((struct ipa_rt_rule_add_ext_i *)
  1296. rules->rules)[i].rt_rule_hdl),
  1297. ((struct ipa_rt_rule_add_ext_i *)
  1298. rules->rules)[i].rule_id, true)) {
  1299. IPAERR_RL("failed to add rt rule %d\n", i);
  1300. ((struct ipa_rt_rule_add_ext_i *)
  1301. rules->rules)[i].status = IPA_RT_STATUS_OF_ADD_FAILED;
  1302. } else {
  1303. ((struct ipa_rt_rule_add_ext_i *)
  1304. rules->rules)[i].status = 0;
  1305. }
  1306. }
  1307. if (rules->commit)
  1308. if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
  1309. ret = -EPERM;
  1310. goto bail;
  1311. }
  1312. ret = 0;
  1313. bail:
  1314. mutex_unlock(&ipa3_ctx->lock);
  1315. return ret;
  1316. }
  1317. /**
  1318. * ipa3_add_rt_rule_after() - Add the given routing rules after the
  1319. * specified rule to SW and optionally commit to IPA HW
  1320. * @rules: [inout] set of routing rules to add + handle where to add
  1321. *
  1322. * Returns: 0 on success, negative on failure
  1323. *
  1324. * Note: Should not be called from atomic context
  1325. */
  1326. int ipa3_add_rt_rule_after(struct ipa_ioc_add_rt_rule_after *rules)
  1327. {
  1328. int i;
  1329. int ret = 0;
  1330. struct ipa3_rt_tbl *tbl = NULL;
  1331. struct ipa3_rt_entry *entry = NULL;
  1332. struct ipa_rt_rule_i rule;
  1333. if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
  1334. IPAERR_RL("bad param\n");
  1335. return -EINVAL;
  1336. }
  1337. mutex_lock(&ipa3_ctx->lock);
  1338. rules->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
  1339. tbl = __ipa3_find_rt_tbl(rules->ip, rules->rt_tbl_name);
  1340. if (tbl == NULL || (tbl->cookie != IPA_RT_TBL_COOKIE)) {
  1341. IPAERR_RL("failed finding rt tbl name = %s\n",
  1342. rules->rt_tbl_name);
  1343. ret = -EINVAL;
  1344. goto bail;
  1345. }
  1346. if (!tbl->rule_cnt) {
  1347. IPAERR_RL("tbl->rule_cnt == 0");
  1348. ret = -EINVAL;
  1349. goto bail;
  1350. }
  1351. entry = ipa3_id_find(rules->add_after_hdl);
  1352. if (!entry) {
  1353. IPAERR_RL("failed finding rule %d in rt tbls\n",
  1354. rules->add_after_hdl);
  1355. ret = -EINVAL;
  1356. goto bail;
  1357. }
  1358. if (entry->cookie != IPA_RT_RULE_COOKIE) {
  1359. IPAERR_RL("Invalid cookie value = %u rule %d in rt tbls\n",
  1360. entry->cookie, rules->add_after_hdl);
  1361. ret = -EINVAL;
  1362. goto bail;
  1363. }
  1364. if (entry->tbl != tbl) {
  1365. IPAERR_RL("given rt rule does not match the table\n");
  1366. ret = -EINVAL;
  1367. goto bail;
  1368. }
  1369. /*
  1370. * do not allow any rule to be added at "default" routing
  1371. * table
  1372. */
  1373. if (!strcmp(tbl->name, IPA_DFLT_RT_TBL_NAME) &&
  1374. (tbl->rule_cnt > 0)) {
  1375. IPAERR_RL("cannot add rules to default rt table\n");
  1376. ret = -EINVAL;
  1377. goto bail;
  1378. }
  1379. /*
  1380. * we add all rules one after the other, if one insertion fails, it cuts
  1381. * the chain (all following will receive fail status) following calls to
  1382. * __ipa_add_rt_rule_after will fail (entry == NULL)
  1383. */
  1384. for (i = 0; i < rules->num_rules; i++) {
  1385. /* if hashing not supported, all tables are non-hash tables*/
  1386. if (ipa3_ctx->ipa_fltrt_not_hashable)
  1387. rules->rules[i].rule.hashable = false;
  1388. __ipa_convert_rt_rule_in(
  1389. rules->rules[i].rule, &rule);
  1390. if (__ipa_add_rt_rule_after(tbl,
  1391. &rule,
  1392. &rules->rules[i].rt_rule_hdl,
  1393. &entry)) {
  1394. IPAERR_RL("failed to add rt rule %d\n", i);
  1395. rules->rules[i].status = IPA_RT_STATUS_OF_ADD_FAILED;
  1396. } else {
  1397. rules->rules[i].status = 0;
  1398. __ipa_convert_rt_rule_out(rule, &rules->rules[i].rule);
  1399. }
  1400. }
  1401. if (rules->commit)
  1402. if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
  1403. IPAERR_RL("failed to commit\n");
  1404. ret = -EPERM;
  1405. goto bail;
  1406. }
  1407. ret = 0;
  1408. goto bail;
  1409. bail:
  1410. mutex_unlock(&ipa3_ctx->lock);
  1411. return ret;
  1412. }
  1413. /**
  1414. * ipa3_add_rt_rule_after_v2() - Add the given routing rules
  1415. * after the specified rule to SW and optionally commit to IPA
  1416. * HW
  1417. * @rules: [inout] set of routing rules to add + handle where to add
  1418. *
  1419. * Returns: 0 on success, negative on failure
  1420. *
  1421. * Note: Should not be called from atomic context
  1422. */
  1423. int ipa3_add_rt_rule_after_v2(struct ipa_ioc_add_rt_rule_after_v2
  1424. *rules)
  1425. {
  1426. int i;
  1427. int ret = 0;
  1428. struct ipa3_rt_tbl *tbl = NULL;
  1429. struct ipa3_rt_entry *entry = NULL;
  1430. if (rules == NULL || rules->num_rules == 0 || rules->ip >= IPA_IP_MAX) {
  1431. IPAERR_RL("bad param\n");
  1432. return -EINVAL;
  1433. }
  1434. mutex_lock(&ipa3_ctx->lock);
  1435. rules->rt_tbl_name[IPA_RESOURCE_NAME_MAX-1] = '\0';
  1436. tbl = __ipa3_find_rt_tbl(rules->ip, rules->rt_tbl_name);
  1437. if (tbl == NULL || (tbl->cookie != IPA_RT_TBL_COOKIE)) {
  1438. IPAERR_RL("failed finding rt tbl name = %s\n",
  1439. rules->rt_tbl_name);
  1440. ret = -EINVAL;
  1441. goto bail;
  1442. }
  1443. if (!tbl->rule_cnt) {
  1444. IPAERR_RL("tbl->rule_cnt == 0");
  1445. ret = -EINVAL;
  1446. goto bail;
  1447. }
  1448. entry = ipa3_id_find(rules->add_after_hdl);
  1449. if (!entry) {
  1450. IPAERR_RL("failed finding rule %d in rt tbls\n",
  1451. rules->add_after_hdl);
  1452. ret = -EINVAL;
  1453. goto bail;
  1454. }
  1455. if (entry->cookie != IPA_RT_RULE_COOKIE) {
  1456. IPAERR_RL("Invalid cookie value = %u rule %d in rt tbls\n",
  1457. entry->cookie, rules->add_after_hdl);
  1458. ret = -EINVAL;
  1459. goto bail;
  1460. }
  1461. if (entry->tbl != tbl) {
  1462. IPAERR_RL("given rt rule does not match the table\n");
  1463. ret = -EINVAL;
  1464. goto bail;
  1465. }
  1466. /*
  1467. * do not allow any rule to be added at "default" routing
  1468. * table
  1469. */
  1470. if (!strcmp(tbl->name, IPA_DFLT_RT_TBL_NAME) &&
  1471. (tbl->rule_cnt > 0)) {
  1472. IPAERR_RL("cannot add rules to default rt table\n");
  1473. ret = -EINVAL;
  1474. goto bail;
  1475. }
  1476. /*
  1477. * we add all rules one after the other, if one insertion fails, it cuts
  1478. * the chain (all following will receive fail status) following calls to
  1479. * __ipa_add_rt_rule_after will fail (entry == NULL)
  1480. */
  1481. for (i = 0; i < rules->num_rules; i++) {
  1482. /* if hashing not supported, all tables are non-hash tables*/
  1483. if (ipa3_ctx->ipa_fltrt_not_hashable)
  1484. ((struct ipa_rt_rule_add_i *)
  1485. rules->rules)[i].rule.hashable = false;
  1486. if (__ipa_add_rt_rule_after(tbl,
  1487. &(((struct ipa_rt_rule_add_i *)
  1488. rules->rules)[i].rule),
  1489. &(((struct ipa_rt_rule_add_i *)
  1490. rules->rules)[i].rt_rule_hdl),
  1491. &entry)) {
  1492. IPAERR_RL("failed to add rt rule %d\n", i);
  1493. ((struct ipa_rt_rule_add_i *)
  1494. rules->rules)[i].status = IPA_RT_STATUS_OF_ADD_FAILED;
  1495. } else {
  1496. ((struct ipa_rt_rule_add_i *)
  1497. rules->rules)[i].status = 0;
  1498. }
  1499. }
  1500. if (rules->commit)
  1501. if (ipa3_ctx->ctrl->ipa3_commit_rt(rules->ip)) {
  1502. IPAERR_RL("failed to commit\n");
  1503. ret = -EPERM;
  1504. goto bail;
  1505. }
  1506. ret = 0;
  1507. goto bail;
  1508. bail:
  1509. mutex_unlock(&ipa3_ctx->lock);
  1510. return ret;
  1511. }
  1512. int __ipa3_del_rt_rule(u32 rule_hdl)
  1513. {
  1514. struct ipa3_rt_entry *entry;
  1515. int id;
  1516. struct ipa3_hdr_entry *hdr_entry;
  1517. struct ipa3_hdr_proc_ctx_entry *hdr_proc_entry;
  1518. entry = ipa3_id_find(rule_hdl);
  1519. if (entry == NULL) {
  1520. IPAERR_RL("lookup failed\n");
  1521. return -EINVAL;
  1522. }
  1523. if (entry->cookie != IPA_RT_RULE_COOKIE) {
  1524. IPAERR_RL("bad params\n");
  1525. return -EINVAL;
  1526. }
  1527. if (!strcmp(entry->tbl->name, IPA_DFLT_RT_TBL_NAME)) {
  1528. IPADBG("Deleting rule from default rt table idx=%u\n",
  1529. entry->tbl->idx);
  1530. if (entry->tbl->rule_cnt == 1) {
  1531. IPAERR_RL("Default tbl last rule cannot be deleted\n");
  1532. return -EINVAL;
  1533. }
  1534. }
  1535. /* Adding check to confirm still
  1536. * header entry present in header table or not
  1537. */
  1538. if (entry->hdr) {
  1539. hdr_entry = ipa3_id_find(entry->rule.hdr_hdl);
  1540. if (!hdr_entry || hdr_entry->cookie != IPA_HDR_COOKIE) {
  1541. IPAERR_RL("Header entry already deleted\n");
  1542. return -EINVAL;
  1543. }
  1544. } else if (entry->proc_ctx) {
  1545. hdr_proc_entry = ipa3_id_find(entry->rule.hdr_proc_ctx_hdl);
  1546. if (!hdr_proc_entry ||
  1547. hdr_proc_entry->cookie != IPA_PROC_HDR_COOKIE) {
  1548. IPAERR_RL("Proc header entry already deleted\n");
  1549. return -EINVAL;
  1550. }
  1551. }
  1552. if (entry->hdr)
  1553. __ipa3_release_hdr(entry->hdr->id);
  1554. else if (entry->proc_ctx)
  1555. __ipa3_release_hdr_proc_ctx(entry->proc_ctx->id);
  1556. list_del(&entry->link);
  1557. entry->tbl->rule_cnt--;
  1558. IPADBG("del rt rule tbl_idx=%d rule_cnt=%d rule_id=%d\n ref_cnt=%u",
  1559. entry->tbl->idx, entry->tbl->rule_cnt,
  1560. entry->rule_id, entry->tbl->ref_cnt);
  1561. /* if rule id was allocated from idr, remove it */
  1562. if (!entry->rule_id_valid)
  1563. idr_remove(entry->tbl->rule_ids, entry->rule_id);
  1564. if (entry->tbl->rule_cnt == 0 && entry->tbl->ref_cnt == 0) {
  1565. if (__ipa_del_rt_tbl(entry->tbl))
  1566. IPAERR_RL("fail to del RT tbl\n");
  1567. }
  1568. entry->cookie = 0;
  1569. id = entry->id;
  1570. kmem_cache_free(ipa3_ctx->rt_rule_cache, entry);
  1571. /* remove the handle from the database */
  1572. ipa3_id_remove(id);
  1573. return 0;
  1574. }
  1575. /**
  1576. * ipa3_del_rt_rule() - Remove the specified routing rules to SW and optionally
  1577. * commit to IPA HW
  1578. * @hdls: [inout] set of routing rules to delete
  1579. *
  1580. * Returns: 0 on success, negative on failure
  1581. *
  1582. * Note: Should not be called from atomic context
  1583. */
  1584. int ipa3_del_rt_rule(struct ipa_ioc_del_rt_rule *hdls)
  1585. {
  1586. int i;
  1587. int ret;
  1588. if (hdls == NULL || hdls->num_hdls == 0 || hdls->ip >= IPA_IP_MAX) {
  1589. IPAERR_RL("bad param\n");
  1590. return -EINVAL;
  1591. }
  1592. mutex_lock(&ipa3_ctx->lock);
  1593. for (i = 0; i < hdls->num_hdls; i++) {
  1594. if (__ipa3_del_rt_rule(hdls->hdl[i].hdl)) {
  1595. IPAERR_RL("failed to del rt rule %i\n", i);
  1596. hdls->hdl[i].status = IPA_RT_STATUS_OF_DEL_FAILED;
  1597. } else {
  1598. hdls->hdl[i].status = 0;
  1599. }
  1600. }
  1601. if (hdls->commit)
  1602. if (ipa3_ctx->ctrl->ipa3_commit_rt(hdls->ip)) {
  1603. ret = -EPERM;
  1604. goto bail;
  1605. }
  1606. ret = 0;
  1607. bail:
  1608. mutex_unlock(&ipa3_ctx->lock);
  1609. return ret;
  1610. }
  1611. /**
  1612. * ipa_commit_rt_rule() - Commit the current SW routing table of specified type
  1613. * to IPA HW
  1614. * @ip: The family of routing tables
  1615. *
  1616. * Returns: 0 on success, negative on failure
  1617. *
  1618. * Note: Should not be called from atomic context
  1619. */
  1620. int ipa3_commit_rt(enum ipa_ip_type ip)
  1621. {
  1622. int ret;
  1623. if (ip >= IPA_IP_MAX) {
  1624. IPAERR_RL("bad param\n");
  1625. return -EINVAL;
  1626. }
  1627. /*
  1628. * issue a commit on the filtering module of same IP type since
  1629. * filtering rules point to routing tables
  1630. */
  1631. if (ipa3_commit_flt(ip))
  1632. return -EPERM;
  1633. mutex_lock(&ipa3_ctx->lock);
  1634. if (ipa3_ctx->ctrl->ipa3_commit_rt(ip)) {
  1635. ret = -EPERM;
  1636. goto bail;
  1637. }
  1638. ret = 0;
  1639. bail:
  1640. mutex_unlock(&ipa3_ctx->lock);
  1641. return ret;
  1642. }
  1643. /**
  1644. * ipa3_reset_rt() - reset the current SW routing table of specified type
  1645. * (does not commit to HW)
  1646. * @ip: [in] The family of routing tables
  1647. * @user_only: [in] indicate delete rules installed by userspace
  1648. *
  1649. * Returns: 0 on success, negative on failure
  1650. *
  1651. * Note: Should not be called from atomic context
  1652. */
  1653. int ipa3_reset_rt(enum ipa_ip_type ip, bool user_only)
  1654. {
  1655. struct ipa3_rt_tbl *tbl;
  1656. struct ipa3_rt_tbl *tbl_next;
  1657. struct ipa3_rt_tbl_set *set;
  1658. struct ipa3_rt_entry *rule;
  1659. struct ipa3_rt_entry *rule_next;
  1660. struct ipa3_rt_tbl_set *rset;
  1661. struct ipa3_hdr_entry *hdr_entry;
  1662. struct ipa3_hdr_proc_ctx_entry *hdr_proc_entry;
  1663. u32 apps_start_idx;
  1664. int id;
  1665. bool tbl_user = false;
  1666. if (ip >= IPA_IP_MAX) {
  1667. IPAERR_RL("bad param\n");
  1668. return -EINVAL;
  1669. }
  1670. if (ip == IPA_IP_v4)
  1671. apps_start_idx =
  1672. IPA_MEM_PART(v4_apps_rt_index_lo);
  1673. else
  1674. apps_start_idx =
  1675. IPA_MEM_PART(v6_apps_rt_index_lo);
  1676. /*
  1677. * issue a reset on the filtering module of same IP type since
  1678. * filtering rules point to routing tables
  1679. */
  1680. if (ipa3_reset_flt(ip, user_only))
  1681. IPAERR_RL("fail to reset flt ip=%d\n", ip);
  1682. set = &ipa3_ctx->rt_tbl_set[ip];
  1683. rset = &ipa3_ctx->reap_rt_tbl_set[ip];
  1684. mutex_lock(&ipa3_ctx->lock);
  1685. IPADBG("reset rt ip=%d\n", ip);
  1686. list_for_each_entry_safe(tbl, tbl_next, &set->head_rt_tbl_list, link) {
  1687. tbl_user = false;
  1688. list_for_each_entry_safe(rule, rule_next,
  1689. &tbl->head_rt_rule_list, link) {
  1690. if (ipa3_id_find(rule->id) == NULL) {
  1691. WARN_ON_RATELIMIT_IPA(1);
  1692. mutex_unlock(&ipa3_ctx->lock);
  1693. return -EFAULT;
  1694. }
  1695. /* indicate if tbl used for user-specified rules*/
  1696. if (rule->ipacm_installed) {
  1697. IPADBG("tbl_user %d, tbl-index %d\n",
  1698. tbl_user, tbl->id);
  1699. tbl_user = true;
  1700. }
  1701. /*
  1702. * for the "default" routing tbl, remove all but the
  1703. * last rule
  1704. */
  1705. if (tbl->idx == apps_start_idx && tbl->rule_cnt == 1)
  1706. continue;
  1707. if (!user_only ||
  1708. rule->ipacm_installed) {
  1709. list_del(&rule->link);
  1710. if (rule->hdr) {
  1711. hdr_entry = ipa3_id_find(
  1712. rule->rule.hdr_hdl);
  1713. if (!hdr_entry ||
  1714. hdr_entry->cookie != IPA_HDR_COOKIE) {
  1715. mutex_unlock(&ipa3_ctx->lock);
  1716. IPAERR_RL(
  1717. "Header already deleted\n");
  1718. return -EINVAL;
  1719. }
  1720. } else if (rule->proc_ctx) {
  1721. hdr_proc_entry =
  1722. ipa3_id_find(
  1723. rule->rule.hdr_proc_ctx_hdl);
  1724. if (!hdr_proc_entry ||
  1725. hdr_proc_entry->cookie !=
  1726. IPA_PROC_HDR_COOKIE) {
  1727. mutex_unlock(&ipa3_ctx->lock);
  1728. IPAERR_RL(
  1729. "Proc entry already deleted\n");
  1730. return -EINVAL;
  1731. }
  1732. }
  1733. tbl->rule_cnt--;
  1734. if (rule->hdr)
  1735. __ipa3_release_hdr(rule->hdr->id);
  1736. else if (rule->proc_ctx)
  1737. __ipa3_release_hdr_proc_ctx(
  1738. rule->proc_ctx->id);
  1739. rule->cookie = 0;
  1740. if (!rule->rule_id_valid)
  1741. idr_remove(tbl->rule_ids,
  1742. rule->rule_id);
  1743. id = rule->id;
  1744. kmem_cache_free(ipa3_ctx->rt_rule_cache, rule);
  1745. /* remove the handle from the database */
  1746. ipa3_id_remove(id);
  1747. }
  1748. }
  1749. if (ipa3_id_find(tbl->id) == NULL) {
  1750. WARN_ON_RATELIMIT_IPA(1);
  1751. mutex_unlock(&ipa3_ctx->lock);
  1752. return -EFAULT;
  1753. }
  1754. id = tbl->id;
  1755. /* do not remove the "default" routing tbl which has index 0 */
  1756. if (tbl->idx != apps_start_idx) {
  1757. if (!user_only || tbl_user) {
  1758. tbl->rule_ids = NULL;
  1759. if (tbl->in_sys[IPA_RULE_HASHABLE] ||
  1760. tbl->in_sys[IPA_RULE_NON_HASHABLE]) {
  1761. list_move(&tbl->link,
  1762. &rset->head_rt_tbl_list);
  1763. clear_bit(tbl->idx,
  1764. &ipa3_ctx->rt_idx_bitmap[ip]);
  1765. set->tbl_cnt--;
  1766. IPADBG("rst tbl_idx=%d cnt=%d\n",
  1767. tbl->idx, set->tbl_cnt);
  1768. } else {
  1769. list_del(&tbl->link);
  1770. set->tbl_cnt--;
  1771. clear_bit(tbl->idx,
  1772. &ipa3_ctx->rt_idx_bitmap[ip]);
  1773. IPADBG("rst rt tbl_idx=%d tbl_cnt=%d\n",
  1774. tbl->idx, set->tbl_cnt);
  1775. kmem_cache_free(ipa3_ctx->rt_tbl_cache,
  1776. tbl);
  1777. }
  1778. /* remove the handle from the database */
  1779. ipa3_id_remove(id);
  1780. }
  1781. }
  1782. }
  1783. /* commit the change to IPA-HW */
  1784. if (ipa3_ctx->ctrl->ipa3_commit_rt(IPA_IP_v4) ||
  1785. ipa3_ctx->ctrl->ipa3_commit_rt(IPA_IP_v6)) {
  1786. IPAERR("fail to commit rt-rule\n");
  1787. WARN_ON_RATELIMIT_IPA(1);
  1788. mutex_unlock(&ipa3_ctx->lock);
  1789. return -EPERM;
  1790. }
  1791. mutex_unlock(&ipa3_ctx->lock);
  1792. return 0;
  1793. }
  1794. /**
  1795. * ipa3_get_rt_tbl() - lookup the specified routing table and return handle if
  1796. * it exists, if lookup succeeds the routing table ref cnt is increased
  1797. * @lookup: [inout] routing table to lookup and its handle
  1798. *
  1799. * Returns: 0 on success, negative on failure
  1800. *
  1801. * Note: Should not be called from atomic context
  1802. * Caller should call ipa3_put_rt_tbl later if this function succeeds
  1803. */
  1804. int ipa3_get_rt_tbl(struct ipa_ioc_get_rt_tbl *lookup)
  1805. {
  1806. struct ipa3_rt_tbl *entry;
  1807. int result = -EFAULT;
  1808. if (lookup == NULL || lookup->ip >= IPA_IP_MAX) {
  1809. IPAERR_RL("bad param\n");
  1810. return -EINVAL;
  1811. }
  1812. mutex_lock(&ipa3_ctx->lock);
  1813. lookup->name[IPA_RESOURCE_NAME_MAX-1] = '\0';
  1814. entry = __ipa3_find_rt_tbl(lookup->ip, lookup->name);
  1815. if (entry && entry->cookie == IPA_RT_TBL_COOKIE) {
  1816. if (entry->ref_cnt == U32_MAX) {
  1817. IPAERR_RL("fail: ref count crossed limit\n");
  1818. goto ret;
  1819. }
  1820. entry->ref_cnt++;
  1821. lookup->hdl = entry->id;
  1822. /* commit for get */
  1823. if (ipa3_ctx->ctrl->ipa3_commit_rt(lookup->ip))
  1824. IPAERR_RL("fail to commit RT tbl\n");
  1825. result = 0;
  1826. }
  1827. ret:
  1828. mutex_unlock(&ipa3_ctx->lock);
  1829. return result;
  1830. }
  1831. /**
  1832. * ipa3_put_rt_tbl() - Release the specified routing table handle
  1833. * @rt_tbl_hdl: [in] the routing table handle to release
  1834. *
  1835. * Returns: 0 on success, negative on failure
  1836. *
  1837. * Note: Should not be called from atomic context
  1838. */
  1839. int ipa3_put_rt_tbl(u32 rt_tbl_hdl)
  1840. {
  1841. struct ipa3_rt_tbl *entry;
  1842. enum ipa_ip_type ip = IPA_IP_MAX;
  1843. int result = 0;
  1844. mutex_lock(&ipa3_ctx->lock);
  1845. entry = ipa3_id_find(rt_tbl_hdl);
  1846. if (entry == NULL) {
  1847. IPAERR_RL("lookup failed\n");
  1848. result = -EINVAL;
  1849. goto ret;
  1850. }
  1851. if ((entry->cookie != IPA_RT_TBL_COOKIE) || entry->ref_cnt == 0) {
  1852. IPAERR_RL("bad params\n");
  1853. result = -EINVAL;
  1854. goto ret;
  1855. }
  1856. if (entry->set == &ipa3_ctx->rt_tbl_set[IPA_IP_v4])
  1857. ip = IPA_IP_v4;
  1858. else if (entry->set == &ipa3_ctx->rt_tbl_set[IPA_IP_v6])
  1859. ip = IPA_IP_v6;
  1860. else {
  1861. WARN_ON_RATELIMIT_IPA(1);
  1862. result = -EINVAL;
  1863. goto ret;
  1864. }
  1865. entry->ref_cnt--;
  1866. if (entry->ref_cnt == 0 && entry->rule_cnt == 0) {
  1867. IPADBG("zero ref_cnt, delete rt tbl (idx=%u)\n",
  1868. entry->idx);
  1869. if (__ipa_del_rt_tbl(entry))
  1870. IPAERR_RL("fail to del RT tbl\n");
  1871. /* commit for put */
  1872. if (ipa3_ctx->ctrl->ipa3_commit_rt(ip))
  1873. IPAERR_RL("fail to commit RT tbl\n");
  1874. }
  1875. result = 0;
  1876. ret:
  1877. mutex_unlock(&ipa3_ctx->lock);
  1878. return result;
  1879. }
  1880. static int __ipa_mdfy_rt_rule(struct ipa_rt_rule_mdfy_i *rtrule)
  1881. {
  1882. struct ipa3_rt_entry *entry;
  1883. struct ipa3_hdr_entry *hdr = NULL;
  1884. struct ipa3_hdr_proc_ctx_entry *proc_ctx = NULL;
  1885. struct ipa3_hdr_entry *hdr_entry;
  1886. struct ipa3_hdr_proc_ctx_entry *hdr_proc_entry;
  1887. if (rtrule->rule.hdr_hdl) {
  1888. hdr = ipa3_id_find(rtrule->rule.hdr_hdl);
  1889. if ((hdr == NULL) || (hdr->cookie != IPA_HDR_COOKIE)) {
  1890. IPAERR_RL("rt rule does not point to valid hdr\n");
  1891. goto error;
  1892. }
  1893. } else if (rtrule->rule.hdr_proc_ctx_hdl) {
  1894. proc_ctx = ipa3_id_find(rtrule->rule.hdr_proc_ctx_hdl);
  1895. if ((proc_ctx == NULL) ||
  1896. (proc_ctx->cookie != IPA_PROC_HDR_COOKIE)) {
  1897. IPAERR_RL("rt rule does not point to valid proc ctx\n");
  1898. goto error;
  1899. }
  1900. }
  1901. entry = ipa3_id_find(rtrule->rt_rule_hdl);
  1902. if (entry == NULL) {
  1903. IPAERR_RL("lookup failed\n");
  1904. goto error;
  1905. }
  1906. if (entry->cookie != IPA_RT_RULE_COOKIE) {
  1907. IPAERR_RL("bad params\n");
  1908. goto error;
  1909. }
  1910. if (!strcmp(entry->tbl->name, IPA_DFLT_RT_TBL_NAME)) {
  1911. IPAERR_RL("Default tbl rule cannot be modified\n");
  1912. return -EINVAL;
  1913. }
  1914. /* Adding check to confirm still
  1915. * header entry present in header table or not
  1916. */
  1917. if (entry->hdr) {
  1918. hdr_entry = ipa3_id_find(entry->rule.hdr_hdl);
  1919. if (!hdr_entry || hdr_entry->cookie != IPA_HDR_COOKIE) {
  1920. IPAERR_RL("Header entry already deleted\n");
  1921. return -EPERM;
  1922. }
  1923. } else if (entry->proc_ctx) {
  1924. hdr_proc_entry = ipa3_id_find(entry->rule.hdr_proc_ctx_hdl);
  1925. if (!hdr_proc_entry ||
  1926. hdr_proc_entry->cookie != IPA_PROC_HDR_COOKIE) {
  1927. IPAERR_RL("Proc header entry already deleted\n");
  1928. return -EPERM;
  1929. }
  1930. }
  1931. if (entry->hdr)
  1932. entry->hdr->ref_cnt--;
  1933. if (entry->proc_ctx)
  1934. entry->proc_ctx->ref_cnt--;
  1935. entry->rule = rtrule->rule;
  1936. entry->hdr = hdr;
  1937. entry->proc_ctx = proc_ctx;
  1938. if (entry->hdr)
  1939. entry->hdr->ref_cnt++;
  1940. if (entry->proc_ctx)
  1941. entry->proc_ctx->ref_cnt++;
  1942. entry->hw_len = 0;
  1943. entry->prio = 0;
  1944. if (rtrule->rule.enable_stats)
  1945. entry->cnt_idx = rtrule->rule.cnt_idx;
  1946. else
  1947. entry->cnt_idx = 0;
  1948. return 0;
  1949. error:
  1950. return -EPERM;
  1951. }
  1952. /**
  1953. * ipa3_mdfy_rt_rule() - Modify the specified routing rules in SW and optionally
  1954. * commit to IPA HW
  1955. *
  1956. * Returns: 0 on success, negative on failure
  1957. *
  1958. * Note: Should not be called from atomic context
  1959. */
  1960. int ipa3_mdfy_rt_rule(struct ipa_ioc_mdfy_rt_rule *hdls)
  1961. {
  1962. int i;
  1963. int result;
  1964. struct ipa_rt_rule_mdfy_i rule;
  1965. if (hdls == NULL || hdls->num_rules == 0 || hdls->ip >= IPA_IP_MAX) {
  1966. IPAERR_RL("bad param\n");
  1967. return -EINVAL;
  1968. }
  1969. mutex_lock(&ipa3_ctx->lock);
  1970. for (i = 0; i < hdls->num_rules; i++) {
  1971. /* if hashing not supported, all tables are non-hash tables*/
  1972. if (ipa3_ctx->ipa_fltrt_not_hashable)
  1973. hdls->rules[i].rule.hashable = false;
  1974. __ipa_convert_rt_mdfy_in(hdls->rules[i], &rule);
  1975. if (__ipa_mdfy_rt_rule(&rule)) {
  1976. IPAERR_RL("failed to mdfy rt rule %i\n", i);
  1977. hdls->rules[i].status = IPA_RT_STATUS_OF_MDFY_FAILED;
  1978. } else {
  1979. hdls->rules[i].status = 0;
  1980. __ipa_convert_rt_mdfy_out(rule, &hdls->rules[i]);
  1981. }
  1982. }
  1983. if (hdls->commit)
  1984. if (ipa3_ctx->ctrl->ipa3_commit_rt(hdls->ip)) {
  1985. result = -EPERM;
  1986. goto bail;
  1987. }
  1988. result = 0;
  1989. bail:
  1990. mutex_unlock(&ipa3_ctx->lock);
  1991. return result;
  1992. }
  1993. /**
  1994. * ipa3_mdfy_rt_rule_v2() - Modify the specified routing rules
  1995. * in SW and optionally commit to IPA HW
  1996. *
  1997. * Returns: 0 on success, negative on failure
  1998. *
  1999. * Note: Should not be called from atomic context
  2000. */
  2001. int ipa3_mdfy_rt_rule_v2(struct ipa_ioc_mdfy_rt_rule_v2 *hdls)
  2002. {
  2003. int i;
  2004. int result;
  2005. if (hdls == NULL || hdls->num_rules == 0 || hdls->ip >= IPA_IP_MAX) {
  2006. IPAERR_RL("bad param\n");
  2007. return -EINVAL;
  2008. }
  2009. mutex_lock(&ipa3_ctx->lock);
  2010. for (i = 0; i < hdls->num_rules; i++) {
  2011. /* if hashing not supported, all tables are non-hash tables*/
  2012. if (ipa3_ctx->ipa_fltrt_not_hashable)
  2013. ((struct ipa_rt_rule_mdfy_i *)
  2014. hdls->rules)[i].rule.hashable = false;
  2015. if (__ipa_mdfy_rt_rule(&(((struct ipa_rt_rule_mdfy_i *)
  2016. hdls->rules)[i]))) {
  2017. IPAERR_RL("failed to mdfy rt rule %i\n", i);
  2018. ((struct ipa_rt_rule_mdfy_i *)
  2019. hdls->rules)[i].status = IPA_RT_STATUS_OF_MDFY_FAILED;
  2020. } else {
  2021. ((struct ipa_rt_rule_mdfy_i *)
  2022. hdls->rules)[i].status = 0;
  2023. }
  2024. }
  2025. if (hdls->commit)
  2026. if (ipa3_ctx->ctrl->ipa3_commit_rt(hdls->ip)) {
  2027. result = -EPERM;
  2028. goto bail;
  2029. }
  2030. result = 0;
  2031. bail:
  2032. mutex_unlock(&ipa3_ctx->lock);
  2033. return result;
  2034. }
  2035. /**
  2036. * ipa3_set_rt_tuple_mask() - Sets the rt tuple masking for the given tbl
  2037. * table index must be for AP EP (not modem)
  2038. * updates the the routing masking values without changing the flt ones.
  2039. *
  2040. * @tbl_idx: routing table index to configure the tuple masking
  2041. * @tuple: the tuple members masking
  2042. * Returns: 0 on success, negative on failure
  2043. *
  2044. */
  2045. int ipa3_set_rt_tuple_mask(int tbl_idx, struct ipahal_reg_hash_tuple *tuple)
  2046. {
  2047. struct ipahal_reg_fltrt_hash_tuple fltrt_tuple;
  2048. if (!tuple) {
  2049. IPAERR_RL("bad tuple\n");
  2050. return -EINVAL;
  2051. }
  2052. if (tbl_idx >=
  2053. max(IPA_MEM_PART(v6_rt_num_index),
  2054. IPA_MEM_PART(v4_rt_num_index)) ||
  2055. tbl_idx < 0) {
  2056. IPAERR_RL("bad table index\n");
  2057. return -EINVAL;
  2058. }
  2059. if (tbl_idx >= IPA_MEM_PART(v4_modem_rt_index_lo) &&
  2060. tbl_idx <= IPA_MEM_PART(v4_modem_rt_index_hi)) {
  2061. IPAERR_RL("cannot configure modem v4 rt tuple by AP\n");
  2062. return -EINVAL;
  2063. }
  2064. if (tbl_idx >= IPA_MEM_PART(v6_modem_rt_index_lo) &&
  2065. tbl_idx <= IPA_MEM_PART(v6_modem_rt_index_hi)) {
  2066. IPAERR_RL("cannot configure modem v6 rt tuple by AP\n");
  2067. return -EINVAL;
  2068. }
  2069. ipahal_read_reg_n_fields(IPA_ENDP_FILTER_ROUTER_HSH_CFG_n,
  2070. tbl_idx, &fltrt_tuple);
  2071. fltrt_tuple.rt = *tuple;
  2072. ipahal_write_reg_n_fields(IPA_ENDP_FILTER_ROUTER_HSH_CFG_n,
  2073. tbl_idx, &fltrt_tuple);
  2074. return 0;
  2075. }
  2076. /**
  2077. * ipa3_rt_read_tbl_from_hw() -Read routing table from IPA HW
  2078. * @tbl_idx: routing table index
  2079. * @ip_type: IPv4 or IPv6 table
  2080. * @hashable: hashable or non-hashable table
  2081. * @entry: array to fill the table entries
  2082. * @num_entry: number of entries in entry array. set by the caller to indicate
  2083. * entry array size. Then set by this function as an output parameter to
  2084. * indicate the number of entries in the array
  2085. *
  2086. * This function reads the routing table from IPA SRAM and prepares an array
  2087. * of entries. This function is mainly used for debugging purposes.
  2088. *
  2089. * If empty table or Modem Apps table, zero entries will be returned.
  2090. *
  2091. * Returns: 0 on success, negative on failure
  2092. */
  2093. int ipa3_rt_read_tbl_from_hw(u32 tbl_idx, enum ipa_ip_type ip_type,
  2094. bool hashable, struct ipahal_rt_rule_entry entry[], int *num_entry)
  2095. {
  2096. void *ipa_sram_mmio;
  2097. u64 hdr_base_ofst;
  2098. int res = 0;
  2099. u64 tbl_addr;
  2100. bool is_sys;
  2101. struct ipa_mem_buffer *sys_tbl_mem;
  2102. u8 *rule_addr;
  2103. int rule_idx;
  2104. IPADBG_LOW("tbl_idx=%d ip_t=%d hash=%d entry=0x%pK num_entry=0x%pK\n",
  2105. tbl_idx, ip_type, hashable, entry, num_entry);
  2106. /*
  2107. * SRAM memory not allocated to hash tables. Reading of hash table
  2108. * rules operation not supported
  2109. */
  2110. if (hashable && ipa3_ctx->ipa_fltrt_not_hashable) {
  2111. IPADBG("Reading hashable rules not supported\n");
  2112. *num_entry = 0;
  2113. return 0;
  2114. }
  2115. if (ip_type == IPA_IP_v4 && tbl_idx >= IPA_MEM_PART(v4_rt_num_index)) {
  2116. IPAERR_RL("Invalid params\n");
  2117. return -EFAULT;
  2118. }
  2119. if (ip_type == IPA_IP_v6 && tbl_idx >= IPA_MEM_PART(v6_rt_num_index)) {
  2120. IPAERR_RL("Invalid params\n");
  2121. return -EFAULT;
  2122. }
  2123. /* map IPA SRAM */
  2124. ipa_sram_mmio = ioremap(ipa3_ctx->ipa_wrapper_base +
  2125. ipa3_ctx->ctrl->ipa_reg_base_ofst +
  2126. ipahal_get_reg_n_ofst(IPA_SW_AREA_RAM_DIRECT_ACCESS_n,
  2127. ipa3_ctx->smem_restricted_bytes / 4),
  2128. ipa3_ctx->smem_sz);
  2129. if (!ipa_sram_mmio) {
  2130. IPAERR("fail to ioremap IPA SRAM\n");
  2131. return -ENOMEM;
  2132. }
  2133. memset(entry, 0, sizeof(*entry) * (*num_entry));
  2134. if (hashable) {
  2135. if (ip_type == IPA_IP_v4)
  2136. hdr_base_ofst =
  2137. IPA_MEM_PART(v4_rt_hash_ofst);
  2138. else
  2139. hdr_base_ofst =
  2140. IPA_MEM_PART(v6_rt_hash_ofst);
  2141. } else {
  2142. if (ip_type == IPA_IP_v4)
  2143. hdr_base_ofst =
  2144. IPA_MEM_PART(v4_rt_nhash_ofst);
  2145. else
  2146. hdr_base_ofst =
  2147. IPA_MEM_PART(v6_rt_nhash_ofst);
  2148. }
  2149. IPADBG_LOW("hdr_base_ofst=0x%llx\n", hdr_base_ofst);
  2150. res = ipahal_fltrt_read_addr_from_hdr(ipa_sram_mmio + hdr_base_ofst,
  2151. tbl_idx, &tbl_addr, &is_sys);
  2152. if (res) {
  2153. IPAERR("failed to read table address from header structure\n");
  2154. goto bail;
  2155. }
  2156. IPADBG_LOW("rt tbl %d: tbl_addr=0x%llx is_sys=%d\n",
  2157. tbl_idx, tbl_addr, is_sys);
  2158. if (!tbl_addr) {
  2159. IPAERR("invalid rt tbl addr\n");
  2160. res = -EFAULT;
  2161. goto bail;
  2162. }
  2163. /* for tables which reside in DDR access it from the virtual memory */
  2164. if (is_sys) {
  2165. struct ipa3_rt_tbl_set *set;
  2166. struct ipa3_rt_tbl *tbl;
  2167. set = &ipa3_ctx->rt_tbl_set[ip_type];
  2168. rule_addr = NULL;
  2169. list_for_each_entry(tbl, &set->head_rt_tbl_list, link) {
  2170. if (tbl->idx == tbl_idx) {
  2171. sys_tbl_mem = &(tbl->curr_mem[hashable ?
  2172. IPA_RULE_HASHABLE :
  2173. IPA_RULE_NON_HASHABLE]);
  2174. if (sys_tbl_mem->phys_base &&
  2175. sys_tbl_mem->phys_base != tbl_addr) {
  2176. IPAERR("mismatch:parsed=%llx sw=%pad\n"
  2177. , tbl_addr,
  2178. &sys_tbl_mem->phys_base);
  2179. }
  2180. if (sys_tbl_mem->phys_base)
  2181. rule_addr = sys_tbl_mem->base;
  2182. else
  2183. rule_addr = NULL;
  2184. }
  2185. }
  2186. } else {
  2187. rule_addr = ipa_sram_mmio + hdr_base_ofst + tbl_addr;
  2188. }
  2189. IPADBG_LOW("First rule addr 0x%pK\n", rule_addr);
  2190. if (!rule_addr) {
  2191. /* Modem table in system memory or empty table */
  2192. *num_entry = 0;
  2193. goto bail;
  2194. }
  2195. rule_idx = 0;
  2196. while (rule_idx < *num_entry) {
  2197. res = ipahal_rt_parse_hw_rule(rule_addr, &entry[rule_idx]);
  2198. if (res) {
  2199. IPAERR("failed parsing rt rule\n");
  2200. goto bail;
  2201. }
  2202. IPADBG_LOW("rule_size=%d\n", entry[rule_idx].rule_size);
  2203. if (!entry[rule_idx].rule_size)
  2204. break;
  2205. rule_addr += entry[rule_idx].rule_size;
  2206. rule_idx++;
  2207. }
  2208. *num_entry = rule_idx;
  2209. bail:
  2210. iounmap(ipa_sram_mmio);
  2211. return res;
  2212. }