ipahal_fltrt.c 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/ipa.h>
  6. #include <linux/errno.h>
  7. #include <linux/ipc_logging.h>
  8. #include <linux/debugfs.h>
  9. #include "ipahal.h"
  10. #include "ipahal_fltrt.h"
  11. #include "ipahal_fltrt_i.h"
  12. #include "ipahal_i.h"
  13. #include "../../ipa_common_i.h"
  14. /*
  15. * struct ipahal_fltrt_obj - Flt/Rt H/W information for specific IPA version
  16. * @support_hash: Is hashable tables supported
  17. * @tbl_width: Width of table in bytes
  18. * @sysaddr_alignment: System table address alignment
  19. * @lcladdr_alignment: Local table offset alignment
  20. * @blk_sz_alignment: Rules block size alignment
  21. * @rule_start_alignment: Rule start address alignment
  22. * @tbl_hdr_width: Width of the header structure in bytes
  23. * @tbl_addr_mask: Masking for Table address
  24. * @rule_max_prio: Max possible priority of a rule
  25. * @rule_min_prio: Min possible priority of a rule
  26. * @low_rule_id: Low value of Rule ID that can be used
  27. * @rule_id_bit_len: Rule is high (MSB) bit len
  28. * @rule_buf_size: Max size rule may utilize.
  29. * @write_val_to_hdr: Write address or offset to header entry
  30. * @create_flt_bitmap: Create bitmap in H/W format using given bitmap
  31. * @create_tbl_addr: Given raw table address, create H/W formated one
  32. * @parse_tbl_addr: Parse the given H/W address (hdr format)
  33. * @rt_generate_hw_rule: Generate RT rule in H/W format
  34. * @flt_generate_hw_rule: Generate FLT rule in H/W format
  35. * @flt_generate_eq: Generate flt equation attributes from rule attributes
  36. * @rt_parse_hw_rule: Parse rt rule read from H/W
  37. * @flt_parse_hw_rule: Parse flt rule read from H/W
  38. * @eq_bitfield: Array of the bit fields of the support equations.
  39. * 0xFF means the equation is not supported
  40. */
  41. struct ipahal_fltrt_obj {
  42. bool support_hash;
  43. u32 tbl_width;
  44. u32 sysaddr_alignment;
  45. u32 lcladdr_alignment;
  46. u32 blk_sz_alignment;
  47. u32 rule_start_alignment;
  48. u32 tbl_hdr_width;
  49. u32 tbl_addr_mask;
  50. int rule_max_prio;
  51. int rule_min_prio;
  52. u32 low_rule_id;
  53. u32 rule_id_bit_len;
  54. u32 rule_buf_size;
  55. u8* (*write_val_to_hdr)(u64 val, u8 *hdr);
  56. u64 (*create_flt_bitmap)(u64 ep_bitmap);
  57. u64 (*create_tbl_addr)(bool is_sys, u64 addr);
  58. void (*parse_tbl_addr)(u64 hwaddr, u64 *addr, bool *is_sys);
  59. int (*rt_generate_hw_rule)(struct ipahal_rt_rule_gen_params *params,
  60. u32 *hw_len, u8 *buf);
  61. int (*flt_generate_hw_rule)(struct ipahal_flt_rule_gen_params *params,
  62. u32 *hw_len, u8 *buf);
  63. int (*flt_generate_eq)(enum ipa_ip_type ipt,
  64. const struct ipa_rule_attrib *attrib,
  65. struct ipa_ipfltri_rule_eq *eq_atrb);
  66. int (*rt_parse_hw_rule)(u8 *addr, struct ipahal_rt_rule_entry *rule);
  67. int (*flt_parse_hw_rule)(u8 *addr, struct ipahal_flt_rule_entry *rule);
  68. u8 eq_bitfield[IPA_EQ_MAX];
  69. };
  70. static u64 ipa_fltrt_create_flt_bitmap(u64 ep_bitmap)
  71. {
  72. /* At IPA3, there global configuration is possible but not used */
  73. return (ep_bitmap << 1) & ~0x1;
  74. }
  75. static u64 ipa_fltrt_create_tbl_addr(bool is_sys, u64 addr)
  76. {
  77. if (is_sys) {
  78. if (addr & IPA3_0_HW_TBL_SYSADDR_ALIGNMENT) {
  79. IPAHAL_ERR(
  80. "sys addr is not aligned accordingly addr=0x%pad\n",
  81. &addr);
  82. ipa_assert();
  83. return 0;
  84. }
  85. } else {
  86. if (addr & IPA3_0_HW_TBL_LCLADDR_ALIGNMENT) {
  87. IPAHAL_ERR("addr/ofst isn't lcl addr aligned %llu\n",
  88. addr);
  89. ipa_assert();
  90. return 0;
  91. }
  92. /*
  93. * for local tables (at sram) offsets is used as tables
  94. * addresses. offset need to be in 8B units
  95. * (local address aligned) and left shifted to its place.
  96. * Local bit need to be enabled.
  97. */
  98. addr /= IPA3_0_HW_TBL_LCLADDR_ALIGNMENT + 1;
  99. addr *= IPA3_0_HW_TBL_ADDR_MASK + 1;
  100. addr += 1;
  101. }
  102. return addr;
  103. }
  104. static void ipa_fltrt_parse_tbl_addr(u64 hwaddr, u64 *addr, bool *is_sys)
  105. {
  106. IPAHAL_DBG_LOW("Parsing hwaddr 0x%llx\n", hwaddr);
  107. *is_sys = !(hwaddr & 0x1);
  108. hwaddr &= (~0ULL - 1);
  109. if (hwaddr & IPA3_0_HW_TBL_SYSADDR_ALIGNMENT) {
  110. IPAHAL_ERR(
  111. "sys addr is not aligned accordingly addr=0x%pad\n",
  112. &hwaddr);
  113. ipa_assert();
  114. return;
  115. }
  116. if (!*is_sys) {
  117. hwaddr /= IPA3_0_HW_TBL_ADDR_MASK + 1;
  118. hwaddr *= IPA3_0_HW_TBL_LCLADDR_ALIGNMENT + 1;
  119. }
  120. *addr = hwaddr;
  121. }
  122. /* Update these tables of the number of equations changes */
  123. static const int ipa3_0_ofst_meq32[] = { IPA_OFFSET_MEQ32_0,
  124. IPA_OFFSET_MEQ32_1};
  125. static const int ipa3_0_ofst_meq128[] = { IPA_OFFSET_MEQ128_0,
  126. IPA_OFFSET_MEQ128_1};
  127. static const int ipa3_0_ihl_ofst_rng16[] = { IPA_IHL_OFFSET_RANGE16_0,
  128. IPA_IHL_OFFSET_RANGE16_1};
  129. static const int ipa3_0_ihl_ofst_meq32[] = { IPA_IHL_OFFSET_MEQ32_0,
  130. IPA_IHL_OFFSET_MEQ32_1};
  131. static int ipa_fltrt_generate_hw_rule_bdy(enum ipa_ip_type ipt,
  132. const struct ipa_rule_attrib *attrib, u8 **buf, u16 *en_rule);
  133. static int ipa_fltrt_generate_hw_rule_bdy_from_eq(
  134. const struct ipa_ipfltri_rule_eq *attrib, u8 **buf);
  135. static int ipa_flt_generate_eq_ip4(enum ipa_ip_type ip,
  136. const struct ipa_rule_attrib *attrib,
  137. struct ipa_ipfltri_rule_eq *eq_atrb);
  138. static int ipa_flt_generate_eq_ip6(enum ipa_ip_type ip,
  139. const struct ipa_rule_attrib *attrib,
  140. struct ipa_ipfltri_rule_eq *eq_atrb);
  141. static int ipa_flt_generate_eq(enum ipa_ip_type ipt,
  142. const struct ipa_rule_attrib *attrib,
  143. struct ipa_ipfltri_rule_eq *eq_atrb);
  144. static int ipa_rt_parse_hw_rule(u8 *addr,
  145. struct ipahal_rt_rule_entry *rule);
  146. static int ipa_rt_parse_hw_rule_ipav4_5(u8 *addr,
  147. struct ipahal_rt_rule_entry *rule);
  148. static int ipa_flt_parse_hw_rule(u8 *addr,
  149. struct ipahal_flt_rule_entry *rule);
  150. static int ipa_flt_parse_hw_rule_ipav4(u8 *addr,
  151. struct ipahal_flt_rule_entry *rule);
  152. static int ipa_flt_parse_hw_rule_ipav4_5(u8 *addr,
  153. struct ipahal_flt_rule_entry *rule);
  154. #define IPA_IS_RAN_OUT_OF_EQ(__eq_array, __eq_index) \
  155. (ARRAY_SIZE(__eq_array) <= (__eq_index))
  156. #define IPA_GET_RULE_EQ_BIT_PTRN(__eq) \
  157. (BIT(ipahal_fltrt_objs[ipahal_ctx->hw_type].eq_bitfield[(__eq)]))
  158. #define IPA_IS_RULE_EQ_VALID(__eq) \
  159. (ipahal_fltrt_objs[ipahal_ctx->hw_type].eq_bitfield[(__eq)] != 0xFF)
  160. /*
  161. * ipa_fltrt_rule_generation_err_check() - check basic validity on the rule
  162. * attribs before starting building it
  163. * checks if not not using ipv4 attribs on ipv6 and vice-versa
  164. * @ip: IP address type
  165. * @attrib: IPA rule attribute
  166. *
  167. * Return: 0 on success, -EPERM on failure
  168. */
  169. static int ipa_fltrt_rule_generation_err_check(
  170. enum ipa_ip_type ipt, const struct ipa_rule_attrib *attrib)
  171. {
  172. if (ipt == IPA_IP_v4) {
  173. if (attrib->attrib_mask & IPA_FLT_NEXT_HDR ||
  174. attrib->attrib_mask & IPA_FLT_TC ||
  175. attrib->attrib_mask & IPA_FLT_FLOW_LABEL) {
  176. IPAHAL_ERR_RL("v6 attrib's specified for v4 rule\n");
  177. return -EPERM;
  178. }
  179. } else if (ipt == IPA_IP_v6) {
  180. if (attrib->attrib_mask & IPA_FLT_TOS ||
  181. attrib->attrib_mask & IPA_FLT_PROTOCOL) {
  182. IPAHAL_ERR_RL("v4 attrib's specified for v6 rule\n");
  183. return -EPERM;
  184. }
  185. } else {
  186. IPAHAL_ERR_RL("unsupported ip %d\n", ipt);
  187. return -EPERM;
  188. }
  189. return 0;
  190. }
  191. static int ipa_rt_gen_hw_rule(struct ipahal_rt_rule_gen_params *params,
  192. u32 *hw_len, u8 *buf)
  193. {
  194. struct ipa3_0_rt_rule_hw_hdr *rule_hdr;
  195. u8 *start;
  196. u16 en_rule = 0;
  197. start = buf;
  198. rule_hdr = (struct ipa3_0_rt_rule_hw_hdr *)buf;
  199. ipa_assert_on(params->dst_pipe_idx & ~0x1F);
  200. rule_hdr->u.hdr.pipe_dest_idx = params->dst_pipe_idx;
  201. switch (params->hdr_type) {
  202. case IPAHAL_RT_RULE_HDR_PROC_CTX:
  203. rule_hdr->u.hdr.system = !params->hdr_lcl;
  204. rule_hdr->u.hdr.proc_ctx = 1;
  205. ipa_assert_on(params->hdr_ofst & 31);
  206. rule_hdr->u.hdr.hdr_offset = (params->hdr_ofst) >> 5;
  207. break;
  208. case IPAHAL_RT_RULE_HDR_RAW:
  209. rule_hdr->u.hdr.system = !params->hdr_lcl;
  210. rule_hdr->u.hdr.proc_ctx = 0;
  211. ipa_assert_on(params->hdr_ofst & 3);
  212. rule_hdr->u.hdr.hdr_offset = (params->hdr_ofst) >> 2;
  213. break;
  214. case IPAHAL_RT_RULE_HDR_NONE:
  215. rule_hdr->u.hdr.system = !params->hdr_lcl;
  216. rule_hdr->u.hdr.proc_ctx = 0;
  217. rule_hdr->u.hdr.hdr_offset = 0;
  218. break;
  219. default:
  220. IPAHAL_ERR("Invalid HDR type %d\n", params->hdr_type);
  221. WARN_ON_RATELIMIT_IPA(1);
  222. return -EINVAL;
  223. }
  224. ipa_assert_on(params->priority & ~0x3FF);
  225. rule_hdr->u.hdr.priority = params->priority;
  226. rule_hdr->u.hdr.retain_hdr = params->rule->retain_hdr ? 0x1 : 0x0;
  227. ipa_assert_on(params->id & ~((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  228. ipa_assert_on(params->id == ((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  229. rule_hdr->u.hdr.rule_id = params->id;
  230. buf += sizeof(struct ipa3_0_rt_rule_hw_hdr);
  231. if (ipa_fltrt_generate_hw_rule_bdy(params->ipt, &params->rule->attrib,
  232. &buf, &en_rule)) {
  233. IPAHAL_ERR("fail to generate hw rule\n");
  234. return -EPERM;
  235. }
  236. rule_hdr->u.hdr.en_rule = en_rule;
  237. IPAHAL_DBG_LOW("en_rule 0x%x\n", en_rule);
  238. ipa_write_64(rule_hdr->u.word, (u8 *)rule_hdr);
  239. if (*hw_len == 0) {
  240. *hw_len = buf - start;
  241. } else if (*hw_len != (buf - start)) {
  242. IPAHAL_ERR("hw_len differs b/w passed=0x%x calc=%td\n",
  243. *hw_len, (buf - start));
  244. return -EPERM;
  245. }
  246. return 0;
  247. }
  248. static int ipa_rt_gen_hw_rule_ipav4_5(struct ipahal_rt_rule_gen_params *params,
  249. u32 *hw_len, u8 *buf)
  250. {
  251. struct ipa4_5_rt_rule_hw_hdr *rule_hdr;
  252. u8 *start;
  253. u16 en_rule = 0;
  254. start = buf;
  255. rule_hdr = (struct ipa4_5_rt_rule_hw_hdr *)buf;
  256. ipa_assert_on(params->dst_pipe_idx & ~0x1F);
  257. rule_hdr->u.hdr.pipe_dest_idx = params->dst_pipe_idx;
  258. switch (params->hdr_type) {
  259. case IPAHAL_RT_RULE_HDR_PROC_CTX:
  260. rule_hdr->u.hdr.system = !params->hdr_lcl;
  261. rule_hdr->u.hdr.proc_ctx = 1;
  262. ipa_assert_on(params->hdr_ofst & 31);
  263. rule_hdr->u.hdr.hdr_offset = (params->hdr_ofst) >> 5;
  264. break;
  265. case IPAHAL_RT_RULE_HDR_RAW:
  266. rule_hdr->u.hdr.system = !params->hdr_lcl;
  267. rule_hdr->u.hdr.proc_ctx = 0;
  268. ipa_assert_on(params->hdr_ofst & 3);
  269. rule_hdr->u.hdr.hdr_offset = (params->hdr_ofst) >> 2;
  270. break;
  271. case IPAHAL_RT_RULE_HDR_NONE:
  272. rule_hdr->u.hdr.system = !params->hdr_lcl;
  273. rule_hdr->u.hdr.proc_ctx = 0;
  274. rule_hdr->u.hdr.hdr_offset = 0;
  275. break;
  276. default:
  277. IPAHAL_ERR("Invalid HDR type %d\n", params->hdr_type);
  278. WARN_ON_RATELIMIT_IPA(1);
  279. return -EINVAL;
  280. }
  281. ipa_assert_on(params->priority & ~0x3FF);
  282. rule_hdr->u.hdr.priority = params->priority;
  283. rule_hdr->u.hdr.retain_hdr = params->rule->retain_hdr ? 0x1 : 0x0;
  284. ipa_assert_on(params->id & ~((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  285. ipa_assert_on(params->id == ((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  286. rule_hdr->u.hdr.rule_id = params->id;
  287. rule_hdr->u.hdr.stats_cnt_idx_lsb = params->cnt_idx & 0x3F;
  288. rule_hdr->u.hdr.stats_cnt_idx_msb = (params->cnt_idx & 0xC0) >> 6;
  289. buf += sizeof(struct ipa4_5_rt_rule_hw_hdr);
  290. if (ipa_fltrt_generate_hw_rule_bdy(params->ipt, &params->rule->attrib,
  291. &buf, &en_rule)) {
  292. IPAHAL_ERR("fail to generate hw rule\n");
  293. return -EPERM;
  294. }
  295. rule_hdr->u.hdr.en_rule = en_rule;
  296. IPAHAL_DBG_LOW("en_rule 0x%x\n", en_rule);
  297. ipa_write_64(rule_hdr->u.word, (u8 *)rule_hdr);
  298. if (*hw_len == 0) {
  299. *hw_len = buf - start;
  300. } else if (*hw_len != (buf - start)) {
  301. IPAHAL_ERR("hw_len differs b/w passed=0x%x calc=%td\n",
  302. *hw_len, (buf - start));
  303. return -EPERM;
  304. }
  305. return 0;
  306. }
  307. static int ipa_flt_gen_hw_rule(struct ipahal_flt_rule_gen_params *params,
  308. u32 *hw_len, u8 *buf)
  309. {
  310. struct ipa3_0_flt_rule_hw_hdr *rule_hdr;
  311. u8 *start;
  312. u16 en_rule = 0;
  313. start = buf;
  314. rule_hdr = (struct ipa3_0_flt_rule_hw_hdr *)buf;
  315. switch (params->rule->action) {
  316. case IPA_PASS_TO_ROUTING:
  317. rule_hdr->u.hdr.action = 0x0;
  318. break;
  319. case IPA_PASS_TO_SRC_NAT:
  320. rule_hdr->u.hdr.action = 0x1;
  321. break;
  322. case IPA_PASS_TO_DST_NAT:
  323. rule_hdr->u.hdr.action = 0x2;
  324. break;
  325. case IPA_PASS_TO_EXCEPTION:
  326. rule_hdr->u.hdr.action = 0x3;
  327. break;
  328. default:
  329. IPAHAL_ERR_RL("Invalid Rule Action %d\n", params->rule->action);
  330. WARN_ON_RATELIMIT_IPA(1);
  331. return -EINVAL;
  332. }
  333. ipa_assert_on(params->rt_tbl_idx & ~0x1F);
  334. rule_hdr->u.hdr.rt_tbl_idx = params->rt_tbl_idx;
  335. rule_hdr->u.hdr.retain_hdr = params->rule->retain_hdr ? 0x1 : 0x0;
  336. rule_hdr->u.hdr.rsvd1 = 0;
  337. rule_hdr->u.hdr.rsvd2 = 0;
  338. rule_hdr->u.hdr.rsvd3 = 0;
  339. ipa_assert_on(params->priority & ~0x3FF);
  340. rule_hdr->u.hdr.priority = params->priority;
  341. ipa_assert_on(params->id & ~((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  342. ipa_assert_on(params->id == ((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  343. rule_hdr->u.hdr.rule_id = params->id;
  344. buf += sizeof(struct ipa3_0_flt_rule_hw_hdr);
  345. if (params->rule->eq_attrib_type) {
  346. if (ipa_fltrt_generate_hw_rule_bdy_from_eq(
  347. &params->rule->eq_attrib, &buf)) {
  348. IPAHAL_ERR_RL("fail to generate hw rule from eq\n");
  349. return -EPERM;
  350. }
  351. en_rule = params->rule->eq_attrib.rule_eq_bitmap;
  352. } else {
  353. if (ipa_fltrt_generate_hw_rule_bdy(params->ipt,
  354. &params->rule->attrib, &buf, &en_rule)) {
  355. IPAHAL_ERR_RL("fail to generate hw rule\n");
  356. return -EPERM;
  357. }
  358. }
  359. rule_hdr->u.hdr.en_rule = en_rule;
  360. IPAHAL_DBG_LOW("en_rule=0x%x, action=%d, rt_idx=%d, retain_hdr=%d\n",
  361. en_rule,
  362. rule_hdr->u.hdr.action,
  363. rule_hdr->u.hdr.rt_tbl_idx,
  364. rule_hdr->u.hdr.retain_hdr);
  365. IPAHAL_DBG_LOW("priority=%d, rule_id=%d\n",
  366. rule_hdr->u.hdr.priority,
  367. rule_hdr->u.hdr.rule_id);
  368. ipa_write_64(rule_hdr->u.word, (u8 *)rule_hdr);
  369. if (*hw_len == 0) {
  370. *hw_len = buf - start;
  371. } else if (*hw_len != (buf - start)) {
  372. IPAHAL_ERR_RL("hw_len differs b/w passed=0x%x calc=%td\n",
  373. *hw_len, (buf - start));
  374. return -EPERM;
  375. }
  376. return 0;
  377. }
  378. static int ipa_flt_gen_hw_rule_ipav4(struct ipahal_flt_rule_gen_params *params,
  379. u32 *hw_len, u8 *buf)
  380. {
  381. struct ipa4_0_flt_rule_hw_hdr *rule_hdr;
  382. u8 *start;
  383. u16 en_rule = 0;
  384. start = buf;
  385. rule_hdr = (struct ipa4_0_flt_rule_hw_hdr *)buf;
  386. switch (params->rule->action) {
  387. case IPA_PASS_TO_ROUTING:
  388. rule_hdr->u.hdr.action = 0x0;
  389. break;
  390. case IPA_PASS_TO_SRC_NAT:
  391. rule_hdr->u.hdr.action = 0x1;
  392. break;
  393. case IPA_PASS_TO_DST_NAT:
  394. rule_hdr->u.hdr.action = 0x2;
  395. break;
  396. case IPA_PASS_TO_EXCEPTION:
  397. rule_hdr->u.hdr.action = 0x3;
  398. break;
  399. default:
  400. IPAHAL_ERR("Invalid Rule Action %d\n", params->rule->action);
  401. WARN_ON_RATELIMIT_IPA(1);
  402. return -EINVAL;
  403. }
  404. ipa_assert_on(params->rt_tbl_idx & ~0x1F);
  405. rule_hdr->u.hdr.rt_tbl_idx = params->rt_tbl_idx;
  406. rule_hdr->u.hdr.retain_hdr = params->rule->retain_hdr ? 0x1 : 0x0;
  407. ipa_assert_on(params->rule->pdn_idx & ~0xF);
  408. rule_hdr->u.hdr.pdn_idx = params->rule->pdn_idx;
  409. rule_hdr->u.hdr.set_metadata = params->rule->set_metadata;
  410. rule_hdr->u.hdr.rsvd2 = 0;
  411. rule_hdr->u.hdr.rsvd3 = 0;
  412. ipa_assert_on(params->priority & ~0x3FF);
  413. rule_hdr->u.hdr.priority = params->priority;
  414. ipa_assert_on(params->id & ~((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  415. ipa_assert_on(params->id == ((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  416. rule_hdr->u.hdr.rule_id = params->id;
  417. buf += sizeof(struct ipa4_0_flt_rule_hw_hdr);
  418. if (params->rule->eq_attrib_type) {
  419. if (ipa_fltrt_generate_hw_rule_bdy_from_eq(
  420. &params->rule->eq_attrib, &buf)) {
  421. IPAHAL_ERR("fail to generate hw rule from eq\n");
  422. return -EPERM;
  423. }
  424. en_rule = params->rule->eq_attrib.rule_eq_bitmap;
  425. } else {
  426. if (ipa_fltrt_generate_hw_rule_bdy(params->ipt,
  427. &params->rule->attrib, &buf, &en_rule)) {
  428. IPAHAL_ERR("fail to generate hw rule\n");
  429. return -EPERM;
  430. }
  431. }
  432. rule_hdr->u.hdr.en_rule = en_rule;
  433. IPAHAL_DBG_LOW("en_rule=0x%x, action=%d, rt_idx=%d, retain_hdr=%d\n",
  434. en_rule,
  435. rule_hdr->u.hdr.action,
  436. rule_hdr->u.hdr.rt_tbl_idx,
  437. rule_hdr->u.hdr.retain_hdr);
  438. IPAHAL_DBG_LOW("priority=%d, rule_id=%d, pdn=%d, set_metadata=%d\n",
  439. rule_hdr->u.hdr.priority,
  440. rule_hdr->u.hdr.rule_id,
  441. rule_hdr->u.hdr.pdn_idx,
  442. rule_hdr->u.hdr.set_metadata);
  443. ipa_write_64(rule_hdr->u.word, (u8 *)rule_hdr);
  444. if (*hw_len == 0) {
  445. *hw_len = buf - start;
  446. } else if (*hw_len != (buf - start)) {
  447. IPAHAL_ERR("hw_len differs b/w passed=0x%x calc=%td\n",
  448. *hw_len, (buf - start));
  449. return -EPERM;
  450. }
  451. return 0;
  452. }
  453. static int ipa_flt_gen_hw_rule_ipav4_5(
  454. struct ipahal_flt_rule_gen_params *params,
  455. u32 *hw_len, u8 *buf)
  456. {
  457. struct ipa4_5_flt_rule_hw_hdr *rule_hdr;
  458. u8 *start;
  459. u16 en_rule = 0;
  460. start = buf;
  461. rule_hdr = (struct ipa4_5_flt_rule_hw_hdr *)buf;
  462. switch (params->rule->action) {
  463. case IPA_PASS_TO_ROUTING:
  464. rule_hdr->u.hdr.action = 0x0;
  465. break;
  466. case IPA_PASS_TO_SRC_NAT:
  467. rule_hdr->u.hdr.action = 0x1;
  468. break;
  469. case IPA_PASS_TO_DST_NAT:
  470. rule_hdr->u.hdr.action = 0x2;
  471. break;
  472. case IPA_PASS_TO_EXCEPTION:
  473. rule_hdr->u.hdr.action = 0x3;
  474. break;
  475. default:
  476. IPAHAL_ERR("Invalid Rule Action %d\n", params->rule->action);
  477. WARN_ON_RATELIMIT_IPA(1);
  478. return -EINVAL;
  479. }
  480. ipa_assert_on(params->rt_tbl_idx & ~0x1F);
  481. rule_hdr->u.hdr.rt_tbl_idx = params->rt_tbl_idx;
  482. rule_hdr->u.hdr.retain_hdr = params->rule->retain_hdr ? 0x1 : 0x0;
  483. ipa_assert_on(params->rule->pdn_idx & ~0xF);
  484. rule_hdr->u.hdr.pdn_idx = params->rule->pdn_idx;
  485. rule_hdr->u.hdr.set_metadata = params->rule->set_metadata;
  486. rule_hdr->u.hdr.rsvd2 = 0;
  487. ipa_assert_on(params->priority & ~0x3FF);
  488. rule_hdr->u.hdr.priority = params->priority;
  489. ipa_assert_on(params->id & ~((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  490. ipa_assert_on(params->id == ((1 << IPA3_0_RULE_ID_BIT_LEN) - 1));
  491. rule_hdr->u.hdr.rule_id = params->id;
  492. rule_hdr->u.hdr.stats_cnt_idx_lsb = params->cnt_idx & 0x3F;
  493. rule_hdr->u.hdr.stats_cnt_idx_msb = (params->cnt_idx & 0xC0) >> 6;
  494. buf += sizeof(struct ipa4_5_flt_rule_hw_hdr);
  495. if (params->rule->eq_attrib_type) {
  496. if (ipa_fltrt_generate_hw_rule_bdy_from_eq(
  497. &params->rule->eq_attrib, &buf)) {
  498. IPAHAL_ERR("fail to generate hw rule from eq\n");
  499. return -EPERM;
  500. }
  501. en_rule = params->rule->eq_attrib.rule_eq_bitmap;
  502. } else {
  503. if (ipa_fltrt_generate_hw_rule_bdy(params->ipt,
  504. &params->rule->attrib, &buf, &en_rule)) {
  505. IPAHAL_ERR("fail to generate hw rule\n");
  506. return -EPERM;
  507. }
  508. }
  509. rule_hdr->u.hdr.en_rule = en_rule;
  510. IPAHAL_DBG_LOW("en_rule=0x%x, action=%d, rt_idx=%d, retain_hdr=%d\n",
  511. en_rule,
  512. rule_hdr->u.hdr.action,
  513. rule_hdr->u.hdr.rt_tbl_idx,
  514. rule_hdr->u.hdr.retain_hdr);
  515. IPAHAL_DBG_LOW("priority=%d, rule_id=%d, pdn=%d, set_metadata=%d\n",
  516. rule_hdr->u.hdr.priority,
  517. rule_hdr->u.hdr.rule_id,
  518. rule_hdr->u.hdr.pdn_idx,
  519. rule_hdr->u.hdr.set_metadata);
  520. ipa_write_64(rule_hdr->u.word, (u8 *)rule_hdr);
  521. if (*hw_len == 0) {
  522. *hw_len = buf - start;
  523. } else if (*hw_len != (buf - start)) {
  524. IPAHAL_ERR("hw_len differs b/w passed=0x%x calc=%td\n",
  525. *hw_len, (buf - start));
  526. return -EPERM;
  527. }
  528. return 0;
  529. }
  530. /*
  531. * This array contains the FLT/RT info for IPAv3 and later.
  532. * All the information on IPAv3 are statically defined below.
  533. * If information is missing regarding on some IPA version,
  534. * the init function will fill it with the information from the previous
  535. * IPA version.
  536. * Information is considered missing if all of the fields are 0.
  537. */
  538. static struct ipahal_fltrt_obj ipahal_fltrt_objs[IPA_HW_MAX] = {
  539. /* IPAv3 */
  540. [IPA_HW_v3_0] = {
  541. true,
  542. IPA3_0_HW_TBL_WIDTH,
  543. IPA3_0_HW_TBL_SYSADDR_ALIGNMENT,
  544. IPA3_0_HW_TBL_LCLADDR_ALIGNMENT,
  545. IPA3_0_HW_TBL_BLK_SIZE_ALIGNMENT,
  546. IPA3_0_HW_RULE_START_ALIGNMENT,
  547. IPA3_0_HW_TBL_HDR_WIDTH,
  548. IPA3_0_HW_TBL_ADDR_MASK,
  549. IPA3_0_RULE_MAX_PRIORITY,
  550. IPA3_0_RULE_MIN_PRIORITY,
  551. IPA3_0_LOW_RULE_ID,
  552. IPA3_0_RULE_ID_BIT_LEN,
  553. IPA3_0_HW_RULE_BUF_SIZE,
  554. ipa_write_64,
  555. ipa_fltrt_create_flt_bitmap,
  556. ipa_fltrt_create_tbl_addr,
  557. ipa_fltrt_parse_tbl_addr,
  558. ipa_rt_gen_hw_rule,
  559. ipa_flt_gen_hw_rule,
  560. ipa_flt_generate_eq,
  561. ipa_rt_parse_hw_rule,
  562. ipa_flt_parse_hw_rule,
  563. {
  564. [IPA_TOS_EQ] = 0,
  565. [IPA_PROTOCOL_EQ] = 1,
  566. [IPA_TC_EQ] = 2,
  567. [IPA_OFFSET_MEQ128_0] = 3,
  568. [IPA_OFFSET_MEQ128_1] = 4,
  569. [IPA_OFFSET_MEQ32_0] = 5,
  570. [IPA_OFFSET_MEQ32_1] = 6,
  571. [IPA_IHL_OFFSET_MEQ32_0] = 7,
  572. [IPA_IHL_OFFSET_MEQ32_1] = 8,
  573. [IPA_METADATA_COMPARE] = 9,
  574. [IPA_IHL_OFFSET_RANGE16_0] = 10,
  575. [IPA_IHL_OFFSET_RANGE16_1] = 11,
  576. [IPA_IHL_OFFSET_EQ_32] = 12,
  577. [IPA_IHL_OFFSET_EQ_16] = 13,
  578. [IPA_FL_EQ] = 14,
  579. [IPA_IS_FRAG] = 15,
  580. [IPA_IS_PURE_ACK] = 0xFF,
  581. },
  582. },
  583. /* IPAv4 */
  584. [IPA_HW_v4_0] = {
  585. true,
  586. IPA3_0_HW_TBL_WIDTH,
  587. IPA3_0_HW_TBL_SYSADDR_ALIGNMENT,
  588. IPA3_0_HW_TBL_LCLADDR_ALIGNMENT,
  589. IPA3_0_HW_TBL_BLK_SIZE_ALIGNMENT,
  590. IPA3_0_HW_RULE_START_ALIGNMENT,
  591. IPA3_0_HW_TBL_HDR_WIDTH,
  592. IPA3_0_HW_TBL_ADDR_MASK,
  593. IPA3_0_RULE_MAX_PRIORITY,
  594. IPA3_0_RULE_MIN_PRIORITY,
  595. IPA3_0_LOW_RULE_ID,
  596. IPA3_0_RULE_ID_BIT_LEN,
  597. IPA3_0_HW_RULE_BUF_SIZE,
  598. ipa_write_64,
  599. ipa_fltrt_create_flt_bitmap,
  600. ipa_fltrt_create_tbl_addr,
  601. ipa_fltrt_parse_tbl_addr,
  602. ipa_rt_gen_hw_rule,
  603. ipa_flt_gen_hw_rule_ipav4,
  604. ipa_flt_generate_eq,
  605. ipa_rt_parse_hw_rule,
  606. ipa_flt_parse_hw_rule_ipav4,
  607. {
  608. [IPA_TOS_EQ] = 0,
  609. [IPA_PROTOCOL_EQ] = 1,
  610. [IPA_TC_EQ] = 2,
  611. [IPA_OFFSET_MEQ128_0] = 3,
  612. [IPA_OFFSET_MEQ128_1] = 4,
  613. [IPA_OFFSET_MEQ32_0] = 5,
  614. [IPA_OFFSET_MEQ32_1] = 6,
  615. [IPA_IHL_OFFSET_MEQ32_0] = 7,
  616. [IPA_IHL_OFFSET_MEQ32_1] = 8,
  617. [IPA_METADATA_COMPARE] = 9,
  618. [IPA_IHL_OFFSET_RANGE16_0] = 10,
  619. [IPA_IHL_OFFSET_RANGE16_1] = 11,
  620. [IPA_IHL_OFFSET_EQ_32] = 12,
  621. [IPA_IHL_OFFSET_EQ_16] = 13,
  622. [IPA_FL_EQ] = 14,
  623. [IPA_IS_FRAG] = 15,
  624. [IPA_IS_PURE_ACK] = 0xFF,
  625. },
  626. },
  627. /* IPAv4.2 */
  628. [IPA_HW_v4_2] = {
  629. false,
  630. IPA3_0_HW_TBL_WIDTH,
  631. IPA3_0_HW_TBL_SYSADDR_ALIGNMENT,
  632. IPA3_0_HW_TBL_LCLADDR_ALIGNMENT,
  633. IPA3_0_HW_TBL_BLK_SIZE_ALIGNMENT,
  634. IPA3_0_HW_RULE_START_ALIGNMENT,
  635. IPA3_0_HW_TBL_HDR_WIDTH,
  636. IPA3_0_HW_TBL_ADDR_MASK,
  637. IPA3_0_RULE_MAX_PRIORITY,
  638. IPA3_0_RULE_MIN_PRIORITY,
  639. IPA3_0_LOW_RULE_ID,
  640. IPA3_0_RULE_ID_BIT_LEN,
  641. IPA3_0_HW_RULE_BUF_SIZE,
  642. ipa_write_64,
  643. ipa_fltrt_create_flt_bitmap,
  644. ipa_fltrt_create_tbl_addr,
  645. ipa_fltrt_parse_tbl_addr,
  646. ipa_rt_gen_hw_rule,
  647. ipa_flt_gen_hw_rule_ipav4,
  648. ipa_flt_generate_eq,
  649. ipa_rt_parse_hw_rule,
  650. ipa_flt_parse_hw_rule_ipav4,
  651. {
  652. [IPA_TOS_EQ] = 0,
  653. [IPA_PROTOCOL_EQ] = 1,
  654. [IPA_TC_EQ] = 2,
  655. [IPA_OFFSET_MEQ128_0] = 3,
  656. [IPA_OFFSET_MEQ128_1] = 4,
  657. [IPA_OFFSET_MEQ32_0] = 5,
  658. [IPA_OFFSET_MEQ32_1] = 6,
  659. [IPA_IHL_OFFSET_MEQ32_0] = 7,
  660. [IPA_IHL_OFFSET_MEQ32_1] = 8,
  661. [IPA_METADATA_COMPARE] = 9,
  662. [IPA_IHL_OFFSET_RANGE16_0] = 10,
  663. [IPA_IHL_OFFSET_RANGE16_1] = 11,
  664. [IPA_IHL_OFFSET_EQ_32] = 12,
  665. [IPA_IHL_OFFSET_EQ_16] = 13,
  666. [IPA_FL_EQ] = 14,
  667. [IPA_IS_FRAG] = 15,
  668. [IPA_IS_PURE_ACK] = 0xFF,
  669. },
  670. },
  671. /* IPAv4.5 */
  672. [IPA_HW_v4_5] = {
  673. true,
  674. IPA3_0_HW_TBL_WIDTH,
  675. IPA3_0_HW_TBL_SYSADDR_ALIGNMENT,
  676. IPA3_0_HW_TBL_LCLADDR_ALIGNMENT,
  677. IPA3_0_HW_TBL_BLK_SIZE_ALIGNMENT,
  678. IPA3_0_HW_RULE_START_ALIGNMENT,
  679. IPA3_0_HW_TBL_HDR_WIDTH,
  680. IPA3_0_HW_TBL_ADDR_MASK,
  681. IPA3_0_RULE_MAX_PRIORITY,
  682. IPA3_0_RULE_MIN_PRIORITY,
  683. IPA3_0_LOW_RULE_ID,
  684. IPA3_0_RULE_ID_BIT_LEN,
  685. IPA3_0_HW_RULE_BUF_SIZE,
  686. ipa_write_64,
  687. ipa_fltrt_create_flt_bitmap,
  688. ipa_fltrt_create_tbl_addr,
  689. ipa_fltrt_parse_tbl_addr,
  690. ipa_rt_gen_hw_rule_ipav4_5,
  691. ipa_flt_gen_hw_rule_ipav4_5,
  692. ipa_flt_generate_eq,
  693. ipa_rt_parse_hw_rule_ipav4_5,
  694. ipa_flt_parse_hw_rule_ipav4_5,
  695. {
  696. [IPA_TOS_EQ] = 0xFF,
  697. [IPA_PROTOCOL_EQ] = 1,
  698. [IPA_TC_EQ] = 2,
  699. [IPA_OFFSET_MEQ128_0] = 3,
  700. [IPA_OFFSET_MEQ128_1] = 4,
  701. [IPA_OFFSET_MEQ32_0] = 5,
  702. [IPA_OFFSET_MEQ32_1] = 6,
  703. [IPA_IHL_OFFSET_MEQ32_0] = 7,
  704. [IPA_IHL_OFFSET_MEQ32_1] = 8,
  705. [IPA_METADATA_COMPARE] = 9,
  706. [IPA_IHL_OFFSET_RANGE16_0] = 10,
  707. [IPA_IHL_OFFSET_RANGE16_1] = 11,
  708. [IPA_IHL_OFFSET_EQ_32] = 12,
  709. [IPA_IHL_OFFSET_EQ_16] = 13,
  710. [IPA_FL_EQ] = 14,
  711. [IPA_IS_FRAG] = 15,
  712. [IPA_IS_PURE_ACK] = 0,
  713. },
  714. },
  715. };
  716. static int ipa_flt_generate_eq(enum ipa_ip_type ipt,
  717. const struct ipa_rule_attrib *attrib,
  718. struct ipa_ipfltri_rule_eq *eq_atrb)
  719. {
  720. if (ipa_fltrt_rule_generation_err_check(ipt, attrib))
  721. return -EPERM;
  722. if (ipt == IPA_IP_v4) {
  723. if (ipa_flt_generate_eq_ip4(ipt, attrib, eq_atrb)) {
  724. IPAHAL_ERR("failed to build ipv4 flt eq rule\n");
  725. return -EPERM;
  726. }
  727. } else if (ipt == IPA_IP_v6) {
  728. if (ipa_flt_generate_eq_ip6(ipt, attrib, eq_atrb)) {
  729. IPAHAL_ERR("failed to build ipv6 flt eq rule\n");
  730. return -EPERM;
  731. }
  732. } else {
  733. IPAHAL_ERR("unsupported ip %d\n", ipt);
  734. return -EPERM;
  735. }
  736. /*
  737. * default "rule" means no attributes set -> map to
  738. * OFFSET_MEQ32_0 with mask of 0 and val of 0 and offset 0
  739. */
  740. if (attrib->attrib_mask == 0) {
  741. eq_atrb->rule_eq_bitmap = 0;
  742. eq_atrb->rule_eq_bitmap |= IPA_GET_RULE_EQ_BIT_PTRN(
  743. IPA_OFFSET_MEQ32_0);
  744. eq_atrb->offset_meq_32[0].offset = 0;
  745. eq_atrb->offset_meq_32[0].mask = 0;
  746. eq_atrb->offset_meq_32[0].value = 0;
  747. }
  748. return 0;
  749. }
  750. static void ipa_fltrt_generate_mac_addr_hw_rule(u8 **extra, u8 **rest,
  751. u8 hdr_mac_addr_offset,
  752. const uint8_t mac_addr_mask[ETH_ALEN],
  753. const uint8_t mac_addr[ETH_ALEN])
  754. {
  755. int i;
  756. *extra = ipa_write_8(hdr_mac_addr_offset, *extra);
  757. /* LSB MASK and ADDR */
  758. *rest = ipa_write_64(0, *rest);
  759. *rest = ipa_write_64(0, *rest);
  760. /* MSB MASK and ADDR */
  761. *rest = ipa_write_16(0, *rest);
  762. for (i = 5; i >= 0; i--)
  763. *rest = ipa_write_8(mac_addr_mask[i], *rest);
  764. *rest = ipa_write_16(0, *rest);
  765. for (i = 5; i >= 0; i--)
  766. *rest = ipa_write_8(mac_addr[i], *rest);
  767. }
  768. static int ipa_fltrt_generate_hw_rule_bdy_ip4(u16 *en_rule,
  769. const struct ipa_rule_attrib *attrib,
  770. u8 **extra_wrds, u8 **rest_wrds)
  771. {
  772. u8 *extra = *extra_wrds;
  773. u8 *rest = *rest_wrds;
  774. u8 ofst_meq32 = 0;
  775. u8 ihl_ofst_rng16 = 0;
  776. u8 ihl_ofst_meq32 = 0;
  777. u8 ofst_meq128 = 0;
  778. int rc = 0;
  779. bool tos_done = false;
  780. if (attrib->attrib_mask & IPA_FLT_IS_PURE_ACK) {
  781. if (!IPA_IS_RULE_EQ_VALID(IPA_IS_PURE_ACK)) {
  782. IPAHAL_ERR("is_pure_ack eq not supported\n");
  783. goto err;
  784. }
  785. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_PURE_ACK);
  786. extra = ipa_write_8(0, extra);
  787. }
  788. if (attrib->attrib_mask & IPA_FLT_TOS && !tos_done) {
  789. if (!IPA_IS_RULE_EQ_VALID(IPA_TOS_EQ)) {
  790. IPAHAL_DBG("tos eq not supported\n");
  791. } else {
  792. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_TOS_EQ);
  793. extra = ipa_write_8(attrib->u.v4.tos, extra);
  794. tos_done = true;
  795. }
  796. }
  797. if (attrib->attrib_mask & IPA_FLT_PROTOCOL) {
  798. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_PROTOCOL_EQ);
  799. extra = ipa_write_8(attrib->u.v4.protocol, extra);
  800. }
  801. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_ETHER_II) {
  802. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  803. IPAHAL_ERR("ran out of meq128 eq\n");
  804. goto err;
  805. }
  806. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  807. ipa3_0_ofst_meq128[ofst_meq128]);
  808. /* -14 => offset of dst mac addr in Ethernet II hdr */
  809. ipa_fltrt_generate_mac_addr_hw_rule(
  810. &extra,
  811. &rest,
  812. -14,
  813. attrib->dst_mac_addr_mask,
  814. attrib->dst_mac_addr);
  815. ofst_meq128++;
  816. }
  817. if (attrib->attrib_mask & IPA_FLT_MAC_SRC_ADDR_ETHER_II) {
  818. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  819. IPAHAL_ERR("ran out of meq128 eq\n");
  820. goto err;
  821. }
  822. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  823. ipa3_0_ofst_meq128[ofst_meq128]);
  824. /* -8 => offset of src mac addr in Ethernet II hdr */
  825. ipa_fltrt_generate_mac_addr_hw_rule(
  826. &extra,
  827. &rest,
  828. -8,
  829. attrib->src_mac_addr_mask,
  830. attrib->src_mac_addr);
  831. ofst_meq128++;
  832. }
  833. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_802_3) {
  834. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  835. IPAHAL_ERR("ran out of meq128 eq\n");
  836. goto err;
  837. }
  838. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  839. ipa3_0_ofst_meq128[ofst_meq128]);
  840. /* -22 => offset of dst mac addr in 802.3 hdr */
  841. ipa_fltrt_generate_mac_addr_hw_rule(
  842. &extra,
  843. &rest,
  844. -22,
  845. attrib->dst_mac_addr_mask,
  846. attrib->dst_mac_addr);
  847. ofst_meq128++;
  848. }
  849. if (attrib->attrib_mask & IPA_FLT_MAC_SRC_ADDR_802_3) {
  850. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  851. IPAHAL_ERR("ran out of meq128 eq\n");
  852. goto err;
  853. }
  854. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  855. ipa3_0_ofst_meq128[ofst_meq128]);
  856. /* -16 => offset of src mac addr in 802.3 hdr */
  857. ipa_fltrt_generate_mac_addr_hw_rule(
  858. &extra,
  859. &rest,
  860. -16,
  861. attrib->src_mac_addr_mask,
  862. attrib->src_mac_addr);
  863. ofst_meq128++;
  864. }
  865. if (attrib->attrib_mask & IPA_FLT_TOS_MASKED) {
  866. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  867. IPAHAL_ERR("ran out of meq32 eq\n");
  868. goto err;
  869. }
  870. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  871. ipa3_0_ofst_meq32[ofst_meq32]);
  872. /* 0 => Take the first word. offset of TOS in v4 header is 1 */
  873. extra = ipa_write_8(0, extra);
  874. rest = ipa_write_32((attrib->tos_mask << 16), rest);
  875. rest = ipa_write_32((attrib->tos_value << 16), rest);
  876. ofst_meq32++;
  877. }
  878. if (attrib->attrib_mask & IPA_FLT_SRC_ADDR) {
  879. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  880. IPAHAL_ERR("ran out of meq32 eq\n");
  881. goto err;
  882. }
  883. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  884. ipa3_0_ofst_meq32[ofst_meq32]);
  885. /* 12 => offset of src ip in v4 header */
  886. extra = ipa_write_8(12, extra);
  887. rest = ipa_write_32(attrib->u.v4.src_addr_mask, rest);
  888. rest = ipa_write_32(attrib->u.v4.src_addr, rest);
  889. ofst_meq32++;
  890. }
  891. if (attrib->attrib_mask & IPA_FLT_DST_ADDR) {
  892. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  893. IPAHAL_ERR("ran out of meq32 eq\n");
  894. goto err;
  895. }
  896. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  897. ipa3_0_ofst_meq32[ofst_meq32]);
  898. /* 16 => offset of dst ip in v4 header */
  899. extra = ipa_write_8(16, extra);
  900. rest = ipa_write_32(attrib->u.v4.dst_addr_mask, rest);
  901. rest = ipa_write_32(attrib->u.v4.dst_addr, rest);
  902. ofst_meq32++;
  903. }
  904. if (attrib->attrib_mask & IPA_FLT_MAC_ETHER_TYPE) {
  905. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  906. IPAHAL_ERR("ran out of meq32 eq\n");
  907. goto err;
  908. }
  909. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  910. ipa3_0_ofst_meq32[ofst_meq32]);
  911. /* -2 => offset of ether type in L2 hdr */
  912. extra = ipa_write_8((u8)-2, extra);
  913. rest = ipa_write_16(0, rest);
  914. rest = ipa_write_16(htons(attrib->ether_type), rest);
  915. rest = ipa_write_16(0, rest);
  916. rest = ipa_write_16(htons(attrib->ether_type), rest);
  917. ofst_meq32++;
  918. }
  919. if (attrib->attrib_mask & IPA_FLT_TOS && !tos_done) {
  920. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  921. IPAHAL_DBG("ran out of meq32 eq\n");
  922. } else {
  923. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  924. ipa3_0_ofst_meq32[ofst_meq32]);
  925. /*
  926. * 0 => Take the first word.
  927. * offset of TOS in v4 header is 1
  928. */
  929. extra = ipa_write_8(0, extra);
  930. rest = ipa_write_32(0xFF << 16, rest);
  931. rest = ipa_write_32((attrib->u.v4.tos << 16), rest);
  932. ofst_meq32++;
  933. tos_done = true;
  934. }
  935. }
  936. if (attrib->attrib_mask & IPA_FLT_TYPE) {
  937. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  938. ihl_ofst_meq32)) {
  939. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  940. goto err;
  941. }
  942. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  943. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  944. /* 0 => offset of type after v4 header */
  945. extra = ipa_write_8(0, extra);
  946. rest = ipa_write_32(0xFF, rest);
  947. rest = ipa_write_32(attrib->type, rest);
  948. ihl_ofst_meq32++;
  949. }
  950. if (attrib->attrib_mask & IPA_FLT_CODE) {
  951. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  952. ihl_ofst_meq32)) {
  953. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  954. goto err;
  955. }
  956. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  957. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  958. /* 1 => offset of code after v4 header */
  959. extra = ipa_write_8(1, extra);
  960. rest = ipa_write_32(0xFF, rest);
  961. rest = ipa_write_32(attrib->code, rest);
  962. ihl_ofst_meq32++;
  963. }
  964. if (attrib->attrib_mask & IPA_FLT_SPI) {
  965. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  966. ihl_ofst_meq32)) {
  967. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  968. goto err;
  969. }
  970. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  971. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  972. /* 0 => offset of SPI after v4 header */
  973. extra = ipa_write_8(0, extra);
  974. rest = ipa_write_32(0xFFFFFFFF, rest);
  975. rest = ipa_write_32(attrib->spi, rest);
  976. ihl_ofst_meq32++;
  977. }
  978. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_L2TP) {
  979. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  980. ihl_ofst_meq32) || IPA_IS_RAN_OUT_OF_EQ(
  981. ipa3_0_ihl_ofst_meq32, ihl_ofst_meq32 + 1)) {
  982. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  983. goto err;
  984. }
  985. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  986. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  987. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  988. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32 + 1]);
  989. /* populate first ihl meq eq */
  990. extra = ipa_write_8(8, extra);
  991. rest = ipa_write_8(attrib->dst_mac_addr_mask[3], rest);
  992. rest = ipa_write_8(attrib->dst_mac_addr_mask[2], rest);
  993. rest = ipa_write_8(attrib->dst_mac_addr_mask[1], rest);
  994. rest = ipa_write_8(attrib->dst_mac_addr_mask[0], rest);
  995. rest = ipa_write_8(attrib->dst_mac_addr[3], rest);
  996. rest = ipa_write_8(attrib->dst_mac_addr[2], rest);
  997. rest = ipa_write_8(attrib->dst_mac_addr[1], rest);
  998. rest = ipa_write_8(attrib->dst_mac_addr[0], rest);
  999. /* populate second ihl meq eq */
  1000. extra = ipa_write_8(12, extra);
  1001. rest = ipa_write_16(0, rest);
  1002. rest = ipa_write_8(attrib->dst_mac_addr_mask[5], rest);
  1003. rest = ipa_write_8(attrib->dst_mac_addr_mask[4], rest);
  1004. rest = ipa_write_16(0, rest);
  1005. rest = ipa_write_8(attrib->dst_mac_addr[5], rest);
  1006. rest = ipa_write_8(attrib->dst_mac_addr[4], rest);
  1007. ihl_ofst_meq32 += 2;
  1008. }
  1009. if (attrib->attrib_mask & IPA_FLT_TCP_SYN) {
  1010. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1011. ihl_ofst_meq32)) {
  1012. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1013. goto err;
  1014. }
  1015. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1016. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1017. /* 12 => offset of SYN after v4 header */
  1018. extra = ipa_write_8(12, extra);
  1019. rest = ipa_write_32(0x20000, rest);
  1020. rest = ipa_write_32(0x20000, rest);
  1021. ihl_ofst_meq32++;
  1022. }
  1023. if (attrib->attrib_mask & IPA_FLT_TOS && !tos_done) {
  1024. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1025. ihl_ofst_meq32)) {
  1026. IPAHAL_DBG("ran out of ihl_meq32 eq\n");
  1027. } else {
  1028. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1029. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1030. /*
  1031. * 0 => Take the first word. offset of TOS in
  1032. * v4 header is 1. MSB bit asserted at IHL means
  1033. * to ignore packet IHL and do offset inside IPA header
  1034. */
  1035. extra = ipa_write_8(0x80, extra);
  1036. rest = ipa_write_32(0xFF << 16, rest);
  1037. rest = ipa_write_32((attrib->u.v4.tos << 16), rest);
  1038. ihl_ofst_meq32++;
  1039. tos_done = true;
  1040. }
  1041. }
  1042. if (attrib->attrib_mask & IPA_FLT_META_DATA) {
  1043. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_METADATA_COMPARE);
  1044. rest = ipa_write_32(attrib->meta_data_mask, rest);
  1045. rest = ipa_write_32(attrib->meta_data, rest);
  1046. }
  1047. if (attrib->attrib_mask & IPA_FLT_SRC_PORT_RANGE) {
  1048. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1049. ihl_ofst_rng16)) {
  1050. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1051. goto err;
  1052. }
  1053. if (attrib->src_port_hi < attrib->src_port_lo) {
  1054. IPAHAL_ERR("bad src port range param\n");
  1055. goto err;
  1056. }
  1057. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1058. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1059. /* 0 => offset of src port after v4 header */
  1060. extra = ipa_write_8(0, extra);
  1061. rest = ipa_write_16(attrib->src_port_hi, rest);
  1062. rest = ipa_write_16(attrib->src_port_lo, rest);
  1063. ihl_ofst_rng16++;
  1064. }
  1065. if (attrib->attrib_mask & IPA_FLT_DST_PORT_RANGE) {
  1066. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1067. ihl_ofst_rng16)) {
  1068. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1069. goto err;
  1070. }
  1071. if (attrib->dst_port_hi < attrib->dst_port_lo) {
  1072. IPAHAL_ERR("bad dst port range param\n");
  1073. goto err;
  1074. }
  1075. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1076. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1077. /* 2 => offset of dst port after v4 header */
  1078. extra = ipa_write_8(2, extra);
  1079. rest = ipa_write_16(attrib->dst_port_hi, rest);
  1080. rest = ipa_write_16(attrib->dst_port_lo, rest);
  1081. ihl_ofst_rng16++;
  1082. }
  1083. if (attrib->attrib_mask & IPA_FLT_SRC_PORT) {
  1084. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1085. ihl_ofst_rng16)) {
  1086. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1087. goto err;
  1088. }
  1089. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1090. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1091. /* 0 => offset of src port after v4 header */
  1092. extra = ipa_write_8(0, extra);
  1093. rest = ipa_write_16(attrib->src_port, rest);
  1094. rest = ipa_write_16(attrib->src_port, rest);
  1095. ihl_ofst_rng16++;
  1096. }
  1097. if (attrib->attrib_mask & IPA_FLT_DST_PORT) {
  1098. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1099. ihl_ofst_rng16)) {
  1100. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1101. goto err;
  1102. }
  1103. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1104. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1105. /* 2 => offset of dst port after v4 header */
  1106. extra = ipa_write_8(2, extra);
  1107. rest = ipa_write_16(attrib->dst_port, rest);
  1108. rest = ipa_write_16(attrib->dst_port, rest);
  1109. ihl_ofst_rng16++;
  1110. }
  1111. if (attrib->attrib_mask & IPA_FLT_FRAGMENT)
  1112. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_FRAG);
  1113. if (attrib->attrib_mask & IPA_FLT_TOS && !tos_done) {
  1114. IPAHAL_ERR("could not find equation for tos\n");
  1115. goto err;
  1116. }
  1117. goto done;
  1118. err:
  1119. rc = -EPERM;
  1120. done:
  1121. *extra_wrds = extra;
  1122. *rest_wrds = rest;
  1123. return rc;
  1124. }
  1125. static int ipa_fltrt_generate_hw_rule_bdy_ip6(u16 *en_rule,
  1126. const struct ipa_rule_attrib *attrib,
  1127. u8 **extra_wrds, u8 **rest_wrds)
  1128. {
  1129. u8 *extra = *extra_wrds;
  1130. u8 *rest = *rest_wrds;
  1131. u8 ofst_meq32 = 0;
  1132. u8 ihl_ofst_rng16 = 0;
  1133. u8 ihl_ofst_meq32 = 0;
  1134. u8 ofst_meq128 = 0;
  1135. int rc = 0;
  1136. /* v6 code below assumes no extension headers TODO: fix this */
  1137. if (attrib->attrib_mask & IPA_FLT_IS_PURE_ACK) {
  1138. if (!IPA_IS_RULE_EQ_VALID(IPA_IS_PURE_ACK)) {
  1139. IPAHAL_ERR("is_pure_ack eq not supported\n");
  1140. goto err;
  1141. }
  1142. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_PURE_ACK);
  1143. extra = ipa_write_8(0, extra);
  1144. }
  1145. if (attrib->attrib_mask & IPA_FLT_NEXT_HDR) {
  1146. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_PROTOCOL_EQ);
  1147. extra = ipa_write_8(attrib->u.v6.next_hdr, extra);
  1148. }
  1149. if (attrib->attrib_mask & IPA_FLT_TC) {
  1150. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_TC_EQ);
  1151. extra = ipa_write_8(attrib->u.v6.tc, extra);
  1152. }
  1153. if (attrib->attrib_mask & IPA_FLT_SRC_ADDR) {
  1154. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1155. IPAHAL_ERR("ran out of meq128 eq\n");
  1156. goto err;
  1157. }
  1158. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1159. ipa3_0_ofst_meq128[ofst_meq128]);
  1160. /* 8 => offset of src ip in v6 header */
  1161. extra = ipa_write_8(8, extra);
  1162. rest = ipa_write_32(attrib->u.v6.src_addr_mask[3], rest);
  1163. rest = ipa_write_32(attrib->u.v6.src_addr_mask[2], rest);
  1164. rest = ipa_write_32(attrib->u.v6.src_addr[3], rest);
  1165. rest = ipa_write_32(attrib->u.v6.src_addr[2], rest);
  1166. rest = ipa_write_32(attrib->u.v6.src_addr_mask[1], rest);
  1167. rest = ipa_write_32(attrib->u.v6.src_addr_mask[0], rest);
  1168. rest = ipa_write_32(attrib->u.v6.src_addr[1], rest);
  1169. rest = ipa_write_32(attrib->u.v6.src_addr[0], rest);
  1170. ofst_meq128++;
  1171. }
  1172. if (attrib->attrib_mask & IPA_FLT_DST_ADDR) {
  1173. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1174. IPAHAL_ERR("ran out of meq128 eq\n");
  1175. goto err;
  1176. }
  1177. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1178. ipa3_0_ofst_meq128[ofst_meq128]);
  1179. /* 24 => offset of dst ip in v6 header */
  1180. extra = ipa_write_8(24, extra);
  1181. rest = ipa_write_32(attrib->u.v6.dst_addr_mask[3], rest);
  1182. rest = ipa_write_32(attrib->u.v6.dst_addr_mask[2], rest);
  1183. rest = ipa_write_32(attrib->u.v6.dst_addr[3], rest);
  1184. rest = ipa_write_32(attrib->u.v6.dst_addr[2], rest);
  1185. rest = ipa_write_32(attrib->u.v6.dst_addr_mask[1], rest);
  1186. rest = ipa_write_32(attrib->u.v6.dst_addr_mask[0], rest);
  1187. rest = ipa_write_32(attrib->u.v6.dst_addr[1], rest);
  1188. rest = ipa_write_32(attrib->u.v6.dst_addr[0], rest);
  1189. ofst_meq128++;
  1190. }
  1191. if (attrib->attrib_mask & IPA_FLT_TOS_MASKED) {
  1192. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1193. IPAHAL_ERR("ran out of meq128 eq\n");
  1194. goto err;
  1195. }
  1196. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1197. ipa3_0_ofst_meq128[ofst_meq128]);
  1198. /* 0 => offset of TOS in v6 header */
  1199. extra = ipa_write_8(0, extra);
  1200. rest = ipa_write_64(0, rest);
  1201. rest = ipa_write_64(0, rest);
  1202. rest = ipa_write_32(0, rest);
  1203. rest = ipa_write_32((attrib->tos_mask << 20), rest);
  1204. rest = ipa_write_32(0, rest);
  1205. rest = ipa_write_32((attrib->tos_value << 20), rest);
  1206. ofst_meq128++;
  1207. }
  1208. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_ETHER_II) {
  1209. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1210. IPAHAL_ERR("ran out of meq128 eq\n");
  1211. goto err;
  1212. }
  1213. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1214. ipa3_0_ofst_meq128[ofst_meq128]);
  1215. /* -14 => offset of dst mac addr in Ethernet II hdr */
  1216. ipa_fltrt_generate_mac_addr_hw_rule(
  1217. &extra,
  1218. &rest,
  1219. -14,
  1220. attrib->dst_mac_addr_mask,
  1221. attrib->dst_mac_addr);
  1222. ofst_meq128++;
  1223. }
  1224. if (attrib->attrib_mask & IPA_FLT_MAC_SRC_ADDR_ETHER_II) {
  1225. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1226. IPAHAL_ERR("ran out of meq128 eq\n");
  1227. goto err;
  1228. }
  1229. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1230. ipa3_0_ofst_meq128[ofst_meq128]);
  1231. /* -8 => offset of src mac addr in Ethernet II hdr */
  1232. ipa_fltrt_generate_mac_addr_hw_rule(
  1233. &extra,
  1234. &rest,
  1235. -8,
  1236. attrib->src_mac_addr_mask,
  1237. attrib->src_mac_addr);
  1238. ofst_meq128++;
  1239. }
  1240. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_802_3) {
  1241. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1242. IPAHAL_ERR("ran out of meq128 eq\n");
  1243. goto err;
  1244. }
  1245. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1246. ipa3_0_ofst_meq128[ofst_meq128]);
  1247. /* -22 => offset of dst mac addr in 802.3 hdr */
  1248. ipa_fltrt_generate_mac_addr_hw_rule(
  1249. &extra,
  1250. &rest,
  1251. -22,
  1252. attrib->dst_mac_addr_mask,
  1253. attrib->dst_mac_addr);
  1254. ofst_meq128++;
  1255. }
  1256. if (attrib->attrib_mask & IPA_FLT_MAC_SRC_ADDR_802_3) {
  1257. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1258. IPAHAL_ERR("ran out of meq128 eq\n");
  1259. goto err;
  1260. }
  1261. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1262. ipa3_0_ofst_meq128[ofst_meq128]);
  1263. /* -16 => offset of src mac addr in 802.3 hdr */
  1264. ipa_fltrt_generate_mac_addr_hw_rule(
  1265. &extra,
  1266. &rest,
  1267. -16,
  1268. attrib->src_mac_addr_mask,
  1269. attrib->src_mac_addr);
  1270. ofst_meq128++;
  1271. }
  1272. if (attrib->attrib_mask & IPA_FLT_MAC_ETHER_TYPE) {
  1273. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  1274. IPAHAL_ERR("ran out of meq32 eq\n");
  1275. goto err;
  1276. }
  1277. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1278. ipa3_0_ofst_meq32[ofst_meq32]);
  1279. /* -2 => offset of ether type in L2 hdr */
  1280. extra = ipa_write_8((u8)-2, extra);
  1281. rest = ipa_write_16(0, rest);
  1282. rest = ipa_write_16(htons(attrib->ether_type), rest);
  1283. rest = ipa_write_16(0, rest);
  1284. rest = ipa_write_16(htons(attrib->ether_type), rest);
  1285. ofst_meq32++;
  1286. }
  1287. if (attrib->attrib_mask & IPA_FLT_TYPE) {
  1288. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1289. ihl_ofst_meq32)) {
  1290. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1291. goto err;
  1292. }
  1293. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1294. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1295. /* 0 => offset of type after v6 header */
  1296. extra = ipa_write_8(0, extra);
  1297. rest = ipa_write_32(0xFF, rest);
  1298. rest = ipa_write_32(attrib->type, rest);
  1299. ihl_ofst_meq32++;
  1300. }
  1301. if (attrib->attrib_mask & IPA_FLT_CODE) {
  1302. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1303. ihl_ofst_meq32)) {
  1304. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1305. goto err;
  1306. }
  1307. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1308. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1309. /* 1 => offset of code after v6 header */
  1310. extra = ipa_write_8(1, extra);
  1311. rest = ipa_write_32(0xFF, rest);
  1312. rest = ipa_write_32(attrib->code, rest);
  1313. ihl_ofst_meq32++;
  1314. }
  1315. if (attrib->attrib_mask & IPA_FLT_SPI) {
  1316. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1317. ihl_ofst_meq32)) {
  1318. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1319. goto err;
  1320. }
  1321. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1322. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1323. /* 0 => offset of SPI after v6 header FIXME */
  1324. extra = ipa_write_8(0, extra);
  1325. rest = ipa_write_32(0xFFFFFFFF, rest);
  1326. rest = ipa_write_32(attrib->spi, rest);
  1327. ihl_ofst_meq32++;
  1328. }
  1329. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_L2TP) {
  1330. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1331. ihl_ofst_meq32) || IPA_IS_RAN_OUT_OF_EQ(
  1332. ipa3_0_ihl_ofst_meq32, ihl_ofst_meq32 + 1)) {
  1333. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1334. goto err;
  1335. }
  1336. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1337. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1338. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1339. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32 + 1]);
  1340. /* populate first ihl meq eq */
  1341. extra = ipa_write_8(8, extra);
  1342. rest = ipa_write_8(attrib->dst_mac_addr_mask[3], rest);
  1343. rest = ipa_write_8(attrib->dst_mac_addr_mask[2], rest);
  1344. rest = ipa_write_8(attrib->dst_mac_addr_mask[1], rest);
  1345. rest = ipa_write_8(attrib->dst_mac_addr_mask[0], rest);
  1346. rest = ipa_write_8(attrib->dst_mac_addr[3], rest);
  1347. rest = ipa_write_8(attrib->dst_mac_addr[2], rest);
  1348. rest = ipa_write_8(attrib->dst_mac_addr[1], rest);
  1349. rest = ipa_write_8(attrib->dst_mac_addr[0], rest);
  1350. /* populate second ihl meq eq */
  1351. extra = ipa_write_8(12, extra);
  1352. rest = ipa_write_16(0, rest);
  1353. rest = ipa_write_8(attrib->dst_mac_addr_mask[5], rest);
  1354. rest = ipa_write_8(attrib->dst_mac_addr_mask[4], rest);
  1355. rest = ipa_write_16(0, rest);
  1356. rest = ipa_write_8(attrib->dst_mac_addr[5], rest);
  1357. rest = ipa_write_8(attrib->dst_mac_addr[4], rest);
  1358. ihl_ofst_meq32 += 2;
  1359. }
  1360. if (attrib->attrib_mask & IPA_FLT_TCP_SYN) {
  1361. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1362. ihl_ofst_meq32)) {
  1363. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1364. goto err;
  1365. }
  1366. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1367. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1368. /* 12 => offset of SYN after v4 header */
  1369. extra = ipa_write_8(12, extra);
  1370. rest = ipa_write_32(0x20000, rest);
  1371. rest = ipa_write_32(0x20000, rest);
  1372. ihl_ofst_meq32++;
  1373. }
  1374. if (attrib->attrib_mask & IPA_FLT_TCP_SYN_L2TP) {
  1375. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1376. ihl_ofst_meq32) || IPA_IS_RAN_OUT_OF_EQ(
  1377. ipa3_0_ihl_ofst_meq32, ihl_ofst_meq32 + 1)) {
  1378. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1379. goto err;
  1380. }
  1381. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1382. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1383. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1384. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32 + 1]);
  1385. /* populate TCP protocol eq */
  1386. if (attrib->ether_type == 0x0800) {
  1387. extra = ipa_write_8(30, extra);
  1388. rest = ipa_write_32(0xFF0000, rest);
  1389. rest = ipa_write_32(0x60000, rest);
  1390. } else {
  1391. extra = ipa_write_8(26, extra);
  1392. rest = ipa_write_32(0xFF00, rest);
  1393. rest = ipa_write_32(0x600, rest);
  1394. }
  1395. /* populate TCP SYN eq */
  1396. if (attrib->ether_type == 0x0800) {
  1397. extra = ipa_write_8(54, extra);
  1398. rest = ipa_write_32(0x20000, rest);
  1399. rest = ipa_write_32(0x20000, rest);
  1400. } else {
  1401. extra = ipa_write_8(74, extra);
  1402. rest = ipa_write_32(0x20000, rest);
  1403. rest = ipa_write_32(0x20000, rest);
  1404. }
  1405. ihl_ofst_meq32 += 2;
  1406. }
  1407. if (attrib->attrib_mask & IPA_FLT_L2TP_INNER_IP_TYPE) {
  1408. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1409. ihl_ofst_meq32)) {
  1410. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1411. goto err;
  1412. }
  1413. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1414. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1415. /* 22 => offset of IP type after v6 header */
  1416. extra = ipa_write_8(22, extra);
  1417. rest = ipa_write_32(0xF0000000, rest);
  1418. if (attrib->type == 0x40)
  1419. rest = ipa_write_32(0x40000000, rest);
  1420. else
  1421. rest = ipa_write_32(0x60000000, rest);
  1422. ihl_ofst_meq32++;
  1423. }
  1424. if (attrib->attrib_mask & IPA_FLT_L2TP_INNER_IPV4_DST_ADDR) {
  1425. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1426. ihl_ofst_meq32)) {
  1427. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1428. goto err;
  1429. }
  1430. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1431. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1432. /* 38 => offset of inner IPv4 addr */
  1433. extra = ipa_write_8(38, extra);
  1434. rest = ipa_write_32(attrib->u.v4.dst_addr_mask, rest);
  1435. rest = ipa_write_32(attrib->u.v4.dst_addr, rest);
  1436. ihl_ofst_meq32++;
  1437. }
  1438. if (attrib->attrib_mask & IPA_FLT_META_DATA) {
  1439. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_METADATA_COMPARE);
  1440. rest = ipa_write_32(attrib->meta_data_mask, rest);
  1441. rest = ipa_write_32(attrib->meta_data, rest);
  1442. }
  1443. if (attrib->attrib_mask & IPA_FLT_SRC_PORT) {
  1444. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1445. ihl_ofst_rng16)) {
  1446. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1447. goto err;
  1448. }
  1449. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1450. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1451. /* 0 => offset of src port after v6 header */
  1452. extra = ipa_write_8(0, extra);
  1453. rest = ipa_write_16(attrib->src_port, rest);
  1454. rest = ipa_write_16(attrib->src_port, rest);
  1455. ihl_ofst_rng16++;
  1456. }
  1457. if (attrib->attrib_mask & IPA_FLT_DST_PORT) {
  1458. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1459. ihl_ofst_rng16)) {
  1460. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1461. goto err;
  1462. }
  1463. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1464. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1465. /* 2 => offset of dst port after v6 header */
  1466. extra = ipa_write_8(2, extra);
  1467. rest = ipa_write_16(attrib->dst_port, rest);
  1468. rest = ipa_write_16(attrib->dst_port, rest);
  1469. ihl_ofst_rng16++;
  1470. }
  1471. if (attrib->attrib_mask & IPA_FLT_SRC_PORT_RANGE) {
  1472. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1473. ihl_ofst_rng16)) {
  1474. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1475. goto err;
  1476. }
  1477. if (attrib->src_port_hi < attrib->src_port_lo) {
  1478. IPAHAL_ERR("bad src port range param\n");
  1479. goto err;
  1480. }
  1481. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1482. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1483. /* 0 => offset of src port after v6 header */
  1484. extra = ipa_write_8(0, extra);
  1485. rest = ipa_write_16(attrib->src_port_hi, rest);
  1486. rest = ipa_write_16(attrib->src_port_lo, rest);
  1487. ihl_ofst_rng16++;
  1488. }
  1489. if (attrib->attrib_mask & IPA_FLT_DST_PORT_RANGE) {
  1490. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1491. ihl_ofst_rng16)) {
  1492. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1493. goto err;
  1494. }
  1495. if (attrib->dst_port_hi < attrib->dst_port_lo) {
  1496. IPAHAL_ERR("bad dst port range param\n");
  1497. goto err;
  1498. }
  1499. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1500. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1501. /* 2 => offset of dst port after v6 header */
  1502. extra = ipa_write_8(2, extra);
  1503. rest = ipa_write_16(attrib->dst_port_hi, rest);
  1504. rest = ipa_write_16(attrib->dst_port_lo, rest);
  1505. ihl_ofst_rng16++;
  1506. }
  1507. if (attrib->attrib_mask & IPA_FLT_TCP_SYN_L2TP) {
  1508. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  1509. ihl_ofst_rng16)) {
  1510. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  1511. goto err;
  1512. }
  1513. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1514. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  1515. /* 20 => offset of Ethertype after v4 header */
  1516. if (attrib->ether_type == 0x0800) {
  1517. extra = ipa_write_8(21, extra);
  1518. rest = ipa_write_16(0x0045, rest);
  1519. rest = ipa_write_16(0x0045, rest);
  1520. } else {
  1521. extra = ipa_write_8(20, extra);
  1522. rest = ipa_write_16(attrib->ether_type, rest);
  1523. rest = ipa_write_16(attrib->ether_type, rest);
  1524. }
  1525. ihl_ofst_rng16++;
  1526. }
  1527. if (attrib->attrib_mask & IPA_FLT_FLOW_LABEL) {
  1528. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_FL_EQ);
  1529. rest = ipa_write_32(attrib->u.v6.flow_label & 0xFFFFF,
  1530. rest);
  1531. }
  1532. if (attrib->attrib_mask & IPA_FLT_FRAGMENT)
  1533. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_FRAG);
  1534. goto done;
  1535. err:
  1536. rc = -EPERM;
  1537. done:
  1538. *extra_wrds = extra;
  1539. *rest_wrds = rest;
  1540. return rc;
  1541. }
  1542. static u8 *ipa_fltrt_copy_mem(u8 *src, u8 *dst, int cnt)
  1543. {
  1544. while (cnt--)
  1545. *dst++ = *src++;
  1546. return dst;
  1547. }
  1548. /*
  1549. * ipa_fltrt_generate_hw_rule_bdy() - generate HW rule body (w/o header)
  1550. * @ip: IP address type
  1551. * @attrib: IPA rule attribute
  1552. * @buf: output buffer. Advance it after building the rule
  1553. * @en_rule: enable rule
  1554. *
  1555. * Return codes:
  1556. * 0: success
  1557. * -EPERM: wrong input
  1558. */
  1559. static int ipa_fltrt_generate_hw_rule_bdy(enum ipa_ip_type ipt,
  1560. const struct ipa_rule_attrib *attrib, u8 **buf, u16 *en_rule)
  1561. {
  1562. int sz;
  1563. int rc = 0;
  1564. u8 *extra_wrd_buf;
  1565. u8 *rest_wrd_buf;
  1566. u8 *extra_wrd_start;
  1567. u8 *rest_wrd_start;
  1568. u8 *extra_wrd_i;
  1569. u8 *rest_wrd_i;
  1570. sz = IPA3_0_HW_TBL_WIDTH * 2 + IPA3_0_HW_RULE_START_ALIGNMENT;
  1571. extra_wrd_buf = kzalloc(sz, GFP_KERNEL);
  1572. if (!extra_wrd_buf) {
  1573. rc = -ENOMEM;
  1574. goto fail_extra_alloc;
  1575. }
  1576. sz = IPA3_0_HW_RULE_BUF_SIZE + IPA3_0_HW_RULE_START_ALIGNMENT;
  1577. rest_wrd_buf = kzalloc(sz, GFP_KERNEL);
  1578. if (!rest_wrd_buf) {
  1579. rc = -ENOMEM;
  1580. goto fail_rest_alloc;
  1581. }
  1582. extra_wrd_start = extra_wrd_buf + IPA3_0_HW_RULE_START_ALIGNMENT;
  1583. extra_wrd_start = (u8 *)((long)extra_wrd_start &
  1584. ~IPA3_0_HW_RULE_START_ALIGNMENT);
  1585. rest_wrd_start = rest_wrd_buf + IPA3_0_HW_RULE_START_ALIGNMENT;
  1586. rest_wrd_start = (u8 *)((long)rest_wrd_start &
  1587. ~IPA3_0_HW_RULE_START_ALIGNMENT);
  1588. extra_wrd_i = extra_wrd_start;
  1589. rest_wrd_i = rest_wrd_start;
  1590. rc = ipa_fltrt_rule_generation_err_check(ipt, attrib);
  1591. if (rc) {
  1592. IPAHAL_ERR_RL("rule generation err check failed\n");
  1593. goto fail_err_check;
  1594. }
  1595. if (ipt == IPA_IP_v4) {
  1596. if (ipa_fltrt_generate_hw_rule_bdy_ip4(en_rule, attrib,
  1597. &extra_wrd_i, &rest_wrd_i)) {
  1598. IPAHAL_ERR_RL("failed to build ipv4 hw rule\n");
  1599. rc = -EPERM;
  1600. goto fail_err_check;
  1601. }
  1602. } else if (ipt == IPA_IP_v6) {
  1603. if (ipa_fltrt_generate_hw_rule_bdy_ip6(en_rule, attrib,
  1604. &extra_wrd_i, &rest_wrd_i)) {
  1605. IPAHAL_ERR_RL("failed to build ipv6 hw rule\n");
  1606. rc = -EPERM;
  1607. goto fail_err_check;
  1608. }
  1609. } else {
  1610. IPAHAL_ERR_RL("unsupported ip %d\n", ipt);
  1611. goto fail_err_check;
  1612. }
  1613. /*
  1614. * default "rule" means no attributes set -> map to
  1615. * OFFSET_MEQ32_0 with mask of 0 and val of 0 and offset 0
  1616. */
  1617. if (attrib->attrib_mask == 0) {
  1618. IPAHAL_DBG_LOW("building default rule\n");
  1619. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(ipa3_0_ofst_meq32[0]);
  1620. extra_wrd_i = ipa_write_8(0, extra_wrd_i); /* offset */
  1621. rest_wrd_i = ipa_write_32(0, rest_wrd_i); /* mask */
  1622. rest_wrd_i = ipa_write_32(0, rest_wrd_i); /* val */
  1623. }
  1624. IPAHAL_DBG_LOW("extra_word_1 0x%llx\n", *(u64 *)extra_wrd_start);
  1625. IPAHAL_DBG_LOW("extra_word_2 0x%llx\n",
  1626. *(u64 *)(extra_wrd_start + IPA3_0_HW_TBL_WIDTH));
  1627. extra_wrd_i = ipa_pad_to_64(extra_wrd_i);
  1628. sz = extra_wrd_i - extra_wrd_start;
  1629. IPAHAL_DBG_LOW("extra words params sz %d\n", sz);
  1630. *buf = ipa_fltrt_copy_mem(extra_wrd_start, *buf, sz);
  1631. rest_wrd_i = ipa_pad_to_64(rest_wrd_i);
  1632. sz = rest_wrd_i - rest_wrd_start;
  1633. IPAHAL_DBG_LOW("non extra words params sz %d\n", sz);
  1634. *buf = ipa_fltrt_copy_mem(rest_wrd_start, *buf, sz);
  1635. fail_err_check:
  1636. kfree(rest_wrd_buf);
  1637. fail_rest_alloc:
  1638. kfree(extra_wrd_buf);
  1639. fail_extra_alloc:
  1640. return rc;
  1641. }
  1642. /**
  1643. * ipa_fltrt_calc_extra_wrd_bytes()- Calculate the number of extra words for eq
  1644. * @attrib: equation attribute
  1645. *
  1646. * Return value: 0 on success, negative otherwise
  1647. */
  1648. static int ipa_fltrt_calc_extra_wrd_bytes(
  1649. const struct ipa_ipfltri_rule_eq *attrib)
  1650. {
  1651. int num = 0;
  1652. /*
  1653. * tos_eq_present field has two meanings:
  1654. * tos equation for IPA ver < 4.5 (as the field name reveals)
  1655. * pure_ack equation for IPA ver >= 4.5
  1656. * In both cases it needs one extra word.
  1657. */
  1658. if (attrib->tos_eq_present)
  1659. num++;
  1660. if (attrib->protocol_eq_present)
  1661. num++;
  1662. if (attrib->tc_eq_present)
  1663. num++;
  1664. num += attrib->num_offset_meq_128;
  1665. num += attrib->num_offset_meq_32;
  1666. num += attrib->num_ihl_offset_meq_32;
  1667. num += attrib->num_ihl_offset_range_16;
  1668. if (attrib->ihl_offset_eq_32_present)
  1669. num++;
  1670. if (attrib->ihl_offset_eq_16_present)
  1671. num++;
  1672. IPAHAL_DBG_LOW("extra bytes number %d\n", num);
  1673. return num;
  1674. }
  1675. static int ipa_fltrt_generate_hw_rule_bdy_from_eq(
  1676. const struct ipa_ipfltri_rule_eq *attrib, u8 **buf)
  1677. {
  1678. uint8_t num_offset_meq_32 = attrib->num_offset_meq_32;
  1679. uint8_t num_ihl_offset_range_16 = attrib->num_ihl_offset_range_16;
  1680. uint8_t num_ihl_offset_meq_32 = attrib->num_ihl_offset_meq_32;
  1681. uint8_t num_offset_meq_128 = attrib->num_offset_meq_128;
  1682. int i;
  1683. int extra_bytes;
  1684. u8 *extra;
  1685. u8 *rest;
  1686. extra_bytes = ipa_fltrt_calc_extra_wrd_bytes(attrib);
  1687. /* only 3 eq does not have extra word param, 13 out of 16 is the number
  1688. * of equations that needs extra word param
  1689. */
  1690. if (extra_bytes > 13) {
  1691. IPAHAL_ERR_RL("too much extra bytes\n");
  1692. return -EPERM;
  1693. } else if (extra_bytes > IPA3_0_HW_TBL_HDR_WIDTH) {
  1694. /* two extra words */
  1695. extra = *buf;
  1696. rest = *buf + IPA3_0_HW_TBL_HDR_WIDTH * 2;
  1697. } else if (extra_bytes > 0) {
  1698. /* single exra word */
  1699. extra = *buf;
  1700. rest = *buf + IPA3_0_HW_TBL_HDR_WIDTH;
  1701. } else {
  1702. /* no extra words */
  1703. extra = NULL;
  1704. rest = *buf;
  1705. }
  1706. /*
  1707. * tos_eq_present field has two meanings:
  1708. * tos equation for IPA ver < 4.5 (as the field name reveals)
  1709. * pure_ack equation for IPA ver >= 4.5
  1710. * In both cases it needs one extra word.
  1711. */
  1712. if (attrib->tos_eq_present) {
  1713. if (IPA_IS_RULE_EQ_VALID(IPA_IS_PURE_ACK)) {
  1714. extra = ipa_write_8(0, extra);
  1715. } else if (IPA_IS_RULE_EQ_VALID(IPA_TOS_EQ)) {
  1716. extra = ipa_write_8(attrib->tos_eq, extra);
  1717. } else {
  1718. IPAHAL_ERR("no support for pure_ack and tos eqs\n");
  1719. return -EPERM;
  1720. }
  1721. }
  1722. if (attrib->protocol_eq_present)
  1723. extra = ipa_write_8(attrib->protocol_eq, extra);
  1724. if (attrib->tc_eq_present)
  1725. extra = ipa_write_8(attrib->tc_eq, extra);
  1726. if (num_offset_meq_128) {
  1727. extra = ipa_write_8(attrib->offset_meq_128[0].offset, extra);
  1728. for (i = 0; i < 8; i++)
  1729. rest = ipa_write_8(attrib->offset_meq_128[0].mask[i],
  1730. rest);
  1731. for (i = 0; i < 8; i++)
  1732. rest = ipa_write_8(attrib->offset_meq_128[0].value[i],
  1733. rest);
  1734. for (i = 8; i < 16; i++)
  1735. rest = ipa_write_8(attrib->offset_meq_128[0].mask[i],
  1736. rest);
  1737. for (i = 8; i < 16; i++)
  1738. rest = ipa_write_8(attrib->offset_meq_128[0].value[i],
  1739. rest);
  1740. num_offset_meq_128--;
  1741. }
  1742. if (num_offset_meq_128) {
  1743. extra = ipa_write_8(attrib->offset_meq_128[1].offset, extra);
  1744. for (i = 0; i < 8; i++)
  1745. rest = ipa_write_8(attrib->offset_meq_128[1].mask[i],
  1746. rest);
  1747. for (i = 0; i < 8; i++)
  1748. rest = ipa_write_8(attrib->offset_meq_128[1].value[i],
  1749. rest);
  1750. for (i = 8; i < 16; i++)
  1751. rest = ipa_write_8(attrib->offset_meq_128[1].mask[i],
  1752. rest);
  1753. for (i = 8; i < 16; i++)
  1754. rest = ipa_write_8(attrib->offset_meq_128[1].value[i],
  1755. rest);
  1756. num_offset_meq_128--;
  1757. }
  1758. if (num_offset_meq_32) {
  1759. extra = ipa_write_8(attrib->offset_meq_32[0].offset, extra);
  1760. rest = ipa_write_32(attrib->offset_meq_32[0].mask, rest);
  1761. rest = ipa_write_32(attrib->offset_meq_32[0].value, rest);
  1762. num_offset_meq_32--;
  1763. }
  1764. if (num_offset_meq_32) {
  1765. extra = ipa_write_8(attrib->offset_meq_32[1].offset, extra);
  1766. rest = ipa_write_32(attrib->offset_meq_32[1].mask, rest);
  1767. rest = ipa_write_32(attrib->offset_meq_32[1].value, rest);
  1768. num_offset_meq_32--;
  1769. }
  1770. if (num_ihl_offset_meq_32) {
  1771. extra = ipa_write_8(attrib->ihl_offset_meq_32[0].offset,
  1772. extra);
  1773. rest = ipa_write_32(attrib->ihl_offset_meq_32[0].mask, rest);
  1774. rest = ipa_write_32(attrib->ihl_offset_meq_32[0].value, rest);
  1775. num_ihl_offset_meq_32--;
  1776. }
  1777. if (num_ihl_offset_meq_32) {
  1778. extra = ipa_write_8(attrib->ihl_offset_meq_32[1].offset,
  1779. extra);
  1780. rest = ipa_write_32(attrib->ihl_offset_meq_32[1].mask, rest);
  1781. rest = ipa_write_32(attrib->ihl_offset_meq_32[1].value, rest);
  1782. num_ihl_offset_meq_32--;
  1783. }
  1784. if (attrib->metadata_meq32_present) {
  1785. rest = ipa_write_32(attrib->metadata_meq32.mask, rest);
  1786. rest = ipa_write_32(attrib->metadata_meq32.value, rest);
  1787. }
  1788. if (num_ihl_offset_range_16) {
  1789. extra = ipa_write_8(attrib->ihl_offset_range_16[0].offset,
  1790. extra);
  1791. rest = ipa_write_16(attrib->ihl_offset_range_16[0].range_high,
  1792. rest);
  1793. rest = ipa_write_16(attrib->ihl_offset_range_16[0].range_low,
  1794. rest);
  1795. num_ihl_offset_range_16--;
  1796. }
  1797. if (num_ihl_offset_range_16) {
  1798. extra = ipa_write_8(attrib->ihl_offset_range_16[1].offset,
  1799. extra);
  1800. rest = ipa_write_16(attrib->ihl_offset_range_16[1].range_high,
  1801. rest);
  1802. rest = ipa_write_16(attrib->ihl_offset_range_16[1].range_low,
  1803. rest);
  1804. num_ihl_offset_range_16--;
  1805. }
  1806. if (attrib->ihl_offset_eq_32_present) {
  1807. extra = ipa_write_8(attrib->ihl_offset_eq_32.offset, extra);
  1808. rest = ipa_write_32(attrib->ihl_offset_eq_32.value, rest);
  1809. }
  1810. if (attrib->ihl_offset_eq_16_present) {
  1811. extra = ipa_write_8(attrib->ihl_offset_eq_16.offset, extra);
  1812. rest = ipa_write_16(attrib->ihl_offset_eq_16.value, rest);
  1813. rest = ipa_write_16(0, rest);
  1814. }
  1815. if (attrib->fl_eq_present)
  1816. rest = ipa_write_32(attrib->fl_eq & 0xFFFFF, rest);
  1817. if (extra)
  1818. extra = ipa_pad_to_64(extra);
  1819. rest = ipa_pad_to_64(rest);
  1820. *buf = rest;
  1821. return 0;
  1822. }
  1823. static void ipa_flt_generate_mac_addr_eq(struct ipa_ipfltri_rule_eq *eq_atrb,
  1824. u8 hdr_mac_addr_offset, const uint8_t mac_addr_mask[ETH_ALEN],
  1825. const uint8_t mac_addr[ETH_ALEN], u8 ofst_meq128)
  1826. {
  1827. int i;
  1828. eq_atrb->offset_meq_128[ofst_meq128].offset = hdr_mac_addr_offset;
  1829. /* LSB MASK and ADDR */
  1830. memset(eq_atrb->offset_meq_128[ofst_meq128].mask, 0, 8);
  1831. memset(eq_atrb->offset_meq_128[ofst_meq128].value, 0, 8);
  1832. /* MSB MASK and ADDR */
  1833. memset(eq_atrb->offset_meq_128[ofst_meq128].mask + 8, 0, 2);
  1834. for (i = 0; i <= 5; i++)
  1835. eq_atrb->offset_meq_128[ofst_meq128].mask[15 - i] =
  1836. mac_addr_mask[i];
  1837. memset(eq_atrb->offset_meq_128[ofst_meq128].value + 8, 0, 2);
  1838. for (i = 0; i <= 5; i++)
  1839. eq_atrb->offset_meq_128[ofst_meq128].value[15 - i] =
  1840. mac_addr[i];
  1841. }
  1842. static int ipa_flt_generate_eq_ip4(enum ipa_ip_type ip,
  1843. const struct ipa_rule_attrib *attrib,
  1844. struct ipa_ipfltri_rule_eq *eq_atrb)
  1845. {
  1846. u8 ofst_meq32 = 0;
  1847. u8 ihl_ofst_rng16 = 0;
  1848. u8 ihl_ofst_meq32 = 0;
  1849. u8 ofst_meq128 = 0;
  1850. u16 eq_bitmap = 0;
  1851. u16 *en_rule = &eq_bitmap;
  1852. bool tos_done = false;
  1853. if (attrib->attrib_mask & IPA_FLT_IS_PURE_ACK) {
  1854. if (!IPA_IS_RULE_EQ_VALID(IPA_IS_PURE_ACK)) {
  1855. IPAHAL_ERR("is_pure_ack eq not supported\n");
  1856. return -EPERM;
  1857. }
  1858. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_PURE_ACK);
  1859. /*
  1860. * Starting IPA 4.5, where PURE ACK equation supported
  1861. * and TOS equation support removed, field tos_eq_present
  1862. * represent pure_ack presence.
  1863. */
  1864. eq_atrb->tos_eq_present = 1;
  1865. eq_atrb->tos_eq = 0;
  1866. }
  1867. if (attrib->attrib_mask & IPA_FLT_TOS && !tos_done) {
  1868. if (!IPA_IS_RULE_EQ_VALID(IPA_TOS_EQ)) {
  1869. IPAHAL_DBG("tos eq not supported\n");
  1870. } else {
  1871. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_TOS_EQ);
  1872. eq_atrb->tos_eq_present = 1;
  1873. eq_atrb->tos_eq = attrib->u.v4.tos;
  1874. }
  1875. }
  1876. if (attrib->attrib_mask & IPA_FLT_PROTOCOL) {
  1877. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_PROTOCOL_EQ);
  1878. eq_atrb->protocol_eq_present = 1;
  1879. eq_atrb->protocol_eq = attrib->u.v4.protocol;
  1880. }
  1881. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_ETHER_II) {
  1882. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1883. IPAHAL_ERR("ran out of meq128 eq\n");
  1884. return -EPERM;
  1885. }
  1886. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1887. ipa3_0_ofst_meq128[ofst_meq128]);
  1888. /* -14 => offset of dst mac addr in Ethernet II hdr */
  1889. ipa_flt_generate_mac_addr_eq(eq_atrb, -14,
  1890. attrib->dst_mac_addr_mask, attrib->dst_mac_addr,
  1891. ofst_meq128);
  1892. ofst_meq128++;
  1893. }
  1894. if (attrib->attrib_mask & IPA_FLT_MAC_SRC_ADDR_ETHER_II) {
  1895. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1896. IPAHAL_ERR("ran out of meq128 eq\n");
  1897. return -EPERM;
  1898. }
  1899. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1900. ipa3_0_ofst_meq128[ofst_meq128]);
  1901. /* -8 => offset of src mac addr in Ethernet II hdr */
  1902. ipa_flt_generate_mac_addr_eq(eq_atrb, -8,
  1903. attrib->src_mac_addr_mask, attrib->src_mac_addr,
  1904. ofst_meq128);
  1905. ofst_meq128++;
  1906. }
  1907. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_802_3) {
  1908. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1909. IPAHAL_ERR("ran out of meq128 eq\n");
  1910. return -EPERM;
  1911. }
  1912. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1913. ipa3_0_ofst_meq128[ofst_meq128]);
  1914. /* -22 => offset of dst mac addr in 802.3 hdr */
  1915. ipa_flt_generate_mac_addr_eq(eq_atrb, -22,
  1916. attrib->dst_mac_addr_mask, attrib->dst_mac_addr,
  1917. ofst_meq128);
  1918. ofst_meq128++;
  1919. }
  1920. if (attrib->attrib_mask & IPA_FLT_MAC_SRC_ADDR_802_3) {
  1921. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  1922. IPAHAL_ERR("ran out of meq128 eq\n");
  1923. return -EPERM;
  1924. }
  1925. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1926. ipa3_0_ofst_meq128[ofst_meq128]);
  1927. /* -16 => offset of src mac addr in 802.3 hdr */
  1928. ipa_flt_generate_mac_addr_eq(eq_atrb, -16,
  1929. attrib->src_mac_addr_mask, attrib->src_mac_addr,
  1930. ofst_meq128);
  1931. ofst_meq128++;
  1932. }
  1933. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_L2TP) {
  1934. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1935. ihl_ofst_meq32) || IPA_IS_RAN_OUT_OF_EQ(
  1936. ipa3_0_ihl_ofst_meq32, ihl_ofst_meq32 + 1)) {
  1937. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1938. return -EPERM;
  1939. }
  1940. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1941. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1942. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1943. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32 + 1]);
  1944. /* populate the first ihl meq 32 eq */
  1945. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 8;
  1946. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  1947. (attrib->dst_mac_addr_mask[3] & 0xFF) |
  1948. ((attrib->dst_mac_addr_mask[2] << 8) & 0xFF00) |
  1949. ((attrib->dst_mac_addr_mask[1] << 16) & 0xFF0000) |
  1950. ((attrib->dst_mac_addr_mask[0] << 24) & 0xFF000000);
  1951. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  1952. (attrib->dst_mac_addr[3] & 0xFF) |
  1953. ((attrib->dst_mac_addr[2] << 8) & 0xFF00) |
  1954. ((attrib->dst_mac_addr[1] << 16) & 0xFF0000) |
  1955. ((attrib->dst_mac_addr[0] << 24) & 0xFF000000);
  1956. /* populate the second ihl meq 32 eq */
  1957. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32 + 1].offset = 12;
  1958. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32 + 1].mask =
  1959. ((attrib->dst_mac_addr_mask[5] << 16) & 0xFF0000) |
  1960. ((attrib->dst_mac_addr_mask[4] << 24) & 0xFF000000);
  1961. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32 + 1].value =
  1962. ((attrib->dst_mac_addr[5] << 16) & 0xFF0000) |
  1963. ((attrib->dst_mac_addr[4] << 24) & 0xFF000000);
  1964. ihl_ofst_meq32 += 2;
  1965. }
  1966. if (attrib->attrib_mask & IPA_FLT_TCP_SYN) {
  1967. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  1968. ihl_ofst_meq32)) {
  1969. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  1970. return -EPERM;
  1971. }
  1972. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1973. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  1974. /* 12 => offset of SYN after v4 header */
  1975. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 12;
  1976. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask = 0x20000;
  1977. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value = 0x20000;
  1978. ihl_ofst_meq32++;
  1979. }
  1980. if (attrib->attrib_mask & IPA_FLT_TOS_MASKED) {
  1981. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  1982. IPAHAL_ERR("ran out of meq32 eq\n");
  1983. return -EPERM;
  1984. }
  1985. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  1986. ipa3_0_ofst_meq32[ofst_meq32]);
  1987. eq_atrb->offset_meq_32[ofst_meq32].offset = 0;
  1988. eq_atrb->offset_meq_32[ofst_meq32].mask =
  1989. attrib->tos_mask << 16;
  1990. eq_atrb->offset_meq_32[ofst_meq32].value =
  1991. attrib->tos_value << 16;
  1992. ofst_meq32++;
  1993. }
  1994. if (attrib->attrib_mask & IPA_FLT_SRC_ADDR) {
  1995. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  1996. IPAHAL_ERR("ran out of meq32 eq\n");
  1997. return -EPERM;
  1998. }
  1999. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2000. ipa3_0_ofst_meq32[ofst_meq32]);
  2001. eq_atrb->offset_meq_32[ofst_meq32].offset = 12;
  2002. eq_atrb->offset_meq_32[ofst_meq32].mask =
  2003. attrib->u.v4.src_addr_mask;
  2004. eq_atrb->offset_meq_32[ofst_meq32].value =
  2005. attrib->u.v4.src_addr;
  2006. ofst_meq32++;
  2007. }
  2008. if (attrib->attrib_mask & IPA_FLT_DST_ADDR) {
  2009. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  2010. IPAHAL_ERR("ran out of meq32 eq\n");
  2011. return -EPERM;
  2012. }
  2013. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2014. ipa3_0_ofst_meq32[ofst_meq32]);
  2015. eq_atrb->offset_meq_32[ofst_meq32].offset = 16;
  2016. eq_atrb->offset_meq_32[ofst_meq32].mask =
  2017. attrib->u.v4.dst_addr_mask;
  2018. eq_atrb->offset_meq_32[ofst_meq32].value =
  2019. attrib->u.v4.dst_addr;
  2020. ofst_meq32++;
  2021. }
  2022. if (attrib->attrib_mask & IPA_FLT_MAC_ETHER_TYPE) {
  2023. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  2024. IPAHAL_ERR("ran out of meq32 eq\n");
  2025. return -EPERM;
  2026. }
  2027. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2028. ipa3_0_ofst_meq32[ofst_meq32]);
  2029. eq_atrb->offset_meq_32[ofst_meq32].offset = -2;
  2030. eq_atrb->offset_meq_32[ofst_meq32].mask =
  2031. htons(attrib->ether_type);
  2032. eq_atrb->offset_meq_32[ofst_meq32].value =
  2033. htons(attrib->ether_type);
  2034. ofst_meq32++;
  2035. }
  2036. if (attrib->attrib_mask & IPA_FLT_TOS && !tos_done) {
  2037. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  2038. IPAHAL_DBG("ran out of meq32 eq\n");
  2039. } else {
  2040. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2041. ipa3_0_ofst_meq32[ofst_meq32]);
  2042. /*
  2043. * offset 0 => Take the first word.
  2044. * offset of TOS in v4 header is 1
  2045. */
  2046. eq_atrb->offset_meq_32[ofst_meq32].offset = 0;
  2047. eq_atrb->offset_meq_32[ofst_meq32].mask =
  2048. 0xFF << 16;
  2049. eq_atrb->offset_meq_32[ofst_meq32].value =
  2050. attrib->u.v4.tos << 16;
  2051. ofst_meq32++;
  2052. tos_done = true;
  2053. }
  2054. }
  2055. if (attrib->attrib_mask & IPA_FLT_TYPE) {
  2056. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2057. ihl_ofst_meq32)) {
  2058. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  2059. return -EPERM;
  2060. }
  2061. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2062. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2063. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 0;
  2064. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask = 0xFF;
  2065. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2066. attrib->type;
  2067. ihl_ofst_meq32++;
  2068. }
  2069. if (attrib->attrib_mask & IPA_FLT_CODE) {
  2070. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2071. ihl_ofst_meq32)) {
  2072. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  2073. return -EPERM;
  2074. }
  2075. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2076. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2077. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 1;
  2078. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask = 0xFF;
  2079. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2080. attrib->code;
  2081. ihl_ofst_meq32++;
  2082. }
  2083. if (attrib->attrib_mask & IPA_FLT_SPI) {
  2084. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2085. ihl_ofst_meq32)) {
  2086. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  2087. return -EPERM;
  2088. }
  2089. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2090. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2091. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 0;
  2092. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2093. 0xFFFFFFFF;
  2094. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2095. attrib->spi;
  2096. ihl_ofst_meq32++;
  2097. }
  2098. if (attrib->attrib_mask & IPA_FLT_TOS && !tos_done) {
  2099. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2100. ihl_ofst_meq32)) {
  2101. IPAHAL_DBG("ran out of ihl_meq32 eq\n");
  2102. } else {
  2103. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2104. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2105. /*
  2106. * 0 => Take the first word. offset of TOS in
  2107. * v4 header is 1. MSB bit asserted at IHL means
  2108. * to ignore packet IHL and do offset inside IPA header
  2109. */
  2110. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset =
  2111. 0x80;
  2112. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2113. 0xFF << 16;
  2114. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2115. attrib->u.v4.tos << 16;
  2116. ihl_ofst_meq32++;
  2117. tos_done = true;
  2118. }
  2119. }
  2120. if (attrib->attrib_mask & IPA_FLT_META_DATA) {
  2121. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2122. IPA_METADATA_COMPARE);
  2123. eq_atrb->metadata_meq32_present = 1;
  2124. eq_atrb->metadata_meq32.offset = 0;
  2125. eq_atrb->metadata_meq32.mask = attrib->meta_data_mask;
  2126. eq_atrb->metadata_meq32.value = attrib->meta_data;
  2127. }
  2128. if (attrib->attrib_mask & IPA_FLT_SRC_PORT_RANGE) {
  2129. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2130. ihl_ofst_rng16)) {
  2131. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  2132. return -EPERM;
  2133. }
  2134. if (attrib->src_port_hi < attrib->src_port_lo) {
  2135. IPAHAL_ERR("bad src port range param\n");
  2136. return -EPERM;
  2137. }
  2138. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2139. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2140. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset = 0;
  2141. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2142. = attrib->src_port_lo;
  2143. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2144. = attrib->src_port_hi;
  2145. ihl_ofst_rng16++;
  2146. }
  2147. if (attrib->attrib_mask & IPA_FLT_DST_PORT_RANGE) {
  2148. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2149. ihl_ofst_rng16)) {
  2150. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  2151. return -EPERM;
  2152. }
  2153. if (attrib->dst_port_hi < attrib->dst_port_lo) {
  2154. IPAHAL_ERR("bad dst port range param\n");
  2155. return -EPERM;
  2156. }
  2157. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2158. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2159. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset = 2;
  2160. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2161. = attrib->dst_port_lo;
  2162. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2163. = attrib->dst_port_hi;
  2164. ihl_ofst_rng16++;
  2165. }
  2166. if (attrib->attrib_mask & IPA_FLT_SRC_PORT) {
  2167. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2168. ihl_ofst_rng16)) {
  2169. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  2170. return -EPERM;
  2171. }
  2172. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2173. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2174. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset = 0;
  2175. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2176. = attrib->src_port;
  2177. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2178. = attrib->src_port;
  2179. ihl_ofst_rng16++;
  2180. }
  2181. if (attrib->attrib_mask & IPA_FLT_DST_PORT) {
  2182. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2183. ihl_ofst_rng16)) {
  2184. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  2185. return -EPERM;
  2186. }
  2187. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2188. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2189. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset = 2;
  2190. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2191. = attrib->dst_port;
  2192. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2193. = attrib->dst_port;
  2194. ihl_ofst_rng16++;
  2195. }
  2196. if (attrib->attrib_mask & IPA_FLT_FRAGMENT) {
  2197. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_FRAG);
  2198. eq_atrb->ipv4_frag_eq_present = 1;
  2199. }
  2200. if (attrib->attrib_mask & IPA_FLT_TOS && !tos_done) {
  2201. IPAHAL_ERR("could not find equation for tos\n");
  2202. return -EPERM;
  2203. }
  2204. eq_atrb->rule_eq_bitmap = *en_rule;
  2205. eq_atrb->num_offset_meq_32 = ofst_meq32;
  2206. eq_atrb->num_ihl_offset_range_16 = ihl_ofst_rng16;
  2207. eq_atrb->num_ihl_offset_meq_32 = ihl_ofst_meq32;
  2208. eq_atrb->num_offset_meq_128 = ofst_meq128;
  2209. return 0;
  2210. }
  2211. static int ipa_flt_generate_eq_ip6(enum ipa_ip_type ip,
  2212. const struct ipa_rule_attrib *attrib,
  2213. struct ipa_ipfltri_rule_eq *eq_atrb)
  2214. {
  2215. u8 ofst_meq32 = 0;
  2216. u8 ihl_ofst_rng16 = 0;
  2217. u8 ihl_ofst_meq32 = 0;
  2218. u8 ofst_meq128 = 0;
  2219. u16 eq_bitmap = 0;
  2220. u16 *en_rule = &eq_bitmap;
  2221. if (attrib->attrib_mask & IPA_FLT_IS_PURE_ACK) {
  2222. if (!IPA_IS_RULE_EQ_VALID(IPA_IS_PURE_ACK)) {
  2223. IPAHAL_ERR("is_pure_ack eq not supported\n");
  2224. return -EPERM;
  2225. }
  2226. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_PURE_ACK);
  2227. /*
  2228. * Starting IPA 4.5, where PURE ACK equation supported
  2229. * and TOS equation support removed, field tos_eq_present
  2230. * represent pure_ack presenence.
  2231. */
  2232. eq_atrb->tos_eq_present = 1;
  2233. eq_atrb->tos_eq = 0;
  2234. }
  2235. if (attrib->attrib_mask & IPA_FLT_NEXT_HDR) {
  2236. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2237. IPA_PROTOCOL_EQ);
  2238. eq_atrb->protocol_eq_present = 1;
  2239. eq_atrb->protocol_eq = attrib->u.v6.next_hdr;
  2240. }
  2241. if (attrib->attrib_mask & IPA_FLT_TC) {
  2242. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2243. IPA_TC_EQ);
  2244. eq_atrb->tc_eq_present = 1;
  2245. eq_atrb->tc_eq = attrib->u.v6.tc;
  2246. }
  2247. if (attrib->attrib_mask & IPA_FLT_SRC_ADDR) {
  2248. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  2249. IPAHAL_ERR_RL("ran out of meq128 eq\n");
  2250. return -EPERM;
  2251. }
  2252. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2253. ipa3_0_ofst_meq128[ofst_meq128]);
  2254. /* use the same word order as in ipa v2 */
  2255. eq_atrb->offset_meq_128[ofst_meq128].offset = 8;
  2256. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 0)
  2257. = attrib->u.v6.src_addr_mask[0];
  2258. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 4)
  2259. = attrib->u.v6.src_addr_mask[1];
  2260. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 8)
  2261. = attrib->u.v6.src_addr_mask[2];
  2262. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 12)
  2263. = attrib->u.v6.src_addr_mask[3];
  2264. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value + 0)
  2265. = attrib->u.v6.src_addr[0];
  2266. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value + 4)
  2267. = attrib->u.v6.src_addr[1];
  2268. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value + 8)
  2269. = attrib->u.v6.src_addr[2];
  2270. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value +
  2271. 12) = attrib->u.v6.src_addr[3];
  2272. ofst_meq128++;
  2273. }
  2274. if (attrib->attrib_mask & IPA_FLT_DST_ADDR) {
  2275. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  2276. IPAHAL_ERR_RL("ran out of meq128 eq\n");
  2277. return -EPERM;
  2278. }
  2279. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2280. ipa3_0_ofst_meq128[ofst_meq128]);
  2281. eq_atrb->offset_meq_128[ofst_meq128].offset = 24;
  2282. /* use the same word order as in ipa v2 */
  2283. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 0)
  2284. = attrib->u.v6.dst_addr_mask[0];
  2285. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 4)
  2286. = attrib->u.v6.dst_addr_mask[1];
  2287. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 8)
  2288. = attrib->u.v6.dst_addr_mask[2];
  2289. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 12)
  2290. = attrib->u.v6.dst_addr_mask[3];
  2291. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value + 0)
  2292. = attrib->u.v6.dst_addr[0];
  2293. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value + 4)
  2294. = attrib->u.v6.dst_addr[1];
  2295. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value + 8)
  2296. = attrib->u.v6.dst_addr[2];
  2297. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value +
  2298. 12) = attrib->u.v6.dst_addr[3];
  2299. ofst_meq128++;
  2300. }
  2301. if (attrib->attrib_mask & IPA_FLT_TOS_MASKED) {
  2302. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  2303. IPAHAL_ERR_RL("ran out of meq128 eq\n");
  2304. return -EPERM;
  2305. }
  2306. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2307. ipa3_0_ofst_meq128[ofst_meq128]);
  2308. eq_atrb->offset_meq_128[ofst_meq128].offset = 0;
  2309. memset(eq_atrb->offset_meq_128[ofst_meq128].mask, 0, 12);
  2310. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].mask + 12)
  2311. = attrib->tos_mask << 20;
  2312. memset(eq_atrb->offset_meq_128[ofst_meq128].value, 0, 12);
  2313. *(u32 *)(eq_atrb->offset_meq_128[ofst_meq128].value +
  2314. 12) = attrib->tos_value << 20;
  2315. ofst_meq128++;
  2316. }
  2317. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_ETHER_II) {
  2318. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  2319. IPAHAL_ERR_RL("ran out of meq128 eq\n");
  2320. return -EPERM;
  2321. }
  2322. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2323. ipa3_0_ofst_meq128[ofst_meq128]);
  2324. /* -14 => offset of dst mac addr in Ethernet II hdr */
  2325. ipa_flt_generate_mac_addr_eq(eq_atrb, -14,
  2326. attrib->dst_mac_addr_mask, attrib->dst_mac_addr,
  2327. ofst_meq128);
  2328. ofst_meq128++;
  2329. }
  2330. if (attrib->attrib_mask & IPA_FLT_MAC_SRC_ADDR_ETHER_II) {
  2331. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  2332. IPAHAL_ERR_RL("ran out of meq128 eq\n");
  2333. return -EPERM;
  2334. }
  2335. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2336. ipa3_0_ofst_meq128[ofst_meq128]);
  2337. /* -8 => offset of src mac addr in Ethernet II hdr */
  2338. ipa_flt_generate_mac_addr_eq(eq_atrb, -8,
  2339. attrib->src_mac_addr_mask, attrib->src_mac_addr,
  2340. ofst_meq128);
  2341. ofst_meq128++;
  2342. }
  2343. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_802_3) {
  2344. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  2345. IPAHAL_ERR_RL("ran out of meq128 eq\n");
  2346. return -EPERM;
  2347. }
  2348. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2349. ipa3_0_ofst_meq128[ofst_meq128]);
  2350. /* -22 => offset of dst mac addr in 802.3 hdr */
  2351. ipa_flt_generate_mac_addr_eq(eq_atrb, -22,
  2352. attrib->dst_mac_addr_mask, attrib->dst_mac_addr,
  2353. ofst_meq128);
  2354. ofst_meq128++;
  2355. }
  2356. if (attrib->attrib_mask & IPA_FLT_MAC_SRC_ADDR_802_3) {
  2357. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq128, ofst_meq128)) {
  2358. IPAHAL_ERR_RL("ran out of meq128 eq\n");
  2359. return -EPERM;
  2360. }
  2361. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2362. ipa3_0_ofst_meq128[ofst_meq128]);
  2363. /* -16 => offset of src mac addr in 802.3 hdr */
  2364. ipa_flt_generate_mac_addr_eq(eq_atrb, -16,
  2365. attrib->src_mac_addr_mask, attrib->src_mac_addr,
  2366. ofst_meq128);
  2367. ofst_meq128++;
  2368. }
  2369. if (attrib->attrib_mask & IPA_FLT_MAC_DST_ADDR_L2TP) {
  2370. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2371. ihl_ofst_meq32) || IPA_IS_RAN_OUT_OF_EQ(
  2372. ipa3_0_ihl_ofst_meq32, ihl_ofst_meq32 + 1)) {
  2373. IPAHAL_ERR_RL("ran out of ihl_meq32 eq\n");
  2374. return -EPERM;
  2375. }
  2376. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2377. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2378. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2379. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32 + 1]);
  2380. /* populate the first ihl meq 32 eq */
  2381. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 8;
  2382. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2383. (attrib->dst_mac_addr_mask[3] & 0xFF) |
  2384. ((attrib->dst_mac_addr_mask[2] << 8) & 0xFF00) |
  2385. ((attrib->dst_mac_addr_mask[1] << 16) & 0xFF0000) |
  2386. ((attrib->dst_mac_addr_mask[0] << 24) & 0xFF000000);
  2387. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2388. (attrib->dst_mac_addr[3] & 0xFF) |
  2389. ((attrib->dst_mac_addr[2] << 8) & 0xFF00) |
  2390. ((attrib->dst_mac_addr[1] << 16) & 0xFF0000) |
  2391. ((attrib->dst_mac_addr[0] << 24) & 0xFF000000);
  2392. /* populate the second ihl meq 32 eq */
  2393. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32 + 1].offset = 12;
  2394. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32 + 1].mask =
  2395. ((attrib->dst_mac_addr_mask[5] << 16) & 0xFF0000) |
  2396. ((attrib->dst_mac_addr_mask[4] << 24) & 0xFF000000);
  2397. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32 + 1].value =
  2398. ((attrib->dst_mac_addr[5] << 16) & 0xFF0000) |
  2399. ((attrib->dst_mac_addr[4] << 24) & 0xFF000000);
  2400. ihl_ofst_meq32 += 2;
  2401. }
  2402. if (attrib->attrib_mask & IPA_FLT_TCP_SYN) {
  2403. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2404. ihl_ofst_meq32)) {
  2405. IPAHAL_ERR_RL("ran out of ihl_meq32 eq\n");
  2406. return -EPERM;
  2407. }
  2408. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2409. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2410. /* 12 => offset of SYN after v4 header */
  2411. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 12;
  2412. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask = 0x20000;
  2413. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value = 0x20000;
  2414. ihl_ofst_meq32++;
  2415. }
  2416. if (attrib->attrib_mask & IPA_FLT_TCP_SYN_L2TP) {
  2417. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2418. ihl_ofst_meq32) || IPA_IS_RAN_OUT_OF_EQ(
  2419. ipa3_0_ihl_ofst_meq32, ihl_ofst_meq32 + 1)) {
  2420. IPAHAL_ERR_RL("ran out of ihl_meq32 eq\n");
  2421. return -EPERM;
  2422. }
  2423. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2424. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2425. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2426. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32 + 1]);
  2427. /* populate TCP protocol eq */
  2428. if (attrib->ether_type == 0x0800) {
  2429. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 30;
  2430. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2431. 0xFF0000;
  2432. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2433. 0x60000;
  2434. } else {
  2435. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 26;
  2436. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2437. 0xFF00;
  2438. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2439. 0x600;
  2440. }
  2441. /* populate TCP SYN eq */
  2442. if (attrib->ether_type == 0x0800) {
  2443. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 54;
  2444. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2445. 0x20000;
  2446. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2447. 0x20000;
  2448. } else {
  2449. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 74;
  2450. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2451. 0x20000;
  2452. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2453. 0x20000;
  2454. }
  2455. ihl_ofst_meq32 += 2;
  2456. }
  2457. if (attrib->attrib_mask & IPA_FLT_L2TP_INNER_IP_TYPE) {
  2458. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2459. ihl_ofst_meq32)) {
  2460. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  2461. return -EPERM;
  2462. }
  2463. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2464. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2465. /* 22 => offset of inner IP type after v6 header */
  2466. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 22;
  2467. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2468. 0xF0000000;
  2469. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2470. (u32)attrib->type << 24;
  2471. ihl_ofst_meq32++;
  2472. }
  2473. if (attrib->attrib_mask & IPA_FLT_L2TP_INNER_IPV4_DST_ADDR) {
  2474. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2475. ihl_ofst_meq32)) {
  2476. IPAHAL_ERR("ran out of ihl_meq32 eq\n");
  2477. return -EPERM;
  2478. }
  2479. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2480. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2481. /* 38 => offset of inner IPv4 addr */
  2482. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 38;
  2483. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2484. attrib->u.v4.dst_addr_mask;
  2485. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2486. attrib->u.v4.dst_addr;
  2487. ihl_ofst_meq32++;
  2488. }
  2489. if (attrib->attrib_mask & IPA_FLT_MAC_ETHER_TYPE) {
  2490. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ofst_meq32, ofst_meq32)) {
  2491. IPAHAL_ERR_RL("ran out of meq32 eq\n");
  2492. return -EPERM;
  2493. }
  2494. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2495. ipa3_0_ofst_meq32[ofst_meq32]);
  2496. eq_atrb->offset_meq_32[ofst_meq32].offset = -2;
  2497. eq_atrb->offset_meq_32[ofst_meq32].mask =
  2498. htons(attrib->ether_type);
  2499. eq_atrb->offset_meq_32[ofst_meq32].value =
  2500. htons(attrib->ether_type);
  2501. ofst_meq32++;
  2502. }
  2503. if (attrib->attrib_mask & IPA_FLT_TYPE) {
  2504. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2505. ihl_ofst_meq32)) {
  2506. IPAHAL_ERR_RL("ran out of ihl_meq32 eq\n");
  2507. return -EPERM;
  2508. }
  2509. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2510. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2511. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 0;
  2512. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask = 0xFF;
  2513. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2514. attrib->type;
  2515. ihl_ofst_meq32++;
  2516. }
  2517. if (attrib->attrib_mask & IPA_FLT_CODE) {
  2518. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2519. ihl_ofst_meq32)) {
  2520. IPAHAL_ERR_RL("ran out of ihl_meq32 eq\n");
  2521. return -EPERM;
  2522. }
  2523. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2524. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2525. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 1;
  2526. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask = 0xFF;
  2527. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2528. attrib->code;
  2529. ihl_ofst_meq32++;
  2530. }
  2531. if (attrib->attrib_mask & IPA_FLT_SPI) {
  2532. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_meq32,
  2533. ihl_ofst_meq32)) {
  2534. IPAHAL_ERR_RL("ran out of ihl_meq32 eq\n");
  2535. return -EPERM;
  2536. }
  2537. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2538. ipa3_0_ihl_ofst_meq32[ihl_ofst_meq32]);
  2539. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].offset = 0;
  2540. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].mask =
  2541. 0xFFFFFFFF;
  2542. eq_atrb->ihl_offset_meq_32[ihl_ofst_meq32].value =
  2543. attrib->spi;
  2544. ihl_ofst_meq32++;
  2545. }
  2546. if (attrib->attrib_mask & IPA_FLT_META_DATA) {
  2547. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2548. IPA_METADATA_COMPARE);
  2549. eq_atrb->metadata_meq32_present = 1;
  2550. eq_atrb->metadata_meq32.offset = 0;
  2551. eq_atrb->metadata_meq32.mask = attrib->meta_data_mask;
  2552. eq_atrb->metadata_meq32.value = attrib->meta_data;
  2553. }
  2554. if (attrib->attrib_mask & IPA_FLT_SRC_PORT) {
  2555. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2556. ihl_ofst_rng16)) {
  2557. IPAHAL_ERR_RL("ran out of ihl_rng16 eq\n");
  2558. return -EPERM;
  2559. }
  2560. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2561. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2562. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset = 0;
  2563. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2564. = attrib->src_port;
  2565. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2566. = attrib->src_port;
  2567. ihl_ofst_rng16++;
  2568. }
  2569. if (attrib->attrib_mask & IPA_FLT_DST_PORT) {
  2570. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2571. ihl_ofst_rng16)) {
  2572. IPAHAL_ERR_RL("ran out of ihl_rng16 eq\n");
  2573. return -EPERM;
  2574. }
  2575. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2576. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2577. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset = 2;
  2578. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2579. = attrib->dst_port;
  2580. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2581. = attrib->dst_port;
  2582. ihl_ofst_rng16++;
  2583. }
  2584. if (attrib->attrib_mask & IPA_FLT_SRC_PORT_RANGE) {
  2585. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2586. ihl_ofst_rng16)) {
  2587. IPAHAL_ERR_RL("ran out of ihl_rng16 eq\n");
  2588. return -EPERM;
  2589. }
  2590. if (attrib->src_port_hi < attrib->src_port_lo) {
  2591. IPAHAL_ERR_RL("bad src port range param\n");
  2592. return -EPERM;
  2593. }
  2594. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2595. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2596. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset = 0;
  2597. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2598. = attrib->src_port_lo;
  2599. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2600. = attrib->src_port_hi;
  2601. ihl_ofst_rng16++;
  2602. }
  2603. if (attrib->attrib_mask & IPA_FLT_DST_PORT_RANGE) {
  2604. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2605. ihl_ofst_rng16)) {
  2606. IPAHAL_ERR_RL("ran out of ihl_rng16 eq\n");
  2607. return -EPERM;
  2608. }
  2609. if (attrib->dst_port_hi < attrib->dst_port_lo) {
  2610. IPAHAL_ERR_RL("bad dst port range param\n");
  2611. return -EPERM;
  2612. }
  2613. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2614. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2615. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset = 2;
  2616. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2617. = attrib->dst_port_lo;
  2618. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2619. = attrib->dst_port_hi;
  2620. ihl_ofst_rng16++;
  2621. }
  2622. if (attrib->attrib_mask & IPA_FLT_TCP_SYN_L2TP) {
  2623. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2624. ihl_ofst_rng16)) {
  2625. IPAHAL_ERR("ran out of ihl_rng16 eq\n");
  2626. return -EPERM;
  2627. }
  2628. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2629. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2630. if (attrib->ether_type == 0x0800) {
  2631. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset
  2632. = 21;
  2633. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2634. = 0x0045;
  2635. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2636. = 0x0045;
  2637. } else {
  2638. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset =
  2639. 20;
  2640. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2641. = attrib->ether_type;
  2642. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2643. = attrib->ether_type;
  2644. }
  2645. ihl_ofst_rng16++;
  2646. }
  2647. if (attrib->attrib_mask & IPA_FLT_TCP_SYN_L2TP) {
  2648. if (IPA_IS_RAN_OUT_OF_EQ(ipa3_0_ihl_ofst_rng16,
  2649. ihl_ofst_rng16)) {
  2650. IPAHAL_ERR_RL("ran out of ihl_rng16 eq\n");
  2651. return -EPERM;
  2652. }
  2653. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2654. ipa3_0_ihl_ofst_rng16[ihl_ofst_rng16]);
  2655. if (attrib->ether_type == 0x0800) {
  2656. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset
  2657. = 21;
  2658. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2659. = 0x0045;
  2660. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2661. = 0x0045;
  2662. } else {
  2663. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].offset =
  2664. 20;
  2665. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_low
  2666. = attrib->ether_type;
  2667. eq_atrb->ihl_offset_range_16[ihl_ofst_rng16].range_high
  2668. = attrib->ether_type;
  2669. }
  2670. ihl_ofst_rng16++;
  2671. }
  2672. if (attrib->attrib_mask & IPA_FLT_FLOW_LABEL) {
  2673. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(IPA_FL_EQ);
  2674. eq_atrb->fl_eq_present = 1;
  2675. eq_atrb->fl_eq = attrib->u.v6.flow_label;
  2676. }
  2677. if (attrib->attrib_mask & IPA_FLT_FRAGMENT) {
  2678. *en_rule |= IPA_GET_RULE_EQ_BIT_PTRN(
  2679. IPA_IS_FRAG);
  2680. eq_atrb->ipv4_frag_eq_present = 1;
  2681. }
  2682. eq_atrb->rule_eq_bitmap = *en_rule;
  2683. eq_atrb->num_offset_meq_32 = ofst_meq32;
  2684. eq_atrb->num_ihl_offset_range_16 = ihl_ofst_rng16;
  2685. eq_atrb->num_ihl_offset_meq_32 = ihl_ofst_meq32;
  2686. eq_atrb->num_offset_meq_128 = ofst_meq128;
  2687. return 0;
  2688. }
  2689. static int ipa_fltrt_parse_hw_rule_eq(u8 *addr, u32 hdr_sz,
  2690. struct ipa_ipfltri_rule_eq *atrb, u32 *rule_size)
  2691. {
  2692. u16 eq_bitmap;
  2693. int extra_bytes;
  2694. u8 *extra;
  2695. u8 *rest;
  2696. int i;
  2697. u8 dummy_extra_wrd;
  2698. if (!addr || !atrb || !rule_size) {
  2699. IPAHAL_ERR("Input error: addr=%pK atrb=%pK rule_size=%pK\n",
  2700. addr, atrb, rule_size);
  2701. return -EINVAL;
  2702. }
  2703. eq_bitmap = atrb->rule_eq_bitmap;
  2704. IPAHAL_DBG_LOW("eq_bitmap=0x%x\n", eq_bitmap);
  2705. if (IPA_IS_RULE_EQ_VALID(IPA_IS_PURE_ACK) &&
  2706. (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_PURE_ACK))) {
  2707. /*
  2708. * tos_eq_present field represents pure_ack when pure
  2709. * ack equation valid (started IPA 4.5). In this case
  2710. * tos equation should not be supported.
  2711. */
  2712. atrb->tos_eq_present = true;
  2713. }
  2714. if (IPA_IS_RULE_EQ_VALID(IPA_TOS_EQ) &&
  2715. (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_TOS_EQ))) {
  2716. atrb->tos_eq_present = true;
  2717. }
  2718. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_PROTOCOL_EQ))
  2719. atrb->protocol_eq_present = true;
  2720. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_TC_EQ))
  2721. atrb->tc_eq_present = true;
  2722. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_OFFSET_MEQ128_0))
  2723. atrb->num_offset_meq_128++;
  2724. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_OFFSET_MEQ128_1))
  2725. atrb->num_offset_meq_128++;
  2726. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_OFFSET_MEQ32_0))
  2727. atrb->num_offset_meq_32++;
  2728. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_OFFSET_MEQ32_1))
  2729. atrb->num_offset_meq_32++;
  2730. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_IHL_OFFSET_MEQ32_0))
  2731. atrb->num_ihl_offset_meq_32++;
  2732. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_IHL_OFFSET_MEQ32_1))
  2733. atrb->num_ihl_offset_meq_32++;
  2734. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_METADATA_COMPARE))
  2735. atrb->metadata_meq32_present = true;
  2736. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_IHL_OFFSET_RANGE16_0))
  2737. atrb->num_ihl_offset_range_16++;
  2738. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_IHL_OFFSET_RANGE16_1))
  2739. atrb->num_ihl_offset_range_16++;
  2740. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_IHL_OFFSET_EQ_32))
  2741. atrb->ihl_offset_eq_32_present = true;
  2742. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_IHL_OFFSET_EQ_16))
  2743. atrb->ihl_offset_eq_16_present = true;
  2744. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_FL_EQ))
  2745. atrb->fl_eq_present = true;
  2746. if (eq_bitmap & IPA_GET_RULE_EQ_BIT_PTRN(IPA_IS_FRAG))
  2747. atrb->ipv4_frag_eq_present = true;
  2748. extra_bytes = ipa_fltrt_calc_extra_wrd_bytes(atrb);
  2749. /* only 3 eq does not have extra word param, 13 out of 16 is the number
  2750. * of equations that needs extra word param
  2751. */
  2752. if (extra_bytes > 13) {
  2753. IPAHAL_ERR("too much extra bytes\n");
  2754. return -EPERM;
  2755. } else if (extra_bytes > IPA3_0_HW_TBL_HDR_WIDTH) {
  2756. /* two extra words */
  2757. extra = addr + hdr_sz;
  2758. rest = extra + IPA3_0_HW_TBL_HDR_WIDTH * 2;
  2759. } else if (extra_bytes > 0) {
  2760. /* single extra word */
  2761. extra = addr + hdr_sz;
  2762. rest = extra + IPA3_0_HW_TBL_HDR_WIDTH;
  2763. } else {
  2764. /* no extra words */
  2765. dummy_extra_wrd = 0;
  2766. extra = &dummy_extra_wrd;
  2767. rest = addr + hdr_sz;
  2768. }
  2769. IPAHAL_DBG_LOW("addr=0x%pK extra=0x%pK rest=0x%pK\n",
  2770. addr, extra, rest);
  2771. if (IPA_IS_RULE_EQ_VALID(IPA_TOS_EQ) && atrb->tos_eq_present)
  2772. atrb->tos_eq = *extra++;
  2773. if (IPA_IS_RULE_EQ_VALID(IPA_IS_PURE_ACK) && atrb->tos_eq_present) {
  2774. atrb->tos_eq = 0;
  2775. extra++;
  2776. }
  2777. if (atrb->protocol_eq_present)
  2778. atrb->protocol_eq = *extra++;
  2779. if (atrb->tc_eq_present)
  2780. atrb->tc_eq = *extra++;
  2781. if (atrb->num_offset_meq_128 > 0) {
  2782. atrb->offset_meq_128[0].offset = *extra++;
  2783. for (i = 0; i < 8; i++)
  2784. atrb->offset_meq_128[0].mask[i] = *rest++;
  2785. for (i = 0; i < 8; i++)
  2786. atrb->offset_meq_128[0].value[i] = *rest++;
  2787. for (i = 8; i < 16; i++)
  2788. atrb->offset_meq_128[0].mask[i] = *rest++;
  2789. for (i = 8; i < 16; i++)
  2790. atrb->offset_meq_128[0].value[i] = *rest++;
  2791. }
  2792. if (atrb->num_offset_meq_128 > 1) {
  2793. atrb->offset_meq_128[1].offset = *extra++;
  2794. for (i = 0; i < 8; i++)
  2795. atrb->offset_meq_128[1].mask[i] = *rest++;
  2796. for (i = 0; i < 8; i++)
  2797. atrb->offset_meq_128[1].value[i] = *rest++;
  2798. for (i = 8; i < 16; i++)
  2799. atrb->offset_meq_128[1].mask[i] = *rest++;
  2800. for (i = 8; i < 16; i++)
  2801. atrb->offset_meq_128[1].value[i] = *rest++;
  2802. }
  2803. if (atrb->num_offset_meq_32 > 0) {
  2804. atrb->offset_meq_32[0].offset = *extra++;
  2805. atrb->offset_meq_32[0].mask = *((u32 *)rest);
  2806. rest += 4;
  2807. atrb->offset_meq_32[0].value = *((u32 *)rest);
  2808. rest += 4;
  2809. }
  2810. if (atrb->num_offset_meq_32 > 1) {
  2811. atrb->offset_meq_32[1].offset = *extra++;
  2812. atrb->offset_meq_32[1].mask = *((u32 *)rest);
  2813. rest += 4;
  2814. atrb->offset_meq_32[1].value = *((u32 *)rest);
  2815. rest += 4;
  2816. }
  2817. if (atrb->num_ihl_offset_meq_32 > 0) {
  2818. atrb->ihl_offset_meq_32[0].offset = *extra++;
  2819. atrb->ihl_offset_meq_32[0].mask = *((u32 *)rest);
  2820. rest += 4;
  2821. atrb->ihl_offset_meq_32[0].value = *((u32 *)rest);
  2822. rest += 4;
  2823. }
  2824. if (atrb->num_ihl_offset_meq_32 > 1) {
  2825. atrb->ihl_offset_meq_32[1].offset = *extra++;
  2826. atrb->ihl_offset_meq_32[1].mask = *((u32 *)rest);
  2827. rest += 4;
  2828. atrb->ihl_offset_meq_32[1].value = *((u32 *)rest);
  2829. rest += 4;
  2830. }
  2831. if (atrb->metadata_meq32_present) {
  2832. atrb->metadata_meq32.mask = *((u32 *)rest);
  2833. rest += 4;
  2834. atrb->metadata_meq32.value = *((u32 *)rest);
  2835. rest += 4;
  2836. }
  2837. if (atrb->num_ihl_offset_range_16 > 0) {
  2838. atrb->ihl_offset_range_16[0].offset = *extra++;
  2839. atrb->ihl_offset_range_16[0].range_high = *((u16 *)rest);
  2840. rest += 2;
  2841. atrb->ihl_offset_range_16[0].range_low = *((u16 *)rest);
  2842. rest += 2;
  2843. }
  2844. if (atrb->num_ihl_offset_range_16 > 1) {
  2845. atrb->ihl_offset_range_16[1].offset = *extra++;
  2846. atrb->ihl_offset_range_16[1].range_high = *((u16 *)rest);
  2847. rest += 2;
  2848. atrb->ihl_offset_range_16[1].range_low = *((u16 *)rest);
  2849. rest += 2;
  2850. }
  2851. if (atrb->ihl_offset_eq_32_present) {
  2852. atrb->ihl_offset_eq_32.offset = *extra++;
  2853. atrb->ihl_offset_eq_32.value = *((u32 *)rest);
  2854. rest += 4;
  2855. }
  2856. if (atrb->ihl_offset_eq_16_present) {
  2857. atrb->ihl_offset_eq_16.offset = *extra++;
  2858. atrb->ihl_offset_eq_16.value = *((u16 *)rest);
  2859. rest += 4;
  2860. }
  2861. if (atrb->fl_eq_present) {
  2862. atrb->fl_eq = *((u32 *)rest);
  2863. atrb->fl_eq &= 0xfffff;
  2864. rest += 4;
  2865. }
  2866. IPAHAL_DBG_LOW("before rule alignment rest=0x%pK\n", rest);
  2867. rest = (u8 *)(((unsigned long)rest + IPA3_0_HW_RULE_START_ALIGNMENT) &
  2868. ~IPA3_0_HW_RULE_START_ALIGNMENT);
  2869. IPAHAL_DBG_LOW("after rule alignment rest=0x%pK\n", rest);
  2870. *rule_size = rest - addr;
  2871. IPAHAL_DBG_LOW("rule_size=0x%x\n", *rule_size);
  2872. return 0;
  2873. }
  2874. static int ipa_rt_parse_hw_rule(u8 *addr, struct ipahal_rt_rule_entry *rule)
  2875. {
  2876. struct ipa3_0_rt_rule_hw_hdr *rule_hdr;
  2877. struct ipa_ipfltri_rule_eq *atrb;
  2878. IPAHAL_DBG_LOW("Entry\n");
  2879. rule_hdr = (struct ipa3_0_rt_rule_hw_hdr *)addr;
  2880. atrb = &rule->eq_attrib;
  2881. IPAHAL_DBG_LOW("read hdr 0x%llx\n", rule_hdr->u.word);
  2882. if (rule_hdr->u.word == 0) {
  2883. /* table terminator - empty table */
  2884. rule->rule_size = 0;
  2885. return 0;
  2886. }
  2887. rule->dst_pipe_idx = rule_hdr->u.hdr.pipe_dest_idx;
  2888. if (rule_hdr->u.hdr.proc_ctx) {
  2889. rule->hdr_type = IPAHAL_RT_RULE_HDR_PROC_CTX;
  2890. rule->hdr_ofst = (rule_hdr->u.hdr.hdr_offset) << 5;
  2891. } else {
  2892. rule->hdr_type = IPAHAL_RT_RULE_HDR_RAW;
  2893. rule->hdr_ofst = (rule_hdr->u.hdr.hdr_offset) << 2;
  2894. }
  2895. rule->hdr_lcl = !rule_hdr->u.hdr.system;
  2896. rule->priority = rule_hdr->u.hdr.priority;
  2897. rule->retain_hdr = rule_hdr->u.hdr.retain_hdr;
  2898. rule->id = rule_hdr->u.hdr.rule_id;
  2899. atrb->rule_eq_bitmap = rule_hdr->u.hdr.en_rule;
  2900. return ipa_fltrt_parse_hw_rule_eq(addr, sizeof(*rule_hdr),
  2901. atrb, &rule->rule_size);
  2902. }
  2903. static int ipa_rt_parse_hw_rule_ipav4_5(u8 *addr,
  2904. struct ipahal_rt_rule_entry *rule)
  2905. {
  2906. struct ipa4_5_rt_rule_hw_hdr *rule_hdr;
  2907. struct ipa_ipfltri_rule_eq *atrb;
  2908. IPAHAL_DBG_LOW("Entry\n");
  2909. rule_hdr = (struct ipa4_5_rt_rule_hw_hdr *)addr;
  2910. atrb = &rule->eq_attrib;
  2911. IPAHAL_DBG_LOW("read hdr 0x%llx\n", rule_hdr->u.word);
  2912. if (rule_hdr->u.word == 0) {
  2913. /* table termintator - empty table */
  2914. rule->rule_size = 0;
  2915. return 0;
  2916. }
  2917. rule->dst_pipe_idx = rule_hdr->u.hdr.pipe_dest_idx;
  2918. if (rule_hdr->u.hdr.proc_ctx) {
  2919. rule->hdr_type = IPAHAL_RT_RULE_HDR_PROC_CTX;
  2920. rule->hdr_ofst = (rule_hdr->u.hdr.hdr_offset) << 5;
  2921. } else {
  2922. rule->hdr_type = IPAHAL_RT_RULE_HDR_RAW;
  2923. rule->hdr_ofst = (rule_hdr->u.hdr.hdr_offset) << 2;
  2924. }
  2925. rule->hdr_lcl = !rule_hdr->u.hdr.system;
  2926. rule->priority = rule_hdr->u.hdr.priority;
  2927. rule->retain_hdr = rule_hdr->u.hdr.retain_hdr;
  2928. rule->cnt_idx = rule_hdr->u.hdr.stats_cnt_idx_lsb |
  2929. (rule_hdr->u.hdr.stats_cnt_idx_msb) << 6;
  2930. rule->id = rule_hdr->u.hdr.rule_id;
  2931. atrb->rule_eq_bitmap = rule_hdr->u.hdr.en_rule;
  2932. return ipa_fltrt_parse_hw_rule_eq(addr, sizeof(*rule_hdr),
  2933. atrb, &rule->rule_size);
  2934. }
  2935. static int ipa_flt_parse_hw_rule(u8 *addr, struct ipahal_flt_rule_entry *rule)
  2936. {
  2937. struct ipa3_0_flt_rule_hw_hdr *rule_hdr;
  2938. struct ipa_ipfltri_rule_eq *atrb;
  2939. IPAHAL_DBG_LOW("Entry\n");
  2940. rule_hdr = (struct ipa3_0_flt_rule_hw_hdr *)addr;
  2941. atrb = &rule->rule.eq_attrib;
  2942. if (rule_hdr->u.word == 0) {
  2943. /* table termintator - empty table */
  2944. rule->rule_size = 0;
  2945. return 0;
  2946. }
  2947. switch (rule_hdr->u.hdr.action) {
  2948. case 0x0:
  2949. rule->rule.action = IPA_PASS_TO_ROUTING;
  2950. break;
  2951. case 0x1:
  2952. rule->rule.action = IPA_PASS_TO_SRC_NAT;
  2953. break;
  2954. case 0x2:
  2955. rule->rule.action = IPA_PASS_TO_DST_NAT;
  2956. break;
  2957. case 0x3:
  2958. rule->rule.action = IPA_PASS_TO_EXCEPTION;
  2959. break;
  2960. default:
  2961. IPAHAL_ERR("Invalid Rule Action %d\n", rule_hdr->u.hdr.action);
  2962. WARN_ON_RATELIMIT_IPA(1);
  2963. rule->rule.action = rule_hdr->u.hdr.action;
  2964. }
  2965. rule->rule.rt_tbl_idx = rule_hdr->u.hdr.rt_tbl_idx;
  2966. rule->rule.retain_hdr = rule_hdr->u.hdr.retain_hdr;
  2967. rule->priority = rule_hdr->u.hdr.priority;
  2968. rule->id = rule_hdr->u.hdr.rule_id;
  2969. atrb->rule_eq_bitmap = rule_hdr->u.hdr.en_rule;
  2970. rule->rule.eq_attrib_type = 1;
  2971. return ipa_fltrt_parse_hw_rule_eq(addr, sizeof(*rule_hdr),
  2972. atrb, &rule->rule_size);
  2973. }
  2974. static int ipa_flt_parse_hw_rule_ipav4(u8 *addr,
  2975. struct ipahal_flt_rule_entry *rule)
  2976. {
  2977. struct ipa4_0_flt_rule_hw_hdr *rule_hdr;
  2978. struct ipa_ipfltri_rule_eq *atrb;
  2979. IPAHAL_DBG_LOW("Entry\n");
  2980. rule_hdr = (struct ipa4_0_flt_rule_hw_hdr *)addr;
  2981. atrb = &rule->rule.eq_attrib;
  2982. if (rule_hdr->u.word == 0) {
  2983. /* table termintator - empty table */
  2984. rule->rule_size = 0;
  2985. return 0;
  2986. }
  2987. switch (rule_hdr->u.hdr.action) {
  2988. case 0x0:
  2989. rule->rule.action = IPA_PASS_TO_ROUTING;
  2990. break;
  2991. case 0x1:
  2992. rule->rule.action = IPA_PASS_TO_SRC_NAT;
  2993. break;
  2994. case 0x2:
  2995. rule->rule.action = IPA_PASS_TO_DST_NAT;
  2996. break;
  2997. case 0x3:
  2998. rule->rule.action = IPA_PASS_TO_EXCEPTION;
  2999. break;
  3000. default:
  3001. IPAHAL_ERR("Invalid Rule Action %d\n", rule_hdr->u.hdr.action);
  3002. WARN_ON_RATELIMIT_IPA(1);
  3003. rule->rule.action = rule_hdr->u.hdr.action;
  3004. }
  3005. rule->rule.rt_tbl_idx = rule_hdr->u.hdr.rt_tbl_idx;
  3006. rule->rule.retain_hdr = rule_hdr->u.hdr.retain_hdr;
  3007. rule->priority = rule_hdr->u.hdr.priority;
  3008. rule->id = rule_hdr->u.hdr.rule_id;
  3009. rule->rule.pdn_idx = rule_hdr->u.hdr.pdn_idx;
  3010. rule->rule.set_metadata = rule_hdr->u.hdr.set_metadata;
  3011. atrb->rule_eq_bitmap = rule_hdr->u.hdr.en_rule;
  3012. rule->rule.eq_attrib_type = 1;
  3013. return ipa_fltrt_parse_hw_rule_eq(addr, sizeof(*rule_hdr),
  3014. atrb, &rule->rule_size);
  3015. }
  3016. static int ipa_flt_parse_hw_rule_ipav4_5(u8 *addr,
  3017. struct ipahal_flt_rule_entry *rule)
  3018. {
  3019. struct ipa4_5_flt_rule_hw_hdr *rule_hdr;
  3020. struct ipa_ipfltri_rule_eq *atrb;
  3021. IPAHAL_DBG_LOW("Entry\n");
  3022. rule_hdr = (struct ipa4_5_flt_rule_hw_hdr *)addr;
  3023. atrb = &rule->rule.eq_attrib;
  3024. if (rule_hdr->u.word == 0) {
  3025. /* table termintator - empty table */
  3026. rule->rule_size = 0;
  3027. return 0;
  3028. }
  3029. switch (rule_hdr->u.hdr.action) {
  3030. case 0x0:
  3031. rule->rule.action = IPA_PASS_TO_ROUTING;
  3032. break;
  3033. case 0x1:
  3034. rule->rule.action = IPA_PASS_TO_SRC_NAT;
  3035. break;
  3036. case 0x2:
  3037. rule->rule.action = IPA_PASS_TO_DST_NAT;
  3038. break;
  3039. case 0x3:
  3040. rule->rule.action = IPA_PASS_TO_EXCEPTION;
  3041. break;
  3042. default:
  3043. IPAHAL_ERR("Invalid Rule Action %d\n", rule_hdr->u.hdr.action);
  3044. WARN_ON_RATELIMIT_IPA(1);
  3045. rule->rule.action = rule_hdr->u.hdr.action;
  3046. }
  3047. rule->rule.rt_tbl_idx = rule_hdr->u.hdr.rt_tbl_idx;
  3048. rule->rule.retain_hdr = rule_hdr->u.hdr.retain_hdr;
  3049. rule->priority = rule_hdr->u.hdr.priority;
  3050. rule->id = rule_hdr->u.hdr.rule_id;
  3051. rule->rule.pdn_idx = rule_hdr->u.hdr.pdn_idx;
  3052. rule->rule.set_metadata = rule_hdr->u.hdr.set_metadata;
  3053. rule->cnt_idx = rule_hdr->u.hdr.stats_cnt_idx_lsb |
  3054. (rule_hdr->u.hdr.stats_cnt_idx_msb) << 6;
  3055. atrb->rule_eq_bitmap = rule_hdr->u.hdr.en_rule;
  3056. rule->rule.eq_attrib_type = 1;
  3057. return ipa_fltrt_parse_hw_rule_eq(addr, sizeof(*rule_hdr),
  3058. atrb, &rule->rule_size);
  3059. }
  3060. /*
  3061. * ipahal_fltrt_init() - Build the FLT/RT information table
  3062. * See ipahal_fltrt_objs[] comments
  3063. *
  3064. * Note: As global variables are initialized with zero, any un-overridden
  3065. * register entry will be zero. By this we recognize them.
  3066. */
  3067. int ipahal_fltrt_init(enum ipa_hw_type ipa_hw_type)
  3068. {
  3069. struct ipahal_fltrt_obj zero_obj;
  3070. int i;
  3071. struct ipa_mem_buffer *mem;
  3072. int rc = -EFAULT;
  3073. u32 eq_bits;
  3074. u8 *eq_bitfield;
  3075. IPAHAL_DBG("Entry - HW_TYPE=%d\n", ipa_hw_type);
  3076. if (ipa_hw_type >= IPA_HW_MAX) {
  3077. IPAHAL_ERR("Invalid H/W type\n");
  3078. return -EFAULT;
  3079. }
  3080. memset(&zero_obj, 0, sizeof(zero_obj));
  3081. for (i = IPA_HW_v3_0 ; i < ipa_hw_type ; i++) {
  3082. if (!memcmp(&ipahal_fltrt_objs[i+1], &zero_obj,
  3083. sizeof(struct ipahal_fltrt_obj))) {
  3084. memcpy(&ipahal_fltrt_objs[i+1],
  3085. &ipahal_fltrt_objs[i],
  3086. sizeof(struct ipahal_fltrt_obj));
  3087. } else {
  3088. /*
  3089. * explicitly overridden FLT RT info
  3090. * Check validity
  3091. */
  3092. if (!ipahal_fltrt_objs[i+1].tbl_width) {
  3093. IPAHAL_ERR(
  3094. "Zero tbl width ipaver=%d\n",
  3095. i+1);
  3096. WARN_ON(1);
  3097. }
  3098. if (!ipahal_fltrt_objs[i+1].sysaddr_alignment) {
  3099. IPAHAL_ERR(
  3100. "No tbl sysaddr alignment ipaver=%d\n",
  3101. i+1);
  3102. WARN_ON(1);
  3103. }
  3104. if (!ipahal_fltrt_objs[i+1].lcladdr_alignment) {
  3105. IPAHAL_ERR(
  3106. "No tbl lcladdr alignment ipaver=%d\n",
  3107. i+1);
  3108. WARN_ON(1);
  3109. }
  3110. if (!ipahal_fltrt_objs[i+1].blk_sz_alignment) {
  3111. IPAHAL_ERR(
  3112. "No blk sz alignment ipaver=%d\n",
  3113. i+1);
  3114. WARN_ON(1);
  3115. }
  3116. if (!ipahal_fltrt_objs[i+1].rule_start_alignment) {
  3117. IPAHAL_ERR(
  3118. "No rule start alignment ipaver=%d\n",
  3119. i+1);
  3120. WARN_ON(1);
  3121. }
  3122. if (!ipahal_fltrt_objs[i+1].tbl_hdr_width) {
  3123. IPAHAL_ERR(
  3124. "Zero tbl hdr width ipaver=%d\n",
  3125. i+1);
  3126. WARN_ON(1);
  3127. }
  3128. if (!ipahal_fltrt_objs[i+1].tbl_addr_mask) {
  3129. IPAHAL_ERR(
  3130. "Zero tbl hdr width ipaver=%d\n",
  3131. i+1);
  3132. WARN_ON(1);
  3133. }
  3134. if (ipahal_fltrt_objs[i+1].rule_id_bit_len < 2) {
  3135. IPAHAL_ERR(
  3136. "Too little bits for rule_id ipaver=%d\n",
  3137. i+1);
  3138. WARN_ON(1);
  3139. }
  3140. if (!ipahal_fltrt_objs[i+1].rule_buf_size) {
  3141. IPAHAL_ERR(
  3142. "zero rule buf size ipaver=%d\n",
  3143. i+1);
  3144. WARN_ON(1);
  3145. }
  3146. if (!ipahal_fltrt_objs[i+1].write_val_to_hdr) {
  3147. IPAHAL_ERR(
  3148. "No write_val_to_hdr CB ipaver=%d\n",
  3149. i+1);
  3150. WARN_ON(1);
  3151. }
  3152. if (!ipahal_fltrt_objs[i+1].create_flt_bitmap) {
  3153. IPAHAL_ERR(
  3154. "No create_flt_bitmap CB ipaver=%d\n",
  3155. i+1);
  3156. WARN_ON(1);
  3157. }
  3158. if (!ipahal_fltrt_objs[i+1].create_tbl_addr) {
  3159. IPAHAL_ERR(
  3160. "No create_tbl_addr CB ipaver=%d\n",
  3161. i+1);
  3162. WARN_ON(1);
  3163. }
  3164. if (!ipahal_fltrt_objs[i+1].parse_tbl_addr) {
  3165. IPAHAL_ERR(
  3166. "No parse_tbl_addr CB ipaver=%d\n",
  3167. i+1);
  3168. WARN_ON(1);
  3169. }
  3170. if (!ipahal_fltrt_objs[i+1].rt_generate_hw_rule) {
  3171. IPAHAL_ERR(
  3172. "No rt_generate_hw_rule CB ipaver=%d\n",
  3173. i+1);
  3174. WARN_ON(1);
  3175. }
  3176. if (!ipahal_fltrt_objs[i+1].flt_generate_hw_rule) {
  3177. IPAHAL_ERR(
  3178. "No flt_generate_hw_rule CB ipaver=%d\n",
  3179. i+1);
  3180. WARN_ON(1);
  3181. }
  3182. if (!ipahal_fltrt_objs[i+1].flt_generate_eq) {
  3183. IPAHAL_ERR(
  3184. "No flt_generate_eq CB ipaver=%d\n",
  3185. i+1);
  3186. WARN_ON(1);
  3187. }
  3188. if (!ipahal_fltrt_objs[i+1].rt_parse_hw_rule) {
  3189. IPAHAL_ERR(
  3190. "No rt_parse_hw_rule CB ipaver=%d\n",
  3191. i+1);
  3192. WARN_ON(1);
  3193. }
  3194. if (!ipahal_fltrt_objs[i+1].flt_parse_hw_rule) {
  3195. IPAHAL_ERR(
  3196. "No flt_parse_hw_rule CB ipaver=%d\n",
  3197. i+1);
  3198. WARN_ON(1);
  3199. }
  3200. }
  3201. }
  3202. eq_bits = 0;
  3203. eq_bitfield = ipahal_fltrt_objs[ipa_hw_type].eq_bitfield;
  3204. for (i = 0; i < IPA_EQ_MAX; i++) {
  3205. if (!IPA_IS_RULE_EQ_VALID(i))
  3206. continue;
  3207. if (eq_bits & IPA_GET_RULE_EQ_BIT_PTRN(eq_bitfield[i])) {
  3208. IPAHAL_ERR("more than eq with same bit. eq=%d\n", i);
  3209. WARN_ON(1);
  3210. return -EFAULT;
  3211. }
  3212. eq_bits |= IPA_GET_RULE_EQ_BIT_PTRN(eq_bitfield[i]);
  3213. }
  3214. mem = &ipahal_ctx->empty_fltrt_tbl;
  3215. /* setup an empty table in system memory; This will
  3216. * be used, for example, to delete a rt tbl safely
  3217. */
  3218. mem->size = ipahal_fltrt_objs[ipa_hw_type].tbl_width;
  3219. mem->base = dma_alloc_coherent(ipahal_ctx->ipa_pdev, mem->size,
  3220. &mem->phys_base, GFP_KERNEL);
  3221. if (!mem->base) {
  3222. IPAHAL_ERR("DMA buff alloc fail %d bytes for empty tbl\n",
  3223. mem->size);
  3224. return -ENOMEM;
  3225. }
  3226. if (mem->phys_base &
  3227. ipahal_fltrt_objs[ipa_hw_type].sysaddr_alignment) {
  3228. IPAHAL_ERR("Empty table buf is not address aligned 0x%pad\n",
  3229. &mem->phys_base);
  3230. rc = -EFAULT;
  3231. goto clear_empty_tbl;
  3232. }
  3233. memset(mem->base, 0, mem->size);
  3234. IPAHAL_DBG("empty table allocated in system memory");
  3235. return 0;
  3236. clear_empty_tbl:
  3237. dma_free_coherent(ipahal_ctx->ipa_pdev, mem->size, mem->base,
  3238. mem->phys_base);
  3239. return rc;
  3240. }
  3241. void ipahal_fltrt_destroy(void)
  3242. {
  3243. IPAHAL_DBG("Entry\n");
  3244. if (ipahal_ctx && ipahal_ctx->empty_fltrt_tbl.base)
  3245. dma_free_coherent(ipahal_ctx->ipa_pdev,
  3246. ipahal_ctx->empty_fltrt_tbl.size,
  3247. ipahal_ctx->empty_fltrt_tbl.base,
  3248. ipahal_ctx->empty_fltrt_tbl.phys_base);
  3249. }
  3250. /* Get the H/W table (flt/rt) header width */
  3251. u32 ipahal_get_hw_tbl_hdr_width(void)
  3252. {
  3253. return ipahal_fltrt_objs[ipahal_ctx->hw_type].tbl_hdr_width;
  3254. }
  3255. /* Get the H/W local table (SRAM) address alignment
  3256. * Tables headers references to local tables via offsets in SRAM
  3257. * This function return the alignment of the offset that IPA expects
  3258. */
  3259. u32 ipahal_get_lcl_tbl_addr_alignment(void)
  3260. {
  3261. return ipahal_fltrt_objs[ipahal_ctx->hw_type].lcladdr_alignment;
  3262. }
  3263. /*
  3264. * Rule priority is used to distinguish rules order
  3265. * at the integrated table consisting from hashable and
  3266. * non-hashable tables. Max priority are rules that once are
  3267. * scanned by IPA, IPA will not look for further rules and use it.
  3268. */
  3269. int ipahal_get_rule_max_priority(void)
  3270. {
  3271. return ipahal_fltrt_objs[ipahal_ctx->hw_type].rule_max_prio;
  3272. }
  3273. /* Given a priority, calc and return the next lower one if it is in
  3274. * legal range.
  3275. */
  3276. int ipahal_rule_decrease_priority(int *prio)
  3277. {
  3278. struct ipahal_fltrt_obj *obj;
  3279. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3280. if (!prio) {
  3281. IPAHAL_ERR("Invalid Input\n");
  3282. return -EINVAL;
  3283. }
  3284. /* Priority logic is reverse. 0 priority considred max priority */
  3285. if (*prio > obj->rule_min_prio || *prio < obj->rule_max_prio) {
  3286. IPAHAL_ERR("Invalid given priority %d\n", *prio);
  3287. return -EINVAL;
  3288. }
  3289. *prio += 1;
  3290. if (*prio > obj->rule_min_prio) {
  3291. IPAHAL_ERR("Cannot decrease priority. Already on min\n");
  3292. *prio -= 1;
  3293. return -EFAULT;
  3294. }
  3295. return 0;
  3296. }
  3297. /* Does the given ID represents rule miss?
  3298. * Rule miss ID, is always the max ID possible in the bit-pattern
  3299. */
  3300. bool ipahal_is_rule_miss_id(u32 id)
  3301. {
  3302. return (id ==
  3303. ((1U << ipahal_fltrt_objs[ipahal_ctx->hw_type].rule_id_bit_len)
  3304. -1));
  3305. }
  3306. /* Get rule ID with high bit only asserted
  3307. * Used e.g. to create groups of IDs according to this bit
  3308. */
  3309. u32 ipahal_get_rule_id_hi_bit(void)
  3310. {
  3311. return BIT(ipahal_fltrt_objs[ipahal_ctx->hw_type].rule_id_bit_len - 1);
  3312. }
  3313. /* Get the low value possible to be used for rule-id */
  3314. u32 ipahal_get_low_rule_id(void)
  3315. {
  3316. return ipahal_fltrt_objs[ipahal_ctx->hw_type].low_rule_id;
  3317. }
  3318. /*
  3319. * Is the given counter id valid
  3320. */
  3321. bool ipahal_is_rule_cnt_id_valid(u8 cnt_id)
  3322. {
  3323. if (cnt_id < 0 || cnt_id > IPA_FLT_RT_HW_COUNTER)
  3324. return false;
  3325. return true;
  3326. }
  3327. /*
  3328. * low value possible for counter hdl id
  3329. */
  3330. u32 ipahal_get_low_hdl_id(void)
  3331. {
  3332. return IPA4_5_LOW_CNT_ID;
  3333. }
  3334. /*
  3335. * max counter hdl id for stats
  3336. */
  3337. u32 ipahal_get_high_hdl_id(void)
  3338. {
  3339. return IPA_MAX_FLT_RT_CNT_INDEX;
  3340. }
  3341. /*
  3342. * ipahal_rt_generate_empty_img() - Generate empty route image
  3343. * Creates routing header buffer for the given tables number.
  3344. * For each table, make it point to the empty table on DDR.
  3345. * @tbls_num: Number of tables. For each will have an entry in the header
  3346. * @hash_hdr_size: SRAM buf size of the hash tbls hdr. Used for space check
  3347. * @nhash_hdr_size: SRAM buf size of the nhash tbls hdr. Used for space check
  3348. * @mem: mem object that points to DMA mem representing the hdr structure
  3349. * @atomic: should DMA allocation be executed with atomic flag
  3350. */
  3351. int ipahal_rt_generate_empty_img(u32 tbls_num, u32 hash_hdr_size,
  3352. u32 nhash_hdr_size, struct ipa_mem_buffer *mem, bool atomic)
  3353. {
  3354. int i;
  3355. u64 addr;
  3356. struct ipahal_fltrt_obj *obj;
  3357. int flag;
  3358. IPAHAL_DBG("Entry\n");
  3359. flag = atomic ? GFP_ATOMIC : GFP_KERNEL;
  3360. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3361. if (!tbls_num || !nhash_hdr_size || !mem) {
  3362. IPAHAL_ERR("Input Error: tbls_num=%d nhash_hdr_sz=%d mem=%pK\n",
  3363. tbls_num, nhash_hdr_size, mem);
  3364. return -EINVAL;
  3365. }
  3366. if (obj->support_hash && !hash_hdr_size) {
  3367. IPAHAL_ERR("Input Error: hash_hdr_sz=%d\n", hash_hdr_size);
  3368. return -EINVAL;
  3369. }
  3370. if (nhash_hdr_size < (tbls_num * obj->tbl_hdr_width)) {
  3371. IPAHAL_ERR("No enough spc at non-hash hdr blk for all tbls\n");
  3372. WARN_ON(1);
  3373. return -EINVAL;
  3374. }
  3375. if (obj->support_hash &&
  3376. (hash_hdr_size < (tbls_num * obj->tbl_hdr_width))) {
  3377. IPAHAL_ERR("No enough spc at hash hdr blk for all tbls\n");
  3378. WARN_ON(1);
  3379. return -EINVAL;
  3380. }
  3381. mem->size = tbls_num * obj->tbl_hdr_width;
  3382. mem->base = dma_alloc_coherent(ipahal_ctx->ipa_pdev, mem->size,
  3383. &mem->phys_base, flag);
  3384. if (!mem->base) {
  3385. IPAHAL_ERR("fail to alloc DMA buff of size %d\n", mem->size);
  3386. return -ENOMEM;
  3387. }
  3388. addr = obj->create_tbl_addr(true,
  3389. ipahal_ctx->empty_fltrt_tbl.phys_base);
  3390. for (i = 0; i < tbls_num; i++)
  3391. obj->write_val_to_hdr(addr,
  3392. mem->base + i * obj->tbl_hdr_width);
  3393. return 0;
  3394. }
  3395. /*
  3396. * ipahal_flt_generate_empty_img() - Generate empty filter image
  3397. * Creates filter header buffer for the given tables number.
  3398. * For each table, make it point to the empty table on DDR.
  3399. * @tbls_num: Number of tables. For each will have an entry in the header
  3400. * @hash_hdr_size: SRAM buf size of the hash tbls hdr. Used for space check
  3401. * @nhash_hdr_size: SRAM buf size of the nhash tbls hdr. Used for space check
  3402. * @ep_bitmap: Bitmap representing the EP that has flt tables. The format
  3403. * should be: bit0->EP0, bit1->EP1
  3404. * If bitmap is zero -> create tbl without bitmap entry
  3405. * @mem: mem object that points to DMA mem representing the hdr structure
  3406. * @atomic: should DMA allocation be executed with atomic flag
  3407. */
  3408. int ipahal_flt_generate_empty_img(u32 tbls_num, u32 hash_hdr_size,
  3409. u32 nhash_hdr_size, u64 ep_bitmap, struct ipa_mem_buffer *mem,
  3410. bool atomic)
  3411. {
  3412. int flt_spc;
  3413. u64 flt_bitmap;
  3414. int i;
  3415. u64 addr;
  3416. struct ipahal_fltrt_obj *obj;
  3417. int flag;
  3418. IPAHAL_DBG("Entry - ep_bitmap 0x%llx\n", ep_bitmap);
  3419. flag = atomic ? GFP_ATOMIC : GFP_KERNEL;
  3420. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3421. if (!tbls_num || !nhash_hdr_size || !mem) {
  3422. IPAHAL_ERR("Input Error: tbls_num=%d nhash_hdr_sz=%d mem=%pK\n",
  3423. tbls_num, nhash_hdr_size, mem);
  3424. return -EINVAL;
  3425. }
  3426. if (obj->support_hash && !hash_hdr_size) {
  3427. IPAHAL_ERR("Input Error: hash_hdr_sz=%d\n", hash_hdr_size);
  3428. return -EINVAL;
  3429. }
  3430. if (obj->support_hash) {
  3431. flt_spc = hash_hdr_size;
  3432. /* bitmap word */
  3433. if (ep_bitmap)
  3434. flt_spc -= obj->tbl_hdr_width;
  3435. flt_spc /= obj->tbl_hdr_width;
  3436. if (tbls_num > flt_spc) {
  3437. IPAHAL_ERR("space for hash flt hdr is too small\n");
  3438. WARN_ON(1);
  3439. return -EPERM;
  3440. }
  3441. }
  3442. flt_spc = nhash_hdr_size;
  3443. /* bitmap word */
  3444. if (ep_bitmap)
  3445. flt_spc -= obj->tbl_hdr_width;
  3446. flt_spc /= obj->tbl_hdr_width;
  3447. if (tbls_num > flt_spc) {
  3448. IPAHAL_ERR("space for non-hash flt hdr is too small\n");
  3449. WARN_ON(1);
  3450. return -EPERM;
  3451. }
  3452. mem->size = tbls_num * obj->tbl_hdr_width;
  3453. if (ep_bitmap)
  3454. mem->size += obj->tbl_hdr_width;
  3455. mem->base = dma_alloc_coherent(ipahal_ctx->ipa_pdev, mem->size,
  3456. &mem->phys_base, flag);
  3457. if (!mem->base) {
  3458. IPAHAL_ERR("fail to alloc DMA buff of size %d\n", mem->size);
  3459. return -ENOMEM;
  3460. }
  3461. if (ep_bitmap) {
  3462. flt_bitmap = obj->create_flt_bitmap(ep_bitmap);
  3463. IPAHAL_DBG("flt bitmap 0x%llx\n", flt_bitmap);
  3464. obj->write_val_to_hdr(flt_bitmap, mem->base);
  3465. }
  3466. addr = obj->create_tbl_addr(true,
  3467. ipahal_ctx->empty_fltrt_tbl.phys_base);
  3468. if (ep_bitmap) {
  3469. for (i = 1; i <= tbls_num; i++)
  3470. obj->write_val_to_hdr(addr,
  3471. mem->base + i * obj->tbl_hdr_width);
  3472. } else {
  3473. for (i = 0; i < tbls_num; i++)
  3474. obj->write_val_to_hdr(addr,
  3475. mem->base + i * obj->tbl_hdr_width);
  3476. }
  3477. return 0;
  3478. }
  3479. /*
  3480. * ipa_fltrt_alloc_init_tbl_hdr() - allocate and initialize buffers for
  3481. * flt/rt tables headers to be filled into sram. Init each table to point
  3482. * to empty system table
  3483. * @params: Allocate IN and OUT params
  3484. *
  3485. * Return: 0 on success, negative on failure
  3486. */
  3487. static int ipa_fltrt_alloc_init_tbl_hdr(
  3488. struct ipahal_fltrt_alloc_imgs_params *params)
  3489. {
  3490. u64 addr;
  3491. int i;
  3492. struct ipahal_fltrt_obj *obj;
  3493. gfp_t flag = GFP_KERNEL;
  3494. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3495. if (!params) {
  3496. IPAHAL_ERR_RL("Input error: params=%pK\n", params);
  3497. return -EINVAL;
  3498. }
  3499. params->nhash_hdr.size = params->tbls_num * obj->tbl_hdr_width;
  3500. alloc:
  3501. params->nhash_hdr.base = dma_alloc_coherent(ipahal_ctx->ipa_pdev,
  3502. params->nhash_hdr.size,
  3503. &params->nhash_hdr.phys_base, flag);
  3504. if (!params->nhash_hdr.base) {
  3505. if (flag == GFP_KERNEL) {
  3506. flag = GFP_ATOMIC;
  3507. goto alloc;
  3508. }
  3509. IPAHAL_ERR_RL("fail to alloc DMA buff of size %d\n",
  3510. params->nhash_hdr.size);
  3511. goto nhash_alloc_fail;
  3512. }
  3513. if (obj->support_hash) {
  3514. params->hash_hdr.size = params->tbls_num * obj->tbl_hdr_width;
  3515. params->hash_hdr.base = dma_alloc_coherent(ipahal_ctx->ipa_pdev,
  3516. params->hash_hdr.size, &params->hash_hdr.phys_base,
  3517. GFP_KERNEL);
  3518. if (!params->hash_hdr.base) {
  3519. IPAHAL_ERR_RL("fail to alloc DMA buff of size %d\n",
  3520. params->hash_hdr.size);
  3521. goto hash_alloc_fail;
  3522. }
  3523. }
  3524. addr = obj->create_tbl_addr(true,
  3525. ipahal_ctx->empty_fltrt_tbl.phys_base);
  3526. for (i = 0; i < params->tbls_num; i++) {
  3527. obj->write_val_to_hdr(addr,
  3528. params->nhash_hdr.base + i * obj->tbl_hdr_width);
  3529. if (obj->support_hash)
  3530. obj->write_val_to_hdr(addr,
  3531. params->hash_hdr.base +
  3532. i * obj->tbl_hdr_width);
  3533. }
  3534. return 0;
  3535. hash_alloc_fail:
  3536. ipahal_free_dma_mem(&params->nhash_hdr);
  3537. nhash_alloc_fail:
  3538. return -ENOMEM;
  3539. }
  3540. /*
  3541. * ipa_fltrt_alloc_lcl_bdy() - allocate and initialize buffers for
  3542. * local flt/rt tables bodies to be filled into sram
  3543. * @params: Allocate IN and OUT params
  3544. *
  3545. * Return: 0 on success, negative on failure
  3546. */
  3547. static int ipa_fltrt_alloc_lcl_bdy(
  3548. struct ipahal_fltrt_alloc_imgs_params *params)
  3549. {
  3550. struct ipahal_fltrt_obj *obj;
  3551. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3552. /* The HAL allocates larger sizes than the given effective ones
  3553. * for alignments and border indications
  3554. */
  3555. IPAHAL_DBG_LOW("lcl tbl bdy total effective sizes: hash=%u nhash=%u\n",
  3556. params->total_sz_lcl_hash_tbls,
  3557. params->total_sz_lcl_nhash_tbls);
  3558. IPAHAL_DBG_LOW("lcl tbl bdy count: hash=%u nhash=%u\n",
  3559. params->num_lcl_hash_tbls,
  3560. params->num_lcl_nhash_tbls);
  3561. /* Align the sizes to coop with termination word
  3562. * and H/W local table start offset alignment
  3563. */
  3564. if (params->nhash_bdy.size) {
  3565. params->nhash_bdy.size = params->total_sz_lcl_nhash_tbls;
  3566. /* for table terminator */
  3567. params->nhash_bdy.size += obj->tbl_width *
  3568. params->num_lcl_nhash_tbls;
  3569. /* align the start of local rule-set */
  3570. params->nhash_bdy.size += obj->lcladdr_alignment *
  3571. params->num_lcl_nhash_tbls;
  3572. /* SRAM block size alignment */
  3573. params->nhash_bdy.size += obj->blk_sz_alignment;
  3574. params->nhash_bdy.size &= ~(obj->blk_sz_alignment);
  3575. IPAHAL_DBG_LOW("nhash lcl tbl bdy total h/w size = %u\n",
  3576. params->nhash_bdy.size);
  3577. params->nhash_bdy.base = dma_zalloc_coherent(
  3578. ipahal_ctx->ipa_pdev, params->nhash_bdy.size,
  3579. &params->nhash_bdy.phys_base, GFP_KERNEL);
  3580. if (!params->nhash_bdy.base) {
  3581. IPAHAL_ERR("fail to alloc DMA buff of size %d\n",
  3582. params->nhash_bdy.size);
  3583. return -ENOMEM;
  3584. }
  3585. }
  3586. if (!obj->support_hash && params->hash_bdy.size) {
  3587. IPAHAL_ERR("No HAL Hash tbls support - Will be ignored\n");
  3588. WARN_ON(1);
  3589. }
  3590. if (obj->support_hash && params->hash_bdy.size) {
  3591. params->hash_bdy.size = params->total_sz_lcl_hash_tbls;
  3592. /* for table terminator */
  3593. params->hash_bdy.size += obj->tbl_width *
  3594. params->num_lcl_hash_tbls;
  3595. /* align the start of local rule-set */
  3596. params->hash_bdy.size += obj->lcladdr_alignment *
  3597. params->num_lcl_hash_tbls;
  3598. /* SRAM block size alignment */
  3599. params->hash_bdy.size += obj->blk_sz_alignment;
  3600. params->hash_bdy.size &= ~(obj->blk_sz_alignment);
  3601. IPAHAL_DBG_LOW("hash lcl tbl bdy total h/w size = %u\n",
  3602. params->hash_bdy.size);
  3603. params->hash_bdy.base = dma_zalloc_coherent(
  3604. ipahal_ctx->ipa_pdev, params->hash_bdy.size,
  3605. &params->hash_bdy.phys_base, GFP_KERNEL);
  3606. if (!params->hash_bdy.base) {
  3607. IPAHAL_ERR("fail to alloc DMA buff of size %d\n",
  3608. params->hash_bdy.size);
  3609. goto hash_bdy_fail;
  3610. }
  3611. }
  3612. return 0;
  3613. hash_bdy_fail:
  3614. if (params->nhash_bdy.size)
  3615. ipahal_free_dma_mem(&params->nhash_bdy);
  3616. return -ENOMEM;
  3617. }
  3618. /*
  3619. * ipahal_fltrt_allocate_hw_tbl_imgs() - Allocate tbl images DMA structures
  3620. * Used usually during commit.
  3621. * Allocates header structures and init them to point to empty DDR table
  3622. * Allocate body strucutres for local bodies tables
  3623. * @params: Parameters for IN and OUT regard the allocation.
  3624. */
  3625. int ipahal_fltrt_allocate_hw_tbl_imgs(
  3626. struct ipahal_fltrt_alloc_imgs_params *params)
  3627. {
  3628. IPAHAL_DBG_LOW("Entry\n");
  3629. /* Input validation */
  3630. if (!params) {
  3631. IPAHAL_ERR_RL("Input err: no params\n");
  3632. return -EINVAL;
  3633. }
  3634. if (params->ipt >= IPA_IP_MAX) {
  3635. IPAHAL_ERR_RL("Input err: Invalid ip type %d\n", params->ipt);
  3636. return -EINVAL;
  3637. }
  3638. if (ipa_fltrt_alloc_init_tbl_hdr(params)) {
  3639. IPAHAL_ERR_RL("fail to alloc and init tbl hdr\n");
  3640. return -ENOMEM;
  3641. }
  3642. if (ipa_fltrt_alloc_lcl_bdy(params)) {
  3643. IPAHAL_ERR_RL("fail to alloc tbl bodies\n");
  3644. goto bdy_alloc_fail;
  3645. }
  3646. return 0;
  3647. bdy_alloc_fail:
  3648. ipahal_free_dma_mem(&params->nhash_hdr);
  3649. if (params->hash_hdr.size)
  3650. ipahal_free_dma_mem(&params->hash_hdr);
  3651. return -ENOMEM;
  3652. }
  3653. /*
  3654. * ipahal_fltrt_allocate_hw_sys_tbl() - Allocate DMA mem for H/W flt/rt sys tbl
  3655. * @tbl_mem: IN/OUT param. size for effective table size. Pointer, for the
  3656. * allocated memory.
  3657. *
  3658. * The size is adapted for needed alignments/borders.
  3659. */
  3660. int ipahal_fltrt_allocate_hw_sys_tbl(struct ipa_mem_buffer *tbl_mem)
  3661. {
  3662. struct ipahal_fltrt_obj *obj;
  3663. IPAHAL_DBG_LOW("Entry\n");
  3664. if (!tbl_mem) {
  3665. IPAHAL_ERR("Input err\n");
  3666. return -EINVAL;
  3667. }
  3668. if (!tbl_mem->size) {
  3669. IPAHAL_ERR("Input err: zero table size\n");
  3670. return -EINVAL;
  3671. }
  3672. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3673. /* add word for rule-set terminator */
  3674. tbl_mem->size += obj->tbl_width;
  3675. tbl_mem->base = dma_alloc_coherent(ipahal_ctx->ipa_pdev, tbl_mem->size,
  3676. &tbl_mem->phys_base, GFP_KERNEL);
  3677. if (!tbl_mem->base) {
  3678. IPAHAL_ERR("fail to alloc DMA buf of size %d\n",
  3679. tbl_mem->size);
  3680. return -ENOMEM;
  3681. }
  3682. if (tbl_mem->phys_base & obj->sysaddr_alignment) {
  3683. IPAHAL_ERR("sys rt tbl address is not aligned\n");
  3684. goto align_err;
  3685. }
  3686. memset(tbl_mem->base, 0, tbl_mem->size);
  3687. return 0;
  3688. align_err:
  3689. ipahal_free_dma_mem(tbl_mem);
  3690. return -EPERM;
  3691. }
  3692. /*
  3693. * ipahal_fltrt_write_addr_to_hdr() - Fill table header with table address
  3694. * Given table addr/offset, adapt it to IPA H/W format and write it
  3695. * to given header index.
  3696. * @addr: Address or offset to be used
  3697. * @hdr_base: base address of header structure to write the address
  3698. * @hdr_idx: index of the address in the header structure
  3699. * @is_sys: Is it system address or local offset
  3700. */
  3701. int ipahal_fltrt_write_addr_to_hdr(u64 addr, void *hdr_base, u32 hdr_idx,
  3702. bool is_sys)
  3703. {
  3704. struct ipahal_fltrt_obj *obj;
  3705. u64 hwaddr;
  3706. u8 *hdr;
  3707. IPAHAL_DBG_LOW("Entry\n");
  3708. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3709. if (!addr || !hdr_base) {
  3710. IPAHAL_ERR("Input err: addr=0x%llx hdr_base=%pK\n",
  3711. addr, hdr_base);
  3712. return -EINVAL;
  3713. }
  3714. hdr = (u8 *)hdr_base;
  3715. hdr += hdr_idx * obj->tbl_hdr_width;
  3716. hwaddr = obj->create_tbl_addr(is_sys, addr);
  3717. obj->write_val_to_hdr(hwaddr, hdr);
  3718. return 0;
  3719. }
  3720. /*
  3721. * ipahal_fltrt_read_addr_from_hdr() - Given sram address, read it's
  3722. * content (physical address or offset) and parse it.
  3723. * @hdr_base: base sram address of the header structure.
  3724. * @hdr_idx: index of the header entry line in the header structure.
  3725. * @addr: The parsed address - Out parameter
  3726. * @is_sys: Is this system or local address - Out parameter
  3727. */
  3728. int ipahal_fltrt_read_addr_from_hdr(void *hdr_base, u32 hdr_idx, u64 *addr,
  3729. bool *is_sys)
  3730. {
  3731. struct ipahal_fltrt_obj *obj;
  3732. u64 hwaddr;
  3733. u8 *hdr;
  3734. IPAHAL_DBG_LOW("Entry\n");
  3735. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3736. if (!addr || !hdr_base || !is_sys) {
  3737. IPAHAL_ERR("Input err: addr=%pK hdr_base=%pK is_sys=%pK\n",
  3738. addr, hdr_base, is_sys);
  3739. return -EINVAL;
  3740. }
  3741. hdr = (u8 *)hdr_base;
  3742. hdr += hdr_idx * obj->tbl_hdr_width;
  3743. hwaddr = *((u64 *)hdr);
  3744. obj->parse_tbl_addr(hwaddr, addr, is_sys);
  3745. return 0;
  3746. }
  3747. /*
  3748. * ipahal_rt_generate_hw_rule() - generates the routing hardware rule
  3749. * @params: Params for the rule creation.
  3750. * @hw_len: Size of the H/W rule to be returned
  3751. * @buf: Buffer to build the rule in. If buf is NULL, then the rule will
  3752. * be built in internal temp buf. This is used e.g. to get the rule size
  3753. * only.
  3754. */
  3755. int ipahal_rt_generate_hw_rule(struct ipahal_rt_rule_gen_params *params,
  3756. u32 *hw_len, u8 *buf)
  3757. {
  3758. struct ipahal_fltrt_obj *obj;
  3759. u8 *tmp = NULL;
  3760. int rc;
  3761. IPAHAL_DBG_LOW("Entry\n");
  3762. if (!params || !hw_len) {
  3763. IPAHAL_ERR("Input err: params=%pK hw_len=%pK\n",
  3764. params, hw_len);
  3765. return -EINVAL;
  3766. }
  3767. if (!params->rule) {
  3768. IPAHAL_ERR("Input err: invalid rule\n");
  3769. return -EINVAL;
  3770. }
  3771. if (params->ipt >= IPA_IP_MAX) {
  3772. IPAHAL_ERR("Input err: Invalid ip type %d\n", params->ipt);
  3773. return -EINVAL;
  3774. }
  3775. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3776. if (buf == NULL) {
  3777. tmp = kzalloc(obj->rule_buf_size, GFP_KERNEL);
  3778. if (!tmp)
  3779. return -ENOMEM;
  3780. buf = tmp;
  3781. } else {
  3782. if ((long)buf & obj->rule_start_alignment) {
  3783. IPAHAL_ERR("buff is not rule start aligned\n");
  3784. return -EPERM;
  3785. }
  3786. }
  3787. rc = obj->rt_generate_hw_rule(params, hw_len, buf);
  3788. if (!tmp && !rc) {
  3789. /* write the rule-set terminator */
  3790. memset(buf + *hw_len, 0, obj->tbl_width);
  3791. }
  3792. kfree(tmp);
  3793. return rc;
  3794. }
  3795. /*
  3796. * ipahal_flt_generate_hw_rule() - generates the filtering hardware rule.
  3797. * @params: Params for the rule creation.
  3798. * @hw_len: Size of the H/W rule to be returned
  3799. * @buf: Buffer to build the rule in. If buf is NULL, then the rule will
  3800. * be built in internal temp buf. This is used e.g. to get the rule size
  3801. * only.
  3802. */
  3803. int ipahal_flt_generate_hw_rule(struct ipahal_flt_rule_gen_params *params,
  3804. u32 *hw_len, u8 *buf)
  3805. {
  3806. struct ipahal_fltrt_obj *obj;
  3807. u8 *tmp = NULL;
  3808. int rc;
  3809. IPAHAL_DBG_LOW("Entry\n");
  3810. if (!params || !hw_len) {
  3811. IPAHAL_ERR("Input err: params=%pK hw_len=%pK\n",
  3812. params, hw_len);
  3813. return -EINVAL;
  3814. }
  3815. if (!params->rule) {
  3816. IPAHAL_ERR("Input err: invalid rule\n");
  3817. return -EINVAL;
  3818. }
  3819. if (params->ipt >= IPA_IP_MAX) {
  3820. IPAHAL_ERR("Input err: Invalid ip type %d\n", params->ipt);
  3821. return -EINVAL;
  3822. }
  3823. obj = &ipahal_fltrt_objs[ipahal_ctx->hw_type];
  3824. if (buf == NULL) {
  3825. tmp = kzalloc(obj->rule_buf_size, GFP_KERNEL);
  3826. if (!tmp) {
  3827. IPAHAL_ERR("failed to alloc %u bytes\n",
  3828. obj->rule_buf_size);
  3829. return -ENOMEM;
  3830. }
  3831. buf = tmp;
  3832. } else
  3833. if ((long)buf & obj->rule_start_alignment) {
  3834. IPAHAL_ERR("buff is not rule rule start aligned\n");
  3835. return -EPERM;
  3836. }
  3837. rc = obj->flt_generate_hw_rule(params, hw_len, buf);
  3838. if (!tmp && !rc) {
  3839. /* write the rule-set terminator */
  3840. memset(buf + *hw_len, 0, obj->tbl_width);
  3841. }
  3842. kfree(tmp);
  3843. return rc;
  3844. }
  3845. /*
  3846. * ipahal_flt_generate_equation() - generate flt rule in equation form
  3847. * Will build equation form flt rule from given info.
  3848. * @ipt: IP family
  3849. * @attrib: Rule attribute to be generated
  3850. * @eq_atrb: Equation form generated rule
  3851. * Note: Usage example: Pass the generated form to other sub-systems
  3852. * for inter-subsystems rules exchange.
  3853. */
  3854. int ipahal_flt_generate_equation(enum ipa_ip_type ipt,
  3855. const struct ipa_rule_attrib *attrib,
  3856. struct ipa_ipfltri_rule_eq *eq_atrb)
  3857. {
  3858. IPAHAL_DBG_LOW("Entry\n");
  3859. if (ipt >= IPA_IP_MAX) {
  3860. IPAHAL_ERR_RL("Input err: Invalid ip type %d\n", ipt);
  3861. return -EINVAL;
  3862. }
  3863. if (!attrib || !eq_atrb) {
  3864. IPAHAL_ERR_RL("Input err: attrib=%pK eq_atrb=%pK\n",
  3865. attrib, eq_atrb);
  3866. return -EINVAL;
  3867. }
  3868. return ipahal_fltrt_objs[ipahal_ctx->hw_type].flt_generate_eq(ipt,
  3869. attrib, eq_atrb);
  3870. }
  3871. /*
  3872. * ipahal_rt_parse_hw_rule() - Parse H/W formated rt rule
  3873. * Given the rule address, read the rule info from H/W and parse it.
  3874. * @rule_addr: Rule address (virtual memory)
  3875. * @rule: Out parameter for parsed rule info
  3876. */
  3877. int ipahal_rt_parse_hw_rule(u8 *rule_addr,
  3878. struct ipahal_rt_rule_entry *rule)
  3879. {
  3880. IPAHAL_DBG_LOW("Entry\n");
  3881. if (!rule_addr || !rule) {
  3882. IPAHAL_ERR("Input err: rule_addr=%pK rule=%pK\n",
  3883. rule_addr, rule);
  3884. return -EINVAL;
  3885. }
  3886. return ipahal_fltrt_objs[ipahal_ctx->hw_type].rt_parse_hw_rule(
  3887. rule_addr, rule);
  3888. }
  3889. /*
  3890. * ipahal_flt_parse_hw_rule() - Parse H/W formated flt rule
  3891. * Given the rule address, read the rule info from H/W and parse it.
  3892. * @rule_addr: Rule address (virtual memory)
  3893. * @rule: Out parameter for parsed rule info
  3894. */
  3895. int ipahal_flt_parse_hw_rule(u8 *rule_addr,
  3896. struct ipahal_flt_rule_entry *rule)
  3897. {
  3898. IPAHAL_DBG_LOW("Entry\n");
  3899. if (!rule_addr || !rule) {
  3900. IPAHAL_ERR("Input err: rule_addr=%pK rule=%pK\n",
  3901. rule_addr, rule);
  3902. return -EINVAL;
  3903. }
  3904. return ipahal_fltrt_objs[ipahal_ctx->hw_type].flt_parse_hw_rule(
  3905. rule_addr, rule);
  3906. }