xref: /openbmc/linux/drivers/infiniband/hw/mlx5/main.c (revision 2904bb37b35d07be7bfa3fb4a0fc1a3daa6678b3)
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.
490da7dc8SJianxin Xiong  * Copyright (c) 2020, Intel Corporation. All rights reserved.
5e126ba97SEli Cohen  */
6e126ba97SEli Cohen 
7fe248c3aSMaor Gottlieb #include <linux/debugfs.h>
8adec640eSChristoph Hellwig #include <linux/highmem.h>
9e126ba97SEli Cohen #include <linux/module.h>
10e126ba97SEli Cohen #include <linux/init.h>
11e126ba97SEli Cohen #include <linux/errno.h>
12e126ba97SEli Cohen #include <linux/pci.h>
13e126ba97SEli Cohen #include <linux/dma-mapping.h>
14e126ba97SEli Cohen #include <linux/slab.h>
1524da0016SAriel Levkovich #include <linux/bitmap.h>
16e126ba97SEli Cohen #include <linux/sched.h>
176e84f315SIngo Molnar #include <linux/sched/mm.h>
180881e7bdSIngo Molnar #include <linux/sched/task.h>
197c2344c3SMaor Gottlieb #include <linux/delay.h>
20e126ba97SEli Cohen #include <rdma/ib_user_verbs.h>
213f89a643SAchiad Shochat #include <rdma/ib_addr.h>
222811ba51SAchiad Shochat #include <rdma/ib_cache.h>
23ada68c31SAchiad Shochat #include <linux/mlx5/port.h>
241b5daf11SMajd Dibbiny #include <linux/mlx5/vport.h>
2572c7fe90SPravin Shedge #include <linux/mlx5/fs.h>
26cecae747SMaor Gottlieb #include <linux/mlx5/eswitch.h>
277c2344c3SMaor Gottlieb #include <linux/list.h>
28e126ba97SEli Cohen #include <rdma/ib_smi.h>
29e126ba97SEli Cohen #include <rdma/ib_umem.h>
30cfc1a89eSMaor Gottlieb #include <rdma/lag.h>
31038d2ef8SMaor Gottlieb #include <linux/in.h>
32038d2ef8SMaor Gottlieb #include <linux/etherdevice.h>
33e126ba97SEli Cohen #include "mlx5_ib.h"
34fc385b7aSMark Bloch #include "ib_rep.h"
35e1f24a79SParav Pandit #include "cmd.h"
36d8b7515eSLeon Romanovsky #include "devx.h"
37f7c4ffdaSLeon Romanovsky #include "fs.h"
38f3da6577SLeon Romanovsky #include "srq.h"
39333fbaa0SLeon Romanovsky #include "qp.h"
40029e88fdSLeon Romanovsky #include "wr.h"
41b572ebe6SLeon Romanovsky #include "restrack.h"
4264825827SLeon Romanovsky #include "counters.h"
43c6475a0bSAviad Yehezkel #include <linux/mlx5/accel.h>
448c84660bSMatan Barak #include <rdma/uverbs_std_types.h>
45*2904bb37SYishai Hadas #include <rdma/uverbs_ioctl.h>
46c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_verbs.h>
47c6475a0bSAviad Yehezkel #include <rdma/mlx5_user_ioctl_cmds.h>
484061ff7aSErez Alfasi #include <rdma/ib_umem_odp.h>
498c84660bSMatan Barak 
508c84660bSMatan Barak #define UVERBS_MODULE_NAME mlx5_ib
518c84660bSMatan Barak #include <rdma/uverbs_named_ioctl.h>
52e126ba97SEli Cohen 
53e126ba97SEli Cohen MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
54daeee976SShay Drory MODULE_DESCRIPTION("Mellanox 5th generation network adapters (ConnectX series) IB driver");
55e126ba97SEli Cohen MODULE_LICENSE("Dual BSD/GPL");
56e126ba97SEli Cohen 
57d69a24e0SDaniel Jurgens struct mlx5_ib_event_work {
58d69a24e0SDaniel Jurgens 	struct work_struct	work;
59df097a27SSaeed Mahameed 	union {
60df097a27SSaeed Mahameed 		struct mlx5_ib_dev	      *dev;
61df097a27SSaeed Mahameed 		struct mlx5_ib_multiport_info *mpi;
62df097a27SSaeed Mahameed 	};
63df097a27SSaeed Mahameed 	bool			is_slave;
64134e9349SSaeed Mahameed 	unsigned int		event;
65df097a27SSaeed Mahameed 	void			*param;
66d69a24e0SDaniel Jurgens };
67d69a24e0SDaniel Jurgens 
68da7525d2SEran Ben Elisha enum {
69da7525d2SEran Ben Elisha 	MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
70da7525d2SEran Ben Elisha };
711b5daf11SMajd Dibbiny 
72d69a24e0SDaniel Jurgens static struct workqueue_struct *mlx5_ib_event_wq;
7332f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
7432f69e4bSDaniel Jurgens static LIST_HEAD(mlx5_ib_dev_list);
7532f69e4bSDaniel Jurgens /*
7632f69e4bSDaniel Jurgens  * This mutex should be held when accessing either of the above lists
7732f69e4bSDaniel Jurgens  */
7832f69e4bSDaniel Jurgens static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
7932f69e4bSDaniel Jurgens 
8032f69e4bSDaniel Jurgens struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
8132f69e4bSDaniel Jurgens {
8232f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
8332f69e4bSDaniel Jurgens 
8432f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
8532f69e4bSDaniel Jurgens 	dev = mpi->ibdev;
8632f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
8732f69e4bSDaniel Jurgens 	return dev;
8832f69e4bSDaniel Jurgens }
8932f69e4bSDaniel Jurgens 
901b5daf11SMajd Dibbiny static enum rdma_link_layer
91ebd61f68SAchiad Shochat mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
921b5daf11SMajd Dibbiny {
93ebd61f68SAchiad Shochat 	switch (port_type_cap) {
941b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_IB:
951b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_INFINIBAND;
961b5daf11SMajd Dibbiny 	case MLX5_CAP_PORT_TYPE_ETH:
971b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_ETHERNET;
981b5daf11SMajd Dibbiny 	default:
991b5daf11SMajd Dibbiny 		return IB_LINK_LAYER_UNSPECIFIED;
1001b5daf11SMajd Dibbiny 	}
1011b5daf11SMajd Dibbiny }
1021b5daf11SMajd Dibbiny 
103ebd61f68SAchiad Shochat static enum rdma_link_layer
104ebd61f68SAchiad Shochat mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
105ebd61f68SAchiad Shochat {
106ebd61f68SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
107ebd61f68SAchiad Shochat 	int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
108ebd61f68SAchiad Shochat 
109ebd61f68SAchiad Shochat 	return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
110ebd61f68SAchiad Shochat }
111ebd61f68SAchiad Shochat 
112fd65f1b8SMoni Shoua static int get_port_state(struct ib_device *ibdev,
113fd65f1b8SMoni Shoua 			  u8 port_num,
114fd65f1b8SMoni Shoua 			  enum ib_port_state *state)
115fd65f1b8SMoni Shoua {
116fd65f1b8SMoni Shoua 	struct ib_port_attr attr;
117fd65f1b8SMoni Shoua 	int ret;
118fd65f1b8SMoni Shoua 
119fd65f1b8SMoni Shoua 	memset(&attr, 0, sizeof(attr));
1203023a1e9SKamal Heib 	ret = ibdev->ops.query_port(ibdev, port_num, &attr);
121fd65f1b8SMoni Shoua 	if (!ret)
122fd65f1b8SMoni Shoua 		*state = attr.state;
123fd65f1b8SMoni Shoua 	return ret;
124fd65f1b8SMoni Shoua }
125fd65f1b8SMoni Shoua 
12635b0aa67SMark Bloch static struct mlx5_roce *mlx5_get_rep_roce(struct mlx5_ib_dev *dev,
12735b0aa67SMark Bloch 					   struct net_device *ndev,
12835b0aa67SMark Bloch 					   u8 *port_num)
12935b0aa67SMark Bloch {
13035b0aa67SMark Bloch 	struct mlx5_eswitch *esw = dev->mdev->priv.eswitch;
13135b0aa67SMark Bloch 	struct net_device *rep_ndev;
13235b0aa67SMark Bloch 	struct mlx5_ib_port *port;
13335b0aa67SMark Bloch 	int i;
13435b0aa67SMark Bloch 
13535b0aa67SMark Bloch 	for (i = 0; i < dev->num_ports; i++) {
13635b0aa67SMark Bloch 		port  = &dev->port[i];
13735b0aa67SMark Bloch 		if (!port->rep)
13835b0aa67SMark Bloch 			continue;
13935b0aa67SMark Bloch 
14035b0aa67SMark Bloch 		read_lock(&port->roce.netdev_lock);
14135b0aa67SMark Bloch 		rep_ndev = mlx5_ib_get_rep_netdev(esw,
14235b0aa67SMark Bloch 						  port->rep->vport);
14335b0aa67SMark Bloch 		if (rep_ndev == ndev) {
14435b0aa67SMark Bloch 			read_unlock(&port->roce.netdev_lock);
14535b0aa67SMark Bloch 			*port_num = i + 1;
14635b0aa67SMark Bloch 			return &port->roce;
14735b0aa67SMark Bloch 		}
14835b0aa67SMark Bloch 		read_unlock(&port->roce.netdev_lock);
14935b0aa67SMark Bloch 	}
15035b0aa67SMark Bloch 
15135b0aa67SMark Bloch 	return NULL;
15235b0aa67SMark Bloch }
15335b0aa67SMark Bloch 
154fc24fc5eSAchiad Shochat static int mlx5_netdev_event(struct notifier_block *this,
155fc24fc5eSAchiad Shochat 			     unsigned long event, void *ptr)
156fc24fc5eSAchiad Shochat {
1577fd8aefbSDaniel Jurgens 	struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
158fc24fc5eSAchiad Shochat 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
1597fd8aefbSDaniel Jurgens 	u8 port_num = roce->native_port_num;
1607fd8aefbSDaniel Jurgens 	struct mlx5_core_dev *mdev;
1617fd8aefbSDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
1627fd8aefbSDaniel Jurgens 
1637fd8aefbSDaniel Jurgens 	ibdev = roce->dev;
16432f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
16532f69e4bSDaniel Jurgens 	if (!mdev)
16632f69e4bSDaniel Jurgens 		return NOTIFY_DONE;
167fc24fc5eSAchiad Shochat 
1685ec8c83eSAviv Heller 	switch (event) {
1695ec8c83eSAviv Heller 	case NETDEV_REGISTER:
17035b0aa67SMark Bloch 		/* Should already be registered during the load */
17135b0aa67SMark Bloch 		if (ibdev->is_rep)
17235b0aa67SMark Bloch 			break;
1737fd8aefbSDaniel Jurgens 		write_lock(&roce->netdev_lock);
174dce45af5SLinus Torvalds 		if (ndev->dev.parent == mdev->device)
175842a9c83SOr Gerlitz 			roce->netdev = ndev;
1767fd8aefbSDaniel Jurgens 		write_unlock(&roce->netdev_lock);
1775ec8c83eSAviv Heller 		break;
1785ec8c83eSAviv Heller 
179842a9c83SOr Gerlitz 	case NETDEV_UNREGISTER:
18035b0aa67SMark Bloch 		/* In case of reps, ib device goes away before the netdevs */
181842a9c83SOr Gerlitz 		write_lock(&roce->netdev_lock);
182842a9c83SOr Gerlitz 		if (roce->netdev == ndev)
183842a9c83SOr Gerlitz 			roce->netdev = NULL;
184842a9c83SOr Gerlitz 		write_unlock(&roce->netdev_lock);
185842a9c83SOr Gerlitz 		break;
186842a9c83SOr Gerlitz 
187fd65f1b8SMoni Shoua 	case NETDEV_CHANGE:
1885ec8c83eSAviv Heller 	case NETDEV_UP:
18988621dfeSAviv Heller 	case NETDEV_DOWN: {
1907fd8aefbSDaniel Jurgens 		struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
19188621dfeSAviv Heller 		struct net_device *upper = NULL;
19288621dfeSAviv Heller 
19388621dfeSAviv Heller 		if (lag_ndev) {
19488621dfeSAviv Heller 			upper = netdev_master_upper_dev_get(lag_ndev);
19588621dfeSAviv Heller 			dev_put(lag_ndev);
19688621dfeSAviv Heller 		}
19788621dfeSAviv Heller 
19835b0aa67SMark Bloch 		if (ibdev->is_rep)
19935b0aa67SMark Bloch 			roce = mlx5_get_rep_roce(ibdev, ndev, &port_num);
20035b0aa67SMark Bloch 		if (!roce)
20135b0aa67SMark Bloch 			return NOTIFY_DONE;
2027fd8aefbSDaniel Jurgens 		if ((upper == ndev || (!upper && ndev == roce->netdev))
20388621dfeSAviv Heller 		    && ibdev->ib_active) {
204626bc02dSBart Van Assche 			struct ib_event ibev = { };
205fd65f1b8SMoni Shoua 			enum ib_port_state port_state;
2065ec8c83eSAviv Heller 
2077fd8aefbSDaniel Jurgens 			if (get_port_state(&ibdev->ib_dev, port_num,
2087fd8aefbSDaniel Jurgens 					   &port_state))
2097fd8aefbSDaniel Jurgens 				goto done;
210fd65f1b8SMoni Shoua 
2117fd8aefbSDaniel Jurgens 			if (roce->last_port_state == port_state)
2127fd8aefbSDaniel Jurgens 				goto done;
213fd65f1b8SMoni Shoua 
2147fd8aefbSDaniel Jurgens 			roce->last_port_state = port_state;
2155ec8c83eSAviv Heller 			ibev.device = &ibdev->ib_dev;
216fd65f1b8SMoni Shoua 			if (port_state == IB_PORT_DOWN)
217fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ERR;
218fd65f1b8SMoni Shoua 			else if (port_state == IB_PORT_ACTIVE)
219fd65f1b8SMoni Shoua 				ibev.event = IB_EVENT_PORT_ACTIVE;
220fd65f1b8SMoni Shoua 			else
2217fd8aefbSDaniel Jurgens 				goto done;
222fd65f1b8SMoni Shoua 
2237fd8aefbSDaniel Jurgens 			ibev.element.port_num = port_num;
2245ec8c83eSAviv Heller 			ib_dispatch_event(&ibev);
2255ec8c83eSAviv Heller 		}
2265ec8c83eSAviv Heller 		break;
22788621dfeSAviv Heller 	}
2285ec8c83eSAviv Heller 
2295ec8c83eSAviv Heller 	default:
2305ec8c83eSAviv Heller 		break;
2315ec8c83eSAviv Heller 	}
2327fd8aefbSDaniel Jurgens done:
23332f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
234fc24fc5eSAchiad Shochat 	return NOTIFY_DONE;
235fc24fc5eSAchiad Shochat }
236fc24fc5eSAchiad Shochat 
237fc24fc5eSAchiad Shochat static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
238fc24fc5eSAchiad Shochat 					     u8 port_num)
239fc24fc5eSAchiad Shochat {
240fc24fc5eSAchiad Shochat 	struct mlx5_ib_dev *ibdev = to_mdev(device);
241fc24fc5eSAchiad Shochat 	struct net_device *ndev;
24232f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev;
243fc24fc5eSAchiad Shochat 
24432f69e4bSDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
24532f69e4bSDaniel Jurgens 	if (!mdev)
24632f69e4bSDaniel Jurgens 		return NULL;
24732f69e4bSDaniel Jurgens 
24832f69e4bSDaniel Jurgens 	ndev = mlx5_lag_get_roce_netdev(mdev);
24988621dfeSAviv Heller 	if (ndev)
25032f69e4bSDaniel Jurgens 		goto out;
25188621dfeSAviv Heller 
252fc24fc5eSAchiad Shochat 	/* Ensure ndev does not disappear before we invoke dev_hold()
253fc24fc5eSAchiad Shochat 	 */
25495579e78SMark Bloch 	read_lock(&ibdev->port[port_num - 1].roce.netdev_lock);
25595579e78SMark Bloch 	ndev = ibdev->port[port_num - 1].roce.netdev;
256fc24fc5eSAchiad Shochat 	if (ndev)
257fc24fc5eSAchiad Shochat 		dev_hold(ndev);
25895579e78SMark Bloch 	read_unlock(&ibdev->port[port_num - 1].roce.netdev_lock);
259fc24fc5eSAchiad Shochat 
26032f69e4bSDaniel Jurgens out:
26132f69e4bSDaniel Jurgens 	mlx5_ib_put_native_port_mdev(ibdev, port_num);
262fc24fc5eSAchiad Shochat 	return ndev;
263fc24fc5eSAchiad Shochat }
264fc24fc5eSAchiad Shochat 
26532f69e4bSDaniel Jurgens struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
26632f69e4bSDaniel Jurgens 						   u8 ib_port_num,
26732f69e4bSDaniel Jurgens 						   u8 *native_port_num)
26832f69e4bSDaniel Jurgens {
26932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
27032f69e4bSDaniel Jurgens 							  ib_port_num);
27132f69e4bSDaniel Jurgens 	struct mlx5_core_dev *mdev = NULL;
27232f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
27332f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
27432f69e4bSDaniel Jurgens 
275210b1f78SMark Bloch 	if (!mlx5_core_mp_enabled(ibdev->mdev) ||
276210b1f78SMark Bloch 	    ll != IB_LINK_LAYER_ETHERNET) {
277210b1f78SMark Bloch 		if (native_port_num)
278210b1f78SMark Bloch 			*native_port_num = ib_port_num;
279210b1f78SMark Bloch 		return ibdev->mdev;
280210b1f78SMark Bloch 	}
281210b1f78SMark Bloch 
28232f69e4bSDaniel Jurgens 	if (native_port_num)
28332f69e4bSDaniel Jurgens 		*native_port_num = 1;
28432f69e4bSDaniel Jurgens 
28532f69e4bSDaniel Jurgens 	port = &ibdev->port[ib_port_num - 1];
28632f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
28732f69e4bSDaniel Jurgens 	mpi = ibdev->port[ib_port_num - 1].mp.mpi;
28832f69e4bSDaniel Jurgens 	if (mpi && !mpi->unaffiliate) {
28932f69e4bSDaniel Jurgens 		mdev = mpi->mdev;
29032f69e4bSDaniel Jurgens 		/* If it's the master no need to refcount, it'll exist
29132f69e4bSDaniel Jurgens 		 * as long as the ib_dev exists.
29232f69e4bSDaniel Jurgens 		 */
29332f69e4bSDaniel Jurgens 		if (!mpi->is_master)
29432f69e4bSDaniel Jurgens 			mpi->mdev_refcnt++;
29532f69e4bSDaniel Jurgens 	}
29632f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
29732f69e4bSDaniel Jurgens 
29832f69e4bSDaniel Jurgens 	return mdev;
29932f69e4bSDaniel Jurgens }
30032f69e4bSDaniel Jurgens 
30132f69e4bSDaniel Jurgens void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
30232f69e4bSDaniel Jurgens {
30332f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
30432f69e4bSDaniel Jurgens 							  port_num);
30532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
30632f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port;
30732f69e4bSDaniel Jurgens 
30832f69e4bSDaniel Jurgens 	if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
30932f69e4bSDaniel Jurgens 		return;
31032f69e4bSDaniel Jurgens 
31132f69e4bSDaniel Jurgens 	port = &ibdev->port[port_num - 1];
31232f69e4bSDaniel Jurgens 
31332f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
31432f69e4bSDaniel Jurgens 	mpi = ibdev->port[port_num - 1].mp.mpi;
31532f69e4bSDaniel Jurgens 	if (mpi->is_master)
31632f69e4bSDaniel Jurgens 		goto out;
31732f69e4bSDaniel Jurgens 
31832f69e4bSDaniel Jurgens 	mpi->mdev_refcnt--;
31932f69e4bSDaniel Jurgens 	if (mpi->unaffiliate)
32032f69e4bSDaniel Jurgens 		complete(&mpi->unref_comp);
32132f69e4bSDaniel Jurgens out:
32232f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
32332f69e4bSDaniel Jurgens }
32432f69e4bSDaniel Jurgens 
325639bf441SAharon Landau static int translate_eth_legacy_proto_oper(u32 eth_proto_oper,
326639bf441SAharon Landau 					   u16 *active_speed, u8 *active_width)
327f1b65df5SNoa Osherovich {
328f1b65df5SNoa Osherovich 	switch (eth_proto_oper) {
329f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
330f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
331f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
332f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
333f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
334f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_SDR;
335f1b65df5SNoa Osherovich 		break;
336f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
337f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
338f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
339f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
340f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
341f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
342f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
343f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
344f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
345f1b65df5SNoa Osherovich 		break;
346f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
347f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
348f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
349f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
350f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
351f1b65df5SNoa Osherovich 		break;
352f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
353f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
354f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
355f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
356f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
357f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_QDR;
358f1b65df5SNoa Osherovich 		break;
359f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
360f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
361f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
362f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_1X;
363f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_HDR;
364f1b65df5SNoa Osherovich 		break;
365f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
366f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
367f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_FDR;
368f1b65df5SNoa Osherovich 		break;
369f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
370f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
371f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
372f1b65df5SNoa Osherovich 	case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
373f1b65df5SNoa Osherovich 		*active_width = IB_WIDTH_4X;
374f1b65df5SNoa Osherovich 		*active_speed = IB_SPEED_EDR;
375f1b65df5SNoa Osherovich 		break;
376f1b65df5SNoa Osherovich 	default:
377f1b65df5SNoa Osherovich 		return -EINVAL;
378f1b65df5SNoa Osherovich 	}
379f1b65df5SNoa Osherovich 
380f1b65df5SNoa Osherovich 	return 0;
381f1b65df5SNoa Osherovich }
382f1b65df5SNoa Osherovich 
383639bf441SAharon Landau static int translate_eth_ext_proto_oper(u32 eth_proto_oper, u16 *active_speed,
38408e8676fSAya Levin 					u8 *active_width)
38508e8676fSAya Levin {
38608e8676fSAya Levin 	switch (eth_proto_oper) {
38708e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_SGMII_100M):
38808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_1000BASE_X_SGMII):
38908e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39008e8676fSAya Levin 		*active_speed = IB_SPEED_SDR;
39108e8676fSAya Levin 		break;
39208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_5GBASE_R):
39308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39408e8676fSAya Levin 		*active_speed = IB_SPEED_DDR;
39508e8676fSAya Levin 		break;
39608e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_10GBASE_XFI_XAUI_1):
39708e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
39808e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
39908e8676fSAya Levin 		break;
40008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_40GBASE_XLAUI_4_XLPPI_4):
40108e8676fSAya Levin 		*active_width = IB_WIDTH_4X;
40208e8676fSAya Levin 		*active_speed = IB_SPEED_QDR;
40308e8676fSAya Levin 		break;
40408e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_25GAUI_1_25GBASE_CR_KR):
40508e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
40608e8676fSAya Levin 		*active_speed = IB_SPEED_EDR;
40708e8676fSAya Levin 		break;
40808e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_2_LAUI_2_50GBASE_CR2_KR2):
409cd272875SAya Levin 		*active_width = IB_WIDTH_2X;
410cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
411cd272875SAya Levin 		break;
41208e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_50GAUI_1_LAUI_1_50GBASE_CR_KR):
41308e8676fSAya Levin 		*active_width = IB_WIDTH_1X;
41408e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
41508e8676fSAya Levin 		break;
416cd272875SAya Levin 	case MLX5E_PROT_MASK(MLX5E_CAUI_4_100GBASE_CR4_KR4):
417cd272875SAya Levin 		*active_width = IB_WIDTH_4X;
418cd272875SAya Levin 		*active_speed = IB_SPEED_EDR;
419cd272875SAya Levin 		break;
42008e8676fSAya Levin 	case MLX5E_PROT_MASK(MLX5E_100GAUI_2_100GBASE_CR2_KR2):
42108e8676fSAya Levin 		*active_width = IB_WIDTH_2X;
42208e8676fSAya Levin 		*active_speed = IB_SPEED_HDR;
42308e8676fSAya Levin 		break;
424f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_100GAUI_1_100GBASE_CR_KR):
425f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_1X;
426f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
427f946e45fSMeir Lichtinger 		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;
432f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_200GAUI_2_200GBASE_CR2_KR2):
433f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_2X;
434f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
435f946e45fSMeir Lichtinger 		break;
436f946e45fSMeir Lichtinger 	case MLX5E_PROT_MASK(MLX5E_400GAUI_4_400GBASE_CR4_KR4):
437f946e45fSMeir Lichtinger 		*active_width = IB_WIDTH_4X;
438f946e45fSMeir Lichtinger 		*active_speed = IB_SPEED_NDR;
439f946e45fSMeir Lichtinger 		break;
44008e8676fSAya Levin 	default:
44108e8676fSAya Levin 		return -EINVAL;
44208e8676fSAya Levin 	}
44308e8676fSAya Levin 
44408e8676fSAya Levin 	return 0;
44508e8676fSAya Levin }
44608e8676fSAya Levin 
447639bf441SAharon Landau static int translate_eth_proto_oper(u32 eth_proto_oper, u16 *active_speed,
44808e8676fSAya Levin 				    u8 *active_width, bool ext)
44908e8676fSAya Levin {
45008e8676fSAya Levin 	return ext ?
45108e8676fSAya Levin 		translate_eth_ext_proto_oper(eth_proto_oper, active_speed,
45208e8676fSAya Levin 					     active_width) :
45308e8676fSAya Levin 		translate_eth_legacy_proto_oper(eth_proto_oper, active_speed,
45408e8676fSAya Levin 						active_width);
45508e8676fSAya Levin }
45608e8676fSAya Levin 
457095b0927SIlan Tayari static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
4583f89a643SAchiad Shochat 				struct ib_port_attr *props)
4593f89a643SAchiad Shochat {
4603f89a643SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(device);
461bc4e12ffSAya Levin 	u32 out[MLX5_ST_SZ_DW(ptys_reg)] = {0};
462da005f9fSColin Ian King 	struct mlx5_core_dev *mdev;
46388621dfeSAviv Heller 	struct net_device *ndev, *upper;
4643f89a643SAchiad Shochat 	enum ib_mtu ndev_ib_mtu;
465b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
466f1b65df5SNoa Osherovich 	u32 eth_prot_oper;
467b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
46808e8676fSAya Levin 	bool ext;
469095b0927SIlan Tayari 	int err;
4703f89a643SAchiad Shochat 
471b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
472b3cbd6f0SDaniel Jurgens 	if (!mdev) {
473b3cbd6f0SDaniel Jurgens 		/* This means the port isn't affiliated yet. Get the
474b3cbd6f0SDaniel Jurgens 		 * info for the master port instead.
475b3cbd6f0SDaniel Jurgens 		 */
476b3cbd6f0SDaniel Jurgens 		put_mdev = false;
477b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
478b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
479b3cbd6f0SDaniel Jurgens 		port_num = 1;
480b3cbd6f0SDaniel Jurgens 	}
481b3cbd6f0SDaniel Jurgens 
482f1b65df5SNoa Osherovich 	/* Possible bad flows are checked before filling out props so in case
483f1b65df5SNoa Osherovich 	 * of an error it will still be zeroed out.
48426628e2dSMark Bloch 	 * Use native port in case of reps
48550f22fd8SNoa Osherovich 	 */
48626628e2dSMark Bloch 	if (dev->is_rep)
48726628e2dSMark Bloch 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
48826628e2dSMark Bloch 					   1);
48926628e2dSMark Bloch 	else
490bc4e12ffSAya Levin 		err = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN,
491b3cbd6f0SDaniel Jurgens 					   mdev_port_num);
492095b0927SIlan Tayari 	if (err)
493b3cbd6f0SDaniel Jurgens 		goto out;
494530c8632SAya Levin 	ext = !!MLX5_GET_ETH_PROTO(ptys_reg, out, true, eth_proto_capability);
49508e8676fSAya Levin 	eth_prot_oper = MLX5_GET_ETH_PROTO(ptys_reg, out, ext, eth_proto_oper);
496f1b65df5SNoa Osherovich 
4977672ed33SHonggang Li 	props->active_width     = IB_WIDTH_4X;
4987672ed33SHonggang Li 	props->active_speed     = IB_SPEED_QDR;
4997672ed33SHonggang Li 
500f1b65df5SNoa Osherovich 	translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
50108e8676fSAya Levin 				 &props->active_width, ext);
5023f89a643SAchiad Shochat 
5037852546fSMaor Gottlieb 	if (!dev->is_rep && dev->mdev->roce.roce_en) {
5047a58779eSParav Pandit 		u16 qkey_viol_cntr;
5057a58779eSParav Pandit 
5063f89a643SAchiad Shochat 		props->port_cap_flags |= IB_PORT_CM_SUP;
5072f944c0fSJason Gunthorpe 		props->ip_gids = true;
5083f89a643SAchiad Shochat 		props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
5093f89a643SAchiad Shochat 						   roce_address_table_size);
5107a58779eSParav Pandit 		mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
5117a58779eSParav Pandit 		props->qkey_viol_cntr = qkey_viol_cntr;
5127a58779eSParav Pandit 	}
5133f89a643SAchiad Shochat 	props->max_mtu          = IB_MTU_4096;
5143f89a643SAchiad Shochat 	props->max_msg_sz       = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
5153f89a643SAchiad Shochat 	props->pkey_tbl_len     = 1;
5163f89a643SAchiad Shochat 	props->state            = IB_PORT_DOWN;
51772a7720fSKamal Heib 	props->phys_state       = IB_PORT_PHYS_STATE_DISABLED;
5183f89a643SAchiad Shochat 
519b3cbd6f0SDaniel Jurgens 	/* If this is a stub query for an unaffiliated port stop here */
520b3cbd6f0SDaniel Jurgens 	if (!put_mdev)
521b3cbd6f0SDaniel Jurgens 		goto out;
522b3cbd6f0SDaniel Jurgens 
5233f89a643SAchiad Shochat 	ndev = mlx5_ib_get_netdev(device, port_num);
5243f89a643SAchiad Shochat 	if (!ndev)
525b3cbd6f0SDaniel Jurgens 		goto out;
5263f89a643SAchiad Shochat 
5277c34ec19SAviv Heller 	if (dev->lag_active) {
52888621dfeSAviv Heller 		rcu_read_lock();
52988621dfeSAviv Heller 		upper = netdev_master_upper_dev_get_rcu(ndev);
53088621dfeSAviv Heller 		if (upper) {
53188621dfeSAviv Heller 			dev_put(ndev);
53288621dfeSAviv Heller 			ndev = upper;
53388621dfeSAviv Heller 			dev_hold(ndev);
53488621dfeSAviv Heller 		}
53588621dfeSAviv Heller 		rcu_read_unlock();
53688621dfeSAviv Heller 	}
53788621dfeSAviv Heller 
5383f89a643SAchiad Shochat 	if (netif_running(ndev) && netif_carrier_ok(ndev)) {
5393f89a643SAchiad Shochat 		props->state      = IB_PORT_ACTIVE;
54072a7720fSKamal Heib 		props->phys_state = IB_PORT_PHYS_STATE_LINK_UP;
5413f89a643SAchiad Shochat 	}
5423f89a643SAchiad Shochat 
5433f89a643SAchiad Shochat 	ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
5443f89a643SAchiad Shochat 
5453f89a643SAchiad Shochat 	dev_put(ndev);
5463f89a643SAchiad Shochat 
5473f89a643SAchiad Shochat 	props->active_mtu	= min(props->max_mtu, ndev_ib_mtu);
548b3cbd6f0SDaniel Jurgens out:
549b3cbd6f0SDaniel Jurgens 	if (put_mdev)
550b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port_num);
551b3cbd6f0SDaniel Jurgens 	return err;
5523f89a643SAchiad Shochat }
5533f89a643SAchiad Shochat 
554095b0927SIlan Tayari static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
555095b0927SIlan Tayari 			 unsigned int index, const union ib_gid *gid,
556095b0927SIlan Tayari 			 const struct ib_gid_attr *attr)
5573cca2606SAchiad Shochat {
5581c15b4f2SAvihai Horon 	enum ib_gid_type gid_type = IB_GID_TYPE_ROCE;
559a70c0739SParav Pandit 	u16 vlan_id = 0xffff;
560095b0927SIlan Tayari 	u8 roce_version = 0;
561095b0927SIlan Tayari 	u8 roce_l3_type = 0;
562095b0927SIlan Tayari 	u8 mac[ETH_ALEN];
563a70c0739SParav Pandit 	int ret;
5643cca2606SAchiad Shochat 
565095b0927SIlan Tayari 	if (gid) {
566095b0927SIlan Tayari 		gid_type = attr->gid_type;
567a70c0739SParav Pandit 		ret = rdma_read_gid_l2_fields(attr, &vlan_id, &mac[0]);
568a70c0739SParav Pandit 		if (ret)
569a70c0739SParav Pandit 			return ret;
5703cca2606SAchiad Shochat 	}
5713cca2606SAchiad Shochat 
572095b0927SIlan Tayari 	switch (gid_type) {
5731c15b4f2SAvihai Horon 	case IB_GID_TYPE_ROCE:
574095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_1;
5753cca2606SAchiad Shochat 		break;
5763cca2606SAchiad Shochat 	case IB_GID_TYPE_ROCE_UDP_ENCAP:
577095b0927SIlan Tayari 		roce_version = MLX5_ROCE_VERSION_2;
578095b0927SIlan Tayari 		if (ipv6_addr_v4mapped((void *)gid))
579095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
580095b0927SIlan Tayari 		else
581095b0927SIlan Tayari 			roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
5823cca2606SAchiad Shochat 		break;
5833cca2606SAchiad Shochat 
5843cca2606SAchiad Shochat 	default:
585095b0927SIlan Tayari 		mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
5863cca2606SAchiad Shochat 	}
5873cca2606SAchiad Shochat 
588095b0927SIlan Tayari 	return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
589cf34e1feSParav Pandit 				      roce_l3_type, gid->raw, mac,
590a70c0739SParav Pandit 				      vlan_id < VLAN_CFI_MASK, vlan_id,
591cf34e1feSParav Pandit 				      port_num);
5923cca2606SAchiad Shochat }
5933cca2606SAchiad Shochat 
594f4df9a7cSParav Pandit static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
5953cca2606SAchiad Shochat 			   __always_unused void **context)
5963cca2606SAchiad Shochat {
597414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
598f4df9a7cSParav Pandit 			     attr->index, &attr->gid, attr);
5993cca2606SAchiad Shochat }
6003cca2606SAchiad Shochat 
601414448d2SParav Pandit static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
602414448d2SParav Pandit 			   __always_unused void **context)
6033cca2606SAchiad Shochat {
604414448d2SParav Pandit 	return set_roce_addr(to_mdev(attr->device), attr->port_num,
605414448d2SParav Pandit 			     attr->index, NULL, NULL);
6063cca2606SAchiad Shochat }
6073cca2606SAchiad Shochat 
6085ac55dfcSMark Zhang __be16 mlx5_get_roce_udp_sport_min(const struct mlx5_ib_dev *dev,
60947ec3866SParav Pandit 				   const struct ib_gid_attr *attr)
6102811ba51SAchiad Shochat {
61147ec3866SParav Pandit 	if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
6122811ba51SAchiad Shochat 		return 0;
6132811ba51SAchiad Shochat 
6142811ba51SAchiad Shochat 	return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
6152811ba51SAchiad Shochat }
6162811ba51SAchiad Shochat 
6171b5daf11SMajd Dibbiny static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
6181b5daf11SMajd Dibbiny {
6197fae6655SNoa Osherovich 	if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
620d603c809SEli Cohen 		return !MLX5_CAP_GEN(dev->mdev, ib_virt);
6217fae6655SNoa Osherovich 	return 0;
6221b5daf11SMajd Dibbiny }
6231b5daf11SMajd Dibbiny 
6241b5daf11SMajd Dibbiny enum {
6251b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_MAD,
6261b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_HCA,
6271b5daf11SMajd Dibbiny 	MLX5_VPORT_ACCESS_METHOD_NIC,
6281b5daf11SMajd Dibbiny };
6291b5daf11SMajd Dibbiny 
6301b5daf11SMajd Dibbiny static int mlx5_get_vport_access_method(struct ib_device *ibdev)
6311b5daf11SMajd Dibbiny {
6321b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(to_mdev(ibdev)))
6331b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_MAD;
6341b5daf11SMajd Dibbiny 
635ebd61f68SAchiad Shochat 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
6361b5daf11SMajd Dibbiny 	    IB_LINK_LAYER_ETHERNET)
6371b5daf11SMajd Dibbiny 		return MLX5_VPORT_ACCESS_METHOD_NIC;
6381b5daf11SMajd Dibbiny 
6391b5daf11SMajd Dibbiny 	return MLX5_VPORT_ACCESS_METHOD_HCA;
6401b5daf11SMajd Dibbiny }
6411b5daf11SMajd Dibbiny 
642da7525d2SEran Ben Elisha static void get_atomic_caps(struct mlx5_ib_dev *dev,
643776a3906SMoni Shoua 			    u8 atomic_size_qp,
644da7525d2SEran Ben Elisha 			    struct ib_device_attr *props)
645da7525d2SEran Ben Elisha {
646da7525d2SEran Ben Elisha 	u8 tmp;
647da7525d2SEran Ben Elisha 	u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
648da7525d2SEran Ben Elisha 	u8 atomic_req_8B_endianness_mode =
649bd10838aSOr Gerlitz 		MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
650da7525d2SEran Ben Elisha 
651da7525d2SEran Ben Elisha 	/* Check if HW supports 8 bytes standard atomic operations and capable
652da7525d2SEran Ben Elisha 	 * of host endianness respond
653da7525d2SEran Ben Elisha 	 */
654da7525d2SEran Ben Elisha 	tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
655da7525d2SEran Ben Elisha 	if (((atomic_operations & tmp) == tmp) &&
656da7525d2SEran Ben Elisha 	    (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
657da7525d2SEran Ben Elisha 	    (atomic_req_8B_endianness_mode)) {
658da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_HCA;
659da7525d2SEran Ben Elisha 	} else {
660da7525d2SEran Ben Elisha 		props->atomic_cap = IB_ATOMIC_NONE;
661da7525d2SEran Ben Elisha 	}
662da7525d2SEran Ben Elisha }
663da7525d2SEran Ben Elisha 
664776a3906SMoni Shoua static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
665776a3906SMoni Shoua 			       struct ib_device_attr *props)
666776a3906SMoni Shoua {
667776a3906SMoni Shoua 	u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
668776a3906SMoni Shoua 
669776a3906SMoni Shoua 	get_atomic_caps(dev, atomic_size_qp, props);
670776a3906SMoni Shoua }
671776a3906SMoni Shoua 
6721b5daf11SMajd Dibbiny static int mlx5_query_system_image_guid(struct ib_device *ibdev,
6731b5daf11SMajd Dibbiny 					__be64 *sys_image_guid)
6741b5daf11SMajd Dibbiny {
6751b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
6761b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
6771b5daf11SMajd Dibbiny 	u64 tmp;
6781b5daf11SMajd Dibbiny 	int err;
6791b5daf11SMajd Dibbiny 
6801b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
6811b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
6821b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_system_image_guid(ibdev,
6831b5daf11SMajd Dibbiny 							    sys_image_guid);
6841b5daf11SMajd Dibbiny 
6851b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
6861b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
6873f89a643SAchiad Shochat 		break;
6883f89a643SAchiad Shochat 
6893f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
6903f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
6913f89a643SAchiad Shochat 		break;
6921b5daf11SMajd Dibbiny 
6931b5daf11SMajd Dibbiny 	default:
6941b5daf11SMajd Dibbiny 		return -EINVAL;
6951b5daf11SMajd Dibbiny 	}
6963f89a643SAchiad Shochat 
6973f89a643SAchiad Shochat 	if (!err)
6983f89a643SAchiad Shochat 		*sys_image_guid = cpu_to_be64(tmp);
6993f89a643SAchiad Shochat 
7003f89a643SAchiad Shochat 	return err;
7013f89a643SAchiad Shochat 
7021b5daf11SMajd Dibbiny }
7031b5daf11SMajd Dibbiny 
7041b5daf11SMajd Dibbiny static int mlx5_query_max_pkeys(struct ib_device *ibdev,
7051b5daf11SMajd Dibbiny 				u16 *max_pkeys)
7061b5daf11SMajd Dibbiny {
7071b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7081b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
7091b5daf11SMajd Dibbiny 
7101b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7111b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7121b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
7131b5daf11SMajd Dibbiny 
7141b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7151b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7161b5daf11SMajd Dibbiny 		*max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
7171b5daf11SMajd Dibbiny 						pkey_table_size));
7181b5daf11SMajd Dibbiny 		return 0;
7191b5daf11SMajd Dibbiny 
7201b5daf11SMajd Dibbiny 	default:
7211b5daf11SMajd Dibbiny 		return -EINVAL;
7221b5daf11SMajd Dibbiny 	}
7231b5daf11SMajd Dibbiny }
7241b5daf11SMajd Dibbiny 
7251b5daf11SMajd Dibbiny static int mlx5_query_vendor_id(struct ib_device *ibdev,
7261b5daf11SMajd Dibbiny 				u32 *vendor_id)
7271b5daf11SMajd Dibbiny {
7281b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
7291b5daf11SMajd Dibbiny 
7301b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
7311b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7321b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
7331b5daf11SMajd Dibbiny 
7341b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7351b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7361b5daf11SMajd Dibbiny 		return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
7371b5daf11SMajd Dibbiny 
7381b5daf11SMajd Dibbiny 	default:
7391b5daf11SMajd Dibbiny 		return -EINVAL;
7401b5daf11SMajd Dibbiny 	}
7411b5daf11SMajd Dibbiny }
7421b5daf11SMajd Dibbiny 
7431b5daf11SMajd Dibbiny static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
7441b5daf11SMajd Dibbiny 				__be64 *node_guid)
7451b5daf11SMajd Dibbiny {
7461b5daf11SMajd Dibbiny 	u64 tmp;
7471b5daf11SMajd Dibbiny 	int err;
7481b5daf11SMajd Dibbiny 
7491b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
7501b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
7511b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_guid(dev, node_guid);
7521b5daf11SMajd Dibbiny 
7531b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
7541b5daf11SMajd Dibbiny 		err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
7553f89a643SAchiad Shochat 		break;
7563f89a643SAchiad Shochat 
7573f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
7583f89a643SAchiad Shochat 		err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
7593f89a643SAchiad Shochat 		break;
7601b5daf11SMajd Dibbiny 
7611b5daf11SMajd Dibbiny 	default:
7621b5daf11SMajd Dibbiny 		return -EINVAL;
7631b5daf11SMajd Dibbiny 	}
7643f89a643SAchiad Shochat 
7653f89a643SAchiad Shochat 	if (!err)
7663f89a643SAchiad Shochat 		*node_guid = cpu_to_be64(tmp);
7673f89a643SAchiad Shochat 
7683f89a643SAchiad Shochat 	return err;
7691b5daf11SMajd Dibbiny }
7701b5daf11SMajd Dibbiny 
7711b5daf11SMajd Dibbiny struct mlx5_reg_node_desc {
772bd99fdeaSYuval Shaia 	u8	desc[IB_DEVICE_NODE_DESC_MAX];
7731b5daf11SMajd Dibbiny };
7741b5daf11SMajd Dibbiny 
7751b5daf11SMajd Dibbiny static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
7761b5daf11SMajd Dibbiny {
7771b5daf11SMajd Dibbiny 	struct mlx5_reg_node_desc in;
7781b5daf11SMajd Dibbiny 
7791b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
7801b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_node_desc(dev, node_desc);
7811b5daf11SMajd Dibbiny 
7821b5daf11SMajd Dibbiny 	memset(&in, 0, sizeof(in));
7831b5daf11SMajd Dibbiny 
7841b5daf11SMajd Dibbiny 	return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
7851b5daf11SMajd Dibbiny 				    sizeof(struct mlx5_reg_node_desc),
7861b5daf11SMajd Dibbiny 				    MLX5_REG_NODE_DESC, 0, 0);
7871b5daf11SMajd Dibbiny }
7881b5daf11SMajd Dibbiny 
789e126ba97SEli Cohen static int mlx5_ib_query_device(struct ib_device *ibdev,
7902528e33eSMatan Barak 				struct ib_device_attr *props,
7912528e33eSMatan Barak 				struct ib_udata *uhw)
792e126ba97SEli Cohen {
79348357091SLeon Romanovsky 	size_t uhw_outlen = (uhw) ? uhw->outlen : 0;
794e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
795938fe83cSSaeed Mahameed 	struct mlx5_core_dev *mdev = dev->mdev;
796e126ba97SEli Cohen 	int err = -ENOMEM;
797288c01b7SEli Cohen 	int max_sq_desc;
798e126ba97SEli Cohen 	int max_rq_sg;
799e126ba97SEli Cohen 	int max_sq_sg;
800e0238a6aSSagi Grimberg 	u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
80185c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(mdev);
802402ca536SBodong Wang 	struct mlx5_ib_query_device_resp resp = {};
803402ca536SBodong Wang 	size_t resp_len;
804402ca536SBodong Wang 	u64 max_tso;
805e126ba97SEli Cohen 
806402ca536SBodong Wang 	resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
80748357091SLeon Romanovsky 	if (uhw_outlen && uhw_outlen < resp_len)
808402ca536SBodong Wang 		return -EINVAL;
8096f26b2acSErez Alfasi 
810402ca536SBodong Wang 	resp.response_length = resp_len;
811402ca536SBodong Wang 
81248357091SLeon Romanovsky 	if (uhw && uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
8132528e33eSMatan Barak 		return -EINVAL;
8142528e33eSMatan Barak 
815e126ba97SEli Cohen 	memset(props, 0, sizeof(*props));
8161b5daf11SMajd Dibbiny 	err = mlx5_query_system_image_guid(ibdev,
8171b5daf11SMajd Dibbiny 					   &props->sys_image_guid);
8181b5daf11SMajd Dibbiny 	if (err)
8191b5daf11SMajd Dibbiny 		return err;
8201b5daf11SMajd Dibbiny 
8212019d70eSParav Pandit 	props->max_pkeys = dev->pkey_table_len;
8221b5daf11SMajd Dibbiny 
8231b5daf11SMajd Dibbiny 	err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
8241b5daf11SMajd Dibbiny 	if (err)
8251b5daf11SMajd Dibbiny 		return err;
826e126ba97SEli Cohen 
8279603b61dSJack Morgenstein 	props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
8289603b61dSJack Morgenstein 		(fw_rev_min(dev->mdev) << 16) |
8299603b61dSJack Morgenstein 		fw_rev_sub(dev->mdev);
830e126ba97SEli Cohen 	props->device_cap_flags    = IB_DEVICE_CHANGE_PHY_PORT |
831e126ba97SEli Cohen 		IB_DEVICE_PORT_ACTIVE_EVENT		|
832e126ba97SEli Cohen 		IB_DEVICE_SYS_IMAGE_GUID		|
8331a4c3a3dSEli Cohen 		IB_DEVICE_RC_RNR_NAK_GEN;
834938fe83cSSaeed Mahameed 
835938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, pkv))
836e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
837938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, qkv))
838e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
839938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, apm))
840e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
841938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, xrc))
842e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_XRC;
843d2370e0aSMatan Barak 	if (MLX5_CAP_GEN(mdev, imaicl)) {
844d2370e0aSMatan Barak 		props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
845d2370e0aSMatan Barak 					   IB_DEVICE_MEM_WINDOW_TYPE_2B;
846d2370e0aSMatan Barak 		props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
847b005d316SSagi Grimberg 		/* We support 'Gappy' memory registration too */
848b005d316SSagi Grimberg 		props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
849d2370e0aSMatan Barak 	}
8500ec52f01SJason Gunthorpe 	/* IB_WR_REG_MR always requires changing the entity size with UMR */
8510ec52f01SJason Gunthorpe 	if (!MLX5_CAP_GEN(dev->mdev, umr_modify_entity_size_disabled))
852e126ba97SEli Cohen 		props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
853938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, sho)) {
854c0a6cbb9SIsrael Rukshin 		props->device_cap_flags |= IB_DEVICE_INTEGRITY_HANDOVER;
8552dea9094SSagi Grimberg 		/* At this stage no support for signature handover */
8562dea9094SSagi Grimberg 		props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
8572dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_2 |
8582dea9094SSagi Grimberg 				      IB_PROT_T10DIF_TYPE_3;
8592dea9094SSagi Grimberg 		props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
8602dea9094SSagi Grimberg 				       IB_GUARD_T10DIF_CSUM;
8612dea9094SSagi Grimberg 	}
862938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(mdev, block_lb_mc))
863f360d88aSEli Cohen 		props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
864e126ba97SEli Cohen 
86585c7c014SDaniel Jurgens 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
866e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, csum_cap)) {
867e8161334SNoa Osherovich 			/* Legacy bit to support old userspace libraries */
86888115fe7SBodong Wang 			props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
869e8161334SNoa Osherovich 			props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
870e8161334SNoa Osherovich 		}
871e8161334SNoa Osherovich 
872e8161334SNoa Osherovich 		if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
873e8161334SNoa Osherovich 			props->raw_packet_caps |=
874e8161334SNoa Osherovich 				IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
87588115fe7SBodong Wang 
876a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen) {
877402ca536SBodong Wang 			max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
878402ca536SBodong Wang 			if (max_tso) {
879402ca536SBodong Wang 				resp.tso_caps.max_tso = 1 << max_tso;
880402ca536SBodong Wang 				resp.tso_caps.supported_qpts |=
881402ca536SBodong Wang 					1 << IB_QPT_RAW_PACKET;
882402ca536SBodong Wang 				resp.response_length += sizeof(resp.tso_caps);
883402ca536SBodong Wang 			}
884402ca536SBodong Wang 		}
88531f69a82SYishai Hadas 
886a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen) {
88731f69a82SYishai Hadas 			resp.rss_caps.rx_hash_function =
88831f69a82SYishai Hadas 						MLX5_RX_HASH_FUNC_TOEPLITZ;
88931f69a82SYishai Hadas 			resp.rss_caps.rx_hash_fields_mask =
89031f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV4 |
89131f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV4 |
89231f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_IPV6 |
89331f69a82SYishai Hadas 						MLX5_RX_HASH_DST_IPV6 |
89431f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_TCP |
89531f69a82SYishai Hadas 						MLX5_RX_HASH_DST_PORT_TCP |
89631f69a82SYishai Hadas 						MLX5_RX_HASH_SRC_PORT_UDP |
8974e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_DST_PORT_UDP |
8984e2b53a5SMaor Gottlieb 						MLX5_RX_HASH_INNER;
8992d93fc85SMatan Barak 			if (mlx5_accel_ipsec_device_caps(dev->mdev) &
9002d93fc85SMatan Barak 			    MLX5_ACCEL_IPSEC_CAP_DEVICE)
9012d93fc85SMatan Barak 				resp.rss_caps.rx_hash_fields_mask |=
9022d93fc85SMatan Barak 					MLX5_RX_HASH_IPSEC_SPI;
90331f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
90431f69a82SYishai Hadas 		}
90531f69a82SYishai Hadas 	} else {
906a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), tso_caps) <= uhw_outlen)
90731f69a82SYishai Hadas 			resp.response_length += sizeof(resp.tso_caps);
908a762d460SLeon Romanovsky 		if (offsetofend(typeof(resp), rss_caps) <= uhw_outlen)
90931f69a82SYishai Hadas 			resp.response_length += sizeof(resp.rss_caps);
910402ca536SBodong Wang 	}
911402ca536SBodong Wang 
912f0313965SErez Shitrit 	if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
913f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
914f0313965SErez Shitrit 		props->device_cap_flags |= IB_DEVICE_UD_TSO;
915f0313965SErez Shitrit 	}
916f0313965SErez Shitrit 
91703404e8aSMaor Gottlieb 	if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
91885c7c014SDaniel Jurgens 	    MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
91985c7c014SDaniel Jurgens 	    raw_support)
92003404e8aSMaor Gottlieb 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
92103404e8aSMaor Gottlieb 
9221d54f890SYishai Hadas 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
9231d54f890SYishai Hadas 	    MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
9241d54f890SYishai Hadas 		props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
9251d54f890SYishai Hadas 
926cff5a0f3SMajd Dibbiny 	if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
92785c7c014SDaniel Jurgens 	    MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
92885c7c014SDaniel Jurgens 	    raw_support) {
929e8161334SNoa Osherovich 		/* Legacy bit to support old userspace libraries */
930cff5a0f3SMajd Dibbiny 		props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
931e8161334SNoa Osherovich 		props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
932e8161334SNoa Osherovich 	}
933cff5a0f3SMajd Dibbiny 
93424da0016SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic)) {
93524da0016SAriel Levkovich 		props->max_dm_size =
93624da0016SAriel Levkovich 			MLX5_CAP_DEV_MEM(mdev, max_memic_size);
93724da0016SAriel Levkovich 	}
93824da0016SAriel Levkovich 
939da6d6ba3SMaor Gottlieb 	if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
940da6d6ba3SMaor Gottlieb 		props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
941da6d6ba3SMaor Gottlieb 
942b1383aa6SNoa Osherovich 	if (MLX5_CAP_GEN(mdev, end_pad))
943b1383aa6SNoa Osherovich 		props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
944b1383aa6SNoa Osherovich 
9451b5daf11SMajd Dibbiny 	props->vendor_part_id	   = mdev->pdev->device;
9461b5daf11SMajd Dibbiny 	props->hw_ver		   = mdev->pdev->revision;
947e126ba97SEli Cohen 
948e126ba97SEli Cohen 	props->max_mr_size	   = ~0ull;
949e0238a6aSSagi Grimberg 	props->page_size_cap	   = ~(min_page_size - 1);
950938fe83cSSaeed Mahameed 	props->max_qp		   = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
951938fe83cSSaeed Mahameed 	props->max_qp_wr	   = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
952938fe83cSSaeed Mahameed 	max_rq_sg =  MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
953938fe83cSSaeed Mahameed 		     sizeof(struct mlx5_wqe_data_seg);
954288c01b7SEli Cohen 	max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
955288c01b7SEli Cohen 	max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
956288c01b7SEli Cohen 		     sizeof(struct mlx5_wqe_raddr_seg)) /
957e126ba97SEli Cohen 		sizeof(struct mlx5_wqe_data_seg);
95833023fb8SSteve Wise 	props->max_send_sge = max_sq_sg;
95933023fb8SSteve Wise 	props->max_recv_sge = max_rq_sg;
960986ef95eSSagi Grimberg 	props->max_sge_rd	   = MLX5_MAX_SGE_RD;
961938fe83cSSaeed Mahameed 	props->max_cq		   = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
9629f177686SLeon Romanovsky 	props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
963938fe83cSSaeed Mahameed 	props->max_mr		   = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
964938fe83cSSaeed Mahameed 	props->max_pd		   = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
965938fe83cSSaeed Mahameed 	props->max_qp_rd_atom	   = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
966938fe83cSSaeed Mahameed 	props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
967938fe83cSSaeed Mahameed 	props->max_srq		   = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
968938fe83cSSaeed Mahameed 	props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
969938fe83cSSaeed Mahameed 	props->local_ca_ack_delay  = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
970e126ba97SEli Cohen 	props->max_res_rd_atom	   = props->max_qp_rd_atom * props->max_qp;
971e126ba97SEli Cohen 	props->max_srq_sge	   = max_rq_sg - 1;
972911f4331SSagi Grimberg 	props->max_fast_reg_page_list_len =
973911f4331SSagi Grimberg 		1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
97462e3c379SMax Gurtovoy 	props->max_pi_fast_reg_page_list_len =
97562e3c379SMax Gurtovoy 		props->max_fast_reg_page_list_len / 2;
97636609056SYamin Friedman 	props->max_sgl_rd =
97736609056SYamin Friedman 		MLX5_CAP_GEN(mdev, max_sgl_for_optimized_performance);
978776a3906SMoni Shoua 	get_atomic_caps_qp(dev, props);
97981bea28fSEli Cohen 	props->masked_atomic_cap   = IB_ATOMIC_NONE;
980938fe83cSSaeed Mahameed 	props->max_mcast_grp	   = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
981938fe83cSSaeed Mahameed 	props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
982e126ba97SEli Cohen 	props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
983e126ba97SEli Cohen 					   props->max_mcast_grp;
98486695a65SMaor Gottlieb 	props->max_ah = INT_MAX;
9857c60bcbbSMatan Barak 	props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
9867c60bcbbSMatan Barak 	props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
987e126ba97SEli Cohen 
988e502b8b0SLeon Romanovsky 	if (IS_ENABLED(CONFIG_INFINIBAND_ON_DEMAND_PAGING)) {
98900815752SMoni Shoua 		if (dev->odp_caps.general_caps & IB_ODP_SUPPORT)
9908cdd312cSHaggai Eran 			props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
9918cdd312cSHaggai Eran 		props->odp_caps = dev->odp_caps;
992a73a8955SMoni Shoua 		if (!uhw) {
993a73a8955SMoni Shoua 			/* ODP for kernel QPs is not implemented for receive
994a73a8955SMoni Shoua 			 * WQEs and SRQ WQEs
995a73a8955SMoni Shoua 			 */
996a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.rc_odp_caps &=
997a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
998a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
999a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.uc_odp_caps &=
1000a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1001a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1002a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.ud_odp_caps &=
1003a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1004a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1005a73a8955SMoni Shoua 			props->odp_caps.per_transport_caps.xrc_odp_caps &=
1006a73a8955SMoni Shoua 				~(IB_ODP_SUPPORT_READ |
1007a73a8955SMoni Shoua 				  IB_ODP_SUPPORT_SRQ_RECV);
1008a73a8955SMoni Shoua 		}
1009e502b8b0SLeon Romanovsky 	}
10108cdd312cSHaggai Eran 
1011051f2630SLeon Romanovsky 	if (MLX5_CAP_GEN(mdev, cd))
1012051f2630SLeon Romanovsky 		props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
1013051f2630SLeon Romanovsky 
1014e53a9d26SParav Pandit 	if (mlx5_core_is_vf(mdev))
1015eff901d3SEli Cohen 		props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
1016eff901d3SEli Cohen 
101731f69a82SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) ==
101885c7c014SDaniel Jurgens 	    IB_LINK_LAYER_ETHERNET && raw_support) {
101931f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_tables =
102031f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
102131f69a82SYishai Hadas 		props->rss_caps.max_rwq_indirection_table_size =
102231f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
102331f69a82SYishai Hadas 		props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
102431f69a82SYishai Hadas 		props->max_wq_type_rq =
102531f69a82SYishai Hadas 			1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
102631f69a82SYishai Hadas 	}
102731f69a82SYishai Hadas 
1028eb761894SArtemy Kovalyov 	if (MLX5_CAP_GEN(mdev, tag_matching)) {
102978b1beb0SLeon Romanovsky 		props->tm_caps.max_num_tags =
1030eb761894SArtemy Kovalyov 			(1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
103178b1beb0SLeon Romanovsky 		props->tm_caps.max_ops =
1032eb761894SArtemy Kovalyov 			1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
103378b1beb0SLeon Romanovsky 		props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
1034eb761894SArtemy Kovalyov 	}
1035eb761894SArtemy Kovalyov 
103689705e92SDanit Goldberg 	if (MLX5_CAP_GEN(mdev, tag_matching) &&
103789705e92SDanit Goldberg 	    MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
103889705e92SDanit Goldberg 		props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
103989705e92SDanit Goldberg 		props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
104089705e92SDanit Goldberg 	}
104189705e92SDanit Goldberg 
104287ab3f52SYonatan Cohen 	if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
104387ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_count =
104487ab3f52SYonatan Cohen 						MLX5_MAX_CQ_COUNT;
104587ab3f52SYonatan Cohen 		props->cq_caps.max_cq_moderation_period =
104687ab3f52SYonatan Cohen 						MLX5_MAX_CQ_PERIOD;
104787ab3f52SYonatan Cohen 	}
104887ab3f52SYonatan Cohen 
1049a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), cqe_comp_caps) <= uhw_outlen) {
1050572f46bfSYonatan Cohen 		resp.response_length += sizeof(resp.cqe_comp_caps);
1051572f46bfSYonatan Cohen 
1052572f46bfSYonatan Cohen 		if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
10537e43a2a5SBodong Wang 			resp.cqe_comp_caps.max_num =
1054572f46bfSYonatan Cohen 				MLX5_CAP_GEN(dev->mdev,
1055572f46bfSYonatan Cohen 					     cqe_compression_max_num);
1056572f46bfSYonatan Cohen 
10577e43a2a5SBodong Wang 			resp.cqe_comp_caps.supported_format =
10587e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_HASH |
10597e43a2a5SBodong Wang 				MLX5_IB_CQE_RES_FORMAT_CSUM;
10606f1006a4SYonatan Cohen 
10616f1006a4SYonatan Cohen 			if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
10626f1006a4SYonatan Cohen 				resp.cqe_comp_caps.supported_format |=
10636f1006a4SYonatan Cohen 					MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
1064572f46bfSYonatan Cohen 		}
10657e43a2a5SBodong Wang 	}
10667e43a2a5SBodong Wang 
1067a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), packet_pacing_caps) <= uhw_outlen &&
106885c7c014SDaniel Jurgens 	    raw_support) {
1069d949167dSBodong Wang 		if (MLX5_CAP_QOS(mdev, packet_pacing) &&
1070d949167dSBodong Wang 		    MLX5_CAP_GEN(mdev, qos)) {
1071d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_max =
1072d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
1073d949167dSBodong Wang 			resp.packet_pacing_caps.qp_rate_limit_min =
1074d949167dSBodong Wang 				MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
1075d949167dSBodong Wang 			resp.packet_pacing_caps.supported_qpts |=
1076d949167dSBodong Wang 				1 << IB_QPT_RAW_PACKET;
107761147f39SBodong Wang 			if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
107861147f39SBodong Wang 			    MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
107961147f39SBodong Wang 				resp.packet_pacing_caps.cap_flags |=
108061147f39SBodong Wang 					MLX5_IB_PP_SUPPORT_BURST;
1081d949167dSBodong Wang 		}
1082d949167dSBodong Wang 		resp.response_length += sizeof(resp.packet_pacing_caps);
1083d949167dSBodong Wang 	}
1084d949167dSBodong Wang 
1085a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes) <=
1086a762d460SLeon Romanovsky 	    uhw_outlen) {
1087795b609cSBodong Wang 		if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
10889f885201SLeon Romanovsky 			resp.mlx5_ib_support_multi_pkt_send_wqes =
1089795b609cSBodong Wang 				MLX5_IB_ALLOW_MPW;
1090050da902SBodong Wang 
1091050da902SBodong Wang 		if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
1092050da902SBodong Wang 			resp.mlx5_ib_support_multi_pkt_send_wqes |=
1093050da902SBodong Wang 				MLX5_IB_SUPPORT_EMPW;
1094050da902SBodong Wang 
10959f885201SLeon Romanovsky 		resp.response_length +=
10969f885201SLeon Romanovsky 			sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
10979f885201SLeon Romanovsky 	}
10989f885201SLeon Romanovsky 
1099a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), flags) <= uhw_outlen) {
1100de57f2adSGuy Levi 		resp.response_length += sizeof(resp.flags);
11017a0c8f42SGuy Levi 
1102de57f2adSGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_compression_128))
1103de57f2adSGuy Levi 			resp.flags |=
1104de57f2adSGuy Levi 				MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
11057a0c8f42SGuy Levi 
11067a0c8f42SGuy Levi 		if (MLX5_CAP_GEN(mdev, cqe_128_always))
11077a0c8f42SGuy Levi 			resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
11087e11b911SDanit Goldberg 		if (MLX5_CAP_GEN(mdev, qp_packet_based))
11097e11b911SDanit Goldberg 			resp.flags |=
11107e11b911SDanit Goldberg 				MLX5_IB_QUERY_DEV_RESP_PACKET_BASED_CREDIT_MODE;
11117249c8eaSGuy Levi 
11127249c8eaSGuy Levi 		resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_SCAT2CQE_DCT;
1113de57f2adSGuy Levi 	}
11149f885201SLeon Romanovsky 
1115a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), sw_parsing_caps) <= uhw_outlen) {
111696dc3fc5SNoa Osherovich 		resp.response_length += sizeof(resp.sw_parsing_caps);
111796dc3fc5SNoa Osherovich 		if (MLX5_CAP_ETH(mdev, swp)) {
111896dc3fc5SNoa Osherovich 			resp.sw_parsing_caps.sw_parsing_offloads |=
111996dc3fc5SNoa Osherovich 				MLX5_IB_SW_PARSING;
112096dc3fc5SNoa Osherovich 
112196dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_csum))
112296dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
112396dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_CSUM;
112496dc3fc5SNoa Osherovich 
112596dc3fc5SNoa Osherovich 			if (MLX5_CAP_ETH(mdev, swp_lso))
112696dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.sw_parsing_offloads |=
112796dc3fc5SNoa Osherovich 					MLX5_IB_SW_PARSING_LSO;
112896dc3fc5SNoa Osherovich 
112996dc3fc5SNoa Osherovich 			if (resp.sw_parsing_caps.sw_parsing_offloads)
113096dc3fc5SNoa Osherovich 				resp.sw_parsing_caps.supported_qpts =
113196dc3fc5SNoa Osherovich 					BIT(IB_QPT_RAW_PACKET);
113296dc3fc5SNoa Osherovich 		}
113396dc3fc5SNoa Osherovich 	}
113496dc3fc5SNoa Osherovich 
1135a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), striding_rq_caps) <= uhw_outlen &&
113685c7c014SDaniel Jurgens 	    raw_support) {
1137b4f34597SNoa Osherovich 		resp.response_length += sizeof(resp.striding_rq_caps);
1138b4f34597SNoa Osherovich 		if (MLX5_CAP_GEN(mdev, striding_rq)) {
1139b4f34597SNoa Osherovich 			resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
1140b4f34597SNoa Osherovich 				MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
1141b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
1142b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
1143c16339b6SMark Zhang 			if (MLX5_CAP_GEN(dev->mdev, ext_stride_num_range))
1144c16339b6SMark Zhang 				resp.striding_rq_caps
1145c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1146c16339b6SMark Zhang 					MLX5_EXT_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1147c16339b6SMark Zhang 			else
1148c16339b6SMark Zhang 				resp.striding_rq_caps
1149c16339b6SMark Zhang 					.min_single_wqe_log_num_of_strides =
1150b4f34597SNoa Osherovich 					MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
1151b4f34597SNoa Osherovich 			resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
1152b4f34597SNoa Osherovich 				MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
1153b4f34597SNoa Osherovich 			resp.striding_rq_caps.supported_qpts =
1154b4f34597SNoa Osherovich 				BIT(IB_QPT_RAW_PACKET);
1155b4f34597SNoa Osherovich 		}
1156b4f34597SNoa Osherovich 	}
1157b4f34597SNoa Osherovich 
1158a762d460SLeon Romanovsky 	if (offsetofend(typeof(resp), tunnel_offloads_caps) <= uhw_outlen) {
1159f95ef6cbSMaor Gottlieb 		resp.response_length += sizeof(resp.tunnel_offloads_caps);
1160f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
1161f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1162f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
1163f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
1164f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1165f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
1166f95ef6cbSMaor Gottlieb 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
1167f95ef6cbSMaor Gottlieb 			resp.tunnel_offloads_caps |=
1168f95ef6cbSMaor Gottlieb 				MLX5_IB_TUNNELED_OFFLOADS_GRE;
116941e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre))
1170e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1171e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
117241e684efSAlex Vesker 		if (MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_udp))
1173e818e255SAriel Levkovich 			resp.tunnel_offloads_caps |=
1174e818e255SAriel Levkovich 				MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
1175f95ef6cbSMaor Gottlieb 	}
1176f95ef6cbSMaor Gottlieb 
117748357091SLeon Romanovsky 	if (uhw_outlen) {
1178402ca536SBodong Wang 		err = ib_copy_to_udata(uhw, &resp, resp.response_length);
1179402ca536SBodong Wang 
1180402ca536SBodong Wang 		if (err)
1181402ca536SBodong Wang 			return err;
1182402ca536SBodong Wang 	}
1183402ca536SBodong Wang 
11841b5daf11SMajd Dibbiny 	return 0;
11851b5daf11SMajd Dibbiny }
1186e126ba97SEli Cohen 
1187639bf441SAharon Landau static void translate_active_width(struct ib_device *ibdev, u16 active_width,
11881b5daf11SMajd Dibbiny 				   u8 *ib_width)
11891b5daf11SMajd Dibbiny {
11901b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
11911b5daf11SMajd Dibbiny 
1192e27014bdSAharon Landau 	if (active_width & MLX5_PTYS_WIDTH_1X)
11931b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_1X;
1194e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_2X)
1195d764970bSMichael Guralnik 		*ib_width = IB_WIDTH_2X;
1196e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_4X)
11971b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_4X;
1198e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_8X)
11991b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_8X;
1200e27014bdSAharon Landau 	else if (active_width & MLX5_PTYS_WIDTH_12X)
12011b5daf11SMajd Dibbiny 		*ib_width = IB_WIDTH_12X;
1202db7a691aSMichael Guralnik 	else {
1203db7a691aSMichael Guralnik 		mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
1204e27014bdSAharon Landau 			    active_width);
1205db7a691aSMichael Guralnik 		*ib_width = IB_WIDTH_4X;
12061b5daf11SMajd Dibbiny 	}
12071b5daf11SMajd Dibbiny 
1208db7a691aSMichael Guralnik 	return;
12091b5daf11SMajd Dibbiny }
12101b5daf11SMajd Dibbiny 
12111b5daf11SMajd Dibbiny static int mlx5_mtu_to_ib_mtu(int mtu)
12121b5daf11SMajd Dibbiny {
12131b5daf11SMajd Dibbiny 	switch (mtu) {
12141b5daf11SMajd Dibbiny 	case 256: return 1;
12151b5daf11SMajd Dibbiny 	case 512: return 2;
12161b5daf11SMajd Dibbiny 	case 1024: return 3;
12171b5daf11SMajd Dibbiny 	case 2048: return 4;
12181b5daf11SMajd Dibbiny 	case 4096: return 5;
12191b5daf11SMajd Dibbiny 	default:
12201b5daf11SMajd Dibbiny 		pr_warn("invalid mtu\n");
12211b5daf11SMajd Dibbiny 		return -1;
12221b5daf11SMajd Dibbiny 	}
12231b5daf11SMajd Dibbiny }
12241b5daf11SMajd Dibbiny 
12251b5daf11SMajd Dibbiny enum ib_max_vl_num {
12261b5daf11SMajd Dibbiny 	__IB_MAX_VL_0		= 1,
12271b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_1		= 2,
12281b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_3		= 3,
12291b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_7		= 4,
12301b5daf11SMajd Dibbiny 	__IB_MAX_VL_0_14	= 5,
12311b5daf11SMajd Dibbiny };
12321b5daf11SMajd Dibbiny 
12331b5daf11SMajd Dibbiny enum mlx5_vl_hw_cap {
12341b5daf11SMajd Dibbiny 	MLX5_VL_HW_0	= 1,
12351b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_1	= 2,
12361b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_2	= 3,
12371b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_3	= 4,
12381b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_4	= 5,
12391b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_5	= 6,
12401b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_6	= 7,
12411b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_7	= 8,
12421b5daf11SMajd Dibbiny 	MLX5_VL_HW_0_14	= 15
12431b5daf11SMajd Dibbiny };
12441b5daf11SMajd Dibbiny 
12451b5daf11SMajd Dibbiny static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
12461b5daf11SMajd Dibbiny 				u8 *max_vl_num)
12471b5daf11SMajd Dibbiny {
12481b5daf11SMajd Dibbiny 	switch (vl_hw_cap) {
12491b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0:
12501b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0;
12511b5daf11SMajd Dibbiny 		break;
12521b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_1:
12531b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_1;
12541b5daf11SMajd Dibbiny 		break;
12551b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_3:
12561b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_3;
12571b5daf11SMajd Dibbiny 		break;
12581b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_7:
12591b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_7;
12601b5daf11SMajd Dibbiny 		break;
12611b5daf11SMajd Dibbiny 	case MLX5_VL_HW_0_14:
12621b5daf11SMajd Dibbiny 		*max_vl_num = __IB_MAX_VL_0_14;
12631b5daf11SMajd Dibbiny 		break;
12641b5daf11SMajd Dibbiny 
12651b5daf11SMajd Dibbiny 	default:
12661b5daf11SMajd Dibbiny 		return -EINVAL;
12671b5daf11SMajd Dibbiny 	}
12681b5daf11SMajd Dibbiny 
12691b5daf11SMajd Dibbiny 	return 0;
12701b5daf11SMajd Dibbiny }
12711b5daf11SMajd Dibbiny 
12721b5daf11SMajd Dibbiny static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
12731b5daf11SMajd Dibbiny 			       struct ib_port_attr *props)
12741b5daf11SMajd Dibbiny {
12751b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
12761b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
12771b5daf11SMajd Dibbiny 	struct mlx5_hca_vport_context *rep;
1278046339eaSSaeed Mahameed 	u16 max_mtu;
1279046339eaSSaeed Mahameed 	u16 oper_mtu;
12801b5daf11SMajd Dibbiny 	int err;
1281639bf441SAharon Landau 	u16 ib_link_width_oper;
12821b5daf11SMajd Dibbiny 	u8 vl_hw_cap;
12831b5daf11SMajd Dibbiny 
12841b5daf11SMajd Dibbiny 	rep = kzalloc(sizeof(*rep), GFP_KERNEL);
12851b5daf11SMajd Dibbiny 	if (!rep) {
12861b5daf11SMajd Dibbiny 		err = -ENOMEM;
12871b5daf11SMajd Dibbiny 		goto out;
12881b5daf11SMajd Dibbiny 	}
12891b5daf11SMajd Dibbiny 
1290c4550c63SOr Gerlitz 	/* props being zeroed by the caller, avoid zeroing it here */
12911b5daf11SMajd Dibbiny 
12921b5daf11SMajd Dibbiny 	err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
12931b5daf11SMajd Dibbiny 	if (err)
12941b5daf11SMajd Dibbiny 		goto out;
12951b5daf11SMajd Dibbiny 
12961b5daf11SMajd Dibbiny 	props->lid		= rep->lid;
12971b5daf11SMajd Dibbiny 	props->lmc		= rep->lmc;
12981b5daf11SMajd Dibbiny 	props->sm_lid		= rep->sm_lid;
12991b5daf11SMajd Dibbiny 	props->sm_sl		= rep->sm_sl;
13001b5daf11SMajd Dibbiny 	props->state		= rep->vport_state;
13011b5daf11SMajd Dibbiny 	props->phys_state	= rep->port_physical_state;
13021b5daf11SMajd Dibbiny 	props->port_cap_flags	= rep->cap_mask1;
13031b5daf11SMajd Dibbiny 	props->gid_tbl_len	= mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
13041b5daf11SMajd Dibbiny 	props->max_msg_sz	= 1 << MLX5_CAP_GEN(mdev, log_max_msg);
13051b5daf11SMajd Dibbiny 	props->pkey_tbl_len	= mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
13061b5daf11SMajd Dibbiny 	props->bad_pkey_cntr	= rep->pkey_violation_counter;
13071b5daf11SMajd Dibbiny 	props->qkey_viol_cntr	= rep->qkey_violation_counter;
13081b5daf11SMajd Dibbiny 	props->subnet_timeout	= rep->subnet_timeout;
13091b5daf11SMajd Dibbiny 	props->init_type_reply	= rep->init_type_reply;
13101b5daf11SMajd Dibbiny 
13114106a758SMichael Guralnik 	if (props->port_cap_flags & IB_PORT_CAP_MASK2_SUP)
13124106a758SMichael Guralnik 		props->port_cap_flags2 = rep->cap_mask2;
13134106a758SMichael Guralnik 
1314639bf441SAharon Landau 	err = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper,
1315376ceb31SAharon Landau 				      &props->active_speed, port);
13161b5daf11SMajd Dibbiny 	if (err)
13171b5daf11SMajd Dibbiny 		goto out;
13181b5daf11SMajd Dibbiny 
1319db7a691aSMichael Guralnik 	translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
1320db7a691aSMichael Guralnik 
1321facc9699SSaeed Mahameed 	mlx5_query_port_max_mtu(mdev, &max_mtu, port);
13221b5daf11SMajd Dibbiny 
13231b5daf11SMajd Dibbiny 	props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
13241b5daf11SMajd Dibbiny 
1325facc9699SSaeed Mahameed 	mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
13261b5daf11SMajd Dibbiny 
13271b5daf11SMajd Dibbiny 	props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
13281b5daf11SMajd Dibbiny 
13291b5daf11SMajd Dibbiny 	err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
13301b5daf11SMajd Dibbiny 	if (err)
13311b5daf11SMajd Dibbiny 		goto out;
13321b5daf11SMajd Dibbiny 
13331b5daf11SMajd Dibbiny 	err = translate_max_vl_num(ibdev, vl_hw_cap,
13341b5daf11SMajd Dibbiny 				   &props->max_vl_num);
13351b5daf11SMajd Dibbiny out:
13361b5daf11SMajd Dibbiny 	kfree(rep);
1337e126ba97SEli Cohen 	return err;
1338e126ba97SEli Cohen }
1339e126ba97SEli Cohen 
1340e126ba97SEli Cohen int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
1341e126ba97SEli Cohen 		       struct ib_port_attr *props)
1342e126ba97SEli Cohen {
1343095b0927SIlan Tayari 	unsigned int count;
1344095b0927SIlan Tayari 	int ret;
1345095b0927SIlan Tayari 
13461b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
13471b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
1348095b0927SIlan Tayari 		ret = mlx5_query_mad_ifc_port(ibdev, port, props);
1349095b0927SIlan Tayari 		break;
1350e126ba97SEli Cohen 
13511b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
1352095b0927SIlan Tayari 		ret = mlx5_query_hca_port(ibdev, port, props);
1353095b0927SIlan Tayari 		break;
13541b5daf11SMajd Dibbiny 
13553f89a643SAchiad Shochat 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1356095b0927SIlan Tayari 		ret = mlx5_query_port_roce(ibdev, port, props);
1357095b0927SIlan Tayari 		break;
13583f89a643SAchiad Shochat 
13591b5daf11SMajd Dibbiny 	default:
1360095b0927SIlan Tayari 		ret = -EINVAL;
1361e126ba97SEli Cohen 	}
1362095b0927SIlan Tayari 
1363095b0927SIlan Tayari 	if (!ret && props) {
1364b3cbd6f0SDaniel Jurgens 		struct mlx5_ib_dev *dev = to_mdev(ibdev);
1365b3cbd6f0SDaniel Jurgens 		struct mlx5_core_dev *mdev;
1366b3cbd6f0SDaniel Jurgens 		bool put_mdev = true;
1367b3cbd6f0SDaniel Jurgens 
1368b3cbd6f0SDaniel Jurgens 		mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
1369b3cbd6f0SDaniel Jurgens 		if (!mdev) {
1370b3cbd6f0SDaniel Jurgens 			/* If the port isn't affiliated yet query the master.
1371b3cbd6f0SDaniel Jurgens 			 * The master and slave will have the same values.
1372b3cbd6f0SDaniel Jurgens 			 */
1373b3cbd6f0SDaniel Jurgens 			mdev = dev->mdev;
1374b3cbd6f0SDaniel Jurgens 			port = 1;
1375b3cbd6f0SDaniel Jurgens 			put_mdev = false;
1376b3cbd6f0SDaniel Jurgens 		}
1377b3cbd6f0SDaniel Jurgens 		count = mlx5_core_reserved_gids_count(mdev);
1378b3cbd6f0SDaniel Jurgens 		if (put_mdev)
1379b3cbd6f0SDaniel Jurgens 			mlx5_ib_put_native_port_mdev(dev, port);
1380095b0927SIlan Tayari 		props->gid_tbl_len -= count;
1381095b0927SIlan Tayari 	}
1382095b0927SIlan Tayari 	return ret;
1383e126ba97SEli Cohen }
1384e126ba97SEli Cohen 
13858e6efa3aSMark Bloch static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
13868e6efa3aSMark Bloch 				  struct ib_port_attr *props)
13878e6efa3aSMark Bloch {
13887a58779eSParav Pandit 	return mlx5_query_port_roce(ibdev, port, props);
13898e6efa3aSMark Bloch }
13908e6efa3aSMark Bloch 
1391d6fd59e1SParav Pandit static int mlx5_ib_rep_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1392d6fd59e1SParav Pandit 				  u16 *pkey)
1393d6fd59e1SParav Pandit {
1394d6fd59e1SParav Pandit 	/* Default special Pkey for representor device port as per the
1395d6fd59e1SParav Pandit 	 * IB specification 1.3 section 10.9.1.2.
1396d6fd59e1SParav Pandit 	 */
1397d6fd59e1SParav Pandit 	*pkey = 0xffff;
1398d6fd59e1SParav Pandit 	return 0;
1399d6fd59e1SParav Pandit }
1400d6fd59e1SParav Pandit 
1401e126ba97SEli Cohen static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
1402e126ba97SEli Cohen 			     union ib_gid *gid)
1403e126ba97SEli Cohen {
14041b5daf11SMajd Dibbiny 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
14051b5daf11SMajd Dibbiny 	struct mlx5_core_dev *mdev = dev->mdev;
1406e126ba97SEli Cohen 
14071b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14081b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14091b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
1410e126ba97SEli Cohen 
14111b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14121b5daf11SMajd Dibbiny 		return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
1413e126ba97SEli Cohen 
14141b5daf11SMajd Dibbiny 	default:
14151b5daf11SMajd Dibbiny 		return -EINVAL;
14161b5daf11SMajd Dibbiny 	}
1417e126ba97SEli Cohen 
1418e126ba97SEli Cohen }
1419e126ba97SEli Cohen 
1420b3cbd6f0SDaniel Jurgens static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
1421b3cbd6f0SDaniel Jurgens 				   u16 index, u16 *pkey)
1422b3cbd6f0SDaniel Jurgens {
1423b3cbd6f0SDaniel Jurgens 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1424b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1425b3cbd6f0SDaniel Jurgens 	bool put_mdev = true;
1426b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1427b3cbd6f0SDaniel Jurgens 	int err;
1428b3cbd6f0SDaniel Jurgens 
1429b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
1430b3cbd6f0SDaniel Jurgens 	if (!mdev) {
1431b3cbd6f0SDaniel Jurgens 		/* The port isn't affiliated yet, get the PKey from the master
1432b3cbd6f0SDaniel Jurgens 		 * port. For RoCE the PKey tables will be the same.
1433b3cbd6f0SDaniel Jurgens 		 */
1434b3cbd6f0SDaniel Jurgens 		put_mdev = false;
1435b3cbd6f0SDaniel Jurgens 		mdev = dev->mdev;
1436b3cbd6f0SDaniel Jurgens 		mdev_port_num = 1;
1437b3cbd6f0SDaniel Jurgens 	}
1438b3cbd6f0SDaniel Jurgens 
1439b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
1440b3cbd6f0SDaniel Jurgens 					index, pkey);
1441b3cbd6f0SDaniel Jurgens 	if (put_mdev)
1442b3cbd6f0SDaniel Jurgens 		mlx5_ib_put_native_port_mdev(dev, port);
1443b3cbd6f0SDaniel Jurgens 
1444b3cbd6f0SDaniel Jurgens 	return err;
1445b3cbd6f0SDaniel Jurgens }
1446b3cbd6f0SDaniel Jurgens 
1447e126ba97SEli Cohen static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
1448e126ba97SEli Cohen 			      u16 *pkey)
1449e126ba97SEli Cohen {
14501b5daf11SMajd Dibbiny 	switch (mlx5_get_vport_access_method(ibdev)) {
14511b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_MAD:
14521b5daf11SMajd Dibbiny 		return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
1453e126ba97SEli Cohen 
14541b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_HCA:
14551b5daf11SMajd Dibbiny 	case MLX5_VPORT_ACCESS_METHOD_NIC:
1456b3cbd6f0SDaniel Jurgens 		return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
14571b5daf11SMajd Dibbiny 	default:
14581b5daf11SMajd Dibbiny 		return -EINVAL;
1459e126ba97SEli Cohen 	}
14601b5daf11SMajd Dibbiny }
1461e126ba97SEli Cohen 
1462e126ba97SEli Cohen static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
1463e126ba97SEli Cohen 				 struct ib_device_modify *props)
1464e126ba97SEli Cohen {
1465e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1466e126ba97SEli Cohen 	struct mlx5_reg_node_desc in;
1467e126ba97SEli Cohen 	struct mlx5_reg_node_desc out;
1468e126ba97SEli Cohen 	int err;
1469e126ba97SEli Cohen 
1470e126ba97SEli Cohen 	if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
1471e126ba97SEli Cohen 		return -EOPNOTSUPP;
1472e126ba97SEli Cohen 
1473e126ba97SEli Cohen 	if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
1474e126ba97SEli Cohen 		return 0;
1475e126ba97SEli Cohen 
1476e126ba97SEli Cohen 	/*
1477e126ba97SEli Cohen 	 * If possible, pass node desc to FW, so it can generate
1478e126ba97SEli Cohen 	 * a 144 trap.  If cmd fails, just ignore.
1479e126ba97SEli Cohen 	 */
1480bd99fdeaSYuval Shaia 	memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
14819603b61dSJack Morgenstein 	err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
1482e126ba97SEli Cohen 				   sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
1483e126ba97SEli Cohen 	if (err)
1484e126ba97SEli Cohen 		return err;
1485e126ba97SEli Cohen 
1486bd99fdeaSYuval Shaia 	memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
1487e126ba97SEli Cohen 
1488e126ba97SEli Cohen 	return err;
1489e126ba97SEli Cohen }
1490e126ba97SEli Cohen 
1491cdbe33d0SEli Cohen static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
1492cdbe33d0SEli Cohen 				u32 value)
1493cdbe33d0SEli Cohen {
1494cdbe33d0SEli Cohen 	struct mlx5_hca_vport_context ctx = {};
1495b3cbd6f0SDaniel Jurgens 	struct mlx5_core_dev *mdev;
1496b3cbd6f0SDaniel Jurgens 	u8 mdev_port_num;
1497cdbe33d0SEli Cohen 	int err;
1498cdbe33d0SEli Cohen 
1499b3cbd6f0SDaniel Jurgens 	mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
1500b3cbd6f0SDaniel Jurgens 	if (!mdev)
1501b3cbd6f0SDaniel Jurgens 		return -ENODEV;
1502b3cbd6f0SDaniel Jurgens 
1503b3cbd6f0SDaniel Jurgens 	err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
1504cdbe33d0SEli Cohen 	if (err)
1505b3cbd6f0SDaniel Jurgens 		goto out;
1506cdbe33d0SEli Cohen 
1507cdbe33d0SEli Cohen 	if (~ctx.cap_mask1_perm & mask) {
1508cdbe33d0SEli Cohen 		mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
1509cdbe33d0SEli Cohen 			     mask, ctx.cap_mask1_perm);
1510b3cbd6f0SDaniel Jurgens 		err = -EINVAL;
1511b3cbd6f0SDaniel Jurgens 		goto out;
1512cdbe33d0SEli Cohen 	}
1513cdbe33d0SEli Cohen 
1514cdbe33d0SEli Cohen 	ctx.cap_mask1 = value;
1515cdbe33d0SEli Cohen 	ctx.cap_mask1_perm = mask;
1516b3cbd6f0SDaniel Jurgens 	err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
1517b3cbd6f0SDaniel Jurgens 						 0, &ctx);
1518b3cbd6f0SDaniel Jurgens 
1519b3cbd6f0SDaniel Jurgens out:
1520b3cbd6f0SDaniel Jurgens 	mlx5_ib_put_native_port_mdev(dev, port_num);
1521cdbe33d0SEli Cohen 
1522cdbe33d0SEli Cohen 	return err;
1523cdbe33d0SEli Cohen }
1524cdbe33d0SEli Cohen 
1525e126ba97SEli Cohen static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
1526e126ba97SEli Cohen 			       struct ib_port_modify *props)
1527e126ba97SEli Cohen {
1528e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1529e126ba97SEli Cohen 	struct ib_port_attr attr;
1530e126ba97SEli Cohen 	u32 tmp;
1531e126ba97SEli Cohen 	int err;
1532cdbe33d0SEli Cohen 	u32 change_mask;
1533cdbe33d0SEli Cohen 	u32 value;
1534cdbe33d0SEli Cohen 	bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
1535cdbe33d0SEli Cohen 		      IB_LINK_LAYER_INFINIBAND);
1536cdbe33d0SEli Cohen 
1537ec255879SMajd Dibbiny 	/* CM layer calls ib_modify_port() regardless of the link layer. For
1538ec255879SMajd Dibbiny 	 * Ethernet ports, qkey violation and Port capabilities are meaningless.
1539ec255879SMajd Dibbiny 	 */
1540ec255879SMajd Dibbiny 	if (!is_ib)
1541ec255879SMajd Dibbiny 		return 0;
1542ec255879SMajd Dibbiny 
1543cdbe33d0SEli Cohen 	if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
1544cdbe33d0SEli Cohen 		change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
1545cdbe33d0SEli Cohen 		value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
1546cdbe33d0SEli Cohen 		return set_port_caps_atomic(dev, port, change_mask, value);
1547cdbe33d0SEli Cohen 	}
1548e126ba97SEli Cohen 
1549e126ba97SEli Cohen 	mutex_lock(&dev->cap_mask_mutex);
1550e126ba97SEli Cohen 
1551c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port, &attr);
1552e126ba97SEli Cohen 	if (err)
1553e126ba97SEli Cohen 		goto out;
1554e126ba97SEli Cohen 
1555e126ba97SEli Cohen 	tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
1556e126ba97SEli Cohen 		~props->clr_port_cap_mask;
1557e126ba97SEli Cohen 
15589603b61dSJack Morgenstein 	err = mlx5_set_port_caps(dev->mdev, port, tmp);
1559e126ba97SEli Cohen 
1560e126ba97SEli Cohen out:
1561e126ba97SEli Cohen 	mutex_unlock(&dev->cap_mask_mutex);
1562e126ba97SEli Cohen 	return err;
1563e126ba97SEli Cohen }
1564e126ba97SEli Cohen 
156530aa60b3SEli Cohen static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
156630aa60b3SEli Cohen {
156730aa60b3SEli Cohen 	mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
156830aa60b3SEli Cohen 		    caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
156930aa60b3SEli Cohen }
157030aa60b3SEli Cohen 
157131a78a5aSYishai Hadas static u16 calc_dynamic_bfregs(int uars_per_sys_page)
157231a78a5aSYishai Hadas {
157331a78a5aSYishai Hadas 	/* Large page with non 4k uar support might limit the dynamic size */
157431a78a5aSYishai Hadas 	if (uars_per_sys_page == 1  && PAGE_SIZE > 4096)
157531a78a5aSYishai Hadas 		return MLX5_MIN_DYN_BFREGS;
157631a78a5aSYishai Hadas 
157731a78a5aSYishai Hadas 	return MLX5_MAX_DYN_BFREGS;
157831a78a5aSYishai Hadas }
157931a78a5aSYishai Hadas 
1580b037c29aSEli Cohen static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
1581b037c29aSEli Cohen 			     struct mlx5_ib_alloc_ucontext_req_v2 *req,
158231a78a5aSYishai Hadas 			     struct mlx5_bfreg_info *bfregi)
1583b037c29aSEli Cohen {
1584b037c29aSEli Cohen 	int uars_per_sys_page;
1585b037c29aSEli Cohen 	int bfregs_per_sys_page;
1586b037c29aSEli Cohen 	int ref_bfregs = req->total_num_bfregs;
1587b037c29aSEli Cohen 
1588b037c29aSEli Cohen 	if (req->total_num_bfregs == 0)
1589b037c29aSEli Cohen 		return -EINVAL;
1590b037c29aSEli Cohen 
1591b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
1592b037c29aSEli Cohen 	BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
1593b037c29aSEli Cohen 
1594b037c29aSEli Cohen 	if (req->total_num_bfregs > MLX5_MAX_BFREGS)
1595b037c29aSEli Cohen 		return -ENOMEM;
1596b037c29aSEli Cohen 
1597b037c29aSEli Cohen 	uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
1598b037c29aSEli Cohen 	bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
159931a78a5aSYishai Hadas 	/* This holds the required static allocation asked by the user */
1600b037c29aSEli Cohen 	req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
1601b037c29aSEli Cohen 	if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
1602b037c29aSEli Cohen 		return -EINVAL;
1603b037c29aSEli Cohen 
160431a78a5aSYishai Hadas 	bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
160531a78a5aSYishai Hadas 	bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
160631a78a5aSYishai Hadas 	bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
160731a78a5aSYishai Hadas 	bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
160831a78a5aSYishai Hadas 
160931a78a5aSYishai 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",
1610b037c29aSEli Cohen 		    MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
1611b037c29aSEli Cohen 		    lib_uar_4k ? "yes" : "no", ref_bfregs,
161231a78a5aSYishai Hadas 		    req->total_num_bfregs, bfregi->total_num_bfregs,
161331a78a5aSYishai Hadas 		    bfregi->num_sys_pages);
1614b037c29aSEli Cohen 
1615b037c29aSEli Cohen 	return 0;
1616b037c29aSEli Cohen }
1617b037c29aSEli Cohen 
1618b037c29aSEli Cohen static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
1619b037c29aSEli Cohen {
1620b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1621b037c29aSEli Cohen 	int err;
1622b037c29aSEli Cohen 	int i;
1623b037c29aSEli Cohen 
1624b037c29aSEli Cohen 	bfregi = &context->bfregi;
162531a78a5aSYishai Hadas 	for (i = 0; i < bfregi->num_static_sys_pages; i++) {
1626b037c29aSEli Cohen 		err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
1627b037c29aSEli Cohen 		if (err)
1628b037c29aSEli Cohen 			goto error;
1629b037c29aSEli Cohen 
1630b037c29aSEli Cohen 		mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
1631b037c29aSEli Cohen 	}
16324ed131d0SYishai Hadas 
16334ed131d0SYishai Hadas 	for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
16344ed131d0SYishai Hadas 		bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
16354ed131d0SYishai Hadas 
1636b037c29aSEli Cohen 	return 0;
1637b037c29aSEli Cohen 
1638b037c29aSEli Cohen error:
1639b037c29aSEli Cohen 	for (--i; i >= 0; i--)
1640b037c29aSEli Cohen 		if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
1641b037c29aSEli Cohen 			mlx5_ib_warn(dev, "failed to free uar %d\n", i);
1642b037c29aSEli Cohen 
1643b037c29aSEli Cohen 	return err;
1644b037c29aSEli Cohen }
1645b037c29aSEli Cohen 
164615177999SLeon Romanovsky static void deallocate_uars(struct mlx5_ib_dev *dev,
164715177999SLeon Romanovsky 			    struct mlx5_ib_ucontext *context)
1648b037c29aSEli Cohen {
1649b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1650b037c29aSEli Cohen 	int i;
1651b037c29aSEli Cohen 
1652b037c29aSEli Cohen 	bfregi = &context->bfregi;
165315177999SLeon Romanovsky 	for (i = 0; i < bfregi->num_sys_pages; i++)
16544ed131d0SYishai Hadas 		if (i < bfregi->num_static_sys_pages ||
165515177999SLeon Romanovsky 		    bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
165615177999SLeon Romanovsky 			mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
1657b037c29aSEli Cohen }
1658b037c29aSEli Cohen 
16590042f9e4SMark Bloch int mlx5_ib_enable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1660a560f1d9SMark Bloch {
1661a560f1d9SMark Bloch 	int err = 0;
1662a560f1d9SMark Bloch 
1663a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16640042f9e4SMark Bloch 	if (td)
1665a560f1d9SMark Bloch 		dev->lb.user_td++;
16660042f9e4SMark Bloch 	if (qp)
16670042f9e4SMark Bloch 		dev->lb.qps++;
1668a560f1d9SMark Bloch 
16690042f9e4SMark Bloch 	if (dev->lb.user_td == 2 ||
16700042f9e4SMark Bloch 	    dev->lb.qps == 1) {
16710042f9e4SMark Bloch 		if (!dev->lb.enabled) {
1672a560f1d9SMark Bloch 			err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
16730042f9e4SMark Bloch 			dev->lb.enabled = true;
16740042f9e4SMark Bloch 		}
16750042f9e4SMark Bloch 	}
1676a560f1d9SMark Bloch 
1677a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1678a560f1d9SMark Bloch 
1679a560f1d9SMark Bloch 	return err;
1680a560f1d9SMark Bloch }
1681a560f1d9SMark Bloch 
16820042f9e4SMark Bloch void mlx5_ib_disable_lb(struct mlx5_ib_dev *dev, bool td, bool qp)
1683a560f1d9SMark Bloch {
1684a560f1d9SMark Bloch 	mutex_lock(&dev->lb.mutex);
16850042f9e4SMark Bloch 	if (td)
1686a560f1d9SMark Bloch 		dev->lb.user_td--;
16870042f9e4SMark Bloch 	if (qp)
16880042f9e4SMark Bloch 		dev->lb.qps--;
1689a560f1d9SMark Bloch 
16900042f9e4SMark Bloch 	if (dev->lb.user_td == 1 &&
16910042f9e4SMark Bloch 	    dev->lb.qps == 0) {
16920042f9e4SMark Bloch 		if (dev->lb.enabled) {
1693a560f1d9SMark Bloch 			mlx5_nic_vport_update_local_lb(dev->mdev, false);
16940042f9e4SMark Bloch 			dev->lb.enabled = false;
16950042f9e4SMark Bloch 		}
16960042f9e4SMark Bloch 	}
1697a560f1d9SMark Bloch 
1698a560f1d9SMark Bloch 	mutex_unlock(&dev->lb.mutex);
1699a560f1d9SMark Bloch }
1700a560f1d9SMark Bloch 
1701d2d19121SYishai Hadas static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn,
1702d2d19121SYishai Hadas 					  u16 uid)
1703c85023e1SHuy Nguyen {
1704c85023e1SHuy Nguyen 	int err;
1705c85023e1SHuy Nguyen 
1706cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1707cfdeb893SLeon Romanovsky 		return 0;
1708cfdeb893SLeon Romanovsky 
1709d2d19121SYishai Hadas 	err = mlx5_cmd_alloc_transport_domain(dev->mdev, tdn, uid);
1710c85023e1SHuy Nguyen 	if (err)
1711c85023e1SHuy Nguyen 		return err;
1712c85023e1SHuy Nguyen 
1713c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17148978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17158978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1716c85023e1SHuy Nguyen 		return err;
1717c85023e1SHuy Nguyen 
17180042f9e4SMark Bloch 	return mlx5_ib_enable_lb(dev, true, false);
1719c85023e1SHuy Nguyen }
1720c85023e1SHuy Nguyen 
1721d2d19121SYishai Hadas static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn,
1722d2d19121SYishai Hadas 					     u16 uid)
1723c85023e1SHuy Nguyen {
1724cfdeb893SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
1725cfdeb893SLeon Romanovsky 		return;
1726cfdeb893SLeon Romanovsky 
1727d2d19121SYishai Hadas 	mlx5_cmd_dealloc_transport_domain(dev->mdev, tdn, uid);
1728c85023e1SHuy Nguyen 
1729c85023e1SHuy Nguyen 	if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
17308978cc92SEran Ben Elisha 	    (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
17318978cc92SEran Ben Elisha 	     !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
1732c85023e1SHuy Nguyen 		return;
1733c85023e1SHuy Nguyen 
17340042f9e4SMark Bloch 	mlx5_ib_disable_lb(dev, true, false);
1735c85023e1SHuy Nguyen }
1736c85023e1SHuy Nguyen 
173745ec21c9SYishai Hadas static int set_ucontext_resp(struct ib_ucontext *uctx,
173845ec21c9SYishai Hadas 			     struct mlx5_ib_alloc_ucontext_resp *resp)
173945ec21c9SYishai Hadas {
174045ec21c9SYishai Hadas 	struct ib_device *ibdev = uctx->device;
174145ec21c9SYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
174245ec21c9SYishai Hadas 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
174345ec21c9SYishai Hadas 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
174445ec21c9SYishai Hadas 	int err;
174545ec21c9SYishai Hadas 
174645ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
174745ec21c9SYishai Hadas 		err = mlx5_cmd_dump_fill_mkey(dev->mdev,
174845ec21c9SYishai Hadas 					      &resp->dump_fill_mkey);
174945ec21c9SYishai Hadas 		if (err)
175045ec21c9SYishai Hadas 			return err;
175145ec21c9SYishai Hadas 		resp->comp_mask |=
175245ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
175345ec21c9SYishai Hadas 	}
175445ec21c9SYishai Hadas 
175545ec21c9SYishai Hadas 	resp->qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
175645ec21c9SYishai Hadas 	if (dev->wc_support)
175745ec21c9SYishai Hadas 		resp->bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev,
175845ec21c9SYishai Hadas 						      log_bf_reg_size);
175945ec21c9SYishai Hadas 	resp->cache_line_size = cache_line_size();
176045ec21c9SYishai Hadas 	resp->max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
176145ec21c9SYishai Hadas 	resp->max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
176245ec21c9SYishai Hadas 	resp->max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
176345ec21c9SYishai Hadas 	resp->max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
176445ec21c9SYishai Hadas 	resp->max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
176545ec21c9SYishai Hadas 	resp->cqe_version = context->cqe_version;
176645ec21c9SYishai Hadas 	resp->log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
176745ec21c9SYishai Hadas 				MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
176845ec21c9SYishai Hadas 	resp->num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
176945ec21c9SYishai Hadas 					MLX5_CAP_GEN(dev->mdev,
177045ec21c9SYishai Hadas 						     num_of_uars_per_page) : 1;
177145ec21c9SYishai Hadas 
177245ec21c9SYishai Hadas 	if (mlx5_accel_ipsec_device_caps(dev->mdev) &
177345ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_DEVICE) {
177445ec21c9SYishai Hadas 		if (mlx5_get_flow_namespace(dev->mdev,
177545ec21c9SYishai Hadas 				MLX5_FLOW_NAMESPACE_EGRESS))
177645ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
177745ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
177845ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
177945ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
178045ec21c9SYishai Hadas 		if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
178145ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
178245ec21c9SYishai Hadas 		if (mlx5_accel_ipsec_device_caps(dev->mdev) &
178345ec21c9SYishai Hadas 				MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
178445ec21c9SYishai Hadas 			resp->flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
178545ec21c9SYishai Hadas 		/* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
178645ec21c9SYishai Hadas 	}
178745ec21c9SYishai Hadas 
178845ec21c9SYishai Hadas 	resp->tot_bfregs = bfregi->lib_uar_dyn ? 0 :
178945ec21c9SYishai Hadas 			bfregi->total_num_bfregs - bfregi->num_dyn_bfregs;
179045ec21c9SYishai Hadas 	resp->num_ports = dev->num_ports;
179145ec21c9SYishai Hadas 	resp->cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
179245ec21c9SYishai Hadas 				      MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
179345ec21c9SYishai Hadas 
179445ec21c9SYishai Hadas 	if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
179545ec21c9SYishai Hadas 		mlx5_query_min_inline(dev->mdev, &resp->eth_min_inline);
179645ec21c9SYishai Hadas 		resp->eth_min_inline++;
179745ec21c9SYishai Hadas 	}
179845ec21c9SYishai Hadas 
179945ec21c9SYishai Hadas 	if (dev->mdev->clock_info)
180045ec21c9SYishai Hadas 		resp->clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
180145ec21c9SYishai Hadas 
180245ec21c9SYishai Hadas 	/*
180345ec21c9SYishai Hadas 	 * We don't want to expose information from the PCI bar that is located
180445ec21c9SYishai Hadas 	 * after 4096 bytes, so if the arch only supports larger pages, let's
180545ec21c9SYishai Hadas 	 * pretend we don't support reading the HCA's core clock. This is also
180645ec21c9SYishai Hadas 	 * forced by mmap function.
180745ec21c9SYishai Hadas 	 */
180845ec21c9SYishai Hadas 	if (PAGE_SIZE <= 4096) {
180945ec21c9SYishai Hadas 		resp->comp_mask |=
181045ec21c9SYishai Hadas 			MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
181145ec21c9SYishai Hadas 		resp->hca_core_clock_offset =
181245ec21c9SYishai Hadas 			offsetof(struct mlx5_init_seg,
181345ec21c9SYishai Hadas 				 internal_timer_h) % PAGE_SIZE;
181445ec21c9SYishai Hadas 	}
181545ec21c9SYishai Hadas 
181645ec21c9SYishai Hadas 	if (MLX5_CAP_GEN(dev->mdev, ece_support))
181745ec21c9SYishai Hadas 		resp->comp_mask |= MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_ECE;
181845ec21c9SYishai Hadas 
181945ec21c9SYishai Hadas 	resp->num_dyn_bfregs = bfregi->num_dyn_bfregs;
182045ec21c9SYishai Hadas 	return 0;
182145ec21c9SYishai Hadas }
182245ec21c9SYishai Hadas 
1823a2a074efSLeon Romanovsky static int mlx5_ib_alloc_ucontext(struct ib_ucontext *uctx,
1824e126ba97SEli Cohen 				  struct ib_udata *udata)
1825e126ba97SEli Cohen {
1826a2a074efSLeon Romanovsky 	struct ib_device *ibdev = uctx->device;
1827e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
1828b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_req_v2 req = {};
1829b368d7cbSMatan Barak 	struct mlx5_ib_alloc_ucontext_resp resp = {};
1830a2a074efSLeon Romanovsky 	struct mlx5_ib_ucontext *context = to_mucontext(uctx);
18312f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi;
183278c0f98cSEli Cohen 	int ver;
1833e126ba97SEli Cohen 	int err;
1834a168a41cSMajd Dibbiny 	size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
1835a168a41cSMajd Dibbiny 				     max_cqe_version);
1836b037c29aSEli Cohen 	bool lib_uar_4k;
18370a2fd01cSYishai Hadas 	bool lib_uar_dyn;
1838e126ba97SEli Cohen 
1839e126ba97SEli Cohen 	if (!dev->ib_active)
1840a2a074efSLeon Romanovsky 		return -EAGAIN;
1841e126ba97SEli Cohen 
1842e093111dSAmrani, Ram 	if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
184378c0f98cSEli Cohen 		ver = 0;
1844e093111dSAmrani, Ram 	else if (udata->inlen >= min_req_v2)
184578c0f98cSEli Cohen 		ver = 2;
184678c0f98cSEli Cohen 	else
1847a2a074efSLeon Romanovsky 		return -EINVAL;
184878c0f98cSEli Cohen 
1849e093111dSAmrani, Ram 	err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
1850e126ba97SEli Cohen 	if (err)
1851a2a074efSLeon Romanovsky 		return err;
1852e126ba97SEli Cohen 
1853a8b92ca1SYishai Hadas 	if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
1854a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
185578c0f98cSEli Cohen 
1856f72300c5SHaggai Abramovsky 	if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
1857a2a074efSLeon Romanovsky 		return -EOPNOTSUPP;
1858b368d7cbSMatan Barak 
18592f5ff264SEli Cohen 	req.total_num_bfregs = ALIGN(req.total_num_bfregs,
18602f5ff264SEli Cohen 				    MLX5_NON_FP_BFREGS_PER_UAR);
18612f5ff264SEli Cohen 	if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
1862a2a074efSLeon Romanovsky 		return -EINVAL;
1863e126ba97SEli Cohen 
186430aa60b3SEli Cohen 	lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
18650a2fd01cSYishai Hadas 	lib_uar_dyn = req.lib_caps & MLX5_LIB_CAP_DYN_UAR;
18662f5ff264SEli Cohen 	bfregi = &context->bfregi;
1867b037c29aSEli Cohen 
18680a2fd01cSYishai Hadas 	if (lib_uar_dyn) {
18690a2fd01cSYishai Hadas 		bfregi->lib_uar_dyn = lib_uar_dyn;
18700a2fd01cSYishai Hadas 		goto uar_done;
18710a2fd01cSYishai Hadas 	}
18720a2fd01cSYishai Hadas 
1873b037c29aSEli Cohen 	/* updates req->total_num_bfregs */
187431a78a5aSYishai Hadas 	err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
1875b037c29aSEli Cohen 	if (err)
1876b037c29aSEli Cohen 		goto out_ctx;
1877b037c29aSEli Cohen 
18782f5ff264SEli Cohen 	mutex_init(&bfregi->lock);
1879b037c29aSEli Cohen 	bfregi->lib_uar_4k = lib_uar_4k;
188031a78a5aSYishai Hadas 	bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
1881b037c29aSEli Cohen 				GFP_KERNEL);
1882b037c29aSEli Cohen 	if (!bfregi->count) {
1883e126ba97SEli Cohen 		err = -ENOMEM;
1884e126ba97SEli Cohen 		goto out_ctx;
1885e126ba97SEli Cohen 	}
1886e126ba97SEli Cohen 
1887b037c29aSEli Cohen 	bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
1888b037c29aSEli Cohen 				    sizeof(*bfregi->sys_pages),
1889e126ba97SEli Cohen 				    GFP_KERNEL);
1890b037c29aSEli Cohen 	if (!bfregi->sys_pages) {
1891e126ba97SEli Cohen 		err = -ENOMEM;
1892e126ba97SEli Cohen 		goto out_count;
1893e126ba97SEli Cohen 	}
1894e126ba97SEli Cohen 
1895b037c29aSEli Cohen 	err = allocate_uars(dev, context);
1896b037c29aSEli Cohen 	if (err)
1897b037c29aSEli Cohen 		goto out_sys_pages;
1898b037c29aSEli Cohen 
18990a2fd01cSYishai Hadas uar_done:
1900a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
1901fb98153bSYishai Hadas 		err = mlx5_ib_devx_create(dev, true);
190276dc5a84SYishai Hadas 		if (err < 0)
1903d2d19121SYishai Hadas 			goto out_uars;
190476dc5a84SYishai Hadas 		context->devx_uid = err;
1905a8b92ca1SYishai Hadas 	}
1906a8b92ca1SYishai Hadas 
1907d2d19121SYishai Hadas 	err = mlx5_ib_alloc_transport_domain(dev, &context->tdn,
1908d2d19121SYishai Hadas 					     context->devx_uid);
1909a8b92ca1SYishai Hadas 	if (err)
1910d2d19121SYishai Hadas 		goto out_devx;
1911a8b92ca1SYishai Hadas 
1912e126ba97SEli Cohen 	INIT_LIST_HEAD(&context->db_page_list);
1913e126ba97SEli Cohen 	mutex_init(&context->db_page_mutex);
1914e126ba97SEli Cohen 
191545ec21c9SYishai Hadas 	context->cqe_version = min_t(__u8,
191645ec21c9SYishai Hadas 				 (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
191745ec21c9SYishai Hadas 				 req.max_cqe_version);
1918b368d7cbSMatan Barak 
191945ec21c9SYishai Hadas 	err = set_ucontext_resp(uctx, &resp);
192045ec21c9SYishai Hadas 	if (err)
192145ec21c9SYishai Hadas 		goto out_mdev;
1922b368d7cbSMatan Barak 
192345ec21c9SYishai Hadas 	resp.response_length = min(udata->outlen, sizeof(resp));
1924b368d7cbSMatan Barak 	err = ib_copy_to_udata(udata, &resp, resp.response_length);
1925e126ba97SEli Cohen 	if (err)
1926a8b92ca1SYishai Hadas 		goto out_mdev;
1927e126ba97SEli Cohen 
19282f5ff264SEli Cohen 	bfregi->ver = ver;
19292f5ff264SEli Cohen 	bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
193030aa60b3SEli Cohen 	context->lib_caps = req.lib_caps;
193130aa60b3SEli Cohen 	print_lib_caps(dev, context->lib_caps);
1932f72300c5SHaggai Abramovsky 
1933802dcc7fSMark Zhang 	if (mlx5_ib_lag_should_assign_affinity(dev)) {
193495579e78SMark Bloch 		u8 port = mlx5_core_native_port_num(dev->mdev) - 1;
1935c6a21c38SMajd Dibbiny 
1936c6a21c38SMajd Dibbiny 		atomic_set(&context->tx_port_affinity,
1937c6a21c38SMajd Dibbiny 			   atomic_add_return(
193895579e78SMark Bloch 				   1, &dev->port[port].roce.tx_port_affinity));
1939c6a21c38SMajd Dibbiny 	}
1940c6a21c38SMajd Dibbiny 
1941a2a074efSLeon Romanovsky 	return 0;
1942e126ba97SEli Cohen 
1943a8b92ca1SYishai Hadas out_mdev:
1944d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1945d2d19121SYishai Hadas out_devx:
1946a8b92ca1SYishai Hadas 	if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
194776dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1948146d2f1aSmajd@mellanox.com 
1949e126ba97SEli Cohen out_uars:
1950b037c29aSEli Cohen 	deallocate_uars(dev, context);
1951b037c29aSEli Cohen 
1952b037c29aSEli Cohen out_sys_pages:
1953b037c29aSEli Cohen 	kfree(bfregi->sys_pages);
1954b037c29aSEli Cohen 
1955e126ba97SEli Cohen out_count:
19562f5ff264SEli Cohen 	kfree(bfregi->count);
1957e126ba97SEli Cohen 
1958e126ba97SEli Cohen out_ctx:
1959a2a074efSLeon Romanovsky 	return err;
1960e126ba97SEli Cohen }
1961e126ba97SEli Cohen 
19620fb556b2SYishai Hadas static int mlx5_ib_query_ucontext(struct ib_ucontext *ibcontext,
19630fb556b2SYishai Hadas 				  struct uverbs_attr_bundle *attrs)
19640fb556b2SYishai Hadas {
19650fb556b2SYishai Hadas 	struct mlx5_ib_alloc_ucontext_resp uctx_resp = {};
19660fb556b2SYishai Hadas 	int ret;
19670fb556b2SYishai Hadas 
19680fb556b2SYishai Hadas 	ret = set_ucontext_resp(ibcontext, &uctx_resp);
19690fb556b2SYishai Hadas 	if (ret)
19700fb556b2SYishai Hadas 		return ret;
19710fb556b2SYishai Hadas 
19720fb556b2SYishai Hadas 	uctx_resp.response_length =
19730fb556b2SYishai Hadas 		min_t(size_t,
19740fb556b2SYishai Hadas 		      uverbs_attr_get_len(attrs,
19750fb556b2SYishai Hadas 				MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX),
19760fb556b2SYishai Hadas 		      sizeof(uctx_resp));
19770fb556b2SYishai Hadas 
19780fb556b2SYishai Hadas 	ret = uverbs_copy_to_struct_or_zero(attrs,
19790fb556b2SYishai Hadas 					MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
19800fb556b2SYishai Hadas 					&uctx_resp,
19810fb556b2SYishai Hadas 					sizeof(uctx_resp));
19820fb556b2SYishai Hadas 	return ret;
19830fb556b2SYishai Hadas }
19840fb556b2SYishai Hadas 
1985a2a074efSLeon Romanovsky static void mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
1986e126ba97SEli Cohen {
1987e126ba97SEli Cohen 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
1988e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
1989b037c29aSEli Cohen 	struct mlx5_bfreg_info *bfregi;
1990e126ba97SEli Cohen 
1991b037c29aSEli Cohen 	bfregi = &context->bfregi;
1992d2d19121SYishai Hadas 	mlx5_ib_dealloc_transport_domain(dev, context->tdn, context->devx_uid);
1993d2d19121SYishai Hadas 
1994b037c29aSEli Cohen 	if (context->devx_uid)
199576dc5a84SYishai Hadas 		mlx5_ib_devx_destroy(dev, context->devx_uid);
1996e126ba97SEli Cohen 
1997e126ba97SEli Cohen 	deallocate_uars(dev, context);
19982f5ff264SEli Cohen 	kfree(bfregi->sys_pages);
19992f5ff264SEli Cohen 	kfree(bfregi->count);
2000e126ba97SEli Cohen }
2001e126ba97SEli Cohen 
2002e126ba97SEli Cohen static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
2003e126ba97SEli Cohen 				 int uar_idx)
2004e126ba97SEli Cohen {
2005b037c29aSEli Cohen 	int fw_uars_per_page;
2006b037c29aSEli Cohen 
2007b037c29aSEli Cohen 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
2008b037c29aSEli Cohen 
2009aa8106f1SHuy Nguyen 	return (dev->mdev->bar_addr >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
2010e126ba97SEli Cohen }
2011e126ba97SEli Cohen 
2012342ee59dSYishai Hadas static u64 uar_index2paddress(struct mlx5_ib_dev *dev,
2013342ee59dSYishai Hadas 				 int uar_idx)
2014342ee59dSYishai Hadas {
2015342ee59dSYishai Hadas 	unsigned int fw_uars_per_page;
2016342ee59dSYishai Hadas 
2017342ee59dSYishai Hadas 	fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
2018342ee59dSYishai Hadas 				MLX5_UARS_IN_PAGE : 1;
2019342ee59dSYishai Hadas 
2020342ee59dSYishai Hadas 	return (dev->mdev->bar_addr + (uar_idx / fw_uars_per_page) * PAGE_SIZE);
2021342ee59dSYishai Hadas }
2022342ee59dSYishai Hadas 
2023e126ba97SEli Cohen static int get_command(unsigned long offset)
2024e126ba97SEli Cohen {
2025e126ba97SEli Cohen 	return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
2026e126ba97SEli Cohen }
2027e126ba97SEli Cohen 
2028e126ba97SEli Cohen static int get_arg(unsigned long offset)
2029e126ba97SEli Cohen {
2030e126ba97SEli Cohen 	return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
2031e126ba97SEli Cohen }
2032e126ba97SEli Cohen 
2033e126ba97SEli Cohen static int get_index(unsigned long offset)
2034e126ba97SEli Cohen {
2035e126ba97SEli Cohen 	return get_arg(offset);
2036e126ba97SEli Cohen }
2037e126ba97SEli Cohen 
20384ed131d0SYishai Hadas /* Index resides in an extra byte to enable larger values than 255 */
20394ed131d0SYishai Hadas static int get_extended_index(unsigned long offset)
20404ed131d0SYishai Hadas {
20414ed131d0SYishai Hadas 	return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
20424ed131d0SYishai Hadas }
20434ed131d0SYishai Hadas 
20447c2344c3SMaor Gottlieb 
20457c2344c3SMaor Gottlieb static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
20467c2344c3SMaor Gottlieb {
20477c2344c3SMaor Gottlieb }
20487c2344c3SMaor Gottlieb 
204937aa5c36SGuy Levi static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
2050e126ba97SEli Cohen {
205137aa5c36SGuy Levi 	switch (cmd) {
205237aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
205337aa5c36SGuy Levi 		return "WC";
2054e126ba97SEli Cohen 	case MLX5_IB_MMAP_REGULAR_PAGE:
205537aa5c36SGuy Levi 		return "best effort WC";
205637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
205737aa5c36SGuy Levi 		return "NC";
205824da0016SAriel Levkovich 	case MLX5_IB_MMAP_DEVICE_MEM:
205924da0016SAriel Levkovich 		return "Device Memory";
206037aa5c36SGuy Levi 	default:
206137aa5c36SGuy Levi 		return NULL;
206237aa5c36SGuy Levi 	}
206337aa5c36SGuy Levi }
206437aa5c36SGuy Levi 
20655c99eaecSFeras Daoud static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
20665c99eaecSFeras Daoud 					struct vm_area_struct *vma,
20675c99eaecSFeras Daoud 					struct mlx5_ib_ucontext *context)
20685c99eaecSFeras Daoud {
20694eb6ab13SJason Gunthorpe 	if ((vma->vm_end - vma->vm_start != PAGE_SIZE) ||
20704eb6ab13SJason Gunthorpe 	    !(vma->vm_flags & VM_SHARED))
20715c99eaecSFeras Daoud 		return -EINVAL;
20725c99eaecSFeras Daoud 
20735c99eaecSFeras Daoud 	if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
20745c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20755c99eaecSFeras Daoud 
20764eb6ab13SJason Gunthorpe 	if (vma->vm_flags & (VM_WRITE | VM_EXEC))
20775c99eaecSFeras Daoud 		return -EPERM;
2078c660133cSJason Gunthorpe 	vma->vm_flags &= ~VM_MAYWRITE;
20795c99eaecSFeras Daoud 
2080ddcdc368SJason Gunthorpe 	if (!dev->mdev->clock_info)
20815c99eaecSFeras Daoud 		return -EOPNOTSUPP;
20825c99eaecSFeras Daoud 
20834eb6ab13SJason Gunthorpe 	return vm_insert_page(vma, vma->vm_start,
20844eb6ab13SJason Gunthorpe 			      virt_to_page(dev->mdev->clock_info));
20855c99eaecSFeras Daoud }
20865c99eaecSFeras Daoud 
2087dc2316ebSYishai Hadas static void mlx5_ib_mmap_free(struct rdma_user_mmap_entry *entry)
2088dc2316ebSYishai Hadas {
2089dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry = to_mmmap(entry);
2090dc2316ebSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(entry->ucontext->device);
20917be76befSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
2092dc2316ebSYishai Hadas 	struct mlx5_ib_dm *mdm;
2093dc2316ebSYishai Hadas 
2094dc2316ebSYishai Hadas 	switch (mentry->mmap_flag) {
2095dc2316ebSYishai Hadas 	case MLX5_IB_MMAP_TYPE_MEMIC:
2096dc2316ebSYishai Hadas 		mdm = container_of(mentry, struct mlx5_ib_dm, mentry);
2097dc2316ebSYishai Hadas 		mlx5_cmd_dealloc_memic(&dev->dm, mdm->dev_addr,
2098dc2316ebSYishai Hadas 				       mdm->size);
2099dc2316ebSYishai Hadas 		kfree(mdm);
2100dc2316ebSYishai Hadas 		break;
21017be76befSYishai Hadas 	case MLX5_IB_MMAP_TYPE_VAR:
21027be76befSYishai Hadas 		mutex_lock(&var_table->bitmap_lock);
21037be76befSYishai Hadas 		clear_bit(mentry->page_idx, var_table->bitmap);
21047be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
21057be76befSYishai Hadas 		kfree(mentry);
21067be76befSYishai Hadas 		break;
2107342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_WC:
2108342ee59dSYishai Hadas 	case MLX5_IB_MMAP_TYPE_UAR_NC:
2109342ee59dSYishai Hadas 		mlx5_cmd_free_uar(dev->mdev, mentry->page_idx);
2110342ee59dSYishai Hadas 		kfree(mentry);
2111342ee59dSYishai Hadas 		break;
2112dc2316ebSYishai Hadas 	default:
2113dc2316ebSYishai Hadas 		WARN_ON(true);
2114dc2316ebSYishai Hadas 	}
2115dc2316ebSYishai Hadas }
2116dc2316ebSYishai Hadas 
211737aa5c36SGuy Levi static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
21187c2344c3SMaor Gottlieb 		    struct vm_area_struct *vma,
21197c2344c3SMaor Gottlieb 		    struct mlx5_ib_ucontext *context)
212037aa5c36SGuy Levi {
21212f5ff264SEli Cohen 	struct mlx5_bfreg_info *bfregi = &context->bfregi;
212237aa5c36SGuy Levi 	int err;
212337aa5c36SGuy Levi 	unsigned long idx;
2124aa09ea6eSKamal Heib 	phys_addr_t pfn;
212537aa5c36SGuy Levi 	pgprot_t prot;
21264ed131d0SYishai Hadas 	u32 bfreg_dyn_idx = 0;
21274ed131d0SYishai Hadas 	u32 uar_index;
21284ed131d0SYishai Hadas 	int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
21294ed131d0SYishai Hadas 	int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
21304ed131d0SYishai Hadas 				bfregi->num_static_sys_pages;
2131b037c29aSEli Cohen 
21320a2fd01cSYishai Hadas 	if (bfregi->lib_uar_dyn)
21330a2fd01cSYishai Hadas 		return -EINVAL;
21340a2fd01cSYishai Hadas 
2135b037c29aSEli Cohen 	if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2136b037c29aSEli Cohen 		return -EINVAL;
2137b037c29aSEli Cohen 
21384ed131d0SYishai Hadas 	if (dyn_uar)
21394ed131d0SYishai Hadas 		idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
21404ed131d0SYishai Hadas 	else
2141b037c29aSEli Cohen 		idx = get_index(vma->vm_pgoff);
21424ed131d0SYishai Hadas 
21434ed131d0SYishai Hadas 	if (idx >= max_valid_idx) {
21444ed131d0SYishai Hadas 		mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
21454ed131d0SYishai Hadas 			     idx, max_valid_idx);
2146b037c29aSEli Cohen 		return -EINVAL;
2147b037c29aSEli Cohen 	}
214837aa5c36SGuy Levi 
214937aa5c36SGuy Levi 	switch (cmd) {
215037aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
21514ed131d0SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
215237aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
215337aa5c36SGuy Levi 		/* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
215437aa5c36SGuy Levi 		prot = pgprot_writecombine(vma->vm_page_prot);
215537aa5c36SGuy Levi 		break;
215637aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
215737aa5c36SGuy Levi 		prot = pgprot_noncached(vma->vm_page_prot);
215837aa5c36SGuy Levi 		break;
215937aa5c36SGuy Levi 	default:
216037aa5c36SGuy Levi 		return -EINVAL;
216137aa5c36SGuy Levi 	}
216237aa5c36SGuy Levi 
21634ed131d0SYishai Hadas 	if (dyn_uar) {
21644ed131d0SYishai Hadas 		int uars_per_page;
21654ed131d0SYishai Hadas 
21664ed131d0SYishai Hadas 		uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
21674ed131d0SYishai Hadas 		bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
21684ed131d0SYishai Hadas 		if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
21694ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
21704ed131d0SYishai Hadas 				     bfreg_dyn_idx, bfregi->total_num_bfregs);
21714ed131d0SYishai Hadas 			return -EINVAL;
21724ed131d0SYishai Hadas 		}
21734ed131d0SYishai Hadas 
21744ed131d0SYishai Hadas 		mutex_lock(&bfregi->lock);
21754ed131d0SYishai Hadas 		/* Fail if uar already allocated, first bfreg index of each
21764ed131d0SYishai Hadas 		 * page holds its count.
21774ed131d0SYishai Hadas 		 */
21784ed131d0SYishai Hadas 		if (bfregi->count[bfreg_dyn_idx]) {
21794ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
21804ed131d0SYishai Hadas 			mutex_unlock(&bfregi->lock);
21814ed131d0SYishai Hadas 			return -EINVAL;
21824ed131d0SYishai Hadas 		}
21834ed131d0SYishai Hadas 
21844ed131d0SYishai Hadas 		bfregi->count[bfreg_dyn_idx]++;
21854ed131d0SYishai Hadas 		mutex_unlock(&bfregi->lock);
21864ed131d0SYishai Hadas 
21874ed131d0SYishai Hadas 		err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
21884ed131d0SYishai Hadas 		if (err) {
21894ed131d0SYishai Hadas 			mlx5_ib_warn(dev, "UAR alloc failed\n");
21904ed131d0SYishai Hadas 			goto free_bfreg;
21914ed131d0SYishai Hadas 		}
21924ed131d0SYishai Hadas 	} else {
21934ed131d0SYishai Hadas 		uar_index = bfregi->sys_pages[idx];
21944ed131d0SYishai Hadas 	}
21954ed131d0SYishai Hadas 
21964ed131d0SYishai Hadas 	pfn = uar_index2pfn(dev, uar_index);
219737aa5c36SGuy Levi 	mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
2198e126ba97SEli Cohen 
2199e2cd1d1aSJason Gunthorpe 	err = rdma_user_mmap_io(&context->ibucontext, vma, pfn, PAGE_SIZE,
2200c043ff2cSMichal Kalderon 				prot, NULL);
220137aa5c36SGuy Levi 	if (err) {
22028f062287SLeon Romanovsky 		mlx5_ib_err(dev,
2203e2cd1d1aSJason Gunthorpe 			    "rdma_user_mmap_io failed with error=%d, mmap_cmd=%s\n",
22048f062287SLeon Romanovsky 			    err, mmap_cmd2str(cmd));
22054ed131d0SYishai Hadas 		goto err;
220637aa5c36SGuy Levi 	}
2207e126ba97SEli Cohen 
22084ed131d0SYishai Hadas 	if (dyn_uar)
22094ed131d0SYishai Hadas 		bfregi->sys_pages[idx] = uar_index;
22104ed131d0SYishai Hadas 	return 0;
22114ed131d0SYishai Hadas 
22124ed131d0SYishai Hadas err:
22134ed131d0SYishai Hadas 	if (!dyn_uar)
22144ed131d0SYishai Hadas 		return err;
22154ed131d0SYishai Hadas 
22164ed131d0SYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, idx);
22174ed131d0SYishai Hadas 
22184ed131d0SYishai Hadas free_bfreg:
22194ed131d0SYishai Hadas 	mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
22204ed131d0SYishai Hadas 
22214ed131d0SYishai Hadas 	return err;
222237aa5c36SGuy Levi }
222337aa5c36SGuy Levi 
2224dc2316ebSYishai Hadas static int add_dm_mmap_entry(struct ib_ucontext *context,
2225dc2316ebSYishai Hadas 			     struct mlx5_ib_dm *mdm,
2226dc2316ebSYishai Hadas 			     u64 address)
222724da0016SAriel Levkovich {
2228dc2316ebSYishai Hadas 	mdm->mentry.mmap_flag = MLX5_IB_MMAP_TYPE_MEMIC;
2229dc2316ebSYishai Hadas 	mdm->mentry.address = address;
2230dc2316ebSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
2231dc2316ebSYishai Hadas 			context, &mdm->mentry.rdma_entry,
2232dc2316ebSYishai Hadas 			mdm->size,
2233dc2316ebSYishai Hadas 			MLX5_IB_MMAP_DEVICE_MEM << 16,
2234dc2316ebSYishai Hadas 			(MLX5_IB_MMAP_DEVICE_MEM << 16) + (1UL << 16) - 1);
2235dc2316ebSYishai Hadas }
223624da0016SAriel Levkovich 
2237dc2316ebSYishai Hadas static unsigned long mlx5_vma_to_pgoff(struct vm_area_struct *vma)
2238dc2316ebSYishai Hadas {
2239dc2316ebSYishai Hadas 	unsigned long idx;
2240dc2316ebSYishai Hadas 	u8 command;
2241dc2316ebSYishai Hadas 
2242dc2316ebSYishai Hadas 	command = get_command(vma->vm_pgoff);
2243dc2316ebSYishai Hadas 	idx = get_extended_index(vma->vm_pgoff);
2244dc2316ebSYishai Hadas 
2245dc2316ebSYishai Hadas 	return (command << 16 | idx);
2246dc2316ebSYishai Hadas }
2247dc2316ebSYishai Hadas 
2248dc2316ebSYishai Hadas static int mlx5_ib_mmap_offset(struct mlx5_ib_dev *dev,
2249dc2316ebSYishai Hadas 			       struct vm_area_struct *vma,
2250dc2316ebSYishai Hadas 			       struct ib_ucontext *ucontext)
2251dc2316ebSYishai Hadas {
2252dc2316ebSYishai Hadas 	struct mlx5_user_mmap_entry *mentry;
2253dc2316ebSYishai Hadas 	struct rdma_user_mmap_entry *entry;
2254dc2316ebSYishai Hadas 	unsigned long pgoff;
2255dc2316ebSYishai Hadas 	pgprot_t prot;
2256dc2316ebSYishai Hadas 	phys_addr_t pfn;
2257dc2316ebSYishai Hadas 	int ret;
2258dc2316ebSYishai Hadas 
2259dc2316ebSYishai Hadas 	pgoff = mlx5_vma_to_pgoff(vma);
2260dc2316ebSYishai Hadas 	entry = rdma_user_mmap_entry_get_pgoff(ucontext, pgoff);
2261dc2316ebSYishai Hadas 	if (!entry)
226224da0016SAriel Levkovich 		return -EINVAL;
226324da0016SAriel Levkovich 
2264dc2316ebSYishai Hadas 	mentry = to_mmmap(entry);
2265dc2316ebSYishai Hadas 	pfn = (mentry->address >> PAGE_SHIFT);
2266342ee59dSYishai Hadas 	if (mentry->mmap_flag == MLX5_IB_MMAP_TYPE_VAR ||
2267342ee59dSYishai Hadas 	    mentry->mmap_flag == MLX5_IB_MMAP_TYPE_UAR_NC)
22683f59b6c3SYishai Hadas 		prot = pgprot_noncached(vma->vm_page_prot);
22693f59b6c3SYishai Hadas 	else
2270dc2316ebSYishai Hadas 		prot = pgprot_writecombine(vma->vm_page_prot);
2271dc2316ebSYishai Hadas 	ret = rdma_user_mmap_io(ucontext, vma, pfn,
2272dc2316ebSYishai Hadas 				entry->npages * PAGE_SIZE,
2273dc2316ebSYishai Hadas 				prot,
2274dc2316ebSYishai Hadas 				entry);
2275dc2316ebSYishai Hadas 	rdma_user_mmap_entry_put(&mentry->rdma_entry);
2276dc2316ebSYishai Hadas 	return ret;
227724da0016SAriel Levkovich }
227824da0016SAriel Levkovich 
22797be76befSYishai Hadas static u64 mlx5_entry_to_mmap_offset(struct mlx5_user_mmap_entry *entry)
22807be76befSYishai Hadas {
22819b6d3bbcSLeon Romanovsky 	u64 cmd = (entry->rdma_entry.start_pgoff >> 16) & 0xFFFF;
22829b6d3bbcSLeon Romanovsky 	u64 index = entry->rdma_entry.start_pgoff & 0xFFFF;
22837be76befSYishai Hadas 
22847be76befSYishai Hadas 	return (((index >> 8) << 16) | (cmd << MLX5_IB_MMAP_CMD_SHIFT) |
22857be76befSYishai Hadas 		(index & 0xFF)) << PAGE_SHIFT;
22867be76befSYishai Hadas }
22877be76befSYishai Hadas 
228837aa5c36SGuy Levi static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
228937aa5c36SGuy Levi {
229037aa5c36SGuy Levi 	struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
229137aa5c36SGuy Levi 	struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
229237aa5c36SGuy Levi 	unsigned long command;
229337aa5c36SGuy Levi 	phys_addr_t pfn;
229437aa5c36SGuy Levi 
229537aa5c36SGuy Levi 	command = get_command(vma->vm_pgoff);
229637aa5c36SGuy Levi 	switch (command) {
229737aa5c36SGuy Levi 	case MLX5_IB_MMAP_WC_PAGE:
22981f3db161SYishai Hadas 	case MLX5_IB_MMAP_ALLOC_WC:
22991f3db161SYishai Hadas 		if (!dev->wc_support)
23001f3db161SYishai Hadas 			return -EPERM;
23011f3db161SYishai Hadas 		fallthrough;
230237aa5c36SGuy Levi 	case MLX5_IB_MMAP_NC_PAGE:
230337aa5c36SGuy Levi 	case MLX5_IB_MMAP_REGULAR_PAGE:
23047c2344c3SMaor Gottlieb 		return uar_mmap(dev, command, vma, context);
2305e126ba97SEli Cohen 
2306e126ba97SEli Cohen 	case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
2307e126ba97SEli Cohen 		return -ENOSYS;
2308e126ba97SEli Cohen 
2309d69e3bcfSMatan Barak 	case MLX5_IB_MMAP_CORE_CLOCK:
2310d69e3bcfSMatan Barak 		if (vma->vm_end - vma->vm_start != PAGE_SIZE)
2311d69e3bcfSMatan Barak 			return -EINVAL;
2312d69e3bcfSMatan Barak 
23136cbac1e4SMatan Barak 		if (vma->vm_flags & VM_WRITE)
2314d69e3bcfSMatan Barak 			return -EPERM;
2315c660133cSJason Gunthorpe 		vma->vm_flags &= ~VM_MAYWRITE;
2316d69e3bcfSMatan Barak 
2317d69e3bcfSMatan Barak 		/* Don't expose to user-space information it shouldn't have */
2318d69e3bcfSMatan Barak 		if (PAGE_SIZE > 4096)
2319d69e3bcfSMatan Barak 			return -EOPNOTSUPP;
2320d69e3bcfSMatan Barak 
2321d69e3bcfSMatan Barak 		pfn = (dev->mdev->iseg_base +
2322d69e3bcfSMatan Barak 		       offsetof(struct mlx5_init_seg, internal_timer_h)) >>
2323d69e3bcfSMatan Barak 			PAGE_SHIFT;
2324d5e560d3SJason Gunthorpe 		return rdma_user_mmap_io(&context->ibucontext, vma, pfn,
2325d5e560d3SJason Gunthorpe 					 PAGE_SIZE,
2326c043ff2cSMichal Kalderon 					 pgprot_noncached(vma->vm_page_prot),
2327c043ff2cSMichal Kalderon 					 NULL);
23285c99eaecSFeras Daoud 	case MLX5_IB_MMAP_CLOCK_INFO:
23295c99eaecSFeras Daoud 		return mlx5_ib_mmap_clock_info_page(dev, vma, context);
2330d69e3bcfSMatan Barak 
2331e126ba97SEli Cohen 	default:
2332dc2316ebSYishai Hadas 		return mlx5_ib_mmap_offset(dev, vma, ibcontext);
2333e126ba97SEli Cohen 	}
2334e126ba97SEli Cohen 
2335e126ba97SEli Cohen 	return 0;
2336e126ba97SEli Cohen }
2337e126ba97SEli Cohen 
233825c13324SAriel Levkovich static inline int check_dm_type_support(struct mlx5_ib_dev *dev,
233925c13324SAriel Levkovich 					u32 type)
234025c13324SAriel Levkovich {
234125c13324SAriel Levkovich 	switch (type) {
234225c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
234325c13324SAriel Levkovich 		if (!MLX5_CAP_DEV_MEM(dev->mdev, memic))
234425c13324SAriel Levkovich 			return -EOPNOTSUPP;
234525c13324SAriel Levkovich 		break;
234625c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2347c9b9dcb4SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
234825c13324SAriel Levkovich 		if (!capable(CAP_SYS_RAWIO) ||
234925c13324SAriel Levkovich 		    !capable(CAP_NET_RAW))
235025c13324SAriel Levkovich 			return -EPERM;
235125c13324SAriel Levkovich 
235225c13324SAriel Levkovich 		if (!(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner) ||
23538310e327SAlex Vesker 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner) ||
23548310e327SAlex Vesker 		      MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev, sw_owner_v2) ||
23558310e327SAlex Vesker 		      MLX5_CAP_FLOWTABLE_NIC_TX(dev->mdev, sw_owner_v2)))
235625c13324SAriel Levkovich 			return -EOPNOTSUPP;
235725c13324SAriel Levkovich 		break;
235825c13324SAriel Levkovich 	}
235925c13324SAriel Levkovich 
236025c13324SAriel Levkovich 	return 0;
236125c13324SAriel Levkovich }
236225c13324SAriel Levkovich 
23633b113a1eSAriel Levkovich static int handle_alloc_dm_memic(struct ib_ucontext *ctx,
23643b113a1eSAriel Levkovich 				 struct mlx5_ib_dm *dm,
236524da0016SAriel Levkovich 				 struct ib_dm_alloc_attr *attr,
236624da0016SAriel Levkovich 				 struct uverbs_attr_bundle *attrs)
236724da0016SAriel Levkovich {
23683b113a1eSAriel Levkovich 	struct mlx5_dm *dm_db = &to_mdev(ctx->device)->dm;
236924da0016SAriel Levkovich 	u64 start_offset;
2370dc2316ebSYishai Hadas 	u16 page_idx;
237124da0016SAriel Levkovich 	int err;
2372dc2316ebSYishai Hadas 	u64 address;
237324da0016SAriel Levkovich 
23743b113a1eSAriel Levkovich 	dm->size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
237524da0016SAriel Levkovich 
23763b113a1eSAriel Levkovich 	err = mlx5_cmd_alloc_memic(dm_db, &dm->dev_addr,
23773b113a1eSAriel Levkovich 				   dm->size, attr->alignment);
237824da0016SAriel Levkovich 	if (err)
23793b113a1eSAriel Levkovich 		return err;
238024da0016SAriel Levkovich 
2381dc2316ebSYishai Hadas 	address = dm->dev_addr & PAGE_MASK;
2382dc2316ebSYishai Hadas 	err = add_dm_mmap_entry(ctx, dm, address);
238324da0016SAriel Levkovich 	if (err)
238424da0016SAriel Levkovich 		goto err_dealloc;
238524da0016SAriel Levkovich 
2386dc2316ebSYishai Hadas 	page_idx = dm->mentry.rdma_entry.start_pgoff & 0xFFFF;
2387dc2316ebSYishai Hadas 	err = uverbs_copy_to(attrs,
2388dc2316ebSYishai Hadas 			     MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
2389dc2316ebSYishai Hadas 			     &page_idx,
2390dc2316ebSYishai Hadas 			     sizeof(page_idx));
2391dc2316ebSYishai Hadas 	if (err)
2392dc2316ebSYishai Hadas 		goto err_copy;
2393dc2316ebSYishai Hadas 
23943b113a1eSAriel Levkovich 	start_offset = dm->dev_addr & ~PAGE_MASK;
23953b113a1eSAriel Levkovich 	err = uverbs_copy_to(attrs,
23963b113a1eSAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
23973b113a1eSAriel Levkovich 			     &start_offset, sizeof(start_offset));
23983b113a1eSAriel Levkovich 	if (err)
2399dc2316ebSYishai Hadas 		goto err_copy;
24003b113a1eSAriel Levkovich 
24013b113a1eSAriel Levkovich 	return 0;
24023b113a1eSAriel Levkovich 
2403dc2316ebSYishai Hadas err_copy:
2404dc2316ebSYishai Hadas 	rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
24053b113a1eSAriel Levkovich err_dealloc:
24063b113a1eSAriel Levkovich 	mlx5_cmd_dealloc_memic(dm_db, dm->dev_addr, dm->size);
24073b113a1eSAriel Levkovich 
24083b113a1eSAriel Levkovich 	return err;
24093b113a1eSAriel Levkovich }
24103b113a1eSAriel Levkovich 
241125c13324SAriel Levkovich static int handle_alloc_dm_sw_icm(struct ib_ucontext *ctx,
241225c13324SAriel Levkovich 				  struct mlx5_ib_dm *dm,
241325c13324SAriel Levkovich 				  struct ib_dm_alloc_attr *attr,
241425c13324SAriel Levkovich 				  struct uverbs_attr_bundle *attrs,
241525c13324SAriel Levkovich 				  int type)
241625c13324SAriel Levkovich {
2417c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ctx->device)->mdev;
241825c13324SAriel Levkovich 	u64 act_size;
241925c13324SAriel Levkovich 	int err;
242025c13324SAriel Levkovich 
242125c13324SAriel Levkovich 	/* Allocation size must a multiple of the basic block size
242225c13324SAriel Levkovich 	 * and a power of 2.
242325c13324SAriel Levkovich 	 */
2424c9b9dcb4SAriel Levkovich 	act_size = round_up(attr->length, MLX5_SW_ICM_BLOCK_SIZE(dev));
242525c13324SAriel Levkovich 	act_size = roundup_pow_of_two(act_size);
242625c13324SAriel Levkovich 
242725c13324SAriel Levkovich 	dm->size = act_size;
2428dff8e2d1SErez Shitrit 	err = mlx5_dm_sw_icm_alloc(dev, type, act_size, attr->alignment,
242925c13324SAriel Levkovich 				   to_mucontext(ctx)->devx_uid, &dm->dev_addr,
243025c13324SAriel Levkovich 				   &dm->icm_dm.obj_id);
243125c13324SAriel Levkovich 	if (err)
243225c13324SAriel Levkovich 		return err;
243325c13324SAriel Levkovich 
243425c13324SAriel Levkovich 	err = uverbs_copy_to(attrs,
243525c13324SAriel Levkovich 			     MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
243625c13324SAriel Levkovich 			     &dm->dev_addr, sizeof(dm->dev_addr));
243725c13324SAriel Levkovich 	if (err)
2438c9b9dcb4SAriel Levkovich 		mlx5_dm_sw_icm_dealloc(dev, type, dm->size,
2439c9b9dcb4SAriel Levkovich 				       to_mucontext(ctx)->devx_uid, dm->dev_addr,
2440c9b9dcb4SAriel Levkovich 				       dm->icm_dm.obj_id);
244125c13324SAriel Levkovich 
244225c13324SAriel Levkovich 	return err;
244325c13324SAriel Levkovich }
244425c13324SAriel Levkovich 
24453b113a1eSAriel Levkovich struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
24463b113a1eSAriel Levkovich 			       struct ib_ucontext *context,
24473b113a1eSAriel Levkovich 			       struct ib_dm_alloc_attr *attr,
24483b113a1eSAriel Levkovich 			       struct uverbs_attr_bundle *attrs)
24493b113a1eSAriel Levkovich {
24503b113a1eSAriel Levkovich 	struct mlx5_ib_dm *dm;
24513b113a1eSAriel Levkovich 	enum mlx5_ib_uapi_dm_type type;
24523b113a1eSAriel Levkovich 	int err;
24533b113a1eSAriel Levkovich 
24543b113a1eSAriel Levkovich 	err = uverbs_get_const_default(&type, attrs,
24553b113a1eSAriel Levkovich 				       MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
24563b113a1eSAriel Levkovich 				       MLX5_IB_UAPI_DM_TYPE_MEMIC);
24573b113a1eSAriel Levkovich 	if (err)
24583b113a1eSAriel Levkovich 		return ERR_PTR(err);
24593b113a1eSAriel Levkovich 
24603b113a1eSAriel Levkovich 	mlx5_ib_dbg(to_mdev(ibdev), "alloc_dm req: dm_type=%d user_length=0x%llx log_alignment=%d\n",
24613b113a1eSAriel Levkovich 		    type, attr->length, attr->alignment);
24623b113a1eSAriel Levkovich 
246325c13324SAriel Levkovich 	err = check_dm_type_support(to_mdev(ibdev), type);
246425c13324SAriel Levkovich 	if (err)
246525c13324SAriel Levkovich 		return ERR_PTR(err);
246625c13324SAriel Levkovich 
24673b113a1eSAriel Levkovich 	dm = kzalloc(sizeof(*dm), GFP_KERNEL);
24683b113a1eSAriel Levkovich 	if (!dm)
24693b113a1eSAriel Levkovich 		return ERR_PTR(-ENOMEM);
24703b113a1eSAriel Levkovich 
24713b113a1eSAriel Levkovich 	dm->type = type;
24723b113a1eSAriel Levkovich 
24733b113a1eSAriel Levkovich 	switch (type) {
24743b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
24753b113a1eSAriel Levkovich 		err = handle_alloc_dm_memic(context, dm,
24763b113a1eSAriel Levkovich 					    attr,
24773b113a1eSAriel Levkovich 					    attrs);
24783b113a1eSAriel Levkovich 		break;
247925c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2480c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2481c9b9dcb4SAriel Levkovich 					     attr, attrs,
2482c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_STEERING);
2483c9b9dcb4SAriel Levkovich 		break;
248425c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2485c9b9dcb4SAriel Levkovich 		err = handle_alloc_dm_sw_icm(context, dm,
2486c9b9dcb4SAriel Levkovich 					     attr, attrs,
2487c9b9dcb4SAriel Levkovich 					     MLX5_SW_ICM_TYPE_HEADER_MODIFY);
248825c13324SAriel Levkovich 		break;
24893b113a1eSAriel Levkovich 	default:
24903b113a1eSAriel Levkovich 		err = -EOPNOTSUPP;
24913b113a1eSAriel Levkovich 	}
24923b113a1eSAriel Levkovich 
24933b113a1eSAriel Levkovich 	if (err)
24943b113a1eSAriel Levkovich 		goto err_free;
249524da0016SAriel Levkovich 
249624da0016SAriel Levkovich 	return &dm->ibdm;
249724da0016SAriel Levkovich 
249824da0016SAriel Levkovich err_free:
249924da0016SAriel Levkovich 	kfree(dm);
250024da0016SAriel Levkovich 	return ERR_PTR(err);
250124da0016SAriel Levkovich }
250224da0016SAriel Levkovich 
2503c4367a26SShamir Rabinovitch int mlx5_ib_dealloc_dm(struct ib_dm *ibdm, struct uverbs_attr_bundle *attrs)
250424da0016SAriel Levkovich {
250525c13324SAriel Levkovich 	struct mlx5_ib_ucontext *ctx = rdma_udata_to_drv_context(
250625c13324SAriel Levkovich 		&attrs->driver_udata, struct mlx5_ib_ucontext, ibucontext);
2507c9b9dcb4SAriel Levkovich 	struct mlx5_core_dev *dev = to_mdev(ibdm->device)->mdev;
250824da0016SAriel Levkovich 	struct mlx5_ib_dm *dm = to_mdm(ibdm);
250924da0016SAriel Levkovich 	int ret;
251024da0016SAriel Levkovich 
25113b113a1eSAriel Levkovich 	switch (dm->type) {
25123b113a1eSAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_MEMIC:
2513dc2316ebSYishai Hadas 		rdma_user_mmap_entry_remove(&dm->mentry.rdma_entry);
2514dc2316ebSYishai Hadas 		return 0;
251525c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_STEERING_SW_ICM:
2516c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_STEERING,
2517c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
2518c9b9dcb4SAriel Levkovich 					     dm->icm_dm.obj_id);
2519c9b9dcb4SAriel Levkovich 		if (ret)
2520c9b9dcb4SAriel Levkovich 			return ret;
2521c9b9dcb4SAriel Levkovich 		break;
252225c13324SAriel Levkovich 	case MLX5_IB_UAPI_DM_TYPE_HEADER_MODIFY_SW_ICM:
2523c9b9dcb4SAriel Levkovich 		ret = mlx5_dm_sw_icm_dealloc(dev, MLX5_SW_ICM_TYPE_HEADER_MODIFY,
2524c9b9dcb4SAriel Levkovich 					     dm->size, ctx->devx_uid, dm->dev_addr,
252525c13324SAriel Levkovich 					     dm->icm_dm.obj_id);
252625c13324SAriel Levkovich 		if (ret)
252725c13324SAriel Levkovich 			return ret;
25283b113a1eSAriel Levkovich 		break;
25293b113a1eSAriel Levkovich 	default:
25303b113a1eSAriel Levkovich 		return -EOPNOTSUPP;
25313b113a1eSAriel Levkovich 	}
253224da0016SAriel Levkovich 
253324da0016SAriel Levkovich 	kfree(dm);
253424da0016SAriel Levkovich 
253524da0016SAriel Levkovich 	return 0;
253624da0016SAriel Levkovich }
253724da0016SAriel Levkovich 
2538ff23dfa1SShamir Rabinovitch static int mlx5_ib_alloc_pd(struct ib_pd *ibpd, struct ib_udata *udata)
2539e126ba97SEli Cohen {
254021a428a0SLeon Romanovsky 	struct mlx5_ib_pd *pd = to_mpd(ibpd);
254121a428a0SLeon Romanovsky 	struct ib_device *ibdev = ibpd->device;
2542e126ba97SEli Cohen 	struct mlx5_ib_alloc_pd_resp resp;
2543e126ba97SEli Cohen 	int err;
2544a1069c1cSYishai Hadas 	u32 out[MLX5_ST_SZ_DW(alloc_pd_out)] = {};
2545a1069c1cSYishai Hadas 	u32 in[MLX5_ST_SZ_DW(alloc_pd_in)] = {};
2546a1069c1cSYishai Hadas 	u16 uid = 0;
2547ff23dfa1SShamir Rabinovitch 	struct mlx5_ib_ucontext *context = rdma_udata_to_drv_context(
2548ff23dfa1SShamir Rabinovitch 		udata, struct mlx5_ib_ucontext, ibucontext);
2549e126ba97SEli Cohen 
2550ff23dfa1SShamir Rabinovitch 	uid = context ? context->devx_uid : 0;
2551a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, opcode, MLX5_CMD_OP_ALLOC_PD);
2552a1069c1cSYishai Hadas 	MLX5_SET(alloc_pd_in, in, uid, uid);
255331578defSLeon Romanovsky 	err = mlx5_cmd_exec_inout(to_mdev(ibdev)->mdev, alloc_pd, in, out);
255421a428a0SLeon Romanovsky 	if (err)
255521a428a0SLeon Romanovsky 		return err;
2556e126ba97SEli Cohen 
2557a1069c1cSYishai Hadas 	pd->pdn = MLX5_GET(alloc_pd_out, out, pd);
2558a1069c1cSYishai Hadas 	pd->uid = uid;
2559ff23dfa1SShamir Rabinovitch 	if (udata) {
2560e126ba97SEli Cohen 		resp.pdn = pd->pdn;
2561e126ba97SEli Cohen 		if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
2562a1069c1cSYishai Hadas 			mlx5_cmd_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn, uid);
256321a428a0SLeon Romanovsky 			return -EFAULT;
2564e126ba97SEli Cohen 		}
2565e126ba97SEli Cohen 	}
2566e126ba97SEli Cohen 
256721a428a0SLeon Romanovsky 	return 0;
2568e126ba97SEli Cohen }
2569e126ba97SEli Cohen 
257091a7c58fSLeon Romanovsky static int mlx5_ib_dealloc_pd(struct ib_pd *pd, struct ib_udata *udata)
2571e126ba97SEli Cohen {
2572e126ba97SEli Cohen 	struct mlx5_ib_dev *mdev = to_mdev(pd->device);
2573e126ba97SEli Cohen 	struct mlx5_ib_pd *mpd = to_mpd(pd);
2574e126ba97SEli Cohen 
257591a7c58fSLeon Romanovsky 	return mlx5_cmd_dealloc_pd(mdev->mdev, mpd->pdn, mpd->uid);
2576e126ba97SEli Cohen }
2577e126ba97SEli Cohen 
2578e126ba97SEli Cohen static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2579e126ba97SEli Cohen {
2580e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
258181e30880SYishai Hadas 	struct mlx5_ib_qp *mqp = to_mqp(ibqp);
2582e126ba97SEli Cohen 	int err;
2583539ec982SYishai Hadas 	u16 uid;
2584539ec982SYishai Hadas 
2585539ec982SYishai Hadas 	uid = ibqp->pd ?
2586539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2587e126ba97SEli Cohen 
25882be08c30SLeon Romanovsky 	if (mqp->flags & IB_QP_CREATE_SOURCE_QPN) {
258981e30880SYishai Hadas 		mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
259081e30880SYishai Hadas 		return -EOPNOTSUPP;
259181e30880SYishai Hadas 	}
259281e30880SYishai Hadas 
2593539ec982SYishai Hadas 	err = mlx5_cmd_attach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2594e126ba97SEli Cohen 	if (err)
2595e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
2596e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2597e126ba97SEli Cohen 
2598e126ba97SEli Cohen 	return err;
2599e126ba97SEli Cohen }
2600e126ba97SEli Cohen 
2601e126ba97SEli Cohen static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
2602e126ba97SEli Cohen {
2603e126ba97SEli Cohen 	struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
2604e126ba97SEli Cohen 	int err;
2605539ec982SYishai Hadas 	u16 uid;
2606e126ba97SEli Cohen 
2607539ec982SYishai Hadas 	uid = ibqp->pd ?
2608539ec982SYishai Hadas 		to_mpd(ibqp->pd)->uid : 0;
2609539ec982SYishai Hadas 	err = mlx5_cmd_detach_mcg(dev->mdev, gid, ibqp->qp_num, uid);
2610e126ba97SEli Cohen 	if (err)
2611e126ba97SEli Cohen 		mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
2612e126ba97SEli Cohen 			     ibqp->qp_num, gid->raw);
2613e126ba97SEli Cohen 
2614e126ba97SEli Cohen 	return err;
2615e126ba97SEli Cohen }
2616e126ba97SEli Cohen 
2617e126ba97SEli Cohen static int init_node_data(struct mlx5_ib_dev *dev)
2618e126ba97SEli Cohen {
26191b5daf11SMajd Dibbiny 	int err;
2620e126ba97SEli Cohen 
26211b5daf11SMajd Dibbiny 	err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
2622e126ba97SEli Cohen 	if (err)
2623e126ba97SEli Cohen 		return err;
26241b5daf11SMajd Dibbiny 
26251b5daf11SMajd Dibbiny 	dev->mdev->rev_id = dev->mdev->pdev->revision;
26261b5daf11SMajd Dibbiny 
26271b5daf11SMajd Dibbiny 	return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
2628e126ba97SEli Cohen }
2629e126ba97SEli Cohen 
2630508a523fSParav Pandit static ssize_t fw_pages_show(struct device *device,
2631508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2632e126ba97SEli Cohen {
2633e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
263454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2635e126ba97SEli Cohen 
26361c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", dev->mdev->priv.fw_pages);
2637e126ba97SEli Cohen }
2638508a523fSParav Pandit static DEVICE_ATTR_RO(fw_pages);
2639e126ba97SEli Cohen 
2640508a523fSParav Pandit static ssize_t reg_pages_show(struct device *device,
2641e126ba97SEli Cohen 			      struct device_attribute *attr, char *buf)
2642e126ba97SEli Cohen {
2643e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
264454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
2645e126ba97SEli Cohen 
26461c7fd726SJoe Perches 	return sysfs_emit(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
2647e126ba97SEli Cohen }
2648508a523fSParav Pandit static DEVICE_ATTR_RO(reg_pages);
2649e126ba97SEli Cohen 
2650508a523fSParav Pandit static ssize_t hca_type_show(struct device *device,
2651508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2652e126ba97SEli Cohen {
2653e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
265454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
265554747231SParav Pandit 
26561c7fd726SJoe Perches 	return sysfs_emit(buf, "MT%d\n", dev->mdev->pdev->device);
2657e126ba97SEli Cohen }
2658508a523fSParav Pandit static DEVICE_ATTR_RO(hca_type);
2659e126ba97SEli Cohen 
2660508a523fSParav Pandit static ssize_t hw_rev_show(struct device *device,
2661508a523fSParav Pandit 			   struct device_attribute *attr, char *buf)
2662e126ba97SEli Cohen {
2663e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
266454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
266554747231SParav Pandit 
26661c7fd726SJoe Perches 	return sysfs_emit(buf, "%x\n", dev->mdev->rev_id);
2667e126ba97SEli Cohen }
2668508a523fSParav Pandit static DEVICE_ATTR_RO(hw_rev);
2669e126ba97SEli Cohen 
2670508a523fSParav Pandit static ssize_t board_id_show(struct device *device,
2671508a523fSParav Pandit 			     struct device_attribute *attr, char *buf)
2672e126ba97SEli Cohen {
2673e126ba97SEli Cohen 	struct mlx5_ib_dev *dev =
267454747231SParav Pandit 		rdma_device_to_drv_device(device, struct mlx5_ib_dev, ib_dev);
267554747231SParav Pandit 
26761c7fd726SJoe Perches 	return sysfs_emit(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
26779603b61dSJack Morgenstein 			  dev->mdev->board_id);
2678e126ba97SEli Cohen }
2679508a523fSParav Pandit static DEVICE_ATTR_RO(board_id);
2680e126ba97SEli Cohen 
2681508a523fSParav Pandit static struct attribute *mlx5_class_attributes[] = {
2682508a523fSParav Pandit 	&dev_attr_hw_rev.attr,
2683508a523fSParav Pandit 	&dev_attr_hca_type.attr,
2684508a523fSParav Pandit 	&dev_attr_board_id.attr,
2685508a523fSParav Pandit 	&dev_attr_fw_pages.attr,
2686508a523fSParav Pandit 	&dev_attr_reg_pages.attr,
2687508a523fSParav Pandit 	NULL,
2688508a523fSParav Pandit };
2689e126ba97SEli Cohen 
2690508a523fSParav Pandit static const struct attribute_group mlx5_attr_group = {
2691508a523fSParav Pandit 	.attrs = mlx5_class_attributes,
2692e126ba97SEli Cohen };
2693e126ba97SEli Cohen 
26947722f47eSHaggai Eran static void pkey_change_handler(struct work_struct *work)
26957722f47eSHaggai Eran {
26967722f47eSHaggai Eran 	struct mlx5_ib_port_resources *ports =
26977722f47eSHaggai Eran 		container_of(work, struct mlx5_ib_port_resources,
26987722f47eSHaggai Eran 			     pkey_change_work);
26997722f47eSHaggai Eran 
27007722f47eSHaggai Eran 	mlx5_ib_gsi_pkey_change(ports->gsi);
27017722f47eSHaggai Eran }
27027722f47eSHaggai Eran 
270389ea94a7SMaor Gottlieb static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
270489ea94a7SMaor Gottlieb {
270589ea94a7SMaor Gottlieb 	struct mlx5_ib_qp *mqp;
270689ea94a7SMaor Gottlieb 	struct mlx5_ib_cq *send_mcq, *recv_mcq;
270789ea94a7SMaor Gottlieb 	struct mlx5_core_cq *mcq;
270889ea94a7SMaor Gottlieb 	struct list_head cq_armed_list;
270989ea94a7SMaor Gottlieb 	unsigned long flags_qp;
271089ea94a7SMaor Gottlieb 	unsigned long flags_cq;
271189ea94a7SMaor Gottlieb 	unsigned long flags;
271289ea94a7SMaor Gottlieb 
271389ea94a7SMaor Gottlieb 	INIT_LIST_HEAD(&cq_armed_list);
271489ea94a7SMaor Gottlieb 
271589ea94a7SMaor Gottlieb 	/* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
271689ea94a7SMaor Gottlieb 	spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
271789ea94a7SMaor Gottlieb 	list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
271889ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->sq.lock, flags_qp);
271989ea94a7SMaor Gottlieb 		if (mqp->sq.tail != mqp->sq.head) {
272089ea94a7SMaor Gottlieb 			send_mcq = to_mcq(mqp->ibqp.send_cq);
272189ea94a7SMaor Gottlieb 			spin_lock_irqsave(&send_mcq->lock, flags_cq);
272289ea94a7SMaor Gottlieb 			if (send_mcq->mcq.comp &&
272389ea94a7SMaor Gottlieb 			    mqp->ibqp.send_cq->comp_handler) {
272489ea94a7SMaor Gottlieb 				if (!send_mcq->mcq.reset_notify_added) {
272589ea94a7SMaor Gottlieb 					send_mcq->mcq.reset_notify_added = 1;
272689ea94a7SMaor Gottlieb 					list_add_tail(&send_mcq->mcq.reset_notify,
272789ea94a7SMaor Gottlieb 						      &cq_armed_list);
272889ea94a7SMaor Gottlieb 				}
272989ea94a7SMaor Gottlieb 			}
273089ea94a7SMaor Gottlieb 			spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
273189ea94a7SMaor Gottlieb 		}
273289ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
273389ea94a7SMaor Gottlieb 		spin_lock_irqsave(&mqp->rq.lock, flags_qp);
273489ea94a7SMaor Gottlieb 		/* no handling is needed for SRQ */
273589ea94a7SMaor Gottlieb 		if (!mqp->ibqp.srq) {
273689ea94a7SMaor Gottlieb 			if (mqp->rq.tail != mqp->rq.head) {
273789ea94a7SMaor Gottlieb 				recv_mcq = to_mcq(mqp->ibqp.recv_cq);
273889ea94a7SMaor Gottlieb 				spin_lock_irqsave(&recv_mcq->lock, flags_cq);
273989ea94a7SMaor Gottlieb 				if (recv_mcq->mcq.comp &&
274089ea94a7SMaor Gottlieb 				    mqp->ibqp.recv_cq->comp_handler) {
274189ea94a7SMaor Gottlieb 					if (!recv_mcq->mcq.reset_notify_added) {
274289ea94a7SMaor Gottlieb 						recv_mcq->mcq.reset_notify_added = 1;
274389ea94a7SMaor Gottlieb 						list_add_tail(&recv_mcq->mcq.reset_notify,
274489ea94a7SMaor Gottlieb 							      &cq_armed_list);
274589ea94a7SMaor Gottlieb 					}
274689ea94a7SMaor Gottlieb 				}
274789ea94a7SMaor Gottlieb 				spin_unlock_irqrestore(&recv_mcq->lock,
274889ea94a7SMaor Gottlieb 						       flags_cq);
274989ea94a7SMaor Gottlieb 			}
275089ea94a7SMaor Gottlieb 		}
275189ea94a7SMaor Gottlieb 		spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
275289ea94a7SMaor Gottlieb 	}
275389ea94a7SMaor Gottlieb 	/*At that point all inflight post send were put to be executed as of we
275489ea94a7SMaor Gottlieb 	 * lock/unlock above locks Now need to arm all involved CQs.
275589ea94a7SMaor Gottlieb 	 */
275689ea94a7SMaor Gottlieb 	list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
27574e0e2ea1SYishai Hadas 		mcq->comp(mcq, NULL);
275889ea94a7SMaor Gottlieb 	}
275989ea94a7SMaor Gottlieb 	spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
276089ea94a7SMaor Gottlieb }
276189ea94a7SMaor Gottlieb 
276203404e8aSMaor Gottlieb static void delay_drop_handler(struct work_struct *work)
276303404e8aSMaor Gottlieb {
276403404e8aSMaor Gottlieb 	int err;
276503404e8aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop =
276603404e8aSMaor Gottlieb 		container_of(work, struct mlx5_ib_delay_drop,
276703404e8aSMaor Gottlieb 			     delay_drop_work);
276803404e8aSMaor Gottlieb 
2769fe248c3aSMaor Gottlieb 	atomic_inc(&delay_drop->events_cnt);
2770fe248c3aSMaor Gottlieb 
277103404e8aSMaor Gottlieb 	mutex_lock(&delay_drop->lock);
2772333fbaa0SLeon Romanovsky 	err = mlx5_core_set_delay_drop(delay_drop->dev, delay_drop->timeout);
277303404e8aSMaor Gottlieb 	if (err) {
277403404e8aSMaor Gottlieb 		mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
277503404e8aSMaor Gottlieb 			     delay_drop->timeout);
277603404e8aSMaor Gottlieb 		delay_drop->activate = false;
277703404e8aSMaor Gottlieb 	}
277803404e8aSMaor Gottlieb 	mutex_unlock(&delay_drop->lock);
277903404e8aSMaor Gottlieb }
278003404e8aSMaor Gottlieb 
278109e574faSSaeed Mahameed static void handle_general_event(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
278209e574faSSaeed Mahameed 				 struct ib_event *ibev)
278309e574faSSaeed Mahameed {
27846cfdc7e4SAya Levin 	u8 port = (eqe->data.port.port >> 4) & 0xf;
27856cfdc7e4SAya Levin 
278609e574faSSaeed Mahameed 	switch (eqe->sub_type) {
278709e574faSSaeed Mahameed 	case MLX5_GENERAL_SUBTYPE_DELAY_DROP_TIMEOUT:
27886cfdc7e4SAya Levin 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
27896cfdc7e4SAya Levin 					    IB_LINK_LAYER_ETHERNET)
279009e574faSSaeed Mahameed 			schedule_work(&ibdev->delay_drop.delay_drop_work);
279109e574faSSaeed Mahameed 		break;
279209e574faSSaeed Mahameed 	default: /* do nothing */
279309e574faSSaeed Mahameed 		return;
279409e574faSSaeed Mahameed 	}
279509e574faSSaeed Mahameed }
279609e574faSSaeed Mahameed 
2797134e9349SSaeed Mahameed static int handle_port_change(struct mlx5_ib_dev *ibdev, struct mlx5_eqe *eqe,
2798134e9349SSaeed Mahameed 			      struct ib_event *ibev)
2799134e9349SSaeed Mahameed {
2800134e9349SSaeed Mahameed 	u8 port = (eqe->data.port.port >> 4) & 0xf;
2801134e9349SSaeed Mahameed 
2802134e9349SSaeed Mahameed 	ibev->element.port_num = port;
2803134e9349SSaeed Mahameed 
2804134e9349SSaeed Mahameed 	switch (eqe->sub_type) {
2805134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE:
2806134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_DOWN:
2807134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_INITIALIZED:
2808134e9349SSaeed Mahameed 		/* In RoCE, port up/down events are handled in
2809134e9349SSaeed Mahameed 		 * mlx5_netdev_event().
2810134e9349SSaeed Mahameed 		 */
2811134e9349SSaeed Mahameed 		if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
2812134e9349SSaeed Mahameed 					    IB_LINK_LAYER_ETHERNET)
2813134e9349SSaeed Mahameed 			return -EINVAL;
2814134e9349SSaeed Mahameed 
2815134e9349SSaeed Mahameed 		ibev->event = (eqe->sub_type == MLX5_PORT_CHANGE_SUBTYPE_ACTIVE) ?
2816134e9349SSaeed Mahameed 				IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
2817134e9349SSaeed Mahameed 		break;
2818134e9349SSaeed Mahameed 
2819134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_LID:
2820134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_LID_CHANGE;
2821134e9349SSaeed Mahameed 		break;
2822134e9349SSaeed Mahameed 
2823134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_PKEY:
2824134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_PKEY_CHANGE;
2825134e9349SSaeed Mahameed 		schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
2826134e9349SSaeed Mahameed 		break;
2827134e9349SSaeed Mahameed 
2828134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_GUID:
2829134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_GID_CHANGE;
2830134e9349SSaeed Mahameed 		break;
2831134e9349SSaeed Mahameed 
2832134e9349SSaeed Mahameed 	case MLX5_PORT_CHANGE_SUBTYPE_CLIENT_REREG:
2833134e9349SSaeed Mahameed 		ibev->event = IB_EVENT_CLIENT_REREGISTER;
2834134e9349SSaeed Mahameed 		break;
2835134e9349SSaeed Mahameed 	default:
2836134e9349SSaeed Mahameed 		return -EINVAL;
2837134e9349SSaeed Mahameed 	}
2838134e9349SSaeed Mahameed 
2839134e9349SSaeed Mahameed 	return 0;
2840134e9349SSaeed Mahameed }
2841134e9349SSaeed Mahameed 
2842d69a24e0SDaniel Jurgens static void mlx5_ib_handle_event(struct work_struct *_work)
2843e126ba97SEli Cohen {
2844d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work =
2845d69a24e0SDaniel Jurgens 		container_of(_work, struct mlx5_ib_event_work, work);
2846d69a24e0SDaniel Jurgens 	struct mlx5_ib_dev *ibdev;
2847e126ba97SEli Cohen 	struct ib_event ibev;
2848dbaaff2aSEli Cohen 	bool fatal = false;
2849e126ba97SEli Cohen 
2850df097a27SSaeed Mahameed 	if (work->is_slave) {
2851df097a27SSaeed Mahameed 		ibdev = mlx5_ib_get_ibdev_from_mpi(work->mpi);
2852d69a24e0SDaniel Jurgens 		if (!ibdev)
2853d69a24e0SDaniel Jurgens 			goto out;
2854d69a24e0SDaniel Jurgens 	} else {
2855df097a27SSaeed Mahameed 		ibdev = work->dev;
2856d69a24e0SDaniel Jurgens 	}
2857d69a24e0SDaniel Jurgens 
2858d69a24e0SDaniel Jurgens 	switch (work->event) {
2859e126ba97SEli Cohen 	case MLX5_DEV_EVENT_SYS_ERROR:
2860e126ba97SEli Cohen 		ibev.event = IB_EVENT_DEVICE_FATAL;
286189ea94a7SMaor Gottlieb 		mlx5_ib_handle_internal_error(ibdev);
2862134e9349SSaeed Mahameed 		ibev.element.port_num  = (u8)(unsigned long)work->param;
2863dbaaff2aSEli Cohen 		fatal = true;
2864e126ba97SEli Cohen 		break;
2865134e9349SSaeed Mahameed 	case MLX5_EVENT_TYPE_PORT_CHANGE:
2866134e9349SSaeed Mahameed 		if (handle_port_change(ibdev, work->param, &ibev))
2867d69a24e0SDaniel Jurgens 			goto out;
2868e126ba97SEli Cohen 		break;
286909e574faSSaeed Mahameed 	case MLX5_EVENT_TYPE_GENERAL_EVENT:
287009e574faSSaeed Mahameed 		handle_general_event(ibdev, work->param, &ibev);
2871df561f66SGustavo A. R. Silva 		fallthrough;
2872bdc37924SSaeed Mahameed 	default:
287303404e8aSMaor Gottlieb 		goto out;
2874e126ba97SEli Cohen 	}
2875e126ba97SEli Cohen 
2876e126ba97SEli Cohen 	ibev.device = &ibdev->ib_dev;
2877e126ba97SEli Cohen 
2878134e9349SSaeed Mahameed 	if (!rdma_is_port_valid(&ibdev->ib_dev, ibev.element.port_num)) {
2879134e9349SSaeed Mahameed 		mlx5_ib_warn(ibdev, "warning: event on port %d\n",  ibev.element.port_num);
288003404e8aSMaor Gottlieb 		goto out;
2881a0c84c32SEli Cohen 	}
2882a0c84c32SEli Cohen 
2883e126ba97SEli Cohen 	if (ibdev->ib_active)
2884e126ba97SEli Cohen 		ib_dispatch_event(&ibev);
2885dbaaff2aSEli Cohen 
2886dbaaff2aSEli Cohen 	if (fatal)
2887dbaaff2aSEli Cohen 		ibdev->ib_active = false;
288803404e8aSMaor Gottlieb out:
2889d69a24e0SDaniel Jurgens 	kfree(work);
2890d69a24e0SDaniel Jurgens }
2891d69a24e0SDaniel Jurgens 
2892df097a27SSaeed Mahameed static int mlx5_ib_event(struct notifier_block *nb,
2893df097a27SSaeed Mahameed 			 unsigned long event, void *param)
2894d69a24e0SDaniel Jurgens {
2895d69a24e0SDaniel Jurgens 	struct mlx5_ib_event_work *work;
2896d69a24e0SDaniel Jurgens 
2897d69a24e0SDaniel Jurgens 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
289810bea9c8SLeon Romanovsky 	if (!work)
2899df097a27SSaeed Mahameed 		return NOTIFY_DONE;
290010bea9c8SLeon Romanovsky 
2901d69a24e0SDaniel Jurgens 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2902df097a27SSaeed Mahameed 	work->dev = container_of(nb, struct mlx5_ib_dev, mdev_events);
2903df097a27SSaeed Mahameed 	work->is_slave = false;
2904d69a24e0SDaniel Jurgens 	work->param = param;
2905d69a24e0SDaniel Jurgens 	work->event = event;
2906d69a24e0SDaniel Jurgens 
2907d69a24e0SDaniel Jurgens 	queue_work(mlx5_ib_event_wq, &work->work);
2908df097a27SSaeed Mahameed 
2909df097a27SSaeed Mahameed 	return NOTIFY_OK;
2910df097a27SSaeed Mahameed }
2911df097a27SSaeed Mahameed 
2912df097a27SSaeed Mahameed static int mlx5_ib_event_slave_port(struct notifier_block *nb,
2913df097a27SSaeed Mahameed 				    unsigned long event, void *param)
2914df097a27SSaeed Mahameed {
2915df097a27SSaeed Mahameed 	struct mlx5_ib_event_work *work;
2916df097a27SSaeed Mahameed 
2917df097a27SSaeed Mahameed 	work = kmalloc(sizeof(*work), GFP_ATOMIC);
2918df097a27SSaeed Mahameed 	if (!work)
2919df097a27SSaeed Mahameed 		return NOTIFY_DONE;
2920df097a27SSaeed Mahameed 
2921df097a27SSaeed Mahameed 	INIT_WORK(&work->work, mlx5_ib_handle_event);
2922df097a27SSaeed Mahameed 	work->mpi = container_of(nb, struct mlx5_ib_multiport_info, mdev_events);
2923df097a27SSaeed Mahameed 	work->is_slave = true;
2924df097a27SSaeed Mahameed 	work->param = param;
2925df097a27SSaeed Mahameed 	work->event = event;
2926df097a27SSaeed Mahameed 	queue_work(mlx5_ib_event_wq, &work->work);
2927df097a27SSaeed Mahameed 
2928df097a27SSaeed Mahameed 	return NOTIFY_OK;
2929d69a24e0SDaniel Jurgens }
2930d69a24e0SDaniel Jurgens 
2931c43f1112SMaor Gottlieb static int set_has_smi_cap(struct mlx5_ib_dev *dev)
2932c43f1112SMaor Gottlieb {
2933c43f1112SMaor Gottlieb 	struct mlx5_hca_vport_context vport_ctx;
2934c43f1112SMaor Gottlieb 	int err;
2935c43f1112SMaor Gottlieb 	int port;
2936c43f1112SMaor Gottlieb 
29373ce60f44SParav Pandit 	for (port = 1; port <= ARRAY_SIZE(dev->port_caps); port++) {
29383ce60f44SParav Pandit 		dev->port_caps[port - 1].has_smi = false;
2939c43f1112SMaor Gottlieb 		if (MLX5_CAP_GEN(dev->mdev, port_type) ==
2940c43f1112SMaor Gottlieb 		    MLX5_CAP_PORT_TYPE_IB) {
2941c43f1112SMaor Gottlieb 			if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
2942c43f1112SMaor Gottlieb 				err = mlx5_query_hca_vport_context(dev->mdev, 0,
2943c43f1112SMaor Gottlieb 								   port, 0,
2944c43f1112SMaor Gottlieb 								   &vport_ctx);
2945c43f1112SMaor Gottlieb 				if (err) {
2946c43f1112SMaor Gottlieb 					mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
2947c43f1112SMaor Gottlieb 						    port, err);
2948c43f1112SMaor Gottlieb 					return err;
2949c43f1112SMaor Gottlieb 				}
29503ce60f44SParav Pandit 				dev->port_caps[port - 1].has_smi =
2951c43f1112SMaor Gottlieb 					vport_ctx.has_smi;
2952c43f1112SMaor Gottlieb 			} else {
29533ce60f44SParav Pandit 				dev->port_caps[port - 1].has_smi = true;
2954c43f1112SMaor Gottlieb 			}
2955c43f1112SMaor Gottlieb 		}
2956c43f1112SMaor Gottlieb 	}
2957c43f1112SMaor Gottlieb 	return 0;
2958c43f1112SMaor Gottlieb }
2959c43f1112SMaor Gottlieb 
2960e126ba97SEli Cohen static void get_ext_port_caps(struct mlx5_ib_dev *dev)
2961e126ba97SEli Cohen {
296213179652SParav Pandit 	unsigned int port;
2963e126ba97SEli Cohen 
296413179652SParav Pandit 	rdma_for_each_port (&dev->ib_dev, port)
2965e126ba97SEli Cohen 		mlx5_query_ext_port_caps(dev, port);
2966e126ba97SEli Cohen }
2967e126ba97SEli Cohen 
29686e8484c5SMax Gurtovoy static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
29696e8484c5SMax Gurtovoy {
29706e8484c5SMax Gurtovoy 	switch (umr_fence_cap) {
29716e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_NONE:
29726e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_NONE;
29736e8484c5SMax Gurtovoy 	case MLX5_CAP_UMR_FENCE_SMALL:
29746e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_INITIATOR_SMALL;
29756e8484c5SMax Gurtovoy 	default:
29766e8484c5SMax Gurtovoy 		return MLX5_FENCE_MODE_STRONG_ORDERING;
29776e8484c5SMax Gurtovoy 	}
29786e8484c5SMax Gurtovoy }
29796e8484c5SMax Gurtovoy 
29801e2b5a90SLeon Romanovsky static int mlx5_ib_dev_res_init(struct mlx5_ib_dev *dev)
2981e126ba97SEli Cohen {
29821e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
2983e126ba97SEli Cohen 	struct ib_srq_init_attr attr;
298421a428a0SLeon Romanovsky 	struct ib_device *ibdev;
2985bcf4c1eaSMatan Barak 	struct ib_cq_init_attr cq_attr = {.cqe = 1};
29867722f47eSHaggai Eran 	int port;
2987e126ba97SEli Cohen 	int ret = 0;
2988e126ba97SEli Cohen 
298921a428a0SLeon Romanovsky 	ibdev = &dev->ib_dev;
2990e126ba97SEli Cohen 
2991f4375443SLeon Romanovsky 	if (!MLX5_CAP_GEN(dev->mdev, xrc))
2992f4375443SLeon Romanovsky 		return -EOPNOTSUPP;
2993f4375443SLeon Romanovsky 
2994d16e91daSHaggai Eran 	mutex_init(&devr->mutex);
2995d16e91daSHaggai Eran 
299621a428a0SLeon Romanovsky 	devr->p0 = rdma_zalloc_drv_obj(ibdev, ib_pd);
299721a428a0SLeon Romanovsky 	if (!devr->p0)
299821a428a0SLeon Romanovsky 		return -ENOMEM;
299921a428a0SLeon Romanovsky 
300021a428a0SLeon Romanovsky 	devr->p0->device  = ibdev;
3001e126ba97SEli Cohen 	devr->p0->uobject = NULL;
3002e126ba97SEli Cohen 	atomic_set(&devr->p0->usecnt, 0);
3003e126ba97SEli Cohen 
3004ff23dfa1SShamir Rabinovitch 	ret = mlx5_ib_alloc_pd(devr->p0, NULL);
300521a428a0SLeon Romanovsky 	if (ret)
300621a428a0SLeon Romanovsky 		goto error0;
300721a428a0SLeon Romanovsky 
3008e39afe3dSLeon Romanovsky 	devr->c0 = rdma_zalloc_drv_obj(ibdev, ib_cq);
3009e39afe3dSLeon Romanovsky 	if (!devr->c0) {
3010e39afe3dSLeon Romanovsky 		ret = -ENOMEM;
3011e126ba97SEli Cohen 		goto error1;
3012e126ba97SEli Cohen 	}
3013e39afe3dSLeon Romanovsky 
3014e126ba97SEli Cohen 	devr->c0->device = &dev->ib_dev;
3015e126ba97SEli Cohen 	atomic_set(&devr->c0->usecnt, 0);
3016e126ba97SEli Cohen 
3017e39afe3dSLeon Romanovsky 	ret = mlx5_ib_create_cq(devr->c0, &cq_attr, NULL);
3018e39afe3dSLeon Romanovsky 	if (ret)
3019e39afe3dSLeon Romanovsky 		goto err_create_cq;
3020e39afe3dSLeon Romanovsky 
3021f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn0, 0);
3022f4375443SLeon Romanovsky 	if (ret)
3023e126ba97SEli Cohen 		goto error2;
3024e126ba97SEli Cohen 
3025f4375443SLeon Romanovsky 	ret = mlx5_cmd_xrcd_alloc(dev->mdev, &devr->xrcdn1, 0);
3026f4375443SLeon Romanovsky 	if (ret)
3027e126ba97SEli Cohen 		goto error3;
3028e126ba97SEli Cohen 
3029e126ba97SEli Cohen 	memset(&attr, 0, sizeof(attr));
3030e126ba97SEli Cohen 	attr.attr.max_sge = 1;
3031e126ba97SEli Cohen 	attr.attr.max_wr = 1;
3032e126ba97SEli Cohen 	attr.srq_type = IB_SRQT_XRC;
30331a56ff6dSArtemy Kovalyov 	attr.ext.cq = devr->c0;
3034e126ba97SEli Cohen 
303568e326deSLeon Romanovsky 	devr->s0 = rdma_zalloc_drv_obj(ibdev, ib_srq);
303668e326deSLeon Romanovsky 	if (!devr->s0) {
303768e326deSLeon Romanovsky 		ret = -ENOMEM;
3038e126ba97SEli Cohen 		goto error4;
3039e126ba97SEli Cohen 	}
304068e326deSLeon Romanovsky 
3041e126ba97SEli Cohen 	devr->s0->device	= &dev->ib_dev;
3042e126ba97SEli Cohen 	devr->s0->pd		= devr->p0;
3043e126ba97SEli Cohen 	devr->s0->srq_type      = IB_SRQT_XRC;
30441a56ff6dSArtemy Kovalyov 	devr->s0->ext.cq	= devr->c0;
304568e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s0, &attr, NULL);
304668e326deSLeon Romanovsky 	if (ret)
304768e326deSLeon Romanovsky 		goto err_create;
304868e326deSLeon Romanovsky 
30491a56ff6dSArtemy Kovalyov 	atomic_inc(&devr->s0->ext.cq->usecnt);
3050e126ba97SEli Cohen 	atomic_inc(&devr->p0->usecnt);
3051e126ba97SEli Cohen 	atomic_set(&devr->s0->usecnt, 0);
3052e126ba97SEli Cohen 
30534aa17b28SHaggai Abramonvsky 	memset(&attr, 0, sizeof(attr));
30544aa17b28SHaggai Abramonvsky 	attr.attr.max_sge = 1;
30554aa17b28SHaggai Abramonvsky 	attr.attr.max_wr = 1;
30564aa17b28SHaggai Abramonvsky 	attr.srq_type = IB_SRQT_BASIC;
305768e326deSLeon Romanovsky 	devr->s1 = rdma_zalloc_drv_obj(ibdev, ib_srq);
305868e326deSLeon Romanovsky 	if (!devr->s1) {
305968e326deSLeon Romanovsky 		ret = -ENOMEM;
30604aa17b28SHaggai Abramonvsky 		goto error5;
30614aa17b28SHaggai Abramonvsky 	}
306268e326deSLeon Romanovsky 
30634aa17b28SHaggai Abramonvsky 	devr->s1->device	= &dev->ib_dev;
30644aa17b28SHaggai Abramonvsky 	devr->s1->pd		= devr->p0;
30654aa17b28SHaggai Abramonvsky 	devr->s1->srq_type      = IB_SRQT_BASIC;
30661a56ff6dSArtemy Kovalyov 	devr->s1->ext.cq	= devr->c0;
306768e326deSLeon Romanovsky 
306868e326deSLeon Romanovsky 	ret = mlx5_ib_create_srq(devr->s1, &attr, NULL);
306968e326deSLeon Romanovsky 	if (ret)
307068e326deSLeon Romanovsky 		goto error6;
307168e326deSLeon Romanovsky 
30724aa17b28SHaggai Abramonvsky 	atomic_inc(&devr->p0->usecnt);
30731a56ff6dSArtemy Kovalyov 	atomic_set(&devr->s1->usecnt, 0);
30744aa17b28SHaggai Abramonvsky 
3075f8225e34SLeon Romanovsky 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
30767722f47eSHaggai Eran 		INIT_WORK(&devr->ports[port].pkey_change_work,
30777722f47eSHaggai Eran 			  pkey_change_handler);
30787722f47eSHaggai Eran 
3079e126ba97SEli Cohen 	return 0;
3080e126ba97SEli Cohen 
308168e326deSLeon Romanovsky error6:
308268e326deSLeon Romanovsky 	kfree(devr->s1);
30834aa17b28SHaggai Abramonvsky error5:
3084c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
308568e326deSLeon Romanovsky err_create:
308668e326deSLeon Romanovsky 	kfree(devr->s0);
3087e126ba97SEli Cohen error4:
3088f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
3089e126ba97SEli Cohen error3:
3090f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
3091e126ba97SEli Cohen error2:
3092c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
3093e39afe3dSLeon Romanovsky err_create_cq:
3094e39afe3dSLeon Romanovsky 	kfree(devr->c0);
3095e126ba97SEli Cohen error1:
3096c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
3097e126ba97SEli Cohen error0:
309821a428a0SLeon Romanovsky 	kfree(devr->p0);
3099e126ba97SEli Cohen 	return ret;
3100e126ba97SEli Cohen }
3101e126ba97SEli Cohen 
31021e2b5a90SLeon Romanovsky static void mlx5_ib_dev_res_cleanup(struct mlx5_ib_dev *dev)
3103e126ba97SEli Cohen {
31041e2b5a90SLeon Romanovsky 	struct mlx5_ib_resources *devr = &dev->devr;
31057722f47eSHaggai Eran 	int port;
31067722f47eSHaggai Eran 
3107c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s1, NULL);
310868e326deSLeon Romanovsky 	kfree(devr->s1);
3109c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_srq(devr->s0, NULL);
311068e326deSLeon Romanovsky 	kfree(devr->s0);
3111f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn1, 0);
3112f4375443SLeon Romanovsky 	mlx5_cmd_xrcd_dealloc(dev->mdev, devr->xrcdn0, 0);
3113c4367a26SShamir Rabinovitch 	mlx5_ib_destroy_cq(devr->c0, NULL);
3114e39afe3dSLeon Romanovsky 	kfree(devr->c0);
3115c4367a26SShamir Rabinovitch 	mlx5_ib_dealloc_pd(devr->p0, NULL);
311621a428a0SLeon Romanovsky 	kfree(devr->p0);
31177722f47eSHaggai Eran 
31187722f47eSHaggai Eran 	/* Make sure no change P_Key work items are still executing */
31195d8f6a0eSMark Bloch 	for (port = 0; port < ARRAY_SIZE(devr->ports); ++port)
31207722f47eSHaggai Eran 		cancel_work_sync(&devr->ports[port].pkey_change_work);
3121e126ba97SEli Cohen }
3122e126ba97SEli Cohen 
3123b02289b3SArtemy Kovalyov static u32 get_core_cap_flags(struct ib_device *ibdev,
3124b02289b3SArtemy Kovalyov 			      struct mlx5_hca_vport_context *rep)
3125e53505a8SAchiad Shochat {
3126e53505a8SAchiad Shochat 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3127e53505a8SAchiad Shochat 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
3128e53505a8SAchiad Shochat 	u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
3129e53505a8SAchiad Shochat 	u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
313085c7c014SDaniel Jurgens 	bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
3131e53505a8SAchiad Shochat 	u32 ret = 0;
3132e53505a8SAchiad Shochat 
3133b02289b3SArtemy Kovalyov 	if (rep->grh_required)
3134b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
3135b02289b3SArtemy Kovalyov 
3136e53505a8SAchiad Shochat 	if (ll == IB_LINK_LAYER_INFINIBAND)
3137b02289b3SArtemy Kovalyov 		return ret | RDMA_CORE_PORT_IBA_IB;
3138e53505a8SAchiad Shochat 
313985c7c014SDaniel Jurgens 	if (raw_support)
3140b02289b3SArtemy Kovalyov 		ret |= RDMA_CORE_PORT_RAW_PACKET;
314172cd5717SOr Gerlitz 
3142e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
314372cd5717SOr Gerlitz 		return ret;
3144e53505a8SAchiad Shochat 
3145e53505a8SAchiad Shochat 	if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
314672cd5717SOr Gerlitz 		return ret;
3147e53505a8SAchiad Shochat 
3148e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
3149e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE;
3150e53505a8SAchiad Shochat 
3151e53505a8SAchiad Shochat 	if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
3152e53505a8SAchiad Shochat 		ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
3153e53505a8SAchiad Shochat 
3154e53505a8SAchiad Shochat 	return ret;
3155e53505a8SAchiad Shochat }
3156e53505a8SAchiad Shochat 
31577738613eSIra Weiny static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
31587738613eSIra Weiny 			       struct ib_port_immutable *immutable)
31597738613eSIra Weiny {
31607738613eSIra Weiny 	struct ib_port_attr attr;
3161ca5b91d6SOr Gerlitz 	struct mlx5_ib_dev *dev = to_mdev(ibdev);
3162ca5b91d6SOr Gerlitz 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
3163b02289b3SArtemy Kovalyov 	struct mlx5_hca_vport_context rep = {0};
31647738613eSIra Weiny 	int err;
31657738613eSIra Weiny 
3166c4550c63SOr Gerlitz 	err = ib_query_port(ibdev, port_num, &attr);
31677738613eSIra Weiny 	if (err)
31687738613eSIra Weiny 		return err;
31697738613eSIra Weiny 
3170b02289b3SArtemy Kovalyov 	if (ll == IB_LINK_LAYER_INFINIBAND) {
3171b02289b3SArtemy Kovalyov 		err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
3172b02289b3SArtemy Kovalyov 						   &rep);
3173b02289b3SArtemy Kovalyov 		if (err)
3174b02289b3SArtemy Kovalyov 			return err;
3175b02289b3SArtemy Kovalyov 	}
3176b02289b3SArtemy Kovalyov 
31777738613eSIra Weiny 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
31787738613eSIra Weiny 	immutable->gid_tbl_len = attr.gid_tbl_len;
3179b02289b3SArtemy Kovalyov 	immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
3180337877a4SIra Weiny 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
31817738613eSIra Weiny 
31827738613eSIra Weiny 	return 0;
31837738613eSIra Weiny }
31847738613eSIra Weiny 
31858e6efa3aSMark Bloch static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
31868e6efa3aSMark Bloch 				   struct ib_port_immutable *immutable)
31878e6efa3aSMark Bloch {
31888e6efa3aSMark Bloch 	struct ib_port_attr attr;
31898e6efa3aSMark Bloch 	int err;
31908e6efa3aSMark Bloch 
31918e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
31928e6efa3aSMark Bloch 
31938e6efa3aSMark Bloch 	err = ib_query_port(ibdev, port_num, &attr);
31948e6efa3aSMark Bloch 	if (err)
31958e6efa3aSMark Bloch 		return err;
31968e6efa3aSMark Bloch 
31978e6efa3aSMark Bloch 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
31988e6efa3aSMark Bloch 	immutable->gid_tbl_len = attr.gid_tbl_len;
31998e6efa3aSMark Bloch 	immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
32008e6efa3aSMark Bloch 
32018e6efa3aSMark Bloch 	return 0;
32028e6efa3aSMark Bloch }
32038e6efa3aSMark Bloch 
32049abb0d1bSLeon Romanovsky static void get_dev_fw_str(struct ib_device *ibdev, char *str)
3205c7342823SIra Weiny {
3206c7342823SIra Weiny 	struct mlx5_ib_dev *dev =
3207c7342823SIra Weiny 		container_of(ibdev, struct mlx5_ib_dev, ib_dev);
32089abb0d1bSLeon Romanovsky 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
32099abb0d1bSLeon Romanovsky 		 fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
32109abb0d1bSLeon Romanovsky 		 fw_rev_sub(dev->mdev));
3211c7342823SIra Weiny }
3212c7342823SIra Weiny 
321345f95acdSOr Gerlitz static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
32149ef9c640SAviv Heller {
32159ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
32169ef9c640SAviv Heller 	struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
32179ef9c640SAviv Heller 								 MLX5_FLOW_NAMESPACE_LAG);
32189ef9c640SAviv Heller 	struct mlx5_flow_table *ft;
32199ef9c640SAviv Heller 	int err;
32209ef9c640SAviv Heller 
32217c34ec19SAviv Heller 	if (!ns || !mlx5_lag_is_roce(mdev))
32229ef9c640SAviv Heller 		return 0;
32239ef9c640SAviv Heller 
32249ef9c640SAviv Heller 	err = mlx5_cmd_create_vport_lag(mdev);
32259ef9c640SAviv Heller 	if (err)
32269ef9c640SAviv Heller 		return err;
32279ef9c640SAviv Heller 
32289ef9c640SAviv Heller 	ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
32299ef9c640SAviv Heller 	if (IS_ERR(ft)) {
32309ef9c640SAviv Heller 		err = PTR_ERR(ft);
32319ef9c640SAviv Heller 		goto err_destroy_vport_lag;
32329ef9c640SAviv Heller 	}
32339ef9c640SAviv Heller 
32349a4ca38dSMark Bloch 	dev->flow_db->lag_demux_ft = ft;
32357c34ec19SAviv Heller 	dev->lag_active = true;
32369ef9c640SAviv Heller 	return 0;
32379ef9c640SAviv Heller 
32389ef9c640SAviv Heller err_destroy_vport_lag:
32399ef9c640SAviv Heller 	mlx5_cmd_destroy_vport_lag(mdev);
32409ef9c640SAviv Heller 	return err;
32419ef9c640SAviv Heller }
32429ef9c640SAviv Heller 
324345f95acdSOr Gerlitz static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
32449ef9c640SAviv Heller {
32459ef9c640SAviv Heller 	struct mlx5_core_dev *mdev = dev->mdev;
32469ef9c640SAviv Heller 
32477c34ec19SAviv Heller 	if (dev->lag_active) {
32487c34ec19SAviv Heller 		dev->lag_active = false;
32497c34ec19SAviv Heller 
32509a4ca38dSMark Bloch 		mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
32519a4ca38dSMark Bloch 		dev->flow_db->lag_demux_ft = NULL;
32529ef9c640SAviv Heller 
32539ef9c640SAviv Heller 		mlx5_cmd_destroy_vport_lag(mdev);
32549ef9c640SAviv Heller 	}
32559ef9c640SAviv Heller }
32569ef9c640SAviv Heller 
32577fd8aefbSDaniel Jurgens static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
3258fc24fc5eSAchiad Shochat {
3259e53505a8SAchiad Shochat 	int err;
3260e53505a8SAchiad Shochat 
326195579e78SMark Bloch 	dev->port[port_num].roce.nb.notifier_call = mlx5_netdev_event;
3262de641d74SParav Pandit 	err = register_netdevice_notifier(&dev->port[port_num].roce.nb);
32635ec8c83eSAviv Heller 	if (err) {
326495579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3265e53505a8SAchiad Shochat 		return err;
32665ec8c83eSAviv Heller 	}
3267e53505a8SAchiad Shochat 
3268d012f5d6SOr Gerlitz 	return 0;
3269d012f5d6SOr Gerlitz }
3270d012f5d6SOr Gerlitz 
32717fd8aefbSDaniel Jurgens static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
3272e126ba97SEli Cohen {
327395579e78SMark Bloch 	if (dev->port[port_num].roce.nb.notifier_call) {
3274de641d74SParav Pandit 		unregister_netdevice_notifier(&dev->port[port_num].roce.nb);
327595579e78SMark Bloch 		dev->port[port_num].roce.nb.notifier_call = NULL;
3276e126ba97SEli Cohen 	}
3277e126ba97SEli Cohen }
3278e126ba97SEli Cohen 
3279e3f1ed1fSLeon Romanovsky static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
3280e126ba97SEli Cohen {
3281e126ba97SEli Cohen 	int err;
3282e126ba97SEli Cohen 
3283e53505a8SAchiad Shochat 	err = mlx5_nic_vport_enable_roce(dev->mdev);
3284e53505a8SAchiad Shochat 	if (err)
32858e6efa3aSMark Bloch 		return err;
3286e53505a8SAchiad Shochat 
328745f95acdSOr Gerlitz 	err = mlx5_eth_lag_init(dev);
32889ef9c640SAviv Heller 	if (err)
32899ef9c640SAviv Heller 		goto err_disable_roce;
32909ef9c640SAviv Heller 
3291e53505a8SAchiad Shochat 	return 0;
3292e53505a8SAchiad Shochat 
32939ef9c640SAviv Heller err_disable_roce:
32949ef9c640SAviv Heller 	mlx5_nic_vport_disable_roce(dev->mdev);
32959ef9c640SAviv Heller 
3296e53505a8SAchiad Shochat 	return err;
3297fc24fc5eSAchiad Shochat }
3298fc24fc5eSAchiad Shochat 
329945f95acdSOr Gerlitz static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
3300fc24fc5eSAchiad Shochat {
330145f95acdSOr Gerlitz 	mlx5_eth_lag_cleanup(dev);
3302e53505a8SAchiad Shochat 	mlx5_nic_vport_disable_roce(dev->mdev);
3303fc24fc5eSAchiad Shochat }
3304fc24fc5eSAchiad Shochat 
3305f6a8a19bSDenis Drozdov static int mlx5_ib_rn_get_params(struct ib_device *device, u8 port_num,
3306693dfd5aSErez Shitrit 				 enum rdma_netdev_t type,
3307f6a8a19bSDenis Drozdov 				 struct rdma_netdev_alloc_params *params)
3308693dfd5aSErez Shitrit {
3309693dfd5aSErez Shitrit 	if (type != RDMA_NETDEV_IPOIB)
3310f6a8a19bSDenis Drozdov 		return -EOPNOTSUPP;
3311693dfd5aSErez Shitrit 
3312f6a8a19bSDenis Drozdov 	return mlx5_rdma_rn_get_params(to_mdev(device)->mdev, device, params);
3313693dfd5aSErez Shitrit }
3314693dfd5aSErez Shitrit 
3315fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
3316fe248c3aSMaor Gottlieb 				       size_t count, loff_t *pos)
3317fe248c3aSMaor Gottlieb {
3318fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3319fe248c3aSMaor Gottlieb 	char lbuf[20];
3320fe248c3aSMaor Gottlieb 	int len;
3321fe248c3aSMaor Gottlieb 
3322fe248c3aSMaor Gottlieb 	len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
3323fe248c3aSMaor Gottlieb 	return simple_read_from_buffer(buf, count, pos, lbuf, len);
3324fe248c3aSMaor Gottlieb }
3325fe248c3aSMaor Gottlieb 
3326fe248c3aSMaor Gottlieb static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
3327fe248c3aSMaor Gottlieb 					size_t count, loff_t *pos)
3328fe248c3aSMaor Gottlieb {
3329fe248c3aSMaor Gottlieb 	struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
3330fe248c3aSMaor Gottlieb 	u32 timeout;
3331fe248c3aSMaor Gottlieb 	u32 var;
3332fe248c3aSMaor Gottlieb 
3333fe248c3aSMaor Gottlieb 	if (kstrtouint_from_user(buf, count, 0, &var))
3334fe248c3aSMaor Gottlieb 		return -EFAULT;
3335fe248c3aSMaor Gottlieb 
3336fe248c3aSMaor Gottlieb 	timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
3337fe248c3aSMaor Gottlieb 			1000);
3338fe248c3aSMaor Gottlieb 	if (timeout != var)
3339fe248c3aSMaor Gottlieb 		mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
3340fe248c3aSMaor Gottlieb 			    timeout);
3341fe248c3aSMaor Gottlieb 
3342fe248c3aSMaor Gottlieb 	delay_drop->timeout = timeout;
3343fe248c3aSMaor Gottlieb 
3344fe248c3aSMaor Gottlieb 	return count;
3345fe248c3aSMaor Gottlieb }
3346fe248c3aSMaor Gottlieb 
3347fe248c3aSMaor Gottlieb static const struct file_operations fops_delay_drop_timeout = {
3348fe248c3aSMaor Gottlieb 	.owner	= THIS_MODULE,
3349fe248c3aSMaor Gottlieb 	.open	= simple_open,
3350fe248c3aSMaor Gottlieb 	.write	= delay_drop_timeout_write,
3351fe248c3aSMaor Gottlieb 	.read	= delay_drop_timeout_read,
3352fe248c3aSMaor Gottlieb };
3353fe248c3aSMaor Gottlieb 
335432f69e4bSDaniel Jurgens static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
335532f69e4bSDaniel Jurgens 				      struct mlx5_ib_multiport_info *mpi)
335632f69e4bSDaniel Jurgens {
335732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
335832f69e4bSDaniel Jurgens 	struct mlx5_ib_port *port = &ibdev->port[port_num];
335932f69e4bSDaniel Jurgens 	int comps;
336032f69e4bSDaniel Jurgens 	int err;
336132f69e4bSDaniel Jurgens 	int i;
336232f69e4bSDaniel Jurgens 
33639dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
33649dc4cfffSLeon Romanovsky 
3365a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
3366a9e546e7SParav Pandit 
336732f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
336832f69e4bSDaniel Jurgens 	if (!mpi->ibdev) {
336932f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
337032f69e4bSDaniel Jurgens 		return;
337132f69e4bSDaniel Jurgens 	}
3372df097a27SSaeed Mahameed 
337332f69e4bSDaniel Jurgens 	mpi->ibdev = NULL;
337432f69e4bSDaniel Jurgens 
337532f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
337623eaf3b5SLeon Romanovsky 	if (mpi->mdev_events.notifier_call)
337723eaf3b5SLeon Romanovsky 		mlx5_notifier_unregister(mpi->mdev, &mpi->mdev_events);
337823eaf3b5SLeon Romanovsky 	mpi->mdev_events.notifier_call = NULL;
337932f69e4bSDaniel Jurgens 	mlx5_remove_netdev_notifier(ibdev, port_num);
338032f69e4bSDaniel Jurgens 	spin_lock(&port->mp.mpi_lock);
338132f69e4bSDaniel Jurgens 
338232f69e4bSDaniel Jurgens 	comps = mpi->mdev_refcnt;
338332f69e4bSDaniel Jurgens 	if (comps) {
338432f69e4bSDaniel Jurgens 		mpi->unaffiliate = true;
338532f69e4bSDaniel Jurgens 		init_completion(&mpi->unref_comp);
338632f69e4bSDaniel Jurgens 		spin_unlock(&port->mp.mpi_lock);
338732f69e4bSDaniel Jurgens 
338832f69e4bSDaniel Jurgens 		for (i = 0; i < comps; i++)
338932f69e4bSDaniel Jurgens 			wait_for_completion(&mpi->unref_comp);
339032f69e4bSDaniel Jurgens 
339132f69e4bSDaniel Jurgens 		spin_lock(&port->mp.mpi_lock);
339232f69e4bSDaniel Jurgens 		mpi->unaffiliate = false;
339332f69e4bSDaniel Jurgens 	}
339432f69e4bSDaniel Jurgens 
339532f69e4bSDaniel Jurgens 	port->mp.mpi = NULL;
339632f69e4bSDaniel Jurgens 
339732f69e4bSDaniel Jurgens 	list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
339832f69e4bSDaniel Jurgens 
339932f69e4bSDaniel Jurgens 	spin_unlock(&port->mp.mpi_lock);
340032f69e4bSDaniel Jurgens 
340132f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
340232f69e4bSDaniel Jurgens 
340332f69e4bSDaniel Jurgens 	mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
340432f69e4bSDaniel Jurgens 	/* Log an error, still needed to cleanup the pointers and add
340532f69e4bSDaniel Jurgens 	 * it back to the list.
340632f69e4bSDaniel Jurgens 	 */
340732f69e4bSDaniel Jurgens 	if (err)
340832f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
340932f69e4bSDaniel Jurgens 			    port_num + 1);
341032f69e4bSDaniel Jurgens 
341195579e78SMark Bloch 	ibdev->port[port_num].roce.last_port_state = IB_PORT_DOWN;
341232f69e4bSDaniel Jurgens }
341332f69e4bSDaniel Jurgens 
341432f69e4bSDaniel Jurgens static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
341532f69e4bSDaniel Jurgens 				    struct mlx5_ib_multiport_info *mpi)
341632f69e4bSDaniel Jurgens {
341732f69e4bSDaniel Jurgens 	u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
341832f69e4bSDaniel Jurgens 	int err;
341932f69e4bSDaniel Jurgens 
34209dc4cfffSLeon Romanovsky 	lockdep_assert_held(&mlx5_ib_multiport_mutex);
34219dc4cfffSLeon Romanovsky 
342232f69e4bSDaniel Jurgens 	spin_lock(&ibdev->port[port_num].mp.mpi_lock);
342332f69e4bSDaniel Jurgens 	if (ibdev->port[port_num].mp.mpi) {
34242577188eSQing Huang 		mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
342532f69e4bSDaniel Jurgens 			    port_num + 1);
342632f69e4bSDaniel Jurgens 		spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
342732f69e4bSDaniel Jurgens 		return false;
342832f69e4bSDaniel Jurgens 	}
342932f69e4bSDaniel Jurgens 
343032f69e4bSDaniel Jurgens 	ibdev->port[port_num].mp.mpi = mpi;
343132f69e4bSDaniel Jurgens 	mpi->ibdev = ibdev;
3432df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = NULL;
343332f69e4bSDaniel Jurgens 	spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
343432f69e4bSDaniel Jurgens 
343532f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
343632f69e4bSDaniel Jurgens 	if (err)
343732f69e4bSDaniel Jurgens 		goto unbind;
343832f69e4bSDaniel Jurgens 
343932f69e4bSDaniel Jurgens 	err = mlx5_add_netdev_notifier(ibdev, port_num);
344032f69e4bSDaniel Jurgens 	if (err) {
344132f69e4bSDaniel Jurgens 		mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
344232f69e4bSDaniel Jurgens 			    port_num + 1);
344332f69e4bSDaniel Jurgens 		goto unbind;
344432f69e4bSDaniel Jurgens 	}
344532f69e4bSDaniel Jurgens 
3446df097a27SSaeed Mahameed 	mpi->mdev_events.notifier_call = mlx5_ib_event_slave_port;
3447df097a27SSaeed Mahameed 	mlx5_notifier_register(mpi->mdev, &mpi->mdev_events);
3448df097a27SSaeed Mahameed 
344973eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(ibdev, port_num);
3450a9e546e7SParav Pandit 
345132f69e4bSDaniel Jurgens 	return true;
345232f69e4bSDaniel Jurgens 
345332f69e4bSDaniel Jurgens unbind:
345432f69e4bSDaniel Jurgens 	mlx5_ib_unbind_slave_port(ibdev, mpi);
345532f69e4bSDaniel Jurgens 	return false;
345632f69e4bSDaniel Jurgens }
345732f69e4bSDaniel Jurgens 
345832f69e4bSDaniel Jurgens static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
345932f69e4bSDaniel Jurgens {
346032f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
346132f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
346232f69e4bSDaniel Jurgens 							  port_num + 1);
346332f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
346432f69e4bSDaniel Jurgens 	int err;
346532f69e4bSDaniel Jurgens 	int i;
346632f69e4bSDaniel Jurgens 
346732f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
346832f69e4bSDaniel Jurgens 		return 0;
346932f69e4bSDaniel Jurgens 
347032f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
347132f69e4bSDaniel Jurgens 						     &dev->sys_image_guid);
347232f69e4bSDaniel Jurgens 	if (err)
347332f69e4bSDaniel Jurgens 		return err;
347432f69e4bSDaniel Jurgens 
347532f69e4bSDaniel Jurgens 	err = mlx5_nic_vport_enable_roce(dev->mdev);
347632f69e4bSDaniel Jurgens 	if (err)
347732f69e4bSDaniel Jurgens 		return err;
347832f69e4bSDaniel Jurgens 
347932f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
348032f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
348132f69e4bSDaniel Jurgens 		bool bound = false;
348232f69e4bSDaniel Jurgens 
348332f69e4bSDaniel Jurgens 		/* build a stub multiport info struct for the native port. */
348432f69e4bSDaniel Jurgens 		if (i == port_num) {
348532f69e4bSDaniel Jurgens 			mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
348632f69e4bSDaniel Jurgens 			if (!mpi) {
348732f69e4bSDaniel Jurgens 				mutex_unlock(&mlx5_ib_multiport_mutex);
348832f69e4bSDaniel Jurgens 				mlx5_nic_vport_disable_roce(dev->mdev);
348932f69e4bSDaniel Jurgens 				return -ENOMEM;
349032f69e4bSDaniel Jurgens 			}
349132f69e4bSDaniel Jurgens 
349232f69e4bSDaniel Jurgens 			mpi->is_master = true;
349332f69e4bSDaniel Jurgens 			mpi->mdev = dev->mdev;
349432f69e4bSDaniel Jurgens 			mpi->sys_image_guid = dev->sys_image_guid;
349532f69e4bSDaniel Jurgens 			dev->port[i].mp.mpi = mpi;
349632f69e4bSDaniel Jurgens 			mpi->ibdev = dev;
349732f69e4bSDaniel Jurgens 			mpi = NULL;
349832f69e4bSDaniel Jurgens 			continue;
349932f69e4bSDaniel Jurgens 		}
350032f69e4bSDaniel Jurgens 
350132f69e4bSDaniel Jurgens 		list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
350232f69e4bSDaniel Jurgens 				    list) {
350332f69e4bSDaniel Jurgens 			if (dev->sys_image_guid == mpi->sys_image_guid &&
350432f69e4bSDaniel Jurgens 			    (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
350532f69e4bSDaniel Jurgens 				bound = mlx5_ib_bind_slave_port(dev, mpi);
350632f69e4bSDaniel Jurgens 			}
350732f69e4bSDaniel Jurgens 
350832f69e4bSDaniel Jurgens 			if (bound) {
3509c42260f1SVu Pham 				dev_dbg(mpi->mdev->device,
3510c42260f1SVu Pham 					"removing port from unaffiliated list.\n");
351132f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
351232f69e4bSDaniel Jurgens 				list_del(&mpi->list);
351332f69e4bSDaniel Jurgens 				break;
351432f69e4bSDaniel Jurgens 			}
351532f69e4bSDaniel Jurgens 		}
35167416790eSParav Pandit 		if (!bound)
351732f69e4bSDaniel Jurgens 			mlx5_ib_dbg(dev, "no free port found for port %d\n",
351832f69e4bSDaniel Jurgens 				    i + 1);
351932f69e4bSDaniel Jurgens 	}
352032f69e4bSDaniel Jurgens 
352132f69e4bSDaniel Jurgens 	list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
352232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
352332f69e4bSDaniel Jurgens 	return err;
352432f69e4bSDaniel Jurgens }
352532f69e4bSDaniel Jurgens 
352632f69e4bSDaniel Jurgens static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
352732f69e4bSDaniel Jurgens {
352832f69e4bSDaniel Jurgens 	int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
352932f69e4bSDaniel Jurgens 	enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
353032f69e4bSDaniel Jurgens 							  port_num + 1);
353132f69e4bSDaniel Jurgens 	int i;
353232f69e4bSDaniel Jurgens 
353332f69e4bSDaniel Jurgens 	if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
353432f69e4bSDaniel Jurgens 		return;
353532f69e4bSDaniel Jurgens 
353632f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
353732f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
353832f69e4bSDaniel Jurgens 		if (dev->port[i].mp.mpi) {
353932f69e4bSDaniel Jurgens 			/* Destroy the native port stub */
354032f69e4bSDaniel Jurgens 			if (i == port_num) {
354132f69e4bSDaniel Jurgens 				kfree(dev->port[i].mp.mpi);
354232f69e4bSDaniel Jurgens 				dev->port[i].mp.mpi = NULL;
354332f69e4bSDaniel Jurgens 			} else {
354432f69e4bSDaniel Jurgens 				mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
354532f69e4bSDaniel Jurgens 				mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
354632f69e4bSDaniel Jurgens 			}
354732f69e4bSDaniel Jurgens 		}
354832f69e4bSDaniel Jurgens 	}
354932f69e4bSDaniel Jurgens 
355032f69e4bSDaniel Jurgens 	mlx5_ib_dbg(dev, "removing from devlist\n");
355132f69e4bSDaniel Jurgens 	list_del(&dev->ib_dev_list);
355232f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
355332f69e4bSDaniel Jurgens 
355432f69e4bSDaniel Jurgens 	mlx5_nic_vport_disable_roce(dev->mdev);
355532f69e4bSDaniel Jurgens }
355632f69e4bSDaniel Jurgens 
3557342ee59dSYishai Hadas static int mmap_obj_cleanup(struct ib_uobject *uobject,
35587be76befSYishai Hadas 			    enum rdma_remove_reason why,
35597be76befSYishai Hadas 			    struct uverbs_attr_bundle *attrs)
35607be76befSYishai Hadas {
35617be76befSYishai Hadas 	struct mlx5_user_mmap_entry *obj = uobject->object;
35627be76befSYishai Hadas 
35637be76befSYishai Hadas 	rdma_user_mmap_entry_remove(&obj->rdma_entry);
35647be76befSYishai Hadas 	return 0;
35657be76befSYishai Hadas }
35667be76befSYishai Hadas 
3567342ee59dSYishai Hadas static int mlx5_rdma_user_mmap_entry_insert(struct mlx5_ib_ucontext *c,
3568342ee59dSYishai Hadas 					    struct mlx5_user_mmap_entry *entry,
3569342ee59dSYishai Hadas 					    size_t length)
3570342ee59dSYishai Hadas {
3571342ee59dSYishai Hadas 	return rdma_user_mmap_entry_insert_range(
3572342ee59dSYishai Hadas 		&c->ibucontext, &entry->rdma_entry, length,
3573342ee59dSYishai Hadas 		(MLX5_IB_MMAP_OFFSET_START << 16),
3574342ee59dSYishai Hadas 		((MLX5_IB_MMAP_OFFSET_END << 16) + (1UL << 16) - 1));
3575342ee59dSYishai Hadas }
3576342ee59dSYishai Hadas 
35777be76befSYishai Hadas static struct mlx5_user_mmap_entry *
35787be76befSYishai Hadas alloc_var_entry(struct mlx5_ib_ucontext *c)
35797be76befSYishai Hadas {
35807be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
35817be76befSYishai Hadas 	struct mlx5_var_table *var_table;
35827be76befSYishai Hadas 	u32 page_idx;
35837be76befSYishai Hadas 	int err;
35847be76befSYishai Hadas 
35857be76befSYishai Hadas 	var_table = &to_mdev(c->ibucontext.device)->var_table;
35867be76befSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
35877be76befSYishai Hadas 	if (!entry)
35887be76befSYishai Hadas 		return ERR_PTR(-ENOMEM);
35897be76befSYishai Hadas 
35907be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
35917be76befSYishai Hadas 	page_idx = find_first_zero_bit(var_table->bitmap,
35927be76befSYishai Hadas 				       var_table->num_var_hw_entries);
35937be76befSYishai Hadas 	if (page_idx >= var_table->num_var_hw_entries) {
35947be76befSYishai Hadas 		err = -ENOSPC;
35957be76befSYishai Hadas 		mutex_unlock(&var_table->bitmap_lock);
35967be76befSYishai Hadas 		goto end;
35977be76befSYishai Hadas 	}
35987be76befSYishai Hadas 
35997be76befSYishai Hadas 	set_bit(page_idx, var_table->bitmap);
36007be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
36017be76befSYishai Hadas 
36027be76befSYishai Hadas 	entry->address = var_table->hw_start_addr +
36037be76befSYishai Hadas 				(page_idx * var_table->stride_size);
36047be76befSYishai Hadas 	entry->page_idx = page_idx;
36057be76befSYishai Hadas 	entry->mmap_flag = MLX5_IB_MMAP_TYPE_VAR;
36067be76befSYishai Hadas 
3607342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry,
3608342ee59dSYishai Hadas 					       var_table->stride_size);
36097be76befSYishai Hadas 	if (err)
36107be76befSYishai Hadas 		goto err_insert;
36117be76befSYishai Hadas 
36127be76befSYishai Hadas 	return entry;
36137be76befSYishai Hadas 
36147be76befSYishai Hadas err_insert:
36157be76befSYishai Hadas 	mutex_lock(&var_table->bitmap_lock);
36167be76befSYishai Hadas 	clear_bit(page_idx, var_table->bitmap);
36177be76befSYishai Hadas 	mutex_unlock(&var_table->bitmap_lock);
36187be76befSYishai Hadas end:
36197be76befSYishai Hadas 	kfree(entry);
36207be76befSYishai Hadas 	return ERR_PTR(err);
36217be76befSYishai Hadas }
36227be76befSYishai Hadas 
36237be76befSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_VAR_OBJ_ALLOC)(
36247be76befSYishai Hadas 	struct uverbs_attr_bundle *attrs)
36257be76befSYishai Hadas {
36267be76befSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
36277be76befSYishai Hadas 		attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
36287be76befSYishai Hadas 	struct mlx5_ib_ucontext *c;
36297be76befSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
36307be76befSYishai Hadas 	u64 mmap_offset;
36317be76befSYishai Hadas 	u32 length;
36327be76befSYishai Hadas 	int err;
36337be76befSYishai Hadas 
36347be76befSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
36357be76befSYishai Hadas 	if (IS_ERR(c))
36367be76befSYishai Hadas 		return PTR_ERR(c);
36377be76befSYishai Hadas 
36387be76befSYishai Hadas 	entry = alloc_var_entry(c);
36397be76befSYishai Hadas 	if (IS_ERR(entry))
36407be76befSYishai Hadas 		return PTR_ERR(entry);
36417be76befSYishai Hadas 
36427be76befSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
36437be76befSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
36447be76befSYishai Hadas 	uobj->object = entry;
36450ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE);
36467be76befSYishai Hadas 
36477be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
36487be76befSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
36497be76befSYishai Hadas 	if (err)
36500ac8903cSJason Gunthorpe 		return err;
36517be76befSYishai Hadas 
36527be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
36537be76befSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
36547be76befSYishai Hadas 	if (err)
36550ac8903cSJason Gunthorpe 		return err;
36567be76befSYishai Hadas 
36577be76befSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
36587be76befSYishai Hadas 			     &length, sizeof(length));
36597be76befSYishai Hadas 	return err;
36607be76befSYishai Hadas }
36617be76befSYishai Hadas 
36627be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
36637be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_ALLOC,
36647be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_ALLOC_HANDLE,
36657be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
36667be76befSYishai Hadas 			UVERBS_ACCESS_NEW,
36677be76befSYishai Hadas 			UA_MANDATORY),
36687be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_PAGE_ID,
36697be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
36707be76befSYishai Hadas 			   UA_MANDATORY),
36717be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_LENGTH,
36727be76befSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
36737be76befSYishai Hadas 			   UA_MANDATORY),
36747be76befSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_VAR_OBJ_ALLOC_MMAP_OFFSET,
36757be76befSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
36767be76befSYishai Hadas 			    UA_MANDATORY));
36777be76befSYishai Hadas 
36787be76befSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
36797be76befSYishai Hadas 	MLX5_IB_METHOD_VAR_OBJ_DESTROY,
36807be76befSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_VAR_OBJ_DESTROY_HANDLE,
36817be76befSYishai Hadas 			MLX5_IB_OBJECT_VAR,
36827be76befSYishai Hadas 			UVERBS_ACCESS_DESTROY,
36837be76befSYishai Hadas 			UA_MANDATORY));
36847be76befSYishai Hadas 
36857be76befSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_VAR,
3686342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
36877be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_ALLOC),
36887be76befSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_VAR_OBJ_DESTROY));
36897be76befSYishai Hadas 
36907be76befSYishai Hadas static bool var_is_supported(struct ib_device *device)
36917be76befSYishai Hadas {
36927be76befSYishai Hadas 	struct mlx5_ib_dev *dev = to_mdev(device);
36937be76befSYishai Hadas 
36947be76befSYishai Hadas 	return (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
36957be76befSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q);
36967be76befSYishai Hadas }
36977be76befSYishai Hadas 
3698342ee59dSYishai Hadas static struct mlx5_user_mmap_entry *
3699342ee59dSYishai Hadas alloc_uar_entry(struct mlx5_ib_ucontext *c,
3700342ee59dSYishai Hadas 		enum mlx5_ib_uapi_uar_alloc_type alloc_type)
3701342ee59dSYishai Hadas {
3702342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3703342ee59dSYishai Hadas 	struct mlx5_ib_dev *dev;
3704342ee59dSYishai Hadas 	u32 uar_index;
3705342ee59dSYishai Hadas 	int err;
3706342ee59dSYishai Hadas 
3707342ee59dSYishai Hadas 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
3708342ee59dSYishai Hadas 	if (!entry)
3709342ee59dSYishai Hadas 		return ERR_PTR(-ENOMEM);
3710342ee59dSYishai Hadas 
3711342ee59dSYishai Hadas 	dev = to_mdev(c->ibucontext.device);
3712342ee59dSYishai Hadas 	err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
3713342ee59dSYishai Hadas 	if (err)
3714342ee59dSYishai Hadas 		goto end;
3715342ee59dSYishai Hadas 
3716342ee59dSYishai Hadas 	entry->page_idx = uar_index;
3717342ee59dSYishai Hadas 	entry->address = uar_index2paddress(dev, uar_index);
3718342ee59dSYishai Hadas 	if (alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3719342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_WC;
3720342ee59dSYishai Hadas 	else
3721342ee59dSYishai Hadas 		entry->mmap_flag = MLX5_IB_MMAP_TYPE_UAR_NC;
3722342ee59dSYishai Hadas 
3723342ee59dSYishai Hadas 	err = mlx5_rdma_user_mmap_entry_insert(c, entry, PAGE_SIZE);
3724342ee59dSYishai Hadas 	if (err)
3725342ee59dSYishai Hadas 		goto err_insert;
3726342ee59dSYishai Hadas 
3727342ee59dSYishai Hadas 	return entry;
3728342ee59dSYishai Hadas 
3729342ee59dSYishai Hadas err_insert:
3730342ee59dSYishai Hadas 	mlx5_cmd_free_uar(dev->mdev, uar_index);
3731342ee59dSYishai Hadas end:
3732342ee59dSYishai Hadas 	kfree(entry);
3733342ee59dSYishai Hadas 	return ERR_PTR(err);
3734342ee59dSYishai Hadas }
3735342ee59dSYishai Hadas 
3736342ee59dSYishai Hadas static int UVERBS_HANDLER(MLX5_IB_METHOD_UAR_OBJ_ALLOC)(
3737342ee59dSYishai Hadas 	struct uverbs_attr_bundle *attrs)
3738342ee59dSYishai Hadas {
3739342ee59dSYishai Hadas 	struct ib_uobject *uobj = uverbs_attr_get_uobject(
3740342ee59dSYishai Hadas 		attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3741342ee59dSYishai Hadas 	enum mlx5_ib_uapi_uar_alloc_type alloc_type;
3742342ee59dSYishai Hadas 	struct mlx5_ib_ucontext *c;
3743342ee59dSYishai Hadas 	struct mlx5_user_mmap_entry *entry;
3744342ee59dSYishai Hadas 	u64 mmap_offset;
3745342ee59dSYishai Hadas 	u32 length;
3746342ee59dSYishai Hadas 	int err;
3747342ee59dSYishai Hadas 
3748342ee59dSYishai Hadas 	c = to_mucontext(ib_uverbs_get_ucontext(attrs));
3749342ee59dSYishai Hadas 	if (IS_ERR(c))
3750342ee59dSYishai Hadas 		return PTR_ERR(c);
3751342ee59dSYishai Hadas 
3752342ee59dSYishai Hadas 	err = uverbs_get_const(&alloc_type, attrs,
3753342ee59dSYishai Hadas 			       MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE);
3754342ee59dSYishai Hadas 	if (err)
3755342ee59dSYishai Hadas 		return err;
3756342ee59dSYishai Hadas 
3757342ee59dSYishai Hadas 	if (alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF &&
3758342ee59dSYishai Hadas 	    alloc_type != MLX5_IB_UAPI_UAR_ALLOC_TYPE_NC)
3759342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3760342ee59dSYishai Hadas 
3761342ee59dSYishai Hadas 	if (!to_mdev(c->ibucontext.device)->wc_support &&
3762342ee59dSYishai Hadas 	    alloc_type == MLX5_IB_UAPI_UAR_ALLOC_TYPE_BF)
3763342ee59dSYishai Hadas 		return -EOPNOTSUPP;
3764342ee59dSYishai Hadas 
3765342ee59dSYishai Hadas 	entry = alloc_uar_entry(c, alloc_type);
3766342ee59dSYishai Hadas 	if (IS_ERR(entry))
3767342ee59dSYishai Hadas 		return PTR_ERR(entry);
3768342ee59dSYishai Hadas 
3769342ee59dSYishai Hadas 	mmap_offset = mlx5_entry_to_mmap_offset(entry);
3770342ee59dSYishai Hadas 	length = entry->rdma_entry.npages * PAGE_SIZE;
3771342ee59dSYishai Hadas 	uobj->object = entry;
37720ac8903cSJason Gunthorpe 	uverbs_finalize_uobj_create(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE);
3773342ee59dSYishai Hadas 
3774342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3775342ee59dSYishai Hadas 			     &mmap_offset, sizeof(mmap_offset));
3776342ee59dSYishai Hadas 	if (err)
37770ac8903cSJason Gunthorpe 		return err;
3778342ee59dSYishai Hadas 
3779342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3780342ee59dSYishai Hadas 			     &entry->page_idx, sizeof(entry->page_idx));
3781342ee59dSYishai Hadas 	if (err)
37820ac8903cSJason Gunthorpe 		return err;
3783342ee59dSYishai Hadas 
3784342ee59dSYishai Hadas 	err = uverbs_copy_to(attrs, MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3785342ee59dSYishai Hadas 			     &length, sizeof(length));
3786342ee59dSYishai Hadas 	return err;
3787342ee59dSYishai Hadas }
3788342ee59dSYishai Hadas 
3789342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD(
3790342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_ALLOC,
3791342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_ALLOC_HANDLE,
3792342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3793342ee59dSYishai Hadas 			UVERBS_ACCESS_NEW,
3794342ee59dSYishai Hadas 			UA_MANDATORY),
3795342ee59dSYishai Hadas 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_UAR_OBJ_ALLOC_TYPE,
3796342ee59dSYishai Hadas 			     enum mlx5_ib_uapi_uar_alloc_type,
3797342ee59dSYishai Hadas 			     UA_MANDATORY),
3798342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_PAGE_ID,
3799342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3800342ee59dSYishai Hadas 			   UA_MANDATORY),
3801342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_LENGTH,
3802342ee59dSYishai Hadas 			   UVERBS_ATTR_TYPE(u32),
3803342ee59dSYishai Hadas 			   UA_MANDATORY),
3804342ee59dSYishai Hadas 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_UAR_OBJ_ALLOC_MMAP_OFFSET,
3805342ee59dSYishai Hadas 			    UVERBS_ATTR_TYPE(u64),
3806342ee59dSYishai Hadas 			    UA_MANDATORY));
3807342ee59dSYishai Hadas 
3808342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_METHOD_DESTROY(
3809342ee59dSYishai Hadas 	MLX5_IB_METHOD_UAR_OBJ_DESTROY,
3810342ee59dSYishai Hadas 	UVERBS_ATTR_IDR(MLX5_IB_ATTR_UAR_OBJ_DESTROY_HANDLE,
3811342ee59dSYishai Hadas 			MLX5_IB_OBJECT_UAR,
3812342ee59dSYishai Hadas 			UVERBS_ACCESS_DESTROY,
3813342ee59dSYishai Hadas 			UA_MANDATORY));
3814342ee59dSYishai Hadas 
3815342ee59dSYishai Hadas DECLARE_UVERBS_NAMED_OBJECT(MLX5_IB_OBJECT_UAR,
3816342ee59dSYishai Hadas 			    UVERBS_TYPE_ALLOC_IDR(mmap_obj_cleanup),
3817342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_ALLOC),
3818342ee59dSYishai Hadas 			    &UVERBS_METHOD(MLX5_IB_METHOD_UAR_OBJ_DESTROY));
3819342ee59dSYishai Hadas 
38209a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
38219a119cd5SJason Gunthorpe 	mlx5_ib_dm,
38229a119cd5SJason Gunthorpe 	UVERBS_OBJECT_DM,
382324da0016SAriel Levkovich 	UVERBS_METHOD_DM_ALLOC,
38249a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
382524da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u64),
382683bb4442SJason Gunthorpe 			    UA_MANDATORY),
38279a119cd5SJason Gunthorpe 	UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
382824da0016SAriel Levkovich 			    UVERBS_ATTR_TYPE(u16),
38293b113a1eSAriel Levkovich 			    UA_OPTIONAL),
38303b113a1eSAriel Levkovich 	UVERBS_ATTR_CONST_IN(MLX5_IB_ATTR_ALLOC_DM_REQ_TYPE,
38313b113a1eSAriel Levkovich 			     enum mlx5_ib_uapi_dm_type,
38323b113a1eSAriel Levkovich 			     UA_OPTIONAL));
383324da0016SAriel Levkovich 
38349a119cd5SJason Gunthorpe ADD_UVERBS_ATTRIBUTES_SIMPLE(
38359a119cd5SJason Gunthorpe 	mlx5_ib_flow_action,
38369a119cd5SJason Gunthorpe 	UVERBS_OBJECT_FLOW_ACTION,
3837c6475a0bSAviad Yehezkel 	UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
3838bccd0622SJason Gunthorpe 	UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
3839bccd0622SJason Gunthorpe 			     enum mlx5_ib_uapi_flow_action_flags));
3840c6475a0bSAviad Yehezkel 
38410fb556b2SYishai Hadas ADD_UVERBS_ATTRIBUTES_SIMPLE(
38420fb556b2SYishai Hadas 	mlx5_ib_query_context,
38430fb556b2SYishai Hadas 	UVERBS_OBJECT_DEVICE,
38440fb556b2SYishai Hadas 	UVERBS_METHOD_QUERY_CONTEXT,
38450fb556b2SYishai Hadas 	UVERBS_ATTR_PTR_OUT(
38460fb556b2SYishai Hadas 		MLX5_IB_ATTR_QUERY_CONTEXT_RESP_UCTX,
38470fb556b2SYishai Hadas 		UVERBS_ATTR_STRUCT(struct mlx5_ib_alloc_ucontext_resp,
38480fb556b2SYishai Hadas 				   dump_fill_mkey),
38490fb556b2SYishai Hadas 		UA_MANDATORY));
38500fb556b2SYishai Hadas 
38510cbf432dSJason Gunthorpe static const struct uapi_definition mlx5_ib_defs[] = {
385236e235c8SJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_devx_defs),
38530cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN(mlx5_ib_flow_defs),
385430f2fe40SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_qos_defs),
385505f71ef9SYishai Hadas 	UAPI_DEF_CHAIN(mlx5_ib_std_types_defs),
38568c84660bSMatan Barak 
38570cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_FLOW_ACTION,
38580cbf432dSJason Gunthorpe 				&mlx5_ib_flow_action),
38590cbf432dSJason Gunthorpe 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DM, &mlx5_ib_dm),
38600fb556b2SYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE(UVERBS_OBJECT_DEVICE, &mlx5_ib_query_context),
38617be76befSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_VAR,
38627be76befSYishai Hadas 				UAPI_DEF_IS_OBJ_SUPPORTED(var_is_supported)),
3863342ee59dSYishai Hadas 	UAPI_DEF_CHAIN_OBJ_TREE_NAMED(MLX5_IB_OBJECT_UAR),
38640cbf432dSJason Gunthorpe 	{}
38650cbf432dSJason Gunthorpe };
38668c84660bSMatan Barak 
3867fb652d32SMark Bloch static void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
3868e126ba97SEli Cohen {
386932f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3870806b101bSJason Gunthorpe 	WARN_ON(!xa_empty(&dev->odp_mkeys));
3871ab40530aSParav Pandit 	mutex_destroy(&dev->cap_mask_mutex);
387250211ec9SJason Gunthorpe 	WARN_ON(!xa_empty(&dev->sig_mrs));
38734056b12eSAriel Levkovich 	WARN_ON(!bitmap_empty(dev->dm.memic_alloc_pages, MLX5_MAX_MEMIC_PAGES));
387416c1975fSMark Bloch }
387516c1975fSMark Bloch 
3876fb652d32SMark Bloch static int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
387716c1975fSMark Bloch {
387816c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
3879e126ba97SEli Cohen 	int err;
388032f69e4bSDaniel Jurgens 	int i;
3881e126ba97SEli Cohen 
388213179652SParav Pandit 	dev->ib_dev.node_type = RDMA_NODE_IB_CA;
388313179652SParav Pandit 	dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
388413179652SParav Pandit 	dev->ib_dev.phys_port_cnt = dev->num_ports;
388513179652SParav Pandit 	dev->ib_dev.dev.parent = mdev->device;
388613179652SParav Pandit 	dev->ib_dev.lag_flags = RDMA_LAG_FLAGS_HASH_ALL_SLAVES;
388713179652SParav Pandit 
388832f69e4bSDaniel Jurgens 	for (i = 0; i < dev->num_ports; i++) {
388932f69e4bSDaniel Jurgens 		spin_lock_init(&dev->port[i].mp.mpi_lock);
389095579e78SMark Bloch 		rwlock_init(&dev->port[i].roce.netdev_lock);
3891d3b5cc1cSMark Bloch 		dev->port[i].roce.dev = dev;
3892d3b5cc1cSMark Bloch 		dev->port[i].roce.native_port_num = i + 1;
3893d3b5cc1cSMark Bloch 		dev->port[i].roce.last_port_state = IB_PORT_DOWN;
389432f69e4bSDaniel Jurgens 	}
389532f69e4bSDaniel Jurgens 
389600815752SMoni Shoua 	mlx5_ib_internal_fill_odp_caps(dev);
389700815752SMoni Shoua 
389832f69e4bSDaniel Jurgens 	err = mlx5_ib_init_multiport_master(dev);
3899e126ba97SEli Cohen 	if (err)
3900da796ccbSMark Bloch 		return err;
3901e126ba97SEli Cohen 
3902a989ea01SMark Bloch 	err = set_has_smi_cap(dev);
3903a989ea01SMark Bloch 	if (err)
39042cb091f6SParav Pandit 		goto err_mp;
3905e126ba97SEli Cohen 
39062019d70eSParav Pandit 	err = mlx5_query_max_pkeys(&dev->ib_dev, &dev->pkey_table_len);
39072019d70eSParav Pandit 	if (err)
39082019d70eSParav Pandit 		goto err_mp;
39092019d70eSParav Pandit 
39101b5daf11SMajd Dibbiny 	if (mlx5_use_mad_ifc(dev))
3911e126ba97SEli Cohen 		get_ext_port_caps(dev);
3912e126ba97SEli Cohen 
3913f2f3df55SSaeed Mahameed 	dev->ib_dev.num_comp_vectors    = mlx5_comp_vectors_count(mdev);
3914e126ba97SEli Cohen 
39153cc297dbSMark Bloch 	mutex_init(&dev->cap_mask_mutex);
39163cc297dbSMark Bloch 	INIT_LIST_HEAD(&dev->qp_list);
39173cc297dbSMark Bloch 	spin_lock_init(&dev->reset_flow_resource_lock);
3918806b101bSJason Gunthorpe 	xa_init(&dev->odp_mkeys);
391950211ec9SJason Gunthorpe 	xa_init(&dev->sig_mrs);
3920f743ff3bSSaeed Mahameed 	atomic_set(&dev->mkey_var, 0);
39213cc297dbSMark Bloch 
39223b113a1eSAriel Levkovich 	spin_lock_init(&dev->dm.lock);
39233b113a1eSAriel Levkovich 	dev->dm.dev = mdev;
392416c1975fSMark Bloch 	return 0;
392525c13324SAriel Levkovich 
392632f69e4bSDaniel Jurgens err_mp:
392732f69e4bSDaniel Jurgens 	mlx5_ib_cleanup_multiport_master(dev);
3928d286ac1dSParav Pandit 	return err;
392916c1975fSMark Bloch }
393016c1975fSMark Bloch 
3931ac47bf5eSLeon Romanovsky static int mlx5_ib_enable_driver(struct ib_device *dev)
39329a4ca38dSMark Bloch {
3933ac47bf5eSLeon Romanovsky 	struct mlx5_ib_dev *mdev = to_mdev(dev);
3934ac47bf5eSLeon Romanovsky 	int ret;
39359a4ca38dSMark Bloch 
3936ac47bf5eSLeon Romanovsky 	ret = mlx5_ib_test_wc(mdev);
3937ac47bf5eSLeon Romanovsky 	mlx5_ib_dbg(mdev, "Write-Combining %s",
3938ac47bf5eSLeon Romanovsky 		    mdev->wc_support ? "supported" : "not supported");
39399a4ca38dSMark Bloch 
3940ac47bf5eSLeon Romanovsky 	return ret;
39419a4ca38dSMark Bloch }
39429a4ca38dSMark Bloch 
394396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ops = {
39447a154142SJason Gunthorpe 	.owner = THIS_MODULE,
3945b9560a41SJason Gunthorpe 	.driver_id = RDMA_DRIVER_MLX5,
394672c6ec18SJason Gunthorpe 	.uverbs_abi_ver	= MLX5_IB_UVERBS_ABI_VERSION,
3947b9560a41SJason Gunthorpe 
394896458233SKamal Heib 	.add_gid = mlx5_ib_add_gid,
394996458233SKamal Heib 	.alloc_mr = mlx5_ib_alloc_mr,
39506c984472SMax Gurtovoy 	.alloc_mr_integrity = mlx5_ib_alloc_mr_integrity,
395196458233SKamal Heib 	.alloc_pd = mlx5_ib_alloc_pd,
395296458233SKamal Heib 	.alloc_ucontext = mlx5_ib_alloc_ucontext,
395396458233SKamal Heib 	.attach_mcast = mlx5_ib_mcg_attach,
395496458233SKamal Heib 	.check_mr_status = mlx5_ib_check_mr_status,
395596458233SKamal Heib 	.create_ah = mlx5_ib_create_ah,
395696458233SKamal Heib 	.create_cq = mlx5_ib_create_cq,
395796458233SKamal Heib 	.create_qp = mlx5_ib_create_qp,
395896458233SKamal Heib 	.create_srq = mlx5_ib_create_srq,
3959676a80adSJason Gunthorpe 	.create_user_ah = mlx5_ib_create_ah,
396096458233SKamal Heib 	.dealloc_pd = mlx5_ib_dealloc_pd,
396196458233SKamal Heib 	.dealloc_ucontext = mlx5_ib_dealloc_ucontext,
396296458233SKamal Heib 	.del_gid = mlx5_ib_del_gid,
396396458233SKamal Heib 	.dereg_mr = mlx5_ib_dereg_mr,
396496458233SKamal Heib 	.destroy_ah = mlx5_ib_destroy_ah,
396596458233SKamal Heib 	.destroy_cq = mlx5_ib_destroy_cq,
396696458233SKamal Heib 	.destroy_qp = mlx5_ib_destroy_qp,
396796458233SKamal Heib 	.destroy_srq = mlx5_ib_destroy_srq,
396896458233SKamal Heib 	.detach_mcast = mlx5_ib_mcg_detach,
396996458233SKamal Heib 	.disassociate_ucontext = mlx5_ib_disassociate_ucontext,
397096458233SKamal Heib 	.drain_rq = mlx5_ib_drain_rq,
397196458233SKamal Heib 	.drain_sq = mlx5_ib_drain_sq,
397211f552e2SMichael Guralnik 	.enable_driver = mlx5_ib_enable_driver,
397396458233SKamal Heib 	.get_dev_fw_str = get_dev_fw_str,
397496458233SKamal Heib 	.get_dma_mr = mlx5_ib_get_dma_mr,
397596458233SKamal Heib 	.get_link_layer = mlx5_ib_port_link_layer,
397696458233SKamal Heib 	.map_mr_sg = mlx5_ib_map_mr_sg,
39776c984472SMax Gurtovoy 	.map_mr_sg_pi = mlx5_ib_map_mr_sg_pi,
397896458233SKamal Heib 	.mmap = mlx5_ib_mmap,
3979dc2316ebSYishai Hadas 	.mmap_free = mlx5_ib_mmap_free,
398096458233SKamal Heib 	.modify_cq = mlx5_ib_modify_cq,
398196458233SKamal Heib 	.modify_device = mlx5_ib_modify_device,
398296458233SKamal Heib 	.modify_port = mlx5_ib_modify_port,
398396458233SKamal Heib 	.modify_qp = mlx5_ib_modify_qp,
398496458233SKamal Heib 	.modify_srq = mlx5_ib_modify_srq,
398596458233SKamal Heib 	.poll_cq = mlx5_ib_poll_cq,
3986029e88fdSLeon Romanovsky 	.post_recv = mlx5_ib_post_recv_nodrain,
3987029e88fdSLeon Romanovsky 	.post_send = mlx5_ib_post_send_nodrain,
398896458233SKamal Heib 	.post_srq_recv = mlx5_ib_post_srq_recv,
398996458233SKamal Heib 	.process_mad = mlx5_ib_process_mad,
399096458233SKamal Heib 	.query_ah = mlx5_ib_query_ah,
399196458233SKamal Heib 	.query_device = mlx5_ib_query_device,
399296458233SKamal Heib 	.query_gid = mlx5_ib_query_gid,
399396458233SKamal Heib 	.query_pkey = mlx5_ib_query_pkey,
399496458233SKamal Heib 	.query_qp = mlx5_ib_query_qp,
399596458233SKamal Heib 	.query_srq = mlx5_ib_query_srq,
39960fb556b2SYishai Hadas 	.query_ucontext = mlx5_ib_query_ucontext,
399796458233SKamal Heib 	.reg_user_mr = mlx5_ib_reg_user_mr,
399890da7dc8SJianxin Xiong 	.reg_user_mr_dmabuf = mlx5_ib_reg_user_mr_dmabuf,
399996458233SKamal Heib 	.req_notify_cq = mlx5_ib_arm_cq,
400096458233SKamal Heib 	.rereg_user_mr = mlx5_ib_rereg_user_mr,
400196458233SKamal Heib 	.resize_cq = mlx5_ib_resize_cq,
4002d3456914SLeon Romanovsky 
4003d3456914SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ah, mlx5_ib_ah, ibah),
40043b023e1bSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_counters, mlx5_ib_mcounters, ibcntrs),
4005e39afe3dSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_cq, mlx5_ib_cq, ibcq),
400621a428a0SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_pd, mlx5_ib_pd, ibpd),
400768e326deSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_srq, mlx5_ib_srq, ibsrq),
4008a2a074efSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_ucontext, mlx5_ib_ucontext, ibucontext),
400996458233SKamal Heib };
401096458233SKamal Heib 
401196458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_ipoib_enhanced_ops = {
401296458233SKamal Heib 	.rdma_netdev_get_params = mlx5_ib_rn_get_params,
401396458233SKamal Heib };
401496458233SKamal Heib 
401596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_sriov_ops = {
401696458233SKamal Heib 	.get_vf_config = mlx5_ib_get_vf_config,
40179c0015efSDanit Goldberg 	.get_vf_guid = mlx5_ib_get_vf_guid,
401896458233SKamal Heib 	.get_vf_stats = mlx5_ib_get_vf_stats,
401996458233SKamal Heib 	.set_vf_guid = mlx5_ib_set_vf_guid,
402096458233SKamal Heib 	.set_vf_link_state = mlx5_ib_set_vf_link_state,
402196458233SKamal Heib };
402296458233SKamal Heib 
402396458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_mw_ops = {
402496458233SKamal Heib 	.alloc_mw = mlx5_ib_alloc_mw,
402596458233SKamal Heib 	.dealloc_mw = mlx5_ib_dealloc_mw,
4026d18bb3e1SLeon Romanovsky 
4027d18bb3e1SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_mw, mlx5_ib_mw, ibmw),
402896458233SKamal Heib };
402996458233SKamal Heib 
403096458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_xrc_ops = {
403196458233SKamal Heib 	.alloc_xrcd = mlx5_ib_alloc_xrcd,
403296458233SKamal Heib 	.dealloc_xrcd = mlx5_ib_dealloc_xrcd,
403328ad5f65SLeon Romanovsky 
403428ad5f65SLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_xrcd, mlx5_ib_xrcd, ibxrcd),
403596458233SKamal Heib };
403696458233SKamal Heib 
403796458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_dm_ops = {
403896458233SKamal Heib 	.alloc_dm = mlx5_ib_alloc_dm,
403996458233SKamal Heib 	.dealloc_dm = mlx5_ib_dealloc_dm,
404096458233SKamal Heib 	.reg_dm_mr = mlx5_ib_reg_dm_mr,
404196458233SKamal Heib };
404296458233SKamal Heib 
4043f164be8cSYishai Hadas static int mlx5_ib_init_var_table(struct mlx5_ib_dev *dev)
4044f164be8cSYishai Hadas {
4045f164be8cSYishai Hadas 	struct mlx5_core_dev *mdev = dev->mdev;
4046f164be8cSYishai Hadas 	struct mlx5_var_table *var_table = &dev->var_table;
4047f164be8cSYishai Hadas 	u8 log_doorbell_bar_size;
4048f164be8cSYishai Hadas 	u8 log_doorbell_stride;
4049f164be8cSYishai Hadas 	u64 bar_size;
4050f164be8cSYishai Hadas 
4051f164be8cSYishai Hadas 	log_doorbell_bar_size = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
4052f164be8cSYishai Hadas 					log_doorbell_bar_size);
4053f164be8cSYishai Hadas 	log_doorbell_stride = MLX5_CAP_DEV_VDPA_EMULATION(mdev,
4054f164be8cSYishai Hadas 					log_doorbell_stride);
4055f164be8cSYishai Hadas 	var_table->hw_start_addr = dev->mdev->bar_addr +
4056f164be8cSYishai Hadas 				MLX5_CAP64_DEV_VDPA_EMULATION(mdev,
4057f164be8cSYishai Hadas 					doorbell_bar_offset);
4058f164be8cSYishai Hadas 	bar_size = (1ULL << log_doorbell_bar_size) * 4096;
4059f164be8cSYishai Hadas 	var_table->stride_size = 1ULL << log_doorbell_stride;
406091b74bf5SAlexander Lobakin 	var_table->num_var_hw_entries = div_u64(bar_size,
406191b74bf5SAlexander Lobakin 						var_table->stride_size);
4062f164be8cSYishai Hadas 	mutex_init(&var_table->bitmap_lock);
4063f164be8cSYishai Hadas 	var_table->bitmap = bitmap_zalloc(var_table->num_var_hw_entries,
4064f164be8cSYishai Hadas 					  GFP_KERNEL);
4065f164be8cSYishai Hadas 	return (var_table->bitmap) ? 0 : -ENOMEM;
4066f164be8cSYishai Hadas }
4067f164be8cSYishai Hadas 
4068f164be8cSYishai Hadas static void mlx5_ib_stage_caps_cleanup(struct mlx5_ib_dev *dev)
4069f164be8cSYishai Hadas {
4070f164be8cSYishai Hadas 	bitmap_free(dev->var_table.bitmap);
4071f164be8cSYishai Hadas }
4072f164be8cSYishai Hadas 
4073fb652d32SMark Bloch static int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
407416c1975fSMark Bloch {
407516c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
407616c1975fSMark Bloch 	int err;
407716c1975fSMark Bloch 
4078f6a8a19bSDenis Drozdov 	if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
4079f6a8a19bSDenis Drozdov 	    IS_ENABLED(CONFIG_MLX5_CORE_IPOIB))
408096458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev,
408196458233SKamal Heib 				  &mlx5_ib_dev_ipoib_enhanced_ops);
40828e959601SNiranjana Vishwanathapura 
408396458233SKamal Heib 	if (mlx5_core_is_pf(mdev))
408496458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_sriov_ops);
40857c2344c3SMaor Gottlieb 
40866e8484c5SMax Gurtovoy 	dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
40876e8484c5SMax Gurtovoy 
408844ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, imaicl))
408996458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_mw_ops);
4090d2370e0aSMatan Barak 
409144ce37bcSJason Gunthorpe 	if (MLX5_CAP_GEN(mdev, xrc))
409296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_xrc_ops);
4093e126ba97SEli Cohen 
409425c13324SAriel Levkovich 	if (MLX5_CAP_DEV_MEM(mdev, memic) ||
409525c13324SAriel Levkovich 	    MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
409625c13324SAriel Levkovich 	    MLX5_GENERAL_OBJ_TYPES_CAP_SW_ICM)
409796458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_dm_ops);
409824da0016SAriel Levkovich 
409996458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_ops);
410081e30880SYishai Hadas 
410136e235c8SJason Gunthorpe 	if (IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS))
410236e235c8SJason Gunthorpe 		dev->ib_dev.driver_def = mlx5_ib_defs;
410381e30880SYishai Hadas 
4104e126ba97SEli Cohen 	err = init_node_data(dev);
4105e126ba97SEli Cohen 	if (err)
410616c1975fSMark Bloch 		return err;
4107e126ba97SEli Cohen 
4108c8b89924SMark Bloch 	if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
4109e7996a9aSJason Gunthorpe 	    (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
4110e7996a9aSJason Gunthorpe 	     MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
4111a560f1d9SMark Bloch 		mutex_init(&dev->lb.mutex);
4112c8b89924SMark Bloch 
4113f164be8cSYishai Hadas 	if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
4114f164be8cSYishai Hadas 			MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q) {
4115f164be8cSYishai Hadas 		err = mlx5_ib_init_var_table(dev);
4116f164be8cSYishai Hadas 		if (err)
4117f164be8cSYishai Hadas 			return err;
4118f164be8cSYishai Hadas 	}
4119f164be8cSYishai Hadas 
412096e2fd73SLeon Romanovsky 	dev->ib_dev.use_cq_dim = true;
412196e2fd73SLeon Romanovsky 
412216c1975fSMark Bloch 	return 0;
412316c1975fSMark Bloch }
412416c1975fSMark Bloch 
412596458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_ops = {
412696458233SKamal Heib 	.get_port_immutable = mlx5_port_immutable,
412796458233SKamal Heib 	.query_port = mlx5_ib_query_port,
412896458233SKamal Heib };
412996458233SKamal Heib 
41308e6efa3aSMark Bloch static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
413116c1975fSMark Bloch {
413296458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_ops);
41338e6efa3aSMark Bloch 	return 0;
41348e6efa3aSMark Bloch }
41358e6efa3aSMark Bloch 
413696458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_port_rep_ops = {
413796458233SKamal Heib 	.get_port_immutable = mlx5_port_rep_immutable,
413896458233SKamal Heib 	.query_port = mlx5_ib_rep_query_port,
4139d6fd59e1SParav Pandit 	.query_pkey = mlx5_ib_rep_query_pkey,
414096458233SKamal Heib };
414196458233SKamal Heib 
4142b5a498baSMichael Guralnik static int mlx5_ib_stage_raw_eth_non_default_cb(struct mlx5_ib_dev *dev)
41438e6efa3aSMark Bloch {
414496458233SKamal Heib 	ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_port_rep_ops);
41458e6efa3aSMark Bloch 	return 0;
41468e6efa3aSMark Bloch }
41478e6efa3aSMark Bloch 
414896458233SKamal Heib static const struct ib_device_ops mlx5_ib_dev_common_roce_ops = {
414996458233SKamal Heib 	.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table,
415096458233SKamal Heib 	.create_wq = mlx5_ib_create_wq,
415196458233SKamal Heib 	.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table,
415296458233SKamal Heib 	.destroy_wq = mlx5_ib_destroy_wq,
415396458233SKamal Heib 	.get_netdev = mlx5_ib_get_netdev,
415496458233SKamal Heib 	.modify_wq = mlx5_ib_modify_wq,
4155c0a6b5ecSLeon Romanovsky 
4156c0a6b5ecSLeon Romanovsky 	INIT_RDMA_OBJ_SIZE(ib_rwq_ind_table, mlx5_ib_rwq_ind_table,
4157c0a6b5ecSLeon Romanovsky 			   ib_rwq_ind_tbl),
415896458233SKamal Heib };
415996458233SKamal Heib 
41601e2b5a90SLeon Romanovsky static int mlx5_ib_roce_init(struct mlx5_ib_dev *dev)
41618e6efa3aSMark Bloch {
41621e2b5a90SLeon Romanovsky 	struct mlx5_core_dev *mdev = dev->mdev;
41631e2b5a90SLeon Romanovsky 	enum rdma_link_layer ll;
41641e2b5a90SLeon Romanovsky 	int port_type_cap;
41651e2b5a90SLeon Romanovsky 	u8 port_num = 0;
41661e2b5a90SLeon Romanovsky 	int err;
41677fd8aefbSDaniel Jurgens 
41681e2b5a90SLeon Romanovsky 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
41691e2b5a90SLeon Romanovsky 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
41701e2b5a90SLeon Romanovsky 
41711e2b5a90SLeon Romanovsky 	if (ll == IB_LINK_LAYER_ETHERNET) {
417296458233SKamal Heib 		ib_set_device_ops(&dev->ib_dev, &mlx5_ib_dev_common_roce_ops);
41738e6efa3aSMark Bloch 
4174e3f1ed1fSLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
4175e3f1ed1fSLeon Romanovsky 
417626628e2dSMark Bloch 		/* Register only for native ports */
41771e2b5a90SLeon Romanovsky 		err = mlx5_add_netdev_notifier(dev, port_num);
41787852546fSMaor Gottlieb 		if (err || dev->is_rep || !mlx5_is_roce_init_enabled(mdev))
41791e2b5a90SLeon Romanovsky 			/*
41801e2b5a90SLeon Romanovsky 			 * We don't enable ETH interface for
41811e2b5a90SLeon Romanovsky 			 * 1. IB representors
41821e2b5a90SLeon Romanovsky 			 * 2. User disabled ROCE through devlink interface
41831e2b5a90SLeon Romanovsky 			 */
41848e6efa3aSMark Bloch 			return err;
41858e6efa3aSMark Bloch 
4186e3f1ed1fSLeon Romanovsky 		err = mlx5_enable_eth(dev);
41878e6efa3aSMark Bloch 		if (err)
41888e6efa3aSMark Bloch 			goto cleanup;
41898e6efa3aSMark Bloch 	}
41908e6efa3aSMark Bloch 
419116c1975fSMark Bloch 	return 0;
41928e6efa3aSMark Bloch cleanup:
41931e2b5a90SLeon Romanovsky 	mlx5_remove_netdev_notifier(dev, port_num);
41948e6efa3aSMark Bloch 	return err;
419545bded2cSKamal Heib }
41966aec21f6SHaggai Eran 
41971e2b5a90SLeon Romanovsky static void mlx5_ib_roce_cleanup(struct mlx5_ib_dev *dev)
419816c1975fSMark Bloch {
419916c1975fSMark Bloch 	struct mlx5_core_dev *mdev = dev->mdev;
420016c1975fSMark Bloch 	enum rdma_link_layer ll;
420116c1975fSMark Bloch 	int port_type_cap;
42021e2b5a90SLeon Romanovsky 	u8 port_num;
42034a2da0b8SParav Pandit 
420416c1975fSMark Bloch 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
420516c1975fSMark Bloch 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
420616c1975fSMark Bloch 
420716c1975fSMark Bloch 	if (ll == IB_LINK_LAYER_ETHERNET) {
42081e2b5a90SLeon Romanovsky 		if (!dev->is_rep)
420916c1975fSMark Bloch 			mlx5_disable_eth(dev);
421016c1975fSMark Bloch 
42111e2b5a90SLeon Romanovsky 		port_num = mlx5_core_native_port_num(dev->mdev) - 1;
42121e2b5a90SLeon Romanovsky 		mlx5_remove_netdev_notifier(dev, port_num);
421316c1975fSMark Bloch 	}
421416c1975fSMark Bloch }
421516c1975fSMark Bloch 
421616c1975fSMark Bloch static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
421716c1975fSMark Bloch {
421873eb8f03SGreg Kroah-Hartman 	mlx5_ib_init_cong_debugfs(dev,
4219a9e546e7SParav Pandit 				  mlx5_core_native_port_num(dev->mdev) - 1);
422073eb8f03SGreg Kroah-Hartman 	return 0;
422116c1975fSMark Bloch }
422216c1975fSMark Bloch 
422316c1975fSMark Bloch static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
422416c1975fSMark Bloch {
4225a9e546e7SParav Pandit 	mlx5_ib_cleanup_cong_debugfs(dev,
4226a9e546e7SParav Pandit 				     mlx5_core_native_port_num(dev->mdev) - 1);
422716c1975fSMark Bloch }
422816c1975fSMark Bloch 
422916c1975fSMark Bloch static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
423016c1975fSMark Bloch {
42315fe9dec0SEli Cohen 	dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
4232444261caSLeon Romanovsky 	return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
423316c1975fSMark Bloch }
423416c1975fSMark Bloch 
423516c1975fSMark Bloch static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
423616c1975fSMark Bloch {
423716c1975fSMark Bloch 	mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
423816c1975fSMark Bloch }
423916c1975fSMark Bloch 
4240fb652d32SMark Bloch static int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
424116c1975fSMark Bloch {
424216c1975fSMark Bloch 	int err;
42435fe9dec0SEli Cohen 
42445fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
42455fe9dec0SEli Cohen 	if (err)
424616c1975fSMark Bloch 		return err;
42475fe9dec0SEli Cohen 
42485fe9dec0SEli Cohen 	err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
42495fe9dec0SEli Cohen 	if (err)
42501c3aa6bdSMark Bloch 		mlx5_free_bfreg(dev->mdev, &dev->bfreg);
42515fe9dec0SEli Cohen 
425216c1975fSMark Bloch 	return err;
425316c1975fSMark Bloch }
42540837e86aSMark Bloch 
4255fb652d32SMark Bloch static void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
425616c1975fSMark Bloch {
425716c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
425816c1975fSMark Bloch 	mlx5_free_bfreg(dev->mdev, &dev->bfreg);
425916c1975fSMark Bloch }
4260e126ba97SEli Cohen 
4261fb652d32SMark Bloch static int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
426216c1975fSMark Bloch {
4263e349f858SJason Gunthorpe 	const char *name;
4264e349f858SJason Gunthorpe 
4265508a523fSParav Pandit 	rdma_set_device_sysfs_group(&dev->ib_dev, &mlx5_attr_group);
42667c34ec19SAviv Heller 	if (!mlx5_lag_is_roce(dev->mdev))
4267e349f858SJason Gunthorpe 		name = "mlx5_%d";
4268e349f858SJason Gunthorpe 	else
4269e349f858SJason Gunthorpe 		name = "mlx5_bond_%d";
4270e0477b34SJason Gunthorpe 	return ib_register_device(&dev->ib_dev, name, &dev->mdev->pdev->dev);
427116c1975fSMark Bloch }
427216c1975fSMark Bloch 
4273fb652d32SMark Bloch static void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
427442cea83fSMark Bloch {
42751e2b5a90SLeon Romanovsky 	int err;
42761e2b5a90SLeon Romanovsky 
42771e2b5a90SLeon Romanovsky 	err = mlx5_mr_cache_cleanup(dev);
42781e2b5a90SLeon Romanovsky 	if (err)
42791e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache cleanup failed\n");
42801e2b5a90SLeon Romanovsky 
42811e2b5a90SLeon Romanovsky 	if (dev->umrc.qp)
42821e2b5a90SLeon Romanovsky 		mlx5_ib_destroy_qp(dev->umrc.qp, NULL);
42831e2b5a90SLeon Romanovsky 	if (dev->umrc.cq)
42841e2b5a90SLeon Romanovsky 		ib_free_cq(dev->umrc.cq);
42851e2b5a90SLeon Romanovsky 	if (dev->umrc.pd)
42861e2b5a90SLeon Romanovsky 		ib_dealloc_pd(dev->umrc.pd);
428742cea83fSMark Bloch }
428842cea83fSMark Bloch 
4289fb652d32SMark Bloch static void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
429016c1975fSMark Bloch {
429116c1975fSMark Bloch 	ib_unregister_device(&dev->ib_dev);
429216c1975fSMark Bloch }
429316c1975fSMark Bloch 
42941e2b5a90SLeon Romanovsky enum {
42951e2b5a90SLeon Romanovsky 	MAX_UMR_WR = 128,
42961e2b5a90SLeon Romanovsky };
42971e2b5a90SLeon Romanovsky 
4298fb652d32SMark Bloch static int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
429916c1975fSMark Bloch {
43001e2b5a90SLeon Romanovsky 	struct ib_qp_init_attr *init_attr = NULL;
43011e2b5a90SLeon Romanovsky 	struct ib_qp_attr *attr = NULL;
43021e2b5a90SLeon Romanovsky 	struct ib_pd *pd;
43031e2b5a90SLeon Romanovsky 	struct ib_cq *cq;
43041e2b5a90SLeon Romanovsky 	struct ib_qp *qp;
43051e2b5a90SLeon Romanovsky 	int ret;
43061e2b5a90SLeon Romanovsky 
43071e2b5a90SLeon Romanovsky 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
43081e2b5a90SLeon Romanovsky 	init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
43091e2b5a90SLeon Romanovsky 	if (!attr || !init_attr) {
43101e2b5a90SLeon Romanovsky 		ret = -ENOMEM;
43111e2b5a90SLeon Romanovsky 		goto error_0;
43121e2b5a90SLeon Romanovsky 	}
43131e2b5a90SLeon Romanovsky 
43141e2b5a90SLeon Romanovsky 	pd = ib_alloc_pd(&dev->ib_dev, 0);
43151e2b5a90SLeon Romanovsky 	if (IS_ERR(pd)) {
43161e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
43171e2b5a90SLeon Romanovsky 		ret = PTR_ERR(pd);
43181e2b5a90SLeon Romanovsky 		goto error_0;
43191e2b5a90SLeon Romanovsky 	}
43201e2b5a90SLeon Romanovsky 
43211e2b5a90SLeon Romanovsky 	cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
43221e2b5a90SLeon Romanovsky 	if (IS_ERR(cq)) {
43231e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
43241e2b5a90SLeon Romanovsky 		ret = PTR_ERR(cq);
43251e2b5a90SLeon Romanovsky 		goto error_2;
43261e2b5a90SLeon Romanovsky 	}
43271e2b5a90SLeon Romanovsky 
43281e2b5a90SLeon Romanovsky 	init_attr->send_cq = cq;
43291e2b5a90SLeon Romanovsky 	init_attr->recv_cq = cq;
43301e2b5a90SLeon Romanovsky 	init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
43311e2b5a90SLeon Romanovsky 	init_attr->cap.max_send_wr = MAX_UMR_WR;
43321e2b5a90SLeon Romanovsky 	init_attr->cap.max_send_sge = 1;
43331e2b5a90SLeon Romanovsky 	init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
43341e2b5a90SLeon Romanovsky 	init_attr->port_num = 1;
43351e2b5a90SLeon Romanovsky 	qp = mlx5_ib_create_qp(pd, init_attr, NULL);
43361e2b5a90SLeon Romanovsky 	if (IS_ERR(qp)) {
43371e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
43381e2b5a90SLeon Romanovsky 		ret = PTR_ERR(qp);
43391e2b5a90SLeon Romanovsky 		goto error_3;
43401e2b5a90SLeon Romanovsky 	}
43411e2b5a90SLeon Romanovsky 	qp->device     = &dev->ib_dev;
43421e2b5a90SLeon Romanovsky 	qp->real_qp    = qp;
43431e2b5a90SLeon Romanovsky 	qp->uobject    = NULL;
43441e2b5a90SLeon Romanovsky 	qp->qp_type    = MLX5_IB_QPT_REG_UMR;
43451e2b5a90SLeon Romanovsky 	qp->send_cq    = init_attr->send_cq;
43461e2b5a90SLeon Romanovsky 	qp->recv_cq    = init_attr->recv_cq;
43471e2b5a90SLeon Romanovsky 
43481e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_INIT;
43491e2b5a90SLeon Romanovsky 	attr->port_num = 1;
43501e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
43511e2b5a90SLeon Romanovsky 				IB_QP_PORT, NULL);
43521e2b5a90SLeon Romanovsky 	if (ret) {
43531e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
43541e2b5a90SLeon Romanovsky 		goto error_4;
43551e2b5a90SLeon Romanovsky 	}
43561e2b5a90SLeon Romanovsky 
43571e2b5a90SLeon Romanovsky 	memset(attr, 0, sizeof(*attr));
43581e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_RTR;
43591e2b5a90SLeon Romanovsky 	attr->path_mtu = IB_MTU_256;
43601e2b5a90SLeon Romanovsky 
43611e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
43621e2b5a90SLeon Romanovsky 	if (ret) {
43631e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
43641e2b5a90SLeon Romanovsky 		goto error_4;
43651e2b5a90SLeon Romanovsky 	}
43661e2b5a90SLeon Romanovsky 
43671e2b5a90SLeon Romanovsky 	memset(attr, 0, sizeof(*attr));
43681e2b5a90SLeon Romanovsky 	attr->qp_state = IB_QPS_RTS;
43691e2b5a90SLeon Romanovsky 	ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
43701e2b5a90SLeon Romanovsky 	if (ret) {
43711e2b5a90SLeon Romanovsky 		mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
43721e2b5a90SLeon Romanovsky 		goto error_4;
43731e2b5a90SLeon Romanovsky 	}
43741e2b5a90SLeon Romanovsky 
43751e2b5a90SLeon Romanovsky 	dev->umrc.qp = qp;
43761e2b5a90SLeon Romanovsky 	dev->umrc.cq = cq;
43771e2b5a90SLeon Romanovsky 	dev->umrc.pd = pd;
43781e2b5a90SLeon Romanovsky 
43791e2b5a90SLeon Romanovsky 	sema_init(&dev->umrc.sem, MAX_UMR_WR);
43801e2b5a90SLeon Romanovsky 	ret = mlx5_mr_cache_init(dev);
43811e2b5a90SLeon Romanovsky 	if (ret) {
43821e2b5a90SLeon Romanovsky 		mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
43831e2b5a90SLeon Romanovsky 		goto error_4;
43841e2b5a90SLeon Romanovsky 	}
43851e2b5a90SLeon Romanovsky 
43861e2b5a90SLeon Romanovsky 	kfree(attr);
43871e2b5a90SLeon Romanovsky 	kfree(init_attr);
43881e2b5a90SLeon Romanovsky 
43891e2b5a90SLeon Romanovsky 	return 0;
43901e2b5a90SLeon Romanovsky 
43911e2b5a90SLeon Romanovsky error_4:
43921e2b5a90SLeon Romanovsky 	mlx5_ib_destroy_qp(qp, NULL);
43931e2b5a90SLeon Romanovsky 	dev->umrc.qp = NULL;
43941e2b5a90SLeon Romanovsky 
43951e2b5a90SLeon Romanovsky error_3:
43961e2b5a90SLeon Romanovsky 	ib_free_cq(cq);
43971e2b5a90SLeon Romanovsky 	dev->umrc.cq = NULL;
43981e2b5a90SLeon Romanovsky 
43991e2b5a90SLeon Romanovsky error_2:
44001e2b5a90SLeon Romanovsky 	ib_dealloc_pd(pd);
44011e2b5a90SLeon Romanovsky 	dev->umrc.pd = NULL;
44021e2b5a90SLeon Romanovsky 
44031e2b5a90SLeon Romanovsky error_0:
44041e2b5a90SLeon Romanovsky 	kfree(attr);
44051e2b5a90SLeon Romanovsky 	kfree(init_attr);
44061e2b5a90SLeon Romanovsky 	return ret;
440716c1975fSMark Bloch }
440816c1975fSMark Bloch 
440916c1975fSMark Bloch static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
441016c1975fSMark Bloch {
44111e2b5a90SLeon Romanovsky 	struct dentry *root;
441203404e8aSMaor Gottlieb 
44131e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
44141e2b5a90SLeon Romanovsky 		return 0;
44151e2b5a90SLeon Romanovsky 
44161e2b5a90SLeon Romanovsky 	mutex_init(&dev->delay_drop.lock);
44171e2b5a90SLeon Romanovsky 	dev->delay_drop.dev = dev;
44181e2b5a90SLeon Romanovsky 	dev->delay_drop.activate = false;
44191e2b5a90SLeon Romanovsky 	dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
44201e2b5a90SLeon Romanovsky 	INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
44211e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.rqs_cnt, 0);
44221e2b5a90SLeon Romanovsky 	atomic_set(&dev->delay_drop.events_cnt, 0);
44231e2b5a90SLeon Romanovsky 
44241e2b5a90SLeon Romanovsky 	if (!mlx5_debugfs_root)
44251e2b5a90SLeon Romanovsky 		return 0;
44261e2b5a90SLeon Romanovsky 
44271e2b5a90SLeon Romanovsky 	root = debugfs_create_dir("delay_drop", dev->mdev->priv.dbg_root);
44281e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = root;
44291e2b5a90SLeon Romanovsky 
44301e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_timeout_events", 0400, root,
44311e2b5a90SLeon Romanovsky 				&dev->delay_drop.events_cnt);
44321e2b5a90SLeon Romanovsky 	debugfs_create_atomic_t("num_rqs", 0400, root,
44331e2b5a90SLeon Romanovsky 				&dev->delay_drop.rqs_cnt);
44341e2b5a90SLeon Romanovsky 	debugfs_create_file("timeout", 0600, root, &dev->delay_drop,
44351e2b5a90SLeon Romanovsky 			    &fops_delay_drop_timeout);
443616c1975fSMark Bloch 	return 0;
443716c1975fSMark Bloch }
443816c1975fSMark Bloch 
443916c1975fSMark Bloch static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
444016c1975fSMark Bloch {
44411e2b5a90SLeon Romanovsky 	if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
44421e2b5a90SLeon Romanovsky 		return;
44431e2b5a90SLeon Romanovsky 
44441e2b5a90SLeon Romanovsky 	cancel_work_sync(&dev->delay_drop.delay_drop_work);
44451e2b5a90SLeon Romanovsky 	if (!dev->delay_drop.dir_debugfs)
44461e2b5a90SLeon Romanovsky 		return;
44471e2b5a90SLeon Romanovsky 
44481e2b5a90SLeon Romanovsky 	debugfs_remove_recursive(dev->delay_drop.dir_debugfs);
44491e2b5a90SLeon Romanovsky 	dev->delay_drop.dir_debugfs = NULL;
445016c1975fSMark Bloch }
445116c1975fSMark Bloch 
4452df097a27SSaeed Mahameed static int mlx5_ib_stage_dev_notifier_init(struct mlx5_ib_dev *dev)
4453df097a27SSaeed Mahameed {
4454df097a27SSaeed Mahameed 	dev->mdev_events.notifier_call = mlx5_ib_event;
4455df097a27SSaeed Mahameed 	mlx5_notifier_register(dev->mdev, &dev->mdev_events);
4456df097a27SSaeed Mahameed 	return 0;
4457df097a27SSaeed Mahameed }
4458df097a27SSaeed Mahameed 
4459df097a27SSaeed Mahameed static void mlx5_ib_stage_dev_notifier_cleanup(struct mlx5_ib_dev *dev)
4460df097a27SSaeed Mahameed {
4461df097a27SSaeed Mahameed 	mlx5_notifier_unregister(dev->mdev, &dev->mdev_events);
4462df097a27SSaeed Mahameed }
4463df097a27SSaeed Mahameed 
4464b5ca15adSMark Bloch void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
446516c1975fSMark Bloch 		      const struct mlx5_ib_profile *profile,
446616c1975fSMark Bloch 		      int stage)
446716c1975fSMark Bloch {
44684cca96a8SParav Pandit 	dev->ib_active = false;
44694cca96a8SParav Pandit 
447016c1975fSMark Bloch 	/* Number of stages to cleanup */
447116c1975fSMark Bloch 	while (stage) {
447216c1975fSMark Bloch 		stage--;
447316c1975fSMark Bloch 		if (profile->stage[stage].cleanup)
447416c1975fSMark Bloch 			profile->stage[stage].cleanup(dev);
447516c1975fSMark Bloch 	}
44764a6dc855SMark Bloch 
4477da796ccbSMark Bloch 	kfree(dev->port);
44784a6dc855SMark Bloch 	ib_dealloc_device(&dev->ib_dev);
447916c1975fSMark Bloch }
448016c1975fSMark Bloch 
448193f82444SLeon Romanovsky int __mlx5_ib_add(struct mlx5_ib_dev *dev,
448216c1975fSMark Bloch 		  const struct mlx5_ib_profile *profile)
448316c1975fSMark Bloch {
448416c1975fSMark Bloch 	int err;
448516c1975fSMark Bloch 	int i;
448616c1975fSMark Bloch 
44878d93efb8SMark Bloch 	dev->profile = profile;
44888d93efb8SMark Bloch 
448916c1975fSMark Bloch 	for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
449016c1975fSMark Bloch 		if (profile->stage[i].init) {
449116c1975fSMark Bloch 			err = profile->stage[i].init(dev);
449216c1975fSMark Bloch 			if (err)
449316c1975fSMark Bloch 				goto err_out;
449416c1975fSMark Bloch 		}
449516c1975fSMark Bloch 	}
449616c1975fSMark Bloch 
4497e126ba97SEli Cohen 	dev->ib_active = true;
449893f82444SLeon Romanovsky 	return 0;
4499e126ba97SEli Cohen 
450016c1975fSMark Bloch err_out:
450193f82444SLeon Romanovsky 	/* Clean up stages which were initialized */
450293f82444SLeon Romanovsky 	while (i) {
450393f82444SLeon Romanovsky 		i--;
450493f82444SLeon Romanovsky 		if (profile->stage[i].cleanup)
450593f82444SLeon Romanovsky 			profile->stage[i].cleanup(dev);
450693f82444SLeon Romanovsky 	}
450793f82444SLeon Romanovsky 	return -ENOMEM;
4508e126ba97SEli Cohen }
4509e126ba97SEli Cohen 
451016c1975fSMark Bloch static const struct mlx5_ib_profile pf_profile = {
451116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
451216c1975fSMark Bloch 		     mlx5_ib_stage_init_init,
451316c1975fSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4514f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4515f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4516f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
451716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
451816c1975fSMark Bloch 		     mlx5_ib_stage_caps_init,
4519f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
45208e6efa3aSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
45218e6efa3aSMark Bloch 		     mlx5_ib_stage_non_default_cb,
45228e6efa3aSMark Bloch 		     NULL),
452316c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
45241e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
45251e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4526333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4527333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4528333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4529f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4530f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4531f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
453216c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
45331e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
45341e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4535df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4536df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4537df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
453816c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ODP,
45391e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_init_one,
45401e2b5a90SLeon Romanovsky 		     mlx5_ib_odp_cleanup_one),
454116c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
454264825827SLeon Romanovsky 		     mlx5_ib_counters_init,
454364825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
454416c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
454516c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_init,
454616c1975fSMark Bloch 		     mlx5_ib_stage_cong_debugfs_cleanup),
454716c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
454816c1975fSMark Bloch 		     mlx5_ib_stage_uar_init,
454916c1975fSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
455016c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
455116c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_init,
455216c1975fSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
455342cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
455442cea83fSMark Bloch 		     NULL,
455542cea83fSMark Bloch 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
455681773ce5SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4557d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4558d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
455916c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
456016c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
456116c1975fSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
456242cea83fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
456342cea83fSMark Bloch 		     mlx5_ib_stage_post_ib_reg_umr_init,
456442cea83fSMark Bloch 		     NULL),
456516c1975fSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
456616c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_init,
456716c1975fSMark Bloch 		     mlx5_ib_stage_delay_drop_cleanup),
4568b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4569b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4570b572ebe6SLeon Romanovsky 		     NULL),
457116c1975fSMark Bloch };
457216c1975fSMark Bloch 
4573b5a498baSMichael Guralnik const struct mlx5_ib_profile raw_eth_profile = {
4574b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_INIT,
4575b5ca15adSMark Bloch 		     mlx5_ib_stage_init_init,
4576b5ca15adSMark Bloch 		     mlx5_ib_stage_init_cleanup),
4577f7c4ffdaSLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_FS,
4578f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_init,
4579f7c4ffdaSLeon Romanovsky 		     mlx5_ib_fs_cleanup),
4580b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_CAPS,
4581b5ca15adSMark Bloch 		     mlx5_ib_stage_caps_init,
4582f164be8cSYishai Hadas 		     mlx5_ib_stage_caps_cleanup),
4583b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
4584b5a498baSMichael Guralnik 		     mlx5_ib_stage_raw_eth_non_default_cb,
4585b5ca15adSMark Bloch 		     NULL),
4586b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_ROCE,
45871e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_init,
45881e2b5a90SLeon Romanovsky 		     mlx5_ib_roce_cleanup),
4589333fbaa0SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_QP,
4590333fbaa0SLeon Romanovsky 		     mlx5_init_qp_table,
4591333fbaa0SLeon Romanovsky 		     mlx5_cleanup_qp_table),
4592f3da6577SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_SRQ,
4593f3da6577SLeon Romanovsky 		     mlx5_init_srq_table,
4594f3da6577SLeon Romanovsky 		     mlx5_cleanup_srq_table),
4595b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
45961e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_init,
45971e2b5a90SLeon Romanovsky 		     mlx5_ib_dev_res_cleanup),
4598df097a27SSaeed Mahameed 	STAGE_CREATE(MLX5_IB_STAGE_DEVICE_NOTIFIER,
4599df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_init,
4600df097a27SSaeed Mahameed 		     mlx5_ib_stage_dev_notifier_cleanup),
4601b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
460264825827SLeon Romanovsky 		     mlx5_ib_counters_init,
460364825827SLeon Romanovsky 		     mlx5_ib_counters_cleanup),
460479db784eSParav Pandit 	STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
460579db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_init,
460679db784eSParav Pandit 		     mlx5_ib_stage_cong_debugfs_cleanup),
4607b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_UAR,
4608b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_init,
4609b5ca15adSMark Bloch 		     mlx5_ib_stage_uar_cleanup),
4610b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_BFREG,
4611b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_init,
4612b5ca15adSMark Bloch 		     mlx5_ib_stage_bfrag_cleanup),
461303fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
461403fe2debSDavid S. Miller 		     NULL,
461503fe2debSDavid S. Miller 		     mlx5_ib_stage_pre_ib_reg_umr_cleanup),
46167f575103SMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_WHITELIST_UID,
4617d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_init,
4618d8b7515eSLeon Romanovsky 		     mlx5_ib_devx_cleanup),
4619b5ca15adSMark Bloch 	STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
4620b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_init,
4621b5ca15adSMark Bloch 		     mlx5_ib_stage_ib_reg_cleanup),
462203fe2debSDavid S. Miller 	STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
462303fe2debSDavid S. Miller 		     mlx5_ib_stage_post_ib_reg_umr_init,
462403fe2debSDavid S. Miller 		     NULL),
4625b572ebe6SLeon Romanovsky 	STAGE_CREATE(MLX5_IB_STAGE_RESTRACK,
4626b572ebe6SLeon Romanovsky 		     mlx5_ib_restrack_init,
4627b572ebe6SLeon Romanovsky 		     NULL),
4628b5ca15adSMark Bloch };
4629b5ca15adSMark Bloch 
463093f82444SLeon Romanovsky static int mlx5r_mp_probe(struct auxiliary_device *adev,
463193f82444SLeon Romanovsky 			  const struct auxiliary_device_id *id)
463232f69e4bSDaniel Jurgens {
463393f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
463493f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
463532f69e4bSDaniel Jurgens 	struct mlx5_ib_multiport_info *mpi;
463632f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
463732f69e4bSDaniel Jurgens 	bool bound = false;
463832f69e4bSDaniel Jurgens 	int err;
463932f69e4bSDaniel Jurgens 
464032f69e4bSDaniel Jurgens 	mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
464132f69e4bSDaniel Jurgens 	if (!mpi)
464293f82444SLeon Romanovsky 		return -ENOMEM;
464332f69e4bSDaniel Jurgens 
464432f69e4bSDaniel Jurgens 	mpi->mdev = mdev;
464532f69e4bSDaniel Jurgens 	err = mlx5_query_nic_vport_system_image_guid(mdev,
464632f69e4bSDaniel Jurgens 						     &mpi->sys_image_guid);
464732f69e4bSDaniel Jurgens 	if (err) {
464832f69e4bSDaniel Jurgens 		kfree(mpi);
464993f82444SLeon Romanovsky 		return err;
465032f69e4bSDaniel Jurgens 	}
465132f69e4bSDaniel Jurgens 
465232f69e4bSDaniel Jurgens 	mutex_lock(&mlx5_ib_multiport_mutex);
465332f69e4bSDaniel Jurgens 	list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
465432f69e4bSDaniel Jurgens 		if (dev->sys_image_guid == mpi->sys_image_guid)
465532f69e4bSDaniel Jurgens 			bound = mlx5_ib_bind_slave_port(dev, mpi);
465632f69e4bSDaniel Jurgens 
465732f69e4bSDaniel Jurgens 		if (bound) {
465832f69e4bSDaniel Jurgens 			rdma_roce_rescan_device(&dev->ib_dev);
465932f69e4bSDaniel Jurgens 			break;
466032f69e4bSDaniel Jurgens 		}
466132f69e4bSDaniel Jurgens 	}
466232f69e4bSDaniel Jurgens 
466332f69e4bSDaniel Jurgens 	if (!bound) {
466432f69e4bSDaniel Jurgens 		list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
4665c42260f1SVu Pham 		dev_dbg(mdev->device,
4666c42260f1SVu Pham 			"no suitable IB device found to bind to, added to unaffiliated list.\n");
466732f69e4bSDaniel Jurgens 	}
466832f69e4bSDaniel Jurgens 	mutex_unlock(&mlx5_ib_multiport_mutex);
466932f69e4bSDaniel Jurgens 
467093f82444SLeon Romanovsky 	dev_set_drvdata(&adev->dev, mpi);
467193f82444SLeon Romanovsky 	return 0;
467232f69e4bSDaniel Jurgens }
467332f69e4bSDaniel Jurgens 
467493f82444SLeon Romanovsky static void mlx5r_mp_remove(struct auxiliary_device *adev)
467516c1975fSMark Bloch {
467693f82444SLeon Romanovsky 	struct mlx5_ib_multiport_info *mpi;
467793f82444SLeon Romanovsky 
467893f82444SLeon Romanovsky 	mpi = dev_get_drvdata(&adev->dev);
467993f82444SLeon Romanovsky 	mutex_lock(&mlx5_ib_multiport_mutex);
468093f82444SLeon Romanovsky 	if (mpi->ibdev)
468193f82444SLeon Romanovsky 		mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
468293f82444SLeon Romanovsky 	list_del(&mpi->list);
468393f82444SLeon Romanovsky 	mutex_unlock(&mlx5_ib_multiport_mutex);
468493f82444SLeon Romanovsky 	kfree(mpi);
468593f82444SLeon Romanovsky }
468693f82444SLeon Romanovsky 
468793f82444SLeon Romanovsky static int mlx5r_probe(struct auxiliary_device *adev,
468893f82444SLeon Romanovsky 		       const struct auxiliary_device_id *id)
468993f82444SLeon Romanovsky {
469093f82444SLeon Romanovsky 	struct mlx5_adev *idev = container_of(adev, struct mlx5_adev, adev);
469193f82444SLeon Romanovsky 	struct mlx5_core_dev *mdev = idev->mdev;
469294de879cSMichael Guralnik 	const struct mlx5_ib_profile *profile;
469393f82444SLeon Romanovsky 	int port_type_cap, num_ports, ret;
469432f69e4bSDaniel Jurgens 	enum rdma_link_layer ll;
4695b5ca15adSMark Bloch 	struct mlx5_ib_dev *dev;
4696f0666f1fSBodong Wang 
469732f69e4bSDaniel Jurgens 	port_type_cap = MLX5_CAP_GEN(mdev, port_type);
469832f69e4bSDaniel Jurgens 	ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
469932f69e4bSDaniel Jurgens 
4700da796ccbSMark Bloch 	num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
4701da796ccbSMark Bloch 			MLX5_CAP_GEN(mdev, num_vhca_ports));
4702459cc69fSLeon Romanovsky 	dev = ib_alloc_device(mlx5_ib_dev, ib_dev);
4703b5ca15adSMark Bloch 	if (!dev)
470493f82444SLeon Romanovsky 		return -ENOMEM;
4705da796ccbSMark Bloch 	dev->port = kcalloc(num_ports, sizeof(*dev->port),
4706da796ccbSMark Bloch 			     GFP_KERNEL);
4707da796ccbSMark Bloch 	if (!dev->port) {
4708a5c9c299SParav Pandit 		ib_dealloc_device(&dev->ib_dev);
470993f82444SLeon Romanovsky 		return -ENOMEM;
4710da796ccbSMark Bloch 	}
4711b5ca15adSMark Bloch 
4712b5ca15adSMark Bloch 	dev->mdev = mdev;
4713da796ccbSMark Bloch 	dev->num_ports = num_ports;
4714b5ca15adSMark Bloch 
47157852546fSMaor Gottlieb 	if (ll == IB_LINK_LAYER_ETHERNET && !mlx5_is_roce_init_enabled(mdev))
471694de879cSMichael Guralnik 		profile = &raw_eth_profile;
471794de879cSMichael Guralnik 	else
471894de879cSMichael Guralnik 		profile = &pf_profile;
471994de879cSMichael Guralnik 
472093f82444SLeon Romanovsky 	ret = __mlx5_ib_add(dev, profile);
472193f82444SLeon Romanovsky 	if (ret) {
472293f82444SLeon Romanovsky 		kfree(dev->port);
472393f82444SLeon Romanovsky 		ib_dealloc_device(&dev->ib_dev);
472493f82444SLeon Romanovsky 		return ret;
472516c1975fSMark Bloch 	}
472616c1975fSMark Bloch 
472793f82444SLeon Romanovsky 	dev_set_drvdata(&adev->dev, dev);
472893f82444SLeon Romanovsky 	return 0;
472993f82444SLeon Romanovsky }
473093f82444SLeon Romanovsky 
473193f82444SLeon Romanovsky static void mlx5r_remove(struct auxiliary_device *adev)
4732e126ba97SEli Cohen {
473332f69e4bSDaniel Jurgens 	struct mlx5_ib_dev *dev;
47346aec21f6SHaggai Eran 
473593f82444SLeon Romanovsky 	dev = dev_get_drvdata(&adev->dev);
473616c1975fSMark Bloch 	__mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
4737e126ba97SEli Cohen }
4738e126ba97SEli Cohen 
473993f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_mp_id_table[] = {
474093f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".multiport", },
474193f82444SLeon Romanovsky 	{},
474293f82444SLeon Romanovsky };
474393f82444SLeon Romanovsky 
474493f82444SLeon Romanovsky static const struct auxiliary_device_id mlx5r_id_table[] = {
474593f82444SLeon Romanovsky 	{ .name = MLX5_ADEV_NAME ".rdma", },
474693f82444SLeon Romanovsky 	{},
474793f82444SLeon Romanovsky };
474893f82444SLeon Romanovsky 
474993f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_mp_id_table);
475093f82444SLeon Romanovsky MODULE_DEVICE_TABLE(auxiliary, mlx5r_id_table);
475193f82444SLeon Romanovsky 
475293f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_mp_driver = {
475393f82444SLeon Romanovsky 	.name = "multiport",
475493f82444SLeon Romanovsky 	.probe = mlx5r_mp_probe,
475593f82444SLeon Romanovsky 	.remove = mlx5r_mp_remove,
475693f82444SLeon Romanovsky 	.id_table = mlx5r_mp_id_table,
475793f82444SLeon Romanovsky };
475893f82444SLeon Romanovsky 
475993f82444SLeon Romanovsky static struct auxiliary_driver mlx5r_driver = {
476093f82444SLeon Romanovsky 	.name = "rdma",
476193f82444SLeon Romanovsky 	.probe = mlx5r_probe,
476293f82444SLeon Romanovsky 	.remove = mlx5r_remove,
476393f82444SLeon Romanovsky 	.id_table = mlx5r_id_table,
4764e126ba97SEli Cohen };
4765e126ba97SEli Cohen 
4766e126ba97SEli Cohen static int __init mlx5_ib_init(void)
4767e126ba97SEli Cohen {
476893f82444SLeon Romanovsky 	int ret;
47696aec21f6SHaggai Eran 
47708010d74bSJason Gunthorpe 	xlt_emergency_page = (void *)__get_free_page(GFP_KERNEL);
4771c44ef998SIlya Lesokhin 	if (!xlt_emergency_page)
4772d69a24e0SDaniel Jurgens 		return -ENOMEM;
4773d69a24e0SDaniel Jurgens 
4774c44ef998SIlya Lesokhin 	mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
4775c44ef998SIlya Lesokhin 	if (!mlx5_ib_event_wq) {
47768010d74bSJason Gunthorpe 		free_page((unsigned long)xlt_emergency_page);
4777c44ef998SIlya Lesokhin 		return -ENOMEM;
4778c44ef998SIlya Lesokhin 	}
4779c44ef998SIlya Lesokhin 
478081713d37SArtemy Kovalyov 	mlx5_ib_odp_init();
478193f82444SLeon Romanovsky 	ret = mlx5r_rep_init();
478293f82444SLeon Romanovsky 	if (ret)
478393f82444SLeon Romanovsky 		goto rep_err;
478493f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_mp_driver);
478593f82444SLeon Romanovsky 	if (ret)
478693f82444SLeon Romanovsky 		goto mp_err;
478793f82444SLeon Romanovsky 	ret = auxiliary_driver_register(&mlx5r_driver);
478893f82444SLeon Romanovsky 	if (ret)
478993f82444SLeon Romanovsky 		goto drv_err;
479093f82444SLeon Romanovsky 	return 0;
47919603b61dSJack Morgenstein 
479293f82444SLeon Romanovsky drv_err:
479393f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
479493f82444SLeon Romanovsky mp_err:
479593f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
479693f82444SLeon Romanovsky rep_err:
479793f82444SLeon Romanovsky 	destroy_workqueue(mlx5_ib_event_wq);
479893f82444SLeon Romanovsky 	free_page((unsigned long)xlt_emergency_page);
479993f82444SLeon Romanovsky 	return ret;
4800e126ba97SEli Cohen }
4801e126ba97SEli Cohen 
4802e126ba97SEli Cohen static void __exit mlx5_ib_cleanup(void)
4803e126ba97SEli Cohen {
480493f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_driver);
480593f82444SLeon Romanovsky 	auxiliary_driver_unregister(&mlx5r_mp_driver);
480693f82444SLeon Romanovsky 	mlx5r_rep_cleanup();
480793f82444SLeon Romanovsky 
4808d69a24e0SDaniel Jurgens 	destroy_workqueue(mlx5_ib_event_wq);
48098010d74bSJason Gunthorpe 	free_page((unsigned long)xlt_emergency_page);
4810e126ba97SEli Cohen }
4811e126ba97SEli Cohen 
4812e126ba97SEli Cohen module_init(mlx5_ib_init);
4813e126ba97SEli Cohen module_exit(mlx5_ib_cleanup);
4814