mthca_cq.c 24 KB

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