qp.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504
  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 <linux/vmalloc.h>
  39. #include <rdma/ib_cache.h>
  40. #include <rdma/ib_pack.h>
  41. #include <rdma/ib_addr.h>
  42. #include <rdma/ib_mad.h>
  43. #include <linux/mlx4/driver.h>
  44. #include <linux/mlx4/qp.h>
  45. #include "mlx4_ib.h"
  46. #include "user.h"
  47. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq,
  48. struct mlx4_ib_cq *recv_cq);
  49. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq,
  50. struct mlx4_ib_cq *recv_cq);
  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. enum {
  71. MLX4_IB_IBOE_ETHERTYPE = 0x8915
  72. };
  73. struct mlx4_ib_sqp {
  74. struct mlx4_ib_qp qp;
  75. int pkey_index;
  76. u32 qkey;
  77. u32 send_psn;
  78. struct ib_ud_header ud_header;
  79. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  80. struct ib_qp *roce_v2_gsi;
  81. };
  82. enum {
  83. MLX4_IB_MIN_SQ_STRIDE = 6,
  84. MLX4_IB_CACHE_LINE_SIZE = 64,
  85. };
  86. enum {
  87. MLX4_RAW_QP_MTU = 7,
  88. MLX4_RAW_QP_MSGMAX = 31,
  89. };
  90. #ifndef ETH_ALEN
  91. #define ETH_ALEN 6
  92. #endif
  93. static const __be32 mlx4_ib_opcode[] = {
  94. [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
  95. [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
  96. [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  97. [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  98. [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  99. [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  100. [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  101. [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  102. [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
  103. [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
  104. [IB_WR_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
  105. [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
  106. [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
  107. };
  108. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  109. {
  110. return container_of(mqp, struct mlx4_ib_sqp, qp);
  111. }
  112. static int is_tunnel_qp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  113. {
  114. if (!mlx4_is_master(dev->dev))
  115. return 0;
  116. return qp->mqp.qpn >= dev->dev->phys_caps.base_tunnel_sqpn &&
  117. qp->mqp.qpn < dev->dev->phys_caps.base_tunnel_sqpn +
  118. 8 * MLX4_MFUNC_MAX;
  119. }
  120. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  121. {
  122. int proxy_sqp = 0;
  123. int real_sqp = 0;
  124. int i;
  125. /* PPF or Native -- real SQP */
  126. real_sqp = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  127. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  128. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 3);
  129. if (real_sqp)
  130. return 1;
  131. /* VF or PF -- proxy SQP */
  132. if (mlx4_is_mfunc(dev->dev)) {
  133. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  134. if (qp->mqp.qpn == dev->dev->caps.qp0_proxy[i] ||
  135. qp->mqp.qpn == dev->dev->caps.qp1_proxy[i]) {
  136. proxy_sqp = 1;
  137. break;
  138. }
  139. }
  140. }
  141. if (proxy_sqp)
  142. return 1;
  143. return !!(qp->flags & MLX4_IB_ROCE_V2_GSI_QP);
  144. }
  145. /* used for INIT/CLOSE port logic */
  146. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  147. {
  148. int proxy_qp0 = 0;
  149. int real_qp0 = 0;
  150. int i;
  151. /* PPF or Native -- real QP0 */
  152. real_qp0 = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
  153. qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
  154. qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 1);
  155. if (real_qp0)
  156. return 1;
  157. /* VF or PF -- proxy QP0 */
  158. if (mlx4_is_mfunc(dev->dev)) {
  159. for (i = 0; i < dev->dev->caps.num_ports; i++) {
  160. if (qp->mqp.qpn == dev->dev->caps.qp0_proxy[i]) {
  161. proxy_qp0 = 1;
  162. break;
  163. }
  164. }
  165. }
  166. return proxy_qp0;
  167. }
  168. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  169. {
  170. return mlx4_buf_offset(&qp->buf, offset);
  171. }
  172. static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
  173. {
  174. return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
  175. }
  176. static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
  177. {
  178. return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
  179. }
  180. /*
  181. * Stamp a SQ WQE so that it is invalid if prefetched by marking the
  182. * first four bytes of every 64 byte chunk with
  183. * 0x7FFFFFF | (invalid_ownership_value << 31).
  184. *
  185. * When the max work request size is less than or equal to the WQE
  186. * basic block size, as an optimization, we can stamp all WQEs with
  187. * 0xffffffff, and skip the very first chunk of each WQE.
  188. */
  189. static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n, int size)
  190. {
  191. __be32 *wqe;
  192. int i;
  193. int s;
  194. int ind;
  195. void *buf;
  196. __be32 stamp;
  197. struct mlx4_wqe_ctrl_seg *ctrl;
  198. if (qp->sq_max_wqes_per_wr > 1) {
  199. s = roundup(size, 1U << qp->sq.wqe_shift);
  200. for (i = 0; i < s; i += 64) {
  201. ind = (i >> qp->sq.wqe_shift) + n;
  202. stamp = ind & qp->sq.wqe_cnt ? cpu_to_be32(0x7fffffff) :
  203. cpu_to_be32(0xffffffff);
  204. buf = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  205. wqe = buf + (i & ((1 << qp->sq.wqe_shift) - 1));
  206. *wqe = stamp;
  207. }
  208. } else {
  209. ctrl = buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  210. s = (ctrl->qpn_vlan.fence_size & 0x3f) << 4;
  211. for (i = 64; i < s; i += 64) {
  212. wqe = buf + i;
  213. *wqe = cpu_to_be32(0xffffffff);
  214. }
  215. }
  216. }
  217. static void post_nop_wqe(struct mlx4_ib_qp *qp, int n, int size)
  218. {
  219. struct mlx4_wqe_ctrl_seg *ctrl;
  220. struct mlx4_wqe_inline_seg *inl;
  221. void *wqe;
  222. int s;
  223. ctrl = wqe = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  224. s = sizeof(struct mlx4_wqe_ctrl_seg);
  225. if (qp->ibqp.qp_type == IB_QPT_UD) {
  226. struct mlx4_wqe_datagram_seg *dgram = wqe + sizeof *ctrl;
  227. struct mlx4_av *av = (struct mlx4_av *)dgram->av;
  228. memset(dgram, 0, sizeof *dgram);
  229. av->port_pd = cpu_to_be32((qp->port << 24) | to_mpd(qp->ibqp.pd)->pdn);
  230. s += sizeof(struct mlx4_wqe_datagram_seg);
  231. }
  232. /* Pad the remainder of the WQE with an inline data segment. */
  233. if (size > s) {
  234. inl = wqe + s;
  235. inl->byte_count = cpu_to_be32(1 << 31 | (size - s - sizeof *inl));
  236. }
  237. ctrl->srcrb_flags = 0;
  238. ctrl->qpn_vlan.fence_size = size / 16;
  239. /*
  240. * Make sure descriptor is fully written before setting ownership bit
  241. * (because HW can start executing as soon as we do).
  242. */
  243. wmb();
  244. ctrl->owner_opcode = cpu_to_be32(MLX4_OPCODE_NOP | MLX4_WQE_CTRL_NEC) |
  245. (n & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  246. stamp_send_wqe(qp, n + qp->sq_spare_wqes, size);
  247. }
  248. /* Post NOP WQE to prevent wrap-around in the middle of WR */
  249. static inline unsigned pad_wraparound(struct mlx4_ib_qp *qp, int ind)
  250. {
  251. unsigned s = qp->sq.wqe_cnt - (ind & (qp->sq.wqe_cnt - 1));
  252. if (unlikely(s < qp->sq_max_wqes_per_wr)) {
  253. post_nop_wqe(qp, ind, s << qp->sq.wqe_shift);
  254. ind += s;
  255. }
  256. return ind;
  257. }
  258. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  259. {
  260. struct ib_event event;
  261. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  262. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  263. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  264. if (ibqp->event_handler) {
  265. event.device = ibqp->device;
  266. event.element.qp = ibqp;
  267. switch (type) {
  268. case MLX4_EVENT_TYPE_PATH_MIG:
  269. event.event = IB_EVENT_PATH_MIG;
  270. break;
  271. case MLX4_EVENT_TYPE_COMM_EST:
  272. event.event = IB_EVENT_COMM_EST;
  273. break;
  274. case MLX4_EVENT_TYPE_SQ_DRAINED:
  275. event.event = IB_EVENT_SQ_DRAINED;
  276. break;
  277. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  278. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  279. break;
  280. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  281. event.event = IB_EVENT_QP_FATAL;
  282. break;
  283. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  284. event.event = IB_EVENT_PATH_MIG_ERR;
  285. break;
  286. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  287. event.event = IB_EVENT_QP_REQ_ERR;
  288. break;
  289. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  290. event.event = IB_EVENT_QP_ACCESS_ERR;
  291. break;
  292. default:
  293. pr_warn("Unexpected event type %d "
  294. "on QP %06x\n", type, qp->qpn);
  295. return;
  296. }
  297. ibqp->event_handler(&event, ibqp->qp_context);
  298. }
  299. }
  300. static int send_wqe_overhead(enum mlx4_ib_qp_type type, u32 flags)
  301. {
  302. /*
  303. * UD WQEs must have a datagram segment.
  304. * RC and UC WQEs might have a remote address segment.
  305. * MLX WQEs need two extra inline data segments (for the UD
  306. * header and space for the ICRC).
  307. */
  308. switch (type) {
  309. case MLX4_IB_QPT_UD:
  310. return sizeof (struct mlx4_wqe_ctrl_seg) +
  311. sizeof (struct mlx4_wqe_datagram_seg) +
  312. ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
  313. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  314. case MLX4_IB_QPT_PROXY_SMI:
  315. case MLX4_IB_QPT_PROXY_GSI:
  316. return sizeof (struct mlx4_wqe_ctrl_seg) +
  317. sizeof (struct mlx4_wqe_datagram_seg) + 64;
  318. case MLX4_IB_QPT_TUN_SMI_OWNER:
  319. case MLX4_IB_QPT_TUN_GSI:
  320. return sizeof (struct mlx4_wqe_ctrl_seg) +
  321. sizeof (struct mlx4_wqe_datagram_seg);
  322. case MLX4_IB_QPT_UC:
  323. return sizeof (struct mlx4_wqe_ctrl_seg) +
  324. sizeof (struct mlx4_wqe_raddr_seg);
  325. case MLX4_IB_QPT_RC:
  326. return sizeof (struct mlx4_wqe_ctrl_seg) +
  327. sizeof (struct mlx4_wqe_masked_atomic_seg) +
  328. sizeof (struct mlx4_wqe_raddr_seg);
  329. case MLX4_IB_QPT_SMI:
  330. case MLX4_IB_QPT_GSI:
  331. return sizeof (struct mlx4_wqe_ctrl_seg) +
  332. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  333. DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
  334. MLX4_INLINE_ALIGN) *
  335. sizeof (struct mlx4_wqe_inline_seg),
  336. sizeof (struct mlx4_wqe_data_seg)) +
  337. ALIGN(4 +
  338. sizeof (struct mlx4_wqe_inline_seg),
  339. sizeof (struct mlx4_wqe_data_seg));
  340. default:
  341. return sizeof (struct mlx4_wqe_ctrl_seg);
  342. }
  343. }
  344. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  345. int is_user, int has_rq, struct mlx4_ib_qp *qp)
  346. {
  347. /* Sanity check RQ size before proceeding */
  348. if (cap->max_recv_wr > dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE ||
  349. cap->max_recv_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg))
  350. return -EINVAL;
  351. if (!has_rq) {
  352. if (cap->max_recv_wr)
  353. return -EINVAL;
  354. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  355. } else {
  356. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  357. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
  358. return -EINVAL;
  359. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  360. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  361. qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
  362. }
  363. /* leave userspace return values as they were, so as not to break ABI */
  364. if (is_user) {
  365. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  366. cap->max_recv_sge = qp->rq.max_gs;
  367. } else {
  368. cap->max_recv_wr = qp->rq.max_post =
  369. min(dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE, qp->rq.wqe_cnt);
  370. cap->max_recv_sge = min(qp->rq.max_gs,
  371. min(dev->dev->caps.max_sq_sg,
  372. dev->dev->caps.max_rq_sg));
  373. }
  374. return 0;
  375. }
  376. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  377. enum mlx4_ib_qp_type type, struct mlx4_ib_qp *qp,
  378. bool shrink_wqe)
  379. {
  380. int s;
  381. /* Sanity check SQ size before proceeding */
  382. if (cap->max_send_wr > (dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE) ||
  383. cap->max_send_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg) ||
  384. cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
  385. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  386. return -EINVAL;
  387. /*
  388. * For MLX transport we need 2 extra S/G entries:
  389. * one for the header and one for the checksum at the end
  390. */
  391. if ((type == MLX4_IB_QPT_SMI || type == MLX4_IB_QPT_GSI ||
  392. type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) &&
  393. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  394. return -EINVAL;
  395. s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
  396. cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
  397. send_wqe_overhead(type, qp->flags);
  398. if (s > dev->dev->caps.max_sq_desc_sz)
  399. return -EINVAL;
  400. /*
  401. * Hermon supports shrinking WQEs, such that a single work
  402. * request can include multiple units of 1 << wqe_shift. This
  403. * way, work requests can differ in size, and do not have to
  404. * be a power of 2 in size, saving memory and speeding up send
  405. * WR posting. Unfortunately, if we do this then the
  406. * wqe_index field in CQEs can't be used to look up the WR ID
  407. * anymore, so we do this only if selective signaling is off.
  408. *
  409. * Further, on 32-bit platforms, we can't use vmap() to make
  410. * the QP buffer virtually contiguous. Thus we have to use
  411. * constant-sized WRs to make sure a WR is always fully within
  412. * a single page-sized chunk.
  413. *
  414. * Finally, we use NOP work requests to pad the end of the
  415. * work queue, to avoid wrap-around in the middle of WR. We
  416. * set NEC bit to avoid getting completions with error for
  417. * these NOP WRs, but since NEC is only supported starting
  418. * with firmware 2.2.232, we use constant-sized WRs for older
  419. * firmware.
  420. *
  421. * And, since MLX QPs only support SEND, we use constant-sized
  422. * WRs in this case.
  423. *
  424. * We look for the smallest value of wqe_shift such that the
  425. * resulting number of wqes does not exceed device
  426. * capabilities.
  427. *
  428. * We set WQE size to at least 64 bytes, this way stamping
  429. * invalidates each WQE.
  430. */
  431. if (shrink_wqe && dev->dev->caps.fw_ver >= MLX4_FW_VER_WQE_CTRL_NEC &&
  432. qp->sq_signal_bits && BITS_PER_LONG == 64 &&
  433. type != MLX4_IB_QPT_SMI && type != MLX4_IB_QPT_GSI &&
  434. !(type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_PROXY_SMI |
  435. MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER)))
  436. qp->sq.wqe_shift = ilog2(64);
  437. else
  438. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
  439. for (;;) {
  440. qp->sq_max_wqes_per_wr = DIV_ROUND_UP(s, 1U << qp->sq.wqe_shift);
  441. /*
  442. * We need to leave 2 KB + 1 WR of headroom in the SQ to
  443. * allow HW to prefetch.
  444. */
  445. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + qp->sq_max_wqes_per_wr;
  446. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr *
  447. qp->sq_max_wqes_per_wr +
  448. qp->sq_spare_wqes);
  449. if (qp->sq.wqe_cnt <= dev->dev->caps.max_wqes)
  450. break;
  451. if (qp->sq_max_wqes_per_wr <= 1)
  452. return -EINVAL;
  453. ++qp->sq.wqe_shift;
  454. }
  455. qp->sq.max_gs = (min(dev->dev->caps.max_sq_desc_sz,
  456. (qp->sq_max_wqes_per_wr << qp->sq.wqe_shift)) -
  457. send_wqe_overhead(type, qp->flags)) /
  458. sizeof (struct mlx4_wqe_data_seg);
  459. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  460. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  461. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  462. qp->rq.offset = 0;
  463. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  464. } else {
  465. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  466. qp->sq.offset = 0;
  467. }
  468. cap->max_send_wr = qp->sq.max_post =
  469. (qp->sq.wqe_cnt - qp->sq_spare_wqes) / qp->sq_max_wqes_per_wr;
  470. cap->max_send_sge = min(qp->sq.max_gs,
  471. min(dev->dev->caps.max_sq_sg,
  472. dev->dev->caps.max_rq_sg));
  473. /* We don't support inline sends for kernel QPs (yet) */
  474. cap->max_inline_data = 0;
  475. return 0;
  476. }
  477. static int set_user_sq_size(struct mlx4_ib_dev *dev,
  478. struct mlx4_ib_qp *qp,
  479. struct mlx4_ib_create_qp *ucmd)
  480. {
  481. /* Sanity check SQ size before proceeding */
  482. if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes ||
  483. ucmd->log_sq_stride >
  484. ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
  485. ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
  486. return -EINVAL;
  487. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  488. qp->sq.wqe_shift = ucmd->log_sq_stride;
  489. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  490. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  491. return 0;
  492. }
  493. static int alloc_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  494. {
  495. int i;
  496. qp->sqp_proxy_rcv =
  497. kmalloc(sizeof (struct mlx4_ib_buf) * qp->rq.wqe_cnt,
  498. GFP_KERNEL);
  499. if (!qp->sqp_proxy_rcv)
  500. return -ENOMEM;
  501. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  502. qp->sqp_proxy_rcv[i].addr =
  503. kmalloc(sizeof (struct mlx4_ib_proxy_sqp_hdr),
  504. GFP_KERNEL);
  505. if (!qp->sqp_proxy_rcv[i].addr)
  506. goto err;
  507. qp->sqp_proxy_rcv[i].map =
  508. ib_dma_map_single(dev, qp->sqp_proxy_rcv[i].addr,
  509. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  510. DMA_FROM_DEVICE);
  511. if (ib_dma_mapping_error(dev, qp->sqp_proxy_rcv[i].map)) {
  512. kfree(qp->sqp_proxy_rcv[i].addr);
  513. goto err;
  514. }
  515. }
  516. return 0;
  517. err:
  518. while (i > 0) {
  519. --i;
  520. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  521. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  522. DMA_FROM_DEVICE);
  523. kfree(qp->sqp_proxy_rcv[i].addr);
  524. }
  525. kfree(qp->sqp_proxy_rcv);
  526. qp->sqp_proxy_rcv = NULL;
  527. return -ENOMEM;
  528. }
  529. static void free_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
  530. {
  531. int i;
  532. for (i = 0; i < qp->rq.wqe_cnt; i++) {
  533. ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
  534. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  535. DMA_FROM_DEVICE);
  536. kfree(qp->sqp_proxy_rcv[i].addr);
  537. }
  538. kfree(qp->sqp_proxy_rcv);
  539. }
  540. static int qp_has_rq(struct ib_qp_init_attr *attr)
  541. {
  542. if (attr->qp_type == IB_QPT_XRC_INI || attr->qp_type == IB_QPT_XRC_TGT)
  543. return 0;
  544. return !attr->srq;
  545. }
  546. static int qp0_enabled_vf(struct mlx4_dev *dev, int qpn)
  547. {
  548. int i;
  549. for (i = 0; i < dev->caps.num_ports; i++) {
  550. if (qpn == dev->caps.qp0_proxy[i])
  551. return !!dev->caps.qp0_qkey[i];
  552. }
  553. return 0;
  554. }
  555. static void mlx4_ib_free_qp_counter(struct mlx4_ib_dev *dev,
  556. struct mlx4_ib_qp *qp)
  557. {
  558. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  559. mlx4_counter_free(dev->dev, qp->counter_index->index);
  560. list_del(&qp->counter_index->list);
  561. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  562. kfree(qp->counter_index);
  563. qp->counter_index = NULL;
  564. }
  565. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  566. struct ib_qp_init_attr *init_attr,
  567. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp **caller_qp,
  568. gfp_t gfp)
  569. {
  570. int qpn;
  571. int err;
  572. struct ib_qp_cap backup_cap;
  573. struct mlx4_ib_sqp *sqp;
  574. struct mlx4_ib_qp *qp;
  575. enum mlx4_ib_qp_type qp_type = (enum mlx4_ib_qp_type) init_attr->qp_type;
  576. struct mlx4_ib_cq *mcq;
  577. unsigned long flags;
  578. /* When tunneling special qps, we use a plain UD qp */
  579. if (sqpn) {
  580. if (mlx4_is_mfunc(dev->dev) &&
  581. (!mlx4_is_master(dev->dev) ||
  582. !(init_attr->create_flags & MLX4_IB_SRIOV_SQP))) {
  583. if (init_attr->qp_type == IB_QPT_GSI)
  584. qp_type = MLX4_IB_QPT_PROXY_GSI;
  585. else {
  586. if (mlx4_is_master(dev->dev) ||
  587. qp0_enabled_vf(dev->dev, sqpn))
  588. qp_type = MLX4_IB_QPT_PROXY_SMI_OWNER;
  589. else
  590. qp_type = MLX4_IB_QPT_PROXY_SMI;
  591. }
  592. }
  593. qpn = sqpn;
  594. /* add extra sg entry for tunneling */
  595. init_attr->cap.max_recv_sge++;
  596. } else if (init_attr->create_flags & MLX4_IB_SRIOV_TUNNEL_QP) {
  597. struct mlx4_ib_qp_tunnel_init_attr *tnl_init =
  598. container_of(init_attr,
  599. struct mlx4_ib_qp_tunnel_init_attr, init_attr);
  600. if ((tnl_init->proxy_qp_type != IB_QPT_SMI &&
  601. tnl_init->proxy_qp_type != IB_QPT_GSI) ||
  602. !mlx4_is_master(dev->dev))
  603. return -EINVAL;
  604. if (tnl_init->proxy_qp_type == IB_QPT_GSI)
  605. qp_type = MLX4_IB_QPT_TUN_GSI;
  606. else if (tnl_init->slave == mlx4_master_func_num(dev->dev) ||
  607. mlx4_vf_smi_enabled(dev->dev, tnl_init->slave,
  608. tnl_init->port))
  609. qp_type = MLX4_IB_QPT_TUN_SMI_OWNER;
  610. else
  611. qp_type = MLX4_IB_QPT_TUN_SMI;
  612. /* we are definitely in the PPF here, since we are creating
  613. * tunnel QPs. base_tunnel_sqpn is therefore valid. */
  614. qpn = dev->dev->phys_caps.base_tunnel_sqpn + 8 * tnl_init->slave
  615. + tnl_init->proxy_qp_type * 2 + tnl_init->port - 1;
  616. sqpn = qpn;
  617. }
  618. if (!*caller_qp) {
  619. if (qp_type == MLX4_IB_QPT_SMI || qp_type == MLX4_IB_QPT_GSI ||
  620. (qp_type & (MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_SMI_OWNER |
  621. MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  622. sqp = kzalloc(sizeof (struct mlx4_ib_sqp), gfp);
  623. if (!sqp)
  624. return -ENOMEM;
  625. qp = &sqp->qp;
  626. qp->pri.vid = 0xFFFF;
  627. qp->alt.vid = 0xFFFF;
  628. } else {
  629. qp = kzalloc(sizeof (struct mlx4_ib_qp), gfp);
  630. if (!qp)
  631. return -ENOMEM;
  632. qp->pri.vid = 0xFFFF;
  633. qp->alt.vid = 0xFFFF;
  634. }
  635. } else
  636. qp = *caller_qp;
  637. qp->mlx4_ib_qp_type = qp_type;
  638. mutex_init(&qp->mutex);
  639. spin_lock_init(&qp->sq.lock);
  640. spin_lock_init(&qp->rq.lock);
  641. INIT_LIST_HEAD(&qp->gid_list);
  642. INIT_LIST_HEAD(&qp->steering_rules);
  643. qp->state = IB_QPS_RESET;
  644. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  645. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  646. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, qp_has_rq(init_attr), qp);
  647. if (err)
  648. goto err;
  649. if (pd->uobject) {
  650. struct mlx4_ib_create_qp ucmd;
  651. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  652. err = -EFAULT;
  653. goto err;
  654. }
  655. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  656. err = set_user_sq_size(dev, qp, &ucmd);
  657. if (err)
  658. goto err;
  659. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  660. qp->buf_size, 0, 0);
  661. if (IS_ERR(qp->umem)) {
  662. err = PTR_ERR(qp->umem);
  663. goto err;
  664. }
  665. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  666. ilog2(qp->umem->page_size), &qp->mtt);
  667. if (err)
  668. goto err_buf;
  669. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  670. if (err)
  671. goto err_mtt;
  672. if (qp_has_rq(init_attr)) {
  673. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  674. ucmd.db_addr, &qp->db);
  675. if (err)
  676. goto err_mtt;
  677. }
  678. } else {
  679. qp->sq_no_prefetch = 0;
  680. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
  681. qp->flags |= MLX4_IB_QP_LSO;
  682. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  683. if (dev->steering_support ==
  684. MLX4_STEERING_MODE_DEVICE_MANAGED)
  685. qp->flags |= MLX4_IB_QP_NETIF;
  686. else
  687. goto err;
  688. }
  689. memcpy(&backup_cap, &init_attr->cap, sizeof(backup_cap));
  690. err = set_kernel_sq_size(dev, &init_attr->cap,
  691. qp_type, qp, true);
  692. if (err)
  693. goto err;
  694. if (qp_has_rq(init_attr)) {
  695. err = mlx4_db_alloc(dev->dev, &qp->db, 0, gfp);
  696. if (err)
  697. goto err;
  698. *qp->db.db = 0;
  699. }
  700. if (mlx4_buf_alloc(dev->dev, qp->buf_size, qp->buf_size,
  701. &qp->buf, gfp)) {
  702. memcpy(&init_attr->cap, &backup_cap,
  703. sizeof(backup_cap));
  704. err = set_kernel_sq_size(dev, &init_attr->cap, qp_type,
  705. qp, false);
  706. if (err)
  707. goto err_db;
  708. if (mlx4_buf_alloc(dev->dev, qp->buf_size,
  709. PAGE_SIZE * 2, &qp->buf, gfp)) {
  710. err = -ENOMEM;
  711. goto err_db;
  712. }
  713. }
  714. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  715. &qp->mtt);
  716. if (err)
  717. goto err_buf;
  718. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf, gfp);
  719. if (err)
  720. goto err_mtt;
  721. qp->sq.wrid = kmalloc_array(qp->sq.wqe_cnt, sizeof(u64),
  722. gfp | __GFP_NOWARN);
  723. if (!qp->sq.wrid)
  724. qp->sq.wrid = __vmalloc(qp->sq.wqe_cnt * sizeof(u64),
  725. gfp, PAGE_KERNEL);
  726. qp->rq.wrid = kmalloc_array(qp->rq.wqe_cnt, sizeof(u64),
  727. gfp | __GFP_NOWARN);
  728. if (!qp->rq.wrid)
  729. qp->rq.wrid = __vmalloc(qp->rq.wqe_cnt * sizeof(u64),
  730. gfp, PAGE_KERNEL);
  731. if (!qp->sq.wrid || !qp->rq.wrid) {
  732. err = -ENOMEM;
  733. goto err_wrid;
  734. }
  735. }
  736. if (sqpn) {
  737. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  738. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  739. if (alloc_proxy_bufs(pd->device, qp)) {
  740. err = -ENOMEM;
  741. goto err_wrid;
  742. }
  743. }
  744. } else {
  745. /* Raw packet QPNs may not have bits 6,7 set in their qp_num;
  746. * otherwise, the WQE BlueFlame setup flow wrongly causes
  747. * VLAN insertion. */
  748. if (init_attr->qp_type == IB_QPT_RAW_PACKET)
  749. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn,
  750. (init_attr->cap.max_send_wr ?
  751. MLX4_RESERVE_ETH_BF_QP : 0) |
  752. (init_attr->cap.max_recv_wr ?
  753. MLX4_RESERVE_A0_QP : 0));
  754. else
  755. if (qp->flags & MLX4_IB_QP_NETIF)
  756. err = mlx4_ib_steer_qp_alloc(dev, 1, &qpn);
  757. else
  758. err = mlx4_qp_reserve_range(dev->dev, 1, 1,
  759. &qpn, 0);
  760. if (err)
  761. goto err_proxy;
  762. }
  763. if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
  764. qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  765. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp, gfp);
  766. if (err)
  767. goto err_qpn;
  768. if (init_attr->qp_type == IB_QPT_XRC_TGT)
  769. qp->mqp.qpn |= (1 << 23);
  770. /*
  771. * Hardware wants QPN written in big-endian order (after
  772. * shifting) for send doorbell. Precompute this value to save
  773. * a little bit when posting sends.
  774. */
  775. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  776. qp->mqp.event = mlx4_ib_qp_event;
  777. if (!*caller_qp)
  778. *caller_qp = qp;
  779. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  780. mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq),
  781. to_mcq(init_attr->recv_cq));
  782. /* Maintain device to QPs access, needed for further handling
  783. * via reset flow
  784. */
  785. list_add_tail(&qp->qps_list, &dev->qp_list);
  786. /* Maintain CQ to QPs access, needed for further handling
  787. * via reset flow
  788. */
  789. mcq = to_mcq(init_attr->send_cq);
  790. list_add_tail(&qp->cq_send_list, &mcq->send_qp_list);
  791. mcq = to_mcq(init_attr->recv_cq);
  792. list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list);
  793. mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq),
  794. to_mcq(init_attr->recv_cq));
  795. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  796. return 0;
  797. err_qpn:
  798. if (!sqpn) {
  799. if (qp->flags & MLX4_IB_QP_NETIF)
  800. mlx4_ib_steer_qp_free(dev, qpn, 1);
  801. else
  802. mlx4_qp_release_range(dev->dev, qpn, 1);
  803. }
  804. err_proxy:
  805. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  806. free_proxy_bufs(pd->device, qp);
  807. err_wrid:
  808. if (pd->uobject) {
  809. if (qp_has_rq(init_attr))
  810. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  811. } else {
  812. kvfree(qp->sq.wrid);
  813. kvfree(qp->rq.wrid);
  814. }
  815. err_mtt:
  816. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  817. err_buf:
  818. if (pd->uobject)
  819. ib_umem_release(qp->umem);
  820. else
  821. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  822. err_db:
  823. if (!pd->uobject && qp_has_rq(init_attr))
  824. mlx4_db_free(dev->dev, &qp->db);
  825. err:
  826. if (!*caller_qp)
  827. kfree(qp);
  828. return err;
  829. }
  830. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  831. {
  832. switch (state) {
  833. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  834. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  835. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  836. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  837. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  838. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  839. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  840. default: return -1;
  841. }
  842. }
  843. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  844. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  845. {
  846. if (send_cq == recv_cq) {
  847. spin_lock(&send_cq->lock);
  848. __acquire(&recv_cq->lock);
  849. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  850. spin_lock(&send_cq->lock);
  851. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  852. } else {
  853. spin_lock(&recv_cq->lock);
  854. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  855. }
  856. }
  857. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  858. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  859. {
  860. if (send_cq == recv_cq) {
  861. __release(&recv_cq->lock);
  862. spin_unlock(&send_cq->lock);
  863. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  864. spin_unlock(&recv_cq->lock);
  865. spin_unlock(&send_cq->lock);
  866. } else {
  867. spin_unlock(&send_cq->lock);
  868. spin_unlock(&recv_cq->lock);
  869. }
  870. }
  871. static void del_gid_entries(struct mlx4_ib_qp *qp)
  872. {
  873. struct mlx4_ib_gid_entry *ge, *tmp;
  874. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  875. list_del(&ge->list);
  876. kfree(ge);
  877. }
  878. }
  879. static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp)
  880. {
  881. if (qp->ibqp.qp_type == IB_QPT_XRC_TGT)
  882. return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd);
  883. else
  884. return to_mpd(qp->ibqp.pd);
  885. }
  886. static void get_cqs(struct mlx4_ib_qp *qp,
  887. struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq)
  888. {
  889. switch (qp->ibqp.qp_type) {
  890. case IB_QPT_XRC_TGT:
  891. *send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq);
  892. *recv_cq = *send_cq;
  893. break;
  894. case IB_QPT_XRC_INI:
  895. *send_cq = to_mcq(qp->ibqp.send_cq);
  896. *recv_cq = *send_cq;
  897. break;
  898. default:
  899. *send_cq = to_mcq(qp->ibqp.send_cq);
  900. *recv_cq = to_mcq(qp->ibqp.recv_cq);
  901. break;
  902. }
  903. }
  904. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  905. int is_user)
  906. {
  907. struct mlx4_ib_cq *send_cq, *recv_cq;
  908. unsigned long flags;
  909. if (qp->state != IB_QPS_RESET) {
  910. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  911. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  912. pr_warn("modify QP %06x to RESET failed.\n",
  913. qp->mqp.qpn);
  914. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  915. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  916. qp->pri.smac = 0;
  917. qp->pri.smac_port = 0;
  918. }
  919. if (qp->alt.smac) {
  920. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  921. qp->alt.smac = 0;
  922. }
  923. if (qp->pri.vid < 0x1000) {
  924. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  925. qp->pri.vid = 0xFFFF;
  926. qp->pri.candidate_vid = 0xFFFF;
  927. qp->pri.update_vid = 0;
  928. }
  929. if (qp->alt.vid < 0x1000) {
  930. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  931. qp->alt.vid = 0xFFFF;
  932. qp->alt.candidate_vid = 0xFFFF;
  933. qp->alt.update_vid = 0;
  934. }
  935. }
  936. get_cqs(qp, &send_cq, &recv_cq);
  937. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  938. mlx4_ib_lock_cqs(send_cq, recv_cq);
  939. /* del from lists under both locks above to protect reset flow paths */
  940. list_del(&qp->qps_list);
  941. list_del(&qp->cq_send_list);
  942. list_del(&qp->cq_recv_list);
  943. if (!is_user) {
  944. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  945. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  946. if (send_cq != recv_cq)
  947. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  948. }
  949. mlx4_qp_remove(dev->dev, &qp->mqp);
  950. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  951. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  952. mlx4_qp_free(dev->dev, &qp->mqp);
  953. if (!is_sqp(dev, qp) && !is_tunnel_qp(dev, qp)) {
  954. if (qp->flags & MLX4_IB_QP_NETIF)
  955. mlx4_ib_steer_qp_free(dev, qp->mqp.qpn, 1);
  956. else
  957. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  958. }
  959. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  960. if (is_user) {
  961. if (qp->rq.wqe_cnt)
  962. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  963. &qp->db);
  964. ib_umem_release(qp->umem);
  965. } else {
  966. kvfree(qp->sq.wrid);
  967. kvfree(qp->rq.wrid);
  968. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  969. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI))
  970. free_proxy_bufs(&dev->ib_dev, qp);
  971. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  972. if (qp->rq.wqe_cnt)
  973. mlx4_db_free(dev->dev, &qp->db);
  974. }
  975. del_gid_entries(qp);
  976. }
  977. static u32 get_sqp_num(struct mlx4_ib_dev *dev, struct ib_qp_init_attr *attr)
  978. {
  979. /* Native or PPF */
  980. if (!mlx4_is_mfunc(dev->dev) ||
  981. (mlx4_is_master(dev->dev) &&
  982. attr->create_flags & MLX4_IB_SRIOV_SQP)) {
  983. return dev->dev->phys_caps.base_sqpn +
  984. (attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  985. attr->port_num - 1;
  986. }
  987. /* PF or VF -- creating proxies */
  988. if (attr->qp_type == IB_QPT_SMI)
  989. return dev->dev->caps.qp0_proxy[attr->port_num - 1];
  990. else
  991. return dev->dev->caps.qp1_proxy[attr->port_num - 1];
  992. }
  993. static struct ib_qp *_mlx4_ib_create_qp(struct ib_pd *pd,
  994. struct ib_qp_init_attr *init_attr,
  995. struct ib_udata *udata)
  996. {
  997. struct mlx4_ib_qp *qp = NULL;
  998. int err;
  999. int sup_u_create_flags = MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  1000. u16 xrcdn = 0;
  1001. gfp_t gfp;
  1002. gfp = (init_attr->create_flags & MLX4_IB_QP_CREATE_USE_GFP_NOIO) ?
  1003. GFP_NOIO : GFP_KERNEL;
  1004. /*
  1005. * We only support LSO, vendor flag1, and multicast loopback blocking,
  1006. * and only for kernel UD QPs.
  1007. */
  1008. if (init_attr->create_flags & ~(MLX4_IB_QP_LSO |
  1009. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK |
  1010. MLX4_IB_SRIOV_TUNNEL_QP |
  1011. MLX4_IB_SRIOV_SQP |
  1012. MLX4_IB_QP_NETIF |
  1013. MLX4_IB_QP_CREATE_ROCE_V2_GSI |
  1014. MLX4_IB_QP_CREATE_USE_GFP_NOIO))
  1015. return ERR_PTR(-EINVAL);
  1016. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  1017. if (init_attr->qp_type != IB_QPT_UD)
  1018. return ERR_PTR(-EINVAL);
  1019. }
  1020. if (init_attr->create_flags) {
  1021. if (udata && init_attr->create_flags & ~(sup_u_create_flags))
  1022. return ERR_PTR(-EINVAL);
  1023. if ((init_attr->create_flags & ~(MLX4_IB_SRIOV_SQP |
  1024. MLX4_IB_QP_CREATE_USE_GFP_NOIO |
  1025. MLX4_IB_QP_CREATE_ROCE_V2_GSI |
  1026. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) &&
  1027. init_attr->qp_type != IB_QPT_UD) ||
  1028. (init_attr->create_flags & MLX4_IB_SRIOV_SQP &&
  1029. init_attr->qp_type > IB_QPT_GSI) ||
  1030. (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI &&
  1031. init_attr->qp_type != IB_QPT_GSI))
  1032. return ERR_PTR(-EINVAL);
  1033. }
  1034. switch (init_attr->qp_type) {
  1035. case IB_QPT_XRC_TGT:
  1036. pd = to_mxrcd(init_attr->xrcd)->pd;
  1037. xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
  1038. init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq;
  1039. /* fall through */
  1040. case IB_QPT_XRC_INI:
  1041. if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  1042. return ERR_PTR(-ENOSYS);
  1043. init_attr->recv_cq = init_attr->send_cq;
  1044. /* fall through */
  1045. case IB_QPT_RC:
  1046. case IB_QPT_UC:
  1047. case IB_QPT_RAW_PACKET:
  1048. qp = kzalloc(sizeof *qp, gfp);
  1049. if (!qp)
  1050. return ERR_PTR(-ENOMEM);
  1051. qp->pri.vid = 0xFFFF;
  1052. qp->alt.vid = 0xFFFF;
  1053. /* fall through */
  1054. case IB_QPT_UD:
  1055. {
  1056. err = create_qp_common(to_mdev(pd->device), pd, init_attr,
  1057. udata, 0, &qp, gfp);
  1058. if (err) {
  1059. kfree(qp);
  1060. return ERR_PTR(err);
  1061. }
  1062. qp->ibqp.qp_num = qp->mqp.qpn;
  1063. qp->xrcdn = xrcdn;
  1064. break;
  1065. }
  1066. case IB_QPT_SMI:
  1067. case IB_QPT_GSI:
  1068. {
  1069. int sqpn;
  1070. /* Userspace is not allowed to create special QPs: */
  1071. if (udata)
  1072. return ERR_PTR(-EINVAL);
  1073. if (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI) {
  1074. int res = mlx4_qp_reserve_range(to_mdev(pd->device)->dev, 1, 1, &sqpn, 0);
  1075. if (res)
  1076. return ERR_PTR(res);
  1077. } else {
  1078. sqpn = get_sqp_num(to_mdev(pd->device), init_attr);
  1079. }
  1080. err = create_qp_common(to_mdev(pd->device), pd, init_attr, udata,
  1081. sqpn,
  1082. &qp, gfp);
  1083. if (err)
  1084. return ERR_PTR(err);
  1085. qp->port = init_attr->port_num;
  1086. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 :
  1087. init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI ? sqpn : 1;
  1088. break;
  1089. }
  1090. default:
  1091. /* Don't support raw QPs */
  1092. return ERR_PTR(-EINVAL);
  1093. }
  1094. return &qp->ibqp;
  1095. }
  1096. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  1097. struct ib_qp_init_attr *init_attr,
  1098. struct ib_udata *udata) {
  1099. struct ib_device *device = pd ? pd->device : init_attr->xrcd->device;
  1100. struct ib_qp *ibqp;
  1101. struct mlx4_ib_dev *dev = to_mdev(device);
  1102. ibqp = _mlx4_ib_create_qp(pd, init_attr, udata);
  1103. if (!IS_ERR(ibqp) &&
  1104. (init_attr->qp_type == IB_QPT_GSI) &&
  1105. !(init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI)) {
  1106. struct mlx4_ib_sqp *sqp = to_msqp((to_mqp(ibqp)));
  1107. int is_eth = rdma_cap_eth_ah(&dev->ib_dev, init_attr->port_num);
  1108. if (is_eth &&
  1109. dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
  1110. init_attr->create_flags |= MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1111. sqp->roce_v2_gsi = ib_create_qp(pd, init_attr);
  1112. if (IS_ERR(sqp->roce_v2_gsi)) {
  1113. pr_err("Failed to create GSI QP for RoCEv2 (%ld)\n", PTR_ERR(sqp->roce_v2_gsi));
  1114. sqp->roce_v2_gsi = NULL;
  1115. } else {
  1116. sqp = to_msqp(to_mqp(sqp->roce_v2_gsi));
  1117. sqp->qp.flags |= MLX4_IB_ROCE_V2_GSI_QP;
  1118. }
  1119. init_attr->create_flags &= ~MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1120. }
  1121. }
  1122. return ibqp;
  1123. }
  1124. static int _mlx4_ib_destroy_qp(struct ib_qp *qp)
  1125. {
  1126. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  1127. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1128. struct mlx4_ib_pd *pd;
  1129. if (is_qp0(dev, mqp))
  1130. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  1131. if (dev->qp1_proxy[mqp->port - 1] == mqp) {
  1132. mutex_lock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1133. dev->qp1_proxy[mqp->port - 1] = NULL;
  1134. mutex_unlock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1135. }
  1136. if (mqp->counter_index)
  1137. mlx4_ib_free_qp_counter(dev, mqp);
  1138. pd = get_pd(mqp);
  1139. destroy_qp_common(dev, mqp, !!pd->ibpd.uobject);
  1140. if (is_sqp(dev, mqp))
  1141. kfree(to_msqp(mqp));
  1142. else
  1143. kfree(mqp);
  1144. return 0;
  1145. }
  1146. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  1147. {
  1148. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1149. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  1150. struct mlx4_ib_sqp *sqp = to_msqp(mqp);
  1151. if (sqp->roce_v2_gsi)
  1152. ib_destroy_qp(sqp->roce_v2_gsi);
  1153. }
  1154. return _mlx4_ib_destroy_qp(qp);
  1155. }
  1156. static int to_mlx4_st(struct mlx4_ib_dev *dev, enum mlx4_ib_qp_type type)
  1157. {
  1158. switch (type) {
  1159. case MLX4_IB_QPT_RC: return MLX4_QP_ST_RC;
  1160. case MLX4_IB_QPT_UC: return MLX4_QP_ST_UC;
  1161. case MLX4_IB_QPT_UD: return MLX4_QP_ST_UD;
  1162. case MLX4_IB_QPT_XRC_INI:
  1163. case MLX4_IB_QPT_XRC_TGT: return MLX4_QP_ST_XRC;
  1164. case MLX4_IB_QPT_SMI:
  1165. case MLX4_IB_QPT_GSI:
  1166. case MLX4_IB_QPT_RAW_PACKET: return MLX4_QP_ST_MLX;
  1167. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  1168. case MLX4_IB_QPT_TUN_SMI_OWNER: return (mlx4_is_mfunc(dev->dev) ?
  1169. MLX4_QP_ST_MLX : -1);
  1170. case MLX4_IB_QPT_PROXY_SMI:
  1171. case MLX4_IB_QPT_TUN_SMI:
  1172. case MLX4_IB_QPT_PROXY_GSI:
  1173. case MLX4_IB_QPT_TUN_GSI: return (mlx4_is_mfunc(dev->dev) ?
  1174. MLX4_QP_ST_UD : -1);
  1175. default: return -1;
  1176. }
  1177. }
  1178. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  1179. int attr_mask)
  1180. {
  1181. u8 dest_rd_atomic;
  1182. u32 access_flags;
  1183. u32 hw_access_flags = 0;
  1184. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1185. dest_rd_atomic = attr->max_dest_rd_atomic;
  1186. else
  1187. dest_rd_atomic = qp->resp_depth;
  1188. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1189. access_flags = attr->qp_access_flags;
  1190. else
  1191. access_flags = qp->atomic_rd_en;
  1192. if (!dest_rd_atomic)
  1193. access_flags &= IB_ACCESS_REMOTE_WRITE;
  1194. if (access_flags & IB_ACCESS_REMOTE_READ)
  1195. hw_access_flags |= MLX4_QP_BIT_RRE;
  1196. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  1197. hw_access_flags |= MLX4_QP_BIT_RAE;
  1198. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  1199. hw_access_flags |= MLX4_QP_BIT_RWE;
  1200. return cpu_to_be32(hw_access_flags);
  1201. }
  1202. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  1203. int attr_mask)
  1204. {
  1205. if (attr_mask & IB_QP_PKEY_INDEX)
  1206. sqp->pkey_index = attr->pkey_index;
  1207. if (attr_mask & IB_QP_QKEY)
  1208. sqp->qkey = attr->qkey;
  1209. if (attr_mask & IB_QP_SQ_PSN)
  1210. sqp->send_psn = attr->sq_psn;
  1211. }
  1212. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  1213. {
  1214. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  1215. }
  1216. static int _mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  1217. u64 smac, u16 vlan_tag, struct mlx4_qp_path *path,
  1218. struct mlx4_roce_smac_vlan_info *smac_info, u8 port)
  1219. {
  1220. int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port) ==
  1221. IB_LINK_LAYER_ETHERNET;
  1222. int vidx;
  1223. int smac_index;
  1224. int err;
  1225. path->grh_mylmc = ah->src_path_bits & 0x7f;
  1226. path->rlid = cpu_to_be16(ah->dlid);
  1227. if (ah->static_rate) {
  1228. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  1229. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  1230. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  1231. --path->static_rate;
  1232. } else
  1233. path->static_rate = 0;
  1234. if (ah->ah_flags & IB_AH_GRH) {
  1235. int real_sgid_index = mlx4_ib_gid_index_to_real_index(dev,
  1236. port,
  1237. ah->grh.sgid_index);
  1238. if (real_sgid_index >= dev->dev->caps.gid_table_len[port]) {
  1239. pr_err("sgid_index (%u) too large. max is %d\n",
  1240. real_sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  1241. return -1;
  1242. }
  1243. path->grh_mylmc |= 1 << 7;
  1244. path->mgid_index = real_sgid_index;
  1245. path->hop_limit = ah->grh.hop_limit;
  1246. path->tclass_flowlabel =
  1247. cpu_to_be32((ah->grh.traffic_class << 20) |
  1248. (ah->grh.flow_label));
  1249. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  1250. }
  1251. if (is_eth) {
  1252. if (!(ah->ah_flags & IB_AH_GRH))
  1253. return -1;
  1254. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1255. ((port - 1) << 6) | ((ah->sl & 7) << 3);
  1256. path->feup |= MLX4_FEUP_FORCE_ETH_UP;
  1257. if (vlan_tag < 0x1000) {
  1258. if (smac_info->vid < 0x1000) {
  1259. /* both valid vlan ids */
  1260. if (smac_info->vid != vlan_tag) {
  1261. /* different VIDs. unreg old and reg new */
  1262. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1263. if (err)
  1264. return err;
  1265. smac_info->candidate_vid = vlan_tag;
  1266. smac_info->candidate_vlan_index = vidx;
  1267. smac_info->candidate_vlan_port = port;
  1268. smac_info->update_vid = 1;
  1269. path->vlan_index = vidx;
  1270. } else {
  1271. path->vlan_index = smac_info->vlan_index;
  1272. }
  1273. } else {
  1274. /* no current vlan tag in qp */
  1275. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1276. if (err)
  1277. return err;
  1278. smac_info->candidate_vid = vlan_tag;
  1279. smac_info->candidate_vlan_index = vidx;
  1280. smac_info->candidate_vlan_port = port;
  1281. smac_info->update_vid = 1;
  1282. path->vlan_index = vidx;
  1283. }
  1284. path->feup |= MLX4_FVL_FORCE_ETH_VLAN;
  1285. path->fl = 1 << 6;
  1286. } else {
  1287. /* have current vlan tag. unregister it at modify-qp success */
  1288. if (smac_info->vid < 0x1000) {
  1289. smac_info->candidate_vid = 0xFFFF;
  1290. smac_info->update_vid = 1;
  1291. }
  1292. }
  1293. /* get smac_index for RoCE use.
  1294. * If no smac was yet assigned, register one.
  1295. * If one was already assigned, but the new mac differs,
  1296. * unregister the old one and register the new one.
  1297. */
  1298. if ((!smac_info->smac && !smac_info->smac_port) ||
  1299. smac_info->smac != smac) {
  1300. /* register candidate now, unreg if needed, after success */
  1301. smac_index = mlx4_register_mac(dev->dev, port, smac);
  1302. if (smac_index >= 0) {
  1303. smac_info->candidate_smac_index = smac_index;
  1304. smac_info->candidate_smac = smac;
  1305. smac_info->candidate_smac_port = port;
  1306. } else {
  1307. return -EINVAL;
  1308. }
  1309. } else {
  1310. smac_index = smac_info->smac_index;
  1311. }
  1312. memcpy(path->dmac, ah->dmac, 6);
  1313. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  1314. /* put MAC table smac index for IBoE */
  1315. path->grh_mylmc = (u8) (smac_index) | 0x80;
  1316. } else {
  1317. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1318. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  1319. }
  1320. return 0;
  1321. }
  1322. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_qp_attr *qp,
  1323. enum ib_qp_attr_mask qp_attr_mask,
  1324. struct mlx4_ib_qp *mqp,
  1325. struct mlx4_qp_path *path, u8 port,
  1326. u16 vlan_id, u8 *smac)
  1327. {
  1328. return _mlx4_set_path(dev, &qp->ah_attr,
  1329. mlx4_mac_to_u64(smac),
  1330. vlan_id,
  1331. path, &mqp->pri, port);
  1332. }
  1333. static int mlx4_set_alt_path(struct mlx4_ib_dev *dev,
  1334. const struct ib_qp_attr *qp,
  1335. enum ib_qp_attr_mask qp_attr_mask,
  1336. struct mlx4_ib_qp *mqp,
  1337. struct mlx4_qp_path *path, u8 port)
  1338. {
  1339. return _mlx4_set_path(dev, &qp->alt_ah_attr,
  1340. 0,
  1341. 0xffff,
  1342. path, &mqp->alt, port);
  1343. }
  1344. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1345. {
  1346. struct mlx4_ib_gid_entry *ge, *tmp;
  1347. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1348. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  1349. ge->added = 1;
  1350. ge->port = qp->port;
  1351. }
  1352. }
  1353. }
  1354. static int handle_eth_ud_smac_index(struct mlx4_ib_dev *dev,
  1355. struct mlx4_ib_qp *qp,
  1356. struct mlx4_qp_context *context)
  1357. {
  1358. u64 u64_mac;
  1359. int smac_index;
  1360. u64_mac = atomic64_read(&dev->iboe.mac[qp->port - 1]);
  1361. context->pri_path.sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | ((qp->port - 1) << 6);
  1362. if (!qp->pri.smac && !qp->pri.smac_port) {
  1363. smac_index = mlx4_register_mac(dev->dev, qp->port, u64_mac);
  1364. if (smac_index >= 0) {
  1365. qp->pri.candidate_smac_index = smac_index;
  1366. qp->pri.candidate_smac = u64_mac;
  1367. qp->pri.candidate_smac_port = qp->port;
  1368. context->pri_path.grh_mylmc = 0x80 | (u8) smac_index;
  1369. } else {
  1370. return -ENOENT;
  1371. }
  1372. }
  1373. return 0;
  1374. }
  1375. static int create_qp_lb_counter(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1376. {
  1377. struct counter_index *new_counter_index;
  1378. int err;
  1379. u32 tmp_idx;
  1380. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) !=
  1381. IB_LINK_LAYER_ETHERNET ||
  1382. !(qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) ||
  1383. !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_LB_SRC_CHK))
  1384. return 0;
  1385. err = mlx4_counter_alloc(dev->dev, &tmp_idx);
  1386. if (err)
  1387. return err;
  1388. new_counter_index = kmalloc(sizeof(*new_counter_index), GFP_KERNEL);
  1389. if (!new_counter_index) {
  1390. mlx4_counter_free(dev->dev, tmp_idx);
  1391. return -ENOMEM;
  1392. }
  1393. new_counter_index->index = tmp_idx;
  1394. new_counter_index->allocated = 1;
  1395. qp->counter_index = new_counter_index;
  1396. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  1397. list_add_tail(&new_counter_index->list,
  1398. &dev->counters_table[qp->port - 1].counters_list);
  1399. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  1400. return 0;
  1401. }
  1402. enum {
  1403. MLX4_QPC_ROCE_MODE_1 = 0,
  1404. MLX4_QPC_ROCE_MODE_2 = 2,
  1405. MLX4_QPC_ROCE_MODE_UNDEFINED = 0xff
  1406. };
  1407. static u8 gid_type_to_qpc(enum ib_gid_type gid_type)
  1408. {
  1409. switch (gid_type) {
  1410. case IB_GID_TYPE_ROCE:
  1411. return MLX4_QPC_ROCE_MODE_1;
  1412. case IB_GID_TYPE_ROCE_UDP_ENCAP:
  1413. return MLX4_QPC_ROCE_MODE_2;
  1414. default:
  1415. return MLX4_QPC_ROCE_MODE_UNDEFINED;
  1416. }
  1417. }
  1418. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  1419. const struct ib_qp_attr *attr, int attr_mask,
  1420. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  1421. {
  1422. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1423. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1424. struct mlx4_ib_pd *pd;
  1425. struct mlx4_ib_cq *send_cq, *recv_cq;
  1426. struct mlx4_qp_context *context;
  1427. enum mlx4_qp_optpar optpar = 0;
  1428. int sqd_event;
  1429. int steer_qp = 0;
  1430. int err = -EINVAL;
  1431. int counter_index;
  1432. /* APM is not supported under RoCE */
  1433. if (attr_mask & IB_QP_ALT_PATH &&
  1434. rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  1435. IB_LINK_LAYER_ETHERNET)
  1436. return -ENOTSUPP;
  1437. context = kzalloc(sizeof *context, GFP_KERNEL);
  1438. if (!context)
  1439. return -ENOMEM;
  1440. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  1441. (to_mlx4_st(dev, qp->mlx4_ib_qp_type) << 16));
  1442. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  1443. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1444. else {
  1445. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  1446. switch (attr->path_mig_state) {
  1447. case IB_MIG_MIGRATED:
  1448. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1449. break;
  1450. case IB_MIG_REARM:
  1451. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  1452. break;
  1453. case IB_MIG_ARMED:
  1454. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  1455. break;
  1456. }
  1457. }
  1458. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
  1459. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  1460. else if (ibqp->qp_type == IB_QPT_RAW_PACKET)
  1461. context->mtu_msgmax = (MLX4_RAW_QP_MTU << 5) | MLX4_RAW_QP_MSGMAX;
  1462. else if (ibqp->qp_type == IB_QPT_UD) {
  1463. if (qp->flags & MLX4_IB_QP_LSO)
  1464. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  1465. ilog2(dev->dev->caps.max_gso_sz);
  1466. else
  1467. context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
  1468. } else if (attr_mask & IB_QP_PATH_MTU) {
  1469. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  1470. pr_err("path MTU (%u) is invalid\n",
  1471. attr->path_mtu);
  1472. goto out;
  1473. }
  1474. context->mtu_msgmax = (attr->path_mtu << 5) |
  1475. ilog2(dev->dev->caps.max_msg_sz);
  1476. }
  1477. if (qp->rq.wqe_cnt)
  1478. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  1479. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  1480. if (qp->sq.wqe_cnt)
  1481. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  1482. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  1483. if (new_state == IB_QPS_RESET && qp->counter_index)
  1484. mlx4_ib_free_qp_counter(dev, qp);
  1485. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1486. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  1487. context->xrcd = cpu_to_be32((u32) qp->xrcdn);
  1488. if (ibqp->qp_type == IB_QPT_RAW_PACKET)
  1489. context->param3 |= cpu_to_be32(1 << 30);
  1490. }
  1491. if (qp->ibqp.uobject)
  1492. context->usr_page = cpu_to_be32(
  1493. mlx4_to_hw_uar_index(dev->dev,
  1494. to_mucontext(ibqp->uobject->context)->uar.index));
  1495. else
  1496. context->usr_page = cpu_to_be32(
  1497. mlx4_to_hw_uar_index(dev->dev, dev->priv_uar.index));
  1498. if (attr_mask & IB_QP_DEST_QPN)
  1499. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  1500. if (attr_mask & IB_QP_PORT) {
  1501. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  1502. !(attr_mask & IB_QP_AV)) {
  1503. mlx4_set_sched(&context->pri_path, attr->port_num);
  1504. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  1505. }
  1506. }
  1507. if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
  1508. err = create_qp_lb_counter(dev, qp);
  1509. if (err)
  1510. goto out;
  1511. counter_index =
  1512. dev->counters_table[qp->port - 1].default_counter;
  1513. if (qp->counter_index)
  1514. counter_index = qp->counter_index->index;
  1515. if (counter_index != -1) {
  1516. context->pri_path.counter_index = counter_index;
  1517. optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
  1518. if (qp->counter_index) {
  1519. context->pri_path.fl |=
  1520. MLX4_FL_ETH_SRC_CHECK_MC_LB;
  1521. context->pri_path.vlan_control |=
  1522. MLX4_CTRL_ETH_SRC_CHECK_IF_COUNTER;
  1523. }
  1524. } else
  1525. context->pri_path.counter_index =
  1526. MLX4_SINK_COUNTER_INDEX(dev->dev);
  1527. if (qp->flags & MLX4_IB_QP_NETIF) {
  1528. mlx4_ib_steer_qp_reg(dev, qp, 1);
  1529. steer_qp = 1;
  1530. }
  1531. if (ibqp->qp_type == IB_QPT_GSI) {
  1532. enum ib_gid_type gid_type = qp->flags & MLX4_IB_ROCE_V2_GSI_QP ?
  1533. IB_GID_TYPE_ROCE_UDP_ENCAP : IB_GID_TYPE_ROCE;
  1534. u8 qpc_roce_mode = gid_type_to_qpc(gid_type);
  1535. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1536. }
  1537. }
  1538. if (attr_mask & IB_QP_PKEY_INDEX) {
  1539. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1540. context->pri_path.disable_pkey_check = 0x40;
  1541. context->pri_path.pkey_index = attr->pkey_index;
  1542. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  1543. }
  1544. if (attr_mask & IB_QP_AV) {
  1545. u8 port_num = mlx4_is_bonded(to_mdev(ibqp->device)->dev) ? 1 :
  1546. attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1547. union ib_gid gid;
  1548. struct ib_gid_attr gid_attr;
  1549. u16 vlan = 0xffff;
  1550. u8 smac[ETH_ALEN];
  1551. int status = 0;
  1552. int is_eth = rdma_cap_eth_ah(&dev->ib_dev, port_num) &&
  1553. attr->ah_attr.ah_flags & IB_AH_GRH;
  1554. if (is_eth) {
  1555. int index = attr->ah_attr.grh.sgid_index;
  1556. status = ib_get_cached_gid(ibqp->device, port_num,
  1557. index, &gid, &gid_attr);
  1558. if (!status && !memcmp(&gid, &zgid, sizeof(gid)))
  1559. status = -ENOENT;
  1560. if (!status && gid_attr.ndev) {
  1561. vlan = rdma_vlan_dev_vlan_id(gid_attr.ndev);
  1562. memcpy(smac, gid_attr.ndev->dev_addr, ETH_ALEN);
  1563. dev_put(gid_attr.ndev);
  1564. }
  1565. }
  1566. if (status)
  1567. goto out;
  1568. if (mlx4_set_path(dev, attr, attr_mask, qp, &context->pri_path,
  1569. port_num, vlan, smac))
  1570. goto out;
  1571. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  1572. MLX4_QP_OPTPAR_SCHED_QUEUE);
  1573. if (is_eth &&
  1574. (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR)) {
  1575. u8 qpc_roce_mode = gid_type_to_qpc(gid_attr.gid_type);
  1576. if (qpc_roce_mode == MLX4_QPC_ROCE_MODE_UNDEFINED) {
  1577. err = -EINVAL;
  1578. goto out;
  1579. }
  1580. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1581. }
  1582. }
  1583. if (attr_mask & IB_QP_TIMEOUT) {
  1584. context->pri_path.ackto |= attr->timeout << 3;
  1585. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  1586. }
  1587. if (attr_mask & IB_QP_ALT_PATH) {
  1588. if (attr->alt_port_num == 0 ||
  1589. attr->alt_port_num > dev->dev->caps.num_ports)
  1590. goto out;
  1591. if (attr->alt_pkey_index >=
  1592. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  1593. goto out;
  1594. if (mlx4_set_alt_path(dev, attr, attr_mask, qp,
  1595. &context->alt_path,
  1596. attr->alt_port_num))
  1597. goto out;
  1598. context->alt_path.pkey_index = attr->alt_pkey_index;
  1599. context->alt_path.ackto = attr->alt_timeout << 3;
  1600. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  1601. }
  1602. pd = get_pd(qp);
  1603. get_cqs(qp, &send_cq, &recv_cq);
  1604. context->pd = cpu_to_be32(pd->pdn);
  1605. context->cqn_send = cpu_to_be32(send_cq->mcq.cqn);
  1606. context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn);
  1607. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  1608. /* Set "fast registration enabled" for all kernel QPs */
  1609. if (!qp->ibqp.uobject)
  1610. context->params1 |= cpu_to_be32(1 << 11);
  1611. if (attr_mask & IB_QP_RNR_RETRY) {
  1612. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  1613. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  1614. }
  1615. if (attr_mask & IB_QP_RETRY_CNT) {
  1616. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  1617. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  1618. }
  1619. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  1620. if (attr->max_rd_atomic)
  1621. context->params1 |=
  1622. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  1623. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  1624. }
  1625. if (attr_mask & IB_QP_SQ_PSN)
  1626. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  1627. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  1628. if (attr->max_dest_rd_atomic)
  1629. context->params2 |=
  1630. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  1631. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  1632. }
  1633. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  1634. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  1635. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  1636. }
  1637. if (ibqp->srq)
  1638. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  1639. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  1640. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  1641. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  1642. }
  1643. if (attr_mask & IB_QP_RQ_PSN)
  1644. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  1645. /* proxy and tunnel qp qkeys will be changed in modify-qp wrappers */
  1646. if (attr_mask & IB_QP_QKEY) {
  1647. if (qp->mlx4_ib_qp_type &
  1648. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))
  1649. context->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  1650. else {
  1651. if (mlx4_is_mfunc(dev->dev) &&
  1652. !(qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) &&
  1653. (attr->qkey & MLX4_RESERVED_QKEY_MASK) ==
  1654. MLX4_RESERVED_QKEY_BASE) {
  1655. pr_err("Cannot use reserved QKEY"
  1656. " 0x%x (range 0xffff0000..0xffffffff"
  1657. " is reserved)\n", attr->qkey);
  1658. err = -EINVAL;
  1659. goto out;
  1660. }
  1661. context->qkey = cpu_to_be32(attr->qkey);
  1662. }
  1663. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  1664. }
  1665. if (ibqp->srq)
  1666. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  1667. if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1668. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  1669. if (cur_state == IB_QPS_INIT &&
  1670. new_state == IB_QPS_RTR &&
  1671. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  1672. ibqp->qp_type == IB_QPT_UD ||
  1673. ibqp->qp_type == IB_QPT_RAW_PACKET)) {
  1674. context->pri_path.sched_queue = (qp->port - 1) << 6;
  1675. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  1676. qp->mlx4_ib_qp_type &
  1677. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) {
  1678. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  1679. if (qp->mlx4_ib_qp_type != MLX4_IB_QPT_SMI)
  1680. context->pri_path.fl = 0x80;
  1681. } else {
  1682. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1683. context->pri_path.fl = 0x80;
  1684. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  1685. }
  1686. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  1687. IB_LINK_LAYER_ETHERNET) {
  1688. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI ||
  1689. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI)
  1690. context->pri_path.feup = 1 << 7; /* don't fsm */
  1691. /* handle smac_index */
  1692. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_UD ||
  1693. qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI ||
  1694. qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI) {
  1695. err = handle_eth_ud_smac_index(dev, qp, context);
  1696. if (err) {
  1697. err = -EINVAL;
  1698. goto out;
  1699. }
  1700. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  1701. dev->qp1_proxy[qp->port - 1] = qp;
  1702. }
  1703. }
  1704. }
  1705. if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
  1706. context->pri_path.ackto = (context->pri_path.ackto & 0xf8) |
  1707. MLX4_IB_LINK_TYPE_ETH;
  1708. if (dev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  1709. /* set QP to receive both tunneled & non-tunneled packets */
  1710. if (!(context->flags & cpu_to_be32(1 << MLX4_RSS_QPC_FLAG_OFFSET)))
  1711. context->srqn = cpu_to_be32(7 << 28);
  1712. }
  1713. }
  1714. if (ibqp->qp_type == IB_QPT_UD && (new_state == IB_QPS_RTR)) {
  1715. int is_eth = rdma_port_get_link_layer(
  1716. &dev->ib_dev, qp->port) ==
  1717. IB_LINK_LAYER_ETHERNET;
  1718. if (is_eth) {
  1719. context->pri_path.ackto = MLX4_IB_LINK_TYPE_ETH;
  1720. optpar |= MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH;
  1721. }
  1722. }
  1723. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  1724. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  1725. sqd_event = 1;
  1726. else
  1727. sqd_event = 0;
  1728. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1729. context->rlkey_roce_mode |= (1 << 4);
  1730. /*
  1731. * Before passing a kernel QP to the HW, make sure that the
  1732. * ownership bits of the send queue are set and the SQ
  1733. * headroom is stamped so that the hardware doesn't start
  1734. * processing stale work requests.
  1735. */
  1736. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1737. struct mlx4_wqe_ctrl_seg *ctrl;
  1738. int i;
  1739. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  1740. ctrl = get_send_wqe(qp, i);
  1741. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  1742. if (qp->sq_max_wqes_per_wr == 1)
  1743. ctrl->qpn_vlan.fence_size =
  1744. 1 << (qp->sq.wqe_shift - 4);
  1745. stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
  1746. }
  1747. }
  1748. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  1749. to_mlx4_state(new_state), context, optpar,
  1750. sqd_event, &qp->mqp);
  1751. if (err)
  1752. goto out;
  1753. qp->state = new_state;
  1754. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1755. qp->atomic_rd_en = attr->qp_access_flags;
  1756. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1757. qp->resp_depth = attr->max_dest_rd_atomic;
  1758. if (attr_mask & IB_QP_PORT) {
  1759. qp->port = attr->port_num;
  1760. update_mcg_macs(dev, qp);
  1761. }
  1762. if (attr_mask & IB_QP_ALT_PATH)
  1763. qp->alt_port = attr->alt_port_num;
  1764. if (is_sqp(dev, qp))
  1765. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  1766. /*
  1767. * If we moved QP0 to RTR, bring the IB link up; if we moved
  1768. * QP0 to RESET or ERROR, bring the link back down.
  1769. */
  1770. if (is_qp0(dev, qp)) {
  1771. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  1772. if (mlx4_INIT_PORT(dev->dev, qp->port))
  1773. pr_warn("INIT_PORT failed for port %d\n",
  1774. qp->port);
  1775. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  1776. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  1777. mlx4_CLOSE_PORT(dev->dev, qp->port);
  1778. }
  1779. /*
  1780. * If we moved a kernel QP to RESET, clean up all old CQ
  1781. * entries and reinitialize the QP.
  1782. */
  1783. if (new_state == IB_QPS_RESET) {
  1784. if (!ibqp->uobject) {
  1785. mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  1786. ibqp->srq ? to_msrq(ibqp->srq) : NULL);
  1787. if (send_cq != recv_cq)
  1788. mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  1789. qp->rq.head = 0;
  1790. qp->rq.tail = 0;
  1791. qp->sq.head = 0;
  1792. qp->sq.tail = 0;
  1793. qp->sq_next_wqe = 0;
  1794. if (qp->rq.wqe_cnt)
  1795. *qp->db.db = 0;
  1796. if (qp->flags & MLX4_IB_QP_NETIF)
  1797. mlx4_ib_steer_qp_reg(dev, qp, 0);
  1798. }
  1799. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  1800. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  1801. qp->pri.smac = 0;
  1802. qp->pri.smac_port = 0;
  1803. }
  1804. if (qp->alt.smac) {
  1805. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  1806. qp->alt.smac = 0;
  1807. }
  1808. if (qp->pri.vid < 0x1000) {
  1809. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  1810. qp->pri.vid = 0xFFFF;
  1811. qp->pri.candidate_vid = 0xFFFF;
  1812. qp->pri.update_vid = 0;
  1813. }
  1814. if (qp->alt.vid < 0x1000) {
  1815. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  1816. qp->alt.vid = 0xFFFF;
  1817. qp->alt.candidate_vid = 0xFFFF;
  1818. qp->alt.update_vid = 0;
  1819. }
  1820. }
  1821. out:
  1822. if (err && qp->counter_index)
  1823. mlx4_ib_free_qp_counter(dev, qp);
  1824. if (err && steer_qp)
  1825. mlx4_ib_steer_qp_reg(dev, qp, 0);
  1826. kfree(context);
  1827. if (qp->pri.candidate_smac ||
  1828. (!qp->pri.candidate_smac && qp->pri.candidate_smac_port)) {
  1829. if (err) {
  1830. mlx4_unregister_mac(dev->dev, qp->pri.candidate_smac_port, qp->pri.candidate_smac);
  1831. } else {
  1832. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port))
  1833. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  1834. qp->pri.smac = qp->pri.candidate_smac;
  1835. qp->pri.smac_index = qp->pri.candidate_smac_index;
  1836. qp->pri.smac_port = qp->pri.candidate_smac_port;
  1837. }
  1838. qp->pri.candidate_smac = 0;
  1839. qp->pri.candidate_smac_index = 0;
  1840. qp->pri.candidate_smac_port = 0;
  1841. }
  1842. if (qp->alt.candidate_smac) {
  1843. if (err) {
  1844. mlx4_unregister_mac(dev->dev, qp->alt.candidate_smac_port, qp->alt.candidate_smac);
  1845. } else {
  1846. if (qp->alt.smac)
  1847. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  1848. qp->alt.smac = qp->alt.candidate_smac;
  1849. qp->alt.smac_index = qp->alt.candidate_smac_index;
  1850. qp->alt.smac_port = qp->alt.candidate_smac_port;
  1851. }
  1852. qp->alt.candidate_smac = 0;
  1853. qp->alt.candidate_smac_index = 0;
  1854. qp->alt.candidate_smac_port = 0;
  1855. }
  1856. if (qp->pri.update_vid) {
  1857. if (err) {
  1858. if (qp->pri.candidate_vid < 0x1000)
  1859. mlx4_unregister_vlan(dev->dev, qp->pri.candidate_vlan_port,
  1860. qp->pri.candidate_vid);
  1861. } else {
  1862. if (qp->pri.vid < 0x1000)
  1863. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port,
  1864. qp->pri.vid);
  1865. qp->pri.vid = qp->pri.candidate_vid;
  1866. qp->pri.vlan_port = qp->pri.candidate_vlan_port;
  1867. qp->pri.vlan_index = qp->pri.candidate_vlan_index;
  1868. }
  1869. qp->pri.candidate_vid = 0xFFFF;
  1870. qp->pri.update_vid = 0;
  1871. }
  1872. if (qp->alt.update_vid) {
  1873. if (err) {
  1874. if (qp->alt.candidate_vid < 0x1000)
  1875. mlx4_unregister_vlan(dev->dev, qp->alt.candidate_vlan_port,
  1876. qp->alt.candidate_vid);
  1877. } else {
  1878. if (qp->alt.vid < 0x1000)
  1879. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port,
  1880. qp->alt.vid);
  1881. qp->alt.vid = qp->alt.candidate_vid;
  1882. qp->alt.vlan_port = qp->alt.candidate_vlan_port;
  1883. qp->alt.vlan_index = qp->alt.candidate_vlan_index;
  1884. }
  1885. qp->alt.candidate_vid = 0xFFFF;
  1886. qp->alt.update_vid = 0;
  1887. }
  1888. return err;
  1889. }
  1890. static int _mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1891. int attr_mask, struct ib_udata *udata)
  1892. {
  1893. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1894. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1895. enum ib_qp_state cur_state, new_state;
  1896. int err = -EINVAL;
  1897. int ll;
  1898. mutex_lock(&qp->mutex);
  1899. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  1900. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1901. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1902. ll = IB_LINK_LAYER_UNSPECIFIED;
  1903. } else {
  1904. int port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1905. ll = rdma_port_get_link_layer(&dev->ib_dev, port);
  1906. }
  1907. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  1908. attr_mask, ll)) {
  1909. pr_debug("qpn 0x%x: invalid attribute mask specified "
  1910. "for transition %d to %d. qp_type %d,"
  1911. " attr_mask 0x%x\n",
  1912. ibqp->qp_num, cur_state, new_state,
  1913. ibqp->qp_type, attr_mask);
  1914. goto out;
  1915. }
  1916. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT)) {
  1917. if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
  1918. if ((ibqp->qp_type == IB_QPT_RC) ||
  1919. (ibqp->qp_type == IB_QPT_UD) ||
  1920. (ibqp->qp_type == IB_QPT_UC) ||
  1921. (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
  1922. (ibqp->qp_type == IB_QPT_XRC_INI)) {
  1923. attr->port_num = mlx4_ib_bond_next_port(dev);
  1924. }
  1925. } else {
  1926. /* no sense in changing port_num
  1927. * when ports are bonded */
  1928. attr_mask &= ~IB_QP_PORT;
  1929. }
  1930. }
  1931. if ((attr_mask & IB_QP_PORT) &&
  1932. (attr->port_num == 0 || attr->port_num > dev->num_ports)) {
  1933. pr_debug("qpn 0x%x: invalid port number (%d) specified "
  1934. "for transition %d to %d. qp_type %d\n",
  1935. ibqp->qp_num, attr->port_num, cur_state,
  1936. new_state, ibqp->qp_type);
  1937. goto out;
  1938. }
  1939. if ((attr_mask & IB_QP_PORT) && (ibqp->qp_type == IB_QPT_RAW_PACKET) &&
  1940. (rdma_port_get_link_layer(&dev->ib_dev, attr->port_num) !=
  1941. IB_LINK_LAYER_ETHERNET))
  1942. goto out;
  1943. if (attr_mask & IB_QP_PKEY_INDEX) {
  1944. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1945. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p]) {
  1946. pr_debug("qpn 0x%x: invalid pkey index (%d) specified "
  1947. "for transition %d to %d. qp_type %d\n",
  1948. ibqp->qp_num, attr->pkey_index, cur_state,
  1949. new_state, ibqp->qp_type);
  1950. goto out;
  1951. }
  1952. }
  1953. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  1954. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  1955. pr_debug("qpn 0x%x: max_rd_atomic (%d) too large. "
  1956. "Transition %d to %d. qp_type %d\n",
  1957. ibqp->qp_num, attr->max_rd_atomic, cur_state,
  1958. new_state, ibqp->qp_type);
  1959. goto out;
  1960. }
  1961. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  1962. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  1963. pr_debug("qpn 0x%x: max_dest_rd_atomic (%d) too large. "
  1964. "Transition %d to %d. qp_type %d\n",
  1965. ibqp->qp_num, attr->max_dest_rd_atomic, cur_state,
  1966. new_state, ibqp->qp_type);
  1967. goto out;
  1968. }
  1969. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1970. err = 0;
  1971. goto out;
  1972. }
  1973. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  1974. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT))
  1975. attr->port_num = 1;
  1976. out:
  1977. mutex_unlock(&qp->mutex);
  1978. return err;
  1979. }
  1980. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1981. int attr_mask, struct ib_udata *udata)
  1982. {
  1983. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  1984. int ret;
  1985. ret = _mlx4_ib_modify_qp(ibqp, attr, attr_mask, udata);
  1986. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  1987. struct mlx4_ib_sqp *sqp = to_msqp(mqp);
  1988. int err = 0;
  1989. if (sqp->roce_v2_gsi)
  1990. err = ib_modify_qp(sqp->roce_v2_gsi, attr, attr_mask);
  1991. if (err)
  1992. pr_err("Failed to modify GSI QP for RoCEv2 (%d)\n",
  1993. err);
  1994. }
  1995. return ret;
  1996. }
  1997. static int vf_get_qp0_qkey(struct mlx4_dev *dev, int qpn, u32 *qkey)
  1998. {
  1999. int i;
  2000. for (i = 0; i < dev->caps.num_ports; i++) {
  2001. if (qpn == dev->caps.qp0_proxy[i] ||
  2002. qpn == dev->caps.qp0_tunnel[i]) {
  2003. *qkey = dev->caps.qp0_qkey[i];
  2004. return 0;
  2005. }
  2006. }
  2007. return -EINVAL;
  2008. }
  2009. static int build_sriov_qp0_header(struct mlx4_ib_sqp *sqp,
  2010. struct ib_ud_wr *wr,
  2011. void *wqe, unsigned *mlx_seg_len)
  2012. {
  2013. struct mlx4_ib_dev *mdev = to_mdev(sqp->qp.ibqp.device);
  2014. struct ib_device *ib_dev = &mdev->ib_dev;
  2015. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2016. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2017. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2018. u16 pkey;
  2019. u32 qkey;
  2020. int send_size;
  2021. int header_size;
  2022. int spc;
  2023. int i;
  2024. if (wr->wr.opcode != IB_WR_SEND)
  2025. return -EINVAL;
  2026. send_size = 0;
  2027. for (i = 0; i < wr->wr.num_sge; ++i)
  2028. send_size += wr->wr.sg_list[i].length;
  2029. /* for proxy-qp0 sends, need to add in size of tunnel header */
  2030. /* for tunnel-qp0 sends, tunnel header is already in s/g list */
  2031. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER)
  2032. send_size += sizeof (struct mlx4_ib_tunnel_header);
  2033. ib_ud_header_init(send_size, 1, 0, 0, 0, 0, 0, 0, &sqp->ud_header);
  2034. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) {
  2035. sqp->ud_header.lrh.service_level =
  2036. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2037. sqp->ud_header.lrh.destination_lid =
  2038. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2039. sqp->ud_header.lrh.source_lid =
  2040. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2041. }
  2042. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2043. /* force loopback */
  2044. mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR);
  2045. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2046. sqp->ud_header.lrh.virtual_lane = 0;
  2047. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2048. ib_get_cached_pkey(ib_dev, sqp->qp.port, 0, &pkey);
  2049. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2050. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER)
  2051. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2052. else
  2053. sqp->ud_header.bth.destination_qpn =
  2054. cpu_to_be32(mdev->dev->caps.qp0_tunnel[sqp->qp.port - 1]);
  2055. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2056. if (mlx4_is_master(mdev->dev)) {
  2057. if (mlx4_get_parav_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2058. return -EINVAL;
  2059. } else {
  2060. if (vf_get_qp0_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2061. return -EINVAL;
  2062. }
  2063. sqp->ud_header.deth.qkey = cpu_to_be32(qkey);
  2064. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.mqp.qpn);
  2065. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2066. sqp->ud_header.immediate_present = 0;
  2067. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2068. /*
  2069. * Inline data segments may not cross a 64 byte boundary. If
  2070. * our UD header is bigger than the space available up to the
  2071. * next 64 byte boundary in the WQE, use two inline data
  2072. * segments to hold the UD header.
  2073. */
  2074. spc = MLX4_INLINE_ALIGN -
  2075. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2076. if (header_size <= spc) {
  2077. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2078. memcpy(inl + 1, sqp->header_buf, header_size);
  2079. i = 1;
  2080. } else {
  2081. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2082. memcpy(inl + 1, sqp->header_buf, spc);
  2083. inl = (void *) (inl + 1) + spc;
  2084. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2085. /*
  2086. * Need a barrier here to make sure all the data is
  2087. * visible before the byte_count field is set.
  2088. * Otherwise the HCA prefetcher could grab the 64-byte
  2089. * chunk with this inline segment and get a valid (!=
  2090. * 0xffffffff) byte count but stale data, and end up
  2091. * generating a packet with bad headers.
  2092. *
  2093. * The first inline segment's byte_count field doesn't
  2094. * need a barrier, because it comes after a
  2095. * control/MLX segment and therefore is at an offset
  2096. * of 16 mod 64.
  2097. */
  2098. wmb();
  2099. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2100. i = 2;
  2101. }
  2102. *mlx_seg_len =
  2103. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2104. return 0;
  2105. }
  2106. #define MLX4_ROCEV2_QP1_SPORT 0xC000
  2107. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_ud_wr *wr,
  2108. void *wqe, unsigned *mlx_seg_len)
  2109. {
  2110. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  2111. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2112. struct mlx4_wqe_ctrl_seg *ctrl = wqe;
  2113. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2114. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2115. union ib_gid sgid;
  2116. u16 pkey;
  2117. int send_size;
  2118. int header_size;
  2119. int spc;
  2120. int i;
  2121. int err = 0;
  2122. u16 vlan = 0xffff;
  2123. bool is_eth;
  2124. bool is_vlan = false;
  2125. bool is_grh;
  2126. bool is_udp = false;
  2127. int ip_version = 0;
  2128. send_size = 0;
  2129. for (i = 0; i < wr->wr.num_sge; ++i)
  2130. send_size += wr->wr.sg_list[i].length;
  2131. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  2132. is_grh = mlx4_ib_ah_grh_present(ah);
  2133. if (is_eth) {
  2134. struct ib_gid_attr gid_attr;
  2135. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2136. /* When multi-function is enabled, the ib_core gid
  2137. * indexes don't necessarily match the hw ones, so
  2138. * we must use our own cache */
  2139. err = mlx4_get_roce_gid_from_slave(to_mdev(ib_dev)->dev,
  2140. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2141. ah->av.ib.gid_index, &sgid.raw[0]);
  2142. if (err)
  2143. return err;
  2144. } else {
  2145. err = ib_get_cached_gid(ib_dev,
  2146. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2147. ah->av.ib.gid_index, &sgid,
  2148. &gid_attr);
  2149. if (!err) {
  2150. if (gid_attr.ndev)
  2151. dev_put(gid_attr.ndev);
  2152. if (!memcmp(&sgid, &zgid, sizeof(sgid)))
  2153. err = -ENOENT;
  2154. }
  2155. if (!err) {
  2156. is_udp = gid_attr.gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
  2157. if (is_udp) {
  2158. if (ipv6_addr_v4mapped((struct in6_addr *)&sgid))
  2159. ip_version = 4;
  2160. else
  2161. ip_version = 6;
  2162. is_grh = false;
  2163. }
  2164. } else {
  2165. return err;
  2166. }
  2167. }
  2168. if (ah->av.eth.vlan != cpu_to_be16(0xffff)) {
  2169. vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff;
  2170. is_vlan = 1;
  2171. }
  2172. }
  2173. err = ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh,
  2174. ip_version, is_udp, 0, &sqp->ud_header);
  2175. if (err)
  2176. return err;
  2177. if (!is_eth) {
  2178. sqp->ud_header.lrh.service_level =
  2179. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2180. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  2181. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2182. }
  2183. if (is_grh || (ip_version == 6)) {
  2184. sqp->ud_header.grh.traffic_class =
  2185. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2186. sqp->ud_header.grh.flow_label =
  2187. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  2188. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  2189. if (is_eth)
  2190. memcpy(sqp->ud_header.grh.source_gid.raw, sgid.raw, 16);
  2191. else {
  2192. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2193. /* When multi-function is enabled, the ib_core gid
  2194. * indexes don't necessarily match the hw ones, so
  2195. * we must use our own cache */
  2196. sqp->ud_header.grh.source_gid.global.subnet_prefix =
  2197. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  2198. subnet_prefix;
  2199. sqp->ud_header.grh.source_gid.global.interface_id =
  2200. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  2201. guid_cache[ah->av.ib.gid_index];
  2202. } else
  2203. ib_get_cached_gid(ib_dev,
  2204. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2205. ah->av.ib.gid_index,
  2206. &sqp->ud_header.grh.source_gid, NULL);
  2207. }
  2208. memcpy(sqp->ud_header.grh.destination_gid.raw,
  2209. ah->av.ib.dgid, 16);
  2210. }
  2211. if (ip_version == 4) {
  2212. sqp->ud_header.ip4.tos =
  2213. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2214. sqp->ud_header.ip4.id = 0;
  2215. sqp->ud_header.ip4.frag_off = htons(IP_DF);
  2216. sqp->ud_header.ip4.ttl = ah->av.eth.hop_limit;
  2217. memcpy(&sqp->ud_header.ip4.saddr,
  2218. sgid.raw + 12, 4);
  2219. memcpy(&sqp->ud_header.ip4.daddr, ah->av.ib.dgid + 12, 4);
  2220. sqp->ud_header.ip4.check = ib_ud_ip4_csum(&sqp->ud_header);
  2221. }
  2222. if (is_udp) {
  2223. sqp->ud_header.udp.dport = htons(ROCE_V2_UDP_DPORT);
  2224. sqp->ud_header.udp.sport = htons(MLX4_ROCEV2_QP1_SPORT);
  2225. sqp->ud_header.udp.csum = 0;
  2226. }
  2227. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2228. if (!is_eth) {
  2229. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  2230. (sqp->ud_header.lrh.destination_lid ==
  2231. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  2232. (sqp->ud_header.lrh.service_level << 8));
  2233. if (ah->av.ib.port_pd & cpu_to_be32(0x80000000))
  2234. mlx->flags |= cpu_to_be32(0x1); /* force loopback */
  2235. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2236. }
  2237. switch (wr->wr.opcode) {
  2238. case IB_WR_SEND:
  2239. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2240. sqp->ud_header.immediate_present = 0;
  2241. break;
  2242. case IB_WR_SEND_WITH_IMM:
  2243. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  2244. sqp->ud_header.immediate_present = 1;
  2245. sqp->ud_header.immediate_data = wr->wr.ex.imm_data;
  2246. break;
  2247. default:
  2248. return -EINVAL;
  2249. }
  2250. if (is_eth) {
  2251. struct in6_addr in6;
  2252. u16 ether_type;
  2253. u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
  2254. ether_type = (!is_udp) ? MLX4_IB_IBOE_ETHERTYPE :
  2255. (ip_version == 4 ? ETH_P_IP : ETH_P_IPV6);
  2256. mlx->sched_prio = cpu_to_be16(pcp);
  2257. ether_addr_copy(sqp->ud_header.eth.smac_h, ah->av.eth.s_mac);
  2258. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  2259. memcpy(&ctrl->srcrb_flags16[0], ah->av.eth.mac, 2);
  2260. memcpy(&ctrl->imm, ah->av.eth.mac + 2, 4);
  2261. memcpy(&in6, sgid.raw, sizeof(in6));
  2262. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  2263. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  2264. if (!is_vlan) {
  2265. sqp->ud_header.eth.type = cpu_to_be16(ether_type);
  2266. } else {
  2267. sqp->ud_header.vlan.type = cpu_to_be16(ether_type);
  2268. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  2269. }
  2270. } else {
  2271. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  2272. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  2273. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  2274. }
  2275. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2276. if (!sqp->qp.ibqp.qp_num)
  2277. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  2278. else
  2279. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->pkey_index, &pkey);
  2280. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2281. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2282. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2283. sqp->ud_header.deth.qkey = cpu_to_be32(wr->remote_qkey & 0x80000000 ?
  2284. sqp->qkey : wr->remote_qkey);
  2285. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  2286. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2287. if (0) {
  2288. pr_err("built UD header of size %d:\n", header_size);
  2289. for (i = 0; i < header_size / 4; ++i) {
  2290. if (i % 8 == 0)
  2291. pr_err(" [%02x] ", i * 4);
  2292. pr_cont(" %08x",
  2293. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  2294. if ((i + 1) % 8 == 0)
  2295. pr_cont("\n");
  2296. }
  2297. pr_err("\n");
  2298. }
  2299. /*
  2300. * Inline data segments may not cross a 64 byte boundary. If
  2301. * our UD header is bigger than the space available up to the
  2302. * next 64 byte boundary in the WQE, use two inline data
  2303. * segments to hold the UD header.
  2304. */
  2305. spc = MLX4_INLINE_ALIGN -
  2306. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2307. if (header_size <= spc) {
  2308. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2309. memcpy(inl + 1, sqp->header_buf, header_size);
  2310. i = 1;
  2311. } else {
  2312. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2313. memcpy(inl + 1, sqp->header_buf, spc);
  2314. inl = (void *) (inl + 1) + spc;
  2315. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2316. /*
  2317. * Need a barrier here to make sure all the data is
  2318. * visible before the byte_count field is set.
  2319. * Otherwise the HCA prefetcher could grab the 64-byte
  2320. * chunk with this inline segment and get a valid (!=
  2321. * 0xffffffff) byte count but stale data, and end up
  2322. * generating a packet with bad headers.
  2323. *
  2324. * The first inline segment's byte_count field doesn't
  2325. * need a barrier, because it comes after a
  2326. * control/MLX segment and therefore is at an offset
  2327. * of 16 mod 64.
  2328. */
  2329. wmb();
  2330. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2331. i = 2;
  2332. }
  2333. *mlx_seg_len =
  2334. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2335. return 0;
  2336. }
  2337. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  2338. {
  2339. unsigned cur;
  2340. struct mlx4_ib_cq *cq;
  2341. cur = wq->head - wq->tail;
  2342. if (likely(cur + nreq < wq->max_post))
  2343. return 0;
  2344. cq = to_mcq(ib_cq);
  2345. spin_lock(&cq->lock);
  2346. cur = wq->head - wq->tail;
  2347. spin_unlock(&cq->lock);
  2348. return cur + nreq >= wq->max_post;
  2349. }
  2350. static __be32 convert_access(int acc)
  2351. {
  2352. return (acc & IB_ACCESS_REMOTE_ATOMIC ?
  2353. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC) : 0) |
  2354. (acc & IB_ACCESS_REMOTE_WRITE ?
  2355. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) |
  2356. (acc & IB_ACCESS_REMOTE_READ ?
  2357. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ) : 0) |
  2358. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  2359. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  2360. }
  2361. static void set_reg_seg(struct mlx4_wqe_fmr_seg *fseg,
  2362. struct ib_reg_wr *wr)
  2363. {
  2364. struct mlx4_ib_mr *mr = to_mmr(wr->mr);
  2365. fseg->flags = convert_access(wr->access);
  2366. fseg->mem_key = cpu_to_be32(wr->key);
  2367. fseg->buf_list = cpu_to_be64(mr->page_map);
  2368. fseg->start_addr = cpu_to_be64(mr->ibmr.iova);
  2369. fseg->reg_len = cpu_to_be64(mr->ibmr.length);
  2370. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  2371. fseg->page_size = cpu_to_be32(ilog2(mr->ibmr.page_size));
  2372. fseg->reserved[0] = 0;
  2373. fseg->reserved[1] = 0;
  2374. }
  2375. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  2376. {
  2377. memset(iseg, 0, sizeof(*iseg));
  2378. iseg->mem_key = cpu_to_be32(rkey);
  2379. }
  2380. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  2381. u64 remote_addr, u32 rkey)
  2382. {
  2383. rseg->raddr = cpu_to_be64(remote_addr);
  2384. rseg->rkey = cpu_to_be32(rkey);
  2385. rseg->reserved = 0;
  2386. }
  2387. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg,
  2388. struct ib_atomic_wr *wr)
  2389. {
  2390. if (wr->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  2391. aseg->swap_add = cpu_to_be64(wr->swap);
  2392. aseg->compare = cpu_to_be64(wr->compare_add);
  2393. } else if (wr->wr.opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  2394. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2395. aseg->compare = cpu_to_be64(wr->compare_add_mask);
  2396. } else {
  2397. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2398. aseg->compare = 0;
  2399. }
  2400. }
  2401. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  2402. struct ib_atomic_wr *wr)
  2403. {
  2404. aseg->swap_add = cpu_to_be64(wr->swap);
  2405. aseg->swap_add_mask = cpu_to_be64(wr->swap_mask);
  2406. aseg->compare = cpu_to_be64(wr->compare_add);
  2407. aseg->compare_mask = cpu_to_be64(wr->compare_add_mask);
  2408. }
  2409. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  2410. struct ib_ud_wr *wr)
  2411. {
  2412. memcpy(dseg->av, &to_mah(wr->ah)->av, sizeof (struct mlx4_av));
  2413. dseg->dqpn = cpu_to_be32(wr->remote_qpn);
  2414. dseg->qkey = cpu_to_be32(wr->remote_qkey);
  2415. dseg->vlan = to_mah(wr->ah)->av.eth.vlan;
  2416. memcpy(dseg->mac, to_mah(wr->ah)->av.eth.mac, 6);
  2417. }
  2418. static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev,
  2419. struct mlx4_wqe_datagram_seg *dseg,
  2420. struct ib_ud_wr *wr,
  2421. enum mlx4_ib_qp_type qpt)
  2422. {
  2423. union mlx4_ext_av *av = &to_mah(wr->ah)->av;
  2424. struct mlx4_av sqp_av = {0};
  2425. int port = *((u8 *) &av->ib.port_pd) & 0x3;
  2426. /* force loopback */
  2427. sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000);
  2428. sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */
  2429. sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel &
  2430. cpu_to_be32(0xf0000000);
  2431. memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av));
  2432. if (qpt == MLX4_IB_QPT_PROXY_GSI)
  2433. dseg->dqpn = cpu_to_be32(dev->dev->caps.qp1_tunnel[port - 1]);
  2434. else
  2435. dseg->dqpn = cpu_to_be32(dev->dev->caps.qp0_tunnel[port - 1]);
  2436. /* Use QKEY from the QP context, which is set by master */
  2437. dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  2438. }
  2439. static void build_tunnel_header(struct ib_ud_wr *wr, void *wqe, unsigned *mlx_seg_len)
  2440. {
  2441. struct mlx4_wqe_inline_seg *inl = wqe;
  2442. struct mlx4_ib_tunnel_header hdr;
  2443. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2444. int spc;
  2445. int i;
  2446. memcpy(&hdr.av, &ah->av, sizeof hdr.av);
  2447. hdr.remote_qpn = cpu_to_be32(wr->remote_qpn);
  2448. hdr.pkey_index = cpu_to_be16(wr->pkey_index);
  2449. hdr.qkey = cpu_to_be32(wr->remote_qkey);
  2450. memcpy(hdr.mac, ah->av.eth.mac, 6);
  2451. hdr.vlan = ah->av.eth.vlan;
  2452. spc = MLX4_INLINE_ALIGN -
  2453. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2454. if (sizeof (hdr) <= spc) {
  2455. memcpy(inl + 1, &hdr, sizeof (hdr));
  2456. wmb();
  2457. inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr));
  2458. i = 1;
  2459. } else {
  2460. memcpy(inl + 1, &hdr, spc);
  2461. wmb();
  2462. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2463. inl = (void *) (inl + 1) + spc;
  2464. memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc);
  2465. wmb();
  2466. inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc));
  2467. i = 2;
  2468. }
  2469. *mlx_seg_len =
  2470. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16);
  2471. }
  2472. static void set_mlx_icrc_seg(void *dseg)
  2473. {
  2474. u32 *t = dseg;
  2475. struct mlx4_wqe_inline_seg *iseg = dseg;
  2476. t[1] = 0;
  2477. /*
  2478. * Need a barrier here before writing the byte_count field to
  2479. * make sure that all the data is visible before the
  2480. * byte_count field is set. Otherwise, if the segment begins
  2481. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2482. * chunk and get a valid (!= * 0xffffffff) byte count but
  2483. * stale data, and end up sending the wrong data.
  2484. */
  2485. wmb();
  2486. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  2487. }
  2488. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2489. {
  2490. dseg->lkey = cpu_to_be32(sg->lkey);
  2491. dseg->addr = cpu_to_be64(sg->addr);
  2492. /*
  2493. * Need a barrier here before writing the byte_count field to
  2494. * make sure that all the data is visible before the
  2495. * byte_count field is set. Otherwise, if the segment begins
  2496. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2497. * chunk and get a valid (!= * 0xffffffff) byte count but
  2498. * stale data, and end up sending the wrong data.
  2499. */
  2500. wmb();
  2501. dseg->byte_count = cpu_to_be32(sg->length);
  2502. }
  2503. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2504. {
  2505. dseg->byte_count = cpu_to_be32(sg->length);
  2506. dseg->lkey = cpu_to_be32(sg->lkey);
  2507. dseg->addr = cpu_to_be64(sg->addr);
  2508. }
  2509. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, struct ib_ud_wr *wr,
  2510. struct mlx4_ib_qp *qp, unsigned *lso_seg_len,
  2511. __be32 *lso_hdr_sz, __be32 *blh)
  2512. {
  2513. unsigned halign = ALIGN(sizeof *wqe + wr->hlen, 16);
  2514. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  2515. *blh = cpu_to_be32(1 << 6);
  2516. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  2517. wr->wr.num_sge > qp->sq.max_gs - (halign >> 4)))
  2518. return -EINVAL;
  2519. memcpy(wqe->header, wr->header, wr->hlen);
  2520. *lso_hdr_sz = cpu_to_be32(wr->mss << 16 | wr->hlen);
  2521. *lso_seg_len = halign;
  2522. return 0;
  2523. }
  2524. static __be32 send_ieth(struct ib_send_wr *wr)
  2525. {
  2526. switch (wr->opcode) {
  2527. case IB_WR_SEND_WITH_IMM:
  2528. case IB_WR_RDMA_WRITE_WITH_IMM:
  2529. return wr->ex.imm_data;
  2530. case IB_WR_SEND_WITH_INV:
  2531. return cpu_to_be32(wr->ex.invalidate_rkey);
  2532. default:
  2533. return 0;
  2534. }
  2535. }
  2536. static void add_zero_len_inline(void *wqe)
  2537. {
  2538. struct mlx4_wqe_inline_seg *inl = wqe;
  2539. memset(wqe, 0, 16);
  2540. inl->byte_count = cpu_to_be32(1 << 31);
  2541. }
  2542. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  2543. struct ib_send_wr **bad_wr)
  2544. {
  2545. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2546. void *wqe;
  2547. struct mlx4_wqe_ctrl_seg *ctrl;
  2548. struct mlx4_wqe_data_seg *dseg;
  2549. unsigned long flags;
  2550. int nreq;
  2551. int err = 0;
  2552. unsigned ind;
  2553. int uninitialized_var(stamp);
  2554. int uninitialized_var(size);
  2555. unsigned uninitialized_var(seglen);
  2556. __be32 dummy;
  2557. __be32 *lso_wqe;
  2558. __be32 uninitialized_var(lso_hdr_sz);
  2559. __be32 blh;
  2560. int i;
  2561. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  2562. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  2563. struct mlx4_ib_sqp *sqp = to_msqp(qp);
  2564. if (sqp->roce_v2_gsi) {
  2565. struct mlx4_ib_ah *ah = to_mah(ud_wr(wr)->ah);
  2566. struct ib_gid_attr gid_attr;
  2567. union ib_gid gid;
  2568. if (!ib_get_cached_gid(ibqp->device,
  2569. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2570. ah->av.ib.gid_index, &gid,
  2571. &gid_attr)) {
  2572. if (gid_attr.ndev)
  2573. dev_put(gid_attr.ndev);
  2574. qp = (gid_attr.gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
  2575. to_mqp(sqp->roce_v2_gsi) : qp;
  2576. } else {
  2577. pr_err("Failed to get gid at index %d. RoCEv2 will not work properly\n",
  2578. ah->av.ib.gid_index);
  2579. }
  2580. }
  2581. }
  2582. spin_lock_irqsave(&qp->sq.lock, flags);
  2583. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  2584. err = -EIO;
  2585. *bad_wr = wr;
  2586. nreq = 0;
  2587. goto out;
  2588. }
  2589. ind = qp->sq_next_wqe;
  2590. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  2591. lso_wqe = &dummy;
  2592. blh = 0;
  2593. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  2594. err = -ENOMEM;
  2595. *bad_wr = wr;
  2596. goto out;
  2597. }
  2598. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  2599. err = -EINVAL;
  2600. *bad_wr = wr;
  2601. goto out;
  2602. }
  2603. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  2604. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  2605. ctrl->srcrb_flags =
  2606. (wr->send_flags & IB_SEND_SIGNALED ?
  2607. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  2608. (wr->send_flags & IB_SEND_SOLICITED ?
  2609. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  2610. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  2611. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  2612. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  2613. qp->sq_signal_bits;
  2614. ctrl->imm = send_ieth(wr);
  2615. wqe += sizeof *ctrl;
  2616. size = sizeof *ctrl / 16;
  2617. switch (qp->mlx4_ib_qp_type) {
  2618. case MLX4_IB_QPT_RC:
  2619. case MLX4_IB_QPT_UC:
  2620. switch (wr->opcode) {
  2621. case IB_WR_ATOMIC_CMP_AND_SWP:
  2622. case IB_WR_ATOMIC_FETCH_AND_ADD:
  2623. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  2624. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  2625. atomic_wr(wr)->rkey);
  2626. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2627. set_atomic_seg(wqe, atomic_wr(wr));
  2628. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  2629. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  2630. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  2631. break;
  2632. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  2633. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  2634. atomic_wr(wr)->rkey);
  2635. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2636. set_masked_atomic_seg(wqe, atomic_wr(wr));
  2637. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  2638. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  2639. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  2640. break;
  2641. case IB_WR_RDMA_READ:
  2642. case IB_WR_RDMA_WRITE:
  2643. case IB_WR_RDMA_WRITE_WITH_IMM:
  2644. set_raddr_seg(wqe, rdma_wr(wr)->remote_addr,
  2645. rdma_wr(wr)->rkey);
  2646. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2647. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  2648. break;
  2649. case IB_WR_LOCAL_INV:
  2650. ctrl->srcrb_flags |=
  2651. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2652. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  2653. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  2654. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  2655. break;
  2656. case IB_WR_REG_MR:
  2657. ctrl->srcrb_flags |=
  2658. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2659. set_reg_seg(wqe, reg_wr(wr));
  2660. wqe += sizeof(struct mlx4_wqe_fmr_seg);
  2661. size += sizeof(struct mlx4_wqe_fmr_seg) / 16;
  2662. break;
  2663. default:
  2664. /* No extra segments required for sends */
  2665. break;
  2666. }
  2667. break;
  2668. case MLX4_IB_QPT_TUN_SMI_OWNER:
  2669. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  2670. ctrl, &seglen);
  2671. if (unlikely(err)) {
  2672. *bad_wr = wr;
  2673. goto out;
  2674. }
  2675. wqe += seglen;
  2676. size += seglen / 16;
  2677. break;
  2678. case MLX4_IB_QPT_TUN_SMI:
  2679. case MLX4_IB_QPT_TUN_GSI:
  2680. /* this is a UD qp used in MAD responses to slaves. */
  2681. set_datagram_seg(wqe, ud_wr(wr));
  2682. /* set the forced-loopback bit in the data seg av */
  2683. *(__be32 *) wqe |= cpu_to_be32(0x80000000);
  2684. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2685. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2686. break;
  2687. case MLX4_IB_QPT_UD:
  2688. set_datagram_seg(wqe, ud_wr(wr));
  2689. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2690. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2691. if (wr->opcode == IB_WR_LSO) {
  2692. err = build_lso_seg(wqe, ud_wr(wr), qp, &seglen,
  2693. &lso_hdr_sz, &blh);
  2694. if (unlikely(err)) {
  2695. *bad_wr = wr;
  2696. goto out;
  2697. }
  2698. lso_wqe = (__be32 *) wqe;
  2699. wqe += seglen;
  2700. size += seglen / 16;
  2701. }
  2702. break;
  2703. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  2704. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  2705. ctrl, &seglen);
  2706. if (unlikely(err)) {
  2707. *bad_wr = wr;
  2708. goto out;
  2709. }
  2710. wqe += seglen;
  2711. size += seglen / 16;
  2712. /* to start tunnel header on a cache-line boundary */
  2713. add_zero_len_inline(wqe);
  2714. wqe += 16;
  2715. size++;
  2716. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  2717. wqe += seglen;
  2718. size += seglen / 16;
  2719. break;
  2720. case MLX4_IB_QPT_PROXY_SMI:
  2721. case MLX4_IB_QPT_PROXY_GSI:
  2722. /* If we are tunneling special qps, this is a UD qp.
  2723. * In this case we first add a UD segment targeting
  2724. * the tunnel qp, and then add a header with address
  2725. * information */
  2726. set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe,
  2727. ud_wr(wr),
  2728. qp->mlx4_ib_qp_type);
  2729. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2730. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2731. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  2732. wqe += seglen;
  2733. size += seglen / 16;
  2734. break;
  2735. case MLX4_IB_QPT_SMI:
  2736. case MLX4_IB_QPT_GSI:
  2737. err = build_mlx_header(to_msqp(qp), ud_wr(wr), ctrl,
  2738. &seglen);
  2739. if (unlikely(err)) {
  2740. *bad_wr = wr;
  2741. goto out;
  2742. }
  2743. wqe += seglen;
  2744. size += seglen / 16;
  2745. break;
  2746. default:
  2747. break;
  2748. }
  2749. /*
  2750. * Write data segments in reverse order, so as to
  2751. * overwrite cacheline stamp last within each
  2752. * cacheline. This avoids issues with WQE
  2753. * prefetching.
  2754. */
  2755. dseg = wqe;
  2756. dseg += wr->num_sge - 1;
  2757. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  2758. /* Add one more inline data segment for ICRC for MLX sends */
  2759. if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  2760. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI ||
  2761. qp->mlx4_ib_qp_type &
  2762. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  2763. set_mlx_icrc_seg(dseg + 1);
  2764. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  2765. }
  2766. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  2767. set_data_seg(dseg, wr->sg_list + i);
  2768. /*
  2769. * Possibly overwrite stamping in cacheline with LSO
  2770. * segment only after making sure all data segments
  2771. * are written.
  2772. */
  2773. wmb();
  2774. *lso_wqe = lso_hdr_sz;
  2775. ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ?
  2776. MLX4_WQE_CTRL_FENCE : 0) | size;
  2777. /*
  2778. * Make sure descriptor is fully written before
  2779. * setting ownership bit (because HW can start
  2780. * executing as soon as we do).
  2781. */
  2782. wmb();
  2783. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  2784. *bad_wr = wr;
  2785. err = -EINVAL;
  2786. goto out;
  2787. }
  2788. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  2789. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  2790. stamp = ind + qp->sq_spare_wqes;
  2791. ind += DIV_ROUND_UP(size * 16, 1U << qp->sq.wqe_shift);
  2792. /*
  2793. * We can improve latency by not stamping the last
  2794. * send queue WQE until after ringing the doorbell, so
  2795. * only stamp here if there are still more WQEs to post.
  2796. *
  2797. * Same optimization applies to padding with NOP wqe
  2798. * in case of WQE shrinking (used to prevent wrap-around
  2799. * in the middle of WR).
  2800. */
  2801. if (wr->next) {
  2802. stamp_send_wqe(qp, stamp, size * 16);
  2803. ind = pad_wraparound(qp, ind);
  2804. }
  2805. }
  2806. out:
  2807. if (likely(nreq)) {
  2808. qp->sq.head += nreq;
  2809. /*
  2810. * Make sure that descriptors are written before
  2811. * doorbell record.
  2812. */
  2813. wmb();
  2814. writel(qp->doorbell_qpn,
  2815. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  2816. /*
  2817. * Make sure doorbells don't leak out of SQ spinlock
  2818. * and reach the HCA out of order.
  2819. */
  2820. mmiowb();
  2821. stamp_send_wqe(qp, stamp, size * 16);
  2822. ind = pad_wraparound(qp, ind);
  2823. qp->sq_next_wqe = ind;
  2824. }
  2825. spin_unlock_irqrestore(&qp->sq.lock, flags);
  2826. return err;
  2827. }
  2828. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  2829. struct ib_recv_wr **bad_wr)
  2830. {
  2831. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2832. struct mlx4_wqe_data_seg *scat;
  2833. unsigned long flags;
  2834. int err = 0;
  2835. int nreq;
  2836. int ind;
  2837. int max_gs;
  2838. int i;
  2839. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  2840. max_gs = qp->rq.max_gs;
  2841. spin_lock_irqsave(&qp->rq.lock, flags);
  2842. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  2843. err = -EIO;
  2844. *bad_wr = wr;
  2845. nreq = 0;
  2846. goto out;
  2847. }
  2848. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  2849. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  2850. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  2851. err = -ENOMEM;
  2852. *bad_wr = wr;
  2853. goto out;
  2854. }
  2855. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  2856. err = -EINVAL;
  2857. *bad_wr = wr;
  2858. goto out;
  2859. }
  2860. scat = get_recv_wqe(qp, ind);
  2861. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  2862. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  2863. ib_dma_sync_single_for_device(ibqp->device,
  2864. qp->sqp_proxy_rcv[ind].map,
  2865. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  2866. DMA_FROM_DEVICE);
  2867. scat->byte_count =
  2868. cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr));
  2869. /* use dma lkey from upper layer entry */
  2870. scat->lkey = cpu_to_be32(wr->sg_list->lkey);
  2871. scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map);
  2872. scat++;
  2873. max_gs--;
  2874. }
  2875. for (i = 0; i < wr->num_sge; ++i)
  2876. __set_data_seg(scat + i, wr->sg_list + i);
  2877. if (i < max_gs) {
  2878. scat[i].byte_count = 0;
  2879. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  2880. scat[i].addr = 0;
  2881. }
  2882. qp->rq.wrid[ind] = wr->wr_id;
  2883. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  2884. }
  2885. out:
  2886. if (likely(nreq)) {
  2887. qp->rq.head += nreq;
  2888. /*
  2889. * Make sure that descriptors are written before
  2890. * doorbell record.
  2891. */
  2892. wmb();
  2893. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  2894. }
  2895. spin_unlock_irqrestore(&qp->rq.lock, flags);
  2896. return err;
  2897. }
  2898. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  2899. {
  2900. switch (mlx4_state) {
  2901. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  2902. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  2903. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  2904. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  2905. case MLX4_QP_STATE_SQ_DRAINING:
  2906. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  2907. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  2908. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  2909. default: return -1;
  2910. }
  2911. }
  2912. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  2913. {
  2914. switch (mlx4_mig_state) {
  2915. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  2916. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  2917. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  2918. default: return -1;
  2919. }
  2920. }
  2921. static int to_ib_qp_access_flags(int mlx4_flags)
  2922. {
  2923. int ib_flags = 0;
  2924. if (mlx4_flags & MLX4_QP_BIT_RRE)
  2925. ib_flags |= IB_ACCESS_REMOTE_READ;
  2926. if (mlx4_flags & MLX4_QP_BIT_RWE)
  2927. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  2928. if (mlx4_flags & MLX4_QP_BIT_RAE)
  2929. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  2930. return ib_flags;
  2931. }
  2932. static void to_ib_ah_attr(struct mlx4_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
  2933. struct mlx4_qp_path *path)
  2934. {
  2935. struct mlx4_dev *dev = ibdev->dev;
  2936. int is_eth;
  2937. memset(ib_ah_attr, 0, sizeof *ib_ah_attr);
  2938. ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
  2939. if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
  2940. return;
  2941. is_eth = rdma_port_get_link_layer(&ibdev->ib_dev, ib_ah_attr->port_num) ==
  2942. IB_LINK_LAYER_ETHERNET;
  2943. if (is_eth)
  2944. ib_ah_attr->sl = ((path->sched_queue >> 3) & 0x7) |
  2945. ((path->sched_queue & 4) << 1);
  2946. else
  2947. ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
  2948. ib_ah_attr->dlid = be16_to_cpu(path->rlid);
  2949. ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
  2950. ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
  2951. ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
  2952. if (ib_ah_attr->ah_flags) {
  2953. ib_ah_attr->grh.sgid_index = path->mgid_index;
  2954. ib_ah_attr->grh.hop_limit = path->hop_limit;
  2955. ib_ah_attr->grh.traffic_class =
  2956. (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
  2957. ib_ah_attr->grh.flow_label =
  2958. be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
  2959. memcpy(ib_ah_attr->grh.dgid.raw,
  2960. path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
  2961. }
  2962. }
  2963. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  2964. struct ib_qp_init_attr *qp_init_attr)
  2965. {
  2966. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  2967. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2968. struct mlx4_qp_context context;
  2969. int mlx4_state;
  2970. int err = 0;
  2971. mutex_lock(&qp->mutex);
  2972. if (qp->state == IB_QPS_RESET) {
  2973. qp_attr->qp_state = IB_QPS_RESET;
  2974. goto done;
  2975. }
  2976. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  2977. if (err) {
  2978. err = -EINVAL;
  2979. goto out;
  2980. }
  2981. mlx4_state = be32_to_cpu(context.flags) >> 28;
  2982. qp->state = to_ib_qp_state(mlx4_state);
  2983. qp_attr->qp_state = qp->state;
  2984. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  2985. qp_attr->path_mig_state =
  2986. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  2987. qp_attr->qkey = be32_to_cpu(context.qkey);
  2988. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  2989. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  2990. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  2991. qp_attr->qp_access_flags =
  2992. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  2993. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  2994. to_ib_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  2995. to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  2996. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  2997. qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
  2998. }
  2999. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  3000. if (qp_attr->qp_state == IB_QPS_INIT)
  3001. qp_attr->port_num = qp->port;
  3002. else
  3003. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  3004. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  3005. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  3006. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  3007. qp_attr->max_dest_rd_atomic =
  3008. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  3009. qp_attr->min_rnr_timer =
  3010. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  3011. qp_attr->timeout = context.pri_path.ackto >> 3;
  3012. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  3013. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  3014. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  3015. done:
  3016. qp_attr->cur_qp_state = qp_attr->qp_state;
  3017. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  3018. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  3019. if (!ibqp->uobject) {
  3020. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  3021. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  3022. } else {
  3023. qp_attr->cap.max_send_wr = 0;
  3024. qp_attr->cap.max_send_sge = 0;
  3025. }
  3026. /*
  3027. * We don't support inline sends for kernel QPs (yet), and we
  3028. * don't know what userspace's value should be.
  3029. */
  3030. qp_attr->cap.max_inline_data = 0;
  3031. qp_init_attr->cap = qp_attr->cap;
  3032. qp_init_attr->create_flags = 0;
  3033. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  3034. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  3035. if (qp->flags & MLX4_IB_QP_LSO)
  3036. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  3037. if (qp->flags & MLX4_IB_QP_NETIF)
  3038. qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP;
  3039. qp_init_attr->sq_sig_type =
  3040. qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ?
  3041. IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  3042. out:
  3043. mutex_unlock(&qp->mutex);
  3044. return err;
  3045. }