xref: /openbmc/linux/drivers/infiniband/hw/mlx4/qp.c (revision be122522)
1 /*
2  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/log2.h>
35 #include <linux/etherdevice.h>
36 #include <net/ip.h>
37 #include <linux/slab.h>
38 #include <linux/netdevice.h>
39 
40 #include <rdma/ib_cache.h>
41 #include <rdma/ib_pack.h>
42 #include <rdma/ib_addr.h>
43 #include <rdma/ib_mad.h>
44 #include <rdma/uverbs_ioctl.h>
45 
46 #include <linux/mlx4/driver.h>
47 #include <linux/mlx4/qp.h>
48 
49 #include "mlx4_ib.h"
50 #include <rdma/mlx4-abi.h>
51 
52 static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq,
53 			     struct mlx4_ib_cq *recv_cq);
54 static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq,
55 			       struct mlx4_ib_cq *recv_cq);
56 static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state,
57 			      struct ib_udata *udata);
58 
59 enum {
60 	MLX4_IB_ACK_REQ_FREQ	= 8,
61 };
62 
63 enum {
64 	MLX4_IB_DEFAULT_SCHED_QUEUE	= 0x83,
65 	MLX4_IB_DEFAULT_QP0_SCHED_QUEUE	= 0x3f,
66 	MLX4_IB_LINK_TYPE_IB		= 0,
67 	MLX4_IB_LINK_TYPE_ETH		= 1
68 };
69 
70 enum {
71 	/*
72 	 * Largest possible UD header: send with GRH and immediate
73 	 * data plus 18 bytes for an Ethernet header with VLAN/802.1Q
74 	 * tag.  (LRH would only use 8 bytes, so Ethernet is the
75 	 * biggest case)
76 	 */
77 	MLX4_IB_UD_HEADER_SIZE		= 82,
78 	MLX4_IB_LSO_HEADER_SPARE	= 128,
79 };
80 
81 struct mlx4_ib_sqp {
82 	struct mlx4_ib_qp	qp;
83 	int			pkey_index;
84 	u32			qkey;
85 	u32			send_psn;
86 	struct ib_ud_header	ud_header;
87 	u8			header_buf[MLX4_IB_UD_HEADER_SIZE];
88 	struct ib_qp		*roce_v2_gsi;
89 };
90 
91 enum {
92 	MLX4_IB_MIN_SQ_STRIDE	= 6,
93 	MLX4_IB_CACHE_LINE_SIZE	= 64,
94 };
95 
96 enum {
97 	MLX4_RAW_QP_MTU		= 7,
98 	MLX4_RAW_QP_MSGMAX	= 31,
99 };
100 
101 #ifndef ETH_ALEN
102 #define ETH_ALEN        6
103 #endif
104 
105 static const __be32 mlx4_ib_opcode[] = {
106 	[IB_WR_SEND]				= cpu_to_be32(MLX4_OPCODE_SEND),
107 	[IB_WR_LSO]				= cpu_to_be32(MLX4_OPCODE_LSO),
108 	[IB_WR_SEND_WITH_IMM]			= cpu_to_be32(MLX4_OPCODE_SEND_IMM),
109 	[IB_WR_RDMA_WRITE]			= cpu_to_be32(MLX4_OPCODE_RDMA_WRITE),
110 	[IB_WR_RDMA_WRITE_WITH_IMM]		= cpu_to_be32(MLX4_OPCODE_RDMA_WRITE_IMM),
111 	[IB_WR_RDMA_READ]			= cpu_to_be32(MLX4_OPCODE_RDMA_READ),
112 	[IB_WR_ATOMIC_CMP_AND_SWP]		= cpu_to_be32(MLX4_OPCODE_ATOMIC_CS),
113 	[IB_WR_ATOMIC_FETCH_AND_ADD]		= cpu_to_be32(MLX4_OPCODE_ATOMIC_FA),
114 	[IB_WR_SEND_WITH_INV]			= cpu_to_be32(MLX4_OPCODE_SEND_INVAL),
115 	[IB_WR_LOCAL_INV]			= cpu_to_be32(MLX4_OPCODE_LOCAL_INVAL),
116 	[IB_WR_REG_MR]				= cpu_to_be32(MLX4_OPCODE_FMR),
117 	[IB_WR_MASKED_ATOMIC_CMP_AND_SWP]	= cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_CS),
118 	[IB_WR_MASKED_ATOMIC_FETCH_AND_ADD]	= cpu_to_be32(MLX4_OPCODE_MASKED_ATOMIC_FA),
119 };
120 
121 enum mlx4_ib_source_type {
122 	MLX4_IB_QP_SRC	= 0,
123 	MLX4_IB_RWQ_SRC	= 1,
124 };
125 
126 static struct mlx4_ib_sqp *to_msqp(struct mlx4_ib_qp *mqp)
127 {
128 	return container_of(mqp, struct mlx4_ib_sqp, qp);
129 }
130 
131 static int is_tunnel_qp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
132 {
133 	if (!mlx4_is_master(dev->dev))
134 		return 0;
135 
136 	return qp->mqp.qpn >= dev->dev->phys_caps.base_tunnel_sqpn &&
137 	       qp->mqp.qpn < dev->dev->phys_caps.base_tunnel_sqpn +
138 		8 * MLX4_MFUNC_MAX;
139 }
140 
141 static int is_sqp(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
142 {
143 	int proxy_sqp = 0;
144 	int real_sqp = 0;
145 	int i;
146 	/* PPF or Native -- real SQP */
147 	real_sqp = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
148 		    qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
149 		    qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 3);
150 	if (real_sqp)
151 		return 1;
152 	/* VF or PF -- proxy SQP */
153 	if (mlx4_is_mfunc(dev->dev)) {
154 		for (i = 0; i < dev->dev->caps.num_ports; i++) {
155 			if (qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp0_proxy ||
156 			    qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp1_proxy) {
157 				proxy_sqp = 1;
158 				break;
159 			}
160 		}
161 	}
162 	if (proxy_sqp)
163 		return 1;
164 
165 	return !!(qp->flags & MLX4_IB_ROCE_V2_GSI_QP);
166 }
167 
168 /* used for INIT/CLOSE port logic */
169 static int is_qp0(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
170 {
171 	int proxy_qp0 = 0;
172 	int real_qp0 = 0;
173 	int i;
174 	/* PPF or Native -- real QP0 */
175 	real_qp0 = ((mlx4_is_master(dev->dev) || !mlx4_is_mfunc(dev->dev)) &&
176 		    qp->mqp.qpn >= dev->dev->phys_caps.base_sqpn &&
177 		    qp->mqp.qpn <= dev->dev->phys_caps.base_sqpn + 1);
178 	if (real_qp0)
179 		return 1;
180 	/* VF or PF -- proxy QP0 */
181 	if (mlx4_is_mfunc(dev->dev)) {
182 		for (i = 0; i < dev->dev->caps.num_ports; i++) {
183 			if (qp->mqp.qpn == dev->dev->caps.spec_qps[i].qp0_proxy) {
184 				proxy_qp0 = 1;
185 				break;
186 			}
187 		}
188 	}
189 	return proxy_qp0;
190 }
191 
192 static void *get_wqe(struct mlx4_ib_qp *qp, int offset)
193 {
194 	return mlx4_buf_offset(&qp->buf, offset);
195 }
196 
197 static void *get_recv_wqe(struct mlx4_ib_qp *qp, int n)
198 {
199 	return get_wqe(qp, qp->rq.offset + (n << qp->rq.wqe_shift));
200 }
201 
202 static void *get_send_wqe(struct mlx4_ib_qp *qp, int n)
203 {
204 	return get_wqe(qp, qp->sq.offset + (n << qp->sq.wqe_shift));
205 }
206 
207 /*
208  * Stamp a SQ WQE so that it is invalid if prefetched by marking the
209  * first four bytes of every 64 byte chunk with 0xffffffff, except for
210  * the very first chunk of the WQE.
211  */
212 static void stamp_send_wqe(struct mlx4_ib_qp *qp, int n)
213 {
214 	__be32 *wqe;
215 	int i;
216 	int s;
217 	void *buf;
218 	struct mlx4_wqe_ctrl_seg *ctrl;
219 
220 	buf = get_send_wqe(qp, n & (qp->sq.wqe_cnt - 1));
221 	ctrl = (struct mlx4_wqe_ctrl_seg *)buf;
222 	s = (ctrl->qpn_vlan.fence_size & 0x3f) << 4;
223 	for (i = 64; i < s; i += 64) {
224 		wqe = buf + i;
225 		*wqe = cpu_to_be32(0xffffffff);
226 	}
227 }
228 
229 static void mlx4_ib_qp_event(struct mlx4_qp *qp, enum mlx4_event type)
230 {
231 	struct ib_event event;
232 	struct ib_qp *ibqp = &to_mibqp(qp)->ibqp;
233 
234 	if (type == MLX4_EVENT_TYPE_PATH_MIG)
235 		to_mibqp(qp)->port = to_mibqp(qp)->alt_port;
236 
237 	if (ibqp->event_handler) {
238 		event.device     = ibqp->device;
239 		event.element.qp = ibqp;
240 		switch (type) {
241 		case MLX4_EVENT_TYPE_PATH_MIG:
242 			event.event = IB_EVENT_PATH_MIG;
243 			break;
244 		case MLX4_EVENT_TYPE_COMM_EST:
245 			event.event = IB_EVENT_COMM_EST;
246 			break;
247 		case MLX4_EVENT_TYPE_SQ_DRAINED:
248 			event.event = IB_EVENT_SQ_DRAINED;
249 			break;
250 		case MLX4_EVENT_TYPE_SRQ_QP_LAST_WQE:
251 			event.event = IB_EVENT_QP_LAST_WQE_REACHED;
252 			break;
253 		case MLX4_EVENT_TYPE_WQ_CATAS_ERROR:
254 			event.event = IB_EVENT_QP_FATAL;
255 			break;
256 		case MLX4_EVENT_TYPE_PATH_MIG_FAILED:
257 			event.event = IB_EVENT_PATH_MIG_ERR;
258 			break;
259 		case MLX4_EVENT_TYPE_WQ_INVAL_REQ_ERROR:
260 			event.event = IB_EVENT_QP_REQ_ERR;
261 			break;
262 		case MLX4_EVENT_TYPE_WQ_ACCESS_ERROR:
263 			event.event = IB_EVENT_QP_ACCESS_ERR;
264 			break;
265 		default:
266 			pr_warn("Unexpected event type %d "
267 			       "on QP %06x\n", type, qp->qpn);
268 			return;
269 		}
270 
271 		ibqp->event_handler(&event, ibqp->qp_context);
272 	}
273 }
274 
275 static void mlx4_ib_wq_event(struct mlx4_qp *qp, enum mlx4_event type)
276 {
277 	pr_warn_ratelimited("Unexpected event type %d on WQ 0x%06x. Events are not supported for WQs\n",
278 			    type, qp->qpn);
279 }
280 
281 static int send_wqe_overhead(enum mlx4_ib_qp_type type, u32 flags)
282 {
283 	/*
284 	 * UD WQEs must have a datagram segment.
285 	 * RC and UC WQEs might have a remote address segment.
286 	 * MLX WQEs need two extra inline data segments (for the UD
287 	 * header and space for the ICRC).
288 	 */
289 	switch (type) {
290 	case MLX4_IB_QPT_UD:
291 		return sizeof (struct mlx4_wqe_ctrl_seg) +
292 			sizeof (struct mlx4_wqe_datagram_seg) +
293 			((flags & MLX4_IB_QP_LSO) ? MLX4_IB_LSO_HEADER_SPARE : 0);
294 	case MLX4_IB_QPT_PROXY_SMI_OWNER:
295 	case MLX4_IB_QPT_PROXY_SMI:
296 	case MLX4_IB_QPT_PROXY_GSI:
297 		return sizeof (struct mlx4_wqe_ctrl_seg) +
298 			sizeof (struct mlx4_wqe_datagram_seg) + 64;
299 	case MLX4_IB_QPT_TUN_SMI_OWNER:
300 	case MLX4_IB_QPT_TUN_GSI:
301 		return sizeof (struct mlx4_wqe_ctrl_seg) +
302 			sizeof (struct mlx4_wqe_datagram_seg);
303 
304 	case MLX4_IB_QPT_UC:
305 		return sizeof (struct mlx4_wqe_ctrl_seg) +
306 			sizeof (struct mlx4_wqe_raddr_seg);
307 	case MLX4_IB_QPT_RC:
308 		return sizeof (struct mlx4_wqe_ctrl_seg) +
309 			sizeof (struct mlx4_wqe_masked_atomic_seg) +
310 			sizeof (struct mlx4_wqe_raddr_seg);
311 	case MLX4_IB_QPT_SMI:
312 	case MLX4_IB_QPT_GSI:
313 		return sizeof (struct mlx4_wqe_ctrl_seg) +
314 			ALIGN(MLX4_IB_UD_HEADER_SIZE +
315 			      DIV_ROUND_UP(MLX4_IB_UD_HEADER_SIZE,
316 					   MLX4_INLINE_ALIGN) *
317 			      sizeof (struct mlx4_wqe_inline_seg),
318 			      sizeof (struct mlx4_wqe_data_seg)) +
319 			ALIGN(4 +
320 			      sizeof (struct mlx4_wqe_inline_seg),
321 			      sizeof (struct mlx4_wqe_data_seg));
322 	default:
323 		return sizeof (struct mlx4_wqe_ctrl_seg);
324 	}
325 }
326 
327 static int set_rq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
328 		       bool is_user, bool has_rq, struct mlx4_ib_qp *qp,
329 		       u32 inl_recv_sz)
330 {
331 	/* Sanity check RQ size before proceeding */
332 	if (cap->max_recv_wr > dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE ||
333 	    cap->max_recv_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg))
334 		return -EINVAL;
335 
336 	if (!has_rq) {
337 		if (cap->max_recv_wr || inl_recv_sz)
338 			return -EINVAL;
339 
340 		qp->rq.wqe_cnt = qp->rq.max_gs = 0;
341 	} else {
342 		u32 max_inl_recv_sz = dev->dev->caps.max_rq_sg *
343 			sizeof(struct mlx4_wqe_data_seg);
344 		u32 wqe_size;
345 
346 		/* HW requires >= 1 RQ entry with >= 1 gather entry */
347 		if (is_user && (!cap->max_recv_wr || !cap->max_recv_sge ||
348 				inl_recv_sz > max_inl_recv_sz))
349 			return -EINVAL;
350 
351 		qp->rq.wqe_cnt	 = roundup_pow_of_two(max(1U, cap->max_recv_wr));
352 		qp->rq.max_gs	 = roundup_pow_of_two(max(1U, cap->max_recv_sge));
353 		wqe_size = qp->rq.max_gs * sizeof(struct mlx4_wqe_data_seg);
354 		qp->rq.wqe_shift = ilog2(max_t(u32, wqe_size, inl_recv_sz));
355 	}
356 
357 	/* leave userspace return values as they were, so as not to break ABI */
358 	if (is_user) {
359 		cap->max_recv_wr  = qp->rq.max_post = qp->rq.wqe_cnt;
360 		cap->max_recv_sge = qp->rq.max_gs;
361 	} else {
362 		cap->max_recv_wr  = qp->rq.max_post =
363 			min(dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE, qp->rq.wqe_cnt);
364 		cap->max_recv_sge = min(qp->rq.max_gs,
365 					min(dev->dev->caps.max_sq_sg,
366 					    dev->dev->caps.max_rq_sg));
367 	}
368 
369 	return 0;
370 }
371 
372 static int set_kernel_sq_size(struct mlx4_ib_dev *dev, struct ib_qp_cap *cap,
373 			      enum mlx4_ib_qp_type type, struct mlx4_ib_qp *qp)
374 {
375 	int s;
376 
377 	/* Sanity check SQ size before proceeding */
378 	if (cap->max_send_wr  > (dev->dev->caps.max_wqes - MLX4_IB_SQ_MAX_SPARE) ||
379 	    cap->max_send_sge > min(dev->dev->caps.max_sq_sg, dev->dev->caps.max_rq_sg) ||
380 	    cap->max_inline_data + send_wqe_overhead(type, qp->flags) +
381 	    sizeof (struct mlx4_wqe_inline_seg) > dev->dev->caps.max_sq_desc_sz)
382 		return -EINVAL;
383 
384 	/*
385 	 * For MLX transport we need 2 extra S/G entries:
386 	 * one for the header and one for the checksum at the end
387 	 */
388 	if ((type == MLX4_IB_QPT_SMI || type == MLX4_IB_QPT_GSI ||
389 	     type & (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) &&
390 	    cap->max_send_sge + 2 > dev->dev->caps.max_sq_sg)
391 		return -EINVAL;
392 
393 	s = max(cap->max_send_sge * sizeof (struct mlx4_wqe_data_seg),
394 		cap->max_inline_data + sizeof (struct mlx4_wqe_inline_seg)) +
395 		send_wqe_overhead(type, qp->flags);
396 
397 	if (s > dev->dev->caps.max_sq_desc_sz)
398 		return -EINVAL;
399 
400 	qp->sq.wqe_shift = ilog2(roundup_pow_of_two(s));
401 
402 	/*
403 	 * We need to leave 2 KB + 1 WR of headroom in the SQ to
404 	 * allow HW to prefetch.
405 	 */
406 	qp->sq_spare_wqes = MLX4_IB_SQ_HEADROOM(qp->sq.wqe_shift);
407 	qp->sq.wqe_cnt = roundup_pow_of_two(cap->max_send_wr +
408 					    qp->sq_spare_wqes);
409 
410 	qp->sq.max_gs =
411 		(min(dev->dev->caps.max_sq_desc_sz,
412 		     (1 << qp->sq.wqe_shift)) -
413 		 send_wqe_overhead(type, qp->flags)) /
414 		sizeof (struct mlx4_wqe_data_seg);
415 
416 	qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
417 		(qp->sq.wqe_cnt << qp->sq.wqe_shift);
418 	if (qp->rq.wqe_shift > qp->sq.wqe_shift) {
419 		qp->rq.offset = 0;
420 		qp->sq.offset = qp->rq.wqe_cnt << qp->rq.wqe_shift;
421 	} else {
422 		qp->rq.offset = qp->sq.wqe_cnt << qp->sq.wqe_shift;
423 		qp->sq.offset = 0;
424 	}
425 
426 	cap->max_send_wr  = qp->sq.max_post =
427 		qp->sq.wqe_cnt - qp->sq_spare_wqes;
428 	cap->max_send_sge = min(qp->sq.max_gs,
429 				min(dev->dev->caps.max_sq_sg,
430 				    dev->dev->caps.max_rq_sg));
431 	/* We don't support inline sends for kernel QPs (yet) */
432 	cap->max_inline_data = 0;
433 
434 	return 0;
435 }
436 
437 static int set_user_sq_size(struct mlx4_ib_dev *dev,
438 			    struct mlx4_ib_qp *qp,
439 			    struct mlx4_ib_create_qp *ucmd)
440 {
441 	/* Sanity check SQ size before proceeding */
442 	if ((1 << ucmd->log_sq_bb_count) > dev->dev->caps.max_wqes	 ||
443 	    ucmd->log_sq_stride >
444 		ilog2(roundup_pow_of_two(dev->dev->caps.max_sq_desc_sz)) ||
445 	    ucmd->log_sq_stride < MLX4_IB_MIN_SQ_STRIDE)
446 		return -EINVAL;
447 
448 	qp->sq.wqe_cnt   = 1 << ucmd->log_sq_bb_count;
449 	qp->sq.wqe_shift = ucmd->log_sq_stride;
450 
451 	qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
452 		(qp->sq.wqe_cnt << qp->sq.wqe_shift);
453 
454 	return 0;
455 }
456 
457 static int alloc_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
458 {
459 	int i;
460 
461 	qp->sqp_proxy_rcv =
462 		kmalloc_array(qp->rq.wqe_cnt, sizeof(struct mlx4_ib_buf),
463 			      GFP_KERNEL);
464 	if (!qp->sqp_proxy_rcv)
465 		return -ENOMEM;
466 	for (i = 0; i < qp->rq.wqe_cnt; i++) {
467 		qp->sqp_proxy_rcv[i].addr =
468 			kmalloc(sizeof (struct mlx4_ib_proxy_sqp_hdr),
469 				GFP_KERNEL);
470 		if (!qp->sqp_proxy_rcv[i].addr)
471 			goto err;
472 		qp->sqp_proxy_rcv[i].map =
473 			ib_dma_map_single(dev, qp->sqp_proxy_rcv[i].addr,
474 					  sizeof (struct mlx4_ib_proxy_sqp_hdr),
475 					  DMA_FROM_DEVICE);
476 		if (ib_dma_mapping_error(dev, qp->sqp_proxy_rcv[i].map)) {
477 			kfree(qp->sqp_proxy_rcv[i].addr);
478 			goto err;
479 		}
480 	}
481 	return 0;
482 
483 err:
484 	while (i > 0) {
485 		--i;
486 		ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
487 				    sizeof (struct mlx4_ib_proxy_sqp_hdr),
488 				    DMA_FROM_DEVICE);
489 		kfree(qp->sqp_proxy_rcv[i].addr);
490 	}
491 	kfree(qp->sqp_proxy_rcv);
492 	qp->sqp_proxy_rcv = NULL;
493 	return -ENOMEM;
494 }
495 
496 static void free_proxy_bufs(struct ib_device *dev, struct mlx4_ib_qp *qp)
497 {
498 	int i;
499 
500 	for (i = 0; i < qp->rq.wqe_cnt; i++) {
501 		ib_dma_unmap_single(dev, qp->sqp_proxy_rcv[i].map,
502 				    sizeof (struct mlx4_ib_proxy_sqp_hdr),
503 				    DMA_FROM_DEVICE);
504 		kfree(qp->sqp_proxy_rcv[i].addr);
505 	}
506 	kfree(qp->sqp_proxy_rcv);
507 }
508 
509 static bool qp_has_rq(struct ib_qp_init_attr *attr)
510 {
511 	if (attr->qp_type == IB_QPT_XRC_INI || attr->qp_type == IB_QPT_XRC_TGT)
512 		return false;
513 
514 	return !attr->srq;
515 }
516 
517 static int qp0_enabled_vf(struct mlx4_dev *dev, int qpn)
518 {
519 	int i;
520 	for (i = 0; i < dev->caps.num_ports; i++) {
521 		if (qpn == dev->caps.spec_qps[i].qp0_proxy)
522 			return !!dev->caps.spec_qps[i].qp0_qkey;
523 	}
524 	return 0;
525 }
526 
527 static void mlx4_ib_free_qp_counter(struct mlx4_ib_dev *dev,
528 				    struct mlx4_ib_qp *qp)
529 {
530 	mutex_lock(&dev->counters_table[qp->port - 1].mutex);
531 	mlx4_counter_free(dev->dev, qp->counter_index->index);
532 	list_del(&qp->counter_index->list);
533 	mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
534 
535 	kfree(qp->counter_index);
536 	qp->counter_index = NULL;
537 }
538 
539 static int set_qp_rss(struct mlx4_ib_dev *dev, struct mlx4_ib_rss *rss_ctx,
540 		      struct ib_qp_init_attr *init_attr,
541 		      struct mlx4_ib_create_qp_rss *ucmd)
542 {
543 	rss_ctx->base_qpn_tbl_sz = init_attr->rwq_ind_tbl->ind_tbl[0]->wq_num |
544 		(init_attr->rwq_ind_tbl->log_ind_tbl_size << 24);
545 
546 	if ((ucmd->rx_hash_function == MLX4_IB_RX_HASH_FUNC_TOEPLITZ) &&
547 	    (dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_RSS_TOP)) {
548 		memcpy(rss_ctx->rss_key, ucmd->rx_hash_key,
549 		       MLX4_EN_RSS_KEY_SIZE);
550 	} else {
551 		pr_debug("RX Hash function is not supported\n");
552 		return (-EOPNOTSUPP);
553 	}
554 
555 	if (ucmd->rx_hash_fields_mask & ~(MLX4_IB_RX_HASH_SRC_IPV4	|
556 					  MLX4_IB_RX_HASH_DST_IPV4	|
557 					  MLX4_IB_RX_HASH_SRC_IPV6	|
558 					  MLX4_IB_RX_HASH_DST_IPV6	|
559 					  MLX4_IB_RX_HASH_SRC_PORT_TCP	|
560 					  MLX4_IB_RX_HASH_DST_PORT_TCP	|
561 					  MLX4_IB_RX_HASH_SRC_PORT_UDP	|
562 					  MLX4_IB_RX_HASH_DST_PORT_UDP  |
563 					  MLX4_IB_RX_HASH_INNER)) {
564 		pr_debug("RX Hash fields_mask has unsupported mask (0x%llx)\n",
565 			 ucmd->rx_hash_fields_mask);
566 		return (-EOPNOTSUPP);
567 	}
568 
569 	if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV4) &&
570 	    (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV4)) {
571 		rss_ctx->flags = MLX4_RSS_IPV4;
572 	} else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV4) ||
573 		   (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV4)) {
574 		pr_debug("RX Hash fields_mask is not supported - both IPv4 SRC and DST must be set\n");
575 		return (-EOPNOTSUPP);
576 	}
577 
578 	if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV6) &&
579 	    (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV6)) {
580 		rss_ctx->flags |= MLX4_RSS_IPV6;
581 	} else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_IPV6) ||
582 		   (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_IPV6)) {
583 		pr_debug("RX Hash fields_mask is not supported - both IPv6 SRC and DST must be set\n");
584 		return (-EOPNOTSUPP);
585 	}
586 
587 	if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_UDP) &&
588 	    (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_UDP)) {
589 		if (!(dev->dev->caps.flags & MLX4_DEV_CAP_FLAG_UDP_RSS)) {
590 			pr_debug("RX Hash fields_mask for UDP is not supported\n");
591 			return (-EOPNOTSUPP);
592 		}
593 
594 		if (rss_ctx->flags & MLX4_RSS_IPV4)
595 			rss_ctx->flags |= MLX4_RSS_UDP_IPV4;
596 		if (rss_ctx->flags & MLX4_RSS_IPV6)
597 			rss_ctx->flags |= MLX4_RSS_UDP_IPV6;
598 		if (!(rss_ctx->flags & (MLX4_RSS_IPV6 | MLX4_RSS_IPV4))) {
599 			pr_debug("RX Hash fields_mask is not supported - UDP must be set with IPv4 or IPv6\n");
600 			return (-EOPNOTSUPP);
601 		}
602 	} else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_UDP) ||
603 		   (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_UDP)) {
604 		pr_debug("RX Hash fields_mask is not supported - both UDP SRC and DST must be set\n");
605 		return (-EOPNOTSUPP);
606 	}
607 
608 	if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_TCP) &&
609 	    (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_TCP)) {
610 		if (rss_ctx->flags & MLX4_RSS_IPV4)
611 			rss_ctx->flags |= MLX4_RSS_TCP_IPV4;
612 		if (rss_ctx->flags & MLX4_RSS_IPV6)
613 			rss_ctx->flags |= MLX4_RSS_TCP_IPV6;
614 		if (!(rss_ctx->flags & (MLX4_RSS_IPV6 | MLX4_RSS_IPV4))) {
615 			pr_debug("RX Hash fields_mask is not supported - TCP must be set with IPv4 or IPv6\n");
616 			return (-EOPNOTSUPP);
617 		}
618 	} else if ((ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_SRC_PORT_TCP) ||
619 		   (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_DST_PORT_TCP)) {
620 		pr_debug("RX Hash fields_mask is not supported - both TCP SRC and DST must be set\n");
621 		return (-EOPNOTSUPP);
622 	}
623 
624 	if (ucmd->rx_hash_fields_mask & MLX4_IB_RX_HASH_INNER) {
625 		if (dev->dev->caps.tunnel_offload_mode ==
626 		    MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
627 			/*
628 			 * Hash according to inner headers if exist, otherwise
629 			 * according to outer headers.
630 			 */
631 			rss_ctx->flags |= MLX4_RSS_BY_INNER_HEADERS_IPONLY;
632 		} else {
633 			pr_debug("RSS Hash for inner headers isn't supported\n");
634 			return (-EOPNOTSUPP);
635 		}
636 	}
637 
638 	return 0;
639 }
640 
641 static int create_qp_rss(struct mlx4_ib_dev *dev,
642 			 struct ib_qp_init_attr *init_attr,
643 			 struct mlx4_ib_create_qp_rss *ucmd,
644 			 struct mlx4_ib_qp *qp)
645 {
646 	int qpn;
647 	int err;
648 
649 	qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS;
650 
651 	err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn, 0, qp->mqp.usage);
652 	if (err)
653 		return err;
654 
655 	err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
656 	if (err)
657 		goto err_qpn;
658 
659 	mutex_init(&qp->mutex);
660 
661 	INIT_LIST_HEAD(&qp->gid_list);
662 	INIT_LIST_HEAD(&qp->steering_rules);
663 
664 	qp->mlx4_ib_qp_type = MLX4_IB_QPT_RAW_PACKET;
665 	qp->state = IB_QPS_RESET;
666 
667 	/* Set dummy send resources to be compatible with HV and PRM */
668 	qp->sq_no_prefetch = 1;
669 	qp->sq.wqe_cnt = 1;
670 	qp->sq.wqe_shift = MLX4_IB_MIN_SQ_STRIDE;
671 	qp->buf_size = qp->sq.wqe_cnt << MLX4_IB_MIN_SQ_STRIDE;
672 	qp->mtt = (to_mqp(
673 		   (struct ib_qp *)init_attr->rwq_ind_tbl->ind_tbl[0]))->mtt;
674 
675 	qp->rss_ctx = kzalloc(sizeof(*qp->rss_ctx), GFP_KERNEL);
676 	if (!qp->rss_ctx) {
677 		err = -ENOMEM;
678 		goto err_qp_alloc;
679 	}
680 
681 	err = set_qp_rss(dev, qp->rss_ctx, init_attr, ucmd);
682 	if (err)
683 		goto err;
684 
685 	return 0;
686 
687 err:
688 	kfree(qp->rss_ctx);
689 
690 err_qp_alloc:
691 	mlx4_qp_remove(dev->dev, &qp->mqp);
692 	mlx4_qp_free(dev->dev, &qp->mqp);
693 
694 err_qpn:
695 	mlx4_qp_release_range(dev->dev, qpn, 1);
696 	return err;
697 }
698 
699 static struct ib_qp *_mlx4_ib_create_qp_rss(struct ib_pd *pd,
700 					    struct ib_qp_init_attr *init_attr,
701 					    struct ib_udata *udata)
702 {
703 	struct mlx4_ib_qp *qp;
704 	struct mlx4_ib_create_qp_rss ucmd = {};
705 	size_t required_cmd_sz;
706 	int err;
707 
708 	if (!udata) {
709 		pr_debug("RSS QP with NULL udata\n");
710 		return ERR_PTR(-EINVAL);
711 	}
712 
713 	if (udata->outlen)
714 		return ERR_PTR(-EOPNOTSUPP);
715 
716 	required_cmd_sz = offsetof(typeof(ucmd), reserved1) +
717 					sizeof(ucmd.reserved1);
718 	if (udata->inlen < required_cmd_sz) {
719 		pr_debug("invalid inlen\n");
720 		return ERR_PTR(-EINVAL);
721 	}
722 
723 	if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen))) {
724 		pr_debug("copy failed\n");
725 		return ERR_PTR(-EFAULT);
726 	}
727 
728 	if (memchr_inv(ucmd.reserved, 0, sizeof(ucmd.reserved)))
729 		return ERR_PTR(-EOPNOTSUPP);
730 
731 	if (ucmd.comp_mask || ucmd.reserved1)
732 		return ERR_PTR(-EOPNOTSUPP);
733 
734 	if (udata->inlen > sizeof(ucmd) &&
735 	    !ib_is_udata_cleared(udata, sizeof(ucmd),
736 				 udata->inlen - sizeof(ucmd))) {
737 		pr_debug("inlen is not supported\n");
738 		return ERR_PTR(-EOPNOTSUPP);
739 	}
740 
741 	if (init_attr->qp_type != IB_QPT_RAW_PACKET) {
742 		pr_debug("RSS QP with unsupported QP type %d\n",
743 			 init_attr->qp_type);
744 		return ERR_PTR(-EOPNOTSUPP);
745 	}
746 
747 	if (init_attr->create_flags) {
748 		pr_debug("RSS QP doesn't support create flags\n");
749 		return ERR_PTR(-EOPNOTSUPP);
750 	}
751 
752 	if (init_attr->send_cq || init_attr->cap.max_send_wr) {
753 		pr_debug("RSS QP with unsupported send attributes\n");
754 		return ERR_PTR(-EOPNOTSUPP);
755 	}
756 
757 	qp = kzalloc(sizeof(*qp), GFP_KERNEL);
758 	if (!qp)
759 		return ERR_PTR(-ENOMEM);
760 
761 	qp->pri.vid = 0xFFFF;
762 	qp->alt.vid = 0xFFFF;
763 
764 	err = create_qp_rss(to_mdev(pd->device), init_attr, &ucmd, qp);
765 	if (err) {
766 		kfree(qp);
767 		return ERR_PTR(err);
768 	}
769 
770 	qp->ibqp.qp_num = qp->mqp.qpn;
771 
772 	return &qp->ibqp;
773 }
774 
775 /*
776  * This function allocates a WQN from a range which is consecutive and aligned
777  * to its size. In case the range is full, then it creates a new range and
778  * allocates WQN from it. The new range will be used for following allocations.
779  */
780 static int mlx4_ib_alloc_wqn(struct mlx4_ib_ucontext *context,
781 			     struct mlx4_ib_qp *qp, int range_size, int *wqn)
782 {
783 	struct mlx4_ib_dev *dev = to_mdev(context->ibucontext.device);
784 	struct mlx4_wqn_range *range;
785 	int err = 0;
786 
787 	mutex_lock(&context->wqn_ranges_mutex);
788 
789 	range = list_first_entry_or_null(&context->wqn_ranges_list,
790 					 struct mlx4_wqn_range, list);
791 
792 	if (!range || (range->refcount == range->size) || range->dirty) {
793 		range = kzalloc(sizeof(*range), GFP_KERNEL);
794 		if (!range) {
795 			err = -ENOMEM;
796 			goto out;
797 		}
798 
799 		err = mlx4_qp_reserve_range(dev->dev, range_size,
800 					    range_size, &range->base_wqn, 0,
801 					    qp->mqp.usage);
802 		if (err) {
803 			kfree(range);
804 			goto out;
805 		}
806 
807 		range->size = range_size;
808 		list_add(&range->list, &context->wqn_ranges_list);
809 	} else if (range_size != 1) {
810 		/*
811 		 * Requesting a new range (>1) when last range is still open, is
812 		 * not valid.
813 		 */
814 		err = -EINVAL;
815 		goto out;
816 	}
817 
818 	qp->wqn_range = range;
819 
820 	*wqn = range->base_wqn + range->refcount;
821 
822 	range->refcount++;
823 
824 out:
825 	mutex_unlock(&context->wqn_ranges_mutex);
826 
827 	return err;
828 }
829 
830 static void mlx4_ib_release_wqn(struct mlx4_ib_ucontext *context,
831 				struct mlx4_ib_qp *qp, bool dirty_release)
832 {
833 	struct mlx4_ib_dev *dev = to_mdev(context->ibucontext.device);
834 	struct mlx4_wqn_range *range;
835 
836 	mutex_lock(&context->wqn_ranges_mutex);
837 
838 	range = qp->wqn_range;
839 
840 	range->refcount--;
841 	if (!range->refcount) {
842 		mlx4_qp_release_range(dev->dev, range->base_wqn,
843 				      range->size);
844 		list_del(&range->list);
845 		kfree(range);
846 	} else if (dirty_release) {
847 	/*
848 	 * A range which one of its WQNs is destroyed, won't be able to be
849 	 * reused for further WQN allocations.
850 	 * The next created WQ will allocate a new range.
851 	 */
852 		range->dirty = 1;
853 	}
854 
855 	mutex_unlock(&context->wqn_ranges_mutex);
856 }
857 
858 static int create_rq(struct ib_pd *pd, struct ib_qp_init_attr *init_attr,
859 		     struct ib_udata *udata, struct mlx4_ib_qp *qp)
860 {
861 	struct mlx4_ib_dev *dev = to_mdev(pd->device);
862 	int qpn;
863 	int err;
864 	struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context(
865 		udata, struct mlx4_ib_ucontext, ibucontext);
866 	struct mlx4_ib_cq *mcq;
867 	unsigned long flags;
868 	int range_size;
869 	struct mlx4_ib_create_wq wq;
870 	size_t copy_len;
871 	int shift;
872 	int n;
873 
874 	qp->mlx4_ib_qp_type = MLX4_IB_QPT_RAW_PACKET;
875 
876 	mutex_init(&qp->mutex);
877 	spin_lock_init(&qp->sq.lock);
878 	spin_lock_init(&qp->rq.lock);
879 	INIT_LIST_HEAD(&qp->gid_list);
880 	INIT_LIST_HEAD(&qp->steering_rules);
881 
882 	qp->state = IB_QPS_RESET;
883 
884 	copy_len = min(sizeof(struct mlx4_ib_create_wq), udata->inlen);
885 
886 	if (ib_copy_from_udata(&wq, udata, copy_len)) {
887 		err = -EFAULT;
888 		goto err;
889 	}
890 
891 	if (wq.comp_mask || wq.reserved[0] || wq.reserved[1] ||
892 	    wq.reserved[2]) {
893 		pr_debug("user command isn't supported\n");
894 		err = -EOPNOTSUPP;
895 		goto err;
896 	}
897 
898 	if (wq.log_range_size > ilog2(dev->dev->caps.max_rss_tbl_sz)) {
899 		pr_debug("WQN range size must be equal or smaller than %d\n",
900 			 dev->dev->caps.max_rss_tbl_sz);
901 		err = -EOPNOTSUPP;
902 		goto err;
903 	}
904 	range_size = 1 << wq.log_range_size;
905 
906 	if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS)
907 		qp->flags |= MLX4_IB_QP_SCATTER_FCS;
908 
909 	err = set_rq_size(dev, &init_attr->cap, true, true, qp, qp->inl_recv_sz);
910 	if (err)
911 		goto err;
912 
913 	qp->sq_no_prefetch = 1;
914 	qp->sq.wqe_cnt = 1;
915 	qp->sq.wqe_shift = MLX4_IB_MIN_SQ_STRIDE;
916 	qp->buf_size = (qp->rq.wqe_cnt << qp->rq.wqe_shift) +
917 		       (qp->sq.wqe_cnt << qp->sq.wqe_shift);
918 
919 	qp->umem = ib_umem_get(udata, wq.buf_addr, qp->buf_size, 0);
920 	if (IS_ERR(qp->umem)) {
921 		err = PTR_ERR(qp->umem);
922 		goto err;
923 	}
924 
925 	n = ib_umem_page_count(qp->umem);
926 	shift = mlx4_ib_umem_calc_optimal_mtt_size(qp->umem, 0, &n);
927 	err = mlx4_mtt_init(dev->dev, n, shift, &qp->mtt);
928 
929 	if (err)
930 		goto err_buf;
931 
932 	err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
933 	if (err)
934 		goto err_mtt;
935 
936 	err = mlx4_ib_db_map_user(udata, wq.db_addr, &qp->db);
937 	if (err)
938 		goto err_mtt;
939 	qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS;
940 
941 	err = mlx4_ib_alloc_wqn(context, qp, range_size, &qpn);
942 	if (err)
943 		goto err_wrid;
944 
945 	err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
946 	if (err)
947 		goto err_qpn;
948 
949 	/*
950 	 * Hardware wants QPN written in big-endian order (after
951 	 * shifting) for send doorbell.  Precompute this value to save
952 	 * a little bit when posting sends.
953 	 */
954 	qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
955 
956 	qp->mqp.event = mlx4_ib_wq_event;
957 
958 	spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
959 	mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq),
960 			 to_mcq(init_attr->recv_cq));
961 	/* Maintain device to QPs access, needed for further handling
962 	 * via reset flow
963 	 */
964 	list_add_tail(&qp->qps_list, &dev->qp_list);
965 	/* Maintain CQ to QPs access, needed for further handling
966 	 * via reset flow
967 	 */
968 	mcq = to_mcq(init_attr->send_cq);
969 	list_add_tail(&qp->cq_send_list, &mcq->send_qp_list);
970 	mcq = to_mcq(init_attr->recv_cq);
971 	list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list);
972 	mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq),
973 			   to_mcq(init_attr->recv_cq));
974 	spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
975 	return 0;
976 
977 err_qpn:
978 	mlx4_ib_release_wqn(context, qp, 0);
979 err_wrid:
980 	mlx4_ib_db_unmap_user(context, &qp->db);
981 
982 err_mtt:
983 	mlx4_mtt_cleanup(dev->dev, &qp->mtt);
984 err_buf:
985 	ib_umem_release(qp->umem);
986 err:
987 	return err;
988 }
989 
990 static int create_qp_common(struct ib_pd *pd, struct ib_qp_init_attr *init_attr,
991 			    struct ib_udata *udata, int sqpn,
992 			    struct mlx4_ib_qp **caller_qp)
993 {
994 	struct mlx4_ib_dev *dev = to_mdev(pd->device);
995 	int qpn;
996 	int err;
997 	struct mlx4_ib_sqp *sqp = NULL;
998 	struct mlx4_ib_qp *qp;
999 	struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context(
1000 		udata, struct mlx4_ib_ucontext, ibucontext);
1001 	enum mlx4_ib_qp_type qp_type = (enum mlx4_ib_qp_type) init_attr->qp_type;
1002 	struct mlx4_ib_cq *mcq;
1003 	unsigned long flags;
1004 
1005 	/* When tunneling special qps, we use a plain UD qp */
1006 	if (sqpn) {
1007 		if (mlx4_is_mfunc(dev->dev) &&
1008 		    (!mlx4_is_master(dev->dev) ||
1009 		     !(init_attr->create_flags & MLX4_IB_SRIOV_SQP))) {
1010 			if (init_attr->qp_type == IB_QPT_GSI)
1011 				qp_type = MLX4_IB_QPT_PROXY_GSI;
1012 			else {
1013 				if (mlx4_is_master(dev->dev) ||
1014 				    qp0_enabled_vf(dev->dev, sqpn))
1015 					qp_type = MLX4_IB_QPT_PROXY_SMI_OWNER;
1016 				else
1017 					qp_type = MLX4_IB_QPT_PROXY_SMI;
1018 			}
1019 		}
1020 		qpn = sqpn;
1021 		/* add extra sg entry for tunneling */
1022 		init_attr->cap.max_recv_sge++;
1023 	} else if (init_attr->create_flags & MLX4_IB_SRIOV_TUNNEL_QP) {
1024 		struct mlx4_ib_qp_tunnel_init_attr *tnl_init =
1025 			container_of(init_attr,
1026 				     struct mlx4_ib_qp_tunnel_init_attr, init_attr);
1027 		if ((tnl_init->proxy_qp_type != IB_QPT_SMI &&
1028 		     tnl_init->proxy_qp_type != IB_QPT_GSI)   ||
1029 		    !mlx4_is_master(dev->dev))
1030 			return -EINVAL;
1031 		if (tnl_init->proxy_qp_type == IB_QPT_GSI)
1032 			qp_type = MLX4_IB_QPT_TUN_GSI;
1033 		else if (tnl_init->slave == mlx4_master_func_num(dev->dev) ||
1034 			 mlx4_vf_smi_enabled(dev->dev, tnl_init->slave,
1035 					     tnl_init->port))
1036 			qp_type = MLX4_IB_QPT_TUN_SMI_OWNER;
1037 		else
1038 			qp_type = MLX4_IB_QPT_TUN_SMI;
1039 		/* we are definitely in the PPF here, since we are creating
1040 		 * tunnel QPs. base_tunnel_sqpn is therefore valid. */
1041 		qpn = dev->dev->phys_caps.base_tunnel_sqpn + 8 * tnl_init->slave
1042 			+ tnl_init->proxy_qp_type * 2 + tnl_init->port - 1;
1043 		sqpn = qpn;
1044 	}
1045 
1046 	if (!*caller_qp) {
1047 		if (qp_type == MLX4_IB_QPT_SMI || qp_type == MLX4_IB_QPT_GSI ||
1048 		    (qp_type & (MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_SMI_OWNER |
1049 				MLX4_IB_QPT_PROXY_GSI | MLX4_IB_QPT_TUN_SMI_OWNER))) {
1050 			sqp = kzalloc(sizeof(struct mlx4_ib_sqp), GFP_KERNEL);
1051 			if (!sqp)
1052 				return -ENOMEM;
1053 			qp = &sqp->qp;
1054 		} else {
1055 			qp = kzalloc(sizeof(struct mlx4_ib_qp), GFP_KERNEL);
1056 			if (!qp)
1057 				return -ENOMEM;
1058 		}
1059 		qp->pri.vid = 0xFFFF;
1060 		qp->alt.vid = 0xFFFF;
1061 	} else
1062 		qp = *caller_qp;
1063 
1064 	qp->mlx4_ib_qp_type = qp_type;
1065 
1066 	mutex_init(&qp->mutex);
1067 	spin_lock_init(&qp->sq.lock);
1068 	spin_lock_init(&qp->rq.lock);
1069 	INIT_LIST_HEAD(&qp->gid_list);
1070 	INIT_LIST_HEAD(&qp->steering_rules);
1071 
1072 	qp->state = IB_QPS_RESET;
1073 	if (init_attr->sq_sig_type == IB_SIGNAL_ALL_WR)
1074 		qp->sq_signal_bits = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
1075 
1076 	if (udata) {
1077 		struct mlx4_ib_create_qp ucmd;
1078 		size_t copy_len;
1079 		int shift;
1080 		int n;
1081 
1082 		copy_len = sizeof(struct mlx4_ib_create_qp);
1083 
1084 		if (ib_copy_from_udata(&ucmd, udata, copy_len)) {
1085 			err = -EFAULT;
1086 			goto err;
1087 		}
1088 
1089 		qp->inl_recv_sz = ucmd.inl_recv_sz;
1090 
1091 		if (init_attr->create_flags & IB_QP_CREATE_SCATTER_FCS) {
1092 			if (!(dev->dev->caps.flags &
1093 			      MLX4_DEV_CAP_FLAG_FCS_KEEP)) {
1094 				pr_debug("scatter FCS is unsupported\n");
1095 				err = -EOPNOTSUPP;
1096 				goto err;
1097 			}
1098 
1099 			qp->flags |= MLX4_IB_QP_SCATTER_FCS;
1100 		}
1101 
1102 		err = set_rq_size(dev, &init_attr->cap, udata,
1103 				  qp_has_rq(init_attr), qp, qp->inl_recv_sz);
1104 		if (err)
1105 			goto err;
1106 
1107 		qp->sq_no_prefetch = ucmd.sq_no_prefetch;
1108 
1109 		err = set_user_sq_size(dev, qp, &ucmd);
1110 		if (err)
1111 			goto err;
1112 
1113 		qp->umem = ib_umem_get(udata, ucmd.buf_addr, qp->buf_size, 0);
1114 		if (IS_ERR(qp->umem)) {
1115 			err = PTR_ERR(qp->umem);
1116 			goto err;
1117 		}
1118 
1119 		n = ib_umem_page_count(qp->umem);
1120 		shift = mlx4_ib_umem_calc_optimal_mtt_size(qp->umem, 0, &n);
1121 		err = mlx4_mtt_init(dev->dev, n, shift, &qp->mtt);
1122 
1123 		if (err)
1124 			goto err_buf;
1125 
1126 		err = mlx4_ib_umem_write_mtt(dev, &qp->mtt, qp->umem);
1127 		if (err)
1128 			goto err_mtt;
1129 
1130 		if (qp_has_rq(init_attr)) {
1131 			err = mlx4_ib_db_map_user(udata, ucmd.db_addr, &qp->db);
1132 			if (err)
1133 				goto err_mtt;
1134 		}
1135 		qp->mqp.usage = MLX4_RES_USAGE_USER_VERBS;
1136 	} else {
1137 		err = set_rq_size(dev, &init_attr->cap, udata,
1138 				  qp_has_rq(init_attr), qp, 0);
1139 		if (err)
1140 			goto err;
1141 
1142 		qp->sq_no_prefetch = 0;
1143 
1144 		if (init_attr->create_flags & IB_QP_CREATE_IPOIB_UD_LSO)
1145 			qp->flags |= MLX4_IB_QP_LSO;
1146 
1147 		if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
1148 			if (dev->steering_support ==
1149 			    MLX4_STEERING_MODE_DEVICE_MANAGED)
1150 				qp->flags |= MLX4_IB_QP_NETIF;
1151 			else
1152 				goto err;
1153 		}
1154 
1155 		err = set_kernel_sq_size(dev, &init_attr->cap, qp_type, qp);
1156 		if (err)
1157 			goto err;
1158 
1159 		if (qp_has_rq(init_attr)) {
1160 			err = mlx4_db_alloc(dev->dev, &qp->db, 0);
1161 			if (err)
1162 				goto err;
1163 
1164 			*qp->db.db = 0;
1165 		}
1166 
1167 		if (mlx4_buf_alloc(dev->dev, qp->buf_size,  PAGE_SIZE * 2,
1168 				   &qp->buf)) {
1169 			err = -ENOMEM;
1170 			goto err_db;
1171 		}
1172 
1173 		err = mlx4_mtt_init(dev->dev, qp->buf.npages, qp->buf.page_shift,
1174 				    &qp->mtt);
1175 		if (err)
1176 			goto err_buf;
1177 
1178 		err = mlx4_buf_write_mtt(dev->dev, &qp->mtt, &qp->buf);
1179 		if (err)
1180 			goto err_mtt;
1181 
1182 		qp->sq.wrid = kvmalloc_array(qp->sq.wqe_cnt,
1183 					     sizeof(u64), GFP_KERNEL);
1184 		qp->rq.wrid = kvmalloc_array(qp->rq.wqe_cnt,
1185 					     sizeof(u64), GFP_KERNEL);
1186 		if (!qp->sq.wrid || !qp->rq.wrid) {
1187 			err = -ENOMEM;
1188 			goto err_wrid;
1189 		}
1190 		qp->mqp.usage = MLX4_RES_USAGE_DRIVER;
1191 	}
1192 
1193 	if (sqpn) {
1194 		if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
1195 		    MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
1196 			if (alloc_proxy_bufs(pd->device, qp)) {
1197 				err = -ENOMEM;
1198 				goto err_wrid;
1199 			}
1200 		}
1201 	} else {
1202 		/* Raw packet QPNs may not have bits 6,7 set in their qp_num;
1203 		 * otherwise, the WQE BlueFlame setup flow wrongly causes
1204 		 * VLAN insertion. */
1205 		if (init_attr->qp_type == IB_QPT_RAW_PACKET)
1206 			err = mlx4_qp_reserve_range(dev->dev, 1, 1, &qpn,
1207 						    (init_attr->cap.max_send_wr ?
1208 						     MLX4_RESERVE_ETH_BF_QP : 0) |
1209 						    (init_attr->cap.max_recv_wr ?
1210 						     MLX4_RESERVE_A0_QP : 0),
1211 						    qp->mqp.usage);
1212 		else
1213 			if (qp->flags & MLX4_IB_QP_NETIF)
1214 				err = mlx4_ib_steer_qp_alloc(dev, 1, &qpn);
1215 			else
1216 				err = mlx4_qp_reserve_range(dev->dev, 1, 1,
1217 							    &qpn, 0, qp->mqp.usage);
1218 		if (err)
1219 			goto err_proxy;
1220 	}
1221 
1222 	if (init_attr->create_flags & IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK)
1223 		qp->flags |= MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1224 
1225 	err = mlx4_qp_alloc(dev->dev, qpn, &qp->mqp);
1226 	if (err)
1227 		goto err_qpn;
1228 
1229 	if (init_attr->qp_type == IB_QPT_XRC_TGT)
1230 		qp->mqp.qpn |= (1 << 23);
1231 
1232 	/*
1233 	 * Hardware wants QPN written in big-endian order (after
1234 	 * shifting) for send doorbell.  Precompute this value to save
1235 	 * a little bit when posting sends.
1236 	 */
1237 	qp->doorbell_qpn = swab32(qp->mqp.qpn << 8);
1238 
1239 	qp->mqp.event = mlx4_ib_qp_event;
1240 
1241 	if (!*caller_qp)
1242 		*caller_qp = qp;
1243 
1244 	spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1245 	mlx4_ib_lock_cqs(to_mcq(init_attr->send_cq),
1246 			 to_mcq(init_attr->recv_cq));
1247 	/* Maintain device to QPs access, needed for further handling
1248 	 * via reset flow
1249 	 */
1250 	list_add_tail(&qp->qps_list, &dev->qp_list);
1251 	/* Maintain CQ to QPs access, needed for further handling
1252 	 * via reset flow
1253 	 */
1254 	mcq = to_mcq(init_attr->send_cq);
1255 	list_add_tail(&qp->cq_send_list, &mcq->send_qp_list);
1256 	mcq = to_mcq(init_attr->recv_cq);
1257 	list_add_tail(&qp->cq_recv_list, &mcq->recv_qp_list);
1258 	mlx4_ib_unlock_cqs(to_mcq(init_attr->send_cq),
1259 			   to_mcq(init_attr->recv_cq));
1260 	spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1261 	return 0;
1262 
1263 err_qpn:
1264 	if (!sqpn) {
1265 		if (qp->flags & MLX4_IB_QP_NETIF)
1266 			mlx4_ib_steer_qp_free(dev, qpn, 1);
1267 		else
1268 			mlx4_qp_release_range(dev->dev, qpn, 1);
1269 	}
1270 err_proxy:
1271 	if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
1272 		free_proxy_bufs(pd->device, qp);
1273 err_wrid:
1274 	if (udata) {
1275 		if (qp_has_rq(init_attr))
1276 			mlx4_ib_db_unmap_user(context, &qp->db);
1277 	} else {
1278 		kvfree(qp->sq.wrid);
1279 		kvfree(qp->rq.wrid);
1280 	}
1281 
1282 err_mtt:
1283 	mlx4_mtt_cleanup(dev->dev, &qp->mtt);
1284 
1285 err_buf:
1286 	if (!qp->umem)
1287 		mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
1288 	ib_umem_release(qp->umem);
1289 
1290 err_db:
1291 	if (!udata && qp_has_rq(init_attr))
1292 		mlx4_db_free(dev->dev, &qp->db);
1293 
1294 err:
1295 	if (!sqp && !*caller_qp)
1296 		kfree(qp);
1297 	kfree(sqp);
1298 
1299 	return err;
1300 }
1301 
1302 static enum mlx4_qp_state to_mlx4_state(enum ib_qp_state state)
1303 {
1304 	switch (state) {
1305 	case IB_QPS_RESET:	return MLX4_QP_STATE_RST;
1306 	case IB_QPS_INIT:	return MLX4_QP_STATE_INIT;
1307 	case IB_QPS_RTR:	return MLX4_QP_STATE_RTR;
1308 	case IB_QPS_RTS:	return MLX4_QP_STATE_RTS;
1309 	case IB_QPS_SQD:	return MLX4_QP_STATE_SQD;
1310 	case IB_QPS_SQE:	return MLX4_QP_STATE_SQER;
1311 	case IB_QPS_ERR:	return MLX4_QP_STATE_ERR;
1312 	default:		return -1;
1313 	}
1314 }
1315 
1316 static void mlx4_ib_lock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
1317 	__acquires(&send_cq->lock) __acquires(&recv_cq->lock)
1318 {
1319 	if (send_cq == recv_cq) {
1320 		spin_lock(&send_cq->lock);
1321 		__acquire(&recv_cq->lock);
1322 	} else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1323 		spin_lock(&send_cq->lock);
1324 		spin_lock_nested(&recv_cq->lock, SINGLE_DEPTH_NESTING);
1325 	} else {
1326 		spin_lock(&recv_cq->lock);
1327 		spin_lock_nested(&send_cq->lock, SINGLE_DEPTH_NESTING);
1328 	}
1329 }
1330 
1331 static void mlx4_ib_unlock_cqs(struct mlx4_ib_cq *send_cq, struct mlx4_ib_cq *recv_cq)
1332 	__releases(&send_cq->lock) __releases(&recv_cq->lock)
1333 {
1334 	if (send_cq == recv_cq) {
1335 		__release(&recv_cq->lock);
1336 		spin_unlock(&send_cq->lock);
1337 	} else if (send_cq->mcq.cqn < recv_cq->mcq.cqn) {
1338 		spin_unlock(&recv_cq->lock);
1339 		spin_unlock(&send_cq->lock);
1340 	} else {
1341 		spin_unlock(&send_cq->lock);
1342 		spin_unlock(&recv_cq->lock);
1343 	}
1344 }
1345 
1346 static void del_gid_entries(struct mlx4_ib_qp *qp)
1347 {
1348 	struct mlx4_ib_gid_entry *ge, *tmp;
1349 
1350 	list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
1351 		list_del(&ge->list);
1352 		kfree(ge);
1353 	}
1354 }
1355 
1356 static struct mlx4_ib_pd *get_pd(struct mlx4_ib_qp *qp)
1357 {
1358 	if (qp->ibqp.qp_type == IB_QPT_XRC_TGT)
1359 		return to_mpd(to_mxrcd(qp->ibqp.xrcd)->pd);
1360 	else
1361 		return to_mpd(qp->ibqp.pd);
1362 }
1363 
1364 static void get_cqs(struct mlx4_ib_qp *qp, enum mlx4_ib_source_type src,
1365 		    struct mlx4_ib_cq **send_cq, struct mlx4_ib_cq **recv_cq)
1366 {
1367 	switch (qp->ibqp.qp_type) {
1368 	case IB_QPT_XRC_TGT:
1369 		*send_cq = to_mcq(to_mxrcd(qp->ibqp.xrcd)->cq);
1370 		*recv_cq = *send_cq;
1371 		break;
1372 	case IB_QPT_XRC_INI:
1373 		*send_cq = to_mcq(qp->ibqp.send_cq);
1374 		*recv_cq = *send_cq;
1375 		break;
1376 	default:
1377 		*recv_cq = (src == MLX4_IB_QP_SRC) ? to_mcq(qp->ibqp.recv_cq) :
1378 						     to_mcq(qp->ibwq.cq);
1379 		*send_cq = (src == MLX4_IB_QP_SRC) ? to_mcq(qp->ibqp.send_cq) :
1380 						     *recv_cq;
1381 		break;
1382 	}
1383 }
1384 
1385 static void destroy_qp_rss(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
1386 {
1387 	if (qp->state != IB_QPS_RESET) {
1388 		int i;
1389 
1390 		for (i = 0; i < (1 << qp->ibqp.rwq_ind_tbl->log_ind_tbl_size);
1391 		     i++) {
1392 			struct ib_wq *ibwq = qp->ibqp.rwq_ind_tbl->ind_tbl[i];
1393 			struct mlx4_ib_qp *wq =	to_mqp((struct ib_qp *)ibwq);
1394 
1395 			mutex_lock(&wq->mutex);
1396 
1397 			wq->rss_usecnt--;
1398 
1399 			mutex_unlock(&wq->mutex);
1400 		}
1401 
1402 		if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
1403 				   MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
1404 			pr_warn("modify QP %06x to RESET failed.\n",
1405 				qp->mqp.qpn);
1406 	}
1407 
1408 	mlx4_qp_remove(dev->dev, &qp->mqp);
1409 	mlx4_qp_free(dev->dev, &qp->mqp);
1410 	mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
1411 	del_gid_entries(qp);
1412 	kfree(qp->rss_ctx);
1413 }
1414 
1415 static void destroy_qp_common(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp,
1416 			      enum mlx4_ib_source_type src,
1417 			      struct ib_udata *udata)
1418 {
1419 	struct mlx4_ib_cq *send_cq, *recv_cq;
1420 	unsigned long flags;
1421 
1422 	if (qp->state != IB_QPS_RESET) {
1423 		if (mlx4_qp_modify(dev->dev, NULL, to_mlx4_state(qp->state),
1424 				   MLX4_QP_STATE_RST, NULL, 0, 0, &qp->mqp))
1425 			pr_warn("modify QP %06x to RESET failed.\n",
1426 			       qp->mqp.qpn);
1427 		if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
1428 			mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
1429 			qp->pri.smac = 0;
1430 			qp->pri.smac_port = 0;
1431 		}
1432 		if (qp->alt.smac) {
1433 			mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
1434 			qp->alt.smac = 0;
1435 		}
1436 		if (qp->pri.vid < 0x1000) {
1437 			mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
1438 			qp->pri.vid = 0xFFFF;
1439 			qp->pri.candidate_vid = 0xFFFF;
1440 			qp->pri.update_vid = 0;
1441 		}
1442 		if (qp->alt.vid < 0x1000) {
1443 			mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
1444 			qp->alt.vid = 0xFFFF;
1445 			qp->alt.candidate_vid = 0xFFFF;
1446 			qp->alt.update_vid = 0;
1447 		}
1448 	}
1449 
1450 	get_cqs(qp, src, &send_cq, &recv_cq);
1451 
1452 	spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
1453 	mlx4_ib_lock_cqs(send_cq, recv_cq);
1454 
1455 	/* del from lists under both locks above to protect reset flow paths */
1456 	list_del(&qp->qps_list);
1457 	list_del(&qp->cq_send_list);
1458 	list_del(&qp->cq_recv_list);
1459 	if (!udata) {
1460 		__mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
1461 				 qp->ibqp.srq ? to_msrq(qp->ibqp.srq): NULL);
1462 		if (send_cq != recv_cq)
1463 			__mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
1464 	}
1465 
1466 	mlx4_qp_remove(dev->dev, &qp->mqp);
1467 
1468 	mlx4_ib_unlock_cqs(send_cq, recv_cq);
1469 	spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
1470 
1471 	mlx4_qp_free(dev->dev, &qp->mqp);
1472 
1473 	if (!is_sqp(dev, qp) && !is_tunnel_qp(dev, qp)) {
1474 		if (qp->flags & MLX4_IB_QP_NETIF)
1475 			mlx4_ib_steer_qp_free(dev, qp->mqp.qpn, 1);
1476 		else if (src == MLX4_IB_RWQ_SRC)
1477 			mlx4_ib_release_wqn(
1478 				rdma_udata_to_drv_context(
1479 					udata,
1480 					struct mlx4_ib_ucontext,
1481 					ibucontext),
1482 				qp, 1);
1483 		else
1484 			mlx4_qp_release_range(dev->dev, qp->mqp.qpn, 1);
1485 	}
1486 
1487 	mlx4_mtt_cleanup(dev->dev, &qp->mtt);
1488 
1489 	if (udata) {
1490 		if (qp->rq.wqe_cnt) {
1491 			struct mlx4_ib_ucontext *mcontext =
1492 				rdma_udata_to_drv_context(
1493 					udata,
1494 					struct mlx4_ib_ucontext,
1495 					ibucontext);
1496 
1497 			mlx4_ib_db_unmap_user(mcontext, &qp->db);
1498 		}
1499 	} else {
1500 		kvfree(qp->sq.wrid);
1501 		kvfree(qp->rq.wrid);
1502 		if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
1503 		    MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI))
1504 			free_proxy_bufs(&dev->ib_dev, qp);
1505 		mlx4_buf_free(dev->dev, qp->buf_size, &qp->buf);
1506 		if (qp->rq.wqe_cnt)
1507 			mlx4_db_free(dev->dev, &qp->db);
1508 	}
1509 	ib_umem_release(qp->umem);
1510 
1511 	del_gid_entries(qp);
1512 }
1513 
1514 static u32 get_sqp_num(struct mlx4_ib_dev *dev, struct ib_qp_init_attr *attr)
1515 {
1516 	/* Native or PPF */
1517 	if (!mlx4_is_mfunc(dev->dev) ||
1518 	    (mlx4_is_master(dev->dev) &&
1519 	     attr->create_flags & MLX4_IB_SRIOV_SQP)) {
1520 		return  dev->dev->phys_caps.base_sqpn +
1521 			(attr->qp_type == IB_QPT_SMI ? 0 : 2) +
1522 			attr->port_num - 1;
1523 	}
1524 	/* PF or VF -- creating proxies */
1525 	if (attr->qp_type == IB_QPT_SMI)
1526 		return dev->dev->caps.spec_qps[attr->port_num - 1].qp0_proxy;
1527 	else
1528 		return dev->dev->caps.spec_qps[attr->port_num - 1].qp1_proxy;
1529 }
1530 
1531 static struct ib_qp *_mlx4_ib_create_qp(struct ib_pd *pd,
1532 					struct ib_qp_init_attr *init_attr,
1533 					struct ib_udata *udata)
1534 {
1535 	struct mlx4_ib_qp *qp = NULL;
1536 	int err;
1537 	int sup_u_create_flags = MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK;
1538 	u16 xrcdn = 0;
1539 
1540 	if (init_attr->rwq_ind_tbl)
1541 		return _mlx4_ib_create_qp_rss(pd, init_attr, udata);
1542 
1543 	/*
1544 	 * We only support LSO, vendor flag1, and multicast loopback blocking,
1545 	 * and only for kernel UD QPs.
1546 	 */
1547 	if (init_attr->create_flags & ~(MLX4_IB_QP_LSO |
1548 					MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK |
1549 					MLX4_IB_SRIOV_TUNNEL_QP |
1550 					MLX4_IB_SRIOV_SQP |
1551 					MLX4_IB_QP_NETIF |
1552 					MLX4_IB_QP_CREATE_ROCE_V2_GSI))
1553 		return ERR_PTR(-EINVAL);
1554 
1555 	if (init_attr->create_flags & IB_QP_CREATE_NETIF_QP) {
1556 		if (init_attr->qp_type != IB_QPT_UD)
1557 			return ERR_PTR(-EINVAL);
1558 	}
1559 
1560 	if (init_attr->create_flags) {
1561 		if (udata && init_attr->create_flags & ~(sup_u_create_flags))
1562 			return ERR_PTR(-EINVAL);
1563 
1564 		if ((init_attr->create_flags & ~(MLX4_IB_SRIOV_SQP |
1565 						 MLX4_IB_QP_CREATE_ROCE_V2_GSI  |
1566 						 MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) &&
1567 		     init_attr->qp_type != IB_QPT_UD) ||
1568 		    (init_attr->create_flags & MLX4_IB_SRIOV_SQP &&
1569 		     init_attr->qp_type > IB_QPT_GSI) ||
1570 		    (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI &&
1571 		     init_attr->qp_type != IB_QPT_GSI))
1572 			return ERR_PTR(-EINVAL);
1573 	}
1574 
1575 	switch (init_attr->qp_type) {
1576 	case IB_QPT_XRC_TGT:
1577 		pd = to_mxrcd(init_attr->xrcd)->pd;
1578 		xrcdn = to_mxrcd(init_attr->xrcd)->xrcdn;
1579 		init_attr->send_cq = to_mxrcd(init_attr->xrcd)->cq;
1580 		/* fall through */
1581 	case IB_QPT_XRC_INI:
1582 		if (!(to_mdev(pd->device)->dev->caps.flags & MLX4_DEV_CAP_FLAG_XRC))
1583 			return ERR_PTR(-ENOSYS);
1584 		init_attr->recv_cq = init_attr->send_cq;
1585 		/* fall through */
1586 	case IB_QPT_RC:
1587 	case IB_QPT_UC:
1588 	case IB_QPT_RAW_PACKET:
1589 		qp = kzalloc(sizeof(*qp), GFP_KERNEL);
1590 		if (!qp)
1591 			return ERR_PTR(-ENOMEM);
1592 		qp->pri.vid = 0xFFFF;
1593 		qp->alt.vid = 0xFFFF;
1594 		/* fall through */
1595 	case IB_QPT_UD:
1596 	{
1597 		err = create_qp_common(pd, init_attr, udata, 0, &qp);
1598 		if (err) {
1599 			kfree(qp);
1600 			return ERR_PTR(err);
1601 		}
1602 
1603 		qp->ibqp.qp_num = qp->mqp.qpn;
1604 		qp->xrcdn = xrcdn;
1605 
1606 		break;
1607 	}
1608 	case IB_QPT_SMI:
1609 	case IB_QPT_GSI:
1610 	{
1611 		int sqpn;
1612 
1613 		/* Userspace is not allowed to create special QPs: */
1614 		if (udata)
1615 			return ERR_PTR(-EINVAL);
1616 		if (init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI) {
1617 			int res = mlx4_qp_reserve_range(to_mdev(pd->device)->dev,
1618 							1, 1, &sqpn, 0,
1619 							MLX4_RES_USAGE_DRIVER);
1620 
1621 			if (res)
1622 				return ERR_PTR(res);
1623 		} else {
1624 			sqpn = get_sqp_num(to_mdev(pd->device), init_attr);
1625 		}
1626 
1627 		err = create_qp_common(pd, init_attr, udata, sqpn, &qp);
1628 		if (err)
1629 			return ERR_PTR(err);
1630 
1631 		qp->port	= init_attr->port_num;
1632 		qp->ibqp.qp_num = init_attr->qp_type == IB_QPT_SMI ? 0 :
1633 			init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI ? sqpn : 1;
1634 		break;
1635 	}
1636 	default:
1637 		/* Don't support raw QPs */
1638 		return ERR_PTR(-EINVAL);
1639 	}
1640 
1641 	return &qp->ibqp;
1642 }
1643 
1644 struct ib_qp *mlx4_ib_create_qp(struct ib_pd *pd,
1645 				struct ib_qp_init_attr *init_attr,
1646 				struct ib_udata *udata) {
1647 	struct ib_device *device = pd ? pd->device : init_attr->xrcd->device;
1648 	struct ib_qp *ibqp;
1649 	struct mlx4_ib_dev *dev = to_mdev(device);
1650 
1651 	ibqp = _mlx4_ib_create_qp(pd, init_attr, udata);
1652 
1653 	if (!IS_ERR(ibqp) &&
1654 	    (init_attr->qp_type == IB_QPT_GSI) &&
1655 	    !(init_attr->create_flags & MLX4_IB_QP_CREATE_ROCE_V2_GSI)) {
1656 		struct mlx4_ib_sqp *sqp = to_msqp((to_mqp(ibqp)));
1657 		int is_eth = rdma_cap_eth_ah(&dev->ib_dev, init_attr->port_num);
1658 
1659 		if (is_eth &&
1660 		    dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2) {
1661 			init_attr->create_flags |= MLX4_IB_QP_CREATE_ROCE_V2_GSI;
1662 			sqp->roce_v2_gsi = ib_create_qp(pd, init_attr);
1663 
1664 			if (IS_ERR(sqp->roce_v2_gsi)) {
1665 				pr_err("Failed to create GSI QP for RoCEv2 (%ld)\n", PTR_ERR(sqp->roce_v2_gsi));
1666 				sqp->roce_v2_gsi = NULL;
1667 			} else {
1668 				sqp = to_msqp(to_mqp(sqp->roce_v2_gsi));
1669 				sqp->qp.flags |= MLX4_IB_ROCE_V2_GSI_QP;
1670 			}
1671 
1672 			init_attr->create_flags &= ~MLX4_IB_QP_CREATE_ROCE_V2_GSI;
1673 		}
1674 	}
1675 	return ibqp;
1676 }
1677 
1678 static int _mlx4_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
1679 {
1680 	struct mlx4_ib_dev *dev = to_mdev(qp->device);
1681 	struct mlx4_ib_qp *mqp = to_mqp(qp);
1682 
1683 	if (is_qp0(dev, mqp))
1684 		mlx4_CLOSE_PORT(dev->dev, mqp->port);
1685 
1686 	if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI &&
1687 	    dev->qp1_proxy[mqp->port - 1] == mqp) {
1688 		mutex_lock(&dev->qp1_proxy_lock[mqp->port - 1]);
1689 		dev->qp1_proxy[mqp->port - 1] = NULL;
1690 		mutex_unlock(&dev->qp1_proxy_lock[mqp->port - 1]);
1691 	}
1692 
1693 	if (mqp->counter_index)
1694 		mlx4_ib_free_qp_counter(dev, mqp);
1695 
1696 	if (qp->rwq_ind_tbl) {
1697 		destroy_qp_rss(dev, mqp);
1698 	} else {
1699 		destroy_qp_common(dev, mqp, MLX4_IB_QP_SRC, udata);
1700 	}
1701 
1702 	if (is_sqp(dev, mqp))
1703 		kfree(to_msqp(mqp));
1704 	else
1705 		kfree(mqp);
1706 
1707 	return 0;
1708 }
1709 
1710 int mlx4_ib_destroy_qp(struct ib_qp *qp, struct ib_udata *udata)
1711 {
1712 	struct mlx4_ib_qp *mqp = to_mqp(qp);
1713 
1714 	if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
1715 		struct mlx4_ib_sqp *sqp = to_msqp(mqp);
1716 
1717 		if (sqp->roce_v2_gsi)
1718 			ib_destroy_qp(sqp->roce_v2_gsi);
1719 	}
1720 
1721 	return _mlx4_ib_destroy_qp(qp, udata);
1722 }
1723 
1724 static int to_mlx4_st(struct mlx4_ib_dev *dev, enum mlx4_ib_qp_type type)
1725 {
1726 	switch (type) {
1727 	case MLX4_IB_QPT_RC:		return MLX4_QP_ST_RC;
1728 	case MLX4_IB_QPT_UC:		return MLX4_QP_ST_UC;
1729 	case MLX4_IB_QPT_UD:		return MLX4_QP_ST_UD;
1730 	case MLX4_IB_QPT_XRC_INI:
1731 	case MLX4_IB_QPT_XRC_TGT:	return MLX4_QP_ST_XRC;
1732 	case MLX4_IB_QPT_SMI:
1733 	case MLX4_IB_QPT_GSI:
1734 	case MLX4_IB_QPT_RAW_PACKET:	return MLX4_QP_ST_MLX;
1735 
1736 	case MLX4_IB_QPT_PROXY_SMI_OWNER:
1737 	case MLX4_IB_QPT_TUN_SMI_OWNER:	return (mlx4_is_mfunc(dev->dev) ?
1738 						MLX4_QP_ST_MLX : -1);
1739 	case MLX4_IB_QPT_PROXY_SMI:
1740 	case MLX4_IB_QPT_TUN_SMI:
1741 	case MLX4_IB_QPT_PROXY_GSI:
1742 	case MLX4_IB_QPT_TUN_GSI:	return (mlx4_is_mfunc(dev->dev) ?
1743 						MLX4_QP_ST_UD : -1);
1744 	default:			return -1;
1745 	}
1746 }
1747 
1748 static __be32 to_mlx4_access_flags(struct mlx4_ib_qp *qp, const struct ib_qp_attr *attr,
1749 				   int attr_mask)
1750 {
1751 	u8 dest_rd_atomic;
1752 	u32 access_flags;
1753 	u32 hw_access_flags = 0;
1754 
1755 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
1756 		dest_rd_atomic = attr->max_dest_rd_atomic;
1757 	else
1758 		dest_rd_atomic = qp->resp_depth;
1759 
1760 	if (attr_mask & IB_QP_ACCESS_FLAGS)
1761 		access_flags = attr->qp_access_flags;
1762 	else
1763 		access_flags = qp->atomic_rd_en;
1764 
1765 	if (!dest_rd_atomic)
1766 		access_flags &= IB_ACCESS_REMOTE_WRITE;
1767 
1768 	if (access_flags & IB_ACCESS_REMOTE_READ)
1769 		hw_access_flags |= MLX4_QP_BIT_RRE;
1770 	if (access_flags & IB_ACCESS_REMOTE_ATOMIC)
1771 		hw_access_flags |= MLX4_QP_BIT_RAE;
1772 	if (access_flags & IB_ACCESS_REMOTE_WRITE)
1773 		hw_access_flags |= MLX4_QP_BIT_RWE;
1774 
1775 	return cpu_to_be32(hw_access_flags);
1776 }
1777 
1778 static void store_sqp_attrs(struct mlx4_ib_sqp *sqp, const struct ib_qp_attr *attr,
1779 			    int attr_mask)
1780 {
1781 	if (attr_mask & IB_QP_PKEY_INDEX)
1782 		sqp->pkey_index = attr->pkey_index;
1783 	if (attr_mask & IB_QP_QKEY)
1784 		sqp->qkey = attr->qkey;
1785 	if (attr_mask & IB_QP_SQ_PSN)
1786 		sqp->send_psn = attr->sq_psn;
1787 }
1788 
1789 static void mlx4_set_sched(struct mlx4_qp_path *path, u8 port)
1790 {
1791 	path->sched_queue = (path->sched_queue & 0xbf) | ((port - 1) << 6);
1792 }
1793 
1794 static int _mlx4_set_path(struct mlx4_ib_dev *dev,
1795 			  const struct rdma_ah_attr *ah,
1796 			  u64 smac, u16 vlan_tag, struct mlx4_qp_path *path,
1797 			  struct mlx4_roce_smac_vlan_info *smac_info, u8 port)
1798 {
1799 	int vidx;
1800 	int smac_index;
1801 	int err;
1802 
1803 	path->grh_mylmc = rdma_ah_get_path_bits(ah) & 0x7f;
1804 	path->rlid = cpu_to_be16(rdma_ah_get_dlid(ah));
1805 	if (rdma_ah_get_static_rate(ah)) {
1806 		path->static_rate = rdma_ah_get_static_rate(ah) +
1807 				    MLX4_STAT_RATE_OFFSET;
1808 		while (path->static_rate > IB_RATE_2_5_GBPS + MLX4_STAT_RATE_OFFSET &&
1809 		       !(1 << path->static_rate & dev->dev->caps.stat_rate_support))
1810 			--path->static_rate;
1811 	} else
1812 		path->static_rate = 0;
1813 
1814 	if (rdma_ah_get_ah_flags(ah) & IB_AH_GRH) {
1815 		const struct ib_global_route *grh = rdma_ah_read_grh(ah);
1816 		int real_sgid_index =
1817 			mlx4_ib_gid_index_to_real_index(dev, grh->sgid_attr);
1818 
1819 		if (real_sgid_index < 0)
1820 			return real_sgid_index;
1821 		if (real_sgid_index >= dev->dev->caps.gid_table_len[port]) {
1822 			pr_err("sgid_index (%u) too large. max is %d\n",
1823 			       real_sgid_index, dev->dev->caps.gid_table_len[port] - 1);
1824 			return -1;
1825 		}
1826 
1827 		path->grh_mylmc |= 1 << 7;
1828 		path->mgid_index = real_sgid_index;
1829 		path->hop_limit  = grh->hop_limit;
1830 		path->tclass_flowlabel =
1831 			cpu_to_be32((grh->traffic_class << 20) |
1832 				    (grh->flow_label));
1833 		memcpy(path->rgid, grh->dgid.raw, 16);
1834 	}
1835 
1836 	if (ah->type == RDMA_AH_ATTR_TYPE_ROCE) {
1837 		if (!(rdma_ah_get_ah_flags(ah) & IB_AH_GRH))
1838 			return -1;
1839 
1840 		path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
1841 			((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 7) << 3);
1842 
1843 		path->feup |= MLX4_FEUP_FORCE_ETH_UP;
1844 		if (vlan_tag < 0x1000) {
1845 			if (smac_info->vid < 0x1000) {
1846 				/* both valid vlan ids */
1847 				if (smac_info->vid != vlan_tag) {
1848 					/* different VIDs.  unreg old and reg new */
1849 					err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
1850 					if (err)
1851 						return err;
1852 					smac_info->candidate_vid = vlan_tag;
1853 					smac_info->candidate_vlan_index = vidx;
1854 					smac_info->candidate_vlan_port = port;
1855 					smac_info->update_vid = 1;
1856 					path->vlan_index = vidx;
1857 				} else {
1858 					path->vlan_index = smac_info->vlan_index;
1859 				}
1860 			} else {
1861 				/* no current vlan tag in qp */
1862 				err = mlx4_register_vlan(dev->dev, port, vlan_tag, &vidx);
1863 				if (err)
1864 					return err;
1865 				smac_info->candidate_vid = vlan_tag;
1866 				smac_info->candidate_vlan_index = vidx;
1867 				smac_info->candidate_vlan_port = port;
1868 				smac_info->update_vid = 1;
1869 				path->vlan_index = vidx;
1870 			}
1871 			path->feup |= MLX4_FVL_FORCE_ETH_VLAN;
1872 			path->fl = 1 << 6;
1873 		} else {
1874 			/* have current vlan tag. unregister it at modify-qp success */
1875 			if (smac_info->vid < 0x1000) {
1876 				smac_info->candidate_vid = 0xFFFF;
1877 				smac_info->update_vid = 1;
1878 			}
1879 		}
1880 
1881 		/* get smac_index for RoCE use.
1882 		 * If no smac was yet assigned, register one.
1883 		 * If one was already assigned, but the new mac differs,
1884 		 * unregister the old one and register the new one.
1885 		*/
1886 		if ((!smac_info->smac && !smac_info->smac_port) ||
1887 		    smac_info->smac != smac) {
1888 			/* register candidate now, unreg if needed, after success */
1889 			smac_index = mlx4_register_mac(dev->dev, port, smac);
1890 			if (smac_index >= 0) {
1891 				smac_info->candidate_smac_index = smac_index;
1892 				smac_info->candidate_smac = smac;
1893 				smac_info->candidate_smac_port = port;
1894 			} else {
1895 				return -EINVAL;
1896 			}
1897 		} else {
1898 			smac_index = smac_info->smac_index;
1899 		}
1900 		memcpy(path->dmac, ah->roce.dmac, 6);
1901 		path->ackto = MLX4_IB_LINK_TYPE_ETH;
1902 		/* put MAC table smac index for IBoE */
1903 		path->grh_mylmc = (u8) (smac_index) | 0x80;
1904 	} else {
1905 		path->sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE |
1906 			((port - 1) << 6) | ((rdma_ah_get_sl(ah) & 0xf) << 2);
1907 	}
1908 
1909 	return 0;
1910 }
1911 
1912 static int mlx4_set_path(struct mlx4_ib_dev *dev, const struct ib_qp_attr *qp,
1913 			 enum ib_qp_attr_mask qp_attr_mask,
1914 			 struct mlx4_ib_qp *mqp,
1915 			 struct mlx4_qp_path *path, u8 port,
1916 			 u16 vlan_id, u8 *smac)
1917 {
1918 	return _mlx4_set_path(dev, &qp->ah_attr,
1919 			      mlx4_mac_to_u64(smac),
1920 			      vlan_id,
1921 			      path, &mqp->pri, port);
1922 }
1923 
1924 static int mlx4_set_alt_path(struct mlx4_ib_dev *dev,
1925 			     const struct ib_qp_attr *qp,
1926 			     enum ib_qp_attr_mask qp_attr_mask,
1927 			     struct mlx4_ib_qp *mqp,
1928 			     struct mlx4_qp_path *path, u8 port)
1929 {
1930 	return _mlx4_set_path(dev, &qp->alt_ah_attr,
1931 			      0,
1932 			      0xffff,
1933 			      path, &mqp->alt, port);
1934 }
1935 
1936 static void update_mcg_macs(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
1937 {
1938 	struct mlx4_ib_gid_entry *ge, *tmp;
1939 
1940 	list_for_each_entry_safe(ge, tmp, &qp->gid_list, list) {
1941 		if (!ge->added && mlx4_ib_add_mc(dev, qp, &ge->gid)) {
1942 			ge->added = 1;
1943 			ge->port = qp->port;
1944 		}
1945 	}
1946 }
1947 
1948 static int handle_eth_ud_smac_index(struct mlx4_ib_dev *dev,
1949 				    struct mlx4_ib_qp *qp,
1950 				    struct mlx4_qp_context *context)
1951 {
1952 	u64 u64_mac;
1953 	int smac_index;
1954 
1955 	u64_mac = atomic64_read(&dev->iboe.mac[qp->port - 1]);
1956 
1957 	context->pri_path.sched_queue = MLX4_IB_DEFAULT_SCHED_QUEUE | ((qp->port - 1) << 6);
1958 	if (!qp->pri.smac && !qp->pri.smac_port) {
1959 		smac_index = mlx4_register_mac(dev->dev, qp->port, u64_mac);
1960 		if (smac_index >= 0) {
1961 			qp->pri.candidate_smac_index = smac_index;
1962 			qp->pri.candidate_smac = u64_mac;
1963 			qp->pri.candidate_smac_port = qp->port;
1964 			context->pri_path.grh_mylmc = 0x80 | (u8) smac_index;
1965 		} else {
1966 			return -ENOENT;
1967 		}
1968 	}
1969 	return 0;
1970 }
1971 
1972 static int create_qp_lb_counter(struct mlx4_ib_dev *dev, struct mlx4_ib_qp *qp)
1973 {
1974 	struct counter_index *new_counter_index;
1975 	int err;
1976 	u32 tmp_idx;
1977 
1978 	if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) !=
1979 	    IB_LINK_LAYER_ETHERNET ||
1980 	    !(qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK) ||
1981 	    !(dev->dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_LB_SRC_CHK))
1982 		return 0;
1983 
1984 	err = mlx4_counter_alloc(dev->dev, &tmp_idx, MLX4_RES_USAGE_DRIVER);
1985 	if (err)
1986 		return err;
1987 
1988 	new_counter_index = kmalloc(sizeof(*new_counter_index), GFP_KERNEL);
1989 	if (!new_counter_index) {
1990 		mlx4_counter_free(dev->dev, tmp_idx);
1991 		return -ENOMEM;
1992 	}
1993 
1994 	new_counter_index->index = tmp_idx;
1995 	new_counter_index->allocated = 1;
1996 	qp->counter_index = new_counter_index;
1997 
1998 	mutex_lock(&dev->counters_table[qp->port - 1].mutex);
1999 	list_add_tail(&new_counter_index->list,
2000 		      &dev->counters_table[qp->port - 1].counters_list);
2001 	mutex_unlock(&dev->counters_table[qp->port - 1].mutex);
2002 
2003 	return 0;
2004 }
2005 
2006 enum {
2007 	MLX4_QPC_ROCE_MODE_1 = 0,
2008 	MLX4_QPC_ROCE_MODE_2 = 2,
2009 	MLX4_QPC_ROCE_MODE_UNDEFINED = 0xff
2010 };
2011 
2012 static u8 gid_type_to_qpc(enum ib_gid_type gid_type)
2013 {
2014 	switch (gid_type) {
2015 	case IB_GID_TYPE_ROCE:
2016 		return MLX4_QPC_ROCE_MODE_1;
2017 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
2018 		return MLX4_QPC_ROCE_MODE_2;
2019 	default:
2020 		return MLX4_QPC_ROCE_MODE_UNDEFINED;
2021 	}
2022 }
2023 
2024 /*
2025  * Go over all RSS QP's childes (WQs) and apply their HW state according to
2026  * their logic state if the RSS QP is the first RSS QP associated for the WQ.
2027  */
2028 static int bringup_rss_rwqs(struct ib_rwq_ind_table *ind_tbl, u8 port_num,
2029 			    struct ib_udata *udata)
2030 {
2031 	int err = 0;
2032 	int i;
2033 
2034 	for (i = 0; i < (1 << ind_tbl->log_ind_tbl_size); i++) {
2035 		struct ib_wq *ibwq = ind_tbl->ind_tbl[i];
2036 		struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
2037 
2038 		mutex_lock(&wq->mutex);
2039 
2040 		/* Mlx4_ib restrictions:
2041 		 * WQ's is associated to a port according to the RSS QP it is
2042 		 * associates to.
2043 		 * In case the WQ is associated to a different port by another
2044 		 * RSS QP, return a failure.
2045 		 */
2046 		if ((wq->rss_usecnt > 0) && (wq->port != port_num)) {
2047 			err = -EINVAL;
2048 			mutex_unlock(&wq->mutex);
2049 			break;
2050 		}
2051 		wq->port = port_num;
2052 		if ((wq->rss_usecnt == 0) && (ibwq->state == IB_WQS_RDY)) {
2053 			err = _mlx4_ib_modify_wq(ibwq, IB_WQS_RDY, udata);
2054 			if (err) {
2055 				mutex_unlock(&wq->mutex);
2056 				break;
2057 			}
2058 		}
2059 		wq->rss_usecnt++;
2060 
2061 		mutex_unlock(&wq->mutex);
2062 	}
2063 
2064 	if (i && err) {
2065 		int j;
2066 
2067 		for (j = (i - 1); j >= 0; j--) {
2068 			struct ib_wq *ibwq = ind_tbl->ind_tbl[j];
2069 			struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
2070 
2071 			mutex_lock(&wq->mutex);
2072 
2073 			if ((wq->rss_usecnt == 1) &&
2074 			    (ibwq->state == IB_WQS_RDY))
2075 				if (_mlx4_ib_modify_wq(ibwq, IB_WQS_RESET,
2076 						       udata))
2077 					pr_warn("failed to reverse WQN=0x%06x\n",
2078 						ibwq->wq_num);
2079 			wq->rss_usecnt--;
2080 
2081 			mutex_unlock(&wq->mutex);
2082 		}
2083 	}
2084 
2085 	return err;
2086 }
2087 
2088 static void bring_down_rss_rwqs(struct ib_rwq_ind_table *ind_tbl,
2089 				struct ib_udata *udata)
2090 {
2091 	int i;
2092 
2093 	for (i = 0; i < (1 << ind_tbl->log_ind_tbl_size); i++) {
2094 		struct ib_wq *ibwq = ind_tbl->ind_tbl[i];
2095 		struct mlx4_ib_qp *wq = to_mqp((struct ib_qp *)ibwq);
2096 
2097 		mutex_lock(&wq->mutex);
2098 
2099 		if ((wq->rss_usecnt == 1) && (ibwq->state == IB_WQS_RDY))
2100 			if (_mlx4_ib_modify_wq(ibwq, IB_WQS_RESET, udata))
2101 				pr_warn("failed to reverse WQN=%x\n",
2102 					ibwq->wq_num);
2103 		wq->rss_usecnt--;
2104 
2105 		mutex_unlock(&wq->mutex);
2106 	}
2107 }
2108 
2109 static void fill_qp_rss_context(struct mlx4_qp_context *context,
2110 				struct mlx4_ib_qp *qp)
2111 {
2112 	struct mlx4_rss_context *rss_context;
2113 
2114 	rss_context = (void *)context + offsetof(struct mlx4_qp_context,
2115 			pri_path) + MLX4_RSS_OFFSET_IN_QPC_PRI_PATH;
2116 
2117 	rss_context->base_qpn = cpu_to_be32(qp->rss_ctx->base_qpn_tbl_sz);
2118 	rss_context->default_qpn =
2119 		cpu_to_be32(qp->rss_ctx->base_qpn_tbl_sz & 0xffffff);
2120 	if (qp->rss_ctx->flags & (MLX4_RSS_UDP_IPV4 | MLX4_RSS_UDP_IPV6))
2121 		rss_context->base_qpn_udp = rss_context->default_qpn;
2122 	rss_context->flags = qp->rss_ctx->flags;
2123 	/* Currently support just toeplitz */
2124 	rss_context->hash_fn = MLX4_RSS_HASH_TOP;
2125 
2126 	memcpy(rss_context->rss_key, qp->rss_ctx->rss_key,
2127 	       MLX4_EN_RSS_KEY_SIZE);
2128 }
2129 
2130 static int __mlx4_ib_modify_qp(void *src, enum mlx4_ib_source_type src_type,
2131 			       const struct ib_qp_attr *attr, int attr_mask,
2132 			       enum ib_qp_state cur_state,
2133 			       enum ib_qp_state new_state,
2134 			       struct ib_udata *udata)
2135 {
2136 	struct ib_srq  *ibsrq;
2137 	const struct ib_gid_attr *gid_attr = NULL;
2138 	struct ib_rwq_ind_table *rwq_ind_tbl;
2139 	enum ib_qp_type qp_type;
2140 	struct mlx4_ib_dev *dev;
2141 	struct mlx4_ib_qp *qp;
2142 	struct mlx4_ib_pd *pd;
2143 	struct mlx4_ib_cq *send_cq, *recv_cq;
2144 	struct mlx4_ib_ucontext *ucontext = rdma_udata_to_drv_context(
2145 		udata, struct mlx4_ib_ucontext, ibucontext);
2146 	struct mlx4_qp_context *context;
2147 	enum mlx4_qp_optpar optpar = 0;
2148 	int sqd_event;
2149 	int steer_qp = 0;
2150 	int err = -EINVAL;
2151 	int counter_index;
2152 
2153 	if (src_type == MLX4_IB_RWQ_SRC) {
2154 		struct ib_wq *ibwq;
2155 
2156 		ibwq	    = (struct ib_wq *)src;
2157 		ibsrq	    = NULL;
2158 		rwq_ind_tbl = NULL;
2159 		qp_type     = IB_QPT_RAW_PACKET;
2160 		qp	    = to_mqp((struct ib_qp *)ibwq);
2161 		dev	    = to_mdev(ibwq->device);
2162 		pd	    = to_mpd(ibwq->pd);
2163 	} else {
2164 		struct ib_qp *ibqp;
2165 
2166 		ibqp	    = (struct ib_qp *)src;
2167 		ibsrq	    = ibqp->srq;
2168 		rwq_ind_tbl = ibqp->rwq_ind_tbl;
2169 		qp_type     = ibqp->qp_type;
2170 		qp	    = to_mqp(ibqp);
2171 		dev	    = to_mdev(ibqp->device);
2172 		pd	    = get_pd(qp);
2173 	}
2174 
2175 	/* APM is not supported under RoCE */
2176 	if (attr_mask & IB_QP_ALT_PATH &&
2177 	    rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
2178 	    IB_LINK_LAYER_ETHERNET)
2179 		return -ENOTSUPP;
2180 
2181 	context = kzalloc(sizeof *context, GFP_KERNEL);
2182 	if (!context)
2183 		return -ENOMEM;
2184 
2185 	context->flags = cpu_to_be32((to_mlx4_state(new_state) << 28) |
2186 				     (to_mlx4_st(dev, qp->mlx4_ib_qp_type) << 16));
2187 
2188 	if (!(attr_mask & IB_QP_PATH_MIG_STATE))
2189 		context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
2190 	else {
2191 		optpar |= MLX4_QP_OPTPAR_PM_STATE;
2192 		switch (attr->path_mig_state) {
2193 		case IB_MIG_MIGRATED:
2194 			context->flags |= cpu_to_be32(MLX4_QP_PM_MIGRATED << 11);
2195 			break;
2196 		case IB_MIG_REARM:
2197 			context->flags |= cpu_to_be32(MLX4_QP_PM_REARM << 11);
2198 			break;
2199 		case IB_MIG_ARMED:
2200 			context->flags |= cpu_to_be32(MLX4_QP_PM_ARMED << 11);
2201 			break;
2202 		}
2203 	}
2204 
2205 	if (qp->inl_recv_sz)
2206 		context->param3 |= cpu_to_be32(1 << 25);
2207 
2208 	if (qp->flags & MLX4_IB_QP_SCATTER_FCS)
2209 		context->param3 |= cpu_to_be32(1 << 29);
2210 
2211 	if (qp_type == IB_QPT_GSI || qp_type == IB_QPT_SMI)
2212 		context->mtu_msgmax = (IB_MTU_4096 << 5) | 11;
2213 	else if (qp_type == IB_QPT_RAW_PACKET)
2214 		context->mtu_msgmax = (MLX4_RAW_QP_MTU << 5) | MLX4_RAW_QP_MSGMAX;
2215 	else if (qp_type == IB_QPT_UD) {
2216 		if (qp->flags & MLX4_IB_QP_LSO)
2217 			context->mtu_msgmax = (IB_MTU_4096 << 5) |
2218 					      ilog2(dev->dev->caps.max_gso_sz);
2219 		else
2220 			context->mtu_msgmax = (IB_MTU_4096 << 5) | 13;
2221 	} else if (attr_mask & IB_QP_PATH_MTU) {
2222 		if (attr->path_mtu < IB_MTU_256 || attr->path_mtu > IB_MTU_4096) {
2223 			pr_err("path MTU (%u) is invalid\n",
2224 			       attr->path_mtu);
2225 			goto out;
2226 		}
2227 		context->mtu_msgmax = (attr->path_mtu << 5) |
2228 			ilog2(dev->dev->caps.max_msg_sz);
2229 	}
2230 
2231 	if (!rwq_ind_tbl) { /* PRM RSS receive side should be left zeros */
2232 		if (qp->rq.wqe_cnt)
2233 			context->rq_size_stride = ilog2(qp->rq.wqe_cnt) << 3;
2234 		context->rq_size_stride |= qp->rq.wqe_shift - 4;
2235 	}
2236 
2237 	if (qp->sq.wqe_cnt)
2238 		context->sq_size_stride = ilog2(qp->sq.wqe_cnt) << 3;
2239 	context->sq_size_stride |= qp->sq.wqe_shift - 4;
2240 
2241 	if (new_state == IB_QPS_RESET && qp->counter_index)
2242 		mlx4_ib_free_qp_counter(dev, qp);
2243 
2244 	if (cur_state == IB_QPS_RESET && new_state == IB_QPS_INIT) {
2245 		context->sq_size_stride |= !!qp->sq_no_prefetch << 7;
2246 		context->xrcd = cpu_to_be32((u32) qp->xrcdn);
2247 		if (qp_type == IB_QPT_RAW_PACKET)
2248 			context->param3 |= cpu_to_be32(1 << 30);
2249 	}
2250 
2251 	if (ucontext)
2252 		context->usr_page = cpu_to_be32(
2253 			mlx4_to_hw_uar_index(dev->dev, ucontext->uar.index));
2254 	else
2255 		context->usr_page = cpu_to_be32(
2256 			mlx4_to_hw_uar_index(dev->dev, dev->priv_uar.index));
2257 
2258 	if (attr_mask & IB_QP_DEST_QPN)
2259 		context->remote_qpn = cpu_to_be32(attr->dest_qp_num);
2260 
2261 	if (attr_mask & IB_QP_PORT) {
2262 		if (cur_state == IB_QPS_SQD && new_state == IB_QPS_SQD &&
2263 		    !(attr_mask & IB_QP_AV)) {
2264 			mlx4_set_sched(&context->pri_path, attr->port_num);
2265 			optpar |= MLX4_QP_OPTPAR_SCHED_QUEUE;
2266 		}
2267 	}
2268 
2269 	if (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR) {
2270 		err = create_qp_lb_counter(dev, qp);
2271 		if (err)
2272 			goto out;
2273 
2274 		counter_index =
2275 			dev->counters_table[qp->port - 1].default_counter;
2276 		if (qp->counter_index)
2277 			counter_index = qp->counter_index->index;
2278 
2279 		if (counter_index != -1) {
2280 			context->pri_path.counter_index = counter_index;
2281 			optpar |= MLX4_QP_OPTPAR_COUNTER_INDEX;
2282 			if (qp->counter_index) {
2283 				context->pri_path.fl |=
2284 					MLX4_FL_ETH_SRC_CHECK_MC_LB;
2285 				context->pri_path.vlan_control |=
2286 					MLX4_CTRL_ETH_SRC_CHECK_IF_COUNTER;
2287 			}
2288 		} else
2289 			context->pri_path.counter_index =
2290 				MLX4_SINK_COUNTER_INDEX(dev->dev);
2291 
2292 		if (qp->flags & MLX4_IB_QP_NETIF) {
2293 			mlx4_ib_steer_qp_reg(dev, qp, 1);
2294 			steer_qp = 1;
2295 		}
2296 
2297 		if (qp_type == IB_QPT_GSI) {
2298 			enum ib_gid_type gid_type = qp->flags & MLX4_IB_ROCE_V2_GSI_QP ?
2299 				IB_GID_TYPE_ROCE_UDP_ENCAP : IB_GID_TYPE_ROCE;
2300 			u8 qpc_roce_mode = gid_type_to_qpc(gid_type);
2301 
2302 			context->rlkey_roce_mode |= (qpc_roce_mode << 6);
2303 		}
2304 	}
2305 
2306 	if (attr_mask & IB_QP_PKEY_INDEX) {
2307 		if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
2308 			context->pri_path.disable_pkey_check = 0x40;
2309 		context->pri_path.pkey_index = attr->pkey_index;
2310 		optpar |= MLX4_QP_OPTPAR_PKEY_INDEX;
2311 	}
2312 
2313 	if (attr_mask & IB_QP_AV) {
2314 		u8 port_num = mlx4_is_bonded(dev->dev) ? 1 :
2315 			attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
2316 		u16 vlan = 0xffff;
2317 		u8 smac[ETH_ALEN];
2318 		int is_eth =
2319 			rdma_cap_eth_ah(&dev->ib_dev, port_num) &&
2320 			rdma_ah_get_ah_flags(&attr->ah_attr) & IB_AH_GRH;
2321 
2322 		if (is_eth) {
2323 			gid_attr = attr->ah_attr.grh.sgid_attr;
2324 			err = rdma_read_gid_l2_fields(gid_attr, &vlan,
2325 						      &smac[0]);
2326 			if (err)
2327 				goto out;
2328 		}
2329 
2330 		if (mlx4_set_path(dev, attr, attr_mask, qp, &context->pri_path,
2331 				  port_num, vlan, smac))
2332 			goto out;
2333 
2334 		optpar |= (MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH |
2335 			   MLX4_QP_OPTPAR_SCHED_QUEUE);
2336 
2337 		if (is_eth &&
2338 		    (cur_state == IB_QPS_INIT && new_state == IB_QPS_RTR)) {
2339 			u8 qpc_roce_mode = gid_type_to_qpc(gid_attr->gid_type);
2340 
2341 			if (qpc_roce_mode == MLX4_QPC_ROCE_MODE_UNDEFINED) {
2342 				err = -EINVAL;
2343 				goto out;
2344 			}
2345 			context->rlkey_roce_mode |= (qpc_roce_mode << 6);
2346 		}
2347 
2348 	}
2349 
2350 	if (attr_mask & IB_QP_TIMEOUT) {
2351 		context->pri_path.ackto |= attr->timeout << 3;
2352 		optpar |= MLX4_QP_OPTPAR_ACK_TIMEOUT;
2353 	}
2354 
2355 	if (attr_mask & IB_QP_ALT_PATH) {
2356 		if (attr->alt_port_num == 0 ||
2357 		    attr->alt_port_num > dev->dev->caps.num_ports)
2358 			goto out;
2359 
2360 		if (attr->alt_pkey_index >=
2361 		    dev->dev->caps.pkey_table_len[attr->alt_port_num])
2362 			goto out;
2363 
2364 		if (mlx4_set_alt_path(dev, attr, attr_mask, qp,
2365 				      &context->alt_path,
2366 				      attr->alt_port_num))
2367 			goto out;
2368 
2369 		context->alt_path.pkey_index = attr->alt_pkey_index;
2370 		context->alt_path.ackto = attr->alt_timeout << 3;
2371 		optpar |= MLX4_QP_OPTPAR_ALT_ADDR_PATH;
2372 	}
2373 
2374 	context->pd = cpu_to_be32(pd->pdn);
2375 
2376 	if (!rwq_ind_tbl) {
2377 		context->params1 = cpu_to_be32(MLX4_IB_ACK_REQ_FREQ << 28);
2378 		get_cqs(qp, src_type, &send_cq, &recv_cq);
2379 	} else { /* Set dummy CQs to be compatible with HV and PRM */
2380 		send_cq = to_mcq(rwq_ind_tbl->ind_tbl[0]->cq);
2381 		recv_cq = send_cq;
2382 	}
2383 	context->cqn_send = cpu_to_be32(send_cq->mcq.cqn);
2384 	context->cqn_recv = cpu_to_be32(recv_cq->mcq.cqn);
2385 
2386 	/* Set "fast registration enabled" for all kernel QPs */
2387 	if (!ucontext)
2388 		context->params1 |= cpu_to_be32(1 << 11);
2389 
2390 	if (attr_mask & IB_QP_RNR_RETRY) {
2391 		context->params1 |= cpu_to_be32(attr->rnr_retry << 13);
2392 		optpar |= MLX4_QP_OPTPAR_RNR_RETRY;
2393 	}
2394 
2395 	if (attr_mask & IB_QP_RETRY_CNT) {
2396 		context->params1 |= cpu_to_be32(attr->retry_cnt << 16);
2397 		optpar |= MLX4_QP_OPTPAR_RETRY_COUNT;
2398 	}
2399 
2400 	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC) {
2401 		if (attr->max_rd_atomic)
2402 			context->params1 |=
2403 				cpu_to_be32(fls(attr->max_rd_atomic - 1) << 21);
2404 		optpar |= MLX4_QP_OPTPAR_SRA_MAX;
2405 	}
2406 
2407 	if (attr_mask & IB_QP_SQ_PSN)
2408 		context->next_send_psn = cpu_to_be32(attr->sq_psn);
2409 
2410 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC) {
2411 		if (attr->max_dest_rd_atomic)
2412 			context->params2 |=
2413 				cpu_to_be32(fls(attr->max_dest_rd_atomic - 1) << 21);
2414 		optpar |= MLX4_QP_OPTPAR_RRA_MAX;
2415 	}
2416 
2417 	if (attr_mask & (IB_QP_ACCESS_FLAGS | IB_QP_MAX_DEST_RD_ATOMIC)) {
2418 		context->params2 |= to_mlx4_access_flags(qp, attr, attr_mask);
2419 		optpar |= MLX4_QP_OPTPAR_RWE | MLX4_QP_OPTPAR_RRE | MLX4_QP_OPTPAR_RAE;
2420 	}
2421 
2422 	if (ibsrq)
2423 		context->params2 |= cpu_to_be32(MLX4_QP_BIT_RIC);
2424 
2425 	if (attr_mask & IB_QP_MIN_RNR_TIMER) {
2426 		context->rnr_nextrecvpsn |= cpu_to_be32(attr->min_rnr_timer << 24);
2427 		optpar |= MLX4_QP_OPTPAR_RNR_TIMEOUT;
2428 	}
2429 	if (attr_mask & IB_QP_RQ_PSN)
2430 		context->rnr_nextrecvpsn |= cpu_to_be32(attr->rq_psn);
2431 
2432 	/* proxy and tunnel qp qkeys will be changed in modify-qp wrappers */
2433 	if (attr_mask & IB_QP_QKEY) {
2434 		if (qp->mlx4_ib_qp_type &
2435 		    (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))
2436 			context->qkey = cpu_to_be32(IB_QP_SET_QKEY);
2437 		else {
2438 			if (mlx4_is_mfunc(dev->dev) &&
2439 			    !(qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV) &&
2440 			    (attr->qkey & MLX4_RESERVED_QKEY_MASK) ==
2441 			    MLX4_RESERVED_QKEY_BASE) {
2442 				pr_err("Cannot use reserved QKEY"
2443 				       " 0x%x (range 0xffff0000..0xffffffff"
2444 				       " is reserved)\n", attr->qkey);
2445 				err = -EINVAL;
2446 				goto out;
2447 			}
2448 			context->qkey = cpu_to_be32(attr->qkey);
2449 		}
2450 		optpar |= MLX4_QP_OPTPAR_Q_KEY;
2451 	}
2452 
2453 	if (ibsrq)
2454 		context->srqn = cpu_to_be32(1 << 24 |
2455 					    to_msrq(ibsrq)->msrq.srqn);
2456 
2457 	if (qp->rq.wqe_cnt &&
2458 	    cur_state == IB_QPS_RESET &&
2459 	    new_state == IB_QPS_INIT)
2460 		context->db_rec_addr = cpu_to_be64(qp->db.dma);
2461 
2462 	if (cur_state == IB_QPS_INIT &&
2463 	    new_state == IB_QPS_RTR  &&
2464 	    (qp_type == IB_QPT_GSI || qp_type == IB_QPT_SMI ||
2465 	     qp_type == IB_QPT_UD || qp_type == IB_QPT_RAW_PACKET)) {
2466 		context->pri_path.sched_queue = (qp->port - 1) << 6;
2467 		if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
2468 		    qp->mlx4_ib_qp_type &
2469 		    (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER)) {
2470 			context->pri_path.sched_queue |= MLX4_IB_DEFAULT_QP0_SCHED_QUEUE;
2471 			if (qp->mlx4_ib_qp_type != MLX4_IB_QPT_SMI)
2472 				context->pri_path.fl = 0x80;
2473 		} else {
2474 			if (qp->mlx4_ib_qp_type & MLX4_IB_QPT_ANY_SRIOV)
2475 				context->pri_path.fl = 0x80;
2476 			context->pri_path.sched_queue |= MLX4_IB_DEFAULT_SCHED_QUEUE;
2477 		}
2478 		if (rdma_port_get_link_layer(&dev->ib_dev, qp->port) ==
2479 		    IB_LINK_LAYER_ETHERNET) {
2480 			if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI ||
2481 			    qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI)
2482 				context->pri_path.feup = 1 << 7; /* don't fsm */
2483 			/* handle smac_index */
2484 			if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_UD ||
2485 			    qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI ||
2486 			    qp->mlx4_ib_qp_type == MLX4_IB_QPT_TUN_GSI) {
2487 				err = handle_eth_ud_smac_index(dev, qp, context);
2488 				if (err) {
2489 					err = -EINVAL;
2490 					goto out;
2491 				}
2492 				if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_GSI)
2493 					dev->qp1_proxy[qp->port - 1] = qp;
2494 			}
2495 		}
2496 	}
2497 
2498 	if (qp_type == IB_QPT_RAW_PACKET) {
2499 		context->pri_path.ackto = (context->pri_path.ackto & 0xf8) |
2500 					MLX4_IB_LINK_TYPE_ETH;
2501 		if (dev->dev->caps.tunnel_offload_mode ==  MLX4_TUNNEL_OFFLOAD_MODE_VXLAN) {
2502 			/* set QP to receive both tunneled & non-tunneled packets */
2503 			if (!rwq_ind_tbl)
2504 				context->srqn = cpu_to_be32(7 << 28);
2505 		}
2506 	}
2507 
2508 	if (qp_type == IB_QPT_UD && (new_state == IB_QPS_RTR)) {
2509 		int is_eth = rdma_port_get_link_layer(
2510 				&dev->ib_dev, qp->port) ==
2511 				IB_LINK_LAYER_ETHERNET;
2512 		if (is_eth) {
2513 			context->pri_path.ackto = MLX4_IB_LINK_TYPE_ETH;
2514 			optpar |= MLX4_QP_OPTPAR_PRIMARY_ADDR_PATH;
2515 		}
2516 	}
2517 
2518 	if (cur_state == IB_QPS_RTS && new_state == IB_QPS_SQD	&&
2519 	    attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY && attr->en_sqd_async_notify)
2520 		sqd_event = 1;
2521 	else
2522 		sqd_event = 0;
2523 
2524 	if (!ucontext &&
2525 	    cur_state == IB_QPS_RESET &&
2526 	    new_state == IB_QPS_INIT)
2527 		context->rlkey_roce_mode |= (1 << 4);
2528 
2529 	/*
2530 	 * Before passing a kernel QP to the HW, make sure that the
2531 	 * ownership bits of the send queue are set and the SQ
2532 	 * headroom is stamped so that the hardware doesn't start
2533 	 * processing stale work requests.
2534 	 */
2535 	if (!ucontext &&
2536 	    cur_state == IB_QPS_RESET &&
2537 	    new_state == IB_QPS_INIT) {
2538 		struct mlx4_wqe_ctrl_seg *ctrl;
2539 		int i;
2540 
2541 		for (i = 0; i < qp->sq.wqe_cnt; ++i) {
2542 			ctrl = get_send_wqe(qp, i);
2543 			ctrl->owner_opcode = cpu_to_be32(1 << 31);
2544 			ctrl->qpn_vlan.fence_size =
2545 				1 << (qp->sq.wqe_shift - 4);
2546 			stamp_send_wqe(qp, i);
2547 		}
2548 	}
2549 
2550 	if (rwq_ind_tbl	&&
2551 	    cur_state == IB_QPS_RESET &&
2552 	    new_state == IB_QPS_INIT) {
2553 		fill_qp_rss_context(context, qp);
2554 		context->flags |= cpu_to_be32(1 << MLX4_RSS_QPC_FLAG_OFFSET);
2555 	}
2556 
2557 	err = mlx4_qp_modify(dev->dev, &qp->mtt, to_mlx4_state(cur_state),
2558 			     to_mlx4_state(new_state), context, optpar,
2559 			     sqd_event, &qp->mqp);
2560 	if (err)
2561 		goto out;
2562 
2563 	qp->state = new_state;
2564 
2565 	if (attr_mask & IB_QP_ACCESS_FLAGS)
2566 		qp->atomic_rd_en = attr->qp_access_flags;
2567 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
2568 		qp->resp_depth = attr->max_dest_rd_atomic;
2569 	if (attr_mask & IB_QP_PORT) {
2570 		qp->port = attr->port_num;
2571 		update_mcg_macs(dev, qp);
2572 	}
2573 	if (attr_mask & IB_QP_ALT_PATH)
2574 		qp->alt_port = attr->alt_port_num;
2575 
2576 	if (is_sqp(dev, qp))
2577 		store_sqp_attrs(to_msqp(qp), attr, attr_mask);
2578 
2579 	/*
2580 	 * If we moved QP0 to RTR, bring the IB link up; if we moved
2581 	 * QP0 to RESET or ERROR, bring the link back down.
2582 	 */
2583 	if (is_qp0(dev, qp)) {
2584 		if (cur_state != IB_QPS_RTR && new_state == IB_QPS_RTR)
2585 			if (mlx4_INIT_PORT(dev->dev, qp->port))
2586 				pr_warn("INIT_PORT failed for port %d\n",
2587 				       qp->port);
2588 
2589 		if (cur_state != IB_QPS_RESET && cur_state != IB_QPS_ERR &&
2590 		    (new_state == IB_QPS_RESET || new_state == IB_QPS_ERR))
2591 			mlx4_CLOSE_PORT(dev->dev, qp->port);
2592 	}
2593 
2594 	/*
2595 	 * If we moved a kernel QP to RESET, clean up all old CQ
2596 	 * entries and reinitialize the QP.
2597 	 */
2598 	if (new_state == IB_QPS_RESET) {
2599 		if (!ucontext) {
2600 			mlx4_ib_cq_clean(recv_cq, qp->mqp.qpn,
2601 					 ibsrq ? to_msrq(ibsrq) : NULL);
2602 			if (send_cq != recv_cq)
2603 				mlx4_ib_cq_clean(send_cq, qp->mqp.qpn, NULL);
2604 
2605 			qp->rq.head = 0;
2606 			qp->rq.tail = 0;
2607 			qp->sq.head = 0;
2608 			qp->sq.tail = 0;
2609 			qp->sq_next_wqe = 0;
2610 			if (qp->rq.wqe_cnt)
2611 				*qp->db.db  = 0;
2612 
2613 			if (qp->flags & MLX4_IB_QP_NETIF)
2614 				mlx4_ib_steer_qp_reg(dev, qp, 0);
2615 		}
2616 		if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port)) {
2617 			mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
2618 			qp->pri.smac = 0;
2619 			qp->pri.smac_port = 0;
2620 		}
2621 		if (qp->alt.smac) {
2622 			mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
2623 			qp->alt.smac = 0;
2624 		}
2625 		if (qp->pri.vid < 0x1000) {
2626 			mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port, qp->pri.vid);
2627 			qp->pri.vid = 0xFFFF;
2628 			qp->pri.candidate_vid = 0xFFFF;
2629 			qp->pri.update_vid = 0;
2630 		}
2631 
2632 		if (qp->alt.vid < 0x1000) {
2633 			mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port, qp->alt.vid);
2634 			qp->alt.vid = 0xFFFF;
2635 			qp->alt.candidate_vid = 0xFFFF;
2636 			qp->alt.update_vid = 0;
2637 		}
2638 	}
2639 out:
2640 	if (err && qp->counter_index)
2641 		mlx4_ib_free_qp_counter(dev, qp);
2642 	if (err && steer_qp)
2643 		mlx4_ib_steer_qp_reg(dev, qp, 0);
2644 	kfree(context);
2645 	if (qp->pri.candidate_smac ||
2646 	    (!qp->pri.candidate_smac && qp->pri.candidate_smac_port)) {
2647 		if (err) {
2648 			mlx4_unregister_mac(dev->dev, qp->pri.candidate_smac_port, qp->pri.candidate_smac);
2649 		} else {
2650 			if (qp->pri.smac || (!qp->pri.smac && qp->pri.smac_port))
2651 				mlx4_unregister_mac(dev->dev, qp->pri.smac_port, qp->pri.smac);
2652 			qp->pri.smac = qp->pri.candidate_smac;
2653 			qp->pri.smac_index = qp->pri.candidate_smac_index;
2654 			qp->pri.smac_port = qp->pri.candidate_smac_port;
2655 		}
2656 		qp->pri.candidate_smac = 0;
2657 		qp->pri.candidate_smac_index = 0;
2658 		qp->pri.candidate_smac_port = 0;
2659 	}
2660 	if (qp->alt.candidate_smac) {
2661 		if (err) {
2662 			mlx4_unregister_mac(dev->dev, qp->alt.candidate_smac_port, qp->alt.candidate_smac);
2663 		} else {
2664 			if (qp->alt.smac)
2665 				mlx4_unregister_mac(dev->dev, qp->alt.smac_port, qp->alt.smac);
2666 			qp->alt.smac = qp->alt.candidate_smac;
2667 			qp->alt.smac_index = qp->alt.candidate_smac_index;
2668 			qp->alt.smac_port = qp->alt.candidate_smac_port;
2669 		}
2670 		qp->alt.candidate_smac = 0;
2671 		qp->alt.candidate_smac_index = 0;
2672 		qp->alt.candidate_smac_port = 0;
2673 	}
2674 
2675 	if (qp->pri.update_vid) {
2676 		if (err) {
2677 			if (qp->pri.candidate_vid < 0x1000)
2678 				mlx4_unregister_vlan(dev->dev, qp->pri.candidate_vlan_port,
2679 						     qp->pri.candidate_vid);
2680 		} else {
2681 			if (qp->pri.vid < 0x1000)
2682 				mlx4_unregister_vlan(dev->dev, qp->pri.vlan_port,
2683 						     qp->pri.vid);
2684 			qp->pri.vid = qp->pri.candidate_vid;
2685 			qp->pri.vlan_port = qp->pri.candidate_vlan_port;
2686 			qp->pri.vlan_index =  qp->pri.candidate_vlan_index;
2687 		}
2688 		qp->pri.candidate_vid = 0xFFFF;
2689 		qp->pri.update_vid = 0;
2690 	}
2691 
2692 	if (qp->alt.update_vid) {
2693 		if (err) {
2694 			if (qp->alt.candidate_vid < 0x1000)
2695 				mlx4_unregister_vlan(dev->dev, qp->alt.candidate_vlan_port,
2696 						     qp->alt.candidate_vid);
2697 		} else {
2698 			if (qp->alt.vid < 0x1000)
2699 				mlx4_unregister_vlan(dev->dev, qp->alt.vlan_port,
2700 						     qp->alt.vid);
2701 			qp->alt.vid = qp->alt.candidate_vid;
2702 			qp->alt.vlan_port = qp->alt.candidate_vlan_port;
2703 			qp->alt.vlan_index =  qp->alt.candidate_vlan_index;
2704 		}
2705 		qp->alt.candidate_vid = 0xFFFF;
2706 		qp->alt.update_vid = 0;
2707 	}
2708 
2709 	return err;
2710 }
2711 
2712 enum {
2713 	MLX4_IB_MODIFY_QP_RSS_SUP_ATTR_MSK = (IB_QP_STATE	|
2714 					      IB_QP_PORT),
2715 };
2716 
2717 static int _mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2718 			      int attr_mask, struct ib_udata *udata)
2719 {
2720 	struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
2721 	struct mlx4_ib_qp *qp = to_mqp(ibqp);
2722 	enum ib_qp_state cur_state, new_state;
2723 	int err = -EINVAL;
2724 	mutex_lock(&qp->mutex);
2725 
2726 	cur_state = attr_mask & IB_QP_CUR_STATE ? attr->cur_qp_state : qp->state;
2727 	new_state = attr_mask & IB_QP_STATE ? attr->qp_state : cur_state;
2728 
2729 	if (!ib_modify_qp_is_ok(cur_state, new_state, ibqp->qp_type,
2730 				attr_mask)) {
2731 		pr_debug("qpn 0x%x: invalid attribute mask specified "
2732 			 "for transition %d to %d. qp_type %d,"
2733 			 " attr_mask 0x%x\n",
2734 			 ibqp->qp_num, cur_state, new_state,
2735 			 ibqp->qp_type, attr_mask);
2736 		goto out;
2737 	}
2738 
2739 	if (ibqp->rwq_ind_tbl) {
2740 		if (!(((cur_state == IB_QPS_RESET) &&
2741 		       (new_state == IB_QPS_INIT)) ||
2742 		      ((cur_state == IB_QPS_INIT)  &&
2743 		       (new_state == IB_QPS_RTR)))) {
2744 			pr_debug("qpn 0x%x: RSS QP unsupported transition %d to %d\n",
2745 				 ibqp->qp_num, cur_state, new_state);
2746 
2747 			err = -EOPNOTSUPP;
2748 			goto out;
2749 		}
2750 
2751 		if (attr_mask & ~MLX4_IB_MODIFY_QP_RSS_SUP_ATTR_MSK) {
2752 			pr_debug("qpn 0x%x: RSS QP unsupported attribute mask 0x%x for transition %d to %d\n",
2753 				 ibqp->qp_num, attr_mask, cur_state, new_state);
2754 
2755 			err = -EOPNOTSUPP;
2756 			goto out;
2757 		}
2758 	}
2759 
2760 	if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT)) {
2761 		if ((cur_state == IB_QPS_RESET) && (new_state == IB_QPS_INIT)) {
2762 			if ((ibqp->qp_type == IB_QPT_RC) ||
2763 			    (ibqp->qp_type == IB_QPT_UD) ||
2764 			    (ibqp->qp_type == IB_QPT_UC) ||
2765 			    (ibqp->qp_type == IB_QPT_RAW_PACKET) ||
2766 			    (ibqp->qp_type == IB_QPT_XRC_INI)) {
2767 				attr->port_num = mlx4_ib_bond_next_port(dev);
2768 			}
2769 		} else {
2770 			/* no sense in changing port_num
2771 			 * when ports are bonded */
2772 			attr_mask &= ~IB_QP_PORT;
2773 		}
2774 	}
2775 
2776 	if ((attr_mask & IB_QP_PORT) &&
2777 	    (attr->port_num == 0 || attr->port_num > dev->num_ports)) {
2778 		pr_debug("qpn 0x%x: invalid port number (%d) specified "
2779 			 "for transition %d to %d. qp_type %d\n",
2780 			 ibqp->qp_num, attr->port_num, cur_state,
2781 			 new_state, ibqp->qp_type);
2782 		goto out;
2783 	}
2784 
2785 	if ((attr_mask & IB_QP_PORT) && (ibqp->qp_type == IB_QPT_RAW_PACKET) &&
2786 	    (rdma_port_get_link_layer(&dev->ib_dev, attr->port_num) !=
2787 	     IB_LINK_LAYER_ETHERNET))
2788 		goto out;
2789 
2790 	if (attr_mask & IB_QP_PKEY_INDEX) {
2791 		int p = attr_mask & IB_QP_PORT ? attr->port_num : qp->port;
2792 		if (attr->pkey_index >= dev->dev->caps.pkey_table_len[p]) {
2793 			pr_debug("qpn 0x%x: invalid pkey index (%d) specified "
2794 				 "for transition %d to %d. qp_type %d\n",
2795 				 ibqp->qp_num, attr->pkey_index, cur_state,
2796 				 new_state, ibqp->qp_type);
2797 			goto out;
2798 		}
2799 	}
2800 
2801 	if (attr_mask & IB_QP_MAX_QP_RD_ATOMIC &&
2802 	    attr->max_rd_atomic > dev->dev->caps.max_qp_init_rdma) {
2803 		pr_debug("qpn 0x%x: max_rd_atomic (%d) too large. "
2804 			 "Transition %d to %d. qp_type %d\n",
2805 			 ibqp->qp_num, attr->max_rd_atomic, cur_state,
2806 			 new_state, ibqp->qp_type);
2807 		goto out;
2808 	}
2809 
2810 	if (attr_mask & IB_QP_MAX_DEST_RD_ATOMIC &&
2811 	    attr->max_dest_rd_atomic > dev->dev->caps.max_qp_dest_rdma) {
2812 		pr_debug("qpn 0x%x: max_dest_rd_atomic (%d) too large. "
2813 			 "Transition %d to %d. qp_type %d\n",
2814 			 ibqp->qp_num, attr->max_dest_rd_atomic, cur_state,
2815 			 new_state, ibqp->qp_type);
2816 		goto out;
2817 	}
2818 
2819 	if (cur_state == new_state && cur_state == IB_QPS_RESET) {
2820 		err = 0;
2821 		goto out;
2822 	}
2823 
2824 	if (ibqp->rwq_ind_tbl && (new_state == IB_QPS_INIT)) {
2825 		err = bringup_rss_rwqs(ibqp->rwq_ind_tbl, attr->port_num,
2826 				       udata);
2827 		if (err)
2828 			goto out;
2829 	}
2830 
2831 	err = __mlx4_ib_modify_qp(ibqp, MLX4_IB_QP_SRC, attr, attr_mask,
2832 				  cur_state, new_state, udata);
2833 
2834 	if (ibqp->rwq_ind_tbl && err)
2835 		bring_down_rss_rwqs(ibqp->rwq_ind_tbl, udata);
2836 
2837 	if (mlx4_is_bonded(dev->dev) && (attr_mask & IB_QP_PORT))
2838 		attr->port_num = 1;
2839 
2840 out:
2841 	mutex_unlock(&qp->mutex);
2842 	return err;
2843 }
2844 
2845 int mlx4_ib_modify_qp(struct ib_qp *ibqp, struct ib_qp_attr *attr,
2846 		      int attr_mask, struct ib_udata *udata)
2847 {
2848 	struct mlx4_ib_qp *mqp = to_mqp(ibqp);
2849 	int ret;
2850 
2851 	ret = _mlx4_ib_modify_qp(ibqp, attr, attr_mask, udata);
2852 
2853 	if (mqp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
2854 		struct mlx4_ib_sqp *sqp = to_msqp(mqp);
2855 		int err = 0;
2856 
2857 		if (sqp->roce_v2_gsi)
2858 			err = ib_modify_qp(sqp->roce_v2_gsi, attr, attr_mask);
2859 		if (err)
2860 			pr_err("Failed to modify GSI QP for RoCEv2 (%d)\n",
2861 			       err);
2862 	}
2863 	return ret;
2864 }
2865 
2866 static int vf_get_qp0_qkey(struct mlx4_dev *dev, int qpn, u32 *qkey)
2867 {
2868 	int i;
2869 	for (i = 0; i < dev->caps.num_ports; i++) {
2870 		if (qpn == dev->caps.spec_qps[i].qp0_proxy ||
2871 		    qpn == dev->caps.spec_qps[i].qp0_tunnel) {
2872 			*qkey = dev->caps.spec_qps[i].qp0_qkey;
2873 			return 0;
2874 		}
2875 	}
2876 	return -EINVAL;
2877 }
2878 
2879 static int build_sriov_qp0_header(struct mlx4_ib_sqp *sqp,
2880 				  const struct ib_ud_wr *wr,
2881 				  void *wqe, unsigned *mlx_seg_len)
2882 {
2883 	struct mlx4_ib_dev *mdev = to_mdev(sqp->qp.ibqp.device);
2884 	struct ib_device *ib_dev = &mdev->ib_dev;
2885 	struct mlx4_wqe_mlx_seg *mlx = wqe;
2886 	struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
2887 	struct mlx4_ib_ah *ah = to_mah(wr->ah);
2888 	u16 pkey;
2889 	u32 qkey;
2890 	int send_size;
2891 	int header_size;
2892 	int spc;
2893 	int i;
2894 
2895 	if (wr->wr.opcode != IB_WR_SEND)
2896 		return -EINVAL;
2897 
2898 	send_size = 0;
2899 
2900 	for (i = 0; i < wr->wr.num_sge; ++i)
2901 		send_size += wr->wr.sg_list[i].length;
2902 
2903 	/* for proxy-qp0 sends, need to add in size of tunnel header */
2904 	/* for tunnel-qp0 sends, tunnel header is already in s/g list */
2905 	if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER)
2906 		send_size += sizeof (struct mlx4_ib_tunnel_header);
2907 
2908 	ib_ud_header_init(send_size, 1, 0, 0, 0, 0, 0, 0, &sqp->ud_header);
2909 
2910 	if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_PROXY_SMI_OWNER) {
2911 		sqp->ud_header.lrh.service_level =
2912 			be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
2913 		sqp->ud_header.lrh.destination_lid =
2914 			cpu_to_be16(ah->av.ib.g_slid & 0x7f);
2915 		sqp->ud_header.lrh.source_lid =
2916 			cpu_to_be16(ah->av.ib.g_slid & 0x7f);
2917 	}
2918 
2919 	mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
2920 
2921 	/* force loopback */
2922 	mlx->flags |= cpu_to_be32(MLX4_WQE_MLX_VL15 | 0x1 | MLX4_WQE_MLX_SLR);
2923 	mlx->rlid = sqp->ud_header.lrh.destination_lid;
2924 
2925 	sqp->ud_header.lrh.virtual_lane    = 0;
2926 	sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
2927 	ib_get_cached_pkey(ib_dev, sqp->qp.port, 0, &pkey);
2928 	sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
2929 	if (sqp->qp.mlx4_ib_qp_type == MLX4_IB_QPT_TUN_SMI_OWNER)
2930 		sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
2931 	else
2932 		sqp->ud_header.bth.destination_qpn =
2933 			cpu_to_be32(mdev->dev->caps.spec_qps[sqp->qp.port - 1].qp0_tunnel);
2934 
2935 	sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
2936 	if (mlx4_is_master(mdev->dev)) {
2937 		if (mlx4_get_parav_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
2938 			return -EINVAL;
2939 	} else {
2940 		if (vf_get_qp0_qkey(mdev->dev, sqp->qp.mqp.qpn, &qkey))
2941 			return -EINVAL;
2942 	}
2943 	sqp->ud_header.deth.qkey = cpu_to_be32(qkey);
2944 	sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.mqp.qpn);
2945 
2946 	sqp->ud_header.bth.opcode        = IB_OPCODE_UD_SEND_ONLY;
2947 	sqp->ud_header.immediate_present = 0;
2948 
2949 	header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
2950 
2951 	/*
2952 	 * Inline data segments may not cross a 64 byte boundary.  If
2953 	 * our UD header is bigger than the space available up to the
2954 	 * next 64 byte boundary in the WQE, use two inline data
2955 	 * segments to hold the UD header.
2956 	 */
2957 	spc = MLX4_INLINE_ALIGN -
2958 	      ((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
2959 	if (header_size <= spc) {
2960 		inl->byte_count = cpu_to_be32(1 << 31 | header_size);
2961 		memcpy(inl + 1, sqp->header_buf, header_size);
2962 		i = 1;
2963 	} else {
2964 		inl->byte_count = cpu_to_be32(1 << 31 | spc);
2965 		memcpy(inl + 1, sqp->header_buf, spc);
2966 
2967 		inl = (void *) (inl + 1) + spc;
2968 		memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
2969 		/*
2970 		 * Need a barrier here to make sure all the data is
2971 		 * visible before the byte_count field is set.
2972 		 * Otherwise the HCA prefetcher could grab the 64-byte
2973 		 * chunk with this inline segment and get a valid (!=
2974 		 * 0xffffffff) byte count but stale data, and end up
2975 		 * generating a packet with bad headers.
2976 		 *
2977 		 * The first inline segment's byte_count field doesn't
2978 		 * need a barrier, because it comes after a
2979 		 * control/MLX segment and therefore is at an offset
2980 		 * of 16 mod 64.
2981 		 */
2982 		wmb();
2983 		inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
2984 		i = 2;
2985 	}
2986 
2987 	*mlx_seg_len =
2988 	ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
2989 	return 0;
2990 }
2991 
2992 static u8 sl_to_vl(struct mlx4_ib_dev *dev, u8 sl, int port_num)
2993 {
2994 	union sl2vl_tbl_to_u64 tmp_vltab;
2995 	u8 vl;
2996 
2997 	if (sl > 15)
2998 		return 0xf;
2999 	tmp_vltab.sl64 = atomic64_read(&dev->sl2vl[port_num - 1]);
3000 	vl = tmp_vltab.sl8[sl >> 1];
3001 	if (sl & 1)
3002 		vl &= 0x0f;
3003 	else
3004 		vl >>= 4;
3005 	return vl;
3006 }
3007 
3008 static int fill_gid_by_hw_index(struct mlx4_ib_dev *ibdev, u8 port_num,
3009 				int index, union ib_gid *gid,
3010 				enum ib_gid_type *gid_type)
3011 {
3012 	struct mlx4_ib_iboe *iboe = &ibdev->iboe;
3013 	struct mlx4_port_gid_table *port_gid_table;
3014 	unsigned long flags;
3015 
3016 	port_gid_table = &iboe->gids[port_num - 1];
3017 	spin_lock_irqsave(&iboe->lock, flags);
3018 	memcpy(gid, &port_gid_table->gids[index].gid, sizeof(*gid));
3019 	*gid_type = port_gid_table->gids[index].gid_type;
3020 	spin_unlock_irqrestore(&iboe->lock, flags);
3021 	if (rdma_is_zero_gid(gid))
3022 		return -ENOENT;
3023 
3024 	return 0;
3025 }
3026 
3027 #define MLX4_ROCEV2_QP1_SPORT 0xC000
3028 static int build_mlx_header(struct mlx4_ib_sqp *sqp, const struct ib_ud_wr *wr,
3029 			    void *wqe, unsigned *mlx_seg_len)
3030 {
3031 	struct ib_device *ib_dev = sqp->qp.ibqp.device;
3032 	struct mlx4_ib_dev *ibdev = to_mdev(ib_dev);
3033 	struct mlx4_wqe_mlx_seg *mlx = wqe;
3034 	struct mlx4_wqe_ctrl_seg *ctrl = wqe;
3035 	struct mlx4_wqe_inline_seg *inl = wqe + sizeof *mlx;
3036 	struct mlx4_ib_ah *ah = to_mah(wr->ah);
3037 	union ib_gid sgid;
3038 	u16 pkey;
3039 	int send_size;
3040 	int header_size;
3041 	int spc;
3042 	int i;
3043 	int err = 0;
3044 	u16 vlan = 0xffff;
3045 	bool is_eth;
3046 	bool is_vlan = false;
3047 	bool is_grh;
3048 	bool is_udp = false;
3049 	int ip_version = 0;
3050 
3051 	send_size = 0;
3052 	for (i = 0; i < wr->wr.num_sge; ++i)
3053 		send_size += wr->wr.sg_list[i].length;
3054 
3055 	is_eth = rdma_port_get_link_layer(sqp->qp.ibqp.device, sqp->qp.port) == IB_LINK_LAYER_ETHERNET;
3056 	is_grh = mlx4_ib_ah_grh_present(ah);
3057 	if (is_eth) {
3058 		enum ib_gid_type gid_type;
3059 		if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
3060 			/* When multi-function is enabled, the ib_core gid
3061 			 * indexes don't necessarily match the hw ones, so
3062 			 * we must use our own cache */
3063 			err = mlx4_get_roce_gid_from_slave(to_mdev(ib_dev)->dev,
3064 							   be32_to_cpu(ah->av.ib.port_pd) >> 24,
3065 							   ah->av.ib.gid_index, &sgid.raw[0]);
3066 			if (err)
3067 				return err;
3068 		} else  {
3069 			err = fill_gid_by_hw_index(ibdev, sqp->qp.port,
3070 					    ah->av.ib.gid_index,
3071 					    &sgid, &gid_type);
3072 			if (!err) {
3073 				is_udp = gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP;
3074 				if (is_udp) {
3075 					if (ipv6_addr_v4mapped((struct in6_addr *)&sgid))
3076 						ip_version = 4;
3077 					else
3078 						ip_version = 6;
3079 					is_grh = false;
3080 				}
3081 			} else {
3082 				return err;
3083 			}
3084 		}
3085 		if (ah->av.eth.vlan != cpu_to_be16(0xffff)) {
3086 			vlan = be16_to_cpu(ah->av.eth.vlan) & 0x0fff;
3087 			is_vlan = 1;
3088 		}
3089 	}
3090 	err = ib_ud_header_init(send_size, !is_eth, is_eth, is_vlan, is_grh,
3091 			  ip_version, is_udp, 0, &sqp->ud_header);
3092 	if (err)
3093 		return err;
3094 
3095 	if (!is_eth) {
3096 		sqp->ud_header.lrh.service_level =
3097 			be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 28;
3098 		sqp->ud_header.lrh.destination_lid = ah->av.ib.dlid;
3099 		sqp->ud_header.lrh.source_lid = cpu_to_be16(ah->av.ib.g_slid & 0x7f);
3100 	}
3101 
3102 	if (is_grh || (ip_version == 6)) {
3103 		sqp->ud_header.grh.traffic_class =
3104 			(be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
3105 		sqp->ud_header.grh.flow_label    =
3106 			ah->av.ib.sl_tclass_flowlabel & cpu_to_be32(0xfffff);
3107 		sqp->ud_header.grh.hop_limit     = ah->av.ib.hop_limit;
3108 		if (is_eth) {
3109 			memcpy(sqp->ud_header.grh.source_gid.raw, sgid.raw, 16);
3110 		} else {
3111 			if (mlx4_is_mfunc(to_mdev(ib_dev)->dev)) {
3112 				/* When multi-function is enabled, the ib_core gid
3113 				 * indexes don't necessarily match the hw ones, so
3114 				 * we must use our own cache
3115 				 */
3116 				sqp->ud_header.grh.source_gid.global.subnet_prefix =
3117 					cpu_to_be64(atomic64_read(&(to_mdev(ib_dev)->sriov.
3118 								    demux[sqp->qp.port - 1].
3119 								    subnet_prefix)));
3120 				sqp->ud_header.grh.source_gid.global.interface_id =
3121 					to_mdev(ib_dev)->sriov.demux[sqp->qp.port - 1].
3122 						       guid_cache[ah->av.ib.gid_index];
3123 			} else {
3124 				sqp->ud_header.grh.source_gid =
3125 					ah->ibah.sgid_attr->gid;
3126 			}
3127 		}
3128 		memcpy(sqp->ud_header.grh.destination_gid.raw,
3129 		       ah->av.ib.dgid, 16);
3130 	}
3131 
3132 	if (ip_version == 4) {
3133 		sqp->ud_header.ip4.tos =
3134 			(be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 20) & 0xff;
3135 		sqp->ud_header.ip4.id = 0;
3136 		sqp->ud_header.ip4.frag_off = htons(IP_DF);
3137 		sqp->ud_header.ip4.ttl = ah->av.eth.hop_limit;
3138 
3139 		memcpy(&sqp->ud_header.ip4.saddr,
3140 		       sgid.raw + 12, 4);
3141 		memcpy(&sqp->ud_header.ip4.daddr, ah->av.ib.dgid + 12, 4);
3142 		sqp->ud_header.ip4.check = ib_ud_ip4_csum(&sqp->ud_header);
3143 	}
3144 
3145 	if (is_udp) {
3146 		sqp->ud_header.udp.dport = htons(ROCE_V2_UDP_DPORT);
3147 		sqp->ud_header.udp.sport = htons(MLX4_ROCEV2_QP1_SPORT);
3148 		sqp->ud_header.udp.csum = 0;
3149 	}
3150 
3151 	mlx->flags &= cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE);
3152 
3153 	if (!is_eth) {
3154 		mlx->flags |= cpu_to_be32((!sqp->qp.ibqp.qp_num ? MLX4_WQE_MLX_VL15 : 0) |
3155 					  (sqp->ud_header.lrh.destination_lid ==
3156 					   IB_LID_PERMISSIVE ? MLX4_WQE_MLX_SLR : 0) |
3157 					  (sqp->ud_header.lrh.service_level << 8));
3158 		if (ah->av.ib.port_pd & cpu_to_be32(0x80000000))
3159 			mlx->flags |= cpu_to_be32(0x1); /* force loopback */
3160 		mlx->rlid = sqp->ud_header.lrh.destination_lid;
3161 	}
3162 
3163 	switch (wr->wr.opcode) {
3164 	case IB_WR_SEND:
3165 		sqp->ud_header.bth.opcode	 = IB_OPCODE_UD_SEND_ONLY;
3166 		sqp->ud_header.immediate_present = 0;
3167 		break;
3168 	case IB_WR_SEND_WITH_IMM:
3169 		sqp->ud_header.bth.opcode	 = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
3170 		sqp->ud_header.immediate_present = 1;
3171 		sqp->ud_header.immediate_data    = wr->wr.ex.imm_data;
3172 		break;
3173 	default:
3174 		return -EINVAL;
3175 	}
3176 
3177 	if (is_eth) {
3178 		struct in6_addr in6;
3179 		u16 ether_type;
3180 		u16 pcp = (be32_to_cpu(ah->av.ib.sl_tclass_flowlabel) >> 29) << 13;
3181 
3182 		ether_type = (!is_udp) ? ETH_P_IBOE:
3183 			(ip_version == 4 ? ETH_P_IP : ETH_P_IPV6);
3184 
3185 		mlx->sched_prio = cpu_to_be16(pcp);
3186 
3187 		ether_addr_copy(sqp->ud_header.eth.smac_h, ah->av.eth.s_mac);
3188 		memcpy(sqp->ud_header.eth.dmac_h, ah->av.eth.mac, 6);
3189 		memcpy(&ctrl->srcrb_flags16[0], ah->av.eth.mac, 2);
3190 		memcpy(&ctrl->imm, ah->av.eth.mac + 2, 4);
3191 		memcpy(&in6, sgid.raw, sizeof(in6));
3192 
3193 
3194 		if (!memcmp(sqp->ud_header.eth.smac_h, sqp->ud_header.eth.dmac_h, 6))
3195 			mlx->flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
3196 		if (!is_vlan) {
3197 			sqp->ud_header.eth.type = cpu_to_be16(ether_type);
3198 		} else {
3199 			sqp->ud_header.vlan.type = cpu_to_be16(ether_type);
3200 			sqp->ud_header.vlan.tag = cpu_to_be16(vlan | pcp);
3201 		}
3202 	} else {
3203 		sqp->ud_header.lrh.virtual_lane    = !sqp->qp.ibqp.qp_num ? 15 :
3204 							sl_to_vl(to_mdev(ib_dev),
3205 								 sqp->ud_header.lrh.service_level,
3206 								 sqp->qp.port);
3207 		if (sqp->qp.ibqp.qp_num && sqp->ud_header.lrh.virtual_lane == 15)
3208 			return -EINVAL;
3209 		if (sqp->ud_header.lrh.destination_lid == IB_LID_PERMISSIVE)
3210 			sqp->ud_header.lrh.source_lid = IB_LID_PERMISSIVE;
3211 	}
3212 	sqp->ud_header.bth.solicited_event = !!(wr->wr.send_flags & IB_SEND_SOLICITED);
3213 	if (!sqp->qp.ibqp.qp_num)
3214 		ib_get_cached_pkey(ib_dev, sqp->qp.port, sqp->pkey_index, &pkey);
3215 	else
3216 		ib_get_cached_pkey(ib_dev, sqp->qp.port, wr->pkey_index, &pkey);
3217 	sqp->ud_header.bth.pkey = cpu_to_be16(pkey);
3218 	sqp->ud_header.bth.destination_qpn = cpu_to_be32(wr->remote_qpn);
3219 	sqp->ud_header.bth.psn = cpu_to_be32((sqp->send_psn++) & ((1 << 24) - 1));
3220 	sqp->ud_header.deth.qkey = cpu_to_be32(wr->remote_qkey & 0x80000000 ?
3221 					       sqp->qkey : wr->remote_qkey);
3222 	sqp->ud_header.deth.source_qpn = cpu_to_be32(sqp->qp.ibqp.qp_num);
3223 
3224 	header_size = ib_ud_header_pack(&sqp->ud_header, sqp->header_buf);
3225 
3226 	if (0) {
3227 		pr_err("built UD header of size %d:\n", header_size);
3228 		for (i = 0; i < header_size / 4; ++i) {
3229 			if (i % 8 == 0)
3230 				pr_err("  [%02x] ", i * 4);
3231 			pr_cont(" %08x",
3232 				be32_to_cpu(((__be32 *) sqp->header_buf)[i]));
3233 			if ((i + 1) % 8 == 0)
3234 				pr_cont("\n");
3235 		}
3236 		pr_err("\n");
3237 	}
3238 
3239 	/*
3240 	 * Inline data segments may not cross a 64 byte boundary.  If
3241 	 * our UD header is bigger than the space available up to the
3242 	 * next 64 byte boundary in the WQE, use two inline data
3243 	 * segments to hold the UD header.
3244 	 */
3245 	spc = MLX4_INLINE_ALIGN -
3246 		((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
3247 	if (header_size <= spc) {
3248 		inl->byte_count = cpu_to_be32(1 << 31 | header_size);
3249 		memcpy(inl + 1, sqp->header_buf, header_size);
3250 		i = 1;
3251 	} else {
3252 		inl->byte_count = cpu_to_be32(1 << 31 | spc);
3253 		memcpy(inl + 1, sqp->header_buf, spc);
3254 
3255 		inl = (void *) (inl + 1) + spc;
3256 		memcpy(inl + 1, sqp->header_buf + spc, header_size - spc);
3257 		/*
3258 		 * Need a barrier here to make sure all the data is
3259 		 * visible before the byte_count field is set.
3260 		 * Otherwise the HCA prefetcher could grab the 64-byte
3261 		 * chunk with this inline segment and get a valid (!=
3262 		 * 0xffffffff) byte count but stale data, and end up
3263 		 * generating a packet with bad headers.
3264 		 *
3265 		 * The first inline segment's byte_count field doesn't
3266 		 * need a barrier, because it comes after a
3267 		 * control/MLX segment and therefore is at an offset
3268 		 * of 16 mod 64.
3269 		 */
3270 		wmb();
3271 		inl->byte_count = cpu_to_be32(1 << 31 | (header_size - spc));
3272 		i = 2;
3273 	}
3274 
3275 	*mlx_seg_len =
3276 		ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + header_size, 16);
3277 	return 0;
3278 }
3279 
3280 static int mlx4_wq_overflow(struct mlx4_ib_wq *wq, int nreq, struct ib_cq *ib_cq)
3281 {
3282 	unsigned cur;
3283 	struct mlx4_ib_cq *cq;
3284 
3285 	cur = wq->head - wq->tail;
3286 	if (likely(cur + nreq < wq->max_post))
3287 		return 0;
3288 
3289 	cq = to_mcq(ib_cq);
3290 	spin_lock(&cq->lock);
3291 	cur = wq->head - wq->tail;
3292 	spin_unlock(&cq->lock);
3293 
3294 	return cur + nreq >= wq->max_post;
3295 }
3296 
3297 static __be32 convert_access(int acc)
3298 {
3299 	return (acc & IB_ACCESS_REMOTE_ATOMIC ?
3300 		cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_ATOMIC)       : 0) |
3301 	       (acc & IB_ACCESS_REMOTE_WRITE  ?
3302 		cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_WRITE) : 0) |
3303 	       (acc & IB_ACCESS_REMOTE_READ   ?
3304 		cpu_to_be32(MLX4_WQE_FMR_AND_BIND_PERM_REMOTE_READ)  : 0) |
3305 	       (acc & IB_ACCESS_LOCAL_WRITE   ? cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_WRITE)  : 0) |
3306 		cpu_to_be32(MLX4_WQE_FMR_PERM_LOCAL_READ);
3307 }
3308 
3309 static void set_reg_seg(struct mlx4_wqe_fmr_seg *fseg,
3310 			const struct ib_reg_wr *wr)
3311 {
3312 	struct mlx4_ib_mr *mr = to_mmr(wr->mr);
3313 
3314 	fseg->flags		= convert_access(wr->access);
3315 	fseg->mem_key		= cpu_to_be32(wr->key);
3316 	fseg->buf_list		= cpu_to_be64(mr->page_map);
3317 	fseg->start_addr	= cpu_to_be64(mr->ibmr.iova);
3318 	fseg->reg_len		= cpu_to_be64(mr->ibmr.length);
3319 	fseg->offset		= 0; /* XXX -- is this just for ZBVA? */
3320 	fseg->page_size		= cpu_to_be32(ilog2(mr->ibmr.page_size));
3321 	fseg->reserved[0]	= 0;
3322 	fseg->reserved[1]	= 0;
3323 }
3324 
3325 static void set_local_inv_seg(struct mlx4_wqe_local_inval_seg *iseg, u32 rkey)
3326 {
3327 	memset(iseg, 0, sizeof(*iseg));
3328 	iseg->mem_key = cpu_to_be32(rkey);
3329 }
3330 
3331 static __always_inline void set_raddr_seg(struct mlx4_wqe_raddr_seg *rseg,
3332 					  u64 remote_addr, u32 rkey)
3333 {
3334 	rseg->raddr    = cpu_to_be64(remote_addr);
3335 	rseg->rkey     = cpu_to_be32(rkey);
3336 	rseg->reserved = 0;
3337 }
3338 
3339 static void set_atomic_seg(struct mlx4_wqe_atomic_seg *aseg,
3340 			   const struct ib_atomic_wr *wr)
3341 {
3342 	if (wr->wr.opcode == IB_WR_ATOMIC_CMP_AND_SWP) {
3343 		aseg->swap_add = cpu_to_be64(wr->swap);
3344 		aseg->compare  = cpu_to_be64(wr->compare_add);
3345 	} else if (wr->wr.opcode == IB_WR_MASKED_ATOMIC_FETCH_AND_ADD) {
3346 		aseg->swap_add = cpu_to_be64(wr->compare_add);
3347 		aseg->compare  = cpu_to_be64(wr->compare_add_mask);
3348 	} else {
3349 		aseg->swap_add = cpu_to_be64(wr->compare_add);
3350 		aseg->compare  = 0;
3351 	}
3352 
3353 }
3354 
3355 static void set_masked_atomic_seg(struct mlx4_wqe_masked_atomic_seg *aseg,
3356 				  const struct ib_atomic_wr *wr)
3357 {
3358 	aseg->swap_add		= cpu_to_be64(wr->swap);
3359 	aseg->swap_add_mask	= cpu_to_be64(wr->swap_mask);
3360 	aseg->compare		= cpu_to_be64(wr->compare_add);
3361 	aseg->compare_mask	= cpu_to_be64(wr->compare_add_mask);
3362 }
3363 
3364 static void set_datagram_seg(struct mlx4_wqe_datagram_seg *dseg,
3365 			     const struct ib_ud_wr *wr)
3366 {
3367 	memcpy(dseg->av, &to_mah(wr->ah)->av, sizeof (struct mlx4_av));
3368 	dseg->dqpn = cpu_to_be32(wr->remote_qpn);
3369 	dseg->qkey = cpu_to_be32(wr->remote_qkey);
3370 	dseg->vlan = to_mah(wr->ah)->av.eth.vlan;
3371 	memcpy(dseg->mac, to_mah(wr->ah)->av.eth.mac, 6);
3372 }
3373 
3374 static void set_tunnel_datagram_seg(struct mlx4_ib_dev *dev,
3375 				    struct mlx4_wqe_datagram_seg *dseg,
3376 				    const struct ib_ud_wr *wr,
3377 				    enum mlx4_ib_qp_type qpt)
3378 {
3379 	union mlx4_ext_av *av = &to_mah(wr->ah)->av;
3380 	struct mlx4_av sqp_av = {0};
3381 	int port = *((u8 *) &av->ib.port_pd) & 0x3;
3382 
3383 	/* force loopback */
3384 	sqp_av.port_pd = av->ib.port_pd | cpu_to_be32(0x80000000);
3385 	sqp_av.g_slid = av->ib.g_slid & 0x7f; /* no GRH */
3386 	sqp_av.sl_tclass_flowlabel = av->ib.sl_tclass_flowlabel &
3387 			cpu_to_be32(0xf0000000);
3388 
3389 	memcpy(dseg->av, &sqp_av, sizeof (struct mlx4_av));
3390 	if (qpt == MLX4_IB_QPT_PROXY_GSI)
3391 		dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp1_tunnel);
3392 	else
3393 		dseg->dqpn = cpu_to_be32(dev->dev->caps.spec_qps[port - 1].qp0_tunnel);
3394 	/* Use QKEY from the QP context, which is set by master */
3395 	dseg->qkey = cpu_to_be32(IB_QP_SET_QKEY);
3396 }
3397 
3398 static void build_tunnel_header(const struct ib_ud_wr *wr, void *wqe,
3399 				unsigned *mlx_seg_len)
3400 {
3401 	struct mlx4_wqe_inline_seg *inl = wqe;
3402 	struct mlx4_ib_tunnel_header hdr;
3403 	struct mlx4_ib_ah *ah = to_mah(wr->ah);
3404 	int spc;
3405 	int i;
3406 
3407 	memcpy(&hdr.av, &ah->av, sizeof hdr.av);
3408 	hdr.remote_qpn = cpu_to_be32(wr->remote_qpn);
3409 	hdr.pkey_index = cpu_to_be16(wr->pkey_index);
3410 	hdr.qkey = cpu_to_be32(wr->remote_qkey);
3411 	memcpy(hdr.mac, ah->av.eth.mac, 6);
3412 	hdr.vlan = ah->av.eth.vlan;
3413 
3414 	spc = MLX4_INLINE_ALIGN -
3415 		((unsigned long) (inl + 1) & (MLX4_INLINE_ALIGN - 1));
3416 	if (sizeof (hdr) <= spc) {
3417 		memcpy(inl + 1, &hdr, sizeof (hdr));
3418 		wmb();
3419 		inl->byte_count = cpu_to_be32(1 << 31 | sizeof (hdr));
3420 		i = 1;
3421 	} else {
3422 		memcpy(inl + 1, &hdr, spc);
3423 		wmb();
3424 		inl->byte_count = cpu_to_be32(1 << 31 | spc);
3425 
3426 		inl = (void *) (inl + 1) + spc;
3427 		memcpy(inl + 1, (void *) &hdr + spc, sizeof (hdr) - spc);
3428 		wmb();
3429 		inl->byte_count = cpu_to_be32(1 << 31 | (sizeof (hdr) - spc));
3430 		i = 2;
3431 	}
3432 
3433 	*mlx_seg_len =
3434 		ALIGN(i * sizeof (struct mlx4_wqe_inline_seg) + sizeof (hdr), 16);
3435 }
3436 
3437 static void set_mlx_icrc_seg(void *dseg)
3438 {
3439 	u32 *t = dseg;
3440 	struct mlx4_wqe_inline_seg *iseg = dseg;
3441 
3442 	t[1] = 0;
3443 
3444 	/*
3445 	 * Need a barrier here before writing the byte_count field to
3446 	 * make sure that all the data is visible before the
3447 	 * byte_count field is set.  Otherwise, if the segment begins
3448 	 * a new cacheline, the HCA prefetcher could grab the 64-byte
3449 	 * chunk and get a valid (!= * 0xffffffff) byte count but
3450 	 * stale data, and end up sending the wrong data.
3451 	 */
3452 	wmb();
3453 
3454 	iseg->byte_count = cpu_to_be32((1 << 31) | 4);
3455 }
3456 
3457 static void set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
3458 {
3459 	dseg->lkey       = cpu_to_be32(sg->lkey);
3460 	dseg->addr       = cpu_to_be64(sg->addr);
3461 
3462 	/*
3463 	 * Need a barrier here before writing the byte_count field to
3464 	 * make sure that all the data is visible before the
3465 	 * byte_count field is set.  Otherwise, if the segment begins
3466 	 * a new cacheline, the HCA prefetcher could grab the 64-byte
3467 	 * chunk and get a valid (!= * 0xffffffff) byte count but
3468 	 * stale data, and end up sending the wrong data.
3469 	 */
3470 	wmb();
3471 
3472 	dseg->byte_count = cpu_to_be32(sg->length);
3473 }
3474 
3475 static void __set_data_seg(struct mlx4_wqe_data_seg *dseg, struct ib_sge *sg)
3476 {
3477 	dseg->byte_count = cpu_to_be32(sg->length);
3478 	dseg->lkey       = cpu_to_be32(sg->lkey);
3479 	dseg->addr       = cpu_to_be64(sg->addr);
3480 }
3481 
3482 static int build_lso_seg(struct mlx4_wqe_lso_seg *wqe,
3483 			 const struct ib_ud_wr *wr, struct mlx4_ib_qp *qp,
3484 			 unsigned *lso_seg_len, __be32 *lso_hdr_sz, __be32 *blh)
3485 {
3486 	unsigned halign = ALIGN(sizeof *wqe + wr->hlen, 16);
3487 
3488 	if (unlikely(halign > MLX4_IB_CACHE_LINE_SIZE))
3489 		*blh = cpu_to_be32(1 << 6);
3490 
3491 	if (unlikely(!(qp->flags & MLX4_IB_QP_LSO) &&
3492 		     wr->wr.num_sge > qp->sq.max_gs - (halign >> 4)))
3493 		return -EINVAL;
3494 
3495 	memcpy(wqe->header, wr->header, wr->hlen);
3496 
3497 	*lso_hdr_sz  = cpu_to_be32(wr->mss << 16 | wr->hlen);
3498 	*lso_seg_len = halign;
3499 	return 0;
3500 }
3501 
3502 static __be32 send_ieth(const struct ib_send_wr *wr)
3503 {
3504 	switch (wr->opcode) {
3505 	case IB_WR_SEND_WITH_IMM:
3506 	case IB_WR_RDMA_WRITE_WITH_IMM:
3507 		return wr->ex.imm_data;
3508 
3509 	case IB_WR_SEND_WITH_INV:
3510 		return cpu_to_be32(wr->ex.invalidate_rkey);
3511 
3512 	default:
3513 		return 0;
3514 	}
3515 }
3516 
3517 static void add_zero_len_inline(void *wqe)
3518 {
3519 	struct mlx4_wqe_inline_seg *inl = wqe;
3520 	memset(wqe, 0, 16);
3521 	inl->byte_count = cpu_to_be32(1 << 31);
3522 }
3523 
3524 static int _mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
3525 			      const struct ib_send_wr **bad_wr, bool drain)
3526 {
3527 	struct mlx4_ib_qp *qp = to_mqp(ibqp);
3528 	void *wqe;
3529 	struct mlx4_wqe_ctrl_seg *ctrl;
3530 	struct mlx4_wqe_data_seg *dseg;
3531 	unsigned long flags;
3532 	int nreq;
3533 	int err = 0;
3534 	unsigned ind;
3535 	int uninitialized_var(size);
3536 	unsigned uninitialized_var(seglen);
3537 	__be32 dummy;
3538 	__be32 *lso_wqe;
3539 	__be32 uninitialized_var(lso_hdr_sz);
3540 	__be32 blh;
3541 	int i;
3542 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
3543 
3544 	if (qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI) {
3545 		struct mlx4_ib_sqp *sqp = to_msqp(qp);
3546 
3547 		if (sqp->roce_v2_gsi) {
3548 			struct mlx4_ib_ah *ah = to_mah(ud_wr(wr)->ah);
3549 			enum ib_gid_type gid_type;
3550 			union ib_gid gid;
3551 
3552 			if (!fill_gid_by_hw_index(mdev, sqp->qp.port,
3553 					   ah->av.ib.gid_index,
3554 					   &gid, &gid_type))
3555 				qp = (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ?
3556 						to_mqp(sqp->roce_v2_gsi) : qp;
3557 			else
3558 				pr_err("Failed to get gid at index %d. RoCEv2 will not work properly\n",
3559 				       ah->av.ib.gid_index);
3560 		}
3561 	}
3562 
3563 	spin_lock_irqsave(&qp->sq.lock, flags);
3564 	if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR &&
3565 	    !drain) {
3566 		err = -EIO;
3567 		*bad_wr = wr;
3568 		nreq = 0;
3569 		goto out;
3570 	}
3571 
3572 	ind = qp->sq_next_wqe;
3573 
3574 	for (nreq = 0; wr; ++nreq, wr = wr->next) {
3575 		lso_wqe = &dummy;
3576 		blh = 0;
3577 
3578 		if (mlx4_wq_overflow(&qp->sq, nreq, qp->ibqp.send_cq)) {
3579 			err = -ENOMEM;
3580 			*bad_wr = wr;
3581 			goto out;
3582 		}
3583 
3584 		if (unlikely(wr->num_sge > qp->sq.max_gs)) {
3585 			err = -EINVAL;
3586 			*bad_wr = wr;
3587 			goto out;
3588 		}
3589 
3590 		ctrl = wqe = get_send_wqe(qp, ind & (qp->sq.wqe_cnt - 1));
3591 		qp->sq.wrid[(qp->sq.head + nreq) & (qp->sq.wqe_cnt - 1)] = wr->wr_id;
3592 
3593 		ctrl->srcrb_flags =
3594 			(wr->send_flags & IB_SEND_SIGNALED ?
3595 			 cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) : 0) |
3596 			(wr->send_flags & IB_SEND_SOLICITED ?
3597 			 cpu_to_be32(MLX4_WQE_CTRL_SOLICITED) : 0) |
3598 			((wr->send_flags & IB_SEND_IP_CSUM) ?
3599 			 cpu_to_be32(MLX4_WQE_CTRL_IP_CSUM |
3600 				     MLX4_WQE_CTRL_TCP_UDP_CSUM) : 0) |
3601 			qp->sq_signal_bits;
3602 
3603 		ctrl->imm = send_ieth(wr);
3604 
3605 		wqe += sizeof *ctrl;
3606 		size = sizeof *ctrl / 16;
3607 
3608 		switch (qp->mlx4_ib_qp_type) {
3609 		case MLX4_IB_QPT_RC:
3610 		case MLX4_IB_QPT_UC:
3611 			switch (wr->opcode) {
3612 			case IB_WR_ATOMIC_CMP_AND_SWP:
3613 			case IB_WR_ATOMIC_FETCH_AND_ADD:
3614 			case IB_WR_MASKED_ATOMIC_FETCH_AND_ADD:
3615 				set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
3616 					      atomic_wr(wr)->rkey);
3617 				wqe  += sizeof (struct mlx4_wqe_raddr_seg);
3618 
3619 				set_atomic_seg(wqe, atomic_wr(wr));
3620 				wqe  += sizeof (struct mlx4_wqe_atomic_seg);
3621 
3622 				size += (sizeof (struct mlx4_wqe_raddr_seg) +
3623 					 sizeof (struct mlx4_wqe_atomic_seg)) / 16;
3624 
3625 				break;
3626 
3627 			case IB_WR_MASKED_ATOMIC_CMP_AND_SWP:
3628 				set_raddr_seg(wqe, atomic_wr(wr)->remote_addr,
3629 					      atomic_wr(wr)->rkey);
3630 				wqe  += sizeof (struct mlx4_wqe_raddr_seg);
3631 
3632 				set_masked_atomic_seg(wqe, atomic_wr(wr));
3633 				wqe  += sizeof (struct mlx4_wqe_masked_atomic_seg);
3634 
3635 				size += (sizeof (struct mlx4_wqe_raddr_seg) +
3636 					 sizeof (struct mlx4_wqe_masked_atomic_seg)) / 16;
3637 
3638 				break;
3639 
3640 			case IB_WR_RDMA_READ:
3641 			case IB_WR_RDMA_WRITE:
3642 			case IB_WR_RDMA_WRITE_WITH_IMM:
3643 				set_raddr_seg(wqe, rdma_wr(wr)->remote_addr,
3644 					      rdma_wr(wr)->rkey);
3645 				wqe  += sizeof (struct mlx4_wqe_raddr_seg);
3646 				size += sizeof (struct mlx4_wqe_raddr_seg) / 16;
3647 				break;
3648 
3649 			case IB_WR_LOCAL_INV:
3650 				ctrl->srcrb_flags |=
3651 					cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
3652 				set_local_inv_seg(wqe, wr->ex.invalidate_rkey);
3653 				wqe  += sizeof (struct mlx4_wqe_local_inval_seg);
3654 				size += sizeof (struct mlx4_wqe_local_inval_seg) / 16;
3655 				break;
3656 
3657 			case IB_WR_REG_MR:
3658 				ctrl->srcrb_flags |=
3659 					cpu_to_be32(MLX4_WQE_CTRL_STRONG_ORDER);
3660 				set_reg_seg(wqe, reg_wr(wr));
3661 				wqe  += sizeof(struct mlx4_wqe_fmr_seg);
3662 				size += sizeof(struct mlx4_wqe_fmr_seg) / 16;
3663 				break;
3664 
3665 			default:
3666 				/* No extra segments required for sends */
3667 				break;
3668 			}
3669 			break;
3670 
3671 		case MLX4_IB_QPT_TUN_SMI_OWNER:
3672 			err =  build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
3673 					ctrl, &seglen);
3674 			if (unlikely(err)) {
3675 				*bad_wr = wr;
3676 				goto out;
3677 			}
3678 			wqe  += seglen;
3679 			size += seglen / 16;
3680 			break;
3681 		case MLX4_IB_QPT_TUN_SMI:
3682 		case MLX4_IB_QPT_TUN_GSI:
3683 			/* this is a UD qp used in MAD responses to slaves. */
3684 			set_datagram_seg(wqe, ud_wr(wr));
3685 			/* set the forced-loopback bit in the data seg av */
3686 			*(__be32 *) wqe |= cpu_to_be32(0x80000000);
3687 			wqe  += sizeof (struct mlx4_wqe_datagram_seg);
3688 			size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
3689 			break;
3690 		case MLX4_IB_QPT_UD:
3691 			set_datagram_seg(wqe, ud_wr(wr));
3692 			wqe  += sizeof (struct mlx4_wqe_datagram_seg);
3693 			size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
3694 
3695 			if (wr->opcode == IB_WR_LSO) {
3696 				err = build_lso_seg(wqe, ud_wr(wr), qp, &seglen,
3697 						&lso_hdr_sz, &blh);
3698 				if (unlikely(err)) {
3699 					*bad_wr = wr;
3700 					goto out;
3701 				}
3702 				lso_wqe = (__be32 *) wqe;
3703 				wqe  += seglen;
3704 				size += seglen / 16;
3705 			}
3706 			break;
3707 
3708 		case MLX4_IB_QPT_PROXY_SMI_OWNER:
3709 			err = build_sriov_qp0_header(to_msqp(qp), ud_wr(wr),
3710 					ctrl, &seglen);
3711 			if (unlikely(err)) {
3712 				*bad_wr = wr;
3713 				goto out;
3714 			}
3715 			wqe  += seglen;
3716 			size += seglen / 16;
3717 			/* to start tunnel header on a cache-line boundary */
3718 			add_zero_len_inline(wqe);
3719 			wqe += 16;
3720 			size++;
3721 			build_tunnel_header(ud_wr(wr), wqe, &seglen);
3722 			wqe  += seglen;
3723 			size += seglen / 16;
3724 			break;
3725 		case MLX4_IB_QPT_PROXY_SMI:
3726 		case MLX4_IB_QPT_PROXY_GSI:
3727 			/* If we are tunneling special qps, this is a UD qp.
3728 			 * In this case we first add a UD segment targeting
3729 			 * the tunnel qp, and then add a header with address
3730 			 * information */
3731 			set_tunnel_datagram_seg(to_mdev(ibqp->device), wqe,
3732 						ud_wr(wr),
3733 						qp->mlx4_ib_qp_type);
3734 			wqe  += sizeof (struct mlx4_wqe_datagram_seg);
3735 			size += sizeof (struct mlx4_wqe_datagram_seg) / 16;
3736 			build_tunnel_header(ud_wr(wr), wqe, &seglen);
3737 			wqe  += seglen;
3738 			size += seglen / 16;
3739 			break;
3740 
3741 		case MLX4_IB_QPT_SMI:
3742 		case MLX4_IB_QPT_GSI:
3743 			err = build_mlx_header(to_msqp(qp), ud_wr(wr), ctrl,
3744 					&seglen);
3745 			if (unlikely(err)) {
3746 				*bad_wr = wr;
3747 				goto out;
3748 			}
3749 			wqe  += seglen;
3750 			size += seglen / 16;
3751 			break;
3752 
3753 		default:
3754 			break;
3755 		}
3756 
3757 		/*
3758 		 * Write data segments in reverse order, so as to
3759 		 * overwrite cacheline stamp last within each
3760 		 * cacheline.  This avoids issues with WQE
3761 		 * prefetching.
3762 		 */
3763 
3764 		dseg = wqe;
3765 		dseg += wr->num_sge - 1;
3766 		size += wr->num_sge * (sizeof (struct mlx4_wqe_data_seg) / 16);
3767 
3768 		/* Add one more inline data segment for ICRC for MLX sends */
3769 		if (unlikely(qp->mlx4_ib_qp_type == MLX4_IB_QPT_SMI ||
3770 			     qp->mlx4_ib_qp_type == MLX4_IB_QPT_GSI ||
3771 			     qp->mlx4_ib_qp_type &
3772 			     (MLX4_IB_QPT_PROXY_SMI_OWNER | MLX4_IB_QPT_TUN_SMI_OWNER))) {
3773 			set_mlx_icrc_seg(dseg + 1);
3774 			size += sizeof (struct mlx4_wqe_data_seg) / 16;
3775 		}
3776 
3777 		for (i = wr->num_sge - 1; i >= 0; --i, --dseg)
3778 			set_data_seg(dseg, wr->sg_list + i);
3779 
3780 		/*
3781 		 * Possibly overwrite stamping in cacheline with LSO
3782 		 * segment only after making sure all data segments
3783 		 * are written.
3784 		 */
3785 		wmb();
3786 		*lso_wqe = lso_hdr_sz;
3787 
3788 		ctrl->qpn_vlan.fence_size = (wr->send_flags & IB_SEND_FENCE ?
3789 					     MLX4_WQE_CTRL_FENCE : 0) | size;
3790 
3791 		/*
3792 		 * Make sure descriptor is fully written before
3793 		 * setting ownership bit (because HW can start
3794 		 * executing as soon as we do).
3795 		 */
3796 		wmb();
3797 
3798 		if (wr->opcode < 0 || wr->opcode >= ARRAY_SIZE(mlx4_ib_opcode)) {
3799 			*bad_wr = wr;
3800 			err = -EINVAL;
3801 			goto out;
3802 		}
3803 
3804 		ctrl->owner_opcode = mlx4_ib_opcode[wr->opcode] |
3805 			(ind & qp->sq.wqe_cnt ? cpu_to_be32(1 << 31) : 0) | blh;
3806 
3807 		/*
3808 		 * We can improve latency by not stamping the last
3809 		 * send queue WQE until after ringing the doorbell, so
3810 		 * only stamp here if there are still more WQEs to post.
3811 		 */
3812 		if (wr->next)
3813 			stamp_send_wqe(qp, ind + qp->sq_spare_wqes);
3814 		ind++;
3815 	}
3816 
3817 out:
3818 	if (likely(nreq)) {
3819 		qp->sq.head += nreq;
3820 
3821 		/*
3822 		 * Make sure that descriptors are written before
3823 		 * doorbell record.
3824 		 */
3825 		wmb();
3826 
3827 		writel_relaxed(qp->doorbell_qpn,
3828 			to_mdev(ibqp->device)->uar_map + MLX4_SEND_DOORBELL);
3829 
3830 		stamp_send_wqe(qp, ind + qp->sq_spare_wqes - 1);
3831 
3832 		qp->sq_next_wqe = ind;
3833 	}
3834 
3835 	spin_unlock_irqrestore(&qp->sq.lock, flags);
3836 
3837 	return err;
3838 }
3839 
3840 int mlx4_ib_post_send(struct ib_qp *ibqp, const struct ib_send_wr *wr,
3841 		      const struct ib_send_wr **bad_wr)
3842 {
3843 	return _mlx4_ib_post_send(ibqp, wr, bad_wr, false);
3844 }
3845 
3846 static int _mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
3847 			      const struct ib_recv_wr **bad_wr, bool drain)
3848 {
3849 	struct mlx4_ib_qp *qp = to_mqp(ibqp);
3850 	struct mlx4_wqe_data_seg *scat;
3851 	unsigned long flags;
3852 	int err = 0;
3853 	int nreq;
3854 	int ind;
3855 	int max_gs;
3856 	int i;
3857 	struct mlx4_ib_dev *mdev = to_mdev(ibqp->device);
3858 
3859 	max_gs = qp->rq.max_gs;
3860 	spin_lock_irqsave(&qp->rq.lock, flags);
3861 
3862 	if (mdev->dev->persist->state & MLX4_DEVICE_STATE_INTERNAL_ERROR &&
3863 	    !drain) {
3864 		err = -EIO;
3865 		*bad_wr = wr;
3866 		nreq = 0;
3867 		goto out;
3868 	}
3869 
3870 	ind = qp->rq.head & (qp->rq.wqe_cnt - 1);
3871 
3872 	for (nreq = 0; wr; ++nreq, wr = wr->next) {
3873 		if (mlx4_wq_overflow(&qp->rq, nreq, qp->ibqp.recv_cq)) {
3874 			err = -ENOMEM;
3875 			*bad_wr = wr;
3876 			goto out;
3877 		}
3878 
3879 		if (unlikely(wr->num_sge > qp->rq.max_gs)) {
3880 			err = -EINVAL;
3881 			*bad_wr = wr;
3882 			goto out;
3883 		}
3884 
3885 		scat = get_recv_wqe(qp, ind);
3886 
3887 		if (qp->mlx4_ib_qp_type & (MLX4_IB_QPT_PROXY_SMI_OWNER |
3888 		    MLX4_IB_QPT_PROXY_SMI | MLX4_IB_QPT_PROXY_GSI)) {
3889 			ib_dma_sync_single_for_device(ibqp->device,
3890 						      qp->sqp_proxy_rcv[ind].map,
3891 						      sizeof (struct mlx4_ib_proxy_sqp_hdr),
3892 						      DMA_FROM_DEVICE);
3893 			scat->byte_count =
3894 				cpu_to_be32(sizeof (struct mlx4_ib_proxy_sqp_hdr));
3895 			/* use dma lkey from upper layer entry */
3896 			scat->lkey = cpu_to_be32(wr->sg_list->lkey);
3897 			scat->addr = cpu_to_be64(qp->sqp_proxy_rcv[ind].map);
3898 			scat++;
3899 			max_gs--;
3900 		}
3901 
3902 		for (i = 0; i < wr->num_sge; ++i)
3903 			__set_data_seg(scat + i, wr->sg_list + i);
3904 
3905 		if (i < max_gs) {
3906 			scat[i].byte_count = 0;
3907 			scat[i].lkey       = cpu_to_be32(MLX4_INVALID_LKEY);
3908 			scat[i].addr       = 0;
3909 		}
3910 
3911 		qp->rq.wrid[ind] = wr->wr_id;
3912 
3913 		ind = (ind + 1) & (qp->rq.wqe_cnt - 1);
3914 	}
3915 
3916 out:
3917 	if (likely(nreq)) {
3918 		qp->rq.head += nreq;
3919 
3920 		/*
3921 		 * Make sure that descriptors are written before
3922 		 * doorbell record.
3923 		 */
3924 		wmb();
3925 
3926 		*qp->db.db = cpu_to_be32(qp->rq.head & 0xffff);
3927 	}
3928 
3929 	spin_unlock_irqrestore(&qp->rq.lock, flags);
3930 
3931 	return err;
3932 }
3933 
3934 int mlx4_ib_post_recv(struct ib_qp *ibqp, const struct ib_recv_wr *wr,
3935 		      const struct ib_recv_wr **bad_wr)
3936 {
3937 	return _mlx4_ib_post_recv(ibqp, wr, bad_wr, false);
3938 }
3939 
3940 static inline enum ib_qp_state to_ib_qp_state(enum mlx4_qp_state mlx4_state)
3941 {
3942 	switch (mlx4_state) {
3943 	case MLX4_QP_STATE_RST:      return IB_QPS_RESET;
3944 	case MLX4_QP_STATE_INIT:     return IB_QPS_INIT;
3945 	case MLX4_QP_STATE_RTR:      return IB_QPS_RTR;
3946 	case MLX4_QP_STATE_RTS:      return IB_QPS_RTS;
3947 	case MLX4_QP_STATE_SQ_DRAINING:
3948 	case MLX4_QP_STATE_SQD:      return IB_QPS_SQD;
3949 	case MLX4_QP_STATE_SQER:     return IB_QPS_SQE;
3950 	case MLX4_QP_STATE_ERR:      return IB_QPS_ERR;
3951 	default:		     return -1;
3952 	}
3953 }
3954 
3955 static inline enum ib_mig_state to_ib_mig_state(int mlx4_mig_state)
3956 {
3957 	switch (mlx4_mig_state) {
3958 	case MLX4_QP_PM_ARMED:		return IB_MIG_ARMED;
3959 	case MLX4_QP_PM_REARM:		return IB_MIG_REARM;
3960 	case MLX4_QP_PM_MIGRATED:	return IB_MIG_MIGRATED;
3961 	default: return -1;
3962 	}
3963 }
3964 
3965 static int to_ib_qp_access_flags(int mlx4_flags)
3966 {
3967 	int ib_flags = 0;
3968 
3969 	if (mlx4_flags & MLX4_QP_BIT_RRE)
3970 		ib_flags |= IB_ACCESS_REMOTE_READ;
3971 	if (mlx4_flags & MLX4_QP_BIT_RWE)
3972 		ib_flags |= IB_ACCESS_REMOTE_WRITE;
3973 	if (mlx4_flags & MLX4_QP_BIT_RAE)
3974 		ib_flags |= IB_ACCESS_REMOTE_ATOMIC;
3975 
3976 	return ib_flags;
3977 }
3978 
3979 static void to_rdma_ah_attr(struct mlx4_ib_dev *ibdev,
3980 			    struct rdma_ah_attr *ah_attr,
3981 			    struct mlx4_qp_path *path)
3982 {
3983 	struct mlx4_dev *dev = ibdev->dev;
3984 	u8 port_num = path->sched_queue & 0x40 ? 2 : 1;
3985 
3986 	memset(ah_attr, 0, sizeof(*ah_attr));
3987 	if (port_num == 0 || port_num > dev->caps.num_ports)
3988 		return;
3989 	ah_attr->type = rdma_ah_find_type(&ibdev->ib_dev, port_num);
3990 
3991 	if (ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE)
3992 		rdma_ah_set_sl(ah_attr, ((path->sched_queue >> 3) & 0x7) |
3993 			       ((path->sched_queue & 4) << 1));
3994 	else
3995 		rdma_ah_set_sl(ah_attr, (path->sched_queue >> 2) & 0xf);
3996 	rdma_ah_set_port_num(ah_attr, port_num);
3997 
3998 	rdma_ah_set_dlid(ah_attr, be16_to_cpu(path->rlid));
3999 	rdma_ah_set_path_bits(ah_attr, path->grh_mylmc & 0x7f);
4000 	rdma_ah_set_static_rate(ah_attr,
4001 				path->static_rate ? path->static_rate - 5 : 0);
4002 	if (path->grh_mylmc & (1 << 7)) {
4003 		rdma_ah_set_grh(ah_attr, NULL,
4004 				be32_to_cpu(path->tclass_flowlabel) & 0xfffff,
4005 				path->mgid_index,
4006 				path->hop_limit,
4007 				(be32_to_cpu(path->tclass_flowlabel)
4008 				 >> 20) & 0xff);
4009 		rdma_ah_set_dgid_raw(ah_attr, path->rgid);
4010 	}
4011 }
4012 
4013 int mlx4_ib_query_qp(struct ib_qp *ibqp, struct ib_qp_attr *qp_attr, int qp_attr_mask,
4014 		     struct ib_qp_init_attr *qp_init_attr)
4015 {
4016 	struct mlx4_ib_dev *dev = to_mdev(ibqp->device);
4017 	struct mlx4_ib_qp *qp = to_mqp(ibqp);
4018 	struct mlx4_qp_context context;
4019 	int mlx4_state;
4020 	int err = 0;
4021 
4022 	if (ibqp->rwq_ind_tbl)
4023 		return -EOPNOTSUPP;
4024 
4025 	mutex_lock(&qp->mutex);
4026 
4027 	if (qp->state == IB_QPS_RESET) {
4028 		qp_attr->qp_state = IB_QPS_RESET;
4029 		goto done;
4030 	}
4031 
4032 	err = mlx4_qp_query(dev->dev, &qp->mqp, &context);
4033 	if (err) {
4034 		err = -EINVAL;
4035 		goto out;
4036 	}
4037 
4038 	mlx4_state = be32_to_cpu(context.flags) >> 28;
4039 
4040 	qp->state		     = to_ib_qp_state(mlx4_state);
4041 	qp_attr->qp_state	     = qp->state;
4042 	qp_attr->path_mtu	     = context.mtu_msgmax >> 5;
4043 	qp_attr->path_mig_state	     =
4044 		to_ib_mig_state((be32_to_cpu(context.flags) >> 11) & 0x3);
4045 	qp_attr->qkey		     = be32_to_cpu(context.qkey);
4046 	qp_attr->rq_psn		     = be32_to_cpu(context.rnr_nextrecvpsn) & 0xffffff;
4047 	qp_attr->sq_psn		     = be32_to_cpu(context.next_send_psn) & 0xffffff;
4048 	qp_attr->dest_qp_num	     = be32_to_cpu(context.remote_qpn) & 0xffffff;
4049 	qp_attr->qp_access_flags     =
4050 		to_ib_qp_access_flags(be32_to_cpu(context.params2));
4051 
4052 	if (qp->ibqp.qp_type == IB_QPT_RC || qp->ibqp.qp_type == IB_QPT_UC) {
4053 		to_rdma_ah_attr(dev, &qp_attr->ah_attr, &context.pri_path);
4054 		to_rdma_ah_attr(dev, &qp_attr->alt_ah_attr, &context.alt_path);
4055 		qp_attr->alt_pkey_index = context.alt_path.pkey_index & 0x7f;
4056 		qp_attr->alt_port_num	=
4057 			rdma_ah_get_port_num(&qp_attr->alt_ah_attr);
4058 	}
4059 
4060 	qp_attr->pkey_index = context.pri_path.pkey_index & 0x7f;
4061 	if (qp_attr->qp_state == IB_QPS_INIT)
4062 		qp_attr->port_num = qp->port;
4063 	else
4064 		qp_attr->port_num = context.pri_path.sched_queue & 0x40 ? 2 : 1;
4065 
4066 	/* qp_attr->en_sqd_async_notify is only applicable in modify qp */
4067 	qp_attr->sq_draining = mlx4_state == MLX4_QP_STATE_SQ_DRAINING;
4068 
4069 	qp_attr->max_rd_atomic = 1 << ((be32_to_cpu(context.params1) >> 21) & 0x7);
4070 
4071 	qp_attr->max_dest_rd_atomic =
4072 		1 << ((be32_to_cpu(context.params2) >> 21) & 0x7);
4073 	qp_attr->min_rnr_timer	    =
4074 		(be32_to_cpu(context.rnr_nextrecvpsn) >> 24) & 0x1f;
4075 	qp_attr->timeout	    = context.pri_path.ackto >> 3;
4076 	qp_attr->retry_cnt	    = (be32_to_cpu(context.params1) >> 16) & 0x7;
4077 	qp_attr->rnr_retry	    = (be32_to_cpu(context.params1) >> 13) & 0x7;
4078 	qp_attr->alt_timeout	    = context.alt_path.ackto >> 3;
4079 
4080 done:
4081 	qp_attr->cur_qp_state	     = qp_attr->qp_state;
4082 	qp_attr->cap.max_recv_wr     = qp->rq.wqe_cnt;
4083 	qp_attr->cap.max_recv_sge    = qp->rq.max_gs;
4084 
4085 	if (!ibqp->uobject) {
4086 		qp_attr->cap.max_send_wr  = qp->sq.wqe_cnt;
4087 		qp_attr->cap.max_send_sge = qp->sq.max_gs;
4088 	} else {
4089 		qp_attr->cap.max_send_wr  = 0;
4090 		qp_attr->cap.max_send_sge = 0;
4091 	}
4092 
4093 	/*
4094 	 * We don't support inline sends for kernel QPs (yet), and we
4095 	 * don't know what userspace's value should be.
4096 	 */
4097 	qp_attr->cap.max_inline_data = 0;
4098 
4099 	qp_init_attr->cap	     = qp_attr->cap;
4100 
4101 	qp_init_attr->create_flags = 0;
4102 	if (qp->flags & MLX4_IB_QP_BLOCK_MULTICAST_LOOPBACK)
4103 		qp_init_attr->create_flags |= IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK;
4104 
4105 	if (qp->flags & MLX4_IB_QP_LSO)
4106 		qp_init_attr->create_flags |= IB_QP_CREATE_IPOIB_UD_LSO;
4107 
4108 	if (qp->flags & MLX4_IB_QP_NETIF)
4109 		qp_init_attr->create_flags |= IB_QP_CREATE_NETIF_QP;
4110 
4111 	qp_init_attr->sq_sig_type =
4112 		qp->sq_signal_bits == cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE) ?
4113 		IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
4114 
4115 out:
4116 	mutex_unlock(&qp->mutex);
4117 	return err;
4118 }
4119 
4120 struct ib_wq *mlx4_ib_create_wq(struct ib_pd *pd,
4121 				struct ib_wq_init_attr *init_attr,
4122 				struct ib_udata *udata)
4123 {
4124 	struct mlx4_dev *dev = to_mdev(pd->device)->dev;
4125 	struct ib_qp_init_attr ib_qp_init_attr = {};
4126 	struct mlx4_ib_qp *qp;
4127 	struct mlx4_ib_create_wq ucmd;
4128 	int err, required_cmd_sz;
4129 
4130 	if (!udata)
4131 		return ERR_PTR(-EINVAL);
4132 
4133 	required_cmd_sz = offsetof(typeof(ucmd), comp_mask) +
4134 			  sizeof(ucmd.comp_mask);
4135 	if (udata->inlen < required_cmd_sz) {
4136 		pr_debug("invalid inlen\n");
4137 		return ERR_PTR(-EINVAL);
4138 	}
4139 
4140 	if (udata->inlen > sizeof(ucmd) &&
4141 	    !ib_is_udata_cleared(udata, sizeof(ucmd),
4142 				 udata->inlen - sizeof(ucmd))) {
4143 		pr_debug("inlen is not supported\n");
4144 		return ERR_PTR(-EOPNOTSUPP);
4145 	}
4146 
4147 	if (udata->outlen)
4148 		return ERR_PTR(-EOPNOTSUPP);
4149 
4150 	if (init_attr->wq_type != IB_WQT_RQ) {
4151 		pr_debug("unsupported wq type %d\n", init_attr->wq_type);
4152 		return ERR_PTR(-EOPNOTSUPP);
4153 	}
4154 
4155 	if (init_attr->create_flags & ~IB_WQ_FLAGS_SCATTER_FCS ||
4156 	    !(dev->caps.flags & MLX4_DEV_CAP_FLAG_FCS_KEEP)) {
4157 		pr_debug("unsupported create_flags %u\n",
4158 			 init_attr->create_flags);
4159 		return ERR_PTR(-EOPNOTSUPP);
4160 	}
4161 
4162 	qp = kzalloc(sizeof(*qp), GFP_KERNEL);
4163 	if (!qp)
4164 		return ERR_PTR(-ENOMEM);
4165 
4166 	qp->pri.vid = 0xFFFF;
4167 	qp->alt.vid = 0xFFFF;
4168 
4169 	ib_qp_init_attr.qp_context = init_attr->wq_context;
4170 	ib_qp_init_attr.qp_type = IB_QPT_RAW_PACKET;
4171 	ib_qp_init_attr.cap.max_recv_wr = init_attr->max_wr;
4172 	ib_qp_init_attr.cap.max_recv_sge = init_attr->max_sge;
4173 	ib_qp_init_attr.recv_cq = init_attr->cq;
4174 	ib_qp_init_attr.send_cq = ib_qp_init_attr.recv_cq; /* Dummy CQ */
4175 
4176 	if (init_attr->create_flags & IB_WQ_FLAGS_SCATTER_FCS)
4177 		ib_qp_init_attr.create_flags |= IB_QP_CREATE_SCATTER_FCS;
4178 
4179 	err = create_rq(pd, &ib_qp_init_attr, udata, qp);
4180 	if (err) {
4181 		kfree(qp);
4182 		return ERR_PTR(err);
4183 	}
4184 
4185 	qp->ibwq.event_handler = init_attr->event_handler;
4186 	qp->ibwq.wq_num = qp->mqp.qpn;
4187 	qp->ibwq.state = IB_WQS_RESET;
4188 
4189 	return &qp->ibwq;
4190 }
4191 
4192 static int ib_wq2qp_state(enum ib_wq_state state)
4193 {
4194 	switch (state) {
4195 	case IB_WQS_RESET:
4196 		return IB_QPS_RESET;
4197 	case IB_WQS_RDY:
4198 		return IB_QPS_RTR;
4199 	default:
4200 		return IB_QPS_ERR;
4201 	}
4202 }
4203 
4204 static int _mlx4_ib_modify_wq(struct ib_wq *ibwq, enum ib_wq_state new_state,
4205 			      struct ib_udata *udata)
4206 {
4207 	struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
4208 	enum ib_qp_state qp_cur_state;
4209 	enum ib_qp_state qp_new_state;
4210 	int attr_mask;
4211 	int err;
4212 
4213 	/* ib_qp.state represents the WQ HW state while ib_wq.state represents
4214 	 * the WQ logic state.
4215 	 */
4216 	qp_cur_state = qp->state;
4217 	qp_new_state = ib_wq2qp_state(new_state);
4218 
4219 	if (ib_wq2qp_state(new_state) == qp_cur_state)
4220 		return 0;
4221 
4222 	if (new_state == IB_WQS_RDY) {
4223 		struct ib_qp_attr attr = {};
4224 
4225 		attr.port_num = qp->port;
4226 		attr_mask = IB_QP_PORT;
4227 
4228 		err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, &attr,
4229 					  attr_mask, IB_QPS_RESET, IB_QPS_INIT,
4230 					  udata);
4231 		if (err) {
4232 			pr_debug("WQN=0x%06x failed to apply RST->INIT on the HW QP\n",
4233 				 ibwq->wq_num);
4234 			return err;
4235 		}
4236 
4237 		qp_cur_state = IB_QPS_INIT;
4238 	}
4239 
4240 	attr_mask = 0;
4241 	err = __mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL, attr_mask,
4242 				  qp_cur_state,  qp_new_state, udata);
4243 
4244 	if (err && (qp_cur_state == IB_QPS_INIT)) {
4245 		qp_new_state = IB_QPS_RESET;
4246 		if (__mlx4_ib_modify_qp(ibwq, MLX4_IB_RWQ_SRC, NULL,
4247 					attr_mask, IB_QPS_INIT, IB_QPS_RESET,
4248 					udata)) {
4249 			pr_warn("WQN=0x%06x failed with reverting HW's resources failure\n",
4250 				ibwq->wq_num);
4251 			qp_new_state = IB_QPS_INIT;
4252 		}
4253 	}
4254 
4255 	qp->state = qp_new_state;
4256 
4257 	return err;
4258 }
4259 
4260 int mlx4_ib_modify_wq(struct ib_wq *ibwq, struct ib_wq_attr *wq_attr,
4261 		      u32 wq_attr_mask, struct ib_udata *udata)
4262 {
4263 	struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
4264 	struct mlx4_ib_modify_wq ucmd = {};
4265 	size_t required_cmd_sz;
4266 	enum ib_wq_state cur_state, new_state;
4267 	int err = 0;
4268 
4269 	required_cmd_sz = offsetof(typeof(ucmd), reserved) +
4270 				   sizeof(ucmd.reserved);
4271 	if (udata->inlen < required_cmd_sz)
4272 		return -EINVAL;
4273 
4274 	if (udata->inlen > sizeof(ucmd) &&
4275 	    !ib_is_udata_cleared(udata, sizeof(ucmd),
4276 				 udata->inlen - sizeof(ucmd)))
4277 		return -EOPNOTSUPP;
4278 
4279 	if (ib_copy_from_udata(&ucmd, udata, min(sizeof(ucmd), udata->inlen)))
4280 		return -EFAULT;
4281 
4282 	if (ucmd.comp_mask || ucmd.reserved)
4283 		return -EOPNOTSUPP;
4284 
4285 	if (wq_attr_mask & IB_WQ_FLAGS)
4286 		return -EOPNOTSUPP;
4287 
4288 	cur_state = wq_attr_mask & IB_WQ_CUR_STATE ? wq_attr->curr_wq_state :
4289 						     ibwq->state;
4290 	new_state = wq_attr_mask & IB_WQ_STATE ? wq_attr->wq_state : cur_state;
4291 
4292 	if (cur_state  < IB_WQS_RESET || cur_state  > IB_WQS_ERR ||
4293 	    new_state < IB_WQS_RESET || new_state > IB_WQS_ERR)
4294 		return -EINVAL;
4295 
4296 	if ((new_state == IB_WQS_RDY) && (cur_state == IB_WQS_ERR))
4297 		return -EINVAL;
4298 
4299 	if ((new_state == IB_WQS_ERR) && (cur_state == IB_WQS_RESET))
4300 		return -EINVAL;
4301 
4302 	/* Need to protect against the parent RSS which also may modify WQ
4303 	 * state.
4304 	 */
4305 	mutex_lock(&qp->mutex);
4306 
4307 	/* Can update HW state only if a RSS QP has already associated to this
4308 	 * WQ, so we can apply its port on the WQ.
4309 	 */
4310 	if (qp->rss_usecnt)
4311 		err = _mlx4_ib_modify_wq(ibwq, new_state, udata);
4312 
4313 	if (!err)
4314 		ibwq->state = new_state;
4315 
4316 	mutex_unlock(&qp->mutex);
4317 
4318 	return err;
4319 }
4320 
4321 void mlx4_ib_destroy_wq(struct ib_wq *ibwq, struct ib_udata *udata)
4322 {
4323 	struct mlx4_ib_dev *dev = to_mdev(ibwq->device);
4324 	struct mlx4_ib_qp *qp = to_mqp((struct ib_qp *)ibwq);
4325 
4326 	if (qp->counter_index)
4327 		mlx4_ib_free_qp_counter(dev, qp);
4328 
4329 	destroy_qp_common(dev, qp, MLX4_IB_RWQ_SRC, udata);
4330 
4331 	kfree(qp);
4332 }
4333 
4334 struct ib_rwq_ind_table
4335 *mlx4_ib_create_rwq_ind_table(struct ib_device *device,
4336 			      struct ib_rwq_ind_table_init_attr *init_attr,
4337 			      struct ib_udata *udata)
4338 {
4339 	struct ib_rwq_ind_table *rwq_ind_table;
4340 	struct mlx4_ib_create_rwq_ind_tbl_resp resp = {};
4341 	unsigned int ind_tbl_size = 1 << init_attr->log_ind_tbl_size;
4342 	unsigned int base_wqn;
4343 	size_t min_resp_len;
4344 	int i;
4345 	int err;
4346 
4347 	if (udata->inlen > 0 &&
4348 	    !ib_is_udata_cleared(udata, 0,
4349 				 udata->inlen))
4350 		return ERR_PTR(-EOPNOTSUPP);
4351 
4352 	min_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
4353 	if (udata->outlen && udata->outlen < min_resp_len)
4354 		return ERR_PTR(-EINVAL);
4355 
4356 	if (ind_tbl_size >
4357 	    device->attrs.rss_caps.max_rwq_indirection_table_size) {
4358 		pr_debug("log_ind_tbl_size = %d is bigger than supported = %d\n",
4359 			 ind_tbl_size,
4360 			 device->attrs.rss_caps.max_rwq_indirection_table_size);
4361 		return ERR_PTR(-EINVAL);
4362 	}
4363 
4364 	base_wqn = init_attr->ind_tbl[0]->wq_num;
4365 
4366 	if (base_wqn % ind_tbl_size) {
4367 		pr_debug("WQN=0x%x isn't aligned with indirection table size\n",
4368 			 base_wqn);
4369 		return ERR_PTR(-EINVAL);
4370 	}
4371 
4372 	for (i = 1; i < ind_tbl_size; i++) {
4373 		if (++base_wqn != init_attr->ind_tbl[i]->wq_num) {
4374 			pr_debug("indirection table's WQNs aren't consecutive\n");
4375 			return ERR_PTR(-EINVAL);
4376 		}
4377 	}
4378 
4379 	rwq_ind_table = kzalloc(sizeof(*rwq_ind_table), GFP_KERNEL);
4380 	if (!rwq_ind_table)
4381 		return ERR_PTR(-ENOMEM);
4382 
4383 	if (udata->outlen) {
4384 		resp.response_length = offsetof(typeof(resp), response_length) +
4385 					sizeof(resp.response_length);
4386 		err = ib_copy_to_udata(udata, &resp, resp.response_length);
4387 		if (err)
4388 			goto err;
4389 	}
4390 
4391 	return rwq_ind_table;
4392 
4393 err:
4394 	kfree(rwq_ind_table);
4395 	return ERR_PTR(err);
4396 }
4397 
4398 int mlx4_ib_destroy_rwq_ind_table(struct ib_rwq_ind_table *ib_rwq_ind_tbl)
4399 {
4400 	kfree(ib_rwq_ind_tbl);
4401 	return 0;
4402 }
4403 
4404 struct mlx4_ib_drain_cqe {
4405 	struct ib_cqe cqe;
4406 	struct completion done;
4407 };
4408 
4409 static void mlx4_ib_drain_qp_done(struct ib_cq *cq, struct ib_wc *wc)
4410 {
4411 	struct mlx4_ib_drain_cqe *cqe = container_of(wc->wr_cqe,
4412 						     struct mlx4_ib_drain_cqe,
4413 						     cqe);
4414 
4415 	complete(&cqe->done);
4416 }
4417 
4418 /* This function returns only once the drained WR was completed */
4419 static void handle_drain_completion(struct ib_cq *cq,
4420 				    struct mlx4_ib_drain_cqe *sdrain,
4421 				    struct mlx4_ib_dev *dev)
4422 {
4423 	struct mlx4_dev *mdev = dev->dev;
4424 
4425 	if (cq->poll_ctx == IB_POLL_DIRECT) {
4426 		while (wait_for_completion_timeout(&sdrain->done, HZ / 10) <= 0)
4427 			ib_process_cq_direct(cq, -1);
4428 		return;
4429 	}
4430 
4431 	if (mdev->persist->state == MLX4_DEVICE_STATE_INTERNAL_ERROR) {
4432 		struct mlx4_ib_cq *mcq = to_mcq(cq);
4433 		bool triggered = false;
4434 		unsigned long flags;
4435 
4436 		spin_lock_irqsave(&dev->reset_flow_resource_lock, flags);
4437 		/* Make sure that the CQ handler won't run if wasn't run yet */
4438 		if (!mcq->mcq.reset_notify_added)
4439 			mcq->mcq.reset_notify_added = 1;
4440 		else
4441 			triggered = true;
4442 		spin_unlock_irqrestore(&dev->reset_flow_resource_lock, flags);
4443 
4444 		if (triggered) {
4445 			/* Wait for any scheduled/running task to be ended */
4446 			switch (cq->poll_ctx) {
4447 			case IB_POLL_SOFTIRQ:
4448 				irq_poll_disable(&cq->iop);
4449 				irq_poll_enable(&cq->iop);
4450 				break;
4451 			case IB_POLL_WORKQUEUE:
4452 				cancel_work_sync(&cq->work);
4453 				break;
4454 			default:
4455 				WARN_ON_ONCE(1);
4456 			}
4457 		}
4458 
4459 		/* Run the CQ handler - this makes sure that the drain WR will
4460 		 * be processed if wasn't processed yet.
4461 		 */
4462 		mcq->mcq.comp(&mcq->mcq);
4463 	}
4464 
4465 	wait_for_completion(&sdrain->done);
4466 }
4467 
4468 void mlx4_ib_drain_sq(struct ib_qp *qp)
4469 {
4470 	struct ib_cq *cq = qp->send_cq;
4471 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
4472 	struct mlx4_ib_drain_cqe sdrain;
4473 	const struct ib_send_wr *bad_swr;
4474 	struct ib_rdma_wr swr = {
4475 		.wr = {
4476 			.next = NULL,
4477 			{ .wr_cqe	= &sdrain.cqe, },
4478 			.opcode	= IB_WR_RDMA_WRITE,
4479 		},
4480 	};
4481 	int ret;
4482 	struct mlx4_ib_dev *dev = to_mdev(qp->device);
4483 	struct mlx4_dev *mdev = dev->dev;
4484 
4485 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
4486 	if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) {
4487 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
4488 		return;
4489 	}
4490 
4491 	sdrain.cqe.done = mlx4_ib_drain_qp_done;
4492 	init_completion(&sdrain.done);
4493 
4494 	ret = _mlx4_ib_post_send(qp, &swr.wr, &bad_swr, true);
4495 	if (ret) {
4496 		WARN_ONCE(ret, "failed to drain send queue: %d\n", ret);
4497 		return;
4498 	}
4499 
4500 	handle_drain_completion(cq, &sdrain, dev);
4501 }
4502 
4503 void mlx4_ib_drain_rq(struct ib_qp *qp)
4504 {
4505 	struct ib_cq *cq = qp->recv_cq;
4506 	struct ib_qp_attr attr = { .qp_state = IB_QPS_ERR };
4507 	struct mlx4_ib_drain_cqe rdrain;
4508 	struct ib_recv_wr rwr = {};
4509 	const struct ib_recv_wr *bad_rwr;
4510 	int ret;
4511 	struct mlx4_ib_dev *dev = to_mdev(qp->device);
4512 	struct mlx4_dev *mdev = dev->dev;
4513 
4514 	ret = ib_modify_qp(qp, &attr, IB_QP_STATE);
4515 	if (ret && mdev->persist->state != MLX4_DEVICE_STATE_INTERNAL_ERROR) {
4516 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
4517 		return;
4518 	}
4519 
4520 	rwr.wr_cqe = &rdrain.cqe;
4521 	rdrain.cqe.done = mlx4_ib_drain_qp_done;
4522 	init_completion(&rdrain.done);
4523 
4524 	ret = _mlx4_ib_post_recv(qp, &rwr, &bad_rwr, true);
4525 	if (ret) {
4526 		WARN_ONCE(ret, "failed to drain recv queue: %d\n", ret);
4527 		return;
4528 	}
4529 
4530 	handle_drain_completion(cq, &rdrain, dev);
4531 }
4532