xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 91a7c58fce065506fd98954d27694e5d83c96638)
1b572ebe6SLeon Romanovsky // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2e126ba97SEli Cohen /*
3b572ebe6SLeon Romanovsky  * Copyright (c) 2013-2020, Mellanox Technologies inc. All rights reserved.
4e126ba97SEli Cohen  */
5e126ba97SEli Cohen 
6fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
7adec640eSChristoph Hellwig #include <linux/highmem.h>
8e126ba97SEli Cohen #include <linux/module.h>
9e126ba97SEli Cohen #include <linux/init.h>
10e126ba97SEli Cohen #include <linux/errno.h>
11e126ba97SEli Cohen #include <linux/pci.h>
12e126ba97SEli Cohen #include <linux/dma-mapping.h>
13e126ba97SEli Cohen #include <linux/slab.h>
1424da0016SAriel Levkovich #include <linux/bitmap.h>
15e126ba97SEli Cohen #include <linux/sched.h>
166e84f315SIngo Molnar #include <linux/sched/mm.h>
170881e7bdSIngo Molnar #include <linux/sched/task.h>
187c2344c3SMaor Gottlieb #include <linux/delay.h>
19e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
203f89a643SAchiad Shochat #include <rdma/ib_addr.h>
212811ba51SAchiad Shochat #include <rdma/ib_cache.h>
22ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
231b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
2472c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
25cecae747SMaor Gottlieb #include <linux/mlx5/eswitch.h>
267c2344c3SMaor Gottlieb #include <linux/list.h>
27e126ba97SEli Cohen #include <rdma/ib_smi.h>
28e126ba97SEli Cohen #include <rdma/ib_umem.h>
29cfc1a89eSMaor Gottlieb #include <rdma/lag.h>
30038d2ef8SMaor Gottlieb #include <linux/in.h>
31038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
32e126ba97SEli Cohen #include "mlx5_ib.h"
33fc385b7aSMark Bloch #include "ib_rep.h"
34e1f24a79SParav Pandit #include "cmd.h"
35d8b7515eSLeon Romanovsky #include "devx.h"
36f7c4ffdaSLeon Romanovsky #include "fs.h"
37f3da6577SLeon Romanovsky #include "srq.h"
38333fbaa0SLeon Romanovsky #include "qp.h"
39029e88fdSLeon Romanovsky #include "wr.h"
40b572ebe6SLeon Romanovsky #include "restrack.h"
4164825827SLeon Romanovsky #include "counters.h"
42c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
438c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
44c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
45c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
464061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h>
478c84660bSMatan Barak 
488c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
498c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
50e126ba97SEli Cohen 
51e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
52daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
53e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
54e126ba97SEli Cohen 
55d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
56d69a24e0SDaniel Jurgens 	struct work_struct	work;
57df097a27SSaeed Mahameed 	union {
58df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
59df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
60df097a27SSaeed Mahameed 	};
61df097a27SSaeed Mahameed 	bool			is_slave;
62134e9349SSaeed Mahameed 	unsigned int		event;
63df097a27SSaeed Mahameed 	void			*param;
64d69a24e0SDaniel Jurgens };
65d69a24e0SDaniel Jurgens 
66da7525d2SEran Ben Elisha enum {
67da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
68da7525d2SEran Ben Elisha };
691b5daf11SMajd Dibbiny 
70d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
7132f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
7232f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
7332f69e4bSDaniel Jurgens /*
7432f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
7532f69e4bSDaniel Jurgens  */
7632f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
7732f69e4bSDaniel Jurgens 
78c44ef998SIlya Lesokhin /* We can't use an array for xlt_emergency_page because dma_map_single
79c44ef998SIlya Lesokhin  * doesn't work on kernel modules memory
80c44ef998SIlya Lesokhin  */
81c44ef998SIlya Lesokhin static unsigned long xlt_emergency_page;
82c44ef998SIlya Lesokhin static struct mutex xlt_emergency_page_mutex;
83c44ef998SIlya Lesokhin 
8432f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
8532f69e4bSDaniel Jurgens {
8632f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
8732f69e4bSDaniel Jurgens 
8832f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
8932f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
9032f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
9132f69e4bSDaniel Jurgens 	return dev;
9232f69e4bSDaniel Jurgens }
9332f69e4bSDaniel Jurgens 
941b5daf11SMajd Dibbiny static enum rdma_link_layer
95ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
961b5daf11SMajd Dibbiny {
97ebd61f68SAchiad Shochat 	switch (port_type_cap) {
981b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
991b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
1001b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
1011b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
1021b5daf11SMajd Dibbiny 	default:
1031b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1041b5daf11SMajd Dibbiny 	}
1051b5daf11SMajd Dibbiny }
1061b5daf11SMajd Dibbiny 
107ebd61f68SAchiad Shochat static enum rdma_link_layer
108ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
109ebd61f68SAchiad Shochat {
110ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
111ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
112ebd61f68SAchiad Shochat 
113ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
114ebd61f68SAchiad Shochat }
115ebd61f68SAchiad Shochat 
116fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
117fd65f1b8SMoni Shoua 			  u8 port_num,
118fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
119fd65f1b8SMoni Shoua {
120fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
121fd65f1b8SMoni Shoua 	int ret;
122fd65f1b8SMoni Shoua 
123fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1243023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
125fd65f1b8SMoni Shoua 	if (!ret)
126fd65f1b8SMoni Shoua 		*state = attr.state;
127fd65f1b8SMoni Shoua 	return ret;
128fd65f1b8SMoni Shoua }
129fd65f1b8SMoni Shoua 
13035b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
13135b0aa67SMark Bloch 					   struct net_device *ndev,
13235b0aa67SMark Bloch 					   u8 *port_num)
13335b0aa67SMark Bloch {
13435b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
13535b0aa67SMark Bloch 	struct net_device *rep_ndev;
13635b0aa67SMark Bloch 	struct mlx5_ib_port *port;
13735b0aa67SMark Bloch 	int i;
13835b0aa67SMark Bloch 
13935b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
14035b0aa67SMark Bloch 		port  = &dev->port[i];
14135b0aa67SMark Bloch 		if (!port->rep)
14235b0aa67SMark Bloch 			continue;
14335b0aa67SMark Bloch 
14435b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
14535b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
14635b0aa67SMark Bloch 						  port->rep->vport);
14735b0aa67SMark Bloch 		if (rep_ndev == ndev) {
14835b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
14935b0aa67SMark Bloch 			*port_num = i + 1;
15035b0aa67SMark Bloch 			return &port->roce;
15135b0aa67SMark Bloch 		}
15235b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
15335b0aa67SMark Bloch 	}
15435b0aa67SMark Bloch 
15535b0aa67SMark Bloch 	return NULL;
15635b0aa67SMark Bloch }
15735b0aa67SMark Bloch 
158fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
159fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
160fc24fc5eSAchiad Shochat {
1617fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
162fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1637fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1647fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1657fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1667fd8aefbSDaniel Jurgens 
1677fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
16832f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
16932f69e4bSDaniel Jurgens 	if (!mdev)
17032f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
171fc24fc5eSAchiad Shochat 
1725ec8c83eSAviv Heller 	switch (event) {
1735ec8c83eSAviv Heller 	case NETDEV_REGISTER:
17435b0aa67SMark Bloch 		/* Should already be registered during the load */
17535b0aa67SMark Bloch 		if (ibdev->is_rep)
17635b0aa67SMark Bloch 			break;
1777fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
178dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
179842a9c83SOr Gerlitz 			roce->netdev = ndev;
1807fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1815ec8c83eSAviv Heller 		break;
1825ec8c83eSAviv Heller 
183842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
18435b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
185842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
186842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
187842a9c83SOr Gerlitz 			roce->netdev = NULL;
188842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
189842a9c83SOr Gerlitz 		break;
190842a9c83SOr Gerlitz 
191fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1925ec8c83eSAviv Heller 	case NETDEV_UP:
19388621dfeSAviv Heller 	case NETDEV_DOWN: {
1947fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
19588621dfeSAviv Heller 		struct net_device *upper = NULL;
19688621dfeSAviv Heller 
19788621dfeSAviv Heller 		if (lag_ndev) {
19888621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
19988621dfeSAviv Heller 			dev_put(lag_ndev);
20088621dfeSAviv Heller 		}
20188621dfeSAviv Heller 
20235b0aa67SMark Bloch 		if (ibdev->is_rep)
20335b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
20435b0aa67SMark Bloch 		if (!roce)
20535b0aa67SMark Bloch 			return NOTIFY_DONE;
2067fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
20788621dfeSAviv Heller 		    && ibdev->ib_active) {
208626bc02dSBart Van Assche 			struct ib_event ibev = { };
209fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2105ec8c83eSAviv Heller 
2117fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2127fd8aefbSDaniel Jurgens 					   &port_state))
2137fd8aefbSDaniel Jurgens 				goto done;
214fd65f1b8SMoni Shoua 
2157fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2167fd8aefbSDaniel Jurgens 				goto done;
217fd65f1b8SMoni Shoua 
2187fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2195ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
220fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
221fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
222fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
223fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
224fd65f1b8SMoni Shoua 			else
2257fd8aefbSDaniel Jurgens 				goto done;
226fd65f1b8SMoni Shoua 
2277fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2285ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2295ec8c83eSAviv Heller 		}
2305ec8c83eSAviv Heller 		break;
23188621dfeSAviv Heller 	}
2325ec8c83eSAviv Heller 
2335ec8c83eSAviv Heller 	default:
2345ec8c83eSAviv Heller 		break;
2355ec8c83eSAviv Heller 	}
2367fd8aefbSDaniel Jurgens done:
23732f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
238fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
239fc24fc5eSAchiad Shochat }
240fc24fc5eSAchiad Shochat 
241fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
242fc24fc5eSAchiad Shochat 					     u8 port_num)
243fc24fc5eSAchiad Shochat {
244fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
245fc24fc5eSAchiad Shochat 	struct net_device *ndev;
24632f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
247fc24fc5eSAchiad Shochat 
24832f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
24932f69e4bSDaniel Jurgens 	if (!mdev)
25032f69e4bSDaniel Jurgens 		return NULL;
25132f69e4bSDaniel Jurgens 
25232f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
25388621dfeSAviv Heller 	if (ndev)
25432f69e4bSDaniel Jurgens 		goto out;
25588621dfeSAviv Heller 
256fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
257fc24fc5eSAchiad Shochat 	 */
25895579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
25995579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
260fc24fc5eSAchiad Shochat 	if (ndev)
261fc24fc5eSAchiad Shochat 		dev_hold(ndev);
26295579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
263fc24fc5eSAchiad Shochat 
26432f69e4bSDaniel Jurgens out:
26532f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
266fc24fc5eSAchiad Shochat 	return ndev;
267fc24fc5eSAchiad Shochat }
268fc24fc5eSAchiad Shochat 
26932f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
27032f69e4bSDaniel Jurgens 						   u8 ib_port_num,
27132f69e4bSDaniel Jurgens 						   u8 *native_port_num)
27232f69e4bSDaniel Jurgens {
27332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
27432f69e4bSDaniel Jurgens 							  ib_port_num);
27532f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
27632f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
27732f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
27832f69e4bSDaniel Jurgens 
279210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
280210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
281210b1f78SMark Bloch 		if (native_port_num)
282210b1f78SMark Bloch 			*native_port_num = ib_port_num;
283210b1f78SMark Bloch 		return ibdev->mdev;
284210b1f78SMark Bloch 	}
285210b1f78SMark Bloch 
28632f69e4bSDaniel Jurgens 	if (native_port_num)
28732f69e4bSDaniel Jurgens 		*native_port_num = 1;
28832f69e4bSDaniel Jurgens 
28932f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
29032f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
29132f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
29232f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
29332f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
29432f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
29532f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
29632f69e4bSDaniel Jurgens 		 */
29732f69e4bSDaniel Jurgens 		if (!mpi->is_master)
29832f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
29932f69e4bSDaniel Jurgens 	}
30032f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
30132f69e4bSDaniel Jurgens 
30232f69e4bSDaniel Jurgens 	return mdev;
30332f69e4bSDaniel Jurgens }
30432f69e4bSDaniel Jurgens 
30532f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
30632f69e4bSDaniel Jurgens {
30732f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30832f69e4bSDaniel Jurgens 							  port_num);
30932f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
31032f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
31132f69e4bSDaniel Jurgens 
31232f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
31332f69e4bSDaniel Jurgens 		return;
31432f69e4bSDaniel Jurgens 
31532f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
31632f69e4bSDaniel Jurgens 
31732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
31832f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
31932f69e4bSDaniel Jurgens 	if (mpi->is_master)
32032f69e4bSDaniel Jurgens 		goto out;
32132f69e4bSDaniel Jurgens 
32232f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
32332f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
32432f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
32532f69e4bSDaniel Jurgens out:
32632f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
32732f69e4bSDaniel Jurgens }
32832f69e4bSDaniel Jurgens 
32908e8676fSAya Levin static int translate_eth_legacy_proto_oper(u32 eth_proto_oper, u8 *active_speed,
330f1b65df5SNoa Osherovich 					   u8 *active_width)
331f1b65df5SNoa Osherovich {
332f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
333f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
334f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
335f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
336f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
337f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
338f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
339f1b65df5SNoa Osherovich 		break;
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
342f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
343f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
344f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
345f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
346f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
347f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
348f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
349f1b65df5SNoa Osherovich 		break;
350f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
351f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
352f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
353f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
354f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
355f1b65df5SNoa Osherovich 		break;
356f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
357f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
358f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
359f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
360f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
361f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
362f1b65df5SNoa Osherovich 		break;
363f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
364f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
366f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
367f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
368f1b65df5SNoa Osherovich 		break;
369f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
370f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
371f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
372f1b65df5SNoa Osherovich 		break;
373f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
374f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
375f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
376f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
377f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
378f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
379f1b65df5SNoa Osherovich 		break;
380f1b65df5SNoa Osherovich 	default:
381f1b65df5SNoa Osherovich 		return -EINVAL;
382f1b65df5SNoa Osherovich 	}
383f1b65df5SNoa Osherovich 
384f1b65df5SNoa Osherovich 	return 0;
385f1b65df5SNoa Osherovich }
386f1b65df5SNoa Osherovich 
38708e8676fSAya Levin static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u8 *active_speed,
38808e8676fSAya Levin 					u8 *active_width)
38908e8676fSAya Levin {
39008e8676fSAya Levin 	switch (eth_proto_oper) {
39108e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
39208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
39308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39408e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
39508e8676fSAya Levin 		break;
39608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
39708e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39808e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
39908e8676fSAya Levin 		break;
40008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
40108e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40208e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
40308e8676fSAya Levin 		break;
40408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
40508e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
40608e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
40708e8676fSAya Levin 		break;
40808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
40908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
41008e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
41108e8676fSAya Levin 		break;
41208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
413cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
414cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
415cd272875SAya Levin 		break;
41608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
41708e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
41808e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
41908e8676fSAya Levin 		break;
420cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
421cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
422cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
423cd272875SAya Levin 		break;
42408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
42508e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
42608e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
42708e8676fSAya Levin 		break;
42808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_200GAUI_4_200GBASE_CR4_KR4):
42908e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
43008e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
43108e8676fSAya Levin 		break;
43208e8676fSAya Levin 	default:
43308e8676fSAya Levin 		return -EINVAL;
43408e8676fSAya Levin 	}
43508e8676fSAya Levin 
43608e8676fSAya Levin 	return 0;
43708e8676fSAya Levin }
43808e8676fSAya Levin 
43908e8676fSAya Levin static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
44008e8676fSAya Levin 				    u8 *active_width, bool ext)
44108e8676fSAya Levin {
44208e8676fSAya Levin 	return ext ?
44308e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
44408e8676fSAya Levin 					     active_width) :
44508e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
44608e8676fSAya Levin 						active_width);
44708e8676fSAya Levin }
44808e8676fSAya Levin 
449095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4503f89a643SAchiad Shochat 				struct ib_port_attr *props)
4513f89a643SAchiad Shochat {
4523f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
453bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
454da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
45588621dfeSAviv Heller 	struct net_device *ndev, *upper;
4563f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
457b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
458c876a1b7SLeon Romanovsky 	u16 qkey_viol_cntr;
459f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
460b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
46108e8676fSAya Levin 	bool ext;
462095b0927SIlan Tayari 	int err;
4633f89a643SAchiad Shochat 
464b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
465b3cbd6f0SDaniel Jurgens 	if (!mdev) {
466b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
467b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
468b3cbd6f0SDaniel Jurgens 		 */
469b3cbd6f0SDaniel Jurgens 		put_mdev = false;
470b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
471b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
472b3cbd6f0SDaniel Jurgens 		port_num = 1;
473b3cbd6f0SDaniel Jurgens 	}
474b3cbd6f0SDaniel Jurgens 
475f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
476f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
47726628e2dSMark Bloch 	 * Use native port in case of reps
47850f22fd8SNoa Osherovich 	 */
47926628e2dSMark Bloch 	if (dev->is_rep)
48026628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
48126628e2dSMark Bloch 					   1);
48226628e2dSMark Bloch 	else
483bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
484b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
485095b0927SIlan Tayari 	if (err)
486b3cbd6f0SDaniel Jurgens 		goto out;
487530c8632SAya Levin 	ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability);
48808e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
489f1b65df5SNoa Osherovich 
4907672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
4917672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
4927672ed33SHonggang Li 
493f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
49408e8676fSAya Levin 				 &props->active_width, ext);
4953f89a643SAchiad Shochat 
4963f89a643SAchiad Shochat 	props->port_cap_flags |= IB_PORT_CM_SUP;
4972f944c0fSJason Gunthorpe 	props->ip_gids = true;
4983f89a643SAchiad Shochat 
4993f89a643SAchiad Shochat 	props->gid_tbl_len      = MLX5_CAP_ROCE(dev->mdev,
5003f89a643SAchiad Shochat 						roce_address_table_size);
5013f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5023f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5033f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5043f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
50572a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5063f89a643SAchiad Shochat 
507b3cbd6f0SDaniel Jurgens 	mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
508c876a1b7SLeon Romanovsky 	props->qkey_viol_cntr = qkey_viol_cntr;
5093f89a643SAchiad Shochat 
510b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
511b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
512b3cbd6f0SDaniel Jurgens 		goto out;
513b3cbd6f0SDaniel Jurgens 
5143f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5153f89a643SAchiad Shochat 	if (!ndev)
516b3cbd6f0SDaniel Jurgens 		goto out;
5173f89a643SAchiad Shochat 
5187c34ec19SAviv Heller 	if (dev->lag_active) {
51988621dfeSAviv Heller 		rcu_read_lock();
52088621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
52188621dfeSAviv Heller 		if (upper) {
52288621dfeSAviv Heller 			dev_put(ndev);
52388621dfeSAviv Heller 			ndev = upper;
52488621dfeSAviv Heller 			dev_hold(ndev);
52588621dfeSAviv Heller 		}
52688621dfeSAviv Heller 		rcu_read_unlock();
52788621dfeSAviv Heller 	}
52888621dfeSAviv Heller 
5293f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5303f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
53172a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5323f89a643SAchiad Shochat 	}
5333f89a643SAchiad Shochat 
5343f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5353f89a643SAchiad Shochat 
5363f89a643SAchiad Shochat 	dev_put(ndev);
5373f89a643SAchiad Shochat 
5383f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
539b3cbd6f0SDaniel Jurgens out:
540b3cbd6f0SDaniel Jurgens 	if (put_mdev)
541b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
542b3cbd6f0SDaniel Jurgens 	return err;
5433f89a643SAchiad Shochat }
5443f89a643SAchiad Shochat 
545095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
546095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
547095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5483cca2606SAchiad Shochat {
549095b0927SIlan Tayari 	enum ib_gid_type gid_type = IB_GID_TYPE_IB;
550a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
551095b0927SIlan Tayari 	u8 roce_version = 0;
552095b0927SIlan Tayari 	u8 roce_l3_type = 0;
553095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
554a70c0739SParav Pandit 	int ret;
5553cca2606SAchiad Shochat 
556095b0927SIlan Tayari 	if (gid) {
557095b0927SIlan Tayari 		gid_type = attr->gid_type;
558a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
559a70c0739SParav Pandit 		if (ret)
560a70c0739SParav Pandit 			return ret;
5613cca2606SAchiad Shochat 	}
5623cca2606SAchiad Shochat 
563095b0927SIlan Tayari 	switch (gid_type) {
5643cca2606SAchiad Shochat 	case IB_GID_TYPE_IB:
565095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5663cca2606SAchiad Shochat 		break;
5673cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
568095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
569095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
570095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
571095b0927SIlan Tayari 		else
572095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5733cca2606SAchiad Shochat 		break;
5743cca2606SAchiad Shochat 
5753cca2606SAchiad Shochat 	default:
576095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
5773cca2606SAchiad Shochat 	}
5783cca2606SAchiad Shochat 
579095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
580cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
581a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
582cf34e1feSParav Pandit 				      port_num);
5833cca2606SAchiad Shochat }
5843cca2606SAchiad Shochat 
585f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
5863cca2606SAchiad Shochat 			   __always_unused void **context)
5873cca2606SAchiad Shochat {
588414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
589f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
5903cca2606SAchiad Shochat }
5913cca2606SAchiad Shochat 
592414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
593414448d2SParav Pandit 			   __always_unused void **context)
5943cca2606SAchiad Shochat {
595414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
596414448d2SParav Pandit 			     attr->index, NULL, NULL);
5973cca2606SAchiad Shochat }
5983cca2606SAchiad Shochat 
5995ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
60047ec3866SParav Pandit 				   const struct ib_gid_attr *attr)
6012811ba51SAchiad Shochat {
60247ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6032811ba51SAchiad Shochat 		return 0;
6042811ba51SAchiad Shochat 
6052811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6062811ba51SAchiad Shochat }
6072811ba51SAchiad Shochat 
6081b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6091b5daf11SMajd Dibbiny {
6107fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
611d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6127fae6655SNoa Osherovich 	return 0;
6131b5daf11SMajd Dibbiny }
6141b5daf11SMajd Dibbiny 
6151b5daf11SMajd Dibbiny enum {
6161b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6171b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6181b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6191b5daf11SMajd Dibbiny };
6201b5daf11SMajd Dibbiny 
6211b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6221b5daf11SMajd Dibbiny {
6231b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6241b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6251b5daf11SMajd Dibbiny 
626ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6271b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6281b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6291b5daf11SMajd Dibbiny 
6301b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6311b5daf11SMajd Dibbiny }
6321b5daf11SMajd Dibbiny 
633da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
634776a3906SMoni Shoua 			    u8 atomic_size_qp,
635da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
636da7525d2SEran Ben Elisha {
637da7525d2SEran Ben Elisha 	u8 tmp;
638da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
639da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
640bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
641da7525d2SEran Ben Elisha 
642da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
643da7525d2SEran Ben Elisha 	 * of host endianness respond
644da7525d2SEran Ben Elisha 	 */
645da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
646da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
647da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
648da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
649da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
650da7525d2SEran Ben Elisha 	} else {
651da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
652da7525d2SEran Ben Elisha 	}
653da7525d2SEran Ben Elisha }
654da7525d2SEran Ben Elisha 
655776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
656776a3906SMoni Shoua 			       struct ib_device_attr *props)
657776a3906SMoni Shoua {
658776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
659776a3906SMoni Shoua 
660776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
661776a3906SMoni Shoua }
662776a3906SMoni Shoua 
6631b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6641b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6651b5daf11SMajd Dibbiny {
6661b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6671b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6681b5daf11SMajd Dibbiny 	u64 tmp;
6691b5daf11SMajd Dibbiny 	int err;
6701b5daf11SMajd Dibbiny 
6711b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6721b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6731b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6741b5daf11SMajd Dibbiny 							    sys_image_guid);
6751b5daf11SMajd Dibbiny 
6761b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6771b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6783f89a643SAchiad Shochat 		break;
6793f89a643SAchiad Shochat 
6803f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6813f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
6823f89a643SAchiad Shochat 		break;
6831b5daf11SMajd Dibbiny 
6841b5daf11SMajd Dibbiny 	default:
6851b5daf11SMajd Dibbiny 		return -EINVAL;
6861b5daf11SMajd Dibbiny 	}
6873f89a643SAchiad Shochat 
6883f89a643SAchiad Shochat 	if (!err)
6893f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
6903f89a643SAchiad Shochat 
6913f89a643SAchiad Shochat 	return err;
6923f89a643SAchiad Shochat 
6931b5daf11SMajd Dibbiny }
6941b5daf11SMajd Dibbiny 
6951b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
6961b5daf11SMajd Dibbiny 				u16 *max_pkeys)
6971b5daf11SMajd Dibbiny {
6981b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6991b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7001b5daf11SMajd Dibbiny 
7011b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7021b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7031b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7041b5daf11SMajd Dibbiny 
7051b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7061b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7071b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7081b5daf11SMajd Dibbiny 						pkey_table_size));
7091b5daf11SMajd Dibbiny 		return 0;
7101b5daf11SMajd Dibbiny 
7111b5daf11SMajd Dibbiny 	default:
7121b5daf11SMajd Dibbiny 		return -EINVAL;
7131b5daf11SMajd Dibbiny 	}
7141b5daf11SMajd Dibbiny }
7151b5daf11SMajd Dibbiny 
7161b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7171b5daf11SMajd Dibbiny 				u32 *vendor_id)
7181b5daf11SMajd Dibbiny {
7191b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7201b5daf11SMajd Dibbiny 
7211b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7221b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7231b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7241b5daf11SMajd Dibbiny 
7251b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7261b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7271b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7281b5daf11SMajd Dibbiny 
7291b5daf11SMajd Dibbiny 	default:
7301b5daf11SMajd Dibbiny 		return -EINVAL;
7311b5daf11SMajd Dibbiny 	}
7321b5daf11SMajd Dibbiny }
7331b5daf11SMajd Dibbiny 
7341b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7351b5daf11SMajd Dibbiny 				__be64 *node_guid)
7361b5daf11SMajd Dibbiny {
7371b5daf11SMajd Dibbiny 	u64 tmp;
7381b5daf11SMajd Dibbiny 	int err;
7391b5daf11SMajd Dibbiny 
7401b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7411b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7421b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7431b5daf11SMajd Dibbiny 
7441b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7451b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7463f89a643SAchiad Shochat 		break;
7473f89a643SAchiad Shochat 
7483f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7493f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7503f89a643SAchiad Shochat 		break;
7511b5daf11SMajd Dibbiny 
7521b5daf11SMajd Dibbiny 	default:
7531b5daf11SMajd Dibbiny 		return -EINVAL;
7541b5daf11SMajd Dibbiny 	}
7553f89a643SAchiad Shochat 
7563f89a643SAchiad Shochat 	if (!err)
7573f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7583f89a643SAchiad Shochat 
7593f89a643SAchiad Shochat 	return err;
7601b5daf11SMajd Dibbiny }
7611b5daf11SMajd Dibbiny 
7621b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
763bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7641b5daf11SMajd Dibbiny };
7651b5daf11SMajd Dibbiny 
7661b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7671b5daf11SMajd Dibbiny {
7681b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7691b5daf11SMajd Dibbiny 
7701b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7711b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7721b5daf11SMajd Dibbiny 
7731b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7741b5daf11SMajd Dibbiny 
7751b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7761b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7771b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7781b5daf11SMajd Dibbiny }
7791b5daf11SMajd Dibbiny 
780e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7812528e33eSMatan Barak 				struct ib_device_attr *props,
7822528e33eSMatan Barak 				struct ib_udata *uhw)
783e126ba97SEli Cohen {
78448357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
785e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
786938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
787e126ba97SEli Cohen 	int err = -ENOMEM;
788288c01b7SEli Cohen 	int max_sq_desc;
789e126ba97SEli Cohen 	int max_rq_sg;
790e126ba97SEli Cohen 	int max_sq_sg;
791e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
79285c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
793402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
794402ca536SBodong Wang 	size_t resp_len;
795402ca536SBodong Wang 	u64 max_tso;
796e126ba97SEli Cohen 
797402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
79848357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
799402ca536SBodong Wang 		return -EINVAL;
8006f26b2acSErez Alfasi 
801402ca536SBodong Wang 	resp.response_length = resp_len;
802402ca536SBodong Wang 
80348357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8042528e33eSMatan Barak 		return -EINVAL;
8052528e33eSMatan Barak 
806e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8071b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8081b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8091b5daf11SMajd Dibbiny 	if (err)
8101b5daf11SMajd Dibbiny 		return err;
8111b5daf11SMajd Dibbiny 
8121b5daf11SMajd Dibbiny 	err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
8131b5daf11SMajd Dibbiny 	if (err)
8141b5daf11SMajd Dibbiny 		return err;
8151b5daf11SMajd Dibbiny 
8161b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8171b5daf11SMajd Dibbiny 	if (err)
8181b5daf11SMajd Dibbiny 		return err;
819e126ba97SEli Cohen 
8209603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8219603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8229603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
823e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
824e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
825e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8261a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
827938fe83cSSaeed Mahameed 
828938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
829e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
830938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
831e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
832938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
833e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
834938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
835e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
836d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
837d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
838d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
839d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
840b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
841b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
842d2370e0aSMatan Barak 	}
843e126ba97SEli Cohen 	props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
844938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
845c0a6cbb9SIsrael Rukshin 		props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
8462dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8472dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8482dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8492dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8502dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8512dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8522dea9094SSagi Grimberg 	}
853938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
854f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
855e126ba97SEli Cohen 
85685c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
857e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
858e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
85988115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
860e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
861e8161334SNoa Osherovich 		}
862e8161334SNoa Osherovich 
863e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
864e8161334SNoa Osherovich 			props->raw_packet_caps |=
865e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
86688115fe7SBodong Wang 
867a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
868402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
869402ca536SBodong Wang 			if (max_tso) {
870402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
871402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
872402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
873402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
874402ca536SBodong Wang 			}
875402ca536SBodong Wang 		}
87631f69a82SYishai Hadas 
877a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
87831f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
87931f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
88031f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
88131f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
88231f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
88331f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
88431f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
88531f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
88631f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
88731f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
8884e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
8894e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
8902d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
8912d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
8922d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
8932d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
89431f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
89531f69a82SYishai Hadas 		}
89631f69a82SYishai Hadas 	} else {
897a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
89831f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
899a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
90031f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
901402ca536SBodong Wang 	}
902402ca536SBodong Wang 
903f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
904f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
905f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
906f0313965SErez Shitrit 	}
907f0313965SErez Shitrit 
90803404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
90985c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
91085c7c014SDaniel Jurgens 	    raw_support)
91103404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
91203404e8aSMaor Gottlieb 
9131d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9141d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9151d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9161d54f890SYishai Hadas 
917cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
91885c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
91985c7c014SDaniel Jurgens 	    raw_support) {
920e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
921cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
922e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
923e8161334SNoa Osherovich 	}
924cff5a0f3SMajd Dibbiny 
92524da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
92624da0016SAriel Levkovich 		props->max_dm_size =
92724da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
92824da0016SAriel Levkovich 	}
92924da0016SAriel Levkovich 
930da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
931da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
932da6d6ba3SMaor Gottlieb 
933b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
934b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
935b1383aa6SNoa Osherovich 
9361b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9371b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
938e126ba97SEli Cohen 
939e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
940e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
941938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
942938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
943938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
944938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
945288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
946288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
947288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
948e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
94933023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
95033023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
951986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
952938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9539f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
954938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
955938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
956938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
957938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
958938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
959938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
960938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
961e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
962e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
963911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
964911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
96562e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
96662e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
96736609056SYamin Friedman 	props->max_sgl_rd =
96836609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
969776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
97081bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
971938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
972938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
973e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
974e126ba97SEli Cohen 					   props->max_mcast_grp;
97586695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9767c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9777c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
978e126ba97SEli Cohen 
979e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
98000815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
9818cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
9828cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
983a73a8955SMoni Shoua 		if (!uhw) {
984a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
985a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
986a73a8955SMoni Shoua 			 */
987a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
988a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
989a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
990a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
991a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
992a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
993a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
994a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
995a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
996a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
997a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
998a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
999a73a8955SMoni Shoua 		}
1000e502b8b0SLeon Romanovsky 	}
10018cdd312cSHaggai Eran 
1002051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1003051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1004051f2630SLeon Romanovsky 
1005e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1006eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1007eff901d3SEli Cohen 
100831f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
100985c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
101031f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
101131f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
101231f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
101331f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
101431f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
101531f69a82SYishai Hadas 		props->max_wq_type_rq =
101631f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
101731f69a82SYishai Hadas 	}
101831f69a82SYishai Hadas 
1019eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
102078b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1021eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
102278b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1023eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
102478b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1025eb761894SArtemy Kovalyov 	}
1026eb761894SArtemy Kovalyov 
102789705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
102889705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
102989705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
103089705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
103189705e92SDanit Goldberg 	}
103289705e92SDanit Goldberg 
103387ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
103487ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
103587ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
103687ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
103787ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
103887ab3f52SYonatan Cohen 	}
103987ab3f52SYonatan Cohen 
1040a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1041572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1042572f46bfSYonatan Cohen 
1043572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10447e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1045572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1046572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1047572f46bfSYonatan Cohen 
10487e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10497e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10507e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10516f1006a4SYonatan Cohen 
10526f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10536f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10546f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1055572f46bfSYonatan Cohen 		}
10567e43a2a5SBodong Wang 	}
10577e43a2a5SBodong Wang 
1058a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
105985c7c014SDaniel Jurgens 	    raw_support) {
1060d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1061d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1062d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1063d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1064d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1065d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1066d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1067d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
106861147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
106961147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
107061147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
107161147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1072d949167dSBodong Wang 		}
1073d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1074d949167dSBodong Wang 	}
1075d949167dSBodong Wang 
1076a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1077a762d460SLeon Romanovsky 	    uhw_outlen) {
1078795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10799f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1080795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1081050da902SBodong Wang 
1082050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1083050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1084050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1085050da902SBodong Wang 
10869f885201SLeon Romanovsky 		resp.response_length +=
10879f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10889f885201SLeon Romanovsky 	}
10899f885201SLeon Romanovsky 
1090a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1091de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
10927a0c8f42SGuy Levi 
1093de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1094de57f2adSGuy Levi 			resp.flags |=
1095de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
10967a0c8f42SGuy Levi 
10977a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
10987a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
10997e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11007e11b911SDanit Goldberg 			resp.flags |=
11017e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11027249c8eaSGuy Levi 
11037249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1104de57f2adSGuy Levi 	}
11059f885201SLeon Romanovsky 
1106a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
110796dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
110896dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
110996dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
111096dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
111196dc3fc5SNoa Osherovich 
111296dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
111396dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
111496dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
111596dc3fc5SNoa Osherovich 
111696dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
111796dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
111896dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
111996dc3fc5SNoa Osherovich 
112096dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
112196dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
112296dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
112396dc3fc5SNoa Osherovich 		}
112496dc3fc5SNoa Osherovich 	}
112596dc3fc5SNoa Osherovich 
1126a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
112785c7c014SDaniel Jurgens 	    raw_support) {
1128b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1129b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1130b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1131b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1132b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1133b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1134c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1135c16339b6SMark Zhang 				resp.striding_rq_caps
1136c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1137c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1138c16339b6SMark Zhang 			else
1139c16339b6SMark Zhang 				resp.striding_rq_caps
1140c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1141b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1142b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1143b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1144b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1145b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1146b4f34597SNoa Osherovich 		}
1147b4f34597SNoa Osherovich 	}
1148b4f34597SNoa Osherovich 
1149a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1150f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1151f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1152f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1153f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1154f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1155f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1156f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1157f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1158f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1159f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
116041e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1161e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1162e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
116341e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1164e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1165e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1166f95ef6cbSMaor Gottlieb 	}
1167f95ef6cbSMaor Gottlieb 
116848357091SLeon Romanovsky 	if (uhw_outlen) {
1169402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1170402ca536SBodong Wang 
1171402ca536SBodong Wang 		if (err)
1172402ca536SBodong Wang 			return err;
1173402ca536SBodong Wang 	}
1174402ca536SBodong Wang 
11751b5daf11SMajd Dibbiny 	return 0;
11761b5daf11SMajd Dibbiny }
1177e126ba97SEli Cohen 
11781b5daf11SMajd Dibbiny enum mlx5_ib_width {
11791b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_1X	= 1 << 0,
11801b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_2X	= 1 << 1,
11811b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_4X	= 1 << 2,
11821b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_8X	= 1 << 3,
11831b5daf11SMajd Dibbiny 	MLX5_IB_WIDTH_12X	= 1 << 4
11841b5daf11SMajd Dibbiny };
11851b5daf11SMajd Dibbiny 
1186db7a691aSMichael Guralnik static void translate_active_width(struct ib_device *ibdev, u8 active_width,
11871b5daf11SMajd Dibbiny 				  u8 *ib_width)
11881b5daf11SMajd Dibbiny {
11891b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11901b5daf11SMajd Dibbiny 
1191db7a691aSMichael Guralnik 	if (active_width & MLX5_IB_WIDTH_1X)
11921b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1193d764970bSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_2X)
1194d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1195db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_4X)
11961b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1197db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_8X)
11981b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1199db7a691aSMichael Guralnik 	else if (active_width & MLX5_IB_WIDTH_12X)
12001b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1201db7a691aSMichael Guralnik 	else {
1202db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
12031b5daf11SMajd Dibbiny 			    (int)active_width);
1204db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12051b5daf11SMajd Dibbiny 	}
12061b5daf11SMajd Dibbiny 
1207db7a691aSMichael Guralnik 	return;
12081b5daf11SMajd Dibbiny }
12091b5daf11SMajd Dibbiny 
12101b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12111b5daf11SMajd Dibbiny {
12121b5daf11SMajd Dibbiny 	switch (mtu) {
12131b5daf11SMajd Dibbiny 	case 256: return 1;
12141b5daf11SMajd Dibbiny 	case 512: return 2;
12151b5daf11SMajd Dibbiny 	case 1024: return 3;
12161b5daf11SMajd Dibbiny 	case 2048: return 4;
12171b5daf11SMajd Dibbiny 	case 4096: return 5;
12181b5daf11SMajd Dibbiny 	default:
12191b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12201b5daf11SMajd Dibbiny 		return -1;
12211b5daf11SMajd Dibbiny 	}
12221b5daf11SMajd Dibbiny }
12231b5daf11SMajd Dibbiny 
12241b5daf11SMajd Dibbiny enum ib_max_vl_num {
12251b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12261b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12271b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12281b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12291b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12301b5daf11SMajd Dibbiny };
12311b5daf11SMajd Dibbiny 
12321b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12331b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12341b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12351b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12361b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12371b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12381b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12391b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12401b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12411b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12421b5daf11SMajd Dibbiny };
12431b5daf11SMajd Dibbiny 
12441b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12451b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12461b5daf11SMajd Dibbiny {
12471b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12481b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12491b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12501b5daf11SMajd Dibbiny 		break;
12511b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12521b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12531b5daf11SMajd Dibbiny 		break;
12541b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12551b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12561b5daf11SMajd Dibbiny 		break;
12571b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12581b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12591b5daf11SMajd Dibbiny 		break;
12601b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12611b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12621b5daf11SMajd Dibbiny 		break;
12631b5daf11SMajd Dibbiny 
12641b5daf11SMajd Dibbiny 	default:
12651b5daf11SMajd Dibbiny 		return -EINVAL;
12661b5daf11SMajd Dibbiny 	}
12671b5daf11SMajd Dibbiny 
12681b5daf11SMajd Dibbiny 	return 0;
12691b5daf11SMajd Dibbiny }
12701b5daf11SMajd Dibbiny 
12711b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
12721b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12731b5daf11SMajd Dibbiny {
12741b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12751b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12761b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1277046339eaSSaeed Mahameed 	u16 max_mtu;
1278046339eaSSaeed Mahameed 	u16 oper_mtu;
12791b5daf11SMajd Dibbiny 	int err;
12801b5daf11SMajd Dibbiny 	u8 ib_link_width_oper;
12811b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
12821b5daf11SMajd Dibbiny 
12831b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
12841b5daf11SMajd Dibbiny 	if (!rep) {
12851b5daf11SMajd Dibbiny 		err = -ENOMEM;
12861b5daf11SMajd Dibbiny 		goto out;
12871b5daf11SMajd Dibbiny 	}
12881b5daf11SMajd Dibbiny 
1289c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
12901b5daf11SMajd Dibbiny 
12911b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
12921b5daf11SMajd Dibbiny 	if (err)
12931b5daf11SMajd Dibbiny 		goto out;
12941b5daf11SMajd Dibbiny 
12951b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
12961b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
12971b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
12981b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
12991b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13001b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13011b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13021b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13031b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13041b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13051b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13061b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13071b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13081b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13091b5daf11SMajd Dibbiny 
13104106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13114106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13124106a758SMichael Guralnik 
13131b5daf11SMajd Dibbiny 	err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
13141b5daf11SMajd Dibbiny 	if (err)
13151b5daf11SMajd Dibbiny 		goto out;
13161b5daf11SMajd Dibbiny 
1317db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1318db7a691aSMichael Guralnik 
1319d5beb7f2SNoa Osherovich 	err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
13201b5daf11SMajd Dibbiny 	if (err)
13211b5daf11SMajd Dibbiny 		goto out;
13221b5daf11SMajd Dibbiny 
1323facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13241b5daf11SMajd Dibbiny 
13251b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13261b5daf11SMajd Dibbiny 
1327facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13281b5daf11SMajd Dibbiny 
13291b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13301b5daf11SMajd Dibbiny 
13311b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13321b5daf11SMajd Dibbiny 	if (err)
13331b5daf11SMajd Dibbiny 		goto out;
13341b5daf11SMajd Dibbiny 
13351b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13361b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13371b5daf11SMajd Dibbiny out:
13381b5daf11SMajd Dibbiny 	kfree(rep);
1339e126ba97SEli Cohen 	return err;
1340e126ba97SEli Cohen }
1341e126ba97SEli Cohen 
1342e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1343e126ba97SEli Cohen 		       struct ib_port_attr *props)
1344e126ba97SEli Cohen {
1345095b0927SIlan Tayari 	unsigned int count;
1346095b0927SIlan Tayari 	int ret;
1347095b0927SIlan Tayari 
13481b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13491b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1350095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1351095b0927SIlan Tayari 		break;
1352e126ba97SEli Cohen 
13531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1354095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1355095b0927SIlan Tayari 		break;
13561b5daf11SMajd Dibbiny 
13573f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1358095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1359095b0927SIlan Tayari 		break;
13603f89a643SAchiad Shochat 
13611b5daf11SMajd Dibbiny 	default:
1362095b0927SIlan Tayari 		ret = -EINVAL;
1363e126ba97SEli Cohen 	}
1364095b0927SIlan Tayari 
1365095b0927SIlan Tayari 	if (!ret && props) {
1366b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1367b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1368b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1369b3cbd6f0SDaniel Jurgens 
1370b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1371b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1372b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1373b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1374b3cbd6f0SDaniel Jurgens 			 */
1375b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1376b3cbd6f0SDaniel Jurgens 			port = 1;
1377b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1378b3cbd6f0SDaniel Jurgens 		}
1379b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1380b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1381b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1382095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1383095b0927SIlan Tayari 	}
1384095b0927SIlan Tayari 	return ret;
1385e126ba97SEli Cohen }
1386e126ba97SEli Cohen 
13878e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
13888e6efa3aSMark Bloch 				  struct ib_port_attr *props)
13898e6efa3aSMark Bloch {
13908e6efa3aSMark Bloch 	int ret;
13918e6efa3aSMark Bloch 
139226628e2dSMark Bloch 	/* Only link layer == ethernet is valid for representors
139326628e2dSMark Bloch 	 * and we always use port 1
139426628e2dSMark Bloch 	 */
13958e6efa3aSMark Bloch 	ret = mlx5_query_port_roce(ibdev, port, props);
13968e6efa3aSMark Bloch 	if (ret || !props)
13978e6efa3aSMark Bloch 		return ret;
13988e6efa3aSMark Bloch 
13998e6efa3aSMark Bloch 	/* We don't support GIDS */
14008e6efa3aSMark Bloch 	props->gid_tbl_len = 0;
14018e6efa3aSMark Bloch 
14028e6efa3aSMark Bloch 	return ret;
14038e6efa3aSMark Bloch }
14048e6efa3aSMark Bloch 
1405e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1406e126ba97SEli Cohen 			     union ib_gid *gid)
1407e126ba97SEli Cohen {
14081b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14091b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1410e126ba97SEli Cohen 
14111b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14121b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14131b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1414e126ba97SEli Cohen 
14151b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14161b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1417e126ba97SEli Cohen 
14181b5daf11SMajd Dibbiny 	default:
14191b5daf11SMajd Dibbiny 		return -EINVAL;
14201b5daf11SMajd Dibbiny 	}
1421e126ba97SEli Cohen 
1422e126ba97SEli Cohen }
1423e126ba97SEli Cohen 
1424b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1425b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1426b3cbd6f0SDaniel Jurgens {
1427b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1428b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1429b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1430b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1431b3cbd6f0SDaniel Jurgens 	int err;
1432b3cbd6f0SDaniel Jurgens 
1433b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1434b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1435b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1436b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1437b3cbd6f0SDaniel Jurgens 		 */
1438b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1439b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1440b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1441b3cbd6f0SDaniel Jurgens 	}
1442b3cbd6f0SDaniel Jurgens 
1443b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1444b3cbd6f0SDaniel Jurgens 					index, pkey);
1445b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1446b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1447b3cbd6f0SDaniel Jurgens 
1448b3cbd6f0SDaniel Jurgens 	return err;
1449b3cbd6f0SDaniel Jurgens }
1450b3cbd6f0SDaniel Jurgens 
1451e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1452e126ba97SEli Cohen 			      u16 *pkey)
1453e126ba97SEli Cohen {
14541b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14551b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14561b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1457e126ba97SEli Cohen 
14581b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14591b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1460b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14611b5daf11SMajd Dibbiny 	default:
14621b5daf11SMajd Dibbiny 		return -EINVAL;
1463e126ba97SEli Cohen 	}
14641b5daf11SMajd Dibbiny }
1465e126ba97SEli Cohen 
1466e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1467e126ba97SEli Cohen 				 struct ib_device_modify *props)
1468e126ba97SEli Cohen {
1469e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1470e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1471e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1472e126ba97SEli Cohen 	int err;
1473e126ba97SEli Cohen 
1474e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1475e126ba97SEli Cohen 		return -EOPNOTSUPP;
1476e126ba97SEli Cohen 
1477e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1478e126ba97SEli Cohen 		return 0;
1479e126ba97SEli Cohen 
1480e126ba97SEli Cohen 	/*
1481e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1482e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1483e126ba97SEli Cohen 	 */
1484bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
14859603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1486e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1487e126ba97SEli Cohen 	if (err)
1488e126ba97SEli Cohen 		return err;
1489e126ba97SEli Cohen 
1490bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1491e126ba97SEli Cohen 
1492e126ba97SEli Cohen 	return err;
1493e126ba97SEli Cohen }
1494e126ba97SEli Cohen 
1495cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1496cdbe33d0SEli Cohen 				u32 value)
1497cdbe33d0SEli Cohen {
1498cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1499b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1500b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1501cdbe33d0SEli Cohen 	int err;
1502cdbe33d0SEli Cohen 
1503b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1504b3cbd6f0SDaniel Jurgens 	if (!mdev)
1505b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1506b3cbd6f0SDaniel Jurgens 
1507b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1508cdbe33d0SEli Cohen 	if (err)
1509b3cbd6f0SDaniel Jurgens 		goto out;
1510cdbe33d0SEli Cohen 
1511cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1512cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1513cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1514b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1515b3cbd6f0SDaniel Jurgens 		goto out;
1516cdbe33d0SEli Cohen 	}
1517cdbe33d0SEli Cohen 
1518cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1519cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1520b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1521b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1522b3cbd6f0SDaniel Jurgens 
1523b3cbd6f0SDaniel Jurgens out:
1524b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1525cdbe33d0SEli Cohen 
1526cdbe33d0SEli Cohen 	return err;
1527cdbe33d0SEli Cohen }
1528cdbe33d0SEli Cohen 
1529e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1530e126ba97SEli Cohen 			       struct ib_port_modify *props)
1531e126ba97SEli Cohen {
1532e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1533e126ba97SEli Cohen 	struct ib_port_attr attr;
1534e126ba97SEli Cohen 	u32 tmp;
1535e126ba97SEli Cohen 	int err;
1536cdbe33d0SEli Cohen 	u32 change_mask;
1537cdbe33d0SEli Cohen 	u32 value;
1538cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1539cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1540cdbe33d0SEli Cohen 
1541ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1542ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1543ec255879SMajd Dibbiny 	 */
1544ec255879SMajd Dibbiny 	if (!is_ib)
1545ec255879SMajd Dibbiny 		return 0;
1546ec255879SMajd Dibbiny 
1547cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1548cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1549cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1550cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1551cdbe33d0SEli Cohen 	}
1552e126ba97SEli Cohen 
1553e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1554e126ba97SEli Cohen 
1555c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1556e126ba97SEli Cohen 	if (err)
1557e126ba97SEli Cohen 		goto out;
1558e126ba97SEli Cohen 
1559e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1560e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1561e126ba97SEli Cohen 
15629603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1563e126ba97SEli Cohen 
1564e126ba97SEli Cohen out:
1565e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1566e126ba97SEli Cohen 	return err;
1567e126ba97SEli Cohen }
1568e126ba97SEli Cohen 
156930aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
157030aa60b3SEli Cohen {
157130aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
157230aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
157330aa60b3SEli Cohen }
157430aa60b3SEli Cohen 
157531a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
157631a78a5aSYishai Hadas {
157731a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
157831a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
157931a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
158031a78a5aSYishai Hadas 
158131a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
158231a78a5aSYishai Hadas }
158331a78a5aSYishai Hadas 
1584b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1585b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
158631a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1587b037c29aSEli Cohen {
1588b037c29aSEli Cohen 	int uars_per_sys_page;
1589b037c29aSEli Cohen 	int bfregs_per_sys_page;
1590b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1591b037c29aSEli Cohen 
1592b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1593b037c29aSEli Cohen 		return -EINVAL;
1594b037c29aSEli Cohen 
1595b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1596b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1597b037c29aSEli Cohen 
1598b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1599b037c29aSEli Cohen 		return -ENOMEM;
1600b037c29aSEli Cohen 
1601b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1602b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
160331a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1604b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1605b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1606b037c29aSEli Cohen 		return -EINVAL;
1607b037c29aSEli Cohen 
160831a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
160931a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
161031a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
161131a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
161231a78a5aSYishai Hadas 
161331a78a5aSYishai Hadas 	mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n",
1614b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1615b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
161631a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
161731a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1618b037c29aSEli Cohen 
1619b037c29aSEli Cohen 	return 0;
1620b037c29aSEli Cohen }
1621b037c29aSEli Cohen 
1622b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1623b037c29aSEli Cohen {
1624b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1625b037c29aSEli Cohen 	int err;
1626b037c29aSEli Cohen 	int i;
1627b037c29aSEli Cohen 
1628b037c29aSEli Cohen 	bfregi = &context->bfregi;
162931a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1630b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1631b037c29aSEli Cohen 		if (err)
1632b037c29aSEli Cohen 			goto error;
1633b037c29aSEli Cohen 
1634b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1635b037c29aSEli Cohen 	}
16364ed131d0SYishai Hadas 
16374ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16384ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16394ed131d0SYishai Hadas 
1640b037c29aSEli Cohen 	return 0;
1641b037c29aSEli Cohen 
1642b037c29aSEli Cohen error:
1643b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1644b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1645b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1646b037c29aSEli Cohen 
1647b037c29aSEli Cohen 	return err;
1648b037c29aSEli Cohen }
1649b037c29aSEli Cohen 
165015177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
165115177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1652b037c29aSEli Cohen {
1653b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1654b037c29aSEli Cohen 	int i;
1655b037c29aSEli Cohen 
1656b037c29aSEli Cohen 	bfregi = &context->bfregi;
165715177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16584ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
165915177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
166015177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1661b037c29aSEli Cohen }
1662b037c29aSEli Cohen 
16630042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1664a560f1d9SMark Bloch {
1665a560f1d9SMark Bloch 	int err = 0;
1666a560f1d9SMark Bloch 
1667a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16680042f9e4SMark Bloch 	if (td)
1669a560f1d9SMark Bloch 		dev->lb.user_td++;
16700042f9e4SMark Bloch 	if (qp)
16710042f9e4SMark Bloch 		dev->lb.qps++;
1672a560f1d9SMark Bloch 
16730042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16740042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16750042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1676a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16770042f9e4SMark Bloch 			dev->lb.enabled = true;
16780042f9e4SMark Bloch 		}
16790042f9e4SMark Bloch 	}
1680a560f1d9SMark Bloch 
1681a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1682a560f1d9SMark Bloch 
1683a560f1d9SMark Bloch 	return err;
1684a560f1d9SMark Bloch }
1685a560f1d9SMark Bloch 
16860042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1687a560f1d9SMark Bloch {
1688a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16890042f9e4SMark Bloch 	if (td)
1690a560f1d9SMark Bloch 		dev->lb.user_td--;
16910042f9e4SMark Bloch 	if (qp)
16920042f9e4SMark Bloch 		dev->lb.qps--;
1693a560f1d9SMark Bloch 
16940042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
16950042f9e4SMark Bloch 	    dev->lb.qps == 0) {
16960042f9e4SMark Bloch 		if (dev->lb.enabled) {
1697a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
16980042f9e4SMark Bloch 			dev->lb.enabled = false;
16990042f9e4SMark Bloch 		}
17000042f9e4SMark Bloch 	}
1701a560f1d9SMark Bloch 
1702a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1703a560f1d9SMark Bloch }
1704a560f1d9SMark Bloch 
1705d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1706d2d19121SYishai Hadas 					  u16 uid)
1707c85023e1SHuy Nguyen {
1708c85023e1SHuy Nguyen 	int err;
1709c85023e1SHuy Nguyen 
1710cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1711cfdeb893SLeon Romanovsky 		return 0;
1712cfdeb893SLeon Romanovsky 
1713d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1714c85023e1SHuy Nguyen 	if (err)
1715c85023e1SHuy Nguyen 		return err;
1716c85023e1SHuy Nguyen 
1717c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17188978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17198978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1720c85023e1SHuy Nguyen 		return err;
1721c85023e1SHuy Nguyen 
17220042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1723c85023e1SHuy Nguyen }
1724c85023e1SHuy Nguyen 
1725d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1726d2d19121SYishai Hadas 					     u16 uid)
1727c85023e1SHuy Nguyen {
1728cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1729cfdeb893SLeon Romanovsky 		return;
1730cfdeb893SLeon Romanovsky 
1731d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1732c85023e1SHuy Nguyen 
1733c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17348978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17358978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1736c85023e1SHuy Nguyen 		return;
1737c85023e1SHuy Nguyen 
17380042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1739c85023e1SHuy Nguyen }
1740c85023e1SHuy Nguyen 
174145ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
174245ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
174345ec21c9SYishai Hadas {
174445ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
174545ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
174645ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
174745ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
174845ec21c9SYishai Hadas 	int err;
174945ec21c9SYishai Hadas 
175045ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
175145ec21c9SYishai Hadas 		err = mlx5_cmd_dump_fill_mkey(dev->mdev,
175245ec21c9SYishai Hadas 					      &resp->dump_fill_mkey);
175345ec21c9SYishai Hadas 		if (err)
175445ec21c9SYishai Hadas 			return err;
175545ec21c9SYishai Hadas 		resp->comp_mask |=
175645ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
175745ec21c9SYishai Hadas 	}
175845ec21c9SYishai Hadas 
175945ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
176045ec21c9SYishai Hadas 	if (dev->wc_support)
176145ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
176245ec21c9SYishai Hadas 						      log_bf_reg_size);
176345ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
176445ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
176545ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
176645ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
176745ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
176845ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
176945ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
177045ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
177145ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
177245ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
177345ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
177445ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
177545ec21c9SYishai Hadas 
177645ec21c9SYishai Hadas 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
177745ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_DEVICE) {
177845ec21c9SYishai Hadas 		if (mlx5_get_flow_namespace(dev->mdev,
177945ec21c9SYishai Hadas 				MLX5_FLOW_NAMESPACE_EGRESS))
178045ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
178145ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
178245ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
178345ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
178445ec21c9SYishai Hadas 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
178545ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
178645ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
178745ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
178845ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
178945ec21c9SYishai Hadas 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
179045ec21c9SYishai Hadas 	}
179145ec21c9SYishai Hadas 
179245ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
179345ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
179445ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
179545ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
179645ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
179745ec21c9SYishai Hadas 
179845ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
179945ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
180045ec21c9SYishai Hadas 		resp->eth_min_inline++;
180145ec21c9SYishai Hadas 	}
180245ec21c9SYishai Hadas 
180345ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
180445ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
180545ec21c9SYishai Hadas 
180645ec21c9SYishai Hadas 	/*
180745ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
180845ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
180945ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
181045ec21c9SYishai Hadas 	 * forced by mmap function.
181145ec21c9SYishai Hadas 	 */
181245ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
181345ec21c9SYishai Hadas 		resp->comp_mask |=
181445ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
181545ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
181645ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
181745ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
181845ec21c9SYishai Hadas 	}
181945ec21c9SYishai Hadas 
182045ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
182145ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
182245ec21c9SYishai Hadas 
182345ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
182445ec21c9SYishai Hadas 	return 0;
182545ec21c9SYishai Hadas }
182645ec21c9SYishai Hadas 
1827a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1828e126ba97SEli Cohen 				  struct ib_udata *udata)
1829e126ba97SEli Cohen {
1830a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1831e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1832b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1833b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1834a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
18352f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
183678c0f98cSEli Cohen 	int ver;
1837e126ba97SEli Cohen 	int err;
1838a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1839a168a41cSMajd Dibbiny 				     max_cqe_version);
1840b037c29aSEli Cohen 	bool lib_uar_4k;
18410a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1842e126ba97SEli Cohen 
1843e126ba97SEli Cohen 	if (!dev->ib_active)
1844a2a074efSLeon Romanovsky 		return -EAGAIN;
1845e126ba97SEli Cohen 
1846e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
184778c0f98cSEli Cohen 		ver = 0;
1848e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
184978c0f98cSEli Cohen 		ver = 2;
185078c0f98cSEli Cohen 	else
1851a2a074efSLeon Romanovsky 		return -EINVAL;
185278c0f98cSEli Cohen 
1853e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1854e126ba97SEli Cohen 	if (err)
1855a2a074efSLeon Romanovsky 		return err;
1856e126ba97SEli Cohen 
1857a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1858a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
185978c0f98cSEli Cohen 
1860f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1861a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1862b368d7cbSMatan Barak 
18632f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18642f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18652f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1866a2a074efSLeon Romanovsky 		return -EINVAL;
1867e126ba97SEli Cohen 
186830aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18690a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18702f5ff264SEli Cohen 	bfregi = &context->bfregi;
1871b037c29aSEli Cohen 
18720a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
18730a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
18740a2fd01cSYishai Hadas 		goto uar_done;
18750a2fd01cSYishai Hadas 	}
18760a2fd01cSYishai Hadas 
1877b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
187831a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1879b037c29aSEli Cohen 	if (err)
1880b037c29aSEli Cohen 		goto out_ctx;
1881b037c29aSEli Cohen 
18822f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1883b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
188431a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1885b037c29aSEli Cohen 				GFP_KERNEL);
1886b037c29aSEli Cohen 	if (!bfregi->count) {
1887e126ba97SEli Cohen 		err = -ENOMEM;
1888e126ba97SEli Cohen 		goto out_ctx;
1889e126ba97SEli Cohen 	}
1890e126ba97SEli Cohen 
1891b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1892b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1893e126ba97SEli Cohen 				    GFP_KERNEL);
1894b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1895e126ba97SEli Cohen 		err = -ENOMEM;
1896e126ba97SEli Cohen 		goto out_count;
1897e126ba97SEli Cohen 	}
1898e126ba97SEli Cohen 
1899b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1900b037c29aSEli Cohen 	if (err)
1901b037c29aSEli Cohen 		goto out_sys_pages;
1902b037c29aSEli Cohen 
19030a2fd01cSYishai Hadas uar_done:
1904a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1905fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
190676dc5a84SYishai Hadas 		if (err < 0)
1907d2d19121SYishai Hadas 			goto out_uars;
190876dc5a84SYishai Hadas 		context->devx_uid = err;
1909a8b92ca1SYishai Hadas 	}
1910a8b92ca1SYishai Hadas 
1911d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1912d2d19121SYishai Hadas 					     context->devx_uid);
1913a8b92ca1SYishai Hadas 	if (err)
1914d2d19121SYishai Hadas 		goto out_devx;
1915a8b92ca1SYishai Hadas 
1916e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1917e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1918e126ba97SEli Cohen 
191945ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
192045ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
192145ec21c9SYishai Hadas 				 req.max_cqe_version);
1922b368d7cbSMatan Barak 
192345ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
192445ec21c9SYishai Hadas 	if (err)
192545ec21c9SYishai Hadas 		goto out_mdev;
1926b368d7cbSMatan Barak 
192745ec21c9SYishai Hadas 	resp.response_length = min(udata->outlen, sizeof(resp));
1928b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1929e126ba97SEli Cohen 	if (err)
1930a8b92ca1SYishai Hadas 		goto out_mdev;
1931e126ba97SEli Cohen 
19322f5ff264SEli Cohen 	bfregi->ver = ver;
19332f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
193430aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
193530aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1936f72300c5SHaggai Abramovsky 
1937802dcc7fSMark Zhang 	if (mlx5_ib_lag_should_assign_affinity(dev)) {
193895579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1939c6a21c38SMajd Dibbiny 
1940c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1941c6a21c38SMajd Dibbiny 			   atomic_add_return(
194295579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1943c6a21c38SMajd Dibbiny 	}
1944c6a21c38SMajd Dibbiny 
1945a2a074efSLeon Romanovsky 	return 0;
1946e126ba97SEli Cohen 
1947a8b92ca1SYishai Hadas out_mdev:
1948d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1949d2d19121SYishai Hadas out_devx:
1950a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
195176dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1952146d2f1aSmajd@mellanox.com 
1953e126ba97SEli Cohen out_uars:
1954b037c29aSEli Cohen 	deallocate_uars(dev, context);
1955b037c29aSEli Cohen 
1956b037c29aSEli Cohen out_sys_pages:
1957b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1958b037c29aSEli Cohen 
1959e126ba97SEli Cohen out_count:
19602f5ff264SEli Cohen 	kfree(bfregi->count);
1961e126ba97SEli Cohen 
1962e126ba97SEli Cohen out_ctx:
1963a2a074efSLeon Romanovsky 	return err;
1964e126ba97SEli Cohen }
1965e126ba97SEli Cohen 
19660fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
19670fb556b2SYishai Hadas 				  struct uverbs_attr_bundle *attrs)
19680fb556b2SYishai Hadas {
19690fb556b2SYishai Hadas 	struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
19700fb556b2SYishai Hadas 	int ret;
19710fb556b2SYishai Hadas 
19720fb556b2SYishai Hadas 	ret = set_ucontext_resp(ibcontext, &uctx_resp);
19730fb556b2SYishai Hadas 	if (ret)
19740fb556b2SYishai Hadas 		return ret;
19750fb556b2SYishai Hadas 
19760fb556b2SYishai Hadas 	uctx_resp.response_length =
19770fb556b2SYishai Hadas 		min_t(size_t,
19780fb556b2SYishai Hadas 		      uverbs_attr_get_len(attrs,
19790fb556b2SYishai Hadas 				MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
19800fb556b2SYishai Hadas 		      sizeof(uctx_resp));
19810fb556b2SYishai Hadas 
19820fb556b2SYishai Hadas 	ret = uverbs_copy_to_struct_or_zero(attrs,
19830fb556b2SYishai Hadas 					MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
19840fb556b2SYishai Hadas 					&uctx_resp,
19850fb556b2SYishai Hadas 					sizeof(uctx_resp));
19860fb556b2SYishai Hadas 	return ret;
19870fb556b2SYishai Hadas }
19880fb556b2SYishai Hadas 
1989a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1990e126ba97SEli Cohen {
1991e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1992e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1993b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1994e126ba97SEli Cohen 
1995b037c29aSEli Cohen 	bfregi = &context->bfregi;
1996d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1997d2d19121SYishai Hadas 
1998b037c29aSEli Cohen 	if (context->devx_uid)
199976dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
2000e126ba97SEli Cohen 
2001e126ba97SEli Cohen 	deallocate_uars(dev, context);
20022f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
20032f5ff264SEli Cohen 	kfree(bfregi->count);
2004e126ba97SEli Cohen }
2005e126ba97SEli Cohen 
2006e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2007e126ba97SEli Cohen 				 int uar_idx)
2008e126ba97SEli Cohen {
2009b037c29aSEli Cohen 	int fw_uars_per_page;
2010b037c29aSEli Cohen 
2011b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2012b037c29aSEli Cohen 
2013aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2014e126ba97SEli Cohen }
2015e126ba97SEli Cohen 
2016342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2017342ee59dSYishai Hadas 				 int uar_idx)
2018342ee59dSYishai Hadas {
2019342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2020342ee59dSYishai Hadas 
2021342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2022342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2023342ee59dSYishai Hadas 
2024342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2025342ee59dSYishai Hadas }
2026342ee59dSYishai Hadas 
2027e126ba97SEli Cohen static int get_command(unsigned long offset)
2028e126ba97SEli Cohen {
2029e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2030e126ba97SEli Cohen }
2031e126ba97SEli Cohen 
2032e126ba97SEli Cohen static int get_arg(unsigned long offset)
2033e126ba97SEli Cohen {
2034e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2035e126ba97SEli Cohen }
2036e126ba97SEli Cohen 
2037e126ba97SEli Cohen static int get_index(unsigned long offset)
2038e126ba97SEli Cohen {
2039e126ba97SEli Cohen 	return get_arg(offset);
2040e126ba97SEli Cohen }
2041e126ba97SEli Cohen 
20424ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20434ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20444ed131d0SYishai Hadas {
20454ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20464ed131d0SYishai Hadas }
20474ed131d0SYishai Hadas 
20487c2344c3SMaor Gottlieb 
20497c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20507c2344c3SMaor Gottlieb {
20517c2344c3SMaor Gottlieb }
20527c2344c3SMaor Gottlieb 
205337aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2054e126ba97SEli Cohen {
205537aa5c36SGuy Levi 	switch (cmd) {
205637aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
205737aa5c36SGuy Levi 		return "WC";
2058e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
205937aa5c36SGuy Levi 		return "best effort WC";
206037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
206137aa5c36SGuy Levi 		return "NC";
206224da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
206324da0016SAriel Levkovich 		return "Device Memory";
206437aa5c36SGuy Levi 	default:
206537aa5c36SGuy Levi 		return NULL;
206637aa5c36SGuy Levi 	}
206737aa5c36SGuy Levi }
206837aa5c36SGuy Levi 
20695c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20705c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20715c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20725c99eaecSFeras Daoud {
20734eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20744eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20755c99eaecSFeras Daoud 		return -EINVAL;
20765c99eaecSFeras Daoud 
20775c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20785c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20795c99eaecSFeras Daoud 
20804eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20815c99eaecSFeras Daoud 		return -EPERM;
2082c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20835c99eaecSFeras Daoud 
2084ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20855c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20865c99eaecSFeras Daoud 
20874eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20884eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20895c99eaecSFeras Daoud }
20905c99eaecSFeras Daoud 
2091dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2092dc2316ebSYishai Hadas {
2093dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2094dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
20957be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2096dc2316ebSYishai Hadas 	struct mlx5_ib_dm *mdm;
2097dc2316ebSYishai Hadas 
2098dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2099dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2100dc2316ebSYishai Hadas 		mdm = container_of(mentry, struct mlx5_ib_dm, mentry);
2101dc2316ebSYishai Hadas 		mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr,
2102dc2316ebSYishai Hadas 				       mdm->size);
2103dc2316ebSYishai Hadas 		kfree(mdm);
2104dc2316ebSYishai Hadas 		break;
21057be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21067be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21077be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21087be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21097be76befSYishai Hadas 		kfree(mentry);
21107be76befSYishai Hadas 		break;
2111342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2112342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2113342ee59dSYishai Hadas 		mlx5_cmd_free_uar(dev->mdev, mentry->page_idx);
2114342ee59dSYishai Hadas 		kfree(mentry);
2115342ee59dSYishai Hadas 		break;
2116dc2316ebSYishai Hadas 	default:
2117dc2316ebSYishai Hadas 		WARN_ON(true);
2118dc2316ebSYishai Hadas 	}
2119dc2316ebSYishai Hadas }
2120dc2316ebSYishai Hadas 
212137aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21227c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21237c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
212437aa5c36SGuy Levi {
21252f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
212637aa5c36SGuy Levi 	int err;
212737aa5c36SGuy Levi 	unsigned long idx;
2128aa09ea6eSKamal Heib 	phys_addr_t pfn;
212937aa5c36SGuy Levi 	pgprot_t prot;
21304ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21314ed131d0SYishai Hadas 	u32 uar_index;
21324ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21334ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21344ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2135b037c29aSEli Cohen 
21360a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21370a2fd01cSYishai Hadas 		return -EINVAL;
21380a2fd01cSYishai Hadas 
2139b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2140b037c29aSEli Cohen 		return -EINVAL;
2141b037c29aSEli Cohen 
21424ed131d0SYishai Hadas 	if (dyn_uar)
21434ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21444ed131d0SYishai Hadas 	else
2145b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21464ed131d0SYishai Hadas 
21474ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21484ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21494ed131d0SYishai Hadas 			     idx, max_valid_idx);
2150b037c29aSEli Cohen 		return -EINVAL;
2151b037c29aSEli Cohen 	}
215237aa5c36SGuy Levi 
215337aa5c36SGuy Levi 	switch (cmd) {
215437aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21554ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
215637aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
215737aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
215837aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
215937aa5c36SGuy Levi 		break;
216037aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
216137aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
216237aa5c36SGuy Levi 		break;
216337aa5c36SGuy Levi 	default:
216437aa5c36SGuy Levi 		return -EINVAL;
216537aa5c36SGuy Levi 	}
216637aa5c36SGuy Levi 
21674ed131d0SYishai Hadas 	if (dyn_uar) {
21684ed131d0SYishai Hadas 		int uars_per_page;
21694ed131d0SYishai Hadas 
21704ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21714ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21724ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21734ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21744ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21754ed131d0SYishai Hadas 			return -EINVAL;
21764ed131d0SYishai Hadas 		}
21774ed131d0SYishai Hadas 
21784ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21794ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21804ed131d0SYishai Hadas 		 * page holds its count.
21814ed131d0SYishai Hadas 		 */
21824ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21834ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21844ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21854ed131d0SYishai Hadas 			return -EINVAL;
21864ed131d0SYishai Hadas 		}
21874ed131d0SYishai Hadas 
21884ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21894ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21904ed131d0SYishai Hadas 
21914ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21924ed131d0SYishai Hadas 		if (err) {
21934ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21944ed131d0SYishai Hadas 			goto free_bfreg;
21954ed131d0SYishai Hadas 		}
21964ed131d0SYishai Hadas 	} else {
21974ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21984ed131d0SYishai Hadas 	}
21994ed131d0SYishai Hadas 
22004ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
220137aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2202e126ba97SEli Cohen 
2203e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2204c043ff2cSMichal Kalderon 				prot, NULL);
220537aa5c36SGuy Levi 	if (err) {
22068f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2207e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22088f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22094ed131d0SYishai Hadas 		goto err;
221037aa5c36SGuy Levi 	}
2211e126ba97SEli Cohen 
22124ed131d0SYishai Hadas 	if (dyn_uar)
22134ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22144ed131d0SYishai Hadas 	return 0;
22154ed131d0SYishai Hadas 
22164ed131d0SYishai Hadas err:
22174ed131d0SYishai Hadas 	if (!dyn_uar)
22184ed131d0SYishai Hadas 		return err;
22194ed131d0SYishai Hadas 
22204ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
22214ed131d0SYishai Hadas 
22224ed131d0SYishai Hadas free_bfreg:
22234ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22244ed131d0SYishai Hadas 
22254ed131d0SYishai Hadas 	return err;
222637aa5c36SGuy Levi }
222737aa5c36SGuy Levi 
2228dc2316ebSYishai Hadas static int add_dm_mmap_entry(struct ib_ucontext *context,
2229dc2316ebSYishai Hadas 			     struct mlx5_ib_dm *mdm,
2230dc2316ebSYishai Hadas 			     u64 address)
223124da0016SAriel Levkovich {
2232dc2316ebSYishai Hadas 	mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC;
2233dc2316ebSYishai Hadas 	mdm->mentry.address = address;
2234dc2316ebSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
2235dc2316ebSYishai Hadas 			context, &mdm->mentry.rdma_entry,
2236dc2316ebSYishai Hadas 			mdm->size,
2237dc2316ebSYishai Hadas 			MLX5_IB_MMAP_DEVICE_MEM << 16,
2238dc2316ebSYishai Hadas 			(MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1);
2239dc2316ebSYishai Hadas }
224024da0016SAriel Levkovich 
2241dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2242dc2316ebSYishai Hadas {
2243dc2316ebSYishai Hadas 	unsigned long idx;
2244dc2316ebSYishai Hadas 	u8 command;
2245dc2316ebSYishai Hadas 
2246dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2247dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2248dc2316ebSYishai Hadas 
2249dc2316ebSYishai Hadas 	return (command << 16 | idx);
2250dc2316ebSYishai Hadas }
2251dc2316ebSYishai Hadas 
2252dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2253dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2254dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2255dc2316ebSYishai Hadas {
2256dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2257dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2258dc2316ebSYishai Hadas 	unsigned long pgoff;
2259dc2316ebSYishai Hadas 	pgprot_t prot;
2260dc2316ebSYishai Hadas 	phys_addr_t pfn;
2261dc2316ebSYishai Hadas 	int ret;
2262dc2316ebSYishai Hadas 
2263dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2264dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2265dc2316ebSYishai Hadas 	if (!entry)
226624da0016SAriel Levkovich 		return -EINVAL;
226724da0016SAriel Levkovich 
2268dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2269dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2270342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2271342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22723f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22733f59b6c3SYishai Hadas 	else
2274dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2275dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2276dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2277dc2316ebSYishai Hadas 				prot,
2278dc2316ebSYishai Hadas 				entry);
2279dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2280dc2316ebSYishai Hadas 	return ret;
228124da0016SAriel Levkovich }
228224da0016SAriel Levkovich 
22837be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22847be76befSYishai Hadas {
22859b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22869b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22877be76befSYishai Hadas 
22887be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22897be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
22907be76befSYishai Hadas }
22917be76befSYishai Hadas 
229237aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
229337aa5c36SGuy Levi {
229437aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
229537aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
229637aa5c36SGuy Levi 	unsigned long command;
229737aa5c36SGuy Levi 	phys_addr_t pfn;
229837aa5c36SGuy Levi 
229937aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
230037aa5c36SGuy Levi 	switch (command) {
230137aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
23021f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
23031f3db161SYishai Hadas 		if (!dev->wc_support)
23041f3db161SYishai Hadas 			return -EPERM;
23051f3db161SYishai Hadas 		fallthrough;
230637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
230737aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23087c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2309e126ba97SEli Cohen 
2310e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2311e126ba97SEli Cohen 		return -ENOSYS;
2312e126ba97SEli Cohen 
2313d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2314d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2315d69e3bcfSMatan Barak 			return -EINVAL;
2316d69e3bcfSMatan Barak 
23176cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2318d69e3bcfSMatan Barak 			return -EPERM;
2319c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2320d69e3bcfSMatan Barak 
2321d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2322d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2323d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2324d69e3bcfSMatan Barak 
2325d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2326d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2327d69e3bcfSMatan Barak 			PAGE_SHIFT;
2328d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2329d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2330c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2331c043ff2cSMichal Kalderon 					 NULL);
23325c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23335c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2334d69e3bcfSMatan Barak 
2335e126ba97SEli Cohen 	default:
2336dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2337e126ba97SEli Cohen 	}
2338e126ba97SEli Cohen 
2339e126ba97SEli Cohen 	return 0;
2340e126ba97SEli Cohen }
2341e126ba97SEli Cohen 
234225c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
234325c13324SAriel Levkovich 					u32 type)
234425c13324SAriel Levkovich {
234525c13324SAriel Levkovich 	switch (type) {
234625c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
234725c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
234825c13324SAriel Levkovich 			return -EOPNOTSUPP;
234925c13324SAriel Levkovich 		break;
235025c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2351c9b9dcb4SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
235225c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
235325c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
235425c13324SAriel Levkovich 			return -EPERM;
235525c13324SAriel Levkovich 
235625c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
235725c13324SAriel Levkovich 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner)))
235825c13324SAriel Levkovich 			return -EOPNOTSUPP;
235925c13324SAriel Levkovich 		break;
236025c13324SAriel Levkovich 	}
236125c13324SAriel Levkovich 
236225c13324SAriel Levkovich 	return 0;
236325c13324SAriel Levkovich }
236425c13324SAriel Levkovich 
23653b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
23663b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
236724da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
236824da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
236924da0016SAriel Levkovich {
23703b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
237124da0016SAriel Levkovich 	u64 start_offset;
2372dc2316ebSYishai Hadas 	u16 page_idx;
237324da0016SAriel Levkovich 	int err;
2374dc2316ebSYishai Hadas 	u64 address;
237524da0016SAriel Levkovich 
23763b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
237724da0016SAriel Levkovich 
23783b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
23793b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
238024da0016SAriel Levkovich 	if (err)
23813b113a1eSAriel Levkovich 		return err;
238224da0016SAriel Levkovich 
2383dc2316ebSYishai Hadas 	address = dm->dev_addr & PAGE_MASK;
2384dc2316ebSYishai Hadas 	err = add_dm_mmap_entry(ctx, dm, address);
238524da0016SAriel Levkovich 	if (err)
238624da0016SAriel Levkovich 		goto err_dealloc;
238724da0016SAriel Levkovich 
2388dc2316ebSYishai Hadas 	page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF;
2389dc2316ebSYishai Hadas 	err = uverbs_copy_to(attrs,
2390dc2316ebSYishai Hadas 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2391dc2316ebSYishai Hadas 			     &page_idx,
2392dc2316ebSYishai Hadas 			     sizeof(page_idx));
2393dc2316ebSYishai Hadas 	if (err)
2394dc2316ebSYishai Hadas 		goto err_copy;
2395dc2316ebSYishai Hadas 
23963b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
23973b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
23983b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
23993b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
24003b113a1eSAriel Levkovich 	if (err)
2401dc2316ebSYishai Hadas 		goto err_copy;
24023b113a1eSAriel Levkovich 
24033b113a1eSAriel Levkovich 	return 0;
24043b113a1eSAriel Levkovich 
2405dc2316ebSYishai Hadas err_copy:
2406dc2316ebSYishai Hadas 	rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
24073b113a1eSAriel Levkovich err_dealloc:
24083b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
24093b113a1eSAriel Levkovich 
24103b113a1eSAriel Levkovich 	return err;
24113b113a1eSAriel Levkovich }
24123b113a1eSAriel Levkovich 
241325c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
241425c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
241525c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
241625c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
241725c13324SAriel Levkovich 				  int type)
241825c13324SAriel Levkovich {
2419c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
242025c13324SAriel Levkovich 	u64 act_size;
242125c13324SAriel Levkovich 	int err;
242225c13324SAriel Levkovich 
242325c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
242425c13324SAriel Levkovich 	 * and a power of 2.
242525c13324SAriel Levkovich 	 */
2426c9b9dcb4SAriel Levkovich 	act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
242725c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
242825c13324SAriel Levkovich 
242925c13324SAriel Levkovich 	dm->size = act_size;
2430dff8e2d1SErez Shitrit 	err = mlx5_dm_sw_icm_alloc(dev, type, act_size, attr->alignment,
243125c13324SAriel Levkovich 				   to_mucontext(ctx)->devx_uid, &dm->dev_addr,
243225c13324SAriel Levkovich 				   &dm->icm_dm.obj_id);
243325c13324SAriel Levkovich 	if (err)
243425c13324SAriel Levkovich 		return err;
243525c13324SAriel Levkovich 
243625c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
243725c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
243825c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
243925c13324SAriel Levkovich 	if (err)
2440c9b9dcb4SAriel Levkovich 		mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2441c9b9dcb4SAriel Levkovich 				       to_mucontext(ctx)->devx_uid, dm->dev_addr,
2442c9b9dcb4SAriel Levkovich 				       dm->icm_dm.obj_id);
244325c13324SAriel Levkovich 
244425c13324SAriel Levkovich 	return err;
244525c13324SAriel Levkovich }
244625c13324SAriel Levkovich 
24473b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
24483b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
24493b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
24503b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
24513b113a1eSAriel Levkovich {
24523b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
24533b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
24543b113a1eSAriel Levkovich 	int err;
24553b113a1eSAriel Levkovich 
24563b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
24573b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
24583b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
24593b113a1eSAriel Levkovich 	if (err)
24603b113a1eSAriel Levkovich 		return ERR_PTR(err);
24613b113a1eSAriel Levkovich 
24623b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
24633b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
24643b113a1eSAriel Levkovich 
246525c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
246625c13324SAriel Levkovich 	if (err)
246725c13324SAriel Levkovich 		return ERR_PTR(err);
246825c13324SAriel Levkovich 
24693b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
24703b113a1eSAriel Levkovich 	if (!dm)
24713b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
24723b113a1eSAriel Levkovich 
24733b113a1eSAriel Levkovich 	dm->type = type;
24743b113a1eSAriel Levkovich 
24753b113a1eSAriel Levkovich 	switch (type) {
24763b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24773b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
24783b113a1eSAriel Levkovich 					    attr,
24793b113a1eSAriel Levkovich 					    attrs);
24803b113a1eSAriel Levkovich 		break;
248125c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2482c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2483c9b9dcb4SAriel Levkovich 					     attr, attrs,
2484c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_STEERING);
2485c9b9dcb4SAriel Levkovich 		break;
248625c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2487c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2488c9b9dcb4SAriel Levkovich 					     attr, attrs,
2489c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_HEADER_MODIFY);
249025c13324SAriel Levkovich 		break;
24913b113a1eSAriel Levkovich 	default:
24923b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
24933b113a1eSAriel Levkovich 	}
24943b113a1eSAriel Levkovich 
24953b113a1eSAriel Levkovich 	if (err)
24963b113a1eSAriel Levkovich 		goto err_free;
249724da0016SAriel Levkovich 
249824da0016SAriel Levkovich 	return &dm->ibdm;
249924da0016SAriel Levkovich 
250024da0016SAriel Levkovich err_free:
250124da0016SAriel Levkovich 	kfree(dm);
250224da0016SAriel Levkovich 	return ERR_PTR(err);
250324da0016SAriel Levkovich }
250424da0016SAriel Levkovich 
2505c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
250624da0016SAriel Levkovich {
250725c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
250825c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
2509c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
251024da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
251124da0016SAriel Levkovich 	int ret;
251224da0016SAriel Levkovich 
25133b113a1eSAriel Levkovich 	switch (dm->type) {
25143b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2515dc2316ebSYishai Hadas 		rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
2516dc2316ebSYishai Hadas 		return 0;
251725c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2518c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2519c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
2520c9b9dcb4SAriel Levkovich 					     dm->icm_dm.obj_id);
2521c9b9dcb4SAriel Levkovich 		if (ret)
2522c9b9dcb4SAriel Levkovich 			return ret;
2523c9b9dcb4SAriel Levkovich 		break;
252425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2525c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2526c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
252725c13324SAriel Levkovich 					     dm->icm_dm.obj_id);
252825c13324SAriel Levkovich 		if (ret)
252925c13324SAriel Levkovich 			return ret;
25303b113a1eSAriel Levkovich 		break;
25313b113a1eSAriel Levkovich 	default:
25323b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
25333b113a1eSAriel Levkovich 	}
253424da0016SAriel Levkovich 
253524da0016SAriel Levkovich 	kfree(dm);
253624da0016SAriel Levkovich 
253724da0016SAriel Levkovich 	return 0;
253824da0016SAriel Levkovich }
253924da0016SAriel Levkovich 
2540ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2541e126ba97SEli Cohen {
254221a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
254321a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2544e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2545e126ba97SEli Cohen 	int err;
2546a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2547a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2548a1069c1cSYishai Hadas 	u16 uid = 0;
2549ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2550ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2551e126ba97SEli Cohen 
2552ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2553a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2554a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
255531578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
255621a428a0SLeon Romanovsky 	if (err)
255721a428a0SLeon Romanovsky 		return err;
2558e126ba97SEli Cohen 
2559a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2560a1069c1cSYishai Hadas 	pd->uid = uid;
2561ff23dfa1SShamir Rabinovitch 	if (udata) {
2562e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2563e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2564a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
256521a428a0SLeon Romanovsky 			return -EFAULT;
2566e126ba97SEli Cohen 		}
2567e126ba97SEli Cohen 	}
2568e126ba97SEli Cohen 
256921a428a0SLeon Romanovsky 	return 0;
2570e126ba97SEli Cohen }
2571e126ba97SEli Cohen 
2572*91a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2573e126ba97SEli Cohen {
2574e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2575e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2576e126ba97SEli Cohen 
2577*91a7c58fSLeon Romanovsky 	return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2578e126ba97SEli Cohen }
2579e126ba97SEli Cohen 
2580e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2581e126ba97SEli Cohen {
2582e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
258381e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2584e126ba97SEli Cohen 	int err;
2585539ec982SYishai Hadas 	u16 uid;
2586539ec982SYishai Hadas 
2587539ec982SYishai Hadas 	uid = ibqp->pd ?
2588539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2589e126ba97SEli Cohen 
25902be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
259181e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
259281e30880SYishai Hadas 		return -EOPNOTSUPP;
259381e30880SYishai Hadas 	}
259481e30880SYishai Hadas 
2595539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2596e126ba97SEli Cohen 	if (err)
2597e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2598e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2599e126ba97SEli Cohen 
2600e126ba97SEli Cohen 	return err;
2601e126ba97SEli Cohen }
2602e126ba97SEli Cohen 
2603e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2604e126ba97SEli Cohen {
2605e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2606e126ba97SEli Cohen 	int err;
2607539ec982SYishai Hadas 	u16 uid;
2608e126ba97SEli Cohen 
2609539ec982SYishai Hadas 	uid = ibqp->pd ?
2610539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2611539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2612e126ba97SEli Cohen 	if (err)
2613e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2614e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2615e126ba97SEli Cohen 
2616e126ba97SEli Cohen 	return err;
2617e126ba97SEli Cohen }
2618e126ba97SEli Cohen 
2619e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2620e126ba97SEli Cohen {
26211b5daf11SMajd Dibbiny 	int err;
2622e126ba97SEli Cohen 
26231b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2624e126ba97SEli Cohen 	if (err)
2625e126ba97SEli Cohen 		return err;
26261b5daf11SMajd Dibbiny 
26271b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
26281b5daf11SMajd Dibbiny 
26291b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2630e126ba97SEli Cohen }
2631e126ba97SEli Cohen 
2632508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
2633508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2634e126ba97SEli Cohen {
2635e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
263654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2637e126ba97SEli Cohen 
26389603b61dSJack Morgenstein 	return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
2639e126ba97SEli Cohen }
2640508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
2641e126ba97SEli Cohen 
2642508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
2643e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2644e126ba97SEli Cohen {
2645e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
264654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2647e126ba97SEli Cohen 
26486aec21f6SHaggai Eran 	return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2649e126ba97SEli Cohen }
2650508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
2651e126ba97SEli Cohen 
2652508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
2653508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2654e126ba97SEli Cohen {
2655e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
265654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
265754747231SParav Pandit 
26589603b61dSJack Morgenstein 	return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
2659e126ba97SEli Cohen }
2660508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
2661e126ba97SEli Cohen 
2662508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
2663508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
2664e126ba97SEli Cohen {
2665e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
266654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
266754747231SParav Pandit 
26689603b61dSJack Morgenstein 	return sprintf(buf, "%x\n", dev->mdev->rev_id);
2669e126ba97SEli Cohen }
2670508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
2671e126ba97SEli Cohen 
2672508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
2673508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2674e126ba97SEli Cohen {
2675e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
267654747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
267754747231SParav Pandit 
2678e126ba97SEli Cohen 	return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
26799603b61dSJack Morgenstein 		       dev->mdev->board_id);
2680e126ba97SEli Cohen }
2681508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
2682e126ba97SEli Cohen 
2683508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
2684508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
2685508a523fSParav Pandit 	&dev_attr_hca_type.attr,
2686508a523fSParav Pandit 	&dev_attr_board_id.attr,
2687508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
2688508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
2689508a523fSParav Pandit 	NULL,
2690508a523fSParav Pandit };
2691e126ba97SEli Cohen 
2692508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
2693508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
2694e126ba97SEli Cohen };
2695e126ba97SEli Cohen 
26967722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
26977722f47eSHaggai Eran {
26987722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
26997722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
27007722f47eSHaggai Eran 			     pkey_change_work);
27017722f47eSHaggai Eran 
27027722f47eSHaggai Eran 	mutex_lock(&ports->devr->mutex);
27037722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
27047722f47eSHaggai Eran 	mutex_unlock(&ports->devr->mutex);
27057722f47eSHaggai Eran }
27067722f47eSHaggai Eran 
270789ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
270889ea94a7SMaor Gottlieb {
270989ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
271089ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
271189ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
271289ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
271389ea94a7SMaor Gottlieb 	unsigned long flags_qp;
271489ea94a7SMaor Gottlieb 	unsigned long flags_cq;
271589ea94a7SMaor Gottlieb 	unsigned long flags;
271689ea94a7SMaor Gottlieb 
271789ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
271889ea94a7SMaor Gottlieb 
271989ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
272089ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
272189ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
272289ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
272389ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
272489ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
272589ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
272689ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
272789ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
272889ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
272989ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
273089ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
273189ea94a7SMaor Gottlieb 						      &cq_armed_list);
273289ea94a7SMaor Gottlieb 				}
273389ea94a7SMaor Gottlieb 			}
273489ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
273589ea94a7SMaor Gottlieb 		}
273689ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
273789ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
273889ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
273989ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
274089ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
274189ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
274289ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
274389ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
274489ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
274589ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
274689ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
274789ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
274889ea94a7SMaor Gottlieb 							      &cq_armed_list);
274989ea94a7SMaor Gottlieb 					}
275089ea94a7SMaor Gottlieb 				}
275189ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
275289ea94a7SMaor Gottlieb 						       flags_cq);
275389ea94a7SMaor Gottlieb 			}
275489ea94a7SMaor Gottlieb 		}
275589ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
275689ea94a7SMaor Gottlieb 	}
275789ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
275889ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
275989ea94a7SMaor Gottlieb 	 */
276089ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
27614e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
276289ea94a7SMaor Gottlieb 	}
276389ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
276489ea94a7SMaor Gottlieb }
276589ea94a7SMaor Gottlieb 
276603404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
276703404e8aSMaor Gottlieb {
276803404e8aSMaor Gottlieb 	int err;
276903404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
277003404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
277103404e8aSMaor Gottlieb 			     delay_drop_work);
277203404e8aSMaor Gottlieb 
2773fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2774fe248c3aSMaor Gottlieb 
277503404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
2776333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
277703404e8aSMaor Gottlieb 	if (err) {
277803404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
277903404e8aSMaor Gottlieb 			     delay_drop->timeout);
278003404e8aSMaor Gottlieb 		delay_drop->activate = false;
278103404e8aSMaor Gottlieb 	}
278203404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
278303404e8aSMaor Gottlieb }
278403404e8aSMaor Gottlieb 
278509e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
278609e574faSSaeed Mahameed 				 struct ib_event *ibev)
278709e574faSSaeed Mahameed {
27886cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
27896cfdc7e4SAya Levin 
279009e574faSSaeed Mahameed 	switch (eqe->sub_type) {
279109e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
27926cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
27936cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
279409e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
279509e574faSSaeed Mahameed 		break;
279609e574faSSaeed Mahameed 	default: /* do nothing */
279709e574faSSaeed Mahameed 		return;
279809e574faSSaeed Mahameed 	}
279909e574faSSaeed Mahameed }
280009e574faSSaeed Mahameed 
2801134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2802134e9349SSaeed Mahameed 			      struct ib_event *ibev)
2803134e9349SSaeed Mahameed {
2804134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
2805134e9349SSaeed Mahameed 
2806134e9349SSaeed Mahameed 	ibev->element.port_num = port;
2807134e9349SSaeed Mahameed 
2808134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
2809134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2810134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2811134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
2812134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
2813134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
2814134e9349SSaeed Mahameed 		 */
2815134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2816134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
2817134e9349SSaeed Mahameed 			return -EINVAL;
2818134e9349SSaeed Mahameed 
2819134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
2820134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2821134e9349SSaeed Mahameed 		break;
2822134e9349SSaeed Mahameed 
2823134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
2824134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
2825134e9349SSaeed Mahameed 		break;
2826134e9349SSaeed Mahameed 
2827134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
2828134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
2829134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2830134e9349SSaeed Mahameed 		break;
2831134e9349SSaeed Mahameed 
2832134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
2833134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
2834134e9349SSaeed Mahameed 		break;
2835134e9349SSaeed Mahameed 
2836134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
2837134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
2838134e9349SSaeed Mahameed 		break;
2839134e9349SSaeed Mahameed 	default:
2840134e9349SSaeed Mahameed 		return -EINVAL;
2841134e9349SSaeed Mahameed 	}
2842134e9349SSaeed Mahameed 
2843134e9349SSaeed Mahameed 	return 0;
2844134e9349SSaeed Mahameed }
2845134e9349SSaeed Mahameed 
2846d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
2847e126ba97SEli Cohen {
2848d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
2849d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
2850d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
2851e126ba97SEli Cohen 	struct ib_event ibev;
2852dbaaff2aSEli Cohen 	bool fatal = false;
2853e126ba97SEli Cohen 
2854df097a27SSaeed Mahameed 	if (work->is_slave) {
2855df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
2856d69a24e0SDaniel Jurgens 		if (!ibdev)
2857d69a24e0SDaniel Jurgens 			goto out;
2858d69a24e0SDaniel Jurgens 	} else {
2859df097a27SSaeed Mahameed 		ibdev = work->dev;
2860d69a24e0SDaniel Jurgens 	}
2861d69a24e0SDaniel Jurgens 
2862d69a24e0SDaniel Jurgens 	switch (work->event) {
2863e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2864e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
286589ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2866134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
2867dbaaff2aSEli Cohen 		fatal = true;
2868e126ba97SEli Cohen 		break;
2869134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
2870134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
2871d69a24e0SDaniel Jurgens 			goto out;
2872e126ba97SEli Cohen 		break;
287309e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
287409e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
2875df561f66SGustavo A. R. Silva 		fallthrough;
2876bdc37924SSaeed Mahameed 	default:
287703404e8aSMaor Gottlieb 		goto out;
2878e126ba97SEli Cohen 	}
2879e126ba97SEli Cohen 
2880e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
2881e126ba97SEli Cohen 
2882134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
2883134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
288403404e8aSMaor Gottlieb 		goto out;
2885a0c84c32SEli Cohen 	}
2886a0c84c32SEli Cohen 
2887e126ba97SEli Cohen 	if (ibdev->ib_active)
2888e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2889dbaaff2aSEli Cohen 
2890dbaaff2aSEli Cohen 	if (fatal)
2891dbaaff2aSEli Cohen 		ibdev->ib_active = false;
289203404e8aSMaor Gottlieb out:
2893d69a24e0SDaniel Jurgens 	kfree(work);
2894d69a24e0SDaniel Jurgens }
2895d69a24e0SDaniel Jurgens 
2896df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
2897df097a27SSaeed Mahameed 			 unsigned long event, void *param)
2898d69a24e0SDaniel Jurgens {
2899d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
2900d69a24e0SDaniel Jurgens 
2901d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
290210bea9c8SLeon Romanovsky 	if (!work)
2903df097a27SSaeed Mahameed 		return NOTIFY_DONE;
290410bea9c8SLeon Romanovsky 
2905d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2906df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
2907df097a27SSaeed Mahameed 	work->is_slave = false;
2908d69a24e0SDaniel Jurgens 	work->param = param;
2909d69a24e0SDaniel Jurgens 	work->event = event;
2910d69a24e0SDaniel Jurgens 
2911d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
2912df097a27SSaeed Mahameed 
2913df097a27SSaeed Mahameed 	return NOTIFY_OK;
2914df097a27SSaeed Mahameed }
2915df097a27SSaeed Mahameed 
2916df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
2917df097a27SSaeed Mahameed 				    unsigned long event, void *param)
2918df097a27SSaeed Mahameed {
2919df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
2920df097a27SSaeed Mahameed 
2921df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
2922df097a27SSaeed Mahameed 	if (!work)
2923df097a27SSaeed Mahameed 		return NOTIFY_DONE;
2924df097a27SSaeed Mahameed 
2925df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2926df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
2927df097a27SSaeed Mahameed 	work->is_slave = true;
2928df097a27SSaeed Mahameed 	work->param = param;
2929df097a27SSaeed Mahameed 	work->event = event;
2930df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
2931df097a27SSaeed Mahameed 
2932df097a27SSaeed Mahameed 	return NOTIFY_OK;
2933d69a24e0SDaniel Jurgens }
2934d69a24e0SDaniel Jurgens 
2935c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2936c43f1112SMaor Gottlieb {
2937c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2938c43f1112SMaor Gottlieb 	int err;
2939c43f1112SMaor Gottlieb 	int port;
2940c43f1112SMaor Gottlieb 
2941a989ea01SMark Bloch 	for (port = 1; port <= ARRAY_SIZE(dev->mdev->port_caps); port++) {
2942c43f1112SMaor Gottlieb 		dev->mdev->port_caps[port - 1].has_smi = false;
2943c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2944c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2945c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2946c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2947c43f1112SMaor Gottlieb 								   port, 0,
2948c43f1112SMaor Gottlieb 								   &vport_ctx);
2949c43f1112SMaor Gottlieb 				if (err) {
2950c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2951c43f1112SMaor Gottlieb 						    port, err);
2952c43f1112SMaor Gottlieb 					return err;
2953c43f1112SMaor Gottlieb 				}
2954c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi =
2955c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2956c43f1112SMaor Gottlieb 			} else {
2957c43f1112SMaor Gottlieb 				dev->mdev->port_caps[port - 1].has_smi = true;
2958c43f1112SMaor Gottlieb 			}
2959c43f1112SMaor Gottlieb 		}
2960c43f1112SMaor Gottlieb 	}
2961c43f1112SMaor Gottlieb 	return 0;
2962c43f1112SMaor Gottlieb }
2963c43f1112SMaor Gottlieb 
2964e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2965e126ba97SEli Cohen {
2966e126ba97SEli Cohen 	int port;
2967e126ba97SEli Cohen 
2968508562d6SDaniel Jurgens 	for (port = 1; port <= dev->num_ports; port++)
2969e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2970e126ba97SEli Cohen }
2971e126ba97SEli Cohen 
297226628e2dSMark Bloch static int __get_port_caps(struct mlx5_ib_dev *dev, u8 port)
2973e126ba97SEli Cohen {
2974e126ba97SEli Cohen 	struct ib_device_attr *dprops = NULL;
2975e126ba97SEli Cohen 	struct ib_port_attr *pprops = NULL;
2976f614fc15SDan Carpenter 	int err = -ENOMEM;
2977e126ba97SEli Cohen 
297850ba3c18SLeon Romanovsky 	pprops = kzalloc(sizeof(*pprops), GFP_KERNEL);
2979e126ba97SEli Cohen 	if (!pprops)
2980e126ba97SEli Cohen 		goto out;
2981e126ba97SEli Cohen 
2982e126ba97SEli Cohen 	dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
2983e126ba97SEli Cohen 	if (!dprops)
2984e126ba97SEli Cohen 		goto out;
2985e126ba97SEli Cohen 
298648357091SLeon Romanovsky 	err = mlx5_ib_query_device(&dev->ib_dev, dprops, NULL);
2987e126ba97SEli Cohen 	if (err) {
2988e126ba97SEli Cohen 		mlx5_ib_warn(dev, "query_device failed %d\n", err);
2989e126ba97SEli Cohen 		goto out;
2990e126ba97SEli Cohen 	}
2991e126ba97SEli Cohen 
2992e126ba97SEli Cohen 	err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
2993e126ba97SEli Cohen 	if (err) {
2994938fe83cSSaeed Mahameed 		mlx5_ib_warn(dev, "query_port %d failed %d\n",
2995938fe83cSSaeed Mahameed 			     port, err);
299632f69e4bSDaniel Jurgens 		goto out;
2997e126ba97SEli Cohen 	}
299832f69e4bSDaniel Jurgens 
2999938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].pkey_table_len =
3000938fe83cSSaeed Mahameed 					dprops->max_pkeys;
3001938fe83cSSaeed Mahameed 	dev->mdev->port_caps[port - 1].gid_table_len =
3002938fe83cSSaeed Mahameed 					pprops->gid_tbl_len;
300332f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
300432f69e4bSDaniel Jurgens 		    port, dprops->max_pkeys, pprops->gid_tbl_len);
3005e126ba97SEli Cohen 
3006e126ba97SEli Cohen out:
3007e126ba97SEli Cohen 	kfree(pprops);
3008e126ba97SEli Cohen 	kfree(dprops);
3009e126ba97SEli Cohen 
3010e126ba97SEli Cohen 	return err;
3011e126ba97SEli Cohen }
3012e126ba97SEli Cohen 
301326628e2dSMark Bloch static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
301426628e2dSMark Bloch {
301526628e2dSMark Bloch 	/* For representors use port 1, is this is the only native
301626628e2dSMark Bloch 	 * port
301726628e2dSMark Bloch 	 */
301826628e2dSMark Bloch 	if (dev->is_rep)
301926628e2dSMark Bloch 		return __get_port_caps(dev, 1);
302026628e2dSMark Bloch 	return __get_port_caps(dev, port);
302126628e2dSMark Bloch }
302226628e2dSMark Bloch 
30236e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
30246e8484c5SMax Gurtovoy {
30256e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
30266e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
30276e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
30286e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
30296e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
30306e8484c5SMax Gurtovoy 	default:
30316e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
30326e8484c5SMax Gurtovoy 	}
30336e8484c5SMax Gurtovoy }
30346e8484c5SMax Gurtovoy 
30351e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev)
3036e126ba97SEli Cohen {
30371e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
3038e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
303921a428a0SLeon Romanovsky 	struct ib_device *ibdev;
3040bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
30417722f47eSHaggai Eran 	int port;
3042e126ba97SEli Cohen 	int ret = 0;
3043e126ba97SEli Cohen 
304421a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
3045e126ba97SEli Cohen 
3046f4375443SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
3047f4375443SLeon Romanovsky 		return -EOPNOTSUPP;
3048f4375443SLeon Romanovsky 
3049d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
3050d16e91daSHaggai Eran 
305121a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
305221a428a0SLeon Romanovsky 	if (!devr->p0)
305321a428a0SLeon Romanovsky 		return -ENOMEM;
305421a428a0SLeon Romanovsky 
305521a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
3056e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3057e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3058e126ba97SEli Cohen 
3059ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
306021a428a0SLeon Romanovsky 	if (ret)
306121a428a0SLeon Romanovsky 		goto error0;
306221a428a0SLeon Romanovsky 
3063e39afe3dSLeon Romanovsky 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
3064e39afe3dSLeon Romanovsky 	if (!devr->c0) {
3065e39afe3dSLeon Romanovsky 		ret = -ENOMEM;
3066e126ba97SEli Cohen 		goto error1;
3067e126ba97SEli Cohen 	}
3068e39afe3dSLeon Romanovsky 
3069e126ba97SEli Cohen 	devr->c0->device = &dev->ib_dev;
3070e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3071e126ba97SEli Cohen 
3072e39afe3dSLeon Romanovsky 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
3073e39afe3dSLeon Romanovsky 	if (ret)
3074e39afe3dSLeon Romanovsky 		goto err_create_cq;
3075e39afe3dSLeon Romanovsky 
3076f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
3077f4375443SLeon Romanovsky 	if (ret)
3078e126ba97SEli Cohen 		goto error2;
3079e126ba97SEli Cohen 
3080f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
3081f4375443SLeon Romanovsky 	if (ret)
3082e126ba97SEli Cohen 		goto error3;
3083e126ba97SEli Cohen 
3084e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3085e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3086e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3087e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
30881a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3089e126ba97SEli Cohen 
309068e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
309168e326deSLeon Romanovsky 	if (!devr->s0) {
309268e326deSLeon Romanovsky 		ret = -ENOMEM;
3093e126ba97SEli Cohen 		goto error4;
3094e126ba97SEli Cohen 	}
309568e326deSLeon Romanovsky 
3096e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3097e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3098e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
30991a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
310068e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
310168e326deSLeon Romanovsky 	if (ret)
310268e326deSLeon Romanovsky 		goto err_create;
310368e326deSLeon Romanovsky 
31041a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3105e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3106e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3107e126ba97SEli Cohen 
31084aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
31094aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
31104aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
31114aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
311268e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
311368e326deSLeon Romanovsky 	if (!devr->s1) {
311468e326deSLeon Romanovsky 		ret = -ENOMEM;
31154aa17b28SHaggai Abramonvsky 		goto error5;
31164aa17b28SHaggai Abramonvsky 	}
311768e326deSLeon Romanovsky 
31184aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
31194aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
31204aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
31211a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
312268e326deSLeon Romanovsky 
312368e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
312468e326deSLeon Romanovsky 	if (ret)
312568e326deSLeon Romanovsky 		goto error6;
312668e326deSLeon Romanovsky 
31274aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
31281a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
31294aa17b28SHaggai Abramonvsky 
31307722f47eSHaggai Eran 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
31317722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
31327722f47eSHaggai Eran 			  pkey_change_handler);
31337722f47eSHaggai Eran 		devr->ports[port].devr = devr;
31347722f47eSHaggai Eran 	}
31357722f47eSHaggai Eran 
3136e126ba97SEli Cohen 	return 0;
3137e126ba97SEli Cohen 
313868e326deSLeon Romanovsky error6:
313968e326deSLeon Romanovsky 	kfree(devr->s1);
31404aa17b28SHaggai Abramonvsky error5:
3141c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
314268e326deSLeon Romanovsky err_create:
314368e326deSLeon Romanovsky 	kfree(devr->s0);
3144e126ba97SEli Cohen error4:
3145f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
3146e126ba97SEli Cohen error3:
3147f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
3148e126ba97SEli Cohen error2:
3149c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
3150e39afe3dSLeon Romanovsky err_create_cq:
3151e39afe3dSLeon Romanovsky 	kfree(devr->c0);
3152e126ba97SEli Cohen error1:
3153c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
3154e126ba97SEli Cohen error0:
315521a428a0SLeon Romanovsky 	kfree(devr->p0);
3156e126ba97SEli Cohen 	return ret;
3157e126ba97SEli Cohen }
3158e126ba97SEli Cohen 
31591e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev)
3160e126ba97SEli Cohen {
31611e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
31627722f47eSHaggai Eran 	int port;
31637722f47eSHaggai Eran 
3164c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
316568e326deSLeon Romanovsky 	kfree(devr->s1);
3166c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
316768e326deSLeon Romanovsky 	kfree(devr->s0);
3168f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
3169f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
3170c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
3171e39afe3dSLeon Romanovsky 	kfree(devr->c0);
3172c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
317321a428a0SLeon Romanovsky 	kfree(devr->p0);
31747722f47eSHaggai Eran 
31757722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
31765d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
31777722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3178e126ba97SEli Cohen }
3179e126ba97SEli Cohen 
3180b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
3181b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
3182e53505a8SAchiad Shochat {
3183e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3184e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3185e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3186e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
318785c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
3188e53505a8SAchiad Shochat 	u32 ret = 0;
3189e53505a8SAchiad Shochat 
3190b02289b3SArtemy Kovalyov 	if (rep->grh_required)
3191b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
3192b02289b3SArtemy Kovalyov 
3193e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3194b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
3195e53505a8SAchiad Shochat 
319685c7c014SDaniel Jurgens 	if (raw_support)
3197b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
319872cd5717SOr Gerlitz 
3199e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
320072cd5717SOr Gerlitz 		return ret;
3201e53505a8SAchiad Shochat 
3202e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
320372cd5717SOr Gerlitz 		return ret;
3204e53505a8SAchiad Shochat 
3205e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3206e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3207e53505a8SAchiad Shochat 
3208e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3209e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3210e53505a8SAchiad Shochat 
3211e53505a8SAchiad Shochat 	return ret;
3212e53505a8SAchiad Shochat }
3213e53505a8SAchiad Shochat 
32147738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
32157738613eSIra Weiny 			       struct ib_port_immutable *immutable)
32167738613eSIra Weiny {
32177738613eSIra Weiny 	struct ib_port_attr attr;
3218ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3219ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
3220b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
32217738613eSIra Weiny 	int err;
32227738613eSIra Weiny 
3223c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
32247738613eSIra Weiny 	if (err)
32257738613eSIra Weiny 		return err;
32267738613eSIra Weiny 
3227b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
3228b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
3229b02289b3SArtemy Kovalyov 						   &rep);
3230b02289b3SArtemy Kovalyov 		if (err)
3231b02289b3SArtemy Kovalyov 			return err;
3232b02289b3SArtemy Kovalyov 	}
3233b02289b3SArtemy Kovalyov 
32347738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
32357738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3236b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
3237337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
32387738613eSIra Weiny 
32397738613eSIra Weiny 	return 0;
32407738613eSIra Weiny }
32417738613eSIra Weiny 
32428e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
32438e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
32448e6efa3aSMark Bloch {
32458e6efa3aSMark Bloch 	struct ib_port_attr attr;
32468e6efa3aSMark Bloch 	int err;
32478e6efa3aSMark Bloch 
32488e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
32498e6efa3aSMark Bloch 
32508e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
32518e6efa3aSMark Bloch 	if (err)
32528e6efa3aSMark Bloch 		return err;
32538e6efa3aSMark Bloch 
32548e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
32558e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
32568e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
32578e6efa3aSMark Bloch 
32588e6efa3aSMark Bloch 	return 0;
32598e6efa3aSMark Bloch }
32608e6efa3aSMark Bloch 
32619abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3262c7342823SIra Weiny {
3263c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3264c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
32659abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
32669abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
32679abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3268c7342823SIra Weiny }
3269c7342823SIra Weiny 
327045f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
32719ef9c640SAviv Heller {
32729ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
32739ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
32749ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
32759ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
32769ef9c640SAviv Heller 	int err;
32779ef9c640SAviv Heller 
32787c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
32799ef9c640SAviv Heller 		return 0;
32809ef9c640SAviv Heller 
32819ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
32829ef9c640SAviv Heller 	if (err)
32839ef9c640SAviv Heller 		return err;
32849ef9c640SAviv Heller 
32859ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
32869ef9c640SAviv Heller 	if (IS_ERR(ft)) {
32879ef9c640SAviv Heller 		err = PTR_ERR(ft);
32889ef9c640SAviv Heller 		goto err_destroy_vport_lag;
32899ef9c640SAviv Heller 	}
32909ef9c640SAviv Heller 
32919a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
32927c34ec19SAviv Heller 	dev->lag_active = true;
32939ef9c640SAviv Heller 	return 0;
32949ef9c640SAviv Heller 
32959ef9c640SAviv Heller err_destroy_vport_lag:
32969ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
32979ef9c640SAviv Heller 	return err;
32989ef9c640SAviv Heller }
32999ef9c640SAviv Heller 
330045f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
33019ef9c640SAviv Heller {
33029ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
33039ef9c640SAviv Heller 
33047c34ec19SAviv Heller 	if (dev->lag_active) {
33057c34ec19SAviv Heller 		dev->lag_active = false;
33067c34ec19SAviv Heller 
33079a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
33089a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
33099ef9c640SAviv Heller 
33109ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
33119ef9c640SAviv Heller 	}
33129ef9c640SAviv Heller }
33139ef9c640SAviv Heller 
33147fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
3315fc24fc5eSAchiad Shochat {
3316e53505a8SAchiad Shochat 	int err;
3317e53505a8SAchiad Shochat 
331895579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
331995579e78SMark Bloch 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
33205ec8c83eSAviv Heller 	if (err) {
332195579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3322e53505a8SAchiad Shochat 		return err;
33235ec8c83eSAviv Heller 	}
3324e53505a8SAchiad Shochat 
3325d012f5d6SOr Gerlitz 	return 0;
3326d012f5d6SOr Gerlitz }
3327d012f5d6SOr Gerlitz 
33287fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
3329e126ba97SEli Cohen {
333095579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
333195579e78SMark Bloch 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
333295579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3333e126ba97SEli Cohen 	}
3334e126ba97SEli Cohen }
3335e126ba97SEli Cohen 
3336e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3337e126ba97SEli Cohen {
3338e126ba97SEli Cohen 	int err;
3339e126ba97SEli Cohen 
3340e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
3341e53505a8SAchiad Shochat 	if (err)
33428e6efa3aSMark Bloch 		return err;
3343e53505a8SAchiad Shochat 
334445f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
33459ef9c640SAviv Heller 	if (err)
33469ef9c640SAviv Heller 		goto err_disable_roce;
33479ef9c640SAviv Heller 
3348e53505a8SAchiad Shochat 	return 0;
3349e53505a8SAchiad Shochat 
33509ef9c640SAviv Heller err_disable_roce:
33519ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
33529ef9c640SAviv Heller 
3353e53505a8SAchiad Shochat 	return err;
3354fc24fc5eSAchiad Shochat }
3355fc24fc5eSAchiad Shochat 
335645f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3357fc24fc5eSAchiad Shochat {
335845f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3359e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
3360fc24fc5eSAchiad Shochat }
3361fc24fc5eSAchiad Shochat 
3362f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
3363693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
3364f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
3365693dfd5aSErez Shitrit {
3366693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3367f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
3368693dfd5aSErez Shitrit 
3369f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
3370693dfd5aSErez Shitrit }
3371693dfd5aSErez Shitrit 
3372fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3373fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3374fe248c3aSMaor Gottlieb {
3375fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3376fe248c3aSMaor Gottlieb 	char lbuf[20];
3377fe248c3aSMaor Gottlieb 	int len;
3378fe248c3aSMaor Gottlieb 
3379fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3380fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3381fe248c3aSMaor Gottlieb }
3382fe248c3aSMaor Gottlieb 
3383fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3384fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3385fe248c3aSMaor Gottlieb {
3386fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3387fe248c3aSMaor Gottlieb 	u32 timeout;
3388fe248c3aSMaor Gottlieb 	u32 var;
3389fe248c3aSMaor Gottlieb 
3390fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3391fe248c3aSMaor Gottlieb 		return -EFAULT;
3392fe248c3aSMaor Gottlieb 
3393fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3394fe248c3aSMaor Gottlieb 			1000);
3395fe248c3aSMaor Gottlieb 	if (timeout != var)
3396fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3397fe248c3aSMaor Gottlieb 			    timeout);
3398fe248c3aSMaor Gottlieb 
3399fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3400fe248c3aSMaor Gottlieb 
3401fe248c3aSMaor Gottlieb 	return count;
3402fe248c3aSMaor Gottlieb }
3403fe248c3aSMaor Gottlieb 
3404fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3405fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3406fe248c3aSMaor Gottlieb 	.open	= simple_open,
3407fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3408fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3409fe248c3aSMaor Gottlieb };
3410fe248c3aSMaor Gottlieb 
341132f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
341232f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
341332f69e4bSDaniel Jurgens {
341432f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
341532f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
341632f69e4bSDaniel Jurgens 	int comps;
341732f69e4bSDaniel Jurgens 	int err;
341832f69e4bSDaniel Jurgens 	int i;
341932f69e4bSDaniel Jurgens 
34209dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
34219dc4cfffSLeon Romanovsky 
3422a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
3423a9e546e7SParav Pandit 
342432f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
342532f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
342632f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
342732f69e4bSDaniel Jurgens 		return;
342832f69e4bSDaniel Jurgens 	}
3429df097a27SSaeed Mahameed 
343032f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
343132f69e4bSDaniel Jurgens 
343232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
343323eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
343423eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
343523eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
343632f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
343732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
343832f69e4bSDaniel Jurgens 
343932f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
344032f69e4bSDaniel Jurgens 	if (comps) {
344132f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
344232f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
344332f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
344432f69e4bSDaniel Jurgens 
344532f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
344632f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
344732f69e4bSDaniel Jurgens 
344832f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
344932f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
345032f69e4bSDaniel Jurgens 	}
345132f69e4bSDaniel Jurgens 
345232f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
345332f69e4bSDaniel Jurgens 
345432f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
345532f69e4bSDaniel Jurgens 
345632f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
345732f69e4bSDaniel Jurgens 
345832f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
345932f69e4bSDaniel Jurgens 
346032f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
346132f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
346232f69e4bSDaniel Jurgens 	 * it back to the list.
346332f69e4bSDaniel Jurgens 	 */
346432f69e4bSDaniel Jurgens 	if (err)
346532f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
346632f69e4bSDaniel Jurgens 			    port_num + 1);
346732f69e4bSDaniel Jurgens 
346895579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
346932f69e4bSDaniel Jurgens }
347032f69e4bSDaniel Jurgens 
347132f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
347232f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
347332f69e4bSDaniel Jurgens {
347432f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
347532f69e4bSDaniel Jurgens 	int err;
347632f69e4bSDaniel Jurgens 
34779dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
34789dc4cfffSLeon Romanovsky 
347932f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
348032f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
34812577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
348232f69e4bSDaniel Jurgens 			    port_num + 1);
348332f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
348432f69e4bSDaniel Jurgens 		return false;
348532f69e4bSDaniel Jurgens 	}
348632f69e4bSDaniel Jurgens 
348732f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
348832f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
3489df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
349032f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
349132f69e4bSDaniel Jurgens 
349232f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
349332f69e4bSDaniel Jurgens 	if (err)
349432f69e4bSDaniel Jurgens 		goto unbind;
349532f69e4bSDaniel Jurgens 
349632f69e4bSDaniel Jurgens 	err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
349732f69e4bSDaniel Jurgens 	if (err)
349832f69e4bSDaniel Jurgens 		goto unbind;
349932f69e4bSDaniel Jurgens 
350032f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
350132f69e4bSDaniel Jurgens 	if (err) {
350232f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
350332f69e4bSDaniel Jurgens 			    port_num + 1);
350432f69e4bSDaniel Jurgens 		goto unbind;
350532f69e4bSDaniel Jurgens 	}
350632f69e4bSDaniel Jurgens 
3507df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
3508df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
3509df097a27SSaeed Mahameed 
351073eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
3511a9e546e7SParav Pandit 
351232f69e4bSDaniel Jurgens 	return true;
351332f69e4bSDaniel Jurgens 
351432f69e4bSDaniel Jurgens unbind:
351532f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
351632f69e4bSDaniel Jurgens 	return false;
351732f69e4bSDaniel Jurgens }
351832f69e4bSDaniel Jurgens 
351932f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
352032f69e4bSDaniel Jurgens {
352132f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
352232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
352332f69e4bSDaniel Jurgens 							  port_num + 1);
352432f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
352532f69e4bSDaniel Jurgens 	int err;
352632f69e4bSDaniel Jurgens 	int i;
352732f69e4bSDaniel Jurgens 
352832f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
352932f69e4bSDaniel Jurgens 		return 0;
353032f69e4bSDaniel Jurgens 
353132f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
353232f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
353332f69e4bSDaniel Jurgens 	if (err)
353432f69e4bSDaniel Jurgens 		return err;
353532f69e4bSDaniel Jurgens 
353632f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
353732f69e4bSDaniel Jurgens 	if (err)
353832f69e4bSDaniel Jurgens 		return err;
353932f69e4bSDaniel Jurgens 
354032f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
354132f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
354232f69e4bSDaniel Jurgens 		bool bound = false;
354332f69e4bSDaniel Jurgens 
354432f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
354532f69e4bSDaniel Jurgens 		if (i == port_num) {
354632f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
354732f69e4bSDaniel Jurgens 			if (!mpi) {
354832f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
354932f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
355032f69e4bSDaniel Jurgens 				return -ENOMEM;
355132f69e4bSDaniel Jurgens 			}
355232f69e4bSDaniel Jurgens 
355332f69e4bSDaniel Jurgens 			mpi->is_master = true;
355432f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
355532f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
355632f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
355732f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
355832f69e4bSDaniel Jurgens 			mpi = NULL;
355932f69e4bSDaniel Jurgens 			continue;
356032f69e4bSDaniel Jurgens 		}
356132f69e4bSDaniel Jurgens 
356232f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
356332f69e4bSDaniel Jurgens 				    list) {
356432f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
356532f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
356632f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
356732f69e4bSDaniel Jurgens 			}
356832f69e4bSDaniel Jurgens 
356932f69e4bSDaniel Jurgens 			if (bound) {
3570c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
3571c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
357232f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
357332f69e4bSDaniel Jurgens 				list_del(&mpi->list);
357432f69e4bSDaniel Jurgens 				break;
357532f69e4bSDaniel Jurgens 			}
357632f69e4bSDaniel Jurgens 		}
357732f69e4bSDaniel Jurgens 		if (!bound) {
357832f69e4bSDaniel Jurgens 			get_port_caps(dev, i + 1);
357932f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
358032f69e4bSDaniel Jurgens 				    i + 1);
358132f69e4bSDaniel Jurgens 		}
358232f69e4bSDaniel Jurgens 	}
358332f69e4bSDaniel Jurgens 
358432f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
358532f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
358632f69e4bSDaniel Jurgens 	return err;
358732f69e4bSDaniel Jurgens }
358832f69e4bSDaniel Jurgens 
358932f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
359032f69e4bSDaniel Jurgens {
359132f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
359232f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
359332f69e4bSDaniel Jurgens 							  port_num + 1);
359432f69e4bSDaniel Jurgens 	int i;
359532f69e4bSDaniel Jurgens 
359632f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
359732f69e4bSDaniel Jurgens 		return;
359832f69e4bSDaniel Jurgens 
359932f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
360032f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
360132f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
360232f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
360332f69e4bSDaniel Jurgens 			if (i == port_num) {
360432f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
360532f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
360632f69e4bSDaniel Jurgens 			} else {
360732f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
360832f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
360932f69e4bSDaniel Jurgens 			}
361032f69e4bSDaniel Jurgens 		}
361132f69e4bSDaniel Jurgens 	}
361232f69e4bSDaniel Jurgens 
361332f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
361432f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
361532f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
361632f69e4bSDaniel Jurgens 
361732f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
361832f69e4bSDaniel Jurgens }
361932f69e4bSDaniel Jurgens 
3620342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
36217be76befSYishai Hadas 			    enum rdma_remove_reason why,
36227be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
36237be76befSYishai Hadas {
36247be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
36257be76befSYishai Hadas 
36267be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
36277be76befSYishai Hadas 	return 0;
36287be76befSYishai Hadas }
36297be76befSYishai Hadas 
3630342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
3631342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
3632342ee59dSYishai Hadas 					    size_t length)
3633342ee59dSYishai Hadas {
3634342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
3635342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
3636342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
3637342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
3638342ee59dSYishai Hadas }
3639342ee59dSYishai Hadas 
36407be76befSYishai Hadas static struct mlx5_user_mmap_entry *
36417be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
36427be76befSYishai Hadas {
36437be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
36447be76befSYishai Hadas 	struct mlx5_var_table *var_table;
36457be76befSYishai Hadas 	u32 page_idx;
36467be76befSYishai Hadas 	int err;
36477be76befSYishai Hadas 
36487be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
36497be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
36507be76befSYishai Hadas 	if (!entry)
36517be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
36527be76befSYishai Hadas 
36537be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
36547be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
36557be76befSYishai Hadas 				       var_table->num_var_hw_entries);
36567be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
36577be76befSYishai Hadas 		err = -ENOSPC;
36587be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
36597be76befSYishai Hadas 		goto end;
36607be76befSYishai Hadas 	}
36617be76befSYishai Hadas 
36627be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
36637be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
36647be76befSYishai Hadas 
36657be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
36667be76befSYishai Hadas 				(page_idx * var_table->stride_size);
36677be76befSYishai Hadas 	entry->page_idx = page_idx;
36687be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
36697be76befSYishai Hadas 
3670342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
3671342ee59dSYishai Hadas 					       var_table->stride_size);
36727be76befSYishai Hadas 	if (err)
36737be76befSYishai Hadas 		goto err_insert;
36747be76befSYishai Hadas 
36757be76befSYishai Hadas 	return entry;
36767be76befSYishai Hadas 
36777be76befSYishai Hadas err_insert:
36787be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
36797be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
36807be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
36817be76befSYishai Hadas end:
36827be76befSYishai Hadas 	kfree(entry);
36837be76befSYishai Hadas 	return ERR_PTR(err);
36847be76befSYishai Hadas }
36857be76befSYishai Hadas 
36867be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
36877be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
36887be76befSYishai Hadas {
36897be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
36907be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
36917be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
36927be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
36937be76befSYishai Hadas 	u64 mmap_offset;
36947be76befSYishai Hadas 	u32 length;
36957be76befSYishai Hadas 	int err;
36967be76befSYishai Hadas 
36977be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
36987be76befSYishai Hadas 	if (IS_ERR(c))
36997be76befSYishai Hadas 		return PTR_ERR(c);
37007be76befSYishai Hadas 
37017be76befSYishai Hadas 	entry = alloc_var_entry(c);
37027be76befSYishai Hadas 	if (IS_ERR(entry))
37037be76befSYishai Hadas 		return PTR_ERR(entry);
37047be76befSYishai Hadas 
37057be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
37067be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
37077be76befSYishai Hadas 	uobj->object = entry;
37080ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
37097be76befSYishai Hadas 
37107be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
37117be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
37127be76befSYishai Hadas 	if (err)
37130ac8903cSJason Gunthorpe 		return err;
37147be76befSYishai Hadas 
37157be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
37167be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
37177be76befSYishai Hadas 	if (err)
37180ac8903cSJason Gunthorpe 		return err;
37197be76befSYishai Hadas 
37207be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
37217be76befSYishai Hadas 			     &length, sizeof(length));
37227be76befSYishai Hadas 	return err;
37237be76befSYishai Hadas }
37247be76befSYishai Hadas 
37257be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
37267be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
37277be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
37287be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
37297be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
37307be76befSYishai Hadas 			UA_MANDATORY),
37317be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
37327be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
37337be76befSYishai Hadas 			   UA_MANDATORY),
37347be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
37357be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
37367be76befSYishai Hadas 			   UA_MANDATORY),
37377be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
37387be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
37397be76befSYishai Hadas 			    UA_MANDATORY));
37407be76befSYishai Hadas 
37417be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
37427be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
37437be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
37447be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
37457be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
37467be76befSYishai Hadas 			UA_MANDATORY));
37477be76befSYishai Hadas 
37487be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
3749342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
37507be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
37517be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
37527be76befSYishai Hadas 
37537be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
37547be76befSYishai Hadas {
37557be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
37567be76befSYishai Hadas 
37577be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
37587be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
37597be76befSYishai Hadas }
37607be76befSYishai Hadas 
3761342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
3762342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
3763342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
3764342ee59dSYishai Hadas {
3765342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3766342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
3767342ee59dSYishai Hadas 	u32 uar_index;
3768342ee59dSYishai Hadas 	int err;
3769342ee59dSYishai Hadas 
3770342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3771342ee59dSYishai Hadas 	if (!entry)
3772342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
3773342ee59dSYishai Hadas 
3774342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
3775342ee59dSYishai Hadas 	err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
3776342ee59dSYishai Hadas 	if (err)
3777342ee59dSYishai Hadas 		goto end;
3778342ee59dSYishai Hadas 
3779342ee59dSYishai Hadas 	entry->page_idx = uar_index;
3780342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
3781342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3782342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
3783342ee59dSYishai Hadas 	else
3784342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
3785342ee59dSYishai Hadas 
3786342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
3787342ee59dSYishai Hadas 	if (err)
3788342ee59dSYishai Hadas 		goto err_insert;
3789342ee59dSYishai Hadas 
3790342ee59dSYishai Hadas 	return entry;
3791342ee59dSYishai Hadas 
3792342ee59dSYishai Hadas err_insert:
3793342ee59dSYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, uar_index);
3794342ee59dSYishai Hadas end:
3795342ee59dSYishai Hadas 	kfree(entry);
3796342ee59dSYishai Hadas 	return ERR_PTR(err);
3797342ee59dSYishai Hadas }
3798342ee59dSYishai Hadas 
3799342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
3800342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
3801342ee59dSYishai Hadas {
3802342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
3803342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3804342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
3805342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
3806342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3807342ee59dSYishai Hadas 	u64 mmap_offset;
3808342ee59dSYishai Hadas 	u32 length;
3809342ee59dSYishai Hadas 	int err;
3810342ee59dSYishai Hadas 
3811342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3812342ee59dSYishai Hadas 	if (IS_ERR(c))
3813342ee59dSYishai Hadas 		return PTR_ERR(c);
3814342ee59dSYishai Hadas 
3815342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
3816342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
3817342ee59dSYishai Hadas 	if (err)
3818342ee59dSYishai Hadas 		return err;
3819342ee59dSYishai Hadas 
3820342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
3821342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
3822342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3823342ee59dSYishai Hadas 
3824342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
3825342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3826342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3827342ee59dSYishai Hadas 
3828342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
3829342ee59dSYishai Hadas 	if (IS_ERR(entry))
3830342ee59dSYishai Hadas 		return PTR_ERR(entry);
3831342ee59dSYishai Hadas 
3832342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
3833342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
3834342ee59dSYishai Hadas 	uobj->object = entry;
38350ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3836342ee59dSYishai Hadas 
3837342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3838342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
3839342ee59dSYishai Hadas 	if (err)
38400ac8903cSJason Gunthorpe 		return err;
3841342ee59dSYishai Hadas 
3842342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3843342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
3844342ee59dSYishai Hadas 	if (err)
38450ac8903cSJason Gunthorpe 		return err;
3846342ee59dSYishai Hadas 
3847342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3848342ee59dSYishai Hadas 			     &length, sizeof(length));
3849342ee59dSYishai Hadas 	return err;
3850342ee59dSYishai Hadas }
3851342ee59dSYishai Hadas 
3852342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
3853342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
3854342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
3855342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3856342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
3857342ee59dSYishai Hadas 			UA_MANDATORY),
3858342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
3859342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
3860342ee59dSYishai Hadas 			     UA_MANDATORY),
3861342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3862342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3863342ee59dSYishai Hadas 			   UA_MANDATORY),
3864342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3865342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3866342ee59dSYishai Hadas 			   UA_MANDATORY),
3867342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3868342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
3869342ee59dSYishai Hadas 			    UA_MANDATORY));
3870342ee59dSYishai Hadas 
3871342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3872342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
3873342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
3874342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3875342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
3876342ee59dSYishai Hadas 			UA_MANDATORY));
3877342ee59dSYishai Hadas 
3878342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
3879342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3880342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
3881342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
3882342ee59dSYishai Hadas 
38839a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
38849a119cd5SJason Gunthorpe 	mlx5_ib_dm,
38859a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
388624da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
38879a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
388824da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
388983bb4442SJason Gunthorpe 			    UA_MANDATORY),
38909a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
389124da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
38923b113a1eSAriel Levkovich 			    UA_OPTIONAL),
38933b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
38943b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
38953b113a1eSAriel Levkovich 			     UA_OPTIONAL));
389624da0016SAriel Levkovich 
38979a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
38989a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
38999a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
3900c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
3901bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3902bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
3903c6475a0bSAviad Yehezkel 
39040fb556b2SYishai Hadas ADD_UVERBS_ATTRIBUTES_SIMPLE(
39050fb556b2SYishai Hadas 	mlx5_ib_query_context,
39060fb556b2SYishai Hadas 	UVERBS_OBJECT_DEVICE,
39070fb556b2SYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
39080fb556b2SYishai Hadas 	UVERBS_ATTR_PTR_OUT(
39090fb556b2SYishai Hadas 		MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
39100fb556b2SYishai Hadas 		UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
39110fb556b2SYishai Hadas 				   dump_fill_mkey),
39120fb556b2SYishai Hadas 		UA_MANDATORY));
39130fb556b2SYishai Hadas 
39140cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
391536e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
39160cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
391730f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
391805f71ef9SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
39198c84660bSMatan Barak 
39200cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
39210cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
39220cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
39230fb556b2SYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
39247be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
39257be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
3926342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
39270cbf432dSJason Gunthorpe 	{}
39280cbf432dSJason Gunthorpe };
39298c84660bSMatan Barak 
3930fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
3931e126ba97SEli Cohen {
393232f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3933806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
3934806b101bSJason Gunthorpe 	cleanup_srcu_struct(&dev->odp_srcu);
39354056b12eSAriel Levkovich 
393650211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
39374056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
393816c1975fSMark Bloch }
393916c1975fSMark Bloch 
3940fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
394116c1975fSMark Bloch {
394216c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
3943e126ba97SEli Cohen 	int err;
394432f69e4bSDaniel Jurgens 	int i;
3945e126ba97SEli Cohen 
394632f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
394732f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
394895579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
3949d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
3950d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
3951d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
395232f69e4bSDaniel Jurgens 	}
395332f69e4bSDaniel Jurgens 
395400815752SMoni Shoua 	mlx5_ib_internal_fill_odp_caps(dev);
395500815752SMoni Shoua 
395632f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
3957e126ba97SEli Cohen 	if (err)
3958da796ccbSMark Bloch 		return err;
3959e126ba97SEli Cohen 
3960a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
3961a989ea01SMark Bloch 	if (err)
3962a989ea01SMark Bloch 		return err;
3963e126ba97SEli Cohen 
396432f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(mdev)) {
396532f69e4bSDaniel Jurgens 		for (i = 1; i <= dev->num_ports; i++) {
396632f69e4bSDaniel Jurgens 			err = get_port_caps(dev, i);
396732f69e4bSDaniel Jurgens 			if (err)
396832f69e4bSDaniel Jurgens 				break;
396932f69e4bSDaniel Jurgens 		}
397032f69e4bSDaniel Jurgens 	} else {
397132f69e4bSDaniel Jurgens 		err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
397232f69e4bSDaniel Jurgens 	}
397332f69e4bSDaniel Jurgens 	if (err)
397432f69e4bSDaniel Jurgens 		goto err_mp;
397532f69e4bSDaniel Jurgens 
39761b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3977e126ba97SEli Cohen 		get_ext_port_caps(dev);
3978e126ba97SEli Cohen 
3979e126ba97SEli Cohen 	dev->ib_dev.node_type		= RDMA_NODE_IB_CA;
3980c6790aa9SSagi Grimberg 	dev->ib_dev.local_dma_lkey	= 0 /* not supported for now */;
3981e126ba97SEli Cohen 	dev->ib_dev.phys_port_cnt	= dev->num_ports;
3982f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
3983c42260f1SVu Pham 	dev->ib_dev.dev.parent		= mdev->device;
3984cfc1a89eSMaor Gottlieb 	dev->ib_dev.lag_flags		= RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
3985e126ba97SEli Cohen 
39863cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
39873cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
39883cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
3989806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
399050211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
3991f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
39923cc297dbSMark Bloch 
39933b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
39943b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
399524da0016SAriel Levkovich 
3996806b101bSJason Gunthorpe 	err = init_srcu_struct(&dev->odp_srcu);
3997a6bc3875SMoni Shoua 	if (err)
3998c9b9dcb4SAriel Levkovich 		goto err_mp;
39993cc297dbSMark Bloch 
400016c1975fSMark Bloch 	return 0;
400125c13324SAriel Levkovich 
400232f69e4bSDaniel Jurgens err_mp:
400332f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
400416c1975fSMark Bloch 
400516c1975fSMark Bloch 	return -ENOMEM;
400616c1975fSMark Bloch }
400716c1975fSMark Bloch 
4008ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev)
40099a4ca38dSMark Bloch {
4010ac47bf5eSLeon Romanovsky 	struct mlx5_ib_dev *mdev = to_mdev(dev);
4011ac47bf5eSLeon Romanovsky 	int ret;
40129a4ca38dSMark Bloch 
4013ac47bf5eSLeon Romanovsky 	ret = mlx5_ib_test_wc(mdev);
4014ac47bf5eSLeon Romanovsky 	mlx5_ib_dbg(mdev, "Write-Combining %s",
4015ac47bf5eSLeon Romanovsky 		    mdev->wc_support ? "supported" : "not supported");
40169a4ca38dSMark Bloch 
4017ac47bf5eSLeon Romanovsky 	return ret;
40189a4ca38dSMark Bloch }
40199a4ca38dSMark Bloch 
402096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
40217a154142SJason Gunthorpe 	.owner = THIS_MODULE,
4022b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
402372c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
4024b9560a41SJason Gunthorpe 
402596458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
402696458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
40276c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
402896458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
402996458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
403096458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
403196458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
403296458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
403396458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
403496458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
403596458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
403696458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
403796458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
403896458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
403996458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
404096458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
404196458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
404296458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
404396458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
404496458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
404596458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
404696458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
404796458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
404811f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
404996458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
405096458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
405196458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
405296458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
40536c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
405496458233SKamal Heib 	.mmap = mlx5_ib_mmap,
4055dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
405696458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
405796458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
405896458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
405996458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
406096458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
406196458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
4062029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
4063029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
406496458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
406596458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
406696458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
406796458233SKamal Heib 	.query_device = mlx5_ib_query_device,
406896458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
406996458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
407096458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
407196458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
40720fb556b2SYishai Hadas 	.query_ucontext = mlx5_ib_query_ucontext,
407396458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
407496458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
407596458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
407696458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
4077d3456914SLeon Romanovsky 
4078d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
40793b023e1bSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
4080e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
408121a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
408268e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
4083a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
408496458233SKamal Heib };
408596458233SKamal Heib 
408696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
408796458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
408896458233SKamal Heib };
408996458233SKamal Heib 
409096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
409196458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
40929c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
409396458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
409496458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
409596458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
409696458233SKamal Heib };
409796458233SKamal Heib 
409896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
409996458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
410096458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
410196458233SKamal Heib };
410296458233SKamal Heib 
410396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
410496458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
410596458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
410628ad5f65SLeon Romanovsky 
410728ad5f65SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd),
410896458233SKamal Heib };
410996458233SKamal Heib 
411096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
411196458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
411296458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
411396458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
411496458233SKamal Heib };
411596458233SKamal Heib 
4116f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
4117f164be8cSYishai Hadas {
4118f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
4119f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
4120f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
4121f164be8cSYishai Hadas 	u8 log_doorbell_stride;
4122f164be8cSYishai Hadas 	u64 bar_size;
4123f164be8cSYishai Hadas 
4124f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
4125f164be8cSYishai Hadas 					log_doorbell_bar_size);
4126f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
4127f164be8cSYishai Hadas 					log_doorbell_stride);
4128f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
4129f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
4130f164be8cSYishai Hadas 					doorbell_bar_offset);
4131f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
4132f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
413391b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
413491b74bf5SAlexander Lobakin 						var_table->stride_size);
4135f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
4136f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
4137f164be8cSYishai Hadas 					  GFP_KERNEL);
4138f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
4139f164be8cSYishai Hadas }
4140f164be8cSYishai Hadas 
4141f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
4142f164be8cSYishai Hadas {
4143f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
4144f164be8cSYishai Hadas }
4145f164be8cSYishai Hadas 
4146fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
414716c1975fSMark Bloch {
414816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
414916c1975fSMark Bloch 	int err;
415016c1975fSMark Bloch 
4151e126ba97SEli Cohen 	dev->ib_dev.uverbs_cmd_mask	=
4152e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_GET_CONTEXT)		|
4153e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_DEVICE)	|
4154e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_PORT)		|
4155e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ALLOC_PD)		|
4156e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEALLOC_PD)		|
415741c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_CREATE_AH)		|
415841c450fdSMoni Shoua 		(1ull << IB_USER_VERBS_CMD_DESTROY_AH)		|
4159e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_REG_MR)		|
416056e11d62SNoa Osherovich 		(1ull << IB_USER_VERBS_CMD_REREG_MR)		|
4161e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DEREG_MR)		|
4162e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL)	|
4163e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_CQ)		|
4164e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_RESIZE_CQ)		|
4165e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_CQ)		|
4166e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_QP)		|
4167e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_QP)		|
4168e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_QP)		|
4169e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_QP)		|
4170e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_ATTACH_MCAST)	|
4171e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DETACH_MCAST)	|
4172e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_SRQ)		|
4173e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_MODIFY_SRQ)		|
4174e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_QUERY_SRQ)		|
4175e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_DESTROY_SRQ)		|
4176e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_CREATE_XSRQ)		|
4177e126ba97SEli Cohen 		(1ull << IB_USER_VERBS_CMD_OPEN_QP);
41781707cb4aSHaggai Eran 	dev->ib_dev.uverbs_ex_cmd_mask =
4179d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE)	|
4180d4584ddfSMatan Barak 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ)	|
41817d29f349SBodong Wang 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_QP)	|
4182b0e9df6dSYonatan Cohen 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP)	|
418396458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ)	|
418496458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW)	|
418596458233SKamal Heib 		(1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
4186e126ba97SEli Cohen 
4187f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
4188f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
418996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
419096458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
41918e959601SNiranjana Vishwanathapura 
419296458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
419396458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
41947c2344c3SMaor Gottlieb 
41956e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
41966e8484c5SMax Gurtovoy 
4197d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
4198d2370e0aSMatan Barak 		dev->ib_dev.uverbs_cmd_mask |=
4199d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_ALLOC_MW)	|
4200d2370e0aSMatan Barak 			(1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
420196458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
4202d2370e0aSMatan Barak 	}
4203d2370e0aSMatan Barak 
4204938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc)) {
4205e126ba97SEli Cohen 		dev->ib_dev.uverbs_cmd_mask |=
4206e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
4207e126ba97SEli Cohen 			(1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
420896458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
4209e126ba97SEli Cohen 	}
4210e126ba97SEli Cohen 
421125c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
421225c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
421325c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
421496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
421524da0016SAriel Levkovich 
421696458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
421781e30880SYishai Hadas 
421836e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
421936e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
422081e30880SYishai Hadas 
4221e126ba97SEli Cohen 	err = init_node_data(dev);
4222e126ba97SEli Cohen 	if (err)
422316c1975fSMark Bloch 		return err;
4224e126ba97SEli Cohen 
4225c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4226e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
4227e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
4228a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
4229c8b89924SMark Bloch 
4230f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
4231f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
4232f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
4233f164be8cSYishai Hadas 		if (err)
4234f164be8cSYishai Hadas 			return err;
4235f164be8cSYishai Hadas 	}
4236f164be8cSYishai Hadas 
423796e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
423896e2fd73SLeon Romanovsky 
423916c1975fSMark Bloch 	return 0;
424016c1975fSMark Bloch }
424116c1975fSMark Bloch 
424296458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
424396458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
424496458233SKamal Heib 	.query_port = mlx5_ib_query_port,
424596458233SKamal Heib };
424696458233SKamal Heib 
42478e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
424816c1975fSMark Bloch {
424996458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
42508e6efa3aSMark Bloch 	return 0;
42518e6efa3aSMark Bloch }
42528e6efa3aSMark Bloch 
425396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
425496458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
425596458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
425696458233SKamal Heib };
425796458233SKamal Heib 
4258b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
42598e6efa3aSMark Bloch {
426096458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
42618e6efa3aSMark Bloch 	return 0;
42628e6efa3aSMark Bloch }
42638e6efa3aSMark Bloch 
426496458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
426596458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
426696458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
426796458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
426896458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
426996458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
427096458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
427196458233SKamal Heib };
427296458233SKamal Heib 
42731e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev)
42748e6efa3aSMark Bloch {
42751e2b5a90SLeon Romanovsky 	struct mlx5_core_dev *mdev = dev->mdev;
42761e2b5a90SLeon Romanovsky 	enum rdma_link_layer ll;
42771e2b5a90SLeon Romanovsky 	int port_type_cap;
42781e2b5a90SLeon Romanovsky 	u8 port_num = 0;
42791e2b5a90SLeon Romanovsky 	int err;
42807fd8aefbSDaniel Jurgens 
42811e2b5a90SLeon Romanovsky 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
42821e2b5a90SLeon Romanovsky 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
42831e2b5a90SLeon Romanovsky 
42841e2b5a90SLeon Romanovsky 	if (ll == IB_LINK_LAYER_ETHERNET) {
4285c11a226aSMark Bloch 		dev->ib_dev.uverbs_ex_cmd_mask |=
4286c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
4287c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
4288c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
4289c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
4290c11a226aSMark Bloch 			(1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
429196458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
42928e6efa3aSMark Bloch 
4293e3f1ed1fSLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4294e3f1ed1fSLeon Romanovsky 
429526628e2dSMark Bloch 		/* Register only for native ports */
42961e2b5a90SLeon Romanovsky 		err = mlx5_add_netdev_notifier(dev, port_num);
42971e2b5a90SLeon Romanovsky 		if (err || dev->is_rep || !mlx5_is_roce_enabled(mdev))
42981e2b5a90SLeon Romanovsky 			/*
42991e2b5a90SLeon Romanovsky 			 * We don't enable ETH interface for
43001e2b5a90SLeon Romanovsky 			 * 1. IB representors
43011e2b5a90SLeon Romanovsky 			 * 2. User disabled ROCE through devlink interface
43021e2b5a90SLeon Romanovsky 			 */
43038e6efa3aSMark Bloch 			return err;
43048e6efa3aSMark Bloch 
4305e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
43068e6efa3aSMark Bloch 		if (err)
43078e6efa3aSMark Bloch 			goto cleanup;
43088e6efa3aSMark Bloch 	}
43098e6efa3aSMark Bloch 
431016c1975fSMark Bloch 	return 0;
43118e6efa3aSMark Bloch cleanup:
43121e2b5a90SLeon Romanovsky 	mlx5_remove_netdev_notifier(dev, port_num);
43138e6efa3aSMark Bloch 	return err;
431445bded2cSKamal Heib }
43156aec21f6SHaggai Eran 
43161e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev)
431716c1975fSMark Bloch {
431816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
431916c1975fSMark Bloch 	enum rdma_link_layer ll;
432016c1975fSMark Bloch 	int port_type_cap;
43211e2b5a90SLeon Romanovsky 	u8 port_num;
43224a2da0b8SParav Pandit 
432316c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
432416c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
432516c1975fSMark Bloch 
432616c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
43271e2b5a90SLeon Romanovsky 		if (!dev->is_rep)
432816c1975fSMark Bloch 			mlx5_disable_eth(dev);
432916c1975fSMark Bloch 
43301e2b5a90SLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
43311e2b5a90SLeon Romanovsky 		mlx5_remove_netdev_notifier(dev, port_num);
433216c1975fSMark Bloch 	}
433316c1975fSMark Bloch }
433416c1975fSMark Bloch 
433516c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
433616c1975fSMark Bloch {
433773eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
4338a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
433973eb8f03SGreg Kroah-Hartman 	return 0;
434016c1975fSMark Bloch }
434116c1975fSMark Bloch 
434216c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
434316c1975fSMark Bloch {
4344a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
4345a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
434616c1975fSMark Bloch }
434716c1975fSMark Bloch 
434816c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
434916c1975fSMark Bloch {
43505fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4351444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
435216c1975fSMark Bloch }
435316c1975fSMark Bloch 
435416c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
435516c1975fSMark Bloch {
435616c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
435716c1975fSMark Bloch }
435816c1975fSMark Bloch 
4359fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
436016c1975fSMark Bloch {
436116c1975fSMark Bloch 	int err;
43625fe9dec0SEli Cohen 
43635fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
43645fe9dec0SEli Cohen 	if (err)
436516c1975fSMark Bloch 		return err;
43665fe9dec0SEli Cohen 
43675fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
43685fe9dec0SEli Cohen 	if (err)
436916c1975fSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
43705fe9dec0SEli Cohen 
437116c1975fSMark Bloch 	return err;
437216c1975fSMark Bloch }
43730837e86aSMark Bloch 
4374fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
437516c1975fSMark Bloch {
437616c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
437716c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
437816c1975fSMark Bloch }
4379e126ba97SEli Cohen 
4380fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
438116c1975fSMark Bloch {
4382e349f858SJason Gunthorpe 	const char *name;
4383e349f858SJason Gunthorpe 
4384508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
43857c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
4386e349f858SJason Gunthorpe 		name = "mlx5_%d";
4387e349f858SJason Gunthorpe 	else
4388e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
4389ea4baf7fSParav Pandit 	return ib_register_device(&dev->ib_dev, name);
439016c1975fSMark Bloch }
439116c1975fSMark Bloch 
4392fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
439342cea83fSMark Bloch {
43941e2b5a90SLeon Romanovsky 	int err;
43951e2b5a90SLeon Romanovsky 
43961e2b5a90SLeon Romanovsky 	err = mlx5_mr_cache_cleanup(dev);
43971e2b5a90SLeon Romanovsky 	if (err)
43981e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
43991e2b5a90SLeon Romanovsky 
44001e2b5a90SLeon Romanovsky 	if (dev->umrc.qp)
44011e2b5a90SLeon Romanovsky 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
44021e2b5a90SLeon Romanovsky 	if (dev->umrc.cq)
44031e2b5a90SLeon Romanovsky 		ib_free_cq(dev->umrc.cq);
44041e2b5a90SLeon Romanovsky 	if (dev->umrc.pd)
44051e2b5a90SLeon Romanovsky 		ib_dealloc_pd(dev->umrc.pd);
440642cea83fSMark Bloch }
440742cea83fSMark Bloch 
4408fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
440916c1975fSMark Bloch {
441016c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
441116c1975fSMark Bloch }
441216c1975fSMark Bloch 
44131e2b5a90SLeon Romanovsky enum {
44141e2b5a90SLeon Romanovsky 	MAX_UMR_WR = 128,
44151e2b5a90SLeon Romanovsky };
44161e2b5a90SLeon Romanovsky 
4417fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
441816c1975fSMark Bloch {
44191e2b5a90SLeon Romanovsky 	struct ib_qp_init_attr *init_attr = NULL;
44201e2b5a90SLeon Romanovsky 	struct ib_qp_attr *attr = NULL;
44211e2b5a90SLeon Romanovsky 	struct ib_pd *pd;
44221e2b5a90SLeon Romanovsky 	struct ib_cq *cq;
44231e2b5a90SLeon Romanovsky 	struct ib_qp *qp;
44241e2b5a90SLeon Romanovsky 	int ret;
44251e2b5a90SLeon Romanovsky 
44261e2b5a90SLeon Romanovsky 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
44271e2b5a90SLeon Romanovsky 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
44281e2b5a90SLeon Romanovsky 	if (!attr || !init_attr) {
44291e2b5a90SLeon Romanovsky 		ret = -ENOMEM;
44301e2b5a90SLeon Romanovsky 		goto error_0;
44311e2b5a90SLeon Romanovsky 	}
44321e2b5a90SLeon Romanovsky 
44331e2b5a90SLeon Romanovsky 	pd = ib_alloc_pd(&dev->ib_dev, 0);
44341e2b5a90SLeon Romanovsky 	if (IS_ERR(pd)) {
44351e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
44361e2b5a90SLeon Romanovsky 		ret = PTR_ERR(pd);
44371e2b5a90SLeon Romanovsky 		goto error_0;
44381e2b5a90SLeon Romanovsky 	}
44391e2b5a90SLeon Romanovsky 
44401e2b5a90SLeon Romanovsky 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
44411e2b5a90SLeon Romanovsky 	if (IS_ERR(cq)) {
44421e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
44431e2b5a90SLeon Romanovsky 		ret = PTR_ERR(cq);
44441e2b5a90SLeon Romanovsky 		goto error_2;
44451e2b5a90SLeon Romanovsky 	}
44461e2b5a90SLeon Romanovsky 
44471e2b5a90SLeon Romanovsky 	init_attr->send_cq = cq;
44481e2b5a90SLeon Romanovsky 	init_attr->recv_cq = cq;
44491e2b5a90SLeon Romanovsky 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
44501e2b5a90SLeon Romanovsky 	init_attr->cap.max_send_wr = MAX_UMR_WR;
44511e2b5a90SLeon Romanovsky 	init_attr->cap.max_send_sge = 1;
44521e2b5a90SLeon Romanovsky 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
44531e2b5a90SLeon Romanovsky 	init_attr->port_num = 1;
44541e2b5a90SLeon Romanovsky 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
44551e2b5a90SLeon Romanovsky 	if (IS_ERR(qp)) {
44561e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
44571e2b5a90SLeon Romanovsky 		ret = PTR_ERR(qp);
44581e2b5a90SLeon Romanovsky 		goto error_3;
44591e2b5a90SLeon Romanovsky 	}
44601e2b5a90SLeon Romanovsky 	qp->device     = &dev->ib_dev;
44611e2b5a90SLeon Romanovsky 	qp->real_qp    = qp;
44621e2b5a90SLeon Romanovsky 	qp->uobject    = NULL;
44631e2b5a90SLeon Romanovsky 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
44641e2b5a90SLeon Romanovsky 	qp->send_cq    = init_attr->send_cq;
44651e2b5a90SLeon Romanovsky 	qp->recv_cq    = init_attr->recv_cq;
44661e2b5a90SLeon Romanovsky 
44671e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_INIT;
44681e2b5a90SLeon Romanovsky 	attr->port_num = 1;
44691e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
44701e2b5a90SLeon Romanovsky 				IB_QP_PORT, NULL);
44711e2b5a90SLeon Romanovsky 	if (ret) {
44721e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
44731e2b5a90SLeon Romanovsky 		goto error_4;
44741e2b5a90SLeon Romanovsky 	}
44751e2b5a90SLeon Romanovsky 
44761e2b5a90SLeon Romanovsky 	memset(attr, 0, sizeof(*attr));
44771e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_RTR;
44781e2b5a90SLeon Romanovsky 	attr->path_mtu = IB_MTU_256;
44791e2b5a90SLeon Romanovsky 
44801e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
44811e2b5a90SLeon Romanovsky 	if (ret) {
44821e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
44831e2b5a90SLeon Romanovsky 		goto error_4;
44841e2b5a90SLeon Romanovsky 	}
44851e2b5a90SLeon Romanovsky 
44861e2b5a90SLeon Romanovsky 	memset(attr, 0, sizeof(*attr));
44871e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_RTS;
44881e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
44891e2b5a90SLeon Romanovsky 	if (ret) {
44901e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
44911e2b5a90SLeon Romanovsky 		goto error_4;
44921e2b5a90SLeon Romanovsky 	}
44931e2b5a90SLeon Romanovsky 
44941e2b5a90SLeon Romanovsky 	dev->umrc.qp = qp;
44951e2b5a90SLeon Romanovsky 	dev->umrc.cq = cq;
44961e2b5a90SLeon Romanovsky 	dev->umrc.pd = pd;
44971e2b5a90SLeon Romanovsky 
44981e2b5a90SLeon Romanovsky 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
44991e2b5a90SLeon Romanovsky 	ret = mlx5_mr_cache_init(dev);
45001e2b5a90SLeon Romanovsky 	if (ret) {
45011e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
45021e2b5a90SLeon Romanovsky 		goto error_4;
45031e2b5a90SLeon Romanovsky 	}
45041e2b5a90SLeon Romanovsky 
45051e2b5a90SLeon Romanovsky 	kfree(attr);
45061e2b5a90SLeon Romanovsky 	kfree(init_attr);
45071e2b5a90SLeon Romanovsky 
45081e2b5a90SLeon Romanovsky 	return 0;
45091e2b5a90SLeon Romanovsky 
45101e2b5a90SLeon Romanovsky error_4:
45111e2b5a90SLeon Romanovsky 	mlx5_ib_destroy_qp(qp, NULL);
45121e2b5a90SLeon Romanovsky 	dev->umrc.qp = NULL;
45131e2b5a90SLeon Romanovsky 
45141e2b5a90SLeon Romanovsky error_3:
45151e2b5a90SLeon Romanovsky 	ib_free_cq(cq);
45161e2b5a90SLeon Romanovsky 	dev->umrc.cq = NULL;
45171e2b5a90SLeon Romanovsky 
45181e2b5a90SLeon Romanovsky error_2:
45191e2b5a90SLeon Romanovsky 	ib_dealloc_pd(pd);
45201e2b5a90SLeon Romanovsky 	dev->umrc.pd = NULL;
45211e2b5a90SLeon Romanovsky 
45221e2b5a90SLeon Romanovsky error_0:
45231e2b5a90SLeon Romanovsky 	kfree(attr);
45241e2b5a90SLeon Romanovsky 	kfree(init_attr);
45251e2b5a90SLeon Romanovsky 	return ret;
452616c1975fSMark Bloch }
452716c1975fSMark Bloch 
452816c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
452916c1975fSMark Bloch {
45301e2b5a90SLeon Romanovsky 	struct dentry *root;
453103404e8aSMaor Gottlieb 
45321e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
45331e2b5a90SLeon Romanovsky 		return 0;
45341e2b5a90SLeon Romanovsky 
45351e2b5a90SLeon Romanovsky 	mutex_init(&dev->delay_drop.lock);
45361e2b5a90SLeon Romanovsky 	dev->delay_drop.dev = dev;
45371e2b5a90SLeon Romanovsky 	dev->delay_drop.activate = false;
45381e2b5a90SLeon Romanovsky 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
45391e2b5a90SLeon Romanovsky 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
45401e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
45411e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.events_cnt, 0);
45421e2b5a90SLeon Romanovsky 
45431e2b5a90SLeon Romanovsky 	if (!mlx5_debugfs_root)
45441e2b5a90SLeon Romanovsky 		return 0;
45451e2b5a90SLeon Romanovsky 
45461e2b5a90SLeon Romanovsky 	root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
45471e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = root;
45481e2b5a90SLeon Romanovsky 
45491e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
45501e2b5a90SLeon Romanovsky 				&dev->delay_drop.events_cnt);
45511e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_rqs", 0400, root,
45521e2b5a90SLeon Romanovsky 				&dev->delay_drop.rqs_cnt);
45531e2b5a90SLeon Romanovsky 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
45541e2b5a90SLeon Romanovsky 			    &fops_delay_drop_timeout);
455516c1975fSMark Bloch 	return 0;
455616c1975fSMark Bloch }
455716c1975fSMark Bloch 
455816c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
455916c1975fSMark Bloch {
45601e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
45611e2b5a90SLeon Romanovsky 		return;
45621e2b5a90SLeon Romanovsky 
45631e2b5a90SLeon Romanovsky 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
45641e2b5a90SLeon Romanovsky 	if (!dev->delay_drop.dir_debugfs)
45651e2b5a90SLeon Romanovsky 		return;
45661e2b5a90SLeon Romanovsky 
45671e2b5a90SLeon Romanovsky 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
45681e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = NULL;
456916c1975fSMark Bloch }
457016c1975fSMark Bloch 
4571df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
4572df097a27SSaeed Mahameed {
4573df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
4574df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
4575df097a27SSaeed Mahameed 	return 0;
4576df097a27SSaeed Mahameed }
4577df097a27SSaeed Mahameed 
4578df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
4579df097a27SSaeed Mahameed {
4580df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
4581df097a27SSaeed Mahameed }
4582df097a27SSaeed Mahameed 
4583b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
458416c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
458516c1975fSMark Bloch 		      int stage)
458616c1975fSMark Bloch {
45874cca96a8SParav Pandit 	dev->ib_active = false;
45884cca96a8SParav Pandit 
458916c1975fSMark Bloch 	/* Number of stages to cleanup */
459016c1975fSMark Bloch 	while (stage) {
459116c1975fSMark Bloch 		stage--;
459216c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
459316c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
459416c1975fSMark Bloch 	}
45954a6dc855SMark Bloch 
4596da796ccbSMark Bloch 	kfree(dev->port);
45974a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
459816c1975fSMark Bloch }
459916c1975fSMark Bloch 
4600b5ca15adSMark Bloch void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
460116c1975fSMark Bloch 		    const struct mlx5_ib_profile *profile)
460216c1975fSMark Bloch {
460316c1975fSMark Bloch 	int err;
460416c1975fSMark Bloch 	int i;
460516c1975fSMark Bloch 
46068d93efb8SMark Bloch 	dev->profile = profile;
46078d93efb8SMark Bloch 
460816c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
460916c1975fSMark Bloch 		if (profile->stage[i].init) {
461016c1975fSMark Bloch 			err = profile->stage[i].init(dev);
461116c1975fSMark Bloch 			if (err)
461216c1975fSMark Bloch 				goto err_out;
461316c1975fSMark Bloch 		}
461416c1975fSMark Bloch 	}
461516c1975fSMark Bloch 
4616e126ba97SEli Cohen 	dev->ib_active = true;
4617e126ba97SEli Cohen 
46189603b61dSJack Morgenstein 	return dev;
4619e126ba97SEli Cohen 
462016c1975fSMark Bloch err_out:
462116c1975fSMark Bloch 	__mlx5_ib_remove(dev, profile, i);
4622e126ba97SEli Cohen 
46239603b61dSJack Morgenstein 	return NULL;
4624e126ba97SEli Cohen }
4625e126ba97SEli Cohen 
462616c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
462716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
462816c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
462916c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4630f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4631f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4632f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
463316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
463416c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
4635f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
46368e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
46378e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
46388e6efa3aSMark Bloch 		     NULL),
463916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
46401e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
46411e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4642333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4643333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4644333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4645f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4646f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4647f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
464816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
46491e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
46501e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4651df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4652df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4653df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
465416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
46551e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_init_one,
46561e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_cleanup_one),
465716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
465864825827SLeon Romanovsky 		     mlx5_ib_counters_init,
465964825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
466016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
466116c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
466216c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
466316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
466416c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
466516c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
466616c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
466716c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
466816c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
466942cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
467042cea83fSMark Bloch 		     NULL,
467142cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
467281773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4673d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4674d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
467516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
467616c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
467716c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
467842cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
467942cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
468042cea83fSMark Bloch 		     NULL),
468116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
468216c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
468316c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
4684b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4685b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4686b572ebe6SLeon Romanovsky 		     NULL),
468716c1975fSMark Bloch };
468816c1975fSMark Bloch 
4689b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
4690b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
4691b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
4692b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4693f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4694f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4695f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
4696b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4697b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
4698f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
4699b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4700b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
4701b5ca15adSMark Bloch 		     NULL),
4702b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
47031e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
47041e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4705333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4706333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4707333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4708f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4709f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4710f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
4711b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
47121e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
47131e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4714df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4715df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4716df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
4717b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
471864825827SLeon Romanovsky 		     mlx5_ib_counters_init,
471964825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
472079db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
472179db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
472279db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
4723b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
4724b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
4725b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
4726b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4727b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
4728b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
472903fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
473003fe2debSDavid S. Miller 		     NULL,
473103fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
47327f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4733d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4734d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
4735b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4736b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
4737b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
473803fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
473903fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
474003fe2debSDavid S. Miller 		     NULL),
4741b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4742b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4743b572ebe6SLeon Romanovsky 		     NULL),
4744b5ca15adSMark Bloch };
4745b5ca15adSMark Bloch 
4746e3f1ed1fSLeon Romanovsky static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
474732f69e4bSDaniel Jurgens {
474832f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
474932f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
475032f69e4bSDaniel Jurgens 	bool bound = false;
475132f69e4bSDaniel Jurgens 	int err;
475232f69e4bSDaniel Jurgens 
475332f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
475432f69e4bSDaniel Jurgens 	if (!mpi)
475532f69e4bSDaniel Jurgens 		return NULL;
475632f69e4bSDaniel Jurgens 
475732f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
475832f69e4bSDaniel Jurgens 
475932f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
476032f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
476132f69e4bSDaniel Jurgens 	if (err) {
476232f69e4bSDaniel Jurgens 		kfree(mpi);
476332f69e4bSDaniel Jurgens 		return NULL;
476432f69e4bSDaniel Jurgens 	}
476532f69e4bSDaniel Jurgens 
476632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
476732f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
476832f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
476932f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
477032f69e4bSDaniel Jurgens 
477132f69e4bSDaniel Jurgens 		if (bound) {
477232f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
477332f69e4bSDaniel Jurgens 			break;
477432f69e4bSDaniel Jurgens 		}
477532f69e4bSDaniel Jurgens 	}
477632f69e4bSDaniel Jurgens 
477732f69e4bSDaniel Jurgens 	if (!bound) {
477832f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4779c42260f1SVu Pham 		dev_dbg(mdev->device,
4780c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
478132f69e4bSDaniel Jurgens 	}
478232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
478332f69e4bSDaniel Jurgens 
478432f69e4bSDaniel Jurgens 	return mpi;
478532f69e4bSDaniel Jurgens }
478632f69e4bSDaniel Jurgens 
478716c1975fSMark Bloch static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
478816c1975fSMark Bloch {
478994de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
479032f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
4791b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
479232f69e4bSDaniel Jurgens 	int port_type_cap;
4793da796ccbSMark Bloch 	int num_ports;
479432f69e4bSDaniel Jurgens 
4795f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) &&
4796f6455de0SBodong Wang 	    mlx5_ib_eswitch_mode(mdev->priv.eswitch) == MLX5_ESWITCH_OFFLOADS) {
47975fb58c9eSMark Bloch 		if (!mlx5_core_mp_enabled(mdev))
4798f0666f1fSBodong Wang 			mlx5_ib_register_vport_reps(mdev);
4799f0666f1fSBodong Wang 		return mdev;
4800f0666f1fSBodong Wang 	}
4801f0666f1fSBodong Wang 
480232f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
480332f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
480432f69e4bSDaniel Jurgens 
4805e3f1ed1fSLeon Romanovsky 	if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
4806e3f1ed1fSLeon Romanovsky 		return mlx5_ib_add_slave_port(mdev);
480732f69e4bSDaniel Jurgens 
4808da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4809da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
4810459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
4811b5ca15adSMark Bloch 	if (!dev)
4812b5ca15adSMark Bloch 		return NULL;
4813da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
4814da796ccbSMark Bloch 			     GFP_KERNEL);
4815da796ccbSMark Bloch 	if (!dev->port) {
4816a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
4817da796ccbSMark Bloch 		return NULL;
4818da796ccbSMark Bloch 	}
4819b5ca15adSMark Bloch 
4820b5ca15adSMark Bloch 	dev->mdev = mdev;
4821da796ccbSMark Bloch 	dev->num_ports = num_ports;
4822b5ca15adSMark Bloch 
482394de879cSMichael Guralnik 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_enabled(mdev))
482494de879cSMichael Guralnik 		profile = &raw_eth_profile;
482594de879cSMichael Guralnik 	else
482694de879cSMichael Guralnik 		profile = &pf_profile;
482794de879cSMichael Guralnik 
482894de879cSMichael Guralnik 	return __mlx5_ib_add(dev, profile);
482916c1975fSMark Bloch }
483016c1975fSMark Bloch 
48319603b61dSJack Morgenstein static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
4832e126ba97SEli Cohen {
483332f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
483432f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
48356aec21f6SHaggai Eran 
4836f0666f1fSBodong Wang 	if (MLX5_ESWITCH_MANAGER(mdev) && context == mdev) {
4837f0666f1fSBodong Wang 		mlx5_ib_unregister_vport_reps(mdev);
4838f0666f1fSBodong Wang 		return;
4839f0666f1fSBodong Wang 	}
4840f0666f1fSBodong Wang 
484132f69e4bSDaniel Jurgens 	if (mlx5_core_is_mp_slave(mdev)) {
484232f69e4bSDaniel Jurgens 		mpi = context;
484332f69e4bSDaniel Jurgens 		mutex_lock(&mlx5_ib_multiport_mutex);
484432f69e4bSDaniel Jurgens 		if (mpi->ibdev)
484532f69e4bSDaniel Jurgens 			mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
484632f69e4bSDaniel Jurgens 		list_del(&mpi->list);
484732f69e4bSDaniel Jurgens 		mutex_unlock(&mlx5_ib_multiport_mutex);
48485d44adebSDanit Goldberg 		kfree(mpi);
484932f69e4bSDaniel Jurgens 		return;
485032f69e4bSDaniel Jurgens 	}
485132f69e4bSDaniel Jurgens 
485232f69e4bSDaniel Jurgens 	dev = context;
485316c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4854e126ba97SEli Cohen }
4855e126ba97SEli Cohen 
48569603b61dSJack Morgenstein static struct mlx5_interface mlx5_ib_interface = {
48579603b61dSJack Morgenstein 	.add            = mlx5_ib_add,
48589603b61dSJack Morgenstein 	.remove         = mlx5_ib_remove,
485964613d94SSaeed Mahameed 	.protocol	= MLX5_INTERFACE_PROTOCOL_IB,
4860e126ba97SEli Cohen };
4861e126ba97SEli Cohen 
4862c44ef998SIlya Lesokhin unsigned long mlx5_ib_get_xlt_emergency_page(void)
4863c44ef998SIlya Lesokhin {
4864c44ef998SIlya Lesokhin 	mutex_lock(&xlt_emergency_page_mutex);
4865c44ef998SIlya Lesokhin 	return xlt_emergency_page;
4866c44ef998SIlya Lesokhin }
4867c44ef998SIlya Lesokhin 
4868c44ef998SIlya Lesokhin void mlx5_ib_put_xlt_emergency_page(void)
4869c44ef998SIlya Lesokhin {
4870c44ef998SIlya Lesokhin 	mutex_unlock(&xlt_emergency_page_mutex);
4871c44ef998SIlya Lesokhin }
4872c44ef998SIlya Lesokhin 
4873e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4874e126ba97SEli Cohen {
48756aec21f6SHaggai Eran 	int err;
48766aec21f6SHaggai Eran 
4877c44ef998SIlya Lesokhin 	xlt_emergency_page = __get_free_page(GFP_KERNEL);
4878c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
4879d69a24e0SDaniel Jurgens 		return -ENOMEM;
4880d69a24e0SDaniel Jurgens 
4881c44ef998SIlya Lesokhin 	mutex_init(&xlt_emergency_page_mutex);
4882c44ef998SIlya Lesokhin 
4883c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
4884c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
4885c44ef998SIlya Lesokhin 		free_page(xlt_emergency_page);
4886c44ef998SIlya Lesokhin 		return -ENOMEM;
4887c44ef998SIlya Lesokhin 	}
4888c44ef998SIlya Lesokhin 
488981713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
48909603b61dSJack Morgenstein 
48916aec21f6SHaggai Eran 	err = mlx5_register_interface(&mlx5_ib_interface);
48926aec21f6SHaggai Eran 
48936aec21f6SHaggai Eran 	return err;
4894e126ba97SEli Cohen }
4895e126ba97SEli Cohen 
4896e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4897e126ba97SEli Cohen {
48989603b61dSJack Morgenstein 	mlx5_unregister_interface(&mlx5_ib_interface);
4899d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
4900c44ef998SIlya Lesokhin 	mutex_destroy(&xlt_emergency_page_mutex);
4901c44ef998SIlya Lesokhin 	free_page(xlt_emergency_page);
4902e126ba97SEli Cohen }
4903e126ba97SEli Cohen 
4904e126ba97SEli Cohen module_init(mlx5_ib_init);
4905e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4906