mthca_cq.c 23 KB

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