qp.c 51 KB

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