xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision ad9a3668a434faca1339789ed2f043d679199309)
1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
34adec640eSChristoph Hellwig #include <linux/highmem.h>
35e126ba97SEli Cohen #include <linux/module.h>
36e126ba97SEli Cohen #include <linux/init.h>
37e126ba97SEli Cohen #include <linux/errno.h>
38e126ba97SEli Cohen #include <linux/pci.h>
39e126ba97SEli Cohen #include <linux/dma-mapping.h>
40e126ba97SEli Cohen #include <linux/slab.h>
4137aa5c36SGuy Levi #if defined(CONFIG_X86)
4237aa5c36SGuy Levi #include <asm/pat.h>
4337aa5c36SGuy Levi #endif
44e126ba97SEli Cohen #include <linux/sched.h>
456e84f315SIngo Molnar #include <linux/sched/mm.h>
460881e7bdSIngo Molnar #include <linux/sched/task.h>
477c2344c3SMaor Gottlieb #include <linux/delay.h>
48e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
493f89a643SAchiad Shochat #include <rdma/ib_addr.h>
502811ba51SAchiad Shochat #include <rdma/ib_cache.h>
51ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
521b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
537c2344c3SMaor Gottlieb #include <linux/list.h>
54e126ba97SEli Cohen #include <rdma/ib_smi.h>
55e126ba97SEli Cohen #include <rdma/ib_umem.h>
56038d2ef8SMaor Gottlieb #include <linux/in.h>
57038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
58038d2ef8SMaor Gottlieb #include <linux/mlx5/fs.h>
5978984898SOr Gerlitz #include <linux/mlx5/vport.h>
60e126ba97SEli Cohen #include "mlx5_ib.h"
61e1f24a79SParav Pandit #include "cmd.h"
62c85023e1SHuy Nguyen #include <linux/mlx5/vport.h>
63e126ba97SEli Cohen 
64e126ba97SEli Cohen #define DRIVER_NAME "mlx5_ib"
65b359911dSTariq Toukan #define DRIVER_VERSION "5.0-0"
66e126ba97SEli Cohen 
67e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
68e126ba97SEli Cohen MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
69e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
70e126ba97SEli Cohen 
71e126ba97SEli Cohen static char mlx5_version[] =
72e126ba97SEli Cohen 	DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
73b359911dSTariq Toukan 	DRIVER_VERSION "\n";
74e126ba97SEli Cohen 
75da7525d2SEran Ben Elisha enum {
76da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
77da7525d2SEran Ben Elisha };
781b5daf11SMajd Dibbiny 
791b5daf11SMajd Dibbiny static enum rdma_link_layer
80ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
811b5daf11SMajd Dibbiny {
82ebd61f68SAchiad Shochat 	switch (port_type_cap) {
831b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
841b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
851b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
861b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
871b5daf11SMajd Dibbiny 	default:
881b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
891b5daf11SMajd Dibbiny 	}
901b5daf11SMajd Dibbiny }
911b5daf11SMajd Dibbiny 
92ebd61f68SAchiad Shochat static enum rdma_link_layer
93ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
94ebd61f68SAchiad Shochat {
95ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
96ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
97ebd61f68SAchiad Shochat 
98ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
99ebd61f68SAchiad Shochat }
100ebd61f68SAchiad Shochat 
101fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
102fd65f1b8SMoni Shoua 			  u8 port_num,
103fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
104fd65f1b8SMoni Shoua {
105fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
106fd65f1b8SMoni Shoua 	int ret;
107fd65f1b8SMoni Shoua 
108fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
109fd65f1b8SMoni Shoua 	ret = mlx5_ib_query_port(ibdev, port_num, &attr);
110fd65f1b8SMoni Shoua 	if (!ret)
111fd65f1b8SMoni Shoua 		*state = attr.state;
112fd65f1b8SMoni Shoua 	return ret;
113fd65f1b8SMoni Shoua }
114fd65f1b8SMoni Shoua 
115fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
116fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
117fc24fc5eSAchiad Shochat {
118fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
119fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = container_of(this, struct mlx5_ib_dev,
120fc24fc5eSAchiad Shochat 						 roce.nb);
121fc24fc5eSAchiad Shochat 
1225ec8c83eSAviv Heller 	switch (event) {
1235ec8c83eSAviv Heller 	case NETDEV_REGISTER:
1245ec8c83eSAviv Heller 	case NETDEV_UNREGISTER:
125fc24fc5eSAchiad Shochat 		write_lock(&ibdev->roce.netdev_lock);
126fc24fc5eSAchiad Shochat 		if (ndev->dev.parent == &ibdev->mdev->pdev->dev)
1275ec8c83eSAviv Heller 			ibdev->roce.netdev = (event == NETDEV_UNREGISTER) ?
1285ec8c83eSAviv Heller 					     NULL : ndev;
129fc24fc5eSAchiad Shochat 		write_unlock(&ibdev->roce.netdev_lock);
1305ec8c83eSAviv Heller 		break;
1315ec8c83eSAviv Heller 
132fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1335ec8c83eSAviv Heller 	case NETDEV_UP:
13488621dfeSAviv Heller 	case NETDEV_DOWN: {
13588621dfeSAviv Heller 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
13688621dfeSAviv Heller 		struct net_device *upper = NULL;
13788621dfeSAviv Heller 
13888621dfeSAviv Heller 		if (lag_ndev) {
13988621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
14088621dfeSAviv Heller 			dev_put(lag_ndev);
14188621dfeSAviv Heller 		}
14288621dfeSAviv Heller 
14388621dfeSAviv Heller 		if ((upper == ndev || (!upper && ndev == ibdev->roce.netdev))
14488621dfeSAviv Heller 		    && ibdev->ib_active) {
145626bc02dSBart Van Assche 			struct ib_event ibev = { };
146fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
1475ec8c83eSAviv Heller 
148fd65f1b8SMoni Shoua 			if (get_port_state(&ibdev->ib_dev, 1, &port_state))
149fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
150fd65f1b8SMoni Shoua 
151fd65f1b8SMoni Shoua 			if (ibdev->roce.last_port_state == port_state)
152fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
153fd65f1b8SMoni Shoua 
154fd65f1b8SMoni Shoua 			ibdev->roce.last_port_state = port_state;
1555ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
156fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
157fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
158fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
159fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
160fd65f1b8SMoni Shoua 			else
161fd65f1b8SMoni Shoua 				return NOTIFY_DONE;
162fd65f1b8SMoni Shoua 
1635ec8c83eSAviv Heller 			ibev.element.port_num = 1;
1645ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
1655ec8c83eSAviv Heller 		}
1665ec8c83eSAviv Heller 		break;
16788621dfeSAviv Heller 	}
1685ec8c83eSAviv Heller 
1695ec8c83eSAviv Heller 	default:
1705ec8c83eSAviv Heller 		break;
1715ec8c83eSAviv Heller 	}
172fc24fc5eSAchiad Shochat 
173fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
174fc24fc5eSAchiad Shochat }
175fc24fc5eSAchiad Shochat 
176fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
177fc24fc5eSAchiad Shochat 					     u8 port_num)
178fc24fc5eSAchiad Shochat {
179fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
180fc24fc5eSAchiad Shochat 	struct net_device *ndev;
181fc24fc5eSAchiad Shochat 
18288621dfeSAviv Heller 	ndev = mlx5_lag_get_roce_netdev(ibdev->mdev);
18388621dfeSAviv Heller 	if (ndev)
18488621dfeSAviv Heller 		return ndev;
18588621dfeSAviv Heller 
186fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
187fc24fc5eSAchiad Shochat 	 */
188fc24fc5eSAchiad Shochat 	read_lock(&ibdev->roce.netdev_lock);
189fc24fc5eSAchiad Shochat 	ndev = ibdev->roce.netdev;
190fc24fc5eSAchiad Shochat 	if (ndev)
191fc24fc5eSAchiad Shochat 		dev_hold(ndev);
192fc24fc5eSAchiad Shochat 	read_unlock(&ibdev->roce.netdev_lock);
193fc24fc5eSAchiad Shochat 
194fc24fc5eSAchiad Shochat 	return ndev;
195fc24fc5eSAchiad Shochat }
196fc24fc5eSAchiad Shochat 
197f1b65df5SNoa Osherovich static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
198f1b65df5SNoa Osherovich 				    u8 *active_width)
199f1b65df5SNoa Osherovich {
200f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
201f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
202f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
203f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
204f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
205f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
206f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
207f1b65df5SNoa Osherovich 		break;
208f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
209f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
210f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
211f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
212f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
213f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
214f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
215f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
216f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
217f1b65df5SNoa Osherovich 		break;
218f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
219f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
220f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
221f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
222f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
223f1b65df5SNoa Osherovich 		break;
224f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
225f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
226f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
227f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
228f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
229f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
230f1b65df5SNoa Osherovich 		break;
231f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
232f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
233f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
234f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
235f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
236f1b65df5SNoa Osherovich 		break;
237f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
238f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
239f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
240f1b65df5SNoa Osherovich 		break;
241f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
242f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
243f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
244f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
245f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
246f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
247f1b65df5SNoa Osherovich 		break;
248f1b65df5SNoa Osherovich 	default:
249f1b65df5SNoa Osherovich 		return -EINVAL;
250f1b65df5SNoa Osherovich 	}
251f1b65df5SNoa Osherovich 
252f1b65df5SNoa Osherovich 	return 0;
253f1b65df5SNoa Osherovich }
254f1b65df5SNoa Osherovich 
255095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
2563f89a643SAchiad Shochat 				struct ib_port_attr *props)
2573f89a643SAchiad Shochat {
2583f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
259f1b65df5SNoa Osherovich 	struct mlx5_core_dev *mdev = dev->mdev;
26088621dfeSAviv Heller 	struct net_device *ndev, *upper;
2613f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
262c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
263f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
264095b0927SIlan Tayari 	int err;
2653f89a643SAchiad Shochat 
266f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
267f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
26850f22fd8SNoa Osherovich 	 */
269095b0927SIlan Tayari 	err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper, port_num);
270095b0927SIlan Tayari 	if (err)
271095b0927SIlan Tayari 		return err;
272f1b65df5SNoa Osherovich 
273f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
274f1b65df5SNoa Osherovich 				 &props->active_width);
2753f89a643SAchiad Shochat 
2763f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_CM_SUP;
2773f89a643SAchiad Shochat 	props->port_cap_flags  |= IB_PORT_IP_BASED_GIDS;
2783f89a643SAchiad Shochat 
2793f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
2803f89a643SAchiad Shochat 						roce_address_table_size);
2813f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
2823f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
2833f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
2843f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
2853f89a643SAchiad Shochat 	props->phys_state       = 3;
2863f89a643SAchiad Shochat 
287c876a1b7SLeon Romanovsky 	mlx5_query_nic_vport_qkey_viol_cntr(dev->mdev, &qkey_viol_cntr);
288c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
2893f89a643SAchiad Shochat 
2903f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
2913f89a643SAchiad Shochat 	if (!ndev)
292095b0927SIlan Tayari 		return 0;
2933f89a643SAchiad Shochat 
29488621dfeSAviv Heller 	if (mlx5_lag_is_active(dev->mdev)) {
29588621dfeSAviv Heller 		rcu_read_lock();
29688621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
29788621dfeSAviv Heller 		if (upper) {
29888621dfeSAviv Heller 			dev_put(ndev);
29988621dfeSAviv Heller 			ndev = upper;
30088621dfeSAviv Heller 			dev_hold(ndev);
30188621dfeSAviv Heller 		}
30288621dfeSAviv Heller 		rcu_read_unlock();
30388621dfeSAviv Heller 	}
30488621dfeSAviv Heller 
3053f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
3063f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
3073f89a643SAchiad Shochat 		props->phys_state = 5;
3083f89a643SAchiad Shochat 	}
3093f89a643SAchiad Shochat 
3103f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
3113f89a643SAchiad Shochat 
3123f89a643SAchiad Shochat 	dev_put(ndev);
3133f89a643SAchiad Shochat 
3143f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
315095b0927SIlan Tayari 	return 0;
3163f89a643SAchiad Shochat }
3173f89a643SAchiad Shochat 
318095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
319095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
320095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
3213cca2606SAchiad Shochat {
322095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
323095b0927SIlan Tayari 	u8 roce_version = 0;
324095b0927SIlan Tayari 	u8 roce_l3_type = 0;
325095b0927SIlan Tayari 	bool vlan = false;
326095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
327095b0927SIlan Tayari 	u16 vlan_id = 0;
3283cca2606SAchiad Shochat 
329095b0927SIlan Tayari 	if (gid) {
330095b0927SIlan Tayari 		gid_type = attr->gid_type;
331095b0927SIlan Tayari 		ether_addr_copy(mac, attr->ndev->dev_addr);
3323cca2606SAchiad Shochat 
3333cca2606SAchiad Shochat 		if (is_vlan_dev(attr->ndev)) {
334095b0927SIlan Tayari 			vlan = true;
335095b0927SIlan Tayari 			vlan_id = vlan_dev_vlan_id(attr->ndev);
336095b0927SIlan Tayari 		}
3373cca2606SAchiad Shochat 	}
3383cca2606SAchiad Shochat 
339095b0927SIlan Tayari 	switch (gid_type) {
3403cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
341095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
3423cca2606SAchiad Shochat 		break;
3433cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
344095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
345095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
346095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
347095b0927SIlan Tayari 		else
348095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
3493cca2606SAchiad Shochat 		break;
3503cca2606SAchiad Shochat 
3513cca2606SAchiad Shochat 	default:
352095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
3533cca2606SAchiad Shochat 	}
3543cca2606SAchiad Shochat 
355095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
356095b0927SIlan Tayari 				      roce_l3_type, gid->raw, mac, vlan,
357095b0927SIlan Tayari 				      vlan_id);
3583cca2606SAchiad Shochat }
3593cca2606SAchiad Shochat 
3603cca2606SAchiad Shochat static int mlx5_ib_add_gid(struct ib_device *device, u8 port_num,
3613cca2606SAchiad Shochat 			   unsigned int index, const union ib_gid *gid,
3623cca2606SAchiad Shochat 			   const struct ib_gid_attr *attr,
3633cca2606SAchiad Shochat 			   __always_unused void **context)
3643cca2606SAchiad Shochat {
365095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, gid, attr);
3663cca2606SAchiad Shochat }
3673cca2606SAchiad Shochat 
3683cca2606SAchiad Shochat static int mlx5_ib_del_gid(struct ib_device *device, u8 port_num,
3693cca2606SAchiad Shochat 			   unsigned int index, __always_unused void **context)
3703cca2606SAchiad Shochat {
371095b0927SIlan Tayari 	return set_roce_addr(to_mdev(device), port_num, index, NULL, NULL);
3723cca2606SAchiad Shochat }
3733cca2606SAchiad Shochat 
3742811ba51SAchiad Shochat __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev, u8 port_num,
3752811ba51SAchiad Shochat 			       int index)
3762811ba51SAchiad Shochat {
3772811ba51SAchiad Shochat 	struct ib_gid_attr attr;
3782811ba51SAchiad Shochat 	union ib_gid gid;
3792811ba51SAchiad Shochat 
3802811ba51SAchiad Shochat 	if (ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr))
3812811ba51SAchiad Shochat 		return 0;
3822811ba51SAchiad Shochat 
3832811ba51SAchiad Shochat 	if (!attr.ndev)
3842811ba51SAchiad Shochat 		return 0;
3852811ba51SAchiad Shochat 
3862811ba51SAchiad Shochat 	dev_put(attr.ndev);
3872811ba51SAchiad Shochat 
3882811ba51SAchiad Shochat 	if (attr.gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
3892811ba51SAchiad Shochat 		return 0;
3902811ba51SAchiad Shochat 
3912811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
3922811ba51SAchiad Shochat }
3932811ba51SAchiad Shochat 
394ed88451eSMajd Dibbiny int mlx5_get_roce_gid_type(struct mlx5_ib_dev *dev, u8 port_num,
395ed88451eSMajd Dibbiny 			   int index, enum ib_gid_type *gid_type)
396ed88451eSMajd Dibbiny {
397ed88451eSMajd Dibbiny 	struct ib_gid_attr attr;
398ed88451eSMajd Dibbiny 	union ib_gid gid;
399ed88451eSMajd Dibbiny 	int ret;
400ed88451eSMajd Dibbiny 
401ed88451eSMajd Dibbiny 	ret = ib_get_cached_gid(&dev->ib_dev, port_num, index, &gid, &attr);
402ed88451eSMajd Dibbiny 	if (ret)
403ed88451eSMajd Dibbiny 		return ret;
404ed88451eSMajd Dibbiny 
405ed88451eSMajd Dibbiny 	if (!attr.ndev)
406ed88451eSMajd Dibbiny 		return -ENODEV;
407ed88451eSMajd Dibbiny 
408ed88451eSMajd Dibbiny 	dev_put(attr.ndev);
409ed88451eSMajd Dibbiny 
410ed88451eSMajd Dibbiny 	*gid_type = attr.gid_type;
411ed88451eSMajd Dibbiny 
412ed88451eSMajd Dibbiny 	return 0;
413ed88451eSMajd Dibbiny }
414ed88451eSMajd Dibbiny 
4151b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
4161b5daf11SMajd Dibbiny {
4177fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
418d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
4197fae6655SNoa Osherovich 	return 0;
4201b5daf11SMajd Dibbiny }
4211b5daf11SMajd Dibbiny 
4221b5daf11SMajd Dibbiny enum {
4231b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
4241b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
4251b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
4261b5daf11SMajd Dibbiny };
4271b5daf11SMajd Dibbiny 
4281b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
4291b5daf11SMajd Dibbiny {
4301b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
4311b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
4321b5daf11SMajd Dibbiny 
433ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
4341b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
4351b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
4361b5daf11SMajd Dibbiny 
4371b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
4381b5daf11SMajd Dibbiny }
4391b5daf11SMajd Dibbiny 
440da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
441da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
442da7525d2SEran Ben Elisha {
443da7525d2SEran Ben Elisha 	u8 tmp;
444da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
445da7525d2SEran Ben Elisha 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
446da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
447bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
448da7525d2SEran Ben Elisha 
449da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
450da7525d2SEran Ben Elisha 	 * of host endianness respond
451da7525d2SEran Ben Elisha 	 */
452da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
453da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
454da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
455da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
456da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
457da7525d2SEran Ben Elisha 	} else {
458da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
459da7525d2SEran Ben Elisha 	}
460da7525d2SEran Ben Elisha }
461da7525d2SEran Ben Elisha 
4621b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
4631b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
4641b5daf11SMajd Dibbiny {
4651b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4661b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4671b5daf11SMajd Dibbiny 	u64 tmp;
4681b5daf11SMajd Dibbiny 	int err;
4691b5daf11SMajd Dibbiny 
4701b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
4711b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
4721b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
4731b5daf11SMajd Dibbiny 							    sys_image_guid);
4741b5daf11SMajd Dibbiny 
4751b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
4761b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
4773f89a643SAchiad Shochat 		break;
4783f89a643SAchiad Shochat 
4793f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
4803f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
4813f89a643SAchiad Shochat 		break;
4821b5daf11SMajd Dibbiny 
4831b5daf11SMajd Dibbiny 	default:
4841b5daf11SMajd Dibbiny 		return -EINVAL;
4851b5daf11SMajd Dibbiny 	}
4863f89a643SAchiad Shochat 
4873f89a643SAchiad Shochat 	if (!err)
4883f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
4893f89a643SAchiad Shochat 
4903f89a643SAchiad Shochat 	return err;
4913f89a643SAchiad Shochat 
4921b5daf11SMajd Dibbiny }
4931b5daf11SMajd Dibbiny 
4941b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
4951b5daf11SMajd Dibbiny 				u16 *max_pkeys)
4961b5daf11SMajd Dibbiny {
4971b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
4981b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
4991b5daf11SMajd Dibbiny 
5001b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5011b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5021b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
5031b5daf11SMajd Dibbiny 
5041b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5051b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5061b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
5071b5daf11SMajd Dibbiny 						pkey_table_size));
5081b5daf11SMajd Dibbiny 		return 0;
5091b5daf11SMajd Dibbiny 
5101b5daf11SMajd Dibbiny 	default:
5111b5daf11SMajd Dibbiny 		return -EINVAL;
5121b5daf11SMajd Dibbiny 	}
5131b5daf11SMajd Dibbiny }
5141b5daf11SMajd Dibbiny 
5151b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
5161b5daf11SMajd Dibbiny 				u32 *vendor_id)
5171b5daf11SMajd Dibbiny {
5181b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
5191b5daf11SMajd Dibbiny 
5201b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
5211b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5221b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
5231b5daf11SMajd Dibbiny 
5241b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5251b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5261b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
5271b5daf11SMajd Dibbiny 
5281b5daf11SMajd Dibbiny 	default:
5291b5daf11SMajd Dibbiny 		return -EINVAL;
5301b5daf11SMajd Dibbiny 	}
5311b5daf11SMajd Dibbiny }
5321b5daf11SMajd Dibbiny 
5331b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
5341b5daf11SMajd Dibbiny 				__be64 *node_guid)
5351b5daf11SMajd Dibbiny {
5361b5daf11SMajd Dibbiny 	u64 tmp;
5371b5daf11SMajd Dibbiny 	int err;
5381b5daf11SMajd Dibbiny 
5391b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
5401b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
5411b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
5421b5daf11SMajd Dibbiny 
5431b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
5441b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
5453f89a643SAchiad Shochat 		break;
5463f89a643SAchiad Shochat 
5473f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
5483f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
5493f89a643SAchiad Shochat 		break;
5501b5daf11SMajd Dibbiny 
5511b5daf11SMajd Dibbiny 	default:
5521b5daf11SMajd Dibbiny 		return -EINVAL;
5531b5daf11SMajd Dibbiny 	}
5543f89a643SAchiad Shochat 
5553f89a643SAchiad Shochat 	if (!err)
5563f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
5573f89a643SAchiad Shochat 
5583f89a643SAchiad Shochat 	return err;
5591b5daf11SMajd Dibbiny }
5601b5daf11SMajd Dibbiny 
5611b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
562bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
5631b5daf11SMajd Dibbiny };
5641b5daf11SMajd Dibbiny 
5651b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
5661b5daf11SMajd Dibbiny {
5671b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
5681b5daf11SMajd Dibbiny 
5691b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
5701b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
5711b5daf11SMajd Dibbiny 
5721b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
5731b5daf11SMajd Dibbiny 
5741b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
5751b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
5761b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
5771b5daf11SMajd Dibbiny }
5781b5daf11SMajd Dibbiny 
579e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
5802528e33eSMatan Barak 				struct ib_device_attr *props,
5812528e33eSMatan Barak 				struct ib_udata *uhw)
582e126ba97SEli Cohen {
583e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
584938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
585e126ba97SEli Cohen 	int err = -ENOMEM;
586288c01b7SEli Cohen 	int max_sq_desc;
587e126ba97SEli Cohen 	int max_rq_sg;
588e126ba97SEli Cohen 	int max_sq_sg;
589e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
590402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
591402ca536SBodong Wang 	size_t resp_len;
592402ca536SBodong Wang 	u64 max_tso;
593e126ba97SEli Cohen 
594402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
595402ca536SBodong Wang 	if (uhw->outlen && uhw->outlen < resp_len)
596402ca536SBodong Wang 		return -EINVAL;
597402ca536SBodong Wang 	else
598402ca536SBodong Wang 		resp.response_length = resp_len;
599402ca536SBodong Wang 
600402ca536SBodong Wang 	if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
6012528e33eSMatan Barak 		return -EINVAL;
6022528e33eSMatan Barak 
603e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
6041b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
6051b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
6061b5daf11SMajd Dibbiny 	if (err)
6071b5daf11SMajd Dibbiny 		return err;
6081b5daf11SMajd Dibbiny 
6091b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
6101b5daf11SMajd Dibbiny 	if (err)
6111b5daf11SMajd Dibbiny 		return err;
6121b5daf11SMajd Dibbiny 
6131b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
6141b5daf11SMajd Dibbiny 	if (err)
6151b5daf11SMajd Dibbiny 		return err;
616e126ba97SEli Cohen 
6179603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
6189603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
6199603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
620e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
621e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
622e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
6231a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
624938fe83cSSaeed Mahameed 
625938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
626e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
627938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
628e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
629938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
630e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
631938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
632e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
633d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
634d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
635d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
636d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
637b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
638b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
639d2370e0aSMatan Barak 	}
640e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
641938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
6422dea9094SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
6432dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
6442dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
6452dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
6462dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
6472dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
6482dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
6492dea9094SSagi Grimberg 	}
650938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
651f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
652e126ba97SEli Cohen 
653402ca536SBodong Wang 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads)) {
654e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
655e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
65688115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
657e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
658e8161334SNoa Osherovich 		}
659e8161334SNoa Osherovich 
660e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
661e8161334SNoa Osherovich 			props->raw_packet_caps |=
662e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
66388115fe7SBodong Wang 
664402ca536SBodong Wang 		if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
665402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
666402ca536SBodong Wang 			if (max_tso) {
667402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
668402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
669402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
670402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
671402ca536SBodong Wang 			}
672402ca536SBodong Wang 		}
67331f69a82SYishai Hadas 
67431f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
67531f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
67631f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
67731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
67831f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
67931f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
68031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
68131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
68231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
68331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
68431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
68531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_UDP;
68631f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
68731f69a82SYishai Hadas 		}
68831f69a82SYishai Hadas 	} else {
68931f69a82SYishai Hadas 		if (field_avail(typeof(resp), tso_caps, uhw->outlen))
69031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
69131f69a82SYishai Hadas 		if (field_avail(typeof(resp), rss_caps, uhw->outlen))
69231f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
693402ca536SBodong Wang 	}
694402ca536SBodong Wang 
695f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
696f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
697f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
698f0313965SErez Shitrit 	}
699f0313965SErez Shitrit 
70003404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
70103404e8aSMaor Gottlieb 	    MLX5_CAP_GEN(dev->mdev, general_notification_event))
70203404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
70303404e8aSMaor Gottlieb 
7041d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
7051d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
7061d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
7071d54f890SYishai Hadas 
708cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
709e8161334SNoa Osherovich 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs)) {
710e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
711cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
712e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
713e8161334SNoa Osherovich 	}
714cff5a0f3SMajd Dibbiny 
715da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
716da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
717da6d6ba3SMaor Gottlieb 
718b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
719b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
720b1383aa6SNoa Osherovich 
7211b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
7221b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
723e126ba97SEli Cohen 
724e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
725e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
726938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
727938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
728938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
729938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
730288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
731288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
732288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
733e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
734e126ba97SEli Cohen 	props->max_sge = min(max_rq_sg, max_sq_sg);
735986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
736938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
7379f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
738938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
739938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
740938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
741938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
742938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
743938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
744938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
745e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
746e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
747911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
748911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
749da7525d2SEran Ben Elisha 	get_atomic_caps(dev, props);
75081bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
751938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
752938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
753e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
754e126ba97SEli Cohen 					   props->max_mcast_grp;
755e126ba97SEli Cohen 	props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
75686695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
7577c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
7587c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
759e126ba97SEli Cohen 
7608cdd312cSHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
761938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pg))
7628cdd312cSHaggai Eran 		props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
7638cdd312cSHaggai Eran 	props->odp_caps = dev->odp_caps;
7648cdd312cSHaggai Eran #endif
7658cdd312cSHaggai Eran 
766051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
767051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
768051f2630SLeon Romanovsky 
769eff901d3SEli Cohen 	if (!mlx5_core_is_pf(mdev))
770eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
771eff901d3SEli Cohen 
77231f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
77331f69a82SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
77431f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
77531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
77631f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
77731f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
77831f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
77931f69a82SYishai Hadas 		props->max_wq_type_rq =
78031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
78131f69a82SYishai Hadas 	}
78231f69a82SYishai Hadas 
783eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
78478b1beb0SLeon Romanovsky 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
78578b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
786eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
78778b1beb0SLeon Romanovsky 		props->tm_caps.flags = IB_TM_CAP_RC;
78878b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
789eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
79078b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
791eb761894SArtemy Kovalyov 	}
792eb761894SArtemy Kovalyov 
79387ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
79487ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
79587ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
79687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
79787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
79887ab3f52SYonatan Cohen 	}
79987ab3f52SYonatan Cohen 
8007e43a2a5SBodong Wang 	if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
8017e43a2a5SBodong Wang 		resp.cqe_comp_caps.max_num =
8027e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression) ?
8037e43a2a5SBodong Wang 			MLX5_CAP_GEN(dev->mdev, cqe_compression_max_num) : 0;
8047e43a2a5SBodong Wang 		resp.cqe_comp_caps.supported_format =
8057e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_HASH |
8067e43a2a5SBodong Wang 			MLX5_IB_CQE_RES_FORMAT_CSUM;
8077e43a2a5SBodong Wang 		resp.response_length += sizeof(resp.cqe_comp_caps);
8087e43a2a5SBodong Wang 	}
8097e43a2a5SBodong Wang 
810d949167dSBodong Wang 	if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen)) {
811d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
812d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
813d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
814d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
815d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
816d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
817d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
818d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
819d949167dSBodong Wang 		}
820d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
821d949167dSBodong Wang 	}
822d949167dSBodong Wang 
8239f885201SLeon Romanovsky 	if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
8249f885201SLeon Romanovsky 			uhw->outlen)) {
825795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
8269f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
827795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
828050da902SBodong Wang 
829050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
830050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
831050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
832050da902SBodong Wang 
8339f885201SLeon Romanovsky 		resp.response_length +=
8349f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
8359f885201SLeon Romanovsky 	}
8369f885201SLeon Romanovsky 
837de57f2adSGuy Levi 	if (field_avail(typeof(resp), flags, uhw->outlen)) {
838de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
8397a0c8f42SGuy Levi 
840de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
841de57f2adSGuy Levi 			resp.flags |=
842de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
8437a0c8f42SGuy Levi 
8447a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
8457a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
846de57f2adSGuy Levi 	}
8479f885201SLeon Romanovsky 
84896dc3fc5SNoa Osherovich 	if (field_avail(typeof(resp), sw_parsing_caps,
84996dc3fc5SNoa Osherovich 			uhw->outlen)) {
85096dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
85196dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
85296dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
85396dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
85496dc3fc5SNoa Osherovich 
85596dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
85696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
85796dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
85896dc3fc5SNoa Osherovich 
85996dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
86096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
86196dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
86296dc3fc5SNoa Osherovich 
86396dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
86496dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
86596dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
86696dc3fc5SNoa Osherovich 		}
86796dc3fc5SNoa Osherovich 	}
86896dc3fc5SNoa Osherovich 
869b4f34597SNoa Osherovich 	if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen)) {
870b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
871b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
872b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
873b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
874b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
875b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
876b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
877b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
878b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
879b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
880b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
881b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
882b4f34597SNoa Osherovich 		}
883b4f34597SNoa Osherovich 	}
884b4f34597SNoa Osherovich 
885f95ef6cbSMaor Gottlieb 	if (field_avail(typeof(resp), tunnel_offloads_caps,
886f95ef6cbSMaor Gottlieb 			uhw->outlen)) {
887f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
888f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
889f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
890f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
891f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
892f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
893f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
894f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
895f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
896f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
897f95ef6cbSMaor Gottlieb 	}
898f95ef6cbSMaor Gottlieb 
899402ca536SBodong Wang 	if (uhw->outlen) {
900402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
901402ca536SBodong Wang 
902402ca536SBodong Wang 		if (err)
903402ca536SBodong Wang 			return err;
904402ca536SBodong Wang 	}
905402ca536SBodong Wang 
9061b5daf11SMajd Dibbiny 	return 0;
9071b5daf11SMajd Dibbiny }
908e126ba97SEli Cohen 
9091b5daf11SMajd Dibbiny enum mlx5_ib_width {
9101b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
9111b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
9121b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
9131b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
9141b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
9151b5daf11SMajd Dibbiny };
9161b5daf11SMajd Dibbiny 
9171b5daf11SMajd Dibbiny static int translate_active_width(struct ib_device *ibdev, u8 active_width,
9181b5daf11SMajd Dibbiny 				  u8 *ib_width)
9191b5daf11SMajd Dibbiny {
9201b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
9211b5daf11SMajd Dibbiny 	int err = 0;
9221b5daf11SMajd Dibbiny 
9231b5daf11SMajd Dibbiny 	if (active_width & MLX5_IB_WIDTH_1X) {
9241b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
9251b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_2X) {
9261b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "active_width %d is not supported by IB spec\n",
9271b5daf11SMajd Dibbiny 			    (int)active_width);
9281b5daf11SMajd Dibbiny 		err = -EINVAL;
9291b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_4X) {
9301b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
9311b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_8X) {
9321b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
9331b5daf11SMajd Dibbiny 	} else if (active_width & MLX5_IB_WIDTH_12X) {
9341b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
9351b5daf11SMajd Dibbiny 	} else {
9361b5daf11SMajd Dibbiny 		mlx5_ib_dbg(dev, "Invalid active_width %d\n",
9371b5daf11SMajd Dibbiny 			    (int)active_width);
9381b5daf11SMajd Dibbiny 		err = -EINVAL;
9391b5daf11SMajd Dibbiny 	}
9401b5daf11SMajd Dibbiny 
9411b5daf11SMajd Dibbiny 	return err;
9421b5daf11SMajd Dibbiny }
9431b5daf11SMajd Dibbiny 
9441b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
9451b5daf11SMajd Dibbiny {
9461b5daf11SMajd Dibbiny 	switch (mtu) {
9471b5daf11SMajd Dibbiny 	case 256: return 1;
9481b5daf11SMajd Dibbiny 	case 512: return 2;
9491b5daf11SMajd Dibbiny 	case 1024: return 3;
9501b5daf11SMajd Dibbiny 	case 2048: return 4;
9511b5daf11SMajd Dibbiny 	case 4096: return 5;
9521b5daf11SMajd Dibbiny 	default:
9531b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
9541b5daf11SMajd Dibbiny 		return -1;
9551b5daf11SMajd Dibbiny 	}
9561b5daf11SMajd Dibbiny }
9571b5daf11SMajd Dibbiny 
9581b5daf11SMajd Dibbiny enum ib_max_vl_num {
9591b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
9601b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
9611b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
9621b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
9631b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
9641b5daf11SMajd Dibbiny };
9651b5daf11SMajd Dibbiny 
9661b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
9671b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
9681b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
9691b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
9701b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
9711b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
9721b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
9731b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
9741b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
9751b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
9761b5daf11SMajd Dibbiny };
9771b5daf11SMajd Dibbiny 
9781b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
9791b5daf11SMajd Dibbiny 				u8 *max_vl_num)
9801b5daf11SMajd Dibbiny {
9811b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
9821b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
9831b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
9841b5daf11SMajd Dibbiny 		break;
9851b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
9861b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
9871b5daf11SMajd Dibbiny 		break;
9881b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
9891b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
9901b5daf11SMajd Dibbiny 		break;
9911b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
9921b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
9931b5daf11SMajd Dibbiny 		break;
9941b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
9951b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
9961b5daf11SMajd Dibbiny 		break;
9971b5daf11SMajd Dibbiny 
9981b5daf11SMajd Dibbiny 	default:
9991b5daf11SMajd Dibbiny 		return -EINVAL;
10001b5daf11SMajd Dibbiny 	}
10011b5daf11SMajd Dibbiny 
10021b5daf11SMajd Dibbiny 	return 0;
10031b5daf11SMajd Dibbiny }
10041b5daf11SMajd Dibbiny 
10051b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
10061b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
10071b5daf11SMajd Dibbiny {
10081b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
10091b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
10101b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1011046339eaSSaeed Mahameed 	u16 max_mtu;
1012046339eaSSaeed Mahameed 	u16 oper_mtu;
10131b5daf11SMajd Dibbiny 	int err;
10141b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
10151b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
10161b5daf11SMajd Dibbiny 
10171b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
10181b5daf11SMajd Dibbiny 	if (!rep) {
10191b5daf11SMajd Dibbiny 		err = -ENOMEM;
10201b5daf11SMajd Dibbiny 		goto out;
10211b5daf11SMajd Dibbiny 	}
10221b5daf11SMajd Dibbiny 
1023c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
10241b5daf11SMajd Dibbiny 
10251b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
10261b5daf11SMajd Dibbiny 	if (err)
10271b5daf11SMajd Dibbiny 		goto out;
10281b5daf11SMajd Dibbiny 
10291b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
10301b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
10311b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
10321b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
10331b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
10341b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
10351b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
10361b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
10371b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
10381b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
10391b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
10401b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
10411b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
10421b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
1043eff901d3SEli Cohen 	props->grh_required	= rep->grh_required;
10441b5daf11SMajd Dibbiny 
10451b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
10461b5daf11SMajd Dibbiny 	if (err)
10471b5daf11SMajd Dibbiny 		goto out;
10481b5daf11SMajd Dibbiny 
10491b5daf11SMajd Dibbiny 	err = translate_active_width(ibdev, ib_link_width_oper,
10501b5daf11SMajd Dibbiny 				     &props->active_width);
10511b5daf11SMajd Dibbiny 	if (err)
10521b5daf11SMajd Dibbiny 		goto out;
1053d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
10541b5daf11SMajd Dibbiny 	if (err)
10551b5daf11SMajd Dibbiny 		goto out;
10561b5daf11SMajd Dibbiny 
1057facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
10581b5daf11SMajd Dibbiny 
10591b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
10601b5daf11SMajd Dibbiny 
1061facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
10621b5daf11SMajd Dibbiny 
10631b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
10641b5daf11SMajd Dibbiny 
10651b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
10661b5daf11SMajd Dibbiny 	if (err)
10671b5daf11SMajd Dibbiny 		goto out;
10681b5daf11SMajd Dibbiny 
10691b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
10701b5daf11SMajd Dibbiny 				   &props->max_vl_num);
10711b5daf11SMajd Dibbiny out:
10721b5daf11SMajd Dibbiny 	kfree(rep);
1073e126ba97SEli Cohen 	return err;
1074e126ba97SEli Cohen }
1075e126ba97SEli Cohen 
1076e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1077e126ba97SEli Cohen 		       struct ib_port_attr *props)
1078e126ba97SEli Cohen {
1079095b0927SIlan Tayari 	unsigned int count;
1080095b0927SIlan Tayari 	int ret;
1081095b0927SIlan Tayari 
10821b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
10831b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1084095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1085095b0927SIlan Tayari 		break;
1086e126ba97SEli Cohen 
10871b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1088095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1089095b0927SIlan Tayari 		break;
10901b5daf11SMajd Dibbiny 
10913f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1092095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1093095b0927SIlan Tayari 		break;
10943f89a643SAchiad Shochat 
10951b5daf11SMajd Dibbiny 	default:
1096095b0927SIlan Tayari 		ret = -EINVAL;
1097e126ba97SEli Cohen 	}
1098095b0927SIlan Tayari 
1099095b0927SIlan Tayari 	if (!ret && props) {
1100095b0927SIlan Tayari 		count = mlx5_core_reserved_gids_count(to_mdev(ibdev)->mdev);
1101095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1102095b0927SIlan Tayari 	}
1103095b0927SIlan Tayari 	return ret;
1104e126ba97SEli Cohen }
1105e126ba97SEli Cohen 
1106e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1107e126ba97SEli Cohen 			     union ib_gid *gid)
1108e126ba97SEli Cohen {
11091b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11101b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1111e126ba97SEli Cohen 
11121b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11131b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11141b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1115e126ba97SEli Cohen 
11161b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11171b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1118e126ba97SEli Cohen 
11191b5daf11SMajd Dibbiny 	default:
11201b5daf11SMajd Dibbiny 		return -EINVAL;
11211b5daf11SMajd Dibbiny 	}
1122e126ba97SEli Cohen 
1123e126ba97SEli Cohen }
1124e126ba97SEli Cohen 
1125e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1126e126ba97SEli Cohen 			      u16 *pkey)
1127e126ba97SEli Cohen {
11281b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11291b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1130e126ba97SEli Cohen 
11311b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
11321b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
11331b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1134e126ba97SEli Cohen 
11351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
11361b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
11371b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_pkey(mdev, 0, port,  0, index,
11381b5daf11SMajd Dibbiny 						 pkey);
11391b5daf11SMajd Dibbiny 	default:
11401b5daf11SMajd Dibbiny 		return -EINVAL;
1141e126ba97SEli Cohen 	}
11421b5daf11SMajd Dibbiny }
1143e126ba97SEli Cohen 
1144e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1145e126ba97SEli Cohen 				 struct ib_device_modify *props)
1146e126ba97SEli Cohen {
1147e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1148e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1149e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1150e126ba97SEli Cohen 	int err;
1151e126ba97SEli Cohen 
1152e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1153e126ba97SEli Cohen 		return -EOPNOTSUPP;
1154e126ba97SEli Cohen 
1155e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1156e126ba97SEli Cohen 		return 0;
1157e126ba97SEli Cohen 
1158e126ba97SEli Cohen 	/*
1159e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1160e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1161e126ba97SEli Cohen 	 */
1162bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
11639603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1164e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1165e126ba97SEli Cohen 	if (err)
1166e126ba97SEli Cohen 		return err;
1167e126ba97SEli Cohen 
1168bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1169e126ba97SEli Cohen 
1170e126ba97SEli Cohen 	return err;
1171e126ba97SEli Cohen }
1172e126ba97SEli Cohen 
1173cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1174cdbe33d0SEli Cohen 				u32 value)
1175cdbe33d0SEli Cohen {
1176cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1177cdbe33d0SEli Cohen 	int err;
1178cdbe33d0SEli Cohen 
1179cdbe33d0SEli Cohen 	err = mlx5_query_hca_vport_context(dev->mdev, 0,
1180cdbe33d0SEli Cohen 					   port_num, 0, &ctx);
1181cdbe33d0SEli Cohen 	if (err)
1182cdbe33d0SEli Cohen 		return err;
1183cdbe33d0SEli Cohen 
1184cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1185cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1186cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1187cdbe33d0SEli Cohen 		return -EINVAL;
1188cdbe33d0SEli Cohen 	}
1189cdbe33d0SEli Cohen 
1190cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1191cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1192cdbe33d0SEli Cohen 	err = mlx5_core_modify_hca_vport_context(dev->mdev, 0,
1193cdbe33d0SEli Cohen 						 port_num, 0, &ctx);
1194cdbe33d0SEli Cohen 
1195cdbe33d0SEli Cohen 	return err;
1196cdbe33d0SEli Cohen }
1197cdbe33d0SEli Cohen 
1198e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1199e126ba97SEli Cohen 			       struct ib_port_modify *props)
1200e126ba97SEli Cohen {
1201e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1202e126ba97SEli Cohen 	struct ib_port_attr attr;
1203e126ba97SEli Cohen 	u32 tmp;
1204e126ba97SEli Cohen 	int err;
1205cdbe33d0SEli Cohen 	u32 change_mask;
1206cdbe33d0SEli Cohen 	u32 value;
1207cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1208cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1209cdbe33d0SEli Cohen 
1210ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1211ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1212ec255879SMajd Dibbiny 	 */
1213ec255879SMajd Dibbiny 	if (!is_ib)
1214ec255879SMajd Dibbiny 		return 0;
1215ec255879SMajd Dibbiny 
1216cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1217cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1218cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1219cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1220cdbe33d0SEli Cohen 	}
1221e126ba97SEli Cohen 
1222e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1223e126ba97SEli Cohen 
1224c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1225e126ba97SEli Cohen 	if (err)
1226e126ba97SEli Cohen 		goto out;
1227e126ba97SEli Cohen 
1228e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1229e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1230e126ba97SEli Cohen 
12319603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1232e126ba97SEli Cohen 
1233e126ba97SEli Cohen out:
1234e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1235e126ba97SEli Cohen 	return err;
1236e126ba97SEli Cohen }
1237e126ba97SEli Cohen 
123830aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
123930aa60b3SEli Cohen {
124030aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
124130aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
124230aa60b3SEli Cohen }
124330aa60b3SEli Cohen 
1244b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1245b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
1246b037c29aSEli Cohen 			     u32 *num_sys_pages)
1247b037c29aSEli Cohen {
1248b037c29aSEli Cohen 	int uars_per_sys_page;
1249b037c29aSEli Cohen 	int bfregs_per_sys_page;
1250b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1251b037c29aSEli Cohen 
1252b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1253b037c29aSEli Cohen 		return -EINVAL;
1254b037c29aSEli Cohen 
1255b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1256b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1257b037c29aSEli Cohen 
1258b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1259b037c29aSEli Cohen 		return -ENOMEM;
1260b037c29aSEli Cohen 
1261b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1262b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
1263b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1264b037c29aSEli Cohen 	*num_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
1265b037c29aSEli Cohen 
1266b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1267b037c29aSEli Cohen 		return -EINVAL;
1268b037c29aSEli Cohen 
12699c2d33d4SColin Ian King 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, using %d sys pages\n",
1270b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1271b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
1272b037c29aSEli Cohen 		    req->total_num_bfregs, *num_sys_pages);
1273b037c29aSEli Cohen 
1274b037c29aSEli Cohen 	return 0;
1275b037c29aSEli Cohen }
1276b037c29aSEli Cohen 
1277b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1278b037c29aSEli Cohen {
1279b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1280b037c29aSEli Cohen 	int err;
1281b037c29aSEli Cohen 	int i;
1282b037c29aSEli Cohen 
1283b037c29aSEli Cohen 	bfregi = &context->bfregi;
1284b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1285b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1286b037c29aSEli Cohen 		if (err)
1287b037c29aSEli Cohen 			goto error;
1288b037c29aSEli Cohen 
1289b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1290b037c29aSEli Cohen 	}
1291b037c29aSEli Cohen 	return 0;
1292b037c29aSEli Cohen 
1293b037c29aSEli Cohen error:
1294b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1295b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1296b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1297b037c29aSEli Cohen 
1298b037c29aSEli Cohen 	return err;
1299b037c29aSEli Cohen }
1300b037c29aSEli Cohen 
1301b037c29aSEli Cohen static int deallocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1302b037c29aSEli Cohen {
1303b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1304b037c29aSEli Cohen 	int err;
1305b037c29aSEli Cohen 	int i;
1306b037c29aSEli Cohen 
1307b037c29aSEli Cohen 	bfregi = &context->bfregi;
1308b037c29aSEli Cohen 	for (i = 0; i < bfregi->num_sys_pages; i++) {
1309b037c29aSEli Cohen 		err = mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1310b037c29aSEli Cohen 		if (err) {
1311b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1312b037c29aSEli Cohen 			return err;
1313b037c29aSEli Cohen 		}
1314b037c29aSEli Cohen 	}
1315b037c29aSEli Cohen 	return 0;
1316b037c29aSEli Cohen }
1317b037c29aSEli Cohen 
1318c85023e1SHuy Nguyen static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
1319c85023e1SHuy Nguyen {
1320c85023e1SHuy Nguyen 	int err;
1321c85023e1SHuy Nguyen 
1322c85023e1SHuy Nguyen 	err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
1323c85023e1SHuy Nguyen 	if (err)
1324c85023e1SHuy Nguyen 		return err;
1325c85023e1SHuy Nguyen 
1326c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1327c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1328c85023e1SHuy Nguyen 		return err;
1329c85023e1SHuy Nguyen 
1330c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1331c85023e1SHuy Nguyen 	dev->user_td++;
1332c85023e1SHuy Nguyen 
1333c85023e1SHuy Nguyen 	if (dev->user_td == 2)
1334c85023e1SHuy Nguyen 		err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
1335c85023e1SHuy Nguyen 
1336c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1337c85023e1SHuy Nguyen 	return err;
1338c85023e1SHuy Nguyen }
1339c85023e1SHuy Nguyen 
1340c85023e1SHuy Nguyen static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
1341c85023e1SHuy Nguyen {
1342c85023e1SHuy Nguyen 	mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
1343c85023e1SHuy Nguyen 
1344c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
1345c85023e1SHuy Nguyen 	    !MLX5_CAP_GEN(dev->mdev, disable_local_lb))
1346c85023e1SHuy Nguyen 		return;
1347c85023e1SHuy Nguyen 
1348c85023e1SHuy Nguyen 	mutex_lock(&dev->lb_mutex);
1349c85023e1SHuy Nguyen 	dev->user_td--;
1350c85023e1SHuy Nguyen 
1351c85023e1SHuy Nguyen 	if (dev->user_td < 2)
1352c85023e1SHuy Nguyen 		mlx5_nic_vport_update_local_lb(dev->mdev, false);
1353c85023e1SHuy Nguyen 
1354c85023e1SHuy Nguyen 	mutex_unlock(&dev->lb_mutex);
1355c85023e1SHuy Nguyen }
1356c85023e1SHuy Nguyen 
1357e126ba97SEli Cohen static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
1358e126ba97SEli Cohen 						  struct ib_udata *udata)
1359e126ba97SEli Cohen {
1360e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1361b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1362b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1363e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context;
13642f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
136578c0f98cSEli Cohen 	int ver;
1366e126ba97SEli Cohen 	int err;
1367a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1368a168a41cSMajd Dibbiny 				     max_cqe_version);
1369b037c29aSEli Cohen 	bool lib_uar_4k;
1370e126ba97SEli Cohen 
1371e126ba97SEli Cohen 	if (!dev->ib_active)
1372e126ba97SEli Cohen 		return ERR_PTR(-EAGAIN);
1373e126ba97SEli Cohen 
1374e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
137578c0f98cSEli Cohen 		ver = 0;
1376e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
137778c0f98cSEli Cohen 		ver = 2;
137878c0f98cSEli Cohen 	else
137978c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
138078c0f98cSEli Cohen 
1381e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1382e126ba97SEli Cohen 	if (err)
1383e126ba97SEli Cohen 		return ERR_PTR(err);
1384e126ba97SEli Cohen 
1385b368d7cbSMatan Barak 	if (req.flags)
138678c0f98cSEli Cohen 		return ERR_PTR(-EINVAL);
138778c0f98cSEli Cohen 
1388f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1389b368d7cbSMatan Barak 		return ERR_PTR(-EOPNOTSUPP);
1390b368d7cbSMatan Barak 
13912f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
13922f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
13932f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1394e126ba97SEli Cohen 		return ERR_PTR(-EINVAL);
1395e126ba97SEli Cohen 
1396938fe83cSSaeed Mahameed 	resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
13972cc6ad5fSNoa Osherovich 	if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
1398938fe83cSSaeed Mahameed 		resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
1399b47bd6eaSDaniel Jurgens 	resp.cache_line_size = cache_line_size();
1400938fe83cSSaeed Mahameed 	resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
1401938fe83cSSaeed Mahameed 	resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
1402938fe83cSSaeed Mahameed 	resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1403938fe83cSSaeed Mahameed 	resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
1404938fe83cSSaeed Mahameed 	resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
1405f72300c5SHaggai Abramovsky 	resp.cqe_version = min_t(__u8,
1406f72300c5SHaggai Abramovsky 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
1407f72300c5SHaggai Abramovsky 				 req.max_cqe_version);
140830aa60b3SEli Cohen 	resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
140930aa60b3SEli Cohen 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
141030aa60b3SEli Cohen 	resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
141130aa60b3SEli Cohen 					MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
1412b368d7cbSMatan Barak 	resp.response_length = min(offsetof(typeof(resp), response_length) +
1413b368d7cbSMatan Barak 				   sizeof(resp.response_length), udata->outlen);
1414e126ba97SEli Cohen 
1415e126ba97SEli Cohen 	context = kzalloc(sizeof(*context), GFP_KERNEL);
1416e126ba97SEli Cohen 	if (!context)
1417e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1418e126ba97SEli Cohen 
141930aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
14202f5ff264SEli Cohen 	bfregi = &context->bfregi;
1421b037c29aSEli Cohen 
1422b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
1423b037c29aSEli Cohen 	err = calc_total_bfregs(dev, lib_uar_4k, &req, &bfregi->num_sys_pages);
1424b037c29aSEli Cohen 	if (err)
1425b037c29aSEli Cohen 		goto out_ctx;
1426b037c29aSEli Cohen 
14272f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1428b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
1429b037c29aSEli Cohen 	bfregi->count = kcalloc(req.total_num_bfregs, sizeof(*bfregi->count),
1430b037c29aSEli Cohen 				GFP_KERNEL);
1431b037c29aSEli Cohen 	if (!bfregi->count) {
1432e126ba97SEli Cohen 		err = -ENOMEM;
1433e126ba97SEli Cohen 		goto out_ctx;
1434e126ba97SEli Cohen 	}
1435e126ba97SEli Cohen 
1436b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1437b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1438e126ba97SEli Cohen 				    GFP_KERNEL);
1439b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1440e126ba97SEli Cohen 		err = -ENOMEM;
1441e126ba97SEli Cohen 		goto out_count;
1442e126ba97SEli Cohen 	}
1443e126ba97SEli Cohen 
1444b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1445b037c29aSEli Cohen 	if (err)
1446b037c29aSEli Cohen 		goto out_sys_pages;
1447b037c29aSEli Cohen 
1448b4cfe447SHaggai Eran #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1449b4cfe447SHaggai Eran 	context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
1450b4cfe447SHaggai Eran #endif
1451b4cfe447SHaggai Eran 
14527d0cc6edSArtemy Kovalyov 	context->upd_xlt_page = __get_free_page(GFP_KERNEL);
14537d0cc6edSArtemy Kovalyov 	if (!context->upd_xlt_page) {
14547d0cc6edSArtemy Kovalyov 		err = -ENOMEM;
14557d0cc6edSArtemy Kovalyov 		goto out_uars;
14567d0cc6edSArtemy Kovalyov 	}
14577d0cc6edSArtemy Kovalyov 	mutex_init(&context->upd_xlt_page_mutex);
14587d0cc6edSArtemy Kovalyov 
1459146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain)) {
1460c85023e1SHuy Nguyen 		err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
1461146d2f1aSmajd@mellanox.com 		if (err)
14627d0cc6edSArtemy Kovalyov 			goto out_page;
1463146d2f1aSmajd@mellanox.com 	}
1464146d2f1aSmajd@mellanox.com 
14657c2344c3SMaor Gottlieb 	INIT_LIST_HEAD(&context->vma_private_list);
1466*ad9a3668SMajd Dibbiny 	mutex_init(&context->vma_private_list_mutex);
1467e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1468e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1469e126ba97SEli Cohen 
14702f5ff264SEli Cohen 	resp.tot_bfregs = req.total_num_bfregs;
1471938fe83cSSaeed Mahameed 	resp.num_ports = MLX5_CAP_GEN(dev->mdev, num_ports);
1472b368d7cbSMatan Barak 
1473f72300c5SHaggai Abramovsky 	if (field_avail(typeof(resp), cqe_version, udata->outlen))
1474f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.cqe_version);
1475b368d7cbSMatan Barak 
1476402ca536SBodong Wang 	if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
14776ad279c5SMoni Shoua 		resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
14786ad279c5SMoni Shoua 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
1479402ca536SBodong Wang 		resp.response_length += sizeof(resp.cmds_supp_uhw);
1480402ca536SBodong Wang 	}
1481402ca536SBodong Wang 
148278984898SOr Gerlitz 	if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
148378984898SOr Gerlitz 		if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
148478984898SOr Gerlitz 			mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
148578984898SOr Gerlitz 			resp.eth_min_inline++;
148678984898SOr Gerlitz 		}
148778984898SOr Gerlitz 		resp.response_length += sizeof(resp.eth_min_inline);
148878984898SOr Gerlitz 	}
148978984898SOr Gerlitz 
1490bc5c6eedSNoa Osherovich 	/*
1491bc5c6eedSNoa Osherovich 	 * We don't want to expose information from the PCI bar that is located
1492bc5c6eedSNoa Osherovich 	 * after 4096 bytes, so if the arch only supports larger pages, let's
1493bc5c6eedSNoa Osherovich 	 * pretend we don't support reading the HCA's core clock. This is also
1494bc5c6eedSNoa Osherovich 	 * forced by mmap function.
1495bc5c6eedSNoa Osherovich 	 */
1496de8d6e02SEli Cohen 	if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
1497de8d6e02SEli Cohen 		if (PAGE_SIZE <= 4096) {
1498b368d7cbSMatan Barak 			resp.comp_mask |=
1499b368d7cbSMatan Barak 				MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
1500b368d7cbSMatan Barak 			resp.hca_core_clock_offset =
1501de8d6e02SEli Cohen 				offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
1502de8d6e02SEli Cohen 		}
1503f72300c5SHaggai Abramovsky 		resp.response_length += sizeof(resp.hca_core_clock_offset) +
1504402ca536SBodong Wang 					sizeof(resp.reserved2);
1505b368d7cbSMatan Barak 	}
1506b368d7cbSMatan Barak 
150730aa60b3SEli Cohen 	if (field_avail(typeof(resp), log_uar_size, udata->outlen))
150830aa60b3SEli Cohen 		resp.response_length += sizeof(resp.log_uar_size);
150930aa60b3SEli Cohen 
151030aa60b3SEli Cohen 	if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
151130aa60b3SEli Cohen 		resp.response_length += sizeof(resp.num_uars_per_page);
151230aa60b3SEli Cohen 
1513b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1514e126ba97SEli Cohen 	if (err)
1515146d2f1aSmajd@mellanox.com 		goto out_td;
1516e126ba97SEli Cohen 
15172f5ff264SEli Cohen 	bfregi->ver = ver;
15182f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
1519f72300c5SHaggai Abramovsky 	context->cqe_version = resp.cqe_version;
152030aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
152130aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1522f72300c5SHaggai Abramovsky 
1523e126ba97SEli Cohen 	return &context->ibucontext;
1524e126ba97SEli Cohen 
1525146d2f1aSmajd@mellanox.com out_td:
1526146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1527c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1528146d2f1aSmajd@mellanox.com 
15297d0cc6edSArtemy Kovalyov out_page:
15307d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
15317d0cc6edSArtemy Kovalyov 
1532e126ba97SEli Cohen out_uars:
1533b037c29aSEli Cohen 	deallocate_uars(dev, context);
1534b037c29aSEli Cohen 
1535b037c29aSEli Cohen out_sys_pages:
1536b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1537b037c29aSEli Cohen 
1538e126ba97SEli Cohen out_count:
15392f5ff264SEli Cohen 	kfree(bfregi->count);
1540e126ba97SEli Cohen 
1541e126ba97SEli Cohen out_ctx:
1542e126ba97SEli Cohen 	kfree(context);
1543b037c29aSEli Cohen 
1544e126ba97SEli Cohen 	return ERR_PTR(err);
1545e126ba97SEli Cohen }
1546e126ba97SEli Cohen 
1547e126ba97SEli Cohen static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1548e126ba97SEli Cohen {
1549e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1550e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1551b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1552e126ba97SEli Cohen 
1553b037c29aSEli Cohen 	bfregi = &context->bfregi;
1554146d2f1aSmajd@mellanox.com 	if (MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1555c85023e1SHuy Nguyen 		mlx5_ib_dealloc_transport_domain(dev, context->tdn);
1556146d2f1aSmajd@mellanox.com 
15577d0cc6edSArtemy Kovalyov 	free_page(context->upd_xlt_page);
1558b037c29aSEli Cohen 	deallocate_uars(dev, context);
1559b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
15602f5ff264SEli Cohen 	kfree(bfregi->count);
1561e126ba97SEli Cohen 	kfree(context);
1562e126ba97SEli Cohen 
1563e126ba97SEli Cohen 	return 0;
1564e126ba97SEli Cohen }
1565e126ba97SEli Cohen 
1566b037c29aSEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
1567b037c29aSEli Cohen 				 struct mlx5_bfreg_info *bfregi,
1568b037c29aSEli Cohen 				 int idx)
1569e126ba97SEli Cohen {
1570b037c29aSEli Cohen 	int fw_uars_per_page;
1571b037c29aSEli Cohen 
1572b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
1573b037c29aSEli Cohen 
1574b037c29aSEli Cohen 	return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) +
1575b037c29aSEli Cohen 			bfregi->sys_pages[idx] / fw_uars_per_page;
1576e126ba97SEli Cohen }
1577e126ba97SEli Cohen 
1578e126ba97SEli Cohen static int get_command(unsigned long offset)
1579e126ba97SEli Cohen {
1580e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
1581e126ba97SEli Cohen }
1582e126ba97SEli Cohen 
1583e126ba97SEli Cohen static int get_arg(unsigned long offset)
1584e126ba97SEli Cohen {
1585e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
1586e126ba97SEli Cohen }
1587e126ba97SEli Cohen 
1588e126ba97SEli Cohen static int get_index(unsigned long offset)
1589e126ba97SEli Cohen {
1590e126ba97SEli Cohen 	return get_arg(offset);
1591e126ba97SEli Cohen }
1592e126ba97SEli Cohen 
15937c2344c3SMaor Gottlieb static void  mlx5_ib_vma_open(struct vm_area_struct *area)
15947c2344c3SMaor Gottlieb {
15957c2344c3SMaor Gottlieb 	/* vma_open is called when a new VMA is created on top of our VMA.  This
15967c2344c3SMaor Gottlieb 	 * is done through either mremap flow or split_vma (usually due to
15977c2344c3SMaor Gottlieb 	 * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
15987c2344c3SMaor Gottlieb 	 * as this VMA is strongly hardware related.  Therefore we set the
15997c2344c3SMaor Gottlieb 	 * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
16007c2344c3SMaor Gottlieb 	 * calling us again and trying to do incorrect actions.  We assume that
16017c2344c3SMaor Gottlieb 	 * the original VMA size is exactly a single page, and therefore all
16027c2344c3SMaor Gottlieb 	 * "splitting" operation will not happen to it.
16037c2344c3SMaor Gottlieb 	 */
16047c2344c3SMaor Gottlieb 	area->vm_ops = NULL;
16057c2344c3SMaor Gottlieb }
16067c2344c3SMaor Gottlieb 
16077c2344c3SMaor Gottlieb static void  mlx5_ib_vma_close(struct vm_area_struct *area)
16087c2344c3SMaor Gottlieb {
16097c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
16107c2344c3SMaor Gottlieb 
16117c2344c3SMaor Gottlieb 	/* It's guaranteed that all VMAs opened on a FD are closed before the
16127c2344c3SMaor Gottlieb 	 * file itself is closed, therefore no sync is needed with the regular
16137c2344c3SMaor Gottlieb 	 * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
16147c2344c3SMaor Gottlieb 	 * However need a sync with accessing the vma as part of
16157c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16167c2344c3SMaor Gottlieb 	 * The close operation is usually called under mm->mmap_sem except when
16177c2344c3SMaor Gottlieb 	 * process is exiting.
16187c2344c3SMaor Gottlieb 	 * The exiting case is handled explicitly as part of
16197c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext.
16207c2344c3SMaor Gottlieb 	 */
16217c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
16227c2344c3SMaor Gottlieb 
16237c2344c3SMaor Gottlieb 	/* setting the vma context pointer to null in the mlx5_ib driver's
16247c2344c3SMaor Gottlieb 	 * private data, to protect a race condition in
16257c2344c3SMaor Gottlieb 	 * mlx5_ib_disassociate_ucontext().
16267c2344c3SMaor Gottlieb 	 */
16277c2344c3SMaor Gottlieb 	mlx5_ib_vma_priv_data->vma = NULL;
1628*ad9a3668SMajd Dibbiny 	mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
16297c2344c3SMaor Gottlieb 	list_del(&mlx5_ib_vma_priv_data->list);
1630*ad9a3668SMajd Dibbiny 	mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
16317c2344c3SMaor Gottlieb 	kfree(mlx5_ib_vma_priv_data);
16327c2344c3SMaor Gottlieb }
16337c2344c3SMaor Gottlieb 
16347c2344c3SMaor Gottlieb static const struct vm_operations_struct mlx5_ib_vm_ops = {
16357c2344c3SMaor Gottlieb 	.open = mlx5_ib_vma_open,
16367c2344c3SMaor Gottlieb 	.close = mlx5_ib_vma_close
16377c2344c3SMaor Gottlieb };
16387c2344c3SMaor Gottlieb 
16397c2344c3SMaor Gottlieb static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
16407c2344c3SMaor Gottlieb 				struct mlx5_ib_ucontext *ctx)
16417c2344c3SMaor Gottlieb {
16427c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_prv;
16437c2344c3SMaor Gottlieb 	struct list_head *vma_head = &ctx->vma_private_list;
16447c2344c3SMaor Gottlieb 
16457c2344c3SMaor Gottlieb 	vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
16467c2344c3SMaor Gottlieb 	if (!vma_prv)
16477c2344c3SMaor Gottlieb 		return -ENOMEM;
16487c2344c3SMaor Gottlieb 
16497c2344c3SMaor Gottlieb 	vma_prv->vma = vma;
1650*ad9a3668SMajd Dibbiny 	vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
16517c2344c3SMaor Gottlieb 	vma->vm_private_data = vma_prv;
16527c2344c3SMaor Gottlieb 	vma->vm_ops =  &mlx5_ib_vm_ops;
16537c2344c3SMaor Gottlieb 
1654*ad9a3668SMajd Dibbiny 	mutex_lock(&ctx->vma_private_list_mutex);
16557c2344c3SMaor Gottlieb 	list_add(&vma_prv->list, vma_head);
1656*ad9a3668SMajd Dibbiny 	mutex_unlock(&ctx->vma_private_list_mutex);
16577c2344c3SMaor Gottlieb 
16587c2344c3SMaor Gottlieb 	return 0;
16597c2344c3SMaor Gottlieb }
16607c2344c3SMaor Gottlieb 
16617c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
16627c2344c3SMaor Gottlieb {
16637c2344c3SMaor Gottlieb 	int ret;
16647c2344c3SMaor Gottlieb 	struct vm_area_struct *vma;
16657c2344c3SMaor Gottlieb 	struct mlx5_ib_vma_private_data *vma_private, *n;
16667c2344c3SMaor Gottlieb 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
16677c2344c3SMaor Gottlieb 	struct task_struct *owning_process  = NULL;
16687c2344c3SMaor Gottlieb 	struct mm_struct   *owning_mm       = NULL;
16697c2344c3SMaor Gottlieb 
16707c2344c3SMaor Gottlieb 	owning_process = get_pid_task(ibcontext->tgid, PIDTYPE_PID);
16717c2344c3SMaor Gottlieb 	if (!owning_process)
16727c2344c3SMaor Gottlieb 		return;
16737c2344c3SMaor Gottlieb 
16747c2344c3SMaor Gottlieb 	owning_mm = get_task_mm(owning_process);
16757c2344c3SMaor Gottlieb 	if (!owning_mm) {
16767c2344c3SMaor Gottlieb 		pr_info("no mm, disassociate ucontext is pending task termination\n");
16777c2344c3SMaor Gottlieb 		while (1) {
16787c2344c3SMaor Gottlieb 			put_task_struct(owning_process);
16797c2344c3SMaor Gottlieb 			usleep_range(1000, 2000);
16807c2344c3SMaor Gottlieb 			owning_process = get_pid_task(ibcontext->tgid,
16817c2344c3SMaor Gottlieb 						      PIDTYPE_PID);
16827c2344c3SMaor Gottlieb 			if (!owning_process ||
16837c2344c3SMaor Gottlieb 			    owning_process->state == TASK_DEAD) {
16847c2344c3SMaor Gottlieb 				pr_info("disassociate ucontext done, task was terminated\n");
16857c2344c3SMaor Gottlieb 				/* in case task was dead need to release the
16867c2344c3SMaor Gottlieb 				 * task struct.
16877c2344c3SMaor Gottlieb 				 */
16887c2344c3SMaor Gottlieb 				if (owning_process)
16897c2344c3SMaor Gottlieb 					put_task_struct(owning_process);
16907c2344c3SMaor Gottlieb 				return;
16917c2344c3SMaor Gottlieb 			}
16927c2344c3SMaor Gottlieb 		}
16937c2344c3SMaor Gottlieb 	}
16947c2344c3SMaor Gottlieb 
16957c2344c3SMaor Gottlieb 	/* need to protect from a race on closing the vma as part of
16967c2344c3SMaor Gottlieb 	 * mlx5_ib_vma_close.
16977c2344c3SMaor Gottlieb 	 */
1698ecc7d83bSMaor Gottlieb 	down_write(&owning_mm->mmap_sem);
1699*ad9a3668SMajd Dibbiny 	mutex_lock(&context->vma_private_list_mutex);
17007c2344c3SMaor Gottlieb 	list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
17017c2344c3SMaor Gottlieb 				 list) {
17027c2344c3SMaor Gottlieb 		vma = vma_private->vma;
17037c2344c3SMaor Gottlieb 		ret = zap_vma_ptes(vma, vma->vm_start,
17047c2344c3SMaor Gottlieb 				   PAGE_SIZE);
17057c2344c3SMaor Gottlieb 		WARN_ONCE(ret, "%s: zap_vma_ptes failed", __func__);
17067c2344c3SMaor Gottlieb 		/* context going to be destroyed, should
17077c2344c3SMaor Gottlieb 		 * not access ops any more.
17087c2344c3SMaor Gottlieb 		 */
170913776612SMaor Gottlieb 		vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
17107c2344c3SMaor Gottlieb 		vma->vm_ops = NULL;
17117c2344c3SMaor Gottlieb 		list_del(&vma_private->list);
17127c2344c3SMaor Gottlieb 		kfree(vma_private);
17137c2344c3SMaor Gottlieb 	}
1714*ad9a3668SMajd Dibbiny 	mutex_unlock(&context->vma_private_list_mutex);
1715ecc7d83bSMaor Gottlieb 	up_write(&owning_mm->mmap_sem);
17167c2344c3SMaor Gottlieb 	mmput(owning_mm);
17177c2344c3SMaor Gottlieb 	put_task_struct(owning_process);
17187c2344c3SMaor Gottlieb }
17197c2344c3SMaor Gottlieb 
172037aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
1721e126ba97SEli Cohen {
172237aa5c36SGuy Levi 	switch (cmd) {
172337aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
172437aa5c36SGuy Levi 		return "WC";
1725e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
172637aa5c36SGuy Levi 		return "best effort WC";
172737aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
172837aa5c36SGuy Levi 		return "NC";
172937aa5c36SGuy Levi 	default:
173037aa5c36SGuy Levi 		return NULL;
173137aa5c36SGuy Levi 	}
173237aa5c36SGuy Levi }
173337aa5c36SGuy Levi 
173437aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
17357c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
17367c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
173737aa5c36SGuy Levi {
17382f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
173937aa5c36SGuy Levi 	int err;
174037aa5c36SGuy Levi 	unsigned long idx;
174137aa5c36SGuy Levi 	phys_addr_t pfn, pa;
174237aa5c36SGuy Levi 	pgprot_t prot;
1743b037c29aSEli Cohen 	int uars_per_page;
1744b037c29aSEli Cohen 
1745b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1746b037c29aSEli Cohen 		return -EINVAL;
1747b037c29aSEli Cohen 
1748b037c29aSEli Cohen 	uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
1749b037c29aSEli Cohen 	idx = get_index(vma->vm_pgoff);
1750b037c29aSEli Cohen 	if (idx % uars_per_page ||
1751b037c29aSEli Cohen 	    idx * uars_per_page >= bfregi->num_sys_pages) {
1752b037c29aSEli Cohen 		mlx5_ib_warn(dev, "invalid uar index %lu\n", idx);
1753b037c29aSEli Cohen 		return -EINVAL;
1754b037c29aSEli Cohen 	}
175537aa5c36SGuy Levi 
175637aa5c36SGuy Levi 	switch (cmd) {
175737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
175837aa5c36SGuy Levi /* Some architectures don't support WC memory */
175937aa5c36SGuy Levi #if defined(CONFIG_X86)
176037aa5c36SGuy Levi 		if (!pat_enabled())
176137aa5c36SGuy Levi 			return -EPERM;
176237aa5c36SGuy Levi #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
176337aa5c36SGuy Levi 			return -EPERM;
176437aa5c36SGuy Levi #endif
176537aa5c36SGuy Levi 	/* fall through */
176637aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
176737aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
176837aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
176937aa5c36SGuy Levi 		break;
177037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
177137aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
177237aa5c36SGuy Levi 		break;
177337aa5c36SGuy Levi 	default:
177437aa5c36SGuy Levi 		return -EINVAL;
177537aa5c36SGuy Levi 	}
177637aa5c36SGuy Levi 
1777b037c29aSEli Cohen 	pfn = uar_index2pfn(dev, bfregi, idx);
177837aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
1779e126ba97SEli Cohen 
178037aa5c36SGuy Levi 	vma->vm_page_prot = prot;
178137aa5c36SGuy Levi 	err = io_remap_pfn_range(vma, vma->vm_start, pfn,
178237aa5c36SGuy Levi 				 PAGE_SIZE, vma->vm_page_prot);
178337aa5c36SGuy Levi 	if (err) {
178437aa5c36SGuy Levi 		mlx5_ib_err(dev, "io_remap_pfn_range failed with error=%d, vm_start=0x%lx, pfn=%pa, mmap_cmd=%s\n",
178537aa5c36SGuy Levi 			    err, vma->vm_start, &pfn, mmap_cmd2str(cmd));
1786e126ba97SEli Cohen 		return -EAGAIN;
178737aa5c36SGuy Levi 	}
1788e126ba97SEli Cohen 
178937aa5c36SGuy Levi 	pa = pfn << PAGE_SHIFT;
179037aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "mapped %s at 0x%lx, PA %pa\n", mmap_cmd2str(cmd),
179137aa5c36SGuy Levi 		    vma->vm_start, &pa);
179237aa5c36SGuy Levi 
17937c2344c3SMaor Gottlieb 	return mlx5_ib_set_vma_data(vma, context);
179437aa5c36SGuy Levi }
179537aa5c36SGuy Levi 
179637aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
179737aa5c36SGuy Levi {
179837aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
179937aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
180037aa5c36SGuy Levi 	unsigned long command;
180137aa5c36SGuy Levi 	phys_addr_t pfn;
180237aa5c36SGuy Levi 
180337aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
180437aa5c36SGuy Levi 	switch (command) {
180537aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
180637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
180737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
18087c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
1809e126ba97SEli Cohen 
1810e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
1811e126ba97SEli Cohen 		return -ENOSYS;
1812e126ba97SEli Cohen 
1813d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
1814d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
1815d69e3bcfSMatan Barak 			return -EINVAL;
1816d69e3bcfSMatan Barak 
18176cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
1818d69e3bcfSMatan Barak 			return -EPERM;
1819d69e3bcfSMatan Barak 
1820d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
1821d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
1822d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
1823d69e3bcfSMatan Barak 
1824d69e3bcfSMatan Barak 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1825d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
1826d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
1827d69e3bcfSMatan Barak 			PAGE_SHIFT;
1828d69e3bcfSMatan Barak 		if (io_remap_pfn_range(vma, vma->vm_start, pfn,
1829d69e3bcfSMatan Barak 				       PAGE_SIZE, vma->vm_page_prot))
1830d69e3bcfSMatan Barak 			return -EAGAIN;
1831d69e3bcfSMatan Barak 
1832d69e3bcfSMatan Barak 		mlx5_ib_dbg(dev, "mapped internal timer at 0x%lx, PA 0x%llx\n",
1833d69e3bcfSMatan Barak 			    vma->vm_start,
1834d69e3bcfSMatan Barak 			    (unsigned long long)pfn << PAGE_SHIFT);
1835d69e3bcfSMatan Barak 		break;
1836d69e3bcfSMatan Barak 
1837e126ba97SEli Cohen 	default:
1838e126ba97SEli Cohen 		return -EINVAL;
1839e126ba97SEli Cohen 	}
1840e126ba97SEli Cohen 
1841e126ba97SEli Cohen 	return 0;
1842e126ba97SEli Cohen }
1843e126ba97SEli Cohen 
1844e126ba97SEli Cohen static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
1845e126ba97SEli Cohen 				      struct ib_ucontext *context,
1846e126ba97SEli Cohen 				      struct ib_udata *udata)
1847e126ba97SEli Cohen {
1848e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
1849e126ba97SEli Cohen 	struct mlx5_ib_pd *pd;
1850e126ba97SEli Cohen 	int err;
1851e126ba97SEli Cohen 
1852e126ba97SEli Cohen 	pd = kmalloc(sizeof(*pd), GFP_KERNEL);
1853e126ba97SEli Cohen 	if (!pd)
1854e126ba97SEli Cohen 		return ERR_PTR(-ENOMEM);
1855e126ba97SEli Cohen 
18569603b61dSJack Morgenstein 	err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
1857e126ba97SEli Cohen 	if (err) {
1858e126ba97SEli Cohen 		kfree(pd);
1859e126ba97SEli Cohen 		return ERR_PTR(err);
1860e126ba97SEli Cohen 	}
1861e126ba97SEli Cohen 
1862e126ba97SEli Cohen 	if (context) {
1863e126ba97SEli Cohen 		resp.pdn = pd->pdn;
1864e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
18659603b61dSJack Morgenstein 			mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
1866e126ba97SEli Cohen 			kfree(pd);
1867e126ba97SEli Cohen 			return ERR_PTR(-EFAULT);
1868e126ba97SEli Cohen 		}
1869e126ba97SEli Cohen 	}
1870e126ba97SEli Cohen 
1871e126ba97SEli Cohen 	return &pd->ibpd;
1872e126ba97SEli Cohen }
1873e126ba97SEli Cohen 
1874e126ba97SEli Cohen static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
1875e126ba97SEli Cohen {
1876e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
1877e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
1878e126ba97SEli Cohen 
18799603b61dSJack Morgenstein 	mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
1880e126ba97SEli Cohen 	kfree(mpd);
1881e126ba97SEli Cohen 
1882e126ba97SEli Cohen 	return 0;
1883e126ba97SEli Cohen }
1884e126ba97SEli Cohen 
1885466fa6d2SMaor Gottlieb enum {
1886466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_OUTER_BIT,
1887466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_MISC_BIT,
1888466fa6d2SMaor Gottlieb 	MATCH_CRITERIA_ENABLE_INNER_BIT
1889466fa6d2SMaor Gottlieb };
1890038d2ef8SMaor Gottlieb 
1891466fa6d2SMaor Gottlieb #define HEADER_IS_ZERO(match_criteria, headers)			           \
1892466fa6d2SMaor Gottlieb 	!(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
1893466fa6d2SMaor Gottlieb 		    0, MLX5_FLD_SZ_BYTES(fte_match_param, headers)))       \
1894466fa6d2SMaor Gottlieb 
1895466fa6d2SMaor Gottlieb static u8 get_match_criteria_enable(u32 *match_criteria)
1896466fa6d2SMaor Gottlieb {
1897466fa6d2SMaor Gottlieb 	u8 match_criteria_enable;
1898466fa6d2SMaor Gottlieb 
1899466fa6d2SMaor Gottlieb 	match_criteria_enable =
1900466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
1901466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_OUTER_BIT;
1902466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1903466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
1904466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_MISC_BIT;
1905466fa6d2SMaor Gottlieb 	match_criteria_enable |=
1906466fa6d2SMaor Gottlieb 		(!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
1907466fa6d2SMaor Gottlieb 		MATCH_CRITERIA_ENABLE_INNER_BIT;
1908466fa6d2SMaor Gottlieb 
1909466fa6d2SMaor Gottlieb 	return match_criteria_enable;
1910038d2ef8SMaor Gottlieb }
1911038d2ef8SMaor Gottlieb 
1912ca0d4753SMaor Gottlieb static void set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
1913ca0d4753SMaor Gottlieb {
1914ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
1915ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
1916ca0d4753SMaor Gottlieb }
1917ca0d4753SMaor Gottlieb 
19182d1e697eSMoses Reuben static void set_flow_label(void *misc_c, void *misc_v, u8 mask, u8 val,
19192d1e697eSMoses Reuben 			   bool inner)
19202d1e697eSMoses Reuben {
19212d1e697eSMoses Reuben 	if (inner) {
19222d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19232d1e697eSMoses Reuben 			 misc_c, inner_ipv6_flow_label, mask);
19242d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19252d1e697eSMoses Reuben 			 misc_v, inner_ipv6_flow_label, val);
19262d1e697eSMoses Reuben 	} else {
19272d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19282d1e697eSMoses Reuben 			 misc_c, outer_ipv6_flow_label, mask);
19292d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_misc,
19302d1e697eSMoses Reuben 			 misc_v, outer_ipv6_flow_label, val);
19312d1e697eSMoses Reuben 	}
19322d1e697eSMoses Reuben }
19332d1e697eSMoses Reuben 
1934ca0d4753SMaor Gottlieb static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
1935ca0d4753SMaor Gottlieb {
1936ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
1937ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
1938ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
1939ca0d4753SMaor Gottlieb 	MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
1940ca0d4753SMaor Gottlieb }
1941ca0d4753SMaor Gottlieb 
1942c47ac6aeSMaor Gottlieb #define LAST_ETH_FIELD vlan_tag
1943c47ac6aeSMaor Gottlieb #define LAST_IB_FIELD sl
1944ca0d4753SMaor Gottlieb #define LAST_IPV4_FIELD tos
1945466fa6d2SMaor Gottlieb #define LAST_IPV6_FIELD traffic_class
1946c47ac6aeSMaor Gottlieb #define LAST_TCP_UDP_FIELD src_port
1947ffb30d8fSMoses Reuben #define LAST_TUNNEL_FIELD tunnel_id
19482ac693f9SMoses Reuben #define LAST_FLOW_TAG_FIELD tag_id
1949a22ed86cSSlava Shwartsman #define LAST_DROP_FIELD size
1950c47ac6aeSMaor Gottlieb 
1951c47ac6aeSMaor Gottlieb /* Field is the last supported field */
1952c47ac6aeSMaor Gottlieb #define FIELDS_NOT_SUPPORTED(filter, field)\
1953c47ac6aeSMaor Gottlieb 	memchr_inv((void *)&filter.field  +\
1954c47ac6aeSMaor Gottlieb 		   sizeof(filter.field), 0,\
1955c47ac6aeSMaor Gottlieb 		   sizeof(filter) -\
1956c47ac6aeSMaor Gottlieb 		   offsetof(typeof(filter), field) -\
1957c47ac6aeSMaor Gottlieb 		   sizeof(filter.field))
1958c47ac6aeSMaor Gottlieb 
195919cc7524SAriel Levkovich #define IPV4_VERSION 4
196019cc7524SAriel Levkovich #define IPV6_VERSION 6
196119cc7524SAriel Levkovich static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
196219cc7524SAriel Levkovich 			   u32 *match_v, const union ib_flow_spec *ib_spec,
1963a22ed86cSSlava Shwartsman 			   u32 *tag_id, bool *is_drop)
1964038d2ef8SMaor Gottlieb {
1965466fa6d2SMaor Gottlieb 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
1966466fa6d2SMaor Gottlieb 					   misc_parameters);
1967466fa6d2SMaor Gottlieb 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
1968466fa6d2SMaor Gottlieb 					   misc_parameters);
19692d1e697eSMoses Reuben 	void *headers_c;
19702d1e697eSMoses Reuben 	void *headers_v;
197119cc7524SAriel Levkovich 	int match_ipv;
1972466fa6d2SMaor Gottlieb 
19732d1e697eSMoses Reuben 	if (ib_spec->type & IB_FLOW_SPEC_INNER) {
19742d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19752d1e697eSMoses Reuben 					 inner_headers);
19762d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19772d1e697eSMoses Reuben 					 inner_headers);
197819cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
197919cc7524SAriel Levkovich 					ft_field_support.inner_ip_version);
19802d1e697eSMoses Reuben 	} else {
19812d1e697eSMoses Reuben 		headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
19822d1e697eSMoses Reuben 					 outer_headers);
19832d1e697eSMoses Reuben 		headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
19842d1e697eSMoses Reuben 					 outer_headers);
198519cc7524SAriel Levkovich 		match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
198619cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
19872d1e697eSMoses Reuben 	}
19882d1e697eSMoses Reuben 
19892d1e697eSMoses Reuben 	switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
1990038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_ETH:
1991c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
19921ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
1993038d2ef8SMaor Gottlieb 
19942d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
1995038d2ef8SMaor Gottlieb 					     dmac_47_16),
1996038d2ef8SMaor Gottlieb 				ib_spec->eth.mask.dst_mac);
19972d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
1998038d2ef8SMaor Gottlieb 					     dmac_47_16),
1999038d2ef8SMaor Gottlieb 				ib_spec->eth.val.dst_mac);
2000038d2ef8SMaor Gottlieb 
20012d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2002ee3da804SMaor Gottlieb 					     smac_47_16),
2003ee3da804SMaor Gottlieb 				ib_spec->eth.mask.src_mac);
20042d1e697eSMoses Reuben 		ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2005ee3da804SMaor Gottlieb 					     smac_47_16),
2006ee3da804SMaor Gottlieb 				ib_spec->eth.val.src_mac);
2007ee3da804SMaor Gottlieb 
2008038d2ef8SMaor Gottlieb 		if (ib_spec->eth.mask.vlan_tag) {
20092d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
201010543365SMohamad Haj Yahia 				 cvlan_tag, 1);
20112d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
201210543365SMohamad Haj Yahia 				 cvlan_tag, 1);
2013038d2ef8SMaor Gottlieb 
20142d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2015038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
20162d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2017038d2ef8SMaor Gottlieb 				 first_vid, ntohs(ib_spec->eth.val.vlan_tag));
2018038d2ef8SMaor Gottlieb 
20192d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2020038d2ef8SMaor Gottlieb 				 first_cfi,
2021038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
20222d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2023038d2ef8SMaor Gottlieb 				 first_cfi,
2024038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 12);
2025038d2ef8SMaor Gottlieb 
20262d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2027038d2ef8SMaor Gottlieb 				 first_prio,
2028038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
20292d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2030038d2ef8SMaor Gottlieb 				 first_prio,
2031038d2ef8SMaor Gottlieb 				 ntohs(ib_spec->eth.val.vlan_tag) >> 13);
2032038d2ef8SMaor Gottlieb 		}
20332d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2034038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.mask.ether_type));
20352d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2036038d2ef8SMaor Gottlieb 			 ethertype, ntohs(ib_spec->eth.val.ether_type));
2037038d2ef8SMaor Gottlieb 		break;
2038038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_IPV4:
2039c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
20401ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2041038d2ef8SMaor Gottlieb 
204219cc7524SAriel Levkovich 		if (match_ipv) {
204319cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
204419cc7524SAriel Levkovich 				 ip_version, 0xf);
204519cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
204619cc7524SAriel Levkovich 				 ip_version, IPV4_VERSION);
204719cc7524SAriel Levkovich 		} else {
20482d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2049038d2ef8SMaor Gottlieb 				 ethertype, 0xffff);
20502d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2051038d2ef8SMaor Gottlieb 				 ethertype, ETH_P_IP);
205219cc7524SAriel Levkovich 		}
2053038d2ef8SMaor Gottlieb 
20542d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2055038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2056038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.src_ip,
2057038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.src_ip));
20582d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2059038d2ef8SMaor Gottlieb 				    src_ipv4_src_ipv6.ipv4_layout.ipv4),
2060038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.src_ip,
2061038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.src_ip));
20622d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2063038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2064038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.mask.dst_ip,
2065038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.mask.dst_ip));
20662d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2067038d2ef8SMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
2068038d2ef8SMaor Gottlieb 		       &ib_spec->ipv4.val.dst_ip,
2069038d2ef8SMaor Gottlieb 		       sizeof(ib_spec->ipv4.val.dst_ip));
2070ca0d4753SMaor Gottlieb 
20712d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2072ca0d4753SMaor Gottlieb 			ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
2073ca0d4753SMaor Gottlieb 
20742d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2075ca0d4753SMaor Gottlieb 			  ib_spec->ipv4.mask.proto, ib_spec->ipv4.val.proto);
2076038d2ef8SMaor Gottlieb 		break;
2077026bae0cSMaor Gottlieb 	case IB_FLOW_SPEC_IPV6:
2078c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
20791ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2080026bae0cSMaor Gottlieb 
208119cc7524SAriel Levkovich 		if (match_ipv) {
208219cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
208319cc7524SAriel Levkovich 				 ip_version, 0xf);
208419cc7524SAriel Levkovich 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
208519cc7524SAriel Levkovich 				 ip_version, IPV6_VERSION);
208619cc7524SAriel Levkovich 		} else {
20872d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_c,
2088026bae0cSMaor Gottlieb 				 ethertype, 0xffff);
20892d1e697eSMoses Reuben 			MLX5_SET(fte_match_set_lyr_2_4, headers_v,
2090026bae0cSMaor Gottlieb 				 ethertype, ETH_P_IPV6);
209119cc7524SAriel Levkovich 		}
2092026bae0cSMaor Gottlieb 
20932d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2094026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2095026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.src_ip,
2096026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.src_ip));
20972d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2098026bae0cSMaor Gottlieb 				    src_ipv4_src_ipv6.ipv6_layout.ipv6),
2099026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.src_ip,
2100026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.src_ip));
21012d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
2102026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2103026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.mask.dst_ip,
2104026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.mask.dst_ip));
21052d1e697eSMoses Reuben 		memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
2106026bae0cSMaor Gottlieb 				    dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
2107026bae0cSMaor Gottlieb 		       &ib_spec->ipv6.val.dst_ip,
2108026bae0cSMaor Gottlieb 		       sizeof(ib_spec->ipv6.val.dst_ip));
2109466fa6d2SMaor Gottlieb 
21102d1e697eSMoses Reuben 		set_tos(headers_c, headers_v,
2111466fa6d2SMaor Gottlieb 			ib_spec->ipv6.mask.traffic_class,
2112466fa6d2SMaor Gottlieb 			ib_spec->ipv6.val.traffic_class);
2113466fa6d2SMaor Gottlieb 
21142d1e697eSMoses Reuben 		set_proto(headers_c, headers_v,
2115466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.mask.next_hdr,
2116466fa6d2SMaor Gottlieb 			  ib_spec->ipv6.val.next_hdr);
2117466fa6d2SMaor Gottlieb 
21182d1e697eSMoses Reuben 		set_flow_label(misc_params_c, misc_params_v,
21192d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.mask.flow_label),
21202d1e697eSMoses Reuben 			       ntohl(ib_spec->ipv6.val.flow_label),
21212d1e697eSMoses Reuben 			       ib_spec->type & IB_FLOW_SPEC_INNER);
21222d1e697eSMoses Reuben 
2123026bae0cSMaor Gottlieb 		break;
2124038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_TCP:
2125c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2126c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
21271ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2128038d2ef8SMaor Gottlieb 
21292d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2130038d2ef8SMaor Gottlieb 			 0xff);
21312d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2132038d2ef8SMaor Gottlieb 			 IPPROTO_TCP);
2133038d2ef8SMaor Gottlieb 
21342d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
2135038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21362d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
2137038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2138038d2ef8SMaor Gottlieb 
21392d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
2140038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21412d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
2142038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2143038d2ef8SMaor Gottlieb 		break;
2144038d2ef8SMaor Gottlieb 	case IB_FLOW_SPEC_UDP:
2145c47ac6aeSMaor Gottlieb 		if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
2146c47ac6aeSMaor Gottlieb 					 LAST_TCP_UDP_FIELD))
21471ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2148038d2ef8SMaor Gottlieb 
21492d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
2150038d2ef8SMaor Gottlieb 			 0xff);
21512d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
2152038d2ef8SMaor Gottlieb 			 IPPROTO_UDP);
2153038d2ef8SMaor Gottlieb 
21542d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
2155038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.src_port));
21562d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
2157038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.src_port));
2158038d2ef8SMaor Gottlieb 
21592d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
2160038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.mask.dst_port));
21612d1e697eSMoses Reuben 		MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
2162038d2ef8SMaor Gottlieb 			 ntohs(ib_spec->tcp_udp.val.dst_port));
2163038d2ef8SMaor Gottlieb 		break;
2164ffb30d8fSMoses Reuben 	case IB_FLOW_SPEC_VXLAN_TUNNEL:
2165ffb30d8fSMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
2166ffb30d8fSMoses Reuben 					 LAST_TUNNEL_FIELD))
21671ffd3a26SLeon Romanovsky 			return -EOPNOTSUPP;
2168ffb30d8fSMoses Reuben 
2169ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
2170ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.mask.tunnel_id));
2171ffb30d8fSMoses Reuben 		MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
2172ffb30d8fSMoses Reuben 			 ntohl(ib_spec->tunnel.val.tunnel_id));
2173ffb30d8fSMoses Reuben 		break;
21742ac693f9SMoses Reuben 	case IB_FLOW_SPEC_ACTION_TAG:
21752ac693f9SMoses Reuben 		if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
21762ac693f9SMoses Reuben 					 LAST_FLOW_TAG_FIELD))
21772ac693f9SMoses Reuben 			return -EOPNOTSUPP;
21782ac693f9SMoses Reuben 		if (ib_spec->flow_tag.tag_id >= BIT(24))
21792ac693f9SMoses Reuben 			return -EINVAL;
21802ac693f9SMoses Reuben 
21812ac693f9SMoses Reuben 		*tag_id = ib_spec->flow_tag.tag_id;
21822ac693f9SMoses Reuben 		break;
2183a22ed86cSSlava Shwartsman 	case IB_FLOW_SPEC_ACTION_DROP:
2184a22ed86cSSlava Shwartsman 		if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
2185a22ed86cSSlava Shwartsman 					 LAST_DROP_FIELD))
2186a22ed86cSSlava Shwartsman 			return -EOPNOTSUPP;
2187a22ed86cSSlava Shwartsman 		*is_drop = true;
2188a22ed86cSSlava Shwartsman 		break;
2189038d2ef8SMaor Gottlieb 	default:
2190038d2ef8SMaor Gottlieb 		return -EINVAL;
2191038d2ef8SMaor Gottlieb 	}
2192038d2ef8SMaor Gottlieb 
2193038d2ef8SMaor Gottlieb 	return 0;
2194038d2ef8SMaor Gottlieb }
2195038d2ef8SMaor Gottlieb 
2196038d2ef8SMaor Gottlieb /* If a flow could catch both multicast and unicast packets,
2197038d2ef8SMaor Gottlieb  * it won't fall into the multicast flow steering table and this rule
2198038d2ef8SMaor Gottlieb  * could steal other multicast packets.
2199038d2ef8SMaor Gottlieb  */
2200a550ddfcSYishai Hadas static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
2201038d2ef8SMaor Gottlieb {
220281e30880SYishai Hadas 	union ib_flow_spec *flow_spec;
2203038d2ef8SMaor Gottlieb 
2204038d2ef8SMaor Gottlieb 	if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
2205038d2ef8SMaor Gottlieb 	    ib_attr->num_of_specs < 1)
2206038d2ef8SMaor Gottlieb 		return false;
2207038d2ef8SMaor Gottlieb 
220881e30880SYishai Hadas 	flow_spec = (union ib_flow_spec *)(ib_attr + 1);
220981e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
221081e30880SYishai Hadas 		struct ib_flow_spec_ipv4 *ipv4_spec;
2211038d2ef8SMaor Gottlieb 
221281e30880SYishai Hadas 		ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
221381e30880SYishai Hadas 		if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
221481e30880SYishai Hadas 			return true;
221581e30880SYishai Hadas 
221681e30880SYishai Hadas 		return false;
221781e30880SYishai Hadas 	}
221881e30880SYishai Hadas 
221981e30880SYishai Hadas 	if (flow_spec->type == IB_FLOW_SPEC_ETH) {
222081e30880SYishai Hadas 		struct ib_flow_spec_eth *eth_spec;
222181e30880SYishai Hadas 
222281e30880SYishai Hadas 		eth_spec = (struct ib_flow_spec_eth *)flow_spec;
2223038d2ef8SMaor Gottlieb 		return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
2224038d2ef8SMaor Gottlieb 		       is_multicast_ether_addr(eth_spec->val.dst_mac);
2225038d2ef8SMaor Gottlieb 	}
2226038d2ef8SMaor Gottlieb 
222781e30880SYishai Hadas 	return false;
222881e30880SYishai Hadas }
222981e30880SYishai Hadas 
223019cc7524SAriel Levkovich static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
223119cc7524SAriel Levkovich 			       const struct ib_flow_attr *flow_attr,
22320f750966SAriel Levkovich 			       bool check_inner)
2233038d2ef8SMaor Gottlieb {
2234038d2ef8SMaor Gottlieb 	union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
223519cc7524SAriel Levkovich 	int match_ipv = check_inner ?
223619cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
223719cc7524SAriel Levkovich 					ft_field_support.inner_ip_version) :
223819cc7524SAriel Levkovich 			MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
223919cc7524SAriel Levkovich 					ft_field_support.outer_ip_version);
22400f750966SAriel Levkovich 	int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
22410f750966SAriel Levkovich 	bool ipv4_spec_valid, ipv6_spec_valid;
22420f750966SAriel Levkovich 	unsigned int ip_spec_type = 0;
22430f750966SAriel Levkovich 	bool has_ethertype = false;
2244038d2ef8SMaor Gottlieb 	unsigned int spec_index;
22450f750966SAriel Levkovich 	bool mask_valid = true;
22460f750966SAriel Levkovich 	u16 eth_type = 0;
22470f750966SAriel Levkovich 	bool type_valid;
2248038d2ef8SMaor Gottlieb 
2249038d2ef8SMaor Gottlieb 	/* Validate that ethertype is correct */
2250038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
22510f750966SAriel Levkovich 		if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
2252038d2ef8SMaor Gottlieb 		    ib_spec->eth.mask.ether_type) {
22530f750966SAriel Levkovich 			mask_valid = (ib_spec->eth.mask.ether_type ==
22540f750966SAriel Levkovich 				      htons(0xffff));
22550f750966SAriel Levkovich 			has_ethertype = true;
22560f750966SAriel Levkovich 			eth_type = ntohs(ib_spec->eth.val.ether_type);
22570f750966SAriel Levkovich 		} else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
22580f750966SAriel Levkovich 			   (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
22590f750966SAriel Levkovich 			ip_spec_type = ib_spec->type;
2260038d2ef8SMaor Gottlieb 		}
2261038d2ef8SMaor Gottlieb 		ib_spec = (void *)ib_spec + ib_spec->size;
2262038d2ef8SMaor Gottlieb 	}
22630f750966SAriel Levkovich 
22640f750966SAriel Levkovich 	type_valid = (!has_ethertype) || (!ip_spec_type);
22650f750966SAriel Levkovich 	if (!type_valid && mask_valid) {
22660f750966SAriel Levkovich 		ipv4_spec_valid = (eth_type == ETH_P_IP) &&
22670f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
22680f750966SAriel Levkovich 		ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
22690f750966SAriel Levkovich 			(ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
227019cc7524SAriel Levkovich 
227119cc7524SAriel Levkovich 		type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
227219cc7524SAriel Levkovich 			     (((eth_type == ETH_P_MPLS_UC) ||
227319cc7524SAriel Levkovich 			       (eth_type == ETH_P_MPLS_MC)) && match_ipv);
22740f750966SAriel Levkovich 	}
22750f750966SAriel Levkovich 
22760f750966SAriel Levkovich 	return type_valid;
22770f750966SAriel Levkovich }
22780f750966SAriel Levkovich 
227919cc7524SAriel Levkovich static bool is_valid_attr(struct mlx5_core_dev *mdev,
228019cc7524SAriel Levkovich 			  const struct ib_flow_attr *flow_attr)
22810f750966SAriel Levkovich {
228219cc7524SAriel Levkovich 	return is_valid_ethertype(mdev, flow_attr, false) &&
228319cc7524SAriel Levkovich 	       is_valid_ethertype(mdev, flow_attr, true);
2284038d2ef8SMaor Gottlieb }
2285038d2ef8SMaor Gottlieb 
2286038d2ef8SMaor Gottlieb static void put_flow_table(struct mlx5_ib_dev *dev,
2287038d2ef8SMaor Gottlieb 			   struct mlx5_ib_flow_prio *prio, bool ft_added)
2288038d2ef8SMaor Gottlieb {
2289038d2ef8SMaor Gottlieb 	prio->refcount -= !!ft_added;
2290038d2ef8SMaor Gottlieb 	if (!prio->refcount) {
2291038d2ef8SMaor Gottlieb 		mlx5_destroy_flow_table(prio->flow_table);
2292038d2ef8SMaor Gottlieb 		prio->flow_table = NULL;
2293038d2ef8SMaor Gottlieb 	}
2294038d2ef8SMaor Gottlieb }
2295038d2ef8SMaor Gottlieb 
2296038d2ef8SMaor Gottlieb static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
2297038d2ef8SMaor Gottlieb {
2298038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(flow_id->qp->device);
2299038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = container_of(flow_id,
2300038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_handler,
2301038d2ef8SMaor Gottlieb 							  ibflow);
2302038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *iter, *tmp;
2303038d2ef8SMaor Gottlieb 
2304038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2305038d2ef8SMaor Gottlieb 
2306038d2ef8SMaor Gottlieb 	list_for_each_entry_safe(iter, tmp, &handler->list, list) {
230774491de9SMark Bloch 		mlx5_del_flow_rules(iter->rule);
2308cc0e5d42SMaor Gottlieb 		put_flow_table(dev, iter->prio, true);
2309038d2ef8SMaor Gottlieb 		list_del(&iter->list);
2310038d2ef8SMaor Gottlieb 		kfree(iter);
2311038d2ef8SMaor Gottlieb 	}
2312038d2ef8SMaor Gottlieb 
231374491de9SMark Bloch 	mlx5_del_flow_rules(handler->rule);
23145497adc6SMaor Gottlieb 	put_flow_table(dev, handler->prio, true);
2315038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2316038d2ef8SMaor Gottlieb 
2317038d2ef8SMaor Gottlieb 	kfree(handler);
2318038d2ef8SMaor Gottlieb 
2319038d2ef8SMaor Gottlieb 	return 0;
2320038d2ef8SMaor Gottlieb }
2321038d2ef8SMaor Gottlieb 
232235d19011SMaor Gottlieb static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
232335d19011SMaor Gottlieb {
232435d19011SMaor Gottlieb 	priority *= 2;
232535d19011SMaor Gottlieb 	if (!dont_trap)
232635d19011SMaor Gottlieb 		priority++;
232735d19011SMaor Gottlieb 	return priority;
232835d19011SMaor Gottlieb }
232935d19011SMaor Gottlieb 
2330cc0e5d42SMaor Gottlieb enum flow_table_type {
2331cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_RX,
2332cc0e5d42SMaor Gottlieb 	MLX5_IB_FT_TX
2333cc0e5d42SMaor Gottlieb };
2334cc0e5d42SMaor Gottlieb 
233500b7c2abSMaor Gottlieb #define MLX5_FS_MAX_TYPES	 6
233600b7c2abSMaor Gottlieb #define MLX5_FS_MAX_ENTRIES	 BIT(16)
2337038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
2338cc0e5d42SMaor Gottlieb 						struct ib_flow_attr *flow_attr,
2339cc0e5d42SMaor Gottlieb 						enum flow_table_type ft_type)
2340038d2ef8SMaor Gottlieb {
234135d19011SMaor Gottlieb 	bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
2342038d2ef8SMaor Gottlieb 	struct mlx5_flow_namespace *ns = NULL;
2343038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *prio;
2344038d2ef8SMaor Gottlieb 	struct mlx5_flow_table *ft;
2345dac388efSMaor Gottlieb 	int max_table_size;
2346038d2ef8SMaor Gottlieb 	int num_entries;
2347038d2ef8SMaor Gottlieb 	int num_groups;
2348038d2ef8SMaor Gottlieb 	int priority;
2349038d2ef8SMaor Gottlieb 	int err = 0;
2350038d2ef8SMaor Gottlieb 
2351dac388efSMaor Gottlieb 	max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2352dac388efSMaor Gottlieb 						       log_max_ft_size));
2353038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
235435d19011SMaor Gottlieb 		if (flow_is_multicast_only(flow_attr) &&
235535d19011SMaor Gottlieb 		    !dont_trap)
2356038d2ef8SMaor Gottlieb 			priority = MLX5_IB_FLOW_MCAST_PRIO;
2357038d2ef8SMaor Gottlieb 		else
235835d19011SMaor Gottlieb 			priority = ib_prio_to_core_prio(flow_attr->priority,
235935d19011SMaor Gottlieb 							dont_trap);
2360038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2361038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_BYPASS);
2362038d2ef8SMaor Gottlieb 		num_entries = MLX5_FS_MAX_ENTRIES;
2363038d2ef8SMaor Gottlieb 		num_groups = MLX5_FS_MAX_TYPES;
2364038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[priority];
2365038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2366038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2367038d2ef8SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev,
2368038d2ef8SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_LEFTOVERS);
2369038d2ef8SMaor Gottlieb 		build_leftovers_ft_param(&priority,
2370038d2ef8SMaor Gottlieb 					 &num_entries,
2371038d2ef8SMaor Gottlieb 					 &num_groups);
2372038d2ef8SMaor Gottlieb 		prio = &dev->flow_db.prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
2373cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2374cc0e5d42SMaor Gottlieb 		if (!MLX5_CAP_FLOWTABLE(dev->mdev,
2375cc0e5d42SMaor Gottlieb 					allow_sniffer_and_nic_rx_shared_tir))
2376cc0e5d42SMaor Gottlieb 			return ERR_PTR(-ENOTSUPP);
2377cc0e5d42SMaor Gottlieb 
2378cc0e5d42SMaor Gottlieb 		ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
2379cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_RX :
2380cc0e5d42SMaor Gottlieb 					     MLX5_FLOW_NAMESPACE_SNIFFER_TX);
2381cc0e5d42SMaor Gottlieb 
2382cc0e5d42SMaor Gottlieb 		prio = &dev->flow_db.sniffer[ft_type];
2383cc0e5d42SMaor Gottlieb 		priority = 0;
2384cc0e5d42SMaor Gottlieb 		num_entries = 1;
2385cc0e5d42SMaor Gottlieb 		num_groups = 1;
2386038d2ef8SMaor Gottlieb 	}
2387038d2ef8SMaor Gottlieb 
2388038d2ef8SMaor Gottlieb 	if (!ns)
2389038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOTSUPP);
2390038d2ef8SMaor Gottlieb 
2391dac388efSMaor Gottlieb 	if (num_entries > max_table_size)
2392dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2393dac388efSMaor Gottlieb 
2394038d2ef8SMaor Gottlieb 	ft = prio->flow_table;
2395038d2ef8SMaor Gottlieb 	if (!ft) {
2396038d2ef8SMaor Gottlieb 		ft = mlx5_create_auto_grouped_flow_table(ns, priority,
2397038d2ef8SMaor Gottlieb 							 num_entries,
2398d63cd286SMaor Gottlieb 							 num_groups,
2399c9f1b073SHadar Hen Zion 							 0, 0);
2400038d2ef8SMaor Gottlieb 
2401038d2ef8SMaor Gottlieb 		if (!IS_ERR(ft)) {
2402038d2ef8SMaor Gottlieb 			prio->refcount = 0;
2403038d2ef8SMaor Gottlieb 			prio->flow_table = ft;
2404038d2ef8SMaor Gottlieb 		} else {
2405038d2ef8SMaor Gottlieb 			err = PTR_ERR(ft);
2406038d2ef8SMaor Gottlieb 		}
2407038d2ef8SMaor Gottlieb 	}
2408038d2ef8SMaor Gottlieb 
2409038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : prio;
2410038d2ef8SMaor Gottlieb }
2411038d2ef8SMaor Gottlieb 
2412a550ddfcSYishai Hadas static void set_underlay_qp(struct mlx5_ib_dev *dev,
2413a550ddfcSYishai Hadas 			    struct mlx5_flow_spec *spec,
2414a550ddfcSYishai Hadas 			    u32 underlay_qpn)
2415a550ddfcSYishai Hadas {
2416a550ddfcSYishai Hadas 	void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
2417a550ddfcSYishai Hadas 					   spec->match_criteria,
2418a550ddfcSYishai Hadas 					   misc_parameters);
2419a550ddfcSYishai Hadas 	void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
2420a550ddfcSYishai Hadas 					   misc_parameters);
2421a550ddfcSYishai Hadas 
2422a550ddfcSYishai Hadas 	if (underlay_qpn &&
2423a550ddfcSYishai Hadas 	    MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
2424a550ddfcSYishai Hadas 				      ft_field_support.bth_dst_qp)) {
2425a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2426a550ddfcSYishai Hadas 			 misc_params_v, bth_dst_qp, underlay_qpn);
2427a550ddfcSYishai Hadas 		MLX5_SET(fte_match_set_misc,
2428a550ddfcSYishai Hadas 			 misc_params_c, bth_dst_qp, 0xffffff);
2429a550ddfcSYishai Hadas 	}
2430a550ddfcSYishai Hadas }
2431a550ddfcSYishai Hadas 
2432a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
2433038d2ef8SMaor Gottlieb 						      struct mlx5_ib_flow_prio *ft_prio,
2434dd063d0eSMaor Gottlieb 						      const struct ib_flow_attr *flow_attr,
2435a550ddfcSYishai Hadas 						      struct mlx5_flow_destination *dst,
2436a550ddfcSYishai Hadas 						      u32 underlay_qpn)
2437038d2ef8SMaor Gottlieb {
2438038d2ef8SMaor Gottlieb 	struct mlx5_flow_table	*ft = ft_prio->flow_table;
2439038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler;
244066958ed9SHadar Hen Zion 	struct mlx5_flow_act flow_act = {0};
2441c5bb1730SMaor Gottlieb 	struct mlx5_flow_spec *spec;
2442a22ed86cSSlava Shwartsman 	struct mlx5_flow_destination *rule_dst = dst;
2443dd063d0eSMaor Gottlieb 	const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
2444038d2ef8SMaor Gottlieb 	unsigned int spec_index;
24452ac693f9SMoses Reuben 	u32 flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
2446a22ed86cSSlava Shwartsman 	bool is_drop = false;
2447038d2ef8SMaor Gottlieb 	int err = 0;
2448a22ed86cSSlava Shwartsman 	int dest_num = 1;
2449038d2ef8SMaor Gottlieb 
245019cc7524SAriel Levkovich 	if (!is_valid_attr(dev->mdev, flow_attr))
2451038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2452038d2ef8SMaor Gottlieb 
24531b9a07eeSLeon Romanovsky 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
2454038d2ef8SMaor Gottlieb 	handler = kzalloc(sizeof(*handler), GFP_KERNEL);
2455c5bb1730SMaor Gottlieb 	if (!handler || !spec) {
2456038d2ef8SMaor Gottlieb 		err = -ENOMEM;
2457038d2ef8SMaor Gottlieb 		goto free;
2458038d2ef8SMaor Gottlieb 	}
2459038d2ef8SMaor Gottlieb 
2460038d2ef8SMaor Gottlieb 	INIT_LIST_HEAD(&handler->list);
2461038d2ef8SMaor Gottlieb 
2462038d2ef8SMaor Gottlieb 	for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
246319cc7524SAriel Levkovich 		err = parse_flow_attr(dev->mdev, spec->match_criteria,
2464a22ed86cSSlava Shwartsman 				      spec->match_value,
2465a22ed86cSSlava Shwartsman 				      ib_flow, &flow_tag, &is_drop);
2466038d2ef8SMaor Gottlieb 		if (err < 0)
2467038d2ef8SMaor Gottlieb 			goto free;
2468038d2ef8SMaor Gottlieb 
2469038d2ef8SMaor Gottlieb 		ib_flow += ((union ib_flow_spec *)ib_flow)->size;
2470038d2ef8SMaor Gottlieb 	}
2471038d2ef8SMaor Gottlieb 
2472a550ddfcSYishai Hadas 	if (!flow_is_multicast_only(flow_attr))
2473a550ddfcSYishai Hadas 		set_underlay_qp(dev, spec, underlay_qpn);
2474a550ddfcSYishai Hadas 
2475466fa6d2SMaor Gottlieb 	spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
2476a22ed86cSSlava Shwartsman 	if (is_drop) {
2477a22ed86cSSlava Shwartsman 		flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
2478a22ed86cSSlava Shwartsman 		rule_dst = NULL;
2479a22ed86cSSlava Shwartsman 		dest_num = 0;
2480a22ed86cSSlava Shwartsman 	} else {
248166958ed9SHadar Hen Zion 		flow_act.action = dst ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
248235d19011SMaor Gottlieb 		    MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
2483a22ed86cSSlava Shwartsman 	}
24842ac693f9SMoses Reuben 
24852ac693f9SMoses Reuben 	if (flow_tag != MLX5_FS_DEFAULT_FLOW_TAG &&
24862ac693f9SMoses Reuben 	    (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
24872ac693f9SMoses Reuben 	     flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
24882ac693f9SMoses Reuben 		mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
24892ac693f9SMoses Reuben 			     flow_tag, flow_attr->type);
24902ac693f9SMoses Reuben 		err = -EINVAL;
24912ac693f9SMoses Reuben 		goto free;
24922ac693f9SMoses Reuben 	}
24932ac693f9SMoses Reuben 	flow_act.flow_tag = flow_tag;
249474491de9SMark Bloch 	handler->rule = mlx5_add_flow_rules(ft, spec,
249566958ed9SHadar Hen Zion 					    &flow_act,
2496a22ed86cSSlava Shwartsman 					    rule_dst, dest_num);
2497038d2ef8SMaor Gottlieb 
2498038d2ef8SMaor Gottlieb 	if (IS_ERR(handler->rule)) {
2499038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler->rule);
2500038d2ef8SMaor Gottlieb 		goto free;
2501038d2ef8SMaor Gottlieb 	}
2502038d2ef8SMaor Gottlieb 
2503d9d4980aSMaor Gottlieb 	ft_prio->refcount++;
25045497adc6SMaor Gottlieb 	handler->prio = ft_prio;
2505038d2ef8SMaor Gottlieb 
2506038d2ef8SMaor Gottlieb 	ft_prio->flow_table = ft;
2507038d2ef8SMaor Gottlieb free:
2508038d2ef8SMaor Gottlieb 	if (err)
2509038d2ef8SMaor Gottlieb 		kfree(handler);
2510c5bb1730SMaor Gottlieb 	kvfree(spec);
2511038d2ef8SMaor Gottlieb 	return err ? ERR_PTR(err) : handler;
2512038d2ef8SMaor Gottlieb }
2513038d2ef8SMaor Gottlieb 
2514a550ddfcSYishai Hadas static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
2515a550ddfcSYishai Hadas 						     struct mlx5_ib_flow_prio *ft_prio,
2516a550ddfcSYishai Hadas 						     const struct ib_flow_attr *flow_attr,
2517a550ddfcSYishai Hadas 						     struct mlx5_flow_destination *dst)
2518a550ddfcSYishai Hadas {
2519a550ddfcSYishai Hadas 	return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0);
2520a550ddfcSYishai Hadas }
2521a550ddfcSYishai Hadas 
252235d19011SMaor Gottlieb static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
252335d19011SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
252435d19011SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
252535d19011SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
252635d19011SMaor Gottlieb {
252735d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_dst = NULL;
252835d19011SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
252935d19011SMaor Gottlieb 
253035d19011SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
253135d19011SMaor Gottlieb 	if (!IS_ERR(handler)) {
253235d19011SMaor Gottlieb 		handler_dst = create_flow_rule(dev, ft_prio,
253335d19011SMaor Gottlieb 					       flow_attr, dst);
253435d19011SMaor Gottlieb 		if (IS_ERR(handler_dst)) {
253574491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2536d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
253735d19011SMaor Gottlieb 			kfree(handler);
253835d19011SMaor Gottlieb 			handler = handler_dst;
253935d19011SMaor Gottlieb 		} else {
254035d19011SMaor Gottlieb 			list_add(&handler_dst->list, &handler->list);
254135d19011SMaor Gottlieb 		}
254235d19011SMaor Gottlieb 	}
254335d19011SMaor Gottlieb 
254435d19011SMaor Gottlieb 	return handler;
254535d19011SMaor Gottlieb }
2546038d2ef8SMaor Gottlieb enum {
2547038d2ef8SMaor Gottlieb 	LEFTOVERS_MC,
2548038d2ef8SMaor Gottlieb 	LEFTOVERS_UC,
2549038d2ef8SMaor Gottlieb };
2550038d2ef8SMaor Gottlieb 
2551038d2ef8SMaor Gottlieb static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
2552038d2ef8SMaor Gottlieb 							  struct mlx5_ib_flow_prio *ft_prio,
2553038d2ef8SMaor Gottlieb 							  struct ib_flow_attr *flow_attr,
2554038d2ef8SMaor Gottlieb 							  struct mlx5_flow_destination *dst)
2555038d2ef8SMaor Gottlieb {
2556038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_ucast = NULL;
2557038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2558038d2ef8SMaor Gottlieb 
2559038d2ef8SMaor Gottlieb 	static struct {
2560038d2ef8SMaor Gottlieb 		struct ib_flow_attr	flow_attr;
2561038d2ef8SMaor Gottlieb 		struct ib_flow_spec_eth eth_flow;
2562038d2ef8SMaor Gottlieb 	} leftovers_specs[] = {
2563038d2ef8SMaor Gottlieb 		[LEFTOVERS_MC] = {
2564038d2ef8SMaor Gottlieb 			.flow_attr = {
2565038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2566038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2567038d2ef8SMaor Gottlieb 			},
2568038d2ef8SMaor Gottlieb 			.eth_flow = {
2569038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2570038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2571038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2572038d2ef8SMaor Gottlieb 				.val =  {.dst_mac = {0x1} }
2573038d2ef8SMaor Gottlieb 			}
2574038d2ef8SMaor Gottlieb 		},
2575038d2ef8SMaor Gottlieb 		[LEFTOVERS_UC] = {
2576038d2ef8SMaor Gottlieb 			.flow_attr = {
2577038d2ef8SMaor Gottlieb 				.num_of_specs = 1,
2578038d2ef8SMaor Gottlieb 				.size = sizeof(leftovers_specs[0])
2579038d2ef8SMaor Gottlieb 			},
2580038d2ef8SMaor Gottlieb 			.eth_flow = {
2581038d2ef8SMaor Gottlieb 				.type = IB_FLOW_SPEC_ETH,
2582038d2ef8SMaor Gottlieb 				.size = sizeof(struct ib_flow_spec_eth),
2583038d2ef8SMaor Gottlieb 				.mask = {.dst_mac = {0x1} },
2584038d2ef8SMaor Gottlieb 				.val = {.dst_mac = {} }
2585038d2ef8SMaor Gottlieb 			}
2586038d2ef8SMaor Gottlieb 		}
2587038d2ef8SMaor Gottlieb 	};
2588038d2ef8SMaor Gottlieb 
2589038d2ef8SMaor Gottlieb 	handler = create_flow_rule(dev, ft_prio,
2590038d2ef8SMaor Gottlieb 				   &leftovers_specs[LEFTOVERS_MC].flow_attr,
2591038d2ef8SMaor Gottlieb 				   dst);
2592038d2ef8SMaor Gottlieb 	if (!IS_ERR(handler) &&
2593038d2ef8SMaor Gottlieb 	    flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
2594038d2ef8SMaor Gottlieb 		handler_ucast = create_flow_rule(dev, ft_prio,
2595038d2ef8SMaor Gottlieb 						 &leftovers_specs[LEFTOVERS_UC].flow_attr,
2596038d2ef8SMaor Gottlieb 						 dst);
2597038d2ef8SMaor Gottlieb 		if (IS_ERR(handler_ucast)) {
259874491de9SMark Bloch 			mlx5_del_flow_rules(handler->rule);
2599d9d4980aSMaor Gottlieb 			ft_prio->refcount--;
2600038d2ef8SMaor Gottlieb 			kfree(handler);
2601038d2ef8SMaor Gottlieb 			handler = handler_ucast;
2602038d2ef8SMaor Gottlieb 		} else {
2603038d2ef8SMaor Gottlieb 			list_add(&handler_ucast->list, &handler->list);
2604038d2ef8SMaor Gottlieb 		}
2605038d2ef8SMaor Gottlieb 	}
2606038d2ef8SMaor Gottlieb 
2607038d2ef8SMaor Gottlieb 	return handler;
2608038d2ef8SMaor Gottlieb }
2609038d2ef8SMaor Gottlieb 
2610cc0e5d42SMaor Gottlieb static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
2611cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_rx,
2612cc0e5d42SMaor Gottlieb 							struct mlx5_ib_flow_prio *ft_tx,
2613cc0e5d42SMaor Gottlieb 							struct mlx5_flow_destination *dst)
2614cc0e5d42SMaor Gottlieb {
2615cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_rx;
2616cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler_tx;
2617cc0e5d42SMaor Gottlieb 	int err;
2618cc0e5d42SMaor Gottlieb 	static const struct ib_flow_attr flow_attr  = {
2619cc0e5d42SMaor Gottlieb 		.num_of_specs = 0,
2620cc0e5d42SMaor Gottlieb 		.size = sizeof(flow_attr)
2621cc0e5d42SMaor Gottlieb 	};
2622cc0e5d42SMaor Gottlieb 
2623cc0e5d42SMaor Gottlieb 	handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
2624cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_rx)) {
2625cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_rx);
2626cc0e5d42SMaor Gottlieb 		goto err;
2627cc0e5d42SMaor Gottlieb 	}
2628cc0e5d42SMaor Gottlieb 
2629cc0e5d42SMaor Gottlieb 	handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
2630cc0e5d42SMaor Gottlieb 	if (IS_ERR(handler_tx)) {
2631cc0e5d42SMaor Gottlieb 		err = PTR_ERR(handler_tx);
2632cc0e5d42SMaor Gottlieb 		goto err_tx;
2633cc0e5d42SMaor Gottlieb 	}
2634cc0e5d42SMaor Gottlieb 
2635cc0e5d42SMaor Gottlieb 	list_add(&handler_tx->list, &handler_rx->list);
2636cc0e5d42SMaor Gottlieb 
2637cc0e5d42SMaor Gottlieb 	return handler_rx;
2638cc0e5d42SMaor Gottlieb 
2639cc0e5d42SMaor Gottlieb err_tx:
264074491de9SMark Bloch 	mlx5_del_flow_rules(handler_rx->rule);
2641cc0e5d42SMaor Gottlieb 	ft_rx->refcount--;
2642cc0e5d42SMaor Gottlieb 	kfree(handler_rx);
2643cc0e5d42SMaor Gottlieb err:
2644cc0e5d42SMaor Gottlieb 	return ERR_PTR(err);
2645cc0e5d42SMaor Gottlieb }
2646cc0e5d42SMaor Gottlieb 
2647038d2ef8SMaor Gottlieb static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
2648038d2ef8SMaor Gottlieb 					   struct ib_flow_attr *flow_attr,
2649038d2ef8SMaor Gottlieb 					   int domain)
2650038d2ef8SMaor Gottlieb {
2651038d2ef8SMaor Gottlieb 	struct mlx5_ib_dev *dev = to_mdev(qp->device);
2652d9f88e5aSYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(qp);
2653038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_handler *handler = NULL;
2654038d2ef8SMaor Gottlieb 	struct mlx5_flow_destination *dst = NULL;
2655cc0e5d42SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
2656038d2ef8SMaor Gottlieb 	struct mlx5_ib_flow_prio *ft_prio;
2657038d2ef8SMaor Gottlieb 	int err;
2658a550ddfcSYishai Hadas 	int underlay_qpn;
2659038d2ef8SMaor Gottlieb 
2660038d2ef8SMaor Gottlieb 	if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO)
2661dac388efSMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2662038d2ef8SMaor Gottlieb 
2663038d2ef8SMaor Gottlieb 	if (domain != IB_FLOW_DOMAIN_USER ||
2664038d2ef8SMaor Gottlieb 	    flow_attr->port > MLX5_CAP_GEN(dev->mdev, num_ports) ||
266535d19011SMaor Gottlieb 	    (flow_attr->flags & ~IB_FLOW_ATTR_FLAGS_DONT_TRAP))
2666038d2ef8SMaor Gottlieb 		return ERR_PTR(-EINVAL);
2667038d2ef8SMaor Gottlieb 
2668038d2ef8SMaor Gottlieb 	dst = kzalloc(sizeof(*dst), GFP_KERNEL);
2669038d2ef8SMaor Gottlieb 	if (!dst)
2670038d2ef8SMaor Gottlieb 		return ERR_PTR(-ENOMEM);
2671038d2ef8SMaor Gottlieb 
2672038d2ef8SMaor Gottlieb 	mutex_lock(&dev->flow_db.lock);
2673038d2ef8SMaor Gottlieb 
2674cc0e5d42SMaor Gottlieb 	ft_prio = get_flow_table(dev, flow_attr, MLX5_IB_FT_RX);
2675038d2ef8SMaor Gottlieb 	if (IS_ERR(ft_prio)) {
2676038d2ef8SMaor Gottlieb 		err = PTR_ERR(ft_prio);
2677038d2ef8SMaor Gottlieb 		goto unlock;
2678038d2ef8SMaor Gottlieb 	}
2679cc0e5d42SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2680cc0e5d42SMaor Gottlieb 		ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
2681cc0e5d42SMaor Gottlieb 		if (IS_ERR(ft_prio_tx)) {
2682cc0e5d42SMaor Gottlieb 			err = PTR_ERR(ft_prio_tx);
2683cc0e5d42SMaor Gottlieb 			ft_prio_tx = NULL;
2684cc0e5d42SMaor Gottlieb 			goto destroy_ft;
2685cc0e5d42SMaor Gottlieb 		}
2686cc0e5d42SMaor Gottlieb 	}
2687038d2ef8SMaor Gottlieb 
2688038d2ef8SMaor Gottlieb 	dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
2689d9f88e5aSYishai Hadas 	if (mqp->flags & MLX5_IB_QP_RSS)
2690d9f88e5aSYishai Hadas 		dst->tir_num = mqp->rss_qp.tirn;
2691d9f88e5aSYishai Hadas 	else
2692d9f88e5aSYishai Hadas 		dst->tir_num = mqp->raw_packet_qp.rq.tirn;
2693038d2ef8SMaor Gottlieb 
2694038d2ef8SMaor Gottlieb 	if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
269535d19011SMaor Gottlieb 		if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP)  {
269635d19011SMaor Gottlieb 			handler = create_dont_trap_rule(dev, ft_prio,
269735d19011SMaor Gottlieb 							flow_attr, dst);
269835d19011SMaor Gottlieb 		} else {
2699a550ddfcSYishai Hadas 			underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
2700a550ddfcSYishai Hadas 					mqp->underlay_qpn : 0;
2701a550ddfcSYishai Hadas 			handler = _create_flow_rule(dev, ft_prio, flow_attr,
2702a550ddfcSYishai Hadas 						    dst, underlay_qpn);
270335d19011SMaor Gottlieb 		}
2704038d2ef8SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
2705038d2ef8SMaor Gottlieb 		   flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
2706038d2ef8SMaor Gottlieb 		handler = create_leftovers_rule(dev, ft_prio, flow_attr,
2707038d2ef8SMaor Gottlieb 						dst);
2708cc0e5d42SMaor Gottlieb 	} else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
2709cc0e5d42SMaor Gottlieb 		handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
2710038d2ef8SMaor Gottlieb 	} else {
2711038d2ef8SMaor Gottlieb 		err = -EINVAL;
2712038d2ef8SMaor Gottlieb 		goto destroy_ft;
2713038d2ef8SMaor Gottlieb 	}
2714038d2ef8SMaor Gottlieb 
2715038d2ef8SMaor Gottlieb 	if (IS_ERR(handler)) {
2716038d2ef8SMaor Gottlieb 		err = PTR_ERR(handler);
2717038d2ef8SMaor Gottlieb 		handler = NULL;
2718038d2ef8SMaor Gottlieb 		goto destroy_ft;
2719038d2ef8SMaor Gottlieb 	}
2720038d2ef8SMaor Gottlieb 
2721038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2722038d2ef8SMaor Gottlieb 	kfree(dst);
2723038d2ef8SMaor Gottlieb 
2724038d2ef8SMaor Gottlieb 	return &handler->ibflow;
2725038d2ef8SMaor Gottlieb 
2726038d2ef8SMaor Gottlieb destroy_ft:
2727038d2ef8SMaor Gottlieb 	put_flow_table(dev, ft_prio, false);
2728cc0e5d42SMaor Gottlieb 	if (ft_prio_tx)
2729cc0e5d42SMaor Gottlieb 		put_flow_table(dev, ft_prio_tx, false);
2730038d2ef8SMaor Gottlieb unlock:
2731038d2ef8SMaor Gottlieb 	mutex_unlock(&dev->flow_db.lock);
2732038d2ef8SMaor Gottlieb 	kfree(dst);
2733038d2ef8SMaor Gottlieb 	kfree(handler);
2734038d2ef8SMaor Gottlieb 	return ERR_PTR(err);
2735038d2ef8SMaor Gottlieb }
2736038d2ef8SMaor Gottlieb 
2737e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2738e126ba97SEli Cohen {
2739e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
274081e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2741e126ba97SEli Cohen 	int err;
2742e126ba97SEli Cohen 
274381e30880SYishai Hadas 	if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
274481e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
274581e30880SYishai Hadas 		return -EOPNOTSUPP;
274681e30880SYishai Hadas 	}
274781e30880SYishai Hadas 
27489603b61dSJack Morgenstein 	err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
2749e126ba97SEli Cohen 	if (err)
2750e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2751e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2752e126ba97SEli Cohen 
2753e126ba97SEli Cohen 	return err;
2754e126ba97SEli Cohen }
2755e126ba97SEli Cohen 
2756e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2757e126ba97SEli Cohen {
2758e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2759e126ba97SEli Cohen 	int err;
2760e126ba97SEli Cohen 
27619603b61dSJack Morgenstein 	err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
2762e126ba97SEli Cohen 	if (err)
2763e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2764e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2765e126ba97SEli Cohen 
2766e126ba97SEli Cohen 	return err;
2767e126ba97SEli Cohen }
2768e126ba97SEli Cohen 
2769e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2770e126ba97SEli Cohen {
27711b5daf11SMajd Dibbiny 	int err;
2772e126ba97SEli Cohen 
27731b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2774e126ba97SEli Cohen 	if (err)
2775e126ba97SEli Cohen 		return err;
27761b5daf11SMajd Dibbiny 
27771b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
27781b5daf11SMajd Dibbiny 
27791b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2780e126ba97SEli Cohen }
2781e126ba97SEli Cohen 
2782e126ba97SEli Cohen static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
2783e126ba97SEli Cohen 			     char *buf)
2784e126ba97SEli Cohen {
2785e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2786e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2787e126ba97SEli Cohen 
27889603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2789e126ba97SEli Cohen }
2790e126ba97SEli Cohen 
2791e126ba97SEli Cohen static ssize_t show_reg_pages(struct device *device,
2792e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2793e126ba97SEli Cohen {
2794e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2795e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2796e126ba97SEli Cohen 
27976aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2798e126ba97SEli Cohen }
2799e126ba97SEli Cohen 
2800e126ba97SEli Cohen static ssize_t show_hca(struct device *device, struct device_attribute *attr,
2801e126ba97SEli Cohen 			char *buf)
2802e126ba97SEli Cohen {
2803e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2804e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
28059603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2806e126ba97SEli Cohen }
2807e126ba97SEli Cohen 
2808e126ba97SEli Cohen static ssize_t show_rev(struct device *device, struct device_attribute *attr,
2809e126ba97SEli Cohen 			char *buf)
2810e126ba97SEli Cohen {
2811e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2812e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
28139603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2814e126ba97SEli Cohen }
2815e126ba97SEli Cohen 
2816e126ba97SEli Cohen static ssize_t show_board(struct device *device, struct device_attribute *attr,
2817e126ba97SEli Cohen 			  char *buf)
2818e126ba97SEli Cohen {
2819e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
2820e126ba97SEli Cohen 		container_of(device, struct mlx5_ib_dev, ib_dev.dev);
2821e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
28229603b61dSJack Morgenstein 		       dev->mdev->board_id);
2823e126ba97SEli Cohen }
2824e126ba97SEli Cohen 
2825e126ba97SEli Cohen static DEVICE_ATTR(hw_rev,   S_IRUGO, show_rev,    NULL);
2826e126ba97SEli Cohen static DEVICE_ATTR(hca_type, S_IRUGO, show_hca,    NULL);
2827e126ba97SEli Cohen static DEVICE_ATTR(board_id, S_IRUGO, show_board,  NULL);
2828e126ba97SEli Cohen static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
2829e126ba97SEli Cohen static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
2830e126ba97SEli Cohen 
2831e126ba97SEli Cohen static struct device_attribute *mlx5_class_attributes[] = {
2832e126ba97SEli Cohen 	&dev_attr_hw_rev,
2833e126ba97SEli Cohen 	&dev_attr_hca_type,
2834e126ba97SEli Cohen 	&dev_attr_board_id,
2835e126ba97SEli Cohen 	&dev_attr_fw_pages,
2836e126ba97SEli Cohen 	&dev_attr_reg_pages,
2837e126ba97SEli Cohen };
2838e126ba97SEli Cohen 
28397722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
28407722f47eSHaggai Eran {
28417722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
28427722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
28437722f47eSHaggai Eran 			     pkey_change_work);
28447722f47eSHaggai Eran 
28457722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
28467722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
28477722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
28487722f47eSHaggai Eran }
28497722f47eSHaggai Eran 
285089ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
285189ea94a7SMaor Gottlieb {
285289ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
285389ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
285489ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
285589ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
285689ea94a7SMaor Gottlieb 	unsigned long flags_qp;
285789ea94a7SMaor Gottlieb 	unsigned long flags_cq;
285889ea94a7SMaor Gottlieb 	unsigned long flags;
285989ea94a7SMaor Gottlieb 
286089ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
286189ea94a7SMaor Gottlieb 
286289ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
286389ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
286489ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
286589ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
286689ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
286789ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
286889ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
286989ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
287089ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
287189ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
287289ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
287389ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
287489ea94a7SMaor Gottlieb 						      &cq_armed_list);
287589ea94a7SMaor Gottlieb 				}
287689ea94a7SMaor Gottlieb 			}
287789ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
287889ea94a7SMaor Gottlieb 		}
287989ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
288089ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
288189ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
288289ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
288389ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
288489ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
288589ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
288689ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
288789ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
288889ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
288989ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
289089ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
289189ea94a7SMaor Gottlieb 							      &cq_armed_list);
289289ea94a7SMaor Gottlieb 					}
289389ea94a7SMaor Gottlieb 				}
289489ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
289589ea94a7SMaor Gottlieb 						       flags_cq);
289689ea94a7SMaor Gottlieb 			}
289789ea94a7SMaor Gottlieb 		}
289889ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
289989ea94a7SMaor Gottlieb 	}
290089ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
290189ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
290289ea94a7SMaor Gottlieb 	 */
290389ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
290489ea94a7SMaor Gottlieb 		mcq->comp(mcq);
290589ea94a7SMaor Gottlieb 	}
290689ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
290789ea94a7SMaor Gottlieb }
290889ea94a7SMaor Gottlieb 
290903404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
291003404e8aSMaor Gottlieb {
291103404e8aSMaor Gottlieb 	int err;
291203404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
291303404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
291403404e8aSMaor Gottlieb 			     delay_drop_work);
291503404e8aSMaor Gottlieb 
2916fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2917fe248c3aSMaor Gottlieb 
291803404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
291903404e8aSMaor Gottlieb 	err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
292003404e8aSMaor Gottlieb 				       delay_drop->timeout);
292103404e8aSMaor Gottlieb 	if (err) {
292203404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
292303404e8aSMaor Gottlieb 			     delay_drop->timeout);
292403404e8aSMaor Gottlieb 		delay_drop->activate = false;
292503404e8aSMaor Gottlieb 	}
292603404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
292703404e8aSMaor Gottlieb }
292803404e8aSMaor Gottlieb 
29299603b61dSJack Morgenstein static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
29304d2f9bbbSJack Morgenstein 			  enum mlx5_dev_event event, unsigned long param)
2931e126ba97SEli Cohen {
29329603b61dSJack Morgenstein 	struct mlx5_ib_dev *ibdev = (struct mlx5_ib_dev *)context;
2933e126ba97SEli Cohen 	struct ib_event ibev;
2934dbaaff2aSEli Cohen 	bool fatal = false;
2935e126ba97SEli Cohen 	u8 port = 0;
2936e126ba97SEli Cohen 
2937e126ba97SEli Cohen 	switch (event) {
2938e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2939e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
294089ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2941dbaaff2aSEli Cohen 		fatal = true;
2942e126ba97SEli Cohen 		break;
2943e126ba97SEli Cohen 
2944e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_UP:
2945e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PORT_DOWN:
29462788cf3bSNoa Osherovich 	case MLX5_DEV_EVENT_PORT_INITIALIZED:
29474d2f9bbbSJack Morgenstein 		port = (u8)param;
29485ec8c83eSAviv Heller 
29495ec8c83eSAviv Heller 		/* In RoCE, port up/down events are handled in
29505ec8c83eSAviv Heller 		 * mlx5_netdev_event().
29515ec8c83eSAviv Heller 		 */
29525ec8c83eSAviv Heller 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
29535ec8c83eSAviv Heller 			IB_LINK_LAYER_ETHERNET)
29545ec8c83eSAviv Heller 			return;
29555ec8c83eSAviv Heller 
29565ec8c83eSAviv Heller 		ibev.event = (event == MLX5_DEV_EVENT_PORT_UP) ?
29575ec8c83eSAviv Heller 			     IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2958e126ba97SEli Cohen 		break;
2959e126ba97SEli Cohen 
2960e126ba97SEli Cohen 	case MLX5_DEV_EVENT_LID_CHANGE:
2961e126ba97SEli Cohen 		ibev.event = IB_EVENT_LID_CHANGE;
29624d2f9bbbSJack Morgenstein 		port = (u8)param;
2963e126ba97SEli Cohen 		break;
2964e126ba97SEli Cohen 
2965e126ba97SEli Cohen 	case MLX5_DEV_EVENT_PKEY_CHANGE:
2966e126ba97SEli Cohen 		ibev.event = IB_EVENT_PKEY_CHANGE;
29674d2f9bbbSJack Morgenstein 		port = (u8)param;
29687722f47eSHaggai Eran 
29697722f47eSHaggai Eran 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2970e126ba97SEli Cohen 		break;
2971e126ba97SEli Cohen 
2972e126ba97SEli Cohen 	case MLX5_DEV_EVENT_GUID_CHANGE:
2973e126ba97SEli Cohen 		ibev.event = IB_EVENT_GID_CHANGE;
29744d2f9bbbSJack Morgenstein 		port = (u8)param;
2975e126ba97SEli Cohen 		break;
2976e126ba97SEli Cohen 
2977e126ba97SEli Cohen 	case MLX5_DEV_EVENT_CLIENT_REREG:
2978e126ba97SEli Cohen 		ibev.event = IB_EVENT_CLIENT_REREGISTER;
29794d2f9bbbSJack Morgenstein 		port = (u8)param;
2980e126ba97SEli Cohen 		break;
298103404e8aSMaor Gottlieb 	case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
298203404e8aSMaor Gottlieb 		schedule_work(&ibdev->delay_drop.delay_drop_work);
298303404e8aSMaor Gottlieb 		goto out;
2984bdc37924SSaeed Mahameed 	default:
298503404e8aSMaor Gottlieb 		goto out;
2986e126ba97SEli Cohen 	}
2987e126ba97SEli Cohen 
2988e126ba97SEli Cohen 	ibev.device	      = &ibdev->ib_dev;
2989e126ba97SEli Cohen 	ibev.element.port_num = port;
2990e126ba97SEli Cohen 
2991a0c84c32SEli Cohen 	if (port < 1 || port > ibdev->num_ports) {
2992a0c84c32SEli Cohen 		mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
299303404e8aSMaor Gottlieb 		goto out;
2994a0c84c32SEli Cohen 	}
2995a0c84c32SEli Cohen 
2996e126ba97SEli Cohen 	if (ibdev->ib_active)
2997e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2998dbaaff2aSEli Cohen 
2999dbaaff2aSEli Cohen 	if (fatal)
3000dbaaff2aSEli Cohen 		ibdev->ib_active = false;
300103404e8aSMaor Gottlieb 
300203404e8aSMaor Gottlieb out:
300303404e8aSMaor Gottlieb 	return;
3004e126ba97SEli Cohen }
3005e126ba97SEli Cohen 
3006c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
3007c43f1112SMaor Gottlieb {
3008c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
3009c43f1112SMaor Gottlieb 	int err;
3010c43f1112SMaor Gottlieb 	int port;
3011c43f1112SMaor Gottlieb 
3012c43f1112SMaor Gottlieb 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3013c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
3014c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
3015c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
3016c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
3017c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
3018c43f1112SMaor Gottlieb 								   port, 0,
3019c43f1112SMaor Gottlieb 								   &vport_ctx);
3020c43f1112SMaor Gottlieb 				if (err) {
3021c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
3022c43f1112SMaor Gottlieb 						    port, err);
3023c43f1112SMaor Gottlieb 					return err;
3024c43f1112SMaor Gottlieb 				}
3025c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
3026c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
3027c43f1112SMaor Gottlieb 			} else {
3028c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
3029c43f1112SMaor Gottlieb 			}
3030c43f1112SMaor Gottlieb 		}
3031c43f1112SMaor Gottlieb 	}
3032c43f1112SMaor Gottlieb 	return 0;
3033c43f1112SMaor Gottlieb }
3034c43f1112SMaor Gottlieb 
3035e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
3036e126ba97SEli Cohen {
3037e126ba97SEli Cohen 	int port;
3038e126ba97SEli Cohen 
3039938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++)
3040e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
3041e126ba97SEli Cohen }
3042e126ba97SEli Cohen 
3043e126ba97SEli Cohen static int get_port_caps(struct mlx5_ib_dev *dev)
3044e126ba97SEli Cohen {
3045e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
3046e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
3047f614fc15SDan Carpenter 	int err = -ENOMEM;
3048e126ba97SEli Cohen 	int port;
30492528e33eSMatan Barak 	struct ib_udata uhw = {.inlen = 0, .outlen = 0};
3050e126ba97SEli Cohen 
3051e126ba97SEli Cohen 	pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
3052e126ba97SEli Cohen 	if (!pprops)
3053e126ba97SEli Cohen 		goto out;
3054e126ba97SEli Cohen 
3055e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
3056e126ba97SEli Cohen 	if (!dprops)
3057e126ba97SEli Cohen 		goto out;
3058e126ba97SEli Cohen 
3059c43f1112SMaor Gottlieb 	err = set_has_smi_cap(dev);
3060c43f1112SMaor Gottlieb 	if (err)
3061c43f1112SMaor Gottlieb 		goto out;
3062c43f1112SMaor Gottlieb 
30632528e33eSMatan Barak 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
3064e126ba97SEli Cohen 	if (err) {
3065e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
3066e126ba97SEli Cohen 		goto out;
3067e126ba97SEli Cohen 	}
3068e126ba97SEli Cohen 
3069938fe83cSSaeed Mahameed 	for (port = 1; port <= MLX5_CAP_GEN(dev->mdev, num_ports); port++) {
3070c4550c63SOr Gerlitz 		memset(pprops, 0, sizeof(*pprops));
3071e126ba97SEli Cohen 		err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
3072e126ba97SEli Cohen 		if (err) {
3073938fe83cSSaeed Mahameed 			mlx5_ib_warn(dev, "query_port %d failed %d\n",
3074938fe83cSSaeed Mahameed 				     port, err);
3075e126ba97SEli Cohen 			break;
3076e126ba97SEli Cohen 		}
3077938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].pkey_table_len =
3078938fe83cSSaeed Mahameed 						dprops->max_pkeys;
3079938fe83cSSaeed Mahameed 		dev->mdev->port_caps[port - 1].gid_table_len =
3080938fe83cSSaeed Mahameed 						pprops->gid_tbl_len;
3081e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "pkey_table_len %d, gid_table_len %d\n",
3082e126ba97SEli Cohen 			    dprops->max_pkeys, pprops->gid_tbl_len);
3083e126ba97SEli Cohen 	}
3084e126ba97SEli Cohen 
3085e126ba97SEli Cohen out:
3086e126ba97SEli Cohen 	kfree(pprops);
3087e126ba97SEli Cohen 	kfree(dprops);
3088e126ba97SEli Cohen 
3089e126ba97SEli Cohen 	return err;
3090e126ba97SEli Cohen }
3091e126ba97SEli Cohen 
3092e126ba97SEli Cohen static void destroy_umrc_res(struct mlx5_ib_dev *dev)
3093e126ba97SEli Cohen {
3094e126ba97SEli Cohen 	int err;
3095e126ba97SEli Cohen 
3096e126ba97SEli Cohen 	err = mlx5_mr_cache_cleanup(dev);
3097e126ba97SEli Cohen 	if (err)
3098e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
3099e126ba97SEli Cohen 
3100e126ba97SEli Cohen 	mlx5_ib_destroy_qp(dev->umrc.qp);
3101add08d76SChristoph Hellwig 	ib_free_cq(dev->umrc.cq);
3102e126ba97SEli Cohen 	ib_dealloc_pd(dev->umrc.pd);
3103e126ba97SEli Cohen }
3104e126ba97SEli Cohen 
3105e126ba97SEli Cohen enum {
3106e126ba97SEli Cohen 	MAX_UMR_WR = 128,
3107e126ba97SEli Cohen };
3108e126ba97SEli Cohen 
3109e126ba97SEli Cohen static int create_umr_res(struct mlx5_ib_dev *dev)
3110e126ba97SEli Cohen {
3111e126ba97SEli Cohen 	struct ib_qp_init_attr *init_attr = NULL;
3112e126ba97SEli Cohen 	struct ib_qp_attr *attr = NULL;
3113e126ba97SEli Cohen 	struct ib_pd *pd;
3114e126ba97SEli Cohen 	struct ib_cq *cq;
3115e126ba97SEli Cohen 	struct ib_qp *qp;
3116e126ba97SEli Cohen 	int ret;
3117e126ba97SEli Cohen 
3118e126ba97SEli Cohen 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
3119e126ba97SEli Cohen 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
3120e126ba97SEli Cohen 	if (!attr || !init_attr) {
3121e126ba97SEli Cohen 		ret = -ENOMEM;
3122e126ba97SEli Cohen 		goto error_0;
3123e126ba97SEli Cohen 	}
3124e126ba97SEli Cohen 
3125ed082d36SChristoph Hellwig 	pd = ib_alloc_pd(&dev->ib_dev, 0);
3126e126ba97SEli Cohen 	if (IS_ERR(pd)) {
3127e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
3128e126ba97SEli Cohen 		ret = PTR_ERR(pd);
3129e126ba97SEli Cohen 		goto error_0;
3130e126ba97SEli Cohen 	}
3131e126ba97SEli Cohen 
3132add08d76SChristoph Hellwig 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
3133e126ba97SEli Cohen 	if (IS_ERR(cq)) {
3134e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
3135e126ba97SEli Cohen 		ret = PTR_ERR(cq);
3136e126ba97SEli Cohen 		goto error_2;
3137e126ba97SEli Cohen 	}
3138e126ba97SEli Cohen 
3139e126ba97SEli Cohen 	init_attr->send_cq = cq;
3140e126ba97SEli Cohen 	init_attr->recv_cq = cq;
3141e126ba97SEli Cohen 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
3142e126ba97SEli Cohen 	init_attr->cap.max_send_wr = MAX_UMR_WR;
3143e126ba97SEli Cohen 	init_attr->cap.max_send_sge = 1;
3144e126ba97SEli Cohen 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
3145e126ba97SEli Cohen 	init_attr->port_num = 1;
3146e126ba97SEli Cohen 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
3147e126ba97SEli Cohen 	if (IS_ERR(qp)) {
3148e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
3149e126ba97SEli Cohen 		ret = PTR_ERR(qp);
3150e126ba97SEli Cohen 		goto error_3;
3151e126ba97SEli Cohen 	}
3152e126ba97SEli Cohen 	qp->device     = &dev->ib_dev;
3153e126ba97SEli Cohen 	qp->real_qp    = qp;
3154e126ba97SEli Cohen 	qp->uobject    = NULL;
3155e126ba97SEli Cohen 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
315631fde034SMajd Dibbiny 	qp->send_cq    = init_attr->send_cq;
315731fde034SMajd Dibbiny 	qp->recv_cq    = init_attr->recv_cq;
3158e126ba97SEli Cohen 
3159e126ba97SEli Cohen 	attr->qp_state = IB_QPS_INIT;
3160e126ba97SEli Cohen 	attr->port_num = 1;
3161e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
3162e126ba97SEli Cohen 				IB_QP_PORT, NULL);
3163e126ba97SEli Cohen 	if (ret) {
3164e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
3165e126ba97SEli Cohen 		goto error_4;
3166e126ba97SEli Cohen 	}
3167e126ba97SEli Cohen 
3168e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3169e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTR;
3170e126ba97SEli Cohen 	attr->path_mtu = IB_MTU_256;
3171e126ba97SEli Cohen 
3172e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3173e126ba97SEli Cohen 	if (ret) {
3174e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
3175e126ba97SEli Cohen 		goto error_4;
3176e126ba97SEli Cohen 	}
3177e126ba97SEli Cohen 
3178e126ba97SEli Cohen 	memset(attr, 0, sizeof(*attr));
3179e126ba97SEli Cohen 	attr->qp_state = IB_QPS_RTS;
3180e126ba97SEli Cohen 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
3181e126ba97SEli Cohen 	if (ret) {
3182e126ba97SEli Cohen 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
3183e126ba97SEli Cohen 		goto error_4;
3184e126ba97SEli Cohen 	}
3185e126ba97SEli Cohen 
3186e126ba97SEli Cohen 	dev->umrc.qp = qp;
3187e126ba97SEli Cohen 	dev->umrc.cq = cq;
3188e126ba97SEli Cohen 	dev->umrc.pd = pd;
3189e126ba97SEli Cohen 
3190e126ba97SEli Cohen 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
3191e126ba97SEli Cohen 	ret = mlx5_mr_cache_init(dev);
3192e126ba97SEli Cohen 	if (ret) {
3193e126ba97SEli Cohen 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
3194e126ba97SEli Cohen 		goto error_4;
3195e126ba97SEli Cohen 	}
3196e126ba97SEli Cohen 
3197e126ba97SEli Cohen 	kfree(attr);
3198e126ba97SEli Cohen 	kfree(init_attr);
3199e126ba97SEli Cohen 
3200e126ba97SEli Cohen 	return 0;
3201e126ba97SEli Cohen 
3202e126ba97SEli Cohen error_4:
3203e126ba97SEli Cohen 	mlx5_ib_destroy_qp(qp);
3204e126ba97SEli Cohen 
3205e126ba97SEli Cohen error_3:
3206add08d76SChristoph Hellwig 	ib_free_cq(cq);
3207e126ba97SEli Cohen 
3208e126ba97SEli Cohen error_2:
3209e126ba97SEli Cohen 	ib_dealloc_pd(pd);
3210e126ba97SEli Cohen 
3211e126ba97SEli Cohen error_0:
3212e126ba97SEli Cohen 	kfree(attr);
3213e126ba97SEli Cohen 	kfree(init_attr);
3214e126ba97SEli Cohen 	return ret;
3215e126ba97SEli Cohen }
3216e126ba97SEli Cohen 
32176e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
32186e8484c5SMax Gurtovoy {
32196e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
32206e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
32216e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
32226e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
32236e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
32246e8484c5SMax Gurtovoy 	default:
32256e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
32266e8484c5SMax Gurtovoy 	}
32276e8484c5SMax Gurtovoy }
32286e8484c5SMax Gurtovoy 
3229e126ba97SEli Cohen static int create_dev_resources(struct mlx5_ib_resources *devr)
3230e126ba97SEli Cohen {
3231e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
3232e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3233bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
32347722f47eSHaggai Eran 	int port;
3235e126ba97SEli Cohen 	int ret = 0;
3236e126ba97SEli Cohen 
3237e126ba97SEli Cohen 	dev = container_of(devr, struct mlx5_ib_dev, devr);
3238e126ba97SEli Cohen 
3239d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3240d16e91daSHaggai Eran 
3241e126ba97SEli Cohen 	devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
3242e126ba97SEli Cohen 	if (IS_ERR(devr->p0)) {
3243e126ba97SEli Cohen 		ret = PTR_ERR(devr->p0);
3244e126ba97SEli Cohen 		goto error0;
3245e126ba97SEli Cohen 	}
3246e126ba97SEli Cohen 	devr->p0->device  = &dev->ib_dev;
3247e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3248e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3249e126ba97SEli Cohen 
3250bcf4c1eaSMatan Barak 	devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
3251e126ba97SEli Cohen 	if (IS_ERR(devr->c0)) {
3252e126ba97SEli Cohen 		ret = PTR_ERR(devr->c0);
3253e126ba97SEli Cohen 		goto error1;
3254e126ba97SEli Cohen 	}
3255e126ba97SEli Cohen 	devr->c0->device        = &dev->ib_dev;
3256e126ba97SEli Cohen 	devr->c0->uobject       = NULL;
3257e126ba97SEli Cohen 	devr->c0->comp_handler  = NULL;
3258e126ba97SEli Cohen 	devr->c0->event_handler = NULL;
3259e126ba97SEli Cohen 	devr->c0->cq_context    = NULL;
3260e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3261e126ba97SEli Cohen 
3262e126ba97SEli Cohen 	devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3263e126ba97SEli Cohen 	if (IS_ERR(devr->x0)) {
3264e126ba97SEli Cohen 		ret = PTR_ERR(devr->x0);
3265e126ba97SEli Cohen 		goto error2;
3266e126ba97SEli Cohen 	}
3267e126ba97SEli Cohen 	devr->x0->device = &dev->ib_dev;
3268e126ba97SEli Cohen 	devr->x0->inode = NULL;
3269e126ba97SEli Cohen 	atomic_set(&devr->x0->usecnt, 0);
3270e126ba97SEli Cohen 	mutex_init(&devr->x0->tgt_qp_mutex);
3271e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
3272e126ba97SEli Cohen 
3273e126ba97SEli Cohen 	devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
3274e126ba97SEli Cohen 	if (IS_ERR(devr->x1)) {
3275e126ba97SEli Cohen 		ret = PTR_ERR(devr->x1);
3276e126ba97SEli Cohen 		goto error3;
3277e126ba97SEli Cohen 	}
3278e126ba97SEli Cohen 	devr->x1->device = &dev->ib_dev;
3279e126ba97SEli Cohen 	devr->x1->inode = NULL;
3280e126ba97SEli Cohen 	atomic_set(&devr->x1->usecnt, 0);
3281e126ba97SEli Cohen 	mutex_init(&devr->x1->tgt_qp_mutex);
3282e126ba97SEli Cohen 	INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
3283e126ba97SEli Cohen 
3284e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3285e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3286e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3287e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
32881a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3289e126ba97SEli Cohen 	attr.ext.xrc.xrcd = devr->x0;
3290e126ba97SEli Cohen 
3291e126ba97SEli Cohen 	devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
3292e126ba97SEli Cohen 	if (IS_ERR(devr->s0)) {
3293e126ba97SEli Cohen 		ret = PTR_ERR(devr->s0);
3294e126ba97SEli Cohen 		goto error4;
3295e126ba97SEli Cohen 	}
3296e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3297e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3298e126ba97SEli Cohen 	devr->s0->uobject       = NULL;
3299e126ba97SEli Cohen 	devr->s0->event_handler = NULL;
3300e126ba97SEli Cohen 	devr->s0->srq_context   = NULL;
3301e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
3302e126ba97SEli Cohen 	devr->s0->ext.xrc.xrcd	= devr->x0;
33031a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
3304e126ba97SEli Cohen 	atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
33051a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3306e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3307e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3308e126ba97SEli Cohen 
33094aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
33104aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
33114aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
33124aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
33134aa17b28SHaggai Abramonvsky 	devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
33144aa17b28SHaggai Abramonvsky 	if (IS_ERR(devr->s1)) {
33154aa17b28SHaggai Abramonvsky 		ret = PTR_ERR(devr->s1);
33164aa17b28SHaggai Abramonvsky 		goto error5;
33174aa17b28SHaggai Abramonvsky 	}
33184aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
33194aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
33204aa17b28SHaggai Abramonvsky 	devr->s1->uobject       = NULL;
33214aa17b28SHaggai Abramonvsky 	devr->s1->event_handler = NULL;
33224aa17b28SHaggai Abramonvsky 	devr->s1->srq_context   = NULL;
33234aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
33241a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
33254aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
33261a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
33274aa17b28SHaggai Abramonvsky 
33287722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
33297722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
33307722f47eSHaggai Eran 			  pkey_change_handler);
33317722f47eSHaggai Eran 		devr->ports[port].devr = devr;
33327722f47eSHaggai Eran 	}
33337722f47eSHaggai Eran 
3334e126ba97SEli Cohen 	return 0;
3335e126ba97SEli Cohen 
33364aa17b28SHaggai Abramonvsky error5:
33374aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s0);
3338e126ba97SEli Cohen error4:
3339e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3340e126ba97SEli Cohen error3:
3341e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3342e126ba97SEli Cohen error2:
3343e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3344e126ba97SEli Cohen error1:
3345e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
3346e126ba97SEli Cohen error0:
3347e126ba97SEli Cohen 	return ret;
3348e126ba97SEli Cohen }
3349e126ba97SEli Cohen 
3350e126ba97SEli Cohen static void destroy_dev_resources(struct mlx5_ib_resources *devr)
3351e126ba97SEli Cohen {
33527722f47eSHaggai Eran 	struct mlx5_ib_dev *dev =
33537722f47eSHaggai Eran 		container_of(devr, struct mlx5_ib_dev, devr);
33547722f47eSHaggai Eran 	int port;
33557722f47eSHaggai Eran 
33564aa17b28SHaggai Abramonvsky 	mlx5_ib_destroy_srq(devr->s1);
3357e126ba97SEli Cohen 	mlx5_ib_destroy_srq(devr->s0);
3358e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x0);
3359e126ba97SEli Cohen 	mlx5_ib_dealloc_xrcd(devr->x1);
3360e126ba97SEli Cohen 	mlx5_ib_destroy_cq(devr->c0);
3361e126ba97SEli Cohen 	mlx5_ib_dealloc_pd(devr->p0);
33627722f47eSHaggai Eran 
33637722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
33647722f47eSHaggai Eran 	for (port = 0; port < dev->num_ports; ++port)
33657722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3366e126ba97SEli Cohen }
3367e126ba97SEli Cohen 
3368e53505a8SAchiad Shochat static u32 get_core_cap_flags(struct ib_device *ibdev)
3369e53505a8SAchiad Shochat {
3370e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3371e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3372e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3373e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
3374e53505a8SAchiad Shochat 	u32 ret = 0;
3375e53505a8SAchiad Shochat 
3376e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3377e53505a8SAchiad Shochat 		return RDMA_CORE_PORT_IBA_IB;
3378e53505a8SAchiad Shochat 
337972cd5717SOr Gerlitz 	ret = RDMA_CORE_PORT_RAW_PACKET;
338072cd5717SOr Gerlitz 
3381e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
338272cd5717SOr Gerlitz 		return ret;
3383e53505a8SAchiad Shochat 
3384e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
338572cd5717SOr Gerlitz 		return ret;
3386e53505a8SAchiad Shochat 
3387e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3388e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3389e53505a8SAchiad Shochat 
3390e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3391e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3392e53505a8SAchiad Shochat 
3393e53505a8SAchiad Shochat 	return ret;
3394e53505a8SAchiad Shochat }
3395e53505a8SAchiad Shochat 
33967738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
33977738613eSIra Weiny 			       struct ib_port_immutable *immutable)
33987738613eSIra Weiny {
33997738613eSIra Weiny 	struct ib_port_attr attr;
3400ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3401ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
34027738613eSIra Weiny 	int err;
34037738613eSIra Weiny 
3404c4550c63SOr Gerlitz 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3405c4550c63SOr Gerlitz 
3406c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
34077738613eSIra Weiny 	if (err)
34087738613eSIra Weiny 		return err;
34097738613eSIra Weiny 
34107738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
34117738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3412e53505a8SAchiad Shochat 	immutable->core_cap_flags = get_core_cap_flags(ibdev);
3413ca5b91d6SOr Gerlitz 	if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
3414337877a4SIra Weiny 		immutable->max_mad_size = IB_MGMT_MAD_SIZE;
34157738613eSIra Weiny 
34167738613eSIra Weiny 	return 0;
34177738613eSIra Weiny }
34187738613eSIra Weiny 
34199abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3420c7342823SIra Weiny {
3421c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3422c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
34239abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
34249abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
34259abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3426c7342823SIra Weiny }
3427c7342823SIra Weiny 
342845f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
34299ef9c640SAviv Heller {
34309ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34319ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
34329ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
34339ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
34349ef9c640SAviv Heller 	int err;
34359ef9c640SAviv Heller 
34369ef9c640SAviv Heller 	if (!ns || !mlx5_lag_is_active(mdev))
34379ef9c640SAviv Heller 		return 0;
34389ef9c640SAviv Heller 
34399ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
34409ef9c640SAviv Heller 	if (err)
34419ef9c640SAviv Heller 		return err;
34429ef9c640SAviv Heller 
34439ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
34449ef9c640SAviv Heller 	if (IS_ERR(ft)) {
34459ef9c640SAviv Heller 		err = PTR_ERR(ft);
34469ef9c640SAviv Heller 		goto err_destroy_vport_lag;
34479ef9c640SAviv Heller 	}
34489ef9c640SAviv Heller 
34499ef9c640SAviv Heller 	dev->flow_db.lag_demux_ft = ft;
34509ef9c640SAviv Heller 	return 0;
34519ef9c640SAviv Heller 
34529ef9c640SAviv Heller err_destroy_vport_lag:
34539ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
34549ef9c640SAviv Heller 	return err;
34559ef9c640SAviv Heller }
34569ef9c640SAviv Heller 
345745f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
34589ef9c640SAviv Heller {
34599ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
34609ef9c640SAviv Heller 
34619ef9c640SAviv Heller 	if (dev->flow_db.lag_demux_ft) {
34629ef9c640SAviv Heller 		mlx5_destroy_flow_table(dev->flow_db.lag_demux_ft);
34639ef9c640SAviv Heller 		dev->flow_db.lag_demux_ft = NULL;
34649ef9c640SAviv Heller 
34659ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
34669ef9c640SAviv Heller 	}
34679ef9c640SAviv Heller }
34689ef9c640SAviv Heller 
3469d012f5d6SOr Gerlitz static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev)
3470fc24fc5eSAchiad Shochat {
3471e53505a8SAchiad Shochat 	int err;
3472e53505a8SAchiad Shochat 
3473fc24fc5eSAchiad Shochat 	dev->roce.nb.notifier_call = mlx5_netdev_event;
3474e53505a8SAchiad Shochat 	err = register_netdevice_notifier(&dev->roce.nb);
34755ec8c83eSAviv Heller 	if (err) {
34765ec8c83eSAviv Heller 		dev->roce.nb.notifier_call = NULL;
3477e53505a8SAchiad Shochat 		return err;
34785ec8c83eSAviv Heller 	}
3479e53505a8SAchiad Shochat 
3480d012f5d6SOr Gerlitz 	return 0;
3481d012f5d6SOr Gerlitz }
3482d012f5d6SOr Gerlitz 
3483d012f5d6SOr Gerlitz static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev)
3484e126ba97SEli Cohen {
3485e126ba97SEli Cohen 	if (dev->roce.nb.notifier_call) {
3486e126ba97SEli Cohen 		unregister_netdevice_notifier(&dev->roce.nb);
3487e126ba97SEli Cohen 		dev->roce.nb.notifier_call = NULL;
3488e126ba97SEli Cohen 	}
3489e126ba97SEli Cohen }
3490e126ba97SEli Cohen 
349145f95acdSOr Gerlitz static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3492e126ba97SEli Cohen {
3493e126ba97SEli Cohen 	int err;
3494e126ba97SEli Cohen 
3495d012f5d6SOr Gerlitz 	err = mlx5_add_netdev_notifier(dev);
3496d012f5d6SOr Gerlitz 	if (err)
3497e53505a8SAchiad Shochat 		return err;
3498e53505a8SAchiad Shochat 
3499ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce)) {
3500e53505a8SAchiad Shochat 		err = mlx5_nic_vport_enable_roce(dev->mdev);
3501e53505a8SAchiad Shochat 		if (err)
3502e53505a8SAchiad Shochat 			goto err_unregister_netdevice_notifier;
3503ca5b91d6SOr Gerlitz 	}
3504e53505a8SAchiad Shochat 
350545f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
35069ef9c640SAviv Heller 	if (err)
35079ef9c640SAviv Heller 		goto err_disable_roce;
35089ef9c640SAviv Heller 
3509e53505a8SAchiad Shochat 	return 0;
3510e53505a8SAchiad Shochat 
35119ef9c640SAviv Heller err_disable_roce:
3512ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
35139ef9c640SAviv Heller 		mlx5_nic_vport_disable_roce(dev->mdev);
35149ef9c640SAviv Heller 
3515e53505a8SAchiad Shochat err_unregister_netdevice_notifier:
3516d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
3517e53505a8SAchiad Shochat 	return err;
3518fc24fc5eSAchiad Shochat }
3519fc24fc5eSAchiad Shochat 
352045f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3521fc24fc5eSAchiad Shochat {
352245f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3523ca5b91d6SOr Gerlitz 	if (MLX5_CAP_GEN(dev->mdev, roce))
3524e53505a8SAchiad Shochat 		mlx5_nic_vport_disable_roce(dev->mdev);
3525fc24fc5eSAchiad Shochat }
3526fc24fc5eSAchiad Shochat 
3527e1f24a79SParav Pandit struct mlx5_ib_counter {
35287c16f477SKamal Heib 	const char *name;
35297c16f477SKamal Heib 	size_t offset;
35307c16f477SKamal Heib };
35317c16f477SKamal Heib 
35327c16f477SKamal Heib #define INIT_Q_COUNTER(_name)		\
35337c16f477SKamal Heib 	{ .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
35347c16f477SKamal Heib 
3535e1f24a79SParav Pandit static const struct mlx5_ib_counter basic_q_cnts[] = {
35367c16f477SKamal Heib 	INIT_Q_COUNTER(rx_write_requests),
35377c16f477SKamal Heib 	INIT_Q_COUNTER(rx_read_requests),
35387c16f477SKamal Heib 	INIT_Q_COUNTER(rx_atomic_requests),
35397c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_buffer),
35407c16f477SKamal Heib };
35417c16f477SKamal Heib 
3542e1f24a79SParav Pandit static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
35437c16f477SKamal Heib 	INIT_Q_COUNTER(out_of_sequence),
35447c16f477SKamal Heib };
35457c16f477SKamal Heib 
3546e1f24a79SParav Pandit static const struct mlx5_ib_counter retrans_q_cnts[] = {
35477c16f477SKamal Heib 	INIT_Q_COUNTER(duplicate_request),
35487c16f477SKamal Heib 	INIT_Q_COUNTER(rnr_nak_retry_err),
35497c16f477SKamal Heib 	INIT_Q_COUNTER(packet_seq_err),
35507c16f477SKamal Heib 	INIT_Q_COUNTER(implied_nak_seq_err),
35517c16f477SKamal Heib 	INIT_Q_COUNTER(local_ack_timeout_err),
35527c16f477SKamal Heib };
35537c16f477SKamal Heib 
3554e1f24a79SParav Pandit #define INIT_CONG_COUNTER(_name)		\
3555e1f24a79SParav Pandit 	{ .name = #_name, .offset =	\
3556e1f24a79SParav Pandit 		MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
3557e1f24a79SParav Pandit 
3558e1f24a79SParav Pandit static const struct mlx5_ib_counter cong_cnts[] = {
3559e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_ignored),
3560e1f24a79SParav Pandit 	INIT_CONG_COUNTER(rp_cnp_handled),
3561e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
3562e1f24a79SParav Pandit 	INIT_CONG_COUNTER(np_cnp_sent),
3563e1f24a79SParav Pandit };
3564e1f24a79SParav Pandit 
356558dcb60aSParav Pandit static const struct mlx5_ib_counter extended_err_cnts[] = {
356658dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_local_length_error),
356758dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_error),
356858dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_error),
356958dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_invalid_request),
357058dcb60aSParav Pandit 	INIT_Q_COUNTER(req_remote_access_errors),
357158dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_remote_access_errors),
357258dcb60aSParav Pandit 	INIT_Q_COUNTER(resp_cqe_flush_error),
357358dcb60aSParav Pandit 	INIT_Q_COUNTER(req_cqe_flush_error),
357458dcb60aSParav Pandit };
357558dcb60aSParav Pandit 
3576e1f24a79SParav Pandit static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
35770837e86aSMark Bloch {
35780837e86aSMark Bloch 	unsigned int i;
35790837e86aSMark Bloch 
35807c16f477SKamal Heib 	for (i = 0; i < dev->num_ports; i++) {
35810837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3582e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
3583e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.names);
3584e1f24a79SParav Pandit 		kfree(dev->port[i].cnts.offsets);
35857c16f477SKamal Heib 	}
35867c16f477SKamal Heib }
35877c16f477SKamal Heib 
3588e1f24a79SParav Pandit static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
3589e1f24a79SParav Pandit 				    struct mlx5_ib_counters *cnts)
35907c16f477SKamal Heib {
35917c16f477SKamal Heib 	u32 num_counters;
35927c16f477SKamal Heib 
35937c16f477SKamal Heib 	num_counters = ARRAY_SIZE(basic_q_cnts);
35947c16f477SKamal Heib 
35957c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
35967c16f477SKamal Heib 		num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
35977c16f477SKamal Heib 
35987c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
35997c16f477SKamal Heib 		num_counters += ARRAY_SIZE(retrans_q_cnts);
360058dcb60aSParav Pandit 
360158dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
360258dcb60aSParav Pandit 		num_counters += ARRAY_SIZE(extended_err_cnts);
360358dcb60aSParav Pandit 
3604e1f24a79SParav Pandit 	cnts->num_q_counters = num_counters;
36057c16f477SKamal Heib 
3606e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3607e1f24a79SParav Pandit 		cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
3608e1f24a79SParav Pandit 		num_counters += ARRAY_SIZE(cong_cnts);
3609e1f24a79SParav Pandit 	}
3610e1f24a79SParav Pandit 
3611e1f24a79SParav Pandit 	cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
3612e1f24a79SParav Pandit 	if (!cnts->names)
36137c16f477SKamal Heib 		return -ENOMEM;
36147c16f477SKamal Heib 
3615e1f24a79SParav Pandit 	cnts->offsets = kcalloc(num_counters,
3616e1f24a79SParav Pandit 				sizeof(cnts->offsets), GFP_KERNEL);
3617e1f24a79SParav Pandit 	if (!cnts->offsets)
36187c16f477SKamal Heib 		goto err_names;
36197c16f477SKamal Heib 
36207c16f477SKamal Heib 	return 0;
36217c16f477SKamal Heib 
36227c16f477SKamal Heib err_names:
3623e1f24a79SParav Pandit 	kfree(cnts->names);
36247c16f477SKamal Heib 	return -ENOMEM;
36257c16f477SKamal Heib }
36267c16f477SKamal Heib 
3627e1f24a79SParav Pandit static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
36287c16f477SKamal Heib 				  const char **names,
36297c16f477SKamal Heib 				  size_t *offsets)
36307c16f477SKamal Heib {
36317c16f477SKamal Heib 	int i;
36327c16f477SKamal Heib 	int j = 0;
36337c16f477SKamal Heib 
36347c16f477SKamal Heib 	for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
36357c16f477SKamal Heib 		names[j] = basic_q_cnts[i].name;
36367c16f477SKamal Heib 		offsets[j] = basic_q_cnts[i].offset;
36377c16f477SKamal Heib 	}
36387c16f477SKamal Heib 
36397c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
36407c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
36417c16f477SKamal Heib 			names[j] = out_of_seq_q_cnts[i].name;
36427c16f477SKamal Heib 			offsets[j] = out_of_seq_q_cnts[i].offset;
36437c16f477SKamal Heib 		}
36447c16f477SKamal Heib 	}
36457c16f477SKamal Heib 
36467c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
36477c16f477SKamal Heib 		for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
36487c16f477SKamal Heib 			names[j] = retrans_q_cnts[i].name;
36497c16f477SKamal Heib 			offsets[j] = retrans_q_cnts[i].offset;
36507c16f477SKamal Heib 		}
36517c16f477SKamal Heib 	}
3652e1f24a79SParav Pandit 
365358dcb60aSParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
365458dcb60aSParav Pandit 		for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
365558dcb60aSParav Pandit 			names[j] = extended_err_cnts[i].name;
365658dcb60aSParav Pandit 			offsets[j] = extended_err_cnts[i].offset;
365758dcb60aSParav Pandit 		}
365858dcb60aSParav Pandit 	}
365958dcb60aSParav Pandit 
3660e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
3661e1f24a79SParav Pandit 		for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
3662e1f24a79SParav Pandit 			names[j] = cong_cnts[i].name;
3663e1f24a79SParav Pandit 			offsets[j] = cong_cnts[i].offset;
3664e1f24a79SParav Pandit 		}
3665e1f24a79SParav Pandit 	}
36660837e86aSMark Bloch }
36670837e86aSMark Bloch 
3668e1f24a79SParav Pandit static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
36690837e86aSMark Bloch {
36700837e86aSMark Bloch 	int i;
36710837e86aSMark Bloch 	int ret;
36720837e86aSMark Bloch 
36730837e86aSMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
36747c16f477SKamal Heib 		struct mlx5_ib_port *port = &dev->port[i];
36757c16f477SKamal Heib 
36760837e86aSMark Bloch 		ret = mlx5_core_alloc_q_counter(dev->mdev,
3677e1f24a79SParav Pandit 						&port->cnts.set_id);
36780837e86aSMark Bloch 		if (ret) {
36790837e86aSMark Bloch 			mlx5_ib_warn(dev,
36800837e86aSMark Bloch 				     "couldn't allocate queue counter for port %d, err %d\n",
36810837e86aSMark Bloch 				     i + 1, ret);
36820837e86aSMark Bloch 			goto dealloc_counters;
36830837e86aSMark Bloch 		}
36847c16f477SKamal Heib 
3685e1f24a79SParav Pandit 		ret = __mlx5_ib_alloc_counters(dev, &port->cnts);
36867c16f477SKamal Heib 		if (ret)
36877c16f477SKamal Heib 			goto dealloc_counters;
36887c16f477SKamal Heib 
3689e1f24a79SParav Pandit 		mlx5_ib_fill_counters(dev, port->cnts.names,
3690e1f24a79SParav Pandit 				      port->cnts.offsets);
36910837e86aSMark Bloch 	}
36920837e86aSMark Bloch 
36930837e86aSMark Bloch 	return 0;
36940837e86aSMark Bloch 
36950837e86aSMark Bloch dealloc_counters:
36960837e86aSMark Bloch 	while (--i >= 0)
36970837e86aSMark Bloch 		mlx5_core_dealloc_q_counter(dev->mdev,
3698e1f24a79SParav Pandit 					    dev->port[i].cnts.set_id);
36990837e86aSMark Bloch 
37000837e86aSMark Bloch 	return ret;
37010837e86aSMark Bloch }
37020837e86aSMark Bloch 
37030ad17a8fSMark Bloch static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
37040ad17a8fSMark Bloch 						    u8 port_num)
37050ad17a8fSMark Bloch {
37067c16f477SKamal Heib 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
37077c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
37080ad17a8fSMark Bloch 
37090ad17a8fSMark Bloch 	/* We support only per port stats */
37100ad17a8fSMark Bloch 	if (port_num == 0)
37110ad17a8fSMark Bloch 		return NULL;
37120ad17a8fSMark Bloch 
3713e1f24a79SParav Pandit 	return rdma_alloc_hw_stats_struct(port->cnts.names,
3714e1f24a79SParav Pandit 					  port->cnts.num_q_counters +
3715e1f24a79SParav Pandit 					  port->cnts.num_cong_counters,
37160ad17a8fSMark Bloch 					  RDMA_HW_STATS_DEFAULT_LIFESPAN);
37170ad17a8fSMark Bloch }
37180ad17a8fSMark Bloch 
3719e1f24a79SParav Pandit static int mlx5_ib_query_q_counters(struct mlx5_ib_dev *dev,
3720e1f24a79SParav Pandit 				    struct mlx5_ib_port *port,
3721e1f24a79SParav Pandit 				    struct rdma_hw_stats *stats)
3722e1f24a79SParav Pandit {
3723e1f24a79SParav Pandit 	int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
3724e1f24a79SParav Pandit 	void *out;
3725e1f24a79SParav Pandit 	__be32 val;
3726e1f24a79SParav Pandit 	int ret, i;
3727e1f24a79SParav Pandit 
37281b9a07eeSLeon Romanovsky 	out = kvzalloc(outlen, GFP_KERNEL);
3729e1f24a79SParav Pandit 	if (!out)
3730e1f24a79SParav Pandit 		return -ENOMEM;
3731e1f24a79SParav Pandit 
3732e1f24a79SParav Pandit 	ret = mlx5_core_query_q_counter(dev->mdev,
3733e1f24a79SParav Pandit 					port->cnts.set_id, 0,
3734e1f24a79SParav Pandit 					out, outlen);
3735e1f24a79SParav Pandit 	if (ret)
3736e1f24a79SParav Pandit 		goto free;
3737e1f24a79SParav Pandit 
3738e1f24a79SParav Pandit 	for (i = 0; i < port->cnts.num_q_counters; i++) {
3739e1f24a79SParav Pandit 		val = *(__be32 *)(out + port->cnts.offsets[i]);
3740e1f24a79SParav Pandit 		stats->value[i] = (u64)be32_to_cpu(val);
3741e1f24a79SParav Pandit 	}
3742e1f24a79SParav Pandit 
3743e1f24a79SParav Pandit free:
3744e1f24a79SParav Pandit 	kvfree(out);
3745e1f24a79SParav Pandit 	return ret;
3746e1f24a79SParav Pandit }
3747e1f24a79SParav Pandit 
37480ad17a8fSMark Bloch static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
37490ad17a8fSMark Bloch 				struct rdma_hw_stats *stats,
37507c16f477SKamal Heib 				u8 port_num, int index)
37510ad17a8fSMark Bloch {
37520ad17a8fSMark Bloch 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
37537c16f477SKamal Heib 	struct mlx5_ib_port *port = &dev->port[port_num - 1];
3754e1f24a79SParav Pandit 	int ret, num_counters;
37550ad17a8fSMark Bloch 
37567c16f477SKamal Heib 	if (!stats)
3757e1f24a79SParav Pandit 		return -EINVAL;
37580ad17a8fSMark Bloch 
3759e1f24a79SParav Pandit 	ret = mlx5_ib_query_q_counters(dev, port, stats);
37600ad17a8fSMark Bloch 	if (ret)
3761e1f24a79SParav Pandit 		return ret;
3762e1f24a79SParav Pandit 	num_counters = port->cnts.num_q_counters;
37630ad17a8fSMark Bloch 
3764e1f24a79SParav Pandit 	if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
376571a0ff65SMajd Dibbiny 		ret = mlx5_lag_query_cong_counters(dev->mdev,
376671a0ff65SMajd Dibbiny 						   stats->value +
376771a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters,
376871a0ff65SMajd Dibbiny 						   port->cnts.num_cong_counters,
376971a0ff65SMajd Dibbiny 						   port->cnts.offsets +
377071a0ff65SMajd Dibbiny 						   port->cnts.num_q_counters);
3771e1f24a79SParav Pandit 		if (ret)
3772e1f24a79SParav Pandit 			return ret;
3773e1f24a79SParav Pandit 		num_counters += port->cnts.num_cong_counters;
37740ad17a8fSMark Bloch 	}
37757c16f477SKamal Heib 
3776e1f24a79SParav Pandit 	return num_counters;
37770ad17a8fSMark Bloch }
37780ad17a8fSMark Bloch 
37798e959601SNiranjana Vishwanathapura static void mlx5_ib_free_rdma_netdev(struct net_device *netdev)
37808e959601SNiranjana Vishwanathapura {
37818e959601SNiranjana Vishwanathapura 	return mlx5_rdma_netdev_free(netdev);
37828e959601SNiranjana Vishwanathapura }
37838e959601SNiranjana Vishwanathapura 
3784693dfd5aSErez Shitrit static struct net_device*
3785693dfd5aSErez Shitrit mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
3786693dfd5aSErez Shitrit 			  u8 port_num,
3787693dfd5aSErez Shitrit 			  enum rdma_netdev_t type,
3788693dfd5aSErez Shitrit 			  const char *name,
3789693dfd5aSErez Shitrit 			  unsigned char name_assign_type,
3790693dfd5aSErez Shitrit 			  void (*setup)(struct net_device *))
3791693dfd5aSErez Shitrit {
37928e959601SNiranjana Vishwanathapura 	struct net_device *netdev;
37938e959601SNiranjana Vishwanathapura 	struct rdma_netdev *rn;
37948e959601SNiranjana Vishwanathapura 
3795693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3796693dfd5aSErez Shitrit 		return ERR_PTR(-EOPNOTSUPP);
3797693dfd5aSErez Shitrit 
37988e959601SNiranjana Vishwanathapura 	netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
3799693dfd5aSErez Shitrit 					name, setup);
38008e959601SNiranjana Vishwanathapura 	if (likely(!IS_ERR_OR_NULL(netdev))) {
38018e959601SNiranjana Vishwanathapura 		rn = netdev_priv(netdev);
38028e959601SNiranjana Vishwanathapura 		rn->free_rdma_netdev = mlx5_ib_free_rdma_netdev;
3803693dfd5aSErez Shitrit 	}
38048e959601SNiranjana Vishwanathapura 	return netdev;
3805693dfd5aSErez Shitrit }
3806693dfd5aSErez Shitrit 
3807fe248c3aSMaor Gottlieb static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
3808fe248c3aSMaor Gottlieb {
3809fe248c3aSMaor Gottlieb 	if (!dev->delay_drop.dbg)
3810fe248c3aSMaor Gottlieb 		return;
3811fe248c3aSMaor Gottlieb 	debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
3812fe248c3aSMaor Gottlieb 	kfree(dev->delay_drop.dbg);
3813fe248c3aSMaor Gottlieb 	dev->delay_drop.dbg = NULL;
3814fe248c3aSMaor Gottlieb }
3815fe248c3aSMaor Gottlieb 
381603404e8aSMaor Gottlieb static void cancel_delay_drop(struct mlx5_ib_dev *dev)
381703404e8aSMaor Gottlieb {
381803404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
381903404e8aSMaor Gottlieb 		return;
382003404e8aSMaor Gottlieb 
382103404e8aSMaor Gottlieb 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
3822fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3823fe248c3aSMaor Gottlieb }
3824fe248c3aSMaor Gottlieb 
3825fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3826fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3827fe248c3aSMaor Gottlieb {
3828fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3829fe248c3aSMaor Gottlieb 	char lbuf[20];
3830fe248c3aSMaor Gottlieb 	int len;
3831fe248c3aSMaor Gottlieb 
3832fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3833fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3834fe248c3aSMaor Gottlieb }
3835fe248c3aSMaor Gottlieb 
3836fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3837fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3838fe248c3aSMaor Gottlieb {
3839fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3840fe248c3aSMaor Gottlieb 	u32 timeout;
3841fe248c3aSMaor Gottlieb 	u32 var;
3842fe248c3aSMaor Gottlieb 
3843fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3844fe248c3aSMaor Gottlieb 		return -EFAULT;
3845fe248c3aSMaor Gottlieb 
3846fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3847fe248c3aSMaor Gottlieb 			1000);
3848fe248c3aSMaor Gottlieb 	if (timeout != var)
3849fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3850fe248c3aSMaor Gottlieb 			    timeout);
3851fe248c3aSMaor Gottlieb 
3852fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3853fe248c3aSMaor Gottlieb 
3854fe248c3aSMaor Gottlieb 	return count;
3855fe248c3aSMaor Gottlieb }
3856fe248c3aSMaor Gottlieb 
3857fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3858fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3859fe248c3aSMaor Gottlieb 	.open	= simple_open,
3860fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3861fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3862fe248c3aSMaor Gottlieb };
3863fe248c3aSMaor Gottlieb 
3864fe248c3aSMaor Gottlieb static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
3865fe248c3aSMaor Gottlieb {
3866fe248c3aSMaor Gottlieb 	struct mlx5_ib_dbg_delay_drop *dbg;
3867fe248c3aSMaor Gottlieb 
3868fe248c3aSMaor Gottlieb 	if (!mlx5_debugfs_root)
3869fe248c3aSMaor Gottlieb 		return 0;
3870fe248c3aSMaor Gottlieb 
3871fe248c3aSMaor Gottlieb 	dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
3872fe248c3aSMaor Gottlieb 	if (!dbg)
3873fe248c3aSMaor Gottlieb 		return -ENOMEM;
3874fe248c3aSMaor Gottlieb 
3875cbafad87SSudip Mukherjee 	dev->delay_drop.dbg = dbg;
3876cbafad87SSudip Mukherjee 
3877fe248c3aSMaor Gottlieb 	dbg->dir_debugfs =
3878fe248c3aSMaor Gottlieb 		debugfs_create_dir("delay_drop",
3879fe248c3aSMaor Gottlieb 				   dev->mdev->priv.dbg_root);
3880fe248c3aSMaor Gottlieb 	if (!dbg->dir_debugfs)
3881cbafad87SSudip Mukherjee 		goto out_debugfs;
3882fe248c3aSMaor Gottlieb 
3883fe248c3aSMaor Gottlieb 	dbg->events_cnt_debugfs =
3884fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_timeout_events", 0400,
3885fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3886fe248c3aSMaor Gottlieb 					&dev->delay_drop.events_cnt);
3887fe248c3aSMaor Gottlieb 	if (!dbg->events_cnt_debugfs)
3888fe248c3aSMaor Gottlieb 		goto out_debugfs;
3889fe248c3aSMaor Gottlieb 
3890fe248c3aSMaor Gottlieb 	dbg->rqs_cnt_debugfs =
3891fe248c3aSMaor Gottlieb 		debugfs_create_atomic_t("num_rqs", 0400,
3892fe248c3aSMaor Gottlieb 					dbg->dir_debugfs,
3893fe248c3aSMaor Gottlieb 					&dev->delay_drop.rqs_cnt);
3894fe248c3aSMaor Gottlieb 	if (!dbg->rqs_cnt_debugfs)
3895fe248c3aSMaor Gottlieb 		goto out_debugfs;
3896fe248c3aSMaor Gottlieb 
3897fe248c3aSMaor Gottlieb 	dbg->timeout_debugfs =
3898fe248c3aSMaor Gottlieb 		debugfs_create_file("timeout", 0600,
3899fe248c3aSMaor Gottlieb 				    dbg->dir_debugfs,
3900fe248c3aSMaor Gottlieb 				    &dev->delay_drop,
3901fe248c3aSMaor Gottlieb 				    &fops_delay_drop_timeout);
3902fe248c3aSMaor Gottlieb 	if (!dbg->timeout_debugfs)
3903fe248c3aSMaor Gottlieb 		goto out_debugfs;
3904fe248c3aSMaor Gottlieb 
3905fe248c3aSMaor Gottlieb 	return 0;
3906fe248c3aSMaor Gottlieb 
3907fe248c3aSMaor Gottlieb out_debugfs:
3908fe248c3aSMaor Gottlieb 	delay_drop_debugfs_cleanup(dev);
3909fe248c3aSMaor Gottlieb 	return -ENOMEM;
391003404e8aSMaor Gottlieb }
391103404e8aSMaor Gottlieb 
391203404e8aSMaor Gottlieb static void init_delay_drop(struct mlx5_ib_dev *dev)
391303404e8aSMaor Gottlieb {
391403404e8aSMaor Gottlieb 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
391503404e8aSMaor Gottlieb 		return;
391603404e8aSMaor Gottlieb 
391703404e8aSMaor Gottlieb 	mutex_init(&dev->delay_drop.lock);
391803404e8aSMaor Gottlieb 	dev->delay_drop.dev = dev;
391903404e8aSMaor Gottlieb 	dev->delay_drop.activate = false;
392003404e8aSMaor Gottlieb 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
392103404e8aSMaor Gottlieb 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
3922fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
3923fe248c3aSMaor Gottlieb 	atomic_set(&dev->delay_drop.events_cnt, 0);
3924fe248c3aSMaor Gottlieb 
3925fe248c3aSMaor Gottlieb 	if (delay_drop_debugfs_init(dev))
3926fe248c3aSMaor Gottlieb 		mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
392703404e8aSMaor Gottlieb }
392803404e8aSMaor Gottlieb 
392984305d71SLeon Romanovsky static const struct cpumask *
393084305d71SLeon Romanovsky mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
393140b24403SSagi Grimberg {
393240b24403SSagi Grimberg 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
393340b24403SSagi Grimberg 
393440b24403SSagi Grimberg 	return mlx5_get_vector_affinity(dev->mdev, comp_vector);
393540b24403SSagi Grimberg }
393640b24403SSagi Grimberg 
39379603b61dSJack Morgenstein static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
3938e126ba97SEli Cohen {
3939e126ba97SEli Cohen 	struct mlx5_ib_dev *dev;
3940ebd61f68SAchiad Shochat 	enum rdma_link_layer ll;
3941ebd61f68SAchiad Shochat 	int port_type_cap;
39424babcf97SAviv Heller 	const char *name;
3943e126ba97SEli Cohen 	int err;
3944e126ba97SEli Cohen 	int i;
3945e126ba97SEli Cohen 
3946ebd61f68SAchiad Shochat 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
3947ebd61f68SAchiad Shochat 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
3948ebd61f68SAchiad Shochat 
3949e126ba97SEli Cohen 	printk_once(KERN_INFO "%s", mlx5_version);
3950e126ba97SEli Cohen 
3951e126ba97SEli Cohen 	dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
3952e126ba97SEli Cohen 	if (!dev)
39539603b61dSJack Morgenstein 		return NULL;
3954e126ba97SEli Cohen 
39559603b61dSJack Morgenstein 	dev->mdev = mdev;
3956e126ba97SEli Cohen 
39570837e86aSMark Bloch 	dev->port = kcalloc(MLX5_CAP_GEN(mdev, num_ports), sizeof(*dev->port),
39580837e86aSMark Bloch 			    GFP_KERNEL);
39590837e86aSMark Bloch 	if (!dev->port)
39600837e86aSMark Bloch 		goto err_dealloc;
39610837e86aSMark Bloch 
3962fc24fc5eSAchiad Shochat 	rwlock_init(&dev->roce.netdev_lock);
3963e126ba97SEli Cohen 	err = get_port_caps(dev);
3964e126ba97SEli Cohen 	if (err)
39650837e86aSMark Bloch 		goto err_free_port;
3966e126ba97SEli Cohen 
39671b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3968e126ba97SEli Cohen 		get_ext_port_caps(dev);
3969e126ba97SEli Cohen 
39704babcf97SAviv Heller 	if (!mlx5_lag_is_active(mdev))
39714babcf97SAviv Heller 		name = "mlx5_%d";
39724babcf97SAviv Heller 	else
39734babcf97SAviv Heller 		name = "mlx5_bond_%d";
39744babcf97SAviv Heller 
39754babcf97SAviv Heller 	strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
3976e126ba97SEli Cohen 	dev->ib_dev.owner		= THIS_MODULE;
3977e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3978c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3979938fe83cSSaeed Mahameed 	dev->num_ports		= MLX5_CAP_GEN(mdev, num_ports);
3980e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt     = dev->num_ports;
3981233d05d2SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    =
3982233d05d2SSaeed Mahameed 		dev->mdev->priv.eq_table.num_comp_vectors;
39839b0c289eSBart Van Assche 	dev->ib_dev.dev.parent		= &mdev->pdev->dev;
3984e126ba97SEli Cohen 
3985e126ba97SEli Cohen 	dev->ib_dev.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION;
3986e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
3987e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
3988e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
3989e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
3990e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
3991e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
399241c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
399341c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
3994e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
399556e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
3996e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
3997e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
3998e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
3999e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
4000e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
4001e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
4002e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
4003e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
4004e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
4005e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
4006e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
4007e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
4008e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
4009e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
4010e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
4011e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
4012e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
40131707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
4014d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
4015d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
40167d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
4017b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
4018b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
4019e126ba97SEli Cohen 
4020e126ba97SEli Cohen 	dev->ib_dev.query_device	= mlx5_ib_query_device;
4021e126ba97SEli Cohen 	dev->ib_dev.query_port		= mlx5_ib_query_port;
4022ebd61f68SAchiad Shochat 	dev->ib_dev.get_link_layer	= mlx5_ib_port_link_layer;
4023fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
4024fc24fc5eSAchiad Shochat 		dev->ib_dev.get_netdev	= mlx5_ib_get_netdev;
4025e126ba97SEli Cohen 	dev->ib_dev.query_gid		= mlx5_ib_query_gid;
40263cca2606SAchiad Shochat 	dev->ib_dev.add_gid		= mlx5_ib_add_gid;
40273cca2606SAchiad Shochat 	dev->ib_dev.del_gid		= mlx5_ib_del_gid;
4028e126ba97SEli Cohen 	dev->ib_dev.query_pkey		= mlx5_ib_query_pkey;
4029e126ba97SEli Cohen 	dev->ib_dev.modify_device	= mlx5_ib_modify_device;
4030e126ba97SEli Cohen 	dev->ib_dev.modify_port		= mlx5_ib_modify_port;
4031e126ba97SEli Cohen 	dev->ib_dev.alloc_ucontext	= mlx5_ib_alloc_ucontext;
4032e126ba97SEli Cohen 	dev->ib_dev.dealloc_ucontext	= mlx5_ib_dealloc_ucontext;
4033e126ba97SEli Cohen 	dev->ib_dev.mmap		= mlx5_ib_mmap;
4034e126ba97SEli Cohen 	dev->ib_dev.alloc_pd		= mlx5_ib_alloc_pd;
4035e126ba97SEli Cohen 	dev->ib_dev.dealloc_pd		= mlx5_ib_dealloc_pd;
4036e126ba97SEli Cohen 	dev->ib_dev.create_ah		= mlx5_ib_create_ah;
4037e126ba97SEli Cohen 	dev->ib_dev.query_ah		= mlx5_ib_query_ah;
4038e126ba97SEli Cohen 	dev->ib_dev.destroy_ah		= mlx5_ib_destroy_ah;
4039e126ba97SEli Cohen 	dev->ib_dev.create_srq		= mlx5_ib_create_srq;
4040e126ba97SEli Cohen 	dev->ib_dev.modify_srq		= mlx5_ib_modify_srq;
4041e126ba97SEli Cohen 	dev->ib_dev.query_srq		= mlx5_ib_query_srq;
4042e126ba97SEli Cohen 	dev->ib_dev.destroy_srq		= mlx5_ib_destroy_srq;
4043e126ba97SEli Cohen 	dev->ib_dev.post_srq_recv	= mlx5_ib_post_srq_recv;
4044e126ba97SEli Cohen 	dev->ib_dev.create_qp		= mlx5_ib_create_qp;
4045e126ba97SEli Cohen 	dev->ib_dev.modify_qp		= mlx5_ib_modify_qp;
4046e126ba97SEli Cohen 	dev->ib_dev.query_qp		= mlx5_ib_query_qp;
4047e126ba97SEli Cohen 	dev->ib_dev.destroy_qp		= mlx5_ib_destroy_qp;
4048e126ba97SEli Cohen 	dev->ib_dev.post_send		= mlx5_ib_post_send;
4049e126ba97SEli Cohen 	dev->ib_dev.post_recv		= mlx5_ib_post_recv;
4050e126ba97SEli Cohen 	dev->ib_dev.create_cq		= mlx5_ib_create_cq;
4051e126ba97SEli Cohen 	dev->ib_dev.modify_cq		= mlx5_ib_modify_cq;
4052e126ba97SEli Cohen 	dev->ib_dev.resize_cq		= mlx5_ib_resize_cq;
4053e126ba97SEli Cohen 	dev->ib_dev.destroy_cq		= mlx5_ib_destroy_cq;
4054e126ba97SEli Cohen 	dev->ib_dev.poll_cq		= mlx5_ib_poll_cq;
4055e126ba97SEli Cohen 	dev->ib_dev.req_notify_cq	= mlx5_ib_arm_cq;
4056e126ba97SEli Cohen 	dev->ib_dev.get_dma_mr		= mlx5_ib_get_dma_mr;
4057e126ba97SEli Cohen 	dev->ib_dev.reg_user_mr		= mlx5_ib_reg_user_mr;
405856e11d62SNoa Osherovich 	dev->ib_dev.rereg_user_mr	= mlx5_ib_rereg_user_mr;
4059e126ba97SEli Cohen 	dev->ib_dev.dereg_mr		= mlx5_ib_dereg_mr;
4060e126ba97SEli Cohen 	dev->ib_dev.attach_mcast	= mlx5_ib_mcg_attach;
4061e126ba97SEli Cohen 	dev->ib_dev.detach_mcast	= mlx5_ib_mcg_detach;
4062e126ba97SEli Cohen 	dev->ib_dev.process_mad		= mlx5_ib_process_mad;
40639bee178bSSagi Grimberg 	dev->ib_dev.alloc_mr		= mlx5_ib_alloc_mr;
40648a187ee5SSagi Grimberg 	dev->ib_dev.map_mr_sg		= mlx5_ib_map_mr_sg;
4065d5436ba0SSagi Grimberg 	dev->ib_dev.check_mr_status	= mlx5_ib_check_mr_status;
40667738613eSIra Weiny 	dev->ib_dev.get_port_immutable  = mlx5_port_immutable;
4067c7342823SIra Weiny 	dev->ib_dev.get_dev_fw_str      = get_dev_fw_str;
406840b24403SSagi Grimberg 	dev->ib_dev.get_vector_affinity	= mlx5_ib_get_vector_affinity;
40698e959601SNiranjana Vishwanathapura 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
4070693dfd5aSErez Shitrit 		dev->ib_dev.alloc_rdma_netdev	= mlx5_ib_alloc_rdma_netdev;
40718e959601SNiranjana Vishwanathapura 
4072eff901d3SEli Cohen 	if (mlx5_core_is_pf(mdev)) {
4073eff901d3SEli Cohen 		dev->ib_dev.get_vf_config	= mlx5_ib_get_vf_config;
4074eff901d3SEli Cohen 		dev->ib_dev.set_vf_link_state	= mlx5_ib_set_vf_link_state;
4075eff901d3SEli Cohen 		dev->ib_dev.get_vf_stats	= mlx5_ib_get_vf_stats;
4076eff901d3SEli Cohen 		dev->ib_dev.set_vf_guid		= mlx5_ib_set_vf_guid;
4077eff901d3SEli Cohen 	}
4078e126ba97SEli Cohen 
40797c2344c3SMaor Gottlieb 	dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
40807c2344c3SMaor Gottlieb 
4081938fe83cSSaeed Mahameed 	mlx5_ib_internal_fill_odp_caps(dev);
40828cdd312cSHaggai Eran 
40836e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
40846e8484c5SMax Gurtovoy 
4085d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
4086d2370e0aSMatan Barak 		dev->ib_dev.alloc_mw		= mlx5_ib_alloc_mw;
4087d2370e0aSMatan Barak 		dev->ib_dev.dealloc_mw		= mlx5_ib_dealloc_mw;
4088d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
4089d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
4090d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
4091d2370e0aSMatan Barak 	}
4092d2370e0aSMatan Barak 
40937c16f477SKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
40940ad17a8fSMark Bloch 		dev->ib_dev.get_hw_stats	= mlx5_ib_get_hw_stats;
40950ad17a8fSMark Bloch 		dev->ib_dev.alloc_hw_stats	= mlx5_ib_alloc_hw_stats;
40960ad17a8fSMark Bloch 	}
40970ad17a8fSMark Bloch 
4098938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
4099e126ba97SEli Cohen 		dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
4100e126ba97SEli Cohen 		dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
4101e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
4102e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4103e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
4104e126ba97SEli Cohen 	}
4105e126ba97SEli Cohen 
4106038d2ef8SMaor Gottlieb 	dev->ib_dev.create_flow	= mlx5_ib_create_flow;
4107038d2ef8SMaor Gottlieb 	dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
410881e30880SYishai Hadas 	dev->ib_dev.uverbs_ex_cmd_mask |=
410981e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
411081e30880SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
411181e30880SYishai Hadas 
411281e30880SYishai Hadas 	if (mlx5_ib_port_link_layer(&dev->ib_dev, 1) ==
411381e30880SYishai Hadas 	    IB_LINK_LAYER_ETHERNET) {
411479b20a6cSYishai Hadas 		dev->ib_dev.create_wq	 = mlx5_ib_create_wq;
411579b20a6cSYishai Hadas 		dev->ib_dev.modify_wq	 = mlx5_ib_modify_wq;
411679b20a6cSYishai Hadas 		dev->ib_dev.destroy_wq	 = mlx5_ib_destroy_wq;
4117c5f90929SYishai Hadas 		dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
4118c5f90929SYishai Hadas 		dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
4119038d2ef8SMaor Gottlieb 		dev->ib_dev.uverbs_ex_cmd_mask |=
412079b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
412179b20a6cSYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4122c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4123c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4124c5f90929SYishai Hadas 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
4125038d2ef8SMaor Gottlieb 	}
4126e126ba97SEli Cohen 	err = init_node_data(dev);
4127e126ba97SEli Cohen 	if (err)
412890be7c8aSMajd Dibbiny 		goto err_free_port;
4129e126ba97SEli Cohen 
4130038d2ef8SMaor Gottlieb 	mutex_init(&dev->flow_db.lock);
4131e126ba97SEli Cohen 	mutex_init(&dev->cap_mask_mutex);
413289ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&dev->qp_list);
413389ea94a7SMaor Gottlieb 	spin_lock_init(&dev->reset_flow_resource_lock);
4134e126ba97SEli Cohen 
4135fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET) {
413645f95acdSOr Gerlitz 		err = mlx5_enable_eth(dev);
4137e126ba97SEli Cohen 		if (err)
413890be7c8aSMajd Dibbiny 			goto err_free_port;
4139fd65f1b8SMoni Shoua 		dev->roce.last_port_state = IB_PORT_DOWN;
4140fc24fc5eSAchiad Shochat 	}
4141fc24fc5eSAchiad Shochat 
4142fc24fc5eSAchiad Shochat 	err = create_dev_resources(&dev->devr);
4143fc24fc5eSAchiad Shochat 	if (err)
414445f95acdSOr Gerlitz 		goto err_disable_eth;
4145e126ba97SEli Cohen 
41466aec21f6SHaggai Eran 	err = mlx5_ib_odp_init_one(dev);
4147281d1a92SWei Yongjun 	if (err)
4148e126ba97SEli Cohen 		goto err_rsrc;
4149e126ba97SEli Cohen 
415045bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
4151e1f24a79SParav Pandit 		err = mlx5_ib_alloc_counters(dev);
41526aec21f6SHaggai Eran 		if (err)
41536aec21f6SHaggai Eran 			goto err_odp;
415445bded2cSKamal Heib 	}
41556aec21f6SHaggai Eran 
41564a2da0b8SParav Pandit 	err = mlx5_ib_init_cong_debugfs(dev);
41574a2da0b8SParav Pandit 	if (err)
41584a2da0b8SParav Pandit 		goto err_cnt;
41594a2da0b8SParav Pandit 
41605fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
41615fe9dec0SEli Cohen 	if (!dev->mdev->priv.uar)
41624a2da0b8SParav Pandit 		goto err_cong;
41635fe9dec0SEli Cohen 
41645fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
41655fe9dec0SEli Cohen 	if (err)
41665fe9dec0SEli Cohen 		goto err_uar_page;
41675fe9dec0SEli Cohen 
41685fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
41695fe9dec0SEli Cohen 	if (err)
41705fe9dec0SEli Cohen 		goto err_bfreg;
41715fe9dec0SEli Cohen 
41720837e86aSMark Bloch 	err = ib_register_device(&dev->ib_dev, NULL);
41730837e86aSMark Bloch 	if (err)
41745fe9dec0SEli Cohen 		goto err_fp_bfreg;
41750837e86aSMark Bloch 
4176e126ba97SEli Cohen 	err = create_umr_res(dev);
4177e126ba97SEli Cohen 	if (err)
4178e126ba97SEli Cohen 		goto err_dev;
4179e126ba97SEli Cohen 
418003404e8aSMaor Gottlieb 	init_delay_drop(dev);
418103404e8aSMaor Gottlieb 
4182e126ba97SEli Cohen 	for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
4183281d1a92SWei Yongjun 		err = device_create_file(&dev->ib_dev.dev,
4184281d1a92SWei Yongjun 					 mlx5_class_attributes[i]);
4185281d1a92SWei Yongjun 		if (err)
418603404e8aSMaor Gottlieb 			goto err_delay_drop;
4187e126ba97SEli Cohen 	}
4188e126ba97SEli Cohen 
4189c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4190c85023e1SHuy Nguyen 	    MLX5_CAP_GEN(mdev, disable_local_lb))
4191c85023e1SHuy Nguyen 		mutex_init(&dev->lb_mutex);
4192c85023e1SHuy Nguyen 
4193e126ba97SEli Cohen 	dev->ib_active = true;
4194e126ba97SEli Cohen 
41959603b61dSJack Morgenstein 	return dev;
4196e126ba97SEli Cohen 
419703404e8aSMaor Gottlieb err_delay_drop:
419803404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4199e126ba97SEli Cohen 	destroy_umrc_res(dev);
4200e126ba97SEli Cohen 
4201e126ba97SEli Cohen err_dev:
4202e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
4203e126ba97SEli Cohen 
42045fe9dec0SEli Cohen err_fp_bfreg:
42055fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42065fe9dec0SEli Cohen 
42075fe9dec0SEli Cohen err_bfreg:
42085fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42095fe9dec0SEli Cohen 
42105fe9dec0SEli Cohen err_uar_page:
42115fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
42125fe9dec0SEli Cohen 
42134a2da0b8SParav Pandit err_cong:
4214e19cd282SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
4215e19cd282SParav Pandit err_cnt:
421645bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4217e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
42180837e86aSMark Bloch 
42196aec21f6SHaggai Eran err_odp:
42206aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
42216aec21f6SHaggai Eran 
4222e126ba97SEli Cohen err_rsrc:
4223e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4224e126ba97SEli Cohen 
422545f95acdSOr Gerlitz err_disable_eth:
42265ec8c83eSAviv Heller 	if (ll == IB_LINK_LAYER_ETHERNET) {
422745f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
4228d012f5d6SOr Gerlitz 		mlx5_remove_netdev_notifier(dev);
42295ec8c83eSAviv Heller 	}
4230fc24fc5eSAchiad Shochat 
42310837e86aSMark Bloch err_free_port:
42320837e86aSMark Bloch 	kfree(dev->port);
42330837e86aSMark Bloch 
42349603b61dSJack Morgenstein err_dealloc:
4235e126ba97SEli Cohen 	ib_dealloc_device((struct ib_device *)dev);
4236e126ba97SEli Cohen 
42379603b61dSJack Morgenstein 	return NULL;
4238e126ba97SEli Cohen }
4239e126ba97SEli Cohen 
42409603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
4241e126ba97SEli Cohen {
42429603b61dSJack Morgenstein 	struct mlx5_ib_dev *dev = context;
4243fc24fc5eSAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev, 1);
42446aec21f6SHaggai Eran 
424503404e8aSMaor Gottlieb 	cancel_delay_drop(dev);
4246d012f5d6SOr Gerlitz 	mlx5_remove_netdev_notifier(dev);
4247e126ba97SEli Cohen 	ib_unregister_device(&dev->ib_dev);
42485fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
42495fe9dec0SEli Cohen 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42505fe9dec0SEli Cohen 	mlx5_put_uars_page(dev->mdev, mdev->priv.uar);
42514a2da0b8SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev);
425245bded2cSKamal Heib 	if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
4253e1f24a79SParav Pandit 		mlx5_ib_dealloc_counters(dev);
4254eefd56e5SEli Cohen 	destroy_umrc_res(dev);
42556aec21f6SHaggai Eran 	mlx5_ib_odp_remove_one(dev);
4256e126ba97SEli Cohen 	destroy_dev_resources(&dev->devr);
4257fc24fc5eSAchiad Shochat 	if (ll == IB_LINK_LAYER_ETHERNET)
425845f95acdSOr Gerlitz 		mlx5_disable_eth(dev);
42590837e86aSMark Bloch 	kfree(dev->port);
4260e126ba97SEli Cohen 	ib_dealloc_device(&dev->ib_dev);
4261e126ba97SEli Cohen }
4262e126ba97SEli Cohen 
42639603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
42649603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
42659603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
42669603b61dSJack Morgenstein 	.event          = mlx5_ib_event,
4267d9aaed83SArtemy Kovalyov #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
4268d9aaed83SArtemy Kovalyov 	.pfault		= mlx5_ib_pfault,
4269d9aaed83SArtemy Kovalyov #endif
427064613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
4271e126ba97SEli Cohen };
4272e126ba97SEli Cohen 
4273e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4274e126ba97SEli Cohen {
42756aec21f6SHaggai Eran 	int err;
42766aec21f6SHaggai Eran 
427781713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
42789603b61dSJack Morgenstein 
42796aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
42806aec21f6SHaggai Eran 
42816aec21f6SHaggai Eran 	return err;
4282e126ba97SEli Cohen }
4283e126ba97SEli Cohen 
4284e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4285e126ba97SEli Cohen {
42869603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
4287e126ba97SEli Cohen }
4288e126ba97SEli Cohen 
4289e126ba97SEli Cohen module_init(mlx5_ib_init);
4290e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4291