ehca_reqs.c 25 KB

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