mthca_cq.c 24 KB

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