mthca_cq.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2005 Cisco Systems, Inc. All rights reserved.
  5. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  6. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  7. *
  8. * This software is available to you under a choice of one of two
  9. * licenses. You may choose to be licensed under the terms of the GNU
  10. * General Public License (GPL) Version 2, available from the file
  11. * COPYING in the main directory of this source tree, or the
  12. * OpenIB.org BSD license below:
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials
  25. * provided with the distribution.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  31. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  32. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  33. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. * SOFTWARE.
  35. *
  36. * $Id: mthca_cq.c 1369 2004-12-20 16:17:07Z roland $
  37. */
  38. #include <linux/init.h>
  39. #include <linux/hardirq.h>
  40. #include <rdma/ib_pack.h>
  41. #include "mthca_dev.h"
  42. #include "mthca_cmd.h"
  43. #include "mthca_memfree.h"
  44. enum {
  45. MTHCA_MAX_DIRECT_CQ_SIZE = 4 * PAGE_SIZE
  46. };
  47. enum {
  48. MTHCA_CQ_ENTRY_SIZE = 0x20
  49. };
  50. /*
  51. * Must be packed because start is 64 bits but only aligned to 32 bits.
  52. */
  53. struct mthca_cq_context {
  54. __be32 flags;
  55. __be64 start;
  56. __be32 logsize_usrpage;
  57. __be32 error_eqn; /* Tavor only */
  58. __be32 comp_eqn;
  59. __be32 pd;
  60. __be32 lkey;
  61. __be32 last_notified_index;
  62. __be32 solicit_producer_index;
  63. __be32 consumer_index;
  64. __be32 producer_index;
  65. __be32 cqn;
  66. __be32 ci_db; /* Arbel only */
  67. __be32 state_db; /* Arbel only */
  68. u32 reserved;
  69. } __attribute__((packed));
  70. #define MTHCA_CQ_STATUS_OK ( 0 << 28)
  71. #define MTHCA_CQ_STATUS_OVERFLOW ( 9 << 28)
  72. #define MTHCA_CQ_STATUS_WRITE_FAIL (10 << 28)
  73. #define MTHCA_CQ_FLAG_TR ( 1 << 18)
  74. #define MTHCA_CQ_FLAG_OI ( 1 << 17)
  75. #define MTHCA_CQ_STATE_DISARMED ( 0 << 8)
  76. #define MTHCA_CQ_STATE_ARMED ( 1 << 8)
  77. #define MTHCA_CQ_STATE_ARMED_SOL ( 4 << 8)
  78. #define MTHCA_EQ_STATE_FIRED (10 << 8)
  79. enum {
  80. MTHCA_ERROR_CQE_OPCODE_MASK = 0xfe
  81. };
  82. enum {
  83. SYNDROME_LOCAL_LENGTH_ERR = 0x01,
  84. SYNDROME_LOCAL_QP_OP_ERR = 0x02,
  85. SYNDROME_LOCAL_EEC_OP_ERR = 0x03,
  86. SYNDROME_LOCAL_PROT_ERR = 0x04,
  87. SYNDROME_WR_FLUSH_ERR = 0x05,
  88. SYNDROME_MW_BIND_ERR = 0x06,
  89. SYNDROME_BAD_RESP_ERR = 0x10,
  90. SYNDROME_LOCAL_ACCESS_ERR = 0x11,
  91. SYNDROME_REMOTE_INVAL_REQ_ERR = 0x12,
  92. SYNDROME_REMOTE_ACCESS_ERR = 0x13,
  93. SYNDROME_REMOTE_OP_ERR = 0x14,
  94. SYNDROME_RETRY_EXC_ERR = 0x15,
  95. SYNDROME_RNR_RETRY_EXC_ERR = 0x16,
  96. SYNDROME_LOCAL_RDD_VIOL_ERR = 0x20,
  97. SYNDROME_REMOTE_INVAL_RD_REQ_ERR = 0x21,
  98. SYNDROME_REMOTE_ABORTED_ERR = 0x22,
  99. SYNDROME_INVAL_EECN_ERR = 0x23,
  100. SYNDROME_INVAL_EEC_STATE_ERR = 0x24
  101. };
  102. struct mthca_cqe {
  103. __be32 my_qpn;
  104. __be32 my_ee;
  105. __be32 rqpn;
  106. __be16 sl_g_mlpath;
  107. __be16 rlid;
  108. __be32 imm_etype_pkey_eec;
  109. __be32 byte_cnt;
  110. __be32 wqe;
  111. u8 opcode;
  112. u8 is_send;
  113. u8 reserved;
  114. u8 owner;
  115. };
  116. struct mthca_err_cqe {
  117. __be32 my_qpn;
  118. u32 reserved1[3];
  119. u8 syndrome;
  120. u8 reserved2;
  121. __be16 db_cnt;
  122. u32 reserved3;
  123. __be32 wqe;
  124. u8 opcode;
  125. u8 reserved4[2];
  126. u8 owner;
  127. };
  128. #define MTHCA_CQ_ENTRY_OWNER_SW (0 << 7)
  129. #define MTHCA_CQ_ENTRY_OWNER_HW (1 << 7)
  130. #define MTHCA_TAVOR_CQ_DB_INC_CI (1 << 24)
  131. #define MTHCA_TAVOR_CQ_DB_REQ_NOT (2 << 24)
  132. #define MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL (3 << 24)
  133. #define MTHCA_TAVOR_CQ_DB_SET_CI (4 << 24)
  134. #define MTHCA_TAVOR_CQ_DB_REQ_NOT_MULT (5 << 24)
  135. #define MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL (1 << 24)
  136. #define MTHCA_ARBEL_CQ_DB_REQ_NOT (2 << 24)
  137. #define MTHCA_ARBEL_CQ_DB_REQ_NOT_MULT (3 << 24)
  138. static inline struct mthca_cqe *get_cqe(struct mthca_cq *cq, int entry)
  139. {
  140. if (cq->is_direct)
  141. return cq->queue.direct.buf + (entry * MTHCA_CQ_ENTRY_SIZE);
  142. else
  143. return cq->queue.page_list[entry * MTHCA_CQ_ENTRY_SIZE / PAGE_SIZE].buf
  144. + (entry * MTHCA_CQ_ENTRY_SIZE) % PAGE_SIZE;
  145. }
  146. static inline struct mthca_cqe *cqe_sw(struct mthca_cq *cq, int i)
  147. {
  148. struct mthca_cqe *cqe = get_cqe(cq, i);
  149. return MTHCA_CQ_ENTRY_OWNER_HW & cqe->owner ? NULL : cqe;
  150. }
  151. static inline struct mthca_cqe *next_cqe_sw(struct mthca_cq *cq)
  152. {
  153. return cqe_sw(cq, cq->cons_index & cq->ibcq.cqe);
  154. }
  155. static inline void set_cqe_hw(struct mthca_cqe *cqe)
  156. {
  157. cqe->owner = MTHCA_CQ_ENTRY_OWNER_HW;
  158. }
  159. static void dump_cqe(struct mthca_dev *dev, void *cqe_ptr)
  160. {
  161. __be32 *cqe = cqe_ptr;
  162. (void) cqe; /* avoid warning if mthca_dbg compiled away... */
  163. mthca_dbg(dev, "CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
  164. be32_to_cpu(cqe[0]), be32_to_cpu(cqe[1]), be32_to_cpu(cqe[2]),
  165. be32_to_cpu(cqe[3]), be32_to_cpu(cqe[4]), be32_to_cpu(cqe[5]),
  166. be32_to_cpu(cqe[6]), be32_to_cpu(cqe[7]));
  167. }
  168. /*
  169. * incr is ignored in native Arbel (mem-free) mode, so cq->cons_index
  170. * should be correct before calling update_cons_index().
  171. */
  172. static inline void update_cons_index(struct mthca_dev *dev, struct mthca_cq *cq,
  173. int incr)
  174. {
  175. __be32 doorbell[2];
  176. if (mthca_is_memfree(dev)) {
  177. *cq->set_ci_db = cpu_to_be32(cq->cons_index);
  178. wmb();
  179. } else {
  180. doorbell[0] = cpu_to_be32(MTHCA_TAVOR_CQ_DB_INC_CI | cq->cqn);
  181. doorbell[1] = cpu_to_be32(incr - 1);
  182. mthca_write64(doorbell,
  183. dev->kar + MTHCA_CQ_DOORBELL,
  184. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  185. }
  186. }
  187. void mthca_cq_completion(struct mthca_dev *dev, u32 cqn)
  188. {
  189. struct mthca_cq *cq;
  190. cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
  191. if (!cq) {
  192. mthca_warn(dev, "Completion event for bogus CQ %08x\n", cqn);
  193. return;
  194. }
  195. ++cq->arm_sn;
  196. cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
  197. }
  198. void mthca_cq_event(struct mthca_dev *dev, u32 cqn,
  199. enum ib_event_type event_type)
  200. {
  201. struct mthca_cq *cq;
  202. struct ib_event event;
  203. spin_lock(&dev->cq_table.lock);
  204. cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
  205. if (cq)
  206. atomic_inc(&cq->refcount);
  207. spin_unlock(&dev->cq_table.lock);
  208. if (!cq) {
  209. mthca_warn(dev, "Async event for bogus CQ %08x\n", cqn);
  210. return;
  211. }
  212. event.device = &dev->ib_dev;
  213. event.event = event_type;
  214. event.element.cq = &cq->ibcq;
  215. if (cq->ibcq.event_handler)
  216. cq->ibcq.event_handler(&event, cq->ibcq.cq_context);
  217. if (atomic_dec_and_test(&cq->refcount))
  218. wake_up(&cq->wait);
  219. }
  220. void mthca_cq_clean(struct mthca_dev *dev, u32 cqn, u32 qpn,
  221. struct mthca_srq *srq)
  222. {
  223. struct mthca_cq *cq;
  224. struct mthca_cqe *cqe;
  225. u32 prod_index;
  226. int nfreed = 0;
  227. spin_lock_irq(&dev->cq_table.lock);
  228. cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
  229. if (cq)
  230. atomic_inc(&cq->refcount);
  231. spin_unlock_irq(&dev->cq_table.lock);
  232. if (!cq)
  233. return;
  234. spin_lock_irq(&cq->lock);
  235. /*
  236. * First we need to find the current producer index, so we
  237. * know where to start cleaning from. It doesn't matter if HW
  238. * adds new entries after this loop -- the QP we're worried
  239. * about is already in RESET, so the new entries won't come
  240. * from our QP and therefore don't need to be checked.
  241. */
  242. for (prod_index = cq->cons_index;
  243. cqe_sw(cq, prod_index & cq->ibcq.cqe);
  244. ++prod_index)
  245. if (prod_index == cq->cons_index + cq->ibcq.cqe)
  246. break;
  247. if (0)
  248. mthca_dbg(dev, "Cleaning QPN %06x from CQN %06x; ci %d, pi %d\n",
  249. qpn, cqn, cq->cons_index, prod_index);
  250. /*
  251. * Now sweep backwards through the CQ, removing CQ entries
  252. * that match our QP by copying older entries on top of them.
  253. */
  254. while ((int) --prod_index - (int) cq->cons_index >= 0) {
  255. cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
  256. if (cqe->my_qpn == cpu_to_be32(qpn)) {
  257. if (srq)
  258. mthca_free_srq_wqe(srq, be32_to_cpu(cqe->wqe));
  259. ++nfreed;
  260. } else if (nfreed)
  261. memcpy(get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe),
  262. cqe, MTHCA_CQ_ENTRY_SIZE);
  263. }
  264. if (nfreed) {
  265. wmb();
  266. cq->cons_index += nfreed;
  267. update_cons_index(dev, cq, nfreed);
  268. }
  269. spin_unlock_irq(&cq->lock);
  270. if (atomic_dec_and_test(&cq->refcount))
  271. wake_up(&cq->wait);
  272. }
  273. static int handle_error_cqe(struct mthca_dev *dev, struct mthca_cq *cq,
  274. struct mthca_qp *qp, int wqe_index, int is_send,
  275. struct mthca_err_cqe *cqe,
  276. struct ib_wc *entry, int *free_cqe)
  277. {
  278. int err;
  279. int dbd;
  280. __be32 new_wqe;
  281. if (cqe->syndrome == SYNDROME_LOCAL_QP_OP_ERR) {
  282. mthca_dbg(dev, "local QP operation err "
  283. "(QPN %06x, WQE @ %08x, CQN %06x, index %d)\n",
  284. be32_to_cpu(cqe->my_qpn), be32_to_cpu(cqe->wqe),
  285. cq->cqn, cq->cons_index);
  286. dump_cqe(dev, cqe);
  287. }
  288. /*
  289. * For completions in error, only work request ID, status (and
  290. * freed resource count for RD) have to be set.
  291. */
  292. switch (cqe->syndrome) {
  293. case SYNDROME_LOCAL_LENGTH_ERR:
  294. entry->status = IB_WC_LOC_LEN_ERR;
  295. break;
  296. case SYNDROME_LOCAL_QP_OP_ERR:
  297. entry->status = IB_WC_LOC_QP_OP_ERR;
  298. break;
  299. case SYNDROME_LOCAL_EEC_OP_ERR:
  300. entry->status = IB_WC_LOC_EEC_OP_ERR;
  301. break;
  302. case SYNDROME_LOCAL_PROT_ERR:
  303. entry->status = IB_WC_LOC_PROT_ERR;
  304. break;
  305. case SYNDROME_WR_FLUSH_ERR:
  306. entry->status = IB_WC_WR_FLUSH_ERR;
  307. break;
  308. case SYNDROME_MW_BIND_ERR:
  309. entry->status = IB_WC_MW_BIND_ERR;
  310. break;
  311. case SYNDROME_BAD_RESP_ERR:
  312. entry->status = IB_WC_BAD_RESP_ERR;
  313. break;
  314. case SYNDROME_LOCAL_ACCESS_ERR:
  315. entry->status = IB_WC_LOC_ACCESS_ERR;
  316. break;
  317. case SYNDROME_REMOTE_INVAL_REQ_ERR:
  318. entry->status = IB_WC_REM_INV_REQ_ERR;
  319. break;
  320. case SYNDROME_REMOTE_ACCESS_ERR:
  321. entry->status = IB_WC_REM_ACCESS_ERR;
  322. break;
  323. case SYNDROME_REMOTE_OP_ERR:
  324. entry->status = IB_WC_REM_OP_ERR;
  325. break;
  326. case SYNDROME_RETRY_EXC_ERR:
  327. entry->status = IB_WC_RETRY_EXC_ERR;
  328. break;
  329. case SYNDROME_RNR_RETRY_EXC_ERR:
  330. entry->status = IB_WC_RNR_RETRY_EXC_ERR;
  331. break;
  332. case SYNDROME_LOCAL_RDD_VIOL_ERR:
  333. entry->status = IB_WC_LOC_RDD_VIOL_ERR;
  334. break;
  335. case SYNDROME_REMOTE_INVAL_RD_REQ_ERR:
  336. entry->status = IB_WC_REM_INV_RD_REQ_ERR;
  337. break;
  338. case SYNDROME_REMOTE_ABORTED_ERR:
  339. entry->status = IB_WC_REM_ABORT_ERR;
  340. break;
  341. case SYNDROME_INVAL_EECN_ERR:
  342. entry->status = IB_WC_INV_EECN_ERR;
  343. break;
  344. case SYNDROME_INVAL_EEC_STATE_ERR:
  345. entry->status = IB_WC_INV_EEC_STATE_ERR;
  346. break;
  347. default:
  348. entry->status = IB_WC_GENERAL_ERR;
  349. break;
  350. }
  351. /*
  352. * Mem-free HCAs always generate one CQE per WQE, even in the
  353. * error case, so we don't have to check the doorbell count, etc.
  354. */
  355. if (mthca_is_memfree(dev))
  356. return 0;
  357. err = mthca_free_err_wqe(dev, qp, is_send, wqe_index, &dbd, &new_wqe);
  358. if (err)
  359. return err;
  360. /*
  361. * If we're at the end of the WQE chain, or we've used up our
  362. * doorbell count, free the CQE. Otherwise just update it for
  363. * the next poll operation.
  364. */
  365. if (!(new_wqe & cpu_to_be32(0x3f)) || (!cqe->db_cnt && dbd))
  366. return 0;
  367. cqe->db_cnt = cpu_to_be16(be16_to_cpu(cqe->db_cnt) - dbd);
  368. cqe->wqe = new_wqe;
  369. cqe->syndrome = SYNDROME_WR_FLUSH_ERR;
  370. *free_cqe = 0;
  371. return 0;
  372. }
  373. static inline int mthca_poll_one(struct mthca_dev *dev,
  374. struct mthca_cq *cq,
  375. struct mthca_qp **cur_qp,
  376. int *freed,
  377. struct ib_wc *entry)
  378. {
  379. struct mthca_wq *wq;
  380. struct mthca_cqe *cqe;
  381. int wqe_index;
  382. int is_error;
  383. int is_send;
  384. int free_cqe = 1;
  385. int err = 0;
  386. cqe = next_cqe_sw(cq);
  387. if (!cqe)
  388. return -EAGAIN;
  389. /*
  390. * Make sure we read CQ entry contents after we've checked the
  391. * ownership bit.
  392. */
  393. rmb();
  394. if (0) {
  395. mthca_dbg(dev, "%x/%d: CQE -> QPN %06x, WQE @ %08x\n",
  396. cq->cqn, cq->cons_index, be32_to_cpu(cqe->my_qpn),
  397. be32_to_cpu(cqe->wqe));
  398. dump_cqe(dev, cqe);
  399. }
  400. is_error = (cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==
  401. MTHCA_ERROR_CQE_OPCODE_MASK;
  402. is_send = is_error ? cqe->opcode & 0x01 : cqe->is_send & 0x80;
  403. if (!*cur_qp || be32_to_cpu(cqe->my_qpn) != (*cur_qp)->qpn) {
  404. /*
  405. * We do not have to take the QP table lock here,
  406. * because CQs will be locked while QPs are removed
  407. * from the table.
  408. */
  409. *cur_qp = mthca_array_get(&dev->qp_table.qp,
  410. be32_to_cpu(cqe->my_qpn) &
  411. (dev->limits.num_qps - 1));
  412. if (!*cur_qp) {
  413. mthca_warn(dev, "CQ entry for unknown QP %06x\n",
  414. be32_to_cpu(cqe->my_qpn) & 0xffffff);
  415. err = -EINVAL;
  416. goto out;
  417. }
  418. }
  419. entry->qp_num = (*cur_qp)->qpn;
  420. if (is_send) {
  421. wq = &(*cur_qp)->sq;
  422. wqe_index = ((be32_to_cpu(cqe->wqe) - (*cur_qp)->send_wqe_offset)
  423. >> wq->wqe_shift);
  424. entry->wr_id = (*cur_qp)->wrid[wqe_index +
  425. (*cur_qp)->rq.max];
  426. } else if ((*cur_qp)->ibqp.srq) {
  427. struct mthca_srq *srq = to_msrq((*cur_qp)->ibqp.srq);
  428. u32 wqe = be32_to_cpu(cqe->wqe);
  429. wq = NULL;
  430. wqe_index = wqe >> srq->wqe_shift;
  431. entry->wr_id = srq->wrid[wqe_index];
  432. mthca_free_srq_wqe(srq, wqe);
  433. } else {
  434. wq = &(*cur_qp)->rq;
  435. wqe_index = be32_to_cpu(cqe->wqe) >> wq->wqe_shift;
  436. entry->wr_id = (*cur_qp)->wrid[wqe_index];
  437. }
  438. if (wq) {
  439. if (wq->last_comp < wqe_index)
  440. wq->tail += wqe_index - wq->last_comp;
  441. else
  442. wq->tail += wqe_index + wq->max - wq->last_comp;
  443. wq->last_comp = wqe_index;
  444. }
  445. if (is_error) {
  446. err = handle_error_cqe(dev, cq, *cur_qp, wqe_index, is_send,
  447. (struct mthca_err_cqe *) cqe,
  448. entry, &free_cqe);
  449. goto out;
  450. }
  451. if (is_send) {
  452. entry->wc_flags = 0;
  453. switch (cqe->opcode) {
  454. case MTHCA_OPCODE_RDMA_WRITE:
  455. entry->opcode = IB_WC_RDMA_WRITE;
  456. break;
  457. case MTHCA_OPCODE_RDMA_WRITE_IMM:
  458. entry->opcode = IB_WC_RDMA_WRITE;
  459. entry->wc_flags |= IB_WC_WITH_IMM;
  460. break;
  461. case MTHCA_OPCODE_SEND:
  462. entry->opcode = IB_WC_SEND;
  463. break;
  464. case MTHCA_OPCODE_SEND_IMM:
  465. entry->opcode = IB_WC_SEND;
  466. entry->wc_flags |= IB_WC_WITH_IMM;
  467. break;
  468. case MTHCA_OPCODE_RDMA_READ:
  469. entry->opcode = IB_WC_RDMA_READ;
  470. entry->byte_len = be32_to_cpu(cqe->byte_cnt);
  471. break;
  472. case MTHCA_OPCODE_ATOMIC_CS:
  473. entry->opcode = IB_WC_COMP_SWAP;
  474. entry->byte_len = be32_to_cpu(cqe->byte_cnt);
  475. break;
  476. case MTHCA_OPCODE_ATOMIC_FA:
  477. entry->opcode = IB_WC_FETCH_ADD;
  478. entry->byte_len = be32_to_cpu(cqe->byte_cnt);
  479. break;
  480. case MTHCA_OPCODE_BIND_MW:
  481. entry->opcode = IB_WC_BIND_MW;
  482. break;
  483. default:
  484. entry->opcode = MTHCA_OPCODE_INVALID;
  485. break;
  486. }
  487. } else {
  488. entry->byte_len = be32_to_cpu(cqe->byte_cnt);
  489. switch (cqe->opcode & 0x1f) {
  490. case IB_OPCODE_SEND_LAST_WITH_IMMEDIATE:
  491. case IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE:
  492. entry->wc_flags = IB_WC_WITH_IMM;
  493. entry->imm_data = cqe->imm_etype_pkey_eec;
  494. entry->opcode = IB_WC_RECV;
  495. break;
  496. case IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE:
  497. case IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE:
  498. entry->wc_flags = IB_WC_WITH_IMM;
  499. entry->imm_data = cqe->imm_etype_pkey_eec;
  500. entry->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  501. break;
  502. default:
  503. entry->wc_flags = 0;
  504. entry->opcode = IB_WC_RECV;
  505. break;
  506. }
  507. entry->slid = be16_to_cpu(cqe->rlid);
  508. entry->sl = be16_to_cpu(cqe->sl_g_mlpath) >> 12;
  509. entry->src_qp = be32_to_cpu(cqe->rqpn) & 0xffffff;
  510. entry->dlid_path_bits = be16_to_cpu(cqe->sl_g_mlpath) & 0x7f;
  511. entry->pkey_index = be32_to_cpu(cqe->imm_etype_pkey_eec) >> 16;
  512. entry->wc_flags |= be16_to_cpu(cqe->sl_g_mlpath) & 0x80 ?
  513. IB_WC_GRH : 0;
  514. }
  515. entry->status = IB_WC_SUCCESS;
  516. out:
  517. if (likely(free_cqe)) {
  518. set_cqe_hw(cqe);
  519. ++(*freed);
  520. ++cq->cons_index;
  521. }
  522. return err;
  523. }
  524. int mthca_poll_cq(struct ib_cq *ibcq, int num_entries,
  525. struct ib_wc *entry)
  526. {
  527. struct mthca_dev *dev = to_mdev(ibcq->device);
  528. struct mthca_cq *cq = to_mcq(ibcq);
  529. struct mthca_qp *qp = NULL;
  530. unsigned long flags;
  531. int err = 0;
  532. int freed = 0;
  533. int npolled;
  534. spin_lock_irqsave(&cq->lock, flags);
  535. for (npolled = 0; npolled < num_entries; ++npolled) {
  536. err = mthca_poll_one(dev, cq, &qp,
  537. &freed, entry + npolled);
  538. if (err)
  539. break;
  540. }
  541. if (freed) {
  542. wmb();
  543. update_cons_index(dev, cq, freed);
  544. }
  545. spin_unlock_irqrestore(&cq->lock, flags);
  546. return err == 0 || err == -EAGAIN ? npolled : err;
  547. }
  548. int mthca_tavor_arm_cq(struct ib_cq *cq, enum ib_cq_notify notify)
  549. {
  550. __be32 doorbell[2];
  551. doorbell[0] = cpu_to_be32((notify == IB_CQ_SOLICITED ?
  552. MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL :
  553. MTHCA_TAVOR_CQ_DB_REQ_NOT) |
  554. to_mcq(cq)->cqn);
  555. doorbell[1] = (__force __be32) 0xffffffff;
  556. mthca_write64(doorbell,
  557. to_mdev(cq->device)->kar + MTHCA_CQ_DOORBELL,
  558. MTHCA_GET_DOORBELL_LOCK(&to_mdev(cq->device)->doorbell_lock));
  559. return 0;
  560. }
  561. int mthca_arbel_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
  562. {
  563. struct mthca_cq *cq = to_mcq(ibcq);
  564. __be32 doorbell[2];
  565. u32 sn;
  566. __be32 ci;
  567. sn = cq->arm_sn & 3;
  568. ci = cpu_to_be32(cq->cons_index);
  569. doorbell[0] = ci;
  570. doorbell[1] = cpu_to_be32((cq->cqn << 8) | (2 << 5) | (sn << 3) |
  571. (notify == IB_CQ_SOLICITED ? 1 : 2));
  572. mthca_write_db_rec(doorbell, cq->arm_db);
  573. /*
  574. * Make sure that the doorbell record in host memory is
  575. * written before ringing the doorbell via PCI MMIO.
  576. */
  577. wmb();
  578. doorbell[0] = cpu_to_be32((sn << 28) |
  579. (notify == IB_CQ_SOLICITED ?
  580. MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL :
  581. MTHCA_ARBEL_CQ_DB_REQ_NOT) |
  582. cq->cqn);
  583. doorbell[1] = ci;
  584. mthca_write64(doorbell,
  585. to_mdev(ibcq->device)->kar + MTHCA_CQ_DOORBELL,
  586. MTHCA_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->doorbell_lock));
  587. return 0;
  588. }
  589. static void mthca_free_cq_buf(struct mthca_dev *dev, struct mthca_cq *cq)
  590. {
  591. mthca_buf_free(dev, (cq->ibcq.cqe + 1) * MTHCA_CQ_ENTRY_SIZE,
  592. &cq->queue, cq->is_direct, &cq->mr);
  593. }
  594. int mthca_init_cq(struct mthca_dev *dev, int nent,
  595. struct mthca_ucontext *ctx, u32 pdn,
  596. struct mthca_cq *cq)
  597. {
  598. int size = nent * MTHCA_CQ_ENTRY_SIZE;
  599. struct mthca_mailbox *mailbox;
  600. struct mthca_cq_context *cq_context;
  601. int err = -ENOMEM;
  602. u8 status;
  603. int i;
  604. might_sleep();
  605. cq->ibcq.cqe = nent - 1;
  606. cq->is_kernel = !ctx;
  607. cq->cqn = mthca_alloc(&dev->cq_table.alloc);
  608. if (cq->cqn == -1)
  609. return -ENOMEM;
  610. if (mthca_is_memfree(dev)) {
  611. err = mthca_table_get(dev, dev->cq_table.table, cq->cqn);
  612. if (err)
  613. goto err_out;
  614. if (cq->is_kernel) {
  615. cq->arm_sn = 1;
  616. err = -ENOMEM;
  617. cq->set_ci_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_SET_CI,
  618. cq->cqn, &cq->set_ci_db);
  619. if (cq->set_ci_db_index < 0)
  620. goto err_out_icm;
  621. cq->arm_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_ARM,
  622. cq->cqn, &cq->arm_db);
  623. if (cq->arm_db_index < 0)
  624. goto err_out_ci;
  625. }
  626. }
  627. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  628. if (IS_ERR(mailbox))
  629. goto err_out_arm;
  630. cq_context = mailbox->buf;
  631. if (cq->is_kernel) {
  632. err = mthca_buf_alloc(dev, size, MTHCA_MAX_DIRECT_CQ_SIZE,
  633. &cq->queue, &cq->is_direct,
  634. &dev->driver_pd, 1, &cq->mr);
  635. if (err)
  636. goto err_out_mailbox;
  637. for (i = 0; i < nent; ++i)
  638. set_cqe_hw(get_cqe(cq, i));
  639. }
  640. spin_lock_init(&cq->lock);
  641. atomic_set(&cq->refcount, 1);
  642. init_waitqueue_head(&cq->wait);
  643. memset(cq_context, 0, sizeof *cq_context);
  644. cq_context->flags = cpu_to_be32(MTHCA_CQ_STATUS_OK |
  645. MTHCA_CQ_STATE_DISARMED |
  646. MTHCA_CQ_FLAG_TR);
  647. cq_context->logsize_usrpage = cpu_to_be32((ffs(nent) - 1) << 24);
  648. if (ctx)
  649. cq_context->logsize_usrpage |= cpu_to_be32(ctx->uar.index);
  650. else
  651. cq_context->logsize_usrpage |= cpu_to_be32(dev->driver_uar.index);
  652. cq_context->error_eqn = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);
  653. cq_context->comp_eqn = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_COMP].eqn);
  654. cq_context->pd = cpu_to_be32(pdn);
  655. cq_context->lkey = cpu_to_be32(cq->mr.ibmr.lkey);
  656. cq_context->cqn = cpu_to_be32(cq->cqn);
  657. if (mthca_is_memfree(dev)) {
  658. cq_context->ci_db = cpu_to_be32(cq->set_ci_db_index);
  659. cq_context->state_db = cpu_to_be32(cq->arm_db_index);
  660. }
  661. err = mthca_SW2HW_CQ(dev, mailbox, cq->cqn, &status);
  662. if (err) {
  663. mthca_warn(dev, "SW2HW_CQ failed (%d)\n", err);
  664. goto err_out_free_mr;
  665. }
  666. if (status) {
  667. mthca_warn(dev, "SW2HW_CQ returned status 0x%02x\n",
  668. status);
  669. err = -EINVAL;
  670. goto err_out_free_mr;
  671. }
  672. spin_lock_irq(&dev->cq_table.lock);
  673. if (mthca_array_set(&dev->cq_table.cq,
  674. cq->cqn & (dev->limits.num_cqs - 1),
  675. cq)) {
  676. spin_unlock_irq(&dev->cq_table.lock);
  677. goto err_out_free_mr;
  678. }
  679. spin_unlock_irq(&dev->cq_table.lock);
  680. cq->cons_index = 0;
  681. mthca_free_mailbox(dev, mailbox);
  682. return 0;
  683. err_out_free_mr:
  684. if (cq->is_kernel)
  685. mthca_free_cq_buf(dev, cq);
  686. err_out_mailbox:
  687. mthca_free_mailbox(dev, mailbox);
  688. err_out_arm:
  689. if (cq->is_kernel && mthca_is_memfree(dev))
  690. mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
  691. err_out_ci:
  692. if (cq->is_kernel && mthca_is_memfree(dev))
  693. mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
  694. err_out_icm:
  695. mthca_table_put(dev, dev->cq_table.table, cq->cqn);
  696. err_out:
  697. mthca_free(&dev->cq_table.alloc, cq->cqn);
  698. return err;
  699. }
  700. void mthca_free_cq(struct mthca_dev *dev,
  701. struct mthca_cq *cq)
  702. {
  703. struct mthca_mailbox *mailbox;
  704. int err;
  705. u8 status;
  706. might_sleep();
  707. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  708. if (IS_ERR(mailbox)) {
  709. mthca_warn(dev, "No memory for mailbox to free CQ.\n");
  710. return;
  711. }
  712. err = mthca_HW2SW_CQ(dev, mailbox, cq->cqn, &status);
  713. if (err)
  714. mthca_warn(dev, "HW2SW_CQ failed (%d)\n", err);
  715. else if (status)
  716. mthca_warn(dev, "HW2SW_CQ returned status 0x%02x\n", status);
  717. if (0) {
  718. __be32 *ctx = mailbox->buf;
  719. int j;
  720. printk(KERN_ERR "context for CQN %x (cons index %x, next sw %d)\n",
  721. cq->cqn, cq->cons_index,
  722. cq->is_kernel ? !!next_cqe_sw(cq) : 0);
  723. for (j = 0; j < 16; ++j)
  724. printk(KERN_ERR "[%2x] %08x\n", j * 4, be32_to_cpu(ctx[j]));
  725. }
  726. spin_lock_irq(&dev->cq_table.lock);
  727. mthca_array_clear(&dev->cq_table.cq,
  728. cq->cqn & (dev->limits.num_cqs - 1));
  729. spin_unlock_irq(&dev->cq_table.lock);
  730. if (dev->mthca_flags & MTHCA_FLAG_MSI_X)
  731. synchronize_irq(dev->eq_table.eq[MTHCA_EQ_COMP].msi_x_vector);
  732. else
  733. synchronize_irq(dev->pdev->irq);
  734. atomic_dec(&cq->refcount);
  735. wait_event(cq->wait, !atomic_read(&cq->refcount));
  736. if (cq->is_kernel) {
  737. mthca_free_cq_buf(dev, cq);
  738. if (mthca_is_memfree(dev)) {
  739. mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
  740. mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
  741. }
  742. }
  743. mthca_table_put(dev, dev->cq_table.table, cq->cqn);
  744. mthca_free(&dev->cq_table.alloc, cq->cqn);
  745. mthca_free_mailbox(dev, mailbox);
  746. }
  747. int __devinit mthca_init_cq_table(struct mthca_dev *dev)
  748. {
  749. int err;
  750. spin_lock_init(&dev->cq_table.lock);
  751. err = mthca_alloc_init(&dev->cq_table.alloc,
  752. dev->limits.num_cqs,
  753. (1 << 24) - 1,
  754. dev->limits.reserved_cqs);
  755. if (err)
  756. return err;
  757. err = mthca_array_init(&dev->cq_table.cq,
  758. dev->limits.num_cqs);
  759. if (err)
  760. mthca_alloc_cleanup(&dev->cq_table.alloc);
  761. return err;
  762. }
  763. void __devexit mthca_cleanup_cq_table(struct mthca_dev *dev)
  764. {
  765. mthca_array_cleanup(&dev->cq_table.cq, dev->limits.num_cqs);
  766. mthca_alloc_cleanup(&dev->cq_table.alloc);
  767. }