ehca_reqs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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 "ehca_classes.h"
  44. #include "ehca_tools.h"
  45. #include "ehca_qes.h"
  46. #include "ehca_iverbs.h"
  47. #include "hcp_if.h"
  48. #include "hipz_fns.h"
  49. /* in RC traffic, insert an empty RDMA READ every this many packets */
  50. #define ACK_CIRC_THRESHOLD 2000000
  51. static u64 replace_wr_id(u64 wr_id, u16 idx)
  52. {
  53. u64 ret;
  54. ret = wr_id & ~QMAP_IDX_MASK;
  55. ret |= idx & QMAP_IDX_MASK;
  56. return ret;
  57. }
  58. static u16 get_app_wr_id(u64 wr_id)
  59. {
  60. return wr_id & QMAP_IDX_MASK;
  61. }
  62. static inline int ehca_write_rwqe(struct ipz_queue *ipz_rqueue,
  63. struct ehca_wqe *wqe_p,
  64. struct ib_recv_wr *recv_wr,
  65. u32 rq_map_idx)
  66. {
  67. u8 cnt_ds;
  68. if (unlikely((recv_wr->num_sge < 0) ||
  69. (recv_wr->num_sge > ipz_rqueue->act_nr_of_sg))) {
  70. ehca_gen_err("Invalid number of WQE SGE. "
  71. "num_sqe=%x max_nr_of_sg=%x",
  72. recv_wr->num_sge, ipz_rqueue->act_nr_of_sg);
  73. return -EINVAL; /* invalid SG list length */
  74. }
  75. /* clear wqe header until sglist */
  76. memset(wqe_p, 0, offsetof(struct ehca_wqe, u.ud_av.sg_list));
  77. wqe_p->work_request_id = replace_wr_id(recv_wr->wr_id, rq_map_idx);
  78. wqe_p->nr_of_data_seg = recv_wr->num_sge;
  79. for (cnt_ds = 0; cnt_ds < recv_wr->num_sge; cnt_ds++) {
  80. wqe_p->u.all_rcv.sg_list[cnt_ds].vaddr =
  81. recv_wr->sg_list[cnt_ds].addr;
  82. wqe_p->u.all_rcv.sg_list[cnt_ds].lkey =
  83. recv_wr->sg_list[cnt_ds].lkey;
  84. wqe_p->u.all_rcv.sg_list[cnt_ds].length =
  85. recv_wr->sg_list[cnt_ds].length;
  86. }
  87. if (ehca_debug_level >= 3) {
  88. ehca_gen_dbg("RECEIVE WQE written into ipz_rqueue=%p",
  89. ipz_rqueue);
  90. ehca_dmp(wqe_p, 16*(6 + wqe_p->nr_of_data_seg), "recv wqe");
  91. }
  92. return 0;
  93. }
  94. #if defined(DEBUG_GSI_SEND_WR)
  95. /* need ib_mad struct */
  96. #include <rdma/ib_mad.h>
  97. static void trace_send_wr_ud(const struct ib_send_wr *send_wr)
  98. {
  99. int idx;
  100. int j;
  101. while (send_wr) {
  102. struct ib_mad_hdr *mad_hdr = send_wr->wr.ud.mad_hdr;
  103. struct ib_sge *sge = send_wr->sg_list;
  104. ehca_gen_dbg("send_wr#%x wr_id=%lx num_sge=%x "
  105. "send_flags=%x opcode=%x", idx, send_wr->wr_id,
  106. send_wr->num_sge, send_wr->send_flags,
  107. send_wr->opcode);
  108. if (mad_hdr) {
  109. ehca_gen_dbg("send_wr#%x mad_hdr base_version=%x "
  110. "mgmt_class=%x class_version=%x method=%x "
  111. "status=%x class_specific=%x tid=%lx "
  112. "attr_id=%x resv=%x attr_mod=%x",
  113. idx, mad_hdr->base_version,
  114. mad_hdr->mgmt_class,
  115. mad_hdr->class_version, mad_hdr->method,
  116. mad_hdr->status, mad_hdr->class_specific,
  117. mad_hdr->tid, mad_hdr->attr_id,
  118. mad_hdr->resv,
  119. mad_hdr->attr_mod);
  120. }
  121. for (j = 0; j < send_wr->num_sge; j++) {
  122. u8 *data = (u8 *)abs_to_virt(sge->addr);
  123. ehca_gen_dbg("send_wr#%x sge#%x addr=%p length=%x "
  124. "lkey=%x",
  125. idx, j, data, sge->length, sge->lkey);
  126. /* assume length is n*16 */
  127. ehca_dmp(data, sge->length, "send_wr#%x sge#%x",
  128. idx, j);
  129. sge++;
  130. } /* eof for j */
  131. idx++;
  132. send_wr = send_wr->next;
  133. } /* eof while send_wr */
  134. }
  135. #endif /* DEBUG_GSI_SEND_WR */
  136. static inline int ehca_write_swqe(struct ehca_qp *qp,
  137. struct ehca_wqe *wqe_p,
  138. const struct ib_send_wr *send_wr,
  139. u32 sq_map_idx,
  140. int hidden)
  141. {
  142. u32 idx;
  143. u64 dma_length;
  144. struct ehca_av *my_av;
  145. u32 remote_qkey = send_wr->wr.ud.remote_qkey;
  146. struct ehca_qmap_entry *qmap_entry = &qp->sq_map.map[sq_map_idx];
  147. if (unlikely((send_wr->num_sge < 0) ||
  148. (send_wr->num_sge > qp->ipz_squeue.act_nr_of_sg))) {
  149. ehca_gen_err("Invalid number of WQE SGE. "
  150. "num_sqe=%x max_nr_of_sg=%x",
  151. send_wr->num_sge, qp->ipz_squeue.act_nr_of_sg);
  152. return -EINVAL; /* invalid SG list length */
  153. }
  154. /* clear wqe header until sglist */
  155. memset(wqe_p, 0, offsetof(struct ehca_wqe, u.ud_av.sg_list));
  156. wqe_p->work_request_id = replace_wr_id(send_wr->wr_id, sq_map_idx);
  157. qmap_entry->app_wr_id = get_app_wr_id(send_wr->wr_id);
  158. qmap_entry->reported = 0;
  159. qmap_entry->cqe_req = 0;
  160. switch (send_wr->opcode) {
  161. case IB_WR_SEND:
  162. case IB_WR_SEND_WITH_IMM:
  163. wqe_p->optype = WQE_OPTYPE_SEND;
  164. break;
  165. case IB_WR_RDMA_WRITE:
  166. case IB_WR_RDMA_WRITE_WITH_IMM:
  167. wqe_p->optype = WQE_OPTYPE_RDMAWRITE;
  168. break;
  169. case IB_WR_RDMA_READ:
  170. wqe_p->optype = WQE_OPTYPE_RDMAREAD;
  171. break;
  172. default:
  173. ehca_gen_err("Invalid opcode=%x", send_wr->opcode);
  174. return -EINVAL; /* invalid opcode */
  175. }
  176. wqe_p->wqef = (send_wr->opcode) & WQEF_HIGH_NIBBLE;
  177. wqe_p->wr_flag = 0;
  178. if ((send_wr->send_flags & IB_SEND_SIGNALED ||
  179. qp->init_attr.sq_sig_type == IB_SIGNAL_ALL_WR)
  180. && !hidden) {
  181. wqe_p->wr_flag |= WQE_WRFLAG_REQ_SIGNAL_COM;
  182. qmap_entry->cqe_req = 1;
  183. }
  184. if (send_wr->opcode == IB_WR_SEND_WITH_IMM ||
  185. send_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  186. /* this might not work as long as HW does not support it */
  187. wqe_p->immediate_data = be32_to_cpu(send_wr->ex.imm_data);
  188. wqe_p->wr_flag |= WQE_WRFLAG_IMM_DATA_PRESENT;
  189. }
  190. wqe_p->nr_of_data_seg = send_wr->num_sge;
  191. switch (qp->qp_type) {
  192. case IB_QPT_SMI:
  193. case IB_QPT_GSI:
  194. /* no break is intential here */
  195. case IB_QPT_UD:
  196. /* IB 1.2 spec C10-15 compliance */
  197. if (send_wr->wr.ud.remote_qkey & 0x80000000)
  198. remote_qkey = qp->qkey;
  199. wqe_p->destination_qp_number = send_wr->wr.ud.remote_qpn << 8;
  200. wqe_p->local_ee_context_qkey = remote_qkey;
  201. if (unlikely(!send_wr->wr.ud.ah)) {
  202. ehca_gen_err("wr.ud.ah is NULL. qp=%p", qp);
  203. return -EINVAL;
  204. }
  205. if (unlikely(send_wr->wr.ud.remote_qpn == 0)) {
  206. ehca_gen_err("dest QP# is 0. qp=%x", qp->real_qp_num);
  207. return -EINVAL;
  208. }
  209. my_av = container_of(send_wr->wr.ud.ah, struct ehca_av, ib_ah);
  210. wqe_p->u.ud_av.ud_av = my_av->av;
  211. /*
  212. * omitted check of IB_SEND_INLINE
  213. * since HW does not support it
  214. */
  215. for (idx = 0; idx < send_wr->num_sge; idx++) {
  216. wqe_p->u.ud_av.sg_list[idx].vaddr =
  217. send_wr->sg_list[idx].addr;
  218. wqe_p->u.ud_av.sg_list[idx].lkey =
  219. send_wr->sg_list[idx].lkey;
  220. wqe_p->u.ud_av.sg_list[idx].length =
  221. send_wr->sg_list[idx].length;
  222. } /* eof for idx */
  223. if (qp->qp_type == IB_QPT_SMI ||
  224. qp->qp_type == IB_QPT_GSI)
  225. wqe_p->u.ud_av.ud_av.pmtu = 1;
  226. if (qp->qp_type == IB_QPT_GSI) {
  227. wqe_p->pkeyi = send_wr->wr.ud.pkey_index;
  228. #ifdef DEBUG_GSI_SEND_WR
  229. trace_send_wr_ud(send_wr);
  230. #endif /* DEBUG_GSI_SEND_WR */
  231. }
  232. break;
  233. case IB_QPT_UC:
  234. if (send_wr->send_flags & IB_SEND_FENCE)
  235. wqe_p->wr_flag |= WQE_WRFLAG_FENCE;
  236. /* no break is intentional here */
  237. case IB_QPT_RC:
  238. /* TODO: atomic not implemented */
  239. wqe_p->u.nud.remote_virtual_address =
  240. send_wr->wr.rdma.remote_addr;
  241. wqe_p->u.nud.rkey = send_wr->wr.rdma.rkey;
  242. /*
  243. * omitted checking of IB_SEND_INLINE
  244. * since HW does not support it
  245. */
  246. dma_length = 0;
  247. for (idx = 0; idx < send_wr->num_sge; idx++) {
  248. wqe_p->u.nud.sg_list[idx].vaddr =
  249. send_wr->sg_list[idx].addr;
  250. wqe_p->u.nud.sg_list[idx].lkey =
  251. send_wr->sg_list[idx].lkey;
  252. wqe_p->u.nud.sg_list[idx].length =
  253. send_wr->sg_list[idx].length;
  254. dma_length += send_wr->sg_list[idx].length;
  255. } /* eof idx */
  256. wqe_p->u.nud.atomic_1st_op_dma_len = dma_length;
  257. /* unsolicited ack circumvention */
  258. if (send_wr->opcode == IB_WR_RDMA_READ) {
  259. /* on RDMA read, switch on and reset counters */
  260. qp->message_count = qp->packet_count = 0;
  261. qp->unsol_ack_circ = 1;
  262. } else
  263. /* else estimate #packets */
  264. qp->packet_count += (dma_length >> qp->mtu_shift) + 1;
  265. break;
  266. default:
  267. ehca_gen_err("Invalid qptype=%x", qp->qp_type);
  268. return -EINVAL;
  269. }
  270. if (ehca_debug_level >= 3) {
  271. ehca_gen_dbg("SEND WQE written into queue qp=%p ", qp);
  272. ehca_dmp( wqe_p, 16*(6 + wqe_p->nr_of_data_seg), "send wqe");
  273. }
  274. return 0;
  275. }
  276. /* map_ib_wc_status converts raw cqe_status to ib_wc_status */
  277. static inline void map_ib_wc_status(u32 cqe_status,
  278. enum ib_wc_status *wc_status)
  279. {
  280. if (unlikely(cqe_status & WC_STATUS_ERROR_BIT)) {
  281. switch (cqe_status & 0x3F) {
  282. case 0x01:
  283. case 0x21:
  284. *wc_status = IB_WC_LOC_LEN_ERR;
  285. break;
  286. case 0x02:
  287. case 0x22:
  288. *wc_status = IB_WC_LOC_QP_OP_ERR;
  289. break;
  290. case 0x03:
  291. case 0x23:
  292. *wc_status = IB_WC_LOC_EEC_OP_ERR;
  293. break;
  294. case 0x04:
  295. case 0x24:
  296. *wc_status = IB_WC_LOC_PROT_ERR;
  297. break;
  298. case 0x05:
  299. case 0x25:
  300. *wc_status = IB_WC_WR_FLUSH_ERR;
  301. break;
  302. case 0x06:
  303. *wc_status = IB_WC_MW_BIND_ERR;
  304. break;
  305. case 0x07: /* remote error - look into bits 20:24 */
  306. switch ((cqe_status
  307. & WC_STATUS_REMOTE_ERROR_FLAGS) >> 11) {
  308. case 0x0:
  309. /*
  310. * PSN Sequence Error!
  311. * couldn't find a matching status!
  312. */
  313. *wc_status = IB_WC_GENERAL_ERR;
  314. break;
  315. case 0x1:
  316. *wc_status = IB_WC_REM_INV_REQ_ERR;
  317. break;
  318. case 0x2:
  319. *wc_status = IB_WC_REM_ACCESS_ERR;
  320. break;
  321. case 0x3:
  322. *wc_status = IB_WC_REM_OP_ERR;
  323. break;
  324. case 0x4:
  325. *wc_status = IB_WC_REM_INV_RD_REQ_ERR;
  326. break;
  327. }
  328. break;
  329. case 0x08:
  330. *wc_status = IB_WC_RETRY_EXC_ERR;
  331. break;
  332. case 0x09:
  333. *wc_status = IB_WC_RNR_RETRY_EXC_ERR;
  334. break;
  335. case 0x0A:
  336. case 0x2D:
  337. *wc_status = IB_WC_REM_ABORT_ERR;
  338. break;
  339. case 0x0B:
  340. case 0x2E:
  341. *wc_status = IB_WC_INV_EECN_ERR;
  342. break;
  343. case 0x0C:
  344. case 0x2F:
  345. *wc_status = IB_WC_INV_EEC_STATE_ERR;
  346. break;
  347. case 0x0D:
  348. *wc_status = IB_WC_BAD_RESP_ERR;
  349. break;
  350. case 0x10:
  351. /* WQE purged */
  352. *wc_status = IB_WC_WR_FLUSH_ERR;
  353. break;
  354. default:
  355. *wc_status = IB_WC_FATAL_ERR;
  356. }
  357. } else
  358. *wc_status = IB_WC_SUCCESS;
  359. }
  360. static inline int post_one_send(struct ehca_qp *my_qp,
  361. struct ib_send_wr *cur_send_wr,
  362. int hidden)
  363. {
  364. struct ehca_wqe *wqe_p;
  365. int ret;
  366. u32 sq_map_idx;
  367. u64 start_offset = my_qp->ipz_squeue.current_q_offset;
  368. /* get pointer next to free WQE */
  369. wqe_p = ipz_qeit_get_inc(&my_qp->ipz_squeue);
  370. if (unlikely(!wqe_p)) {
  371. /* too many posted work requests: queue overflow */
  372. ehca_err(my_qp->ib_qp.device, "Too many posted WQEs "
  373. "qp_num=%x", my_qp->ib_qp.qp_num);
  374. return -ENOMEM;
  375. }
  376. /*
  377. * Get the index of the WQE in the send queue. The same index is used
  378. * for writing into the sq_map.
  379. */
  380. sq_map_idx = start_offset / my_qp->ipz_squeue.qe_size;
  381. /* write a SEND WQE into the QUEUE */
  382. ret = ehca_write_swqe(my_qp, wqe_p, cur_send_wr, sq_map_idx, hidden);
  383. /*
  384. * if something failed,
  385. * reset the free entry pointer to the start value
  386. */
  387. if (unlikely(ret)) {
  388. my_qp->ipz_squeue.current_q_offset = start_offset;
  389. ehca_err(my_qp->ib_qp.device, "Could not write WQE "
  390. "qp_num=%x", my_qp->ib_qp.qp_num);
  391. return -EINVAL;
  392. }
  393. return 0;
  394. }
  395. int ehca_post_send(struct ib_qp *qp,
  396. struct ib_send_wr *send_wr,
  397. struct ib_send_wr **bad_send_wr)
  398. {
  399. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  400. int wqe_cnt = 0;
  401. int ret = 0;
  402. unsigned long flags;
  403. /* Reject WR if QP is in RESET, INIT or RTR state */
  404. if (unlikely(my_qp->state < IB_QPS_RTS)) {
  405. ehca_err(qp->device, "Invalid QP state qp_state=%d qpn=%x",
  406. my_qp->state, qp->qp_num);
  407. ret = -EINVAL;
  408. goto out;
  409. }
  410. /* LOCK the QUEUE */
  411. spin_lock_irqsave(&my_qp->spinlock_s, flags);
  412. /* Send an empty extra RDMA read if:
  413. * 1) there has been an RDMA read on this connection before
  414. * 2) no RDMA read occurred for ACK_CIRC_THRESHOLD link packets
  415. * 3) we can be sure that any previous extra RDMA read has been
  416. * processed so we don't overflow the SQ
  417. */
  418. if (unlikely(my_qp->unsol_ack_circ &&
  419. my_qp->packet_count > ACK_CIRC_THRESHOLD &&
  420. my_qp->message_count > my_qp->init_attr.cap.max_send_wr)) {
  421. /* insert an empty RDMA READ to fix up the remote QP state */
  422. struct ib_send_wr circ_wr;
  423. memset(&circ_wr, 0, sizeof(circ_wr));
  424. circ_wr.opcode = IB_WR_RDMA_READ;
  425. post_one_send(my_qp, &circ_wr, 1); /* ignore retcode */
  426. wqe_cnt++;
  427. ehca_dbg(qp->device, "posted circ wr qp_num=%x", qp->qp_num);
  428. my_qp->message_count = my_qp->packet_count = 0;
  429. }
  430. /* loop processes list of send reqs */
  431. while (send_wr) {
  432. ret = post_one_send(my_qp, send_wr, 0);
  433. if (unlikely(ret)) {
  434. goto post_send_exit0;
  435. }
  436. wqe_cnt++;
  437. send_wr = send_wr->next;
  438. }
  439. post_send_exit0:
  440. iosync(); /* serialize GAL register access */
  441. hipz_update_sqa(my_qp, wqe_cnt);
  442. if (unlikely(ret || ehca_debug_level >= 2))
  443. ehca_dbg(qp->device, "ehca_qp=%p qp_num=%x wqe_cnt=%d ret=%i",
  444. my_qp, qp->qp_num, wqe_cnt, ret);
  445. my_qp->message_count += wqe_cnt;
  446. spin_unlock_irqrestore(&my_qp->spinlock_s, flags);
  447. out:
  448. if (ret)
  449. *bad_send_wr = send_wr;
  450. return ret;
  451. }
  452. static int internal_post_recv(struct ehca_qp *my_qp,
  453. struct ib_device *dev,
  454. struct ib_recv_wr *recv_wr,
  455. struct ib_recv_wr **bad_recv_wr)
  456. {
  457. struct ehca_wqe *wqe_p;
  458. int wqe_cnt = 0;
  459. int ret = 0;
  460. u32 rq_map_idx;
  461. unsigned long flags;
  462. struct ehca_qmap_entry *qmap_entry;
  463. if (unlikely(!HAS_RQ(my_qp))) {
  464. ehca_err(dev, "QP has no RQ ehca_qp=%p qp_num=%x ext_type=%d",
  465. my_qp, my_qp->real_qp_num, my_qp->ext_type);
  466. ret = -ENODEV;
  467. goto out;
  468. }
  469. /* LOCK the QUEUE */
  470. spin_lock_irqsave(&my_qp->spinlock_r, flags);
  471. /* loop processes list of recv reqs */
  472. while (recv_wr) {
  473. u64 start_offset = my_qp->ipz_rqueue.current_q_offset;
  474. /* get pointer next to free WQE */
  475. wqe_p = ipz_qeit_get_inc(&my_qp->ipz_rqueue);
  476. if (unlikely(!wqe_p)) {
  477. /* too many posted work requests: queue overflow */
  478. ret = -ENOMEM;
  479. ehca_err(dev, "Too many posted WQEs "
  480. "qp_num=%x", my_qp->real_qp_num);
  481. goto post_recv_exit0;
  482. }
  483. /*
  484. * Get the index of the WQE in the recv queue. The same index
  485. * is used for writing into the rq_map.
  486. */
  487. rq_map_idx = start_offset / my_qp->ipz_rqueue.qe_size;
  488. /* write a RECV WQE into the QUEUE */
  489. ret = ehca_write_rwqe(&my_qp->ipz_rqueue, wqe_p, recv_wr,
  490. rq_map_idx);
  491. /*
  492. * if something failed,
  493. * reset the free entry pointer to the start value
  494. */
  495. if (unlikely(ret)) {
  496. my_qp->ipz_rqueue.current_q_offset = start_offset;
  497. ret = -EINVAL;
  498. ehca_err(dev, "Could not write WQE "
  499. "qp_num=%x", my_qp->real_qp_num);
  500. goto post_recv_exit0;
  501. }
  502. qmap_entry = &my_qp->rq_map.map[rq_map_idx];
  503. qmap_entry->app_wr_id = get_app_wr_id(recv_wr->wr_id);
  504. qmap_entry->reported = 0;
  505. qmap_entry->cqe_req = 1;
  506. wqe_cnt++;
  507. recv_wr = recv_wr->next;
  508. } /* eof for recv_wr */
  509. post_recv_exit0:
  510. iosync(); /* serialize GAL register access */
  511. hipz_update_rqa(my_qp, wqe_cnt);
  512. if (unlikely(ret || ehca_debug_level >= 2))
  513. ehca_dbg(dev, "ehca_qp=%p qp_num=%x wqe_cnt=%d ret=%i",
  514. my_qp, my_qp->real_qp_num, wqe_cnt, ret);
  515. spin_unlock_irqrestore(&my_qp->spinlock_r, flags);
  516. out:
  517. if (ret)
  518. *bad_recv_wr = recv_wr;
  519. return ret;
  520. }
  521. int ehca_post_recv(struct ib_qp *qp,
  522. struct ib_recv_wr *recv_wr,
  523. struct ib_recv_wr **bad_recv_wr)
  524. {
  525. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  526. /* Reject WR if QP is in RESET state */
  527. if (unlikely(my_qp->state == IB_QPS_RESET)) {
  528. ehca_err(qp->device, "Invalid QP state qp_state=%d qpn=%x",
  529. my_qp->state, qp->qp_num);
  530. *bad_recv_wr = recv_wr;
  531. return -EINVAL;
  532. }
  533. return internal_post_recv(my_qp, qp->device, recv_wr, bad_recv_wr);
  534. }
  535. int ehca_post_srq_recv(struct ib_srq *srq,
  536. struct ib_recv_wr *recv_wr,
  537. struct ib_recv_wr **bad_recv_wr)
  538. {
  539. return internal_post_recv(container_of(srq, struct ehca_qp, ib_srq),
  540. srq->device, recv_wr, bad_recv_wr);
  541. }
  542. /*
  543. * ib_wc_opcode table converts ehca wc opcode to ib
  544. * Since we use zero to indicate invalid opcode, the actual ib opcode must
  545. * be decremented!!!
  546. */
  547. static const u8 ib_wc_opcode[255] = {
  548. [0x01] = IB_WC_RECV+1,
  549. [0x02] = IB_WC_RECV_RDMA_WITH_IMM+1,
  550. [0x04] = IB_WC_BIND_MW+1,
  551. [0x08] = IB_WC_FETCH_ADD+1,
  552. [0x10] = IB_WC_COMP_SWAP+1,
  553. [0x20] = IB_WC_RDMA_WRITE+1,
  554. [0x40] = IB_WC_RDMA_READ+1,
  555. [0x80] = IB_WC_SEND+1
  556. };
  557. /* internal function to poll one entry of cq */
  558. static inline int ehca_poll_cq_one(struct ib_cq *cq, struct ib_wc *wc)
  559. {
  560. int ret = 0, qmap_tail_idx;
  561. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  562. struct ehca_cqe *cqe;
  563. struct ehca_qp *my_qp;
  564. struct ehca_qmap_entry *qmap_entry;
  565. struct ehca_queue_map *qmap;
  566. int cqe_count = 0, is_error;
  567. repoll:
  568. cqe = (struct ehca_cqe *)
  569. ipz_qeit_get_inc_valid(&my_cq->ipz_queue);
  570. if (!cqe) {
  571. ret = -EAGAIN;
  572. if (ehca_debug_level >= 3)
  573. ehca_dbg(cq->device, "Completion queue is empty "
  574. "my_cq=%p cq_num=%x", my_cq, my_cq->cq_number);
  575. goto poll_cq_one_exit0;
  576. }
  577. /* prevents loads being reordered across this point */
  578. rmb();
  579. cqe_count++;
  580. if (unlikely(cqe->status & WC_STATUS_PURGE_BIT)) {
  581. struct ehca_qp *qp;
  582. int purgeflag;
  583. unsigned long flags;
  584. qp = ehca_cq_get_qp(my_cq, cqe->local_qp_number);
  585. if (!qp) {
  586. ehca_err(cq->device, "cq_num=%x qp_num=%x "
  587. "could not find qp -> ignore cqe",
  588. my_cq->cq_number, cqe->local_qp_number);
  589. ehca_dmp(cqe, 64, "cq_num=%x qp_num=%x",
  590. my_cq->cq_number, cqe->local_qp_number);
  591. /* ignore this purged cqe */
  592. goto repoll;
  593. }
  594. spin_lock_irqsave(&qp->spinlock_s, flags);
  595. purgeflag = qp->sqerr_purgeflag;
  596. spin_unlock_irqrestore(&qp->spinlock_s, flags);
  597. if (purgeflag) {
  598. ehca_dbg(cq->device,
  599. "Got CQE with purged bit qp_num=%x src_qp=%x",
  600. cqe->local_qp_number, cqe->remote_qp_number);
  601. if (ehca_debug_level >= 2)
  602. ehca_dmp(cqe, 64, "qp_num=%x src_qp=%x",
  603. cqe->local_qp_number,
  604. cqe->remote_qp_number);
  605. /*
  606. * ignore this to avoid double cqes of bad wqe
  607. * that caused sqe and turn off purge flag
  608. */
  609. qp->sqerr_purgeflag = 0;
  610. goto repoll;
  611. }
  612. }
  613. is_error = cqe->status & WC_STATUS_ERROR_BIT;
  614. /* trace error CQEs if debug_level >= 1, trace all CQEs if >= 3 */
  615. if (unlikely(ehca_debug_level >= 3 || (ehca_debug_level && is_error))) {
  616. ehca_dbg(cq->device,
  617. "Received %sCOMPLETION ehca_cq=%p cq_num=%x -----",
  618. is_error ? "ERROR " : "", my_cq, my_cq->cq_number);
  619. ehca_dmp(cqe, 64, "ehca_cq=%p cq_num=%x",
  620. my_cq, my_cq->cq_number);
  621. ehca_dbg(cq->device,
  622. "ehca_cq=%p cq_num=%x -------------------------",
  623. my_cq, my_cq->cq_number);
  624. }
  625. read_lock(&ehca_qp_idr_lock);
  626. my_qp = idr_find(&ehca_qp_idr, cqe->qp_token);
  627. read_unlock(&ehca_qp_idr_lock);
  628. if (!my_qp)
  629. goto repoll;
  630. wc->qp = &my_qp->ib_qp;
  631. qmap_tail_idx = get_app_wr_id(cqe->work_request_id);
  632. if (!(cqe->w_completion_flags & WC_SEND_RECEIVE_BIT))
  633. /* We got a send completion. */
  634. qmap = &my_qp->sq_map;
  635. else
  636. /* We got a receive completion. */
  637. qmap = &my_qp->rq_map;
  638. /* advance the tail pointer */
  639. qmap->tail = qmap_tail_idx;
  640. if (is_error) {
  641. /*
  642. * set left_to_poll to 0 because in error state, we will not
  643. * get any additional CQEs
  644. */
  645. my_qp->sq_map.next_wqe_idx = next_index(my_qp->sq_map.tail,
  646. my_qp->sq_map.entries);
  647. my_qp->sq_map.left_to_poll = 0;
  648. ehca_add_to_err_list(my_qp, 1);
  649. my_qp->rq_map.next_wqe_idx = next_index(my_qp->rq_map.tail,
  650. my_qp->rq_map.entries);
  651. my_qp->rq_map.left_to_poll = 0;
  652. if (HAS_RQ(my_qp))
  653. ehca_add_to_err_list(my_qp, 0);
  654. }
  655. qmap_entry = &qmap->map[qmap_tail_idx];
  656. if (qmap_entry->reported) {
  657. ehca_warn(cq->device, "Double cqe on qp_num=%#x",
  658. my_qp->real_qp_num);
  659. /* found a double cqe, discard it and read next one */
  660. goto repoll;
  661. }
  662. wc->wr_id = replace_wr_id(cqe->work_request_id, qmap_entry->app_wr_id);
  663. qmap_entry->reported = 1;
  664. /* if left_to_poll is decremented to 0, add the QP to the error list */
  665. if (qmap->left_to_poll > 0) {
  666. qmap->left_to_poll--;
  667. if ((my_qp->sq_map.left_to_poll == 0) &&
  668. (my_qp->rq_map.left_to_poll == 0)) {
  669. ehca_add_to_err_list(my_qp, 1);
  670. if (HAS_RQ(my_qp))
  671. ehca_add_to_err_list(my_qp, 0);
  672. }
  673. }
  674. /* eval ib_wc_opcode */
  675. wc->opcode = ib_wc_opcode[cqe->optype]-1;
  676. if (unlikely(wc->opcode == -1)) {
  677. ehca_err(cq->device, "Invalid cqe->OPType=%x cqe->status=%x "
  678. "ehca_cq=%p cq_num=%x",
  679. cqe->optype, cqe->status, my_cq, my_cq->cq_number);
  680. /* dump cqe for other infos */
  681. ehca_dmp(cqe, 64, "ehca_cq=%p cq_num=%x",
  682. my_cq, my_cq->cq_number);
  683. /* update also queue adder to throw away this entry!!! */
  684. goto repoll;
  685. }
  686. /* eval ib_wc_status */
  687. if (unlikely(is_error)) {
  688. /* complete with errors */
  689. map_ib_wc_status(cqe->status, &wc->status);
  690. wc->vendor_err = wc->status;
  691. } else
  692. wc->status = IB_WC_SUCCESS;
  693. wc->byte_len = cqe->nr_bytes_transferred;
  694. wc->pkey_index = cqe->pkey_index;
  695. wc->slid = cqe->rlid;
  696. wc->dlid_path_bits = cqe->dlid;
  697. wc->src_qp = cqe->remote_qp_number;
  698. /*
  699. * HW has "Immed data present" and "GRH present" in bits 6 and 5.
  700. * SW defines those in bits 1 and 0, so we can just shift and mask.
  701. */
  702. wc->wc_flags = (cqe->w_completion_flags >> 5) & 3;
  703. wc->ex.imm_data = cpu_to_be32(cqe->immediate_data);
  704. wc->sl = cqe->service_level;
  705. poll_cq_one_exit0:
  706. if (cqe_count > 0)
  707. hipz_update_feca(my_cq, cqe_count);
  708. return ret;
  709. }
  710. static int generate_flush_cqes(struct ehca_qp *my_qp, struct ib_cq *cq,
  711. struct ib_wc *wc, int num_entries,
  712. struct ipz_queue *ipz_queue, int on_sq)
  713. {
  714. int nr = 0;
  715. struct ehca_wqe *wqe;
  716. u64 offset;
  717. struct ehca_queue_map *qmap;
  718. struct ehca_qmap_entry *qmap_entry;
  719. if (on_sq)
  720. qmap = &my_qp->sq_map;
  721. else
  722. qmap = &my_qp->rq_map;
  723. qmap_entry = &qmap->map[qmap->next_wqe_idx];
  724. while ((nr < num_entries) && (qmap_entry->reported == 0)) {
  725. /* generate flush CQE */
  726. memset(wc, 0, sizeof(*wc));
  727. offset = qmap->next_wqe_idx * ipz_queue->qe_size;
  728. wqe = (struct ehca_wqe *)ipz_qeit_calc(ipz_queue, offset);
  729. if (!wqe) {
  730. ehca_err(cq->device, "Invalid wqe offset=%#llx on "
  731. "qp_num=%#x", offset, my_qp->real_qp_num);
  732. return nr;
  733. }
  734. wc->wr_id = replace_wr_id(wqe->work_request_id,
  735. qmap_entry->app_wr_id);
  736. if (on_sq) {
  737. switch (wqe->optype) {
  738. case WQE_OPTYPE_SEND:
  739. wc->opcode = IB_WC_SEND;
  740. break;
  741. case WQE_OPTYPE_RDMAWRITE:
  742. wc->opcode = IB_WC_RDMA_WRITE;
  743. break;
  744. case WQE_OPTYPE_RDMAREAD:
  745. wc->opcode = IB_WC_RDMA_READ;
  746. break;
  747. default:
  748. ehca_err(cq->device, "Invalid optype=%x",
  749. wqe->optype);
  750. return nr;
  751. }
  752. } else
  753. wc->opcode = IB_WC_RECV;
  754. if (wqe->wr_flag & WQE_WRFLAG_IMM_DATA_PRESENT) {
  755. wc->ex.imm_data = wqe->immediate_data;
  756. wc->wc_flags |= IB_WC_WITH_IMM;
  757. }
  758. wc->status = IB_WC_WR_FLUSH_ERR;
  759. wc->qp = &my_qp->ib_qp;
  760. /* mark as reported and advance next_wqe pointer */
  761. qmap_entry->reported = 1;
  762. qmap->next_wqe_idx = next_index(qmap->next_wqe_idx,
  763. qmap->entries);
  764. qmap_entry = &qmap->map[qmap->next_wqe_idx];
  765. wc++; nr++;
  766. }
  767. return nr;
  768. }
  769. int ehca_poll_cq(struct ib_cq *cq, int num_entries, struct ib_wc *wc)
  770. {
  771. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  772. int nr;
  773. struct ehca_qp *err_qp;
  774. struct ib_wc *current_wc = wc;
  775. int ret = 0;
  776. unsigned long flags;
  777. int entries_left = num_entries;
  778. if (num_entries < 1) {
  779. ehca_err(cq->device, "Invalid num_entries=%d ehca_cq=%p "
  780. "cq_num=%x", num_entries, my_cq, my_cq->cq_number);
  781. ret = -EINVAL;
  782. goto poll_cq_exit0;
  783. }
  784. spin_lock_irqsave(&my_cq->spinlock, flags);
  785. /* generate flush cqes for send queues */
  786. list_for_each_entry(err_qp, &my_cq->sqp_err_list, sq_err_node) {
  787. nr = generate_flush_cqes(err_qp, cq, current_wc, entries_left,
  788. &err_qp->ipz_squeue, 1);
  789. entries_left -= nr;
  790. current_wc += nr;
  791. if (entries_left == 0)
  792. break;
  793. }
  794. /* generate flush cqes for receive queues */
  795. list_for_each_entry(err_qp, &my_cq->rqp_err_list, rq_err_node) {
  796. nr = generate_flush_cqes(err_qp, cq, current_wc, entries_left,
  797. &err_qp->ipz_rqueue, 0);
  798. entries_left -= nr;
  799. current_wc += nr;
  800. if (entries_left == 0)
  801. break;
  802. }
  803. for (nr = 0; nr < entries_left; nr++) {
  804. ret = ehca_poll_cq_one(cq, current_wc);
  805. if (ret)
  806. break;
  807. current_wc++;
  808. } /* eof for nr */
  809. entries_left -= nr;
  810. spin_unlock_irqrestore(&my_cq->spinlock, flags);
  811. if (ret == -EAGAIN || !ret)
  812. ret = num_entries - entries_left;
  813. poll_cq_exit0:
  814. return ret;
  815. }
  816. int ehca_req_notify_cq(struct ib_cq *cq, enum ib_cq_notify_flags notify_flags)
  817. {
  818. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  819. int ret = 0;
  820. switch (notify_flags & IB_CQ_SOLICITED_MASK) {
  821. case IB_CQ_SOLICITED:
  822. hipz_set_cqx_n0(my_cq, 1);
  823. break;
  824. case IB_CQ_NEXT_COMP:
  825. hipz_set_cqx_n1(my_cq, 1);
  826. break;
  827. default:
  828. return -EINVAL;
  829. }
  830. if (notify_flags & IB_CQ_REPORT_MISSED_EVENTS) {
  831. unsigned long spl_flags;
  832. spin_lock_irqsave(&my_cq->spinlock, spl_flags);
  833. ret = ipz_qeit_is_valid(&my_cq->ipz_queue);
  834. spin_unlock_irqrestore(&my_cq->spinlock, spl_flags);
  835. }
  836. return ret;
  837. }