qp.c 55 KB

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