mthca_cq.c 25 KB

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