ipa_rt.c 66 KB

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