ehca_reqs.c 21 KB

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