ehca_reqs.c 26 KB

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