ehca_reqs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. /*
  2. * IBM eServer eHCA Infiniband device driver for Linux on POWER
  3. *
  4. * post_send/recv, poll_cq, req_notify
  5. *
  6. * Authors: Hoang-Nam Nguyen <hnguyen@de.ibm.com>
  7. * Waleri Fomin <fomin@de.ibm.com>
  8. * Joachim Fenkes <fenkes@de.ibm.com>
  9. * Reinhard Ernst <rernst@de.ibm.com>
  10. *
  11. * Copyright (c) 2005 IBM Corporation
  12. *
  13. * All rights reserved.
  14. *
  15. * This source code is distributed under a dual license of GPL v2.0 and OpenIB
  16. * BSD.
  17. *
  18. * OpenIB BSD License
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * Redistributions of source code must retain the above copyright notice, this
  24. * list of conditions and the following disclaimer.
  25. *
  26. * Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials
  29. * provided with the distribution.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  32. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  33. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  35. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  38. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  39. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. #include <asm/system.h>
  44. #include "ehca_classes.h"
  45. #include "ehca_tools.h"
  46. #include "ehca_qes.h"
  47. #include "ehca_iverbs.h"
  48. #include "hcp_if.h"
  49. #include "hipz_fns.h"
  50. /* in RC traffic, insert an empty RDMA READ every this many packets */
  51. #define ACK_CIRC_THRESHOLD 2000000
  52. static inline int ehca_write_rwqe(struct ipz_queue *ipz_rqueue,
  53. struct ehca_wqe *wqe_p,
  54. struct ib_recv_wr *recv_wr)
  55. {
  56. u8 cnt_ds;
  57. if (unlikely((recv_wr->num_sge < 0) ||
  58. (recv_wr->num_sge > ipz_rqueue->act_nr_of_sg))) {
  59. ehca_gen_err("Invalid number of WQE SGE. "
  60. "num_sqe=%x max_nr_of_sg=%x",
  61. recv_wr->num_sge, ipz_rqueue->act_nr_of_sg);
  62. return -EINVAL; /* invalid SG list length */
  63. }
  64. /* clear wqe header until sglist */
  65. memset(wqe_p, 0, offsetof(struct ehca_wqe, u.ud_av.sg_list));
  66. wqe_p->work_request_id = recv_wr->wr_id;
  67. wqe_p->nr_of_data_seg = recv_wr->num_sge;
  68. for (cnt_ds = 0; cnt_ds < recv_wr->num_sge; cnt_ds++) {
  69. wqe_p->u.all_rcv.sg_list[cnt_ds].vaddr =
  70. recv_wr->sg_list[cnt_ds].addr;
  71. wqe_p->u.all_rcv.sg_list[cnt_ds].lkey =
  72. recv_wr->sg_list[cnt_ds].lkey;
  73. wqe_p->u.all_rcv.sg_list[cnt_ds].length =
  74. recv_wr->sg_list[cnt_ds].length;
  75. }
  76. if (ehca_debug_level >= 3) {
  77. ehca_gen_dbg("RECEIVE WQE written into ipz_rqueue=%p",
  78. ipz_rqueue);
  79. ehca_dmp(wqe_p, 16*(6 + wqe_p->nr_of_data_seg), "recv wqe");
  80. }
  81. return 0;
  82. }
  83. #if defined(DEBUG_GSI_SEND_WR)
  84. /* need ib_mad struct */
  85. #include <rdma/ib_mad.h>
  86. static void trace_send_wr_ud(const struct ib_send_wr *send_wr)
  87. {
  88. int idx;
  89. int j;
  90. while (send_wr) {
  91. struct ib_mad_hdr *mad_hdr = send_wr->wr.ud.mad_hdr;
  92. struct ib_sge *sge = send_wr->sg_list;
  93. ehca_gen_dbg("send_wr#%x wr_id=%lx num_sge=%x "
  94. "send_flags=%x opcode=%x", idx, send_wr->wr_id,
  95. send_wr->num_sge, send_wr->send_flags,
  96. send_wr->opcode);
  97. if (mad_hdr) {
  98. ehca_gen_dbg("send_wr#%x mad_hdr base_version=%x "
  99. "mgmt_class=%x class_version=%x method=%x "
  100. "status=%x class_specific=%x tid=%lx "
  101. "attr_id=%x resv=%x attr_mod=%x",
  102. idx, mad_hdr->base_version,
  103. mad_hdr->mgmt_class,
  104. mad_hdr->class_version, mad_hdr->method,
  105. mad_hdr->status, mad_hdr->class_specific,
  106. mad_hdr->tid, mad_hdr->attr_id,
  107. mad_hdr->resv,
  108. mad_hdr->attr_mod);
  109. }
  110. for (j = 0; j < send_wr->num_sge; j++) {
  111. u8 *data = (u8 *)abs_to_virt(sge->addr);
  112. ehca_gen_dbg("send_wr#%x sge#%x addr=%p length=%x "
  113. "lkey=%x",
  114. idx, j, data, sge->length, sge->lkey);
  115. /* assume length is n*16 */
  116. ehca_dmp(data, sge->length, "send_wr#%x sge#%x",
  117. idx, j);
  118. sge++;
  119. } /* eof for j */
  120. idx++;
  121. send_wr = send_wr->next;
  122. } /* eof while send_wr */
  123. }
  124. #endif /* DEBUG_GSI_SEND_WR */
  125. static inline int ehca_write_swqe(struct ehca_qp *qp,
  126. struct ehca_wqe *wqe_p,
  127. const struct ib_send_wr *send_wr,
  128. u32 sq_map_idx,
  129. int hidden)
  130. {
  131. u32 idx;
  132. u64 dma_length;
  133. struct ehca_av *my_av;
  134. u32 remote_qkey = send_wr->wr.ud.remote_qkey;
  135. if (unlikely((send_wr->num_sge < 0) ||
  136. (send_wr->num_sge > qp->ipz_squeue.act_nr_of_sg))) {
  137. ehca_gen_err("Invalid number of WQE SGE. "
  138. "num_sqe=%x max_nr_of_sg=%x",
  139. send_wr->num_sge, qp->ipz_squeue.act_nr_of_sg);
  140. return -EINVAL; /* invalid SG list length */
  141. }
  142. /* clear wqe header until sglist */
  143. memset(wqe_p, 0, offsetof(struct ehca_wqe, u.ud_av.sg_list));
  144. wqe_p->work_request_id = send_wr->wr_id & ~QMAP_IDX_MASK;
  145. wqe_p->work_request_id |= sq_map_idx & QMAP_IDX_MASK;
  146. qp->sq_map[sq_map_idx].app_wr_id = send_wr->wr_id & QMAP_IDX_MASK;
  147. qp->sq_map[sq_map_idx].reported = 0;
  148. switch (send_wr->opcode) {
  149. case IB_WR_SEND:
  150. case IB_WR_SEND_WITH_IMM:
  151. wqe_p->optype = WQE_OPTYPE_SEND;
  152. break;
  153. case IB_WR_RDMA_WRITE:
  154. case IB_WR_RDMA_WRITE_WITH_IMM:
  155. wqe_p->optype = WQE_OPTYPE_RDMAWRITE;
  156. break;
  157. case IB_WR_RDMA_READ:
  158. wqe_p->optype = WQE_OPTYPE_RDMAREAD;
  159. break;
  160. default:
  161. ehca_gen_err("Invalid opcode=%x", send_wr->opcode);
  162. return -EINVAL; /* invalid opcode */
  163. }
  164. wqe_p->wqef = (send_wr->opcode) & WQEF_HIGH_NIBBLE;
  165. wqe_p->wr_flag = 0;
  166. if ((send_wr->send_flags & IB_SEND_SIGNALED ||
  167. qp->init_attr.sq_sig_type == IB_SIGNAL_ALL_WR)
  168. && !hidden)
  169. wqe_p->wr_flag |= WQE_WRFLAG_REQ_SIGNAL_COM;
  170. if (send_wr->opcode == IB_WR_SEND_WITH_IMM ||
  171. send_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  172. /* this might not work as long as HW does not support it */
  173. wqe_p->immediate_data = be32_to_cpu(send_wr->ex.imm_data);
  174. wqe_p->wr_flag |= WQE_WRFLAG_IMM_DATA_PRESENT;
  175. }
  176. wqe_p->nr_of_data_seg = send_wr->num_sge;
  177. switch (qp->qp_type) {
  178. case IB_QPT_SMI:
  179. case IB_QPT_GSI:
  180. /* no break is intential here */
  181. case IB_QPT_UD:
  182. /* IB 1.2 spec C10-15 compliance */
  183. if (send_wr->wr.ud.remote_qkey & 0x80000000)
  184. remote_qkey = qp->qkey;
  185. wqe_p->destination_qp_number = send_wr->wr.ud.remote_qpn << 8;
  186. wqe_p->local_ee_context_qkey = remote_qkey;
  187. if (unlikely(!send_wr->wr.ud.ah)) {
  188. ehca_gen_err("wr.ud.ah is NULL. qp=%p", qp);
  189. return -EINVAL;
  190. }
  191. if (unlikely(send_wr->wr.ud.remote_qpn == 0)) {
  192. ehca_gen_err("dest QP# is 0. qp=%x", qp->real_qp_num);
  193. return -EINVAL;
  194. }
  195. my_av = container_of(send_wr->wr.ud.ah, struct ehca_av, ib_ah);
  196. wqe_p->u.ud_av.ud_av = my_av->av;
  197. /*
  198. * omitted check of IB_SEND_INLINE
  199. * since HW does not support it
  200. */
  201. for (idx = 0; idx < send_wr->num_sge; idx++) {
  202. wqe_p->u.ud_av.sg_list[idx].vaddr =
  203. send_wr->sg_list[idx].addr;
  204. wqe_p->u.ud_av.sg_list[idx].lkey =
  205. send_wr->sg_list[idx].lkey;
  206. wqe_p->u.ud_av.sg_list[idx].length =
  207. send_wr->sg_list[idx].length;
  208. } /* eof for idx */
  209. if (qp->qp_type == IB_QPT_SMI ||
  210. qp->qp_type == IB_QPT_GSI)
  211. wqe_p->u.ud_av.ud_av.pmtu = 1;
  212. if (qp->qp_type == IB_QPT_GSI) {
  213. wqe_p->pkeyi = send_wr->wr.ud.pkey_index;
  214. #ifdef DEBUG_GSI_SEND_WR
  215. trace_send_wr_ud(send_wr);
  216. #endif /* DEBUG_GSI_SEND_WR */
  217. }
  218. break;
  219. case IB_QPT_UC:
  220. if (send_wr->send_flags & IB_SEND_FENCE)
  221. wqe_p->wr_flag |= WQE_WRFLAG_FENCE;
  222. /* no break is intentional here */
  223. case IB_QPT_RC:
  224. /* TODO: atomic not implemented */
  225. wqe_p->u.nud.remote_virtual_adress =
  226. send_wr->wr.rdma.remote_addr;
  227. wqe_p->u.nud.rkey = send_wr->wr.rdma.rkey;
  228. /*
  229. * omitted checking of IB_SEND_INLINE
  230. * since HW does not support it
  231. */
  232. dma_length = 0;
  233. for (idx = 0; idx < send_wr->num_sge; idx++) {
  234. wqe_p->u.nud.sg_list[idx].vaddr =
  235. send_wr->sg_list[idx].addr;
  236. wqe_p->u.nud.sg_list[idx].lkey =
  237. send_wr->sg_list[idx].lkey;
  238. wqe_p->u.nud.sg_list[idx].length =
  239. send_wr->sg_list[idx].length;
  240. dma_length += send_wr->sg_list[idx].length;
  241. } /* eof idx */
  242. wqe_p->u.nud.atomic_1st_op_dma_len = dma_length;
  243. /* unsolicited ack circumvention */
  244. if (send_wr->opcode == IB_WR_RDMA_READ) {
  245. /* on RDMA read, switch on and reset counters */
  246. qp->message_count = qp->packet_count = 0;
  247. qp->unsol_ack_circ = 1;
  248. } else
  249. /* else estimate #packets */
  250. qp->packet_count += (dma_length >> qp->mtu_shift) + 1;
  251. break;
  252. default:
  253. ehca_gen_err("Invalid qptype=%x", qp->qp_type);
  254. return -EINVAL;
  255. }
  256. if (ehca_debug_level >= 3) {
  257. ehca_gen_dbg("SEND WQE written into queue qp=%p ", qp);
  258. ehca_dmp( wqe_p, 16*(6 + wqe_p->nr_of_data_seg), "send wqe");
  259. }
  260. return 0;
  261. }
  262. /* map_ib_wc_status converts raw cqe_status to ib_wc_status */
  263. static inline void map_ib_wc_status(u32 cqe_status,
  264. enum ib_wc_status *wc_status)
  265. {
  266. if (unlikely(cqe_status & WC_STATUS_ERROR_BIT)) {
  267. switch (cqe_status & 0x3F) {
  268. case 0x01:
  269. case 0x21:
  270. *wc_status = IB_WC_LOC_LEN_ERR;
  271. break;
  272. case 0x02:
  273. case 0x22:
  274. *wc_status = IB_WC_LOC_QP_OP_ERR;
  275. break;
  276. case 0x03:
  277. case 0x23:
  278. *wc_status = IB_WC_LOC_EEC_OP_ERR;
  279. break;
  280. case 0x04:
  281. case 0x24:
  282. *wc_status = IB_WC_LOC_PROT_ERR;
  283. break;
  284. case 0x05:
  285. case 0x25:
  286. *wc_status = IB_WC_WR_FLUSH_ERR;
  287. break;
  288. case 0x06:
  289. *wc_status = IB_WC_MW_BIND_ERR;
  290. break;
  291. case 0x07: /* remote error - look into bits 20:24 */
  292. switch ((cqe_status
  293. & WC_STATUS_REMOTE_ERROR_FLAGS) >> 11) {
  294. case 0x0:
  295. /*
  296. * PSN Sequence Error!
  297. * couldn't find a matching status!
  298. */
  299. *wc_status = IB_WC_GENERAL_ERR;
  300. break;
  301. case 0x1:
  302. *wc_status = IB_WC_REM_INV_REQ_ERR;
  303. break;
  304. case 0x2:
  305. *wc_status = IB_WC_REM_ACCESS_ERR;
  306. break;
  307. case 0x3:
  308. *wc_status = IB_WC_REM_OP_ERR;
  309. break;
  310. case 0x4:
  311. *wc_status = IB_WC_REM_INV_RD_REQ_ERR;
  312. break;
  313. }
  314. break;
  315. case 0x08:
  316. *wc_status = IB_WC_RETRY_EXC_ERR;
  317. break;
  318. case 0x09:
  319. *wc_status = IB_WC_RNR_RETRY_EXC_ERR;
  320. break;
  321. case 0x0A:
  322. case 0x2D:
  323. *wc_status = IB_WC_REM_ABORT_ERR;
  324. break;
  325. case 0x0B:
  326. case 0x2E:
  327. *wc_status = IB_WC_INV_EECN_ERR;
  328. break;
  329. case 0x0C:
  330. case 0x2F:
  331. *wc_status = IB_WC_INV_EEC_STATE_ERR;
  332. break;
  333. case 0x0D:
  334. *wc_status = IB_WC_BAD_RESP_ERR;
  335. break;
  336. case 0x10:
  337. /* WQE purged */
  338. *wc_status = IB_WC_WR_FLUSH_ERR;
  339. break;
  340. default:
  341. *wc_status = IB_WC_FATAL_ERR;
  342. }
  343. } else
  344. *wc_status = IB_WC_SUCCESS;
  345. }
  346. static inline int post_one_send(struct ehca_qp *my_qp,
  347. struct ib_send_wr *cur_send_wr,
  348. struct ib_send_wr **bad_send_wr,
  349. int hidden)
  350. {
  351. struct ehca_wqe *wqe_p;
  352. int ret;
  353. u32 sq_map_idx;
  354. u64 start_offset = my_qp->ipz_squeue.current_q_offset;
  355. /* get pointer next to free WQE */
  356. wqe_p = ipz_qeit_get_inc(&my_qp->ipz_squeue);
  357. if (unlikely(!wqe_p)) {
  358. /* too many posted work requests: queue overflow */
  359. if (bad_send_wr)
  360. *bad_send_wr = cur_send_wr;
  361. ehca_err(my_qp->ib_qp.device, "Too many posted WQEs "
  362. "qp_num=%x", my_qp->ib_qp.qp_num);
  363. return -ENOMEM;
  364. }
  365. /*
  366. * Get the index of the WQE in the send queue. The same index is used
  367. * for writing into the sq_map.
  368. */
  369. sq_map_idx = start_offset / my_qp->ipz_squeue.qe_size;
  370. /* write a SEND WQE into the QUEUE */
  371. ret = ehca_write_swqe(my_qp, wqe_p, cur_send_wr, sq_map_idx, hidden);
  372. /*
  373. * if something failed,
  374. * reset the free entry pointer to the start value
  375. */
  376. if (unlikely(ret)) {
  377. my_qp->ipz_squeue.current_q_offset = start_offset;
  378. if (bad_send_wr)
  379. *bad_send_wr = cur_send_wr;
  380. ehca_err(my_qp->ib_qp.device, "Could not write WQE "
  381. "qp_num=%x", my_qp->ib_qp.qp_num);
  382. return -EINVAL;
  383. }
  384. return 0;
  385. }
  386. int ehca_post_send(struct ib_qp *qp,
  387. struct ib_send_wr *send_wr,
  388. struct ib_send_wr **bad_send_wr)
  389. {
  390. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  391. struct ib_send_wr *cur_send_wr;
  392. int wqe_cnt = 0;
  393. int ret = 0;
  394. unsigned long flags;
  395. /* Reject WR if QP is in RESET, INIT or RTR state */
  396. if (unlikely(my_qp->state < IB_QPS_RTS)) {
  397. ehca_err(qp->device, "Invalid QP state qp_state=%d qpn=%x",
  398. my_qp->state, qp->qp_num);
  399. return -EINVAL;
  400. }
  401. /* LOCK the QUEUE */
  402. spin_lock_irqsave(&my_qp->spinlock_s, flags);
  403. /* Send an empty extra RDMA read if:
  404. * 1) there has been an RDMA read on this connection before
  405. * 2) no RDMA read occurred for ACK_CIRC_THRESHOLD link packets
  406. * 3) we can be sure that any previous extra RDMA read has been
  407. * processed so we don't overflow the SQ
  408. */
  409. if (unlikely(my_qp->unsol_ack_circ &&
  410. my_qp->packet_count > ACK_CIRC_THRESHOLD &&
  411. my_qp->message_count > my_qp->init_attr.cap.max_send_wr)) {
  412. /* insert an empty RDMA READ to fix up the remote QP state */
  413. struct ib_send_wr circ_wr;
  414. memset(&circ_wr, 0, sizeof(circ_wr));
  415. circ_wr.opcode = IB_WR_RDMA_READ;
  416. post_one_send(my_qp, &circ_wr, NULL, 1); /* ignore retcode */
  417. wqe_cnt++;
  418. ehca_dbg(qp->device, "posted circ wr qp_num=%x", qp->qp_num);
  419. my_qp->message_count = my_qp->packet_count = 0;
  420. }
  421. /* loop processes list of send reqs */
  422. for (cur_send_wr = send_wr; cur_send_wr != NULL;
  423. cur_send_wr = cur_send_wr->next) {
  424. ret = post_one_send(my_qp, cur_send_wr, bad_send_wr, 0);
  425. if (unlikely(ret)) {
  426. /* if one or more WQEs were successful, don't fail */
  427. if (wqe_cnt)
  428. ret = 0;
  429. goto post_send_exit0;
  430. }
  431. wqe_cnt++;
  432. } /* eof for cur_send_wr */
  433. post_send_exit0:
  434. iosync(); /* serialize GAL register access */
  435. hipz_update_sqa(my_qp, wqe_cnt);
  436. if (unlikely(ret || ehca_debug_level >= 2))
  437. ehca_dbg(qp->device, "ehca_qp=%p qp_num=%x wqe_cnt=%d ret=%i",
  438. my_qp, qp->qp_num, wqe_cnt, ret);
  439. my_qp->message_count += wqe_cnt;
  440. spin_unlock_irqrestore(&my_qp->spinlock_s, flags);
  441. return ret;
  442. }
  443. static int internal_post_recv(struct ehca_qp *my_qp,
  444. struct ib_device *dev,
  445. struct ib_recv_wr *recv_wr,
  446. struct ib_recv_wr **bad_recv_wr)
  447. {
  448. struct ib_recv_wr *cur_recv_wr;
  449. struct ehca_wqe *wqe_p;
  450. int wqe_cnt = 0;
  451. int ret = 0;
  452. unsigned long flags;
  453. if (unlikely(!HAS_RQ(my_qp))) {
  454. ehca_err(dev, "QP has no RQ ehca_qp=%p qp_num=%x ext_type=%d",
  455. my_qp, my_qp->real_qp_num, my_qp->ext_type);
  456. return -ENODEV;
  457. }
  458. /* LOCK the QUEUE */
  459. spin_lock_irqsave(&my_qp->spinlock_r, flags);
  460. /* loop processes list of send reqs */
  461. for (cur_recv_wr = recv_wr; cur_recv_wr != NULL;
  462. cur_recv_wr = cur_recv_wr->next) {
  463. u64 start_offset = my_qp->ipz_rqueue.current_q_offset;
  464. /* get pointer next to free WQE */
  465. wqe_p = ipz_qeit_get_inc(&my_qp->ipz_rqueue);
  466. if (unlikely(!wqe_p)) {
  467. /* too many posted work requests: queue overflow */
  468. if (bad_recv_wr)
  469. *bad_recv_wr = cur_recv_wr;
  470. if (wqe_cnt == 0) {
  471. ret = -ENOMEM;
  472. ehca_err(dev, "Too many posted WQEs "
  473. "qp_num=%x", my_qp->real_qp_num);
  474. }
  475. goto post_recv_exit0;
  476. }
  477. /* write a RECV WQE into the QUEUE */
  478. ret = ehca_write_rwqe(&my_qp->ipz_rqueue, wqe_p, cur_recv_wr);
  479. /*
  480. * if something failed,
  481. * reset the free entry pointer to the start value
  482. */
  483. if (unlikely(ret)) {
  484. my_qp->ipz_rqueue.current_q_offset = start_offset;
  485. *bad_recv_wr = cur_recv_wr;
  486. if (wqe_cnt == 0) {
  487. ret = -EINVAL;
  488. ehca_err(dev, "Could not write WQE "
  489. "qp_num=%x", my_qp->real_qp_num);
  490. }
  491. goto post_recv_exit0;
  492. }
  493. wqe_cnt++;
  494. } /* eof for cur_recv_wr */
  495. post_recv_exit0:
  496. iosync(); /* serialize GAL register access */
  497. hipz_update_rqa(my_qp, wqe_cnt);
  498. if (unlikely(ret || ehca_debug_level >= 2))
  499. ehca_dbg(dev, "ehca_qp=%p qp_num=%x wqe_cnt=%d ret=%i",
  500. my_qp, my_qp->real_qp_num, wqe_cnt, ret);
  501. spin_unlock_irqrestore(&my_qp->spinlock_r, flags);
  502. return ret;
  503. }
  504. int ehca_post_recv(struct ib_qp *qp,
  505. struct ib_recv_wr *recv_wr,
  506. struct ib_recv_wr **bad_recv_wr)
  507. {
  508. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  509. /* Reject WR if QP is in RESET state */
  510. if (unlikely(my_qp->state == IB_QPS_RESET)) {
  511. ehca_err(qp->device, "Invalid QP state qp_state=%d qpn=%x",
  512. my_qp->state, qp->qp_num);
  513. return -EINVAL;
  514. }
  515. return internal_post_recv(my_qp, qp->device, recv_wr, bad_recv_wr);
  516. }
  517. int ehca_post_srq_recv(struct ib_srq *srq,
  518. struct ib_recv_wr *recv_wr,
  519. struct ib_recv_wr **bad_recv_wr)
  520. {
  521. return internal_post_recv(container_of(srq, struct ehca_qp, ib_srq),
  522. srq->device, recv_wr, bad_recv_wr);
  523. }
  524. /*
  525. * ib_wc_opcode table converts ehca wc opcode to ib
  526. * Since we use zero to indicate invalid opcode, the actual ib opcode must
  527. * be decremented!!!
  528. */
  529. static const u8 ib_wc_opcode[255] = {
  530. [0x01] = IB_WC_RECV+1,
  531. [0x02] = IB_WC_RECV_RDMA_WITH_IMM+1,
  532. [0x04] = IB_WC_BIND_MW+1,
  533. [0x08] = IB_WC_FETCH_ADD+1,
  534. [0x10] = IB_WC_COMP_SWAP+1,
  535. [0x20] = IB_WC_RDMA_WRITE+1,
  536. [0x40] = IB_WC_RDMA_READ+1,
  537. [0x80] = IB_WC_SEND+1
  538. };
  539. /* internal function to poll one entry of cq */
  540. static inline int ehca_poll_cq_one(struct ib_cq *cq, struct ib_wc *wc)
  541. {
  542. int ret = 0;
  543. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  544. struct ehca_cqe *cqe;
  545. struct ehca_qp *my_qp;
  546. int cqe_count = 0, is_error;
  547. repoll:
  548. cqe = (struct ehca_cqe *)
  549. ipz_qeit_get_inc_valid(&my_cq->ipz_queue);
  550. if (!cqe) {
  551. ret = -EAGAIN;
  552. if (ehca_debug_level >= 3)
  553. ehca_dbg(cq->device, "Completion queue is empty "
  554. "my_cq=%p cq_num=%x", my_cq, my_cq->cq_number);
  555. goto poll_cq_one_exit0;
  556. }
  557. /* prevents loads being reordered across this point */
  558. rmb();
  559. cqe_count++;
  560. if (unlikely(cqe->status & WC_STATUS_PURGE_BIT)) {
  561. struct ehca_qp *qp;
  562. int purgeflag;
  563. unsigned long flags;
  564. qp = ehca_cq_get_qp(my_cq, cqe->local_qp_number);
  565. if (!qp) {
  566. ehca_err(cq->device, "cq_num=%x qp_num=%x "
  567. "could not find qp -> ignore cqe",
  568. my_cq->cq_number, cqe->local_qp_number);
  569. ehca_dmp(cqe, 64, "cq_num=%x qp_num=%x",
  570. my_cq->cq_number, cqe->local_qp_number);
  571. /* ignore this purged cqe */
  572. goto repoll;
  573. }
  574. spin_lock_irqsave(&qp->spinlock_s, flags);
  575. purgeflag = qp->sqerr_purgeflag;
  576. spin_unlock_irqrestore(&qp->spinlock_s, flags);
  577. if (purgeflag) {
  578. ehca_dbg(cq->device,
  579. "Got CQE with purged bit qp_num=%x src_qp=%x",
  580. cqe->local_qp_number, cqe->remote_qp_number);
  581. if (ehca_debug_level >= 2)
  582. ehca_dmp(cqe, 64, "qp_num=%x src_qp=%x",
  583. cqe->local_qp_number,
  584. cqe->remote_qp_number);
  585. /*
  586. * ignore this to avoid double cqes of bad wqe
  587. * that caused sqe and turn off purge flag
  588. */
  589. qp->sqerr_purgeflag = 0;
  590. goto repoll;
  591. }
  592. }
  593. is_error = cqe->status & WC_STATUS_ERROR_BIT;
  594. /* trace error CQEs if debug_level >= 1, trace all CQEs if >= 3 */
  595. if (unlikely(ehca_debug_level >= 3 || (ehca_debug_level && is_error))) {
  596. ehca_dbg(cq->device,
  597. "Received %sCOMPLETION ehca_cq=%p cq_num=%x -----",
  598. is_error ? "ERROR " : "", my_cq, my_cq->cq_number);
  599. ehca_dmp(cqe, 64, "ehca_cq=%p cq_num=%x",
  600. my_cq, my_cq->cq_number);
  601. ehca_dbg(cq->device,
  602. "ehca_cq=%p cq_num=%x -------------------------",
  603. my_cq, my_cq->cq_number);
  604. }
  605. read_lock(&ehca_qp_idr_lock);
  606. my_qp = idr_find(&ehca_qp_idr, cqe->qp_token);
  607. read_unlock(&ehca_qp_idr_lock);
  608. if (!my_qp)
  609. goto repoll;
  610. wc->qp = &my_qp->ib_qp;
  611. if (!(cqe->w_completion_flags & WC_SEND_RECEIVE_BIT)) {
  612. struct ehca_qmap_entry *qmap_entry;
  613. /*
  614. * We got a send completion and need to restore the original
  615. * wr_id.
  616. */
  617. qmap_entry = &my_qp->sq_map[cqe->work_request_id &
  618. QMAP_IDX_MASK];
  619. if (qmap_entry->reported) {
  620. ehca_warn(cq->device, "Double cqe on qp_num=%#x",
  621. my_qp->real_qp_num);
  622. /* found a double cqe, discard it and read next one */
  623. goto repoll;
  624. }
  625. wc->wr_id = cqe->work_request_id & ~QMAP_IDX_MASK;
  626. wc->wr_id |= qmap_entry->app_wr_id;
  627. qmap_entry->reported = 1;
  628. } else
  629. /* We got a receive completion. */
  630. wc->wr_id = cqe->work_request_id;
  631. /* eval ib_wc_opcode */
  632. wc->opcode = ib_wc_opcode[cqe->optype]-1;
  633. if (unlikely(wc->opcode == -1)) {
  634. ehca_err(cq->device, "Invalid cqe->OPType=%x cqe->status=%x "
  635. "ehca_cq=%p cq_num=%x",
  636. cqe->optype, cqe->status, my_cq, my_cq->cq_number);
  637. /* dump cqe for other infos */
  638. ehca_dmp(cqe, 64, "ehca_cq=%p cq_num=%x",
  639. my_cq, my_cq->cq_number);
  640. /* update also queue adder to throw away this entry!!! */
  641. goto repoll;
  642. }
  643. /* eval ib_wc_status */
  644. if (unlikely(is_error)) {
  645. /* complete with errors */
  646. map_ib_wc_status(cqe->status, &wc->status);
  647. wc->vendor_err = wc->status;
  648. } else
  649. wc->status = IB_WC_SUCCESS;
  650. wc->byte_len = cqe->nr_bytes_transferred;
  651. wc->pkey_index = cqe->pkey_index;
  652. wc->slid = cqe->rlid;
  653. wc->dlid_path_bits = cqe->dlid;
  654. wc->src_qp = cqe->remote_qp_number;
  655. wc->wc_flags = cqe->w_completion_flags;
  656. wc->ex.imm_data = cpu_to_be32(cqe->immediate_data);
  657. wc->sl = cqe->service_level;
  658. poll_cq_one_exit0:
  659. if (cqe_count > 0)
  660. hipz_update_feca(my_cq, cqe_count);
  661. return ret;
  662. }
  663. int ehca_poll_cq(struct ib_cq *cq, int num_entries, struct ib_wc *wc)
  664. {
  665. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  666. int nr;
  667. struct ib_wc *current_wc = wc;
  668. int ret = 0;
  669. unsigned long flags;
  670. if (num_entries < 1) {
  671. ehca_err(cq->device, "Invalid num_entries=%d ehca_cq=%p "
  672. "cq_num=%x", num_entries, my_cq, my_cq->cq_number);
  673. ret = -EINVAL;
  674. goto poll_cq_exit0;
  675. }
  676. spin_lock_irqsave(&my_cq->spinlock, flags);
  677. for (nr = 0; nr < num_entries; nr++) {
  678. ret = ehca_poll_cq_one(cq, current_wc);
  679. if (ret)
  680. break;
  681. current_wc++;
  682. } /* eof for nr */
  683. spin_unlock_irqrestore(&my_cq->spinlock, flags);
  684. if (ret == -EAGAIN || !ret)
  685. ret = nr;
  686. poll_cq_exit0:
  687. return ret;
  688. }
  689. int ehca_req_notify_cq(struct ib_cq *cq, enum ib_cq_notify_flags notify_flags)
  690. {
  691. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  692. int ret = 0;
  693. switch (notify_flags & IB_CQ_SOLICITED_MASK) {
  694. case IB_CQ_SOLICITED:
  695. hipz_set_cqx_n0(my_cq, 1);
  696. break;
  697. case IB_CQ_NEXT_COMP:
  698. hipz_set_cqx_n1(my_cq, 1);
  699. break;
  700. default:
  701. return -EINVAL;
  702. }
  703. if (notify_flags & IB_CQ_REPORT_MISSED_EVENTS) {
  704. unsigned long spl_flags;
  705. spin_lock_irqsave(&my_cq->spinlock, spl_flags);
  706. ret = ipz_qeit_is_valid(&my_cq->ipz_queue);
  707. spin_unlock_irqrestore(&my_cq->spinlock, spl_flags);
  708. }
  709. return ret;
  710. }