]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/ofed/libmlx4/src/cq.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / ofed / libmlx4 / src / cq.c
1 /*
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.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34
35 #if HAVE_CONFIG_H
36 #  include <config.h>
37 #endif /* HAVE_CONFIG_H */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <pthread.h>
42 #include <netinet/in.h>
43 #include <string.h>
44
45 #include <infiniband/opcode.h>
46
47 #include "mlx4.h"
48 #include "doorbell.h"
49
50 enum {
51         MLX4_CQ_DOORBELL                        = 0x20
52 };
53
54 enum {
55         CQ_OK                                   =  0,
56         CQ_EMPTY                                = -1,
57         CQ_POLL_ERR                             = -2
58 };
59
60 #define MLX4_CQ_DB_REQ_NOT_SOL                  (1 << 24)
61 #define MLX4_CQ_DB_REQ_NOT                      (2 << 24)
62
63 enum {
64         MLX4_CQE_OWNER_MASK                     = 0x80,
65         MLX4_CQE_IS_SEND_MASK                   = 0x40,
66         MLX4_CQE_OPCODE_MASK                    = 0x1f
67 };
68
69 enum {
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,
83 };
84
85 struct mlx4_cqe {
86         uint32_t        my_qpn;
87         uint32_t        immed_rss_invalid;
88         uint32_t        g_mlpath_rqpn;
89         uint8_t         sl;
90         uint8_t         reserved1;
91         uint16_t        rlid;
92         uint32_t        reserved2;
93         uint32_t        byte_cnt;
94         uint16_t        wqe_index;
95         uint16_t        checksum;
96         uint8_t         reserved3[3];
97         uint8_t         owner_sr_opcode;
98 };
99
100 struct mlx4_err_cqe {
101         uint32_t        my_qpn;
102         uint32_t        reserved1[5];
103         uint16_t        wqe_index;
104         uint8_t         vendor_err;
105         uint8_t         syndrome;
106         uint8_t         reserved2[3];
107         uint8_t         owner_sr_opcode;
108 };
109
110 static struct mlx4_cqe *get_cqe(struct mlx4_cq *cq, int entry)
111 {
112         return cq->buf.buf + entry * MLX4_CQ_ENTRY_SIZE;
113 }
114
115 static void *get_sw_cqe(struct mlx4_cq *cq, int n)
116 {
117         struct mlx4_cqe *cqe = get_cqe(cq, n & cq->ibv_cq.cqe);
118
119         return (!!(cqe->owner_sr_opcode & MLX4_CQE_OWNER_MASK) ^
120                 !!(n & (cq->ibv_cq.cqe + 1))) ? NULL : cqe;
121 }
122
123 static struct mlx4_cqe *next_cqe_sw(struct mlx4_cq *cq)
124 {
125         return get_sw_cqe(cq, cq->cons_index);
126 }
127
128 static void update_cons_index(struct mlx4_cq *cq)
129 {
130         *cq->set_ci_db = htonl(cq->cons_index & 0xffffff);
131 }
132
133 static void mlx4_handle_error_cqe(struct mlx4_err_cqe *cqe, struct ibv_wc *wc)
134 {
135         if (cqe->syndrome == MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR)
136                 printf(PFX "local QP operation err "
137                        "(QPN %06x, WQE index %x, vendor syndrome %02x, "
138                        "opcode = %02x)\n",
139                        htonl(cqe->my_qpn), htonl(cqe->wqe_index),
140                        cqe->vendor_err,
141                        cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
142
143         switch (cqe->syndrome) {
144         case MLX4_CQE_SYNDROME_LOCAL_LENGTH_ERR:
145                 wc->status = IBV_WC_LOC_LEN_ERR;
146                 break;
147         case MLX4_CQE_SYNDROME_LOCAL_QP_OP_ERR:
148                 wc->status = IBV_WC_LOC_QP_OP_ERR;
149                 break;
150         case MLX4_CQE_SYNDROME_LOCAL_PROT_ERR:
151                 wc->status = IBV_WC_LOC_PROT_ERR;
152                 break;
153         case MLX4_CQE_SYNDROME_WR_FLUSH_ERR:
154                 wc->status = IBV_WC_WR_FLUSH_ERR;
155                 break;
156         case MLX4_CQE_SYNDROME_MW_BIND_ERR:
157                 wc->status = IBV_WC_MW_BIND_ERR;
158                 break;
159         case MLX4_CQE_SYNDROME_BAD_RESP_ERR:
160                 wc->status = IBV_WC_BAD_RESP_ERR;
161                 break;
162         case MLX4_CQE_SYNDROME_LOCAL_ACCESS_ERR:
163                 wc->status = IBV_WC_LOC_ACCESS_ERR;
164                 break;
165         case MLX4_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
166                 wc->status = IBV_WC_REM_INV_REQ_ERR;
167                 break;
168         case MLX4_CQE_SYNDROME_REMOTE_ACCESS_ERR:
169                 wc->status = IBV_WC_REM_ACCESS_ERR;
170                 break;
171         case MLX4_CQE_SYNDROME_REMOTE_OP_ERR:
172                 wc->status = IBV_WC_REM_OP_ERR;
173                 break;
174         case MLX4_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
175                 wc->status = IBV_WC_RETRY_EXC_ERR;
176                 break;
177         case MLX4_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
178                 wc->status = IBV_WC_RNR_RETRY_EXC_ERR;
179                 break;
180         case MLX4_CQE_SYNDROME_REMOTE_ABORTED_ERR:
181                 wc->status = IBV_WC_REM_ABORT_ERR;
182                 break;
183         default:
184                 wc->status = IBV_WC_GENERAL_ERR;
185                 break;
186         }
187
188         wc->vendor_err = cqe->vendor_err;
189 }
190
191 static int mlx4_poll_one(struct mlx4_cq *cq,
192                          struct mlx4_qp **cur_qp,
193                          struct ibv_wc *wc)
194 {
195         struct mlx4_wq *wq;
196         struct mlx4_cqe *cqe;
197         struct mlx4_srq *srq = NULL;
198         uint32_t qpn;
199         uint32_t srqn;
200         uint32_t g_mlpath_rqpn;
201         uint16_t wqe_index;
202         int is_error;
203         int is_send;
204
205         cqe = next_cqe_sw(cq);
206         if (!cqe)
207                 return CQ_EMPTY;
208
209         ++cq->cons_index;
210
211         VALGRIND_MAKE_MEM_DEFINED(cqe, sizeof *cqe);
212
213         /*
214          * Make sure we read CQ entry contents after we've checked the
215          * ownership bit.
216          */
217         rmb();
218
219         qpn = ntohl(cqe->my_qpn);
220
221         is_send  = cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK;
222         is_error = (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) ==
223                 MLX4_CQE_OPCODE_ERROR;
224
225         if (qpn & MLX4_XRC_QPN_BIT && !is_send) {
226                 srqn = ntohl(cqe->g_mlpath_rqpn) & 0xffffff;
227                 /*
228                  * We do not have to take the XRC SRQ table lock here,
229                  * because CQs will be locked while XRC SRQs are removed
230                  * from the table.
231                  */
232                 srq = mlx4_find_xrc_srq(to_mctx(cq->ibv_cq.context), srqn);
233                 if (!srq)
234                         return CQ_POLL_ERR;
235         } else if (!*cur_qp || (qpn & 0xffffff) != (*cur_qp)->ibv_qp.qp_num) {
236                 /*
237                  * We do not have to take the QP table lock here,
238                  * because CQs will be locked while QPs are removed
239                  * from the table.
240                  */
241                 *cur_qp = mlx4_find_qp(to_mctx(cq->ibv_cq.context),
242                                        qpn & 0xffffff);
243                 if (!*cur_qp)
244                         return CQ_POLL_ERR;
245         }
246
247         wc->qp_num = qpn & 0xffffff;
248
249         if (is_send) {
250                 wq = &(*cur_qp)->sq;
251                 wqe_index = ntohs(cqe->wqe_index);
252                 wq->tail += (uint16_t) (wqe_index - (uint16_t) wq->tail);
253                 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
254                 ++wq->tail;
255         } else if (srq) {
256                 wqe_index = htons(cqe->wqe_index);
257                 wc->wr_id = srq->wrid[wqe_index];
258                 mlx4_free_srq_wqe(srq, wqe_index);
259         } else if ((*cur_qp)->ibv_qp.srq) {
260                 srq = to_msrq((*cur_qp)->ibv_qp.srq);
261                 wqe_index = htons(cqe->wqe_index);
262                 wc->wr_id = srq->wrid[wqe_index];
263                 mlx4_free_srq_wqe(srq, wqe_index);
264         } else {
265                 wq = &(*cur_qp)->rq;
266                 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
267                 ++wq->tail;
268         }
269
270         if (is_error) {
271                 mlx4_handle_error_cqe((struct mlx4_err_cqe *) cqe, wc);
272                 return CQ_OK;
273         }
274
275         wc->status = IBV_WC_SUCCESS;
276
277         if (is_send) {
278                 wc->wc_flags = 0;
279                 switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
280                 case MLX4_OPCODE_RDMA_WRITE_IMM:
281                         wc->wc_flags |= IBV_WC_WITH_IMM;
282                 case MLX4_OPCODE_RDMA_WRITE:
283                         wc->opcode    = IBV_WC_RDMA_WRITE;
284                         break;
285                 case MLX4_OPCODE_SEND_IMM:
286                         wc->wc_flags |= IBV_WC_WITH_IMM;
287                 case MLX4_OPCODE_SEND:
288                         wc->opcode    = IBV_WC_SEND;
289                         break;
290                 case MLX4_OPCODE_RDMA_READ:
291                         wc->opcode    = IBV_WC_RDMA_READ;
292                         wc->byte_len  = ntohl(cqe->byte_cnt);
293                         break;
294                 case MLX4_OPCODE_ATOMIC_CS:
295                         wc->opcode    = IBV_WC_COMP_SWAP;
296                         wc->byte_len  = 8;
297                         break;
298                 case MLX4_OPCODE_ATOMIC_FA:
299                         wc->opcode    = IBV_WC_FETCH_ADD;
300                         wc->byte_len  = 8;
301                         break;
302                 case MLX4_OPCODE_BIND_MW:
303                         wc->opcode    = IBV_WC_BIND_MW;
304                         break;
305                 default:
306                         /* assume it's a send completion */
307                         wc->opcode    = IBV_WC_SEND;
308                         break;
309                 }
310         } else {
311                 wc->byte_len = ntohl(cqe->byte_cnt);
312
313                 switch (cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) {
314                 case MLX4_RECV_OPCODE_RDMA_WRITE_IMM:
315                         wc->opcode   = IBV_WC_RECV_RDMA_WITH_IMM;
316                         wc->wc_flags = IBV_WC_WITH_IMM;
317                         wc->imm_data = cqe->immed_rss_invalid;
318                         break;
319                 case MLX4_RECV_OPCODE_SEND:
320                         wc->opcode   = IBV_WC_RECV;
321                         wc->wc_flags = 0;
322                         break;
323                 case MLX4_RECV_OPCODE_SEND_IMM:
324                         wc->opcode   = IBV_WC_RECV;
325                         wc->wc_flags = IBV_WC_WITH_IMM;
326                         wc->imm_data = cqe->immed_rss_invalid;
327                         break;
328                 }
329
330                 wc->slid           = ntohs(cqe->rlid);
331                 wc->sl             = cqe->sl >> 4;
332                 g_mlpath_rqpn      = ntohl(cqe->g_mlpath_rqpn);
333                 wc->src_qp         = g_mlpath_rqpn & 0xffffff;
334                 wc->dlid_path_bits = (g_mlpath_rqpn >> 24) & 0x7f;
335                 wc->wc_flags      |= g_mlpath_rqpn & 0x80000000 ? IBV_WC_GRH : 0;
336                 wc->pkey_index     = ntohl(cqe->immed_rss_invalid) & 0x7f;
337         }
338
339         return CQ_OK;
340 }
341
342 int mlx4_poll_cq(struct ibv_cq *ibcq, int ne, struct ibv_wc *wc)
343 {
344         struct mlx4_cq *cq = to_mcq(ibcq);
345         struct mlx4_qp *qp = NULL;
346         int npolled;
347         int err = CQ_OK;
348
349         pthread_spin_lock(&cq->lock);
350
351         for (npolled = 0; npolled < ne; ++npolled) {
352                 err = mlx4_poll_one(cq, &qp, wc + npolled);
353                 if (err != CQ_OK)
354                         break;
355         }
356
357         if (npolled)
358                 update_cons_index(cq);
359
360         pthread_spin_unlock(&cq->lock);
361
362         return err == CQ_POLL_ERR ? err : npolled;
363 }
364
365 int mlx4_arm_cq(struct ibv_cq *ibvcq, int solicited)
366 {
367         struct mlx4_cq *cq = to_mcq(ibvcq);
368         uint32_t doorbell[2];
369         uint32_t sn;
370         uint32_t ci;
371         uint32_t cmd;
372
373         sn  = cq->arm_sn & 3;
374         ci  = cq->cons_index & 0xffffff;
375         cmd = solicited ? MLX4_CQ_DB_REQ_NOT_SOL : MLX4_CQ_DB_REQ_NOT;
376
377         *cq->arm_db = htonl(sn << 28 | cmd | ci);
378
379         /*
380          * Make sure that the doorbell record in host memory is
381          * written before ringing the doorbell via PCI MMIO.
382          */
383         wmb();
384
385         doorbell[0] = htonl(sn << 28 | cmd | cq->cqn);
386         doorbell[1] = htonl(ci);
387
388         mlx4_write64(doorbell, to_mctx(ibvcq->context), MLX4_CQ_DOORBELL);
389
390         return 0;
391 }
392
393 void mlx4_cq_event(struct ibv_cq *cq)
394 {
395         to_mcq(cq)->arm_sn++;
396 }
397
398 void __mlx4_cq_clean(struct mlx4_cq *cq, uint32_t qpn, struct mlx4_srq *srq)
399 {
400         struct mlx4_cqe *cqe, *dest;
401         uint32_t prod_index;
402         uint8_t owner_bit;
403         int nfreed = 0;
404         int is_xrc_srq = 0;
405
406         if (srq && srq->ibv_srq.xrc_cq)
407                 is_xrc_srq = 1;
408
409         /*
410          * First we need to find the current producer index, so we
411          * know where to start cleaning from.  It doesn't matter if HW
412          * adds new entries after this loop -- the QP we're worried
413          * about is already in RESET, so the new entries won't come
414          * from our QP and therefore don't need to be checked.
415          */
416         for (prod_index = cq->cons_index; get_sw_cqe(cq, prod_index); ++prod_index)
417                 if (prod_index == cq->cons_index + cq->ibv_cq.cqe)
418                         break;
419
420         /*
421          * Now sweep backwards through the CQ, removing CQ entries
422          * that match our QP by copying older entries on top of them.
423          */
424         while ((int) --prod_index - (int) cq->cons_index >= 0) {
425                 cqe = get_cqe(cq, prod_index & cq->ibv_cq.cqe);
426                 if (is_xrc_srq &&
427                     (ntohl(cqe->g_mlpath_rqpn & 0xffffff) == srq->srqn) &&
428                     !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK)) {
429                         mlx4_free_srq_wqe(srq, ntohs(cqe->wqe_index));
430                         ++nfreed;
431                 } else if ((ntohl(cqe->my_qpn) & 0xffffff) == qpn) {
432                         if (srq && !(cqe->owner_sr_opcode & MLX4_CQE_IS_SEND_MASK))
433                                 mlx4_free_srq_wqe(srq, ntohs(cqe->wqe_index));
434                         ++nfreed;
435                 } else if (nfreed) {
436                         dest = get_cqe(cq, (prod_index + nfreed) & cq->ibv_cq.cqe);
437                         owner_bit = dest->owner_sr_opcode & MLX4_CQE_OWNER_MASK;
438                         memcpy(dest, cqe, sizeof *cqe);
439                         dest->owner_sr_opcode = owner_bit |
440                                 (dest->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK);
441                 }
442         }
443
444         if (nfreed) {
445                 cq->cons_index += nfreed;
446                 /*
447                  * Make sure update of buffer contents is done before
448                  * updating consumer index.
449                  */
450                 wmb();
451                 update_cons_index(cq);
452         }
453 }
454
455 void mlx4_cq_clean(struct mlx4_cq *cq, uint32_t qpn, struct mlx4_srq *srq)
456 {
457         pthread_spin_lock(&cq->lock);
458         __mlx4_cq_clean(cq, qpn, srq);
459         pthread_spin_unlock(&cq->lock);
460 }
461
462 int mlx4_get_outstanding_cqes(struct mlx4_cq *cq)
463 {
464         uint32_t i;
465
466         for (i = cq->cons_index; get_sw_cqe(cq, (i & cq->ibv_cq.cqe)); ++i)
467                 ;
468
469         return i - cq->cons_index;
470 }
471
472 void mlx4_cq_resize_copy_cqes(struct mlx4_cq *cq, void *buf, int old_cqe)
473 {
474         struct mlx4_cqe *cqe;
475         int i;
476
477         i = cq->cons_index;
478         cqe = get_cqe(cq, (i & old_cqe));
479
480         while ((cqe->owner_sr_opcode & MLX4_CQE_OPCODE_MASK) != MLX4_CQE_OPCODE_RESIZE) {
481                 cqe->owner_sr_opcode = (cqe->owner_sr_opcode & ~MLX4_CQE_OWNER_MASK) |
482                         (((i + 1) & (cq->ibv_cq.cqe + 1)) ? MLX4_CQE_OWNER_MASK : 0);
483                 memcpy(buf + ((i + 1) & cq->ibv_cq.cqe) * MLX4_CQ_ENTRY_SIZE,
484                        cqe, MLX4_CQ_ENTRY_SIZE);
485                 ++i;
486                 cqe = get_cqe(cq, (i & old_cqe));
487         }
488
489         ++cq->cons_index;
490 }
491
492 int mlx4_alloc_cq_buf(struct mlx4_device *dev, struct mlx4_buf *buf, int nent)
493 {
494         if (mlx4_alloc_buf(buf, align(nent * MLX4_CQ_ENTRY_SIZE, dev->page_size),
495                            dev->page_size))
496                 return -1;
497         memset(buf->buf, 0, nent * MLX4_CQ_ENTRY_SIZE);
498
499         return 0;
500 }