qp.c 89 KB

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