2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2006, 2007 Cisco Systems. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #endif /* HAVE_CONFIG_H */
42 #include <netinet/in.h>
45 #include <infiniband/opcode.h>
51 MLX4_CQ_DOORBELL = 0x20
60 #define MLX4_CQ_DB_REQ_NOT_SOL (1 << 24)
61 #define MLX4_CQ_DB_REQ_NOT (2 << 24)
64 MLX4_CQE_OWNER_MASK = 0x80,
65 MLX4_CQE_IS_SEND_MASK = 0x40,
66 MLX4_CQE_OPCODE_MASK = 0x1f
70 MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR = 0x01,
71 MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR = 0x02,
72 MLX4_CQE_SYNDROME_LOCAL_PROT_ERR = 0x04,
73 MLX4_CQE_SYNDROME_WR_FLUSH_ERR = 0x05,
74 MLX4_CQE_SYNDROME_MW_BIND_ERR = 0x06,
75 MLX4_CQE_SYNDROME_BAD_RESP_ERR = 0x10,
76 MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR = 0x11,
77 MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR = 0x12,
78 MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR = 0x13,
79 MLX4_CQE_SYNDROME_REMOTE_OP_ERR = 0x14,
80 MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR = 0x15,
81 MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR = 0x16,
82 MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR = 0x22,
87 uint32_t immed_rss_invalid;
88 uint32_t g_mlpath_rqpn;
97 uint8_t owner_sr_opcode;
100 struct mlx4_err_cqe {
102 uint32_t reserved1[5];
106 uint8_t reserved2[3];
107 uint8_t owner_sr_opcode;
110 static struct mlx4_cqe *get_cqe(struct mlx4_cq *cq, int entry)
112 return cq->buf.buf + entry * cq->cqe_size;
115 static void *get_sw_cqe(struct mlx4_cq *cq, int n)
117 struct mlx4_cqe *cqe = get_cqe(cq, n & cq->ibv_cq.cqe);
118 struct mlx4_cqe *tcqe = cq->cqe_size == 64 ? cqe + 1 : cqe;
120 return (!!(tcqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
121 !!(n & (cq->ibv_cq.cqe + 1))) ? NULL : tcqe;
124 static struct mlx4_cqe *next_cqe_sw(struct mlx4_cq *cq)
126 return get_sw_cqe(cq, cq->cons_index);
129 static void update_cons_index(struct mlx4_cq *cq)
131 *cq->set_ci_db = htonl(cq->cons_index & 0xffffff);
134 static void mlx4_handle_error_cqe(struct mlx4_err_cqe *cqe, struct ibv_wc *wc)
136 if (cqe->syndrome == MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR)
137 printf(PFX "local QP operation err "
138 "(QPN %06x, WQE index %x, vendor syndrome %02x, "
140 htonl(cqe->my_qpn), htonl(cqe->wqe_index),
142 cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
144 switch (cqe->syndrome) {
145 case MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR:
146 wc->status = IBV_WC_LOC_LEN_ERR;
148 case MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR:
149 wc->status = IBV_WC_LOC_QP_OP_ERR;
151 case MLX4_CQE_SYNDROME_LOCAL_PROT_ERR:
152 wc->status = IBV_WC_LOC_PROT_ERR;
154 case MLX4_CQE_SYNDROME_WR_FLUSH_ERR:
155 wc->status = IBV_WC_WR_FLUSH_ERR;
157 case MLX4_CQE_SYNDROME_MW_BIND_ERR:
158 wc->status = IBV_WC_MW_BIND_ERR;
160 case MLX4_CQE_SYNDROME_BAD_RESP_ERR:
161 wc->status = IBV_WC_BAD_RESP_ERR;
163 case MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR:
164 wc->status = IBV_WC_LOC_ACCESS_ERR;
166 case MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
167 wc->status = IBV_WC_REM_INV_REQ_ERR;
169 case MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR:
170 wc->status = IBV_WC_REM_ACCESS_ERR;
172 case MLX4_CQE_SYNDROME_REMOTE_OP_ERR:
173 wc->status = IBV_WC_REM_OP_ERR;
175 case MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
176 wc->status = IBV_WC_RETRY_EXC_ERR;
178 case MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
179 wc->status = IBV_WC_RNR_RETRY_EXC_ERR;
181 case MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR:
182 wc->status = IBV_WC_REM_ABORT_ERR;
185 wc->status = IBV_WC_GENERAL_ERR;
189 wc->vendor_err = cqe->vendor_err;
192 static int mlx4_poll_one(struct mlx4_cq *cq,
193 struct mlx4_qp **cur_qp,
197 struct mlx4_cqe *cqe;
198 struct mlx4_srq *srq = NULL;
201 uint32_t g_mlpath_rqpn;
206 cqe = next_cqe_sw(cq);
212 VALGRIND_MAKE_MEM_DEFINED(cqe, sizeof *cqe);
215 * Make sure we read CQ entry contents after we've checked the
220 qpn = ntohl(cqe->my_qpn);
222 is_send = cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK;
223 is_error = (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
224 MLX4_CQE_OPCODE_ERROR;
226 if (qpn & MLX4_XRC_QPN_BIT && !is_send) {
227 srqn = ntohl(cqe->g_mlpath_rqpn) & 0xffffff;
229 * We do not have to take the XRC SRQ table lock here,
230 * because CQs will be locked while XRC SRQs are removed
233 srq = mlx4_find_xrc_srq(to_mctx(cq->ibv_cq.context), srqn);
236 } else if (!*cur_qp || (qpn & 0xffffff) != (*cur_qp)->ibv_qp.qp_num) {
238 * We do not have to take the QP table lock here,
239 * because CQs will be locked while QPs are removed
242 *cur_qp = mlx4_find_qp(to_mctx(cq->ibv_cq.context),
248 wc->qp_num = qpn & 0xffffff;
252 wqe_index = ntohs(cqe->wqe_index);
253 wq->tail += (uint16_t) (wqe_index - (uint16_t) wq->tail);
254 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
257 wqe_index = htons(cqe->wqe_index);
258 wc->wr_id = srq->wrid[wqe_index];
259 mlx4_free_srq_wqe(srq, wqe_index);
260 } else if ((*cur_qp)->ibv_qp.srq) {
261 srq = to_msrq((*cur_qp)->ibv_qp.srq);
262 wqe_index = htons(cqe->wqe_index);
263 wc->wr_id = srq->wrid[wqe_index];
264 mlx4_free_srq_wqe(srq, wqe_index);
267 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
272 mlx4_handle_error_cqe((struct mlx4_err_cqe *) cqe, wc);
276 wc->status = IBV_WC_SUCCESS;
280 switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
281 case MLX4_OPCODE_RDMA_WRITE_IMM:
282 wc->wc_flags |= IBV_WC_WITH_IMM;
283 case MLX4_OPCODE_RDMA_WRITE:
284 wc->opcode = IBV_WC_RDMA_WRITE;
286 case MLX4_OPCODE_SEND_IMM:
287 wc->wc_flags |= IBV_WC_WITH_IMM;
288 case MLX4_OPCODE_SEND:
289 wc->opcode = IBV_WC_SEND;
291 case MLX4_OPCODE_RDMA_READ:
292 wc->opcode = IBV_WC_RDMA_READ;
293 wc->byte_len = ntohl(cqe->byte_cnt);
295 case MLX4_OPCODE_ATOMIC_CS:
296 wc->opcode = IBV_WC_COMP_SWAP;
299 case MLX4_OPCODE_ATOMIC_FA:
300 wc->opcode = IBV_WC_FETCH_ADD;
303 case MLX4_OPCODE_BIND_MW:
304 wc->opcode = IBV_WC_BIND_MW;
307 /* assume it's a send completion */
308 wc->opcode = IBV_WC_SEND;
312 wc->byte_len = ntohl(cqe->byte_cnt);
314 switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
315 case MLX4_RECV_OPCODE_RDMA_WRITE_IMM:
316 wc->opcode = IBV_WC_RECV_RDMA_WITH_IMM;
317 wc->wc_flags = IBV_WC_WITH_IMM;
318 wc->imm_data = cqe->immed_rss_invalid;
320 case MLX4_RECV_OPCODE_SEND:
321 wc->opcode = IBV_WC_RECV;
324 case MLX4_RECV_OPCODE_SEND_IMM:
325 wc->opcode = IBV_WC_RECV;
326 wc->wc_flags = IBV_WC_WITH_IMM;
327 wc->imm_data = cqe->immed_rss_invalid;
331 wc->slid = ntohs(cqe->rlid);
332 wc->sl = cqe->sl >> 4;
333 g_mlpath_rqpn = ntohl(cqe->g_mlpath_rqpn);
334 wc->src_qp = g_mlpath_rqpn & 0xffffff;
335 wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
336 wc->wc_flags |= g_mlpath_rqpn & 0x80000000 ? IBV_WC_GRH : 0;
337 wc->pkey_index = ntohl(cqe->immed_rss_invalid) & 0x7f;
343 int mlx4_poll_cq(struct ibv_cq *ibcq, int ne, struct ibv_wc *wc)
345 struct mlx4_cq *cq = to_mcq(ibcq);
346 struct mlx4_qp *qp = NULL;
350 pthread_spin_lock(&cq->lock);
352 for (npolled = 0; npolled < ne; ++npolled) {
353 err = mlx4_poll_one(cq, &qp, wc + npolled);
359 update_cons_index(cq);
361 pthread_spin_unlock(&cq->lock);
363 return err == CQ_POLL_ERR ? err : npolled;
366 int mlx4_arm_cq(struct ibv_cq *ibvcq, int solicited)
368 struct mlx4_cq *cq = to_mcq(ibvcq);
369 uint32_t doorbell[2];
375 ci = cq->cons_index & 0xffffff;
376 cmd = solicited ? MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT;
378 *cq->arm_db = htonl(sn << 28 | cmd | ci);
381 * Make sure that the doorbell record in host memory is
382 * written before ringing the doorbell via PCI MMIO.
386 doorbell[0] = htonl(sn << 28 | cmd | cq->cqn);
387 doorbell[1] = htonl(ci);
389 mlx4_write64(doorbell, to_mctx(ibvcq->context), MLX4_CQ_DOORBELL);
394 void mlx4_cq_event(struct ibv_cq *cq)
396 to_mcq(cq)->arm_sn++;
399 void __mlx4_cq_clean(struct mlx4_cq *cq, uint32_t qpn, struct mlx4_srq *srq)
401 struct mlx4_cqe *cqe, *dest;
406 int cqe_inc = cq->cqe_size == 64 ? 1 : 0;
408 if (srq && srq->ibv_srq.xrc_cq)
412 * First we need to find the current producer index, so we
413 * know where to start cleaning from. It doesn't matter if HW
414 * adds new entries after this loop -- the QP we're worried
415 * about is already in RESET, so the new entries won't come
416 * from our QP and therefore don't need to be checked.
418 for (prod_index = cq->cons_index; get_sw_cqe(cq, prod_index); ++prod_index)
419 if (prod_index == cq->cons_index + cq->ibv_cq.cqe)
423 * Now sweep backwards through the CQ, removing CQ entries
424 * that match our QP by copying older entries on top of them.
426 while ((int) --prod_index - (int) cq->cons_index >= 0) {
427 cqe = get_cqe(cq, prod_index & cq->ibv_cq.cqe);
430 (ntohl(cqe->g_mlpath_rqpn & 0xffffff) == srq->srqn) &&
431 !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK)) {
432 mlx4_free_srq_wqe(srq, ntohs(cqe->wqe_index));
434 } else if ((ntohl(cqe->my_qpn) & 0xffffff) == qpn) {
435 if (srq && !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK))
436 mlx4_free_srq_wqe(srq, ntohs(cqe->wqe_index));
439 dest = get_cqe(cq, (prod_index + nfreed) & cq->ibv_cq.cqe);
441 owner_bit = dest->owner_sr_opcode & MLX4_CQE_OWNER_MASK;
442 memcpy(dest, cqe, sizeof *cqe);
443 dest->owner_sr_opcode = owner_bit |
444 (dest->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
449 cq->cons_index += nfreed;
451 * Make sure update of buffer contents is done before
452 * updating consumer index.
455 update_cons_index(cq);
459 void mlx4_cq_clean(struct mlx4_cq *cq, uint32_t qpn, struct mlx4_srq *srq)
461 pthread_spin_lock(&cq->lock);
462 __mlx4_cq_clean(cq, qpn, srq);
463 pthread_spin_unlock(&cq->lock);
466 int mlx4_get_outstanding_cqes(struct mlx4_cq *cq)
470 for (i = cq->cons_index; get_sw_cqe(cq, (i & cq->ibv_cq.cqe)); ++i)
473 return i - cq->cons_index;
476 void mlx4_cq_resize_copy_cqes(struct mlx4_cq *cq, void *buf, int old_cqe)
478 struct mlx4_cqe *cqe;
480 int cqe_inc = cq->cqe_size == 64 ? 1 : 0;
483 cqe = get_cqe(cq, (i & old_cqe));
486 while ((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) != MLX4_CQE_OPCODE_RESIZE) {
487 cqe->owner_sr_opcode = (cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK) |
488 (((i + 1) & (cq->ibv_cq.cqe + 1)) ? MLX4_CQE_OWNER_MASK : 0);
489 memcpy(buf + ((i + 1) & cq->ibv_cq.cqe) * cq->cqe_size,
490 cqe - cqe_inc, cq->cqe_size);
492 cqe = get_cqe(cq, (i & old_cqe));
499 int mlx4_alloc_cq_buf(struct mlx4_device *dev, struct mlx4_buf *buf, int nent,
502 if (mlx4_alloc_buf(buf, align(nent * entry_size, dev->page_size),
505 memset(buf->buf, 0, nent * entry_size);