qp.c 62 KB

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