net: dsa: bcm_sf2: Get VLAN_PORT_MASK from b53_device
[cascardo/linux.git] / drivers / infiniband / hw / mlx5 / cq.c
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies. 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
33 #include <linux/kref.h>
34 #include <rdma/ib_umem.h>
35 #include <rdma/ib_user_verbs.h>
36 #include <rdma/ib_cache.h>
37 #include "mlx5_ib.h"
38 #include "user.h"
39
40 static void mlx5_ib_cq_comp(struct mlx5_core_cq *cq)
41 {
42         struct ib_cq *ibcq = &to_mibcq(cq)->ibcq;
43
44         ibcq->comp_handler(ibcq, ibcq->cq_context);
45 }
46
47 static void mlx5_ib_cq_event(struct mlx5_core_cq *mcq, enum mlx5_event type)
48 {
49         struct mlx5_ib_cq *cq = container_of(mcq, struct mlx5_ib_cq, mcq);
50         struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
51         struct ib_cq *ibcq = &cq->ibcq;
52         struct ib_event event;
53
54         if (type != MLX5_EVENT_TYPE_CQ_ERROR) {
55                 mlx5_ib_warn(dev, "Unexpected event type %d on CQ %06x\n",
56                              type, mcq->cqn);
57                 return;
58         }
59
60         if (ibcq->event_handler) {
61                 event.device     = &dev->ib_dev;
62                 event.event      = IB_EVENT_CQ_ERR;
63                 event.element.cq = ibcq;
64                 ibcq->event_handler(&event, ibcq->cq_context);
65         }
66 }
67
68 static void *get_cqe_from_buf(struct mlx5_ib_cq_buf *buf, int n, int size)
69 {
70         return mlx5_buf_offset(&buf->buf, n * size);
71 }
72
73 static void *get_cqe(struct mlx5_ib_cq *cq, int n)
74 {
75         return get_cqe_from_buf(&cq->buf, n, cq->mcq.cqe_sz);
76 }
77
78 static u8 sw_ownership_bit(int n, int nent)
79 {
80         return (n & nent) ? 1 : 0;
81 }
82
83 static void *get_sw_cqe(struct mlx5_ib_cq *cq, int n)
84 {
85         void *cqe = get_cqe(cq, n & cq->ibcq.cqe);
86         struct mlx5_cqe64 *cqe64;
87
88         cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
89
90         if (likely((cqe64->op_own) >> 4 != MLX5_CQE_INVALID) &&
91             !((cqe64->op_own & MLX5_CQE_OWNER_MASK) ^ !!(n & (cq->ibcq.cqe + 1)))) {
92                 return cqe;
93         } else {
94                 return NULL;
95         }
96 }
97
98 static void *next_cqe_sw(struct mlx5_ib_cq *cq)
99 {
100         return get_sw_cqe(cq, cq->mcq.cons_index);
101 }
102
103 static enum ib_wc_opcode get_umr_comp(struct mlx5_ib_wq *wq, int idx)
104 {
105         switch (wq->wr_data[idx]) {
106         case MLX5_IB_WR_UMR:
107                 return 0;
108
109         case IB_WR_LOCAL_INV:
110                 return IB_WC_LOCAL_INV;
111
112         case IB_WR_REG_MR:
113                 return IB_WC_REG_MR;
114
115         default:
116                 pr_warn("unknown completion status\n");
117                 return 0;
118         }
119 }
120
121 static void handle_good_req(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
122                             struct mlx5_ib_wq *wq, int idx)
123 {
124         wc->wc_flags = 0;
125         switch (be32_to_cpu(cqe->sop_drop_qpn) >> 24) {
126         case MLX5_OPCODE_RDMA_WRITE_IMM:
127                 wc->wc_flags |= IB_WC_WITH_IMM;
128         case MLX5_OPCODE_RDMA_WRITE:
129                 wc->opcode    = IB_WC_RDMA_WRITE;
130                 break;
131         case MLX5_OPCODE_SEND_IMM:
132                 wc->wc_flags |= IB_WC_WITH_IMM;
133         case MLX5_OPCODE_SEND:
134         case MLX5_OPCODE_SEND_INVAL:
135                 wc->opcode    = IB_WC_SEND;
136                 break;
137         case MLX5_OPCODE_RDMA_READ:
138                 wc->opcode    = IB_WC_RDMA_READ;
139                 wc->byte_len  = be32_to_cpu(cqe->byte_cnt);
140                 break;
141         case MLX5_OPCODE_ATOMIC_CS:
142                 wc->opcode    = IB_WC_COMP_SWAP;
143                 wc->byte_len  = 8;
144                 break;
145         case MLX5_OPCODE_ATOMIC_FA:
146                 wc->opcode    = IB_WC_FETCH_ADD;
147                 wc->byte_len  = 8;
148                 break;
149         case MLX5_OPCODE_ATOMIC_MASKED_CS:
150                 wc->opcode    = IB_WC_MASKED_COMP_SWAP;
151                 wc->byte_len  = 8;
152                 break;
153         case MLX5_OPCODE_ATOMIC_MASKED_FA:
154                 wc->opcode    = IB_WC_MASKED_FETCH_ADD;
155                 wc->byte_len  = 8;
156                 break;
157         case MLX5_OPCODE_UMR:
158                 wc->opcode = get_umr_comp(wq, idx);
159                 break;
160         }
161 }
162
163 enum {
164         MLX5_GRH_IN_BUFFER = 1,
165         MLX5_GRH_IN_CQE    = 2,
166 };
167
168 static void handle_responder(struct ib_wc *wc, struct mlx5_cqe64 *cqe,
169                              struct mlx5_ib_qp *qp)
170 {
171         enum rdma_link_layer ll = rdma_port_get_link_layer(qp->ibqp.device, 1);
172         struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
173         struct mlx5_ib_srq *srq;
174         struct mlx5_ib_wq *wq;
175         u16 wqe_ctr;
176         u8 g;
177
178         if (qp->ibqp.srq || qp->ibqp.xrcd) {
179                 struct mlx5_core_srq *msrq = NULL;
180
181                 if (qp->ibqp.xrcd) {
182                         msrq = mlx5_core_get_srq(dev->mdev,
183                                                  be32_to_cpu(cqe->srqn));
184                         srq = to_mibsrq(msrq);
185                 } else {
186                         srq = to_msrq(qp->ibqp.srq);
187                 }
188                 if (srq) {
189                         wqe_ctr = be16_to_cpu(cqe->wqe_counter);
190                         wc->wr_id = srq->wrid[wqe_ctr];
191                         mlx5_ib_free_srq_wqe(srq, wqe_ctr);
192                         if (msrq && atomic_dec_and_test(&msrq->refcount))
193                                 complete(&msrq->free);
194                 }
195         } else {
196                 wq        = &qp->rq;
197                 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
198                 ++wq->tail;
199         }
200         wc->byte_len = be32_to_cpu(cqe->byte_cnt);
201
202         switch (cqe->op_own >> 4) {
203         case MLX5_CQE_RESP_WR_IMM:
204                 wc->opcode      = IB_WC_RECV_RDMA_WITH_IMM;
205                 wc->wc_flags    = IB_WC_WITH_IMM;
206                 wc->ex.imm_data = cqe->imm_inval_pkey;
207                 break;
208         case MLX5_CQE_RESP_SEND:
209                 wc->opcode   = IB_WC_RECV;
210                 wc->wc_flags = IB_WC_IP_CSUM_OK;
211                 if (unlikely(!((cqe->hds_ip_ext & CQE_L3_OK) &&
212                                (cqe->hds_ip_ext & CQE_L4_OK))))
213                         wc->wc_flags = 0;
214                 break;
215         case MLX5_CQE_RESP_SEND_IMM:
216                 wc->opcode      = IB_WC_RECV;
217                 wc->wc_flags    = IB_WC_WITH_IMM;
218                 wc->ex.imm_data = cqe->imm_inval_pkey;
219                 break;
220         case MLX5_CQE_RESP_SEND_INV:
221                 wc->opcode      = IB_WC_RECV;
222                 wc->wc_flags    = IB_WC_WITH_INVALIDATE;
223                 wc->ex.invalidate_rkey = be32_to_cpu(cqe->imm_inval_pkey);
224                 break;
225         }
226         wc->slid           = be16_to_cpu(cqe->slid);
227         wc->sl             = (be32_to_cpu(cqe->flags_rqpn) >> 24) & 0xf;
228         wc->src_qp         = be32_to_cpu(cqe->flags_rqpn) & 0xffffff;
229         wc->dlid_path_bits = cqe->ml_path;
230         g = (be32_to_cpu(cqe->flags_rqpn) >> 28) & 3;
231         wc->wc_flags |= g ? IB_WC_GRH : 0;
232         if (unlikely(is_qp1(qp->ibqp.qp_type))) {
233                 u16 pkey = be32_to_cpu(cqe->imm_inval_pkey) & 0xffff;
234
235                 ib_find_cached_pkey(&dev->ib_dev, qp->port, pkey,
236                                     &wc->pkey_index);
237         } else {
238                 wc->pkey_index = 0;
239         }
240
241         if (ll != IB_LINK_LAYER_ETHERNET)
242                 return;
243
244         switch (wc->sl & 0x3) {
245         case MLX5_CQE_ROCE_L3_HEADER_TYPE_GRH:
246                 wc->network_hdr_type = RDMA_NETWORK_IB;
247                 break;
248         case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV6:
249                 wc->network_hdr_type = RDMA_NETWORK_IPV6;
250                 break;
251         case MLX5_CQE_ROCE_L3_HEADER_TYPE_IPV4:
252                 wc->network_hdr_type = RDMA_NETWORK_IPV4;
253                 break;
254         }
255         wc->wc_flags |= IB_WC_WITH_NETWORK_HDR_TYPE;
256 }
257
258 static void dump_cqe(struct mlx5_ib_dev *dev, struct mlx5_err_cqe *cqe)
259 {
260         __be32 *p = (__be32 *)cqe;
261         int i;
262
263         mlx5_ib_warn(dev, "dump error cqe\n");
264         for (i = 0; i < sizeof(*cqe) / 16; i++, p += 4)
265                 pr_info("%08x %08x %08x %08x\n", be32_to_cpu(p[0]),
266                         be32_to_cpu(p[1]), be32_to_cpu(p[2]),
267                         be32_to_cpu(p[3]));
268 }
269
270 static void mlx5_handle_error_cqe(struct mlx5_ib_dev *dev,
271                                   struct mlx5_err_cqe *cqe,
272                                   struct ib_wc *wc)
273 {
274         int dump = 1;
275
276         switch (cqe->syndrome) {
277         case MLX5_CQE_SYNDROME_LOCAL_LENGTH_ERR:
278                 wc->status = IB_WC_LOC_LEN_ERR;
279                 break;
280         case MLX5_CQE_SYNDROME_LOCAL_QP_OP_ERR:
281                 wc->status = IB_WC_LOC_QP_OP_ERR;
282                 break;
283         case MLX5_CQE_SYNDROME_LOCAL_PROT_ERR:
284                 wc->status = IB_WC_LOC_PROT_ERR;
285                 break;
286         case MLX5_CQE_SYNDROME_WR_FLUSH_ERR:
287                 dump = 0;
288                 wc->status = IB_WC_WR_FLUSH_ERR;
289                 break;
290         case MLX5_CQE_SYNDROME_MW_BIND_ERR:
291                 wc->status = IB_WC_MW_BIND_ERR;
292                 break;
293         case MLX5_CQE_SYNDROME_BAD_RESP_ERR:
294                 wc->status = IB_WC_BAD_RESP_ERR;
295                 break;
296         case MLX5_CQE_SYNDROME_LOCAL_ACCESS_ERR:
297                 wc->status = IB_WC_LOC_ACCESS_ERR;
298                 break;
299         case MLX5_CQE_SYNDROME_REMOTE_INVAL_REQ_ERR:
300                 wc->status = IB_WC_REM_INV_REQ_ERR;
301                 break;
302         case MLX5_CQE_SYNDROME_REMOTE_ACCESS_ERR:
303                 wc->status = IB_WC_REM_ACCESS_ERR;
304                 break;
305         case MLX5_CQE_SYNDROME_REMOTE_OP_ERR:
306                 wc->status = IB_WC_REM_OP_ERR;
307                 break;
308         case MLX5_CQE_SYNDROME_TRANSPORT_RETRY_EXC_ERR:
309                 wc->status = IB_WC_RETRY_EXC_ERR;
310                 dump = 0;
311                 break;
312         case MLX5_CQE_SYNDROME_RNR_RETRY_EXC_ERR:
313                 wc->status = IB_WC_RNR_RETRY_EXC_ERR;
314                 dump = 0;
315                 break;
316         case MLX5_CQE_SYNDROME_REMOTE_ABORTED_ERR:
317                 wc->status = IB_WC_REM_ABORT_ERR;
318                 break;
319         default:
320                 wc->status = IB_WC_GENERAL_ERR;
321                 break;
322         }
323
324         wc->vendor_err = cqe->vendor_err_synd;
325         if (dump)
326                 dump_cqe(dev, cqe);
327 }
328
329 static int is_atomic_response(struct mlx5_ib_qp *qp, uint16_t idx)
330 {
331         /* TBD: waiting decision
332         */
333         return 0;
334 }
335
336 static void *mlx5_get_atomic_laddr(struct mlx5_ib_qp *qp, uint16_t idx)
337 {
338         struct mlx5_wqe_data_seg *dpseg;
339         void *addr;
340
341         dpseg = mlx5_get_send_wqe(qp, idx) + sizeof(struct mlx5_wqe_ctrl_seg) +
342                 sizeof(struct mlx5_wqe_raddr_seg) +
343                 sizeof(struct mlx5_wqe_atomic_seg);
344         addr = (void *)(unsigned long)be64_to_cpu(dpseg->addr);
345         return addr;
346 }
347
348 static void handle_atomic(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
349                           uint16_t idx)
350 {
351         void *addr;
352         int byte_count;
353         int i;
354
355         if (!is_atomic_response(qp, idx))
356                 return;
357
358         byte_count = be32_to_cpu(cqe64->byte_cnt);
359         addr = mlx5_get_atomic_laddr(qp, idx);
360
361         if (byte_count == 4) {
362                 *(uint32_t *)addr = be32_to_cpu(*((__be32 *)addr));
363         } else {
364                 for (i = 0; i < byte_count; i += 8) {
365                         *(uint64_t *)addr = be64_to_cpu(*((__be64 *)addr));
366                         addr += 8;
367                 }
368         }
369
370         return;
371 }
372
373 static void handle_atomics(struct mlx5_ib_qp *qp, struct mlx5_cqe64 *cqe64,
374                            u16 tail, u16 head)
375 {
376         u16 idx;
377
378         do {
379                 idx = tail & (qp->sq.wqe_cnt - 1);
380                 handle_atomic(qp, cqe64, idx);
381                 if (idx == head)
382                         break;
383
384                 tail = qp->sq.w_list[idx].next;
385         } while (1);
386         tail = qp->sq.w_list[idx].next;
387         qp->sq.last_poll = tail;
388 }
389
390 static void free_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf)
391 {
392         mlx5_buf_free(dev->mdev, &buf->buf);
393 }
394
395 static void get_sig_err_item(struct mlx5_sig_err_cqe *cqe,
396                              struct ib_sig_err *item)
397 {
398         u16 syndrome = be16_to_cpu(cqe->syndrome);
399
400 #define GUARD_ERR   (1 << 13)
401 #define APPTAG_ERR  (1 << 12)
402 #define REFTAG_ERR  (1 << 11)
403
404         if (syndrome & GUARD_ERR) {
405                 item->err_type = IB_SIG_BAD_GUARD;
406                 item->expected = be32_to_cpu(cqe->expected_trans_sig) >> 16;
407                 item->actual = be32_to_cpu(cqe->actual_trans_sig) >> 16;
408         } else
409         if (syndrome & REFTAG_ERR) {
410                 item->err_type = IB_SIG_BAD_REFTAG;
411                 item->expected = be32_to_cpu(cqe->expected_reftag);
412                 item->actual = be32_to_cpu(cqe->actual_reftag);
413         } else
414         if (syndrome & APPTAG_ERR) {
415                 item->err_type = IB_SIG_BAD_APPTAG;
416                 item->expected = be32_to_cpu(cqe->expected_trans_sig) & 0xffff;
417                 item->actual = be32_to_cpu(cqe->actual_trans_sig) & 0xffff;
418         } else {
419                 pr_err("Got signature completion error with bad syndrome %04x\n",
420                        syndrome);
421         }
422
423         item->sig_err_offset = be64_to_cpu(cqe->err_offset);
424         item->key = be32_to_cpu(cqe->mkey);
425 }
426
427 static void sw_send_comp(struct mlx5_ib_qp *qp, int num_entries,
428                          struct ib_wc *wc, int *npolled)
429 {
430         struct mlx5_ib_wq *wq;
431         unsigned int cur;
432         unsigned int idx;
433         int np;
434         int i;
435
436         wq = &qp->sq;
437         cur = wq->head - wq->tail;
438         np = *npolled;
439
440         if (cur == 0)
441                 return;
442
443         for (i = 0;  i < cur && np < num_entries; i++) {
444                 idx = wq->last_poll & (wq->wqe_cnt - 1);
445                 wc->wr_id = wq->wrid[idx];
446                 wc->status = IB_WC_WR_FLUSH_ERR;
447                 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
448                 wq->tail++;
449                 np++;
450                 wc->qp = &qp->ibqp;
451                 wc++;
452                 wq->last_poll = wq->w_list[idx].next;
453         }
454         *npolled = np;
455 }
456
457 static void sw_recv_comp(struct mlx5_ib_qp *qp, int num_entries,
458                          struct ib_wc *wc, int *npolled)
459 {
460         struct mlx5_ib_wq *wq;
461         unsigned int cur;
462         int np;
463         int i;
464
465         wq = &qp->rq;
466         cur = wq->head - wq->tail;
467         np = *npolled;
468
469         if (cur == 0)
470                 return;
471
472         for (i = 0;  i < cur && np < num_entries; i++) {
473                 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
474                 wc->status = IB_WC_WR_FLUSH_ERR;
475                 wc->vendor_err = MLX5_CQE_SYNDROME_WR_FLUSH_ERR;
476                 wq->tail++;
477                 np++;
478                 wc->qp = &qp->ibqp;
479                 wc++;
480         }
481         *npolled = np;
482 }
483
484 static void mlx5_ib_poll_sw_comp(struct mlx5_ib_cq *cq, int num_entries,
485                                  struct ib_wc *wc, int *npolled)
486 {
487         struct mlx5_ib_qp *qp;
488
489         *npolled = 0;
490         /* Find uncompleted WQEs belonging to that cq and retrun mmics ones */
491         list_for_each_entry(qp, &cq->list_send_qp, cq_send_list) {
492                 sw_send_comp(qp, num_entries, wc + *npolled, npolled);
493                 if (*npolled >= num_entries)
494                         return;
495         }
496
497         list_for_each_entry(qp, &cq->list_recv_qp, cq_recv_list) {
498                 sw_recv_comp(qp, num_entries, wc + *npolled, npolled);
499                 if (*npolled >= num_entries)
500                         return;
501         }
502 }
503
504 static int mlx5_poll_one(struct mlx5_ib_cq *cq,
505                          struct mlx5_ib_qp **cur_qp,
506                          struct ib_wc *wc)
507 {
508         struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
509         struct mlx5_err_cqe *err_cqe;
510         struct mlx5_cqe64 *cqe64;
511         struct mlx5_core_qp *mqp;
512         struct mlx5_ib_wq *wq;
513         struct mlx5_sig_err_cqe *sig_err_cqe;
514         struct mlx5_core_mkey *mmkey;
515         struct mlx5_ib_mr *mr;
516         uint8_t opcode;
517         uint32_t qpn;
518         u16 wqe_ctr;
519         void *cqe;
520         int idx;
521
522 repoll:
523         cqe = next_cqe_sw(cq);
524         if (!cqe)
525                 return -EAGAIN;
526
527         cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
528
529         ++cq->mcq.cons_index;
530
531         /* Make sure we read CQ entry contents after we've checked the
532          * ownership bit.
533          */
534         rmb();
535
536         opcode = cqe64->op_own >> 4;
537         if (unlikely(opcode == MLX5_CQE_RESIZE_CQ)) {
538                 if (likely(cq->resize_buf)) {
539                         free_cq_buf(dev, &cq->buf);
540                         cq->buf = *cq->resize_buf;
541                         kfree(cq->resize_buf);
542                         cq->resize_buf = NULL;
543                         goto repoll;
544                 } else {
545                         mlx5_ib_warn(dev, "unexpected resize cqe\n");
546                 }
547         }
548
549         qpn = ntohl(cqe64->sop_drop_qpn) & 0xffffff;
550         if (!*cur_qp || (qpn != (*cur_qp)->ibqp.qp_num)) {
551                 /* We do not have to take the QP table lock here,
552                  * because CQs will be locked while QPs are removed
553                  * from the table.
554                  */
555                 mqp = __mlx5_qp_lookup(dev->mdev, qpn);
556                 if (unlikely(!mqp)) {
557                         mlx5_ib_warn(dev, "CQE@CQ %06x for unknown QPN %6x\n",
558                                      cq->mcq.cqn, qpn);
559                         return -EINVAL;
560                 }
561
562                 *cur_qp = to_mibqp(mqp);
563         }
564
565         wc->qp  = &(*cur_qp)->ibqp;
566         switch (opcode) {
567         case MLX5_CQE_REQ:
568                 wq = &(*cur_qp)->sq;
569                 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
570                 idx = wqe_ctr & (wq->wqe_cnt - 1);
571                 handle_good_req(wc, cqe64, wq, idx);
572                 handle_atomics(*cur_qp, cqe64, wq->last_poll, idx);
573                 wc->wr_id = wq->wrid[idx];
574                 wq->tail = wq->wqe_head[idx] + 1;
575                 wc->status = IB_WC_SUCCESS;
576                 break;
577         case MLX5_CQE_RESP_WR_IMM:
578         case MLX5_CQE_RESP_SEND:
579         case MLX5_CQE_RESP_SEND_IMM:
580         case MLX5_CQE_RESP_SEND_INV:
581                 handle_responder(wc, cqe64, *cur_qp);
582                 wc->status = IB_WC_SUCCESS;
583                 break;
584         case MLX5_CQE_RESIZE_CQ:
585                 break;
586         case MLX5_CQE_REQ_ERR:
587         case MLX5_CQE_RESP_ERR:
588                 err_cqe = (struct mlx5_err_cqe *)cqe64;
589                 mlx5_handle_error_cqe(dev, err_cqe, wc);
590                 mlx5_ib_dbg(dev, "%s error cqe on cqn 0x%x:\n",
591                             opcode == MLX5_CQE_REQ_ERR ?
592                             "Requestor" : "Responder", cq->mcq.cqn);
593                 mlx5_ib_dbg(dev, "syndrome 0x%x, vendor syndrome 0x%x\n",
594                             err_cqe->syndrome, err_cqe->vendor_err_synd);
595                 if (opcode == MLX5_CQE_REQ_ERR) {
596                         wq = &(*cur_qp)->sq;
597                         wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
598                         idx = wqe_ctr & (wq->wqe_cnt - 1);
599                         wc->wr_id = wq->wrid[idx];
600                         wq->tail = wq->wqe_head[idx] + 1;
601                 } else {
602                         struct mlx5_ib_srq *srq;
603
604                         if ((*cur_qp)->ibqp.srq) {
605                                 srq = to_msrq((*cur_qp)->ibqp.srq);
606                                 wqe_ctr = be16_to_cpu(cqe64->wqe_counter);
607                                 wc->wr_id = srq->wrid[wqe_ctr];
608                                 mlx5_ib_free_srq_wqe(srq, wqe_ctr);
609                         } else {
610                                 wq = &(*cur_qp)->rq;
611                                 wc->wr_id = wq->wrid[wq->tail & (wq->wqe_cnt - 1)];
612                                 ++wq->tail;
613                         }
614                 }
615                 break;
616         case MLX5_CQE_SIG_ERR:
617                 sig_err_cqe = (struct mlx5_sig_err_cqe *)cqe64;
618
619                 read_lock(&dev->mdev->priv.mkey_table.lock);
620                 mmkey = __mlx5_mr_lookup(dev->mdev,
621                                          mlx5_base_mkey(be32_to_cpu(sig_err_cqe->mkey)));
622                 if (unlikely(!mmkey)) {
623                         read_unlock(&dev->mdev->priv.mkey_table.lock);
624                         mlx5_ib_warn(dev, "CQE@CQ %06x for unknown MR %6x\n",
625                                      cq->mcq.cqn, be32_to_cpu(sig_err_cqe->mkey));
626                         return -EINVAL;
627                 }
628
629                 mr = to_mibmr(mmkey);
630                 get_sig_err_item(sig_err_cqe, &mr->sig->err_item);
631                 mr->sig->sig_err_exists = true;
632                 mr->sig->sigerr_count++;
633
634                 mlx5_ib_warn(dev, "CQN: 0x%x Got SIGERR on key: 0x%x err_type %x err_offset %llx expected %x actual %x\n",
635                              cq->mcq.cqn, mr->sig->err_item.key,
636                              mr->sig->err_item.err_type,
637                              mr->sig->err_item.sig_err_offset,
638                              mr->sig->err_item.expected,
639                              mr->sig->err_item.actual);
640
641                 read_unlock(&dev->mdev->priv.mkey_table.lock);
642                 goto repoll;
643         }
644
645         return 0;
646 }
647
648 static int poll_soft_wc(struct mlx5_ib_cq *cq, int num_entries,
649                         struct ib_wc *wc)
650 {
651         struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
652         struct mlx5_ib_wc *soft_wc, *next;
653         int npolled = 0;
654
655         list_for_each_entry_safe(soft_wc, next, &cq->wc_list, list) {
656                 if (npolled >= num_entries)
657                         break;
658
659                 mlx5_ib_dbg(dev, "polled software generated completion on CQ 0x%x\n",
660                             cq->mcq.cqn);
661
662                 wc[npolled++] = soft_wc->wc;
663                 list_del(&soft_wc->list);
664                 kfree(soft_wc);
665         }
666
667         return npolled;
668 }
669
670 int mlx5_ib_poll_cq(struct ib_cq *ibcq, int num_entries, struct ib_wc *wc)
671 {
672         struct mlx5_ib_cq *cq = to_mcq(ibcq);
673         struct mlx5_ib_qp *cur_qp = NULL;
674         struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
675         struct mlx5_core_dev *mdev = dev->mdev;
676         unsigned long flags;
677         int soft_polled = 0;
678         int npolled;
679         int err = 0;
680
681         spin_lock_irqsave(&cq->lock, flags);
682         if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
683                 mlx5_ib_poll_sw_comp(cq, num_entries, wc, &npolled);
684                 goto out;
685         }
686
687         if (unlikely(!list_empty(&cq->wc_list)))
688                 soft_polled = poll_soft_wc(cq, num_entries, wc);
689
690         for (npolled = 0; npolled < num_entries - soft_polled; npolled++) {
691                 err = mlx5_poll_one(cq, &cur_qp, wc + soft_polled + npolled);
692                 if (err)
693                         break;
694         }
695
696         if (npolled)
697                 mlx5_cq_set_ci(&cq->mcq);
698 out:
699         spin_unlock_irqrestore(&cq->lock, flags);
700
701         if (err == 0 || err == -EAGAIN)
702                 return soft_polled + npolled;
703         else
704                 return err;
705 }
706
707 int mlx5_ib_arm_cq(struct ib_cq *ibcq, enum ib_cq_notify_flags flags)
708 {
709         struct mlx5_core_dev *mdev = to_mdev(ibcq->device)->mdev;
710         struct mlx5_ib_cq *cq = to_mcq(ibcq);
711         void __iomem *uar_page = mdev->priv.uuari.uars[0].map;
712         unsigned long irq_flags;
713         int ret = 0;
714
715         spin_lock_irqsave(&cq->lock, irq_flags);
716         if (cq->notify_flags != IB_CQ_NEXT_COMP)
717                 cq->notify_flags = flags & IB_CQ_SOLICITED_MASK;
718
719         if ((flags & IB_CQ_REPORT_MISSED_EVENTS) && !list_empty(&cq->wc_list))
720                 ret = 1;
721         spin_unlock_irqrestore(&cq->lock, irq_flags);
722
723         mlx5_cq_arm(&cq->mcq,
724                     (flags & IB_CQ_SOLICITED_MASK) == IB_CQ_SOLICITED ?
725                     MLX5_CQ_DB_REQ_NOT_SOL : MLX5_CQ_DB_REQ_NOT,
726                     uar_page,
727                     MLX5_GET_DOORBELL_LOCK(&mdev->priv.cq_uar_lock),
728                     to_mcq(ibcq)->mcq.cons_index);
729
730         return ret;
731 }
732
733 static int alloc_cq_buf(struct mlx5_ib_dev *dev, struct mlx5_ib_cq_buf *buf,
734                         int nent, int cqe_size)
735 {
736         int err;
737
738         err = mlx5_buf_alloc(dev->mdev, nent * cqe_size, &buf->buf);
739         if (err)
740                 return err;
741
742         buf->cqe_size = cqe_size;
743         buf->nent = nent;
744
745         return 0;
746 }
747
748 static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata,
749                           struct ib_ucontext *context, struct mlx5_ib_cq *cq,
750                           int entries, u32 **cqb,
751                           int *cqe_size, int *index, int *inlen)
752 {
753         struct mlx5_ib_create_cq ucmd;
754         size_t ucmdlen;
755         int page_shift;
756         __be64 *pas;
757         int npages;
758         int ncont;
759         void *cqc;
760         int err;
761
762         ucmdlen =
763                 (udata->inlen - sizeof(struct ib_uverbs_cmd_hdr) <
764                  sizeof(ucmd)) ? (sizeof(ucmd) -
765                                   sizeof(ucmd.reserved)) : sizeof(ucmd);
766
767         if (ib_copy_from_udata(&ucmd, udata, ucmdlen))
768                 return -EFAULT;
769
770         if (ucmdlen == sizeof(ucmd) &&
771             ucmd.reserved != 0)
772                 return -EINVAL;
773
774         if (ucmd.cqe_size != 64 && ucmd.cqe_size != 128)
775                 return -EINVAL;
776
777         *cqe_size = ucmd.cqe_size;
778
779         cq->buf.umem = ib_umem_get(context, ucmd.buf_addr,
780                                    entries * ucmd.cqe_size,
781                                    IB_ACCESS_LOCAL_WRITE, 1);
782         if (IS_ERR(cq->buf.umem)) {
783                 err = PTR_ERR(cq->buf.umem);
784                 return err;
785         }
786
787         err = mlx5_ib_db_map_user(to_mucontext(context), ucmd.db_addr,
788                                   &cq->db);
789         if (err)
790                 goto err_umem;
791
792         mlx5_ib_cont_pages(cq->buf.umem, ucmd.buf_addr, &npages, &page_shift,
793                            &ncont, NULL);
794         mlx5_ib_dbg(dev, "addr 0x%llx, size %u, npages %d, page_shift %d, ncont %d\n",
795                     ucmd.buf_addr, entries * ucmd.cqe_size, npages, page_shift, ncont);
796
797         *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
798                  MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * ncont;
799         *cqb = mlx5_vzalloc(*inlen);
800         if (!*cqb) {
801                 err = -ENOMEM;
802                 goto err_db;
803         }
804
805         pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
806         mlx5_ib_populate_pas(dev, cq->buf.umem, page_shift, pas, 0);
807
808         cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
809         MLX5_SET(cqc, cqc, log_page_size,
810                  page_shift - MLX5_ADAPTER_PAGE_SHIFT);
811
812         *index = to_mucontext(context)->uuari.uars[0].index;
813
814         return 0;
815
816 err_db:
817         mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
818
819 err_umem:
820         ib_umem_release(cq->buf.umem);
821         return err;
822 }
823
824 static void destroy_cq_user(struct mlx5_ib_cq *cq, struct ib_ucontext *context)
825 {
826         mlx5_ib_db_unmap_user(to_mucontext(context), &cq->db);
827         ib_umem_release(cq->buf.umem);
828 }
829
830 static void init_cq_buf(struct mlx5_ib_cq *cq, struct mlx5_ib_cq_buf *buf)
831 {
832         int i;
833         void *cqe;
834         struct mlx5_cqe64 *cqe64;
835
836         for (i = 0; i < buf->nent; i++) {
837                 cqe = get_cqe_from_buf(buf, i, buf->cqe_size);
838                 cqe64 = buf->cqe_size == 64 ? cqe : cqe + 64;
839                 cqe64->op_own = MLX5_CQE_INVALID << 4;
840         }
841 }
842
843 static int create_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
844                             int entries, int cqe_size,
845                             u32 **cqb, int *index, int *inlen)
846 {
847         __be64 *pas;
848         void *cqc;
849         int err;
850
851         err = mlx5_db_alloc(dev->mdev, &cq->db);
852         if (err)
853                 return err;
854
855         cq->mcq.set_ci_db  = cq->db.db;
856         cq->mcq.arm_db     = cq->db.db + 1;
857         cq->mcq.cqe_sz = cqe_size;
858
859         err = alloc_cq_buf(dev, &cq->buf, entries, cqe_size);
860         if (err)
861                 goto err_db;
862
863         init_cq_buf(cq, &cq->buf);
864
865         *inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
866                  MLX5_FLD_SZ_BYTES(create_cq_in, pas[0]) * cq->buf.buf.npages;
867         *cqb = mlx5_vzalloc(*inlen);
868         if (!*cqb) {
869                 err = -ENOMEM;
870                 goto err_buf;
871         }
872
873         pas = (__be64 *)MLX5_ADDR_OF(create_cq_in, *cqb, pas);
874         mlx5_fill_page_array(&cq->buf.buf, pas);
875
876         cqc = MLX5_ADDR_OF(create_cq_in, *cqb, cq_context);
877         MLX5_SET(cqc, cqc, log_page_size,
878                  cq->buf.buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);
879
880         *index = dev->mdev->priv.uuari.uars[0].index;
881
882         return 0;
883
884 err_buf:
885         free_cq_buf(dev, &cq->buf);
886
887 err_db:
888         mlx5_db_free(dev->mdev, &cq->db);
889         return err;
890 }
891
892 static void destroy_cq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
893 {
894         free_cq_buf(dev, &cq->buf);
895         mlx5_db_free(dev->mdev, &cq->db);
896 }
897
898 static void notify_soft_wc_handler(struct work_struct *work)
899 {
900         struct mlx5_ib_cq *cq = container_of(work, struct mlx5_ib_cq,
901                                              notify_work);
902
903         cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context);
904 }
905
906 struct ib_cq *mlx5_ib_create_cq(struct ib_device *ibdev,
907                                 const struct ib_cq_init_attr *attr,
908                                 struct ib_ucontext *context,
909                                 struct ib_udata *udata)
910 {
911         int entries = attr->cqe;
912         int vector = attr->comp_vector;
913         struct mlx5_ib_dev *dev = to_mdev(ibdev);
914         struct mlx5_ib_cq *cq;
915         int uninitialized_var(index);
916         int uninitialized_var(inlen);
917         u32 *cqb = NULL;
918         void *cqc;
919         int cqe_size;
920         unsigned int irqn;
921         int eqn;
922         int err;
923
924         if (entries < 0 ||
925             (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))))
926                 return ERR_PTR(-EINVAL);
927
928         if (check_cq_create_flags(attr->flags))
929                 return ERR_PTR(-EOPNOTSUPP);
930
931         entries = roundup_pow_of_two(entries + 1);
932         if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)))
933                 return ERR_PTR(-EINVAL);
934
935         cq = kzalloc(sizeof(*cq), GFP_KERNEL);
936         if (!cq)
937                 return ERR_PTR(-ENOMEM);
938
939         cq->ibcq.cqe = entries - 1;
940         mutex_init(&cq->resize_mutex);
941         spin_lock_init(&cq->lock);
942         cq->resize_buf = NULL;
943         cq->resize_umem = NULL;
944         cq->create_flags = attr->flags;
945         INIT_LIST_HEAD(&cq->list_send_qp);
946         INIT_LIST_HEAD(&cq->list_recv_qp);
947
948         if (context) {
949                 err = create_cq_user(dev, udata, context, cq, entries,
950                                      &cqb, &cqe_size, &index, &inlen);
951                 if (err)
952                         goto err_create;
953         } else {
954                 /* for now choose 64 bytes till we have a proper interface */
955                 cqe_size = 64;
956                 err = create_cq_kernel(dev, cq, entries, cqe_size, &cqb,
957                                        &index, &inlen);
958                 if (err)
959                         goto err_create;
960
961                 INIT_WORK(&cq->notify_work, notify_soft_wc_handler);
962         }
963
964         err = mlx5_vector2eqn(dev->mdev, vector, &eqn, &irqn);
965         if (err)
966                 goto err_cqb;
967
968         cq->cqe_size = cqe_size;
969
970         cqc = MLX5_ADDR_OF(create_cq_in, cqb, cq_context);
971         MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
972         MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
973         MLX5_SET(cqc, cqc, uar_page, index);
974         MLX5_SET(cqc, cqc, c_eqn, eqn);
975         MLX5_SET64(cqc, cqc, dbr_addr, cq->db.dma);
976         if (cq->create_flags & IB_CQ_FLAGS_IGNORE_OVERRUN)
977                 MLX5_SET(cqc, cqc, oi, 1);
978
979         err = mlx5_core_create_cq(dev->mdev, &cq->mcq, cqb, inlen);
980         if (err)
981                 goto err_cqb;
982
983         mlx5_ib_dbg(dev, "cqn 0x%x\n", cq->mcq.cqn);
984         cq->mcq.irqn = irqn;
985         if (context)
986                 cq->mcq.tasklet_ctx.comp = mlx5_ib_cq_comp;
987         else
988                 cq->mcq.comp  = mlx5_ib_cq_comp;
989         cq->mcq.event = mlx5_ib_cq_event;
990
991         INIT_LIST_HEAD(&cq->wc_list);
992
993         if (context)
994                 if (ib_copy_to_udata(udata, &cq->mcq.cqn, sizeof(__u32))) {
995                         err = -EFAULT;
996                         goto err_cmd;
997                 }
998
999
1000         kvfree(cqb);
1001         return &cq->ibcq;
1002
1003 err_cmd:
1004         mlx5_core_destroy_cq(dev->mdev, &cq->mcq);
1005
1006 err_cqb:
1007         kvfree(cqb);
1008         if (context)
1009                 destroy_cq_user(cq, context);
1010         else
1011                 destroy_cq_kernel(dev, cq);
1012
1013 err_create:
1014         kfree(cq);
1015
1016         return ERR_PTR(err);
1017 }
1018
1019
1020 int mlx5_ib_destroy_cq(struct ib_cq *cq)
1021 {
1022         struct mlx5_ib_dev *dev = to_mdev(cq->device);
1023         struct mlx5_ib_cq *mcq = to_mcq(cq);
1024         struct ib_ucontext *context = NULL;
1025
1026         if (cq->uobject)
1027                 context = cq->uobject->context;
1028
1029         mlx5_core_destroy_cq(dev->mdev, &mcq->mcq);
1030         if (context)
1031                 destroy_cq_user(mcq, context);
1032         else
1033                 destroy_cq_kernel(dev, mcq);
1034
1035         kfree(mcq);
1036
1037         return 0;
1038 }
1039
1040 static int is_equal_rsn(struct mlx5_cqe64 *cqe64, u32 rsn)
1041 {
1042         return rsn == (ntohl(cqe64->sop_drop_qpn) & 0xffffff);
1043 }
1044
1045 void __mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 rsn, struct mlx5_ib_srq *srq)
1046 {
1047         struct mlx5_cqe64 *cqe64, *dest64;
1048         void *cqe, *dest;
1049         u32 prod_index;
1050         int nfreed = 0;
1051         u8 owner_bit;
1052
1053         if (!cq)
1054                 return;
1055
1056         /* First we need to find the current producer index, so we
1057          * know where to start cleaning from.  It doesn't matter if HW
1058          * adds new entries after this loop -- the QP we're worried
1059          * about is already in RESET, so the new entries won't come
1060          * from our QP and therefore don't need to be checked.
1061          */
1062         for (prod_index = cq->mcq.cons_index; get_sw_cqe(cq, prod_index); prod_index++)
1063                 if (prod_index == cq->mcq.cons_index + cq->ibcq.cqe)
1064                         break;
1065
1066         /* Now sweep backwards through the CQ, removing CQ entries
1067          * that match our QP by copying older entries on top of them.
1068          */
1069         while ((int) --prod_index - (int) cq->mcq.cons_index >= 0) {
1070                 cqe = get_cqe(cq, prod_index & cq->ibcq.cqe);
1071                 cqe64 = (cq->mcq.cqe_sz == 64) ? cqe : cqe + 64;
1072                 if (is_equal_rsn(cqe64, rsn)) {
1073                         if (srq && (ntohl(cqe64->srqn) & 0xffffff))
1074                                 mlx5_ib_free_srq_wqe(srq, be16_to_cpu(cqe64->wqe_counter));
1075                         ++nfreed;
1076                 } else if (nfreed) {
1077                         dest = get_cqe(cq, (prod_index + nfreed) & cq->ibcq.cqe);
1078                         dest64 = (cq->mcq.cqe_sz == 64) ? dest : dest + 64;
1079                         owner_bit = dest64->op_own & MLX5_CQE_OWNER_MASK;
1080                         memcpy(dest, cqe, cq->mcq.cqe_sz);
1081                         dest64->op_own = owner_bit |
1082                                 (dest64->op_own & ~MLX5_CQE_OWNER_MASK);
1083                 }
1084         }
1085
1086         if (nfreed) {
1087                 cq->mcq.cons_index += nfreed;
1088                 /* Make sure update of buffer contents is done before
1089                  * updating consumer index.
1090                  */
1091                 wmb();
1092                 mlx5_cq_set_ci(&cq->mcq);
1093         }
1094 }
1095
1096 void mlx5_ib_cq_clean(struct mlx5_ib_cq *cq, u32 qpn, struct mlx5_ib_srq *srq)
1097 {
1098         if (!cq)
1099                 return;
1100
1101         spin_lock_irq(&cq->lock);
1102         __mlx5_ib_cq_clean(cq, qpn, srq);
1103         spin_unlock_irq(&cq->lock);
1104 }
1105
1106 int mlx5_ib_modify_cq(struct ib_cq *cq, u16 cq_count, u16 cq_period)
1107 {
1108         struct mlx5_ib_dev *dev = to_mdev(cq->device);
1109         struct mlx5_ib_cq *mcq = to_mcq(cq);
1110         int err;
1111
1112         if (!MLX5_CAP_GEN(dev->mdev, cq_moderation))
1113                 return -ENOSYS;
1114
1115         err = mlx5_core_modify_cq_moderation(dev->mdev, &mcq->mcq,
1116                                              cq_period, cq_count);
1117         if (err)
1118                 mlx5_ib_warn(dev, "modify cq 0x%x failed\n", mcq->mcq.cqn);
1119
1120         return err;
1121 }
1122
1123 static int resize_user(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1124                        int entries, struct ib_udata *udata, int *npas,
1125                        int *page_shift, int *cqe_size)
1126 {
1127         struct mlx5_ib_resize_cq ucmd;
1128         struct ib_umem *umem;
1129         int err;
1130         int npages;
1131         struct ib_ucontext *context = cq->buf.umem->context;
1132
1133         err = ib_copy_from_udata(&ucmd, udata, sizeof(ucmd));
1134         if (err)
1135                 return err;
1136
1137         if (ucmd.reserved0 || ucmd.reserved1)
1138                 return -EINVAL;
1139
1140         umem = ib_umem_get(context, ucmd.buf_addr, entries * ucmd.cqe_size,
1141                            IB_ACCESS_LOCAL_WRITE, 1);
1142         if (IS_ERR(umem)) {
1143                 err = PTR_ERR(umem);
1144                 return err;
1145         }
1146
1147         mlx5_ib_cont_pages(umem, ucmd.buf_addr, &npages, page_shift,
1148                            npas, NULL);
1149
1150         cq->resize_umem = umem;
1151         *cqe_size = ucmd.cqe_size;
1152
1153         return 0;
1154 }
1155
1156 static void un_resize_user(struct mlx5_ib_cq *cq)
1157 {
1158         ib_umem_release(cq->resize_umem);
1159 }
1160
1161 static int resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq,
1162                          int entries, int cqe_size)
1163 {
1164         int err;
1165
1166         cq->resize_buf = kzalloc(sizeof(*cq->resize_buf), GFP_KERNEL);
1167         if (!cq->resize_buf)
1168                 return -ENOMEM;
1169
1170         err = alloc_cq_buf(dev, cq->resize_buf, entries, cqe_size);
1171         if (err)
1172                 goto ex;
1173
1174         init_cq_buf(cq, cq->resize_buf);
1175
1176         return 0;
1177
1178 ex:
1179         kfree(cq->resize_buf);
1180         return err;
1181 }
1182
1183 static void un_resize_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_cq *cq)
1184 {
1185         free_cq_buf(dev, cq->resize_buf);
1186         cq->resize_buf = NULL;
1187 }
1188
1189 static int copy_resize_cqes(struct mlx5_ib_cq *cq)
1190 {
1191         struct mlx5_ib_dev *dev = to_mdev(cq->ibcq.device);
1192         struct mlx5_cqe64 *scqe64;
1193         struct mlx5_cqe64 *dcqe64;
1194         void *start_cqe;
1195         void *scqe;
1196         void *dcqe;
1197         int ssize;
1198         int dsize;
1199         int i;
1200         u8 sw_own;
1201
1202         ssize = cq->buf.cqe_size;
1203         dsize = cq->resize_buf->cqe_size;
1204         if (ssize != dsize) {
1205                 mlx5_ib_warn(dev, "resize from different cqe size is not supported\n");
1206                 return -EINVAL;
1207         }
1208
1209         i = cq->mcq.cons_index;
1210         scqe = get_sw_cqe(cq, i);
1211         scqe64 = ssize == 64 ? scqe : scqe + 64;
1212         start_cqe = scqe;
1213         if (!scqe) {
1214                 mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1215                 return -EINVAL;
1216         }
1217
1218         while ((scqe64->op_own >> 4) != MLX5_CQE_RESIZE_CQ) {
1219                 dcqe = get_cqe_from_buf(cq->resize_buf,
1220                                         (i + 1) & (cq->resize_buf->nent),
1221                                         dsize);
1222                 dcqe64 = dsize == 64 ? dcqe : dcqe + 64;
1223                 sw_own = sw_ownership_bit(i + 1, cq->resize_buf->nent);
1224                 memcpy(dcqe, scqe, dsize);
1225                 dcqe64->op_own = (dcqe64->op_own & ~MLX5_CQE_OWNER_MASK) | sw_own;
1226
1227                 ++i;
1228                 scqe = get_sw_cqe(cq, i);
1229                 scqe64 = ssize == 64 ? scqe : scqe + 64;
1230                 if (!scqe) {
1231                         mlx5_ib_warn(dev, "expected cqe in sw ownership\n");
1232                         return -EINVAL;
1233                 }
1234
1235                 if (scqe == start_cqe) {
1236                         pr_warn("resize CQ failed to get resize CQE, CQN 0x%x\n",
1237                                 cq->mcq.cqn);
1238                         return -ENOMEM;
1239                 }
1240         }
1241         ++cq->mcq.cons_index;
1242         return 0;
1243 }
1244
1245 int mlx5_ib_resize_cq(struct ib_cq *ibcq, int entries, struct ib_udata *udata)
1246 {
1247         struct mlx5_ib_dev *dev = to_mdev(ibcq->device);
1248         struct mlx5_ib_cq *cq = to_mcq(ibcq);
1249         void *cqc;
1250         u32 *in;
1251         int err;
1252         int npas;
1253         __be64 *pas;
1254         int page_shift;
1255         int inlen;
1256         int uninitialized_var(cqe_size);
1257         unsigned long flags;
1258
1259         if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) {
1260                 pr_info("Firmware does not support resize CQ\n");
1261                 return -ENOSYS;
1262         }
1263
1264         if (entries < 1 ||
1265             entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) {
1266                 mlx5_ib_warn(dev, "wrong entries number %d, max %d\n",
1267                              entries,
1268                              1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz));
1269                 return -EINVAL;
1270         }
1271
1272         entries = roundup_pow_of_two(entries + 1);
1273         if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)) + 1)
1274                 return -EINVAL;
1275
1276         if (entries == ibcq->cqe + 1)
1277                 return 0;
1278
1279         mutex_lock(&cq->resize_mutex);
1280         if (udata) {
1281                 err = resize_user(dev, cq, entries, udata, &npas, &page_shift,
1282                                   &cqe_size);
1283         } else {
1284                 cqe_size = 64;
1285                 err = resize_kernel(dev, cq, entries, cqe_size);
1286                 if (!err) {
1287                         npas = cq->resize_buf->buf.npages;
1288                         page_shift = cq->resize_buf->buf.page_shift;
1289                 }
1290         }
1291
1292         if (err)
1293                 goto ex;
1294
1295         inlen = MLX5_ST_SZ_BYTES(modify_cq_in) +
1296                 MLX5_FLD_SZ_BYTES(modify_cq_in, pas[0]) * npas;
1297
1298         in = mlx5_vzalloc(inlen);
1299         if (!in) {
1300                 err = -ENOMEM;
1301                 goto ex_resize;
1302         }
1303
1304         pas = (__be64 *)MLX5_ADDR_OF(modify_cq_in, in, pas);
1305         if (udata)
1306                 mlx5_ib_populate_pas(dev, cq->resize_umem, page_shift,
1307                                      pas, 0);
1308         else
1309                 mlx5_fill_page_array(&cq->resize_buf->buf, pas);
1310
1311         MLX5_SET(modify_cq_in, in,
1312                  modify_field_select_resize_field_select.resize_field_select.resize_field_select,
1313                  MLX5_MODIFY_CQ_MASK_LOG_SIZE  |
1314                  MLX5_MODIFY_CQ_MASK_PG_OFFSET |
1315                  MLX5_MODIFY_CQ_MASK_PG_SIZE);
1316
1317         cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
1318
1319         MLX5_SET(cqc, cqc, log_page_size,
1320                  page_shift - MLX5_ADAPTER_PAGE_SHIFT);
1321         MLX5_SET(cqc, cqc, cqe_sz, cqe_sz_to_mlx_sz(cqe_size));
1322         MLX5_SET(cqc, cqc, log_cq_size, ilog2(entries));
1323
1324         MLX5_SET(modify_cq_in, in, op_mod, MLX5_CQ_OPMOD_RESIZE);
1325         MLX5_SET(modify_cq_in, in, cqn, cq->mcq.cqn);
1326
1327         err = mlx5_core_modify_cq(dev->mdev, &cq->mcq, in, inlen);
1328         if (err)
1329                 goto ex_alloc;
1330
1331         if (udata) {
1332                 cq->ibcq.cqe = entries - 1;
1333                 ib_umem_release(cq->buf.umem);
1334                 cq->buf.umem = cq->resize_umem;
1335                 cq->resize_umem = NULL;
1336         } else {
1337                 struct mlx5_ib_cq_buf tbuf;
1338                 int resized = 0;
1339
1340                 spin_lock_irqsave(&cq->lock, flags);
1341                 if (cq->resize_buf) {
1342                         err = copy_resize_cqes(cq);
1343                         if (!err) {
1344                                 tbuf = cq->buf;
1345                                 cq->buf = *cq->resize_buf;
1346                                 kfree(cq->resize_buf);
1347                                 cq->resize_buf = NULL;
1348                                 resized = 1;
1349                         }
1350                 }
1351                 cq->ibcq.cqe = entries - 1;
1352                 spin_unlock_irqrestore(&cq->lock, flags);
1353                 if (resized)
1354                         free_cq_buf(dev, &tbuf);
1355         }
1356         mutex_unlock(&cq->resize_mutex);
1357
1358         kvfree(in);
1359         return 0;
1360
1361 ex_alloc:
1362         kvfree(in);
1363
1364 ex_resize:
1365         if (udata)
1366                 un_resize_user(cq);
1367         else
1368                 un_resize_kernel(dev, cq);
1369 ex:
1370         mutex_unlock(&cq->resize_mutex);
1371         return err;
1372 }
1373
1374 int mlx5_ib_get_cqe_size(struct mlx5_ib_dev *dev, struct ib_cq *ibcq)
1375 {
1376         struct mlx5_ib_cq *cq;
1377
1378         if (!ibcq)
1379                 return 128;
1380
1381         cq = to_mcq(ibcq);
1382         return cq->cqe_size;
1383 }
1384
1385 /* Called from atomic context */
1386 int mlx5_ib_generate_wc(struct ib_cq *ibcq, struct ib_wc *wc)
1387 {
1388         struct mlx5_ib_wc *soft_wc;
1389         struct mlx5_ib_cq *cq = to_mcq(ibcq);
1390         unsigned long flags;
1391
1392         soft_wc = kmalloc(sizeof(*soft_wc), GFP_ATOMIC);
1393         if (!soft_wc)
1394                 return -ENOMEM;
1395
1396         soft_wc->wc = *wc;
1397         spin_lock_irqsave(&cq->lock, flags);
1398         list_add_tail(&soft_wc->list, &cq->wc_list);
1399         if (cq->notify_flags == IB_CQ_NEXT_COMP ||
1400             wc->status != IB_WC_SUCCESS) {
1401                 cq->notify_flags = 0;
1402                 schedule_work(&cq->notify_work);
1403         }
1404         spin_unlock_irqrestore(&cq->lock, flags);
1405
1406         return 0;
1407 }