qp.c 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/log2.h>
  34. #include <linux/etherdevice.h>
  35. #include <net/ip.h>
  36. #include <linux/slab.h>
  37. #include <linux/netdevice.h>
  38. #include <rdma/ib_cache.h>
  39. #include <rdma/ib_pack.h>
  40. #include <rdma/ib_addr.h>
  41. #include <rdma/ib_mad.h>
  42. #include <linux/mlx4/driver.h>
  43. #include <linux/mlx4/qp.h>
  44. #include "mlx4_ib.h"
  45. #include <rdma/mlx4-abi.h>
  46. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq,
  47. struct mlx4_ib_cq *recv_cq);
  48. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq,
  49. struct mlx4_ib_cq *recv_cq);
  50. static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state);
  51. enum {
  52. MLX4_IB_ACK_REQ_FREQ = 8,
  53. };
  54. enum {
  55. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  56. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
  57. MLX4_IB_LINK_TYPE_IB = 0,
  58. MLX4_IB_LINK_TYPE_ETH = 1
  59. };
  60. enum {
  61. /*
  62. * Largest possible UD header: send with GRH and immediate
  63. * data plus 18 bytes for an Ethernet header with VLAN/802.1Q
  64. * tag. (LRH would only use 8 bytes, so Ethernet is the
  65. * biggest case)
  66. */
  67. MLX4_IB_UD_HEADER_SIZE = 82,
  68. MLX4_IB_LSO_HEADER_SPARE = 128,
  69. };
  70. struct mlx4_ib_sqp {
  71. struct mlx4_ib_qp qp;
  72. int pkey_index;
  73. u32 qkey;
  74. u32 send_psn;
  75. struct ib_ud_header ud_header;
  76. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  77. struct ib_qp *roce_v2_gsi;
  78. };
  79. enum {
  80. MLX4_IB_MIN_SQ_STRIDE = 6,
  81. MLX4_IB_CACHE_LINE_SIZE = 64,
  82. };
  83. enum {
  84. MLX4_RAW_QP_MTU = 7,
  85. MLX4_RAW_QP_MSGMAX = 31,
  86. };
  87. #ifndef ETH_ALEN
  88. #define ETH_ALEN 6
  89. #endif
  90. static const __be32 mlx4_ib_opcode[] = {
  91. [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
  92. [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
  93. [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  94. [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  95. [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  96. [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  97. [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  98. [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  99. [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
  100. [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
  101. [IB_WR_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
  102. [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
  103. [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
  104. };
  105. enum mlx4_ib_source_type {
  106. MLX4_IB_QP_SRC = 0,
  107. MLX4_IB_RWQ_SRC = 1,
  108. };
  109. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  110. {
  111. return container_of(mqp, struct mlx4_ib_sqp, qp);
  112. }
  113. static int is_tunnel_qp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  114. {
  115. if (!mlx4_is_master(dev->dev))
  116. return 0;
  117. return qp->mqp.qpn >= dev->dev->phys_caps.base_tunnel_sqpn &&
  118. qp->mqp.qpn < dev->dev->phys_caps.base_tunnel_sqpn +
  119. 8 * MLX4_MFUNC_MAX;
  120. }
  121. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  122. {
  123. int proxy_sqp = 0;
  124. int real_sqp = 0;
  125. int i;
  126. /* PPF or Native -- real SQP */
  127. real_sqp = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  128. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  129. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 3);
  130. if (real_sqp)
  131. return 1;
  132. /* VF or PF -- proxy SQP */
  133. if (mlx4_is_mfunc(dev->dev)) {
  134. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  135. if (qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp0_proxy ||
  136. qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp1_proxy) {
  137. proxy_sqp = 1;
  138. break;
  139. }
  140. }
  141. }
  142. if (proxy_sqp)
  143. return 1;
  144. return !!(qp->flags & MLX4_IB_ROCE_V2_GSI_QP);
  145. }
  146. /* used for INIT/CLOSE port logic */
  147. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  148. {
  149. int proxy_qp0 = 0;
  150. int real_qp0 = 0;
  151. int i;
  152. /* PPF or Native -- real QP0 */
  153. real_qp0 = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  154. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  155. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 1);
  156. if (real_qp0)
  157. return 1;
  158. /* VF or PF -- proxy QP0 */
  159. if (mlx4_is_mfunc(dev->dev)) {
  160. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  161. if (qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp0_proxy) {
  162. proxy_qp0 = 1;
  163. break;
  164. }
  165. }
  166. }
  167. return proxy_qp0;
  168. }
  169. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  170. {
  171. return mlx4_buf_offset(&qp->buf, offset);
  172. }
  173. static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
  174. {
  175. return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
  176. }
  177. static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
  178. {
  179. return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
  180. }
  181. /*
  182. * Stamp a SQ WQE so that it is invalid if prefetched by marking the
  183. * first four bytes of every 64 byte chunk with 0xffffffff, except for
  184. * the very first chunk of the WQE.
  185. */
  186. static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n)
  187. {
  188. __be32 *wqe;
  189. int i;
  190. int s;
  191. void *buf;
  192. struct mlx4_wqe_ctrl_seg *ctrl;
  193. buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  194. ctrl = (struct mlx4_wqe_ctrl_seg *)buf;
  195. s = (ctrl->qpn_vlan.fence_size & 0x3f) << 4;
  196. for (i = 64; i < s; i += 64) {
  197. wqe = buf + i;
  198. *wqe = cpu_to_be32(0xffffffff);
  199. }
  200. }
  201. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  202. {
  203. struct ib_event event;
  204. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  205. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  206. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  207. if (ibqp->event_handler) {
  208. event.device = ibqp->device;
  209. event.element.qp = ibqp;
  210. switch (type) {
  211. case MLX4_EVENT_TYPE_PATH_MIG:
  212. event.event = IB_EVENT_PATH_MIG;
  213. break;
  214. case MLX4_EVENT_TYPE_COMM_EST:
  215. event.event = IB_EVENT_COMM_EST;
  216. break;
  217. case MLX4_EVENT_TYPE_SQ_DRAINED:
  218. event.event = IB_EVENT_SQ_DRAINED;
  219. break;
  220. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  221. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  222. break;
  223. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  224. event.event = IB_EVENT_QP_FATAL;
  225. break;
  226. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  227. event.event = IB_EVENT_PATH_MIG_ERR;
  228. break;
  229. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  230. event.event = IB_EVENT_QP_REQ_ERR;
  231. break;
  232. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  233. event.event = IB_EVENT_QP_ACCESS_ERR;
  234. break;
  235. default:
  236. pr_warn("Unexpected event type %d "
  237. "on QP %06x\n", type, qp->qpn);
  238. return;
  239. }
  240. ibqp->event_handler(&event, ibqp->qp_context);
  241. }
  242. }
  243. static void mlx4_ib_wq_event(struct mlx4_qp *qp, enum mlx4_event type)
  244. {
  245. pr_warn_ratelimited("Unexpected event type %d on WQ 0x%06x. Events are not supported for WQs\n",
  246. type, qp->qpn);
  247. }
  248. static int send_wqe_overhead(enum mlx4_ib_qp_type type, u32 flags)
  249. {
  250. /*
  251. * UD WQEs must have a datagram segment.
  252. * RC and UC WQEs might have a remote address segment.
  253. * MLX WQEs need two extra inline data segments (for the UD
  254. * header and space for the ICRC).
  255. */
  256. switch (type) {
  257. case MLX4_IB_QPT_UD:
  258. return sizeof (struct mlx4_wqe_ctrl_seg) +
  259. sizeof (struct mlx4_wqe_datagram_seg) +
  260. ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
  261. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  262. case MLX4_IB_QPT_PROXY_SMI:
  263. case MLX4_IB_QPT_PROXY_GSI:
  264. return sizeof (struct mlx4_wqe_ctrl_seg) +
  265. sizeof (struct mlx4_wqe_datagram_seg) + 64;
  266. case MLX4_IB_QPT_TUN_SMI_OWNER:
  267. case MLX4_IB_QPT_TUN_GSI:
  268. return sizeof (struct mlx4_wqe_ctrl_seg) +
  269. sizeof (struct mlx4_wqe_datagram_seg);
  270. case MLX4_IB_QPT_UC:
  271. return sizeof (struct mlx4_wqe_ctrl_seg) +
  272. sizeof (struct mlx4_wqe_raddr_seg);
  273. case MLX4_IB_QPT_RC:
  274. return sizeof (struct mlx4_wqe_ctrl_seg) +
  275. sizeof (struct mlx4_wqe_masked_atomic_seg) +
  276. sizeof (struct mlx4_wqe_raddr_seg);
  277. case MLX4_IB_QPT_SMI:
  278. case MLX4_IB_QPT_GSI:
  279. return sizeof (struct mlx4_wqe_ctrl_seg) +
  280. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  281. DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
  282. MLX4_INLINE_ALIGN) *
  283. sizeof (struct mlx4_wqe_inline_seg),
  284. sizeof (struct mlx4_wqe_data_seg)) +
  285. ALIGN(4 +
  286. sizeof (struct mlx4_wqe_inline_seg),
  287. sizeof (struct mlx4_wqe_data_seg));
  288. default:
  289. return sizeof (struct mlx4_wqe_ctrl_seg);
  290. }
  291. }
  292. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  293. int is_user, int has_rq, struct mlx4_ib_qp *qp,
  294. u32 inl_recv_sz)
  295. {
  296. /* Sanity check RQ size before proceeding */
  297. if (cap->max_recv_wr > dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE ||
  298. cap->max_recv_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg))
  299. return -EINVAL;
  300. if (!has_rq) {
  301. if (cap->max_recv_wr || inl_recv_sz)
  302. return -EINVAL;
  303. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  304. } else {
  305. u32 max_inl_recv_sz = dev->dev->caps.max_rq_sg *
  306. sizeof(struct mlx4_wqe_data_seg);
  307. u32 wqe_size;
  308. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  309. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge ||
  310. inl_recv_sz > max_inl_recv_sz))
  311. return -EINVAL;
  312. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  313. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  314. wqe_size = qp->rq.max_gs * sizeof(struct mlx4_wqe_data_seg);
  315. qp->rq.wqe_shift = ilog2(max_t(u32, wqe_size, inl_recv_sz));
  316. }
  317. /* leave userspace return values as they were, so as not to break ABI */
  318. if (is_user) {
  319. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  320. cap->max_recv_sge = qp->rq.max_gs;
  321. } else {
  322. cap->max_recv_wr = qp->rq.max_post =
  323. min(dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE, qp->rq.wqe_cnt);
  324. cap->max_recv_sge = min(qp->rq.max_gs,
  325. min(dev->dev->caps.max_sq_sg,
  326. dev->dev->caps.max_rq_sg));
  327. }
  328. return 0;
  329. }
  330. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  331. enum mlx4_ib_qp_type type, struct mlx4_ib_qp *qp)
  332. {
  333. int s;
  334. /* Sanity check SQ size before proceeding */
  335. if (cap->max_send_wr > (dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE) ||
  336. cap->max_send_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg) ||
  337. cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
  338. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  339. return -EINVAL;
  340. /*
  341. * For MLX transport we need 2 extra S/G entries:
  342. * one for the header and one for the checksum at the end
  343. */
  344. if ((type == MLX4_IB_QPT_SMI || type == MLX4_IB_QPT_GSI ||
  345. type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) &&
  346. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  347. return -EINVAL;
  348. s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
  349. cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
  350. send_wqe_overhead(type, qp->flags);
  351. if (s > dev->dev->caps.max_sq_desc_sz)
  352. return -EINVAL;
  353. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
  354. /*
  355. * We need to leave 2 KB + 1 WR of headroom in the SQ to
  356. * allow HW to prefetch.
  357. */
  358. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + 1;
  359. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr +
  360. qp->sq_spare_wqes);
  361. qp->sq.max_gs =
  362. (min(dev->dev->caps.max_sq_desc_sz,
  363. (1 << qp->sq.wqe_shift)) -
  364. send_wqe_overhead(type, qp->flags)) /
  365. sizeof (struct mlx4_wqe_data_seg);
  366. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  367. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  368. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  369. qp->rq.offset = 0;
  370. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  371. } else {
  372. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  373. qp->sq.offset = 0;
  374. }
  375. cap->max_send_wr = qp->sq.max_post =
  376. qp->sq.wqe_cnt - qp->sq_spare_wqes;
  377. cap->max_send_sge = min(qp->sq.max_gs,
  378. min(dev->dev->caps.max_sq_sg,
  379. dev->dev->caps.max_rq_sg));
  380. /* We don't support inline sends for kernel QPs (yet) */
  381. cap->max_inline_data = 0;
  382. return 0;
  383. }
  384. static int set_user_sq_size(struct mlx4_ib_dev *dev,
  385. struct mlx4_ib_qp *qp,
  386. struct mlx4_ib_create_qp *ucmd)
  387. {
  388. /* Sanity check SQ size before proceeding */
  389. if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes ||
  390. ucmd->log_sq_stride >
  391. ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
  392. ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
  393. return -EINVAL;
  394. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  395. qp->sq.wqe_shift = ucmd->log_sq_stride;
  396. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  397. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  398. return 0;
  399. }
  400. static int alloc_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  401. {
  402. int i;
  403. qp->sqp_proxy_rcv =
  404. kmalloc_array(qp->rq.wqe_cnt, sizeof(struct mlx4_ib_buf),
  405. GFP_KERNEL);
  406. if (!qp->sqp_proxy_rcv)
  407. return -ENOMEM;
  408. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  409. qp->sqp_proxy_rcv[i].addr =
  410. kmalloc(sizeof (struct mlx4_ib_proxy_sqp_hdr),
  411. GFP_KERNEL);
  412. if (!qp->sqp_proxy_rcv[i].addr)
  413. goto err;
  414. qp->sqp_proxy_rcv[i].map =
  415. ib_dma_map_single(dev, qp->sqp_proxy_rcv[i].addr,
  416. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  417. DMA_FROM_DEVICE);
  418. if (ib_dma_mapping_error(dev, qp->sqp_proxy_rcv[i].map)) {
  419. kfree(qp->sqp_proxy_rcv[i].addr);
  420. goto err;
  421. }
  422. }
  423. return 0;
  424. err:
  425. while (i > 0) {
  426. --i;
  427. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  428. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  429. DMA_FROM_DEVICE);
  430. kfree(qp->sqp_proxy_rcv[i].addr);
  431. }
  432. kfree(qp->sqp_proxy_rcv);
  433. qp->sqp_proxy_rcv = NULL;
  434. return -ENOMEM;
  435. }
  436. static void free_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  437. {
  438. int i;
  439. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  440. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  441. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  442. DMA_FROM_DEVICE);
  443. kfree(qp->sqp_proxy_rcv[i].addr);
  444. }
  445. kfree(qp->sqp_proxy_rcv);
  446. }
  447. static int qp_has_rq(struct ib_qp_init_attr *attr)
  448. {
  449. if (attr->qp_type == IB_QPT_XRC_INI || attr->qp_type == IB_QPT_XRC_TGT)
  450. return 0;
  451. return !attr->srq;
  452. }
  453. static int qp0_enabled_vf(struct mlx4_dev *dev, int qpn)
  454. {
  455. int i;
  456. for (i = 0; i < dev->caps.num_ports; i++) {
  457. if (qpn == dev->caps.spec_qps[i].qp0_proxy)
  458. return !!dev->caps.spec_qps[i].qp0_qkey;
  459. }
  460. return 0;
  461. }
  462. static void mlx4_ib_free_qp_counter(struct mlx4_ib_dev *dev,
  463. struct mlx4_ib_qp *qp)
  464. {
  465. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  466. mlx4_counter_free(dev->dev, qp->counter_index->index);
  467. list_del(&qp->counter_index->list);
  468. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  469. kfree(qp->counter_index);
  470. qp->counter_index = NULL;
  471. }
  472. static int set_qp_rss(struct mlx4_ib_dev *dev, struct mlx4_ib_rss *rss_ctx,
  473. struct ib_qp_init_attr *init_attr,
  474. struct mlx4_ib_create_qp_rss *ucmd)
  475. {
  476. rss_ctx->base_qpn_tbl_sz = init_attr->rwq_ind_tbl->ind_tbl[0]->wq_num |
  477. (init_attr->rwq_ind_tbl->log_ind_tbl_size << 24);
  478. if ((ucmd->rx_hash_function == MLX4_IB_RX_HASH_FUNC_TOEPLITZ) &&
  479. (dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP)) {
  480. memcpy(rss_ctx->rss_key, ucmd->rx_hash_key,
  481. MLX4_EN_RSS_KEY_SIZE);
  482. } else {
  483. pr_debug("RX Hash function is not supported\n");
  484. return (-EOPNOTSUPP);
  485. }
  486. if (ucmd->rx_hash_fields_mask & ~(MLX4_IB_RX_HASH_SRC_IPV4 |
  487. MLX4_IB_RX_HASH_DST_IPV4 |
  488. MLX4_IB_RX_HASH_SRC_IPV6 |
  489. MLX4_IB_RX_HASH_DST_IPV6 |
  490. MLX4_IB_RX_HASH_SRC_PORT_TCP |
  491. MLX4_IB_RX_HASH_DST_PORT_TCP |
  492. MLX4_IB_RX_HASH_SRC_PORT_UDP |
  493. MLX4_IB_RX_HASH_DST_PORT_UDP |
  494. MLX4_IB_RX_HASH_INNER)) {
  495. pr_debug("RX Hash fields_mask has unsupported mask (0x%llx)\n",
  496. ucmd->rx_hash_fields_mask);
  497. return (-EOPNOTSUPP);
  498. }
  499. if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV4) &&
  500. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV4)) {
  501. rss_ctx->flags = MLX4_RSS_IPV4;
  502. } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV4) ||
  503. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV4)) {
  504. pr_debug("RX Hash fields_mask is not supported - both IPv4 SRC and DST must be set\n");
  505. return (-EOPNOTSUPP);
  506. }
  507. if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV6) &&
  508. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV6)) {
  509. rss_ctx->flags |= MLX4_RSS_IPV6;
  510. } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV6) ||
  511. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV6)) {
  512. pr_debug("RX Hash fields_mask is not supported - both IPv6 SRC and DST must be set\n");
  513. return (-EOPNOTSUPP);
  514. }
  515. if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_UDP) &&
  516. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_UDP)) {
  517. if (!(dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UDP_RSS)) {
  518. pr_debug("RX Hash fields_mask for UDP is not supported\n");
  519. return (-EOPNOTSUPP);
  520. }
  521. if (rss_ctx->flags & MLX4_RSS_IPV4)
  522. rss_ctx->flags |= MLX4_RSS_UDP_IPV4;
  523. if (rss_ctx->flags & MLX4_RSS_IPV6)
  524. rss_ctx->flags |= MLX4_RSS_UDP_IPV6;
  525. if (!(rss_ctx->flags & (MLX4_RSS_IPV6 | MLX4_RSS_IPV4))) {
  526. pr_debug("RX Hash fields_mask is not supported - UDP must be set with IPv4 or IPv6\n");
  527. return (-EOPNOTSUPP);
  528. }
  529. } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_UDP) ||
  530. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_UDP)) {
  531. pr_debug("RX Hash fields_mask is not supported - both UDP SRC and DST must be set\n");
  532. return (-EOPNOTSUPP);
  533. }
  534. if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_TCP) &&
  535. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_TCP)) {
  536. if (rss_ctx->flags & MLX4_RSS_IPV4)
  537. rss_ctx->flags |= MLX4_RSS_TCP_IPV4;
  538. if (rss_ctx->flags & MLX4_RSS_IPV6)
  539. rss_ctx->flags |= MLX4_RSS_TCP_IPV6;
  540. if (!(rss_ctx->flags & (MLX4_RSS_IPV6 | MLX4_RSS_IPV4))) {
  541. pr_debug("RX Hash fields_mask is not supported - TCP must be set with IPv4 or IPv6\n");
  542. return (-EOPNOTSUPP);
  543. }
  544. } else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_TCP) ||
  545. (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_TCP)) {
  546. pr_debug("RX Hash fields_mask is not supported - both TCP SRC and DST must be set\n");
  547. return (-EOPNOTSUPP);
  548. }
  549. if (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_INNER) {
  550. if (dev->dev->caps.tunnel_offload_mode ==
  551. MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  552. /*
  553. * Hash according to inner headers if exist, otherwise
  554. * according to outer headers.
  555. */
  556. rss_ctx->flags |= MLX4_RSS_BY_INNER_HEADERS_IPONLY;
  557. } else {
  558. pr_debug("RSS Hash for inner headers isn't supported\n");
  559. return (-EOPNOTSUPP);
  560. }
  561. }
  562. return 0;
  563. }
  564. static int create_qp_rss(struct mlx4_ib_dev *dev,
  565. struct ib_qp_init_attr *init_attr,
  566. struct mlx4_ib_create_qp_rss *ucmd,
  567. struct mlx4_ib_qp *qp)
  568. {
  569. int qpn;
  570. int err;
  571. qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS;
  572. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn, 0, qp->mqp.usage);
  573. if (err)
  574. return err;
  575. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  576. if (err)
  577. goto err_qpn;
  578. mutex_init(&qp->mutex);
  579. INIT_LIST_HEAD(&qp->gid_list);
  580. INIT_LIST_HEAD(&qp->steering_rules);
  581. qp->mlx4_ib_qp_type = MLX4_IB_QPT_RAW_PACKET;
  582. qp->state = IB_QPS_RESET;
  583. /* Set dummy send resources to be compatible with HV and PRM */
  584. qp->sq_no_prefetch = 1;
  585. qp->sq.wqe_cnt = 1;
  586. qp->sq.wqe_shift = MLX4_IB_MIN_SQ_STRIDE;
  587. qp->buf_size = qp->sq.wqe_cnt << MLX4_IB_MIN_SQ_STRIDE;
  588. qp->mtt = (to_mqp(
  589. (struct ib_qp *)init_attr->rwq_ind_tbl->ind_tbl[0]))->mtt;
  590. qp->rss_ctx = kzalloc(sizeof(*qp->rss_ctx), GFP_KERNEL);
  591. if (!qp->rss_ctx) {
  592. err = -ENOMEM;
  593. goto err_qp_alloc;
  594. }
  595. err = set_qp_rss(dev, qp->rss_ctx, init_attr, ucmd);
  596. if (err)
  597. goto err;
  598. return 0;
  599. err:
  600. kfree(qp->rss_ctx);
  601. err_qp_alloc:
  602. mlx4_qp_remove(dev->dev, &qp->mqp);
  603. mlx4_qp_free(dev->dev, &qp->mqp);
  604. err_qpn:
  605. mlx4_qp_release_range(dev->dev, qpn, 1);
  606. return err;
  607. }
  608. static struct ib_qp *_mlx4_ib_create_qp_rss(struct ib_pd *pd,
  609. struct ib_qp_init_attr *init_attr,
  610. struct ib_udata *udata)
  611. {
  612. struct mlx4_ib_qp *qp;
  613. struct mlx4_ib_create_qp_rss ucmd = {};
  614. size_t required_cmd_sz;
  615. int err;
  616. if (!udata) {
  617. pr_debug("RSS QP with NULL udata\n");
  618. return ERR_PTR(-EINVAL);
  619. }
  620. if (udata->outlen)
  621. return ERR_PTR(-EOPNOTSUPP);
  622. required_cmd_sz = offsetof(typeof(ucmd), reserved1) +
  623. sizeof(ucmd.reserved1);
  624. if (udata->inlen < required_cmd_sz) {
  625. pr_debug("invalid inlen\n");
  626. return ERR_PTR(-EINVAL);
  627. }
  628. if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
  629. pr_debug("copy failed\n");
  630. return ERR_PTR(-EFAULT);
  631. }
  632. if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)))
  633. return ERR_PTR(-EOPNOTSUPP);
  634. if (ucmd.comp_mask || ucmd.reserved1)
  635. return ERR_PTR(-EOPNOTSUPP);
  636. if (udata->inlen > sizeof(ucmd) &&
  637. !ib_is_udata_cleared(udata, sizeof(ucmd),
  638. udata->inlen - sizeof(ucmd))) {
  639. pr_debug("inlen is not supported\n");
  640. return ERR_PTR(-EOPNOTSUPP);
  641. }
  642. if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
  643. pr_debug("RSS QP with unsupported QP type %d\n",
  644. init_attr->qp_type);
  645. return ERR_PTR(-EOPNOTSUPP);
  646. }
  647. if (init_attr->create_flags) {
  648. pr_debug("RSS QP doesn't support create flags\n");
  649. return ERR_PTR(-EOPNOTSUPP);
  650. }
  651. if (init_attr->send_cq || init_attr->cap.max_send_wr) {
  652. pr_debug("RSS QP with unsupported send attributes\n");
  653. return ERR_PTR(-EOPNOTSUPP);
  654. }
  655. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  656. if (!qp)
  657. return ERR_PTR(-ENOMEM);
  658. qp->pri.vid = 0xFFFF;
  659. qp->alt.vid = 0xFFFF;
  660. err = create_qp_rss(to_mdev(pd->device), init_attr, &ucmd, qp);
  661. if (err) {
  662. kfree(qp);
  663. return ERR_PTR(err);
  664. }
  665. qp->ibqp.qp_num = qp->mqp.qpn;
  666. return &qp->ibqp;
  667. }
  668. /*
  669. * This function allocates a WQN from a range which is consecutive and aligned
  670. * to its size. In case the range is full, then it creates a new range and
  671. * allocates WQN from it. The new range will be used for following allocations.
  672. */
  673. static int mlx4_ib_alloc_wqn(struct mlx4_ib_ucontext *context,
  674. struct mlx4_ib_qp *qp, int range_size, int *wqn)
  675. {
  676. struct mlx4_ib_dev *dev = to_mdev(context->ibucontext.device);
  677. struct mlx4_wqn_range *range;
  678. int err = 0;
  679. mutex_lock(&context->wqn_ranges_mutex);
  680. range = list_first_entry_or_null(&context->wqn_ranges_list,
  681. struct mlx4_wqn_range, list);
  682. if (!range || (range->refcount == range->size) || range->dirty) {
  683. range = kzalloc(sizeof(*range), GFP_KERNEL);
  684. if (!range) {
  685. err = -ENOMEM;
  686. goto out;
  687. }
  688. err = mlx4_qp_reserve_range(dev->dev, range_size,
  689. range_size, &range->base_wqn, 0,
  690. qp->mqp.usage);
  691. if (err) {
  692. kfree(range);
  693. goto out;
  694. }
  695. range->size = range_size;
  696. list_add(&range->list, &context->wqn_ranges_list);
  697. } else if (range_size != 1) {
  698. /*
  699. * Requesting a new range (>1) when last range is still open, is
  700. * not valid.
  701. */
  702. err = -EINVAL;
  703. goto out;
  704. }
  705. qp->wqn_range = range;
  706. *wqn = range->base_wqn + range->refcount;
  707. range->refcount++;
  708. out:
  709. mutex_unlock(&context->wqn_ranges_mutex);
  710. return err;
  711. }
  712. static void mlx4_ib_release_wqn(struct mlx4_ib_ucontext *context,
  713. struct mlx4_ib_qp *qp, bool dirty_release)
  714. {
  715. struct mlx4_ib_dev *dev = to_mdev(context->ibucontext.device);
  716. struct mlx4_wqn_range *range;
  717. mutex_lock(&context->wqn_ranges_mutex);
  718. range = qp->wqn_range;
  719. range->refcount--;
  720. if (!range->refcount) {
  721. mlx4_qp_release_range(dev->dev, range->base_wqn,
  722. range->size);
  723. list_del(&range->list);
  724. kfree(range);
  725. } else if (dirty_release) {
  726. /*
  727. * A range which one of its WQNs is destroyed, won't be able to be
  728. * reused for further WQN allocations.
  729. * The next created WQ will allocate a new range.
  730. */
  731. range->dirty = 1;
  732. }
  733. mutex_unlock(&context->wqn_ranges_mutex);
  734. }
  735. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  736. enum mlx4_ib_source_type src,
  737. struct ib_qp_init_attr *init_attr,
  738. struct ib_udata *udata, int sqpn,
  739. struct mlx4_ib_qp **caller_qp)
  740. {
  741. int qpn;
  742. int err;
  743. struct mlx4_ib_sqp *sqp = NULL;
  744. struct mlx4_ib_qp *qp;
  745. enum mlx4_ib_qp_type qp_type = (enum mlx4_ib_qp_type) init_attr->qp_type;
  746. struct mlx4_ib_cq *mcq;
  747. unsigned long flags;
  748. int range_size = 0;
  749. /* When tunneling special qps, we use a plain UD qp */
  750. if (sqpn) {
  751. if (mlx4_is_mfunc(dev->dev) &&
  752. (!mlx4_is_master(dev->dev) ||
  753. !(init_attr->create_flags & MLX4_IB_SRIOV_SQP))) {
  754. if (init_attr->qp_type == IB_QPT_GSI)
  755. qp_type = MLX4_IB_QPT_PROXY_GSI;
  756. else {
  757. if (mlx4_is_master(dev->dev) ||
  758. qp0_enabled_vf(dev->dev, sqpn))
  759. qp_type = MLX4_IB_QPT_PROXY_SMI_OWNER;
  760. else
  761. qp_type = MLX4_IB_QPT_PROXY_SMI;
  762. }
  763. }
  764. qpn = sqpn;
  765. /* add extra sg entry for tunneling */
  766. init_attr->cap.max_recv_sge++;
  767. } else if (init_attr->create_flags & MLX4_IB_SRIOV_TUNNEL_QP) {
  768. struct mlx4_ib_qp_tunnel_init_attr *tnl_init =
  769. container_of(init_attr,
  770. struct mlx4_ib_qp_tunnel_init_attr, init_attr);
  771. if ((tnl_init->proxy_qp_type != IB_QPT_SMI &&
  772. tnl_init->proxy_qp_type != IB_QPT_GSI) ||
  773. !mlx4_is_master(dev->dev))
  774. return -EINVAL;
  775. if (tnl_init->proxy_qp_type == IB_QPT_GSI)
  776. qp_type = MLX4_IB_QPT_TUN_GSI;
  777. else if (tnl_init->slave == mlx4_master_func_num(dev->dev) ||
  778. mlx4_vf_smi_enabled(dev->dev, tnl_init->slave,
  779. tnl_init->port))
  780. qp_type = MLX4_IB_QPT_TUN_SMI_OWNER;
  781. else
  782. qp_type = MLX4_IB_QPT_TUN_SMI;
  783. /* we are definitely in the PPF here, since we are creating
  784. * tunnel QPs. base_tunnel_sqpn is therefore valid. */
  785. qpn = dev->dev->phys_caps.base_tunnel_sqpn + 8 * tnl_init->slave
  786. + tnl_init->proxy_qp_type * 2 + tnl_init->port - 1;
  787. sqpn = qpn;
  788. }
  789. if (!*caller_qp) {
  790. if (qp_type == MLX4_IB_QPT_SMI || qp_type == MLX4_IB_QPT_GSI ||
  791. (qp_type & (MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_SMI_OWNER |
  792. MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  793. sqp = kzalloc(sizeof(struct mlx4_ib_sqp), GFP_KERNEL);
  794. if (!sqp)
  795. return -ENOMEM;
  796. qp = &sqp->qp;
  797. qp->pri.vid = 0xFFFF;
  798. qp->alt.vid = 0xFFFF;
  799. } else {
  800. qp = kzalloc(sizeof(struct mlx4_ib_qp), GFP_KERNEL);
  801. if (!qp)
  802. return -ENOMEM;
  803. qp->pri.vid = 0xFFFF;
  804. qp->alt.vid = 0xFFFF;
  805. }
  806. } else
  807. qp = *caller_qp;
  808. qp->mlx4_ib_qp_type = qp_type;
  809. mutex_init(&qp->mutex);
  810. spin_lock_init(&qp->sq.lock);
  811. spin_lock_init(&qp->rq.lock);
  812. INIT_LIST_HEAD(&qp->gid_list);
  813. INIT_LIST_HEAD(&qp->steering_rules);
  814. qp->state = IB_QPS_RESET;
  815. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  816. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  817. if (pd->uobject) {
  818. union {
  819. struct mlx4_ib_create_qp qp;
  820. struct mlx4_ib_create_wq wq;
  821. } ucmd;
  822. size_t copy_len;
  823. int shift;
  824. int n;
  825. copy_len = (src == MLX4_IB_QP_SRC) ?
  826. sizeof(struct mlx4_ib_create_qp) :
  827. min(sizeof(struct mlx4_ib_create_wq), udata->inlen);
  828. if (ib_copy_from_udata(&ucmd, udata, copy_len)) {
  829. err = -EFAULT;
  830. goto err;
  831. }
  832. if (src == MLX4_IB_RWQ_SRC) {
  833. if (ucmd.wq.comp_mask || ucmd.wq.reserved[0] ||
  834. ucmd.wq.reserved[1] || ucmd.wq.reserved[2]) {
  835. pr_debug("user command isn't supported\n");
  836. err = -EOPNOTSUPP;
  837. goto err;
  838. }
  839. if (ucmd.wq.log_range_size >
  840. ilog2(dev->dev->caps.max_rss_tbl_sz)) {
  841. pr_debug("WQN range size must be equal or smaller than %d\n",
  842. dev->dev->caps.max_rss_tbl_sz);
  843. err = -EOPNOTSUPP;
  844. goto err;
  845. }
  846. range_size = 1 << ucmd.wq.log_range_size;
  847. } else {
  848. qp->inl_recv_sz = ucmd.qp.inl_recv_sz;
  849. }
  850. if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
  851. if (!(dev->dev->caps.flags &
  852. MLX4_DEV_CAP_FLAG_FCS_KEEP)) {
  853. pr_debug("scatter FCS is unsupported\n");
  854. err = -EOPNOTSUPP;
  855. goto err;
  856. }
  857. qp->flags |= MLX4_IB_QP_SCATTER_FCS;
  858. }
  859. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject,
  860. qp_has_rq(init_attr), qp, qp->inl_recv_sz);
  861. if (err)
  862. goto err;
  863. if (src == MLX4_IB_QP_SRC) {
  864. qp->sq_no_prefetch = ucmd.qp.sq_no_prefetch;
  865. err = set_user_sq_size(dev, qp,
  866. (struct mlx4_ib_create_qp *)
  867. &ucmd);
  868. if (err)
  869. goto err;
  870. } else {
  871. qp->sq_no_prefetch = 1;
  872. qp->sq.wqe_cnt = 1;
  873. qp->sq.wqe_shift = MLX4_IB_MIN_SQ_STRIDE;
  874. /* Allocated buffer expects to have at least that SQ
  875. * size.
  876. */
  877. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  878. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  879. }
  880. qp->umem = ib_umem_get(pd->uobject->context,
  881. (src == MLX4_IB_QP_SRC) ? ucmd.qp.buf_addr :
  882. ucmd.wq.buf_addr, qp->buf_size, 0, 0);
  883. if (IS_ERR(qp->umem)) {
  884. err = PTR_ERR(qp->umem);
  885. goto err;
  886. }
  887. n = ib_umem_page_count(qp->umem);
  888. shift = mlx4_ib_umem_calc_optimal_mtt_size(qp->umem, 0, &n);
  889. err = mlx4_mtt_init(dev->dev, n, shift, &qp->mtt);
  890. if (err)
  891. goto err_buf;
  892. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  893. if (err)
  894. goto err_mtt;
  895. if (qp_has_rq(init_attr)) {
  896. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  897. (src == MLX4_IB_QP_SRC) ? ucmd.qp.db_addr :
  898. ucmd.wq.db_addr, &qp->db);
  899. if (err)
  900. goto err_mtt;
  901. }
  902. qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS;
  903. } else {
  904. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject,
  905. qp_has_rq(init_attr), qp, 0);
  906. if (err)
  907. goto err;
  908. qp->sq_no_prefetch = 0;
  909. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
  910. qp->flags |= MLX4_IB_QP_LSO;
  911. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  912. if (dev->steering_support ==
  913. MLX4_STEERING_MODE_DEVICE_MANAGED)
  914. qp->flags |= MLX4_IB_QP_NETIF;
  915. else
  916. goto err;
  917. }
  918. err = set_kernel_sq_size(dev, &init_attr->cap, qp_type, qp);
  919. if (err)
  920. goto err;
  921. if (qp_has_rq(init_attr)) {
  922. err = mlx4_db_alloc(dev->dev, &qp->db, 0);
  923. if (err)
  924. goto err;
  925. *qp->db.db = 0;
  926. }
  927. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2,
  928. &qp->buf)) {
  929. err = -ENOMEM;
  930. goto err_db;
  931. }
  932. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  933. &qp->mtt);
  934. if (err)
  935. goto err_buf;
  936. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  937. if (err)
  938. goto err_mtt;
  939. qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
  940. sizeof(u64), GFP_KERNEL);
  941. qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
  942. sizeof(u64), GFP_KERNEL);
  943. if (!qp->sq.wrid || !qp->rq.wrid) {
  944. err = -ENOMEM;
  945. goto err_wrid;
  946. }
  947. qp->mqp.usage = MLX4_RES_USAGE_DRIVER;
  948. }
  949. if (sqpn) {
  950. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  951. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  952. if (alloc_proxy_bufs(pd->device, qp)) {
  953. err = -ENOMEM;
  954. goto err_wrid;
  955. }
  956. }
  957. } else if (src == MLX4_IB_RWQ_SRC) {
  958. err = mlx4_ib_alloc_wqn(to_mucontext(pd->uobject->context), qp,
  959. range_size, &qpn);
  960. if (err)
  961. goto err_wrid;
  962. } else {
  963. /* Raw packet QPNs may not have bits 6,7 set in their qp_num;
  964. * otherwise, the WQE BlueFlame setup flow wrongly causes
  965. * VLAN insertion. */
  966. if (init_attr->qp_type == IB_QPT_RAW_PACKET)
  967. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn,
  968. (init_attr->cap.max_send_wr ?
  969. MLX4_RESERVE_ETH_BF_QP : 0) |
  970. (init_attr->cap.max_recv_wr ?
  971. MLX4_RESERVE_A0_QP : 0),
  972. qp->mqp.usage);
  973. else
  974. if (qp->flags & MLX4_IB_QP_NETIF)
  975. err = mlx4_ib_steer_qp_alloc(dev, 1, &qpn);
  976. else
  977. err = mlx4_qp_reserve_range(dev->dev, 1, 1,
  978. &qpn, 0, qp->mqp.usage);
  979. if (err)
  980. goto err_proxy;
  981. }
  982. if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
  983. qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  984. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  985. if (err)
  986. goto err_qpn;
  987. if (init_attr->qp_type == IB_QPT_XRC_TGT)
  988. qp->mqp.qpn |= (1 << 23);
  989. /*
  990. * Hardware wants QPN written in big-endian order (after
  991. * shifting) for send doorbell. Precompute this value to save
  992. * a little bit when posting sends.
  993. */
  994. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  995. qp->mqp.event = (src == MLX4_IB_QP_SRC) ? mlx4_ib_qp_event :
  996. mlx4_ib_wq_event;
  997. if (!*caller_qp)
  998. *caller_qp = qp;
  999. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  1000. mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq),
  1001. to_mcq(init_attr->recv_cq));
  1002. /* Maintain device to QPs access, needed for further handling
  1003. * via reset flow
  1004. */
  1005. list_add_tail(&qp->qps_list, &dev->qp_list);
  1006. /* Maintain CQ to QPs access, needed for further handling
  1007. * via reset flow
  1008. */
  1009. mcq = to_mcq(init_attr->send_cq);
  1010. list_add_tail(&qp->cq_send_list, &mcq->send_qp_list);
  1011. mcq = to_mcq(init_attr->recv_cq);
  1012. list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list);
  1013. mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq),
  1014. to_mcq(init_attr->recv_cq));
  1015. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  1016. return 0;
  1017. err_qpn:
  1018. if (!sqpn) {
  1019. if (qp->flags & MLX4_IB_QP_NETIF)
  1020. mlx4_ib_steer_qp_free(dev, qpn, 1);
  1021. else if (src == MLX4_IB_RWQ_SRC)
  1022. mlx4_ib_release_wqn(to_mucontext(pd->uobject->context),
  1023. qp, 0);
  1024. else
  1025. mlx4_qp_release_range(dev->dev, qpn, 1);
  1026. }
  1027. err_proxy:
  1028. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  1029. free_proxy_bufs(pd->device, qp);
  1030. err_wrid:
  1031. if (pd->uobject) {
  1032. if (qp_has_rq(init_attr))
  1033. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  1034. } else {
  1035. kvfree(qp->sq.wrid);
  1036. kvfree(qp->rq.wrid);
  1037. }
  1038. err_mtt:
  1039. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  1040. err_buf:
  1041. if (pd->uobject)
  1042. ib_umem_release(qp->umem);
  1043. else
  1044. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  1045. err_db:
  1046. if (!pd->uobject && qp_has_rq(init_attr))
  1047. mlx4_db_free(dev->dev, &qp->db);
  1048. err:
  1049. if (sqp)
  1050. kfree(sqp);
  1051. else if (!*caller_qp)
  1052. kfree(qp);
  1053. return err;
  1054. }
  1055. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  1056. {
  1057. switch (state) {
  1058. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  1059. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  1060. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  1061. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  1062. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  1063. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  1064. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  1065. default: return -1;
  1066. }
  1067. }
  1068. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  1069. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  1070. {
  1071. if (send_cq == recv_cq) {
  1072. spin_lock(&send_cq->lock);
  1073. __acquire(&recv_cq->lock);
  1074. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  1075. spin_lock(&send_cq->lock);
  1076. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  1077. } else {
  1078. spin_lock(&recv_cq->lock);
  1079. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  1080. }
  1081. }
  1082. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  1083. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  1084. {
  1085. if (send_cq == recv_cq) {
  1086. __release(&recv_cq->lock);
  1087. spin_unlock(&send_cq->lock);
  1088. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  1089. spin_unlock(&recv_cq->lock);
  1090. spin_unlock(&send_cq->lock);
  1091. } else {
  1092. spin_unlock(&send_cq->lock);
  1093. spin_unlock(&recv_cq->lock);
  1094. }
  1095. }
  1096. static void del_gid_entries(struct mlx4_ib_qp *qp)
  1097. {
  1098. struct mlx4_ib_gid_entry *ge, *tmp;
  1099. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1100. list_del(&ge->list);
  1101. kfree(ge);
  1102. }
  1103. }
  1104. static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp)
  1105. {
  1106. if (qp->ibqp.qp_type == IB_QPT_XRC_TGT)
  1107. return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd);
  1108. else
  1109. return to_mpd(qp->ibqp.pd);
  1110. }
  1111. static void get_cqs(struct mlx4_ib_qp *qp, enum mlx4_ib_source_type src,
  1112. struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq)
  1113. {
  1114. switch (qp->ibqp.qp_type) {
  1115. case IB_QPT_XRC_TGT:
  1116. *send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq);
  1117. *recv_cq = *send_cq;
  1118. break;
  1119. case IB_QPT_XRC_INI:
  1120. *send_cq = to_mcq(qp->ibqp.send_cq);
  1121. *recv_cq = *send_cq;
  1122. break;
  1123. default:
  1124. *recv_cq = (src == MLX4_IB_QP_SRC) ? to_mcq(qp->ibqp.recv_cq) :
  1125. to_mcq(qp->ibwq.cq);
  1126. *send_cq = (src == MLX4_IB_QP_SRC) ? to_mcq(qp->ibqp.send_cq) :
  1127. *recv_cq;
  1128. break;
  1129. }
  1130. }
  1131. static void destroy_qp_rss(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1132. {
  1133. if (qp->state != IB_QPS_RESET) {
  1134. int i;
  1135. for (i = 0; i < (1 << qp->ibqp.rwq_ind_tbl->log_ind_tbl_size);
  1136. i++) {
  1137. struct ib_wq *ibwq = qp->ibqp.rwq_ind_tbl->ind_tbl[i];
  1138. struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
  1139. mutex_lock(&wq->mutex);
  1140. wq->rss_usecnt--;
  1141. mutex_unlock(&wq->mutex);
  1142. }
  1143. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  1144. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  1145. pr_warn("modify QP %06x to RESET failed.\n",
  1146. qp->mqp.qpn);
  1147. }
  1148. mlx4_qp_remove(dev->dev, &qp->mqp);
  1149. mlx4_qp_free(dev->dev, &qp->mqp);
  1150. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  1151. del_gid_entries(qp);
  1152. kfree(qp->rss_ctx);
  1153. }
  1154. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  1155. enum mlx4_ib_source_type src, int is_user)
  1156. {
  1157. struct mlx4_ib_cq *send_cq, *recv_cq;
  1158. unsigned long flags;
  1159. if (qp->state != IB_QPS_RESET) {
  1160. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  1161. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  1162. pr_warn("modify QP %06x to RESET failed.\n",
  1163. qp->mqp.qpn);
  1164. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  1165. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  1166. qp->pri.smac = 0;
  1167. qp->pri.smac_port = 0;
  1168. }
  1169. if (qp->alt.smac) {
  1170. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  1171. qp->alt.smac = 0;
  1172. }
  1173. if (qp->pri.vid < 0x1000) {
  1174. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  1175. qp->pri.vid = 0xFFFF;
  1176. qp->pri.candidate_vid = 0xFFFF;
  1177. qp->pri.update_vid = 0;
  1178. }
  1179. if (qp->alt.vid < 0x1000) {
  1180. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  1181. qp->alt.vid = 0xFFFF;
  1182. qp->alt.candidate_vid = 0xFFFF;
  1183. qp->alt.update_vid = 0;
  1184. }
  1185. }
  1186. get_cqs(qp, src, &send_cq, &recv_cq);
  1187. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  1188. mlx4_ib_lock_cqs(send_cq, recv_cq);
  1189. /* del from lists under both locks above to protect reset flow paths */
  1190. list_del(&qp->qps_list);
  1191. list_del(&qp->cq_send_list);
  1192. list_del(&qp->cq_recv_list);
  1193. if (!is_user) {
  1194. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  1195. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  1196. if (send_cq != recv_cq)
  1197. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  1198. }
  1199. mlx4_qp_remove(dev->dev, &qp->mqp);
  1200. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  1201. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  1202. mlx4_qp_free(dev->dev, &qp->mqp);
  1203. if (!is_sqp(dev, qp) && !is_tunnel_qp(dev, qp)) {
  1204. if (qp->flags & MLX4_IB_QP_NETIF)
  1205. mlx4_ib_steer_qp_free(dev, qp->mqp.qpn, 1);
  1206. else if (src == MLX4_IB_RWQ_SRC)
  1207. mlx4_ib_release_wqn(to_mucontext(
  1208. qp->ibwq.uobject->context), qp, 1);
  1209. else
  1210. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  1211. }
  1212. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  1213. if (is_user) {
  1214. if (qp->rq.wqe_cnt) {
  1215. struct mlx4_ib_ucontext *mcontext = !src ?
  1216. to_mucontext(qp->ibqp.uobject->context) :
  1217. to_mucontext(qp->ibwq.uobject->context);
  1218. mlx4_ib_db_unmap_user(mcontext, &qp->db);
  1219. }
  1220. ib_umem_release(qp->umem);
  1221. } else {
  1222. kvfree(qp->sq.wrid);
  1223. kvfree(qp->rq.wrid);
  1224. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  1225. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI))
  1226. free_proxy_bufs(&dev->ib_dev, qp);
  1227. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  1228. if (qp->rq.wqe_cnt)
  1229. mlx4_db_free(dev->dev, &qp->db);
  1230. }
  1231. del_gid_entries(qp);
  1232. }
  1233. static u32 get_sqp_num(struct mlx4_ib_dev *dev, struct ib_qp_init_attr *attr)
  1234. {
  1235. /* Native or PPF */
  1236. if (!mlx4_is_mfunc(dev->dev) ||
  1237. (mlx4_is_master(dev->dev) &&
  1238. attr->create_flags & MLX4_IB_SRIOV_SQP)) {
  1239. return dev->dev->phys_caps.base_sqpn +
  1240. (attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  1241. attr->port_num - 1;
  1242. }
  1243. /* PF or VF -- creating proxies */
  1244. if (attr->qp_type == IB_QPT_SMI)
  1245. return dev->dev->caps.spec_qps[attr->port_num - 1].qp0_proxy;
  1246. else
  1247. return dev->dev->caps.spec_qps[attr->port_num - 1].qp1_proxy;
  1248. }
  1249. static struct ib_qp *_mlx4_ib_create_qp(struct ib_pd *pd,
  1250. struct ib_qp_init_attr *init_attr,
  1251. struct ib_udata *udata)
  1252. {
  1253. struct mlx4_ib_qp *qp = NULL;
  1254. int err;
  1255. int sup_u_create_flags = MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  1256. u16 xrcdn = 0;
  1257. if (init_attr->rwq_ind_tbl)
  1258. return _mlx4_ib_create_qp_rss(pd, init_attr, udata);
  1259. /*
  1260. * We only support LSO, vendor flag1, and multicast loopback blocking,
  1261. * and only for kernel UD QPs.
  1262. */
  1263. if (init_attr->create_flags & ~(MLX4_IB_QP_LSO |
  1264. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK |
  1265. MLX4_IB_SRIOV_TUNNEL_QP |
  1266. MLX4_IB_SRIOV_SQP |
  1267. MLX4_IB_QP_NETIF |
  1268. MLX4_IB_QP_CREATE_ROCE_V2_GSI))
  1269. return ERR_PTR(-EINVAL);
  1270. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  1271. if (init_attr->qp_type != IB_QPT_UD)
  1272. return ERR_PTR(-EINVAL);
  1273. }
  1274. if (init_attr->create_flags) {
  1275. if (udata && init_attr->create_flags & ~(sup_u_create_flags))
  1276. return ERR_PTR(-EINVAL);
  1277. if ((init_attr->create_flags & ~(MLX4_IB_SRIOV_SQP |
  1278. MLX4_IB_QP_CREATE_ROCE_V2_GSI |
  1279. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) &&
  1280. init_attr->qp_type != IB_QPT_UD) ||
  1281. (init_attr->create_flags & MLX4_IB_SRIOV_SQP &&
  1282. init_attr->qp_type > IB_QPT_GSI) ||
  1283. (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI &&
  1284. init_attr->qp_type != IB_QPT_GSI))
  1285. return ERR_PTR(-EINVAL);
  1286. }
  1287. switch (init_attr->qp_type) {
  1288. case IB_QPT_XRC_TGT:
  1289. pd = to_mxrcd(init_attr->xrcd)->pd;
  1290. xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
  1291. init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq;
  1292. /* fall through */
  1293. case IB_QPT_XRC_INI:
  1294. if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  1295. return ERR_PTR(-ENOSYS);
  1296. init_attr->recv_cq = init_attr->send_cq;
  1297. /* fall through */
  1298. case IB_QPT_RC:
  1299. case IB_QPT_UC:
  1300. case IB_QPT_RAW_PACKET:
  1301. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  1302. if (!qp)
  1303. return ERR_PTR(-ENOMEM);
  1304. qp->pri.vid = 0xFFFF;
  1305. qp->alt.vid = 0xFFFF;
  1306. /* fall through */
  1307. case IB_QPT_UD:
  1308. {
  1309. err = create_qp_common(to_mdev(pd->device), pd, MLX4_IB_QP_SRC,
  1310. init_attr, udata, 0, &qp);
  1311. if (err) {
  1312. kfree(qp);
  1313. return ERR_PTR(err);
  1314. }
  1315. qp->ibqp.qp_num = qp->mqp.qpn;
  1316. qp->xrcdn = xrcdn;
  1317. break;
  1318. }
  1319. case IB_QPT_SMI:
  1320. case IB_QPT_GSI:
  1321. {
  1322. int sqpn;
  1323. /* Userspace is not allowed to create special QPs: */
  1324. if (udata)
  1325. return ERR_PTR(-EINVAL);
  1326. if (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI) {
  1327. int res = mlx4_qp_reserve_range(to_mdev(pd->device)->dev,
  1328. 1, 1, &sqpn, 0,
  1329. MLX4_RES_USAGE_DRIVER);
  1330. if (res)
  1331. return ERR_PTR(res);
  1332. } else {
  1333. sqpn = get_sqp_num(to_mdev(pd->device), init_attr);
  1334. }
  1335. err = create_qp_common(to_mdev(pd->device), pd, MLX4_IB_QP_SRC,
  1336. init_attr, udata, sqpn, &qp);
  1337. if (err)
  1338. return ERR_PTR(err);
  1339. qp->port = init_attr->port_num;
  1340. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 :
  1341. init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI ? sqpn : 1;
  1342. break;
  1343. }
  1344. default:
  1345. /* Don't support raw QPs */
  1346. return ERR_PTR(-EINVAL);
  1347. }
  1348. return &qp->ibqp;
  1349. }
  1350. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  1351. struct ib_qp_init_attr *init_attr,
  1352. struct ib_udata *udata) {
  1353. struct ib_device *device = pd ? pd->device : init_attr->xrcd->device;
  1354. struct ib_qp *ibqp;
  1355. struct mlx4_ib_dev *dev = to_mdev(device);
  1356. ibqp = _mlx4_ib_create_qp(pd, init_attr, udata);
  1357. if (!IS_ERR(ibqp) &&
  1358. (init_attr->qp_type == IB_QPT_GSI) &&
  1359. !(init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI)) {
  1360. struct mlx4_ib_sqp *sqp = to_msqp((to_mqp(ibqp)));
  1361. int is_eth = rdma_cap_eth_ah(&dev->ib_dev, init_attr->port_num);
  1362. if (is_eth &&
  1363. dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
  1364. init_attr->create_flags |= MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1365. sqp->roce_v2_gsi = ib_create_qp(pd, init_attr);
  1366. if (IS_ERR(sqp->roce_v2_gsi)) {
  1367. pr_err("Failed to create GSI QP for RoCEv2 (%ld)\n", PTR_ERR(sqp->roce_v2_gsi));
  1368. sqp->roce_v2_gsi = NULL;
  1369. } else {
  1370. sqp = to_msqp(to_mqp(sqp->roce_v2_gsi));
  1371. sqp->qp.flags |= MLX4_IB_ROCE_V2_GSI_QP;
  1372. }
  1373. init_attr->create_flags &= ~MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1374. }
  1375. }
  1376. return ibqp;
  1377. }
  1378. static int _mlx4_ib_destroy_qp(struct ib_qp *qp)
  1379. {
  1380. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  1381. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1382. if (is_qp0(dev, mqp))
  1383. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  1384. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI &&
  1385. dev->qp1_proxy[mqp->port - 1] == mqp) {
  1386. mutex_lock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1387. dev->qp1_proxy[mqp->port - 1] = NULL;
  1388. mutex_unlock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1389. }
  1390. if (mqp->counter_index)
  1391. mlx4_ib_free_qp_counter(dev, mqp);
  1392. if (qp->rwq_ind_tbl) {
  1393. destroy_qp_rss(dev, mqp);
  1394. } else {
  1395. struct mlx4_ib_pd *pd;
  1396. pd = get_pd(mqp);
  1397. destroy_qp_common(dev, mqp, MLX4_IB_QP_SRC, !!pd->ibpd.uobject);
  1398. }
  1399. if (is_sqp(dev, mqp))
  1400. kfree(to_msqp(mqp));
  1401. else
  1402. kfree(mqp);
  1403. return 0;
  1404. }
  1405. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  1406. {
  1407. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1408. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  1409. struct mlx4_ib_sqp *sqp = to_msqp(mqp);
  1410. if (sqp->roce_v2_gsi)
  1411. ib_destroy_qp(sqp->roce_v2_gsi);
  1412. }
  1413. return _mlx4_ib_destroy_qp(qp);
  1414. }
  1415. static int to_mlx4_st(struct mlx4_ib_dev *dev, enum mlx4_ib_qp_type type)
  1416. {
  1417. switch (type) {
  1418. case MLX4_IB_QPT_RC: return MLX4_QP_ST_RC;
  1419. case MLX4_IB_QPT_UC: return MLX4_QP_ST_UC;
  1420. case MLX4_IB_QPT_UD: return MLX4_QP_ST_UD;
  1421. case MLX4_IB_QPT_XRC_INI:
  1422. case MLX4_IB_QPT_XRC_TGT: return MLX4_QP_ST_XRC;
  1423. case MLX4_IB_QPT_SMI:
  1424. case MLX4_IB_QPT_GSI:
  1425. case MLX4_IB_QPT_RAW_PACKET: return MLX4_QP_ST_MLX;
  1426. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  1427. case MLX4_IB_QPT_TUN_SMI_OWNER: return (mlx4_is_mfunc(dev->dev) ?
  1428. MLX4_QP_ST_MLX : -1);
  1429. case MLX4_IB_QPT_PROXY_SMI:
  1430. case MLX4_IB_QPT_TUN_SMI:
  1431. case MLX4_IB_QPT_PROXY_GSI:
  1432. case MLX4_IB_QPT_TUN_GSI: return (mlx4_is_mfunc(dev->dev) ?
  1433. MLX4_QP_ST_UD : -1);
  1434. default: return -1;
  1435. }
  1436. }
  1437. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  1438. int attr_mask)
  1439. {
  1440. u8 dest_rd_atomic;
  1441. u32 access_flags;
  1442. u32 hw_access_flags = 0;
  1443. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1444. dest_rd_atomic = attr->max_dest_rd_atomic;
  1445. else
  1446. dest_rd_atomic = qp->resp_depth;
  1447. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1448. access_flags = attr->qp_access_flags;
  1449. else
  1450. access_flags = qp->atomic_rd_en;
  1451. if (!dest_rd_atomic)
  1452. access_flags &= IB_ACCESS_REMOTE_WRITE;
  1453. if (access_flags & IB_ACCESS_REMOTE_READ)
  1454. hw_access_flags |= MLX4_QP_BIT_RRE;
  1455. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  1456. hw_access_flags |= MLX4_QP_BIT_RAE;
  1457. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  1458. hw_access_flags |= MLX4_QP_BIT_RWE;
  1459. return cpu_to_be32(hw_access_flags);
  1460. }
  1461. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  1462. int attr_mask)
  1463. {
  1464. if (attr_mask & IB_QP_PKEY_INDEX)
  1465. sqp->pkey_index = attr->pkey_index;
  1466. if (attr_mask & IB_QP_QKEY)
  1467. sqp->qkey = attr->qkey;
  1468. if (attr_mask & IB_QP_SQ_PSN)
  1469. sqp->send_psn = attr->sq_psn;
  1470. }
  1471. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  1472. {
  1473. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  1474. }
  1475. static int _mlx4_set_path(struct mlx4_ib_dev *dev,
  1476. const struct rdma_ah_attr *ah,
  1477. u64 smac, u16 vlan_tag, struct mlx4_qp_path *path,
  1478. struct mlx4_roce_smac_vlan_info *smac_info, u8 port)
  1479. {
  1480. int vidx;
  1481. int smac_index;
  1482. int err;
  1483. path->grh_mylmc = rdma_ah_get_path_bits(ah) & 0x7f;
  1484. path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
  1485. if (rdma_ah_get_static_rate(ah)) {
  1486. path->static_rate = rdma_ah_get_static_rate(ah) +
  1487. MLX4_STAT_RATE_OFFSET;
  1488. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  1489. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  1490. --path->static_rate;
  1491. } else
  1492. path->static_rate = 0;
  1493. if (rdma_ah_get_ah_flags(ah) & IB_AH_GRH) {
  1494. const struct ib_global_route *grh = rdma_ah_read_grh(ah);
  1495. int real_sgid_index =
  1496. mlx4_ib_gid_index_to_real_index(dev, grh->sgid_attr);
  1497. if (real_sgid_index < 0)
  1498. return real_sgid_index;
  1499. if (real_sgid_index >= dev->dev->caps.gid_table_len[port]) {
  1500. pr_err("sgid_index (%u) too large. max is %d\n",
  1501. real_sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  1502. return -1;
  1503. }
  1504. path->grh_mylmc |= 1 << 7;
  1505. path->mgid_index = real_sgid_index;
  1506. path->hop_limit = grh->hop_limit;
  1507. path->tclass_flowlabel =
  1508. cpu_to_be32((grh->traffic_class << 20) |
  1509. (grh->flow_label));
  1510. memcpy(path->rgid, grh->dgid.raw, 16);
  1511. }
  1512. if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
  1513. if (!(rdma_ah_get_ah_flags(ah) & IB_AH_GRH))
  1514. return -1;
  1515. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1516. ((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 7) << 3);
  1517. path->feup |= MLX4_FEUP_FORCE_ETH_UP;
  1518. if (vlan_tag < 0x1000) {
  1519. if (smac_info->vid < 0x1000) {
  1520. /* both valid vlan ids */
  1521. if (smac_info->vid != vlan_tag) {
  1522. /* different VIDs. unreg old and reg new */
  1523. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1524. if (err)
  1525. return err;
  1526. smac_info->candidate_vid = vlan_tag;
  1527. smac_info->candidate_vlan_index = vidx;
  1528. smac_info->candidate_vlan_port = port;
  1529. smac_info->update_vid = 1;
  1530. path->vlan_index = vidx;
  1531. } else {
  1532. path->vlan_index = smac_info->vlan_index;
  1533. }
  1534. } else {
  1535. /* no current vlan tag in qp */
  1536. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1537. if (err)
  1538. return err;
  1539. smac_info->candidate_vid = vlan_tag;
  1540. smac_info->candidate_vlan_index = vidx;
  1541. smac_info->candidate_vlan_port = port;
  1542. smac_info->update_vid = 1;
  1543. path->vlan_index = vidx;
  1544. }
  1545. path->feup |= MLX4_FVL_FORCE_ETH_VLAN;
  1546. path->fl = 1 << 6;
  1547. } else {
  1548. /* have current vlan tag. unregister it at modify-qp success */
  1549. if (smac_info->vid < 0x1000) {
  1550. smac_info->candidate_vid = 0xFFFF;
  1551. smac_info->update_vid = 1;
  1552. }
  1553. }
  1554. /* get smac_index for RoCE use.
  1555. * If no smac was yet assigned, register one.
  1556. * If one was already assigned, but the new mac differs,
  1557. * unregister the old one and register the new one.
  1558. */
  1559. if ((!smac_info->smac && !smac_info->smac_port) ||
  1560. smac_info->smac != smac) {
  1561. /* register candidate now, unreg if needed, after success */
  1562. smac_index = mlx4_register_mac(dev->dev, port, smac);
  1563. if (smac_index >= 0) {
  1564. smac_info->candidate_smac_index = smac_index;
  1565. smac_info->candidate_smac = smac;
  1566. smac_info->candidate_smac_port = port;
  1567. } else {
  1568. return -EINVAL;
  1569. }
  1570. } else {
  1571. smac_index = smac_info->smac_index;
  1572. }
  1573. memcpy(path->dmac, ah->roce.dmac, 6);
  1574. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  1575. /* put MAC table smac index for IBoE */
  1576. path->grh_mylmc = (u8) (smac_index) | 0x80;
  1577. } else {
  1578. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1579. ((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 0xf) << 2);
  1580. }
  1581. return 0;
  1582. }
  1583. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_qp_attr *qp,
  1584. enum ib_qp_attr_mask qp_attr_mask,
  1585. struct mlx4_ib_qp *mqp,
  1586. struct mlx4_qp_path *path, u8 port,
  1587. u16 vlan_id, u8 *smac)
  1588. {
  1589. return _mlx4_set_path(dev, &qp->ah_attr,
  1590. mlx4_mac_to_u64(smac),
  1591. vlan_id,
  1592. path, &mqp->pri, port);
  1593. }
  1594. static int mlx4_set_alt_path(struct mlx4_ib_dev *dev,
  1595. const struct ib_qp_attr *qp,
  1596. enum ib_qp_attr_mask qp_attr_mask,
  1597. struct mlx4_ib_qp *mqp,
  1598. struct mlx4_qp_path *path, u8 port)
  1599. {
  1600. return _mlx4_set_path(dev, &qp->alt_ah_attr,
  1601. 0,
  1602. 0xffff,
  1603. path, &mqp->alt, port);
  1604. }
  1605. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1606. {
  1607. struct mlx4_ib_gid_entry *ge, *tmp;
  1608. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1609. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  1610. ge->added = 1;
  1611. ge->port = qp->port;
  1612. }
  1613. }
  1614. }
  1615. static int handle_eth_ud_smac_index(struct mlx4_ib_dev *dev,
  1616. struct mlx4_ib_qp *qp,
  1617. struct mlx4_qp_context *context)
  1618. {
  1619. u64 u64_mac;
  1620. int smac_index;
  1621. u64_mac = atomic64_read(&dev->iboe.mac[qp->port - 1]);
  1622. context->pri_path.sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | ((qp->port - 1) << 6);
  1623. if (!qp->pri.smac && !qp->pri.smac_port) {
  1624. smac_index = mlx4_register_mac(dev->dev, qp->port, u64_mac);
  1625. if (smac_index >= 0) {
  1626. qp->pri.candidate_smac_index = smac_index;
  1627. qp->pri.candidate_smac = u64_mac;
  1628. qp->pri.candidate_smac_port = qp->port;
  1629. context->pri_path.grh_mylmc = 0x80 | (u8) smac_index;
  1630. } else {
  1631. return -ENOENT;
  1632. }
  1633. }
  1634. return 0;
  1635. }
  1636. static int create_qp_lb_counter(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1637. {
  1638. struct counter_index *new_counter_index;
  1639. int err;
  1640. u32 tmp_idx;
  1641. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) !=
  1642. IB_LINK_LAYER_ETHERNET ||
  1643. !(qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) ||
  1644. !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_LB_SRC_CHK))
  1645. return 0;
  1646. err = mlx4_counter_alloc(dev->dev, &tmp_idx, MLX4_RES_USAGE_DRIVER);
  1647. if (err)
  1648. return err;
  1649. new_counter_index = kmalloc(sizeof(*new_counter_index), GFP_KERNEL);
  1650. if (!new_counter_index) {
  1651. mlx4_counter_free(dev->dev, tmp_idx);
  1652. return -ENOMEM;
  1653. }
  1654. new_counter_index->index = tmp_idx;
  1655. new_counter_index->allocated = 1;
  1656. qp->counter_index = new_counter_index;
  1657. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  1658. list_add_tail(&new_counter_index->list,
  1659. &dev->counters_table[qp->port - 1].counters_list);
  1660. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  1661. return 0;
  1662. }
  1663. enum {
  1664. MLX4_QPC_ROCE_MODE_1 = 0,
  1665. MLX4_QPC_ROCE_MODE_2 = 2,
  1666. MLX4_QPC_ROCE_MODE_UNDEFINED = 0xff
  1667. };
  1668. static u8 gid_type_to_qpc(enum ib_gid_type gid_type)
  1669. {
  1670. switch (gid_type) {
  1671. case IB_GID_TYPE_ROCE:
  1672. return MLX4_QPC_ROCE_MODE_1;
  1673. case IB_GID_TYPE_ROCE_UDP_ENCAP:
  1674. return MLX4_QPC_ROCE_MODE_2;
  1675. default:
  1676. return MLX4_QPC_ROCE_MODE_UNDEFINED;
  1677. }
  1678. }
  1679. /*
  1680. * Go over all RSS QP's childes (WQs) and apply their HW state according to
  1681. * their logic state if the RSS QP is the first RSS QP associated for the WQ.
  1682. */
  1683. static int bringup_rss_rwqs(struct ib_rwq_ind_table *ind_tbl, u8 port_num)
  1684. {
  1685. int err = 0;
  1686. int i;
  1687. for (i = 0; i < (1 << ind_tbl->log_ind_tbl_size); i++) {
  1688. struct ib_wq *ibwq = ind_tbl->ind_tbl[i];
  1689. struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
  1690. mutex_lock(&wq->mutex);
  1691. /* Mlx4_ib restrictions:
  1692. * WQ's is associated to a port according to the RSS QP it is
  1693. * associates to.
  1694. * In case the WQ is associated to a different port by another
  1695. * RSS QP, return a failure.
  1696. */
  1697. if ((wq->rss_usecnt > 0) && (wq->port != port_num)) {
  1698. err = -EINVAL;
  1699. mutex_unlock(&wq->mutex);
  1700. break;
  1701. }
  1702. wq->port = port_num;
  1703. if ((wq->rss_usecnt == 0) && (ibwq->state == IB_WQS_RDY)) {
  1704. err = _mlx4_ib_modify_wq(ibwq, IB_WQS_RDY);
  1705. if (err) {
  1706. mutex_unlock(&wq->mutex);
  1707. break;
  1708. }
  1709. }
  1710. wq->rss_usecnt++;
  1711. mutex_unlock(&wq->mutex);
  1712. }
  1713. if (i && err) {
  1714. int j;
  1715. for (j = (i - 1); j >= 0; j--) {
  1716. struct ib_wq *ibwq = ind_tbl->ind_tbl[j];
  1717. struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
  1718. mutex_lock(&wq->mutex);
  1719. if ((wq->rss_usecnt == 1) &&
  1720. (ibwq->state == IB_WQS_RDY))
  1721. if (_mlx4_ib_modify_wq(ibwq, IB_WQS_RESET))
  1722. pr_warn("failed to reverse WQN=0x%06x\n",
  1723. ibwq->wq_num);
  1724. wq->rss_usecnt--;
  1725. mutex_unlock(&wq->mutex);
  1726. }
  1727. }
  1728. return err;
  1729. }
  1730. static void bring_down_rss_rwqs(struct ib_rwq_ind_table *ind_tbl)
  1731. {
  1732. int i;
  1733. for (i = 0; i < (1 << ind_tbl->log_ind_tbl_size); i++) {
  1734. struct ib_wq *ibwq = ind_tbl->ind_tbl[i];
  1735. struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
  1736. mutex_lock(&wq->mutex);
  1737. if ((wq->rss_usecnt == 1) && (ibwq->state == IB_WQS_RDY))
  1738. if (_mlx4_ib_modify_wq(ibwq, IB_WQS_RESET))
  1739. pr_warn("failed to reverse WQN=%x\n",
  1740. ibwq->wq_num);
  1741. wq->rss_usecnt--;
  1742. mutex_unlock(&wq->mutex);
  1743. }
  1744. }
  1745. static void fill_qp_rss_context(struct mlx4_qp_context *context,
  1746. struct mlx4_ib_qp *qp)
  1747. {
  1748. struct mlx4_rss_context *rss_context;
  1749. rss_context = (void *)context + offsetof(struct mlx4_qp_context,
  1750. pri_path) + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
  1751. rss_context->base_qpn = cpu_to_be32(qp->rss_ctx->base_qpn_tbl_sz);
  1752. rss_context->default_qpn =
  1753. cpu_to_be32(qp->rss_ctx->base_qpn_tbl_sz & 0xffffff);
  1754. if (qp->rss_ctx->flags & (MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6))
  1755. rss_context->base_qpn_udp = rss_context->default_qpn;
  1756. rss_context->flags = qp->rss_ctx->flags;
  1757. /* Currently support just toeplitz */
  1758. rss_context->hash_fn = MLX4_RSS_HASH_TOP;
  1759. memcpy(rss_context->rss_key, qp->rss_ctx->rss_key,
  1760. MLX4_EN_RSS_KEY_SIZE);
  1761. }
  1762. static int __mlx4_ib_modify_qp(void *src, enum mlx4_ib_source_type src_type,
  1763. const struct ib_qp_attr *attr, int attr_mask,
  1764. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  1765. {
  1766. struct ib_uobject *ibuobject;
  1767. struct ib_srq *ibsrq;
  1768. const struct ib_gid_attr *gid_attr = NULL;
  1769. struct ib_rwq_ind_table *rwq_ind_tbl;
  1770. enum ib_qp_type qp_type;
  1771. struct mlx4_ib_dev *dev;
  1772. struct mlx4_ib_qp *qp;
  1773. struct mlx4_ib_pd *pd;
  1774. struct mlx4_ib_cq *send_cq, *recv_cq;
  1775. struct mlx4_qp_context *context;
  1776. enum mlx4_qp_optpar optpar = 0;
  1777. int sqd_event;
  1778. int steer_qp = 0;
  1779. int err = -EINVAL;
  1780. int counter_index;
  1781. if (src_type == MLX4_IB_RWQ_SRC) {
  1782. struct ib_wq *ibwq;
  1783. ibwq = (struct ib_wq *)src;
  1784. ibuobject = ibwq->uobject;
  1785. ibsrq = NULL;
  1786. rwq_ind_tbl = NULL;
  1787. qp_type = IB_QPT_RAW_PACKET;
  1788. qp = to_mqp((struct ib_qp *)ibwq);
  1789. dev = to_mdev(ibwq->device);
  1790. pd = to_mpd(ibwq->pd);
  1791. } else {
  1792. struct ib_qp *ibqp;
  1793. ibqp = (struct ib_qp *)src;
  1794. ibuobject = ibqp->uobject;
  1795. ibsrq = ibqp->srq;
  1796. rwq_ind_tbl = ibqp->rwq_ind_tbl;
  1797. qp_type = ibqp->qp_type;
  1798. qp = to_mqp(ibqp);
  1799. dev = to_mdev(ibqp->device);
  1800. pd = get_pd(qp);
  1801. }
  1802. /* APM is not supported under RoCE */
  1803. if (attr_mask & IB_QP_ALT_PATH &&
  1804. rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  1805. IB_LINK_LAYER_ETHERNET)
  1806. return -ENOTSUPP;
  1807. context = kzalloc(sizeof *context, GFP_KERNEL);
  1808. if (!context)
  1809. return -ENOMEM;
  1810. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  1811. (to_mlx4_st(dev, qp->mlx4_ib_qp_type) << 16));
  1812. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  1813. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1814. else {
  1815. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  1816. switch (attr->path_mig_state) {
  1817. case IB_MIG_MIGRATED:
  1818. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1819. break;
  1820. case IB_MIG_REARM:
  1821. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  1822. break;
  1823. case IB_MIG_ARMED:
  1824. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  1825. break;
  1826. }
  1827. }
  1828. if (qp->inl_recv_sz)
  1829. context->param3 |= cpu_to_be32(1 << 25);
  1830. if (qp->flags & MLX4_IB_QP_SCATTER_FCS)
  1831. context->param3 |= cpu_to_be32(1 << 29);
  1832. if (qp_type == IB_QPT_GSI || qp_type == IB_QPT_SMI)
  1833. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  1834. else if (qp_type == IB_QPT_RAW_PACKET)
  1835. context->mtu_msgmax = (MLX4_RAW_QP_MTU << 5) | MLX4_RAW_QP_MSGMAX;
  1836. else if (qp_type == IB_QPT_UD) {
  1837. if (qp->flags & MLX4_IB_QP_LSO)
  1838. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  1839. ilog2(dev->dev->caps.max_gso_sz);
  1840. else
  1841. context->mtu_msgmax = (IB_MTU_4096 << 5) | 13;
  1842. } else if (attr_mask & IB_QP_PATH_MTU) {
  1843. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  1844. pr_err("path MTU (%u) is invalid\n",
  1845. attr->path_mtu);
  1846. goto out;
  1847. }
  1848. context->mtu_msgmax = (attr->path_mtu << 5) |
  1849. ilog2(dev->dev->caps.max_msg_sz);
  1850. }
  1851. if (!rwq_ind_tbl) { /* PRM RSS receive side should be left zeros */
  1852. if (qp->rq.wqe_cnt)
  1853. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  1854. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  1855. }
  1856. if (qp->sq.wqe_cnt)
  1857. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  1858. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  1859. if (new_state == IB_QPS_RESET && qp->counter_index)
  1860. mlx4_ib_free_qp_counter(dev, qp);
  1861. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1862. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  1863. context->xrcd = cpu_to_be32((u32) qp->xrcdn);
  1864. if (qp_type == IB_QPT_RAW_PACKET)
  1865. context->param3 |= cpu_to_be32(1 << 30);
  1866. }
  1867. if (ibuobject)
  1868. context->usr_page = cpu_to_be32(
  1869. mlx4_to_hw_uar_index(dev->dev,
  1870. to_mucontext(ibuobject->context)
  1871. ->uar.index));
  1872. else
  1873. context->usr_page = cpu_to_be32(
  1874. mlx4_to_hw_uar_index(dev->dev, dev->priv_uar.index));
  1875. if (attr_mask & IB_QP_DEST_QPN)
  1876. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  1877. if (attr_mask & IB_QP_PORT) {
  1878. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  1879. !(attr_mask & IB_QP_AV)) {
  1880. mlx4_set_sched(&context->pri_path, attr->port_num);
  1881. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  1882. }
  1883. }
  1884. if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
  1885. err = create_qp_lb_counter(dev, qp);
  1886. if (err)
  1887. goto out;
  1888. counter_index =
  1889. dev->counters_table[qp->port - 1].default_counter;
  1890. if (qp->counter_index)
  1891. counter_index = qp->counter_index->index;
  1892. if (counter_index != -1) {
  1893. context->pri_path.counter_index = counter_index;
  1894. optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
  1895. if (qp->counter_index) {
  1896. context->pri_path.fl |=
  1897. MLX4_FL_ETH_SRC_CHECK_MC_LB;
  1898. context->pri_path.vlan_control |=
  1899. MLX4_CTRL_ETH_SRC_CHECK_IF_COUNTER;
  1900. }
  1901. } else
  1902. context->pri_path.counter_index =
  1903. MLX4_SINK_COUNTER_INDEX(dev->dev);
  1904. if (qp->flags & MLX4_IB_QP_NETIF) {
  1905. mlx4_ib_steer_qp_reg(dev, qp, 1);
  1906. steer_qp = 1;
  1907. }
  1908. if (qp_type == IB_QPT_GSI) {
  1909. enum ib_gid_type gid_type = qp->flags & MLX4_IB_ROCE_V2_GSI_QP ?
  1910. IB_GID_TYPE_ROCE_UDP_ENCAP : IB_GID_TYPE_ROCE;
  1911. u8 qpc_roce_mode = gid_type_to_qpc(gid_type);
  1912. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1913. }
  1914. }
  1915. if (attr_mask & IB_QP_PKEY_INDEX) {
  1916. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1917. context->pri_path.disable_pkey_check = 0x40;
  1918. context->pri_path.pkey_index = attr->pkey_index;
  1919. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  1920. }
  1921. if (attr_mask & IB_QP_AV) {
  1922. u8 port_num = mlx4_is_bonded(dev->dev) ? 1 :
  1923. attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1924. u16 vlan = 0xffff;
  1925. u8 smac[ETH_ALEN];
  1926. int is_eth =
  1927. rdma_cap_eth_ah(&dev->ib_dev, port_num) &&
  1928. rdma_ah_get_ah_flags(&attr->ah_attr) & IB_AH_GRH;
  1929. if (is_eth) {
  1930. gid_attr = attr->ah_attr.grh.sgid_attr;
  1931. vlan = rdma_vlan_dev_vlan_id(gid_attr->ndev);
  1932. memcpy(smac, gid_attr->ndev->dev_addr, ETH_ALEN);
  1933. }
  1934. if (mlx4_set_path(dev, attr, attr_mask, qp, &context->pri_path,
  1935. port_num, vlan, smac))
  1936. goto out;
  1937. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  1938. MLX4_QP_OPTPAR_SCHED_QUEUE);
  1939. if (is_eth &&
  1940. (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR)) {
  1941. u8 qpc_roce_mode = gid_type_to_qpc(gid_attr->gid_type);
  1942. if (qpc_roce_mode == MLX4_QPC_ROCE_MODE_UNDEFINED) {
  1943. err = -EINVAL;
  1944. goto out;
  1945. }
  1946. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1947. }
  1948. }
  1949. if (attr_mask & IB_QP_TIMEOUT) {
  1950. context->pri_path.ackto |= attr->timeout << 3;
  1951. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  1952. }
  1953. if (attr_mask & IB_QP_ALT_PATH) {
  1954. if (attr->alt_port_num == 0 ||
  1955. attr->alt_port_num > dev->dev->caps.num_ports)
  1956. goto out;
  1957. if (attr->alt_pkey_index >=
  1958. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  1959. goto out;
  1960. if (mlx4_set_alt_path(dev, attr, attr_mask, qp,
  1961. &context->alt_path,
  1962. attr->alt_port_num))
  1963. goto out;
  1964. context->alt_path.pkey_index = attr->alt_pkey_index;
  1965. context->alt_path.ackto = attr->alt_timeout << 3;
  1966. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  1967. }
  1968. context->pd = cpu_to_be32(pd->pdn);
  1969. if (!rwq_ind_tbl) {
  1970. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  1971. get_cqs(qp, src_type, &send_cq, &recv_cq);
  1972. } else { /* Set dummy CQs to be compatible with HV and PRM */
  1973. send_cq = to_mcq(rwq_ind_tbl->ind_tbl[0]->cq);
  1974. recv_cq = send_cq;
  1975. }
  1976. context->cqn_send = cpu_to_be32(send_cq->mcq.cqn);
  1977. context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn);
  1978. /* Set "fast registration enabled" for all kernel QPs */
  1979. if (!ibuobject)
  1980. context->params1 |= cpu_to_be32(1 << 11);
  1981. if (attr_mask & IB_QP_RNR_RETRY) {
  1982. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  1983. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  1984. }
  1985. if (attr_mask & IB_QP_RETRY_CNT) {
  1986. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  1987. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  1988. }
  1989. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  1990. if (attr->max_rd_atomic)
  1991. context->params1 |=
  1992. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  1993. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  1994. }
  1995. if (attr_mask & IB_QP_SQ_PSN)
  1996. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  1997. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  1998. if (attr->max_dest_rd_atomic)
  1999. context->params2 |=
  2000. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  2001. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  2002. }
  2003. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  2004. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  2005. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  2006. }
  2007. if (ibsrq)
  2008. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  2009. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  2010. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  2011. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  2012. }
  2013. if (attr_mask & IB_QP_RQ_PSN)
  2014. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  2015. /* proxy and tunnel qp qkeys will be changed in modify-qp wrappers */
  2016. if (attr_mask & IB_QP_QKEY) {
  2017. if (qp->mlx4_ib_qp_type &
  2018. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))
  2019. context->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  2020. else {
  2021. if (mlx4_is_mfunc(dev->dev) &&
  2022. !(qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) &&
  2023. (attr->qkey & MLX4_RESERVED_QKEY_MASK) ==
  2024. MLX4_RESERVED_QKEY_BASE) {
  2025. pr_err("Cannot use reserved QKEY"
  2026. " 0x%x (range 0xffff0000..0xffffffff"
  2027. " is reserved)\n", attr->qkey);
  2028. err = -EINVAL;
  2029. goto out;
  2030. }
  2031. context->qkey = cpu_to_be32(attr->qkey);
  2032. }
  2033. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  2034. }
  2035. if (ibsrq)
  2036. context->srqn = cpu_to_be32(1 << 24 |
  2037. to_msrq(ibsrq)->msrq.srqn);
  2038. if (qp->rq.wqe_cnt &&
  2039. cur_state == IB_QPS_RESET &&
  2040. new_state == IB_QPS_INIT)
  2041. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  2042. if (cur_state == IB_QPS_INIT &&
  2043. new_state == IB_QPS_RTR &&
  2044. (qp_type == IB_QPT_GSI || qp_type == IB_QPT_SMI ||
  2045. qp_type == IB_QPT_UD || qp_type == IB_QPT_RAW_PACKET)) {
  2046. context->pri_path.sched_queue = (qp->port - 1) << 6;
  2047. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  2048. qp->mlx4_ib_qp_type &
  2049. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) {
  2050. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  2051. if (qp->mlx4_ib_qp_type != MLX4_IB_QPT_SMI)
  2052. context->pri_path.fl = 0x80;
  2053. } else {
  2054. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  2055. context->pri_path.fl = 0x80;
  2056. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  2057. }
  2058. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  2059. IB_LINK_LAYER_ETHERNET) {
  2060. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI ||
  2061. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI)
  2062. context->pri_path.feup = 1 << 7; /* don't fsm */
  2063. /* handle smac_index */
  2064. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_UD ||
  2065. qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI ||
  2066. qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI) {
  2067. err = handle_eth_ud_smac_index(dev, qp, context);
  2068. if (err) {
  2069. err = -EINVAL;
  2070. goto out;
  2071. }
  2072. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  2073. dev->qp1_proxy[qp->port - 1] = qp;
  2074. }
  2075. }
  2076. }
  2077. if (qp_type == IB_QPT_RAW_PACKET) {
  2078. context->pri_path.ackto = (context->pri_path.ackto & 0xf8) |
  2079. MLX4_IB_LINK_TYPE_ETH;
  2080. if (dev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  2081. /* set QP to receive both tunneled & non-tunneled packets */
  2082. if (!rwq_ind_tbl)
  2083. context->srqn = cpu_to_be32(7 << 28);
  2084. }
  2085. }
  2086. if (qp_type == IB_QPT_UD && (new_state == IB_QPS_RTR)) {
  2087. int is_eth = rdma_port_get_link_layer(
  2088. &dev->ib_dev, qp->port) ==
  2089. IB_LINK_LAYER_ETHERNET;
  2090. if (is_eth) {
  2091. context->pri_path.ackto = MLX4_IB_LINK_TYPE_ETH;
  2092. optpar |= MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH;
  2093. }
  2094. }
  2095. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  2096. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  2097. sqd_event = 1;
  2098. else
  2099. sqd_event = 0;
  2100. if (!ibuobject &&
  2101. cur_state == IB_QPS_RESET &&
  2102. new_state == IB_QPS_INIT)
  2103. context->rlkey_roce_mode |= (1 << 4);
  2104. /*
  2105. * Before passing a kernel QP to the HW, make sure that the
  2106. * ownership bits of the send queue are set and the SQ
  2107. * headroom is stamped so that the hardware doesn't start
  2108. * processing stale work requests.
  2109. */
  2110. if (!ibuobject &&
  2111. cur_state == IB_QPS_RESET &&
  2112. new_state == IB_QPS_INIT) {
  2113. struct mlx4_wqe_ctrl_seg *ctrl;
  2114. int i;
  2115. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  2116. ctrl = get_send_wqe(qp, i);
  2117. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  2118. ctrl->qpn_vlan.fence_size =
  2119. 1 << (qp->sq.wqe_shift - 4);
  2120. stamp_send_wqe(qp, i);
  2121. }
  2122. }
  2123. if (rwq_ind_tbl &&
  2124. cur_state == IB_QPS_RESET &&
  2125. new_state == IB_QPS_INIT) {
  2126. fill_qp_rss_context(context, qp);
  2127. context->flags |= cpu_to_be32(1 << MLX4_RSS_QPC_FLAG_OFFSET);
  2128. }
  2129. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  2130. to_mlx4_state(new_state), context, optpar,
  2131. sqd_event, &qp->mqp);
  2132. if (err)
  2133. goto out;
  2134. qp->state = new_state;
  2135. if (attr_mask & IB_QP_ACCESS_FLAGS)
  2136. qp->atomic_rd_en = attr->qp_access_flags;
  2137. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  2138. qp->resp_depth = attr->max_dest_rd_atomic;
  2139. if (attr_mask & IB_QP_PORT) {
  2140. qp->port = attr->port_num;
  2141. update_mcg_macs(dev, qp);
  2142. }
  2143. if (attr_mask & IB_QP_ALT_PATH)
  2144. qp->alt_port = attr->alt_port_num;
  2145. if (is_sqp(dev, qp))
  2146. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  2147. /*
  2148. * If we moved QP0 to RTR, bring the IB link up; if we moved
  2149. * QP0 to RESET or ERROR, bring the link back down.
  2150. */
  2151. if (is_qp0(dev, qp)) {
  2152. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  2153. if (mlx4_INIT_PORT(dev->dev, qp->port))
  2154. pr_warn("INIT_PORT failed for port %d\n",
  2155. qp->port);
  2156. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  2157. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  2158. mlx4_CLOSE_PORT(dev->dev, qp->port);
  2159. }
  2160. /*
  2161. * If we moved a kernel QP to RESET, clean up all old CQ
  2162. * entries and reinitialize the QP.
  2163. */
  2164. if (new_state == IB_QPS_RESET) {
  2165. if (!ibuobject) {
  2166. mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  2167. ibsrq ? to_msrq(ibsrq) : NULL);
  2168. if (send_cq != recv_cq)
  2169. mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  2170. qp->rq.head = 0;
  2171. qp->rq.tail = 0;
  2172. qp->sq.head = 0;
  2173. qp->sq.tail = 0;
  2174. qp->sq_next_wqe = 0;
  2175. if (qp->rq.wqe_cnt)
  2176. *qp->db.db = 0;
  2177. if (qp->flags & MLX4_IB_QP_NETIF)
  2178. mlx4_ib_steer_qp_reg(dev, qp, 0);
  2179. }
  2180. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  2181. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  2182. qp->pri.smac = 0;
  2183. qp->pri.smac_port = 0;
  2184. }
  2185. if (qp->alt.smac) {
  2186. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  2187. qp->alt.smac = 0;
  2188. }
  2189. if (qp->pri.vid < 0x1000) {
  2190. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  2191. qp->pri.vid = 0xFFFF;
  2192. qp->pri.candidate_vid = 0xFFFF;
  2193. qp->pri.update_vid = 0;
  2194. }
  2195. if (qp->alt.vid < 0x1000) {
  2196. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  2197. qp->alt.vid = 0xFFFF;
  2198. qp->alt.candidate_vid = 0xFFFF;
  2199. qp->alt.update_vid = 0;
  2200. }
  2201. }
  2202. out:
  2203. if (err && qp->counter_index)
  2204. mlx4_ib_free_qp_counter(dev, qp);
  2205. if (err && steer_qp)
  2206. mlx4_ib_steer_qp_reg(dev, qp, 0);
  2207. kfree(context);
  2208. if (qp->pri.candidate_smac ||
  2209. (!qp->pri.candidate_smac && qp->pri.candidate_smac_port)) {
  2210. if (err) {
  2211. mlx4_unregister_mac(dev->dev, qp->pri.candidate_smac_port, qp->pri.candidate_smac);
  2212. } else {
  2213. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port))
  2214. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  2215. qp->pri.smac = qp->pri.candidate_smac;
  2216. qp->pri.smac_index = qp->pri.candidate_smac_index;
  2217. qp->pri.smac_port = qp->pri.candidate_smac_port;
  2218. }
  2219. qp->pri.candidate_smac = 0;
  2220. qp->pri.candidate_smac_index = 0;
  2221. qp->pri.candidate_smac_port = 0;
  2222. }
  2223. if (qp->alt.candidate_smac) {
  2224. if (err) {
  2225. mlx4_unregister_mac(dev->dev, qp->alt.candidate_smac_port, qp->alt.candidate_smac);
  2226. } else {
  2227. if (qp->alt.smac)
  2228. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  2229. qp->alt.smac = qp->alt.candidate_smac;
  2230. qp->alt.smac_index = qp->alt.candidate_smac_index;
  2231. qp->alt.smac_port = qp->alt.candidate_smac_port;
  2232. }
  2233. qp->alt.candidate_smac = 0;
  2234. qp->alt.candidate_smac_index = 0;
  2235. qp->alt.candidate_smac_port = 0;
  2236. }
  2237. if (qp->pri.update_vid) {
  2238. if (err) {
  2239. if (qp->pri.candidate_vid < 0x1000)
  2240. mlx4_unregister_vlan(dev->dev, qp->pri.candidate_vlan_port,
  2241. qp->pri.candidate_vid);
  2242. } else {
  2243. if (qp->pri.vid < 0x1000)
  2244. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port,
  2245. qp->pri.vid);
  2246. qp->pri.vid = qp->pri.candidate_vid;
  2247. qp->pri.vlan_port = qp->pri.candidate_vlan_port;
  2248. qp->pri.vlan_index = qp->pri.candidate_vlan_index;
  2249. }
  2250. qp->pri.candidate_vid = 0xFFFF;
  2251. qp->pri.update_vid = 0;
  2252. }
  2253. if (qp->alt.update_vid) {
  2254. if (err) {
  2255. if (qp->alt.candidate_vid < 0x1000)
  2256. mlx4_unregister_vlan(dev->dev, qp->alt.candidate_vlan_port,
  2257. qp->alt.candidate_vid);
  2258. } else {
  2259. if (qp->alt.vid < 0x1000)
  2260. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port,
  2261. qp->alt.vid);
  2262. qp->alt.vid = qp->alt.candidate_vid;
  2263. qp->alt.vlan_port = qp->alt.candidate_vlan_port;
  2264. qp->alt.vlan_index = qp->alt.candidate_vlan_index;
  2265. }
  2266. qp->alt.candidate_vid = 0xFFFF;
  2267. qp->alt.update_vid = 0;
  2268. }
  2269. return err;
  2270. }
  2271. enum {
  2272. MLX4_IB_MODIFY_QP_RSS_SUP_ATTR_MSK = (IB_QP_STATE |
  2273. IB_QP_PORT),
  2274. };
  2275. static int _mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  2276. int attr_mask, struct ib_udata *udata)
  2277. {
  2278. enum rdma_link_layer ll = IB_LINK_LAYER_UNSPECIFIED;
  2279. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  2280. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2281. enum ib_qp_state cur_state, new_state;
  2282. int err = -EINVAL;
  2283. mutex_lock(&qp->mutex);
  2284. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  2285. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  2286. if (cur_state != new_state || cur_state != IB_QPS_RESET) {
  2287. int port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  2288. ll = rdma_port_get_link_layer(&dev->ib_dev, port);
  2289. }
  2290. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  2291. attr_mask, ll)) {
  2292. pr_debug("qpn 0x%x: invalid attribute mask specified "
  2293. "for transition %d to %d. qp_type %d,"
  2294. " attr_mask 0x%x\n",
  2295. ibqp->qp_num, cur_state, new_state,
  2296. ibqp->qp_type, attr_mask);
  2297. goto out;
  2298. }
  2299. if (ibqp->rwq_ind_tbl) {
  2300. if (!(((cur_state == IB_QPS_RESET) &&
  2301. (new_state == IB_QPS_INIT)) ||
  2302. ((cur_state == IB_QPS_INIT) &&
  2303. (new_state == IB_QPS_RTR)))) {
  2304. pr_debug("qpn 0x%x: RSS QP unsupported transition %d to %d\n",
  2305. ibqp->qp_num, cur_state, new_state);
  2306. err = -EOPNOTSUPP;
  2307. goto out;
  2308. }
  2309. if (attr_mask & ~MLX4_IB_MODIFY_QP_RSS_SUP_ATTR_MSK) {
  2310. pr_debug("qpn 0x%x: RSS QP unsupported attribute mask 0x%x for transition %d to %d\n",
  2311. ibqp->qp_num, attr_mask, cur_state, new_state);
  2312. err = -EOPNOTSUPP;
  2313. goto out;
  2314. }
  2315. }
  2316. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT)) {
  2317. if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
  2318. if ((ibqp->qp_type == IB_QPT_RC) ||
  2319. (ibqp->qp_type == IB_QPT_UD) ||
  2320. (ibqp->qp_type == IB_QPT_UC) ||
  2321. (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
  2322. (ibqp->qp_type == IB_QPT_XRC_INI)) {
  2323. attr->port_num = mlx4_ib_bond_next_port(dev);
  2324. }
  2325. } else {
  2326. /* no sense in changing port_num
  2327. * when ports are bonded */
  2328. attr_mask &= ~IB_QP_PORT;
  2329. }
  2330. }
  2331. if ((attr_mask & IB_QP_PORT) &&
  2332. (attr->port_num == 0 || attr->port_num > dev->num_ports)) {
  2333. pr_debug("qpn 0x%x: invalid port number (%d) specified "
  2334. "for transition %d to %d. qp_type %d\n",
  2335. ibqp->qp_num, attr->port_num, cur_state,
  2336. new_state, ibqp->qp_type);
  2337. goto out;
  2338. }
  2339. if ((attr_mask & IB_QP_PORT) && (ibqp->qp_type == IB_QPT_RAW_PACKET) &&
  2340. (rdma_port_get_link_layer(&dev->ib_dev, attr->port_num) !=
  2341. IB_LINK_LAYER_ETHERNET))
  2342. goto out;
  2343. if (attr_mask & IB_QP_PKEY_INDEX) {
  2344. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  2345. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p]) {
  2346. pr_debug("qpn 0x%x: invalid pkey index (%d) specified "
  2347. "for transition %d to %d. qp_type %d\n",
  2348. ibqp->qp_num, attr->pkey_index, cur_state,
  2349. new_state, ibqp->qp_type);
  2350. goto out;
  2351. }
  2352. }
  2353. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  2354. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  2355. pr_debug("qpn 0x%x: max_rd_atomic (%d) too large. "
  2356. "Transition %d to %d. qp_type %d\n",
  2357. ibqp->qp_num, attr->max_rd_atomic, cur_state,
  2358. new_state, ibqp->qp_type);
  2359. goto out;
  2360. }
  2361. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  2362. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  2363. pr_debug("qpn 0x%x: max_dest_rd_atomic (%d) too large. "
  2364. "Transition %d to %d. qp_type %d\n",
  2365. ibqp->qp_num, attr->max_dest_rd_atomic, cur_state,
  2366. new_state, ibqp->qp_type);
  2367. goto out;
  2368. }
  2369. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  2370. err = 0;
  2371. goto out;
  2372. }
  2373. if (ibqp->rwq_ind_tbl && (new_state == IB_QPS_INIT)) {
  2374. err = bringup_rss_rwqs(ibqp->rwq_ind_tbl, attr->port_num);
  2375. if (err)
  2376. goto out;
  2377. }
  2378. err = __mlx4_ib_modify_qp(ibqp, MLX4_IB_QP_SRC, attr, attr_mask,
  2379. cur_state, new_state);
  2380. if (ibqp->rwq_ind_tbl && err)
  2381. bring_down_rss_rwqs(ibqp->rwq_ind_tbl);
  2382. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT))
  2383. attr->port_num = 1;
  2384. out:
  2385. mutex_unlock(&qp->mutex);
  2386. return err;
  2387. }
  2388. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  2389. int attr_mask, struct ib_udata *udata)
  2390. {
  2391. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  2392. int ret;
  2393. ret = _mlx4_ib_modify_qp(ibqp, attr, attr_mask, udata);
  2394. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  2395. struct mlx4_ib_sqp *sqp = to_msqp(mqp);
  2396. int err = 0;
  2397. if (sqp->roce_v2_gsi)
  2398. err = ib_modify_qp(sqp->roce_v2_gsi, attr, attr_mask);
  2399. if (err)
  2400. pr_err("Failed to modify GSI QP for RoCEv2 (%d)\n",
  2401. err);
  2402. }
  2403. return ret;
  2404. }
  2405. static int vf_get_qp0_qkey(struct mlx4_dev *dev, int qpn, u32 *qkey)
  2406. {
  2407. int i;
  2408. for (i = 0; i < dev->caps.num_ports; i++) {
  2409. if (qpn == dev->caps.spec_qps[i].qp0_proxy ||
  2410. qpn == dev->caps.spec_qps[i].qp0_tunnel) {
  2411. *qkey = dev->caps.spec_qps[i].qp0_qkey;
  2412. return 0;
  2413. }
  2414. }
  2415. return -EINVAL;
  2416. }
  2417. static int build_sriov_qp0_header(struct mlx4_ib_sqp *sqp,
  2418. const struct ib_ud_wr *wr,
  2419. void *wqe, unsigned *mlx_seg_len)
  2420. {
  2421. struct mlx4_ib_dev *mdev = to_mdev(sqp->qp.ibqp.device);
  2422. struct ib_device *ib_dev = &mdev->ib_dev;
  2423. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2424. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2425. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2426. u16 pkey;
  2427. u32 qkey;
  2428. int send_size;
  2429. int header_size;
  2430. int spc;
  2431. int i;
  2432. if (wr->wr.opcode != IB_WR_SEND)
  2433. return -EINVAL;
  2434. send_size = 0;
  2435. for (i = 0; i < wr->wr.num_sge; ++i)
  2436. send_size += wr->wr.sg_list[i].length;
  2437. /* for proxy-qp0 sends, need to add in size of tunnel header */
  2438. /* for tunnel-qp0 sends, tunnel header is already in s/g list */
  2439. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER)
  2440. send_size += sizeof (struct mlx4_ib_tunnel_header);
  2441. ib_ud_header_init(send_size, 1, 0, 0, 0, 0, 0, 0, &sqp->ud_header);
  2442. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) {
  2443. sqp->ud_header.lrh.service_level =
  2444. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2445. sqp->ud_header.lrh.destination_lid =
  2446. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2447. sqp->ud_header.lrh.source_lid =
  2448. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2449. }
  2450. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2451. /* force loopback */
  2452. mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR);
  2453. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2454. sqp->ud_header.lrh.virtual_lane = 0;
  2455. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2456. ib_get_cached_pkey(ib_dev, sqp->qp.port, 0, &pkey);
  2457. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2458. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER)
  2459. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2460. else
  2461. sqp->ud_header.bth.destination_qpn =
  2462. cpu_to_be32(mdev->dev->caps.spec_qps[sqp->qp.port - 1].qp0_tunnel);
  2463. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2464. if (mlx4_is_master(mdev->dev)) {
  2465. if (mlx4_get_parav_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2466. return -EINVAL;
  2467. } else {
  2468. if (vf_get_qp0_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2469. return -EINVAL;
  2470. }
  2471. sqp->ud_header.deth.qkey = cpu_to_be32(qkey);
  2472. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.mqp.qpn);
  2473. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2474. sqp->ud_header.immediate_present = 0;
  2475. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2476. /*
  2477. * Inline data segments may not cross a 64 byte boundary. If
  2478. * our UD header is bigger than the space available up to the
  2479. * next 64 byte boundary in the WQE, use two inline data
  2480. * segments to hold the UD header.
  2481. */
  2482. spc = MLX4_INLINE_ALIGN -
  2483. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2484. if (header_size <= spc) {
  2485. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2486. memcpy(inl + 1, sqp->header_buf, header_size);
  2487. i = 1;
  2488. } else {
  2489. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2490. memcpy(inl + 1, sqp->header_buf, spc);
  2491. inl = (void *) (inl + 1) + spc;
  2492. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2493. /*
  2494. * Need a barrier here to make sure all the data is
  2495. * visible before the byte_count field is set.
  2496. * Otherwise the HCA prefetcher could grab the 64-byte
  2497. * chunk with this inline segment and get a valid (!=
  2498. * 0xffffffff) byte count but stale data, and end up
  2499. * generating a packet with bad headers.
  2500. *
  2501. * The first inline segment's byte_count field doesn't
  2502. * need a barrier, because it comes after a
  2503. * control/MLX segment and therefore is at an offset
  2504. * of 16 mod 64.
  2505. */
  2506. wmb();
  2507. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2508. i = 2;
  2509. }
  2510. *mlx_seg_len =
  2511. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2512. return 0;
  2513. }
  2514. static u8 sl_to_vl(struct mlx4_ib_dev *dev, u8 sl, int port_num)
  2515. {
  2516. union sl2vl_tbl_to_u64 tmp_vltab;
  2517. u8 vl;
  2518. if (sl > 15)
  2519. return 0xf;
  2520. tmp_vltab.sl64 = atomic64_read(&dev->sl2vl[port_num - 1]);
  2521. vl = tmp_vltab.sl8[sl >> 1];
  2522. if (sl & 1)
  2523. vl &= 0x0f;
  2524. else
  2525. vl >>= 4;
  2526. return vl;
  2527. }
  2528. static int fill_gid_by_hw_index(struct mlx4_ib_dev *ibdev, u8 port_num,
  2529. int index, union ib_gid *gid,
  2530. enum ib_gid_type *gid_type)
  2531. {
  2532. struct mlx4_ib_iboe *iboe = &ibdev->iboe;
  2533. struct mlx4_port_gid_table *port_gid_table;
  2534. unsigned long flags;
  2535. port_gid_table = &iboe->gids[port_num - 1];
  2536. spin_lock_irqsave(&iboe->lock, flags);
  2537. memcpy(gid, &port_gid_table->gids[index].gid, sizeof(*gid));
  2538. *gid_type = port_gid_table->gids[index].gid_type;
  2539. spin_unlock_irqrestore(&iboe->lock, flags);
  2540. if (rdma_is_zero_gid(gid))
  2541. return -ENOENT;
  2542. return 0;
  2543. }
  2544. #define MLX4_ROCEV2_QP1_SPORT 0xC000
  2545. static int build_mlx_header(struct mlx4_ib_sqp *sqp, const struct ib_ud_wr *wr,
  2546. void *wqe, unsigned *mlx_seg_len)
  2547. {
  2548. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  2549. struct mlx4_ib_dev *ibdev = to_mdev(ib_dev);
  2550. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2551. struct mlx4_wqe_ctrl_seg *ctrl = wqe;
  2552. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2553. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2554. union ib_gid sgid;
  2555. u16 pkey;
  2556. int send_size;
  2557. int header_size;
  2558. int spc;
  2559. int i;
  2560. int err = 0;
  2561. u16 vlan = 0xffff;
  2562. bool is_eth;
  2563. bool is_vlan = false;
  2564. bool is_grh;
  2565. bool is_udp = false;
  2566. int ip_version = 0;
  2567. send_size = 0;
  2568. for (i = 0; i < wr->wr.num_sge; ++i)
  2569. send_size += wr->wr.sg_list[i].length;
  2570. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  2571. is_grh = mlx4_ib_ah_grh_present(ah);
  2572. if (is_eth) {
  2573. enum ib_gid_type gid_type;
  2574. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2575. /* When multi-function is enabled, the ib_core gid
  2576. * indexes don't necessarily match the hw ones, so
  2577. * we must use our own cache */
  2578. err = mlx4_get_roce_gid_from_slave(to_mdev(ib_dev)->dev,
  2579. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2580. ah->av.ib.gid_index, &sgid.raw[0]);
  2581. if (err)
  2582. return err;
  2583. } else {
  2584. err = fill_gid_by_hw_index(ibdev, sqp->qp.port,
  2585. ah->av.ib.gid_index,
  2586. &sgid, &gid_type);
  2587. if (!err) {
  2588. is_udp = gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
  2589. if (is_udp) {
  2590. if (ipv6_addr_v4mapped((struct in6_addr *)&sgid))
  2591. ip_version = 4;
  2592. else
  2593. ip_version = 6;
  2594. is_grh = false;
  2595. }
  2596. } else {
  2597. return err;
  2598. }
  2599. }
  2600. if (ah->av.eth.vlan != cpu_to_be16(0xffff)) {
  2601. vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff;
  2602. is_vlan = 1;
  2603. }
  2604. }
  2605. err = ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh,
  2606. ip_version, is_udp, 0, &sqp->ud_header);
  2607. if (err)
  2608. return err;
  2609. if (!is_eth) {
  2610. sqp->ud_header.lrh.service_level =
  2611. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2612. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  2613. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2614. }
  2615. if (is_grh || (ip_version == 6)) {
  2616. sqp->ud_header.grh.traffic_class =
  2617. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2618. sqp->ud_header.grh.flow_label =
  2619. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  2620. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  2621. if (is_eth) {
  2622. memcpy(sqp->ud_header.grh.source_gid.raw, sgid.raw, 16);
  2623. } else {
  2624. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2625. /* When multi-function is enabled, the ib_core gid
  2626. * indexes don't necessarily match the hw ones, so
  2627. * we must use our own cache
  2628. */
  2629. sqp->ud_header.grh.source_gid.global.subnet_prefix =
  2630. cpu_to_be64(atomic64_read(&(to_mdev(ib_dev)->sriov.
  2631. demux[sqp->qp.port - 1].
  2632. subnet_prefix)));
  2633. sqp->ud_header.grh.source_gid.global.interface_id =
  2634. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  2635. guid_cache[ah->av.ib.gid_index];
  2636. } else {
  2637. sqp->ud_header.grh.source_gid =
  2638. ah->ibah.sgid_attr->gid;
  2639. }
  2640. }
  2641. memcpy(sqp->ud_header.grh.destination_gid.raw,
  2642. ah->av.ib.dgid, 16);
  2643. }
  2644. if (ip_version == 4) {
  2645. sqp->ud_header.ip4.tos =
  2646. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2647. sqp->ud_header.ip4.id = 0;
  2648. sqp->ud_header.ip4.frag_off = htons(IP_DF);
  2649. sqp->ud_header.ip4.ttl = ah->av.eth.hop_limit;
  2650. memcpy(&sqp->ud_header.ip4.saddr,
  2651. sgid.raw + 12, 4);
  2652. memcpy(&sqp->ud_header.ip4.daddr, ah->av.ib.dgid + 12, 4);
  2653. sqp->ud_header.ip4.check = ib_ud_ip4_csum(&sqp->ud_header);
  2654. }
  2655. if (is_udp) {
  2656. sqp->ud_header.udp.dport = htons(ROCE_V2_UDP_DPORT);
  2657. sqp->ud_header.udp.sport = htons(MLX4_ROCEV2_QP1_SPORT);
  2658. sqp->ud_header.udp.csum = 0;
  2659. }
  2660. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2661. if (!is_eth) {
  2662. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  2663. (sqp->ud_header.lrh.destination_lid ==
  2664. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  2665. (sqp->ud_header.lrh.service_level << 8));
  2666. if (ah->av.ib.port_pd & cpu_to_be32(0x80000000))
  2667. mlx->flags |= cpu_to_be32(0x1); /* force loopback */
  2668. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2669. }
  2670. switch (wr->wr.opcode) {
  2671. case IB_WR_SEND:
  2672. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2673. sqp->ud_header.immediate_present = 0;
  2674. break;
  2675. case IB_WR_SEND_WITH_IMM:
  2676. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  2677. sqp->ud_header.immediate_present = 1;
  2678. sqp->ud_header.immediate_data = wr->wr.ex.imm_data;
  2679. break;
  2680. default:
  2681. return -EINVAL;
  2682. }
  2683. if (is_eth) {
  2684. struct in6_addr in6;
  2685. u16 ether_type;
  2686. u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
  2687. ether_type = (!is_udp) ? ETH_P_IBOE:
  2688. (ip_version == 4 ? ETH_P_IP : ETH_P_IPV6);
  2689. mlx->sched_prio = cpu_to_be16(pcp);
  2690. ether_addr_copy(sqp->ud_header.eth.smac_h, ah->av.eth.s_mac);
  2691. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  2692. memcpy(&ctrl->srcrb_flags16[0], ah->av.eth.mac, 2);
  2693. memcpy(&ctrl->imm, ah->av.eth.mac + 2, 4);
  2694. memcpy(&in6, sgid.raw, sizeof(in6));
  2695. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  2696. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  2697. if (!is_vlan) {
  2698. sqp->ud_header.eth.type = cpu_to_be16(ether_type);
  2699. } else {
  2700. sqp->ud_header.vlan.type = cpu_to_be16(ether_type);
  2701. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  2702. }
  2703. } else {
  2704. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 :
  2705. sl_to_vl(to_mdev(ib_dev),
  2706. sqp->ud_header.lrh.service_level,
  2707. sqp->qp.port);
  2708. if (sqp->qp.ibqp.qp_num && sqp->ud_header.lrh.virtual_lane == 15)
  2709. return -EINVAL;
  2710. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  2711. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  2712. }
  2713. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2714. if (!sqp->qp.ibqp.qp_num)
  2715. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  2716. else
  2717. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->pkey_index, &pkey);
  2718. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2719. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2720. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2721. sqp->ud_header.deth.qkey = cpu_to_be32(wr->remote_qkey & 0x80000000 ?
  2722. sqp->qkey : wr->remote_qkey);
  2723. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  2724. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2725. if (0) {
  2726. pr_err("built UD header of size %d:\n", header_size);
  2727. for (i = 0; i < header_size / 4; ++i) {
  2728. if (i % 8 == 0)
  2729. pr_err(" [%02x] ", i * 4);
  2730. pr_cont(" %08x",
  2731. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  2732. if ((i + 1) % 8 == 0)
  2733. pr_cont("\n");
  2734. }
  2735. pr_err("\n");
  2736. }
  2737. /*
  2738. * Inline data segments may not cross a 64 byte boundary. If
  2739. * our UD header is bigger than the space available up to the
  2740. * next 64 byte boundary in the WQE, use two inline data
  2741. * segments to hold the UD header.
  2742. */
  2743. spc = MLX4_INLINE_ALIGN -
  2744. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2745. if (header_size <= spc) {
  2746. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2747. memcpy(inl + 1, sqp->header_buf, header_size);
  2748. i = 1;
  2749. } else {
  2750. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2751. memcpy(inl + 1, sqp->header_buf, spc);
  2752. inl = (void *) (inl + 1) + spc;
  2753. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2754. /*
  2755. * Need a barrier here to make sure all the data is
  2756. * visible before the byte_count field is set.
  2757. * Otherwise the HCA prefetcher could grab the 64-byte
  2758. * chunk with this inline segment and get a valid (!=
  2759. * 0xffffffff) byte count but stale data, and end up
  2760. * generating a packet with bad headers.
  2761. *
  2762. * The first inline segment's byte_count field doesn't
  2763. * need a barrier, because it comes after a
  2764. * control/MLX segment and therefore is at an offset
  2765. * of 16 mod 64.
  2766. */
  2767. wmb();
  2768. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2769. i = 2;
  2770. }
  2771. *mlx_seg_len =
  2772. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2773. return 0;
  2774. }
  2775. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  2776. {
  2777. unsigned cur;
  2778. struct mlx4_ib_cq *cq;
  2779. cur = wq->head - wq->tail;
  2780. if (likely(cur + nreq < wq->max_post))
  2781. return 0;
  2782. cq = to_mcq(ib_cq);
  2783. spin_lock(&cq->lock);
  2784. cur = wq->head - wq->tail;
  2785. spin_unlock(&cq->lock);
  2786. return cur + nreq >= wq->max_post;
  2787. }
  2788. static __be32 convert_access(int acc)
  2789. {
  2790. return (acc & IB_ACCESS_REMOTE_ATOMIC ?
  2791. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC) : 0) |
  2792. (acc & IB_ACCESS_REMOTE_WRITE ?
  2793. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) |
  2794. (acc & IB_ACCESS_REMOTE_READ ?
  2795. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ) : 0) |
  2796. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  2797. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  2798. }
  2799. static void set_reg_seg(struct mlx4_wqe_fmr_seg *fseg,
  2800. const struct ib_reg_wr *wr)
  2801. {
  2802. struct mlx4_ib_mr *mr = to_mmr(wr->mr);
  2803. fseg->flags = convert_access(wr->access);
  2804. fseg->mem_key = cpu_to_be32(wr->key);
  2805. fseg->buf_list = cpu_to_be64(mr->page_map);
  2806. fseg->start_addr = cpu_to_be64(mr->ibmr.iova);
  2807. fseg->reg_len = cpu_to_be64(mr->ibmr.length);
  2808. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  2809. fseg->page_size = cpu_to_be32(ilog2(mr->ibmr.page_size));
  2810. fseg->reserved[0] = 0;
  2811. fseg->reserved[1] = 0;
  2812. }
  2813. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  2814. {
  2815. memset(iseg, 0, sizeof(*iseg));
  2816. iseg->mem_key = cpu_to_be32(rkey);
  2817. }
  2818. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  2819. u64 remote_addr, u32 rkey)
  2820. {
  2821. rseg->raddr = cpu_to_be64(remote_addr);
  2822. rseg->rkey = cpu_to_be32(rkey);
  2823. rseg->reserved = 0;
  2824. }
  2825. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg,
  2826. const struct ib_atomic_wr *wr)
  2827. {
  2828. if (wr->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  2829. aseg->swap_add = cpu_to_be64(wr->swap);
  2830. aseg->compare = cpu_to_be64(wr->compare_add);
  2831. } else if (wr->wr.opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  2832. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2833. aseg->compare = cpu_to_be64(wr->compare_add_mask);
  2834. } else {
  2835. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2836. aseg->compare = 0;
  2837. }
  2838. }
  2839. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  2840. const struct ib_atomic_wr *wr)
  2841. {
  2842. aseg->swap_add = cpu_to_be64(wr->swap);
  2843. aseg->swap_add_mask = cpu_to_be64(wr->swap_mask);
  2844. aseg->compare = cpu_to_be64(wr->compare_add);
  2845. aseg->compare_mask = cpu_to_be64(wr->compare_add_mask);
  2846. }
  2847. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  2848. const struct ib_ud_wr *wr)
  2849. {
  2850. memcpy(dseg->av, &to_mah(wr->ah)->av, sizeof (struct mlx4_av));
  2851. dseg->dqpn = cpu_to_be32(wr->remote_qpn);
  2852. dseg->qkey = cpu_to_be32(wr->remote_qkey);
  2853. dseg->vlan = to_mah(wr->ah)->av.eth.vlan;
  2854. memcpy(dseg->mac, to_mah(wr->ah)->av.eth.mac, 6);
  2855. }
  2856. static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev,
  2857. struct mlx4_wqe_datagram_seg *dseg,
  2858. const struct ib_ud_wr *wr,
  2859. enum mlx4_ib_qp_type qpt)
  2860. {
  2861. union mlx4_ext_av *av = &to_mah(wr->ah)->av;
  2862. struct mlx4_av sqp_av = {0};
  2863. int port = *((u8 *) &av->ib.port_pd) & 0x3;
  2864. /* force loopback */
  2865. sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000);
  2866. sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */
  2867. sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel &
  2868. cpu_to_be32(0xf0000000);
  2869. memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av));
  2870. if (qpt == MLX4_IB_QPT_PROXY_GSI)
  2871. dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp1_tunnel);
  2872. else
  2873. dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp0_tunnel);
  2874. /* Use QKEY from the QP context, which is set by master */
  2875. dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  2876. }
  2877. static void build_tunnel_header(const struct ib_ud_wr *wr, void *wqe,
  2878. unsigned *mlx_seg_len)
  2879. {
  2880. struct mlx4_wqe_inline_seg *inl = wqe;
  2881. struct mlx4_ib_tunnel_header hdr;
  2882. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2883. int spc;
  2884. int i;
  2885. memcpy(&hdr.av, &ah->av, sizeof hdr.av);
  2886. hdr.remote_qpn = cpu_to_be32(wr->remote_qpn);
  2887. hdr.pkey_index = cpu_to_be16(wr->pkey_index);
  2888. hdr.qkey = cpu_to_be32(wr->remote_qkey);
  2889. memcpy(hdr.mac, ah->av.eth.mac, 6);
  2890. hdr.vlan = ah->av.eth.vlan;
  2891. spc = MLX4_INLINE_ALIGN -
  2892. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2893. if (sizeof (hdr) <= spc) {
  2894. memcpy(inl + 1, &hdr, sizeof (hdr));
  2895. wmb();
  2896. inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr));
  2897. i = 1;
  2898. } else {
  2899. memcpy(inl + 1, &hdr, spc);
  2900. wmb();
  2901. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2902. inl = (void *) (inl + 1) + spc;
  2903. memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc);
  2904. wmb();
  2905. inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc));
  2906. i = 2;
  2907. }
  2908. *mlx_seg_len =
  2909. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16);
  2910. }
  2911. static void set_mlx_icrc_seg(void *dseg)
  2912. {
  2913. u32 *t = dseg;
  2914. struct mlx4_wqe_inline_seg *iseg = dseg;
  2915. t[1] = 0;
  2916. /*
  2917. * Need a barrier here before writing the byte_count field to
  2918. * make sure that all the data is visible before the
  2919. * byte_count field is set. Otherwise, if the segment begins
  2920. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2921. * chunk and get a valid (!= * 0xffffffff) byte count but
  2922. * stale data, and end up sending the wrong data.
  2923. */
  2924. wmb();
  2925. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  2926. }
  2927. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2928. {
  2929. dseg->lkey = cpu_to_be32(sg->lkey);
  2930. dseg->addr = cpu_to_be64(sg->addr);
  2931. /*
  2932. * Need a barrier here before writing the byte_count field to
  2933. * make sure that all the data is visible before the
  2934. * byte_count field is set. Otherwise, if the segment begins
  2935. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2936. * chunk and get a valid (!= * 0xffffffff) byte count but
  2937. * stale data, and end up sending the wrong data.
  2938. */
  2939. wmb();
  2940. dseg->byte_count = cpu_to_be32(sg->length);
  2941. }
  2942. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2943. {
  2944. dseg->byte_count = cpu_to_be32(sg->length);
  2945. dseg->lkey = cpu_to_be32(sg->lkey);
  2946. dseg->addr = cpu_to_be64(sg->addr);
  2947. }
  2948. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe,
  2949. const struct ib_ud_wr *wr, struct mlx4_ib_qp *qp,
  2950. unsigned *lso_seg_len, __be32 *lso_hdr_sz, __be32 *blh)
  2951. {
  2952. unsigned halign = ALIGN(sizeof *wqe + wr->hlen, 16);
  2953. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  2954. *blh = cpu_to_be32(1 << 6);
  2955. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  2956. wr->wr.num_sge > qp->sq.max_gs - (halign >> 4)))
  2957. return -EINVAL;
  2958. memcpy(wqe->header, wr->header, wr->hlen);
  2959. *lso_hdr_sz = cpu_to_be32(wr->mss << 16 | wr->hlen);
  2960. *lso_seg_len = halign;
  2961. return 0;
  2962. }
  2963. static __be32 send_ieth(const struct ib_send_wr *wr)
  2964. {
  2965. switch (wr->opcode) {
  2966. case IB_WR_SEND_WITH_IMM:
  2967. case IB_WR_RDMA_WRITE_WITH_IMM:
  2968. return wr->ex.imm_data;
  2969. case IB_WR_SEND_WITH_INV:
  2970. return cpu_to_be32(wr->ex.invalidate_rkey);
  2971. default:
  2972. return 0;
  2973. }
  2974. }
  2975. static void add_zero_len_inline(void *wqe)
  2976. {
  2977. struct mlx4_wqe_inline_seg *inl = wqe;
  2978. memset(wqe, 0, 16);
  2979. inl->byte_count = cpu_to_be32(1 << 31);
  2980. }
  2981. static int _mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  2982. const struct ib_send_wr **bad_wr, bool drain)
  2983. {
  2984. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2985. void *wqe;
  2986. struct mlx4_wqe_ctrl_seg *ctrl;
  2987. struct mlx4_wqe_data_seg *dseg;
  2988. unsigned long flags;
  2989. int nreq;
  2990. int err = 0;
  2991. unsigned ind;
  2992. int uninitialized_var(size);
  2993. unsigned uninitialized_var(seglen);
  2994. __be32 dummy;
  2995. __be32 *lso_wqe;
  2996. __be32 uninitialized_var(lso_hdr_sz);
  2997. __be32 blh;
  2998. int i;
  2999. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  3000. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  3001. struct mlx4_ib_sqp *sqp = to_msqp(qp);
  3002. if (sqp->roce_v2_gsi) {
  3003. struct mlx4_ib_ah *ah = to_mah(ud_wr(wr)->ah);
  3004. enum ib_gid_type gid_type;
  3005. union ib_gid gid;
  3006. if (!fill_gid_by_hw_index(mdev, sqp->qp.port,
  3007. ah->av.ib.gid_index,
  3008. &gid, &gid_type))
  3009. qp = (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
  3010. to_mqp(sqp->roce_v2_gsi) : qp;
  3011. else
  3012. pr_err("Failed to get gid at index %d. RoCEv2 will not work properly\n",
  3013. ah->av.ib.gid_index);
  3014. }
  3015. }
  3016. spin_lock_irqsave(&qp->sq.lock, flags);
  3017. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR &&
  3018. !drain) {
  3019. err = -EIO;
  3020. *bad_wr = wr;
  3021. nreq = 0;
  3022. goto out;
  3023. }
  3024. ind = qp->sq_next_wqe;
  3025. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  3026. lso_wqe = &dummy;
  3027. blh = 0;
  3028. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  3029. err = -ENOMEM;
  3030. *bad_wr = wr;
  3031. goto out;
  3032. }
  3033. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  3034. err = -EINVAL;
  3035. *bad_wr = wr;
  3036. goto out;
  3037. }
  3038. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  3039. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  3040. ctrl->srcrb_flags =
  3041. (wr->send_flags & IB_SEND_SIGNALED ?
  3042. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  3043. (wr->send_flags & IB_SEND_SOLICITED ?
  3044. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  3045. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  3046. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  3047. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  3048. qp->sq_signal_bits;
  3049. ctrl->imm = send_ieth(wr);
  3050. wqe += sizeof *ctrl;
  3051. size = sizeof *ctrl / 16;
  3052. switch (qp->mlx4_ib_qp_type) {
  3053. case MLX4_IB_QPT_RC:
  3054. case MLX4_IB_QPT_UC:
  3055. switch (wr->opcode) {
  3056. case IB_WR_ATOMIC_CMP_AND_SWP:
  3057. case IB_WR_ATOMIC_FETCH_AND_ADD:
  3058. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  3059. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  3060. atomic_wr(wr)->rkey);
  3061. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3062. set_atomic_seg(wqe, atomic_wr(wr));
  3063. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  3064. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  3065. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  3066. break;
  3067. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  3068. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  3069. atomic_wr(wr)->rkey);
  3070. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3071. set_masked_atomic_seg(wqe, atomic_wr(wr));
  3072. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  3073. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  3074. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  3075. break;
  3076. case IB_WR_RDMA_READ:
  3077. case IB_WR_RDMA_WRITE:
  3078. case IB_WR_RDMA_WRITE_WITH_IMM:
  3079. set_raddr_seg(wqe, rdma_wr(wr)->remote_addr,
  3080. rdma_wr(wr)->rkey);
  3081. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3082. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  3083. break;
  3084. case IB_WR_LOCAL_INV:
  3085. ctrl->srcrb_flags |=
  3086. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  3087. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  3088. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  3089. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  3090. break;
  3091. case IB_WR_REG_MR:
  3092. ctrl->srcrb_flags |=
  3093. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  3094. set_reg_seg(wqe, reg_wr(wr));
  3095. wqe += sizeof(struct mlx4_wqe_fmr_seg);
  3096. size += sizeof(struct mlx4_wqe_fmr_seg) / 16;
  3097. break;
  3098. default:
  3099. /* No extra segments required for sends */
  3100. break;
  3101. }
  3102. break;
  3103. case MLX4_IB_QPT_TUN_SMI_OWNER:
  3104. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  3105. ctrl, &seglen);
  3106. if (unlikely(err)) {
  3107. *bad_wr = wr;
  3108. goto out;
  3109. }
  3110. wqe += seglen;
  3111. size += seglen / 16;
  3112. break;
  3113. case MLX4_IB_QPT_TUN_SMI:
  3114. case MLX4_IB_QPT_TUN_GSI:
  3115. /* this is a UD qp used in MAD responses to slaves. */
  3116. set_datagram_seg(wqe, ud_wr(wr));
  3117. /* set the forced-loopback bit in the data seg av */
  3118. *(__be32 *) wqe |= cpu_to_be32(0x80000000);
  3119. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3120. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3121. break;
  3122. case MLX4_IB_QPT_UD:
  3123. set_datagram_seg(wqe, ud_wr(wr));
  3124. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3125. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3126. if (wr->opcode == IB_WR_LSO) {
  3127. err = build_lso_seg(wqe, ud_wr(wr), qp, &seglen,
  3128. &lso_hdr_sz, &blh);
  3129. if (unlikely(err)) {
  3130. *bad_wr = wr;
  3131. goto out;
  3132. }
  3133. lso_wqe = (__be32 *) wqe;
  3134. wqe += seglen;
  3135. size += seglen / 16;
  3136. }
  3137. break;
  3138. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  3139. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  3140. ctrl, &seglen);
  3141. if (unlikely(err)) {
  3142. *bad_wr = wr;
  3143. goto out;
  3144. }
  3145. wqe += seglen;
  3146. size += seglen / 16;
  3147. /* to start tunnel header on a cache-line boundary */
  3148. add_zero_len_inline(wqe);
  3149. wqe += 16;
  3150. size++;
  3151. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  3152. wqe += seglen;
  3153. size += seglen / 16;
  3154. break;
  3155. case MLX4_IB_QPT_PROXY_SMI:
  3156. case MLX4_IB_QPT_PROXY_GSI:
  3157. /* If we are tunneling special qps, this is a UD qp.
  3158. * In this case we first add a UD segment targeting
  3159. * the tunnel qp, and then add a header with address
  3160. * information */
  3161. set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe,
  3162. ud_wr(wr),
  3163. qp->mlx4_ib_qp_type);
  3164. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3165. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3166. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  3167. wqe += seglen;
  3168. size += seglen / 16;
  3169. break;
  3170. case MLX4_IB_QPT_SMI:
  3171. case MLX4_IB_QPT_GSI:
  3172. err = build_mlx_header(to_msqp(qp), ud_wr(wr), ctrl,
  3173. &seglen);
  3174. if (unlikely(err)) {
  3175. *bad_wr = wr;
  3176. goto out;
  3177. }
  3178. wqe += seglen;
  3179. size += seglen / 16;
  3180. break;
  3181. default:
  3182. break;
  3183. }
  3184. /*
  3185. * Write data segments in reverse order, so as to
  3186. * overwrite cacheline stamp last within each
  3187. * cacheline. This avoids issues with WQE
  3188. * prefetching.
  3189. */
  3190. dseg = wqe;
  3191. dseg += wr->num_sge - 1;
  3192. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  3193. /* Add one more inline data segment for ICRC for MLX sends */
  3194. if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  3195. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI ||
  3196. qp->mlx4_ib_qp_type &
  3197. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  3198. set_mlx_icrc_seg(dseg + 1);
  3199. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  3200. }
  3201. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  3202. set_data_seg(dseg, wr->sg_list + i);
  3203. /*
  3204. * Possibly overwrite stamping in cacheline with LSO
  3205. * segment only after making sure all data segments
  3206. * are written.
  3207. */
  3208. wmb();
  3209. *lso_wqe = lso_hdr_sz;
  3210. ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ?
  3211. MLX4_WQE_CTRL_FENCE : 0) | size;
  3212. /*
  3213. * Make sure descriptor is fully written before
  3214. * setting ownership bit (because HW can start
  3215. * executing as soon as we do).
  3216. */
  3217. wmb();
  3218. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  3219. *bad_wr = wr;
  3220. err = -EINVAL;
  3221. goto out;
  3222. }
  3223. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  3224. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  3225. /*
  3226. * We can improve latency by not stamping the last
  3227. * send queue WQE until after ringing the doorbell, so
  3228. * only stamp here if there are still more WQEs to post.
  3229. */
  3230. if (wr->next)
  3231. stamp_send_wqe(qp, ind + qp->sq_spare_wqes);
  3232. ind++;
  3233. }
  3234. out:
  3235. if (likely(nreq)) {
  3236. qp->sq.head += nreq;
  3237. /*
  3238. * Make sure that descriptors are written before
  3239. * doorbell record.
  3240. */
  3241. wmb();
  3242. writel_relaxed(qp->doorbell_qpn,
  3243. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  3244. /*
  3245. * Make sure doorbells don't leak out of SQ spinlock
  3246. * and reach the HCA out of order.
  3247. */
  3248. mmiowb();
  3249. stamp_send_wqe(qp, ind + qp->sq_spare_wqes - 1);
  3250. qp->sq_next_wqe = ind;
  3251. }
  3252. spin_unlock_irqrestore(&qp->sq.lock, flags);
  3253. return err;
  3254. }
  3255. int mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  3256. const struct ib_send_wr **bad_wr)
  3257. {
  3258. return _mlx4_ib_post_send(ibqp, wr, bad_wr, false);
  3259. }
  3260. static int _mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  3261. const struct ib_recv_wr **bad_wr, bool drain)
  3262. {
  3263. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  3264. struct mlx4_wqe_data_seg *scat;
  3265. unsigned long flags;
  3266. int err = 0;
  3267. int nreq;
  3268. int ind;
  3269. int max_gs;
  3270. int i;
  3271. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  3272. max_gs = qp->rq.max_gs;
  3273. spin_lock_irqsave(&qp->rq.lock, flags);
  3274. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR &&
  3275. !drain) {
  3276. err = -EIO;
  3277. *bad_wr = wr;
  3278. nreq = 0;
  3279. goto out;
  3280. }
  3281. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  3282. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  3283. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  3284. err = -ENOMEM;
  3285. *bad_wr = wr;
  3286. goto out;
  3287. }
  3288. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  3289. err = -EINVAL;
  3290. *bad_wr = wr;
  3291. goto out;
  3292. }
  3293. scat = get_recv_wqe(qp, ind);
  3294. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  3295. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  3296. ib_dma_sync_single_for_device(ibqp->device,
  3297. qp->sqp_proxy_rcv[ind].map,
  3298. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  3299. DMA_FROM_DEVICE);
  3300. scat->byte_count =
  3301. cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr));
  3302. /* use dma lkey from upper layer entry */
  3303. scat->lkey = cpu_to_be32(wr->sg_list->lkey);
  3304. scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map);
  3305. scat++;
  3306. max_gs--;
  3307. }
  3308. for (i = 0; i < wr->num_sge; ++i)
  3309. __set_data_seg(scat + i, wr->sg_list + i);
  3310. if (i < max_gs) {
  3311. scat[i].byte_count = 0;
  3312. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  3313. scat[i].addr = 0;
  3314. }
  3315. qp->rq.wrid[ind] = wr->wr_id;
  3316. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  3317. }
  3318. out:
  3319. if (likely(nreq)) {
  3320. qp->rq.head += nreq;
  3321. /*
  3322. * Make sure that descriptors are written before
  3323. * doorbell record.
  3324. */
  3325. wmb();
  3326. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  3327. }
  3328. spin_unlock_irqrestore(&qp->rq.lock, flags);
  3329. return err;
  3330. }
  3331. int mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  3332. const struct ib_recv_wr **bad_wr)
  3333. {
  3334. return _mlx4_ib_post_recv(ibqp, wr, bad_wr, false);
  3335. }
  3336. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  3337. {
  3338. switch (mlx4_state) {
  3339. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  3340. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  3341. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  3342. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  3343. case MLX4_QP_STATE_SQ_DRAINING:
  3344. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  3345. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  3346. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  3347. default: return -1;
  3348. }
  3349. }
  3350. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  3351. {
  3352. switch (mlx4_mig_state) {
  3353. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  3354. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  3355. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  3356. default: return -1;
  3357. }
  3358. }
  3359. static int to_ib_qp_access_flags(int mlx4_flags)
  3360. {
  3361. int ib_flags = 0;
  3362. if (mlx4_flags & MLX4_QP_BIT_RRE)
  3363. ib_flags |= IB_ACCESS_REMOTE_READ;
  3364. if (mlx4_flags & MLX4_QP_BIT_RWE)
  3365. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  3366. if (mlx4_flags & MLX4_QP_BIT_RAE)
  3367. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  3368. return ib_flags;
  3369. }
  3370. static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
  3371. struct rdma_ah_attr *ah_attr,
  3372. struct mlx4_qp_path *path)
  3373. {
  3374. struct mlx4_dev *dev = ibdev->dev;
  3375. u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
  3376. memset(ah_attr, 0, sizeof(*ah_attr));
  3377. if (port_num == 0 || port_num > dev->caps.num_ports)
  3378. return;
  3379. ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
  3380. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
  3381. rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) |
  3382. ((path->sched_queue & 4) << 1));
  3383. else
  3384. rdma_ah_set_sl(ah_attr, (path->sched_queue >> 2) & 0xf);
  3385. rdma_ah_set_port_num(ah_attr, port_num);
  3386. rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
  3387. rdma_ah_set_path_bits(ah_attr, path->grh_mylmc & 0x7f);
  3388. rdma_ah_set_static_rate(ah_attr,
  3389. path->static_rate ? path->static_rate - 5 : 0);
  3390. if (path->grh_mylmc & (1 << 7)) {
  3391. rdma_ah_set_grh(ah_attr, NULL,
  3392. be32_to_cpu(path->tclass_flowlabel) & 0xfffff,
  3393. path->mgid_index,
  3394. path->hop_limit,
  3395. (be32_to_cpu(path->tclass_flowlabel)
  3396. >> 20) & 0xff);
  3397. rdma_ah_set_dgid_raw(ah_attr, path->rgid);
  3398. }
  3399. }
  3400. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  3401. struct ib_qp_init_attr *qp_init_attr)
  3402. {
  3403. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  3404. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  3405. struct mlx4_qp_context context;
  3406. int mlx4_state;
  3407. int err = 0;
  3408. if (ibqp->rwq_ind_tbl)
  3409. return -EOPNOTSUPP;
  3410. mutex_lock(&qp->mutex);
  3411. if (qp->state == IB_QPS_RESET) {
  3412. qp_attr->qp_state = IB_QPS_RESET;
  3413. goto done;
  3414. }
  3415. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  3416. if (err) {
  3417. err = -EINVAL;
  3418. goto out;
  3419. }
  3420. mlx4_state = be32_to_cpu(context.flags) >> 28;
  3421. qp->state = to_ib_qp_state(mlx4_state);
  3422. qp_attr->qp_state = qp->state;
  3423. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  3424. qp_attr->path_mig_state =
  3425. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  3426. qp_attr->qkey = be32_to_cpu(context.qkey);
  3427. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  3428. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  3429. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  3430. qp_attr->qp_access_flags =
  3431. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  3432. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  3433. to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  3434. to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  3435. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  3436. qp_attr->alt_port_num =
  3437. rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
  3438. }
  3439. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  3440. if (qp_attr->qp_state == IB_QPS_INIT)
  3441. qp_attr->port_num = qp->port;
  3442. else
  3443. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  3444. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  3445. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  3446. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  3447. qp_attr->max_dest_rd_atomic =
  3448. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  3449. qp_attr->min_rnr_timer =
  3450. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  3451. qp_attr->timeout = context.pri_path.ackto >> 3;
  3452. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  3453. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  3454. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  3455. done:
  3456. qp_attr->cur_qp_state = qp_attr->qp_state;
  3457. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  3458. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  3459. if (!ibqp->uobject) {
  3460. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  3461. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  3462. } else {
  3463. qp_attr->cap.max_send_wr = 0;
  3464. qp_attr->cap.max_send_sge = 0;
  3465. }
  3466. /*
  3467. * We don't support inline sends for kernel QPs (yet), and we
  3468. * don't know what userspace's value should be.
  3469. */
  3470. qp_attr->cap.max_inline_data = 0;
  3471. qp_init_attr->cap = qp_attr->cap;
  3472. qp_init_attr->create_flags = 0;
  3473. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  3474. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  3475. if (qp->flags & MLX4_IB_QP_LSO)
  3476. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  3477. if (qp->flags & MLX4_IB_QP_NETIF)
  3478. qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP;
  3479. qp_init_attr->sq_sig_type =
  3480. qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ?
  3481. IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  3482. out:
  3483. mutex_unlock(&qp->mutex);
  3484. return err;
  3485. }
  3486. struct ib_wq *mlx4_ib_create_wq(struct ib_pd *pd,
  3487. struct ib_wq_init_attr *init_attr,
  3488. struct ib_udata *udata)
  3489. {
  3490. struct mlx4_ib_dev *dev;
  3491. struct ib_qp_init_attr ib_qp_init_attr;
  3492. struct mlx4_ib_qp *qp;
  3493. struct mlx4_ib_create_wq ucmd;
  3494. int err, required_cmd_sz;
  3495. if (!(udata && pd->uobject))
  3496. return ERR_PTR(-EINVAL);
  3497. required_cmd_sz = offsetof(typeof(ucmd), comp_mask) +
  3498. sizeof(ucmd.comp_mask);
  3499. if (udata->inlen < required_cmd_sz) {
  3500. pr_debug("invalid inlen\n");
  3501. return ERR_PTR(-EINVAL);
  3502. }
  3503. if (udata->inlen > sizeof(ucmd) &&
  3504. !ib_is_udata_cleared(udata, sizeof(ucmd),
  3505. udata->inlen - sizeof(ucmd))) {
  3506. pr_debug("inlen is not supported\n");
  3507. return ERR_PTR(-EOPNOTSUPP);
  3508. }
  3509. if (udata->outlen)
  3510. return ERR_PTR(-EOPNOTSUPP);
  3511. dev = to_mdev(pd->device);
  3512. if (init_attr->wq_type != IB_WQT_RQ) {
  3513. pr_debug("unsupported wq type %d\n", init_attr->wq_type);
  3514. return ERR_PTR(-EOPNOTSUPP);
  3515. }
  3516. if (init_attr->create_flags & ~IB_WQ_FLAGS_SCATTER_FCS) {
  3517. pr_debug("unsupported create_flags %u\n",
  3518. init_attr->create_flags);
  3519. return ERR_PTR(-EOPNOTSUPP);
  3520. }
  3521. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  3522. if (!qp)
  3523. return ERR_PTR(-ENOMEM);
  3524. qp->pri.vid = 0xFFFF;
  3525. qp->alt.vid = 0xFFFF;
  3526. memset(&ib_qp_init_attr, 0, sizeof(ib_qp_init_attr));
  3527. ib_qp_init_attr.qp_context = init_attr->wq_context;
  3528. ib_qp_init_attr.qp_type = IB_QPT_RAW_PACKET;
  3529. ib_qp_init_attr.cap.max_recv_wr = init_attr->max_wr;
  3530. ib_qp_init_attr.cap.max_recv_sge = init_attr->max_sge;
  3531. ib_qp_init_attr.recv_cq = init_attr->cq;
  3532. ib_qp_init_attr.send_cq = ib_qp_init_attr.recv_cq; /* Dummy CQ */
  3533. if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS)
  3534. ib_qp_init_attr.create_flags |= IB_QP_CREATE_SCATTER_FCS;
  3535. err = create_qp_common(dev, pd, MLX4_IB_RWQ_SRC, &ib_qp_init_attr,
  3536. udata, 0, &qp);
  3537. if (err) {
  3538. kfree(qp);
  3539. return ERR_PTR(err);
  3540. }
  3541. qp->ibwq.event_handler = init_attr->event_handler;
  3542. qp->ibwq.wq_num = qp->mqp.qpn;
  3543. qp->ibwq.state = IB_WQS_RESET;
  3544. return &qp->ibwq;
  3545. }
  3546. static int ib_wq2qp_state(enum ib_wq_state state)
  3547. {
  3548. switch (state) {
  3549. case IB_WQS_RESET:
  3550. return IB_QPS_RESET;
  3551. case IB_WQS_RDY:
  3552. return IB_QPS_RTR;
  3553. default:
  3554. return IB_QPS_ERR;
  3555. }
  3556. }
  3557. static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state)
  3558. {
  3559. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3560. enum ib_qp_state qp_cur_state;
  3561. enum ib_qp_state qp_new_state;
  3562. int attr_mask;
  3563. int err;
  3564. /* ib_qp.state represents the WQ HW state while ib_wq.state represents
  3565. * the WQ logic state.
  3566. */
  3567. qp_cur_state = qp->state;
  3568. qp_new_state = ib_wq2qp_state(new_state);
  3569. if (ib_wq2qp_state(new_state) == qp_cur_state)
  3570. return 0;
  3571. if (new_state == IB_WQS_RDY) {
  3572. struct ib_qp_attr attr = {};
  3573. attr.port_num = qp->port;
  3574. attr_mask = IB_QP_PORT;
  3575. err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, &attr,
  3576. attr_mask, IB_QPS_RESET, IB_QPS_INIT);
  3577. if (err) {
  3578. pr_debug("WQN=0x%06x failed to apply RST->INIT on the HW QP\n",
  3579. ibwq->wq_num);
  3580. return err;
  3581. }
  3582. qp_cur_state = IB_QPS_INIT;
  3583. }
  3584. attr_mask = 0;
  3585. err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL, attr_mask,
  3586. qp_cur_state, qp_new_state);
  3587. if (err && (qp_cur_state == IB_QPS_INIT)) {
  3588. qp_new_state = IB_QPS_RESET;
  3589. if (__mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL,
  3590. attr_mask, IB_QPS_INIT, IB_QPS_RESET)) {
  3591. pr_warn("WQN=0x%06x failed with reverting HW's resources failure\n",
  3592. ibwq->wq_num);
  3593. qp_new_state = IB_QPS_INIT;
  3594. }
  3595. }
  3596. qp->state = qp_new_state;
  3597. return err;
  3598. }
  3599. int mlx4_ib_modify_wq(struct ib_wq *ibwq, struct ib_wq_attr *wq_attr,
  3600. u32 wq_attr_mask, struct ib_udata *udata)
  3601. {
  3602. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3603. struct mlx4_ib_modify_wq ucmd = {};
  3604. size_t required_cmd_sz;
  3605. enum ib_wq_state cur_state, new_state;
  3606. int err = 0;
  3607. required_cmd_sz = offsetof(typeof(ucmd), reserved) +
  3608. sizeof(ucmd.reserved);
  3609. if (udata->inlen < required_cmd_sz)
  3610. return -EINVAL;
  3611. if (udata->inlen > sizeof(ucmd) &&
  3612. !ib_is_udata_cleared(udata, sizeof(ucmd),
  3613. udata->inlen - sizeof(ucmd)))
  3614. return -EOPNOTSUPP;
  3615. if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
  3616. return -EFAULT;
  3617. if (ucmd.comp_mask || ucmd.reserved)
  3618. return -EOPNOTSUPP;
  3619. if (wq_attr_mask & IB_WQ_FLAGS)
  3620. return -EOPNOTSUPP;
  3621. cur_state = wq_attr_mask & IB_WQ_CUR_STATE ? wq_attr->curr_wq_state :
  3622. ibwq->state;
  3623. new_state = wq_attr_mask & IB_WQ_STATE ? wq_attr->wq_state : cur_state;
  3624. if (cur_state < IB_WQS_RESET || cur_state > IB_WQS_ERR ||
  3625. new_state < IB_WQS_RESET || new_state > IB_WQS_ERR)
  3626. return -EINVAL;
  3627. if ((new_state == IB_WQS_RDY) && (cur_state == IB_WQS_ERR))
  3628. return -EINVAL;
  3629. if ((new_state == IB_WQS_ERR) && (cur_state == IB_WQS_RESET))
  3630. return -EINVAL;
  3631. /* Need to protect against the parent RSS which also may modify WQ
  3632. * state.
  3633. */
  3634. mutex_lock(&qp->mutex);
  3635. /* Can update HW state only if a RSS QP has already associated to this
  3636. * WQ, so we can apply its port on the WQ.
  3637. */
  3638. if (qp->rss_usecnt)
  3639. err = _mlx4_ib_modify_wq(ibwq, new_state);
  3640. if (!err)
  3641. ibwq->state = new_state;
  3642. mutex_unlock(&qp->mutex);
  3643. return err;
  3644. }
  3645. int mlx4_ib_destroy_wq(struct ib_wq *ibwq)
  3646. {
  3647. struct mlx4_ib_dev *dev = to_mdev(ibwq->device);
  3648. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3649. if (qp->counter_index)
  3650. mlx4_ib_free_qp_counter(dev, qp);
  3651. destroy_qp_common(dev, qp, MLX4_IB_RWQ_SRC, 1);
  3652. kfree(qp);
  3653. return 0;
  3654. }
  3655. struct ib_rwq_ind_table
  3656. *mlx4_ib_create_rwq_ind_table(struct ib_device *device,
  3657. struct ib_rwq_ind_table_init_attr *init_attr,
  3658. struct ib_udata *udata)
  3659. {
  3660. struct ib_rwq_ind_table *rwq_ind_table;
  3661. struct mlx4_ib_create_rwq_ind_tbl_resp resp = {};
  3662. unsigned int ind_tbl_size = 1 << init_attr->log_ind_tbl_size;
  3663. unsigned int base_wqn;
  3664. size_t min_resp_len;
  3665. int i;
  3666. int err;
  3667. if (udata->inlen > 0 &&
  3668. !ib_is_udata_cleared(udata, 0,
  3669. udata->inlen))
  3670. return ERR_PTR(-EOPNOTSUPP);
  3671. min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
  3672. if (udata->outlen && udata->outlen < min_resp_len)
  3673. return ERR_PTR(-EINVAL);
  3674. if (ind_tbl_size >
  3675. device->attrs.rss_caps.max_rwq_indirection_table_size) {
  3676. pr_debug("log_ind_tbl_size = %d is bigger than supported = %d\n",
  3677. ind_tbl_size,
  3678. device->attrs.rss_caps.max_rwq_indirection_table_size);
  3679. return ERR_PTR(-EINVAL);
  3680. }
  3681. base_wqn = init_attr->ind_tbl[0]->wq_num;
  3682. if (base_wqn % ind_tbl_size) {
  3683. pr_debug("WQN=0x%x isn't aligned with indirection table size\n",
  3684. base_wqn);
  3685. return ERR_PTR(-EINVAL);
  3686. }
  3687. for (i = 1; i < ind_tbl_size; i++) {
  3688. if (++base_wqn != init_attr->ind_tbl[i]->wq_num) {
  3689. pr_debug("indirection table's WQNs aren't consecutive\n");
  3690. return ERR_PTR(-EINVAL);
  3691. }
  3692. }
  3693. rwq_ind_table = kzalloc(sizeof(*rwq_ind_table), GFP_KERNEL);
  3694. if (!rwq_ind_table)
  3695. return ERR_PTR(-ENOMEM);
  3696. if (udata->outlen) {
  3697. resp.response_length = offsetof(typeof(resp), response_length) +
  3698. sizeof(resp.response_length);
  3699. err = ib_copy_to_udata(udata, &resp, resp.response_length);
  3700. if (err)
  3701. goto err;
  3702. }
  3703. return rwq_ind_table;
  3704. err:
  3705. kfree(rwq_ind_table);
  3706. return ERR_PTR(err);
  3707. }
  3708. int mlx4_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
  3709. {
  3710. kfree(ib_rwq_ind_tbl);
  3711. return 0;
  3712. }
  3713. struct mlx4_ib_drain_cqe {
  3714. struct ib_cqe cqe;
  3715. struct completion done;
  3716. };
  3717. static void mlx4_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
  3718. {
  3719. struct mlx4_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
  3720. struct mlx4_ib_drain_cqe,
  3721. cqe);
  3722. complete(&cqe->done);
  3723. }
  3724. /* This function returns only once the drained WR was completed */
  3725. static void handle_drain_completion(struct ib_cq *cq,
  3726. struct mlx4_ib_drain_cqe *sdrain,
  3727. struct mlx4_ib_dev *dev)
  3728. {
  3729. struct mlx4_dev *mdev = dev->dev;
  3730. if (cq->poll_ctx == IB_POLL_DIRECT) {
  3731. while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
  3732. ib_process_cq_direct(cq, -1);
  3733. return;
  3734. }
  3735. if (mdev->persist->state == MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3736. struct mlx4_ib_cq *mcq = to_mcq(cq);
  3737. bool triggered = false;
  3738. unsigned long flags;
  3739. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  3740. /* Make sure that the CQ handler won't run if wasn't run yet */
  3741. if (!mcq->mcq.reset_notify_added)
  3742. mcq->mcq.reset_notify_added = 1;
  3743. else
  3744. triggered = true;
  3745. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  3746. if (triggered) {
  3747. /* Wait for any scheduled/running task to be ended */
  3748. switch (cq->poll_ctx) {
  3749. case IB_POLL_SOFTIRQ:
  3750. irq_poll_disable(&cq->iop);
  3751. irq_poll_enable(&cq->iop);
  3752. break;
  3753. case IB_POLL_WORKQUEUE:
  3754. cancel_work_sync(&cq->work);
  3755. break;
  3756. default:
  3757. WARN_ON_ONCE(1);
  3758. }
  3759. }
  3760. /* Run the CQ handler - this makes sure that the drain WR will
  3761. * be processed if wasn't processed yet.
  3762. */
  3763. mcq->mcq.comp(&mcq->mcq);
  3764. }
  3765. wait_for_completion(&sdrain->done);
  3766. }
  3767. void mlx4_ib_drain_sq(struct ib_qp *qp)
  3768. {
  3769. struct ib_cq *cq = qp->send_cq;
  3770. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  3771. struct mlx4_ib_drain_cqe sdrain;
  3772. const struct ib_send_wr *bad_swr;
  3773. struct ib_rdma_wr swr = {
  3774. .wr = {
  3775. .next = NULL,
  3776. { .wr_cqe = &sdrain.cqe, },
  3777. .opcode = IB_WR_RDMA_WRITE,
  3778. },
  3779. };
  3780. int ret;
  3781. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  3782. struct mlx4_dev *mdev = dev->dev;
  3783. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  3784. if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3785. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  3786. return;
  3787. }
  3788. sdrain.cqe.done = mlx4_ib_drain_qp_done;
  3789. init_completion(&sdrain.done);
  3790. ret = _mlx4_ib_post_send(qp, &swr.wr, &bad_swr, true);
  3791. if (ret) {
  3792. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  3793. return;
  3794. }
  3795. handle_drain_completion(cq, &sdrain, dev);
  3796. }
  3797. void mlx4_ib_drain_rq(struct ib_qp *qp)
  3798. {
  3799. struct ib_cq *cq = qp->recv_cq;
  3800. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  3801. struct mlx4_ib_drain_cqe rdrain;
  3802. struct ib_recv_wr rwr = {};
  3803. const struct ib_recv_wr *bad_rwr;
  3804. int ret;
  3805. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  3806. struct mlx4_dev *mdev = dev->dev;
  3807. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  3808. if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3809. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  3810. return;
  3811. }
  3812. rwr.wr_cqe = &rdrain.cqe;
  3813. rdrain.cqe.done = mlx4_ib_drain_qp_done;
  3814. init_completion(&rdrain.done);
  3815. ret = _mlx4_ib_post_recv(qp, &rwr, &bad_rwr, true);
  3816. if (ret) {
  3817. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  3818. return;
  3819. }
  3820. handle_drain_completion(cq, &rdrain, dev);
  3821. }