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 <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. int hidden)
  364. {
  365. struct ehca_wqe *wqe_p;
  366. int ret;
  367. u32 sq_map_idx;
  368. u64 start_offset = my_qp->ipz_squeue.current_q_offset;
  369. /* get pointer next to free WQE */
  370. wqe_p = ipz_qeit_get_inc(&my_qp->ipz_squeue);
  371. if (unlikely(!wqe_p)) {
  372. /* too many posted work requests: queue overflow */
  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. ehca_err(my_qp->ib_qp.device, "Could not write WQE "
  391. "qp_num=%x", my_qp->ib_qp.qp_num);
  392. return -EINVAL;
  393. }
  394. return 0;
  395. }
  396. int ehca_post_send(struct ib_qp *qp,
  397. struct ib_send_wr *send_wr,
  398. struct ib_send_wr **bad_send_wr)
  399. {
  400. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  401. int wqe_cnt = 0;
  402. int ret = 0;
  403. unsigned long flags;
  404. /* Reject WR if QP is in RESET, INIT or RTR state */
  405. if (unlikely(my_qp->state < IB_QPS_RTS)) {
  406. ehca_err(qp->device, "Invalid QP state qp_state=%d qpn=%x",
  407. my_qp->state, qp->qp_num);
  408. ret = -EINVAL;
  409. goto out;
  410. }
  411. /* LOCK the QUEUE */
  412. spin_lock_irqsave(&my_qp->spinlock_s, flags);
  413. /* Send an empty extra RDMA read if:
  414. * 1) there has been an RDMA read on this connection before
  415. * 2) no RDMA read occurred for ACK_CIRC_THRESHOLD link packets
  416. * 3) we can be sure that any previous extra RDMA read has been
  417. * processed so we don't overflow the SQ
  418. */
  419. if (unlikely(my_qp->unsol_ack_circ &&
  420. my_qp->packet_count > ACK_CIRC_THRESHOLD &&
  421. my_qp->message_count > my_qp->init_attr.cap.max_send_wr)) {
  422. /* insert an empty RDMA READ to fix up the remote QP state */
  423. struct ib_send_wr circ_wr;
  424. memset(&circ_wr, 0, sizeof(circ_wr));
  425. circ_wr.opcode = IB_WR_RDMA_READ;
  426. post_one_send(my_qp, &circ_wr, 1); /* ignore retcode */
  427. wqe_cnt++;
  428. ehca_dbg(qp->device, "posted circ wr qp_num=%x", qp->qp_num);
  429. my_qp->message_count = my_qp->packet_count = 0;
  430. }
  431. /* loop processes list of send reqs */
  432. while (send_wr) {
  433. ret = post_one_send(my_qp, send_wr, 0);
  434. if (unlikely(ret)) {
  435. goto post_send_exit0;
  436. }
  437. wqe_cnt++;
  438. send_wr = send_wr->next;
  439. }
  440. post_send_exit0:
  441. iosync(); /* serialize GAL register access */
  442. hipz_update_sqa(my_qp, wqe_cnt);
  443. if (unlikely(ret || ehca_debug_level >= 2))
  444. ehca_dbg(qp->device, "ehca_qp=%p qp_num=%x wqe_cnt=%d ret=%i",
  445. my_qp, qp->qp_num, wqe_cnt, ret);
  446. my_qp->message_count += wqe_cnt;
  447. spin_unlock_irqrestore(&my_qp->spinlock_s, flags);
  448. out:
  449. if (ret)
  450. *bad_send_wr = send_wr;
  451. return ret;
  452. }
  453. static int internal_post_recv(struct ehca_qp *my_qp,
  454. struct ib_device *dev,
  455. struct ib_recv_wr *recv_wr,
  456. struct ib_recv_wr **bad_recv_wr)
  457. {
  458. struct ehca_wqe *wqe_p;
  459. int wqe_cnt = 0;
  460. int ret = 0;
  461. u32 rq_map_idx;
  462. unsigned long flags;
  463. struct ehca_qmap_entry *qmap_entry;
  464. if (unlikely(!HAS_RQ(my_qp))) {
  465. ehca_err(dev, "QP has no RQ ehca_qp=%p qp_num=%x ext_type=%d",
  466. my_qp, my_qp->real_qp_num, my_qp->ext_type);
  467. ret = -ENODEV;
  468. goto out;
  469. }
  470. /* LOCK the QUEUE */
  471. spin_lock_irqsave(&my_qp->spinlock_r, flags);
  472. /* loop processes list of recv reqs */
  473. while (recv_wr) {
  474. u64 start_offset = my_qp->ipz_rqueue.current_q_offset;
  475. /* get pointer next to free WQE */
  476. wqe_p = ipz_qeit_get_inc(&my_qp->ipz_rqueue);
  477. if (unlikely(!wqe_p)) {
  478. /* too many posted work requests: queue overflow */
  479. ret = -ENOMEM;
  480. ehca_err(dev, "Too many posted WQEs "
  481. "qp_num=%x", my_qp->real_qp_num);
  482. goto post_recv_exit0;
  483. }
  484. /*
  485. * Get the index of the WQE in the recv queue. The same index
  486. * is used for writing into the rq_map.
  487. */
  488. rq_map_idx = start_offset / my_qp->ipz_rqueue.qe_size;
  489. /* write a RECV WQE into the QUEUE */
  490. ret = ehca_write_rwqe(&my_qp->ipz_rqueue, wqe_p, recv_wr,
  491. rq_map_idx);
  492. /*
  493. * if something failed,
  494. * reset the free entry pointer to the start value
  495. */
  496. if (unlikely(ret)) {
  497. my_qp->ipz_rqueue.current_q_offset = start_offset;
  498. ret = -EINVAL;
  499. ehca_err(dev, "Could not write WQE "
  500. "qp_num=%x", my_qp->real_qp_num);
  501. goto post_recv_exit0;
  502. }
  503. qmap_entry = &my_qp->rq_map.map[rq_map_idx];
  504. qmap_entry->app_wr_id = get_app_wr_id(recv_wr->wr_id);
  505. qmap_entry->reported = 0;
  506. qmap_entry->cqe_req = 1;
  507. wqe_cnt++;
  508. recv_wr = recv_wr->next;
  509. } /* eof for recv_wr */
  510. post_recv_exit0:
  511. iosync(); /* serialize GAL register access */
  512. hipz_update_rqa(my_qp, wqe_cnt);
  513. if (unlikely(ret || ehca_debug_level >= 2))
  514. ehca_dbg(dev, "ehca_qp=%p qp_num=%x wqe_cnt=%d ret=%i",
  515. my_qp, my_qp->real_qp_num, wqe_cnt, ret);
  516. spin_unlock_irqrestore(&my_qp->spinlock_r, flags);
  517. out:
  518. if (ret)
  519. *bad_recv_wr = recv_wr;
  520. return ret;
  521. }
  522. int ehca_post_recv(struct ib_qp *qp,
  523. struct ib_recv_wr *recv_wr,
  524. struct ib_recv_wr **bad_recv_wr)
  525. {
  526. struct ehca_qp *my_qp = container_of(qp, struct ehca_qp, ib_qp);
  527. /* Reject WR if QP is in RESET state */
  528. if (unlikely(my_qp->state == IB_QPS_RESET)) {
  529. ehca_err(qp->device, "Invalid QP state qp_state=%d qpn=%x",
  530. my_qp->state, qp->qp_num);
  531. *bad_recv_wr = recv_wr;
  532. return -EINVAL;
  533. }
  534. return internal_post_recv(my_qp, qp->device, recv_wr, bad_recv_wr);
  535. }
  536. int ehca_post_srq_recv(struct ib_srq *srq,
  537. struct ib_recv_wr *recv_wr,
  538. struct ib_recv_wr **bad_recv_wr)
  539. {
  540. return internal_post_recv(container_of(srq, struct ehca_qp, ib_srq),
  541. srq->device, recv_wr, bad_recv_wr);
  542. }
  543. /*
  544. * ib_wc_opcode table converts ehca wc opcode to ib
  545. * Since we use zero to indicate invalid opcode, the actual ib opcode must
  546. * be decremented!!!
  547. */
  548. static const u8 ib_wc_opcode[255] = {
  549. [0x01] = IB_WC_RECV+1,
  550. [0x02] = IB_WC_RECV_RDMA_WITH_IMM+1,
  551. [0x04] = IB_WC_BIND_MW+1,
  552. [0x08] = IB_WC_FETCH_ADD+1,
  553. [0x10] = IB_WC_COMP_SWAP+1,
  554. [0x20] = IB_WC_RDMA_WRITE+1,
  555. [0x40] = IB_WC_RDMA_READ+1,
  556. [0x80] = IB_WC_SEND+1
  557. };
  558. /* internal function to poll one entry of cq */
  559. static inline int ehca_poll_cq_one(struct ib_cq *cq, struct ib_wc *wc)
  560. {
  561. int ret = 0, qmap_tail_idx;
  562. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  563. struct ehca_cqe *cqe;
  564. struct ehca_qp *my_qp;
  565. struct ehca_qmap_entry *qmap_entry;
  566. struct ehca_queue_map *qmap;
  567. int cqe_count = 0, is_error;
  568. repoll:
  569. cqe = (struct ehca_cqe *)
  570. ipz_qeit_get_inc_valid(&my_cq->ipz_queue);
  571. if (!cqe) {
  572. ret = -EAGAIN;
  573. if (ehca_debug_level >= 3)
  574. ehca_dbg(cq->device, "Completion queue is empty "
  575. "my_cq=%p cq_num=%x", my_cq, my_cq->cq_number);
  576. goto poll_cq_one_exit0;
  577. }
  578. /* prevents loads being reordered across this point */
  579. rmb();
  580. cqe_count++;
  581. if (unlikely(cqe->status & WC_STATUS_PURGE_BIT)) {
  582. struct ehca_qp *qp;
  583. int purgeflag;
  584. unsigned long flags;
  585. qp = ehca_cq_get_qp(my_cq, cqe->local_qp_number);
  586. if (!qp) {
  587. ehca_err(cq->device, "cq_num=%x qp_num=%x "
  588. "could not find qp -> ignore cqe",
  589. my_cq->cq_number, cqe->local_qp_number);
  590. ehca_dmp(cqe, 64, "cq_num=%x qp_num=%x",
  591. my_cq->cq_number, cqe->local_qp_number);
  592. /* ignore this purged cqe */
  593. goto repoll;
  594. }
  595. spin_lock_irqsave(&qp->spinlock_s, flags);
  596. purgeflag = qp->sqerr_purgeflag;
  597. spin_unlock_irqrestore(&qp->spinlock_s, flags);
  598. if (purgeflag) {
  599. ehca_dbg(cq->device,
  600. "Got CQE with purged bit qp_num=%x src_qp=%x",
  601. cqe->local_qp_number, cqe->remote_qp_number);
  602. if (ehca_debug_level >= 2)
  603. ehca_dmp(cqe, 64, "qp_num=%x src_qp=%x",
  604. cqe->local_qp_number,
  605. cqe->remote_qp_number);
  606. /*
  607. * ignore this to avoid double cqes of bad wqe
  608. * that caused sqe and turn off purge flag
  609. */
  610. qp->sqerr_purgeflag = 0;
  611. goto repoll;
  612. }
  613. }
  614. is_error = cqe->status & WC_STATUS_ERROR_BIT;
  615. /* trace error CQEs if debug_level >= 1, trace all CQEs if >= 3 */
  616. if (unlikely(ehca_debug_level >= 3 || (ehca_debug_level && is_error))) {
  617. ehca_dbg(cq->device,
  618. "Received %sCOMPLETION ehca_cq=%p cq_num=%x -----",
  619. is_error ? "ERROR " : "", my_cq, my_cq->cq_number);
  620. ehca_dmp(cqe, 64, "ehca_cq=%p cq_num=%x",
  621. my_cq, my_cq->cq_number);
  622. ehca_dbg(cq->device,
  623. "ehca_cq=%p cq_num=%x -------------------------",
  624. my_cq, my_cq->cq_number);
  625. }
  626. read_lock(&ehca_qp_idr_lock);
  627. my_qp = idr_find(&ehca_qp_idr, cqe->qp_token);
  628. read_unlock(&ehca_qp_idr_lock);
  629. if (!my_qp)
  630. goto repoll;
  631. wc->qp = &my_qp->ib_qp;
  632. qmap_tail_idx = get_app_wr_id(cqe->work_request_id);
  633. if (!(cqe->w_completion_flags & WC_SEND_RECEIVE_BIT))
  634. /* We got a send completion. */
  635. qmap = &my_qp->sq_map;
  636. else
  637. /* We got a receive completion. */
  638. qmap = &my_qp->rq_map;
  639. /* advance the tail pointer */
  640. qmap->tail = qmap_tail_idx;
  641. if (is_error) {
  642. /*
  643. * set left_to_poll to 0 because in error state, we will not
  644. * get any additional CQEs
  645. */
  646. my_qp->sq_map.next_wqe_idx = next_index(my_qp->sq_map.tail,
  647. my_qp->sq_map.entries);
  648. my_qp->sq_map.left_to_poll = 0;
  649. ehca_add_to_err_list(my_qp, 1);
  650. my_qp->rq_map.next_wqe_idx = next_index(my_qp->rq_map.tail,
  651. my_qp->rq_map.entries);
  652. my_qp->rq_map.left_to_poll = 0;
  653. if (HAS_RQ(my_qp))
  654. ehca_add_to_err_list(my_qp, 0);
  655. }
  656. qmap_entry = &qmap->map[qmap_tail_idx];
  657. if (qmap_entry->reported) {
  658. ehca_warn(cq->device, "Double cqe on qp_num=%#x",
  659. my_qp->real_qp_num);
  660. /* found a double cqe, discard it and read next one */
  661. goto repoll;
  662. }
  663. wc->wr_id = replace_wr_id(cqe->work_request_id, qmap_entry->app_wr_id);
  664. qmap_entry->reported = 1;
  665. /* if left_to_poll is decremented to 0, add the QP to the error list */
  666. if (qmap->left_to_poll > 0) {
  667. qmap->left_to_poll--;
  668. if ((my_qp->sq_map.left_to_poll == 0) &&
  669. (my_qp->rq_map.left_to_poll == 0)) {
  670. ehca_add_to_err_list(my_qp, 1);
  671. if (HAS_RQ(my_qp))
  672. ehca_add_to_err_list(my_qp, 0);
  673. }
  674. }
  675. /* eval ib_wc_opcode */
  676. wc->opcode = ib_wc_opcode[cqe->optype]-1;
  677. if (unlikely(wc->opcode == -1)) {
  678. ehca_err(cq->device, "Invalid cqe->OPType=%x cqe->status=%x "
  679. "ehca_cq=%p cq_num=%x",
  680. cqe->optype, cqe->status, my_cq, my_cq->cq_number);
  681. /* dump cqe for other infos */
  682. ehca_dmp(cqe, 64, "ehca_cq=%p cq_num=%x",
  683. my_cq, my_cq->cq_number);
  684. /* update also queue adder to throw away this entry!!! */
  685. goto repoll;
  686. }
  687. /* eval ib_wc_status */
  688. if (unlikely(is_error)) {
  689. /* complete with errors */
  690. map_ib_wc_status(cqe->status, &wc->status);
  691. wc->vendor_err = wc->status;
  692. } else
  693. wc->status = IB_WC_SUCCESS;
  694. wc->byte_len = cqe->nr_bytes_transferred;
  695. wc->pkey_index = cqe->pkey_index;
  696. wc->slid = cqe->rlid;
  697. wc->dlid_path_bits = cqe->dlid;
  698. wc->src_qp = cqe->remote_qp_number;
  699. /*
  700. * HW has "Immed data present" and "GRH present" in bits 6 and 5.
  701. * SW defines those in bits 1 and 0, so we can just shift and mask.
  702. */
  703. wc->wc_flags = (cqe->w_completion_flags >> 5) & 3;
  704. wc->ex.imm_data = cpu_to_be32(cqe->immediate_data);
  705. wc->sl = cqe->service_level;
  706. poll_cq_one_exit0:
  707. if (cqe_count > 0)
  708. hipz_update_feca(my_cq, cqe_count);
  709. return ret;
  710. }
  711. static int generate_flush_cqes(struct ehca_qp *my_qp, struct ib_cq *cq,
  712. struct ib_wc *wc, int num_entries,
  713. struct ipz_queue *ipz_queue, int on_sq)
  714. {
  715. int nr = 0;
  716. struct ehca_wqe *wqe;
  717. u64 offset;
  718. struct ehca_queue_map *qmap;
  719. struct ehca_qmap_entry *qmap_entry;
  720. if (on_sq)
  721. qmap = &my_qp->sq_map;
  722. else
  723. qmap = &my_qp->rq_map;
  724. qmap_entry = &qmap->map[qmap->next_wqe_idx];
  725. while ((nr < num_entries) && (qmap_entry->reported == 0)) {
  726. /* generate flush CQE */
  727. memset(wc, 0, sizeof(*wc));
  728. offset = qmap->next_wqe_idx * ipz_queue->qe_size;
  729. wqe = (struct ehca_wqe *)ipz_qeit_calc(ipz_queue, offset);
  730. if (!wqe) {
  731. ehca_err(cq->device, "Invalid wqe offset=%#llx on "
  732. "qp_num=%#x", offset, my_qp->real_qp_num);
  733. return nr;
  734. }
  735. wc->wr_id = replace_wr_id(wqe->work_request_id,
  736. qmap_entry->app_wr_id);
  737. if (on_sq) {
  738. switch (wqe->optype) {
  739. case WQE_OPTYPE_SEND:
  740. wc->opcode = IB_WC_SEND;
  741. break;
  742. case WQE_OPTYPE_RDMAWRITE:
  743. wc->opcode = IB_WC_RDMA_WRITE;
  744. break;
  745. case WQE_OPTYPE_RDMAREAD:
  746. wc->opcode = IB_WC_RDMA_READ;
  747. break;
  748. default:
  749. ehca_err(cq->device, "Invalid optype=%x",
  750. wqe->optype);
  751. return nr;
  752. }
  753. } else
  754. wc->opcode = IB_WC_RECV;
  755. if (wqe->wr_flag & WQE_WRFLAG_IMM_DATA_PRESENT) {
  756. wc->ex.imm_data = wqe->immediate_data;
  757. wc->wc_flags |= IB_WC_WITH_IMM;
  758. }
  759. wc->status = IB_WC_WR_FLUSH_ERR;
  760. wc->qp = &my_qp->ib_qp;
  761. /* mark as reported and advance next_wqe pointer */
  762. qmap_entry->reported = 1;
  763. qmap->next_wqe_idx = next_index(qmap->next_wqe_idx,
  764. qmap->entries);
  765. qmap_entry = &qmap->map[qmap->next_wqe_idx];
  766. wc++; nr++;
  767. }
  768. return nr;
  769. }
  770. int ehca_poll_cq(struct ib_cq *cq, int num_entries, struct ib_wc *wc)
  771. {
  772. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  773. int nr;
  774. struct ehca_qp *err_qp;
  775. struct ib_wc *current_wc = wc;
  776. int ret = 0;
  777. unsigned long flags;
  778. int entries_left = num_entries;
  779. if (num_entries < 1) {
  780. ehca_err(cq->device, "Invalid num_entries=%d ehca_cq=%p "
  781. "cq_num=%x", num_entries, my_cq, my_cq->cq_number);
  782. ret = -EINVAL;
  783. goto poll_cq_exit0;
  784. }
  785. spin_lock_irqsave(&my_cq->spinlock, flags);
  786. /* generate flush cqes for send queues */
  787. list_for_each_entry(err_qp, &my_cq->sqp_err_list, sq_err_node) {
  788. nr = generate_flush_cqes(err_qp, cq, current_wc, entries_left,
  789. &err_qp->ipz_squeue, 1);
  790. entries_left -= nr;
  791. current_wc += nr;
  792. if (entries_left == 0)
  793. break;
  794. }
  795. /* generate flush cqes for receive queues */
  796. list_for_each_entry(err_qp, &my_cq->rqp_err_list, rq_err_node) {
  797. nr = generate_flush_cqes(err_qp, cq, current_wc, entries_left,
  798. &err_qp->ipz_rqueue, 0);
  799. entries_left -= nr;
  800. current_wc += nr;
  801. if (entries_left == 0)
  802. break;
  803. }
  804. for (nr = 0; nr < entries_left; nr++) {
  805. ret = ehca_poll_cq_one(cq, current_wc);
  806. if (ret)
  807. break;
  808. current_wc++;
  809. } /* eof for nr */
  810. entries_left -= nr;
  811. spin_unlock_irqrestore(&my_cq->spinlock, flags);
  812. if (ret == -EAGAIN || !ret)
  813. ret = num_entries - entries_left;
  814. poll_cq_exit0:
  815. return ret;
  816. }
  817. int ehca_req_notify_cq(struct ib_cq *cq, enum ib_cq_notify_flags notify_flags)
  818. {
  819. struct ehca_cq *my_cq = container_of(cq, struct ehca_cq, ib_cq);
  820. int ret = 0;
  821. switch (notify_flags & IB_CQ_SOLICITED_MASK) {
  822. case IB_CQ_SOLICITED:
  823. hipz_set_cqx_n0(my_cq, 1);
  824. break;
  825. case IB_CQ_NEXT_COMP:
  826. hipz_set_cqx_n1(my_cq, 1);
  827. break;
  828. default:
  829. return -EINVAL;
  830. }
  831. if (notify_flags & IB_CQ_REPORT_MISSED_EVENTS) {
  832. unsigned long spl_flags;
  833. spin_lock_irqsave(&my_cq->spinlock, spl_flags);
  834. ret = ipz_qeit_is_valid(&my_cq->ipz_queue);
  835. spin_unlock_irqrestore(&my_cq->spinlock, spl_flags);
  836. }
  837. return ret;
  838. }