qp.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127
  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 <linux/mlx4/qp.h>
  40. #include "mlx4_ib.h"
  41. #include "user.h"
  42. enum {
  43. MLX4_IB_ACK_REQ_FREQ = 8,
  44. };
  45. enum {
  46. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  47. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f,
  48. MLX4_IB_LINK_TYPE_IB = 0,
  49. MLX4_IB_LINK_TYPE_ETH = 1
  50. };
  51. enum {
  52. /*
  53. * Largest possible UD header: send with GRH and immediate
  54. * data plus 18 bytes for an Ethernet header with VLAN/802.1Q
  55. * tag. (LRH would only use 8 bytes, so Ethernet is the
  56. * biggest case)
  57. */
  58. MLX4_IB_UD_HEADER_SIZE = 82,
  59. MLX4_IB_LSO_HEADER_SPARE = 128,
  60. };
  61. enum {
  62. MLX4_IB_IBOE_ETHERTYPE = 0x8915
  63. };
  64. struct mlx4_ib_sqp {
  65. struct mlx4_ib_qp qp;
  66. int pkey_index;
  67. u32 qkey;
  68. u32 send_psn;
  69. struct ib_ud_header ud_header;
  70. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  71. };
  72. enum {
  73. MLX4_IB_MIN_SQ_STRIDE = 6,
  74. MLX4_IB_CACHE_LINE_SIZE = 64,
  75. };
  76. static const __be32 mlx4_ib_opcode[] = {
  77. [IB_WR_SEND] = cpu_to_be32(MLX4_OPCODE_SEND),
  78. [IB_WR_LSO] = cpu_to_be32(MLX4_OPCODE_LSO),
  79. [IB_WR_SEND_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  80. [IB_WR_RDMA_WRITE] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  81. [IB_WR_RDMA_WRITE_WITH_IMM] = cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  82. [IB_WR_RDMA_READ] = cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  83. [IB_WR_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  84. [IB_WR_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  85. [IB_WR_SEND_WITH_INV] = cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
  86. [IB_WR_LOCAL_INV] = cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
  87. [IB_WR_FAST_REG_MR] = cpu_to_be32(MLX4_OPCODE_FMR),
  88. [IB_WR_MASKED_ATOMIC_CMP_AND_SWP] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
  89. [IB_WR_MASKED_ATOMIC_FETCH_AND_ADD] = cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
  90. };
  91. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  92. {
  93. return container_of(mqp, struct mlx4_ib_sqp, qp);
  94. }
  95. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  96. {
  97. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  98. qp->mqp.qpn <= dev->dev->caps.sqp_start + 3;
  99. }
  100. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  101. {
  102. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  103. qp->mqp.qpn <= dev->dev->caps.sqp_start + 1;
  104. }
  105. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  106. {
  107. return mlx4_buf_offset(&qp->buf, offset);
  108. }
  109. static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
  110. {
  111. return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
  112. }
  113. static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
  114. {
  115. return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
  116. }
  117. /*
  118. * Stamp a SQ WQE so that it is invalid if prefetched by marking the
  119. * first four bytes of every 64 byte chunk with
  120. * 0x7FFFFFF | (invalid_ownership_value << 31).
  121. *
  122. * When the max work request size is less than or equal to the WQE
  123. * basic block size, as an optimization, we can stamp all WQEs with
  124. * 0xffffffff, and skip the very first chunk of each WQE.
  125. */
  126. static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n, int size)
  127. {
  128. __be32 *wqe;
  129. int i;
  130. int s;
  131. int ind;
  132. void *buf;
  133. __be32 stamp;
  134. struct mlx4_wqe_ctrl_seg *ctrl;
  135. if (qp->sq_max_wqes_per_wr > 1) {
  136. s = roundup(size, 1U << qp->sq.wqe_shift);
  137. for (i = 0; i < s; i += 64) {
  138. ind = (i >> qp->sq.wqe_shift) + n;
  139. stamp = ind & qp->sq.wqe_cnt ? cpu_to_be32(0x7fffffff) :
  140. cpu_to_be32(0xffffffff);
  141. buf = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  142. wqe = buf + (i & ((1 << qp->sq.wqe_shift) - 1));
  143. *wqe = stamp;
  144. }
  145. } else {
  146. ctrl = buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  147. s = (ctrl->fence_size & 0x3f) << 4;
  148. for (i = 64; i < s; i += 64) {
  149. wqe = buf + i;
  150. *wqe = cpu_to_be32(0xffffffff);
  151. }
  152. }
  153. }
  154. static void post_nop_wqe(struct mlx4_ib_qp *qp, int n, int size)
  155. {
  156. struct mlx4_wqe_ctrl_seg *ctrl;
  157. struct mlx4_wqe_inline_seg *inl;
  158. void *wqe;
  159. int s;
  160. ctrl = wqe = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
  161. s = sizeof(struct mlx4_wqe_ctrl_seg);
  162. if (qp->ibqp.qp_type == IB_QPT_UD) {
  163. struct mlx4_wqe_datagram_seg *dgram = wqe + sizeof *ctrl;
  164. struct mlx4_av *av = (struct mlx4_av *)dgram->av;
  165. memset(dgram, 0, sizeof *dgram);
  166. av->port_pd = cpu_to_be32((qp->port << 24) | to_mpd(qp->ibqp.pd)->pdn);
  167. s += sizeof(struct mlx4_wqe_datagram_seg);
  168. }
  169. /* Pad the remainder of the WQE with an inline data segment. */
  170. if (size > s) {
  171. inl = wqe + s;
  172. inl->byte_count = cpu_to_be32(1 << 31 | (size - s - sizeof *inl));
  173. }
  174. ctrl->srcrb_flags = 0;
  175. ctrl->fence_size = size / 16;
  176. /*
  177. * Make sure descriptor is fully written before setting ownership bit
  178. * (because HW can start executing as soon as we do).
  179. */
  180. wmb();
  181. ctrl->owner_opcode = cpu_to_be32(MLX4_OPCODE_NOP | MLX4_WQE_CTRL_NEC) |
  182. (n & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0);
  183. stamp_send_wqe(qp, n + qp->sq_spare_wqes, size);
  184. }
  185. /* Post NOP WQE to prevent wrap-around in the middle of WR */
  186. static inline unsigned pad_wraparound(struct mlx4_ib_qp *qp, int ind)
  187. {
  188. unsigned s = qp->sq.wqe_cnt - (ind & (qp->sq.wqe_cnt - 1));
  189. if (unlikely(s < qp->sq_max_wqes_per_wr)) {
  190. post_nop_wqe(qp, ind, s << qp->sq.wqe_shift);
  191. ind += s;
  192. }
  193. return ind;
  194. }
  195. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  196. {
  197. struct ib_event event;
  198. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  199. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  200. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  201. if (ibqp->event_handler) {
  202. event.device = ibqp->device;
  203. event.element.qp = ibqp;
  204. switch (type) {
  205. case MLX4_EVENT_TYPE_PATH_MIG:
  206. event.event = IB_EVENT_PATH_MIG;
  207. break;
  208. case MLX4_EVENT_TYPE_COMM_EST:
  209. event.event = IB_EVENT_COMM_EST;
  210. break;
  211. case MLX4_EVENT_TYPE_SQ_DRAINED:
  212. event.event = IB_EVENT_SQ_DRAINED;
  213. break;
  214. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  215. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  216. break;
  217. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  218. event.event = IB_EVENT_QP_FATAL;
  219. break;
  220. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  221. event.event = IB_EVENT_PATH_MIG_ERR;
  222. break;
  223. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  224. event.event = IB_EVENT_QP_REQ_ERR;
  225. break;
  226. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  227. event.event = IB_EVENT_QP_ACCESS_ERR;
  228. break;
  229. default:
  230. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  231. "on QP %06x\n", type, qp->qpn);
  232. return;
  233. }
  234. ibqp->event_handler(&event, ibqp->qp_context);
  235. }
  236. }
  237. static int send_wqe_overhead(enum ib_qp_type type, u32 flags)
  238. {
  239. /*
  240. * UD WQEs must have a datagram segment.
  241. * RC and UC WQEs might have a remote address segment.
  242. * MLX WQEs need two extra inline data segments (for the UD
  243. * header and space for the ICRC).
  244. */
  245. switch (type) {
  246. case IB_QPT_UD:
  247. return sizeof (struct mlx4_wqe_ctrl_seg) +
  248. sizeof (struct mlx4_wqe_datagram_seg) +
  249. ((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
  250. case IB_QPT_UC:
  251. return sizeof (struct mlx4_wqe_ctrl_seg) +
  252. sizeof (struct mlx4_wqe_raddr_seg);
  253. case IB_QPT_RC:
  254. return sizeof (struct mlx4_wqe_ctrl_seg) +
  255. sizeof (struct mlx4_wqe_atomic_seg) +
  256. sizeof (struct mlx4_wqe_raddr_seg);
  257. case IB_QPT_SMI:
  258. case IB_QPT_GSI:
  259. return sizeof (struct mlx4_wqe_ctrl_seg) +
  260. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  261. DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
  262. MLX4_INLINE_ALIGN) *
  263. sizeof (struct mlx4_wqe_inline_seg),
  264. sizeof (struct mlx4_wqe_data_seg)) +
  265. ALIGN(4 +
  266. sizeof (struct mlx4_wqe_inline_seg),
  267. sizeof (struct mlx4_wqe_data_seg));
  268. default:
  269. return sizeof (struct mlx4_wqe_ctrl_seg);
  270. }
  271. }
  272. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  273. int is_user, int has_srq, struct mlx4_ib_qp *qp)
  274. {
  275. /* Sanity check RQ size before proceeding */
  276. if (cap->max_recv_wr > dev->dev->caps.max_wqes ||
  277. cap->max_recv_sge > dev->dev->caps.max_rq_sg)
  278. return -EINVAL;
  279. if (has_srq) {
  280. /* QPs attached to an SRQ should have no RQ */
  281. if (cap->max_recv_wr)
  282. return -EINVAL;
  283. qp->rq.wqe_cnt = qp->rq.max_gs = 0;
  284. } else {
  285. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  286. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
  287. return -EINVAL;
  288. qp->rq.wqe_cnt = roundup_pow_of_two(max(1U, cap->max_recv_wr));
  289. qp->rq.max_gs = roundup_pow_of_two(max(1U, cap->max_recv_sge));
  290. qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
  291. }
  292. cap->max_recv_wr = qp->rq.max_post = qp->rq.wqe_cnt;
  293. cap->max_recv_sge = qp->rq.max_gs;
  294. return 0;
  295. }
  296. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  297. enum ib_qp_type type, struct mlx4_ib_qp *qp)
  298. {
  299. int s;
  300. /* Sanity check SQ size before proceeding */
  301. if (cap->max_send_wr > dev->dev->caps.max_wqes ||
  302. cap->max_send_sge > dev->dev->caps.max_sq_sg ||
  303. cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
  304. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  305. return -EINVAL;
  306. /*
  307. * For MLX transport we need 2 extra S/G entries:
  308. * one for the header and one for the checksum at the end
  309. */
  310. if ((type == IB_QPT_SMI || type == IB_QPT_GSI) &&
  311. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  312. return -EINVAL;
  313. s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
  314. cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
  315. send_wqe_overhead(type, qp->flags);
  316. if (s > dev->dev->caps.max_sq_desc_sz)
  317. return -EINVAL;
  318. /*
  319. * Hermon supports shrinking WQEs, such that a single work
  320. * request can include multiple units of 1 << wqe_shift. This
  321. * way, work requests can differ in size, and do not have to
  322. * be a power of 2 in size, saving memory and speeding up send
  323. * WR posting. Unfortunately, if we do this then the
  324. * wqe_index field in CQEs can't be used to look up the WR ID
  325. * anymore, so we do this only if selective signaling is off.
  326. *
  327. * Further, on 32-bit platforms, we can't use vmap() to make
  328. * the QP buffer virtually contiguous. Thus we have to use
  329. * constant-sized WRs to make sure a WR is always fully within
  330. * a single page-sized chunk.
  331. *
  332. * Finally, we use NOP work requests to pad the end of the
  333. * work queue, to avoid wrap-around in the middle of WR. We
  334. * set NEC bit to avoid getting completions with error for
  335. * these NOP WRs, but since NEC is only supported starting
  336. * with firmware 2.2.232, we use constant-sized WRs for older
  337. * firmware.
  338. *
  339. * And, since MLX QPs only support SEND, we use constant-sized
  340. * WRs in this case.
  341. *
  342. * We look for the smallest value of wqe_shift such that the
  343. * resulting number of wqes does not exceed device
  344. * capabilities.
  345. *
  346. * We set WQE size to at least 64 bytes, this way stamping
  347. * invalidates each WQE.
  348. */
  349. if (dev->dev->caps.fw_ver >= MLX4_FW_VER_WQE_CTRL_NEC &&
  350. qp->sq_signal_bits && BITS_PER_LONG == 64 &&
  351. type != IB_QPT_SMI && type != IB_QPT_GSI)
  352. qp->sq.wqe_shift = ilog2(64);
  353. else
  354. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
  355. for (;;) {
  356. qp->sq_max_wqes_per_wr = DIV_ROUND_UP(s, 1U << qp->sq.wqe_shift);
  357. /*
  358. * We need to leave 2 KB + 1 WR of headroom in the SQ to
  359. * allow HW to prefetch.
  360. */
  361. qp->sq_spare_wqes = (2048 >> qp->sq.wqe_shift) + qp->sq_max_wqes_per_wr;
  362. qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr *
  363. qp->sq_max_wqes_per_wr +
  364. qp->sq_spare_wqes);
  365. if (qp->sq.wqe_cnt <= dev->dev->caps.max_wqes)
  366. break;
  367. if (qp->sq_max_wqes_per_wr <= 1)
  368. return -EINVAL;
  369. ++qp->sq.wqe_shift;
  370. }
  371. qp->sq.max_gs = (min(dev->dev->caps.max_sq_desc_sz,
  372. (qp->sq_max_wqes_per_wr << qp->sq.wqe_shift)) -
  373. send_wqe_overhead(type, qp->flags)) /
  374. sizeof (struct mlx4_wqe_data_seg);
  375. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  376. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  377. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  378. qp->rq.offset = 0;
  379. qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
  380. } else {
  381. qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
  382. qp->sq.offset = 0;
  383. }
  384. cap->max_send_wr = qp->sq.max_post =
  385. (qp->sq.wqe_cnt - qp->sq_spare_wqes) / qp->sq_max_wqes_per_wr;
  386. cap->max_send_sge = min(qp->sq.max_gs,
  387. min(dev->dev->caps.max_sq_sg,
  388. dev->dev->caps.max_rq_sg));
  389. /* We don't support inline sends for kernel QPs (yet) */
  390. cap->max_inline_data = 0;
  391. return 0;
  392. }
  393. static int set_user_sq_size(struct mlx4_ib_dev *dev,
  394. struct mlx4_ib_qp *qp,
  395. struct mlx4_ib_create_qp *ucmd)
  396. {
  397. /* Sanity check SQ size before proceeding */
  398. if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes ||
  399. ucmd->log_sq_stride >
  400. ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
  401. ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
  402. return -EINVAL;
  403. qp->sq.wqe_cnt = 1 << ucmd->log_sq_bb_count;
  404. qp->sq.wqe_shift = ucmd->log_sq_stride;
  405. qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
  406. (qp->sq.wqe_cnt << qp->sq.wqe_shift);
  407. return 0;
  408. }
  409. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  410. struct ib_qp_init_attr *init_attr,
  411. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp *qp)
  412. {
  413. int qpn;
  414. int err;
  415. mutex_init(&qp->mutex);
  416. spin_lock_init(&qp->sq.lock);
  417. spin_lock_init(&qp->rq.lock);
  418. INIT_LIST_HEAD(&qp->gid_list);
  419. qp->state = IB_QPS_RESET;
  420. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  421. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  422. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, !!init_attr->srq, qp);
  423. if (err)
  424. goto err;
  425. if (pd->uobject) {
  426. struct mlx4_ib_create_qp ucmd;
  427. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  428. err = -EFAULT;
  429. goto err;
  430. }
  431. qp->sq_no_prefetch = ucmd.sq_no_prefetch;
  432. err = set_user_sq_size(dev, qp, &ucmd);
  433. if (err)
  434. goto err;
  435. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  436. qp->buf_size, 0, 0);
  437. if (IS_ERR(qp->umem)) {
  438. err = PTR_ERR(qp->umem);
  439. goto err;
  440. }
  441. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  442. ilog2(qp->umem->page_size), &qp->mtt);
  443. if (err)
  444. goto err_buf;
  445. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  446. if (err)
  447. goto err_mtt;
  448. if (!init_attr->srq) {
  449. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  450. ucmd.db_addr, &qp->db);
  451. if (err)
  452. goto err_mtt;
  453. }
  454. } else {
  455. qp->sq_no_prefetch = 0;
  456. if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
  457. qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
  458. if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
  459. qp->flags |= MLX4_IB_QP_LSO;
  460. err = set_kernel_sq_size(dev, &init_attr->cap, init_attr->qp_type, qp);
  461. if (err)
  462. goto err;
  463. if (!init_attr->srq) {
  464. err = mlx4_db_alloc(dev->dev, &qp->db, 0);
  465. if (err)
  466. goto err;
  467. *qp->db.db = 0;
  468. }
  469. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
  470. err = -ENOMEM;
  471. goto err_db;
  472. }
  473. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  474. &qp->mtt);
  475. if (err)
  476. goto err_buf;
  477. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  478. if (err)
  479. goto err_mtt;
  480. qp->sq.wrid = kmalloc(qp->sq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  481. qp->rq.wrid = kmalloc(qp->rq.wqe_cnt * sizeof (u64), GFP_KERNEL);
  482. if (!qp->sq.wrid || !qp->rq.wrid) {
  483. err = -ENOMEM;
  484. goto err_wrid;
  485. }
  486. }
  487. if (sqpn) {
  488. qpn = sqpn;
  489. } else {
  490. err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn);
  491. if (err)
  492. goto err_wrid;
  493. }
  494. err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
  495. if (err)
  496. goto err_qpn;
  497. /*
  498. * Hardware wants QPN written in big-endian order (after
  499. * shifting) for send doorbell. Precompute this value to save
  500. * a little bit when posting sends.
  501. */
  502. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  503. qp->mqp.event = mlx4_ib_qp_event;
  504. return 0;
  505. err_qpn:
  506. if (!sqpn)
  507. mlx4_qp_release_range(dev->dev, qpn, 1);
  508. err_wrid:
  509. if (pd->uobject) {
  510. if (!init_attr->srq)
  511. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context),
  512. &qp->db);
  513. } else {
  514. kfree(qp->sq.wrid);
  515. kfree(qp->rq.wrid);
  516. }
  517. err_mtt:
  518. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  519. err_buf:
  520. if (pd->uobject)
  521. ib_umem_release(qp->umem);
  522. else
  523. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  524. err_db:
  525. if (!pd->uobject && !init_attr->srq)
  526. mlx4_db_free(dev->dev, &qp->db);
  527. err:
  528. return err;
  529. }
  530. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  531. {
  532. switch (state) {
  533. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  534. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  535. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  536. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  537. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  538. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  539. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  540. default: return -1;
  541. }
  542. }
  543. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  544. __acquires(&send_cq->lock) __acquires(&recv_cq->lock)
  545. {
  546. if (send_cq == recv_cq) {
  547. spin_lock_irq(&send_cq->lock);
  548. __acquire(&recv_cq->lock);
  549. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  550. spin_lock_irq(&send_cq->lock);
  551. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  552. } else {
  553. spin_lock_irq(&recv_cq->lock);
  554. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  555. }
  556. }
  557. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  558. __releases(&send_cq->lock) __releases(&recv_cq->lock)
  559. {
  560. if (send_cq == recv_cq) {
  561. __release(&recv_cq->lock);
  562. spin_unlock_irq(&send_cq->lock);
  563. } else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  564. spin_unlock(&recv_cq->lock);
  565. spin_unlock_irq(&send_cq->lock);
  566. } else {
  567. spin_unlock(&send_cq->lock);
  568. spin_unlock_irq(&recv_cq->lock);
  569. }
  570. }
  571. static void del_gid_entries(struct mlx4_ib_qp *qp)
  572. {
  573. struct mlx4_ib_gid_entry *ge, *tmp;
  574. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  575. list_del(&ge->list);
  576. kfree(ge);
  577. }
  578. }
  579. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  580. int is_user)
  581. {
  582. struct mlx4_ib_cq *send_cq, *recv_cq;
  583. if (qp->state != IB_QPS_RESET)
  584. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  585. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  586. printk(KERN_WARNING "mlx4_ib: modify QP %06x to RESET failed.\n",
  587. qp->mqp.qpn);
  588. send_cq = to_mcq(qp->ibqp.send_cq);
  589. recv_cq = to_mcq(qp->ibqp.recv_cq);
  590. mlx4_ib_lock_cqs(send_cq, recv_cq);
  591. if (!is_user) {
  592. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  593. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  594. if (send_cq != recv_cq)
  595. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  596. }
  597. mlx4_qp_remove(dev->dev, &qp->mqp);
  598. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  599. mlx4_qp_free(dev->dev, &qp->mqp);
  600. if (!is_sqp(dev, qp))
  601. mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
  602. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  603. if (is_user) {
  604. if (!qp->ibqp.srq)
  605. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  606. &qp->db);
  607. ib_umem_release(qp->umem);
  608. } else {
  609. kfree(qp->sq.wrid);
  610. kfree(qp->rq.wrid);
  611. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  612. if (!qp->ibqp.srq)
  613. mlx4_db_free(dev->dev, &qp->db);
  614. }
  615. del_gid_entries(qp);
  616. }
  617. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  618. struct ib_qp_init_attr *init_attr,
  619. struct ib_udata *udata)
  620. {
  621. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  622. struct mlx4_ib_sqp *sqp;
  623. struct mlx4_ib_qp *qp;
  624. int err;
  625. /*
  626. * We only support LSO and multicast loopback blocking, and
  627. * only for kernel UD QPs.
  628. */
  629. if (init_attr->create_flags & ~(IB_QP_CREATE_IPOIB_UD_LSO |
  630. IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK))
  631. return ERR_PTR(-EINVAL);
  632. if (init_attr->create_flags &&
  633. (pd->uobject || init_attr->qp_type != IB_QPT_UD))
  634. return ERR_PTR(-EINVAL);
  635. switch (init_attr->qp_type) {
  636. case IB_QPT_RC:
  637. case IB_QPT_UC:
  638. case IB_QPT_UD:
  639. {
  640. qp = kzalloc(sizeof *qp, GFP_KERNEL);
  641. if (!qp)
  642. return ERR_PTR(-ENOMEM);
  643. err = create_qp_common(dev, pd, init_attr, udata, 0, qp);
  644. if (err) {
  645. kfree(qp);
  646. return ERR_PTR(err);
  647. }
  648. qp->ibqp.qp_num = qp->mqp.qpn;
  649. break;
  650. }
  651. case IB_QPT_SMI:
  652. case IB_QPT_GSI:
  653. {
  654. /* Userspace is not allowed to create special QPs: */
  655. if (pd->uobject)
  656. return ERR_PTR(-EINVAL);
  657. sqp = kzalloc(sizeof *sqp, GFP_KERNEL);
  658. if (!sqp)
  659. return ERR_PTR(-ENOMEM);
  660. qp = &sqp->qp;
  661. err = create_qp_common(dev, pd, init_attr, udata,
  662. dev->dev->caps.sqp_start +
  663. (init_attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  664. init_attr->port_num - 1,
  665. qp);
  666. if (err) {
  667. kfree(sqp);
  668. return ERR_PTR(err);
  669. }
  670. qp->port = init_attr->port_num;
  671. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  672. break;
  673. }
  674. default:
  675. /* Don't support raw QPs */
  676. return ERR_PTR(-EINVAL);
  677. }
  678. return &qp->ibqp;
  679. }
  680. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  681. {
  682. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  683. struct mlx4_ib_qp *mqp = to_mqp(qp);
  684. if (is_qp0(dev, mqp))
  685. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  686. destroy_qp_common(dev, mqp, !!qp->pd->uobject);
  687. if (is_sqp(dev, mqp))
  688. kfree(to_msqp(mqp));
  689. else
  690. kfree(mqp);
  691. return 0;
  692. }
  693. static int to_mlx4_st(enum ib_qp_type type)
  694. {
  695. switch (type) {
  696. case IB_QPT_RC: return MLX4_QP_ST_RC;
  697. case IB_QPT_UC: return MLX4_QP_ST_UC;
  698. case IB_QPT_UD: return MLX4_QP_ST_UD;
  699. case IB_QPT_SMI:
  700. case IB_QPT_GSI: return MLX4_QP_ST_MLX;
  701. default: return -1;
  702. }
  703. }
  704. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  705. int attr_mask)
  706. {
  707. u8 dest_rd_atomic;
  708. u32 access_flags;
  709. u32 hw_access_flags = 0;
  710. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  711. dest_rd_atomic = attr->max_dest_rd_atomic;
  712. else
  713. dest_rd_atomic = qp->resp_depth;
  714. if (attr_mask & IB_QP_ACCESS_FLAGS)
  715. access_flags = attr->qp_access_flags;
  716. else
  717. access_flags = qp->atomic_rd_en;
  718. if (!dest_rd_atomic)
  719. access_flags &= IB_ACCESS_REMOTE_WRITE;
  720. if (access_flags & IB_ACCESS_REMOTE_READ)
  721. hw_access_flags |= MLX4_QP_BIT_RRE;
  722. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  723. hw_access_flags |= MLX4_QP_BIT_RAE;
  724. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  725. hw_access_flags |= MLX4_QP_BIT_RWE;
  726. return cpu_to_be32(hw_access_flags);
  727. }
  728. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  729. int attr_mask)
  730. {
  731. if (attr_mask & IB_QP_PKEY_INDEX)
  732. sqp->pkey_index = attr->pkey_index;
  733. if (attr_mask & IB_QP_QKEY)
  734. sqp->qkey = attr->qkey;
  735. if (attr_mask & IB_QP_SQ_PSN)
  736. sqp->send_psn = attr->sq_psn;
  737. }
  738. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  739. {
  740. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  741. }
  742. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  743. struct mlx4_qp_path *path, u8 port)
  744. {
  745. int err;
  746. int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port) ==
  747. IB_LINK_LAYER_ETHERNET;
  748. u8 mac[6];
  749. int is_mcast;
  750. u16 vlan_tag;
  751. int vidx;
  752. path->grh_mylmc = ah->src_path_bits & 0x7f;
  753. path->rlid = cpu_to_be16(ah->dlid);
  754. if (ah->static_rate) {
  755. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  756. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  757. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  758. --path->static_rate;
  759. } else
  760. path->static_rate = 0;
  761. if (ah->ah_flags & IB_AH_GRH) {
  762. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len[port]) {
  763. printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
  764. ah->grh.sgid_index, dev->dev->caps.gid_table_len[port] - 1);
  765. return -1;
  766. }
  767. path->grh_mylmc |= 1 << 7;
  768. path->mgid_index = ah->grh.sgid_index;
  769. path->hop_limit = ah->grh.hop_limit;
  770. path->tclass_flowlabel =
  771. cpu_to_be32((ah->grh.traffic_class << 20) |
  772. (ah->grh.flow_label));
  773. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  774. }
  775. if (is_eth) {
  776. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  777. ((port - 1) << 6) | ((ah->sl & 7) << 3) | ((ah->sl & 8) >> 1);
  778. if (!(ah->ah_flags & IB_AH_GRH))
  779. return -1;
  780. err = mlx4_ib_resolve_grh(dev, ah, mac, &is_mcast, port);
  781. if (err)
  782. return err;
  783. memcpy(path->dmac, mac, 6);
  784. path->ackto = MLX4_IB_LINK_TYPE_ETH;
  785. /* use index 0 into MAC table for IBoE */
  786. path->grh_mylmc &= 0x80;
  787. vlan_tag = rdma_get_vlan_id(&dev->iboe.gid_table[port - 1][ah->grh.sgid_index]);
  788. if (vlan_tag < 0x1000) {
  789. if (mlx4_find_cached_vlan(dev->dev, port, vlan_tag, &vidx))
  790. return -ENOENT;
  791. path->vlan_index = vidx;
  792. path->fl = 1 << 6;
  793. }
  794. } else
  795. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  796. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  797. return 0;
  798. }
  799. static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  800. {
  801. struct mlx4_ib_gid_entry *ge, *tmp;
  802. list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
  803. if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
  804. ge->added = 1;
  805. ge->port = qp->port;
  806. }
  807. }
  808. }
  809. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  810. const struct ib_qp_attr *attr, int attr_mask,
  811. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  812. {
  813. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  814. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  815. struct mlx4_qp_context *context;
  816. enum mlx4_qp_optpar optpar = 0;
  817. int sqd_event;
  818. int err = -EINVAL;
  819. context = kzalloc(sizeof *context, GFP_KERNEL);
  820. if (!context)
  821. return -ENOMEM;
  822. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  823. (to_mlx4_st(ibqp->qp_type) << 16));
  824. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  825. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  826. else {
  827. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  828. switch (attr->path_mig_state) {
  829. case IB_MIG_MIGRATED:
  830. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  831. break;
  832. case IB_MIG_REARM:
  833. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  834. break;
  835. case IB_MIG_ARMED:
  836. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  837. break;
  838. }
  839. }
  840. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI)
  841. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  842. else if (ibqp->qp_type == IB_QPT_UD) {
  843. if (qp->flags & MLX4_IB_QP_LSO)
  844. context->mtu_msgmax = (IB_MTU_4096 << 5) |
  845. ilog2(dev->dev->caps.max_gso_sz);
  846. else
  847. context->mtu_msgmax = (IB_MTU_4096 << 5) | 12;
  848. } else if (attr_mask & IB_QP_PATH_MTU) {
  849. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  850. printk(KERN_ERR "path MTU (%u) is invalid\n",
  851. attr->path_mtu);
  852. goto out;
  853. }
  854. context->mtu_msgmax = (attr->path_mtu << 5) |
  855. ilog2(dev->dev->caps.max_msg_sz);
  856. }
  857. if (qp->rq.wqe_cnt)
  858. context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
  859. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  860. if (qp->sq.wqe_cnt)
  861. context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
  862. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  863. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  864. context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
  865. if (qp->ibqp.uobject)
  866. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  867. else
  868. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  869. if (attr_mask & IB_QP_DEST_QPN)
  870. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  871. if (attr_mask & IB_QP_PORT) {
  872. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  873. !(attr_mask & IB_QP_AV)) {
  874. mlx4_set_sched(&context->pri_path, attr->port_num);
  875. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  876. }
  877. }
  878. if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
  879. if (dev->counters[qp->port - 1] != -1) {
  880. context->pri_path.counter_index =
  881. dev->counters[qp->port - 1];
  882. optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
  883. } else
  884. context->pri_path.counter_index = 0xff;
  885. }
  886. if (attr_mask & IB_QP_PKEY_INDEX) {
  887. context->pri_path.pkey_index = attr->pkey_index;
  888. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  889. }
  890. if (attr_mask & IB_QP_AV) {
  891. if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
  892. attr_mask & IB_QP_PORT ? attr->port_num : qp->port))
  893. goto out;
  894. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  895. MLX4_QP_OPTPAR_SCHED_QUEUE);
  896. }
  897. if (attr_mask & IB_QP_TIMEOUT) {
  898. context->pri_path.ackto |= attr->timeout << 3;
  899. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  900. }
  901. if (attr_mask & IB_QP_ALT_PATH) {
  902. if (attr->alt_port_num == 0 ||
  903. attr->alt_port_num > dev->dev->caps.num_ports)
  904. goto out;
  905. if (attr->alt_pkey_index >=
  906. dev->dev->caps.pkey_table_len[attr->alt_port_num])
  907. goto out;
  908. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  909. attr->alt_port_num))
  910. goto out;
  911. context->alt_path.pkey_index = attr->alt_pkey_index;
  912. context->alt_path.ackto = attr->alt_timeout << 3;
  913. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  914. }
  915. context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
  916. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  917. /* Set "fast registration enabled" for all kernel QPs */
  918. if (!qp->ibqp.uobject)
  919. context->params1 |= cpu_to_be32(1 << 11);
  920. if (attr_mask & IB_QP_RNR_RETRY) {
  921. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  922. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  923. }
  924. if (attr_mask & IB_QP_RETRY_CNT) {
  925. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  926. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  927. }
  928. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  929. if (attr->max_rd_atomic)
  930. context->params1 |=
  931. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  932. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  933. }
  934. if (attr_mask & IB_QP_SQ_PSN)
  935. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  936. context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
  937. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  938. if (attr->max_dest_rd_atomic)
  939. context->params2 |=
  940. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  941. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  942. }
  943. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  944. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  945. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  946. }
  947. if (ibqp->srq)
  948. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  949. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  950. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  951. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  952. }
  953. if (attr_mask & IB_QP_RQ_PSN)
  954. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  955. context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
  956. if (attr_mask & IB_QP_QKEY) {
  957. context->qkey = cpu_to_be32(attr->qkey);
  958. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  959. }
  960. if (ibqp->srq)
  961. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  962. if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  963. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  964. if (cur_state == IB_QPS_INIT &&
  965. new_state == IB_QPS_RTR &&
  966. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  967. ibqp->qp_type == IB_QPT_UD)) {
  968. context->pri_path.sched_queue = (qp->port - 1) << 6;
  969. if (is_qp0(dev, qp))
  970. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  971. else
  972. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  973. }
  974. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  975. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  976. sqd_event = 1;
  977. else
  978. sqd_event = 0;
  979. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  980. context->rlkey |= (1 << 4);
  981. /*
  982. * Before passing a kernel QP to the HW, make sure that the
  983. * ownership bits of the send queue are set and the SQ
  984. * headroom is stamped so that the hardware doesn't start
  985. * processing stale work requests.
  986. */
  987. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  988. struct mlx4_wqe_ctrl_seg *ctrl;
  989. int i;
  990. for (i = 0; i < qp->sq.wqe_cnt; ++i) {
  991. ctrl = get_send_wqe(qp, i);
  992. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  993. if (qp->sq_max_wqes_per_wr == 1)
  994. ctrl->fence_size = 1 << (qp->sq.wqe_shift - 4);
  995. stamp_send_wqe(qp, i, 1 << qp->sq.wqe_shift);
  996. }
  997. }
  998. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  999. to_mlx4_state(new_state), context, optpar,
  1000. sqd_event, &qp->mqp);
  1001. if (err)
  1002. goto out;
  1003. qp->state = new_state;
  1004. if (attr_mask & IB_QP_ACCESS_FLAGS)
  1005. qp->atomic_rd_en = attr->qp_access_flags;
  1006. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1007. qp->resp_depth = attr->max_dest_rd_atomic;
  1008. if (attr_mask & IB_QP_PORT) {
  1009. qp->port = attr->port_num;
  1010. update_mcg_macs(dev, qp);
  1011. }
  1012. if (attr_mask & IB_QP_ALT_PATH)
  1013. qp->alt_port = attr->alt_port_num;
  1014. if (is_sqp(dev, qp))
  1015. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  1016. /*
  1017. * If we moved QP0 to RTR, bring the IB link up; if we moved
  1018. * QP0 to RESET or ERROR, bring the link back down.
  1019. */
  1020. if (is_qp0(dev, qp)) {
  1021. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  1022. if (mlx4_INIT_PORT(dev->dev, qp->port))
  1023. printk(KERN_WARNING "INIT_PORT failed for port %d\n",
  1024. qp->port);
  1025. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  1026. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  1027. mlx4_CLOSE_PORT(dev->dev, qp->port);
  1028. }
  1029. /*
  1030. * If we moved a kernel QP to RESET, clean up all old CQ
  1031. * entries and reinitialize the QP.
  1032. */
  1033. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  1034. mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
  1035. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  1036. if (ibqp->send_cq != ibqp->recv_cq)
  1037. mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
  1038. qp->rq.head = 0;
  1039. qp->rq.tail = 0;
  1040. qp->sq.head = 0;
  1041. qp->sq.tail = 0;
  1042. qp->sq_next_wqe = 0;
  1043. if (!ibqp->srq)
  1044. *qp->db.db = 0;
  1045. }
  1046. out:
  1047. kfree(context);
  1048. return err;
  1049. }
  1050. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  1051. int attr_mask, struct ib_udata *udata)
  1052. {
  1053. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1054. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1055. enum ib_qp_state cur_state, new_state;
  1056. int err = -EINVAL;
  1057. mutex_lock(&qp->mutex);
  1058. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  1059. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  1060. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  1061. goto out;
  1062. if ((attr_mask & IB_QP_PORT) &&
  1063. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  1064. goto out;
  1065. }
  1066. if (attr_mask & IB_QP_PKEY_INDEX) {
  1067. int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
  1068. if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p])
  1069. goto out;
  1070. }
  1071. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  1072. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  1073. goto out;
  1074. }
  1075. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  1076. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  1077. goto out;
  1078. }
  1079. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  1080. err = 0;
  1081. goto out;
  1082. }
  1083. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  1084. out:
  1085. mutex_unlock(&qp->mutex);
  1086. return err;
  1087. }
  1088. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  1089. void *wqe, unsigned *mlx_seg_len)
  1090. {
  1091. struct ib_device *ib_dev = sqp->qp.ibqp.device;
  1092. struct mlx4_wqe_mlx_seg *mlx = wqe;
  1093. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  1094. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  1095. union ib_gid sgid;
  1096. u16 pkey;
  1097. int send_size;
  1098. int header_size;
  1099. int spc;
  1100. int i;
  1101. int is_eth;
  1102. int is_vlan = 0;
  1103. int is_grh;
  1104. u16 vlan;
  1105. send_size = 0;
  1106. for (i = 0; i < wr->num_sge; ++i)
  1107. send_size += wr->sg_list[i].length;
  1108. is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
  1109. is_grh = mlx4_ib_ah_grh_present(ah);
  1110. if (is_eth) {
  1111. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.ib.port_pd) >> 24,
  1112. ah->av.ib.gid_index, &sgid);
  1113. vlan = rdma_get_vlan_id(&sgid);
  1114. is_vlan = vlan < 0x1000;
  1115. }
  1116. ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh, 0, &sqp->ud_header);
  1117. if (!is_eth) {
  1118. sqp->ud_header.lrh.service_level =
  1119. be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
  1120. sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
  1121. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
  1122. }
  1123. if (is_grh) {
  1124. sqp->ud_header.grh.traffic_class =
  1125. (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
  1126. sqp->ud_header.grh.flow_label =
  1127. ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  1128. sqp->ud_header.grh.hop_limit = ah->av.ib.hop_limit;
  1129. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.ib.port_pd) >> 24,
  1130. ah->av.ib.gid_index, &sqp->ud_header.grh.source_gid);
  1131. memcpy(sqp->ud_header.grh.destination_gid.raw,
  1132. ah->av.ib.dgid, 16);
  1133. }
  1134. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  1135. if (!is_eth) {
  1136. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  1137. (sqp->ud_header.lrh.destination_lid ==
  1138. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  1139. (sqp->ud_header.lrh.service_level << 8));
  1140. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  1141. }
  1142. switch (wr->opcode) {
  1143. case IB_WR_SEND:
  1144. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  1145. sqp->ud_header.immediate_present = 0;
  1146. break;
  1147. case IB_WR_SEND_WITH_IMM:
  1148. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  1149. sqp->ud_header.immediate_present = 1;
  1150. sqp->ud_header.immediate_data = wr->ex.imm_data;
  1151. break;
  1152. default:
  1153. return -EINVAL;
  1154. }
  1155. if (is_eth) {
  1156. u8 *smac;
  1157. memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
  1158. /* FIXME: cache smac value? */
  1159. smac = to_mdev(sqp->qp.ibqp.device)->iboe.netdevs[sqp->qp.port - 1]->dev_addr;
  1160. memcpy(sqp->ud_header.eth.smac_h, smac, 6);
  1161. if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
  1162. mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  1163. if (!is_vlan) {
  1164. sqp->ud_header.eth.type = cpu_to_be16(MLX4_IB_IBOE_ETHERTYPE);
  1165. } else {
  1166. u16 pcp;
  1167. sqp->ud_header.vlan.type = cpu_to_be16(MLX4_IB_IBOE_ETHERTYPE);
  1168. pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 27 & 3) << 13;
  1169. sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
  1170. }
  1171. } else {
  1172. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  1173. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  1174. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  1175. }
  1176. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  1177. if (!sqp->qp.ibqp.qp_num)
  1178. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  1179. else
  1180. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  1181. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  1182. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1183. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  1184. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  1185. sqp->qkey : wr->wr.ud.remote_qkey);
  1186. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  1187. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  1188. if (0) {
  1189. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  1190. for (i = 0; i < header_size / 4; ++i) {
  1191. if (i % 8 == 0)
  1192. printk(" [%02x] ", i * 4);
  1193. printk(" %08x",
  1194. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  1195. if ((i + 1) % 8 == 0)
  1196. printk("\n");
  1197. }
  1198. printk("\n");
  1199. }
  1200. /*
  1201. * Inline data segments may not cross a 64 byte boundary. If
  1202. * our UD header is bigger than the space available up to the
  1203. * next 64 byte boundary in the WQE, use two inline data
  1204. * segments to hold the UD header.
  1205. */
  1206. spc = MLX4_INLINE_ALIGN -
  1207. ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
  1208. if (header_size <= spc) {
  1209. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  1210. memcpy(inl + 1, sqp->header_buf, header_size);
  1211. i = 1;
  1212. } else {
  1213. inl->byte_count = cpu_to_be32(1 << 31 | spc);
  1214. memcpy(inl + 1, sqp->header_buf, spc);
  1215. inl = (void *) (inl + 1) + spc;
  1216. memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
  1217. /*
  1218. * Need a barrier here to make sure all the data is
  1219. * visible before the byte_count field is set.
  1220. * Otherwise the HCA prefetcher could grab the 64-byte
  1221. * chunk with this inline segment and get a valid (!=
  1222. * 0xffffffff) byte count but stale data, and end up
  1223. * generating a packet with bad headers.
  1224. *
  1225. * The first inline segment's byte_count field doesn't
  1226. * need a barrier, because it comes after a
  1227. * control/MLX segment and therefore is at an offset
  1228. * of 16 mod 64.
  1229. */
  1230. wmb();
  1231. inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
  1232. i = 2;
  1233. }
  1234. *mlx_seg_len =
  1235. ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  1236. return 0;
  1237. }
  1238. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  1239. {
  1240. unsigned cur;
  1241. struct mlx4_ib_cq *cq;
  1242. cur = wq->head - wq->tail;
  1243. if (likely(cur + nreq < wq->max_post))
  1244. return 0;
  1245. cq = to_mcq(ib_cq);
  1246. spin_lock(&cq->lock);
  1247. cur = wq->head - wq->tail;
  1248. spin_unlock(&cq->lock);
  1249. return cur + nreq >= wq->max_post;
  1250. }
  1251. static __be32 convert_access(int acc)
  1252. {
  1253. return (acc & IB_ACCESS_REMOTE_ATOMIC ? cpu_to_be32(MLX4_WQE_FMR_PERM_ATOMIC) : 0) |
  1254. (acc & IB_ACCESS_REMOTE_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_REMOTE_WRITE) : 0) |
  1255. (acc & IB_ACCESS_REMOTE_READ ? cpu_to_be32(MLX4_WQE_FMR_PERM_REMOTE_READ) : 0) |
  1256. (acc & IB_ACCESS_LOCAL_WRITE ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE) : 0) |
  1257. cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
  1258. }
  1259. static void set_fmr_seg(struct mlx4_wqe_fmr_seg *fseg, struct ib_send_wr *wr)
  1260. {
  1261. struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(wr->wr.fast_reg.page_list);
  1262. int i;
  1263. for (i = 0; i < wr->wr.fast_reg.page_list_len; ++i)
  1264. mfrpl->mapped_page_list[i] =
  1265. cpu_to_be64(wr->wr.fast_reg.page_list->page_list[i] |
  1266. MLX4_MTT_FLAG_PRESENT);
  1267. fseg->flags = convert_access(wr->wr.fast_reg.access_flags);
  1268. fseg->mem_key = cpu_to_be32(wr->wr.fast_reg.rkey);
  1269. fseg->buf_list = cpu_to_be64(mfrpl->map);
  1270. fseg->start_addr = cpu_to_be64(wr->wr.fast_reg.iova_start);
  1271. fseg->reg_len = cpu_to_be64(wr->wr.fast_reg.length);
  1272. fseg->offset = 0; /* XXX -- is this just for ZBVA? */
  1273. fseg->page_size = cpu_to_be32(wr->wr.fast_reg.page_shift);
  1274. fseg->reserved[0] = 0;
  1275. fseg->reserved[1] = 0;
  1276. }
  1277. static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
  1278. {
  1279. iseg->flags = 0;
  1280. iseg->mem_key = cpu_to_be32(rkey);
  1281. iseg->guest_id = 0;
  1282. iseg->pa = 0;
  1283. }
  1284. static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
  1285. u64 remote_addr, u32 rkey)
  1286. {
  1287. rseg->raddr = cpu_to_be64(remote_addr);
  1288. rseg->rkey = cpu_to_be32(rkey);
  1289. rseg->reserved = 0;
  1290. }
  1291. static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg, struct ib_send_wr *wr)
  1292. {
  1293. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1294. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1295. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1296. } else if (wr->opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
  1297. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1298. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add_mask);
  1299. } else {
  1300. aseg->swap_add = cpu_to_be64(wr->wr.atomic.compare_add);
  1301. aseg->compare = 0;
  1302. }
  1303. }
  1304. static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
  1305. struct ib_send_wr *wr)
  1306. {
  1307. aseg->swap_add = cpu_to_be64(wr->wr.atomic.swap);
  1308. aseg->swap_add_mask = cpu_to_be64(wr->wr.atomic.swap_mask);
  1309. aseg->compare = cpu_to_be64(wr->wr.atomic.compare_add);
  1310. aseg->compare_mask = cpu_to_be64(wr->wr.atomic.compare_add_mask);
  1311. }
  1312. static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
  1313. struct ib_send_wr *wr, __be16 *vlan)
  1314. {
  1315. memcpy(dseg->av, &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1316. dseg->dqpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  1317. dseg->qkey = cpu_to_be32(wr->wr.ud.remote_qkey);
  1318. dseg->vlan = to_mah(wr->wr.ud.ah)->av.eth.vlan;
  1319. memcpy(dseg->mac, to_mah(wr->wr.ud.ah)->av.eth.mac, 6);
  1320. *vlan = dseg->vlan;
  1321. }
  1322. static void set_mlx_icrc_seg(void *dseg)
  1323. {
  1324. u32 *t = dseg;
  1325. struct mlx4_wqe_inline_seg *iseg = dseg;
  1326. t[1] = 0;
  1327. /*
  1328. * Need a barrier here before writing the byte_count field to
  1329. * make sure that all the data is visible before the
  1330. * byte_count field is set. Otherwise, if the segment begins
  1331. * a new cacheline, the HCA prefetcher could grab the 64-byte
  1332. * chunk and get a valid (!= * 0xffffffff) byte count but
  1333. * stale data, and end up sending the wrong data.
  1334. */
  1335. wmb();
  1336. iseg->byte_count = cpu_to_be32((1 << 31) | 4);
  1337. }
  1338. static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  1339. {
  1340. dseg->lkey = cpu_to_be32(sg->lkey);
  1341. dseg->addr = cpu_to_be64(sg->addr);
  1342. /*
  1343. * Need a barrier here before writing the byte_count field to
  1344. * make sure that all the data is visible before the
  1345. * byte_count field is set. Otherwise, if the segment begins
  1346. * a new cacheline, the HCA prefetcher could grab the 64-byte
  1347. * chunk and get a valid (!= * 0xffffffff) byte count but
  1348. * stale data, and end up sending the wrong data.
  1349. */
  1350. wmb();
  1351. dseg->byte_count = cpu_to_be32(sg->length);
  1352. }
  1353. static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
  1354. {
  1355. dseg->byte_count = cpu_to_be32(sg->length);
  1356. dseg->lkey = cpu_to_be32(sg->lkey);
  1357. dseg->addr = cpu_to_be64(sg->addr);
  1358. }
  1359. static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe, struct ib_send_wr *wr,
  1360. struct mlx4_ib_qp *qp, unsigned *lso_seg_len,
  1361. __be32 *lso_hdr_sz, __be32 *blh)
  1362. {
  1363. unsigned halign = ALIGN(sizeof *wqe + wr->wr.ud.hlen, 16);
  1364. if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
  1365. *blh = cpu_to_be32(1 << 6);
  1366. if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
  1367. wr->num_sge > qp->sq.max_gs - (halign >> 4)))
  1368. return -EINVAL;
  1369. memcpy(wqe->header, wr->wr.ud.header, wr->wr.ud.hlen);
  1370. *lso_hdr_sz = cpu_to_be32((wr->wr.ud.mss - wr->wr.ud.hlen) << 16 |
  1371. wr->wr.ud.hlen);
  1372. *lso_seg_len = halign;
  1373. return 0;
  1374. }
  1375. static __be32 send_ieth(struct ib_send_wr *wr)
  1376. {
  1377. switch (wr->opcode) {
  1378. case IB_WR_SEND_WITH_IMM:
  1379. case IB_WR_RDMA_WRITE_WITH_IMM:
  1380. return wr->ex.imm_data;
  1381. case IB_WR_SEND_WITH_INV:
  1382. return cpu_to_be32(wr->ex.invalidate_rkey);
  1383. default:
  1384. return 0;
  1385. }
  1386. }
  1387. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  1388. struct ib_send_wr **bad_wr)
  1389. {
  1390. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1391. void *wqe;
  1392. struct mlx4_wqe_ctrl_seg *ctrl;
  1393. struct mlx4_wqe_data_seg *dseg;
  1394. unsigned long flags;
  1395. int nreq;
  1396. int err = 0;
  1397. unsigned ind;
  1398. int uninitialized_var(stamp);
  1399. int uninitialized_var(size);
  1400. unsigned uninitialized_var(seglen);
  1401. __be32 dummy;
  1402. __be32 *lso_wqe;
  1403. __be32 uninitialized_var(lso_hdr_sz);
  1404. __be32 blh;
  1405. int i;
  1406. __be16 vlan = cpu_to_be16(0xffff);
  1407. spin_lock_irqsave(&qp->sq.lock, flags);
  1408. ind = qp->sq_next_wqe;
  1409. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1410. lso_wqe = &dummy;
  1411. blh = 0;
  1412. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  1413. err = -ENOMEM;
  1414. *bad_wr = wr;
  1415. goto out;
  1416. }
  1417. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  1418. err = -EINVAL;
  1419. *bad_wr = wr;
  1420. goto out;
  1421. }
  1422. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
  1423. qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
  1424. ctrl->srcrb_flags =
  1425. (wr->send_flags & IB_SEND_SIGNALED ?
  1426. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  1427. (wr->send_flags & IB_SEND_SOLICITED ?
  1428. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  1429. ((wr->send_flags & IB_SEND_IP_CSUM) ?
  1430. cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
  1431. MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
  1432. qp->sq_signal_bits;
  1433. ctrl->imm = send_ieth(wr);
  1434. wqe += sizeof *ctrl;
  1435. size = sizeof *ctrl / 16;
  1436. switch (ibqp->qp_type) {
  1437. case IB_QPT_RC:
  1438. case IB_QPT_UC:
  1439. switch (wr->opcode) {
  1440. case IB_WR_ATOMIC_CMP_AND_SWP:
  1441. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1442. case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
  1443. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  1444. wr->wr.atomic.rkey);
  1445. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1446. set_atomic_seg(wqe, wr);
  1447. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1448. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1449. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1450. break;
  1451. case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
  1452. set_raddr_seg(wqe, wr->wr.atomic.remote_addr,
  1453. wr->wr.atomic.rkey);
  1454. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1455. set_masked_atomic_seg(wqe, wr);
  1456. wqe += sizeof (struct mlx4_wqe_masked_atomic_seg);
  1457. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1458. sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
  1459. break;
  1460. case IB_WR_RDMA_READ:
  1461. case IB_WR_RDMA_WRITE:
  1462. case IB_WR_RDMA_WRITE_WITH_IMM:
  1463. set_raddr_seg(wqe, wr->wr.rdma.remote_addr,
  1464. wr->wr.rdma.rkey);
  1465. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1466. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1467. break;
  1468. case IB_WR_LOCAL_INV:
  1469. ctrl->srcrb_flags |=
  1470. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  1471. set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
  1472. wqe += sizeof (struct mlx4_wqe_local_inval_seg);
  1473. size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
  1474. break;
  1475. case IB_WR_FAST_REG_MR:
  1476. ctrl->srcrb_flags |=
  1477. cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
  1478. set_fmr_seg(wqe, wr);
  1479. wqe += sizeof (struct mlx4_wqe_fmr_seg);
  1480. size += sizeof (struct mlx4_wqe_fmr_seg) / 16;
  1481. break;
  1482. default:
  1483. /* No extra segments required for sends */
  1484. break;
  1485. }
  1486. break;
  1487. case IB_QPT_UD:
  1488. set_datagram_seg(wqe, wr, &vlan);
  1489. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1490. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1491. if (wr->opcode == IB_WR_LSO) {
  1492. err = build_lso_seg(wqe, wr, qp, &seglen, &lso_hdr_sz, &blh);
  1493. if (unlikely(err)) {
  1494. *bad_wr = wr;
  1495. goto out;
  1496. }
  1497. lso_wqe = (__be32 *) wqe;
  1498. wqe += seglen;
  1499. size += seglen / 16;
  1500. }
  1501. break;
  1502. case IB_QPT_SMI:
  1503. case IB_QPT_GSI:
  1504. err = build_mlx_header(to_msqp(qp), wr, ctrl, &seglen);
  1505. if (unlikely(err)) {
  1506. *bad_wr = wr;
  1507. goto out;
  1508. }
  1509. wqe += seglen;
  1510. size += seglen / 16;
  1511. break;
  1512. default:
  1513. break;
  1514. }
  1515. /*
  1516. * Write data segments in reverse order, so as to
  1517. * overwrite cacheline stamp last within each
  1518. * cacheline. This avoids issues with WQE
  1519. * prefetching.
  1520. */
  1521. dseg = wqe;
  1522. dseg += wr->num_sge - 1;
  1523. size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
  1524. /* Add one more inline data segment for ICRC for MLX sends */
  1525. if (unlikely(qp->ibqp.qp_type == IB_QPT_SMI ||
  1526. qp->ibqp.qp_type == IB_QPT_GSI)) {
  1527. set_mlx_icrc_seg(dseg + 1);
  1528. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1529. }
  1530. for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
  1531. set_data_seg(dseg, wr->sg_list + i);
  1532. /*
  1533. * Possibly overwrite stamping in cacheline with LSO
  1534. * segment only after making sure all data segments
  1535. * are written.
  1536. */
  1537. wmb();
  1538. *lso_wqe = lso_hdr_sz;
  1539. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1540. MLX4_WQE_CTRL_FENCE : 0) | size;
  1541. if (be16_to_cpu(vlan) < 0x1000) {
  1542. ctrl->ins_vlan = 1 << 6;
  1543. ctrl->vlan_tag = vlan;
  1544. }
  1545. /*
  1546. * Make sure descriptor is fully written before
  1547. * setting ownership bit (because HW can start
  1548. * executing as soon as we do).
  1549. */
  1550. wmb();
  1551. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1552. err = -EINVAL;
  1553. goto out;
  1554. }
  1555. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1556. (ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
  1557. stamp = ind + qp->sq_spare_wqes;
  1558. ind += DIV_ROUND_UP(size * 16, 1U << qp->sq.wqe_shift);
  1559. /*
  1560. * We can improve latency by not stamping the last
  1561. * send queue WQE until after ringing the doorbell, so
  1562. * only stamp here if there are still more WQEs to post.
  1563. *
  1564. * Same optimization applies to padding with NOP wqe
  1565. * in case of WQE shrinking (used to prevent wrap-around
  1566. * in the middle of WR).
  1567. */
  1568. if (wr->next) {
  1569. stamp_send_wqe(qp, stamp, size * 16);
  1570. ind = pad_wraparound(qp, ind);
  1571. }
  1572. }
  1573. out:
  1574. if (likely(nreq)) {
  1575. qp->sq.head += nreq;
  1576. /*
  1577. * Make sure that descriptors are written before
  1578. * doorbell record.
  1579. */
  1580. wmb();
  1581. writel(qp->doorbell_qpn,
  1582. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1583. /*
  1584. * Make sure doorbells don't leak out of SQ spinlock
  1585. * and reach the HCA out of order.
  1586. */
  1587. mmiowb();
  1588. stamp_send_wqe(qp, stamp, size * 16);
  1589. ind = pad_wraparound(qp, ind);
  1590. qp->sq_next_wqe = ind;
  1591. }
  1592. spin_unlock_irqrestore(&qp->sq.lock, flags);
  1593. return err;
  1594. }
  1595. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1596. struct ib_recv_wr **bad_wr)
  1597. {
  1598. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1599. struct mlx4_wqe_data_seg *scat;
  1600. unsigned long flags;
  1601. int err = 0;
  1602. int nreq;
  1603. int ind;
  1604. int i;
  1605. spin_lock_irqsave(&qp->rq.lock, flags);
  1606. ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
  1607. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1608. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
  1609. err = -ENOMEM;
  1610. *bad_wr = wr;
  1611. goto out;
  1612. }
  1613. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1614. err = -EINVAL;
  1615. *bad_wr = wr;
  1616. goto out;
  1617. }
  1618. scat = get_recv_wqe(qp, ind);
  1619. for (i = 0; i < wr->num_sge; ++i)
  1620. __set_data_seg(scat + i, wr->sg_list + i);
  1621. if (i < qp->rq.max_gs) {
  1622. scat[i].byte_count = 0;
  1623. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1624. scat[i].addr = 0;
  1625. }
  1626. qp->rq.wrid[ind] = wr->wr_id;
  1627. ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
  1628. }
  1629. out:
  1630. if (likely(nreq)) {
  1631. qp->rq.head += nreq;
  1632. /*
  1633. * Make sure that descriptors are written before
  1634. * doorbell record.
  1635. */
  1636. wmb();
  1637. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1638. }
  1639. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1640. return err;
  1641. }
  1642. static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
  1643. {
  1644. switch (mlx4_state) {
  1645. case MLX4_QP_STATE_RST: return IB_QPS_RESET;
  1646. case MLX4_QP_STATE_INIT: return IB_QPS_INIT;
  1647. case MLX4_QP_STATE_RTR: return IB_QPS_RTR;
  1648. case MLX4_QP_STATE_RTS: return IB_QPS_RTS;
  1649. case MLX4_QP_STATE_SQ_DRAINING:
  1650. case MLX4_QP_STATE_SQD: return IB_QPS_SQD;
  1651. case MLX4_QP_STATE_SQER: return IB_QPS_SQE;
  1652. case MLX4_QP_STATE_ERR: return IB_QPS_ERR;
  1653. default: return -1;
  1654. }
  1655. }
  1656. static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
  1657. {
  1658. switch (mlx4_mig_state) {
  1659. case MLX4_QP_PM_ARMED: return IB_MIG_ARMED;
  1660. case MLX4_QP_PM_REARM: return IB_MIG_REARM;
  1661. case MLX4_QP_PM_MIGRATED: return IB_MIG_MIGRATED;
  1662. default: return -1;
  1663. }
  1664. }
  1665. static int to_ib_qp_access_flags(int mlx4_flags)
  1666. {
  1667. int ib_flags = 0;
  1668. if (mlx4_flags & MLX4_QP_BIT_RRE)
  1669. ib_flags |= IB_ACCESS_REMOTE_READ;
  1670. if (mlx4_flags & MLX4_QP_BIT_RWE)
  1671. ib_flags |= IB_ACCESS_REMOTE_WRITE;
  1672. if (mlx4_flags & MLX4_QP_BIT_RAE)
  1673. ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
  1674. return ib_flags;
  1675. }
  1676. static void to_ib_ah_attr(struct mlx4_ib_dev *ibdev, struct ib_ah_attr *ib_ah_attr,
  1677. struct mlx4_qp_path *path)
  1678. {
  1679. struct mlx4_dev *dev = ibdev->dev;
  1680. int is_eth;
  1681. memset(ib_ah_attr, 0, sizeof *ib_ah_attr);
  1682. ib_ah_attr->port_num = path->sched_queue & 0x40 ? 2 : 1;
  1683. if (ib_ah_attr->port_num == 0 || ib_ah_attr->port_num > dev->caps.num_ports)
  1684. return;
  1685. is_eth = rdma_port_get_link_layer(&ibdev->ib_dev, ib_ah_attr->port_num) ==
  1686. IB_LINK_LAYER_ETHERNET;
  1687. if (is_eth)
  1688. ib_ah_attr->sl = ((path->sched_queue >> 3) & 0x7) |
  1689. ((path->sched_queue & 4) << 1);
  1690. else
  1691. ib_ah_attr->sl = (path->sched_queue >> 2) & 0xf;
  1692. ib_ah_attr->dlid = be16_to_cpu(path->rlid);
  1693. ib_ah_attr->src_path_bits = path->grh_mylmc & 0x7f;
  1694. ib_ah_attr->static_rate = path->static_rate ? path->static_rate - 5 : 0;
  1695. ib_ah_attr->ah_flags = (path->grh_mylmc & (1 << 7)) ? IB_AH_GRH : 0;
  1696. if (ib_ah_attr->ah_flags) {
  1697. ib_ah_attr->grh.sgid_index = path->mgid_index;
  1698. ib_ah_attr->grh.hop_limit = path->hop_limit;
  1699. ib_ah_attr->grh.traffic_class =
  1700. (be32_to_cpu(path->tclass_flowlabel) >> 20) & 0xff;
  1701. ib_ah_attr->grh.flow_label =
  1702. be32_to_cpu(path->tclass_flowlabel) & 0xfffff;
  1703. memcpy(ib_ah_attr->grh.dgid.raw,
  1704. path->rgid, sizeof ib_ah_attr->grh.dgid.raw);
  1705. }
  1706. }
  1707. int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
  1708. struct ib_qp_init_attr *qp_init_attr)
  1709. {
  1710. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  1711. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1712. struct mlx4_qp_context context;
  1713. int mlx4_state;
  1714. int err = 0;
  1715. mutex_lock(&qp->mutex);
  1716. if (qp->state == IB_QPS_RESET) {
  1717. qp_attr->qp_state = IB_QPS_RESET;
  1718. goto done;
  1719. }
  1720. err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
  1721. if (err) {
  1722. err = -EINVAL;
  1723. goto out;
  1724. }
  1725. mlx4_state = be32_to_cpu(context.flags) >> 28;
  1726. qp->state = to_ib_qp_state(mlx4_state);
  1727. qp_attr->qp_state = qp->state;
  1728. qp_attr->path_mtu = context.mtu_msgmax >> 5;
  1729. qp_attr->path_mig_state =
  1730. to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
  1731. qp_attr->qkey = be32_to_cpu(context.qkey);
  1732. qp_attr->rq_psn = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
  1733. qp_attr->sq_psn = be32_to_cpu(context.next_send_psn) & 0xffffff;
  1734. qp_attr->dest_qp_num = be32_to_cpu(context.remote_qpn) & 0xffffff;
  1735. qp_attr->qp_access_flags =
  1736. to_ib_qp_access_flags(be32_to_cpu(context.params2));
  1737. if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
  1738. to_ib_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
  1739. to_ib_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
  1740. qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
  1741. qp_attr->alt_port_num = qp_attr->alt_ah_attr.port_num;
  1742. }
  1743. qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
  1744. if (qp_attr->qp_state == IB_QPS_INIT)
  1745. qp_attr->port_num = qp->port;
  1746. else
  1747. qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
  1748. /* qp_attr->en_sqd_async_notify is only applicable in modify qp */
  1749. qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
  1750. qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
  1751. qp_attr->max_dest_rd_atomic =
  1752. 1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
  1753. qp_attr->min_rnr_timer =
  1754. (be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
  1755. qp_attr->timeout = context.pri_path.ackto >> 3;
  1756. qp_attr->retry_cnt = (be32_to_cpu(context.params1) >> 16) & 0x7;
  1757. qp_attr->rnr_retry = (be32_to_cpu(context.params1) >> 13) & 0x7;
  1758. qp_attr->alt_timeout = context.alt_path.ackto >> 3;
  1759. done:
  1760. qp_attr->cur_qp_state = qp_attr->qp_state;
  1761. qp_attr->cap.max_recv_wr = qp->rq.wqe_cnt;
  1762. qp_attr->cap.max_recv_sge = qp->rq.max_gs;
  1763. if (!ibqp->uobject) {
  1764. qp_attr->cap.max_send_wr = qp->sq.wqe_cnt;
  1765. qp_attr->cap.max_send_sge = qp->sq.max_gs;
  1766. } else {
  1767. qp_attr->cap.max_send_wr = 0;
  1768. qp_attr->cap.max_send_sge = 0;
  1769. }
  1770. /*
  1771. * We don't support inline sends for kernel QPs (yet), and we
  1772. * don't know what userspace's value should be.
  1773. */
  1774. qp_attr->cap.max_inline_data = 0;
  1775. qp_init_attr->cap = qp_attr->cap;
  1776. qp_init_attr->create_flags = 0;
  1777. if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
  1778. qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
  1779. if (qp->flags & MLX4_IB_QP_LSO)
  1780. qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
  1781. out:
  1782. mutex_unlock(&qp->mutex);
  1783. return err;
  1784. }