xref: /freebsd/sys/dev/mlx5/mlx5_ib/mlx5_ib_main.c (revision e23731db48ef9c6568d4768b1f87d48514339faa)
1 /*-
2  * Copyright (c) 2013-2021, Mellanox Technologies, Ltd.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include "opt_rss.h"
27 #include "opt_ratelimit.h"
28 
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/pci.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/slab.h>
34 #if defined(CONFIG_X86)
35 #include <asm/pat.h>
36 #endif
37 #include <linux/sched.h>
38 #include <linux/delay.h>
39 #include <linux/fs.h>
40 #undef inode
41 #include <rdma/ib_user_verbs.h>
42 #include <rdma/ib_addr.h>
43 #include <rdma/ib_cache.h>
44 #include <dev/mlx5/port.h>
45 #include <dev/mlx5/vport.h>
46 #include <linux/list.h>
47 #include <rdma/ib_smi.h>
48 #include <rdma/ib_umem.h>
49 #include <rdma/uverbs_ioctl.h>
50 #include <linux/in.h>
51 #include <linux/etherdevice.h>
52 #include <dev/mlx5/fs.h>
53 #include <dev/mlx5/mlx5_ib/mlx5_ib.h>
54 
55 MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
56 MODULE_LICENSE("Dual BSD/GPL");
57 MODULE_DEPEND(mlx5ib, linuxkpi, 1, 1, 1);
58 MODULE_DEPEND(mlx5ib, mlx5, 1, 1, 1);
59 MODULE_DEPEND(mlx5ib, ibcore, 1, 1, 1);
60 MODULE_VERSION(mlx5ib, 1);
61 
62 enum {
63 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
64 };
65 
66 static enum rdma_link_layer
67 mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
68 {
69 	switch (port_type_cap) {
70 	case MLX5_CAP_PORT_TYPE_IB:
71 		return IB_LINK_LAYER_INFINIBAND;
72 	case MLX5_CAP_PORT_TYPE_ETH:
73 		return IB_LINK_LAYER_ETHERNET;
74 	default:
75 		return IB_LINK_LAYER_UNSPECIFIED;
76 	}
77 }
78 
79 static enum rdma_link_layer
80 mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
81 {
82 	struct mlx5_ib_dev *dev = to_mdev(device);
83 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
84 
85 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
86 }
87 
88 static bool mlx5_netdev_match(if_t ndev,
89 			      struct mlx5_core_dev *mdev,
90 			      const char *dname)
91 {
92 	return if_gettype(ndev) == IFT_ETHER &&
93 	  if_getdname(ndev) != NULL &&
94 	  strcmp(if_getdname(ndev), dname) == 0 &&
95 	  if_getsoftc(ndev) != NULL &&
96 	  *(struct mlx5_core_dev **)if_getsoftc(ndev) == mdev;
97 }
98 
99 static int mlx5_netdev_event(struct notifier_block *this,
100 			     unsigned long event, void *ptr)
101 {
102 	if_t ndev = netdev_notifier_info_to_ifp(ptr);
103 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
104 						 roce.nb);
105 
106 	switch (event) {
107 	case NETDEV_REGISTER:
108 	case NETDEV_UNREGISTER:
109 		write_lock(&ibdev->roce.netdev_lock);
110 		/* check if network interface belongs to mlx5en */
111 		if (mlx5_netdev_match(ndev, ibdev->mdev, "mce"))
112 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
113 					     NULL : ndev;
114 		write_unlock(&ibdev->roce.netdev_lock);
115 		break;
116 
117 	case NETDEV_UP:
118 	case NETDEV_DOWN: {
119 		if_t upper = NULL;
120 
121 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
122 		    && ibdev->ib_active) {
123 			struct ib_event ibev = {0};
124 
125 			ibev.device = &ibdev->ib_dev;
126 			ibev.event = (event == NETDEV_UP) ?
127 				     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
128 			ibev.element.port_num = 1;
129 			ib_dispatch_event(&ibev);
130 		}
131 		break;
132 	}
133 
134 	default:
135 		break;
136 	}
137 
138 	return NOTIFY_DONE;
139 }
140 
141 static if_t mlx5_ib_get_netdev(struct ib_device *device,
142 					     u8 port_num)
143 {
144 	struct mlx5_ib_dev *ibdev = to_mdev(device);
145 	if_t ndev;
146 
147 	/* Ensure ndev does not disappear before we invoke if_ref()
148 	 */
149 	read_lock(&ibdev->roce.netdev_lock);
150 	ndev = ibdev->roce.netdev;
151 	if (ndev)
152 		if_ref(ndev);
153 	read_unlock(&ibdev->roce.netdev_lock);
154 
155 	return ndev;
156 }
157 
158 static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
159 				    u8 *active_width)
160 {
161 	switch (eth_proto_oper) {
162 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
163 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
164 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
165 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
166 		*active_width = IB_WIDTH_1X;
167 		*active_speed = IB_SPEED_SDR;
168 		break;
169 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
170 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
171 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
172 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
173 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
174 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
175 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER_LR):
176 		*active_width = IB_WIDTH_1X;
177 		*active_speed = IB_SPEED_QDR;
178 		break;
179 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
180 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
181 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
182 		*active_width = IB_WIDTH_1X;
183 		*active_speed = IB_SPEED_EDR;
184 		break;
185 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
186 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
187 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
188 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4_ER4):
189 		*active_width = IB_WIDTH_4X;
190 		*active_speed = IB_SPEED_QDR;
191 		break;
192 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
193 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
194 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR4):
195 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
196 		*active_width = IB_WIDTH_1X;
197 		*active_speed = IB_SPEED_HDR;
198 		break;
199 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
200 		*active_width = IB_WIDTH_4X;
201 		*active_speed = IB_SPEED_FDR;
202 		break;
203 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
204 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
205 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
206 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
207 		*active_width = IB_WIDTH_4X;
208 		*active_speed = IB_SPEED_EDR;
209 		break;
210 	default:
211 		*active_width = IB_WIDTH_4X;
212 		*active_speed = IB_SPEED_QDR;
213 		return -EINVAL;
214 	}
215 
216 	return 0;
217 }
218 
219 static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
220 					u8 *active_width)
221 {
222 	switch (eth_proto_oper) {
223 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
224 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
225 		*active_width = IB_WIDTH_1X;
226 		*active_speed = IB_SPEED_SDR;
227 		break;
228 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
229 		*active_width = IB_WIDTH_1X;
230 		*active_speed = IB_SPEED_DDR;
231 		break;
232 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
233 		*active_width = IB_WIDTH_1X;
234 		*active_speed = IB_SPEED_QDR;
235 		break;
236 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
237 		*active_width = IB_WIDTH_4X;
238 		*active_speed = IB_SPEED_QDR;
239 		break;
240 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
241 		*active_width = IB_WIDTH_1X;
242 		*active_speed = IB_SPEED_EDR;
243 		break;
244 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
245 		*active_width = IB_WIDTH_2X;
246 		*active_speed = IB_SPEED_EDR;
247 		break;
248 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
249 		*active_width = IB_WIDTH_1X;
250 		*active_speed = IB_SPEED_HDR;
251 		break;
252 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
253 		*active_width = IB_WIDTH_4X;
254 		*active_speed = IB_SPEED_EDR;
255 		break;
256 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
257 		*active_width = IB_WIDTH_2X;
258 		*active_speed = IB_SPEED_HDR;
259 		break;
260 	case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR):
261 		*active_width = IB_WIDTH_1X;
262 		*active_speed = IB_SPEED_NDR;
263 		break;
264 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
265 		*active_width = IB_WIDTH_4X;
266 		*active_speed = IB_SPEED_HDR;
267 		break;
268 	case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2):
269 		*active_width = IB_WIDTH_2X;
270 		*active_speed = IB_SPEED_NDR;
271 		break;
272 	case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4):
273 		*active_width = IB_WIDTH_4X;
274 		*active_speed = IB_SPEED_NDR;
275 		break;
276 	default:
277 		*active_width = IB_WIDTH_4X;
278 		*active_speed = IB_SPEED_QDR;
279 		return -EINVAL;
280 	}
281 
282 	return 0;
283 }
284 
285 static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
286 				struct ib_port_attr *props)
287 {
288 	struct mlx5_ib_dev *dev = to_mdev(device);
289 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {};
290 	if_t ndev;
291 	enum ib_mtu ndev_ib_mtu;
292 	u16 qkey_viol_cntr;
293 	u32 eth_prot_oper;
294 	bool ext;
295 	int err;
296 
297 	memset(props, 0, sizeof(*props));
298 
299 	/* Possible bad flows are checked before filling out props so in case
300 	 * of an error it will still be zeroed out.
301 	 */
302 	err = mlx5_query_port_ptys(dev->mdev, out, sizeof(out), MLX5_PTYS_EN,
303 	    port_num);
304 	if (err)
305 		return err;
306 
307 	ext = MLX5_CAP_PCAM_FEATURE(dev->mdev, ptys_extended_ethernet);
308 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
309 
310 	if (ext)
311 		translate_eth_ext_proto_oper(eth_prot_oper, &props->active_speed,
312 		    &props->active_width);
313 	else
314 		translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
315 		    &props->active_width);
316 
317 	props->port_cap_flags  |= IB_PORT_CM_SUP;
318 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
319 
320 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
321 						roce_address_table_size);
322 	props->max_mtu          = IB_MTU_4096;
323 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
324 	props->pkey_tbl_len     = 1;
325 	props->state            = IB_PORT_DOWN;
326 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
327 
328 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
329 	props->qkey_viol_cntr = qkey_viol_cntr;
330 
331 	ndev = mlx5_ib_get_netdev(device, port_num);
332 	if (!ndev)
333 		return 0;
334 
335 	if (if_getdrvflags(ndev) & IFF_DRV_RUNNING &&
336 	    if_getlinkstate(ndev) == LINK_STATE_UP) {
337 		props->state      = IB_PORT_ACTIVE;
338 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
339 	}
340 
341 	ndev_ib_mtu = iboe_get_mtu(if_getmtu(ndev));
342 
343 	if_rele(ndev);
344 
345 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
346 	return 0;
347 }
348 
349 static void ib_gid_to_mlx5_roce_addr(const union ib_gid *gid,
350 				     const struct ib_gid_attr *attr,
351 				     void *mlx5_addr)
352 {
353 #define MLX5_SET_RA(p, f, v) MLX5_SET(roce_addr_layout, p, f, v)
354 	char *mlx5_addr_l3_addr	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
355 					       source_l3_address);
356 	void *mlx5_addr_mac	= MLX5_ADDR_OF(roce_addr_layout, mlx5_addr,
357 					       source_mac_47_32);
358 	u16 vlan_id;
359 
360 	if (!gid)
361 		return;
362 	ether_addr_copy(mlx5_addr_mac, if_getlladdr(attr->ndev));
363 
364 	vlan_id = rdma_vlan_dev_vlan_id(attr->ndev);
365 	if (vlan_id != 0xffff) {
366 		MLX5_SET_RA(mlx5_addr, vlan_valid, 1);
367 		MLX5_SET_RA(mlx5_addr, vlan_id, vlan_id);
368 	}
369 
370 	switch (attr->gid_type) {
371 	case IB_GID_TYPE_IB:
372 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_1);
373 		break;
374 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
375 		MLX5_SET_RA(mlx5_addr, roce_version, MLX5_ROCE_VERSION_2);
376 		break;
377 
378 	default:
379 		WARN_ON(true);
380 	}
381 
382 	if (attr->gid_type != IB_GID_TYPE_IB) {
383 		if (ipv6_addr_v4mapped((void *)gid))
384 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
385 				    MLX5_ROCE_L3_TYPE_IPV4);
386 		else
387 			MLX5_SET_RA(mlx5_addr, roce_l3_type,
388 				    MLX5_ROCE_L3_TYPE_IPV6);
389 	}
390 
391 	if ((attr->gid_type == IB_GID_TYPE_IB) ||
392 	    !ipv6_addr_v4mapped((void *)gid))
393 		memcpy(mlx5_addr_l3_addr, gid, sizeof(*gid));
394 	else
395 		memcpy(&mlx5_addr_l3_addr[12], &gid->raw[12], 4);
396 }
397 
398 static int set_roce_addr(struct ib_device *device, u8 port_num,
399 			 unsigned int index,
400 			 const union ib_gid *gid,
401 			 const struct ib_gid_attr *attr)
402 {
403 	struct mlx5_ib_dev *dev = to_mdev(device);
404 	u32  in[MLX5_ST_SZ_DW(set_roce_address_in)]  = {0};
405 	u32 out[MLX5_ST_SZ_DW(set_roce_address_out)] = {0};
406 	void *in_addr = MLX5_ADDR_OF(set_roce_address_in, in, roce_address);
407 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(device, port_num);
408 
409 	if (ll != IB_LINK_LAYER_ETHERNET)
410 		return -EINVAL;
411 
412 	ib_gid_to_mlx5_roce_addr(gid, attr, in_addr);
413 
414 	MLX5_SET(set_roce_address_in, in, roce_address_index, index);
415 	MLX5_SET(set_roce_address_in, in, opcode, MLX5_CMD_OP_SET_ROCE_ADDRESS);
416 	return mlx5_cmd_exec(dev->mdev, in, sizeof(in), out, sizeof(out));
417 }
418 
419 static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
420 			   unsigned int index, const union ib_gid *gid,
421 			   const struct ib_gid_attr *attr,
422 			   __always_unused void **context)
423 {
424 	return set_roce_addr(device, port_num, index, gid, attr);
425 }
426 
427 static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
428 			   unsigned int index, __always_unused void **context)
429 {
430 	return set_roce_addr(device, port_num, index, NULL, NULL);
431 }
432 
433 __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
434 			       int index)
435 {
436 	struct ib_gid_attr attr;
437 	union ib_gid gid;
438 
439 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
440 		return 0;
441 
442 	if (!attr.ndev)
443 		return 0;
444 
445 	if_rele(attr.ndev);
446 
447 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
448 		return 0;
449 
450 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
451 }
452 
453 int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
454 			   int index, enum ib_gid_type *gid_type)
455 {
456 	struct ib_gid_attr attr;
457 	union ib_gid gid;
458 	int ret;
459 
460 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
461 	if (ret)
462 		return ret;
463 
464 	if (!attr.ndev)
465 		return -ENODEV;
466 
467 	if_rele(attr.ndev);
468 
469 	*gid_type = attr.gid_type;
470 
471 	return 0;
472 }
473 
474 static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
475 {
476 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
477 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
478 	return 0;
479 }
480 
481 enum {
482 	MLX5_VPORT_ACCESS_METHOD_MAD,
483 	MLX5_VPORT_ACCESS_METHOD_HCA,
484 	MLX5_VPORT_ACCESS_METHOD_NIC,
485 };
486 
487 static int mlx5_get_vport_access_method(struct ib_device *ibdev)
488 {
489 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
490 		return MLX5_VPORT_ACCESS_METHOD_MAD;
491 
492 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
493 	    IB_LINK_LAYER_ETHERNET)
494 		return MLX5_VPORT_ACCESS_METHOD_NIC;
495 
496 	return MLX5_VPORT_ACCESS_METHOD_HCA;
497 }
498 
499 static void get_atomic_caps(struct mlx5_ib_dev *dev,
500 			    struct ib_device_attr *props)
501 {
502 	u8 tmp;
503 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
504 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
505 	u8 atomic_req_8B_endianness_mode =
506 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianess_mode);
507 
508 	/* Check if HW supports 8 bytes standard atomic operations and capable
509 	 * of host endianness respond
510 	 */
511 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
512 	if (((atomic_operations & tmp) == tmp) &&
513 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
514 	    (atomic_req_8B_endianness_mode)) {
515 		props->atomic_cap = IB_ATOMIC_HCA;
516 	} else {
517 		props->atomic_cap = IB_ATOMIC_NONE;
518 	}
519 }
520 
521 static int mlx5_query_system_image_guid(struct ib_device *ibdev,
522 					__be64 *sys_image_guid)
523 {
524 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
525 	struct mlx5_core_dev *mdev = dev->mdev;
526 	u64 tmp;
527 	int err;
528 
529 	switch (mlx5_get_vport_access_method(ibdev)) {
530 	case MLX5_VPORT_ACCESS_METHOD_MAD:
531 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
532 							    sys_image_guid);
533 
534 	case MLX5_VPORT_ACCESS_METHOD_HCA:
535 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
536 		break;
537 
538 	case MLX5_VPORT_ACCESS_METHOD_NIC:
539 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
540 		break;
541 
542 	default:
543 		return -EINVAL;
544 	}
545 
546 	if (!err)
547 		*sys_image_guid = cpu_to_be64(tmp);
548 
549 	return err;
550 
551 }
552 
553 static int mlx5_query_max_pkeys(struct ib_device *ibdev,
554 				u16 *max_pkeys)
555 {
556 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
557 	struct mlx5_core_dev *mdev = dev->mdev;
558 
559 	switch (mlx5_get_vport_access_method(ibdev)) {
560 	case MLX5_VPORT_ACCESS_METHOD_MAD:
561 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
562 
563 	case MLX5_VPORT_ACCESS_METHOD_HCA:
564 	case MLX5_VPORT_ACCESS_METHOD_NIC:
565 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
566 						pkey_table_size));
567 		return 0;
568 
569 	default:
570 		return -EINVAL;
571 	}
572 }
573 
574 static int mlx5_query_vendor_id(struct ib_device *ibdev,
575 				u32 *vendor_id)
576 {
577 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
578 
579 	switch (mlx5_get_vport_access_method(ibdev)) {
580 	case MLX5_VPORT_ACCESS_METHOD_MAD:
581 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
582 
583 	case MLX5_VPORT_ACCESS_METHOD_HCA:
584 	case MLX5_VPORT_ACCESS_METHOD_NIC:
585 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
586 
587 	default:
588 		return -EINVAL;
589 	}
590 }
591 
592 static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
593 				__be64 *node_guid)
594 {
595 	u64 tmp;
596 	int err;
597 
598 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
599 	case MLX5_VPORT_ACCESS_METHOD_MAD:
600 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
601 
602 	case MLX5_VPORT_ACCESS_METHOD_HCA:
603 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
604 		break;
605 
606 	case MLX5_VPORT_ACCESS_METHOD_NIC:
607 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
608 		break;
609 
610 	default:
611 		return -EINVAL;
612 	}
613 
614 	if (!err)
615 		*node_guid = cpu_to_be64(tmp);
616 
617 	return err;
618 }
619 
620 struct mlx5_reg_node_desc {
621 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
622 };
623 
624 static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
625 {
626 	struct mlx5_reg_node_desc in;
627 
628 	if (mlx5_use_mad_ifc(dev))
629 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
630 
631 	memset(&in, 0, sizeof(in));
632 
633 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
634 				    sizeof(struct mlx5_reg_node_desc),
635 				    MLX5_REG_NODE_DESC, 0, 0);
636 }
637 
638 static int mlx5_ib_query_device(struct ib_device *ibdev,
639 				struct ib_device_attr *props,
640 				struct ib_udata *uhw)
641 {
642 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
643 	struct mlx5_core_dev *mdev = dev->mdev;
644 	int err = -ENOMEM;
645 	int max_sq_desc;
646 	int max_rq_sg;
647 	int max_sq_sg;
648 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
649 	struct mlx5_ib_query_device_resp resp = {};
650 	size_t resp_len;
651 	u64 max_tso;
652 
653 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
654 	if (uhw->outlen && uhw->outlen < resp_len)
655 		return -EINVAL;
656 	else
657 		resp.response_length = resp_len;
658 
659 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
660 		return -EINVAL;
661 
662 	memset(props, 0, sizeof(*props));
663 	err = mlx5_query_system_image_guid(ibdev,
664 					   &props->sys_image_guid);
665 	if (err)
666 		return err;
667 
668 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
669 	if (err)
670 		return err;
671 
672 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
673 	if (err)
674 		return err;
675 
676 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
677 		((u32)fw_rev_min(dev->mdev) << 16) |
678 		fw_rev_sub(dev->mdev);
679 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
680 		IB_DEVICE_PORT_ACTIVE_EVENT		|
681 		IB_DEVICE_SYS_IMAGE_GUID		|
682 		IB_DEVICE_RC_RNR_NAK_GEN		|
683 		IB_DEVICE_KNOWSEPOCH;
684 
685 	if (MLX5_CAP_GEN(mdev, pkv))
686 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
687 	if (MLX5_CAP_GEN(mdev, qkv))
688 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
689 	if (MLX5_CAP_GEN(mdev, apm))
690 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
691 	if (MLX5_CAP_GEN(mdev, xrc))
692 		props->device_cap_flags |= IB_DEVICE_XRC;
693 	if (MLX5_CAP_GEN(mdev, imaicl)) {
694 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
695 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
696 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
697 		/* We support 'Gappy' memory registration too */
698 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
699 	}
700 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
701 	if (MLX5_CAP_GEN(mdev, sho)) {
702 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
703 		/* At this stage no support for signature handover */
704 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
705 				      IB_PROT_T10DIF_TYPE_2 |
706 				      IB_PROT_T10DIF_TYPE_3;
707 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
708 				       IB_GUARD_T10DIF_CSUM;
709 	}
710 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
711 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
712 
713 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
714 		if (MLX5_CAP_ETH(mdev, csum_cap))
715 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
716 
717 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
718 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
719 			if (max_tso) {
720 				resp.tso_caps.max_tso = 1 << max_tso;
721 				resp.tso_caps.supported_qpts |=
722 					1 << IB_QPT_RAW_PACKET;
723 				resp.response_length += sizeof(resp.tso_caps);
724 			}
725 		}
726 
727 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
728 			resp.rss_caps.rx_hash_function =
729 						MLX5_RX_HASH_FUNC_TOEPLITZ;
730 			resp.rss_caps.rx_hash_fields_mask =
731 						MLX5_RX_HASH_SRC_IPV4 |
732 						MLX5_RX_HASH_DST_IPV4 |
733 						MLX5_RX_HASH_SRC_IPV6 |
734 						MLX5_RX_HASH_DST_IPV6 |
735 						MLX5_RX_HASH_SRC_PORT_TCP |
736 						MLX5_RX_HASH_DST_PORT_TCP |
737 						MLX5_RX_HASH_SRC_PORT_UDP |
738 						MLX5_RX_HASH_DST_PORT_UDP;
739 			resp.response_length += sizeof(resp.rss_caps);
740 		}
741 	} else {
742 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
743 			resp.response_length += sizeof(resp.tso_caps);
744 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
745 			resp.response_length += sizeof(resp.rss_caps);
746 	}
747 
748 	if (MLX5_CAP_GEN(mdev, ipoib_ipoib_offloads)) {
749 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
750 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
751 	}
752 
753 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
754 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs))
755 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
756 
757 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
758 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
759 
760 	props->vendor_part_id	   = mdev->pdev->device;
761 	props->hw_ver		   = mdev->pdev->revision;
762 
763 	props->max_mr_size	   = ~0ull;
764 	props->page_size_cap	   = ~(min_page_size - 1);
765 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
766 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
767 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
768 		     sizeof(struct mlx5_wqe_data_seg);
769 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
770 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
771 		     sizeof(struct mlx5_wqe_raddr_seg)) /
772 		sizeof(struct mlx5_wqe_data_seg);
773 	props->max_sge = min(max_rq_sg, max_sq_sg);
774 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
775 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
776 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
777 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
778 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
779 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
780 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
781 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
782 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
783 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
784 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
785 	props->max_srq_sge	   = max_rq_sg - 1;
786 	props->max_fast_reg_page_list_len =
787 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
788 	get_atomic_caps(dev, props);
789 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
790 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
791 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
792 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
793 					   props->max_mcast_grp;
794 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
795 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
796 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
797 
798 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
799 	if (MLX5_CAP_GEN(mdev, pg))
800 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
801 	props->odp_caps = dev->odp_caps;
802 #endif
803 
804 	if (MLX5_CAP_GEN(mdev, cd))
805 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
806 
807 	if (!mlx5_core_is_pf(mdev))
808 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
809 
810 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
811 	    IB_LINK_LAYER_ETHERNET) {
812 		props->rss_caps.max_rwq_indirection_tables =
813 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
814 		props->rss_caps.max_rwq_indirection_table_size =
815 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
816 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
817 		props->max_wq_type_rq =
818 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
819 	}
820 
821 	if (uhw->outlen) {
822 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
823 
824 		if (err)
825 			return err;
826 	}
827 
828 	return 0;
829 }
830 
831 enum mlx5_ib_width {
832 	MLX5_IB_WIDTH_1X	= 1 << 0,
833 	MLX5_IB_WIDTH_2X	= 1 << 1,
834 	MLX5_IB_WIDTH_4X	= 1 << 2,
835 	MLX5_IB_WIDTH_8X	= 1 << 3,
836 	MLX5_IB_WIDTH_12X	= 1 << 4
837 };
838 
839 static int translate_active_width(struct ib_device *ibdev, u8 active_width,
840 				  u8 *ib_width)
841 {
842 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
843 	int err = 0;
844 
845 	if (active_width & MLX5_IB_WIDTH_1X) {
846 		*ib_width = IB_WIDTH_1X;
847 	} else if (active_width & MLX5_IB_WIDTH_2X) {
848 		*ib_width = IB_WIDTH_2X;
849 	} else if (active_width & MLX5_IB_WIDTH_4X) {
850 		*ib_width = IB_WIDTH_4X;
851 	} else if (active_width & MLX5_IB_WIDTH_8X) {
852 		*ib_width = IB_WIDTH_8X;
853 	} else if (active_width & MLX5_IB_WIDTH_12X) {
854 		*ib_width = IB_WIDTH_12X;
855 	} else {
856 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
857 			    (int)active_width);
858 		err = -EINVAL;
859 	}
860 
861 	return err;
862 }
863 
864 enum ib_max_vl_num {
865 	__IB_MAX_VL_0		= 1,
866 	__IB_MAX_VL_0_1		= 2,
867 	__IB_MAX_VL_0_3		= 3,
868 	__IB_MAX_VL_0_7		= 4,
869 	__IB_MAX_VL_0_14	= 5,
870 };
871 
872 enum mlx5_vl_hw_cap {
873 	MLX5_VL_HW_0	= 1,
874 	MLX5_VL_HW_0_1	= 2,
875 	MLX5_VL_HW_0_2	= 3,
876 	MLX5_VL_HW_0_3	= 4,
877 	MLX5_VL_HW_0_4	= 5,
878 	MLX5_VL_HW_0_5	= 6,
879 	MLX5_VL_HW_0_6	= 7,
880 	MLX5_VL_HW_0_7	= 8,
881 	MLX5_VL_HW_0_14	= 15
882 };
883 
884 static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
885 				u8 *max_vl_num)
886 {
887 	switch (vl_hw_cap) {
888 	case MLX5_VL_HW_0:
889 		*max_vl_num = __IB_MAX_VL_0;
890 		break;
891 	case MLX5_VL_HW_0_1:
892 		*max_vl_num = __IB_MAX_VL_0_1;
893 		break;
894 	case MLX5_VL_HW_0_3:
895 		*max_vl_num = __IB_MAX_VL_0_3;
896 		break;
897 	case MLX5_VL_HW_0_7:
898 		*max_vl_num = __IB_MAX_VL_0_7;
899 		break;
900 	case MLX5_VL_HW_0_14:
901 		*max_vl_num = __IB_MAX_VL_0_14;
902 		break;
903 
904 	default:
905 		return -EINVAL;
906 	}
907 
908 	return 0;
909 }
910 
911 static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
912 			       struct ib_port_attr *props)
913 {
914 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
915 	struct mlx5_core_dev *mdev = dev->mdev;
916 	u32 *rep;
917 	int replen = MLX5_ST_SZ_BYTES(query_hca_vport_context_out);
918 	struct mlx5_ptys_reg *ptys;
919 	struct mlx5_pmtu_reg *pmtu;
920 	struct mlx5_pvlc_reg pvlc;
921 	void *ctx;
922 	int err;
923 
924 	rep = mlx5_vzalloc(replen);
925 	ptys = kzalloc(sizeof(*ptys), GFP_KERNEL);
926 	pmtu = kzalloc(sizeof(*pmtu), GFP_KERNEL);
927 	if (!rep || !ptys || !pmtu) {
928 		err = -ENOMEM;
929 		goto out;
930 	}
931 
932 	memset(props, 0, sizeof(*props));
933 
934 	err = mlx5_query_hca_vport_context(mdev, port, 0, rep, replen);
935 	if (err)
936 		goto out;
937 
938 	ctx = MLX5_ADDR_OF(query_hca_vport_context_out, rep, hca_vport_context);
939 
940 	props->lid		= MLX5_GET(hca_vport_context, ctx, lid);
941 	props->lmc		= MLX5_GET(hca_vport_context, ctx, lmc);
942 	props->sm_lid		= MLX5_GET(hca_vport_context, ctx, sm_lid);
943 	props->sm_sl		= MLX5_GET(hca_vport_context, ctx, sm_sl);
944 	props->state		= MLX5_GET(hca_vport_context, ctx, vport_state);
945 	props->phys_state	= MLX5_GET(hca_vport_context, ctx,
946 					port_physical_state);
947 	props->port_cap_flags	= MLX5_GET(hca_vport_context, ctx, cap_mask1);
948 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
949 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
950 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
951 	props->bad_pkey_cntr	= MLX5_GET(hca_vport_context, ctx,
952 					pkey_violation_counter);
953 	props->qkey_viol_cntr	= MLX5_GET(hca_vport_context, ctx,
954 					qkey_violation_counter);
955 	props->subnet_timeout	= MLX5_GET(hca_vport_context, ctx,
956 					subnet_timeout);
957 	props->init_type_reply	= MLX5_GET(hca_vport_context, ctx,
958 					init_type_reply);
959 	props->grh_required	= MLX5_GET(hca_vport_context, ctx, grh_required);
960 
961 	ptys->proto_mask |= MLX5_PTYS_IB;
962 	ptys->local_port = port;
963 	err = mlx5_core_access_ptys(mdev, ptys, 0);
964 	if (err)
965 		goto out;
966 
967 	err = translate_active_width(ibdev, ptys->ib_link_width_oper,
968 				     &props->active_width);
969 	if (err)
970 		goto out;
971 
972 	props->active_speed	= (u8)ptys->ib_proto_oper;
973 
974 	pmtu->local_port = port;
975 	err = mlx5_core_access_pmtu(mdev, pmtu, 0);
976 	if (err)
977 		goto out;
978 
979 	props->max_mtu		= pmtu->max_mtu;
980 	props->active_mtu	= pmtu->oper_mtu;
981 
982 	memset(&pvlc, 0, sizeof(pvlc));
983 	pvlc.local_port = port;
984 	err = mlx5_core_access_pvlc(mdev, &pvlc, 0);
985 	if (err)
986 		goto out;
987 
988 	err = translate_max_vl_num(ibdev, pvlc.vl_hw_cap,
989 				   &props->max_vl_num);
990 out:
991 	kvfree(rep);
992 	kfree(ptys);
993 	kfree(pmtu);
994 	return err;
995 }
996 
997 int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
998 		       struct ib_port_attr *props)
999 {
1000 	switch (mlx5_get_vport_access_method(ibdev)) {
1001 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1002 		return mlx5_query_mad_ifc_port(ibdev, port, props);
1003 
1004 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1005 		return mlx5_query_hca_port(ibdev, port, props);
1006 
1007 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1008 		return mlx5_query_port_roce(ibdev, port, props);
1009 
1010 	default:
1011 		return -EINVAL;
1012 	}
1013 }
1014 
1015 static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1016 			     union ib_gid *gid)
1017 {
1018 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1019 	struct mlx5_core_dev *mdev = dev->mdev;
1020 
1021 	switch (mlx5_get_vport_access_method(ibdev)) {
1022 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1023 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1024 
1025 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1026 		return mlx5_query_hca_vport_gid(mdev, port, 0, index, gid);
1027 
1028 	default:
1029 		return -EINVAL;
1030 	}
1031 
1032 }
1033 
1034 static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1035 			      u16 *pkey)
1036 {
1037 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1038 	struct mlx5_core_dev *mdev = dev->mdev;
1039 
1040 	switch (mlx5_get_vport_access_method(ibdev)) {
1041 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1042 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1043 
1044 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1045 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1046 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
1047 						 pkey);
1048 	default:
1049 		return -EINVAL;
1050 	}
1051 }
1052 
1053 static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1054 				 struct ib_device_modify *props)
1055 {
1056 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1057 	struct mlx5_reg_node_desc in;
1058 	struct mlx5_reg_node_desc out;
1059 	int err;
1060 
1061 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1062 		return -EOPNOTSUPP;
1063 
1064 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1065 		return 0;
1066 
1067 	/*
1068 	 * If possible, pass node desc to FW, so it can generate
1069 	 * a 144 trap.  If cmd fails, just ignore.
1070 	 */
1071 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1072 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1073 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1074 	if (err)
1075 		return err;
1076 
1077 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1078 
1079 	return err;
1080 }
1081 
1082 static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1083 			       struct ib_port_modify *props)
1084 {
1085 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1086 	struct ib_port_attr attr;
1087 	u32 tmp;
1088 	int err;
1089 
1090 	/*
1091 	 * CM layer calls ib_modify_port() regardless of the link
1092 	 * layer. For Ethernet ports, qkey violation and Port
1093 	 * capabilities are meaningless.
1094 	 */
1095 	if (mlx5_ib_port_link_layer(ibdev, port) == IB_LINK_LAYER_ETHERNET)
1096 		return 0;
1097 
1098 	mutex_lock(&dev->cap_mask_mutex);
1099 
1100 	err = mlx5_ib_query_port(ibdev, port, &attr);
1101 	if (err)
1102 		goto out;
1103 
1104 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1105 		~props->clr_port_cap_mask;
1106 
1107 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1108 
1109 out:
1110 	mutex_unlock(&dev->cap_mask_mutex);
1111 	return err;
1112 }
1113 
1114 static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
1115 {
1116 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
1117 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
1118 }
1119 
1120 static u16 calc_dynamic_bfregs(int uars_per_sys_page)
1121 {
1122 	/* Large page with non 4k uar support might limit the dynamic size */
1123 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
1124 		return MLX5_MIN_DYN_BFREGS;
1125 
1126 	return MLX5_MAX_DYN_BFREGS;
1127 }
1128 
1129 static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1130 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1131 			     struct mlx5_bfreg_info *bfregi)
1132 {
1133 	int uars_per_sys_page;
1134 	int bfregs_per_sys_page;
1135 	int ref_bfregs = req->total_num_bfregs;
1136 
1137 	if (req->total_num_bfregs == 0)
1138 		return -EINVAL;
1139 
1140 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1141 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1142 
1143 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1144 		return -ENOMEM;
1145 
1146 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1147 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1148 	/* This holds the required static allocation asked by the user */
1149 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1150 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1151 		return -EINVAL;
1152 
1153 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1154 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
1155 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
1156 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
1157 
1158 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n",
1159 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1160 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1161 		    req->total_num_bfregs, bfregi->total_num_bfregs,
1162 		    bfregi->num_sys_pages);
1163 
1164 	return 0;
1165 }
1166 
1167 static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1168 {
1169 	struct mlx5_bfreg_info *bfregi;
1170 	int err;
1171 	int i;
1172 
1173 	bfregi = &context->bfregi;
1174 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1175 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1176 		if (err)
1177 			goto error;
1178 
1179 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1180 	}
1181 
1182 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
1183 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
1184 
1185 	return 0;
1186 
1187 error:
1188 	for (--i; i >= 0; i--)
1189 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1190 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1191 
1192 	return err;
1193 }
1194 
1195 static void deallocate_uars(struct mlx5_ib_dev *dev,
1196 			    struct mlx5_ib_ucontext *context)
1197 {
1198 	struct mlx5_bfreg_info *bfregi;
1199 	int i;
1200 
1201 	bfregi = &context->bfregi;
1202 	for (i = 0; i < bfregi->num_sys_pages; i++)
1203 		if (i < bfregi->num_static_sys_pages ||
1204 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
1205 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1206 }
1207 
1208 static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1209 					  u16 uid)
1210 {
1211 	int err;
1212 
1213 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1214 		return 0;
1215 
1216 	err = mlx5_alloc_transport_domain(dev->mdev, tdn, uid);
1217 	if (err)
1218 		return err;
1219 
1220 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1221 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1222 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1223 		return 0;
1224 
1225 	mutex_lock(&dev->lb_mutex);
1226 	dev->user_td++;
1227 
1228 	if (dev->user_td == 2)
1229 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1230 
1231 	mutex_unlock(&dev->lb_mutex);
1232 
1233 	if (err != 0)
1234 		mlx5_dealloc_transport_domain(dev->mdev, *tdn, uid);
1235 	return err;
1236 }
1237 
1238 static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1239 					     u16 uid)
1240 {
1241 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1242 		return;
1243 
1244 	mlx5_dealloc_transport_domain(dev->mdev, tdn, uid);
1245 
1246 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1247 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
1248 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1249 		return;
1250 
1251 	mutex_lock(&dev->lb_mutex);
1252 	dev->user_td--;
1253 
1254 	if (dev->user_td < 2)
1255 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1256 
1257 	mutex_unlock(&dev->lb_mutex);
1258 }
1259 
1260 static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1261 				  struct ib_udata *udata)
1262 {
1263 	struct ib_device *ibdev = uctx->device;
1264 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1265 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1266 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1267 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
1268 	struct mlx5_bfreg_info *bfregi;
1269 	int ver;
1270 	int err;
1271 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1272 				     max_cqe_version);
1273 	bool lib_uar_4k;
1274 	bool lib_uar_dyn;
1275 
1276 	if (!dev->ib_active)
1277 		return -EAGAIN;
1278 
1279 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
1280 		ver = 0;
1281 	else if (udata->inlen >= min_req_v2)
1282 		ver = 2;
1283 	else
1284 		return -EINVAL;
1285 
1286 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1287 	if (err)
1288 		return err;
1289 
1290 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1291 		return -EOPNOTSUPP;
1292 
1293 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1294 		return -EOPNOTSUPP;
1295 
1296 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
1297 				    MLX5_NON_FP_BFREGS_PER_UAR);
1298 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1299 		return -EINVAL;
1300 
1301 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
1302 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1303 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1304 	resp.cache_line_size = cache_line_size();
1305 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1306 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1307 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1308 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1309 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1310 	resp.cqe_version = min_t(__u8,
1311 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1312 				 req.max_cqe_version);
1313 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1314 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
1315 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
1316 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1317 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1318 				   sizeof(resp.response_length), udata->outlen);
1319 
1320 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
1321 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
1322 	bfregi = &context->bfregi;
1323 
1324 	if (lib_uar_dyn) {
1325 		bfregi->lib_uar_dyn = lib_uar_dyn;
1326 		goto uar_done;
1327 	}
1328 
1329 	/* updates req->total_num_bfregs */
1330 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1331 	if (err)
1332 		goto out_ctx;
1333 
1334 	mutex_init(&bfregi->lock);
1335 	bfregi->lib_uar_4k = lib_uar_4k;
1336 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1337 				GFP_KERNEL);
1338 	if (!bfregi->count) {
1339 		err = -ENOMEM;
1340 		goto out_ctx;
1341 	}
1342 
1343 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1344 				    sizeof(*bfregi->sys_pages),
1345 				    GFP_KERNEL);
1346 	if (!bfregi->sys_pages) {
1347 		err = -ENOMEM;
1348 		goto out_count;
1349 	}
1350 
1351 	err = allocate_uars(dev, context);
1352 	if (err)
1353 		goto out_sys_pages;
1354 
1355 uar_done:
1356 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1357 		err = mlx5_ib_devx_create(dev, true);
1358 		if (err < 0)
1359 			goto out_uars;
1360 		context->devx_uid = err;
1361 	}
1362 
1363 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1364 					     context->devx_uid);
1365 	if (err)
1366 		goto out_devx;
1367 
1368 	INIT_LIST_HEAD(&context->db_page_list);
1369 	mutex_init(&context->db_page_mutex);
1370 
1371 	resp.tot_bfregs = lib_uar_dyn ? 0 : req.total_num_bfregs;
1372 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1373 
1374 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1375 		resp.response_length += sizeof(resp.cqe_version);
1376 
1377 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
1378 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
1379 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1380 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1381 	}
1382 
1383 	/*
1384 	 * We don't want to expose information from the PCI bar that is located
1385 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1386 	 * pretend we don't support reading the HCA's core clock. This is also
1387 	 * forced by mmap function.
1388 	 */
1389 	if (offsetofend(typeof(resp), hca_core_clock_offset) <= udata->outlen) {
1390 		if (PAGE_SIZE <= 4096) {
1391 			resp.comp_mask |=
1392 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1393 			resp.hca_core_clock_offset =
1394 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1395 		}
1396 		resp.response_length += sizeof(resp.hca_core_clock_offset);
1397 	}
1398 
1399 	if (offsetofend(typeof(resp), log_uar_size) <= udata->outlen)
1400 		resp.response_length += sizeof(resp.log_uar_size);
1401 
1402 	if (offsetofend(typeof(resp), num_uars_per_page) <= udata->outlen)
1403 		resp.response_length += sizeof(resp.num_uars_per_page);
1404 
1405 	if (offsetofend(typeof(resp), num_dyn_bfregs) <= udata->outlen) {
1406 		resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
1407 		resp.response_length += sizeof(resp.num_dyn_bfregs);
1408 	}
1409 
1410 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1411 	if (err)
1412 		goto out_mdev;
1413 
1414 	bfregi->ver = ver;
1415 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1416 	context->cqe_version = resp.cqe_version;
1417 	context->lib_caps = req.lib_caps;
1418 	print_lib_caps(dev, context->lib_caps);
1419 
1420 	return 0;
1421 
1422 out_mdev:
1423 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1424 out_devx:
1425 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
1426 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1427 
1428 out_uars:
1429 	deallocate_uars(dev, context);
1430 
1431 out_sys_pages:
1432 	kfree(bfregi->sys_pages);
1433 
1434 out_count:
1435 	kfree(bfregi->count);
1436 
1437 out_ctx:
1438 	return err;
1439 }
1440 
1441 static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1442 {
1443 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1444 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1445 	struct mlx5_bfreg_info *bfregi;
1446 
1447 	bfregi = &context->bfregi;
1448 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1449 
1450 	if (context->devx_uid)
1451 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1452 
1453 	deallocate_uars(dev, context);
1454 	kfree(bfregi->sys_pages);
1455 	kfree(bfregi->count);
1456 }
1457 
1458 static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1459 				 int uar_idx)
1460 {
1461 	int fw_uars_per_page;
1462 
1463 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1464 
1465 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
1466 }
1467 
1468 static int get_command(unsigned long offset)
1469 {
1470 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1471 }
1472 
1473 static int get_arg(unsigned long offset)
1474 {
1475 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1476 }
1477 
1478 static int get_index(unsigned long offset)
1479 {
1480 	return get_arg(offset);
1481 }
1482 
1483 /* Index resides in an extra byte to enable larger values than 255 */
1484 static int get_extended_index(unsigned long offset)
1485 {
1486 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
1487 }
1488 
1489 
1490 static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
1491 {
1492 }
1493 
1494 static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1495 {
1496 	switch (cmd) {
1497 	case MLX5_IB_MMAP_WC_PAGE:
1498 		return "WC";
1499 	case MLX5_IB_MMAP_REGULAR_PAGE:
1500 		return "best effort WC";
1501 	case MLX5_IB_MMAP_NC_PAGE:
1502 		return "NC";
1503 	default:
1504 		return NULL;
1505 	}
1506 }
1507 
1508 static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
1509 					struct vm_area_struct *vma,
1510 					struct mlx5_ib_ucontext *context)
1511 {
1512 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
1513 	    !(vma->vm_flags & VM_SHARED))
1514 		return -EINVAL;
1515 
1516 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
1517 		return -EOPNOTSUPP;
1518 
1519 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
1520 		return -EPERM;
1521 
1522 	return -EOPNOTSUPP;
1523 }
1524 
1525 static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
1526 {
1527 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
1528 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
1529 
1530 	switch (mentry->mmap_flag) {
1531 	case MLX5_IB_MMAP_TYPE_UAR_WC:
1532 	case MLX5_IB_MMAP_TYPE_UAR_NC:
1533 		mlx5_cmd_free_uar(dev->mdev, mentry->page_idx);
1534 		kfree(mentry);
1535 		break;
1536 	default:
1537 		WARN_ON(true);
1538 	}
1539 }
1540 
1541 static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
1542 		    struct vm_area_struct *vma,
1543 		    struct mlx5_ib_ucontext *context)
1544 {
1545 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
1546 	int err;
1547 	unsigned long idx;
1548 	phys_addr_t pfn;
1549 	pgprot_t prot;
1550 	u32 bfreg_dyn_idx = 0;
1551 	u32 uar_index;
1552 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
1553 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
1554 				bfregi->num_static_sys_pages;
1555 
1556 	if (bfregi->lib_uar_dyn)
1557 		return -EINVAL;
1558 
1559 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1560 		return -EINVAL;
1561 
1562 	if (dyn_uar)
1563 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
1564 	else
1565 		idx = get_index(vma->vm_pgoff);
1566 
1567 	if (idx >= max_valid_idx) {
1568 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
1569 			     idx, max_valid_idx);
1570 		return -EINVAL;
1571 	}
1572 
1573 	switch (cmd) {
1574 	case MLX5_IB_MMAP_WC_PAGE:
1575 	case MLX5_IB_MMAP_ALLOC_WC:
1576 	case MLX5_IB_MMAP_REGULAR_PAGE:
1577 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
1578 		prot = pgprot_writecombine(vma->vm_page_prot);
1579 		break;
1580 	case MLX5_IB_MMAP_NC_PAGE:
1581 		prot = pgprot_noncached(vma->vm_page_prot);
1582 		break;
1583 	default:
1584 		return -EINVAL;
1585 	}
1586 
1587 	if (dyn_uar) {
1588 		int uars_per_page;
1589 
1590 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1591 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
1592 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
1593 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
1594 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
1595 			return -EINVAL;
1596 		}
1597 
1598 		mutex_lock(&bfregi->lock);
1599 		/* Fail if uar already allocated, first bfreg index of each
1600 		 * page holds its count.
1601 		 */
1602 		if (bfregi->count[bfreg_dyn_idx]) {
1603 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
1604 			mutex_unlock(&bfregi->lock);
1605 			return -EINVAL;
1606 		}
1607 
1608 		bfregi->count[bfreg_dyn_idx]++;
1609 		mutex_unlock(&bfregi->lock);
1610 
1611 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
1612 		if (err) {
1613 			mlx5_ib_warn(dev, "UAR alloc failed\n");
1614 			goto free_bfreg;
1615 		}
1616 	} else {
1617 		uar_index = bfregi->sys_pages[idx];
1618 	}
1619 
1620 	pfn = uar_index2pfn(dev, uar_index);
1621 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1622 
1623 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
1624 				prot, NULL);
1625 	if (err) {
1626 		mlx5_ib_err(dev,
1627 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
1628 			    err, mmap_cmd2str(cmd));
1629 		goto err;
1630 	}
1631 
1632 	if (dyn_uar)
1633 		bfregi->sys_pages[idx] = uar_index;
1634 	return 0;
1635 
1636 err:
1637 	if (!dyn_uar)
1638 		return err;
1639 
1640 	mlx5_cmd_free_uar(dev->mdev, idx);
1641 
1642 free_bfreg:
1643 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
1644 
1645 	return err;
1646 }
1647 
1648 static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
1649 {
1650 	unsigned long idx;
1651 	u8 command;
1652 
1653 	command = get_command(vma->vm_pgoff);
1654 	idx = get_extended_index(vma->vm_pgoff);
1655 
1656 	return (command << 16 | idx);
1657 }
1658 
1659 static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
1660 			       struct vm_area_struct *vma,
1661 			       struct ib_ucontext *ucontext)
1662 {
1663 	struct mlx5_user_mmap_entry *mentry;
1664 	struct rdma_user_mmap_entry *entry;
1665 	unsigned long pgoff;
1666 	pgprot_t prot;
1667 	phys_addr_t pfn;
1668 	int ret;
1669 
1670 	pgoff = mlx5_vma_to_pgoff(vma);
1671 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
1672 	if (!entry)
1673 		return -EINVAL;
1674 
1675 	mentry = to_mmmap(entry);
1676 	pfn = (mentry->address >> PAGE_SHIFT);
1677 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
1678 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
1679 		prot = pgprot_noncached(vma->vm_page_prot);
1680 	else
1681 		prot = pgprot_writecombine(vma->vm_page_prot);
1682 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
1683 				entry->npages * PAGE_SIZE,
1684 				prot,
1685 				entry);
1686 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
1687 	return ret;
1688 }
1689 
1690 static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
1691 {
1692 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1693 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1694 	unsigned long command;
1695 	phys_addr_t pfn;
1696 
1697 	command = get_command(vma->vm_pgoff);
1698 	switch (command) {
1699 	case MLX5_IB_MMAP_WC_PAGE:
1700 	case MLX5_IB_MMAP_ALLOC_WC:
1701 		if (!dev->wc_support)
1702 			return -EPERM;
1703 		/* FALLTHROUGH */
1704 	case MLX5_IB_MMAP_NC_PAGE:
1705 	case MLX5_IB_MMAP_REGULAR_PAGE:
1706 		return uar_mmap(dev, command, vma, context);
1707 
1708 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1709 		return -ENOSYS;
1710 
1711 	case MLX5_IB_MMAP_CORE_CLOCK:
1712 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1713 			return -EINVAL;
1714 
1715 		if (vma->vm_flags & VM_WRITE)
1716 			return -EPERM;
1717 
1718 		/* Don't expose to user-space information it shouldn't have */
1719 		if (PAGE_SIZE > 4096)
1720 			return -EOPNOTSUPP;
1721 
1722 		pfn = (dev->mdev->iseg_base +
1723 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1724 			PAGE_SHIFT;
1725 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
1726 					 PAGE_SIZE,
1727 					 pgprot_noncached(vma->vm_page_prot),
1728 					 NULL);
1729 	case MLX5_IB_MMAP_CLOCK_INFO:
1730 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
1731 
1732 	default:
1733 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
1734 	}
1735 
1736 	return 0;
1737 }
1738 
1739 static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
1740 {
1741 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
1742 	struct ib_device *ibdev = ibpd->device;
1743 	struct mlx5_ib_alloc_pd_resp resp;
1744 	int err;
1745 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
1746 		udata, struct mlx5_ib_ucontext, ibucontext);
1747 	u16 uid = context ? context->devx_uid : 0;
1748 
1749 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn, uid);
1750 	if (err)
1751 		return (err);
1752 
1753 	pd->uid = uid;
1754 	if (udata) {
1755 		resp.pdn = pd->pdn;
1756 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
1757 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
1758 			return -EFAULT;
1759 		}
1760 	}
1761 
1762 	return 0;
1763 }
1764 
1765 static void mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
1766 {
1767 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1768 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1769 
1770 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
1771 }
1772 
1773 enum {
1774 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1775 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1776 	MATCH_CRITERIA_ENABLE_INNER_BIT
1777 };
1778 
1779 #define HEADER_IS_ZERO(match_criteria, headers)			           \
1780 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1781 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1782 
1783 static u8 get_match_criteria_enable(u32 *match_criteria)
1784 {
1785 	u8 match_criteria_enable;
1786 
1787 	match_criteria_enable =
1788 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1789 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1790 	match_criteria_enable |=
1791 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1792 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1793 	match_criteria_enable |=
1794 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1795 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1796 
1797 	return match_criteria_enable;
1798 }
1799 
1800 static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1801 {
1802 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1803 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1804 }
1805 
1806 static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1807 {
1808 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1809 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1810 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1811 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1812 }
1813 
1814 #define LAST_ETH_FIELD vlan_tag
1815 #define LAST_IB_FIELD sl
1816 #define LAST_IPV4_FIELD tos
1817 #define LAST_IPV6_FIELD traffic_class
1818 #define LAST_TCP_UDP_FIELD src_port
1819 
1820 /* Field is the last supported field */
1821 #define FIELDS_NOT_SUPPORTED(filter, field)\
1822 	memchr_inv((void *)&filter.field  +\
1823 		   sizeof(filter.field), 0,\
1824 		   sizeof(filter) -\
1825 		   offsetof(typeof(filter), field) -\
1826 		   sizeof(filter.field))
1827 
1828 static int parse_flow_attr(u32 *match_c, u32 *match_v,
1829 			   const union ib_flow_spec *ib_spec)
1830 {
1831 	void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
1832 					     outer_headers);
1833 	void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
1834 					     outer_headers);
1835 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1836 					   misc_parameters);
1837 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1838 					   misc_parameters);
1839 
1840 	switch (ib_spec->type) {
1841 	case IB_FLOW_SPEC_ETH:
1842 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
1843 			return -ENOTSUPP;
1844 
1845 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1846 					     dmac_47_16),
1847 				ib_spec->eth.mask.dst_mac);
1848 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1849 					     dmac_47_16),
1850 				ib_spec->eth.val.dst_mac);
1851 
1852 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1853 					     smac_47_16),
1854 				ib_spec->eth.mask.src_mac);
1855 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1856 					     smac_47_16),
1857 				ib_spec->eth.val.src_mac);
1858 
1859 		if (ib_spec->eth.mask.vlan_tag) {
1860 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1861 				 cvlan_tag, 1);
1862 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1863 				 cvlan_tag, 1);
1864 
1865 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1866 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
1867 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1868 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
1869 
1870 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1871 				 first_cfi,
1872 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
1873 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1874 				 first_cfi,
1875 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
1876 
1877 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1878 				 first_prio,
1879 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
1880 			MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1881 				 first_prio,
1882 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
1883 		}
1884 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1885 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
1886 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1887 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
1888 		break;
1889 	case IB_FLOW_SPEC_IPV4:
1890 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
1891 			return -ENOTSUPP;
1892 
1893 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1894 			 ethertype, 0xffff);
1895 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1896 			 ethertype, ETH_P_IP);
1897 
1898 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1899 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1900 		       &ib_spec->ipv4.mask.src_ip,
1901 		       sizeof(ib_spec->ipv4.mask.src_ip));
1902 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1903 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
1904 		       &ib_spec->ipv4.val.src_ip,
1905 		       sizeof(ib_spec->ipv4.val.src_ip));
1906 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1907 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1908 		       &ib_spec->ipv4.mask.dst_ip,
1909 		       sizeof(ib_spec->ipv4.mask.dst_ip));
1910 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1911 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
1912 		       &ib_spec->ipv4.val.dst_ip,
1913 		       sizeof(ib_spec->ipv4.val.dst_ip));
1914 
1915 		set_tos(outer_headers_c, outer_headers_v,
1916 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
1917 
1918 		set_proto(outer_headers_c, outer_headers_v,
1919 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
1920 		break;
1921 	case IB_FLOW_SPEC_IPV6:
1922 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
1923 			return -ENOTSUPP;
1924 
1925 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c,
1926 			 ethertype, 0xffff);
1927 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
1928 			 ethertype, ETH_P_IPV6);
1929 
1930 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1931 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1932 		       &ib_spec->ipv6.mask.src_ip,
1933 		       sizeof(ib_spec->ipv6.mask.src_ip));
1934 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1935 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
1936 		       &ib_spec->ipv6.val.src_ip,
1937 		       sizeof(ib_spec->ipv6.val.src_ip));
1938 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_c,
1939 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1940 		       &ib_spec->ipv6.mask.dst_ip,
1941 		       sizeof(ib_spec->ipv6.mask.dst_ip));
1942 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, outer_headers_v,
1943 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
1944 		       &ib_spec->ipv6.val.dst_ip,
1945 		       sizeof(ib_spec->ipv6.val.dst_ip));
1946 
1947 		set_tos(outer_headers_c, outer_headers_v,
1948 			ib_spec->ipv6.mask.traffic_class,
1949 			ib_spec->ipv6.val.traffic_class);
1950 
1951 		set_proto(outer_headers_c, outer_headers_v,
1952 			  ib_spec->ipv6.mask.next_hdr,
1953 			  ib_spec->ipv6.val.next_hdr);
1954 
1955 		MLX5_SET(fte_match_set_misc, misc_params_c,
1956 			 outer_ipv6_flow_label,
1957 			 ntohl(ib_spec->ipv6.mask.flow_label));
1958 		MLX5_SET(fte_match_set_misc, misc_params_v,
1959 			 outer_ipv6_flow_label,
1960 			 ntohl(ib_spec->ipv6.val.flow_label));
1961 		break;
1962 	case IB_FLOW_SPEC_TCP:
1963 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1964 					 LAST_TCP_UDP_FIELD))
1965 			return -ENOTSUPP;
1966 
1967 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1968 			 0xff);
1969 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1970 			 IPPROTO_TCP);
1971 
1972 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_sport,
1973 			 ntohs(ib_spec->tcp_udp.mask.src_port));
1974 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_sport,
1975 			 ntohs(ib_spec->tcp_udp.val.src_port));
1976 
1977 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, tcp_dport,
1978 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
1979 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, tcp_dport,
1980 			 ntohs(ib_spec->tcp_udp.val.dst_port));
1981 		break;
1982 	case IB_FLOW_SPEC_UDP:
1983 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
1984 					 LAST_TCP_UDP_FIELD))
1985 			return -ENOTSUPP;
1986 
1987 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol,
1988 			 0xff);
1989 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, ip_protocol,
1990 			 IPPROTO_UDP);
1991 
1992 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_sport,
1993 			 ntohs(ib_spec->tcp_udp.mask.src_port));
1994 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_sport,
1995 			 ntohs(ib_spec->tcp_udp.val.src_port));
1996 
1997 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_c, udp_dport,
1998 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
1999 		MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v, udp_dport,
2000 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2001 		break;
2002 	default:
2003 		return -EINVAL;
2004 	}
2005 
2006 	return 0;
2007 }
2008 
2009 /* If a flow could catch both multicast and unicast packets,
2010  * it won't fall into the multicast flow steering table and this rule
2011  * could steal other multicast packets.
2012  */
2013 static bool flow_is_multicast_only(struct ib_flow_attr *ib_attr)
2014 {
2015 	struct ib_flow_spec_eth *eth_spec;
2016 
2017 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2018 	    ib_attr->size < sizeof(struct ib_flow_attr) +
2019 	    sizeof(struct ib_flow_spec_eth) ||
2020 	    ib_attr->num_of_specs < 1)
2021 		return false;
2022 
2023 	eth_spec = (struct ib_flow_spec_eth *)(ib_attr + 1);
2024 	if (eth_spec->type != IB_FLOW_SPEC_ETH ||
2025 	    eth_spec->size != sizeof(*eth_spec))
2026 		return false;
2027 
2028 	return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2029 	       is_multicast_ether_addr(eth_spec->val.dst_mac);
2030 }
2031 
2032 static bool is_valid_attr(const struct ib_flow_attr *flow_attr)
2033 {
2034 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
2035 	bool has_ipv4_spec = false;
2036 	bool eth_type_ipv4 = true;
2037 	unsigned int spec_index;
2038 
2039 	/* Validate that ethertype is correct */
2040 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
2041 		if (ib_spec->type == IB_FLOW_SPEC_ETH &&
2042 		    ib_spec->eth.mask.ether_type) {
2043 			if (!((ib_spec->eth.mask.ether_type == htons(0xffff)) &&
2044 			      ib_spec->eth.val.ether_type == htons(ETH_P_IP)))
2045 				eth_type_ipv4 = false;
2046 		} else if (ib_spec->type == IB_FLOW_SPEC_IPV4) {
2047 			has_ipv4_spec = true;
2048 		}
2049 		ib_spec = (void *)ib_spec + ib_spec->size;
2050 	}
2051 	return !has_ipv4_spec || eth_type_ipv4;
2052 }
2053 
2054 static void put_flow_table(struct mlx5_ib_dev *dev,
2055 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2056 {
2057 	prio->refcount -= !!ft_added;
2058 	if (!prio->refcount) {
2059 		mlx5_destroy_flow_table(prio->flow_table);
2060 		prio->flow_table = NULL;
2061 	}
2062 }
2063 
2064 static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2065 {
2066 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2067 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2068 							  struct mlx5_ib_flow_handler,
2069 							  ibflow);
2070 	struct mlx5_ib_flow_handler *iter, *tmp;
2071 
2072 	mutex_lock(&dev->flow_db.lock);
2073 
2074 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
2075 		mlx5_del_flow_rules(&iter->rule);
2076 		put_flow_table(dev, iter->prio, true);
2077 		list_del(&iter->list);
2078 		kfree(iter);
2079 	}
2080 
2081 	mlx5_del_flow_rules(&handler->rule);
2082 	put_flow_table(dev, handler->prio, true);
2083 	mutex_unlock(&dev->flow_db.lock);
2084 
2085 	kfree(handler);
2086 
2087 	return 0;
2088 }
2089 
2090 static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
2091 {
2092 	priority *= 2;
2093 	if (!dont_trap)
2094 		priority++;
2095 	return priority;
2096 }
2097 
2098 enum flow_table_type {
2099 	MLX5_IB_FT_RX,
2100 	MLX5_IB_FT_TX
2101 };
2102 
2103 #define MLX5_FS_MAX_TYPES	 10
2104 #define MLX5_FS_MAX_ENTRIES	 32000UL
2105 static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2106 						struct ib_flow_attr *flow_attr,
2107 						enum flow_table_type ft_type)
2108 {
2109 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2110 	struct mlx5_flow_table_attr ft_attr = {};
2111 	struct mlx5_flow_namespace *ns = NULL;
2112 	struct mlx5_ib_flow_prio *prio;
2113 	struct mlx5_flow_table *ft;
2114 	int num_entries;
2115 	int num_groups;
2116 	int priority;
2117 	int err = 0;
2118 
2119 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
2120 		if (flow_is_multicast_only(flow_attr) &&
2121 		    !dont_trap)
2122 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2123 		else
2124 			priority = ib_prio_to_core_prio(flow_attr->priority,
2125 							dont_trap);
2126 		ns = mlx5_get_flow_namespace(dev->mdev,
2127 					     MLX5_FLOW_NAMESPACE_BYPASS);
2128 		num_entries = MLX5_FS_MAX_ENTRIES;
2129 		num_groups = MLX5_FS_MAX_TYPES;
2130 		prio = &dev->flow_db.prios[priority];
2131 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2132 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2133 		ns = mlx5_get_flow_namespace(dev->mdev,
2134 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2135 		build_leftovers_ft_param("bypass", &priority,
2136 					 &num_entries,
2137 					 &num_groups);
2138 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2139 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2140 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2141 					allow_sniffer_and_nic_rx_shared_tir))
2142 			return ERR_PTR(-ENOTSUPP);
2143 
2144 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2145 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2146 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2147 
2148 		prio = &dev->flow_db.sniffer[ft_type];
2149 		priority = 0;
2150 		num_entries = 1;
2151 		num_groups = 1;
2152 	}
2153 
2154 	if (!ns)
2155 		return ERR_PTR(-ENOTSUPP);
2156 
2157 	ft = prio->flow_table;
2158 	if (!ft) {
2159 		ft_attr.prio = priority;
2160 		ft_attr.max_fte = num_entries;
2161 		ft_attr.autogroup.max_num_groups = num_groups;
2162 
2163 		ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
2164 
2165 		if (!IS_ERR(ft)) {
2166 			prio->refcount = 0;
2167 			prio->flow_table = ft;
2168 		} else {
2169 			err = PTR_ERR(ft);
2170 		}
2171 	}
2172 
2173 	return err ? ERR_PTR(err) : prio;
2174 }
2175 
2176 static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2177 						     struct mlx5_ib_flow_prio *ft_prio,
2178 						     const struct ib_flow_attr *flow_attr,
2179 						     struct mlx5_flow_destination *dst)
2180 {
2181 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2182 	struct mlx5_ib_flow_handler *handler;
2183 	struct mlx5_flow_spec *spec;
2184 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2185 	unsigned int spec_index;
2186 	struct mlx5_flow_act flow_act = {};
2187 
2188 	u32 action;
2189 	int err = 0;
2190 
2191 	if (!is_valid_attr(flow_attr))
2192 		return ERR_PTR(-EINVAL);
2193 
2194 	spec = mlx5_vzalloc(sizeof(*spec));
2195 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2196 	if (!handler || !spec) {
2197 		err = -ENOMEM;
2198 		goto free;
2199 	}
2200 
2201 	spec->flow_context.flags = FLOW_CONTEXT_HAS_TAG;
2202 	spec->flow_context.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2203 
2204 	INIT_LIST_HEAD(&handler->list);
2205 
2206 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
2207 		err = parse_flow_attr(spec->match_criteria,
2208 				      spec->match_value, ib_flow);
2209 		if (err < 0)
2210 			goto free;
2211 
2212 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2213 	}
2214 
2215 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2216 	action = dst ? MLX5_FLOW_RULE_FWD_ACTION_DEST : 0;
2217 	flow_act.action = action;
2218 	handler->rule = mlx5_add_flow_rules(ft, spec, &flow_act, dst, 1);
2219 
2220 	if (IS_ERR(handler->rule)) {
2221 		err = PTR_ERR(handler->rule);
2222 		goto free;
2223 	}
2224 
2225 	ft_prio->refcount++;
2226 	handler->prio = ft_prio;
2227 
2228 	ft_prio->flow_table = ft;
2229 free:
2230 	if (err)
2231 		kfree(handler);
2232 	kvfree(spec);
2233 	return err ? ERR_PTR(err) : handler;
2234 }
2235 
2236 static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
2237 							  struct mlx5_ib_flow_prio *ft_prio,
2238 							  struct ib_flow_attr *flow_attr,
2239 							  struct mlx5_flow_destination *dst)
2240 {
2241 	struct mlx5_ib_flow_handler *handler_dst = NULL;
2242 	struct mlx5_ib_flow_handler *handler = NULL;
2243 
2244 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
2245 	if (!IS_ERR(handler)) {
2246 		handler_dst = create_flow_rule(dev, ft_prio,
2247 					       flow_attr, dst);
2248 		if (IS_ERR(handler_dst)) {
2249 			mlx5_del_flow_rules(&handler->rule);
2250 			ft_prio->refcount--;
2251 			kfree(handler);
2252 			handler = handler_dst;
2253 		} else {
2254 			list_add(&handler_dst->list, &handler->list);
2255 		}
2256 	}
2257 
2258 	return handler;
2259 }
2260 enum {
2261 	LEFTOVERS_MC,
2262 	LEFTOVERS_UC,
2263 };
2264 
2265 static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2266 							  struct mlx5_ib_flow_prio *ft_prio,
2267 							  struct ib_flow_attr *flow_attr,
2268 							  struct mlx5_flow_destination *dst)
2269 {
2270 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2271 	struct mlx5_ib_flow_handler *handler = NULL;
2272 
2273 	static struct {
2274 		struct ib_flow_attr	flow_attr;
2275 		struct ib_flow_spec_eth eth_flow;
2276 	} leftovers_specs[] = {
2277 		[LEFTOVERS_MC] = {
2278 			.flow_attr = {
2279 				.num_of_specs = 1,
2280 				.size = sizeof(leftovers_specs[0])
2281 			},
2282 			.eth_flow = {
2283 				.type = IB_FLOW_SPEC_ETH,
2284 				.size = sizeof(struct ib_flow_spec_eth),
2285 				.mask = {.dst_mac = {0x1} },
2286 				.val =  {.dst_mac = {0x1} }
2287 			}
2288 		},
2289 		[LEFTOVERS_UC] = {
2290 			.flow_attr = {
2291 				.num_of_specs = 1,
2292 				.size = sizeof(leftovers_specs[0])
2293 			},
2294 			.eth_flow = {
2295 				.type = IB_FLOW_SPEC_ETH,
2296 				.size = sizeof(struct ib_flow_spec_eth),
2297 				.mask = {.dst_mac = {0x1} },
2298 				.val = {.dst_mac = {} }
2299 			}
2300 		}
2301 	};
2302 
2303 	handler = create_flow_rule(dev, ft_prio,
2304 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2305 				   dst);
2306 	if (!IS_ERR(handler) &&
2307 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2308 		handler_ucast = create_flow_rule(dev, ft_prio,
2309 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2310 						 dst);
2311 		if (IS_ERR(handler_ucast)) {
2312 			mlx5_del_flow_rules(&handler->rule);
2313 			ft_prio->refcount--;
2314 			kfree(handler);
2315 			handler = handler_ucast;
2316 		} else {
2317 			list_add(&handler_ucast->list, &handler->list);
2318 		}
2319 	}
2320 
2321 	return handler;
2322 }
2323 
2324 static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2325 							struct mlx5_ib_flow_prio *ft_rx,
2326 							struct mlx5_ib_flow_prio *ft_tx,
2327 							struct mlx5_flow_destination *dst)
2328 {
2329 	struct mlx5_ib_flow_handler *handler_rx;
2330 	struct mlx5_ib_flow_handler *handler_tx;
2331 	int err;
2332 	static const struct ib_flow_attr flow_attr  = {
2333 		.num_of_specs = 0,
2334 		.type = IB_FLOW_ATTR_SNIFFER,
2335 		.size = sizeof(flow_attr)
2336 	};
2337 
2338 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2339 	if (IS_ERR(handler_rx)) {
2340 		err = PTR_ERR(handler_rx);
2341 		goto err;
2342 	}
2343 
2344 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2345 	if (IS_ERR(handler_tx)) {
2346 		err = PTR_ERR(handler_tx);
2347 		goto err_tx;
2348 	}
2349 
2350 	list_add(&handler_tx->list, &handler_rx->list);
2351 
2352 	return handler_rx;
2353 
2354 err_tx:
2355 	mlx5_del_flow_rules(&handler_rx->rule);
2356 	ft_rx->refcount--;
2357 	kfree(handler_rx);
2358 err:
2359 	return ERR_PTR(err);
2360 }
2361 
2362 static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2363 					   struct ib_flow_attr *flow_attr,
2364 					   int domain,
2365 					   struct ib_udata *udata)
2366 {
2367 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2368 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2369 	struct mlx5_ib_flow_handler *handler = NULL;
2370 	struct mlx5_flow_destination *dst = NULL;
2371 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2372 	struct mlx5_ib_flow_prio *ft_prio;
2373 	struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
2374 	size_t min_ucmd_sz, required_ucmd_sz;
2375 	int err;
2376 
2377 	if (udata && udata->inlen) {
2378 		min_ucmd_sz = offsetofend(struct mlx5_ib_create_flow, reserved);
2379 		if (udata->inlen < min_ucmd_sz)
2380 			return ERR_PTR(-EOPNOTSUPP);
2381 
2382 		err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
2383 		if (err)
2384 			return ERR_PTR(err);
2385 
2386 		/* currently supports only one counters data */
2387 		if (ucmd_hdr.ncounters_data > 1)
2388 			return ERR_PTR(-EINVAL);
2389 
2390 		required_ucmd_sz = min_ucmd_sz +
2391 			sizeof(struct mlx5_ib_flow_counters_data) *
2392 			ucmd_hdr.ncounters_data;
2393 		if (udata->inlen > required_ucmd_sz &&
2394 		    !ib_is_udata_cleared(udata, required_ucmd_sz,
2395 					 udata->inlen - required_ucmd_sz))
2396 			return ERR_PTR(-EOPNOTSUPP);
2397 
2398 		ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
2399 		if (!ucmd)
2400 			return ERR_PTR(-ENOMEM);
2401 
2402 		err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
2403 		if (err)
2404 			goto free_ucmd;
2405 	}
2406 
2407 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
2408 		err = -ENOMEM;
2409 		goto free_ucmd;
2410 	}
2411 
2412 	if (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2413 		err = -EINVAL;
2414 		goto free_ucmd;
2415 	}
2416 
2417 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2418 	if (!dst) {
2419 		err = -ENOMEM;
2420 		goto free_ucmd;
2421 	}
2422 
2423 	mutex_lock(&dev->flow_db.lock);
2424 
2425 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2426 	if (IS_ERR(ft_prio)) {
2427 		err = PTR_ERR(ft_prio);
2428 		goto unlock;
2429 	}
2430 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2431 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2432 		if (IS_ERR(ft_prio_tx)) {
2433 			err = PTR_ERR(ft_prio_tx);
2434 			ft_prio_tx = NULL;
2435 			goto destroy_ft;
2436 		}
2437 	}
2438 
2439 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2440 	if (mqp->flags & MLX5_IB_QP_RSS)
2441 		dst->tir_num = mqp->rss_qp.tirn;
2442 	else
2443 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2444 
2445 	switch (flow_attr->type) {
2446 	case IB_FLOW_ATTR_NORMAL:
2447 		if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
2448 			err = -EOPNOTSUPP;
2449 			goto destroy_ft;
2450 		}
2451 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
2452 			handler = create_dont_trap_rule(dev, ft_prio, flow_attr, dst);
2453 		} else {
2454 			handler = create_flow_rule(dev, ft_prio, flow_attr, dst);
2455 		}
2456 		break;
2457 	case IB_FLOW_ATTR_ALL_DEFAULT:
2458 	case IB_FLOW_ATTR_MC_DEFAULT:
2459 		handler = create_leftovers_rule(dev, ft_prio, flow_attr, dst);
2460 		break;
2461 	case IB_FLOW_ATTR_SNIFFER:
2462 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2463 		break;
2464 	default:
2465 		err = -EINVAL;
2466 		goto destroy_ft;
2467 	}
2468 
2469 	if (IS_ERR(handler)) {
2470 		err = PTR_ERR(handler);
2471 		handler = NULL;
2472 		goto destroy_ft;
2473 	}
2474 
2475 	mutex_unlock(&dev->flow_db.lock);
2476 	kfree(dst);
2477 	kfree(ucmd);
2478 
2479 	return &handler->ibflow;
2480 
2481 destroy_ft:
2482 	put_flow_table(dev, ft_prio, false);
2483 	if (ft_prio_tx)
2484 		put_flow_table(dev, ft_prio_tx, false);
2485 unlock:
2486 	mutex_unlock(&dev->flow_db.lock);
2487 	kfree(dst);
2488 free_ucmd:
2489 	kfree(ucmd);
2490 	return ERR_PTR(err);
2491 }
2492 
2493 static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2494 {
2495 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2496 	int err;
2497 
2498 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2499 	if (err)
2500 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2501 			     ibqp->qp_num, gid->raw);
2502 
2503 	return err;
2504 }
2505 
2506 static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2507 {
2508 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2509 	int err;
2510 
2511 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2512 	if (err)
2513 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2514 			     ibqp->qp_num, gid->raw);
2515 
2516 	return err;
2517 }
2518 
2519 static int init_node_data(struct mlx5_ib_dev *dev)
2520 {
2521 	int err;
2522 
2523 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2524 	if (err)
2525 		return err;
2526 
2527 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2528 }
2529 
2530 static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2531 			     char *buf)
2532 {
2533 	struct mlx5_ib_dev *dev =
2534 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2535 
2536 	return sprintf(buf, "%lld\n", (long long)dev->mdev->priv.fw_pages);
2537 }
2538 
2539 static ssize_t show_reg_pages(struct device *device,
2540 			      struct device_attribute *attr, char *buf)
2541 {
2542 	struct mlx5_ib_dev *dev =
2543 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2544 
2545 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2546 }
2547 
2548 static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2549 			char *buf)
2550 {
2551 	struct mlx5_ib_dev *dev =
2552 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2553 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2554 }
2555 
2556 static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2557 			char *buf)
2558 {
2559 	struct mlx5_ib_dev *dev =
2560 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2561 	return sprintf(buf, "%x\n", dev->mdev->pdev->revision);
2562 }
2563 
2564 static ssize_t show_board(struct device *device, struct device_attribute *attr,
2565 			  char *buf)
2566 {
2567 	struct mlx5_ib_dev *dev =
2568 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2569 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
2570 		       dev->mdev->board_id);
2571 }
2572 
2573 static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2574 static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2575 static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2576 static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2577 static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2578 
2579 static struct device_attribute *mlx5_class_attributes[] = {
2580 	&dev_attr_hw_rev,
2581 	&dev_attr_hca_type,
2582 	&dev_attr_board_id,
2583 	&dev_attr_fw_pages,
2584 	&dev_attr_reg_pages,
2585 };
2586 
2587 static void pkey_change_handler(struct work_struct *work)
2588 {
2589 	struct mlx5_ib_port_resources *ports =
2590 		container_of(work, struct mlx5_ib_port_resources,
2591 			     pkey_change_work);
2592 
2593 	mutex_lock(&ports->devr->mutex);
2594 	mlx5_ib_gsi_pkey_change(ports->gsi);
2595 	mutex_unlock(&ports->devr->mutex);
2596 }
2597 
2598 static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
2599 {
2600 	struct mlx5_ib_qp *mqp;
2601 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
2602 	struct mlx5_core_cq *mcq;
2603 	struct list_head cq_armed_list;
2604 	unsigned long flags_qp;
2605 	unsigned long flags_cq;
2606 	unsigned long flags;
2607 
2608 	INIT_LIST_HEAD(&cq_armed_list);
2609 
2610 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
2611 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
2612 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
2613 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
2614 		if (mqp->sq.tail != mqp->sq.head) {
2615 			send_mcq = to_mcq(mqp->ibqp.send_cq);
2616 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
2617 			if (send_mcq->mcq.comp &&
2618 			    mqp->ibqp.send_cq->comp_handler) {
2619 				if (!send_mcq->mcq.reset_notify_added) {
2620 					send_mcq->mcq.reset_notify_added = 1;
2621 					list_add_tail(&send_mcq->mcq.reset_notify,
2622 						      &cq_armed_list);
2623 				}
2624 			}
2625 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
2626 		}
2627 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
2628 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
2629 		/* no handling is needed for SRQ */
2630 		if (!mqp->ibqp.srq) {
2631 			if (mqp->rq.tail != mqp->rq.head) {
2632 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
2633 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
2634 				if (recv_mcq->mcq.comp &&
2635 				    mqp->ibqp.recv_cq->comp_handler) {
2636 					if (!recv_mcq->mcq.reset_notify_added) {
2637 						recv_mcq->mcq.reset_notify_added = 1;
2638 						list_add_tail(&recv_mcq->mcq.reset_notify,
2639 							      &cq_armed_list);
2640 					}
2641 				}
2642 				spin_unlock_irqrestore(&recv_mcq->lock,
2643 						       flags_cq);
2644 			}
2645 		}
2646 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
2647 	}
2648 	/*At that point all inflight post send were put to be executed as of we
2649 	 * lock/unlock above locks Now need to arm all involved CQs.
2650 	 */
2651 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
2652 		mcq->comp(mcq, NULL);
2653 	}
2654 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
2655 }
2656 
2657 static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
2658 			  enum mlx5_dev_event event, unsigned long param)
2659 {
2660 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2661 	struct ib_event ibev;
2662 	bool fatal = false;
2663 	u8 port = (u8)param;
2664 
2665 	switch (event) {
2666 	case MLX5_DEV_EVENT_SYS_ERROR:
2667 		ibev.event = IB_EVENT_DEVICE_FATAL;
2668 		mlx5_ib_handle_internal_error(ibdev);
2669 		fatal = true;
2670 		break;
2671 
2672 	case MLX5_DEV_EVENT_PORT_UP:
2673 	case MLX5_DEV_EVENT_PORT_DOWN:
2674 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
2675 		/* In RoCE, port up/down events are handled in
2676 		 * mlx5_netdev_event().
2677 		 */
2678 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2679 			IB_LINK_LAYER_ETHERNET)
2680 			return;
2681 
2682 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
2683 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2684 		break;
2685 
2686 	case MLX5_DEV_EVENT_LID_CHANGE:
2687 		ibev.event = IB_EVENT_LID_CHANGE;
2688 		break;
2689 
2690 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2691 		ibev.event = IB_EVENT_PKEY_CHANGE;
2692 
2693 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2694 		break;
2695 
2696 	case MLX5_DEV_EVENT_GUID_CHANGE:
2697 		ibev.event = IB_EVENT_GID_CHANGE;
2698 		break;
2699 
2700 	case MLX5_DEV_EVENT_CLIENT_REREG:
2701 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
2702 		break;
2703 
2704 	default:
2705 		/* unsupported event */
2706 		return;
2707 	}
2708 
2709 	ibev.device	      = &ibdev->ib_dev;
2710 	ibev.element.port_num = port;
2711 
2712 	if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
2713 		mlx5_ib_warn(ibdev, "warning: event(%d) on port %d\n", event, port);
2714 		return;
2715 	}
2716 
2717 	if (ibdev->ib_active)
2718 		ib_dispatch_event(&ibev);
2719 
2720 	if (fatal)
2721 		ibdev->ib_active = false;
2722 }
2723 
2724 static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2725 {
2726 	int port;
2727 
2728 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
2729 		mlx5_query_ext_port_caps(dev, port);
2730 }
2731 
2732 static int get_port_caps(struct mlx5_ib_dev *dev)
2733 {
2734 	struct ib_device_attr *dprops = NULL;
2735 	struct ib_port_attr *pprops = NULL;
2736 	int err = -ENOMEM;
2737 	int port;
2738 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
2739 
2740 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
2741 	if (!pprops)
2742 		goto out;
2743 
2744 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2745 	if (!dprops)
2746 		goto out;
2747 
2748 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
2749 	if (err) {
2750 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2751 		goto out;
2752 	}
2753 
2754 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
2755 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2756 		if (err) {
2757 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
2758 				     port, err);
2759 			break;
2760 		}
2761 		dev->mdev->port_caps[port - 1].pkey_table_len =
2762 						dprops->max_pkeys;
2763 		dev->mdev->port_caps[port - 1].gid_table_len =
2764 						pprops->gid_tbl_len;
2765 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
2766 			    dprops->max_pkeys, pprops->gid_tbl_len);
2767 	}
2768 
2769 out:
2770 	kfree(pprops);
2771 	kfree(dprops);
2772 
2773 	return err;
2774 }
2775 
2776 static void destroy_umrc_res(struct mlx5_ib_dev *dev)
2777 {
2778 	int err;
2779 
2780 	err = mlx5_mr_cache_cleanup(dev);
2781 	if (err)
2782 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
2783 
2784 	if (dev->umrc.qp)
2785 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
2786 	if (dev->umrc.cq)
2787 		ib_free_cq(dev->umrc.cq);
2788 	if (dev->umrc.pd)
2789 		ib_dealloc_pd(dev->umrc.pd);
2790 }
2791 
2792 enum {
2793 	MAX_UMR_WR = 128,
2794 };
2795 
2796 static int create_umr_res(struct mlx5_ib_dev *dev)
2797 {
2798 	struct ib_qp_init_attr *init_attr = NULL;
2799 	struct ib_qp_attr *attr = NULL;
2800 	struct ib_pd *pd;
2801 	struct ib_cq *cq;
2802 	struct ib_qp *qp;
2803 	int ret;
2804 
2805 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
2806 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
2807 	if (!attr || !init_attr) {
2808 		ret = -ENOMEM;
2809 		goto error_0;
2810 	}
2811 
2812 	pd = ib_alloc_pd(&dev->ib_dev, 0);
2813 	if (IS_ERR(pd)) {
2814 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
2815 		ret = PTR_ERR(pd);
2816 		goto error_0;
2817 	}
2818 
2819 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
2820 	if (IS_ERR(cq)) {
2821 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
2822 		ret = PTR_ERR(cq);
2823 		goto error_2;
2824 	}
2825 
2826 	init_attr->send_cq = cq;
2827 	init_attr->recv_cq = cq;
2828 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
2829 	init_attr->cap.max_send_wr = MAX_UMR_WR;
2830 	init_attr->cap.max_send_sge = 1;
2831 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
2832 	init_attr->port_num = 1;
2833 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
2834 	if (IS_ERR(qp)) {
2835 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
2836 		ret = PTR_ERR(qp);
2837 		goto error_3;
2838 	}
2839 	qp->device     = &dev->ib_dev;
2840 	qp->real_qp    = qp;
2841 	qp->uobject    = NULL;
2842 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
2843 
2844 	attr->qp_state = IB_QPS_INIT;
2845 	attr->port_num = 1;
2846 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
2847 				IB_QP_PORT, NULL);
2848 	if (ret) {
2849 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
2850 		goto error_4;
2851 	}
2852 
2853 	memset(attr, 0, sizeof(*attr));
2854 	attr->qp_state = IB_QPS_RTR;
2855 	attr->path_mtu = IB_MTU_256;
2856 
2857 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2858 	if (ret) {
2859 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
2860 		goto error_4;
2861 	}
2862 
2863 	memset(attr, 0, sizeof(*attr));
2864 	attr->qp_state = IB_QPS_RTS;
2865 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
2866 	if (ret) {
2867 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
2868 		goto error_4;
2869 	}
2870 
2871 	dev->umrc.qp = qp;
2872 	dev->umrc.cq = cq;
2873 	dev->umrc.pd = pd;
2874 
2875 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
2876 	ret = mlx5_mr_cache_init(dev);
2877 	if (ret) {
2878 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
2879 		goto error_4;
2880 	}
2881 
2882 	kfree(attr);
2883 	kfree(init_attr);
2884 
2885 	return 0;
2886 
2887 error_4:
2888 	mlx5_ib_destroy_qp(qp, NULL);
2889 	dev->umrc.qp = NULL;
2890 
2891 error_3:
2892 	ib_free_cq(cq);
2893 	dev->umrc.cq = NULL;
2894 
2895 error_2:
2896 	ib_dealloc_pd(pd);
2897 	dev->umrc.pd = NULL;
2898 
2899 error_0:
2900 	kfree(attr);
2901 	kfree(init_attr);
2902 	return ret;
2903 }
2904 
2905 static int create_dev_resources(struct mlx5_ib_resources *devr)
2906 {
2907 	struct ib_srq_init_attr attr;
2908 	struct mlx5_ib_dev *dev;
2909 	struct ib_device *ibdev;
2910 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
2911 	int port;
2912 	int ret = 0;
2913 
2914 	dev = container_of(devr, struct mlx5_ib_dev, devr);
2915 	ibdev = &dev->ib_dev;
2916 
2917 	mutex_init(&devr->mutex);
2918 
2919 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
2920 	if (!devr->p0)
2921 		return -ENOMEM;
2922 
2923 	devr->p0->device  = ibdev;
2924 	devr->p0->uobject = NULL;
2925 	atomic_set(&devr->p0->usecnt, 0);
2926 
2927 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
2928 	if (ret)
2929 		goto error0;
2930 
2931 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
2932 	if (!devr->c0) {
2933 		ret = -ENOMEM;
2934 		goto error1;
2935 	}
2936 
2937 	devr->c0->device = &dev->ib_dev;
2938 	atomic_set(&devr->c0->usecnt, 0);
2939 
2940 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
2941 	if (ret)
2942 		goto err_create_cq;
2943 
2944 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
2945 	if (IS_ERR(devr->x0)) {
2946 		ret = PTR_ERR(devr->x0);
2947 		goto error2;
2948 	}
2949 	devr->x0->device = &dev->ib_dev;
2950 	devr->x0->inode = NULL;
2951 	atomic_set(&devr->x0->usecnt, 0);
2952 	mutex_init(&devr->x0->tgt_qp_mutex);
2953 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
2954 
2955 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL);
2956 	if (IS_ERR(devr->x1)) {
2957 		ret = PTR_ERR(devr->x1);
2958 		goto error3;
2959 	}
2960 	devr->x1->device = &dev->ib_dev;
2961 	devr->x1->inode = NULL;
2962 	atomic_set(&devr->x1->usecnt, 0);
2963 	mutex_init(&devr->x1->tgt_qp_mutex);
2964 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
2965 
2966 	memset(&attr, 0, sizeof(attr));
2967 	attr.attr.max_sge = 1;
2968 	attr.attr.max_wr = 1;
2969 	attr.srq_type = IB_SRQT_XRC;
2970 	attr.ext.cq = devr->c0;
2971 	attr.ext.xrc.xrcd = devr->x0;
2972 
2973 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
2974 	if (!devr->s0) {
2975 		ret = -ENOMEM;
2976 		goto error4;
2977 	}
2978 
2979 	devr->s0->device	= &dev->ib_dev;
2980 	devr->s0->pd		= devr->p0;
2981 	devr->s0->srq_type      = IB_SRQT_XRC;
2982 	devr->s0->ext.xrc.xrcd	= devr->x0;
2983 	devr->s0->ext.cq	= devr->c0;
2984 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
2985 	if (ret)
2986 		goto err_create;
2987 
2988 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
2989 	atomic_inc(&devr->s0->ext.cq->usecnt);
2990 	atomic_inc(&devr->p0->usecnt);
2991 	atomic_set(&devr->s0->usecnt, 0);
2992 
2993 	memset(&attr, 0, sizeof(attr));
2994 	attr.attr.max_sge = 1;
2995 	attr.attr.max_wr = 1;
2996 	attr.srq_type = IB_SRQT_BASIC;
2997 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
2998 	if (!devr->s1) {
2999 		ret = -ENOMEM;
3000 		goto error5;
3001 	}
3002 
3003 	devr->s1->device	= &dev->ib_dev;
3004 	devr->s1->pd		= devr->p0;
3005 	devr->s1->srq_type      = IB_SRQT_BASIC;
3006 	devr->s1->ext.cq	= devr->c0;
3007 
3008 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
3009 	if (ret)
3010 		goto error6;
3011 
3012 	atomic_inc(&devr->p0->usecnt);
3013 	atomic_set(&devr->s1->usecnt, 0);
3014 
3015 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
3016 		INIT_WORK(&devr->ports[port].pkey_change_work,
3017 			  pkey_change_handler);
3018 		devr->ports[port].devr = devr;
3019 	}
3020 
3021 	return 0;
3022 
3023 error6:
3024 	kfree(devr->s1);
3025 error5:
3026 	mlx5_ib_destroy_srq(devr->s0, NULL);
3027 err_create:
3028 	kfree(devr->s0);
3029 error4:
3030 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
3031 error3:
3032 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
3033 error2:
3034 	mlx5_ib_destroy_cq(devr->c0, NULL);
3035 err_create_cq:
3036 	kfree(devr->c0);
3037 error1:
3038 	mlx5_ib_dealloc_pd(devr->p0, NULL);
3039 error0:
3040 	kfree(devr->p0);
3041 	return ret;
3042 }
3043 
3044 static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3045 {
3046 	int port;
3047 
3048 	mlx5_ib_destroy_srq(devr->s1, NULL);
3049 	kfree(devr->s1);
3050 	mlx5_ib_destroy_srq(devr->s0, NULL);
3051 	kfree(devr->s0);
3052 	mlx5_ib_dealloc_xrcd(devr->x0, NULL);
3053 	mlx5_ib_dealloc_xrcd(devr->x1, NULL);
3054 	mlx5_ib_destroy_cq(devr->c0, NULL);
3055 	kfree(devr->c0);
3056 	mlx5_ib_dealloc_pd(devr->p0, NULL);
3057 	kfree(devr->p0);
3058 
3059 	/* Make sure no change P_Key work items are still executing */
3060 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
3061 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3062 }
3063 
3064 static u32 get_core_cap_flags(struct ib_device *ibdev)
3065 {
3066 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3067 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3068 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3069 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3070 	u32 ret = 0;
3071 
3072 	if (ll == IB_LINK_LAYER_INFINIBAND)
3073 		return RDMA_CORE_PORT_IBA_IB;
3074 
3075 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
3076 		return 0;
3077 
3078 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
3079 		return 0;
3080 
3081 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3082 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3083 
3084 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3085 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3086 
3087 	return ret;
3088 }
3089 
3090 static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
3091 			       struct ib_port_immutable *immutable)
3092 {
3093 	struct ib_port_attr attr;
3094 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3095 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
3096 	int err;
3097 
3098 	err = mlx5_ib_query_port(ibdev, port_num, &attr);
3099 	if (err)
3100 		return err;
3101 
3102 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
3103 	immutable->gid_tbl_len = attr.gid_tbl_len;
3104 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3105 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3106 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
3107 
3108 	return 0;
3109 }
3110 
3111 static void get_dev_fw_str(struct ib_device *ibdev, char *str,
3112 			   size_t str_len)
3113 {
3114 	struct mlx5_ib_dev *dev =
3115 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
3116 	snprintf(str, str_len, "%d.%d.%04d", fw_rev_maj(dev->mdev),
3117 		       fw_rev_min(dev->mdev), fw_rev_sub(dev->mdev));
3118 }
3119 
3120 static int mlx5_roce_lag_init(struct mlx5_ib_dev *dev)
3121 {
3122 	return 0;
3123 }
3124 
3125 static void mlx5_roce_lag_cleanup(struct mlx5_ib_dev *dev)
3126 {
3127 }
3128 
3129 static void mlx5_remove_roce_notifier(struct mlx5_ib_dev *dev)
3130 {
3131 	if (dev->roce.nb.notifier_call) {
3132 		unregister_netdevice_notifier(&dev->roce.nb);
3133 		dev->roce.nb.notifier_call = NULL;
3134 	}
3135 }
3136 
3137 static int
3138 mlx5_enable_roce_if_cb(if_t ifp, void *arg)
3139 {
3140 	struct mlx5_ib_dev *dev = arg;
3141 
3142 	/* check if network interface belongs to mlx5en */
3143 	if (!mlx5_netdev_match(ifp, dev->mdev, "mce"))
3144 		return (0);
3145 
3146 	write_lock(&dev->roce.netdev_lock);
3147 	dev->roce.netdev = ifp;
3148 	write_unlock(&dev->roce.netdev_lock);
3149 
3150 	return (0);
3151 }
3152 
3153 static int mlx5_enable_roce(struct mlx5_ib_dev *dev)
3154 {
3155 	struct epoch_tracker et;
3156 	VNET_ITERATOR_DECL(vnet_iter);
3157 	int err;
3158 
3159 	/* Check if mlx5en net device already exists */
3160 	VNET_LIST_RLOCK();
3161 	NET_EPOCH_ENTER(et);
3162 	VNET_FOREACH(vnet_iter) {
3163 		CURVNET_SET_QUIET(vnet_iter);
3164 		if_foreach(mlx5_enable_roce_if_cb, dev);
3165 		CURVNET_RESTORE();
3166 	}
3167 	NET_EPOCH_EXIT(et);
3168 	VNET_LIST_RUNLOCK();
3169 
3170 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3171 	err = register_netdevice_notifier(&dev->roce.nb);
3172 	if (err) {
3173 		dev->roce.nb.notifier_call = NULL;
3174 		return err;
3175 	}
3176 
3177 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3178 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3179 		if (err)
3180 			goto err_unregister_netdevice_notifier;
3181 	}
3182 
3183 	err = mlx5_roce_lag_init(dev);
3184 	if (err)
3185 		goto err_disable_roce;
3186 
3187 	return 0;
3188 
3189 err_disable_roce:
3190 	if (MLX5_CAP_GEN(dev->mdev, roce))
3191 		mlx5_nic_vport_disable_roce(dev->mdev);
3192 
3193 err_unregister_netdevice_notifier:
3194 	mlx5_remove_roce_notifier(dev);
3195 	return err;
3196 }
3197 
3198 static void mlx5_disable_roce(struct mlx5_ib_dev *dev)
3199 {
3200 	mlx5_roce_lag_cleanup(dev);
3201 	if (MLX5_CAP_GEN(dev->mdev, roce))
3202 		mlx5_nic_vport_disable_roce(dev->mdev);
3203 }
3204 
3205 static void mlx5_ib_dealloc_q_port_counter(struct mlx5_ib_dev *dev, u8 port_num)
3206 {
3207 	mlx5_vport_dealloc_q_counter(dev->mdev,
3208 				     MLX5_INTERFACE_PROTOCOL_IB,
3209 				     dev->port[port_num].q_cnt_id);
3210 	dev->port[port_num].q_cnt_id = 0;
3211 }
3212 
3213 static void mlx5_ib_dealloc_q_counters(struct mlx5_ib_dev *dev)
3214 {
3215 	unsigned int i;
3216 
3217 	for (i = 0; i < dev->num_ports; i++)
3218 		mlx5_ib_dealloc_q_port_counter(dev, i);
3219 }
3220 
3221 static int mlx5_ib_alloc_q_counters(struct mlx5_ib_dev *dev)
3222 {
3223 	int i;
3224 	int ret;
3225 
3226 	for (i = 0; i < dev->num_ports; i++) {
3227 		ret = mlx5_vport_alloc_q_counter(dev->mdev,
3228 						 MLX5_INTERFACE_PROTOCOL_IB,
3229 						 &dev->port[i].q_cnt_id);
3230 		if (ret) {
3231 			mlx5_ib_warn(dev,
3232 				     "couldn't allocate queue counter for port %d, err %d\n",
3233 				     i + 1, ret);
3234 			goto dealloc_counters;
3235 		}
3236 	}
3237 
3238 	return 0;
3239 
3240 dealloc_counters:
3241 	while (--i >= 0)
3242 		mlx5_ib_dealloc_q_port_counter(dev, i);
3243 
3244 	return ret;
3245 }
3246 
3247 static const char * const names[] = {
3248 	"rx_write_requests",
3249 	"rx_read_requests",
3250 	"rx_atomic_requests",
3251 	"out_of_buffer",
3252 	"out_of_sequence",
3253 	"duplicate_request",
3254 	"rnr_nak_retry_err",
3255 	"packet_seq_err",
3256 	"implied_nak_seq_err",
3257 	"local_ack_timeout_err",
3258 };
3259 
3260 static const size_t stats_offsets[] = {
3261 	MLX5_BYTE_OFF(query_q_counter_out, rx_write_requests),
3262 	MLX5_BYTE_OFF(query_q_counter_out, rx_read_requests),
3263 	MLX5_BYTE_OFF(query_q_counter_out, rx_atomic_requests),
3264 	MLX5_BYTE_OFF(query_q_counter_out, out_of_buffer),
3265 	MLX5_BYTE_OFF(query_q_counter_out, out_of_sequence),
3266 	MLX5_BYTE_OFF(query_q_counter_out, duplicate_request),
3267 	MLX5_BYTE_OFF(query_q_counter_out, rnr_nak_retry_err),
3268 	MLX5_BYTE_OFF(query_q_counter_out, packet_seq_err),
3269 	MLX5_BYTE_OFF(query_q_counter_out, implied_nak_seq_err),
3270 	MLX5_BYTE_OFF(query_q_counter_out, local_ack_timeout_err),
3271 };
3272 
3273 static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
3274 						    u8 port_num)
3275 {
3276 	BUILD_BUG_ON(ARRAY_SIZE(names) != ARRAY_SIZE(stats_offsets));
3277 
3278 	/* We support only per port stats */
3279 	if (port_num == 0)
3280 		return NULL;
3281 
3282 	return rdma_alloc_hw_stats_struct(names, ARRAY_SIZE(names),
3283 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
3284 }
3285 
3286 static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
3287 				struct rdma_hw_stats *stats,
3288 				u8 port, int index)
3289 {
3290 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3291 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3292 	void *out;
3293 	__be32 val;
3294 	int ret;
3295 	int i;
3296 
3297 	if (!port || !stats)
3298 		return -ENOSYS;
3299 
3300 	out = mlx5_vzalloc(outlen);
3301 	if (!out)
3302 		return -ENOMEM;
3303 
3304 	ret = mlx5_vport_query_q_counter(dev->mdev,
3305 					dev->port[port - 1].q_cnt_id, 0,
3306 					out, outlen);
3307 	if (ret)
3308 		goto free;
3309 
3310 	for (i = 0; i < ARRAY_SIZE(names); i++) {
3311 		val = *(__be32 *)(out + stats_offsets[i]);
3312 		stats->value[i] = (u64)be32_to_cpu(val);
3313 	}
3314 free:
3315 	kvfree(out);
3316 	return ARRAY_SIZE(names);
3317 }
3318 
3319 static int mlx5_ib_stage_bfreg_init(struct mlx5_ib_dev *dev)
3320 {
3321 	int err;
3322 
3323 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
3324 	if (err)
3325 		return err;
3326 
3327 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
3328 	if (err) {
3329 		mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3330 		return err;
3331 	}
3332 
3333 	err = mlx5_alloc_bfreg(dev->mdev, &dev->wc_bfreg, true, false);
3334 	if (err) {
3335 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3336 		mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3337 	}
3338 
3339 	return err;
3340 }
3341 
3342 static void mlx5_ib_stage_bfreg_cleanup(struct mlx5_ib_dev *dev)
3343 {
3344 	mlx5_free_bfreg(dev->mdev, &dev->wc_bfreg);
3345 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
3346 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
3347 }
3348 
3349 static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3350 {
3351 	struct mlx5_ib_dev *dev;
3352 	enum rdma_link_layer ll;
3353 	int port_type_cap;
3354 	int err;
3355 	int i;
3356 
3357 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3358 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3359 
3360 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3361 	if (!dev)
3362 		return NULL;
3363 
3364 	dev->mdev = mdev;
3365 
3366 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
3367 			    GFP_KERNEL);
3368 	if (!dev->port)
3369 		goto err_dealloc;
3370 
3371 	rwlock_init(&dev->roce.netdev_lock);
3372 	err = get_port_caps(dev);
3373 	if (err)
3374 		goto err_free_port;
3375 
3376 	if (mlx5_use_mad_ifc(dev))
3377 		get_ext_port_caps(dev);
3378 
3379 	MLX5_INIT_DOORBELL_LOCK(&dev->uar_lock);
3380 
3381 	mutex_init(&dev->lb_mutex);
3382 
3383 	INIT_IB_DEVICE_OPS(&dev->ib_dev.ops, mlx5, MLX5);
3384 	snprintf(dev->ib_dev.name, IB_DEVICE_NAME_MAX, "mlx5_%d", device_get_unit(mdev->pdev->dev.bsddev));
3385 	dev->ib_dev.owner		= THIS_MODULE;
3386 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3387 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3388 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3389 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3390 	dev->ib_dev.num_comp_vectors    =
3391 		dev->mdev->priv.eq_table.num_comp_vectors;
3392 	dev->ib_dev.dma_device	= &mdev->pdev->dev;
3393 
3394 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3395 	dev->ib_dev.uverbs_cmd_mask	=
3396 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3397 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3398 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3399 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3400 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
3401 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
3402 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
3403 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
3404 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
3405 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
3406 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
3407 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
3408 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
3409 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
3410 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
3411 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
3412 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
3413 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
3414 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
3415 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
3416 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
3417 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
3418 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
3419 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
3420 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
3421 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
3422 	dev->ib_dev.uverbs_ex_cmd_mask =
3423 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
3424 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
3425 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP);
3426 
3427 	dev->ib_dev.query_device	= mlx5_ib_query_device;
3428 	dev->ib_dev.query_port		= mlx5_ib_query_port;
3429 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
3430 	if (ll == IB_LINK_LAYER_ETHERNET)
3431 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
3432 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
3433 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
3434 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
3435 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
3436 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
3437 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
3438 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
3439 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
3440 	dev->ib_dev.mmap		= mlx5_ib_mmap;
3441 	dev->ib_dev.mmap_free		= mlx5_ib_mmap_free;
3442 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
3443 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
3444 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
3445 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
3446 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
3447 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
3448 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
3449 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
3450 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
3451 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
3452 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
3453 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
3454 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
3455 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
3456 	dev->ib_dev.post_send		= mlx5_ib_post_send;
3457 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
3458 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
3459 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
3460 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
3461 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
3462 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
3463 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
3464 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
3465 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
3466 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
3467 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
3468 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
3469 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
3470 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
3471 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
3472 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
3473 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
3474 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
3475 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
3476 	if (mlx5_core_is_pf(mdev)) {
3477 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
3478 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
3479 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
3480 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
3481 	}
3482 
3483 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
3484 
3485 	mlx5_ib_internal_fill_odp_caps(dev);
3486 
3487 	if (MLX5_CAP_GEN(mdev, imaicl)) {
3488 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
3489 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
3490 		dev->ib_dev.uverbs_cmd_mask |=
3491 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
3492 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
3493 	}
3494 
3495 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt) &&
3496 	    MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
3497 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
3498 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
3499 	}
3500 
3501 	if (MLX5_CAP_GEN(mdev, xrc)) {
3502 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
3503 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
3504 		dev->ib_dev.uverbs_cmd_mask |=
3505 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
3506 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
3507 	}
3508 
3509 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
3510 	    IB_LINK_LAYER_ETHERNET) {
3511 		dev->ib_dev.create_flow	= mlx5_ib_create_flow;
3512 		dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
3513 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
3514 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
3515 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
3516 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
3517 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
3518 		dev->ib_dev.uverbs_ex_cmd_mask |=
3519 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
3520 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW) |
3521 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
3522 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
3523 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
3524 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
3525 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
3526 	}
3527 	err = init_node_data(dev);
3528 	if (err)
3529 		goto err_free_port;
3530 
3531 	mutex_init(&dev->flow_db.lock);
3532 	mutex_init(&dev->cap_mask_mutex);
3533 	INIT_LIST_HEAD(&dev->qp_list);
3534 	spin_lock_init(&dev->reset_flow_resource_lock);
3535 
3536 	if (ll == IB_LINK_LAYER_ETHERNET) {
3537 		err = mlx5_enable_roce(dev);
3538 		if (err)
3539 			goto err_free_port;
3540 	}
3541 
3542 	err = create_dev_resources(&dev->devr);
3543 	if (err)
3544 		goto err_disable_roce;
3545 
3546 	err = mlx5_ib_odp_init_one(dev);
3547 	if (err)
3548 		goto err_rsrc;
3549 
3550 	err = mlx5_ib_alloc_q_counters(dev);
3551 	if (err)
3552 		goto err_odp;
3553 
3554 	err = mlx5_ib_stage_bfreg_init(dev);
3555 	if (err)
3556 		goto err_q_cnt;
3557 
3558 	err = ib_register_device(&dev->ib_dev, NULL);
3559 	if (err)
3560 		goto err_bfreg;
3561 
3562 	err = create_umr_res(dev);
3563 	if (err)
3564 		goto err_dev;
3565 
3566 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
3567 		err = device_create_file(&dev->ib_dev.dev,
3568 					 mlx5_class_attributes[i]);
3569 		if (err)
3570 			goto err_umrc;
3571 	}
3572 
3573 	err = mlx5_ib_init_congestion(dev);
3574 	if (err)
3575 		goto err_umrc;
3576 
3577 	dev->ib_active = true;
3578 
3579 	return dev;
3580 
3581 err_umrc:
3582 	destroy_umrc_res(dev);
3583 
3584 err_dev:
3585 	ib_unregister_device(&dev->ib_dev);
3586 
3587 err_bfreg:
3588 	mlx5_ib_stage_bfreg_cleanup(dev);
3589 
3590 err_q_cnt:
3591 	mlx5_ib_dealloc_q_counters(dev);
3592 
3593 err_odp:
3594 	mlx5_ib_odp_remove_one(dev);
3595 
3596 err_rsrc:
3597 	destroy_dev_resources(&dev->devr);
3598 
3599 err_disable_roce:
3600 	if (ll == IB_LINK_LAYER_ETHERNET) {
3601 		mlx5_disable_roce(dev);
3602 		mlx5_remove_roce_notifier(dev);
3603 	}
3604 
3605 err_free_port:
3606 	kfree(dev->port);
3607 
3608 err_dealloc:
3609 	ib_dealloc_device((struct ib_device *)dev);
3610 
3611 	return NULL;
3612 }
3613 
3614 static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
3615 {
3616 	struct mlx5_ib_dev *dev = context;
3617 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
3618 
3619 	mlx5_ib_cleanup_congestion(dev);
3620 	mlx5_remove_roce_notifier(dev);
3621 	ib_unregister_device(&dev->ib_dev);
3622 	mlx5_ib_stage_bfreg_cleanup(dev);
3623 	mlx5_ib_dealloc_q_counters(dev);
3624 	destroy_umrc_res(dev);
3625 	mlx5_ib_odp_remove_one(dev);
3626 	destroy_dev_resources(&dev->devr);
3627 	if (ll == IB_LINK_LAYER_ETHERNET)
3628 		mlx5_disable_roce(dev);
3629 	kfree(dev->port);
3630 	ib_dealloc_device(&dev->ib_dev);
3631 }
3632 
3633 static struct mlx5_interface mlx5_ib_interface = {
3634 	.add            = mlx5_ib_add,
3635 	.remove         = mlx5_ib_remove,
3636 	.event          = mlx5_ib_event,
3637 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
3638 };
3639 
3640 static int __init mlx5_ib_init(void)
3641 {
3642 	int err;
3643 
3644 	err = mlx5_ib_odp_init();
3645 	if (err)
3646 		return err;
3647 
3648 	err = mlx5_register_interface(&mlx5_ib_interface);
3649 	if (err)
3650 		goto clean_odp;
3651 
3652 	return err;
3653 
3654 clean_odp:
3655 	mlx5_ib_odp_cleanup();
3656 	return err;
3657 }
3658 
3659 static void __exit mlx5_ib_cleanup(void)
3660 {
3661 	mlx5_unregister_interface(&mlx5_ib_interface);
3662 	mlx5_ib_odp_cleanup();
3663 }
3664 
3665 module_init_order(mlx5_ib_init, SI_ORDER_SEVENTH);
3666 module_exit_order(mlx5_ib_cleanup, SI_ORDER_SEVENTH);
3667