qp.c 37 KB

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