ice_flex_pipe.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2019, Intel Corporation. */
  3. #include "ice_common.h"
  4. #include "ice_flex_pipe.h"
  5. #include "ice_flow.h"
  6. #include "ice.h"
  7. static const u32 ice_sect_lkup[ICE_BLK_COUNT][ICE_SECT_COUNT] = {
  8. /* SWITCH */
  9. {
  10. ICE_SID_XLT0_SW,
  11. ICE_SID_XLT_KEY_BUILDER_SW,
  12. ICE_SID_XLT1_SW,
  13. ICE_SID_XLT2_SW,
  14. ICE_SID_PROFID_TCAM_SW,
  15. ICE_SID_PROFID_REDIR_SW,
  16. ICE_SID_FLD_VEC_SW,
  17. ICE_SID_CDID_KEY_BUILDER_SW,
  18. ICE_SID_CDID_REDIR_SW
  19. },
  20. /* ACL */
  21. {
  22. ICE_SID_XLT0_ACL,
  23. ICE_SID_XLT_KEY_BUILDER_ACL,
  24. ICE_SID_XLT1_ACL,
  25. ICE_SID_XLT2_ACL,
  26. ICE_SID_PROFID_TCAM_ACL,
  27. ICE_SID_PROFID_REDIR_ACL,
  28. ICE_SID_FLD_VEC_ACL,
  29. ICE_SID_CDID_KEY_BUILDER_ACL,
  30. ICE_SID_CDID_REDIR_ACL
  31. },
  32. /* FD */
  33. {
  34. ICE_SID_XLT0_FD,
  35. ICE_SID_XLT_KEY_BUILDER_FD,
  36. ICE_SID_XLT1_FD,
  37. ICE_SID_XLT2_FD,
  38. ICE_SID_PROFID_TCAM_FD,
  39. ICE_SID_PROFID_REDIR_FD,
  40. ICE_SID_FLD_VEC_FD,
  41. ICE_SID_CDID_KEY_BUILDER_FD,
  42. ICE_SID_CDID_REDIR_FD
  43. },
  44. /* RSS */
  45. {
  46. ICE_SID_XLT0_RSS,
  47. ICE_SID_XLT_KEY_BUILDER_RSS,
  48. ICE_SID_XLT1_RSS,
  49. ICE_SID_XLT2_RSS,
  50. ICE_SID_PROFID_TCAM_RSS,
  51. ICE_SID_PROFID_REDIR_RSS,
  52. ICE_SID_FLD_VEC_RSS,
  53. ICE_SID_CDID_KEY_BUILDER_RSS,
  54. ICE_SID_CDID_REDIR_RSS
  55. },
  56. /* PE */
  57. {
  58. ICE_SID_XLT0_PE,
  59. ICE_SID_XLT_KEY_BUILDER_PE,
  60. ICE_SID_XLT1_PE,
  61. ICE_SID_XLT2_PE,
  62. ICE_SID_PROFID_TCAM_PE,
  63. ICE_SID_PROFID_REDIR_PE,
  64. ICE_SID_FLD_VEC_PE,
  65. ICE_SID_CDID_KEY_BUILDER_PE,
  66. ICE_SID_CDID_REDIR_PE
  67. }
  68. };
  69. /**
  70. * ice_sect_id - returns section ID
  71. * @blk: block type
  72. * @sect: section type
  73. *
  74. * This helper function returns the proper section ID given a block type and a
  75. * section type.
  76. */
  77. static u32 ice_sect_id(enum ice_block blk, enum ice_sect sect)
  78. {
  79. return ice_sect_lkup[blk][sect];
  80. }
  81. /**
  82. * ice_hw_ptype_ena - check if the PTYPE is enabled or not
  83. * @hw: pointer to the HW structure
  84. * @ptype: the hardware PTYPE
  85. */
  86. bool ice_hw_ptype_ena(struct ice_hw *hw, u16 ptype)
  87. {
  88. return ptype < ICE_FLOW_PTYPE_MAX &&
  89. test_bit(ptype, hw->hw_ptype);
  90. }
  91. /* Key creation */
  92. #define ICE_DC_KEY 0x1 /* don't care */
  93. #define ICE_DC_KEYINV 0x1
  94. #define ICE_NM_KEY 0x0 /* never match */
  95. #define ICE_NM_KEYINV 0x0
  96. #define ICE_0_KEY 0x1 /* match 0 */
  97. #define ICE_0_KEYINV 0x0
  98. #define ICE_1_KEY 0x0 /* match 1 */
  99. #define ICE_1_KEYINV 0x1
  100. /**
  101. * ice_gen_key_word - generate 16-bits of a key/mask word
  102. * @val: the value
  103. * @valid: valid bits mask (change only the valid bits)
  104. * @dont_care: don't care mask
  105. * @nvr_mtch: never match mask
  106. * @key: pointer to an array of where the resulting key portion
  107. * @key_inv: pointer to an array of where the resulting key invert portion
  108. *
  109. * This function generates 16-bits from a 8-bit value, an 8-bit don't care mask
  110. * and an 8-bit never match mask. The 16-bits of output are divided into 8 bits
  111. * of key and 8 bits of key invert.
  112. *
  113. * '0' = b01, always match a 0 bit
  114. * '1' = b10, always match a 1 bit
  115. * '?' = b11, don't care bit (always matches)
  116. * '~' = b00, never match bit
  117. *
  118. * Input:
  119. * val: b0 1 0 1 0 1
  120. * dont_care: b0 0 1 1 0 0
  121. * never_mtch: b0 0 0 0 1 1
  122. * ------------------------------
  123. * Result: key: b01 10 11 11 00 00
  124. */
  125. static int
  126. ice_gen_key_word(u8 val, u8 valid, u8 dont_care, u8 nvr_mtch, u8 *key,
  127. u8 *key_inv)
  128. {
  129. u8 in_key = *key, in_key_inv = *key_inv;
  130. u8 i;
  131. /* 'dont_care' and 'nvr_mtch' masks cannot overlap */
  132. if ((dont_care ^ nvr_mtch) != (dont_care | nvr_mtch))
  133. return -EIO;
  134. *key = 0;
  135. *key_inv = 0;
  136. /* encode the 8 bits into 8-bit key and 8-bit key invert */
  137. for (i = 0; i < 8; i++) {
  138. *key >>= 1;
  139. *key_inv >>= 1;
  140. if (!(valid & 0x1)) { /* change only valid bits */
  141. *key |= (in_key & 0x1) << 7;
  142. *key_inv |= (in_key_inv & 0x1) << 7;
  143. } else if (dont_care & 0x1) { /* don't care bit */
  144. *key |= ICE_DC_KEY << 7;
  145. *key_inv |= ICE_DC_KEYINV << 7;
  146. } else if (nvr_mtch & 0x1) { /* never match bit */
  147. *key |= ICE_NM_KEY << 7;
  148. *key_inv |= ICE_NM_KEYINV << 7;
  149. } else if (val & 0x01) { /* exact 1 match */
  150. *key |= ICE_1_KEY << 7;
  151. *key_inv |= ICE_1_KEYINV << 7;
  152. } else { /* exact 0 match */
  153. *key |= ICE_0_KEY << 7;
  154. *key_inv |= ICE_0_KEYINV << 7;
  155. }
  156. dont_care >>= 1;
  157. nvr_mtch >>= 1;
  158. valid >>= 1;
  159. val >>= 1;
  160. in_key >>= 1;
  161. in_key_inv >>= 1;
  162. }
  163. return 0;
  164. }
  165. /**
  166. * ice_bits_max_set - determine if the number of bits set is within a maximum
  167. * @mask: pointer to the byte array which is the mask
  168. * @size: the number of bytes in the mask
  169. * @max: the max number of set bits
  170. *
  171. * This function determines if there are at most 'max' number of bits set in an
  172. * array. Returns true if the number for bits set is <= max or will return false
  173. * otherwise.
  174. */
  175. static bool ice_bits_max_set(const u8 *mask, u16 size, u16 max)
  176. {
  177. u16 count = 0;
  178. u16 i;
  179. /* check each byte */
  180. for (i = 0; i < size; i++) {
  181. /* if 0, go to next byte */
  182. if (!mask[i])
  183. continue;
  184. /* We know there is at least one set bit in this byte because of
  185. * the above check; if we already have found 'max' number of
  186. * bits set, then we can return failure now.
  187. */
  188. if (count == max)
  189. return false;
  190. /* count the bits in this byte, checking threshold */
  191. count += hweight8(mask[i]);
  192. if (count > max)
  193. return false;
  194. }
  195. return true;
  196. }
  197. /**
  198. * ice_set_key - generate a variable sized key with multiples of 16-bits
  199. * @key: pointer to where the key will be stored
  200. * @size: the size of the complete key in bytes (must be even)
  201. * @val: array of 8-bit values that makes up the value portion of the key
  202. * @upd: array of 8-bit masks that determine what key portion to update
  203. * @dc: array of 8-bit masks that make up the don't care mask
  204. * @nm: array of 8-bit masks that make up the never match mask
  205. * @off: the offset of the first byte in the key to update
  206. * @len: the number of bytes in the key update
  207. *
  208. * This function generates a key from a value, a don't care mask and a never
  209. * match mask.
  210. * upd, dc, and nm are optional parameters, and can be NULL:
  211. * upd == NULL --> upd mask is all 1's (update all bits)
  212. * dc == NULL --> dc mask is all 0's (no don't care bits)
  213. * nm == NULL --> nm mask is all 0's (no never match bits)
  214. */
  215. static int
  216. ice_set_key(u8 *key, u16 size, u8 *val, u8 *upd, u8 *dc, u8 *nm, u16 off,
  217. u16 len)
  218. {
  219. u16 half_size;
  220. u16 i;
  221. /* size must be a multiple of 2 bytes. */
  222. if (size % 2)
  223. return -EIO;
  224. half_size = size / 2;
  225. if (off + len > half_size)
  226. return -EIO;
  227. /* Make sure at most one bit is set in the never match mask. Having more
  228. * than one never match mask bit set will cause HW to consume excessive
  229. * power otherwise; this is a power management efficiency check.
  230. */
  231. #define ICE_NVR_MTCH_BITS_MAX 1
  232. if (nm && !ice_bits_max_set(nm, len, ICE_NVR_MTCH_BITS_MAX))
  233. return -EIO;
  234. for (i = 0; i < len; i++)
  235. if (ice_gen_key_word(val[i], upd ? upd[i] : 0xff,
  236. dc ? dc[i] : 0, nm ? nm[i] : 0,
  237. key + off + i, key + half_size + off + i))
  238. return -EIO;
  239. return 0;
  240. }
  241. /**
  242. * ice_acquire_change_lock
  243. * @hw: pointer to the HW structure
  244. * @access: access type (read or write)
  245. *
  246. * This function will request ownership of the change lock.
  247. */
  248. int
  249. ice_acquire_change_lock(struct ice_hw *hw, enum ice_aq_res_access_type access)
  250. {
  251. return ice_acquire_res(hw, ICE_CHANGE_LOCK_RES_ID, access,
  252. ICE_CHANGE_LOCK_TIMEOUT);
  253. }
  254. /**
  255. * ice_release_change_lock
  256. * @hw: pointer to the HW structure
  257. *
  258. * This function will release the change lock using the proper Admin Command.
  259. */
  260. void ice_release_change_lock(struct ice_hw *hw)
  261. {
  262. ice_release_res(hw, ICE_CHANGE_LOCK_RES_ID);
  263. }
  264. /**
  265. * ice_get_open_tunnel_port - retrieve an open tunnel port
  266. * @hw: pointer to the HW structure
  267. * @port: returns open port
  268. * @type: type of tunnel, can be TNL_LAST if it doesn't matter
  269. */
  270. bool
  271. ice_get_open_tunnel_port(struct ice_hw *hw, u16 *port,
  272. enum ice_tunnel_type type)
  273. {
  274. bool res = false;
  275. u16 i;
  276. mutex_lock(&hw->tnl_lock);
  277. for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
  278. if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].port &&
  279. (type == TNL_LAST || type == hw->tnl.tbl[i].type)) {
  280. *port = hw->tnl.tbl[i].port;
  281. res = true;
  282. break;
  283. }
  284. mutex_unlock(&hw->tnl_lock);
  285. return res;
  286. }
  287. /**
  288. * ice_upd_dvm_boost_entry
  289. * @hw: pointer to the HW structure
  290. * @entry: pointer to double vlan boost entry info
  291. */
  292. static int
  293. ice_upd_dvm_boost_entry(struct ice_hw *hw, struct ice_dvm_entry *entry)
  294. {
  295. struct ice_boost_tcam_section *sect_rx, *sect_tx;
  296. int status = -ENOSPC;
  297. struct ice_buf_build *bld;
  298. u8 val, dc, nm;
  299. bld = ice_pkg_buf_alloc(hw);
  300. if (!bld)
  301. return -ENOMEM;
  302. /* allocate 2 sections, one for Rx parser, one for Tx parser */
  303. if (ice_pkg_buf_reserve_section(bld, 2))
  304. goto ice_upd_dvm_boost_entry_err;
  305. sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
  306. struct_size(sect_rx, tcam, 1));
  307. if (!sect_rx)
  308. goto ice_upd_dvm_boost_entry_err;
  309. sect_rx->count = cpu_to_le16(1);
  310. sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
  311. struct_size(sect_tx, tcam, 1));
  312. if (!sect_tx)
  313. goto ice_upd_dvm_boost_entry_err;
  314. sect_tx->count = cpu_to_le16(1);
  315. /* copy original boost entry to update package buffer */
  316. memcpy(sect_rx->tcam, entry->boost_entry, sizeof(*sect_rx->tcam));
  317. /* re-write the don't care and never match bits accordingly */
  318. if (entry->enable) {
  319. /* all bits are don't care */
  320. val = 0x00;
  321. dc = 0xFF;
  322. nm = 0x00;
  323. } else {
  324. /* disable, one never match bit, the rest are don't care */
  325. val = 0x00;
  326. dc = 0xF7;
  327. nm = 0x08;
  328. }
  329. ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
  330. &val, NULL, &dc, &nm, 0, sizeof(u8));
  331. /* exact copy of entry to Tx section entry */
  332. memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
  333. status = ice_update_pkg_no_lock(hw, ice_pkg_buf(bld), 1);
  334. ice_upd_dvm_boost_entry_err:
  335. ice_pkg_buf_free(hw, bld);
  336. return status;
  337. }
  338. /**
  339. * ice_set_dvm_boost_entries
  340. * @hw: pointer to the HW structure
  341. *
  342. * Enable double vlan by updating the appropriate boost tcam entries.
  343. */
  344. int ice_set_dvm_boost_entries(struct ice_hw *hw)
  345. {
  346. u16 i;
  347. for (i = 0; i < hw->dvm_upd.count; i++) {
  348. int status;
  349. status = ice_upd_dvm_boost_entry(hw, &hw->dvm_upd.tbl[i]);
  350. if (status)
  351. return status;
  352. }
  353. return 0;
  354. }
  355. /**
  356. * ice_tunnel_idx_to_entry - convert linear index to the sparse one
  357. * @hw: pointer to the HW structure
  358. * @type: type of tunnel
  359. * @idx: linear index
  360. *
  361. * Stack assumes we have 2 linear tables with indexes [0, count_valid),
  362. * but really the port table may be sprase, and types are mixed, so convert
  363. * the stack index into the device index.
  364. */
  365. static u16 ice_tunnel_idx_to_entry(struct ice_hw *hw, enum ice_tunnel_type type,
  366. u16 idx)
  367. {
  368. u16 i;
  369. for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
  370. if (hw->tnl.tbl[i].valid &&
  371. hw->tnl.tbl[i].type == type &&
  372. idx-- == 0)
  373. return i;
  374. WARN_ON_ONCE(1);
  375. return 0;
  376. }
  377. /**
  378. * ice_create_tunnel
  379. * @hw: pointer to the HW structure
  380. * @index: device table entry
  381. * @type: type of tunnel
  382. * @port: port of tunnel to create
  383. *
  384. * Create a tunnel by updating the parse graph in the parser. We do that by
  385. * creating a package buffer with the tunnel info and issuing an update package
  386. * command.
  387. */
  388. static int
  389. ice_create_tunnel(struct ice_hw *hw, u16 index,
  390. enum ice_tunnel_type type, u16 port)
  391. {
  392. struct ice_boost_tcam_section *sect_rx, *sect_tx;
  393. struct ice_buf_build *bld;
  394. int status = -ENOSPC;
  395. mutex_lock(&hw->tnl_lock);
  396. bld = ice_pkg_buf_alloc(hw);
  397. if (!bld) {
  398. status = -ENOMEM;
  399. goto ice_create_tunnel_end;
  400. }
  401. /* allocate 2 sections, one for Rx parser, one for Tx parser */
  402. if (ice_pkg_buf_reserve_section(bld, 2))
  403. goto ice_create_tunnel_err;
  404. sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
  405. struct_size(sect_rx, tcam, 1));
  406. if (!sect_rx)
  407. goto ice_create_tunnel_err;
  408. sect_rx->count = cpu_to_le16(1);
  409. sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
  410. struct_size(sect_tx, tcam, 1));
  411. if (!sect_tx)
  412. goto ice_create_tunnel_err;
  413. sect_tx->count = cpu_to_le16(1);
  414. /* copy original boost entry to update package buffer */
  415. memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
  416. sizeof(*sect_rx->tcam));
  417. /* over-write the never-match dest port key bits with the encoded port
  418. * bits
  419. */
  420. ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
  421. (u8 *)&port, NULL, NULL, NULL,
  422. (u16)offsetof(struct ice_boost_key_value, hv_dst_port_key),
  423. sizeof(sect_rx->tcam[0].key.key.hv_dst_port_key));
  424. /* exact copy of entry to Tx section entry */
  425. memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
  426. status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
  427. if (!status)
  428. hw->tnl.tbl[index].port = port;
  429. ice_create_tunnel_err:
  430. ice_pkg_buf_free(hw, bld);
  431. ice_create_tunnel_end:
  432. mutex_unlock(&hw->tnl_lock);
  433. return status;
  434. }
  435. /**
  436. * ice_destroy_tunnel
  437. * @hw: pointer to the HW structure
  438. * @index: device table entry
  439. * @type: type of tunnel
  440. * @port: port of tunnel to destroy (ignored if the all parameter is true)
  441. *
  442. * Destroys a tunnel or all tunnels by creating an update package buffer
  443. * targeting the specific updates requested and then performing an update
  444. * package.
  445. */
  446. static int
  447. ice_destroy_tunnel(struct ice_hw *hw, u16 index, enum ice_tunnel_type type,
  448. u16 port)
  449. {
  450. struct ice_boost_tcam_section *sect_rx, *sect_tx;
  451. struct ice_buf_build *bld;
  452. int status = -ENOSPC;
  453. mutex_lock(&hw->tnl_lock);
  454. if (WARN_ON(!hw->tnl.tbl[index].valid ||
  455. hw->tnl.tbl[index].type != type ||
  456. hw->tnl.tbl[index].port != port)) {
  457. status = -EIO;
  458. goto ice_destroy_tunnel_end;
  459. }
  460. bld = ice_pkg_buf_alloc(hw);
  461. if (!bld) {
  462. status = -ENOMEM;
  463. goto ice_destroy_tunnel_end;
  464. }
  465. /* allocate 2 sections, one for Rx parser, one for Tx parser */
  466. if (ice_pkg_buf_reserve_section(bld, 2))
  467. goto ice_destroy_tunnel_err;
  468. sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
  469. struct_size(sect_rx, tcam, 1));
  470. if (!sect_rx)
  471. goto ice_destroy_tunnel_err;
  472. sect_rx->count = cpu_to_le16(1);
  473. sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
  474. struct_size(sect_tx, tcam, 1));
  475. if (!sect_tx)
  476. goto ice_destroy_tunnel_err;
  477. sect_tx->count = cpu_to_le16(1);
  478. /* copy original boost entry to update package buffer, one copy to Rx
  479. * section, another copy to the Tx section
  480. */
  481. memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
  482. sizeof(*sect_rx->tcam));
  483. memcpy(sect_tx->tcam, hw->tnl.tbl[index].boost_entry,
  484. sizeof(*sect_tx->tcam));
  485. status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
  486. if (!status)
  487. hw->tnl.tbl[index].port = 0;
  488. ice_destroy_tunnel_err:
  489. ice_pkg_buf_free(hw, bld);
  490. ice_destroy_tunnel_end:
  491. mutex_unlock(&hw->tnl_lock);
  492. return status;
  493. }
  494. int ice_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
  495. unsigned int idx, struct udp_tunnel_info *ti)
  496. {
  497. struct ice_netdev_priv *np = netdev_priv(netdev);
  498. struct ice_vsi *vsi = np->vsi;
  499. struct ice_pf *pf = vsi->back;
  500. enum ice_tunnel_type tnl_type;
  501. int status;
  502. u16 index;
  503. tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE;
  504. index = ice_tunnel_idx_to_entry(&pf->hw, tnl_type, idx);
  505. status = ice_create_tunnel(&pf->hw, index, tnl_type, ntohs(ti->port));
  506. if (status) {
  507. netdev_err(netdev, "Error adding UDP tunnel - %d\n",
  508. status);
  509. return -EIO;
  510. }
  511. udp_tunnel_nic_set_port_priv(netdev, table, idx, index);
  512. return 0;
  513. }
  514. int ice_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
  515. unsigned int idx, struct udp_tunnel_info *ti)
  516. {
  517. struct ice_netdev_priv *np = netdev_priv(netdev);
  518. struct ice_vsi *vsi = np->vsi;
  519. struct ice_pf *pf = vsi->back;
  520. enum ice_tunnel_type tnl_type;
  521. int status;
  522. tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE;
  523. status = ice_destroy_tunnel(&pf->hw, ti->hw_priv, tnl_type,
  524. ntohs(ti->port));
  525. if (status) {
  526. netdev_err(netdev, "Error removing UDP tunnel - %d\n",
  527. status);
  528. return -EIO;
  529. }
  530. return 0;
  531. }
  532. /**
  533. * ice_find_prot_off - find prot ID and offset pair, based on prof and FV index
  534. * @hw: pointer to the hardware structure
  535. * @blk: hardware block
  536. * @prof: profile ID
  537. * @fv_idx: field vector word index
  538. * @prot: variable to receive the protocol ID
  539. * @off: variable to receive the protocol offset
  540. */
  541. int
  542. ice_find_prot_off(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 fv_idx,
  543. u8 *prot, u16 *off)
  544. {
  545. struct ice_fv_word *fv_ext;
  546. if (prof >= hw->blk[blk].es.count)
  547. return -EINVAL;
  548. if (fv_idx >= hw->blk[blk].es.fvw)
  549. return -EINVAL;
  550. fv_ext = hw->blk[blk].es.t + (prof * hw->blk[blk].es.fvw);
  551. *prot = fv_ext[fv_idx].prot_id;
  552. *off = fv_ext[fv_idx].off;
  553. return 0;
  554. }
  555. /* PTG Management */
  556. /**
  557. * ice_ptg_find_ptype - Search for packet type group using packet type (ptype)
  558. * @hw: pointer to the hardware structure
  559. * @blk: HW block
  560. * @ptype: the ptype to search for
  561. * @ptg: pointer to variable that receives the PTG
  562. *
  563. * This function will search the PTGs for a particular ptype, returning the
  564. * PTG ID that contains it through the PTG parameter, with the value of
  565. * ICE_DEFAULT_PTG (0) meaning it is part the default PTG.
  566. */
  567. static int
  568. ice_ptg_find_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 *ptg)
  569. {
  570. if (ptype >= ICE_XLT1_CNT || !ptg)
  571. return -EINVAL;
  572. *ptg = hw->blk[blk].xlt1.ptypes[ptype].ptg;
  573. return 0;
  574. }
  575. /**
  576. * ice_ptg_alloc_val - Allocates a new packet type group ID by value
  577. * @hw: pointer to the hardware structure
  578. * @blk: HW block
  579. * @ptg: the PTG to allocate
  580. *
  581. * This function allocates a given packet type group ID specified by the PTG
  582. * parameter.
  583. */
  584. static void ice_ptg_alloc_val(struct ice_hw *hw, enum ice_block blk, u8 ptg)
  585. {
  586. hw->blk[blk].xlt1.ptg_tbl[ptg].in_use = true;
  587. }
  588. /**
  589. * ice_ptg_remove_ptype - Removes ptype from a particular packet type group
  590. * @hw: pointer to the hardware structure
  591. * @blk: HW block
  592. * @ptype: the ptype to remove
  593. * @ptg: the PTG to remove the ptype from
  594. *
  595. * This function will remove the ptype from the specific PTG, and move it to
  596. * the default PTG (ICE_DEFAULT_PTG).
  597. */
  598. static int
  599. ice_ptg_remove_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
  600. {
  601. struct ice_ptg_ptype **ch;
  602. struct ice_ptg_ptype *p;
  603. if (ptype > ICE_XLT1_CNT - 1)
  604. return -EINVAL;
  605. if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use)
  606. return -ENOENT;
  607. /* Should not happen if .in_use is set, bad config */
  608. if (!hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype)
  609. return -EIO;
  610. /* find the ptype within this PTG, and bypass the link over it */
  611. p = hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
  612. ch = &hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
  613. while (p) {
  614. if (ptype == (p - hw->blk[blk].xlt1.ptypes)) {
  615. *ch = p->next_ptype;
  616. break;
  617. }
  618. ch = &p->next_ptype;
  619. p = p->next_ptype;
  620. }
  621. hw->blk[blk].xlt1.ptypes[ptype].ptg = ICE_DEFAULT_PTG;
  622. hw->blk[blk].xlt1.ptypes[ptype].next_ptype = NULL;
  623. return 0;
  624. }
  625. /**
  626. * ice_ptg_add_mv_ptype - Adds/moves ptype to a particular packet type group
  627. * @hw: pointer to the hardware structure
  628. * @blk: HW block
  629. * @ptype: the ptype to add or move
  630. * @ptg: the PTG to add or move the ptype to
  631. *
  632. * This function will either add or move a ptype to a particular PTG depending
  633. * on if the ptype is already part of another group. Note that using a
  634. * destination PTG ID of ICE_DEFAULT_PTG (0) will move the ptype to the
  635. * default PTG.
  636. */
  637. static int
  638. ice_ptg_add_mv_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
  639. {
  640. u8 original_ptg;
  641. int status;
  642. if (ptype > ICE_XLT1_CNT - 1)
  643. return -EINVAL;
  644. if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use && ptg != ICE_DEFAULT_PTG)
  645. return -ENOENT;
  646. status = ice_ptg_find_ptype(hw, blk, ptype, &original_ptg);
  647. if (status)
  648. return status;
  649. /* Is ptype already in the correct PTG? */
  650. if (original_ptg == ptg)
  651. return 0;
  652. /* Remove from original PTG and move back to the default PTG */
  653. if (original_ptg != ICE_DEFAULT_PTG)
  654. ice_ptg_remove_ptype(hw, blk, ptype, original_ptg);
  655. /* Moving to default PTG? Then we're done with this request */
  656. if (ptg == ICE_DEFAULT_PTG)
  657. return 0;
  658. /* Add ptype to PTG at beginning of list */
  659. hw->blk[blk].xlt1.ptypes[ptype].next_ptype =
  660. hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
  661. hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype =
  662. &hw->blk[blk].xlt1.ptypes[ptype];
  663. hw->blk[blk].xlt1.ptypes[ptype].ptg = ptg;
  664. hw->blk[blk].xlt1.t[ptype] = ptg;
  665. return 0;
  666. }
  667. /* Block / table size info */
  668. struct ice_blk_size_details {
  669. u16 xlt1; /* # XLT1 entries */
  670. u16 xlt2; /* # XLT2 entries */
  671. u16 prof_tcam; /* # profile ID TCAM entries */
  672. u16 prof_id; /* # profile IDs */
  673. u8 prof_cdid_bits; /* # CDID one-hot bits used in key */
  674. u16 prof_redir; /* # profile redirection entries */
  675. u16 es; /* # extraction sequence entries */
  676. u16 fvw; /* # field vector words */
  677. u8 overwrite; /* overwrite existing entries allowed */
  678. u8 reverse; /* reverse FV order */
  679. };
  680. static const struct ice_blk_size_details blk_sizes[ICE_BLK_COUNT] = {
  681. /**
  682. * Table Definitions
  683. * XLT1 - Number of entries in XLT1 table
  684. * XLT2 - Number of entries in XLT2 table
  685. * TCAM - Number of entries Profile ID TCAM table
  686. * CDID - Control Domain ID of the hardware block
  687. * PRED - Number of entries in the Profile Redirection Table
  688. * FV - Number of entries in the Field Vector
  689. * FVW - Width (in WORDs) of the Field Vector
  690. * OVR - Overwrite existing table entries
  691. * REV - Reverse FV
  692. */
  693. /* XLT1 , XLT2 ,TCAM, PID,CDID,PRED, FV, FVW */
  694. /* Overwrite , Reverse FV */
  695. /* SW */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 256, 0, 256, 256, 48,
  696. false, false },
  697. /* ACL */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128, 0, 128, 128, 32,
  698. false, false },
  699. /* FD */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128, 0, 128, 128, 24,
  700. false, true },
  701. /* RSS */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128, 0, 128, 128, 24,
  702. true, true },
  703. /* PE */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 64, 32, 0, 32, 32, 24,
  704. false, false },
  705. };
  706. enum ice_sid_all {
  707. ICE_SID_XLT1_OFF = 0,
  708. ICE_SID_XLT2_OFF,
  709. ICE_SID_PR_OFF,
  710. ICE_SID_PR_REDIR_OFF,
  711. ICE_SID_ES_OFF,
  712. ICE_SID_OFF_COUNT,
  713. };
  714. /* Characteristic handling */
  715. /**
  716. * ice_match_prop_lst - determine if properties of two lists match
  717. * @list1: first properties list
  718. * @list2: second properties list
  719. *
  720. * Count, cookies and the order must match in order to be considered equivalent.
  721. */
  722. static bool
  723. ice_match_prop_lst(struct list_head *list1, struct list_head *list2)
  724. {
  725. struct ice_vsig_prof *tmp1;
  726. struct ice_vsig_prof *tmp2;
  727. u16 chk_count = 0;
  728. u16 count = 0;
  729. /* compare counts */
  730. list_for_each_entry(tmp1, list1, list)
  731. count++;
  732. list_for_each_entry(tmp2, list2, list)
  733. chk_count++;
  734. if (!count || count != chk_count)
  735. return false;
  736. tmp1 = list_first_entry(list1, struct ice_vsig_prof, list);
  737. tmp2 = list_first_entry(list2, struct ice_vsig_prof, list);
  738. /* profile cookies must compare, and in the exact same order to take
  739. * into account priority
  740. */
  741. while (count--) {
  742. if (tmp2->profile_cookie != tmp1->profile_cookie)
  743. return false;
  744. tmp1 = list_next_entry(tmp1, list);
  745. tmp2 = list_next_entry(tmp2, list);
  746. }
  747. return true;
  748. }
  749. /* VSIG Management */
  750. /**
  751. * ice_vsig_find_vsi - find a VSIG that contains a specified VSI
  752. * @hw: pointer to the hardware structure
  753. * @blk: HW block
  754. * @vsi: VSI of interest
  755. * @vsig: pointer to receive the VSI group
  756. *
  757. * This function will lookup the VSI entry in the XLT2 list and return
  758. * the VSI group its associated with.
  759. */
  760. static int
  761. ice_vsig_find_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 *vsig)
  762. {
  763. if (!vsig || vsi >= ICE_MAX_VSI)
  764. return -EINVAL;
  765. /* As long as there's a default or valid VSIG associated with the input
  766. * VSI, the functions returns a success. Any handling of VSIG will be
  767. * done by the following add, update or remove functions.
  768. */
  769. *vsig = hw->blk[blk].xlt2.vsis[vsi].vsig;
  770. return 0;
  771. }
  772. /**
  773. * ice_vsig_alloc_val - allocate a new VSIG by value
  774. * @hw: pointer to the hardware structure
  775. * @blk: HW block
  776. * @vsig: the VSIG to allocate
  777. *
  778. * This function will allocate a given VSIG specified by the VSIG parameter.
  779. */
  780. static u16 ice_vsig_alloc_val(struct ice_hw *hw, enum ice_block blk, u16 vsig)
  781. {
  782. u16 idx = vsig & ICE_VSIG_IDX_M;
  783. if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) {
  784. INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
  785. hw->blk[blk].xlt2.vsig_tbl[idx].in_use = true;
  786. }
  787. return ICE_VSIG_VALUE(idx, hw->pf_id);
  788. }
  789. /**
  790. * ice_vsig_alloc - Finds a free entry and allocates a new VSIG
  791. * @hw: pointer to the hardware structure
  792. * @blk: HW block
  793. *
  794. * This function will iterate through the VSIG list and mark the first
  795. * unused entry for the new VSIG entry as used and return that value.
  796. */
  797. static u16 ice_vsig_alloc(struct ice_hw *hw, enum ice_block blk)
  798. {
  799. u16 i;
  800. for (i = 1; i < ICE_MAX_VSIGS; i++)
  801. if (!hw->blk[blk].xlt2.vsig_tbl[i].in_use)
  802. return ice_vsig_alloc_val(hw, blk, i);
  803. return ICE_DEFAULT_VSIG;
  804. }
  805. /**
  806. * ice_find_dup_props_vsig - find VSI group with a specified set of properties
  807. * @hw: pointer to the hardware structure
  808. * @blk: HW block
  809. * @chs: characteristic list
  810. * @vsig: returns the VSIG with the matching profiles, if found
  811. *
  812. * Each VSIG is associated with a characteristic set; i.e. all VSIs under
  813. * a group have the same characteristic set. To check if there exists a VSIG
  814. * which has the same characteristics as the input characteristics; this
  815. * function will iterate through the XLT2 list and return the VSIG that has a
  816. * matching configuration. In order to make sure that priorities are accounted
  817. * for, the list must match exactly, including the order in which the
  818. * characteristics are listed.
  819. */
  820. static int
  821. ice_find_dup_props_vsig(struct ice_hw *hw, enum ice_block blk,
  822. struct list_head *chs, u16 *vsig)
  823. {
  824. struct ice_xlt2 *xlt2 = &hw->blk[blk].xlt2;
  825. u16 i;
  826. for (i = 0; i < xlt2->count; i++)
  827. if (xlt2->vsig_tbl[i].in_use &&
  828. ice_match_prop_lst(chs, &xlt2->vsig_tbl[i].prop_lst)) {
  829. *vsig = ICE_VSIG_VALUE(i, hw->pf_id);
  830. return 0;
  831. }
  832. return -ENOENT;
  833. }
  834. /**
  835. * ice_vsig_free - free VSI group
  836. * @hw: pointer to the hardware structure
  837. * @blk: HW block
  838. * @vsig: VSIG to remove
  839. *
  840. * The function will remove all VSIs associated with the input VSIG and move
  841. * them to the DEFAULT_VSIG and mark the VSIG available.
  842. */
  843. static int ice_vsig_free(struct ice_hw *hw, enum ice_block blk, u16 vsig)
  844. {
  845. struct ice_vsig_prof *dtmp, *del;
  846. struct ice_vsig_vsi *vsi_cur;
  847. u16 idx;
  848. idx = vsig & ICE_VSIG_IDX_M;
  849. if (idx >= ICE_MAX_VSIGS)
  850. return -EINVAL;
  851. if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
  852. return -ENOENT;
  853. hw->blk[blk].xlt2.vsig_tbl[idx].in_use = false;
  854. vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
  855. /* If the VSIG has at least 1 VSI then iterate through the
  856. * list and remove the VSIs before deleting the group.
  857. */
  858. if (vsi_cur) {
  859. /* remove all vsis associated with this VSIG XLT2 entry */
  860. do {
  861. struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
  862. vsi_cur->vsig = ICE_DEFAULT_VSIG;
  863. vsi_cur->changed = 1;
  864. vsi_cur->next_vsi = NULL;
  865. vsi_cur = tmp;
  866. } while (vsi_cur);
  867. /* NULL terminate head of VSI list */
  868. hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = NULL;
  869. }
  870. /* free characteristic list */
  871. list_for_each_entry_safe(del, dtmp,
  872. &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
  873. list) {
  874. list_del(&del->list);
  875. devm_kfree(ice_hw_to_dev(hw), del);
  876. }
  877. /* if VSIG characteristic list was cleared for reset
  878. * re-initialize the list head
  879. */
  880. INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
  881. return 0;
  882. }
  883. /**
  884. * ice_vsig_remove_vsi - remove VSI from VSIG
  885. * @hw: pointer to the hardware structure
  886. * @blk: HW block
  887. * @vsi: VSI to remove
  888. * @vsig: VSI group to remove from
  889. *
  890. * The function will remove the input VSI from its VSI group and move it
  891. * to the DEFAULT_VSIG.
  892. */
  893. static int
  894. ice_vsig_remove_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
  895. {
  896. struct ice_vsig_vsi **vsi_head, *vsi_cur, *vsi_tgt;
  897. u16 idx;
  898. idx = vsig & ICE_VSIG_IDX_M;
  899. if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
  900. return -EINVAL;
  901. if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
  902. return -ENOENT;
  903. /* entry already in default VSIG, don't have to remove */
  904. if (idx == ICE_DEFAULT_VSIG)
  905. return 0;
  906. vsi_head = &hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
  907. if (!(*vsi_head))
  908. return -EIO;
  909. vsi_tgt = &hw->blk[blk].xlt2.vsis[vsi];
  910. vsi_cur = (*vsi_head);
  911. /* iterate the VSI list, skip over the entry to be removed */
  912. while (vsi_cur) {
  913. if (vsi_tgt == vsi_cur) {
  914. (*vsi_head) = vsi_cur->next_vsi;
  915. break;
  916. }
  917. vsi_head = &vsi_cur->next_vsi;
  918. vsi_cur = vsi_cur->next_vsi;
  919. }
  920. /* verify if VSI was removed from group list */
  921. if (!vsi_cur)
  922. return -ENOENT;
  923. vsi_cur->vsig = ICE_DEFAULT_VSIG;
  924. vsi_cur->changed = 1;
  925. vsi_cur->next_vsi = NULL;
  926. return 0;
  927. }
  928. /**
  929. * ice_vsig_add_mv_vsi - add or move a VSI to a VSI group
  930. * @hw: pointer to the hardware structure
  931. * @blk: HW block
  932. * @vsi: VSI to move
  933. * @vsig: destination VSI group
  934. *
  935. * This function will move or add the input VSI to the target VSIG.
  936. * The function will find the original VSIG the VSI belongs to and
  937. * move the entry to the DEFAULT_VSIG, update the original VSIG and
  938. * then move entry to the new VSIG.
  939. */
  940. static int
  941. ice_vsig_add_mv_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
  942. {
  943. struct ice_vsig_vsi *tmp;
  944. u16 orig_vsig, idx;
  945. int status;
  946. idx = vsig & ICE_VSIG_IDX_M;
  947. if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
  948. return -EINVAL;
  949. /* if VSIG not in use and VSIG is not default type this VSIG
  950. * doesn't exist.
  951. */
  952. if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use &&
  953. vsig != ICE_DEFAULT_VSIG)
  954. return -ENOENT;
  955. status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
  956. if (status)
  957. return status;
  958. /* no update required if vsigs match */
  959. if (orig_vsig == vsig)
  960. return 0;
  961. if (orig_vsig != ICE_DEFAULT_VSIG) {
  962. /* remove entry from orig_vsig and add to default VSIG */
  963. status = ice_vsig_remove_vsi(hw, blk, vsi, orig_vsig);
  964. if (status)
  965. return status;
  966. }
  967. if (idx == ICE_DEFAULT_VSIG)
  968. return 0;
  969. /* Create VSI entry and add VSIG and prop_mask values */
  970. hw->blk[blk].xlt2.vsis[vsi].vsig = vsig;
  971. hw->blk[blk].xlt2.vsis[vsi].changed = 1;
  972. /* Add new entry to the head of the VSIG list */
  973. tmp = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
  974. hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi =
  975. &hw->blk[blk].xlt2.vsis[vsi];
  976. hw->blk[blk].xlt2.vsis[vsi].next_vsi = tmp;
  977. hw->blk[blk].xlt2.t[vsi] = vsig;
  978. return 0;
  979. }
  980. /**
  981. * ice_prof_has_mask_idx - determine if profile index masking is identical
  982. * @hw: pointer to the hardware structure
  983. * @blk: HW block
  984. * @prof: profile to check
  985. * @idx: profile index to check
  986. * @mask: mask to match
  987. */
  988. static bool
  989. ice_prof_has_mask_idx(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 idx,
  990. u16 mask)
  991. {
  992. bool expect_no_mask = false;
  993. bool found = false;
  994. bool match = false;
  995. u16 i;
  996. /* If mask is 0x0000 or 0xffff, then there is no masking */
  997. if (mask == 0 || mask == 0xffff)
  998. expect_no_mask = true;
  999. /* Scan the enabled masks on this profile, for the specified idx */
  1000. for (i = hw->blk[blk].masks.first; i < hw->blk[blk].masks.first +
  1001. hw->blk[blk].masks.count; i++)
  1002. if (hw->blk[blk].es.mask_ena[prof] & BIT(i))
  1003. if (hw->blk[blk].masks.masks[i].in_use &&
  1004. hw->blk[blk].masks.masks[i].idx == idx) {
  1005. found = true;
  1006. if (hw->blk[blk].masks.masks[i].mask == mask)
  1007. match = true;
  1008. break;
  1009. }
  1010. if (expect_no_mask) {
  1011. if (found)
  1012. return false;
  1013. } else {
  1014. if (!match)
  1015. return false;
  1016. }
  1017. return true;
  1018. }
  1019. /**
  1020. * ice_prof_has_mask - determine if profile masking is identical
  1021. * @hw: pointer to the hardware structure
  1022. * @blk: HW block
  1023. * @prof: profile to check
  1024. * @masks: masks to match
  1025. */
  1026. static bool
  1027. ice_prof_has_mask(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 *masks)
  1028. {
  1029. u16 i;
  1030. /* es->mask_ena[prof] will have the mask */
  1031. for (i = 0; i < hw->blk[blk].es.fvw; i++)
  1032. if (!ice_prof_has_mask_idx(hw, blk, prof, i, masks[i]))
  1033. return false;
  1034. return true;
  1035. }
  1036. /**
  1037. * ice_find_prof_id_with_mask - find profile ID for a given field vector
  1038. * @hw: pointer to the hardware structure
  1039. * @blk: HW block
  1040. * @fv: field vector to search for
  1041. * @masks: masks for FV
  1042. * @symm: symmetric setting for RSS flows
  1043. * @prof_id: receives the profile ID
  1044. */
  1045. static int
  1046. ice_find_prof_id_with_mask(struct ice_hw *hw, enum ice_block blk,
  1047. struct ice_fv_word *fv, u16 *masks, bool symm,
  1048. u8 *prof_id)
  1049. {
  1050. struct ice_es *es = &hw->blk[blk].es;
  1051. u8 i;
  1052. /* For FD, we don't want to re-use a existed profile with the same
  1053. * field vector and mask. This will cause rule interference.
  1054. */
  1055. if (blk == ICE_BLK_FD)
  1056. return -ENOENT;
  1057. for (i = 0; i < (u8)es->count; i++) {
  1058. u16 off = i * es->fvw;
  1059. if (blk == ICE_BLK_RSS && es->symm[i] != symm)
  1060. continue;
  1061. if (memcmp(&es->t[off], fv, es->fvw * sizeof(*fv)))
  1062. continue;
  1063. /* check if masks settings are the same for this profile */
  1064. if (masks && !ice_prof_has_mask(hw, blk, i, masks))
  1065. continue;
  1066. *prof_id = i;
  1067. return 0;
  1068. }
  1069. return -ENOENT;
  1070. }
  1071. /**
  1072. * ice_prof_id_rsrc_type - get profile ID resource type for a block type
  1073. * @blk: the block type
  1074. * @rsrc_type: pointer to variable to receive the resource type
  1075. */
  1076. static bool ice_prof_id_rsrc_type(enum ice_block blk, u16 *rsrc_type)
  1077. {
  1078. switch (blk) {
  1079. case ICE_BLK_FD:
  1080. *rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_PROFID;
  1081. break;
  1082. case ICE_BLK_RSS:
  1083. *rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_PROFID;
  1084. break;
  1085. default:
  1086. return false;
  1087. }
  1088. return true;
  1089. }
  1090. /**
  1091. * ice_tcam_ent_rsrc_type - get TCAM entry resource type for a block type
  1092. * @blk: the block type
  1093. * @rsrc_type: pointer to variable to receive the resource type
  1094. */
  1095. static bool ice_tcam_ent_rsrc_type(enum ice_block blk, u16 *rsrc_type)
  1096. {
  1097. switch (blk) {
  1098. case ICE_BLK_FD:
  1099. *rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_TCAM;
  1100. break;
  1101. case ICE_BLK_RSS:
  1102. *rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_TCAM;
  1103. break;
  1104. default:
  1105. return false;
  1106. }
  1107. return true;
  1108. }
  1109. /**
  1110. * ice_alloc_tcam_ent - allocate hardware TCAM entry
  1111. * @hw: pointer to the HW struct
  1112. * @blk: the block to allocate the TCAM for
  1113. * @btm: true to allocate from bottom of table, false to allocate from top
  1114. * @tcam_idx: pointer to variable to receive the TCAM entry
  1115. *
  1116. * This function allocates a new entry in a Profile ID TCAM for a specific
  1117. * block.
  1118. */
  1119. static int
  1120. ice_alloc_tcam_ent(struct ice_hw *hw, enum ice_block blk, bool btm,
  1121. u16 *tcam_idx)
  1122. {
  1123. u16 res_type;
  1124. if (!ice_tcam_ent_rsrc_type(blk, &res_type))
  1125. return -EINVAL;
  1126. return ice_alloc_hw_res(hw, res_type, 1, btm, tcam_idx);
  1127. }
  1128. /**
  1129. * ice_free_tcam_ent - free hardware TCAM entry
  1130. * @hw: pointer to the HW struct
  1131. * @blk: the block from which to free the TCAM entry
  1132. * @tcam_idx: the TCAM entry to free
  1133. *
  1134. * This function frees an entry in a Profile ID TCAM for a specific block.
  1135. */
  1136. static int
  1137. ice_free_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 tcam_idx)
  1138. {
  1139. u16 res_type;
  1140. if (!ice_tcam_ent_rsrc_type(blk, &res_type))
  1141. return -EINVAL;
  1142. return ice_free_hw_res(hw, res_type, 1, &tcam_idx);
  1143. }
  1144. /**
  1145. * ice_alloc_prof_id - allocate profile ID
  1146. * @hw: pointer to the HW struct
  1147. * @blk: the block to allocate the profile ID for
  1148. * @prof_id: pointer to variable to receive the profile ID
  1149. *
  1150. * This function allocates a new profile ID, which also corresponds to a Field
  1151. * Vector (Extraction Sequence) entry.
  1152. */
  1153. static int ice_alloc_prof_id(struct ice_hw *hw, enum ice_block blk, u8 *prof_id)
  1154. {
  1155. u16 res_type;
  1156. u16 get_prof;
  1157. int status;
  1158. if (!ice_prof_id_rsrc_type(blk, &res_type))
  1159. return -EINVAL;
  1160. status = ice_alloc_hw_res(hw, res_type, 1, false, &get_prof);
  1161. if (!status)
  1162. *prof_id = (u8)get_prof;
  1163. return status;
  1164. }
  1165. /**
  1166. * ice_free_prof_id - free profile ID
  1167. * @hw: pointer to the HW struct
  1168. * @blk: the block from which to free the profile ID
  1169. * @prof_id: the profile ID to free
  1170. *
  1171. * This function frees a profile ID, which also corresponds to a Field Vector.
  1172. */
  1173. static int ice_free_prof_id(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
  1174. {
  1175. u16 tmp_prof_id = (u16)prof_id;
  1176. u16 res_type;
  1177. if (!ice_prof_id_rsrc_type(blk, &res_type))
  1178. return -EINVAL;
  1179. return ice_free_hw_res(hw, res_type, 1, &tmp_prof_id);
  1180. }
  1181. /**
  1182. * ice_prof_inc_ref - increment reference count for profile
  1183. * @hw: pointer to the HW struct
  1184. * @blk: the block from which to free the profile ID
  1185. * @prof_id: the profile ID for which to increment the reference count
  1186. */
  1187. static int ice_prof_inc_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
  1188. {
  1189. if (prof_id > hw->blk[blk].es.count)
  1190. return -EINVAL;
  1191. hw->blk[blk].es.ref_count[prof_id]++;
  1192. return 0;
  1193. }
  1194. /**
  1195. * ice_write_prof_mask_reg - write profile mask register
  1196. * @hw: pointer to the HW struct
  1197. * @blk: hardware block
  1198. * @mask_idx: mask index
  1199. * @idx: index of the FV which will use the mask
  1200. * @mask: the 16-bit mask
  1201. */
  1202. static void
  1203. ice_write_prof_mask_reg(struct ice_hw *hw, enum ice_block blk, u16 mask_idx,
  1204. u16 idx, u16 mask)
  1205. {
  1206. u32 offset;
  1207. u32 val;
  1208. switch (blk) {
  1209. case ICE_BLK_RSS:
  1210. offset = GLQF_HMASK(mask_idx);
  1211. val = FIELD_PREP(GLQF_HMASK_MSK_INDEX_M, idx);
  1212. val |= FIELD_PREP(GLQF_HMASK_MASK_M, mask);
  1213. break;
  1214. case ICE_BLK_FD:
  1215. offset = GLQF_FDMASK(mask_idx);
  1216. val = FIELD_PREP(GLQF_FDMASK_MSK_INDEX_M, idx);
  1217. val |= FIELD_PREP(GLQF_FDMASK_MASK_M, mask);
  1218. break;
  1219. default:
  1220. ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
  1221. blk);
  1222. return;
  1223. }
  1224. wr32(hw, offset, val);
  1225. ice_debug(hw, ICE_DBG_PKG, "write mask, blk %d (%d): %x = %x\n",
  1226. blk, idx, offset, val);
  1227. }
  1228. /**
  1229. * ice_write_prof_mask_enable_res - write profile mask enable register
  1230. * @hw: pointer to the HW struct
  1231. * @blk: hardware block
  1232. * @prof_id: profile ID
  1233. * @enable_mask: enable mask
  1234. */
  1235. static void
  1236. ice_write_prof_mask_enable_res(struct ice_hw *hw, enum ice_block blk,
  1237. u16 prof_id, u32 enable_mask)
  1238. {
  1239. u32 offset;
  1240. switch (blk) {
  1241. case ICE_BLK_RSS:
  1242. offset = GLQF_HMASK_SEL(prof_id);
  1243. break;
  1244. case ICE_BLK_FD:
  1245. offset = GLQF_FDMASK_SEL(prof_id);
  1246. break;
  1247. default:
  1248. ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
  1249. blk);
  1250. return;
  1251. }
  1252. wr32(hw, offset, enable_mask);
  1253. ice_debug(hw, ICE_DBG_PKG, "write mask enable, blk %d (%d): %x = %x\n",
  1254. blk, prof_id, offset, enable_mask);
  1255. }
  1256. /**
  1257. * ice_init_prof_masks - initial prof masks
  1258. * @hw: pointer to the HW struct
  1259. * @blk: hardware block
  1260. */
  1261. static void ice_init_prof_masks(struct ice_hw *hw, enum ice_block blk)
  1262. {
  1263. u16 per_pf;
  1264. u16 i;
  1265. mutex_init(&hw->blk[blk].masks.lock);
  1266. per_pf = ICE_PROF_MASK_COUNT / hw->dev_caps.num_funcs;
  1267. hw->blk[blk].masks.count = per_pf;
  1268. hw->blk[blk].masks.first = hw->pf_id * per_pf;
  1269. memset(hw->blk[blk].masks.masks, 0, sizeof(hw->blk[blk].masks.masks));
  1270. for (i = hw->blk[blk].masks.first;
  1271. i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
  1272. ice_write_prof_mask_reg(hw, blk, i, 0, 0);
  1273. }
  1274. /**
  1275. * ice_init_all_prof_masks - initialize all prof masks
  1276. * @hw: pointer to the HW struct
  1277. */
  1278. static void ice_init_all_prof_masks(struct ice_hw *hw)
  1279. {
  1280. ice_init_prof_masks(hw, ICE_BLK_RSS);
  1281. ice_init_prof_masks(hw, ICE_BLK_FD);
  1282. }
  1283. /**
  1284. * ice_alloc_prof_mask - allocate profile mask
  1285. * @hw: pointer to the HW struct
  1286. * @blk: hardware block
  1287. * @idx: index of FV which will use the mask
  1288. * @mask: the 16-bit mask
  1289. * @mask_idx: variable to receive the mask index
  1290. */
  1291. static int
  1292. ice_alloc_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 idx, u16 mask,
  1293. u16 *mask_idx)
  1294. {
  1295. bool found_unused = false, found_copy = false;
  1296. u16 unused_idx = 0, copy_idx = 0;
  1297. int status = -ENOSPC;
  1298. u16 i;
  1299. if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
  1300. return -EINVAL;
  1301. mutex_lock(&hw->blk[blk].masks.lock);
  1302. for (i = hw->blk[blk].masks.first;
  1303. i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
  1304. if (hw->blk[blk].masks.masks[i].in_use) {
  1305. /* if mask is in use and it exactly duplicates the
  1306. * desired mask and index, then in can be reused
  1307. */
  1308. if (hw->blk[blk].masks.masks[i].mask == mask &&
  1309. hw->blk[blk].masks.masks[i].idx == idx) {
  1310. found_copy = true;
  1311. copy_idx = i;
  1312. break;
  1313. }
  1314. } else {
  1315. /* save off unused index, but keep searching in case
  1316. * there is an exact match later on
  1317. */
  1318. if (!found_unused) {
  1319. found_unused = true;
  1320. unused_idx = i;
  1321. }
  1322. }
  1323. if (found_copy)
  1324. i = copy_idx;
  1325. else if (found_unused)
  1326. i = unused_idx;
  1327. else
  1328. goto err_ice_alloc_prof_mask;
  1329. /* update mask for a new entry */
  1330. if (found_unused) {
  1331. hw->blk[blk].masks.masks[i].in_use = true;
  1332. hw->blk[blk].masks.masks[i].mask = mask;
  1333. hw->blk[blk].masks.masks[i].idx = idx;
  1334. hw->blk[blk].masks.masks[i].ref = 0;
  1335. ice_write_prof_mask_reg(hw, blk, i, idx, mask);
  1336. }
  1337. hw->blk[blk].masks.masks[i].ref++;
  1338. *mask_idx = i;
  1339. status = 0;
  1340. err_ice_alloc_prof_mask:
  1341. mutex_unlock(&hw->blk[blk].masks.lock);
  1342. return status;
  1343. }
  1344. /**
  1345. * ice_free_prof_mask - free profile mask
  1346. * @hw: pointer to the HW struct
  1347. * @blk: hardware block
  1348. * @mask_idx: index of mask
  1349. */
  1350. static int
  1351. ice_free_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 mask_idx)
  1352. {
  1353. if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
  1354. return -EINVAL;
  1355. if (!(mask_idx >= hw->blk[blk].masks.first &&
  1356. mask_idx < hw->blk[blk].masks.first + hw->blk[blk].masks.count))
  1357. return -ENOENT;
  1358. mutex_lock(&hw->blk[blk].masks.lock);
  1359. if (!hw->blk[blk].masks.masks[mask_idx].in_use)
  1360. goto exit_ice_free_prof_mask;
  1361. if (hw->blk[blk].masks.masks[mask_idx].ref > 1) {
  1362. hw->blk[blk].masks.masks[mask_idx].ref--;
  1363. goto exit_ice_free_prof_mask;
  1364. }
  1365. /* remove mask */
  1366. hw->blk[blk].masks.masks[mask_idx].in_use = false;
  1367. hw->blk[blk].masks.masks[mask_idx].mask = 0;
  1368. hw->blk[blk].masks.masks[mask_idx].idx = 0;
  1369. /* update mask as unused entry */
  1370. ice_debug(hw, ICE_DBG_PKG, "Free mask, blk %d, mask %d\n", blk,
  1371. mask_idx);
  1372. ice_write_prof_mask_reg(hw, blk, mask_idx, 0, 0);
  1373. exit_ice_free_prof_mask:
  1374. mutex_unlock(&hw->blk[blk].masks.lock);
  1375. return 0;
  1376. }
  1377. /**
  1378. * ice_free_prof_masks - free all profile masks for a profile
  1379. * @hw: pointer to the HW struct
  1380. * @blk: hardware block
  1381. * @prof_id: profile ID
  1382. */
  1383. static int
  1384. ice_free_prof_masks(struct ice_hw *hw, enum ice_block blk, u16 prof_id)
  1385. {
  1386. u32 mask_bm;
  1387. u16 i;
  1388. if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
  1389. return -EINVAL;
  1390. mask_bm = hw->blk[blk].es.mask_ena[prof_id];
  1391. for (i = 0; i < BITS_PER_BYTE * sizeof(mask_bm); i++)
  1392. if (mask_bm & BIT(i))
  1393. ice_free_prof_mask(hw, blk, i);
  1394. return 0;
  1395. }
  1396. /**
  1397. * ice_shutdown_prof_masks - releases lock for masking
  1398. * @hw: pointer to the HW struct
  1399. * @blk: hardware block
  1400. *
  1401. * This should be called before unloading the driver
  1402. */
  1403. static void ice_shutdown_prof_masks(struct ice_hw *hw, enum ice_block blk)
  1404. {
  1405. u16 i;
  1406. mutex_lock(&hw->blk[blk].masks.lock);
  1407. for (i = hw->blk[blk].masks.first;
  1408. i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) {
  1409. ice_write_prof_mask_reg(hw, blk, i, 0, 0);
  1410. hw->blk[blk].masks.masks[i].in_use = false;
  1411. hw->blk[blk].masks.masks[i].idx = 0;
  1412. hw->blk[blk].masks.masks[i].mask = 0;
  1413. }
  1414. mutex_unlock(&hw->blk[blk].masks.lock);
  1415. mutex_destroy(&hw->blk[blk].masks.lock);
  1416. }
  1417. /**
  1418. * ice_shutdown_all_prof_masks - releases all locks for masking
  1419. * @hw: pointer to the HW struct
  1420. *
  1421. * This should be called before unloading the driver
  1422. */
  1423. static void ice_shutdown_all_prof_masks(struct ice_hw *hw)
  1424. {
  1425. ice_shutdown_prof_masks(hw, ICE_BLK_RSS);
  1426. ice_shutdown_prof_masks(hw, ICE_BLK_FD);
  1427. }
  1428. /**
  1429. * ice_update_prof_masking - set registers according to masking
  1430. * @hw: pointer to the HW struct
  1431. * @blk: hardware block
  1432. * @prof_id: profile ID
  1433. * @masks: masks
  1434. */
  1435. static int
  1436. ice_update_prof_masking(struct ice_hw *hw, enum ice_block blk, u16 prof_id,
  1437. u16 *masks)
  1438. {
  1439. bool err = false;
  1440. u32 ena_mask = 0;
  1441. u16 idx;
  1442. u16 i;
  1443. /* Only support FD and RSS masking, otherwise nothing to be done */
  1444. if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
  1445. return 0;
  1446. for (i = 0; i < hw->blk[blk].es.fvw; i++)
  1447. if (masks[i] && masks[i] != 0xFFFF) {
  1448. if (!ice_alloc_prof_mask(hw, blk, i, masks[i], &idx)) {
  1449. ena_mask |= BIT(idx);
  1450. } else {
  1451. /* not enough bitmaps */
  1452. err = true;
  1453. break;
  1454. }
  1455. }
  1456. if (err) {
  1457. /* free any bitmaps we have allocated */
  1458. for (i = 0; i < BITS_PER_BYTE * sizeof(ena_mask); i++)
  1459. if (ena_mask & BIT(i))
  1460. ice_free_prof_mask(hw, blk, i);
  1461. return -EIO;
  1462. }
  1463. /* enable the masks for this profile */
  1464. ice_write_prof_mask_enable_res(hw, blk, prof_id, ena_mask);
  1465. /* store enabled masks with profile so that they can be freed later */
  1466. hw->blk[blk].es.mask_ena[prof_id] = ena_mask;
  1467. return 0;
  1468. }
  1469. /**
  1470. * ice_write_es - write an extraction sequence and symmetric setting to hardware
  1471. * @hw: pointer to the HW struct
  1472. * @blk: the block in which to write the extraction sequence
  1473. * @prof_id: the profile ID to write
  1474. * @fv: pointer to the extraction sequence to write - NULL to clear extraction
  1475. * @symm: symmetric setting for RSS profiles
  1476. */
  1477. static void
  1478. ice_write_es(struct ice_hw *hw, enum ice_block blk, u8 prof_id,
  1479. struct ice_fv_word *fv, bool symm)
  1480. {
  1481. u16 off;
  1482. off = prof_id * hw->blk[blk].es.fvw;
  1483. if (!fv) {
  1484. memset(&hw->blk[blk].es.t[off], 0,
  1485. hw->blk[blk].es.fvw * sizeof(*fv));
  1486. hw->blk[blk].es.written[prof_id] = false;
  1487. } else {
  1488. memcpy(&hw->blk[blk].es.t[off], fv,
  1489. hw->blk[blk].es.fvw * sizeof(*fv));
  1490. }
  1491. if (blk == ICE_BLK_RSS)
  1492. hw->blk[blk].es.symm[prof_id] = symm;
  1493. }
  1494. /**
  1495. * ice_prof_dec_ref - decrement reference count for profile
  1496. * @hw: pointer to the HW struct
  1497. * @blk: the block from which to free the profile ID
  1498. * @prof_id: the profile ID for which to decrement the reference count
  1499. */
  1500. static int
  1501. ice_prof_dec_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
  1502. {
  1503. if (prof_id > hw->blk[blk].es.count)
  1504. return -EINVAL;
  1505. if (hw->blk[blk].es.ref_count[prof_id] > 0) {
  1506. if (!--hw->blk[blk].es.ref_count[prof_id]) {
  1507. ice_write_es(hw, blk, prof_id, NULL, false);
  1508. ice_free_prof_masks(hw, blk, prof_id);
  1509. return ice_free_prof_id(hw, blk, prof_id);
  1510. }
  1511. }
  1512. return 0;
  1513. }
  1514. /* Block / table section IDs */
  1515. static const u32 ice_blk_sids[ICE_BLK_COUNT][ICE_SID_OFF_COUNT] = {
  1516. /* SWITCH */
  1517. { ICE_SID_XLT1_SW,
  1518. ICE_SID_XLT2_SW,
  1519. ICE_SID_PROFID_TCAM_SW,
  1520. ICE_SID_PROFID_REDIR_SW,
  1521. ICE_SID_FLD_VEC_SW
  1522. },
  1523. /* ACL */
  1524. { ICE_SID_XLT1_ACL,
  1525. ICE_SID_XLT2_ACL,
  1526. ICE_SID_PROFID_TCAM_ACL,
  1527. ICE_SID_PROFID_REDIR_ACL,
  1528. ICE_SID_FLD_VEC_ACL
  1529. },
  1530. /* FD */
  1531. { ICE_SID_XLT1_FD,
  1532. ICE_SID_XLT2_FD,
  1533. ICE_SID_PROFID_TCAM_FD,
  1534. ICE_SID_PROFID_REDIR_FD,
  1535. ICE_SID_FLD_VEC_FD
  1536. },
  1537. /* RSS */
  1538. { ICE_SID_XLT1_RSS,
  1539. ICE_SID_XLT2_RSS,
  1540. ICE_SID_PROFID_TCAM_RSS,
  1541. ICE_SID_PROFID_REDIR_RSS,
  1542. ICE_SID_FLD_VEC_RSS
  1543. },
  1544. /* PE */
  1545. { ICE_SID_XLT1_PE,
  1546. ICE_SID_XLT2_PE,
  1547. ICE_SID_PROFID_TCAM_PE,
  1548. ICE_SID_PROFID_REDIR_PE,
  1549. ICE_SID_FLD_VEC_PE
  1550. }
  1551. };
  1552. /**
  1553. * ice_init_sw_xlt1_db - init software XLT1 database from HW tables
  1554. * @hw: pointer to the hardware structure
  1555. * @blk: the HW block to initialize
  1556. */
  1557. static void ice_init_sw_xlt1_db(struct ice_hw *hw, enum ice_block blk)
  1558. {
  1559. u16 pt;
  1560. for (pt = 0; pt < hw->blk[blk].xlt1.count; pt++) {
  1561. u8 ptg;
  1562. ptg = hw->blk[blk].xlt1.t[pt];
  1563. if (ptg != ICE_DEFAULT_PTG) {
  1564. ice_ptg_alloc_val(hw, blk, ptg);
  1565. ice_ptg_add_mv_ptype(hw, blk, pt, ptg);
  1566. }
  1567. }
  1568. }
  1569. /**
  1570. * ice_init_sw_xlt2_db - init software XLT2 database from HW tables
  1571. * @hw: pointer to the hardware structure
  1572. * @blk: the HW block to initialize
  1573. */
  1574. static void ice_init_sw_xlt2_db(struct ice_hw *hw, enum ice_block blk)
  1575. {
  1576. u16 vsi;
  1577. for (vsi = 0; vsi < hw->blk[blk].xlt2.count; vsi++) {
  1578. u16 vsig;
  1579. vsig = hw->blk[blk].xlt2.t[vsi];
  1580. if (vsig) {
  1581. ice_vsig_alloc_val(hw, blk, vsig);
  1582. ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
  1583. /* no changes at this time, since this has been
  1584. * initialized from the original package
  1585. */
  1586. hw->blk[blk].xlt2.vsis[vsi].changed = 0;
  1587. }
  1588. }
  1589. }
  1590. /**
  1591. * ice_init_sw_db - init software database from HW tables
  1592. * @hw: pointer to the hardware structure
  1593. */
  1594. static void ice_init_sw_db(struct ice_hw *hw)
  1595. {
  1596. u16 i;
  1597. for (i = 0; i < ICE_BLK_COUNT; i++) {
  1598. ice_init_sw_xlt1_db(hw, (enum ice_block)i);
  1599. ice_init_sw_xlt2_db(hw, (enum ice_block)i);
  1600. }
  1601. }
  1602. /**
  1603. * ice_fill_tbl - Reads content of a single table type into database
  1604. * @hw: pointer to the hardware structure
  1605. * @block_id: Block ID of the table to copy
  1606. * @sid: Section ID of the table to copy
  1607. *
  1608. * Will attempt to read the entire content of a given table of a single block
  1609. * into the driver database. We assume that the buffer will always
  1610. * be as large or larger than the data contained in the package. If
  1611. * this condition is not met, there is most likely an error in the package
  1612. * contents.
  1613. */
  1614. static void ice_fill_tbl(struct ice_hw *hw, enum ice_block block_id, u32 sid)
  1615. {
  1616. u32 dst_len, sect_len, offset = 0;
  1617. struct ice_prof_redir_section *pr;
  1618. struct ice_prof_id_section *pid;
  1619. struct ice_xlt1_section *xlt1;
  1620. struct ice_xlt2_section *xlt2;
  1621. struct ice_sw_fv_section *es;
  1622. struct ice_pkg_enum state;
  1623. u8 *src, *dst;
  1624. void *sect;
  1625. /* if the HW segment pointer is null then the first iteration of
  1626. * ice_pkg_enum_section() will fail. In this case the HW tables will
  1627. * not be filled and return success.
  1628. */
  1629. if (!hw->seg) {
  1630. ice_debug(hw, ICE_DBG_PKG, "hw->seg is NULL, tables are not filled\n");
  1631. return;
  1632. }
  1633. memset(&state, 0, sizeof(state));
  1634. sect = ice_pkg_enum_section(hw->seg, &state, sid);
  1635. while (sect) {
  1636. switch (sid) {
  1637. case ICE_SID_XLT1_SW:
  1638. case ICE_SID_XLT1_FD:
  1639. case ICE_SID_XLT1_RSS:
  1640. case ICE_SID_XLT1_ACL:
  1641. case ICE_SID_XLT1_PE:
  1642. xlt1 = sect;
  1643. src = xlt1->value;
  1644. sect_len = le16_to_cpu(xlt1->count) *
  1645. sizeof(*hw->blk[block_id].xlt1.t);
  1646. dst = hw->blk[block_id].xlt1.t;
  1647. dst_len = hw->blk[block_id].xlt1.count *
  1648. sizeof(*hw->blk[block_id].xlt1.t);
  1649. break;
  1650. case ICE_SID_XLT2_SW:
  1651. case ICE_SID_XLT2_FD:
  1652. case ICE_SID_XLT2_RSS:
  1653. case ICE_SID_XLT2_ACL:
  1654. case ICE_SID_XLT2_PE:
  1655. xlt2 = sect;
  1656. src = (__force u8 *)xlt2->value;
  1657. sect_len = le16_to_cpu(xlt2->count) *
  1658. sizeof(*hw->blk[block_id].xlt2.t);
  1659. dst = (u8 *)hw->blk[block_id].xlt2.t;
  1660. dst_len = hw->blk[block_id].xlt2.count *
  1661. sizeof(*hw->blk[block_id].xlt2.t);
  1662. break;
  1663. case ICE_SID_PROFID_TCAM_SW:
  1664. case ICE_SID_PROFID_TCAM_FD:
  1665. case ICE_SID_PROFID_TCAM_RSS:
  1666. case ICE_SID_PROFID_TCAM_ACL:
  1667. case ICE_SID_PROFID_TCAM_PE:
  1668. pid = sect;
  1669. src = (u8 *)pid->entry;
  1670. sect_len = le16_to_cpu(pid->count) *
  1671. sizeof(*hw->blk[block_id].prof.t);
  1672. dst = (u8 *)hw->blk[block_id].prof.t;
  1673. dst_len = hw->blk[block_id].prof.count *
  1674. sizeof(*hw->blk[block_id].prof.t);
  1675. break;
  1676. case ICE_SID_PROFID_REDIR_SW:
  1677. case ICE_SID_PROFID_REDIR_FD:
  1678. case ICE_SID_PROFID_REDIR_RSS:
  1679. case ICE_SID_PROFID_REDIR_ACL:
  1680. case ICE_SID_PROFID_REDIR_PE:
  1681. pr = sect;
  1682. src = pr->redir_value;
  1683. sect_len = le16_to_cpu(pr->count) *
  1684. sizeof(*hw->blk[block_id].prof_redir.t);
  1685. dst = hw->blk[block_id].prof_redir.t;
  1686. dst_len = hw->blk[block_id].prof_redir.count *
  1687. sizeof(*hw->blk[block_id].prof_redir.t);
  1688. break;
  1689. case ICE_SID_FLD_VEC_SW:
  1690. case ICE_SID_FLD_VEC_FD:
  1691. case ICE_SID_FLD_VEC_RSS:
  1692. case ICE_SID_FLD_VEC_ACL:
  1693. case ICE_SID_FLD_VEC_PE:
  1694. es = sect;
  1695. src = (u8 *)es->fv;
  1696. sect_len = (u32)(le16_to_cpu(es->count) *
  1697. hw->blk[block_id].es.fvw) *
  1698. sizeof(*hw->blk[block_id].es.t);
  1699. dst = (u8 *)hw->blk[block_id].es.t;
  1700. dst_len = (u32)(hw->blk[block_id].es.count *
  1701. hw->blk[block_id].es.fvw) *
  1702. sizeof(*hw->blk[block_id].es.t);
  1703. break;
  1704. default:
  1705. return;
  1706. }
  1707. /* if the section offset exceeds destination length, terminate
  1708. * table fill.
  1709. */
  1710. if (offset > dst_len)
  1711. return;
  1712. /* if the sum of section size and offset exceed destination size
  1713. * then we are out of bounds of the HW table size for that PF.
  1714. * Changing section length to fill the remaining table space
  1715. * of that PF.
  1716. */
  1717. if ((offset + sect_len) > dst_len)
  1718. sect_len = dst_len - offset;
  1719. memcpy(dst + offset, src, sect_len);
  1720. offset += sect_len;
  1721. sect = ice_pkg_enum_section(NULL, &state, sid);
  1722. }
  1723. }
  1724. /**
  1725. * ice_fill_blk_tbls - Read package context for tables
  1726. * @hw: pointer to the hardware structure
  1727. *
  1728. * Reads the current package contents and populates the driver
  1729. * database with the data iteratively for all advanced feature
  1730. * blocks. Assume that the HW tables have been allocated.
  1731. */
  1732. void ice_fill_blk_tbls(struct ice_hw *hw)
  1733. {
  1734. u8 i;
  1735. for (i = 0; i < ICE_BLK_COUNT; i++) {
  1736. enum ice_block blk_id = (enum ice_block)i;
  1737. ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt1.sid);
  1738. ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt2.sid);
  1739. ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof.sid);
  1740. ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof_redir.sid);
  1741. ice_fill_tbl(hw, blk_id, hw->blk[blk_id].es.sid);
  1742. }
  1743. ice_init_sw_db(hw);
  1744. }
  1745. /**
  1746. * ice_free_prof_map - free profile map
  1747. * @hw: pointer to the hardware structure
  1748. * @blk_idx: HW block index
  1749. */
  1750. static void ice_free_prof_map(struct ice_hw *hw, u8 blk_idx)
  1751. {
  1752. struct ice_es *es = &hw->blk[blk_idx].es;
  1753. struct ice_prof_map *del, *tmp;
  1754. mutex_lock(&es->prof_map_lock);
  1755. list_for_each_entry_safe(del, tmp, &es->prof_map, list) {
  1756. list_del(&del->list);
  1757. devm_kfree(ice_hw_to_dev(hw), del);
  1758. }
  1759. INIT_LIST_HEAD(&es->prof_map);
  1760. mutex_unlock(&es->prof_map_lock);
  1761. }
  1762. /**
  1763. * ice_free_flow_profs - free flow profile entries
  1764. * @hw: pointer to the hardware structure
  1765. * @blk_idx: HW block index
  1766. */
  1767. static void ice_free_flow_profs(struct ice_hw *hw, u8 blk_idx)
  1768. {
  1769. struct ice_flow_prof *p, *tmp;
  1770. mutex_lock(&hw->fl_profs_locks[blk_idx]);
  1771. list_for_each_entry_safe(p, tmp, &hw->fl_profs[blk_idx], l_entry) {
  1772. struct ice_flow_entry *e, *t;
  1773. list_for_each_entry_safe(e, t, &p->entries, l_entry)
  1774. ice_flow_rem_entry(hw, (enum ice_block)blk_idx,
  1775. ICE_FLOW_ENTRY_HNDL(e));
  1776. list_del(&p->l_entry);
  1777. mutex_destroy(&p->entries_lock);
  1778. devm_kfree(ice_hw_to_dev(hw), p);
  1779. }
  1780. mutex_unlock(&hw->fl_profs_locks[blk_idx]);
  1781. /* if driver is in reset and tables are being cleared
  1782. * re-initialize the flow profile list heads
  1783. */
  1784. INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
  1785. }
  1786. /**
  1787. * ice_free_vsig_tbl - free complete VSIG table entries
  1788. * @hw: pointer to the hardware structure
  1789. * @blk: the HW block on which to free the VSIG table entries
  1790. */
  1791. static void ice_free_vsig_tbl(struct ice_hw *hw, enum ice_block blk)
  1792. {
  1793. u16 i;
  1794. if (!hw->blk[blk].xlt2.vsig_tbl)
  1795. return;
  1796. for (i = 1; i < ICE_MAX_VSIGS; i++)
  1797. if (hw->blk[blk].xlt2.vsig_tbl[i].in_use)
  1798. ice_vsig_free(hw, blk, i);
  1799. }
  1800. /**
  1801. * ice_free_hw_tbls - free hardware table memory
  1802. * @hw: pointer to the hardware structure
  1803. */
  1804. void ice_free_hw_tbls(struct ice_hw *hw)
  1805. {
  1806. struct ice_rss_cfg *r, *rt;
  1807. u8 i;
  1808. for (i = 0; i < ICE_BLK_COUNT; i++) {
  1809. if (hw->blk[i].is_list_init) {
  1810. struct ice_es *es = &hw->blk[i].es;
  1811. ice_free_prof_map(hw, i);
  1812. mutex_destroy(&es->prof_map_lock);
  1813. ice_free_flow_profs(hw, i);
  1814. mutex_destroy(&hw->fl_profs_locks[i]);
  1815. hw->blk[i].is_list_init = false;
  1816. }
  1817. ice_free_vsig_tbl(hw, (enum ice_block)i);
  1818. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptypes);
  1819. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptg_tbl);
  1820. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.t);
  1821. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.t);
  1822. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsig_tbl);
  1823. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsis);
  1824. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof.t);
  1825. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_redir.t);
  1826. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.t);
  1827. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.ref_count);
  1828. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.symm);
  1829. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.written);
  1830. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.mask_ena);
  1831. devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_id.id);
  1832. }
  1833. list_for_each_entry_safe(r, rt, &hw->rss_list_head, l_entry) {
  1834. list_del(&r->l_entry);
  1835. devm_kfree(ice_hw_to_dev(hw), r);
  1836. }
  1837. mutex_destroy(&hw->rss_locks);
  1838. ice_shutdown_all_prof_masks(hw);
  1839. memset(hw->blk, 0, sizeof(hw->blk));
  1840. }
  1841. /**
  1842. * ice_init_flow_profs - init flow profile locks and list heads
  1843. * @hw: pointer to the hardware structure
  1844. * @blk_idx: HW block index
  1845. */
  1846. static void ice_init_flow_profs(struct ice_hw *hw, u8 blk_idx)
  1847. {
  1848. mutex_init(&hw->fl_profs_locks[blk_idx]);
  1849. INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
  1850. }
  1851. /**
  1852. * ice_clear_hw_tbls - clear HW tables and flow profiles
  1853. * @hw: pointer to the hardware structure
  1854. */
  1855. void ice_clear_hw_tbls(struct ice_hw *hw)
  1856. {
  1857. u8 i;
  1858. for (i = 0; i < ICE_BLK_COUNT; i++) {
  1859. struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
  1860. struct ice_prof_id *prof_id = &hw->blk[i].prof_id;
  1861. struct ice_prof_tcam *prof = &hw->blk[i].prof;
  1862. struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
  1863. struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
  1864. struct ice_es *es = &hw->blk[i].es;
  1865. if (hw->blk[i].is_list_init) {
  1866. ice_free_prof_map(hw, i);
  1867. ice_free_flow_profs(hw, i);
  1868. }
  1869. ice_free_vsig_tbl(hw, (enum ice_block)i);
  1870. memset(xlt1->ptypes, 0, xlt1->count * sizeof(*xlt1->ptypes));
  1871. memset(xlt1->ptg_tbl, 0,
  1872. ICE_MAX_PTGS * sizeof(*xlt1->ptg_tbl));
  1873. memset(xlt1->t, 0, xlt1->count * sizeof(*xlt1->t));
  1874. memset(xlt2->vsis, 0, xlt2->count * sizeof(*xlt2->vsis));
  1875. memset(xlt2->vsig_tbl, 0,
  1876. xlt2->count * sizeof(*xlt2->vsig_tbl));
  1877. memset(xlt2->t, 0, xlt2->count * sizeof(*xlt2->t));
  1878. memset(prof->t, 0, prof->count * sizeof(*prof->t));
  1879. memset(prof_redir->t, 0,
  1880. prof_redir->count * sizeof(*prof_redir->t));
  1881. memset(es->t, 0, es->count * sizeof(*es->t) * es->fvw);
  1882. memset(es->ref_count, 0, es->count * sizeof(*es->ref_count));
  1883. memset(es->symm, 0, es->count * sizeof(*es->symm));
  1884. memset(es->written, 0, es->count * sizeof(*es->written));
  1885. memset(es->mask_ena, 0, es->count * sizeof(*es->mask_ena));
  1886. memset(prof_id->id, 0, prof_id->count * sizeof(*prof_id->id));
  1887. }
  1888. }
  1889. /**
  1890. * ice_init_hw_tbls - init hardware table memory
  1891. * @hw: pointer to the hardware structure
  1892. */
  1893. int ice_init_hw_tbls(struct ice_hw *hw)
  1894. {
  1895. u8 i;
  1896. mutex_init(&hw->rss_locks);
  1897. INIT_LIST_HEAD(&hw->rss_list_head);
  1898. ice_init_all_prof_masks(hw);
  1899. for (i = 0; i < ICE_BLK_COUNT; i++) {
  1900. struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
  1901. struct ice_prof_id *prof_id = &hw->blk[i].prof_id;
  1902. struct ice_prof_tcam *prof = &hw->blk[i].prof;
  1903. struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
  1904. struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
  1905. struct ice_es *es = &hw->blk[i].es;
  1906. u16 j;
  1907. if (hw->blk[i].is_list_init)
  1908. continue;
  1909. ice_init_flow_profs(hw, i);
  1910. mutex_init(&es->prof_map_lock);
  1911. INIT_LIST_HEAD(&es->prof_map);
  1912. hw->blk[i].is_list_init = true;
  1913. hw->blk[i].overwrite = blk_sizes[i].overwrite;
  1914. es->reverse = blk_sizes[i].reverse;
  1915. xlt1->sid = ice_blk_sids[i][ICE_SID_XLT1_OFF];
  1916. xlt1->count = blk_sizes[i].xlt1;
  1917. xlt1->ptypes = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
  1918. sizeof(*xlt1->ptypes), GFP_KERNEL);
  1919. if (!xlt1->ptypes)
  1920. goto err;
  1921. xlt1->ptg_tbl = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_PTGS,
  1922. sizeof(*xlt1->ptg_tbl),
  1923. GFP_KERNEL);
  1924. if (!xlt1->ptg_tbl)
  1925. goto err;
  1926. xlt1->t = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
  1927. sizeof(*xlt1->t), GFP_KERNEL);
  1928. if (!xlt1->t)
  1929. goto err;
  1930. xlt2->sid = ice_blk_sids[i][ICE_SID_XLT2_OFF];
  1931. xlt2->count = blk_sizes[i].xlt2;
  1932. xlt2->vsis = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
  1933. sizeof(*xlt2->vsis), GFP_KERNEL);
  1934. if (!xlt2->vsis)
  1935. goto err;
  1936. xlt2->vsig_tbl = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
  1937. sizeof(*xlt2->vsig_tbl),
  1938. GFP_KERNEL);
  1939. if (!xlt2->vsig_tbl)
  1940. goto err;
  1941. for (j = 0; j < xlt2->count; j++)
  1942. INIT_LIST_HEAD(&xlt2->vsig_tbl[j].prop_lst);
  1943. xlt2->t = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
  1944. sizeof(*xlt2->t), GFP_KERNEL);
  1945. if (!xlt2->t)
  1946. goto err;
  1947. prof->sid = ice_blk_sids[i][ICE_SID_PR_OFF];
  1948. prof->count = blk_sizes[i].prof_tcam;
  1949. prof->max_prof_id = blk_sizes[i].prof_id;
  1950. prof->cdid_bits = blk_sizes[i].prof_cdid_bits;
  1951. prof->t = devm_kcalloc(ice_hw_to_dev(hw), prof->count,
  1952. sizeof(*prof->t), GFP_KERNEL);
  1953. if (!prof->t)
  1954. goto err;
  1955. prof_redir->sid = ice_blk_sids[i][ICE_SID_PR_REDIR_OFF];
  1956. prof_redir->count = blk_sizes[i].prof_redir;
  1957. prof_redir->t = devm_kcalloc(ice_hw_to_dev(hw),
  1958. prof_redir->count,
  1959. sizeof(*prof_redir->t),
  1960. GFP_KERNEL);
  1961. if (!prof_redir->t)
  1962. goto err;
  1963. es->sid = ice_blk_sids[i][ICE_SID_ES_OFF];
  1964. es->count = blk_sizes[i].es;
  1965. es->fvw = blk_sizes[i].fvw;
  1966. es->t = devm_kcalloc(ice_hw_to_dev(hw),
  1967. (u32)(es->count * es->fvw),
  1968. sizeof(*es->t), GFP_KERNEL);
  1969. if (!es->t)
  1970. goto err;
  1971. es->ref_count = devm_kcalloc(ice_hw_to_dev(hw), es->count,
  1972. sizeof(*es->ref_count),
  1973. GFP_KERNEL);
  1974. if (!es->ref_count)
  1975. goto err;
  1976. es->symm = devm_kcalloc(ice_hw_to_dev(hw), es->count,
  1977. sizeof(*es->symm), GFP_KERNEL);
  1978. if (!es->symm)
  1979. goto err;
  1980. es->written = devm_kcalloc(ice_hw_to_dev(hw), es->count,
  1981. sizeof(*es->written), GFP_KERNEL);
  1982. if (!es->written)
  1983. goto err;
  1984. es->mask_ena = devm_kcalloc(ice_hw_to_dev(hw), es->count,
  1985. sizeof(*es->mask_ena), GFP_KERNEL);
  1986. if (!es->mask_ena)
  1987. goto err;
  1988. prof_id->count = blk_sizes[i].prof_id;
  1989. prof_id->id = devm_kcalloc(ice_hw_to_dev(hw), prof_id->count,
  1990. sizeof(*prof_id->id), GFP_KERNEL);
  1991. if (!prof_id->id)
  1992. goto err;
  1993. }
  1994. return 0;
  1995. err:
  1996. ice_free_hw_tbls(hw);
  1997. return -ENOMEM;
  1998. }
  1999. /**
  2000. * ice_prof_gen_key - generate profile ID key
  2001. * @hw: pointer to the HW struct
  2002. * @blk: the block in which to write profile ID to
  2003. * @ptg: packet type group (PTG) portion of key
  2004. * @vsig: VSIG portion of key
  2005. * @cdid: CDID portion of key
  2006. * @flags: flag portion of key
  2007. * @vl_msk: valid mask
  2008. * @dc_msk: don't care mask
  2009. * @nm_msk: never match mask
  2010. * @key: output of profile ID key
  2011. */
  2012. static int
  2013. ice_prof_gen_key(struct ice_hw *hw, enum ice_block blk, u8 ptg, u16 vsig,
  2014. u8 cdid, u16 flags, u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
  2015. u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], u8 nm_msk[ICE_TCAM_KEY_VAL_SZ],
  2016. u8 key[ICE_TCAM_KEY_SZ])
  2017. {
  2018. struct ice_prof_id_key inkey;
  2019. inkey.xlt1 = ptg;
  2020. inkey.xlt2_cdid = cpu_to_le16(vsig);
  2021. inkey.flags = cpu_to_le16(flags);
  2022. switch (hw->blk[blk].prof.cdid_bits) {
  2023. case 0:
  2024. break;
  2025. case 2:
  2026. #define ICE_CD_2_M 0xC000U
  2027. #define ICE_CD_2_S 14
  2028. inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_2_M);
  2029. inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_2_S);
  2030. break;
  2031. case 4:
  2032. #define ICE_CD_4_M 0xF000U
  2033. #define ICE_CD_4_S 12
  2034. inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_4_M);
  2035. inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_4_S);
  2036. break;
  2037. case 8:
  2038. #define ICE_CD_8_M 0xFF00U
  2039. #define ICE_CD_8_S 16
  2040. inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_8_M);
  2041. inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_8_S);
  2042. break;
  2043. default:
  2044. ice_debug(hw, ICE_DBG_PKG, "Error in profile config\n");
  2045. break;
  2046. }
  2047. return ice_set_key(key, ICE_TCAM_KEY_SZ, (u8 *)&inkey, vl_msk, dc_msk,
  2048. nm_msk, 0, ICE_TCAM_KEY_SZ / 2);
  2049. }
  2050. /**
  2051. * ice_tcam_write_entry - write TCAM entry
  2052. * @hw: pointer to the HW struct
  2053. * @blk: the block in which to write profile ID to
  2054. * @idx: the entry index to write to
  2055. * @prof_id: profile ID
  2056. * @ptg: packet type group (PTG) portion of key
  2057. * @vsig: VSIG portion of key
  2058. * @cdid: CDID portion of key
  2059. * @flags: flag portion of key
  2060. * @vl_msk: valid mask
  2061. * @dc_msk: don't care mask
  2062. * @nm_msk: never match mask
  2063. */
  2064. static int
  2065. ice_tcam_write_entry(struct ice_hw *hw, enum ice_block blk, u16 idx,
  2066. u8 prof_id, u8 ptg, u16 vsig, u8 cdid, u16 flags,
  2067. u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
  2068. u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],
  2069. u8 nm_msk[ICE_TCAM_KEY_VAL_SZ])
  2070. {
  2071. struct ice_prof_tcam_entry;
  2072. int status;
  2073. status = ice_prof_gen_key(hw, blk, ptg, vsig, cdid, flags, vl_msk,
  2074. dc_msk, nm_msk, hw->blk[blk].prof.t[idx].key);
  2075. if (!status) {
  2076. hw->blk[blk].prof.t[idx].addr = cpu_to_le16(idx);
  2077. hw->blk[blk].prof.t[idx].prof_id = prof_id;
  2078. }
  2079. return status;
  2080. }
  2081. /**
  2082. * ice_vsig_get_ref - returns number of VSIs belong to a VSIG
  2083. * @hw: pointer to the hardware structure
  2084. * @blk: HW block
  2085. * @vsig: VSIG to query
  2086. * @refs: pointer to variable to receive the reference count
  2087. */
  2088. static int
  2089. ice_vsig_get_ref(struct ice_hw *hw, enum ice_block blk, u16 vsig, u16 *refs)
  2090. {
  2091. u16 idx = vsig & ICE_VSIG_IDX_M;
  2092. struct ice_vsig_vsi *ptr;
  2093. *refs = 0;
  2094. if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
  2095. return -ENOENT;
  2096. ptr = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
  2097. while (ptr) {
  2098. (*refs)++;
  2099. ptr = ptr->next_vsi;
  2100. }
  2101. return 0;
  2102. }
  2103. /**
  2104. * ice_has_prof_vsig - check to see if VSIG has a specific profile
  2105. * @hw: pointer to the hardware structure
  2106. * @blk: HW block
  2107. * @vsig: VSIG to check against
  2108. * @hdl: profile handle
  2109. */
  2110. static bool
  2111. ice_has_prof_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl)
  2112. {
  2113. u16 idx = vsig & ICE_VSIG_IDX_M;
  2114. struct ice_vsig_prof *ent;
  2115. list_for_each_entry(ent, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
  2116. list)
  2117. if (ent->profile_cookie == hdl)
  2118. return true;
  2119. ice_debug(hw, ICE_DBG_INIT, "Characteristic list for VSI group %d not found.\n",
  2120. vsig);
  2121. return false;
  2122. }
  2123. /**
  2124. * ice_prof_bld_es - build profile ID extraction sequence changes
  2125. * @hw: pointer to the HW struct
  2126. * @blk: hardware block
  2127. * @bld: the update package buffer build to add to
  2128. * @chgs: the list of changes to make in hardware
  2129. */
  2130. static int
  2131. ice_prof_bld_es(struct ice_hw *hw, enum ice_block blk,
  2132. struct ice_buf_build *bld, struct list_head *chgs)
  2133. {
  2134. u16 vec_size = hw->blk[blk].es.fvw * sizeof(struct ice_fv_word);
  2135. struct ice_chs_chg *tmp;
  2136. list_for_each_entry(tmp, chgs, list_entry)
  2137. if (tmp->type == ICE_PTG_ES_ADD && tmp->add_prof) {
  2138. u16 off = tmp->prof_id * hw->blk[blk].es.fvw;
  2139. struct ice_pkg_es *p;
  2140. u32 id;
  2141. id = ice_sect_id(blk, ICE_VEC_TBL);
  2142. p = ice_pkg_buf_alloc_section(bld, id,
  2143. struct_size(p, es, 1) +
  2144. vec_size -
  2145. sizeof(p->es[0]));
  2146. if (!p)
  2147. return -ENOSPC;
  2148. p->count = cpu_to_le16(1);
  2149. p->offset = cpu_to_le16(tmp->prof_id);
  2150. memcpy(p->es, &hw->blk[blk].es.t[off], vec_size);
  2151. }
  2152. return 0;
  2153. }
  2154. /**
  2155. * ice_prof_bld_tcam - build profile ID TCAM changes
  2156. * @hw: pointer to the HW struct
  2157. * @blk: hardware block
  2158. * @bld: the update package buffer build to add to
  2159. * @chgs: the list of changes to make in hardware
  2160. */
  2161. static int
  2162. ice_prof_bld_tcam(struct ice_hw *hw, enum ice_block blk,
  2163. struct ice_buf_build *bld, struct list_head *chgs)
  2164. {
  2165. struct ice_chs_chg *tmp;
  2166. list_for_each_entry(tmp, chgs, list_entry)
  2167. if (tmp->type == ICE_TCAM_ADD && tmp->add_tcam_idx) {
  2168. struct ice_prof_id_section *p;
  2169. u32 id;
  2170. id = ice_sect_id(blk, ICE_PROF_TCAM);
  2171. p = ice_pkg_buf_alloc_section(bld, id,
  2172. struct_size(p, entry, 1));
  2173. if (!p)
  2174. return -ENOSPC;
  2175. p->count = cpu_to_le16(1);
  2176. p->entry[0].addr = cpu_to_le16(tmp->tcam_idx);
  2177. p->entry[0].prof_id = tmp->prof_id;
  2178. memcpy(p->entry[0].key,
  2179. &hw->blk[blk].prof.t[tmp->tcam_idx].key,
  2180. sizeof(hw->blk[blk].prof.t->key));
  2181. }
  2182. return 0;
  2183. }
  2184. /**
  2185. * ice_prof_bld_xlt1 - build XLT1 changes
  2186. * @blk: hardware block
  2187. * @bld: the update package buffer build to add to
  2188. * @chgs: the list of changes to make in hardware
  2189. */
  2190. static int
  2191. ice_prof_bld_xlt1(enum ice_block blk, struct ice_buf_build *bld,
  2192. struct list_head *chgs)
  2193. {
  2194. struct ice_chs_chg *tmp;
  2195. list_for_each_entry(tmp, chgs, list_entry)
  2196. if (tmp->type == ICE_PTG_ES_ADD && tmp->add_ptg) {
  2197. struct ice_xlt1_section *p;
  2198. u32 id;
  2199. id = ice_sect_id(blk, ICE_XLT1);
  2200. p = ice_pkg_buf_alloc_section(bld, id,
  2201. struct_size(p, value, 1));
  2202. if (!p)
  2203. return -ENOSPC;
  2204. p->count = cpu_to_le16(1);
  2205. p->offset = cpu_to_le16(tmp->ptype);
  2206. p->value[0] = tmp->ptg;
  2207. }
  2208. return 0;
  2209. }
  2210. /**
  2211. * ice_prof_bld_xlt2 - build XLT2 changes
  2212. * @blk: hardware block
  2213. * @bld: the update package buffer build to add to
  2214. * @chgs: the list of changes to make in hardware
  2215. */
  2216. static int
  2217. ice_prof_bld_xlt2(enum ice_block blk, struct ice_buf_build *bld,
  2218. struct list_head *chgs)
  2219. {
  2220. struct ice_chs_chg *tmp;
  2221. list_for_each_entry(tmp, chgs, list_entry) {
  2222. struct ice_xlt2_section *p;
  2223. u32 id;
  2224. switch (tmp->type) {
  2225. case ICE_VSIG_ADD:
  2226. case ICE_VSI_MOVE:
  2227. case ICE_VSIG_REM:
  2228. id = ice_sect_id(blk, ICE_XLT2);
  2229. p = ice_pkg_buf_alloc_section(bld, id,
  2230. struct_size(p, value, 1));
  2231. if (!p)
  2232. return -ENOSPC;
  2233. p->count = cpu_to_le16(1);
  2234. p->offset = cpu_to_le16(tmp->vsi);
  2235. p->value[0] = cpu_to_le16(tmp->vsig);
  2236. break;
  2237. default:
  2238. break;
  2239. }
  2240. }
  2241. return 0;
  2242. }
  2243. /**
  2244. * ice_upd_prof_hw - update hardware using the change list
  2245. * @hw: pointer to the HW struct
  2246. * @blk: hardware block
  2247. * @chgs: the list of changes to make in hardware
  2248. */
  2249. static int
  2250. ice_upd_prof_hw(struct ice_hw *hw, enum ice_block blk,
  2251. struct list_head *chgs)
  2252. {
  2253. struct ice_buf_build *b;
  2254. struct ice_chs_chg *tmp;
  2255. u16 pkg_sects;
  2256. u16 xlt1 = 0;
  2257. u16 xlt2 = 0;
  2258. u16 tcam = 0;
  2259. u16 es = 0;
  2260. int status;
  2261. u16 sects;
  2262. /* count number of sections we need */
  2263. list_for_each_entry(tmp, chgs, list_entry) {
  2264. switch (tmp->type) {
  2265. case ICE_PTG_ES_ADD:
  2266. if (tmp->add_ptg)
  2267. xlt1++;
  2268. if (tmp->add_prof)
  2269. es++;
  2270. break;
  2271. case ICE_TCAM_ADD:
  2272. tcam++;
  2273. break;
  2274. case ICE_VSIG_ADD:
  2275. case ICE_VSI_MOVE:
  2276. case ICE_VSIG_REM:
  2277. xlt2++;
  2278. break;
  2279. default:
  2280. break;
  2281. }
  2282. }
  2283. sects = xlt1 + xlt2 + tcam + es;
  2284. if (!sects)
  2285. return 0;
  2286. /* Build update package buffer */
  2287. b = ice_pkg_buf_alloc(hw);
  2288. if (!b)
  2289. return -ENOMEM;
  2290. status = ice_pkg_buf_reserve_section(b, sects);
  2291. if (status)
  2292. goto error_tmp;
  2293. /* Preserve order of table update: ES, TCAM, PTG, VSIG */
  2294. if (es) {
  2295. status = ice_prof_bld_es(hw, blk, b, chgs);
  2296. if (status)
  2297. goto error_tmp;
  2298. }
  2299. if (tcam) {
  2300. status = ice_prof_bld_tcam(hw, blk, b, chgs);
  2301. if (status)
  2302. goto error_tmp;
  2303. }
  2304. if (xlt1) {
  2305. status = ice_prof_bld_xlt1(blk, b, chgs);
  2306. if (status)
  2307. goto error_tmp;
  2308. }
  2309. if (xlt2) {
  2310. status = ice_prof_bld_xlt2(blk, b, chgs);
  2311. if (status)
  2312. goto error_tmp;
  2313. }
  2314. /* After package buffer build check if the section count in buffer is
  2315. * non-zero and matches the number of sections detected for package
  2316. * update.
  2317. */
  2318. pkg_sects = ice_pkg_buf_get_active_sections(b);
  2319. if (!pkg_sects || pkg_sects != sects) {
  2320. status = -EINVAL;
  2321. goto error_tmp;
  2322. }
  2323. /* update package */
  2324. status = ice_update_pkg(hw, ice_pkg_buf(b), 1);
  2325. if (status == -EIO)
  2326. ice_debug(hw, ICE_DBG_INIT, "Unable to update HW profile\n");
  2327. error_tmp:
  2328. ice_pkg_buf_free(hw, b);
  2329. return status;
  2330. }
  2331. /**
  2332. * ice_update_fd_mask - set Flow Director Field Vector mask for a profile
  2333. * @hw: pointer to the HW struct
  2334. * @prof_id: profile ID
  2335. * @mask_sel: mask select
  2336. *
  2337. * This function enable any of the masks selected by the mask select parameter
  2338. * for the profile specified.
  2339. */
  2340. static void ice_update_fd_mask(struct ice_hw *hw, u16 prof_id, u32 mask_sel)
  2341. {
  2342. wr32(hw, GLQF_FDMASK_SEL(prof_id), mask_sel);
  2343. ice_debug(hw, ICE_DBG_INIT, "fd mask(%d): %x = %x\n", prof_id,
  2344. GLQF_FDMASK_SEL(prof_id), mask_sel);
  2345. }
  2346. struct ice_fd_src_dst_pair {
  2347. u8 prot_id;
  2348. u8 count;
  2349. u16 off;
  2350. };
  2351. static const struct ice_fd_src_dst_pair ice_fd_pairs[] = {
  2352. /* These are defined in pairs */
  2353. { ICE_PROT_IPV4_OF_OR_S, 2, 12 },
  2354. { ICE_PROT_IPV4_OF_OR_S, 2, 16 },
  2355. { ICE_PROT_IPV4_IL, 2, 12 },
  2356. { ICE_PROT_IPV4_IL, 2, 16 },
  2357. { ICE_PROT_IPV6_OF_OR_S, 8, 8 },
  2358. { ICE_PROT_IPV6_OF_OR_S, 8, 24 },
  2359. { ICE_PROT_IPV6_IL, 8, 8 },
  2360. { ICE_PROT_IPV6_IL, 8, 24 },
  2361. { ICE_PROT_TCP_IL, 1, 0 },
  2362. { ICE_PROT_TCP_IL, 1, 2 },
  2363. { ICE_PROT_UDP_OF, 1, 0 },
  2364. { ICE_PROT_UDP_OF, 1, 2 },
  2365. { ICE_PROT_UDP_IL_OR_S, 1, 0 },
  2366. { ICE_PROT_UDP_IL_OR_S, 1, 2 },
  2367. { ICE_PROT_SCTP_IL, 1, 0 },
  2368. { ICE_PROT_SCTP_IL, 1, 2 }
  2369. };
  2370. #define ICE_FD_SRC_DST_PAIR_COUNT ARRAY_SIZE(ice_fd_pairs)
  2371. /**
  2372. * ice_update_fd_swap - set register appropriately for a FD FV extraction
  2373. * @hw: pointer to the HW struct
  2374. * @prof_id: profile ID
  2375. * @es: extraction sequence (length of array is determined by the block)
  2376. */
  2377. static int
  2378. ice_update_fd_swap(struct ice_hw *hw, u16 prof_id, struct ice_fv_word *es)
  2379. {
  2380. DECLARE_BITMAP(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
  2381. u8 pair_start[ICE_FD_SRC_DST_PAIR_COUNT] = { 0 };
  2382. #define ICE_FD_FV_NOT_FOUND (-2)
  2383. s8 first_free = ICE_FD_FV_NOT_FOUND;
  2384. u8 used[ICE_MAX_FV_WORDS] = { 0 };
  2385. s8 orig_free, si;
  2386. u32 mask_sel = 0;
  2387. u8 i, j, k;
  2388. bitmap_zero(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
  2389. /* This code assumes that the Flow Director field vectors are assigned
  2390. * from the end of the FV indexes working towards the zero index, that
  2391. * only complete fields will be included and will be consecutive, and
  2392. * that there are no gaps between valid indexes.
  2393. */
  2394. /* Determine swap fields present */
  2395. for (i = 0; i < hw->blk[ICE_BLK_FD].es.fvw; i++) {
  2396. /* Find the first free entry, assuming right to left population.
  2397. * This is where we can start adding additional pairs if needed.
  2398. */
  2399. if (first_free == ICE_FD_FV_NOT_FOUND && es[i].prot_id !=
  2400. ICE_PROT_INVALID)
  2401. first_free = i - 1;
  2402. for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
  2403. if (es[i].prot_id == ice_fd_pairs[j].prot_id &&
  2404. es[i].off == ice_fd_pairs[j].off) {
  2405. __set_bit(j, pair_list);
  2406. pair_start[j] = i;
  2407. }
  2408. }
  2409. orig_free = first_free;
  2410. /* determine missing swap fields that need to be added */
  2411. for (i = 0; i < ICE_FD_SRC_DST_PAIR_COUNT; i += 2) {
  2412. u8 bit1 = test_bit(i + 1, pair_list);
  2413. u8 bit0 = test_bit(i, pair_list);
  2414. if (bit0 ^ bit1) {
  2415. u8 index;
  2416. /* add the appropriate 'paired' entry */
  2417. if (!bit0)
  2418. index = i;
  2419. else
  2420. index = i + 1;
  2421. /* check for room */
  2422. if (first_free + 1 < (s8)ice_fd_pairs[index].count)
  2423. return -ENOSPC;
  2424. /* place in extraction sequence */
  2425. for (k = 0; k < ice_fd_pairs[index].count; k++) {
  2426. es[first_free - k].prot_id =
  2427. ice_fd_pairs[index].prot_id;
  2428. es[first_free - k].off =
  2429. ice_fd_pairs[index].off + (k * 2);
  2430. if (k > first_free)
  2431. return -EIO;
  2432. /* keep track of non-relevant fields */
  2433. mask_sel |= BIT(first_free - k);
  2434. }
  2435. pair_start[index] = first_free;
  2436. first_free -= ice_fd_pairs[index].count;
  2437. }
  2438. }
  2439. /* fill in the swap array */
  2440. si = hw->blk[ICE_BLK_FD].es.fvw - 1;
  2441. while (si >= 0) {
  2442. u8 indexes_used = 1;
  2443. /* assume flat at this index */
  2444. #define ICE_SWAP_VALID 0x80
  2445. used[si] = si | ICE_SWAP_VALID;
  2446. if (orig_free == ICE_FD_FV_NOT_FOUND || si <= orig_free) {
  2447. si -= indexes_used;
  2448. continue;
  2449. }
  2450. /* check for a swap location */
  2451. for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
  2452. if (es[si].prot_id == ice_fd_pairs[j].prot_id &&
  2453. es[si].off == ice_fd_pairs[j].off) {
  2454. u8 idx;
  2455. /* determine the appropriate matching field */
  2456. idx = j + ((j % 2) ? -1 : 1);
  2457. indexes_used = ice_fd_pairs[idx].count;
  2458. for (k = 0; k < indexes_used; k++) {
  2459. used[si - k] = (pair_start[idx] - k) |
  2460. ICE_SWAP_VALID;
  2461. }
  2462. break;
  2463. }
  2464. si -= indexes_used;
  2465. }
  2466. /* for each set of 4 swap and 4 inset indexes, write the appropriate
  2467. * register
  2468. */
  2469. for (j = 0; j < hw->blk[ICE_BLK_FD].es.fvw / 4; j++) {
  2470. u32 raw_swap = 0;
  2471. u32 raw_in = 0;
  2472. for (k = 0; k < 4; k++) {
  2473. u8 idx;
  2474. idx = (j * 4) + k;
  2475. if (used[idx] && !(mask_sel & BIT(idx))) {
  2476. raw_swap |= used[idx] << (k * BITS_PER_BYTE);
  2477. #define ICE_INSET_DFLT 0x9f
  2478. raw_in |= ICE_INSET_DFLT << (k * BITS_PER_BYTE);
  2479. }
  2480. }
  2481. /* write the appropriate swap register set */
  2482. wr32(hw, GLQF_FDSWAP(prof_id, j), raw_swap);
  2483. ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): %x = %08x\n",
  2484. prof_id, j, GLQF_FDSWAP(prof_id, j), raw_swap);
  2485. /* write the appropriate inset register set */
  2486. wr32(hw, GLQF_FDINSET(prof_id, j), raw_in);
  2487. ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): %x = %08x\n",
  2488. prof_id, j, GLQF_FDINSET(prof_id, j), raw_in);
  2489. }
  2490. /* initially clear the mask select for this profile */
  2491. ice_update_fd_mask(hw, prof_id, 0);
  2492. return 0;
  2493. }
  2494. /* The entries here needs to match the order of enum ice_ptype_attrib */
  2495. static const struct ice_ptype_attrib_info ice_ptype_attributes[] = {
  2496. { ICE_GTP_PDU_EH, ICE_GTP_PDU_FLAG_MASK },
  2497. { ICE_GTP_SESSION, ICE_GTP_FLAGS_MASK },
  2498. { ICE_GTP_DOWNLINK, ICE_GTP_FLAGS_MASK },
  2499. { ICE_GTP_UPLINK, ICE_GTP_FLAGS_MASK },
  2500. };
  2501. /**
  2502. * ice_get_ptype_attrib_info - get PTYPE attribute information
  2503. * @type: attribute type
  2504. * @info: pointer to variable to the attribute information
  2505. */
  2506. static void
  2507. ice_get_ptype_attrib_info(enum ice_ptype_attrib_type type,
  2508. struct ice_ptype_attrib_info *info)
  2509. {
  2510. *info = ice_ptype_attributes[type];
  2511. }
  2512. /**
  2513. * ice_add_prof_attrib - add any PTG with attributes to profile
  2514. * @prof: pointer to the profile to which PTG entries will be added
  2515. * @ptg: PTG to be added
  2516. * @ptype: PTYPE that needs to be looked up
  2517. * @attr: array of attributes that will be considered
  2518. * @attr_cnt: number of elements in the attribute array
  2519. */
  2520. static int
  2521. ice_add_prof_attrib(struct ice_prof_map *prof, u8 ptg, u16 ptype,
  2522. const struct ice_ptype_attributes *attr, u16 attr_cnt)
  2523. {
  2524. bool found = false;
  2525. u16 i;
  2526. for (i = 0; i < attr_cnt; i++)
  2527. if (attr[i].ptype == ptype) {
  2528. found = true;
  2529. prof->ptg[prof->ptg_cnt] = ptg;
  2530. ice_get_ptype_attrib_info(attr[i].attrib,
  2531. &prof->attr[prof->ptg_cnt]);
  2532. if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE)
  2533. return -ENOSPC;
  2534. }
  2535. if (!found)
  2536. return -ENOENT;
  2537. return 0;
  2538. }
  2539. /**
  2540. * ice_disable_fd_swap - set register appropriately to disable FD SWAP
  2541. * @hw: pointer to the HW struct
  2542. * @prof_id: profile ID
  2543. */
  2544. static void
  2545. ice_disable_fd_swap(struct ice_hw *hw, u8 prof_id)
  2546. {
  2547. u16 swap_val, fvw_num;
  2548. unsigned int i;
  2549. swap_val = ICE_SWAP_VALID;
  2550. fvw_num = hw->blk[ICE_BLK_FD].es.fvw / ICE_FDIR_REG_SET_SIZE;
  2551. /* Since the SWAP Flag in the Programming Desc doesn't work,
  2552. * here add method to disable the SWAP Option via setting
  2553. * certain SWAP and INSET register sets.
  2554. */
  2555. for (i = 0; i < fvw_num ; i++) {
  2556. u32 raw_swap, raw_in;
  2557. unsigned int j;
  2558. raw_swap = 0;
  2559. raw_in = 0;
  2560. for (j = 0; j < ICE_FDIR_REG_SET_SIZE; j++) {
  2561. raw_swap |= (swap_val++) << (j * BITS_PER_BYTE);
  2562. raw_in |= ICE_INSET_DFLT << (j * BITS_PER_BYTE);
  2563. }
  2564. /* write the FDIR swap register set */
  2565. wr32(hw, GLQF_FDSWAP(prof_id, i), raw_swap);
  2566. ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): 0x%x = 0x%08x\n",
  2567. prof_id, i, GLQF_FDSWAP(prof_id, i), raw_swap);
  2568. /* write the FDIR inset register set */
  2569. wr32(hw, GLQF_FDINSET(prof_id, i), raw_in);
  2570. ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): 0x%x = 0x%08x\n",
  2571. prof_id, i, GLQF_FDINSET(prof_id, i), raw_in);
  2572. }
  2573. }
  2574. /*
  2575. * ice_add_prof - add profile
  2576. * @hw: pointer to the HW struct
  2577. * @blk: hardware block
  2578. * @id: profile tracking ID
  2579. * @ptypes: array of bitmaps indicating ptypes (ICE_FLOW_PTYPE_MAX bits)
  2580. * @attr: array of attributes
  2581. * @attr_cnt: number of elements in attr array
  2582. * @es: extraction sequence (length of array is determined by the block)
  2583. * @masks: mask for extraction sequence
  2584. * @symm: symmetric setting for RSS profiles
  2585. * @fd_swap: enable/disable FDIR paired src/dst fields swap option
  2586. *
  2587. * This function registers a profile, which matches a set of PTYPES with a
  2588. * particular extraction sequence. While the hardware profile is allocated
  2589. * it will not be written until the first call to ice_add_flow that specifies
  2590. * the ID value used here.
  2591. */
  2592. int
  2593. ice_add_prof(struct ice_hw *hw, enum ice_block blk, u64 id, u8 ptypes[],
  2594. const struct ice_ptype_attributes *attr, u16 attr_cnt,
  2595. struct ice_fv_word *es, u16 *masks, bool symm, bool fd_swap)
  2596. {
  2597. u32 bytes = DIV_ROUND_UP(ICE_FLOW_PTYPE_MAX, BITS_PER_BYTE);
  2598. DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
  2599. struct ice_prof_map *prof;
  2600. u8 byte = 0;
  2601. u8 prof_id;
  2602. int status;
  2603. bitmap_zero(ptgs_used, ICE_XLT1_CNT);
  2604. mutex_lock(&hw->blk[blk].es.prof_map_lock);
  2605. /* search for existing profile */
  2606. status = ice_find_prof_id_with_mask(hw, blk, es, masks, symm, &prof_id);
  2607. if (status) {
  2608. /* allocate profile ID */
  2609. status = ice_alloc_prof_id(hw, blk, &prof_id);
  2610. if (status)
  2611. goto err_ice_add_prof;
  2612. if (blk == ICE_BLK_FD && fd_swap) {
  2613. /* For Flow Director block, the extraction sequence may
  2614. * need to be altered in the case where there are paired
  2615. * fields that have no match. This is necessary because
  2616. * for Flow Director, src and dest fields need to paired
  2617. * for filter programming and these values are swapped
  2618. * during Tx.
  2619. */
  2620. status = ice_update_fd_swap(hw, prof_id, es);
  2621. if (status)
  2622. goto err_ice_add_prof;
  2623. } else if (blk == ICE_BLK_FD) {
  2624. ice_disable_fd_swap(hw, prof_id);
  2625. }
  2626. status = ice_update_prof_masking(hw, blk, prof_id, masks);
  2627. if (status)
  2628. goto err_ice_add_prof;
  2629. /* and write new es */
  2630. ice_write_es(hw, blk, prof_id, es, symm);
  2631. }
  2632. ice_prof_inc_ref(hw, blk, prof_id);
  2633. /* add profile info */
  2634. prof = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*prof), GFP_KERNEL);
  2635. if (!prof) {
  2636. status = -ENOMEM;
  2637. goto err_ice_add_prof;
  2638. }
  2639. prof->profile_cookie = id;
  2640. prof->prof_id = prof_id;
  2641. prof->ptg_cnt = 0;
  2642. prof->context = 0;
  2643. /* build list of ptgs */
  2644. while (bytes && prof->ptg_cnt < ICE_MAX_PTG_PER_PROFILE) {
  2645. u8 bit;
  2646. if (!ptypes[byte]) {
  2647. bytes--;
  2648. byte++;
  2649. continue;
  2650. }
  2651. /* Examine 8 bits per byte */
  2652. for_each_set_bit(bit, (unsigned long *)&ptypes[byte],
  2653. BITS_PER_BYTE) {
  2654. u16 ptype;
  2655. u8 ptg;
  2656. ptype = byte * BITS_PER_BYTE + bit;
  2657. /* The package should place all ptypes in a non-zero
  2658. * PTG, so the following call should never fail.
  2659. */
  2660. if (ice_ptg_find_ptype(hw, blk, ptype, &ptg))
  2661. continue;
  2662. /* If PTG is already added, skip and continue */
  2663. if (test_bit(ptg, ptgs_used))
  2664. continue;
  2665. __set_bit(ptg, ptgs_used);
  2666. /* Check to see there are any attributes for
  2667. * this PTYPE, and add them if found.
  2668. */
  2669. status = ice_add_prof_attrib(prof, ptg, ptype,
  2670. attr, attr_cnt);
  2671. if (status == -ENOSPC)
  2672. break;
  2673. if (status) {
  2674. /* This is simple a PTYPE/PTG with no
  2675. * attribute
  2676. */
  2677. prof->ptg[prof->ptg_cnt] = ptg;
  2678. prof->attr[prof->ptg_cnt].flags = 0;
  2679. prof->attr[prof->ptg_cnt].mask = 0;
  2680. if (++prof->ptg_cnt >=
  2681. ICE_MAX_PTG_PER_PROFILE)
  2682. break;
  2683. }
  2684. }
  2685. bytes--;
  2686. byte++;
  2687. }
  2688. list_add(&prof->list, &hw->blk[blk].es.prof_map);
  2689. status = 0;
  2690. err_ice_add_prof:
  2691. mutex_unlock(&hw->blk[blk].es.prof_map_lock);
  2692. return status;
  2693. }
  2694. /**
  2695. * ice_search_prof_id - Search for a profile tracking ID
  2696. * @hw: pointer to the HW struct
  2697. * @blk: hardware block
  2698. * @id: profile tracking ID
  2699. *
  2700. * This will search for a profile tracking ID which was previously added.
  2701. * The profile map lock should be held before calling this function.
  2702. */
  2703. struct ice_prof_map *
  2704. ice_search_prof_id(struct ice_hw *hw, enum ice_block blk, u64 id)
  2705. {
  2706. struct ice_prof_map *entry = NULL;
  2707. struct ice_prof_map *map;
  2708. list_for_each_entry(map, &hw->blk[blk].es.prof_map, list)
  2709. if (map->profile_cookie == id) {
  2710. entry = map;
  2711. break;
  2712. }
  2713. return entry;
  2714. }
  2715. /**
  2716. * ice_vsig_prof_id_count - count profiles in a VSIG
  2717. * @hw: pointer to the HW struct
  2718. * @blk: hardware block
  2719. * @vsig: VSIG to remove the profile from
  2720. */
  2721. static u16
  2722. ice_vsig_prof_id_count(struct ice_hw *hw, enum ice_block blk, u16 vsig)
  2723. {
  2724. u16 idx = vsig & ICE_VSIG_IDX_M, count = 0;
  2725. struct ice_vsig_prof *p;
  2726. list_for_each_entry(p, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
  2727. list)
  2728. count++;
  2729. return count;
  2730. }
  2731. /**
  2732. * ice_rel_tcam_idx - release a TCAM index
  2733. * @hw: pointer to the HW struct
  2734. * @blk: hardware block
  2735. * @idx: the index to release
  2736. */
  2737. static int ice_rel_tcam_idx(struct ice_hw *hw, enum ice_block blk, u16 idx)
  2738. {
  2739. /* Masks to invoke a never match entry */
  2740. u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  2741. u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF };
  2742. u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
  2743. int status;
  2744. /* write the TCAM entry */
  2745. status = ice_tcam_write_entry(hw, blk, idx, 0, 0, 0, 0, 0, vl_msk,
  2746. dc_msk, nm_msk);
  2747. if (status)
  2748. return status;
  2749. /* release the TCAM entry */
  2750. status = ice_free_tcam_ent(hw, blk, idx);
  2751. return status;
  2752. }
  2753. /**
  2754. * ice_rem_prof_id - remove one profile from a VSIG
  2755. * @hw: pointer to the HW struct
  2756. * @blk: hardware block
  2757. * @prof: pointer to profile structure to remove
  2758. */
  2759. static int
  2760. ice_rem_prof_id(struct ice_hw *hw, enum ice_block blk,
  2761. struct ice_vsig_prof *prof)
  2762. {
  2763. int status;
  2764. u16 i;
  2765. for (i = 0; i < prof->tcam_count; i++)
  2766. if (prof->tcam[i].in_use) {
  2767. prof->tcam[i].in_use = false;
  2768. status = ice_rel_tcam_idx(hw, blk,
  2769. prof->tcam[i].tcam_idx);
  2770. if (status)
  2771. return -EIO;
  2772. }
  2773. return 0;
  2774. }
  2775. /**
  2776. * ice_rem_vsig - remove VSIG
  2777. * @hw: pointer to the HW struct
  2778. * @blk: hardware block
  2779. * @vsig: the VSIG to remove
  2780. * @chg: the change list
  2781. */
  2782. static int
  2783. ice_rem_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
  2784. struct list_head *chg)
  2785. {
  2786. u16 idx = vsig & ICE_VSIG_IDX_M;
  2787. struct ice_vsig_vsi *vsi_cur;
  2788. struct ice_vsig_prof *d, *t;
  2789. /* remove TCAM entries */
  2790. list_for_each_entry_safe(d, t,
  2791. &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
  2792. list) {
  2793. int status;
  2794. status = ice_rem_prof_id(hw, blk, d);
  2795. if (status)
  2796. return status;
  2797. list_del(&d->list);
  2798. devm_kfree(ice_hw_to_dev(hw), d);
  2799. }
  2800. /* Move all VSIS associated with this VSIG to the default VSIG */
  2801. vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
  2802. /* If the VSIG has at least 1 VSI then iterate through the list
  2803. * and remove the VSIs before deleting the group.
  2804. */
  2805. if (vsi_cur)
  2806. do {
  2807. struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
  2808. struct ice_chs_chg *p;
  2809. p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
  2810. GFP_KERNEL);
  2811. if (!p)
  2812. return -ENOMEM;
  2813. p->type = ICE_VSIG_REM;
  2814. p->orig_vsig = vsig;
  2815. p->vsig = ICE_DEFAULT_VSIG;
  2816. p->vsi = vsi_cur - hw->blk[blk].xlt2.vsis;
  2817. list_add(&p->list_entry, chg);
  2818. vsi_cur = tmp;
  2819. } while (vsi_cur);
  2820. return ice_vsig_free(hw, blk, vsig);
  2821. }
  2822. /**
  2823. * ice_rem_prof_id_vsig - remove a specific profile from a VSIG
  2824. * @hw: pointer to the HW struct
  2825. * @blk: hardware block
  2826. * @vsig: VSIG to remove the profile from
  2827. * @hdl: profile handle indicating which profile to remove
  2828. * @chg: list to receive a record of changes
  2829. */
  2830. static int
  2831. ice_rem_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
  2832. struct list_head *chg)
  2833. {
  2834. u16 idx = vsig & ICE_VSIG_IDX_M;
  2835. struct ice_vsig_prof *p, *t;
  2836. list_for_each_entry_safe(p, t,
  2837. &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
  2838. list)
  2839. if (p->profile_cookie == hdl) {
  2840. int status;
  2841. if (ice_vsig_prof_id_count(hw, blk, vsig) == 1)
  2842. /* this is the last profile, remove the VSIG */
  2843. return ice_rem_vsig(hw, blk, vsig, chg);
  2844. status = ice_rem_prof_id(hw, blk, p);
  2845. if (!status) {
  2846. list_del(&p->list);
  2847. devm_kfree(ice_hw_to_dev(hw), p);
  2848. }
  2849. return status;
  2850. }
  2851. return -ENOENT;
  2852. }
  2853. /**
  2854. * ice_rem_flow_all - remove all flows with a particular profile
  2855. * @hw: pointer to the HW struct
  2856. * @blk: hardware block
  2857. * @id: profile tracking ID
  2858. */
  2859. static int ice_rem_flow_all(struct ice_hw *hw, enum ice_block blk, u64 id)
  2860. {
  2861. struct ice_chs_chg *del, *tmp;
  2862. struct list_head chg;
  2863. int status;
  2864. u16 i;
  2865. INIT_LIST_HEAD(&chg);
  2866. for (i = 1; i < ICE_MAX_VSIGS; i++)
  2867. if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) {
  2868. if (ice_has_prof_vsig(hw, blk, i, id)) {
  2869. status = ice_rem_prof_id_vsig(hw, blk, i, id,
  2870. &chg);
  2871. if (status)
  2872. goto err_ice_rem_flow_all;
  2873. }
  2874. }
  2875. status = ice_upd_prof_hw(hw, blk, &chg);
  2876. err_ice_rem_flow_all:
  2877. list_for_each_entry_safe(del, tmp, &chg, list_entry) {
  2878. list_del(&del->list_entry);
  2879. devm_kfree(ice_hw_to_dev(hw), del);
  2880. }
  2881. return status;
  2882. }
  2883. /**
  2884. * ice_rem_prof - remove profile
  2885. * @hw: pointer to the HW struct
  2886. * @blk: hardware block
  2887. * @id: profile tracking ID
  2888. *
  2889. * This will remove the profile specified by the ID parameter, which was
  2890. * previously created through ice_add_prof. If any existing entries
  2891. * are associated with this profile, they will be removed as well.
  2892. */
  2893. int ice_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 id)
  2894. {
  2895. struct ice_prof_map *pmap;
  2896. int status;
  2897. mutex_lock(&hw->blk[blk].es.prof_map_lock);
  2898. pmap = ice_search_prof_id(hw, blk, id);
  2899. if (!pmap) {
  2900. status = -ENOENT;
  2901. goto err_ice_rem_prof;
  2902. }
  2903. /* remove all flows with this profile */
  2904. status = ice_rem_flow_all(hw, blk, pmap->profile_cookie);
  2905. if (status)
  2906. goto err_ice_rem_prof;
  2907. /* dereference profile, and possibly remove */
  2908. ice_prof_dec_ref(hw, blk, pmap->prof_id);
  2909. list_del(&pmap->list);
  2910. devm_kfree(ice_hw_to_dev(hw), pmap);
  2911. err_ice_rem_prof:
  2912. mutex_unlock(&hw->blk[blk].es.prof_map_lock);
  2913. return status;
  2914. }
  2915. /**
  2916. * ice_get_prof - get profile
  2917. * @hw: pointer to the HW struct
  2918. * @blk: hardware block
  2919. * @hdl: profile handle
  2920. * @chg: change list
  2921. */
  2922. static int
  2923. ice_get_prof(struct ice_hw *hw, enum ice_block blk, u64 hdl,
  2924. struct list_head *chg)
  2925. {
  2926. struct ice_prof_map *map;
  2927. struct ice_chs_chg *p;
  2928. int status = 0;
  2929. u16 i;
  2930. mutex_lock(&hw->blk[blk].es.prof_map_lock);
  2931. /* Get the details on the profile specified by the handle ID */
  2932. map = ice_search_prof_id(hw, blk, hdl);
  2933. if (!map) {
  2934. status = -ENOENT;
  2935. goto err_ice_get_prof;
  2936. }
  2937. for (i = 0; i < map->ptg_cnt; i++)
  2938. if (!hw->blk[blk].es.written[map->prof_id]) {
  2939. /* add ES to change list */
  2940. p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
  2941. GFP_KERNEL);
  2942. if (!p) {
  2943. status = -ENOMEM;
  2944. goto err_ice_get_prof;
  2945. }
  2946. p->type = ICE_PTG_ES_ADD;
  2947. p->ptype = 0;
  2948. p->ptg = map->ptg[i];
  2949. p->add_ptg = 0;
  2950. p->add_prof = 1;
  2951. p->prof_id = map->prof_id;
  2952. hw->blk[blk].es.written[map->prof_id] = true;
  2953. list_add(&p->list_entry, chg);
  2954. }
  2955. err_ice_get_prof:
  2956. mutex_unlock(&hw->blk[blk].es.prof_map_lock);
  2957. /* let caller clean up the change list */
  2958. return status;
  2959. }
  2960. /**
  2961. * ice_get_profs_vsig - get a copy of the list of profiles from a VSIG
  2962. * @hw: pointer to the HW struct
  2963. * @blk: hardware block
  2964. * @vsig: VSIG from which to copy the list
  2965. * @lst: output list
  2966. *
  2967. * This routine makes a copy of the list of profiles in the specified VSIG.
  2968. */
  2969. static int
  2970. ice_get_profs_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
  2971. struct list_head *lst)
  2972. {
  2973. struct ice_vsig_prof *ent1, *ent2;
  2974. u16 idx = vsig & ICE_VSIG_IDX_M;
  2975. list_for_each_entry(ent1, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
  2976. list) {
  2977. struct ice_vsig_prof *p;
  2978. /* copy to the input list */
  2979. p = devm_kmemdup(ice_hw_to_dev(hw), ent1, sizeof(*p),
  2980. GFP_KERNEL);
  2981. if (!p)
  2982. goto err_ice_get_profs_vsig;
  2983. list_add_tail(&p->list, lst);
  2984. }
  2985. return 0;
  2986. err_ice_get_profs_vsig:
  2987. list_for_each_entry_safe(ent1, ent2, lst, list) {
  2988. list_del(&ent1->list);
  2989. devm_kfree(ice_hw_to_dev(hw), ent1);
  2990. }
  2991. return -ENOMEM;
  2992. }
  2993. /**
  2994. * ice_add_prof_to_lst - add profile entry to a list
  2995. * @hw: pointer to the HW struct
  2996. * @blk: hardware block
  2997. * @lst: the list to be added to
  2998. * @hdl: profile handle of entry to add
  2999. */
  3000. static int
  3001. ice_add_prof_to_lst(struct ice_hw *hw, enum ice_block blk,
  3002. struct list_head *lst, u64 hdl)
  3003. {
  3004. struct ice_prof_map *map;
  3005. struct ice_vsig_prof *p;
  3006. int status = 0;
  3007. u16 i;
  3008. mutex_lock(&hw->blk[blk].es.prof_map_lock);
  3009. map = ice_search_prof_id(hw, blk, hdl);
  3010. if (!map) {
  3011. status = -ENOENT;
  3012. goto err_ice_add_prof_to_lst;
  3013. }
  3014. p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
  3015. if (!p) {
  3016. status = -ENOMEM;
  3017. goto err_ice_add_prof_to_lst;
  3018. }
  3019. p->profile_cookie = map->profile_cookie;
  3020. p->prof_id = map->prof_id;
  3021. p->tcam_count = map->ptg_cnt;
  3022. for (i = 0; i < map->ptg_cnt; i++) {
  3023. p->tcam[i].prof_id = map->prof_id;
  3024. p->tcam[i].tcam_idx = ICE_INVALID_TCAM;
  3025. p->tcam[i].ptg = map->ptg[i];
  3026. }
  3027. list_add(&p->list, lst);
  3028. err_ice_add_prof_to_lst:
  3029. mutex_unlock(&hw->blk[blk].es.prof_map_lock);
  3030. return status;
  3031. }
  3032. /**
  3033. * ice_move_vsi - move VSI to another VSIG
  3034. * @hw: pointer to the HW struct
  3035. * @blk: hardware block
  3036. * @vsi: the VSI to move
  3037. * @vsig: the VSIG to move the VSI to
  3038. * @chg: the change list
  3039. */
  3040. static int
  3041. ice_move_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig,
  3042. struct list_head *chg)
  3043. {
  3044. struct ice_chs_chg *p;
  3045. u16 orig_vsig;
  3046. int status;
  3047. p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
  3048. if (!p)
  3049. return -ENOMEM;
  3050. status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
  3051. if (!status)
  3052. status = ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
  3053. if (status) {
  3054. devm_kfree(ice_hw_to_dev(hw), p);
  3055. return status;
  3056. }
  3057. p->type = ICE_VSI_MOVE;
  3058. p->vsi = vsi;
  3059. p->orig_vsig = orig_vsig;
  3060. p->vsig = vsig;
  3061. list_add(&p->list_entry, chg);
  3062. return 0;
  3063. }
  3064. /**
  3065. * ice_rem_chg_tcam_ent - remove a specific TCAM entry from change list
  3066. * @hw: pointer to the HW struct
  3067. * @idx: the index of the TCAM entry to remove
  3068. * @chg: the list of change structures to search
  3069. */
  3070. static void
  3071. ice_rem_chg_tcam_ent(struct ice_hw *hw, u16 idx, struct list_head *chg)
  3072. {
  3073. struct ice_chs_chg *pos, *tmp;
  3074. list_for_each_entry_safe(tmp, pos, chg, list_entry)
  3075. if (tmp->type == ICE_TCAM_ADD && tmp->tcam_idx == idx) {
  3076. list_del(&tmp->list_entry);
  3077. devm_kfree(ice_hw_to_dev(hw), tmp);
  3078. }
  3079. }
  3080. /**
  3081. * ice_prof_tcam_ena_dis - add enable or disable TCAM change
  3082. * @hw: pointer to the HW struct
  3083. * @blk: hardware block
  3084. * @enable: true to enable, false to disable
  3085. * @vsig: the VSIG of the TCAM entry
  3086. * @tcam: pointer the TCAM info structure of the TCAM to disable
  3087. * @chg: the change list
  3088. *
  3089. * This function appends an enable or disable TCAM entry in the change log
  3090. */
  3091. static int
  3092. ice_prof_tcam_ena_dis(struct ice_hw *hw, enum ice_block blk, bool enable,
  3093. u16 vsig, struct ice_tcam_inf *tcam,
  3094. struct list_head *chg)
  3095. {
  3096. struct ice_chs_chg *p;
  3097. int status;
  3098. u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  3099. u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
  3100. u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
  3101. /* if disabling, free the TCAM */
  3102. if (!enable) {
  3103. status = ice_rel_tcam_idx(hw, blk, tcam->tcam_idx);
  3104. /* if we have already created a change for this TCAM entry, then
  3105. * we need to remove that entry, in order to prevent writing to
  3106. * a TCAM entry we no longer will have ownership of.
  3107. */
  3108. ice_rem_chg_tcam_ent(hw, tcam->tcam_idx, chg);
  3109. tcam->tcam_idx = 0;
  3110. tcam->in_use = 0;
  3111. return status;
  3112. }
  3113. /* for re-enabling, reallocate a TCAM */
  3114. /* for entries with empty attribute masks, allocate entry from
  3115. * the bottom of the TCAM table; otherwise, allocate from the
  3116. * top of the table in order to give it higher priority
  3117. */
  3118. status = ice_alloc_tcam_ent(hw, blk, tcam->attr.mask == 0,
  3119. &tcam->tcam_idx);
  3120. if (status)
  3121. return status;
  3122. /* add TCAM to change list */
  3123. p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
  3124. if (!p)
  3125. return -ENOMEM;
  3126. status = ice_tcam_write_entry(hw, blk, tcam->tcam_idx, tcam->prof_id,
  3127. tcam->ptg, vsig, 0, tcam->attr.flags,
  3128. vl_msk, dc_msk, nm_msk);
  3129. if (status)
  3130. goto err_ice_prof_tcam_ena_dis;
  3131. tcam->in_use = 1;
  3132. p->type = ICE_TCAM_ADD;
  3133. p->add_tcam_idx = true;
  3134. p->prof_id = tcam->prof_id;
  3135. p->ptg = tcam->ptg;
  3136. p->vsig = 0;
  3137. p->tcam_idx = tcam->tcam_idx;
  3138. /* log change */
  3139. list_add(&p->list_entry, chg);
  3140. return 0;
  3141. err_ice_prof_tcam_ena_dis:
  3142. devm_kfree(ice_hw_to_dev(hw), p);
  3143. return status;
  3144. }
  3145. /**
  3146. * ice_adj_prof_priorities - adjust profile based on priorities
  3147. * @hw: pointer to the HW struct
  3148. * @blk: hardware block
  3149. * @vsig: the VSIG for which to adjust profile priorities
  3150. * @chg: the change list
  3151. */
  3152. static int
  3153. ice_adj_prof_priorities(struct ice_hw *hw, enum ice_block blk, u16 vsig,
  3154. struct list_head *chg)
  3155. {
  3156. DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
  3157. struct ice_vsig_prof *t;
  3158. int status;
  3159. u16 idx;
  3160. bitmap_zero(ptgs_used, ICE_XLT1_CNT);
  3161. idx = vsig & ICE_VSIG_IDX_M;
  3162. /* Priority is based on the order in which the profiles are added. The
  3163. * newest added profile has highest priority and the oldest added
  3164. * profile has the lowest priority. Since the profile property list for
  3165. * a VSIG is sorted from newest to oldest, this code traverses the list
  3166. * in order and enables the first of each PTG that it finds (that is not
  3167. * already enabled); it also disables any duplicate PTGs that it finds
  3168. * in the older profiles (that are currently enabled).
  3169. */
  3170. list_for_each_entry(t, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
  3171. list) {
  3172. u16 i;
  3173. for (i = 0; i < t->tcam_count; i++) {
  3174. /* Scan the priorities from newest to oldest.
  3175. * Make sure that the newest profiles take priority.
  3176. */
  3177. if (test_bit(t->tcam[i].ptg, ptgs_used) &&
  3178. t->tcam[i].in_use) {
  3179. /* need to mark this PTG as never match, as it
  3180. * was already in use and therefore duplicate
  3181. * (and lower priority)
  3182. */
  3183. status = ice_prof_tcam_ena_dis(hw, blk, false,
  3184. vsig,
  3185. &t->tcam[i],
  3186. chg);
  3187. if (status)
  3188. return status;
  3189. } else if (!test_bit(t->tcam[i].ptg, ptgs_used) &&
  3190. !t->tcam[i].in_use) {
  3191. /* need to enable this PTG, as it in not in use
  3192. * and not enabled (highest priority)
  3193. */
  3194. status = ice_prof_tcam_ena_dis(hw, blk, true,
  3195. vsig,
  3196. &t->tcam[i],
  3197. chg);
  3198. if (status)
  3199. return status;
  3200. }
  3201. /* keep track of used ptgs */
  3202. __set_bit(t->tcam[i].ptg, ptgs_used);
  3203. }
  3204. }
  3205. return 0;
  3206. }
  3207. /**
  3208. * ice_add_prof_id_vsig - add profile to VSIG
  3209. * @hw: pointer to the HW struct
  3210. * @blk: hardware block
  3211. * @vsig: the VSIG to which this profile is to be added
  3212. * @hdl: the profile handle indicating the profile to add
  3213. * @rev: true to add entries to the end of the list
  3214. * @chg: the change list
  3215. */
  3216. static int
  3217. ice_add_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
  3218. bool rev, struct list_head *chg)
  3219. {
  3220. /* Masks that ignore flags */
  3221. u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  3222. u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
  3223. u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
  3224. struct ice_prof_map *map;
  3225. struct ice_vsig_prof *t;
  3226. struct ice_chs_chg *p;
  3227. u16 vsig_idx, i;
  3228. int status = 0;
  3229. /* Error, if this VSIG already has this profile */
  3230. if (ice_has_prof_vsig(hw, blk, vsig, hdl))
  3231. return -EEXIST;
  3232. /* new VSIG profile structure */
  3233. t = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*t), GFP_KERNEL);
  3234. if (!t)
  3235. return -ENOMEM;
  3236. mutex_lock(&hw->blk[blk].es.prof_map_lock);
  3237. /* Get the details on the profile specified by the handle ID */
  3238. map = ice_search_prof_id(hw, blk, hdl);
  3239. if (!map) {
  3240. status = -ENOENT;
  3241. goto err_ice_add_prof_id_vsig;
  3242. }
  3243. t->profile_cookie = map->profile_cookie;
  3244. t->prof_id = map->prof_id;
  3245. t->tcam_count = map->ptg_cnt;
  3246. /* create TCAM entries */
  3247. for (i = 0; i < map->ptg_cnt; i++) {
  3248. u16 tcam_idx;
  3249. /* add TCAM to change list */
  3250. p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
  3251. if (!p) {
  3252. status = -ENOMEM;
  3253. goto err_ice_add_prof_id_vsig;
  3254. }
  3255. /* allocate the TCAM entry index */
  3256. /* for entries with empty attribute masks, allocate entry from
  3257. * the bottom of the TCAM table; otherwise, allocate from the
  3258. * top of the table in order to give it higher priority
  3259. */
  3260. status = ice_alloc_tcam_ent(hw, blk, map->attr[i].mask == 0,
  3261. &tcam_idx);
  3262. if (status) {
  3263. devm_kfree(ice_hw_to_dev(hw), p);
  3264. goto err_ice_add_prof_id_vsig;
  3265. }
  3266. t->tcam[i].ptg = map->ptg[i];
  3267. t->tcam[i].prof_id = map->prof_id;
  3268. t->tcam[i].tcam_idx = tcam_idx;
  3269. t->tcam[i].attr = map->attr[i];
  3270. t->tcam[i].in_use = true;
  3271. p->type = ICE_TCAM_ADD;
  3272. p->add_tcam_idx = true;
  3273. p->prof_id = t->tcam[i].prof_id;
  3274. p->ptg = t->tcam[i].ptg;
  3275. p->vsig = vsig;
  3276. p->tcam_idx = t->tcam[i].tcam_idx;
  3277. /* write the TCAM entry */
  3278. status = ice_tcam_write_entry(hw, blk, t->tcam[i].tcam_idx,
  3279. t->tcam[i].prof_id,
  3280. t->tcam[i].ptg, vsig, 0, 0,
  3281. vl_msk, dc_msk, nm_msk);
  3282. if (status) {
  3283. devm_kfree(ice_hw_to_dev(hw), p);
  3284. goto err_ice_add_prof_id_vsig;
  3285. }
  3286. /* log change */
  3287. list_add(&p->list_entry, chg);
  3288. }
  3289. /* add profile to VSIG */
  3290. vsig_idx = vsig & ICE_VSIG_IDX_M;
  3291. if (rev)
  3292. list_add_tail(&t->list,
  3293. &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
  3294. else
  3295. list_add(&t->list,
  3296. &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
  3297. mutex_unlock(&hw->blk[blk].es.prof_map_lock);
  3298. return status;
  3299. err_ice_add_prof_id_vsig:
  3300. mutex_unlock(&hw->blk[blk].es.prof_map_lock);
  3301. /* let caller clean up the change list */
  3302. devm_kfree(ice_hw_to_dev(hw), t);
  3303. return status;
  3304. }
  3305. /**
  3306. * ice_create_prof_id_vsig - add a new VSIG with a single profile
  3307. * @hw: pointer to the HW struct
  3308. * @blk: hardware block
  3309. * @vsi: the initial VSI that will be in VSIG
  3310. * @hdl: the profile handle of the profile that will be added to the VSIG
  3311. * @chg: the change list
  3312. */
  3313. static int
  3314. ice_create_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl,
  3315. struct list_head *chg)
  3316. {
  3317. struct ice_chs_chg *p;
  3318. u16 new_vsig;
  3319. int status;
  3320. p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
  3321. if (!p)
  3322. return -ENOMEM;
  3323. new_vsig = ice_vsig_alloc(hw, blk);
  3324. if (!new_vsig) {
  3325. status = -EIO;
  3326. goto err_ice_create_prof_id_vsig;
  3327. }
  3328. status = ice_move_vsi(hw, blk, vsi, new_vsig, chg);
  3329. if (status)
  3330. goto err_ice_create_prof_id_vsig;
  3331. status = ice_add_prof_id_vsig(hw, blk, new_vsig, hdl, false, chg);
  3332. if (status)
  3333. goto err_ice_create_prof_id_vsig;
  3334. p->type = ICE_VSIG_ADD;
  3335. p->vsi = vsi;
  3336. p->orig_vsig = ICE_DEFAULT_VSIG;
  3337. p->vsig = new_vsig;
  3338. list_add(&p->list_entry, chg);
  3339. return 0;
  3340. err_ice_create_prof_id_vsig:
  3341. /* let caller clean up the change list */
  3342. devm_kfree(ice_hw_to_dev(hw), p);
  3343. return status;
  3344. }
  3345. /**
  3346. * ice_create_vsig_from_lst - create a new VSIG with a list of profiles
  3347. * @hw: pointer to the HW struct
  3348. * @blk: hardware block
  3349. * @vsi: the initial VSI that will be in VSIG
  3350. * @lst: the list of profile that will be added to the VSIG
  3351. * @new_vsig: return of new VSIG
  3352. * @chg: the change list
  3353. */
  3354. static int
  3355. ice_create_vsig_from_lst(struct ice_hw *hw, enum ice_block blk, u16 vsi,
  3356. struct list_head *lst, u16 *new_vsig,
  3357. struct list_head *chg)
  3358. {
  3359. struct ice_vsig_prof *t;
  3360. int status;
  3361. u16 vsig;
  3362. vsig = ice_vsig_alloc(hw, blk);
  3363. if (!vsig)
  3364. return -EIO;
  3365. status = ice_move_vsi(hw, blk, vsi, vsig, chg);
  3366. if (status)
  3367. return status;
  3368. list_for_each_entry(t, lst, list) {
  3369. /* Reverse the order here since we are copying the list */
  3370. status = ice_add_prof_id_vsig(hw, blk, vsig, t->profile_cookie,
  3371. true, chg);
  3372. if (status)
  3373. return status;
  3374. }
  3375. *new_vsig = vsig;
  3376. return 0;
  3377. }
  3378. /**
  3379. * ice_find_prof_vsig - find a VSIG with a specific profile handle
  3380. * @hw: pointer to the HW struct
  3381. * @blk: hardware block
  3382. * @hdl: the profile handle of the profile to search for
  3383. * @vsig: returns the VSIG with the matching profile
  3384. */
  3385. static bool
  3386. ice_find_prof_vsig(struct ice_hw *hw, enum ice_block blk, u64 hdl, u16 *vsig)
  3387. {
  3388. struct ice_vsig_prof *t;
  3389. struct list_head lst;
  3390. int status;
  3391. INIT_LIST_HEAD(&lst);
  3392. t = kzalloc(sizeof(*t), GFP_KERNEL);
  3393. if (!t)
  3394. return false;
  3395. t->profile_cookie = hdl;
  3396. list_add(&t->list, &lst);
  3397. status = ice_find_dup_props_vsig(hw, blk, &lst, vsig);
  3398. list_del(&t->list);
  3399. kfree(t);
  3400. return !status;
  3401. }
  3402. /**
  3403. * ice_add_prof_id_flow - add profile flow
  3404. * @hw: pointer to the HW struct
  3405. * @blk: hardware block
  3406. * @vsi: the VSI to enable with the profile specified by ID
  3407. * @hdl: profile handle
  3408. *
  3409. * Calling this function will update the hardware tables to enable the
  3410. * profile indicated by the ID parameter for the VSIs specified in the VSI
  3411. * array. Once successfully called, the flow will be enabled.
  3412. */
  3413. int
  3414. ice_add_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
  3415. {
  3416. struct ice_vsig_prof *tmp1, *del1;
  3417. struct ice_chs_chg *tmp, *del;
  3418. struct list_head union_lst;
  3419. struct list_head chg;
  3420. int status;
  3421. u16 vsig;
  3422. INIT_LIST_HEAD(&union_lst);
  3423. INIT_LIST_HEAD(&chg);
  3424. /* Get profile */
  3425. status = ice_get_prof(hw, blk, hdl, &chg);
  3426. if (status)
  3427. return status;
  3428. /* determine if VSI is already part of a VSIG */
  3429. status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
  3430. if (!status && vsig) {
  3431. bool only_vsi;
  3432. u16 or_vsig;
  3433. u16 ref;
  3434. /* found in VSIG */
  3435. or_vsig = vsig;
  3436. /* make sure that there is no overlap/conflict between the new
  3437. * characteristics and the existing ones; we don't support that
  3438. * scenario
  3439. */
  3440. if (ice_has_prof_vsig(hw, blk, vsig, hdl)) {
  3441. status = -EEXIST;
  3442. goto err_ice_add_prof_id_flow;
  3443. }
  3444. /* last VSI in the VSIG? */
  3445. status = ice_vsig_get_ref(hw, blk, vsig, &ref);
  3446. if (status)
  3447. goto err_ice_add_prof_id_flow;
  3448. only_vsi = (ref == 1);
  3449. /* create a union of the current profiles and the one being
  3450. * added
  3451. */
  3452. status = ice_get_profs_vsig(hw, blk, vsig, &union_lst);
  3453. if (status)
  3454. goto err_ice_add_prof_id_flow;
  3455. status = ice_add_prof_to_lst(hw, blk, &union_lst, hdl);
  3456. if (status)
  3457. goto err_ice_add_prof_id_flow;
  3458. /* search for an existing VSIG with an exact charc match */
  3459. status = ice_find_dup_props_vsig(hw, blk, &union_lst, &vsig);
  3460. if (!status) {
  3461. /* move VSI to the VSIG that matches */
  3462. status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
  3463. if (status)
  3464. goto err_ice_add_prof_id_flow;
  3465. /* VSI has been moved out of or_vsig. If the or_vsig had
  3466. * only that VSI it is now empty and can be removed.
  3467. */
  3468. if (only_vsi) {
  3469. status = ice_rem_vsig(hw, blk, or_vsig, &chg);
  3470. if (status)
  3471. goto err_ice_add_prof_id_flow;
  3472. }
  3473. } else if (only_vsi) {
  3474. /* If the original VSIG only contains one VSI, then it
  3475. * will be the requesting VSI. In this case the VSI is
  3476. * not sharing entries and we can simply add the new
  3477. * profile to the VSIG.
  3478. */
  3479. status = ice_add_prof_id_vsig(hw, blk, vsig, hdl, false,
  3480. &chg);
  3481. if (status)
  3482. goto err_ice_add_prof_id_flow;
  3483. /* Adjust priorities */
  3484. status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
  3485. if (status)
  3486. goto err_ice_add_prof_id_flow;
  3487. } else {
  3488. /* No match, so we need a new VSIG */
  3489. status = ice_create_vsig_from_lst(hw, blk, vsi,
  3490. &union_lst, &vsig,
  3491. &chg);
  3492. if (status)
  3493. goto err_ice_add_prof_id_flow;
  3494. /* Adjust priorities */
  3495. status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
  3496. if (status)
  3497. goto err_ice_add_prof_id_flow;
  3498. }
  3499. } else {
  3500. /* need to find or add a VSIG */
  3501. /* search for an existing VSIG with an exact charc match */
  3502. if (ice_find_prof_vsig(hw, blk, hdl, &vsig)) {
  3503. /* found an exact match */
  3504. /* add or move VSI to the VSIG that matches */
  3505. status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
  3506. if (status)
  3507. goto err_ice_add_prof_id_flow;
  3508. } else {
  3509. /* we did not find an exact match */
  3510. /* we need to add a VSIG */
  3511. status = ice_create_prof_id_vsig(hw, blk, vsi, hdl,
  3512. &chg);
  3513. if (status)
  3514. goto err_ice_add_prof_id_flow;
  3515. }
  3516. }
  3517. /* update hardware */
  3518. if (!status)
  3519. status = ice_upd_prof_hw(hw, blk, &chg);
  3520. err_ice_add_prof_id_flow:
  3521. list_for_each_entry_safe(del, tmp, &chg, list_entry) {
  3522. list_del(&del->list_entry);
  3523. devm_kfree(ice_hw_to_dev(hw), del);
  3524. }
  3525. list_for_each_entry_safe(del1, tmp1, &union_lst, list) {
  3526. list_del(&del1->list);
  3527. devm_kfree(ice_hw_to_dev(hw), del1);
  3528. }
  3529. return status;
  3530. }
  3531. /**
  3532. * ice_flow_assoc_fdir_prof - add an FDIR profile for main/ctrl VSI
  3533. * @hw: pointer to the HW struct
  3534. * @blk: HW block
  3535. * @dest_vsi: dest VSI
  3536. * @fdir_vsi: fdir programming VSI
  3537. * @hdl: profile handle
  3538. *
  3539. * Update the hardware tables to enable the FDIR profile indicated by @hdl for
  3540. * the VSI specified by @dest_vsi. On success, the flow will be enabled.
  3541. *
  3542. * Return: 0 on success or negative errno on failure.
  3543. */
  3544. int
  3545. ice_flow_assoc_fdir_prof(struct ice_hw *hw, enum ice_block blk,
  3546. u16 dest_vsi, u16 fdir_vsi, u64 hdl)
  3547. {
  3548. u16 vsi_num;
  3549. int status;
  3550. if (blk != ICE_BLK_FD)
  3551. return -EINVAL;
  3552. vsi_num = ice_get_hw_vsi_num(hw, dest_vsi);
  3553. status = ice_add_prof_id_flow(hw, blk, vsi_num, hdl);
  3554. if (status) {
  3555. ice_debug(hw, ICE_DBG_FLOW, "Adding HW profile failed for main VSI flow entry: %d\n",
  3556. status);
  3557. return status;
  3558. }
  3559. vsi_num = ice_get_hw_vsi_num(hw, fdir_vsi);
  3560. status = ice_add_prof_id_flow(hw, blk, vsi_num, hdl);
  3561. if (status) {
  3562. ice_debug(hw, ICE_DBG_FLOW, "Adding HW profile failed for ctrl VSI flow entry: %d\n",
  3563. status);
  3564. goto err;
  3565. }
  3566. return 0;
  3567. err:
  3568. vsi_num = ice_get_hw_vsi_num(hw, dest_vsi);
  3569. ice_rem_prof_id_flow(hw, blk, vsi_num, hdl);
  3570. return status;
  3571. }
  3572. /**
  3573. * ice_rem_prof_from_list - remove a profile from list
  3574. * @hw: pointer to the HW struct
  3575. * @lst: list to remove the profile from
  3576. * @hdl: the profile handle indicating the profile to remove
  3577. */
  3578. static int
  3579. ice_rem_prof_from_list(struct ice_hw *hw, struct list_head *lst, u64 hdl)
  3580. {
  3581. struct ice_vsig_prof *ent, *tmp;
  3582. list_for_each_entry_safe(ent, tmp, lst, list)
  3583. if (ent->profile_cookie == hdl) {
  3584. list_del(&ent->list);
  3585. devm_kfree(ice_hw_to_dev(hw), ent);
  3586. return 0;
  3587. }
  3588. return -ENOENT;
  3589. }
  3590. /**
  3591. * ice_rem_prof_id_flow - remove flow
  3592. * @hw: pointer to the HW struct
  3593. * @blk: hardware block
  3594. * @vsi: the VSI from which to remove the profile specified by ID
  3595. * @hdl: profile tracking handle
  3596. *
  3597. * Calling this function will update the hardware tables to remove the
  3598. * profile indicated by the ID parameter for the VSIs specified in the VSI
  3599. * array. Once successfully called, the flow will be disabled.
  3600. */
  3601. int
  3602. ice_rem_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
  3603. {
  3604. struct ice_vsig_prof *tmp1, *del1;
  3605. struct ice_chs_chg *tmp, *del;
  3606. struct list_head chg, copy;
  3607. int status;
  3608. u16 vsig;
  3609. INIT_LIST_HEAD(&copy);
  3610. INIT_LIST_HEAD(&chg);
  3611. /* determine if VSI is already part of a VSIG */
  3612. status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
  3613. if (!status && vsig) {
  3614. bool last_profile;
  3615. bool only_vsi;
  3616. u16 ref;
  3617. /* found in VSIG */
  3618. last_profile = ice_vsig_prof_id_count(hw, blk, vsig) == 1;
  3619. status = ice_vsig_get_ref(hw, blk, vsig, &ref);
  3620. if (status)
  3621. goto err_ice_rem_prof_id_flow;
  3622. only_vsi = (ref == 1);
  3623. if (only_vsi) {
  3624. /* If the original VSIG only contains one reference,
  3625. * which will be the requesting VSI, then the VSI is not
  3626. * sharing entries and we can simply remove the specific
  3627. * characteristics from the VSIG.
  3628. */
  3629. if (last_profile) {
  3630. /* If there are no profiles left for this VSIG,
  3631. * then simply remove the VSIG.
  3632. */
  3633. status = ice_rem_vsig(hw, blk, vsig, &chg);
  3634. if (status)
  3635. goto err_ice_rem_prof_id_flow;
  3636. } else {
  3637. status = ice_rem_prof_id_vsig(hw, blk, vsig,
  3638. hdl, &chg);
  3639. if (status)
  3640. goto err_ice_rem_prof_id_flow;
  3641. /* Adjust priorities */
  3642. status = ice_adj_prof_priorities(hw, blk, vsig,
  3643. &chg);
  3644. if (status)
  3645. goto err_ice_rem_prof_id_flow;
  3646. }
  3647. } else {
  3648. /* Make a copy of the VSIG's list of Profiles */
  3649. status = ice_get_profs_vsig(hw, blk, vsig, &copy);
  3650. if (status)
  3651. goto err_ice_rem_prof_id_flow;
  3652. /* Remove specified profile entry from the list */
  3653. status = ice_rem_prof_from_list(hw, &copy, hdl);
  3654. if (status)
  3655. goto err_ice_rem_prof_id_flow;
  3656. if (list_empty(&copy)) {
  3657. status = ice_move_vsi(hw, blk, vsi,
  3658. ICE_DEFAULT_VSIG, &chg);
  3659. if (status)
  3660. goto err_ice_rem_prof_id_flow;
  3661. } else if (!ice_find_dup_props_vsig(hw, blk, &copy,
  3662. &vsig)) {
  3663. /* found an exact match */
  3664. /* add or move VSI to the VSIG that matches */
  3665. /* Search for a VSIG with a matching profile
  3666. * list
  3667. */
  3668. /* Found match, move VSI to the matching VSIG */
  3669. status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
  3670. if (status)
  3671. goto err_ice_rem_prof_id_flow;
  3672. } else {
  3673. /* since no existing VSIG supports this
  3674. * characteristic pattern, we need to create a
  3675. * new VSIG and TCAM entries
  3676. */
  3677. status = ice_create_vsig_from_lst(hw, blk, vsi,
  3678. &copy, &vsig,
  3679. &chg);
  3680. if (status)
  3681. goto err_ice_rem_prof_id_flow;
  3682. /* Adjust priorities */
  3683. status = ice_adj_prof_priorities(hw, blk, vsig,
  3684. &chg);
  3685. if (status)
  3686. goto err_ice_rem_prof_id_flow;
  3687. }
  3688. }
  3689. } else {
  3690. status = -ENOENT;
  3691. }
  3692. /* update hardware tables */
  3693. if (!status)
  3694. status = ice_upd_prof_hw(hw, blk, &chg);
  3695. err_ice_rem_prof_id_flow:
  3696. list_for_each_entry_safe(del, tmp, &chg, list_entry) {
  3697. list_del(&del->list_entry);
  3698. devm_kfree(ice_hw_to_dev(hw), del);
  3699. }
  3700. list_for_each_entry_safe(del1, tmp1, &copy, list) {
  3701. list_del(&del1->list);
  3702. devm_kfree(ice_hw_to_dev(hw), del1);
  3703. }
  3704. return status;
  3705. }