qp.c 125 KB

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