qp.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464
  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 err;
  2432. int i;
  2433. if (wr->wr.opcode != IB_WR_SEND)
  2434. return -EINVAL;
  2435. send_size = 0;
  2436. for (i = 0; i < wr->wr.num_sge; ++i)
  2437. send_size += wr->wr.sg_list[i].length;
  2438. /* for proxy-qp0 sends, need to add in size of tunnel header */
  2439. /* for tunnel-qp0 sends, tunnel header is already in s/g list */
  2440. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER)
  2441. send_size += sizeof (struct mlx4_ib_tunnel_header);
  2442. ib_ud_header_init(send_size, 1, 0, 0, 0, 0, 0, 0, &sqp->ud_header);
  2443. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) {
  2444. sqp->ud_header.lrh.service_level =
  2445. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2446. sqp->ud_header.lrh.destination_lid =
  2447. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2448. sqp->ud_header.lrh.source_lid =
  2449. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2450. }
  2451. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2452. /* force loopback */
  2453. mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR);
  2454. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2455. sqp->ud_header.lrh.virtual_lane = 0;
  2456. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2457. err = ib_get_cached_pkey(ib_dev, sqp->qp.port, 0, &pkey);
  2458. if (err)
  2459. return err;
  2460. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2461. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER)
  2462. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2463. else
  2464. sqp->ud_header.bth.destination_qpn =
  2465. cpu_to_be32(mdev->dev->caps.spec_qps[sqp->qp.port - 1].qp0_tunnel);
  2466. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2467. if (mlx4_is_master(mdev->dev)) {
  2468. if (mlx4_get_parav_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2469. return -EINVAL;
  2470. } else {
  2471. if (vf_get_qp0_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2472. return -EINVAL;
  2473. }
  2474. sqp->ud_header.deth.qkey = cpu_to_be32(qkey);
  2475. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.mqp.qpn);
  2476. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2477. sqp->ud_header.immediate_present = 0;
  2478. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2479. /*
  2480. * Inline data segments may not cross a 64 byte boundary. If
  2481. * our UD header is bigger than the space available up to the
  2482. * next 64 byte boundary in the WQE, use two inline data
  2483. * segments to hold the UD header.
  2484. */
  2485. spc = MLX4_INLINE_ALIGN -
  2486. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2487. if (header_size <= spc) {
  2488. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2489. memcpy(inl + 1, sqp->header_buf, header_size);
  2490. i = 1;
  2491. } else {
  2492. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2493. memcpy(inl + 1, sqp->header_buf, spc);
  2494. inl = (void *) (inl + 1) + spc;
  2495. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2496. /*
  2497. * Need a barrier here to make sure all the data is
  2498. * visible before the byte_count field is set.
  2499. * Otherwise the HCA prefetcher could grab the 64-byte
  2500. * chunk with this inline segment and get a valid (!=
  2501. * 0xffffffff) byte count but stale data, and end up
  2502. * generating a packet with bad headers.
  2503. *
  2504. * The first inline segment's byte_count field doesn't
  2505. * need a barrier, because it comes after a
  2506. * control/MLX segment and therefore is at an offset
  2507. * of 16 mod 64.
  2508. */
  2509. wmb();
  2510. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2511. i = 2;
  2512. }
  2513. *mlx_seg_len =
  2514. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2515. return 0;
  2516. }
  2517. static u8 sl_to_vl(struct mlx4_ib_dev *dev, u8 sl, int port_num)
  2518. {
  2519. union sl2vl_tbl_to_u64 tmp_vltab;
  2520. u8 vl;
  2521. if (sl > 15)
  2522. return 0xf;
  2523. tmp_vltab.sl64 = atomic64_read(&dev->sl2vl[port_num - 1]);
  2524. vl = tmp_vltab.sl8[sl >> 1];
  2525. if (sl & 1)
  2526. vl &= 0x0f;
  2527. else
  2528. vl >>= 4;
  2529. return vl;
  2530. }
  2531. static int fill_gid_by_hw_index(struct mlx4_ib_dev *ibdev, u8 port_num,
  2532. int index, union ib_gid *gid,
  2533. enum ib_gid_type *gid_type)
  2534. {
  2535. struct mlx4_ib_iboe *iboe = &ibdev->iboe;
  2536. struct mlx4_port_gid_table *port_gid_table;
  2537. unsigned long flags;
  2538. port_gid_table = &iboe->gids[port_num - 1];
  2539. spin_lock_irqsave(&iboe->lock, flags);
  2540. memcpy(gid, &port_gid_table->gids[index].gid, sizeof(*gid));
  2541. *gid_type = port_gid_table->gids[index].gid_type;
  2542. spin_unlock_irqrestore(&iboe->lock, flags);
  2543. if (rdma_is_zero_gid(gid))
  2544. return -ENOENT;
  2545. return 0;
  2546. }
  2547. #define MLX4_ROCEV2_QP1_SPORT 0xC000
  2548. static int build_mlx_header(struct mlx4_ib_sqp *sqp, const struct ib_ud_wr *wr,
  2549. void *wqe, unsigned *mlx_seg_len)
  2550. {
  2551. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  2552. struct mlx4_ib_dev *ibdev = to_mdev(ib_dev);
  2553. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2554. struct mlx4_wqe_ctrl_seg *ctrl = wqe;
  2555. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2556. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2557. union ib_gid sgid;
  2558. u16 pkey;
  2559. int send_size;
  2560. int header_size;
  2561. int spc;
  2562. int i;
  2563. int err = 0;
  2564. u16 vlan = 0xffff;
  2565. bool is_eth;
  2566. bool is_vlan = false;
  2567. bool is_grh;
  2568. bool is_udp = false;
  2569. int ip_version = 0;
  2570. send_size = 0;
  2571. for (i = 0; i < wr->wr.num_sge; ++i)
  2572. send_size += wr->wr.sg_list[i].length;
  2573. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  2574. is_grh = mlx4_ib_ah_grh_present(ah);
  2575. if (is_eth) {
  2576. enum ib_gid_type gid_type;
  2577. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2578. /* When multi-function is enabled, the ib_core gid
  2579. * indexes don't necessarily match the hw ones, so
  2580. * we must use our own cache */
  2581. err = mlx4_get_roce_gid_from_slave(to_mdev(ib_dev)->dev,
  2582. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2583. ah->av.ib.gid_index, &sgid.raw[0]);
  2584. if (err)
  2585. return err;
  2586. } else {
  2587. err = fill_gid_by_hw_index(ibdev, sqp->qp.port,
  2588. ah->av.ib.gid_index,
  2589. &sgid, &gid_type);
  2590. if (!err) {
  2591. is_udp = gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
  2592. if (is_udp) {
  2593. if (ipv6_addr_v4mapped((struct in6_addr *)&sgid))
  2594. ip_version = 4;
  2595. else
  2596. ip_version = 6;
  2597. is_grh = false;
  2598. }
  2599. } else {
  2600. return err;
  2601. }
  2602. }
  2603. if (ah->av.eth.vlan != cpu_to_be16(0xffff)) {
  2604. vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff;
  2605. is_vlan = 1;
  2606. }
  2607. }
  2608. err = ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh,
  2609. ip_version, is_udp, 0, &sqp->ud_header);
  2610. if (err)
  2611. return err;
  2612. if (!is_eth) {
  2613. sqp->ud_header.lrh.service_level =
  2614. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2615. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  2616. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2617. }
  2618. if (is_grh || (ip_version == 6)) {
  2619. sqp->ud_header.grh.traffic_class =
  2620. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2621. sqp->ud_header.grh.flow_label =
  2622. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  2623. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  2624. if (is_eth) {
  2625. memcpy(sqp->ud_header.grh.source_gid.raw, sgid.raw, 16);
  2626. } else {
  2627. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2628. /* When multi-function is enabled, the ib_core gid
  2629. * indexes don't necessarily match the hw ones, so
  2630. * we must use our own cache
  2631. */
  2632. sqp->ud_header.grh.source_gid.global.subnet_prefix =
  2633. cpu_to_be64(atomic64_read(&(to_mdev(ib_dev)->sriov.
  2634. demux[sqp->qp.port - 1].
  2635. subnet_prefix)));
  2636. sqp->ud_header.grh.source_gid.global.interface_id =
  2637. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  2638. guid_cache[ah->av.ib.gid_index];
  2639. } else {
  2640. sqp->ud_header.grh.source_gid =
  2641. ah->ibah.sgid_attr->gid;
  2642. }
  2643. }
  2644. memcpy(sqp->ud_header.grh.destination_gid.raw,
  2645. ah->av.ib.dgid, 16);
  2646. }
  2647. if (ip_version == 4) {
  2648. sqp->ud_header.ip4.tos =
  2649. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2650. sqp->ud_header.ip4.id = 0;
  2651. sqp->ud_header.ip4.frag_off = htons(IP_DF);
  2652. sqp->ud_header.ip4.ttl = ah->av.eth.hop_limit;
  2653. memcpy(&sqp->ud_header.ip4.saddr,
  2654. sgid.raw + 12, 4);
  2655. memcpy(&sqp->ud_header.ip4.daddr, ah->av.ib.dgid + 12, 4);
  2656. sqp->ud_header.ip4.check = ib_ud_ip4_csum(&sqp->ud_header);
  2657. }
  2658. if (is_udp) {
  2659. sqp->ud_header.udp.dport = htons(ROCE_V2_UDP_DPORT);
  2660. sqp->ud_header.udp.sport = htons(MLX4_ROCEV2_QP1_SPORT);
  2661. sqp->ud_header.udp.csum = 0;
  2662. }
  2663. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2664. if (!is_eth) {
  2665. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  2666. (sqp->ud_header.lrh.destination_lid ==
  2667. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  2668. (sqp->ud_header.lrh.service_level << 8));
  2669. if (ah->av.ib.port_pd & cpu_to_be32(0x80000000))
  2670. mlx->flags |= cpu_to_be32(0x1); /* force loopback */
  2671. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2672. }
  2673. switch (wr->wr.opcode) {
  2674. case IB_WR_SEND:
  2675. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2676. sqp->ud_header.immediate_present = 0;
  2677. break;
  2678. case IB_WR_SEND_WITH_IMM:
  2679. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  2680. sqp->ud_header.immediate_present = 1;
  2681. sqp->ud_header.immediate_data = wr->wr.ex.imm_data;
  2682. break;
  2683. default:
  2684. return -EINVAL;
  2685. }
  2686. if (is_eth) {
  2687. struct in6_addr in6;
  2688. u16 ether_type;
  2689. u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
  2690. ether_type = (!is_udp) ? ETH_P_IBOE:
  2691. (ip_version == 4 ? ETH_P_IP : ETH_P_IPV6);
  2692. mlx->sched_prio = cpu_to_be16(pcp);
  2693. ether_addr_copy(sqp->ud_header.eth.smac_h, ah->av.eth.s_mac);
  2694. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  2695. memcpy(&ctrl->srcrb_flags16[0], ah->av.eth.mac, 2);
  2696. memcpy(&ctrl->imm, ah->av.eth.mac + 2, 4);
  2697. memcpy(&in6, sgid.raw, sizeof(in6));
  2698. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  2699. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  2700. if (!is_vlan) {
  2701. sqp->ud_header.eth.type = cpu_to_be16(ether_type);
  2702. } else {
  2703. sqp->ud_header.vlan.type = cpu_to_be16(ether_type);
  2704. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  2705. }
  2706. } else {
  2707. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 :
  2708. sl_to_vl(to_mdev(ib_dev),
  2709. sqp->ud_header.lrh.service_level,
  2710. sqp->qp.port);
  2711. if (sqp->qp.ibqp.qp_num && sqp->ud_header.lrh.virtual_lane == 15)
  2712. return -EINVAL;
  2713. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  2714. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  2715. }
  2716. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2717. if (!sqp->qp.ibqp.qp_num)
  2718. err = ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index,
  2719. &pkey);
  2720. else
  2721. err = ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->pkey_index,
  2722. &pkey);
  2723. if (err)
  2724. return err;
  2725. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2726. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2727. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2728. sqp->ud_header.deth.qkey = cpu_to_be32(wr->remote_qkey & 0x80000000 ?
  2729. sqp->qkey : wr->remote_qkey);
  2730. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  2731. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2732. if (0) {
  2733. pr_err("built UD header of size %d:\n", header_size);
  2734. for (i = 0; i < header_size / 4; ++i) {
  2735. if (i % 8 == 0)
  2736. pr_err(" [%02x] ", i * 4);
  2737. pr_cont(" %08x",
  2738. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  2739. if ((i + 1) % 8 == 0)
  2740. pr_cont("\n");
  2741. }
  2742. pr_err("\n");
  2743. }
  2744. /*
  2745. * Inline data segments may not cross a 64 byte boundary. If
  2746. * our UD header is bigger than the space available up to the
  2747. * next 64 byte boundary in the WQE, use two inline data
  2748. * segments to hold the UD header.
  2749. */
  2750. spc = MLX4_INLINE_ALIGN -
  2751. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2752. if (header_size <= spc) {
  2753. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2754. memcpy(inl + 1, sqp->header_buf, header_size);
  2755. i = 1;
  2756. } else {
  2757. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2758. memcpy(inl + 1, sqp->header_buf, spc);
  2759. inl = (void *) (inl + 1) + spc;
  2760. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2761. /*
  2762. * Need a barrier here to make sure all the data is
  2763. * visible before the byte_count field is set.
  2764. * Otherwise the HCA prefetcher could grab the 64-byte
  2765. * chunk with this inline segment and get a valid (!=
  2766. * 0xffffffff) byte count but stale data, and end up
  2767. * generating a packet with bad headers.
  2768. *
  2769. * The first inline segment's byte_count field doesn't
  2770. * need a barrier, because it comes after a
  2771. * control/MLX segment and therefore is at an offset
  2772. * of 16 mod 64.
  2773. */
  2774. wmb();
  2775. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2776. i = 2;
  2777. }
  2778. *mlx_seg_len =
  2779. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2780. return 0;
  2781. }
  2782. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  2783. {
  2784. unsigned cur;
  2785. struct mlx4_ib_cq *cq;
  2786. cur = wq->head - wq->tail;
  2787. if (likely(cur + nreq < wq->max_post))
  2788. return 0;
  2789. cq = to_mcq(ib_cq);
  2790. spin_lock(&cq->lock);
  2791. cur = wq->head - wq->tail;
  2792. spin_unlock(&cq->lock);
  2793. return cur + nreq >= wq->max_post;
  2794. }
  2795. static __be32 convert_access(int acc)
  2796. {
  2797. return (acc & IB_ACCESS_REMOTE_ATOMIC ?
  2798. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC) : 0) |
  2799. (acc & IB_ACCESS_REMOTE_WRITE ?
  2800. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) |
  2801. (acc & IB_ACCESS_REMOTE_READ ?
  2802. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ) : 0) |
  2803. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  2804. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  2805. }
  2806. static void set_reg_seg(struct mlx4_wqe_fmr_seg *fseg,
  2807. const struct ib_reg_wr *wr)
  2808. {
  2809. struct mlx4_ib_mr *mr = to_mmr(wr->mr);
  2810. fseg->flags = convert_access(wr->access);
  2811. fseg->mem_key = cpu_to_be32(wr->key);
  2812. fseg->buf_list = cpu_to_be64(mr->page_map);
  2813. fseg->start_addr = cpu_to_be64(mr->ibmr.iova);
  2814. fseg->reg_len = cpu_to_be64(mr->ibmr.length);
  2815. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  2816. fseg->page_size = cpu_to_be32(ilog2(mr->ibmr.page_size));
  2817. fseg->reserved[0] = 0;
  2818. fseg->reserved[1] = 0;
  2819. }
  2820. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  2821. {
  2822. memset(iseg, 0, sizeof(*iseg));
  2823. iseg->mem_key = cpu_to_be32(rkey);
  2824. }
  2825. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  2826. u64 remote_addr, u32 rkey)
  2827. {
  2828. rseg->raddr = cpu_to_be64(remote_addr);
  2829. rseg->rkey = cpu_to_be32(rkey);
  2830. rseg->reserved = 0;
  2831. }
  2832. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg,
  2833. const struct ib_atomic_wr *wr)
  2834. {
  2835. if (wr->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  2836. aseg->swap_add = cpu_to_be64(wr->swap);
  2837. aseg->compare = cpu_to_be64(wr->compare_add);
  2838. } else if (wr->wr.opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  2839. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2840. aseg->compare = cpu_to_be64(wr->compare_add_mask);
  2841. } else {
  2842. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2843. aseg->compare = 0;
  2844. }
  2845. }
  2846. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  2847. const struct ib_atomic_wr *wr)
  2848. {
  2849. aseg->swap_add = cpu_to_be64(wr->swap);
  2850. aseg->swap_add_mask = cpu_to_be64(wr->swap_mask);
  2851. aseg->compare = cpu_to_be64(wr->compare_add);
  2852. aseg->compare_mask = cpu_to_be64(wr->compare_add_mask);
  2853. }
  2854. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  2855. const struct ib_ud_wr *wr)
  2856. {
  2857. memcpy(dseg->av, &to_mah(wr->ah)->av, sizeof (struct mlx4_av));
  2858. dseg->dqpn = cpu_to_be32(wr->remote_qpn);
  2859. dseg->qkey = cpu_to_be32(wr->remote_qkey);
  2860. dseg->vlan = to_mah(wr->ah)->av.eth.vlan;
  2861. memcpy(dseg->mac, to_mah(wr->ah)->av.eth.mac, 6);
  2862. }
  2863. static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev,
  2864. struct mlx4_wqe_datagram_seg *dseg,
  2865. const struct ib_ud_wr *wr,
  2866. enum mlx4_ib_qp_type qpt)
  2867. {
  2868. union mlx4_ext_av *av = &to_mah(wr->ah)->av;
  2869. struct mlx4_av sqp_av = {0};
  2870. int port = *((u8 *) &av->ib.port_pd) & 0x3;
  2871. /* force loopback */
  2872. sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000);
  2873. sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */
  2874. sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel &
  2875. cpu_to_be32(0xf0000000);
  2876. memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av));
  2877. if (qpt == MLX4_IB_QPT_PROXY_GSI)
  2878. dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp1_tunnel);
  2879. else
  2880. dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp0_tunnel);
  2881. /* Use QKEY from the QP context, which is set by master */
  2882. dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  2883. }
  2884. static void build_tunnel_header(const struct ib_ud_wr *wr, void *wqe,
  2885. unsigned *mlx_seg_len)
  2886. {
  2887. struct mlx4_wqe_inline_seg *inl = wqe;
  2888. struct mlx4_ib_tunnel_header hdr;
  2889. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2890. int spc;
  2891. int i;
  2892. memcpy(&hdr.av, &ah->av, sizeof hdr.av);
  2893. hdr.remote_qpn = cpu_to_be32(wr->remote_qpn);
  2894. hdr.pkey_index = cpu_to_be16(wr->pkey_index);
  2895. hdr.qkey = cpu_to_be32(wr->remote_qkey);
  2896. memcpy(hdr.mac, ah->av.eth.mac, 6);
  2897. hdr.vlan = ah->av.eth.vlan;
  2898. spc = MLX4_INLINE_ALIGN -
  2899. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2900. if (sizeof (hdr) <= spc) {
  2901. memcpy(inl + 1, &hdr, sizeof (hdr));
  2902. wmb();
  2903. inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr));
  2904. i = 1;
  2905. } else {
  2906. memcpy(inl + 1, &hdr, spc);
  2907. wmb();
  2908. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2909. inl = (void *) (inl + 1) + spc;
  2910. memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc);
  2911. wmb();
  2912. inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc));
  2913. i = 2;
  2914. }
  2915. *mlx_seg_len =
  2916. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16);
  2917. }
  2918. static void set_mlx_icrc_seg(void *dseg)
  2919. {
  2920. u32 *t = dseg;
  2921. struct mlx4_wqe_inline_seg *iseg = dseg;
  2922. t[1] = 0;
  2923. /*
  2924. * Need a barrier here before writing the byte_count field to
  2925. * make sure that all the data is visible before the
  2926. * byte_count field is set. Otherwise, if the segment begins
  2927. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2928. * chunk and get a valid (!= * 0xffffffff) byte count but
  2929. * stale data, and end up sending the wrong data.
  2930. */
  2931. wmb();
  2932. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  2933. }
  2934. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2935. {
  2936. dseg->lkey = cpu_to_be32(sg->lkey);
  2937. dseg->addr = cpu_to_be64(sg->addr);
  2938. /*
  2939. * Need a barrier here before writing the byte_count field to
  2940. * make sure that all the data is visible before the
  2941. * byte_count field is set. Otherwise, if the segment begins
  2942. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2943. * chunk and get a valid (!= * 0xffffffff) byte count but
  2944. * stale data, and end up sending the wrong data.
  2945. */
  2946. wmb();
  2947. dseg->byte_count = cpu_to_be32(sg->length);
  2948. }
  2949. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2950. {
  2951. dseg->byte_count = cpu_to_be32(sg->length);
  2952. dseg->lkey = cpu_to_be32(sg->lkey);
  2953. dseg->addr = cpu_to_be64(sg->addr);
  2954. }
  2955. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe,
  2956. const struct ib_ud_wr *wr, struct mlx4_ib_qp *qp,
  2957. unsigned *lso_seg_len, __be32 *lso_hdr_sz, __be32 *blh)
  2958. {
  2959. unsigned halign = ALIGN(sizeof *wqe + wr->hlen, 16);
  2960. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  2961. *blh = cpu_to_be32(1 << 6);
  2962. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  2963. wr->wr.num_sge > qp->sq.max_gs - (halign >> 4)))
  2964. return -EINVAL;
  2965. memcpy(wqe->header, wr->header, wr->hlen);
  2966. *lso_hdr_sz = cpu_to_be32(wr->mss << 16 | wr->hlen);
  2967. *lso_seg_len = halign;
  2968. return 0;
  2969. }
  2970. static __be32 send_ieth(const struct ib_send_wr *wr)
  2971. {
  2972. switch (wr->opcode) {
  2973. case IB_WR_SEND_WITH_IMM:
  2974. case IB_WR_RDMA_WRITE_WITH_IMM:
  2975. return wr->ex.imm_data;
  2976. case IB_WR_SEND_WITH_INV:
  2977. return cpu_to_be32(wr->ex.invalidate_rkey);
  2978. default:
  2979. return 0;
  2980. }
  2981. }
  2982. static void add_zero_len_inline(void *wqe)
  2983. {
  2984. struct mlx4_wqe_inline_seg *inl = wqe;
  2985. memset(wqe, 0, 16);
  2986. inl->byte_count = cpu_to_be32(1 << 31);
  2987. }
  2988. static int _mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  2989. const struct ib_send_wr **bad_wr, bool drain)
  2990. {
  2991. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2992. void *wqe;
  2993. struct mlx4_wqe_ctrl_seg *ctrl;
  2994. struct mlx4_wqe_data_seg *dseg;
  2995. unsigned long flags;
  2996. int nreq;
  2997. int err = 0;
  2998. unsigned ind;
  2999. int uninitialized_var(size);
  3000. unsigned uninitialized_var(seglen);
  3001. __be32 dummy;
  3002. __be32 *lso_wqe;
  3003. __be32 uninitialized_var(lso_hdr_sz);
  3004. __be32 blh;
  3005. int i;
  3006. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  3007. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  3008. struct mlx4_ib_sqp *sqp = to_msqp(qp);
  3009. if (sqp->roce_v2_gsi) {
  3010. struct mlx4_ib_ah *ah = to_mah(ud_wr(wr)->ah);
  3011. enum ib_gid_type gid_type;
  3012. union ib_gid gid;
  3013. if (!fill_gid_by_hw_index(mdev, sqp->qp.port,
  3014. ah->av.ib.gid_index,
  3015. &gid, &gid_type))
  3016. qp = (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
  3017. to_mqp(sqp->roce_v2_gsi) : qp;
  3018. else
  3019. pr_err("Failed to get gid at index %d. RoCEv2 will not work properly\n",
  3020. ah->av.ib.gid_index);
  3021. }
  3022. }
  3023. spin_lock_irqsave(&qp->sq.lock, flags);
  3024. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR &&
  3025. !drain) {
  3026. err = -EIO;
  3027. *bad_wr = wr;
  3028. nreq = 0;
  3029. goto out;
  3030. }
  3031. ind = qp->sq_next_wqe;
  3032. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  3033. lso_wqe = &dummy;
  3034. blh = 0;
  3035. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  3036. err = -ENOMEM;
  3037. *bad_wr = wr;
  3038. goto out;
  3039. }
  3040. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  3041. err = -EINVAL;
  3042. *bad_wr = wr;
  3043. goto out;
  3044. }
  3045. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  3046. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  3047. ctrl->srcrb_flags =
  3048. (wr->send_flags & IB_SEND_SIGNALED ?
  3049. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  3050. (wr->send_flags & IB_SEND_SOLICITED ?
  3051. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  3052. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  3053. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  3054. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  3055. qp->sq_signal_bits;
  3056. ctrl->imm = send_ieth(wr);
  3057. wqe += sizeof *ctrl;
  3058. size = sizeof *ctrl / 16;
  3059. switch (qp->mlx4_ib_qp_type) {
  3060. case MLX4_IB_QPT_RC:
  3061. case MLX4_IB_QPT_UC:
  3062. switch (wr->opcode) {
  3063. case IB_WR_ATOMIC_CMP_AND_SWP:
  3064. case IB_WR_ATOMIC_FETCH_AND_ADD:
  3065. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  3066. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  3067. atomic_wr(wr)->rkey);
  3068. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3069. set_atomic_seg(wqe, atomic_wr(wr));
  3070. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  3071. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  3072. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  3073. break;
  3074. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  3075. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  3076. atomic_wr(wr)->rkey);
  3077. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3078. set_masked_atomic_seg(wqe, atomic_wr(wr));
  3079. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  3080. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  3081. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  3082. break;
  3083. case IB_WR_RDMA_READ:
  3084. case IB_WR_RDMA_WRITE:
  3085. case IB_WR_RDMA_WRITE_WITH_IMM:
  3086. set_raddr_seg(wqe, rdma_wr(wr)->remote_addr,
  3087. rdma_wr(wr)->rkey);
  3088. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  3089. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  3090. break;
  3091. case IB_WR_LOCAL_INV:
  3092. ctrl->srcrb_flags |=
  3093. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  3094. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  3095. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  3096. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  3097. break;
  3098. case IB_WR_REG_MR:
  3099. ctrl->srcrb_flags |=
  3100. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  3101. set_reg_seg(wqe, reg_wr(wr));
  3102. wqe += sizeof(struct mlx4_wqe_fmr_seg);
  3103. size += sizeof(struct mlx4_wqe_fmr_seg) / 16;
  3104. break;
  3105. default:
  3106. /* No extra segments required for sends */
  3107. break;
  3108. }
  3109. break;
  3110. case MLX4_IB_QPT_TUN_SMI_OWNER:
  3111. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  3112. ctrl, &seglen);
  3113. if (unlikely(err)) {
  3114. *bad_wr = wr;
  3115. goto out;
  3116. }
  3117. wqe += seglen;
  3118. size += seglen / 16;
  3119. break;
  3120. case MLX4_IB_QPT_TUN_SMI:
  3121. case MLX4_IB_QPT_TUN_GSI:
  3122. /* this is a UD qp used in MAD responses to slaves. */
  3123. set_datagram_seg(wqe, ud_wr(wr));
  3124. /* set the forced-loopback bit in the data seg av */
  3125. *(__be32 *) wqe |= cpu_to_be32(0x80000000);
  3126. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3127. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3128. break;
  3129. case MLX4_IB_QPT_UD:
  3130. set_datagram_seg(wqe, ud_wr(wr));
  3131. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3132. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3133. if (wr->opcode == IB_WR_LSO) {
  3134. err = build_lso_seg(wqe, ud_wr(wr), qp, &seglen,
  3135. &lso_hdr_sz, &blh);
  3136. if (unlikely(err)) {
  3137. *bad_wr = wr;
  3138. goto out;
  3139. }
  3140. lso_wqe = (__be32 *) wqe;
  3141. wqe += seglen;
  3142. size += seglen / 16;
  3143. }
  3144. break;
  3145. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  3146. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  3147. ctrl, &seglen);
  3148. if (unlikely(err)) {
  3149. *bad_wr = wr;
  3150. goto out;
  3151. }
  3152. wqe += seglen;
  3153. size += seglen / 16;
  3154. /* to start tunnel header on a cache-line boundary */
  3155. add_zero_len_inline(wqe);
  3156. wqe += 16;
  3157. size++;
  3158. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  3159. wqe += seglen;
  3160. size += seglen / 16;
  3161. break;
  3162. case MLX4_IB_QPT_PROXY_SMI:
  3163. case MLX4_IB_QPT_PROXY_GSI:
  3164. /* If we are tunneling special qps, this is a UD qp.
  3165. * In this case we first add a UD segment targeting
  3166. * the tunnel qp, and then add a header with address
  3167. * information */
  3168. set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe,
  3169. ud_wr(wr),
  3170. qp->mlx4_ib_qp_type);
  3171. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  3172. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  3173. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  3174. wqe += seglen;
  3175. size += seglen / 16;
  3176. break;
  3177. case MLX4_IB_QPT_SMI:
  3178. case MLX4_IB_QPT_GSI:
  3179. err = build_mlx_header(to_msqp(qp), ud_wr(wr), ctrl,
  3180. &seglen);
  3181. if (unlikely(err)) {
  3182. *bad_wr = wr;
  3183. goto out;
  3184. }
  3185. wqe += seglen;
  3186. size += seglen / 16;
  3187. break;
  3188. default:
  3189. break;
  3190. }
  3191. /*
  3192. * Write data segments in reverse order, so as to
  3193. * overwrite cacheline stamp last within each
  3194. * cacheline. This avoids issues with WQE
  3195. * prefetching.
  3196. */
  3197. dseg = wqe;
  3198. dseg += wr->num_sge - 1;
  3199. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  3200. /* Add one more inline data segment for ICRC for MLX sends */
  3201. if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  3202. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI ||
  3203. qp->mlx4_ib_qp_type &
  3204. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  3205. set_mlx_icrc_seg(dseg + 1);
  3206. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  3207. }
  3208. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  3209. set_data_seg(dseg, wr->sg_list + i);
  3210. /*
  3211. * Possibly overwrite stamping in cacheline with LSO
  3212. * segment only after making sure all data segments
  3213. * are written.
  3214. */
  3215. wmb();
  3216. *lso_wqe = lso_hdr_sz;
  3217. ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ?
  3218. MLX4_WQE_CTRL_FENCE : 0) | size;
  3219. /*
  3220. * Make sure descriptor is fully written before
  3221. * setting ownership bit (because HW can start
  3222. * executing as soon as we do).
  3223. */
  3224. wmb();
  3225. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  3226. *bad_wr = wr;
  3227. err = -EINVAL;
  3228. goto out;
  3229. }
  3230. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  3231. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  3232. /*
  3233. * We can improve latency by not stamping the last
  3234. * send queue WQE until after ringing the doorbell, so
  3235. * only stamp here if there are still more WQEs to post.
  3236. */
  3237. if (wr->next)
  3238. stamp_send_wqe(qp, ind + qp->sq_spare_wqes);
  3239. ind++;
  3240. }
  3241. out:
  3242. if (likely(nreq)) {
  3243. qp->sq.head += nreq;
  3244. /*
  3245. * Make sure that descriptors are written before
  3246. * doorbell record.
  3247. */
  3248. wmb();
  3249. writel_relaxed(qp->doorbell_qpn,
  3250. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  3251. /*
  3252. * Make sure doorbells don't leak out of SQ spinlock
  3253. * and reach the HCA out of order.
  3254. */
  3255. mmiowb();
  3256. stamp_send_wqe(qp, ind + qp->sq_spare_wqes - 1);
  3257. qp->sq_next_wqe = ind;
  3258. }
  3259. spin_unlock_irqrestore(&qp->sq.lock, flags);
  3260. return err;
  3261. }
  3262. int mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
  3263. const struct ib_send_wr **bad_wr)
  3264. {
  3265. return _mlx4_ib_post_send(ibqp, wr, bad_wr, false);
  3266. }
  3267. static int _mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  3268. const struct ib_recv_wr **bad_wr, bool drain)
  3269. {
  3270. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  3271. struct mlx4_wqe_data_seg *scat;
  3272. unsigned long flags;
  3273. int err = 0;
  3274. int nreq;
  3275. int ind;
  3276. int max_gs;
  3277. int i;
  3278. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  3279. max_gs = qp->rq.max_gs;
  3280. spin_lock_irqsave(&qp->rq.lock, flags);
  3281. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR &&
  3282. !drain) {
  3283. err = -EIO;
  3284. *bad_wr = wr;
  3285. nreq = 0;
  3286. goto out;
  3287. }
  3288. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  3289. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  3290. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  3291. err = -ENOMEM;
  3292. *bad_wr = wr;
  3293. goto out;
  3294. }
  3295. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  3296. err = -EINVAL;
  3297. *bad_wr = wr;
  3298. goto out;
  3299. }
  3300. scat = get_recv_wqe(qp, ind);
  3301. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  3302. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  3303. ib_dma_sync_single_for_device(ibqp->device,
  3304. qp->sqp_proxy_rcv[ind].map,
  3305. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  3306. DMA_FROM_DEVICE);
  3307. scat->byte_count =
  3308. cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr));
  3309. /* use dma lkey from upper layer entry */
  3310. scat->lkey = cpu_to_be32(wr->sg_list->lkey);
  3311. scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map);
  3312. scat++;
  3313. max_gs--;
  3314. }
  3315. for (i = 0; i < wr->num_sge; ++i)
  3316. __set_data_seg(scat + i, wr->sg_list + i);
  3317. if (i < max_gs) {
  3318. scat[i].byte_count = 0;
  3319. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  3320. scat[i].addr = 0;
  3321. }
  3322. qp->rq.wrid[ind] = wr->wr_id;
  3323. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  3324. }
  3325. out:
  3326. if (likely(nreq)) {
  3327. qp->rq.head += nreq;
  3328. /*
  3329. * Make sure that descriptors are written before
  3330. * doorbell record.
  3331. */
  3332. wmb();
  3333. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  3334. }
  3335. spin_unlock_irqrestore(&qp->rq.lock, flags);
  3336. return err;
  3337. }
  3338. int mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
  3339. const struct ib_recv_wr **bad_wr)
  3340. {
  3341. return _mlx4_ib_post_recv(ibqp, wr, bad_wr, false);
  3342. }
  3343. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  3344. {
  3345. switch (mlx4_state) {
  3346. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  3347. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  3348. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  3349. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  3350. case MLX4_QP_STATE_SQ_DRAINING:
  3351. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  3352. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  3353. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  3354. default: return -1;
  3355. }
  3356. }
  3357. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  3358. {
  3359. switch (mlx4_mig_state) {
  3360. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  3361. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  3362. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  3363. default: return -1;
  3364. }
  3365. }
  3366. static int to_ib_qp_access_flags(int mlx4_flags)
  3367. {
  3368. int ib_flags = 0;
  3369. if (mlx4_flags & MLX4_QP_BIT_RRE)
  3370. ib_flags |= IB_ACCESS_REMOTE_READ;
  3371. if (mlx4_flags & MLX4_QP_BIT_RWE)
  3372. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  3373. if (mlx4_flags & MLX4_QP_BIT_RAE)
  3374. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  3375. return ib_flags;
  3376. }
  3377. static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
  3378. struct rdma_ah_attr *ah_attr,
  3379. struct mlx4_qp_path *path)
  3380. {
  3381. struct mlx4_dev *dev = ibdev->dev;
  3382. u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
  3383. memset(ah_attr, 0, sizeof(*ah_attr));
  3384. if (port_num == 0 || port_num > dev->caps.num_ports)
  3385. return;
  3386. ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
  3387. if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
  3388. rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) |
  3389. ((path->sched_queue & 4) << 1));
  3390. else
  3391. rdma_ah_set_sl(ah_attr, (path->sched_queue >> 2) & 0xf);
  3392. rdma_ah_set_port_num(ah_attr, port_num);
  3393. rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
  3394. rdma_ah_set_path_bits(ah_attr, path->grh_mylmc & 0x7f);
  3395. rdma_ah_set_static_rate(ah_attr,
  3396. path->static_rate ? path->static_rate - 5 : 0);
  3397. if (path->grh_mylmc & (1 << 7)) {
  3398. rdma_ah_set_grh(ah_attr, NULL,
  3399. be32_to_cpu(path->tclass_flowlabel) & 0xfffff,
  3400. path->mgid_index,
  3401. path->hop_limit,
  3402. (be32_to_cpu(path->tclass_flowlabel)
  3403. >> 20) & 0xff);
  3404. rdma_ah_set_dgid_raw(ah_attr, path->rgid);
  3405. }
  3406. }
  3407. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  3408. struct ib_qp_init_attr *qp_init_attr)
  3409. {
  3410. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  3411. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  3412. struct mlx4_qp_context context;
  3413. int mlx4_state;
  3414. int err = 0;
  3415. if (ibqp->rwq_ind_tbl)
  3416. return -EOPNOTSUPP;
  3417. mutex_lock(&qp->mutex);
  3418. if (qp->state == IB_QPS_RESET) {
  3419. qp_attr->qp_state = IB_QPS_RESET;
  3420. goto done;
  3421. }
  3422. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  3423. if (err) {
  3424. err = -EINVAL;
  3425. goto out;
  3426. }
  3427. mlx4_state = be32_to_cpu(context.flags) >> 28;
  3428. qp->state = to_ib_qp_state(mlx4_state);
  3429. qp_attr->qp_state = qp->state;
  3430. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  3431. qp_attr->path_mig_state =
  3432. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  3433. qp_attr->qkey = be32_to_cpu(context.qkey);
  3434. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  3435. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  3436. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  3437. qp_attr->qp_access_flags =
  3438. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  3439. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  3440. to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  3441. to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  3442. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  3443. qp_attr->alt_port_num =
  3444. rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
  3445. }
  3446. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  3447. if (qp_attr->qp_state == IB_QPS_INIT)
  3448. qp_attr->port_num = qp->port;
  3449. else
  3450. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  3451. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  3452. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  3453. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  3454. qp_attr->max_dest_rd_atomic =
  3455. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  3456. qp_attr->min_rnr_timer =
  3457. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  3458. qp_attr->timeout = context.pri_path.ackto >> 3;
  3459. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  3460. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  3461. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  3462. done:
  3463. qp_attr->cur_qp_state = qp_attr->qp_state;
  3464. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  3465. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  3466. if (!ibqp->uobject) {
  3467. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  3468. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  3469. } else {
  3470. qp_attr->cap.max_send_wr = 0;
  3471. qp_attr->cap.max_send_sge = 0;
  3472. }
  3473. /*
  3474. * We don't support inline sends for kernel QPs (yet), and we
  3475. * don't know what userspace's value should be.
  3476. */
  3477. qp_attr->cap.max_inline_data = 0;
  3478. qp_init_attr->cap = qp_attr->cap;
  3479. qp_init_attr->create_flags = 0;
  3480. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  3481. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  3482. if (qp->flags & MLX4_IB_QP_LSO)
  3483. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  3484. if (qp->flags & MLX4_IB_QP_NETIF)
  3485. qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP;
  3486. qp_init_attr->sq_sig_type =
  3487. qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ?
  3488. IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  3489. out:
  3490. mutex_unlock(&qp->mutex);
  3491. return err;
  3492. }
  3493. struct ib_wq *mlx4_ib_create_wq(struct ib_pd *pd,
  3494. struct ib_wq_init_attr *init_attr,
  3495. struct ib_udata *udata)
  3496. {
  3497. struct mlx4_ib_dev *dev;
  3498. struct ib_qp_init_attr ib_qp_init_attr;
  3499. struct mlx4_ib_qp *qp;
  3500. struct mlx4_ib_create_wq ucmd;
  3501. int err, required_cmd_sz;
  3502. if (!(udata && pd->uobject))
  3503. return ERR_PTR(-EINVAL);
  3504. required_cmd_sz = offsetof(typeof(ucmd), comp_mask) +
  3505. sizeof(ucmd.comp_mask);
  3506. if (udata->inlen < required_cmd_sz) {
  3507. pr_debug("invalid inlen\n");
  3508. return ERR_PTR(-EINVAL);
  3509. }
  3510. if (udata->inlen > sizeof(ucmd) &&
  3511. !ib_is_udata_cleared(udata, sizeof(ucmd),
  3512. udata->inlen - sizeof(ucmd))) {
  3513. pr_debug("inlen is not supported\n");
  3514. return ERR_PTR(-EOPNOTSUPP);
  3515. }
  3516. if (udata->outlen)
  3517. return ERR_PTR(-EOPNOTSUPP);
  3518. dev = to_mdev(pd->device);
  3519. if (init_attr->wq_type != IB_WQT_RQ) {
  3520. pr_debug("unsupported wq type %d\n", init_attr->wq_type);
  3521. return ERR_PTR(-EOPNOTSUPP);
  3522. }
  3523. if (init_attr->create_flags & ~IB_WQ_FLAGS_SCATTER_FCS) {
  3524. pr_debug("unsupported create_flags %u\n",
  3525. init_attr->create_flags);
  3526. return ERR_PTR(-EOPNOTSUPP);
  3527. }
  3528. qp = kzalloc(sizeof(*qp), GFP_KERNEL);
  3529. if (!qp)
  3530. return ERR_PTR(-ENOMEM);
  3531. qp->pri.vid = 0xFFFF;
  3532. qp->alt.vid = 0xFFFF;
  3533. memset(&ib_qp_init_attr, 0, sizeof(ib_qp_init_attr));
  3534. ib_qp_init_attr.qp_context = init_attr->wq_context;
  3535. ib_qp_init_attr.qp_type = IB_QPT_RAW_PACKET;
  3536. ib_qp_init_attr.cap.max_recv_wr = init_attr->max_wr;
  3537. ib_qp_init_attr.cap.max_recv_sge = init_attr->max_sge;
  3538. ib_qp_init_attr.recv_cq = init_attr->cq;
  3539. ib_qp_init_attr.send_cq = ib_qp_init_attr.recv_cq; /* Dummy CQ */
  3540. if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS)
  3541. ib_qp_init_attr.create_flags |= IB_QP_CREATE_SCATTER_FCS;
  3542. err = create_qp_common(dev, pd, MLX4_IB_RWQ_SRC, &ib_qp_init_attr,
  3543. udata, 0, &qp);
  3544. if (err) {
  3545. kfree(qp);
  3546. return ERR_PTR(err);
  3547. }
  3548. qp->ibwq.event_handler = init_attr->event_handler;
  3549. qp->ibwq.wq_num = qp->mqp.qpn;
  3550. qp->ibwq.state = IB_WQS_RESET;
  3551. return &qp->ibwq;
  3552. }
  3553. static int ib_wq2qp_state(enum ib_wq_state state)
  3554. {
  3555. switch (state) {
  3556. case IB_WQS_RESET:
  3557. return IB_QPS_RESET;
  3558. case IB_WQS_RDY:
  3559. return IB_QPS_RTR;
  3560. default:
  3561. return IB_QPS_ERR;
  3562. }
  3563. }
  3564. static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state)
  3565. {
  3566. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3567. enum ib_qp_state qp_cur_state;
  3568. enum ib_qp_state qp_new_state;
  3569. int attr_mask;
  3570. int err;
  3571. /* ib_qp.state represents the WQ HW state while ib_wq.state represents
  3572. * the WQ logic state.
  3573. */
  3574. qp_cur_state = qp->state;
  3575. qp_new_state = ib_wq2qp_state(new_state);
  3576. if (ib_wq2qp_state(new_state) == qp_cur_state)
  3577. return 0;
  3578. if (new_state == IB_WQS_RDY) {
  3579. struct ib_qp_attr attr = {};
  3580. attr.port_num = qp->port;
  3581. attr_mask = IB_QP_PORT;
  3582. err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, &attr,
  3583. attr_mask, IB_QPS_RESET, IB_QPS_INIT);
  3584. if (err) {
  3585. pr_debug("WQN=0x%06x failed to apply RST->INIT on the HW QP\n",
  3586. ibwq->wq_num);
  3587. return err;
  3588. }
  3589. qp_cur_state = IB_QPS_INIT;
  3590. }
  3591. attr_mask = 0;
  3592. err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL, attr_mask,
  3593. qp_cur_state, qp_new_state);
  3594. if (err && (qp_cur_state == IB_QPS_INIT)) {
  3595. qp_new_state = IB_QPS_RESET;
  3596. if (__mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL,
  3597. attr_mask, IB_QPS_INIT, IB_QPS_RESET)) {
  3598. pr_warn("WQN=0x%06x failed with reverting HW's resources failure\n",
  3599. ibwq->wq_num);
  3600. qp_new_state = IB_QPS_INIT;
  3601. }
  3602. }
  3603. qp->state = qp_new_state;
  3604. return err;
  3605. }
  3606. int mlx4_ib_modify_wq(struct ib_wq *ibwq, struct ib_wq_attr *wq_attr,
  3607. u32 wq_attr_mask, struct ib_udata *udata)
  3608. {
  3609. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3610. struct mlx4_ib_modify_wq ucmd = {};
  3611. size_t required_cmd_sz;
  3612. enum ib_wq_state cur_state, new_state;
  3613. int err = 0;
  3614. required_cmd_sz = offsetof(typeof(ucmd), reserved) +
  3615. sizeof(ucmd.reserved);
  3616. if (udata->inlen < required_cmd_sz)
  3617. return -EINVAL;
  3618. if (udata->inlen > sizeof(ucmd) &&
  3619. !ib_is_udata_cleared(udata, sizeof(ucmd),
  3620. udata->inlen - sizeof(ucmd)))
  3621. return -EOPNOTSUPP;
  3622. if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
  3623. return -EFAULT;
  3624. if (ucmd.comp_mask || ucmd.reserved)
  3625. return -EOPNOTSUPP;
  3626. if (wq_attr_mask & IB_WQ_FLAGS)
  3627. return -EOPNOTSUPP;
  3628. cur_state = wq_attr_mask & IB_WQ_CUR_STATE ? wq_attr->curr_wq_state :
  3629. ibwq->state;
  3630. new_state = wq_attr_mask & IB_WQ_STATE ? wq_attr->wq_state : cur_state;
  3631. if (cur_state < IB_WQS_RESET || cur_state > IB_WQS_ERR ||
  3632. new_state < IB_WQS_RESET || new_state > IB_WQS_ERR)
  3633. return -EINVAL;
  3634. if ((new_state == IB_WQS_RDY) && (cur_state == IB_WQS_ERR))
  3635. return -EINVAL;
  3636. if ((new_state == IB_WQS_ERR) && (cur_state == IB_WQS_RESET))
  3637. return -EINVAL;
  3638. /* Need to protect against the parent RSS which also may modify WQ
  3639. * state.
  3640. */
  3641. mutex_lock(&qp->mutex);
  3642. /* Can update HW state only if a RSS QP has already associated to this
  3643. * WQ, so we can apply its port on the WQ.
  3644. */
  3645. if (qp->rss_usecnt)
  3646. err = _mlx4_ib_modify_wq(ibwq, new_state);
  3647. if (!err)
  3648. ibwq->state = new_state;
  3649. mutex_unlock(&qp->mutex);
  3650. return err;
  3651. }
  3652. int mlx4_ib_destroy_wq(struct ib_wq *ibwq)
  3653. {
  3654. struct mlx4_ib_dev *dev = to_mdev(ibwq->device);
  3655. struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
  3656. if (qp->counter_index)
  3657. mlx4_ib_free_qp_counter(dev, qp);
  3658. destroy_qp_common(dev, qp, MLX4_IB_RWQ_SRC, 1);
  3659. kfree(qp);
  3660. return 0;
  3661. }
  3662. struct ib_rwq_ind_table
  3663. *mlx4_ib_create_rwq_ind_table(struct ib_device *device,
  3664. struct ib_rwq_ind_table_init_attr *init_attr,
  3665. struct ib_udata *udata)
  3666. {
  3667. struct ib_rwq_ind_table *rwq_ind_table;
  3668. struct mlx4_ib_create_rwq_ind_tbl_resp resp = {};
  3669. unsigned int ind_tbl_size = 1 << init_attr->log_ind_tbl_size;
  3670. unsigned int base_wqn;
  3671. size_t min_resp_len;
  3672. int i;
  3673. int err;
  3674. if (udata->inlen > 0 &&
  3675. !ib_is_udata_cleared(udata, 0,
  3676. udata->inlen))
  3677. return ERR_PTR(-EOPNOTSUPP);
  3678. min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
  3679. if (udata->outlen && udata->outlen < min_resp_len)
  3680. return ERR_PTR(-EINVAL);
  3681. if (ind_tbl_size >
  3682. device->attrs.rss_caps.max_rwq_indirection_table_size) {
  3683. pr_debug("log_ind_tbl_size = %d is bigger than supported = %d\n",
  3684. ind_tbl_size,
  3685. device->attrs.rss_caps.max_rwq_indirection_table_size);
  3686. return ERR_PTR(-EINVAL);
  3687. }
  3688. base_wqn = init_attr->ind_tbl[0]->wq_num;
  3689. if (base_wqn % ind_tbl_size) {
  3690. pr_debug("WQN=0x%x isn't aligned with indirection table size\n",
  3691. base_wqn);
  3692. return ERR_PTR(-EINVAL);
  3693. }
  3694. for (i = 1; i < ind_tbl_size; i++) {
  3695. if (++base_wqn != init_attr->ind_tbl[i]->wq_num) {
  3696. pr_debug("indirection table's WQNs aren't consecutive\n");
  3697. return ERR_PTR(-EINVAL);
  3698. }
  3699. }
  3700. rwq_ind_table = kzalloc(sizeof(*rwq_ind_table), GFP_KERNEL);
  3701. if (!rwq_ind_table)
  3702. return ERR_PTR(-ENOMEM);
  3703. if (udata->outlen) {
  3704. resp.response_length = offsetof(typeof(resp), response_length) +
  3705. sizeof(resp.response_length);
  3706. err = ib_copy_to_udata(udata, &resp, resp.response_length);
  3707. if (err)
  3708. goto err;
  3709. }
  3710. return rwq_ind_table;
  3711. err:
  3712. kfree(rwq_ind_table);
  3713. return ERR_PTR(err);
  3714. }
  3715. int mlx4_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
  3716. {
  3717. kfree(ib_rwq_ind_tbl);
  3718. return 0;
  3719. }
  3720. struct mlx4_ib_drain_cqe {
  3721. struct ib_cqe cqe;
  3722. struct completion done;
  3723. };
  3724. static void mlx4_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
  3725. {
  3726. struct mlx4_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
  3727. struct mlx4_ib_drain_cqe,
  3728. cqe);
  3729. complete(&cqe->done);
  3730. }
  3731. /* This function returns only once the drained WR was completed */
  3732. static void handle_drain_completion(struct ib_cq *cq,
  3733. struct mlx4_ib_drain_cqe *sdrain,
  3734. struct mlx4_ib_dev *dev)
  3735. {
  3736. struct mlx4_dev *mdev = dev->dev;
  3737. if (cq->poll_ctx == IB_POLL_DIRECT) {
  3738. while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
  3739. ib_process_cq_direct(cq, -1);
  3740. return;
  3741. }
  3742. if (mdev->persist->state == MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3743. struct mlx4_ib_cq *mcq = to_mcq(cq);
  3744. bool triggered = false;
  3745. unsigned long flags;
  3746. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  3747. /* Make sure that the CQ handler won't run if wasn't run yet */
  3748. if (!mcq->mcq.reset_notify_added)
  3749. mcq->mcq.reset_notify_added = 1;
  3750. else
  3751. triggered = true;
  3752. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  3753. if (triggered) {
  3754. /* Wait for any scheduled/running task to be ended */
  3755. switch (cq->poll_ctx) {
  3756. case IB_POLL_SOFTIRQ:
  3757. irq_poll_disable(&cq->iop);
  3758. irq_poll_enable(&cq->iop);
  3759. break;
  3760. case IB_POLL_WORKQUEUE:
  3761. cancel_work_sync(&cq->work);
  3762. break;
  3763. default:
  3764. WARN_ON_ONCE(1);
  3765. }
  3766. }
  3767. /* Run the CQ handler - this makes sure that the drain WR will
  3768. * be processed if wasn't processed yet.
  3769. */
  3770. mcq->mcq.comp(&mcq->mcq);
  3771. }
  3772. wait_for_completion(&sdrain->done);
  3773. }
  3774. void mlx4_ib_drain_sq(struct ib_qp *qp)
  3775. {
  3776. struct ib_cq *cq = qp->send_cq;
  3777. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  3778. struct mlx4_ib_drain_cqe sdrain;
  3779. const struct ib_send_wr *bad_swr;
  3780. struct ib_rdma_wr swr = {
  3781. .wr = {
  3782. .next = NULL,
  3783. { .wr_cqe = &sdrain.cqe, },
  3784. .opcode = IB_WR_RDMA_WRITE,
  3785. },
  3786. };
  3787. int ret;
  3788. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  3789. struct mlx4_dev *mdev = dev->dev;
  3790. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  3791. if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3792. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  3793. return;
  3794. }
  3795. sdrain.cqe.done = mlx4_ib_drain_qp_done;
  3796. init_completion(&sdrain.done);
  3797. ret = _mlx4_ib_post_send(qp, &swr.wr, &bad_swr, true);
  3798. if (ret) {
  3799. WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
  3800. return;
  3801. }
  3802. handle_drain_completion(cq, &sdrain, dev);
  3803. }
  3804. void mlx4_ib_drain_rq(struct ib_qp *qp)
  3805. {
  3806. struct ib_cq *cq = qp->recv_cq;
  3807. struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
  3808. struct mlx4_ib_drain_cqe rdrain;
  3809. struct ib_recv_wr rwr = {};
  3810. const struct ib_recv_wr *bad_rwr;
  3811. int ret;
  3812. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  3813. struct mlx4_dev *mdev = dev->dev;
  3814. ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
  3815. if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  3816. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  3817. return;
  3818. }
  3819. rwr.wr_cqe = &rdrain.cqe;
  3820. rdrain.cqe.done = mlx4_ib_drain_qp_done;
  3821. init_completion(&rdrain.done);
  3822. ret = _mlx4_ib_post_recv(qp, &rwr, &bad_rwr, true);
  3823. if (ret) {
  3824. WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
  3825. return;
  3826. }
  3827. handle_drain_completion(cq, &rdrain, dev);
  3828. }