resource_tracker.c 127 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies.
  4. * All rights reserved.
  5. * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/sched.h>
  36. #include <linux/pci.h>
  37. #include <linux/errno.h>
  38. #include <linux/kernel.h>
  39. #include <linux/io.h>
  40. #include <linux/slab.h>
  41. #include <linux/mlx4/cmd.h>
  42. #include <linux/mlx4/qp.h>
  43. #include <linux/if_ether.h>
  44. #include <linux/etherdevice.h>
  45. #include "mlx4.h"
  46. #include "fw.h"
  47. #include "mlx4_stats.h"
  48. #define MLX4_MAC_VALID (1ull << 63)
  49. #define MLX4_PF_COUNTERS_PER_PORT 2
  50. #define MLX4_VF_COUNTERS_PER_PORT 1
  51. struct mac_res {
  52. struct list_head list;
  53. u64 mac;
  54. int ref_count;
  55. u8 smac_index;
  56. u8 port;
  57. };
  58. struct vlan_res {
  59. struct list_head list;
  60. u16 vlan;
  61. int ref_count;
  62. int vlan_index;
  63. u8 port;
  64. };
  65. struct res_common {
  66. struct list_head list;
  67. struct rb_node node;
  68. u64 res_id;
  69. int owner;
  70. int state;
  71. int from_state;
  72. int to_state;
  73. int removing;
  74. };
  75. enum {
  76. RES_ANY_BUSY = 1
  77. };
  78. struct res_gid {
  79. struct list_head list;
  80. u8 gid[16];
  81. enum mlx4_protocol prot;
  82. enum mlx4_steer_type steer;
  83. u64 reg_id;
  84. };
  85. enum res_qp_states {
  86. RES_QP_BUSY = RES_ANY_BUSY,
  87. /* QP number was allocated */
  88. RES_QP_RESERVED,
  89. /* ICM memory for QP context was mapped */
  90. RES_QP_MAPPED,
  91. /* QP is in hw ownership */
  92. RES_QP_HW
  93. };
  94. struct res_qp {
  95. struct res_common com;
  96. struct res_mtt *mtt;
  97. struct res_cq *rcq;
  98. struct res_cq *scq;
  99. struct res_srq *srq;
  100. struct list_head mcg_list;
  101. spinlock_t mcg_spl;
  102. int local_qpn;
  103. atomic_t ref_count;
  104. u32 qpc_flags;
  105. /* saved qp params before VST enforcement in order to restore on VGT */
  106. u8 sched_queue;
  107. __be32 param3;
  108. u8 vlan_control;
  109. u8 fvl_rx;
  110. u8 pri_path_fl;
  111. u8 vlan_index;
  112. u8 feup;
  113. };
  114. enum res_mtt_states {
  115. RES_MTT_BUSY = RES_ANY_BUSY,
  116. RES_MTT_ALLOCATED,
  117. };
  118. static inline const char *mtt_states_str(enum res_mtt_states state)
  119. {
  120. switch (state) {
  121. case RES_MTT_BUSY: return "RES_MTT_BUSY";
  122. case RES_MTT_ALLOCATED: return "RES_MTT_ALLOCATED";
  123. default: return "Unknown";
  124. }
  125. }
  126. struct res_mtt {
  127. struct res_common com;
  128. int order;
  129. atomic_t ref_count;
  130. };
  131. enum res_mpt_states {
  132. RES_MPT_BUSY = RES_ANY_BUSY,
  133. RES_MPT_RESERVED,
  134. RES_MPT_MAPPED,
  135. RES_MPT_HW,
  136. };
  137. struct res_mpt {
  138. struct res_common com;
  139. struct res_mtt *mtt;
  140. int key;
  141. };
  142. enum res_eq_states {
  143. RES_EQ_BUSY = RES_ANY_BUSY,
  144. RES_EQ_RESERVED,
  145. RES_EQ_HW,
  146. };
  147. struct res_eq {
  148. struct res_common com;
  149. struct res_mtt *mtt;
  150. };
  151. enum res_cq_states {
  152. RES_CQ_BUSY = RES_ANY_BUSY,
  153. RES_CQ_ALLOCATED,
  154. RES_CQ_HW,
  155. };
  156. struct res_cq {
  157. struct res_common com;
  158. struct res_mtt *mtt;
  159. atomic_t ref_count;
  160. };
  161. enum res_srq_states {
  162. RES_SRQ_BUSY = RES_ANY_BUSY,
  163. RES_SRQ_ALLOCATED,
  164. RES_SRQ_HW,
  165. };
  166. struct res_srq {
  167. struct res_common com;
  168. struct res_mtt *mtt;
  169. struct res_cq *cq;
  170. atomic_t ref_count;
  171. };
  172. enum res_counter_states {
  173. RES_COUNTER_BUSY = RES_ANY_BUSY,
  174. RES_COUNTER_ALLOCATED,
  175. };
  176. struct res_counter {
  177. struct res_common com;
  178. int port;
  179. };
  180. enum res_xrcdn_states {
  181. RES_XRCD_BUSY = RES_ANY_BUSY,
  182. RES_XRCD_ALLOCATED,
  183. };
  184. struct res_xrcdn {
  185. struct res_common com;
  186. int port;
  187. };
  188. enum res_fs_rule_states {
  189. RES_FS_RULE_BUSY = RES_ANY_BUSY,
  190. RES_FS_RULE_ALLOCATED,
  191. };
  192. struct res_fs_rule {
  193. struct res_common com;
  194. int qpn;
  195. };
  196. static void *res_tracker_lookup(struct rb_root *root, u64 res_id)
  197. {
  198. struct rb_node *node = root->rb_node;
  199. while (node) {
  200. struct res_common *res = container_of(node, struct res_common,
  201. node);
  202. if (res_id < res->res_id)
  203. node = node->rb_left;
  204. else if (res_id > res->res_id)
  205. node = node->rb_right;
  206. else
  207. return res;
  208. }
  209. return NULL;
  210. }
  211. static int res_tracker_insert(struct rb_root *root, struct res_common *res)
  212. {
  213. struct rb_node **new = &(root->rb_node), *parent = NULL;
  214. /* Figure out where to put new node */
  215. while (*new) {
  216. struct res_common *this = container_of(*new, struct res_common,
  217. node);
  218. parent = *new;
  219. if (res->res_id < this->res_id)
  220. new = &((*new)->rb_left);
  221. else if (res->res_id > this->res_id)
  222. new = &((*new)->rb_right);
  223. else
  224. return -EEXIST;
  225. }
  226. /* Add new node and rebalance tree. */
  227. rb_link_node(&res->node, parent, new);
  228. rb_insert_color(&res->node, root);
  229. return 0;
  230. }
  231. enum qp_transition {
  232. QP_TRANS_INIT2RTR,
  233. QP_TRANS_RTR2RTS,
  234. QP_TRANS_RTS2RTS,
  235. QP_TRANS_SQERR2RTS,
  236. QP_TRANS_SQD2SQD,
  237. QP_TRANS_SQD2RTS
  238. };
  239. /* For Debug uses */
  240. static const char *resource_str(enum mlx4_resource rt)
  241. {
  242. switch (rt) {
  243. case RES_QP: return "RES_QP";
  244. case RES_CQ: return "RES_CQ";
  245. case RES_SRQ: return "RES_SRQ";
  246. case RES_MPT: return "RES_MPT";
  247. case RES_MTT: return "RES_MTT";
  248. case RES_MAC: return "RES_MAC";
  249. case RES_VLAN: return "RES_VLAN";
  250. case RES_EQ: return "RES_EQ";
  251. case RES_COUNTER: return "RES_COUNTER";
  252. case RES_FS_RULE: return "RES_FS_RULE";
  253. case RES_XRCD: return "RES_XRCD";
  254. default: return "Unknown resource type !!!";
  255. };
  256. }
  257. static void rem_slave_vlans(struct mlx4_dev *dev, int slave);
  258. static inline int mlx4_grant_resource(struct mlx4_dev *dev, int slave,
  259. enum mlx4_resource res_type, int count,
  260. int port)
  261. {
  262. struct mlx4_priv *priv = mlx4_priv(dev);
  263. struct resource_allocator *res_alloc =
  264. &priv->mfunc.master.res_tracker.res_alloc[res_type];
  265. int err = -EINVAL;
  266. int allocated, free, reserved, guaranteed, from_free;
  267. int from_rsvd;
  268. if (slave > dev->persist->num_vfs)
  269. return -EINVAL;
  270. spin_lock(&res_alloc->alloc_lock);
  271. allocated = (port > 0) ?
  272. res_alloc->allocated[(port - 1) *
  273. (dev->persist->num_vfs + 1) + slave] :
  274. res_alloc->allocated[slave];
  275. free = (port > 0) ? res_alloc->res_port_free[port - 1] :
  276. res_alloc->res_free;
  277. reserved = (port > 0) ? res_alloc->res_port_rsvd[port - 1] :
  278. res_alloc->res_reserved;
  279. guaranteed = res_alloc->guaranteed[slave];
  280. if (allocated + count > res_alloc->quota[slave]) {
  281. mlx4_warn(dev, "VF %d port %d res %s: quota exceeded, count %d alloc %d quota %d\n",
  282. slave, port, resource_str(res_type), count,
  283. allocated, res_alloc->quota[slave]);
  284. goto out;
  285. }
  286. if (allocated + count <= guaranteed) {
  287. err = 0;
  288. from_rsvd = count;
  289. } else {
  290. /* portion may need to be obtained from free area */
  291. if (guaranteed - allocated > 0)
  292. from_free = count - (guaranteed - allocated);
  293. else
  294. from_free = count;
  295. from_rsvd = count - from_free;
  296. if (free - from_free >= reserved)
  297. err = 0;
  298. else
  299. mlx4_warn(dev, "VF %d port %d res %s: free pool empty, free %d from_free %d rsvd %d\n",
  300. slave, port, resource_str(res_type), free,
  301. from_free, reserved);
  302. }
  303. if (!err) {
  304. /* grant the request */
  305. if (port > 0) {
  306. res_alloc->allocated[(port - 1) *
  307. (dev->persist->num_vfs + 1) + slave] += count;
  308. res_alloc->res_port_free[port - 1] -= count;
  309. res_alloc->res_port_rsvd[port - 1] -= from_rsvd;
  310. } else {
  311. res_alloc->allocated[slave] += count;
  312. res_alloc->res_free -= count;
  313. res_alloc->res_reserved -= from_rsvd;
  314. }
  315. }
  316. out:
  317. spin_unlock(&res_alloc->alloc_lock);
  318. return err;
  319. }
  320. static inline void mlx4_release_resource(struct mlx4_dev *dev, int slave,
  321. enum mlx4_resource res_type, int count,
  322. int port)
  323. {
  324. struct mlx4_priv *priv = mlx4_priv(dev);
  325. struct resource_allocator *res_alloc =
  326. &priv->mfunc.master.res_tracker.res_alloc[res_type];
  327. int allocated, guaranteed, from_rsvd;
  328. if (slave > dev->persist->num_vfs)
  329. return;
  330. spin_lock(&res_alloc->alloc_lock);
  331. allocated = (port > 0) ?
  332. res_alloc->allocated[(port - 1) *
  333. (dev->persist->num_vfs + 1) + slave] :
  334. res_alloc->allocated[slave];
  335. guaranteed = res_alloc->guaranteed[slave];
  336. if (allocated - count >= guaranteed) {
  337. from_rsvd = 0;
  338. } else {
  339. /* portion may need to be returned to reserved area */
  340. if (allocated - guaranteed > 0)
  341. from_rsvd = count - (allocated - guaranteed);
  342. else
  343. from_rsvd = count;
  344. }
  345. if (port > 0) {
  346. res_alloc->allocated[(port - 1) *
  347. (dev->persist->num_vfs + 1) + slave] -= count;
  348. res_alloc->res_port_free[port - 1] += count;
  349. res_alloc->res_port_rsvd[port - 1] += from_rsvd;
  350. } else {
  351. res_alloc->allocated[slave] -= count;
  352. res_alloc->res_free += count;
  353. res_alloc->res_reserved += from_rsvd;
  354. }
  355. spin_unlock(&res_alloc->alloc_lock);
  356. return;
  357. }
  358. static inline void initialize_res_quotas(struct mlx4_dev *dev,
  359. struct resource_allocator *res_alloc,
  360. enum mlx4_resource res_type,
  361. int vf, int num_instances)
  362. {
  363. res_alloc->guaranteed[vf] = num_instances /
  364. (2 * (dev->persist->num_vfs + 1));
  365. res_alloc->quota[vf] = (num_instances / 2) + res_alloc->guaranteed[vf];
  366. if (vf == mlx4_master_func_num(dev)) {
  367. res_alloc->res_free = num_instances;
  368. if (res_type == RES_MTT) {
  369. /* reserved mtts will be taken out of the PF allocation */
  370. res_alloc->res_free += dev->caps.reserved_mtts;
  371. res_alloc->guaranteed[vf] += dev->caps.reserved_mtts;
  372. res_alloc->quota[vf] += dev->caps.reserved_mtts;
  373. }
  374. }
  375. }
  376. void mlx4_init_quotas(struct mlx4_dev *dev)
  377. {
  378. struct mlx4_priv *priv = mlx4_priv(dev);
  379. int pf;
  380. /* quotas for VFs are initialized in mlx4_slave_cap */
  381. if (mlx4_is_slave(dev))
  382. return;
  383. if (!mlx4_is_mfunc(dev)) {
  384. dev->quotas.qp = dev->caps.num_qps - dev->caps.reserved_qps -
  385. mlx4_num_reserved_sqps(dev);
  386. dev->quotas.cq = dev->caps.num_cqs - dev->caps.reserved_cqs;
  387. dev->quotas.srq = dev->caps.num_srqs - dev->caps.reserved_srqs;
  388. dev->quotas.mtt = dev->caps.num_mtts - dev->caps.reserved_mtts;
  389. dev->quotas.mpt = dev->caps.num_mpts - dev->caps.reserved_mrws;
  390. return;
  391. }
  392. pf = mlx4_master_func_num(dev);
  393. dev->quotas.qp =
  394. priv->mfunc.master.res_tracker.res_alloc[RES_QP].quota[pf];
  395. dev->quotas.cq =
  396. priv->mfunc.master.res_tracker.res_alloc[RES_CQ].quota[pf];
  397. dev->quotas.srq =
  398. priv->mfunc.master.res_tracker.res_alloc[RES_SRQ].quota[pf];
  399. dev->quotas.mtt =
  400. priv->mfunc.master.res_tracker.res_alloc[RES_MTT].quota[pf];
  401. dev->quotas.mpt =
  402. priv->mfunc.master.res_tracker.res_alloc[RES_MPT].quota[pf];
  403. }
  404. static int get_max_gauranteed_vfs_counter(struct mlx4_dev *dev)
  405. {
  406. /* reduce the sink counter */
  407. return (dev->caps.max_counters - 1 -
  408. (MLX4_PF_COUNTERS_PER_PORT * MLX4_MAX_PORTS))
  409. / MLX4_MAX_PORTS;
  410. }
  411. int mlx4_init_resource_tracker(struct mlx4_dev *dev)
  412. {
  413. struct mlx4_priv *priv = mlx4_priv(dev);
  414. int i, j;
  415. int t;
  416. int max_vfs_guarantee_counter = get_max_gauranteed_vfs_counter(dev);
  417. priv->mfunc.master.res_tracker.slave_list =
  418. kzalloc(dev->num_slaves * sizeof(struct slave_list),
  419. GFP_KERNEL);
  420. if (!priv->mfunc.master.res_tracker.slave_list)
  421. return -ENOMEM;
  422. for (i = 0 ; i < dev->num_slaves; i++) {
  423. for (t = 0; t < MLX4_NUM_OF_RESOURCE_TYPE; ++t)
  424. INIT_LIST_HEAD(&priv->mfunc.master.res_tracker.
  425. slave_list[i].res_list[t]);
  426. mutex_init(&priv->mfunc.master.res_tracker.slave_list[i].mutex);
  427. }
  428. mlx4_dbg(dev, "Started init_resource_tracker: %ld slaves\n",
  429. dev->num_slaves);
  430. for (i = 0 ; i < MLX4_NUM_OF_RESOURCE_TYPE; i++)
  431. priv->mfunc.master.res_tracker.res_tree[i] = RB_ROOT;
  432. for (i = 0; i < MLX4_NUM_OF_RESOURCE_TYPE; i++) {
  433. struct resource_allocator *res_alloc =
  434. &priv->mfunc.master.res_tracker.res_alloc[i];
  435. res_alloc->quota = kmalloc((dev->persist->num_vfs + 1) *
  436. sizeof(int), GFP_KERNEL);
  437. res_alloc->guaranteed = kmalloc((dev->persist->num_vfs + 1) *
  438. sizeof(int), GFP_KERNEL);
  439. if (i == RES_MAC || i == RES_VLAN)
  440. res_alloc->allocated = kzalloc(MLX4_MAX_PORTS *
  441. (dev->persist->num_vfs
  442. + 1) *
  443. sizeof(int), GFP_KERNEL);
  444. else
  445. res_alloc->allocated = kzalloc((dev->persist->
  446. num_vfs + 1) *
  447. sizeof(int), GFP_KERNEL);
  448. /* Reduce the sink counter */
  449. if (i == RES_COUNTER)
  450. res_alloc->res_free = dev->caps.max_counters - 1;
  451. if (!res_alloc->quota || !res_alloc->guaranteed ||
  452. !res_alloc->allocated)
  453. goto no_mem_err;
  454. spin_lock_init(&res_alloc->alloc_lock);
  455. for (t = 0; t < dev->persist->num_vfs + 1; t++) {
  456. struct mlx4_active_ports actv_ports =
  457. mlx4_get_active_ports(dev, t);
  458. switch (i) {
  459. case RES_QP:
  460. initialize_res_quotas(dev, res_alloc, RES_QP,
  461. t, dev->caps.num_qps -
  462. dev->caps.reserved_qps -
  463. mlx4_num_reserved_sqps(dev));
  464. break;
  465. case RES_CQ:
  466. initialize_res_quotas(dev, res_alloc, RES_CQ,
  467. t, dev->caps.num_cqs -
  468. dev->caps.reserved_cqs);
  469. break;
  470. case RES_SRQ:
  471. initialize_res_quotas(dev, res_alloc, RES_SRQ,
  472. t, dev->caps.num_srqs -
  473. dev->caps.reserved_srqs);
  474. break;
  475. case RES_MPT:
  476. initialize_res_quotas(dev, res_alloc, RES_MPT,
  477. t, dev->caps.num_mpts -
  478. dev->caps.reserved_mrws);
  479. break;
  480. case RES_MTT:
  481. initialize_res_quotas(dev, res_alloc, RES_MTT,
  482. t, dev->caps.num_mtts -
  483. dev->caps.reserved_mtts);
  484. break;
  485. case RES_MAC:
  486. if (t == mlx4_master_func_num(dev)) {
  487. int max_vfs_pport = 0;
  488. /* Calculate the max vfs per port for */
  489. /* both ports. */
  490. for (j = 0; j < dev->caps.num_ports;
  491. j++) {
  492. struct mlx4_slaves_pport slaves_pport =
  493. mlx4_phys_to_slaves_pport(dev, j + 1);
  494. unsigned current_slaves =
  495. bitmap_weight(slaves_pport.slaves,
  496. dev->caps.num_ports) - 1;
  497. if (max_vfs_pport < current_slaves)
  498. max_vfs_pport =
  499. current_slaves;
  500. }
  501. res_alloc->quota[t] =
  502. MLX4_MAX_MAC_NUM -
  503. 2 * max_vfs_pport;
  504. res_alloc->guaranteed[t] = 2;
  505. for (j = 0; j < MLX4_MAX_PORTS; j++)
  506. res_alloc->res_port_free[j] =
  507. MLX4_MAX_MAC_NUM;
  508. } else {
  509. res_alloc->quota[t] = MLX4_MAX_MAC_NUM;
  510. res_alloc->guaranteed[t] = 2;
  511. }
  512. break;
  513. case RES_VLAN:
  514. if (t == mlx4_master_func_num(dev)) {
  515. res_alloc->quota[t] = MLX4_MAX_VLAN_NUM;
  516. res_alloc->guaranteed[t] = MLX4_MAX_VLAN_NUM / 2;
  517. for (j = 0; j < MLX4_MAX_PORTS; j++)
  518. res_alloc->res_port_free[j] =
  519. res_alloc->quota[t];
  520. } else {
  521. res_alloc->quota[t] = MLX4_MAX_VLAN_NUM / 2;
  522. res_alloc->guaranteed[t] = 0;
  523. }
  524. break;
  525. case RES_COUNTER:
  526. res_alloc->quota[t] = dev->caps.max_counters;
  527. if (t == mlx4_master_func_num(dev))
  528. res_alloc->guaranteed[t] =
  529. MLX4_PF_COUNTERS_PER_PORT *
  530. MLX4_MAX_PORTS;
  531. else if (t <= max_vfs_guarantee_counter)
  532. res_alloc->guaranteed[t] =
  533. MLX4_VF_COUNTERS_PER_PORT *
  534. MLX4_MAX_PORTS;
  535. else
  536. res_alloc->guaranteed[t] = 0;
  537. res_alloc->res_free -= res_alloc->guaranteed[t];
  538. break;
  539. default:
  540. break;
  541. }
  542. if (i == RES_MAC || i == RES_VLAN) {
  543. for (j = 0; j < dev->caps.num_ports; j++)
  544. if (test_bit(j, actv_ports.ports))
  545. res_alloc->res_port_rsvd[j] +=
  546. res_alloc->guaranteed[t];
  547. } else {
  548. res_alloc->res_reserved += res_alloc->guaranteed[t];
  549. }
  550. }
  551. }
  552. spin_lock_init(&priv->mfunc.master.res_tracker.lock);
  553. return 0;
  554. no_mem_err:
  555. for (i = 0; i < MLX4_NUM_OF_RESOURCE_TYPE; i++) {
  556. kfree(priv->mfunc.master.res_tracker.res_alloc[i].allocated);
  557. priv->mfunc.master.res_tracker.res_alloc[i].allocated = NULL;
  558. kfree(priv->mfunc.master.res_tracker.res_alloc[i].guaranteed);
  559. priv->mfunc.master.res_tracker.res_alloc[i].guaranteed = NULL;
  560. kfree(priv->mfunc.master.res_tracker.res_alloc[i].quota);
  561. priv->mfunc.master.res_tracker.res_alloc[i].quota = NULL;
  562. }
  563. return -ENOMEM;
  564. }
  565. void mlx4_free_resource_tracker(struct mlx4_dev *dev,
  566. enum mlx4_res_tracker_free_type type)
  567. {
  568. struct mlx4_priv *priv = mlx4_priv(dev);
  569. int i;
  570. if (priv->mfunc.master.res_tracker.slave_list) {
  571. if (type != RES_TR_FREE_STRUCTS_ONLY) {
  572. for (i = 0; i < dev->num_slaves; i++) {
  573. if (type == RES_TR_FREE_ALL ||
  574. dev->caps.function != i)
  575. mlx4_delete_all_resources_for_slave(dev, i);
  576. }
  577. /* free master's vlans */
  578. i = dev->caps.function;
  579. mlx4_reset_roce_gids(dev, i);
  580. mutex_lock(&priv->mfunc.master.res_tracker.slave_list[i].mutex);
  581. rem_slave_vlans(dev, i);
  582. mutex_unlock(&priv->mfunc.master.res_tracker.slave_list[i].mutex);
  583. }
  584. if (type != RES_TR_FREE_SLAVES_ONLY) {
  585. for (i = 0; i < MLX4_NUM_OF_RESOURCE_TYPE; i++) {
  586. kfree(priv->mfunc.master.res_tracker.res_alloc[i].allocated);
  587. priv->mfunc.master.res_tracker.res_alloc[i].allocated = NULL;
  588. kfree(priv->mfunc.master.res_tracker.res_alloc[i].guaranteed);
  589. priv->mfunc.master.res_tracker.res_alloc[i].guaranteed = NULL;
  590. kfree(priv->mfunc.master.res_tracker.res_alloc[i].quota);
  591. priv->mfunc.master.res_tracker.res_alloc[i].quota = NULL;
  592. }
  593. kfree(priv->mfunc.master.res_tracker.slave_list);
  594. priv->mfunc.master.res_tracker.slave_list = NULL;
  595. }
  596. }
  597. }
  598. static void update_pkey_index(struct mlx4_dev *dev, int slave,
  599. struct mlx4_cmd_mailbox *inbox)
  600. {
  601. u8 sched = *(u8 *)(inbox->buf + 64);
  602. u8 orig_index = *(u8 *)(inbox->buf + 35);
  603. u8 new_index;
  604. struct mlx4_priv *priv = mlx4_priv(dev);
  605. int port;
  606. port = (sched >> 6 & 1) + 1;
  607. new_index = priv->virt2phys_pkey[slave][port - 1][orig_index];
  608. *(u8 *)(inbox->buf + 35) = new_index;
  609. }
  610. static void update_gid(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *inbox,
  611. u8 slave)
  612. {
  613. struct mlx4_qp_context *qp_ctx = inbox->buf + 8;
  614. enum mlx4_qp_optpar optpar = be32_to_cpu(*(__be32 *) inbox->buf);
  615. u32 ts = (be32_to_cpu(qp_ctx->flags) >> 16) & 0xff;
  616. int port;
  617. if (MLX4_QP_ST_UD == ts) {
  618. port = (qp_ctx->pri_path.sched_queue >> 6 & 1) + 1;
  619. if (mlx4_is_eth(dev, port))
  620. qp_ctx->pri_path.mgid_index =
  621. mlx4_get_base_gid_ix(dev, slave, port) | 0x80;
  622. else
  623. qp_ctx->pri_path.mgid_index = slave | 0x80;
  624. } else if (MLX4_QP_ST_RC == ts || MLX4_QP_ST_XRC == ts || MLX4_QP_ST_UC == ts) {
  625. if (optpar & MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH) {
  626. port = (qp_ctx->pri_path.sched_queue >> 6 & 1) + 1;
  627. if (mlx4_is_eth(dev, port)) {
  628. qp_ctx->pri_path.mgid_index +=
  629. mlx4_get_base_gid_ix(dev, slave, port);
  630. qp_ctx->pri_path.mgid_index &= 0x7f;
  631. } else {
  632. qp_ctx->pri_path.mgid_index = slave & 0x7F;
  633. }
  634. }
  635. if (optpar & MLX4_QP_OPTPAR_ALT_ADDR_PATH) {
  636. port = (qp_ctx->alt_path.sched_queue >> 6 & 1) + 1;
  637. if (mlx4_is_eth(dev, port)) {
  638. qp_ctx->alt_path.mgid_index +=
  639. mlx4_get_base_gid_ix(dev, slave, port);
  640. qp_ctx->alt_path.mgid_index &= 0x7f;
  641. } else {
  642. qp_ctx->alt_path.mgid_index = slave & 0x7F;
  643. }
  644. }
  645. }
  646. }
  647. static int handle_counter(struct mlx4_dev *dev, struct mlx4_qp_context *qpc,
  648. u8 slave, int port);
  649. static int update_vport_qp_param(struct mlx4_dev *dev,
  650. struct mlx4_cmd_mailbox *inbox,
  651. u8 slave, u32 qpn)
  652. {
  653. struct mlx4_qp_context *qpc = inbox->buf + 8;
  654. struct mlx4_vport_oper_state *vp_oper;
  655. struct mlx4_priv *priv;
  656. u32 qp_type;
  657. int port, err = 0;
  658. port = (qpc->pri_path.sched_queue & 0x40) ? 2 : 1;
  659. priv = mlx4_priv(dev);
  660. vp_oper = &priv->mfunc.master.vf_oper[slave].vport[port];
  661. qp_type = (be32_to_cpu(qpc->flags) >> 16) & 0xff;
  662. err = handle_counter(dev, qpc, slave, port);
  663. if (err)
  664. goto out;
  665. if (MLX4_VGT != vp_oper->state.default_vlan) {
  666. /* the reserved QPs (special, proxy, tunnel)
  667. * do not operate over vlans
  668. */
  669. if (mlx4_is_qp_reserved(dev, qpn))
  670. return 0;
  671. /* force strip vlan by clear vsd, MLX QP refers to Raw Ethernet */
  672. if (qp_type == MLX4_QP_ST_UD ||
  673. (qp_type == MLX4_QP_ST_MLX && mlx4_is_eth(dev, port))) {
  674. if (dev->caps.bmme_flags & MLX4_BMME_FLAG_VSD_INIT2RTR) {
  675. *(__be32 *)inbox->buf =
  676. cpu_to_be32(be32_to_cpu(*(__be32 *)inbox->buf) |
  677. MLX4_QP_OPTPAR_VLAN_STRIPPING);
  678. qpc->param3 &= ~cpu_to_be32(MLX4_STRIP_VLAN);
  679. } else {
  680. struct mlx4_update_qp_params params = {.flags = 0};
  681. err = mlx4_update_qp(dev, qpn, MLX4_UPDATE_QP_VSD, &params);
  682. if (err)
  683. goto out;
  684. }
  685. }
  686. /* preserve IF_COUNTER flag */
  687. qpc->pri_path.vlan_control &=
  688. MLX4_CTRL_ETH_SRC_CHECK_IF_COUNTER;
  689. if (vp_oper->state.link_state == IFLA_VF_LINK_STATE_DISABLE &&
  690. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_UPDATE_QP) {
  691. qpc->pri_path.vlan_control |=
  692. MLX4_VLAN_CTRL_ETH_TX_BLOCK_TAGGED |
  693. MLX4_VLAN_CTRL_ETH_TX_BLOCK_PRIO_TAGGED |
  694. MLX4_VLAN_CTRL_ETH_TX_BLOCK_UNTAGGED |
  695. MLX4_VLAN_CTRL_ETH_RX_BLOCK_PRIO_TAGGED |
  696. MLX4_VLAN_CTRL_ETH_RX_BLOCK_UNTAGGED |
  697. MLX4_VLAN_CTRL_ETH_RX_BLOCK_TAGGED;
  698. } else if (0 != vp_oper->state.default_vlan) {
  699. qpc->pri_path.vlan_control |=
  700. MLX4_VLAN_CTRL_ETH_TX_BLOCK_TAGGED |
  701. MLX4_VLAN_CTRL_ETH_RX_BLOCK_PRIO_TAGGED |
  702. MLX4_VLAN_CTRL_ETH_RX_BLOCK_UNTAGGED;
  703. } else { /* priority tagged */
  704. qpc->pri_path.vlan_control |=
  705. MLX4_VLAN_CTRL_ETH_TX_BLOCK_TAGGED |
  706. MLX4_VLAN_CTRL_ETH_RX_BLOCK_TAGGED;
  707. }
  708. qpc->pri_path.fvl_rx |= MLX4_FVL_RX_FORCE_ETH_VLAN;
  709. qpc->pri_path.vlan_index = vp_oper->vlan_idx;
  710. qpc->pri_path.fl |= MLX4_FL_CV | MLX4_FL_ETH_HIDE_CQE_VLAN;
  711. qpc->pri_path.feup |= MLX4_FEUP_FORCE_ETH_UP | MLX4_FVL_FORCE_ETH_VLAN;
  712. qpc->pri_path.sched_queue &= 0xC7;
  713. qpc->pri_path.sched_queue |= (vp_oper->state.default_qos) << 3;
  714. qpc->qos_vport = vp_oper->state.qos_vport;
  715. }
  716. if (vp_oper->state.spoofchk) {
  717. qpc->pri_path.feup |= MLX4_FSM_FORCE_ETH_SRC_MAC;
  718. qpc->pri_path.grh_mylmc = (0x80 & qpc->pri_path.grh_mylmc) + vp_oper->mac_idx;
  719. }
  720. out:
  721. return err;
  722. }
  723. static int mpt_mask(struct mlx4_dev *dev)
  724. {
  725. return dev->caps.num_mpts - 1;
  726. }
  727. static void *find_res(struct mlx4_dev *dev, u64 res_id,
  728. enum mlx4_resource type)
  729. {
  730. struct mlx4_priv *priv = mlx4_priv(dev);
  731. return res_tracker_lookup(&priv->mfunc.master.res_tracker.res_tree[type],
  732. res_id);
  733. }
  734. static int get_res(struct mlx4_dev *dev, int slave, u64 res_id,
  735. enum mlx4_resource type,
  736. void *res)
  737. {
  738. struct res_common *r;
  739. int err = 0;
  740. spin_lock_irq(mlx4_tlock(dev));
  741. r = find_res(dev, res_id, type);
  742. if (!r) {
  743. err = -ENONET;
  744. goto exit;
  745. }
  746. if (r->state == RES_ANY_BUSY) {
  747. err = -EBUSY;
  748. goto exit;
  749. }
  750. if (r->owner != slave) {
  751. err = -EPERM;
  752. goto exit;
  753. }
  754. r->from_state = r->state;
  755. r->state = RES_ANY_BUSY;
  756. if (res)
  757. *((struct res_common **)res) = r;
  758. exit:
  759. spin_unlock_irq(mlx4_tlock(dev));
  760. return err;
  761. }
  762. int mlx4_get_slave_from_resource_id(struct mlx4_dev *dev,
  763. enum mlx4_resource type,
  764. u64 res_id, int *slave)
  765. {
  766. struct res_common *r;
  767. int err = -ENOENT;
  768. int id = res_id;
  769. if (type == RES_QP)
  770. id &= 0x7fffff;
  771. spin_lock(mlx4_tlock(dev));
  772. r = find_res(dev, id, type);
  773. if (r) {
  774. *slave = r->owner;
  775. err = 0;
  776. }
  777. spin_unlock(mlx4_tlock(dev));
  778. return err;
  779. }
  780. static void put_res(struct mlx4_dev *dev, int slave, u64 res_id,
  781. enum mlx4_resource type)
  782. {
  783. struct res_common *r;
  784. spin_lock_irq(mlx4_tlock(dev));
  785. r = find_res(dev, res_id, type);
  786. if (r)
  787. r->state = r->from_state;
  788. spin_unlock_irq(mlx4_tlock(dev));
  789. }
  790. static int counter_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  791. u64 in_param, u64 *out_param, int port);
  792. static int handle_existing_counter(struct mlx4_dev *dev, u8 slave, int port,
  793. int counter_index)
  794. {
  795. struct res_common *r;
  796. struct res_counter *counter;
  797. int ret = 0;
  798. if (counter_index == MLX4_SINK_COUNTER_INDEX(dev))
  799. return ret;
  800. spin_lock_irq(mlx4_tlock(dev));
  801. r = find_res(dev, counter_index, RES_COUNTER);
  802. if (!r || r->owner != slave)
  803. ret = -EINVAL;
  804. counter = container_of(r, struct res_counter, com);
  805. if (!counter->port)
  806. counter->port = port;
  807. spin_unlock_irq(mlx4_tlock(dev));
  808. return ret;
  809. }
  810. static int handle_unexisting_counter(struct mlx4_dev *dev,
  811. struct mlx4_qp_context *qpc, u8 slave,
  812. int port)
  813. {
  814. struct mlx4_priv *priv = mlx4_priv(dev);
  815. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  816. struct res_common *tmp;
  817. struct res_counter *counter;
  818. u64 counter_idx = MLX4_SINK_COUNTER_INDEX(dev);
  819. int err = 0;
  820. spin_lock_irq(mlx4_tlock(dev));
  821. list_for_each_entry(tmp,
  822. &tracker->slave_list[slave].res_list[RES_COUNTER],
  823. list) {
  824. counter = container_of(tmp, struct res_counter, com);
  825. if (port == counter->port) {
  826. qpc->pri_path.counter_index = counter->com.res_id;
  827. spin_unlock_irq(mlx4_tlock(dev));
  828. return 0;
  829. }
  830. }
  831. spin_unlock_irq(mlx4_tlock(dev));
  832. /* No existing counter, need to allocate a new counter */
  833. err = counter_alloc_res(dev, slave, RES_OP_RESERVE, 0, 0, &counter_idx,
  834. port);
  835. if (err == -ENOENT) {
  836. err = 0;
  837. } else if (err && err != -ENOSPC) {
  838. mlx4_err(dev, "%s: failed to create new counter for slave %d err %d\n",
  839. __func__, slave, err);
  840. } else {
  841. qpc->pri_path.counter_index = counter_idx;
  842. mlx4_dbg(dev, "%s: alloc new counter for slave %d index %d\n",
  843. __func__, slave, qpc->pri_path.counter_index);
  844. err = 0;
  845. }
  846. return err;
  847. }
  848. static int handle_counter(struct mlx4_dev *dev, struct mlx4_qp_context *qpc,
  849. u8 slave, int port)
  850. {
  851. if (qpc->pri_path.counter_index != MLX4_SINK_COUNTER_INDEX(dev))
  852. return handle_existing_counter(dev, slave, port,
  853. qpc->pri_path.counter_index);
  854. return handle_unexisting_counter(dev, qpc, slave, port);
  855. }
  856. static struct res_common *alloc_qp_tr(int id)
  857. {
  858. struct res_qp *ret;
  859. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  860. if (!ret)
  861. return NULL;
  862. ret->com.res_id = id;
  863. ret->com.state = RES_QP_RESERVED;
  864. ret->local_qpn = id;
  865. INIT_LIST_HEAD(&ret->mcg_list);
  866. spin_lock_init(&ret->mcg_spl);
  867. atomic_set(&ret->ref_count, 0);
  868. return &ret->com;
  869. }
  870. static struct res_common *alloc_mtt_tr(int id, int order)
  871. {
  872. struct res_mtt *ret;
  873. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  874. if (!ret)
  875. return NULL;
  876. ret->com.res_id = id;
  877. ret->order = order;
  878. ret->com.state = RES_MTT_ALLOCATED;
  879. atomic_set(&ret->ref_count, 0);
  880. return &ret->com;
  881. }
  882. static struct res_common *alloc_mpt_tr(int id, int key)
  883. {
  884. struct res_mpt *ret;
  885. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  886. if (!ret)
  887. return NULL;
  888. ret->com.res_id = id;
  889. ret->com.state = RES_MPT_RESERVED;
  890. ret->key = key;
  891. return &ret->com;
  892. }
  893. static struct res_common *alloc_eq_tr(int id)
  894. {
  895. struct res_eq *ret;
  896. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  897. if (!ret)
  898. return NULL;
  899. ret->com.res_id = id;
  900. ret->com.state = RES_EQ_RESERVED;
  901. return &ret->com;
  902. }
  903. static struct res_common *alloc_cq_tr(int id)
  904. {
  905. struct res_cq *ret;
  906. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  907. if (!ret)
  908. return NULL;
  909. ret->com.res_id = id;
  910. ret->com.state = RES_CQ_ALLOCATED;
  911. atomic_set(&ret->ref_count, 0);
  912. return &ret->com;
  913. }
  914. static struct res_common *alloc_srq_tr(int id)
  915. {
  916. struct res_srq *ret;
  917. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  918. if (!ret)
  919. return NULL;
  920. ret->com.res_id = id;
  921. ret->com.state = RES_SRQ_ALLOCATED;
  922. atomic_set(&ret->ref_count, 0);
  923. return &ret->com;
  924. }
  925. static struct res_common *alloc_counter_tr(int id, int port)
  926. {
  927. struct res_counter *ret;
  928. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  929. if (!ret)
  930. return NULL;
  931. ret->com.res_id = id;
  932. ret->com.state = RES_COUNTER_ALLOCATED;
  933. ret->port = port;
  934. return &ret->com;
  935. }
  936. static struct res_common *alloc_xrcdn_tr(int id)
  937. {
  938. struct res_xrcdn *ret;
  939. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  940. if (!ret)
  941. return NULL;
  942. ret->com.res_id = id;
  943. ret->com.state = RES_XRCD_ALLOCATED;
  944. return &ret->com;
  945. }
  946. static struct res_common *alloc_fs_rule_tr(u64 id, int qpn)
  947. {
  948. struct res_fs_rule *ret;
  949. ret = kzalloc(sizeof *ret, GFP_KERNEL);
  950. if (!ret)
  951. return NULL;
  952. ret->com.res_id = id;
  953. ret->com.state = RES_FS_RULE_ALLOCATED;
  954. ret->qpn = qpn;
  955. return &ret->com;
  956. }
  957. static struct res_common *alloc_tr(u64 id, enum mlx4_resource type, int slave,
  958. int extra)
  959. {
  960. struct res_common *ret;
  961. switch (type) {
  962. case RES_QP:
  963. ret = alloc_qp_tr(id);
  964. break;
  965. case RES_MPT:
  966. ret = alloc_mpt_tr(id, extra);
  967. break;
  968. case RES_MTT:
  969. ret = alloc_mtt_tr(id, extra);
  970. break;
  971. case RES_EQ:
  972. ret = alloc_eq_tr(id);
  973. break;
  974. case RES_CQ:
  975. ret = alloc_cq_tr(id);
  976. break;
  977. case RES_SRQ:
  978. ret = alloc_srq_tr(id);
  979. break;
  980. case RES_MAC:
  981. pr_err("implementation missing\n");
  982. return NULL;
  983. case RES_COUNTER:
  984. ret = alloc_counter_tr(id, extra);
  985. break;
  986. case RES_XRCD:
  987. ret = alloc_xrcdn_tr(id);
  988. break;
  989. case RES_FS_RULE:
  990. ret = alloc_fs_rule_tr(id, extra);
  991. break;
  992. default:
  993. return NULL;
  994. }
  995. if (ret)
  996. ret->owner = slave;
  997. return ret;
  998. }
  999. int mlx4_calc_vf_counters(struct mlx4_dev *dev, int slave, int port,
  1000. struct mlx4_counter *data)
  1001. {
  1002. struct mlx4_priv *priv = mlx4_priv(dev);
  1003. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1004. struct res_common *tmp;
  1005. struct res_counter *counter;
  1006. int *counters_arr;
  1007. int i = 0, err = 0;
  1008. memset(data, 0, sizeof(*data));
  1009. counters_arr = kmalloc_array(dev->caps.max_counters,
  1010. sizeof(*counters_arr), GFP_KERNEL);
  1011. if (!counters_arr)
  1012. return -ENOMEM;
  1013. spin_lock_irq(mlx4_tlock(dev));
  1014. list_for_each_entry(tmp,
  1015. &tracker->slave_list[slave].res_list[RES_COUNTER],
  1016. list) {
  1017. counter = container_of(tmp, struct res_counter, com);
  1018. if (counter->port == port) {
  1019. counters_arr[i] = (int)tmp->res_id;
  1020. i++;
  1021. }
  1022. }
  1023. spin_unlock_irq(mlx4_tlock(dev));
  1024. counters_arr[i] = -1;
  1025. i = 0;
  1026. while (counters_arr[i] != -1) {
  1027. err = mlx4_get_counter_stats(dev, counters_arr[i], data,
  1028. 0);
  1029. if (err) {
  1030. memset(data, 0, sizeof(*data));
  1031. goto table_changed;
  1032. }
  1033. i++;
  1034. }
  1035. table_changed:
  1036. kfree(counters_arr);
  1037. return 0;
  1038. }
  1039. static int add_res_range(struct mlx4_dev *dev, int slave, u64 base, int count,
  1040. enum mlx4_resource type, int extra)
  1041. {
  1042. int i;
  1043. int err;
  1044. struct mlx4_priv *priv = mlx4_priv(dev);
  1045. struct res_common **res_arr;
  1046. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1047. struct rb_root *root = &tracker->res_tree[type];
  1048. res_arr = kzalloc(count * sizeof *res_arr, GFP_KERNEL);
  1049. if (!res_arr)
  1050. return -ENOMEM;
  1051. for (i = 0; i < count; ++i) {
  1052. res_arr[i] = alloc_tr(base + i, type, slave, extra);
  1053. if (!res_arr[i]) {
  1054. for (--i; i >= 0; --i)
  1055. kfree(res_arr[i]);
  1056. kfree(res_arr);
  1057. return -ENOMEM;
  1058. }
  1059. }
  1060. spin_lock_irq(mlx4_tlock(dev));
  1061. for (i = 0; i < count; ++i) {
  1062. if (find_res(dev, base + i, type)) {
  1063. err = -EEXIST;
  1064. goto undo;
  1065. }
  1066. err = res_tracker_insert(root, res_arr[i]);
  1067. if (err)
  1068. goto undo;
  1069. list_add_tail(&res_arr[i]->list,
  1070. &tracker->slave_list[slave].res_list[type]);
  1071. }
  1072. spin_unlock_irq(mlx4_tlock(dev));
  1073. kfree(res_arr);
  1074. return 0;
  1075. undo:
  1076. for (--i; i >= 0; --i) {
  1077. rb_erase(&res_arr[i]->node, root);
  1078. list_del_init(&res_arr[i]->list);
  1079. }
  1080. spin_unlock_irq(mlx4_tlock(dev));
  1081. for (i = 0; i < count; ++i)
  1082. kfree(res_arr[i]);
  1083. kfree(res_arr);
  1084. return err;
  1085. }
  1086. static int remove_qp_ok(struct res_qp *res)
  1087. {
  1088. if (res->com.state == RES_QP_BUSY || atomic_read(&res->ref_count) ||
  1089. !list_empty(&res->mcg_list)) {
  1090. pr_err("resource tracker: fail to remove qp, state %d, ref_count %d\n",
  1091. res->com.state, atomic_read(&res->ref_count));
  1092. return -EBUSY;
  1093. } else if (res->com.state != RES_QP_RESERVED) {
  1094. return -EPERM;
  1095. }
  1096. return 0;
  1097. }
  1098. static int remove_mtt_ok(struct res_mtt *res, int order)
  1099. {
  1100. if (res->com.state == RES_MTT_BUSY ||
  1101. atomic_read(&res->ref_count)) {
  1102. pr_devel("%s-%d: state %s, ref_count %d\n",
  1103. __func__, __LINE__,
  1104. mtt_states_str(res->com.state),
  1105. atomic_read(&res->ref_count));
  1106. return -EBUSY;
  1107. } else if (res->com.state != RES_MTT_ALLOCATED)
  1108. return -EPERM;
  1109. else if (res->order != order)
  1110. return -EINVAL;
  1111. return 0;
  1112. }
  1113. static int remove_mpt_ok(struct res_mpt *res)
  1114. {
  1115. if (res->com.state == RES_MPT_BUSY)
  1116. return -EBUSY;
  1117. else if (res->com.state != RES_MPT_RESERVED)
  1118. return -EPERM;
  1119. return 0;
  1120. }
  1121. static int remove_eq_ok(struct res_eq *res)
  1122. {
  1123. if (res->com.state == RES_MPT_BUSY)
  1124. return -EBUSY;
  1125. else if (res->com.state != RES_MPT_RESERVED)
  1126. return -EPERM;
  1127. return 0;
  1128. }
  1129. static int remove_counter_ok(struct res_counter *res)
  1130. {
  1131. if (res->com.state == RES_COUNTER_BUSY)
  1132. return -EBUSY;
  1133. else if (res->com.state != RES_COUNTER_ALLOCATED)
  1134. return -EPERM;
  1135. return 0;
  1136. }
  1137. static int remove_xrcdn_ok(struct res_xrcdn *res)
  1138. {
  1139. if (res->com.state == RES_XRCD_BUSY)
  1140. return -EBUSY;
  1141. else if (res->com.state != RES_XRCD_ALLOCATED)
  1142. return -EPERM;
  1143. return 0;
  1144. }
  1145. static int remove_fs_rule_ok(struct res_fs_rule *res)
  1146. {
  1147. if (res->com.state == RES_FS_RULE_BUSY)
  1148. return -EBUSY;
  1149. else if (res->com.state != RES_FS_RULE_ALLOCATED)
  1150. return -EPERM;
  1151. return 0;
  1152. }
  1153. static int remove_cq_ok(struct res_cq *res)
  1154. {
  1155. if (res->com.state == RES_CQ_BUSY)
  1156. return -EBUSY;
  1157. else if (res->com.state != RES_CQ_ALLOCATED)
  1158. return -EPERM;
  1159. return 0;
  1160. }
  1161. static int remove_srq_ok(struct res_srq *res)
  1162. {
  1163. if (res->com.state == RES_SRQ_BUSY)
  1164. return -EBUSY;
  1165. else if (res->com.state != RES_SRQ_ALLOCATED)
  1166. return -EPERM;
  1167. return 0;
  1168. }
  1169. static int remove_ok(struct res_common *res, enum mlx4_resource type, int extra)
  1170. {
  1171. switch (type) {
  1172. case RES_QP:
  1173. return remove_qp_ok((struct res_qp *)res);
  1174. case RES_CQ:
  1175. return remove_cq_ok((struct res_cq *)res);
  1176. case RES_SRQ:
  1177. return remove_srq_ok((struct res_srq *)res);
  1178. case RES_MPT:
  1179. return remove_mpt_ok((struct res_mpt *)res);
  1180. case RES_MTT:
  1181. return remove_mtt_ok((struct res_mtt *)res, extra);
  1182. case RES_MAC:
  1183. return -ENOSYS;
  1184. case RES_EQ:
  1185. return remove_eq_ok((struct res_eq *)res);
  1186. case RES_COUNTER:
  1187. return remove_counter_ok((struct res_counter *)res);
  1188. case RES_XRCD:
  1189. return remove_xrcdn_ok((struct res_xrcdn *)res);
  1190. case RES_FS_RULE:
  1191. return remove_fs_rule_ok((struct res_fs_rule *)res);
  1192. default:
  1193. return -EINVAL;
  1194. }
  1195. }
  1196. static int rem_res_range(struct mlx4_dev *dev, int slave, u64 base, int count,
  1197. enum mlx4_resource type, int extra)
  1198. {
  1199. u64 i;
  1200. int err;
  1201. struct mlx4_priv *priv = mlx4_priv(dev);
  1202. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1203. struct res_common *r;
  1204. spin_lock_irq(mlx4_tlock(dev));
  1205. for (i = base; i < base + count; ++i) {
  1206. r = res_tracker_lookup(&tracker->res_tree[type], i);
  1207. if (!r) {
  1208. err = -ENOENT;
  1209. goto out;
  1210. }
  1211. if (r->owner != slave) {
  1212. err = -EPERM;
  1213. goto out;
  1214. }
  1215. err = remove_ok(r, type, extra);
  1216. if (err)
  1217. goto out;
  1218. }
  1219. for (i = base; i < base + count; ++i) {
  1220. r = res_tracker_lookup(&tracker->res_tree[type], i);
  1221. rb_erase(&r->node, &tracker->res_tree[type]);
  1222. list_del(&r->list);
  1223. kfree(r);
  1224. }
  1225. err = 0;
  1226. out:
  1227. spin_unlock_irq(mlx4_tlock(dev));
  1228. return err;
  1229. }
  1230. static int qp_res_start_move_to(struct mlx4_dev *dev, int slave, int qpn,
  1231. enum res_qp_states state, struct res_qp **qp,
  1232. int alloc)
  1233. {
  1234. struct mlx4_priv *priv = mlx4_priv(dev);
  1235. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1236. struct res_qp *r;
  1237. int err = 0;
  1238. spin_lock_irq(mlx4_tlock(dev));
  1239. r = res_tracker_lookup(&tracker->res_tree[RES_QP], qpn);
  1240. if (!r)
  1241. err = -ENOENT;
  1242. else if (r->com.owner != slave)
  1243. err = -EPERM;
  1244. else {
  1245. switch (state) {
  1246. case RES_QP_BUSY:
  1247. mlx4_dbg(dev, "%s: failed RES_QP, 0x%llx\n",
  1248. __func__, r->com.res_id);
  1249. err = -EBUSY;
  1250. break;
  1251. case RES_QP_RESERVED:
  1252. if (r->com.state == RES_QP_MAPPED && !alloc)
  1253. break;
  1254. mlx4_dbg(dev, "failed RES_QP, 0x%llx\n", r->com.res_id);
  1255. err = -EINVAL;
  1256. break;
  1257. case RES_QP_MAPPED:
  1258. if ((r->com.state == RES_QP_RESERVED && alloc) ||
  1259. r->com.state == RES_QP_HW)
  1260. break;
  1261. else {
  1262. mlx4_dbg(dev, "failed RES_QP, 0x%llx\n",
  1263. r->com.res_id);
  1264. err = -EINVAL;
  1265. }
  1266. break;
  1267. case RES_QP_HW:
  1268. if (r->com.state != RES_QP_MAPPED)
  1269. err = -EINVAL;
  1270. break;
  1271. default:
  1272. err = -EINVAL;
  1273. }
  1274. if (!err) {
  1275. r->com.from_state = r->com.state;
  1276. r->com.to_state = state;
  1277. r->com.state = RES_QP_BUSY;
  1278. if (qp)
  1279. *qp = r;
  1280. }
  1281. }
  1282. spin_unlock_irq(mlx4_tlock(dev));
  1283. return err;
  1284. }
  1285. static int mr_res_start_move_to(struct mlx4_dev *dev, int slave, int index,
  1286. enum res_mpt_states state, struct res_mpt **mpt)
  1287. {
  1288. struct mlx4_priv *priv = mlx4_priv(dev);
  1289. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1290. struct res_mpt *r;
  1291. int err = 0;
  1292. spin_lock_irq(mlx4_tlock(dev));
  1293. r = res_tracker_lookup(&tracker->res_tree[RES_MPT], index);
  1294. if (!r)
  1295. err = -ENOENT;
  1296. else if (r->com.owner != slave)
  1297. err = -EPERM;
  1298. else {
  1299. switch (state) {
  1300. case RES_MPT_BUSY:
  1301. err = -EINVAL;
  1302. break;
  1303. case RES_MPT_RESERVED:
  1304. if (r->com.state != RES_MPT_MAPPED)
  1305. err = -EINVAL;
  1306. break;
  1307. case RES_MPT_MAPPED:
  1308. if (r->com.state != RES_MPT_RESERVED &&
  1309. r->com.state != RES_MPT_HW)
  1310. err = -EINVAL;
  1311. break;
  1312. case RES_MPT_HW:
  1313. if (r->com.state != RES_MPT_MAPPED)
  1314. err = -EINVAL;
  1315. break;
  1316. default:
  1317. err = -EINVAL;
  1318. }
  1319. if (!err) {
  1320. r->com.from_state = r->com.state;
  1321. r->com.to_state = state;
  1322. r->com.state = RES_MPT_BUSY;
  1323. if (mpt)
  1324. *mpt = r;
  1325. }
  1326. }
  1327. spin_unlock_irq(mlx4_tlock(dev));
  1328. return err;
  1329. }
  1330. static int eq_res_start_move_to(struct mlx4_dev *dev, int slave, int index,
  1331. enum res_eq_states state, struct res_eq **eq)
  1332. {
  1333. struct mlx4_priv *priv = mlx4_priv(dev);
  1334. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1335. struct res_eq *r;
  1336. int err = 0;
  1337. spin_lock_irq(mlx4_tlock(dev));
  1338. r = res_tracker_lookup(&tracker->res_tree[RES_EQ], index);
  1339. if (!r)
  1340. err = -ENOENT;
  1341. else if (r->com.owner != slave)
  1342. err = -EPERM;
  1343. else {
  1344. switch (state) {
  1345. case RES_EQ_BUSY:
  1346. err = -EINVAL;
  1347. break;
  1348. case RES_EQ_RESERVED:
  1349. if (r->com.state != RES_EQ_HW)
  1350. err = -EINVAL;
  1351. break;
  1352. case RES_EQ_HW:
  1353. if (r->com.state != RES_EQ_RESERVED)
  1354. err = -EINVAL;
  1355. break;
  1356. default:
  1357. err = -EINVAL;
  1358. }
  1359. if (!err) {
  1360. r->com.from_state = r->com.state;
  1361. r->com.to_state = state;
  1362. r->com.state = RES_EQ_BUSY;
  1363. if (eq)
  1364. *eq = r;
  1365. }
  1366. }
  1367. spin_unlock_irq(mlx4_tlock(dev));
  1368. return err;
  1369. }
  1370. static int cq_res_start_move_to(struct mlx4_dev *dev, int slave, int cqn,
  1371. enum res_cq_states state, struct res_cq **cq)
  1372. {
  1373. struct mlx4_priv *priv = mlx4_priv(dev);
  1374. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1375. struct res_cq *r;
  1376. int err;
  1377. spin_lock_irq(mlx4_tlock(dev));
  1378. r = res_tracker_lookup(&tracker->res_tree[RES_CQ], cqn);
  1379. if (!r) {
  1380. err = -ENOENT;
  1381. } else if (r->com.owner != slave) {
  1382. err = -EPERM;
  1383. } else if (state == RES_CQ_ALLOCATED) {
  1384. if (r->com.state != RES_CQ_HW)
  1385. err = -EINVAL;
  1386. else if (atomic_read(&r->ref_count))
  1387. err = -EBUSY;
  1388. else
  1389. err = 0;
  1390. } else if (state != RES_CQ_HW || r->com.state != RES_CQ_ALLOCATED) {
  1391. err = -EINVAL;
  1392. } else {
  1393. err = 0;
  1394. }
  1395. if (!err) {
  1396. r->com.from_state = r->com.state;
  1397. r->com.to_state = state;
  1398. r->com.state = RES_CQ_BUSY;
  1399. if (cq)
  1400. *cq = r;
  1401. }
  1402. spin_unlock_irq(mlx4_tlock(dev));
  1403. return err;
  1404. }
  1405. static int srq_res_start_move_to(struct mlx4_dev *dev, int slave, int index,
  1406. enum res_srq_states state, struct res_srq **srq)
  1407. {
  1408. struct mlx4_priv *priv = mlx4_priv(dev);
  1409. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1410. struct res_srq *r;
  1411. int err = 0;
  1412. spin_lock_irq(mlx4_tlock(dev));
  1413. r = res_tracker_lookup(&tracker->res_tree[RES_SRQ], index);
  1414. if (!r) {
  1415. err = -ENOENT;
  1416. } else if (r->com.owner != slave) {
  1417. err = -EPERM;
  1418. } else if (state == RES_SRQ_ALLOCATED) {
  1419. if (r->com.state != RES_SRQ_HW)
  1420. err = -EINVAL;
  1421. else if (atomic_read(&r->ref_count))
  1422. err = -EBUSY;
  1423. } else if (state != RES_SRQ_HW || r->com.state != RES_SRQ_ALLOCATED) {
  1424. err = -EINVAL;
  1425. }
  1426. if (!err) {
  1427. r->com.from_state = r->com.state;
  1428. r->com.to_state = state;
  1429. r->com.state = RES_SRQ_BUSY;
  1430. if (srq)
  1431. *srq = r;
  1432. }
  1433. spin_unlock_irq(mlx4_tlock(dev));
  1434. return err;
  1435. }
  1436. static void res_abort_move(struct mlx4_dev *dev, int slave,
  1437. enum mlx4_resource type, int id)
  1438. {
  1439. struct mlx4_priv *priv = mlx4_priv(dev);
  1440. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1441. struct res_common *r;
  1442. spin_lock_irq(mlx4_tlock(dev));
  1443. r = res_tracker_lookup(&tracker->res_tree[type], id);
  1444. if (r && (r->owner == slave))
  1445. r->state = r->from_state;
  1446. spin_unlock_irq(mlx4_tlock(dev));
  1447. }
  1448. static void res_end_move(struct mlx4_dev *dev, int slave,
  1449. enum mlx4_resource type, int id)
  1450. {
  1451. struct mlx4_priv *priv = mlx4_priv(dev);
  1452. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1453. struct res_common *r;
  1454. spin_lock_irq(mlx4_tlock(dev));
  1455. r = res_tracker_lookup(&tracker->res_tree[type], id);
  1456. if (r && (r->owner == slave))
  1457. r->state = r->to_state;
  1458. spin_unlock_irq(mlx4_tlock(dev));
  1459. }
  1460. static int valid_reserved(struct mlx4_dev *dev, int slave, int qpn)
  1461. {
  1462. return mlx4_is_qp_reserved(dev, qpn) &&
  1463. (mlx4_is_master(dev) || mlx4_is_guest_proxy(dev, slave, qpn));
  1464. }
  1465. static int fw_reserved(struct mlx4_dev *dev, int qpn)
  1466. {
  1467. return qpn < dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  1468. }
  1469. static int qp_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1470. u64 in_param, u64 *out_param)
  1471. {
  1472. int err;
  1473. int count;
  1474. int align;
  1475. int base;
  1476. int qpn;
  1477. u8 flags;
  1478. switch (op) {
  1479. case RES_OP_RESERVE:
  1480. count = get_param_l(&in_param) & 0xffffff;
  1481. /* Turn off all unsupported QP allocation flags that the
  1482. * slave tries to set.
  1483. */
  1484. flags = (get_param_l(&in_param) >> 24) & dev->caps.alloc_res_qp_mask;
  1485. align = get_param_h(&in_param);
  1486. err = mlx4_grant_resource(dev, slave, RES_QP, count, 0);
  1487. if (err)
  1488. return err;
  1489. err = __mlx4_qp_reserve_range(dev, count, align, &base, flags);
  1490. if (err) {
  1491. mlx4_release_resource(dev, slave, RES_QP, count, 0);
  1492. return err;
  1493. }
  1494. err = add_res_range(dev, slave, base, count, RES_QP, 0);
  1495. if (err) {
  1496. mlx4_release_resource(dev, slave, RES_QP, count, 0);
  1497. __mlx4_qp_release_range(dev, base, count);
  1498. return err;
  1499. }
  1500. set_param_l(out_param, base);
  1501. break;
  1502. case RES_OP_MAP_ICM:
  1503. qpn = get_param_l(&in_param) & 0x7fffff;
  1504. if (valid_reserved(dev, slave, qpn)) {
  1505. err = add_res_range(dev, slave, qpn, 1, RES_QP, 0);
  1506. if (err)
  1507. return err;
  1508. }
  1509. err = qp_res_start_move_to(dev, slave, qpn, RES_QP_MAPPED,
  1510. NULL, 1);
  1511. if (err)
  1512. return err;
  1513. if (!fw_reserved(dev, qpn)) {
  1514. err = __mlx4_qp_alloc_icm(dev, qpn, GFP_KERNEL);
  1515. if (err) {
  1516. res_abort_move(dev, slave, RES_QP, qpn);
  1517. return err;
  1518. }
  1519. }
  1520. res_end_move(dev, slave, RES_QP, qpn);
  1521. break;
  1522. default:
  1523. err = -EINVAL;
  1524. break;
  1525. }
  1526. return err;
  1527. }
  1528. static int mtt_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1529. u64 in_param, u64 *out_param)
  1530. {
  1531. int err = -EINVAL;
  1532. int base;
  1533. int order;
  1534. if (op != RES_OP_RESERVE_AND_MAP)
  1535. return err;
  1536. order = get_param_l(&in_param);
  1537. err = mlx4_grant_resource(dev, slave, RES_MTT, 1 << order, 0);
  1538. if (err)
  1539. return err;
  1540. base = __mlx4_alloc_mtt_range(dev, order);
  1541. if (base == -1) {
  1542. mlx4_release_resource(dev, slave, RES_MTT, 1 << order, 0);
  1543. return -ENOMEM;
  1544. }
  1545. err = add_res_range(dev, slave, base, 1, RES_MTT, order);
  1546. if (err) {
  1547. mlx4_release_resource(dev, slave, RES_MTT, 1 << order, 0);
  1548. __mlx4_free_mtt_range(dev, base, order);
  1549. } else {
  1550. set_param_l(out_param, base);
  1551. }
  1552. return err;
  1553. }
  1554. static int mpt_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1555. u64 in_param, u64 *out_param)
  1556. {
  1557. int err = -EINVAL;
  1558. int index;
  1559. int id;
  1560. struct res_mpt *mpt;
  1561. switch (op) {
  1562. case RES_OP_RESERVE:
  1563. err = mlx4_grant_resource(dev, slave, RES_MPT, 1, 0);
  1564. if (err)
  1565. break;
  1566. index = __mlx4_mpt_reserve(dev);
  1567. if (index == -1) {
  1568. mlx4_release_resource(dev, slave, RES_MPT, 1, 0);
  1569. break;
  1570. }
  1571. id = index & mpt_mask(dev);
  1572. err = add_res_range(dev, slave, id, 1, RES_MPT, index);
  1573. if (err) {
  1574. mlx4_release_resource(dev, slave, RES_MPT, 1, 0);
  1575. __mlx4_mpt_release(dev, index);
  1576. break;
  1577. }
  1578. set_param_l(out_param, index);
  1579. break;
  1580. case RES_OP_MAP_ICM:
  1581. index = get_param_l(&in_param);
  1582. id = index & mpt_mask(dev);
  1583. err = mr_res_start_move_to(dev, slave, id,
  1584. RES_MPT_MAPPED, &mpt);
  1585. if (err)
  1586. return err;
  1587. err = __mlx4_mpt_alloc_icm(dev, mpt->key, GFP_KERNEL);
  1588. if (err) {
  1589. res_abort_move(dev, slave, RES_MPT, id);
  1590. return err;
  1591. }
  1592. res_end_move(dev, slave, RES_MPT, id);
  1593. break;
  1594. }
  1595. return err;
  1596. }
  1597. static int cq_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1598. u64 in_param, u64 *out_param)
  1599. {
  1600. int cqn;
  1601. int err;
  1602. switch (op) {
  1603. case RES_OP_RESERVE_AND_MAP:
  1604. err = mlx4_grant_resource(dev, slave, RES_CQ, 1, 0);
  1605. if (err)
  1606. break;
  1607. err = __mlx4_cq_alloc_icm(dev, &cqn);
  1608. if (err) {
  1609. mlx4_release_resource(dev, slave, RES_CQ, 1, 0);
  1610. break;
  1611. }
  1612. err = add_res_range(dev, slave, cqn, 1, RES_CQ, 0);
  1613. if (err) {
  1614. mlx4_release_resource(dev, slave, RES_CQ, 1, 0);
  1615. __mlx4_cq_free_icm(dev, cqn);
  1616. break;
  1617. }
  1618. set_param_l(out_param, cqn);
  1619. break;
  1620. default:
  1621. err = -EINVAL;
  1622. }
  1623. return err;
  1624. }
  1625. static int srq_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1626. u64 in_param, u64 *out_param)
  1627. {
  1628. int srqn;
  1629. int err;
  1630. switch (op) {
  1631. case RES_OP_RESERVE_AND_MAP:
  1632. err = mlx4_grant_resource(dev, slave, RES_SRQ, 1, 0);
  1633. if (err)
  1634. break;
  1635. err = __mlx4_srq_alloc_icm(dev, &srqn);
  1636. if (err) {
  1637. mlx4_release_resource(dev, slave, RES_SRQ, 1, 0);
  1638. break;
  1639. }
  1640. err = add_res_range(dev, slave, srqn, 1, RES_SRQ, 0);
  1641. if (err) {
  1642. mlx4_release_resource(dev, slave, RES_SRQ, 1, 0);
  1643. __mlx4_srq_free_icm(dev, srqn);
  1644. break;
  1645. }
  1646. set_param_l(out_param, srqn);
  1647. break;
  1648. default:
  1649. err = -EINVAL;
  1650. }
  1651. return err;
  1652. }
  1653. static int mac_find_smac_ix_in_slave(struct mlx4_dev *dev, int slave, int port,
  1654. u8 smac_index, u64 *mac)
  1655. {
  1656. struct mlx4_priv *priv = mlx4_priv(dev);
  1657. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1658. struct list_head *mac_list =
  1659. &tracker->slave_list[slave].res_list[RES_MAC];
  1660. struct mac_res *res, *tmp;
  1661. list_for_each_entry_safe(res, tmp, mac_list, list) {
  1662. if (res->smac_index == smac_index && res->port == (u8) port) {
  1663. *mac = res->mac;
  1664. return 0;
  1665. }
  1666. }
  1667. return -ENOENT;
  1668. }
  1669. static int mac_add_to_slave(struct mlx4_dev *dev, int slave, u64 mac, int port, u8 smac_index)
  1670. {
  1671. struct mlx4_priv *priv = mlx4_priv(dev);
  1672. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1673. struct list_head *mac_list =
  1674. &tracker->slave_list[slave].res_list[RES_MAC];
  1675. struct mac_res *res, *tmp;
  1676. list_for_each_entry_safe(res, tmp, mac_list, list) {
  1677. if (res->mac == mac && res->port == (u8) port) {
  1678. /* mac found. update ref count */
  1679. ++res->ref_count;
  1680. return 0;
  1681. }
  1682. }
  1683. if (mlx4_grant_resource(dev, slave, RES_MAC, 1, port))
  1684. return -EINVAL;
  1685. res = kzalloc(sizeof *res, GFP_KERNEL);
  1686. if (!res) {
  1687. mlx4_release_resource(dev, slave, RES_MAC, 1, port);
  1688. return -ENOMEM;
  1689. }
  1690. res->mac = mac;
  1691. res->port = (u8) port;
  1692. res->smac_index = smac_index;
  1693. res->ref_count = 1;
  1694. list_add_tail(&res->list,
  1695. &tracker->slave_list[slave].res_list[RES_MAC]);
  1696. return 0;
  1697. }
  1698. static void mac_del_from_slave(struct mlx4_dev *dev, int slave, u64 mac,
  1699. int port)
  1700. {
  1701. struct mlx4_priv *priv = mlx4_priv(dev);
  1702. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1703. struct list_head *mac_list =
  1704. &tracker->slave_list[slave].res_list[RES_MAC];
  1705. struct mac_res *res, *tmp;
  1706. list_for_each_entry_safe(res, tmp, mac_list, list) {
  1707. if (res->mac == mac && res->port == (u8) port) {
  1708. if (!--res->ref_count) {
  1709. list_del(&res->list);
  1710. mlx4_release_resource(dev, slave, RES_MAC, 1, port);
  1711. kfree(res);
  1712. }
  1713. break;
  1714. }
  1715. }
  1716. }
  1717. static void rem_slave_macs(struct mlx4_dev *dev, int slave)
  1718. {
  1719. struct mlx4_priv *priv = mlx4_priv(dev);
  1720. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1721. struct list_head *mac_list =
  1722. &tracker->slave_list[slave].res_list[RES_MAC];
  1723. struct mac_res *res, *tmp;
  1724. int i;
  1725. list_for_each_entry_safe(res, tmp, mac_list, list) {
  1726. list_del(&res->list);
  1727. /* dereference the mac the num times the slave referenced it */
  1728. for (i = 0; i < res->ref_count; i++)
  1729. __mlx4_unregister_mac(dev, res->port, res->mac);
  1730. mlx4_release_resource(dev, slave, RES_MAC, 1, res->port);
  1731. kfree(res);
  1732. }
  1733. }
  1734. static int mac_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1735. u64 in_param, u64 *out_param, int in_port)
  1736. {
  1737. int err = -EINVAL;
  1738. int port;
  1739. u64 mac;
  1740. u8 smac_index;
  1741. if (op != RES_OP_RESERVE_AND_MAP)
  1742. return err;
  1743. port = !in_port ? get_param_l(out_param) : in_port;
  1744. port = mlx4_slave_convert_port(
  1745. dev, slave, port);
  1746. if (port < 0)
  1747. return -EINVAL;
  1748. mac = in_param;
  1749. err = __mlx4_register_mac(dev, port, mac);
  1750. if (err >= 0) {
  1751. smac_index = err;
  1752. set_param_l(out_param, err);
  1753. err = 0;
  1754. }
  1755. if (!err) {
  1756. err = mac_add_to_slave(dev, slave, mac, port, smac_index);
  1757. if (err)
  1758. __mlx4_unregister_mac(dev, port, mac);
  1759. }
  1760. return err;
  1761. }
  1762. static int vlan_add_to_slave(struct mlx4_dev *dev, int slave, u16 vlan,
  1763. int port, int vlan_index)
  1764. {
  1765. struct mlx4_priv *priv = mlx4_priv(dev);
  1766. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1767. struct list_head *vlan_list =
  1768. &tracker->slave_list[slave].res_list[RES_VLAN];
  1769. struct vlan_res *res, *tmp;
  1770. list_for_each_entry_safe(res, tmp, vlan_list, list) {
  1771. if (res->vlan == vlan && res->port == (u8) port) {
  1772. /* vlan found. update ref count */
  1773. ++res->ref_count;
  1774. return 0;
  1775. }
  1776. }
  1777. if (mlx4_grant_resource(dev, slave, RES_VLAN, 1, port))
  1778. return -EINVAL;
  1779. res = kzalloc(sizeof(*res), GFP_KERNEL);
  1780. if (!res) {
  1781. mlx4_release_resource(dev, slave, RES_VLAN, 1, port);
  1782. return -ENOMEM;
  1783. }
  1784. res->vlan = vlan;
  1785. res->port = (u8) port;
  1786. res->vlan_index = vlan_index;
  1787. res->ref_count = 1;
  1788. list_add_tail(&res->list,
  1789. &tracker->slave_list[slave].res_list[RES_VLAN]);
  1790. return 0;
  1791. }
  1792. static void vlan_del_from_slave(struct mlx4_dev *dev, int slave, u16 vlan,
  1793. int port)
  1794. {
  1795. struct mlx4_priv *priv = mlx4_priv(dev);
  1796. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1797. struct list_head *vlan_list =
  1798. &tracker->slave_list[slave].res_list[RES_VLAN];
  1799. struct vlan_res *res, *tmp;
  1800. list_for_each_entry_safe(res, tmp, vlan_list, list) {
  1801. if (res->vlan == vlan && res->port == (u8) port) {
  1802. if (!--res->ref_count) {
  1803. list_del(&res->list);
  1804. mlx4_release_resource(dev, slave, RES_VLAN,
  1805. 1, port);
  1806. kfree(res);
  1807. }
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. static void rem_slave_vlans(struct mlx4_dev *dev, int slave)
  1813. {
  1814. struct mlx4_priv *priv = mlx4_priv(dev);
  1815. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  1816. struct list_head *vlan_list =
  1817. &tracker->slave_list[slave].res_list[RES_VLAN];
  1818. struct vlan_res *res, *tmp;
  1819. int i;
  1820. list_for_each_entry_safe(res, tmp, vlan_list, list) {
  1821. list_del(&res->list);
  1822. /* dereference the vlan the num times the slave referenced it */
  1823. for (i = 0; i < res->ref_count; i++)
  1824. __mlx4_unregister_vlan(dev, res->port, res->vlan);
  1825. mlx4_release_resource(dev, slave, RES_VLAN, 1, res->port);
  1826. kfree(res);
  1827. }
  1828. }
  1829. static int vlan_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1830. u64 in_param, u64 *out_param, int in_port)
  1831. {
  1832. struct mlx4_priv *priv = mlx4_priv(dev);
  1833. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  1834. int err;
  1835. u16 vlan;
  1836. int vlan_index;
  1837. int port;
  1838. port = !in_port ? get_param_l(out_param) : in_port;
  1839. if (!port || op != RES_OP_RESERVE_AND_MAP)
  1840. return -EINVAL;
  1841. port = mlx4_slave_convert_port(
  1842. dev, slave, port);
  1843. if (port < 0)
  1844. return -EINVAL;
  1845. /* upstream kernels had NOP for reg/unreg vlan. Continue this. */
  1846. if (!in_port && port > 0 && port <= dev->caps.num_ports) {
  1847. slave_state[slave].old_vlan_api = true;
  1848. return 0;
  1849. }
  1850. vlan = (u16) in_param;
  1851. err = __mlx4_register_vlan(dev, port, vlan, &vlan_index);
  1852. if (!err) {
  1853. set_param_l(out_param, (u32) vlan_index);
  1854. err = vlan_add_to_slave(dev, slave, vlan, port, vlan_index);
  1855. if (err)
  1856. __mlx4_unregister_vlan(dev, port, vlan);
  1857. }
  1858. return err;
  1859. }
  1860. static int counter_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1861. u64 in_param, u64 *out_param, int port)
  1862. {
  1863. u32 index;
  1864. int err;
  1865. if (op != RES_OP_RESERVE)
  1866. return -EINVAL;
  1867. err = mlx4_grant_resource(dev, slave, RES_COUNTER, 1, 0);
  1868. if (err)
  1869. return err;
  1870. err = __mlx4_counter_alloc(dev, &index);
  1871. if (err) {
  1872. mlx4_release_resource(dev, slave, RES_COUNTER, 1, 0);
  1873. return err;
  1874. }
  1875. err = add_res_range(dev, slave, index, 1, RES_COUNTER, port);
  1876. if (err) {
  1877. __mlx4_counter_free(dev, index);
  1878. mlx4_release_resource(dev, slave, RES_COUNTER, 1, 0);
  1879. } else {
  1880. set_param_l(out_param, index);
  1881. }
  1882. return err;
  1883. }
  1884. static int xrcdn_alloc_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1885. u64 in_param, u64 *out_param)
  1886. {
  1887. u32 xrcdn;
  1888. int err;
  1889. if (op != RES_OP_RESERVE)
  1890. return -EINVAL;
  1891. err = __mlx4_xrcd_alloc(dev, &xrcdn);
  1892. if (err)
  1893. return err;
  1894. err = add_res_range(dev, slave, xrcdn, 1, RES_XRCD, 0);
  1895. if (err)
  1896. __mlx4_xrcd_free(dev, xrcdn);
  1897. else
  1898. set_param_l(out_param, xrcdn);
  1899. return err;
  1900. }
  1901. int mlx4_ALLOC_RES_wrapper(struct mlx4_dev *dev, int slave,
  1902. struct mlx4_vhcr *vhcr,
  1903. struct mlx4_cmd_mailbox *inbox,
  1904. struct mlx4_cmd_mailbox *outbox,
  1905. struct mlx4_cmd_info *cmd)
  1906. {
  1907. int err;
  1908. int alop = vhcr->op_modifier;
  1909. switch (vhcr->in_modifier & 0xFF) {
  1910. case RES_QP:
  1911. err = qp_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1912. vhcr->in_param, &vhcr->out_param);
  1913. break;
  1914. case RES_MTT:
  1915. err = mtt_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1916. vhcr->in_param, &vhcr->out_param);
  1917. break;
  1918. case RES_MPT:
  1919. err = mpt_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1920. vhcr->in_param, &vhcr->out_param);
  1921. break;
  1922. case RES_CQ:
  1923. err = cq_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1924. vhcr->in_param, &vhcr->out_param);
  1925. break;
  1926. case RES_SRQ:
  1927. err = srq_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1928. vhcr->in_param, &vhcr->out_param);
  1929. break;
  1930. case RES_MAC:
  1931. err = mac_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1932. vhcr->in_param, &vhcr->out_param,
  1933. (vhcr->in_modifier >> 8) & 0xFF);
  1934. break;
  1935. case RES_VLAN:
  1936. err = vlan_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1937. vhcr->in_param, &vhcr->out_param,
  1938. (vhcr->in_modifier >> 8) & 0xFF);
  1939. break;
  1940. case RES_COUNTER:
  1941. err = counter_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1942. vhcr->in_param, &vhcr->out_param, 0);
  1943. break;
  1944. case RES_XRCD:
  1945. err = xrcdn_alloc_res(dev, slave, vhcr->op_modifier, alop,
  1946. vhcr->in_param, &vhcr->out_param);
  1947. break;
  1948. default:
  1949. err = -EINVAL;
  1950. break;
  1951. }
  1952. return err;
  1953. }
  1954. static int qp_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1955. u64 in_param)
  1956. {
  1957. int err;
  1958. int count;
  1959. int base;
  1960. int qpn;
  1961. switch (op) {
  1962. case RES_OP_RESERVE:
  1963. base = get_param_l(&in_param) & 0x7fffff;
  1964. count = get_param_h(&in_param);
  1965. err = rem_res_range(dev, slave, base, count, RES_QP, 0);
  1966. if (err)
  1967. break;
  1968. mlx4_release_resource(dev, slave, RES_QP, count, 0);
  1969. __mlx4_qp_release_range(dev, base, count);
  1970. break;
  1971. case RES_OP_MAP_ICM:
  1972. qpn = get_param_l(&in_param) & 0x7fffff;
  1973. err = qp_res_start_move_to(dev, slave, qpn, RES_QP_RESERVED,
  1974. NULL, 0);
  1975. if (err)
  1976. return err;
  1977. if (!fw_reserved(dev, qpn))
  1978. __mlx4_qp_free_icm(dev, qpn);
  1979. res_end_move(dev, slave, RES_QP, qpn);
  1980. if (valid_reserved(dev, slave, qpn))
  1981. err = rem_res_range(dev, slave, qpn, 1, RES_QP, 0);
  1982. break;
  1983. default:
  1984. err = -EINVAL;
  1985. break;
  1986. }
  1987. return err;
  1988. }
  1989. static int mtt_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  1990. u64 in_param, u64 *out_param)
  1991. {
  1992. int err = -EINVAL;
  1993. int base;
  1994. int order;
  1995. if (op != RES_OP_RESERVE_AND_MAP)
  1996. return err;
  1997. base = get_param_l(&in_param);
  1998. order = get_param_h(&in_param);
  1999. err = rem_res_range(dev, slave, base, 1, RES_MTT, order);
  2000. if (!err) {
  2001. mlx4_release_resource(dev, slave, RES_MTT, 1 << order, 0);
  2002. __mlx4_free_mtt_range(dev, base, order);
  2003. }
  2004. return err;
  2005. }
  2006. static int mpt_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  2007. u64 in_param)
  2008. {
  2009. int err = -EINVAL;
  2010. int index;
  2011. int id;
  2012. struct res_mpt *mpt;
  2013. switch (op) {
  2014. case RES_OP_RESERVE:
  2015. index = get_param_l(&in_param);
  2016. id = index & mpt_mask(dev);
  2017. err = get_res(dev, slave, id, RES_MPT, &mpt);
  2018. if (err)
  2019. break;
  2020. index = mpt->key;
  2021. put_res(dev, slave, id, RES_MPT);
  2022. err = rem_res_range(dev, slave, id, 1, RES_MPT, 0);
  2023. if (err)
  2024. break;
  2025. mlx4_release_resource(dev, slave, RES_MPT, 1, 0);
  2026. __mlx4_mpt_release(dev, index);
  2027. break;
  2028. case RES_OP_MAP_ICM:
  2029. index = get_param_l(&in_param);
  2030. id = index & mpt_mask(dev);
  2031. err = mr_res_start_move_to(dev, slave, id,
  2032. RES_MPT_RESERVED, &mpt);
  2033. if (err)
  2034. return err;
  2035. __mlx4_mpt_free_icm(dev, mpt->key);
  2036. res_end_move(dev, slave, RES_MPT, id);
  2037. return err;
  2038. break;
  2039. default:
  2040. err = -EINVAL;
  2041. break;
  2042. }
  2043. return err;
  2044. }
  2045. static int cq_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  2046. u64 in_param, u64 *out_param)
  2047. {
  2048. int cqn;
  2049. int err;
  2050. switch (op) {
  2051. case RES_OP_RESERVE_AND_MAP:
  2052. cqn = get_param_l(&in_param);
  2053. err = rem_res_range(dev, slave, cqn, 1, RES_CQ, 0);
  2054. if (err)
  2055. break;
  2056. mlx4_release_resource(dev, slave, RES_CQ, 1, 0);
  2057. __mlx4_cq_free_icm(dev, cqn);
  2058. break;
  2059. default:
  2060. err = -EINVAL;
  2061. break;
  2062. }
  2063. return err;
  2064. }
  2065. static int srq_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  2066. u64 in_param, u64 *out_param)
  2067. {
  2068. int srqn;
  2069. int err;
  2070. switch (op) {
  2071. case RES_OP_RESERVE_AND_MAP:
  2072. srqn = get_param_l(&in_param);
  2073. err = rem_res_range(dev, slave, srqn, 1, RES_SRQ, 0);
  2074. if (err)
  2075. break;
  2076. mlx4_release_resource(dev, slave, RES_SRQ, 1, 0);
  2077. __mlx4_srq_free_icm(dev, srqn);
  2078. break;
  2079. default:
  2080. err = -EINVAL;
  2081. break;
  2082. }
  2083. return err;
  2084. }
  2085. static int mac_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  2086. u64 in_param, u64 *out_param, int in_port)
  2087. {
  2088. int port;
  2089. int err = 0;
  2090. switch (op) {
  2091. case RES_OP_RESERVE_AND_MAP:
  2092. port = !in_port ? get_param_l(out_param) : in_port;
  2093. port = mlx4_slave_convert_port(
  2094. dev, slave, port);
  2095. if (port < 0)
  2096. return -EINVAL;
  2097. mac_del_from_slave(dev, slave, in_param, port);
  2098. __mlx4_unregister_mac(dev, port, in_param);
  2099. break;
  2100. default:
  2101. err = -EINVAL;
  2102. break;
  2103. }
  2104. return err;
  2105. }
  2106. static int vlan_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  2107. u64 in_param, u64 *out_param, int port)
  2108. {
  2109. struct mlx4_priv *priv = mlx4_priv(dev);
  2110. struct mlx4_slave_state *slave_state = priv->mfunc.master.slave_state;
  2111. int err = 0;
  2112. port = mlx4_slave_convert_port(
  2113. dev, slave, port);
  2114. if (port < 0)
  2115. return -EINVAL;
  2116. switch (op) {
  2117. case RES_OP_RESERVE_AND_MAP:
  2118. if (slave_state[slave].old_vlan_api)
  2119. return 0;
  2120. if (!port)
  2121. return -EINVAL;
  2122. vlan_del_from_slave(dev, slave, in_param, port);
  2123. __mlx4_unregister_vlan(dev, port, in_param);
  2124. break;
  2125. default:
  2126. err = -EINVAL;
  2127. break;
  2128. }
  2129. return err;
  2130. }
  2131. static int counter_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  2132. u64 in_param, u64 *out_param)
  2133. {
  2134. int index;
  2135. int err;
  2136. if (op != RES_OP_RESERVE)
  2137. return -EINVAL;
  2138. index = get_param_l(&in_param);
  2139. if (index == MLX4_SINK_COUNTER_INDEX(dev))
  2140. return 0;
  2141. err = rem_res_range(dev, slave, index, 1, RES_COUNTER, 0);
  2142. if (err)
  2143. return err;
  2144. __mlx4_counter_free(dev, index);
  2145. mlx4_release_resource(dev, slave, RES_COUNTER, 1, 0);
  2146. return err;
  2147. }
  2148. static int xrcdn_free_res(struct mlx4_dev *dev, int slave, int op, int cmd,
  2149. u64 in_param, u64 *out_param)
  2150. {
  2151. int xrcdn;
  2152. int err;
  2153. if (op != RES_OP_RESERVE)
  2154. return -EINVAL;
  2155. xrcdn = get_param_l(&in_param);
  2156. err = rem_res_range(dev, slave, xrcdn, 1, RES_XRCD, 0);
  2157. if (err)
  2158. return err;
  2159. __mlx4_xrcd_free(dev, xrcdn);
  2160. return err;
  2161. }
  2162. int mlx4_FREE_RES_wrapper(struct mlx4_dev *dev, int slave,
  2163. struct mlx4_vhcr *vhcr,
  2164. struct mlx4_cmd_mailbox *inbox,
  2165. struct mlx4_cmd_mailbox *outbox,
  2166. struct mlx4_cmd_info *cmd)
  2167. {
  2168. int err = -EINVAL;
  2169. int alop = vhcr->op_modifier;
  2170. switch (vhcr->in_modifier & 0xFF) {
  2171. case RES_QP:
  2172. err = qp_free_res(dev, slave, vhcr->op_modifier, alop,
  2173. vhcr->in_param);
  2174. break;
  2175. case RES_MTT:
  2176. err = mtt_free_res(dev, slave, vhcr->op_modifier, alop,
  2177. vhcr->in_param, &vhcr->out_param);
  2178. break;
  2179. case RES_MPT:
  2180. err = mpt_free_res(dev, slave, vhcr->op_modifier, alop,
  2181. vhcr->in_param);
  2182. break;
  2183. case RES_CQ:
  2184. err = cq_free_res(dev, slave, vhcr->op_modifier, alop,
  2185. vhcr->in_param, &vhcr->out_param);
  2186. break;
  2187. case RES_SRQ:
  2188. err = srq_free_res(dev, slave, vhcr->op_modifier, alop,
  2189. vhcr->in_param, &vhcr->out_param);
  2190. break;
  2191. case RES_MAC:
  2192. err = mac_free_res(dev, slave, vhcr->op_modifier, alop,
  2193. vhcr->in_param, &vhcr->out_param,
  2194. (vhcr->in_modifier >> 8) & 0xFF);
  2195. break;
  2196. case RES_VLAN:
  2197. err = vlan_free_res(dev, slave, vhcr->op_modifier, alop,
  2198. vhcr->in_param, &vhcr->out_param,
  2199. (vhcr->in_modifier >> 8) & 0xFF);
  2200. break;
  2201. case RES_COUNTER:
  2202. err = counter_free_res(dev, slave, vhcr->op_modifier, alop,
  2203. vhcr->in_param, &vhcr->out_param);
  2204. break;
  2205. case RES_XRCD:
  2206. err = xrcdn_free_res(dev, slave, vhcr->op_modifier, alop,
  2207. vhcr->in_param, &vhcr->out_param);
  2208. default:
  2209. break;
  2210. }
  2211. return err;
  2212. }
  2213. /* ugly but other choices are uglier */
  2214. static int mr_phys_mpt(struct mlx4_mpt_entry *mpt)
  2215. {
  2216. return (be32_to_cpu(mpt->flags) >> 9) & 1;
  2217. }
  2218. static int mr_get_mtt_addr(struct mlx4_mpt_entry *mpt)
  2219. {
  2220. return (int)be64_to_cpu(mpt->mtt_addr) & 0xfffffff8;
  2221. }
  2222. static int mr_get_mtt_size(struct mlx4_mpt_entry *mpt)
  2223. {
  2224. return be32_to_cpu(mpt->mtt_sz);
  2225. }
  2226. static u32 mr_get_pd(struct mlx4_mpt_entry *mpt)
  2227. {
  2228. return be32_to_cpu(mpt->pd_flags) & 0x00ffffff;
  2229. }
  2230. static int mr_is_fmr(struct mlx4_mpt_entry *mpt)
  2231. {
  2232. return be32_to_cpu(mpt->pd_flags) & MLX4_MPT_PD_FLAG_FAST_REG;
  2233. }
  2234. static int mr_is_bind_enabled(struct mlx4_mpt_entry *mpt)
  2235. {
  2236. return be32_to_cpu(mpt->flags) & MLX4_MPT_FLAG_BIND_ENABLE;
  2237. }
  2238. static int mr_is_region(struct mlx4_mpt_entry *mpt)
  2239. {
  2240. return be32_to_cpu(mpt->flags) & MLX4_MPT_FLAG_REGION;
  2241. }
  2242. static int qp_get_mtt_addr(struct mlx4_qp_context *qpc)
  2243. {
  2244. return be32_to_cpu(qpc->mtt_base_addr_l) & 0xfffffff8;
  2245. }
  2246. static int srq_get_mtt_addr(struct mlx4_srq_context *srqc)
  2247. {
  2248. return be32_to_cpu(srqc->mtt_base_addr_l) & 0xfffffff8;
  2249. }
  2250. static int qp_get_mtt_size(struct mlx4_qp_context *qpc)
  2251. {
  2252. int page_shift = (qpc->log_page_size & 0x3f) + 12;
  2253. int log_sq_size = (qpc->sq_size_stride >> 3) & 0xf;
  2254. int log_sq_sride = qpc->sq_size_stride & 7;
  2255. int log_rq_size = (qpc->rq_size_stride >> 3) & 0xf;
  2256. int log_rq_stride = qpc->rq_size_stride & 7;
  2257. int srq = (be32_to_cpu(qpc->srqn) >> 24) & 1;
  2258. int rss = (be32_to_cpu(qpc->flags) >> 13) & 1;
  2259. u32 ts = (be32_to_cpu(qpc->flags) >> 16) & 0xff;
  2260. int xrc = (ts == MLX4_QP_ST_XRC) ? 1 : 0;
  2261. int sq_size;
  2262. int rq_size;
  2263. int total_pages;
  2264. int total_mem;
  2265. int page_offset = (be32_to_cpu(qpc->params2) >> 6) & 0x3f;
  2266. int tot;
  2267. sq_size = 1 << (log_sq_size + log_sq_sride + 4);
  2268. rq_size = (srq|rss|xrc) ? 0 : (1 << (log_rq_size + log_rq_stride + 4));
  2269. total_mem = sq_size + rq_size;
  2270. tot = (total_mem + (page_offset << 6)) >> page_shift;
  2271. total_pages = !tot ? 1 : roundup_pow_of_two(tot);
  2272. return total_pages;
  2273. }
  2274. static int check_mtt_range(struct mlx4_dev *dev, int slave, int start,
  2275. int size, struct res_mtt *mtt)
  2276. {
  2277. int res_start = mtt->com.res_id;
  2278. int res_size = (1 << mtt->order);
  2279. if (start < res_start || start + size > res_start + res_size)
  2280. return -EPERM;
  2281. return 0;
  2282. }
  2283. int mlx4_SW2HW_MPT_wrapper(struct mlx4_dev *dev, int slave,
  2284. struct mlx4_vhcr *vhcr,
  2285. struct mlx4_cmd_mailbox *inbox,
  2286. struct mlx4_cmd_mailbox *outbox,
  2287. struct mlx4_cmd_info *cmd)
  2288. {
  2289. int err;
  2290. int index = vhcr->in_modifier;
  2291. struct res_mtt *mtt;
  2292. struct res_mpt *mpt;
  2293. int mtt_base = mr_get_mtt_addr(inbox->buf) / dev->caps.mtt_entry_sz;
  2294. int phys;
  2295. int id;
  2296. u32 pd;
  2297. int pd_slave;
  2298. id = index & mpt_mask(dev);
  2299. err = mr_res_start_move_to(dev, slave, id, RES_MPT_HW, &mpt);
  2300. if (err)
  2301. return err;
  2302. /* Disable memory windows for VFs. */
  2303. if (!mr_is_region(inbox->buf)) {
  2304. err = -EPERM;
  2305. goto ex_abort;
  2306. }
  2307. /* Make sure that the PD bits related to the slave id are zeros. */
  2308. pd = mr_get_pd(inbox->buf);
  2309. pd_slave = (pd >> 17) & 0x7f;
  2310. if (pd_slave != 0 && --pd_slave != slave) {
  2311. err = -EPERM;
  2312. goto ex_abort;
  2313. }
  2314. if (mr_is_fmr(inbox->buf)) {
  2315. /* FMR and Bind Enable are forbidden in slave devices. */
  2316. if (mr_is_bind_enabled(inbox->buf)) {
  2317. err = -EPERM;
  2318. goto ex_abort;
  2319. }
  2320. /* FMR and Memory Windows are also forbidden. */
  2321. if (!mr_is_region(inbox->buf)) {
  2322. err = -EPERM;
  2323. goto ex_abort;
  2324. }
  2325. }
  2326. phys = mr_phys_mpt(inbox->buf);
  2327. if (!phys) {
  2328. err = get_res(dev, slave, mtt_base, RES_MTT, &mtt);
  2329. if (err)
  2330. goto ex_abort;
  2331. err = check_mtt_range(dev, slave, mtt_base,
  2332. mr_get_mtt_size(inbox->buf), mtt);
  2333. if (err)
  2334. goto ex_put;
  2335. mpt->mtt = mtt;
  2336. }
  2337. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2338. if (err)
  2339. goto ex_put;
  2340. if (!phys) {
  2341. atomic_inc(&mtt->ref_count);
  2342. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  2343. }
  2344. res_end_move(dev, slave, RES_MPT, id);
  2345. return 0;
  2346. ex_put:
  2347. if (!phys)
  2348. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  2349. ex_abort:
  2350. res_abort_move(dev, slave, RES_MPT, id);
  2351. return err;
  2352. }
  2353. int mlx4_HW2SW_MPT_wrapper(struct mlx4_dev *dev, int slave,
  2354. struct mlx4_vhcr *vhcr,
  2355. struct mlx4_cmd_mailbox *inbox,
  2356. struct mlx4_cmd_mailbox *outbox,
  2357. struct mlx4_cmd_info *cmd)
  2358. {
  2359. int err;
  2360. int index = vhcr->in_modifier;
  2361. struct res_mpt *mpt;
  2362. int id;
  2363. id = index & mpt_mask(dev);
  2364. err = mr_res_start_move_to(dev, slave, id, RES_MPT_MAPPED, &mpt);
  2365. if (err)
  2366. return err;
  2367. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2368. if (err)
  2369. goto ex_abort;
  2370. if (mpt->mtt)
  2371. atomic_dec(&mpt->mtt->ref_count);
  2372. res_end_move(dev, slave, RES_MPT, id);
  2373. return 0;
  2374. ex_abort:
  2375. res_abort_move(dev, slave, RES_MPT, id);
  2376. return err;
  2377. }
  2378. int mlx4_QUERY_MPT_wrapper(struct mlx4_dev *dev, int slave,
  2379. struct mlx4_vhcr *vhcr,
  2380. struct mlx4_cmd_mailbox *inbox,
  2381. struct mlx4_cmd_mailbox *outbox,
  2382. struct mlx4_cmd_info *cmd)
  2383. {
  2384. int err;
  2385. int index = vhcr->in_modifier;
  2386. struct res_mpt *mpt;
  2387. int id;
  2388. id = index & mpt_mask(dev);
  2389. err = get_res(dev, slave, id, RES_MPT, &mpt);
  2390. if (err)
  2391. return err;
  2392. if (mpt->com.from_state == RES_MPT_MAPPED) {
  2393. /* In order to allow rereg in SRIOV, we need to alter the MPT entry. To do
  2394. * that, the VF must read the MPT. But since the MPT entry memory is not
  2395. * in the VF's virtual memory space, it must use QUERY_MPT to obtain the
  2396. * entry contents. To guarantee that the MPT cannot be changed, the driver
  2397. * must perform HW2SW_MPT before this query and return the MPT entry to HW
  2398. * ownership fofollowing the change. The change here allows the VF to
  2399. * perform QUERY_MPT also when the entry is in SW ownership.
  2400. */
  2401. struct mlx4_mpt_entry *mpt_entry = mlx4_table_find(
  2402. &mlx4_priv(dev)->mr_table.dmpt_table,
  2403. mpt->key, NULL);
  2404. if (NULL == mpt_entry || NULL == outbox->buf) {
  2405. err = -EINVAL;
  2406. goto out;
  2407. }
  2408. memcpy(outbox->buf, mpt_entry, sizeof(*mpt_entry));
  2409. err = 0;
  2410. } else if (mpt->com.from_state == RES_MPT_HW) {
  2411. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2412. } else {
  2413. err = -EBUSY;
  2414. goto out;
  2415. }
  2416. out:
  2417. put_res(dev, slave, id, RES_MPT);
  2418. return err;
  2419. }
  2420. static int qp_get_rcqn(struct mlx4_qp_context *qpc)
  2421. {
  2422. return be32_to_cpu(qpc->cqn_recv) & 0xffffff;
  2423. }
  2424. static int qp_get_scqn(struct mlx4_qp_context *qpc)
  2425. {
  2426. return be32_to_cpu(qpc->cqn_send) & 0xffffff;
  2427. }
  2428. static u32 qp_get_srqn(struct mlx4_qp_context *qpc)
  2429. {
  2430. return be32_to_cpu(qpc->srqn) & 0x1ffffff;
  2431. }
  2432. static void adjust_proxy_tun_qkey(struct mlx4_dev *dev, struct mlx4_vhcr *vhcr,
  2433. struct mlx4_qp_context *context)
  2434. {
  2435. u32 qpn = vhcr->in_modifier & 0xffffff;
  2436. u32 qkey = 0;
  2437. if (mlx4_get_parav_qkey(dev, qpn, &qkey))
  2438. return;
  2439. /* adjust qkey in qp context */
  2440. context->qkey = cpu_to_be32(qkey);
  2441. }
  2442. static int adjust_qp_sched_queue(struct mlx4_dev *dev, int slave,
  2443. struct mlx4_qp_context *qpc,
  2444. struct mlx4_cmd_mailbox *inbox);
  2445. int mlx4_RST2INIT_QP_wrapper(struct mlx4_dev *dev, int slave,
  2446. struct mlx4_vhcr *vhcr,
  2447. struct mlx4_cmd_mailbox *inbox,
  2448. struct mlx4_cmd_mailbox *outbox,
  2449. struct mlx4_cmd_info *cmd)
  2450. {
  2451. int err;
  2452. int qpn = vhcr->in_modifier & 0x7fffff;
  2453. struct res_mtt *mtt;
  2454. struct res_qp *qp;
  2455. struct mlx4_qp_context *qpc = inbox->buf + 8;
  2456. int mtt_base = qp_get_mtt_addr(qpc) / dev->caps.mtt_entry_sz;
  2457. int mtt_size = qp_get_mtt_size(qpc);
  2458. struct res_cq *rcq;
  2459. struct res_cq *scq;
  2460. int rcqn = qp_get_rcqn(qpc);
  2461. int scqn = qp_get_scqn(qpc);
  2462. u32 srqn = qp_get_srqn(qpc) & 0xffffff;
  2463. int use_srq = (qp_get_srqn(qpc) >> 24) & 1;
  2464. struct res_srq *srq;
  2465. int local_qpn = vhcr->in_modifier & 0xffffff;
  2466. err = adjust_qp_sched_queue(dev, slave, qpc, inbox);
  2467. if (err)
  2468. return err;
  2469. err = qp_res_start_move_to(dev, slave, qpn, RES_QP_HW, &qp, 0);
  2470. if (err)
  2471. return err;
  2472. qp->local_qpn = local_qpn;
  2473. qp->sched_queue = 0;
  2474. qp->param3 = 0;
  2475. qp->vlan_control = 0;
  2476. qp->fvl_rx = 0;
  2477. qp->pri_path_fl = 0;
  2478. qp->vlan_index = 0;
  2479. qp->feup = 0;
  2480. qp->qpc_flags = be32_to_cpu(qpc->flags);
  2481. err = get_res(dev, slave, mtt_base, RES_MTT, &mtt);
  2482. if (err)
  2483. goto ex_abort;
  2484. err = check_mtt_range(dev, slave, mtt_base, mtt_size, mtt);
  2485. if (err)
  2486. goto ex_put_mtt;
  2487. err = get_res(dev, slave, rcqn, RES_CQ, &rcq);
  2488. if (err)
  2489. goto ex_put_mtt;
  2490. if (scqn != rcqn) {
  2491. err = get_res(dev, slave, scqn, RES_CQ, &scq);
  2492. if (err)
  2493. goto ex_put_rcq;
  2494. } else
  2495. scq = rcq;
  2496. if (use_srq) {
  2497. err = get_res(dev, slave, srqn, RES_SRQ, &srq);
  2498. if (err)
  2499. goto ex_put_scq;
  2500. }
  2501. adjust_proxy_tun_qkey(dev, vhcr, qpc);
  2502. update_pkey_index(dev, slave, inbox);
  2503. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2504. if (err)
  2505. goto ex_put_srq;
  2506. atomic_inc(&mtt->ref_count);
  2507. qp->mtt = mtt;
  2508. atomic_inc(&rcq->ref_count);
  2509. qp->rcq = rcq;
  2510. atomic_inc(&scq->ref_count);
  2511. qp->scq = scq;
  2512. if (scqn != rcqn)
  2513. put_res(dev, slave, scqn, RES_CQ);
  2514. if (use_srq) {
  2515. atomic_inc(&srq->ref_count);
  2516. put_res(dev, slave, srqn, RES_SRQ);
  2517. qp->srq = srq;
  2518. }
  2519. /* Save param3 for dynamic changes from VST back to VGT */
  2520. qp->param3 = qpc->param3;
  2521. put_res(dev, slave, rcqn, RES_CQ);
  2522. put_res(dev, slave, mtt_base, RES_MTT);
  2523. res_end_move(dev, slave, RES_QP, qpn);
  2524. return 0;
  2525. ex_put_srq:
  2526. if (use_srq)
  2527. put_res(dev, slave, srqn, RES_SRQ);
  2528. ex_put_scq:
  2529. if (scqn != rcqn)
  2530. put_res(dev, slave, scqn, RES_CQ);
  2531. ex_put_rcq:
  2532. put_res(dev, slave, rcqn, RES_CQ);
  2533. ex_put_mtt:
  2534. put_res(dev, slave, mtt_base, RES_MTT);
  2535. ex_abort:
  2536. res_abort_move(dev, slave, RES_QP, qpn);
  2537. return err;
  2538. }
  2539. static int eq_get_mtt_addr(struct mlx4_eq_context *eqc)
  2540. {
  2541. return be32_to_cpu(eqc->mtt_base_addr_l) & 0xfffffff8;
  2542. }
  2543. static int eq_get_mtt_size(struct mlx4_eq_context *eqc)
  2544. {
  2545. int log_eq_size = eqc->log_eq_size & 0x1f;
  2546. int page_shift = (eqc->log_page_size & 0x3f) + 12;
  2547. if (log_eq_size + 5 < page_shift)
  2548. return 1;
  2549. return 1 << (log_eq_size + 5 - page_shift);
  2550. }
  2551. static int cq_get_mtt_addr(struct mlx4_cq_context *cqc)
  2552. {
  2553. return be32_to_cpu(cqc->mtt_base_addr_l) & 0xfffffff8;
  2554. }
  2555. static int cq_get_mtt_size(struct mlx4_cq_context *cqc)
  2556. {
  2557. int log_cq_size = (be32_to_cpu(cqc->logsize_usrpage) >> 24) & 0x1f;
  2558. int page_shift = (cqc->log_page_size & 0x3f) + 12;
  2559. if (log_cq_size + 5 < page_shift)
  2560. return 1;
  2561. return 1 << (log_cq_size + 5 - page_shift);
  2562. }
  2563. int mlx4_SW2HW_EQ_wrapper(struct mlx4_dev *dev, int slave,
  2564. struct mlx4_vhcr *vhcr,
  2565. struct mlx4_cmd_mailbox *inbox,
  2566. struct mlx4_cmd_mailbox *outbox,
  2567. struct mlx4_cmd_info *cmd)
  2568. {
  2569. int err;
  2570. int eqn = vhcr->in_modifier;
  2571. int res_id = (slave << 10) | eqn;
  2572. struct mlx4_eq_context *eqc = inbox->buf;
  2573. int mtt_base = eq_get_mtt_addr(eqc) / dev->caps.mtt_entry_sz;
  2574. int mtt_size = eq_get_mtt_size(eqc);
  2575. struct res_eq *eq;
  2576. struct res_mtt *mtt;
  2577. err = add_res_range(dev, slave, res_id, 1, RES_EQ, 0);
  2578. if (err)
  2579. return err;
  2580. err = eq_res_start_move_to(dev, slave, res_id, RES_EQ_HW, &eq);
  2581. if (err)
  2582. goto out_add;
  2583. err = get_res(dev, slave, mtt_base, RES_MTT, &mtt);
  2584. if (err)
  2585. goto out_move;
  2586. err = check_mtt_range(dev, slave, mtt_base, mtt_size, mtt);
  2587. if (err)
  2588. goto out_put;
  2589. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2590. if (err)
  2591. goto out_put;
  2592. atomic_inc(&mtt->ref_count);
  2593. eq->mtt = mtt;
  2594. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  2595. res_end_move(dev, slave, RES_EQ, res_id);
  2596. return 0;
  2597. out_put:
  2598. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  2599. out_move:
  2600. res_abort_move(dev, slave, RES_EQ, res_id);
  2601. out_add:
  2602. rem_res_range(dev, slave, res_id, 1, RES_EQ, 0);
  2603. return err;
  2604. }
  2605. int mlx4_CONFIG_DEV_wrapper(struct mlx4_dev *dev, int slave,
  2606. struct mlx4_vhcr *vhcr,
  2607. struct mlx4_cmd_mailbox *inbox,
  2608. struct mlx4_cmd_mailbox *outbox,
  2609. struct mlx4_cmd_info *cmd)
  2610. {
  2611. int err;
  2612. u8 get = vhcr->op_modifier;
  2613. if (get != 1)
  2614. return -EPERM;
  2615. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2616. return err;
  2617. }
  2618. static int get_containing_mtt(struct mlx4_dev *dev, int slave, int start,
  2619. int len, struct res_mtt **res)
  2620. {
  2621. struct mlx4_priv *priv = mlx4_priv(dev);
  2622. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  2623. struct res_mtt *mtt;
  2624. int err = -EINVAL;
  2625. spin_lock_irq(mlx4_tlock(dev));
  2626. list_for_each_entry(mtt, &tracker->slave_list[slave].res_list[RES_MTT],
  2627. com.list) {
  2628. if (!check_mtt_range(dev, slave, start, len, mtt)) {
  2629. *res = mtt;
  2630. mtt->com.from_state = mtt->com.state;
  2631. mtt->com.state = RES_MTT_BUSY;
  2632. err = 0;
  2633. break;
  2634. }
  2635. }
  2636. spin_unlock_irq(mlx4_tlock(dev));
  2637. return err;
  2638. }
  2639. static int verify_qp_parameters(struct mlx4_dev *dev,
  2640. struct mlx4_vhcr *vhcr,
  2641. struct mlx4_cmd_mailbox *inbox,
  2642. enum qp_transition transition, u8 slave)
  2643. {
  2644. u32 qp_type;
  2645. u32 qpn;
  2646. struct mlx4_qp_context *qp_ctx;
  2647. enum mlx4_qp_optpar optpar;
  2648. int port;
  2649. int num_gids;
  2650. qp_ctx = inbox->buf + 8;
  2651. qp_type = (be32_to_cpu(qp_ctx->flags) >> 16) & 0xff;
  2652. optpar = be32_to_cpu(*(__be32 *) inbox->buf);
  2653. if (slave != mlx4_master_func_num(dev)) {
  2654. qp_ctx->params2 &= ~MLX4_QP_BIT_FPP;
  2655. /* setting QP rate-limit is disallowed for VFs */
  2656. if (qp_ctx->rate_limit_params)
  2657. return -EPERM;
  2658. }
  2659. switch (qp_type) {
  2660. case MLX4_QP_ST_RC:
  2661. case MLX4_QP_ST_XRC:
  2662. case MLX4_QP_ST_UC:
  2663. switch (transition) {
  2664. case QP_TRANS_INIT2RTR:
  2665. case QP_TRANS_RTR2RTS:
  2666. case QP_TRANS_RTS2RTS:
  2667. case QP_TRANS_SQD2SQD:
  2668. case QP_TRANS_SQD2RTS:
  2669. if (slave != mlx4_master_func_num(dev)) {
  2670. if (optpar & MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH) {
  2671. port = (qp_ctx->pri_path.sched_queue >> 6 & 1) + 1;
  2672. if (dev->caps.port_mask[port] != MLX4_PORT_TYPE_IB)
  2673. num_gids = mlx4_get_slave_num_gids(dev, slave, port);
  2674. else
  2675. num_gids = 1;
  2676. if (qp_ctx->pri_path.mgid_index >= num_gids)
  2677. return -EINVAL;
  2678. }
  2679. if (optpar & MLX4_QP_OPTPAR_ALT_ADDR_PATH) {
  2680. port = (qp_ctx->alt_path.sched_queue >> 6 & 1) + 1;
  2681. if (dev->caps.port_mask[port] != MLX4_PORT_TYPE_IB)
  2682. num_gids = mlx4_get_slave_num_gids(dev, slave, port);
  2683. else
  2684. num_gids = 1;
  2685. if (qp_ctx->alt_path.mgid_index >= num_gids)
  2686. return -EINVAL;
  2687. }
  2688. }
  2689. break;
  2690. default:
  2691. break;
  2692. }
  2693. break;
  2694. case MLX4_QP_ST_MLX:
  2695. qpn = vhcr->in_modifier & 0x7fffff;
  2696. port = (qp_ctx->pri_path.sched_queue >> 6 & 1) + 1;
  2697. if (transition == QP_TRANS_INIT2RTR &&
  2698. slave != mlx4_master_func_num(dev) &&
  2699. mlx4_is_qp_reserved(dev, qpn) &&
  2700. !mlx4_vf_smi_enabled(dev, slave, port)) {
  2701. /* only enabled VFs may create MLX proxy QPs */
  2702. mlx4_err(dev, "%s: unprivileged slave %d attempting to create an MLX proxy special QP on port %d\n",
  2703. __func__, slave, port);
  2704. return -EPERM;
  2705. }
  2706. break;
  2707. default:
  2708. break;
  2709. }
  2710. return 0;
  2711. }
  2712. int mlx4_WRITE_MTT_wrapper(struct mlx4_dev *dev, int slave,
  2713. struct mlx4_vhcr *vhcr,
  2714. struct mlx4_cmd_mailbox *inbox,
  2715. struct mlx4_cmd_mailbox *outbox,
  2716. struct mlx4_cmd_info *cmd)
  2717. {
  2718. struct mlx4_mtt mtt;
  2719. __be64 *page_list = inbox->buf;
  2720. u64 *pg_list = (u64 *)page_list;
  2721. int i;
  2722. struct res_mtt *rmtt = NULL;
  2723. int start = be64_to_cpu(page_list[0]);
  2724. int npages = vhcr->in_modifier;
  2725. int err;
  2726. err = get_containing_mtt(dev, slave, start, npages, &rmtt);
  2727. if (err)
  2728. return err;
  2729. /* Call the SW implementation of write_mtt:
  2730. * - Prepare a dummy mtt struct
  2731. * - Translate inbox contents to simple addresses in host endianness */
  2732. mtt.offset = 0; /* TBD this is broken but I don't handle it since
  2733. we don't really use it */
  2734. mtt.order = 0;
  2735. mtt.page_shift = 0;
  2736. for (i = 0; i < npages; ++i)
  2737. pg_list[i + 2] = (be64_to_cpu(page_list[i + 2]) & ~1ULL);
  2738. err = __mlx4_write_mtt(dev, &mtt, be64_to_cpu(page_list[0]), npages,
  2739. ((u64 *)page_list + 2));
  2740. if (rmtt)
  2741. put_res(dev, slave, rmtt->com.res_id, RES_MTT);
  2742. return err;
  2743. }
  2744. int mlx4_HW2SW_EQ_wrapper(struct mlx4_dev *dev, int slave,
  2745. struct mlx4_vhcr *vhcr,
  2746. struct mlx4_cmd_mailbox *inbox,
  2747. struct mlx4_cmd_mailbox *outbox,
  2748. struct mlx4_cmd_info *cmd)
  2749. {
  2750. int eqn = vhcr->in_modifier;
  2751. int res_id = eqn | (slave << 10);
  2752. struct res_eq *eq;
  2753. int err;
  2754. err = eq_res_start_move_to(dev, slave, res_id, RES_EQ_RESERVED, &eq);
  2755. if (err)
  2756. return err;
  2757. err = get_res(dev, slave, eq->mtt->com.res_id, RES_MTT, NULL);
  2758. if (err)
  2759. goto ex_abort;
  2760. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2761. if (err)
  2762. goto ex_put;
  2763. atomic_dec(&eq->mtt->ref_count);
  2764. put_res(dev, slave, eq->mtt->com.res_id, RES_MTT);
  2765. res_end_move(dev, slave, RES_EQ, res_id);
  2766. rem_res_range(dev, slave, res_id, 1, RES_EQ, 0);
  2767. return 0;
  2768. ex_put:
  2769. put_res(dev, slave, eq->mtt->com.res_id, RES_MTT);
  2770. ex_abort:
  2771. res_abort_move(dev, slave, RES_EQ, res_id);
  2772. return err;
  2773. }
  2774. int mlx4_GEN_EQE(struct mlx4_dev *dev, int slave, struct mlx4_eqe *eqe)
  2775. {
  2776. struct mlx4_priv *priv = mlx4_priv(dev);
  2777. struct mlx4_slave_event_eq_info *event_eq;
  2778. struct mlx4_cmd_mailbox *mailbox;
  2779. u32 in_modifier = 0;
  2780. int err;
  2781. int res_id;
  2782. struct res_eq *req;
  2783. if (!priv->mfunc.master.slave_state)
  2784. return -EINVAL;
  2785. /* check for slave valid, slave not PF, and slave active */
  2786. if (slave < 0 || slave > dev->persist->num_vfs ||
  2787. slave == dev->caps.function ||
  2788. !priv->mfunc.master.slave_state[slave].active)
  2789. return 0;
  2790. event_eq = &priv->mfunc.master.slave_state[slave].event_eq[eqe->type];
  2791. /* Create the event only if the slave is registered */
  2792. if (event_eq->eqn < 0)
  2793. return 0;
  2794. mutex_lock(&priv->mfunc.master.gen_eqe_mutex[slave]);
  2795. res_id = (slave << 10) | event_eq->eqn;
  2796. err = get_res(dev, slave, res_id, RES_EQ, &req);
  2797. if (err)
  2798. goto unlock;
  2799. if (req->com.from_state != RES_EQ_HW) {
  2800. err = -EINVAL;
  2801. goto put;
  2802. }
  2803. mailbox = mlx4_alloc_cmd_mailbox(dev);
  2804. if (IS_ERR(mailbox)) {
  2805. err = PTR_ERR(mailbox);
  2806. goto put;
  2807. }
  2808. if (eqe->type == MLX4_EVENT_TYPE_CMD) {
  2809. ++event_eq->token;
  2810. eqe->event.cmd.token = cpu_to_be16(event_eq->token);
  2811. }
  2812. memcpy(mailbox->buf, (u8 *) eqe, 28);
  2813. in_modifier = (slave & 0xff) | ((event_eq->eqn & 0x3ff) << 16);
  2814. err = mlx4_cmd(dev, mailbox->dma, in_modifier, 0,
  2815. MLX4_CMD_GEN_EQE, MLX4_CMD_TIME_CLASS_B,
  2816. MLX4_CMD_NATIVE);
  2817. put_res(dev, slave, res_id, RES_EQ);
  2818. mutex_unlock(&priv->mfunc.master.gen_eqe_mutex[slave]);
  2819. mlx4_free_cmd_mailbox(dev, mailbox);
  2820. return err;
  2821. put:
  2822. put_res(dev, slave, res_id, RES_EQ);
  2823. unlock:
  2824. mutex_unlock(&priv->mfunc.master.gen_eqe_mutex[slave]);
  2825. return err;
  2826. }
  2827. int mlx4_QUERY_EQ_wrapper(struct mlx4_dev *dev, int slave,
  2828. struct mlx4_vhcr *vhcr,
  2829. struct mlx4_cmd_mailbox *inbox,
  2830. struct mlx4_cmd_mailbox *outbox,
  2831. struct mlx4_cmd_info *cmd)
  2832. {
  2833. int eqn = vhcr->in_modifier;
  2834. int res_id = eqn | (slave << 10);
  2835. struct res_eq *eq;
  2836. int err;
  2837. err = get_res(dev, slave, res_id, RES_EQ, &eq);
  2838. if (err)
  2839. return err;
  2840. if (eq->com.from_state != RES_EQ_HW) {
  2841. err = -EINVAL;
  2842. goto ex_put;
  2843. }
  2844. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2845. ex_put:
  2846. put_res(dev, slave, res_id, RES_EQ);
  2847. return err;
  2848. }
  2849. int mlx4_SW2HW_CQ_wrapper(struct mlx4_dev *dev, int slave,
  2850. struct mlx4_vhcr *vhcr,
  2851. struct mlx4_cmd_mailbox *inbox,
  2852. struct mlx4_cmd_mailbox *outbox,
  2853. struct mlx4_cmd_info *cmd)
  2854. {
  2855. int err;
  2856. int cqn = vhcr->in_modifier;
  2857. struct mlx4_cq_context *cqc = inbox->buf;
  2858. int mtt_base = cq_get_mtt_addr(cqc) / dev->caps.mtt_entry_sz;
  2859. struct res_cq *cq = NULL;
  2860. struct res_mtt *mtt;
  2861. err = cq_res_start_move_to(dev, slave, cqn, RES_CQ_HW, &cq);
  2862. if (err)
  2863. return err;
  2864. err = get_res(dev, slave, mtt_base, RES_MTT, &mtt);
  2865. if (err)
  2866. goto out_move;
  2867. err = check_mtt_range(dev, slave, mtt_base, cq_get_mtt_size(cqc), mtt);
  2868. if (err)
  2869. goto out_put;
  2870. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2871. if (err)
  2872. goto out_put;
  2873. atomic_inc(&mtt->ref_count);
  2874. cq->mtt = mtt;
  2875. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  2876. res_end_move(dev, slave, RES_CQ, cqn);
  2877. return 0;
  2878. out_put:
  2879. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  2880. out_move:
  2881. res_abort_move(dev, slave, RES_CQ, cqn);
  2882. return err;
  2883. }
  2884. int mlx4_HW2SW_CQ_wrapper(struct mlx4_dev *dev, int slave,
  2885. struct mlx4_vhcr *vhcr,
  2886. struct mlx4_cmd_mailbox *inbox,
  2887. struct mlx4_cmd_mailbox *outbox,
  2888. struct mlx4_cmd_info *cmd)
  2889. {
  2890. int err;
  2891. int cqn = vhcr->in_modifier;
  2892. struct res_cq *cq = NULL;
  2893. err = cq_res_start_move_to(dev, slave, cqn, RES_CQ_ALLOCATED, &cq);
  2894. if (err)
  2895. return err;
  2896. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2897. if (err)
  2898. goto out_move;
  2899. atomic_dec(&cq->mtt->ref_count);
  2900. res_end_move(dev, slave, RES_CQ, cqn);
  2901. return 0;
  2902. out_move:
  2903. res_abort_move(dev, slave, RES_CQ, cqn);
  2904. return err;
  2905. }
  2906. int mlx4_QUERY_CQ_wrapper(struct mlx4_dev *dev, int slave,
  2907. struct mlx4_vhcr *vhcr,
  2908. struct mlx4_cmd_mailbox *inbox,
  2909. struct mlx4_cmd_mailbox *outbox,
  2910. struct mlx4_cmd_info *cmd)
  2911. {
  2912. int cqn = vhcr->in_modifier;
  2913. struct res_cq *cq;
  2914. int err;
  2915. err = get_res(dev, slave, cqn, RES_CQ, &cq);
  2916. if (err)
  2917. return err;
  2918. if (cq->com.from_state != RES_CQ_HW)
  2919. goto ex_put;
  2920. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2921. ex_put:
  2922. put_res(dev, slave, cqn, RES_CQ);
  2923. return err;
  2924. }
  2925. static int handle_resize(struct mlx4_dev *dev, int slave,
  2926. struct mlx4_vhcr *vhcr,
  2927. struct mlx4_cmd_mailbox *inbox,
  2928. struct mlx4_cmd_mailbox *outbox,
  2929. struct mlx4_cmd_info *cmd,
  2930. struct res_cq *cq)
  2931. {
  2932. int err;
  2933. struct res_mtt *orig_mtt;
  2934. struct res_mtt *mtt;
  2935. struct mlx4_cq_context *cqc = inbox->buf;
  2936. int mtt_base = cq_get_mtt_addr(cqc) / dev->caps.mtt_entry_sz;
  2937. err = get_res(dev, slave, cq->mtt->com.res_id, RES_MTT, &orig_mtt);
  2938. if (err)
  2939. return err;
  2940. if (orig_mtt != cq->mtt) {
  2941. err = -EINVAL;
  2942. goto ex_put;
  2943. }
  2944. err = get_res(dev, slave, mtt_base, RES_MTT, &mtt);
  2945. if (err)
  2946. goto ex_put;
  2947. err = check_mtt_range(dev, slave, mtt_base, cq_get_mtt_size(cqc), mtt);
  2948. if (err)
  2949. goto ex_put1;
  2950. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2951. if (err)
  2952. goto ex_put1;
  2953. atomic_dec(&orig_mtt->ref_count);
  2954. put_res(dev, slave, orig_mtt->com.res_id, RES_MTT);
  2955. atomic_inc(&mtt->ref_count);
  2956. cq->mtt = mtt;
  2957. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  2958. return 0;
  2959. ex_put1:
  2960. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  2961. ex_put:
  2962. put_res(dev, slave, orig_mtt->com.res_id, RES_MTT);
  2963. return err;
  2964. }
  2965. int mlx4_MODIFY_CQ_wrapper(struct mlx4_dev *dev, int slave,
  2966. struct mlx4_vhcr *vhcr,
  2967. struct mlx4_cmd_mailbox *inbox,
  2968. struct mlx4_cmd_mailbox *outbox,
  2969. struct mlx4_cmd_info *cmd)
  2970. {
  2971. int cqn = vhcr->in_modifier;
  2972. struct res_cq *cq;
  2973. int err;
  2974. err = get_res(dev, slave, cqn, RES_CQ, &cq);
  2975. if (err)
  2976. return err;
  2977. if (cq->com.from_state != RES_CQ_HW)
  2978. goto ex_put;
  2979. if (vhcr->op_modifier == 0) {
  2980. err = handle_resize(dev, slave, vhcr, inbox, outbox, cmd, cq);
  2981. goto ex_put;
  2982. }
  2983. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  2984. ex_put:
  2985. put_res(dev, slave, cqn, RES_CQ);
  2986. return err;
  2987. }
  2988. static int srq_get_mtt_size(struct mlx4_srq_context *srqc)
  2989. {
  2990. int log_srq_size = (be32_to_cpu(srqc->state_logsize_srqn) >> 24) & 0xf;
  2991. int log_rq_stride = srqc->logstride & 7;
  2992. int page_shift = (srqc->log_page_size & 0x3f) + 12;
  2993. if (log_srq_size + log_rq_stride + 4 < page_shift)
  2994. return 1;
  2995. return 1 << (log_srq_size + log_rq_stride + 4 - page_shift);
  2996. }
  2997. int mlx4_SW2HW_SRQ_wrapper(struct mlx4_dev *dev, int slave,
  2998. struct mlx4_vhcr *vhcr,
  2999. struct mlx4_cmd_mailbox *inbox,
  3000. struct mlx4_cmd_mailbox *outbox,
  3001. struct mlx4_cmd_info *cmd)
  3002. {
  3003. int err;
  3004. int srqn = vhcr->in_modifier;
  3005. struct res_mtt *mtt;
  3006. struct res_srq *srq = NULL;
  3007. struct mlx4_srq_context *srqc = inbox->buf;
  3008. int mtt_base = srq_get_mtt_addr(srqc) / dev->caps.mtt_entry_sz;
  3009. if (srqn != (be32_to_cpu(srqc->state_logsize_srqn) & 0xffffff))
  3010. return -EINVAL;
  3011. err = srq_res_start_move_to(dev, slave, srqn, RES_SRQ_HW, &srq);
  3012. if (err)
  3013. return err;
  3014. err = get_res(dev, slave, mtt_base, RES_MTT, &mtt);
  3015. if (err)
  3016. goto ex_abort;
  3017. err = check_mtt_range(dev, slave, mtt_base, srq_get_mtt_size(srqc),
  3018. mtt);
  3019. if (err)
  3020. goto ex_put_mtt;
  3021. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3022. if (err)
  3023. goto ex_put_mtt;
  3024. atomic_inc(&mtt->ref_count);
  3025. srq->mtt = mtt;
  3026. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  3027. res_end_move(dev, slave, RES_SRQ, srqn);
  3028. return 0;
  3029. ex_put_mtt:
  3030. put_res(dev, slave, mtt->com.res_id, RES_MTT);
  3031. ex_abort:
  3032. res_abort_move(dev, slave, RES_SRQ, srqn);
  3033. return err;
  3034. }
  3035. int mlx4_HW2SW_SRQ_wrapper(struct mlx4_dev *dev, int slave,
  3036. struct mlx4_vhcr *vhcr,
  3037. struct mlx4_cmd_mailbox *inbox,
  3038. struct mlx4_cmd_mailbox *outbox,
  3039. struct mlx4_cmd_info *cmd)
  3040. {
  3041. int err;
  3042. int srqn = vhcr->in_modifier;
  3043. struct res_srq *srq = NULL;
  3044. err = srq_res_start_move_to(dev, slave, srqn, RES_SRQ_ALLOCATED, &srq);
  3045. if (err)
  3046. return err;
  3047. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3048. if (err)
  3049. goto ex_abort;
  3050. atomic_dec(&srq->mtt->ref_count);
  3051. if (srq->cq)
  3052. atomic_dec(&srq->cq->ref_count);
  3053. res_end_move(dev, slave, RES_SRQ, srqn);
  3054. return 0;
  3055. ex_abort:
  3056. res_abort_move(dev, slave, RES_SRQ, srqn);
  3057. return err;
  3058. }
  3059. int mlx4_QUERY_SRQ_wrapper(struct mlx4_dev *dev, int slave,
  3060. struct mlx4_vhcr *vhcr,
  3061. struct mlx4_cmd_mailbox *inbox,
  3062. struct mlx4_cmd_mailbox *outbox,
  3063. struct mlx4_cmd_info *cmd)
  3064. {
  3065. int err;
  3066. int srqn = vhcr->in_modifier;
  3067. struct res_srq *srq;
  3068. err = get_res(dev, slave, srqn, RES_SRQ, &srq);
  3069. if (err)
  3070. return err;
  3071. if (srq->com.from_state != RES_SRQ_HW) {
  3072. err = -EBUSY;
  3073. goto out;
  3074. }
  3075. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3076. out:
  3077. put_res(dev, slave, srqn, RES_SRQ);
  3078. return err;
  3079. }
  3080. int mlx4_ARM_SRQ_wrapper(struct mlx4_dev *dev, int slave,
  3081. struct mlx4_vhcr *vhcr,
  3082. struct mlx4_cmd_mailbox *inbox,
  3083. struct mlx4_cmd_mailbox *outbox,
  3084. struct mlx4_cmd_info *cmd)
  3085. {
  3086. int err;
  3087. int srqn = vhcr->in_modifier;
  3088. struct res_srq *srq;
  3089. err = get_res(dev, slave, srqn, RES_SRQ, &srq);
  3090. if (err)
  3091. return err;
  3092. if (srq->com.from_state != RES_SRQ_HW) {
  3093. err = -EBUSY;
  3094. goto out;
  3095. }
  3096. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3097. out:
  3098. put_res(dev, slave, srqn, RES_SRQ);
  3099. return err;
  3100. }
  3101. int mlx4_GEN_QP_wrapper(struct mlx4_dev *dev, int slave,
  3102. struct mlx4_vhcr *vhcr,
  3103. struct mlx4_cmd_mailbox *inbox,
  3104. struct mlx4_cmd_mailbox *outbox,
  3105. struct mlx4_cmd_info *cmd)
  3106. {
  3107. int err;
  3108. int qpn = vhcr->in_modifier & 0x7fffff;
  3109. struct res_qp *qp;
  3110. err = get_res(dev, slave, qpn, RES_QP, &qp);
  3111. if (err)
  3112. return err;
  3113. if (qp->com.from_state != RES_QP_HW) {
  3114. err = -EBUSY;
  3115. goto out;
  3116. }
  3117. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3118. out:
  3119. put_res(dev, slave, qpn, RES_QP);
  3120. return err;
  3121. }
  3122. int mlx4_INIT2INIT_QP_wrapper(struct mlx4_dev *dev, int slave,
  3123. struct mlx4_vhcr *vhcr,
  3124. struct mlx4_cmd_mailbox *inbox,
  3125. struct mlx4_cmd_mailbox *outbox,
  3126. struct mlx4_cmd_info *cmd)
  3127. {
  3128. struct mlx4_qp_context *context = inbox->buf + 8;
  3129. adjust_proxy_tun_qkey(dev, vhcr, context);
  3130. update_pkey_index(dev, slave, inbox);
  3131. return mlx4_GEN_QP_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3132. }
  3133. static int adjust_qp_sched_queue(struct mlx4_dev *dev, int slave,
  3134. struct mlx4_qp_context *qpc,
  3135. struct mlx4_cmd_mailbox *inbox)
  3136. {
  3137. enum mlx4_qp_optpar optpar = be32_to_cpu(*(__be32 *)inbox->buf);
  3138. u8 pri_sched_queue;
  3139. int port = mlx4_slave_convert_port(
  3140. dev, slave, (qpc->pri_path.sched_queue >> 6 & 1) + 1) - 1;
  3141. if (port < 0)
  3142. return -EINVAL;
  3143. pri_sched_queue = (qpc->pri_path.sched_queue & ~(1 << 6)) |
  3144. ((port & 1) << 6);
  3145. if (optpar & (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH | MLX4_QP_OPTPAR_SCHED_QUEUE) ||
  3146. qpc->pri_path.sched_queue || mlx4_is_eth(dev, port + 1)) {
  3147. qpc->pri_path.sched_queue = pri_sched_queue;
  3148. }
  3149. if (optpar & MLX4_QP_OPTPAR_ALT_ADDR_PATH) {
  3150. port = mlx4_slave_convert_port(
  3151. dev, slave, (qpc->alt_path.sched_queue >> 6 & 1)
  3152. + 1) - 1;
  3153. if (port < 0)
  3154. return -EINVAL;
  3155. qpc->alt_path.sched_queue =
  3156. (qpc->alt_path.sched_queue & ~(1 << 6)) |
  3157. (port & 1) << 6;
  3158. }
  3159. return 0;
  3160. }
  3161. static int roce_verify_mac(struct mlx4_dev *dev, int slave,
  3162. struct mlx4_qp_context *qpc,
  3163. struct mlx4_cmd_mailbox *inbox)
  3164. {
  3165. u64 mac;
  3166. int port;
  3167. u32 ts = (be32_to_cpu(qpc->flags) >> 16) & 0xff;
  3168. u8 sched = *(u8 *)(inbox->buf + 64);
  3169. u8 smac_ix;
  3170. port = (sched >> 6 & 1) + 1;
  3171. if (mlx4_is_eth(dev, port) && (ts != MLX4_QP_ST_MLX)) {
  3172. smac_ix = qpc->pri_path.grh_mylmc & 0x7f;
  3173. if (mac_find_smac_ix_in_slave(dev, slave, port, smac_ix, &mac))
  3174. return -ENOENT;
  3175. }
  3176. return 0;
  3177. }
  3178. int mlx4_INIT2RTR_QP_wrapper(struct mlx4_dev *dev, int slave,
  3179. struct mlx4_vhcr *vhcr,
  3180. struct mlx4_cmd_mailbox *inbox,
  3181. struct mlx4_cmd_mailbox *outbox,
  3182. struct mlx4_cmd_info *cmd)
  3183. {
  3184. int err;
  3185. struct mlx4_qp_context *qpc = inbox->buf + 8;
  3186. int qpn = vhcr->in_modifier & 0x7fffff;
  3187. struct res_qp *qp;
  3188. u8 orig_sched_queue;
  3189. u8 orig_vlan_control = qpc->pri_path.vlan_control;
  3190. u8 orig_fvl_rx = qpc->pri_path.fvl_rx;
  3191. u8 orig_pri_path_fl = qpc->pri_path.fl;
  3192. u8 orig_vlan_index = qpc->pri_path.vlan_index;
  3193. u8 orig_feup = qpc->pri_path.feup;
  3194. err = adjust_qp_sched_queue(dev, slave, qpc, inbox);
  3195. if (err)
  3196. return err;
  3197. err = verify_qp_parameters(dev, vhcr, inbox, QP_TRANS_INIT2RTR, slave);
  3198. if (err)
  3199. return err;
  3200. if (roce_verify_mac(dev, slave, qpc, inbox))
  3201. return -EINVAL;
  3202. update_pkey_index(dev, slave, inbox);
  3203. update_gid(dev, inbox, (u8)slave);
  3204. adjust_proxy_tun_qkey(dev, vhcr, qpc);
  3205. orig_sched_queue = qpc->pri_path.sched_queue;
  3206. err = get_res(dev, slave, qpn, RES_QP, &qp);
  3207. if (err)
  3208. return err;
  3209. if (qp->com.from_state != RES_QP_HW) {
  3210. err = -EBUSY;
  3211. goto out;
  3212. }
  3213. err = update_vport_qp_param(dev, inbox, slave, qpn);
  3214. if (err)
  3215. goto out;
  3216. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3217. out:
  3218. /* if no error, save sched queue value passed in by VF. This is
  3219. * essentially the QOS value provided by the VF. This will be useful
  3220. * if we allow dynamic changes from VST back to VGT
  3221. */
  3222. if (!err) {
  3223. qp->sched_queue = orig_sched_queue;
  3224. qp->vlan_control = orig_vlan_control;
  3225. qp->fvl_rx = orig_fvl_rx;
  3226. qp->pri_path_fl = orig_pri_path_fl;
  3227. qp->vlan_index = orig_vlan_index;
  3228. qp->feup = orig_feup;
  3229. }
  3230. put_res(dev, slave, qpn, RES_QP);
  3231. return err;
  3232. }
  3233. int mlx4_RTR2RTS_QP_wrapper(struct mlx4_dev *dev, int slave,
  3234. struct mlx4_vhcr *vhcr,
  3235. struct mlx4_cmd_mailbox *inbox,
  3236. struct mlx4_cmd_mailbox *outbox,
  3237. struct mlx4_cmd_info *cmd)
  3238. {
  3239. int err;
  3240. struct mlx4_qp_context *context = inbox->buf + 8;
  3241. err = adjust_qp_sched_queue(dev, slave, context, inbox);
  3242. if (err)
  3243. return err;
  3244. err = verify_qp_parameters(dev, vhcr, inbox, QP_TRANS_RTR2RTS, slave);
  3245. if (err)
  3246. return err;
  3247. update_pkey_index(dev, slave, inbox);
  3248. update_gid(dev, inbox, (u8)slave);
  3249. adjust_proxy_tun_qkey(dev, vhcr, context);
  3250. return mlx4_GEN_QP_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3251. }
  3252. int mlx4_RTS2RTS_QP_wrapper(struct mlx4_dev *dev, int slave,
  3253. struct mlx4_vhcr *vhcr,
  3254. struct mlx4_cmd_mailbox *inbox,
  3255. struct mlx4_cmd_mailbox *outbox,
  3256. struct mlx4_cmd_info *cmd)
  3257. {
  3258. int err;
  3259. struct mlx4_qp_context *context = inbox->buf + 8;
  3260. err = adjust_qp_sched_queue(dev, slave, context, inbox);
  3261. if (err)
  3262. return err;
  3263. err = verify_qp_parameters(dev, vhcr, inbox, QP_TRANS_RTS2RTS, slave);
  3264. if (err)
  3265. return err;
  3266. update_pkey_index(dev, slave, inbox);
  3267. update_gid(dev, inbox, (u8)slave);
  3268. adjust_proxy_tun_qkey(dev, vhcr, context);
  3269. return mlx4_GEN_QP_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3270. }
  3271. int mlx4_SQERR2RTS_QP_wrapper(struct mlx4_dev *dev, int slave,
  3272. struct mlx4_vhcr *vhcr,
  3273. struct mlx4_cmd_mailbox *inbox,
  3274. struct mlx4_cmd_mailbox *outbox,
  3275. struct mlx4_cmd_info *cmd)
  3276. {
  3277. struct mlx4_qp_context *context = inbox->buf + 8;
  3278. int err = adjust_qp_sched_queue(dev, slave, context, inbox);
  3279. if (err)
  3280. return err;
  3281. adjust_proxy_tun_qkey(dev, vhcr, context);
  3282. return mlx4_GEN_QP_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3283. }
  3284. int mlx4_SQD2SQD_QP_wrapper(struct mlx4_dev *dev, int slave,
  3285. struct mlx4_vhcr *vhcr,
  3286. struct mlx4_cmd_mailbox *inbox,
  3287. struct mlx4_cmd_mailbox *outbox,
  3288. struct mlx4_cmd_info *cmd)
  3289. {
  3290. int err;
  3291. struct mlx4_qp_context *context = inbox->buf + 8;
  3292. err = adjust_qp_sched_queue(dev, slave, context, inbox);
  3293. if (err)
  3294. return err;
  3295. err = verify_qp_parameters(dev, vhcr, inbox, QP_TRANS_SQD2SQD, slave);
  3296. if (err)
  3297. return err;
  3298. adjust_proxy_tun_qkey(dev, vhcr, context);
  3299. update_gid(dev, inbox, (u8)slave);
  3300. update_pkey_index(dev, slave, inbox);
  3301. return mlx4_GEN_QP_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3302. }
  3303. int mlx4_SQD2RTS_QP_wrapper(struct mlx4_dev *dev, int slave,
  3304. struct mlx4_vhcr *vhcr,
  3305. struct mlx4_cmd_mailbox *inbox,
  3306. struct mlx4_cmd_mailbox *outbox,
  3307. struct mlx4_cmd_info *cmd)
  3308. {
  3309. int err;
  3310. struct mlx4_qp_context *context = inbox->buf + 8;
  3311. err = adjust_qp_sched_queue(dev, slave, context, inbox);
  3312. if (err)
  3313. return err;
  3314. err = verify_qp_parameters(dev, vhcr, inbox, QP_TRANS_SQD2RTS, slave);
  3315. if (err)
  3316. return err;
  3317. adjust_proxy_tun_qkey(dev, vhcr, context);
  3318. update_gid(dev, inbox, (u8)slave);
  3319. update_pkey_index(dev, slave, inbox);
  3320. return mlx4_GEN_QP_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3321. }
  3322. int mlx4_2RST_QP_wrapper(struct mlx4_dev *dev, int slave,
  3323. struct mlx4_vhcr *vhcr,
  3324. struct mlx4_cmd_mailbox *inbox,
  3325. struct mlx4_cmd_mailbox *outbox,
  3326. struct mlx4_cmd_info *cmd)
  3327. {
  3328. int err;
  3329. int qpn = vhcr->in_modifier & 0x7fffff;
  3330. struct res_qp *qp;
  3331. err = qp_res_start_move_to(dev, slave, qpn, RES_QP_MAPPED, &qp, 0);
  3332. if (err)
  3333. return err;
  3334. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3335. if (err)
  3336. goto ex_abort;
  3337. atomic_dec(&qp->mtt->ref_count);
  3338. atomic_dec(&qp->rcq->ref_count);
  3339. atomic_dec(&qp->scq->ref_count);
  3340. if (qp->srq)
  3341. atomic_dec(&qp->srq->ref_count);
  3342. res_end_move(dev, slave, RES_QP, qpn);
  3343. return 0;
  3344. ex_abort:
  3345. res_abort_move(dev, slave, RES_QP, qpn);
  3346. return err;
  3347. }
  3348. static struct res_gid *find_gid(struct mlx4_dev *dev, int slave,
  3349. struct res_qp *rqp, u8 *gid)
  3350. {
  3351. struct res_gid *res;
  3352. list_for_each_entry(res, &rqp->mcg_list, list) {
  3353. if (!memcmp(res->gid, gid, 16))
  3354. return res;
  3355. }
  3356. return NULL;
  3357. }
  3358. static int add_mcg_res(struct mlx4_dev *dev, int slave, struct res_qp *rqp,
  3359. u8 *gid, enum mlx4_protocol prot,
  3360. enum mlx4_steer_type steer, u64 reg_id)
  3361. {
  3362. struct res_gid *res;
  3363. int err;
  3364. res = kzalloc(sizeof *res, GFP_KERNEL);
  3365. if (!res)
  3366. return -ENOMEM;
  3367. spin_lock_irq(&rqp->mcg_spl);
  3368. if (find_gid(dev, slave, rqp, gid)) {
  3369. kfree(res);
  3370. err = -EEXIST;
  3371. } else {
  3372. memcpy(res->gid, gid, 16);
  3373. res->prot = prot;
  3374. res->steer = steer;
  3375. res->reg_id = reg_id;
  3376. list_add_tail(&res->list, &rqp->mcg_list);
  3377. err = 0;
  3378. }
  3379. spin_unlock_irq(&rqp->mcg_spl);
  3380. return err;
  3381. }
  3382. static int rem_mcg_res(struct mlx4_dev *dev, int slave, struct res_qp *rqp,
  3383. u8 *gid, enum mlx4_protocol prot,
  3384. enum mlx4_steer_type steer, u64 *reg_id)
  3385. {
  3386. struct res_gid *res;
  3387. int err;
  3388. spin_lock_irq(&rqp->mcg_spl);
  3389. res = find_gid(dev, slave, rqp, gid);
  3390. if (!res || res->prot != prot || res->steer != steer)
  3391. err = -EINVAL;
  3392. else {
  3393. *reg_id = res->reg_id;
  3394. list_del(&res->list);
  3395. kfree(res);
  3396. err = 0;
  3397. }
  3398. spin_unlock_irq(&rqp->mcg_spl);
  3399. return err;
  3400. }
  3401. static int qp_attach(struct mlx4_dev *dev, int slave, struct mlx4_qp *qp,
  3402. u8 gid[16], int block_loopback, enum mlx4_protocol prot,
  3403. enum mlx4_steer_type type, u64 *reg_id)
  3404. {
  3405. switch (dev->caps.steering_mode) {
  3406. case MLX4_STEERING_MODE_DEVICE_MANAGED: {
  3407. int port = mlx4_slave_convert_port(dev, slave, gid[5]);
  3408. if (port < 0)
  3409. return port;
  3410. return mlx4_trans_to_dmfs_attach(dev, qp, gid, port,
  3411. block_loopback, prot,
  3412. reg_id);
  3413. }
  3414. case MLX4_STEERING_MODE_B0:
  3415. if (prot == MLX4_PROT_ETH) {
  3416. int port = mlx4_slave_convert_port(dev, slave, gid[5]);
  3417. if (port < 0)
  3418. return port;
  3419. gid[5] = port;
  3420. }
  3421. return mlx4_qp_attach_common(dev, qp, gid,
  3422. block_loopback, prot, type);
  3423. default:
  3424. return -EINVAL;
  3425. }
  3426. }
  3427. static int qp_detach(struct mlx4_dev *dev, struct mlx4_qp *qp,
  3428. u8 gid[16], enum mlx4_protocol prot,
  3429. enum mlx4_steer_type type, u64 reg_id)
  3430. {
  3431. switch (dev->caps.steering_mode) {
  3432. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  3433. return mlx4_flow_detach(dev, reg_id);
  3434. case MLX4_STEERING_MODE_B0:
  3435. return mlx4_qp_detach_common(dev, qp, gid, prot, type);
  3436. default:
  3437. return -EINVAL;
  3438. }
  3439. }
  3440. static int mlx4_adjust_port(struct mlx4_dev *dev, int slave,
  3441. u8 *gid, enum mlx4_protocol prot)
  3442. {
  3443. int real_port;
  3444. if (prot != MLX4_PROT_ETH)
  3445. return 0;
  3446. if (dev->caps.steering_mode == MLX4_STEERING_MODE_B0 ||
  3447. dev->caps.steering_mode == MLX4_STEERING_MODE_DEVICE_MANAGED) {
  3448. real_port = mlx4_slave_convert_port(dev, slave, gid[5]);
  3449. if (real_port < 0)
  3450. return -EINVAL;
  3451. gid[5] = real_port;
  3452. }
  3453. return 0;
  3454. }
  3455. int mlx4_QP_ATTACH_wrapper(struct mlx4_dev *dev, int slave,
  3456. struct mlx4_vhcr *vhcr,
  3457. struct mlx4_cmd_mailbox *inbox,
  3458. struct mlx4_cmd_mailbox *outbox,
  3459. struct mlx4_cmd_info *cmd)
  3460. {
  3461. struct mlx4_qp qp; /* dummy for calling attach/detach */
  3462. u8 *gid = inbox->buf;
  3463. enum mlx4_protocol prot = (vhcr->in_modifier >> 28) & 0x7;
  3464. int err;
  3465. int qpn;
  3466. struct res_qp *rqp;
  3467. u64 reg_id = 0;
  3468. int attach = vhcr->op_modifier;
  3469. int block_loopback = vhcr->in_modifier >> 31;
  3470. u8 steer_type_mask = 2;
  3471. enum mlx4_steer_type type = (gid[7] & steer_type_mask) >> 1;
  3472. qpn = vhcr->in_modifier & 0xffffff;
  3473. err = get_res(dev, slave, qpn, RES_QP, &rqp);
  3474. if (err)
  3475. return err;
  3476. qp.qpn = qpn;
  3477. if (attach) {
  3478. err = qp_attach(dev, slave, &qp, gid, block_loopback, prot,
  3479. type, &reg_id);
  3480. if (err) {
  3481. pr_err("Fail to attach rule to qp 0x%x\n", qpn);
  3482. goto ex_put;
  3483. }
  3484. err = add_mcg_res(dev, slave, rqp, gid, prot, type, reg_id);
  3485. if (err)
  3486. goto ex_detach;
  3487. } else {
  3488. err = mlx4_adjust_port(dev, slave, gid, prot);
  3489. if (err)
  3490. goto ex_put;
  3491. err = rem_mcg_res(dev, slave, rqp, gid, prot, type, &reg_id);
  3492. if (err)
  3493. goto ex_put;
  3494. err = qp_detach(dev, &qp, gid, prot, type, reg_id);
  3495. if (err)
  3496. pr_err("Fail to detach rule from qp 0x%x reg_id = 0x%llx\n",
  3497. qpn, reg_id);
  3498. }
  3499. put_res(dev, slave, qpn, RES_QP);
  3500. return err;
  3501. ex_detach:
  3502. qp_detach(dev, &qp, gid, prot, type, reg_id);
  3503. ex_put:
  3504. put_res(dev, slave, qpn, RES_QP);
  3505. return err;
  3506. }
  3507. /*
  3508. * MAC validation for Flow Steering rules.
  3509. * VF can attach rules only with a mac address which is assigned to it.
  3510. */
  3511. static int validate_eth_header_mac(int slave, struct _rule_hw *eth_header,
  3512. struct list_head *rlist)
  3513. {
  3514. struct mac_res *res, *tmp;
  3515. __be64 be_mac;
  3516. /* make sure it isn't multicast or broadcast mac*/
  3517. if (!is_multicast_ether_addr(eth_header->eth.dst_mac) &&
  3518. !is_broadcast_ether_addr(eth_header->eth.dst_mac)) {
  3519. list_for_each_entry_safe(res, tmp, rlist, list) {
  3520. be_mac = cpu_to_be64(res->mac << 16);
  3521. if (ether_addr_equal((u8 *)&be_mac, eth_header->eth.dst_mac))
  3522. return 0;
  3523. }
  3524. pr_err("MAC %pM doesn't belong to VF %d, Steering rule rejected\n",
  3525. eth_header->eth.dst_mac, slave);
  3526. return -EINVAL;
  3527. }
  3528. return 0;
  3529. }
  3530. static void handle_eth_header_mcast_prio(struct mlx4_net_trans_rule_hw_ctrl *ctrl,
  3531. struct _rule_hw *eth_header)
  3532. {
  3533. if (is_multicast_ether_addr(eth_header->eth.dst_mac) ||
  3534. is_broadcast_ether_addr(eth_header->eth.dst_mac)) {
  3535. struct mlx4_net_trans_rule_hw_eth *eth =
  3536. (struct mlx4_net_trans_rule_hw_eth *)eth_header;
  3537. struct _rule_hw *next_rule = (struct _rule_hw *)(eth + 1);
  3538. bool last_rule = next_rule->size == 0 && next_rule->id == 0 &&
  3539. next_rule->rsvd == 0;
  3540. if (last_rule)
  3541. ctrl->prio = cpu_to_be16(MLX4_DOMAIN_NIC);
  3542. }
  3543. }
  3544. /*
  3545. * In case of missing eth header, append eth header with a MAC address
  3546. * assigned to the VF.
  3547. */
  3548. static int add_eth_header(struct mlx4_dev *dev, int slave,
  3549. struct mlx4_cmd_mailbox *inbox,
  3550. struct list_head *rlist, int header_id)
  3551. {
  3552. struct mac_res *res, *tmp;
  3553. u8 port;
  3554. struct mlx4_net_trans_rule_hw_ctrl *ctrl;
  3555. struct mlx4_net_trans_rule_hw_eth *eth_header;
  3556. struct mlx4_net_trans_rule_hw_ipv4 *ip_header;
  3557. struct mlx4_net_trans_rule_hw_tcp_udp *l4_header;
  3558. __be64 be_mac = 0;
  3559. __be64 mac_msk = cpu_to_be64(MLX4_MAC_MASK << 16);
  3560. ctrl = (struct mlx4_net_trans_rule_hw_ctrl *)inbox->buf;
  3561. port = ctrl->port;
  3562. eth_header = (struct mlx4_net_trans_rule_hw_eth *)(ctrl + 1);
  3563. /* Clear a space in the inbox for eth header */
  3564. switch (header_id) {
  3565. case MLX4_NET_TRANS_RULE_ID_IPV4:
  3566. ip_header =
  3567. (struct mlx4_net_trans_rule_hw_ipv4 *)(eth_header + 1);
  3568. memmove(ip_header, eth_header,
  3569. sizeof(*ip_header) + sizeof(*l4_header));
  3570. break;
  3571. case MLX4_NET_TRANS_RULE_ID_TCP:
  3572. case MLX4_NET_TRANS_RULE_ID_UDP:
  3573. l4_header = (struct mlx4_net_trans_rule_hw_tcp_udp *)
  3574. (eth_header + 1);
  3575. memmove(l4_header, eth_header, sizeof(*l4_header));
  3576. break;
  3577. default:
  3578. return -EINVAL;
  3579. }
  3580. list_for_each_entry_safe(res, tmp, rlist, list) {
  3581. if (port == res->port) {
  3582. be_mac = cpu_to_be64(res->mac << 16);
  3583. break;
  3584. }
  3585. }
  3586. if (!be_mac) {
  3587. pr_err("Failed adding eth header to FS rule, Can't find matching MAC for port %d\n",
  3588. port);
  3589. return -EINVAL;
  3590. }
  3591. memset(eth_header, 0, sizeof(*eth_header));
  3592. eth_header->size = sizeof(*eth_header) >> 2;
  3593. eth_header->id = cpu_to_be16(__sw_id_hw[MLX4_NET_TRANS_RULE_ID_ETH]);
  3594. memcpy(eth_header->dst_mac, &be_mac, ETH_ALEN);
  3595. memcpy(eth_header->dst_mac_msk, &mac_msk, ETH_ALEN);
  3596. return 0;
  3597. }
  3598. #define MLX4_UPD_QP_PATH_MASK_SUPPORTED ( \
  3599. 1ULL << MLX4_UPD_QP_PATH_MASK_MAC_INDEX |\
  3600. 1ULL << MLX4_UPD_QP_PATH_MASK_ETH_SRC_CHECK_MC_LB)
  3601. int mlx4_UPDATE_QP_wrapper(struct mlx4_dev *dev, int slave,
  3602. struct mlx4_vhcr *vhcr,
  3603. struct mlx4_cmd_mailbox *inbox,
  3604. struct mlx4_cmd_mailbox *outbox,
  3605. struct mlx4_cmd_info *cmd_info)
  3606. {
  3607. int err;
  3608. u32 qpn = vhcr->in_modifier & 0xffffff;
  3609. struct res_qp *rqp;
  3610. u64 mac;
  3611. unsigned port;
  3612. u64 pri_addr_path_mask;
  3613. struct mlx4_update_qp_context *cmd;
  3614. int smac_index;
  3615. cmd = (struct mlx4_update_qp_context *)inbox->buf;
  3616. pri_addr_path_mask = be64_to_cpu(cmd->primary_addr_path_mask);
  3617. if (cmd->qp_mask || cmd->secondary_addr_path_mask ||
  3618. (pri_addr_path_mask & ~MLX4_UPD_QP_PATH_MASK_SUPPORTED))
  3619. return -EPERM;
  3620. if ((pri_addr_path_mask &
  3621. (1ULL << MLX4_UPD_QP_PATH_MASK_ETH_SRC_CHECK_MC_LB)) &&
  3622. !(dev->caps.flags2 &
  3623. MLX4_DEV_CAP_FLAG2_UPDATE_QP_SRC_CHECK_LB)) {
  3624. mlx4_warn(dev,
  3625. "Src check LB for slave %d isn't supported\n",
  3626. slave);
  3627. return -ENOTSUPP;
  3628. }
  3629. /* Just change the smac for the QP */
  3630. err = get_res(dev, slave, qpn, RES_QP, &rqp);
  3631. if (err) {
  3632. mlx4_err(dev, "Updating qpn 0x%x for slave %d rejected\n", qpn, slave);
  3633. return err;
  3634. }
  3635. port = (rqp->sched_queue >> 6 & 1) + 1;
  3636. if (pri_addr_path_mask & (1ULL << MLX4_UPD_QP_PATH_MASK_MAC_INDEX)) {
  3637. smac_index = cmd->qp_context.pri_path.grh_mylmc;
  3638. err = mac_find_smac_ix_in_slave(dev, slave, port,
  3639. smac_index, &mac);
  3640. if (err) {
  3641. mlx4_err(dev, "Failed to update qpn 0x%x, MAC is invalid. smac_ix: %d\n",
  3642. qpn, smac_index);
  3643. goto err_mac;
  3644. }
  3645. }
  3646. err = mlx4_cmd(dev, inbox->dma,
  3647. vhcr->in_modifier, 0,
  3648. MLX4_CMD_UPDATE_QP, MLX4_CMD_TIME_CLASS_A,
  3649. MLX4_CMD_NATIVE);
  3650. if (err) {
  3651. mlx4_err(dev, "Failed to update qpn on qpn 0x%x, command failed\n", qpn);
  3652. goto err_mac;
  3653. }
  3654. err_mac:
  3655. put_res(dev, slave, qpn, RES_QP);
  3656. return err;
  3657. }
  3658. int mlx4_QP_FLOW_STEERING_ATTACH_wrapper(struct mlx4_dev *dev, int slave,
  3659. struct mlx4_vhcr *vhcr,
  3660. struct mlx4_cmd_mailbox *inbox,
  3661. struct mlx4_cmd_mailbox *outbox,
  3662. struct mlx4_cmd_info *cmd)
  3663. {
  3664. struct mlx4_priv *priv = mlx4_priv(dev);
  3665. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  3666. struct list_head *rlist = &tracker->slave_list[slave].res_list[RES_MAC];
  3667. int err;
  3668. int qpn;
  3669. struct res_qp *rqp;
  3670. struct mlx4_net_trans_rule_hw_ctrl *ctrl;
  3671. struct _rule_hw *rule_header;
  3672. int header_id;
  3673. if (dev->caps.steering_mode !=
  3674. MLX4_STEERING_MODE_DEVICE_MANAGED)
  3675. return -EOPNOTSUPP;
  3676. ctrl = (struct mlx4_net_trans_rule_hw_ctrl *)inbox->buf;
  3677. err = mlx4_slave_convert_port(dev, slave, ctrl->port);
  3678. if (err <= 0)
  3679. return -EINVAL;
  3680. ctrl->port = err;
  3681. qpn = be32_to_cpu(ctrl->qpn) & 0xffffff;
  3682. err = get_res(dev, slave, qpn, RES_QP, &rqp);
  3683. if (err) {
  3684. pr_err("Steering rule with qpn 0x%x rejected\n", qpn);
  3685. return err;
  3686. }
  3687. rule_header = (struct _rule_hw *)(ctrl + 1);
  3688. header_id = map_hw_to_sw_id(be16_to_cpu(rule_header->id));
  3689. if (header_id == MLX4_NET_TRANS_RULE_ID_ETH)
  3690. handle_eth_header_mcast_prio(ctrl, rule_header);
  3691. if (slave == dev->caps.function)
  3692. goto execute;
  3693. switch (header_id) {
  3694. case MLX4_NET_TRANS_RULE_ID_ETH:
  3695. if (validate_eth_header_mac(slave, rule_header, rlist)) {
  3696. err = -EINVAL;
  3697. goto err_put;
  3698. }
  3699. break;
  3700. case MLX4_NET_TRANS_RULE_ID_IB:
  3701. break;
  3702. case MLX4_NET_TRANS_RULE_ID_IPV4:
  3703. case MLX4_NET_TRANS_RULE_ID_TCP:
  3704. case MLX4_NET_TRANS_RULE_ID_UDP:
  3705. pr_warn("Can't attach FS rule without L2 headers, adding L2 header\n");
  3706. if (add_eth_header(dev, slave, inbox, rlist, header_id)) {
  3707. err = -EINVAL;
  3708. goto err_put;
  3709. }
  3710. vhcr->in_modifier +=
  3711. sizeof(struct mlx4_net_trans_rule_hw_eth) >> 2;
  3712. break;
  3713. default:
  3714. pr_err("Corrupted mailbox\n");
  3715. err = -EINVAL;
  3716. goto err_put;
  3717. }
  3718. execute:
  3719. err = mlx4_cmd_imm(dev, inbox->dma, &vhcr->out_param,
  3720. vhcr->in_modifier, 0,
  3721. MLX4_QP_FLOW_STEERING_ATTACH, MLX4_CMD_TIME_CLASS_A,
  3722. MLX4_CMD_NATIVE);
  3723. if (err)
  3724. goto err_put;
  3725. err = add_res_range(dev, slave, vhcr->out_param, 1, RES_FS_RULE, qpn);
  3726. if (err) {
  3727. mlx4_err(dev, "Fail to add flow steering resources\n");
  3728. /* detach rule*/
  3729. mlx4_cmd(dev, vhcr->out_param, 0, 0,
  3730. MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
  3731. MLX4_CMD_NATIVE);
  3732. goto err_put;
  3733. }
  3734. atomic_inc(&rqp->ref_count);
  3735. err_put:
  3736. put_res(dev, slave, qpn, RES_QP);
  3737. return err;
  3738. }
  3739. int mlx4_QP_FLOW_STEERING_DETACH_wrapper(struct mlx4_dev *dev, int slave,
  3740. struct mlx4_vhcr *vhcr,
  3741. struct mlx4_cmd_mailbox *inbox,
  3742. struct mlx4_cmd_mailbox *outbox,
  3743. struct mlx4_cmd_info *cmd)
  3744. {
  3745. int err;
  3746. struct res_qp *rqp;
  3747. struct res_fs_rule *rrule;
  3748. if (dev->caps.steering_mode !=
  3749. MLX4_STEERING_MODE_DEVICE_MANAGED)
  3750. return -EOPNOTSUPP;
  3751. err = get_res(dev, slave, vhcr->in_param, RES_FS_RULE, &rrule);
  3752. if (err)
  3753. return err;
  3754. /* Release the rule form busy state before removal */
  3755. put_res(dev, slave, vhcr->in_param, RES_FS_RULE);
  3756. err = get_res(dev, slave, rrule->qpn, RES_QP, &rqp);
  3757. if (err)
  3758. return err;
  3759. err = rem_res_range(dev, slave, vhcr->in_param, 1, RES_FS_RULE, 0);
  3760. if (err) {
  3761. mlx4_err(dev, "Fail to remove flow steering resources\n");
  3762. goto out;
  3763. }
  3764. err = mlx4_cmd(dev, vhcr->in_param, 0, 0,
  3765. MLX4_QP_FLOW_STEERING_DETACH, MLX4_CMD_TIME_CLASS_A,
  3766. MLX4_CMD_NATIVE);
  3767. if (!err)
  3768. atomic_dec(&rqp->ref_count);
  3769. out:
  3770. put_res(dev, slave, rrule->qpn, RES_QP);
  3771. return err;
  3772. }
  3773. enum {
  3774. BUSY_MAX_RETRIES = 10
  3775. };
  3776. int mlx4_QUERY_IF_STAT_wrapper(struct mlx4_dev *dev, int slave,
  3777. struct mlx4_vhcr *vhcr,
  3778. struct mlx4_cmd_mailbox *inbox,
  3779. struct mlx4_cmd_mailbox *outbox,
  3780. struct mlx4_cmd_info *cmd)
  3781. {
  3782. int err;
  3783. int index = vhcr->in_modifier & 0xffff;
  3784. err = get_res(dev, slave, index, RES_COUNTER, NULL);
  3785. if (err)
  3786. return err;
  3787. err = mlx4_DMA_wrapper(dev, slave, vhcr, inbox, outbox, cmd);
  3788. put_res(dev, slave, index, RES_COUNTER);
  3789. return err;
  3790. }
  3791. static void detach_qp(struct mlx4_dev *dev, int slave, struct res_qp *rqp)
  3792. {
  3793. struct res_gid *rgid;
  3794. struct res_gid *tmp;
  3795. struct mlx4_qp qp; /* dummy for calling attach/detach */
  3796. list_for_each_entry_safe(rgid, tmp, &rqp->mcg_list, list) {
  3797. switch (dev->caps.steering_mode) {
  3798. case MLX4_STEERING_MODE_DEVICE_MANAGED:
  3799. mlx4_flow_detach(dev, rgid->reg_id);
  3800. break;
  3801. case MLX4_STEERING_MODE_B0:
  3802. qp.qpn = rqp->local_qpn;
  3803. (void) mlx4_qp_detach_common(dev, &qp, rgid->gid,
  3804. rgid->prot, rgid->steer);
  3805. break;
  3806. }
  3807. list_del(&rgid->list);
  3808. kfree(rgid);
  3809. }
  3810. }
  3811. static int _move_all_busy(struct mlx4_dev *dev, int slave,
  3812. enum mlx4_resource type, int print)
  3813. {
  3814. struct mlx4_priv *priv = mlx4_priv(dev);
  3815. struct mlx4_resource_tracker *tracker =
  3816. &priv->mfunc.master.res_tracker;
  3817. struct list_head *rlist = &tracker->slave_list[slave].res_list[type];
  3818. struct res_common *r;
  3819. struct res_common *tmp;
  3820. int busy;
  3821. busy = 0;
  3822. spin_lock_irq(mlx4_tlock(dev));
  3823. list_for_each_entry_safe(r, tmp, rlist, list) {
  3824. if (r->owner == slave) {
  3825. if (!r->removing) {
  3826. if (r->state == RES_ANY_BUSY) {
  3827. if (print)
  3828. mlx4_dbg(dev,
  3829. "%s id 0x%llx is busy\n",
  3830. resource_str(type),
  3831. r->res_id);
  3832. ++busy;
  3833. } else {
  3834. r->from_state = r->state;
  3835. r->state = RES_ANY_BUSY;
  3836. r->removing = 1;
  3837. }
  3838. }
  3839. }
  3840. }
  3841. spin_unlock_irq(mlx4_tlock(dev));
  3842. return busy;
  3843. }
  3844. static int move_all_busy(struct mlx4_dev *dev, int slave,
  3845. enum mlx4_resource type)
  3846. {
  3847. unsigned long begin;
  3848. int busy;
  3849. begin = jiffies;
  3850. do {
  3851. busy = _move_all_busy(dev, slave, type, 0);
  3852. if (time_after(jiffies, begin + 5 * HZ))
  3853. break;
  3854. if (busy)
  3855. cond_resched();
  3856. } while (busy);
  3857. if (busy)
  3858. busy = _move_all_busy(dev, slave, type, 1);
  3859. return busy;
  3860. }
  3861. static void rem_slave_qps(struct mlx4_dev *dev, int slave)
  3862. {
  3863. struct mlx4_priv *priv = mlx4_priv(dev);
  3864. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  3865. struct list_head *qp_list =
  3866. &tracker->slave_list[slave].res_list[RES_QP];
  3867. struct res_qp *qp;
  3868. struct res_qp *tmp;
  3869. int state;
  3870. u64 in_param;
  3871. int qpn;
  3872. int err;
  3873. err = move_all_busy(dev, slave, RES_QP);
  3874. if (err)
  3875. mlx4_warn(dev, "rem_slave_qps: Could not move all qps to busy for slave %d\n",
  3876. slave);
  3877. spin_lock_irq(mlx4_tlock(dev));
  3878. list_for_each_entry_safe(qp, tmp, qp_list, com.list) {
  3879. spin_unlock_irq(mlx4_tlock(dev));
  3880. if (qp->com.owner == slave) {
  3881. qpn = qp->com.res_id;
  3882. detach_qp(dev, slave, qp);
  3883. state = qp->com.from_state;
  3884. while (state != 0) {
  3885. switch (state) {
  3886. case RES_QP_RESERVED:
  3887. spin_lock_irq(mlx4_tlock(dev));
  3888. rb_erase(&qp->com.node,
  3889. &tracker->res_tree[RES_QP]);
  3890. list_del(&qp->com.list);
  3891. spin_unlock_irq(mlx4_tlock(dev));
  3892. if (!valid_reserved(dev, slave, qpn)) {
  3893. __mlx4_qp_release_range(dev, qpn, 1);
  3894. mlx4_release_resource(dev, slave,
  3895. RES_QP, 1, 0);
  3896. }
  3897. kfree(qp);
  3898. state = 0;
  3899. break;
  3900. case RES_QP_MAPPED:
  3901. if (!valid_reserved(dev, slave, qpn))
  3902. __mlx4_qp_free_icm(dev, qpn);
  3903. state = RES_QP_RESERVED;
  3904. break;
  3905. case RES_QP_HW:
  3906. in_param = slave;
  3907. err = mlx4_cmd(dev, in_param,
  3908. qp->local_qpn, 2,
  3909. MLX4_CMD_2RST_QP,
  3910. MLX4_CMD_TIME_CLASS_A,
  3911. MLX4_CMD_NATIVE);
  3912. if (err)
  3913. mlx4_dbg(dev, "rem_slave_qps: failed to move slave %d qpn %d to reset\n",
  3914. slave, qp->local_qpn);
  3915. atomic_dec(&qp->rcq->ref_count);
  3916. atomic_dec(&qp->scq->ref_count);
  3917. atomic_dec(&qp->mtt->ref_count);
  3918. if (qp->srq)
  3919. atomic_dec(&qp->srq->ref_count);
  3920. state = RES_QP_MAPPED;
  3921. break;
  3922. default:
  3923. state = 0;
  3924. }
  3925. }
  3926. }
  3927. spin_lock_irq(mlx4_tlock(dev));
  3928. }
  3929. spin_unlock_irq(mlx4_tlock(dev));
  3930. }
  3931. static void rem_slave_srqs(struct mlx4_dev *dev, int slave)
  3932. {
  3933. struct mlx4_priv *priv = mlx4_priv(dev);
  3934. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  3935. struct list_head *srq_list =
  3936. &tracker->slave_list[slave].res_list[RES_SRQ];
  3937. struct res_srq *srq;
  3938. struct res_srq *tmp;
  3939. int state;
  3940. u64 in_param;
  3941. LIST_HEAD(tlist);
  3942. int srqn;
  3943. int err;
  3944. err = move_all_busy(dev, slave, RES_SRQ);
  3945. if (err)
  3946. mlx4_warn(dev, "rem_slave_srqs: Could not move all srqs - too busy for slave %d\n",
  3947. slave);
  3948. spin_lock_irq(mlx4_tlock(dev));
  3949. list_for_each_entry_safe(srq, tmp, srq_list, com.list) {
  3950. spin_unlock_irq(mlx4_tlock(dev));
  3951. if (srq->com.owner == slave) {
  3952. srqn = srq->com.res_id;
  3953. state = srq->com.from_state;
  3954. while (state != 0) {
  3955. switch (state) {
  3956. case RES_SRQ_ALLOCATED:
  3957. __mlx4_srq_free_icm(dev, srqn);
  3958. spin_lock_irq(mlx4_tlock(dev));
  3959. rb_erase(&srq->com.node,
  3960. &tracker->res_tree[RES_SRQ]);
  3961. list_del(&srq->com.list);
  3962. spin_unlock_irq(mlx4_tlock(dev));
  3963. mlx4_release_resource(dev, slave,
  3964. RES_SRQ, 1, 0);
  3965. kfree(srq);
  3966. state = 0;
  3967. break;
  3968. case RES_SRQ_HW:
  3969. in_param = slave;
  3970. err = mlx4_cmd(dev, in_param, srqn, 1,
  3971. MLX4_CMD_HW2SW_SRQ,
  3972. MLX4_CMD_TIME_CLASS_A,
  3973. MLX4_CMD_NATIVE);
  3974. if (err)
  3975. mlx4_dbg(dev, "rem_slave_srqs: failed to move slave %d srq %d to SW ownership\n",
  3976. slave, srqn);
  3977. atomic_dec(&srq->mtt->ref_count);
  3978. if (srq->cq)
  3979. atomic_dec(&srq->cq->ref_count);
  3980. state = RES_SRQ_ALLOCATED;
  3981. break;
  3982. default:
  3983. state = 0;
  3984. }
  3985. }
  3986. }
  3987. spin_lock_irq(mlx4_tlock(dev));
  3988. }
  3989. spin_unlock_irq(mlx4_tlock(dev));
  3990. }
  3991. static void rem_slave_cqs(struct mlx4_dev *dev, int slave)
  3992. {
  3993. struct mlx4_priv *priv = mlx4_priv(dev);
  3994. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  3995. struct list_head *cq_list =
  3996. &tracker->slave_list[slave].res_list[RES_CQ];
  3997. struct res_cq *cq;
  3998. struct res_cq *tmp;
  3999. int state;
  4000. u64 in_param;
  4001. LIST_HEAD(tlist);
  4002. int cqn;
  4003. int err;
  4004. err = move_all_busy(dev, slave, RES_CQ);
  4005. if (err)
  4006. mlx4_warn(dev, "rem_slave_cqs: Could not move all cqs - too busy for slave %d\n",
  4007. slave);
  4008. spin_lock_irq(mlx4_tlock(dev));
  4009. list_for_each_entry_safe(cq, tmp, cq_list, com.list) {
  4010. spin_unlock_irq(mlx4_tlock(dev));
  4011. if (cq->com.owner == slave && !atomic_read(&cq->ref_count)) {
  4012. cqn = cq->com.res_id;
  4013. state = cq->com.from_state;
  4014. while (state != 0) {
  4015. switch (state) {
  4016. case RES_CQ_ALLOCATED:
  4017. __mlx4_cq_free_icm(dev, cqn);
  4018. spin_lock_irq(mlx4_tlock(dev));
  4019. rb_erase(&cq->com.node,
  4020. &tracker->res_tree[RES_CQ]);
  4021. list_del(&cq->com.list);
  4022. spin_unlock_irq(mlx4_tlock(dev));
  4023. mlx4_release_resource(dev, slave,
  4024. RES_CQ, 1, 0);
  4025. kfree(cq);
  4026. state = 0;
  4027. break;
  4028. case RES_CQ_HW:
  4029. in_param = slave;
  4030. err = mlx4_cmd(dev, in_param, cqn, 1,
  4031. MLX4_CMD_HW2SW_CQ,
  4032. MLX4_CMD_TIME_CLASS_A,
  4033. MLX4_CMD_NATIVE);
  4034. if (err)
  4035. mlx4_dbg(dev, "rem_slave_cqs: failed to move slave %d cq %d to SW ownership\n",
  4036. slave, cqn);
  4037. atomic_dec(&cq->mtt->ref_count);
  4038. state = RES_CQ_ALLOCATED;
  4039. break;
  4040. default:
  4041. state = 0;
  4042. }
  4043. }
  4044. }
  4045. spin_lock_irq(mlx4_tlock(dev));
  4046. }
  4047. spin_unlock_irq(mlx4_tlock(dev));
  4048. }
  4049. static void rem_slave_mrs(struct mlx4_dev *dev, int slave)
  4050. {
  4051. struct mlx4_priv *priv = mlx4_priv(dev);
  4052. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  4053. struct list_head *mpt_list =
  4054. &tracker->slave_list[slave].res_list[RES_MPT];
  4055. struct res_mpt *mpt;
  4056. struct res_mpt *tmp;
  4057. int state;
  4058. u64 in_param;
  4059. LIST_HEAD(tlist);
  4060. int mptn;
  4061. int err;
  4062. err = move_all_busy(dev, slave, RES_MPT);
  4063. if (err)
  4064. mlx4_warn(dev, "rem_slave_mrs: Could not move all mpts - too busy for slave %d\n",
  4065. slave);
  4066. spin_lock_irq(mlx4_tlock(dev));
  4067. list_for_each_entry_safe(mpt, tmp, mpt_list, com.list) {
  4068. spin_unlock_irq(mlx4_tlock(dev));
  4069. if (mpt->com.owner == slave) {
  4070. mptn = mpt->com.res_id;
  4071. state = mpt->com.from_state;
  4072. while (state != 0) {
  4073. switch (state) {
  4074. case RES_MPT_RESERVED:
  4075. __mlx4_mpt_release(dev, mpt->key);
  4076. spin_lock_irq(mlx4_tlock(dev));
  4077. rb_erase(&mpt->com.node,
  4078. &tracker->res_tree[RES_MPT]);
  4079. list_del(&mpt->com.list);
  4080. spin_unlock_irq(mlx4_tlock(dev));
  4081. mlx4_release_resource(dev, slave,
  4082. RES_MPT, 1, 0);
  4083. kfree(mpt);
  4084. state = 0;
  4085. break;
  4086. case RES_MPT_MAPPED:
  4087. __mlx4_mpt_free_icm(dev, mpt->key);
  4088. state = RES_MPT_RESERVED;
  4089. break;
  4090. case RES_MPT_HW:
  4091. in_param = slave;
  4092. err = mlx4_cmd(dev, in_param, mptn, 0,
  4093. MLX4_CMD_HW2SW_MPT,
  4094. MLX4_CMD_TIME_CLASS_A,
  4095. MLX4_CMD_NATIVE);
  4096. if (err)
  4097. mlx4_dbg(dev, "rem_slave_mrs: failed to move slave %d mpt %d to SW ownership\n",
  4098. slave, mptn);
  4099. if (mpt->mtt)
  4100. atomic_dec(&mpt->mtt->ref_count);
  4101. state = RES_MPT_MAPPED;
  4102. break;
  4103. default:
  4104. state = 0;
  4105. }
  4106. }
  4107. }
  4108. spin_lock_irq(mlx4_tlock(dev));
  4109. }
  4110. spin_unlock_irq(mlx4_tlock(dev));
  4111. }
  4112. static void rem_slave_mtts(struct mlx4_dev *dev, int slave)
  4113. {
  4114. struct mlx4_priv *priv = mlx4_priv(dev);
  4115. struct mlx4_resource_tracker *tracker =
  4116. &priv->mfunc.master.res_tracker;
  4117. struct list_head *mtt_list =
  4118. &tracker->slave_list[slave].res_list[RES_MTT];
  4119. struct res_mtt *mtt;
  4120. struct res_mtt *tmp;
  4121. int state;
  4122. LIST_HEAD(tlist);
  4123. int base;
  4124. int err;
  4125. err = move_all_busy(dev, slave, RES_MTT);
  4126. if (err)
  4127. mlx4_warn(dev, "rem_slave_mtts: Could not move all mtts - too busy for slave %d\n",
  4128. slave);
  4129. spin_lock_irq(mlx4_tlock(dev));
  4130. list_for_each_entry_safe(mtt, tmp, mtt_list, com.list) {
  4131. spin_unlock_irq(mlx4_tlock(dev));
  4132. if (mtt->com.owner == slave) {
  4133. base = mtt->com.res_id;
  4134. state = mtt->com.from_state;
  4135. while (state != 0) {
  4136. switch (state) {
  4137. case RES_MTT_ALLOCATED:
  4138. __mlx4_free_mtt_range(dev, base,
  4139. mtt->order);
  4140. spin_lock_irq(mlx4_tlock(dev));
  4141. rb_erase(&mtt->com.node,
  4142. &tracker->res_tree[RES_MTT]);
  4143. list_del(&mtt->com.list);
  4144. spin_unlock_irq(mlx4_tlock(dev));
  4145. mlx4_release_resource(dev, slave, RES_MTT,
  4146. 1 << mtt->order, 0);
  4147. kfree(mtt);
  4148. state = 0;
  4149. break;
  4150. default:
  4151. state = 0;
  4152. }
  4153. }
  4154. }
  4155. spin_lock_irq(mlx4_tlock(dev));
  4156. }
  4157. spin_unlock_irq(mlx4_tlock(dev));
  4158. }
  4159. static void rem_slave_fs_rule(struct mlx4_dev *dev, int slave)
  4160. {
  4161. struct mlx4_priv *priv = mlx4_priv(dev);
  4162. struct mlx4_resource_tracker *tracker =
  4163. &priv->mfunc.master.res_tracker;
  4164. struct list_head *fs_rule_list =
  4165. &tracker->slave_list[slave].res_list[RES_FS_RULE];
  4166. struct res_fs_rule *fs_rule;
  4167. struct res_fs_rule *tmp;
  4168. int state;
  4169. u64 base;
  4170. int err;
  4171. err = move_all_busy(dev, slave, RES_FS_RULE);
  4172. if (err)
  4173. mlx4_warn(dev, "rem_slave_fs_rule: Could not move all mtts to busy for slave %d\n",
  4174. slave);
  4175. spin_lock_irq(mlx4_tlock(dev));
  4176. list_for_each_entry_safe(fs_rule, tmp, fs_rule_list, com.list) {
  4177. spin_unlock_irq(mlx4_tlock(dev));
  4178. if (fs_rule->com.owner == slave) {
  4179. base = fs_rule->com.res_id;
  4180. state = fs_rule->com.from_state;
  4181. while (state != 0) {
  4182. switch (state) {
  4183. case RES_FS_RULE_ALLOCATED:
  4184. /* detach rule */
  4185. err = mlx4_cmd(dev, base, 0, 0,
  4186. MLX4_QP_FLOW_STEERING_DETACH,
  4187. MLX4_CMD_TIME_CLASS_A,
  4188. MLX4_CMD_NATIVE);
  4189. spin_lock_irq(mlx4_tlock(dev));
  4190. rb_erase(&fs_rule->com.node,
  4191. &tracker->res_tree[RES_FS_RULE]);
  4192. list_del(&fs_rule->com.list);
  4193. spin_unlock_irq(mlx4_tlock(dev));
  4194. kfree(fs_rule);
  4195. state = 0;
  4196. break;
  4197. default:
  4198. state = 0;
  4199. }
  4200. }
  4201. }
  4202. spin_lock_irq(mlx4_tlock(dev));
  4203. }
  4204. spin_unlock_irq(mlx4_tlock(dev));
  4205. }
  4206. static void rem_slave_eqs(struct mlx4_dev *dev, int slave)
  4207. {
  4208. struct mlx4_priv *priv = mlx4_priv(dev);
  4209. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  4210. struct list_head *eq_list =
  4211. &tracker->slave_list[slave].res_list[RES_EQ];
  4212. struct res_eq *eq;
  4213. struct res_eq *tmp;
  4214. int err;
  4215. int state;
  4216. LIST_HEAD(tlist);
  4217. int eqn;
  4218. err = move_all_busy(dev, slave, RES_EQ);
  4219. if (err)
  4220. mlx4_warn(dev, "rem_slave_eqs: Could not move all eqs - too busy for slave %d\n",
  4221. slave);
  4222. spin_lock_irq(mlx4_tlock(dev));
  4223. list_for_each_entry_safe(eq, tmp, eq_list, com.list) {
  4224. spin_unlock_irq(mlx4_tlock(dev));
  4225. if (eq->com.owner == slave) {
  4226. eqn = eq->com.res_id;
  4227. state = eq->com.from_state;
  4228. while (state != 0) {
  4229. switch (state) {
  4230. case RES_EQ_RESERVED:
  4231. spin_lock_irq(mlx4_tlock(dev));
  4232. rb_erase(&eq->com.node,
  4233. &tracker->res_tree[RES_EQ]);
  4234. list_del(&eq->com.list);
  4235. spin_unlock_irq(mlx4_tlock(dev));
  4236. kfree(eq);
  4237. state = 0;
  4238. break;
  4239. case RES_EQ_HW:
  4240. err = mlx4_cmd(dev, slave, eqn & 0x3ff,
  4241. 1, MLX4_CMD_HW2SW_EQ,
  4242. MLX4_CMD_TIME_CLASS_A,
  4243. MLX4_CMD_NATIVE);
  4244. if (err)
  4245. mlx4_dbg(dev, "rem_slave_eqs: failed to move slave %d eqs %d to SW ownership\n",
  4246. slave, eqn & 0x3ff);
  4247. atomic_dec(&eq->mtt->ref_count);
  4248. state = RES_EQ_RESERVED;
  4249. break;
  4250. default:
  4251. state = 0;
  4252. }
  4253. }
  4254. }
  4255. spin_lock_irq(mlx4_tlock(dev));
  4256. }
  4257. spin_unlock_irq(mlx4_tlock(dev));
  4258. }
  4259. static void rem_slave_counters(struct mlx4_dev *dev, int slave)
  4260. {
  4261. struct mlx4_priv *priv = mlx4_priv(dev);
  4262. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  4263. struct list_head *counter_list =
  4264. &tracker->slave_list[slave].res_list[RES_COUNTER];
  4265. struct res_counter *counter;
  4266. struct res_counter *tmp;
  4267. int err;
  4268. int *counters_arr = NULL;
  4269. int i, j;
  4270. err = move_all_busy(dev, slave, RES_COUNTER);
  4271. if (err)
  4272. mlx4_warn(dev, "rem_slave_counters: Could not move all counters - too busy for slave %d\n",
  4273. slave);
  4274. counters_arr = kmalloc_array(dev->caps.max_counters,
  4275. sizeof(*counters_arr), GFP_KERNEL);
  4276. if (!counters_arr)
  4277. return;
  4278. do {
  4279. i = 0;
  4280. j = 0;
  4281. spin_lock_irq(mlx4_tlock(dev));
  4282. list_for_each_entry_safe(counter, tmp, counter_list, com.list) {
  4283. if (counter->com.owner == slave) {
  4284. counters_arr[i++] = counter->com.res_id;
  4285. rb_erase(&counter->com.node,
  4286. &tracker->res_tree[RES_COUNTER]);
  4287. list_del(&counter->com.list);
  4288. kfree(counter);
  4289. }
  4290. }
  4291. spin_unlock_irq(mlx4_tlock(dev));
  4292. while (j < i) {
  4293. __mlx4_counter_free(dev, counters_arr[j++]);
  4294. mlx4_release_resource(dev, slave, RES_COUNTER, 1, 0);
  4295. }
  4296. } while (i);
  4297. kfree(counters_arr);
  4298. }
  4299. static void rem_slave_xrcdns(struct mlx4_dev *dev, int slave)
  4300. {
  4301. struct mlx4_priv *priv = mlx4_priv(dev);
  4302. struct mlx4_resource_tracker *tracker = &priv->mfunc.master.res_tracker;
  4303. struct list_head *xrcdn_list =
  4304. &tracker->slave_list[slave].res_list[RES_XRCD];
  4305. struct res_xrcdn *xrcd;
  4306. struct res_xrcdn *tmp;
  4307. int err;
  4308. int xrcdn;
  4309. err = move_all_busy(dev, slave, RES_XRCD);
  4310. if (err)
  4311. mlx4_warn(dev, "rem_slave_xrcdns: Could not move all xrcdns - too busy for slave %d\n",
  4312. slave);
  4313. spin_lock_irq(mlx4_tlock(dev));
  4314. list_for_each_entry_safe(xrcd, tmp, xrcdn_list, com.list) {
  4315. if (xrcd->com.owner == slave) {
  4316. xrcdn = xrcd->com.res_id;
  4317. rb_erase(&xrcd->com.node, &tracker->res_tree[RES_XRCD]);
  4318. list_del(&xrcd->com.list);
  4319. kfree(xrcd);
  4320. __mlx4_xrcd_free(dev, xrcdn);
  4321. }
  4322. }
  4323. spin_unlock_irq(mlx4_tlock(dev));
  4324. }
  4325. void mlx4_delete_all_resources_for_slave(struct mlx4_dev *dev, int slave)
  4326. {
  4327. struct mlx4_priv *priv = mlx4_priv(dev);
  4328. mlx4_reset_roce_gids(dev, slave);
  4329. mutex_lock(&priv->mfunc.master.res_tracker.slave_list[slave].mutex);
  4330. rem_slave_vlans(dev, slave);
  4331. rem_slave_macs(dev, slave);
  4332. rem_slave_fs_rule(dev, slave);
  4333. rem_slave_qps(dev, slave);
  4334. rem_slave_srqs(dev, slave);
  4335. rem_slave_cqs(dev, slave);
  4336. rem_slave_mrs(dev, slave);
  4337. rem_slave_eqs(dev, slave);
  4338. rem_slave_mtts(dev, slave);
  4339. rem_slave_counters(dev, slave);
  4340. rem_slave_xrcdns(dev, slave);
  4341. mutex_unlock(&priv->mfunc.master.res_tracker.slave_list[slave].mutex);
  4342. }
  4343. static void update_qos_vpp(struct mlx4_update_qp_context *ctx,
  4344. struct mlx4_vf_immed_vlan_work *work)
  4345. {
  4346. ctx->qp_mask |= cpu_to_be64(1ULL << MLX4_UPD_QP_MASK_QOS_VPP);
  4347. ctx->qp_context.qos_vport = work->qos_vport;
  4348. }
  4349. void mlx4_vf_immed_vlan_work_handler(struct work_struct *_work)
  4350. {
  4351. struct mlx4_vf_immed_vlan_work *work =
  4352. container_of(_work, struct mlx4_vf_immed_vlan_work, work);
  4353. struct mlx4_cmd_mailbox *mailbox;
  4354. struct mlx4_update_qp_context *upd_context;
  4355. struct mlx4_dev *dev = &work->priv->dev;
  4356. struct mlx4_resource_tracker *tracker =
  4357. &work->priv->mfunc.master.res_tracker;
  4358. struct list_head *qp_list =
  4359. &tracker->slave_list[work->slave].res_list[RES_QP];
  4360. struct res_qp *qp;
  4361. struct res_qp *tmp;
  4362. u64 qp_path_mask_vlan_ctrl =
  4363. ((1ULL << MLX4_UPD_QP_PATH_MASK_ETH_TX_BLOCK_UNTAGGED) |
  4364. (1ULL << MLX4_UPD_QP_PATH_MASK_ETH_TX_BLOCK_1P) |
  4365. (1ULL << MLX4_UPD_QP_PATH_MASK_ETH_TX_BLOCK_TAGGED) |
  4366. (1ULL << MLX4_UPD_QP_PATH_MASK_ETH_RX_BLOCK_UNTAGGED) |
  4367. (1ULL << MLX4_UPD_QP_PATH_MASK_ETH_RX_BLOCK_1P) |
  4368. (1ULL << MLX4_UPD_QP_PATH_MASK_ETH_RX_BLOCK_TAGGED));
  4369. u64 qp_path_mask = ((1ULL << MLX4_UPD_QP_PATH_MASK_VLAN_INDEX) |
  4370. (1ULL << MLX4_UPD_QP_PATH_MASK_FVL) |
  4371. (1ULL << MLX4_UPD_QP_PATH_MASK_CV) |
  4372. (1ULL << MLX4_UPD_QP_PATH_MASK_ETH_HIDE_CQE_VLAN) |
  4373. (1ULL << MLX4_UPD_QP_PATH_MASK_FEUP) |
  4374. (1ULL << MLX4_UPD_QP_PATH_MASK_FVL_RX) |
  4375. (1ULL << MLX4_UPD_QP_PATH_MASK_SCHED_QUEUE));
  4376. int err;
  4377. int port, errors = 0;
  4378. u8 vlan_control;
  4379. if (mlx4_is_slave(dev)) {
  4380. mlx4_warn(dev, "Trying to update-qp in slave %d\n",
  4381. work->slave);
  4382. goto out;
  4383. }
  4384. mailbox = mlx4_alloc_cmd_mailbox(dev);
  4385. if (IS_ERR(mailbox))
  4386. goto out;
  4387. if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_LINK_DISABLE) /* block all */
  4388. vlan_control = MLX4_VLAN_CTRL_ETH_TX_BLOCK_TAGGED |
  4389. MLX4_VLAN_CTRL_ETH_TX_BLOCK_PRIO_TAGGED |
  4390. MLX4_VLAN_CTRL_ETH_TX_BLOCK_UNTAGGED |
  4391. MLX4_VLAN_CTRL_ETH_RX_BLOCK_PRIO_TAGGED |
  4392. MLX4_VLAN_CTRL_ETH_RX_BLOCK_UNTAGGED |
  4393. MLX4_VLAN_CTRL_ETH_RX_BLOCK_TAGGED;
  4394. else if (!work->vlan_id)
  4395. vlan_control = MLX4_VLAN_CTRL_ETH_TX_BLOCK_TAGGED |
  4396. MLX4_VLAN_CTRL_ETH_RX_BLOCK_TAGGED;
  4397. else
  4398. vlan_control = MLX4_VLAN_CTRL_ETH_TX_BLOCK_TAGGED |
  4399. MLX4_VLAN_CTRL_ETH_RX_BLOCK_PRIO_TAGGED |
  4400. MLX4_VLAN_CTRL_ETH_RX_BLOCK_UNTAGGED;
  4401. upd_context = mailbox->buf;
  4402. upd_context->qp_mask = cpu_to_be64(1ULL << MLX4_UPD_QP_MASK_VSD);
  4403. spin_lock_irq(mlx4_tlock(dev));
  4404. list_for_each_entry_safe(qp, tmp, qp_list, com.list) {
  4405. spin_unlock_irq(mlx4_tlock(dev));
  4406. if (qp->com.owner == work->slave) {
  4407. if (qp->com.from_state != RES_QP_HW ||
  4408. !qp->sched_queue || /* no INIT2RTR trans yet */
  4409. mlx4_is_qp_reserved(dev, qp->local_qpn) ||
  4410. qp->qpc_flags & (1 << MLX4_RSS_QPC_FLAG_OFFSET)) {
  4411. spin_lock_irq(mlx4_tlock(dev));
  4412. continue;
  4413. }
  4414. port = (qp->sched_queue >> 6 & 1) + 1;
  4415. if (port != work->port) {
  4416. spin_lock_irq(mlx4_tlock(dev));
  4417. continue;
  4418. }
  4419. if (MLX4_QP_ST_RC == ((qp->qpc_flags >> 16) & 0xff))
  4420. upd_context->primary_addr_path_mask = cpu_to_be64(qp_path_mask);
  4421. else
  4422. upd_context->primary_addr_path_mask =
  4423. cpu_to_be64(qp_path_mask | qp_path_mask_vlan_ctrl);
  4424. if (work->vlan_id == MLX4_VGT) {
  4425. upd_context->qp_context.param3 = qp->param3;
  4426. upd_context->qp_context.pri_path.vlan_control = qp->vlan_control;
  4427. upd_context->qp_context.pri_path.fvl_rx = qp->fvl_rx;
  4428. upd_context->qp_context.pri_path.vlan_index = qp->vlan_index;
  4429. upd_context->qp_context.pri_path.fl = qp->pri_path_fl;
  4430. upd_context->qp_context.pri_path.feup = qp->feup;
  4431. upd_context->qp_context.pri_path.sched_queue =
  4432. qp->sched_queue;
  4433. } else {
  4434. upd_context->qp_context.param3 = qp->param3 & ~cpu_to_be32(MLX4_STRIP_VLAN);
  4435. upd_context->qp_context.pri_path.vlan_control = vlan_control;
  4436. upd_context->qp_context.pri_path.vlan_index = work->vlan_ix;
  4437. upd_context->qp_context.pri_path.fvl_rx =
  4438. qp->fvl_rx | MLX4_FVL_RX_FORCE_ETH_VLAN;
  4439. upd_context->qp_context.pri_path.fl =
  4440. qp->pri_path_fl | MLX4_FL_CV | MLX4_FL_ETH_HIDE_CQE_VLAN;
  4441. upd_context->qp_context.pri_path.feup =
  4442. qp->feup | MLX4_FEUP_FORCE_ETH_UP | MLX4_FVL_FORCE_ETH_VLAN;
  4443. upd_context->qp_context.pri_path.sched_queue =
  4444. qp->sched_queue & 0xC7;
  4445. upd_context->qp_context.pri_path.sched_queue |=
  4446. ((work->qos & 0x7) << 3);
  4447. if (dev->caps.flags2 &
  4448. MLX4_DEV_CAP_FLAG2_QOS_VPP)
  4449. update_qos_vpp(upd_context, work);
  4450. }
  4451. err = mlx4_cmd(dev, mailbox->dma,
  4452. qp->local_qpn & 0xffffff,
  4453. 0, MLX4_CMD_UPDATE_QP,
  4454. MLX4_CMD_TIME_CLASS_C, MLX4_CMD_NATIVE);
  4455. if (err) {
  4456. mlx4_info(dev, "UPDATE_QP failed for slave %d, port %d, qpn %d (%d)\n",
  4457. work->slave, port, qp->local_qpn, err);
  4458. errors++;
  4459. }
  4460. }
  4461. spin_lock_irq(mlx4_tlock(dev));
  4462. }
  4463. spin_unlock_irq(mlx4_tlock(dev));
  4464. mlx4_free_cmd_mailbox(dev, mailbox);
  4465. if (errors)
  4466. mlx4_err(dev, "%d UPDATE_QP failures for slave %d, port %d\n",
  4467. errors, work->slave, work->port);
  4468. /* unregister previous vlan_id if needed and we had no errors
  4469. * while updating the QPs
  4470. */
  4471. if (work->flags & MLX4_VF_IMMED_VLAN_FLAG_VLAN && !errors &&
  4472. NO_INDX != work->orig_vlan_ix)
  4473. __mlx4_unregister_vlan(&work->priv->dev, work->port,
  4474. work->orig_vlan_id);
  4475. out:
  4476. kfree(work);
  4477. return;
  4478. }