qp.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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 <rdma/ib_cache.h>
  33. #include <rdma/ib_pack.h>
  34. #include <linux/mlx4/qp.h>
  35. #include "mlx4_ib.h"
  36. #include "user.h"
  37. enum {
  38. MLX4_IB_ACK_REQ_FREQ = 8,
  39. };
  40. enum {
  41. MLX4_IB_DEFAULT_SCHED_QUEUE = 0x83,
  42. MLX4_IB_DEFAULT_QP0_SCHED_QUEUE = 0x3f
  43. };
  44. enum {
  45. /*
  46. * Largest possible UD header: send with GRH and immediate data.
  47. */
  48. MLX4_IB_UD_HEADER_SIZE = 72
  49. };
  50. struct mlx4_ib_sqp {
  51. struct mlx4_ib_qp qp;
  52. int pkey_index;
  53. u32 qkey;
  54. u32 send_psn;
  55. struct ib_ud_header ud_header;
  56. u8 header_buf[MLX4_IB_UD_HEADER_SIZE];
  57. };
  58. static const __be32 mlx4_ib_opcode[] = {
  59. [IB_WR_SEND] = __constant_cpu_to_be32(MLX4_OPCODE_SEND),
  60. [IB_WR_SEND_WITH_IMM] = __constant_cpu_to_be32(MLX4_OPCODE_SEND_IMM),
  61. [IB_WR_RDMA_WRITE] = __constant_cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
  62. [IB_WR_RDMA_WRITE_WITH_IMM] = __constant_cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
  63. [IB_WR_RDMA_READ] = __constant_cpu_to_be32(MLX4_OPCODE_RDMA_READ),
  64. [IB_WR_ATOMIC_CMP_AND_SWP] = __constant_cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
  65. [IB_WR_ATOMIC_FETCH_AND_ADD] = __constant_cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
  66. };
  67. static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
  68. {
  69. return container_of(mqp, struct mlx4_ib_sqp, qp);
  70. }
  71. static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  72. {
  73. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  74. qp->mqp.qpn <= dev->dev->caps.sqp_start + 3;
  75. }
  76. static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
  77. {
  78. return qp->mqp.qpn >= dev->dev->caps.sqp_start &&
  79. qp->mqp.qpn <= dev->dev->caps.sqp_start + 1;
  80. }
  81. static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
  82. {
  83. if (qp->buf.nbufs == 1)
  84. return qp->buf.u.direct.buf + offset;
  85. else
  86. return qp->buf.u.page_list[offset >> PAGE_SHIFT].buf +
  87. (offset & (PAGE_SIZE - 1));
  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. static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
  98. {
  99. struct ib_event event;
  100. struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
  101. if (type == MLX4_EVENT_TYPE_PATH_MIG)
  102. to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
  103. if (ibqp->event_handler) {
  104. event.device = ibqp->device;
  105. event.element.qp = ibqp;
  106. switch (type) {
  107. case MLX4_EVENT_TYPE_PATH_MIG:
  108. event.event = IB_EVENT_PATH_MIG;
  109. break;
  110. case MLX4_EVENT_TYPE_COMM_EST:
  111. event.event = IB_EVENT_COMM_EST;
  112. break;
  113. case MLX4_EVENT_TYPE_SQ_DRAINED:
  114. event.event = IB_EVENT_SQ_DRAINED;
  115. break;
  116. case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
  117. event.event = IB_EVENT_QP_LAST_WQE_REACHED;
  118. break;
  119. case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
  120. event.event = IB_EVENT_QP_FATAL;
  121. break;
  122. case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
  123. event.event = IB_EVENT_PATH_MIG_ERR;
  124. break;
  125. case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
  126. event.event = IB_EVENT_QP_REQ_ERR;
  127. break;
  128. case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
  129. event.event = IB_EVENT_QP_ACCESS_ERR;
  130. break;
  131. default:
  132. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  133. "on QP %06x\n", type, qp->qpn);
  134. return;
  135. }
  136. ibqp->event_handler(&event, ibqp->qp_context);
  137. }
  138. }
  139. static int send_wqe_overhead(enum ib_qp_type type)
  140. {
  141. /*
  142. * UD WQEs must have a datagram segment.
  143. * RC and UC WQEs might have a remote address segment.
  144. * MLX WQEs need two extra inline data segments (for the UD
  145. * header and space for the ICRC).
  146. */
  147. switch (type) {
  148. case IB_QPT_UD:
  149. return sizeof (struct mlx4_wqe_ctrl_seg) +
  150. sizeof (struct mlx4_wqe_datagram_seg);
  151. case IB_QPT_UC:
  152. return sizeof (struct mlx4_wqe_ctrl_seg) +
  153. sizeof (struct mlx4_wqe_raddr_seg);
  154. case IB_QPT_RC:
  155. return sizeof (struct mlx4_wqe_ctrl_seg) +
  156. sizeof (struct mlx4_wqe_atomic_seg) +
  157. sizeof (struct mlx4_wqe_raddr_seg);
  158. case IB_QPT_SMI:
  159. case IB_QPT_GSI:
  160. return sizeof (struct mlx4_wqe_ctrl_seg) +
  161. ALIGN(MLX4_IB_UD_HEADER_SIZE +
  162. sizeof (struct mlx4_wqe_inline_seg),
  163. sizeof (struct mlx4_wqe_data_seg)) +
  164. ALIGN(4 +
  165. sizeof (struct mlx4_wqe_inline_seg),
  166. sizeof (struct mlx4_wqe_data_seg));
  167. default:
  168. return sizeof (struct mlx4_wqe_ctrl_seg);
  169. }
  170. }
  171. static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  172. int is_user, int has_srq, struct mlx4_ib_qp *qp)
  173. {
  174. /* Sanity check RQ size before proceeding */
  175. if (cap->max_recv_wr > dev->dev->caps.max_wqes ||
  176. cap->max_recv_sge > dev->dev->caps.max_rq_sg)
  177. return -EINVAL;
  178. if (has_srq) {
  179. /* QPs attached to an SRQ should have no RQ */
  180. if (cap->max_recv_wr)
  181. return -EINVAL;
  182. qp->rq.max = qp->rq.max_gs = 0;
  183. } else {
  184. /* HW requires >= 1 RQ entry with >= 1 gather entry */
  185. if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge))
  186. return -EINVAL;
  187. qp->rq.max = roundup_pow_of_two(max(1, cap->max_recv_wr));
  188. qp->rq.max_gs = roundup_pow_of_two(max(1, cap->max_recv_sge));
  189. qp->rq.wqe_shift = ilog2(qp->rq.max_gs * sizeof (struct mlx4_wqe_data_seg));
  190. }
  191. cap->max_recv_wr = qp->rq.max;
  192. cap->max_recv_sge = qp->rq.max_gs;
  193. return 0;
  194. }
  195. static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
  196. enum ib_qp_type type, struct mlx4_ib_qp *qp)
  197. {
  198. /* Sanity check SQ size before proceeding */
  199. if (cap->max_send_wr > dev->dev->caps.max_wqes ||
  200. cap->max_send_sge > dev->dev->caps.max_sq_sg ||
  201. cap->max_inline_data + send_wqe_overhead(type) +
  202. sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
  203. return -EINVAL;
  204. /*
  205. * For MLX transport we need 2 extra S/G entries:
  206. * one for the header and one for the checksum at the end
  207. */
  208. if ((type == IB_QPT_SMI || type == IB_QPT_GSI) &&
  209. cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
  210. return -EINVAL;
  211. qp->sq.max = cap->max_send_wr ? roundup_pow_of_two(cap->max_send_wr) : 1;
  212. qp->sq.wqe_shift = ilog2(roundup_pow_of_two(max(cap->max_send_sge *
  213. sizeof (struct mlx4_wqe_data_seg),
  214. cap->max_inline_data +
  215. sizeof (struct mlx4_wqe_inline_seg)) +
  216. send_wqe_overhead(type)));
  217. qp->sq.max_gs = ((1 << qp->sq.wqe_shift) - send_wqe_overhead(type)) /
  218. sizeof (struct mlx4_wqe_data_seg);
  219. qp->buf_size = (qp->rq.max << qp->rq.wqe_shift) +
  220. (qp->sq.max << qp->sq.wqe_shift);
  221. if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
  222. qp->rq.offset = 0;
  223. qp->sq.offset = qp->rq.max << qp->rq.wqe_shift;
  224. } else {
  225. qp->rq.offset = qp->sq.max << qp->sq.wqe_shift;
  226. qp->sq.offset = 0;
  227. }
  228. cap->max_send_wr = qp->sq.max;
  229. cap->max_send_sge = qp->sq.max_gs;
  230. cap->max_inline_data = (1 << qp->sq.wqe_shift) - send_wqe_overhead(type) -
  231. sizeof (struct mlx4_wqe_inline_seg);
  232. return 0;
  233. }
  234. static int set_user_sq_size(struct mlx4_ib_qp *qp,
  235. struct mlx4_ib_create_qp *ucmd)
  236. {
  237. qp->sq.max = 1 << ucmd->log_sq_bb_count;
  238. qp->sq.wqe_shift = ucmd->log_sq_stride;
  239. qp->buf_size = (qp->rq.max << qp->rq.wqe_shift) +
  240. (qp->sq.max << qp->sq.wqe_shift);
  241. return 0;
  242. }
  243. static int create_qp_common(struct mlx4_ib_dev *dev, struct ib_pd *pd,
  244. struct ib_qp_init_attr *init_attr,
  245. struct ib_udata *udata, int sqpn, struct mlx4_ib_qp *qp)
  246. {
  247. int err;
  248. mutex_init(&qp->mutex);
  249. spin_lock_init(&qp->sq.lock);
  250. spin_lock_init(&qp->rq.lock);
  251. qp->state = IB_QPS_RESET;
  252. qp->atomic_rd_en = 0;
  253. qp->resp_depth = 0;
  254. qp->rq.head = 0;
  255. qp->rq.tail = 0;
  256. qp->sq.head = 0;
  257. qp->sq.tail = 0;
  258. err = set_rq_size(dev, &init_attr->cap, !!pd->uobject, !!init_attr->srq, qp);
  259. if (err)
  260. goto err;
  261. if (pd->uobject) {
  262. struct mlx4_ib_create_qp ucmd;
  263. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  264. err = -EFAULT;
  265. goto err;
  266. }
  267. err = set_user_sq_size(qp, &ucmd);
  268. if (err)
  269. goto err;
  270. qp->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  271. qp->buf_size, 0);
  272. if (IS_ERR(qp->umem)) {
  273. err = PTR_ERR(qp->umem);
  274. goto err;
  275. }
  276. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(qp->umem),
  277. ilog2(qp->umem->page_size), &qp->mtt);
  278. if (err)
  279. goto err_buf;
  280. err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
  281. if (err)
  282. goto err_mtt;
  283. if (!init_attr->srq) {
  284. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  285. ucmd.db_addr, &qp->db);
  286. if (err)
  287. goto err_mtt;
  288. }
  289. } else {
  290. err = set_kernel_sq_size(dev, &init_attr->cap, init_attr->qp_type, qp);
  291. if (err)
  292. goto err;
  293. if (!init_attr->srq) {
  294. err = mlx4_ib_db_alloc(dev, &qp->db, 0);
  295. if (err)
  296. goto err;
  297. *qp->db.db = 0;
  298. }
  299. if (mlx4_buf_alloc(dev->dev, qp->buf_size, PAGE_SIZE * 2, &qp->buf)) {
  300. err = -ENOMEM;
  301. goto err_db;
  302. }
  303. err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
  304. &qp->mtt);
  305. if (err)
  306. goto err_buf;
  307. err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
  308. if (err)
  309. goto err_mtt;
  310. qp->sq.wrid = kmalloc(qp->sq.max * sizeof (u64), GFP_KERNEL);
  311. qp->rq.wrid = kmalloc(qp->rq.max * sizeof (u64), GFP_KERNEL);
  312. if (!qp->sq.wrid || !qp->rq.wrid) {
  313. err = -ENOMEM;
  314. goto err_wrid;
  315. }
  316. /* We don't support inline sends for kernel QPs (yet) */
  317. init_attr->cap.max_inline_data = 0;
  318. }
  319. err = mlx4_qp_alloc(dev->dev, sqpn, &qp->mqp);
  320. if (err)
  321. goto err_wrid;
  322. /*
  323. * Hardware wants QPN written in big-endian order (after
  324. * shifting) for send doorbell. Precompute this value to save
  325. * a little bit when posting sends.
  326. */
  327. qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
  328. if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
  329. qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  330. else
  331. qp->sq_signal_bits = 0;
  332. qp->mqp.event = mlx4_ib_qp_event;
  333. return 0;
  334. err_wrid:
  335. if (pd->uobject && !init_attr->srq)
  336. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &qp->db);
  337. else {
  338. kfree(qp->sq.wrid);
  339. kfree(qp->rq.wrid);
  340. }
  341. err_mtt:
  342. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  343. err_buf:
  344. if (pd->uobject)
  345. ib_umem_release(qp->umem);
  346. else
  347. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  348. err_db:
  349. if (!pd->uobject && !init_attr->srq)
  350. mlx4_ib_db_free(dev, &qp->db);
  351. err:
  352. return err;
  353. }
  354. static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
  355. {
  356. switch (state) {
  357. case IB_QPS_RESET: return MLX4_QP_STATE_RST;
  358. case IB_QPS_INIT: return MLX4_QP_STATE_INIT;
  359. case IB_QPS_RTR: return MLX4_QP_STATE_RTR;
  360. case IB_QPS_RTS: return MLX4_QP_STATE_RTS;
  361. case IB_QPS_SQD: return MLX4_QP_STATE_SQD;
  362. case IB_QPS_SQE: return MLX4_QP_STATE_SQER;
  363. case IB_QPS_ERR: return MLX4_QP_STATE_ERR;
  364. default: return -1;
  365. }
  366. }
  367. static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  368. {
  369. if (send_cq == recv_cq)
  370. spin_lock_irq(&send_cq->lock);
  371. else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  372. spin_lock_irq(&send_cq->lock);
  373. spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
  374. } else {
  375. spin_lock_irq(&recv_cq->lock);
  376. spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
  377. }
  378. }
  379. static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
  380. {
  381. if (send_cq == recv_cq)
  382. spin_unlock_irq(&send_cq->lock);
  383. else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
  384. spin_unlock(&recv_cq->lock);
  385. spin_unlock_irq(&send_cq->lock);
  386. } else {
  387. spin_unlock(&send_cq->lock);
  388. spin_unlock_irq(&recv_cq->lock);
  389. }
  390. }
  391. static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
  392. int is_user)
  393. {
  394. struct mlx4_ib_cq *send_cq, *recv_cq;
  395. if (qp->state != IB_QPS_RESET)
  396. if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
  397. MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
  398. printk(KERN_WARNING "mlx4_ib: modify QP %06x to RESET failed.\n",
  399. qp->mqp.qpn);
  400. send_cq = to_mcq(qp->ibqp.send_cq);
  401. recv_cq = to_mcq(qp->ibqp.recv_cq);
  402. mlx4_ib_lock_cqs(send_cq, recv_cq);
  403. if (!is_user) {
  404. __mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
  405. qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
  406. if (send_cq != recv_cq)
  407. __mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
  408. }
  409. mlx4_qp_remove(dev->dev, &qp->mqp);
  410. mlx4_ib_unlock_cqs(send_cq, recv_cq);
  411. mlx4_qp_free(dev->dev, &qp->mqp);
  412. mlx4_mtt_cleanup(dev->dev, &qp->mtt);
  413. if (is_user) {
  414. if (!qp->ibqp.srq)
  415. mlx4_ib_db_unmap_user(to_mucontext(qp->ibqp.uobject->context),
  416. &qp->db);
  417. ib_umem_release(qp->umem);
  418. } else {
  419. kfree(qp->sq.wrid);
  420. kfree(qp->rq.wrid);
  421. mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
  422. if (!qp->ibqp.srq)
  423. mlx4_ib_db_free(dev, &qp->db);
  424. }
  425. }
  426. struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
  427. struct ib_qp_init_attr *init_attr,
  428. struct ib_udata *udata)
  429. {
  430. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  431. struct mlx4_ib_sqp *sqp;
  432. struct mlx4_ib_qp *qp;
  433. int err;
  434. switch (init_attr->qp_type) {
  435. case IB_QPT_RC:
  436. case IB_QPT_UC:
  437. case IB_QPT_UD:
  438. {
  439. qp = kmalloc(sizeof *qp, GFP_KERNEL);
  440. if (!qp)
  441. return ERR_PTR(-ENOMEM);
  442. err = create_qp_common(dev, pd, init_attr, udata, 0, qp);
  443. if (err) {
  444. kfree(qp);
  445. return ERR_PTR(err);
  446. }
  447. qp->ibqp.qp_num = qp->mqp.qpn;
  448. break;
  449. }
  450. case IB_QPT_SMI:
  451. case IB_QPT_GSI:
  452. {
  453. /* Userspace is not allowed to create special QPs: */
  454. if (pd->uobject)
  455. return ERR_PTR(-EINVAL);
  456. sqp = kmalloc(sizeof *sqp, GFP_KERNEL);
  457. if (!sqp)
  458. return ERR_PTR(-ENOMEM);
  459. qp = &sqp->qp;
  460. err = create_qp_common(dev, pd, init_attr, udata,
  461. dev->dev->caps.sqp_start +
  462. (init_attr->qp_type == IB_QPT_SMI ? 0 : 2) +
  463. init_attr->port_num - 1,
  464. qp);
  465. if (err) {
  466. kfree(sqp);
  467. return ERR_PTR(err);
  468. }
  469. qp->port = init_attr->port_num;
  470. qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 : 1;
  471. break;
  472. }
  473. default:
  474. /* Don't support raw QPs */
  475. return ERR_PTR(-EINVAL);
  476. }
  477. return &qp->ibqp;
  478. }
  479. int mlx4_ib_destroy_qp(struct ib_qp *qp)
  480. {
  481. struct mlx4_ib_dev *dev = to_mdev(qp->device);
  482. struct mlx4_ib_qp *mqp = to_mqp(qp);
  483. if (is_qp0(dev, mqp))
  484. mlx4_CLOSE_PORT(dev->dev, mqp->port);
  485. destroy_qp_common(dev, mqp, !!qp->pd->uobject);
  486. if (is_sqp(dev, mqp))
  487. kfree(to_msqp(mqp));
  488. else
  489. kfree(mqp);
  490. return 0;
  491. }
  492. static void init_port(struct mlx4_ib_dev *dev, int port)
  493. {
  494. struct mlx4_init_port_param param;
  495. int err;
  496. memset(&param, 0, sizeof param);
  497. param.port_width_cap = dev->dev->caps.port_width_cap;
  498. param.vl_cap = dev->dev->caps.vl_cap;
  499. param.mtu = ib_mtu_enum_to_int(dev->dev->caps.mtu_cap);
  500. param.max_gid = dev->dev->caps.gid_table_len;
  501. param.max_pkey = dev->dev->caps.pkey_table_len;
  502. err = mlx4_INIT_PORT(dev->dev, &param, port);
  503. if (err)
  504. printk(KERN_WARNING "INIT_PORT failed, return code %d.\n", err);
  505. }
  506. static int to_mlx4_st(enum ib_qp_type type)
  507. {
  508. switch (type) {
  509. case IB_QPT_RC: return MLX4_QP_ST_RC;
  510. case IB_QPT_UC: return MLX4_QP_ST_UC;
  511. case IB_QPT_UD: return MLX4_QP_ST_UD;
  512. case IB_QPT_SMI:
  513. case IB_QPT_GSI: return MLX4_QP_ST_MLX;
  514. default: return -1;
  515. }
  516. }
  517. static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
  518. int attr_mask)
  519. {
  520. u8 dest_rd_atomic;
  521. u32 access_flags;
  522. u32 hw_access_flags = 0;
  523. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  524. dest_rd_atomic = attr->max_dest_rd_atomic;
  525. else
  526. dest_rd_atomic = qp->resp_depth;
  527. if (attr_mask & IB_QP_ACCESS_FLAGS)
  528. access_flags = attr->qp_access_flags;
  529. else
  530. access_flags = qp->atomic_rd_en;
  531. if (!dest_rd_atomic)
  532. access_flags &= IB_ACCESS_REMOTE_WRITE;
  533. if (access_flags & IB_ACCESS_REMOTE_READ)
  534. hw_access_flags |= MLX4_QP_BIT_RRE;
  535. if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
  536. hw_access_flags |= MLX4_QP_BIT_RAE;
  537. if (access_flags & IB_ACCESS_REMOTE_WRITE)
  538. hw_access_flags |= MLX4_QP_BIT_RWE;
  539. return cpu_to_be32(hw_access_flags);
  540. }
  541. static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
  542. int attr_mask)
  543. {
  544. if (attr_mask & IB_QP_PKEY_INDEX)
  545. sqp->pkey_index = attr->pkey_index;
  546. if (attr_mask & IB_QP_QKEY)
  547. sqp->qkey = attr->qkey;
  548. if (attr_mask & IB_QP_SQ_PSN)
  549. sqp->send_psn = attr->sq_psn;
  550. }
  551. static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
  552. {
  553. path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
  554. }
  555. static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_ah_attr *ah,
  556. struct mlx4_qp_path *path, u8 port)
  557. {
  558. path->grh_mylmc = ah->src_path_bits & 0x7f;
  559. path->rlid = cpu_to_be16(ah->dlid);
  560. if (ah->static_rate) {
  561. path->static_rate = ah->static_rate + MLX4_STAT_RATE_OFFSET;
  562. while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
  563. !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
  564. --path->static_rate;
  565. } else
  566. path->static_rate = 0;
  567. path->counter_index = 0xff;
  568. if (ah->ah_flags & IB_AH_GRH) {
  569. if (ah->grh.sgid_index >= dev->dev->caps.gid_table_len) {
  570. printk(KERN_ERR "sgid_index (%u) too large. max is %d\n",
  571. ah->grh.sgid_index, dev->dev->caps.gid_table_len - 1);
  572. return -1;
  573. }
  574. path->grh_mylmc |= 1 << 7;
  575. path->mgid_index = ah->grh.sgid_index;
  576. path->hop_limit = ah->grh.hop_limit;
  577. path->tclass_flowlabel =
  578. cpu_to_be32((ah->grh.traffic_class << 20) |
  579. (ah->grh.flow_label));
  580. memcpy(path->rgid, ah->grh.dgid.raw, 16);
  581. }
  582. path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
  583. ((port - 1) << 6) | ((ah->sl & 0xf) << 2);
  584. return 0;
  585. }
  586. static int __mlx4_ib_modify_qp(struct ib_qp *ibqp,
  587. const struct ib_qp_attr *attr, int attr_mask,
  588. enum ib_qp_state cur_state, enum ib_qp_state new_state)
  589. {
  590. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  591. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  592. struct mlx4_qp_context *context;
  593. enum mlx4_qp_optpar optpar = 0;
  594. int sqd_event;
  595. int err = -EINVAL;
  596. context = kzalloc(sizeof *context, GFP_KERNEL);
  597. if (!context)
  598. return -ENOMEM;
  599. context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
  600. (to_mlx4_st(ibqp->qp_type) << 16));
  601. context->flags |= cpu_to_be32(1 << 8); /* DE? */
  602. if (!(attr_mask & IB_QP_PATH_MIG_STATE))
  603. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  604. else {
  605. optpar |= MLX4_QP_OPTPAR_PM_STATE;
  606. switch (attr->path_mig_state) {
  607. case IB_MIG_MIGRATED:
  608. context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
  609. break;
  610. case IB_MIG_REARM:
  611. context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
  612. break;
  613. case IB_MIG_ARMED:
  614. context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
  615. break;
  616. }
  617. }
  618. if (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  619. ibqp->qp_type == IB_QPT_UD)
  620. context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
  621. else if (attr_mask & IB_QP_PATH_MTU) {
  622. if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
  623. printk(KERN_ERR "path MTU (%u) is invalid\n",
  624. attr->path_mtu);
  625. return -EINVAL;
  626. }
  627. context->mtu_msgmax = (attr->path_mtu << 5) | 31;
  628. }
  629. if (qp->rq.max)
  630. context->rq_size_stride = ilog2(qp->rq.max) << 3;
  631. context->rq_size_stride |= qp->rq.wqe_shift - 4;
  632. if (qp->sq.max)
  633. context->sq_size_stride = ilog2(qp->sq.max) << 3;
  634. context->sq_size_stride |= qp->sq.wqe_shift - 4;
  635. if (qp->ibqp.uobject)
  636. context->usr_page = cpu_to_be32(to_mucontext(ibqp->uobject->context)->uar.index);
  637. else
  638. context->usr_page = cpu_to_be32(dev->priv_uar.index);
  639. if (attr_mask & IB_QP_DEST_QPN)
  640. context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
  641. if (attr_mask & IB_QP_PORT) {
  642. if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
  643. !(attr_mask & IB_QP_AV)) {
  644. mlx4_set_sched(&context->pri_path, attr->port_num);
  645. optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
  646. }
  647. }
  648. if (attr_mask & IB_QP_PKEY_INDEX) {
  649. context->pri_path.pkey_index = attr->pkey_index;
  650. optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
  651. }
  652. if (attr_mask & IB_QP_AV) {
  653. if (mlx4_set_path(dev, &attr->ah_attr, &context->pri_path,
  654. attr_mask & IB_QP_PORT ? attr->port_num : qp->port)) {
  655. err = -EINVAL;
  656. goto out;
  657. }
  658. optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
  659. MLX4_QP_OPTPAR_SCHED_QUEUE);
  660. }
  661. if (attr_mask & IB_QP_TIMEOUT) {
  662. context->pri_path.ackto = attr->timeout << 3;
  663. optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
  664. }
  665. if (attr_mask & IB_QP_ALT_PATH) {
  666. if (attr->alt_pkey_index >= dev->dev->caps.pkey_table_len)
  667. return -EINVAL;
  668. if (attr->alt_port_num == 0 ||
  669. attr->alt_port_num > dev->dev->caps.num_ports)
  670. return -EINVAL;
  671. if (mlx4_set_path(dev, &attr->alt_ah_attr, &context->alt_path,
  672. attr->alt_port_num))
  673. return -EINVAL;
  674. context->alt_path.pkey_index = attr->alt_pkey_index;
  675. context->alt_path.ackto = attr->alt_timeout << 3;
  676. optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
  677. }
  678. context->pd = cpu_to_be32(to_mpd(ibqp->pd)->pdn);
  679. context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
  680. if (attr_mask & IB_QP_RNR_RETRY) {
  681. context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
  682. optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
  683. }
  684. if (attr_mask & IB_QP_RETRY_CNT) {
  685. context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
  686. optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
  687. }
  688. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
  689. if (attr->max_rd_atomic)
  690. context->params1 |=
  691. cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
  692. optpar |= MLX4_QP_OPTPAR_SRA_MAX;
  693. }
  694. if (attr_mask & IB_QP_SQ_PSN)
  695. context->next_send_psn = cpu_to_be32(attr->sq_psn);
  696. context->cqn_send = cpu_to_be32(to_mcq(ibqp->send_cq)->mcq.cqn);
  697. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
  698. if (attr->max_dest_rd_atomic)
  699. context->params2 |=
  700. cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
  701. optpar |= MLX4_QP_OPTPAR_RRA_MAX;
  702. }
  703. if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
  704. context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
  705. optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
  706. }
  707. if (ibqp->srq)
  708. context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
  709. if (attr_mask & IB_QP_MIN_RNR_TIMER) {
  710. context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
  711. optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
  712. }
  713. if (attr_mask & IB_QP_RQ_PSN)
  714. context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
  715. context->cqn_recv = cpu_to_be32(to_mcq(ibqp->recv_cq)->mcq.cqn);
  716. if (attr_mask & IB_QP_QKEY) {
  717. context->qkey = cpu_to_be32(attr->qkey);
  718. optpar |= MLX4_QP_OPTPAR_Q_KEY;
  719. }
  720. if (ibqp->srq)
  721. context->srqn = cpu_to_be32(1 << 24 | to_msrq(ibqp->srq)->msrq.srqn);
  722. if (!ibqp->srq && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
  723. context->db_rec_addr = cpu_to_be64(qp->db.dma);
  724. if (cur_state == IB_QPS_INIT &&
  725. new_state == IB_QPS_RTR &&
  726. (ibqp->qp_type == IB_QPT_GSI || ibqp->qp_type == IB_QPT_SMI ||
  727. ibqp->qp_type == IB_QPT_UD)) {
  728. context->pri_path.sched_queue = (qp->port - 1) << 6;
  729. if (is_qp0(dev, qp))
  730. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
  731. else
  732. context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
  733. }
  734. if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD &&
  735. attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
  736. sqd_event = 1;
  737. else
  738. sqd_event = 0;
  739. /*
  740. * Before passing a kernel QP to the HW, make sure that the
  741. * ownership bits of the send queue are set so that the
  742. * hardware doesn't start processing stale work requests.
  743. */
  744. if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
  745. struct mlx4_wqe_ctrl_seg *ctrl;
  746. int i;
  747. for (i = 0; i < qp->sq.max; ++i) {
  748. ctrl = get_send_wqe(qp, i);
  749. ctrl->owner_opcode = cpu_to_be32(1 << 31);
  750. }
  751. }
  752. err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
  753. to_mlx4_state(new_state), context, optpar,
  754. sqd_event, &qp->mqp);
  755. if (err)
  756. goto out;
  757. qp->state = new_state;
  758. if (attr_mask & IB_QP_ACCESS_FLAGS)
  759. qp->atomic_rd_en = attr->qp_access_flags;
  760. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  761. qp->resp_depth = attr->max_dest_rd_atomic;
  762. if (attr_mask & IB_QP_PORT)
  763. qp->port = attr->port_num;
  764. if (attr_mask & IB_QP_ALT_PATH)
  765. qp->alt_port = attr->alt_port_num;
  766. if (is_sqp(dev, qp))
  767. store_sqp_attrs(to_msqp(qp), attr, attr_mask);
  768. /*
  769. * If we moved QP0 to RTR, bring the IB link up; if we moved
  770. * QP0 to RESET or ERROR, bring the link back down.
  771. */
  772. if (is_qp0(dev, qp)) {
  773. if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
  774. init_port(dev, qp->port);
  775. if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
  776. (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
  777. mlx4_CLOSE_PORT(dev->dev, qp->port);
  778. }
  779. /*
  780. * If we moved a kernel QP to RESET, clean up all old CQ
  781. * entries and reinitialize the QP.
  782. */
  783. if (new_state == IB_QPS_RESET && !ibqp->uobject) {
  784. mlx4_ib_cq_clean(to_mcq(ibqp->recv_cq), qp->mqp.qpn,
  785. ibqp->srq ? to_msrq(ibqp->srq): NULL);
  786. if (ibqp->send_cq != ibqp->recv_cq)
  787. mlx4_ib_cq_clean(to_mcq(ibqp->send_cq), qp->mqp.qpn, NULL);
  788. qp->rq.head = 0;
  789. qp->rq.tail = 0;
  790. qp->sq.head = 0;
  791. qp->sq.tail = 0;
  792. if (!ibqp->srq)
  793. *qp->db.db = 0;
  794. }
  795. out:
  796. kfree(context);
  797. return err;
  798. }
  799. static const struct ib_qp_attr mlx4_ib_qp_attr = { .port_num = 1 };
  800. static const int mlx4_ib_qp_attr_mask_table[IB_QPT_UD + 1] = {
  801. [IB_QPT_UD] = (IB_QP_PKEY_INDEX |
  802. IB_QP_PORT |
  803. IB_QP_QKEY),
  804. [IB_QPT_UC] = (IB_QP_PKEY_INDEX |
  805. IB_QP_PORT |
  806. IB_QP_ACCESS_FLAGS),
  807. [IB_QPT_RC] = (IB_QP_PKEY_INDEX |
  808. IB_QP_PORT |
  809. IB_QP_ACCESS_FLAGS),
  810. [IB_QPT_SMI] = (IB_QP_PKEY_INDEX |
  811. IB_QP_QKEY),
  812. [IB_QPT_GSI] = (IB_QP_PKEY_INDEX |
  813. IB_QP_QKEY),
  814. };
  815. int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
  816. int attr_mask, struct ib_udata *udata)
  817. {
  818. struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
  819. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  820. enum ib_qp_state cur_state, new_state;
  821. int err = -EINVAL;
  822. mutex_lock(&qp->mutex);
  823. cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
  824. new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
  825. if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type, attr_mask))
  826. goto out;
  827. if ((attr_mask & IB_QP_PKEY_INDEX) &&
  828. attr->pkey_index >= dev->dev->caps.pkey_table_len) {
  829. goto out;
  830. }
  831. if ((attr_mask & IB_QP_PORT) &&
  832. (attr->port_num == 0 || attr->port_num > dev->dev->caps.num_ports)) {
  833. goto out;
  834. }
  835. if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
  836. attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
  837. goto out;
  838. }
  839. if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
  840. attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
  841. goto out;
  842. }
  843. if (cur_state == new_state && cur_state == IB_QPS_RESET) {
  844. err = 0;
  845. goto out;
  846. }
  847. if (cur_state == IB_QPS_RESET && new_state == IB_QPS_ERR) {
  848. err = __mlx4_ib_modify_qp(ibqp, &mlx4_ib_qp_attr,
  849. mlx4_ib_qp_attr_mask_table[ibqp->qp_type],
  850. IB_QPS_RESET, IB_QPS_INIT);
  851. if (err)
  852. goto out;
  853. cur_state = IB_QPS_INIT;
  854. }
  855. err = __mlx4_ib_modify_qp(ibqp, attr, attr_mask, cur_state, new_state);
  856. out:
  857. mutex_unlock(&qp->mutex);
  858. return err;
  859. }
  860. static int build_mlx_header(struct mlx4_ib_sqp *sqp, struct ib_send_wr *wr,
  861. void *wqe)
  862. {
  863. struct ib_device *ib_dev = &to_mdev(sqp->qp.ibqp.device)->ib_dev;
  864. struct mlx4_wqe_mlx_seg *mlx = wqe;
  865. struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
  866. struct mlx4_ib_ah *ah = to_mah(wr->wr.ud.ah);
  867. u16 pkey;
  868. int send_size;
  869. int header_size;
  870. int i;
  871. send_size = 0;
  872. for (i = 0; i < wr->num_sge; ++i)
  873. send_size += wr->sg_list[i].length;
  874. ib_ud_header_init(send_size, mlx4_ib_ah_grh_present(ah), &sqp->ud_header);
  875. sqp->ud_header.lrh.service_level =
  876. be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 28;
  877. sqp->ud_header.lrh.destination_lid = ah->av.dlid;
  878. sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.g_slid & 0x7f);
  879. if (mlx4_ib_ah_grh_present(ah)) {
  880. sqp->ud_header.grh.traffic_class =
  881. (be32_to_cpu(ah->av.sl_tclass_flowlabel) >> 20) & 0xff;
  882. sqp->ud_header.grh.flow_label =
  883. ah->av.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
  884. sqp->ud_header.grh.hop_limit = ah->av.hop_limit;
  885. ib_get_cached_gid(ib_dev, be32_to_cpu(ah->av.port_pd) >> 24,
  886. ah->av.gid_index, &sqp->ud_header.grh.source_gid);
  887. memcpy(sqp->ud_header.grh.destination_gid.raw,
  888. ah->av.dgid, 16);
  889. }
  890. mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
  891. mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
  892. (sqp->ud_header.lrh.destination_lid ==
  893. IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
  894. (sqp->ud_header.lrh.service_level << 8));
  895. mlx->rlid = sqp->ud_header.lrh.destination_lid;
  896. switch (wr->opcode) {
  897. case IB_WR_SEND:
  898. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  899. sqp->ud_header.immediate_present = 0;
  900. break;
  901. case IB_WR_SEND_WITH_IMM:
  902. sqp->ud_header.bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  903. sqp->ud_header.immediate_present = 1;
  904. sqp->ud_header.immediate_data = wr->imm_data;
  905. break;
  906. default:
  907. return -EINVAL;
  908. }
  909. sqp->ud_header.lrh.virtual_lane = !sqp->qp.ibqp.qp_num ? 15 : 0;
  910. if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
  911. sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
  912. sqp->ud_header.bth.solicited_event = !!(wr->send_flags & IB_SEND_SOLICITED);
  913. if (!sqp->qp.ibqp.qp_num)
  914. ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
  915. else
  916. ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->wr.ud.pkey_index, &pkey);
  917. sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
  918. sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->wr.ud.remote_qpn);
  919. sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
  920. sqp->ud_header.deth.qkey = cpu_to_be32(wr->wr.ud.remote_qkey & 0x80000000 ?
  921. sqp->qkey : wr->wr.ud.remote_qkey);
  922. sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
  923. header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
  924. if (0) {
  925. printk(KERN_ERR "built UD header of size %d:\n", header_size);
  926. for (i = 0; i < header_size / 4; ++i) {
  927. if (i % 8 == 0)
  928. printk(" [%02x] ", i * 4);
  929. printk(" %08x",
  930. be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
  931. if ((i + 1) % 8 == 0)
  932. printk("\n");
  933. }
  934. printk("\n");
  935. }
  936. inl->byte_count = cpu_to_be32(1 << 31 | header_size);
  937. memcpy(inl + 1, sqp->header_buf, header_size);
  938. return ALIGN(sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
  939. }
  940. static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
  941. {
  942. unsigned cur;
  943. struct mlx4_ib_cq *cq;
  944. cur = wq->head - wq->tail;
  945. if (likely(cur + nreq < wq->max))
  946. return 0;
  947. cq = to_mcq(ib_cq);
  948. spin_lock(&cq->lock);
  949. cur = wq->head - wq->tail;
  950. spin_unlock(&cq->lock);
  951. return cur + nreq >= wq->max;
  952. }
  953. int mlx4_ib_post_send(struct ib_qp *ibqp, struct ib_send_wr *wr,
  954. struct ib_send_wr **bad_wr)
  955. {
  956. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  957. void *wqe;
  958. struct mlx4_wqe_ctrl_seg *ctrl;
  959. unsigned long flags;
  960. int nreq;
  961. int err = 0;
  962. int ind;
  963. int size;
  964. int i;
  965. spin_lock_irqsave(&qp->rq.lock, flags);
  966. ind = qp->sq.head;
  967. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  968. if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
  969. err = -ENOMEM;
  970. *bad_wr = wr;
  971. goto out;
  972. }
  973. if (unlikely(wr->num_sge > qp->sq.max_gs)) {
  974. err = -EINVAL;
  975. *bad_wr = wr;
  976. goto out;
  977. }
  978. ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.max - 1));
  979. qp->sq.wrid[ind & (qp->sq.max - 1)] = wr->wr_id;
  980. ctrl->srcrb_flags =
  981. (wr->send_flags & IB_SEND_SIGNALED ?
  982. cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
  983. (wr->send_flags & IB_SEND_SOLICITED ?
  984. cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
  985. qp->sq_signal_bits;
  986. if (wr->opcode == IB_WR_SEND_WITH_IMM ||
  987. wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM)
  988. ctrl->imm = wr->imm_data;
  989. else
  990. ctrl->imm = 0;
  991. wqe += sizeof *ctrl;
  992. size = sizeof *ctrl / 16;
  993. switch (ibqp->qp_type) {
  994. case IB_QPT_RC:
  995. case IB_QPT_UC:
  996. switch (wr->opcode) {
  997. case IB_WR_ATOMIC_CMP_AND_SWP:
  998. case IB_WR_ATOMIC_FETCH_AND_ADD:
  999. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1000. cpu_to_be64(wr->wr.atomic.remote_addr);
  1001. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1002. cpu_to_be32(wr->wr.atomic.rkey);
  1003. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1004. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1005. if (wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
  1006. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  1007. cpu_to_be64(wr->wr.atomic.swap);
  1008. ((struct mlx4_wqe_atomic_seg *) wqe)->compare =
  1009. cpu_to_be64(wr->wr.atomic.compare_add);
  1010. } else {
  1011. ((struct mlx4_wqe_atomic_seg *) wqe)->swap_add =
  1012. cpu_to_be64(wr->wr.atomic.compare_add);
  1013. ((struct mlx4_wqe_atomic_seg *) wqe)->compare = 0;
  1014. }
  1015. wqe += sizeof (struct mlx4_wqe_atomic_seg);
  1016. size += (sizeof (struct mlx4_wqe_raddr_seg) +
  1017. sizeof (struct mlx4_wqe_atomic_seg)) / 16;
  1018. break;
  1019. case IB_WR_RDMA_READ:
  1020. case IB_WR_RDMA_WRITE:
  1021. case IB_WR_RDMA_WRITE_WITH_IMM:
  1022. ((struct mlx4_wqe_raddr_seg *) wqe)->raddr =
  1023. cpu_to_be64(wr->wr.rdma.remote_addr);
  1024. ((struct mlx4_wqe_raddr_seg *) wqe)->rkey =
  1025. cpu_to_be32(wr->wr.rdma.rkey);
  1026. ((struct mlx4_wqe_raddr_seg *) wqe)->reserved = 0;
  1027. wqe += sizeof (struct mlx4_wqe_raddr_seg);
  1028. size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
  1029. break;
  1030. default:
  1031. /* No extra segments required for sends */
  1032. break;
  1033. }
  1034. break;
  1035. case IB_QPT_UD:
  1036. memcpy(((struct mlx4_wqe_datagram_seg *) wqe)->av,
  1037. &to_mah(wr->wr.ud.ah)->av, sizeof (struct mlx4_av));
  1038. ((struct mlx4_wqe_datagram_seg *) wqe)->dqpn =
  1039. cpu_to_be32(wr->wr.ud.remote_qpn);
  1040. ((struct mlx4_wqe_datagram_seg *) wqe)->qkey =
  1041. cpu_to_be32(wr->wr.ud.remote_qkey);
  1042. wqe += sizeof (struct mlx4_wqe_datagram_seg);
  1043. size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
  1044. break;
  1045. case IB_QPT_SMI:
  1046. case IB_QPT_GSI:
  1047. err = build_mlx_header(to_msqp(qp), wr, ctrl);
  1048. if (err < 0) {
  1049. *bad_wr = wr;
  1050. goto out;
  1051. }
  1052. wqe += err;
  1053. size += err / 16;
  1054. err = 0;
  1055. break;
  1056. default:
  1057. break;
  1058. }
  1059. for (i = 0; i < wr->num_sge; ++i) {
  1060. ((struct mlx4_wqe_data_seg *) wqe)->byte_count =
  1061. cpu_to_be32(wr->sg_list[i].length);
  1062. ((struct mlx4_wqe_data_seg *) wqe)->lkey =
  1063. cpu_to_be32(wr->sg_list[i].lkey);
  1064. ((struct mlx4_wqe_data_seg *) wqe)->addr =
  1065. cpu_to_be64(wr->sg_list[i].addr);
  1066. wqe += sizeof (struct mlx4_wqe_data_seg);
  1067. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1068. }
  1069. /* Add one more inline data segment for ICRC for MLX sends */
  1070. if (qp->ibqp.qp_type == IB_QPT_SMI || qp->ibqp.qp_type == IB_QPT_GSI) {
  1071. ((struct mlx4_wqe_inline_seg *) wqe)->byte_count =
  1072. cpu_to_be32((1 << 31) | 4);
  1073. ((u32 *) wqe)[1] = 0;
  1074. wqe += sizeof (struct mlx4_wqe_data_seg);
  1075. size += sizeof (struct mlx4_wqe_data_seg) / 16;
  1076. }
  1077. ctrl->fence_size = (wr->send_flags & IB_SEND_FENCE ?
  1078. MLX4_WQE_CTRL_FENCE : 0) | size;
  1079. /*
  1080. * Make sure descriptor is fully written before
  1081. * setting ownership bit (because HW can start
  1082. * executing as soon as we do).
  1083. */
  1084. wmb();
  1085. if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
  1086. err = -EINVAL;
  1087. goto out;
  1088. }
  1089. ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
  1090. (ind & qp->sq.max ? cpu_to_be32(1 << 31) : 0);
  1091. ++ind;
  1092. }
  1093. out:
  1094. if (likely(nreq)) {
  1095. qp->sq.head += nreq;
  1096. /*
  1097. * Make sure that descriptors are written before
  1098. * doorbell record.
  1099. */
  1100. wmb();
  1101. writel(qp->doorbell_qpn,
  1102. to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
  1103. /*
  1104. * Make sure doorbells don't leak out of SQ spinlock
  1105. * and reach the HCA out of order.
  1106. */
  1107. mmiowb();
  1108. }
  1109. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1110. return err;
  1111. }
  1112. int mlx4_ib_post_recv(struct ib_qp *ibqp, struct ib_recv_wr *wr,
  1113. struct ib_recv_wr **bad_wr)
  1114. {
  1115. struct mlx4_ib_qp *qp = to_mqp(ibqp);
  1116. struct mlx4_wqe_data_seg *scat;
  1117. unsigned long flags;
  1118. int err = 0;
  1119. int nreq;
  1120. int ind;
  1121. int i;
  1122. spin_lock_irqsave(&qp->rq.lock, flags);
  1123. ind = qp->rq.head & (qp->rq.max - 1);
  1124. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  1125. if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.send_cq)) {
  1126. err = -ENOMEM;
  1127. *bad_wr = wr;
  1128. goto out;
  1129. }
  1130. if (unlikely(wr->num_sge > qp->rq.max_gs)) {
  1131. err = -EINVAL;
  1132. *bad_wr = wr;
  1133. goto out;
  1134. }
  1135. scat = get_recv_wqe(qp, ind);
  1136. for (i = 0; i < wr->num_sge; ++i) {
  1137. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  1138. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  1139. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  1140. }
  1141. if (i < qp->rq.max_gs) {
  1142. scat[i].byte_count = 0;
  1143. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  1144. scat[i].addr = 0;
  1145. }
  1146. qp->rq.wrid[ind] = wr->wr_id;
  1147. ind = (ind + 1) & (qp->rq.max - 1);
  1148. }
  1149. out:
  1150. if (likely(nreq)) {
  1151. qp->rq.head += nreq;
  1152. /*
  1153. * Make sure that descriptors are written before
  1154. * doorbell record.
  1155. */
  1156. wmb();
  1157. *qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
  1158. }
  1159. spin_unlock_irqrestore(&qp->rq.lock, flags);
  1160. return err;
  1161. }