qp.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531
  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 <rdma/mlx4-abi.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 = NULL;
  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 (sqp)
  827. kfree(sqp);
  828. else if (!*caller_qp)
  829. kfree(qp);
  830. return err;
  831. }
  832. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  833. {
  834. switch (state) {
  835. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  836. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  837. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  838. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  839. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  840. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  841. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  842. default: return -1;
  843. }
  844. }
  845. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  846. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  847. {
  848. if (send_cq == recv_cq) {
  849. spin_lock(&send_cq->lock);
  850. __acquire(&recv_cq->lock);
  851. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  852. spin_lock(&send_cq->lock);
  853. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  854. } else {
  855. spin_lock(&recv_cq->lock);
  856. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  857. }
  858. }
  859. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  860. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  861. {
  862. if (send_cq == recv_cq) {
  863. __release(&recv_cq->lock);
  864. spin_unlock(&send_cq->lock);
  865. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  866. spin_unlock(&recv_cq->lock);
  867. spin_unlock(&send_cq->lock);
  868. } else {
  869. spin_unlock(&send_cq->lock);
  870. spin_unlock(&recv_cq->lock);
  871. }
  872. }
  873. static void del_gid_entries(struct mlx4_ib_qp *qp)
  874. {
  875. struct mlx4_ib_gid_entry *ge, *tmp;
  876. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  877. list_del(&ge->list);
  878. kfree(ge);
  879. }
  880. }
  881. static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp)
  882. {
  883. if (qp->ibqp.qp_type == IB_QPT_XRC_TGT)
  884. return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd);
  885. else
  886. return to_mpd(qp->ibqp.pd);
  887. }
  888. static void get_cqs(struct mlx4_ib_qp *qp,
  889. struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq)
  890. {
  891. switch (qp->ibqp.qp_type) {
  892. case IB_QPT_XRC_TGT:
  893. *send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq);
  894. *recv_cq = *send_cq;
  895. break;
  896. case IB_QPT_XRC_INI:
  897. *send_cq = to_mcq(qp->ibqp.send_cq);
  898. *recv_cq = *send_cq;
  899. break;
  900. default:
  901. *send_cq = to_mcq(qp->ibqp.send_cq);
  902. *recv_cq = to_mcq(qp->ibqp.recv_cq);
  903. break;
  904. }
  905. }
  906. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  907. int is_user)
  908. {
  909. struct mlx4_ib_cq *send_cq, *recv_cq;
  910. unsigned long flags;
  911. if (qp->state != IB_QPS_RESET) {
  912. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  913. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  914. pr_warn("modify QP %06x to RESET failed.\n",
  915. qp->mqp.qpn);
  916. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  917. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  918. qp->pri.smac = 0;
  919. qp->pri.smac_port = 0;
  920. }
  921. if (qp->alt.smac) {
  922. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  923. qp->alt.smac = 0;
  924. }
  925. if (qp->pri.vid < 0x1000) {
  926. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  927. qp->pri.vid = 0xFFFF;
  928. qp->pri.candidate_vid = 0xFFFF;
  929. qp->pri.update_vid = 0;
  930. }
  931. if (qp->alt.vid < 0x1000) {
  932. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  933. qp->alt.vid = 0xFFFF;
  934. qp->alt.candidate_vid = 0xFFFF;
  935. qp->alt.update_vid = 0;
  936. }
  937. }
  938. get_cqs(qp, &send_cq, &recv_cq);
  939. spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
  940. mlx4_ib_lock_cqs(send_cq, recv_cq);
  941. /* del from lists under both locks above to protect reset flow paths */
  942. list_del(&qp->qps_list);
  943. list_del(&qp->cq_send_list);
  944. list_del(&qp->cq_recv_list);
  945. if (!is_user) {
  946. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  947. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  948. if (send_cq != recv_cq)
  949. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  950. }
  951. mlx4_qp_remove(dev->dev, &qp->mqp);
  952. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  953. spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
  954. mlx4_qp_free(dev->dev, &qp->mqp);
  955. if (!is_sqp(dev, qp) && !is_tunnel_qp(dev, qp)) {
  956. if (qp->flags & MLX4_IB_QP_NETIF)
  957. mlx4_ib_steer_qp_free(dev, qp->mqp.qpn, 1);
  958. else
  959. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  960. }
  961. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  962. if (is_user) {
  963. if (qp->rq.wqe_cnt)
  964. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  965. &qp->db);
  966. ib_umem_release(qp->umem);
  967. } else {
  968. kvfree(qp->sq.wrid);
  969. kvfree(qp->rq.wrid);
  970. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  971. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI))
  972. free_proxy_bufs(&dev->ib_dev, qp);
  973. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  974. if (qp->rq.wqe_cnt)
  975. mlx4_db_free(dev->dev, &qp->db);
  976. }
  977. del_gid_entries(qp);
  978. }
  979. static u32 get_sqp_num(struct mlx4_ib_dev *dev, struct ib_qp_init_attr *attr)
  980. {
  981. /* Native or PPF */
  982. if (!mlx4_is_mfunc(dev->dev) ||
  983. (mlx4_is_master(dev->dev) &&
  984. attr->create_flags & MLX4_IB_SRIOV_SQP)) {
  985. return dev->dev->phys_caps.base_sqpn +
  986. (attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  987. attr->port_num - 1;
  988. }
  989. /* PF or VF -- creating proxies */
  990. if (attr->qp_type == IB_QPT_SMI)
  991. return dev->dev->caps.qp0_proxy[attr->port_num - 1];
  992. else
  993. return dev->dev->caps.qp1_proxy[attr->port_num - 1];
  994. }
  995. static struct ib_qp *_mlx4_ib_create_qp(struct ib_pd *pd,
  996. struct ib_qp_init_attr *init_attr,
  997. struct ib_udata *udata)
  998. {
  999. struct mlx4_ib_qp *qp = NULL;
  1000. int err;
  1001. int sup_u_create_flags = MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  1002. u16 xrcdn = 0;
  1003. gfp_t gfp;
  1004. gfp = (init_attr->create_flags & MLX4_IB_QP_CREATE_USE_GFP_NOIO) ?
  1005. GFP_NOIO : GFP_KERNEL;
  1006. /*
  1007. * We only support LSO, vendor flag1, and multicast loopback blocking,
  1008. * and only for kernel UD QPs.
  1009. */
  1010. if (init_attr->create_flags & ~(MLX4_IB_QP_LSO |
  1011. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK |
  1012. MLX4_IB_SRIOV_TUNNEL_QP |
  1013. MLX4_IB_SRIOV_SQP |
  1014. MLX4_IB_QP_NETIF |
  1015. MLX4_IB_QP_CREATE_ROCE_V2_GSI |
  1016. MLX4_IB_QP_CREATE_USE_GFP_NOIO))
  1017. return ERR_PTR(-EINVAL);
  1018. if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
  1019. if (init_attr->qp_type != IB_QPT_UD)
  1020. return ERR_PTR(-EINVAL);
  1021. }
  1022. if (init_attr->create_flags) {
  1023. if (udata && init_attr->create_flags & ~(sup_u_create_flags))
  1024. return ERR_PTR(-EINVAL);
  1025. if ((init_attr->create_flags & ~(MLX4_IB_SRIOV_SQP |
  1026. MLX4_IB_QP_CREATE_USE_GFP_NOIO |
  1027. MLX4_IB_QP_CREATE_ROCE_V2_GSI |
  1028. MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) &&
  1029. init_attr->qp_type != IB_QPT_UD) ||
  1030. (init_attr->create_flags & MLX4_IB_SRIOV_SQP &&
  1031. init_attr->qp_type > IB_QPT_GSI) ||
  1032. (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI &&
  1033. init_attr->qp_type != IB_QPT_GSI))
  1034. return ERR_PTR(-EINVAL);
  1035. }
  1036. switch (init_attr->qp_type) {
  1037. case IB_QPT_XRC_TGT:
  1038. pd = to_mxrcd(init_attr->xrcd)->pd;
  1039. xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
  1040. init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq;
  1041. /* fall through */
  1042. case IB_QPT_XRC_INI:
  1043. if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
  1044. return ERR_PTR(-ENOSYS);
  1045. init_attr->recv_cq = init_attr->send_cq;
  1046. /* fall through */
  1047. case IB_QPT_RC:
  1048. case IB_QPT_UC:
  1049. case IB_QPT_RAW_PACKET:
  1050. qp = kzalloc(sizeof *qp, gfp);
  1051. if (!qp)
  1052. return ERR_PTR(-ENOMEM);
  1053. qp->pri.vid = 0xFFFF;
  1054. qp->alt.vid = 0xFFFF;
  1055. /* fall through */
  1056. case IB_QPT_UD:
  1057. {
  1058. err = create_qp_common(to_mdev(pd->device), pd, init_attr,
  1059. udata, 0, &qp, gfp);
  1060. if (err) {
  1061. kfree(qp);
  1062. return ERR_PTR(err);
  1063. }
  1064. qp->ibqp.qp_num = qp->mqp.qpn;
  1065. qp->xrcdn = xrcdn;
  1066. break;
  1067. }
  1068. case IB_QPT_SMI:
  1069. case IB_QPT_GSI:
  1070. {
  1071. int sqpn;
  1072. /* Userspace is not allowed to create special QPs: */
  1073. if (udata)
  1074. return ERR_PTR(-EINVAL);
  1075. if (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI) {
  1076. int res = mlx4_qp_reserve_range(to_mdev(pd->device)->dev, 1, 1, &sqpn, 0);
  1077. if (res)
  1078. return ERR_PTR(res);
  1079. } else {
  1080. sqpn = get_sqp_num(to_mdev(pd->device), init_attr);
  1081. }
  1082. err = create_qp_common(to_mdev(pd->device), pd, init_attr, udata,
  1083. sqpn,
  1084. &qp, gfp);
  1085. if (err)
  1086. return ERR_PTR(err);
  1087. qp->port = init_attr->port_num;
  1088. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 :
  1089. init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI ? sqpn : 1;
  1090. break;
  1091. }
  1092. default:
  1093. /* Don't support raw QPs */
  1094. return ERR_PTR(-EINVAL);
  1095. }
  1096. return &qp->ibqp;
  1097. }
  1098. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  1099. struct ib_qp_init_attr *init_attr,
  1100. struct ib_udata *udata) {
  1101. struct ib_device *device = pd ? pd->device : init_attr->xrcd->device;
  1102. struct ib_qp *ibqp;
  1103. struct mlx4_ib_dev *dev = to_mdev(device);
  1104. ibqp = _mlx4_ib_create_qp(pd, init_attr, udata);
  1105. if (!IS_ERR(ibqp) &&
  1106. (init_attr->qp_type == IB_QPT_GSI) &&
  1107. !(init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI)) {
  1108. struct mlx4_ib_sqp *sqp = to_msqp((to_mqp(ibqp)));
  1109. int is_eth = rdma_cap_eth_ah(&dev->ib_dev, init_attr->port_num);
  1110. if (is_eth &&
  1111. dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
  1112. init_attr->create_flags |= MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1113. sqp->roce_v2_gsi = ib_create_qp(pd, init_attr);
  1114. if (IS_ERR(sqp->roce_v2_gsi)) {
  1115. pr_err("Failed to create GSI QP for RoCEv2 (%ld)\n", PTR_ERR(sqp->roce_v2_gsi));
  1116. sqp->roce_v2_gsi = NULL;
  1117. } else {
  1118. sqp = to_msqp(to_mqp(sqp->roce_v2_gsi));
  1119. sqp->qp.flags |= MLX4_IB_ROCE_V2_GSI_QP;
  1120. }
  1121. init_attr->create_flags &= ~MLX4_IB_QP_CREATE_ROCE_V2_GSI;
  1122. }
  1123. }
  1124. return ibqp;
  1125. }
  1126. static int _mlx4_ib_destroy_qp(struct ib_qp *qp)
  1127. {
  1128. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  1129. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1130. struct mlx4_ib_pd *pd;
  1131. if (is_qp0(dev, mqp))
  1132. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  1133. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI &&
  1134. dev->qp1_proxy[mqp->port - 1] == mqp) {
  1135. mutex_lock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1136. dev->qp1_proxy[mqp->port - 1] = NULL;
  1137. mutex_unlock(&dev->qp1_proxy_lock[mqp->port - 1]);
  1138. }
  1139. if (mqp->counter_index)
  1140. mlx4_ib_free_qp_counter(dev, mqp);
  1141. pd = get_pd(mqp);
  1142. destroy_qp_common(dev, mqp, !!pd->ibpd.uobject);
  1143. if (is_sqp(dev, mqp))
  1144. kfree(to_msqp(mqp));
  1145. else
  1146. kfree(mqp);
  1147. return 0;
  1148. }
  1149. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  1150. {
  1151. struct mlx4_ib_qp *mqp = to_mqp(qp);
  1152. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  1153. struct mlx4_ib_sqp *sqp = to_msqp(mqp);
  1154. if (sqp->roce_v2_gsi)
  1155. ib_destroy_qp(sqp->roce_v2_gsi);
  1156. }
  1157. return _mlx4_ib_destroy_qp(qp);
  1158. }
  1159. static int to_mlx4_st(struct mlx4_ib_dev *dev, enum mlx4_ib_qp_type type)
  1160. {
  1161. switch (type) {
  1162. case MLX4_IB_QPT_RC: return MLX4_QP_ST_RC;
  1163. case MLX4_IB_QPT_UC: return MLX4_QP_ST_UC;
  1164. case MLX4_IB_QPT_UD: return MLX4_QP_ST_UD;
  1165. case MLX4_IB_QPT_XRC_INI:
  1166. case MLX4_IB_QPT_XRC_TGT: return MLX4_QP_ST_XRC;
  1167. case MLX4_IB_QPT_SMI:
  1168. case MLX4_IB_QPT_GSI:
  1169. case MLX4_IB_QPT_RAW_PACKET: return MLX4_QP_ST_MLX;
  1170. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  1171. case MLX4_IB_QPT_TUN_SMI_OWNER: return (mlx4_is_mfunc(dev->dev) ?
  1172. MLX4_QP_ST_MLX : -1);
  1173. case MLX4_IB_QPT_PROXY_SMI:
  1174. case MLX4_IB_QPT_TUN_SMI:
  1175. case MLX4_IB_QPT_PROXY_GSI:
  1176. case MLX4_IB_QPT_TUN_GSI: return (mlx4_is_mfunc(dev->dev) ?
  1177. MLX4_QP_ST_UD : -1);
  1178. default: return -1;
  1179. }
  1180. }
  1181. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  1182. int attr_mask)
  1183. {
  1184. u8 dest_rd_atomic;
  1185. u32 access_flags;
  1186. u32 hw_access_flags = 0;
  1187. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1188. dest_rd_atomic = attr->max_dest_rd_atomic;
  1189. else
  1190. dest_rd_atomic = qp->resp_depth;
  1191. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1192. access_flags = attr->qp_access_flags;
  1193. else
  1194. access_flags = qp->atomic_rd_en;
  1195. if (!dest_rd_atomic)
  1196. access_flags &= IB_ACCESS_REMOTE_WRITE;
  1197. if (access_flags & IB_ACCESS_REMOTE_READ)
  1198. hw_access_flags |= MLX4_QP_BIT_RRE;
  1199. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  1200. hw_access_flags |= MLX4_QP_BIT_RAE;
  1201. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  1202. hw_access_flags |= MLX4_QP_BIT_RWE;
  1203. return cpu_to_be32(hw_access_flags);
  1204. }
  1205. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  1206. int attr_mask)
  1207. {
  1208. if (attr_mask & IB_QP_PKEY_INDEX)
  1209. sqp->pkey_index = attr->pkey_index;
  1210. if (attr_mask & IB_QP_QKEY)
  1211. sqp->qkey = attr->qkey;
  1212. if (attr_mask & IB_QP_SQ_PSN)
  1213. sqp->send_psn = attr->sq_psn;
  1214. }
  1215. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  1216. {
  1217. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  1218. }
  1219. static int _mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  1220. u64 smac, u16 vlan_tag, struct mlx4_qp_path *path,
  1221. struct mlx4_roce_smac_vlan_info *smac_info, u8 port)
  1222. {
  1223. int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port) ==
  1224. IB_LINK_LAYER_ETHERNET;
  1225. int vidx;
  1226. int smac_index;
  1227. int err;
  1228. path->grh_mylmc = ah->src_path_bits & 0x7f;
  1229. path->rlid = cpu_to_be16(ah->dlid);
  1230. if (ah->static_rate) {
  1231. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  1232. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  1233. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  1234. --path->static_rate;
  1235. } else
  1236. path->static_rate = 0;
  1237. if (ah->ah_flags & IB_AH_GRH) {
  1238. int real_sgid_index = mlx4_ib_gid_index_to_real_index(dev,
  1239. port,
  1240. ah->grh.sgid_index);
  1241. if (real_sgid_index >= dev->dev->caps.gid_table_len[port]) {
  1242. pr_err("sgid_index (%u) too large. max is %d\n",
  1243. real_sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  1244. return -1;
  1245. }
  1246. path->grh_mylmc |= 1 << 7;
  1247. path->mgid_index = real_sgid_index;
  1248. path->hop_limit = ah->grh.hop_limit;
  1249. path->tclass_flowlabel =
  1250. cpu_to_be32((ah->grh.traffic_class << 20) |
  1251. (ah->grh.flow_label));
  1252. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  1253. }
  1254. if (is_eth) {
  1255. if (!(ah->ah_flags & IB_AH_GRH))
  1256. return -1;
  1257. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1258. ((port - 1) << 6) | ((ah->sl & 7) << 3);
  1259. path->feup |= MLX4_FEUP_FORCE_ETH_UP;
  1260. if (vlan_tag < 0x1000) {
  1261. if (smac_info->vid < 0x1000) {
  1262. /* both valid vlan ids */
  1263. if (smac_info->vid != vlan_tag) {
  1264. /* different VIDs. unreg old and reg new */
  1265. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1266. if (err)
  1267. return err;
  1268. smac_info->candidate_vid = vlan_tag;
  1269. smac_info->candidate_vlan_index = vidx;
  1270. smac_info->candidate_vlan_port = port;
  1271. smac_info->update_vid = 1;
  1272. path->vlan_index = vidx;
  1273. } else {
  1274. path->vlan_index = smac_info->vlan_index;
  1275. }
  1276. } else {
  1277. /* no current vlan tag in qp */
  1278. err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
  1279. if (err)
  1280. return err;
  1281. smac_info->candidate_vid = vlan_tag;
  1282. smac_info->candidate_vlan_index = vidx;
  1283. smac_info->candidate_vlan_port = port;
  1284. smac_info->update_vid = 1;
  1285. path->vlan_index = vidx;
  1286. }
  1287. path->feup |= MLX4_FVL_FORCE_ETH_VLAN;
  1288. path->fl = 1 << 6;
  1289. } else {
  1290. /* have current vlan tag. unregister it at modify-qp success */
  1291. if (smac_info->vid < 0x1000) {
  1292. smac_info->candidate_vid = 0xFFFF;
  1293. smac_info->update_vid = 1;
  1294. }
  1295. }
  1296. /* get smac_index for RoCE use.
  1297. * If no smac was yet assigned, register one.
  1298. * If one was already assigned, but the new mac differs,
  1299. * unregister the old one and register the new one.
  1300. */
  1301. if ((!smac_info->smac && !smac_info->smac_port) ||
  1302. smac_info->smac != smac) {
  1303. /* register candidate now, unreg if needed, after success */
  1304. smac_index = mlx4_register_mac(dev->dev, port, smac);
  1305. if (smac_index >= 0) {
  1306. smac_info->candidate_smac_index = smac_index;
  1307. smac_info->candidate_smac = smac;
  1308. smac_info->candidate_smac_port = port;
  1309. } else {
  1310. return -EINVAL;
  1311. }
  1312. } else {
  1313. smac_index = smac_info->smac_index;
  1314. }
  1315. memcpy(path->dmac, ah->dmac, 6);
  1316. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  1317. /* put MAC table smac index for IBoE */
  1318. path->grh_mylmc = (u8) (smac_index) | 0x80;
  1319. } else {
  1320. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  1321. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  1322. }
  1323. return 0;
  1324. }
  1325. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_qp_attr *qp,
  1326. enum ib_qp_attr_mask qp_attr_mask,
  1327. struct mlx4_ib_qp *mqp,
  1328. struct mlx4_qp_path *path, u8 port,
  1329. u16 vlan_id, u8 *smac)
  1330. {
  1331. return _mlx4_set_path(dev, &qp->ah_attr,
  1332. mlx4_mac_to_u64(smac),
  1333. vlan_id,
  1334. path, &mqp->pri, port);
  1335. }
  1336. static int mlx4_set_alt_path(struct mlx4_ib_dev *dev,
  1337. const struct ib_qp_attr *qp,
  1338. enum ib_qp_attr_mask qp_attr_mask,
  1339. struct mlx4_ib_qp *mqp,
  1340. struct mlx4_qp_path *path, u8 port)
  1341. {
  1342. return _mlx4_set_path(dev, &qp->alt_ah_attr,
  1343. 0,
  1344. 0xffff,
  1345. path, &mqp->alt, port);
  1346. }
  1347. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1348. {
  1349. struct mlx4_ib_gid_entry *ge, *tmp;
  1350. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  1351. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  1352. ge->added = 1;
  1353. ge->port = qp->port;
  1354. }
  1355. }
  1356. }
  1357. static int handle_eth_ud_smac_index(struct mlx4_ib_dev *dev,
  1358. struct mlx4_ib_qp *qp,
  1359. struct mlx4_qp_context *context)
  1360. {
  1361. u64 u64_mac;
  1362. int smac_index;
  1363. u64_mac = atomic64_read(&dev->iboe.mac[qp->port - 1]);
  1364. context->pri_path.sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | ((qp->port - 1) << 6);
  1365. if (!qp->pri.smac && !qp->pri.smac_port) {
  1366. smac_index = mlx4_register_mac(dev->dev, qp->port, u64_mac);
  1367. if (smac_index >= 0) {
  1368. qp->pri.candidate_smac_index = smac_index;
  1369. qp->pri.candidate_smac = u64_mac;
  1370. qp->pri.candidate_smac_port = qp->port;
  1371. context->pri_path.grh_mylmc = 0x80 | (u8) smac_index;
  1372. } else {
  1373. return -ENOENT;
  1374. }
  1375. }
  1376. return 0;
  1377. }
  1378. static int create_qp_lb_counter(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  1379. {
  1380. struct counter_index *new_counter_index;
  1381. int err;
  1382. u32 tmp_idx;
  1383. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) !=
  1384. IB_LINK_LAYER_ETHERNET ||
  1385. !(qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) ||
  1386. !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_LB_SRC_CHK))
  1387. return 0;
  1388. err = mlx4_counter_alloc(dev->dev, &tmp_idx);
  1389. if (err)
  1390. return err;
  1391. new_counter_index = kmalloc(sizeof(*new_counter_index), GFP_KERNEL);
  1392. if (!new_counter_index) {
  1393. mlx4_counter_free(dev->dev, tmp_idx);
  1394. return -ENOMEM;
  1395. }
  1396. new_counter_index->index = tmp_idx;
  1397. new_counter_index->allocated = 1;
  1398. qp->counter_index = new_counter_index;
  1399. mutex_lock(&dev->counters_table[qp->port - 1].mutex);
  1400. list_add_tail(&new_counter_index->list,
  1401. &dev->counters_table[qp->port - 1].counters_list);
  1402. mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
  1403. return 0;
  1404. }
  1405. enum {
  1406. MLX4_QPC_ROCE_MODE_1 = 0,
  1407. MLX4_QPC_ROCE_MODE_2 = 2,
  1408. MLX4_QPC_ROCE_MODE_UNDEFINED = 0xff
  1409. };
  1410. static u8 gid_type_to_qpc(enum ib_gid_type gid_type)
  1411. {
  1412. switch (gid_type) {
  1413. case IB_GID_TYPE_ROCE:
  1414. return MLX4_QPC_ROCE_MODE_1;
  1415. case IB_GID_TYPE_ROCE_UDP_ENCAP:
  1416. return MLX4_QPC_ROCE_MODE_2;
  1417. default:
  1418. return MLX4_QPC_ROCE_MODE_UNDEFINED;
  1419. }
  1420. }
  1421. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  1422. const struct ib_qp_attr *attr, int attr_mask,
  1423. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  1424. {
  1425. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1426. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1427. struct mlx4_ib_pd *pd;
  1428. struct mlx4_ib_cq *send_cq, *recv_cq;
  1429. struct mlx4_qp_context *context;
  1430. enum mlx4_qp_optpar optpar = 0;
  1431. int sqd_event;
  1432. int steer_qp = 0;
  1433. int err = -EINVAL;
  1434. int counter_index;
  1435. /* APM is not supported under RoCE */
  1436. if (attr_mask & IB_QP_ALT_PATH &&
  1437. rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  1438. IB_LINK_LAYER_ETHERNET)
  1439. return -ENOTSUPP;
  1440. context = kzalloc(sizeof *context, GFP_KERNEL);
  1441. if (!context)
  1442. return -ENOMEM;
  1443. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  1444. (to_mlx4_st(dev, qp->mlx4_ib_qp_type) << 16));
  1445. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  1446. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1447. else {
  1448. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  1449. switch (attr->path_mig_state) {
  1450. case IB_MIG_MIGRATED:
  1451. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  1452. break;
  1453. case IB_MIG_REARM:
  1454. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  1455. break;
  1456. case IB_MIG_ARMED:
  1457. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  1458. break;
  1459. }
  1460. }
  1461. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
  1462. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  1463. else if (ibqp->qp_type == IB_QPT_RAW_PACKET)
  1464. context->mtu_msgmax = (MLX4_RAW_QP_MTU << 5) | MLX4_RAW_QP_MSGMAX;
  1465. else if (ibqp->qp_type == IB_QPT_UD) {
  1466. if (qp->flags & MLX4_IB_QP_LSO)
  1467. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  1468. ilog2(dev->dev->caps.max_gso_sz);
  1469. else
  1470. context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
  1471. } else if (attr_mask & IB_QP_PATH_MTU) {
  1472. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  1473. pr_err("path MTU (%u) is invalid\n",
  1474. attr->path_mtu);
  1475. goto out;
  1476. }
  1477. context->mtu_msgmax = (attr->path_mtu << 5) |
  1478. ilog2(dev->dev->caps.max_msg_sz);
  1479. }
  1480. if (qp->rq.wqe_cnt)
  1481. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  1482. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  1483. if (qp->sq.wqe_cnt)
  1484. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  1485. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  1486. if (new_state == IB_QPS_RESET && qp->counter_index)
  1487. mlx4_ib_free_qp_counter(dev, qp);
  1488. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1489. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  1490. context->xrcd = cpu_to_be32((u32) qp->xrcdn);
  1491. if (ibqp->qp_type == IB_QPT_RAW_PACKET)
  1492. context->param3 |= cpu_to_be32(1 << 30);
  1493. }
  1494. if (qp->ibqp.uobject)
  1495. context->usr_page = cpu_to_be32(
  1496. mlx4_to_hw_uar_index(dev->dev,
  1497. to_mucontext(ibqp->uobject->context)->uar.index));
  1498. else
  1499. context->usr_page = cpu_to_be32(
  1500. mlx4_to_hw_uar_index(dev->dev, dev->priv_uar.index));
  1501. if (attr_mask & IB_QP_DEST_QPN)
  1502. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  1503. if (attr_mask & IB_QP_PORT) {
  1504. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  1505. !(attr_mask & IB_QP_AV)) {
  1506. mlx4_set_sched(&context->pri_path, attr->port_num);
  1507. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  1508. }
  1509. }
  1510. if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
  1511. err = create_qp_lb_counter(dev, qp);
  1512. if (err)
  1513. goto out;
  1514. counter_index =
  1515. dev->counters_table[qp->port - 1].default_counter;
  1516. if (qp->counter_index)
  1517. counter_index = qp->counter_index->index;
  1518. if (counter_index != -1) {
  1519. context->pri_path.counter_index = counter_index;
  1520. optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
  1521. if (qp->counter_index) {
  1522. context->pri_path.fl |=
  1523. MLX4_FL_ETH_SRC_CHECK_MC_LB;
  1524. context->pri_path.vlan_control |=
  1525. MLX4_CTRL_ETH_SRC_CHECK_IF_COUNTER;
  1526. }
  1527. } else
  1528. context->pri_path.counter_index =
  1529. MLX4_SINK_COUNTER_INDEX(dev->dev);
  1530. if (qp->flags & MLX4_IB_QP_NETIF) {
  1531. mlx4_ib_steer_qp_reg(dev, qp, 1);
  1532. steer_qp = 1;
  1533. }
  1534. if (ibqp->qp_type == IB_QPT_GSI) {
  1535. enum ib_gid_type gid_type = qp->flags & MLX4_IB_ROCE_V2_GSI_QP ?
  1536. IB_GID_TYPE_ROCE_UDP_ENCAP : IB_GID_TYPE_ROCE;
  1537. u8 qpc_roce_mode = gid_type_to_qpc(gid_type);
  1538. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1539. }
  1540. }
  1541. if (attr_mask & IB_QP_PKEY_INDEX) {
  1542. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1543. context->pri_path.disable_pkey_check = 0x40;
  1544. context->pri_path.pkey_index = attr->pkey_index;
  1545. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  1546. }
  1547. if (attr_mask & IB_QP_AV) {
  1548. u8 port_num = mlx4_is_bonded(to_mdev(ibqp->device)->dev) ? 1 :
  1549. attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1550. union ib_gid gid;
  1551. struct ib_gid_attr gid_attr = {.gid_type = IB_GID_TYPE_IB};
  1552. u16 vlan = 0xffff;
  1553. u8 smac[ETH_ALEN];
  1554. int status = 0;
  1555. int is_eth = rdma_cap_eth_ah(&dev->ib_dev, port_num) &&
  1556. attr->ah_attr.ah_flags & IB_AH_GRH;
  1557. if (is_eth && attr->ah_attr.ah_flags & IB_AH_GRH) {
  1558. int index = attr->ah_attr.grh.sgid_index;
  1559. status = ib_get_cached_gid(ibqp->device, port_num,
  1560. index, &gid, &gid_attr);
  1561. if (!status && !memcmp(&gid, &zgid, sizeof(gid)))
  1562. status = -ENOENT;
  1563. if (!status && gid_attr.ndev) {
  1564. vlan = rdma_vlan_dev_vlan_id(gid_attr.ndev);
  1565. memcpy(smac, gid_attr.ndev->dev_addr, ETH_ALEN);
  1566. dev_put(gid_attr.ndev);
  1567. }
  1568. }
  1569. if (status)
  1570. goto out;
  1571. if (mlx4_set_path(dev, attr, attr_mask, qp, &context->pri_path,
  1572. port_num, vlan, smac))
  1573. goto out;
  1574. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  1575. MLX4_QP_OPTPAR_SCHED_QUEUE);
  1576. if (is_eth &&
  1577. (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR)) {
  1578. u8 qpc_roce_mode = gid_type_to_qpc(gid_attr.gid_type);
  1579. if (qpc_roce_mode == MLX4_QPC_ROCE_MODE_UNDEFINED) {
  1580. err = -EINVAL;
  1581. goto out;
  1582. }
  1583. context->rlkey_roce_mode |= (qpc_roce_mode << 6);
  1584. }
  1585. }
  1586. if (attr_mask & IB_QP_TIMEOUT) {
  1587. context->pri_path.ackto |= attr->timeout << 3;
  1588. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  1589. }
  1590. if (attr_mask & IB_QP_ALT_PATH) {
  1591. if (attr->alt_port_num == 0 ||
  1592. attr->alt_port_num > dev->dev->caps.num_ports)
  1593. goto out;
  1594. if (attr->alt_pkey_index >=
  1595. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  1596. goto out;
  1597. if (mlx4_set_alt_path(dev, attr, attr_mask, qp,
  1598. &context->alt_path,
  1599. attr->alt_port_num))
  1600. goto out;
  1601. context->alt_path.pkey_index = attr->alt_pkey_index;
  1602. context->alt_path.ackto = attr->alt_timeout << 3;
  1603. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  1604. }
  1605. pd = get_pd(qp);
  1606. get_cqs(qp, &send_cq, &recv_cq);
  1607. context->pd = cpu_to_be32(pd->pdn);
  1608. context->cqn_send = cpu_to_be32(send_cq->mcq.cqn);
  1609. context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn);
  1610. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  1611. /* Set "fast registration enabled" for all kernel QPs */
  1612. if (!qp->ibqp.uobject)
  1613. context->params1 |= cpu_to_be32(1 << 11);
  1614. if (attr_mask & IB_QP_RNR_RETRY) {
  1615. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  1616. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  1617. }
  1618. if (attr_mask & IB_QP_RETRY_CNT) {
  1619. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  1620. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  1621. }
  1622. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  1623. if (attr->max_rd_atomic)
  1624. context->params1 |=
  1625. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  1626. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  1627. }
  1628. if (attr_mask & IB_QP_SQ_PSN)
  1629. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  1630. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  1631. if (attr->max_dest_rd_atomic)
  1632. context->params2 |=
  1633. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  1634. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  1635. }
  1636. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  1637. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  1638. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  1639. }
  1640. if (ibqp->srq)
  1641. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  1642. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  1643. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  1644. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  1645. }
  1646. if (attr_mask & IB_QP_RQ_PSN)
  1647. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  1648. /* proxy and tunnel qp qkeys will be changed in modify-qp wrappers */
  1649. if (attr_mask & IB_QP_QKEY) {
  1650. if (qp->mlx4_ib_qp_type &
  1651. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))
  1652. context->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  1653. else {
  1654. if (mlx4_is_mfunc(dev->dev) &&
  1655. !(qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) &&
  1656. (attr->qkey & MLX4_RESERVED_QKEY_MASK) ==
  1657. MLX4_RESERVED_QKEY_BASE) {
  1658. pr_err("Cannot use reserved QKEY"
  1659. " 0x%x (range 0xffff0000..0xffffffff"
  1660. " is reserved)\n", attr->qkey);
  1661. err = -EINVAL;
  1662. goto out;
  1663. }
  1664. context->qkey = cpu_to_be32(attr->qkey);
  1665. }
  1666. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  1667. }
  1668. if (ibqp->srq)
  1669. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  1670. if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1671. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  1672. if (cur_state == IB_QPS_INIT &&
  1673. new_state == IB_QPS_RTR &&
  1674. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  1675. ibqp->qp_type == IB_QPT_UD ||
  1676. ibqp->qp_type == IB_QPT_RAW_PACKET)) {
  1677. context->pri_path.sched_queue = (qp->port - 1) << 6;
  1678. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  1679. qp->mlx4_ib_qp_type &
  1680. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) {
  1681. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  1682. if (qp->mlx4_ib_qp_type != MLX4_IB_QPT_SMI)
  1683. context->pri_path.fl = 0x80;
  1684. } else {
  1685. if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
  1686. context->pri_path.fl = 0x80;
  1687. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  1688. }
  1689. if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
  1690. IB_LINK_LAYER_ETHERNET) {
  1691. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI ||
  1692. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI)
  1693. context->pri_path.feup = 1 << 7; /* don't fsm */
  1694. /* handle smac_index */
  1695. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_UD ||
  1696. qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI ||
  1697. qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI) {
  1698. err = handle_eth_ud_smac_index(dev, qp, context);
  1699. if (err) {
  1700. err = -EINVAL;
  1701. goto out;
  1702. }
  1703. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
  1704. dev->qp1_proxy[qp->port - 1] = qp;
  1705. }
  1706. }
  1707. }
  1708. if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET) {
  1709. context->pri_path.ackto = (context->pri_path.ackto & 0xf8) |
  1710. MLX4_IB_LINK_TYPE_ETH;
  1711. if (dev->dev->caps.tunnel_offload_mode == MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
  1712. /* set QP to receive both tunneled & non-tunneled packets */
  1713. if (!(context->flags & cpu_to_be32(1 << MLX4_RSS_QPC_FLAG_OFFSET)))
  1714. context->srqn = cpu_to_be32(7 << 28);
  1715. }
  1716. }
  1717. if (ibqp->qp_type == IB_QPT_UD && (new_state == IB_QPS_RTR)) {
  1718. int is_eth = rdma_port_get_link_layer(
  1719. &dev->ib_dev, qp->port) ==
  1720. IB_LINK_LAYER_ETHERNET;
  1721. if (is_eth) {
  1722. context->pri_path.ackto = MLX4_IB_LINK_TYPE_ETH;
  1723. optpar |= MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH;
  1724. }
  1725. }
  1726. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  1727. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  1728. sqd_event = 1;
  1729. else
  1730. sqd_event = 0;
  1731. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  1732. context->rlkey_roce_mode |= (1 << 4);
  1733. /*
  1734. * Before passing a kernel QP to the HW, make sure that the
  1735. * ownership bits of the send queue are set and the SQ
  1736. * headroom is stamped so that the hardware doesn't start
  1737. * processing stale work requests.
  1738. */
  1739. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  1740. struct mlx4_wqe_ctrl_seg *ctrl;
  1741. int i;
  1742. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  1743. ctrl = get_send_wqe(qp, i);
  1744. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  1745. if (qp->sq_max_wqes_per_wr == 1)
  1746. ctrl->qpn_vlan.fence_size =
  1747. 1 << (qp->sq.wqe_shift - 4);
  1748. stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
  1749. }
  1750. }
  1751. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  1752. to_mlx4_state(new_state), context, optpar,
  1753. sqd_event, &qp->mqp);
  1754. if (err)
  1755. goto out;
  1756. qp->state = new_state;
  1757. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1758. qp->atomic_rd_en = attr->qp_access_flags;
  1759. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1760. qp->resp_depth = attr->max_dest_rd_atomic;
  1761. if (attr_mask & IB_QP_PORT) {
  1762. qp->port = attr->port_num;
  1763. update_mcg_macs(dev, qp);
  1764. }
  1765. if (attr_mask & IB_QP_ALT_PATH)
  1766. qp->alt_port = attr->alt_port_num;
  1767. if (is_sqp(dev, qp))
  1768. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  1769. /*
  1770. * If we moved QP0 to RTR, bring the IB link up; if we moved
  1771. * QP0 to RESET or ERROR, bring the link back down.
  1772. */
  1773. if (is_qp0(dev, qp)) {
  1774. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  1775. if (mlx4_INIT_PORT(dev->dev, qp->port))
  1776. pr_warn("INIT_PORT failed for port %d\n",
  1777. qp->port);
  1778. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  1779. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  1780. mlx4_CLOSE_PORT(dev->dev, qp->port);
  1781. }
  1782. /*
  1783. * If we moved a kernel QP to RESET, clean up all old CQ
  1784. * entries and reinitialize the QP.
  1785. */
  1786. if (new_state == IB_QPS_RESET) {
  1787. if (!ibqp->uobject) {
  1788. mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  1789. ibqp->srq ? to_msrq(ibqp->srq) : NULL);
  1790. if (send_cq != recv_cq)
  1791. mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  1792. qp->rq.head = 0;
  1793. qp->rq.tail = 0;
  1794. qp->sq.head = 0;
  1795. qp->sq.tail = 0;
  1796. qp->sq_next_wqe = 0;
  1797. if (qp->rq.wqe_cnt)
  1798. *qp->db.db = 0;
  1799. if (qp->flags & MLX4_IB_QP_NETIF)
  1800. mlx4_ib_steer_qp_reg(dev, qp, 0);
  1801. }
  1802. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
  1803. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  1804. qp->pri.smac = 0;
  1805. qp->pri.smac_port = 0;
  1806. }
  1807. if (qp->alt.smac) {
  1808. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  1809. qp->alt.smac = 0;
  1810. }
  1811. if (qp->pri.vid < 0x1000) {
  1812. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
  1813. qp->pri.vid = 0xFFFF;
  1814. qp->pri.candidate_vid = 0xFFFF;
  1815. qp->pri.update_vid = 0;
  1816. }
  1817. if (qp->alt.vid < 0x1000) {
  1818. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
  1819. qp->alt.vid = 0xFFFF;
  1820. qp->alt.candidate_vid = 0xFFFF;
  1821. qp->alt.update_vid = 0;
  1822. }
  1823. }
  1824. out:
  1825. if (err && qp->counter_index)
  1826. mlx4_ib_free_qp_counter(dev, qp);
  1827. if (err && steer_qp)
  1828. mlx4_ib_steer_qp_reg(dev, qp, 0);
  1829. kfree(context);
  1830. if (qp->pri.candidate_smac ||
  1831. (!qp->pri.candidate_smac && qp->pri.candidate_smac_port)) {
  1832. if (err) {
  1833. mlx4_unregister_mac(dev->dev, qp->pri.candidate_smac_port, qp->pri.candidate_smac);
  1834. } else {
  1835. if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port))
  1836. mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
  1837. qp->pri.smac = qp->pri.candidate_smac;
  1838. qp->pri.smac_index = qp->pri.candidate_smac_index;
  1839. qp->pri.smac_port = qp->pri.candidate_smac_port;
  1840. }
  1841. qp->pri.candidate_smac = 0;
  1842. qp->pri.candidate_smac_index = 0;
  1843. qp->pri.candidate_smac_port = 0;
  1844. }
  1845. if (qp->alt.candidate_smac) {
  1846. if (err) {
  1847. mlx4_unregister_mac(dev->dev, qp->alt.candidate_smac_port, qp->alt.candidate_smac);
  1848. } else {
  1849. if (qp->alt.smac)
  1850. mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
  1851. qp->alt.smac = qp->alt.candidate_smac;
  1852. qp->alt.smac_index = qp->alt.candidate_smac_index;
  1853. qp->alt.smac_port = qp->alt.candidate_smac_port;
  1854. }
  1855. qp->alt.candidate_smac = 0;
  1856. qp->alt.candidate_smac_index = 0;
  1857. qp->alt.candidate_smac_port = 0;
  1858. }
  1859. if (qp->pri.update_vid) {
  1860. if (err) {
  1861. if (qp->pri.candidate_vid < 0x1000)
  1862. mlx4_unregister_vlan(dev->dev, qp->pri.candidate_vlan_port,
  1863. qp->pri.candidate_vid);
  1864. } else {
  1865. if (qp->pri.vid < 0x1000)
  1866. mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port,
  1867. qp->pri.vid);
  1868. qp->pri.vid = qp->pri.candidate_vid;
  1869. qp->pri.vlan_port = qp->pri.candidate_vlan_port;
  1870. qp->pri.vlan_index = qp->pri.candidate_vlan_index;
  1871. }
  1872. qp->pri.candidate_vid = 0xFFFF;
  1873. qp->pri.update_vid = 0;
  1874. }
  1875. if (qp->alt.update_vid) {
  1876. if (err) {
  1877. if (qp->alt.candidate_vid < 0x1000)
  1878. mlx4_unregister_vlan(dev->dev, qp->alt.candidate_vlan_port,
  1879. qp->alt.candidate_vid);
  1880. } else {
  1881. if (qp->alt.vid < 0x1000)
  1882. mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port,
  1883. qp->alt.vid);
  1884. qp->alt.vid = qp->alt.candidate_vid;
  1885. qp->alt.vlan_port = qp->alt.candidate_vlan_port;
  1886. qp->alt.vlan_index = qp->alt.candidate_vlan_index;
  1887. }
  1888. qp->alt.candidate_vid = 0xFFFF;
  1889. qp->alt.update_vid = 0;
  1890. }
  1891. return err;
  1892. }
  1893. static int _mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1894. int attr_mask, struct ib_udata *udata)
  1895. {
  1896. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1897. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1898. enum ib_qp_state cur_state, new_state;
  1899. int err = -EINVAL;
  1900. int ll;
  1901. mutex_lock(&qp->mutex);
  1902. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  1903. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1904. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1905. ll = IB_LINK_LAYER_UNSPECIFIED;
  1906. } else {
  1907. int port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1908. ll = rdma_port_get_link_layer(&dev->ib_dev, port);
  1909. }
  1910. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
  1911. attr_mask, ll)) {
  1912. pr_debug("qpn 0x%x: invalid attribute mask specified "
  1913. "for transition %d to %d. qp_type %d,"
  1914. " attr_mask 0x%x\n",
  1915. ibqp->qp_num, cur_state, new_state,
  1916. ibqp->qp_type, attr_mask);
  1917. goto out;
  1918. }
  1919. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT)) {
  1920. if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
  1921. if ((ibqp->qp_type == IB_QPT_RC) ||
  1922. (ibqp->qp_type == IB_QPT_UD) ||
  1923. (ibqp->qp_type == IB_QPT_UC) ||
  1924. (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
  1925. (ibqp->qp_type == IB_QPT_XRC_INI)) {
  1926. attr->port_num = mlx4_ib_bond_next_port(dev);
  1927. }
  1928. } else {
  1929. /* no sense in changing port_num
  1930. * when ports are bonded */
  1931. attr_mask &= ~IB_QP_PORT;
  1932. }
  1933. }
  1934. if ((attr_mask & IB_QP_PORT) &&
  1935. (attr->port_num == 0 || attr->port_num > dev->num_ports)) {
  1936. pr_debug("qpn 0x%x: invalid port number (%d) specified "
  1937. "for transition %d to %d. qp_type %d\n",
  1938. ibqp->qp_num, attr->port_num, cur_state,
  1939. new_state, ibqp->qp_type);
  1940. goto out;
  1941. }
  1942. if ((attr_mask & IB_QP_PORT) && (ibqp->qp_type == IB_QPT_RAW_PACKET) &&
  1943. (rdma_port_get_link_layer(&dev->ib_dev, attr->port_num) !=
  1944. IB_LINK_LAYER_ETHERNET))
  1945. goto out;
  1946. if (attr_mask & IB_QP_PKEY_INDEX) {
  1947. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1948. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p]) {
  1949. pr_debug("qpn 0x%x: invalid pkey index (%d) specified "
  1950. "for transition %d to %d. qp_type %d\n",
  1951. ibqp->qp_num, attr->pkey_index, cur_state,
  1952. new_state, ibqp->qp_type);
  1953. goto out;
  1954. }
  1955. }
  1956. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  1957. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  1958. pr_debug("qpn 0x%x: max_rd_atomic (%d) too large. "
  1959. "Transition %d to %d. qp_type %d\n",
  1960. ibqp->qp_num, attr->max_rd_atomic, cur_state,
  1961. new_state, ibqp->qp_type);
  1962. goto out;
  1963. }
  1964. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  1965. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  1966. pr_debug("qpn 0x%x: max_dest_rd_atomic (%d) too large. "
  1967. "Transition %d to %d. qp_type %d\n",
  1968. ibqp->qp_num, attr->max_dest_rd_atomic, cur_state,
  1969. new_state, ibqp->qp_type);
  1970. goto out;
  1971. }
  1972. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1973. err = 0;
  1974. goto out;
  1975. }
  1976. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  1977. if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT))
  1978. attr->port_num = 1;
  1979. out:
  1980. mutex_unlock(&qp->mutex);
  1981. return err;
  1982. }
  1983. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1984. int attr_mask, struct ib_udata *udata)
  1985. {
  1986. struct mlx4_ib_qp *mqp = to_mqp(ibqp);
  1987. int ret;
  1988. ret = _mlx4_ib_modify_qp(ibqp, attr, attr_mask, udata);
  1989. if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  1990. struct mlx4_ib_sqp *sqp = to_msqp(mqp);
  1991. int err = 0;
  1992. if (sqp->roce_v2_gsi)
  1993. err = ib_modify_qp(sqp->roce_v2_gsi, attr, attr_mask);
  1994. if (err)
  1995. pr_err("Failed to modify GSI QP for RoCEv2 (%d)\n",
  1996. err);
  1997. }
  1998. return ret;
  1999. }
  2000. static int vf_get_qp0_qkey(struct mlx4_dev *dev, int qpn, u32 *qkey)
  2001. {
  2002. int i;
  2003. for (i = 0; i < dev->caps.num_ports; i++) {
  2004. if (qpn == dev->caps.qp0_proxy[i] ||
  2005. qpn == dev->caps.qp0_tunnel[i]) {
  2006. *qkey = dev->caps.qp0_qkey[i];
  2007. return 0;
  2008. }
  2009. }
  2010. return -EINVAL;
  2011. }
  2012. static int build_sriov_qp0_header(struct mlx4_ib_sqp *sqp,
  2013. struct ib_ud_wr *wr,
  2014. void *wqe, unsigned *mlx_seg_len)
  2015. {
  2016. struct mlx4_ib_dev *mdev = to_mdev(sqp->qp.ibqp.device);
  2017. struct ib_device *ib_dev = &mdev->ib_dev;
  2018. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2019. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2020. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2021. u16 pkey;
  2022. u32 qkey;
  2023. int send_size;
  2024. int header_size;
  2025. int spc;
  2026. int i;
  2027. if (wr->wr.opcode != IB_WR_SEND)
  2028. return -EINVAL;
  2029. send_size = 0;
  2030. for (i = 0; i < wr->wr.num_sge; ++i)
  2031. send_size += wr->wr.sg_list[i].length;
  2032. /* for proxy-qp0 sends, need to add in size of tunnel header */
  2033. /* for tunnel-qp0 sends, tunnel header is already in s/g list */
  2034. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER)
  2035. send_size += sizeof (struct mlx4_ib_tunnel_header);
  2036. ib_ud_header_init(send_size, 1, 0, 0, 0, 0, 0, 0, &sqp->ud_header);
  2037. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) {
  2038. sqp->ud_header.lrh.service_level =
  2039. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2040. sqp->ud_header.lrh.destination_lid =
  2041. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2042. sqp->ud_header.lrh.source_lid =
  2043. cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2044. }
  2045. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2046. /* force loopback */
  2047. mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR);
  2048. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2049. sqp->ud_header.lrh.virtual_lane = 0;
  2050. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2051. ib_get_cached_pkey(ib_dev, sqp->qp.port, 0, &pkey);
  2052. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2053. if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER)
  2054. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2055. else
  2056. sqp->ud_header.bth.destination_qpn =
  2057. cpu_to_be32(mdev->dev->caps.qp0_tunnel[sqp->qp.port - 1]);
  2058. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2059. if (mlx4_is_master(mdev->dev)) {
  2060. if (mlx4_get_parav_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2061. return -EINVAL;
  2062. } else {
  2063. if (vf_get_qp0_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
  2064. return -EINVAL;
  2065. }
  2066. sqp->ud_header.deth.qkey = cpu_to_be32(qkey);
  2067. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.mqp.qpn);
  2068. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2069. sqp->ud_header.immediate_present = 0;
  2070. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2071. /*
  2072. * Inline data segments may not cross a 64 byte boundary. If
  2073. * our UD header is bigger than the space available up to the
  2074. * next 64 byte boundary in the WQE, use two inline data
  2075. * segments to hold the UD header.
  2076. */
  2077. spc = MLX4_INLINE_ALIGN -
  2078. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2079. if (header_size <= spc) {
  2080. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2081. memcpy(inl + 1, sqp->header_buf, header_size);
  2082. i = 1;
  2083. } else {
  2084. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2085. memcpy(inl + 1, sqp->header_buf, spc);
  2086. inl = (void *) (inl + 1) + spc;
  2087. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2088. /*
  2089. * Need a barrier here to make sure all the data is
  2090. * visible before the byte_count field is set.
  2091. * Otherwise the HCA prefetcher could grab the 64-byte
  2092. * chunk with this inline segment and get a valid (!=
  2093. * 0xffffffff) byte count but stale data, and end up
  2094. * generating a packet with bad headers.
  2095. *
  2096. * The first inline segment's byte_count field doesn't
  2097. * need a barrier, because it comes after a
  2098. * control/MLX segment and therefore is at an offset
  2099. * of 16 mod 64.
  2100. */
  2101. wmb();
  2102. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2103. i = 2;
  2104. }
  2105. *mlx_seg_len =
  2106. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2107. return 0;
  2108. }
  2109. static u8 sl_to_vl(struct mlx4_ib_dev *dev, u8 sl, int port_num)
  2110. {
  2111. union sl2vl_tbl_to_u64 tmp_vltab;
  2112. u8 vl;
  2113. if (sl > 15)
  2114. return 0xf;
  2115. tmp_vltab.sl64 = atomic64_read(&dev->sl2vl[port_num - 1]);
  2116. vl = tmp_vltab.sl8[sl >> 1];
  2117. if (sl & 1)
  2118. vl &= 0x0f;
  2119. else
  2120. vl >>= 4;
  2121. return vl;
  2122. }
  2123. #define MLX4_ROCEV2_QP1_SPORT 0xC000
  2124. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_ud_wr *wr,
  2125. void *wqe, unsigned *mlx_seg_len)
  2126. {
  2127. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  2128. struct mlx4_wqe_mlx_seg *mlx = wqe;
  2129. struct mlx4_wqe_ctrl_seg *ctrl = wqe;
  2130. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  2131. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2132. union ib_gid sgid;
  2133. u16 pkey;
  2134. int send_size;
  2135. int header_size;
  2136. int spc;
  2137. int i;
  2138. int err = 0;
  2139. u16 vlan = 0xffff;
  2140. bool is_eth;
  2141. bool is_vlan = false;
  2142. bool is_grh;
  2143. bool is_udp = false;
  2144. int ip_version = 0;
  2145. send_size = 0;
  2146. for (i = 0; i < wr->wr.num_sge; ++i)
  2147. send_size += wr->wr.sg_list[i].length;
  2148. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  2149. is_grh = mlx4_ib_ah_grh_present(ah);
  2150. if (is_eth) {
  2151. struct ib_gid_attr gid_attr;
  2152. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2153. /* When multi-function is enabled, the ib_core gid
  2154. * indexes don't necessarily match the hw ones, so
  2155. * we must use our own cache */
  2156. err = mlx4_get_roce_gid_from_slave(to_mdev(ib_dev)->dev,
  2157. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2158. ah->av.ib.gid_index, &sgid.raw[0]);
  2159. if (err)
  2160. return err;
  2161. } else {
  2162. err = ib_get_cached_gid(ib_dev,
  2163. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2164. ah->av.ib.gid_index, &sgid,
  2165. &gid_attr);
  2166. if (!err) {
  2167. if (gid_attr.ndev)
  2168. dev_put(gid_attr.ndev);
  2169. if (!memcmp(&sgid, &zgid, sizeof(sgid)))
  2170. err = -ENOENT;
  2171. }
  2172. if (!err) {
  2173. is_udp = gid_attr.gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
  2174. if (is_udp) {
  2175. if (ipv6_addr_v4mapped((struct in6_addr *)&sgid))
  2176. ip_version = 4;
  2177. else
  2178. ip_version = 6;
  2179. is_grh = false;
  2180. }
  2181. } else {
  2182. return err;
  2183. }
  2184. }
  2185. if (ah->av.eth.vlan != cpu_to_be16(0xffff)) {
  2186. vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff;
  2187. is_vlan = 1;
  2188. }
  2189. }
  2190. err = ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh,
  2191. ip_version, is_udp, 0, &sqp->ud_header);
  2192. if (err)
  2193. return err;
  2194. if (!is_eth) {
  2195. sqp->ud_header.lrh.service_level =
  2196. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  2197. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  2198. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  2199. }
  2200. if (is_grh || (ip_version == 6)) {
  2201. sqp->ud_header.grh.traffic_class =
  2202. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2203. sqp->ud_header.grh.flow_label =
  2204. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  2205. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  2206. if (is_eth) {
  2207. memcpy(sqp->ud_header.grh.source_gid.raw, sgid.raw, 16);
  2208. } else {
  2209. if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
  2210. /* When multi-function is enabled, the ib_core gid
  2211. * indexes don't necessarily match the hw ones, so
  2212. * we must use our own cache
  2213. */
  2214. sqp->ud_header.grh.source_gid.global.subnet_prefix =
  2215. cpu_to_be64(atomic64_read(&(to_mdev(ib_dev)->sriov.
  2216. demux[sqp->qp.port - 1].
  2217. subnet_prefix)));
  2218. sqp->ud_header.grh.source_gid.global.interface_id =
  2219. to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
  2220. guid_cache[ah->av.ib.gid_index];
  2221. } else {
  2222. ib_get_cached_gid(ib_dev,
  2223. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2224. ah->av.ib.gid_index,
  2225. &sqp->ud_header.grh.source_gid, NULL);
  2226. }
  2227. }
  2228. memcpy(sqp->ud_header.grh.destination_gid.raw,
  2229. ah->av.ib.dgid, 16);
  2230. }
  2231. if (ip_version == 4) {
  2232. sqp->ud_header.ip4.tos =
  2233. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  2234. sqp->ud_header.ip4.id = 0;
  2235. sqp->ud_header.ip4.frag_off = htons(IP_DF);
  2236. sqp->ud_header.ip4.ttl = ah->av.eth.hop_limit;
  2237. memcpy(&sqp->ud_header.ip4.saddr,
  2238. sgid.raw + 12, 4);
  2239. memcpy(&sqp->ud_header.ip4.daddr, ah->av.ib.dgid + 12, 4);
  2240. sqp->ud_header.ip4.check = ib_ud_ip4_csum(&sqp->ud_header);
  2241. }
  2242. if (is_udp) {
  2243. sqp->ud_header.udp.dport = htons(ROCE_V2_UDP_DPORT);
  2244. sqp->ud_header.udp.sport = htons(MLX4_ROCEV2_QP1_SPORT);
  2245. sqp->ud_header.udp.csum = 0;
  2246. }
  2247. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  2248. if (!is_eth) {
  2249. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  2250. (sqp->ud_header.lrh.destination_lid ==
  2251. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  2252. (sqp->ud_header.lrh.service_level << 8));
  2253. if (ah->av.ib.port_pd & cpu_to_be32(0x80000000))
  2254. mlx->flags |= cpu_to_be32(0x1); /* force loopback */
  2255. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  2256. }
  2257. switch (wr->wr.opcode) {
  2258. case IB_WR_SEND:
  2259. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  2260. sqp->ud_header.immediate_present = 0;
  2261. break;
  2262. case IB_WR_SEND_WITH_IMM:
  2263. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  2264. sqp->ud_header.immediate_present = 1;
  2265. sqp->ud_header.immediate_data = wr->wr.ex.imm_data;
  2266. break;
  2267. default:
  2268. return -EINVAL;
  2269. }
  2270. if (is_eth) {
  2271. struct in6_addr in6;
  2272. u16 ether_type;
  2273. u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
  2274. ether_type = (!is_udp) ? MLX4_IB_IBOE_ETHERTYPE :
  2275. (ip_version == 4 ? ETH_P_IP : ETH_P_IPV6);
  2276. mlx->sched_prio = cpu_to_be16(pcp);
  2277. ether_addr_copy(sqp->ud_header.eth.smac_h, ah->av.eth.s_mac);
  2278. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  2279. memcpy(&ctrl->srcrb_flags16[0], ah->av.eth.mac, 2);
  2280. memcpy(&ctrl->imm, ah->av.eth.mac + 2, 4);
  2281. memcpy(&in6, sgid.raw, sizeof(in6));
  2282. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  2283. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  2284. if (!is_vlan) {
  2285. sqp->ud_header.eth.type = cpu_to_be16(ether_type);
  2286. } else {
  2287. sqp->ud_header.vlan.type = cpu_to_be16(ether_type);
  2288. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  2289. }
  2290. } else {
  2291. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 :
  2292. sl_to_vl(to_mdev(ib_dev),
  2293. sqp->ud_header.lrh.service_level,
  2294. sqp->qp.port);
  2295. if (sqp->qp.ibqp.qp_num && sqp->ud_header.lrh.virtual_lane == 15)
  2296. return -EINVAL;
  2297. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  2298. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  2299. }
  2300. sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
  2301. if (!sqp->qp.ibqp.qp_num)
  2302. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  2303. else
  2304. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->pkey_index, &pkey);
  2305. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  2306. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
  2307. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  2308. sqp->ud_header.deth.qkey = cpu_to_be32(wr->remote_qkey & 0x80000000 ?
  2309. sqp->qkey : wr->remote_qkey);
  2310. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  2311. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  2312. if (0) {
  2313. pr_err("built UD header of size %d:\n", header_size);
  2314. for (i = 0; i < header_size / 4; ++i) {
  2315. if (i % 8 == 0)
  2316. pr_err(" [%02x] ", i * 4);
  2317. pr_cont(" %08x",
  2318. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  2319. if ((i + 1) % 8 == 0)
  2320. pr_cont("\n");
  2321. }
  2322. pr_err("\n");
  2323. }
  2324. /*
  2325. * Inline data segments may not cross a 64 byte boundary. If
  2326. * our UD header is bigger than the space available up to the
  2327. * next 64 byte boundary in the WQE, use two inline data
  2328. * segments to hold the UD header.
  2329. */
  2330. spc = MLX4_INLINE_ALIGN -
  2331. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2332. if (header_size <= spc) {
  2333. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  2334. memcpy(inl + 1, sqp->header_buf, header_size);
  2335. i = 1;
  2336. } else {
  2337. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2338. memcpy(inl + 1, sqp->header_buf, spc);
  2339. inl = (void *) (inl + 1) + spc;
  2340. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  2341. /*
  2342. * Need a barrier here to make sure all the data is
  2343. * visible before the byte_count field is set.
  2344. * Otherwise the HCA prefetcher could grab the 64-byte
  2345. * chunk with this inline segment and get a valid (!=
  2346. * 0xffffffff) byte count but stale data, and end up
  2347. * generating a packet with bad headers.
  2348. *
  2349. * The first inline segment's byte_count field doesn't
  2350. * need a barrier, because it comes after a
  2351. * control/MLX segment and therefore is at an offset
  2352. * of 16 mod 64.
  2353. */
  2354. wmb();
  2355. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  2356. i = 2;
  2357. }
  2358. *mlx_seg_len =
  2359. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  2360. return 0;
  2361. }
  2362. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  2363. {
  2364. unsigned cur;
  2365. struct mlx4_ib_cq *cq;
  2366. cur = wq->head - wq->tail;
  2367. if (likely(cur + nreq < wq->max_post))
  2368. return 0;
  2369. cq = to_mcq(ib_cq);
  2370. spin_lock(&cq->lock);
  2371. cur = wq->head - wq->tail;
  2372. spin_unlock(&cq->lock);
  2373. return cur + nreq >= wq->max_post;
  2374. }
  2375. static __be32 convert_access(int acc)
  2376. {
  2377. return (acc & IB_ACCESS_REMOTE_ATOMIC ?
  2378. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC) : 0) |
  2379. (acc & IB_ACCESS_REMOTE_WRITE ?
  2380. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) |
  2381. (acc & IB_ACCESS_REMOTE_READ ?
  2382. cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ) : 0) |
  2383. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  2384. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  2385. }
  2386. static void set_reg_seg(struct mlx4_wqe_fmr_seg *fseg,
  2387. struct ib_reg_wr *wr)
  2388. {
  2389. struct mlx4_ib_mr *mr = to_mmr(wr->mr);
  2390. fseg->flags = convert_access(wr->access);
  2391. fseg->mem_key = cpu_to_be32(wr->key);
  2392. fseg->buf_list = cpu_to_be64(mr->page_map);
  2393. fseg->start_addr = cpu_to_be64(mr->ibmr.iova);
  2394. fseg->reg_len = cpu_to_be64(mr->ibmr.length);
  2395. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  2396. fseg->page_size = cpu_to_be32(ilog2(mr->ibmr.page_size));
  2397. fseg->reserved[0] = 0;
  2398. fseg->reserved[1] = 0;
  2399. }
  2400. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  2401. {
  2402. memset(iseg, 0, sizeof(*iseg));
  2403. iseg->mem_key = cpu_to_be32(rkey);
  2404. }
  2405. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  2406. u64 remote_addr, u32 rkey)
  2407. {
  2408. rseg->raddr = cpu_to_be64(remote_addr);
  2409. rseg->rkey = cpu_to_be32(rkey);
  2410. rseg->reserved = 0;
  2411. }
  2412. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg,
  2413. struct ib_atomic_wr *wr)
  2414. {
  2415. if (wr->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  2416. aseg->swap_add = cpu_to_be64(wr->swap);
  2417. aseg->compare = cpu_to_be64(wr->compare_add);
  2418. } else if (wr->wr.opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  2419. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2420. aseg->compare = cpu_to_be64(wr->compare_add_mask);
  2421. } else {
  2422. aseg->swap_add = cpu_to_be64(wr->compare_add);
  2423. aseg->compare = 0;
  2424. }
  2425. }
  2426. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  2427. struct ib_atomic_wr *wr)
  2428. {
  2429. aseg->swap_add = cpu_to_be64(wr->swap);
  2430. aseg->swap_add_mask = cpu_to_be64(wr->swap_mask);
  2431. aseg->compare = cpu_to_be64(wr->compare_add);
  2432. aseg->compare_mask = cpu_to_be64(wr->compare_add_mask);
  2433. }
  2434. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  2435. struct ib_ud_wr *wr)
  2436. {
  2437. memcpy(dseg->av, &to_mah(wr->ah)->av, sizeof (struct mlx4_av));
  2438. dseg->dqpn = cpu_to_be32(wr->remote_qpn);
  2439. dseg->qkey = cpu_to_be32(wr->remote_qkey);
  2440. dseg->vlan = to_mah(wr->ah)->av.eth.vlan;
  2441. memcpy(dseg->mac, to_mah(wr->ah)->av.eth.mac, 6);
  2442. }
  2443. static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev,
  2444. struct mlx4_wqe_datagram_seg *dseg,
  2445. struct ib_ud_wr *wr,
  2446. enum mlx4_ib_qp_type qpt)
  2447. {
  2448. union mlx4_ext_av *av = &to_mah(wr->ah)->av;
  2449. struct mlx4_av sqp_av = {0};
  2450. int port = *((u8 *) &av->ib.port_pd) & 0x3;
  2451. /* force loopback */
  2452. sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000);
  2453. sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */
  2454. sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel &
  2455. cpu_to_be32(0xf0000000);
  2456. memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av));
  2457. if (qpt == MLX4_IB_QPT_PROXY_GSI)
  2458. dseg->dqpn = cpu_to_be32(dev->dev->caps.qp1_tunnel[port - 1]);
  2459. else
  2460. dseg->dqpn = cpu_to_be32(dev->dev->caps.qp0_tunnel[port - 1]);
  2461. /* Use QKEY from the QP context, which is set by master */
  2462. dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY);
  2463. }
  2464. static void build_tunnel_header(struct ib_ud_wr *wr, void *wqe, unsigned *mlx_seg_len)
  2465. {
  2466. struct mlx4_wqe_inline_seg *inl = wqe;
  2467. struct mlx4_ib_tunnel_header hdr;
  2468. struct mlx4_ib_ah *ah = to_mah(wr->ah);
  2469. int spc;
  2470. int i;
  2471. memcpy(&hdr.av, &ah->av, sizeof hdr.av);
  2472. hdr.remote_qpn = cpu_to_be32(wr->remote_qpn);
  2473. hdr.pkey_index = cpu_to_be16(wr->pkey_index);
  2474. hdr.qkey = cpu_to_be32(wr->remote_qkey);
  2475. memcpy(hdr.mac, ah->av.eth.mac, 6);
  2476. hdr.vlan = ah->av.eth.vlan;
  2477. spc = MLX4_INLINE_ALIGN -
  2478. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  2479. if (sizeof (hdr) <= spc) {
  2480. memcpy(inl + 1, &hdr, sizeof (hdr));
  2481. wmb();
  2482. inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr));
  2483. i = 1;
  2484. } else {
  2485. memcpy(inl + 1, &hdr, spc);
  2486. wmb();
  2487. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  2488. inl = (void *) (inl + 1) + spc;
  2489. memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc);
  2490. wmb();
  2491. inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc));
  2492. i = 2;
  2493. }
  2494. *mlx_seg_len =
  2495. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16);
  2496. }
  2497. static void set_mlx_icrc_seg(void *dseg)
  2498. {
  2499. u32 *t = dseg;
  2500. struct mlx4_wqe_inline_seg *iseg = dseg;
  2501. t[1] = 0;
  2502. /*
  2503. * Need a barrier here before writing the byte_count field to
  2504. * make sure that all the data is visible before the
  2505. * byte_count field is set. Otherwise, if the segment begins
  2506. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2507. * chunk and get a valid (!= * 0xffffffff) byte count but
  2508. * stale data, and end up sending the wrong data.
  2509. */
  2510. wmb();
  2511. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  2512. }
  2513. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2514. {
  2515. dseg->lkey = cpu_to_be32(sg->lkey);
  2516. dseg->addr = cpu_to_be64(sg->addr);
  2517. /*
  2518. * Need a barrier here before writing the byte_count field to
  2519. * make sure that all the data is visible before the
  2520. * byte_count field is set. Otherwise, if the segment begins
  2521. * a new cacheline, the HCA prefetcher could grab the 64-byte
  2522. * chunk and get a valid (!= * 0xffffffff) byte count but
  2523. * stale data, and end up sending the wrong data.
  2524. */
  2525. wmb();
  2526. dseg->byte_count = cpu_to_be32(sg->length);
  2527. }
  2528. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  2529. {
  2530. dseg->byte_count = cpu_to_be32(sg->length);
  2531. dseg->lkey = cpu_to_be32(sg->lkey);
  2532. dseg->addr = cpu_to_be64(sg->addr);
  2533. }
  2534. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, struct ib_ud_wr *wr,
  2535. struct mlx4_ib_qp *qp, unsigned *lso_seg_len,
  2536. __be32 *lso_hdr_sz, __be32 *blh)
  2537. {
  2538. unsigned halign = ALIGN(sizeof *wqe + wr->hlen, 16);
  2539. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  2540. *blh = cpu_to_be32(1 << 6);
  2541. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  2542. wr->wr.num_sge > qp->sq.max_gs - (halign >> 4)))
  2543. return -EINVAL;
  2544. memcpy(wqe->header, wr->header, wr->hlen);
  2545. *lso_hdr_sz = cpu_to_be32(wr->mss << 16 | wr->hlen);
  2546. *lso_seg_len = halign;
  2547. return 0;
  2548. }
  2549. static __be32 send_ieth(struct ib_send_wr *wr)
  2550. {
  2551. switch (wr->opcode) {
  2552. case IB_WR_SEND_WITH_IMM:
  2553. case IB_WR_RDMA_WRITE_WITH_IMM:
  2554. return wr->ex.imm_data;
  2555. case IB_WR_SEND_WITH_INV:
  2556. return cpu_to_be32(wr->ex.invalidate_rkey);
  2557. default:
  2558. return 0;
  2559. }
  2560. }
  2561. static void add_zero_len_inline(void *wqe)
  2562. {
  2563. struct mlx4_wqe_inline_seg *inl = wqe;
  2564. memset(wqe, 0, 16);
  2565. inl->byte_count = cpu_to_be32(1 << 31);
  2566. }
  2567. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  2568. struct ib_send_wr **bad_wr)
  2569. {
  2570. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2571. void *wqe;
  2572. struct mlx4_wqe_ctrl_seg *ctrl;
  2573. struct mlx4_wqe_data_seg *dseg;
  2574. unsigned long flags;
  2575. int nreq;
  2576. int err = 0;
  2577. unsigned ind;
  2578. int uninitialized_var(stamp);
  2579. int uninitialized_var(size);
  2580. unsigned uninitialized_var(seglen);
  2581. __be32 dummy;
  2582. __be32 *lso_wqe;
  2583. __be32 uninitialized_var(lso_hdr_sz);
  2584. __be32 blh;
  2585. int i;
  2586. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  2587. if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
  2588. struct mlx4_ib_sqp *sqp = to_msqp(qp);
  2589. if (sqp->roce_v2_gsi) {
  2590. struct mlx4_ib_ah *ah = to_mah(ud_wr(wr)->ah);
  2591. struct ib_gid_attr gid_attr;
  2592. union ib_gid gid;
  2593. if (!ib_get_cached_gid(ibqp->device,
  2594. be32_to_cpu(ah->av.ib.port_pd) >> 24,
  2595. ah->av.ib.gid_index, &gid,
  2596. &gid_attr)) {
  2597. if (gid_attr.ndev)
  2598. dev_put(gid_attr.ndev);
  2599. qp = (gid_attr.gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
  2600. to_mqp(sqp->roce_v2_gsi) : qp;
  2601. } else {
  2602. pr_err("Failed to get gid at index %d. RoCEv2 will not work properly\n",
  2603. ah->av.ib.gid_index);
  2604. }
  2605. }
  2606. }
  2607. spin_lock_irqsave(&qp->sq.lock, flags);
  2608. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  2609. err = -EIO;
  2610. *bad_wr = wr;
  2611. nreq = 0;
  2612. goto out;
  2613. }
  2614. ind = qp->sq_next_wqe;
  2615. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  2616. lso_wqe = &dummy;
  2617. blh = 0;
  2618. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  2619. err = -ENOMEM;
  2620. *bad_wr = wr;
  2621. goto out;
  2622. }
  2623. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  2624. err = -EINVAL;
  2625. *bad_wr = wr;
  2626. goto out;
  2627. }
  2628. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  2629. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  2630. ctrl->srcrb_flags =
  2631. (wr->send_flags & IB_SEND_SIGNALED ?
  2632. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  2633. (wr->send_flags & IB_SEND_SOLICITED ?
  2634. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  2635. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  2636. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  2637. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  2638. qp->sq_signal_bits;
  2639. ctrl->imm = send_ieth(wr);
  2640. wqe += sizeof *ctrl;
  2641. size = sizeof *ctrl / 16;
  2642. switch (qp->mlx4_ib_qp_type) {
  2643. case MLX4_IB_QPT_RC:
  2644. case MLX4_IB_QPT_UC:
  2645. switch (wr->opcode) {
  2646. case IB_WR_ATOMIC_CMP_AND_SWP:
  2647. case IB_WR_ATOMIC_FETCH_AND_ADD:
  2648. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  2649. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  2650. atomic_wr(wr)->rkey);
  2651. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2652. set_atomic_seg(wqe, atomic_wr(wr));
  2653. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  2654. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  2655. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  2656. break;
  2657. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  2658. set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
  2659. atomic_wr(wr)->rkey);
  2660. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2661. set_masked_atomic_seg(wqe, atomic_wr(wr));
  2662. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  2663. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  2664. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  2665. break;
  2666. case IB_WR_RDMA_READ:
  2667. case IB_WR_RDMA_WRITE:
  2668. case IB_WR_RDMA_WRITE_WITH_IMM:
  2669. set_raddr_seg(wqe, rdma_wr(wr)->remote_addr,
  2670. rdma_wr(wr)->rkey);
  2671. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  2672. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  2673. break;
  2674. case IB_WR_LOCAL_INV:
  2675. ctrl->srcrb_flags |=
  2676. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2677. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  2678. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  2679. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  2680. break;
  2681. case IB_WR_REG_MR:
  2682. ctrl->srcrb_flags |=
  2683. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  2684. set_reg_seg(wqe, reg_wr(wr));
  2685. wqe += sizeof(struct mlx4_wqe_fmr_seg);
  2686. size += sizeof(struct mlx4_wqe_fmr_seg) / 16;
  2687. break;
  2688. default:
  2689. /* No extra segments required for sends */
  2690. break;
  2691. }
  2692. break;
  2693. case MLX4_IB_QPT_TUN_SMI_OWNER:
  2694. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  2695. ctrl, &seglen);
  2696. if (unlikely(err)) {
  2697. *bad_wr = wr;
  2698. goto out;
  2699. }
  2700. wqe += seglen;
  2701. size += seglen / 16;
  2702. break;
  2703. case MLX4_IB_QPT_TUN_SMI:
  2704. case MLX4_IB_QPT_TUN_GSI:
  2705. /* this is a UD qp used in MAD responses to slaves. */
  2706. set_datagram_seg(wqe, ud_wr(wr));
  2707. /* set the forced-loopback bit in the data seg av */
  2708. *(__be32 *) wqe |= cpu_to_be32(0x80000000);
  2709. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2710. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2711. break;
  2712. case MLX4_IB_QPT_UD:
  2713. set_datagram_seg(wqe, ud_wr(wr));
  2714. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2715. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2716. if (wr->opcode == IB_WR_LSO) {
  2717. err = build_lso_seg(wqe, ud_wr(wr), qp, &seglen,
  2718. &lso_hdr_sz, &blh);
  2719. if (unlikely(err)) {
  2720. *bad_wr = wr;
  2721. goto out;
  2722. }
  2723. lso_wqe = (__be32 *) wqe;
  2724. wqe += seglen;
  2725. size += seglen / 16;
  2726. }
  2727. break;
  2728. case MLX4_IB_QPT_PROXY_SMI_OWNER:
  2729. err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
  2730. ctrl, &seglen);
  2731. if (unlikely(err)) {
  2732. *bad_wr = wr;
  2733. goto out;
  2734. }
  2735. wqe += seglen;
  2736. size += seglen / 16;
  2737. /* to start tunnel header on a cache-line boundary */
  2738. add_zero_len_inline(wqe);
  2739. wqe += 16;
  2740. size++;
  2741. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  2742. wqe += seglen;
  2743. size += seglen / 16;
  2744. break;
  2745. case MLX4_IB_QPT_PROXY_SMI:
  2746. case MLX4_IB_QPT_PROXY_GSI:
  2747. /* If we are tunneling special qps, this is a UD qp.
  2748. * In this case we first add a UD segment targeting
  2749. * the tunnel qp, and then add a header with address
  2750. * information */
  2751. set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe,
  2752. ud_wr(wr),
  2753. qp->mlx4_ib_qp_type);
  2754. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  2755. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  2756. build_tunnel_header(ud_wr(wr), wqe, &seglen);
  2757. wqe += seglen;
  2758. size += seglen / 16;
  2759. break;
  2760. case MLX4_IB_QPT_SMI:
  2761. case MLX4_IB_QPT_GSI:
  2762. err = build_mlx_header(to_msqp(qp), ud_wr(wr), ctrl,
  2763. &seglen);
  2764. if (unlikely(err)) {
  2765. *bad_wr = wr;
  2766. goto out;
  2767. }
  2768. wqe += seglen;
  2769. size += seglen / 16;
  2770. break;
  2771. default:
  2772. break;
  2773. }
  2774. /*
  2775. * Write data segments in reverse order, so as to
  2776. * overwrite cacheline stamp last within each
  2777. * cacheline. This avoids issues with WQE
  2778. * prefetching.
  2779. */
  2780. dseg = wqe;
  2781. dseg += wr->num_sge - 1;
  2782. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  2783. /* Add one more inline data segment for ICRC for MLX sends */
  2784. if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
  2785. qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI ||
  2786. qp->mlx4_ib_qp_type &
  2787. (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) {
  2788. set_mlx_icrc_seg(dseg + 1);
  2789. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  2790. }
  2791. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  2792. set_data_seg(dseg, wr->sg_list + i);
  2793. /*
  2794. * Possibly overwrite stamping in cacheline with LSO
  2795. * segment only after making sure all data segments
  2796. * are written.
  2797. */
  2798. wmb();
  2799. *lso_wqe = lso_hdr_sz;
  2800. ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ?
  2801. MLX4_WQE_CTRL_FENCE : 0) | size;
  2802. /*
  2803. * Make sure descriptor is fully written before
  2804. * setting ownership bit (because HW can start
  2805. * executing as soon as we do).
  2806. */
  2807. wmb();
  2808. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  2809. *bad_wr = wr;
  2810. err = -EINVAL;
  2811. goto out;
  2812. }
  2813. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  2814. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  2815. stamp = ind + qp->sq_spare_wqes;
  2816. ind += DIV_ROUND_UP(size * 16, 1U << qp->sq.wqe_shift);
  2817. /*
  2818. * We can improve latency by not stamping the last
  2819. * send queue WQE until after ringing the doorbell, so
  2820. * only stamp here if there are still more WQEs to post.
  2821. *
  2822. * Same optimization applies to padding with NOP wqe
  2823. * in case of WQE shrinking (used to prevent wrap-around
  2824. * in the middle of WR).
  2825. */
  2826. if (wr->next) {
  2827. stamp_send_wqe(qp, stamp, size * 16);
  2828. ind = pad_wraparound(qp, ind);
  2829. }
  2830. }
  2831. out:
  2832. if (likely(nreq)) {
  2833. qp->sq.head += nreq;
  2834. /*
  2835. * Make sure that descriptors are written before
  2836. * doorbell record.
  2837. */
  2838. wmb();
  2839. writel(qp->doorbell_qpn,
  2840. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  2841. /*
  2842. * Make sure doorbells don't leak out of SQ spinlock
  2843. * and reach the HCA out of order.
  2844. */
  2845. mmiowb();
  2846. stamp_send_wqe(qp, stamp, size * 16);
  2847. ind = pad_wraparound(qp, ind);
  2848. qp->sq_next_wqe = ind;
  2849. }
  2850. spin_unlock_irqrestore(&qp->sq.lock, flags);
  2851. return err;
  2852. }
  2853. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  2854. struct ib_recv_wr **bad_wr)
  2855. {
  2856. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2857. struct mlx4_wqe_data_seg *scat;
  2858. unsigned long flags;
  2859. int err = 0;
  2860. int nreq;
  2861. int ind;
  2862. int max_gs;
  2863. int i;
  2864. struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
  2865. max_gs = qp->rq.max_gs;
  2866. spin_lock_irqsave(&qp->rq.lock, flags);
  2867. if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR) {
  2868. err = -EIO;
  2869. *bad_wr = wr;
  2870. nreq = 0;
  2871. goto out;
  2872. }
  2873. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  2874. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  2875. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  2876. err = -ENOMEM;
  2877. *bad_wr = wr;
  2878. goto out;
  2879. }
  2880. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  2881. err = -EINVAL;
  2882. *bad_wr = wr;
  2883. goto out;
  2884. }
  2885. scat = get_recv_wqe(qp, ind);
  2886. if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
  2887. MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
  2888. ib_dma_sync_single_for_device(ibqp->device,
  2889. qp->sqp_proxy_rcv[ind].map,
  2890. sizeof (struct mlx4_ib_proxy_sqp_hdr),
  2891. DMA_FROM_DEVICE);
  2892. scat->byte_count =
  2893. cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr));
  2894. /* use dma lkey from upper layer entry */
  2895. scat->lkey = cpu_to_be32(wr->sg_list->lkey);
  2896. scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map);
  2897. scat++;
  2898. max_gs--;
  2899. }
  2900. for (i = 0; i < wr->num_sge; ++i)
  2901. __set_data_seg(scat + i, wr->sg_list + i);
  2902. if (i < max_gs) {
  2903. scat[i].byte_count = 0;
  2904. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  2905. scat[i].addr = 0;
  2906. }
  2907. qp->rq.wrid[ind] = wr->wr_id;
  2908. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  2909. }
  2910. out:
  2911. if (likely(nreq)) {
  2912. qp->rq.head += nreq;
  2913. /*
  2914. * Make sure that descriptors are written before
  2915. * doorbell record.
  2916. */
  2917. wmb();
  2918. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  2919. }
  2920. spin_unlock_irqrestore(&qp->rq.lock, flags);
  2921. return err;
  2922. }
  2923. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  2924. {
  2925. switch (mlx4_state) {
  2926. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  2927. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  2928. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  2929. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  2930. case MLX4_QP_STATE_SQ_DRAINING:
  2931. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  2932. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  2933. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  2934. default: return -1;
  2935. }
  2936. }
  2937. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  2938. {
  2939. switch (mlx4_mig_state) {
  2940. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  2941. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  2942. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  2943. default: return -1;
  2944. }
  2945. }
  2946. static int to_ib_qp_access_flags(int mlx4_flags)
  2947. {
  2948. int ib_flags = 0;
  2949. if (mlx4_flags & MLX4_QP_BIT_RRE)
  2950. ib_flags |= IB_ACCESS_REMOTE_READ;
  2951. if (mlx4_flags & MLX4_QP_BIT_RWE)
  2952. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  2953. if (mlx4_flags & MLX4_QP_BIT_RAE)
  2954. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  2955. return ib_flags;
  2956. }
  2957. static void to_ib_ah_attr(struct mlx4_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
  2958. struct mlx4_qp_path *path)
  2959. {
  2960. struct mlx4_dev *dev = ibdev->dev;
  2961. int is_eth;
  2962. memset(ib_ah_attr, 0, sizeof *ib_ah_attr);
  2963. ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
  2964. if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
  2965. return;
  2966. is_eth = rdma_port_get_link_layer(&ibdev->ib_dev, ib_ah_attr->port_num) ==
  2967. IB_LINK_LAYER_ETHERNET;
  2968. if (is_eth)
  2969. ib_ah_attr->sl = ((path->sched_queue >> 3) & 0x7) |
  2970. ((path->sched_queue & 4) << 1);
  2971. else
  2972. ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
  2973. ib_ah_attr->dlid = be16_to_cpu(path->rlid);
  2974. ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
  2975. ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
  2976. ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
  2977. if (ib_ah_attr->ah_flags) {
  2978. ib_ah_attr->grh.sgid_index = path->mgid_index;
  2979. ib_ah_attr->grh.hop_limit = path->hop_limit;
  2980. ib_ah_attr->grh.traffic_class =
  2981. (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
  2982. ib_ah_attr->grh.flow_label =
  2983. be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
  2984. memcpy(ib_ah_attr->grh.dgid.raw,
  2985. path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
  2986. }
  2987. }
  2988. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  2989. struct ib_qp_init_attr *qp_init_attr)
  2990. {
  2991. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  2992. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  2993. struct mlx4_qp_context context;
  2994. int mlx4_state;
  2995. int err = 0;
  2996. mutex_lock(&qp->mutex);
  2997. if (qp->state == IB_QPS_RESET) {
  2998. qp_attr->qp_state = IB_QPS_RESET;
  2999. goto done;
  3000. }
  3001. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  3002. if (err) {
  3003. err = -EINVAL;
  3004. goto out;
  3005. }
  3006. mlx4_state = be32_to_cpu(context.flags) >> 28;
  3007. qp->state = to_ib_qp_state(mlx4_state);
  3008. qp_attr->qp_state = qp->state;
  3009. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  3010. qp_attr->path_mig_state =
  3011. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  3012. qp_attr->qkey = be32_to_cpu(context.qkey);
  3013. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  3014. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  3015. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  3016. qp_attr->qp_access_flags =
  3017. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  3018. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  3019. to_ib_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  3020. to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  3021. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  3022. qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
  3023. }
  3024. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  3025. if (qp_attr->qp_state == IB_QPS_INIT)
  3026. qp_attr->port_num = qp->port;
  3027. else
  3028. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  3029. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  3030. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  3031. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  3032. qp_attr->max_dest_rd_atomic =
  3033. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  3034. qp_attr->min_rnr_timer =
  3035. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  3036. qp_attr->timeout = context.pri_path.ackto >> 3;
  3037. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  3038. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  3039. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  3040. done:
  3041. qp_attr->cur_qp_state = qp_attr->qp_state;
  3042. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  3043. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  3044. if (!ibqp->uobject) {
  3045. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  3046. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  3047. } else {
  3048. qp_attr->cap.max_send_wr = 0;
  3049. qp_attr->cap.max_send_sge = 0;
  3050. }
  3051. /*
  3052. * We don't support inline sends for kernel QPs (yet), and we
  3053. * don't know what userspace's value should be.
  3054. */
  3055. qp_attr->cap.max_inline_data = 0;
  3056. qp_init_attr->cap = qp_attr->cap;
  3057. qp_init_attr->create_flags = 0;
  3058. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  3059. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  3060. if (qp->flags & MLX4_IB_QP_LSO)
  3061. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  3062. if (qp->flags & MLX4_IB_QP_NETIF)
  3063. qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP;
  3064. qp_init_attr->sq_sig_type =
  3065. qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ?
  3066. IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  3067. out:
  3068. mutex_unlock(&qp->mutex);
  3069. return err;
  3070. }