mthca_cq.c 26 KB

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