mthca_cq.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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, 2006 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/hardirq.h>
  39. #include <asm/io.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 vendor_err;
  121. __be16 db_cnt;
  122. u32 reserved2;
  123. __be32 wqe;
  124. u8 opcode;
  125. u8 reserved3[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_from_buf(struct mthca_cq_buf *buf,
  139. int entry)
  140. {
  141. if (buf->is_direct)
  142. return buf->queue.direct.buf + (entry * MTHCA_CQ_ENTRY_SIZE);
  143. else
  144. return buf->queue.page_list[entry * MTHCA_CQ_ENTRY_SIZE / PAGE_SIZE].buf
  145. + (entry * MTHCA_CQ_ENTRY_SIZE) % PAGE_SIZE;
  146. }
  147. static inline struct mthca_cqe *get_cqe(struct mthca_cq *cq, int entry)
  148. {
  149. return get_cqe_from_buf(&cq->buf, entry);
  150. }
  151. static inline struct mthca_cqe *cqe_sw(struct mthca_cqe *cqe)
  152. {
  153. return MTHCA_CQ_ENTRY_OWNER_HW & cqe->owner ? NULL : cqe;
  154. }
  155. static inline struct mthca_cqe *next_cqe_sw(struct mthca_cq *cq)
  156. {
  157. return cqe_sw(get_cqe(cq, cq->cons_index & cq->ibcq.cqe));
  158. }
  159. static inline void set_cqe_hw(struct mthca_cqe *cqe)
  160. {
  161. cqe->owner = MTHCA_CQ_ENTRY_OWNER_HW;
  162. }
  163. static void dump_cqe(struct mthca_dev *dev, void *cqe_ptr)
  164. {
  165. __be32 *cqe = cqe_ptr;
  166. (void) cqe; /* avoid warning if mthca_dbg compiled away... */
  167. mthca_dbg(dev, "CQE contents %08x %08x %08x %08x %08x %08x %08x %08x\n",
  168. be32_to_cpu(cqe[0]), be32_to_cpu(cqe[1]), be32_to_cpu(cqe[2]),
  169. be32_to_cpu(cqe[3]), be32_to_cpu(cqe[4]), be32_to_cpu(cqe[5]),
  170. be32_to_cpu(cqe[6]), be32_to_cpu(cqe[7]));
  171. }
  172. /*
  173. * incr is ignored in native Arbel (mem-free) mode, so cq->cons_index
  174. * should be correct before calling update_cons_index().
  175. */
  176. static inline void update_cons_index(struct mthca_dev *dev, struct mthca_cq *cq,
  177. int incr)
  178. {
  179. __be32 doorbell[2];
  180. if (mthca_is_memfree(dev)) {
  181. *cq->set_ci_db = cpu_to_be32(cq->cons_index);
  182. wmb();
  183. } else {
  184. doorbell[0] = cpu_to_be32(MTHCA_TAVOR_CQ_DB_INC_CI | cq->cqn);
  185. doorbell[1] = cpu_to_be32(incr - 1);
  186. mthca_write64(doorbell,
  187. dev->kar + MTHCA_CQ_DOORBELL,
  188. MTHCA_GET_DOORBELL_LOCK(&dev->doorbell_lock));
  189. /*
  190. * Make sure doorbells don't leak out of CQ spinlock
  191. * and reach the HCA out of order:
  192. */
  193. mmiowb();
  194. }
  195. }
  196. void mthca_cq_completion(struct mthca_dev *dev, u32 cqn)
  197. {
  198. struct mthca_cq *cq;
  199. cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
  200. if (!cq) {
  201. mthca_warn(dev, "Completion event for bogus CQ %08x\n", cqn);
  202. return;
  203. }
  204. ++cq->arm_sn;
  205. cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
  206. }
  207. void mthca_cq_event(struct mthca_dev *dev, u32 cqn,
  208. enum ib_event_type event_type)
  209. {
  210. struct mthca_cq *cq;
  211. struct ib_event event;
  212. spin_lock(&dev->cq_table.lock);
  213. cq = mthca_array_get(&dev->cq_table.cq, cqn & (dev->limits.num_cqs - 1));
  214. if (cq)
  215. ++cq->refcount;
  216. spin_unlock(&dev->cq_table.lock);
  217. if (!cq) {
  218. mthca_warn(dev, "Async event for bogus CQ %08x\n", cqn);
  219. return;
  220. }
  221. event.device = &dev->ib_dev;
  222. event.event = event_type;
  223. event.element.cq = &cq->ibcq;
  224. if (cq->ibcq.event_handler)
  225. cq->ibcq.event_handler(&event, cq->ibcq.cq_context);
  226. spin_lock(&dev->cq_table.lock);
  227. if (!--cq->refcount)
  228. wake_up(&cq->wait);
  229. spin_unlock(&dev->cq_table.lock);
  230. }
  231. static inline int is_recv_cqe(struct mthca_cqe *cqe)
  232. {
  233. if ((cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==
  234. MTHCA_ERROR_CQE_OPCODE_MASK)
  235. return !(cqe->opcode & 0x01);
  236. else
  237. return !(cqe->is_send & 0x80);
  238. }
  239. void mthca_cq_clean(struct mthca_dev *dev, struct mthca_cq *cq, u32 qpn,
  240. struct mthca_srq *srq)
  241. {
  242. struct mthca_cqe *cqe;
  243. u32 prod_index;
  244. int nfreed = 0;
  245. spin_lock_irq(&cq->lock);
  246. /*
  247. * First we need to find the current producer index, so we
  248. * know where to start cleaning from. It doesn't matter if HW
  249. * adds new entries after this loop -- the QP we're worried
  250. * about is already in RESET, so the new entries won't come
  251. * from our QP and therefore don't need to be checked.
  252. */
  253. for (prod_index = cq->cons_index;
  254. cqe_sw(get_cqe(cq, prod_index & cq->ibcq.cqe));
  255. ++prod_index)
  256. if (prod_index == cq->cons_index + cq->ibcq.cqe)
  257. break;
  258. if (0)
  259. mthca_dbg(dev, "Cleaning QPN %06x from CQN %06x; ci %d, pi %d\n",
  260. qpn, cq->cqn, cq->cons_index, prod_index);
  261. /*
  262. * Now sweep backwards through the CQ, removing CQ entries
  263. * that match our QP by copying older entries on top of them.
  264. */
  265. while ((int) --prod_index - (int) cq->cons_index >= 0) {
  266. cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
  267. if (cqe->my_qpn == cpu_to_be32(qpn)) {
  268. if (srq && is_recv_cqe(cqe))
  269. mthca_free_srq_wqe(srq, be32_to_cpu(cqe->wqe));
  270. ++nfreed;
  271. } else if (nfreed)
  272. memcpy(get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe),
  273. cqe, MTHCA_CQ_ENTRY_SIZE);
  274. }
  275. if (nfreed) {
  276. wmb();
  277. cq->cons_index += nfreed;
  278. update_cons_index(dev, cq, nfreed);
  279. }
  280. spin_unlock_irq(&cq->lock);
  281. }
  282. void mthca_cq_resize_copy_cqes(struct mthca_cq *cq)
  283. {
  284. int i;
  285. /*
  286. * In Tavor mode, the hardware keeps the consumer and producer
  287. * indices mod the CQ size. Since we might be making the CQ
  288. * bigger, we need to deal with the case where the producer
  289. * index wrapped around before the CQ was resized.
  290. */
  291. if (!mthca_is_memfree(to_mdev(cq->ibcq.device)) &&
  292. cq->ibcq.cqe < cq->resize_buf->cqe) {
  293. cq->cons_index &= cq->ibcq.cqe;
  294. if (cqe_sw(get_cqe(cq, cq->ibcq.cqe)))
  295. cq->cons_index -= cq->ibcq.cqe + 1;
  296. }
  297. for (i = cq->cons_index; cqe_sw(get_cqe(cq, i & cq->ibcq.cqe)); ++i)
  298. memcpy(get_cqe_from_buf(&cq->resize_buf->buf,
  299. i & cq->resize_buf->cqe),
  300. get_cqe(cq, i & cq->ibcq.cqe), MTHCA_CQ_ENTRY_SIZE);
  301. }
  302. int mthca_alloc_cq_buf(struct mthca_dev *dev, struct mthca_cq_buf *buf, int nent)
  303. {
  304. int ret;
  305. int i;
  306. ret = mthca_buf_alloc(dev, nent * MTHCA_CQ_ENTRY_SIZE,
  307. MTHCA_MAX_DIRECT_CQ_SIZE,
  308. &buf->queue, &buf->is_direct,
  309. &dev->driver_pd, 1, &buf->mr);
  310. if (ret)
  311. return ret;
  312. for (i = 0; i < nent; ++i)
  313. set_cqe_hw(get_cqe_from_buf(buf, i));
  314. return 0;
  315. }
  316. void mthca_free_cq_buf(struct mthca_dev *dev, struct mthca_cq_buf *buf, int cqe)
  317. {
  318. mthca_buf_free(dev, (cqe + 1) * MTHCA_CQ_ENTRY_SIZE, &buf->queue,
  319. buf->is_direct, &buf->mr);
  320. }
  321. static void handle_error_cqe(struct mthca_dev *dev, struct mthca_cq *cq,
  322. struct mthca_qp *qp, int wqe_index, int is_send,
  323. struct mthca_err_cqe *cqe,
  324. struct ib_wc *entry, int *free_cqe)
  325. {
  326. int dbd;
  327. __be32 new_wqe;
  328. if (cqe->syndrome == SYNDROME_LOCAL_QP_OP_ERR) {
  329. mthca_dbg(dev, "local QP operation err "
  330. "(QPN %06x, WQE @ %08x, CQN %06x, index %d)\n",
  331. be32_to_cpu(cqe->my_qpn), be32_to_cpu(cqe->wqe),
  332. cq->cqn, cq->cons_index);
  333. dump_cqe(dev, cqe);
  334. }
  335. /*
  336. * For completions in error, only work request ID, status, vendor error
  337. * (and freed resource count for RD) have to be set.
  338. */
  339. switch (cqe->syndrome) {
  340. case SYNDROME_LOCAL_LENGTH_ERR:
  341. entry->status = IB_WC_LOC_LEN_ERR;
  342. break;
  343. case SYNDROME_LOCAL_QP_OP_ERR:
  344. entry->status = IB_WC_LOC_QP_OP_ERR;
  345. break;
  346. case SYNDROME_LOCAL_EEC_OP_ERR:
  347. entry->status = IB_WC_LOC_EEC_OP_ERR;
  348. break;
  349. case SYNDROME_LOCAL_PROT_ERR:
  350. entry->status = IB_WC_LOC_PROT_ERR;
  351. break;
  352. case SYNDROME_WR_FLUSH_ERR:
  353. entry->status = IB_WC_WR_FLUSH_ERR;
  354. break;
  355. case SYNDROME_MW_BIND_ERR:
  356. entry->status = IB_WC_MW_BIND_ERR;
  357. break;
  358. case SYNDROME_BAD_RESP_ERR:
  359. entry->status = IB_WC_BAD_RESP_ERR;
  360. break;
  361. case SYNDROME_LOCAL_ACCESS_ERR:
  362. entry->status = IB_WC_LOC_ACCESS_ERR;
  363. break;
  364. case SYNDROME_REMOTE_INVAL_REQ_ERR:
  365. entry->status = IB_WC_REM_INV_REQ_ERR;
  366. break;
  367. case SYNDROME_REMOTE_ACCESS_ERR:
  368. entry->status = IB_WC_REM_ACCESS_ERR;
  369. break;
  370. case SYNDROME_REMOTE_OP_ERR:
  371. entry->status = IB_WC_REM_OP_ERR;
  372. break;
  373. case SYNDROME_RETRY_EXC_ERR:
  374. entry->status = IB_WC_RETRY_EXC_ERR;
  375. break;
  376. case SYNDROME_RNR_RETRY_EXC_ERR:
  377. entry->status = IB_WC_RNR_RETRY_EXC_ERR;
  378. break;
  379. case SYNDROME_LOCAL_RDD_VIOL_ERR:
  380. entry->status = IB_WC_LOC_RDD_VIOL_ERR;
  381. break;
  382. case SYNDROME_REMOTE_INVAL_RD_REQ_ERR:
  383. entry->status = IB_WC_REM_INV_RD_REQ_ERR;
  384. break;
  385. case SYNDROME_REMOTE_ABORTED_ERR:
  386. entry->status = IB_WC_REM_ABORT_ERR;
  387. break;
  388. case SYNDROME_INVAL_EECN_ERR:
  389. entry->status = IB_WC_INV_EECN_ERR;
  390. break;
  391. case SYNDROME_INVAL_EEC_STATE_ERR:
  392. entry->status = IB_WC_INV_EEC_STATE_ERR;
  393. break;
  394. default:
  395. entry->status = IB_WC_GENERAL_ERR;
  396. break;
  397. }
  398. entry->vendor_err = cqe->vendor_err;
  399. /*
  400. * Mem-free HCAs always generate one CQE per WQE, even in the
  401. * error case, so we don't have to check the doorbell count, etc.
  402. */
  403. if (mthca_is_memfree(dev))
  404. return;
  405. mthca_free_err_wqe(dev, qp, is_send, wqe_index, &dbd, &new_wqe);
  406. /*
  407. * If we're at the end of the WQE chain, or we've used up our
  408. * doorbell count, free the CQE. Otherwise just update it for
  409. * the next poll operation.
  410. */
  411. if (!(new_wqe & cpu_to_be32(0x3f)) || (!cqe->db_cnt && dbd))
  412. return;
  413. cqe->db_cnt = cpu_to_be16(be16_to_cpu(cqe->db_cnt) - dbd);
  414. cqe->wqe = new_wqe;
  415. cqe->syndrome = SYNDROME_WR_FLUSH_ERR;
  416. *free_cqe = 0;
  417. }
  418. static inline int mthca_poll_one(struct mthca_dev *dev,
  419. struct mthca_cq *cq,
  420. struct mthca_qp **cur_qp,
  421. int *freed,
  422. struct ib_wc *entry)
  423. {
  424. struct mthca_wq *wq;
  425. struct mthca_cqe *cqe;
  426. int wqe_index;
  427. int is_error;
  428. int is_send;
  429. int free_cqe = 1;
  430. int err = 0;
  431. cqe = next_cqe_sw(cq);
  432. if (!cqe)
  433. return -EAGAIN;
  434. /*
  435. * Make sure we read CQ entry contents after we've checked the
  436. * ownership bit.
  437. */
  438. rmb();
  439. if (0) {
  440. mthca_dbg(dev, "%x/%d: CQE -> QPN %06x, WQE @ %08x\n",
  441. cq->cqn, cq->cons_index, be32_to_cpu(cqe->my_qpn),
  442. be32_to_cpu(cqe->wqe));
  443. dump_cqe(dev, cqe);
  444. }
  445. is_error = (cqe->opcode & MTHCA_ERROR_CQE_OPCODE_MASK) ==
  446. MTHCA_ERROR_CQE_OPCODE_MASK;
  447. is_send = is_error ? cqe->opcode & 0x01 : cqe->is_send & 0x80;
  448. if (!*cur_qp || be32_to_cpu(cqe->my_qpn) != (*cur_qp)->qpn) {
  449. /*
  450. * We do not have to take the QP table lock here,
  451. * because CQs will be locked while QPs are removed
  452. * from the table.
  453. */
  454. *cur_qp = mthca_array_get(&dev->qp_table.qp,
  455. be32_to_cpu(cqe->my_qpn) &
  456. (dev->limits.num_qps - 1));
  457. if (!*cur_qp) {
  458. mthca_warn(dev, "CQ entry for unknown QP %06x\n",
  459. be32_to_cpu(cqe->my_qpn) & 0xffffff);
  460. err = -EINVAL;
  461. goto out;
  462. }
  463. }
  464. entry->qp_num = (*cur_qp)->qpn;
  465. if (is_send) {
  466. wq = &(*cur_qp)->sq;
  467. wqe_index = ((be32_to_cpu(cqe->wqe) - (*cur_qp)->send_wqe_offset)
  468. >> wq->wqe_shift);
  469. entry->wr_id = (*cur_qp)->wrid[wqe_index +
  470. (*cur_qp)->rq.max];
  471. } else if ((*cur_qp)->ibqp.srq) {
  472. struct mthca_srq *srq = to_msrq((*cur_qp)->ibqp.srq);
  473. u32 wqe = be32_to_cpu(cqe->wqe);
  474. wq = NULL;
  475. wqe_index = wqe >> srq->wqe_shift;
  476. entry->wr_id = srq->wrid[wqe_index];
  477. mthca_free_srq_wqe(srq, wqe);
  478. } else {
  479. s32 wqe;
  480. wq = &(*cur_qp)->rq;
  481. wqe = be32_to_cpu(cqe->wqe);
  482. wqe_index = wqe >> wq->wqe_shift;
  483. /*
  484. * WQE addr == base - 1 might be reported in receive completion
  485. * with error instead of (rq size - 1) by Sinai FW 1.0.800 and
  486. * Arbel FW 5.1.400. This bug should be fixed in later FW revs.
  487. */
  488. if (unlikely(wqe_index < 0))
  489. wqe_index = wq->max - 1;
  490. entry->wr_id = (*cur_qp)->wrid[wqe_index];
  491. }
  492. if (wq) {
  493. if (wq->last_comp < wqe_index)
  494. wq->tail += wqe_index - wq->last_comp;
  495. else
  496. wq->tail += wqe_index + wq->max - wq->last_comp;
  497. wq->last_comp = wqe_index;
  498. }
  499. if (is_error) {
  500. handle_error_cqe(dev, cq, *cur_qp, wqe_index, is_send,
  501. (struct mthca_err_cqe *) cqe,
  502. entry, &free_cqe);
  503. goto out;
  504. }
  505. if (is_send) {
  506. entry->wc_flags = 0;
  507. switch (cqe->opcode) {
  508. case MTHCA_OPCODE_RDMA_WRITE:
  509. entry->opcode = IB_WC_RDMA_WRITE;
  510. break;
  511. case MTHCA_OPCODE_RDMA_WRITE_IMM:
  512. entry->opcode = IB_WC_RDMA_WRITE;
  513. entry->wc_flags |= IB_WC_WITH_IMM;
  514. break;
  515. case MTHCA_OPCODE_SEND:
  516. entry->opcode = IB_WC_SEND;
  517. break;
  518. case MTHCA_OPCODE_SEND_IMM:
  519. entry->opcode = IB_WC_SEND;
  520. entry->wc_flags |= IB_WC_WITH_IMM;
  521. break;
  522. case MTHCA_OPCODE_RDMA_READ:
  523. entry->opcode = IB_WC_RDMA_READ;
  524. entry->byte_len = be32_to_cpu(cqe->byte_cnt);
  525. break;
  526. case MTHCA_OPCODE_ATOMIC_CS:
  527. entry->opcode = IB_WC_COMP_SWAP;
  528. entry->byte_len = be32_to_cpu(cqe->byte_cnt);
  529. break;
  530. case MTHCA_OPCODE_ATOMIC_FA:
  531. entry->opcode = IB_WC_FETCH_ADD;
  532. entry->byte_len = be32_to_cpu(cqe->byte_cnt);
  533. break;
  534. case MTHCA_OPCODE_BIND_MW:
  535. entry->opcode = IB_WC_BIND_MW;
  536. break;
  537. default:
  538. entry->opcode = MTHCA_OPCODE_INVALID;
  539. break;
  540. }
  541. } else {
  542. entry->byte_len = be32_to_cpu(cqe->byte_cnt);
  543. switch (cqe->opcode & 0x1f) {
  544. case IB_OPCODE_SEND_LAST_WITH_IMMEDIATE:
  545. case IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE:
  546. entry->wc_flags = IB_WC_WITH_IMM;
  547. entry->imm_data = cqe->imm_etype_pkey_eec;
  548. entry->opcode = IB_WC_RECV;
  549. break;
  550. case IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE:
  551. case IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE:
  552. entry->wc_flags = IB_WC_WITH_IMM;
  553. entry->imm_data = cqe->imm_etype_pkey_eec;
  554. entry->opcode = IB_WC_RECV_RDMA_WITH_IMM;
  555. break;
  556. default:
  557. entry->wc_flags = 0;
  558. entry->opcode = IB_WC_RECV;
  559. break;
  560. }
  561. entry->slid = be16_to_cpu(cqe->rlid);
  562. entry->sl = be16_to_cpu(cqe->sl_g_mlpath) >> 12;
  563. entry->src_qp = be32_to_cpu(cqe->rqpn) & 0xffffff;
  564. entry->dlid_path_bits = be16_to_cpu(cqe->sl_g_mlpath) & 0x7f;
  565. entry->pkey_index = be32_to_cpu(cqe->imm_etype_pkey_eec) >> 16;
  566. entry->wc_flags |= be16_to_cpu(cqe->sl_g_mlpath) & 0x80 ?
  567. IB_WC_GRH : 0;
  568. }
  569. entry->status = IB_WC_SUCCESS;
  570. out:
  571. if (likely(free_cqe)) {
  572. set_cqe_hw(cqe);
  573. ++(*freed);
  574. ++cq->cons_index;
  575. }
  576. return err;
  577. }
  578. int mthca_poll_cq(struct ib_cq *ibcq, int num_entries,
  579. struct ib_wc *entry)
  580. {
  581. struct mthca_dev *dev = to_mdev(ibcq->device);
  582. struct mthca_cq *cq = to_mcq(ibcq);
  583. struct mthca_qp *qp = NULL;
  584. unsigned long flags;
  585. int err = 0;
  586. int freed = 0;
  587. int npolled;
  588. spin_lock_irqsave(&cq->lock, flags);
  589. npolled = 0;
  590. repoll:
  591. while (npolled < num_entries) {
  592. err = mthca_poll_one(dev, cq, &qp,
  593. &freed, entry + npolled);
  594. if (err)
  595. break;
  596. ++npolled;
  597. }
  598. if (freed) {
  599. wmb();
  600. update_cons_index(dev, cq, freed);
  601. }
  602. /*
  603. * If a CQ resize is in progress and we discovered that the
  604. * old buffer is empty, then peek in the new buffer, and if
  605. * it's not empty, switch to the new buffer and continue
  606. * polling there.
  607. */
  608. if (unlikely(err == -EAGAIN && cq->resize_buf &&
  609. cq->resize_buf->state == CQ_RESIZE_READY)) {
  610. /*
  611. * In Tavor mode, the hardware keeps the producer
  612. * index modulo the CQ size. Since we might be making
  613. * the CQ bigger, we need to mask our consumer index
  614. * using the size of the old CQ buffer before looking
  615. * in the new CQ buffer.
  616. */
  617. if (!mthca_is_memfree(dev))
  618. cq->cons_index &= cq->ibcq.cqe;
  619. if (cqe_sw(get_cqe_from_buf(&cq->resize_buf->buf,
  620. cq->cons_index & cq->resize_buf->cqe))) {
  621. struct mthca_cq_buf tbuf;
  622. int tcqe;
  623. tbuf = cq->buf;
  624. tcqe = cq->ibcq.cqe;
  625. cq->buf = cq->resize_buf->buf;
  626. cq->ibcq.cqe = cq->resize_buf->cqe;
  627. cq->resize_buf->buf = tbuf;
  628. cq->resize_buf->cqe = tcqe;
  629. cq->resize_buf->state = CQ_RESIZE_SWAPPED;
  630. goto repoll;
  631. }
  632. }
  633. spin_unlock_irqrestore(&cq->lock, flags);
  634. return err == 0 || err == -EAGAIN ? npolled : err;
  635. }
  636. int mthca_tavor_arm_cq(struct ib_cq *cq, enum ib_cq_notify notify)
  637. {
  638. __be32 doorbell[2];
  639. doorbell[0] = cpu_to_be32((notify == IB_CQ_SOLICITED ?
  640. MTHCA_TAVOR_CQ_DB_REQ_NOT_SOL :
  641. MTHCA_TAVOR_CQ_DB_REQ_NOT) |
  642. to_mcq(cq)->cqn);
  643. doorbell[1] = (__force __be32) 0xffffffff;
  644. mthca_write64(doorbell,
  645. to_mdev(cq->device)->kar + MTHCA_CQ_DOORBELL,
  646. MTHCA_GET_DOORBELL_LOCK(&to_mdev(cq->device)->doorbell_lock));
  647. return 0;
  648. }
  649. int mthca_arbel_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify notify)
  650. {
  651. struct mthca_cq *cq = to_mcq(ibcq);
  652. __be32 doorbell[2];
  653. u32 sn;
  654. __be32 ci;
  655. sn = cq->arm_sn & 3;
  656. ci = cpu_to_be32(cq->cons_index);
  657. doorbell[0] = ci;
  658. doorbell[1] = cpu_to_be32((cq->cqn << 8) | (2 << 5) | (sn << 3) |
  659. (notify == IB_CQ_SOLICITED ? 1 : 2));
  660. mthca_write_db_rec(doorbell, cq->arm_db);
  661. /*
  662. * Make sure that the doorbell record in host memory is
  663. * written before ringing the doorbell via PCI MMIO.
  664. */
  665. wmb();
  666. doorbell[0] = cpu_to_be32((sn << 28) |
  667. (notify == IB_CQ_SOLICITED ?
  668. MTHCA_ARBEL_CQ_DB_REQ_NOT_SOL :
  669. MTHCA_ARBEL_CQ_DB_REQ_NOT) |
  670. cq->cqn);
  671. doorbell[1] = ci;
  672. mthca_write64(doorbell,
  673. to_mdev(ibcq->device)->kar + MTHCA_CQ_DOORBELL,
  674. MTHCA_GET_DOORBELL_LOCK(&to_mdev(ibcq->device)->doorbell_lock));
  675. return 0;
  676. }
  677. int mthca_init_cq(struct mthca_dev *dev, int nent,
  678. struct mthca_ucontext *ctx, u32 pdn,
  679. struct mthca_cq *cq)
  680. {
  681. struct mthca_mailbox *mailbox;
  682. struct mthca_cq_context *cq_context;
  683. int err = -ENOMEM;
  684. u8 status;
  685. cq->ibcq.cqe = nent - 1;
  686. cq->is_kernel = !ctx;
  687. cq->cqn = mthca_alloc(&dev->cq_table.alloc);
  688. if (cq->cqn == -1)
  689. return -ENOMEM;
  690. if (mthca_is_memfree(dev)) {
  691. err = mthca_table_get(dev, dev->cq_table.table, cq->cqn);
  692. if (err)
  693. goto err_out;
  694. if (cq->is_kernel) {
  695. cq->arm_sn = 1;
  696. err = -ENOMEM;
  697. cq->set_ci_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_SET_CI,
  698. cq->cqn, &cq->set_ci_db);
  699. if (cq->set_ci_db_index < 0)
  700. goto err_out_icm;
  701. cq->arm_db_index = mthca_alloc_db(dev, MTHCA_DB_TYPE_CQ_ARM,
  702. cq->cqn, &cq->arm_db);
  703. if (cq->arm_db_index < 0)
  704. goto err_out_ci;
  705. }
  706. }
  707. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  708. if (IS_ERR(mailbox))
  709. goto err_out_arm;
  710. cq_context = mailbox->buf;
  711. if (cq->is_kernel) {
  712. err = mthca_alloc_cq_buf(dev, &cq->buf, nent);
  713. if (err)
  714. goto err_out_mailbox;
  715. }
  716. spin_lock_init(&cq->lock);
  717. cq->refcount = 1;
  718. init_waitqueue_head(&cq->wait);
  719. mutex_init(&cq->mutex);
  720. memset(cq_context, 0, sizeof *cq_context);
  721. cq_context->flags = cpu_to_be32(MTHCA_CQ_STATUS_OK |
  722. MTHCA_CQ_STATE_DISARMED |
  723. MTHCA_CQ_FLAG_TR);
  724. cq_context->logsize_usrpage = cpu_to_be32((ffs(nent) - 1) << 24);
  725. if (ctx)
  726. cq_context->logsize_usrpage |= cpu_to_be32(ctx->uar.index);
  727. else
  728. cq_context->logsize_usrpage |= cpu_to_be32(dev->driver_uar.index);
  729. cq_context->error_eqn = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_ASYNC].eqn);
  730. cq_context->comp_eqn = cpu_to_be32(dev->eq_table.eq[MTHCA_EQ_COMP].eqn);
  731. cq_context->pd = cpu_to_be32(pdn);
  732. cq_context->lkey = cpu_to_be32(cq->buf.mr.ibmr.lkey);
  733. cq_context->cqn = cpu_to_be32(cq->cqn);
  734. if (mthca_is_memfree(dev)) {
  735. cq_context->ci_db = cpu_to_be32(cq->set_ci_db_index);
  736. cq_context->state_db = cpu_to_be32(cq->arm_db_index);
  737. }
  738. err = mthca_SW2HW_CQ(dev, mailbox, cq->cqn, &status);
  739. if (err) {
  740. mthca_warn(dev, "SW2HW_CQ failed (%d)\n", err);
  741. goto err_out_free_mr;
  742. }
  743. if (status) {
  744. mthca_warn(dev, "SW2HW_CQ returned status 0x%02x\n",
  745. status);
  746. err = -EINVAL;
  747. goto err_out_free_mr;
  748. }
  749. spin_lock_irq(&dev->cq_table.lock);
  750. if (mthca_array_set(&dev->cq_table.cq,
  751. cq->cqn & (dev->limits.num_cqs - 1),
  752. cq)) {
  753. spin_unlock_irq(&dev->cq_table.lock);
  754. goto err_out_free_mr;
  755. }
  756. spin_unlock_irq(&dev->cq_table.lock);
  757. cq->cons_index = 0;
  758. mthca_free_mailbox(dev, mailbox);
  759. return 0;
  760. err_out_free_mr:
  761. if (cq->is_kernel)
  762. mthca_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
  763. err_out_mailbox:
  764. mthca_free_mailbox(dev, mailbox);
  765. err_out_arm:
  766. if (cq->is_kernel && mthca_is_memfree(dev))
  767. mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
  768. err_out_ci:
  769. if (cq->is_kernel && mthca_is_memfree(dev))
  770. mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
  771. err_out_icm:
  772. mthca_table_put(dev, dev->cq_table.table, cq->cqn);
  773. err_out:
  774. mthca_free(&dev->cq_table.alloc, cq->cqn);
  775. return err;
  776. }
  777. static inline int get_cq_refcount(struct mthca_dev *dev, struct mthca_cq *cq)
  778. {
  779. int c;
  780. spin_lock_irq(&dev->cq_table.lock);
  781. c = cq->refcount;
  782. spin_unlock_irq(&dev->cq_table.lock);
  783. return c;
  784. }
  785. void mthca_free_cq(struct mthca_dev *dev,
  786. struct mthca_cq *cq)
  787. {
  788. struct mthca_mailbox *mailbox;
  789. int err;
  790. u8 status;
  791. mailbox = mthca_alloc_mailbox(dev, GFP_KERNEL);
  792. if (IS_ERR(mailbox)) {
  793. mthca_warn(dev, "No memory for mailbox to free CQ.\n");
  794. return;
  795. }
  796. err = mthca_HW2SW_CQ(dev, mailbox, cq->cqn, &status);
  797. if (err)
  798. mthca_warn(dev, "HW2SW_CQ failed (%d)\n", err);
  799. else if (status)
  800. mthca_warn(dev, "HW2SW_CQ returned status 0x%02x\n", status);
  801. if (0) {
  802. __be32 *ctx = mailbox->buf;
  803. int j;
  804. printk(KERN_ERR "context for CQN %x (cons index %x, next sw %d)\n",
  805. cq->cqn, cq->cons_index,
  806. cq->is_kernel ? !!next_cqe_sw(cq) : 0);
  807. for (j = 0; j < 16; ++j)
  808. printk(KERN_ERR "[%2x] %08x\n", j * 4, be32_to_cpu(ctx[j]));
  809. }
  810. spin_lock_irq(&dev->cq_table.lock);
  811. mthca_array_clear(&dev->cq_table.cq,
  812. cq->cqn & (dev->limits.num_cqs - 1));
  813. --cq->refcount;
  814. spin_unlock_irq(&dev->cq_table.lock);
  815. if (dev->mthca_flags & MTHCA_FLAG_MSI_X)
  816. synchronize_irq(dev->eq_table.eq[MTHCA_EQ_COMP].msi_x_vector);
  817. else
  818. synchronize_irq(dev->pdev->irq);
  819. wait_event(cq->wait, !get_cq_refcount(dev, cq));
  820. if (cq->is_kernel) {
  821. mthca_free_cq_buf(dev, &cq->buf, cq->ibcq.cqe);
  822. if (mthca_is_memfree(dev)) {
  823. mthca_free_db(dev, MTHCA_DB_TYPE_CQ_ARM, cq->arm_db_index);
  824. mthca_free_db(dev, MTHCA_DB_TYPE_CQ_SET_CI, cq->set_ci_db_index);
  825. }
  826. }
  827. mthca_table_put(dev, dev->cq_table.table, cq->cqn);
  828. mthca_free(&dev->cq_table.alloc, cq->cqn);
  829. mthca_free_mailbox(dev, mailbox);
  830. }
  831. int mthca_init_cq_table(struct mthca_dev *dev)
  832. {
  833. int err;
  834. spin_lock_init(&dev->cq_table.lock);
  835. err = mthca_alloc_init(&dev->cq_table.alloc,
  836. dev->limits.num_cqs,
  837. (1 << 24) - 1,
  838. dev->limits.reserved_cqs);
  839. if (err)
  840. return err;
  841. err = mthca_array_init(&dev->cq_table.cq,
  842. dev->limits.num_cqs);
  843. if (err)
  844. mthca_alloc_cleanup(&dev->cq_table.alloc);
  845. return err;
  846. }
  847. void mthca_cleanup_cq_table(struct mthca_dev *dev)
  848. {
  849. mthca_array_cleanup(&dev->cq_table.cq, dev->limits.num_cqs);
  850. mthca_alloc_cleanup(&dev->cq_table.alloc);
  851. }