ehca_reqs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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-powerpc/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) {
  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. int hidden)
  129. {
  130. u32 idx;
  131. u64 dma_length;
  132. struct ehca_av *my_av;
  133. u32 remote_qkey = send_wr->wr.ud.remote_qkey;
  134. if (unlikely((send_wr->num_sge < 0) ||
  135. (send_wr->num_sge > qp->ipz_squeue.act_nr_of_sg))) {
  136. ehca_gen_err("Invalid number of WQE SGE. "
  137. "num_sqe=%x max_nr_of_sg=%x",
  138. send_wr->num_sge, qp->ipz_squeue.act_nr_of_sg);
  139. return -EINVAL; /* invalid SG list length */
  140. }
  141. /* clear wqe header until sglist */
  142. memset(wqe_p, 0, offsetof(struct ehca_wqe, u.ud_av.sg_list));
  143. wqe_p->work_request_id = send_wr->wr_id;
  144. switch (send_wr->opcode) {
  145. case IB_WR_SEND:
  146. case IB_WR_SEND_WITH_IMM:
  147. wqe_p->optype = WQE_OPTYPE_SEND;
  148. break;
  149. case IB_WR_RDMA_WRITE:
  150. case IB_WR_RDMA_WRITE_WITH_IMM:
  151. wqe_p->optype = WQE_OPTYPE_RDMAWRITE;
  152. break;
  153. case IB_WR_RDMA_READ:
  154. wqe_p->optype = WQE_OPTYPE_RDMAREAD;
  155. break;
  156. default:
  157. ehca_gen_err("Invalid opcode=%x", send_wr->opcode);
  158. return -EINVAL; /* invalid opcode */
  159. }
  160. wqe_p->wqef = (send_wr->opcode) & WQEF_HIGH_NIBBLE;
  161. wqe_p->wr_flag = 0;
  162. if ((send_wr->send_flags & IB_SEND_SIGNALED ||
  163. qp->init_attr.sq_sig_type == IB_SIGNAL_ALL_WR)
  164. && !hidden)
  165. wqe_p->wr_flag |= WQE_WRFLAG_REQ_SIGNAL_COM;
  166. if (send_wr->opcode == IB_WR_SEND_WITH_IMM ||
  167. send_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  168. /* this might not work as long as HW does not support it */
  169. wqe_p->immediate_data = be32_to_cpu(send_wr->imm_data);
  170. wqe_p->wr_flag |= WQE_WRFLAG_IMM_DATA_PRESENT;
  171. }
  172. wqe_p->nr_of_data_seg = send_wr->num_sge;
  173. switch (qp->qp_type) {
  174. case IB_QPT_SMI:
  175. case IB_QPT_GSI:
  176. /* no break is intential here */
  177. case IB_QPT_UD:
  178. /* IB 1.2 spec C10-15 compliance */
  179. if (send_wr->wr.ud.remote_qkey & 0x80000000)
  180. remote_qkey = qp->qkey;
  181. wqe_p->destination_qp_number = send_wr->wr.ud.remote_qpn << 8;
  182. wqe_p->local_ee_context_qkey = remote_qkey;
  183. if (unlikely(!send_wr->wr.ud.ah)) {
  184. ehca_gen_err("wr.ud.ah is NULL. qp=%p", qp);
  185. return -EINVAL;
  186. }
  187. my_av = container_of(send_wr->wr.ud.ah, struct ehca_av, ib_ah);
  188. wqe_p->u.ud_av.ud_av = my_av->av;
  189. /*
  190. * omitted check of IB_SEND_INLINE
  191. * since HW does not support it
  192. */
  193. for (idx = 0; idx < send_wr->num_sge; idx++) {
  194. wqe_p->u.ud_av.sg_list[idx].vaddr =
  195. send_wr->sg_list[idx].addr;
  196. wqe_p->u.ud_av.sg_list[idx].lkey =
  197. send_wr->sg_list[idx].lkey;
  198. wqe_p->u.ud_av.sg_list[idx].length =
  199. send_wr->sg_list[idx].length;
  200. } /* eof for idx */
  201. if (qp->qp_type == IB_QPT_SMI ||
  202. qp->qp_type == IB_QPT_GSI)
  203. wqe_p->u.ud_av.ud_av.pmtu = 1;
  204. if (qp->qp_type == IB_QPT_GSI) {
  205. wqe_p->pkeyi = send_wr->wr.ud.pkey_index;
  206. #ifdef DEBUG_GSI_SEND_WR
  207. trace_send_wr_ud(send_wr);
  208. #endif /* DEBUG_GSI_SEND_WR */
  209. }
  210. break;
  211. case IB_QPT_UC:
  212. if (send_wr->send_flags & IB_SEND_FENCE)
  213. wqe_p->wr_flag |= WQE_WRFLAG_FENCE;
  214. /* no break is intentional here */
  215. case IB_QPT_RC:
  216. /* TODO: atomic not implemented */
  217. wqe_p->u.nud.remote_virtual_adress =
  218. send_wr->wr.rdma.remote_addr;
  219. wqe_p->u.nud.rkey = send_wr->wr.rdma.rkey;
  220. /*
  221. * omitted checking of IB_SEND_INLINE
  222. * since HW does not support it
  223. */
  224. dma_length = 0;
  225. for (idx = 0; idx < send_wr->num_sge; idx++) {
  226. wqe_p->u.nud.sg_list[idx].vaddr =
  227. send_wr->sg_list[idx].addr;
  228. wqe_p->u.nud.sg_list[idx].lkey =
  229. send_wr->sg_list[idx].lkey;
  230. wqe_p->u.nud.sg_list[idx].length =
  231. send_wr->sg_list[idx].length;
  232. dma_length += send_wr->sg_list[idx].length;
  233. } /* eof idx */
  234. wqe_p->u.nud.atomic_1st_op_dma_len = dma_length;
  235. /* unsolicited ack circumvention */
  236. if (send_wr->opcode == IB_WR_RDMA_READ) {
  237. /* on RDMA read, switch on and reset counters */
  238. qp->message_count = qp->packet_count = 0;
  239. qp->unsol_ack_circ = 1;
  240. } else
  241. /* else estimate #packets */
  242. qp->packet_count += (dma_length >> qp->mtu_shift) + 1;
  243. break;
  244. default:
  245. ehca_gen_err("Invalid qptype=%x", qp->qp_type);
  246. return -EINVAL;
  247. }
  248. if (ehca_debug_level) {
  249. ehca_gen_dbg("SEND WQE written into queue qp=%p ", qp);
  250. ehca_dmp( wqe_p, 16*(6 + wqe_p->nr_of_data_seg), "send wqe");
  251. }
  252. return 0;
  253. }
  254. /* map_ib_wc_status converts raw cqe_status to ib_wc_status */
  255. static inline void map_ib_wc_status(u32 cqe_status,
  256. enum ib_wc_status *wc_status)
  257. {
  258. if (unlikely(cqe_status & WC_STATUS_ERROR_BIT)) {
  259. switch (cqe_status & 0x3F) {
  260. case 0x01:
  261. case 0x21:
  262. *wc_status = IB_WC_LOC_LEN_ERR;
  263. break;
  264. case 0x02:
  265. case 0x22:
  266. *wc_status = IB_WC_LOC_QP_OP_ERR;
  267. break;
  268. case 0x03:
  269. case 0x23:
  270. *wc_status = IB_WC_LOC_EEC_OP_ERR;
  271. break;
  272. case 0x04:
  273. case 0x24:
  274. *wc_status = IB_WC_LOC_PROT_ERR;
  275. break;
  276. case 0x05:
  277. case 0x25:
  278. *wc_status = IB_WC_WR_FLUSH_ERR;
  279. break;
  280. case 0x06:
  281. *wc_status = IB_WC_MW_BIND_ERR;
  282. break;
  283. case 0x07: /* remote error - look into bits 20:24 */
  284. switch ((cqe_status
  285. & WC_STATUS_REMOTE_ERROR_FLAGS) >> 11) {
  286. case 0x0:
  287. /*
  288. * PSN Sequence Error!
  289. * couldn't find a matching status!
  290. */
  291. *wc_status = IB_WC_GENERAL_ERR;
  292. break;
  293. case 0x1:
  294. *wc_status = IB_WC_REM_INV_REQ_ERR;
  295. break;
  296. case 0x2:
  297. *wc_status = IB_WC_REM_ACCESS_ERR;
  298. break;
  299. case 0x3:
  300. *wc_status = IB_WC_REM_OP_ERR;
  301. break;
  302. case 0x4:
  303. *wc_status = IB_WC_REM_INV_RD_REQ_ERR;
  304. break;
  305. }
  306. break;
  307. case 0x08:
  308. *wc_status = IB_WC_RETRY_EXC_ERR;
  309. break;
  310. case 0x09:
  311. *wc_status = IB_WC_RNR_RETRY_EXC_ERR;
  312. break;
  313. case 0x0A:
  314. case 0x2D:
  315. *wc_status = IB_WC_REM_ABORT_ERR;
  316. break;
  317. case 0x0B:
  318. case 0x2E:
  319. *wc_status = IB_WC_INV_EECN_ERR;
  320. break;
  321. case 0x0C:
  322. case 0x2F:
  323. *wc_status = IB_WC_INV_EEC_STATE_ERR;
  324. break;
  325. case 0x0D:
  326. *wc_status = IB_WC_BAD_RESP_ERR;
  327. break;
  328. case 0x10:
  329. /* WQE purged */
  330. *wc_status = IB_WC_WR_FLUSH_ERR;
  331. break;
  332. default:
  333. *wc_status = IB_WC_FATAL_ERR;
  334. }
  335. } else
  336. *wc_status = IB_WC_SUCCESS;
  337. }
  338. static inline int post_one_send(struct ehca_qp *my_qp,
  339. struct ib_send_wr *cur_send_wr,
  340. struct ib_send_wr **bad_send_wr,
  341. int hidden)
  342. {
  343. struct ehca_wqe *wqe_p;
  344. int ret;
  345. u64 start_offset = my_qp->ipz_squeue.current_q_offset;
  346. /* get pointer next to free WQE */
  347. wqe_p = ipz_qeit_get_inc(&my_qp->ipz_squeue);
  348. if (unlikely(!wqe_p)) {
  349. /* too many posted work requests: queue overflow */
  350. if (bad_send_wr)
  351. *bad_send_wr = cur_send_wr;
  352. ehca_err(my_qp->ib_qp.device, "Too many posted WQEs "
  353. "qp_num=%x", my_qp->ib_qp.qp_num);
  354. return -ENOMEM;
  355. }
  356. /* write a SEND WQE into the QUEUE */
  357. ret = ehca_write_swqe(my_qp, wqe_p, cur_send_wr, hidden);
  358. /*
  359. * if something failed,
  360. * reset the free entry pointer to the start value
  361. */
  362. if (unlikely(ret)) {
  363. my_qp->ipz_squeue.current_q_offset = start_offset;
  364. if (bad_send_wr)
  365. *bad_send_wr = cur_send_wr;
  366. ehca_err(my_qp->ib_qp.device, "Could not write WQE "
  367. "qp_num=%x", my_qp->ib_qp.qp_num);
  368. return -EINVAL;
  369. }
  370. return 0;
  371. }
  372. int ehca_post_send(struct ib_qp *qp,
  373. struct ib_send_wr *send_wr,
  374. struct ib_send_wr **bad_send_wr)
  375. {
  376. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  377. struct ib_send_wr *cur_send_wr;
  378. int wqe_cnt = 0;
  379. int ret = 0;
  380. unsigned long flags;
  381. /* LOCK the QUEUE */
  382. spin_lock_irqsave(&my_qp->spinlock_s, flags);
  383. /* Send an empty extra RDMA read if:
  384. * 1) there has been an RDMA read on this connection before
  385. * 2) no RDMA read occurred for ACK_CIRC_THRESHOLD link packets
  386. * 3) we can be sure that any previous extra RDMA read has been
  387. * processed so we don't overflow the SQ
  388. */
  389. if (unlikely(my_qp->unsol_ack_circ &&
  390. my_qp->packet_count > ACK_CIRC_THRESHOLD &&
  391. my_qp->message_count > my_qp->init_attr.cap.max_send_wr)) {
  392. /* insert an empty RDMA READ to fix up the remote QP state */
  393. struct ib_send_wr circ_wr;
  394. memset(&circ_wr, 0, sizeof(circ_wr));
  395. circ_wr.opcode = IB_WR_RDMA_READ;
  396. post_one_send(my_qp, &circ_wr, NULL, 1); /* ignore retcode */
  397. wqe_cnt++;
  398. ehca_dbg(qp->device, "posted circ wr qp_num=%x", qp->qp_num);
  399. my_qp->message_count = my_qp->packet_count = 0;
  400. }
  401. /* loop processes list of send reqs */
  402. for (cur_send_wr = send_wr; cur_send_wr != NULL;
  403. cur_send_wr = cur_send_wr->next) {
  404. ret = post_one_send(my_qp, cur_send_wr, bad_send_wr, 0);
  405. if (unlikely(ret)) {
  406. /* if one or more WQEs were successful, don't fail */
  407. if (wqe_cnt)
  408. ret = 0;
  409. goto post_send_exit0;
  410. }
  411. wqe_cnt++;
  412. ehca_dbg(qp->device, "ehca_qp=%p qp_num=%x wqe_cnt=%d",
  413. my_qp, qp->qp_num, wqe_cnt);
  414. } /* eof for cur_send_wr */
  415. post_send_exit0:
  416. iosync(); /* serialize GAL register access */
  417. hipz_update_sqa(my_qp, wqe_cnt);
  418. my_qp->message_count += wqe_cnt;
  419. spin_unlock_irqrestore(&my_qp->spinlock_s, flags);
  420. return ret;
  421. }
  422. static int internal_post_recv(struct ehca_qp *my_qp,
  423. struct ib_device *dev,
  424. struct ib_recv_wr *recv_wr,
  425. struct ib_recv_wr **bad_recv_wr)
  426. {
  427. struct ib_recv_wr *cur_recv_wr;
  428. struct ehca_wqe *wqe_p;
  429. int wqe_cnt = 0;
  430. int ret = 0;
  431. unsigned long flags;
  432. if (unlikely(!HAS_RQ(my_qp))) {
  433. ehca_err(dev, "QP has no RQ ehca_qp=%p qp_num=%x ext_type=%d",
  434. my_qp, my_qp->real_qp_num, my_qp->ext_type);
  435. return -ENODEV;
  436. }
  437. /* LOCK the QUEUE */
  438. spin_lock_irqsave(&my_qp->spinlock_r, flags);
  439. /* loop processes list of send reqs */
  440. for (cur_recv_wr = recv_wr; cur_recv_wr != NULL;
  441. cur_recv_wr = cur_recv_wr->next) {
  442. u64 start_offset = my_qp->ipz_rqueue.current_q_offset;
  443. /* get pointer next to free WQE */
  444. wqe_p = ipz_qeit_get_inc(&my_qp->ipz_rqueue);
  445. if (unlikely(!wqe_p)) {
  446. /* too many posted work requests: queue overflow */
  447. if (bad_recv_wr)
  448. *bad_recv_wr = cur_recv_wr;
  449. if (wqe_cnt == 0) {
  450. ret = -ENOMEM;
  451. ehca_err(dev, "Too many posted WQEs "
  452. "qp_num=%x", my_qp->real_qp_num);
  453. }
  454. goto post_recv_exit0;
  455. }
  456. /* write a RECV WQE into the QUEUE */
  457. ret = ehca_write_rwqe(&my_qp->ipz_rqueue, wqe_p, cur_recv_wr);
  458. /*
  459. * if something failed,
  460. * reset the free entry pointer to the start value
  461. */
  462. if (unlikely(ret)) {
  463. my_qp->ipz_rqueue.current_q_offset = start_offset;
  464. *bad_recv_wr = cur_recv_wr;
  465. if (wqe_cnt == 0) {
  466. ret = -EINVAL;
  467. ehca_err(dev, "Could not write WQE "
  468. "qp_num=%x", my_qp->real_qp_num);
  469. }
  470. goto post_recv_exit0;
  471. }
  472. wqe_cnt++;
  473. ehca_dbg(dev, "ehca_qp=%p qp_num=%x wqe_cnt=%d",
  474. my_qp, my_qp->real_qp_num, wqe_cnt);
  475. } /* eof for cur_recv_wr */
  476. post_recv_exit0:
  477. iosync(); /* serialize GAL register access */
  478. hipz_update_rqa(my_qp, wqe_cnt);
  479. spin_unlock_irqrestore(&my_qp->spinlock_r, flags);
  480. return ret;
  481. }
  482. int ehca_post_recv(struct ib_qp *qp,
  483. struct ib_recv_wr *recv_wr,
  484. struct ib_recv_wr **bad_recv_wr)
  485. {
  486. return internal_post_recv(container_of(qp, struct ehca_qp, ib_qp),
  487. qp->device, recv_wr, bad_recv_wr);
  488. }
  489. int ehca_post_srq_recv(struct ib_srq *srq,
  490. struct ib_recv_wr *recv_wr,
  491. struct ib_recv_wr **bad_recv_wr)
  492. {
  493. return internal_post_recv(container_of(srq, struct ehca_qp, ib_srq),
  494. srq->device, recv_wr, bad_recv_wr);
  495. }
  496. /*
  497. * ib_wc_opcode table converts ehca wc opcode to ib
  498. * Since we use zero to indicate invalid opcode, the actual ib opcode must
  499. * be decremented!!!
  500. */
  501. static const u8 ib_wc_opcode[255] = {
  502. [0x01] = IB_WC_RECV+1,
  503. [0x02] = IB_WC_RECV_RDMA_WITH_IMM+1,
  504. [0x04] = IB_WC_BIND_MW+1,
  505. [0x08] = IB_WC_FETCH_ADD+1,
  506. [0x10] = IB_WC_COMP_SWAP+1,
  507. [0x20] = IB_WC_RDMA_WRITE+1,
  508. [0x40] = IB_WC_RDMA_READ+1,
  509. [0x80] = IB_WC_SEND+1
  510. };
  511. /* internal function to poll one entry of cq */
  512. static inline int ehca_poll_cq_one(struct ib_cq *cq, struct ib_wc *wc)
  513. {
  514. int ret = 0;
  515. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  516. struct ehca_cqe *cqe;
  517. struct ehca_qp *my_qp;
  518. int cqe_count = 0;
  519. poll_cq_one_read_cqe:
  520. cqe = (struct ehca_cqe *)
  521. ipz_qeit_get_inc_valid(&my_cq->ipz_queue);
  522. if (!cqe) {
  523. ret = -EAGAIN;
  524. ehca_dbg(cq->device, "Completion queue is empty ehca_cq=%p "
  525. "cq_num=%x ret=%i", my_cq, my_cq->cq_number, ret);
  526. goto poll_cq_one_exit0;
  527. }
  528. /* prevents loads being reordered across this point */
  529. rmb();
  530. cqe_count++;
  531. if (unlikely(cqe->status & WC_STATUS_PURGE_BIT)) {
  532. struct ehca_qp *qp;
  533. int purgeflag;
  534. unsigned long flags;
  535. qp = ehca_cq_get_qp(my_cq, cqe->local_qp_number);
  536. if (!qp) {
  537. ehca_err(cq->device, "cq_num=%x qp_num=%x "
  538. "could not find qp -> ignore cqe",
  539. my_cq->cq_number, cqe->local_qp_number);
  540. ehca_dmp(cqe, 64, "cq_num=%x qp_num=%x",
  541. my_cq->cq_number, cqe->local_qp_number);
  542. /* ignore this purged cqe */
  543. goto poll_cq_one_read_cqe;
  544. }
  545. spin_lock_irqsave(&qp->spinlock_s, flags);
  546. purgeflag = qp->sqerr_purgeflag;
  547. spin_unlock_irqrestore(&qp->spinlock_s, flags);
  548. if (purgeflag) {
  549. ehca_dbg(cq->device,
  550. "Got CQE with purged bit qp_num=%x src_qp=%x",
  551. cqe->local_qp_number, cqe->remote_qp_number);
  552. if (ehca_debug_level)
  553. ehca_dmp(cqe, 64, "qp_num=%x src_qp=%x",
  554. cqe->local_qp_number,
  555. cqe->remote_qp_number);
  556. /*
  557. * ignore this to avoid double cqes of bad wqe
  558. * that caused sqe and turn off purge flag
  559. */
  560. qp->sqerr_purgeflag = 0;
  561. goto poll_cq_one_read_cqe;
  562. }
  563. }
  564. /* tracing cqe */
  565. if (unlikely(ehca_debug_level)) {
  566. ehca_dbg(cq->device,
  567. "Received COMPLETION ehca_cq=%p cq_num=%x -----",
  568. my_cq, my_cq->cq_number);
  569. ehca_dmp(cqe, 64, "ehca_cq=%p cq_num=%x",
  570. my_cq, my_cq->cq_number);
  571. ehca_dbg(cq->device,
  572. "ehca_cq=%p cq_num=%x -------------------------",
  573. my_cq, my_cq->cq_number);
  574. }
  575. /* we got a completion! */
  576. wc->wr_id = cqe->work_request_id;
  577. /* eval ib_wc_opcode */
  578. wc->opcode = ib_wc_opcode[cqe->optype]-1;
  579. if (unlikely(wc->opcode == -1)) {
  580. ehca_err(cq->device, "Invalid cqe->OPType=%x cqe->status=%x "
  581. "ehca_cq=%p cq_num=%x",
  582. cqe->optype, cqe->status, my_cq, my_cq->cq_number);
  583. /* dump cqe for other infos */
  584. ehca_dmp(cqe, 64, "ehca_cq=%p cq_num=%x",
  585. my_cq, my_cq->cq_number);
  586. /* update also queue adder to throw away this entry!!! */
  587. goto poll_cq_one_exit0;
  588. }
  589. /* eval ib_wc_status */
  590. if (unlikely(cqe->status & WC_STATUS_ERROR_BIT)) {
  591. /* complete with errors */
  592. map_ib_wc_status(cqe->status, &wc->status);
  593. wc->vendor_err = wc->status;
  594. } else
  595. wc->status = IB_WC_SUCCESS;
  596. read_lock(&ehca_qp_idr_lock);
  597. my_qp = idr_find(&ehca_qp_idr, cqe->qp_token);
  598. wc->qp = &my_qp->ib_qp;
  599. read_unlock(&ehca_qp_idr_lock);
  600. wc->byte_len = cqe->nr_bytes_transferred;
  601. wc->pkey_index = cqe->pkey_index;
  602. wc->slid = cqe->rlid;
  603. wc->dlid_path_bits = cqe->dlid;
  604. wc->src_qp = cqe->remote_qp_number;
  605. wc->wc_flags = cqe->w_completion_flags;
  606. wc->imm_data = cpu_to_be32(cqe->immediate_data);
  607. wc->sl = cqe->service_level;
  608. if (unlikely(wc->status != IB_WC_SUCCESS))
  609. ehca_dbg(cq->device,
  610. "ehca_cq=%p cq_num=%x WARNING unsuccessful cqe "
  611. "OPType=%x status=%x qp_num=%x src_qp=%x wr_id=%lx "
  612. "cqe=%p", my_cq, my_cq->cq_number, cqe->optype,
  613. cqe->status, cqe->local_qp_number,
  614. cqe->remote_qp_number, cqe->work_request_id, cqe);
  615. poll_cq_one_exit0:
  616. if (cqe_count > 0)
  617. hipz_update_feca(my_cq, cqe_count);
  618. return ret;
  619. }
  620. int ehca_poll_cq(struct ib_cq *cq, int num_entries, struct ib_wc *wc)
  621. {
  622. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  623. int nr;
  624. struct ib_wc *current_wc = wc;
  625. int ret = 0;
  626. unsigned long flags;
  627. if (num_entries < 1) {
  628. ehca_err(cq->device, "Invalid num_entries=%d ehca_cq=%p "
  629. "cq_num=%x", num_entries, my_cq, my_cq->cq_number);
  630. ret = -EINVAL;
  631. goto poll_cq_exit0;
  632. }
  633. spin_lock_irqsave(&my_cq->spinlock, flags);
  634. for (nr = 0; nr < num_entries; nr++) {
  635. ret = ehca_poll_cq_one(cq, current_wc);
  636. if (ret)
  637. break;
  638. current_wc++;
  639. } /* eof for nr */
  640. spin_unlock_irqrestore(&my_cq->spinlock, flags);
  641. if (ret == -EAGAIN || !ret)
  642. ret = nr;
  643. poll_cq_exit0:
  644. return ret;
  645. }
  646. int ehca_req_notify_cq(struct ib_cq *cq, enum ib_cq_notify_flags notify_flags)
  647. {
  648. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  649. int ret = 0;
  650. switch (notify_flags & IB_CQ_SOLICITED_MASK) {
  651. case IB_CQ_SOLICITED:
  652. hipz_set_cqx_n0(my_cq, 1);
  653. break;
  654. case IB_CQ_NEXT_COMP:
  655. hipz_set_cqx_n1(my_cq, 1);
  656. break;
  657. default:
  658. return -EINVAL;
  659. }
  660. if (notify_flags & IB_CQ_REPORT_MISSED_EVENTS) {
  661. unsigned long spl_flags;
  662. spin_lock_irqsave(&my_cq->spinlock, spl_flags);
  663. ret = ipz_qeit_is_valid(&my_cq->ipz_queue);
  664. spin_unlock_irqrestore(&my_cq->spinlock, spl_flags);
  665. }
  666. return ret;
  667. }