qp.c 93 KB

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