qp.c 79 KB

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