mthca_cq.c 26 KB

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