qp.c 49 KB

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