Linux Audio

Check our new training course

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