qp.c 51 KB

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