qp.c 51 KB

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