qp.c 54 KB

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