ipahal_fltrt.c 124 KB

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