qp.c 147.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/module.h>
#include <rdma/ib_umem.h>
35
#include <rdma/ib_cache.h>
36
#include <rdma/ib_user_verbs.h>
M
Mark Zhang 已提交
37
#include <rdma/rdma_counter.h>
38
#include <linux/mlx5/fs.h>
39
#include "mlx5_ib.h"
40
#include "ib_rep.h"
41
#include "cmd.h"
42
#include "qp.h"
43
#include "wr.h"
44 45 46 47 48 49 50 51 52 53 54 55

enum {
	MLX5_IB_ACK_REQ_FREQ	= 8,
};

enum {
	MLX5_IB_DEFAULT_SCHED_QUEUE	= 0x83,
	MLX5_IB_DEFAULT_QP0_SCHED_QUEUE	= 0x3f,
	MLX5_IB_LINK_TYPE_IB		= 0,
	MLX5_IB_LINK_TYPE_ETH		= 1
};

56 57
enum raw_qp_set_mask_map {
	MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID		= 1UL << 0,
58
	MLX5_RAW_QP_RATE_LIMIT			= 1UL << 1,
59 60
};

61 62
struct mlx5_modify_raw_qp_param {
	u16 operation;
63 64

	u32 set_mask; /* raw_qp_set_mask_map */
65 66 67

	struct mlx5_rate_limit rl;

68
	u8 rq_q_ctr_id;
69
	u16 port;
70 71
};

72 73 74 75
static void get_cqs(enum ib_qp_type qp_type,
		    struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
		    struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq);

76 77 78 79 80 81 82 83 84 85
static int is_qp0(enum ib_qp_type qp_type)
{
	return qp_type == IB_QPT_SMI;
}

static int is_sqp(enum ib_qp_type qp_type)
{
	return is_qp0(qp_type) || is_qp1(qp_type);
}

86
/**
87 88
 * mlx5_ib_read_user_wqe_common() - Copy a WQE (or part of) from user WQ
 * to kernel buffer
89
 *
90 91 92 93 94 95 96 97 98
 * @umem: User space memory where the WQ is
 * @buffer: buffer to copy to
 * @buflen: buffer length
 * @wqe_index: index of WQE to copy from
 * @wq_offset: offset to start of WQ
 * @wq_wqe_cnt: number of WQEs in WQ
 * @wq_wqe_shift: log2 of WQE size
 * @bcnt: number of bytes to copy
 * @bytes_copied: number of bytes to copy (return value)
99
 *
100 101
 * Copies from start of WQE bcnt or less bytes.
 * Does not gurantee to copy the entire WQE.
102
 *
103
 * Return: zero on success, or an error code.
104
 */
105 106 107 108
static int mlx5_ib_read_user_wqe_common(struct ib_umem *umem, void *buffer,
					size_t buflen, int wqe_index,
					int wq_offset, int wq_wqe_cnt,
					int wq_wqe_shift, int bcnt,
109
					size_t *bytes_copied)
110
{
111 112 113
	size_t offset = wq_offset + ((wqe_index % wq_wqe_cnt) << wq_wqe_shift);
	size_t wq_end = wq_offset + (wq_wqe_cnt << wq_wqe_shift);
	size_t copy_length;
114 115
	int ret;

116 117 118 119 120 121 122 123 124
	/* don't copy more than requested, more than buffer length or
	 * beyond WQ end
	 */
	copy_length = min_t(u32, buflen, wq_end - offset);
	copy_length = min_t(u32, copy_length, bcnt);

	ret = ib_umem_copy_from(buffer, umem, offset, copy_length);
	if (ret)
		return ret;
125

126 127
	if (!ret && bytes_copied)
		*bytes_copied = copy_length;
128

129 130
	return 0;
}
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
static int mlx5_ib_read_kernel_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
				      void *buffer, size_t buflen, size_t *bc)
{
	struct mlx5_wqe_ctrl_seg *ctrl;
	size_t bytes_copied = 0;
	size_t wqe_length;
	void *p;
	int ds;

	wqe_index = wqe_index & qp->sq.fbc.sz_m1;

	/* read the control segment first */
	p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
	ctrl = p;
	ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
	wqe_length = ds * MLX5_WQE_DS_UNITS;

	/* read rest of WQE if it spreads over more than one stride */
	while (bytes_copied < wqe_length) {
		size_t copy_length =
			min_t(size_t, buflen - bytes_copied, MLX5_SEND_WQE_BB);

		if (!copy_length)
			break;

		memcpy(buffer + bytes_copied, p, copy_length);
		bytes_copied += copy_length;

		wqe_index = (wqe_index + 1) & qp->sq.fbc.sz_m1;
		p = mlx5_frag_buf_get_wqe(&qp->sq.fbc, wqe_index);
	}
	*bc = bytes_copied;
	return 0;
}

static int mlx5_ib_read_user_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index,
				    void *buffer, size_t buflen, size_t *bc)
169 170 171 172 173 174 175 176 177 178 179 180
{
	struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
	struct ib_umem *umem = base->ubuffer.umem;
	struct mlx5_ib_wq *wq = &qp->sq;
	struct mlx5_wqe_ctrl_seg *ctrl;
	size_t bytes_copied;
	size_t bytes_copied2;
	size_t wqe_length;
	int ret;
	int ds;

	/* at first read as much as possible */
181 182 183
	ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
					   wq->offset, wq->wqe_cnt,
					   wq->wqe_shift, buflen,
184
					   &bytes_copied);
185 186 187
	if (ret)
		return ret;

188 189 190 191 192 193 194
	/* we need at least control segment size to proceed */
	if (bytes_copied < sizeof(*ctrl))
		return -EINVAL;

	ctrl = buffer;
	ds = be32_to_cpu(ctrl->qpn_ds) & MLX5_WQE_CTRL_DS_MASK;
	wqe_length = ds * MLX5_WQE_DS_UNITS;
195

196 197 198 199
	/* if we copied enough then we are done */
	if (bytes_copied >= wqe_length) {
		*bc = bytes_copied;
		return 0;
200 201
	}

202 203 204 205
	/* otherwise this a wrapped around wqe
	 * so read the remaining bytes starting
	 * from  wqe_index 0
	 */
206 207 208
	ret = mlx5_ib_read_user_wqe_common(umem, buffer + bytes_copied,
					   buflen - bytes_copied, 0, wq->offset,
					   wq->wqe_cnt, wq->wqe_shift,
209 210 211 212 213 214 215 216 217
					   wqe_length - bytes_copied,
					   &bytes_copied2);

	if (ret)
		return ret;
	*bc = bytes_copied + bytes_copied2;
	return 0;
}

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
int mlx5_ib_read_wqe_sq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
			size_t buflen, size_t *bc)
{
	struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
	struct ib_umem *umem = base->ubuffer.umem;

	if (buflen < sizeof(struct mlx5_wqe_ctrl_seg))
		return -EINVAL;

	if (!umem)
		return mlx5_ib_read_kernel_wqe_sq(qp, wqe_index, buffer,
						  buflen, bc);

	return mlx5_ib_read_user_wqe_sq(qp, wqe_index, buffer, buflen, bc);
}

static int mlx5_ib_read_user_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index,
				    void *buffer, size_t buflen, size_t *bc)
236 237 238 239 240 241 242
{
	struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
	struct ib_umem *umem = base->ubuffer.umem;
	struct mlx5_ib_wq *wq = &qp->rq;
	size_t bytes_copied;
	int ret;

243 244 245
	ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index,
					   wq->offset, wq->wqe_cnt,
					   wq->wqe_shift, buflen,
246
					   &bytes_copied);
247 248 249

	if (ret)
		return ret;
250 251 252 253
	*bc = bytes_copied;
	return 0;
}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
int mlx5_ib_read_wqe_rq(struct mlx5_ib_qp *qp, int wqe_index, void *buffer,
			size_t buflen, size_t *bc)
{
	struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
	struct ib_umem *umem = base->ubuffer.umem;
	struct mlx5_ib_wq *wq = &qp->rq;
	size_t wqe_size = 1 << wq->wqe_shift;

	if (buflen < wqe_size)
		return -EINVAL;

	if (!umem)
		return -EOPNOTSUPP;

	return mlx5_ib_read_user_wqe_rq(qp, wqe_index, buffer, buflen, bc);
}

static int mlx5_ib_read_user_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index,
				     void *buffer, size_t buflen, size_t *bc)
273 274 275 276
{
	struct ib_umem *umem = srq->umem;
	size_t bytes_copied;
	int ret;
277

278 279 280
	ret = mlx5_ib_read_user_wqe_common(umem, buffer, buflen, wqe_index, 0,
					   srq->msrq.max, srq->msrq.wqe_shift,
					   buflen, &bytes_copied);
281 282 283 284 285

	if (ret)
		return ret;
	*bc = bytes_copied;
	return 0;
286 287
}

288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
int mlx5_ib_read_wqe_srq(struct mlx5_ib_srq *srq, int wqe_index, void *buffer,
			 size_t buflen, size_t *bc)
{
	struct ib_umem *umem = srq->umem;
	size_t wqe_size = 1 << srq->msrq.wqe_shift;

	if (buflen < wqe_size)
		return -EINVAL;

	if (!umem)
		return -EOPNOTSUPP;

	return mlx5_ib_read_user_wqe_srq(srq, wqe_index, buffer, buflen, bc);
}

303 304 305 306 307
static void mlx5_ib_qp_event(struct mlx5_core_qp *qp, int type)
{
	struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
	struct ib_event event;

308 309 310 311
	if (type == MLX5_EVENT_TYPE_PATH_MIG) {
		/* This event is only valid for trans_qps */
		to_mibqp(qp)->port = to_mibqp(qp)->trans_qp.alt_port;
	}
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356

	if (ibqp->event_handler) {
		event.device     = ibqp->device;
		event.element.qp = ibqp;
		switch (type) {
		case MLX5_EVENT_TYPE_PATH_MIG:
			event.event = IB_EVENT_PATH_MIG;
			break;
		case MLX5_EVENT_TYPE_COMM_EST:
			event.event = IB_EVENT_COMM_EST;
			break;
		case MLX5_EVENT_TYPE_SQ_DRAINED:
			event.event = IB_EVENT_SQ_DRAINED;
			break;
		case MLX5_EVENT_TYPE_SRQ_LAST_WQE:
			event.event = IB_EVENT_QP_LAST_WQE_REACHED;
			break;
		case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
			event.event = IB_EVENT_QP_FATAL;
			break;
		case MLX5_EVENT_TYPE_PATH_MIG_FAILED:
			event.event = IB_EVENT_PATH_MIG_ERR;
			break;
		case MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
			event.event = IB_EVENT_QP_REQ_ERR;
			break;
		case MLX5_EVENT_TYPE_WQ_ACCESS_ERROR:
			event.event = IB_EVENT_QP_ACCESS_ERR;
			break;
		default:
			pr_warn("mlx5_ib: Unexpected event type %d on QP %06x\n", type, qp->qpn);
			return;
		}

		ibqp->event_handler(&event, ibqp->qp_context);
	}
}

static int set_rq_size(struct mlx5_ib_dev *dev, struct ib_qp_cap *cap,
		       int has_rq, struct mlx5_ib_qp *qp, struct mlx5_ib_create_qp *ucmd)
{
	int wqe_size;
	int wq_size;

	/* Sanity check RQ size before proceeding */
357
	if (cap->max_recv_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz)))
358 359 360 361 362 363
		return -EINVAL;

	if (!has_rq) {
		qp->rq.max_gs = 0;
		qp->rq.wqe_cnt = 0;
		qp->rq.wqe_shift = 0;
364 365
		cap->max_recv_wr = 0;
		cap->max_recv_sge = 0;
366
	} else {
367 368
		int wq_sig = !!(qp->flags_en & MLX5_QP_FLAG_SIGNATURE);

369 370
		if (ucmd) {
			qp->rq.wqe_cnt = ucmd->rq_wqe_count;
371 372
			if (ucmd->rq_wqe_shift > BITS_PER_BYTE * sizeof(ucmd->rq_wqe_shift))
				return -EINVAL;
373
			qp->rq.wqe_shift = ucmd->rq_wqe_shift;
374 375 376
			if ((1 << qp->rq.wqe_shift) /
				    sizeof(struct mlx5_wqe_data_seg) <
			    wq_sig)
377
				return -EINVAL;
378 379 380 381
			qp->rq.max_gs =
				(1 << qp->rq.wqe_shift) /
					sizeof(struct mlx5_wqe_data_seg) -
				wq_sig;
382 383
			qp->rq.max_post = qp->rq.wqe_cnt;
		} else {
384 385 386
			wqe_size =
				wq_sig ? sizeof(struct mlx5_wqe_signature_seg) :
					 0;
387 388 389 390 391
			wqe_size += cap->max_recv_sge * sizeof(struct mlx5_wqe_data_seg);
			wqe_size = roundup_pow_of_two(wqe_size);
			wq_size = roundup_pow_of_two(cap->max_recv_wr) * wqe_size;
			wq_size = max_t(int, wq_size, MLX5_SEND_WQE_BB);
			qp->rq.wqe_cnt = wq_size / wqe_size;
392
			if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq)) {
393 394
				mlx5_ib_dbg(dev, "wqe_size %d, max %d\n",
					    wqe_size,
395 396
					    MLX5_CAP_GEN(dev->mdev,
							 max_wqe_sz_rq));
397 398 399
				return -EINVAL;
			}
			qp->rq.wqe_shift = ilog2(wqe_size);
400 401 402 403
			qp->rq.max_gs =
				(1 << qp->rq.wqe_shift) /
					sizeof(struct mlx5_wqe_data_seg) -
				wq_sig;
404 405 406 407 408 409 410
			qp->rq.max_post = qp->rq.wqe_cnt;
		}
	}

	return 0;
}

411
static int sq_overhead(struct ib_qp_init_attr *attr)
412
{
413
	int size = 0;
414

415
	switch (attr->qp_type) {
416
	case IB_QPT_XRC_INI:
417
		size += sizeof(struct mlx5_wqe_xrc_seg);
418 419 420
		/* fall through */
	case IB_QPT_RC:
		size += sizeof(struct mlx5_wqe_ctrl_seg) +
421 422 423
			max(sizeof(struct mlx5_wqe_atomic_seg) +
			    sizeof(struct mlx5_wqe_raddr_seg),
			    sizeof(struct mlx5_wqe_umr_ctrl_seg) +
424 425 426
			    sizeof(struct mlx5_mkey_seg) +
			    MLX5_IB_SQ_UMR_INLINE_THRESHOLD /
			    MLX5_IB_UMR_OCTOWORD);
427 428
		break;

429 430 431
	case IB_QPT_XRC_TGT:
		return 0;

432
	case IB_QPT_UC:
433
		size += sizeof(struct mlx5_wqe_ctrl_seg) +
434 435 436
			max(sizeof(struct mlx5_wqe_raddr_seg),
			    sizeof(struct mlx5_wqe_umr_ctrl_seg) +
			    sizeof(struct mlx5_mkey_seg));
437 438 439
		break;

	case IB_QPT_UD:
440 441 442 443
		if (attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
			size += sizeof(struct mlx5_wqe_eth_pad) +
				sizeof(struct mlx5_wqe_eth_seg);
		/* fall through */
444
	case IB_QPT_SMI:
H
Haggai Eran 已提交
445
	case MLX5_IB_QPT_HW_GSI:
446
		size += sizeof(struct mlx5_wqe_ctrl_seg) +
447 448 449 450
			sizeof(struct mlx5_wqe_datagram_seg);
		break;

	case MLX5_IB_QPT_REG_UMR:
451
		size += sizeof(struct mlx5_wqe_ctrl_seg) +
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
			sizeof(struct mlx5_wqe_umr_ctrl_seg) +
			sizeof(struct mlx5_mkey_seg);
		break;

	default:
		return -EINVAL;
	}

	return size;
}

static int calc_send_wqe(struct ib_qp_init_attr *attr)
{
	int inl_size = 0;
	int size;

468
	size = sq_overhead(attr);
469 470 471 472 473 474 475 476 477
	if (size < 0)
		return size;

	if (attr->cap.max_inline_data) {
		inl_size = size + sizeof(struct mlx5_wqe_inline_seg) +
			attr->cap.max_inline_data;
	}

	size += attr->cap.max_send_sge * sizeof(struct mlx5_wqe_data_seg);
478
	if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN &&
479
	    ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB) < MLX5_SIG_WQE_SIZE)
480
		return MLX5_SIG_WQE_SIZE;
481 482
	else
		return ALIGN(max_t(int, inl_size, size), MLX5_SEND_WQE_BB);
483 484
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
static int get_send_sge(struct ib_qp_init_attr *attr, int wqe_size)
{
	int max_sge;

	if (attr->qp_type == IB_QPT_RC)
		max_sge = (min_t(int, wqe_size, 512) -
			   sizeof(struct mlx5_wqe_ctrl_seg) -
			   sizeof(struct mlx5_wqe_raddr_seg)) /
			sizeof(struct mlx5_wqe_data_seg);
	else if (attr->qp_type == IB_QPT_XRC_INI)
		max_sge = (min_t(int, wqe_size, 512) -
			   sizeof(struct mlx5_wqe_ctrl_seg) -
			   sizeof(struct mlx5_wqe_xrc_seg) -
			   sizeof(struct mlx5_wqe_raddr_seg)) /
			sizeof(struct mlx5_wqe_data_seg);
	else
		max_sge = (wqe_size - sq_overhead(attr)) /
			sizeof(struct mlx5_wqe_data_seg);

	return min_t(int, max_sge, wqe_size - sq_overhead(attr) /
		     sizeof(struct mlx5_wqe_data_seg));
}

508 509 510 511 512 513 514 515 516 517 518 519 520 521
static int calc_sq_size(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
			struct mlx5_ib_qp *qp)
{
	int wqe_size;
	int wq_size;

	if (!attr->cap.max_send_wr)
		return 0;

	wqe_size = calc_send_wqe(attr);
	mlx5_ib_dbg(dev, "wqe_size %d\n", wqe_size);
	if (wqe_size < 0)
		return wqe_size;

522
	if (wqe_size > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
523
		mlx5_ib_dbg(dev, "wqe_size(%d) > max_sq_desc_sz(%d)\n",
524
			    wqe_size, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
525 526 527
		return -EINVAL;
	}

528 529
	qp->max_inline_data = wqe_size - sq_overhead(attr) -
			      sizeof(struct mlx5_wqe_inline_seg);
530 531 532 533
	attr->cap.max_inline_data = qp->max_inline_data;

	wq_size = roundup_pow_of_two(attr->cap.max_send_wr * wqe_size);
	qp->sq.wqe_cnt = wq_size / MLX5_SEND_WQE_BB;
534
	if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
535 536
		mlx5_ib_dbg(dev, "send queue size (%d * %d / %d -> %d) exceeds limits(%d)\n",
			    attr->cap.max_send_wr, wqe_size, MLX5_SEND_WQE_BB,
537 538
			    qp->sq.wqe_cnt,
			    1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
539 540
		return -ENOMEM;
	}
541
	qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
542 543 544 545 546
	qp->sq.max_gs = get_send_sge(attr, wqe_size);
	if (qp->sq.max_gs < attr->cap.max_send_sge)
		return -ENOMEM;

	attr->cap.max_send_sge = qp->sq.max_gs;
547 548
	qp->sq.max_post = wq_size / wqe_size;
	attr->cap.max_send_wr = qp->sq.max_post;
549 550 551 552 553 554

	return wq_size;
}

static int set_user_buf_size(struct mlx5_ib_dev *dev,
			    struct mlx5_ib_qp *qp,
555
			    struct mlx5_ib_create_qp *ucmd,
556 557
			    struct mlx5_ib_qp_base *base,
			    struct ib_qp_init_attr *attr)
558 559 560
{
	int desc_sz = 1 << qp->sq.wqe_shift;

561
	if (desc_sz > MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq)) {
562
		mlx5_ib_warn(dev, "desc_sz %d, max_sq_desc_sz %d\n",
563
			     desc_sz, MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq));
564 565 566
		return -EINVAL;
	}

567 568 569
	if (ucmd->sq_wqe_count && !is_power_of_2(ucmd->sq_wqe_count)) {
		mlx5_ib_warn(dev, "sq_wqe_count %d is not a power of two\n",
			     ucmd->sq_wqe_count);
570 571 572 573 574
		return -EINVAL;
	}

	qp->sq.wqe_cnt = ucmd->sq_wqe_count;

575
	if (qp->sq.wqe_cnt > (1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz))) {
576
		mlx5_ib_warn(dev, "wqe_cnt %d, max_wqes %d\n",
577 578
			     qp->sq.wqe_cnt,
			     1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz));
579 580 581
		return -EINVAL;
	}

582
	if (attr->qp_type == IB_QPT_RAW_PACKET ||
583
	    qp->flags & IB_QP_CREATE_SOURCE_QPN) {
584 585 586 587 588 589
		base->ubuffer.buf_size = qp->rq.wqe_cnt << qp->rq.wqe_shift;
		qp->raw_packet_qp.sq.ubuffer.buf_size = qp->sq.wqe_cnt << 6;
	} else {
		base->ubuffer.buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
					 (qp->sq.wqe_cnt << 6);
	}
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604

	return 0;
}

static int qp_has_rq(struct ib_qp_init_attr *attr)
{
	if (attr->qp_type == IB_QPT_XRC_INI ||
	    attr->qp_type == IB_QPT_XRC_TGT || attr->srq ||
	    attr->qp_type == MLX5_IB_QPT_REG_UMR ||
	    !attr->cap.max_recv_wr)
		return 0;

	return 1;
}

605 606 607 608 609 610 611 612 613
enum {
	/* this is the first blue flame register in the array of bfregs assigned
	 * to a processes. Since we do not use it for blue flame but rather
	 * regular 64 bit doorbells, we do not need a lock for maintaiing
	 * "odd/even" order
	 */
	NUM_NON_BLUE_FLAME_BFREGS = 1,
};

614 615
static int max_bfregs(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi)
{
616
	return get_num_static_uars(dev, bfregi) * MLX5_NON_FP_BFREGS_PER_UAR;
617 618 619 620
}

static int num_med_bfreg(struct mlx5_ib_dev *dev,
			 struct mlx5_bfreg_info *bfregi)
E
Eli Cohen 已提交
621 622 623
{
	int n;

624 625
	n = max_bfregs(dev, bfregi) - bfregi->num_low_latency_bfregs -
	    NUM_NON_BLUE_FLAME_BFREGS;
E
Eli Cohen 已提交
626 627 628 629

	return n >= 0 ? n : 0;
}

630 631 632 633 634 635
static int first_med_bfreg(struct mlx5_ib_dev *dev,
			   struct mlx5_bfreg_info *bfregi)
{
	return num_med_bfreg(dev, bfregi) ? 1 : -ENOMEM;
}

636 637
static int first_hi_bfreg(struct mlx5_ib_dev *dev,
			  struct mlx5_bfreg_info *bfregi)
E
Eli Cohen 已提交
638 639 640
{
	int med;

641 642
	med = num_med_bfreg(dev, bfregi);
	return ++med;
E
Eli Cohen 已提交
643 644
}

645 646
static int alloc_high_class_bfreg(struct mlx5_ib_dev *dev,
				  struct mlx5_bfreg_info *bfregi)
647 648 649
{
	int i;

650 651
	for (i = first_hi_bfreg(dev, bfregi); i < max_bfregs(dev, bfregi); i++) {
		if (!bfregi->count[i]) {
652
			bfregi->count[i]++;
653 654 655 656 657 658 659
			return i;
		}
	}

	return -ENOMEM;
}

660 661
static int alloc_med_class_bfreg(struct mlx5_ib_dev *dev,
				 struct mlx5_bfreg_info *bfregi)
662
{
663
	int minidx = first_med_bfreg(dev, bfregi);
664 665
	int i;

666 667 668 669
	if (minidx < 0)
		return minidx;

	for (i = minidx; i < first_hi_bfreg(dev, bfregi); i++) {
670
		if (bfregi->count[i] < bfregi->count[minidx])
671
			minidx = i;
672 673
		if (!bfregi->count[minidx])
			break;
674 675
	}

676
	bfregi->count[minidx]++;
677 678 679
	return minidx;
}

680
static int alloc_bfreg(struct mlx5_ib_dev *dev,
681
		       struct mlx5_bfreg_info *bfregi)
682
{
683
	int bfregn = -ENOMEM;
684

685 686 687
	if (bfregi->lib_uar_dyn)
		return -EINVAL;

688
	mutex_lock(&bfregi->lock);
689 690 691 692 693 694 695
	if (bfregi->ver >= 2) {
		bfregn = alloc_high_class_bfreg(dev, bfregi);
		if (bfregn < 0)
			bfregn = alloc_med_class_bfreg(dev, bfregi);
	}

	if (bfregn < 0) {
696
		BUILD_BUG_ON(NUM_NON_BLUE_FLAME_BFREGS != 1);
697 698
		bfregn = 0;
		bfregi->count[bfregn]++;
699
	}
700
	mutex_unlock(&bfregi->lock);
701

702
	return bfregn;
703 704
}

705
void mlx5_ib_free_bfreg(struct mlx5_ib_dev *dev, struct mlx5_bfreg_info *bfregi, int bfregn)
706
{
707
	mutex_lock(&bfregi->lock);
708
	bfregi->count[bfregn]--;
709
	mutex_unlock(&bfregi->lock);
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
}

static enum mlx5_qp_state to_mlx5_state(enum ib_qp_state state)
{
	switch (state) {
	case IB_QPS_RESET:	return MLX5_QP_STATE_RST;
	case IB_QPS_INIT:	return MLX5_QP_STATE_INIT;
	case IB_QPS_RTR:	return MLX5_QP_STATE_RTR;
	case IB_QPS_RTS:	return MLX5_QP_STATE_RTS;
	case IB_QPS_SQD:	return MLX5_QP_STATE_SQD;
	case IB_QPS_SQE:	return MLX5_QP_STATE_SQER;
	case IB_QPS_ERR:	return MLX5_QP_STATE_ERR;
	default:		return -1;
	}
}

static int to_mlx5_st(enum ib_qp_type type)
{
	switch (type) {
	case IB_QPT_RC:			return MLX5_QP_ST_RC;
	case IB_QPT_UC:			return MLX5_QP_ST_UC;
	case IB_QPT_UD:			return MLX5_QP_ST_UD;
	case MLX5_IB_QPT_REG_UMR:	return MLX5_QP_ST_REG_UMR;
	case IB_QPT_XRC_INI:
	case IB_QPT_XRC_TGT:		return MLX5_QP_ST_XRC;
	case IB_QPT_SMI:		return MLX5_QP_ST_QP0;
H
Haggai Eran 已提交
736
	case MLX5_IB_QPT_HW_GSI:	return MLX5_QP_ST_QP1;
737
	case MLX5_IB_QPT_DCI:		return MLX5_QP_ST_DCI;
738
	case IB_QPT_RAW_PACKET:		return MLX5_QP_ST_RAW_ETHERTYPE;
739 740 741 742
	default:		return -EINVAL;
	}
}

743 744 745 746 747
static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq,
			     struct mlx5_ib_cq *recv_cq);
static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq,
			       struct mlx5_ib_cq *recv_cq);

748
int bfregn_to_uar_index(struct mlx5_ib_dev *dev,
749
			struct mlx5_bfreg_info *bfregi, u32 bfregn,
750
			bool dyn_bfreg)
751
{
752 753 754
	unsigned int bfregs_per_sys_page;
	u32 index_of_sys_page;
	u32 offset;
755

756 757 758
	if (bfregi->lib_uar_dyn)
		return -EINVAL;

759 760 761 762
	bfregs_per_sys_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k) *
				MLX5_NON_FP_BFREGS_PER_UAR;
	index_of_sys_page = bfregn / bfregs_per_sys_page;

763 764
	if (dyn_bfreg) {
		index_of_sys_page += bfregi->num_static_sys_pages;
765 766 767 768

		if (index_of_sys_page >= bfregi->num_sys_pages)
			return -EINVAL;

769 770 771 772 773 774
		if (bfregn > bfregi->num_dyn_bfregs ||
		    bfregi->sys_pages[index_of_sys_page] == MLX5_IB_INVALID_UAR_INDEX) {
			mlx5_ib_dbg(dev, "Invalid dynamic uar index\n");
			return -EINVAL;
		}
	}
775

776
	offset = bfregn % bfregs_per_sys_page / MLX5_NON_FP_BFREGS_PER_UAR;
777
	return bfregi->sys_pages[index_of_sys_page] + offset;
778 779
}

780
static int mlx5_ib_umem_get(struct mlx5_ib_dev *dev, struct ib_udata *udata,
781
			    unsigned long addr, size_t size,
782 783
			    struct ib_umem **umem, int *npages, int *page_shift,
			    int *ncont, u32 *offset)
784 785 786
{
	int err;

787
	*umem = ib_umem_get(&dev->ib_dev, addr, size, 0);
788 789 790 791 792
	if (IS_ERR(*umem)) {
		mlx5_ib_dbg(dev, "umem_get failed\n");
		return PTR_ERR(*umem);
	}

793
	mlx5_ib_cont_pages(*umem, addr, 0, npages, page_shift, ncont, NULL);
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812

	err = mlx5_ib_get_buf_offset(addr, *page_shift, offset);
	if (err) {
		mlx5_ib_warn(dev, "bad offset\n");
		goto err_umem;
	}

	mlx5_ib_dbg(dev, "addr 0x%lx, size %zu, npages %d, page_shift %d, ncont %d, offset %d\n",
		    addr, size, *npages, *page_shift, *ncont, *offset);

	return 0;

err_umem:
	ib_umem_release(*umem);
	*umem = NULL;

	return err;
}

813
static void destroy_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
814
			    struct mlx5_ib_rwq *rwq, struct ib_udata *udata)
815
{
816 817 818 819 820
	struct mlx5_ib_ucontext *context =
		rdma_udata_to_drv_context(
			udata,
			struct mlx5_ib_ucontext,
			ibucontext);
821

822 823 824
	if (rwq->create_flags & MLX5_IB_WQ_FLAGS_DELAY_DROP)
		atomic_dec(&dev->delay_drop.rqs_cnt);

825
	mlx5_ib_db_unmap_user(context, &rwq->db);
826
	ib_umem_release(rwq->umem);
827 828 829
}

static int create_user_rq(struct mlx5_ib_dev *dev, struct ib_pd *pd,
830
			  struct ib_udata *udata, struct mlx5_ib_rwq *rwq,
831 832
			  struct mlx5_ib_create_wq *ucmd)
{
833 834
	struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
		udata, struct mlx5_ib_ucontext, ibucontext);
835 836 837 838 839 840 841 842 843
	int page_shift = 0;
	int npages;
	u32 offset = 0;
	int ncont = 0;
	int err;

	if (!ucmd->buf_addr)
		return -EINVAL;

844
	rwq->umem = ib_umem_get(&dev->ib_dev, ucmd->buf_addr, rwq->buf_size, 0);
845 846 847 848 849 850
	if (IS_ERR(rwq->umem)) {
		mlx5_ib_dbg(dev, "umem_get failed\n");
		err = PTR_ERR(rwq->umem);
		return err;
	}

851
	mlx5_ib_cont_pages(rwq->umem, ucmd->buf_addr, 0, &npages, &page_shift,
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
			   &ncont, NULL);
	err = mlx5_ib_get_buf_offset(ucmd->buf_addr, page_shift,
				     &rwq->rq_page_offset);
	if (err) {
		mlx5_ib_warn(dev, "bad offset\n");
		goto err_umem;
	}

	rwq->rq_num_pas = ncont;
	rwq->page_shift = page_shift;
	rwq->log_page_size =  page_shift - MLX5_ADAPTER_PAGE_SHIFT;
	rwq->wq_sig = !!(ucmd->flags & MLX5_WQ_FLAG_SIGNATURE);

	mlx5_ib_dbg(dev, "addr 0x%llx, size %zd, npages %d, page_shift %d, ncont %d, offset %d\n",
		    (unsigned long long)ucmd->buf_addr, rwq->buf_size,
		    npages, page_shift, ncont, offset);

869
	err = mlx5_ib_db_map_user(ucontext, udata, ucmd->db_addr, &rwq->db);
870 871 872 873 874 875 876 877 878 879 880 881
	if (err) {
		mlx5_ib_dbg(dev, "map failed\n");
		goto err_umem;
	}

	return 0;

err_umem:
	ib_umem_release(rwq->umem);
	return err;
}

882 883 884 885 886 887 888
static int adjust_bfregn(struct mlx5_ib_dev *dev,
			 struct mlx5_bfreg_info *bfregi, int bfregn)
{
	return bfregn / MLX5_NON_FP_BFREGS_PER_UAR * MLX5_BFREGS_PER_UAR +
				bfregn % MLX5_NON_FP_BFREGS_PER_UAR;
}

889 890 891 892 893 894
static int _create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
			   struct mlx5_ib_qp *qp, struct ib_udata *udata,
			   struct ib_qp_init_attr *attr, u32 **in,
			   struct mlx5_ib_create_qp_resp *resp, int *inlen,
			   struct mlx5_ib_qp_base *base,
			   struct mlx5_ib_create_qp *ucmd)
895 896
{
	struct mlx5_ib_ucontext *context;
897
	struct mlx5_ib_ubuffer *ubuffer = &base->ubuffer;
898
	int page_shift = 0;
899
	int uar_index = 0;
900
	int npages;
901
	u32 offset = 0;
902
	int bfregn;
903
	int ncont = 0;
904 905
	__be64 *pas;
	void *qpc;
906
	int err;
907
	u16 uid;
908
	u32 uar_flags;
909

910 911
	context = rdma_udata_to_drv_context(udata, struct mlx5_ib_ucontext,
					    ibucontext);
912 913
	uar_flags = qp->flags_en &
		    (MLX5_QP_FLAG_UAR_PAGE_INDEX | MLX5_QP_FLAG_BFREG_INDEX);
914 915
	switch (uar_flags) {
	case MLX5_QP_FLAG_UAR_PAGE_INDEX:
916
		uar_index = ucmd->bfreg_index;
917 918 919
		bfregn = MLX5_IB_INVALID_BFREG;
		break;
	case MLX5_QP_FLAG_BFREG_INDEX:
920
		uar_index = bfregn_to_uar_index(dev, &context->bfregi,
921
						ucmd->bfreg_index, true);
922 923 924
		if (uar_index < 0)
			return uar_index;
		bfregn = MLX5_IB_INVALID_BFREG;
925 926
		break;
	case 0:
927
		if (qp->flags & IB_QP_CREATE_CROSS_CHANNEL)
928
			return -EINVAL;
929 930 931
		bfregn = alloc_bfreg(dev, &context->bfregi);
		if (bfregn < 0)
			return bfregn;
932 933 934
		break;
	default:
		return -EINVAL;
935 936
	}

937
	mlx5_ib_dbg(dev, "bfregn 0x%x, uar_index 0x%x\n", bfregn, uar_index);
938 939 940
	if (bfregn != MLX5_IB_INVALID_BFREG)
		uar_index = bfregn_to_uar_index(dev, &context->bfregi, bfregn,
						false);
941

942 943 944 945
	qp->rq.offset = 0;
	qp->sq.wqe_shift = ilog2(MLX5_SEND_WQE_BB);
	qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;

946
	err = set_user_buf_size(dev, qp, ucmd, base, attr);
947
	if (err)
948
		goto err_bfreg;
949

950 951
	if (ucmd->buf_addr && ubuffer->buf_size) {
		ubuffer->buf_addr = ucmd->buf_addr;
952 953 954
		err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr,
				       ubuffer->buf_size, &ubuffer->umem,
				       &npages, &page_shift, &ncont, &offset);
955
		if (err)
956
			goto err_bfreg;
957
	} else {
958
		ubuffer->umem = NULL;
959 960
	}

961 962
	*inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
		 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * ncont;
963
	*in = kvzalloc(*inlen, GFP_KERNEL);
964 965 966 967
	if (!*in) {
		err = -ENOMEM;
		goto err_umem;
	}
968

969
	uid = (attr->qp_type != IB_QPT_XRC_INI) ? to_mpd(pd)->uid : 0;
970
	MLX5_SET(create_qp_in, *in, uid, uid);
971
	pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, *in, pas);
972
	if (ubuffer->umem)
973 974 975 976 977 978
		mlx5_ib_populate_pas(dev, ubuffer->umem, page_shift, pas, 0);

	qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);

	MLX5_SET(qpc, qpc, log_page_size, page_shift - MLX5_ADAPTER_PAGE_SHIFT);
	MLX5_SET(qpc, qpc, page_offset, offset);
979

980
	MLX5_SET(qpc, qpc, uar_page, uar_index);
981 982 983 984
	if (bfregn != MLX5_IB_INVALID_BFREG)
		resp->bfreg_index = adjust_bfregn(dev, &context->bfregi, bfregn);
	else
		resp->bfreg_index = MLX5_IB_INVALID_BFREG;
985
	qp->bfregn = bfregn;
986

987
	err = mlx5_ib_db_map_user(context, udata, ucmd->db_addr, &qp->db);
988 989 990 991 992 993 994 995
	if (err) {
		mlx5_ib_dbg(dev, "map failed\n");
		goto err_free;
	}

	return 0;

err_free:
A
Al Viro 已提交
996
	kvfree(*in);
997 998

err_umem:
999
	ib_umem_release(ubuffer->umem);
1000

1001
err_bfreg:
1002 1003
	if (bfregn != MLX5_IB_INVALID_BFREG)
		mlx5_ib_free_bfreg(dev, &context->bfregi, bfregn);
1004 1005 1006
	return err;
}

1007 1008
static void destroy_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
		       struct mlx5_ib_qp_base *base, struct ib_udata *udata)
1009
{
1010 1011
	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
		udata, struct mlx5_ib_ucontext, ibucontext);
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
	if (udata) {
		/* User QP */
		mlx5_ib_db_unmap_user(context, &qp->db);
		ib_umem_release(base->ubuffer.umem);

		/*
		 * Free only the BFREGs which are handled by the kernel.
		 * BFREGs of UARs allocated dynamically are handled by user.
		 */
		if (qp->bfregn != MLX5_IB_INVALID_BFREG)
			mlx5_ib_free_bfreg(dev, &context->bfregi, qp->bfregn);
		return;
	}
1026

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	/* Kernel QP */
	kvfree(qp->sq.wqe_head);
	kvfree(qp->sq.w_list);
	kvfree(qp->sq.wrid);
	kvfree(qp->sq.wr_data);
	kvfree(qp->rq.wrid);
	if (qp->db.db)
		mlx5_db_free(dev->mdev, &qp->db);
	if (qp->buf.frags)
		mlx5_frag_buf_free(dev->mdev, &qp->buf);
1037 1038
}

1039 1040 1041 1042
static int _create_kernel_qp(struct mlx5_ib_dev *dev,
			     struct ib_qp_init_attr *init_attr,
			     struct mlx5_ib_qp *qp, u32 **in, int *inlen,
			     struct mlx5_ib_qp_base *base)
1043 1044
{
	int uar_index;
1045
	void *qpc;
1046 1047 1048
	int err;

	if (init_attr->qp_type == MLX5_IB_QPT_REG_UMR)
1049
		qp->bf.bfreg = &dev->fp_bfreg;
1050
	else if (qp->flags & MLX5_IB_QP_CREATE_WC_TEST)
1051
		qp->bf.bfreg = &dev->wc_bfreg;
1052 1053
	else
		qp->bf.bfreg = &dev->bfreg;
1054

1055 1056 1057 1058
	/* We need to divide by two since each register is comprised of
	 * two buffers of identical size, namely odd and even
	 */
	qp->bf.buf_size = (1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size)) / 2;
1059
	uar_index = qp->bf.bfreg->index;
1060 1061 1062 1063

	err = calc_sq_size(dev, init_attr, qp);
	if (err < 0) {
		mlx5_ib_dbg(dev, "err %d\n", err);
1064
		return err;
1065 1066 1067 1068
	}

	qp->rq.offset = 0;
	qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
1069
	base->ubuffer.buf_size = err + (qp->rq.wqe_cnt << qp->rq.wqe_shift);
1070

1071 1072
	err = mlx5_frag_buf_alloc_node(dev->mdev, base->ubuffer.buf_size,
				       &qp->buf, dev->mdev->priv.numa_node);
1073 1074
	if (err) {
		mlx5_ib_dbg(dev, "err %d\n", err);
1075
		return err;
1076 1077
	}

1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	if (qp->rq.wqe_cnt)
		mlx5_init_fbc(qp->buf.frags, qp->rq.wqe_shift,
			      ilog2(qp->rq.wqe_cnt), &qp->rq.fbc);

	if (qp->sq.wqe_cnt) {
		int sq_strides_offset = (qp->sq.offset  & (PAGE_SIZE - 1)) /
					MLX5_SEND_WQE_BB;
		mlx5_init_fbc_offset(qp->buf.frags +
				     (qp->sq.offset / PAGE_SIZE),
				     ilog2(MLX5_SEND_WQE_BB),
				     ilog2(qp->sq.wqe_cnt),
				     sq_strides_offset, &qp->sq.fbc);

		qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
	}

1094 1095
	*inlen = MLX5_ST_SZ_BYTES(create_qp_in) +
		 MLX5_FLD_SZ_BYTES(create_qp_in, pas[0]) * qp->buf.npages;
1096
	*in = kvzalloc(*inlen, GFP_KERNEL);
1097 1098 1099 1100
	if (!*in) {
		err = -ENOMEM;
		goto err_buf;
	}
1101 1102 1103 1104 1105

	qpc = MLX5_ADDR_OF(create_qp_in, *in, qpc);
	MLX5_SET(qpc, qpc, uar_page, uar_index);
	MLX5_SET(qpc, qpc, log_page_size, qp->buf.page_shift - MLX5_ADAPTER_PAGE_SHIFT);

1106
	/* Set "fast registration enabled" for all kernel QPs */
1107 1108
	MLX5_SET(qpc, qpc, fre, 1);
	MLX5_SET(qpc, qpc, rlky, 1);
1109

1110
	if (qp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
1111
		MLX5_SET(qpc, qpc, deth_sqpn, 1);
1112

1113 1114 1115
	mlx5_fill_page_frag_array(&qp->buf,
				  (__be64 *)MLX5_ADDR_OF(create_qp_in,
							 *in, pas));
1116

1117
	err = mlx5_db_alloc(dev->mdev, &qp->db);
1118 1119 1120 1121 1122
	if (err) {
		mlx5_ib_dbg(dev, "err %d\n", err);
		goto err_free;
	}

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
	qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
				     sizeof(*qp->sq.wrid), GFP_KERNEL);
	qp->sq.wr_data = kvmalloc_array(qp->sq.wqe_cnt,
					sizeof(*qp->sq.wr_data), GFP_KERNEL);
	qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
				     sizeof(*qp->rq.wrid), GFP_KERNEL);
	qp->sq.w_list = kvmalloc_array(qp->sq.wqe_cnt,
				       sizeof(*qp->sq.w_list), GFP_KERNEL);
	qp->sq.wqe_head = kvmalloc_array(qp->sq.wqe_cnt,
					 sizeof(*qp->sq.wqe_head), GFP_KERNEL);
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

	if (!qp->sq.wrid || !qp->sq.wr_data || !qp->rq.wrid ||
	    !qp->sq.w_list || !qp->sq.wqe_head) {
		err = -ENOMEM;
		goto err_wrid;
	}

	return 0;

err_wrid:
1143 1144 1145 1146 1147
	kvfree(qp->sq.wqe_head);
	kvfree(qp->sq.w_list);
	kvfree(qp->sq.wrid);
	kvfree(qp->sq.wr_data);
	kvfree(qp->rq.wrid);
1148
	mlx5_db_free(dev->mdev, &qp->db);
1149 1150

err_free:
A
Al Viro 已提交
1151
	kvfree(*in);
1152 1153

err_buf:
1154
	mlx5_frag_buf_free(dev->mdev, &qp->buf);
1155 1156 1157
	return err;
}

1158
static u32 get_rx_type(struct mlx5_ib_qp *qp, struct ib_qp_init_attr *attr)
1159
{
1160 1161
	if (attr->srq || (qp->type == IB_QPT_XRC_TGT) ||
	    (qp->type == MLX5_IB_QPT_DCI) || (qp->type == IB_QPT_XRC_INI))
1162
		return MLX5_SRQ_RQ;
1163
	else if (!qp->has_rq)
1164
		return MLX5_ZERO_LEN_RQ;
1165 1166

	return MLX5_NON_ZERO_RQ;
1167 1168
}

1169
static int create_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1170
				    struct mlx5_ib_qp *qp,
1171 1172
				    struct mlx5_ib_sq *sq, u32 tdn,
				    struct ib_pd *pd)
1173
{
1174
	u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {};
1175 1176
	void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);

1177
	MLX5_SET(create_tis_in, in, uid, to_mpd(pd)->uid);
1178
	MLX5_SET(tisc, tisc, transport_domain, tdn);
1179
	if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
1180 1181
		MLX5_SET(tisc, tisc, underlay_qpn, qp->underlay_qpn);

1182
	return mlx5_core_create_tis(dev->mdev, in, &sq->tisn);
1183 1184 1185
}

static void destroy_raw_packet_qp_tis(struct mlx5_ib_dev *dev,
1186
				      struct mlx5_ib_sq *sq, struct ib_pd *pd)
1187
{
1188
	mlx5_cmd_destroy_tis(dev->mdev, sq->tisn, to_mpd(pd)->uid);
1189 1190
}

1191
static void destroy_flow_rule_vport_sq(struct mlx5_ib_sq *sq)
1192 1193 1194
{
	if (sq->flow_rule)
		mlx5_del_flow_rules(sq->flow_rule);
1195
	sq->flow_rule = NULL;
1196 1197
}

1198
static int create_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
1199
				   struct ib_udata *udata,
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
				   struct mlx5_ib_sq *sq, void *qpin,
				   struct ib_pd *pd)
{
	struct mlx5_ib_ubuffer *ubuffer = &sq->ubuffer;
	__be64 *pas;
	void *in;
	void *sqc;
	void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
	void *wq;
	int inlen;
	int err;
	int page_shift = 0;
	int npages;
	int ncont = 0;
	u32 offset = 0;

1216 1217 1218
	err = mlx5_ib_umem_get(dev, udata, ubuffer->buf_addr, ubuffer->buf_size,
			       &sq->ubuffer.umem, &npages, &page_shift, &ncont,
			       &offset);
1219 1220 1221 1222
	if (err)
		return err;

	inlen = MLX5_ST_SZ_BYTES(create_sq_in) + sizeof(u64) * ncont;
1223
	in = kvzalloc(inlen, GFP_KERNEL);
1224 1225 1226 1227 1228
	if (!in) {
		err = -ENOMEM;
		goto err_umem;
	}

1229
	MLX5_SET(create_sq_in, in, uid, to_mpd(pd)->uid);
1230 1231
	sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
	MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1232 1233
	if (MLX5_CAP_ETH(dev->mdev, multi_pkt_send_wqe))
		MLX5_SET(sqc, sqc, allow_multi_pkt_send_wqe, 1);
1234 1235 1236 1237 1238
	MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
	MLX5_SET(sqc, sqc, user_index, MLX5_GET(qpc, qpc, user_index));
	MLX5_SET(sqc, sqc, cqn, MLX5_GET(qpc, qpc, cqn_snd));
	MLX5_SET(sqc, sqc, tis_lst_sz, 1);
	MLX5_SET(sqc, sqc, tis_num_0, sq->tisn);
1239 1240 1241
	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
	    MLX5_CAP_ETH(dev->mdev, swp))
		MLX5_SET(sqc, sqc, allow_swp, 1);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255

	wq = MLX5_ADDR_OF(sqc, sqc, wq);
	MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
	MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
	MLX5_SET(wq, wq, uar_page, MLX5_GET(qpc, qpc, uar_page));
	MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
	MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
	MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_sq_size));
	MLX5_SET(wq, wq, log_wq_pg_sz,  page_shift - MLX5_ADAPTER_PAGE_SHIFT);
	MLX5_SET(wq, wq, page_offset, offset);

	pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
	mlx5_ib_populate_pas(dev, sq->ubuffer.umem, page_shift, pas, 0);

1256
	err = mlx5_core_create_sq_tracked(dev, in, inlen, &sq->base.mqp);
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274

	kvfree(in);

	if (err)
		goto err_umem;

	return 0;

err_umem:
	ib_umem_release(sq->ubuffer.umem);
	sq->ubuffer.umem = NULL;

	return err;
}

static void destroy_raw_packet_qp_sq(struct mlx5_ib_dev *dev,
				     struct mlx5_ib_sq *sq)
{
1275
	destroy_flow_rule_vport_sq(sq);
1276
	mlx5_core_destroy_sq_tracked(dev, &sq->base.mqp);
1277 1278 1279
	ib_umem_release(sq->ubuffer.umem);
}

1280
static size_t get_rq_pas_size(void *qpc)
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
{
	u32 log_page_size = MLX5_GET(qpc, qpc, log_page_size) + 12;
	u32 log_rq_stride = MLX5_GET(qpc, qpc, log_rq_stride);
	u32 log_rq_size   = MLX5_GET(qpc, qpc, log_rq_size);
	u32 page_offset   = MLX5_GET(qpc, qpc, page_offset);
	u32 po_quanta	  = 1 << (log_page_size - 6);
	u32 rq_sz	  = 1 << (log_rq_size + 4 + log_rq_stride);
	u32 page_size	  = 1 << log_page_size;
	u32 rq_sz_po      = rq_sz + (page_offset * po_quanta);
	u32 rq_num_pas	  = (rq_sz_po + page_size - 1) / page_size;

	return rq_num_pas * sizeof(u64);
}

static int create_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
1296
				   struct mlx5_ib_rq *rq, void *qpin,
1297
				   size_t qpinlen, struct ib_pd *pd)
1298
{
1299
	struct mlx5_ib_qp *mqp = rq->base.container_mibqp;
1300 1301 1302 1303 1304 1305
	__be64 *pas;
	__be64 *qp_pas;
	void *in;
	void *rqc;
	void *wq;
	void *qpc = MLX5_ADDR_OF(create_qp_in, qpin, qpc);
1306 1307
	size_t rq_pas_size = get_rq_pas_size(qpc);
	size_t inlen;
1308
	int err;
1309 1310 1311

	if (qpinlen < rq_pas_size + MLX5_BYTE_OFF(create_qp_in, pas))
		return -EINVAL;
1312 1313

	inlen = MLX5_ST_SZ_BYTES(create_rq_in) + rq_pas_size;
1314
	in = kvzalloc(inlen, GFP_KERNEL);
1315 1316 1317
	if (!in)
		return -ENOMEM;

1318
	MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
1319
	rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
1320 1321
	if (!(rq->flags & MLX5_IB_RQ_CVLAN_STRIPPING))
		MLX5_SET(rqc, rqc, vsd, 1);
1322 1323 1324 1325 1326 1327
	MLX5_SET(rqc, rqc, mem_rq_type, MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
	MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
	MLX5_SET(rqc, rqc, flush_in_error_en, 1);
	MLX5_SET(rqc, rqc, user_index, MLX5_GET(qpc, qpc, user_index));
	MLX5_SET(rqc, rqc, cqn, MLX5_GET(qpc, qpc, cqn_rcv));

1328
	if (mqp->flags & IB_QP_CREATE_SCATTER_FCS)
1329 1330
		MLX5_SET(rqc, rqc, scatter_fcs, 1);

1331 1332
	wq = MLX5_ADDR_OF(rqc, rqc, wq);
	MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1333 1334
	if (rq->flags & MLX5_IB_RQ_PCI_WRITE_END_PADDING)
		MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345
	MLX5_SET(wq, wq, page_offset, MLX5_GET(qpc, qpc, page_offset));
	MLX5_SET(wq, wq, pd, MLX5_GET(qpc, qpc, pd));
	MLX5_SET64(wq, wq, dbr_addr, MLX5_GET64(qpc, qpc, dbr_addr));
	MLX5_SET(wq, wq, log_wq_stride, MLX5_GET(qpc, qpc, log_rq_stride) + 4);
	MLX5_SET(wq, wq, log_wq_pg_sz, MLX5_GET(qpc, qpc, log_page_size));
	MLX5_SET(wq, wq, log_wq_sz, MLX5_GET(qpc, qpc, log_rq_size));

	pas = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
	qp_pas = (__be64 *)MLX5_ADDR_OF(create_qp_in, qpin, pas);
	memcpy(pas, qp_pas, rq_pas_size);

1346
	err = mlx5_core_create_rq_tracked(dev, in, inlen, &rq->base.mqp);
1347 1348 1349 1350 1351 1352 1353 1354 1355

	kvfree(in);

	return err;
}

static void destroy_raw_packet_qp_rq(struct mlx5_ib_dev *dev,
				     struct mlx5_ib_rq *rq)
{
1356
	mlx5_core_destroy_rq_tracked(dev, &rq->base.mqp);
1357 1358
}

1359 1360
static void destroy_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
				      struct mlx5_ib_rq *rq,
1361 1362
				      u32 qp_flags_en,
				      struct ib_pd *pd)
1363 1364 1365 1366
{
	if (qp_flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
			   MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
		mlx5_ib_disable_lb(dev, false, true);
1367
	mlx5_cmd_destroy_tir(dev->mdev, rq->tirn, to_mpd(pd)->uid);
1368 1369
}

1370
static int create_raw_packet_qp_tir(struct mlx5_ib_dev *dev,
1371
				    struct mlx5_ib_rq *rq, u32 tdn,
1372 1373
				    u32 *qp_flags_en, struct ib_pd *pd,
				    u32 *out)
1374
{
1375
	u8 lb_flag = 0;
1376 1377 1378 1379 1380 1381
	u32 *in;
	void *tirc;
	int inlen;
	int err;

	inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1382
	in = kvzalloc(inlen, GFP_KERNEL);
1383 1384 1385
	if (!in)
		return -ENOMEM;

1386
	MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
1387 1388 1389 1390
	tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
	MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_DIRECT);
	MLX5_SET(tirc, tirc, inline_rqn, rq->base.mqp.qpn);
	MLX5_SET(tirc, tirc, transport_domain, tdn);
1391
	if (*qp_flags_en & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1392
		MLX5_SET(tirc, tirc, tunneled_offload_en, 1);
1393

1394 1395 1396 1397 1398 1399
	if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
		lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;

	if (*qp_flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
		lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;

1400
	if (dev->is_rep) {
1401 1402 1403 1404 1405
		lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;
		*qp_flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;
	}

	MLX5_SET(tirc, tirc, self_lb_block, lb_flag);
1406 1407
	MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
	err = mlx5_cmd_exec_inout(dev->mdev, create_tir, in, out);
1408
	rq->tirn = MLX5_GET(create_tir_out, out, tirn);
1409 1410 1411 1412
	if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
		err = mlx5_ib_enable_lb(dev, false, true);

		if (err)
1413
			destroy_raw_packet_qp_tir(dev, rq, 0, pd);
1414
	}
1415 1416 1417 1418 1419 1420
	kvfree(in);

	return err;
}

static int create_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
1421
				u32 *in, size_t inlen,
1422 1423 1424
				struct ib_pd *pd,
				struct ib_udata *udata,
				struct mlx5_ib_create_qp_resp *resp)
1425 1426 1427 1428
{
	struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
	struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
	struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
1429 1430
	struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
		udata, struct mlx5_ib_ucontext, ibucontext);
1431 1432
	int err;
	u32 tdn = mucontext->tdn;
1433
	u16 uid = to_mpd(pd)->uid;
1434
	u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {};
1435

1436 1437
	if (!qp->sq.wqe_cnt && !qp->rq.wqe_cnt)
		return -EINVAL;
1438
	if (qp->sq.wqe_cnt) {
1439
		err = create_raw_packet_qp_tis(dev, qp, sq, tdn, pd);
1440 1441 1442
		if (err)
			return err;

1443
		err = create_raw_packet_qp_sq(dev, udata, sq, in, pd);
1444 1445 1446
		if (err)
			goto err_destroy_tis;

1447 1448 1449 1450 1451 1452 1453
		if (uid) {
			resp->tisn = sq->tisn;
			resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TISN;
			resp->sqn = sq->base.mqp.qpn;
			resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_SQN;
		}

1454
		sq->base.container_mibqp = qp;
1455
		sq->base.mqp.event = mlx5_ib_qp_event;
1456 1457 1458
	}

	if (qp->rq.wqe_cnt) {
1459 1460
		rq->base.container_mibqp = qp;

1461
		if (qp->flags & IB_QP_CREATE_CVLAN_STRIPPING)
1462
			rq->flags |= MLX5_IB_RQ_CVLAN_STRIPPING;
1463
		if (qp->flags & IB_QP_CREATE_PCI_WRITE_END_PADDING)
1464
			rq->flags |= MLX5_IB_RQ_PCI_WRITE_END_PADDING;
1465
		err = create_raw_packet_qp_rq(dev, rq, in, inlen, pd);
1466 1467 1468
		if (err)
			goto err_destroy_sq;

1469 1470
		err = create_raw_packet_qp_tir(dev, rq, tdn, &qp->flags_en, pd,
					       out);
1471 1472
		if (err)
			goto err_destroy_rq;
1473 1474 1475 1476 1477 1478

		if (uid) {
			resp->rqn = rq->base.mqp.qpn;
			resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_RQN;
			resp->tirn = rq->tirn;
			resp->comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
			if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
				resp->tir_icm_addr = MLX5_GET(
					create_tir_out, out, icm_address_31_0);
				resp->tir_icm_addr |=
					(u64)MLX5_GET(create_tir_out, out,
						      icm_address_39_32)
					<< 32;
				resp->tir_icm_addr |=
					(u64)MLX5_GET(create_tir_out, out,
						      icm_address_63_40)
					<< 40;
				resp->comp_mask |=
					MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
			}
1493
		}
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
	}

	qp->trans_qp.base.mqp.qpn = qp->sq.wqe_cnt ? sq->base.mqp.qpn :
						     rq->base.mqp.qpn;
	return 0;

err_destroy_rq:
	destroy_raw_packet_qp_rq(dev, rq);
err_destroy_sq:
	if (!qp->sq.wqe_cnt)
		return err;
	destroy_raw_packet_qp_sq(dev, sq);
err_destroy_tis:
1507
	destroy_raw_packet_qp_tis(dev, sq, pd);
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519

	return err;
}

static void destroy_raw_packet_qp(struct mlx5_ib_dev *dev,
				  struct mlx5_ib_qp *qp)
{
	struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
	struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
	struct mlx5_ib_rq *rq = &raw_packet_qp->rq;

	if (qp->rq.wqe_cnt) {
1520
		destroy_raw_packet_qp_tir(dev, rq, qp->flags_en, qp->ibqp.pd);
1521 1522 1523 1524 1525
		destroy_raw_packet_qp_rq(dev, rq);
	}

	if (qp->sq.wqe_cnt) {
		destroy_raw_packet_qp_sq(dev, sq);
1526
		destroy_raw_packet_qp_tis(dev, sq, qp->ibqp.pd);
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
	}
}

static void raw_packet_qp_copy_info(struct mlx5_ib_qp *qp,
				    struct mlx5_ib_raw_packet_qp *raw_packet_qp)
{
	struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
	struct mlx5_ib_rq *rq = &raw_packet_qp->rq;

	sq->sq = &qp->sq;
	rq->rq = &qp->rq;
	sq->doorbell = &qp->db;
	rq->doorbell = &qp->db;
}

Y
Yishai Hadas 已提交
1542 1543
static void destroy_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp)
{
1544 1545 1546
	if (qp->flags_en & (MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
			    MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC))
		mlx5_ib_disable_lb(dev, false, true);
1547 1548
	mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
			     to_mpd(qp->ibqp.pd)->uid);
Y
Yishai Hadas 已提交
1549 1550
}

1551 1552 1553
struct mlx5_create_qp_params {
	struct ib_udata *udata;
	size_t inlen;
1554
	size_t outlen;
1555
	size_t ucmd_size;
1556 1557 1558 1559
	void *ucmd;
	u8 is_rss_raw : 1;
	struct ib_qp_init_attr *attr;
	u32 uidx;
1560
	struct mlx5_ib_create_qp_resp resp;
1561 1562 1563 1564 1565
};

static int create_rss_raw_qp_tir(struct mlx5_ib_dev *dev, struct ib_pd *pd,
				 struct mlx5_ib_qp *qp,
				 struct mlx5_create_qp_params *params)
Y
Yishai Hadas 已提交
1566
{
1567 1568 1569
	struct ib_qp_init_attr *init_attr = params->attr;
	struct mlx5_ib_create_qp_rss *ucmd = params->ucmd;
	struct ib_udata *udata = params->udata;
1570 1571
	struct mlx5_ib_ucontext *mucontext = rdma_udata_to_drv_context(
		udata, struct mlx5_ib_ucontext, ibucontext);
Y
Yishai Hadas 已提交
1572
	int inlen;
1573
	int outlen;
Y
Yishai Hadas 已提交
1574 1575
	int err;
	u32 *in;
1576
	u32 *out;
Y
Yishai Hadas 已提交
1577 1578 1579
	void *tirc;
	void *hfso;
	u32 selected_fields = 0;
1580
	u32 outer_l4;
Y
Yishai Hadas 已提交
1581
	u32 tdn = mucontext->tdn;
1582
	u8 lb_flag = 0;
Y
Yishai Hadas 已提交
1583

1584
	if (ucmd->comp_mask) {
Y
Yishai Hadas 已提交
1585 1586 1587 1588
		mlx5_ib_dbg(dev, "invalid comp mask\n");
		return -EOPNOTSUPP;
	}

1589 1590
	if (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_INNER &&
	    !(ucmd->flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)) {
1591 1592 1593 1594
		mlx5_ib_dbg(dev, "Tunnel offloads must be set for inner RSS\n");
		return -EOPNOTSUPP;
	}

1595
	if (dev->is_rep)
1596 1597
		qp->flags_en |= MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC;

1598 1599 1600 1601
	if (qp->flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC)
		lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_UNICAST;

	if (qp->flags_en & MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC)
1602 1603
		lb_flag |= MLX5_TIRC_SELF_LB_BLOCK_BLOCK_MULTICAST;

Y
Yishai Hadas 已提交
1604
	inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1605 1606
	outlen = MLX5_ST_SZ_BYTES(create_tir_out);
	in = kvzalloc(inlen + outlen, GFP_KERNEL);
Y
Yishai Hadas 已提交
1607 1608 1609
	if (!in)
		return -ENOMEM;

1610
	out = in + MLX5_ST_SZ_DW(create_tir_in);
1611
	MLX5_SET(create_tir_in, in, uid, to_mpd(pd)->uid);
Y
Yishai Hadas 已提交
1612 1613 1614 1615 1616 1617 1618 1619
	tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
	MLX5_SET(tirc, tirc, disp_type,
		 MLX5_TIRC_DISP_TYPE_INDIRECT);
	MLX5_SET(tirc, tirc, indirect_table,
		 init_attr->rwq_ind_tbl->ind_tbl_num);
	MLX5_SET(tirc, tirc, transport_domain, tdn);

	hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1620

1621
	if (ucmd->flags & MLX5_QP_FLAG_TUNNEL_OFFLOADS)
1622 1623
		MLX5_SET(tirc, tirc, tunneled_offload_en, 1);

1624 1625
	MLX5_SET(tirc, tirc, self_lb_block, lb_flag);

1626
	if (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_INNER)
1627 1628 1629 1630
		hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_inner);
	else
		hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);

1631
	switch (ucmd->rx_hash_function) {
Y
Yishai Hadas 已提交
1632 1633 1634 1635 1636
	case MLX5_RX_HASH_FUNC_TOEPLITZ:
	{
		void *rss_key = MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
		size_t len = MLX5_FLD_SZ_BYTES(tirc, rx_hash_toeplitz_key);

1637
		if (len != ucmd->rx_key_len) {
Y
Yishai Hadas 已提交
1638 1639 1640 1641 1642
			err = -EINVAL;
			goto err;
		}

		MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_TOEPLITZ);
1643
		memcpy(rss_key, ucmd->rx_hash_key, len);
Y
Yishai Hadas 已提交
1644 1645 1646 1647 1648 1649 1650
		break;
	}
	default:
		err = -EOPNOTSUPP;
		goto err;
	}

1651
	if (!ucmd->rx_hash_fields_mask) {
Y
Yishai Hadas 已提交
1652 1653 1654 1655 1656 1657 1658
		/* special case when this TIR serves as steering entry without hashing */
		if (!init_attr->rwq_ind_tbl->log_ind_tbl_size)
			goto create_tir;
		err = -EINVAL;
		goto err;
	}

1659 1660 1661 1662
	if (((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
	     (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4)) &&
	     ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
	     (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))) {
Y
Yishai Hadas 已提交
1663 1664 1665 1666 1667
		err = -EINVAL;
		goto err;
	}

	/* If none of IPV4 & IPV6 SRC/DST was set - this bit field is ignored */
1668 1669
	if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
	    (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4))
Y
Yishai Hadas 已提交
1670 1671
		MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
			 MLX5_L3_PROT_TYPE_IPV4);
1672 1673
	else if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6) ||
		 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
Y
Yishai Hadas 已提交
1674 1675 1676
		MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
			 MLX5_L3_PROT_TYPE_IPV6);

1677 1678 1679 1680 1681 1682 1683
	outer_l4 = ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
		    (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
			   << 0 |
		   ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
		    (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
			   << 1 |
		   (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI) << 2;
1684 1685 1686

	/* Check that only one l4 protocol is set */
	if (outer_l4 & (outer_l4 - 1)) {
Y
Yishai Hadas 已提交
1687 1688 1689 1690 1691
		err = -EINVAL;
		goto err;
	}

	/* If none of TCP & UDP SRC/DST was set - this bit field is ignored */
1692 1693
	if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
	    (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP))
Y
Yishai Hadas 已提交
1694 1695
		MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
			 MLX5_L4_PROT_TYPE_TCP);
1696 1697
	else if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP) ||
		 (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
Y
Yishai Hadas 已提交
1698 1699 1700
		MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
			 MLX5_L4_PROT_TYPE_UDP);

1701 1702
	if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV4) ||
	    (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_IPV6))
Y
Yishai Hadas 已提交
1703 1704
		selected_fields |= MLX5_HASH_FIELD_SEL_SRC_IP;

1705 1706
	if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV4) ||
	    (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_IPV6))
Y
Yishai Hadas 已提交
1707 1708
		selected_fields |= MLX5_HASH_FIELD_SEL_DST_IP;

1709 1710
	if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_TCP) ||
	    (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_SRC_PORT_UDP))
Y
Yishai Hadas 已提交
1711 1712
		selected_fields |= MLX5_HASH_FIELD_SEL_L4_SPORT;

1713 1714
	if ((ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_TCP) ||
	    (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_DST_PORT_UDP))
Y
Yishai Hadas 已提交
1715 1716
		selected_fields |= MLX5_HASH_FIELD_SEL_L4_DPORT;

1717
	if (ucmd->rx_hash_fields_mask & MLX5_RX_HASH_IPSEC_SPI)
1718 1719
		selected_fields |= MLX5_HASH_FIELD_SEL_IPSEC_SPI;

Y
Yishai Hadas 已提交
1720 1721 1722
	MLX5_SET(rx_hash_field_select, hfso, selected_fields, selected_fields);

create_tir:
1723 1724
	MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
	err = mlx5_cmd_exec_inout(dev->mdev, create_tir, in, out);
Y
Yishai Hadas 已提交
1725

1726
	qp->rss_qp.tirn = MLX5_GET(create_tir_out, out, tirn);
1727 1728 1729 1730
	if (!err && MLX5_GET(tirc, tirc, self_lb_block)) {
		err = mlx5_ib_enable_lb(dev, false, true);

		if (err)
1731 1732
			mlx5_cmd_destroy_tir(dev->mdev, qp->rss_qp.tirn,
					     to_mpd(pd)->uid);
1733 1734
	}

Y
Yishai Hadas 已提交
1735 1736 1737
	if (err)
		goto err;

1738
	if (mucontext->devx_uid) {
1739 1740
		params->resp.comp_mask |= MLX5_IB_CREATE_QP_RESP_MASK_TIRN;
		params->resp.tirn = qp->rss_qp.tirn;
1741
		if (MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner)) {
1742
			params->resp.tir_icm_addr =
1743
				MLX5_GET(create_tir_out, out, icm_address_31_0);
1744 1745 1746 1747 1748 1749 1750 1751 1752
			params->resp.tir_icm_addr |=
				(u64)MLX5_GET(create_tir_out, out,
					      icm_address_39_32)
				<< 32;
			params->resp.tir_icm_addr |=
				(u64)MLX5_GET(create_tir_out, out,
					      icm_address_63_40)
				<< 40;
			params->resp.comp_mask |=
1753 1754
				MLX5_IB_CREATE_QP_RESP_MASK_TIR_ICM_ADDR;
		}
1755 1756
	}

Y
Yishai Hadas 已提交
1757 1758 1759
	kvfree(in);
	/* qpn is reserved for that QP */
	qp->trans_qp.base.mqp.qpn = 0;
1760
	qp->is_rss = true;
Y
Yishai Hadas 已提交
1761 1762 1763 1764 1765 1766 1767
	return 0;

err:
	kvfree(in);
	return err;
}

1768 1769
static void configure_requester_scat_cqe(struct mlx5_ib_dev *dev,
					 struct ib_qp_init_attr *init_attr,
1770
					 struct mlx5_ib_create_qp *ucmd,
1771 1772 1773
					 void *qpc)
{
	int scqe_sz;
1774
	bool allow_scat_cqe = false;
1775

1776 1777 1778 1779
	if (ucmd)
		allow_scat_cqe = ucmd->flags & MLX5_QP_FLAG_ALLOW_SCATTER_CQE;

	if (!allow_scat_cqe && init_attr->sq_sig_type != IB_SIGNAL_ALL_WR)
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
		return;

	scqe_sz = mlx5_ib_get_cqe_size(init_attr->send_cq);
	if (scqe_sz == 128) {
		MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA64_CQE);
		return;
	}

	if (init_attr->qp_type != MLX5_IB_QPT_DCI ||
	    MLX5_CAP_GEN(dev->mdev, dc_req_scat_data_cqe))
		MLX5_SET(qpc, qpc, cs_req, MLX5_REQ_SCAT_DATA32_CQE);
}

1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
static int atomic_size_to_mode(int size_mask)
{
	/* driver does not support atomic_size > 256B
	 * and does not know how to translate bigger sizes
	 */
	int supported_size_mask = size_mask & 0x1ff;
	int log_max_size;

	if (!supported_size_mask)
		return -EOPNOTSUPP;

	log_max_size = __fls(supported_size_mask);

	if (log_max_size > 3)
		return log_max_size;

	return MLX5_ATOMIC_MODE_8B;
}

static int get_atomic_mode(struct mlx5_ib_dev *dev,
			   enum ib_qp_type qp_type)
{
	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
	u8 atomic = MLX5_CAP_GEN(dev->mdev, atomic);
	int atomic_mode = -EOPNOTSUPP;
	int atomic_size_mask;

	if (!atomic)
		return -EOPNOTSUPP;

	if (qp_type == MLX5_IB_QPT_DCT)
		atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
	else
		atomic_size_mask = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);

	if ((atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_CMP_SWAP) ||
	    (atomic_operations & MLX5_ATOMIC_OPS_EXTENDED_FETCH_ADD))
		atomic_mode = atomic_size_to_mode(atomic_size_mask);

	if (atomic_mode <= 0 &&
	    (atomic_operations & MLX5_ATOMIC_OPS_CMP_SWAP &&
	     atomic_operations & MLX5_ATOMIC_OPS_FETCH_ADD))
		atomic_mode = MLX5_ATOMIC_MODE_IB_COMP;

	return atomic_mode;
}

1840 1841
static int create_xrc_tgt_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
			     struct mlx5_create_qp_params *params)
1842
{
1843
	struct mlx5_ib_create_qp *ucmd = params->ucmd;
1844 1845
	struct ib_qp_init_attr *attr = params->attr;
	u32 uidx = params->uidx;
1846
	struct mlx5_ib_resources *devr = &dev->devr;
1847
	u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
	int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
	struct mlx5_core_dev *mdev = dev->mdev;
	struct mlx5_ib_qp_base *base;
	unsigned long flags;
	void *qpc;
	u32 *in;
	int err;

	mutex_init(&qp->mutex);

	if (attr->sq_sig_type == IB_SIGNAL_ALL_WR)
		qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;

	in = kvzalloc(inlen, GFP_KERNEL);
	if (!in)
		return -ENOMEM;

1865 1866
	if (MLX5_CAP_GEN(mdev, ece_support))
		MLX5_SET(create_qp_in, in, ece, ucmd->ece_options);
1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
	qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);

	MLX5_SET(qpc, qpc, st, MLX5_QP_ST_XRC);
	MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
	MLX5_SET(qpc, qpc, pd, to_mpd(devr->p0)->pdn);

	if (qp->flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
		MLX5_SET(qpc, qpc, block_lb_mc, 1);
	if (qp->flags & IB_QP_CREATE_CROSS_CHANNEL)
		MLX5_SET(qpc, qpc, cd_master, 1);
	if (qp->flags & IB_QP_CREATE_MANAGED_SEND)
		MLX5_SET(qpc, qpc, cd_slave_send, 1);
	if (qp->flags & IB_QP_CREATE_MANAGED_RECV)
		MLX5_SET(qpc, qpc, cd_slave_receive, 1);

	MLX5_SET(qpc, qpc, rq_type, MLX5_SRQ_RQ);
	MLX5_SET(qpc, qpc, no_sq, 1);
	MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
	MLX5_SET(qpc, qpc, cqn_snd, to_mcq(devr->c0)->mcq.cqn);
	MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
	MLX5_SET(qpc, qpc, xrcd, to_mxrcd(attr->xrcd)->xrcdn);
	MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);

	/* 0xffffff means we ask to work with cqe version 0 */
	if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
		MLX5_SET(qpc, qpc, user_index, uidx);

	if (qp->flags & IB_QP_CREATE_PCI_WRITE_END_PADDING) {
		MLX5_SET(qpc, qpc, end_padding_mode,
			 MLX5_WQ_END_PAD_MODE_ALIGN);
		/* Special case to clean flag */
		qp->flags &= ~IB_QP_CREATE_PCI_WRITE_END_PADDING;
	}

	base = &qp->trans_qp.base;
1902
	err = mlx5_qpc_create_qp(dev, &base->mqp, in, inlen, out);
1903
	kvfree(in);
1904
	if (err)
1905 1906 1907 1908
		return err;

	base->container_mibqp = qp;
	base->mqp.event = mlx5_ib_qp_event;
1909 1910
	if (MLX5_CAP_GEN(mdev, ece_support))
		params->resp.ece_options = MLX5_GET(create_qp_out, out, ece);
1911 1912 1913 1914 1915

	spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
	list_add_tail(&qp->qps_list, &dev->qp_list);
	spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);

1916
	qp->trans_qp.xrcdn = to_mxrcd(attr->xrcd)->xrcdn;
1917 1918 1919
	return 0;
}

1920
static int create_user_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
1921 1922
			  struct mlx5_ib_qp *qp,
			  struct mlx5_create_qp_params *params)
1923
{
1924 1925
	struct ib_qp_init_attr *init_attr = params->attr;
	struct mlx5_ib_create_qp *ucmd = params->ucmd;
1926
	u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
1927 1928
	struct ib_udata *udata = params->udata;
	u32 uidx = params->uidx;
1929
	struct mlx5_ib_resources *devr = &dev->devr;
1930
	int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
1931
	struct mlx5_core_dev *mdev = dev->mdev;
1932 1933 1934
	struct mlx5_ib_cq *send_cq;
	struct mlx5_ib_cq *recv_cq;
	unsigned long flags;
1935
	struct mlx5_ib_qp_base *base;
1936
	int mlx5_st;
1937
	void *qpc;
1938 1939
	u32 *in;
	int err;
1940 1941 1942 1943 1944

	mutex_init(&qp->mutex);
	spin_lock_init(&qp->sq.lock);
	spin_lock_init(&qp->rq.lock);

1945
	mlx5_st = to_mlx5_st(qp->type);
1946 1947 1948
	if (mlx5_st < 0)
		return -EINVAL;

1949 1950 1951
	if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
		qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;

1952 1953 1954
	if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
		qp->underlay_qpn = init_attr->source_qpn;

1955
	base = (init_attr->qp_type == IB_QPT_RAW_PACKET ||
1956
		qp->flags & IB_QP_CREATE_SOURCE_QPN) ?
1957 1958 1959
	       &qp->raw_packet_qp.rq.base :
	       &qp->trans_qp.base;

1960
	qp->has_rq = qp_has_rq(init_attr);
1961
	err = set_rq_size(dev, &init_attr->cap, qp->has_rq, qp, ucmd);
1962 1963 1964 1965 1966
	if (err) {
		mlx5_ib_dbg(dev, "err %d\n", err);
		return err;
	}

1967 1968 1969
	if (ucmd->rq_wqe_shift != qp->rq.wqe_shift ||
	    ucmd->rq_wqe_count != qp->rq.wqe_cnt)
		return -EINVAL;
1970

1971 1972
	if (ucmd->sq_wqe_count > (1 << MLX5_CAP_GEN(mdev, log_max_qp_sz)))
		return -EINVAL;
1973

1974 1975
	err = _create_user_qp(dev, pd, qp, udata, init_attr, &in, &params->resp,
			      &inlen, base, ucmd);
1976 1977
	if (err)
		return err;
1978 1979 1980 1981

	if (is_sqp(init_attr->qp_type))
		qp->port = init_attr->port_num;

1982 1983
	if (MLX5_CAP_GEN(mdev, ece_support))
		MLX5_SET(create_qp_in, in, ece, ucmd->ece_options);
1984 1985
	qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);

1986
	MLX5_SET(qpc, qpc, st, mlx5_st);
1987
	MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
1988
	MLX5_SET(qpc, qpc, pd, to_mpd(pd)->pdn);
1989

1990
	if (qp->flags_en & MLX5_QP_FLAG_SIGNATURE)
1991
		MLX5_SET(qpc, qpc, wq_signature, 1);
1992

1993
	if (qp->flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
1994
		MLX5_SET(qpc, qpc, block_lb_mc, 1);
1995

1996
	if (qp->flags & IB_QP_CREATE_CROSS_CHANNEL)
1997
		MLX5_SET(qpc, qpc, cd_master, 1);
1998
	if (qp->flags & IB_QP_CREATE_MANAGED_SEND)
1999
		MLX5_SET(qpc, qpc, cd_slave_send, 1);
2000
	if (qp->flags & IB_QP_CREATE_MANAGED_RECV)
2001
		MLX5_SET(qpc, qpc, cd_slave_receive, 1);
2002
	if (qp->flags_en & MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE)
2003
		MLX5_SET(qpc, qpc, req_e2e_credit_mode, 1);
2004 2005 2006
	if ((qp->flags_en & MLX5_QP_FLAG_SCATTER_CQE) &&
	    (init_attr->qp_type == IB_QPT_RC ||
	     init_attr->qp_type == IB_QPT_UC)) {
2007
		int rcqe_sz = mlx5_ib_get_cqe_size(init_attr->recv_cq);
2008 2009 2010 2011 2012

		MLX5_SET(qpc, qpc, cs_res,
			 rcqe_sz == 128 ? MLX5_RES_SCAT_DATA64_CQE :
					  MLX5_RES_SCAT_DATA32_CQE);
	}
2013
	if ((qp->flags_en & MLX5_QP_FLAG_SCATTER_CQE) &&
2014
	    (qp->type == MLX5_IB_QPT_DCI || qp->type == IB_QPT_RC))
2015
		configure_requester_scat_cqe(dev, init_attr, ucmd, qpc);
2016 2017

	if (qp->rq.wqe_cnt) {
2018 2019
		MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
		MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
2020 2021
	}

2022
	MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, init_attr));
2023

A
Artemy Kovalyov 已提交
2024
	if (qp->sq.wqe_cnt) {
2025
		MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
A
Artemy Kovalyov 已提交
2026
	} else {
2027
		MLX5_SET(qpc, qpc, no_sq, 1);
A
Artemy Kovalyov 已提交
2028 2029 2030 2031 2032
		if (init_attr->srq &&
		    init_attr->srq->srq_type == IB_SRQT_TM)
			MLX5_SET(qpc, qpc, offload_type,
				 MLX5_QPC_OFFLOAD_TYPE_RNDV);
	}
2033 2034 2035 2036

	/* Set default resources */
	switch (init_attr->qp_type) {
	case IB_QPT_XRC_INI:
2037 2038 2039
		MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(devr->c0)->mcq.cqn);
		MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
		MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s0)->msrq.srqn);
2040 2041 2042
		break;
	default:
		if (init_attr->srq) {
2043 2044
			MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
			MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(init_attr->srq)->msrq.srqn);
2045
		} else {
2046 2047
			MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
			MLX5_SET(qpc, qpc, srqn_rmpn_xrqn, to_msrq(devr->s1)->msrq.srqn);
2048 2049 2050 2051
		}
	}

	if (init_attr->send_cq)
2052
		MLX5_SET(qpc, qpc, cqn_snd, to_mcq(init_attr->send_cq)->mcq.cqn);
2053 2054

	if (init_attr->recv_cq)
2055
		MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(init_attr->recv_cq)->mcq.cqn);
2056

2057
	MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
2058

2059 2060
	/* 0xffffff means we ask to work with cqe version 0 */
	if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
2061
		MLX5_SET(qpc, qpc, user_index, uidx);
2062

2063 2064 2065 2066 2067 2068
	if (qp->flags & IB_QP_CREATE_PCI_WRITE_END_PADDING &&
	    init_attr->qp_type != IB_QPT_RAW_PACKET) {
		MLX5_SET(qpc, qpc, end_padding_mode,
			 MLX5_WQ_END_PAD_MODE_ALIGN);
		/* Special case to clean flag */
		qp->flags &= ~IB_QP_CREATE_PCI_WRITE_END_PADDING;
2069 2070
	}

2071
	if (init_attr->qp_type == IB_QPT_RAW_PACKET ||
2072
	    qp->flags & IB_QP_CREATE_SOURCE_QPN) {
2073
		qp->raw_packet_qp.sq.ubuffer.buf_addr = ucmd->sq_buf_addr;
2074
		raw_packet_qp_copy_info(qp, &qp->raw_packet_qp);
2075
		err = create_raw_packet_qp(dev, qp, in, inlen, pd, udata,
2076
					   &params->resp);
2077
	} else
2078
		err = mlx5_qpc_create_qp(dev, &base->mqp, in, inlen, out);
2079

A
Al Viro 已提交
2080
	kvfree(in);
2081 2082
	if (err)
		goto err_create;
2083

2084 2085
	base->container_mibqp = qp;
	base->mqp.event = mlx5_ib_qp_event;
2086 2087
	if (MLX5_CAP_GEN(mdev, ece_support))
		params->resp.ece_options = MLX5_GET(create_qp_out, out, ece);
2088

2089
	get_cqs(qp->type, init_attr->send_cq, init_attr->recv_cq,
2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
		&send_cq, &recv_cq);
	spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
	mlx5_ib_lock_cqs(send_cq, recv_cq);
	/* Maintain device to QPs access, needed for further handling via reset
	 * flow
	 */
	list_add_tail(&qp->qps_list, &dev->qp_list);
	/* Maintain CQ to QPs access, needed for further handling via reset flow
	 */
	if (send_cq)
		list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
	if (recv_cq)
		list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
	mlx5_ib_unlock_cqs(send_cq, recv_cq);
	spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);

2106 2107 2108
	return 0;

err_create:
2109
	destroy_qp(dev, qp, base, udata);
2110 2111 2112
	return err;
}

2113
static int create_kernel_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
2114 2115
			    struct mlx5_ib_qp *qp,
			    struct mlx5_create_qp_params *params)
2116
{
2117 2118
	struct ib_qp_init_attr *attr = params->attr;
	u32 uidx = params->uidx;
2119
	struct mlx5_ib_resources *devr = &dev->devr;
2120
	u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {};
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
	int inlen = MLX5_ST_SZ_BYTES(create_qp_in);
	struct mlx5_core_dev *mdev = dev->mdev;
	struct mlx5_ib_cq *send_cq;
	struct mlx5_ib_cq *recv_cq;
	unsigned long flags;
	struct mlx5_ib_qp_base *base;
	int mlx5_st;
	void *qpc;
	u32 *in;
	int err;

	mutex_init(&qp->mutex);
	spin_lock_init(&qp->sq.lock);
	spin_lock_init(&qp->rq.lock);

	mlx5_st = to_mlx5_st(qp->type);
	if (mlx5_st < 0)
		return -EINVAL;

	if (attr->sq_sig_type == IB_SIGNAL_ALL_WR)
		qp->sq_signal_bits = MLX5_WQE_CTRL_CQ_UPDATE;

	base = &qp->trans_qp.base;

	qp->has_rq = qp_has_rq(attr);
	err = set_rq_size(dev, &attr->cap, qp->has_rq, qp, NULL);
	if (err) {
		mlx5_ib_dbg(dev, "err %d\n", err);
		return err;
	}

	err = _create_kernel_qp(dev, attr, qp, &in, &inlen, base);
	if (err)
		return err;

	if (is_sqp(attr->qp_type))
		qp->port = attr->port_num;

	qpc = MLX5_ADDR_OF(create_qp_in, in, qpc);

	MLX5_SET(qpc, qpc, st, mlx5_st);
	MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);

	if (attr->qp_type != MLX5_IB_QPT_REG_UMR)
		MLX5_SET(qpc, qpc, pd, to_mpd(pd ? pd : devr->p0)->pdn);
	else
		MLX5_SET(qpc, qpc, latency_sensitive, 1);


	if (qp->flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
		MLX5_SET(qpc, qpc, block_lb_mc, 1);

	if (qp->rq.wqe_cnt) {
		MLX5_SET(qpc, qpc, log_rq_stride, qp->rq.wqe_shift - 4);
		MLX5_SET(qpc, qpc, log_rq_size, ilog2(qp->rq.wqe_cnt));
	}

	MLX5_SET(qpc, qpc, rq_type, get_rx_type(qp, attr));

	if (qp->sq.wqe_cnt)
		MLX5_SET(qpc, qpc, log_sq_size, ilog2(qp->sq.wqe_cnt));
	else
		MLX5_SET(qpc, qpc, no_sq, 1);

	if (attr->srq) {
		MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x0)->xrcdn);
		MLX5_SET(qpc, qpc, srqn_rmpn_xrqn,
			 to_msrq(attr->srq)->msrq.srqn);
	} else {
		MLX5_SET(qpc, qpc, xrcd, to_mxrcd(devr->x1)->xrcdn);
		MLX5_SET(qpc, qpc, srqn_rmpn_xrqn,
			 to_msrq(devr->s1)->msrq.srqn);
	}

	if (attr->send_cq)
		MLX5_SET(qpc, qpc, cqn_snd, to_mcq(attr->send_cq)->mcq.cqn);

	if (attr->recv_cq)
		MLX5_SET(qpc, qpc, cqn_rcv, to_mcq(attr->recv_cq)->mcq.cqn);

	MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);

	/* 0xffffff means we ask to work with cqe version 0 */
	if (MLX5_CAP_GEN(mdev, cqe_version) == MLX5_CQE_VERSION_V1)
		MLX5_SET(qpc, qpc, user_index, uidx);

	/* we use IB_QP_CREATE_IPOIB_UD_LSO to indicates ipoib qp */
	if (qp->flags & IB_QP_CREATE_IPOIB_UD_LSO)
		MLX5_SET(qpc, qpc, ulp_stateless_offload_mode, 1);

2211
	err = mlx5_qpc_create_qp(dev, &base->mqp, in, inlen, out);
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
	kvfree(in);
	if (err)
		goto err_create;

	base->container_mibqp = qp;
	base->mqp.event = mlx5_ib_qp_event;

	get_cqs(qp->type, attr->send_cq, attr->recv_cq,
		&send_cq, &recv_cq);
	spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
	mlx5_ib_lock_cqs(send_cq, recv_cq);
	/* Maintain device to QPs access, needed for further handling via reset
	 * flow
	 */
	list_add_tail(&qp->qps_list, &dev->qp_list);
	/* Maintain CQ to QPs access, needed for further handling via reset flow
	 */
	if (send_cq)
		list_add_tail(&qp->cq_send_list, &send_cq->list_send_qp);
	if (recv_cq)
		list_add_tail(&qp->cq_recv_list, &recv_cq->list_recv_qp);
	mlx5_ib_unlock_cqs(send_cq, recv_cq);
	spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);

	return 0;

err_create:
2239
	destroy_qp(dev, qp, base, NULL);
2240 2241 2242
	return err;
}

2243 2244 2245 2246 2247 2248
static void mlx5_ib_lock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
	__acquires(&send_cq->lock) __acquires(&recv_cq->lock)
{
	if (send_cq) {
		if (recv_cq) {
			if (send_cq->mcq.cqn < recv_cq->mcq.cqn)  {
2249
				spin_lock(&send_cq->lock);
2250 2251 2252
				spin_lock_nested(&recv_cq->lock,
						 SINGLE_DEPTH_NESTING);
			} else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
2253
				spin_lock(&send_cq->lock);
2254 2255
				__acquire(&recv_cq->lock);
			} else {
2256
				spin_lock(&recv_cq->lock);
2257 2258 2259 2260
				spin_lock_nested(&send_cq->lock,
						 SINGLE_DEPTH_NESTING);
			}
		} else {
2261
			spin_lock(&send_cq->lock);
E
Eli Cohen 已提交
2262
			__acquire(&recv_cq->lock);
2263 2264
		}
	} else if (recv_cq) {
2265
		spin_lock(&recv_cq->lock);
E
Eli Cohen 已提交
2266 2267 2268 2269
		__acquire(&send_cq->lock);
	} else {
		__acquire(&send_cq->lock);
		__acquire(&recv_cq->lock);
2270 2271 2272 2273 2274 2275 2276 2277 2278 2279
	}
}

static void mlx5_ib_unlock_cqs(struct mlx5_ib_cq *send_cq, struct mlx5_ib_cq *recv_cq)
	__releases(&send_cq->lock) __releases(&recv_cq->lock)
{
	if (send_cq) {
		if (recv_cq) {
			if (send_cq->mcq.cqn < recv_cq->mcq.cqn)  {
				spin_unlock(&recv_cq->lock);
2280
				spin_unlock(&send_cq->lock);
2281 2282
			} else if (send_cq->mcq.cqn == recv_cq->mcq.cqn) {
				__release(&recv_cq->lock);
2283
				spin_unlock(&send_cq->lock);
2284 2285
			} else {
				spin_unlock(&send_cq->lock);
2286
				spin_unlock(&recv_cq->lock);
2287 2288
			}
		} else {
E
Eli Cohen 已提交
2289
			__release(&recv_cq->lock);
2290
			spin_unlock(&send_cq->lock);
2291 2292
		}
	} else if (recv_cq) {
E
Eli Cohen 已提交
2293
		__release(&send_cq->lock);
2294
		spin_unlock(&recv_cq->lock);
E
Eli Cohen 已提交
2295 2296 2297
	} else {
		__release(&recv_cq->lock);
		__release(&send_cq->lock);
2298 2299 2300
	}
}

2301 2302
static void get_cqs(enum ib_qp_type qp_type,
		    struct ib_cq *ib_send_cq, struct ib_cq *ib_recv_cq,
2303 2304
		    struct mlx5_ib_cq **send_cq, struct mlx5_ib_cq **recv_cq)
{
2305
	switch (qp_type) {
2306 2307 2308 2309 2310 2311
	case IB_QPT_XRC_TGT:
		*send_cq = NULL;
		*recv_cq = NULL;
		break;
	case MLX5_IB_QPT_REG_UMR:
	case IB_QPT_XRC_INI:
2312
		*send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
2313 2314 2315 2316
		*recv_cq = NULL;
		break;

	case IB_QPT_SMI:
H
Haggai Eran 已提交
2317
	case MLX5_IB_QPT_HW_GSI:
2318 2319 2320
	case IB_QPT_RC:
	case IB_QPT_UC:
	case IB_QPT_UD:
2321
	case IB_QPT_RAW_PACKET:
2322 2323
		*send_cq = ib_send_cq ? to_mcq(ib_send_cq) : NULL;
		*recv_cq = ib_recv_cq ? to_mcq(ib_recv_cq) : NULL;
2324 2325 2326 2327 2328 2329 2330 2331
		break;
	default:
		*send_cq = NULL;
		*recv_cq = NULL;
		break;
	}
}

2332
static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
A
Aviv Heller 已提交
2333 2334
				const struct mlx5_modify_raw_qp_param *raw_qp_param,
				u8 lag_tx_affinity);
2335

2336 2337
static void destroy_qp_common(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
			      struct ib_udata *udata)
2338 2339
{
	struct mlx5_ib_cq *send_cq, *recv_cq;
2340
	struct mlx5_ib_qp_base *base;
2341
	unsigned long flags;
2342 2343
	int err;

Y
Yishai Hadas 已提交
2344 2345 2346 2347 2348
	if (qp->ibqp.rwq_ind_tbl) {
		destroy_rss_raw_qp_tir(dev, qp);
		return;
	}

2349
	base = (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2350
		qp->flags & IB_QP_CREATE_SOURCE_QPN) ?
2351 2352 2353
	       &qp->raw_packet_qp.rq.base :
	       &qp->trans_qp.base;

2354
	if (qp->state != IB_QPS_RESET) {
2355
		if (qp->ibqp.qp_type != IB_QPT_RAW_PACKET &&
2356
		    !(qp->flags & IB_QP_CREATE_SOURCE_QPN)) {
2357
			err = mlx5_core_qp_modify(dev, MLX5_CMD_OP_2RST_QP, 0,
2358
						  NULL, &base->mqp, NULL);
2359
		} else {
2360 2361 2362 2363
			struct mlx5_modify_raw_qp_param raw_qp_param = {
				.operation = MLX5_CMD_OP_2RST_QP
			};

A
Aviv Heller 已提交
2364
			err = modify_raw_packet_qp(dev, qp, &raw_qp_param, 0);
2365 2366
		}
		if (err)
2367
			mlx5_ib_warn(dev, "mlx5_ib: modify QP 0x%06x to RESET failed\n",
2368
				     base->mqp.qpn);
2369
	}
2370

2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
	get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
		&send_cq, &recv_cq);

	spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
	mlx5_ib_lock_cqs(send_cq, recv_cq);
	/* del from lists under both locks above to protect reset flow paths */
	list_del(&qp->qps_list);
	if (send_cq)
		list_del(&qp->cq_send_list);

	if (recv_cq)
		list_del(&qp->cq_recv_list);
2383

2384
	if (!udata) {
2385
		__mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
2386 2387
				   qp->ibqp.srq ? to_msrq(qp->ibqp.srq) : NULL);
		if (send_cq != recv_cq)
2388 2389
			__mlx5_ib_cq_clean(send_cq, base->mqp.qpn,
					   NULL);
2390
	}
2391 2392
	mlx5_ib_unlock_cqs(send_cq, recv_cq);
	spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
2393

2394
	if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
2395
	    qp->flags & IB_QP_CREATE_SOURCE_QPN) {
2396 2397
		destroy_raw_packet_qp(dev, qp);
	} else {
2398
		err = mlx5_core_destroy_qp(dev, &base->mqp);
2399 2400 2401 2402
		if (err)
			mlx5_ib_warn(dev, "failed to destroy QP 0x%x\n",
				     base->mqp.qpn);
	}
2403

2404
	destroy_qp(dev, qp, base, udata);
2405 2406
}

2407 2408
static int create_dct(struct mlx5_ib_dev *dev, struct ib_pd *pd,
		      struct mlx5_ib_qp *qp,
2409
		      struct mlx5_create_qp_params *params)
2410
{
2411 2412 2413
	struct ib_qp_init_attr *attr = params->attr;
	struct mlx5_ib_create_qp *ucmd = params->ucmd;
	u32 uidx = params->uidx;
2414 2415 2416
	void *dctc;

	qp->dct.in = kzalloc(MLX5_ST_SZ_BYTES(create_dct_in), GFP_KERNEL);
2417
	if (!qp->dct.in)
2418
		return -ENOMEM;
2419

2420
	MLX5_SET(create_dct_in, qp->dct.in, uid, to_mpd(pd)->uid);
2421 2422 2423 2424 2425 2426
	dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
	MLX5_SET(dctc, dctc, pd, to_mpd(pd)->pdn);
	MLX5_SET(dctc, dctc, srqn_xrqn, to_msrq(attr->srq)->msrq.srqn);
	MLX5_SET(dctc, dctc, cqn, to_mcq(attr->recv_cq)->mcq.cqn);
	MLX5_SET64(dctc, dctc, dc_access_key, ucmd->access_key);
	MLX5_SET(dctc, dctc, user_index, uidx);
2427 2428
	if (MLX5_CAP_GEN(dev->mdev, ece_support))
		MLX5_SET(dctc, dctc, ece, ucmd->ece_options);
2429

2430
	if (qp->flags_en & MLX5_QP_FLAG_SCATTER_CQE) {
2431 2432 2433 2434 2435
		int rcqe_sz = mlx5_ib_get_cqe_size(attr->recv_cq);

		if (rcqe_sz == 128)
			MLX5_SET(dctc, dctc, cs_res, MLX5_RES_SCAT_DATA64_CQE);
	}
2436

2437 2438
	qp->state = IB_QPS_RESET;

2439
	return 0;
2440 2441
}

2442 2443
static int check_qp_type(struct mlx5_ib_dev *dev, struct ib_qp_init_attr *attr,
			 enum ib_qp_type *type)
2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
{
	if (attr->qp_type == IB_QPT_DRIVER && !MLX5_CAP_GEN(dev->mdev, dct))
		goto out;

	switch (attr->qp_type) {
	case IB_QPT_XRC_TGT:
	case IB_QPT_XRC_INI:
		if (!MLX5_CAP_GEN(dev->mdev, xrc))
			goto out;
		fallthrough;
	case IB_QPT_RC:
	case IB_QPT_UC:
	case IB_QPT_SMI:
	case MLX5_IB_QPT_HW_GSI:
	case IB_QPT_DRIVER:
	case IB_QPT_GSI:
2460 2461 2462 2463 2464
		if (dev->profile == &raw_eth_profile)
			goto out;
	case IB_QPT_RAW_PACKET:
	case IB_QPT_UD:
	case MLX5_IB_QPT_REG_UMR:
2465
		break;
2466 2467
	default:
		goto out;
2468 2469
	}

2470
	*type = attr->qp_type;
2471
	return 0;
2472 2473 2474 2475

out:
	mlx5_ib_dbg(dev, "Unsupported QP type %d\n", attr->qp_type);
	return -EOPNOTSUPP;
2476 2477
}

2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533
static int check_valid_flow(struct mlx5_ib_dev *dev, struct ib_pd *pd,
			    struct ib_qp_init_attr *attr,
			    struct ib_udata *udata)
{
	struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
		udata, struct mlx5_ib_ucontext, ibucontext);

	if (!udata) {
		/* Kernel create_qp callers */
		if (attr->rwq_ind_tbl)
			return -EOPNOTSUPP;

		switch (attr->qp_type) {
		case IB_QPT_RAW_PACKET:
		case IB_QPT_DRIVER:
			return -EOPNOTSUPP;
		default:
			return 0;
		}
	}

	/* Userspace create_qp callers */
	if (attr->qp_type == IB_QPT_RAW_PACKET && !ucontext->cqe_version) {
		mlx5_ib_dbg(dev,
			"Raw Packet QP is only supported for CQE version > 0\n");
		return -EINVAL;
	}

	if (attr->qp_type != IB_QPT_RAW_PACKET && attr->rwq_ind_tbl) {
		mlx5_ib_dbg(dev,
			    "Wrong QP type %d for the RWQ indirect table\n",
			    attr->qp_type);
		return -EINVAL;
	}

	switch (attr->qp_type) {
	case IB_QPT_SMI:
	case MLX5_IB_QPT_HW_GSI:
	case MLX5_IB_QPT_REG_UMR:
	case IB_QPT_GSI:
		mlx5_ib_dbg(dev, "Kernel doesn't support QP type %d\n",
			    attr->qp_type);
		return -EINVAL;
	default:
		break;
	}

	/*
	 * We don't need to see this warning, it means that kernel code
	 * missing ib_pd. Placed here to catch developer's mistakes.
	 */
	WARN_ONCE(!pd && attr->qp_type != IB_QPT_XRC_TGT,
		  "There is a missing PD pointer assignment\n");
	return 0;
}

2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
static void process_vendor_flag(struct mlx5_ib_dev *dev, int *flags, int flag,
				bool cond, struct mlx5_ib_qp *qp)
{
	if (!(*flags & flag))
		return;

	if (cond) {
		qp->flags_en |= flag;
		*flags &= ~flag;
		return;
	}

	if (flag == MLX5_QP_FLAG_SCATTER_CQE) {
		/*
		 * We don't return error if this flag was provided,
		 * and mlx5 doesn't have right capability.
		 */
		*flags &= ~MLX5_QP_FLAG_SCATTER_CQE;
		return;
	}
	mlx5_ib_dbg(dev, "Vendor create QP flag 0x%X is not supported\n", flag);
}

static int process_vendor_flags(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
2558
				void *ucmd, struct ib_qp_init_attr *attr)
2559
{
2560 2561
	struct mlx5_core_dev *mdev = dev->mdev;
	bool cond;
2562 2563 2564 2565 2566 2567
	int flags;

	if (attr->rwq_ind_tbl)
		flags = ((struct mlx5_ib_create_qp_rss *)ucmd)->flags;
	else
		flags = ((struct mlx5_ib_create_qp *)ucmd)->flags;
2568 2569

	switch (flags & (MLX5_QP_FLAG_TYPE_DCT | MLX5_QP_FLAG_TYPE_DCI)) {
2570
	case MLX5_QP_FLAG_TYPE_DCI:
2571
		qp->type = MLX5_IB_QPT_DCI;
2572 2573
		break;
	case MLX5_QP_FLAG_TYPE_DCT:
2574
		qp->type = MLX5_IB_QPT_DCT;
2575
		break;
2576 2577 2578 2579 2580 2581 2582
	default:
		if (qp->type != IB_QPT_DRIVER)
			break;
		/*
		 * It is IB_QPT_DRIVER and or no subtype or
		 * wrong subtype were provided.
		 */
2583
		return -EINVAL;
2584
	}
2585 2586 2587 2588 2589 2590 2591 2592

	process_vendor_flag(dev, &flags, MLX5_QP_FLAG_TYPE_DCI, true, qp);
	process_vendor_flag(dev, &flags, MLX5_QP_FLAG_TYPE_DCT, true, qp);

	process_vendor_flag(dev, &flags, MLX5_QP_FLAG_SIGNATURE, true, qp);
	process_vendor_flag(dev, &flags, MLX5_QP_FLAG_SCATTER_CQE,
			    MLX5_CAP_GEN(mdev, sctr_data_cqe), qp);

2593
	if (qp->type == IB_QPT_RAW_PACKET) {
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
		cond = MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan) ||
		       MLX5_CAP_ETH(mdev, tunnel_stateless_gre) ||
		       MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx);
		process_vendor_flag(dev, &flags, MLX5_QP_FLAG_TUNNEL_OFFLOADS,
				    cond, qp);
		process_vendor_flag(dev, &flags,
				    MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC, true,
				    qp);
		process_vendor_flag(dev, &flags,
				    MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC, true,
				    qp);
2605 2606
	}

2607
	if (qp->type == IB_QPT_RC)
2608 2609 2610 2611
		process_vendor_flag(dev, &flags,
				    MLX5_QP_FLAG_PACKET_BASED_CREDIT_MODE,
				    MLX5_CAP_GEN(mdev, qp_packet_based), qp);

2612 2613 2614
	process_vendor_flag(dev, &flags, MLX5_QP_FLAG_BFREG_INDEX, true, qp);
	process_vendor_flag(dev, &flags, MLX5_QP_FLAG_UAR_PAGE_INDEX, true, qp);

2615 2616 2617 2618 2619 2620 2621 2622 2623
	cond = qp->flags_en & ~(MLX5_QP_FLAG_TUNNEL_OFFLOADS |
				MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_UC |
				MLX5_QP_FLAG_TIR_ALLOW_SELF_LB_MC);
	if (attr->rwq_ind_tbl && cond) {
		mlx5_ib_dbg(dev, "RSS RAW QP has unsupported flags 0x%X\n",
			    cond);
		return -EINVAL;
	}

2624 2625 2626 2627
	if (flags)
		mlx5_ib_dbg(dev, "udata has unsupported flags 0x%X\n", flags);

	return (flags) ? -EINVAL : 0;
2628
	}
2629

2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655
static void process_create_flag(struct mlx5_ib_dev *dev, int *flags, int flag,
				bool cond, struct mlx5_ib_qp *qp)
{
	if (!(*flags & flag))
		return;

	if (cond) {
		qp->flags |= flag;
		*flags &= ~flag;
		return;
	}

	if (flag == MLX5_IB_QP_CREATE_WC_TEST) {
		/*
		 * Special case, if condition didn't meet, it won't be error,
		 * just different in-kernel flow.
		 */
		*flags &= ~MLX5_IB_QP_CREATE_WC_TEST;
		return;
	}
	mlx5_ib_dbg(dev, "Verbs create QP flag 0x%X is not supported\n", flag);
}

static int process_create_flags(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
				struct ib_qp_init_attr *attr)
{
2656
	enum ib_qp_type qp_type = qp->type;
2657 2658 2659 2660
	struct mlx5_core_dev *mdev = dev->mdev;
	int create_flags = attr->create_flags;
	bool cond;

2661 2662 2663 2664
	if (qp->type == IB_QPT_UD && dev->profile == &raw_eth_profile)
		if (create_flags & ~MLX5_IB_QP_CREATE_WC_TEST)
			return -EINVAL;

2665
	if (qp_type == MLX5_IB_QPT_DCT)
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718
		return (create_flags) ? -EINVAL : 0;

	if (qp_type == IB_QPT_RAW_PACKET && attr->rwq_ind_tbl)
		return (create_flags) ? -EINVAL : 0;

	process_create_flag(dev, &create_flags,
			    IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK,
			    MLX5_CAP_GEN(mdev, block_lb_mc), qp);
	process_create_flag(dev, &create_flags, IB_QP_CREATE_CROSS_CHANNEL,
			    MLX5_CAP_GEN(mdev, cd), qp);
	process_create_flag(dev, &create_flags, IB_QP_CREATE_MANAGED_SEND,
			    MLX5_CAP_GEN(mdev, cd), qp);
	process_create_flag(dev, &create_flags, IB_QP_CREATE_MANAGED_RECV,
			    MLX5_CAP_GEN(mdev, cd), qp);

	if (qp_type == IB_QPT_UD) {
		process_create_flag(dev, &create_flags,
				    IB_QP_CREATE_IPOIB_UD_LSO,
				    MLX5_CAP_GEN(mdev, ipoib_basic_offloads),
				    qp);
		cond = MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_IB;
		process_create_flag(dev, &create_flags, IB_QP_CREATE_SOURCE_QPN,
				    cond, qp);
	}

	if (qp_type == IB_QPT_RAW_PACKET) {
		cond = MLX5_CAP_GEN(mdev, eth_net_offloads) &&
		       MLX5_CAP_ETH(mdev, scatter_fcs);
		process_create_flag(dev, &create_flags,
				    IB_QP_CREATE_SCATTER_FCS, cond, qp);

		cond = MLX5_CAP_GEN(mdev, eth_net_offloads) &&
		       MLX5_CAP_ETH(mdev, vlan_cap);
		process_create_flag(dev, &create_flags,
				    IB_QP_CREATE_CVLAN_STRIPPING, cond, qp);
	}

	process_create_flag(dev, &create_flags,
			    IB_QP_CREATE_PCI_WRITE_END_PADDING,
			    MLX5_CAP_GEN(mdev, end_pad), qp);

	process_create_flag(dev, &create_flags, MLX5_IB_QP_CREATE_WC_TEST,
			    qp_type != MLX5_IB_QPT_REG_UMR, qp);
	process_create_flag(dev, &create_flags, MLX5_IB_QP_CREATE_SQPN_QP1,
			    true, qp);

	if (create_flags)
		mlx5_ib_dbg(dev, "Create QP has unsupported flags 0x%X\n",
			    create_flags);

	return (create_flags) ? -EINVAL : 0;
}

2719 2720
static int process_udata_size(struct mlx5_ib_dev *dev,
			      struct mlx5_create_qp_params *params)
2721 2722
{
	size_t ucmd = sizeof(struct mlx5_ib_create_qp);
2723 2724
	struct ib_udata *udata = params->udata;
	size_t outlen = udata->outlen;
2725
	size_t inlen = udata->inlen;
2726

2727
	params->outlen = min(outlen, sizeof(struct mlx5_ib_create_qp_resp));
2728
	params->ucmd_size = ucmd;
2729
	if (!params->is_rss_raw) {
2730 2731 2732 2733 2734 2735 2736 2737 2738
		/* User has old rdma-core, which doesn't support ECE */
		size_t min_inlen =
			offsetof(struct mlx5_ib_create_qp, ece_options);

		/*
		 * We will check in check_ucmd_data() that user
		 * cleared everything after inlen.
		 */
		params->inlen = (inlen < min_inlen) ? 0 : min(inlen, ucmd);
2739 2740
		goto out;
	}
2741

2742
	/* RSS RAW QP */
2743
	if (inlen < offsetofend(struct mlx5_ib_create_qp_rss, flags))
2744 2745 2746 2747
		return -EINVAL;

	if (outlen < offsetofend(struct mlx5_ib_create_qp_resp, bfreg_index))
		return -EINVAL;
2748 2749

	ucmd = sizeof(struct mlx5_ib_create_qp_rss);
2750
	params->ucmd_size = ucmd;
2751
	if (inlen > ucmd && !ib_is_udata_cleared(udata, ucmd, inlen - ucmd))
2752 2753 2754 2755 2756
		return -EINVAL;

	params->inlen = min(ucmd, inlen);
out:
	if (!params->inlen)
2757
		mlx5_ib_dbg(dev, "udata is too small\n");
2758

2759
	return (params->inlen) ? 0 : -EINVAL;
2760 2761
}

2762 2763 2764
static int create_qp(struct mlx5_ib_dev *dev, struct ib_pd *pd,
		     struct mlx5_ib_qp *qp,
		     struct mlx5_create_qp_params *params)
2765
{
2766 2767 2768 2769 2770 2771 2772 2773
	int err;

	if (params->is_rss_raw) {
		err = create_rss_raw_qp_tir(dev, pd, qp, params);
		goto out;
	}

	if (qp->type == MLX5_IB_QPT_DCT) {
2774
		err = create_dct(dev, pd, qp, params);
2775 2776 2777 2778 2779 2780 2781
		goto out;
	}

	if (qp->type == IB_QPT_XRC_TGT) {
		err = create_xrc_tgt_qp(dev, qp, params);
		goto out;
	}
2782

2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
	if (params->udata)
		err = create_user_qp(dev, pd, qp, params);
	else
		err = create_kernel_qp(dev, pd, qp, params);

out:
	if (err) {
		mlx5_ib_err(dev, "Create QP type %d failed\n", qp->type);
		return err;
	}

	if (is_qp0(qp->type))
		qp->ibqp.qp_num = 0;
	else if (is_qp1(qp->type))
		qp->ibqp.qp_num = 1;
	else
		qp->ibqp.qp_num = qp->trans_qp.base.mqp.qpn;

	mlx5_ib_dbg(dev,
2802
		"QP type %d, ib qpn 0x%X, mlx qpn 0x%x, rcqn 0x%x, scqn 0x%x, ece 0x%x\n",
2803 2804 2805 2806
		qp->type, qp->ibqp.qp_num, qp->trans_qp.base.mqp.qpn,
		params->attr->recv_cq ? to_mcq(params->attr->recv_cq)->mcq.cqn :
					-1,
		params->attr->send_cq ? to_mcq(params->attr->send_cq)->mcq.cqn :
2807 2808
					-1,
		params->resp.ece_options);
2809 2810

	return 0;
2811 2812
}

2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
static int check_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
			 struct ib_qp_init_attr *attr)
{
	int ret = 0;

	switch (qp->type) {
	case MLX5_IB_QPT_DCT:
		ret = (!attr->srq || !attr->recv_cq) ? -EINVAL : 0;
		break;
	case MLX5_IB_QPT_DCI:
		ret = (attr->cap.max_recv_wr || attr->cap.max_recv_sge) ?
			      -EINVAL :
			      0;
		break;
2827 2828 2829
	case IB_QPT_RAW_PACKET:
		ret = (attr->rwq_ind_tbl && attr->send_cq) ? -EINVAL : 0;
		break;
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839
	default:
		break;
	}

	if (ret)
		mlx5_ib_dbg(dev, "QP type %d has wrong attributes\n", qp->type);

	return ret;
}

2840 2841
static int get_qp_uidx(struct mlx5_ib_qp *qp,
		       struct mlx5_create_qp_params *params)
2842
{
2843 2844
	struct mlx5_ib_create_qp *ucmd = params->ucmd;
	struct ib_udata *udata = params->udata;
2845 2846 2847
	struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
		udata, struct mlx5_ib_ucontext, ibucontext);

2848
	if (params->is_rss_raw)
2849 2850
		return 0;

2851
	return get_qp_user_index(ucontext, ucmd, sizeof(*ucmd), &params->uidx);
2852 2853
}

2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872
static int mlx5_ib_destroy_dct(struct mlx5_ib_qp *mqp)
{
	struct mlx5_ib_dev *dev = to_mdev(mqp->ibqp.device);

	if (mqp->state == IB_QPS_RTR) {
		int err;

		err = mlx5_core_destroy_dct(dev, &mqp->dct.mdct);
		if (err) {
			mlx5_ib_warn(dev, "failed to destroy DCT %d\n", err);
			return err;
		}
	}

	kfree(mqp->dct.in);
	kfree(mqp);
	return 0;
}

2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
static int check_ucmd_data(struct mlx5_ib_dev *dev,
			   struct mlx5_create_qp_params *params)
{
	struct ib_qp_init_attr *attr = params->attr;
	struct ib_udata *udata = params->udata;
	size_t size, last;
	int ret;

	if (params->is_rss_raw)
		/*
		 * These QPs don't have "reserved" field in their
		 * create_qp input struct, so their data is always valid.
		 */
		last = sizeof(struct mlx5_ib_create_qp_rss);
	else
2888
		/* IB_QPT_RAW_PACKET doesn't have ECE data */
2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914
		switch (attr->qp_type) {
		case IB_QPT_RAW_PACKET:
			last = offsetof(struct mlx5_ib_create_qp, ece_options);
			break;
		default:
			last = offsetof(struct mlx5_ib_create_qp, reserved);
		}

	if (udata->inlen <= last)
		return 0;

	/*
	 * User provides different create_qp structures based on the
	 * flow and we need to know if he cleared memory after our
	 * struct create_qp ends.
	 */
	size = udata->inlen - last;
	ret = ib_is_udata_cleared(params->udata, last, size);
	if (!ret)
		mlx5_ib_dbg(
			dev,
			"udata is not cleared, inlen = %lu, ucmd = %lu, last = %lu, size = %lu\n",
			udata->inlen, params->ucmd_size, last, size);
	return ret ? 0 : -EINVAL;
}

2915
struct ib_qp *mlx5_ib_create_qp(struct ib_pd *pd, struct ib_qp_init_attr *attr,
2916 2917
				struct ib_udata *udata)
{
2918
	struct mlx5_create_qp_params params = {};
2919 2920
	struct mlx5_ib_dev *dev;
	struct mlx5_ib_qp *qp;
2921
	enum ib_qp_type type;
2922 2923
	int err;

2924
	dev = pd ? to_mdev(pd->device) :
2925
		   to_mdev(to_mxrcd(attr->xrcd)->ibxrcd.device);
2926

2927 2928
	err = check_qp_type(dev, attr, &type);
	if (err)
2929 2930
		return ERR_PTR(err);

2931
	err = check_valid_flow(dev, pd, attr, udata);
2932 2933
	if (err)
		return ERR_PTR(err);
2934

2935 2936
	if (attr->qp_type == IB_QPT_GSI)
		return mlx5_ib_gsi_create_qp(pd, attr);
2937

2938 2939 2940 2941
	params.udata = udata;
	params.uidx = MLX5_IB_DEFAULT_UIDX;
	params.attr = attr;
	params.is_rss_raw = !!attr->rwq_ind_tbl;
2942

2943
	if (udata) {
2944 2945 2946
		err = process_udata_size(dev, &params);
		if (err)
			return ERR_PTR(err);
2947

2948 2949 2950 2951 2952
		err = check_ucmd_data(dev, &params);
		if (err)
			return ERR_PTR(err);

		params.ucmd = kzalloc(params.ucmd_size, GFP_KERNEL);
2953
		if (!params.ucmd)
2954 2955
			return ERR_PTR(-ENOMEM);

2956
		err = ib_copy_from_udata(params.ucmd, udata, params.inlen);
2957
		if (err)
2958
			goto free_ucmd;
2959 2960
	}

2961
	qp = kzalloc(sizeof(*qp), GFP_KERNEL);
2962 2963 2964 2965
	if (!qp) {
		err = -ENOMEM;
		goto free_ucmd;
	}
2966

2967
	qp->type = type;
2968
	if (udata) {
2969
		err = process_vendor_flags(dev, qp, params.ucmd, attr);
2970
		if (err)
2971
			goto free_qp;
2972

2973
		err = get_qp_uidx(qp, &params);
2974 2975
		if (err)
			goto free_qp;
2976
	}
2977
	err = process_create_flags(dev, qp, attr);
2978 2979
	if (err)
		goto free_qp;
2980

2981
	err = check_qp_attr(dev, qp, attr);
2982 2983 2984
	if (err)
		goto free_qp;

2985 2986
	err = create_qp(dev, pd, qp, &params);
	if (err)
2987
		goto free_qp;
2988

2989
	kfree(params.ucmd);
2990
	params.ucmd = NULL;
2991

2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
	if (udata)
		/*
		 * It is safe to copy response for all user create QP flows,
		 * including MLX5_IB_QPT_DCT, which doesn't need it.
		 * In that case, resp will be filled with zeros.
		 */
		err = ib_copy_to_udata(udata, &params.resp, params.outlen);
	if (err)
		goto destroy_qp;

3002
	return &qp->ibqp;
3003

3004 3005 3006 3007 3008 3009
destroy_qp:
	if (qp->type == MLX5_IB_QPT_DCT)
		mlx5_ib_destroy_dct(qp);
	else
		destroy_qp_common(dev, qp, udata);
	qp = NULL;
3010 3011
free_qp:
	kfree(qp);
3012
free_ucmd:
3013
	kfree(params.ucmd);
3014
	return ERR_PTR(err);
3015 3016
}

3017
int mlx5_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
3018 3019 3020 3021
{
	struct mlx5_ib_dev *dev = to_mdev(qp->device);
	struct mlx5_ib_qp *mqp = to_mqp(qp);

H
Haggai Eran 已提交
3022 3023 3024
	if (unlikely(qp->qp_type == IB_QPT_GSI))
		return mlx5_ib_gsi_destroy_qp(qp);

3025
	if (mqp->type == MLX5_IB_QPT_DCT)
3026 3027
		return mlx5_ib_destroy_dct(mqp);

3028
	destroy_qp_common(dev, mqp, udata);
3029 3030 3031 3032 3033 3034

	kfree(mqp);

	return 0;
}

3035 3036 3037
static int set_qpc_atomic_flags(struct mlx5_ib_qp *qp,
				const struct ib_qp_attr *attr, int attr_mask,
				void *qpc)
3038
{
3039
	struct mlx5_ib_dev *dev = to_mdev(qp->ibqp.device);
3040 3041
	u8 dest_rd_atomic;
	u32 access_flags;
3042

3043 3044 3045
	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
		dest_rd_atomic = attr->max_dest_rd_atomic;
	else
3046
		dest_rd_atomic = qp->trans_qp.resp_depth;
3047 3048 3049 3050

	if (attr_mask & IB_QP_ACCESS_FLAGS)
		access_flags = attr->qp_access_flags;
	else
3051
		access_flags = qp->trans_qp.atomic_rd_en;
3052 3053 3054 3055

	if (!dest_rd_atomic)
		access_flags &= IB_ACCESS_REMOTE_WRITE;

3056 3057
	MLX5_SET(qpc, qpc, rre, !!(access_flags & IB_ACCESS_REMOTE_READ));

3058
	if (access_flags & IB_ACCESS_REMOTE_ATOMIC) {
3059 3060 3061 3062 3063 3064
		int atomic_mode;

		atomic_mode = get_atomic_mode(dev, qp->ibqp.qp_type);
		if (atomic_mode < 0)
			return -EOPNOTSUPP;

3065 3066
		MLX5_SET(qpc, qpc, rae, 1);
		MLX5_SET(qpc, qpc, atomic_mode, atomic_mode);
3067 3068
	}

3069
	MLX5_SET(qpc, qpc, rwe, !!(access_flags & IB_ACCESS_REMOTE_WRITE));
3070
	return 0;
3071 3072 3073 3074 3075 3076 3077 3078 3079 3080
}

enum {
	MLX5_PATH_FLAG_FL	= 1 << 0,
	MLX5_PATH_FLAG_FREE_AR	= 1 << 1,
	MLX5_PATH_FLAG_COUNTER	= 1 << 2,
};

static int ib_rate_to_mlx5(struct mlx5_ib_dev *dev, u8 rate)
{
3081
	if (rate == IB_RATE_PORT_CURRENT)
3082
		return 0;
3083

M
Michael Guralnik 已提交
3084
	if (rate < IB_RATE_2_5_GBPS || rate > IB_RATE_600_GBPS)
3085 3086
		return -EINVAL;

3087 3088 3089 3090 3091 3092
	while (rate != IB_RATE_PORT_CURRENT &&
	       !(1 << (rate + MLX5_STAT_RATE_OFFSET) &
		 MLX5_CAP_GEN(dev->mdev, stat_rate_support)))
		--rate;

	return rate ? rate + MLX5_STAT_RATE_OFFSET : rate;
3093 3094
}

3095
static int modify_raw_packet_eth_prio(struct mlx5_core_dev *dev,
3096 3097
				      struct mlx5_ib_sq *sq, u8 sl,
				      struct ib_pd *pd)
3098 3099 3100 3101 3102 3103 3104
{
	void *in;
	void *tisc;
	int inlen;
	int err;

	inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
3105
	in = kvzalloc(inlen, GFP_KERNEL);
3106 3107 3108 3109
	if (!in)
		return -ENOMEM;

	MLX5_SET(modify_tis_in, in, bitmask.prio, 1);
3110
	MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
3111 3112 3113 3114

	tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
	MLX5_SET(tisc, tisc, prio, ((sl & 0x7) << 1));

3115
	err = mlx5_core_modify_tis(dev, sq->tisn, in);
3116 3117 3118 3119 3120 3121

	kvfree(in);

	return err;
}

A
Aviv Heller 已提交
3122
static int modify_raw_packet_tx_affinity(struct mlx5_core_dev *dev,
3123 3124
					 struct mlx5_ib_sq *sq, u8 tx_affinity,
					 struct ib_pd *pd)
A
Aviv Heller 已提交
3125 3126 3127 3128 3129 3130 3131
{
	void *in;
	void *tisc;
	int inlen;
	int err;

	inlen = MLX5_ST_SZ_BYTES(modify_tis_in);
3132
	in = kvzalloc(inlen, GFP_KERNEL);
A
Aviv Heller 已提交
3133 3134 3135 3136
	if (!in)
		return -ENOMEM;

	MLX5_SET(modify_tis_in, in, bitmask.lag_tx_port_affinity, 1);
3137
	MLX5_SET(modify_tis_in, in, uid, to_mpd(pd)->uid);
A
Aviv Heller 已提交
3138 3139 3140 3141

	tisc = MLX5_ADDR_OF(modify_tis_in, in, ctx);
	MLX5_SET(tisc, tisc, lag_tx_port_affinity, tx_affinity);

3142
	err = mlx5_core_modify_tis(dev, sq->tisn, in);
A
Aviv Heller 已提交
3143 3144 3145 3146 3147 3148

	kvfree(in);

	return err;
}

3149
static void mlx5_set_path_udp_sport(void *path, const struct rdma_ah_attr *ah,
3150 3151 3152 3153 3154 3155 3156 3157
				    u32 lqpn, u32 rqpn)

{
	u32 fl = ah->grh.flow_label;

	if (!fl)
		fl = rdma_calc_flow_label(lqpn, rqpn);

3158
	MLX5_SET(ads, path, udp_sport, rdma_flow_label_to_udp_sport(fl));
3159 3160
}

3161
static int mlx5_set_path(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
3162 3163 3164
			 const struct rdma_ah_attr *ah, void *path, u8 port,
			 int attr_mask, u32 path_flags,
			 const struct ib_qp_attr *attr, bool alt)
3165
{
3166
	const struct ib_global_route *grh = rdma_ah_read_grh(ah);
3167
	int err;
3168
	enum ib_gid_type gid_type;
3169 3170
	u8 ah_flags = rdma_ah_get_ah_flags(ah);
	u8 sl = rdma_ah_get_sl(ah);
3171 3172

	if (attr_mask & IB_QP_PKEY_INDEX)
3173 3174
		MLX5_SET(ads, path, pkey_index,
			 alt ? attr->alt_pkey_index : attr->pkey_index);
3175

3176 3177
	if (ah_flags & IB_AH_GRH) {
		if (grh->sgid_index >=
3178
		    dev->mdev->port_caps[port - 1].gid_table_len) {
3179
			pr_err("sgid_index (%u) too large. max is %d\n",
3180
			       grh->sgid_index,
3181
			       dev->mdev->port_caps[port - 1].gid_table_len);
3182 3183
			return -EINVAL;
		}
3184
	}
3185 3186

	if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
3187
		if (!(ah_flags & IB_AH_GRH))
3188
			return -EINVAL;
3189

3190 3191
		ether_addr_copy(MLX5_ADDR_OF(ads, path, rmac_47_32),
				ah->roce.dmac);
3192 3193 3194 3195 3196 3197 3198 3199 3200
		if ((qp->ibqp.qp_type == IB_QPT_RC ||
		     qp->ibqp.qp_type == IB_QPT_UC ||
		     qp->ibqp.qp_type == IB_QPT_XRC_INI ||
		     qp->ibqp.qp_type == IB_QPT_XRC_TGT) &&
		    (grh->sgid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) &&
		    (attr_mask & IB_QP_DEST_QPN))
			mlx5_set_path_udp_sport(path, ah,
						qp->ibqp.qp_num,
						attr->dest_qp_num);
3201
		MLX5_SET(ads, path, eth_prio, sl & 0x7);
3202
		gid_type = ah->grh.sgid_attr->gid_type;
3203
		if (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP)
3204
			MLX5_SET(ads, path, dscp, grh->traffic_class >> 2);
3205
	} else {
3206 3207 3208 3209 3210 3211 3212
		MLX5_SET(ads, path, fl, !!(path_flags & MLX5_PATH_FLAG_FL));
		MLX5_SET(ads, path, free_ar,
			 !!(path_flags & MLX5_PATH_FLAG_FREE_AR));
		MLX5_SET(ads, path, rlid, rdma_ah_get_dlid(ah));
		MLX5_SET(ads, path, mlid, rdma_ah_get_path_bits(ah));
		MLX5_SET(ads, path, grh, !!(ah_flags & IB_AH_GRH));
		MLX5_SET(ads, path, sl, sl);
3213 3214
	}

3215
	if (ah_flags & IB_AH_GRH) {
3216 3217 3218 3219 3220 3221
		MLX5_SET(ads, path, src_addr_index, grh->sgid_index);
		MLX5_SET(ads, path, hop_limit, grh->hop_limit);
		MLX5_SET(ads, path, tclass, grh->traffic_class);
		MLX5_SET(ads, path, flow_label, grh->flow_label);
		memcpy(MLX5_ADDR_OF(ads, path, rgid_rip), grh->dgid.raw,
		       sizeof(grh->dgid.raw));
3222 3223
	}

3224
	err = ib_rate_to_mlx5(dev, rdma_ah_get_static_rate(ah));
3225 3226
	if (err < 0)
		return err;
3227 3228
	MLX5_SET(ads, path, stat_rate, err);
	MLX5_SET(ads, path, vhca_port_num, port);
3229 3230

	if (attr_mask & IB_QP_TIMEOUT)
3231 3232
		MLX5_SET(ads, path, ack_timeout,
			 alt ? attr->alt_timeout : attr->timeout);
3233

3234 3235 3236
	if ((qp->ibqp.qp_type == IB_QPT_RAW_PACKET) && qp->sq.wqe_cnt)
		return modify_raw_packet_eth_prio(dev->mdev,
						  &qp->raw_packet_qp.sq,
3237
						  sl & 0xf, qp->ibqp.pd);
3238

3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
	return 0;
}

static enum mlx5_qp_optpar opt_mask[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE][MLX5_QP_ST_MAX] = {
	[MLX5_QP_STATE_INIT] = {
		[MLX5_QP_STATE_INIT] = {
			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE		|
					  MLX5_QP_OPTPAR_RAE		|
					  MLX5_QP_OPTPAR_RWE		|
					  MLX5_QP_OPTPAR_PKEY_INDEX	|
3249 3250
					  MLX5_QP_OPTPAR_PRI_PORT	|
					  MLX5_QP_OPTPAR_LAG_TX_AFF,
3251 3252
			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE		|
					  MLX5_QP_OPTPAR_PKEY_INDEX	|
3253 3254
					  MLX5_QP_OPTPAR_PRI_PORT	|
					  MLX5_QP_OPTPAR_LAG_TX_AFF,
3255 3256 3257
			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX	|
					  MLX5_QP_OPTPAR_Q_KEY		|
					  MLX5_QP_OPTPAR_PRI_PORT,
3258 3259 3260 3261
			[MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE		|
					  MLX5_QP_OPTPAR_RAE		|
					  MLX5_QP_OPTPAR_RWE		|
					  MLX5_QP_OPTPAR_PKEY_INDEX	|
3262 3263
					  MLX5_QP_OPTPAR_PRI_PORT	|
					  MLX5_QP_OPTPAR_LAG_TX_AFF,
3264 3265 3266 3267 3268 3269
		},
		[MLX5_QP_STATE_RTR] = {
			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH  |
					  MLX5_QP_OPTPAR_RRE            |
					  MLX5_QP_OPTPAR_RAE            |
					  MLX5_QP_OPTPAR_RWE            |
3270 3271
					  MLX5_QP_OPTPAR_PKEY_INDEX	|
					  MLX5_QP_OPTPAR_LAG_TX_AFF,
3272 3273
			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH  |
					  MLX5_QP_OPTPAR_RWE            |
3274 3275
					  MLX5_QP_OPTPAR_PKEY_INDEX	|
					  MLX5_QP_OPTPAR_LAG_TX_AFF,
3276 3277 3278 3279
			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_PKEY_INDEX     |
					  MLX5_QP_OPTPAR_Q_KEY,
			[MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_PKEY_INDEX	|
					   MLX5_QP_OPTPAR_Q_KEY,
3280 3281 3282 3283
			[MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH |
					  MLX5_QP_OPTPAR_RRE            |
					  MLX5_QP_OPTPAR_RAE            |
					  MLX5_QP_OPTPAR_RWE            |
3284 3285
					  MLX5_QP_OPTPAR_PKEY_INDEX	|
					  MLX5_QP_OPTPAR_LAG_TX_AFF,
3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
		},
	},
	[MLX5_QP_STATE_RTR] = {
		[MLX5_QP_STATE_RTS] = {
			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH	|
					  MLX5_QP_OPTPAR_RRE		|
					  MLX5_QP_OPTPAR_RAE		|
					  MLX5_QP_OPTPAR_RWE		|
					  MLX5_QP_OPTPAR_PM_STATE	|
					  MLX5_QP_OPTPAR_RNR_TIMEOUT,
			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH	|
					  MLX5_QP_OPTPAR_RWE		|
					  MLX5_QP_OPTPAR_PM_STATE,
			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY,
3300 3301 3302 3303 3304 3305
			[MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_ALT_ADDR_PATH	|
					  MLX5_QP_OPTPAR_RRE		|
					  MLX5_QP_OPTPAR_RAE		|
					  MLX5_QP_OPTPAR_RWE		|
					  MLX5_QP_OPTPAR_PM_STATE	|
					  MLX5_QP_OPTPAR_RNR_TIMEOUT,
3306 3307 3308 3309 3310 3311 3312 3313
		},
	},
	[MLX5_QP_STATE_RTS] = {
		[MLX5_QP_STATE_RTS] = {
			[MLX5_QP_ST_RC] = MLX5_QP_OPTPAR_RRE		|
					  MLX5_QP_OPTPAR_RAE		|
					  MLX5_QP_OPTPAR_RWE		|
					  MLX5_QP_OPTPAR_RNR_TIMEOUT	|
3314 3315
					  MLX5_QP_OPTPAR_PM_STATE	|
					  MLX5_QP_OPTPAR_ALT_ADDR_PATH,
3316
			[MLX5_QP_ST_UC] = MLX5_QP_OPTPAR_RWE		|
3317 3318
					  MLX5_QP_OPTPAR_PM_STATE	|
					  MLX5_QP_OPTPAR_ALT_ADDR_PATH,
3319 3320 3321
			[MLX5_QP_ST_UD] = MLX5_QP_OPTPAR_Q_KEY		|
					  MLX5_QP_OPTPAR_SRQN		|
					  MLX5_QP_OPTPAR_CQN_RCV,
3322 3323 3324 3325 3326 3327
			[MLX5_QP_ST_XRC] = MLX5_QP_OPTPAR_RRE		|
					  MLX5_QP_OPTPAR_RAE		|
					  MLX5_QP_OPTPAR_RWE		|
					  MLX5_QP_OPTPAR_RNR_TIMEOUT	|
					  MLX5_QP_OPTPAR_PM_STATE	|
					  MLX5_QP_OPTPAR_ALT_ADDR_PATH,
3328 3329 3330 3331 3332 3333
		},
	},
	[MLX5_QP_STATE_SQER] = {
		[MLX5_QP_STATE_RTS] = {
			[MLX5_QP_ST_UD]	 = MLX5_QP_OPTPAR_Q_KEY,
			[MLX5_QP_ST_MLX] = MLX5_QP_OPTPAR_Q_KEY,
3334
			[MLX5_QP_ST_UC]	 = MLX5_QP_OPTPAR_RWE,
3335 3336 3337 3338
			[MLX5_QP_ST_RC]	 = MLX5_QP_OPTPAR_RNR_TIMEOUT	|
					   MLX5_QP_OPTPAR_RWE		|
					   MLX5_QP_OPTPAR_RAE		|
					   MLX5_QP_OPTPAR_RRE,
3339 3340 3341 3342
			[MLX5_QP_ST_XRC]  = MLX5_QP_OPTPAR_RNR_TIMEOUT	|
					   MLX5_QP_OPTPAR_RWE		|
					   MLX5_QP_OPTPAR_RAE		|
					   MLX5_QP_OPTPAR_RRE,
3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411
		},
	},
};

static int ib_nr_to_mlx5_nr(int ib_mask)
{
	switch (ib_mask) {
	case IB_QP_STATE:
		return 0;
	case IB_QP_CUR_STATE:
		return 0;
	case IB_QP_EN_SQD_ASYNC_NOTIFY:
		return 0;
	case IB_QP_ACCESS_FLAGS:
		return MLX5_QP_OPTPAR_RWE | MLX5_QP_OPTPAR_RRE |
			MLX5_QP_OPTPAR_RAE;
	case IB_QP_PKEY_INDEX:
		return MLX5_QP_OPTPAR_PKEY_INDEX;
	case IB_QP_PORT:
		return MLX5_QP_OPTPAR_PRI_PORT;
	case IB_QP_QKEY:
		return MLX5_QP_OPTPAR_Q_KEY;
	case IB_QP_AV:
		return MLX5_QP_OPTPAR_PRIMARY_ADDR_PATH |
			MLX5_QP_OPTPAR_PRI_PORT;
	case IB_QP_PATH_MTU:
		return 0;
	case IB_QP_TIMEOUT:
		return MLX5_QP_OPTPAR_ACK_TIMEOUT;
	case IB_QP_RETRY_CNT:
		return MLX5_QP_OPTPAR_RETRY_COUNT;
	case IB_QP_RNR_RETRY:
		return MLX5_QP_OPTPAR_RNR_RETRY;
	case IB_QP_RQ_PSN:
		return 0;
	case IB_QP_MAX_QP_RD_ATOMIC:
		return MLX5_QP_OPTPAR_SRA_MAX;
	case IB_QP_ALT_PATH:
		return MLX5_QP_OPTPAR_ALT_ADDR_PATH;
	case IB_QP_MIN_RNR_TIMER:
		return MLX5_QP_OPTPAR_RNR_TIMEOUT;
	case IB_QP_SQ_PSN:
		return 0;
	case IB_QP_MAX_DEST_RD_ATOMIC:
		return MLX5_QP_OPTPAR_RRA_MAX | MLX5_QP_OPTPAR_RWE |
			MLX5_QP_OPTPAR_RRE | MLX5_QP_OPTPAR_RAE;
	case IB_QP_PATH_MIG_STATE:
		return MLX5_QP_OPTPAR_PM_STATE;
	case IB_QP_CAP:
		return 0;
	case IB_QP_DEST_QPN:
		return 0;
	}
	return 0;
}

static int ib_mask_to_mlx5_opt(int ib_mask)
{
	int result = 0;
	int i;

	for (i = 0; i < 8 * sizeof(int); i++) {
		if ((1 << i) & ib_mask)
			result |= ib_nr_to_mlx5_nr(1 << i);
	}

	return result;
}

3412 3413 3414
static int modify_raw_packet_qp_rq(
	struct mlx5_ib_dev *dev, struct mlx5_ib_rq *rq, int new_state,
	const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
3415 3416 3417 3418 3419 3420 3421
{
	void *in;
	void *rqc;
	int inlen;
	int err;

	inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
3422
	in = kvzalloc(inlen, GFP_KERNEL);
3423 3424 3425 3426
	if (!in)
		return -ENOMEM;

	MLX5_SET(modify_rq_in, in, rq_state, rq->state);
3427
	MLX5_SET(modify_rq_in, in, uid, to_mpd(pd)->uid);
3428 3429 3430 3431

	rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
	MLX5_SET(rqc, rqc, state, new_state);

3432 3433 3434
	if (raw_qp_param->set_mask & MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID) {
		if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
			MLX5_SET64(modify_rq_in, in, modify_bitmask,
3435
				   MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
3436 3437
			MLX5_SET(rqc, rqc, counter_set_id, raw_qp_param->rq_q_ctr_id);
		} else
3438 3439 3440
			dev_info_once(
				&dev->ib_dev.dev,
				"RAW PACKET QP counters are not supported on current FW\n");
3441 3442
	}

3443
	err = mlx5_core_modify_rq(dev->mdev, rq->base.mqp.qpn, in);
3444 3445 3446 3447 3448 3449 3450 3451 3452 3453
	if (err)
		goto out;

	rq->state = new_state;

out:
	kvfree(in);
	return err;
}

3454 3455 3456
static int modify_raw_packet_qp_sq(
	struct mlx5_core_dev *dev, struct mlx5_ib_sq *sq, int new_state,
	const struct mlx5_modify_raw_qp_param *raw_qp_param, struct ib_pd *pd)
3457
{
3458
	struct mlx5_ib_qp *ibqp = sq->base.container_mibqp;
3459 3460 3461
	struct mlx5_rate_limit old_rl = ibqp->rl;
	struct mlx5_rate_limit new_rl = old_rl;
	bool new_rate_added = false;
3462
	u16 rl_index = 0;
3463 3464 3465 3466 3467 3468
	void *in;
	void *sqc;
	int inlen;
	int err;

	inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
3469
	in = kvzalloc(inlen, GFP_KERNEL);
3470 3471 3472
	if (!in)
		return -ENOMEM;

3473
	MLX5_SET(modify_sq_in, in, uid, to_mpd(pd)->uid);
3474 3475 3476 3477 3478
	MLX5_SET(modify_sq_in, in, sq_state, sq->state);

	sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
	MLX5_SET(sqc, sqc, state, new_state);

3479 3480 3481 3482 3483
	if (raw_qp_param->set_mask & MLX5_RAW_QP_RATE_LIMIT) {
		if (new_state != MLX5_SQC_STATE_RDY)
			pr_warn("%s: Rate limit can only be changed when SQ is moving to RDY\n",
				__func__);
		else
3484
			new_rl = raw_qp_param->rl;
3485 3486
	}

3487 3488 3489
	if (!mlx5_rl_are_equal(&old_rl, &new_rl)) {
		if (new_rl.rate) {
			err = mlx5_rl_add_rate(dev, &rl_index, &new_rl);
3490
			if (err) {
3491 3492 3493 3494 3495
				pr_err("Failed configuring rate limit(err %d): \
				       rate %u, max_burst_sz %u, typical_pkt_sz %u\n",
				       err, new_rl.rate, new_rl.max_burst_sz,
				       new_rl.typical_pkt_sz);

3496 3497
				goto out;
			}
3498
			new_rate_added = true;
3499 3500 3501
		}

		MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
3502
		/* index 0 means no limit */
3503 3504 3505
		MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
	}

3506
	err = mlx5_core_modify_sq(dev, sq->base.mqp.qpn, in);
3507 3508
	if (err) {
		/* Remove new rate from table if failed */
3509 3510
		if (new_rate_added)
			mlx5_rl_remove_rate(dev, &new_rl);
3511
		goto out;
3512 3513 3514
	}

	/* Only remove the old rate after new rate was set */
3515 3516
	if ((old_rl.rate && !mlx5_rl_are_equal(&old_rl, &new_rl)) ||
	    (new_state != MLX5_SQC_STATE_RDY)) {
3517
		mlx5_rl_remove_rate(dev, &old_rl);
3518 3519 3520
		if (new_state != MLX5_SQC_STATE_RDY)
			memset(&new_rl, 0, sizeof(new_rl));
	}
3521

3522
	ibqp->rl = new_rl;
3523 3524 3525 3526 3527 3528 3529 3530
	sq->state = new_state;

out:
	kvfree(in);
	return err;
}

static int modify_raw_packet_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
A
Aviv Heller 已提交
3531 3532
				const struct mlx5_modify_raw_qp_param *raw_qp_param,
				u8 tx_affinity)
3533 3534 3535 3536
{
	struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
	struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
	struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
3537 3538
	int modify_rq = !!qp->rq.wqe_cnt;
	int modify_sq = !!qp->sq.wqe_cnt;
3539 3540 3541 3542
	int rq_state;
	int sq_state;
	int err;

3543
	switch (raw_qp_param->operation) {
3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557
	case MLX5_CMD_OP_RST2INIT_QP:
		rq_state = MLX5_RQC_STATE_RDY;
		sq_state = MLX5_SQC_STATE_RDY;
		break;
	case MLX5_CMD_OP_2ERR_QP:
		rq_state = MLX5_RQC_STATE_ERR;
		sq_state = MLX5_SQC_STATE_ERR;
		break;
	case MLX5_CMD_OP_2RST_QP:
		rq_state = MLX5_RQC_STATE_RST;
		sq_state = MLX5_SQC_STATE_RST;
		break;
	case MLX5_CMD_OP_RTR2RTS_QP:
	case MLX5_CMD_OP_RTS2RTS_QP:
3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
		if (raw_qp_param->set_mask ==
		    MLX5_RAW_QP_RATE_LIMIT) {
			modify_rq = 0;
			sq_state = sq->state;
		} else {
			return raw_qp_param->set_mask ? -EINVAL : 0;
		}
		break;
	case MLX5_CMD_OP_INIT2INIT_QP:
	case MLX5_CMD_OP_INIT2RTR_QP:
3568 3569 3570 3571
		if (raw_qp_param->set_mask)
			return -EINVAL;
		else
			return 0;
3572 3573 3574 3575 3576
	default:
		WARN_ON(1);
		return -EINVAL;
	}

3577
	if (modify_rq) {
3578 3579
		err =  modify_raw_packet_qp_rq(dev, rq, rq_state, raw_qp_param,
					       qp->ibqp.pd);
3580 3581 3582 3583
		if (err)
			return err;
	}

3584
	if (modify_sq) {
3585 3586
		struct mlx5_flow_handle *flow_rule;

A
Aviv Heller 已提交
3587 3588
		if (tx_affinity) {
			err = modify_raw_packet_tx_affinity(dev->mdev, sq,
3589 3590
							    tx_affinity,
							    qp->ibqp.pd);
A
Aviv Heller 已提交
3591 3592 3593 3594
			if (err)
				return err;
		}

3595 3596 3597
		flow_rule = create_flow_rule_vport_sq(dev, sq,
						      raw_qp_param->port);
		if (IS_ERR(flow_rule))
3598
			return PTR_ERR(flow_rule);
3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613

		err = modify_raw_packet_qp_sq(dev->mdev, sq, sq_state,
					      raw_qp_param, qp->ibqp.pd);
		if (err) {
			if (flow_rule)
				mlx5_del_flow_rules(flow_rule);
			return err;
		}

		if (flow_rule) {
			destroy_flow_rule_vport_sq(sq);
			sq->flow_rule = flow_rule;
		}

		return err;
A
Aviv Heller 已提交
3614
	}
3615 3616 3617 3618

	return 0;
}

3619 3620
static unsigned int get_tx_affinity_rr(struct mlx5_ib_dev *dev,
				       struct ib_udata *udata)
3621
{
3622 3623
	struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
		udata, struct mlx5_ib_ucontext, ibucontext);
3624 3625
	u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
	atomic_t *tx_port_affinity;
3626

3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638
	if (ucontext)
		tx_port_affinity = &ucontext->tx_port_affinity;
	else
		tx_port_affinity = &dev->port[port_num].roce.tx_port_affinity;

	return (unsigned int)atomic_add_return(1, tx_port_affinity) %
		MLX5_MAX_PORTS + 1;
}

static bool qp_supports_affinity(struct ib_qp *qp)
{
	if ((qp->qp_type == IB_QPT_RC) ||
3639
	    (qp->qp_type == IB_QPT_UD) ||
3640 3641 3642 3643 3644 3645 3646 3647
	    (qp->qp_type == IB_QPT_UC) ||
	    (qp->qp_type == IB_QPT_RAW_PACKET) ||
	    (qp->qp_type == IB_QPT_XRC_INI) ||
	    (qp->qp_type == IB_QPT_XRC_TGT))
		return true;
	return false;
}

3648 3649 3650
static unsigned int get_tx_affinity(struct ib_qp *qp,
				    const struct ib_qp_attr *attr,
				    int attr_mask, u8 init,
3651 3652 3653 3654 3655 3656 3657 3658 3659
				    struct ib_udata *udata)
{
	struct mlx5_ib_ucontext *ucontext = rdma_udata_to_drv_context(
		udata, struct mlx5_ib_ucontext, ibucontext);
	struct mlx5_ib_dev *dev = to_mdev(qp->device);
	struct mlx5_ib_qp *mqp = to_mqp(qp);
	struct mlx5_ib_qp_base *qp_base;
	unsigned int tx_affinity;

3660 3661
	if (!(mlx5_ib_lag_should_assign_affinity(dev) &&
	      qp_supports_affinity(qp)))
3662 3663
		return 0;

3664 3665 3666 3667 3668 3669 3670 3671 3672
	if (mqp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
		tx_affinity = mqp->gsi_lag_port;
	else if (init)
		tx_affinity = get_tx_affinity_rr(dev, udata);
	else if ((attr_mask & IB_QP_AV) && attr->xmit_slave)
		tx_affinity =
			mlx5_lag_get_slave_port(dev->mdev, attr->xmit_slave);
	else
		return 0;
3673 3674 3675

	qp_base = &mqp->trans_qp.base;
	if (ucontext)
3676
		mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x ucontext %p\n",
3677 3678
			    tx_affinity, qp_base->mqp.qpn, ucontext);
	else
3679
		mlx5_ib_dbg(dev, "Set tx affinity 0x%x to qpn 0x%x\n",
3680 3681
			    tx_affinity, qp_base->mqp.qpn);
	return tx_affinity;
3682 3683
}

M
Mark Zhang 已提交
3684 3685 3686 3687
static int __mlx5_ib_qp_set_counter(struct ib_qp *qp,
				    struct rdma_counter *counter)
{
	struct mlx5_ib_dev *dev = to_mdev(qp->device);
3688
	u32 in[MLX5_ST_SZ_DW(rts2rts_qp_in)] = {};
M
Mark Zhang 已提交
3689 3690 3691
	struct mlx5_ib_qp *mqp = to_mqp(qp);
	struct mlx5_ib_qp_base *base;
	u32 set_id;
3692
	u32 *qpc;
M
Mark Zhang 已提交
3693

3694
	if (counter)
M
Mark Zhang 已提交
3695
		set_id = counter->id;
3696 3697
	else
		set_id = mlx5_ib_get_counters_id(dev, mqp->port - 1);
M
Mark Zhang 已提交
3698 3699

	base = &mqp->trans_qp.base;
3700 3701 3702 3703 3704 3705 3706 3707 3708
	MLX5_SET(rts2rts_qp_in, in, opcode, MLX5_CMD_OP_RTS2RTS_QP);
	MLX5_SET(rts2rts_qp_in, in, qpn, base->mqp.qpn);
	MLX5_SET(rts2rts_qp_in, in, uid, base->mqp.uid);
	MLX5_SET(rts2rts_qp_in, in, opt_param_mask,
		 MLX5_QP_OPTPAR_COUNTER_SET_ID);

	qpc = MLX5_ADDR_OF(rts2rts_qp_in, in, qpc);
	MLX5_SET(qpc, qpc, counter_set_id, set_id);
	return mlx5_cmd_exec_in(dev->mdev, rts2rts_qp, in);
M
Mark Zhang 已提交
3709 3710
}

3711 3712
static int __mlx5_ib_modify_qp(struct ib_qp *ibqp,
			       const struct ib_qp_attr *attr, int attr_mask,
3713 3714 3715
			       enum ib_qp_state cur_state,
			       enum ib_qp_state new_state,
			       const struct mlx5_ib_modify_qp *ucmd,
3716
			       struct mlx5_ib_modify_qp_resp *resp,
3717
			       struct ib_udata *udata)
3718
{
3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755
	static const u16 optab[MLX5_QP_NUM_STATE][MLX5_QP_NUM_STATE] = {
		[MLX5_QP_STATE_RST] = {
			[MLX5_QP_STATE_RST]	= MLX5_CMD_OP_2RST_QP,
			[MLX5_QP_STATE_ERR]	= MLX5_CMD_OP_2ERR_QP,
			[MLX5_QP_STATE_INIT]	= MLX5_CMD_OP_RST2INIT_QP,
		},
		[MLX5_QP_STATE_INIT]  = {
			[MLX5_QP_STATE_RST]	= MLX5_CMD_OP_2RST_QP,
			[MLX5_QP_STATE_ERR]	= MLX5_CMD_OP_2ERR_QP,
			[MLX5_QP_STATE_INIT]	= MLX5_CMD_OP_INIT2INIT_QP,
			[MLX5_QP_STATE_RTR]	= MLX5_CMD_OP_INIT2RTR_QP,
		},
		[MLX5_QP_STATE_RTR]   = {
			[MLX5_QP_STATE_RST]	= MLX5_CMD_OP_2RST_QP,
			[MLX5_QP_STATE_ERR]	= MLX5_CMD_OP_2ERR_QP,
			[MLX5_QP_STATE_RTS]	= MLX5_CMD_OP_RTR2RTS_QP,
		},
		[MLX5_QP_STATE_RTS]   = {
			[MLX5_QP_STATE_RST]	= MLX5_CMD_OP_2RST_QP,
			[MLX5_QP_STATE_ERR]	= MLX5_CMD_OP_2ERR_QP,
			[MLX5_QP_STATE_RTS]	= MLX5_CMD_OP_RTS2RTS_QP,
		},
		[MLX5_QP_STATE_SQD] = {
			[MLX5_QP_STATE_RST]	= MLX5_CMD_OP_2RST_QP,
			[MLX5_QP_STATE_ERR]	= MLX5_CMD_OP_2ERR_QP,
		},
		[MLX5_QP_STATE_SQER] = {
			[MLX5_QP_STATE_RST]	= MLX5_CMD_OP_2RST_QP,
			[MLX5_QP_STATE_ERR]	= MLX5_CMD_OP_2ERR_QP,
			[MLX5_QP_STATE_RTS]	= MLX5_CMD_OP_SQERR2RTS_QP,
		},
		[MLX5_QP_STATE_ERR] = {
			[MLX5_QP_STATE_RST]	= MLX5_CMD_OP_2RST_QP,
			[MLX5_QP_STATE_ERR]	= MLX5_CMD_OP_2ERR_QP,
		}
	};

3756 3757
	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
	struct mlx5_ib_qp *qp = to_mqp(ibqp);
3758
	struct mlx5_ib_qp_base *base = &qp->trans_qp.base;
3759 3760 3761
	struct mlx5_ib_cq *send_cq, *recv_cq;
	struct mlx5_ib_pd *pd;
	enum mlx5_qp_state mlx5_cur, mlx5_new;
3762
	void *qpc, *pri_path, *alt_path;
3763
	enum mlx5_qp_optpar optpar = 0;
M
Mark Zhang 已提交
3764
	u32 set_id = 0;
3765 3766
	int mlx5_st;
	int err;
3767
	u16 op;
A
Aviv Heller 已提交
3768
	u8 tx_affinity = 0;
3769

3770
	mlx5_st = to_mlx5_st(qp->type);
3771 3772 3773
	if (mlx5_st < 0)
		return -EINVAL;

3774 3775
	qpc = kzalloc(MLX5_ST_SZ_BYTES(qpc), GFP_KERNEL);
	if (!qpc)
3776 3777
		return -ENOMEM;

3778
	pd = to_mpd(qp->ibqp.pd);
3779
	MLX5_SET(qpc, qpc, st, mlx5_st);
3780 3781

	if (!(attr_mask & IB_QP_PATH_MIG_STATE)) {
3782
		MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
3783 3784 3785
	} else {
		switch (attr->path_mig_state) {
		case IB_MIG_MIGRATED:
3786
			MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_MIGRATED);
3787 3788
			break;
		case IB_MIG_REARM:
3789
			MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_REARM);
3790 3791
			break;
		case IB_MIG_ARMED:
3792
			MLX5_SET(qpc, qpc, pm_state, MLX5_QP_PM_ARMED);
3793 3794 3795 3796
			break;
		}
	}

3797
	tx_affinity = get_tx_affinity(ibqp, attr, attr_mask,
3798 3799
				      cur_state == IB_QPS_RESET &&
				      new_state == IB_QPS_INIT, udata);
3800 3801 3802 3803 3804

	MLX5_SET(qpc, qpc, lag_tx_port_affinity, tx_affinity);
	if (tx_affinity && new_state == IB_QPS_RTR &&
	    MLX5_CAP_GEN(dev->mdev, init2_lag_tx_port_affinity))
		optpar |= MLX5_QP_OPTPAR_LAG_TX_AFF;
A
Aviv Heller 已提交
3805

H
Haggai Eran 已提交
3806
	if (is_sqp(ibqp->qp_type)) {
3807 3808
		MLX5_SET(qpc, qpc, mtu, IB_MTU_256);
		MLX5_SET(qpc, qpc, log_msg_max, 8);
3809
	} else if ((ibqp->qp_type == IB_QPT_UD &&
3810
		    !(qp->flags & IB_QP_CREATE_SOURCE_QPN)) ||
3811
		   ibqp->qp_type == MLX5_IB_QPT_REG_UMR) {
3812 3813
		MLX5_SET(qpc, qpc, mtu, IB_MTU_4096);
		MLX5_SET(qpc, qpc, log_msg_max, 12);
3814 3815 3816 3817 3818 3819 3820
	} else if (attr_mask & IB_QP_PATH_MTU) {
		if (attr->path_mtu < IB_MTU_256 ||
		    attr->path_mtu > IB_MTU_4096) {
			mlx5_ib_warn(dev, "invalid mtu %d\n", attr->path_mtu);
			err = -EINVAL;
			goto out;
		}
3821 3822 3823
		MLX5_SET(qpc, qpc, mtu, attr->path_mtu);
		MLX5_SET(qpc, qpc, log_msg_max,
			 MLX5_CAP_GEN(dev->mdev, log_max_msg));
3824 3825 3826
	}

	if (attr_mask & IB_QP_DEST_QPN)
3827 3828 3829 3830
		MLX5_SET(qpc, qpc, remote_qpn, attr->dest_qp_num);

	pri_path = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
	alt_path = MLX5_ADDR_OF(qpc, qpc, secondary_address_path);
3831 3832

	if (attr_mask & IB_QP_PKEY_INDEX)
3833
		MLX5_SET(ads, pri_path, pkey_index, attr->pkey_index);
3834 3835 3836 3837

	/* todo implement counter_index functionality */

	if (is_sqp(ibqp->qp_type))
3838
		MLX5_SET(ads, pri_path, vhca_port_num, qp->port);
3839 3840

	if (attr_mask & IB_QP_PORT)
3841
		MLX5_SET(ads, pri_path, vhca_port_num, attr->port_num);
3842 3843

	if (attr_mask & IB_QP_AV) {
3844 3845 3846
		err = mlx5_set_path(dev, qp, &attr->ah_attr, pri_path,
				    attr_mask & IB_QP_PORT ? attr->port_num :
							     qp->port,
3847
				    attr_mask, 0, attr, false);
3848 3849 3850 3851 3852
		if (err)
			goto out;
	}

	if (attr_mask & IB_QP_TIMEOUT)
3853
		MLX5_SET(ads, pri_path, ack_timeout, attr->timeout);
3854 3855

	if (attr_mask & IB_QP_ALT_PATH) {
3856
		err = mlx5_set_path(dev, qp, &attr->alt_ah_attr, alt_path,
3857
				    attr->alt_port_num,
3858 3859
				    attr_mask | IB_QP_PKEY_INDEX |
					    IB_QP_TIMEOUT,
3860
				    0, attr, true);
3861 3862 3863 3864
		if (err)
			goto out;
	}

3865 3866
	get_cqs(qp->ibqp.qp_type, qp->ibqp.send_cq, qp->ibqp.recv_cq,
		&send_cq, &recv_cq);
3867

3868 3869 3870 3871 3872 3873 3874
	MLX5_SET(qpc, qpc, pd, pd ? pd->pdn : to_mpd(dev->devr.p0)->pdn);
	if (send_cq)
		MLX5_SET(qpc, qpc, cqn_snd, send_cq->mcq.cqn);
	if (recv_cq)
		MLX5_SET(qpc, qpc, cqn_rcv, recv_cq->mcq.cqn);

	MLX5_SET(qpc, qpc, log_ack_req_freq, MLX5_IB_ACK_REQ_FREQ);
3875 3876

	if (attr_mask & IB_QP_RNR_RETRY)
3877
		MLX5_SET(qpc, qpc, rnr_retry, attr->rnr_retry);
3878 3879

	if (attr_mask & IB_QP_RETRY_CNT)
3880
		MLX5_SET(qpc, qpc, retry_count, attr->retry_cnt);
3881

3882 3883
	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC && attr->max_rd_atomic)
		MLX5_SET(qpc, qpc, log_sra_max, ilog2(attr->max_rd_atomic));
3884 3885

	if (attr_mask & IB_QP_SQ_PSN)
3886
		MLX5_SET(qpc, qpc, next_send_psn, attr->sq_psn);
3887

3888 3889 3890
	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC && attr->max_dest_rd_atomic)
		MLX5_SET(qpc, qpc, log_rra_max,
			 ilog2(attr->max_dest_rd_atomic));
3891

3892
	if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
3893
		err = set_qpc_atomic_flags(qp, attr, attr_mask, qpc);
3894 3895 3896
		if (err)
			goto out;
	}
3897 3898

	if (attr_mask & IB_QP_MIN_RNR_TIMER)
3899
		MLX5_SET(qpc, qpc, min_rnr_nak, attr->min_rnr_timer);
3900 3901

	if (attr_mask & IB_QP_RQ_PSN)
3902
		MLX5_SET(qpc, qpc, next_rcv_psn, attr->rq_psn);
3903 3904

	if (attr_mask & IB_QP_QKEY)
3905
		MLX5_SET(qpc, qpc, q_key, attr->qkey);
3906 3907

	if (qp->rq.wqe_cnt && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3908
		MLX5_SET64(qpc, qpc, dbr_addr, qp->db.dma);
3909

M
Mark Bloch 已提交
3910 3911 3912
	if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
		u8 port_num = (attr_mask & IB_QP_PORT ? attr->port_num :
			       qp->port) - 1;
3913 3914

		/* Underlay port should be used - index 0 function per port */
3915
		if (qp->flags & IB_QP_CREATE_SOURCE_QPN)
3916 3917
			port_num = 0;

M
Mark Zhang 已提交
3918 3919 3920
		if (ibqp->counter)
			set_id = ibqp->counter->id;
		else
3921
			set_id = mlx5_ib_get_counters_id(dev, port_num);
3922
		MLX5_SET(qpc, qpc, counter_set_id, set_id);
M
Mark Bloch 已提交
3923 3924
	}

3925
	if (!ibqp->uobject && cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT)
3926
		MLX5_SET(qpc, qpc, rlky, 1);
3927

3928
	if (qp->flags & MLX5_IB_QP_CREATE_SQPN_QP1)
3929
		MLX5_SET(qpc, qpc, deth_sqpn, 1);
3930 3931 3932 3933

	mlx5_cur = to_mlx5_state(cur_state);
	mlx5_new = to_mlx5_state(new_state);

3934
	if (mlx5_cur >= MLX5_QP_NUM_STATE || mlx5_new >= MLX5_QP_NUM_STATE ||
3935 3936
	    !optab[mlx5_cur][mlx5_new]) {
		err = -EINVAL;
3937
		goto out;
3938
	}
3939 3940

	op = optab[mlx5_cur][mlx5_new];
3941
	optpar |= ib_mask_to_mlx5_opt(attr_mask);
3942
	optpar &= opt_mask[mlx5_cur][mlx5_new][mlx5_st];
3943

3944
	if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
3945
	    qp->flags & IB_QP_CREATE_SOURCE_QPN) {
3946 3947 3948
		struct mlx5_modify_raw_qp_param raw_qp_param = {};

		raw_qp_param.operation = op;
3949
		if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
M
Mark Zhang 已提交
3950
			raw_qp_param.rq_q_ctr_id = set_id;
3951 3952
			raw_qp_param.set_mask |= MLX5_RAW_QP_MOD_SET_RQ_Q_CTR_ID;
		}
3953

3954 3955 3956
		if (attr_mask & IB_QP_PORT)
			raw_qp_param.port = attr->port_num;

3957
		if (attr_mask & IB_QP_RATE_LIMIT) {
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981
			raw_qp_param.rl.rate = attr->rate_limit;

			if (ucmd->burst_info.max_burst_sz) {
				if (attr->rate_limit &&
				    MLX5_CAP_QOS(dev->mdev, packet_pacing_burst_bound)) {
					raw_qp_param.rl.max_burst_sz =
						ucmd->burst_info.max_burst_sz;
				} else {
					err = -EINVAL;
					goto out;
				}
			}

			if (ucmd->burst_info.typical_pkt_sz) {
				if (attr->rate_limit &&
				    MLX5_CAP_QOS(dev->mdev, packet_pacing_typical_size)) {
					raw_qp_param.rl.typical_pkt_sz =
						ucmd->burst_info.typical_pkt_sz;
				} else {
					err = -EINVAL;
					goto out;
				}
			}

3982 3983 3984
			raw_qp_param.set_mask |= MLX5_RAW_QP_RATE_LIMIT;
		}

A
Aviv Heller 已提交
3985
		err = modify_raw_packet_qp(dev, qp, &raw_qp_param, tx_affinity);
3986
	} else {
3987 3988 3989 3990 3991 3992 3993
		if (udata) {
			/* For the kernel flows, the resp will stay zero */
			resp->ece_options =
				MLX5_CAP_GEN(dev->mdev, ece_support) ?
					ucmd->ece_options : 0;
			resp->response_length = sizeof(*resp);
		}
3994
		err = mlx5_core_qp_modify(dev, op, optpar, qpc, &base->mqp,
3995
					  &resp->ece_options);
3996 3997
	}

3998 3999 4000 4001 4002 4003
	if (err)
		goto out;

	qp->state = new_state;

	if (attr_mask & IB_QP_ACCESS_FLAGS)
4004
		qp->trans_qp.atomic_rd_en = attr->qp_access_flags;
4005
	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
4006
		qp->trans_qp.resp_depth = attr->max_dest_rd_atomic;
4007 4008 4009
	if (attr_mask & IB_QP_PORT)
		qp->port = attr->port_num;
	if (attr_mask & IB_QP_ALT_PATH)
4010
		qp->trans_qp.alt_port = attr->alt_port_num;
4011 4012 4013 4014 4015

	/*
	 * If we moved a kernel QP to RESET, clean up all old CQ
	 * entries and reinitialize the QP.
	 */
4016 4017
	if (new_state == IB_QPS_RESET &&
	    !ibqp->uobject && ibqp->qp_type != IB_QPT_XRC_TGT) {
4018
		mlx5_ib_cq_clean(recv_cq, base->mqp.qpn,
4019 4020
				 ibqp->srq ? to_msrq(ibqp->srq) : NULL);
		if (send_cq != recv_cq)
4021
			mlx5_ib_cq_clean(send_cq, base->mqp.qpn, NULL);
4022 4023 4024 4025 4026 4027

		qp->rq.head = 0;
		qp->rq.tail = 0;
		qp->sq.head = 0;
		qp->sq.tail = 0;
		qp->sq.cur_post = 0;
4028 4029
		if (qp->sq.wqe_cnt)
			qp->sq.cur_edge = get_sq_edge(&qp->sq, 0);
4030
		qp->sq.last_poll = 0;
4031 4032 4033 4034
		qp->db.db[MLX5_RCV_DBR] = 0;
		qp->db.db[MLX5_SND_DBR] = 0;
	}

M
Mark Zhang 已提交
4035 4036 4037 4038 4039 4040
	if ((new_state == IB_QPS_RTS) && qp->counter_pending) {
		err = __mlx5_ib_qp_set_counter(ibqp, ibqp->counter);
		if (!err)
			qp->counter_pending = 0;
	}

4041
out:
4042
	kfree(qpc);
4043 4044 4045
	return err;
}

4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065
static inline bool is_valid_mask(int mask, int req, int opt)
{
	if ((mask & req) != req)
		return false;

	if (mask & ~(req | opt))
		return false;

	return true;
}

/* check valid transition for driver QP types
 * for now the only QP type that this function supports is DCI
 */
static bool modify_dci_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state new_state,
				enum ib_qp_attr_mask attr_mask)
{
	int req = IB_QP_STATE;
	int opt = 0;

4066 4067 4068
	if (new_state == IB_QPS_RESET) {
		return is_valid_mask(attr_mask, req, opt);
	} else if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
4069 4070 4071 4072 4073 4074 4075
		req |= IB_QP_PKEY_INDEX | IB_QP_PORT;
		return is_valid_mask(attr_mask, req, opt);
	} else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_INIT) {
		opt = IB_QP_PKEY_INDEX | IB_QP_PORT;
		return is_valid_mask(attr_mask, req, opt);
	} else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
		req |= IB_QP_PATH_MTU;
4076
		opt = IB_QP_PKEY_INDEX | IB_QP_AV;
4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091
		return is_valid_mask(attr_mask, req, opt);
	} else if (cur_state == IB_QPS_RTR && new_state == IB_QPS_RTS) {
		req |= IB_QP_TIMEOUT | IB_QP_RETRY_CNT | IB_QP_RNR_RETRY |
		       IB_QP_MAX_QP_RD_ATOMIC | IB_QP_SQ_PSN;
		opt = IB_QP_MIN_RNR_TIMER;
		return is_valid_mask(attr_mask, req, opt);
	} else if (cur_state == IB_QPS_RTS && new_state == IB_QPS_RTS) {
		opt = IB_QP_MIN_RNR_TIMER;
		return is_valid_mask(attr_mask, req, opt);
	} else if (cur_state != IB_QPS_RESET && new_state == IB_QPS_ERR) {
		return is_valid_mask(attr_mask, req, opt);
	}
	return false;
}

4092 4093 4094 4095 4096 4097 4098 4099
/* mlx5_ib_modify_dct: modify a DCT QP
 * valid transitions are:
 * RESET to INIT: must set access_flags, pkey_index and port
 * INIT  to RTR : must set min_rnr_timer, tclass, flow_label,
 *			   mtu, gid_index and hop_limit
 * Other transitions and attributes are illegal
 */
static int mlx5_ib_modify_dct(struct ib_qp *ibqp, struct ib_qp_attr *attr,
4100 4101
			      int attr_mask, struct mlx5_ib_modify_qp *ucmd,
			      struct ib_udata *udata)
4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116
{
	struct mlx5_ib_qp *qp = to_mqp(ibqp);
	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
	enum ib_qp_state cur_state, new_state;
	int err = 0;
	int required = IB_QP_STATE;
	void *dctc;

	if (!(attr_mask & IB_QP_STATE))
		return -EINVAL;

	cur_state = qp->state;
	new_state = attr->qp_state;

	dctc = MLX5_ADDR_OF(create_dct_in, qp->dct.in, dct_context_entry);
4117 4118 4119 4120 4121 4122 4123 4124 4125
	if (MLX5_CAP_GEN(dev->mdev, ece_support) && ucmd->ece_options)
		/*
		 * DCT doesn't initialize QP till modify command is executed,
		 * so we need to overwrite previously set ECE field if user
		 * provided any value except zero, which means not set/not
		 * valid.
		 */
		MLX5_SET(dctc, dctc, ece, ucmd->ece_options);

4126
	if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
4127 4128
		u16 set_id;

4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143
		required |= IB_QP_ACCESS_FLAGS | IB_QP_PKEY_INDEX | IB_QP_PORT;
		if (!is_valid_mask(attr_mask, required, 0))
			return -EINVAL;

		if (attr->port_num == 0 ||
		    attr->port_num > MLX5_CAP_GEN(dev->mdev, num_ports)) {
			mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
				    attr->port_num, dev->num_ports);
			return -EINVAL;
		}
		if (attr->qp_access_flags & IB_ACCESS_REMOTE_READ)
			MLX5_SET(dctc, dctc, rre, 1);
		if (attr->qp_access_flags & IB_ACCESS_REMOTE_WRITE)
			MLX5_SET(dctc, dctc, rwe, 1);
		if (attr->qp_access_flags & IB_ACCESS_REMOTE_ATOMIC) {
4144 4145 4146 4147
			int atomic_mode;

			atomic_mode = get_atomic_mode(dev, MLX5_IB_QPT_DCT);
			if (atomic_mode < 0)
4148
				return -EOPNOTSUPP;
4149 4150

			MLX5_SET(dctc, dctc, atomic_mode, atomic_mode);
4151 4152 4153 4154
			MLX5_SET(dctc, dctc, rae, 1);
		}
		MLX5_SET(dctc, dctc, pkey_index, attr->pkey_index);
		MLX5_SET(dctc, dctc, port, attr->port_num);
4155 4156 4157

		set_id = mlx5_ib_get_counters_id(dev, attr->port_num - 1);
		MLX5_SET(dctc, dctc, counter_set_id, set_id);
4158 4159
	} else if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
		struct mlx5_ib_modify_qp_resp resp = {};
4160 4161
		u32 out[MLX5_ST_SZ_DW(create_dct_out)] = {};
		u32 min_resp_len = offsetofend(typeof(resp), dctn);
4162 4163 4164 4165 4166

		if (udata->outlen < min_resp_len)
			return -EINVAL;
		resp.response_length = min_resp_len;

4167 4168 4169 4170 4171 4172 4173 4174
		/*
		 * If we don't have enough space for the ECE options,
		 * simply indicate it with resp.response_length.
		 */
		resp.response_length = (udata->outlen < sizeof(resp)) ?
					       min_resp_len :
					       sizeof(resp);

4175 4176 4177 4178 4179 4180 4181 4182 4183 4184
		required |= IB_QP_MIN_RNR_TIMER | IB_QP_AV | IB_QP_PATH_MTU;
		if (!is_valid_mask(attr_mask, required, 0))
			return -EINVAL;
		MLX5_SET(dctc, dctc, min_rnr_nak, attr->min_rnr_timer);
		MLX5_SET(dctc, dctc, tclass, attr->ah_attr.grh.traffic_class);
		MLX5_SET(dctc, dctc, flow_label, attr->ah_attr.grh.flow_label);
		MLX5_SET(dctc, dctc, mtu, attr->path_mtu);
		MLX5_SET(dctc, dctc, my_addr_index, attr->ah_attr.grh.sgid_index);
		MLX5_SET(dctc, dctc, hop_limit, attr->ah_attr.grh.hop_limit);

4185
		err = mlx5_core_create_dct(dev, &qp->dct.mdct, qp->dct.in,
4186 4187
					   MLX5_ST_SZ_BYTES(create_dct_in), out,
					   sizeof(out));
4188 4189 4190
		if (err)
			return err;
		resp.dctn = qp->dct.mdct.mqp.qpn;
4191 4192
		if (MLX5_CAP_GEN(dev->mdev, ece_support))
			resp.ece_options = MLX5_GET(create_dct_out, out, ece);
4193 4194
		err = ib_copy_to_udata(udata, &resp, resp.response_length);
		if (err) {
4195
			mlx5_core_destroy_dct(dev, &qp->dct.mdct);
4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208
			return err;
		}
	} else {
		mlx5_ib_warn(dev, "Modify DCT: Invalid transition from %d to %d\n", cur_state, new_state);
		return -EINVAL;
	}
	if (err)
		qp->state = IB_QPS_ERR;
	else
		qp->state = new_state;
	return err;
}

4209 4210 4211 4212
int mlx5_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
		      int attr_mask, struct ib_udata *udata)
{
	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
4213
	struct mlx5_ib_modify_qp_resp resp = {};
4214
	struct mlx5_ib_qp *qp = to_mqp(ibqp);
4215
	struct mlx5_ib_modify_qp ucmd = {};
H
Haggai Eran 已提交
4216
	enum ib_qp_type qp_type;
4217 4218 4219 4220
	enum ib_qp_state cur_state, new_state;
	int err = -EINVAL;
	int port;

Y
Yishai Hadas 已提交
4221 4222 4223
	if (ibqp->rwq_ind_tbl)
		return -ENOSYS;

4224
	if (udata && udata->inlen) {
4225
		if (udata->inlen < offsetofend(typeof(ucmd), ece_options))
4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240
			return -EINVAL;

		if (udata->inlen > sizeof(ucmd) &&
		    !ib_is_udata_cleared(udata, sizeof(ucmd),
					 udata->inlen - sizeof(ucmd)))
			return -EOPNOTSUPP;

		if (ib_copy_from_udata(&ucmd, udata,
				       min(udata->inlen, sizeof(ucmd))))
			return -EFAULT;

		if (ucmd.comp_mask ||
		    memchr_inv(&ucmd.burst_info.reserved, 0,
			       sizeof(ucmd.burst_info.reserved)))
			return -EOPNOTSUPP;
4241

4242 4243
	}

H
Haggai Eran 已提交
4244 4245 4246
	if (unlikely(ibqp->qp_type == IB_QPT_GSI))
		return mlx5_ib_gsi_modify_qp(ibqp, attr, attr_mask);

4247 4248
	qp_type = (unlikely(ibqp->qp_type == MLX5_IB_QPT_HW_GSI)) ? IB_QPT_GSI :
								    qp->type;
4249

4250 4251
	if (qp_type == MLX5_IB_QPT_DCT)
		return mlx5_ib_modify_dct(ibqp, attr, attr_mask, &ucmd, udata);
H
Haggai Eran 已提交
4252

4253 4254 4255 4256 4257
	mutex_lock(&qp->mutex);

	cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
	new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;

4258 4259 4260 4261
	if (!(cur_state == new_state && cur_state == IB_QPS_RESET)) {
		port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
	}

4262
	if (qp->flags & IB_QP_CREATE_SOURCE_QPN) {
4263 4264 4265 4266 4267 4268
		if (attr_mask & ~(IB_QP_STATE | IB_QP_CUR_STATE)) {
			mlx5_ib_dbg(dev, "invalid attr_mask 0x%x when underlay QP is used\n",
				    attr_mask);
			goto out;
		}
	} else if (qp_type != MLX5_IB_QPT_REG_UMR &&
4269
		   qp_type != MLX5_IB_QPT_DCI &&
4270 4271
		   !ib_modify_qp_is_ok(cur_state, new_state, qp_type,
				       attr_mask)) {
4272 4273
		mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
			    cur_state, new_state, ibqp->qp_type, attr_mask);
4274
		goto out;
4275 4276 4277 4278 4279
	} else if (qp_type == MLX5_IB_QPT_DCI &&
		   !modify_dci_qp_is_ok(cur_state, new_state, attr_mask)) {
		mlx5_ib_dbg(dev, "invalid QP state transition from %d to %d, qp_type %d, attr_mask 0x%x\n",
			    cur_state, new_state, qp_type, attr_mask);
		goto out;
4280
	}
4281 4282

	if ((attr_mask & IB_QP_PORT) &&
4283
	    (attr->port_num == 0 ||
4284
	     attr->port_num > dev->num_ports)) {
4285 4286
		mlx5_ib_dbg(dev, "invalid port number %d. number of ports is %d\n",
			    attr->port_num, dev->num_ports);
4287
		goto out;
4288
	}
4289 4290 4291

	if (attr_mask & IB_QP_PKEY_INDEX) {
		port = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
4292
		if (attr->pkey_index >=
4293 4294 4295
		    dev->mdev->port_caps[port - 1].pkey_table_len) {
			mlx5_ib_dbg(dev, "invalid pkey index %d\n",
				    attr->pkey_index);
4296
			goto out;
4297
		}
4298 4299 4300
	}

	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
4301
	    attr->max_rd_atomic >
4302 4303 4304
	    (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_res_qp))) {
		mlx5_ib_dbg(dev, "invalid max_rd_atomic value %d\n",
			    attr->max_rd_atomic);
4305
		goto out;
4306
	}
4307 4308

	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
4309
	    attr->max_dest_rd_atomic >
4310 4311 4312
	    (1 << MLX5_CAP_GEN(dev->mdev, log_max_ra_req_qp))) {
		mlx5_ib_dbg(dev, "invalid max_dest_rd_atomic value %d\n",
			    attr->max_dest_rd_atomic);
4313
		goto out;
4314
	}
4315 4316 4317 4318 4319 4320

	if (cur_state == new_state && cur_state == IB_QPS_RESET) {
		err = 0;
		goto out;
	}

4321
	err = __mlx5_ib_modify_qp(ibqp, attr, attr_mask, cur_state,
4322 4323 4324 4325 4326
				  new_state, &ucmd, &resp, udata);

	/* resp.response_length is set in ECE supported flows only */
	if (!err && resp.response_length &&
	    udata->outlen >= resp.response_length)
4327 4328
		/* Return -EFAULT to the user and expect him to destroy QP. */
		err = ib_copy_to_udata(udata, &resp, resp.response_length);
4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359

out:
	mutex_unlock(&qp->mutex);
	return err;
}

static inline enum ib_qp_state to_ib_qp_state(enum mlx5_qp_state mlx5_state)
{
	switch (mlx5_state) {
	case MLX5_QP_STATE_RST:      return IB_QPS_RESET;
	case MLX5_QP_STATE_INIT:     return IB_QPS_INIT;
	case MLX5_QP_STATE_RTR:      return IB_QPS_RTR;
	case MLX5_QP_STATE_RTS:      return IB_QPS_RTS;
	case MLX5_QP_STATE_SQ_DRAINING:
	case MLX5_QP_STATE_SQD:      return IB_QPS_SQD;
	case MLX5_QP_STATE_SQER:     return IB_QPS_SQE;
	case MLX5_QP_STATE_ERR:      return IB_QPS_ERR;
	default:		     return -1;
	}
}

static inline enum ib_mig_state to_ib_mig_state(int mlx5_mig_state)
{
	switch (mlx5_mig_state) {
	case MLX5_QP_PM_ARMED:		return IB_MIG_ARMED;
	case MLX5_QP_PM_REARM:		return IB_MIG_REARM;
	case MLX5_QP_PM_MIGRATED:	return IB_MIG_MIGRATED;
	default: return -1;
	}
}

4360
static void to_rdma_ah_attr(struct mlx5_ib_dev *ibdev,
4361
			    struct rdma_ah_attr *ah_attr, void *path)
4362
{
4363 4364
	int port = MLX5_GET(ads, path, vhca_port_num);
	int static_rate;
4365

4366
	memset(ah_attr, 0, sizeof(*ah_attr));
4367

4368
	if (!port || port > ibdev->num_ports)
4369 4370
		return;

4371
	ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port);
4372

4373 4374
	rdma_ah_set_port_num(ah_attr, port);
	rdma_ah_set_sl(ah_attr, MLX5_GET(ads, path, sl));
4375

4376 4377
	rdma_ah_set_dlid(ah_attr, MLX5_GET(ads, path, rlid));
	rdma_ah_set_path_bits(ah_attr, MLX5_GET(ads, path, mlid));
4378

4379 4380 4381
	static_rate = MLX5_GET(ads, path, stat_rate);
	rdma_ah_set_static_rate(ah_attr, static_rate ? static_rate - 5 : 0);
	if (MLX5_GET(ads, path, grh) ||
4382
	    ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE) {
4383 4384 4385 4386 4387 4388
		rdma_ah_set_grh(ah_attr, NULL, MLX5_GET(ads, path, flow_label),
				MLX5_GET(ads, path, src_addr_index),
				MLX5_GET(ads, path, hop_limit),
				MLX5_GET(ads, path, tclass));
		memcpy(ah_attr, MLX5_ADDR_OF(ads, path, rgid_rip),
		       MLX5_FLD_SZ_BYTES(ads, rgid_rip));
4389 4390 4391
	}
}

4392 4393 4394 4395 4396 4397
static int query_raw_packet_qp_sq_state(struct mlx5_ib_dev *dev,
					struct mlx5_ib_sq *sq,
					u8 *sq_state)
{
	int err;

4398
	err = mlx5_core_query_sq_state(dev->mdev, sq->base.mqp.qpn, sq_state);
4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416
	if (err)
		goto out;
	sq->state = *sq_state;

out:
	return err;
}

static int query_raw_packet_qp_rq_state(struct mlx5_ib_dev *dev,
					struct mlx5_ib_rq *rq,
					u8 *rq_state)
{
	void *out;
	void *rqc;
	int inlen;
	int err;

	inlen = MLX5_ST_SZ_BYTES(query_rq_out);
4417
	out = kvzalloc(inlen, GFP_KERNEL);
4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507
	if (!out)
		return -ENOMEM;

	err = mlx5_core_query_rq(dev->mdev, rq->base.mqp.qpn, out);
	if (err)
		goto out;

	rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
	*rq_state = MLX5_GET(rqc, rqc, state);
	rq->state = *rq_state;

out:
	kvfree(out);
	return err;
}

static int sqrq_state_to_qp_state(u8 sq_state, u8 rq_state,
				  struct mlx5_ib_qp *qp, u8 *qp_state)
{
	static const u8 sqrq_trans[MLX5_RQ_NUM_STATE][MLX5_SQ_NUM_STATE] = {
		[MLX5_RQC_STATE_RST] = {
			[MLX5_SQC_STATE_RST]	= IB_QPS_RESET,
			[MLX5_SQC_STATE_RDY]	= MLX5_QP_STATE_BAD,
			[MLX5_SQC_STATE_ERR]	= MLX5_QP_STATE_BAD,
			[MLX5_SQ_STATE_NA]	= IB_QPS_RESET,
		},
		[MLX5_RQC_STATE_RDY] = {
			[MLX5_SQC_STATE_RST]	= MLX5_QP_STATE_BAD,
			[MLX5_SQC_STATE_RDY]	= MLX5_QP_STATE,
			[MLX5_SQC_STATE_ERR]	= IB_QPS_SQE,
			[MLX5_SQ_STATE_NA]	= MLX5_QP_STATE,
		},
		[MLX5_RQC_STATE_ERR] = {
			[MLX5_SQC_STATE_RST]    = MLX5_QP_STATE_BAD,
			[MLX5_SQC_STATE_RDY]	= MLX5_QP_STATE_BAD,
			[MLX5_SQC_STATE_ERR]	= IB_QPS_ERR,
			[MLX5_SQ_STATE_NA]	= IB_QPS_ERR,
		},
		[MLX5_RQ_STATE_NA] = {
			[MLX5_SQC_STATE_RST]    = IB_QPS_RESET,
			[MLX5_SQC_STATE_RDY]	= MLX5_QP_STATE,
			[MLX5_SQC_STATE_ERR]	= MLX5_QP_STATE,
			[MLX5_SQ_STATE_NA]	= MLX5_QP_STATE_BAD,
		},
	};

	*qp_state = sqrq_trans[rq_state][sq_state];

	if (*qp_state == MLX5_QP_STATE_BAD) {
		WARN(1, "Buggy Raw Packet QP state, SQ 0x%x state: 0x%x, RQ 0x%x state: 0x%x",
		     qp->raw_packet_qp.sq.base.mqp.qpn, sq_state,
		     qp->raw_packet_qp.rq.base.mqp.qpn, rq_state);
		return -EINVAL;
	}

	if (*qp_state == MLX5_QP_STATE)
		*qp_state = qp->state;

	return 0;
}

static int query_raw_packet_qp_state(struct mlx5_ib_dev *dev,
				     struct mlx5_ib_qp *qp,
				     u8 *raw_packet_qp_state)
{
	struct mlx5_ib_raw_packet_qp *raw_packet_qp = &qp->raw_packet_qp;
	struct mlx5_ib_sq *sq = &raw_packet_qp->sq;
	struct mlx5_ib_rq *rq = &raw_packet_qp->rq;
	int err;
	u8 sq_state = MLX5_SQ_STATE_NA;
	u8 rq_state = MLX5_RQ_STATE_NA;

	if (qp->sq.wqe_cnt) {
		err = query_raw_packet_qp_sq_state(dev, sq, &sq_state);
		if (err)
			return err;
	}

	if (qp->rq.wqe_cnt) {
		err = query_raw_packet_qp_rq_state(dev, rq, &rq_state);
		if (err)
			return err;
	}

	return sqrq_state_to_qp_state(sq_state, rq_state, qp,
				      raw_packet_qp_state);
}

static int query_qp_attr(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *qp,
			 struct ib_qp_attr *qp_attr)
4508
{
4509
	int outlen = MLX5_ST_SZ_BYTES(query_qp_out);
4510
	void *qpc, *pri_path, *alt_path;
4511
	u32 *outb;
4512
	int err;
4513

4514
	outb = kzalloc(outlen, GFP_KERNEL);
4515 4516 4517
	if (!outb)
		return -ENOMEM;

4518
	err = mlx5_core_qp_query(dev, &qp->trans_qp.base.mqp, outb, outlen);
4519
	if (err)
4520
		goto out;
4521

4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533
	qpc = MLX5_ADDR_OF(query_qp_out, outb, qpc);

	qp->state = to_ib_qp_state(MLX5_GET(qpc, qpc, state));
	if (MLX5_GET(qpc, qpc, state) == MLX5_QP_STATE_SQ_DRAINING)
		qp_attr->sq_draining = 1;

	qp_attr->path_mtu = MLX5_GET(qpc, qpc, mtu);
	qp_attr->path_mig_state = to_ib_mig_state(MLX5_GET(qpc, qpc, pm_state));
	qp_attr->qkey = MLX5_GET(qpc, qpc, q_key);
	qp_attr->rq_psn = MLX5_GET(qpc, qpc, next_rcv_psn);
	qp_attr->sq_psn = MLX5_GET(qpc, qpc, next_send_psn);
	qp_attr->dest_qp_num = MLX5_GET(qpc, qpc, remote_qpn);
4534

4535 4536 4537 4538 4539 4540
	if (MLX5_GET(qpc, qpc, rre))
		qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_READ;
	if (MLX5_GET(qpc, qpc, rwe))
		qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_WRITE;
	if (MLX5_GET(qpc, qpc, rae))
		qp_attr->qp_access_flags |= IB_ACCESS_REMOTE_ATOMIC;
4541

4542 4543 4544 4545 4546 4547 4548 4549
	qp_attr->max_rd_atomic = 1 << MLX5_GET(qpc, qpc, log_sra_max);
	qp_attr->max_dest_rd_atomic = 1 << MLX5_GET(qpc, qpc, log_rra_max);
	qp_attr->min_rnr_timer = MLX5_GET(qpc, qpc, min_rnr_nak);
	qp_attr->retry_cnt = MLX5_GET(qpc, qpc, retry_count);
	qp_attr->rnr_retry = MLX5_GET(qpc, qpc, rnr_retry);

	pri_path = MLX5_ADDR_OF(qpc, qpc, primary_address_path);
	alt_path = MLX5_ADDR_OF(qpc, qpc, secondary_address_path);
4550 4551

	if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
4552 4553 4554 4555 4556 4557 4558 4559 4560 4561
		to_rdma_ah_attr(dev, &qp_attr->ah_attr, pri_path);
		to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, alt_path);
		qp_attr->alt_pkey_index = MLX5_GET(ads, alt_path, pkey_index);
		qp_attr->alt_port_num = MLX5_GET(ads, alt_path, vhca_port_num);
	}

	qp_attr->pkey_index = MLX5_GET(ads, pri_path, pkey_index);
	qp_attr->port_num = MLX5_GET(ads, pri_path, vhca_port_num);
	qp_attr->timeout = MLX5_GET(ads, pri_path, ack_timeout);
	qp_attr->alt_timeout = MLX5_GET(ads, alt_path, ack_timeout);
4562 4563 4564 4565 4566 4567

out:
	kfree(outb);
	return err;
}

4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594
static int mlx5_ib_dct_query_qp(struct mlx5_ib_dev *dev, struct mlx5_ib_qp *mqp,
				struct ib_qp_attr *qp_attr, int qp_attr_mask,
				struct ib_qp_init_attr *qp_init_attr)
{
	struct mlx5_core_dct	*dct = &mqp->dct.mdct;
	u32 *out;
	u32 access_flags = 0;
	int outlen = MLX5_ST_SZ_BYTES(query_dct_out);
	void *dctc;
	int err;
	int supported_mask = IB_QP_STATE |
			     IB_QP_ACCESS_FLAGS |
			     IB_QP_PORT |
			     IB_QP_MIN_RNR_TIMER |
			     IB_QP_AV |
			     IB_QP_PATH_MTU |
			     IB_QP_PKEY_INDEX;

	if (qp_attr_mask & ~supported_mask)
		return -EINVAL;
	if (mqp->state != IB_QPS_RTR)
		return -EINVAL;

	out = kzalloc(outlen, GFP_KERNEL);
	if (!out)
		return -ENOMEM;

4595
	err = mlx5_core_dct_query(dev, dct, out, outlen);
4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632
	if (err)
		goto out;

	dctc = MLX5_ADDR_OF(query_dct_out, out, dct_context_entry);

	if (qp_attr_mask & IB_QP_STATE)
		qp_attr->qp_state = IB_QPS_RTR;

	if (qp_attr_mask & IB_QP_ACCESS_FLAGS) {
		if (MLX5_GET(dctc, dctc, rre))
			access_flags |= IB_ACCESS_REMOTE_READ;
		if (MLX5_GET(dctc, dctc, rwe))
			access_flags |= IB_ACCESS_REMOTE_WRITE;
		if (MLX5_GET(dctc, dctc, rae))
			access_flags |= IB_ACCESS_REMOTE_ATOMIC;
		qp_attr->qp_access_flags = access_flags;
	}

	if (qp_attr_mask & IB_QP_PORT)
		qp_attr->port_num = MLX5_GET(dctc, dctc, port);
	if (qp_attr_mask & IB_QP_MIN_RNR_TIMER)
		qp_attr->min_rnr_timer = MLX5_GET(dctc, dctc, min_rnr_nak);
	if (qp_attr_mask & IB_QP_AV) {
		qp_attr->ah_attr.grh.traffic_class = MLX5_GET(dctc, dctc, tclass);
		qp_attr->ah_attr.grh.flow_label = MLX5_GET(dctc, dctc, flow_label);
		qp_attr->ah_attr.grh.sgid_index = MLX5_GET(dctc, dctc, my_addr_index);
		qp_attr->ah_attr.grh.hop_limit = MLX5_GET(dctc, dctc, hop_limit);
	}
	if (qp_attr_mask & IB_QP_PATH_MTU)
		qp_attr->path_mtu = MLX5_GET(dctc, dctc, mtu);
	if (qp_attr_mask & IB_QP_PKEY_INDEX)
		qp_attr->pkey_index = MLX5_GET(dctc, dctc, pkey_index);
out:
	kfree(out);
	return err;
}

4633 4634 4635 4636 4637 4638 4639 4640
int mlx5_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr,
		     int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr)
{
	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
	struct mlx5_ib_qp *qp = to_mqp(ibqp);
	int err = 0;
	u8 raw_packet_qp_state;

Y
Yishai Hadas 已提交
4641 4642 4643
	if (ibqp->rwq_ind_tbl)
		return -ENOSYS;

H
Haggai Eran 已提交
4644 4645 4646 4647
	if (unlikely(ibqp->qp_type == IB_QPT_GSI))
		return mlx5_ib_gsi_query_qp(ibqp, qp_attr, qp_attr_mask,
					    qp_init_attr);

4648 4649 4650 4651
	/* Not all of output fields are applicable, make sure to zero them */
	memset(qp_init_attr, 0, sizeof(*qp_init_attr));
	memset(qp_attr, 0, sizeof(*qp_attr));

4652
	if (unlikely(qp->type == MLX5_IB_QPT_DCT))
4653 4654 4655
		return mlx5_ib_dct_query_qp(dev, qp, qp_attr,
					    qp_attr_mask, qp_init_attr);

4656 4657
	mutex_lock(&qp->mutex);

4658
	if (qp->ibqp.qp_type == IB_QPT_RAW_PACKET ||
4659
	    qp->flags & IB_QP_CREATE_SOURCE_QPN) {
4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671
		err = query_raw_packet_qp_state(dev, qp, &raw_packet_qp_state);
		if (err)
			goto out;
		qp->state = raw_packet_qp_state;
		qp_attr->port_num = 1;
	} else {
		err = query_qp_attr(dev, qp, qp_attr);
		if (err)
			goto out;
	}

	qp_attr->qp_state	     = qp->state;
4672 4673 4674 4675 4676
	qp_attr->cur_qp_state	     = qp_attr->qp_state;
	qp_attr->cap.max_recv_wr     = qp->rq.wqe_cnt;
	qp_attr->cap.max_recv_sge    = qp->rq.max_gs;

	if (!ibqp->uobject) {
4677
		qp_attr->cap.max_send_wr  = qp->sq.max_post;
4678
		qp_attr->cap.max_send_sge = qp->sq.max_gs;
4679
		qp_init_attr->qp_context = ibqp->qp_context;
4680 4681 4682 4683 4684
	} else {
		qp_attr->cap.max_send_wr  = 0;
		qp_attr->cap.max_send_sge = 0;
	}

4685 4686 4687 4688 4689
	qp_init_attr->qp_type = ibqp->qp_type;
	qp_init_attr->recv_cq = ibqp->recv_cq;
	qp_init_attr->send_cq = ibqp->send_cq;
	qp_init_attr->srq = ibqp->srq;
	qp_attr->cap.max_inline_data = qp->max_inline_data;
4690 4691 4692

	qp_init_attr->cap	     = qp_attr->cap;

4693
	qp_init_attr->create_flags = qp->flags;
4694

4695 4696 4697 4698 4699 4700 4701 4702 4703
	qp_init_attr->sq_sig_type = qp->sq_signal_bits & MLX5_WQE_CTRL_CQ_UPDATE ?
		IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;

out:
	mutex_unlock(&qp->mutex);
	return err;
}

struct ib_xrcd *mlx5_ib_alloc_xrcd(struct ib_device *ibdev,
4704
				   struct ib_udata *udata)
4705 4706 4707 4708 4709
{
	struct mlx5_ib_dev *dev = to_mdev(ibdev);
	struct mlx5_ib_xrcd *xrcd;
	int err;

4710
	if (!MLX5_CAP_GEN(dev->mdev, xrc))
4711 4712 4713 4714 4715 4716
		return ERR_PTR(-ENOSYS);

	xrcd = kmalloc(sizeof(*xrcd), GFP_KERNEL);
	if (!xrcd)
		return ERR_PTR(-ENOMEM);

4717
	err = mlx5_cmd_xrcd_alloc(dev->mdev, &xrcd->xrcdn, 0);
4718 4719 4720 4721 4722 4723 4724 4725
	if (err) {
		kfree(xrcd);
		return ERR_PTR(-ENOMEM);
	}

	return &xrcd->ibxrcd;
}

4726
int mlx5_ib_dealloc_xrcd(struct ib_xrcd *xrcd, struct ib_udata *udata)
4727 4728 4729 4730 4731
{
	struct mlx5_ib_dev *dev = to_mdev(xrcd->device);
	u32 xrcdn = to_mxrcd(xrcd)->xrcdn;
	int err;

4732
	err = mlx5_cmd_xrcd_dealloc(dev->mdev, xrcdn, 0);
4733
	if (err)
4734 4735 4736 4737 4738
		mlx5_ib_warn(dev, "failed to dealloc xrcdn 0x%x\n", xrcdn);

	kfree(xrcd);
	return 0;
}
4739

4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761
static void mlx5_ib_wq_event(struct mlx5_core_qp *core_qp, int type)
{
	struct mlx5_ib_rwq *rwq = to_mibrwq(core_qp);
	struct mlx5_ib_dev *dev = to_mdev(rwq->ibwq.device);
	struct ib_event event;

	if (rwq->ibwq.event_handler) {
		event.device     = rwq->ibwq.device;
		event.element.wq = &rwq->ibwq;
		switch (type) {
		case MLX5_EVENT_TYPE_WQ_CATAS_ERROR:
			event.event = IB_EVENT_WQ_FATAL;
			break;
		default:
			mlx5_ib_warn(dev, "Unexpected event type %d on WQ %06x\n", type, core_qp->qpn);
			return;
		}

		rwq->ibwq.event_handler(&event, rwq->ibwq.wq_context);
	}
}

4762 4763 4764 4765 4766 4767 4768 4769
static int set_delay_drop(struct mlx5_ib_dev *dev)
{
	int err = 0;

	mutex_lock(&dev->delay_drop.lock);
	if (dev->delay_drop.activate)
		goto out;

4770
	err = mlx5_core_set_delay_drop(dev, dev->delay_drop.timeout);
4771 4772 4773 4774 4775 4776
	if (err)
		goto out;

	dev->delay_drop.activate = true;
out:
	mutex_unlock(&dev->delay_drop.lock);
4777 4778 4779

	if (!err)
		atomic_inc(&dev->delay_drop.rqs_cnt);
4780 4781 4782
	return err;
}

4783 4784 4785 4786
static int  create_rq(struct mlx5_ib_rwq *rwq, struct ib_pd *pd,
		      struct ib_wq_init_attr *init_attr)
{
	struct mlx5_ib_dev *dev;
4787
	int has_net_offloads;
4788 4789 4790 4791 4792 4793 4794 4795 4796 4797
	__be64 *rq_pas0;
	void *in;
	void *rqc;
	void *wq;
	int inlen;
	int err;

	dev = to_mdev(pd->device);

	inlen = MLX5_ST_SZ_BYTES(create_rq_in) + sizeof(u64) * rwq->rq_num_pas;
4798
	in = kvzalloc(inlen, GFP_KERNEL);
4799 4800 4801
	if (!in)
		return -ENOMEM;

4802
	MLX5_SET(create_rq_in, in, uid, to_mpd(pd)->uid);
4803 4804 4805 4806 4807 4808 4809 4810
	rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
	MLX5_SET(rqc,  rqc, mem_rq_type,
		 MLX5_RQC_MEM_RQ_TYPE_MEMORY_RQ_INLINE);
	MLX5_SET(rqc, rqc, user_index, rwq->user_index);
	MLX5_SET(rqc,  rqc, cqn, to_mcq(init_attr->cq)->mcq.cqn);
	MLX5_SET(rqc,  rqc, state, MLX5_RQC_STATE_RST);
	MLX5_SET(rqc,  rqc, flush_in_error_en, 1);
	wq = MLX5_ADDR_OF(rqc, rqc, wq);
4811 4812 4813
	MLX5_SET(wq, wq, wq_type,
		 rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ ?
		 MLX5_WQ_TYPE_CYCLIC_STRIDING_RQ : MLX5_WQ_TYPE_CYCLIC);
4814 4815 4816 4817 4818 4819 4820 4821 4822
	if (init_attr->create_flags & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
		if (!MLX5_CAP_GEN(dev->mdev, end_pad)) {
			mlx5_ib_dbg(dev, "Scatter end padding is not supported\n");
			err = -EOPNOTSUPP;
			goto out;
		} else {
			MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
		}
	}
4823
	MLX5_SET(wq, wq, log_wq_stride, rwq->log_rq_stride);
4824
	if (rwq->create_flags & MLX5_IB_WQ_FLAGS_STRIDING_RQ) {
4825 4826 4827 4828 4829 4830 4831 4832
		/*
		 * In Firmware number of strides in each WQE is:
		 *   "512 * 2^single_wqe_log_num_of_strides"
		 * Values 3 to 8 are accepted as 10 to 15, 9 to 18 are
		 * accepted as 0 to 9
		 */
		static const u8 fw_map[] = { 10, 11, 12, 13, 14, 15, 0, 1,
					     2,  3,  4,  5,  6,  7,  8, 9 };
4833 4834 4835 4836
		MLX5_SET(wq, wq, two_byte_shift_en, rwq->two_byte_shift_en);
		MLX5_SET(wq, wq, log_wqe_stride_size,
			 rwq->single_stride_log_num_of_bytes -
			 MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES);
4837 4838 4839
		MLX5_SET(wq, wq, log_wqe_num_of_strides,
			 fw_map[rwq->log_num_strides -
				MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES]);
4840
	}
4841 4842 4843 4844 4845 4846
	MLX5_SET(wq, wq, log_wq_sz, rwq->log_rq_size);
	MLX5_SET(wq, wq, pd, to_mpd(pd)->pdn);
	MLX5_SET(wq, wq, page_offset, rwq->rq_page_offset);
	MLX5_SET(wq, wq, log_wq_pg_sz, rwq->log_page_size);
	MLX5_SET(wq, wq, wq_signature, rwq->wq_sig);
	MLX5_SET64(wq, wq, dbr_addr, rwq->db.dma);
4847
	has_net_offloads = MLX5_CAP_GEN(dev->mdev, eth_net_offloads);
4848
	if (init_attr->create_flags & IB_WQ_FLAGS_CVLAN_STRIPPING) {
4849
		if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
4850 4851 4852 4853 4854 4855 4856
			mlx5_ib_dbg(dev, "VLAN offloads are not supported\n");
			err = -EOPNOTSUPP;
			goto out;
		}
	} else {
		MLX5_SET(rqc, rqc, vsd, 1);
	}
4857 4858 4859 4860 4861 4862 4863 4864
	if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS) {
		if (!(has_net_offloads && MLX5_CAP_ETH(dev->mdev, scatter_fcs))) {
			mlx5_ib_dbg(dev, "Scatter FCS is not supported\n");
			err = -EOPNOTSUPP;
			goto out;
		}
		MLX5_SET(rqc, rqc, scatter_fcs, 1);
	}
4865 4866 4867 4868 4869 4870 4871 4872 4873
	if (init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
		if (!(dev->ib_dev.attrs.raw_packet_caps &
		      IB_RAW_PACKET_CAP_DELAY_DROP)) {
			mlx5_ib_dbg(dev, "Delay drop is not supported\n");
			err = -EOPNOTSUPP;
			goto out;
		}
		MLX5_SET(rqc, rqc, delay_drop_en, 1);
	}
4874 4875
	rq_pas0 = (__be64 *)MLX5_ADDR_OF(wq, wq, pas);
	mlx5_ib_populate_pas(dev, rwq->umem, rwq->page_shift, rq_pas0, 0);
4876
	err = mlx5_core_create_rq_tracked(dev, in, inlen, &rwq->core_qp);
4877 4878 4879 4880 4881
	if (!err && init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP) {
		err = set_delay_drop(dev);
		if (err) {
			mlx5_ib_warn(dev, "Failed to enable delay drop err=%d\n",
				     err);
4882
			mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
4883 4884 4885 4886
		} else {
			rwq->create_flags |= MLX5_IB_WQ_FLAGS_DELAY_DROP;
		}
	}
4887
out:
4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905
	kvfree(in);
	return err;
}

static int set_user_rq_size(struct mlx5_ib_dev *dev,
			    struct ib_wq_init_attr *wq_init_attr,
			    struct mlx5_ib_create_wq *ucmd,
			    struct mlx5_ib_rwq *rwq)
{
	/* Sanity check RQ size before proceeding */
	if (wq_init_attr->max_wr > (1 << MLX5_CAP_GEN(dev->mdev, log_max_wq_sz)))
		return -EINVAL;

	if (!ucmd->rq_wqe_count)
		return -EINVAL;

	rwq->wqe_count = ucmd->rq_wqe_count;
	rwq->wqe_shift = ucmd->rq_wqe_shift;
4906 4907 4908
	if (check_shl_overflow(rwq->wqe_count, rwq->wqe_shift, &rwq->buf_size))
		return -EINVAL;

4909 4910 4911 4912 4913
	rwq->log_rq_stride = rwq->wqe_shift;
	rwq->log_rq_size = ilog2(rwq->wqe_count);
	return 0;
}

4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926
static bool log_of_strides_valid(struct mlx5_ib_dev *dev, u32 log_num_strides)
{
	if ((log_num_strides > MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES) ||
	    (log_num_strides < MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
		return false;

	if (!MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) &&
	    (log_num_strides < MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES))
		return false;

	return true;
}

4927 4928 4929 4930 4931 4932 4933 4934 4935 4936
static int prepare_user_rq(struct ib_pd *pd,
			   struct ib_wq_init_attr *init_attr,
			   struct ib_udata *udata,
			   struct mlx5_ib_rwq *rwq)
{
	struct mlx5_ib_dev *dev = to_mdev(pd->device);
	struct mlx5_ib_create_wq ucmd = {};
	int err;
	size_t required_cmd_sz;

4937 4938
	required_cmd_sz = offsetof(typeof(ucmd), single_stride_log_num_of_bytes)
		+ sizeof(ucmd.single_stride_log_num_of_bytes);
4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955
	if (udata->inlen < required_cmd_sz) {
		mlx5_ib_dbg(dev, "invalid inlen\n");
		return -EINVAL;
	}

	if (udata->inlen > sizeof(ucmd) &&
	    !ib_is_udata_cleared(udata, sizeof(ucmd),
				 udata->inlen - sizeof(ucmd))) {
		mlx5_ib_dbg(dev, "inlen is not supported\n");
		return -EOPNOTSUPP;
	}

	if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
		mlx5_ib_dbg(dev, "copy failed\n");
		return -EFAULT;
	}

4956
	if (ucmd.comp_mask & (~MLX5_IB_CREATE_WQ_STRIDING_RQ)) {
4957 4958
		mlx5_ib_dbg(dev, "invalid comp mask\n");
		return -EOPNOTSUPP;
4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973
	} else if (ucmd.comp_mask & MLX5_IB_CREATE_WQ_STRIDING_RQ) {
		if (!MLX5_CAP_GEN(dev->mdev, striding_rq)) {
			mlx5_ib_dbg(dev, "Striding RQ is not supported\n");
			return -EOPNOTSUPP;
		}
		if ((ucmd.single_stride_log_num_of_bytes <
		    MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES) ||
		    (ucmd.single_stride_log_num_of_bytes >
		     MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES)) {
			mlx5_ib_dbg(dev, "Invalid log stride size (%u. Range is %u - %u)\n",
				    ucmd.single_stride_log_num_of_bytes,
				    MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES,
				    MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES);
			return -EINVAL;
		}
4974 4975 4976 4977 4978 4979 4980 4981 4982 4983
		if (!log_of_strides_valid(dev,
					  ucmd.single_wqe_log_num_of_strides)) {
			mlx5_ib_dbg(
				dev,
				"Invalid log num strides (%u. Range is %u - %u)\n",
				ucmd.single_wqe_log_num_of_strides,
				MLX5_CAP_GEN(dev->mdev, ext_stride_num_range) ?
					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES :
					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES,
				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES);
4984 4985 4986 4987 4988 4989 4990
			return -EINVAL;
		}
		rwq->single_stride_log_num_of_bytes =
			ucmd.single_stride_log_num_of_bytes;
		rwq->log_num_strides = ucmd.single_wqe_log_num_of_strides;
		rwq->two_byte_shift_en = !!ucmd.two_byte_shift_en;
		rwq->create_flags |= MLX5_IB_WQ_FLAGS_STRIDING_RQ;
4991 4992 4993 4994 4995 4996 4997 4998
	}

	err = set_user_rq_size(dev, init_attr, &ucmd, rwq);
	if (err) {
		mlx5_ib_dbg(dev, "err %d\n", err);
		return err;
	}

4999
	err = create_user_rq(dev, pd, udata, rwq, &ucmd);
5000 5001
	if (err) {
		mlx5_ib_dbg(dev, "err %d\n", err);
5002
		return err;
5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025
	}

	rwq->user_index = ucmd.user_index;
	return 0;
}

struct ib_wq *mlx5_ib_create_wq(struct ib_pd *pd,
				struct ib_wq_init_attr *init_attr,
				struct ib_udata *udata)
{
	struct mlx5_ib_dev *dev;
	struct mlx5_ib_rwq *rwq;
	struct mlx5_ib_create_wq_resp resp = {};
	size_t min_resp_len;
	int err;

	if (!udata)
		return ERR_PTR(-ENOSYS);

	min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
	if (udata->outlen && udata->outlen < min_resp_len)
		return ERR_PTR(-EINVAL);

5026 5027 5028 5029
	if (!capable(CAP_SYS_RAWIO) &&
	    init_attr->create_flags & IB_WQ_FLAGS_DELAY_DROP)
		return ERR_PTR(-EPERM);

5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048
	dev = to_mdev(pd->device);
	switch (init_attr->wq_type) {
	case IB_WQT_RQ:
		rwq = kzalloc(sizeof(*rwq), GFP_KERNEL);
		if (!rwq)
			return ERR_PTR(-ENOMEM);
		err = prepare_user_rq(pd, init_attr, udata, rwq);
		if (err)
			goto err;
		err = create_rq(rwq, pd, init_attr);
		if (err)
			goto err_user_rq;
		break;
	default:
		mlx5_ib_dbg(dev, "unsupported wq type %d\n",
			    init_attr->wq_type);
		return ERR_PTR(-EINVAL);
	}

5049
	rwq->ibwq.wq_num = rwq->core_qp.qpn;
5050 5051 5052 5053 5054 5055 5056 5057 5058
	rwq->ibwq.state = IB_WQS_RESET;
	if (udata->outlen) {
		resp.response_length = offsetof(typeof(resp), response_length) +
				sizeof(resp.response_length);
		err = ib_copy_to_udata(udata, &resp, resp.response_length);
		if (err)
			goto err_copy;
	}

5059 5060
	rwq->core_qp.event = mlx5_ib_wq_event;
	rwq->ibwq.event_handler = init_attr->event_handler;
5061 5062 5063
	return &rwq->ibwq;

err_copy:
5064
	mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
5065
err_user_rq:
5066
	destroy_user_rq(dev, pd, rwq, udata);
5067 5068 5069 5070 5071
err:
	kfree(rwq);
	return ERR_PTR(err);
}

5072
void mlx5_ib_destroy_wq(struct ib_wq *wq, struct ib_udata *udata)
5073 5074 5075 5076
{
	struct mlx5_ib_dev *dev = to_mdev(wq->device);
	struct mlx5_ib_rwq *rwq = to_mrwq(wq);

5077
	mlx5_core_destroy_rq_tracked(dev, &rwq->core_qp);
5078
	destroy_user_rq(dev, wq->pd, rwq, udata);
5079 5080 5081
	kfree(rwq);
}

5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101
struct ib_rwq_ind_table *mlx5_ib_create_rwq_ind_table(struct ib_device *device,
						      struct ib_rwq_ind_table_init_attr *init_attr,
						      struct ib_udata *udata)
{
	struct mlx5_ib_dev *dev = to_mdev(device);
	struct mlx5_ib_rwq_ind_table *rwq_ind_tbl;
	int sz = 1 << init_attr->log_ind_tbl_size;
	struct mlx5_ib_create_rwq_ind_tbl_resp resp = {};
	size_t min_resp_len;
	int inlen;
	int err;
	int i;
	u32 *in;
	void *rqtc;

	if (udata->inlen > 0 &&
	    !ib_is_udata_cleared(udata, 0,
				 udata->inlen))
		return ERR_PTR(-EOPNOTSUPP);

5102 5103 5104 5105 5106 5107 5108 5109
	if (init_attr->log_ind_tbl_size >
	    MLX5_CAP_GEN(dev->mdev, log_max_rqt_size)) {
		mlx5_ib_dbg(dev, "log_ind_tbl_size = %d is bigger than supported = %d\n",
			    init_attr->log_ind_tbl_size,
			    MLX5_CAP_GEN(dev->mdev, log_max_rqt_size));
		return ERR_PTR(-EINVAL);
	}

5110 5111 5112 5113 5114 5115 5116 5117 5118
	min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
	if (udata->outlen && udata->outlen < min_resp_len)
		return ERR_PTR(-EINVAL);

	rwq_ind_tbl = kzalloc(sizeof(*rwq_ind_tbl), GFP_KERNEL);
	if (!rwq_ind_tbl)
		return ERR_PTR(-ENOMEM);

	inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
5119
	in = kvzalloc(inlen, GFP_KERNEL);
5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132
	if (!in) {
		err = -ENOMEM;
		goto err;
	}

	rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);

	MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
	MLX5_SET(rqtc, rqtc, rqt_max_size, sz);

	for (i = 0; i < sz; i++)
		MLX5_SET(rqtc, rqtc, rq_num[i], init_attr->ind_tbl[i]->wq_num);

5133 5134 5135
	rwq_ind_tbl->uid = to_mpd(init_attr->ind_tbl[0]->pd)->uid;
	MLX5_SET(create_rqt_in, in, uid, rwq_ind_tbl->uid);

5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153
	err = mlx5_core_create_rqt(dev->mdev, in, inlen, &rwq_ind_tbl->rqtn);
	kvfree(in);

	if (err)
		goto err;

	rwq_ind_tbl->ib_rwq_ind_tbl.ind_tbl_num = rwq_ind_tbl->rqtn;
	if (udata->outlen) {
		resp.response_length = offsetof(typeof(resp), response_length) +
					sizeof(resp.response_length);
		err = ib_copy_to_udata(udata, &resp, resp.response_length);
		if (err)
			goto err_copy;
	}

	return &rwq_ind_tbl->ib_rwq_ind_tbl;

err_copy:
5154
	mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
5155 5156 5157 5158 5159 5160 5161 5162 5163 5164
err:
	kfree(rwq_ind_tbl);
	return ERR_PTR(err);
}

int mlx5_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
{
	struct mlx5_ib_rwq_ind_table *rwq_ind_tbl = to_mrwq_ind_table(ib_rwq_ind_tbl);
	struct mlx5_ib_dev *dev = to_mdev(ib_rwq_ind_tbl->device);

5165
	mlx5_cmd_destroy_rqt(dev->mdev, rwq_ind_tbl->rqtn, rwq_ind_tbl->uid);
5166 5167 5168 5169 5170

	kfree(rwq_ind_tbl);
	return 0;
}

5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200
int mlx5_ib_modify_wq(struct ib_wq *wq, struct ib_wq_attr *wq_attr,
		      u32 wq_attr_mask, struct ib_udata *udata)
{
	struct mlx5_ib_dev *dev = to_mdev(wq->device);
	struct mlx5_ib_rwq *rwq = to_mrwq(wq);
	struct mlx5_ib_modify_wq ucmd = {};
	size_t required_cmd_sz;
	int curr_wq_state;
	int wq_state;
	int inlen;
	int err;
	void *rqc;
	void *in;

	required_cmd_sz = offsetof(typeof(ucmd), reserved) + sizeof(ucmd.reserved);
	if (udata->inlen < required_cmd_sz)
		return -EINVAL;

	if (udata->inlen > sizeof(ucmd) &&
	    !ib_is_udata_cleared(udata, sizeof(ucmd),
				 udata->inlen - sizeof(ucmd)))
		return -EOPNOTSUPP;

	if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
		return -EFAULT;

	if (ucmd.comp_mask || ucmd.reserved)
		return -EOPNOTSUPP;

	inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
5201
	in = kvzalloc(inlen, GFP_KERNEL);
5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215
	if (!in)
		return -ENOMEM;

	rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);

	curr_wq_state = (wq_attr_mask & IB_WQ_CUR_STATE) ?
		wq_attr->curr_wq_state : wq->state;
	wq_state = (wq_attr_mask & IB_WQ_STATE) ?
		wq_attr->wq_state : curr_wq_state;
	if (curr_wq_state == IB_WQS_ERR)
		curr_wq_state = MLX5_RQC_STATE_ERR;
	if (wq_state == IB_WQS_ERR)
		wq_state = MLX5_RQC_STATE_ERR;
	MLX5_SET(modify_rq_in, in, rq_state, curr_wq_state);
5216
	MLX5_SET(modify_rq_in, in, uid, to_mpd(wq->pd)->uid);
5217 5218
	MLX5_SET(rqc, rqc, state, wq_state);

5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232
	if (wq_attr_mask & IB_WQ_FLAGS) {
		if (wq_attr->flags_mask & IB_WQ_FLAGS_CVLAN_STRIPPING) {
			if (!(MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
			      MLX5_CAP_ETH(dev->mdev, vlan_cap))) {
				mlx5_ib_dbg(dev, "VLAN offloads are not "
					    "supported\n");
				err = -EOPNOTSUPP;
				goto out;
			}
			MLX5_SET64(modify_rq_in, in, modify_bitmask,
				   MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
			MLX5_SET(rqc, rqc, vsd,
				 (wq_attr->flags & IB_WQ_FLAGS_CVLAN_STRIPPING) ? 0 : 1);
		}
5233 5234 5235 5236 5237 5238

		if (wq_attr->flags_mask & IB_WQ_FLAGS_PCI_WRITE_END_PADDING) {
			mlx5_ib_dbg(dev, "Modifying scatter end padding is not supported\n");
			err = -EOPNOTSUPP;
			goto out;
		}
5239 5240
	}

5241
	if (curr_wq_state == IB_WQS_RESET && wq_state == IB_WQS_RDY) {
5242 5243 5244
		u16 set_id;

		set_id = mlx5_ib_get_counters_id(dev, 0);
5245 5246 5247
		if (MLX5_CAP_GEN(dev->mdev, modify_rq_counter_set_id)) {
			MLX5_SET64(modify_rq_in, in, modify_bitmask,
				   MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_RQ_COUNTER_SET_ID);
5248
			MLX5_SET(rqc, rqc, counter_set_id, set_id);
5249
		} else
5250 5251 5252
			dev_info_once(
				&dev->ib_dev.dev,
				"Receive WQ counters are not supported on current FW\n");
5253 5254
	}

5255
	err = mlx5_core_modify_rq(dev->mdev, rwq->core_qp.qpn, in);
5256 5257 5258
	if (!err)
		rwq->ibwq.state = (wq_state == MLX5_RQC_STATE_ERR) ? IB_WQS_ERR : wq_state;

5259 5260
out:
	kvfree(in);
5261 5262
	return err;
}
5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321

struct mlx5_ib_drain_cqe {
	struct ib_cqe cqe;
	struct completion done;
};

static void mlx5_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
{
	struct mlx5_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
						     struct mlx5_ib_drain_cqe,
						     cqe);

	complete(&cqe->done);
}

/* This function returns only once the drained WR was completed */
static void handle_drain_completion(struct ib_cq *cq,
				    struct mlx5_ib_drain_cqe *sdrain,
				    struct mlx5_ib_dev *dev)
{
	struct mlx5_core_dev *mdev = dev->mdev;

	if (cq->poll_ctx == IB_POLL_DIRECT) {
		while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
			ib_process_cq_direct(cq, -1);
		return;
	}

	if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) {
		struct mlx5_ib_cq *mcq = to_mcq(cq);
		bool triggered = false;
		unsigned long flags;

		spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
		/* Make sure that the CQ handler won't run if wasn't run yet */
		if (!mcq->mcq.reset_notify_added)
			mcq->mcq.reset_notify_added = 1;
		else
			triggered = true;
		spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);

		if (triggered) {
			/* Wait for any scheduled/running task to be ended */
			switch (cq->poll_ctx) {
			case IB_POLL_SOFTIRQ:
				irq_poll_disable(&cq->iop);
				irq_poll_enable(&cq->iop);
				break;
			case IB_POLL_WORKQUEUE:
				cancel_work_sync(&cq->work);
				break;
			default:
				WARN_ON_ONCE(1);
			}
		}

		/* Run the CQ handler - this makes sure that the drain WR will
		 * be processed if wasn't processed yet.
		 */
5322
		mcq->mcq.comp(&mcq->mcq, NULL);
5323 5324 5325 5326 5327 5328 5329 5330 5331 5332
	}

	wait_for_completion(&sdrain->done);
}

void mlx5_ib_drain_sq(struct ib_qp *qp)
{
	struct ib_cq *cq = qp->send_cq;
	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
	struct mlx5_ib_drain_cqe sdrain;
5333
	const struct ib_send_wr *bad_swr;
5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353
	struct ib_rdma_wr swr = {
		.wr = {
			.next = NULL,
			{ .wr_cqe	= &sdrain.cqe, },
			.opcode	= IB_WR_RDMA_WRITE,
		},
	};
	int ret;
	struct mlx5_ib_dev *dev = to_mdev(qp->device);
	struct mlx5_core_dev *mdev = dev->mdev;

	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
	if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
		return;
	}

	sdrain.cqe.done = mlx5_ib_drain_qp_done;
	init_completion(&sdrain.done);

5354
	ret = mlx5_ib_post_send_drain(qp, &swr.wr, &bad_swr);
5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367
	if (ret) {
		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
		return;
	}

	handle_drain_completion(cq, &sdrain, dev);
}

void mlx5_ib_drain_rq(struct ib_qp *qp)
{
	struct ib_cq *cq = qp->recv_cq;
	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
	struct mlx5_ib_drain_cqe rdrain;
5368 5369
	struct ib_recv_wr rwr = {};
	const struct ib_recv_wr *bad_rwr;
5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383
	int ret;
	struct mlx5_ib_dev *dev = to_mdev(qp->device);
	struct mlx5_core_dev *mdev = dev->mdev;

	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
	if (ret && mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
		return;
	}

	rwr.wr_cqe = &rdrain.cqe;
	rdrain.cqe.done = mlx5_ib_drain_qp_done;
	init_completion(&rdrain.done);

5384
	ret = mlx5_ib_post_recv_drain(qp, &rwr, &bad_rwr);
5385 5386 5387 5388 5389 5390 5391
	if (ret) {
		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
		return;
	}

	handle_drain_completion(cq, &rdrain, dev);
}
M
Mark Zhang 已提交
5392 5393 5394 5395 5396 5397 5398

/**
 * Bind a qp to a counter. If @counter is NULL then bind the qp to
 * the default counter
 */
int mlx5_ib_qp_set_counter(struct ib_qp *qp, struct rdma_counter *counter)
{
5399
	struct mlx5_ib_dev *dev = to_mdev(qp->device);
M
Mark Zhang 已提交
5400 5401 5402 5403 5404 5405 5406 5407 5408
	struct mlx5_ib_qp *mqp = to_mqp(qp);
	int err = 0;

	mutex_lock(&mqp->mutex);
	if (mqp->state == IB_QPS_RESET) {
		qp->counter = counter;
		goto out;
	}

5409 5410 5411 5412 5413
	if (!MLX5_CAP_GEN(dev->mdev, rts2rts_qp_counters_set_id)) {
		err = -EOPNOTSUPP;
		goto out;
	}

M
Mark Zhang 已提交
5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428
	if (mqp->state == IB_QPS_RTS) {
		err = __mlx5_ib_qp_set_counter(qp, counter);
		if (!err)
			qp->counter = counter;

		goto out;
	}

	mqp->counter_pending = 1;
	qp->counter = counter;

out:
	mutex_unlock(&mqp->mutex);
	return err;
}